Hex Artifact Content
Not logged in

Artifact 228761cc28bc0e8fc029d5a5d96f9e6d9db0f1d4:

File src/sqlite3.c part of check-in [a85cc7ce8d] - Update SQLite to version 3.5.9. by drh on 2008-05-15 13:37:49.

0000: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
0010: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0020: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0030: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0040: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
0050: 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20  ** This file is 
0060: 61 6e 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20  an amalgamation 
0070: 6f 66 20 6d 61 6e 79 20 73 65 70 61 72 61 74 65  of many separate
0080: 20 43 20 73 6f 75 72 63 65 20 66 69 6c 65 73 20   C source files 
0090: 66 72 6f 6d 20 53 51 4c 69 74 65 0a 2a 2a 20 76  from SQLite.** v
00a0: 65 72 73 69 6f 6e 20 33 2e 35 2e 39 2e 20 20 42  ersion 3.5.9.  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 35 36 33 38 20 6c 69 6e 65 73  st.** 5638 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 38 2d 30 35 2d 31 34 20 31 36 3a 33 30  2008-05-14 16:30
04c0: 3a 35 32 20 55 54 43 2e 0a 2a 2f 0a 23 64 65 66  :52 UTC..*/.#def
04d0: 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20  ine SQLITE_CORE 
04e0: 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  1.#define SQLITE
04f0: 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 31 0a  _AMALGAMATION 1.
0500: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 50  #ifndef SQLITE_P
0510: 52 49 56 41 54 45 0a 23 20 64 65 66 69 6e 65 20  RIVATE.# define 
0520: 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73  SQLITE_PRIVATE s
0530: 74 61 74 69 63 0a 23 65 6e 64 69 66 0a 23 69 66  tatic.#endif.#if
0540: 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 50 49 0a  ndef SQLITE_API.
0550: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
0560: 41 50 49 0a 23 65 6e 64 69 66 0a 2f 2a 2a 2a 2a  API.#endif./****
0570: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
0580: 20 66 69 6c 65 20 73 71 6c 69 74 65 49 6e 74 2e   file sqliteInt.
0590: 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
05a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
05b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
05c0: 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
05d0: 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
05e0: 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
05f0: 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
0600: 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
0610: 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
0620: 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
0630: 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
0640: 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
0650: 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
0660: 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
0670: 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
0680: 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
0690: 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
06a0: 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
06b0: 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
06c0: 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
06d0: 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
06e0: 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
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 2a 2a 2a 2a 2a  ****************
0720: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0730: 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 69 6e  *.** Internal in
0740: 74 65 72 66 61 63 65 20 64 65 66 69 6e 69 74 69  terface definiti
0750: 6f 6e 73 20 66 6f 72 20 53 51 4c 69 74 65 2e 0a  ons for SQLite..
0760: 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20  **.** @(#) $Id: 
0770: 73 71 6c 69 74 65 49 6e 74 2e 68 2c 76 20 31 2e  sqliteInt.h,v 1.
0780: 37 30 34 20 32 30 30 38 2f 30 35 2f 31 33 20 31  704 2008/05/13 1
0790: 33 3a 32 37 3a 33 34 20 64 72 68 20 45 78 70 20  3:27:34 drh Exp 
07a0: 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51  $.*/.#ifndef _SQ
07b0: 4c 49 54 45 49 4e 54 5f 48 5f 0a 23 64 65 66 69  LITEINT_H_.#defi
07c0: 6e 65 20 5f 53 51 4c 49 54 45 49 4e 54 5f 48 5f  ne _SQLITEINT_H_
07d0: 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20  ../*.** Include 
07e0: 74 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  the configuratio
07f0: 6e 20 68 65 61 64 65 72 20 6f 75 74 70 75 74 20  n header output 
0800: 62 79 20 27 63 6f 6e 66 69 67 75 72 65 27 20 69  by 'configure' i
0810: 66 20 77 65 27 72 65 20 75 73 69 6e 67 20 74 68  f we're using th
0820: 65 0a 2a 2a 20 61 75 74 6f 63 6f 6e 66 2d 62 61  e.** autoconf-ba
0830: 73 65 64 20 62 75 69 6c 64 0a 2a 2f 0a 23 69 66  sed build.*/.#if
0840: 64 65 66 20 5f 48 41 56 45 5f 53 51 4c 49 54 45  def _HAVE_SQLITE
0850: 5f 43 4f 4e 46 49 47 5f 48 0a 23 69 6e 63 6c 75  _CONFIG_H.#inclu
0860: 64 65 20 22 63 6f 6e 66 69 67 2e 68 22 0a 23 65  de "config.h".#e
0870: 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif../*********
0880: 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71  ***** Include sq
0890: 6c 69 74 65 4c 69 6d 69 74 2e 68 20 69 6e 20 74  liteLimit.h in t
08a0: 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c  he middle of sql
08b0: 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
08c0: 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
08d0: 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
08e0: 20 73 71 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a   sqliteLimit.h *
08f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0900: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0910: 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37  ****/./*.** 2007
0920: 20 4d 61 79 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65   May 7.**.** The
0930: 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
0940: 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
0950: 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
0960: 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
0970: 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
0980: 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
0990: 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
09a0: 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
09b0: 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
09c0: 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
09d0: 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
09e0: 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
09f0: 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
0a00: 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
0a10: 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
0a20: 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
0a30: 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
0a40: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0a50: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0a60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0a70: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0a80: 2a 2a 2a 2a 2a 0a 2a 2a 20 0a 2a 2a 20 54 68 69  *****.** .** Thi
0a90: 73 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 76  s file defines v
0aa0: 61 72 69 6f 75 73 20 6c 69 6d 69 74 73 20 6f 66  arious limits of
0ab0: 20 77 68 61 74 20 53 51 4c 69 74 65 20 63 61 6e   what SQLite can
0ac0: 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20   process..**.** 
0ad0: 40 28 23 29 20 24 49 64 3a 20 73 71 6c 69 74 65  @(#) $Id: sqlite
0ae0: 4c 69 6d 69 74 2e 68 2c 76 20 31 2e 38 20 32 30  Limit.h,v 1.8 20
0af0: 30 38 2f 30 33 2f 32 36 20 31 35 3a 35 36 3a 32  08/03/26 15:56:2
0b00: 32 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  2 drh Exp $.*/..
0b10: 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75  /*.** The maximu
0b20: 6d 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 54 45  m length of a TE
0b30: 58 54 20 6f 72 20 42 4c 4f 42 20 69 6e 20 62 79  XT or BLOB in by
0b40: 74 65 73 2e 20 20 20 54 68 69 73 20 61 6c 73 6f  tes.   This also
0b50: 0a 2a 2a 20 6c 69 6d 69 74 73 20 74 68 65 20 73  .** limits the s
0b60: 69 7a 65 20 6f 66 20 61 20 72 6f 77 20 69 6e 20  ize of a row in 
0b70: 61 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  a table or index
0b80: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 61 72 64  ..**.** The hard
0b90: 20 6c 69 6d 69 74 20 69 73 20 74 68 65 20 61 62   limit is the ab
0ba0: 69 6c 69 74 79 20 6f 66 20 61 20 33 32 2d 62 69  ility of a 32-bi
0bb0: 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
0bc0: 0a 2a 2a 20 74 6f 20 63 6f 75 6e 74 20 74 68 65  .** to count the
0bd0: 20 73 69 7a 65 3a 20 32 5e 33 31 2d 31 20 6f 72   size: 2^31-1 or
0be0: 20 32 31 34 37 34 38 33 36 34 37 2e 0a 2a 2f 0a   2147483647..*/.
0bf0: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  #ifndef SQLITE_M
0c00: 41 58 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66 69  AX_LENGTH.# defi
0c10: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45  ne SQLITE_MAX_LE
0c20: 4e 47 54 48 20 31 30 30 30 30 30 30 30 30 30 0a  NGTH 1000000000.
0c30: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
0c40: 69 73 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75  is is the maximu
0c50: 6d 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 0a 2a  m number of.**.*
0c60: 2a 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 69  *    * Columns i
0c70: 6e 20 61 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20  n a table.**    
0c80: 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 20  * Columns in an 
0c90: 69 6e 64 65 78 0a 2a 2a 20 20 20 20 2a 20 43 6f  index.**    * Co
0ca0: 6c 75 6d 6e 73 20 69 6e 20 61 20 76 69 65 77 0a  lumns in a view.
0cb0: 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69 6e  **    * Terms in
0cc0: 20 74 68 65 20 53 45 54 20 63 6c 61 75 73 65 20   the SET clause 
0cd0: 6f 66 20 61 6e 20 55 50 44 41 54 45 20 73 74 61  of an UPDATE sta
0ce0: 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 2a 20 54  tement.**    * T
0cf0: 65 72 6d 73 20 69 6e 20 74 68 65 20 72 65 73 75  erms in the resu
0d00: 6c 74 20 73 65 74 20 6f 66 20 61 20 53 45 4c 45  lt set of a SELE
0d10: 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20  CT statement.** 
0d20: 20 20 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68     * Terms in th
0d30: 65 20 47 52 4f 55 50 20 42 59 20 6f 72 20 4f 52  e GROUP BY or OR
0d40: 44 45 52 20 42 59 20 63 6c 61 75 73 65 73 20 6f  DER BY clauses o
0d50: 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65  f a SELECT state
0d60: 6d 65 6e 74 2e 0a 2a 2a 20 20 20 20 2a 20 54 65  ment..**    * Te
0d70: 72 6d 73 20 69 6e 20 74 68 65 20 56 41 4c 55 45  rms in the VALUE
0d80: 53 20 63 6c 61 75 73 65 20 6f 66 20 61 6e 20 49  S clause of an I
0d90: 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 0a  NSERT statement.
0da0: 2a 2a 0a 2a 2a 20 54 68 65 20 68 61 72 64 20 75  **.** The hard u
0db0: 70 70 65 72 20 6c 69 6d 69 74 20 68 65 72 65 20  pper limit here 
0dc0: 69 73 20 33 32 36 37 36 2e 20 20 4d 6f 73 74 20  is 32676.  Most 
0dd0: 64 61 74 61 62 61 73 65 20 70 65 6f 70 6c 65 20  database people 
0de0: 77 69 6c 6c 0a 2a 2a 20 74 65 6c 6c 20 79 6f 75  will.** tell you
0df0: 20 74 68 61 74 20 69 6e 20 61 20 77 65 6c 6c 2d   that in a well-
0e00: 6e 6f 72 6d 61 6c 69 7a 65 64 20 64 61 74 61 62  normalized datab
0e10: 61 73 65 2c 20 79 6f 75 20 75 73 75 61 6c 6c 79  ase, you usually
0e20: 20 73 68 6f 75 6c 64 0a 2a 2a 20 6e 6f 74 20 68   should.** not h
0e30: 61 76 65 20 6d 6f 72 65 20 74 68 61 6e 20 61 20  ave more than a 
0e40: 64 6f 7a 65 6e 20 6f 72 20 73 6f 20 63 6f 6c 75  dozen or so colu
0e50: 6d 6e 73 20 69 6e 20 61 6e 79 20 74 61 62 6c 65  mns in any table
0e60: 2e 20 20 41 6e 64 20 69 66 0a 2a 2a 20 74 68 61  .  And if.** tha
0e70: 74 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74  t is the case, t
0e80: 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 69 6e 74  here is no point
0e90: 20 69 6e 20 68 61 76 69 6e 67 20 6d 6f 72 65 20   in having more 
0ea0: 74 68 61 6e 20 61 20 66 65 77 0a 2a 2a 20 64 6f  than a few.** do
0eb0: 7a 65 6e 20 76 61 6c 75 65 73 20 69 6e 20 61 6e  zen values in an
0ec0: 79 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 73  y of the other s
0ed0: 69 74 75 61 74 69 6f 6e 73 20 64 65 73 63 72 69  ituations descri
0ee0: 62 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69  bed above..*/.#i
0ef0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  fndef SQLITE_MAX
0f00: 5f 43 4f 4c 55 4d 4e 0a 23 20 64 65 66 69 6e 65  _COLUMN.# define
0f10: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55   SQLITE_MAX_COLU
0f20: 4d 4e 20 32 30 30 30 0a 23 65 6e 64 69 66 0a 0a  MN 2000.#endif..
0f30: 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75  /*.** The maximu
0f40: 6d 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 73 69  m length of a si
0f50: 6e 67 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65  ngle SQL stateme
0f60: 6e 74 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a  nt in bytes..**.
0f70: 2a 2a 20 49 74 20 75 73 65 64 20 74 6f 20 62 65  ** It used to be
0f80: 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20 73   the case that s
0f90: 65 74 74 69 6e 67 20 74 68 69 73 20 76 61 6c 75  etting this valu
0fa0: 65 20 74 6f 20 7a 65 72 6f 20 77 6f 75 6c 64 0a  e to zero would.
0fb0: 2a 2a 20 74 75 72 6e 20 74 68 65 20 6c 69 6d 69  ** turn the limi
0fc0: 74 20 6f 66 66 2e 20 20 54 68 61 74 20 69 73 20  t off.  That is 
0fd0: 6e 6f 20 6c 6f 6e 67 65 72 20 74 72 75 65 2e 20  no longer true. 
0fe0: 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69   It is not possi
0ff0: 62 6c 65 0a 2a 2a 20 74 6f 20 74 75 72 6e 20 74  ble.** to turn t
1000: 68 69 73 20 6c 69 6d 69 74 20 6f 66 66 2e 0a 2a  his limit off..*
1010: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1020: 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 0a  _MAX_SQL_LENGTH.
1030: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
1040: 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 20 31  MAX_SQL_LENGTH 1
1050: 30 30 30 30 30 30 30 30 30 0a 23 65 6e 64 69 66  000000000.#endif
1060: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69  ../*.** The maxi
1070: 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 61 6e 20  mum depth of an 
1080: 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e  expression tree.
1090: 20 54 68 69 73 20 69 73 20 6c 69 6d 69 74 65 64   This is limited
10a0: 20 74 6f 20 0a 2a 2a 20 73 6f 6d 65 20 65 78 74   to .** some ext
10b0: 65 6e 74 20 62 79 20 53 51 4c 49 54 45 5f 4d 41  ent by SQLITE_MA
10c0: 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 2e 20 42 75  X_SQL_LENGTH. Bu
10d0: 74 20 73 6f 6d 65 74 69 6d 65 20 79 6f 75 20 6d  t sometime you m
10e0: 69 67 68 74 20 0a 2a 2a 20 77 61 6e 74 20 74 6f  ight .** want to
10f0: 20 70 6c 61 63 65 20 6d 6f 72 65 20 73 65 76 65   place more seve
1100: 72 65 20 6c 69 6d 69 74 73 20 6f 6e 20 74 68 65  re limits on the
1110: 20 63 6f 6d 70 6c 65 78 69 74 79 20 6f 66 20 61   complexity of a
1120: 6e 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  n .** expression
1130: 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 61 6c 75 65 20  ..**.** A value 
1140: 6f 66 20 30 20 75 73 65 64 20 74 6f 20 6d 65 61  of 0 used to mea
1150: 6e 20 74 68 61 74 20 74 68 65 20 6c 69 6d 69 74  n that the limit
1160: 20 77 61 73 20 6e 6f 74 20 65 6e 66 6f 72 63 65   was not enforce
1170: 64 2e 0a 2a 2a 20 42 75 74 20 74 68 61 74 20 69  d..** But that i
1180: 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 74 72 75 65  s no longer true
1190: 2e 20 20 54 68 65 20 6c 69 6d 69 74 20 69 73 20  .  The limit is 
11a0: 6e 6f 77 20 73 74 72 69 63 74 6c 79 20 65 6e 66  now strictly enf
11b0: 6f 72 63 65 64 0a 2a 2a 20 61 74 20 61 6c 6c 20  orced.** at all 
11c0: 74 69 6d 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65  times..*/.#ifnde
11d0: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50  f SQLITE_MAX_EXP
11e0: 52 5f 44 45 50 54 48 0a 23 20 64 65 66 69 6e 65  R_DEPTH.# define
11f0: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52   SQLITE_MAX_EXPR
1200: 5f 44 45 50 54 48 20 31 30 30 30 0a 23 65 6e 64  _DEPTH 1000.#end
1210: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61  if../*.** The ma
1220: 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
1230: 74 65 72 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f  terms in a compo
1240: 75 6e 64 20 53 45 4c 45 43 54 20 73 74 61 74 65  und SELECT state
1250: 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 63 6f 64  ment..** The cod
1260: 65 20 67 65 6e 65 72 61 74 6f 72 20 66 6f 72 20  e generator for 
1270: 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20  compound SELECT 
1280: 73 74 61 74 65 6d 65 6e 74 73 20 64 6f 65 73 20  statements does 
1290: 6f 6e 65 0a 2a 2a 20 6c 65 76 65 6c 20 6f 66 20  one.** level of 
12a0: 72 65 63 75 72 73 69 6f 6e 20 66 6f 72 20 65 61  recursion for ea
12b0: 63 68 20 74 65 72 6d 2e 20 20 41 20 73 74 61 63  ch term.  A stac
12c0: 6b 20 6f 76 65 72 66 6c 6f 77 20 63 61 6e 20 72  k overflow can r
12d0: 65 73 75 6c 74 0a 2a 2a 20 69 66 20 74 68 65 20  esult.** if the 
12e0: 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20  number of terms 
12f0: 69 73 20 74 6f 6f 20 6c 61 72 67 65 2e 20 20 49  is too large.  I
1300: 6e 20 70 72 61 63 74 69 63 65 2c 20 6d 6f 73 74  n practice, most
1310: 20 53 51 4c 0a 2a 2a 20 6e 65 76 65 72 20 68 61   SQL.** never ha
1320: 73 20 6d 6f 72 65 20 74 68 61 6e 20 33 20 6f 72  s more than 3 or
1330: 20 34 20 74 65 72 6d 73 2e 20 20 55 73 65 20 61   4 terms.  Use a
1340: 20 76 61 6c 75 65 20 6f 66 20 30 20 74 6f 20 64   value of 0 to d
1350: 69 73 61 62 6c 65 0a 2a 2a 20 61 6e 79 20 6c 69  isable.** any li
1360: 6d 69 74 20 6f 6e 20 74 68 65 20 6e 75 6d 62 65  mit on the numbe
1370: 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 61 20  r of terms in a 
1380: 63 6f 6d 70 6f 75 6e 74 20 53 45 4c 45 43 54 2e  compount SELECT.
1390: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
13a0: 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f  TE_MAX_COMPOUND_
13b0: 53 45 4c 45 43 54 0a 23 20 64 65 66 69 6e 65 20  SELECT.# define 
13c0: 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f  SQLITE_MAX_COMPO
13d0: 55 4e 44 5f 53 45 4c 45 43 54 20 35 30 30 0a 23  UND_SELECT 500.#
13e0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
13f0: 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
1400: 6f 66 20 6f 70 63 6f 64 65 73 20 69 6e 20 61 20  of opcodes in a 
1410: 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a  VDBE program..**
1420: 20 4e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65   Not currently e
1430: 6e 66 6f 72 63 65 64 2e 0a 2a 2f 0a 23 69 66 6e  nforced..*/.#ifn
1440: 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56  def SQLITE_MAX_V
1450: 44 42 45 5f 4f 50 0a 23 20 64 65 66 69 6e 65 20  DBE_OP.# define 
1460: 53 51 4c 49 54 45 5f 4d 41 58 5f 56 44 42 45 5f  SQLITE_MAX_VDBE_
1470: 4f 50 20 32 35 30 30 30 0a 23 65 6e 64 69 66 0a  OP 25000.#endif.
1480: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d  ./*.** The maxim
1490: 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67  um number of arg
14a0: 75 6d 65 6e 74 73 20 74 6f 20 61 6e 20 53 51 4c  uments to an SQL
14b0: 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 69   function..*/.#i
14c0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  fndef SQLITE_MAX
14d0: 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 0a 23 20  _FUNCTION_ARG.# 
14e0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
14f0: 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 31  X_FUNCTION_ARG 1
1500: 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  00.#endif../*.**
1510: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   The maximum num
1520: 62 65 72 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79  ber of in-memory
1530: 20 70 61 67 65 73 20 74 6f 20 75 73 65 20 66 6f   pages to use fo
1540: 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  r the main datab
1550: 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64  ase.** table and
1560: 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 74   for temporary t
1570: 61 62 6c 65 73 2e 20 20 54 68 65 20 53 51 4c 49  ables.  The SQLI
1580: 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45  TE_DEFAULT_CACHE
1590: 5f 53 49 5a 45 0a 2a 2f 0a 23 69 66 6e 64 65 66  _SIZE.*/.#ifndef
15a0: 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
15b0: 43 41 43 48 45 5f 53 49 5a 45 0a 23 20 64 65 66  CACHE_SIZE.# def
15c0: 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55  ine SQLITE_DEFAU
15d0: 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 20 20 32  LT_CACHE_SIZE  2
15e0: 30 30 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64  000.#endif.#ifnd
15f0: 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ef SQLITE_DEFAUL
1600: 54 5f 54 45 4d 50 5f 43 41 43 48 45 5f 53 49 5a  T_TEMP_CACHE_SIZ
1610: 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  E.# define SQLIT
1620: 45 5f 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f 43  E_DEFAULT_TEMP_C
1630: 41 43 48 45 5f 53 49 5a 45 20 20 35 30 30 0a 23  ACHE_SIZE  500.#
1640: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
1650: 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
1660: 6f 66 20 61 74 74 61 63 68 65 64 20 64 61 74 61  of attached data
1670: 62 61 73 65 73 2e 20 20 54 68 69 73 20 6d 75 73  bases.  This mus
1680: 74 20 62 65 20 62 65 74 77 65 65 6e 20 30 0a 2a  t be between 0.*
1690: 2a 20 61 6e 64 20 33 30 2e 20 20 54 68 65 20 75  * and 30.  The u
16a0: 70 70 65 72 20 62 6f 75 6e 64 20 6f 6e 20 33 30  pper bound on 30
16b0: 20 69 73 20 62 65 63 61 75 73 65 20 61 20 33 32   is because a 32
16c0: 2d 62 69 74 20 69 6e 74 65 67 65 72 20 62 69 74  -bit integer bit
16d0: 6d 61 70 0a 2a 2a 20 69 73 20 75 73 65 64 20 69  map.** is used i
16e0: 6e 74 65 72 6e 61 6c 6c 79 20 74 6f 20 74 72 61  nternally to tra
16f0: 63 6b 20 61 74 74 61 63 68 65 64 20 64 61 74 61  ck attached data
1700: 62 61 73 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65  bases..*/.#ifnde
1710: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54  f SQLITE_MAX_ATT
1720: 41 43 48 45 44 0a 23 20 64 65 66 69 6e 65 20 53  ACHED.# define S
1730: 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48  QLITE_MAX_ATTACH
1740: 45 44 20 31 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f  ED 10.#endif.../
1750: 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d  *.** The maximum
1760: 20 76 61 6c 75 65 20 6f 66 20 61 20 3f 6e 6e 6e   value of a ?nnn
1770: 20 77 69 6c 64 63 61 72 64 20 74 68 61 74 20 74   wildcard that t
1780: 68 65 20 70 61 72 73 65 72 20 77 69 6c 6c 20 61  he parser will a
1790: 63 63 65 70 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65  ccept..*/.#ifnde
17a0: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52  f SQLITE_MAX_VAR
17b0: 49 41 42 4c 45 5f 4e 55 4d 42 45 52 0a 23 20 64  IABLE_NUMBER.# d
17c0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58  efine SQLITE_MAX
17d0: 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52  _VARIABLE_NUMBER
17e0: 20 39 39 39 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20   999.#endif../* 
17f0: 4d 61 78 69 6d 75 6d 20 70 61 67 65 20 73 69 7a  Maximum page siz
1800: 65 2e 20 20 54 68 65 20 75 70 70 65 72 20 62 6f  e.  The upper bo
1810: 75 6e 64 20 6f 6e 20 74 68 69 73 20 76 61 6c 75  und on this valu
1820: 65 20 69 73 20 33 32 37 36 38 2e 20 20 54 68 69  e is 32768.  Thi
1830: 73 20 61 20 6c 69 6d 69 74 0a 2a 2a 20 69 6d 70  s a limit.** imp
1840: 6f 73 65 64 20 62 79 20 74 68 65 20 6e 65 63 65  osed by the nece
1850: 73 73 69 74 79 20 6f 66 20 73 74 6f 72 69 6e 67  ssity of storing
1860: 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 61 20   the value in a 
1870: 32 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20  2-byte unsigned 
1880: 69 6e 74 65 67 65 72 0a 2a 2a 20 61 6e 64 20 74  integer.** and t
1890: 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65  he fact that the
18a0: 20 70 61 67 65 20 73 69 7a 65 20 6d 75 73 74 20   page size must 
18b0: 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e  be a power of 2.
18c0: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
18d0: 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45  TE_MAX_PAGE_SIZE
18e0: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
18f0: 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 33  _MAX_PAGE_SIZE 3
1900: 32 37 36 38 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  2768.#endif.../*
1910: 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20  .** The default 
1920: 73 69 7a 65 20 6f 66 20 61 20 64 61 74 61 62 61  size of a databa
1930: 73 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 6e  se page..*/.#ifn
1940: 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55  def SQLITE_DEFAU
1950: 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 64  LT_PAGE_SIZE.# d
1960: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46  efine SQLITE_DEF
1970: 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 31  AULT_PAGE_SIZE 1
1980: 30 32 34 0a 23 65 6e 64 69 66 0a 23 69 66 20 53  024.#endif.#if S
1990: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41  QLITE_DEFAULT_PA
19a0: 47 45 5f 53 49 5a 45 3e 53 51 4c 49 54 45 5f 4d  GE_SIZE>SQLITE_M
19b0: 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 75  AX_PAGE_SIZE.# u
19c0: 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41  ndef SQLITE_DEFA
19d0: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20  ULT_PAGE_SIZE.# 
19e0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45  define SQLITE_DE
19f0: 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20  FAULT_PAGE_SIZE 
1a00: 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f  SQLITE_MAX_PAGE_
1a10: 53 49 5a 45 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  SIZE.#endif../*.
1a20: 2a 2a 20 4f 72 64 69 6e 61 72 69 6c 79 2c 20 69  ** Ordinarily, i
1a30: 66 20 6e 6f 20 76 61 6c 75 65 20 69 73 20 65 78  f no value is ex
1a40: 70 6c 69 63 69 74 6c 79 20 70 72 6f 76 69 64 65  plicitly provide
1a50: 64 2c 20 53 51 4c 69 74 65 20 63 72 65 61 74 65  d, SQLite create
1a60: 73 20 64 61 74 61 62 61 73 65 73 0a 2a 2a 20 77  s databases.** w
1a70: 69 74 68 20 70 61 67 65 20 73 69 7a 65 20 53 51  ith page size SQ
1a80: 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47  LITE_DEFAULT_PAG
1a90: 45 5f 53 49 5a 45 2e 20 48 6f 77 65 76 65 72 2c  E_SIZE. However,
1aa0: 20 62 61 73 65 64 20 6f 6e 20 63 65 72 74 61 69   based on certai
1ab0: 6e 0a 2a 2a 20 64 65 76 69 63 65 20 63 68 61 72  n.** device char
1ac0: 61 63 74 65 72 69 73 74 69 63 73 20 28 73 65 63  acteristics (sec
1ad0: 74 6f 72 2d 73 69 7a 65 20 61 6e 64 20 61 74 6f  tor-size and ato
1ae0: 6d 69 63 20 77 72 69 74 65 28 29 20 73 75 70 70  mic write() supp
1af0: 6f 72 74 29 2c 0a 2a 2a 20 53 51 4c 69 74 65 20  ort),.** SQLite 
1b00: 6d 61 79 20 63 68 6f 6f 73 65 20 61 20 6c 61 72  may choose a lar
1b10: 67 65 72 20 76 61 6c 75 65 2e 20 54 68 69 73 20  ger value. This 
1b20: 63 6f 6e 73 74 61 6e 74 20 69 73 20 74 68 65 20  constant is the 
1b30: 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 0a 2a 2a  maximum value.**
1b40: 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 63 68 6f   SQLite will cho
1b50: 6f 73 65 20 6f 6e 20 69 74 73 20 6f 77 6e 2e 0a  ose on its own..
1b60: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
1b70: 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41  E_MAX_DEFAULT_PA
1b80: 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65  GE_SIZE.# define
1b90: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41   SQLITE_MAX_DEFA
1ba0: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 38 31  ULT_PAGE_SIZE 81
1bb0: 39 32 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51  92.#endif.#if SQ
1bc0: 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54  LITE_MAX_DEFAULT
1bd0: 5f 50 41 47 45 5f 53 49 5a 45 3e 53 51 4c 49 54  _PAGE_SIZE>SQLIT
1be0: 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a  E_MAX_PAGE_SIZE.
1bf0: 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  # undef SQLITE_M
1c00: 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  AX_DEFAULT_PAGE_
1c10: 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51  SIZE.# define SQ
1c20: 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54  LITE_MAX_DEFAULT
1c30: 5f 50 41 47 45 5f 53 49 5a 45 20 53 51 4c 49 54  _PAGE_SIZE SQLIT
1c40: 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a  E_MAX_PAGE_SIZE.
1c50: 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 4d  #endif.../*.** M
1c60: 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  aximum number of
1c70: 20 70 61 67 65 73 20 69 6e 20 6f 6e 65 20 64 61   pages in one da
1c80: 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a  tabase file..**.
1c90: 2a 2a 20 54 68 69 73 20 69 73 20 72 65 61 6c 6c  ** This is reall
1ca0: 79 20 6a 75 73 74 20 74 68 65 20 64 65 66 61 75  y just the defau
1cb0: 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  lt value for the
1cc0: 20 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 20   max_page_count 
1cd0: 70 72 61 67 6d 61 2e 0a 2a 2a 20 54 68 69 73 20  pragma..** This 
1ce0: 76 61 6c 75 65 20 63 61 6e 20 62 65 20 6c 6f 77  value can be low
1cf0: 65 72 65 64 20 28 6f 72 20 72 61 69 73 65 64 29  ered (or raised)
1d00: 20 61 74 20 72 75 6e 2d 74 69 6d 65 20 75 73 69   at run-time usi
1d10: 6e 67 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 6d  ng that the.** m
1d20: 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 20 6d 61  ax_page_count ma
1d30: 63 72 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  cro..*/.#ifndef 
1d40: 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f  SQLITE_MAX_PAGE_
1d50: 43 4f 55 4e 54 0a 23 20 64 65 66 69 6e 65 20 53  COUNT.# define S
1d60: 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 43  QLITE_MAX_PAGE_C
1d70: 4f 55 4e 54 20 31 30 37 33 37 34 31 38 32 33 0a  OUNT 1073741823.
1d80: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61  #endif../*.** Ma
1d90: 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 28 69 6e  ximum length (in
1da0: 20 62 79 74 65 73 29 20 6f 66 20 74 68 65 20 70   bytes) of the p
1db0: 61 74 74 65 72 6e 20 69 6e 20 61 20 4c 49 4b 45  attern in a LIKE
1dc0: 20 6f 72 20 47 4c 4f 42 0a 2a 2a 20 6f 70 65 72   or GLOB.** oper
1dd0: 61 74 6f 72 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  ator..*/.#ifndef
1de0: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45   SQLITE_MAX_LIKE
1df0: 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 0a  _PATTERN_LENGTH.
1e00: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
1e10: 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e  MAX_LIKE_PATTERN
1e20: 5f 4c 45 4e 47 54 48 20 35 30 30 30 30 0a 23 65  _LENGTH 50000.#e
1e30: 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif../*********
1e40: 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c  ***** End of sql
1e50: 69 74 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a 2a  iteLimit.h *****
1e60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e70: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e80: 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1e90: 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
1ea0: 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
1eb0: 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e  ff in sqliteInt.
1ec0: 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
1ed0: 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 44 69 73 61 62 6c  ****/../* Disabl
1ee0: 65 20 6e 75 69 73 61 6e 63 65 20 77 61 72 6e 69  e nuisance warni
1ef0: 6e 67 73 20 6f 6e 20 42 6f 72 6c 61 6e 64 20 63  ngs on Borland c
1f00: 6f 6d 70 69 6c 65 72 73 20 2a 2f 0a 23 69 66 20  ompilers */.#if 
1f10: 64 65 66 69 6e 65 64 28 5f 5f 42 4f 52 4c 41 4e  defined(__BORLAN
1f20: 44 43 5f 5f 29 0a 23 70 72 61 67 6d 61 20 77 61  DC__).#pragma wa
1f30: 72 6e 20 2d 72 63 68 20 2f 2a 20 75 6e 72 65 61  rn -rch /* unrea
1f40: 63 68 61 62 6c 65 20 63 6f 64 65 20 2a 2f 0a 23  chable code */.#
1f50: 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 63 63 63  pragma warn -ccc
1f60: 20 2f 2a 20 43 6f 6e 64 69 74 69 6f 6e 20 69 73   /* Condition is
1f70: 20 61 6c 77 61 79 73 20 74 72 75 65 20 6f 72 20   always true or 
1f80: 66 61 6c 73 65 20 2a 2f 0a 23 70 72 61 67 6d 61  false */.#pragma
1f90: 20 77 61 72 6e 20 2d 61 75 73 20 2f 2a 20 41 73   warn -aus /* As
1fa0: 73 69 67 6e 65 64 20 76 61 6c 75 65 20 69 73 20  signed value is 
1fb0: 6e 65 76 65 72 20 75 73 65 64 20 2a 2f 0a 23 70  never used */.#p
1fc0: 72 61 67 6d 61 20 77 61 72 6e 20 2d 63 73 75 20  ragma warn -csu 
1fd0: 2f 2a 20 43 6f 6d 70 61 72 69 6e 67 20 73 69 67  /* Comparing sig
1fe0: 6e 65 64 20 61 6e 64 20 75 6e 73 69 67 6e 65 64  ned and unsigned
1ff0: 20 2a 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e   */.#pragma warn
2000: 20 2d 73 70 61 20 2f 2a 20 53 75 73 70 69 63 6f   -spa /* Suspico
2010: 75 73 20 70 6f 69 6e 74 65 72 20 61 72 69 74 68  us pointer arith
2020: 6d 65 74 69 63 20 2a 2f 0a 23 65 6e 64 69 66 0a  metic */.#endif.
2030: 0a 2f 2a 20 4e 65 65 64 65 64 20 66 6f 72 20 76  ./* Needed for v
2040: 61 72 69 6f 75 73 20 64 65 66 69 6e 69 74 69 6f  arious definitio
2050: 6e 73 2e 2e 2e 20 2a 2f 0a 23 64 65 66 69 6e 65  ns... */.#define
2060: 20 5f 47 4e 55 5f 53 4f 55 52 43 45 0a 0a 2f 2a   _GNU_SOURCE../*
2070: 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 74 61 6e  .** Include stan
2080: 64 61 72 64 20 68 65 61 64 65 72 20 66 69 6c 65  dard header file
2090: 73 20 61 73 20 6e 65 63 65 73 73 61 72 79 0a 2a  s as necessary.*
20a0: 2f 0a 23 69 66 64 65 66 20 48 41 56 45 5f 53 54  /.#ifdef HAVE_ST
20b0: 44 49 4e 54 5f 48 0a 23 69 6e 63 6c 75 64 65 20  DINT_H.#include 
20c0: 3c 73 74 64 69 6e 74 2e 68 3e 0a 23 65 6e 64 69  <stdint.h>.#endi
20d0: 66 0a 23 69 66 64 65 66 20 48 41 56 45 5f 49 4e  f.#ifdef HAVE_IN
20e0: 54 54 59 50 45 53 5f 48 0a 23 69 6e 63 6c 75 64  TTYPES_H.#includ
20f0: 65 20 3c 69 6e 74 74 79 70 65 73 2e 68 3e 0a 23  e <inttypes.h>.#
2100: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d  endif../*.** A m
2110: 61 63 72 6f 20 75 73 65 64 20 74 6f 20 61 69 64  acro used to aid
2120: 20 69 6e 20 63 6f 76 65 72 61 67 65 20 74 65 73   in coverage tes
2130: 74 69 6e 67 2e 20 20 57 68 65 6e 20 64 6f 69 6e  ting.  When doin
2140: 67 20 63 6f 76 65 72 61 67 65 0a 2a 2a 20 74 65  g coverage.** te
2150: 73 74 69 6e 67 2c 20 74 68 65 20 63 6f 6e 64 69  sting, the condi
2160: 74 69 6f 6e 20 69 6e 73 69 64 65 20 74 68 65 20  tion inside the 
2170: 61 72 67 75 6d 65 6e 74 20 6d 75 73 74 20 62 65  argument must be
2180: 20 65 76 61 6c 75 61 74 65 64 20 0a 2a 2a 20 62   evaluated .** b
2190: 6f 74 68 20 74 72 75 65 20 61 6e 64 20 66 61 6c  oth true and fal
21a0: 73 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67  se in order to g
21b0: 65 74 20 66 75 6c 6c 20 62 72 61 6e 63 68 20 63  et full branch c
21c0: 6f 76 65 72 61 67 65 2e 0a 2a 2a 20 54 68 69 73  overage..** This
21d0: 20 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 69 6e   macro can be in
21e0: 73 65 72 74 65 64 20 74 6f 20 65 6e 73 75 72 65  serted to ensure
21f0: 20 61 64 65 71 75 61 74 65 20 74 65 73 74 20 63   adequate test c
2200: 6f 76 65 72 61 67 65 0a 2a 2a 20 69 6e 20 70 6c  overage.** in pl
2210: 61 63 65 73 20 77 68 65 72 65 20 73 69 6d 70 6c  aces where simpl
2220: 65 20 63 6f 6e 64 69 74 69 6f 6e 2f 64 65 63 69  e condition/deci
2230: 73 69 6f 6e 20 63 6f 76 65 72 61 67 65 20 69 73  sion coverage is
2240: 20 69 6e 61 64 65 71 75 61 74 65 2e 0a 2a 2f 0a   inadequate..*/.
2250: 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 4f  #ifdef SQLITE_CO
2260: 56 45 52 41 47 45 5f 54 45 53 54 0a 53 51 4c 49  VERAGE_TEST.SQLI
2270: 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
2280: 64 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61 67  d sqlite3Coverag
2290: 65 28 69 6e 74 29 3b 0a 23 20 64 65 66 69 6e 65  e(int);.# define
22a0: 20 74 65 73 74 63 61 73 65 28 58 29 20 20 69 66   testcase(X)  if
22b0: 28 20 58 20 29 7b 20 73 71 6c 69 74 65 33 43 6f  ( X ){ sqlite3Co
22c0: 76 65 72 61 67 65 28 5f 5f 4c 49 4e 45 5f 5f 29  verage(__LINE__)
22d0: 3b 20 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  ; }.#else.# defi
22e0: 6e 65 20 74 65 73 74 63 61 73 65 28 58 29 0a 23  ne testcase(X).#
22f0: 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  endif.../*.** Th
2300: 65 20 6d 61 63 72 6f 20 75 6e 6c 69 6b 65 6c 79  e macro unlikely
2310: 28 29 20 69 73 20 61 20 68 69 6e 74 20 74 68 61  () is a hint tha
2320: 74 20 73 75 72 72 6f 75 6e 64 73 20 61 20 62 6f  t surrounds a bo
2330: 6f 6c 65 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73  olean.** express
2340: 69 6f 6e 20 74 68 61 74 20 69 73 20 75 73 75 61  ion that is usua
2350: 6c 6c 79 20 66 61 6c 73 65 2e 20 20 4d 61 63 72  lly false.  Macr
2360: 6f 20 6c 69 6b 65 6c 79 28 29 20 73 75 72 72 6f  o likely() surro
2370: 75 6e 64 73 0a 2a 2a 20 61 20 62 6f 6f 6c 65 61  unds.** a boolea
2380: 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61  n expression tha
2390: 74 20 69 73 20 75 73 75 61 6c 6c 79 20 74 72 75  t is usually tru
23a0: 65 2e 20 20 47 43 43 20 69 73 20 61 62 6c 65 20  e.  GCC is able 
23b0: 74 6f 0a 2a 2a 20 75 73 65 20 74 68 65 73 65 20  to.** use these 
23c0: 68 69 6e 74 73 20 74 6f 20 67 65 6e 65 72 61 74  hints to generat
23d0: 65 20 62 65 74 74 65 72 20 63 6f 64 65 2c 20 73  e better code, s
23e0: 6f 6d 65 74 69 6d 65 73 2e 0a 2a 2f 0a 23 69 66  ometimes..*/.#if
23f0: 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f   defined(__GNUC_
2400: 5f 29 20 26 26 20 30 0a 23 20 64 65 66 69 6e 65  _) && 0.# define
2410: 20 6c 69 6b 65 6c 79 28 58 29 20 20 20 20 5f 5f   likely(X)    __
2420: 62 75 69 6c 74 69 6e 5f 65 78 70 65 63 74 28 28  builtin_expect((
2430: 58 29 2c 31 29 0a 23 20 64 65 66 69 6e 65 20 75  X),1).# define u
2440: 6e 6c 69 6b 65 6c 79 28 58 29 20 20 5f 5f 62 75  nlikely(X)  __bu
2450: 69 6c 74 69 6e 5f 65 78 70 65 63 74 28 28 58 29  iltin_expect((X)
2460: 2c 30 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  ,0).#else.# defi
2470: 6e 65 20 6c 69 6b 65 6c 79 28 58 29 20 20 20 20  ne likely(X)    
2480: 21 21 28 58 29 0a 23 20 64 65 66 69 6e 65 20 75  !!(X).# define u
2490: 6e 6c 69 6b 65 6c 79 28 58 29 20 20 21 21 28 58  nlikely(X)  !!(X
24a0: 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  ).#endif.../*.**
24b0: 20 54 68 65 73 65 20 23 64 65 66 69 6e 65 73 20   These #defines 
24c0: 73 68 6f 75 6c 64 20 65 6e 61 62 6c 65 20 3e 32  should enable >2
24d0: 47 42 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20  GB file support 
24e0: 6f 6e 20 50 6f 73 69 78 20 69 66 20 74 68 65 0a  on Posix if the.
24f0: 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70  ** underlying op
2500: 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 73  erating system s
2510: 75 70 70 6f 72 74 73 20 69 74 2e 20 20 49 66 20  upports it.  If 
2520: 74 68 65 20 4f 53 20 6c 61 63 6b 73 0a 2a 2a 20  the OS lacks.** 
2530: 6c 61 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f  large file suppo
2540: 72 74 2c 20 6f 72 20 69 66 20 74 68 65 20 4f 53  rt, or if the OS
2550: 20 69 73 20 77 69 6e 64 6f 77 73 2c 20 74 68 65   is windows, the
2560: 73 65 20 73 68 6f 75 6c 64 20 62 65 20 6e 6f 2d  se should be no-
2570: 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65  ops..**.** Ticke
2580: 74 20 23 32 37 33 39 3a 20 20 54 68 65 20 5f 4c  t #2739:  The _L
2590: 41 52 47 45 46 49 4c 45 5f 53 4f 55 52 43 45 20  ARGEFILE_SOURCE 
25a0: 6d 61 63 72 6f 20 6d 75 73 74 20 61 70 70 65 61  macro must appea
25b0: 72 20 62 65 66 6f 72 65 20 61 6e 79 0a 2a 2a 20  r before any.** 
25c0: 73 79 73 74 65 6d 20 23 69 6e 63 6c 75 64 65 73  system #includes
25d0: 2e 20 20 48 65 6e 63 65 2c 20 74 68 69 73 20 62  .  Hence, this b
25e0: 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 6d 75 73  lock of code mus
25f0: 74 20 62 65 20 74 68 65 20 76 65 72 79 20 66 69  t be the very fi
2600: 72 73 74 0a 2a 2a 20 63 6f 64 65 20 69 6e 20 61  rst.** code in a
2610: 6c 6c 20 73 6f 75 72 63 65 20 66 69 6c 65 73 2e  ll source files.
2620: 0a 2a 2a 0a 2a 2a 20 4c 61 72 67 65 20 66 69 6c  .**.** Large fil
2630: 65 20 73 75 70 70 6f 72 74 20 63 61 6e 20 62 65  e support can be
2640: 20 64 69 73 61 62 6c 65 64 20 75 73 69 6e 67 20   disabled using 
2650: 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 44 49 53  the -DSQLITE_DIS
2660: 41 42 4c 45 5f 4c 46 53 20 73 77 69 74 63 68 0a  ABLE_LFS switch.
2670: 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c  ** on the compil
2680: 65 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e  er command line.
2690: 20 20 54 68 69 73 20 69 73 20 6e 65 63 65 73 73    This is necess
26a0: 61 72 79 20 69 66 20 79 6f 75 20 61 72 65 20 63  ary if you are c
26b0: 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 6f 6e 20 61  ompiling.** on a
26c0: 20 72 65 63 65 6e 74 20 6d 61 63 68 69 6e 65 20   recent machine 
26d0: 28 65 78 3a 20 52 65 64 48 61 74 20 37 2e 32 29  (ex: RedHat 7.2)
26e0: 20 62 75 74 20 79 6f 75 20 77 61 6e 74 20 79 6f   but you want yo
26f0: 75 72 20 63 6f 64 65 20 74 6f 20 77 6f 72 6b 0a  ur code to work.
2700: 2a 2a 20 6f 6e 20 61 6e 20 6f 6c 64 65 72 20 6d  ** on an older m
2710: 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 48  achine (ex: RedH
2720: 61 74 20 36 2e 30 29 2e 20 20 49 66 20 79 6f 75  at 6.0).  If you
2730: 20 63 6f 6d 70 69 6c 65 20 6f 6e 20 52 65 64 48   compile on RedH
2740: 61 74 20 37 2e 32 0a 2a 2a 20 77 69 74 68 6f 75  at 7.2.** withou
2750: 74 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 4c  t this option, L
2760: 46 53 20 69 73 20 65 6e 61 62 6c 65 2e 20 20 42  FS is enable.  B
2770: 75 74 20 4c 46 53 20 64 6f 65 73 20 6e 6f 74 20  ut LFS does not 
2780: 65 78 69 73 74 20 69 6e 20 74 68 65 20 6b 65 72  exist in the ker
2790: 6e 65 6c 0a 2a 2a 20 69 6e 20 52 65 64 48 61 74  nel.** in RedHat
27a0: 20 36 2e 30 2c 20 73 6f 20 74 68 65 20 63 6f 64   6.0, so the cod
27b0: 65 20 77 6f 6e 27 74 20 77 6f 72 6b 2e 20 20 48  e won't work.  H
27c0: 65 6e 63 65 2c 20 66 6f 72 20 6d 61 78 69 6d 75  ence, for maximu
27d0: 6d 20 62 69 6e 61 72 79 0a 2a 2a 20 70 6f 72 74  m binary.** port
27e0: 61 62 69 6c 69 74 79 20 79 6f 75 20 73 68 6f 75  ability you shou
27f0: 6c 64 20 6f 6d 69 74 20 4c 46 53 2e 0a 2a 2a 0a  ld omit LFS..**.
2800: 2a 2a 20 53 69 6d 69 6c 61 72 20 69 73 20 74 72  ** Similar is tr
2810: 75 65 20 66 6f 72 20 4d 61 63 4f 53 2e 20 20 4c  ue for MacOS.  L
2820: 46 53 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f  FS is only suppo
2830: 72 74 65 64 20 6f 6e 20 4d 61 63 4f 53 20 39 20  rted on MacOS 9 
2840: 61 6e 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 23 69  and later..*/.#i
2850: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53  fndef SQLITE_DIS
2860: 41 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66 69 6e  ABLE_LFS.# defin
2870: 65 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20 20 20  e _LARGE_FILE   
2880: 20 20 20 20 31 0a 23 20 69 66 6e 64 65 66 20 5f      1.# ifndef _
2890: 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53  FILE_OFFSET_BITS
28a0: 0a 23 20 20 20 64 65 66 69 6e 65 20 5f 46 49 4c  .#   define _FIL
28b0: 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 20 36 34  E_OFFSET_BITS 64
28c0: 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e  .# endif.# defin
28d0: 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f 55  e _LARGEFILE_SOU
28e0: 52 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a 0a 2f  RCE 1.#endif.../
28f0: 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f  *.** The SQLITE_
2900: 54 48 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f  THREADSAFE macro
2910: 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 64   must be defined
2920: 20 61 73 20 65 69 74 68 65 72 20 30 20 6f 72 20   as either 0 or 
2930: 31 2e 0a 2a 2a 20 4f 6c 64 65 72 20 76 65 72 73  1..** Older vers
2940: 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 75  ions of SQLite u
2950: 73 65 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20  sed an optional 
2960: 54 48 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f  THREADSAFE macro
2970: 2e 0a 2a 2a 20 57 65 20 73 75 70 70 6f 72 74 20  ..** We support 
2980: 74 68 61 74 20 66 6f 72 20 6c 65 67 61 63 79 0a  that for legacy.
2990: 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  */.#if !defined(
29a0: 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
29b0: 45 29 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54  E).#if defined(T
29c0: 48 52 45 41 44 53 41 46 45 29 0a 23 20 64 65 66  HREADSAFE).# def
29d0: 69 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41  ine SQLITE_THREA
29e0: 44 53 41 46 45 20 54 48 52 45 41 44 53 41 46 45  DSAFE THREADSAFE
29f0: 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
2a00: 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
2a10: 45 20 31 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69  E 1.#endif.#endi
2a20: 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 61 63 74 6c 79  f../*.** Exactly
2a30: 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
2a40: 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d 75 73  owing macros mus
2a50: 74 20 62 65 20 64 65 66 69 6e 65 64 20 69 6e 20  t be defined in 
2a60: 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 73 70 65 63  order to.** spec
2a70: 69 66 79 20 77 68 69 63 68 20 6d 65 6d 6f 72 79  ify which memory
2a80: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73   allocation subs
2a90: 79 73 74 65 6d 20 74 6f 20 75 73 65 2e 0a 2a 2a  ystem to use..**
2aa0: 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 53  .**     SQLITE_S
2ab0: 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 20 20 20  YSTEM_MALLOC    
2ac0: 20 20 20 20 20 20 2f 2f 20 55 73 65 20 6e 6f 72        // Use nor
2ad0: 6d 61 6c 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f  mal system mallo
2ae0: 63 28 29 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54  c().**     SQLIT
2af0: 45 5f 4d 45 4d 44 45 42 55 47 20 20 20 20 20 20  E_MEMDEBUG      
2b00: 20 20 20 20 20 20 20 20 20 2f 2f 20 44 65 62 75           // Debu
2b10: 67 67 69 6e 67 20 76 65 72 73 69 6f 6e 20 6f 66  gging version of
2b20: 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29   system malloc()
2b30: 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4d  .**     SQLITE_M
2b40: 45 4d 4f 52 59 5f 53 49 5a 45 20 20 20 20 20 20  EMORY_SIZE      
2b50: 20 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61        // interna
2b60: 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 23 31 0a 2a  l allocator #1.*
2b70: 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 4d 41  *     SQLITE_MMA
2b80: 50 5f 48 45 41 50 5f 53 49 5a 45 20 20 20 20 20  P_HEAP_SIZE     
2b90: 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c 20      // internal 
2ba0: 6d 6d 61 70 28 29 20 61 6c 6c 6f 63 61 74 6f 72  mmap() allocator
2bb0: 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 50  .**     SQLITE_P
2bc0: 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20  OW2_MEMORY_SIZE 
2bd0: 20 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61        // interna
2be0: 6c 20 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 20 61  l power-of-two a
2bf0: 6c 6c 6f 63 61 74 6f 72 0a 2a 2a 0a 2a 2a 20 49  llocator.**.** I
2c00: 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 61 62  f none of the ab
2c10: 6f 76 65 20 61 72 65 20 64 65 66 69 6e 65 64 2c  ove are defined,
2c20: 20 74 68 65 6e 20 73 65 74 20 53 51 4c 49 54 45   then set SQLITE
2c30: 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 61  _SYSTEM_MALLOC a
2c40: 73 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74  s.** the default
2c50: 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64  ..*/.#if defined
2c60: 28 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d  (SQLITE_SYSTEM_M
2c70: 41 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53  ALLOC)+defined(S
2c80: 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b  QLITE_MEMDEBUG)+
2c90: 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51  \.    defined(SQ
2ca0: 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45  LITE_MEMORY_SIZE
2cb0: 29 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  )+defined(SQLITE
2cc0: 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29  _MMAP_HEAP_SIZE)
2cd0: 2b 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53  +\.    defined(S
2ce0: 51 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52  QLITE_POW2_MEMOR
2cf0: 59 5f 53 49 5a 45 29 3e 31 0a 23 20 65 72 72 6f  Y_SIZE)>1.# erro
2d00: 72 20 22 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f  r "At most one o
2d10: 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
2d20: 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 63 6f 6e  compile-time con
2d30: 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f  figuration optio
2d40: 6e 73 5c 0a 20 69 73 20 61 6c 6c 6f 77 73 3a 20  ns\. is allows: 
2d50: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41  SQLITE_SYSTEM_MA
2d60: 4c 4c 4f 43 2c 20 53 51 4c 49 54 45 5f 4d 45 4d  LLOC, SQLITE_MEM
2d70: 44 45 42 55 47 2c 20 53 51 4c 49 54 45 5f 4d 45  DEBUG, SQLITE_ME
2d80: 4d 4f 52 59 5f 53 49 5a 45 2c 5c 0a 20 53 51 4c  MORY_SIZE,\. SQL
2d90: 49 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49  ITE_MMAP_HEAP_SI
2da0: 5a 45 2c 20 53 51 4c 49 54 45 5f 50 4f 57 32 5f  ZE, SQLITE_POW2_
2db0: 4d 45 4d 4f 52 59 5f 53 49 5a 45 22 0a 23 65 6e  MEMORY_SIZE".#en
2dc0: 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28  dif.#if defined(
2dd0: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41  SQLITE_SYSTEM_MA
2de0: 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 51  LLOC)+defined(SQ
2df0: 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b 5c  LITE_MEMDEBUG)+\
2e00: 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c  .    defined(SQL
2e10: 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29  ITE_MEMORY_SIZE)
2e20: 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  +defined(SQLITE_
2e30: 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29 2b  MMAP_HEAP_SIZE)+
2e40: 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51  \.    defined(SQ
2e50: 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59  LITE_POW2_MEMORY
2e60: 5f 53 49 5a 45 29 3d 3d 30 0a 23 20 64 65 66 69  _SIZE)==0.# defi
2e70: 6e 65 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d  ne SQLITE_SYSTEM
2e80: 5f 4d 41 4c 4c 4f 43 20 31 0a 23 65 6e 64 69 66  _MALLOC 1.#endif
2e90: 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54  ../*.** If SQLIT
2ea0: 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49  E_MALLOC_SOFT_LI
2eb0: 4d 49 54 20 69 73 20 64 65 66 69 6e 65 64 2c 20  MIT is defined, 
2ec0: 74 68 65 6e 20 74 72 79 20 74 6f 20 6b 65 65 70  then try to keep
2ed0: 20 74 68 65 0a 2a 2a 20 73 69 7a 65 73 20 6f 66   the.** sizes of
2ee0: 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
2ef0: 6f 6e 73 20 62 65 6c 6f 77 20 74 68 69 73 20 76  ons below this v
2f00: 61 6c 75 65 20 77 68 65 72 65 20 70 6f 73 73 69  alue where possi
2f10: 62 6c 65 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69  ble..*/.#if defi
2f20: 6e 65 64 28 53 51 4c 49 54 45 5f 50 4f 57 32 5f  ned(SQLITE_POW2_
2f30: 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 20 26 26 20  MEMORY_SIZE) && 
2f40: 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2f50: 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49  MALLOC_SOFT_LIMI
2f60: 54 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  T).# define SQLI
2f70: 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c  TE_MALLOC_SOFT_L
2f80: 49 4d 49 54 20 31 30 32 34 0a 23 65 6e 64 69 66  IMIT 1024.#endif
2f90: 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 6e 65 65 64 20  ../*.** We need 
2fa0: 74 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e  to define _XOPEN
2fb0: 5f 53 4f 55 52 43 45 20 61 73 20 66 6f 6c 6c 6f  _SOURCE as follo
2fc0: 77 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 65  ws in order to e
2fd0: 6e 61 62 6c 65 0a 2a 2a 20 72 65 63 75 72 73 69  nable.** recursi
2fe0: 76 65 20 6d 75 74 65 78 65 73 20 6f 6e 20 6d 6f  ve mutexes on mo
2ff0: 73 74 20 75 6e 69 78 20 73 79 73 74 65 6d 73 2e  st unix systems.
3000: 20 20 42 75 74 20 4d 61 63 20 4f 53 20 58 20 69    But Mac OS X i
3010: 73 20 64 69 66 66 65 72 65 6e 74 2e 0a 2a 2a 20  s different..** 
3020: 54 68 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43  The _XOPEN_SOURC
3030: 45 20 64 65 66 69 6e 65 20 63 61 75 73 65 73 20  E define causes 
3040: 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 4d 61 63  problems for Mac
3050: 20 4f 53 20 58 20 77 65 20 61 72 65 20 74 6f 6c   OS X we are tol
3060: 64 2c 0a 2a 2a 20 73 6f 20 69 74 20 69 73 20 6f  d,.** so it is o
3070: 6d 69 74 74 65 64 20 74 68 65 72 65 2e 20 20 53  mitted there.  S
3080: 65 65 20 74 69 63 6b 65 74 20 23 32 36 37 33 2e  ee ticket #2673.
3090: 0a 2a 2a 0a 2a 2a 20 4c 61 74 65 72 20 77 65 20  .**.** Later we 
30a0: 6c 65 61 72 6e 20 74 68 61 74 20 5f 58 4f 50 45  learn that _XOPE
30b0: 4e 5f 53 4f 55 52 43 45 20 69 73 20 70 6f 6f 72  N_SOURCE is poor
30c0: 6c 79 20 6f 72 20 69 6e 63 6f 72 72 65 63 74 6c  ly or incorrectl
30d0: 79 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64  y.** implemented
30e0: 20 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73   on some systems
30f0: 2e 20 20 53 6f 20 77 65 20 61 76 6f 69 64 20 64  .  So we avoid d
3100: 65 66 69 6e 69 6e 67 20 69 74 20 61 74 20 61 6c  efining it at al
3110: 6c 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 61 6c  l.** if it is al
3120: 72 65 61 64 79 20 64 65 66 69 6e 65 64 20 6f 72  ready defined or
3130: 20 69 66 20 69 74 20 69 73 20 75 6e 6e 65 65 64   if it is unneed
3140: 65 64 20 62 65 63 61 75 73 65 20 77 65 20 61 72  ed because we ar
3150: 65 0a 2a 2a 20 6e 6f 74 20 64 6f 69 6e 67 20 61  e.** not doing a
3160: 20 74 68 72 65 61 64 73 61 66 65 20 62 75 69 6c   threadsafe buil
3170: 64 2e 20 20 54 69 63 6b 65 74 20 23 32 36 38 31  d.  Ticket #2681
3180: 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
3190: 20 74 69 63 6b 65 74 20 23 32 37 34 31 2e 0a 2a   ticket #2741..*
31a0: 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 5f  /.#if !defined(_
31b0: 58 4f 50 45 4e 5f 53 4f 55 52 43 45 29 20 26 26  XOPEN_SOURCE) &&
31c0: 20 21 64 65 66 69 6e 65 64 28 5f 5f 44 41 52 57   !defined(__DARW
31d0: 49 4e 5f 5f 29 20 26 26 20 21 64 65 66 69 6e 65  IN__) && !define
31e0: 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20  d(__APPLE__) && 
31f0: 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
3200: 45 0a 23 20 20 64 65 66 69 6e 65 20 5f 58 4f 50  E.#  define _XOP
3210: 45 4e 5f 53 4f 55 52 43 45 20 35 30 30 20 20 2f  EN_SOURCE 500  /
3220: 2a 20 4e 65 65 64 65 64 20 74 6f 20 65 6e 61 62  * Needed to enab
3230: 6c 65 20 70 74 68 72 65 61 64 20 72 65 63 75 72  le pthread recur
3240: 73 69 76 65 20 6d 75 74 65 78 65 73 20 2a 2f 0a  sive mutexes */.
3250: 23 65 6e 64 69 66 0a 0a 23 69 66 20 64 65 66 69  #endif..#if defi
3260: 6e 65 64 28 53 51 4c 49 54 45 5f 54 43 4c 29 20  ned(SQLITE_TCL) 
3270: 7c 7c 20 64 65 66 69 6e 65 64 28 54 43 4c 53 48  || defined(TCLSH
3280: 29 0a 23 20 69 6e 63 6c 75 64 65 20 3c 74 63 6c  ).# include <tcl
3290: 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  .h>.#endif../*.*
32a0: 2a 20 4d 61 6e 79 20 70 65 6f 70 6c 65 20 61 72  * Many people ar
32b0: 65 20 66 61 69 6c 69 6e 67 20 74 6f 20 73 65 74  e failing to set
32c0: 20 2d 44 4e 44 45 42 55 47 3d 31 20 77 68 65 6e   -DNDEBUG=1 when
32d0: 20 63 6f 6d 70 69 6c 69 6e 67 20 53 51 4c 69 74   compiling SQLit
32e0: 65 2e 0a 2a 2a 20 53 65 74 74 69 6e 67 20 4e 44  e..** Setting ND
32f0: 45 42 55 47 20 6d 61 6b 65 73 20 74 68 65 20 63  EBUG makes the c
3300: 6f 64 65 20 73 6d 61 6c 6c 65 72 20 61 6e 64 20  ode smaller and 
3310: 72 75 6e 20 66 61 73 74 65 72 2e 20 20 53 6f 20  run faster.  So 
3320: 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a  the following.**
3330: 20 6c 69 6e 65 73 20 61 72 65 20 61 64 64 65 64   lines are added
3340: 20 74 6f 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   to automaticall
3350: 79 20 73 65 74 20 4e 44 45 42 55 47 20 75 6e 6c  y set NDEBUG unl
3360: 65 73 73 20 74 68 65 20 2d 44 53 51 4c 49 54 45  ess the -DSQLITE
3370: 5f 44 45 42 55 47 3d 31 0a 2a 2a 20 6f 70 74 69  _DEBUG=1.** opti
3380: 6f 6e 20 69 73 20 73 65 74 2e 20 20 54 68 75 73  on is set.  Thus
3390: 20 4e 44 45 42 55 47 20 62 65 63 6f 6d 65 73 20   NDEBUG becomes 
33a0: 61 6e 20 6f 70 74 2d 69 6e 20 72 61 74 68 65 72  an opt-in rather
33b0: 20 74 68 61 6e 20 61 6e 20 6f 70 74 2d 6f 75 74   than an opt-out
33c0: 0a 2a 2a 20 66 65 61 74 75 72 65 2e 0a 2a 2f 0a  .** feature..*/.
33d0: 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45  #if !defined(NDE
33e0: 42 55 47 29 20 26 26 20 21 64 65 66 69 6e 65 64  BUG) && !defined
33f0: 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 0a  (SQLITE_DEBUG) .
3400: 23 20 64 65 66 69 6e 65 20 4e 44 45 42 55 47 20  # define NDEBUG 
3410: 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  1.#endif../*****
3420: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
3430: 65 20 73 71 6c 69 74 65 33 2e 68 20 69 6e 20 74  e sqlite3.h in t
3440: 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c  he middle of sql
3450: 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
3460: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
3470: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
3480: 66 69 6c 65 20 73 71 6c 69 74 65 33 2e 68 20 2a  file sqlite3.h *
3490: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b0: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
34c0: 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
34d0: 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
34e0: 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
34f0: 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
3500: 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
3510: 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
3520: 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
3530: 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
3540: 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
3550: 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
3560: 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
3570: 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
3580: 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
3590: 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
35a0: 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
35b0: 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
35c0: 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
35d0: 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
35e0: 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
35f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3600: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3610: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3620: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3630: 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20  .** This header 
3640: 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65  file defines the
3650: 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20   interface that 
3660: 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
3670: 72 79 0a 2a 2a 20 70 72 65 73 65 6e 74 73 20 74  ry.** presents t
3680: 6f 20 63 6c 69 65 6e 74 20 70 72 6f 67 72 61 6d  o client program
3690: 73 2e 20 20 49 66 20 61 20 43 2d 66 75 6e 63 74  s.  If a C-funct
36a0: 69 6f 6e 2c 20 73 74 72 75 63 74 75 72 65 2c 20  ion, structure, 
36b0: 64 61 74 61 74 79 70 65 2c 0a 2a 2a 20 6f 72 20  datatype,.** or 
36c0: 63 6f 6e 73 74 61 6e 74 20 64 65 66 69 6e 69 74  constant definit
36d0: 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 61 70 70  ion does not app
36e0: 65 61 72 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ear in this file
36f0: 2c 20 74 68 65 6e 20 69 74 20 69 73 0a 2a 2a 20  , then it is.** 
3700: 6e 6f 74 20 61 20 70 75 62 6c 69 73 68 65 64 20  not a published 
3710: 41 50 49 20 6f 66 20 53 51 4c 69 74 65 2c 20 69  API of SQLite, i
3720: 73 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61  s subject to cha
3730: 6e 67 65 20 77 69 74 68 6f 75 74 0a 2a 2a 20 6e  nge without.** n
3740: 6f 74 69 63 65 2c 20 61 6e 64 20 73 68 6f 75 6c  otice, and shoul
3750: 64 20 6e 6f 74 20 62 65 20 72 65 66 65 72 65 6e  d not be referen
3760: 63 65 64 20 62 79 20 70 72 6f 67 72 61 6d 73 20  ced by programs 
3770: 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 2e  that use SQLite.
3780: 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20 74  .**.** Some of t
3790: 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 74  he definitions t
37a0: 68 61 74 20 61 72 65 20 69 6e 20 74 68 69 73 20  hat are in this 
37b0: 66 69 6c 65 20 61 72 65 20 6d 61 72 6b 65 64 20  file are marked 
37c0: 61 73 0a 2a 2a 20 22 65 78 70 65 72 69 6d 65 6e  as.** "experimen
37d0: 74 61 6c 22 2e 20 20 45 78 70 65 72 69 6d 65 6e  tal".  Experimen
37e0: 74 61 6c 20 69 6e 74 65 72 66 61 63 65 73 20 61  tal interfaces a
37f0: 72 65 20 6e 6f 72 6d 61 6c 6c 79 20 6e 65 77 0a  re normally new.
3800: 2a 2a 20 66 65 61 74 75 72 65 73 20 72 65 63 65  ** features rece
3810: 6e 74 6c 79 20 61 64 64 65 64 20 74 6f 20 53 51  ntly added to SQ
3820: 4c 69 74 65 2e 20 20 57 65 20 64 6f 20 6e 6f 74  Lite.  We do not
3830: 20 61 6e 74 69 63 69 70 61 74 65 20 63 68 61 6e   anticipate chan
3840: 67 65 73 20 0a 2a 2a 20 74 6f 20 65 78 70 65 72  ges .** to exper
3850: 69 6d 65 6e 74 61 6c 20 69 6e 74 65 72 66 61 63  imental interfac
3860: 65 73 20 62 75 74 20 72 65 73 65 72 76 65 20 74  es but reserve t
3870: 6f 20 6d 61 6b 65 20 6d 69 6e 6f 72 20 63 68 61  o make minor cha
3880: 6e 67 65 73 20 69 66 0a 2a 2a 20 65 78 70 65 72  nges if.** exper
3890: 69 65 6e 63 65 20 66 72 6f 6d 20 75 73 65 20 22  ience from use "
38a0: 69 6e 20 74 68 65 20 77 69 6c 64 22 20 73 75 67  in the wild" sug
38b0: 67 65 73 74 20 73 75 63 68 20 63 68 61 6e 67 65  gest such change
38c0: 73 20 61 72 65 20 70 72 75 64 65 6e 74 2e 0a 2a  s are prudent..*
38d0: 2a 0a 2a 2a 20 54 68 65 20 6f 66 66 69 63 69 61  *.** The officia
38e0: 6c 20 43 2d 6c 61 6e 67 75 61 67 65 20 41 50 49  l C-language API
38f0: 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66   documentation f
3900: 6f 72 20 53 51 4c 69 74 65 20 69 73 20 64 65 72  or SQLite is der
3910: 69 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 63 6f 6d  ived.** from com
3920: 6d 65 6e 74 73 20 69 6e 20 74 68 69 73 20 66 69  ments in this fi
3930: 6c 65 2e 20 20 54 68 69 73 20 66 69 6c 65 20 69  le.  This file i
3940: 73 20 74 68 65 20 61 75 74 68 6f 72 69 74 61 74  s the authoritat
3950: 69 76 65 20 73 6f 75 72 63 65 0a 2a 2a 20 6f 6e  ive source.** on
3960: 20 68 6f 77 20 53 51 4c 69 74 65 20 69 6e 74 65   how SQLite inte
3970: 72 66 61 63 65 73 20 61 72 65 20 73 75 70 70 6f  rfaces are suppo
3980: 73 65 20 74 6f 20 6f 70 65 72 61 74 65 2e 0a 2a  se to operate..*
3990: 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66  *.** The name of
39a0: 20 74 68 69 73 20 66 69 6c 65 20 75 6e 64 65 72   this file under
39b0: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6d   configuration m
39c0: 61 6e 61 67 65 6d 65 6e 74 20 69 73 20 22 73 71  anagement is "sq
39d0: 6c 69 74 65 2e 68 2e 69 6e 22 2e 0a 2a 2a 20 54  lite.h.in"..** T
39e0: 68 65 20 6d 61 6b 65 66 69 6c 65 20 6d 61 6b 65  he makefile make
39f0: 73 20 73 6f 6d 65 20 6d 69 6e 6f 72 20 63 68 61  s some minor cha
3a00: 6e 67 65 73 20 74 6f 20 74 68 69 73 20 66 69 6c  nges to this fil
3a10: 65 20 28 73 75 63 68 20 61 73 20 69 6e 73 65 72  e (such as inser
3a20: 74 69 6e 67 0a 2a 2a 20 74 68 65 20 76 65 72 73  ting.** the vers
3a30: 69 6f 6e 20 6e 75 6d 62 65 72 29 20 61 6e 64 20  ion number) and 
3a40: 63 68 61 6e 67 65 73 20 69 74 73 20 6e 61 6d 65  changes its name
3a50: 20 74 6f 20 22 73 71 6c 69 74 65 33 2e 68 22 20   to "sqlite3.h" 
3a60: 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 74 68  as.** part of th
3a70: 65 20 62 75 69 6c 64 20 70 72 6f 63 65 73 73 2e  e build process.
3a80: 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a  .**.** @(#) $Id:
3a90: 20 73 71 6c 69 74 65 2e 68 2e 69 6e 2c 76 20 31   sqlite.h.in,v 1
3aa0: 2e 33 31 32 20 32 30 30 38 2f 30 35 2f 31 32 20  .312 2008/05/12 
3ab0: 31 32 3a 33 39 3a 35 36 20 64 72 68 20 45 78 70  12:39:56 drh Exp
3ac0: 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53   $.*/.#ifndef _S
3ad0: 51 4c 49 54 45 33 5f 48 5f 0a 23 64 65 66 69 6e  QLITE3_H_.#defin
3ae0: 65 20 5f 53 51 4c 49 54 45 33 5f 48 5f 0a 23 69  e _SQLITE3_H_.#i
3af0: 6e 63 6c 75 64 65 20 3c 73 74 64 61 72 67 2e 68  nclude <stdarg.h
3b00: 3e 20 20 20 20 20 2f 2a 20 4e 65 65 64 65 64 20  >     /* Needed 
3b10: 66 6f 72 20 74 68 65 20 64 65 66 69 6e 69 74 69  for the definiti
3b20: 6f 6e 20 6f 66 20 76 61 5f 6c 69 73 74 20 2a 2f  on of va_list */
3b30: 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72  ../*.** Make sur
3b40: 65 20 77 65 20 63 61 6e 20 63 61 6c 6c 20 74 68  e we can call th
3b50: 69 73 20 73 74 75 66 66 20 66 72 6f 6d 20 43 2b  is stuff from C+
3b60: 2b 2e 0a 2a 2f 0a 23 69 66 20 30 0a 65 78 74 65  +..*/.#if 0.exte
3b70: 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69 66 0a  rn "C" {.#endif.
3b80: 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20  ../*.** Add the 
3b90: 61 62 69 6c 69 74 79 20 74 6f 20 6f 76 65 72 72  ability to overr
3ba0: 69 64 65 20 27 65 78 74 65 72 6e 27 0a 2a 2f 0a  ide 'extern'.*/.
3bb0: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 45  #ifndef SQLITE_E
3bc0: 58 54 45 52 4e 0a 23 20 64 65 66 69 6e 65 20 53  XTERN.# define S
3bd0: 51 4c 49 54 45 5f 45 58 54 45 52 4e 20 65 78 74  QLITE_EXTERN ext
3be0: 65 72 6e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ern.#endif../*.*
3bf0: 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 73  * Make sure thes
3c00: 65 20 73 79 6d 62 6f 6c 73 20 77 68 65 72 65 20  e symbols where 
3c10: 6e 6f 74 20 64 65 66 69 6e 65 64 20 62 79 20 73  not defined by s
3c20: 6f 6d 65 20 70 72 65 76 69 6f 75 73 20 68 65 61  ome previous hea
3c30: 64 65 72 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2f 0a  der.** file..*/.
3c40: 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 56 45  #ifdef SQLITE_VE
3c50: 52 53 49 4f 4e 0a 23 20 75 6e 64 65 66 20 53 51  RSION.# undef SQ
3c60: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 0a 23 65 6e  LITE_VERSION.#en
3c70: 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
3c80: 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52  E_VERSION_NUMBER
3c90: 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f  .# undef SQLITE_
3ca0: 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23  VERSION_NUMBER.#
3cb0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  endif../*.** CAP
3cc0: 49 33 52 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54  I3REF: Compile-T
3cd0: 69 6d 65 20 4c 69 62 72 61 72 79 20 56 65 72 73  ime Library Vers
3ce0: 69 6f 6e 20 4e 75 6d 62 65 72 73 20 7b 46 31 30  ion Numbers {F10
3cf0: 30 31 30 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53  010}.**.** The S
3d00: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 20 61 6e  QLITE_VERSION an
3d10: 64 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e  d SQLITE_VERSION
3d20: 5f 4e 55 4d 42 45 52 20 23 64 65 66 69 6e 65 73  _NUMBER #defines
3d30: 20 69 6e 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74   in.** the sqlit
3d40: 65 33 2e 68 20 66 69 6c 65 20 73 70 65 63 69 66  e3.h file specif
3d50: 79 20 74 68 65 20 76 65 72 73 69 6f 6e 20 6f 66  y the version of
3d60: 20 53 51 4c 69 74 65 20 77 69 74 68 20 77 68 69   SQLite with whi
3d70: 63 68 0a 2a 2a 20 74 68 61 74 20 68 65 61 64 65  ch.** that heade
3d80: 72 20 66 69 6c 65 20 69 73 20 61 73 73 6f 63 69  r file is associ
3d90: 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ated..**.** The 
3da0: 22 76 65 72 73 69 6f 6e 22 20 6f 66 20 53 51 4c  "version" of SQL
3db0: 69 74 65 20 69 73 20 61 20 73 74 72 69 6e 67 20  ite is a string 
3dc0: 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 58 2e 59  of the form "X.Y
3dd0: 2e 5a 22 2e 0a 2a 2a 20 54 68 65 20 70 68 72 61  .Z"..** The phra
3de0: 73 65 20 22 61 6c 70 68 61 22 20 6f 72 20 22 62  se "alpha" or "b
3df0: 65 74 61 22 20 6d 69 67 68 74 20 62 65 20 61 70  eta" might be ap
3e00: 70 65 6e 64 65 64 20 61 66 74 65 72 20 74 68 65  pended after the
3e10: 20 5a 2e 0a 2a 2a 20 54 68 65 20 58 20 76 61 6c   Z..** The X val
3e20: 75 65 20 69 73 20 6d 61 6a 6f 72 20 76 65 72 73  ue is major vers
3e30: 69 6f 6e 20 6e 75 6d 62 65 72 20 61 6c 77 61 79  ion number alway
3e40: 73 20 33 20 69 6e 20 53 51 4c 69 74 65 33 2e 0a  s 3 in SQLite3..
3e50: 2a 2a 20 54 68 65 20 58 20 76 61 6c 75 65 20 6f  ** The X value o
3e60: 6e 6c 79 20 63 68 61 6e 67 65 73 20 77 68 65 6e  nly changes when
3e70: 20 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70    backwards comp
3e80: 61 74 69 62 69 6c 69 74 79 20 69 73 0a 2a 2a 20  atibility is.** 
3e90: 62 72 6f 6b 65 6e 20 61 6e 64 20 77 65 20 69 6e  broken and we in
3ea0: 74 65 6e 64 20 74 6f 20 6e 65 76 65 72 20 62 72  tend to never br
3eb0: 65 61 6b 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73  eak.** backwards
3ec0: 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 20   compatibility. 
3ed0: 20 54 68 65 20 59 20 76 61 6c 75 65 20 69 73 20   The Y value is 
3ee0: 74 68 65 20 6d 69 6e 6f 72 20 76 65 72 73 69 6f  the minor versio
3ef0: 6e 0a 2a 2a 20 6e 75 6d 62 65 72 20 61 6e 64 20  n.** number and 
3f00: 6f 6e 6c 79 20 63 68 61 6e 67 65 73 20 77 68 65  only changes whe
3f10: 6e 0a 2a 2a 20 74 68 65 72 65 20 61 72 65 20 6d  n.** there are m
3f20: 61 6a 6f 72 20 66 65 61 74 75 72 65 20 65 6e 68  ajor feature enh
3f30: 61 6e 63 65 6d 65 6e 74 73 20 74 68 61 74 20 61  ancements that a
3f40: 72 65 20 66 6f 72 77 61 72 64 73 20 63 6f 6d 70  re forwards comp
3f50: 61 74 69 62 6c 65 0a 2a 2a 20 62 75 74 20 6e 6f  atible.** but no
3f60: 74 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70  t backwards comp
3f70: 61 74 69 62 6c 65 2e 20 20 54 68 65 20 5a 20 76  atible.  The Z v
3f80: 61 6c 75 65 20 69 73 20 72 65 6c 65 61 73 65 20  alue is release 
3f90: 6e 75 6d 62 65 72 0a 2a 2a 20 61 6e 64 20 69 73  number.** and is
3fa0: 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 69 74   incremented wit
3fb0: 68 0a 2a 2a 20 65 61 63 68 20 72 65 6c 65 61 73  h.** each releas
3fc0: 65 20 62 75 74 20 72 65 73 65 74 73 20 62 61 63  e but resets bac
3fd0: 6b 20 74 6f 20 30 20 77 68 65 6e 20 59 20 69 73  k to 0 when Y is
3fe0: 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e 0a 2a 2a   incremented..**
3ff0: 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73  .** See also: [s
4000: 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
4010: 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  n()] and [sqlite
4020: 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d  3_libversion_num
4030: 62 65 72 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e  ber()]..**.** IN
4040: 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20  VARIANTS:.**.** 
4050: 7b 46 31 30 30 31 31 7d 20 54 68 65 20 53 51 4c  {F10011} The SQL
4060: 49 54 45 5f 56 45 52 53 49 4f 4e 20 23 64 65 66  ITE_VERSION #def
4070: 69 6e 65 20 69 6e 20 74 68 65 20 73 71 6c 69 74  ine in the sqlit
4080: 65 33 2e 68 20 68 65 61 64 65 72 20 66 69 6c 65  e3.h header file
4090: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 65 76 61  .**          eva
40a0: 6c 75 61 74 65 73 20 74 6f 20 61 20 73 74 72 69  luates to a stri
40b0: 6e 67 20 6c 69 74 65 72 61 6c 20 74 68 61 74 20  ng literal that 
40c0: 69 73 20 74 68 65 20 53 51 4c 69 74 65 20 76 65  is the SQLite ve
40d0: 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20  rsion.**        
40e0: 20 20 77 69 74 68 20 77 68 69 63 68 20 74 68 65    with which the
40f0: 20 68 65 61 64 65 72 20 66 69 6c 65 20 69 73 20   header file is 
4100: 61 73 73 6f 63 69 61 74 65 64 2e 0a 2a 2a 0a 2a  associated..**.*
4110: 2a 20 7b 46 31 30 30 31 34 7d 20 54 68 65 20 53  * {F10014} The S
4120: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55  QLITE_VERSION_NU
4130: 4d 42 45 52 20 23 64 65 66 69 6e 65 20 72 65 73  MBER #define res
4140: 6f 6c 76 65 73 20 74 6f 20 61 6e 20 69 6e 74 65  olves to an inte
4150: 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ger.**          
4160: 77 69 74 68 20 74 68 65 20 76 61 6c 75 65 20 20  with the value  
4170: 28 58 2a 31 30 30 30 30 30 30 20 2b 20 59 2a 31  (X*1000000 + Y*1
4180: 30 30 30 20 2b 20 5a 29 20 77 68 65 72 65 20 58  000 + Z) where X
4190: 2c 20 59 2c 20 61 6e 64 0a 2a 2a 20 20 20 20 20  , Y, and.**     
41a0: 20 20 20 20 20 5a 20 61 72 65 20 74 68 65 20 6d       Z are the m
41b0: 61 6a 6f 72 20 76 65 72 73 69 6f 6e 2c 20 6d 69  ajor version, mi
41c0: 6e 6f 72 20 76 65 72 73 69 6f 6e 2c 20 61 6e 64  nor version, and
41d0: 20 72 65 6c 65 61 73 65 20 6e 75 6d 62 65 72 2e   release number.
41e0: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
41f0: 54 45 5f 56 45 52 53 49 4f 4e 20 20 20 20 20 20  TE_VERSION      
4200: 20 20 20 22 33 2e 35 2e 39 22 0a 23 64 65 66 69     "3.5.9".#defi
4210: 6e 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f  ne SQLITE_VERSIO
4220: 4e 5f 4e 55 4d 42 45 52 20 20 33 30 30 35 30 30  N_NUMBER  300500
4230: 39 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  9../*.** CAPI3RE
4240: 46 3a 20 52 75 6e 2d 54 69 6d 65 20 4c 69 62 72  F: Run-Time Libr
4250: 61 72 79 20 56 65 72 73 69 6f 6e 20 4e 75 6d 62  ary Version Numb
4260: 65 72 73 20 7b 46 31 30 30 32 30 7d 0a 2a 2a 20  ers {F10020}.** 
4270: 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65  KEYWORDS: sqlite
4280: 33 5f 76 65 72 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20  3_version.**.** 
4290: 54 68 65 73 65 20 66 65 61 74 75 72 65 73 20 70  These features p
42a0: 72 6f 76 69 64 65 20 74 68 65 20 73 61 6d 65 20  rovide the same 
42b0: 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 20 74  information as t
42c0: 68 65 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49  he [SQLITE_VERSI
42d0: 4f 4e 5d 0a 2a 2a 20 61 6e 64 20 5b 53 51 4c 49  ON].** and [SQLI
42e0: 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45  TE_VERSION_NUMBE
42f0: 52 5d 20 23 64 65 66 69 6e 65 73 20 69 6e 20 74  R] #defines in t
4300: 68 65 20 68 65 61 64 65 72 2c 20 62 75 74 20 61  he header, but a
4310: 72 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a  re associated.**
4320: 20 77 69 74 68 20 74 68 65 20 6c 69 62 72 61 72   with the librar
4330: 79 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65  y instead of the
4340: 20 68 65 61 64 65 72 20 66 69 6c 65 2e 20 20 43   header file.  C
4350: 61 75 74 69 6f 75 73 20 70 72 6f 67 72 61 6d 6d  autious programm
4360: 65 72 73 20 6d 69 67 68 74 0a 2a 2a 20 69 6e 63  ers might.** inc
4370: 6c 75 64 65 20 61 20 63 68 65 63 6b 20 69 6e 20  lude a check in 
4380: 74 68 65 69 72 20 61 70 70 6c 69 63 61 74 69 6f  their applicatio
4390: 6e 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 74  n to verify that
43a0: 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 69 62   .** sqlite3_lib
43b0: 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 29  version_number()
43c0: 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20   always returns 
43d0: 74 68 65 20 76 61 6c 75 65 20 0a 2a 2a 20 5b 53  the value .** [S
43e0: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55  QLITE_VERSION_NU
43f0: 4d 42 45 52 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  MBER]..**.** The
4400: 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73   sqlite3_libvers
4410: 69 6f 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20 72  ion() function r
4420: 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20  eturns the same 
4430: 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 20 69  information as i
4440: 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c 69  s.** in the sqli
4450: 74 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 73 74  te3_version[] st
4460: 72 69 6e 67 20 63 6f 6e 73 74 61 6e 74 2e 20 20  ring constant.  
4470: 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  The function is 
4480: 70 72 6f 76 69 64 65 64 0a 2a 2a 20 66 6f 72 20  provided.** for 
4490: 75 73 65 20 69 6e 20 44 4c 4c 73 20 73 69 6e 63  use in DLLs sinc
44a0: 65 20 44 4c 4c 20 75 73 65 72 73 20 75 73 75 61  e DLL users usua
44b0: 6c 6c 79 20 64 6f 20 6e 6f 74 20 68 61 76 65 20  lly do not have 
44c0: 64 69 72 65 63 74 20 61 63 63 65 73 73 20 74 6f  direct access to
44d0: 20 73 74 72 69 6e 67 0a 2a 2a 20 63 6f 6e 73 74   string.** const
44e0: 61 6e 74 73 20 77 69 74 68 69 6e 20 74 68 65 20  ants within the 
44f0: 44 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52  DLL..**.** INVAR
4500: 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31  IANTS:.**.** {F1
4510: 30 30 32 31 7d 20 54 68 65 20 5b 73 71 6c 69 74  0021} The [sqlit
4520: 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75  e3_libversion_nu
4530: 6d 62 65 72 28 29 5d 20 69 6e 74 65 72 66 61 63  mber()] interfac
4540: 65 20 72 65 74 75 72 6e 73 20 61 6e 20 69 6e 74  e returns an int
4550: 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  eger.**         
4560: 20 65 71 75 61 6c 20 74 6f 20 5b 53 51 4c 49 54   equal to [SQLIT
4570: 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52  E_VERSION_NUMBER
4580: 5d 2e 20 0a 2a 2a 0a 2a 2a 20 7b 46 31 30 30 32  ]. .**.** {F1002
4590: 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  2} The [sqlite3_
45a0: 76 65 72 73 69 6f 6e 5d 20 73 74 72 69 6e 67 20  version] string 
45b0: 63 6f 6e 73 74 61 6e 74 20 63 6f 6e 74 61 69 6e  constant contain
45c0: 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 74 68  s the text of th
45d0: 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53  e.**          [S
45e0: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5d 20 73  QLITE_VERSION] s
45f0: 74 72 69 6e 67 2e 20 0a 2a 2a 0a 2a 2a 20 7b 46  tring. .**.** {F
4600: 31 30 30 32 33 7d 20 54 68 65 20 5b 73 71 6c 69  10023} The [sqli
4610: 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28 29  te3_libversion()
4620: 5d 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  ] function retur
4630: 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61  ns.**          a
4640: 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
4650: 5b 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e  [sqlite3_version
4660: 5d 20 73 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e  ] string constan
4670: 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
4680: 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69   const char sqli
4690: 74 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 3b 0a 53  te3_version[];.S
46a0: 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
46b0: 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c 69  char *sqlite3_li
46c0: 62 76 65 72 73 69 6f 6e 28 76 6f 69 64 29 3b 0a  bversion(void);.
46d0: 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
46e0: 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
46f0: 6e 5f 6e 75 6d 62 65 72 28 76 6f 69 64 29 3b 0a  n_number(void);.
4700: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
4710: 20 54 65 73 74 20 54 6f 20 53 65 65 20 49 66 20   Test To See If 
4720: 54 68 65 20 4c 69 62 72 61 72 79 20 49 73 20 54  The Library Is T
4730: 68 72 65 61 64 73 61 66 65 20 7b 46 31 30 31 30  hreadsafe {F1010
4740: 30 7d 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  0}.**.** SQLite 
4750: 63 61 6e 20 62 65 20 63 6f 6d 70 69 6c 65 64 20  can be compiled 
4760: 77 69 74 68 20 6f 72 20 77 69 74 68 6f 75 74 20  with or without 
4770: 6d 75 74 65 78 65 73 2e 20 20 57 68 65 6e 0a 2a  mutexes.  When.*
4780: 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 54 48 52  * the SQLITE_THR
4790: 45 41 44 53 41 46 45 20 43 20 70 72 65 70 72 6f  EADSAFE C prepro
47a0: 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 69 73 20  cessor macro is 
47b0: 74 72 75 65 2c 20 6d 75 74 65 78 65 73 0a 2a 2a  true, mutexes.**
47c0: 20 61 72 65 20 65 6e 61 62 6c 65 64 20 61 6e 64   are enabled and
47d0: 20 53 51 4c 69 74 65 20 69 73 20 74 68 72 65 61   SQLite is threa
47e0: 64 73 61 66 65 2e 20 20 57 68 65 6e 20 74 68 61  dsafe.  When tha
47f0: 74 20 6d 61 63 72 6f 20 69 73 20 66 61 6c 73 65  t macro is false
4800: 2c 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 65 73  ,.** the mutexes
4810: 20 61 72 65 20 6f 6d 69 74 74 65 64 2e 20 20 57   are omitted.  W
4820: 69 74 68 6f 75 74 20 74 68 65 20 6d 75 74 65 78  ithout the mutex
4830: 65 73 2c 20 69 74 20 69 73 20 6e 6f 74 20 73 61  es, it is not sa
4840: 66 65 0a 2a 2a 20 74 6f 20 75 73 65 20 53 51 4c  fe.** to use SQL
4850: 69 74 65 20 66 72 6f 6d 20 6d 6f 72 65 20 74 68  ite from more th
4860: 61 6e 20 6f 6e 65 20 74 68 72 65 61 64 2e 0a 2a  an one thread..*
4870: 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20  *.** There is a 
4880: 6d 65 61 73 75 72 61 62 6c 65 20 70 65 72 66 6f  measurable perfo
4890: 72 6d 61 6e 63 65 20 70 65 6e 61 6c 74 79 20 66  rmance penalty f
48a0: 6f 72 20 65 6e 61 62 6c 69 6e 67 20 6d 75 74 65  or enabling mute
48b0: 78 65 73 2e 0a 2a 2a 20 53 6f 20 69 66 20 73 70  xes..** So if sp
48c0: 65 65 64 20 69 73 20 6f 66 20 75 74 6d 6f 73 74  eed is of utmost
48d0: 20 69 6d 70 6f 72 74 61 6e 63 65 2c 20 69 74 20   importance, it 
48e0: 6d 61 6b 65 73 20 73 65 6e 73 65 20 74 6f 20 64  makes sense to d
48f0: 69 73 61 62 6c 65 0a 2a 2a 20 74 68 65 20 6d 75  isable.** the mu
4900: 74 65 78 65 73 2e 20 20 42 75 74 20 66 6f 72 20  texes.  But for 
4910: 6d 61 78 69 6d 75 6d 20 73 61 66 65 74 79 2c 20  maximum safety, 
4920: 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 20 62  mutexes should b
4930: 65 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a 20 54 68  e enabled..** Th
4940: 65 20 64 65 66 61 75 6c 74 20 62 65 68 61 76 69  e default behavi
4950: 6f 72 20 69 73 20 66 6f 72 20 6d 75 74 65 78 65  or is for mutexe
4960: 73 20 74 6f 20 62 65 20 65 6e 61 62 6c 65 64 2e  s to be enabled.
4970: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65  .**.** This inte
4980: 72 66 61 63 65 20 63 61 6e 20 62 65 20 75 73 65  rface can be use
4990: 64 20 62 79 20 61 20 70 72 6f 67 72 61 6d 20 74  d by a program t
49a0: 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74  o make sure that
49b0: 20 74 68 65 0a 2a 2a 20 76 65 72 73 69 6f 6e 20   the.** version 
49c0: 6f 66 20 53 51 4c 69 74 65 20 74 68 61 74 20 69  of SQLite that i
49d0: 74 20 69 73 20 6c 69 6e 6b 69 6e 67 20 61 67 61  t is linking aga
49e0: 69 6e 73 74 20 77 61 73 20 63 6f 6d 70 69 6c 65  inst was compile
49f0: 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 64 65  d with.** the de
4a00: 73 69 72 65 64 20 73 65 74 74 69 6e 67 20 6f 66  sired setting of
4a10: 20 74 68 65 20 53 51 4c 49 54 45 5f 54 48 52 45   the SQLITE_THRE
4a20: 41 44 53 41 46 45 20 6d 61 63 72 6f 2e 0a 2a 2a  ADSAFE macro..**
4a30: 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a  .** INVARIANTS:.
4a40: 2a 2a 0a 2a 2a 20 7b 46 31 30 31 30 31 7d 20 54  **.** {F10101} T
4a50: 68 65 20 5b 73 71 6c 69 74 65 33 5f 74 68 72 65  he [sqlite3_thre
4a60: 61 64 73 61 66 65 28 29 5d 20 66 75 6e 63 74 69  adsafe()] functi
4a70: 6f 6e 20 72 65 74 75 72 6e 73 20 6e 6f 6e 7a 65  on returns nonze
4a80: 72 6f 20 69 66 0a 2a 2a 20 20 20 20 20 20 20 20  ro if.**        
4a90: 20 20 53 51 4c 69 74 65 20 77 61 73 20 63 6f 6d    SQLite was com
4aa0: 70 69 6c 65 64 20 77 69 74 68 20 69 74 73 20 6d  piled with its m
4ab0: 75 74 65 78 65 73 20 65 6e 61 62 6c 65 64 20 6f  utexes enabled o
4ac0: 72 20 7a 65 72 6f 0a 2a 2a 20 20 20 20 20 20 20  r zero.**       
4ad0: 20 20 20 69 66 20 53 51 4c 69 74 65 20 77 61 73     if SQLite was
4ae0: 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 6d   compiled with m
4af0: 75 74 65 78 65 73 20 64 69 73 61 62 6c 65 64 2e  utexes disabled.
4b00: 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
4b10: 6e 74 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61  nt sqlite3_threa
4b20: 64 73 61 66 65 28 76 6f 69 64 29 3b 0a 0a 2f 2a  dsafe(void);../*
4b30: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61  .** CAPI3REF: Da
4b40: 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f  tabase Connectio
4b50: 6e 20 48 61 6e 64 6c 65 20 7b 46 31 32 30 30 30  n Handle {F12000
4b60: 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b  }.** KEYWORDS: {
4b70: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
4b80: 69 6f 6e 7d 20 7b 64 61 74 61 62 61 73 65 20 63  ion} {database c
4b90: 6f 6e 6e 65 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a  onnections}.**.*
4ba0: 2a 20 45 61 63 68 20 6f 70 65 6e 20 53 51 4c 69  * Each open SQLi
4bb0: 74 65 20 64 61 74 61 62 61 73 65 20 69 73 20 72  te database is r
4bc0: 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 70 6f  epresented by po
4bd0: 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74  inter to an inst
4be0: 61 6e 63 65 20 6f 66 20 74 68 65 0a 2a 2a 20 6f  ance of the.** o
4bf0: 70 61 71 75 65 20 73 74 72 75 63 74 75 72 65 20  paque structure 
4c00: 6e 61 6d 65 64 20 22 73 71 6c 69 74 65 33 22 2e  named "sqlite3".
4c10: 20 20 49 74 20 69 73 20 75 73 65 66 75 6c 20 74    It is useful t
4c20: 6f 20 74 68 69 6e 6b 20 6f 66 20 61 6e 20 73 71  o think of an sq
4c30: 6c 69 74 65 33 0a 2a 2a 20 70 6f 69 6e 74 65 72  lite3.** pointer
4c40: 20 61 73 20 61 6e 20 6f 62 6a 65 63 74 2e 20 20   as an object.  
4c50: 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  The [sqlite3_ope
4c60: 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f  n()], [sqlite3_o
4c70: 70 65 6e 31 36 28 29 5d 2c 20 61 6e 64 0a 2a 2a  pen16()], and.**
4c80: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76   [sqlite3_open_v
4c90: 32 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20  2()] interfaces 
4ca0: 61 72 65 20 69 74 73 20 63 6f 6e 73 74 72 75 63  are its construc
4cb0: 74 6f 72 73 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c  tors.** and [sql
4cc0: 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 20 69 73  ite3_close()] is
4cd0: 20 69 74 73 20 64 65 73 74 72 75 63 74 6f 72 2e   its destructor.
4ce0: 20 20 54 68 65 72 65 20 61 72 65 20 6d 61 6e 79    There are many
4cf0: 20 6f 74 68 65 72 20 69 6e 74 65 72 66 61 63 65   other interface
4d00: 73 0a 2a 2a 20 28 73 75 63 68 20 61 73 20 5b 73  s.** (such as [s
4d10: 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
4d20: 32 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63  2()], [sqlite3_c
4d30: 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29  reate_function()
4d40: 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74  ], and.** [sqlit
4d50: 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28  e3_busy_timeout(
4d60: 29 5d 20 74 6f 20 6e 61 6d 65 20 62 75 74 20 74  )] to name but t
4d70: 68 72 65 65 29 20 74 68 61 74 20 61 72 65 20 6d  hree) that are m
4d80: 65 74 68 6f 64 73 20 6f 6e 20 74 68 69 73 0a 2a  ethods on this.*
4d90: 2a 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 74 79 70  * object..*/.typ
4da0: 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
4db0: 74 65 33 20 73 71 6c 69 74 65 33 3b 0a 0a 0a 2f  te3 sqlite3;.../
4dc0: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 36  *.** CAPI3REF: 6
4dd0: 34 2d 42 69 74 20 49 6e 74 65 67 65 72 20 54 79  4-Bit Integer Ty
4de0: 70 65 73 20 7b 46 31 30 32 30 30 7d 0a 2a 2a 20  pes {F10200}.** 
4df0: 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65  KEYWORDS: sqlite
4e00: 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 5f 75 69  _int64 sqlite_ui
4e10: 6e 74 36 34 0a 2a 2a 0a 2a 2a 20 42 65 63 61 75  nt64.**.** Becau
4e20: 73 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63  se there is no c
4e30: 72 6f 73 73 2d 70 6c 61 74 66 6f 72 6d 20 77 61  ross-platform wa
4e40: 79 20 74 6f 20 73 70 65 63 69 66 79 20 36 34 2d  y to specify 64-
4e50: 62 69 74 20 69 6e 74 65 67 65 72 20 74 79 70 65  bit integer type
4e60: 73 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6e 63 6c  s.** SQLite incl
4e70: 75 64 65 73 20 74 79 70 65 64 65 66 73 20 66 6f  udes typedefs fo
4e80: 72 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20  r 64-bit signed 
4e90: 61 6e 64 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  and unsigned int
4ea0: 65 67 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  egers..**.** The
4eb0: 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 61   sqlite3_int64 a
4ec0: 6e 64 20 73 71 6c 69 74 65 33 5f 75 69 6e 74 36  nd sqlite3_uint6
4ed0: 34 20 61 72 65 20 74 68 65 20 70 72 65 66 65 72  4 are the prefer
4ee0: 72 65 64 20 74 79 70 65 0a 2a 2a 20 64 65 66 69  red type.** defi
4ef0: 6e 69 74 69 6f 6e 73 2e 20 20 54 68 65 20 73 71  nitions.  The sq
4f00: 6c 69 74 65 5f 69 6e 74 36 34 20 61 6e 64 20 73  lite_int64 and s
4f10: 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 74 79 70  qlite_uint64 typ
4f20: 65 73 20 61 72 65 0a 2a 2a 20 73 75 70 70 6f 72  es are.** suppor
4f30: 74 65 64 20 66 6f 72 20 62 61 63 6b 77 61 72 64  ted for backward
4f40: 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20  s compatibility 
4f50: 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41  only..**.** INVA
4f60: 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46  RIANTS:.**.** {F
4f70: 31 30 32 30 31 7d 20 54 68 65 20 5b 73 71 6c 69  10201} The [sqli
4f80: 74 65 5f 69 6e 74 36 34 5d 20 61 6e 64 20 5b 73  te_int64] and [s
4f90: 71 6c 69 74 65 33 5f 69 6e 74 36 34 5d 20 74 79  qlite3_int64] ty
4fa0: 70 65 73 20 73 70 65 63 69 66 79 20 61 0a 2a 2a  pes specify a.**
4fb0: 20 20 20 20 20 20 20 20 20 20 36 34 2d 62 69 74            64-bit
4fc0: 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e   signed integer.
4fd0: 0a 2a 2a 0a 2a 2a 20 7b 46 31 30 32 30 32 7d 20  .**.** {F10202} 
4fe0: 54 68 65 20 5b 73 71 6c 69 74 65 5f 75 69 6e 74  The [sqlite_uint
4ff0: 36 34 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  64] and [sqlite3
5000: 5f 75 69 6e 74 36 34 5d 20 74 79 70 65 73 20 73  _uint64] types s
5010: 70 65 63 69 66 79 0a 2a 2a 20 20 20 20 20 20 20  pecify.**       
5020: 20 20 20 61 20 36 34 2d 62 69 74 20 75 6e 73 69     a 64-bit unsi
5030: 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 0a 2a 2f  gned integer..*/
5040: 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49  .#ifdef SQLITE_I
5050: 4e 54 36 34 5f 54 59 50 45 0a 20 20 74 79 70 65  NT64_TYPE.  type
5060: 64 65 66 20 53 51 4c 49 54 45 5f 49 4e 54 36 34  def SQLITE_INT64
5070: 5f 54 59 50 45 20 73 71 6c 69 74 65 5f 69 6e 74  _TYPE sqlite_int
5080: 36 34 3b 0a 20 20 74 79 70 65 64 65 66 20 75 6e  64;.  typedef un
5090: 73 69 67 6e 65 64 20 53 51 4c 49 54 45 5f 49 4e  signed SQLITE_IN
50a0: 54 36 34 5f 54 59 50 45 20 73 71 6c 69 74 65 5f  T64_TYPE sqlite_
50b0: 75 69 6e 74 36 34 3b 0a 23 65 6c 69 66 20 64 65  uint64;.#elif de
50c0: 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 20  fined(_MSC_VER) 
50d0: 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 42 4f 52  || defined(__BOR
50e0: 4c 41 4e 44 43 5f 5f 29 0a 20 20 74 79 70 65 64  LANDC__).  typed
50f0: 65 66 20 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74  ef __int64 sqlit
5100: 65 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70 65 64  e_int64;.  typed
5110: 65 66 20 75 6e 73 69 67 6e 65 64 20 5f 5f 69 6e  ef unsigned __in
5120: 74 36 34 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  t64 sqlite_uint6
5130: 34 3b 0a 23 65 6c 73 65 0a 20 20 74 79 70 65 64  4;.#else.  typed
5140: 65 66 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74  ef long long int
5150: 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20   sqlite_int64;. 
5160: 20 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65   typedef unsigne
5170: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 20  d long long int 
5180: 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23  sqlite_uint64;.#
5190: 65 6e 64 69 66 0a 74 79 70 65 64 65 66 20 73 71  endif.typedef sq
51a0: 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74  lite_int64 sqlit
51b0: 65 33 5f 69 6e 74 36 34 3b 0a 74 79 70 65 64 65  e3_int64;.typede
51c0: 66 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  f sqlite_uint64 
51d0: 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 3b 0a  sqlite3_uint64;.
51e0: 0a 2f 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c  ./*.** If compil
51f0: 69 6e 67 20 66 6f 72 20 61 20 70 72 6f 63 65 73  ing for a proces
5200: 73 6f 72 20 74 68 61 74 20 6c 61 63 6b 73 20 66  sor that lacks f
5210: 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75  loating point su
5220: 70 70 6f 72 74 2c 0a 2a 2a 20 73 75 62 73 74 69  pport,.** substi
5230: 74 75 74 65 20 69 6e 74 65 67 65 72 20 66 6f 72  tute integer for
5240: 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 0a   floating-point.
5250: 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
5260: 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
5270: 4f 49 4e 54 0a 23 20 64 65 66 69 6e 65 20 64 6f  OINT.# define do
5280: 75 62 6c 65 20 73 71 6c 69 74 65 33 5f 69 6e 74  uble sqlite3_int
5290: 36 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  64.#endif../*.**
52a0: 20 43 41 50 49 33 52 45 46 3a 20 43 6c 6f 73 69   CAPI3REF: Closi
52b0: 6e 67 20 41 20 44 61 74 61 62 61 73 65 20 43 6f  ng A Database Co
52c0: 6e 6e 65 63 74 69 6f 6e 20 7b 46 31 32 30 31 30  nnection {F12010
52d0: 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  }.**.** This rou
52e0: 74 69 6e 65 20 69 73 20 74 68 65 20 64 65 73 74  tine is the dest
52f0: 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 5b  ructor for the [
5300: 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74 2e  sqlite3] object.
5310: 20 20 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61    .**.** Applica
5320: 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 5b 73 71  tions should [sq
5330: 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20 7c  lite3_finalize |
5340: 20 66 69 6e 61 6c 69 7a 65 5d 20 61 6c 6c 0a 2a   finalize] all.*
5350: 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  * [prepared stat
5360: 65 6d 65 6e 74 73 5d 20 61 6e 64 0a 2a 2a 20 5b  ements] and.** [
5370: 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f  sqlite3_blob_clo
5380: 73 65 20 7c 20 63 6c 6f 73 65 5d 20 61 6c 6c 20  se | close] all 
5390: 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 7c 20  [sqlite3_blob | 
53a0: 42 4c 4f 42 73 5d 20 0a 2a 2a 20 61 73 73 6f 63  BLOBs] .** assoc
53b0: 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b  iated with the [
53c0: 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74 20  sqlite3] object 
53d0: 70 72 69 6f 72 0a 2a 2a 20 74 6f 20 61 74 74 65  prior.** to atte
53e0: 6d 70 74 69 6e 67 20 74 6f 20 63 6c 6f 73 65 20  mpting to close 
53f0: 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 20 6f 62  the [sqlite3] ob
5400: 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 3c 74 6f 64  ject..**.** <tod
5410: 6f 3e 57 68 61 74 20 68 61 70 70 65 6e 73 20 74  o>What happens t
5420: 6f 20 70 65 6e 64 69 6e 67 20 74 72 61 6e 73 61  o pending transa
5430: 63 74 69 6f 6e 73 3f 20 20 41 72 65 20 74 68 65  ctions?  Are the
5440: 79 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b  y.** rolled back
5450: 2c 20 6f 72 20 61 62 61 6e 64 6f 6e 65 64 3f 3c  , or abandoned?<
5460: 2f 74 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 49 4e 56  /todo>.**.** INV
5470: 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b  ARIANTS:.**.** {
5480: 46 31 32 30 31 31 7d 20 54 68 65 20 5b 73 71 6c  F12011} The [sql
5490: 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 20 69 6e  ite3_close()] in
54a0: 74 65 72 66 61 63 65 20 64 65 73 74 72 6f 79 73  terface destroys
54b0: 20 61 6e 20 5b 73 71 6c 69 74 65 33 5d 20 6f 62   an [sqlite3] ob
54c0: 6a 65 63 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  ject.**         
54d0: 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 61 20   allocated by a 
54e0: 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 5b 73  prior call to [s
54f0: 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 0a  qlite3_open()],.
5500: 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
5510: 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20  ite3_open16()], 
5520: 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  or [sqlite3_open
5530: 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 46  _v2()]..**.** {F
5540: 31 32 30 31 32 7d 20 54 68 65 20 5b 73 71 6c 69  12012} The [sqli
5550: 74 65 33 5f 63 6c 6f 73 65 28 29 5d 20 66 75 6e  te3_close()] fun
5560: 63 74 69 6f 6e 20 72 65 6c 65 61 73 65 73 20 61  ction releases a
5570: 6c 6c 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 62  ll memory used b
5580: 79 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  y the.**        
5590: 20 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 6e 64    connection and
55a0: 20 63 6c 6f 73 65 73 20 61 6c 6c 20 6f 70 65 6e   closes all open
55b0: 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 46   files..**.** {F
55c0: 31 32 30 31 33 7d 20 49 66 20 74 68 65 20 64 61  12013} If the da
55d0: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
55e0: 6e 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 20 20  n contains.**   
55f0: 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 64         [prepared
5600: 20 73 74 61 74 65 6d 65 6e 74 73 5d 20 74 68 61   statements] tha
5610: 74 20 68 61 76 65 20 6e 6f 74 20 62 65 65 6e 0a  t have not been.
5620: 2a 2a 20 20 20 20 20 20 20 20 20 20 66 69 6e 61  **          fina
5630: 6c 69 7a 65 64 20 62 79 20 5b 73 71 6c 69 74 65  lized by [sqlite
5640: 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2c 20 74  3_finalize()], t
5650: 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f  hen [sqlite3_clo
5660: 73 65 28 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20  se()].**        
5670: 20 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54    returns [SQLIT
5680: 45 5f 42 55 53 59 5d 20 61 6e 64 20 6c 65 61 76  E_BUSY] and leav
5690: 65 73 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f  es the connectio
56a0: 6e 20 6f 70 65 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 46  n open..**.** {F
56b0: 31 32 30 31 34 7d 20 47 69 76 69 6e 67 20 73 71  12014} Giving sq
56c0: 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 20 61 20  lite3_close() a 
56d0: 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 20  NULL pointer is 
56e0: 61 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70  a harmless no-op
56f0: 2e 0a 2a 2a 0a 2a 2a 20 4c 49 4d 49 54 41 54 49  ..**.** LIMITATI
5700: 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 55 31 32 30  ONS:.**.** {U120
5710: 31 35 7d 20 54 68 65 20 70 61 72 61 6d 65 74 65  15} The paramete
5720: 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6c  r to [sqlite3_cl
5730: 6f 73 65 28 29 5d 20 6d 75 73 74 20 62 65 20 61  ose()] must be a
5740: 6e 20 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65  n [sqlite3] obje
5750: 63 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70  ct.**          p
5760: 6f 69 6e 74 65 72 20 70 72 65 76 69 6f 75 73 6c  ointer previousl
5770: 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  y obtained from 
5780: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d  [sqlite3_open()]
5790: 20 6f 72 20 74 68 65 20 0a 2a 2a 20 20 20 20 20   or the .**     
57a0: 20 20 20 20 20 65 71 75 69 76 61 6c 65 6e 74 2c       equivalent,
57b0: 20 6f 72 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20   or NULL..**.** 
57c0: 7b 55 31 32 30 31 36 7d 20 54 68 65 20 70 61 72  {U12016} The par
57d0: 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74  ameter to [sqlit
57e0: 65 33 5f 63 6c 6f 73 65 28 29 5d 20 6d 75 73 74  e3_close()] must
57f0: 20 6e 6f 74 20 68 61 76 65 20 62 65 65 6e 20 70   not have been p
5800: 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 20 20 20  reviously.**    
5810: 20 20 20 20 20 20 63 6c 6f 73 65 64 2e 0a 2a 2f        closed..*/
5820: 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
5830: 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 73 71  sqlite3_close(sq
5840: 6c 69 74 65 33 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  lite3 *);../*.**
5850: 20 54 68 65 20 74 79 70 65 20 66 6f 72 20 61 20   The type for a 
5860: 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f  callback functio
5870: 6e 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 6c 65  n..** This is le
5880: 67 61 63 79 20 61 6e 64 20 64 65 70 72 65 63 61  gacy and depreca
5890: 74 65 64 2e 20 20 49 74 20 69 73 20 69 6e 63 6c  ted.  It is incl
58a0: 75 64 65 64 20 66 6f 72 20 68 69 73 74 6f 72 69  uded for histori
58b0: 63 61 6c 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69  cal.** compatibi
58c0: 6c 69 74 79 20 61 6e 64 20 69 73 20 6e 6f 74 20  lity and is not 
58d0: 64 6f 63 75 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 74  documented..*/.t
58e0: 79 70 65 64 65 66 20 69 6e 74 20 28 2a 73 71 6c  ypedef int (*sql
58f0: 69 74 65 33 5f 63 61 6c 6c 62 61 63 6b 29 28 76  ite3_callback)(v
5900: 6f 69 64 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c  oid*,int,char**,
5910: 20 63 68 61 72 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a   char**);../*.**
5920: 20 43 41 50 49 33 52 45 46 3a 20 4f 6e 65 2d 53   CAPI3REF: One-S
5930: 74 65 70 20 51 75 65 72 79 20 45 78 65 63 75 74  tep Query Execut
5940: 69 6f 6e 20 49 6e 74 65 72 66 61 63 65 20 7b 46  ion Interface {F
5950: 31 32 31 30 30 7d 0a 2a 2a 0a 2a 2a 20 54 68 65  12100}.**.** The
5960: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20   sqlite3_exec() 
5970: 69 6e 74 65 72 66 61 63 65 20 69 73 20 61 20 63  interface is a c
5980: 6f 6e 76 65 6e 69 65 6e 74 20 77 61 79 20 6f 66  onvenient way of
5990: 20 72 75 6e 6e 69 6e 67 0a 2a 2a 20 6f 6e 65 20   running.** one 
59a0: 6f 72 20 6d 6f 72 65 20 53 51 4c 20 73 74 61 74  or more SQL stat
59b0: 65 6d 65 6e 74 73 20 77 69 74 68 6f 75 74 20 61  ements without a
59c0: 20 6c 6f 74 20 6f 66 20 43 20 63 6f 64 65 2e 20   lot of C code. 
59d0: 20 54 68 65 0a 2a 2a 20 53 51 4c 20 73 74 61 74   The.** SQL stat
59e0: 65 6d 65 6e 74 73 20 61 72 65 20 70 61 73 73 65  ements are passe
59f0: 64 20 69 6e 20 61 73 20 74 68 65 20 73 65 63 6f  d in as the seco
5a00: 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0a  nd parameter to.
5a10: 2a 2a 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  ** sqlite3_exec(
5a20: 29 2e 20 20 54 68 65 20 73 74 61 74 65 6d 65 6e  ).  The statemen
5a30: 74 73 20 61 72 65 20 65 76 61 6c 75 61 74 65 64  ts are evaluated
5a40: 20 6f 6e 65 20 62 79 20 6f 6e 65 0a 2a 2a 20 75   one by one.** u
5a50: 6e 74 69 6c 20 65 69 74 68 65 72 20 61 6e 20 65  ntil either an e
5a60: 72 72 6f 72 20 6f 72 20 61 6e 20 69 6e 74 65 72  rror or an inter
5a70: 72 75 70 74 20 69 73 20 65 6e 63 6f 75 6e 74 65  rupt is encounte
5a80: 72 65 64 20 6f 72 0a 2a 2a 20 75 6e 74 69 6c 20  red or.** until 
5a90: 74 68 65 79 20 61 72 65 20 61 6c 6c 20 64 6f 6e  they are all don
5aa0: 65 2e 20 20 54 68 65 20 33 72 64 20 70 61 72 61  e.  The 3rd para
5ab0: 6d 65 74 65 72 20 69 73 20 61 6e 20 6f 70 74 69  meter is an opti
5ac0: 6f 6e 61 6c 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b  onal.** callback
5ad0: 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64   that is invoked
5ae0: 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 72   once for each r
5af0: 6f 77 20 6f 66 20 61 6e 79 20 71 75 65 72 79 20  ow of any query 
5b00: 72 65 73 75 6c 74 73 0a 2a 2a 20 70 72 6f 64 75  results.** produ
5b10: 63 65 64 20 62 79 20 74 68 65 20 53 51 4c 20 73  ced by the SQL s
5b20: 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 65 20  tatements.  The 
5b30: 35 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 65  5th parameter te
5b40: 6c 6c 73 20 77 68 65 72 65 0a 2a 2a 20 74 6f 20  lls where.** to 
5b50: 77 72 69 74 65 20 61 6e 79 20 65 72 72 6f 72 20  write any error 
5b60: 6d 65 73 73 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20  messages..**.** 
5b70: 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63  The sqlite3_exec
5b80: 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  () interface is 
5b90: 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74  implemented in t
5ba0: 65 72 6d 73 20 6f 66 0a 2a 2a 20 5b 73 71 6c 69  erms of.** [sqli
5bb0: 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
5bc0: 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  ], [sqlite3_step
5bd0: 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65  ()], and [sqlite
5be0: 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a  3_finalize()]..*
5bf0: 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78  * The sqlite3_ex
5c00: 65 63 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65  ec() routine doe
5c10: 73 20 6e 6f 74 68 69 6e 67 20 74 68 61 74 20 63  s nothing that c
5c20: 61 6e 6e 6f 74 20 62 65 20 64 6f 6e 65 0a 2a 2a  annot be done.**
5c30: 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 70 72 65   by [sqlite3_pre
5c40: 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b 73 71 6c  pare_v2()], [sql
5c50: 69 74 65 33 5f 73 74 65 70 28 29 5d 2c 20 61 6e  ite3_step()], an
5c60: 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  d [sqlite3_final
5c70: 69 7a 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73  ize()]..** The s
5c80: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 69 73  qlite3_exec() is
5c90: 20 6a 75 73 74 20 61 20 63 6f 6e 76 65 6e 69 65   just a convenie
5ca0: 6e 74 20 77 72 61 70 70 65 72 2e 0a 2a 2a 0a 2a  nt wrapper..**.*
5cb0: 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a  * INVARIANTS:.**
5cc0: 20 0a 2a 2a 20 7b 46 31 32 31 30 31 7d 20 54 68   .** {F12101} Th
5cd0: 65 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28  e [sqlite3_exec(
5ce0: 29 5d 20 69 6e 74 65 72 66 61 63 65 20 65 76 61  )] interface eva
5cf0: 6c 75 61 74 65 73 20 7a 65 72 6f 20 6f 72 20 6d  luates zero or m
5d00: 6f 72 65 20 55 54 46 2d 38 0a 2a 2a 20 20 20 20  ore UTF-8.**    
5d10: 20 20 20 20 20 20 65 6e 63 6f 64 65 64 2c 20 73        encoded, s
5d20: 65 6d 69 63 6f 6c 6f 6e 2d 73 65 70 61 72 61 74  emicolon-separat
5d30: 65 64 2c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ed, SQL statemen
5d40: 74 73 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20  ts in the.**    
5d50: 20 20 20 20 20 20 7a 65 72 6f 2d 74 65 72 6d 69        zero-termi
5d60: 6e 61 74 65 64 20 73 74 72 69 6e 67 20 6f 66 20  nated string of 
5d70: 69 74 73 20 32 6e 64 20 70 61 72 61 6d 65 74 65  its 2nd paramete
5d80: 72 20 77 69 74 68 69 6e 20 74 68 65 0a 2a 2a 20  r within the.** 
5d90: 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65 78 74           context
5da0: 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33   of the [sqlite3
5db0: 5d 20 6f 62 6a 65 63 74 20 67 69 76 65 6e 20 69  ] object given i
5dc0: 6e 20 74 68 65 20 31 73 74 20 70 61 72 61 6d 65  n the 1st parame
5dd0: 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 31  ter..**.** {F121
5de0: 30 34 7d 20 54 68 65 20 72 65 74 75 72 6e 20 76  04} The return v
5df0: 61 6c 75 65 20 6f 66 20 5b 73 71 6c 69 74 65 33  alue of [sqlite3
5e00: 5f 65 78 65 63 28 29 5d 20 69 73 20 53 51 4c 49  _exec()] is SQLI
5e10: 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20  TE_OK if all.** 
5e20: 20 20 20 20 20 20 20 20 20 53 51 4c 20 73 74 61           SQL sta
5e30: 74 65 6d 65 6e 74 73 20 72 75 6e 20 73 75 63 63  tements run succ
5e40: 65 73 73 66 75 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20  essfully..**.** 
5e50: 7b 46 31 32 31 30 35 7d 20 54 68 65 20 72 65 74  {F12105} The ret
5e60: 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 5b 73 71  urn value of [sq
5e70: 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 69 73  lite3_exec()] is
5e80: 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20   an appropriate 
5e90: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 6f 6e  .**          non
5ea0: 2d 7a 65 72 6f 20 65 72 72 6f 72 20 63 6f 64 65  -zero error code
5eb0: 20 69 66 20 61 6e 79 20 53 51 4c 20 73 74 61 74   if any SQL stat
5ec0: 65 6d 65 6e 74 20 66 61 69 6c 73 2e 0a 2a 2a 0a  ement fails..**.
5ed0: 2a 2a 20 7b 46 31 32 31 30 37 7d 20 49 66 20 6f  ** {F12107} If o
5ee0: 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68  ne or more of th
5ef0: 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  e SQL statements
5f00: 20 68 61 6e 64 65 64 20 74 6f 20 5b 73 71 6c 69   handed to [sqli
5f10: 74 65 33 5f 65 78 65 63 28 29 5d 0a 2a 2a 20 20  te3_exec()].**  
5f20: 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
5f30: 65 73 75 6c 74 73 20 61 6e 64 20 74 68 65 20 33  esults and the 3
5f40: 72 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  rd parameter is 
5f50: 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 0a 2a  not NULL, then.*
5f60: 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 63  *          the c
5f70: 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
5f80: 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68   specified by th
5f90: 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20  e 3rd parameter 
5fa0: 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69  is.**          i
5fb0: 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72 20  nvoked once for 
5fc0: 65 61 63 68 20 72 6f 77 20 6f 66 20 72 65 73 75  each row of resu
5fd0: 6c 74 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 31 31  lt..**.** {F1211
5fe0: 30 7d 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61  0} If the callba
5ff0: 63 6b 20 72 65 74 75 72 6e 73 20 61 20 6e 6f 6e  ck returns a non
6000: 2d 7a 65 72 6f 20 76 61 6c 75 65 20 74 68 65 6e  -zero value then
6010: 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29   [sqlite3_exec()
6020: 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 69  ].**          wi
6030: 6c 6c 20 61 62 6f 72 74 65 64 20 74 68 65 20 53  ll aborted the S
6040: 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 74 20  QL statement it 
6050: 69 73 20 63 75 72 72 65 6e 74 6c 79 20 65 76 61  is currently eva
6060: 6c 75 61 74 69 6e 67 2c 0a 2a 2a 20 20 20 20 20  luating,.**     
6070: 20 20 20 20 20 73 6b 69 70 20 61 6c 6c 20 73 75       skip all su
6080: 62 73 65 71 75 65 6e 74 20 53 51 4c 20 73 74 61  bsequent SQL sta
6090: 74 65 6d 65 6e 74 73 2c 20 61 6e 64 20 72 65 74  tements, and ret
60a0: 75 72 6e 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52  urn [SQLITE_ABOR
60b0: 54 5d 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  T]..**          
60c0: 3c 74 6f 64 6f 3e 57 68 61 74 20 68 61 70 70 65  <todo>What happe
60d0: 6e 73 20 74 6f 20 2a 65 72 72 6d 73 67 20 68 65  ns to *errmsg he
60e0: 72 65 3f 20 20 44 6f 65 73 20 74 68 65 20 72 65  re?  Does the re
60f0: 73 75 6c 74 20 63 6f 64 65 20 66 6f 72 0a 2a 2a  sult code for.**
6100: 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
6110: 33 5f 65 72 72 63 6f 64 65 28 29 20 67 65 74 20  3_errcode() get 
6120: 73 65 74 3f 3c 2f 74 6f 64 6f 3e 0a 2a 2a 0a 2a  set?</todo>.**.*
6130: 2a 20 7b 46 31 32 31 31 33 7d 20 54 68 65 20 5b  * {F12113} The [
6140: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20  sqlite3_exec()] 
6150: 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 70 61 73  routine will pas
6160: 73 20 69 74 73 20 34 74 68 20 70 61 72 61 6d 65  s its 4th parame
6170: 74 65 72 20 74 68 72 6f 75 67 68 0a 2a 2a 20 20  ter through.**  
6180: 20 20 20 20 20 20 20 20 61 73 20 74 68 65 20 31          as the 1
6190: 73 74 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20  st parameter of 
61a0: 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a  the callback..**
61b0: 0a 2a 2a 20 7b 46 31 32 31 31 36 7d 20 54 68 65  .** {F12116} The
61c0: 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29   [sqlite3_exec()
61d0: 5d 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74  ] routine sets t
61e0: 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72  he 2nd parameter
61f0: 20 6f 66 20 69 74 73 0a 2a 2a 20 20 20 20 20 20   of its.**      
6200: 20 20 20 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20      callback to 
6210: 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  be the number of
6220: 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
6230: 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 0a 2a  current row of.*
6240: 2a 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c  *          resul
6250: 74 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 31 31 39  t..**.** {F12119
6260: 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 65  } The [sqlite3_e
6270: 78 65 63 28 29 5d 20 72 6f 75 74 69 6e 65 20 73  xec()] routine s
6280: 65 74 73 20 74 68 65 20 33 72 64 20 70 61 72 61  ets the 3rd para
6290: 6d 65 74 65 72 20 6f 66 20 69 74 73 20 0a 2a 2a  meter of its .**
62a0: 20 20 20 20 20 20 20 20 20 20 63 61 6c 6c 62 61            callba
62b0: 63 6b 20 74 6f 20 62 65 20 61 6e 20 61 72 72 61  ck to be an arra
62c0: 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f  y of pointers to
62d0: 20 73 74 72 69 6e 67 73 20 68 6f 6c 64 69 6e 67   strings holding
62e0: 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
62f0: 20 76 61 6c 75 65 73 20 66 6f 72 20 65 61 63 68   values for each
6300: 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 63   column in the c
6310: 75 72 72 65 6e 74 20 72 65 73 75 6c 74 20 73 65  urrent result se
6320: 74 20 72 6f 77 20 61 73 0a 2a 2a 20 20 20 20 20  t row as.**     
6330: 20 20 20 20 20 6f 62 74 61 69 6e 65 64 20 66 72       obtained fr
6340: 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75  om [sqlite3_colu
6350: 6d 6e 5f 74 65 78 74 28 29 5d 2e 0a 2a 2a 0a 2a  mn_text()]..**.*
6360: 2a 20 7b 46 31 32 31 32 32 7d 20 54 68 65 20 5b  * {F12122} The [
6370: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20  sqlite3_exec()] 
6380: 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65  routine sets the
6390: 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 6f   4th parameter o
63a0: 66 20 69 74 73 0a 2a 2a 20 20 20 20 20 20 20 20  f its.**        
63b0: 20 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65    callback to be
63c0: 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 6f 69   an array of poi
63d0: 6e 74 65 72 73 20 74 6f 20 73 74 72 69 6e 67 73  nters to strings
63e0: 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a 2a 2a 20   holding the.** 
63f0: 20 20 20 20 20 20 20 20 20 6e 61 6d 65 73 20 6f           names o
6400: 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73  f result columns
6410: 20 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f   as obtained fro
6420: 6d 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  m [sqlite3_colum
6430: 6e 5f 6e 61 6d 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a  n_name()]..**.**
6440: 20 7b 46 31 32 31 32 35 7d 20 49 66 20 74 68 65   {F12125} If the
6450: 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74   3rd parameter t
6460: 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28  o [sqlite3_exec(
6470: 29 5d 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 0a  )] is NULL then.
6480: 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
6490: 69 74 65 33 5f 65 78 65 63 28 29 5d 20 6e 65 76  ite3_exec()] nev
64a0: 65 72 20 69 6e 76 6f 6b 65 73 20 61 20 63 61 6c  er invokes a cal
64b0: 6c 62 61 63 6b 2e 20 20 41 6c 6c 20 71 75 65 72  lback.  All quer
64c0: 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65  y.**          re
64d0: 73 75 6c 74 73 20 61 72 65 20 73 69 6c 65 6e 74  sults are silent
64e0: 6c 79 20 64 69 73 63 61 72 64 65 64 2e 0a 2a 2a  ly discarded..**
64f0: 0a 2a 2a 20 7b 46 31 32 31 32 38 7d 20 49 66 20  .** {F12128} If 
6500: 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  an error occurs 
6510: 77 68 69 6c 65 20 70 61 72 73 69 6e 67 20 6f 72  while parsing or
6520: 20 65 76 61 6c 75 61 74 69 6e 67 20 61 6e 79 20   evaluating any 
6530: 6f 66 20 74 68 65 20 53 51 4c 0a 2a 2a 20 20 20  of the SQL.**   
6540: 20 20 20 20 20 20 20 73 74 61 74 65 6d 65 6e 74         statement
6550: 73 20 68 61 6e 64 65 64 20 74 6f 20 5b 73 71 6c  s handed to [sql
6560: 69 74 65 33 5f 65 78 65 63 28 29 5d 20 74 68 65  ite3_exec()] the
6570: 6e 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28  n [sqlite3_exec(
6580: 29 5d 20 77 69 6c 6c 0a 2a 2a 20 20 20 20 20 20  )] will.**      
6590: 20 20 20 20 72 65 74 75 72 6e 20 61 6e 20 5b 65      return an [e
65a0: 72 72 6f 72 20 63 6f 64 65 5d 20 6f 74 68 65 72  rror code] other
65b0: 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b   than [SQLITE_OK
65c0: 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 31 33 31  ]..**.** {F12131
65d0: 7d 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  } If an error oc
65e0: 63 75 72 73 20 77 68 69 6c 65 20 70 61 72 73 69  curs while parsi
65f0: 6e 67 20 6f 72 20 65 76 61 6c 75 61 74 69 6e 67  ng or evaluating
6600: 20 61 6e 79 20 6f 66 20 74 68 65 20 53 51 4c 0a   any of the SQL.
6610: 2a 2a 20 20 20 20 20 20 20 20 20 20 68 61 6e 64  **          hand
6620: 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65  ed to [sqlite3_e
6630: 78 65 63 28 29 5d 20 61 6e 64 20 69 66 20 74 68  xec()] and if th
6640: 65 20 35 74 68 20 70 61 72 61 6d 65 74 65 72 20  e 5th parameter 
6650: 28 65 72 72 6d 73 67 29 0a 2a 2a 20 20 20 20 20  (errmsg).**     
6660: 20 20 20 20 20 74 6f 20 5b 73 71 6c 69 74 65 33       to [sqlite3
6670: 5f 65 78 65 63 28 29 5d 20 69 73 20 6e 6f 74 20  _exec()] is not 
6680: 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 6e 20 65 72  NULL, then an er
6690: 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 0a 2a  ror message is.*
66a0: 2a 20 20 20 20 20 20 20 20 20 20 61 6c 6c 6f 63  *          alloc
66b0: 61 74 65 64 20 75 73 69 6e 67 20 74 68 65 20 65  ated using the e
66c0: 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 5b 73 71  quivalent of [sq
66d0: 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 5d  lite3_mprintf()]
66e0: 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20   and.**         
66f0: 20 2a 65 72 72 6d 73 67 20 69 73 20 6d 61 64 65   *errmsg is made
6700: 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 61   to point to tha
6710: 74 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a  t message..**.**
6720: 20 7b 46 31 32 31 33 34 7d 20 54 68 65 20 5b 73   {F12134} The [s
6730: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 72  qlite3_exec()] r
6740: 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20  outine does not 
6750: 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65  change the value
6760: 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   of.**          
6770: 2a 65 72 72 6d 73 67 20 69 66 20 65 72 72 6d 73  *errmsg if errms
6780: 67 20 69 73 20 4e 55 4c 4c 20 6f 72 20 69 66 20  g is NULL or if 
6790: 74 68 65 72 65 20 61 72 65 20 6e 6f 20 65 72 72  there are no err
67a0: 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 31  ors..**.** {F121
67b0: 33 37 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  37} The [sqlite3
67c0: 5f 65 78 65 63 28 29 5d 20 66 75 6e 63 74 69 6f  _exec()] functio
67d0: 6e 20 73 65 74 73 20 74 68 65 20 65 72 72 6f 72  n sets the error
67e0: 20 63 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67   code and messag
67f0: 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 63  e.**          ac
6800: 63 65 73 73 69 62 6c 65 20 76 69 61 20 5b 73 71  cessible via [sq
6810: 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d  lite3_errcode()]
6820: 2c 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  , [sqlite3_errms
6830: 67 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 20 20 20  g()], and.**    
6840: 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65        [sqlite3_e
6850: 72 72 6d 73 67 31 36 28 29 5d 2e 0a 2a 2a 0a 2a  rrmsg16()]..**.*
6860: 2a 20 4c 49 4d 49 54 41 54 49 4f 4e 53 3a 0a 2a  * LIMITATIONS:.*
6870: 2a 0a 2a 2a 20 7b 55 31 32 31 34 31 7d 20 54 68  *.** {U12141} Th
6880: 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
6890: 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78  r to [sqlite3_ex
68a0: 65 63 28 29 5d 20 6d 75 73 74 20 62 65 20 61 6e  ec()] must be an
68b0: 20 76 61 6c 69 64 20 61 6e 64 20 6f 70 65 6e 0a   valid and open.
68c0: 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 64 61 74  **          [dat
68d0: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
68e0: 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 55 31 32 31 34 32  ]..**.** {U12142
68f0: 7d 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  } The database c
6900: 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e  onnection must n
6910: 6f 74 20 62 65 20 63 6c 6f 73 65 64 20 77 68 69  ot be closed whi
6920: 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  le.**          [
6930: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20  sqlite3_exec()] 
6940: 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 20 0a  is running..** .
6950: 2a 2a 20 7b 55 31 32 31 34 33 7d 20 54 68 65 20  ** {U12143} The 
6960: 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  calling function
6970: 20 69 73 20 73 68 6f 75 6c 64 20 75 73 65 20 5b   is should use [
6980: 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20  sqlite3_free()] 
6990: 74 6f 20 66 72 65 65 0a 2a 2a 20 20 20 20 20 20  to free.**      
69a0: 20 20 20 20 74 68 65 20 6d 65 6d 6f 72 79 20 74      the memory t
69b0: 68 61 74 20 2a 65 72 72 6d 73 67 20 69 73 20 6c  hat *errmsg is l
69c0: 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  eft pointing at 
69d0: 6f 6e 63 65 20 74 68 65 20 65 72 72 6f 72 0a 2a  once the error.*
69e0: 2a 20 20 20 20 20 20 20 20 20 20 6d 65 73 73 61  *          messa
69f0: 67 65 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20  ge is no longer 
6a00: 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 55  needed..**.** {U
6a10: 31 32 31 34 35 7d 20 54 68 65 20 53 51 4c 20 73  12145} The SQL s
6a20: 74 61 74 65 6d 65 6e 74 20 74 65 78 74 20 69 6e  tatement text in
6a30: 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74   the 2nd paramet
6a40: 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65  er to [sqlite3_e
6a50: 78 65 63 28 29 5d 0a 2a 2a 20 20 20 20 20 20 20  xec()].**       
6a60: 20 20 20 6d 75 73 74 20 72 65 6d 61 69 6e 20 75     must remain u
6a70: 6e 63 68 61 6e 67 65 64 20 77 68 69 6c 65 20 5b  nchanged while [
6a80: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20  sqlite3_exec()] 
6a90: 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2f 0a 53  is running..*/.S
6aa0: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
6ab0: 6c 69 74 65 33 5f 65 78 65 63 28 0a 20 20 73 71  lite3_exec(.  sq
6ac0: 6c 69 74 65 33 2a 2c 20 20 20 20 20 20 20 20 20  lite3*,         
6ad0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6ae0: 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f           /* An o
6af0: 70 65 6e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  pen database */.
6b00: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71    const char *sq
6b10: 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l,              
6b20: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6b30: 53 51 4c 20 74 6f 20 62 65 20 65 76 61 6c 75 74  SQL to be evalut
6b40: 65 64 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 63 61  ed */.  int (*ca
6b50: 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e  llback)(void*,in
6b60: 74 2c 63 68 61 72 2a 2a 2c 63 68 61 72 2a 2a 29  t,char**,char**)
6b70: 2c 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66  ,  /* Callback f
6b80: 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69  unction */.  voi
6b90: 64 20 2a 2c 20 20 20 20 20 20 20 20 20 20 20 20  d *,            
6ba0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6bb0: 20 20 20 20 20 20 20 20 2f 2a 20 31 73 74 20 61          /* 1st a
6bc0: 72 67 75 6d 65 6e 74 20 74 6f 20 63 61 6c 6c 62  rgument to callb
6bd0: 61 63 6b 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  ack */.  char **
6be0: 65 72 72 6d 73 67 20 20 20 20 20 20 20 20 20 20  errmsg          
6bf0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c00: 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 73 67      /* Error msg
6c10: 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f   written here */
6c20: 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .);../*.** CAPI3
6c30: 52 45 46 3a 20 52 65 73 75 6c 74 20 43 6f 64 65  REF: Result Code
6c40: 73 20 7b 46 31 30 32 31 30 7d 0a 2a 2a 20 4b 45  s {F10210}.** KE
6c50: 59 57 4f 52 44 53 3a 20 53 51 4c 49 54 45 5f 4f  YWORDS: SQLITE_O
6c60: 4b 20 7b 65 72 72 6f 72 20 63 6f 64 65 7d 20 7b  K {error code} {
6c70: 65 72 72 6f 72 20 63 6f 64 65 73 7d 0a 2a 2a 0a  error codes}.**.
6c80: 2a 2a 20 4d 61 6e 79 20 53 51 4c 69 74 65 20 66  ** Many SQLite f
6c90: 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20  unctions return 
6ca0: 61 6e 20 69 6e 74 65 67 65 72 20 72 65 73 75 6c  an integer resul
6cb0: 74 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 65 20  t code from the 
6cc0: 73 65 74 20 73 68 6f 77 6e 0a 2a 2a 20 68 65 72  set shown.** her
6cd0: 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 6e  e in order to in
6ce0: 64 69 63 61 74 65 73 20 73 75 63 63 65 73 73 20  dicates success 
6cf0: 6f 72 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a  or failure..**.*
6d00: 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51 4c  * See also: [SQL
6d10: 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 7c  ITE_IOERR_READ |
6d20: 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74   extended result
6d30: 20 63 6f 64 65 73 5d 0a 2a 2f 0a 23 64 65 66 69   codes].*/.#defi
6d40: 6e 65 20 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20  ne SQLITE_OK    
6d50: 20 20 20 20 20 20 20 30 20 20 20 2f 2a 20 53 75         0   /* Su
6d60: 63 63 65 73 73 66 75 6c 20 72 65 73 75 6c 74 20  ccessful result 
6d70: 2a 2f 0a 2f 2a 20 62 65 67 69 6e 6e 69 6e 67 2d  */./* beginning-
6d80: 6f 66 2d 65 72 72 6f 72 2d 63 6f 64 65 73 20 2a  of-error-codes *
6d90: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
6da0: 5f 45 52 52 4f 52 20 20 20 20 20 20 20 20 31 20  _ERROR        1 
6db0: 20 20 2f 2a 20 53 51 4c 20 65 72 72 6f 72 20 6f    /* SQL error o
6dc0: 72 20 6d 69 73 73 69 6e 67 20 64 61 74 61 62 61  r missing databa
6dd0: 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  se */.#define SQ
6de0: 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c 20 20 20  LITE_INTERNAL   
6df0: 20 20 32 20 20 20 2f 2a 20 49 6e 74 65 72 6e 61    2   /* Interna
6e00: 6c 20 6c 6f 67 69 63 20 65 72 72 6f 72 20 69 6e  l logic error in
6e10: 20 53 51 4c 69 74 65 20 2a 2f 0a 23 64 65 66 69   SQLite */.#defi
6e20: 6e 65 20 53 51 4c 49 54 45 5f 50 45 52 4d 20 20  ne SQLITE_PERM  
6e30: 20 20 20 20 20 20 20 33 20 20 20 2f 2a 20 41 63         3   /* Ac
6e40: 63 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 20  cess permission 
6e50: 64 65 6e 69 65 64 20 2a 2f 0a 23 64 65 66 69 6e  denied */.#defin
6e60: 65 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 20  e SQLITE_ABORT  
6e70: 20 20 20 20 20 20 34 20 20 20 2f 2a 20 43 61 6c        4   /* Cal
6e80: 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 20 72 65  lback routine re
6e90: 71 75 65 73 74 65 64 20 61 6e 20 61 62 6f 72 74  quested an abort
6ea0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
6eb0: 54 45 5f 42 55 53 59 20 20 20 20 20 20 20 20 20  TE_BUSY         
6ec0: 35 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62  5   /* The datab
6ed0: 61 73 65 20 66 69 6c 65 20 69 73 20 6c 6f 63 6b  ase file is lock
6ee0: 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ed */.#define SQ
6ef0: 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 20 20 20 20  LITE_LOCKED     
6f00: 20 20 36 20 20 20 2f 2a 20 41 20 74 61 62 6c 65    6   /* A table
6f10: 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
6f20: 20 69 73 20 6c 6f 63 6b 65 64 20 2a 2f 0a 23 64   is locked */.#d
6f30: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f 4d  efine SQLITE_NOM
6f40: 45 4d 20 20 20 20 20 20 20 20 37 20 20 20 2f 2a  EM        7   /*
6f50: 20 41 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c   A malloc() fail
6f60: 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ed */.#define SQ
6f70: 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 20 20  LITE_READONLY   
6f80: 20 20 38 20 20 20 2f 2a 20 41 74 74 65 6d 70 74    8   /* Attempt
6f90: 20 74 6f 20 77 72 69 74 65 20 61 20 72 65 61 64   to write a read
6fa0: 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f  only database */
6fb0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
6fc0: 49 4e 54 45 52 52 55 50 54 20 20 20 20 39 20 20  INTERRUPT    9  
6fd0: 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 74 65   /* Operation te
6fe0: 72 6d 69 6e 61 74 65 64 20 62 79 20 73 71 6c 69  rminated by sqli
6ff0: 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 2a  te3_interrupt()*
7000: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
7010: 5f 49 4f 45 52 52 20 20 20 20 20 20 20 31 30 20  _IOERR       10 
7020: 20 20 2f 2a 20 53 6f 6d 65 20 6b 69 6e 64 20 6f    /* Some kind o
7030: 66 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72  f disk I/O error
7040: 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 23 64 65   occurred */.#de
7050: 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 52  fine SQLITE_CORR
7060: 55 50 54 20 20 20 20 20 31 31 20 20 20 2f 2a 20  UPT     11   /* 
7070: 54 68 65 20 64 61 74 61 62 61 73 65 20 64 69 73  The database dis
7080: 6b 20 69 6d 61 67 65 20 69 73 20 6d 61 6c 66 6f  k image is malfo
7090: 72 6d 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  rmed */.#define 
70a0: 53 51 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 20  SQLITE_NOTFOUND 
70b0: 20 20 20 31 32 20 20 20 2f 2a 20 4e 4f 54 20 55     12   /* NOT U
70c0: 53 45 44 2e 20 54 61 62 6c 65 20 6f 72 20 72 65  SED. Table or re
70d0: 63 6f 72 64 20 6e 6f 74 20 66 6f 75 6e 64 20 2a  cord not found *
70e0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
70f0: 5f 46 55 4c 4c 20 20 20 20 20 20 20 20 31 33 20  _FULL        13 
7100: 20 20 2f 2a 20 49 6e 73 65 72 74 69 6f 6e 20 66    /* Insertion f
7110: 61 69 6c 65 64 20 62 65 63 61 75 73 65 20 64 61  ailed because da
7120: 74 61 62 61 73 65 20 69 73 20 66 75 6c 6c 20 2a  tabase is full *
7130: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
7140: 5f 43 41 4e 54 4f 50 45 4e 20 20 20 20 31 34 20  _CANTOPEN    14 
7150: 20 20 2f 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f    /* Unable to o
7160: 70 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  pen the database
7170: 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65   file */.#define
7180: 20 53 51 4c 49 54 45 5f 50 52 4f 54 4f 43 4f 4c   SQLITE_PROTOCOL
7190: 20 20 20 20 31 35 20 20 20 2f 2a 20 4e 4f 54 20      15   /* NOT 
71a0: 55 53 45 44 2e 20 44 61 74 61 62 61 73 65 20 6c  USED. Database l
71b0: 6f 63 6b 20 70 72 6f 74 6f 63 6f 6c 20 65 72 72  ock protocol err
71c0: 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  or */.#define SQ
71d0: 4c 49 54 45 5f 45 4d 50 54 59 20 20 20 20 20 20  LITE_EMPTY      
71e0: 20 31 36 20 20 20 2f 2a 20 44 61 74 61 62 61 73   16   /* Databas
71f0: 65 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a 23 64  e is empty */.#d
7200: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 43 48  efine SQLITE_SCH
7210: 45 4d 41 20 20 20 20 20 20 31 37 20 20 20 2f 2a  EMA      17   /*
7220: 20 54 68 65 20 64 61 74 61 62 61 73 65 20 73 63   The database sc
7230: 68 65 6d 61 20 63 68 61 6e 67 65 64 20 2a 2f 0a  hema changed */.
7240: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54  #define SQLITE_T
7250: 4f 4f 42 49 47 20 20 20 20 20 20 31 38 20 20 20  OOBIG      18   
7260: 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f  /* String or BLO
7270: 42 20 65 78 63 65 65 64 73 20 73 69 7a 65 20 6c  B exceeds size l
7280: 69 6d 69 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  imit */.#define 
7290: 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
72a0: 54 20 20 31 39 20 20 20 2f 2a 20 41 62 6f 72 74  T  19   /* Abort
72b0: 20 64 75 65 20 74 6f 20 63 6f 6e 73 74 72 61 69   due to constrai
72c0: 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 2a 2f 0a  nt violation */.
72d0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d  #define SQLITE_M
72e0: 49 53 4d 41 54 43 48 20 20 20 20 32 30 20 20 20  ISMATCH    20   
72f0: 2f 2a 20 44 61 74 61 20 74 79 70 65 20 6d 69 73  /* Data type mis
7300: 6d 61 74 63 68 20 2a 2f 0a 23 64 65 66 69 6e 65  match */.#define
7310: 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 20   SQLITE_MISUSE  
7320: 20 20 20 20 32 31 20 20 20 2f 2a 20 4c 69 62 72      21   /* Libr
7330: 61 72 79 20 75 73 65 64 20 69 6e 63 6f 72 72 65  ary used incorre
7340: 63 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ctly */.#define 
7350: 53 51 4c 49 54 45 5f 4e 4f 4c 46 53 20 20 20 20  SQLITE_NOLFS    
7360: 20 20 20 32 32 20 20 20 2f 2a 20 55 73 65 73 20     22   /* Uses 
7370: 4f 53 20 66 65 61 74 75 72 65 73 20 6e 6f 74 20  OS features not 
7380: 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 68 6f 73  supported on hos
7390: 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  t */.#define SQL
73a0: 49 54 45 5f 41 55 54 48 20 20 20 20 20 20 20 20  ITE_AUTH        
73b0: 32 33 20 20 20 2f 2a 20 41 75 74 68 6f 72 69 7a  23   /* Authoriz
73c0: 61 74 69 6f 6e 20 64 65 6e 69 65 64 20 2a 2f 0a  ation denied */.
73d0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46  #define SQLITE_F
73e0: 4f 52 4d 41 54 20 20 20 20 20 20 32 34 20 20 20  ORMAT      24   
73f0: 2f 2a 20 41 75 78 69 6c 69 61 72 79 20 64 61 74  /* Auxiliary dat
7400: 61 62 61 73 65 20 66 6f 72 6d 61 74 20 65 72 72  abase format err
7410: 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  or */.#define SQ
7420: 4c 49 54 45 5f 52 41 4e 47 45 20 20 20 20 20 20  LITE_RANGE      
7430: 20 32 35 20 20 20 2f 2a 20 32 6e 64 20 70 61 72   25   /* 2nd par
7440: 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65  ameter to sqlite
7450: 33 5f 62 69 6e 64 20 6f 75 74 20 6f 66 20 72 61  3_bind out of ra
7460: 6e 67 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  nge */.#define S
7470: 51 4c 49 54 45 5f 4e 4f 54 41 44 42 20 20 20 20  QLITE_NOTADB    
7480: 20 20 32 36 20 20 20 2f 2a 20 46 69 6c 65 20 6f    26   /* File o
7490: 70 65 6e 65 64 20 74 68 61 74 20 69 73 20 6e 6f  pened that is no
74a0: 74 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c  t a database fil
74b0: 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  e */.#define SQL
74c0: 49 54 45 5f 52 4f 57 20 20 20 20 20 20 20 20 20  ITE_ROW         
74d0: 31 30 30 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f  100  /* sqlite3_
74e0: 73 74 65 70 28 29 20 68 61 73 20 61 6e 6f 74 68  step() has anoth
74f0: 65 72 20 72 6f 77 20 72 65 61 64 79 20 2a 2f 0a  er row ready */.
7500: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
7510: 4f 4e 45 20 20 20 20 20 20 20 20 31 30 31 20 20  ONE        101  
7520: 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  /* sqlite3_step(
7530: 29 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 65  ) has finished e
7540: 78 65 63 75 74 69 6e 67 20 2a 2f 0a 2f 2a 20 65  xecuting */./* e
7550: 6e 64 2d 6f 66 2d 65 72 72 6f 72 2d 63 6f 64 65  nd-of-error-code
7560: 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  s */../*.** CAPI
7570: 33 52 45 46 3a 20 45 78 74 65 6e 64 65 64 20 52  3REF: Extended R
7580: 65 73 75 6c 74 20 43 6f 64 65 73 20 7b 46 31 30  esult Codes {F10
7590: 32 32 30 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  220}.** KEYWORDS
75a0: 3a 20 7b 65 78 74 65 6e 64 65 64 20 65 72 72 6f  : {extended erro
75b0: 72 20 63 6f 64 65 7d 20 7b 65 78 74 65 6e 64 65  r code} {extende
75c0: 64 20 65 72 72 6f 72 20 63 6f 64 65 73 7d 0a 2a  d error codes}.*
75d0: 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 65 78 74  * KEYWORDS: {ext
75e0: 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64  ended result cod
75f0: 65 73 7d 0a 2a 2a 0a 2a 2a 20 49 6e 20 69 74 73  es}.**.** In its
7600: 20 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67 75   default configu
7610: 72 61 74 69 6f 6e 2c 20 53 51 4c 69 74 65 20 41  ration, SQLite A
7620: 50 49 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75  PI routines retu
7630: 72 6e 20 6f 6e 65 20 6f 66 20 32 36 20 69 6e 74  rn one of 26 int
7640: 65 67 65 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  eger.** [SQLITE_
7650: 4f 4b 20 7c 20 72 65 73 75 6c 74 20 63 6f 64 65  OK | result code
7660: 73 5d 2e 20 20 48 6f 77 65 76 65 72 2c 20 65 78  s].  However, ex
7670: 70 65 72 69 65 6e 63 65 20 68 61 73 20 73 68 6f  perience has sho
7680: 77 6e 20 74 68 61 74 0a 2a 2a 20 6d 61 6e 79 20  wn that.** many 
7690: 6f 66 20 74 68 65 73 65 20 72 65 73 75 6c 74 20  of these result 
76a0: 63 6f 64 65 73 20 61 72 65 20 74 6f 6f 20 63 6f  codes are too co
76b0: 75 72 73 65 2d 67 72 61 69 6e 65 64 2e 20 20 54  urse-grained.  T
76c0: 68 65 79 20 64 6f 20 6e 6f 74 20 70 72 6f 76 69  hey do not provi
76d0: 64 65 20 61 73 0a 2a 2a 20 6d 75 63 68 20 69 6e  de as.** much in
76e0: 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
76f0: 70 72 6f 62 6c 65 6d 73 20 61 73 20 70 72 6f 67  problems as prog
7700: 72 61 6d 6d 65 72 73 20 6d 69 67 68 74 20 6c 69  rammers might li
7710: 6b 65 2e 20 20 49 6e 20 61 6e 20 65 66 66 6f 72  ke.  In an effor
7720: 74 20 74 6f 0a 2a 2a 20 61 64 64 72 65 73 73 20  t to.** address 
7730: 74 68 69 73 2c 20 6e 65 77 65 72 20 76 65 72 73  this, newer vers
7740: 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 28  ions of SQLite (
7750: 76 65 72 73 69 6f 6e 20 33 2e 33 2e 38 20 61 6e  version 3.3.8 an
7760: 64 20 6c 61 74 65 72 29 20 69 6e 63 6c 75 64 65  d later) include
7770: 0a 2a 2a 20 73 75 70 70 6f 72 74 20 66 6f 72 20  .** support for 
7780: 61 64 64 69 74 69 6f 6e 61 6c 20 72 65 73 75 6c  additional resul
7790: 74 20 63 6f 64 65 73 20 74 68 61 74 20 70 72 6f  t codes that pro
77a0: 76 69 64 65 20 6d 6f 72 65 20 64 65 74 61 69 6c  vide more detail
77b0: 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a  ed information.*
77c0: 2a 20 61 62 6f 75 74 20 65 72 72 6f 72 73 2e 20  * about errors. 
77d0: 54 68 65 20 65 78 74 65 6e 64 65 64 20 72 65 73  The extended res
77e0: 75 6c 74 20 63 6f 64 65 73 20 61 72 65 20 65 6e  ult codes are en
77f0: 61 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c 65  abled or disable
7800: 64 0a 2a 2a 20 66 6f 72 20 65 61 63 68 20 64 61  d.** for each da
7810: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
7820: 6e 20 75 73 69 6e 67 20 74 68 65 20 5b 73 71 6c  n using the [sql
7830: 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65  ite3_extended_re
7840: 73 75 6c 74 5f 63 6f 64 65 73 28 29 5d 0a 2a 2a  sult_codes()].**
7850: 20 41 50 49 2e 0a 2a 2a 20 0a 2a 2a 20 53 6f 6d   API..** .** Som
7860: 65 20 6f 66 20 74 68 65 20 61 76 61 69 6c 61 62  e of the availab
7870: 6c 65 20 65 78 74 65 6e 64 65 64 20 72 65 73 75  le extended resu
7880: 6c 74 20 63 6f 64 65 73 20 61 72 65 20 6c 69 73  lt codes are lis
7890: 74 65 64 20 68 65 72 65 2e 0a 2a 2a 20 4f 6e 65  ted here..** One
78a0: 20 6d 61 79 20 65 78 70 65 63 74 20 74 68 65 20   may expect the 
78b0: 6e 75 6d 62 65 72 20 6f 66 20 65 78 74 65 6e 64  number of extend
78c0: 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20  ed result codes 
78d0: 77 69 6c 6c 20 62 65 20 65 78 70 61 6e 64 0a 2a  will be expand.*
78e0: 2a 20 6f 76 65 72 20 74 69 6d 65 2e 20 20 53 6f  * over time.  So
78f0: 66 74 77 61 72 65 20 74 68 61 74 20 75 73 65 73  ftware that uses
7900: 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74   extended result
7910: 20 63 6f 64 65 73 20 73 68 6f 75 6c 64 20 65 78   codes should ex
7920: 70 65 63 74 0a 2a 2a 20 74 6f 20 73 65 65 20 6e  pect.** to see n
7930: 65 77 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20  ew result codes 
7940: 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73  in future releas
7950: 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a  es of SQLite..**
7960: 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4f  .** The SQLITE_O
7970: 4b 20 72 65 73 75 6c 74 20 63 6f 64 65 20 77 69  K result code wi
7980: 6c 6c 20 6e 65 76 65 72 20 62 65 20 65 78 74 65  ll never be exte
7990: 6e 64 65 64 2e 20 20 49 74 20 77 69 6c 6c 20 61  nded.  It will a
79a0: 6c 77 61 79 73 0a 2a 2a 20 62 65 20 65 78 61 63  lways.** be exac
79b0: 74 6c 79 20 7a 65 72 6f 2e 0a 2a 2a 20 0a 2a 2a  tly zero..** .**
79c0: 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a   INVARIANTS:.**.
79d0: 2a 2a 20 7b 46 31 30 32 32 33 7d 20 54 68 65 20  ** {F10223} The 
79e0: 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 66 6f  symbolic name fo
79f0: 72 20 61 6e 20 65 78 74 65 6e 64 65 64 20 72 65  r an extended re
7a00: 73 75 6c 74 20 63 6f 64 65 20 61 6c 77 61 79 73  sult code always
7a10: 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 20 20 20   contains.**    
7a20: 20 20 20 20 20 20 61 20 72 65 6c 61 74 65 64 20        a related 
7a30: 70 72 69 6d 61 72 79 20 72 65 73 75 6c 74 20 63  primary result c
7a40: 6f 64 65 20 61 73 20 61 20 70 72 65 66 69 78 2e  ode as a prefix.
7a50: 0a 2a 2a 0a 2a 2a 20 7b 46 31 30 32 32 34 7d 20  .**.** {F10224} 
7a60: 50 72 69 6d 61 72 79 20 72 65 73 75 6c 74 20 63  Primary result c
7a70: 6f 64 65 20 6e 61 6d 65 73 20 63 6f 6e 74 61 69  ode names contai
7a80: 6e 20 61 20 73 69 6e 67 6c 65 20 22 5f 22 20 63  n a single "_" c
7a90: 68 61 72 61 63 74 65 72 2e 0a 2a 2a 0a 2a 2a 20  haracter..**.** 
7aa0: 7b 46 31 30 32 32 35 7d 20 45 78 74 65 6e 64 65  {F10225} Extende
7ab0: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 20 6e 61  d result code na
7ac0: 6d 65 73 20 63 6f 6e 74 61 69 6e 20 74 77 6f 20  mes contain two 
7ad0: 6f 72 20 6d 6f 72 65 20 22 5f 22 20 63 68 61 72  or more "_" char
7ae0: 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 7b 46  acters..**.** {F
7af0: 31 30 32 32 36 7d 20 54 68 65 20 6e 75 6d 65 72  10226} The numer
7b00: 69 63 20 76 61 6c 75 65 20 6f 66 20 61 6e 20 65  ic value of an e
7b10: 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63  xtended result c
7b20: 6f 64 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ode contains the
7b30: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 75 6d  .**          num
7b40: 65 72 69 63 20 76 61 6c 75 65 20 6f 66 20 69 74  eric value of it
7b50: 73 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  s corresponding 
7b60: 70 72 69 6d 61 72 79 20 72 65 73 75 6c 74 20 63  primary result c
7b70: 6f 64 65 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20  ode in.**       
7b80: 20 20 20 69 74 73 20 6c 65 61 73 74 20 73 69 67     its least sig
7b90: 6e 69 66 69 63 61 6e 74 20 38 20 62 69 74 73 2e  nificant 8 bits.
7ba0: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
7bb0: 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 20 20  TE_IOERR_READ   
7bc0: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49         (SQLITE_I
7bd0: 4f 45 52 52 20 7c 20 28 31 3c 3c 38 29 29 0a 23  OERR | (1<<8)).#
7be0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f  define SQLITE_IO
7bf0: 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 20  ERR_SHORT_READ  
7c00: 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20    (SQLITE_IOERR 
7c10: 7c 20 28 32 3c 3c 38 29 29 0a 23 64 65 66 69 6e  | (2<<8)).#defin
7c20: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57  e SQLITE_IOERR_W
7c30: 52 49 54 45 20 20 20 20 20 20 20 20 20 28 53 51  RITE         (SQ
7c40: 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 33 3c  LITE_IOERR | (3<
7c50: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  <8)).#define SQL
7c60: 49 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e 43 20  ITE_IOERR_FSYNC 
7c70: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f          (SQLITE_
7c80: 49 4f 45 52 52 20 7c 20 28 34 3c 3c 38 29 29 0a  IOERR | (4<<8)).
7c90: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
7ca0: 4f 45 52 52 5f 44 49 52 5f 46 53 59 4e 43 20 20  OERR_DIR_FSYNC  
7cb0: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52     (SQLITE_IOERR
7cc0: 20 7c 20 28 35 3c 3c 38 29 29 0a 23 64 65 66 69   | (5<<8)).#defi
7cd0: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  ne SQLITE_IOERR_
7ce0: 54 52 55 4e 43 41 54 45 20 20 20 20 20 20 28 53  TRUNCATE      (S
7cf0: 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 36  QLITE_IOERR | (6
7d00: 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51  <<8)).#define SQ
7d10: 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54  LITE_IOERR_FSTAT
7d20: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45           (SQLITE
7d30: 5f 49 4f 45 52 52 20 7c 20 28 37 3c 3c 38 29 29  _IOERR | (7<<8))
7d40: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
7d50: 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 20 20 20 20  IOERR_UNLOCK    
7d60: 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52      (SQLITE_IOER
7d70: 52 20 7c 20 28 38 3c 3c 38 29 29 0a 23 64 65 66  R | (8<<8)).#def
7d80: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
7d90: 5f 52 44 4c 4f 43 4b 20 20 20 20 20 20 20 20 28  _RDLOCK        (
7da0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
7db0: 39 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53  9<<8)).#define S
7dc0: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45  QLITE_IOERR_DELE
7dd0: 54 45 20 20 20 20 20 20 20 20 28 53 51 4c 49 54  TE        (SQLIT
7de0: 45 5f 49 4f 45 52 52 20 7c 20 28 31 30 3c 3c 38  E_IOERR | (10<<8
7df0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
7e00: 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20  E_IOERR_BLOCKED 
7e10: 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f        (SQLITE_IO
7e20: 45 52 52 20 7c 20 28 31 31 3c 3c 38 29 29 0a 23  ERR | (11<<8)).#
7e30: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f  define SQLITE_IO
7e40: 45 52 52 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 20  ERR_NOMEM       
7e50: 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20    (SQLITE_IOERR 
7e60: 7c 20 28 31 32 3c 3c 38 29 29 0a 0a 2f 2a 0a 2a  | (12<<8))../*.*
7e70: 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6c 61 67  * CAPI3REF: Flag
7e80: 73 20 46 6f 72 20 46 69 6c 65 20 4f 70 65 6e 20  s For File Open 
7e90: 4f 70 65 72 61 74 69 6f 6e 73 20 7b 46 31 30 32  Operations {F102
7ea0: 33 30 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  30}.**.** These 
7eb0: 62 69 74 20 76 61 6c 75 65 73 20 61 72 65 20 69  bit values are i
7ec0: 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20  ntended for use 
7ed0: 69 6e 20 74 68 65 0a 2a 2a 20 33 72 64 20 70 61  in the.** 3rd pa
7ee0: 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 5b  rameter to the [
7ef0: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
7f00: 29 5d 20 69 6e 74 65 72 66 61 63 65 20 61 6e 64  )] interface and
7f10: 0a 2a 2a 20 69 6e 20 74 68 65 20 34 74 68 20 70  .** in the 4th p
7f20: 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20  arameter to the 
7f30: 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 6f 66 20  xOpen method of 
7f40: 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  the.** [sqlite3_
7f50: 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a  vfs] object..*/.
7f60: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
7f70: 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20  PEN_READONLY    
7f80: 20 20 20 20 20 30 78 30 30 30 30 30 30 30 31 0a       0x00000001.
7f90: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
7fa0: 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 20 20  PEN_READWRITE   
7fb0: 20 20 20 20 20 30 78 30 30 30 30 30 30 30 32 0a       0x00000002.
7fc0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
7fd0: 50 45 4e 5f 43 52 45 41 54 45 20 20 20 20 20 20  PEN_CREATE      
7fe0: 20 20 20 20 20 30 78 30 30 30 30 30 30 30 34 0a       0x00000004.
7ff0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
8000: 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53  PEN_DELETEONCLOS
8010: 45 20 20 20 20 30 78 30 30 30 30 30 30 30 38 0a  E    0x00000008.
8020: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
8030: 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 20 20  PEN_EXCLUSIVE   
8040: 20 20 20 20 20 30 78 30 30 30 30 30 30 31 30 0a       0x00000010.
8050: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
8060: 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 20 20 20 20  PEN_MAIN_DB     
8070: 20 20 20 20 20 30 78 30 30 30 30 30 31 30 30 0a       0x00000100.
8080: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
8090: 50 45 4e 5f 54 45 4d 50 5f 44 42 20 20 20 20 20  PEN_TEMP_DB     
80a0: 20 20 20 20 20 30 78 30 30 30 30 30 32 30 30 0a       0x00000200.
80b0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
80c0: 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42  PEN_TRANSIENT_DB
80d0: 20 20 20 20 20 30 78 30 30 30 30 30 34 30 30 0a       0x00000400.
80e0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
80f0: 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c  PEN_MAIN_JOURNAL
8100: 20 20 20 20 20 30 78 30 30 30 30 30 38 30 30 0a       0x00000800.
8110: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
8120: 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c  PEN_TEMP_JOURNAL
8130: 20 20 20 20 20 30 78 30 30 30 30 31 30 30 30 0a       0x00001000.
8140: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
8150: 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 20 20  PEN_SUBJOURNAL  
8160: 20 20 20 20 20 30 78 30 30 30 30 32 30 30 30 0a       0x00002000.
8170: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f  #define SQLITE_O
8180: 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e  PEN_MASTER_JOURN
8190: 41 4c 20 20 20 30 78 30 30 30 30 34 30 30 30 0a  AL   0x00004000.
81a0: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
81b0: 20 44 65 76 69 63 65 20 43 68 61 72 61 63 74 65   Device Characte
81c0: 72 69 73 74 69 63 73 20 7b 46 31 30 32 34 30 7d  ristics {F10240}
81d0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 44 65 76 69  .**.** The xDevi
81e0: 63 65 43 61 70 61 62 69 6c 69 74 69 65 73 20 6d  ceCapabilities m
81f0: 65 74 68 6f 64 20 6f 66 20 74 68 65 20 5b 73 71  ethod of the [sq
8200: 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
8210: 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 72 65 74 75  ].** object retu
8220: 72 6e 73 20 61 6e 20 69 6e 74 65 67 65 72 20 77  rns an integer w
8230: 68 69 63 68 20 69 73 20 61 20 76 65 63 74 6f 72  hich is a vector
8240: 20 6f 66 20 74 68 65 20 74 68 65 73 65 0a 2a 2a   of the these.**
8250: 20 62 69 74 20 76 61 6c 75 65 73 20 65 78 70 72   bit values expr
8260: 65 73 73 69 6e 67 20 49 2f 4f 20 63 68 61 72 61  essing I/O chara
8270: 63 74 65 72 69 73 74 69 63 73 20 6f 66 20 74 68  cteristics of th
8280: 65 20 6d 61 73 73 20 73 74 6f 72 61 67 65 0a 2a  e mass storage.*
8290: 2a 20 64 65 76 69 63 65 20 74 68 61 74 20 68 6f  * device that ho
82a0: 6c 64 73 20 74 68 65 20 66 69 6c 65 20 74 68 61  lds the file tha
82b0: 74 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69  t the [sqlite3_i
82c0: 6f 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 72 65  o_methods].** re
82d0: 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54  fers to..**.** T
82e0: 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  he SQLITE_IOCAP_
82f0: 41 54 4f 4d 49 43 20 70 72 6f 70 65 72 74 79 20  ATOMIC property 
8300: 6d 65 61 6e 73 20 74 68 61 74 20 61 6c 6c 20 77  means that all w
8310: 72 69 74 65 73 20 6f 66 0a 2a 2a 20 61 6e 79 20  rites of.** any 
8320: 73 69 7a 65 20 61 72 65 20 61 74 6f 6d 69 63 2e  size are atomic.
8330: 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43    The SQLITE_IOC
8340: 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e 20 76 61 6c  AP_ATOMICnnn val
8350: 75 65 73 0a 2a 2a 20 6d 65 61 6e 20 74 68 61 74  ues.** mean that
8360: 20 77 72 69 74 65 73 20 6f 66 20 62 6c 6f 63 6b   writes of block
8370: 73 20 74 68 61 74 20 61 72 65 20 6e 6e 6e 20 62  s that are nnn b
8380: 79 74 65 73 20 69 6e 20 73 69 7a 65 20 61 6e 64  ytes in size and
8390: 0a 2a 2a 20 61 72 65 20 61 6c 69 67 6e 65 64 20  .** are aligned 
83a0: 74 6f 20 61 6e 20 61 64 64 72 65 73 73 20 77 68  to an address wh
83b0: 69 63 68 20 69 73 20 61 6e 20 69 6e 74 65 67 65  ich is an intege
83c0: 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 0a 2a 2a  r multiple of.**
83d0: 20 6e 6e 6e 20 61 72 65 20 61 74 6f 6d 69 63 2e   nnn are atomic.
83e0: 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43    The SQLITE_IOC
83f0: 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 76  AP_SAFE_APPEND v
8400: 61 6c 75 65 20 6d 65 61 6e 73 0a 2a 2a 20 74 68  alue means.** th
8410: 61 74 20 77 68 65 6e 20 64 61 74 61 20 69 73 20  at when data is 
8420: 61 70 70 65 6e 64 65 64 20 74 6f 20 61 20 66 69  appended to a fi
8430: 6c 65 2c 20 74 68 65 20 64 61 74 61 20 69 73 20  le, the data is 
8440: 61 70 70 65 6e 64 65 64 0a 2a 2a 20 66 69 72 73  appended.** firs
8450: 74 20 74 68 65 6e 20 74 68 65 20 73 69 7a 65 20  t then the size 
8460: 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 65  of the file is e
8470: 78 74 65 6e 64 65 64 2c 20 6e 65 76 65 72 20 74  xtended, never t
8480: 68 65 20 6f 74 68 65 72 0a 2a 2a 20 77 61 79 20  he other.** way 
8490: 61 72 6f 75 6e 64 2e 20 20 54 68 65 20 53 51 4c  around.  The SQL
84a0: 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e  ITE_IOCAP_SEQUEN
84b0: 54 49 41 4c 20 70 72 6f 70 65 72 74 79 20 6d 65  TIAL property me
84c0: 61 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e 66 6f  ans that.** info
84d0: 72 6d 61 74 69 6f 6e 20 69 73 20 77 72 69 74 74  rmation is writt
84e0: 65 6e 20 74 6f 20 64 69 73 6b 20 69 6e 20 74 68  en to disk in th
84f0: 65 20 73 61 6d 65 20 6f 72 64 65 72 20 61 73 20  e same order as 
8500: 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 78 57 72 69  calls.** to xWri
8510: 74 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  te()..*/.#define
8520: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
8530: 4f 4d 49 43 20 20 20 20 20 20 20 20 20 20 30 78  OMIC          0x
8540: 30 30 30 30 30 30 30 31 0a 23 64 65 66 69 6e 65  00000001.#define
8550: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
8560: 4f 4d 49 43 35 31 32 20 20 20 20 20 20 20 30 78  OMIC512       0x
8570: 30 30 30 30 30 30 30 32 0a 23 64 65 66 69 6e 65  00000002.#define
8580: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
8590: 4f 4d 49 43 31 4b 20 20 20 20 20 20 20 20 30 78  OMIC1K        0x
85a0: 30 30 30 30 30 30 30 34 0a 23 64 65 66 69 6e 65  00000004.#define
85b0: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
85c0: 4f 4d 49 43 32 4b 20 20 20 20 20 20 20 20 30 78  OMIC2K        0x
85d0: 30 30 30 30 30 30 30 38 0a 23 64 65 66 69 6e 65  00000008.#define
85e0: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
85f0: 4f 4d 49 43 34 4b 20 20 20 20 20 20 20 20 30 78  OMIC4K        0x
8600: 30 30 30 30 30 30 31 30 0a 23 64 65 66 69 6e 65  00000010.#define
8610: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
8620: 4f 4d 49 43 38 4b 20 20 20 20 20 20 20 20 30 78  OMIC8K        0x
8630: 30 30 30 30 30 30 32 30 0a 23 64 65 66 69 6e 65  00000020.#define
8640: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
8650: 4f 4d 49 43 31 36 4b 20 20 20 20 20 20 20 30 78  OMIC16K       0x
8660: 30 30 30 30 30 30 34 30 0a 23 64 65 66 69 6e 65  00000040.#define
8670: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
8680: 4f 4d 49 43 33 32 4b 20 20 20 20 20 20 20 30 78  OMIC32K       0x
8690: 30 30 30 30 30 30 38 30 0a 23 64 65 66 69 6e 65  00000080.#define
86a0: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
86b0: 4f 4d 49 43 36 34 4b 20 20 20 20 20 20 20 30 78  OMIC64K       0x
86c0: 30 30 30 30 30 31 30 30 0a 23 64 65 66 69 6e 65  00000100.#define
86d0: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41   SQLITE_IOCAP_SA
86e0: 46 45 5f 41 50 50 45 4e 44 20 20 20 20 20 30 78  FE_APPEND     0x
86f0: 30 30 30 30 30 32 30 30 0a 23 64 65 66 69 6e 65  00000200.#define
8700: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45   SQLITE_IOCAP_SE
8710: 51 55 45 4e 54 49 41 4c 20 20 20 20 20 20 30 78  QUENTIAL      0x
8720: 30 30 30 30 30 34 30 30 0a 0a 2f 2a 0a 2a 2a 20  00000400../*.** 
8730: 43 41 50 49 33 52 45 46 3a 20 46 69 6c 65 20 4c  CAPI3REF: File L
8740: 6f 63 6b 69 6e 67 20 4c 65 76 65 6c 73 20 7b 46  ocking Levels {F
8750: 31 30 32 35 30 7d 0a 2a 2a 0a 2a 2a 20 53 51 4c  10250}.**.** SQL
8760: 69 74 65 20 75 73 65 73 20 6f 6e 65 20 6f 66 20  ite uses one of 
8770: 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 76 61  these integer va
8780: 6c 75 65 73 20 61 73 20 74 68 65 20 73 65 63 6f  lues as the seco
8790: 6e 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74  nd.** argument t
87a0: 6f 20 63 61 6c 6c 73 20 69 74 20 6d 61 6b 65 73  o calls it makes
87b0: 20 74 6f 20 74 68 65 20 78 4c 6f 63 6b 28 29 20   to the xLock() 
87c0: 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 20 6d 65  and xUnlock() me
87d0: 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 61 6e 20 5b  thods.** of an [
87e0: 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
87f0: 64 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 23  ds] object..*/.#
8800: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f  define SQLITE_LO
8810: 43 4b 5f 4e 4f 4e 45 20 20 20 20 20 20 20 20 20  CK_NONE         
8820: 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   0.#define SQLIT
8830: 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 20 20 20  E_LOCK_SHARED   
8840: 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53       1.#define S
8850: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 52  QLITE_LOCK_RESER
8860: 56 45 44 20 20 20 20 20 20 32 0a 23 64 65 66 69  VED      2.#defi
8870: 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50  ne SQLITE_LOCK_P
8880: 45 4e 44 49 4e 47 20 20 20 20 20 20 20 33 0a 23  ENDING       3.#
8890: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f  define SQLITE_LO
88a0: 43 4b 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20  CK_EXCLUSIVE    
88b0: 20 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52   4../*.** CAPI3R
88c0: 45 46 3a 20 53 79 6e 63 68 72 6f 6e 69 7a 61 74  EF: Synchronizat
88d0: 69 6f 6e 20 54 79 70 65 20 46 6c 61 67 73 20 7b  ion Type Flags {
88e0: 46 31 30 32 36 30 7d 0a 2a 2a 0a 2a 2a 20 57 68  F10260}.**.** Wh
88f0: 65 6e 20 53 51 4c 69 74 65 20 69 6e 76 6f 6b 65  en SQLite invoke
8900: 73 20 74 68 65 20 78 53 79 6e 63 28 29 20 6d 65  s the xSync() me
8910: 74 68 6f 64 20 6f 66 20 61 6e 0a 2a 2a 20 5b 73  thod of an.** [s
8920: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
8930: 73 5d 20 6f 62 6a 65 63 74 20 69 74 20 75 73 65  s] object it use
8940: 73 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20  s a combination 
8950: 6f 66 0a 2a 2a 20 74 68 65 73 65 20 69 6e 74 65  of.** these inte
8960: 67 65 72 20 76 61 6c 75 65 73 20 61 73 20 74 68  ger values as th
8970: 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
8980: 74 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68  t..**.** When th
8990: 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41  e SQLITE_SYNC_DA
89a0: 54 41 4f 4e 4c 59 20 66 6c 61 67 20 69 73 20 75  TAONLY flag is u
89b0: 73 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 74 68  sed, it means th
89c0: 61 74 20 74 68 65 0a 2a 2a 20 73 79 6e 63 20 6f  at the.** sync o
89d0: 70 65 72 61 74 69 6f 6e 20 6f 6e 6c 79 20 6e 65  peration only ne
89e0: 65 64 73 20 74 6f 20 66 6c 75 73 68 20 64 61 74  eds to flush dat
89f0: 61 20 74 6f 20 6d 61 73 73 20 73 74 6f 72 61 67  a to mass storag
8a00: 65 2e 20 20 49 6e 6f 64 65 0a 2a 2a 20 69 6e 66  e.  Inode.** inf
8a10: 6f 72 6d 61 74 69 6f 6e 20 6e 65 65 64 20 6e 6f  ormation need no
8a20: 74 20 62 65 20 66 6c 75 73 68 65 64 2e 20 54 68  t be flushed. Th
8a30: 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f  e SQLITE_SYNC_NO
8a40: 52 4d 41 4c 20 66 6c 61 67 20 6d 65 61 6e 73 20  RMAL flag means 
8a50: 0a 2a 2a 20 74 6f 20 75 73 65 20 6e 6f 72 6d 61  .** to use norma
8a60: 6c 20 66 73 79 6e 63 28 29 20 73 65 6d 61 6e 74  l fsync() semant
8a70: 69 63 73 2e 20 54 68 65 20 53 51 4c 49 54 45 5f  ics. The SQLITE_
8a80: 53 59 4e 43 5f 46 55 4c 4c 20 66 6c 61 67 20 6d  SYNC_FULL flag m
8a90: 65 61 6e 73 20 0a 2a 2a 20 74 6f 20 75 73 65 20  eans .** to use 
8aa0: 4d 61 63 20 4f 53 2d 58 20 73 74 79 6c 65 20 66  Mac OS-X style f
8ab0: 75 6c 6c 73 79 6e 63 20 69 6e 73 74 65 61 64 20  ullsync instead 
8ac0: 6f 66 20 66 73 79 6e 63 28 29 2e 0a 2a 2f 0a 23  of fsync()..*/.#
8ad0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59  define SQLITE_SY
8ae0: 4e 43 5f 4e 4f 52 4d 41 4c 20 20 20 20 20 20 20  NC_NORMAL       
8af0: 20 30 78 30 30 30 30 32 0a 23 64 65 66 69 6e 65   0x00002.#define
8b00: 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c   SQLITE_SYNC_FUL
8b10: 4c 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30  L          0x000
8b20: 30 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  03.#define SQLIT
8b30: 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20  E_SYNC_DATAONLY 
8b40: 20 20 20 20 20 30 78 30 30 30 31 30 0a 0a 0a 2f       0x00010.../
8b50: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f  *.** CAPI3REF: O
8b60: 53 20 49 6e 74 65 72 66 61 63 65 20 4f 70 65 6e  S Interface Open
8b70: 20 46 69 6c 65 20 48 61 6e 64 6c 65 20 7b 46 31   File Handle {F1
8b80: 31 31 31 30 7d 0a 2a 2a 0a 2a 2a 20 41 6e 20 5b  1110}.**.** An [
8b90: 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62  sqlite3_file] ob
8ba0: 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 73 20  ject represents 
8bb0: 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 69 6e 20  an open file in 
8bc0: 74 68 65 20 4f 53 0a 2a 2a 20 69 6e 74 65 72 66  the OS.** interf
8bd0: 61 63 65 20 6c 61 79 65 72 2e 20 20 49 6e 64 69  ace layer.  Indi
8be0: 76 69 64 75 61 6c 20 4f 53 20 69 6e 74 65 72 66  vidual OS interf
8bf0: 61 63 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ace implementati
8c00: 6f 6e 73 20 77 69 6c 6c 0a 2a 2a 20 77 61 6e 74  ons will.** want
8c10: 20 74 6f 20 73 75 62 63 6c 61 73 73 20 74 68 69   to subclass thi
8c20: 73 20 6f 62 6a 65 63 74 20 62 79 20 61 70 70 65  s object by appe
8c30: 6e 64 69 6e 67 20 61 64 64 69 74 69 6f 6e 61 6c  nding additional
8c40: 20 66 69 65 6c 64 73 0a 2a 2a 20 66 6f 72 20 74   fields.** for t
8c50: 68 65 69 72 20 6f 77 6e 20 75 73 65 2e 20 20 54  heir own use.  T
8c60: 68 65 20 70 4d 65 74 68 6f 64 73 20 65 6e 74 72  he pMethods entr
8c70: 79 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  y is a pointer t
8c80: 6f 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  o an.** [sqlite3
8c90: 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a  _io_methods] obj
8ca0: 65 63 74 20 74 68 61 74 20 64 65 66 69 6e 65 73  ect that defines
8cb0: 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 70 65 72   methods for per
8cc0: 66 6f 72 6d 69 6e 67 0a 2a 2a 20 49 2f 4f 20 6f  forming.** I/O o
8cd0: 70 65 72 61 74 69 6f 6e 73 20 6f 6e 20 74 68 65  perations on the
8ce0: 20 6f 70 65 6e 20 66 69 6c 65 2e 0a 2a 2f 0a 74   open file..*/.t
8cf0: 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
8d00: 6c 69 74 65 33 5f 66 69 6c 65 20 73 71 6c 69 74  lite3_file sqlit
8d10: 65 33 5f 66 69 6c 65 3b 0a 73 74 72 75 63 74 20  e3_file;.struct 
8d20: 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 7b 0a 20  sqlite3_file {. 
8d30: 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73 71   const struct sq
8d40: 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
8d50: 20 2a 70 4d 65 74 68 6f 64 73 3b 20 20 2f 2a 20   *pMethods;  /* 
8d60: 4d 65 74 68 6f 64 73 20 66 6f 72 20 61 6e 20 6f  Methods for an o
8d70: 70 65 6e 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a  pen file */.};..
8d80: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
8d90: 4f 53 20 49 6e 74 65 72 66 61 63 65 20 46 69 6c  OS Interface Fil
8da0: 65 20 56 69 72 74 75 61 6c 20 4d 65 74 68 6f 64  e Virtual Method
8db0: 73 20 4f 62 6a 65 63 74 20 7b 46 31 31 31 32 30  s Object {F11120
8dc0: 7d 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 66 69  }.**.** Every fi
8dd0: 6c 65 20 6f 70 65 6e 65 64 20 62 79 20 74 68 65  le opened by the
8de0: 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 78   [sqlite3_vfs] x
8df0: 4f 70 65 6e 20 6d 65 74 68 6f 64 20 63 6f 6e 74  Open method cont
8e00: 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  ains a pointer t
8e10: 6f 0a 2a 2a 20 61 6e 20 69 6e 73 74 61 6e 63 65  o.** an instance
8e20: 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 2e   of this object.
8e30: 20 20 54 68 69 73 20 6f 62 6a 65 63 74 20 64 65    This object de
8e40: 66 69 6e 65 73 20 74 68 65 0a 2a 2a 20 6d 65 74  fines the.** met
8e50: 68 6f 64 73 20 75 73 65 64 20 74 6f 20 70 65 72  hods used to per
8e60: 66 6f 72 6d 20 76 61 72 69 6f 75 73 20 6f 70 65  form various ope
8e70: 72 61 74 69 6f 6e 73 20 61 67 61 69 6e 73 74 20  rations against 
8e80: 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 2e 0a 2a  the open file..*
8e90: 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 61  *.** The flags a
8ea0: 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 79 6e 63  rgument to xSync
8eb0: 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f 66 20 5b   may be one of [
8ec0: 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d  SQLITE_SYNC_NORM
8ed0: 41 4c 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54  AL] or.** [SQLIT
8ee0: 45 5f 53 59 4e 43 5f 46 55 4c 4c 5d 2e 20 20 54  E_SYNC_FULL].  T
8ef0: 68 65 20 66 69 72 73 74 20 63 68 6f 69 63 65 20  he first choice 
8f00: 69 73 20 74 68 65 20 6e 6f 72 6d 61 6c 20 66 73  is the normal fs
8f10: 79 6e 63 28 29 2e 0a 2a 20 20 54 68 65 20 73 65  ync()..*  The se
8f20: 63 6f 6e 64 20 63 68 6f 69 63 65 20 69 73 20 61  cond choice is a
8f30: 6e 0a 2a 2a 20 4f 53 2d 58 20 73 74 79 6c 65 20  n.** OS-X style 
8f40: 66 75 6c 6c 73 79 6e 63 2e 20 20 54 68 65 20 53  fullsync.  The S
8f50: 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 20  QLITE_SYNC_DATA 
8f60: 66 6c 61 67 20 6d 61 79 20 62 65 20 4f 52 65 64  flag may be ORed
8f70: 20 69 6e 20 74 6f 0a 2a 2a 20 69 6e 64 69 63 61   in to.** indica
8f80: 74 65 20 74 68 61 74 20 6f 6e 6c 79 20 74 68 65  te that only the
8f90: 20 64 61 74 61 20 6f 66 20 74 68 65 20 66 69 6c   data of the fil
8fa0: 65 20 61 6e 64 20 6e 6f 74 20 69 74 73 20 69 6e  e and not its in
8fb0: 6f 64 65 20 6e 65 65 64 73 20 74 6f 20 62 65 0a  ode needs to be.
8fc0: 2a 2a 20 73 79 6e 63 65 64 2e 0a 2a 2a 20 0a 2a  ** synced..** .*
8fd0: 2a 20 54 68 65 20 69 6e 74 65 67 65 72 20 76 61  * The integer va
8fe0: 6c 75 65 73 20 74 6f 20 78 4c 6f 63 6b 28 29 20  lues to xLock() 
8ff0: 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 20 61 72  and xUnlock() ar
9000: 65 20 6f 6e 65 20 6f 66 0a 2a 2a 20 3c 75 6c 3e  e one of.** <ul>
9010: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
9020: 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c 0a 2a 2a 20  _LOCK_NONE],.** 
9030: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43  <li> [SQLITE_LOC
9040: 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a 2a 20 3c 6c  K_SHARED],.** <l
9050: 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f  i> [SQLITE_LOCK_
9060: 52 45 53 45 52 56 45 44 5d 2c 0a 2a 2a 20 3c 6c  RESERVED],.** <l
9070: 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f  i> [SQLITE_LOCK_
9080: 50 45 4e 44 49 4e 47 5d 2c 20 6f 72 0a 2a 2a 20  PENDING], or.** 
9090: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43  <li> [SQLITE_LOC
90a0: 4b 5f 45 58 43 4c 55 53 49 56 45 5d 2e 0a 2a 2a  K_EXCLUSIVE]..**
90b0: 20 3c 2f 75 6c 3e 0a 2a 2a 20 78 4c 6f 63 6b 28   </ul>.** xLock(
90c0: 29 20 69 6e 63 72 65 61 73 65 73 20 74 68 65 20  ) increases the 
90d0: 6c 6f 63 6b 2e 20 78 55 6e 6c 6f 63 6b 28 29 20  lock. xUnlock() 
90e0: 64 65 63 72 65 61 73 65 73 20 74 68 65 20 6c 6f  decreases the lo
90f0: 63 6b 2e 20 20 0a 2a 2a 20 54 68 65 20 78 43 68  ck.  .** The xCh
9100: 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
9110: 29 20 6d 65 74 68 6f 64 20 6c 6f 6f 6b 73 0a 2a  ) method looks.*
9120: 2a 20 74 6f 20 73 65 65 20 69 66 20 61 6e 79 20  * to see if any 
9130: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
9140: 69 6f 6e 2c 20 65 69 74 68 65 72 20 69 6e 20 74  ion, either in t
9150: 68 69 73 0a 2a 2a 20 70 72 6f 63 65 73 73 20 6f  his.** process o
9160: 72 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72 20  r in some other 
9170: 70 72 6f 63 65 73 73 2c 20 69 73 20 68 6f 6c 64  process, is hold
9180: 69 6e 67 20 61 6e 20 52 45 53 45 52 56 45 44 2c  ing an RESERVED,
9190: 0a 2a 2a 20 50 45 4e 44 49 4e 47 2c 20 6f 72 20  .** PENDING, or 
91a0: 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f  EXCLUSIVE lock o
91b0: 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 49 74 20  n the file.  It 
91c0: 72 65 74 75 72 6e 73 20 74 72 75 65 0a 2a 2a 20  returns true.** 
91d0: 69 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 65  if such a lock e
91e0: 78 69 73 74 73 20 61 6e 64 20 66 61 6c 73 65 20  xists and false 
91f0: 69 66 20 6e 6f 74 2e 0a 2a 2a 20 0a 2a 2a 20 54  if not..** .** T
9200: 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28  he xFileControl(
9210: 29 20 6d 65 74 68 6f 64 20 69 73 20 61 20 67 65  ) method is a ge
9220: 6e 65 72 69 63 20 69 6e 74 65 72 66 61 63 65 20  neric interface 
9230: 74 68 61 74 20 61 6c 6c 6f 77 73 20 63 75 73 74  that allows cust
9240: 6f 6d 0a 2a 2a 20 56 46 53 20 69 6d 70 6c 65 6d  om.** VFS implem
9250: 65 6e 74 61 74 69 6f 6e 73 20 74 6f 20 64 69 72  entations to dir
9260: 65 63 74 6c 79 20 63 6f 6e 74 72 6f 6c 20 61 6e  ectly control an
9270: 20 6f 70 65 6e 20 66 69 6c 65 20 75 73 69 6e 67   open file using
9280: 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   the.** [sqlite3
9290: 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d  _file_control()]
92a0: 20 69 6e 74 65 72 66 61 63 65 2e 20 20 54 68 65   interface.  The
92b0: 20 73 65 63 6f 6e 64 20 22 6f 70 22 20 61 72 67   second "op" arg
92c0: 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 61 6e 20 69  ument.** is an i
92d0: 6e 74 65 67 65 72 20 6f 70 63 6f 64 65 2e 20 20  nteger opcode.  
92e0: 20 54 68 65 20 74 68 69 72 64 0a 2a 2a 20 61 72   The third.** ar
92f0: 67 75 6d 65 6e 74 20 69 73 20 61 20 67 65 6e 65  gument is a gene
9300: 72 69 63 20 70 6f 69 6e 74 65 72 20 77 68 69 63  ric pointer whic
9310: 68 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f  h is intended to
9320: 20 62 65 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a   be a pointer.**
9330: 20 74 6f 20 61 20 73 74 72 75 63 74 75 72 65 20   to a structure 
9340: 74 68 61 74 20 6d 61 79 20 63 6f 6e 74 61 69 6e  that may contain
9350: 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20 73 70   arguments or sp
9360: 61 63 65 20 69 6e 20 77 68 69 63 68 20 74 6f 0a  ace in which to.
9370: 2a 2a 20 77 72 69 74 65 20 72 65 74 75 72 6e 20  ** write return 
9380: 76 61 6c 75 65 73 2e 20 20 50 6f 74 65 6e 74 69  values.  Potenti
9390: 61 6c 20 75 73 65 73 20 66 6f 72 20 78 46 69 6c  al uses for xFil
93a0: 65 43 6f 6e 74 72 6f 6c 28 29 20 6d 69 67 68 74  eControl() might
93b0: 20 62 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73   be.** functions
93c0: 20 74 6f 20 65 6e 61 62 6c 65 20 62 6c 6f 63 6b   to enable block
93d0: 69 6e 67 20 6c 6f 63 6b 73 20 77 69 74 68 20 74  ing locks with t
93e0: 69 6d 65 6f 75 74 73 2c 20 74 6f 20 63 68 61 6e  imeouts, to chan
93f0: 67 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 69 6e  ge the.** lockin
9400: 67 20 73 74 72 61 74 65 67 79 20 28 66 6f 72 20  g strategy (for 
9410: 65 78 61 6d 70 6c 65 20 74 6f 20 75 73 65 20 64  example to use d
9420: 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 73 29 2c 20  ot-file locks), 
9430: 74 6f 20 69 6e 71 75 69 72 65 0a 2a 2a 20 61 62  to inquire.** ab
9440: 6f 75 74 20 74 68 65 20 73 74 61 74 75 73 20 6f  out the status o
9450: 66 20 61 20 6c 6f 63 6b 2c 20 6f 72 20 74 6f 20  f a lock, or to 
9460: 62 72 65 61 6b 20 73 74 61 6c 65 20 6c 6f 63 6b  break stale lock
9470: 73 2e 20 20 54 68 65 20 53 51 4c 69 74 65 0a 2a  s.  The SQLite.*
9480: 2a 20 63 6f 72 65 20 72 65 73 65 72 76 65 73 20  * core reserves 
9490: 6f 70 63 6f 64 65 73 20 6c 65 73 73 20 74 68 61  opcodes less tha
94a0: 6e 20 31 30 30 20 66 6f 72 20 69 74 73 20 6f 77  n 100 for its ow
94b0: 6e 20 75 73 65 2e 20 0a 2a 2a 20 41 20 5b 53 51  n use. .** A [SQ
94c0: 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53  LITE_FCNTL_LOCKS
94d0: 54 41 54 45 20 7c 20 6c 69 73 74 20 6f 66 20 6f  TATE | list of o
94e0: 70 63 6f 64 65 73 5d 20 6c 65 73 73 20 74 68 61  pcodes] less tha
94f0: 6e 20 31 30 30 20 69 73 20 61 76 61 69 6c 61 62  n 100 is availab
9500: 6c 65 2e 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69  le..** Applicati
9510: 6f 6e 73 20 74 68 61 74 20 64 65 66 69 6e 65 20  ons that define 
9520: 61 20 63 75 73 74 6f 6d 20 78 46 69 6c 65 43 6f  a custom xFileCo
9530: 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 73 68 6f  ntrol method sho
9540: 75 6c 64 20 75 73 65 20 6f 70 63 6f 64 65 73 20  uld use opcodes 
9550: 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e  .** greater than
9560: 20 31 30 30 20 74 6f 20 61 76 6f 69 64 20 63 6f   100 to avoid co
9570: 6e 66 6c 69 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 54  nflicts..**.** T
9580: 68 65 20 78 53 65 63 74 6f 72 53 69 7a 65 28 29  he xSectorSize()
9590: 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20   method returns 
95a0: 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20  the sector size 
95b0: 6f 66 20 74 68 65 0a 2a 2a 20 64 65 76 69 63 65  of the.** device
95c0: 20 74 68 61 74 20 75 6e 64 65 72 6c 69 65 73 20   that underlies 
95d0: 74 68 65 20 66 69 6c 65 2e 20 20 54 68 65 20 73  the file.  The s
95e0: 65 63 74 6f 72 20 73 69 7a 65 20 69 73 20 74 68  ector size is th
95f0: 65 0a 2a 2a 20 6d 69 6e 69 6d 75 6d 20 77 72 69  e.** minimum wri
9600: 74 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 70  te that can be p
9610: 65 72 66 6f 72 6d 65 64 20 77 69 74 68 6f 75 74  erformed without
9620: 20 64 69 73 74 75 72 62 69 6e 67 0a 2a 2a 20 6f   disturbing.** o
9630: 74 68 65 72 20 62 79 74 65 73 20 69 6e 20 74 68  ther bytes in th
9640: 65 20 66 69 6c 65 2e 20 20 54 68 65 20 78 44 65  e file.  The xDe
9650: 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
9660: 69 63 73 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20  ics().** method 
9670: 72 65 74 75 72 6e 73 20 61 20 62 69 74 20 76 65  returns a bit ve
9680: 63 74 6f 72 20 64 65 73 63 72 69 62 69 6e 67 20  ctor describing 
9690: 62 65 68 61 76 69 6f 72 73 20 6f 66 20 74 68 65  behaviors of the
96a0: 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 64  .** underlying d
96b0: 65 76 69 63 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c  evice:.**.** <ul
96c0: 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54  >.** <li> [SQLIT
96d0: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 5d 0a  E_IOCAP_ATOMIC].
96e0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
96f0: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 5d  IOCAP_ATOMIC512]
9700: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
9710: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31 4b 5d  _IOCAP_ATOMIC1K]
9720: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
9730: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 32 4b 5d  _IOCAP_ATOMIC2K]
9740: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
9750: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 34 4b 5d  _IOCAP_ATOMIC4K]
9760: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
9770: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 38 4b 5d  _IOCAP_ATOMIC8K]
9780: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
9790: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31 36 4b  _IOCAP_ATOMIC16K
97a0: 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54  ].** <li> [SQLIT
97b0: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 33 32  E_IOCAP_ATOMIC32
97c0: 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49  K].** <li> [SQLI
97d0: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36  TE_IOCAP_ATOMIC6
97e0: 34 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c  4K].** <li> [SQL
97f0: 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41  ITE_IOCAP_SAFE_A
9800: 50 50 45 4e 44 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b  PPEND].** <li> [
9810: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51  SQLITE_IOCAP_SEQ
9820: 55 45 4e 54 49 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c  UENTIAL].** </ul
9830: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  >.**.** The SQLI
9840: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 20  TE_IOCAP_ATOMIC 
9850: 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74  property means t
9860: 68 61 74 20 61 6c 6c 20 77 72 69 74 65 73 20 6f  hat all writes o
9870: 66 0a 2a 2a 20 61 6e 79 20 73 69 7a 65 20 61 72  f.** any size ar
9880: 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53  e atomic.  The S
9890: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
98a0: 49 43 6e 6e 6e 20 76 61 6c 75 65 73 0a 2a 2a 20  ICnnn values.** 
98b0: 6d 65 61 6e 20 74 68 61 74 20 77 72 69 74 65 73  mean that writes
98c0: 20 6f 66 20 62 6c 6f 63 6b 73 20 74 68 61 74 20   of blocks that 
98d0: 61 72 65 20 6e 6e 6e 20 62 79 74 65 73 20 69 6e  are nnn bytes in
98e0: 20 73 69 7a 65 20 61 6e 64 0a 2a 2a 20 61 72 65   size and.** are
98f0: 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 61   aligned to an a
9900: 64 64 72 65 73 73 20 77 68 69 63 68 20 69 73 20  ddress which is 
9910: 61 6e 20 69 6e 74 65 67 65 72 20 6d 75 6c 74 69  an integer multi
9920: 70 6c 65 20 6f 66 0a 2a 2a 20 6e 6e 6e 20 61 72  ple of.** nnn ar
9930: 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53  e atomic.  The S
9940: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45  QLITE_IOCAP_SAFE
9950: 5f 41 50 50 45 4e 44 20 76 61 6c 75 65 20 6d 65  _APPEND value me
9960: 61 6e 73 0a 2a 2a 20 74 68 61 74 20 77 68 65 6e  ans.** that when
9970: 20 64 61 74 61 20 69 73 20 61 70 70 65 6e 64 65   data is appende
9980: 64 20 74 6f 20 61 20 66 69 6c 65 2c 20 74 68 65  d to a file, the
9990: 20 64 61 74 61 20 69 73 20 61 70 70 65 6e 64 65   data is appende
99a0: 64 0a 2a 2a 20 66 69 72 73 74 20 74 68 65 6e 20  d.** first then 
99b0: 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
99c0: 66 69 6c 65 20 69 73 20 65 78 74 65 6e 64 65 64  file is extended
99d0: 2c 20 6e 65 76 65 72 20 74 68 65 20 6f 74 68 65  , never the othe
99e0: 72 0a 2a 2a 20 77 61 79 20 61 72 6f 75 6e 64 2e  r.** way around.
99f0: 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43    The SQLITE_IOC
9a00: 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 70 72  AP_SEQUENTIAL pr
9a10: 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74 68 61  operty means tha
9a20: 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  t.** information
9a30: 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 64   is written to d
9a40: 69 73 6b 20 69 6e 20 74 68 65 20 73 61 6d 65 20  isk in the same 
9a50: 6f 72 64 65 72 20 61 73 20 63 61 6c 6c 73 0a 2a  order as calls.*
9a60: 2a 20 74 6f 20 78 57 72 69 74 65 28 29 2e 0a 2a  * to xWrite()..*
9a70: 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
9a80: 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
9a90: 6f 64 73 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  ods sqlite3_io_m
9aa0: 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73  ethods;.struct s
9ab0: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
9ac0: 73 20 7b 0a 20 20 69 6e 74 20 69 56 65 72 73 69  s {.  int iVersi
9ad0: 6f 6e 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f  on;.  int (*xClo
9ae0: 73 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  se)(sqlite3_file
9af0: 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 65 61  *);.  int (*xRea
9b00: 64 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  d)(sqlite3_file*
9b10: 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 69 41 6d  , void*, int iAm
9b20: 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  t, sqlite3_int64
9b30: 20 69 4f 66 73 74 29 3b 0a 20 20 69 6e 74 20 28   iOfst);.  int (
9b40: 2a 78 57 72 69 74 65 29 28 73 71 6c 69 74 65 33  *xWrite)(sqlite3
9b50: 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73 74 20 76 6f  _file*, const vo
9b60: 69 64 2a 2c 20 69 6e 74 20 69 41 6d 74 2c 20 73  id*, int iAmt, s
9b70: 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66  qlite3_int64 iOf
9b80: 73 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 54 72  st);.  int (*xTr
9b90: 75 6e 63 61 74 65 29 28 73 71 6c 69 74 65 33 5f  uncate)(sqlite3_
9ba0: 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65 33 5f 69  file*, sqlite3_i
9bb0: 6e 74 36 34 20 73 69 7a 65 29 3b 0a 20 20 69 6e  nt64 size);.  in
9bc0: 74 20 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74  t (*xSync)(sqlit
9bd0: 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 66 6c  e3_file*, int fl
9be0: 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46  ags);.  int (*xF
9bf0: 69 6c 65 53 69 7a 65 29 28 73 71 6c 69 74 65 33  ileSize)(sqlite3
9c00: 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65 33 5f  _file*, sqlite3_
9c10: 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 3b 0a 20  int64 *pSize);. 
9c20: 20 69 6e 74 20 28 2a 78 4c 6f 63 6b 29 28 73 71   int (*xLock)(sq
9c30: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74  lite3_file*, int
9c40: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 55 6e 6c 6f  );.  int (*xUnlo
9c50: 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ck)(sqlite3_file
9c60: 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28  *, int);.  int (
9c70: 2a 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  *xCheckReservedL
9c80: 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 6c  ock)(sqlite3_fil
9c90: 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 69  e*);.  int (*xFi
9ca0: 6c 65 43 6f 6e 74 72 6f 6c 29 28 73 71 6c 69 74  leControl)(sqlit
9cb0: 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 6f 70  e3_file*, int op
9cc0: 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 3b 0a 20  , void *pArg);. 
9cd0: 20 69 6e 74 20 28 2a 78 53 65 63 74 6f 72 53 69   int (*xSectorSi
9ce0: 7a 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ze)(sqlite3_file
9cf0: 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44 65 76  *);.  int (*xDev
9d00: 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
9d10: 63 73 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  cs)(sqlite3_file
9d20: 2a 29 3b 0a 20 20 2f 2a 20 41 64 64 69 74 69 6f  *);.  /* Additio
9d30: 6e 61 6c 20 6d 65 74 68 6f 64 73 20 6d 61 79 20  nal methods may 
9d40: 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75  be added in futu
9d50: 72 65 20 72 65 6c 65 61 73 65 73 20 2a 2f 0a 7d  re releases */.}
9d60: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
9d70: 46 3a 20 53 74 61 6e 64 61 72 64 20 46 69 6c 65  F: Standard File
9d80: 20 43 6f 6e 74 72 6f 6c 20 4f 70 63 6f 64 65 73   Control Opcodes
9d90: 20 7b 46 31 31 33 31 30 7d 0a 2a 2a 0a 2a 2a 20   {F11310}.**.** 
9da0: 54 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f  These integer co
9db0: 6e 73 74 61 6e 74 73 20 61 72 65 20 6f 70 63 6f  nstants are opco
9dc0: 64 65 73 20 66 6f 72 20 74 68 65 20 78 46 69 6c  des for the xFil
9dd0: 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 0a  eControl method.
9de0: 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74  ** of the [sqlit
9df0: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f  e3_io_methods] o
9e00: 62 6a 65 63 74 20 61 6e 64 20 74 6f 20 74 68 65  bject and to the
9e10: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63   [sqlite3_file_c
9e20: 6f 6e 74 72 6f 6c 28 29 5d 0a 2a 2a 20 69 6e 74  ontrol()].** int
9e30: 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  erface..**.** Th
9e40: 65 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f  e [SQLITE_FCNTL_
9e50: 4c 4f 43 4b 53 54 41 54 45 5d 20 6f 70 63 6f 64  LOCKSTATE] opcod
9e60: 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 64 65  e is used for de
9e70: 62 75 67 67 69 6e 67 2e 20 20 54 68 69 73 0a 2a  bugging.  This.*
9e80: 2a 20 6f 70 63 6f 64 65 20 63 61 75 73 65 73 20  * opcode causes 
9e90: 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c  the xFileControl
9ea0: 20 6d 65 74 68 6f 64 20 74 6f 20 77 72 69 74 65   method to write
9eb0: 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61   the current sta
9ec0: 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6c 6f 63  te of.** the loc
9ed0: 6b 20 28 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54  k (one of [SQLIT
9ee0: 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c 20 5b 53  E_LOCK_NONE], [S
9ef0: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45  QLITE_LOCK_SHARE
9f00: 44 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4c  D],.** [SQLITE_L
9f10: 4f 43 4b 5f 52 45 53 45 52 56 45 44 5d 2c 20 5b  OCK_RESERVED], [
9f20: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50 45 4e 44  SQLITE_LOCK_PEND
9f30: 49 4e 47 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45  ING], or [SQLITE
9f40: 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 5d  _LOCK_EXCLUSIVE]
9f50: 29 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20 69 6e 74  ).** into an int
9f60: 65 67 65 72 20 74 68 61 74 20 74 68 65 20 70 41  eger that the pA
9f70: 72 67 20 61 72 67 75 6d 65 6e 74 20 70 6f 69 6e  rg argument poin
9f80: 74 73 20 74 6f 2e 20 54 68 69 73 20 63 61 70 61  ts to. This capa
9f90: 62 69 6c 69 74 79 0a 2a 2a 20 69 73 20 75 73 65  bility.** is use
9fa0: 64 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67  d during testing
9fb0: 20 61 6e 64 20 6f 6e 6c 79 20 6e 65 65 64 73 20   and only needs 
9fc0: 74 6f 20 62 65 20 73 75 70 70 6f 72 74 65 64 20  to be supported 
9fd0: 77 68 65 6e 20 53 51 4c 49 54 45 5f 54 45 53 54  when SQLITE_TEST
9fe0: 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 2e 0a  .** is defined..
9ff0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
a000: 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54  E_FCNTL_LOCKSTAT
a010: 45 20 20 20 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a  E        1../*.*
a020: 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65  * CAPI3REF: Mute
a030: 78 20 48 61 6e 64 6c 65 20 7b 46 31 37 31 31 30  x Handle {F17110
a040: 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65  }.**.** The mute
a050: 78 20 6d 6f 64 75 6c 65 20 77 69 74 68 69 6e 20  x module within 
a060: 53 51 4c 69 74 65 20 64 65 66 69 6e 65 73 20 5b  SQLite defines [
a070: 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 20 74  sqlite3_mutex] t
a080: 6f 20 62 65 20 61 6e 0a 2a 2a 20 61 62 73 74 72  o be an.** abstr
a090: 61 63 74 20 74 79 70 65 20 66 6f 72 20 61 20 6d  act type for a m
a0a0: 75 74 65 78 20 6f 62 6a 65 63 74 2e 20 20 54 68  utex object.  Th
a0b0: 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6e 65  e SQLite core ne
a0c0: 76 65 72 20 6c 6f 6f 6b 73 0a 2a 2a 20 61 74 20  ver looks.** at 
a0d0: 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70  the internal rep
a0e0: 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61  resentation of a
a0f0: 6e 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  n [sqlite3_mutex
a100: 5d 2e 20 20 49 74 20 6f 6e 6c 79 0a 2a 2a 20 64  ].  It only.** d
a110: 65 61 6c 73 20 77 69 74 68 20 70 6f 69 6e 74 65  eals with pointe
a120: 72 73 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74  rs to the [sqlit
a130: 65 33 5f 6d 75 74 65 78 5d 20 6f 62 6a 65 63 74  e3_mutex] object
a140: 2e 0a 2a 2a 0a 2a 2a 20 4d 75 74 65 78 65 73 20  ..**.** Mutexes 
a150: 61 72 65 20 63 72 65 61 74 65 64 20 75 73 69 6e  are created usin
a160: 67 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  g [sqlite3_mutex
a170: 5f 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2f 0a 74 79  _alloc()]..*/.ty
a180: 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
a190: 69 74 65 33 5f 6d 75 74 65 78 20 73 71 6c 69 74  ite3_mutex sqlit
a1a0: 65 33 5f 6d 75 74 65 78 3b 0a 0a 2f 2a 0a 2a 2a  e3_mutex;../*.**
a1b0: 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20 49 6e   CAPI3REF: OS In
a1c0: 74 65 72 66 61 63 65 20 4f 62 6a 65 63 74 20 7b  terface Object {
a1d0: 46 31 31 31 34 30 7d 0a 2a 2a 0a 2a 2a 20 41 6e  F11140}.**.** An
a1e0: 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
a1f0: 73 20 6f 62 6a 65 63 74 20 64 65 66 69 6e 65 73  s object defines
a200: 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 62   the interface b
a210: 65 74 77 65 65 6e 20 74 68 65 0a 2a 2a 20 53 51  etween the.** SQ
a220: 4c 69 74 65 20 63 6f 72 65 20 61 6e 64 20 74 68  Lite core and th
a230: 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65  e underlying ope
a240: 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20  rating system.  
a250: 54 68 65 20 22 76 66 73 22 0a 2a 2a 20 69 6e 20  The "vfs".** in 
a260: 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
a270: 6f 62 6a 65 63 74 20 73 74 61 6e 64 73 20 66 6f  object stands fo
a280: 72 20 22 76 69 72 74 75 61 6c 20 66 69 6c 65 20  r "virtual file 
a290: 73 79 73 74 65 6d 22 2e 0a 2a 2a 0a 2a 2a 20 54  system"..**.** T
a2a0: 68 65 20 69 56 65 72 73 69 6f 6e 20 66 69 65 6c  he iVersion fiel
a2b0: 64 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 31  d is initially 1
a2c0: 20 62 75 74 20 6d 61 79 20 62 65 20 6c 61 72 67   but may be larg
a2d0: 65 72 20 66 6f 72 20 66 75 74 75 72 65 0a 2a 2a  er for future.**
a2e0: 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c   versions of SQL
a2f0: 69 74 65 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c  ite.  Additional
a300: 20 66 69 65 6c 64 73 20 6d 61 79 20 62 65 20 61   fields may be a
a310: 70 70 65 6e 64 65 64 20 74 6f 20 74 68 69 73 0a  ppended to this.
a320: 2a 2a 20 6f 62 6a 65 63 74 20 77 68 65 6e 20 74  ** object when t
a330: 68 65 20 69 56 65 72 73 69 6f 6e 20 76 61 6c 75  he iVersion valu
a340: 65 20 69 73 20 69 6e 63 72 65 61 73 65 64 2e 0a  e is increased..
a350: 2a 2a 0a 2a 2a 20 54 68 65 20 73 7a 4f 73 46 69  **.** The szOsFi
a360: 6c 65 20 66 69 65 6c 64 20 69 73 20 74 68 65 20  le field is the 
a370: 73 69 7a 65 20 6f 66 20 74 68 65 20 73 75 62 63  size of the subc
a380: 6c 61 73 73 65 64 20 5b 73 71 6c 69 74 65 33 5f  lassed [sqlite3_
a390: 66 69 6c 65 5d 0a 2a 2a 20 73 74 72 75 63 74 75  file].** structu
a3a0: 72 65 20 75 73 65 64 20 62 79 20 74 68 69 73 20  re used by this 
a3b0: 56 46 53 2e 20 20 6d 78 50 61 74 68 6e 61 6d 65  VFS.  mxPathname
a3c0: 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20   is the maximum 
a3d0: 6c 65 6e 67 74 68 20 6f 66 0a 2a 2a 20 61 20 70  length of.** a p
a3e0: 61 74 68 6e 61 6d 65 20 69 6e 20 74 68 69 73 20  athname in this 
a3f0: 56 46 53 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73  VFS..**.** Regis
a400: 74 65 72 65 64 20 73 71 6c 69 74 65 33 5f 76 66  tered sqlite3_vf
a410: 73 20 6f 62 6a 65 63 74 73 20 61 72 65 20 6b 65  s objects are ke
a420: 70 74 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c  pt on a linked l
a430: 69 73 74 20 66 6f 72 6d 65 64 20 62 79 0a 2a 2a  ist formed by.**
a440: 20 74 68 65 20 70 4e 65 78 74 20 70 6f 69 6e 74   the pNext point
a450: 65 72 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65  er.  The [sqlite
a460: 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 29  3_vfs_register()
a470: 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 74 65  ].** and [sqlite
a480: 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72  3_vfs_unregister
a490: 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 6d  ()] interfaces m
a4a0: 61 6e 61 67 65 20 74 68 69 73 20 6c 69 73 74 0a  anage this list.
a4b0: 2a 2a 20 69 6e 20 61 20 74 68 72 65 61 64 2d 73  ** in a thread-s
a4c0: 61 66 65 20 77 61 79 2e 20 20 54 68 65 20 5b 73  afe way.  The [s
a4d0: 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28  qlite3_vfs_find(
a4e0: 29 5d 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20  )] interface.** 
a4f0: 73 65 61 72 63 68 65 73 20 74 68 65 20 6c 69 73  searches the lis
a500: 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4e 65  t..**.** The pNe
a510: 78 74 20 66 69 65 6c 64 20 69 73 20 74 68 65 20  xt field is the 
a520: 6f 6e 6c 79 20 66 69 65 6c 64 20 69 6e 20 74 68  only field in th
a530: 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20 0a 2a  e sqlite3_vfs .*
a540: 2a 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  * structure that
a550: 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 65 76 65   SQLite will eve
a560: 72 20 6d 6f 64 69 66 79 2e 20 20 53 51 4c 69 74  r modify.  SQLit
a570: 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 61 63 63 65  e will only acce
a580: 73 73 0a 2a 2a 20 6f 72 20 6d 6f 64 69 66 79 20  ss.** or modify 
a590: 74 68 69 73 20 66 69 65 6c 64 20 77 68 69 6c 65  this field while
a5a0: 20 68 6f 6c 64 69 6e 67 20 61 20 70 61 72 74 69   holding a parti
a5b0: 63 75 6c 61 72 20 73 74 61 74 69 63 20 6d 75 74  cular static mut
a5c0: 65 78 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69  ex..** The appli
a5d0: 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65  cation should ne
a5e0: 76 65 72 20 6d 6f 64 69 66 79 20 61 6e 79 74 68  ver modify anyth
a5f0: 69 6e 67 20 77 69 74 68 69 6e 20 74 68 65 20 73  ing within the s
a600: 71 6c 69 74 65 33 5f 76 66 73 0a 2a 2a 20 6f 62  qlite3_vfs.** ob
a610: 6a 65 63 74 20 6f 6e 63 65 20 74 68 65 20 6f 62  ject once the ob
a620: 6a 65 63 74 20 68 61 73 20 62 65 65 6e 20 72 65  ject has been re
a630: 67 69 73 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20  gistered..**.** 
a640: 54 68 65 20 7a 4e 61 6d 65 20 66 69 65 6c 64 20  The zName field 
a650: 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 20 6f  holds the name o
a660: 66 20 74 68 65 20 56 46 53 20 6d 6f 64 75 6c 65  f the VFS module
a670: 2e 20 20 54 68 65 20 6e 61 6d 65 20 6d 75 73 74  .  The name must
a680: 0a 2a 2a 20 62 65 20 75 6e 69 71 75 65 20 61 63  .** be unique ac
a690: 72 6f 73 73 20 61 6c 6c 20 56 46 53 20 6d 6f 64  ross all VFS mod
a6a0: 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 31  ules..**.** {F11
a6b0: 31 34 31 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c  141} SQLite will
a6c0: 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20   guarantee that 
a6d0: 74 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20 73 74  the zFilename st
a6e0: 72 69 6e 67 20 70 61 73 73 65 64 20 74 6f 0a 2a  ring passed to.*
a6f0: 2a 20 78 4f 70 65 6e 28 29 20 69 73 20 61 20 66  * xOpen() is a f
a700: 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 61 73 20  ull pathname as 
a710: 67 65 6e 65 72 61 74 65 64 20 62 79 20 78 46 75  generated by xFu
a720: 6c 6c 50 61 74 68 6e 61 6d 65 28 29 20 61 6e 64  llPathname() and
a730: 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 73 74 72  .** that the str
a740: 69 6e 67 20 77 69 6c 6c 20 62 65 20 76 61 6c 69  ing will be vali
a750: 64 20 61 6e 64 20 75 6e 63 68 61 6e 67 65 64 20  d and unchanged 
a760: 75 6e 74 69 6c 20 78 43 6c 6f 73 65 28 29 20 69  until xClose() i
a770: 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e 20 20 7b 45  s.** called.  {E
a780: 4e 44 7d 20 53 6f 20 74 68 65 20 5b 73 71 6c 69  ND} So the [sqli
a790: 74 65 33 5f 66 69 6c 65 5d 20 63 61 6e 20 73 74  te3_file] can st
a7a0: 6f 72 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ore a pointer to
a7b0: 20 74 68 65 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65   the.** filename
a7c0: 20 69 66 20 69 74 20 6e 65 65 64 73 20 74 6f 20   if it needs to 
a7d0: 72 65 6d 65 6d 62 65 72 20 74 68 65 20 66 69 6c  remember the fil
a7e0: 65 6e 61 6d 65 20 66 6f 72 20 73 6f 6d 65 20 72  ename for some r
a7f0: 65 61 73 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31  eason..**.** {F1
a800: 31 31 34 32 7d 20 54 68 65 20 66 6c 61 67 73 20  1142} The flags 
a810: 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 4f 70 65  argument to xOpe
a820: 6e 28 29 20 69 6e 63 6c 75 64 65 73 20 61 6c 6c  n() includes all
a830: 20 62 69 74 73 20 73 65 74 20 69 6e 0a 2a 2a 20   bits set in.** 
a840: 74 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65  the flags argume
a850: 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6f  nt to [sqlite3_o
a860: 70 65 6e 5f 76 32 28 29 5d 2e 20 20 4f 72 20 69  pen_v2()].  Or i
a870: 66 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  f [sqlite3_open(
a880: 29 5d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65  )].** or [sqlite
a890: 33 5f 6f 70 65 6e 31 36 28 29 5d 20 69 73 20 75  3_open16()] is u
a8a0: 73 65 64 2c 20 74 68 65 6e 20 66 6c 61 67 73 20  sed, then flags 
a8b0: 69 6e 63 6c 75 64 65 73 20 61 74 20 6c 65 61 73  includes at leas
a8c0: 74 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45  t.** [SQLITE_OPE
a8d0: 4e 5f 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b  N_READWRITE] | [
a8e0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
a8f0: 54 45 5d 2e 20 7b 45 4e 44 7d 0a 2a 2a 20 49 66  TE]. {END}.** If
a900: 20 78 4f 70 65 6e 28 29 20 6f 70 65 6e 73 20 61   xOpen() opens a
a910: 20 66 69 6c 65 20 72 65 61 64 2d 6f 6e 6c 79 20   file read-only 
a920: 74 68 65 6e 20 69 74 20 73 65 74 73 20 2a 70 4f  then it sets *pO
a930: 75 74 46 6c 61 67 73 20 74 6f 0a 2a 2a 20 69 6e  utFlags to.** in
a940: 63 6c 75 64 65 20 5b 53 51 4c 49 54 45 5f 4f 50  clude [SQLITE_OP
a950: 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 2e 20 20 4f  EN_READONLY].  O
a960: 74 68 65 72 20 62 69 74 73 20 69 6e 20 2a 70 4f  ther bits in *pO
a970: 75 74 46 6c 61 67 73 20 6d 61 79 20 62 65 0a 2a  utFlags may be.*
a980: 2a 20 73 65 74 2e 0a 2a 2a 20 0a 2a 2a 20 7b 46  * set..** .** {F
a990: 31 31 31 34 33 7d 20 53 51 4c 69 74 65 20 77 69  11143} SQLite wi
a9a0: 6c 6c 20 61 6c 73 6f 20 61 64 64 20 6f 6e 65 20  ll also add one 
a9b0: 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
a9c0: 20 66 6c 61 67 73 20 74 6f 20 74 68 65 20 78 4f   flags to the xO
a9d0: 70 65 6e 28 29 0a 2a 2a 20 63 61 6c 6c 2c 20 64  pen().** call, d
a9e0: 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20  epending on the 
a9f0: 6f 62 6a 65 63 74 20 62 65 69 6e 67 20 6f 70 65  object being ope
aa00: 6e 65 64 3a 0a 2a 2a 20 0a 2a 2a 20 3c 75 6c 3e  ned:.** .** <ul>
aa10: 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54  .** <li>  [SQLIT
aa20: 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 5d 0a  E_OPEN_MAIN_DB].
aa30: 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45  ** <li>  [SQLITE
aa40: 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e  _OPEN_MAIN_JOURN
aa50: 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51  AL].** <li>  [SQ
aa60: 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44  LITE_OPEN_TEMP_D
aa70: 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c  B].** <li>  [SQL
aa80: 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f  ITE_OPEN_TEMP_JO
aa90: 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20  URNAL].** <li>  
aaa0: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41  [SQLITE_OPEN_TRA
aab0: 4e 53 49 45 4e 54 5f 44 42 5d 0a 2a 2a 20 3c 6c  NSIENT_DB].** <l
aac0: 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  i>  [SQLITE_OPEN
aad0: 5f 53 55 42 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20  _SUBJOURNAL].** 
aae0: 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50  <li>  [SQLITE_OP
aaf0: 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41  EN_MASTER_JOURNA
ab00: 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e 20 7b 45 4e 44  L].** </ul> {END
ab10: 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65  }.**.** The file
ab20: 20 49 2f 4f 20 69 6d 70 6c 65 6d 65 6e 74 61 74   I/O implementat
ab30: 69 6f 6e 20 63 61 6e 20 75 73 65 20 74 68 65 20  ion can use the 
ab40: 6f 62 6a 65 63 74 20 74 79 70 65 20 66 6c 61 67  object type flag
ab50: 73 20 74 6f 0a 2a 2a 20 63 68 61 6e 67 65 73 20  s to.** changes 
ab60: 74 68 65 20 77 61 79 20 69 74 20 64 65 61 6c 73  the way it deals
ab70: 20 77 69 74 68 20 66 69 6c 65 73 2e 20 20 46 6f   with files.  Fo
ab80: 72 20 65 78 61 6d 70 6c 65 2c 20 61 6e 20 61 70  r example, an ap
ab90: 70 6c 69 63 61 74 69 6f 6e 0a 2a 2a 20 74 68 61  plication.** tha
aba0: 74 20 64 6f 65 73 20 6e 6f 74 20 63 61 72 65 20  t does not care 
abb0: 61 62 6f 75 74 20 63 72 61 73 68 20 72 65 63 6f  about crash reco
abc0: 76 65 72 79 20 6f 72 20 72 6f 6c 6c 62 61 63 6b  very or rollback
abd0: 20 6d 69 67 68 74 20 6d 61 6b 65 0a 2a 2a 20 74   might make.** t
abe0: 68 65 20 6f 70 65 6e 20 6f 66 20 61 20 6a 6f 75  he open of a jou
abf0: 72 6e 61 6c 20 66 69 6c 65 20 61 20 6e 6f 2d 6f  rnal file a no-o
ac00: 70 2e 20 20 57 72 69 74 65 73 20 74 6f 20 74 68  p.  Writes to th
ac10: 69 73 20 6a 6f 75 72 6e 61 6c 20 77 6f 75 6c 64  is journal would
ac20: 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 6e 6f 2d 6f  .** also be no-o
ac30: 70 73 2c 20 61 6e 64 20 61 6e 79 20 61 74 74 65  ps, and any atte
ac40: 6d 70 74 20 74 6f 20 72 65 61 64 20 74 68 65 20  mpt to read the 
ac50: 6a 6f 75 72 6e 61 6c 20 77 6f 75 6c 64 20 72 65  journal would re
ac60: 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f  turn .** SQLITE_
ac70: 49 4f 45 52 52 2e 20 20 4f 72 20 74 68 65 20 69  IOERR.  Or the i
ac80: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 69  mplementation mi
ac90: 67 68 74 20 72 65 63 6f 67 6e 69 7a 65 20 74 68  ght recognize th
aca0: 61 74 20 61 20 64 61 74 61 62 61 73 65 20 0a 2a  at a database .*
acb0: 2a 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 64  * file will be d
acc0: 6f 69 6e 67 20 70 61 67 65 2d 61 6c 69 67 6e 65  oing page-aligne
acd0: 64 20 73 65 63 74 6f 72 20 72 65 61 64 73 20 61  d sector reads a
ace0: 6e 64 20 77 72 69 74 65 73 20 69 6e 20 61 20 72  nd writes in a r
acf0: 61 6e 64 6f 6d 20 0a 2a 2a 20 6f 72 64 65 72 20  andom .** order 
ad00: 61 6e 64 20 73 65 74 20 75 70 20 69 74 73 20 49  and set up its I
ad10: 2f 4f 20 73 75 62 73 79 73 74 65 6d 20 61 63 63  /O subsystem acc
ad20: 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a 20 0a 2a 2a  ordingly..** .**
ad30: 20 53 51 4c 69 74 65 20 6d 69 67 68 74 20 61 6c   SQLite might al
ad40: 73 6f 20 61 64 64 20 6f 6e 65 20 6f 66 20 74 68  so add one of th
ad50: 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67  e following flag
ad60: 73 20 74 6f 20 74 68 65 20 78 4f 70 65 6e 0a 2a  s to the xOpen.*
ad70: 2a 20 6d 65 74 68 6f 64 3a 0a 2a 2a 20 0a 2a 2a  * method:.** .**
ad80: 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53   <ul>.** <li> [S
ad90: 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54  QLITE_OPEN_DELET
ada0: 45 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 3c 6c 69  EONCLOSE].** <li
adb0: 3e 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45  > [SQLITE_OPEN_E
adc0: 58 43 4c 55 53 49 56 45 5d 0a 2a 2a 20 3c 2f 75  XCLUSIVE].** </u
add0: 6c 3e 0a 2a 2a 20 0a 2a 2a 20 7b 46 31 31 31 34  l>.** .** {F1114
ade0: 35 7d 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f  5} The [SQLITE_O
adf0: 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53  PEN_DELETEONCLOS
ae00: 45 5d 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68  E] flag means th
ae10: 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65  e file should be
ae20: 0a 2a 2a 20 64 65 6c 65 74 65 64 20 77 68 65 6e  .** deleted when
ae30: 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 20 20   it is closed.  
ae40: 7b 46 31 31 31 34 36 7d 20 54 68 65 20 5b 53 51  {F11146} The [SQ
ae50: 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45  LITE_OPEN_DELETE
ae60: 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 77 69 6c 6c  ONCLOSE].** will
ae70: 20 62 65 20 73 65 74 20 66 6f 72 20 54 45 4d 50   be set for TEMP
ae80: 20 20 64 61 74 61 62 61 73 65 73 2c 20 6a 6f 75    databases, jou
ae90: 72 6e 61 6c 73 20 61 6e 64 20 66 6f 72 20 73 75  rnals and for su
aea0: 62 6a 6f 75 72 6e 61 6c 73 2e 20 0a 2a 2a 20 7b  bjournals. .** {
aeb0: 46 31 31 31 34 37 7d 20 54 68 65 20 5b 53 51 4c  F11147} The [SQL
aec0: 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49  ITE_OPEN_EXCLUSI
aed0: 56 45 5d 20 66 6c 61 67 20 6d 65 61 6e 73 20 74  VE] flag means t
aee0: 68 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62  he file should b
aef0: 65 20 6f 70 65 6e 65 64 0a 2a 2a 20 66 6f 72 20  e opened.** for 
af00: 65 78 63 6c 75 73 69 76 65 20 61 63 63 65 73 73  exclusive access
af10: 2e 20 20 54 68 69 73 20 66 6c 61 67 20 69 73 20  .  This flag is 
af20: 73 65 74 20 66 6f 72 20 61 6c 6c 20 66 69 6c 65  set for all file
af30: 73 20 65 78 63 65 70 74 0a 2a 2a 20 66 6f 72 20  s except.** for 
af40: 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
af50: 65 20 66 69 6c 65 2e 20 7b 45 4e 44 7d 0a 2a 2a  e file. {END}.**
af60: 20 0a 2a 2a 20 7b 46 31 31 31 34 38 7d 20 41 74   .** {F11148} At
af70: 20 6c 65 61 73 74 20 73 7a 4f 73 46 69 6c 65 20   least szOsFile 
af80: 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20  bytes of memory 
af90: 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79  are allocated by
afa0: 20 53 51 4c 69 74 65 20 0a 2a 2a 20 74 6f 20 68   SQLite .** to h
afb0: 6f 6c 64 20 74 68 65 20 20 5b 73 71 6c 69 74 65  old the  [sqlite
afc0: 33 5f 66 69 6c 65 5d 20 73 74 72 75 63 74 75 72  3_file] structur
afd0: 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
afe0: 74 68 69 72 64 20 0a 2a 2a 20 61 72 67 75 6d 65  third .** argume
aff0: 6e 74 20 74 6f 20 78 4f 70 65 6e 2e 20 20 7b 45  nt to xOpen.  {E
b000: 4e 44 7d 20 20 54 68 65 20 78 4f 70 65 6e 20 6d  ND}  The xOpen m
b010: 65 74 68 6f 64 20 64 6f 65 73 20 6e 6f 74 20 68  ethod does not h
b020: 61 76 65 20 74 6f 0a 2a 2a 20 61 6c 6c 6f 63 61  ave to.** alloca
b030: 74 65 20 74 68 65 20 73 74 72 75 63 74 75 72 65  te the structure
b040: 3b 20 69 74 20 73 68 6f 75 6c 64 20 6a 75 73 74  ; it should just
b050: 20 66 69 6c 6c 20 69 74 20 69 6e 2e 0a 2a 2a 20   fill it in..** 
b060: 0a 2a 2a 20 7b 46 31 31 31 34 39 7d 20 54 68 65  .** {F11149} The
b070: 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20   flags argument 
b080: 74 6f 20 78 41 63 63 65 73 73 28 29 20 6d 61 79  to xAccess() may
b090: 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 43 43 45   be [SQLITE_ACCE
b0a0: 53 53 5f 45 58 49 53 54 53 5d 20 0a 2a 2a 20 74  SS_EXISTS] .** t
b0b0: 6f 20 74 65 73 74 20 66 6f 72 20 74 68 65 20 65  o test for the e
b0c0: 78 69 73 74 61 6e 63 65 20 6f 66 20 61 20 66 69  xistance of a fi
b0d0: 6c 65 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54  le,.** or [SQLIT
b0e0: 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49  E_ACCESS_READWRI
b0f0: 54 45 5d 20 74 6f 20 74 65 73 74 20 74 6f 20 73  TE] to test to s
b100: 65 65 0a 2a 2a 20 69 66 20 61 20 66 69 6c 65 20  ee.** if a file 
b110: 69 73 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20  is readable and 
b120: 77 72 69 74 61 62 6c 65 2c 20 6f 72 20 5b 53 51  writable, or [SQ
b130: 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44  LITE_ACCESS_READ
b140: 5d 0a 2a 2a 20 74 6f 20 74 65 73 74 20 74 6f 20  ].** to test to 
b150: 73 65 65 20 69 66 20 61 20 66 69 6c 65 20 69 73  see if a file is
b160: 20 61 74 20 6c 65 61 73 74 20 72 65 61 64 61 62   at least readab
b170: 6c 65 2e 20 20 7b 45 4e 44 7d 20 54 68 65 20 66  le.  {END} The f
b180: 69 6c 65 20 63 61 6e 20 62 65 20 61 20 0a 2a 2a  ile can be a .**
b190: 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 20 0a   directory..** .
b1a0: 2a 2a 20 7b 46 31 31 31 35 30 7d 20 53 51 4c 69  ** {F11150} SQLi
b1b0: 74 65 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 61  te will always a
b1c0: 6c 6c 6f 63 61 74 65 20 61 74 20 6c 65 61 73 74  llocate at least
b1d0: 20 6d 78 50 61 74 68 6e 61 6d 65 2b 31 20 62 79   mxPathname+1 by
b1e0: 74 65 73 20 66 6f 72 0a 2a 2a 20 74 68 65 20 6f  tes for.** the o
b1f0: 75 74 70 75 74 20 62 75 66 66 65 72 73 20 66 6f  utput buffers fo
b200: 72 20 78 47 65 74 54 65 6d 70 6e 61 6d 65 20 61  r xGetTempname a
b210: 6e 64 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65  nd xFullPathname
b220: 2e 20 7b 46 31 31 31 35 31 7d 20 54 68 65 20 65  . {F11151} The e
b230: 78 61 63 74 0a 2a 2a 20 73 69 7a 65 20 6f 66 20  xact.** size of 
b240: 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65  the output buffe
b250: 72 20 69 73 20 61 6c 73 6f 20 70 61 73 73 65 64  r is also passed
b260: 20 61 73 20 61 20 70 61 72 61 6d 65 74 65 72 20   as a parameter 
b270: 74 6f 20 62 6f 74 68 20 0a 2a 2a 20 6d 65 74 68  to both .** meth
b280: 6f 64 73 2e 20 7b 45 4e 44 7d 20 49 66 20 74 68  ods. {END} If th
b290: 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20  e output buffer 
b2a0: 69 73 20 6e 6f 74 20 6c 61 72 67 65 20 65 6e 6f  is not large eno
b2b0: 75 67 68 2c 20 53 51 4c 49 54 45 5f 43 41 4e 54  ugh, SQLITE_CANT
b2c0: 4f 50 45 4e 0a 2a 2a 20 73 68 6f 75 6c 64 20 62  OPEN.** should b
b2d0: 65 20 72 65 74 75 72 6e 65 64 2e 20 41 73 20 74  e returned. As t
b2e0: 68 69 73 20 69 73 20 68 61 6e 64 6c 65 64 20 61  his is handled a
b2f0: 73 20 61 20 66 61 74 61 6c 20 65 72 72 6f 72 20  s a fatal error 
b300: 62 79 20 53 51 4c 69 74 65 2c 0a 2a 2a 20 76 66  by SQLite,.** vf
b310: 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  s implementation
b320: 73 20 73 68 6f 75 6c 64 20 65 6e 64 65 61 76 6f  s should endeavo
b330: 72 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68 69  r to prevent thi
b340: 73 20 62 79 20 73 65 74 74 69 6e 67 20 0a 2a 2a  s by setting .**
b350: 20 6d 78 50 61 74 68 6e 61 6d 65 20 74 6f 20 61   mxPathname to a
b360: 20 73 75 66 66 69 63 69 65 6e 74 6c 79 20 6c 61   sufficiently la
b370: 72 67 65 20 76 61 6c 75 65 2e 0a 2a 2a 20 0a 2a  rge value..** .*
b380: 2a 20 54 68 65 20 78 52 61 6e 64 6f 6d 6e 65 73  * The xRandomnes
b390: 73 28 29 2c 20 78 53 6c 65 65 70 28 29 2c 20 61  s(), xSleep(), a
b3a0: 6e 64 20 78 43 75 72 72 65 6e 74 54 69 6d 65 28  nd xCurrentTime(
b3b0: 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20  ) interfaces.** 
b3c0: 61 72 65 20 6e 6f 74 20 73 74 72 69 63 74 6c 79  are not strictly
b3d0: 20 61 20 70 61 72 74 20 6f 66 20 74 68 65 20 66   a part of the f
b3e0: 69 6c 65 73 79 73 74 65 6d 2c 20 62 75 74 20 74  ilesystem, but t
b3f0: 68 65 79 20 61 72 65 0a 2a 2a 20 69 6e 63 6c 75  hey are.** inclu
b400: 64 65 64 20 69 6e 20 74 68 65 20 56 46 53 20 73  ded in the VFS s
b410: 74 72 75 63 74 75 72 65 20 66 6f 72 20 63 6f 6d  tructure for com
b420: 70 6c 65 74 65 6e 65 73 73 2e 0a 2a 2a 20 54 68  pleteness..** Th
b430: 65 20 78 52 61 6e 64 6f 6d 6e 65 73 73 28 29 20  e xRandomness() 
b440: 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74  function attempt
b450: 73 20 74 6f 20 72 65 74 75 72 6e 20 6e 42 79 74  s to return nByt
b460: 65 73 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 67  es bytes.** of g
b470: 6f 6f 64 2d 71 75 61 6c 69 74 79 20 72 61 6e 64  ood-quality rand
b480: 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 4f 75 74  omness into zOut
b490: 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  .  The return va
b4a0: 6c 75 65 20 69 73 0a 2a 2a 20 74 68 65 20 61 63  lue is.** the ac
b4b0: 74 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62  tual number of b
b4c0: 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e 65  ytes of randomne
b4d0: 73 73 20 6f 62 74 61 69 6e 65 64 2e 20 20 54 68  ss obtained.  Th
b4e0: 65 0a 2a 2a 20 78 53 6c 65 65 70 28 29 20 6d 65  e.** xSleep() me
b4f0: 74 68 6f 64 20 63 61 75 73 65 73 20 74 68 65 20  thod causes the 
b500: 63 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 74  calling thread t
b510: 6f 20 73 6c 65 65 70 20 66 6f 72 20 61 74 0a 2a  o sleep for at.*
b520: 2a 20 6c 65 61 73 74 20 74 68 65 20 6e 75 6d 62  * least the numb
b530: 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e  er of microsecon
b540: 64 73 20 67 69 76 65 6e 2e 20 20 54 68 65 20 78  ds given.  The x
b550: 43 75 72 72 65 6e 74 54 69 6d 65 28 29 0a 2a 2a  CurrentTime().**
b560: 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20   method returns 
b570: 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 4e 75 6d  a Julian Day Num
b580: 62 65 72 20 66 6f 72 20 74 68 65 20 63 75 72 72  ber for the curr
b590: 65 6e 74 20 64 61 74 65 20 61 6e 64 0a 2a 2a 20  ent date and.** 
b5a0: 74 69 6d 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  time..*/.typedef
b5b0: 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
b5c0: 76 66 73 20 73 71 6c 69 74 65 33 5f 76 66 73 3b  vfs sqlite3_vfs;
b5d0: 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
b5e0: 76 66 73 20 7b 0a 20 20 69 6e 74 20 69 56 65 72  vfs {.  int iVer
b5f0: 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20  sion;           
b600: 20 2f 2a 20 53 74 72 75 63 74 75 72 65 20 76 65   /* Structure ve
b610: 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a  rsion number */.
b620: 20 20 69 6e 74 20 73 7a 4f 73 46 69 6c 65 3b 20    int szOsFile; 
b630: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
b640: 7a 65 20 6f 66 20 73 75 62 63 6c 61 73 73 65 64  ze of subclassed
b650: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2f   sqlite3_file */
b660: 0a 20 20 69 6e 74 20 6d 78 50 61 74 68 6e 61 6d  .  int mxPathnam
b670: 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  e;          /* M
b680: 61 78 69 6d 75 6d 20 66 69 6c 65 20 70 61 74 68  aximum file path
b690: 6e 61 6d 65 20 6c 65 6e 67 74 68 20 2a 2f 0a 20  name length */. 
b6a0: 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 4e   sqlite3_vfs *pN
b6b0: 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78  ext;      /* Nex
b6c0: 74 20 72 65 67 69 73 74 65 72 65 64 20 56 46 53  t registered VFS
b6d0: 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
b6e0: 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 2f   *zName;       /
b6f0: 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 76  * Name of this v
b700: 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 73 74  irtual file syst
b710: 65 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41  em */.  void *pA
b720: 70 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20  ppData;         
b730: 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61   /* Pointer to a
b740: 70 70 6c 69 63 61 74 69 6f 6e 2d 73 70 65 63 69  pplication-speci
b750: 66 69 63 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e  fic data */.  in
b760: 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c 69 74  t (*xOpen)(sqlit
b770: 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63  e3_vfs*, const c
b780: 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 73 71 6c 69  har *zName, sqli
b790: 74 65 33 5f 66 69 6c 65 2a 2c 0a 20 20 20 20 20  te3_file*,.     
b7a0: 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 66 6c            int fl
b7b0: 61 67 73 2c 20 69 6e 74 20 2a 70 4f 75 74 46 6c  ags, int *pOutFl
b7c0: 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44  ags);.  int (*xD
b7d0: 65 6c 65 74 65 29 28 73 71 6c 69 74 65 33 5f 76  elete)(sqlite3_v
b7e0: 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  fs*, const char 
b7f0: 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 73 79 6e 63  *zName, int sync
b800: 44 69 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 41  Dir);.  int (*xA
b810: 63 63 65 73 73 29 28 73 71 6c 69 74 65 33 5f 76  ccess)(sqlite3_v
b820: 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  fs*, const char 
b830: 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 66 6c 61 67  *zName, int flag
b840: 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 47 65 74  s);.  int (*xGet
b850: 54 65 6d 70 6e 61 6d 65 29 28 73 71 6c 69 74 65  Tempname)(sqlite
b860: 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6e 4f 75 74  3_vfs*, int nOut
b870: 2c 20 63 68 61 72 20 2a 7a 4f 75 74 29 3b 0a 20  , char *zOut);. 
b880: 20 69 6e 74 20 28 2a 78 46 75 6c 6c 50 61 74 68   int (*xFullPath
b890: 6e 61 6d 65 29 28 73 71 6c 69 74 65 33 5f 76 66  name)(sqlite3_vf
b8a0: 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  s*, const char *
b8b0: 7a 4e 61 6d 65 2c 20 69 6e 74 20 6e 4f 75 74 2c  zName, int nOut,
b8c0: 20 63 68 61 72 20 2a 7a 4f 75 74 29 3b 0a 20 20   char *zOut);.  
b8d0: 76 6f 69 64 20 2a 28 2a 78 44 6c 4f 70 65 6e 29  void *(*xDlOpen)
b8e0: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63  (sqlite3_vfs*, c
b8f0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
b900: 6e 61 6d 65 29 3b 0a 20 20 76 6f 69 64 20 28 2a  name);.  void (*
b910: 78 44 6c 45 72 72 6f 72 29 28 73 71 6c 69 74 65  xDlError)(sqlite
b920: 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6e 42 79 74  3_vfs*, int nByt
b930: 65 2c 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67  e, char *zErrMsg
b940: 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 44 6c  );.  void *(*xDl
b950: 53 79 6d 29 28 73 71 6c 69 74 65 33 5f 76 66 73  Sym)(sqlite3_vfs
b960: 2a 2c 76 6f 69 64 2a 2c 20 63 6f 6e 73 74 20 63  *,void*, const c
b970: 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 3b 0a 20  har *zSymbol);. 
b980: 20 76 6f 69 64 20 28 2a 78 44 6c 43 6c 6f 73 65   void (*xDlClose
b990: 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20  )(sqlite3_vfs*, 
b9a0: 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  void*);.  int (*
b9b0: 78 52 61 6e 64 6f 6d 6e 65 73 73 29 28 73 71 6c  xRandomness)(sql
b9c0: 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6e  ite3_vfs*, int n
b9d0: 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 4f 75 74  Byte, char *zOut
b9e0: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 6c 65 65  );.  int (*xSlee
b9f0: 70 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c  p)(sqlite3_vfs*,
ba00: 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 6f 6e 64   int microsecond
ba10: 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 75 72  s);.  int (*xCur
ba20: 72 65 6e 74 54 69 6d 65 29 28 73 71 6c 69 74 65  rentTime)(sqlite
ba30: 33 5f 76 66 73 2a 2c 20 64 6f 75 62 6c 65 2a 29  3_vfs*, double*)
ba40: 3b 0a 20 20 2f 2a 20 4e 65 77 20 66 69 65 6c 64  ;.  /* New field
ba50: 73 20 6d 61 79 20 62 65 20 61 70 70 65 6e 64 65  s may be appende
ba60: 64 20 69 6e 20 66 69 67 75 72 65 20 76 65 72 73  d in figure vers
ba70: 69 6f 6e 73 2e 20 20 54 68 65 20 69 56 65 72 73  ions.  The iVers
ba80: 69 6f 6e 0a 20 20 2a 2a 20 76 61 6c 75 65 20 77  ion.  ** value w
ba90: 69 6c 6c 20 69 6e 63 72 65 6d 65 6e 74 20 77 68  ill increment wh
baa0: 65 6e 65 76 65 72 20 74 68 69 73 20 68 61 70 70  enever this happ
bab0: 65 6e 73 2e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  ens. */.};../*.*
bac0: 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6c 61 67  * CAPI3REF: Flag
bad0: 73 20 66 6f 72 20 74 68 65 20 78 41 63 63 65 73  s for the xAcces
bae0: 73 20 56 46 53 20 6d 65 74 68 6f 64 20 7b 46 31  s VFS method {F1
baf0: 31 31 39 30 7d 0a 2a 2a 0a 2a 2a 20 7b 46 31 31  1190}.**.** {F11
bb00: 31 39 31 7d 20 54 68 65 73 65 20 69 6e 74 65 67  191} These integ
bb10: 65 72 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 6e  er constants can
bb20: 20 62 65 20 75 73 65 64 20 61 73 20 74 68 65 20   be used as the 
bb30: 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20  third parameter 
bb40: 74 6f 0a 2a 2a 20 74 68 65 20 78 41 63 63 65 73  to.** the xAcces
bb50: 73 20 6d 65 74 68 6f 64 20 6f 66 20 61 6e 20 5b  s method of an [
bb60: 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a  sqlite3_vfs] obj
bb70: 65 63 74 2e 20 7b 45 4e 44 7d 20 20 54 68 65 79  ect. {END}  They
bb80: 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 68   determine.** wh
bb90: 61 74 20 6b 69 6e 64 20 6f 66 20 70 65 72 6d 69  at kind of permi
bba0: 73 73 69 6f 6e 73 20 74 68 65 20 78 41 63 63 65  ssions the xAcce
bbb0: 73 73 20 6d 65 74 68 6f 64 20 69 73 0a 2a 2a 20  ss method is.** 
bbc0: 6c 6f 6f 6b 69 6e 67 20 66 6f 72 2e 20 20 7b 46  looking for.  {F
bbd0: 31 31 31 39 32 7d 20 57 69 74 68 20 53 51 4c 49  11192} With SQLI
bbe0: 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53  TE_ACCESS_EXISTS
bbf0: 2c 20 74 68 65 20 78 41 63 63 65 73 73 20 6d 65  , the xAccess me
bc00: 74 68 6f 64 0a 2a 2a 20 73 69 6d 70 6c 79 20 63  thod.** simply c
bc10: 68 65 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20  hecks to see if 
bc20: 74 68 65 20 66 69 6c 65 20 65 78 69 73 74 73 2e  the file exists.
bc30: 20 7b 46 31 31 31 39 33 7d 20 57 69 74 68 0a 2a   {F11193} With.*
bc40: 2a 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  * SQLITE_ACCESS_
bc50: 52 45 41 44 57 52 49 54 45 2c 20 74 68 65 20 78  READWRITE, the x
bc60: 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20 63 68  Access method ch
bc70: 65 63 6b 73 20 74 6f 20 73 65 65 0a 2a 2a 20 69  ecks to see.** i
bc80: 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 62 6f  f the file is bo
bc90: 74 68 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20  th readable and 
bca0: 77 72 69 74 61 62 6c 65 2e 20 20 7b 46 31 31 31  writable.  {F111
bcb0: 39 34 7d 20 57 69 74 68 0a 2a 2a 20 53 51 4c 49  94} With.** SQLI
bcc0: 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 20 74  TE_ACCESS_READ t
bcd0: 68 65 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f  he xAccess metho
bce0: 64 0a 2a 2a 20 63 68 65 63 6b 73 20 74 6f 20 73  d.** checks to s
bcf0: 65 65 20 69 66 20 74 68 65 20 66 69 6c 65 20 69  ee if the file i
bd00: 73 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2f 0a 23  s readable..*/.#
bd10: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 43  define SQLITE_AC
bd20: 43 45 53 53 5f 45 58 49 53 54 53 20 20 20 20 30  CESS_EXISTS    0
bd30: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
bd40: 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 45  ACCESS_READWRITE
bd50: 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   1.#define SQLIT
bd60: 45 5f 41 43 43 45 53 53 5f 52 45 41 44 20 20 20  E_ACCESS_READ   
bd70: 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49     2../*.** CAPI
bd80: 33 52 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72 20  3REF: Enable Or 
bd90: 44 69 73 61 62 6c 65 20 45 78 74 65 6e 64 65 64  Disable Extended
bda0: 20 52 65 73 75 6c 74 20 43 6f 64 65 73 20 7b 46   Result Codes {F
bdb0: 31 32 32 30 30 7d 0a 2a 2a 0a 2a 2a 20 54 68 65  12200}.**.** The
bdc0: 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65   sqlite3_extende
bdd0: 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 29  d_result_codes()
bde0: 20 72 6f 75 74 69 6e 65 20 65 6e 61 62 6c 65 73   routine enables
bdf0: 20 6f 72 20 64 69 73 61 62 6c 65 73 20 74 68 65   or disables the
be00: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52  .** [SQLITE_IOER
be10: 52 5f 52 45 41 44 20 7c 20 65 78 74 65 6e 64 65  R_READ | extende
be20: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20  d result codes] 
be30: 66 65 61 74 75 72 65 20 6f 66 20 53 51 4c 69 74  feature of SQLit
be40: 65 2e 0a 2a 2a 20 54 68 65 20 65 78 74 65 6e 64  e..** The extend
be50: 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20  ed result codes 
be60: 61 72 65 20 64 69 73 61 62 6c 65 64 20 62 79 20  are disabled by 
be70: 64 65 66 61 75 6c 74 20 66 6f 72 20 68 69 73 74  default for hist
be80: 6f 72 69 63 61 6c 0a 2a 2a 20 63 6f 6d 70 61 74  orical.** compat
be90: 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 49  ibility..**.** I
bea0: 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a  NVARIANTS:.**.**
beb0: 20 7b 46 31 32 32 30 31 7d 20 45 61 63 68 20 6e   {F12201} Each n
bec0: 65 77 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  ew [database con
bed0: 6e 65 63 74 69 6f 6e 5d 20 68 61 73 20 74 68 65  nection] has the
bee0: 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 65   .**          [e
bef0: 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63  xtended result c
bf00: 6f 64 65 73 5d 20 66 65 61 74 75 72 65 0a 2a 2a  odes] feature.**
bf10: 20 20 20 20 20 20 20 20 20 20 64 69 73 61 62 6c            disabl
bf20: 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e 0a 2a  ed by default..*
bf30: 2a 0a 2a 2a 20 7b 46 31 32 32 30 32 7d 20 54 68  *.** {F12202} Th
bf40: 65 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 65 6e  e [sqlite3_exten
bf50: 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73  ded_result_codes
bf60: 28 44 2c 46 29 5d 20 69 6e 74 65 72 66 61 63 65  (D,F)] interface
bf70: 20 77 69 6c 6c 20 65 6e 61 62 6c 65 0a 2a 2a 20   will enable.** 
bf80: 20 20 20 20 20 20 20 20 20 5b 65 78 74 65 6e 64           [extend
bf90: 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d  ed result codes]
bfa0: 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 20 20 20   for the .**    
bfb0: 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 65 20        [database 
bfc0: 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 20 69 66  connection] D if
bfd0: 20 74 68 65 20 46 20 70 61 72 61 6d 65 74 65 72   the F parameter
bfe0: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73 20  .**          is 
bff0: 74 72 75 65 2c 20 6f 72 20 64 69 73 61 62 6c 65  true, or disable
c000: 20 74 68 65 6d 20 69 66 20 46 20 69 73 20 66 61   them if F is fa
c010: 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  lse..*/.SQLITE_A
c020: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65  PI int sqlite3_e
c030: 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63  xtended_result_c
c040: 6f 64 65 73 28 73 71 6c 69 74 65 33 2a 2c 20 69  odes(sqlite3*, i
c050: 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a 2a  nt onoff);../*.*
c060: 2a 20 43 41 50 49 33 52 45 46 3a 20 4c 61 73 74  * CAPI3REF: Last
c070: 20 49 6e 73 65 72 74 20 52 6f 77 69 64 20 7b 46   Insert Rowid {F
c080: 31 32 32 32 30 7d 0a 2a 2a 0a 2a 2a 20 45 61 63  12220}.**.** Eac
c090: 68 20 65 6e 74 72 79 20 69 6e 20 61 6e 20 53 51  h entry in an SQ
c0a0: 4c 69 74 65 20 74 61 62 6c 65 20 68 61 73 20 61  Lite table has a
c0b0: 20 75 6e 69 71 75 65 20 36 34 2d 62 69 74 20 73   unique 64-bit s
c0c0: 69 67 6e 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72  igned.** integer
c0d0: 20 6b 65 79 20 63 61 6c 6c 65 64 20 74 68 65 20   key called the 
c0e0: 22 72 6f 77 69 64 22 2e 20 54 68 65 20 72 6f 77  "rowid". The row
c0f0: 69 64 20 69 73 20 61 6c 77 61 79 73 20 61 76 61  id is always ava
c100: 69 6c 61 62 6c 65 0a 2a 2a 20 61 73 20 61 6e 20  ilable.** as an 
c110: 75 6e 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d  undeclared colum
c120: 6e 20 6e 61 6d 65 64 20 52 4f 57 49 44 2c 20 4f  n named ROWID, O
c130: 49 44 2c 20 6f 72 20 5f 52 4f 57 49 44 5f 20 61  ID, or _ROWID_ a
c140: 73 20 6c 6f 6e 67 20 61 73 20 74 68 6f 73 65 0a  s long as those.
c150: 2a 2a 20 6e 61 6d 65 73 20 61 72 65 20 6e 6f 74  ** names are not
c160: 20 61 6c 73 6f 20 75 73 65 64 20 62 79 20 65 78   also used by ex
c170: 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65  plicitly declare
c180: 64 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 0a 2a 2a  d columns. If.**
c190: 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61   the table has a
c1a0: 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 79 70 65 20   column of type 
c1b0: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
c1c0: 4b 45 59 20 74 68 65 6e 20 74 68 61 74 20 63 6f  KEY then that co
c1d0: 6c 75 6d 6e 0a 2a 2a 20 69 73 20 61 6e 6f 74 68  lumn.** is anoth
c1e0: 65 72 20 61 6c 69 61 73 20 66 6f 72 20 74 68 65  er alias for the
c1f0: 20 72 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68   rowid..**.** Th
c200: 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
c210: 6e 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20  ns the rowid of 
c220: 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 0a  the most recent.
c230: 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 49 4e  ** successful IN
c240: 53 45 52 54 20 69 6e 74 6f 20 74 68 65 20 64 61  SERT into the da
c250: 74 61 62 61 73 65 20 66 72 6f 6d 20 74 68 65 20  tabase from the 
c260: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
c270: 69 6f 6e 0a 2a 2a 20 73 68 6f 77 6e 20 69 6e 20  ion.** shown in 
c280: 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
c290: 6e 74 2e 20 20 49 66 20 6e 6f 20 73 75 63 63 65  nt.  If no succe
c2a0: 73 73 66 75 6c 20 69 6e 73 65 72 74 73 0a 2a 2a  ssful inserts.**
c2b0: 20 68 61 76 65 20 65 76 65 72 20 6f 63 63 75 72   have ever occur
c2c0: 72 65 64 20 6f 6e 20 74 68 69 73 20 64 61 74 61  red on this data
c2d0: 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c  base connection,
c2e0: 20 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65   zero is returne
c2f0: 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49  d..**.** If an I
c300: 4e 53 45 52 54 20 6f 63 63 75 72 73 20 77 69 74  NSERT occurs wit
c310: 68 69 6e 20 61 20 74 72 69 67 67 65 72 2c 20 74  hin a trigger, t
c320: 68 65 6e 20 74 68 65 20 72 6f 77 69 64 20 6f 66  hen the rowid of
c330: 20 74 68 65 0a 2a 2a 20 69 6e 73 65 72 74 65 64   the.** inserted
c340: 20 72 6f 77 20 69 73 20 72 65 74 75 72 6e 65 64   row is returned
c350: 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65   by this routine
c360: 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20   as long as the 
c370: 74 72 69 67 67 65 72 0a 2a 2a 20 69 73 20 72 75  trigger.** is ru
c380: 6e 6e 69 6e 67 2e 20 20 42 75 74 20 6f 6e 63 65  nning.  But once
c390: 20 74 68 65 20 74 72 69 67 67 65 72 20 74 65 72   the trigger ter
c3a0: 6d 69 6e 61 74 65 73 2c 20 74 68 65 20 76 61 6c  minates, the val
c3b0: 75 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 62  ue returned.** b
c3c0: 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  y this routine r
c3d0: 65 76 65 72 74 73 20 74 6f 20 74 68 65 20 6c 61  everts to the la
c3e0: 73 74 20 76 61 6c 75 65 20 69 6e 73 65 72 74 65  st value inserte
c3f0: 64 20 62 65 66 6f 72 65 20 74 68 65 0a 2a 2a 20  d before the.** 
c400: 74 72 69 67 67 65 72 20 66 69 72 65 64 2e 0a 2a  trigger fired..*
c410: 2a 0a 2a 2a 20 41 6e 20 49 4e 53 45 52 54 20 74  *.** An INSERT t
c420: 68 61 74 20 66 61 69 6c 73 20 64 75 65 20 74 6f  hat fails due to
c430: 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69   a constraint vi
c440: 6f 6c 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 61  olation is not a
c450: 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 69  .** successful i
c460: 6e 73 65 72 74 20 61 6e 64 20 64 6f 65 73 20 6e  nsert and does n
c470: 6f 74 20 63 68 61 6e 67 65 20 74 68 65 20 76 61  ot change the va
c480: 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20  lue returned by 
c490: 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e  this.** routine.
c4a0: 20 20 54 68 75 73 20 49 4e 53 45 52 54 20 4f 52    Thus INSERT OR
c4b0: 20 46 41 49 4c 2c 20 49 4e 53 45 52 54 20 4f 52   FAIL, INSERT OR
c4c0: 20 49 47 4e 4f 52 45 2c 20 49 4e 53 45 52 54 20   IGNORE, INSERT 
c4d0: 4f 52 20 52 4f 4c 4c 42 41 43 4b 2c 0a 2a 2a 20  OR ROLLBACK,.** 
c4e0: 61 6e 64 20 49 4e 53 45 52 54 20 4f 52 20 41 42  and INSERT OR AB
c4f0: 4f 52 54 20 6d 61 6b 65 20 6e 6f 20 63 68 61 6e  ORT make no chan
c500: 67 65 73 20 74 6f 20 74 68 65 20 72 65 74 75 72  ges to the retur
c510: 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 0a  n value of this.
c520: 2a 2a 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20  ** routine when 
c530: 74 68 65 69 72 20 69 6e 73 65 72 74 69 6f 6e 20  their insertion 
c540: 66 61 69 6c 73 2e 20 20 57 68 65 6e 20 49 4e 53  fails.  When INS
c550: 45 52 54 20 4f 52 20 52 45 50 4c 41 43 45 20 0a  ERT OR REPLACE .
c560: 2a 2a 20 65 6e 63 6f 75 6e 74 65 72 73 20 61 20  ** encounters a 
c570: 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61  constraint viola
c580: 74 69 6f 6e 2c 20 69 74 20 64 6f 65 73 20 6e 6f  tion, it does no
c590: 74 20 66 61 69 6c 2e 20 20 54 68 65 0a 2a 2a 20  t fail.  The.** 
c5a0: 49 4e 53 45 52 54 20 63 6f 6e 74 69 6e 75 65 73  INSERT continues
c5b0: 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 61   to completion a
c5c0: 66 74 65 72 20 64 65 6c 65 74 69 6e 67 20 72 6f  fter deleting ro
c5d0: 77 73 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a  ws that caused.*
c5e0: 2a 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74  * the constraint
c5f0: 20 70 72 6f 62 6c 65 6d 20 73 6f 20 49 4e 53 45   problem so INSE
c600: 52 54 20 4f 52 20 52 45 50 4c 41 43 45 20 77 69  RT OR REPLACE wi
c610: 6c 6c 20 61 6c 77 61 79 73 20 63 68 61 6e 67 65  ll always change
c620: 0a 2a 2a 20 74 68 65 20 72 65 74 75 72 6e 20 76  .** the return v
c630: 61 6c 75 65 20 6f 66 20 74 68 69 73 20 69 6e 74  alue of this int
c640: 65 72 66 61 63 65 2e 20 0a 2a 2a 0a 2a 2a 20 46  erface. .**.** F
c650: 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20  or the purposes 
c660: 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c  of this routine,
c670: 20 61 6e 20 69 6e 73 65 72 74 20 69 73 20 63 6f   an insert is co
c680: 6e 73 69 64 65 72 65 64 20 74 6f 0a 2a 2a 20 62  nsidered to.** b
c690: 65 20 73 75 63 63 65 73 73 66 75 6c 20 65 76 65  e successful eve
c6a0: 6e 20 69 66 20 69 74 20 69 73 20 73 75 62 73 65  n if it is subse
c6b0: 71 75 65 6e 74 6c 79 20 72 6f 6c 6c 65 64 20 62  quently rolled b
c6c0: 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52  ack..**.** INVAR
c6d0: 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31  IANTS:.**.** {F1
c6e0: 32 32 32 31 7d 20 54 68 65 20 5b 73 71 6c 69 74  2221} The [sqlit
c6f0: 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  e3_last_insert_r
c700: 6f 77 69 64 28 29 5d 20 66 75 6e 63 74 69 6f 6e  owid()] function
c710: 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20   returns the.** 
c720: 20 20 20 20 20 20 20 20 20 72 6f 77 69 64 20 6f           rowid o
c730: 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  f the most recen
c740: 74 20 73 75 63 63 65 73 73 66 75 6c 20 69 6e 73  t successful ins
c750: 65 72 74 20 64 6f 6e 65 0a 2a 2a 20 20 20 20 20  ert done.**     
c760: 20 20 20 20 20 6f 6e 20 74 68 65 20 73 61 6d 65       on the same
c770: 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
c780: 74 69 6f 6e 20 61 6e 64 20 77 69 74 68 69 6e 20  tion and within 
c790: 74 68 65 20 73 61 6d 65 0a 2a 2a 20 20 20 20 20  the same.**     
c7a0: 20 20 20 20 20 74 72 69 67 67 65 72 20 63 6f 6e       trigger con
c7b0: 74 65 78 74 2c 20 6f 72 20 7a 65 72 6f 20 69 66  text, or zero if
c7c0: 20 74 68 65 72 65 20 68 61 76 65 0a 2a 2a 20 20   there have.**  
c7d0: 20 20 20 20 20 20 20 20 62 65 65 6e 20 6e 6f 20          been no 
c7e0: 71 75 61 6c 69 66 79 69 6e 67 20 69 6e 73 65 72  qualifying inser
c7f0: 74 73 20 6f 6e 20 74 68 61 74 20 63 6f 6e 6e 65  ts on that conne
c800: 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31  ction..**.** {F1
c810: 32 32 32 33 7d 20 54 68 65 20 5b 73 71 6c 69 74  2223} The [sqlit
c820: 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  e3_last_insert_r
c830: 6f 77 69 64 28 29 5d 20 66 75 6e 63 74 69 6f 6e  owid()] function
c840: 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20   returns.**     
c850: 20 20 20 20 20 73 61 6d 65 20 76 61 6c 75 65 20       same value 
c860: 77 68 65 6e 20 63 61 6c 6c 65 64 20 66 72 6f 6d  when called from
c870: 20 74 68 65 20 73 61 6d 65 20 74 72 69 67 67 65   the same trigge
c880: 72 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 20 20 20  r context.**    
c890: 20 20 20 20 20 20 69 6d 6d 65 64 69 61 74 65 6c        immediatel
c8a0: 79 20 62 65 66 6f 72 65 20 61 6e 64 20 61 66 74  y before and aft
c8b0: 65 72 20 61 20 52 4f 4c 4c 42 41 43 4b 2e 0a 2a  er a ROLLBACK..*
c8c0: 2a 0a 2a 2a 20 4c 49 4d 49 54 41 54 49 4f 4e 53  *.** LIMITATIONS
c8d0: 3a 0a 2a 2a 0a 2a 2a 20 7b 55 31 32 32 33 32 7d  :.**.** {U12232}
c8e0: 20 49 66 20 61 20 73 65 70 61 72 61 74 65 20 74   If a separate t
c8f0: 68 72 65 61 64 20 64 6f 65 73 20 61 20 6e 65 77  hread does a new
c900: 20 69 6e 73 65 72 74 20 6f 6e 20 74 68 65 20 73   insert on the s
c910: 61 6d 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ame.**          
c920: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
c930: 69 6f 6e 20 77 68 69 6c 65 20 74 68 65 20 5b 73  ion while the [s
c940: 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
c950: 72 74 5f 72 6f 77 69 64 28 29 5d 0a 2a 2a 20 20  rt_rowid()].**  
c960: 20 20 20 20 20 20 20 20 66 75 6e 63 74 69 6f 6e          function
c970: 20 69 73 20 72 75 6e 6e 69 6e 67 20 61 6e 64 20   is running and 
c980: 74 68 75 73 20 63 68 61 6e 67 65 73 20 74 68 65  thus changes the
c990: 20 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77   last insert row
c9a0: 69 64 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  id,.**          
c9b0: 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72  then the value r
c9c0: 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69  eturned by [sqli
c9d0: 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f  te3_last_insert_
c9e0: 72 6f 77 69 64 28 29 5d 20 69 73 0a 2a 2a 20 20  rowid()] is.**  
c9f0: 20 20 20 20 20 20 20 20 75 6e 70 72 65 64 69 63          unpredic
ca00: 74 61 62 6c 65 20 61 6e 64 20 6d 69 67 68 74 20  table and might 
ca10: 6e 6f 74 20 65 71 75 61 6c 20 65 69 74 68 65 72  not equal either
ca20: 20 74 68 65 20 6f 6c 64 20 6f 72 20 74 68 65 20   the old or the 
ca30: 6e 65 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  new.**          
ca40: 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77 69  last insert rowi
ca50: 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  d..*/.SQLITE_API
ca60: 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73   sqlite3_int64 s
ca70: 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
ca80: 72 74 5f 72 6f 77 69 64 28 73 71 6c 69 74 65 33  rt_rowid(sqlite3
ca90: 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  *);../*.** CAPI3
caa0: 52 45 46 3a 20 43 6f 75 6e 74 20 54 68 65 20 4e  REF: Count The N
cab0: 75 6d 62 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f  umber Of Rows Mo
cac0: 64 69 66 69 65 64 20 7b 46 31 32 32 34 30 7d 0a  dified {F12240}.
cad0: 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
cae0: 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20  ion returns the 
caf0: 6e 75 6d 62 65 72 20 6f 66 20 64 61 74 61 62 61  number of databa
cb00: 73 65 20 72 6f 77 73 20 74 68 61 74 20 77 65 72  se rows that wer
cb10: 65 20 63 68 61 6e 67 65 64 0a 2a 2a 20 6f 72 20  e changed.** or 
cb20: 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65 6c 65  inserted or dele
cb30: 74 65 64 20 62 79 20 74 68 65 20 6d 6f 73 74 20  ted by the most 
cb40: 72 65 63 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 74  recently complet
cb50: 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ed SQL statement
cb60: 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6e 6e 65  .** on the conne
cb70: 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20  ction specified 
cb80: 62 79 20 74 68 65 20 66 69 72 73 74 20 70 61 72  by the first par
cb90: 61 6d 65 74 65 72 2e 20 20 4f 6e 6c 79 0a 2a 2a  ameter.  Only.**
cba0: 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 61 72   changes that ar
cbb0: 65 20 64 69 72 65 63 74 6c 79 20 73 70 65 63 69  e directly speci
cbc0: 66 69 65 64 20 62 79 20 74 68 65 20 49 4e 53 45  fied by the INSE
cbd0: 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 0a 2a  RT, UPDATE, or.*
cbe0: 2a 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65  * DELETE stateme
cbf0: 6e 74 20 61 72 65 20 63 6f 75 6e 74 65 64 2e 20  nt are counted. 
cc00: 20 41 75 78 69 6c 69 61 72 79 20 63 68 61 6e 67   Auxiliary chang
cc10: 65 73 20 63 61 75 73 65 64 20 62 79 0a 2a 2a 20  es caused by.** 
cc20: 74 72 69 67 67 65 72 73 20 61 72 65 20 6e 6f 74  triggers are not
cc30: 20 63 6f 75 6e 74 65 64 2e 20 55 73 65 20 74 68   counted. Use th
cc40: 65 20 5b 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c  e [sqlite3_total
cc50: 5f 63 68 61 6e 67 65 73 28 29 5d 20 66 75 6e 63  _changes()] func
cc60: 74 69 6f 6e 0a 2a 2a 20 74 6f 20 66 69 6e 64 20  tion.** to find 
cc70: 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  the total number
cc80: 20 6f 66 20 63 68 61 6e 67 65 73 20 69 6e 63 6c   of changes incl
cc90: 75 64 69 6e 67 20 63 68 61 6e 67 65 73 20 63 61  uding changes ca
cca0: 75 73 65 64 20 62 79 20 74 72 69 67 67 65 72 73  used by triggers
ccb0: 2e 0a 2a 2a 0a 2a 2a 20 41 20 22 72 6f 77 20 63  ..**.** A "row c
ccc0: 68 61 6e 67 65 22 20 69 73 20 61 20 63 68 61 6e  hange" is a chan
ccd0: 67 65 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 72  ge to a single r
cce0: 6f 77 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 74  ow of a single t
ccf0: 61 62 6c 65 0a 2a 2a 20 63 61 75 73 65 64 20 62  able.** caused b
cd00: 79 20 61 6e 20 49 4e 53 45 52 54 2c 20 44 45 4c  y an INSERT, DEL
cd10: 45 54 45 2c 20 6f 72 20 55 50 44 41 54 45 20 73  ETE, or UPDATE s
cd20: 74 61 74 65 6d 65 6e 74 2e 20 20 52 6f 77 73 20  tatement.  Rows 
cd30: 74 68 61 74 0a 2a 2a 20 61 72 65 20 63 68 61 6e  that.** are chan
cd40: 67 65 64 20 61 73 20 73 69 64 65 20 65 66 66 65  ged as side effe
cd50: 63 74 73 20 6f 66 20 52 45 50 4c 41 43 45 20 63  cts of REPLACE c
cd60: 6f 6e 73 74 72 61 69 6e 74 20 72 65 73 6f 6c 75  onstraint resolu
cd70: 74 69 6f 6e 2c 0a 2a 2a 20 72 6f 6c 6c 62 61 63  tion,.** rollbac
cd80: 6b 2c 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73  k, ABORT process
cd90: 69 6e 67 2c 20 44 52 4f 50 20 54 41 42 4c 45 2c  ing, DROP TABLE,
cda0: 20 6f 72 20 62 79 20 61 6e 79 20 6f 74 68 65 72   or by any other
cdb0: 0a 2a 2a 20 6d 65 63 68 61 6e 69 73 6d 73 20 64  .** mechanisms d
cdc0: 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73 20 64  o not count as d
cdd0: 69 72 65 63 74 20 72 6f 77 20 63 68 61 6e 67 65  irect row change
cde0: 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 22 74 72 69 67  s..**.** A "trig
cdf0: 67 65 72 20 63 6f 6e 74 65 78 74 22 20 69 73 20  ger context" is 
ce00: 61 20 73 63 6f 70 65 20 6f 66 20 65 78 65 63 75  a scope of execu
ce10: 74 69 6f 6e 20 74 68 61 74 20 62 65 67 69 6e 73  tion that begins
ce20: 20 61 6e 64 0a 2a 2a 20 65 6e 64 73 20 77 69 74   and.** ends wit
ce30: 68 20 74 68 65 20 73 63 72 69 70 74 20 6f 66 20  h the script of 
ce40: 61 20 74 72 69 67 67 65 72 2e 20 20 4d 6f 73 74  a trigger.  Most
ce50: 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20   SQL statements 
ce60: 61 72 65 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64  are.** evaluated
ce70: 20 6f 75 74 73 69 64 65 20 6f 66 20 61 6e 79 20   outside of any 
ce80: 74 72 69 67 67 65 72 2e 20 20 54 68 69 73 20 69  trigger.  This i
ce90: 73 20 74 68 65 20 22 74 6f 70 20 6c 65 76 65 6c  s the "top level
cea0: 22 0a 2a 2a 20 74 72 69 67 67 65 72 20 63 6f 6e  ".** trigger con
ceb0: 74 65 78 74 2e 20 20 49 66 20 61 20 74 72 69 67  text.  If a trig
cec0: 67 65 72 20 66 69 72 65 73 20 66 72 6f 6d 20 74  ger fires from t
ced0: 68 65 20 74 6f 70 20 6c 65 76 65 6c 2c 20 61 0a  he top level, a.
cee0: 2a 2a 20 6e 65 77 20 74 72 69 67 67 65 72 20 63  ** new trigger c
cef0: 6f 6e 74 65 78 74 20 69 73 20 65 6e 74 65 72 65  ontext is entere
cf00: 64 20 66 6f 72 20 74 68 65 20 64 75 72 61 74 69  d for the durati
cf10: 6f 6e 20 6f 66 20 74 68 61 74 20 6f 6e 65 0a 2a  on of that one.*
cf20: 2a 20 74 72 69 67 67 65 72 2e 20 20 53 75 62 74  * trigger.  Subt
cf30: 72 69 67 67 65 72 73 20 63 72 65 61 74 65 20 73  riggers create s
cf40: 75 62 63 6f 6e 74 65 78 74 73 20 66 6f 72 20 74  ubcontexts for t
cf50: 68 65 69 72 20 64 75 72 61 74 69 6f 6e 2e 0a 2a  heir duration..*
cf60: 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 5b 73 71  *.** Calling [sq
cf70: 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 6f 72  lite3_exec()] or
cf80: 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
cf90: 5d 20 72 65 63 75 72 73 69 76 65 6c 79 20 64 6f  ] recursively do
cfa0: 65 73 0a 2a 2a 20 6e 6f 74 20 63 72 65 61 74 65  es.** not create
cfb0: 20 61 20 6e 65 77 20 74 72 69 67 67 65 72 20 63   a new trigger c
cfc0: 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  ontext..**.** Th
cfd0: 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
cfe0: 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
cff0: 66 20 64 69 72 65 63 74 20 72 6f 77 20 63 68 61  f direct row cha
d000: 6e 67 65 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6d  nges in the.** m
d010: 6f 73 74 20 72 65 63 65 6e 74 20 49 4e 53 45 52  ost recent INSER
d020: 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45  T, UPDATE, or DE
d030: 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20 77  LETE statement w
d040: 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 0a 2a  ithin the same.*
d050: 2a 20 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78  * trigger contex
d060: 74 2e 0a 2a 2a 0a 2a 2a 20 53 6f 20 77 68 65 6e  t..**.** So when
d070: 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65   called from the
d080: 20 74 6f 70 20 6c 65 76 65 6c 2c 20 74 68 69 73   top level, this
d090: 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
d0a0: 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20  s the.** number 
d0b0: 6f 66 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68  of changes in th
d0c0: 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 49 4e  e most recent IN
d0d0: 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72  SERT, UPDATE, or
d0e0: 20 44 45 4c 45 54 45 0a 2a 2a 20 74 68 61 74 20   DELETE.** that 
d0f0: 61 6c 73 6f 20 6f 63 63 75 72 72 65 64 20 61 74  also occurred at
d100: 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2e 0a   the top level..
d110: 2a 2a 20 57 69 74 68 69 6e 20 74 68 65 20 62 6f  ** Within the bo
d120: 64 79 20 6f 66 20 61 20 74 72 69 67 67 65 72 2c  dy of a trigger,
d130: 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 68 61   the sqlite3_cha
d140: 6e 67 65 73 28 29 20 69 6e 74 65 72 66 61 63 65  nges() interface
d150: 0a 2a 2a 20 63 61 6e 20 62 65 20 63 61 6c 6c 65  .** can be calle
d160: 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 75  d to find the nu
d170: 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 6e 67  mber of.** chang
d180: 65 73 20 69 6e 20 74 68 65 20 6d 6f 73 74 20 72  es in the most r
d190: 65 63 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65  ecently complete
d1a0: 64 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45  d INSERT, UPDATE
d1b0: 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 73  , or DELETE.** s
d1c0: 74 61 74 65 6d 65 6e 74 20 77 69 74 68 69 6e 20  tatement within 
d1d0: 74 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20  the body of the 
d1e0: 73 61 6d 65 20 74 72 69 67 67 65 72 2e 0a 2a 2a  same trigger..**
d1f0: 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20 6e 75   However, the nu
d200: 6d 62 65 72 20 72 65 74 75 72 6e 65 64 20 64 6f  mber returned do
d210: 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 69  es not include i
d220: 6e 20 63 68 61 6e 67 65 73 0a 2a 2a 20 63 61 75  n changes.** cau
d230: 73 65 64 20 62 79 20 73 75 62 74 72 69 67 67 65  sed by subtrigge
d240: 72 73 20 73 69 6e 63 65 20 74 68 65 79 20 68 61  rs since they ha
d250: 76 65 20 74 68 65 69 72 20 6f 77 6e 20 63 6f 6e  ve their own con
d260: 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69  text..**.** SQLi
d270: 74 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  te implements th
d280: 65 20 63 6f 6d 6d 61 6e 64 20 22 44 45 4c 45 54  e command "DELET
d290: 45 20 46 52 4f 4d 20 74 61 62 6c 65 22 20 77 69  E FROM table" wi
d2a0: 74 68 6f 75 74 0a 2a 2a 20 61 20 57 48 45 52 45  thout.** a WHERE
d2b0: 20 63 6c 61 75 73 65 20 62 79 20 64 72 6f 70 70   clause by dropp
d2c0: 69 6e 67 20 61 6e 64 20 72 65 63 72 65 61 74 69  ing and recreati
d2d0: 6e 67 20 74 68 65 20 74 61 62 6c 65 2e 20 20 28  ng the table.  (
d2e0: 54 68 69 73 20 69 73 20 6d 75 63 68 0a 2a 2a 20  This is much.** 
d2f0: 66 61 73 74 65 72 20 74 68 61 6e 20 67 6f 69 6e  faster than goin
d300: 67 20 74 68 72 6f 75 67 68 20 61 6e 64 20 64 65  g through and de
d310: 6c 65 74 69 6e 67 20 69 6e 64 69 76 69 64 75 61  leting individua
d320: 6c 20 65 6c 65 6d 65 6e 74 73 20 66 72 6f 6d 20  l elements from 
d330: 74 68 65 0a 2a 2a 20 74 61 62 6c 65 2e 29 20 20  the.** table.)  
d340: 42 65 63 61 75 73 65 20 6f 66 20 74 68 69 73 20  Because of this 
d350: 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2c 20 74 68  optimization, th
d360: 65 20 64 65 6c 65 74 69 6f 6e 73 20 69 6e 0a 2a  e deletions in.*
d370: 2a 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 74  * "DELETE FROM t
d380: 61 62 6c 65 22 20 61 72 65 20 6e 6f 74 20 72 6f  able" are not ro
d390: 77 20 63 68 61 6e 67 65 73 20 61 6e 64 20 77 69  w changes and wi
d3a0: 6c 6c 20 6e 6f 74 20 62 65 20 63 6f 75 6e 74 65  ll not be counte
d3b0: 64 0a 2a 2a 20 62 79 20 74 68 65 20 73 71 6c 69  d.** by the sqli
d3c0: 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20 6f 72  te3_changes() or
d3d0: 20 5b 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f   [sqlite3_total_
d3e0: 63 68 61 6e 67 65 73 28 29 5d 20 66 75 6e 63 74  changes()] funct
d3f0: 69 6f 6e 73 2e 0a 2a 2a 20 54 6f 20 67 65 74 20  ions..** To get 
d400: 61 6e 20 61 63 63 75 72 61 74 65 20 63 6f 75 6e  an accurate coun
d410: 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20  t of the number 
d420: 6f 66 20 72 6f 77 73 20 64 65 6c 65 74 65 64 2c  of rows deleted,
d430: 20 75 73 65 0a 2a 2a 20 22 44 45 4c 45 54 45 20   use.** "DELETE 
d440: 46 52 4f 4d 20 74 61 62 6c 65 20 57 48 45 52 45  FROM table WHERE
d450: 20 31 22 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a   1" instead..**.
d460: 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a  ** INVARIANTS:.*
d470: 2a 0a 2a 2a 20 7b 46 31 32 32 34 31 7d 20 54 68  *.** {F12241} Th
d480: 65 20 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67  e [sqlite3_chang
d490: 65 73 28 29 5d 20 66 75 6e 63 74 69 6f 6e 20 72  es()] function r
d4a0: 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65  eturns the numbe
d4b0: 72 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20  r of.**         
d4c0: 20 72 6f 77 20 63 68 61 6e 67 65 73 20 63 61 75   row changes cau
d4d0: 73 65 64 20 62 79 20 74 68 65 20 6d 6f 73 74 20  sed by the most 
d4e0: 72 65 63 65 6e 74 20 49 4e 53 45 52 54 2c 20 55  recent INSERT, U
d4f0: 50 44 41 54 45 2c 0a 2a 2a 20 20 20 20 20 20 20  PDATE,.**       
d500: 20 20 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61     or DELETE sta
d510: 74 65 6d 65 6e 74 20 6f 6e 20 74 68 65 20 73 61  tement on the sa
d520: 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  me database conn
d530: 65 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 20 20  ection and.**   
d540: 20 20 20 20 20 20 20 77 69 74 68 69 6e 20 74 68         within th
d550: 65 20 73 61 6d 65 20 74 72 69 67 67 65 72 20 63  e same trigger c
d560: 6f 6e 74 65 78 74 2c 20 6f 72 20 7a 65 72 6f 20  ontext, or zero 
d570: 69 66 20 74 68 65 72 65 20 68 61 76 65 0a 2a 2a  if there have.**
d580: 20 20 20 20 20 20 20 20 20 20 6e 6f 74 20 62 65            not be
d590: 65 6e 20 61 6e 79 20 71 75 61 6c 69 66 79 69 6e  en any qualifyin
d5a0: 67 20 72 6f 77 20 63 68 61 6e 67 65 73 2e 0a 2a  g row changes..*
d5b0: 2a 0a 2a 2a 20 4c 49 4d 49 54 41 54 49 4f 4e 53  *.** LIMITATIONS
d5c0: 3a 0a 2a 2a 0a 2a 2a 20 7b 55 31 32 32 35 32 7d  :.**.** {U12252}
d5d0: 20 49 66 20 61 20 73 65 70 61 72 61 74 65 20 74   If a separate t
d5e0: 68 72 65 61 64 20 6d 61 6b 65 73 20 63 68 61 6e  hread makes chan
d5f0: 67 65 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 20  ges on the same 
d600: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
d610: 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ion.**          
d620: 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 63  while [sqlite3_c
d630: 68 61 6e 67 65 73 28 29 5d 20 69 73 20 72 75 6e  hanges()] is run
d640: 6e 69 6e 67 20 74 68 65 6e 20 74 68 65 20 76 61  ning then the va
d650: 6c 75 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20  lue returned.** 
d660: 20 20 20 20 20 20 20 20 20 69 73 20 75 6e 70 72           is unpr
d670: 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 75 6e  edictable and un
d680: 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53  meaningful..*/.S
d690: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
d6a0: 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 73 71  lite3_changes(sq
d6b0: 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  lite3*);../*.** 
d6c0: 43 41 50 49 33 52 45 46 3a 20 54 6f 74 61 6c 20  CAPI3REF: Total 
d6d0: 4e 75 6d 62 65 72 20 4f 66 20 52 6f 77 73 20 4d  Number Of Rows M
d6e0: 6f 64 69 66 69 65 64 20 7b 46 31 32 32 36 30 7d  odified {F12260}
d6f0: 0a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  .***.** This fun
d700: 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68  ction returns th
d710: 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 20  e number of row 
d720: 63 68 61 6e 67 65 73 20 63 61 75 73 65 64 0a 2a  changes caused.*
d730: 2a 20 62 79 20 49 4e 53 45 52 54 2c 20 55 50 44  * by INSERT, UPD
d740: 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20 73 74  ATE or DELETE st
d750: 61 74 65 6d 65 6e 74 73 20 73 69 6e 63 65 20 74  atements since t
d760: 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
d770: 6c 65 0a 2a 2a 20 77 61 73 20 6f 70 65 6e 65 64  le.** was opened
d780: 2e 20 20 54 68 65 20 63 6f 75 6e 74 20 69 6e 63  .  The count inc
d790: 6c 75 64 65 73 20 61 6c 6c 20 63 68 61 6e 67 65  ludes all change
d7a0: 73 20 66 72 6f 6d 20 61 6c 6c 20 74 72 69 67 67  s from all trigg
d7b0: 65 72 0a 2a 2a 20 63 6f 6e 74 65 78 74 73 2e 20  er.** contexts. 
d7c0: 20 42 75 74 20 74 68 65 20 63 6f 75 6e 74 20 64   But the count d
d7d0: 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20  oes not include 
d7e0: 63 68 61 6e 67 65 73 20 75 73 65 64 20 74 6f 0a  changes used to.
d7f0: 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 52 45 50  ** implement REP
d800: 4c 41 43 45 20 63 6f 6e 73 74 72 61 69 6e 74 73  LACE constraints
d810: 2c 20 64 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 6f  , do rollbacks o
d820: 72 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69  r ABORT processi
d830: 6e 67 2c 0a 2a 2a 20 6f 72 20 44 52 4f 50 20 74  ng,.** or DROP t
d840: 61 62 6c 65 20 70 72 6f 63 65 73 73 69 6e 67 2e  able processing.
d850: 0a 2a 2a 20 54 68 65 20 63 68 61 6e 67 65 73 0a  .** The changes.
d860: 2a 2a 20 61 72 65 20 63 6f 75 6e 74 65 64 20 61  ** are counted a
d870: 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 73 74  s soon as the st
d880: 61 74 65 6d 65 6e 74 20 74 68 61 74 20 6d 61 6b  atement that mak
d890: 65 73 20 74 68 65 6d 20 69 73 20 63 6f 6d 70 6c  es them is compl
d8a0: 65 74 65 64 20 0a 2a 2a 20 28 77 68 65 6e 20 74  eted .** (when t
d8b0: 68 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e  he statement han
d8c0: 64 6c 65 20 69 73 20 70 61 73 73 65 64 20 74 6f  dle is passed to
d8d0: 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28   [sqlite3_reset(
d8e0: 29 5d 20 6f 72 20 0a 2a 2a 20 5b 73 71 6c 69 74  )] or .** [sqlit
d8f0: 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 29 2e  e3_finalize()]).
d900: 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6d  .**.** SQLite im
d910: 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 63 6f 6d  plements the com
d920: 6d 61 6e 64 20 22 44 45 4c 45 54 45 20 46 52 4f  mand "DELETE FRO
d930: 4d 20 74 61 62 6c 65 22 20 77 69 74 68 6f 75 74  M table" without
d940: 0a 2a 2a 20 61 20 57 48 45 52 45 20 63 6c 61 75  .** a WHERE clau
d950: 73 65 20 62 79 20 64 72 6f 70 70 69 6e 67 20 61  se by dropping a
d960: 6e 64 20 72 65 63 72 65 61 74 69 6e 67 20 74 68  nd recreating th
d970: 65 20 74 61 62 6c 65 2e 20 20 28 54 68 69 73 20  e table.  (This 
d980: 69 73 20 6d 75 63 68 0a 2a 2a 20 66 61 73 74 65  is much.** faste
d990: 72 20 74 68 61 6e 20 67 6f 69 6e 67 0a 2a 2a 20  r than going.** 
d9a0: 74 68 72 6f 75 67 68 20 61 6e 64 20 64 65 6c 65  through and dele
d9b0: 74 69 6e 67 20 69 6e 64 69 76 69 64 75 61 6c 20  ting individual 
d9c0: 65 6c 65 6d 65 6e 74 73 20 66 72 6f 6d 20 74 68  elements from th
d9d0: 65 20 74 61 62 6c 65 2e 29 20 20 42 65 63 61 75  e table.)  Becau
d9e0: 73 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 6f 70  se of.** this op
d9f0: 74 69 6d 69 7a 61 74 69 6f 6e 2c 20 74 68 65 20  timization, the 
da00: 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 66 6f 72  change count for
da10: 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 61   "DELETE FROM ta
da20: 62 6c 65 22 20 77 69 6c 6c 20 62 65 0a 2a 2a 20  ble" will be.** 
da30: 7a 65 72 6f 20 72 65 67 61 72 64 6c 65 73 73 20  zero regardless 
da40: 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  of the number of
da50: 20 65 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 77   elements that w
da60: 65 72 65 20 6f 72 69 67 69 6e 61 6c 6c 79 20 69  ere originally i
da70: 6e 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 2e 20  n the.** table. 
da80: 54 6f 20 67 65 74 20 61 6e 20 61 63 63 75 72 61  To get an accura
da90: 74 65 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20  te count of the 
daa0: 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 64  number of rows d
dab0: 65 6c 65 74 65 64 2c 20 75 73 65 0a 2a 2a 20 22  eleted, use.** "
dac0: 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c  DELETE FROM tabl
dad0: 65 20 57 48 45 52 45 20 31 22 20 69 6e 73 74 65  e WHERE 1" inste
dae0: 61 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  ad..**.** See al
daf0: 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  so the [sqlite3_
db00: 63 68 61 6e 67 65 73 28 29 5d 20 69 6e 74 65 72  changes()] inter
db10: 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41  face..**.** INVA
db20: 52 49 41 4e 54 53 3a 0a 2a 2a 20 0a 2a 2a 20 7b  RIANTS:.** .** {
db30: 46 31 32 32 36 31 7d 20 54 68 65 20 5b 73 71 6c  F12261} The [sql
db40: 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67  ite3_total_chang
db50: 65 73 28 29 5d 20 72 65 74 75 72 6e 73 20 74 68  es()] returns th
db60: 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 0a 2a  e total number.*
db70: 2a 20 20 20 20 20 20 20 20 20 20 6f 66 20 72 6f  *          of ro
db80: 77 20 63 68 61 6e 67 65 73 20 63 61 75 73 65 64  w changes caused
db90: 20 62 79 20 49 4e 53 45 52 54 2c 20 55 50 44 41   by INSERT, UPDA
dba0: 54 45 2c 20 61 6e 64 2f 6f 72 20 44 45 4c 45 54  TE, and/or DELET
dbb0: 45 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74  E.**          st
dbc0: 61 74 65 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20  atements on the 
dbd0: 73 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63  same [database c
dbe0: 6f 6e 6e 65 63 74 69 6f 6e 5d 2c 20 69 6e 20 61  onnection], in a
dbf0: 6e 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  ny.**          t
dc00: 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2c 20  rigger context, 
dc10: 73 69 6e 63 65 20 74 68 65 20 64 61 74 61 62 61  since the databa
dc20: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 61  se connection wa
dc30: 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 72  s.**          cr
dc40: 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 4c 49 4d  eated..**.** LIM
dc50: 49 54 41 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20  ITATIONS:.**.** 
dc60: 7b 55 31 32 32 36 34 7d 20 49 66 20 61 20 73 65  {U12264} If a se
dc70: 70 61 72 61 74 65 20 74 68 72 65 61 64 20 6d 61  parate thread ma
dc80: 6b 65 73 20 63 68 61 6e 67 65 73 20 6f 6e 20 74  kes changes on t
dc90: 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
dca0: 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 20   connection.**  
dcb0: 20 20 20 20 20 20 20 20 77 68 69 6c 65 20 5b 73          while [s
dcc0: 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61  qlite3_total_cha
dcd0: 6e 67 65 73 28 29 5d 20 69 73 20 72 75 6e 6e 69  nges()] is runni
dce0: 6e 67 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75  ng then the valu
dcf0: 65 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72  e .**          r
dd00: 65 74 75 72 6e 65 64 20 69 73 20 75 6e 70 72 65  eturned is unpre
dd10: 64 69 63 74 61 62 6c 65 20 61 6e 64 20 75 6e 6d  dictable and unm
dd20: 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51  eaningful..*/.SQ
dd30: 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
dd40: 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67  ite3_total_chang
dd50: 65 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f  es(sqlite3*);../
dd60: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49  *.** CAPI3REF: I
dd70: 6e 74 65 72 72 75 70 74 20 41 20 4c 6f 6e 67 2d  nterrupt A Long-
dd80: 52 75 6e 6e 69 6e 67 20 51 75 65 72 79 20 7b 46  Running Query {F
dd90: 31 32 32 37 30 7d 0a 2a 2a 0a 2a 2a 20 54 68 69  12270}.**.** Thi
dda0: 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 75 73 65  s function cause
ddb0: 73 20 61 6e 79 20 70 65 6e 64 69 6e 67 20 64 61  s any pending da
ddc0: 74 61 62 61 73 65 20 6f 70 65 72 61 74 69 6f 6e  tabase operation
ddd0: 20 74 6f 20 61 62 6f 72 74 20 61 6e 64 0a 2a 2a   to abort and.**
dde0: 20 72 65 74 75 72 6e 20 61 74 20 69 74 73 20 65   return at its e
ddf0: 61 72 6c 69 65 73 74 20 6f 70 70 6f 72 74 75 6e  arliest opportun
de00: 69 74 79 2e 20 54 68 69 73 20 72 6f 75 74 69 6e  ity. This routin
de10: 65 20 69 73 20 74 79 70 69 63 61 6c 6c 79 0a 2a  e is typically.*
de20: 2a 20 63 61 6c 6c 65 64 20 69 6e 20 72 65 73 70  * called in resp
de30: 6f 6e 73 65 20 74 6f 20 61 20 75 73 65 72 20 61  onse to a user a
de40: 63 74 69 6f 6e 20 73 75 63 68 20 61 73 20 70 72  ction such as pr
de50: 65 73 73 69 6e 67 20 22 43 61 6e 63 65 6c 22 0a  essing "Cancel".
de60: 2a 2a 20 6f 72 20 43 74 72 6c 2d 43 20 77 68 65  ** or Ctrl-C whe
de70: 72 65 20 74 68 65 20 75 73 65 72 20 77 61 6e 74  re the user want
de80: 73 20 61 20 6c 6f 6e 67 20 71 75 65 72 79 20 6f  s a long query o
de90: 70 65 72 61 74 69 6f 6e 20 74 6f 20 68 61 6c 74  peration to halt
dea0: 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e  .** immediately.
deb0: 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 73 61 66  .**.** It is saf
dec0: 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 72  e to call this r
ded0: 6f 75 74 69 6e 65 20 66 72 6f 6d 20 61 20 74 68  outine from a th
dee0: 72 65 61 64 20 64 69 66 66 65 72 65 6e 74 20 66  read different f
def0: 72 6f 6d 20 74 68 65 0a 2a 2a 20 74 68 72 65 61  rom the.** threa
df00: 64 20 74 68 61 74 20 69 73 20 63 75 72 72 65 6e  d that is curren
df10: 74 6c 79 20 72 75 6e 6e 69 6e 67 20 74 68 65 20  tly running the 
df20: 64 61 74 61 62 61 73 65 20 6f 70 65 72 61 74 69  database operati
df30: 6f 6e 2e 20 20 42 75 74 20 69 74 0a 2a 2a 20 69  on.  But it.** i
df40: 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 63 61  s not safe to ca
df50: 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ll this routine 
df60: 77 69 74 68 20 61 20 64 61 74 61 62 61 73 65 20  with a database 
df70: 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 0a  connection that.
df80: 2a 2a 20 69 73 20 63 6c 6f 73 65 64 20 6f 72 20  ** is closed or 
df90: 6d 69 67 68 74 20 63 6c 6f 73 65 20 62 65 66 6f  might close befo
dfa0: 72 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  re sqlite3_inter
dfb0: 72 75 70 74 28 29 20 72 65 74 75 72 6e 73 2e 0a  rupt() returns..
dfc0: 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 53 51 4c 20  **.** If an SQL 
dfd0: 69 73 20 76 65 72 79 20 6e 65 61 72 6c 79 20 66  is very nearly f
dfe0: 69 6e 69 73 68 65 64 20 61 74 20 74 68 65 20 74  inished at the t
dff0: 69 6d 65 20 77 68 65 6e 20 73 71 6c 69 74 65 33  ime when sqlite3
e000: 5f 69 6e 74 65 72 72 75 70 74 28 29 0a 2a 2a 20  _interrupt().** 
e010: 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20  is called, then 
e020: 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 68 61 76  it might not hav
e030: 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79  e an opportunity
e040: 20 74 6f 20 62 65 20 69 6e 74 65 72 72 75 70 74   to be interrupt
e050: 65 64 2e 0a 2a 2a 20 49 74 20 6d 69 67 68 74 20  ed..** It might 
e060: 63 6f 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6d 70  continue to comp
e070: 6c 65 74 69 6f 6e 2e 0a 2a 2a 20 41 6e 20 53 51  letion..** An SQ
e080: 4c 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74  L operation that
e090: 20 69 73 20 69 6e 74 65 72 72 75 70 74 65 64 20   is interrupted 
e0a0: 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 5b  will return.** [
e0b0: 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54  SQLITE_INTERRUPT
e0c0: 5d 2e 20 20 49 66 20 74 68 65 20 69 6e 74 65 72  ].  If the inter
e0d0: 72 75 70 74 65 64 20 53 51 4c 20 6f 70 65 72 61  rupted SQL opera
e0e0: 74 69 6f 6e 20 69 73 20 61 6e 0a 2a 2a 20 49 4e  tion is an.** IN
e0f0: 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72  SERT, UPDATE, or
e100: 20 44 45 4c 45 54 45 20 74 68 61 74 20 69 73 20   DELETE that is 
e110: 69 6e 73 69 64 65 20 61 6e 20 65 78 70 6c 69 63  inside an explic
e120: 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  it transaction, 
e130: 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 65 6e 74  .** then the ent
e140: 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ire transaction 
e150: 77 69 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 20 62  will be rolled b
e160: 61 63 6b 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  ack automaticall
e170: 79 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20  y..** A call to 
e180: 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
e190: 74 28 29 20 68 61 73 20 6e 6f 20 65 66 66 65 63  t() has no effec
e1a0: 74 20 6f 6e 20 53 51 4c 20 73 74 61 74 65 6d 65  t on SQL stateme
e1b0: 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20  nts.** that are 
e1c0: 73 74 61 72 74 65 64 20 61 66 74 65 72 20 73 71  started after sq
e1d0: 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28  lite3_interrupt(
e1e0: 29 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a  ) returns..**.**
e1f0: 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a   INVARIANTS:.**.
e200: 2a 2a 20 7b 46 31 32 32 37 31 7d 20 54 68 65 20  ** {F12271} The 
e210: 5b 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75  [sqlite3_interru
e220: 70 74 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20  pt()] interface 
e230: 77 69 6c 6c 20 66 6f 72 63 65 20 61 6c 6c 20 72  will force all r
e240: 75 6e 6e 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20  unning.**       
e250: 20 20 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74     SQL statement
e260: 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
e270: 68 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  h the same datab
e280: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a  ase connection.*
e290: 2a 20 20 20 20 20 20 20 20 20 20 74 6f 20 68 61  *          to ha
e2a0: 6c 74 20 61 66 74 65 72 20 70 72 6f 63 65 73 73  lt after process
e2b0: 69 6e 67 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20  ing at most one 
e2c0: 61 64 64 69 74 69 6f 6e 61 6c 20 72 6f 77 20 6f  additional row o
e2d0: 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 64 61  f.**          da
e2e0: 74 61 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 32 37  ta..**.** {F1227
e2f0: 32 7d 20 41 6e 79 20 53 51 4c 20 73 74 61 74 65  2} Any SQL state
e300: 6d 65 6e 74 20 74 68 61 74 20 69 73 20 69 6e 74  ment that is int
e310: 65 72 72 75 70 74 65 64 20 62 79 20 5b 73 71 6c  errupted by [sql
e320: 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29  ite3_interrupt()
e330: 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 69  ].**          wi
e340: 6c 6c 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54  ll return [SQLIT
e350: 45 5f 49 4e 54 45 52 52 55 50 54 5d 2e 0a 2a 2a  E_INTERRUPT]..**
e360: 0a 2a 2a 20 4c 49 4d 49 54 41 54 49 4f 4e 53 3a  .** LIMITATIONS:
e370: 0a 2a 2a 0a 2a 2a 20 7b 55 31 32 32 37 39 7d 20  .**.** {U12279} 
e380: 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
e390: 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 6c 6f 73 65  connection close
e3a0: 73 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 33  s while [sqlite3
e3b0: 5f 69 6e 74 65 72 72 75 70 74 28 29 5d 0a 2a 2a  _interrupt()].**
e3c0: 20 20 20 20 20 20 20 20 20 20 69 73 20 72 75 6e            is run
e3d0: 6e 69 6e 67 20 74 68 65 6e 20 62 61 64 20 74 68  ning then bad th
e3e0: 69 6e 67 73 20 77 69 6c 6c 20 6c 69 6b 65 6c 79  ings will likely
e3f0: 20 68 61 70 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49   happen..*/.SQLI
e400: 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
e410: 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 73 71  te3_interrupt(sq
e420: 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  lite3*);../*.** 
e430: 43 41 50 49 33 52 45 46 3a 20 44 65 74 65 72 6d  CAPI3REF: Determ
e440: 69 6e 65 20 49 66 20 41 6e 20 53 51 4c 20 53 74  ine If An SQL St
e450: 61 74 65 6d 65 6e 74 20 49 73 20 43 6f 6d 70 6c  atement Is Compl
e460: 65 74 65 20 7b 46 31 30 35 31 30 7d 0a 2a 2a 0a  ete {F10510}.**.
e470: 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
e480: 73 20 61 72 65 20 75 73 65 66 75 6c 20 66 6f 72  s are useful for
e490: 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 69 6e   command-line in
e4a0: 70 75 74 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  put to determine
e4b0: 20 69 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65   if the.** curre
e4c0: 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 74 65 78  ntly entered tex
e4d0: 74 20 73 65 65 6d 73 20 74 6f 20 66 6f 72 6d 20  t seems to form 
e4e0: 63 6f 6d 70 6c 65 74 65 20 61 20 53 51 4c 20 73  complete a SQL s
e4f0: 74 61 74 65 6d 65 6e 74 20 6f 72 0a 2a 2a 20 69  tatement or.** i
e500: 66 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 70  f additional inp
e510: 75 74 20 69 73 20 6e 65 65 64 65 64 20 62 65 66  ut is needed bef
e520: 6f 72 65 20 73 65 6e 64 69 6e 67 20 74 68 65 20  ore sending the 
e530: 74 65 78 74 20 69 6e 74 6f 0a 2a 2a 20 53 51 4c  text into.** SQL
e540: 69 74 65 20 66 6f 72 20 70 61 72 73 69 6e 67 2e  ite for parsing.
e550: 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73    These routines
e560: 20 72 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   return true if 
e570: 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67  the input string
e580: 0a 2a 2a 20 61 70 70 65 61 72 73 20 74 6f 20 62  .** appears to b
e590: 65 20 61 20 63 6f 6d 70 6c 65 74 65 20 53 51 4c  e a complete SQL
e5a0: 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41 20 73   statement.  A s
e5b0: 74 61 74 65 6d 65 6e 74 20 69 73 20 6a 75 64 67  tatement is judg
e5c0: 65 64 20 74 6f 20 62 65 0a 2a 2a 20 63 6f 6d 70  ed to be.** comp
e5d0: 6c 65 74 65 20 69 66 20 69 74 20 65 6e 64 73 20  lete if it ends 
e5e0: 77 69 74 68 20 61 20 73 65 6d 69 63 6f 6c 6f 6e  with a semicolon
e5f0: 20 74 6f 6b 65 6e 20 61 6e 64 20 69 73 20 6e 6f   token and is no
e600: 74 20 61 20 66 72 61 67 6d 65 6e 74 20 6f 66 20  t a fragment of 
e610: 61 0a 2a 2a 20 43 52 45 41 54 45 20 54 52 49 47  a.** CREATE TRIG
e620: 47 45 52 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  GER statement.  
e630: 53 65 6d 69 63 6f 6c 6f 6e 73 20 74 68 61 74 20  Semicolons that 
e640: 61 72 65 20 65 6d 62 65 64 64 65 64 20 77 69 74  are embedded wit
e650: 68 69 6e 0a 2a 2a 20 73 74 72 69 6e 67 20 6c 69  hin.** string li
e660: 74 65 72 61 6c 73 20 6f 72 20 71 75 6f 74 65 64  terals or quoted
e670: 20 69 64 65 6e 74 69 66 69 65 72 20 6e 61 6d 65   identifier name
e680: 73 20 6f 72 20 63 6f 6d 6d 65 6e 74 73 20 61 72  s or comments ar
e690: 65 20 6e 6f 74 0a 2a 2a 20 69 6e 64 65 70 65 6e  e not.** indepen
e6a0: 64 65 6e 74 20 74 6f 6b 65 6e 73 20 28 74 68 65  dent tokens (the
e6b0: 79 20 61 72 65 20 70 61 72 74 20 6f 66 20 74 68  y are part of th
e6c0: 65 20 74 6f 6b 65 6e 20 69 6e 20 77 68 69 63 68  e token in which
e6d0: 20 74 68 65 79 20 61 72 65 0a 2a 2a 20 65 6d 62   they are.** emb
e6e0: 65 64 64 65 64 29 20 61 6e 64 20 74 68 75 73 20  edded) and thus 
e6f0: 64 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73 20  do not count as 
e700: 61 20 73 74 61 74 65 6d 65 6e 74 20 74 65 72 6d  a statement term
e710: 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  inator..**.** Th
e720: 65 73 65 20 72 6f 75 74 69 6e 65 73 20 64 6f 20  ese routines do 
e730: 6e 6f 74 20 70 61 72 73 65 20 74 68 65 20 53 51  not parse the SQ
e740: 4c 20 61 6e 64 0a 2a 2a 20 73 6f 20 77 69 6c 6c  L and.** so will
e750: 20 6e 6f 74 20 64 65 74 65 63 74 20 73 79 6e 74   not detect synt
e760: 61 63 74 69 63 61 6c 6c 79 20 69 6e 63 6f 72 72  actically incorr
e770: 65 63 74 20 53 51 4c 2e 0a 2a 2a 0a 2a 2a 20 49  ect SQL..**.** I
e780: 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a  NVARIANTS:.**.**
e790: 20 7b 46 31 30 35 31 31 7d 20 54 68 65 20 73 71   {F10511} The sq
e7a0: 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29  lite3_complete()
e7b0: 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f 6d   and sqlite3_com
e7c0: 70 6c 65 74 65 31 36 28 29 20 66 75 6e 63 74 69  plete16() functi
e7d0: 6f 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ons.**          
e7e0: 72 65 74 75 72 6e 20 74 72 75 65 20 28 6e 6f 6e  return true (non
e7f0: 2d 7a 65 72 6f 29 20 69 66 20 61 6e 64 20 6f 6e  -zero) if and on
e800: 6c 79 20 69 66 20 74 68 65 20 6c 61 73 74 0a 2a  ly if the last.*
e810: 2a 20 20 20 20 20 20 20 20 20 20 6e 6f 6e 2d 77  *          non-w
e820: 68 69 74 65 73 70 61 63 65 20 74 6f 6b 65 6e 20  hitespace token 
e830: 69 6e 20 74 68 65 69 72 20 69 6e 70 75 74 20 69  in their input i
e840: 73 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 20 74 68  s a semicolon th
e850: 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69  at.**          i
e860: 73 20 6e 6f 74 20 69 6e 20 62 65 74 77 65 65 6e  s not in between
e870: 20 74 68 65 20 42 45 47 49 4e 20 61 6e 64 20 45   the BEGIN and E
e880: 4e 44 20 6f 66 20 61 20 43 52 45 41 54 45 20 54  ND of a CREATE T
e890: 52 49 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20  RIGGER.**       
e8a0: 20 20 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a     statement..**
e8b0: 0a 2a 2a 20 4c 49 4d 49 54 41 54 49 4f 4e 53 3a  .** LIMITATIONS:
e8c0: 0a 2a 2a 0a 2a 2a 20 7b 55 31 30 35 31 32 7d 20  .**.** {U10512} 
e8d0: 54 68 65 20 69 6e 70 75 74 20 74 6f 20 73 71 6c  The input to sql
e8e0: 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29 20  ite3_complete() 
e8f0: 6d 75 73 74 20 62 65 20 61 20 7a 65 72 6f 2d 74  must be a zero-t
e900: 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 20 20 20  erminated.**    
e910: 20 20 20 20 20 20 55 54 46 2d 38 20 73 74 72 69        UTF-8 stri
e920: 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 55 31 30 35 31  ng..**.** {U1051
e930: 33 7d 20 54 68 65 20 69 6e 70 75 74 20 74 6f 20  3} The input to 
e940: 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65  sqlite3_complete
e950: 31 36 28 29 20 6d 75 73 74 20 62 65 20 61 20 7a  16() must be a z
e960: 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a  ero-terminated.*
e970: 2a 20 20 20 20 20 20 20 20 20 20 55 54 46 2d 31  *          UTF-1
e980: 36 20 73 74 72 69 6e 67 20 69 6e 20 6e 61 74 69  6 string in nati
e990: 76 65 20 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a  ve byte order..*
e9a0: 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
e9b0: 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
e9c0: 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  e(const char *sq
e9d0: 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  l);.SQLITE_API i
e9e0: 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  nt sqlite3_compl
e9f0: 65 74 65 31 36 28 63 6f 6e 73 74 20 76 6f 69 64  ete16(const void
ea00: 20 2a 73 71 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43   *sql);../*.** C
ea10: 41 50 49 33 52 45 46 3a 20 52 65 67 69 73 74 65  API3REF: Registe
ea20: 72 20 41 20 43 61 6c 6c 62 61 63 6b 20 54 6f 20  r A Callback To 
ea30: 48 61 6e 64 6c 65 20 53 51 4c 49 54 45 5f 42 55  Handle SQLITE_BU
ea40: 53 59 20 45 72 72 6f 72 73 20 7b 46 31 32 33 31  SY Errors {F1231
ea50: 30 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  0}.**.** This ro
ea60: 75 74 69 6e 65 20 69 64 65 6e 74 69 66 69 65 73  utine identifies
ea70: 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63   a callback func
ea80: 74 69 6f 6e 20 74 68 61 74 20 6d 69 67 68 74 20  tion that might 
ea90: 62 65 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 77 68  be.** invoked wh
eaa0: 65 6e 65 76 65 72 20 61 6e 20 61 74 74 65 6d 70  enever an attemp
eab0: 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 70 65  t is made to ope
eac0: 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 61 62  n a database tab
ead0: 6c 65 20 0a 2a 2a 20 74 68 61 74 20 61 6e 6f 74  le .** that anot
eae0: 68 65 72 20 74 68 72 65 61 64 20 6f 72 20 70 72  her thread or pr
eaf0: 6f 63 65 73 73 20 68 61 73 20 6c 6f 63 6b 65 64  ocess has locked
eb00: 2e 0a 2a 2a 20 49 66 20 74 68 65 20 62 75 73 79  ..** If the busy
eb10: 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55 4c   callback is NUL
eb20: 4c 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f  L, then [SQLITE_
eb30: 42 55 53 59 5d 0a 2a 2a 20 6f 72 20 5b 53 51 4c  BUSY].** or [SQL
eb40: 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45  ITE_IOERR_BLOCKE
eb50: 44 5d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65  D].** is returne
eb60: 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 75 70  d immediately up
eb70: 6f 6e 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20  on encountering 
eb80: 74 68 65 20 6c 6f 63 6b 2e 0a 2a 2a 20 49 66 20  the lock..** If 
eb90: 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63  the busy callbac
eba0: 6b 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74  k is not NULL, t
ebb0: 68 65 6e 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 62  hen the.** callb
ebc0: 61 63 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f  ack will be invo
ebd0: 6b 65 64 20 77 69 74 68 20 74 77 6f 20 61 72 67  ked with two arg
ebe0: 75 6d 65 6e 74 73 2e 20 20 54 68 65 0a 2a 2a 20  uments.  The.** 
ebf0: 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  first argument t
ec00: 6f 20 74 68 65 20 68 61 6e 64 6c 65 72 20 69 73  o the handler is
ec10: 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76   a copy of the v
ec20: 6f 69 64 2a 20 70 6f 69 6e 74 65 72 20 77 68 69  oid* pointer whi
ec30: 63 68 0a 2a 2a 20 69 73 20 74 68 65 20 74 68 69  ch.** is the thi
ec40: 72 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  rd argument to t
ec50: 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 54 68  his routine.  Th
ec60: 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
ec70: 74 20 74 6f 0a 2a 2a 20 74 68 65 20 68 61 6e 64  t to.** the hand
ec80: 6c 65 72 20 69 73 20 74 68 65 20 6e 75 6d 62 65  ler is the numbe
ec90: 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20  r of times that 
eca0: 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72  the busy handler
ecb0: 20 68 61 73 0a 2a 2a 20 62 65 65 6e 20 69 6e 76   has.** been inv
ecc0: 6f 6b 65 64 20 66 6f 72 20 74 68 69 73 20 6c 6f  oked for this lo
ecd0: 63 6b 69 6e 67 20 65 76 65 6e 74 2e 20 20 20 49  cking event.   I
ece0: 66 20 74 68 65 0a 2a 2a 20 62 75 73 79 20 63 61  f the.** busy ca
ecf0: 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 30  llback returns 0
ed00: 2c 20 74 68 65 6e 20 6e 6f 20 61 64 64 69 74 69  , then no additi
ed10: 6f 6e 61 6c 20 61 74 74 65 6d 70 74 73 20 61 72  onal attempts ar
ed20: 65 20 6d 61 64 65 20 74 6f 0a 2a 2a 20 61 63 63  e made to.** acc
ed30: 65 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65  ess the database
ed40: 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 42 55 53   and [SQLITE_BUS
ed50: 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f  Y] or [SQLITE_IO
ed60: 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 20 69 73 20  ERR_BLOCKED] is 
ed70: 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 49 66 20  returned..** If 
ed80: 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74  the callback ret
ed90: 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  urns non-zero, t
eda0: 68 65 6e 20 61 6e 6f 74 68 65 72 20 61 74 74 65  hen another atte
edb0: 6d 70 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74  mpt.** is made t
edc0: 6f 20 6f 70 65 6e 20 74 68 65 20 64 61 74 61 62  o open the datab
edd0: 61 73 65 20 66 6f 72 20 72 65 61 64 69 6e 67 20  ase for reading 
ede0: 61 6e 64 20 74 68 65 20 63 79 63 6c 65 20 72 65  and the cycle re
edf0: 70 65 61 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  peats..**.** The
ee00: 20 70 72 65 73 65 6e 63 65 20 6f 66 20 61 20 62   presence of a b
ee10: 75 73 79 20 68 61 6e 64 6c 65 72 20 64 6f 65 73  usy handler does
ee20: 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 20 74   not guarantee t
ee30: 68 61 74 0a 2a 2a 20 69 74 20 77 69 6c 6c 20 62  hat.** it will b
ee40: 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 74  e invoked when t
ee50: 68 65 72 65 20 69 73 20 6c 6f 63 6b 20 63 6f 6e  here is lock con
ee60: 74 65 6e 74 69 6f 6e 2e 0a 2a 2a 20 49 66 20 53  tention..** If S
ee70: 51 4c 69 74 65 20 64 65 74 65 72 6d 69 6e 65 73  QLite determines
ee80: 20 74 68 61 74 20 69 6e 76 6f 6b 69 6e 67 20 74   that invoking t
ee90: 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20  he busy handler 
eea0: 63 6f 75 6c 64 20 72 65 73 75 6c 74 20 69 6e 0a  could result in.
eeb0: 2a 2a 20 61 20 64 65 61 64 6c 6f 63 6b 2c 20 69  ** a deadlock, i
eec0: 74 20 77 69 6c 6c 20 67 6f 20 61 68 65 61 64 20  t will go ahead 
eed0: 61 6e 64 20 72 65 74 75 72 6e 20 5b 53 51 4c 49  and return [SQLI
eee0: 54 45 5f 42 55 53 59 5d 20 6f 72 0a 2a 2a 20 5b  TE_BUSY] or.** [
eef0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f  SQLITE_IOERR_BLO
ef00: 43 4b 45 44 5d 20 69 6e 73 74 65 61 64 20 6f 66  CKED] instead of
ef10: 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 0a 2a 2a   invoking the.**
ef20: 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a 2a   busy handler..*
ef30: 2a 20 43 6f 6e 73 69 64 65 72 20 61 20 73 63 65  * Consider a sce
ef40: 6e 61 72 69 6f 20 77 68 65 72 65 20 6f 6e 65 20  nario where one 
ef50: 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 69  process is holdi
ef60: 6e 67 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 74  ng a read lock t
ef70: 68 61 74 0a 2a 2a 20 69 74 20 69 73 20 74 72 79  hat.** it is try
ef80: 69 6e 67 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74  ing to promote t
ef90: 6f 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63  o a reserved loc
efa0: 6b 20 61 6e 64 0a 2a 2a 20 61 20 73 65 63 6f 6e  k and.** a secon
efb0: 64 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c  d process is hol
efc0: 64 69 6e 67 20 61 20 72 65 73 65 72 76 65 64 20  ding a reserved 
efd0: 6c 6f 63 6b 20 74 68 61 74 20 69 74 20 69 73 20  lock that it is 
efe0: 74 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 70 72 6f  trying.** to pro
eff0: 6d 6f 74 65 20 74 6f 20 61 6e 20 65 78 63 6c 75  mote to an exclu
f000: 73 69 76 65 20 6c 6f 63 6b 2e 20 20 54 68 65 20  sive lock.  The 
f010: 66 69 72 73 74 20 70 72 6f 63 65 73 73 20 63 61  first process ca
f020: 6e 6e 6f 74 20 70 72 6f 63 65 65 64 0a 2a 2a 20  nnot proceed.** 
f030: 62 65 63 61 75 73 65 20 69 74 20 69 73 20 62 6c  because it is bl
f040: 6f 63 6b 65 64 20 62 79 20 74 68 65 20 73 65 63  ocked by the sec
f050: 6f 6e 64 20 61 6e 64 20 74 68 65 20 73 65 63 6f  ond and the seco
f060: 6e 64 20 70 72 6f 63 65 73 73 20 63 61 6e 6e 6f  nd process canno
f070: 74 0a 2a 2a 20 70 72 6f 63 65 65 64 20 62 65 63  t.** proceed bec
f080: 61 75 73 65 20 69 74 20 69 73 20 62 6c 6f 63 6b  ause it is block
f090: 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 2e  ed by the first.
f0a0: 20 20 49 66 20 62 6f 74 68 20 70 72 6f 63 65 73    If both proces
f0b0: 73 65 73 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68  ses.** invoke th
f0c0: 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 73 2c  e busy handlers,
f0d0: 20 6e 65 69 74 68 65 72 20 77 69 6c 6c 20 6d 61   neither will ma
f0e0: 6b 65 20 61 6e 79 20 70 72 6f 67 72 65 73 73 2e  ke any progress.
f0f0: 20 20 54 68 65 72 65 66 6f 72 65 2c 0a 2a 2a 20    Therefore,.** 
f100: 53 51 4c 69 74 65 20 72 65 74 75 72 6e 73 20 5b  SQLite returns [
f110: 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 66 6f 72  SQLITE_BUSY] for
f120: 20 74 68 65 20 66 69 72 73 74 20 70 72 6f 63 65   the first proce
f130: 73 73 2c 20 68 6f 70 69 6e 67 20 74 68 61 74 20  ss, hoping that 
f140: 74 68 69 73 0a 2a 2a 20 77 69 6c 6c 20 69 6e 64  this.** will ind
f150: 75 63 65 20 74 68 65 20 66 69 72 73 74 20 70 72  uce the first pr
f160: 6f 63 65 73 73 20 74 6f 20 72 65 6c 65 61 73 65  ocess to release
f170: 20 69 74 73 20 72 65 61 64 20 6c 6f 63 6b 20 61   its read lock a
f180: 6e 64 20 61 6c 6c 6f 77 0a 2a 2a 20 74 68 65 20  nd allow.** the 
f190: 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 20 74  second process t
f1a0: 6f 20 70 72 6f 63 65 65 64 2e 0a 2a 2a 0a 2a 2a  o proceed..**.**
f1b0: 20 54 68 65 20 64 65 66 61 75 6c 74 20 62 75 73   The default bus
f1c0: 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55  y callback is NU
f1d0: 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53  LL..**.** The [S
f1e0: 51 4c 49 54 45 5f 42 55 53 59 5d 20 65 72 72 6f  QLITE_BUSY] erro
f1f0: 72 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20 74  r is converted t
f200: 6f 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  o [SQLITE_IOERR_
f210: 42 4c 4f 43 4b 45 44 5d 0a 2a 2a 20 77 68 65 6e  BLOCKED].** when
f220: 20 53 51 4c 69 74 65 20 69 73 20 69 6e 20 74 68   SQLite is in th
f230: 65 20 6d 69 64 64 6c 65 20 6f 66 20 61 20 6c 61  e middle of a la
f240: 72 67 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  rge transaction 
f250: 77 68 65 72 65 20 61 6c 6c 20 74 68 65 0a 2a 2a  where all the.**
f260: 20 63 68 61 6e 67 65 73 20 77 69 6c 6c 20 6e 6f   changes will no
f270: 74 20 66 69 74 20 69 6e 74 6f 20 74 68 65 20 69  t fit into the i
f280: 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e 20  n-memory cache. 
f290: 20 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a 20   SQLite will.** 
f2a0: 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 52  already hold a R
f2b0: 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20  ESERVED lock on 
f2c0: 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
f2d0: 65 2c 20 62 75 74 20 69 74 20 6e 65 65 64 73 0a  e, but it needs.
f2e0: 2a 2a 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 68  ** to promote th
f2f0: 69 73 20 6c 6f 63 6b 20 74 6f 20 45 58 43 4c 55  is lock to EXCLU
f300: 53 49 56 45 20 73 6f 20 74 68 61 74 20 69 74 20  SIVE so that it 
f310: 63 61 6e 20 73 70 69 6c 6c 20 63 61 63 68 65 0a  can spill cache.
f320: 2a 2a 20 70 61 67 65 73 20 69 6e 74 6f 20 74 68  ** pages into th
f330: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
f340: 77 69 74 68 6f 75 74 20 68 61 72 6d 20 74 6f 20  without harm to 
f350: 63 6f 6e 63 75 72 72 65 6e 74 0a 2a 2a 20 72 65  concurrent.** re
f360: 61 64 65 72 73 2e 20 20 49 66 20 69 74 20 69 73  aders.  If it is
f370: 20 75 6e 61 62 6c 65 20 74 6f 20 70 72 6f 6d 6f   unable to promo
f380: 74 65 20 74 68 65 20 6c 6f 63 6b 2c 20 74 68 65  te the lock, the
f390: 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 0a  n the in-memory.
f3a0: 2a 2a 20 63 61 63 68 65 20 77 69 6c 6c 20 62 65  ** cache will be
f3b0: 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f   left in an inco
f3c0: 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 20 61  nsistent state a
f3d0: 6e 64 20 73 6f 20 74 68 65 20 65 72 72 6f 72 0a  nd so the error.
f3e0: 2a 2a 20 63 6f 64 65 20 69 73 20 70 72 6f 6d 6f  ** code is promo
f3f0: 74 65 64 20 66 72 6f 6d 20 74 68 65 20 72 65 6c  ted from the rel
f400: 61 74 69 76 65 6c 79 20 62 65 6e 69 67 6e 20 5b  atively benign [
f410: 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 74 6f 0a  SQLITE_BUSY] to.
f420: 2a 2a 20 74 68 65 20 6d 6f 72 65 20 73 65 76 65  ** the more seve
f430: 72 65 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52  re [SQLITE_IOERR
f440: 5f 42 4c 4f 43 4b 45 44 5d 2e 20 20 54 68 69 73  _BLOCKED].  This
f450: 20 65 72 72 6f 72 20 63 6f 64 65 20 70 72 6f 6d   error code prom
f460: 6f 74 69 6f 6e 0a 2a 2a 20 66 6f 72 63 65 73 20  otion.** forces 
f470: 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 6c  an automatic rol
f480: 6c 62 61 63 6b 20 6f 66 20 74 68 65 20 63 68 61  lback of the cha
f490: 6e 67 65 73 2e 20 20 53 65 65 20 74 68 65 0a 2a  nges.  See the.*
f4a0: 2a 20 3c 61 20 68 72 65 66 3d 22 68 74 74 70 3a  * <a href="http:
f4b0: 2f 2f 77 77 77 2e 73 71 6c 69 74 65 2e 6f 72 67  //www.sqlite.org
f4c0: 2f 63 76 73 74 72 61 63 2f 77 69 6b 69 3f 70 3d  /cvstrac/wiki?p=
f4d0: 43 6f 72 72 75 70 74 69 6f 6e 46 6f 6c 6c 6f 77  CorruptionFollow
f4e0: 69 6e 67 42 75 73 79 45 72 72 6f 72 22 3e 0a 2a  ingBusyError">.*
f4f0: 2a 20 43 6f 72 72 75 70 74 69 6f 6e 46 6f 6c 6c  * CorruptionFoll
f500: 6f 77 69 6e 67 42 75 73 79 45 72 72 6f 72 3c 2f  owingBusyError</
f510: 61 3e 20 77 69 6b 69 20 70 61 67 65 20 66 6f 72  a> wiki page for
f520: 20 61 20 64 69 73 63 75 73 73 69 6f 6e 20 6f 66   a discussion of
f530: 20 77 68 79 0a 2a 2a 20 74 68 69 73 20 69 73 20   why.** this is 
f540: 69 6d 70 6f 72 74 61 6e 74 2e 0a 2a 2a 09 0a 2a  important..**..*
f550: 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79  * There can only
f560: 20 62 65 20 61 20 73 69 6e 67 6c 65 20 62 75 73   be a single bus
f570: 79 20 68 61 6e 64 6c 65 72 20 64 65 66 69 6e 65  y handler define
f580: 64 20 66 6f 72 20 65 61 63 68 20 64 61 74 61 62  d for each datab
f590: 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f  ase.** connectio
f5a0: 6e 2e 20 20 53 65 74 74 69 6e 67 20 61 20 6e 65  n.  Setting a ne
f5b0: 77 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 63  w busy handler c
f5c0: 6c 65 61 72 73 20 61 6e 79 20 70 72 65 76 69 6f  lears any previo
f5d0: 75 73 20 6f 6e 65 2e 20 0a 2a 2a 20 4e 6f 74 65  us one. .** Note
f5e0: 20 74 68 61 74 20 63 61 6c 6c 69 6e 67 20 5b 73   that calling [s
f5f0: 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65  qlite3_busy_time
f600: 6f 75 74 28 29 5d 20 77 69 6c 6c 20 61 6c 73 6f  out()] will also
f610: 20 73 65 74 20 6f 72 20 63 6c 65 61 72 0a 2a 2a   set or clear.**
f620: 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65   the busy handle
f630: 72 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  r..**.** INVARIA
f640: 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 33  NTS:.**.** {F123
f650: 31 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  11} The [sqlite3
f660: 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 5d  _busy_handler()]
f670: 20 66 75 6e 63 74 69 6f 6e 20 72 65 70 6c 61 63   function replac
f680: 65 73 20 74 68 65 20 62 75 73 79 20 68 61 6e 64  es the busy hand
f690: 6c 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ler.**          
f6a0: 63 61 6c 6c 62 61 63 6b 20 69 6e 20 74 68 65 20  callback in the 
f6b0: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
f6c0: 69 6f 6e 20 69 64 65 6e 74 69 66 69 65 64 20 62  ion identified b
f6d0: 79 20 74 68 65 20 31 73 74 0a 2a 2a 20 20 20 20  y the 1st.**    
f6e0: 20 20 20 20 20 20 70 61 72 61 6d 65 74 65 72 20        parameter 
f6f0: 77 69 74 68 20 61 20 6e 65 77 20 62 75 73 79 20  with a new busy 
f700: 68 61 6e 64 6c 65 72 20 69 64 65 6e 74 69 66 69  handler identifi
f710: 65 64 20 62 79 20 74 68 65 20 32 6e 64 20 61 6e  ed by the 2nd an
f720: 64 20 33 72 64 0a 2a 2a 20 20 20 20 20 20 20 20  d 3rd.**        
f730: 20 20 70 61 72 61 6d 65 74 65 72 73 2e 0a 2a 2a    parameters..**
f740: 0a 2a 2a 20 7b 46 31 32 33 31 32 7d 20 54 68 65  .** {F12312} The
f750: 20 64 65 66 61 75 6c 74 20 62 75 73 79 20 68 61   default busy ha
f760: 6e 64 6c 65 72 20 66 6f 72 20 6e 65 77 20 64 61  ndler for new da
f770: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
f780: 6e 73 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  ns is NULL..**.*
f790: 2a 20 7b 46 31 32 33 31 34 7d 20 57 68 65 6e 20  * {F12314} When 
f7a0: 74 77 6f 20 6f 72 20 6d 6f 72 65 20 64 61 74 61  two or more data
f7b0: 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
f7c0: 73 68 61 72 65 20 61 20 63 6f 6d 6d 6f 6e 20 63  share a common c
f7d0: 61 63 68 65 2c 0a 2a 2a 20 20 20 20 20 20 20 20  ache,.**        
f7e0: 20 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c    the busy handl
f7f0: 65 72 20 66 6f 72 20 74 68 65 20 64 61 74 61 62  er for the datab
f800: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63  ase connection c
f810: 75 72 72 65 6e 74 6c 79 20 75 73 69 6e 67 0a 2a  urrently using.*
f820: 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 63  *          the c
f830: 61 63 68 65 20 69 73 20 69 6e 76 6f 6b 65 64 20  ache is invoked 
f840: 77 68 65 6e 20 74 68 65 20 63 61 63 68 65 20 65  when the cache e
f850: 6e 63 6f 75 6e 74 65 72 73 20 61 20 6c 6f 63 6b  ncounters a lock
f860: 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 33 31 36 7d  ..**.** {F12316}
f870: 20 49 66 20 61 20 62 75 73 79 20 68 61 6e 64 6c   If a busy handl
f880: 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75  er callback retu
f890: 72 6e 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74  rns zero, then t
f8a0: 68 65 20 53 51 4c 69 74 65 0a 2a 2a 20 20 20 20  he SQLite.**    
f8b0: 20 20 20 20 20 20 69 6e 74 65 72 66 61 63 65 20        interface 
f8c0: 74 68 61 74 20 70 72 6f 76 6f 6b 65 64 20 74 68  that provoked th
f8d0: 65 20 6c 6f 63 6b 69 6e 67 20 65 76 65 6e 74 20  e locking event 
f8e0: 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 20  will return.**  
f8f0: 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f          [SQLITE_
f900: 42 55 53 59 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31  BUSY]..**.** {F1
f910: 32 33 31 38 7d 20 53 51 4c 69 74 65 20 77 69 6c  2318} SQLite wil
f920: 6c 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 62 75  l invokes the bu
f930: 73 79 20 68 61 6e 64 6c 65 72 20 77 69 74 68 20  sy handler with 
f940: 74 77 6f 20 61 72 67 75 6d 65 6e 74 20 77 68 69  two argument whi
f950: 63 68 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61  ch.**          a
f960: 72 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  re a copy of the
f970: 20 70 6f 69 6e 74 65 72 20 73 75 70 70 6c 69 65   pointer supplie
f980: 64 20 62 79 20 74 68 65 20 33 72 64 20 70 61 72  d by the 3rd par
f990: 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 20 20 20  ameter to.**    
f9a0: 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 62        [sqlite3_b
f9b0: 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 5d 20 61  usy_handler()] a
f9c0: 6e 64 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68  nd a count of th
f9d0: 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 72 69 6f  e number of prio
f9e0: 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e  r.**          in
f9f0: 76 6f 63 61 74 69 6f 6e 73 20 6f 66 20 74 68 65  vocations of the
fa00: 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66 6f   busy handler fo
fa10: 72 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69  r the same locki
fa20: 6e 67 20 65 76 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  ng event..**.** 
fa30: 4c 49 4d 49 54 41 54 49 4f 4e 53 3a 0a 2a 2a 0a  LIMITATIONS:.**.
fa40: 2a 2a 20 7b 55 31 32 33 31 39 7d 20 41 20 62 75  ** {U12319} A bu
fa50: 73 79 20 68 61 6e 64 6c 65 72 20 73 68 6f 75 6c  sy handler shoul
fa60: 64 20 6e 6f 74 20 63 61 6c 6c 20 63 6c 6f 73 65  d not call close
fa70: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
fa80: 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20  nnection.**     
fa90: 20 20 20 20 20 6f 72 20 70 72 65 70 61 72 65 64       or prepared
faa0: 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
fab0: 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75 73 79  invoked the busy
fac0: 20 68 61 6e 64 6c 65 72 2e 0a 2a 2f 0a 53 51 4c   handler..*/.SQL
fad0: 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
fae0: 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72  te3_busy_handler
faf0: 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a  (sqlite3*, int(*
fb00: 29 28 76 6f 69 64 2a 2c 69 6e 74 29 2c 20 76 6f  )(void*,int), vo
fb10: 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  id*);../*.** CAP
fb20: 49 33 52 45 46 3a 20 53 65 74 20 41 20 42 75 73  I3REF: Set A Bus
fb30: 79 20 54 69 6d 65 6f 75 74 20 7b 46 31 32 33 34  y Timeout {F1234
fb40: 30 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  0}.**.** This ro
fb50: 75 74 69 6e 65 20 73 65 74 73 20 61 20 5b 73 71  utine sets a [sq
fb60: 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c  lite3_busy_handl
fb70: 65 72 20 7c 20 62 75 73 79 20 68 61 6e 64 6c 65  er | busy handle
fb80: 72 5d 0a 2a 2a 20 74 68 61 74 20 73 6c 65 65 70  r].** that sleep
fb90: 73 20 66 6f 72 20 61 20 77 68 69 6c 65 20 77 68  s for a while wh
fba0: 65 6e 20 61 0a 2a 2a 20 74 61 62 6c 65 20 69 73  en a.** table is
fbb0: 20 6c 6f 63 6b 65 64 2e 20 20 54 68 65 20 68 61   locked.  The ha
fbc0: 6e 64 6c 65 72 20 77 69 6c 6c 20 73 6c 65 65 70  ndler will sleep
fbd0: 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20   multiple times 
fbe0: 75 6e 74 69 6c 20 0a 2a 2a 20 61 74 20 6c 65 61  until .** at lea
fbf0: 73 74 20 22 6d 73 22 20 6d 69 6c 6c 69 73 65 63  st "ms" millisec
fc00: 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 69 6e 67  onds of sleeping
fc10: 20 68 61 76 65 20 62 65 65 6e 20 64 6f 6e 65 2e   have been done.
fc20: 20 7b 46 31 32 33 34 33 7d 20 41 66 74 65 72 0a   {F12343} After.
fc30: 2a 2a 20 22 6d 73 22 20 6d 69 6c 6c 69 73 65 63  ** "ms" millisec
fc40: 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 69 6e 67  onds of sleeping
fc50: 2c 20 74 68 65 20 68 61 6e 64 6c 65 72 20 72 65  , the handler re
fc60: 74 75 72 6e 73 20 30 20 77 68 69 63 68 0a 2a 2a  turns 0 which.**
fc70: 20 63 61 75 73 65 73 20 5b 73 71 6c 69 74 65 33   causes [sqlite3
fc80: 5f 73 74 65 70 28 29 5d 20 74 6f 20 72 65 74 75  _step()] to retu
fc90: 72 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d  rn [SQLITE_BUSY]
fca0: 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52   or [SQLITE_IOER
fcb0: 52 5f 42 4c 4f 43 4b 45 44 5d 2e 0a 2a 2a 0a 2a  R_BLOCKED]..**.*
fcc0: 2a 20 43 61 6c 6c 69 6e 67 20 74 68 69 73 20 72  * Calling this r
fcd0: 6f 75 74 69 6e 65 20 77 69 74 68 20 61 6e 20 61  outine with an a
fce0: 72 67 75 6d 65 6e 74 20 6c 65 73 73 20 74 68 61  rgument less tha
fcf0: 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a 65  n or equal to ze
fd00: 72 6f 0a 2a 2a 20 74 75 72 6e 73 20 6f 66 66 20  ro.** turns off 
fd10: 61 6c 6c 20 62 75 73 79 20 68 61 6e 64 6c 65 72  all busy handler
fd20: 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 63  s..**.** There c
fd30: 61 6e 20 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e  an only be a sin
fd40: 67 6c 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72  gle busy handler
fd50: 20 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61   for a particula
fd60: 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f  r database.** co
fd70: 6e 6e 65 63 74 69 6f 6e 2e 20 20 49 66 20 61 6e  nnection.  If an
fd80: 6f 74 68 65 72 20 62 75 73 79 20 68 61 6e 64 6c  other busy handl
fd90: 65 72 20 77 61 73 20 64 65 66 69 6e 65 64 20 20  er was defined  
fda0: 0a 2a 2a 20 28 75 73 69 6e 67 20 5b 73 71 6c 69  .** (using [sqli
fdb0: 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72  te3_busy_handler
fdc0: 28 29 5d 29 20 70 72 69 6f 72 20 74 6f 20 63 61  ()]) prior to ca
fdd0: 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 72 6f  lling.** this ro
fde0: 75 74 69 6e 65 2c 20 74 68 61 74 20 6f 74 68 65  utine, that othe
fdf0: 72 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 69  r busy handler i
fe00: 73 20 63 6c 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a  s cleared..**.**
fe10: 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a   INVARIANTS:.**.
fe20: 2a 2a 20 7b 46 31 32 33 34 31 7d 20 54 68 65 20  ** {F12341} The 
fe30: 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69  [sqlite3_busy_ti
fe40: 6d 65 6f 75 74 28 29 5d 20 66 75 6e 63 74 69 6f  meout()] functio
fe50: 6e 20 6f 76 65 72 72 69 64 65 73 20 61 6e 79 20  n overrides any 
fe60: 70 72 69 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20  prior.**        
fe70: 20 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f    [sqlite3_busy_
fe80: 74 69 6d 65 6f 75 74 28 29 5d 20 6f 72 20 5b 73  timeout()] or [s
fe90: 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64  qlite3_busy_hand
fea0: 6c 65 72 28 29 5d 20 73 65 74 74 69 6e 67 0a 2a  ler()] setting.*
feb0: 2a 20 20 20 20 20 20 20 20 20 20 6f 6e 20 74 68  *          on th
fec0: 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20  e same database 
fed0: 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  connection..**.*
fee0: 2a 20 7b 46 31 32 33 34 33 7d 20 49 66 20 74 68  * {F12343} If th
fef0: 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20  e 2nd parameter 
ff00: 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79  to [sqlite3_busy
ff10: 5f 74 69 6d 65 6f 75 74 28 29 5d 20 69 73 20 6c  _timeout()] is l
ff20: 65 73 73 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20  ess than.**     
ff30: 20 20 20 20 20 6f 72 20 65 71 75 61 6c 20 74 6f       or equal to
ff40: 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20   zero, then the 
ff50: 62 75 73 79 20 68 61 6e 64 6c 65 72 20 69 73 20  busy handler is 
ff60: 63 6c 65 61 72 65 64 20 73 6f 20 74 68 61 74 0a  cleared so that.
ff70: 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6c 6c 20  **          all 
ff80: 73 75 62 73 65 71 75 65 6e 74 20 6c 6f 63 6b 69  subsequent locki
ff90: 6e 67 20 65 76 65 6e 74 73 20 69 6d 6d 65 64 69  ng events immedi
ffa0: 61 74 65 6c 79 20 72 65 74 75 72 6e 20 5b 53 51  ately return [SQ
ffb0: 4c 49 54 45 5f 42 55 53 59 5d 2e 0a 2a 2a 0a 2a  LITE_BUSY]..**.*
ffc0: 2a 20 7b 46 31 32 33 34 34 7d 20 49 66 20 74 68  * {F12344} If th
ffd0: 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20  e 2nd parameter 
ffe0: 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79  to [sqlite3_busy
fff0: 5f 74 69 6d 65 6f 75 74 28 29 5d 20 69 73 20 61  _timeout()] is a
10000 20 70 6f 73 69 74 69 76 65 0a 2a 2a 20 20 20 20   positive.**    
10010 20 20 20 20 20 20 6e 75 6d 62 65 72 20 4e 2c 20        number N, 
10020 74 68 65 6e 20 61 20 62 75 73 79 20 68 61 6e 64  then a busy hand
10030 6c 65 72 20 69 73 20 73 65 74 20 74 68 61 74 20  ler is set that 
10040 72 65 70 65 61 74 65 64 6c 79 20 63 61 6c 6c 73  repeatedly calls
10050 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65  .**          the
10060 20 78 53 6c 65 65 70 28 29 20 6d 65 74 68 6f 64   xSleep() method
10070 20 69 6e 20 74 68 65 20 56 46 53 20 69 6e 74 65   in the VFS inte
10080 72 66 61 63 65 20 75 6e 74 69 6c 20 65 69 74 68  rface until eith
10090 65 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  er the.**       
100a0 20 20 20 6c 6f 63 6b 20 63 6c 65 61 72 73 20 6f     lock clears o
100b0 72 20 75 6e 74 69 6c 20 74 68 65 20 63 75 6d 75  r until the cumu
100c0 6c 61 74 69 76 65 20 73 6c 65 65 70 20 74 69 6d  lative sleep tim
100d0 65 20 72 65 70 6f 72 74 65 64 20 62 61 63 6b 0a  e reported back.
100e0 2a 2a 20 20 20 20 20 20 20 20 20 20 62 79 20 78  **          by x
100f0 53 6c 65 65 70 28 29 20 65 78 63 65 65 64 73 20  Sleep() exceeds 
10100 4e 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 2e 0a  N milliseconds..
10110 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
10120 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74  t sqlite3_busy_t
10130 69 6d 65 6f 75 74 28 73 71 6c 69 74 65 33 2a 2c  imeout(sqlite3*,
10140 20 69 6e 74 20 6d 73 29 3b 0a 0a 2f 2a 0a 2a 2a   int ms);../*.**
10150 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 76 65   CAPI3REF: Conve
10160 6e 69 65 6e 63 65 20 52 6f 75 74 69 6e 65 73 20  nience Routines 
10170 46 6f 72 20 52 75 6e 6e 69 6e 67 20 51 75 65 72  For Running Quer
10180 69 65 73 20 7b 46 31 32 33 37 30 7d 0a 2a 2a 0a  ies {F12370}.**.
10190 2a 2a 20 44 65 66 69 6e 69 74 69 6f 6e 3a 20 41  ** Definition: A
101a0 20 3c 62 3e 72 65 73 75 6c 74 20 74 61 62 6c 65   <b>result table
101b0 3c 2f 62 3e 20 69 73 20 6d 65 6d 6f 72 79 20 64  </b> is memory d
101c0 61 74 61 20 73 74 72 75 63 74 75 72 65 20 63 72  ata structure cr
101d0 65 61 74 65 64 20 62 79 20 74 68 65 0a 2a 2a 20  eated by the.** 
101e0 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62  [sqlite3_get_tab
101f0 6c 65 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e  le()] interface.
10200 20 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65    A result table
10210 20 72 65 63 6f 72 64 73 20 74 68 65 0a 2a 2a 20   records the.** 
10220 63 6f 6d 70 6c 65 74 65 20 71 75 65 72 79 20 72  complete query r
10230 65 73 75 6c 74 73 20 66 72 6f 6d 20 6f 6e 65 20  esults from one 
10240 6f 72 20 6d 6f 72 65 20 71 75 65 72 69 65 73 2e  or more queries.
10250 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65  .**.** The table
10260 20 63 6f 6e 63 65 70 74 75 61 6c 6c 79 20 68 61   conceptually ha
10270 73 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  s a number of ro
10280 77 73 20 61 6e 64 20 63 6f 6c 75 6d 6e 73 2e 20  ws and columns. 
10290 20 42 75 74 0a 2a 2a 20 74 68 65 73 65 20 6e 75   But.** these nu
102a0 6d 62 65 72 73 20 61 72 65 20 6e 6f 74 20 70 61  mbers are not pa
102b0 72 74 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  rt of the result
102c0 20 74 61 62 6c 65 20 69 74 73 65 6c 66 2e 20 20   table itself.  
102d0 54 68 65 73 65 0a 2a 2a 20 6e 75 6d 62 65 72 73  These.** numbers
102e0 20 61 72 65 20 6f 62 74 61 69 6e 65 64 20 73 65   are obtained se
102f0 70 61 72 61 74 65 6c 79 2e 20 20 4c 65 74 20 4e  parately.  Let N
10300 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   be the number o
10310 66 20 72 6f 77 73 0a 2a 2a 20 61 6e 64 20 4d 20  f rows.** and M 
10320 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  be the number of
10330 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 0a 2a 2a 20   columns..**.** 
10340 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 69  A result table i
10350 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 6f  s an array of po
10360 69 6e 74 65 72 73 20 74 6f 20 7a 65 72 6f 2d 74  inters to zero-t
10370 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 55 54 46  erminated.** UTF
10380 2d 38 20 73 74 72 69 6e 67 73 2e 20 20 54 68 65  -8 strings.  The
10390 72 65 20 61 72 65 20 28 4e 2b 31 29 2a 4d 20 65  re are (N+1)*M e
103a0 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 61  lements in the a
103b0 72 72 61 79 2e 20 20 0a 2a 2a 20 54 68 65 20 66  rray.  .** The f
103c0 69 72 73 74 20 4d 20 70 6f 69 6e 74 65 72 73 20  irst M pointers 
103d0 70 6f 69 6e 74 20 74 6f 20 7a 65 72 6f 2d 74 65  point to zero-te
103e0 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73  rminated strings
103f0 20 74 68 61 74 20 0a 2a 2a 20 63 6f 6e 74 61 69   that .** contai
10400 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 74  n the names of t
10410 68 65 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 20 54  he columns..** T
10420 68 65 20 72 65 6d 61 69 6e 69 6e 67 20 65 6e 74  he remaining ent
10430 72 69 65 73 20 61 6c 6c 20 70 6f 69 6e 74 20 74  ries all point t
10440 6f 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 2e  o query results.
10450 20 20 4e 55 4c 4c 0a 2a 2a 20 76 61 6c 75 65 73    NULL.** values
10460 20 61 72 65 20 67 69 76 65 20 61 20 4e 55 4c 4c   are give a NULL
10470 20 70 6f 69 6e 74 65 72 2e 20 20 41 6c 6c 20 6f   pointer.  All o
10480 74 68 65 72 20 76 61 6c 75 65 73 20 61 72 65 20  ther values are 
10490 69 6e 0a 2a 2a 20 74 68 65 69 72 20 55 54 46 2d  in.** their UTF-
104a0 38 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  8 zero-terminate
104b0 64 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65  d string represe
104c0 6e 74 61 74 69 6f 6e 20 61 73 20 72 65 74 75 72  ntation as retur
104d0 6e 65 64 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74  ned by.** [sqlit
104e0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29  e3_column_text()
104f0 5d 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73 75 6c  ]..**.** A resul
10500 74 20 74 61 62 6c 65 20 6d 69 67 68 74 20 63 6f  t table might co
10510 6e 73 69 73 74 73 20 6f 66 20 6f 6e 65 20 6f 72  nsists of one or
10520 20 6d 6f 72 65 20 6d 65 6d 6f 72 79 20 61 6c 6c   more memory all
10530 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2a 20 49 74 20  ocations..** It 
10540 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 70  is not safe to p
10550 61 73 73 20 61 20 72 65 73 75 6c 74 20 74 61 62  ass a result tab
10560 6c 65 20 64 69 72 65 63 74 6c 79 20 74 6f 20 5b  le directly to [
10570 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e  sqlite3_free()].
10580 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 74 61 62  .** A result tab
10590 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 64 65 61  le should be dea
105a0 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20 5b  llocated using [
105b0 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62  sqlite3_free_tab
105c0 6c 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 73 20  le()]..**.** As 
105d0 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 74 68  an example of th
105e0 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 66  e result table f
105f0 6f 72 6d 61 74 2c 20 73 75 70 70 6f 73 65 20 61  ormat, suppose a
10600 20 71 75 65 72 79 20 72 65 73 75 6c 74 0a 2a 2a   query result.**
10610 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a   is as follows:.
10620 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74  **.** <blockquot
10630 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 20  e><pre>.**      
10640 20 20 4e 61 6d 65 20 20 20 20 20 20 20 20 7c 20    Name        | 
10650 41 67 65 0a 2a 2a 20 20 20 20 20 20 20 20 2d 2d  Age.**        --
10660 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
10670 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 20 20  -----.**        
10680 41 6c 69 63 65 20 20 20 20 20 20 20 7c 20 34 33  Alice       | 43
10690 0a 2a 2a 20 20 20 20 20 20 20 20 42 6f 62 20 20  .**        Bob  
106a0 20 20 20 20 20 20 20 7c 20 32 38 0a 2a 2a 20 20         | 28.**  
106b0 20 20 20 20 20 20 43 69 6e 64 79 20 20 20 20 20        Cindy     
106c0 20 20 7c 20 32 31 0a 2a 2a 20 3c 2f 70 72 65 3e    | 21.** </pre>
106d0 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a  </blockquote>.**
106e0 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 77  .** There are tw
106f0 6f 20 63 6f 6c 75 6d 6e 20 28 4d 3d 3d 32 29 20  o column (M==2) 
10700 61 6e 64 20 74 68 72 65 65 20 72 6f 77 73 20 28  and three rows (
10710 4e 3d 3d 33 29 2e 20 20 54 68 75 73 20 74 68 65  N==3).  Thus the
10720 0a 2a 2a 20 72 65 73 75 6c 74 20 74 61 62 6c 65  .** result table
10730 20 68 61 73 20 38 20 65 6e 74 72 69 65 73 2e 20   has 8 entries. 
10740 20 53 75 70 70 6f 73 65 20 74 68 65 20 72 65 73   Suppose the res
10750 75 6c 74 20 74 61 62 6c 65 20 69 73 20 73 74 6f  ult table is sto
10760 72 65 64 0a 2a 2a 20 69 6e 20 61 6e 20 61 72 72  red.** in an arr
10770 61 79 20 6e 61 6d 65 73 20 61 7a 52 65 73 75 6c  ay names azResul
10780 74 2e 20 20 54 68 65 6e 20 61 7a 52 65 73 75 6c  t.  Then azResul
10790 74 20 68 6f 6c 64 73 20 74 68 69 73 20 63 6f 6e  t holds this con
107a0 74 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f  tent:.**.** <blo
107b0 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a  ckquote><pre>.**
107c0 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74          azResult
107d0 26 23 39 31 3b 30 5d 20 3d 20 22 4e 61 6d 65 22  &#91;0] = "Name"
107e0 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65  ;.**        azRe
107f0 73 75 6c 74 26 23 39 31 3b 31 5d 20 3d 20 22 41  sult&#91;1] = "A
10800 67 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61  ge";.**        a
10810 7a 52 65 73 75 6c 74 26 23 39 31 3b 32 5d 20 3d  zResult&#91;2] =
10820 20 22 41 6c 69 63 65 22 3b 0a 2a 2a 20 20 20 20   "Alice";.**    
10830 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31      azResult&#91
10840 3b 33 5d 20 3d 20 22 34 33 22 3b 0a 2a 2a 20 20  ;3] = "43";.**  
10850 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23        azResult&#
10860 39 31 3b 34 5d 20 3d 20 22 42 6f 62 22 3b 0a 2a  91;4] = "Bob";.*
10870 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c  *        azResul
10880 74 26 23 39 31 3b 35 5d 20 3d 20 22 32 38 22 3b  t&#91;5] = "28";
10890 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73  .**        azRes
108a0 75 6c 74 26 23 39 31 3b 36 5d 20 3d 20 22 43 69  ult&#91;6] = "Ci
108b0 6e 64 79 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20  ndy";.**        
108c0 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 37 5d 20  azResult&#91;7] 
108d0 3d 20 22 32 31 22 3b 0a 2a 2a 20 3c 2f 70 72 65  = "21";.** </pre
108e0 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a  ></blockquote>.*
108f0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
10900 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 66 75 6e  _get_table() fun
10910 63 74 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 20  ction evaluates 
10920 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 73  one or more.** s
10930 65 6d 69 63 6f 6c 6f 6e 2d 73 65 70 61 72 61 74  emicolon-separat
10940 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ed SQL statement
10950 73 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d 74 65  s in the zero-te
10960 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 0a 2a  rminated UTF-8.*
10970 2a 20 73 74 72 69 6e 67 20 6f 66 20 69 74 73 20  * string of its 
10980 32 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20  2nd parameter.  
10990 49 74 20 72 65 74 75 72 6e 73 20 61 20 72 65 73  It returns a res
109a0 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 74 68 65  ult table to the
109b0 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 67 69 76 65  .** pointer give
109c0 6e 20 69 6e 20 69 74 73 20 33 72 64 20 70 61 72  n in its 3rd par
109d0 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 66  ameter..**.** Af
109e0 74 65 72 20 74 68 65 20 63 61 6c 6c 69 6e 67 20  ter the calling 
109f0 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 66 69 6e  function has fin
10a00 69 73 68 65 64 20 75 73 69 6e 67 20 74 68 65 20  ished using the 
10a10 72 65 73 75 6c 74 2c 20 69 74 20 73 68 6f 75 6c  result, it shoul
10a20 64 20 0a 2a 2a 20 70 61 73 73 20 74 68 65 20 70  d .** pass the p
10a30 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 65  ointer to the re
10a40 73 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 73 71  sult table to sq
10a50 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65  lite3_free_table
10a60 28 29 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 0a  () in order to .
10a70 2a 2a 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d  ** release the m
10a80 65 6d 6f 72 79 20 74 68 61 74 20 77 61 73 20 6d  emory that was m
10a90 61 6c 6c 6f 63 2d 65 64 2e 20 20 42 65 63 61 75  alloc-ed.  Becau
10aa0 73 65 20 6f 66 20 74 68 65 20 77 61 79 20 74 68  se of the way th
10ab0 65 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d  e .** [sqlite3_m
10ac0 61 6c 6c 6f 63 28 29 5d 20 68 61 70 70 65 6e 73  alloc()] happens
10ad0 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65 33 5f   within sqlite3_
10ae0 67 65 74 5f 74 61 62 6c 65 28 29 2c 20 74 68 65  get_table(), the
10af0 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63   calling.** func
10b00 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 74 72  tion must not tr
10b10 79 20 74 6f 20 63 61 6c 6c 20 5b 73 71 6c 69 74  y to call [sqlit
10b20 65 33 5f 66 72 65 65 28 29 5d 20 64 69 72 65 63  e3_free()] direc
10b30 74 6c 79 2e 20 20 4f 6e 6c 79 20 0a 2a 2a 20 5b  tly.  Only .** [
10b40 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62  sqlite3_free_tab
10b50 6c 65 28 29 5d 20 69 73 20 61 62 6c 65 20 74 6f  le()] is able to
10b60 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d 65 6d   release the mem
10b70 6f 72 79 20 70 72 6f 70 65 72 6c 79 20 61 6e 64  ory properly and
10b80 20 73 61 66 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54   safely..**.** T
10b90 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  he sqlite3_get_t
10ba0 61 62 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65  able() interface
10bb0 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20   is implemented 
10bc0 61 73 20 61 20 77 72 61 70 70 65 72 20 61 72 6f  as a wrapper aro
10bd0 75 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  und.** [sqlite3_
10be0 65 78 65 63 28 29 5d 2e 20 20 54 68 65 20 73 71  exec()].  The sq
10bf0 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28  lite3_get_table(
10c00 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e  ) routine does n
10c10 6f 74 20 68 61 76 65 20 61 63 63 65 73 73 0a 2a  ot have access.*
10c20 2a 20 74 6f 20 61 6e 79 20 69 6e 74 65 72 6e 61  * to any interna
10c30 6c 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65  l data structure
10c40 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 49 74  s of SQLite.  It
10c50 20 75 73 65 73 20 6f 6e 6c 79 20 74 68 65 20 70   uses only the p
10c60 75 62 6c 69 63 0a 2a 2a 20 69 6e 74 65 72 66 61  ublic.** interfa
10c70 63 65 20 64 65 66 69 6e 65 64 20 68 65 72 65 2e  ce defined here.
10c80 20 20 41 73 20 61 20 63 6f 6e 73 65 71 75 65 6e    As a consequen
10c90 63 65 2c 20 65 72 72 6f 72 73 20 74 68 61 74 20  ce, errors that 
10ca0 6f 63 63 75 72 20 69 6e 20 74 68 65 0a 2a 2a 20  occur in the.** 
10cb0 77 72 61 70 70 65 72 20 6c 61 79 65 72 20 6f 75  wrapper layer ou
10cc0 74 73 69 64 65 20 6f 66 20 74 68 65 20 69 6e 74  tside of the int
10cd0 65 72 6e 61 6c 20 5b 73 71 6c 69 74 65 33 5f 65  ernal [sqlite3_e
10ce0 78 65 63 28 29 5d 20 63 61 6c 6c 20 61 72 65 20  xec()] call are 
10cf0 6e 6f 74 0a 2a 2a 20 72 65 66 6c 65 63 74 65 64  not.** reflected
10d00 20 69 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63   in subsequent c
10d10 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33  alls to [sqlite3
10d20 5f 65 72 72 63 6f 64 65 28 29 5d 20 6f 72 0a 2a  _errcode()] or.*
10d30 2a 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  * [sqlite3_errms
10d40 67 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41  g()]..**.** INVA
10d50 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46  RIANTS:.**.** {F
10d60 31 32 33 37 31 7d 20 49 66 20 61 20 5b 73 71 6c  12371} If a [sql
10d70 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29  ite3_get_table()
10d80 5d 20 66 61 69 6c 73 20 61 20 6d 65 6d 6f 72 79  ] fails a memory
10d90 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 74 68 65   allocation, the
10da0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 74  n.**          it
10db0 20 66 72 65 65 73 20 74 68 65 20 72 65 73 75 6c   frees the resul
10dc0 74 20 74 61 62 6c 65 20 75 6e 64 65 72 20 63 6f  t table under co
10dd0 6e 73 74 72 75 63 74 69 6f 6e 2c 20 61 62 6f 72  nstruction, abor
10de0 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  ts the.**       
10df0 20 20 20 71 75 65 72 79 20 69 6e 20 70 72 6f 63     query in proc
10e00 65 73 73 2c 20 73 6b 69 70 73 20 61 6e 79 20 73  ess, skips any s
10e10 75 62 73 65 71 75 65 6e 74 20 71 75 65 72 69 65  ubsequent querie
10e20 73 2c 20 73 65 74 73 20 74 68 65 0a 2a 2a 20 20  s, sets the.**  
10e30 20 20 20 20 20 20 20 20 2a 72 65 73 75 6c 74 70          *resultp
10e40 20 6f 75 74 70 75 74 20 70 6f 69 6e 74 65 72 20   output pointer 
10e50 74 6f 20 4e 55 4c 4c 20 61 6e 64 20 72 65 74 75  to NULL and retu
10e60 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45  rns [SQLITE_NOME
10e70 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 33 37  M]..**.** {F1237
10e80 33 7d 20 49 66 20 74 68 65 20 6e 63 6f 6c 75 6d  3} If the ncolum
10e90 6e 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b  n parameter to [
10ea0 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
10eb0 65 28 29 5d 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  e()] is not NULL
10ec0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65  .**          the
10ed0 6e 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  n [sqlite3_get_t
10ee0 61 62 6c 65 28 29 5d 20 77 72 69 74 65 20 74 68  able()] write th
10ef0 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
10f00 6d 6e 73 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20  mns in the.**   
10f10 20 20 20 20 20 20 20 72 65 73 75 6c 74 20 73 65         result se
10f20 74 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 69  t of the query i
10f30 6e 74 6f 20 2a 6e 63 6f 6c 75 6d 6e 20 69 66 20  nto *ncolumn if 
10f40 74 68 65 20 71 75 65 72 79 20 69 73 0a 2a 2a 20  the query is.** 
10f50 20 20 20 20 20 20 20 20 20 73 75 63 63 65 73 73           success
10f60 66 75 6c 20 28 69 66 20 74 68 65 20 66 75 6e 63  ful (if the func
10f70 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c  tion returns SQL
10f80 49 54 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a 2a 20 7b  ITE_OK)..**.** {
10f90 46 31 32 33 37 34 7d 20 49 66 20 74 68 65 20 6e  F12374} If the n
10fa0 72 6f 77 20 70 61 72 61 6d 65 74 65 72 20 74 6f  row parameter to
10fb0 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61   [sqlite3_get_ta
10fc0 62 6c 65 28 29 5d 20 69 73 20 6e 6f 74 20 4e 55  ble()] is not NU
10fd0 4c 4c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  LL.**          t
10fe0 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 67 65 74  hen [sqlite3_get
10ff0 5f 74 61 62 6c 65 28 29 5d 20 77 72 69 74 65 20  _table()] write 
11000 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  the number of ro
11010 77 73 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20  ws in the.**    
11020 20 20 20 20 20 20 72 65 73 75 6c 74 20 73 65 74        result set
11030 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 69 6e   of the query in
11040 74 6f 20 2a 6e 72 6f 77 20 69 66 20 74 68 65 20  to *nrow if the 
11050 71 75 65 72 79 20 69 73 0a 2a 2a 20 20 20 20 20  query is.**     
11060 20 20 20 20 20 73 75 63 63 65 73 73 66 75 6c 20       successful 
11070 28 69 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  (if the function
11080 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
11090 4f 4b 29 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 33  OK)..**.** {F123
110a0 37 36 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  76} The [sqlite3
110b0 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 20 66 75  _get_table()] fu
110c0 6e 63 74 69 6f 6e 20 73 65 74 73 20 69 74 73 20  nction sets its 
110d0 2a 6e 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 0a 2a  *ncolumn value.*
110e0 2a 20 20 20 20 20 20 20 20 20 20 74 6f 20 74 68  *          to th
110f0 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
11100 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c  mns in the resul
11110 74 20 73 65 74 20 6f 66 20 74 68 65 20 71 75 65  t set of the que
11120 72 79 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20  ry in the.**    
11130 20 20 20 20 20 20 73 71 6c 20 70 61 72 61 6d 65        sql parame
11140 74 65 72 2c 20 6f 72 20 74 6f 20 7a 65 72 6f 20  ter, or to zero 
11150 69 66 20 74 68 65 20 71 75 65 72 79 20 69 6e 20  if the query in 
11160 73 71 6c 20 68 61 73 20 61 6e 20 65 6d 70 74 79  sql has an empty
11170 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 73  .**          res
11180 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49  ult set..*/.SQLI
11190 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
111a0 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 0a 20 20  e3_get_table(.  
111b0 73 71 6c 69 74 65 33 2a 2c 20 20 20 20 20 20 20  sqlite3*,       
111c0 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65 6e        /* An open
111d0 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 63   database */.  c
111e0 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 2c 20  onst char *sql, 
111f0 20 20 20 20 20 2f 2a 20 53 51 4c 20 74 6f 20 62       /* SQL to b
11200 65 20 65 76 61 6c 75 61 74 65 64 20 2a 2f 0a 20  e evaluated */. 
11210 20 63 68 61 72 20 2a 2a 2a 70 52 65 73 75 6c 74   char ***pResult
11220 2c 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74  ,      /* Result
11230 73 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 2a  s of the query *
11240 2f 0a 20 20 69 6e 74 20 2a 6e 72 6f 77 2c 20 20  /.  int *nrow,  
11250 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
11260 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 72 6f  ber of result ro
11270 77 73 20 77 72 69 74 74 65 6e 20 68 65 72 65 20  ws written here 
11280 2a 2f 0a 20 20 69 6e 74 20 2a 6e 63 6f 6c 75 6d  */.  int *ncolum
11290 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  n,         /* Nu
112a0 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63  mber of result c
112b0 6f 6c 75 6d 6e 73 20 77 72 69 74 74 65 6e 20 68  olumns written h
112c0 65 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  ere */.  char **
112d0 65 72 72 6d 73 67 20 20 20 20 20 20 20 20 20 2f  errmsg         /
112e0 2a 20 45 72 72 6f 72 20 6d 73 67 20 77 72 69 74  * Error msg writ
112f0 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 3b 0a 53  ten here */.);.S
11300 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
11310 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c  qlite3_free_tabl
11320 65 28 63 68 61 72 20 2a 2a 72 65 73 75 6c 74 29  e(char **result)
11330 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
11340 46 3a 20 46 6f 72 6d 61 74 74 65 64 20 53 74 72  F: Formatted Str
11350 69 6e 67 20 50 72 69 6e 74 69 6e 67 20 46 75 6e  ing Printing Fun
11360 63 74 69 6f 6e 73 20 7b 46 31 37 34 30 30 7d 0a  ctions {F17400}.
11370 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  **.** These rout
11380 69 6e 65 73 20 61 72 65 20 77 6f 72 6b 61 6c 69  ines are workali
11390 6b 65 73 20 6f 66 20 74 68 65 20 22 70 72 69 6e  kes of the "prin
113a0 74 66 28 29 22 20 66 61 6d 69 6c 79 20 6f 66 20  tf()" family of 
113b0 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 66 72 6f  functions.** fro
113c0 6d 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43  m the standard C
113d0 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20   library..**.** 
113e0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  The sqlite3_mpri
113f0 6e 74 66 28 29 20 61 6e 64 20 73 71 6c 69 74 65  ntf() and sqlite
11400 33 5f 76 6d 70 72 69 6e 74 66 28 29 20 72 6f 75  3_vmprintf() rou
11410 74 69 6e 65 73 20 77 72 69 74 65 20 74 68 65 69  tines write thei
11420 72 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e 74  r.** results int
11430 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  o memory obtaine
11440 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f  d from [sqlite3_
11450 6d 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2a 20 54 68  malloc()]..** Th
11460 65 20 73 74 72 69 6e 67 73 20 72 65 74 75 72 6e  e strings return
11470 65 64 20 62 79 20 74 68 65 73 65 20 74 77 6f 20  ed by these two 
11480 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c 64 20  routines should 
11490 62 65 0a 2a 2a 20 72 65 6c 65 61 73 65 64 20 62  be.** released b
114a0 79 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28  y [sqlite3_free(
114b0 29 5d 2e 20 20 20 42 6f 74 68 20 72 6f 75 74 69  )].   Both routi
114c0 6e 65 73 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20  nes return a.** 
114d0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 66 20  NULL pointer if 
114e0 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
114f0 29 5d 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20  )] is unable to 
11500 61 6c 6c 6f 63 61 74 65 20 65 6e 6f 75 67 68 0a  allocate enough.
11510 2a 2a 20 6d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c  ** memory to hol
11520 64 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20  d the resulting 
11530 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e  string..**.** In
11540 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
11550 66 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 73  f() routine is s
11560 69 6d 69 6c 61 72 20 74 6f 20 22 73 6e 70 72 69  imilar to "snpri
11570 6e 74 66 28 29 22 20 66 72 6f 6d 0a 2a 2a 20 74  ntf()" from.** t
11580 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69  he standard C li
11590 62 72 61 72 79 2e 20 20 54 68 65 20 72 65 73 75  brary.  The resu
115a0 6c 74 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e  lt is written in
115b0 74 6f 20 74 68 65 0a 2a 2a 20 62 75 66 66 65 72  to the.** buffer
115c0 20 73 75 70 70 6c 69 65 64 20 61 73 20 74 68 65   supplied as the
115d0 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
115e0 72 20 77 68 6f 73 65 20 73 69 7a 65 20 69 73 20  r whose size is 
115f0 67 69 76 65 6e 20 62 79 0a 2a 2a 20 74 68 65 20  given by.** the 
11600 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e  first parameter.
11610 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 6f   Note that the o
11620 72 64 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 66  rder of the.** f
11630 69 72 73 74 20 74 77 6f 20 70 61 72 61 6d 65 74  irst two paramet
11640 65 72 73 20 69 73 20 72 65 76 65 72 73 65 64 20  ers is reversed 
11650 66 72 6f 6d 20 73 6e 70 72 69 6e 74 66 28 29 2e  from snprintf().
11660 20 20 54 68 69 73 20 69 73 20 61 6e 0a 2a 2a 20    This is an.** 
11670 68 69 73 74 6f 72 69 63 61 6c 20 61 63 63 69 64  historical accid
11680 65 6e 74 20 74 68 61 74 20 63 61 6e 6e 6f 74 20  ent that cannot 
11690 62 65 20 66 69 78 65 64 20 77 69 74 68 6f 75 74  be fixed without
116a0 20 62 72 65 61 6b 69 6e 67 0a 2a 2a 20 62 61 63   breaking.** bac
116b0 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69  kwards compatibi
116c0 6c 69 74 79 2e 20 20 4e 6f 74 65 20 61 6c 73 6f  lity.  Note also
116d0 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 73 6e   that sqlite3_sn
116e0 70 72 69 6e 74 66 28 29 0a 2a 2a 20 72 65 74 75  printf().** retu
116f0 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  rns a pointer to
11700 20 69 74 73 20 62 75 66 66 65 72 20 69 6e 73 74   its buffer inst
11710 65 61 64 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  ead of the numbe
11720 72 20 6f 66 0a 2a 2a 20 63 68 61 72 61 63 74 65  r of.** characte
11730 72 73 20 61 63 74 75 61 6c 6c 79 20 77 72 69 74  rs actually writ
11740 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 62 75 66  ten into the buf
11750 66 65 72 2e 20 20 57 65 20 61 64 6d 69 74 20 74  fer.  We admit t
11760 68 61 74 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65  hat.** the numbe
11770 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  r of characters 
11780 77 72 69 74 74 65 6e 20 77 6f 75 6c 64 20 62 65  written would be
11790 20 61 20 6d 6f 72 65 20 75 73 65 66 75 6c 20 72   a more useful r
117a0 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 62  eturn.** value b
117b0 75 74 20 77 65 20 63 61 6e 6e 6f 74 20 63 68 61  ut we cannot cha
117c0 6e 67 65 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  nge the implemen
117d0 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65  tation of sqlite
117e0 33 5f 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20  3_snprintf().** 
117f0 6e 6f 77 20 77 69 74 68 6f 75 74 20 62 72 65 61  now without brea
11800 6b 69 6e 67 20 63 6f 6d 70 61 74 69 62 69 6c 69  king compatibili
11810 74 79 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 6c 6f 6e  ty..**.** As lon
11820 67 20 61 73 20 74 68 65 20 62 75 66 66 65 72 20  g as the buffer 
11830 73 69 7a 65 20 69 73 20 67 72 65 61 74 65 72 20  size is greater 
11840 74 68 61 6e 20 7a 65 72 6f 2c 20 73 71 6c 69 74  than zero, sqlit
11850 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a  e3_snprintf().**
11860 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 74   guarantees that
11870 20 74 68 65 20 62 75 66 66 65 72 20 69 73 20 61   the buffer is a
11880 6c 77 61 79 73 20 7a 65 72 6f 2d 74 65 72 6d 69  lways zero-termi
11890 6e 61 74 65 64 2e 20 20 54 68 65 20 66 69 72 73  nated.  The firs
118a0 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 22  t.** parameter "
118b0 6e 22 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20  n" is the total 
118c0 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75 66 66  size of the buff
118d0 65 72 2c 20 69 6e 63 6c 75 64 69 6e 67 20 73 70  er, including sp
118e0 61 63 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 7a  ace for.** the z
118f0 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e 20  ero terminator. 
11900 20 53 6f 20 74 68 65 20 6c 6f 6e 67 65 73 74 20   So the longest 
11910 73 74 72 69 6e 67 20 74 68 61 74 20 63 61 6e 20  string that can 
11920 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a  be completely.**
11930 20 77 72 69 74 74 65 6e 20 77 69 6c 6c 20 62 65   written will be
11940 20 6e 2d 31 20 63 68 61 72 61 63 74 65 72 73 2e   n-1 characters.
11950 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
11960 74 69 6e 65 73 20 61 6c 6c 20 69 6d 70 6c 65 6d  tines all implem
11970 65 6e 74 20 73 6f 6d 65 20 61 64 64 69 74 69 6f  ent some additio
11980 6e 61 6c 20 66 6f 72 6d 61 74 74 69 6e 67 0a 2a  nal formatting.*
11990 2a 20 6f 70 74 69 6f 6e 73 20 74 68 61 74 20 61  * options that a
119a0 72 65 20 75 73 65 66 75 6c 20 66 6f 72 20 63 6f  re useful for co
119b0 6e 73 74 72 75 63 74 69 6e 67 20 53 51 4c 20 73  nstructing SQL s
119c0 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 41 6c  tatements..** Al
119d0 6c 20 6f 66 20 74 68 65 20 75 73 75 61 6c 20 70  l of the usual p
119e0 72 69 6e 74 66 20 66 6f 72 6d 61 74 74 69 6e 67  rintf formatting
119f0 20 6f 70 74 69 6f 6e 73 20 61 70 70 6c 79 2e 20   options apply. 
11a00 20 49 6e 20 61 64 64 69 74 69 6f 6e 2c 20 74 68   In addition, th
11a10 65 72 65 0a 2a 2a 20 69 73 20 61 72 65 20 22 25  ere.** is are "%
11a20 71 22 2c 20 22 25 51 22 2c 20 61 6e 64 20 22 25  q", "%Q", and "%
11a30 7a 22 20 6f 70 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  z" options..**.*
11a40 2a 20 54 68 65 20 25 71 20 6f 70 74 69 6f 6e 20  * The %q option 
11a50 77 6f 72 6b 73 20 6c 69 6b 65 20 25 73 20 69 6e  works like %s in
11a60 20 74 68 61 74 20 69 74 20 73 75 62 73 74 69 74   that it substit
11a70 75 74 65 73 20 61 20 6e 75 6c 6c 2d 74 65 72 6d  utes a null-term
11a80 69 6e 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67  inated.** string
11a90 20 66 72 6f 6d 20 74 68 65 20 61 72 67 75 6d 65   from the argume
11aa0 6e 74 20 6c 69 73 74 2e 20 20 42 75 74 20 25 71  nt list.  But %q
11ab0 20 61 6c 73 6f 20 64 6f 75 62 6c 65 73 20 65 76   also doubles ev
11ac0 65 72 79 20 27 5c 27 27 20 63 68 61 72 61 63 74  ery '\'' charact
11ad0 65 72 2e 0a 2a 2a 20 25 71 20 69 73 20 64 65 73  er..** %q is des
11ae0 69 67 6e 65 64 20 66 6f 72 20 75 73 65 20 69 6e  igned for use in
11af0 73 69 64 65 20 61 20 73 74 72 69 6e 67 20 6c 69  side a string li
11b00 74 65 72 61 6c 2e 20 20 42 79 20 64 6f 75 62 6c  teral.  By doubl
11b10 69 6e 67 20 65 61 63 68 20 27 5c 27 27 0a 2a 2a  ing each '\''.**
11b20 20 63 68 61 72 61 63 74 65 72 20 69 74 20 65 73   character it es
11b30 63 61 70 65 73 20 74 68 61 74 20 63 68 61 72 61  capes that chara
11b40 63 74 65 72 20 61 6e 64 20 61 6c 6c 6f 77 73 20  cter and allows 
11b50 69 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65  it to be inserte
11b60 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 73 74  d into.** the st
11b70 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  ring..**.** For 
11b80 65 78 61 6d 70 6c 65 2c 20 73 6f 20 73 6f 6d 65  example, so some
11b90 20 73 74 72 69 6e 67 20 76 61 72 69 61 62 6c 65   string variable
11ba0 20 63 6f 6e 74 61 69 6e 73 20 74 65 78 74 20 61   contains text a
11bb0 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
11bc0 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72   <blockquote><pr
11bd0 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 54 65  e>.**  char *zTe
11be0 78 74 20 3d 20 22 49 74 27 73 20 61 20 68 61 70  xt = "It's a hap
11bf0 70 79 20 64 61 79 21 22 3b 0a 2a 2a 20 3c 2f 70  py day!";.** </p
11c00 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e  re></blockquote>
11c10 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 63 61 6e 20 75  .**.** One can u
11c20 73 65 20 74 68 69 73 20 74 65 78 74 20 69 6e 20  se this text in 
11c30 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  an SQL statement
11c40 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
11c50 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c  ** <blockquote><
11c60 70 72 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a  pre>.**  char *z
11c70 53 51 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  SQL = sqlite3_mp
11c80 72 69 6e 74 66 28 22 49 4e 53 45 52 54 20 49 4e  rintf("INSERT IN
11c90 54 4f 20 74 61 62 6c 65 20 56 41 4c 55 45 53 28  TO table VALUES(
11ca0 27 25 71 27 29 22 2c 20 7a 54 65 78 74 29 3b 0a  '%q')", zText);.
11cb0 2a 2a 20 20 73 71 6c 69 74 65 33 5f 65 78 65 63  **  sqlite3_exec
11cc0 28 64 62 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 2c  (db, zSQL, 0, 0,
11cd0 20 30 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33   0);.**  sqlite3
11ce0 5f 66 72 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20  _free(zSQL);.** 
11cf0 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f  </pre></blockquo
11d00 74 65 3e 0a 2a 2a 0a 2a 2a 20 42 65 63 61 75 73  te>.**.** Becaus
11d10 65 20 74 68 65 20 25 71 20 66 6f 72 6d 61 74 20  e the %q format 
11d20 73 74 72 69 6e 67 20 69 73 20 75 73 65 64 2c 20  string is used, 
11d30 74 68 65 20 27 5c 27 27 20 63 68 61 72 61 63 74  the '\'' charact
11d40 65 72 20 69 6e 20 7a 54 65 78 74 0a 2a 2a 20 69  er in zText.** i
11d50 73 20 65 73 63 61 70 65 64 20 61 6e 64 20 74 68  s escaped and th
11d60 65 20 53 51 4c 20 67 65 6e 65 72 61 74 65 64 20  e SQL generated 
11d70 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  is as follows:.*
11d80 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65  *.** <blockquote
11d90 3e 3c 70 72 65 3e 0a 2a 2a 20 20 49 4e 53 45 52  ><pre>.**  INSER
11da0 54 20 49 4e 54 4f 20 74 61 62 6c 65 31 20 56 41  T INTO table1 VA
11db0 4c 55 45 53 28 27 49 74 27 27 73 20 61 20 68 61  LUES('It''s a ha
11dc0 70 70 79 20 64 61 79 21 27 29 0a 2a 2a 20 3c 2f  ppy day!').** </
11dd0 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65  pre></blockquote
11de0 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  >.**.** This is 
11df0 63 6f 72 72 65 63 74 2e 20 20 48 61 64 20 77 65  correct.  Had we
11e00 20 75 73 65 64 20 25 73 20 69 6e 73 74 65 61 64   used %s instead
11e10 20 6f 66 20 25 71 2c 20 74 68 65 20 67 65 6e 65   of %q, the gene
11e20 72 61 74 65 64 20 53 51 4c 0a 2a 2a 20 77 6f 75  rated SQL.** wou
11e30 6c 64 20 68 61 76 65 20 6c 6f 6f 6b 65 64 20 6c  ld have looked l
11e40 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
11e50 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65  <blockquote><pre
11e60 3e 0a 2a 2a 20 20 49 4e 53 45 52 54 20 49 4e 54  >.**  INSERT INT
11e70 4f 20 74 61 62 6c 65 31 20 56 41 4c 55 45 53 28  O table1 VALUES(
11e80 27 49 74 27 73 20 61 20 68 61 70 70 79 20 64 61  'It's a happy da
11e90 79 21 27 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c  y!');.** </pre><
11ea0 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a  /blockquote>.**.
11eb0 2a 2a 20 54 68 69 73 20 73 65 63 6f 6e 64 20 65  ** This second e
11ec0 78 61 6d 70 6c 65 20 69 73 20 61 6e 20 53 51 4c  xample is an SQL
11ed0 20 73 79 6e 74 61 78 20 65 72 72 6f 72 2e 20 20   syntax error.  
11ee0 41 73 20 61 20 67 65 6e 65 72 61 6c 20 72 75 6c  As a general rul
11ef0 65 20 79 6f 75 0a 2a 2a 20 73 68 6f 75 6c 64 20  e you.** should 
11f00 61 6c 77 61 79 73 20 75 73 65 20 25 71 20 69 6e  always use %q in
11f10 73 74 65 61 64 20 6f 66 20 25 73 20 77 68 65 6e  stead of %s when
11f20 20 69 6e 73 65 72 74 69 6e 67 20 74 65 78 74 20   inserting text 
11f30 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 0a 2a  into a string .*
11f40 2a 20 6c 69 74 65 72 61 6c 2e 0a 2a 2a 0a 2a 2a  * literal..**.**
11f50 20 54 68 65 20 25 51 20 6f 70 74 69 6f 6e 20 77   The %Q option w
11f60 6f 72 6b 73 20 6c 69 6b 65 20 25 71 20 65 78 63  orks like %q exc
11f70 65 70 74 20 69 74 20 61 6c 73 6f 20 61 64 64 73  ept it also adds
11f80 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 73 20 61   single quotes a
11f90 72 6f 75 6e 64 0a 2a 2a 20 74 68 65 20 6f 75 74  round.** the out
11fa0 73 69 64 65 20 6f 66 20 74 68 65 20 74 6f 74 61  side of the tota
11fb0 6c 20 73 74 72 69 6e 67 2e 20 20 4f 72 20 69 66  l string.  Or if
11fc0 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69   the parameter i
11fd0 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a  n the argument.*
11fe0 2a 20 6c 69 73 74 20 69 73 20 61 20 4e 55 4c 4c  * list is a NULL
11ff0 20 70 6f 69 6e 74 65 72 2c 20 25 51 20 73 75 62   pointer, %Q sub
12000 73 74 69 74 75 74 65 73 20 74 68 65 20 74 65 78  stitutes the tex
12010 74 20 22 4e 55 4c 4c 22 20 28 77 69 74 68 6f 75  t "NULL" (withou
12020 74 20 73 69 6e 67 6c 65 0a 2a 2a 20 71 75 6f 74  t single.** quot
12030 65 73 29 20 69 6e 20 70 6c 61 63 65 20 6f 66 20  es) in place of 
12040 74 68 65 20 25 51 20 6f 70 74 69 6f 6e 2e 20 7b  the %Q option. {
12050 45 4e 44 7d 20 20 53 6f 2c 20 66 6f 72 20 65 78  END}  So, for ex
12060 61 6d 70 6c 65 2c 20 6f 6e 65 20 63 6f 75 6c 64  ample, one could
12070 20 73 61 79 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f   say:.**.** <blo
12080 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a  ckquote><pre>.**
12090 20 20 63 68 61 72 20 2a 7a 53 51 4c 20 3d 20 73    char *zSQL = s
120a0 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
120b0 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c  INSERT INTO tabl
120c0 65 20 56 41 4c 55 45 53 28 25 51 29 22 2c 20 7a  e VALUES(%Q)", z
120d0 54 65 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74  Text);.**  sqlit
120e0 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 51 4c  e3_exec(db, zSQL
120f0 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20  , 0, 0, 0);.**  
12100 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 51  sqlite3_free(zSQ
12110 4c 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62  L);.** </pre></b
12120 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a  lockquote>.**.**
12130 20 54 68 65 20 63 6f 64 65 20 61 62 6f 76 65 20   The code above 
12140 77 69 6c 6c 20 72 65 6e 64 65 72 20 61 20 63 6f  will render a co
12150 72 72 65 63 74 20 53 51 4c 20 73 74 61 74 65 6d  rrect SQL statem
12160 65 6e 74 20 69 6e 20 74 68 65 20 7a 53 51 4c 0a  ent in the zSQL.
12170 2a 2a 20 76 61 72 69 61 62 6c 65 20 65 76 65 6e  ** variable even
12180 20 69 66 20 74 68 65 20 7a 54 65 78 74 20 76 61   if the zText va
12190 72 69 61 62 6c 65 20 69 73 20 61 20 4e 55 4c 4c  riable is a NULL
121a0 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   pointer..**.** 
121b0 54 68 65 20 22 25 7a 22 20 66 6f 72 6d 61 74 74  The "%z" formatt
121c0 69 6e 67 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73  ing option works
121d0 20 65 78 61 63 74 6c 79 20 6c 69 6b 65 20 22 25   exactly like "%
121e0 73 22 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 61  s" with the.** a
121f0 64 64 69 74 69 6f 6e 20 74 68 61 74 20 61 66 74  ddition that aft
12200 65 72 20 74 68 65 20 73 74 72 69 6e 67 20 68 61  er the string ha
12210 73 20 62 65 65 6e 20 72 65 61 64 20 61 6e 64 20  s been read and 
12220 63 6f 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 74  copied into.** t
12230 68 65 20 72 65 73 75 6c 74 2c 20 5b 73 71 6c 69  he result, [sqli
12240 74 65 33 5f 66 72 65 65 28 29 5d 20 69 73 20 63  te3_free()] is c
12250 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 69 6e 70  alled on the inp
12260 75 74 20 73 74 72 69 6e 67 2e 20 7b 45 4e 44 7d  ut string. {END}
12270 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54  .**.** INVARIANT
12280 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31 37 34 30 33  S:.**.** {F17403
12290 7d 20 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  }  The [sqlite3_
122a0 6d 70 72 69 6e 74 66 28 29 5d 20 61 6e 64 20 5b  mprintf()] and [
122b0 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66  sqlite3_vmprintf
122c0 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 0a 2a  ()] interfaces.*
122d0 2a 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75  *           retu
122e0 72 6e 20 65 69 74 68 65 72 20 70 6f 69 6e 74 65  rn either pointe
122f0 72 73 20 74 6f 20 7a 65 72 6f 2d 74 65 72 6d 69  rs to zero-termi
12300 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 69  nated UTF-8 stri
12310 6e 67 73 20 68 65 6c 64 20 69 6e 0a 2a 2a 20 20  ngs held in.**  
12320 20 20 20 20 20 20 20 20 20 6d 65 6d 6f 72 79 20           memory 
12330 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73  obtained from [s
12340 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
12350 20 6f 72 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72   or NULL pointer
12360 73 20 69 66 0a 2a 2a 20 20 20 20 20 20 20 20 20  s if.**         
12370 20 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c    a call to [sql
12380 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 66  ite3_malloc()] f
12390 61 69 6c 73 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 37  ails..**.** {F17
123a0 34 30 36 7d 20 20 54 68 65 20 5b 73 71 6c 69 74  406}  The [sqlit
123b0 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 5d 20 69  e3_snprintf()] i
123c0 6e 74 65 72 66 61 63 65 20 77 72 69 74 65 73 20  nterface writes 
123d0 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  a zero-terminate
123e0 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 55  d.**           U
123f0 54 46 2d 38 20 73 74 72 69 6e 67 20 69 6e 74 6f  TF-8 string into
12400 20 74 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e   the buffer poin
12410 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 73 65  ted to by the se
12420 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 0a 2a  cond parameter.*
12430 2a 20 20 20 20 20 20 20 20 20 20 20 70 72 6f 76  *           prov
12440 69 64 65 64 20 74 68 61 74 20 74 68 65 20 66 69  ided that the fi
12450 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 73  rst parameter is
12460 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65   greater than ze
12470 72 6f 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 37 34 30  ro..**.** {F1740
12480 37 7d 20 20 54 68 65 20 5b 73 71 6c 69 74 65 33  7}  The [sqlite3
12490 5f 73 6e 70 72 69 6e 74 66 28 29 5d 20 69 6e 74  _snprintf()] int
124a0 65 72 66 61 63 65 20 64 6f 65 73 20 6e 6f 74 20  erface does not 
124b0 77 72 69 74 65 73 20 73 6c 6f 74 73 20 6f 66 0a  writes slots of.
124c0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69 74 73  **           its
124d0 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 28   output buffer (
124e0 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  the second param
124f0 65 74 65 72 29 20 6f 75 74 73 69 64 65 20 74 68  eter) outside th
12500 65 20 72 61 6e 67 65 0a 2a 2a 20 20 20 20 20 20  e range.**      
12510 20 20 20 20 20 6f 66 20 30 20 74 68 72 6f 75 67       of 0 throug
12520 68 20 4e 2d 31 20 28 77 68 65 72 65 20 4e 20 69  h N-1 (where N i
12530 73 20 74 68 65 20 66 69 72 73 74 20 70 61 72 61  s the first para
12540 6d 65 74 65 72 29 0a 2a 2a 20 20 20 20 20 20 20  meter).**       
12550 20 20 20 20 72 65 67 61 72 64 6c 65 73 73 20 6f      regardless o
12560 66 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20  f the length of 
12570 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 20 20  the string.**   
12580 20 20 20 20 20 20 20 20 72 65 71 75 65 73 74 65          requeste
12590 64 20 62 79 20 74 68 65 20 66 6f 72 6d 61 74 20  d by the format 
125a0 73 70 65 63 69 66 69 63 61 74 69 6f 6e 2e 0a 2a  specification..*
125b0 2a 20 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  *   .*/.SQLITE_A
125c0 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  PI char *sqlite3
125d0 5f 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 63  _mprintf(const c
125e0 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54  har*,...);.SQLIT
125f0 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69  E_API char *sqli
12600 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 63 6f 6e  te3_vmprintf(con
12610 73 74 20 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73  st char*, va_lis
12620 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  t);.SQLITE_API c
12630 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 6e 70  har *sqlite3_snp
12640 72 69 6e 74 66 28 69 6e 74 2c 63 68 61 72 2a 2c  rintf(int,char*,
12650 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e  const char*, ...
12660 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
12670 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63  EF: Memory Alloc
12680 61 74 69 6f 6e 20 53 75 62 73 79 73 74 65 6d 20  ation Subsystem 
12690 7b 46 31 37 33 30 30 7d 0a 2a 2a 0a 2a 2a 20 54  {F17300}.**.** T
126a0 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 20  he SQLite core  
126b0 75 73 65 73 20 74 68 65 73 65 20 74 68 72 65 65  uses these three
126c0 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 61 6c   routines for al
126d0 6c 20 6f 66 20 69 74 73 20 6f 77 6e 0a 2a 2a 20  l of its own.** 
126e0 69 6e 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20  internal memory 
126f0 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73  allocation needs
12700 2e 20 22 43 6f 72 65 22 20 69 6e 20 74 68 65 20  . "Core" in the 
12710 70 72 65 76 69 6f 75 73 20 73 65 6e 74 65 6e 63  previous sentenc
12720 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 69 6e  e.** does not in
12730 63 6c 75 64 65 20 6f 70 65 72 61 74 69 6e 67 2d  clude operating-
12740 73 79 73 74 65 6d 20 73 70 65 63 69 66 69 63 20  system specific 
12750 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  VFS implementati
12760 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 77 69 6e 64  on.  The.** wind
12770 6f 77 73 20 56 46 53 20 75 73 65 73 20 6e 61 74  ows VFS uses nat
12780 69 76 65 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 66  ive malloc and f
12790 72 65 65 20 66 6f 72 20 73 6f 6d 65 20 6f 70 65  ree for some ope
127a0 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54  rations..**.** T
127b0 68 65 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  he sqlite3_mallo
127c0 63 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74 75  c() routine retu
127d0 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  rns a pointer to
127e0 20 61 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 6d   a block.** of m
127f0 65 6d 6f 72 79 20 61 74 20 6c 65 61 73 74 20 4e  emory at least N
12800 20 62 79 74 65 73 20 69 6e 20 6c 65 6e 67 74 68   bytes in length
12810 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65  , where N is the
12820 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 49   parameter..** I
12830 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  f sqlite3_malloc
12840 28 29 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20  () is unable to 
12850 6f 62 74 61 69 6e 20 73 75 66 66 69 63 69 65 6e  obtain sufficien
12860 74 20 66 72 65 65 0a 2a 2a 20 6d 65 6d 6f 72 79  t free.** memory
12870 2c 20 69 74 20 72 65 74 75 72 6e 73 20 61 20 4e  , it returns a N
12880 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 20 49 66  ULL pointer.  If
12890 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 4e   the parameter N
128a0 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d   to.** sqlite3_m
128b0 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65 72 6f 20  alloc() is zero 
128c0 6f 72 20 6e 65 67 61 74 69 76 65 20 74 68 65 6e  or negative then
128d0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
128e0 29 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 20 4e  ) returns.** a N
128f0 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a  ULL pointer..**.
12900 2a 2a 20 43 61 6c 6c 69 6e 67 20 73 71 6c 69 74  ** Calling sqlit
12910 65 33 5f 66 72 65 65 28 29 20 77 69 74 68 20 61  e3_free() with a
12920 20 70 6f 69 6e 74 65 72 20 70 72 65 76 69 6f 75   pointer previou
12930 73 6c 79 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20  sly returned.** 
12940 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  by sqlite3_mallo
12950 63 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 72  c() or sqlite3_r
12960 65 61 6c 6c 6f 63 28 29 20 72 65 6c 65 61 73 65  ealloc() release
12970 73 20 74 68 61 74 20 6d 65 6d 6f 72 79 20 73 6f  s that memory so
12980 0a 2a 2a 20 74 68 61 74 20 69 74 20 6d 69 67 68  .** that it migh
12990 74 20 62 65 20 72 65 75 73 65 64 2e 20 20 54 68  t be reused.  Th
129a0 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  e sqlite3_free()
129b0 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a 2a 20 61   routine is.** a
129c0 20 6e 6f 2d 6f 70 20 69 66 20 69 73 20 63 61 6c   no-op if is cal
129d0 6c 65 64 20 77 69 74 68 20 61 20 4e 55 4c 4c 20  led with a NULL 
129e0 70 6f 69 6e 74 65 72 2e 20 20 50 61 73 73 69 6e  pointer.  Passin
129f0 67 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  g a NULL pointer
12a00 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 66  .** to sqlite3_f
12a10 72 65 65 28 29 20 69 73 20 68 61 72 6d 6c 65 73  ree() is harmles
12a20 73 2e 20 20 41 66 74 65 72 20 62 65 69 6e 67 20  s.  After being 
12a30 66 72 65 65 64 2c 20 6d 65 6d 6f 72 79 0a 2a 2a  freed, memory.**
12a40 20 73 68 6f 75 6c 64 20 6e 65 69 74 68 65 72 20   should neither 
12a50 62 65 20 72 65 61 64 20 6e 6f 72 20 77 72 69 74  be read nor writ
12a60 74 65 6e 2e 20 20 45 76 65 6e 20 72 65 61 64 69  ten.  Even readi
12a70 6e 67 20 70 72 65 76 69 6f 75 73 6c 79 20 66 72  ng previously fr
12a80 65 65 64 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6d 69  eed.** memory mi
12a90 67 68 74 20 72 65 73 75 6c 74 20 69 6e 20 61 20  ght result in a 
12aa0 73 65 67 6d 65 6e 74 61 74 69 6f 6e 20 66 61 75  segmentation fau
12ab0 6c 74 20 6f 72 20 6f 74 68 65 72 20 73 65 76 65  lt or other seve
12ac0 72 65 20 65 72 72 6f 72 2e 0a 2a 2a 20 4d 65 6d  re error..** Mem
12ad0 6f 72 79 20 63 6f 72 72 75 70 74 69 6f 6e 2c 20  ory corruption, 
12ae0 61 20 73 65 67 6d 65 6e 74 61 74 69 6f 6e 20 66  a segmentation f
12af0 61 75 6c 74 2c 20 6f 72 20 6f 74 68 65 72 20 73  ault, or other s
12b00 65 76 65 72 65 20 65 72 72 6f 72 0a 2a 2a 20 6d  evere error.** m
12b10 69 67 68 74 20 72 65 73 75 6c 74 20 69 66 20 73  ight result if s
12b20 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69 73  qlite3_free() is
12b30 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 6e   called with a n
12b40 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20  on-NULL pointer 
12b50 74 68 61 74 0a 2a 2a 20 77 61 73 20 6e 6f 74 20  that.** was not 
12b60 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
12b70 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 6f  lite3_malloc() o
12b80 72 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  r sqlite3_free()
12b90 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
12ba0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 69 6e  te3_realloc() in
12bb0 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73  terface attempts
12bc0 20 74 6f 20 72 65 73 69 7a 65 20 61 0a 2a 2a 20   to resize a.** 
12bd0 70 72 69 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c  prior memory all
12be0 6f 63 61 74 69 6f 6e 20 74 6f 20 62 65 20 61 74  ocation to be at
12bf0 20 6c 65 61 73 74 20 4e 20 62 79 74 65 73 2c 20   least N bytes, 
12c00 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a  where N is the.*
12c10 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  * second paramet
12c20 65 72 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20  er.  The memory 
12c30 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 62 65  allocation to be
12c40 20 72 65 73 69 7a 65 64 20 69 73 20 74 68 65 20   resized is the 
12c50 66 69 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74  first.** paramet
12c60 65 72 2e 20 20 49 66 20 74 68 65 20 66 69 72 73  er.  If the firs
12c70 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73  t parameter to s
12c80 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29  qlite3_realloc()
12c90 0a 2a 2a 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f  .** is a NULL po
12ca0 69 6e 74 65 72 20 74 68 65 6e 20 69 74 73 20 62  inter then its b
12cb0 65 68 61 76 69 6f 72 20 69 73 20 69 64 65 6e 74  ehavior is ident
12cc0 69 63 61 6c 20 74 6f 20 63 61 6c 6c 69 6e 67 0a  ical to calling.
12cd0 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  ** sqlite3_mallo
12ce0 63 28 4e 29 20 77 68 65 72 65 20 4e 20 69 73 20  c(N) where N is 
12cf0 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  the second param
12d00 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f  eter to sqlite3_
12d10 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 49 66  realloc()..** If
12d20 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61   the second para
12d30 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33  meter to sqlite3
12d40 5f 72 65 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65  _realloc() is ze
12d50 72 6f 20 6f 72 0a 2a 2a 20 6e 65 67 61 74 69 76  ro or.** negativ
12d60 65 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76  e then the behav
12d70 69 6f 72 20 69 73 20 65 78 61 63 74 6c 79 20 74  ior is exactly t
12d80 68 65 20 73 61 6d 65 20 61 73 20 63 61 6c 6c 69  he same as calli
12d90 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 72  ng.** sqlite3_fr
12da0 65 65 28 50 29 20 77 68 65 72 65 20 50 20 69 73  ee(P) where P is
12db0 20 74 68 65 20 66 69 72 73 74 20 70 61 72 61 6d   the first param
12dc0 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f  eter to sqlite3_
12dd0 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 53 71  realloc()..** Sq
12de0 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20  lite3_realloc() 
12df0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
12e00 72 20 74 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c  r to a memory al
12e10 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 61  location.** of a
12e20 74 20 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20  t least N bytes 
12e30 69 6e 20 73 69 7a 65 20 6f 72 20 4e 55 4c 4c 20  in size or NULL 
12e40 69 66 20 73 75 66 66 69 63 69 65 6e 74 20 6d 65  if sufficient me
12e50 6d 6f 72 79 20 69 73 20 75 6e 61 76 61 69 6c 61  mory is unavaila
12e60 62 6c 65 2e 0a 2a 2a 20 49 66 20 4d 20 69 73 20  ble..** If M is 
12e70 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
12e80 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e  prior allocation
12e90 2c 20 74 68 65 6e 20 6d 69 6e 28 4e 2c 4d 29 20  , then min(N,M) 
12ea0 62 79 74 65 73 0a 2a 2a 20 6f 66 20 74 68 65 20  bytes.** of the 
12eb0 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e  prior allocation
12ec0 20 61 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f   are copied into
12ed0 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
12ee0 66 20 62 75 66 66 65 72 20 72 65 74 75 72 6e 65  f buffer returne
12ef0 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 5f  d.** by sqlite3_
12f00 72 65 61 6c 6c 6f 63 28 29 20 61 6e 64 20 74 68  realloc() and th
12f10 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69  e prior allocati
12f20 6f 6e 20 69 73 20 66 72 65 65 64 2e 0a 2a 2a 20  on is freed..** 
12f30 49 66 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  If sqlite3_reall
12f40 6f 63 28 29 20 72 65 74 75 72 6e 73 20 4e 55 4c  oc() returns NUL
12f50 4c 2c 20 74 68 65 6e 20 74 68 65 20 70 72 69 6f  L, then the prio
12f60 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20  r allocation.** 
12f70 69 73 20 6e 6f 74 20 66 72 65 65 64 2e 0a 2a 2a  is not freed..**
12f80 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 72  .** The memory r
12f90 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74  eturned by sqlit
12fa0 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20  e3_malloc() and 
12fb0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
12fc0 29 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61  ).** is always a
12fd0 6c 69 67 6e 65 64 20 74 6f 20 61 74 20 6c 65 61  ligned to at lea
12fe0 73 74 20 61 6e 20 38 20 62 79 74 65 20 62 6f 75  st an 8 byte bou
12ff0 6e 64 61 72 79 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a  ndary. {END}.**.
13000 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 69  ** The default i
13010 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a  mplementation.**
13020 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   of the memory a
13030 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73  llocation subsys
13040 74 65 6d 20 75 73 65 73 20 74 68 65 20 6d 61 6c  tem uses the mal
13050 6c 6f 63 28 29 2c 20 72 65 61 6c 6c 6f 63 28 29  loc(), realloc()
13060 0a 2a 2a 20 61 6e 64 20 66 72 65 65 28 29 20 70  .** and free() p
13070 72 6f 76 69 64 65 64 20 62 79 20 74 68 65 20 73  rovided by the s
13080 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72  tandard C librar
13090 79 2e 20 7b 46 31 37 33 38 32 7d 20 48 6f 77 65  y. {F17382} Howe
130a0 76 65 72 2c 20 69 66 20 0a 2a 2a 20 53 51 4c 69  ver, if .** SQLi
130b0 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77  te is compiled w
130c0 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ith the followin
130d0 67 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f 72  g C preprocessor
130e0 20 6d 61 63 72 6f 0a 2a 2a 0a 2a 2a 20 3c 62 6c   macro.**.** <bl
130f0 6f 63 6b 71 75 6f 74 65 3e 20 53 51 4c 49 54 45  ockquote> SQLITE
13100 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 3d 3c 69 3e  _MEMORY_SIZE=<i>
13110 4e 4e 4e 3c 2f 69 3e 20 3c 2f 62 6c 6f 63 6b 71  NNN</i> </blockq
13120 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 77 68 65 72  uote>.**.** wher
13130 65 20 3c 69 3e 4e 4e 4e 3c 2f 69 3e 20 69 73 20  e <i>NNN</i> is 
13140 61 6e 20 69 6e 74 65 67 65 72 2c 20 74 68 65 6e  an integer, then
13150 20 53 51 4c 69 74 65 20 63 72 65 61 74 65 20 61   SQLite create a
13160 20 73 74 61 74 69 63 0a 2a 2a 20 61 72 72 61 79   static.** array
13170 20 6f 66 20 61 74 20 6c 65 61 73 74 20 3c 69 3e   of at least <i>
13180 4e 4e 4e 3c 2f 69 3e 20 62 79 74 65 73 20 69 6e  NNN</i> bytes in
13190 20 73 69 7a 65 20 61 6e 64 20 75 73 65 20 74 68   size and use th
131a0 61 74 20 61 72 72 61 79 0a 2a 2a 20 66 6f 72 20  at array.** for 
131b0 61 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d  all of its dynam
131c0 69 63 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ic memory alloca
131d0 74 69 6f 6e 20 6e 65 65 64 73 2e 20 7b 45 4e 44  tion needs. {END
131e0 7d 20 20 41 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a  }  Additional.**
131f0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
13200 72 20 6f 70 74 69 6f 6e 73 20 6d 61 79 20 62 65  r options may be
13210 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65   added in future
13220 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a   releases..**.**
13230 20 49 6e 20 53 51 4c 69 74 65 20 76 65 72 73 69   In SQLite versi
13240 6f 6e 20 33 2e 35 2e 30 20 61 6e 64 20 33 2e 35  on 3.5.0 and 3.5
13250 2e 31 2c 20 69 74 20 77 61 73 20 70 6f 73 73 69  .1, it was possi
13260 62 6c 65 20 74 6f 20 64 65 66 69 6e 65 0a 2a 2a  ble to define.**
13270 20 74 68 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54   the SQLITE_OMIT
13280 5f 4d 45 4d 4f 52 59 5f 41 4c 4c 4f 43 41 54 49  _MEMORY_ALLOCATI
13290 4f 4e 20 77 68 69 63 68 20 77 6f 75 6c 64 20 63  ON which would c
132a0 61 75 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69  ause the built-i
132b0 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  n.** implementat
132c0 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 72 6f 75  ion of these rou
132d0 74 69 6e 65 73 20 74 6f 20 62 65 20 6f 6d 69 74  tines to be omit
132e0 74 65 64 2e 20 20 54 68 61 74 20 63 61 70 61 62  ted.  That capab
132f0 69 6c 69 74 79 0a 2a 2a 20 69 73 20 6e 6f 20 6c  ility.** is no l
13300 6f 6e 67 65 72 20 70 72 6f 76 69 64 65 64 2e 20  onger provided. 
13310 20 4f 6e 6c 79 20 62 75 69 6c 74 2d 69 6e 20 6d   Only built-in m
13320 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 73  emory allocators
13330 20 63 61 6e 20 62 65 0a 2a 2a 20 75 73 65 64 2e   can be.** used.
13340 0a 2a 2a 0a 2a 2a 20 54 68 65 20 77 69 6e 64 6f  .**.** The windo
13350 77 73 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20  ws OS interface 
13360 6c 61 79 65 72 20 63 61 6c 6c 73 0a 2a 2a 20 74  layer calls.** t
13370 68 65 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63  he system malloc
13380 28 29 20 61 6e 64 20 66 72 65 65 28 29 20 64 69  () and free() di
13390 72 65 63 74 6c 79 20 77 68 65 6e 20 63 6f 6e 76  rectly when conv
133a0 65 72 74 69 6e 67 0a 2a 2a 20 66 69 6c 65 6e 61  erting.** filena
133b0 6d 65 73 20 62 65 74 77 65 65 6e 20 74 68 65 20  mes between the 
133c0 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 75  UTF-8 encoding u
133d0 73 65 64 20 62 79 20 53 51 4c 69 74 65 0a 2a 2a  sed by SQLite.**
133e0 20 61 6e 64 20 77 68 61 74 65 76 65 72 20 66 69   and whatever fi
133f0 6c 65 6e 61 6d 65 20 65 6e 63 6f 64 69 6e 67 20  lename encoding 
13400 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 70  is used by the p
13410 61 72 74 69 63 75 6c 61 72 20 77 69 6e 64 6f 77  articular window
13420 73 0a 2a 2a 20 69 6e 73 74 61 6c 6c 61 74 69 6f  s.** installatio
13430 6e 2e 20 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63  n.  Memory alloc
13440 61 74 69 6f 6e 20 65 72 72 6f 72 73 20 61 72 65  ation errors are
13450 20 64 65 74 65 63 74 65 64 2c 20 62 75 74 0a 2a   detected, but.*
13460 2a 20 74 68 65 79 20 61 72 65 20 72 65 70 6f 72  * they are repor
13470 74 65 64 20 62 61 63 6b 20 61 73 20 5b 53 51 4c  ted back as [SQL
13480 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 6f 72  ITE_CANTOPEN] or
13490 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52  .** [SQLITE_IOER
134a0 52 5d 20 72 61 74 68 65 72 20 74 68 61 6e 20 5b  R] rather than [
134b0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a  SQLITE_NOMEM]..*
134c0 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
134d0 0a 2a 2a 0a 2a 2a 20 7b 46 31 37 33 30 33 7d 20  .**.** {F17303} 
134e0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 61   The [sqlite3_ma
134f0 6c 6c 6f 63 28 4e 29 5d 20 69 6e 74 65 72 66 61  lloc(N)] interfa
13500 63 65 20 72 65 74 75 72 6e 73 20 65 69 74 68 65  ce returns eithe
13510 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 0a  r a pointer to .
13520 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6e 65 77  **           new
13530 6c 79 20 63 68 65 63 6b 65 64 2d 6f 75 74 20 62  ly checked-out b
13540 6c 6f 63 6b 20 6f 66 20 61 74 20 6c 65 61 73 74  lock of at least
13550 20 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f   N bytes of memo
13560 72 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ry.**           
13570 74 68 61 74 20 69 73 20 38 2d 62 79 74 65 20 61  that is 8-byte a
13580 6c 69 67 6e 65 64 2c 20 0a 2a 2a 20 20 20 20 20  ligned, .**     
13590 20 20 20 20 20 20 6f 72 20 69 74 20 72 65 74 75        or it retu
135a0 72 6e 73 20 4e 55 4c 4c 20 69 66 20 69 74 20 69  rns NULL if it i
135b0 73 20 75 6e 61 62 6c 65 20 74 6f 20 66 75 6c 66  s unable to fulf
135c0 69 6c 6c 20 74 68 65 20 72 65 71 75 65 73 74 2e  ill the request.
135d0 0a 2a 2a 0a 2a 2a 20 7b 46 31 37 33 30 34 7d 20  .**.** {F17304} 
135e0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 61   The [sqlite3_ma
135f0 6c 6c 6f 63 28 4e 29 5d 20 69 6e 74 65 72 66 61  lloc(N)] interfa
13600 63 65 20 72 65 74 75 72 6e 73 20 61 20 4e 55 4c  ce returns a NUL
13610 4c 20 70 6f 69 6e 74 65 72 20 69 66 0a 2a 2a 20  L pointer if.** 
13620 20 20 20 20 20 20 20 20 20 20 4e 20 69 73 20 6c            N is l
13630 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ess than or equa
13640 6c 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a  l to zero..**.**
13650 20 7b 46 31 37 33 30 35 7d 20 20 54 68 65 20 5b   {F17305}  The [
13660 73 71 6c 69 74 65 33 5f 66 72 65 65 28 50 29 5d  sqlite3_free(P)]
13670 20 69 6e 74 65 72 66 61 63 65 20 72 65 6c 65 61   interface relea
13680 73 65 73 20 6d 65 6d 6f 72 79 20 70 72 65 76 69  ses memory previ
13690 6f 75 73 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20  ously.**        
136a0 20 20 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d     returned from
136b0 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
136c0 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ()] or [sqlite3_
136d0 72 65 61 6c 6c 6f 63 28 29 5d 2c 0a 2a 2a 20 20  realloc()],.**  
136e0 20 20 20 20 20 20 20 20 20 6d 61 6b 69 6e 67 20           making 
136f0 69 74 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72  it available for
13700 20 72 65 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 7b 46   reuse..**.** {F
13710 31 37 33 30 36 7d 20 20 41 20 63 61 6c 6c 20 74  17306}  A call t
13720 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28  o [sqlite3_free(
13730 4e 55 4c 4c 29 5d 20 69 73 20 61 20 68 61 72 6d  NULL)] is a harm
13740 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  less no-op..**.*
13750 2a 20 7b 46 31 37 33 31 30 7d 20 20 41 20 63 61  * {F17310}  A ca
13760 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72  ll to [sqlite3_r
13770 65 61 6c 6c 6f 63 28 30 2c 4e 29 5d 20 69 73 20  ealloc(0,N)] is 
13780 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 61 20  equivalent to a 
13790 63 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20  call.**         
137a0 20 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61    to [sqlite3_ma
137b0 6c 6c 6f 63 28 4e 29 5d 2e 0a 2a 2a 0a 2a 2a 20  lloc(N)]..**.** 
137c0 7b 46 31 37 33 31 32 7d 20 20 41 20 63 61 6c 6c  {F17312}  A call
137d0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 61   to [sqlite3_rea
137e0 6c 6c 6f 63 28 50 2c 30 29 5d 20 69 73 20 65 71  lloc(P,0)] is eq
137f0 75 69 76 61 6c 65 6e 74 20 74 6f 20 61 20 63 61  uivalent to a ca
13800 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ll.**           
13810 74 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65  to [sqlite3_free
13820 28 50 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 37  (P)]..**.** {F17
13830 33 31 35 7d 20 20 54 68 65 20 53 51 4c 69 74 65  315}  The SQLite
13840 20 63 6f 72 65 20 75 73 65 73 20 5b 73 71 6c 69   core uses [sqli
13850 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 20 5b  te3_malloc()], [
13860 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
13870 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  )],.**          
13880 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 66 72   and [sqlite3_fr
13890 65 65 28 29 5d 20 66 6f 72 20 61 6c 6c 20 6f 66  ee()] for all of
138a0 20 69 74 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   its memory allo
138b0 63 61 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 20 20  cation and.**   
138c0 20 20 20 20 20 20 20 20 64 65 61 6c 6c 6f 63 61          dealloca
138d0 74 69 6f 6e 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a  tion needs..**.*
138e0 2a 20 7b 46 31 37 33 31 38 7d 20 20 54 68 65 20  * {F17318}  The 
138f0 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63  [sqlite3_realloc
13900 28 50 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65  (P,N)] interface
13910 20 72 65 74 75 72 6e 73 20 65 69 74 68 65 72 20   returns either 
13920 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 20 20 20  a pointer.**    
13930 20 20 20 20 20 20 20 74 6f 20 61 20 62 6c 6f 63         to a bloc
13940 6b 20 6f 66 20 63 68 65 63 6b 65 64 2d 6f 75 74  k of checked-out
13950 20 6d 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65   memory of at le
13960 61 73 74 20 4e 20 62 79 74 65 73 20 69 6e 20 73  ast N bytes in s
13970 69 7a 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ize.**          
13980 20 74 68 61 74 20 69 73 20 38 2d 62 79 74 65 20   that is 8-byte 
13990 61 6c 69 67 6e 65 64 2c 20 6f 72 20 61 20 4e 55  aligned, or a NU
139a0 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a  LL pointer..**.*
139b0 2a 20 7b 46 31 37 33 32 31 7d 20 20 57 68 65 6e  * {F17321}  When
139c0 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f   [sqlite3_reallo
139d0 63 28 50 2c 4e 29 5d 20 72 65 74 75 72 6e 73 20  c(P,N)] returns 
139e0 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74  a non-NULL point
139f0 65 72 2c 20 69 74 20 66 69 72 73 74 0a 2a 2a 20  er, it first.** 
13a00 20 20 20 20 20 20 20 20 20 20 63 6f 70 69 65 73            copies
13a10 20 74 68 65 20 66 69 72 73 74 20 4b 20 62 79 74   the first K byt
13a20 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20 66 72  es of content fr
13a30 6f 6d 20 50 20 69 6e 74 6f 20 74 68 65 20 6e 65  om P into the ne
13a40 77 6c 79 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a  wly allocated.**
13a50 20 20 20 20 20 20 20 20 20 20 20 77 68 65 72 65             where
13a60 20 4b 20 69 73 20 74 68 65 20 6c 65 73 73 6f 72   K is the lessor
13a70 20 6f 66 20 4e 20 61 6e 64 20 74 68 65 20 73 69   of N and the si
13a80 7a 65 20 6f 66 20 74 68 65 20 62 75 66 66 65 72  ze of the buffer
13a90 20 50 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 37 33 32   P..**.** {F1732
13aa0 32 7d 20 20 57 68 65 6e 20 5b 73 71 6c 69 74 65  2}  When [sqlite
13ab0 33 5f 72 65 61 6c 6c 6f 63 28 50 2c 4e 29 5d 20  3_realloc(P,N)] 
13ac0 72 65 74 75 72 6e 73 20 61 20 6e 6f 6e 2d 4e 55  returns a non-NU
13ad0 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 69 74 20 66  LL pointer, it f
13ae0 69 72 73 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  irst.**         
13af0 20 20 72 65 6c 65 61 73 65 73 20 74 68 65 20 62    releases the b
13b00 75 66 66 65 72 20 50 2e 0a 2a 2a 0a 2a 2a 20 7b  uffer P..**.** {
13b10 46 31 37 33 32 33 7d 20 20 57 68 65 6e 20 5b 73  F17323}  When [s
13b20 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 50  qlite3_realloc(P
13b30 2c 4e 29 5d 20 72 65 74 75 72 6e 73 20 4e 55 4c  ,N)] returns NUL
13b40 4c 2c 20 74 68 65 20 62 75 66 66 65 72 20 50 20  L, the buffer P 
13b50 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  is.**           
13b60 6e 6f 74 20 6d 6f 64 69 66 69 65 64 20 6f 72 20  not modified or 
13b70 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20  released..**.** 
13b80 4c 49 4d 49 54 41 54 49 4f 4e 53 3a 0a 2a 2a 0a  LIMITATIONS:.**.
13b90 2a 2a 20 7b 55 31 37 33 35 30 7d 20 20 54 68 65  ** {U17350}  The
13ba0 20 70 6f 69 6e 74 65 72 20 61 72 67 75 6d 65 6e   pointer argumen
13bb0 74 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 66  ts to [sqlite3_f
13bc0 72 65 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69  ree()] and [sqli
13bd0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 0a 2a  te3_realloc()].*
13be0 2a 20 20 20 20 20 20 20 20 20 20 20 6d 75 73 74  *           must
13bf0 20 62 65 20 65 69 74 68 65 72 20 4e 55 4c 4c 20   be either NULL 
13c00 6f 72 20 65 6c 73 65 20 61 20 70 6f 69 6e 74 65  or else a pointe
13c10 72 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  r obtained from 
13c20 61 20 70 72 69 6f 72 0a 2a 2a 20 20 20 20 20 20  a prior.**      
13c30 20 20 20 20 20 69 6e 76 6f 63 61 74 69 6f 6e 20       invocation 
13c40 6f 66 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  of [sqlite3_mall
13c50 6f 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65  oc()] or [sqlite
13c60 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 74 68 61  3_realloc()] tha
13c70 74 20 68 61 73 0a 2a 2a 20 20 20 20 20 20 20 20  t has.**        
13c80 20 20 20 6e 6f 74 20 62 65 65 6e 20 72 65 6c 65     not been rele
13c90 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 55 31 37  ased..**.** {U17
13ca0 33 35 31 7d 20 20 54 68 65 20 61 70 70 6c 69 63  351}  The applic
13cb0 61 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 72  ation must not r
13cc0 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 6e 79  ead or write any
13cd0 20 70 61 72 74 20 6f 66 20 0a 2a 2a 20 20 20 20   part of .**    
13ce0 20 20 20 20 20 20 20 61 20 62 6c 6f 63 6b 20 6f         a block o
13cf0 66 20 6d 65 6d 6f 72 79 20 61 66 74 65 72 20 69  f memory after i
13d00 74 20 68 61 73 20 62 65 65 6e 20 72 65 6c 65 61  t has been relea
13d10 73 65 64 20 75 73 69 6e 67 0a 2a 2a 20 20 20 20  sed using.**    
13d20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
13d30 66 72 65 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69  free()] or [sqli
13d40 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 2e 0a  te3_realloc()]..
13d50 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  **.*/.SQLITE_API
13d60 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 6d   void *sqlite3_m
13d70 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49  alloc(int);.SQLI
13d80 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
13d90 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 76 6f 69  ite3_realloc(voi
13da0 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  d*, int);.SQLITE
13db0 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
13dc0 33 5f 66 72 65 65 28 76 6f 69 64 2a 29 3b 0a 0a  3_free(void*);..
13dd0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
13de0 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 6f 72  Memory Allocator
13df0 20 53 74 61 74 69 73 74 69 63 73 20 7b 46 31 37   Statistics {F17
13e00 33 37 30 7d 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  370}.**.** SQLit
13e10 65 20 70 72 6f 76 69 64 65 73 20 74 68 65 73 65  e provides these
13e20 20 74 77 6f 20 69 6e 74 65 72 66 61 63 65 73 20   two interfaces 
13e30 66 6f 72 20 72 65 70 6f 72 74 69 6e 67 20 6f 6e  for reporting on
13e40 20 74 68 65 20 73 74 61 74 75 73 0a 2a 2a 20 6f   the status.** o
13e50 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d  f the [sqlite3_m
13e60 61 6c 6c 6f 63 28 29 5d 2c 20 5b 73 71 6c 69 74  alloc()], [sqlit
13e70 65 33 5f 66 72 65 65 28 29 5d 2c 20 61 6e 64 20  e3_free()], and 
13e80 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63  [sqlite3_realloc
13e90 28 29 5d 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72  ()].** the memor
13ea0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62  y allocation sub
13eb0 73 79 73 74 65 6d 20 69 6e 63 6c 75 64 65 64 20  system included 
13ec0 77 69 74 68 69 6e 20 74 68 65 20 53 51 4c 69 74  within the SQLit
13ed0 65 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  e..**.** INVARIA
13ee0 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31 37 33  NTS:.**.** {F173
13ef0 37 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  71} The [sqlite3
13f00 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 5d 20  _memory_used()] 
13f10 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
13f20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
13f30 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
13f40 6f 66 20 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e  of memory curren
13f50 74 6c 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20  tly outstanding 
13f60 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 28 6d 61  .**          (ma
13f70 6c 6c 6f 63 65 64 20 62 75 74 20 6e 6f 74 20 66  lloced but not f
13f80 72 65 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31  reed)..**.** {F1
13f90 37 33 37 33 7d 20 54 68 65 20 5b 73 71 6c 69 74  7373} The [sqlit
13fa0 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61  e3_memory_highwa
13fb0 74 65 72 28 29 5d 20 72 6f 75 74 69 6e 65 20 72  ter()] routine r
13fc0 65 74 75 72 6e 73 20 74 68 65 20 6d 61 78 69 6d  eturns the maxim
13fd0 75 6d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 76  um.**          v
13fe0 61 6c 75 65 20 6f 66 20 5b 73 71 6c 69 74 65 33  alue of [sqlite3
13ff0 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 5d 20  _memory_used()] 
14000 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 69 6e  .**          sin
14010 63 65 20 74 68 65 20 68 69 67 68 77 61 74 65 72  ce the highwater
14020 20 6d 61 72 6b 20 77 61 73 20 6c 61 73 74 20 72   mark was last r
14030 65 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 37  eset..**.** {F17
14040 33 37 34 7d 20 54 68 65 20 76 61 6c 75 65 73 20  374} The values 
14050 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c  returned by [sql
14060 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64  ite3_memory_used
14070 28 29 5d 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20  ()] and.**      
14080 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d      [sqlite3_mem
14090 6f 72 79 5f 68 69 67 68 77 61 74 65 72 28 29 5d  ory_highwater()]
140a0 20 69 6e 63 6c 75 64 65 20 61 6e 79 20 6f 76 65   include any ove
140b0 72 68 65 61 64 0a 2a 2a 20 20 20 20 20 20 20 20  rhead.**        
140c0 20 20 61 64 64 65 64 20 62 79 20 53 51 4c 69 74    added by SQLit
140d0 65 20 69 6e 20 69 74 73 20 69 6d 70 6c 65 6d 65  e in its impleme
140e0 6e 74 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69  ntation of [sqli
140f0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 0a 2a  te3_malloc()],.*
14100 2a 20 20 20 20 20 20 20 20 20 20 62 75 74 20 6e  *          but n
14110 6f 74 20 6f 76 65 72 68 65 61 64 20 61 64 64 65  ot overhead adde
14120 64 20 62 79 20 74 68 65 20 61 6e 79 20 75 6e 64  d by the any und
14130 65 72 6c 79 69 6e 67 20 73 79 73 74 65 6d 20 6c  erlying system l
14140 69 62 72 61 72 79 0a 2a 2a 20 20 20 20 20 20 20  ibrary.**       
14150 20 20 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74     routines that
14160 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
14170 28 29 5d 20 6d 61 79 20 63 61 6c 6c 2e 0a 2a 2a  ()] may call..**
14180 20 0a 2a 2a 20 7b 46 31 37 33 37 35 7d 20 54 68   .** {F17375} Th
14190 65 20 6d 65 6d 6f 72 79 20 68 69 67 68 77 61 74  e memory highwat
141a0 65 72 20 6d 61 72 6b 20 69 73 20 72 65 73 65 74  er mark is reset
141b0 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20   to the current 
141c0 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 20 20 20 20  value of.**     
141d0 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 6d 65       [sqlite3_me
141e0 6d 6f 72 79 5f 75 73 65 64 28 29 5d 20 69 66 20  mory_used()] if 
141f0 61 6e 64 20 6f 6e 6c 79 20 69 66 20 74 68 65 20  and only if the 
14200 70 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20  parameter to.** 
14210 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
14220 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74  3_memory_highwat
14230 65 72 28 29 5d 20 69 73 20 74 72 75 65 2e 20 20  er()] is true.  
14240 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  The value return
14250 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62  ed.**          b
14260 79 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72  y [sqlite3_memor
14270 79 5f 68 69 67 68 77 61 74 65 72 28 31 29 5d 20  y_highwater(1)] 
14280 69 73 20 74 68 65 20 68 69 67 68 77 61 74 65 72  is the highwater
14290 20 6d 61 72 6b 0a 2a 2a 20 20 20 20 20 20 20 20   mark.**        
142a0 20 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 72    prior to the r
142b0 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  eset..*/.SQLITE_
142c0 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  API sqlite3_int6
142d0 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79  4 sqlite3_memory
142e0 5f 75 73 65 64 28 76 6f 69 64 29 3b 0a 53 51 4c  _used(void);.SQL
142f0 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
14300 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65  int64 sqlite3_me
14310 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 28 69  mory_highwater(i
14320 6e 74 20 72 65 73 65 74 46 6c 61 67 29 3b 0a 0a  nt resetFlag);..
14330 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
14340 50 73 65 75 64 6f 2d 52 61 6e 64 6f 6d 20 4e 75  Pseudo-Random Nu
14350 6d 62 65 72 20 47 65 6e 65 72 61 74 6f 72 20 7b  mber Generator {
14360 46 31 37 33 39 30 7d 0a 2a 2a 0a 2a 2a 20 53 51  F17390}.**.** SQ
14370 4c 69 74 65 20 63 6f 6e 74 61 69 6e 73 20 61 20  Lite contains a 
14380 68 69 67 68 2d 71 75 61 6c 69 74 79 20 70 73 65  high-quality pse
14390 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 65  udo-random numbe
143a0 72 20 67 65 6e 65 72 61 74 6f 72 20 28 50 52 4e  r generator (PRN
143b0 47 29 20 75 73 65 64 20 74 6f 0a 2a 2a 20 73 65  G) used to.** se
143c0 6c 65 63 74 20 72 61 6e 64 6f 6d 20 52 4f 57 49  lect random ROWI
143d0 44 73 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e  Ds when insertin
143e0 67 20 6e 65 77 20 72 65 63 6f 72 64 73 20 69 6e  g new records in
143f0 74 6f 20 61 20 74 61 62 6c 65 20 74 68 61 74 0a  to a table that.
14400 2a 2a 20 61 6c 72 65 61 64 79 20 75 73 65 73 20  ** already uses 
14410 74 68 65 20 6c 61 72 67 65 73 74 20 70 6f 73 73  the largest poss
14420 69 62 6c 65 20 52 4f 57 49 44 2e 20 20 54 68 65  ible ROWID.  The
14430 20 50 52 4e 47 20 69 73 20 61 6c 73 6f 20 75 73   PRNG is also us
14440 65 64 20 66 6f 72 0a 2a 2a 20 74 68 65 20 62 75  ed for.** the bu
14450 69 6c 64 2d 69 6e 20 72 61 6e 64 6f 6d 28 29 20  ild-in random() 
14460 61 6e 64 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 29  and randomblob()
14470 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 20   SQL functions. 
14480 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   This interface 
14490 61 6c 6c 6f 77 73 0a 2a 2a 20 61 70 70 6c 69 61  allows.** applia
144a0 74 69 6f 6e 73 20 74 6f 20 61 63 63 65 73 73 20  tions to access 
144b0 74 68 65 20 73 61 6d 65 20 50 52 4e 47 20 66 6f  the same PRNG fo
144c0 72 20 6f 74 68 65 72 20 70 75 72 70 6f 73 65 73  r other purposes
144d0 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74  ..**.** A call t
144e0 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73  o this routine s
144f0 74 6f 72 65 73 20 4e 20 62 79 74 65 73 20 6f 66  tores N bytes of
14500 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f   randomness into
14510 20 62 75 66 66 65 72 20 50 2e 0a 2a 2a 0a 2a 2a   buffer P..**.**
14520 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20   The first time 
14530 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
14540 69 6e 76 6f 6b 65 64 20 28 65 69 74 68 65 72 20  invoked (either 
14550 69 6e 74 65 72 6e 61 6c 6c 79 20 6f 72 20 62 79  internally or by
14560 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74  .** the applicat
14570 69 6f 6e 29 20 74 68 65 20 50 52 4e 47 20 69 73  ion) the PRNG is
14580 20 73 65 65 64 65 64 20 75 73 69 6e 67 20 72 61   seeded using ra
14590 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61 69 6e 65  ndomness obtaine
145a0 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 78 52  d.** from the xR
145b0 61 6e 64 6f 6d 6e 65 73 73 20 6d 65 74 68 6f 64  andomness method
145c0 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   of the default 
145d0 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62  [sqlite3_vfs] ob
145e0 6a 65 63 74 2e 0a 2a 2a 20 4f 6e 20 61 6c 6c 20  ject..** On all 
145f0 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f 63  subsequent invoc
14600 61 74 69 6f 6e 73 2c 20 74 68 65 20 70 73 65 75  ations, the pseu
14610 64 6f 2d 72 61 6e 64 6f 6d 6e 65 73 73 20 69 73  do-randomness is
14620 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 69 6e   generated.** in
14630 74 65 72 6e 61 6c 6c 79 20 61 6e 64 20 77 69 74  ternally and wit
14640 68 6f 75 74 20 72 65 63 6f 75 72 73 65 20 74 6f  hout recourse to
14650 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66   the [sqlite3_vf
14660 73 5d 20 78 52 61 6e 64 6f 6d 6e 65 73 73 0a 2a  s] xRandomness.*
14670 2a 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20  * method..**.** 
14680 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a  INVARIANTS:.**.*
14690 2a 20 7b 46 31 37 33 39 32 7d 20 54 68 65 20 5b  * {F17392} The [
146a0 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65  sqlite3_randomne
146b0 73 73 28 4e 2c 50 29 5d 20 69 6e 74 65 72 66 61  ss(N,P)] interfa
146c0 63 65 20 77 72 69 74 65 73 20 4e 20 62 79 74 65  ce writes N byte
146d0 73 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20  s of.**         
146e0 20 68 69 67 68 2d 71 75 61 6c 69 74 79 20 70 73   high-quality ps
146f0 65 75 64 6f 2d 72 61 6e 64 6f 6d 6e 65 73 73 20  eudo-randomness 
14700 69 6e 74 6f 20 62 75 66 66 65 72 20 50 2e 0a 2a  into buffer P..*
14710 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
14720 64 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d  d sqlite3_random
14730 6e 65 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64  ness(int N, void
14740 20 2a 50 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50   *P);../*.** CAP
14750 49 33 52 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54  I3REF: Compile-T
14760 69 6d 65 20 41 75 74 68 6f 72 69 7a 61 74 69 6f  ime Authorizatio
14770 6e 20 43 61 6c 6c 62 61 63 6b 73 20 7b 46 31 32  n Callbacks {F12
14780 35 30 30 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  500}.**.** This 
14790 72 6f 75 74 69 6e 65 20 72 65 67 69 73 74 65 72  routine register
147a0 73 20 61 20 61 75 74 68 6f 72 69 7a 65 72 20 63  s a authorizer c
147b0 61 6c 6c 62 61 63 6b 20 77 69 74 68 20 61 20 70  allback with a p
147c0 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 5b 64 61  articular.** [da
147d0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
147e0 6e 5d 2c 20 73 75 70 70 6c 69 65 64 20 69 6e 20  n], supplied in 
147f0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
14800 6e 74 2e 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  nt..** The autho
14810 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69  rizer callback i
14820 73 20 69 6e 76 6f 6b 65 64 20 61 73 20 53 51 4c  s invoked as SQL
14830 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20   statements are 
14840 62 65 69 6e 67 20 63 6f 6d 70 69 6c 65 64 0a 2a  being compiled.*
14850 2a 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 70 72  * by [sqlite3_pr
14860 65 70 61 72 65 28 29 5d 20 6f 72 20 69 74 73 20  epare()] or its 
14870 76 61 72 69 61 6e 74 73 20 5b 73 71 6c 69 74 65  variants [sqlite
14880 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c  3_prepare_v2()],
14890 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  .** [sqlite3_pre
148a0 70 61 72 65 31 36 28 29 5d 20 61 6e 64 20 5b 73  pare16()] and [s
148b0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
148c0 5f 76 32 28 29 5d 2e 20 20 41 74 20 76 61 72 69  _v2()].  At vari
148d0 6f 75 73 0a 2a 2a 20 70 6f 69 6e 74 73 20 64 75  ous.** points du
148e0 72 69 6e 67 20 74 68 65 20 63 6f 6d 70 69 6c 61  ring the compila
148f0 74 69 6f 6e 20 70 72 6f 63 65 73 73 2c 20 61 73  tion process, as
14900 20 6c 6f 67 69 63 20 69 73 20 62 65 69 6e 67 20   logic is being 
14910 63 72 65 61 74 65 64 0a 2a 2a 20 74 6f 20 70 65  created.** to pe
14920 72 66 6f 72 6d 20 76 61 72 69 6f 75 73 20 61 63  rform various ac
14930 74 69 6f 6e 73 2c 20 74 68 65 20 61 75 74 68 6f  tions, the autho
14940 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69  rizer callback i
14950 73 20 69 6e 76 6f 6b 65 64 20 74 6f 0a 2a 2a 20  s invoked to.** 
14960 73 65 65 20 69 66 20 74 68 6f 73 65 20 61 63 74  see if those act
14970 69 6f 6e 73 20 61 72 65 20 61 6c 6c 6f 77 65 64  ions are allowed
14980 2e 20 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65  .  The authorize
14990 72 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c  r callback shoul
149a0 64 0a 2a 2a 20 72 65 74 75 72 6e 20 5b 53 51 4c  d.** return [SQL
149b0 49 54 45 5f 4f 4b 5d 20 74 6f 20 61 6c 6c 6f 77  ITE_OK] to allow
149c0 20 74 68 65 20 61 63 74 69 6f 6e 2c 20 5b 53 51   the action, [SQ
149d0 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 74 6f 20  LITE_IGNORE] to 
149e0 64 69 73 61 6c 6c 6f 77 20 74 68 65 0a 2a 2a 20  disallow the.** 
149f0 73 70 65 63 69 66 69 63 20 61 63 74 69 6f 6e 20  specific action 
14a00 62 75 74 20 61 6c 6c 6f 77 20 74 68 65 20 53 51  but allow the SQ
14a10 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 63  L statement to c
14a20 6f 6e 74 69 6e 75 65 20 74 6f 20 62 65 0a 2a 2a  ontinue to be.**
14a30 20 63 6f 6d 70 69 6c 65 64 2c 20 6f 72 20 5b 53   compiled, or [S
14a40 51 4c 49 54 45 5f 44 45 4e 59 5d 20 74 6f 20 63  QLITE_DENY] to c
14a50 61 75 73 65 20 74 68 65 20 65 6e 74 69 72 65 20  ause the entire 
14a60 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  SQL statement to
14a70 20 62 65 0a 2a 2a 20 72 65 6a 65 63 74 65 64 20   be.** rejected 
14a80 77 69 74 68 20 61 6e 20 65 72 72 6f 72 2e 20 20  with an error.  
14a90 20 49 66 20 74 68 65 20 61 75 74 68 6f 72 69 7a   If the authoriz
14aa0 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75  er callback retu
14ab0 72 6e 73 0a 2a 2a 20 61 6e 79 20 76 61 6c 75 65  rns.** any value
14ac0 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c   other than [SQL
14ad0 49 54 45 5f 49 47 4e 4f 52 45 5d 2c 20 5b 53 51  ITE_IGNORE], [SQ
14ae0 4c 49 54 45 5f 4f 4b 5d 2c 20 6f 72 20 5b 53 51  LITE_OK], or [SQ
14af0 4c 49 54 45 5f 44 45 4e 59 5d 0a 2a 2a 20 74 68  LITE_DENY].** th
14b00 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  en [sqlite3_prep
14b10 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71 75  are_v2()] or equ
14b20 69 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68 61  ivalent call tha
14b30 74 20 74 72 69 67 67 65 72 65 64 0a 2a 2a 20 74  t triggered.** t
14b40 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 77 69  he authorizer wi
14b50 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 20  ll fail with an 
14b60 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a  error message..*
14b70 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 61  *.** When the ca
14b80 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 5b  llback returns [
14b90 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 74 68 61 74  SQLITE_OK], that
14ba0 20 6d 65 61 6e 73 20 74 68 65 20 6f 70 65 72 61   means the opera
14bb0 74 69 6f 6e 0a 2a 2a 20 72 65 71 75 65 73 74 65  tion.** requeste
14bc0 64 20 69 73 20 6f 6b 2e 20 20 57 68 65 6e 20 74  d is ok.  When t
14bd0 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75  he callback retu
14be0 72 6e 73 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59  rns [SQLITE_DENY
14bf0 5d 2c 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74  ], the.** [sqlit
14c00 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
14c10 20 6f 72 20 65 71 75 69 76 61 6c 65 6e 74 20 63   or equivalent c
14c20 61 6c 6c 20 74 68 61 74 20 74 72 69 67 67 65 72  all that trigger
14c30 65 64 20 74 68 65 0a 2a 2a 20 61 75 74 68 6f 72  ed the.** author
14c40 69 7a 65 72 20 77 69 6c 6c 20 66 61 69 6c 20 77  izer will fail w
14c50 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ith an error mes
14c60 73 61 67 65 20 65 78 70 6c 61 69 6e 69 6e 67 20  sage explaining 
14c70 74 68 61 74 0a 2a 2a 20 61 63 63 65 73 73 20 69  that.** access i
14c80 73 20 64 65 6e 69 65 64 2e 20 20 49 66 20 74 68  s denied.  If th
14c90 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 6f 64  e authorizer cod
14ca0 65 20 69 73 20 5b 53 51 4c 49 54 45 5f 52 45 41  e is [SQLITE_REA
14cb0 44 5d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63 61  D].** and the ca
14cc0 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 5b  llback returns [
14cd0 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 74  SQLITE_IGNORE] t
14ce0 68 65 6e 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70  hen the.** [prep
14cf0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
14d00 73 74 61 74 65 6d 65 6e 74 20 69 73 20 63 6f 6e  statement is con
14d10 73 74 72 75 63 74 65 64 20 74 6f 20 73 75 62 73  structed to subs
14d20 74 69 74 75 74 65 0a 2a 2a 20 61 20 4e 55 4c 4c  titute.** a NULL
14d30 20 76 61 6c 75 65 20 69 6e 20 70 6c 61 63 65 20   value in place 
14d40 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c  of the table col
14d50 75 6d 6e 20 74 68 61 74 20 77 6f 75 6c 64 20 68  umn that would h
14d60 61 76 65 0a 2a 2a 20 62 65 65 6e 20 72 65 61 64  ave.** been read
14d70 20 69 66 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20   if [SQLITE_OK] 
14d80 68 61 64 20 62 65 65 6e 20 72 65 74 75 72 6e 65  had been returne
14d90 64 2e 20 20 54 68 65 20 5b 53 51 4c 49 54 45 5f  d.  The [SQLITE_
14da0 49 47 4e 4f 52 45 5d 0a 2a 2a 20 72 65 74 75 72  IGNORE].** retur
14db0 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  n can be used to
14dc0 20 64 65 6e 79 20 61 6e 20 75 6e 74 72 75 73 74   deny an untrust
14dd0 65 64 20 75 73 65 72 20 61 63 63 65 73 73 20 74  ed user access t
14de0 6f 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20  o individual.** 
14df0 63 6f 6c 75 6d 6e 73 20 6f 66 20 61 20 74 61 62  columns of a tab
14e00 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  le..**.** The fi
14e10 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f  rst parameter to
14e20 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20   the authorizer 
14e30 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 63 6f  callback is a co
14e40 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 74 68 69  py of.** the thi
14e50 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  rd parameter to 
14e60 74 68 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f  the sqlite3_set_
14e70 61 75 74 68 6f 72 69 7a 65 72 28 29 20 69 6e 74  authorizer() int
14e80 65 72 66 61 63 65 2e 0a 2a 2a 20 54 68 65 20 73  erface..** The s
14e90 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
14ea0 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  to the callback 
14eb0 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 0a 2a  is an integer .*
14ec0 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 50 59 20 7c  * [SQLITE_COPY |
14ed0 20 61 63 74 69 6f 6e 20 63 6f 64 65 5d 20 74 68   action code] th
14ee0 61 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65  at specifies the
14ef0 20 70 61 72 74 69 63 75 6c 61 72 20 61 63 74 69   particular acti
14f00 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 61 75 74 68  on.** to be auth
14f10 6f 72 69 7a 65 64 2e 20 54 68 65 20 74 68 69 72  orized. The thir
14f20 64 20 74 68 72 6f 75 67 68 20 73 69 78 74 68 0a  d through sixth.
14f30 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f  ** parameters to
14f40 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 61 72   the callback ar
14f50 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  e zero-terminate
14f60 64 20 73 74 72 69 6e 67 73 20 74 68 61 74 20 63  d strings that c
14f70 6f 6e 74 61 69 6e 20 0a 2a 2a 20 61 64 64 69 74  ontain .** addit
14f80 69 6f 6e 61 6c 20 64 65 74 61 69 6c 73 20 61 62  ional details ab
14f90 6f 75 74 20 74 68 65 20 61 63 74 69 6f 6e 20 74  out the action t
14fa0 6f 20 62 65 20 61 75 74 68 6f 72 69 7a 65 64 2e  o be authorized.
14fb0 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 75 74 68 6f 72  .**.** An author
14fc0 69 7a 65 72 20 69 73 20 75 73 65 64 20 77 68 65  izer is used whe
14fd0 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  n [sqlite3_prepa
14fe0 72 65 20 7c 20 70 72 65 70 61 72 69 6e 67 5d 0a  re | preparing].
14ff0 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ** SQL statement
15000 73 20 66 72 6f 6d 20 61 6e 20 75 6e 74 72 75 73  s from an untrus
15010 74 65 64 0a 2a 2a 20 73 6f 75 72 63 65 2c 20 74  ted.** source, t
15020 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68  o ensure that th
15030 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  e SQL statements
15040 20 64 6f 20 6e 6f 74 20 74 72 79 20 74 6f 20 61   do not try to a
15050 63 63 65 73 73 20 64 61 74 61 0a 2a 2a 20 74 68  ccess data.** th
15060 61 74 20 74 68 65 79 20 61 72 65 20 6e 6f 74 20  at they are not 
15070 61 6c 6c 6f 77 65 64 20 74 6f 20 73 65 65 2c 20  allowed to see, 
15080 6f 72 20 74 68 61 74 20 74 68 65 79 20 64 6f 20  or that they do 
15090 6e 6f 74 20 74 72 79 20 74 6f 0a 2a 2a 20 65 78  not try to.** ex
150a0 65 63 75 74 65 20 6d 61 6c 69 63 69 6f 75 73 20  ecute malicious 
150b0 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20  statements that 
150c0 64 61 6d 61 67 65 20 74 68 65 20 64 61 74 61 62  damage the datab
150d0 61 73 65 2e 20 20 46 6f 72 0a 2a 2a 20 65 78 61  ase.  For.** exa
150e0 6d 70 6c 65 2c 20 61 6e 20 61 70 70 6c 69 63 61  mple, an applica
150f0 74 69 6f 6e 20 6d 61 79 20 61 6c 6c 6f 77 20 61  tion may allow a
15100 20 75 73 65 72 20 74 6f 20 65 6e 74 65 72 20 61   user to enter a
15110 72 62 69 74 72 61 72 79 0a 2a 2a 20 53 51 4c 20  rbitrary.** SQL 
15120 71 75 65 72 69 65 73 20 66 6f 72 20 65 76 61 6c  queries for eval
15130 75 61 74 69 6f 6e 20 62 79 20 61 20 64 61 74 61  uation by a data
15140 62 61 73 65 2e 20 20 42 75 74 20 74 68 65 20 61  base.  But the a
15150 70 70 6c 69 63 61 74 69 6f 6e 20 64 6f 65 73 0a  pplication does.
15160 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 20  ** not want the 
15170 75 73 65 72 20 74 6f 20 62 65 20 61 62 6c 65 20  user to be able 
15180 74 6f 20 6d 61 6b 65 20 61 72 62 69 74 72 61 72  to make arbitrar
15190 79 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65  y changes to the
151a0 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 41  .** database.  A
151b0 6e 20 61 75 74 68 6f 72 69 7a 65 72 20 63 6f 75  n authorizer cou
151c0 6c 64 20 74 68 65 6e 20 62 65 20 70 75 74 20 69  ld then be put i
151d0 6e 20 70 6c 61 63 65 20 77 68 69 6c 65 20 74 68  n place while th
151e0 65 0a 2a 2a 20 75 73 65 72 2d 65 6e 74 65 72 65  e.** user-entere
151f0 64 20 53 51 4c 20 69 73 20 62 65 69 6e 67 20 5b  d SQL is being [
15200 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 20  sqlite3_prepare 
15210 7c 20 70 72 65 70 61 72 65 64 5d 20 74 68 61 74  | prepared] that
15220 0a 2a 2a 20 64 69 73 61 6c 6c 6f 77 73 20 65 76  .** disallows ev
15230 65 72 79 74 68 69 6e 67 20 65 78 63 65 70 74 20  erything except 
15240 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65  [SELECT] stateme
15250 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69  nts..**.** Appli
15260 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6e 65 65  cations that nee
15270 64 20 74 6f 20 70 72 6f 63 65 73 73 20 53 51 4c  d to process SQL
15280 20 66 72 6f 6d 20 75 6e 74 72 75 73 74 65 64 20   from untrusted 
15290 73 6f 75 72 63 65 73 0a 2a 2a 20 6d 69 67 68 74  sources.** might
152a0 20 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72 20 6c   also consider l
152b0 6f 77 65 72 69 6e 67 20 72 65 73 6f 75 72 63 65  owering resource
152c0 20 6c 69 6d 69 74 73 20 75 73 69 6e 67 20 5b 73   limits using [s
152d0 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 5d 0a  qlite3_limit()].
152e0 2a 2a 20 61 6e 64 20 6c 69 6d 69 74 69 6e 67 20  ** and limiting 
152f0 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 75 73  database size us
15300 69 6e 67 20 74 68 65 20 5b 6d 61 78 5f 70 61 67  ing the [max_pag
15310 65 5f 63 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41  e_count] [PRAGMA
15320 5d 0a 2a 2a 20 69 6e 20 61 64 64 69 74 69 6f 6e  ].** in addition
15330 20 74 6f 20 75 73 69 6e 67 20 61 6e 20 61 75 74   to using an aut
15340 68 6f 72 69 7a 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f  horizer..**.** O
15350 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 61 75 74  nly a single aut
15360 68 6f 72 69 7a 65 72 20 63 61 6e 20 62 65 20 69  horizer can be i
15370 6e 20 70 6c 61 63 65 20 6f 6e 20 61 20 64 61 74  n place on a dat
15380 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
15390 0a 2a 2a 20 61 74 20 61 20 74 69 6d 65 2e 20 20  .** at a time.  
153a0 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 73 71 6c  Each call to sql
153b0 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69  ite3_set_authori
153c0 7a 65 72 20 6f 76 65 72 72 69 64 65 73 20 74 68  zer overrides th
153d0 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73 20 63 61  e.** previous ca
153e0 6c 6c 2e 20 20 44 69 73 61 62 6c 65 20 74 68 65  ll.  Disable the
153f0 20 61 75 74 68 6f 72 69 7a 65 72 20 62 79 20 69   authorizer by i
15400 6e 73 74 61 6c 6c 69 6e 67 20 61 20 4e 55 4c 4c  nstalling a NULL
15410 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 54 68   callback..** Th
15420 65 20 61 75 74 68 6f 72 69 7a 65 72 20 69 73 20  e authorizer is 
15430 64 69 73 61 62 6c 65 64 20 62 79 20 64 65 66 61  disabled by defa
15440 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  ult..**.** Note 
15450 74 68 61 74 20 74 68 65 20 61 75 74 68 6f 72 69  that the authori
15460 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  zer callback is 
15470 69 6e 76 6f 6b 65 64 20 6f 6e 6c 79 20 64 75 72  invoked only dur
15480 69 6e 67 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  ing .** [sqlite3
15490 5f 70 72 65 70 61 72 65 28 29 5d 20 6f 72 20 69  _prepare()] or i
154a0 74 73 20 76 61 72 69 61 6e 74 73 2e 20 20 41 75  ts variants.  Au
154b0 74 68 6f 72 69 7a 61 74 69 6f 6e 20 69 73 20 6e  thorization is n
154c0 6f 74 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20  ot.** performed 
154d0 64 75 72 69 6e 67 20 73 74 61 74 65 6d 65 6e 74  during statement
154e0 20 65 76 61 6c 75 61 74 69 6f 6e 20 69 6e 20 5b   evaluation in [
154f0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e  sqlite3_step()].
15500 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54  .**.** INVARIANT
15510 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 35 30 31  S:.**.** {F12501
15520 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73  } The [sqlite3_s
15530 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 44 2c  et_authorizer(D,
15540 2e 2e 2e 29 5d 20 69 6e 74 65 72 66 61 63 65 20  ...)] interface 
15550 72 65 67 69 73 74 65 72 73 20 61 0a 2a 2a 20 20  registers a.**  
15560 20 20 20 20 20 20 20 20 61 75 74 68 6f 72 69 7a          authoriz
15570 65 72 20 63 61 6c 6c 62 61 63 6b 20 77 69 74 68  er callback with
15580 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
15590 74 69 6f 6e 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 46  tion D..**.** {F
155a0 31 32 35 30 32 7d 20 54 68 65 20 61 75 74 68 6f  12502} The autho
155b0 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69  rizer callback i
155c0 73 20 69 6e 76 6f 6b 65 64 20 61 73 20 53 51 4c  s invoked as SQL
155d0 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 0a   statements are.
155e0 2a 2a 20 20 20 20 20 20 20 20 20 20 62 65 69 6e  **          bein
155f0 67 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 0a 2a 2a  g compiled.**.**
15600 20 7b 46 31 32 35 30 33 7d 20 49 66 20 74 68 65   {F12503} If the
15610 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c   authorizer call
15620 62 61 63 6b 20 72 65 74 75 72 6e 73 20 61 6e 79  back returns any
15630 20 76 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61   value other tha
15640 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53  n.**          [S
15650 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 2c 20 5b  QLITE_IGNORE], [
15660 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 6f 72 20 5b  SQLITE_OK], or [
15670 53 51 4c 49 54 45 5f 44 45 4e 59 5d 20 74 68 65  SQLITE_DENY] the
15680 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  n.**          th
15690 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  e [sqlite3_prepa
156a0 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71 75 69  re_v2()] or equi
156b0 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68 61 74  valent call that
156c0 20 63 61 75 73 65 64 0a 2a 2a 20 20 20 20 20 20   caused.**      
156d0 20 20 20 20 74 68 65 20 61 75 74 68 6f 72 69 7a      the authoriz
156e0 65 72 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 72  er callback to r
156f0 75 6e 20 73 68 61 6c 6c 20 66 61 69 6c 20 77 69  un shall fail wi
15700 74 68 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20  th an.**        
15710 20 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d    [SQLITE_ERROR]
15720 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20   error code and 
15730 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 65  an appropriate e
15740 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a  rror message..**
15750 0a 2a 2a 20 7b 46 31 32 35 30 34 7d 20 57 68 65  .** {F12504} Whe
15760 6e 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72  n the authorizer
15770 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e   callback return
15780 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 74  s [SQLITE_OK], t
15790 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20  he operation.** 
157a0 20 20 20 20 20 20 20 20 20 64 65 73 63 72 69 62           describ
157b0 65 64 20 69 73 20 63 6f 64 65 64 20 6e 6f 72 6d  ed is coded norm
157c0 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32  ally..**.** {F12
157d0 35 30 35 7d 20 57 68 65 6e 20 74 68 65 20 61 75  505} When the au
157e0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
157f0 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54  k returns [SQLIT
15800 45 5f 44 45 4e 59 5d 2c 20 74 68 65 0a 2a 2a 20  E_DENY], the.** 
15810 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
15820 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20  3_prepare_v2()] 
15830 6f 72 20 65 71 75 69 76 61 6c 65 6e 74 20 63 61  or equivalent ca
15840 6c 6c 20 74 68 61 74 20 63 61 75 73 65 64 20 74  ll that caused t
15850 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61  he.**          a
15860 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61  uthorizer callba
15870 63 6b 20 74 6f 20 72 75 6e 20 73 68 61 6c 6c 20  ck to run shall 
15880 66 61 69 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20  fail.**         
15890 20 77 69 74 68 20 61 6e 20 5b 53 51 4c 49 54 45   with an [SQLITE
158a0 5f 45 52 52 4f 52 5d 20 65 72 72 6f 72 20 63 6f  _ERROR] error co
158b0 64 65 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20  de and an error 
158c0 6d 65 73 73 61 67 65 0a 2a 2a 20 20 20 20 20 20  message.**      
158d0 20 20 20 20 65 78 70 6c 61 69 6e 69 6e 67 20 74      explaining t
158e0 68 61 74 20 61 63 63 65 73 73 20 69 73 20 64 65  hat access is de
158f0 6e 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32  nied..**.** {F12
15900 35 30 36 7d 20 49 66 20 74 68 65 20 61 75 74 68  506} If the auth
15910 6f 72 69 7a 65 72 20 63 6f 64 65 20 28 74 68 65  orizer code (the
15920 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74   2nd parameter t
15930 6f 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72  o the authorizer
15940 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 61 6c  .**          cal
15950 6c 62 61 63 6b 29 20 69 73 20 5b 53 51 4c 49 54  lback) is [SQLIT
15960 45 5f 52 45 41 44 5d 20 61 6e 64 20 74 68 65 20  E_READ] and the 
15970 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62  authorizer callb
15980 61 63 6b 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20  ack returns.**  
15990 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f          [SQLITE_
159a0 49 47 4e 4f 52 45 5d 20 74 68 65 6e 20 74 68 65  IGNORE] then the
159b0 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
159c0 65 6e 74 20 69 73 20 63 6f 6e 73 74 72 75 63 74  ent is construct
159d0 65 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20  ed to.**        
159e0 20 20 69 6e 73 65 72 74 20 61 20 4e 55 4c 4c 20    insert a NULL 
159f0 76 61 6c 75 65 20 69 6e 20 70 6c 61 63 65 20 6f  value in place o
15a00 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75  f the table colu
15a10 6d 6e 20 74 68 61 74 20 77 6f 75 6c 64 20 68 61  mn that would ha
15a20 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62  ve.**          b
15a30 65 65 6e 20 72 65 61 64 20 69 66 20 5b 53 51 4c  een read if [SQL
15a40 49 54 45 5f 4f 4b 5d 20 68 61 64 20 62 65 65 6e  ITE_OK] had been
15a50 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
15a60 20 7b 46 31 32 35 30 37 7d 20 49 66 20 74 68 65   {F12507} If the
15a70 20 61 75 74 68 6f 72 69 7a 65 72 20 63 6f 64 65   authorizer code
15a80 20 28 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65   (the 2nd parame
15a90 74 65 72 20 74 6f 20 74 68 65 20 61 75 74 68 6f  ter to the autho
15aa0 72 69 7a 65 72 0a 2a 2a 20 20 20 20 20 20 20 20  rizer.**        
15ab0 20 20 63 61 6c 6c 62 61 63 6b 29 20 69 73 20 61    callback) is a
15ac0 6e 79 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68  nything other th
15ad0 61 6e 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 5d  an [SQLITE_READ]
15ae0 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20  , then.**       
15af0 20 20 20 61 20 72 65 74 75 72 6e 20 6f 66 20 5b     a return of [
15b00 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 68  SQLITE_IGNORE] h
15b10 61 73 20 74 68 65 20 73 61 6d 65 20 65 66 66 65  as the same effe
15b20 63 74 20 61 73 20 5b 53 51 4c 49 54 45 5f 44 45  ct as [SQLITE_DE
15b30 4e 59 5d 2e 20 0a 2a 2a 0a 2a 2a 20 7b 46 31 32  NY]. .**.** {F12
15b40 35 31 30 7d 20 54 68 65 20 66 69 72 73 74 20 70  510} The first p
15b50 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20  arameter to the 
15b60 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62  authorizer callb
15b70 61 63 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66  ack is a copy of
15b80 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65  .**          the
15b90 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72   third parameter
15ba0 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33   to the [sqlite3
15bb0 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28  _set_authorizer(
15bc0 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a  )] interface..**
15bd0 0a 2a 2a 20 7b 46 31 32 35 31 31 7d 20 54 68 65  .** {F12511} The
15be0 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
15bf0 72 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63  r to the callbac
15c00 6b 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20  k is an integer 
15c10 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51  .**          [SQ
15c20 4c 49 54 45 5f 43 4f 50 59 20 7c 20 61 63 74 69  LITE_COPY | acti
15c30 6f 6e 20 63 6f 64 65 5d 20 74 68 61 74 20 73 70  on code] that sp
15c40 65 63 69 66 69 65 73 20 74 68 65 20 70 61 72 74  ecifies the part
15c50 69 63 75 6c 61 72 20 61 63 74 69 6f 6e 0a 2a 2a  icular action.**
15c60 20 20 20 20 20 20 20 20 20 20 74 6f 20 62 65 20            to be 
15c70 61 75 74 68 6f 72 69 7a 65 64 2e 0a 2a 2a 0a 2a  authorized..**.*
15c80 2a 20 7b 46 31 32 35 31 32 7d 20 54 68 65 20 74  * {F12512} The t
15c90 68 69 72 64 20 74 68 72 6f 75 67 68 20 73 69 78  hird through six
15ca0 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f  th parameters to
15cb0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 61 72   the callback ar
15cc0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 7a 65  e.**          ze
15cd0 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74  ro-terminated st
15ce0 72 69 6e 67 73 20 74 68 61 74 20 63 6f 6e 74 61  rings that conta
15cf0 69 6e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  in .**          
15d00 61 64 64 69 74 69 6f 6e 61 6c 20 64 65 74 61 69  additional detai
15d10 6c 73 20 61 62 6f 75 74 20 74 68 65 20 61 63 74  ls about the act
15d20 69 6f 6e 20 74 6f 20 62 65 20 61 75 74 68 6f 72  ion to be author
15d30 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32  ized..**.** {F12
15d40 35 32 30 7d 20 45 61 63 68 20 63 61 6c 6c 20 74  520} Each call t
15d50 6f 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  o [sqlite3_set_a
15d60 75 74 68 6f 72 69 7a 65 72 28 29 5d 20 6f 76 65  uthorizer()] ove
15d70 72 72 69 64 65 73 20 74 68 65 0a 2a 2a 20 20 20  rrides the.**   
15d80 20 20 20 20 20 20 20 61 6e 79 20 70 72 65 76 69         any previ
15d90 6f 75 73 6c 79 20 69 6e 73 74 61 6c 6c 65 64 20  ously installed 
15da0 61 75 74 68 6f 72 69 7a 65 72 2e 0a 2a 2a 0a 2a  authorizer..**.*
15db0 2a 20 7b 46 31 32 35 32 31 7d 20 41 20 4e 55 4c  * {F12521} A NUL
15dc0 4c 20 61 75 74 68 6f 72 69 7a 65 72 20 6d 65 61  L authorizer mea
15dd0 6e 73 20 74 68 61 74 20 6e 6f 20 61 75 74 68 6f  ns that no autho
15de0 72 69 7a 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 20  rization.**     
15df0 20 20 20 20 20 63 61 6c 6c 62 61 63 6b 20 69 73       callback is
15e00 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20   invoked..**.** 
15e10 7b 46 31 32 35 32 32 7d 20 54 68 65 20 64 65 66  {F12522} The def
15e20 61 75 6c 74 20 61 75 74 68 6f 72 69 7a 65 72 20  ault authorizer 
15e30 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49  is NULL..*/.SQLI
15e40 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
15e50 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65  e3_set_authorize
15e60 72 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 0a 20  r(.  sqlite3*,. 
15e70 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f   int (*xAuth)(vo
15e80 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68  id*,int,const ch
15e90 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ar*,const char*,
15ea0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
15eb0 74 20 63 68 61 72 2a 29 2c 0a 20 20 76 6f 69 64  t char*),.  void
15ec0 20 2a 70 55 73 65 72 44 61 74 61 0a 29 3b 0a 0a   *pUserData.);..
15ed0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
15ee0 41 75 74 68 6f 72 69 7a 65 72 20 52 65 74 75 72  Authorizer Retur
15ef0 6e 20 43 6f 64 65 73 20 7b 46 31 32 35 39 30 7d  n Codes {F12590}
15f00 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69  .**.** The [sqli
15f10 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a  te3_set_authoriz
15f20 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65 72 20  er | authorizer 
15f30 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f  callback functio
15f40 6e 5d 20 6d 75 73 74 0a 2a 2a 20 72 65 74 75 72  n] must.** retur
15f50 6e 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54 45  n either [SQLITE
15f60 5f 4f 4b 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 74  _OK] or one of t
15f70 68 65 73 65 20 74 77 6f 20 63 6f 6e 73 74 61 6e  hese two constan
15f80 74 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74  ts in order.** t
15f90 6f 20 73 69 67 6e 61 6c 20 53 51 4c 69 74 65 20  o signal SQLite 
15fa0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
15fb0 68 65 20 61 63 74 69 6f 6e 20 69 73 20 70 65 72  he action is per
15fc0 6d 69 74 74 65 64 2e 20 20 53 65 65 20 74 68 65  mitted.  See the
15fd0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 65 74  .** [sqlite3_set
15fe0 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 75  _authorizer | au
15ff0 74 68 6f 72 69 7a 65 72 20 64 6f 63 75 6d 65 6e  thorizer documen
16000 74 61 74 69 6f 6e 5d 20 66 6f 72 20 61 64 64 69  tation] for addi
16010 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d  tional.** inform
16020 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ation..*/.#defin
16030 65 20 53 51 4c 49 54 45 5f 44 45 4e 59 20 20 20  e SQLITE_DENY   
16040 31 20 20 20 2f 2a 20 41 62 6f 72 74 20 74 68 65  1   /* Abort the
16050 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 77   SQL statement w
16060 69 74 68 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a  ith an error */.
16070 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
16080 47 4e 4f 52 45 20 32 20 20 20 2f 2a 20 44 6f 6e  GNORE 2   /* Don
16090 27 74 20 61 6c 6c 6f 77 20 61 63 63 65 73 73 2c  't allow access,
160a0 20 62 75 74 20 64 6f 6e 27 74 20 67 65 6e 65 72   but don't gener
160b0 61 74 65 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a  ate an error */.
160c0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
160d0 20 41 75 74 68 6f 72 69 7a 65 72 20 41 63 74 69   Authorizer Acti
160e0 6f 6e 20 43 6f 64 65 73 20 7b 46 31 32 35 35 30  on Codes {F12550
160f0 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c  }.**.** The [sql
16100 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69  ite3_set_authori
16110 7a 65 72 28 29 5d 20 69 6e 74 65 72 66 61 63 65  zer()] interface
16120 20 72 65 67 69 73 74 65 72 73 20 61 20 63 61 6c   registers a cal
16130 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a  lback function.*
16140 2a 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65  * that is invoke
16150 64 20 74 6f 20 61 75 74 68 6f 72 69 7a 65 72 20  d to authorizer 
16160 63 65 72 74 61 69 6e 20 53 51 4c 20 73 74 61 74  certain SQL stat
16170 65 6d 65 6e 74 20 61 63 74 69 6f 6e 73 2e 20 20  ement actions.  
16180 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61  The.** second pa
16190 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 63  rameter to the c
161a0 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 69 6e  allback is an in
161b0 74 65 67 65 72 20 63 6f 64 65 20 74 68 61 74 20  teger code that 
161c0 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 77 68 61  specifies.** wha
161d0 74 20 61 63 74 69 6f 6e 20 69 73 20 62 65 69 6e  t action is bein
161e0 67 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 20 54  g authorized.  T
161f0 68 65 73 65 20 61 72 65 20 74 68 65 20 69 6e 74  hese are the int
16200 65 67 65 72 20 61 63 74 69 6f 6e 20 63 6f 64 65  eger action code
16210 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 61 75  s that.** the au
16220 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
16230 6b 20 6d 61 79 20 62 65 20 70 61 73 73 65 64 2e  k may be passed.
16240 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61 63 74  .**.** These act
16250 69 6f 6e 20 63 6f 64 65 20 76 61 6c 75 65 73 20  ion code values 
16260 73 69 67 6e 69 66 79 20 77 68 61 74 20 6b 69 6e  signify what kin
16270 64 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 20 69  d of operation i
16280 73 20 74 6f 20 62 65 20 0a 2a 2a 20 61 75 74 68  s to be .** auth
16290 6f 72 69 7a 65 64 2e 20 20 54 68 65 20 33 72 64  orized.  The 3rd
162a0 20 61 6e 64 20 34 74 68 20 70 61 72 61 6d 65 74   and 4th paramet
162b0 65 72 73 20 74 6f 20 74 68 65 20 61 75 74 68 6f  ers to the autho
162c0 72 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6c 6c  rization.** call
162d0 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 77 69  back function wi
162e0 6c 6c 20 62 65 20 70 61 72 61 6d 65 74 65 72 73  ll be parameters
162f0 20 6f 72 20 4e 55 4c 4c 20 64 65 70 65 6e 64 69   or NULL dependi
16300 6e 67 20 6f 6e 20 77 68 69 63 68 20 6f 66 20 74  ng on which of t
16310 68 65 73 65 0a 2a 2a 20 63 6f 64 65 73 20 69 73  hese.** codes is
16320 20 75 73 65 64 20 61 73 20 74 68 65 20 73 65 63   used as the sec
16330 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20  ond parameter.  
16340 54 68 65 20 35 74 68 20 70 61 72 61 6d 65 74 65  The 5th paramete
16350 72 20 74 6f 20 74 68 65 0a 2a 2a 20 61 75 74 68  r to the.** auth
16360 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20  orizer callback 
16370 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
16380 68 65 20 64 61 74 61 62 61 73 65 20 28 22 6d 61  he database ("ma
16390 69 6e 22 2c 20 22 74 65 6d 70 22 2c 20 0a 2a 2a  in", "temp", .**
163a0 20 65 74 63 2e 29 20 69 66 20 61 70 70 6c 69 63   etc.) if applic
163b0 61 62 6c 65 2e 20 20 54 68 65 20 36 74 68 20 70  able.  The 6th p
163c0 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20  arameter to the 
163d0 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62  authorizer callb
163e0 61 63 6b 0a 2a 2a 20 69 73 20 74 68 65 20 6e 61  ack.** is the na
163f0 6d 65 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 2d  me of the inner-
16400 6d 6f 73 74 20 74 72 69 67 67 65 72 20 6f 72 20  most trigger or 
16410 76 69 65 77 20 74 68 61 74 20 69 73 20 72 65 73  view that is res
16420 70 6f 6e 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20  ponsible for.** 
16430 74 68 65 20 61 63 63 65 73 73 20 61 74 74 65 6d  the access attem
16440 70 74 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68  pt or NULL if th
16450 69 73 20 61 63 63 65 73 73 20 61 74 74 65 6d 70  is access attemp
16460 74 20 69 73 20 64 69 72 65 63 74 6c 79 20 66 72  t is directly fr
16470 6f 6d 20 0a 2a 2a 20 74 6f 70 2d 6c 65 76 65 6c  om .** top-level
16480 20 53 51 4c 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a   SQL code..**.**
16490 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a   INVARIANTS:.**.
164a0 2a 2a 20 7b 46 31 32 35 35 31 7d 20 54 68 65 20  ** {F12551} The 
164b0 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
164c0 20 74 6f 20 61 6e 20 0a 2a 2a 20 20 20 20 20 20   to an .**      
164d0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 73 65 74      [sqlite3_set
164e0 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 75  _authorizer | au
164f0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
16500 6b 20 69 73 20 61 6c 77 61 79 73 20 61 6e 20 69  k is always an i
16510 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20  nteger.**       
16520 20 20 20 5b 53 51 4c 49 54 45 5f 43 4f 50 59 20     [SQLITE_COPY 
16530 7c 20 61 75 74 68 6f 72 69 7a 65 72 20 63 6f 64  | authorizer cod
16540 65 5d 20 74 68 61 74 20 73 70 65 63 69 66 69 65  e] that specifie
16550 73 20 77 68 61 74 20 61 63 74 69 6f 6e 0a 2a 2a  s what action.**
16560 20 20 20 20 20 20 20 20 20 20 69 73 20 62 65 69            is bei
16570 6e 67 20 61 75 74 68 6f 72 69 7a 65 64 2e 0a 2a  ng authorized..*
16580 2a 0a 2a 2a 20 7b 46 31 32 35 35 32 7d 20 54 68  *.** {F12552} Th
16590 65 20 33 72 64 20 61 6e 64 20 34 74 68 20 70 61  e 3rd and 4th pa
165a0 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 65 20  rameters to the 
165b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71  .**          [sq
165c0 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72  lite3_set_author
165d0 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 61  izer | authoriza
165e0 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 66 75  tion callback fu
165f0 6e 63 74 69 6f 6e 5d 0a 2a 2a 20 20 20 20 20 20  nction].**      
16600 20 20 20 20 77 69 6c 6c 20 62 65 20 70 61 72 61      will be para
16610 6d 65 74 65 72 73 20 6f 72 20 4e 55 4c 4c 20 64  meters or NULL d
16620 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 69 63  epending on whic
16630 68 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  h .**          [
16640 53 51 4c 49 54 45 5f 43 4f 50 59 20 7c 20 61 75  SQLITE_COPY | au
16650 74 68 6f 72 69 7a 65 72 20 63 6f 64 65 5d 20 69  thorizer code] i
16660 73 20 75 73 65 64 20 61 73 20 74 68 65 20 73 65  s used as the se
16670 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 0a  cond parameter..
16680 2a 2a 0a 2a 2a 20 7b 46 31 32 35 35 33 7d 20 54  **.** {F12553} T
16690 68 65 20 35 74 68 20 70 61 72 61 6d 65 74 65 72  he 5th parameter
166a0 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   to the.**      
166b0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 73 65 74      [sqlite3_set
166c0 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 75  _authorizer | au
166d0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
166e0 6b 5d 20 69 73 20 74 68 65 20 6e 61 6d 65 0a 2a  k] is the name.*
166f0 2a 20 20 20 20 20 20 20 20 20 20 6f 66 20 74 68  *          of th
16700 65 20 64 61 74 61 62 61 73 65 20 28 65 78 61 6d  e database (exam
16710 70 6c 65 3a 20 22 6d 61 69 6e 22 2c 20 22 74 65  ple: "main", "te
16720 6d 70 22 2c 20 65 74 63 2e 29 20 69 66 20 61 70  mp", etc.) if ap
16730 70 6c 69 63 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  plicable..**.** 
16740 7b 46 31 32 35 35 34 7d 20 54 68 65 20 36 74 68  {F12554} The 6th
16750 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68   parameter to th
16760 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73  e.**          [s
16770 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f  qlite3_set_autho
16780 72 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a  rizer | authoriz
16790 65 72 20 63 61 6c 6c 62 61 63 6b 5d 20 69 73 20  er callback] is 
167a0 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 20 20  the name.**     
167b0 20 20 20 20 20 6f 66 20 74 68 65 20 69 6e 6e 65       of the inne
167c0 72 2d 6d 6f 73 74 20 74 72 69 67 67 65 72 20 6f  r-most trigger o
167d0 72 20 76 69 65 77 20 74 68 61 74 20 69 73 20 72  r view that is r
167e0 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 0a 2a  esponsible for.*
167f0 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 61  *          the a
16800 63 63 65 73 73 20 61 74 74 65 6d 70 74 20 6f 72  ccess attempt or
16810 20 4e 55 4c 4c 20 69 66 20 74 68 69 73 20 61 63   NULL if this ac
16820 63 65 73 73 20 61 74 74 65 6d 70 74 20 69 73 20  cess attempt is 
16830 64 69 72 65 63 74 6c 79 20 66 72 6f 6d 20 0a 2a  directly from .*
16840 2a 20 20 20 20 20 20 20 20 20 20 74 6f 70 2d 6c  *          top-l
16850 65 76 65 6c 20 53 51 4c 20 63 6f 64 65 2e 0a 2a  evel SQL code..*
16860 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
16870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 33  ************** 3
16890 72 64 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  rd ************ 
168a0 34 74 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  4th ***********/
168b0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
168c0 43 52 45 41 54 45 5f 49 4e 44 45 58 20 20 20 20  CREATE_INDEX    
168d0 20 20 20 20 20 20 31 20 20 20 2f 2a 20 49 6e 64        1   /* Ind
168e0 65 78 20 4e 61 6d 65 20 20 20 20 20 20 54 61 62  ex Name      Tab
168f0 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a  le Name      */.
16900 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
16910 52 45 41 54 45 5f 54 41 42 4c 45 20 20 20 20 20  REATE_TABLE     
16920 20 20 20 20 20 32 20 20 20 2f 2a 20 54 61 62 6c       2   /* Tabl
16930 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c  e Name      NULL
16940 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23              */.#
16950 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52  define SQLITE_CR
16960 45 41 54 45 5f 54 45 4d 50 5f 49 4e 44 45 58 20  EATE_TEMP_INDEX 
16970 20 20 20 20 33 20 20 20 2f 2a 20 49 6e 64 65 78      3   /* Index
16980 20 4e 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65   Name      Table
16990 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64   Name      */.#d
169a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45  efine SQLITE_CRE
169b0 41 54 45 5f 54 45 4d 50 5f 54 41 42 4c 45 20 20  ATE_TEMP_TABLE  
169c0 20 20 20 34 20 20 20 2f 2a 20 54 61 62 6c 65 20     4   /* Table 
169d0 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20  Name      NULL  
169e0 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65            */.#de
169f0 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41  fine SQLITE_CREA
16a00 54 45 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 20  TE_TEMP_TRIGGER 
16a10 20 20 35 20 20 20 2f 2a 20 54 72 69 67 67 65 72    5   /* Trigger
16a20 20 4e 61 6d 65 20 20 20 20 54 61 62 6c 65 20 4e   Name    Table N
16a30 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  ame      */.#def
16a40 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54  ine SQLITE_CREAT
16a50 45 5f 54 45 4d 50 5f 56 49 45 57 20 20 20 20 20  E_TEMP_VIEW     
16a60 20 36 20 20 20 2f 2a 20 56 69 65 77 20 4e 61 6d   6   /* View Nam
16a70 65 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20  e       NULL    
16a80 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
16a90 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45  ne SQLITE_CREATE
16aa0 5f 54 52 49 47 47 45 52 20 20 20 20 20 20 20 20  _TRIGGER        
16ab0 37 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e  7   /* Trigger N
16ac0 61 6d 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d  ame    Table Nam
16ad0 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  e      */.#defin
16ae0 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f  e SQLITE_CREATE_
16af0 56 49 45 57 20 20 20 20 20 20 20 20 20 20 20 38  VIEW           8
16b00 20 20 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20     /* View Name 
16b10 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20        NULL      
16b20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
16b30 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 20 20   SQLITE_DELETE  
16b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 20                9 
16b50 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20    /* Table Name 
16b60 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20       NULL       
16b70 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
16b80 53 51 4c 49 54 45 5f 44 52 4f 50 5f 49 4e 44 45  SQLITE_DROP_INDE
16b90 58 20 20 20 20 20 20 20 20 20 20 20 31 30 20 20  X           10  
16ba0 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20   /* Index Name  
16bb0 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20      Table Name  
16bc0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
16bd0 51 4c 49 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45  QLITE_DROP_TABLE
16be0 20 20 20 20 20 20 20 20 20 20 20 31 31 20 20 20             11   
16bf0 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20  /* Table Name   
16c00 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20     NULL         
16c10 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
16c20 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49  LITE_DROP_TEMP_I
16c30 4e 44 45 58 20 20 20 20 20 20 31 32 20 20 20 2f  NDEX      12   /
16c40 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20  * Index Name    
16c50 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20    Table Name    
16c60 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
16c70 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 41  ITE_DROP_TEMP_TA
16c80 42 4c 45 20 20 20 20 20 20 31 33 20 20 20 2f 2a  BLE      13   /*
16c90 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
16ca0 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20   NULL           
16cb0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
16cc0 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 52 49  TE_DROP_TEMP_TRI
16cd0 47 47 45 52 20 20 20 20 31 34 20 20 20 2f 2a 20  GGER    14   /* 
16ce0 54 72 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20  Trigger Name    
16cf0 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
16d00 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
16d10 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57  E_DROP_TEMP_VIEW
16d20 20 20 20 20 20 20 20 31 35 20 20 20 2f 2a 20 56         15   /* V
16d30 69 65 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e  iew Name       N
16d40 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a  ULL            *
16d50 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
16d60 5f 44 52 4f 50 5f 54 52 49 47 47 45 52 20 20 20  _DROP_TRIGGER   
16d70 20 20 20 20 20 20 31 36 20 20 20 2f 2a 20 54 72        16   /* Tr
16d80 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61  igger Name    Ta
16d90 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f  ble Name      */
16da0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
16db0 44 52 4f 50 5f 56 49 45 57 20 20 20 20 20 20 20  DROP_VIEW       
16dc0 20 20 20 20 20 31 37 20 20 20 2f 2a 20 56 69 65       17   /* Vie
16dd0 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c  w Name       NUL
16de0 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  L            */.
16df0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
16e00 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 20 20  NSERT           
16e10 20 20 20 20 31 38 20 20 20 2f 2a 20 54 61 62 6c      18   /* Tabl
16e20 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c  e Name      NULL
16e30 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23              */.#
16e40 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 52  define SQLITE_PR
16e50 41 47 4d 41 20 20 20 20 20 20 20 20 20 20 20 20  AGMA            
16e60 20 20 20 31 39 20 20 20 2f 2a 20 50 72 61 67 6d     19   /* Pragm
16e70 61 20 4e 61 6d 65 20 20 20 20 20 31 73 74 20 61  a Name     1st a
16e80 72 67 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 23 64  rg or NULL */.#d
16e90 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 41  efine SQLITE_REA
16ea0 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  D               
16eb0 20 20 32 30 20 20 20 2f 2a 20 54 61 62 6c 65 20    20   /* Table 
16ec0 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c 75 6d 6e  Name      Column
16ed0 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23 64 65   Name     */.#de
16ee0 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 45 4c 45  fine SQLITE_SELE
16ef0 43 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  CT              
16f00 20 32 31 20 20 20 2f 2a 20 4e 55 4c 4c 20 20 20   21   /* NULL   
16f10 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20           NULL   
16f20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66           */.#def
16f30 69 6e 65 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  ine SQLITE_TRANS
16f40 41 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20  ACTION          
16f50 32 32 20 20 20 2f 2a 20 4e 55 4c 4c 20 20 20 20  22   /* NULL    
16f60 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20          NULL    
16f70 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
16f80 6e 65 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45  ne SQLITE_UPDATE
16f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
16fa0 33 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d  3   /* Table Nam
16fb0 65 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 4e 61  e      Column Na
16fc0 6d 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  me     */.#defin
16fd0 65 20 53 51 4c 49 54 45 5f 41 54 54 41 43 48 20  e SQLITE_ATTACH 
16fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 34                24
16ff0 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 20     /* Filename  
17000 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20        NULL      
17010 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
17020 20 53 51 4c 49 54 45 5f 44 45 54 41 43 48 20 20   SQLITE_DETACH  
17030 20 20 20 20 20 20 20 20 20 20 20 20 20 32 35 20               25 
17040 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 4e 61    /* Database Na
17050 6d 65 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20  me   NULL       
17060 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
17070 53 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42  SQLITE_ALTER_TAB
17080 4c 45 20 20 20 20 20 20 20 20 20 20 32 36 20 20  LE          26  
17090 20 2f 2a 20 44 61 74 61 62 61 73 65 20 4e 61 6d   /* Database Nam
170a0 65 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20  e   Table Name  
170b0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
170c0 51 4c 49 54 45 5f 52 45 49 4e 44 45 58 20 20 20  QLITE_REINDEX   
170d0 20 20 20 20 20 20 20 20 20 20 20 32 37 20 20 20             27   
170e0 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20  /* Index Name   
170f0 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20     NULL         
17100 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
17110 4c 49 54 45 5f 41 4e 41 4c 59 5a 45 20 20 20 20  LITE_ANALYZE    
17120 20 20 20 20 20 20 20 20 20 20 32 38 20 20 20 2f            28   /
17130 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20  * Table Name    
17140 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20    NULL          
17150 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
17160 49 54 45 5f 43 52 45 41 54 45 5f 56 54 41 42 4c  ITE_CREATE_VTABL
17170 45 20 20 20 20 20 20 20 20 32 39 20 20 20 2f 2a  E        29   /*
17180 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
17190 20 4d 6f 64 75 6c 65 20 4e 61 6d 65 20 20 20 20   Module Name    
171a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
171b0 54 45 5f 44 52 4f 50 5f 56 54 41 42 4c 45 20 20  TE_DROP_VTABLE  
171c0 20 20 20 20 20 20 20 20 33 30 20 20 20 2f 2a 20          30   /* 
171d0 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
171e0 4d 6f 64 75 6c 65 20 4e 61 6d 65 20 20 20 20 20  Module Name     
171f0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
17200 45 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20  E_FUNCTION      
17210 20 20 20 20 20 20 20 33 31 20 20 20 2f 2a 20 46         31   /* F
17220 75 6e 63 74 69 6f 6e 20 4e 61 6d 65 20 20 20 4e  unction Name   N
17230 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a  ULL            *
17240 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
17250 5f 43 4f 50 59 20 20 20 20 20 20 20 20 20 20 20  _COPY           
17260 20 20 20 20 20 20 20 30 20 20 20 2f 2a 20 4e 6f         0   /* No
17270 20 6c 6f 6e 67 65 72 20 75 73 65 64 20 2a 2f 0a   longer used */.
17280 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
17290 20 54 72 61 63 69 6e 67 20 41 6e 64 20 50 72 6f   Tracing And Pro
172a0 66 69 6c 69 6e 67 20 46 75 6e 63 74 69 6f 6e 73  filing Functions
172b0 20 7b 46 31 32 32 38 30 7d 0a 2a 2a 0a 2a 2a 20   {F12280}.**.** 
172c0 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72  These routines r
172d0 65 67 69 73 74 65 72 20 63 61 6c 6c 62 61 63 6b  egister callback
172e0 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20   functions that 
172f0 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 0a  can be used for.
17300 2a 2a 20 74 72 61 63 69 6e 67 20 61 6e 64 20 70  ** tracing and p
17310 72 6f 66 69 6c 69 6e 67 20 74 68 65 20 65 78 65  rofiling the exe
17320 63 75 74 69 6f 6e 20 6f 66 20 53 51 4c 20 73 74  cution of SQL st
17330 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20  atements..**.** 
17340 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  The callback fun
17350 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64  ction registered
17360 20 62 79 20 73 71 6c 69 74 65 33 5f 74 72 61 63   by sqlite3_trac
17370 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 61  e() is invoked a
17380 74 0a 2a 2a 20 76 61 72 69 6f 75 73 20 74 69 6d  t.** various tim
17390 65 73 20 77 68 65 6e 20 61 6e 20 53 51 4c 20 73  es when an SQL s
173a0 74 61 74 65 6d 65 6e 74 20 69 73 20 62 65 69 6e  tatement is bein
173b0 67 20 72 75 6e 20 62 79 20 5b 73 71 6c 69 74 65  g run by [sqlite
173c0 33 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 20 54 68  3_step()]..** Th
173d0 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72  e callback retur
173e0 6e 73 20 61 20 55 54 46 2d 38 20 72 65 6e 64 65  ns a UTF-8 rende
173f0 72 69 6e 67 20 6f 66 20 74 68 65 20 53 51 4c 20  ring of the SQL 
17400 73 74 61 74 65 6d 65 6e 74 20 74 65 78 74 0a 2a  statement text.*
17410 2a 20 61 73 20 74 68 65 20 73 74 61 74 65 6d 65  * as the stateme
17420 6e 74 20 66 69 72 73 74 20 62 65 67 69 6e 73 20  nt first begins 
17430 65 78 65 63 75 74 69 6e 67 2e 20 20 41 64 64 69  executing.  Addi
17440 74 69 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73  tional callbacks
17450 20 6f 63 63 75 72 0a 2a 2a 20 61 73 20 65 61 63   occur.** as eac
17460 68 20 74 72 69 67 67 65 72 73 75 62 70 72 6f 67  h triggersubprog
17470 72 61 6d 20 69 73 20 65 6e 74 65 72 65 64 2e 20  ram is entered. 
17480 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 73 20 66   The callbacks f
17490 6f 72 20 74 72 69 67 67 65 72 73 0a 2a 2a 20 63  or triggers.** c
174a0 6f 6e 74 61 69 6e 20 61 20 55 54 46 2d 38 20 53  ontain a UTF-8 S
174b0 51 4c 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74 20  QL comment that 
174c0 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 74  identifies the t
174d0 72 69 67 67 65 72 2e 0a 2a 2a 20 0a 2a 2a 20 54  rigger..** .** T
174e0 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  he callback func
174f0 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20  tion registered 
17500 62 79 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69  by sqlite3_profi
17510 6c 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a  le() is invoked.
17520 2a 2a 20 61 73 20 65 61 63 68 20 53 51 4c 20 73  ** as each SQL s
17530 74 61 74 65 6d 65 6e 74 20 66 69 6e 69 73 68 65  tatement finishe
17540 73 2e 20 20 54 68 65 20 70 72 6f 66 69 6c 65 20  s.  The profile 
17550 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e  callback contain
17560 73 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61  s.** the origina
17570 6c 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78 74  l statement text
17580 20 61 6e 64 20 61 6e 20 65 73 74 69 6d 61 74 65   and an estimate
17590 20 6f 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 74   of wall-clock t
175a0 69 6d 65 0a 2a 2a 20 6f 66 20 68 6f 77 20 6c 6f  ime.** of how lo
175b0 6e 67 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e  ng that statemen
175c0 74 20 74 6f 6f 6b 20 74 6f 20 72 75 6e 2e 0a 2a  t took to run..*
175d0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
175e0 5f 70 72 6f 66 69 6c 65 28 29 20 41 50 49 20 69  _profile() API i
175f0 73 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73  s currently cons
17600 69 64 65 72 65 64 20 65 78 70 65 72 69 6d 65 6e  idered experimen
17610 74 61 6c 20 61 6e 64 0a 2a 2a 20 69 73 20 73 75  tal and.** is su
17620 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20  bject to change 
17630 6f 72 20 72 65 6d 6f 76 61 6c 20 69 6e 20 61 20  or removal in a 
17640 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 2e 0a  future release..
17650 2a 2a 0a 2a 2a 20 54 68 65 20 74 72 69 67 67 65  **.** The trigge
17660 72 20 72 65 70 6f 72 74 69 6e 67 20 66 65 61 74  r reporting feat
17670 75 72 65 20 6f 66 20 74 68 65 20 74 72 61 63 65  ure of the trace
17680 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 6f 6e   callback is con
17690 73 69 64 65 72 65 64 0a 2a 2a 20 65 78 70 65 72  sidered.** exper
176a0 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 73  imental and is s
176b0 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65  ubject to change
176c0 20 6f 72 20 72 65 6d 6f 76 61 6c 20 69 6e 20 66   or removal in f
176d0 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 2e 0a  uture releases..
176e0 2a 2a 20 46 75 74 75 72 65 20 76 65 72 73 69 6f  ** Future versio
176f0 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 6d 69 67  ns of SQLite mig
17700 68 74 20 61 6c 73 6f 20 61 64 64 20 6e 65 77 20  ht also add new 
17710 74 72 61 63 65 20 63 61 6c 6c 62 61 63 6b 20 0a  trace callback .
17720 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a  ** invocations..
17730 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53  **.** INVARIANTS
17740 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 32 38 31 7d  :.**.** {F12281}
17750 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75   The callback fu
17760 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65  nction registere
17770 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 74 72  d by [sqlite3_tr
17780 61 63 65 28 29 5d 20 69 73 0a 2a 2a 20 20 20 20  ace()] is.**    
17790 20 20 20 20 20 20 77 68 65 6e 65 76 65 72 20 61        whenever a
177a0 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  n SQL statement 
177b0 66 69 72 73 74 20 62 65 67 69 6e 73 20 74 6f 20  first begins to 
177c0 65 78 65 63 75 74 65 20 61 6e 64 0a 2a 2a 20 20  execute and.**  
177d0 20 20 20 20 20 20 20 20 77 68 65 6e 65 76 65 72          whenever
177e0 20 61 20 74 72 69 67 67 65 72 20 73 75 62 70 72   a trigger subpr
177f0 6f 67 72 61 6d 20 66 69 72 73 74 20 62 65 67 69  ogram first begi
17800 6e 73 20 74 6f 20 72 75 6e 2e 0a 2a 2a 0a 2a 2a  ns to run..**.**
17810 20 7b 46 31 32 32 38 32 7d 20 45 61 63 68 20 63   {F12282} Each c
17820 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
17830 74 72 61 63 65 28 29 5d 20 6f 76 65 72 72 69 64  trace()] overrid
17840 65 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c  es the previousl
17850 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65  y.**          re
17860 67 69 73 74 65 72 65 64 20 74 72 61 63 65 20 63  gistered trace c
17870 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 7b  allback..**.** {
17880 46 31 32 32 38 33 7d 20 41 20 4e 55 4c 4c 20 74  F12283} A NULL t
17890 72 61 63 65 20 63 61 6c 6c 62 61 63 6b 20 64 69  race callback di
178a0 73 61 62 6c 65 73 20 74 72 61 63 69 6e 67 2e 0a  sables tracing..
178b0 2a 2a 0a 2a 2a 20 7b 46 31 32 32 38 34 7d 20 54  **.** {F12284} T
178c0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
178d0 74 20 74 6f 20 74 68 65 20 74 72 61 63 65 20 63  t to the trace c
178e0 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 63 6f 70  allback is a cop
178f0 79 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20  y of.**         
17900 20 74 68 65 20 70 6f 69 6e 74 65 72 20 77 68 69   the pointer whi
17910 63 68 20 77 61 73 20 74 68 65 20 33 72 64 20 61  ch was the 3rd a
17920 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c 69  rgument to [sqli
17930 74 65 33 5f 74 72 61 63 65 28 29 5d 2e 0a 2a 2a  te3_trace()]..**
17940 0a 2a 2a 20 7b 46 31 32 32 38 35 7d 20 54 68 65  .** {F12285} The
17950 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
17960 20 74 6f 20 74 68 65 20 74 72 61 63 65 20 63 61   to the trace ca
17970 6c 6c 62 61 63 6b 20 69 73 20 61 0a 2a 2a 20 20  llback is a.**  
17980 20 20 20 20 20 20 20 20 7a 65 72 6f 2d 74 65 72          zero-ter
17990 6d 69 6e 61 74 65 64 20 55 54 46 38 20 73 74 72  minated UTF8 str
179a0 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ing containing t
179b0 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 65 78 74  he original text
179c0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 66 20  .**          of 
179d0 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
179e0 74 20 61 73 20 69 74 20 77 61 73 20 70 61 73 73  t as it was pass
179f0 65 64 20 69 6e 74 6f 20 5b 73 71 6c 69 74 65 33  ed into [sqlite3
17a00 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 0a 2a  _prepare_v2()].*
17a10 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 74 68  *          or th
17a20 65 20 65 71 75 69 76 61 6c 65 6e 74 2c 20 6f 72  e equivalent, or
17a30 20 61 6e 20 53 51 4c 20 63 6f 6d 6d 65 6e 74 20   an SQL comment 
17a40 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 62  indicating the b
17a50 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 20 20 20 20  eginning.**     
17a60 20 20 20 20 20 6f 66 20 61 20 74 72 69 67 67 65       of a trigge
17a70 72 20 73 75 62 70 72 6f 67 72 61 6d 2e 0a 2a 2a  r subprogram..**
17a80 0a 2a 2a 20 7b 46 31 32 32 38 37 7d 20 54 68 65  .** {F12287} The
17a90 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
17aa0 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 62 79  on registered by
17ab0 20 5b 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c   [sqlite3_profil
17ac0 65 28 29 5d 20 69 73 20 69 6e 76 6f 6b 65 64 0a  e()] is invoked.
17ad0 2a 2a 20 20 20 20 20 20 20 20 20 20 61 73 20 65  **          as e
17ae0 61 63 68 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ach SQL statemen
17af0 74 20 66 69 6e 69 73 68 65 73 2e 0a 2a 2a 0a 2a  t finishes..**.*
17b00 2a 20 7b 46 31 32 32 38 38 7d 20 54 68 65 20 66  * {F12288} The f
17b10 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 74  irst parameter t
17b20 6f 20 74 68 65 20 70 72 6f 66 69 6c 65 20 63 61  o the profile ca
17b30 6c 6c 62 61 63 6b 20 69 73 20 61 20 63 6f 70 79  llback is a copy
17b40 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   of.**          
17b50 74 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 65  the 3rd paramete
17b60 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72  r to [sqlite3_pr
17b70 6f 66 69 6c 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  ofile()]..**.** 
17b80 7b 46 31 32 32 38 39 7d 20 54 68 65 20 73 65 63  {F12289} The sec
17b90 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ond parameter to
17ba0 20 74 68 65 20 70 72 6f 66 69 6c 65 20 63 61 6c   the profile cal
17bb0 6c 62 61 63 6b 20 69 73 20 61 0a 2a 2a 20 20 20  lback is a.**   
17bc0 20 20 20 20 20 20 20 7a 65 72 6f 2d 74 65 72 6d         zero-term
17bd0 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 72  inated UTF-8 str
17be0 69 6e 67 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ing that contain
17bf0 73 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74  s the complete t
17c00 65 78 74 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20  ext of.**       
17c10 20 20 20 74 68 65 20 53 51 4c 20 73 74 61 74 65     the SQL state
17c20 6d 65 6e 74 20 61 73 20 69 74 20 77 61 73 20 70  ment as it was p
17c30 72 6f 63 65 73 73 65 64 20 62 79 20 5b 73 71 6c  rocessed by [sql
17c40 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
17c50 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  )].**          o
17c60 72 20 74 68 65 20 65 71 75 69 76 61 6c 65 6e 74  r the equivalent
17c70 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 32 39 30 7d  ..**.** {F12290}
17c80 20 54 68 65 20 74 68 69 72 64 20 70 61 72 61 6d   The third param
17c90 65 74 65 72 20 74 6f 20 74 68 65 20 70 72 6f 66  eter to the prof
17ca0 69 6c 65 20 20 63 61 6c 6c 62 61 63 6b 20 69 73  ile  callback is
17cb0 20 61 6e 20 65 73 74 69 6d 61 74 65 0a 2a 2a 20   an estimate.** 
17cc0 20 20 20 20 20 20 20 20 20 6f 66 20 74 68 65 20           of the 
17cd0 6e 75 6d 62 65 72 20 6f 66 20 6e 61 6e 6f 73 65  number of nanose
17ce0 63 6f 6e 64 73 20 6f 66 20 77 61 6c 6c 2d 63 6c  conds of wall-cl
17cf0 6f 63 6b 20 74 69 6d 65 20 72 65 71 75 69 72 65  ock time require
17d00 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20  d to.**         
17d10 20 72 75 6e 20 74 68 65 20 53 51 4c 20 73 74 61   run the SQL sta
17d20 74 65 6d 65 6e 74 20 66 72 6f 6d 20 73 74 61 72  tement from star
17d30 74 20 74 6f 20 66 69 6e 69 73 68 2e 0a 2a 2f 0a  t to finish..*/.
17d40 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
17d50 2a 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 73  *sqlite3_trace(s
17d60 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a 78  qlite3*, void(*x
17d70 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e  Trace)(void*,con
17d80 73 74 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 2a  st char*), void*
17d90 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  );.SQLITE_API vo
17da0 69 64 20 2a 73 71 6c 69 74 65 33 5f 70 72 6f 66  id *sqlite3_prof
17db0 69 6c 65 28 73 71 6c 69 74 65 33 2a 2c 0a 20 20  ile(sqlite3*,.  
17dc0 20 76 6f 69 64 28 2a 78 50 72 6f 66 69 6c 65 29   void(*xProfile)
17dd0 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61  (void*,const cha
17de0 72 2a 2c 73 71 6c 69 74 65 33 5f 75 69 6e 74 36  r*,sqlite3_uint6
17df0 34 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a  4), void*);../*.
17e00 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 51 75 65  ** CAPI3REF: Que
17e10 72 79 20 50 72 6f 67 72 65 73 73 20 43 61 6c 6c  ry Progress Call
17e20 62 61 63 6b 73 20 7b 46 31 32 39 31 30 7d 0a 2a  backs {F12910}.*
17e30 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
17e40 65 20 63 6f 6e 66 69 67 75 72 65 73 20 61 20 63  e configures a c
17e50 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
17e60 20 2d 20 74 68 65 0a 2a 2a 20 70 72 6f 67 72 65   - the.** progre
17e70 73 73 20 63 61 6c 6c 62 61 63 6b 20 2d 20 74 68  ss callback - th
17e80 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 70 65  at is invoked pe
17e90 72 69 6f 64 69 63 61 6c 6c 79 20 64 75 72 69 6e  riodically durin
17ea0 67 20 6c 6f 6e 67 0a 2a 2a 20 72 75 6e 6e 69 6e  g long.** runnin
17eb0 67 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69  g calls to [sqli
17ec0 74 65 33 5f 65 78 65 63 28 29 5d 2c 20 5b 73 71  te3_exec()], [sq
17ed0 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 61 6e  lite3_step()] an
17ee0 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 67 65  d.** [sqlite3_ge
17ef0 74 5f 74 61 62 6c 65 28 29 5d 2e 20 20 20 41 6e  t_table()].   An
17f00 20 65 78 61 6d 70 6c 65 20 75 73 65 20 66 6f 72   example use for
17f10 20 74 68 69 73 20 0a 2a 2a 20 69 6e 74 65 72 66   this .** interf
17f20 61 63 65 20 69 73 20 74 6f 20 6b 65 65 70 20 61  ace is to keep a
17f30 20 47 55 49 20 75 70 64 61 74 65 64 20 64 75 72   GUI updated dur
17f40 69 6e 67 20 61 20 6c 61 72 67 65 20 71 75 65 72  ing a large quer
17f50 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  y..**.** If the 
17f60 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63  progress callbac
17f70 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65  k returns non-ze
17f80 72 6f 2c 20 74 68 65 20 6f 70 65 72 74 69 6f 6e  ro, the opertion
17f90 20 69 73 0a 2a 2a 20 69 6e 74 65 72 72 75 70 74   is.** interrupt
17fa0 65 64 2e 20 20 54 68 69 73 20 66 65 61 74 75 72  ed.  This featur
17fb0 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  e can be used to
17fc0 20 69 6d 70 6c 65 6d 65 6e 74 20 61 0a 2a 2a 20   implement a.** 
17fd0 22 43 61 6e 63 65 6c 22 20 62 75 74 74 6f 6e 20  "Cancel" button 
17fe0 6f 6e 20 61 20 47 55 49 20 64 69 61 6c 6f 67 20  on a GUI dialog 
17ff0 62 6f 78 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52  box..**.** INVAR
18000 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31  IANTS:.**.** {F1
18010 32 39 31 31 7d 20 54 68 65 20 63 61 6c 6c 62 61  2911} The callba
18020 63 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69  ck function regi
18030 73 74 65 72 65 64 20 62 79 20 5b 73 71 6c 69 74  stered by [sqlit
18040 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e 64  e3_progress_hand
18050 6c 65 72 28 29 5d 0a 2a 2a 20 20 20 20 20 20 20  ler()].**       
18060 20 20 20 69 73 20 69 6e 76 6f 6b 65 64 20 70 65     is invoked pe
18070 72 69 6f 64 69 63 61 6c 6c 79 20 64 75 72 69 6e  riodically durin
18080 67 20 6c 6f 6e 67 20 72 75 6e 6e 69 6e 67 20 63  g long running c
18090 61 6c 6c 73 20 74 6f 0a 2a 2a 20 20 20 20 20 20  alls to.**      
180a0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 73 74 65      [sqlite3_ste
180b0 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32  p()]..**.** {F12
180c0 39 31 32 7d 20 54 68 65 20 70 72 6f 67 72 65 73  912} The progres
180d0 73 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e  s callback is in
180e0 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72 20 65  voked once for e
180f0 76 65 72 79 20 4e 20 76 69 72 74 75 61 6c 0a 2a  very N virtual.*
18100 2a 20 20 20 20 20 20 20 20 20 20 6d 61 63 68 69  *          machi
18110 6e 65 20 6f 70 63 6f 64 65 73 2c 20 77 68 65 72  ne opcodes, wher
18120 65 20 4e 20 69 73 20 74 68 65 20 73 65 63 6f 6e  e N is the secon
18130 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 0a 2a  d argument to .*
18140 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 5b  *          the [
18150 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73  sqlite3_progress
18160 5f 68 61 6e 64 6c 65 72 28 29 5d 20 63 61 6c 6c  _handler()] call
18170 20 74 68 61 74 20 72 65 67 69 73 74 65 72 65 64   that registered
18180 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65  .**          the
18190 20 63 61 6c 6c 62 61 63 6b 2e 20 20 3c 74 6f 64   callback.  <tod
181a0 6f 3e 57 68 61 74 20 69 66 20 4e 20 69 73 20 6c  o>What if N is l
181b0 65 73 73 20 74 68 61 6e 20 31 3f 3c 2f 74 6f 64  ess than 1?</tod
181c0 6f 3e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 39 31 33  o>.**.** {F12913
181d0 7d 20 54 68 65 20 70 72 6f 67 72 65 73 73 20 63  } The progress c
181e0 61 6c 6c 62 61 63 6b 20 69 74 73 65 6c 66 20 69  allback itself i
181f0 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20  s identified by 
18200 74 68 65 20 74 68 69 72 64 0a 2a 2a 20 20 20 20  the third.**    
18210 20 20 20 20 20 20 61 72 67 75 6d 65 6e 74 20 74        argument t
18220 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72 6f 67 72  o [sqlite3_progr
18230 65 73 73 5f 68 61 6e 64 6c 65 72 28 29 5d 2e 0a  ess_handler()]..
18240 2a 2a 0a 2a 2a 20 7b 46 31 32 39 31 34 7d 20 54  **.** {F12914} T
18250 68 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65  he fourth argume
18260 6e 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 6f 67  nt [sqlite3_prog
18270 72 65 73 73 5f 68 61 6e 64 6c 65 72 28 29 5d 20  ress_handler()] 
18280 69 73 20 61 0a 2a 2a 2a 20 20 20 20 20 20 20 20  is a.***        
18290 20 76 6f 69 64 20 70 6f 69 6e 74 65 72 20 70 61   void pointer pa
182a0 73 73 65 64 20 74 6f 20 74 68 65 20 70 72 6f 67  ssed to the prog
182b0 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a  ress callback.**
182c0 20 20 20 20 20 20 20 20 20 20 66 75 6e 63 74 69            functi
182d0 6f 6e 20 65 61 63 68 20 74 69 6d 65 20 69 74 20  on each time it 
182e0 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a  is invoked..**.*
182f0 2a 20 7b 46 31 32 39 31 35 7d 20 49 66 20 61 20  * {F12915} If a 
18300 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
18310 5f 73 74 65 70 28 29 5d 20 72 65 73 75 6c 74 73  _step()] results
18320 20 69 6e 20 66 65 77 65 72 20 74 68 61 6e 0a 2a   in fewer than.*
18330 2a 20 20 20 20 20 20 20 20 20 20 4e 20 6f 70 63  *          N opc
18340 6f 64 65 73 20 62 65 69 6e 67 20 65 78 65 63 75  odes being execu
18350 74 65 64 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20  ted,.**         
18360 20 74 68 65 6e 20 74 68 65 20 70 72 6f 67 72 65   then the progre
18370 73 73 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e  ss callback is n
18380 65 76 65 72 20 69 6e 76 6f 6b 65 64 2e 20 7b 45  ever invoked. {E
18390 4e 44 7d 0a 2a 2a 20 0a 2a 2a 20 7b 46 31 32 39  ND}.** .** {F129
183a0 31 36 7d 20 45 76 65 72 79 20 63 61 6c 6c 20 74  16} Every call t
183b0 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72 6f 67 72  o [sqlite3_progr
183c0 65 73 73 5f 68 61 6e 64 6c 65 72 28 29 5d 0a 2a  ess_handler()].*
183d0 2a 20 20 20 20 20 20 20 20 20 20 6f 76 65 72 77  *          overw
183e0 72 69 74 65 73 20 61 6e 79 20 70 72 65 76 69 6f  rites any previo
183f0 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 20 70  usly registere p
18400 72 6f 67 72 65 73 73 20 68 61 6e 64 6c 65 72 2e  rogress handler.
18410 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 39 31 37 7d 20  .**.** {F12917} 
18420 49 66 20 74 68 65 20 70 72 6f 67 72 65 73 73 20  If the progress 
18430 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b  handler callback
18440 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 6e 6f   is NULL then no
18450 20 70 72 6f 67 72 65 73 73 0a 2a 2a 20 20 20 20   progress.**    
18460 20 20 20 20 20 20 68 61 6e 64 6c 65 72 20 69 73        handler is
18470 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20   invoked..**.** 
18480 7b 46 31 32 39 31 38 7d 20 49 66 20 74 68 65 20  {F12918} If the 
18490 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63  progress callbac
184a0 6b 20 72 65 74 75 72 6e 73 20 61 20 72 65 73 75  k returns a resu
184b0 6c 74 20 6f 74 68 65 72 20 74 68 61 6e 20 30 2c  lt other than 0,
184c0 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20   then.**        
184d0 20 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69    the behavior i
184e0 73 20 61 20 69 66 20 5b 73 71 6c 69 74 65 33 5f  s a if [sqlite3_
184f0 69 6e 74 65 72 72 75 70 74 28 29 5d 20 68 61 64  interrupt()] had
18500 20 62 65 65 6e 20 63 61 6c 6c 65 64 2e 0a 2a 2f   been called..*/
18510 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
18520 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73   sqlite3_progres
18530 73 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65  s_handler(sqlite
18540 33 2a 2c 20 69 6e 74 2c 20 69 6e 74 28 2a 29 28  3*, int, int(*)(
18550 76 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a  void*), void*);.
18560 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
18570 20 4f 70 65 6e 69 6e 67 20 41 20 4e 65 77 20 44   Opening A New D
18580 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69  atabase Connecti
18590 6f 6e 20 7b 46 31 32 37 30 30 7d 0a 2a 2a 0a 2a  on {F12700}.**.*
185a0 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
185b0 20 6f 70 65 6e 20 61 6e 20 53 51 4c 69 74 65 20   open an SQLite 
185c0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 68  database file wh
185d0 6f 73 65 20 6e 61 6d 65 0a 2a 2a 20 69 73 20 67  ose name.** is g
185e0 69 76 65 6e 20 62 79 20 74 68 65 20 66 69 6c 65  iven by the file
185f0 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a  name argument..*
18600 2a 20 54 68 65 20 66 69 6c 65 6e 61 6d 65 20 61  * The filename a
18610 72 67 75 6d 65 6e 74 20 69 73 20 69 6e 74 65 72  rgument is inter
18620 70 72 65 74 65 64 20 61 73 20 55 54 46 2d 38 0a  preted as UTF-8.
18630 2a 2a 20 66 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ** for [sqlite3_
18640 6f 70 65 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c  open()] and [sql
18650 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20  ite3_open_v2()] 
18660 61 6e 64 20 61 73 20 55 54 46 2d 31 36 0a 2a 2a  and as UTF-16.**
18670 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62   in the native b
18680 79 74 65 20 6f 72 64 65 72 20 66 6f 72 20 5b 73  yte order for [s
18690 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d  qlite3_open16()]
186a0 2e 0a 2a 2a 20 41 6e 20 5b 73 71 6c 69 74 65 33  ..** An [sqlite3
186b0 2a 5d 20 68 61 6e 64 6c 65 20 69 73 20 75 73 75  *] handle is usu
186c0 61 6c 6c 79 20 72 65 74 75 72 6e 65 64 20 69 6e  ally returned in
186d0 20 2a 70 70 44 62 2c 20 65 76 65 6e 0a 2a 2a 20   *ppDb, even.** 
186e0 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  if an error occu
186f0 72 73 2e 20 20 54 68 65 20 6f 6e 6c 79 20 65 78  rs.  The only ex
18700 63 65 70 74 69 6f 6e 20 69 73 20 69 66 20 53 51  ception is if SQ
18710 4c 69 74 65 20 69 73 20 75 6e 61 62 6c 65 0a 2a  Lite is unable.*
18720 2a 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65  * to allocate me
18730 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65  mory to hold the
18740 20 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63   [sqlite3] objec
18750 74 2c 20 61 20 4e 55 4c 4c 20 77 69 6c 6c 0a 2a  t, a NULL will.*
18760 2a 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74  * be written int
18770 6f 20 2a 70 70 44 62 20 69 6e 73 74 65 61 64 20  o *ppDb instead 
18780 6f 66 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  of a pointer to 
18790 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 20 6f 62  the [sqlite3] ob
187a0 6a 65 63 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20  ject..** If the 
187b0 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e  database is open
187c0 65 64 20 28 61 6e 64 2f 6f 72 20 63 72 65 61 74  ed (and/or creat
187d0 65 64 29 0a 2a 2a 20 73 75 63 63 65 73 73 66 75  ed).** successfu
187e0 6c 6c 79 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54  lly, then [SQLIT
187f0 45 5f 4f 4b 5d 20 69 73 20 72 65 74 75 72 6e 65  E_OK] is returne
18800 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 61 6e  d.  Otherwise an
18810 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 69  .** error code i
18820 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65  s returned.  The
18830 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 72 72  .** [sqlite3_err
18840 6d 73 67 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74  msg()] or [sqlit
18850 65 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 20 20  e3_errmsg16()]  
18860 72 6f 75 74 69 6e 65 73 20 63 61 6e 20 62 65 20  routines can be 
18870 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e 0a 2a  used to obtain.*
18880 2a 20 61 6e 20 45 6e 67 6c 69 73 68 20 6c 61 6e  * an English lan
18890 67 75 61 67 65 20 64 65 73 63 72 69 70 74 69 6f  guage descriptio
188a0 6e 20 6f 66 20 74 68 65 20 65 72 72 6f 72 2e 0a  n of the error..
188b0 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c  **.** The defaul
188c0 74 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74  t encoding for t
188d0 68 65 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c  he database will
188e0 20 62 65 20 55 54 46 2d 38 20 69 66 0a 2a 2a 20   be UTF-8 if.** 
188f0 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d  [sqlite3_open()]
18900 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65   or [sqlite3_ope
18910 6e 5f 76 32 28 29 5d 20 69 73 20 63 61 6c 6c 65  n_v2()] is calle
18920 64 20 61 6e 64 0a 2a 2a 20 55 54 46 2d 31 36 20  d and.** UTF-16 
18930 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79  in the native by
18940 74 65 20 6f 72 64 65 72 20 69 66 20 5b 73 71 6c  te order if [sql
18950 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 20 69  ite3_open16()] i
18960 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68  s used..**.** Wh
18970 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 6e 20  ether or not an 
18980 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 65  error occurs whe
18990 6e 20 69 74 20 69 73 20 6f 70 65 6e 65 64 2c 20  n it is opened, 
189a0 72 65 73 6f 75 72 63 65 73 0a 2a 2a 20 61 73 73  resources.** ass
189b0 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
189c0 20 5b 73 71 6c 69 74 65 33 2a 5d 20 68 61 6e 64   [sqlite3*] hand
189d0 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 72 65 6c  le should be rel
189e0 65 61 73 65 64 20 62 79 20 70 61 73 73 69 6e 67  eased by passing
189f0 20 69 74 0a 2a 2a 20 74 6f 20 5b 73 71 6c 69 74   it.** to [sqlit
18a00 65 33 5f 63 6c 6f 73 65 28 29 5d 20 77 68 65 6e  e3_close()] when
18a10 20 69 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72   it is no longer
18a20 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a   required..**.**
18a30 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6f 70   The [sqlite3_op
18a40 65 6e 5f 76 32 28 29 5d 20 69 6e 74 65 72 66 61  en_v2()] interfa
18a50 63 65 20 77 6f 72 6b 73 20 6c 69 6b 65 20 5b 73  ce works like [s
18a60 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 20 0a  qlite3_open()] .
18a70 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 69  ** except that i
18a80 74 20 61 63 63 63 65 70 74 73 20 74 77 6f 20 61  t acccepts two a
18a90 64 64 69 74 69 6f 6e 61 6c 20 70 61 72 61 6d 65  dditional parame
18aa0 74 65 72 73 20 66 6f 72 20 61 64 64 69 74 69 6f  ters for additio
18ab0 6e 61 6c 20 63 6f 6e 74 72 6f 6c 0a 2a 2a 20 6f  nal control.** o
18ac0 76 65 72 20 74 68 65 20 6e 65 77 20 64 61 74 61  ver the new data
18ad0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  base connection.
18ae0 20 20 54 68 65 20 66 6c 61 67 73 20 70 61 72 61    The flags para
18af0 6d 65 74 65 72 20 63 61 6e 20 62 65 0a 2a 2a 20  meter can be.** 
18b00 6f 6e 65 20 6f 66 3a 0a 2a 2a 0a 2a 2a 20 3c 6f  one of:.**.** <o
18b10 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c  l>.** <li>  [SQL
18b20 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c  ITE_OPEN_READONL
18b30 59 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c  Y].** <li>  [SQL
18b40 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
18b50 54 45 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51  TE].** <li>  [SQ
18b60 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
18b70 49 54 45 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f  ITE] | [SQLITE_O
18b80 50 45 4e 5f 43 52 45 41 54 45 5d 0a 2a 2a 20 3c  PEN_CREATE].** <
18b90 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  /ol>.**.** The f
18ba0 69 72 73 74 20 76 61 6c 75 65 20 6f 70 65 6e 73  irst value opens
18bb0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 72 65   the database re
18bc0 61 64 2d 6f 6e 6c 79 2e 20 0a 2a 2a 20 49 66 20  ad-only. .** If 
18bd0 74 68 65 20 64 61 74 61 62 61 73 65 20 64 6f 65  the database doe
18be0 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79  s not previously
18bf0 20 65 78 69 73 74 2c 20 61 6e 20 65 72 72 6f 72   exist, an error
18c00 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
18c10 20 54 68 65 20 73 65 63 6f 6e 64 20 6f 70 74 69   The second opti
18c20 6f 6e 20 6f 70 65 6e 73 0a 2a 2a 20 74 68 65 20  on opens.** the 
18c30 64 61 74 61 62 61 73 65 20 66 6f 72 20 72 65 61  database for rea
18c40 64 69 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67  ding and writing
18c50 20 69 66 20 70 6f 73 73 69 62 6c 65 2c 20 6f 72   if possible, or
18c60 20 72 65 61 64 69 6e 67 20 6f 6e 6c 79 20 69 66   reading only if
18c70 0a 2a 2a 20 69 66 20 74 68 65 20 66 69 6c 65 20  .** if the file 
18c80 69 73 20 77 72 69 74 65 20 70 72 6f 74 65 63 74  is write protect
18c90 65 64 2e 20 20 49 6e 20 65 69 74 68 65 72 20 63  ed.  In either c
18ca0 61 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ase the database
18cb0 0a 2a 2a 20 6d 75 73 74 20 61 6c 72 65 61 64 79  .** must already
18cc0 20 65 78 69 73 74 20 6f 72 20 61 6e 20 65 72 72   exist or an err
18cd0 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  or is returned. 
18ce0 20 54 68 65 20 74 68 69 72 64 20 6f 70 74 69 6f   The third optio
18cf0 6e 0a 2a 2a 20 6f 70 65 6e 73 20 74 68 65 20 64  n.** opens the d
18d00 61 74 61 62 61 73 65 20 66 6f 72 20 72 65 61 64  atabase for read
18d10 69 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67 20  ing and writing 
18d20 61 6e 64 20 63 72 65 61 74 65 73 20 69 74 20 69  and creates it i
18d30 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74  f it does.** not
18d40 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 2e 0a   already exist..
18d50 2a 2a 20 54 68 65 20 74 68 69 72 64 20 6f 70 74  ** The third opt
18d60 69 6f 6e 73 20 69 73 20 62 65 68 61 76 69 6f 72  ions is behavior
18d70 20 74 68 61 74 20 69 73 20 61 6c 77 61 79 73 20   that is always 
18d80 75 73 65 64 20 66 6f 72 20 5b 73 71 6c 69 74 65  used for [sqlite
18d90 33 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 61 6e 64  3_open()].** and
18da0 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36   [sqlite3_open16
18db0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ()]..**.** If th
18dc0 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20  e 3rd parameter 
18dd0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  to [sqlite3_open
18de0 5f 76 32 28 29 5d 20 69 73 20 6e 6f 74 20 6f 6e  _v2()] is not on
18df0 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6d 62  e of the.** comb
18e00 69 6e 61 74 69 6f 6e 73 20 73 68 6f 77 6e 20 61  inations shown a
18e10 62 6f 76 65 20 74 68 65 6e 20 74 68 65 20 62 65  bove then the be
18e20 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69  havior is undefi
18e30 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ned..**.** If th
18e40 65 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a  e filename is ":
18e50 6d 65 6d 6f 72 79 3a 22 2c 20 74 68 65 6e 20 61  memory:", then a
18e60 6e 20 70 72 69 76 61 74 65 0a 2a 2a 20 69 6e 2d  n private.** in-
18e70 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20  memory database 
18e80 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 74  is created for t
18e90 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20  he connection.  
18ea0 54 68 69 73 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a  This in-memory.*
18eb0 2a 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20  * database will 
18ec0 76 61 6e 69 73 68 20 77 68 65 6e 20 74 68 65 20  vanish when the 
18ed0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
18ee0 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 20 20  ion is closed.  
18ef0 46 75 74 75 72 65 0a 2a 2a 20 76 65 72 73 69 6f  Future.** versio
18f00 6e 20 6f 66 20 53 51 4c 69 74 65 20 6d 69 67 68  n of SQLite migh
18f10 74 20 6d 61 6b 65 20 75 73 65 20 6f 66 20 61 64  t make use of ad
18f20 64 69 74 69 6f 6e 61 6c 20 73 70 65 63 69 61 6c  ditional special
18f30 20 66 69 6c 65 6e 61 6d 65 73 0a 2a 2a 20 74 68   filenames.** th
18f40 61 74 20 62 65 67 69 6e 20 77 69 74 68 20 74 68  at begin with th
18f50 65 20 22 3a 22 20 63 68 61 72 61 63 74 65 72 2e  e ":" character.
18f60 20 20 49 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e    It is recommen
18f70 64 65 64 20 74 68 61 74 20 0a 2a 2a 20 77 68 65  ded that .** whe
18f80 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c  n a database fil
18f90 65 6e 61 6d 65 20 72 65 61 6c 6c 79 20 64 6f 65  ename really doe
18fa0 73 20 62 65 67 69 6e 20 77 69 74 68 0a 2a 2a 20  s begin with.** 
18fb0 22 3a 22 20 74 68 61 74 20 79 6f 75 20 70 72 65  ":" that you pre
18fc0 66 69 78 20 74 68 65 20 66 69 6c 65 6e 61 6d 65  fix the filename
18fd0 20 77 69 74 68 20 61 20 70 61 74 68 6e 61 6d 65   with a pathname
18fe0 20 6c 69 6b 65 20 22 2e 2f 22 20 74 6f 0a 2a 2a   like "./" to.**
18ff0 20 61 76 6f 69 64 20 61 6d 62 69 67 75 69 74 79   avoid ambiguity
19000 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66  ..**.** If the f
19010 69 6c 65 6e 61 6d 65 20 69 73 20 61 6e 20 65 6d  ilename is an em
19020 70 74 79 20 73 74 72 69 6e 67 2c 20 74 68 65 6e  pty string, then
19030 20 61 20 70 72 69 76 61 74 65 20 74 65 6d 70 6f   a private tempo
19040 72 61 72 79 0a 2a 2a 20 6f 6e 2d 64 69 73 6b 20  rary.** on-disk 
19050 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65  database will be
19060 20 63 72 65 61 74 65 64 2e 20 20 54 68 69 73 20   created.  This 
19070 70 72 69 76 61 74 65 20 64 61 74 61 62 61 73 65  private database
19080 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 61 75 74 6f   will be.** auto
19090 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65  matically delete
190a0 64 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 65  d as soon as the
190b0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
190c0 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a  tion is closed..
190d0 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 75 72 74 68  **.** The fourth
190e0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71   parameter to sq
190f0 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20  lite3_open_v2() 
19100 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
19110 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76  he.** [sqlite3_v
19120 66 73 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 20  fs] object that 
19130 64 65 66 69 6e 65 73 20 74 68 65 20 6f 70 65 72  defines the oper
19140 61 74 69 6e 67 20 73 79 73 74 65 6d 20 0a 2a 2a  ating system .**
19150 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20   interface that 
19160 74 68 65 20 6e 65 77 20 64 61 74 61 62 61 73 65  the new database
19170 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 73 68 6f 75   connection shou
19180 6c 64 20 75 73 65 2e 20 20 49 66 20 74 68 65 0a  ld use.  If the.
19190 2a 2a 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65  ** fourth parame
191a0 74 65 72 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f  ter is a NULL po
191b0 69 6e 74 65 72 20 74 68 65 6e 20 74 68 65 20 64  inter then the d
191c0 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f  efault [sqlite3_
191d0 76 66 73 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 69  vfs].** object i
191e0 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62  s used..**.** <b
191f0 3e 4e 6f 74 65 20 74 6f 20 77 69 6e 64 6f 77 73  >Note to windows
19200 20 75 73 65 72 73 3a 3c 2f 62 3e 20 20 54 68 65   users:</b>  The
19210 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 66   encoding used f
19220 6f 72 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20  or the filename 
19230 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 6f 66 20 5b  argument.** of [
19240 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 20  sqlite3_open()] 
19250 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  and [sqlite3_ope
19260 6e 5f 76 32 28 29 5d 20 6d 75 73 74 20 62 65 20  n_v2()] must be 
19270 55 54 46 2d 38 2c 20 6e 6f 74 20 77 68 61 74 65  UTF-8, not whate
19280 76 65 72 0a 2a 2a 20 63 6f 64 65 70 61 67 65 20  ver.** codepage 
19290 69 73 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66  is currently def
192a0 69 6e 65 64 2e 20 20 46 69 6c 65 6e 61 6d 65 73  ined.  Filenames
192b0 20 63 6f 6e 74 61 69 6e 69 6e 67 20 69 6e 74 65   containing inte
192c0 72 6e 61 74 69 6f 6e 61 6c 0a 2a 2a 20 63 68 61  rnational.** cha
192d0 72 61 63 74 65 72 73 20 6d 75 73 74 20 62 65 20  racters must be 
192e0 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 55 54 46  converted to UTF
192f0 2d 38 20 70 72 69 6f 72 20 74 6f 20 70 61 73 73  -8 prior to pass
19300 69 6e 67 20 74 68 65 6d 20 69 6e 74 6f 0a 2a 2a  ing them into.**
19310 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29   [sqlite3_open()
19320 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70  ] or [sqlite3_op
19330 65 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  en_v2()]..**.** 
19340 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a  INVARIANTS:.**.*
19350 2a 20 7b 46 31 32 37 30 31 7d 20 54 68 65 20 5b  * {F12701} The [
19360 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c  sqlite3_open()],
19370 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36   [sqlite3_open16
19380 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 20 20 20 20  ()], and.**     
19390 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 6f 70       [sqlite3_op
193a0 65 6e 5f 76 32 28 29 5d 20 69 6e 74 65 72 66 61  en_v2()] interfa
193b0 63 65 73 20 63 72 65 61 74 65 20 61 20 6e 65 77  ces create a new
193c0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 64 61  .**          [da
193d0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
193e0 6e 5d 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  n] associated wi
193f0 74 68 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  th.**          t
19400 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
19410 20 67 69 76 65 6e 20 69 6e 20 74 68 65 69 72 20   given in their 
19420 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e  first parameter.
19430 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 37 30 32 7d 20  .**.** {F12702} 
19440 54 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 72 67  The filename arg
19450 75 6d 65 6e 74 20 69 73 20 69 6e 74 65 72 70 72  ument is interpr
19460 65 74 65 64 20 61 73 20 55 54 46 2d 38 0a 2a 2a  eted as UTF-8.**
19470 20 20 20 20 20 20 20 20 20 20 66 6f 72 20 5b 73            for [s
19480 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 20 61  qlite3_open()] a
19490 6e 64 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  nd [sqlite3_open
194a0 5f 76 32 28 29 5d 20 61 6e 64 20 61 73 20 55 54  _v2()] and as UT
194b0 46 2d 31 36 0a 2a 2a 20 20 20 20 20 20 20 20 20  F-16.**         
194c0 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62   in the native b
194d0 79 74 65 20 6f 72 64 65 72 20 66 6f 72 20 5b 73  yte order for [s
194e0 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d  qlite3_open16()]
194f0 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 37 30 33 7d  ..**.** {F12703}
19500 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 69 6e   A successful in
19510 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c  vocation of [sql
19520 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73  ite3_open()], [s
19530 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d  qlite3_open16()]
19540 2c 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  , .**          o
19550 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  r [sqlite3_open_
19560 76 32 28 29 5d 20 77 72 69 74 65 73 20 61 20 70  v2()] writes a p
19570 6f 69 6e 74 65 72 20 74 6f 20 61 20 6e 65 77 0a  ointer to a new.
19580 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 64 61 74  **          [dat
19590 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
195a0 5d 20 69 6e 74 6f 20 2a 70 70 44 62 2e 0a 2a 2a  ] into *ppDb..**
195b0 0a 2a 2a 20 7b 46 31 32 37 30 34 7d 20 54 68 65  .** {F12704} The
195c0 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29   [sqlite3_open()
195d0 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  ], [sqlite3_open
195e0 31 36 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 20 20  16()], and.**   
195f0 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
19600 6f 70 65 6e 5f 76 32 28 29 5d 20 69 6e 74 65 72  open_v2()] inter
19610 66 61 63 65 73 20 72 65 74 75 72 6e 20 5b 53 51  faces return [SQ
19620 4c 49 54 45 5f 4f 4b 5d 20 75 70 6f 6e 20 73 75  LITE_OK] upon su
19630 63 63 65 73 73 2c 0a 2a 2a 20 20 20 20 20 20 20  ccess,.**       
19640 20 20 20 6f 72 20 61 6e 20 61 70 70 72 6f 70 72     or an appropr
19650 69 61 74 65 20 5b 65 72 72 6f 72 20 63 6f 64 65  iate [error code
19660 5d 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a  ] on failure..**
19670 0a 2a 2a 20 7b 46 31 32 37 30 36 7d 20 54 68 65  .** {F12706} The
19680 20 64 65 66 61 75 6c 74 20 74 65 78 74 20 65 6e   default text en
19690 63 6f 64 69 6e 67 20 66 6f 72 20 61 20 6e 65 77  coding for a new
196a0 20 64 61 74 61 62 61 73 65 20 63 72 65 61 74 65   database create
196b0 64 20 75 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20  d using.**      
196c0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65      [sqlite3_ope
196d0 6e 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  n()] or [sqlite3
196e0 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 77 69 6c 6c  _open_v2()] will
196f0 20 62 65 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a   be UTF-8..**.**
19700 20 7b 46 31 32 37 30 37 7d 20 54 68 65 20 64 65   {F12707} The de
19710 66 61 75 6c 74 20 74 65 78 74 20 65 6e 63 6f 64  fault text encod
19720 69 6e 67 20 66 6f 72 20 61 20 6e 65 77 20 64 61  ing for a new da
19730 74 61 62 61 73 65 20 63 72 65 61 74 65 64 20 75  tabase created u
19740 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20  sing.**         
19750 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36   [sqlite3_open16
19760 28 29 5d 20 77 69 6c 6c 20 62 65 20 55 54 46 2d  ()] will be UTF-
19770 31 36 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 37 30  16..**.** {F1270
19780 39 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  9} The [sqlite3_
19790 6f 70 65 6e 28 46 2c 44 29 5d 20 69 6e 74 65 72  open(F,D)] inter
197a0 66 61 63 65 20 69 73 20 65 71 75 69 76 61 6c 65  face is equivale
197b0 6e 74 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20  nt to.**        
197c0 20 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f    [sqlite3_open_
197d0 76 32 28 46 2c 44 2c 47 2c 30 29 5d 20 77 68 65  v2(F,D,G,0)] whe
197e0 72 65 20 74 68 65 20 47 20 70 61 72 61 6d 65 74  re the G paramet
197f0 65 72 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20  er is.**        
19800 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52    [SQLITE_OPEN_R
19810 45 41 44 57 52 49 54 45 5d 7c 5b 53 51 4c 49 54  EADWRITE]|[SQLIT
19820 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d 2e 0a  E_OPEN_CREATE]..
19830 2a 2a 0a 2a 2a 20 7b 46 31 32 37 31 31 7d 20 49  **.** {F12711} I
19840 66 20 74 68 65 20 47 20 70 61 72 61 6d 65 74 65  f the G paramete
19850 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70  r to [sqlite3_op
19860 65 6e 5f 76 32 28 46 2c 44 2c 47 2c 56 29 5d 20  en_v2(F,D,G,V)] 
19870 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20  contains the.** 
19880 20 20 20 20 20 20 20 20 20 62 69 74 20 76 61 6c           bit val
19890 75 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  ue [SQLITE_OPEN_
198a0 52 45 41 44 4f 4e 4c 59 5d 20 74 68 65 6e 20 74  READONLY] then t
198b0 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6f  he database is o
198c0 70 65 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 20  pened.**        
198d0 20 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f 6e    for reading on
198e0 6c 79 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 37 31  ly..**.** {F1271
198f0 32 7d 20 49 66 20 74 68 65 20 47 20 70 61 72 61  2} If the G para
19900 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65  meter to [sqlite
19910 33 5f 6f 70 65 6e 5f 76 32 28 46 2c 44 2c 47 2c  3_open_v2(F,D,G,
19920 56 29 5d 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  V)] contains the
19930 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62 69 74  .**          bit
19940 20 76 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f 4f   value [SQLITE_O
19950 50 45 4e 5f 52 45 41 44 57 52 49 54 45 5d 20 74  PEN_READWRITE] t
19960 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
19970 20 69 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 20 20   is opened.**   
19980 20 20 20 20 20 20 20 72 65 61 64 69 6e 67 20 61         reading a
19990 6e 64 20 77 72 69 74 69 6e 67 20 69 66 20 70 6f  nd writing if po
199a0 73 73 69 62 6c 65 2c 20 6f 72 20 66 6f 72 20 72  ssible, or for r
199b0 65 61 64 69 6e 67 20 6f 6e 6c 79 20 69 66 20 74  eading only if t
199c0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66  he.**          f
199d0 69 6c 65 20 69 73 20 77 72 69 74 65 20 70 72 6f  ile is write pro
199e0 74 65 63 74 65 64 20 62 79 20 74 68 65 20 6f 70  tected by the op
199f0 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 0a  erating system..
19a00 2a 2a 0a 2a 2a 20 7b 46 31 32 37 31 33 7d 20 49  **.** {F12713} I
19a10 66 20 74 68 65 20 47 20 70 61 72 61 6d 65 74 65  f the G paramete
19a20 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70  r to [sqlite3_op
19a30 65 6e 28 76 32 28 46 2c 44 2c 47 2c 56 29 5d 20  en(v2(F,D,G,V)] 
19a40 6f 6d 69 74 73 20 74 68 65 0a 2a 2a 20 20 20 20  omits the.**    
19a50 20 20 20 20 20 20 62 69 74 20 76 61 6c 75 65 20        bit value 
19a60 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45  [SQLITE_OPEN_CRE
19a70 41 54 45 5d 20 61 6e 64 20 74 68 65 20 64 61 74  ATE] and the dat
19a80 61 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a 2a  abase does not.*
19a90 2a 20 20 20 20 20 20 20 20 20 20 70 72 65 76 69  *          previ
19aa0 6f 75 73 6c 79 20 65 78 69 73 74 2c 20 61 6e 20  ously exist, an 
19ab0 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65  error is returne
19ac0 64 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 37 31 34  d..**.** {F12714
19ad0 7d 20 49 66 20 74 68 65 20 47 20 70 61 72 61 6d  } If the G param
19ae0 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33  eter to [sqlite3
19af0 5f 6f 70 65 6e 28 76 32 28 46 2c 44 2c 47 2c 56  _open(v2(F,D,G,V
19b00 29 5d 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a  )] contains the.
19b10 2a 2a 20 20 20 20 20 20 20 20 20 20 62 69 74 20  **          bit 
19b20 76 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f 4f 50  value [SQLITE_OP
19b30 45 4e 5f 43 52 45 41 54 45 5d 20 61 6e 64 20 74  EN_CREATE] and t
19b40 68 65 20 64 61 74 61 62 61 73 65 20 64 6f 65 73  he database does
19b50 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 20   not.**         
19b60 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69 73   previously exis
19b70 74 2c 20 74 68 65 6e 20 61 6e 20 61 74 74 65 6d  t, then an attem
19b80 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 63 72  pt is made to cr
19b90 65 61 74 65 20 61 6e 64 0a 2a 2a 20 20 20 20 20  eate and.**     
19ba0 20 20 20 20 20 69 6e 69 74 69 61 6c 69 7a 65 20       initialize 
19bb0 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a  the database..**
19bc0 0a 2a 2a 20 7b 46 31 32 37 31 37 7d 20 49 66 20  .** {F12717} If 
19bd0 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 72 67  the filename arg
19be0 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65  ument to [sqlite
19bf0 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69  3_open()], [sqli
19c00 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 0a 2a  te3_open16()],.*
19c10 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 5b 73  *          or [s
19c20 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
19c30 5d 20 69 73 20 22 3a 6d 65 6d 6f 72 79 3a 22 2c  ] is ":memory:",
19c40 20 74 68 65 6e 20 61 6e 20 70 72 69 76 61 74 65   then an private
19c50 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 65 70  ,.**          ep
19c60 68 65 6d 65 72 61 6c 2c 20 69 6e 2d 6d 65 6d 6f  hemeral, in-memo
19c70 72 79 20 64 61 74 61 62 61 73 65 20 69 73 20 63  ry database is c
19c80 72 65 61 74 65 64 20 66 6f 72 20 74 68 65 20 63  reated for the c
19c90 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 20 20 20  onnection..**   
19ca0 20 20 20 20 20 20 20 3c 74 6f 64 6f 3e 49 73 20         <todo>Is 
19cb0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
19cc0 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  TE|SQLITE_OPEN_R
19cd0 45 41 44 57 52 49 54 45 20 72 65 71 75 69 72 65  EADWRITE require
19ce0 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e  d.**          in
19cf0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32   sqlite3_open_v2
19d00 28 29 3f 3c 2f 74 6f 64 6f 3e 0a 2a 2a 0a 2a 2a  ()?</todo>.**.**
19d10 20 7b 46 31 32 37 31 39 7d 20 49 66 20 74 68 65   {F12719} If the
19d20 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c   filename is NUL
19d30 4c 20 6f 72 20 61 6e 20 65 6d 70 74 79 20 73 74  L or an empty st
19d40 72 69 6e 67 2c 20 74 68 65 6e 20 61 20 70 72 69  ring, then a pri
19d50 76 61 74 65 2c 0a 2a 2a 20 20 20 20 20 20 20 20  vate,.**        
19d60 20 20 65 70 68 65 72 6d 65 72 61 6c 20 6f 6e 2d    ephermeral on-
19d70 64 69 73 6b 20 64 61 74 61 62 61 73 65 20 77 69  disk database wi
19d80 6c 6c 20 62 65 20 63 72 65 61 74 65 64 2e 0a 2a  ll be created..*
19d90 2a 20 20 20 20 20 20 20 20 20 20 3c 74 6f 64 6f  *          <todo
19da0 3e 49 73 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  >Is SQLITE_OPEN_
19db0 43 52 45 41 54 45 7c 53 51 4c 49 54 45 5f 4f 50  CREATE|SQLITE_OP
19dc0 45 4e 5f 52 45 41 44 57 52 49 54 45 20 72 65 71  EN_READWRITE req
19dd0 75 69 72 65 64 0a 2a 2a 20 20 20 20 20 20 20 20  uired.**        
19de0 20 20 69 6e 20 73 71 6c 69 74 65 33 5f 6f 70 65    in sqlite3_ope
19df0 6e 5f 76 32 28 29 3f 3c 2f 74 6f 64 6f 3e 0a 2a  n_v2()?</todo>.*
19e00 2a 0a 2a 2a 20 7b 46 31 32 37 32 31 7d 20 54 68  *.** {F12721} Th
19e10 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
19e20 65 63 74 69 6f 6e 5d 20 63 72 65 61 74 65 64 20  ection] created 
19e30 62 79 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  by .**          
19e40 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32  [sqlite3_open_v2
19e50 28 46 2c 44 2c 47 2c 56 29 5d 20 77 69 6c 6c 20  (F,D,G,V)] will 
19e60 75 73 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  use the.**      
19e70 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 76 66 73      [sqlite3_vfs
19e80 5d 20 6f 62 6a 65 63 74 20 69 64 65 6e 74 69 66  ] object identif
19e90 69 65 64 20 62 79 20 74 68 65 20 56 20 70 61 72  ied by the V par
19ea0 61 6d 65 74 65 72 2c 20 6f 72 0a 2a 2a 20 20 20  ameter, or.**   
19eb0 20 20 20 20 20 20 20 74 68 65 20 64 65 66 61 75         the defau
19ec0 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d  lt [sqlite3_vfs]
19ed0 20 6f 62 6a 65 63 74 20 69 73 20 56 20 69 73 20   object is V is 
19ee0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a  a NULL pointer..
19ef0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
19f00 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 0a  t sqlite3_open(.
19f10 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69    const char *fi
19f20 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74  lename,   /* Dat
19f30 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 28  abase filename (
19f40 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69  UTF-8) */.  sqli
19f50 74 65 33 20 2a 2a 70 70 44 62 20 20 20 20 20 20  te3 **ppDb      
19f60 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69      /* OUT: SQLi
19f70 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a  te db handle */.
19f80 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
19f90 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36  t sqlite3_open16
19fa0 28 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  (.  const void *
19fb0 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44  filename,   /* D
19fc0 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65  atabase filename
19fd0 20 28 55 54 46 2d 31 36 29 20 2a 2f 0a 20 20 73   (UTF-16) */.  s
19fe0 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 20 20 20  qlite3 **ppDb   
19ff0 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53         /* OUT: S
1a000 51 4c 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20  QLite db handle 
1a010 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  */.);.SQLITE_API
1a020 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65   int sqlite3_ope
1a030 6e 5f 76 32 28 0a 20 20 63 6f 6e 73 74 20 63 68  n_v2(.  const ch
1a040 61 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20  ar *filename,   
1a050 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c 65  /* Database file
1a060 6e 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a  name (UTF-8) */.
1a070 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62    sqlite3 **ppDb
1a080 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54  ,         /* OUT
1a090 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e 64  : SQLite db hand
1a0a0 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67  le */.  int flag
1a0b0 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
1a0c0 2f 2a 20 46 6c 61 67 73 20 2a 2f 0a 20 20 63 6f  /* Flags */.  co
1a0d0 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73 20 20  nst char *zVfs  
1a0e0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
1a0f0 20 56 46 53 20 6d 6f 64 75 6c 65 20 74 6f 20 75   VFS module to u
1a100 73 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  se */.);../*.** 
1a110 43 41 50 49 33 52 45 46 3a 20 45 72 72 6f 72 20  CAPI3REF: Error 
1a120 43 6f 64 65 73 20 41 6e 64 20 4d 65 73 73 61 67  Codes And Messag
1a130 65 73 20 7b 46 31 32 38 30 30 7d 0a 2a 2a 0a 2a  es {F12800}.**.*
1a140 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 72  * The sqlite3_er
1a150 72 63 6f 64 65 28 29 20 69 6e 74 65 72 66 61 63  rcode() interfac
1a160 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75  e returns the nu
1a170 6d 65 72 69 63 0a 2a 2a 20 5b 53 51 4c 49 54 45  meric.** [SQLITE
1a180 5f 4f 4b 20 7c 20 72 65 73 75 6c 74 20 63 6f 64  _OK | result cod
1a190 65 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f  e] or [SQLITE_IO
1a1a0 45 52 52 5f 52 45 41 44 20 7c 20 65 78 74 65 6e  ERR_READ | exten
1a1b0 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d  ded result code]
1a1c0 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6d 6f 73 74  .** for the most
1a1d0 20 72 65 63 65 6e 74 20 66 61 69 6c 65 64 20 73   recent failed s
1a1e0 71 6c 69 74 65 33 5f 2a 20 41 50 49 20 63 61 6c  qlite3_* API cal
1a1f0 6c 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20  l associated.** 
1a200 77 69 74 68 20 5b 73 71 6c 69 74 65 33 5d 20 68  with [sqlite3] h
1a210 61 6e 64 6c 65 20 27 64 62 27 2e 20 49 66 20 61  andle 'db'. If a
1a220 20 70 72 69 6f 72 20 41 50 49 20 63 61 6c 6c 20   prior API call 
1a230 66 61 69 6c 65 64 20 62 75 74 20 74 68 65 0a 2a  failed but the.*
1a240 2a 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 41 50  * most recent AP
1a250 49 20 63 61 6c 6c 20 73 75 63 63 65 65 64 65 64  I call succeeded
1a260 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  , the return val
1a270 75 65 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  ue from sqlite3_
1a280 65 72 72 63 6f 64 65 28 29 0a 2a 2a 20 69 73 20  errcode().** is 
1a290 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a  undefined..**.**
1a2a0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 72 72   The sqlite3_err
1a2b0 6d 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65  msg() and sqlite
1a2c0 33 5f 65 72 72 6d 73 67 31 36 28 29 20 72 65 74  3_errmsg16() ret
1a2d0 75 72 6e 20 45 6e 67 6c 69 73 68 2d 6c 61 6e 67  urn English-lang
1a2e0 75 61 67 65 0a 2a 2a 20 74 65 78 74 20 74 68 61  uage.** text tha
1a2f0 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20  t describes the 
1a300 65 72 72 6f 72 2c 20 61 73 20 65 69 74 68 65 72  error, as either
1a310 20 55 54 46 38 20 6f 72 20 55 54 46 31 36 20 72   UTF8 or UTF16 r
1a320 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20  espectively..** 
1a330 4d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74  Memory to hold t
1a340 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
1a350 20 73 74 72 69 6e 67 20 69 73 20 6d 61 6e 61 67   string is manag
1a360 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 2a  ed internally..*
1a370 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f  * The applicatio
1a380 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  n does not need 
1a390 74 6f 20 77 6f 72 72 79 20 77 69 74 68 20 66 72  to worry with fr
1a3a0 65 65 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74  eeing the result
1a3b0 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68  ..** However, th
1a3c0 65 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20 6d  e error string m
1a3d0 69 67 68 74 20 62 65 20 6f 76 65 72 77 72 69 74  ight be overwrit
1a3e0 74 65 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74  ten or deallocat
1a3f0 65 64 20 62 79 0a 2a 2a 20 73 75 62 73 65 71 75  ed by.** subsequ
1a400 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 6f 74 68  ent calls to oth
1a410 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66  er SQLite interf
1a420 61 63 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a  ace functions..*
1a430 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
1a440 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 38 30 31 7d 20  .**.** {F12801} 
1a450 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 65 72 72  The [sqlite3_err
1a460 63 6f 64 65 28 44 29 5d 20 69 6e 74 65 72 66 61  code(D)] interfa
1a470 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  ce returns the n
1a480 75 6d 65 72 69 63 0a 2a 2a 20 20 20 20 20 20 20  umeric.**       
1a490 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 20 7c 20     [SQLITE_OK | 
1a4a0 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 6f 72 0a  result code] or.
1a4b0 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c  **          [SQL
1a4c0 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 7c  ITE_IOERR_READ |
1a4d0 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74   extended result
1a4e0 20 63 6f 64 65 5d 0a 2a 2a 20 20 20 20 20 20 20   code].**       
1a4f0 20 20 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20     for the most 
1a500 72 65 63 65 6e 74 6c 79 20 66 61 69 6c 65 64 20  recently failed 
1a510 69 6e 74 65 72 66 61 63 65 20 63 61 6c 6c 20 61  interface call a
1a520 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 20 20 20  ssociated.**    
1a530 20 20 20 20 20 20 77 69 74 68 20 5b 64 61 74 61        with [data
1a540 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
1a550 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 38 30   D..**.** {F1280
1a560 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  3} The [sqlite3_
1a570 65 72 72 6d 73 67 28 44 29 5d 20 61 6e 64 20 5b  errmsg(D)] and [
1a580 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36  sqlite3_errmsg16
1a590 28 44 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20  (D)].**         
1a5a0 20 69 6e 74 65 72 66 61 63 65 73 20 72 65 74 75   interfaces retu
1a5b0 72 6e 20 45 6e 67 6c 69 73 68 2d 6c 61 6e 67 75  rn English-langu
1a5c0 61 67 65 20 74 65 78 74 20 74 68 61 74 20 64 65  age text that de
1a5d0 73 63 72 69 62 65 73 0a 2a 2a 20 20 20 20 20 20  scribes.**      
1a5e0 20 20 20 20 74 68 65 20 65 72 72 6f 72 20 69 6e      the error in
1a5f0 20 74 68 65 20 6d 6f 73 74 6c 79 20 72 65 63 65   the mostly rece
1a600 6e 74 6c 79 20 66 61 69 6c 65 64 20 69 6e 74 65  ntly failed inte
1a610 72 66 61 63 65 20 63 61 6c 6c 2c 0a 2a 2a 20 20  rface call,.**  
1a620 20 20 20 20 20 20 20 20 65 6e 63 6f 64 65 64 20          encoded 
1a630 61 73 20 65 69 74 68 65 72 20 55 54 46 38 20 6f  as either UTF8 o
1a640 72 20 55 54 46 31 36 20 72 65 73 70 65 63 74 69  r UTF16 respecti
1a650 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32  vely..**.** {F12
1a660 38 30 37 7d 20 54 68 65 20 73 74 72 69 6e 67 73  807} The strings
1a670 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71   returned by [sq
1a680 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 20  lite3_errmsg()] 
1a690 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65 72 72  and [sqlite3_err
1a6a0 6d 73 67 31 36 28 29 5d 0a 2a 2a 20 20 20 20 20  msg16()].**     
1a6b0 20 20 20 20 20 61 72 65 20 76 61 6c 69 64 20 75       are valid u
1a6c0 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 20 53 51  ntil the next SQ
1a6d0 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 20 63  Lite interface c
1a6e0 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 38  all..**.** {F128
1a6f0 30 38 7d 20 43 61 6c 6c 73 20 74 6f 20 41 50 49  08} Calls to API
1a700 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 64   routines that d
1a710 6f 20 6e 6f 74 20 72 65 74 75 72 6e 20 61 6e 20  o not return an 
1a720 65 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 20 20  error code.**   
1a730 20 20 20 20 20 20 20 28 65 78 61 6d 70 6c 65 3a         (example:
1a740 20 5b 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63   [sqlite3_data_c
1a750 6f 75 6e 74 28 29 5d 29 20 64 6f 20 6e 6f 74 0a  ount()]) do not.
1a760 2a 2a 20 20 20 20 20 20 20 20 20 20 63 68 61 6e  **          chan
1a770 67 65 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  ge the error cod
1a780 65 20 6f 72 20 6d 65 73 73 61 67 65 20 72 65 74  e or message ret
1a790 75 72 6e 65 64 20 62 79 0a 2a 2a 20 20 20 20 20  urned by.**     
1a7a0 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 72       [sqlite3_er
1a7b0 72 63 6f 64 65 28 29 5d 2c 20 5b 73 71 6c 69 74  rcode()], [sqlit
1a7c0 65 33 5f 65 72 72 6d 73 67 28 29 5d 2c 20 6f 72  e3_errmsg()], or
1a7d0 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67   [sqlite3_errmsg
1a7e0 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31  16()]..**.** {F1
1a7f0 32 38 30 39 7d 20 49 6e 74 65 72 66 61 63 65 73  2809} Interfaces
1a800 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 61 73   that are not as
1a810 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20  sociated with a 
1a820 73 70 65 63 69 66 69 63 0a 2a 2a 20 20 20 20 20  specific.**     
1a830 20 20 20 20 20 5b 64 61 74 61 62 61 73 65 20 63       [database c
1a840 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 65 78 61 6d  onnection] (exam
1a850 70 6c 65 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20  ples:.**        
1a860 20 20 5b 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e    [sqlite3_mprin
1a870 74 66 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65  tf()] or [sqlite
1a880 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f  3_enable_shared_
1a890 63 61 63 68 65 28 29 5d 0a 2a 2a 20 20 20 20 20  cache()].**     
1a8a0 20 20 20 20 20 64 6f 20 6e 6f 74 20 63 68 61 6e       do not chan
1a8b0 67 65 20 74 68 65 20 76 61 6c 75 65 73 20 72 65  ge the values re
1a8c0 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 20 20 20  turned by.**    
1a8d0 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65        [sqlite3_e
1a8e0 72 72 63 6f 64 65 28 29 5d 2c 20 5b 73 71 6c 69  rrcode()], [sqli
1a8f0 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2c 20 6f  te3_errmsg()], o
1a900 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  r [sqlite3_errms
1a910 67 31 36 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54  g16()]..*/.SQLIT
1a920 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1a930 33 5f 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65  3_errcode(sqlite
1a940 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f 41  3 *db);.SQLITE_A
1a950 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  PI const char *s
1a960 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 73 71  qlite3_errmsg(sq
1a970 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f  lite3*);.SQLITE_
1a980 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
1a990 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36  sqlite3_errmsg16
1a9a0 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a  (sqlite3*);../*.
1a9b0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c  ** CAPI3REF: SQL
1a9c0 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a 65 63   Statement Objec
1a9d0 74 20 7b 46 31 33 30 30 30 7d 0a 2a 2a 20 4b 45  t {F13000}.** KE
1a9e0 59 57 4f 52 44 53 3a 20 7b 70 72 65 70 61 72 65  YWORDS: {prepare
1a9f0 64 20 73 74 61 74 65 6d 65 6e 74 7d 20 7b 70 72  d statement} {pr
1aa00 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
1aa10 73 7d 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  s}.**.** An inst
1aa20 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a  ance of this obj
1aa30 65 63 74 20 72 65 70 72 65 73 65 6e 74 20 73 69  ect represent si
1aa40 6e 67 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65  ngle SQL stateme
1aa50 6e 74 73 2e 20 20 54 68 69 73 0a 2a 2a 20 6f 62  nts.  This.** ob
1aa60 6a 65 63 74 20 69 73 20 76 61 72 69 6f 75 73 6c  ject is variousl
1aa70 79 20 6b 6e 6f 77 6e 20 61 73 20 61 20 22 70 72  y known as a "pr
1aa80 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
1aa90 22 20 6f 72 20 61 20 0a 2a 2a 20 22 63 6f 6d 70  " or a .** "comp
1aaa0 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65  iled SQL stateme
1aab0 6e 74 22 20 6f 72 20 73 69 6d 70 6c 79 20 61 73  nt" or simply as
1aac0 20 61 20 22 73 74 61 74 65 6d 65 6e 74 22 2e 0a   a "statement"..
1aad0 2a 2a 20 0a 2a 2a 20 54 68 65 20 6c 69 66 65 20  ** .** The life 
1aae0 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6f  of a statement o
1aaf0 62 6a 65 63 74 20 67 6f 65 73 20 73 6f 6d 65 74  bject goes somet
1ab00 68 69 6e 67 20 6c 69 6b 65 20 74 68 69 73 3a 0a  hing like this:.
1ab10 2a 2a 0a 2a 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c  **.** <ol>.** <l
1ab20 69 3e 20 43 72 65 61 74 65 20 74 68 65 20 6f 62  i> Create the ob
1ab30 6a 65 63 74 20 75 73 69 6e 67 20 5b 73 71 6c 69  ject using [sqli
1ab40 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
1ab50 5d 20 6f 72 20 61 20 72 65 6c 61 74 65 64 0a 2a  ] or a related.*
1ab60 2a 20 20 20 20 20 20 66 75 6e 63 74 69 6f 6e 2e  *      function.
1ab70 0a 2a 2a 20 3c 6c 69 3e 20 42 69 6e 64 20 76 61  .** <li> Bind va
1ab80 6c 75 65 73 20 74 6f 20 68 6f 73 74 20 70 61 72  lues to host par
1ab90 61 6d 65 74 65 72 73 20 75 73 69 6e 67 0a 2a 2a  ameters using.**
1aba0 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 62        [sqlite3_b
1abb0 69 6e 64 5f 62 6c 6f 62 20 7c 20 73 71 6c 69 74  ind_blob | sqlit
1abc0 65 33 5f 62 69 6e 64 5f 2a 20 69 6e 74 65 72 66  e3_bind_* interf
1abd0 61 63 65 73 5d 2e 0a 2a 2a 20 3c 6c 69 3e 20 52  aces]..** <li> R
1abe0 75 6e 20 74 68 65 20 53 51 4c 20 62 79 20 63 61  un the SQL by ca
1abf0 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73  lling [sqlite3_s
1ac00 74 65 70 28 29 5d 20 6f 6e 65 20 6f 72 20 6d 6f  tep()] one or mo
1ac10 72 65 20 74 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69  re times..** <li
1ac20 3e 20 52 65 73 65 74 20 74 68 65 20 73 74 61 74  > Reset the stat
1ac30 65 6d 65 6e 74 20 75 73 69 6e 67 20 5b 73 71 6c  ement using [sql
1ac40 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 74 68  ite3_reset()] th
1ac50 65 6e 20 67 6f 20 62 61 63 6b 0a 2a 2a 20 20 20  en go back.**   
1ac60 20 20 20 74 6f 20 73 74 65 70 20 32 2e 20 20 44     to step 2.  D
1ac70 6f 20 74 68 69 73 20 7a 65 72 6f 20 6f 72 20 6d  o this zero or m
1ac80 6f 72 65 20 74 69 6d 65 73 2e 0a 2a 2a 20 3c 6c  ore times..** <l
1ac90 69 3e 20 44 65 73 74 72 6f 79 20 74 68 65 20 6f  i> Destroy the o
1aca0 62 6a 65 63 74 20 75 73 69 6e 67 20 5b 73 71 6c  bject using [sql
1acb0 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d  ite3_finalize()]
1acc0 2e 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a  ..** </ol>.**.**
1acd0 20 52 65 66 65 72 20 74 6f 20 64 6f 63 75 6d 65   Refer to docume
1ace0 6e 74 61 74 69 6f 6e 20 6f 6e 20 69 6e 64 69 76  ntation on indiv
1acf0 69 64 75 61 6c 20 6d 65 74 68 6f 64 73 20 61 62  idual methods ab
1ad00 6f 76 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  ove for addition
1ad10 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f  al.** informatio
1ad20 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  n..*/.typedef st
1ad30 72 75 63 74 20 73 71 6c 69 74 65 33 5f 73 74 6d  ruct sqlite3_stm
1ad40 74 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 3b 0a  t sqlite3_stmt;.
1ad50 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
1ad60 20 52 75 6e 2d 74 69 6d 65 20 4c 69 6d 69 74 73   Run-time Limits
1ad70 20 7b 46 31 32 37 36 30 7d 0a 2a 2a 0a 2a 2a 20   {F12760}.**.** 
1ad80 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 61  This interface a
1ad90 6c 6c 6f 77 73 20 74 68 65 20 73 69 7a 65 20 6f  llows the size o
1ada0 66 20 76 61 72 69 6f 75 73 20 63 6f 6e 73 74 72  f various constr
1adb0 75 63 74 73 20 74 6f 20 62 65 20 6c 69 6d 69 74  ucts to be limit
1adc0 65 64 0a 2a 2a 20 6f 6e 20 61 20 63 6f 6e 6e 65  ed.** on a conne
1add0 63 74 69 6f 6e 20 62 79 20 63 6f 6e 6e 65 63 74  ction by connect
1ade0 69 6f 6e 20 62 61 73 69 73 2e 20 20 54 68 65 20  ion basis.  The 
1adf0 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20  first parameter 
1ae00 69 73 20 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62  is the.** [datab
1ae10 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
1ae20 77 68 6f 73 65 20 6c 69 6d 69 74 20 69 73 20 74  whose limit is t
1ae30 6f 20 62 65 20 73 65 74 20 6f 72 20 71 75 65 72  o be set or quer
1ae40 69 65 64 2e 20 20 54 68 65 0a 2a 2a 20 73 65 63  ied.  The.** sec
1ae50 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73  ond parameter is
1ae60 20 6f 6e 65 20 6f 66 20 74 68 65 20 5b 6c 69 6d   one of the [lim
1ae70 69 74 20 63 61 74 65 67 6f 72 69 65 73 5d 20 74  it categories] t
1ae80 68 61 74 20 64 65 66 69 6e 65 20 61 0a 2a 2a 20  hat define a.** 
1ae90 63 6c 61 73 73 20 6f 66 20 63 6f 6e 73 74 72 75  class of constru
1aea0 63 74 73 20 74 6f 20 62 65 20 73 69 7a 65 20 6c  cts to be size l
1aeb0 69 6d 69 74 65 64 2e 20 20 54 68 65 20 74 68 69  imited.  The thi
1aec0 72 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  rd parameter is 
1aed0 74 68 65 0a 2a 2a 20 6e 65 77 20 6c 69 6d 69 74  the.** new limit
1aee0 20 66 6f 72 20 74 68 61 74 20 63 6f 6e 73 74 72   for that constr
1aef0 75 63 74 2e 20 20 54 68 65 20 66 75 6e 63 74 69  uct.  The functi
1af00 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 6f  on returns the o
1af10 6c 64 20 6c 69 6d 69 74 2e 0a 2a 2a 0a 2a 2a 20  ld limit..**.** 
1af20 49 66 20 74 68 65 20 6e 65 77 20 6c 69 6d 69 74  If the new limit
1af30 20 69 73 20 61 20 6e 65 67 61 74 69 76 65 20 6e   is a negative n
1af40 75 6d 62 65 72 2c 20 74 68 65 20 6c 69 6d 69 74  umber, the limit
1af50 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a   is unchanged..*
1af60 2a 20 46 6f 72 20 74 68 65 20 6c 69 6d 69 74 20  * For the limit 
1af70 63 61 74 65 67 6f 72 79 20 6f 66 20 53 51 4c 49  category of SQLI
1af80 54 45 5f 4c 49 4d 49 54 5f 58 59 5a 20 74 68 65  TE_LIMIT_XYZ the
1af90 72 65 20 69 73 20 61 20 68 61 72 64 20 75 70 70  re is a hard upp
1afa0 65 72 0a 2a 2a 20 62 6f 75 6e 64 20 73 65 74 20  er.** bound set 
1afb0 62 79 20 61 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  by a compile-tim
1afc0 65 20 43 2d 70 72 65 70 72 6f 63 65 73 73 20 6d  e C-preprocess m
1afd0 61 63 72 6f 20 6e 61 6d 65 64 20 53 51 4c 49 54  acro named SQLIT
1afe0 45 5f 4d 41 58 5f 58 59 5a 2e 0a 2a 2a 20 28 54  E_MAX_XYZ..** (T
1aff0 68 65 20 22 5f 4c 49 4d 49 54 5f 22 20 69 6e 20  he "_LIMIT_" in 
1b000 74 68 65 20 6e 61 6d 65 20 69 73 20 63 68 61 6e  the name is chan
1b010 67 65 64 20 74 6f 20 22 5f 4d 41 58 5f 22 2e 29  ged to "_MAX_".)
1b020 0a 2a 2a 20 41 74 74 65 6d 70 74 73 20 74 6f 20  .** Attempts to 
1b030 69 6e 63 72 65 61 73 65 20 61 20 6c 69 6d 69 74  increase a limit
1b040 20 61 62 6f 76 65 20 69 74 73 20 68 61 72 64 20   above its hard 
1b050 75 70 70 65 72 20 62 6f 75 6e 64 20 61 72 65 0a  upper bound are.
1b060 2a 2a 20 73 69 6c 65 6e 74 6c 79 20 74 72 75 6e  ** silently trun
1b070 63 61 74 65 64 20 74 6f 20 74 68 65 20 68 61 72  cated to the har
1b080 64 20 75 70 70 65 72 20 6c 69 6d 69 74 2e 0a 2a  d upper limit..*
1b090 2a 0a 2a 2a 20 52 75 6e 20 74 69 6d 65 20 6c 69  *.** Run time li
1b0a0 6d 69 74 73 20 61 72 65 20 69 6e 74 65 6e 64 65  mits are intende
1b0b0 64 20 66 6f 72 20 75 73 65 20 69 6e 20 61 70 70  d for use in app
1b0c0 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6d  lications that m
1b0d0 61 6e 61 67 65 0a 2a 2a 20 62 6f 74 68 20 74 68  anage.** both th
1b0e0 65 69 72 20 6f 77 6e 20 69 6e 74 65 72 6e 61 6c  eir own internal
1b0f0 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 61 6c   database and al
1b100 73 6f 20 64 61 74 61 62 61 73 65 73 20 74 68 61  so databases tha
1b110 74 20 61 72 65 20 63 6f 6e 74 72 6f 6c 6c 65 64  t are controlled
1b120 0a 2a 2a 20 62 79 20 75 6e 74 72 75 73 74 65 64  .** by untrusted
1b130 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 65   external source
1b140 73 2e 20 20 41 6e 20 65 78 61 6d 70 6c 65 20 61  s.  An example a
1b150 70 70 6c 69 63 61 74 69 6f 6e 20 6d 69 67 68 74  pplication might
1b160 20 62 65 20 61 0a 2a 2a 20 77 65 62 62 72 6f 77   be a.** webbrow
1b170 73 65 72 20 74 68 61 74 20 68 61 73 20 69 74 73  ser that has its
1b180 20 6f 77 6e 20 64 61 74 61 62 61 73 65 73 20 66   own databases f
1b190 6f 72 20 73 74 6f 72 69 6e 67 20 68 69 73 74 6f  or storing histo
1b1a0 72 79 20 61 6e 64 0a 2a 2a 20 73 65 70 61 72 61  ry and.** separa
1b1b0 74 65 20 64 61 74 61 62 61 73 65 73 20 63 6f 6e  te databases con
1b1c0 74 72 6f 6c 6c 65 64 20 62 79 20 6a 61 76 61 73  trolled by javas
1b1d0 63 72 69 70 74 20 61 70 70 6c 69 63 61 74 69 6f  cript applicatio
1b1e0 6e 73 20 64 6f 77 6e 6c 6f 61 64 65 64 0a 2a 2a  ns downloaded.**
1b1f0 20 6f 66 66 20 74 68 65 20 69 6e 74 65 72 6e 65   off the interne
1b200 74 2e 20 20 54 68 65 20 69 6e 74 65 72 6e 61 6c  t.  The internal
1b210 20 64 61 74 61 62 61 73 65 73 20 63 61 6e 20 62   databases can b
1b220 65 20 67 69 76 65 6e 20 74 68 65 0a 2a 2a 20 6c  e given the.** l
1b230 61 72 67 65 2c 20 64 65 66 61 75 6c 74 20 6c 69  arge, default li
1b240 6d 69 74 73 2e 20 20 44 61 74 61 62 61 73 65 73  mits.  Databases
1b250 20 6d 61 6e 61 67 65 64 20 62 79 20 65 78 74 65   managed by exte
1b260 72 6e 61 6c 20 73 6f 75 72 63 65 73 20 63 61 6e  rnal sources can
1b270 0a 2a 2a 20 62 65 20 67 69 76 65 6e 20 6d 75 63  .** be given muc
1b280 68 20 73 6d 61 6c 6c 65 72 20 6c 69 6d 69 74 73  h smaller limits
1b290 20 64 65 73 69 67 6e 65 64 20 74 6f 20 70 72 65   designed to pre
1b2a0 76 65 6e 74 20 61 20 64 65 6e 69 61 6c 20 6f 66  vent a denial of
1b2b0 20 73 65 72 76 69 63 65 0a 2a 2a 20 61 74 74 61   service.** atta
1b2c0 63 68 2e 20 20 44 65 76 65 6c 6f 70 65 72 73 20  ch.  Developers 
1b2d0 6d 69 67 68 74 20 61 6c 73 6f 20 77 61 6e 74 20  might also want 
1b2e0 74 6f 20 75 73 65 20 74 68 65 20 5b 73 71 6c 69  to use the [sqli
1b2f0 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a  te3_set_authoriz
1b300 65 72 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61  er()].** interfa
1b310 63 65 20 74 6f 20 66 75 72 74 68 65 72 20 63 6f  ce to further co
1b320 6e 74 72 6f 6c 20 75 6e 74 72 75 73 74 65 64 20  ntrol untrusted 
1b330 53 51 4c 2e 20 20 54 68 65 20 73 69 7a 65 20 6f  SQL.  The size o
1b340 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a  f the database.*
1b350 2a 20 63 72 65 61 74 65 64 20 62 79 20 61 6e 20  * created by an 
1b360 75 6e 74 72 75 73 74 65 64 20 73 63 72 69 70 74  untrusted script
1b370 20 63 61 6e 20 62 65 20 63 6f 6e 74 61 69 6e 65   can be containe
1b380 64 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b  d using the.** [
1b390 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 5d 20  max_page_count] 
1b3a0 5b 50 52 41 47 4d 41 5d 2e 0a 2a 2a 0a 2a 2a 20  [PRAGMA]..**.** 
1b3b0 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69  This interface i
1b3c0 73 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73  s currently cons
1b3d0 69 64 65 72 65 64 20 65 78 70 65 72 69 6d 65 6e  idered experimen
1b3e0 74 61 6c 20 61 6e 64 20 69 73 20 73 75 62 6a 65  tal and is subje
1b3f0 63 74 0a 2a 2a 20 74 6f 20 63 68 61 6e 67 65 20  ct.** to change 
1b400 6f 72 20 72 65 6d 6f 76 61 6c 20 77 69 74 68 6f  or removal witho
1b410 75 74 20 70 72 69 6f 72 20 6e 6f 74 69 63 65 2e  ut prior notice.
1b420 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54  .**.** INVARIANT
1b430 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 37 36 32  S:.**.** {F12762
1b440 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63  } A successful c
1b450 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
1b460 6c 69 6d 69 74 28 44 2c 43 2c 56 29 5d 20 77 68  limit(D,C,V)] wh
1b470 65 72 65 20 56 20 69 73 0a 2a 2a 20 20 20 20 20  ere V is.**     
1b480 20 20 20 20 20 70 6f 73 69 74 69 76 65 20 63 68       positive ch
1b490 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 20 20 20  anges the.**    
1b4a0 20 20 20 20 20 20 6c 69 6d 69 74 20 6f 6e 20 74        limit on t
1b4b0 68 65 20 73 69 7a 65 20 6f 66 20 63 6f 6e 73 74  he size of const
1b4c0 72 75 63 74 20 43 20 69 6e 20 5b 64 61 74 61 62  ruct C in [datab
1b4d0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
1b4e0 44 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f  D.**          to
1b4f0 20 74 68 65 20 6c 65 73 73 6f 72 20 6f 66 20 56   the lessor of V
1b500 20 61 6e 64 20 74 68 65 20 68 61 72 64 20 75 70   and the hard up
1b510 70 65 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 65  per bound on the
1b520 20 73 69 7a 65 0a 2a 2a 20 20 20 20 20 20 20 20   size.**        
1b530 20 20 6f 66 20 43 20 74 68 61 74 20 69 73 20 73    of C that is s
1b540 65 74 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69  et at compile-ti
1b550 6d 65 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 37 36  me..**.** {F1276
1b560 36 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20  6} A successful 
1b570 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
1b580 5f 6c 69 6d 69 74 28 44 2c 43 2c 56 29 5d 20 77  _limit(D,C,V)] w
1b590 68 65 72 65 20 56 20 69 73 20 6e 65 67 61 74 69  here V is negati
1b5a0 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6c  ve.**          l
1b5b0 65 61 76 65 73 20 74 68 65 20 73 74 61 74 65 20  eaves the state 
1b5c0 6f 66 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  of [database con
1b5d0 6e 65 63 74 69 6f 6e 5d 20 44 20 75 6e 63 68 61  nection] D uncha
1b5e0 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32  nged..**.** {F12
1b5f0 37 36 39 7d 20 41 20 73 75 63 63 65 73 73 66 75  769} A successfu
1b600 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  l call to [sqlit
1b610 65 33 5f 6c 69 6d 69 74 28 44 2c 43 2c 56 29 5d  e3_limit(D,C,V)]
1b620 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20   returns the.** 
1b630 20 20 20 20 20 20 20 20 20 76 61 6c 75 65 20 6f           value o
1b640 66 20 74 68 65 20 6c 69 6d 69 74 20 6f 6e 20 74  f the limit on t
1b650 68 65 20 73 69 7a 65 20 6f 66 20 63 6f 6e 73 74  he size of const
1b660 72 75 63 74 20 43 20 69 6e 0a 2a 2a 20 20 20 20  ruct C in.**    
1b670 20 20 20 20 20 20 69 6e 20 5b 64 61 74 61 62 61        in [databa
1b680 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44  se connection] D
1b690 20 61 73 20 69 74 20 77 61 73 20 70 72 69 6f 72   as it was prior
1b6a0 20 74 6f 20 74 68 65 20 63 61 6c 6c 2e 0a 2a 2f   to the call..*/
1b6b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1b6c0 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 73 71  sqlite3_limit(sq
1b6d0 6c 69 74 65 33 2a 2c 20 69 6e 74 20 69 64 2c 20  lite3*, int id, 
1b6e0 69 6e 74 20 6e 65 77 56 61 6c 29 3b 0a 0a 2f 2a  int newVal);../*
1b6f0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75  .** CAPI3REF: Ru
1b700 6e 2d 54 69 6d 65 20 4c 69 6d 69 74 20 43 61 74  n-Time Limit Cat
1b710 65 67 6f 72 69 65 73 20 7b 46 31 32 37 39 30 7d  egories {F12790}
1b720 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 6c  .** KEYWORDS: {l
1b730 69 6d 69 74 20 63 61 74 65 67 6f 72 79 7d 20 7b  imit category} {
1b740 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65 73  limit categories
1b750 7d 0a 2a 2a 20 0a 2a 2a 20 54 68 65 73 65 20 63  }.** .** These c
1b760 6f 6e 73 74 61 6e 74 73 20 64 65 66 69 6e 65 20  onstants define 
1b770 76 61 72 69 6f 75 73 20 61 73 70 65 63 74 73 20  various aspects 
1b780 6f 66 20 61 20 5b 64 61 74 61 62 61 73 65 20 63  of a [database c
1b790 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 74 68  onnection].** th
1b7a0 61 74 20 63 61 6e 20 62 65 20 6c 69 6d 69 74 65  at can be limite
1b7b0 64 20 69 6e 20 73 69 7a 65 20 62 79 20 63 61 6c  d in size by cal
1b7c0 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6c  ls to [sqlite3_l
1b7d0 69 6d 69 74 28 29 5d 2e 0a 2a 2a 20 54 68 65 20  imit()]..** The 
1b7e0 6d 65 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20  meanings of the 
1b7f0 76 61 72 69 6f 75 73 20 6c 69 6d 69 74 73 20 61  various limits a
1b800 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  re as follows:.*
1b810 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74  *.** <dl>.** <dt
1b820 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45  >SQLITE_LIMIT_LE
1b830 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  NGTH</dt>.** <dd
1b840 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 73 69 7a  >The maximum siz
1b850 65 20 6f 66 20 61 6e 79 0a 2a 2a 20 73 74 72 69  e of any.** stri
1b860 6e 67 20 6f 72 20 62 6c 6f 62 20 6f 72 20 74 61  ng or blob or ta
1b870 62 6c 65 20 72 6f 77 2e 3c 64 64 3e 0a 2a 2a 0a  ble row.<dd>.**.
1b880 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49  ** <dt>SQLITE_LI
1b890 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54 48 3c 2f  MIT_SQL_LENGTH</
1b8a0 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d  dt>.** <dd>The m
1b8b0 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66  aximum length of
1b8c0 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   an SQL statemen
1b8d0 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64  t.</dd>.**.** <d
1b8e0 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43  t>SQLITE_LIMIT_C
1b8f0 4f 4c 55 4d 4e 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  OLUMN</dt>.** <d
1b900 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75  d>The maximum nu
1b910 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
1b920 69 6e 20 61 20 74 61 62 6c 65 20 64 65 66 69 6e  in a table defin
1b930 69 74 69 6f 6e 20 6f 72 20 69 6e 20 74 68 65 0a  ition or in the.
1b940 2a 2a 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  ** result set of
1b950 20 61 20 53 45 4c 45 43 54 20 6f 72 20 74 68 65   a SELECT or the
1b960 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
1b970 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e  of columns in an
1b980 20 69 6e 64 65 78 0a 2a 2a 20 6f 72 20 69 6e 20   index.** or in 
1b990 61 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47  an ORDER BY or G
1b9a0 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e 3c  ROUP BY clause.<
1b9b0 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53  /dd>.**.** <dt>S
1b9c0 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52  QLITE_LIMIT_EXPR
1b9d0 5f 44 45 50 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c  _DEPTH</dt>.** <
1b9e0 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 64  dd>The maximum d
1b9f0 65 70 74 68 20 6f 66 20 74 68 65 20 70 61 72 73  epth of the pars
1ba00 65 20 74 72 65 65 20 6f 6e 20 61 6e 79 20 65 78  e tree on any ex
1ba10 70 72 65 73 73 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a  pression.</dd>.*
1ba20 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
1ba30 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53  LIMIT_COMPOUND_S
1ba40 45 4c 45 43 54 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  ELECT</dt>.** <d
1ba50 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75  d>The maximum nu
1ba60 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e  mber of terms in
1ba70 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45   a compound SELE
1ba80 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 3c 2f 64  CT statement.</d
1ba90 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c  d>.**.** <dt>SQL
1baa0 49 54 45 5f 4c 49 4d 49 54 5f 56 44 42 45 5f 4f  ITE_LIMIT_VDBE_O
1bab0 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  P</dt>.** <dd>Th
1bac0 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
1bad0 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 73   of instructions
1bae0 20 69 6e 20 61 20 76 69 72 74 75 61 6c 20 6d 61   in a virtual ma
1baf0 63 68 69 6e 65 20 70 72 6f 67 72 61 6d 0a 2a 2a  chine program.**
1bb00 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
1bb10 6e 74 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d  nt an SQL statem
1bb20 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ent.</dd>.**.** 
1bb30 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54  <dt>SQLITE_LIMIT
1bb40 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 3c 2f 64  _FUNCTION_ARG</d
1bb50 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61  t>.** <dd>The ma
1bb60 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
1bb70 61 72 67 75 6d 65 6e 74 73 20 6f 6e 20 61 20 66  arguments on a f
1bb80 75 6e 63 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a  unction.</dd>.**
1bb90 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c  .** <dt>SQLITE_L
1bba0 49 4d 49 54 5f 41 54 54 41 43 48 45 44 3c 2f 64  IMIT_ATTACHED</d
1bbb0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61  t>.** <dd>The ma
1bbc0 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
1bbd0 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
1bbe0 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  es.</dd>.**.** <
1bbf0 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  dt>SQLITE_LIMIT_
1bc00 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e  LIKE_PATTERN_LEN
1bc10 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  GTH</dt>.** <dd>
1bc20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67  The maximum leng
1bc30 74 68 20 6f 66 20 74 68 65 20 70 61 74 74 65 72  th of the patter
1bc40 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  n argument to th
1bc50 65 20 4c 49 4b 45 20 6f 72 0a 2a 2a 20 47 4c 4f  e LIKE or.** GLO
1bc60 42 20 6f 70 65 72 61 74 6f 72 73 2e 3c 2f 64 64  B operators.</dd
1bc70 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
1bc80 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c  TE_LIMIT_VARIABL
1bc90 45 5f 4e 55 4d 42 45 52 3c 2f 64 74 3e 0a 2a 2a  E_NUMBER</dt>.**
1bca0 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d   <dd>The maximum
1bcb0 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 72 69 61   number of varia
1bcc0 62 6c 65 73 20 69 6e 20 61 6e 20 53 51 4c 20 73  bles in an SQL s
1bcd0 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 63 61  tatement that ca
1bce0 6e 0a 2a 2a 20 62 65 20 62 6f 75 6e 64 2e 3c 2f  n.** be bound.</
1bcf0 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a  dd>.** </dl>.*/.
1bd00 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
1bd10 49 4d 49 54 5f 4c 45 4e 47 54 48 20 20 20 20 20  IMIT_LENGTH     
1bd20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
1bd30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1bd40 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54 48  LIMIT_SQL_LENGTH
1bd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bd60 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  1.#define SQLITE
1bd70 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 20 20 20  _LIMIT_COLUMN   
1bd80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bd90 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   2.#define SQLIT
1bda0 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f 44 45 50  E_LIMIT_EXPR_DEP
1bdb0 54 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TH              
1bdc0 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    3.#define SQLI
1bdd0 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55 4e  TE_LIMIT_COMPOUN
1bde0 44 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 20  D_SELECT        
1bdf0 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c     4.#define SQL
1be00 49 54 45 5f 4c 49 4d 49 54 5f 56 44 42 45 5f 4f  ITE_LIMIT_VDBE_O
1be10 50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  P               
1be20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 53 51      5.#define SQ
1be30 4c 49 54 45 5f 4c 49 4d 49 54 5f 46 55 4e 43 54  LITE_LIMIT_FUNCT
1be40 49 4f 4e 5f 41 52 47 20 20 20 20 20 20 20 20 20  ION_ARG         
1be50 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 53       6.#define S
1be60 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41  QLITE_LIMIT_ATTA
1be70 43 48 45 44 20 20 20 20 20 20 20 20 20 20 20 20  CHED            
1be80 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20        7.#define 
1be90 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b  SQLITE_LIMIT_LIK
1bea0 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48  E_PATTERN_LENGTH
1beb0 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65         8.#define
1bec0 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41   SQLITE_LIMIT_VA
1bed0 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 20 20 20  RIABLE_NUMBER   
1bee0 20 20 20 20 20 20 20 20 39 0a 0a 2f 2a 0a 2a 2a          9../*.**
1bef0 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 70 69   CAPI3REF: Compi
1bf00 6c 69 6e 67 20 41 6e 20 53 51 4c 20 53 74 61 74  ling An SQL Stat
1bf10 65 6d 65 6e 74 20 7b 46 31 33 30 31 30 7d 0a 2a  ement {F13010}.*
1bf20 2a 0a 2a 2a 20 54 6f 20 65 78 65 63 75 74 65 20  *.** To execute 
1bf30 61 6e 20 53 51 4c 20 71 75 65 72 79 2c 20 69 74  an SQL query, it
1bf40 20 6d 75 73 74 20 66 69 72 73 74 20 62 65 20 63   must first be c
1bf50 6f 6d 70 69 6c 65 64 20 69 6e 74 6f 20 61 20 62  ompiled into a b
1bf60 79 74 65 2d 63 6f 64 65 0a 2a 2a 20 70 72 6f 67  yte-code.** prog
1bf70 72 61 6d 20 75 73 69 6e 67 20 6f 6e 65 20 6f 66  ram using one of
1bf80 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2e   these routines.
1bf90 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73   .**.** The firs
1bfa0 74 20 61 72 67 75 6d 65 6e 74 20 22 64 62 22 20  t argument "db" 
1bfb0 69 73 20 61 6e 20 5b 64 61 74 61 62 61 73 65 20  is an [database 
1bfc0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 0a 2a 2a 20  connection] .** 
1bfd0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 20  obtained from a 
1bfe0 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 5b 73  prior call to [s
1bff0 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20  qlite3_open()], 
1c000 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32  [sqlite3_open_v2
1c010 28 29 5d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74  ()].** or [sqlit
1c020 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 20 0a 2a  e3_open16()]. .*
1c030 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67  * The second arg
1c040 75 6d 65 6e 74 20 22 7a 53 71 6c 22 20 69 73 20  ument "zSql" is 
1c050 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  the statement to
1c060 20 62 65 20 63 6f 6d 70 69 6c 65 64 2c 20 65 6e   be compiled, en
1c070 63 6f 64 65 64 0a 2a 2a 20 61 73 20 65 69 74 68  coded.** as eith
1c080 65 72 20 55 54 46 2d 38 20 6f 72 20 55 54 46 2d  er UTF-8 or UTF-
1c090 31 36 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  16.  The sqlite3
1c0a0 5f 70 72 65 70 61 72 65 28 29 20 61 6e 64 20 73  _prepare() and s
1c0b0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
1c0c0 32 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  2().** interface
1c0d0 73 20 75 73 65 73 20 55 54 46 2d 38 20 61 6e 64  s uses UTF-8 and
1c0e0 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
1c0f0 31 36 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  16() and sqlite3
1c100 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 0a  _prepare16_v2().
1c110 2a 2a 20 75 73 65 20 55 54 46 2d 31 36 2e 20 7b  ** use UTF-16. {
1c120 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  END}.**.** If th
1c130 65 20 6e 42 79 74 65 20 61 72 67 75 6d 65 6e 74  e nByte argument
1c140 20 69 73 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e   is less.** than
1c150 20 7a 65 72 6f 2c 20 74 68 65 6e 20 7a 53 71 6c   zero, then zSql
1c160 20 69 73 20 72 65 61 64 20 75 70 20 74 6f 20 74   is read up to t
1c170 68 65 20 66 69 72 73 74 20 7a 65 72 6f 20 74 65  he first zero te
1c180 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 20 49 66 20  rminator..** If 
1c190 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e 65 67  nByte is non-neg
1c1a0 61 74 69 76 65 2c 20 74 68 65 6e 20 69 74 20 69  ative, then it i
1c1b0 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75  s the maximum nu
1c1c0 6d 62 65 72 20 6f 66 20 0a 2a 2a 20 62 79 74 65  mber of .** byte
1c1d0 73 20 72 65 61 64 20 66 72 6f 6d 20 7a 53 71 6c  s read from zSql
1c1e0 2e 20 20 57 68 65 6e 20 6e 42 79 74 65 20 69 73  .  When nByte is
1c1f0 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74   non-negative, t
1c200 68 65 0a 2a 2a 20 7a 53 71 6c 20 73 74 72 69 6e  he.** zSql strin
1c210 67 20 65 6e 64 73 20 61 74 20 65 69 74 68 65 72  g ends at either
1c220 20 74 68 65 20 66 69 72 73 74 20 27 5c 30 30 30   the first '\000
1c230 27 20 6f 72 20 27 5c 75 30 30 30 30 27 20 63 68  ' or '\u0000' ch
1c240 61 72 61 63 74 65 72 20 6f 72 20 0a 2a 2a 20 74  aracter or .** t
1c250 68 65 20 6e 42 79 74 65 2d 74 68 20 62 79 74 65  he nByte-th byte
1c260 2c 20 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65  , whichever come
1c270 73 20 66 69 72 73 74 2e 20 49 66 20 74 68 65 20  s first. If the 
1c280 63 61 6c 6c 65 72 20 6b 6e 6f 77 73 0a 2a 2a 20  caller knows.** 
1c290 74 68 61 74 20 74 68 65 20 73 75 70 70 6c 69 65  that the supplie
1c2a0 64 20 73 74 72 69 6e 67 20 69 73 20 6e 75 6c 2d  d string is nul-
1c2b0 74 65 72 6d 69 6e 61 74 65 64 2c 20 74 68 65 6e  terminated, then
1c2c0 20 74 68 65 72 65 20 69 73 20 61 20 73 6d 61 6c   there is a smal
1c2d0 6c 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65  l.** performance
1c2e0 20 61 64 76 61 6e 74 61 67 65 20 74 6f 20 62 65   advantage to be
1c2f0 20 68 61 64 20 62 79 20 70 61 73 73 69 6e 67 20   had by passing 
1c300 61 6e 20 6e 42 79 74 65 20 70 61 72 61 6d 65 74  an nByte paramet
1c310 65 72 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 65  er that .** is e
1c320 71 75 61 6c 20 74 6f 20 74 68 65 20 6e 75 6d 62  qual to the numb
1c330 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74  er of bytes in t
1c340 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20  he input string 
1c350 3c 69 3e 69 6e 63 6c 75 64 69 6e 67 3c 2f 69 3e  <i>including</i>
1c360 20 0a 2a 2a 20 74 68 65 20 6e 75 6c 2d 74 65 72   .** the nul-ter
1c370 6d 69 6e 61 74 6f 72 20 62 79 74 65 73 2e 7b 45  minator bytes.{E
1c380 4e 44 7d 0a 2a 2a 0a 2a 2a 20 2a 70 7a 54 61 69  ND}.**.** *pzTai
1c390 6c 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69  l is made to poi
1c3a0 6e 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  nt to the first 
1c3b0 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65 6e  byte past the en
1c3c0 64 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69 72 73  d of the.** firs
1c3d0 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  t SQL statement 
1c3e0 69 6e 20 7a 53 71 6c 2e 20 20 54 68 65 73 65 20  in zSql.  These 
1c3f0 72 6f 75 74 69 6e 65 73 20 6f 6e 6c 79 20 63 6f  routines only co
1c400 6d 70 69 6c 65 73 20 74 68 65 20 66 69 72 73 74  mpiles the first
1c410 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 6e  .** statement in
1c420 20 7a 53 71 6c 2c 20 73 6f 20 2a 70 7a 54 61 69   zSql, so *pzTai
1c430 6c 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69  l is left pointi
1c440 6e 67 20 74 6f 20 77 68 61 74 20 72 65 6d 61 69  ng to what remai
1c450 6e 73 0a 2a 2a 20 75 6e 63 6f 6d 70 69 6c 65 64  ns.** uncompiled
1c460 2e 0a 2a 2a 0a 2a 2a 20 2a 70 70 53 74 6d 74 20  ..**.** *ppStmt 
1c470 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67  is left pointing
1c480 20 74 6f 20 61 20 63 6f 6d 70 69 6c 65 64 20 5b   to a compiled [
1c490 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
1c4a0 6e 74 5d 20 74 68 61 74 20 63 61 6e 20 62 65 0a  nt] that can be.
1c4b0 2a 2a 20 65 78 65 63 75 74 65 64 20 75 73 69 6e  ** executed usin
1c4c0 67 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  g [sqlite3_step(
1c4d0 29 5d 2e 20 20 4f 72 20 69 66 20 74 68 65 72 65  )].  Or if there
1c4e0 20 69 73 20 61 6e 20 65 72 72 6f 72 2c 20 2a 70   is an error, *p
1c4f0 70 53 74 6d 74 20 69 73 0a 2a 2a 20 73 65 74 20  pStmt is.** set 
1c500 74 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 74 68 65  to NULL.  If the
1c510 20 69 6e 70 75 74 20 74 65 78 74 20 63 6f 6e 74   input text cont
1c520 61 69 6e 73 20 6e 6f 20 53 51 4c 20 28 69 66 20  ains no SQL (if 
1c530 74 68 65 20 69 6e 70 75 74 0a 2a 2a 20 69 73 20  the input.** is 
1c540 61 6e 64 20 65 6d 70 74 79 20 73 74 72 69 6e 67  and empty string
1c550 20 6f 72 20 61 20 63 6f 6d 6d 65 6e 74 29 20 74   or a comment) t
1c560 68 65 6e 20 2a 70 70 53 74 6d 74 20 69 73 20 73  hen *ppStmt is s
1c570 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 20 7b  et to NULL..** {
1c580 55 31 33 30 31 38 7d 20 54 68 65 20 63 61 6c 6c  U13018} The call
1c590 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20 69 73  ing procedure is
1c5a0 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72   responsible for
1c5b0 20 64 65 6c 65 74 69 6e 67 20 74 68 65 0a 2a 2a   deleting the.**
1c5c0 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74   compiled SQL st
1c5d0 61 74 65 6d 65 6e 74 0a 2a 2a 20 75 73 69 6e 67  atement.** using
1c5e0 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
1c5f0 7a 65 28 29 5d 20 61 66 74 65 72 20 69 74 20 68  ze()] after it h
1c600 61 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68  as finished with
1c610 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75   it..**.** On su
1c620 63 63 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f  ccess, [SQLITE_O
1c630 4b 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  K] is returned. 
1c640 20 4f 74 68 65 72 77 69 73 65 20 61 6e 20 0a 2a   Otherwise an .*
1c650 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69  * [error code] i
1c660 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
1c670 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 70 72  * The sqlite3_pr
1c680 65 70 61 72 65 5f 76 32 28 29 20 61 6e 64 20 73  epare_v2() and s
1c690 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
1c6a0 5f 76 32 28 29 20 69 6e 74 65 72 66 61 63 65 73  _v2() interfaces
1c6b0 20 61 72 65 0a 2a 2a 20 72 65 63 6f 6d 6d 65 6e   are.** recommen
1c6c0 64 65 64 20 66 6f 72 20 61 6c 6c 20 6e 65 77 20  ded for all new 
1c6d0 70 72 6f 67 72 61 6d 73 2e 20 54 68 65 20 74 77  programs. The tw
1c6e0 6f 20 6f 6c 64 65 72 20 69 6e 74 65 72 66 61 63  o older interfac
1c6f0 65 73 20 61 72 65 20 72 65 74 61 69 6e 65 64 0a  es are retained.
1c700 2a 2a 20 66 6f 72 20 62 61 63 6b 77 61 72 64 73  ** for backwards
1c710 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2c 20   compatibility, 
1c720 62 75 74 20 74 68 65 69 72 20 75 73 65 20 69 73  but their use is
1c730 20 64 69 73 63 6f 75 72 61 67 65 64 2e 0a 2a 2a   discouraged..**
1c740 20 49 6e 20 74 68 65 20 22 76 32 22 20 69 6e 74   In the "v2" int
1c750 65 72 66 61 63 65 73 2c 20 74 68 65 20 70 72 65  erfaces, the pre
1c760 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a  pared statement.
1c770 2a 2a 20 74 68 61 74 20 69 73 20 72 65 74 75 72  ** that is retur
1c780 6e 65 64 20 28 74 68 65 20 5b 73 71 6c 69 74 65  ned (the [sqlite
1c790 33 5f 73 74 6d 74 5d 20 6f 62 6a 65 63 74 29 20  3_stmt] object) 
1c7a0 63 6f 6e 74 61 69 6e 73 20 61 20 63 6f 70 79 20  contains a copy 
1c7b0 6f 66 20 74 68 65 20 0a 2a 2a 20 6f 72 69 67 69  of the .** origi
1c7c0 6e 61 6c 20 53 51 4c 20 74 65 78 74 2e 20 7b 45  nal SQL text. {E
1c7d0 4e 44 7d 20 54 68 69 73 20 63 61 75 73 65 73 20  ND} This causes 
1c7e0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  the [sqlite3_ste
1c7f0 70 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 74  p()] interface t
1c800 6f 0a 2a 2a 20 62 65 68 61 76 65 20 61 20 64 69  o.** behave a di
1c810 66 66 65 72 65 6e 74 6c 79 20 69 6e 20 74 77 6f  fferently in two
1c820 20 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f 6c   ways:.**.** <ol
1c830 3e 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 49 66 20  >.** <li>.** If 
1c840 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
1c850 65 6d 61 20 63 68 61 6e 67 65 73 2c 20 69 6e 73  ema changes, ins
1c860 74 65 61 64 20 6f 66 20 72 65 74 75 72 6e 69 6e  tead of returnin
1c870 67 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  g [SQLITE_SCHEMA
1c880 5d 20 61 73 20 69 74 0a 2a 2a 20 61 6c 77 61 79  ] as it.** alway
1c890 73 20 75 73 65 64 20 74 6f 20 64 6f 2c 20 5b 73  s used to do, [s
1c8a0 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 77  qlite3_step()] w
1c8b0 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  ill automaticall
1c8c0 79 20 72 65 63 6f 6d 70 69 6c 65 20 74 68 65 20  y recompile the 
1c8d0 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  SQL.** statement
1c8e0 20 61 6e 64 20 74 72 79 20 74 6f 20 72 75 6e 20   and try to run 
1c8f0 69 74 20 61 67 61 69 6e 2e 20 20 49 66 20 74 68  it again.  If th
1c900 65 20 73 63 68 65 6d 61 20 68 61 73 20 63 68 61  e schema has cha
1c910 6e 67 65 64 20 69 6e 0a 2a 2a 20 61 20 77 61 79  nged in.** a way
1c920 20 74 68 61 74 20 6d 61 6b 65 73 20 74 68 65 20   that makes the 
1c930 73 74 61 74 65 6d 65 6e 74 20 6e 6f 20 6c 6f 6e  statement no lon
1c940 67 65 72 20 76 61 6c 69 64 2c 20 5b 73 71 6c 69  ger valid, [sqli
1c950 74 65 33 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c  te3_step()] will
1c960 20 73 74 69 6c 6c 0a 2a 2a 20 72 65 74 75 72 6e   still.** return
1c970 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d   [SQLITE_SCHEMA]
1c980 2e 20 20 42 75 74 20 75 6e 6c 69 6b 65 20 74 68  .  But unlike th
1c990 65 20 6c 65 67 61 63 79 20 62 65 68 61 76 69 6f  e legacy behavio
1c9a0 72 2c 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 53  r, .** [SQLITE_S
1c9b0 43 48 45 4d 41 5d 20 69 73 20 6e 6f 77 20 61 20  CHEMA] is now a 
1c9c0 66 61 74 61 6c 20 65 72 72 6f 72 2e 20 20 43 61  fatal error.  Ca
1c9d0 6c 6c 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65  lling.** [sqlite
1c9e0 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20  3_prepare_v2()] 
1c9f0 61 67 61 69 6e 20 77 69 6c 6c 20 6e 6f 74 20 6d  again will not m
1ca00 61 6b 65 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72  ake the.** error
1ca10 20 67 6f 20 61 77 61 79 2e 20 20 4e 6f 74 65 3a   go away.  Note:
1ca20 20 75 73 65 20 5b 73 71 6c 69 74 65 33 5f 65 72   use [sqlite3_er
1ca30 72 6d 73 67 28 29 5d 20 74 6f 20 66 69 6e 64 20  rmsg()] to find 
1ca40 74 68 65 20 74 65 78 74 0a 2a 2a 20 6f 66 20 74  the text.** of t
1ca50 68 65 20 70 61 72 73 69 6e 67 20 65 72 72 6f 72  he parsing error
1ca60 20 74 68 61 74 20 72 65 73 75 6c 74 73 20 69 6e   that results in
1ca70 20 61 6e 20 5b 53 51 4c 49 54 45 5f 53 43 48 45   an [SQLITE_SCHE
1ca80 4d 41 5d 20 72 65 74 75 72 6e 2e 20 7b 45 4e 44  MA] return. {END
1ca90 7d 0a 2a 2a 20 3c 2f 6c 69 3e 0a 2a 2a 0a 2a 2a  }.** </li>.**.**
1caa0 20 3c 6c 69 3e 0a 2a 2a 20 57 68 65 6e 20 61 6e   <li>.** When an
1cab0 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 0a   error occurs, .
1cac0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  ** [sqlite3_step
1cad0 28 29 5d 20 77 69 6c 6c 20 72 65 74 75 72 6e 20  ()] will return 
1cae0 6f 6e 65 20 6f 66 20 74 68 65 20 64 65 74 61 69  one of the detai
1caf0 6c 65 64 20 0a 2a 2a 20 5b 65 72 72 6f 72 20 63  led .** [error c
1cb00 6f 64 65 73 5d 20 6f 72 20 5b 65 78 74 65 6e 64  odes] or [extend
1cb10 65 64 20 65 72 72 6f 72 20 63 6f 64 65 73 5d 2e  ed error codes].
1cb20 20 0a 2a 2a 20 54 68 65 20 6c 65 67 61 63 79 20   .** The legacy 
1cb30 62 65 68 61 76 69 6f 72 20 77 61 73 20 74 68 61  behavior was tha
1cb40 74 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  t [sqlite3_step(
1cb50 29 5d 20 77 6f 75 6c 64 20 6f 6e 6c 79 20 72 65  )] would only re
1cb60 74 75 72 6e 20 61 20 67 65 6e 65 72 69 63 0a 2a  turn a generic.*
1cb70 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d  * [SQLITE_ERROR]
1cb80 20 72 65 73 75 6c 74 20 63 6f 64 65 20 61 6e 64   result code and
1cb90 20 79 6f 75 20 77 6f 75 6c 64 20 68 61 76 65 20   you would have 
1cba0 74 6f 20 6d 61 6b 65 20 61 20 73 65 63 6f 6e 64  to make a second
1cbb0 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c   call to.** [sql
1cbc0 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 69 6e  ite3_reset()] in
1cbd0 20 6f 72 64 65 72 20 74 6f 20 66 69 6e 64 20 74   order to find t
1cbe0 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 63 61  he underlying ca
1cbf0 75 73 65 20 6f 66 20 74 68 65 20 70 72 6f 62 6c  use of the probl
1cc00 65 6d 2e 0a 2a 2a 20 57 69 74 68 20 74 68 65 20  em..** With the 
1cc10 22 76 32 22 20 70 72 65 70 61 72 65 20 69 6e 74  "v2" prepare int
1cc20 65 72 66 61 63 65 73 2c 20 74 68 65 20 75 6e 64  erfaces, the und
1cc30 65 72 6c 79 69 6e 67 20 72 65 61 73 6f 6e 20 66  erlying reason f
1cc40 6f 72 20 74 68 65 20 65 72 72 6f 72 20 69 73 0a  or the error is.
1cc50 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 6d 6d 65  ** returned imme
1cc60 64 69 61 74 65 6c 79 2e 0a 2a 2a 20 3c 2f 6c 69  diately..** </li
1cc70 3e 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a  >.** </ol>.**.**
1cc80 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a   INVARIANTS:.**.
1cc90 2a 2a 20 7b 46 31 33 30 31 31 7d 20 54 68 65 20  ** {F13011} The 
1cca0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
1ccb0 28 64 62 2c 7a 53 71 6c 2c 2e 2e 2e 29 5d 20 61  (db,zSql,...)] a
1ccc0 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  nd.**          [
1ccd0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
1cce0 76 32 28 64 62 2c 7a 53 71 6c 2c 2e 2e 2e 29 5d  v2(db,zSql,...)]
1ccf0 20 69 6e 74 65 72 66 61 63 65 73 20 69 6e 74 65   interfaces inte
1cd00 72 70 72 65 74 20 74 68 65 0a 2a 2a 20 20 20 20  rpret the.**    
1cd10 20 20 20 20 20 20 74 65 78 74 20 69 6e 20 74 68        text in th
1cd20 65 69 72 20 7a 53 71 6c 20 70 61 72 61 6d 65 74  eir zSql paramet
1cd30 65 72 20 61 73 20 55 54 46 2d 38 2e 0a 2a 2a 0a  er as UTF-8..**.
1cd40 2a 2a 20 7b 46 31 33 30 31 32 7d 20 54 68 65 20  ** {F13012} The 
1cd50 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
1cd60 31 36 28 64 62 2c 7a 53 71 6c 2c 2e 2e 2e 29 5d  16(db,zSql,...)]
1cd70 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20   and.**         
1cd80 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
1cd90 65 31 36 5f 76 32 28 64 62 2c 7a 53 71 6c 2c 2e  e16_v2(db,zSql,.
1cda0 2e 2e 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20  ..)] interfaces 
1cdb0 69 6e 74 65 72 70 72 65 74 20 74 68 65 0a 2a 2a  interpret the.**
1cdc0 20 20 20 20 20 20 20 20 20 20 74 65 78 74 20 69            text i
1cdd0 6e 20 74 68 65 69 72 20 7a 53 71 6c 20 70 61 72  n their zSql par
1cde0 61 6d 65 74 65 72 20 61 73 20 55 54 46 2d 31 36  ameter as UTF-16
1cdf0 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62   in the native b
1ce00 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a 2a  yte order..**.**
1ce10 20 7b 46 31 33 30 31 33 7d 20 49 66 20 74 68 65   {F13013} If the
1ce20 20 6e 42 79 74 65 20 61 72 67 75 6d 65 6e 74 20   nByte argument 
1ce30 74 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  to [sqlite3_prep
1ce40 61 72 65 5f 76 32 28 64 62 2c 7a 53 71 6c 2c 6e  are_v2(db,zSql,n
1ce50 42 79 74 65 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 20  Byte,...)].**   
1ce60 20 20 20 20 20 20 20 61 6e 64 20 69 74 73 20 76         and its v
1ce70 61 72 69 61 6e 74 73 20 69 73 20 6c 65 73 73 20  ariants is less 
1ce80 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20  than zero, then 
1ce90 53 51 4c 20 74 65 78 74 20 69 73 0a 2a 2a 20 20  SQL text is.**  
1cea0 20 20 20 20 20 20 20 20 72 65 61 64 20 66 72 6f          read fro
1ceb0 6d 20 7a 53 71 6c 20 69 73 20 72 65 61 64 20 75  m zSql is read u
1cec0 70 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 7a  p to the first z
1ced0 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a  ero terminator..
1cee0 2a 2a 0a 2a 2a 20 7b 46 31 33 30 31 34 7d 20 49  **.** {F13014} I
1cef0 66 20 74 68 65 20 6e 42 79 74 65 20 61 72 67 75  f the nByte argu
1cf00 6d 65 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33  ment to [sqlite3
1cf10 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c 7a  _prepare_v2(db,z
1cf20 53 71 6c 2c 6e 42 79 74 65 2c 2e 2e 2e 29 5d 0a  Sql,nByte,...)].
1cf30 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e 64 20  **          and 
1cf40 69 74 73 20 76 61 72 69 61 6e 74 73 20 69 73 20  its variants is 
1cf50 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 68  non-negative, th
1cf60 65 6e 20 61 74 20 6d 6f 73 74 20 6e 42 79 74 65  en at most nByte
1cf70 73 20 62 79 74 65 73 0a 2a 2a 20 20 20 20 20 20  s bytes.**      
1cf80 20 20 20 20 53 51 4c 20 74 65 78 74 20 69 73 20      SQL text is 
1cf90 72 65 61 64 20 66 72 6f 6d 20 7a 53 71 6c 2e 0a  read from zSql..
1cfa0 2a 2a 0a 2a 2a 20 7b 46 31 33 30 31 35 7d 20 49  **.** {F13015} I
1cfb0 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  n [sqlite3_prepa
1cfc0 72 65 5f 76 32 28 64 62 2c 7a 53 71 6c 2c 4e 2c  re_v2(db,zSql,N,
1cfd0 50 2c 70 7a 54 61 69 6c 29 5d 20 61 6e 64 20 69  P,pzTail)] and i
1cfe0 74 73 20 76 61 72 69 61 6e 74 73 0a 2a 2a 20 20  ts variants.**  
1cff0 20 20 20 20 20 20 20 20 69 66 20 74 68 65 20 7a          if the z
1d000 53 71 6c 20 69 6e 70 75 74 20 74 65 78 74 20 63  Sql input text c
1d010 6f 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74 68 61  ontains more tha
1d020 6e 20 6f 6e 65 20 53 51 4c 20 73 74 61 74 65 6d  n one SQL statem
1d030 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ent.**          
1d040 61 6e 64 20 70 7a 54 61 69 6c 20 69 73 20 6e 6f  and pzTail is no
1d050 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 2a 70 7a  t NULL, then *pz
1d060 54 61 69 6c 20 69 73 20 6d 61 64 65 20 74 6f 20  Tail is made to 
1d070 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20  point to the.** 
1d080 20 20 20 20 20 20 20 20 20 66 69 72 73 74 20 62           first b
1d090 79 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64  yte past the end
1d0a0 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 53 51   of the first SQ
1d0b0 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 7a  L statement in z
1d0c0 53 71 6c 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20  Sql..**         
1d0d0 20 3c 74 6f 64 6f 3e 57 68 61 74 20 64 6f 65 73   <todo>What does
1d0e0 20 2a 70 7a 54 61 69 6c 20 70 6f 69 6e 74 20 74   *pzTail point t
1d0f0 6f 20 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e  o if there is on
1d100 65 20 73 74 61 74 65 6d 65 6e 74 3f 3c 2f 74 6f  e statement?</to
1d110 64 6f 3e 0a 2a 2a 0a 2a 2a 20 7b 46 31 33 30 31  do>.**.** {F1301
1d120 36 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20  6} A successful 
1d130 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
1d140 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c 7a  _prepare_v2(db,z
1d150 53 71 6c 2c 4e 2c 70 70 53 74 6d 74 2c 2e 2e 2e  Sql,N,ppStmt,...
1d160 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  )].**          o
1d170 72 20 6f 6e 65 20 6f 66 20 69 74 73 20 76 61 72  r one of its var
1d180 69 61 6e 74 73 20 77 72 69 74 65 73 20 69 6e 74  iants writes int
1d190 6f 20 2a 70 70 53 74 6d 74 20 61 20 70 6f 69 6e  o *ppStmt a poin
1d1a0 74 65 72 20 74 6f 20 61 20 6e 65 77 0a 2a 2a 20  ter to a new.** 
1d1b0 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 72           [prepar
1d1c0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 6f 72  ed statement] or
1d1d0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 4e 55   a pointer to NU
1d1e0 4c 4c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69  LL.**          i
1d1f0 66 20 7a 53 71 6c 20 63 6f 6e 74 61 69 6e 73 20  f zSql contains 
1d200 6e 6f 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68  nothing other th
1d210 61 6e 20 77 68 69 74 65 73 70 61 63 65 20 6f 72  an whitespace or
1d220 20 63 6f 6d 6d 65 6e 74 73 2e 20 0a 2a 2a 0a 2a   comments. .**.*
1d230 2a 20 7b 46 31 33 30 31 39 7d 20 54 68 65 20 5b  * {F13019} The [
1d240 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
1d250 76 32 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20  v2()] interface 
1d260 61 6e 64 20 69 74 73 20 76 61 72 69 61 6e 74 73  and its variants
1d270 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20   return.**      
1d280 20 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20      [SQLITE_OK] 
1d290 6f 72 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74  or an appropriat
1d2a0 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 75  e [error code] u
1d2b0 70 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a  pon failure..**.
1d2c0 2a 2a 20 7b 46 31 33 30 32 31 7d 20 42 65 66 6f  ** {F13021} Befo
1d2d0 72 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  re [sqlite3_prep
1d2e0 61 72 65 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74  are(db,zSql,nByt
1d2f0 65 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29  e,ppStmt,pzTail)
1d300 5d 20 6f 72 20 69 74 73 0a 2a 2a 20 20 20 20 20  ] or its.**     
1d310 20 20 20 20 20 76 61 72 69 61 6e 74 73 20 72 65       variants re
1d320 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 28  turns an error (
1d330 61 6e 79 20 76 61 6c 75 65 20 6f 74 68 65 72 20  any value other 
1d340 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d  than [SQLITE_OK]
1d350 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 74  ).**          it
1d360 20 66 69 72 73 74 20 73 65 74 73 20 2a 70 70 53   first sets *ppS
1d370 74 6d 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a  tmt to NULL..*/.
1d380 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1d390 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 0a  qlite3_prepare(.
1d3a0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
1d3b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
1d3c0 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  abase handle */.
1d3d0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
1d3e0 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c  ql,       /* SQL
1d3f0 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d   statement, UTF-
1d400 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69  8 encoded */.  i
1d410 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20  nt nByte,       
1d420 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
1d430 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c  m length of zSql
1d440 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20   in bytes. */.  
1d450 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70  sqlite3_stmt **p
1d460 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20  pStmt,  /* OUT: 
1d470 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65  Statement handle
1d480 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
1d490 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a   **pzTail     /*
1d4a0 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f   OUT: Pointer to
1d4b0 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e 20   unused portion 
1d4c0 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53 51  of zSql */.);.SQ
1d4d0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1d4e0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
1d4f0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
1d500 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
1d510 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f  tabase handle */
1d520 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1d530 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 51  Sql,       /* SQ
1d540 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 46  L statement, UTF
1d550 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20  -8 encoded */.  
1d560 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20  int nByte,      
1d570 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d          /* Maxim
1d580 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71  um length of zSq
1d590 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20  l in bytes. */. 
1d5a0 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
1d5b0 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a  ppStmt,  /* OUT:
1d5c0 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c   Statement handl
1d5d0 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
1d5e0 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f  r **pzTail     /
1d5f0 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74  * OUT: Pointer t
1d600 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e  o unused portion
1d610 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53   of zSql */.);.S
1d620 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1d630 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28  lite3_prepare16(
1d640 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
1d650 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
1d660 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f  tabase handle */
1d670 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
1d680 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 51  Sql,       /* SQ
1d690 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 46  L statement, UTF
1d6a0 2d 31 36 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20  -16 encoded */. 
1d6b0 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20   int nByte,     
1d6c0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69           /* Maxi
1d6d0 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53  mum length of zS
1d6e0 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a  ql in bytes. */.
1d6f0 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
1d700 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54  *ppStmt,  /* OUT
1d710 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64  : Statement hand
1d720 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  le */.  const vo
1d730 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20  id **pzTail     
1d740 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20  /* OUT: Pointer 
1d750 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f  to unused portio
1d760 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a  n of zSql */.);.
1d770 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1d780 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
1d790 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  _v2(.  sqlite3 *
1d7a0 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  db,            /
1d7b0 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c  * Database handl
1d7c0 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69  e */.  const voi
1d7d0 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f  d *zSql,       /
1d7e0 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c  * SQL statement,
1d7f0 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20   UTF-16 encoded 
1d800 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20  */.  int nByte, 
1d810 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1d820 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f  Maximum length o
1d830 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e  f zSql in bytes.
1d840 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
1d850 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a  mt **ppStmt,  /*
1d860 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20   OUT: Statement 
1d870 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  handle */.  cons
1d880 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20  t void **pzTail 
1d890 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e      /* OUT: Poin
1d8a0 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f  ter to unused po
1d8b0 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f  rtion of zSql */
1d8c0 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 52  .);../*.** CAPIR
1d8d0 45 46 3a 20 52 65 74 72 69 65 76 69 6e 67 20 53  EF: Retrieving S
1d8e0 74 61 74 65 6d 65 6e 74 20 53 51 4c 20 7b 46 31  tatement SQL {F1
1d8f0 33 31 30 30 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73  3100}.**.** This
1d900 20 69 6e 74 65 72 65 66 61 63 65 20 63 61 6e 20   intereface can 
1d910 62 65 20 75 73 65 64 20 74 6f 20 72 65 74 72 69  be used to retri
1d920 65 76 65 20 61 20 73 61 76 65 64 20 63 6f 70 79  eve a saved copy
1d930 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   of the original
1d940 0a 2a 2a 20 53 51 4c 20 74 65 78 74 20 75 73 65  .** SQL text use
1d950 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 5b 70  d to create a [p
1d960 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
1d970 74 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49  t]..**.** INVARI
1d980 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31 33  ANTS:.**.** {F13
1d990 31 30 31 7d 20 49 66 20 74 68 65 20 5b 70 72 65  101} If the [pre
1d9a0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
1d9b0 20 70 61 73 73 65 64 20 61 73 20 0a 2a 2a 20 20   passed as .**  
1d9c0 20 20 20 20 20 20 20 20 74 68 65 20 61 6e 20 61          the an a
1d9d0 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c 69  rgument to [sqli
1d9e0 74 65 33 5f 73 71 6c 28 29 5d 20 77 61 73 20 63  te3_sql()] was c
1d9f0 6f 6d 70 69 6c 65 64 0a 2a 2a 20 20 20 20 20 20  ompiled.**      
1da00 20 20 20 20 63 6f 6d 70 69 6c 65 64 20 75 73 69      compiled usi
1da10 6e 67 20 65 69 74 68 65 72 20 5b 73 71 6c 69 74  ng either [sqlit
1da20 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
1da30 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   or.**          
1da40 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
1da50 31 36 5f 76 32 28 29 5d 2c 0a 2a 2a 20 20 20 20  16_v2()],.**    
1da60 20 20 20 20 20 20 74 68 65 6e 20 5b 73 71 6c 69        then [sqli
1da70 74 65 33 5f 73 71 6c 28 29 5d 20 66 75 6e 63 74  te3_sql()] funct
1da80 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 70 6f  ion returns a po
1da90 69 6e 74 65 72 20 74 6f 20 61 0a 2a 2a 20 20 20  inter to a.**   
1daa0 20 20 20 20 20 20 20 7a 65 72 6f 2d 74 65 72 6d         zero-term
1dab0 69 6e 61 74 65 64 20 73 74 72 69 6e 67 20 63 6f  inated string co
1dac0 6e 74 61 69 6e 69 6e 67 20 61 20 55 54 46 2d 38  ntaining a UTF-8
1dad0 20 72 65 6e 64 65 72 69 6e 67 0a 2a 2a 20 20 20   rendering.**   
1dae0 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 6f 72         of the or
1daf0 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 74 65  iginal SQL state
1db00 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 33  ment..**.** {F13
1db10 31 30 32 7d 20 49 66 20 74 68 65 20 5b 70 72 65  102} If the [pre
1db20 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
1db30 20 70 61 73 73 65 64 20 61 73 20 0a 2a 2a 20 20   passed as .**  
1db40 20 20 20 20 20 20 20 20 74 68 65 20 61 6e 20 61          the an a
1db50 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c 69  rgument to [sqli
1db60 74 65 33 5f 73 71 6c 28 29 5d 20 77 61 73 20 63  te3_sql()] was c
1db70 6f 6d 70 69 6c 65 64 0a 2a 2a 20 20 20 20 20 20  ompiled.**      
1db80 20 20 20 20 63 6f 6d 70 69 6c 65 64 20 75 73 69      compiled usi
1db90 6e 67 20 65 69 74 68 65 72 20 5b 73 71 6c 69 74  ng either [sqlit
1dba0 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f 72  e3_prepare()] or
1dbb0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71  .**          [sq
1dbc0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28  lite3_prepare16(
1dbd0 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  )],.**          
1dbe0 74 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 73 71  then [sqlite3_sq
1dbf0 6c 28 29 5d 20 66 75 6e 63 74 69 6f 6e 20 72 65  l()] function re
1dc00 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69  turns a NULL poi
1dc10 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 33  nter..**.** {F13
1dc20 31 30 33 7d 20 54 68 65 20 73 74 72 69 6e 67 20  103} The string 
1dc30 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c  returned by [sql
1dc40 69 74 65 33 5f 73 71 6c 28 53 29 5d 20 69 73 20  ite3_sql(S)] is 
1dc50 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 0a  valid until the.
1dc60 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 65  **          [pre
1dc70 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
1dc80 20 53 20 69 73 20 64 65 6c 65 74 65 64 20 75 73   S is deleted us
1dc90 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e  ing [sqlite3_fin
1dca0 61 6c 69 7a 65 28 53 29 5d 2e 0a 2a 2f 0a 53 51  alize(S)]..*/.SQ
1dcb0 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63  LITE_API const c
1dcc0 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 71 6c  har *sqlite3_sql
1dcd0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1dce0 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  Stmt);../*.** CA
1dcf0 50 49 33 52 45 46 3a 20 20 44 79 6e 61 6d 69 63  PI3REF:  Dynamic
1dd00 61 6c 6c 79 20 54 79 70 65 64 20 56 61 6c 75 65  ally Typed Value
1dd10 20 4f 62 6a 65 63 74 20 20 7b 46 31 35 30 30 30   Object  {F15000
1dd20 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b  }.** KEYWORDS: {
1dd30 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
1dd40 33 5f 76 61 6c 75 65 7d 20 7b 75 6e 70 72 6f 74  3_value} {unprot
1dd50 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61  ected sqlite3_va
1dd60 6c 75 65 7d 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  lue}.**.** SQLit
1dd70 65 20 75 73 65 73 20 74 68 65 20 73 71 6c 69 74  e uses the sqlit
1dd80 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20  e3_value object 
1dd90 74 6f 20 72 65 70 72 65 73 65 6e 74 20 61 6c 6c  to represent all
1dda0 20 76 61 6c 75 65 73 0a 2a 2a 20 74 68 61 74 20   values.** that 
1ddb0 63 61 6e 20 62 65 20 73 74 6f 72 65 64 20 69 6e  can be stored in
1ddc0 20 61 20 64 61 74 61 62 61 73 65 20 74 61 62 6c   a database tabl
1ddd0 65 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 65  e..** SQLite use
1dde0 73 20 64 79 6e 61 6d 69 63 20 74 79 70 69 6e 67  s dynamic typing
1ddf0 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65 73 20   for the values 
1de00 69 74 20 73 74 6f 72 65 73 2e 20 20 0a 2a 2a 20  it stores.  .** 
1de10 56 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e  Values stored in
1de20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
1de30 62 6a 65 63 74 73 20 63 61 6e 20 62 65 0a 2a 2a  bjects can be.**
1de40 20 62 65 20 69 6e 74 65 67 65 72 73 2c 20 66 6c   be integers, fl
1de50 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c  oating point val
1de60 75 65 73 2c 20 73 74 72 69 6e 67 73 2c 20 42 4c  ues, strings, BL
1de70 4f 42 73 2c 20 6f 72 20 4e 55 4c 4c 2e 0a 2a 2a  OBs, or NULL..**
1de80 0a 2a 2a 20 41 6e 20 73 71 6c 69 74 65 33 5f 76  .** An sqlite3_v
1de90 61 6c 75 65 20 6f 62 6a 65 63 74 20 6d 61 79 20  alue object may 
1dea0 62 65 20 65 69 74 68 65 72 20 22 70 72 6f 74 65  be either "prote
1deb0 63 74 65 64 22 20 6f 72 20 22 75 6e 70 72 6f 74  cted" or "unprot
1dec0 65 63 74 65 64 22 2e 0a 2a 2a 20 53 6f 6d 65 20  ected"..** Some 
1ded0 69 6e 74 65 72 66 61 63 65 73 20 72 65 71 75 69  interfaces requi
1dee0 72 65 20 61 20 70 72 6f 74 65 63 74 65 64 20 73  re a protected s
1def0 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 20 20 4f  qlite3_value.  O
1df00 74 68 65 72 20 69 6e 74 65 72 66 61 63 65 73 0a  ther interfaces.
1df10 2a 2a 20 77 69 6c 6c 20 61 63 63 65 70 74 20 65  ** will accept e
1df20 69 74 68 65 72 20 61 20 70 72 6f 74 65 63 74 65  ither a protecte
1df30 64 20 6f 72 20 61 6e 20 75 6e 70 72 6f 74 65 63  d or an unprotec
1df40 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ted sqlite3_valu
1df50 65 2e 0a 2a 2a 20 45 76 65 72 79 20 69 6e 74 65  e..** Every inte
1df60 72 66 61 63 65 20 74 68 61 74 20 61 63 63 65 70  rface that accep
1df70 74 73 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ts sqlite3_value
1df80 20 61 72 67 75 6d 65 6e 74 73 20 73 70 65 63 69   arguments speci
1df90 66 69 65 73 20 0a 2a 2a 20 77 68 65 74 68 65 72  fies .** whether
1dfa0 20 6f 72 20 6e 6f 74 20 69 74 20 72 65 71 75 69   or not it requi
1dfb0 72 65 73 20 61 20 70 72 6f 74 65 63 74 65 64 20  res a protected 
1dfc0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 0a 2a  sqlite3_value..*
1dfd0 2a 0a 2a 2a 20 54 68 65 20 74 65 72 6d 73 20 22  *.** The terms "
1dfe0 70 72 6f 74 65 63 74 65 64 22 20 61 6e 64 20 22  protected" and "
1dff0 75 6e 70 72 6f 74 65 63 74 65 64 22 20 72 65 66  unprotected" ref
1e000 65 72 20 74 6f 20 77 68 65 74 68 65 72 20 6f 72  er to whether or
1e010 20 6e 6f 74 0a 2a 2a 20 61 20 6d 75 74 65 78 20   not.** a mutex 
1e020 69 73 20 68 65 6c 64 2e 20 20 41 20 69 6e 74 65  is held.  A inte
1e030 72 6e 61 6c 20 6d 75 74 65 78 20 69 73 20 68 65  rnal mutex is he
1e040 6c 64 20 66 6f 72 20 61 20 70 72 6f 74 65 63 74  ld for a protect
1e050 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61  ed.** sqlite3_va
1e060 6c 75 65 20 6f 62 6a 65 63 74 20 62 75 74 20 6e  lue object but n
1e070 6f 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20  o mutex is held 
1e080 66 6f 72 20 61 6e 20 75 6e 70 72 6f 74 65 63 74  for an unprotect
1e090 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61  ed.** sqlite3_va
1e0a0 6c 75 65 20 6f 62 6a 65 63 74 2e 20 20 49 66 20  lue object.  If 
1e0b0 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c  SQLite is compil
1e0c0 65 64 20 74 6f 20 62 65 20 73 69 6e 67 6c 65 2d  ed to be single-
1e0d0 74 68 72 65 61 64 65 64 0a 2a 2a 20 28 77 69 74  threaded.** (wit
1e0e0 68 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  h SQLITE_THREADS
1e0f0 41 46 45 3d 30 20 61 6e 64 20 77 69 74 68 20 5b  AFE=0 and with [
1e100 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73 61  sqlite3_threadsa
1e110 66 65 28 29 5d 20 72 65 74 75 72 6e 69 6e 67 20  fe()] returning 
1e120 30 29 0a 2a 2a 20 74 68 65 6e 20 74 68 65 72 65  0).** then there
1e130 20 69 73 20 6e 6f 20 64 69 73 74 69 6e 63 74 69   is no distincti
1e140 6f 6e 20 62 65 74 77 65 65 6e 0a 2a 2a 20 70 72  on between.** pr
1e150 6f 74 65 63 74 65 64 20 61 6e 64 20 75 6e 70 72  otected and unpr
1e160 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
1e170 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 61 6e  value objects an
1e180 64 20 74 68 65 79 20 63 61 6e 20 62 65 0a 2a 2a  d they can be.**
1e190 20 75 73 65 64 20 69 6e 74 65 72 63 68 61 6e 67   used interchang
1e1a0 61 62 6c 65 2e 20 20 48 6f 77 65 76 65 72 2c 20  able.  However, 
1e1b0 66 6f 72 20 6d 61 78 69 6d 75 6d 20 63 6f 64 65  for maximum code
1e1c0 20 70 6f 72 74 61 62 69 6c 69 74 79 20 69 74 0a   portability it.
1e1d0 2a 2a 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65  ** is recommende
1e1e0 64 20 74 68 61 74 20 61 70 70 6c 69 63 61 74 69  d that applicati
1e1f0 6f 6e 73 20 6d 61 6b 65 20 74 68 65 20 64 69 73  ons make the dis
1e200 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65 65 6e  tinction between
1e210 0a 2a 2a 20 62 65 74 77 65 65 6e 20 70 72 6f 74  .** between prot
1e220 65 63 74 65 64 20 61 6e 64 20 75 6e 70 72 6f 74  ected and unprot
1e230 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61  ected sqlite3_va
1e240 6c 75 65 20 6f 62 6a 65 63 74 73 20 65 76 65 6e  lue objects even
1e250 20 69 66 0a 2a 2a 20 74 68 65 79 20 61 72 65 20   if.** they are 
1e260 73 69 6e 67 6c 65 20 74 68 72 65 61 64 65 64 2e  single threaded.
1e270 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
1e280 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73  e3_value objects
1e290 20 74 68 61 74 20 61 72 65 20 70 61 73 73 65 64   that are passed
1e2a0 20 61 73 20 70 61 72 61 6d 65 74 65 72 73 20 69   as parameters i
1e2b0 6e 74 6f 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65  nto the.** imple
1e2c0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 70 70  mentation of app
1e2d0 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
1e2e0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61   SQL functions a
1e2f0 72 65 20 70 72 6f 74 65 63 74 65 64 2e 0a 2a 2a  re protected..**
1e300 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c   The sqlite3_val
1e310 75 65 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e  ue object return
1e320 65 64 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65  ed by.** [sqlite
1e330 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29  3_column_value()
1e340 5d 20 69 73 20 75 6e 70 72 6f 74 65 63 74 65 64  ] is unprotected
1e350 2e 0a 2a 2a 20 55 6e 70 72 6f 74 65 63 74 65 64  ..** Unprotected
1e360 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
1e370 62 6a 65 63 74 73 20 6d 61 79 20 6f 6e 6c 79 20  bjects may only 
1e380 62 65 20 75 73 65 64 20 77 69 74 68 0a 2a 2a 20  be used with.** 
1e390 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  [sqlite3_result_
1e3a0 76 61 6c 75 65 28 29 5d 20 61 6e 64 20 5b 73 71  value()] and [sq
1e3b0 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65  lite3_bind_value
1e3c0 28 29 5d 2e 20 20 41 6c 6c 20 6f 74 68 65 72 0a  ()].  All other.
1e3d0 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 74 68  ** interfaces th
1e3e0 61 74 20 75 73 65 20 73 71 6c 69 74 65 33 5f 76  at use sqlite3_v
1e3f0 61 6c 75 65 20 72 65 71 75 69 72 65 20 70 72 6f  alue require pro
1e400 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76  tected sqlite3_v
1e410 61 6c 75 65 20 6f 62 6a 65 63 74 73 2e 0a 2a 2f  alue objects..*/
1e420 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
1e430 4d 65 6d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  Mem sqlite3_valu
1e440 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  e;../*.** CAPI3R
1e450 45 46 3a 20 20 53 51 4c 20 46 75 6e 63 74 69 6f  EF:  SQL Functio
1e460 6e 20 43 6f 6e 74 65 78 74 20 4f 62 6a 65 63 74  n Context Object
1e470 20 7b 46 31 36 30 30 31 7d 0a 2a 2a 0a 2a 2a 20   {F16001}.**.** 
1e480 54 68 65 20 63 6f 6e 74 65 78 74 20 69 6e 20 77  The context in w
1e490 68 69 63 68 20 61 6e 20 53 51 4c 20 66 75 6e 63  hich an SQL func
1e4a0 74 69 6f 6e 20 65 78 65 63 75 74 65 73 20 69 73  tion executes is
1e4b0 20 73 74 6f 72 65 64 20 69 6e 20 61 6e 0a 2a 2a   stored in an.**
1e4c0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1e4d0 20 6f 62 6a 65 63 74 2e 20 20 41 20 70 6f 69 6e   object.  A poin
1e4e0 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65  ter to an sqlite
1e4f0 33 5f 63 6f 6e 74 65 78 74 0a 2a 2a 20 6f 62 6a  3_context.** obj
1e500 65 63 74 20 69 73 20 61 6c 77 61 79 73 20 66 69  ect is always fi
1e510 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f  rst parameter to
1e520 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66   application-def
1e530 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f  ined SQL functio
1e540 6e 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ns..*/.typedef s
1e550 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 63 6f  truct sqlite3_co
1e560 6e 74 65 78 74 20 73 71 6c 69 74 65 33 5f 63 6f  ntext sqlite3_co
1e570 6e 74 65 78 74 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  ntext;../*.** CA
1e580 50 49 33 52 45 46 3a 20 20 42 69 6e 64 69 6e 67  PI3REF:  Binding
1e590 20 56 61 6c 75 65 73 20 54 6f 20 50 72 65 70 61   Values To Prepa
1e5a0 72 65 64 20 53 74 61 74 65 6d 65 6e 74 73 20 7b  red Statements {
1e5b0 46 31 33 35 30 30 7d 0a 2a 2a 0a 2a 2a 20 49 6e  F13500}.**.** In
1e5c0 20 74 68 65 20 53 51 4c 20 73 74 72 69 6e 67 73   the SQL strings
1e5d0 20 69 6e 70 75 74 20 74 6f 20 5b 73 71 6c 69 74   input to [sqlit
1e5e0 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
1e5f0 20 61 6e 64 20 69 74 73 0a 2a 2a 20 76 61 72 69   and its.** vari
1e600 61 6e 74 73 2c 20 6c 69 74 65 72 61 6c 73 20 6d  ants, literals m
1e610 61 79 20 62 65 20 72 65 70 6c 61 63 65 20 62 79  ay be replace by
1e620 20 61 20 70 61 72 61 6d 65 74 65 72 20 69 6e 20   a parameter in 
1e630 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20  one.** of these 
1e640 66 6f 72 6d 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c  forms:.**.** <ul
1e650 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 0a 2a 2a 20  >.** <li>  ?.** 
1e660 3c 6c 69 3e 20 20 3f 4e 4e 4e 0a 2a 2a 20 3c 6c  <li>  ?NNN.** <l
1e670 69 3e 20 20 3a 56 56 56 0a 2a 2a 20 3c 6c 69 3e  i>  :VVV.** <li>
1e680 20 20 40 56 56 56 0a 2a 2a 20 3c 6c 69 3e 20 20    @VVV.** <li>  
1e690 24 56 56 56 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a  $VVV.** </ul>.**
1e6a0 0a 2a 2a 20 49 6e 20 74 68 65 20 70 61 72 61 6d  .** In the param
1e6b0 65 74 65 72 20 66 6f 72 6d 73 20 73 68 6f 77 6e  eter forms shown
1e6c0 20 61 62 6f 76 65 20 4e 4e 4e 20 69 73 20 61 6e   above NNN is an
1e6d0 20 69 6e 74 65 67 65 72 20 6c 69 74 65 72 61 6c   integer literal
1e6e0 2c 0a 2a 2a 20 56 56 56 20 61 6c 70 68 61 2d 6e  ,.** VVV alpha-n
1e6f0 75 6d 65 72 69 63 20 70 61 72 61 6d 65 74 65 72  umeric parameter
1e700 20 6e 61 6d 65 2e 0a 2a 2a 20 54 68 65 20 76 61   name..** The va
1e710 6c 75 65 73 20 6f 66 20 74 68 65 73 65 20 70 61  lues of these pa
1e720 72 61 6d 65 74 65 72 73 20 28 61 6c 73 6f 20 63  rameters (also c
1e730 61 6c 6c 65 64 20 22 68 6f 73 74 20 70 61 72 61  alled "host para
1e740 6d 65 74 65 72 20 6e 61 6d 65 73 22 0a 2a 2a 20  meter names".** 
1e750 6f 72 20 22 53 51 4c 20 70 61 72 61 6d 65 74 65  or "SQL paramete
1e760 72 73 22 29 0a 2a 2a 20 63 61 6e 20 62 65 20 73  rs").** can be s
1e770 65 74 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c  et using the sql
1e780 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f  ite3_bind_*() ro
1e790 75 74 69 6e 65 73 20 64 65 66 69 6e 65 64 20 68  utines defined h
1e7a0 65 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ere..**.** The f
1e7b0 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
1e7c0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e   the sqlite3_bin
1e7d0 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 61  d_*() routines a
1e7e0 6c 77 61 79 73 0a 2a 2a 20 69 73 20 61 20 70 6f  lways.** is a po
1e7f0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b 73 71  inter to the [sq
1e800 6c 69 74 65 33 5f 73 74 6d 74 5d 20 6f 62 6a 65  lite3_stmt] obje
1e810 63 74 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d  ct returned from
1e820 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  .** [sqlite3_pre
1e830 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 69 74  pare_v2()] or it
1e840 73 20 76 61 72 69 61 6e 74 73 2e 20 54 68 65 20  s variants. The 
1e850 73 65 63 6f 6e 64 0a 2a 2a 20 61 72 67 75 6d 65  second.** argume
1e860 6e 74 20 69 73 20 74 68 65 20 69 6e 64 65 78 20  nt is the index 
1e870 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  of the parameter
1e880 20 74 6f 20 62 65 20 73 65 74 2e 20 54 68 65 0a   to be set. The.
1e890 2a 2a 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  ** first paramet
1e8a0 65 72 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20  er has an index 
1e8b0 6f 66 20 31 2e 20 20 57 68 65 6e 20 74 68 65 20  of 1.  When the 
1e8c0 73 61 6d 65 20 6e 61 6d 65 64 0a 2a 2a 20 70 61  same named.** pa
1e8d0 72 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 20  rameter is used 
1e8e0 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20  more than once, 
1e8f0 73 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65  second and subse
1e900 71 75 65 6e 74 0a 2a 2a 20 6f 63 63 75 72 72 65  quent.** occurre
1e910 6e 63 65 73 20 68 61 76 65 20 74 68 65 20 73 61  nces have the sa
1e920 6d 65 20 69 6e 64 65 78 20 61 73 20 74 68 65 20  me index as the 
1e930 66 69 72 73 74 20 6f 63 63 75 72 72 65 6e 63 65  first occurrence
1e940 2e 20 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20  . .** The index 
1e950 66 6f 72 20 6e 61 6d 65 64 20 70 61 72 61 6d 65  for named parame
1e960 74 65 72 73 20 63 61 6e 20 62 65 20 6c 6f 6f 6b  ters can be look
1e970 65 64 20 75 70 20 75 73 69 6e 67 20 74 68 65 0a  ed up using the.
1e980 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  ** [sqlite3_bind
1e990 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28  _parameter_name(
1e9a0 29 5d 20 41 50 49 20 69 66 20 64 65 73 69 72 65  )] API if desire
1e9b0 64 2e 20 20 54 68 65 20 69 6e 64 65 78 0a 2a 2a  d.  The index.**
1e9c0 20 66 6f 72 20 22 3f 4e 4e 4e 22 20 70 61 72 61   for "?NNN" para
1e9d0 6d 65 74 65 72 73 20 69 73 20 74 68 65 20 76 61  meters is the va
1e9e0 6c 75 65 20 6f 66 20 4e 4e 4e 2e 0a 2a 2a 20 54  lue of NNN..** T
1e9f0 68 65 20 4e 4e 4e 20 76 61 6c 75 65 20 6d 75 73  he NNN value mus
1ea00 74 20 62 65 20 62 65 74 77 65 65 6e 20 31 20 61  t be between 1 a
1ea10 6e 64 20 74 68 65 20 63 6f 6d 70 69 6c 65 2d 74  nd the compile-t
1ea20 69 6d 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  ime.** parameter
1ea30 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49   SQLITE_MAX_VARI
1ea40 41 42 4c 45 5f 4e 55 4d 42 45 52 20 28 64 65 66  ABLE_NUMBER (def
1ea50 61 75 6c 74 20 76 61 6c 75 65 3a 20 39 39 39 29  ault value: 999)
1ea60 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72  ..**.** The thir
1ea70 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  d argument is th
1ea80 65 20 76 61 6c 75 65 20 74 6f 20 62 69 6e 64 20  e value to bind 
1ea90 74 6f 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  to the parameter
1eaa0 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 6f 73 65  ..**.** In those
1eab0 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 74 68 61  .** routines tha
1eac0 74 20 68 61 76 65 20 61 20 66 6f 75 72 74 68 20  t have a fourth 
1ead0 61 72 67 75 6d 65 6e 74 2c 20 69 74 73 20 76 61  argument, its va
1eae0 6c 75 65 20 69 73 20 74 68 65 20 6e 75 6d 62 65  lue is the numbe
1eaf0 72 20 6f 66 20 62 79 74 65 73 0a 2a 2a 20 69 6e  r of bytes.** in
1eb00 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 2e 20   the parameter. 
1eb10 20 54 6f 20 62 65 20 63 6c 65 61 72 3a 20 74 68   To be clear: th
1eb20 65 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 6e  e value is the n
1eb30 75 6d 62 65 72 20 6f 66 20 3c 75 3e 62 79 74 65  umber of <u>byte
1eb40 73 3c 2f 75 3e 0a 2a 2a 20 69 6e 20 74 68 65 20  s</u>.** in the 
1eb50 76 61 6c 75 65 2c 20 6e 6f 74 20 74 68 65 20 6e  value, not the n
1eb60 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
1eb70 65 72 73 2e 20 0a 2a 2a 20 49 66 20 74 68 65 20  ers. .** If the 
1eb80 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72  fourth parameter
1eb90 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68   is negative, th
1eba0 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  e length of the 
1ebb0 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 6e 75 6d  string is.** num
1ebc0 62 65 72 20 6f 66 20 62 79 74 65 73 20 75 70 20  ber of bytes up 
1ebd0 74 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65 72  to the first zer
1ebe0 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a  o terminator..**
1ebf0 0a 2a 2a 20 54 68 65 20 66 69 66 74 68 20 61 72  .** The fifth ar
1ec00 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65  gument to sqlite
1ec10 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 29 2c 20 73  3_bind_blob(), s
1ec20 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
1ec30 28 29 2c 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74  (), and.** sqlit
1ec40 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 28 29  e3_bind_text16()
1ec50 20 69 73 20 61 20 64 65 73 74 72 75 63 74 6f 72   is a destructor
1ec60 20 75 73 65 64 20 74 6f 20 64 69 73 70 6f 73 65   used to dispose
1ec70 20 6f 66 20 74 68 65 20 42 4c 4f 42 20 6f 72 0a   of the BLOB or.
1ec80 2a 2a 20 73 74 72 69 6e 67 20 61 66 74 65 72 20  ** string after 
1ec90 53 51 4c 69 74 65 20 68 61 73 20 66 69 6e 69 73  SQLite has finis
1eca0 68 65 64 20 77 69 74 68 20 69 74 2e 20 49 66 20  hed with it. If 
1ecb0 74 68 65 20 66 69 66 74 68 20 61 72 67 75 6d 65  the fifth argume
1ecc0 6e 74 20 69 73 0a 2a 2a 20 74 68 65 20 73 70 65  nt is.** the spe
1ecd0 63 69 61 6c 20 76 61 6c 75 65 20 5b 53 51 4c 49  cial value [SQLI
1ece0 54 45 5f 53 54 41 54 49 43 5d 2c 20 74 68 65 6e  TE_STATIC], then
1ecf0 20 53 51 4c 69 74 65 20 61 73 73 75 6d 65 73 20   SQLite assumes 
1ed00 74 68 61 74 20 74 68 65 0a 2a 2a 20 69 6e 66 6f  that the.** info
1ed10 72 6d 61 74 69 6f 6e 20 69 73 20 69 6e 20 73 74  rmation is in st
1ed20 61 74 69 63 2c 20 75 6e 6d 61 6e 61 67 65 64 20  atic, unmanaged 
1ed30 73 70 61 63 65 20 61 6e 64 20 64 6f 65 73 20 6e  space and does n
1ed40 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 66 72  ot need to be fr
1ed50 65 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66  eed..** If the f
1ed60 69 66 74 68 20 61 72 67 75 6d 65 6e 74 20 68 61  ifth argument ha
1ed70 73 20 74 68 65 20 76 61 6c 75 65 20 5b 53 51 4c  s the value [SQL
1ed80 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 5d 2c 20  ITE_TRANSIENT], 
1ed90 74 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 6d  then.** SQLite m
1eda0 61 6b 65 73 20 69 74 73 20 6f 77 6e 20 70 72 69  akes its own pri
1edb0 76 61 74 65 20 63 6f 70 79 20 6f 66 20 74 68 65  vate copy of the
1edc0 20 64 61 74 61 20 69 6d 6d 65 64 69 61 74 65 6c   data immediatel
1edd0 79 2c 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65  y, before.** the
1ede0 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28   sqlite3_bind_*(
1edf0 29 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  ) routine return
1ee00 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  s..**.** The sql
1ee10 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c  ite3_bind_zerobl
1ee20 6f 62 28 29 20 72 6f 75 74 69 6e 65 20 62 69 6e  ob() routine bin
1ee30 64 73 20 61 20 42 4c 4f 42 20 6f 66 20 6c 65 6e  ds a BLOB of len
1ee40 67 74 68 20 4e 20 74 68 61 74 0a 2a 2a 20 69 73  gth N that.** is
1ee50 20 66 69 6c 6c 65 64 20 77 69 74 68 20 7a 65 72   filled with zer
1ee60 6f 73 2e 20 20 41 20 7a 65 72 6f 62 6c 6f 62 20  os.  A zeroblob 
1ee70 75 73 65 73 20 61 20 66 69 78 65 64 20 61 6d 6f  uses a fixed amo
1ee80 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a  unt of memory.**
1ee90 20 28 6a 75 73 74 20 61 6e 20 69 6e 74 65 67 65   (just an intege
1eea0 72 20 74 6f 20 68 6f 6c 64 20 69 74 20 73 69 7a  r to hold it siz
1eeb0 65 29 20 77 68 69 6c 65 20 69 74 20 69 73 20 62  e) while it is b
1eec0 65 69 6e 67 20 70 72 6f 63 65 73 73 65 64 2e 0a  eing processed..
1eed0 2a 2a 20 5a 65 72 6f 62 6c 6f 62 73 20 61 72 65  ** Zeroblobs are
1eee0 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 73 65 72   intended to ser
1eef0 76 65 20 61 73 20 70 6c 61 63 65 2d 68 6f 6c 64  ve as place-hold
1ef00 65 72 73 20 66 6f 72 20 42 4c 4f 42 73 20 77 68  ers for BLOBs wh
1ef10 6f 73 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 69  ose.** content i
1ef20 73 20 6c 61 74 65 72 20 77 72 69 74 74 65 6e 20  s later written 
1ef30 75 73 69 6e 67 20 0a 2a 2a 20 5b 73 71 6c 69 74  using .** [sqlit
1ef40 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20 7c 20 69  e3_blob_open | i
1ef50 6e 63 72 65 6d 65 6e 74 20 42 4c 4f 42 20 49 2f  ncrement BLOB I/
1ef60 4f 5d 20 72 6f 75 74 69 6e 65 73 2e 20 41 20 6e  O] routines. A n
1ef70 65 67 61 74 69 76 65 0a 2a 2a 20 76 61 6c 75 65  egative.** value
1ef80 20 66 6f 72 20 74 68 65 20 7a 65 72 6f 62 6c 6f   for the zeroblo
1ef90 62 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 7a  b results in a z
1efa0 65 72 6f 2d 6c 65 6e 67 74 68 20 42 4c 4f 42 2e  ero-length BLOB.
1efb0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
1efc0 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74  e3_bind_*() rout
1efd0 69 6e 65 73 20 6d 75 73 74 20 62 65 20 63 61 6c  ines must be cal
1efe0 6c 65 64 20 61 66 74 65 72 0a 2a 2a 20 5b 73 71  led after.** [sq
1eff0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
1f000 28 29 5d 20 28 61 6e 64 20 69 74 73 20 76 61 72  ()] (and its var
1f010 69 61 6e 74 73 29 20 6f 72 20 5b 73 71 6c 69 74  iants) or [sqlit
1f020 65 33 5f 72 65 73 65 74 28 29 5d 20 61 6e 64 0a  e3_reset()] and.
1f030 2a 2a 20 62 65 66 6f 72 65 20 5b 73 71 6c 69 74  ** before [sqlit
1f040 65 33 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 20 42  e3_step()]..** B
1f050 69 6e 64 69 6e 67 73 20 61 72 65 20 6e 6f 74 20  indings are not 
1f060 63 6c 65 61 72 65 64 20 62 79 20 74 68 65 20 5b  cleared by the [
1f070 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d  sqlite3_reset()]
1f080 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 20 55 6e 62   routine..** Unb
1f090 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72 73 20  ound parameters 
1f0a0 61 72 65 20 69 6e 74 65 72 70 72 65 74 65 64 20  are interpreted 
1f0b0 61 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54  as NULL..**.** T
1f0c0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65  hese routines re
1f0d0 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d  turn [SQLITE_OK]
1f0e0 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61   on success or a
1f0f0 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 0a  n error code if.
1f100 2a 2a 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73  ** anything goes
1f110 20 77 72 6f 6e 67 2e 20 20 5b 53 51 4c 49 54 45   wrong.  [SQLITE
1f120 5f 52 41 4e 47 45 5d 20 69 73 20 72 65 74 75 72  _RANGE] is retur
1f130 6e 65 64 20 69 66 20 74 68 65 20 70 61 72 61 6d  ned if the param
1f140 65 74 65 72 0a 2a 2a 20 69 6e 64 65 78 20 69 73  eter.** index is
1f150 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 20   out of range.  
1f160 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 20 69  [SQLITE_NOMEM] i
1f170 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 6d 61  s returned if ma
1f180 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2a 20 5b  lloc fails..** [
1f190 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 20 6d  SQLITE_MISUSE] m
1f1a0 69 67 68 74 20 62 65 20 72 65 74 75 72 6e 65 64  ight be returned
1f1b0 20 69 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e   if these routin
1f1c0 65 73 20 61 72 65 20 63 61 6c 6c 65 64 20 6f 6e  es are called on
1f1d0 20 61 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61   a.** virtual ma
1f1e0 63 68 69 6e 65 20 74 68 61 74 20 69 73 20 74 68  chine that is th
1f1f0 65 20 77 72 6f 6e 67 20 73 74 61 74 65 20 6f 72  e wrong state or
1f200 20 77 68 69 63 68 20 68 61 73 20 61 6c 72 65 61   which has alrea
1f210 64 79 20 62 65 65 6e 20 66 69 6e 61 6c 69 7a 65  dy been finalize
1f220 64 2e 0a 2a 2a 20 44 65 74 65 63 74 69 6f 6e 20  d..** Detection 
1f230 6f 66 20 6d 69 73 75 73 65 20 69 73 20 75 6e 72  of misuse is unr
1f240 65 6c 69 61 62 6c 65 2e 20 20 41 70 70 6c 69 63  eliable.  Applic
1f250 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f  ations should no
1f260 74 20 64 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 53  t depend.** on S
1f270 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 72 65 74  QLITE_MISUSE ret
1f280 75 72 6e 73 2e 20 20 53 51 4c 49 54 45 5f 4d 49  urns.  SQLITE_MI
1f290 53 55 53 45 20 69 73 20 69 6e 74 65 6e 64 65 64  SUSE is intended
1f2a0 20 74 6f 20 69 6e 64 69 63 61 74 65 20 61 0a 2a   to indicate a.*
1f2b0 2a 20 61 20 6c 6f 67 69 63 20 65 72 72 6f 72 20  * a logic error 
1f2c0 69 6e 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  in the applicati
1f2d0 6f 6e 2e 20 20 46 75 74 75 72 65 20 76 65 72 73  on.  Future vers
1f2e0 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 6d  ions of SQLite m
1f2f0 69 67 68 74 0a 2a 2a 20 70 61 6e 69 63 20 72 61  ight.** panic ra
1f300 74 68 65 72 20 74 68 61 6e 20 72 65 74 75 72 6e  ther than return
1f310 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2e 0a   SQLITE_MISUSE..
1f320 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
1f330 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61  [sqlite3_bind_pa
1f340 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d  rameter_count()]
1f350 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69  ,.** [sqlite3_bi
1f360 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d  nd_parameter_nam
1f370 65 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71  e()], and.** [sq
1f380 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
1f390 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a  eter_index()]..*
1f3a0 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
1f3b0 0a 2a 2a 0a 2a 2a 20 7b 46 31 33 35 30 36 7d 20  .**.** {F13506} 
1f3c0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  The [sqlite3_pre
1f3d0 70 61 72 65 20 7c 20 53 51 4c 20 73 74 61 74 65  pare | SQL state
1f3e0 6d 65 6e 74 20 63 6f 6d 70 69 6c 65 72 5d 20 72  ment compiler] r
1f3f0 65 63 6f 67 6e 69 7a 65 73 0a 2a 2a 20 20 20 20  ecognizes.**    
1f400 20 20 20 20 20 20 74 6f 6b 65 6e 73 20 6f 66 20        tokens of 
1f410 74 68 65 20 66 6f 72 6d 73 20 22 3f 22 2c 20 22  the forms "?", "
1f420 3f 4e 4e 4e 22 2c 20 22 24 56 56 56 22 2c 20 22  ?NNN", "$VVV", "
1f430 3a 56 56 56 22 2c 20 61 6e 64 20 22 40 56 56 56  :VVV", and "@VVV
1f440 22 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 73  ".**          as
1f450 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 2c   SQL parameters,
1f460 20 77 68 65 72 65 20 4e 4e 4e 20 69 73 20 61 6e   where NNN is an
1f470 79 20 73 65 71 75 65 6e 63 65 20 6f 66 20 6f 6e  y sequence of on
1f480 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 20 20 20  e or more.**    
1f490 20 20 20 20 20 20 64 69 67 69 74 73 20 61 6e 64        digits and
1f4a0 20 77 68 65 72 65 20 56 56 56 20 69 73 20 61 6e   where VVV is an
1f4b0 79 20 73 65 71 75 65 6e 63 65 20 6f 66 20 6f 6e  y sequence of on
1f4c0 65 20 6f 72 20 6d 6f 72 65 20 0a 2a 2a 20 20 20  e or more .**   
1f4d0 20 20 20 20 20 20 20 61 6c 70 68 61 6e 75 6d 65         alphanume
1f4e0 72 69 63 20 63 68 61 72 61 63 74 65 72 73 20 6f  ric characters o
1f4f0 72 20 22 3a 3a 22 20 6f 70 74 69 6f 6e 61 6c 6c  r "::" optionall
1f500 79 20 66 6f 6c 6c 6f 77 65 64 20 62 79 0a 2a 2a  y followed by.**
1f510 20 20 20 20 20 20 20 20 20 20 61 20 73 74 72 69            a stri
1f520 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6e 6f  ng containing no
1f530 20 73 70 61 63 65 73 20 61 6e 64 20 63 6f 6e 74   spaces and cont
1f540 61 69 6e 65 64 20 77 69 74 68 69 6e 20 70 61 72  ained within par
1f550 65 6e 74 68 65 73 65 73 2e 0a 2a 2a 0a 2a 2a 20  entheses..**.** 
1f560 7b 46 31 33 35 30 39 7d 20 54 68 65 20 69 6e 69  {F13509} The ini
1f570 74 69 61 6c 20 76 61 6c 75 65 20 6f 66 20 61 6e  tial value of an
1f580 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 69   SQL parameter i
1f590 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 7b 46  s NULL..**.** {F
1f5a0 31 33 35 31 32 7d 20 54 68 65 20 69 6e 64 65 78  13512} The index
1f5b0 20 6f 66 20 61 6e 20 22 3f 22 20 53 51 4c 20 70   of an "?" SQL p
1f5c0 61 72 61 6d 65 74 65 72 20 69 73 20 6f 6e 65 20  arameter is one 
1f5d0 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 0a  larger than the.
1f5e0 2a 2a 20 20 20 20 20 20 20 20 20 20 6c 61 72 67  **          larg
1f5f0 65 73 74 20 69 6e 64 65 78 20 6f 66 20 53 51 4c  est index of SQL
1f600 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68   parameter to th
1f610 65 20 6c 65 66 74 2c 20 6f 72 20 31 20 69 66 0a  e left, or 1 if.
1f620 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20  **          the 
1f630 22 3f 22 20 69 73 20 74 68 65 20 6c 65 66 74 6d  "?" is the leftm
1f640 6f 73 74 20 53 51 4c 20 70 61 72 61 6d 65 74 65  ost SQL paramete
1f650 72 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 33 35 31 35  r..**.** {F13515
1f660 7d 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 61  } The index of a
1f670 6e 20 22 3f 4e 4e 4e 22 20 53 51 4c 20 70 61 72  n "?NNN" SQL par
1f680 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 69 6e  ameter is the in
1f690 74 65 67 65 72 20 4e 4e 4e 2e 0a 2a 2a 0a 2a 2a  teger NNN..**.**
1f6a0 20 7b 46 31 33 35 31 38 7d 20 54 68 65 20 69 6e   {F13518} The in
1f6b0 64 65 78 20 6f 66 20 61 6e 20 22 3a 56 56 56 22  dex of an ":VVV"
1f6c0 2c 20 22 24 56 56 56 22 2c 20 6f 72 20 22 40 56  , "$VVV", or "@V
1f6d0 56 56 22 20 53 51 4c 20 70 61 72 61 6d 65 74 65  VV" SQL paramete
1f6e0 72 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  r is.**         
1f6f0 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
1f700 20 69 6e 64 65 78 20 6f 66 20 6c 65 66 74 6d 6f   index of leftmo
1f710 73 74 20 6f 63 63 75 72 61 6e 63 65 73 20 6f 66  st occurances of
1f720 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 20 20 20   the same.**    
1f730 20 20 20 20 20 20 70 61 72 61 6d 65 74 65 72 2c        parameter,
1f740 20 6f 72 20 6f 6e 65 20 6d 6f 72 65 20 74 68 61   or one more tha
1f750 6e 20 74 68 65 20 6c 61 72 67 65 73 74 20 69 6e  n the largest in
1f760 64 65 78 20 6f 76 65 72 20 61 6c 6c 0a 2a 2a 20  dex over all.** 
1f770 20 20 20 20 20 20 20 20 20 70 61 72 61 6d 65 74           paramet
1f780 65 72 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20  ers to the left 
1f790 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66  if this is the f
1f7a0 69 72 73 74 20 6f 63 63 75 72 72 61 6e 63 65 0a  irst occurrance.
1f7b0 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 66 20 74  **          of t
1f7c0 68 69 73 20 70 61 72 61 6d 65 74 65 72 2c 20 6f  his parameter, o
1f7d0 72 20 31 20 69 66 20 74 68 69 73 20 69 73 20 74  r 1 if this is t
1f7e0 68 65 20 6c 65 66 74 6d 6f 73 74 20 70 61 72 61  he leftmost para
1f7f0 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31  meter..**.** {F1
1f800 33 35 32 31 7d 20 54 68 65 20 5b 73 71 6c 69 74  3521} The [sqlit
1f810 65 33 5f 70 72 65 70 61 72 65 20 7c 20 53 51 4c  e3_prepare | SQL
1f820 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69   statement compi
1f830 6c 65 72 5d 20 66 61 69 6c 20 77 69 74 68 0a 2a  ler] fail with.*
1f840 2a 20 20 20 20 20 20 20 20 20 20 61 6e 20 5b 53  *          an [S
1f850 51 4c 49 54 45 5f 52 41 4e 47 45 5d 20 65 72 72  QLITE_RANGE] err
1f860 6f 72 20 69 66 20 74 68 65 20 69 6e 64 65 78 20  or if the index 
1f870 6f 66 20 61 6e 20 53 51 4c 20 70 61 72 61 6d 65  of an SQL parame
1f880 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ter.**          
1f890 69 73 20 6c 65 73 73 20 74 68 61 6e 20 31 20 6f  is less than 1 o
1f8a0 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 53  r greater than S
1f8b0 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41 42  QLITE_MAX_VARIAB
1f8c0 4c 45 5f 4e 55 4d 42 45 52 2e 0a 2a 2a 0a 2a 2a  LE_NUMBER..**.**
1f8d0 20 7b 46 31 33 35 32 34 7d 20 43 61 6c 6c 73 20   {F13524} Calls 
1f8e0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  to [sqlite3_bind
1f8f0 5f 74 65 78 74 20 7c 20 73 71 6c 69 74 65 33 5f  _text | sqlite3_
1f900 62 69 6e 64 28 53 2c 4e 2c 56 2c 2e 2e 2e 29 5d  bind(S,N,V,...)]
1f910 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 73 73  .**          ass
1f920 6f 63 69 61 74 65 20 74 68 65 20 76 61 6c 75 65  ociate the value
1f930 20 56 20 77 69 74 68 20 61 6c 6c 20 53 51 4c 20   V with all SQL 
1f940 70 61 72 61 6d 65 74 65 72 73 20 68 61 76 69 6e  parameters havin
1f950 67 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  g an.**         
1f960 20 69 6e 64 65 78 20 6f 66 20 4e 20 69 6e 20 74   index of N in t
1f970 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  he [prepared sta
1f980 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a 2a 0a 2a 2a  tement] S..**.**
1f990 20 7b 46 31 33 35 32 37 7d 20 43 61 6c 6c 73 20   {F13527} Calls 
1f9a0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  to [sqlite3_bind
1f9b0 5f 74 65 78 74 20 7c 20 73 71 6c 69 74 65 33 5f  _text | sqlite3_
1f9c0 62 69 6e 64 28 53 2c 4e 2c 2e 2e 2e 29 5d 0a 2a  bind(S,N,...)].*
1f9d0 2a 20 20 20 20 20 20 20 20 20 20 6f 76 65 72 72  *          overr
1f9e0 69 64 65 20 70 72 69 6f 72 20 63 61 6c 6c 73 20  ide prior calls 
1f9f0 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 76 61  with the same va
1fa00 6c 75 65 73 20 6f 66 20 53 20 61 6e 64 20 4e 2e  lues of S and N.
1fa10 0a 2a 2a 0a 2a 2a 20 7b 46 31 33 35 33 30 7d 20  .**.** {F13530} 
1fa20 42 69 6e 64 69 6e 67 73 20 65 73 74 61 62 6c 69  Bindings establi
1fa30 73 68 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33  shed by [sqlite3
1fa40 5f 62 69 6e 64 5f 74 65 78 74 20 7c 20 73 71 6c  _bind_text | sql
1fa50 69 74 65 33 5f 62 69 6e 64 28 53 2c 2e 2e 2e 29  ite3_bind(S,...)
1fa60 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 65  ].**          pe
1fa70 72 73 69 73 74 20 61 63 72 6f 73 73 20 63 61 6c  rsist across cal
1fa80 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72  ls to [sqlite3_r
1fa90 65 73 65 74 28 53 29 5d 2e 0a 2a 2a 0a 2a 2a 20  eset(S)]..**.** 
1faa0 7b 46 31 33 35 33 33 7d 20 49 6e 20 63 61 6c 6c  {F13533} In call
1fab0 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69  s to [sqlite3_bi
1fac0 6e 64 5f 62 6c 6f 62 28 53 2c 4e 2c 56 2c 4c 2c  nd_blob(S,N,V,L,
1fad0 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20  D)],.**         
1fae0 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74   [sqlite3_bind_t
1faf0 65 78 74 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c  ext(S,N,V,L,D)],
1fb00 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   or.**          
1fb10 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65  [sqlite3_bind_te
1fb20 78 74 31 36 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d  xt16(S,N,V,L,D)]
1fb30 20 53 51 4c 69 74 65 20 62 69 6e 64 73 20 74 68   SQLite binds th
1fb40 65 20 66 69 72 73 74 20 4c 0a 2a 2a 20 20 20 20  e first L.**    
1fb50 20 20 20 20 20 20 62 79 74 65 73 20 6f 66 20 74        bytes of t
1fb60 68 65 20 62 6c 6f 62 20 6f 72 20 73 74 72 69 6e  he blob or strin
1fb70 67 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  g pointed to by 
1fb80 56 2c 20 77 68 65 6e 20 4c 0a 2a 2a 20 20 20 20  V, when L.**    
1fb90 20 20 20 20 20 20 69 73 20 6e 6f 6e 2d 6e 65 67        is non-neg
1fba0 61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31  ative..**.** {F1
1fbb0 33 35 33 36 7d 20 49 6e 20 63 61 6c 6c 73 20 74  3536} In calls t
1fbc0 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  o [sqlite3_bind_
1fbd0 74 65 78 74 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d  text(S,N,V,L,D)]
1fbe0 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   or.**          
1fbf0 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65  [sqlite3_bind_te
1fc00 78 74 31 36 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d  xt16(S,N,V,L,D)]
1fc10 20 53 51 4c 69 74 65 20 62 69 6e 64 73 20 63 68   SQLite binds ch
1fc20 61 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20 20  aracters.**     
1fc30 20 20 20 20 20 66 72 6f 6d 20 56 20 74 68 72 6f       from V thro
1fc40 75 67 68 20 74 68 65 20 66 69 72 73 74 20 7a 65  ugh the first ze
1fc50 72 6f 20 63 68 61 72 61 63 74 65 72 20 77 68 65  ro character whe
1fc60 6e 20 4c 20 69 73 20 6e 65 67 61 74 69 76 65 2e  n L is negative.
1fc70 0a 2a 2a 0a 2a 2a 20 7b 46 31 33 35 33 39 7d 20  .**.** {F13539} 
1fc80 49 6e 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c  In calls to [sql
1fc90 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 53  ite3_bind_blob(S
1fca0 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c 0a 2a 2a 20 20  ,N,V,L,D)],.**  
1fcb0 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33          [sqlite3
1fcc0 5f 62 69 6e 64 5f 74 65 78 74 28 53 2c 4e 2c 56  _bind_text(S,N,V
1fcd0 2c 4c 2c 44 29 5d 2c 20 6f 72 0a 2a 2a 20 20 20  ,L,D)], or.**   
1fce0 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
1fcf0 62 69 6e 64 5f 74 65 78 74 31 36 28 53 2c 4e 2c  bind_text16(S,N,
1fd00 56 2c 4c 2c 44 29 5d 20 77 68 65 6e 20 44 20 69  V,L,D)] when D i
1fd10 73 20 74 68 65 20 73 70 65 63 69 61 6c 0a 2a 2a  s the special.**
1fd20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 61            consta
1fd30 6e 74 20 5b 53 51 4c 49 54 45 5f 53 54 41 54 49  nt [SQLITE_STATI
1fd40 43 5d 2c 20 53 51 4c 69 74 65 20 61 73 73 75 6d  C], SQLite assum
1fd50 65 73 20 74 68 61 74 20 74 68 65 20 76 61 6c 75  es that the valu
1fd60 65 20 56 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  e V.**          
1fd70 69 73 20 68 65 6c 64 20 69 6e 20 73 74 61 74 69  is held in stati
1fd80 63 20 75 6e 6d 61 6e 61 67 65 64 20 73 70 61 63  c unmanaged spac
1fd90 65 20 74 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20  e that will not 
1fda0 63 68 61 6e 67 65 0a 2a 2a 20 20 20 20 20 20 20  change.**       
1fdb0 20 20 20 64 75 72 69 6e 67 20 74 68 65 20 6c 69     during the li
1fdc0 66 65 74 69 6d 65 20 6f 66 20 74 68 65 20 62 69  fetime of the bi
1fdd0 6e 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31  nding..**.** {F1
1fde0 33 35 34 32 7d 20 49 6e 20 63 61 6c 6c 73 20 74  3542} In calls t
1fdf0 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  o [sqlite3_bind_
1fe00 62 6c 6f 62 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d  blob(S,N,V,L,D)]
1fe10 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73  ,.**          [s
1fe20 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
1fe30 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c 20 6f 72  (S,N,V,L,D)], or
1fe40 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71  .**          [sq
1fe50 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31  lite3_bind_text1
1fe60 36 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 77 68  6(S,N,V,L,D)] wh
1fe70 65 6e 20 44 20 69 73 20 74 68 65 20 73 70 65 63  en D is the spec
1fe80 69 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ial.**          
1fe90 63 6f 6e 73 74 61 6e 74 20 5b 53 51 4c 49 54 45  constant [SQLITE
1fea0 5f 54 52 41 4e 53 49 45 4e 54 5d 2c 20 74 68 65  _TRANSIENT], the
1feb0 20 72 6f 75 74 69 6e 65 20 6d 61 6b 65 73 20 61   routine makes a
1fec0 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 72   .**          pr
1fed0 69 76 61 74 65 20 63 6f 70 79 20 6f 66 20 56 20  ivate copy of V 
1fee0 76 61 6c 75 65 20 62 65 66 6f 72 65 20 69 74 20  value before it 
1fef0 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 7b  returns..**.** {
1ff00 46 31 33 35 34 35 7d 20 49 6e 20 63 61 6c 6c 73  F13545} In calls
1ff10 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e   to [sqlite3_bin
1ff20 64 5f 62 6c 6f 62 28 53 2c 4e 2c 56 2c 4c 2c 44  d_blob(S,N,V,L,D
1ff30 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  )],.**          
1ff40 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65  [sqlite3_bind_te
1ff50 78 74 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c 20  xt(S,N,V,L,D)], 
1ff60 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  or.**          [
1ff70 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78  sqlite3_bind_tex
1ff80 74 31 36 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20  t16(S,N,V,L,D)] 
1ff90 77 68 65 6e 20 44 20 69 73 20 61 20 70 6f 69 6e  when D is a poin
1ffa0 74 65 72 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20  ter to.**       
1ffb0 20 20 20 61 20 66 75 6e 63 74 69 6f 6e 2c 20 53     a function, S
1ffc0 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 74 68  QLite invokes th
1ffd0 61 74 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 64  at function to d
1ffe0 65 73 74 72 6f 79 20 74 68 65 0a 2a 2a 20 20 20  estroy the.**   
1fff0 20 20 20 20 20 20 20 56 20 76 61 6c 75 65 20 61         V value a
20000 66 74 65 72 20 69 74 20 68 61 73 20 66 69 6e 69  fter it has fini
20010 73 68 65 64 20 75 73 69 6e 67 20 74 68 65 20 56  shed using the V
20020 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 7b 46   value..**.** {F
20030 31 33 35 34 38 7d 20 49 6e 20 63 61 6c 6c 73 20  13548} In calls 
20040 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  to [sqlite3_bind
20050 5f 7a 65 72 6f 62 6c 6f 62 28 53 2c 4e 2c 56 2c  _zeroblob(S,N,V,
20060 4c 29 5d 20 74 68 65 20 76 61 6c 75 65 20 62 6f  L)] the value bo
20070 75 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  und.**          
20080 69 73 20 61 20 62 6c 6f 62 20 6f 66 20 4c 20 62  is a blob of L b
20090 79 74 65 73 2c 20 6f 72 20 61 20 7a 65 72 6f 2d  ytes, or a zero-
200a0 6c 65 6e 67 74 68 20 62 6c 6f 62 20 69 66 20 4c  length blob if L
200b0 20 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a   is negative..**
200c0 0a 2a 2a 20 7b 46 31 33 35 35 31 7d 20 49 6e 20  .** {F13551} In 
200d0 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65  calls to [sqlite
200e0 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 53 2c 4e  3_bind_value(S,N
200f0 2c 56 29 5d 20 74 68 65 20 56 20 61 72 67 75 6d  ,V)] the V argum
20100 65 6e 74 20 6d 61 79 0a 2a 2a 20 20 20 20 20 20  ent may.**      
20110 20 20 20 20 62 65 20 65 69 74 68 65 72 20 61 20      be either a 
20120 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  [protected sqlit
20130 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74  e3_value] object
20140 20 6f 72 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20   or an.**       
20150 20 20 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20     [unprotected 
20160 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f  sqlite3_value] o
20170 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  bject..*/.SQLITE
20180 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
20190 5f 62 69 6e 64 5f 62 6c 6f 62 28 73 71 6c 69 74  _bind_blob(sqlit
201a0 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63  e3_stmt*, int, c
201b0 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20  onst void*, int 
201c0 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  n, void(*)(void*
201d0 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ));.SQLITE_API i
201e0 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
201f0 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73  double(sqlite3_s
20200 74 6d 74 2a 2c 20 69 6e 74 2c 20 64 6f 75 62 6c  tmt*, int, doubl
20210 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  e);.SQLITE_API i
20220 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
20230 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  int(sqlite3_stmt
20240 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  *, int, int);.SQ
20250 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
20260 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
20270 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
20280 6e 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  nt, sqlite3_int6
20290 34 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  4);.SQLITE_API i
202a0 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
202b0 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d  null(sqlite3_stm
202c0 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  t*, int);.SQLITE
202d0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
202e0 5f 62 69 6e 64 5f 74 65 78 74 28 73 71 6c 69 74  _bind_text(sqlit
202f0 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63  e3_stmt*, int, c
20300 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 20  onst char*, int 
20310 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  n, void(*)(void*
20320 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ));.SQLITE_API i
20330 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
20340 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73  text16(sqlite3_s
20350 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74  tmt*, int, const
20360 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69   void*, int, voi
20370 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51  d(*)(void*));.SQ
20380 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
20390 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28  ite3_bind_value(
203a0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
203b0 6e 74 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  nt, const sqlite
203c0 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54  3_value*);.SQLIT
203d0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
203e0 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28  3_bind_zeroblob(
203f0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
20400 6e 74 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a  nt, int n);../*.
20410 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d  ** CAPI3REF: Num
20420 62 65 72 20 4f 66 20 53 51 4c 20 50 61 72 61 6d  ber Of SQL Param
20430 65 74 65 72 73 20 7b 46 31 33 36 30 30 7d 0a 2a  eters {F13600}.*
20440 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
20450 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  e can be used to
20460 20 66 69 6e 64 20 74 68 65 20 6e 75 6d 62 65 72   find the number
20470 20 6f 66 20 53 51 4c 20 70 61 72 61 6d 65 74 65   of SQL paramete
20480 72 73 0a 2a 2a 20 69 6e 20 61 20 70 72 65 70 61  rs.** in a prepa
20490 72 65 64 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  red statement.  
204a0 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 20 61  SQL parameters a
204b0 72 65 20 74 6f 6b 65 6e 73 20 6f 66 20 74 68 65  re tokens of the
204c0 0a 2a 2a 20 66 6f 72 6d 20 22 3f 22 2c 20 22 3f  .** form "?", "?
204d0 4e 4e 4e 22 2c 20 22 3a 41 41 41 22 2c 20 22 24  NNN", ":AAA", "$
204e0 41 41 41 22 2c 20 6f 72 20 22 40 41 41 41 22 20  AAA", or "@AAA" 
204f0 74 68 61 74 20 73 65 72 76 65 20 61 73 0a 2a 2a  that serve as.**
20500 20 70 6c 61 63 65 2d 68 6f 6c 64 65 72 73 20 66   place-holders f
20510 6f 72 20 76 61 6c 75 65 73 20 74 68 61 74 20 61  or values that a
20520 72 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  re [sqlite3_bind
20530 5f 62 6c 6f 62 20 7c 20 62 6f 75 6e 64 5d 0a 2a  _blob | bound].*
20540 2a 20 74 6f 20 74 68 65 20 70 61 72 61 6d 65 74  * to the paramet
20550 65 72 73 20 61 74 20 61 20 6c 61 74 65 72 20 74  ers at a later t
20560 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ime..**.** This 
20570 72 6f 75 74 69 6e 65 20 61 63 74 75 61 6c 6c 79  routine actually
20580 20 72 65 74 75 72 6e 73 20 74 68 65 20 69 6e 64   returns the ind
20590 65 78 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73  ex of the larges
205a0 74 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20  t parameter..** 
205b0 46 6f 72 20 61 6c 6c 20 66 6f 72 6d 73 20 65 78  For all forms ex
205c0 63 65 70 74 20 3f 4e 4e 4e 2c 20 74 68 69 73 20  cept ?NNN, this 
205d0 77 69 6c 6c 20 63 6f 72 72 65 73 70 6f 6e 64 20  will correspond 
205e0 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  to the number of
205f0 0a 2a 2a 20 75 6e 69 71 75 65 20 70 61 72 61 6d  .** unique param
20600 65 74 65 72 73 2e 20 20 49 66 20 70 61 72 61 6d  eters.  If param
20610 65 74 65 72 73 20 6f 66 20 74 68 65 20 3f 4e 4e  eters of the ?NN
20620 4e 20 61 72 65 20 75 73 65 64 2c 20 74 68 65 72  N are used, ther
20630 65 20 6d 61 79 0a 2a 2a 20 62 65 20 67 61 70 73  e may.** be gaps
20640 20 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2a   in the list..**
20650 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73  .** See also: [s
20660 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
20670 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d  |sqlite3_bind()]
20680 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69  ,.** [sqlite3_bi
20690 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d  nd_parameter_nam
206a0 65 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71  e()], and.** [sq
206b0 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
206c0 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a  eter_index()]..*
206d0 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
206e0 0a 2a 2a 0a 2a 2a 20 7b 46 31 33 36 30 31 7d 20  .**.** {F13601} 
206f0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e  The [sqlite3_bin
20700 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e  d_parameter_coun
20710 74 28 53 29 5d 20 69 6e 74 65 72 66 61 63 65 20  t(S)] interface 
20720 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20 20  returns.**      
20730 20 20 20 20 74 68 65 20 6c 61 72 67 65 73 74 20      the largest 
20740 69 6e 64 65 78 20 6f 66 20 61 6c 6c 20 53 51 4c  index of all SQL
20750 20 70 61 72 61 6d 65 74 65 72 73 20 69 6e 20 74   parameters in t
20760 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  he.**          [
20770 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
20780 6e 74 5d 20 53 2c 20 6f 72 20 30 20 69 66 20 53  nt] S, or 0 if S
20790 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e  .**          con
207a0 74 61 69 6e 73 20 6e 6f 20 53 51 4c 20 70 61 72  tains no SQL par
207b0 61 6d 65 74 65 72 73 2e 0a 2a 2f 0a 53 51 4c 49  ameters..*/.SQLI
207c0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
207d0 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
207e0 72 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f  r_count(sqlite3_
207f0 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  stmt*);../*.** C
20800 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20 4f 66  API3REF: Name Of
20810 20 41 20 48 6f 73 74 20 50 61 72 61 6d 65 74 65   A Host Paramete
20820 72 20 7b 46 31 33 36 32 30 7d 0a 2a 2a 0a 2a 2a  r {F13620}.**.**
20830 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
20840 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
20850 74 6f 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  to the name of t
20860 68 65 20 6e 2d 74 68 0a 2a 2a 20 53 51 4c 20 70  he n-th.** SQL p
20870 61 72 61 6d 65 74 65 72 20 69 6e 20 61 20 5b 70  arameter in a [p
20880 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
20890 74 5d 2e 0a 2a 2a 20 53 51 4c 20 70 61 72 61 6d  t]..** SQL param
208a0 65 74 65 72 73 20 6f 66 20 74 68 65 20 66 6f 72  eters of the for
208b0 6d 20 22 3f 4e 4e 4e 22 20 6f 72 20 22 3a 41 41  m "?NNN" or ":AA
208c0 41 22 20 6f 72 20 22 40 41 41 41 22 20 6f 72 20  A" or "@AAA" or 
208d0 22 24 41 41 41 22 0a 2a 2a 20 68 61 76 65 20 61  "$AAA".** have a
208e0 20 6e 61 6d 65 20 77 68 69 63 68 20 69 73 20 74   name which is t
208f0 68 65 20 73 74 72 69 6e 67 20 22 3f 4e 4e 4e 22  he string "?NNN"
20900 20 6f 72 20 22 3a 41 41 41 22 20 6f 72 20 22 40   or ":AAA" or "@
20910 41 41 41 22 20 6f 72 20 22 24 41 41 41 22 0a 2a  AAA" or "$AAA".*
20920 2a 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a  * respectively..
20930 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  ** In other word
20940 73 2c 20 74 68 65 20 69 6e 69 74 69 61 6c 20 22  s, the initial "
20950 3a 22 20 6f 72 20 22 24 22 20 6f 72 20 22 40 22  :" or "$" or "@"
20960 20 6f 72 20 22 3f 22 0a 2a 2a 20 69 73 20 69 6e   or "?".** is in
20970 63 6c 75 64 65 64 20 61 73 20 70 61 72 74 20 6f  cluded as part o
20980 66 20 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 20 50  f the name..** P
20990 61 72 61 6d 65 74 65 72 73 20 6f 66 20 74 68 65  arameters of the
209a0 20 66 6f 72 6d 20 22 3f 22 20 77 69 74 68 6f 75   form "?" withou
209b0 74 20 61 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e  t a following in
209c0 74 65 67 65 72 20 68 61 76 65 20 6e 6f 20 6e 61  teger have no na
209d0 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  me..**.** The fi
209e0 72 73 74 20 68 6f 73 74 20 70 61 72 61 6d 65 74  rst host paramet
209f0 65 72 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20  er has an index 
20a00 6f 66 20 31 2c 20 6e 6f 74 20 30 2e 0a 2a 2a 0a  of 1, not 0..**.
20a10 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20  ** If the value 
20a20 6e 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67  n is out of rang
20a30 65 20 6f 72 20 69 66 20 74 68 65 20 6e 2d 74 68  e or if the n-th
20a40 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a   parameter is.**
20a50 20 6e 61 6d 65 6c 65 73 73 2c 20 74 68 65 6e 20   nameless, then 
20a60 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64  NULL is returned
20a70 2e 20 20 54 68 65 20 72 65 74 75 72 6e 65 64 20  .  The returned 
20a80 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 61 6c 77  string is.** alw
20a90 61 79 73 20 69 6e 20 74 68 65 20 55 54 46 2d 38  ays in the UTF-8
20aa0 20 65 6e 63 6f 64 69 6e 67 20 65 76 65 6e 20 69   encoding even i
20ab0 66 20 74 68 65 20 6e 61 6d 65 64 20 70 61 72 61  f the named para
20ac0 6d 65 74 65 72 20 77 61 73 0a 2a 2a 20 6f 72 69  meter was.** ori
20ad0 67 69 6e 61 6c 6c 79 20 73 70 65 63 69 66 69 65  ginally specifie
20ae0 64 20 61 73 20 55 54 46 2d 31 36 20 69 6e 20 5b  d as UTF-16 in [
20af0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
20b00 36 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69  6()] or.** [sqli
20b10 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32  te3_prepare16_v2
20b20 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  ()]..**.** See a
20b30 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62 69  lso: [sqlite3_bi
20b40 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f  nd_blob|sqlite3_
20b50 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c  bind()],.** [sql
20b60 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
20b70 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 20 61 6e  ter_count()], an
20b80 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69  d.** [sqlite3_bi
20b90 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64  nd_parameter_ind
20ba0 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56  ex()]..**.** INV
20bb0 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b  ARIANTS:.**.** {
20bc0 46 31 33 36 32 31 7d 20 54 68 65 20 5b 73 71 6c  F13621} The [sql
20bd0 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
20be0 74 65 72 5f 6e 61 6d 65 28 53 2c 4e 29 5d 20 69  ter_name(S,N)] i
20bf0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
20c00 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 20 55  .**          a U
20c10 54 46 2d 38 20 72 65 6e 64 65 72 69 6e 67 20 6f  TF-8 rendering o
20c20 66 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  f the name of th
20c30 65 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20  e SQL parameter 
20c40 69 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  in.**          [
20c50 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
20c60 6e 74 5d 20 53 20 68 61 76 69 6e 67 20 69 6e 64  nt] S having ind
20c70 65 78 20 4e 2c 20 6f 72 0a 2a 2a 20 20 20 20 20  ex N, or.**     
20c80 20 20 20 20 20 4e 55 4c 4c 20 69 66 20 74 68 65       NULL if the
20c90 72 65 20 69 73 20 6e 6f 20 53 51 4c 20 70 61 72  re is no SQL par
20ca0 61 6d 65 74 65 72 20 77 69 74 68 20 69 6e 64 65  ameter with inde
20cb0 78 20 4e 20 6f 72 20 69 66 20 74 68 65 0a 2a 2a  x N or if the.**
20cc0 20 20 20 20 20 20 20 20 20 20 70 61 72 61 6d 65            parame
20cd0 74 65 72 20 77 69 74 68 20 69 6e 64 65 78 20 4e  ter with index N
20ce0 20 69 73 20 61 6e 20 61 6e 6f 6e 79 6d 6f 75 73   is an anonymous
20cf0 20 70 61 72 61 6d 65 74 65 72 20 22 3f 22 2e 0a   parameter "?"..
20d00 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
20d10 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
20d20 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  3_bind_parameter
20d30 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74  _name(sqlite3_st
20d40 6d 74 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a  mt*, int);../*.*
20d50 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6e 64 65  * CAPI3REF: Inde
20d60 78 20 4f 66 20 41 20 50 61 72 61 6d 65 74 65 72  x Of A Parameter
20d70 20 57 69 74 68 20 41 20 47 69 76 65 6e 20 4e 61   With A Given Na
20d80 6d 65 20 7b 46 31 33 36 34 30 7d 0a 2a 2a 0a 2a  me {F13640}.**.*
20d90 2a 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64  * Return the ind
20da0 65 78 20 6f 66 20 61 6e 20 53 51 4c 20 70 61 72  ex of an SQL par
20db0 61 6d 65 74 65 72 20 67 69 76 65 6e 20 69 74 73  ameter given its
20dc0 20 6e 61 6d 65 2e 20 20 54 68 65 0a 2a 2a 20 69   name.  The.** i
20dd0 6e 64 65 78 20 76 61 6c 75 65 20 72 65 74 75 72  ndex value retur
20de0 6e 65 64 20 69 73 20 73 75 69 74 61 62 6c 65 20  ned is suitable 
20df0 66 6f 72 20 75 73 65 20 61 73 20 74 68 65 20 73  for use as the s
20e00 65 63 6f 6e 64 0a 2a 2a 20 70 61 72 61 6d 65 74  econd.** paramet
20e10 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62  er to [sqlite3_b
20e20 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33  ind_blob|sqlite3
20e30 5f 62 69 6e 64 28 29 5d 2e 20 20 41 20 7a 65 72  _bind()].  A zer
20e40 6f 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64  o.** is returned
20e50 20 69 66 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20   if no matching 
20e60 70 61 72 61 6d 65 74 65 72 20 69 73 20 66 6f 75  parameter is fou
20e70 6e 64 2e 20 20 54 68 65 20 70 61 72 61 6d 65 74  nd.  The paramet
20e80 65 72 0a 2a 2a 20 6e 61 6d 65 20 6d 75 73 74 20  er.** name must 
20e90 62 65 20 67 69 76 65 6e 20 69 6e 20 55 54 46 2d  be given in UTF-
20ea0 38 20 65 76 65 6e 20 69 66 20 74 68 65 20 6f 72  8 even if the or
20eb0 69 67 69 6e 61 6c 20 73 74 61 74 65 6d 65 6e 74  iginal statement
20ec0 0a 2a 2a 20 77 61 73 20 70 72 65 70 61 72 65 64  .** was prepared
20ed0 20 66 72 6f 6d 20 55 54 46 2d 31 36 20 74 65 78   from UTF-16 tex
20ee0 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  t using [sqlite3
20ef0 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d  _prepare16_v2()]
20f00 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
20f10 3a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  : [sqlite3_bind_
20f20 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e  blob|sqlite3_bin
20f30 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65  d()],.** [sqlite
20f40 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  3_bind_parameter
20f50 5f 63 6f 75 6e 74 28 29 5d 2c 20 61 6e 64 0a 2a  _count()], and.*
20f60 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  * [sqlite3_bind_
20f70 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28  parameter_index(
20f80 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49  )]..**.** INVARI
20f90 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31 33  ANTS:.**.** {F13
20fa0 36 34 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  641} The [sqlite
20fb0 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  3_bind_parameter
20fc0 5f 69 6e 64 65 78 28 53 2c 4e 29 5d 20 69 6e 74  _index(S,N)] int
20fd0 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 0a 2a  erface returns.*
20fe0 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 69  *          the i
20ff0 6e 64 65 78 20 6f 66 20 53 51 4c 20 70 61 72 61  ndex of SQL para
21000 6d 65 74 65 72 20 69 6e 20 5b 70 72 65 70 61 72  meter in [prepar
21010 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a  ed statement].**
21020 20 20 20 20 20 20 20 20 20 20 53 20 77 68 6f 73            S whos
21030 65 20 6e 61 6d 65 20 6d 61 74 63 68 65 73 20 74  e name matches t
21040 68 65 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20  he UTF-8 string 
21050 4e 2c 20 6f 72 20 30 20 69 66 20 74 68 65 72 65  N, or 0 if there
21060 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   is.**          
21070 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c  no match..*/.SQL
21080 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
21090 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
210a0 65 72 5f 69 6e 64 65 78 28 73 71 6c 69 74 65 33  er_index(sqlite3
210b0 5f 73 74 6d 74 2a 2c 20 63 6f 6e 73 74 20 63 68  _stmt*, const ch
210c0 61 72 20 2a 7a 4e 61 6d 65 29 3b 0a 0a 2f 2a 0a  ar *zName);../*.
210d0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 73  ** CAPI3REF: Res
210e0 65 74 20 41 6c 6c 20 42 69 6e 64 69 6e 67 73 20  et All Bindings 
210f0 4f 6e 20 41 20 50 72 65 70 61 72 65 64 20 53 74  On A Prepared St
21100 61 74 65 6d 65 6e 74 20 7b 46 31 33 36 36 30 7d  atement {F13660}
21110 0a 2a 2a 0a 2a 2a 20 43 6f 6e 74 72 61 72 79 20  .**.** Contrary 
21120 74 6f 20 74 68 65 20 69 6e 74 75 69 74 69 6f 6e  to the intuition
21130 20 6f 66 20 6d 61 6e 79 2c 20 5b 73 71 6c 69 74   of many, [sqlit
21140 65 33 5f 72 65 73 65 74 28 29 5d 20 64 6f 65 73  e3_reset()] does
21150 20 6e 6f 74 0a 2a 2a 20 72 65 73 65 74 20 74 68   not.** reset th
21160 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  e [sqlite3_bind_
21170 62 6c 6f 62 20 7c 20 62 69 6e 64 69 6e 67 73 5d  blob | bindings]
21180 20 6f 6e 20 61 20 0a 2a 2a 20 5b 70 72 65 70 61   on a .** [prepa
21190 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20  red statement]. 
211a0 20 55 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e   Use this routin
211b0 65 20 74 6f 0a 2a 2a 20 72 65 73 65 74 20 61 6c  e to.** reset al
211c0 6c 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72  l host parameter
211d0 73 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  s to NULL..**.**
211e0 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a   INVARIANTS:.**.
211f0 2a 2a 20 7b 46 31 33 36 36 31 7d 20 54 68 65 20  ** {F13661} The 
21200 5b 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62  [sqlite3_clear_b
21210 69 6e 64 69 6e 67 73 28 53 29 5d 20 69 6e 74 65  indings(S)] inte
21220 72 66 61 63 65 20 72 65 73 65 74 73 20 61 6c 6c  rface resets all
21230 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 53 51 4c  .**          SQL
21240 20 70 61 72 61 6d 65 74 65 72 20 62 69 6e 64 69   parameter bindi
21250 6e 67 73 20 69 6e 20 5b 70 72 65 70 61 72 65 64  ngs in [prepared
21260 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 0a 2a 2a   statement] S.**
21270 20 20 20 20 20 20 20 20 20 20 62 61 63 6b 20 74            back t
21280 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54  o NULL..*/.SQLIT
21290 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
212a0 33 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73  3_clear_bindings
212b0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b  (sqlite3_stmt*);
212c0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
212d0 3a 20 4e 75 6d 62 65 72 20 4f 66 20 43 6f 6c 75  : Number Of Colu
212e0 6d 6e 73 20 49 6e 20 41 20 52 65 73 75 6c 74 20  mns In A Result 
212f0 53 65 74 20 7b 46 31 33 37 31 30 7d 0a 2a 2a 0a  Set {F13710}.**.
21300 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
21310 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
21320 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
21330 74 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  t returned by th
21340 65 20 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20  e .** [prepared 
21350 73 74 61 74 65 6d 65 6e 74 5d 2e 20 54 68 69 73  statement]. This
21360 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
21370 20 30 0a 2a 2a 20 69 66 20 70 53 74 6d 74 20 69   0.** if pStmt i
21380 73 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65  s an SQL stateme
21390 6e 74 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74  nt that does not
213a0 20 72 65 74 75 72 6e 20 64 61 74 61 20 28 66 6f   return data (fo
213b0 72 20 0a 2a 2a 20 65 78 61 6d 70 6c 65 20 61 6e  r .** example an
213c0 20 55 50 44 41 54 45 29 2e 0a 2a 2a 0a 2a 2a 20   UPDATE)..**.** 
213d0 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a  INVARIANTS:.**.*
213e0 2a 20 7b 46 31 33 37 31 31 7d 20 54 68 65 20 5b  * {F13711} The [
213f0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63  sqlite3_column_c
21400 6f 75 6e 74 28 53 29 5d 20 69 6e 74 65 72 66 61  ount(S)] interfa
21410 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  ce returns the n
21420 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 20 20 20 20  umber of.**     
21430 20 20 20 20 20 63 6f 6c 75 6d 6e 73 20 69 6e 20       columns in 
21440 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 67  the result set g
21450 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 0a  enerated by the.
21460 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 65  **          [pre
21470 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
21480 20 53 2c 20 6f 72 20 30 20 69 66 20 53 20 64 6f   S, or 0 if S do
21490 65 73 20 6e 6f 74 20 67 65 6e 65 72 61 74 65 0a  es not generate.
214a0 2a 2a 20 20 20 20 20 20 20 20 20 20 61 20 72 65  **          a re
214b0 73 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c  sult set..*/.SQL
214c0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
214d0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74  te3_column_count
214e0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
214f0 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  Stmt);../*.** CA
21500 50 49 33 52 45 46 3a 20 43 6f 6c 75 6d 6e 20 4e  PI3REF: Column N
21510 61 6d 65 73 20 49 6e 20 41 20 52 65 73 75 6c 74  ames In A Result
21520 20 53 65 74 20 7b 46 31 33 37 32 30 7d 0a 2a 2a   Set {F13720}.**
21530 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e  .** These routin
21540 65 73 20 72 65 74 75 72 6e 20 74 68 65 20 6e 61  es return the na
21550 6d 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20 61  me assigned to a
21560 20 70 61 72 74 69 63 75 6c 61 72 20 63 6f 6c 75   particular colu
21570 6d 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73  mn.** in the res
21580 75 6c 74 20 73 65 74 20 6f 66 20 61 20 53 45 4c  ult set of a SEL
21590 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ECT statement.  
215a0 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  The sqlite3_colu
215b0 6d 6e 5f 6e 61 6d 65 28 29 0a 2a 2a 20 69 6e 74  mn_name().** int
215c0 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61  erface returns a
215d0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 7a 65   pointer to a ze
215e0 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54  ro-terminated UT
215f0 46 38 20 73 74 72 69 6e 67 0a 2a 2a 20 61 6e 64  F8 string.** and
21600 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
21610 6e 61 6d 65 31 36 28 29 20 72 65 74 75 72 6e 73  name16() returns
21620 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
21630 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a  zero-terminated.
21640 2a 2a 20 55 54 46 31 36 20 73 74 72 69 6e 67 2e  ** UTF16 string.
21650 20 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61    The first para
21660 6d 65 74 65 72 20 69 73 20 74 68 65 0a 2a 2a 20  meter is the.** 
21670 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
21680 65 6e 74 5d 20 74 68 61 74 20 69 6d 70 6c 65 6d  ent] that implem
21690 65 6e 74 73 20 74 68 65 20 53 45 4c 45 43 54 20  ents the SELECT 
216a0 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 54 68  statement..** Th
216b0 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
216c0 65 72 20 69 73 20 74 68 65 20 63 6f 6c 75 6d 6e  er is the column
216d0 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 20 6c 65   number.  The le
216e0 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 69  ft-most column i
216f0 73 0a 2a 2a 20 6e 75 6d 62 65 72 20 30 2e 0a 2a  s.** number 0..*
21700 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65  *.** The returne
21710 64 20 73 74 72 69 6e 67 20 70 6f 69 6e 74 65 72  d string pointer
21720 20 69 73 20 76 61 6c 69 64 20 75 6e 74 69 6c 20   is valid until 
21730 65 69 74 68 65 72 20 74 68 65 20 0a 2a 2a 20 5b  either the .** [
21740 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
21750 6e 74 5d 20 69 73 20 64 65 73 74 72 6f 79 65 64  nt] is destroyed
21760 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e   by [sqlite3_fin
21770 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 6f 72 20 75  alize()].** or u
21780 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 20 63 61  ntil the next ca
21790 6c 6c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ll sqlite3_colum
217a0 6e 5f 6e 61 6d 65 28 29 20 6f 72 20 73 71 6c 69  n_name() or sqli
217b0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31  te3_column_name1
217c0 36 28 29 0a 2a 2a 20 6f 6e 20 74 68 65 20 73 61  6().** on the sa
217d0 6d 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a  me column..**.**
217e0 20 49 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c   If sqlite3_mall
217f0 6f 63 28 29 20 66 61 69 6c 73 20 64 75 72 69 6e  oc() fails durin
21800 67 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67  g the processing
21810 20 6f 66 20 65 69 74 68 65 72 20 72 6f 75 74 69   of either routi
21820 6e 65 0a 2a 2a 20 28 66 6f 72 20 65 78 61 6d 70  ne.** (for examp
21830 6c 65 20 64 75 72 69 6e 67 20 61 20 63 6f 6e 76  le during a conv
21840 65 72 73 69 6f 6e 20 66 72 6f 6d 20 55 54 46 2d  ersion from UTF-
21850 38 20 74 6f 20 55 54 46 2d 31 36 29 20 74 68 65  8 to UTF-16) the
21860 6e 20 61 0a 2a 2a 20 4e 55 4c 4c 20 70 6f 69 6e  n a.** NULL poin
21870 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ter is returned.
21880 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20  .**.** The name 
21890 6f 66 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75  of a result colu
218a0 6d 6e 20 69 73 20 74 68 65 20 76 61 6c 75 65 20  mn is the value 
218b0 6f 66 20 74 68 65 20 22 41 53 22 20 63 6c 61 75  of the "AS" clau
218c0 73 65 20 66 6f 72 0a 2a 2a 20 74 68 61 74 20 63  se for.** that c
218d0 6f 6c 75 6d 6e 2c 20 69 66 20 74 68 65 72 65 20  olumn, if there 
218e0 69 73 20 61 6e 20 41 53 20 63 6c 61 75 73 65 2e  is an AS clause.
218f0 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f    If there is no
21900 20 41 53 20 63 6c 61 75 73 65 0a 2a 2a 20 74 68   AS clause.** th
21910 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  en the name of t
21920 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 75 6e 73  he column is uns
21930 70 65 63 69 66 69 65 64 20 61 6e 64 20 6d 61 79  pecified and may
21940 20 63 68 61 6e 67 65 20 66 72 6f 6d 0a 2a 2a 20   change from.** 
21950 6f 6e 65 20 72 65 6c 65 61 73 65 20 6f 66 20 53  one release of S
21960 51 4c 69 74 65 20 74 6f 20 74 68 65 20 6e 65 78  QLite to the nex
21970 74 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  t..**.** INVARIA
21980 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31 33 37  NTS:.**.** {F137
21990 32 31 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c  21} A successful
219a0 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74   invocation of t
219b0 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75  he [sqlite3_colu
219c0 6d 6e 5f 6e 61 6d 65 28 53 2c 4e 29 5d 0a 2a 2a  mn_name(S,N)].**
219d0 20 20 20 20 20 20 20 20 20 20 69 6e 74 65 72 66            interf
219e0 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ace returns the 
219f0 6e 61 6d 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  name.**         
21a00 20 6f 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75   of the Nth colu
21a10 6d 6e 20 28 77 68 65 72 65 20 30 20 69 73 20 74  mn (where 0 is t
21a20 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c  he left-most col
21a30 75 6d 6e 29 20 66 6f 72 20 74 68 65 0a 2a 2a 20  umn) for the.** 
21a40 20 20 20 20 20 20 20 20 20 72 65 73 75 6c 74 20           result 
21a50 73 65 74 20 6f 66 20 5b 70 72 65 70 61 72 65 64  set of [prepared
21a60 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 61 73   statement] S as
21a70 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 7a   a.**          z
21a80 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55  ero-terminated U
21a90 54 46 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a  TF-8 string..**.
21aa0 2a 2a 20 7b 46 31 33 37 32 33 7d 20 41 20 73 75  ** {F13723} A su
21ab0 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 74  ccessful invocat
21ac0 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c 69  ion of the [sqli
21ad0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31  te3_column_name1
21ae0 36 28 53 2c 4e 29 5d 0a 2a 2a 20 20 20 20 20 20  6(S,N)].**      
21af0 20 20 20 20 69 6e 74 65 72 66 61 63 65 20 72 65      interface re
21b00 74 75 72 6e 73 20 74 68 65 20 6e 61 6d 65 0a 2a  turns the name.*
21b10 2a 20 20 20 20 20 20 20 20 20 20 6f 66 20 74 68  *          of th
21b20 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 28 77 68  e Nth column (wh
21b30 65 72 65 20 30 20 69 73 20 74 68 65 20 6c 65 66  ere 0 is the lef
21b40 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 29 20 66  t-most column) f
21b50 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  or the.**       
21b60 20 20 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66     result set of
21b70 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
21b80 6d 65 6e 74 5d 20 53 20 61 73 20 61 0a 2a 2a 20  ment] S as a.** 
21b90 20 20 20 20 20 20 20 20 20 7a 65 72 6f 2d 74 65           zero-te
21ba0 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 31 36 20  rminated UTF-16 
21bb0 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20 6e 61  string in the na
21bc0 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 2e  tive byte order.
21bd0 0a 2a 2a 0a 2a 2a 20 7b 46 31 33 37 32 34 7d 20  .**.** {F13724} 
21be0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c  The [sqlite3_col
21bf0 75 6d 6e 5f 6e 61 6d 65 28 29 5d 20 61 6e 64 20  umn_name()] and 
21c00 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  [sqlite3_column_
21c10 6e 61 6d 65 31 36 28 29 5d 0a 2a 2a 20 20 20 20  name16()].**    
21c20 20 20 20 20 20 20 69 6e 74 65 72 66 61 63 65 73        interfaces
21c30 20 72 65 74 75 72 6e 20 61 20 4e 55 4c 4c 20 70   return a NULL p
21c40 6f 69 6e 74 65 72 20 69 66 20 74 68 65 79 20 61  ointer if they a
21c50 72 65 20 75 6e 61 62 6c 65 20 74 6f 0a 2a 2a 20  re unable to.** 
21c60 20 20 20 20 20 20 20 20 20 61 6c 6c 6f 63 61 74           allocat
21c70 65 20 6d 65 6d 6f 72 79 20 6d 65 6d 6f 72 79 20  e memory memory 
21c80 74 6f 20 68 6f 6c 64 20 74 68 65 72 65 20 6e 6f  to hold there no
21c90 72 6d 61 6c 20 72 65 74 75 72 6e 20 73 74 72 69  rmal return stri
21ca0 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 33 37  ngs..**.** {F137
21cb0 32 35 7d 20 49 66 20 74 68 65 20 4e 20 70 61 72  25} If the N par
21cc0 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74  ameter to [sqlit
21cd0 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 53  e3_column_name(S
21ce0 2c 4e 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20  ,N)] or.**      
21cf0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c      [sqlite3_col
21d00 75 6d 6e 5f 6e 61 6d 65 31 36 28 53 2c 4e 29 5d  umn_name16(S,N)]
21d10 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65   is out of range
21d20 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 20 20  , then the.**   
21d30 20 20 20 20 20 20 20 69 6e 74 65 72 66 61 63 65         interface
21d40 73 20 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c  s returns a NULL
21d50 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 20 0a 2a 2a   pointer..** .**
21d60 20 7b 46 31 33 37 32 36 7d 20 54 68 65 20 73 74   {F13726} The st
21d70 72 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 62  rings returned b
21d80 79 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  y [sqlite3_colum
21d90 6e 5f 6e 61 6d 65 28 53 2c 4e 29 5d 20 61 6e 64  n_name(S,N)] and
21da0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71  .**          [sq
21db0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d  lite3_column_nam
21dc0 65 31 36 28 53 2c 4e 29 5d 20 61 72 65 20 76 61  e16(S,N)] are va
21dd0 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 6e 65  lid until the ne
21de0 78 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63  xt.**          c
21df0 61 6c 6c 20 74 6f 20 65 69 74 68 65 72 20 72 6f  all to either ro
21e00 75 74 69 6e 65 20 77 69 74 68 20 74 68 65 20 73  utine with the s
21e10 61 6d 65 20 53 20 61 6e 64 20 4e 20 70 61 72 61  ame S and N para
21e20 6d 65 74 65 72 73 0a 2a 2a 20 20 20 20 20 20 20  meters.**       
21e30 20 20 20 6f 72 20 75 6e 74 69 6c 20 5b 73 71 6c     or until [sql
21e40 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 53 29  ite3_finalize(S)
21e50 5d 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a  ] is called..**.
21e60 2a 2a 20 7b 46 31 33 37 32 37 7d 20 57 68 65 6e  ** {F13727} When
21e70 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e   a result column
21e80 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 73   of a [SELECT] s
21e90 74 61 74 65 6d 65 6e 74 20 63 6f 6e 74 61 69 6e  tatement contain
21ea0 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e  s.**          an
21eb0 20 41 53 20 63 6c 61 75 73 65 2c 20 74 68 65 20   AS clause, the 
21ec0 6e 61 6d 65 20 6f 66 20 74 68 61 74 20 63 6f 6c  name of that col
21ed0 75 6d 6e 20 69 73 20 74 68 65 20 69 6e 64 65 6e  umn is the inden
21ee0 74 69 66 69 65 72 0a 2a 2a 20 20 20 20 20 20 20  tifier.**       
21ef0 20 20 20 74 6f 20 74 68 65 20 72 69 67 68 74 20     to the right 
21f00 6f 66 20 74 68 65 20 41 53 20 6b 65 79 77 6f 72  of the AS keywor
21f10 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  d..*/.SQLITE_API
21f20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
21f30 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  ite3_column_name
21f40 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20  (sqlite3_stmt*, 
21f50 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f 41  int N);.SQLITE_A
21f60 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
21f70 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61  qlite3_column_na
21f80 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d  me16(sqlite3_stm
21f90 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a 0a 2f 2a 0a  t*, int N);../*.
21fa0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 6f 75  ** CAPI3REF: Sou
21fb0 72 63 65 20 4f 66 20 44 61 74 61 20 49 6e 20 41  rce Of Data In A
21fc0 20 51 75 65 72 79 20 52 65 73 75 6c 74 20 7b 46   Query Result {F
21fd0 31 33 37 34 30 7d 0a 2a 2a 0a 2a 2a 20 54 68 65  13740}.**.** The
21fe0 73 65 20 72 6f 75 74 69 6e 65 73 20 70 72 6f 76  se routines prov
21ff0 69 64 65 20 61 20 6d 65 61 6e 73 20 74 6f 20 64  ide a means to d
22000 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20 63 6f  etermine what co
22010 6c 75 6d 6e 20 6f 66 20 77 68 61 74 0a 2a 2a 20  lumn of what.** 
22020 74 61 62 6c 65 20 69 6e 20 77 68 69 63 68 20 64  table in which d
22030 61 74 61 62 61 73 65 20 61 20 72 65 73 75 6c 74  atabase a result
22040 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61   of a SELECT sta
22050 74 65 6d 65 6e 74 20 63 6f 6d 65 73 20 66 72 6f  tement comes fro
22060 6d 2e 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f  m..** The name o
22070 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f  f the database o
22080 72 20 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d  r table or colum
22090 6e 20 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65  n can be returne
220a0 64 20 61 73 0a 2a 2a 20 65 69 74 68 65 72 20 61  d as.** either a
220b0 20 55 54 46 38 20 6f 72 20 55 54 46 31 36 20 73   UTF8 or UTF16 s
220c0 74 72 69 6e 67 2e 20 20 54 68 65 20 5f 64 61 74  tring.  The _dat
220d0 61 62 61 73 65 5f 20 72 6f 75 74 69 6e 65 73 20  abase_ routines 
220e0 72 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 64 61  return.** the da
220f0 74 61 62 61 73 65 20 6e 61 6d 65 2c 20 74 68 65  tabase name, the
22100 20 5f 74 61 62 6c 65 5f 20 72 6f 75 74 69 6e 65   _table_ routine
22110 73 20 72 65 74 75 72 6e 20 74 68 65 20 74 61 62  s return the tab
22120 6c 65 20 6e 61 6d 65 2c 20 61 6e 64 0a 2a 2a 20  le name, and.** 
22130 74 68 65 20 6f 72 69 67 69 6e 5f 20 72 6f 75 74  the origin_ rout
22140 69 6e 65 73 20 72 65 74 75 72 6e 20 74 68 65 20  ines return the 
22150 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a 2a 20  column name..** 
22160 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72  The returned str
22170 69 6e 67 20 69 73 20 76 61 6c 69 64 20 75 6e 74  ing is valid unt
22180 69 6c 0a 2a 2a 20 74 68 65 20 5b 70 72 65 70 61  il.** the [prepa
22190 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69  red statement] i
221a0 73 20 64 65 73 74 72 6f 79 65 64 20 75 73 69 6e  s destroyed usin
221b0 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69  g.** [sqlite3_fi
221c0 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20 75 6e 74  nalize()] or unt
221d0 69 6c 20 74 68 65 20 73 61 6d 65 20 69 6e 66 6f  il the same info
221e0 72 6d 61 74 69 6f 6e 20 69 73 20 72 65 71 75 65  rmation is reque
221f0 73 74 65 64 0a 2a 2a 20 61 67 61 69 6e 20 69 6e  sted.** again in
22200 20 61 20 64 69 66 66 65 72 65 6e 74 20 65 6e 63   a different enc
22210 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  oding..**.** The
22220 20 6e 61 6d 65 73 20 72 65 74 75 72 6e 65 64 20   names returned 
22230 61 72 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  are the original
22240 20 75 6e 2d 61 6c 69 61 73 65 64 20 6e 61 6d 65   un-aliased name
22250 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61  s of the.** data
22260 62 61 73 65 2c 20 74 61 62 6c 65 2c 20 61 6e 64  base, table, and
22270 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 54   column..**.** T
22280 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
22290 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  t to the followi
222a0 6e 67 20 63 61 6c 6c 73 20 69 73 20 61 20 5b 70  ng calls is a [p
222b0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
222c0 74 5d 2e 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e  t]..** These fun
222d0 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 69 6e  ctions return in
222e0 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
222f0 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 72  the Nth column r
22300 65 74 75 72 6e 65 64 20 62 79 20 0a 2a 2a 20 74  eturned by .** t
22310 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 77 68  he statement, wh
22320 65 72 65 20 4e 20 69 73 20 74 68 65 20 73 65 63  ere N is the sec
22330 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61 72 67  ond function arg
22340 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ument..**.** If 
22350 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 72  the Nth column r
22360 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 73  eturned by the s
22370 74 61 74 65 6d 65 6e 74 20 69 73 20 61 6e 20 65  tatement is an e
22380 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 6f 72 20  xpression.** or 
22390 73 75 62 71 75 65 72 79 20 61 6e 64 20 69 73 20  subquery and is 
223a0 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20 76 61 6c  not a column val
223b0 75 65 2c 20 74 68 65 6e 20 61 6c 6c 20 6f 66 20  ue, then all of 
223c0 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 0a  these functions.
223d0 2a 2a 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20  ** return NULL. 
223e0 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 20 6d   These routine m
223f0 69 67 68 74 20 61 6c 73 6f 20 72 65 74 75 72 6e  ight also return
22400 20 4e 55 4c 4c 20 69 66 20 61 20 6d 65 6d 6f 72   NULL if a memor
22410 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  y.** allocation 
22420 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 20 20 4f  error occurs.  O
22430 74 68 65 72 77 69 73 65 2c 20 74 68 65 79 20 72  therwise, they r
22440 65 74 75 72 6e 20 74 68 65 20 0a 2a 2a 20 6e 61  eturn the .** na
22450 6d 65 20 6f 66 20 74 68 65 20 61 74 74 61 63 68  me of the attach
22460 65 64 20 64 61 74 61 62 61 73 65 2c 20 74 61 62  ed database, tab
22470 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 74 68  le and column th
22480 61 74 20 71 75 65 72 79 20 72 65 73 75 6c 74 0a  at query result.
22490 2a 2a 20 63 6f 6c 75 6d 6e 20 77 61 73 20 65 78  ** column was ex
224a0 74 72 61 63 74 65 64 20 66 72 6f 6d 2e 0a 2a 2a  tracted from..**
224b0 0a 2a 2a 20 41 73 20 77 69 74 68 20 61 6c 6c 20  .** As with all 
224c0 6f 74 68 65 72 20 53 51 4c 69 74 65 20 41 50 49  other SQLite API
224d0 73 2c 20 74 68 6f 73 65 20 70 6f 73 74 66 69 78  s, those postfix
224e0 65 64 20 77 69 74 68 20 22 31 36 22 20 72 65 74  ed with "16" ret
224f0 75 72 6e 0a 2a 2a 20 55 54 46 2d 31 36 20 65 6e  urn.** UTF-16 en
22500 63 6f 64 65 64 20 73 74 72 69 6e 67 73 2c 20 74  coded strings, t
22510 68 65 20 6f 74 68 65 72 20 66 75 6e 63 74 69 6f  he other functio
22520 6e 73 20 72 65 74 75 72 6e 20 55 54 46 2d 38 2e  ns return UTF-8.
22530 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65   {END}.**.** The
22540 73 65 20 41 50 49 73 20 61 72 65 20 6f 6e 6c 79  se APIs are only
22550 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68   available if th
22560 65 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f  e library was co
22570 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 20  mpiled with the 
22580 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  .** SQLITE_ENABL
22590 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54  E_COLUMN_METADAT
225a0 41 20 70 72 65 70 72 6f 63 65 73 73 6f 72 20 73  A preprocessor s
225b0 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a 2a  ymbol defined..*
225c0 2a 0a 2a 2a 20 7b 55 31 33 37 35 31 7d 0a 2a 2a  *.** {U13751}.**
225d0 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20   If two or more 
225e0 74 68 72 65 61 64 73 20 63 61 6c 6c 20 6f 6e 65  threads call one
225f0 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 73   or more of thes
22600 65 20 72 6f 75 74 69 6e 65 73 20 61 67 61 69 6e  e routines again
22610 73 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 70  st the same.** p
22620 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
22630 74 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 61 74 20  t and column at 
22640 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 74 68  the same time th
22650 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 61  en the results a
22660 72 65 0a 2a 2a 20 75 6e 64 65 66 69 6e 65 64 2e  re.** undefined.
22670 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54  .**.** INVARIANT
22680 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31 33 37 34 31  S:.**.** {F13741
22690 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63  } The [sqlite3_c
226a0 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e  olumn_database_n
226b0 61 6d 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66  ame(S,N)] interf
226c0 61 63 65 20 72 65 74 75 72 6e 73 20 65 69 74 68  ace returns eith
226d0 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  er.**          t
226e0 68 65 20 55 54 46 2d 38 20 7a 65 72 6f 2d 74 65  he UTF-8 zero-te
226f0 72 6d 69 6e 61 74 65 64 20 6e 61 6d 65 20 6f 66  rminated name of
22700 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 72   the database fr
22710 6f 6d 20 77 68 69 63 68 20 74 68 65 20 0a 2a 2a  om which the .**
22720 20 20 20 20 20 20 20 20 20 20 4e 74 68 20 72 65            Nth re
22730 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 5b  sult column of [
22740 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
22750 6e 74 5d 20 53 20 0a 2a 2a 20 20 20 20 20 20 20  nt] S .**       
22760 20 20 20 69 73 20 65 78 74 72 61 63 74 65 64 2c     is extracted,
22770 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 20   or NULL if the 
22780 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f  the Nth column o
22790 66 20 53 20 69 73 20 61 0a 2a 2a 20 20 20 20 20  f S is a.**     
227a0 20 20 20 20 20 67 65 6e 65 72 61 6c 20 65 78 70       general exp
227b0 72 65 73 73 69 6f 6e 20 6f 72 20 69 66 20 75 6e  ression or if un
227c0 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65  able to allocate
227d0 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20   memory.**      
227e0 20 20 20 20 74 6f 20 73 74 6f 72 65 20 74 68 65      to store the
227f0 20 6e 61 6d 65 2e 0a 2a 2a 20 20 20 20 20 20 20   name..**       
22800 20 20 20 0a 2a 2a 20 7b 46 31 33 37 34 32 7d 20     .** {F13742} 
22810 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c  The [sqlite3_col
22820 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d  umn_database_nam
22830 65 31 36 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66  e16(S,N)] interf
22840 61 63 65 20 72 65 74 75 72 6e 73 20 65 69 74 68  ace returns eith
22850 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  er.**          t
22860 68 65 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65  he UTF-16 native
22870 20 62 79 74 65 20 6f 72 64 65 72 0a 2a 2a 20 20   byte order.**  
22880 20 20 20 20 20 20 20 20 7a 65 72 6f 2d 74 65 72          zero-ter
22890 6d 69 6e 61 74 65 64 20 6e 61 6d 65 20 6f 66 20  minated name of 
228a0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 72 6f  the database fro
228b0 6d 20 77 68 69 63 68 20 74 68 65 20 0a 2a 2a 20  m which the .** 
228c0 20 20 20 20 20 20 20 20 20 4e 74 68 20 72 65 73           Nth res
228d0 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 5b 70  ult column of [p
228e0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
228f0 74 5d 20 53 20 0a 2a 2a 20 20 20 20 20 20 20 20  t] S .**        
22900 20 20 69 73 20 65 78 74 72 61 63 74 65 64 2c 20    is extracted, 
22910 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 74  or NULL if the t
22920 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  he Nth column of
22930 20 53 20 69 73 20 61 0a 2a 2a 20 20 20 20 20 20   S is a.**      
22940 20 20 20 20 67 65 6e 65 72 61 6c 20 65 78 70 72      general expr
22950 65 73 73 69 6f 6e 20 6f 72 20 69 66 20 75 6e 61  ession or if una
22960 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ble to allocate 
22970 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20 20  memory.**       
22980 20 20 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20     to store the 
22990 6e 61 6d 65 2e 0a 2a 2a 20 20 20 20 20 20 20 20  name..**        
229a0 20 20 0a 2a 2a 20 7b 46 31 33 37 34 33 7d 20 54    .** {F13743} T
229b0 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75  he [sqlite3_colu
229c0 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 28 53 2c  mn_table_name(S,
229d0 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65  N)] interface re
229e0 74 75 72 6e 73 20 65 69 74 68 65 72 0a 2a 2a 20  turns either.** 
229f0 20 20 20 20 20 20 20 20 20 74 68 65 20 55 54 46           the UTF
22a00 2d 38 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74  -8 zero-terminat
22a10 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  ed name of the t
22a20 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20  able from which 
22a30 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 20 20 20  the .**         
22a40 20 4e 74 68 20 72 65 73 75 6c 74 20 63 6f 6c 75   Nth result colu
22a50 6d 6e 20 6f 66 20 5b 70 72 65 70 61 72 65 64 20  mn of [prepared 
22a60 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 0a 2a 2a  statement] S .**
22a70 20 20 20 20 20 20 20 20 20 20 69 73 20 65 78 74            is ext
22a80 72 61 63 74 65 64 2c 20 6f 72 20 4e 55 4c 4c 20  racted, or NULL 
22a90 69 66 20 74 68 65 20 74 68 65 20 4e 74 68 20 63  if the the Nth c
22aa0 6f 6c 75 6d 6e 20 6f 66 20 53 20 69 73 20 61 0a  olumn of S is a.
22ab0 2a 2a 20 20 20 20 20 20 20 20 20 20 67 65 6e 65  **          gene
22ac0 72 61 6c 20 65 78 70 72 65 73 73 69 6f 6e 20 6f  ral expression o
22ad0 72 20 69 66 20 75 6e 61 62 6c 65 20 74 6f 20 61  r if unable to a
22ae0 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 0a 2a  llocate memory.*
22af0 2a 20 20 20 20 20 20 20 20 20 20 74 6f 20 73 74  *          to st
22b00 6f 72 65 20 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a  ore the name..**
22b10 20 20 20 20 20 20 20 20 20 20 0a 2a 2a 20 7b 46            .** {F
22b20 31 33 37 34 34 7d 20 54 68 65 20 5b 73 71 6c 69  13744} The [sqli
22b30 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65  te3_column_table
22b40 5f 6e 61 6d 65 31 36 28 53 2c 4e 29 5d 20 69 6e  _name16(S,N)] in
22b50 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
22b60 65 69 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 20  either.**       
22b70 20 20 20 74 68 65 20 55 54 46 2d 31 36 20 6e 61     the UTF-16 na
22b80 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 0a  tive byte order.
22b90 2a 2a 20 20 20 20 20 20 20 20 20 20 7a 65 72 6f  **          zero
22ba0 2d 74 65 72 6d 69 6e 61 74 65 64 20 6e 61 6d 65  -terminated name
22bb0 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 66 72   of the table fr
22bc0 6f 6d 20 77 68 69 63 68 20 74 68 65 20 0a 2a 2a  om which the .**
22bd0 20 20 20 20 20 20 20 20 20 20 4e 74 68 20 72 65            Nth re
22be0 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 5b  sult column of [
22bf0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
22c00 6e 74 5d 20 53 20 0a 2a 2a 20 20 20 20 20 20 20  nt] S .**       
22c10 20 20 20 69 73 20 65 78 74 72 61 63 74 65 64 2c     is extracted,
22c20 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 20   or NULL if the 
22c30 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f  the Nth column o
22c40 66 20 53 20 69 73 20 61 0a 2a 2a 20 20 20 20 20  f S is a.**     
22c50 20 20 20 20 20 67 65 6e 65 72 61 6c 20 65 78 70       general exp
22c60 72 65 73 73 69 6f 6e 20 6f 72 20 69 66 20 75 6e  ression or if un
22c70 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65  able to allocate
22c80 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20   memory.**      
22c90 20 20 20 20 74 6f 20 73 74 6f 72 65 20 74 68 65      to store the
22ca0 20 6e 61 6d 65 2e 0a 2a 2a 20 20 20 20 20 20 20   name..**       
22cb0 20 20 20 0a 2a 2a 20 7b 46 31 33 37 34 35 7d 20     .** {F13745} 
22cc0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c  The [sqlite3_col
22cd0 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 28  umn_origin_name(
22ce0 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20  S,N)] interface 
22cf0 72 65 74 75 72 6e 73 20 65 69 74 68 65 72 0a 2a  returns either.*
22d00 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 55  *          the U
22d10 54 46 2d 38 20 7a 65 72 6f 2d 74 65 72 6d 69 6e  TF-8 zero-termin
22d20 61 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65  ated name of the
22d30 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 66 72   table column fr
22d40 6f 6d 20 77 68 69 63 68 20 74 68 65 20 0a 2a 2a  om which the .**
22d50 20 20 20 20 20 20 20 20 20 20 4e 74 68 20 72 65            Nth re
22d60 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 5b  sult column of [
22d70 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
22d80 6e 74 5d 20 53 20 0a 2a 2a 20 20 20 20 20 20 20  nt] S .**       
22d90 20 20 20 69 73 20 65 78 74 72 61 63 74 65 64 2c     is extracted,
22da0 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 20   or NULL if the 
22db0 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f  the Nth column o
22dc0 66 20 53 20 69 73 20 61 0a 2a 2a 20 20 20 20 20  f S is a.**     
22dd0 20 20 20 20 20 67 65 6e 65 72 61 6c 20 65 78 70       general exp
22de0 72 65 73 73 69 6f 6e 20 6f 72 20 69 66 20 75 6e  ression or if un
22df0 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65  able to allocate
22e00 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20   memory.**      
22e10 20 20 20 20 74 6f 20 73 74 6f 72 65 20 74 68 65      to store the
22e20 20 6e 61 6d 65 2e 0a 2a 2a 20 20 20 20 20 20 20   name..**       
22e30 20 20 20 0a 2a 2a 20 7b 46 31 33 37 34 36 7d 20     .** {F13746} 
22e40 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c  The [sqlite3_col
22e50 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31  umn_origin_name1
22e60 36 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63  6(S,N)] interfac
22e70 65 20 72 65 74 75 72 6e 73 20 65 69 74 68 65 72  e returns either
22e80 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65  .**          the
22e90 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62   UTF-16 native b
22ea0 79 74 65 20 6f 72 64 65 72 0a 2a 2a 20 20 20 20  yte order.**    
22eb0 20 20 20 20 20 20 7a 65 72 6f 2d 74 65 72 6d 69        zero-termi
22ec0 6e 61 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68  nated name of th
22ed0 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 66  e table column f
22ee0 72 6f 6d 20 77 68 69 63 68 20 74 68 65 20 0a 2a  rom which the .*
22ef0 2a 20 20 20 20 20 20 20 20 20 20 4e 74 68 20 72  *          Nth r
22f00 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20  esult column of 
22f10 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
22f20 65 6e 74 5d 20 53 20 0a 2a 2a 20 20 20 20 20 20  ent] S .**      
22f30 20 20 20 20 69 73 20 65 78 74 72 61 63 74 65 64      is extracted
22f40 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65  , or NULL if the
22f50 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20   the Nth column 
22f60 6f 66 20 53 20 69 73 20 61 0a 2a 2a 20 20 20 20  of S is a.**    
22f70 20 20 20 20 20 20 67 65 6e 65 72 61 6c 20 65 78        general ex
22f80 70 72 65 73 73 69 6f 6e 20 6f 72 20 69 66 20 75  pression or if u
22f90 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74  nable to allocat
22fa0 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 20  e memory.**     
22fb0 20 20 20 20 20 74 6f 20 73 74 6f 72 65 20 74 68       to store th
22fc0 65 20 6e 61 6d 65 2e 0a 2a 2a 20 20 20 20 20 20  e name..**      
22fd0 20 20 20 20 0a 2a 2a 20 7b 46 31 33 37 34 38 7d      .** {F13748}
22fe0 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
22ff0 65 73 20 66 72 6f 6d 0a 2a 2a 20 20 20 20 20 20  es from.**      
23000 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c      [sqlite3_col
23010 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d  umn_database_nam
23020 65 7c 63 6f 6c 75 6d 6e 20 6d 65 74 61 64 61 74  e|column metadat
23030 61 20 69 6e 74 65 72 66 61 63 65 73 5d 0a 2a 2a  a interfaces].**
23040 20 20 20 20 20 20 20 20 20 20 61 72 65 20 76 61            are va
23050 6c 69 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  lid.**          
23060 66 6f 72 20 74 68 65 20 6c 69 66 65 74 69 6d 65  for the lifetime
23070 20 6f 66 20 74 68 65 20 5b 70 72 65 70 61 72 65   of the [prepare
23080 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20  d statement].** 
23090 20 20 20 20 20 20 20 20 20 6f 72 20 75 6e 74 69           or unti
230a0 6c 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 69  l the encoding i
230b0 73 20 63 68 61 6e 67 65 64 20 62 79 20 61 6e 6f  s changed by ano
230c0 74 68 65 72 20 6d 65 74 61 64 61 74 61 0a 2a 2a  ther metadata.**
230d0 20 20 20 20 20 20 20 20 20 20 69 6e 74 65 72 66            interf
230e0 61 63 65 20 63 61 6c 6c 20 66 6f 72 20 74 68 65  ace call for the
230f0 20 73 61 6d 65 20 70 72 65 70 61 72 65 64 20 73   same prepared s
23100 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 63 6f 6c  tatement and col
23110 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 4c 49 4d 49 54  umn..**.** LIMIT
23120 41 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 55  ATIONS:.**.** {U
23130 31 33 37 35 31 7d 20 49 66 20 74 77 6f 20 6f 72  13751} If two or
23140 20 6d 6f 72 65 20 74 68 72 65 61 64 73 20 63 61   more threads ca
23150 6c 6c 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a  ll one or more.*
23160 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
23170 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
23180 61 73 65 5f 6e 61 6d 65 7c 63 6f 6c 75 6d 6e 20  ase_name|column 
23190 6d 65 74 61 64 61 74 61 20 69 6e 74 65 72 66 61  metadata interfa
231a0 63 65 73 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20  ces].**         
231b0 20 74 68 65 20 73 61 6d 65 20 5b 70 72 65 70 61   the same [prepa
231c0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 61  red statement] a
231d0 6e 64 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  nd result column
231e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 74 20  .**          at 
231f0 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 74 68  the same time th
23200 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 61  en the results a
23210 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f  re undefined..*/
23220 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
23230 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  t char *sqlite3_
23240 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f  column_database_
23250 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  name(sqlite3_stm
23260 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t*,int);.SQLITE_
23270 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
23280 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
23290 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 73  atabase_name16(s
232a0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
232b0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  );.SQLITE_API co
232c0 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
232d0 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e  3_column_table_n
232e0 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ame(sqlite3_stmt
232f0 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41  *,int);.SQLITE_A
23300 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
23310 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61  qlite3_column_ta
23320 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74  ble_name16(sqlit
23330 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53  e3_stmt*,int);.S
23340 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
23350 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f  char *sqlite3_co
23360 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
23370 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
23380 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  nt);.SQLITE_API 
23390 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
233a0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69  te3_column_origi
233b0 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33  n_name16(sqlite3
233c0 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f 2a  _stmt*,int);../*
233d0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65  .** CAPI3REF: De
233e0 63 6c 61 72 65 64 20 44 61 74 61 74 79 70 65 20  clared Datatype 
233f0 4f 66 20 41 20 51 75 65 72 79 20 52 65 73 75 6c  Of A Query Resul
23400 74 20 7b 46 31 33 37 36 30 7d 0a 2a 2a 0a 2a 2a  t {F13760}.**.**
23410 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d   The first param
23420 65 74 65 72 20 69 73 20 61 20 5b 70 72 65 70 61  eter is a [prepa
23430 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20  red statement]. 
23440 0a 2a 2a 20 49 66 20 74 68 69 73 20 73 74 61 74  .** If this stat
23450 65 6d 65 6e 74 20 69 73 20 61 20 53 45 4c 45 43  ement is a SELEC
23460 54 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20  T statement and 
23470 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f  the Nth column o
23480 66 20 74 68 65 20 0a 2a 2a 20 72 65 74 75 72 6e  f the .** return
23490 65 64 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  ed result set of
234a0 20 74 68 61 74 20 53 45 4c 45 43 54 20 69 73 20   that SELECT is 
234b0 61 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 28  a table column (
234c0 6e 6f 74 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73  not an.** expres
234d0 73 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 79  sion or subquery
234e0 29 20 74 68 65 6e 20 74 68 65 20 64 65 63 6c 61  ) then the decla
234f0 72 65 64 20 74 79 70 65 20 6f 66 20 74 68 65 20  red type of the 
23500 74 61 62 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20  table.** column 
23510 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66  is returned.  If
23520 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20   the Nth column 
23530 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  of the result se
23540 74 20 69 73 20 61 6e 0a 2a 2a 20 65 78 70 72 65  t is an.** expre
23550 73 73 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72  ssion or subquer
23560 79 2c 20 74 68 65 6e 20 61 20 4e 55 4c 4c 20 70  y, then a NULL p
23570 6f 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e  ointer is return
23580 65 64 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  ed..** The retur
23590 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 61 6c  ned string is al
235a0 77 61 79 73 20 55 54 46 2d 38 20 65 6e 63 6f 64  ways UTF-8 encod
235b0 65 64 2e 20 20 7b 45 4e 44 7d 20 0a 2a 2a 20 46  ed.  {END} .** F
235c0 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 6e 20 74  or example, in t
235d0 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
235e0 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45  ma:.**.** CREATE
235f0 20 54 41 42 4c 45 20 74 31 28 63 31 20 56 41 52   TABLE t1(c1 VAR
23600 49 41 4e 54 29 3b 0a 2a 2a 0a 2a 2a 20 41 6e 64  IANT);.**.** And
23610 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
23620 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 65  tatement compile
23630 64 3a 0a 2a 2a 0a 2a 2a 20 53 45 4c 45 43 54 20  d:.**.** SELECT 
23640 63 31 20 2b 20 31 2c 20 63 31 20 46 52 4f 4d 20  c1 + 1, c1 FROM 
23650 74 31 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 74  t1;.**.** Then t
23660 68 69 73 20 72 6f 75 74 69 6e 65 20 77 6f 75 6c  his routine woul
23670 64 20 72 65 74 75 72 6e 20 74 68 65 20 73 74 72  d return the str
23680 69 6e 67 20 22 56 41 52 49 41 4e 54 22 20 66 6f  ing "VARIANT" fo
23690 72 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20  r the second.** 
236a0 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 28 69  result column (i
236b0 3d 3d 31 29 2c 20 61 6e 64 20 61 20 4e 55 4c 4c  ==1), and a NULL
236c0 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 74 68 65   pointer for the
236d0 20 66 69 72 73 74 20 72 65 73 75 6c 74 20 63 6f   first result co
236e0 6c 75 6d 6e 0a 2a 2a 20 28 69 3d 3d 30 29 2e 0a  lumn.** (i==0)..
236f0 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 65  **.** SQLite use
23700 73 20 64 79 6e 61 6d 69 63 20 72 75 6e 2d 74 69  s dynamic run-ti
23710 6d 65 20 74 79 70 69 6e 67 2e 20 20 53 6f 20 6a  me typing.  So j
23720 75 73 74 20 62 65 63 61 75 73 65 20 61 20 63 6f  ust because a co
23730 6c 75 6d 6e 0a 2a 2a 20 69 73 20 64 65 63 6c 61  lumn.** is decla
23740 72 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61  red to contain a
23750 20 70 61 72 74 69 63 75 6c 61 72 20 74 79 70 65   particular type
23760 20 64 6f 65 73 20 6e 6f 74 20 6d 65 61 6e 20 74   does not mean t
23770 68 61 74 20 74 68 65 0a 2a 2a 20 64 61 74 61 20  hat the.** data 
23780 73 74 6f 72 65 64 20 69 6e 20 74 68 61 74 20 63  stored in that c
23790 6f 6c 75 6d 6e 20 69 73 20 6f 66 20 74 68 65 20  olumn is of the 
237a0 64 65 63 6c 61 72 65 64 20 74 79 70 65 2e 20 20  declared type.  
237b0 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20 73 74 72  SQLite is.** str
237c0 6f 6e 67 6c 79 20 74 79 70 65 64 2c 20 62 75 74  ongly typed, but
237d0 20 74 68 65 20 74 79 70 69 6e 67 20 69 73 20 64   the typing is d
237e0 79 6e 61 6d 69 63 20 6e 6f 74 20 73 74 61 74 69  ynamic not stati
237f0 63 2e 20 20 54 79 70 65 0a 2a 2a 20 69 73 20 61  c.  Type.** is a
23800 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 69  ssociated with i
23810 6e 64 69 76 69 64 75 61 6c 20 76 61 6c 75 65 73  ndividual values
23820 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 65 20 63  , not with the c
23830 6f 6e 74 61 69 6e 65 72 73 0a 2a 2a 20 75 73 65  ontainers.** use
23840 64 20 74 6f 20 68 6f 6c 64 20 74 68 6f 73 65 20  d to hold those 
23850 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e  values..**.** IN
23860 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20  VARIANTS:.**.** 
23870 7b 46 31 33 37 36 31 7d 20 20 41 20 73 75 63 63  {F13761}  A succ
23880 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b  essful call to [
23890 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
238a0 65 63 6c 74 79 70 65 28 53 2c 4e 29 5d 0a 2a 2a  ecltype(S,N)].**
238b0 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72             retur
238c0 6e 73 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e  ns a zero-termin
238d0 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e  ated UTF-8 strin
238e0 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  g containing the
238f0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 68  .**           th
23900 65 20 64 65 63 6c 61 72 65 64 20 64 61 74 61 74  e declared datat
23910 79 70 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ype of the table
23920 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 61 70 70   column that app
23930 65 61 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  ears.**         
23940 20 20 61 73 20 74 68 65 20 4e 74 68 20 63 6f 6c    as the Nth col
23950 75 6d 6e 20 28 6e 75 6d 62 65 72 65 64 20 66 72  umn (numbered fr
23960 6f 6d 20 30 29 20 6f 66 20 74 68 65 20 72 65 73  om 0) of the res
23970 75 6c 74 20 73 65 74 20 74 6f 20 74 68 65 0a 2a  ult set to the.*
23980 2a 20 20 20 20 20 20 20 20 20 20 20 5b 70 72 65  *           [pre
23990 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
239a0 20 53 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 33 37 36   S..**.** {F1376
239b0 32 7d 20 20 41 20 73 75 63 63 65 73 73 66 75 6c  2}  A successful
239c0 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
239d0 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70  3_column_decltyp
239e0 65 31 36 28 53 2c 4e 29 5d 0a 2a 2a 20 20 20 20  e16(S,N)].**    
239f0 20 20 20 20 20 20 20 72 65 74 75 72 6e 73 20 61         returns a
23a00 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
23a10 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62   UTF-16 native b
23a20 79 74 65 20 6f 72 64 65 72 20 73 74 72 69 6e 67  yte order string
23a30 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 63 6f  .**           co
23a40 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 64 65 63  ntaining the dec
23a50 6c 61 72 65 64 20 64 61 74 61 74 79 70 65 20 6f  lared datatype o
23a60 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75  f the table colu
23a70 6d 6e 20 74 68 61 74 20 61 70 70 65 61 72 73 0a  mn that appears.
23a80 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 73 20  **           as 
23a90 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 28  the Nth column (
23aa0 6e 75 6d 62 65 72 65 64 20 66 72 6f 6d 20 30 29  numbered from 0)
23ab0 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73   of the result s
23ac0 65 74 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20  et to the.**    
23ad0 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 64         [prepared
23ae0 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a   statement] S..*
23af0 2a 0a 2a 2a 20 7b 46 31 33 37 36 33 7d 20 20 49  *.** {F13763}  I
23b00 66 20 4e 20 69 73 20 6c 65 73 73 20 74 68 61 6e  f N is less than
23b10 20 30 20 6f 72 20 4e 20 69 73 20 67 72 65 61 74   0 or N is great
23b20 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  er than or equal
23b30 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   to.**          
23b40 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
23b50 6f 6c 75 6d 6e 73 20 69 6e 20 5b 70 72 65 70 61  olumns in [prepa
23b60 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53  red statement] S
23b70 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6f 72  .**           or
23b80 20 69 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75   if the Nth colu
23b90 6d 6e 20 6f 66 20 53 20 69 73 20 61 6e 20 65 78  mn of S is an ex
23ba0 70 72 65 73 73 69 6f 6e 20 6f 72 20 73 75 62 71  pression or subq
23bb0 75 65 72 79 20 72 61 74 68 65 72 0a 2a 2a 20 20  uery rather.**  
23bc0 20 20 20 20 20 20 20 20 20 74 68 61 6e 20 61 20           than a 
23bd0 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 6f 72 20  table column or 
23be0 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  if a memory allo
23bf0 63 61 74 69 6f 6e 20 66 61 69 6c 75 72 65 0a 2a  cation failure.*
23c00 2a 20 20 20 20 20 20 20 20 20 20 20 6f 63 63 75  *           occu
23c10 72 73 20 64 75 72 69 6e 67 20 65 6e 63 6f 64 69  rs during encodi
23c20 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 73 2c 20  ng conversions, 
23c30 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  then.**         
23c40 20 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69    calls to [sqli
23c50 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74  te3_column_declt
23c60 79 70 65 28 53 2c 4e 29 5d 20 6f 72 0a 2a 2a 20  ype(S,N)] or.** 
23c70 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
23c80 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79  e3_column_declty
23c90 70 65 31 36 28 53 2c 4e 29 5d 20 72 65 74 75 72  pe16(S,N)] retur
23ca0 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54  n NULL..*/.SQLIT
23cb0 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72  E_API const char
23cc0 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
23cd0 5f 64 65 63 6c 74 79 70 65 28 73 71 6c 69 74 65  _decltype(sqlite
23ce0 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51  3_stmt*,int);.SQ
23cf0 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
23d00 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  oid *sqlite3_col
23d10 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 28 73  umn_decltype16(s
23d20 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
23d30 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 43 41 50 49 33  );../* .** CAPI3
23d40 52 45 46 3a 20 20 45 76 61 6c 75 61 74 65 20 41  REF:  Evaluate A
23d50 6e 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20  n SQL Statement 
23d60 7b 46 31 33 32 30 30 7d 0a 2a 2a 0a 2a 2a 20 41  {F13200}.**.** A
23d70 66 74 65 72 20 61 6e 20 5b 70 72 65 70 61 72 65  fter an [prepare
23d80 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 68 61 73  d statement] has
23d90 20 62 65 65 6e 20 70 72 65 70 61 72 65 64 20 77   been prepared w
23da0 69 74 68 20 61 20 63 61 6c 6c 0a 2a 2a 20 74 6f  ith a call.** to
23db0 20 65 69 74 68 65 72 20 5b 73 71 6c 69 74 65 33   either [sqlite3
23dc0 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f  _prepare_v2()] o
23dd0 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  r [sqlite3_prepa
23de0 72 65 31 36 5f 76 32 28 29 5d 20 6f 72 20 74 6f  re16_v2()] or to
23df0 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6c   one of.** the l
23e00 65 67 61 63 79 20 69 6e 74 65 72 66 61 63 65 73  egacy interfaces
23e10 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
23e20 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  e()] or [sqlite3
23e30 5f 70 72 65 70 61 72 65 31 36 28 29 5d 2c 0a 2a  _prepare16()],.*
23e40 2a 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63  * then this func
23e50 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 63 61 6c  tion must be cal
23e60 6c 65 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  led one or more 
23e70 74 69 6d 65 73 20 74 6f 20 65 76 61 6c 75 61 74  times to evaluat
23e80 65 20 74 68 65 20 0a 2a 2a 20 73 74 61 74 65 6d  e the .** statem
23e90 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  ent..**.** The d
23ea0 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20 62 65  etails of the be
23eb0 68 61 76 69 6f 72 20 6f 66 20 74 68 69 73 20 73  havior of this s
23ec0 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 69 6e  qlite3_step() in
23ed0 74 65 72 66 61 63 65 20 64 65 70 65 6e 64 0a 2a  terface depend.*
23ee0 2a 20 6f 6e 20 77 68 65 74 68 65 72 20 74 68 65  * on whether the
23ef0 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 70   statement was p
23f00 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 74 68  repared using th
23f10 65 20 6e 65 77 65 72 20 22 76 32 22 20 69 6e 74  e newer "v2" int
23f20 65 72 66 61 63 65 0a 2a 2a 20 5b 73 71 6c 69 74  erface.** [sqlit
23f30 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
23f40 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72   and [sqlite3_pr
23f50 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20 6f 72  epare16_v2()] or
23f60 20 74 68 65 20 6f 6c 64 65 72 20 6c 65 67 61 63   the older legac
23f70 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 5b  y.** interface [
23f80 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
23f90 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
23fa0 70 72 65 70 61 72 65 31 36 28 29 5d 2e 20 20 54  prepare16()].  T
23fb0 68 65 20 75 73 65 20 6f 66 20 74 68 65 0a 2a 2a  he use of the.**
23fc0 20 6e 65 77 20 22 76 32 22 20 69 6e 74 65 72 66   new "v2" interf
23fd0 61 63 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64  ace is recommend
23fe0 65 64 20 66 6f 72 20 6e 65 77 20 61 70 70 6c 69  ed for new appli
23ff0 63 61 74 69 6f 6e 73 20 62 75 74 20 74 68 65 20  cations but the 
24000 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66  legacy.** interf
24010 61 63 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75  ace will continu
24020 65 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74 65  e to be supporte
24030 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20  d..**.** In the 
24040 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61 63 65  legacy interface
24050 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  , the return val
24060 75 65 20 77 69 6c 6c 20 62 65 20 65 69 74 68 65  ue will be eithe
24070 72 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c  r [SQLITE_BUSY],
24080 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e   .** [SQLITE_DON
24090 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d  E], [SQLITE_ROW]
240a0 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d  , [SQLITE_ERROR]
240b0 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4d 49 53  , or [SQLITE_MIS
240c0 55 53 45 5d 2e 0a 2a 2a 20 57 69 74 68 20 74 68  USE]..** With th
240d0 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65  e "v2" interface
240e0 2c 20 61 6e 79 20 6f 66 20 74 68 65 20 6f 74 68  , any of the oth
240f0 65 72 20 5b 53 51 4c 49 54 45 5f 4f 4b 20 7c 20  er [SQLITE_OK | 
24100 72 65 73 75 6c 74 20 63 6f 64 65 5d 0a 2a 2a 20  result code].** 
24110 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52  or [SQLITE_IOERR
24120 5f 52 45 41 44 20 7c 20 65 78 74 65 6e 64 65 64  _READ | extended
24130 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 6d 69   result code] mi
24140 67 68 74 20 62 65 20 72 65 74 75 72 6e 65 64 20  ght be returned 
24150 61 73 0a 2a 2a 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a  as.** well..**.*
24160 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20  * [SQLITE_BUSY] 
24170 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 64  means that the d
24180 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 20 77  atabase engine w
24190 61 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 63 71  as unable to acq
241a0 75 69 72 65 20 74 68 65 0a 2a 2a 20 64 61 74 61  uire the.** data
241b0 62 61 73 65 20 6c 6f 63 6b 73 20 69 74 20 6e 65  base locks it ne
241c0 65 64 73 20 74 6f 20 64 6f 20 69 74 73 20 6a 6f  eds to do its jo
241d0 62 2e 20 20 49 66 20 74 68 65 20 73 74 61 74 65  b.  If the state
241e0 6d 65 6e 74 20 69 73 20 61 20 43 4f 4d 4d 49 54  ment is a COMMIT
241f0 0a 2a 2a 20 6f 72 20 6f 63 63 75 72 73 20 6f 75  .** or occurs ou
24200 74 73 69 64 65 20 6f 66 20 61 6e 20 65 78 70 6c  tside of an expl
24210 69 63 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  icit transaction
24220 2c 20 74 68 65 6e 20 79 6f 75 20 63 61 6e 20 72  , then you can r
24230 65 74 72 79 20 74 68 65 0a 2a 2a 20 73 74 61 74  etry the.** stat
24240 65 6d 65 6e 74 2e 20 20 49 66 20 74 68 65 20 73  ement.  If the s
24250 74 61 74 65 6d 65 6e 74 20 69 73 20 6e 6f 74 20  tatement is not 
24260 61 20 43 4f 4d 4d 49 54 20 61 6e 64 20 6f 63 63  a COMMIT and occ
24270 75 72 73 20 77 69 74 68 69 6e 20 61 0a 2a 2a 20  urs within a.** 
24280 65 78 70 6c 69 63 69 74 20 74 72 61 6e 73 61 63  explicit transac
24290 74 69 6f 6e 20 74 68 65 6e 20 79 6f 75 20 73 68  tion then you sh
242a0 6f 75 6c 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68  ould rollback th
242b0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65  e transaction be
242c0 66 6f 72 65 0a 2a 2a 20 63 6f 6e 74 69 6e 75 69  fore.** continui
242d0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54  ng..**.** [SQLIT
242e0 45 5f 44 4f 4e 45 5d 20 6d 65 61 6e 73 20 74 68  E_DONE] means th
242f0 61 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  at the statement
24300 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 65 78   has finished ex
24310 65 63 75 74 69 6e 67 0a 2a 2a 20 73 75 63 63 65  ecuting.** succe
24320 73 73 66 75 6c 6c 79 2e 20 20 73 71 6c 69 74 65  ssfully.  sqlite
24330 33 5f 73 74 65 70 28 29 20 73 68 6f 75 6c 64 20  3_step() should 
24340 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 61 67  not be called ag
24350 61 69 6e 20 6f 6e 20 74 68 69 73 20 76 69 72 74  ain on this virt
24360 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 20 77  ual.** machine w
24370 69 74 68 6f 75 74 20 66 69 72 73 74 20 63 61 6c  ithout first cal
24380 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 72 65  ling [sqlite3_re
24390 73 65 74 28 29 5d 20 74 6f 20 72 65 73 65 74 20  set()] to reset 
243a0 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 6d  the virtual.** m
243b0 61 63 68 69 6e 65 20 62 61 63 6b 20 74 6f 20 69  achine back to i
243c0 74 73 20 69 6e 69 74 69 61 6c 20 73 74 61 74 65  ts initial state
243d0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53  ..**.** If the S
243e0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69  QL statement bei
243f0 6e 67 20 65 78 65 63 75 74 65 64 20 72 65 74 75  ng executed retu
24400 72 6e 73 20 61 6e 79 20 64 61 74 61 2c 20 74 68  rns any data, th
24410 65 6e 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 52  en .** [SQLITE_R
24420 4f 57 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20  OW] is returned 
24430 65 61 63 68 20 74 69 6d 65 20 61 20 6e 65 77 20  each time a new 
24440 72 6f 77 20 6f 66 20 64 61 74 61 20 69 73 20 72  row of data is r
24450 65 61 64 79 0a 2a 2a 20 66 6f 72 20 70 72 6f 63  eady.** for proc
24460 65 73 73 69 6e 67 20 62 79 20 74 68 65 20 63 61  essing by the ca
24470 6c 6c 65 72 2e 20 54 68 65 20 76 61 6c 75 65 73  ller. The values
24480 20 6d 61 79 20 62 65 20 61 63 63 65 73 73 65 64   may be accessed
24490 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 5b 73   using.** the [s
244a0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
244b0 74 20 7c 20 63 6f 6c 75 6d 6e 20 61 63 63 65 73  t | column acces
244c0 73 20 66 75 6e 63 74 69 6f 6e 73 5d 2e 0a 2a 2a  s functions]..**
244d0 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20   sqlite3_step() 
244e0 69 73 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 20  is called again 
244f0 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 20  to retrieve the 
24500 6e 65 78 74 20 72 6f 77 20 6f 66 20 64 61 74 61  next row of data
24510 2e 0a 2a 2a 20 0a 2a 2a 20 5b 53 51 4c 49 54 45  ..** .** [SQLITE
24520 5f 45 52 52 4f 52 5d 20 6d 65 61 6e 73 20 74 68  _ERROR] means th
24530 61 74 20 61 20 72 75 6e 2d 74 69 6d 65 20 65 72  at a run-time er
24540 72 6f 72 20 28 73 75 63 68 20 61 73 20 61 20 63  ror (such as a c
24550 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 76 69 6f  onstraint.** vio
24560 6c 61 74 69 6f 6e 29 20 68 61 73 20 6f 63 63 75  lation) has occu
24570 72 72 65 64 2e 20 20 73 71 6c 69 74 65 33 5f 73  rred.  sqlite3_s
24580 74 65 70 28 29 20 73 68 6f 75 6c 64 20 6e 6f 74  tep() should not
24590 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61 69 6e   be called again
245a0 20 6f 6e 0a 2a 2a 20 74 68 65 20 56 4d 2e 20 4d   on.** the VM. M
245b0 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ore information 
245c0 6d 61 79 20 62 65 20 66 6f 75 6e 64 20 62 79 20  may be found by 
245d0 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33  calling [sqlite3
245e0 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a 2a 20 57  _errmsg()]..** W
245f0 69 74 68 20 74 68 65 20 6c 65 67 61 63 79 20 69  ith the legacy i
24600 6e 74 65 72 66 61 63 65 2c 20 61 20 6d 6f 72 65  nterface, a more
24610 20 73 70 65 63 69 66 69 63 20 65 72 72 6f 72 20   specific error 
24620 63 6f 64 65 20 28 65 78 61 6d 70 6c 65 3a 0a 2a  code (example:.*
24630 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52 52  * [SQLITE_INTERR
24640 55 50 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 53 43  UPT], [SQLITE_SC
24650 48 45 4d 41 5d 2c 20 5b 53 51 4c 49 54 45 5f 43  HEMA], [SQLITE_C
24660 4f 52 52 55 50 54 5d 2c 20 61 6e 64 20 73 6f 20  ORRUPT], and so 
24670 66 6f 72 74 68 29 0a 2a 2a 20 63 61 6e 20 62 65  forth).** can be
24680 20 6f 62 74 61 69 6e 65 64 20 62 79 20 63 61 6c   obtained by cal
24690 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 72 65  ling [sqlite3_re
246a0 73 65 74 28 29 5d 20 6f 6e 20 74 68 65 0a 2a 2a  set()] on the.**
246b0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
246c0 6d 65 6e 74 5d 2e 20 20 49 6e 20 74 68 65 20 22  ment].  In the "
246d0 76 32 22 20 69 6e 74 65 72 66 61 63 65 2c 0a 2a  v2" interface,.*
246e0 2a 20 74 68 65 20 6d 6f 72 65 20 73 70 65 63 69  * the more speci
246f0 66 69 63 20 65 72 72 6f 72 20 63 6f 64 65 20 69  fic error code i
24700 73 20 72 65 74 75 72 6e 65 64 20 64 69 72 65 63  s returned direc
24710 74 6c 79 20 62 79 20 73 71 6c 69 74 65 33 5f 73  tly by sqlite3_s
24720 74 65 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51  tep()..**.** [SQ
24730 4c 49 54 45 5f 4d 49 53 55 53 45 5d 20 6d 65 61  LITE_MISUSE] mea
24740 6e 73 20 74 68 61 74 20 74 68 65 20 74 68 69 73  ns that the this
24750 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c   routine was cal
24760 6c 65 64 20 69 6e 61 70 70 72 6f 70 72 69 61 74  led inappropriat
24770 65 6c 79 2e 0a 2a 2a 20 50 65 72 68 61 70 73 20  ely..** Perhaps 
24780 69 74 20 77 61 73 20 63 61 6c 6c 65 64 20 6f 6e  it was called on
24790 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61   a [prepared sta
247a0 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 68 61 73  tement] that has
247b0 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e  .** already been
247c0 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
247d0 7a 65 20 7c 20 66 69 6e 61 6c 69 7a 65 64 5d 20  ze | finalized] 
247e0 6f 72 20 6f 6e 20 6f 6e 65 20 74 68 61 74 20 68  or on one that h
247f0 61 64 20 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c  ad .** previousl
24800 79 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49  y returned [SQLI
24810 54 45 5f 45 52 52 4f 52 5d 20 6f 72 20 5b 53 51  TE_ERROR] or [SQ
24820 4c 49 54 45 5f 44 4f 4e 45 5d 2e 20 20 4f 72 20  LITE_DONE].  Or 
24830 69 74 20 63 6f 75 6c 64 0a 2a 2a 20 62 65 20 74  it could.** be t
24840 68 65 20 63 61 73 65 20 74 68 61 74 20 74 68 65  he case that the
24850 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63   same database c
24860 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 62 65 69  onnection is bei
24870 6e 67 20 75 73 65 64 20 62 79 20 74 77 6f 20 6f  ng used by two o
24880 72 0a 2a 2a 20 6d 6f 72 65 20 74 68 72 65 61 64  r.** more thread
24890 73 20 61 74 20 74 68 65 20 73 61 6d 65 20 6d 6f  s at the same mo
248a0 6d 65 6e 74 20 69 6e 20 74 69 6d 65 2e 0a 2a 2a  ment in time..**
248b0 0a 2a 2a 20 3c 62 3e 47 6f 6f 66 79 20 49 6e 74  .** <b>Goofy Int
248c0 65 72 66 61 63 65 20 41 6c 65 72 74 3a 3c 2f 62  erface Alert:</b
248d0 3e 0a 2a 2a 20 49 6e 20 74 68 65 20 6c 65 67 61  >.** In the lega
248e0 63 79 20 69 6e 74 65 72 66 61 63 65 2c 20 0a 2a  cy interface, .*
248f0 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74  * the sqlite3_st
24900 65 70 28 29 20 41 50 49 20 61 6c 77 61 79 73 20  ep() API always 
24910 72 65 74 75 72 6e 73 20 61 20 67 65 6e 65 72 69  returns a generi
24920 63 20 65 72 72 6f 72 20 63 6f 64 65 2c 0a 2a 2a  c error code,.**
24930 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 2c   [SQLITE_ERROR],
24940 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e 79 20 65   following any e
24950 72 72 6f 72 20 6f 74 68 65 72 20 74 68 61 6e 20  rror other than 
24960 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 0a 2a 2a  [SQLITE_BUSY].**
24970 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 4d 49 53   and [SQLITE_MIS
24980 55 53 45 5d 2e 20 20 59 6f 75 20 6d 75 73 74 20  USE].  You must 
24990 63 61 6c 6c 20 5b 73 71 6c 69 74 65 33 5f 72 65  call [sqlite3_re
249a0 73 65 74 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71  set()] or.** [sq
249b0 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29  lite3_finalize()
249c0 5d 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 66 69  ] in order to fi
249d0 6e 64 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 70  nd one of the sp
249e0 65 63 69 66 69 63 0a 2a 2a 20 5b 65 72 72 6f 72  ecific.** [error
249f0 20 63 6f 64 65 73 5d 20 74 68 61 74 20 62 65 74   codes] that bet
24a00 74 65 72 20 64 65 73 63 72 69 62 65 73 20 74 68  ter describes th
24a10 65 20 65 72 72 6f 72 2e 0a 2a 2a 20 57 65 20 61  e error..** We a
24a20 64 6d 69 74 20 74 68 61 74 20 74 68 69 73 20 69  dmit that this i
24a30 73 20 61 20 67 6f 6f 66 79 20 64 65 73 69 67 6e  s a goofy design
24a40 2e 20 20 54 68 65 20 70 72 6f 62 6c 65 6d 20 68  .  The problem h
24a50 61 73 20 62 65 65 6e 20 66 69 78 65 64 0a 2a 2a  as been fixed.**
24a60 20 77 69 74 68 20 74 68 65 20 22 76 32 22 20 69   with the "v2" i
24a70 6e 74 65 72 66 61 63 65 2e 20 20 49 66 20 79 6f  nterface.  If yo
24a80 75 20 70 72 65 70 61 72 65 20 61 6c 6c 20 6f 66  u prepare all of
24a90 20 79 6f 75 72 20 53 51 4c 20 73 74 61 74 65 6d   your SQL statem
24aa0 65 6e 74 73 0a 2a 2a 20 75 73 69 6e 67 20 65 69  ents.** using ei
24ab0 74 68 65 72 20 5b 73 71 6c 69 74 65 33 5f 70 72  ther [sqlite3_pr
24ac0 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b  epare_v2()] or [
24ad0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
24ae0 36 5f 76 32 28 29 5d 20 69 6e 73 74 65 61 64 0a  6_v2()] instead.
24af0 2a 2a 20 6f 66 20 74 68 65 20 6c 65 67 61 63 79  ** of the legacy
24b00 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
24b10 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  e()] and [sqlite
24b20 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 2c 20  3_prepare16()], 
24b30 74 68 65 6e 20 74 68 65 20 0a 2a 2a 20 6d 6f 72  then the .** mor
24b40 65 20 73 70 65 63 69 66 69 63 20 5b 65 72 72 6f  e specific [erro
24b50 72 20 63 6f 64 65 73 5d 20 61 72 65 20 72 65 74  r codes] are ret
24b60 75 72 6e 65 64 20 64 69 72 65 63 74 6c 79 0a 2a  urned directly.*
24b70 2a 20 62 79 20 73 71 6c 69 74 65 33 5f 73 74 65  * by sqlite3_ste
24b80 70 28 29 2e 20 20 54 68 65 20 75 73 65 20 6f 66  p().  The use of
24b90 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66   the "v2" interf
24ba0 61 63 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64  ace is recommend
24bb0 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49  ed..**.** INVARI
24bc0 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31 33  ANTS:.**.** {F13
24bd0 32 30 32 7d 20 20 49 66 20 5b 70 72 65 70 61 72  202}  If [prepar
24be0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20  ed statement] S 
24bf0 69 73 20 72 65 61 64 79 20 74 6f 20 62 65 0a 2a  is ready to be.*
24c00 2a 20 20 20 20 20 20 20 20 20 20 20 72 75 6e 2c  *           run,
24c10 20 74 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 73   then [sqlite3_s
24c20 74 65 70 28 53 29 5d 20 61 64 76 61 6e 63 65 73  tep(S)] advances
24c30 20 74 68 61 74 20 70 72 65 70 61 72 65 64 20 73   that prepared s
24c40 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 20  tatement.**     
24c50 20 20 20 20 20 20 75 6e 74 69 6c 20 74 6f 20 63        until to c
24c60 6f 6d 70 6c 65 74 69 6f 6e 20 6f 72 20 75 6e 74  ompletion or unt
24c70 69 6c 20 69 74 20 69 73 20 72 65 61 64 79 20 74  il it is ready t
24c80 6f 20 72 65 74 75 72 6e 20 61 6e 6f 74 68 65 72  o return another
24c90 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 72 6f  .**           ro
24ca0 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20  w of the result 
24cb0 73 65 74 20 6f 72 20 61 6e 20 69 6e 74 65 72 72  set or an interr
24cc0 75 70 74 20 6f 72 20 72 75 6e 2d 74 69 6d 65 20  upt or run-time 
24cd0 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a  error occurs..**
24ce0 0a 2a 2a 20 7b 46 31 35 33 30 34 7d 20 20 57 68  .** {F15304}  Wh
24cf0 65 6e 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71  en a call to [sq
24d00 6c 69 74 65 33 5f 73 74 65 70 28 53 29 5d 20 63  lite3_step(S)] c
24d10 61 75 73 65 73 20 74 68 65 20 0a 2a 2a 20 20 20  auses the .**   
24d20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 65          [prepare
24d30 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 74  d statement] S t
24d40 6f 20 72 75 6e 20 74 6f 20 63 6f 6d 70 6c 65 74  o run to complet
24d50 69 6f 6e 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20  ion,.**         
24d60 20 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72    the function r
24d70 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 44  eturns [SQLITE_D
24d80 4f 4e 45 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 35  ONE]..**.** {F15
24d90 33 30 36 7d 20 20 57 68 65 6e 20 61 20 63 61 6c  306}  When a cal
24da0 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74  l to [sqlite3_st
24db0 65 70 28 53 29 5d 20 73 74 6f 70 73 20 62 65 63  ep(S)] stops bec
24dc0 61 75 73 65 20 69 74 20 69 73 20 72 65 61 64 79  ause it is ready
24dd0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 6f  .**           to
24de0 20 72 65 74 75 72 6e 20 61 6e 6f 74 68 65 72 20   return another 
24df0 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c  row of the resul
24e00 74 20 73 65 74 2c 20 69 74 20 72 65 74 75 72 6e  t set, it return
24e10 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b  s.**           [
24e20 53 51 4c 49 54 45 5f 52 4f 57 5d 2e 0a 2a 2a 0a  SQLITE_ROW]..**.
24e30 2a 2a 20 7b 46 31 35 33 30 38 7d 20 20 49 66 20  ** {F15308}  If 
24e40 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  a call to [sqlit
24e50 65 33 5f 73 74 65 70 28 53 29 5d 20 65 6e 63 6f  e3_step(S)] enco
24e60 75 6e 74 65 72 73 20 61 6e 0a 2a 2a 20 20 20 20  unters an.**    
24e70 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
24e80 69 6e 74 65 72 72 75 70 74 7c 69 6e 74 65 72 72  interrupt|interr
24e90 75 70 74 5d 20 6f 72 20 61 20 72 75 6e 2d 74 69  upt] or a run-ti
24ea0 6d 65 20 65 72 72 6f 72 2c 0a 2a 2a 20 20 20 20  me error,.**    
24eb0 20 20 20 20 20 20 20 69 74 20 72 65 74 75 72 6e         it return
24ec0 73 20 61 6e 20 61 70 70 72 6f 70 72 61 69 74 65  s an appropraite
24ed0 20 65 72 72 6f 72 20 63 6f 64 65 20 74 68 61 74   error code that
24ee0 20 69 73 20 6e 6f 74 20 6f 6e 65 20 6f 66 0a 2a   is not one of.*
24ef0 2a 20 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c  *           [SQL
24f00 49 54 45 5f 4f 4b 5d 2c 20 5b 53 51 4c 49 54 45  ITE_OK], [SQLITE
24f10 5f 52 4f 57 5d 2c 20 6f 72 20 5b 53 51 4c 49 54  _ROW], or [SQLIT
24f20 45 5f 44 4f 4e 45 5d 2e 0a 2a 2a 0a 2a 2a 20 7b  E_DONE]..**.** {
24f30 46 31 35 33 31 30 7d 20 20 49 66 20 61 6e 20 5b  F15310}  If an [
24f40 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
24f50 74 7c 69 6e 74 65 72 72 75 70 74 5d 20 6f 72 20  t|interrupt] or 
24f60 72 75 6e 2d 74 69 6d 65 20 65 72 72 6f 72 0a 2a  run-time error.*
24f70 2a 20 20 20 20 20 20 20 20 20 20 20 6f 63 63 75  *           occu
24f80 72 73 20 64 75 72 69 6e 67 20 61 20 63 61 6c 6c  rs during a call
24f90 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65   to [sqlite3_ste
24fa0 70 28 53 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20  p(S)].**        
24fb0 20 20 20 66 6f 72 20 61 20 5b 70 72 65 70 61 72     for a [prepar
24fc0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20  ed statement] S 
24fd0 63 72 65 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a  created using.**
24fe0 20 20 20 20 20 20 20 20 20 20 20 6c 65 67 61 63             legac
24ff0 79 20 69 6e 74 65 72 66 61 63 65 73 20 5b 73 71  y interfaces [sq
25000 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d  lite3_prepare()]
25010 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   or.**          
25020 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
25030 65 31 36 28 29 5d 20 74 68 65 6e 20 74 68 65 20  e16()] then the 
25040 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
25050 20 65 69 74 68 65 72 0a 2a 2a 20 20 20 20 20 20   either.**      
25060 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 45 52 52       [SQLITE_ERR
25070 4f 52 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 55 53  OR], [SQLITE_BUS
25080 59 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4d  Y], or [SQLITE_M
25090 49 53 55 53 45 5d 2e 0a 2a 2f 0a 53 51 4c 49 54  ISUSE]..*/.SQLIT
250a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
250b0 33 5f 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73  3_step(sqlite3_s
250c0 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  tmt*);../*.** CA
250d0 50 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 6f  PI3REF: Number o
250e0 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 72  f columns in a r
250f0 65 73 75 6c 74 20 73 65 74 20 7b 46 31 33 37 37  esult set {F1377
25100 30 7d 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  0}.**.** Return 
25110 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 61  the number of va
25120 6c 75 65 73 20 69 6e 20 74 68 65 20 63 75 72 72  lues in the curr
25130 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 72  ent row of the r
25140 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2a 0a 2a 2a  esult set..**.**
25150 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a   INVARIANTS:.**.
25160 2a 2a 20 7b 46 31 33 37 37 31 7d 20 20 41 66 74  ** {F13771}  Aft
25170 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71  er a call to [sq
25180 6c 69 74 65 33 5f 73 74 65 70 28 53 29 5d 20 74  lite3_step(S)] t
25190 68 61 74 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20  hat returns.**  
251a0 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45           [SQLITE
251b0 5f 52 4f 57 5d 2c 20 74 68 65 20 5b 73 71 6c 69  _ROW], the [sqli
251c0 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 28 53  te3_data_count(S
251d0 29 5d 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 20 20  )] routine.**   
251e0 20 20 20 20 20 20 20 20 77 69 6c 6c 20 72 65 74          will ret
251f0 75 72 6e 20 74 68 65 20 73 61 6d 65 20 76 61 6c  urn the same val
25200 75 65 20 61 73 20 74 68 65 0a 2a 2a 20 20 20 20  ue as the.**    
25210 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
25220 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 53 29 5d  column_count(S)]
25230 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
25240 20 7b 46 31 33 37 37 32 7d 20 20 41 66 74 65 72   {F13772}  After
25250 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53   [sqlite3_step(S
25260 29 5d 20 68 61 73 20 72 65 74 75 72 6e 65 64 20  )] has returned 
25270 61 6e 79 20 76 61 6c 75 65 20 6f 74 68 65 72 20  any value other 
25280 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  than.**         
25290 20 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 20 6f    [SQLITE_ROW] o
252a0 72 20 62 65 66 6f 72 65 20 5b 73 71 6c 69 74 65  r before [sqlite
252b0 33 5f 73 74 65 70 28 53 29 5d 20 68 61 73 20 62  3_step(S)] has b
252c0 65 65 6e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20  een .**         
252d0 20 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20    called on the 
252e0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
252f0 65 6e 74 5d 20 66 6f 72 0a 2a 2a 20 20 20 20 20  ent] for.**     
25300 20 20 20 20 20 20 74 68 65 20 66 69 72 73 74 20        the first 
25310 74 69 6d 65 20 73 69 6e 63 65 20 69 74 20 77 61  time since it wa
25320 73 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  s [sqlite3_prepa
25330 72 65 7c 70 72 65 70 61 72 65 64 5d 0a 2a 2a 20  re|prepared].** 
25340 20 20 20 20 20 20 20 20 20 20 6f 72 20 5b 73 71            or [sq
25350 6c 69 74 65 33 5f 72 65 73 65 74 7c 72 65 73 65  lite3_reset|rese
25360 74 5d 2c 20 74 68 65 20 5b 73 71 6c 69 74 65 33  t], the [sqlite3
25370 5f 64 61 74 61 5f 63 6f 75 6e 74 28 53 29 5d 0a  _data_count(S)].
25380 2a 2a 20 20 20 20 20 20 20 20 20 20 20 72 6f 75  **           rou
25390 74 69 6e 65 20 72 65 74 75 72 6e 73 20 7a 65 72  tine returns zer
253a0 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  o..*/.SQLITE_API
253b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 61 74   int sqlite3_dat
253c0 61 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f  a_count(sqlite3_
253d0 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f  stmt *pStmt);../
253e0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46  *.** CAPI3REF: F
253f0 75 6e 64 61 6d 65 6e 74 61 6c 20 44 61 74 61 74  undamental Datat
25400 79 70 65 73 20 7b 46 31 30 32 36 35 7d 0a 2a 2a  ypes {F10265}.**
25410 20 4b 45 59 57 4f 52 44 53 3a 20 53 51 4c 49 54   KEYWORDS: SQLIT
25420 45 5f 54 45 58 54 0a 2a 2a 0a 2a 2a 20 7b 46 31  E_TEXT.**.** {F1
25430 30 32 36 36 7d 45 76 65 72 79 20 76 61 6c 75 65  0266}Every value
25440 20 69 6e 20 53 51 4c 69 74 65 20 68 61 73 20 6f   in SQLite has o
25450 6e 65 20 6f 66 20 66 69 76 65 20 66 75 6e 64 61  ne of five funda
25460 6d 65 6e 74 61 6c 20 64 61 74 61 74 79 70 65 73  mental datatypes
25470 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  :.**.** <ul>.** 
25480 3c 6c 69 3e 20 36 34 2d 62 69 74 20 73 69 67 6e  <li> 64-bit sign
25490 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 6c  ed integer.** <l
254a0 69 3e 20 36 34 2d 62 69 74 20 49 45 45 45 20 66  i> 64-bit IEEE f
254b0 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75  loating point nu
254c0 6d 62 65 72 0a 2a 2a 20 3c 6c 69 3e 20 73 74 72  mber.** <li> str
254d0 69 6e 67 0a 2a 2a 20 3c 6c 69 3e 20 42 4c 4f 42  ing.** <li> BLOB
254e0 0a 2a 2a 20 3c 6c 69 3e 20 4e 55 4c 4c 0a 2a 2a  .** <li> NULL.**
254f0 20 3c 2f 75 6c 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a   </ul> {END}.**.
25500 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e  ** These constan
25510 74 73 20 61 72 65 20 63 6f 64 65 73 20 66 6f 72  ts are codes for
25520 20 65 61 63 68 20 6f 66 20 74 68 6f 73 65 20 74   each of those t
25530 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65  ypes..**.** Note
25540 20 74 68 61 74 20 74 68 65 20 53 51 4c 49 54 45   that the SQLITE
25550 5f 54 45 58 54 20 63 6f 6e 73 74 61 6e 74 20 77  _TEXT constant w
25560 61 73 20 61 6c 73 6f 20 75 73 65 64 20 69 6e 20  as also used in 
25570 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 32  SQLite version 2
25580 0a 2a 2a 20 66 6f 72 20 61 20 63 6f 6d 70 6c 65  .** for a comple
25590 74 65 6c 79 20 64 69 66 66 65 72 65 6e 74 20 6d  tely different m
255a0 65 61 6e 69 6e 67 2e 20 20 53 6f 66 74 77 61 72  eaning.  Softwar
255b0 65 20 74 68 61 74 20 6c 69 6e 6b 73 20 61 67 61  e that links aga
255c0 69 6e 73 74 20 62 6f 74 68 0a 2a 2a 20 53 51 4c  inst both.** SQL
255d0 69 74 65 20 76 65 72 73 69 6f 6e 20 32 20 61 6e  ite version 2 an
255e0 64 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e  d SQLite version
255f0 20 33 20 73 68 6f 75 6c 64 20 75 73 65 20 53 51   3 should use SQ
25600 4c 49 54 45 33 5f 54 45 58 54 20 6e 6f 74 0a 2a  LITE3_TEXT not.*
25610 2a 20 53 51 4c 49 54 45 5f 54 45 58 54 2e 0a 2a  * SQLITE_TEXT..*
25620 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
25630 5f 49 4e 54 45 47 45 52 20 20 31 0a 23 64 65 66  _INTEGER  1.#def
25640 69 6e 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54  ine SQLITE_FLOAT
25650 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51      2.#define SQ
25660 4c 49 54 45 5f 42 4c 4f 42 20 20 20 20 20 34 0a  LITE_BLOB     4.
25670 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e  #define SQLITE_N
25680 55 4c 4c 20 20 20 20 20 35 0a 23 69 66 64 65 66  ULL     5.#ifdef
25690 20 53 51 4c 49 54 45 5f 54 45 58 54 0a 23 20 75   SQLITE_TEXT.# u
256a0 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 58 54  ndef SQLITE_TEXT
256b0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
256c0 53 51 4c 49 54 45 5f 54 45 58 54 20 20 20 20 20  SQLITE_TEXT     
256d0 33 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65  3.#endif.#define
256e0 20 53 51 4c 49 54 45 33 5f 54 45 58 54 20 20 20   SQLITE3_TEXT   
256f0 20 20 33 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33    3../*.** CAPI3
25700 52 45 46 3a 20 52 65 73 75 6c 74 73 20 56 61 6c  REF: Results Val
25710 75 65 73 20 46 72 6f 6d 20 41 20 51 75 65 72 79  ues From A Query
25720 20 7b 46 31 33 38 30 30 7d 0a 2a 2a 0a 2a 2a 20   {F13800}.**.** 
25730 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 66  These routines f
25740 6f 72 6d 20 74 68 65 20 22 72 65 73 75 6c 74 20  orm the "result 
25750 73 65 74 20 71 75 65 72 79 22 20 69 6e 74 65 72  set query" inter
25760 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  face..**.** Thes
25770 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72  e routines retur
25780 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  n information ab
25790 6f 75 74 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20  out.** a single 
257a0 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 63 75  column of the cu
257b0 72 72 65 6e 74 20 72 65 73 75 6c 74 20 72 6f 77  rrent result row
257c0 20 6f 66 20 61 20 71 75 65 72 79 2e 20 20 49 6e   of a query.  In
257d0 20 65 76 65 72 79 0a 2a 2a 20 63 61 73 65 20 74   every.** case t
257e0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
257f0 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  t is a pointer t
25800 6f 20 74 68 65 20 0a 2a 2a 20 5b 70 72 65 70 61  o the .** [prepa
25810 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74  red statement] t
25820 68 61 74 20 69 73 20 62 65 69 6e 67 0a 2a 2a 20  hat is being.** 
25830 65 76 61 6c 75 61 74 65 64 20 28 74 68 65 20 5b  evaluated (the [
25840 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 5d 20 74  sqlite3_stmt*] t
25850 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 64  hat was returned
25860 20 66 72 6f 6d 20 0a 2a 2a 20 5b 73 71 6c 69 74   from .** [sqlit
25870 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
25880 20 6f 72 20 6f 6e 65 20 6f 66 20 69 74 73 20 76   or one of its v
25890 61 72 69 61 6e 74 73 29 20 61 6e 64 0a 2a 2a 20  ariants) and.** 
258a0 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
258b0 65 6e 74 20 69 73 20 74 68 65 20 69 6e 64 65 78  ent is the index
258c0 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 66   of the column f
258d0 6f 72 20 77 68 69 63 68 20 69 6e 66 6f 72 6d 61  or which informa
258e0 74 69 6f 6e 20 0a 2a 2a 20 73 68 6f 75 6c 64 20  tion .** should 
258f0 62 65 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68  be returned.  Th
25900 65 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75  e left-most colu
25910 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  mn of the result
25920 20 73 65 74 0a 2a 2a 20 68 61 73 20 61 6e 20 69   set.** has an i
25930 6e 64 65 78 20 6f 66 20 30 2e 0a 2a 2a 0a 2a 2a  ndex of 0..**.**
25940 20 49 66 20 74 68 65 20 53 51 4c 20 73 74 61 74   If the SQL stat
25950 65 6d 65 6e 74 20 69 73 20 6e 6f 74 20 63 75 72  ement is not cur
25960 72 65 6e 74 6c 79 20 70 6f 69 6e 74 20 74 6f 20  rently point to 
25970 61 20 76 61 6c 69 64 20 72 6f 77 2c 20 6f 72 20  a valid row, or 
25980 69 66 20 74 68 65 0a 2a 2a 20 74 68 65 20 63 6f  if the.** the co
25990 6c 75 6d 6e 20 69 6e 64 65 78 20 69 73 20 6f 75  lumn index is ou
259a0 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 68 65 20  t of range, the 
259b0 72 65 73 75 6c 74 20 69 73 20 75 6e 64 65 66 69  result is undefi
259c0 6e 65 64 2e 20 0a 2a 2a 20 54 68 65 73 65 20 72  ned. .** These r
259d0 6f 75 74 69 6e 65 73 20 6d 61 79 20 6f 6e 6c 79  outines may only
259e0 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   be called when 
259f0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
25a00 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69  call to.** [sqli
25a10 74 65 33 5f 73 74 65 70 28 29 5d 20 68 61 73 20  te3_step()] has 
25a20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45  returned [SQLITE
25a30 5f 52 4f 57 5d 20 61 6e 64 20 6e 65 69 74 68 65  _ROW] and neithe
25a40 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65  r.** [sqlite3_re
25a50 73 65 74 28 29 5d 20 6e 6f 72 20 5b 73 71 6c 69  set()] nor [sqli
25a60 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20  te3_finalize()] 
25a70 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 20 73 75  has been call su
25a80 62 73 65 71 75 65 6e 74 6c 79 2e 0a 2a 2a 20 49  bsequently..** I
25a90 66 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 72  f any of these r
25aa0 6f 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c  outines are call
25ab0 65 64 20 61 66 74 65 72 20 5b 73 71 6c 69 74 65  ed after [sqlite
25ac0 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a 2a  3_reset()] or.**
25ad0 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
25ae0 7a 65 28 29 5d 20 6f 72 20 61 66 74 65 72 20 5b  ze()] or after [
25af0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
25b00 68 61 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20  has returned.** 
25b10 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 20  something other 
25b20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 52 4f 57  than [SQLITE_ROW
25b30 5d 2c 20 74 68 65 20 72 65 73 75 6c 74 73 20 61  ], the results a
25b40 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a  re undefined..**
25b50 20 49 66 20 5b 73 71 6c 69 74 65 33 5f 73 74 65   If [sqlite3_ste
25b60 70 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  p()] or [sqlite3
25b70 5f 72 65 73 65 74 28 29 5d 20 6f 72 20 5b 73 71  _reset()] or [sq
25b80 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29  lite3_finalize()
25b90 5d 0a 2a 2a 20 61 72 65 20 63 61 6c 6c 65 64 20  ].** are called 
25ba0 66 72 6f 6d 20 61 20 64 69 66 66 65 72 65 6e 74  from a different
25bb0 20 74 68 72 65 61 64 20 77 68 69 6c 65 20 61 6e   thread while an
25bc0 79 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69  y of these routi
25bd0 6e 65 73 0a 2a 2a 20 61 72 65 20 70 65 6e 64 69  nes.** are pendi
25be0 6e 67 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73  ng, then the res
25bf0 75 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e  ults are undefin
25c00 65 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ed.  .**.** The 
25c10 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
25c20 79 70 65 28 29 20 72 6f 75 74 69 6e 65 20 72 65  ype() routine re
25c30 74 75 72 6e 73 20 0a 2a 2a 20 5b 53 51 4c 49 54  turns .** [SQLIT
25c40 45 5f 49 4e 54 45 47 45 52 20 7c 20 64 61 74 61  E_INTEGER | data
25c50 74 79 70 65 20 63 6f 64 65 5d 20 66 6f 72 20 74  type code] for t
25c60 68 65 20 69 6e 69 74 69 61 6c 20 64 61 74 61 20  he initial data 
25c70 74 79 70 65 0a 2a 2a 20 6f 66 20 74 68 65 20 72  type.** of the r
25c80 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 2e 20 20 54  esult column.  T
25c90 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75  he returned valu
25ca0 65 20 69 73 20 6f 6e 65 20 6f 66 20 5b 53 51 4c  e is one of [SQL
25cb0 49 54 45 5f 49 4e 54 45 47 45 52 5d 2c 0a 2a 2a  ITE_INTEGER],.**
25cc0 20 5b 53 51 4c 49 54 45 5f 46 4c 4f 41 54 5d 2c   [SQLITE_FLOAT],
25cd0 20 5b 53 51 4c 49 54 45 5f 54 45 58 54 5d 2c 20   [SQLITE_TEXT], 
25ce0 5b 53 51 4c 49 54 45 5f 42 4c 4f 42 5d 2c 20 6f  [SQLITE_BLOB], o
25cf0 72 20 5b 53 51 4c 49 54 45 5f 4e 55 4c 4c 5d 2e  r [SQLITE_NULL].
25d00 20 20 54 68 65 20 76 61 6c 75 65 0a 2a 2a 20 72    The value.** r
25d10 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74  eturned by sqlit
25d20 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29  e3_column_type()
25d30 20 69 73 20 6f 6e 6c 79 20 6d 65 61 6e 69 6e 67   is only meaning
25d40 66 75 6c 20 69 66 20 6e 6f 20 74 79 70 65 0a 2a  ful if no type.*
25d50 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 68 61  * conversions ha
25d60 76 65 20 6f 63 63 75 72 72 65 64 20 61 73 20 64  ve occurred as d
25d70 65 73 63 72 69 62 65 64 20 62 65 6c 6f 77 2e 20  escribed below. 
25d80 20 41 66 74 65 72 20 61 20 74 79 70 65 20 63 6f   After a type co
25d90 6e 76 65 72 73 69 6f 6e 2c 0a 2a 2a 20 74 68 65  nversion,.** the
25da0 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
25db0 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  by sqlite3_colum
25dc0 6e 5f 74 79 70 65 28 29 20 69 73 20 75 6e 64 65  n_type() is unde
25dd0 66 69 6e 65 64 2e 20 20 46 75 74 75 72 65 0a 2a  fined.  Future.*
25de0 2a 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  * versions of SQ
25df0 4c 69 74 65 20 6d 61 79 20 63 68 61 6e 67 65 20  Lite may change 
25e00 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20  the behavior of 
25e10 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
25e20 79 70 65 28 29 0a 2a 2a 20 66 6f 6c 6c 6f 77 69  ype().** followi
25e30 6e 67 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72  ng a type conver
25e40 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  sion..**.** If t
25e50 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 42  he result is a B
25e60 4c 4f 42 20 6f 72 20 55 54 46 2d 38 20 73 74 72  LOB or UTF-8 str
25e70 69 6e 67 20 74 68 65 6e 20 74 68 65 20 73 71 6c  ing then the sql
25e80 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
25e90 73 28 29 20 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  s() .** routine 
25ea0 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  returns the numb
25eb0 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74  er of bytes in t
25ec0 68 61 74 20 42 4c 4f 42 20 6f 72 20 73 74 72 69  hat BLOB or stri
25ed0 6e 67 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65  ng..** If the re
25ee0 73 75 6c 74 20 69 73 20 61 20 55 54 46 2d 31 36  sult is a UTF-16
25ef0 20 73 74 72 69 6e 67 2c 20 74 68 65 6e 20 73 71   string, then sq
25f00 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
25f10 65 73 28 29 20 63 6f 6e 76 65 72 74 73 0a 2a 2a  es() converts.**
25f20 20 74 68 65 20 73 74 72 69 6e 67 20 74 6f 20 55   the string to U
25f30 54 46 2d 38 20 61 6e 64 20 74 68 65 6e 20 72 65  TF-8 and then re
25f40 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
25f50 20 6f 66 20 62 79 74 65 73 2e 0a 2a 2a 20 49 66   of bytes..** If
25f60 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61   the result is a
25f70 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20 74   numeric value t
25f80 68 65 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  hen sqlite3_colu
25f90 6d 6e 5f 62 79 74 65 73 28 29 20 75 73 65 73 0a  mn_bytes() uses.
25fa0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72  ** [sqlite3_snpr
25fb0 69 6e 74 66 28 29 5d 20 74 6f 20 63 6f 6e 76 65  intf()] to conve
25fc0 72 74 20 74 68 61 74 20 76 61 6c 75 65 20 74 6f  rt that value to
25fd0 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20   a UTF-8 string 
25fe0 61 6e 64 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74  and returns.** t
25ff0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
26000 65 73 20 69 6e 20 74 68 61 74 20 73 74 72 69 6e  es in that strin
26010 67 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20  g..** The value 
26020 72 65 74 75 72 6e 65 64 20 64 6f 65 73 20 6e 6f  returned does no
26030 74 20 69 6e 63 6c 75 64 65 20 74 68 65 20 7a 65  t include the ze
26040 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 61 74  ro terminator at
26050 20 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20 74   the end.** of t
26060 68 65 20 73 74 72 69 6e 67 2e 20 20 46 6f 72 20  he string.  For 
26070 63 6c 61 72 69 74 79 3a 20 74 68 65 20 76 61 6c  clarity: the val
26080 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74  ue returned is t
26090 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20  he number of.** 
260a0 62 79 74 65 73 20 69 6e 20 74 68 65 20 73 74 72  bytes in the str
260b0 69 6e 67 2c 20 6e 6f 74 20 74 68 65 20 6e 75 6d  ing, not the num
260c0 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72  ber of character
260d0 73 2e 0a 2a 2a 0a 2a 2a 20 53 74 72 69 6e 67 73  s..**.** Strings
260e0 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c   returned by sql
260f0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
26100 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63  () and sqlite3_c
26110 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 2c 0a  olumn_text16(),.
26120 2a 2a 20 65 76 65 6e 20 65 6d 70 74 79 20 73 74  ** even empty st
26130 72 69 6e 67 73 2c 20 61 72 65 20 61 6c 77 61 79  rings, are alway
26140 73 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65  s zero terminate
26150 64 2e 20 20 54 68 65 20 72 65 74 75 72 6e 0a 2a  d.  The return.*
26160 2a 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 71 6c  * value from sql
26170 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  ite3_column_blob
26180 28 29 20 66 6f 72 20 61 20 7a 65 72 6f 2d 6c 65  () for a zero-le
26190 6e 67 74 68 20 62 6c 6f 62 20 69 73 20 61 6e 20  ngth blob is an 
261a0 61 72 62 69 74 72 61 72 79 0a 2a 2a 20 70 6f 69  arbitrary.** poi
261b0 6e 74 65 72 2c 20 70 6f 73 73 69 62 6c 79 20 65  nter, possibly e
261c0 76 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  ven a NULL point
261d0 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  er..**.** The sq
261e0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
261f0 65 73 31 36 28 29 20 72 6f 75 74 69 6e 65 20 69  es16() routine i
26200 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 73 71 6c  s similar to sql
26210 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
26220 73 28 29 0a 2a 2a 20 62 75 74 20 6c 65 61 76 65  s().** but leave
26230 73 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20  s the result in 
26240 55 54 46 2d 31 36 20 69 6e 20 6e 61 74 69 76 65  UTF-16 in native
26250 20 62 79 74 65 20 6f 72 64 65 72 20 69 6e 73 74   byte order inst
26260 65 61 64 20 6f 66 20 55 54 46 2d 38 2e 20 20 0a  ead of UTF-8.  .
26270 2a 2a 20 54 68 65 20 7a 65 72 6f 20 74 65 72 6d  ** The zero term
26280 69 6e 61 74 6f 72 20 69 73 20 6e 6f 74 20 69 6e  inator is not in
26290 63 6c 75 64 65 64 20 69 6e 20 74 68 69 73 20 63  cluded in this c
262a0 6f 75 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ount..**.** The 
262b0 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20  object returned 
262c0 62 79 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75  by [sqlite3_colu
262d0 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73 20 61  mn_value()] is a
262e0 6e 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65 63 74 65  n.** [unprotecte
262f0 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d  d sqlite3_value]
26300 20 6f 62 6a 65 63 74 2e 20 20 41 6e 20 75 6e 70   object.  An unp
26310 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33  rotected sqlite3
26320 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2a  _value object.**
26330 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65   may only be use
26340 64 20 77 69 74 68 20 5b 73 71 6c 69 74 65 33 5f  d with [sqlite3_
26350 62 69 6e 64 5f 76 61 6c 75 65 28 29 5d 20 61 6e  bind_value()] an
26360 64 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  d [sqlite3_resul
26370 74 5f 76 61 6c 75 65 28 29 5d 2e 0a 2a 2a 20 49  t_value()]..** I
26380 66 20 74 68 65 20 5b 75 6e 70 72 6f 74 65 63 74  f the [unprotect
26390 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
263a0 5d 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 65  ] object returne
263b0 64 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  d by.** [sqlite3
263c0 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d  _column_value()]
263d0 20 69 73 20 75 73 65 64 20 69 6e 20 61 6e 79 20   is used in any 
263e0 6f 74 68 65 72 20 77 61 79 2c 20 69 6e 63 6c 75  other way, inclu
263f0 64 69 6e 67 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f  ding calls.** to
26400 20 72 6f 75 74 69 6e 65 73 20 6c 69 6b 65 20 0a   routines like .
26410 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  ** [sqlite3_valu
26420 65 5f 69 6e 74 28 29 5d 2c 20 5b 73 71 6c 69 74  e_int()], [sqlit
26430 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d  e3_value_text()]
26440 2c 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 76 61  , or [sqlite3_va
26450 6c 75 65 5f 62 79 74 65 73 28 29 5d 2c 0a 2a 2a  lue_bytes()],.**
26460 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69   then the behavi
26470 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e  or is undefined.
26480 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
26490 74 69 6e 65 73 20 61 74 74 65 6d 70 74 20 74 6f  tines attempt to
264a0 20 63 6f 6e 76 65 72 74 20 74 68 65 20 76 61 6c   convert the val
264b0 75 65 20 77 68 65 72 65 20 61 70 70 72 6f 70 72  ue where appropr
264c0 69 61 74 65 2e 20 20 46 6f 72 0a 2a 2a 20 65 78  iate.  For.** ex
264d0 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 69 6e  ample, if the in
264e0 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74  ternal represent
264f0 61 74 69 6f 6e 20 69 73 20 46 4c 4f 41 54 20 61  ation is FLOAT a
26500 6e 64 20 61 20 74 65 78 74 20 72 65 73 75 6c 74  nd a text result
26510 0a 2a 2a 20 69 73 20 72 65 71 75 65 73 74 65 64  .** is requested
26520 2c 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  , [sqlite3_snpri
26530 6e 74 66 28 29 5d 20 69 73 20 75 73 65 64 20 69  ntf()] is used i
26540 6e 74 65 72 6e 61 6c 6c 79 20 74 6f 20 64 6f 20  nternally to do 
26550 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 0a 2a  the conversion.*
26560 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e  * automatically.
26570 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
26580 74 61 62 6c 65 20 64 65 74 61 69 6c 73 20 74 68  table details th
26590 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 74 68  e conversions th
265a0 61 74 0a 2a 2a 20 61 72 65 20 61 70 70 6c 69 65  at.** are applie
265b0 64 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71  d:.**.** <blockq
265c0 75 6f 74 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20  uote>.** <table 
265d0 62 6f 72 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c  border="1">.** <
265e0 74 72 3e 3c 74 68 3e 20 49 6e 74 65 72 6e 61 6c  tr><th> Internal
265f0 3c 62 72 3e 54 79 70 65 20 3c 74 68 3e 20 52 65  <br>Type <th> Re
26600 71 75 65 73 74 65 64 3c 62 72 3e 54 79 70 65 20  quested<br>Type 
26610 3c 74 68 3e 20 20 43 6f 6e 76 65 72 73 69 6f 6e  <th>  Conversion
26620 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  .**.** <tr><td> 
26630 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 49 4e   NULL    <td> IN
26640 54 45 47 45 52 20 20 20 3c 74 64 3e 20 52 65 73  TEGER   <td> Res
26650 75 6c 74 20 69 73 20 30 0a 2a 2a 20 3c 74 72 3e  ult is 0.** <tr>
26660 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74  <td>  NULL    <t
26670 64 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 64  d>  FLOAT    <td
26680 3e 20 52 65 73 75 6c 74 20 69 73 20 30 2e 30 0a  > Result is 0.0.
26690 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c  ** <tr><td>  NUL
266a0 4c 20 20 20 20 3c 74 64 3e 20 20 20 54 45 58 54  L    <td>   TEXT
266b0 20 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20      <td> Result 
266c0 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a  is NULL pointer.
266d0 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c  ** <tr><td>  NUL
266e0 4c 20 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42  L    <td>   BLOB
266f0 20 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20      <td> Result 
26700 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a  is NULL pointer.
26710 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45  ** <tr><td> INTE
26720 47 45 52 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54  GER  <td>  FLOAT
26730 20 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74      <td> Convert
26740 20 66 72 6f 6d 20 69 6e 74 65 67 65 72 20 74 6f   from integer to
26750 20 66 6c 6f 61 74 0a 2a 2a 20 3c 74 72 3e 3c 74   float.** <tr><t
26760 64 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e  d> INTEGER  <td>
26770 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20     TEXT    <td> 
26780 41 53 43 49 49 20 72 65 6e 64 65 72 69 6e 67 20  ASCII rendering 
26790 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a  of the integer.*
267a0 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47  * <tr><td> INTEG
267b0 45 52 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20  ER  <td>   BLOB 
267c0 20 20 20 3c 74 64 3e 20 53 61 6d 65 20 61 73 20     <td> Same as 
267d0 66 6f 72 20 49 4e 54 45 47 45 52 2d 3e 54 45 58  for INTEGER->TEX
267e0 54 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 46  T.** <tr><td>  F
267f0 4c 4f 41 54 20 20 20 3c 74 64 3e 20 49 4e 54 45  LOAT   <td> INTE
26800 47 45 52 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65  GER   <td> Conve
26810 72 74 20 66 72 6f 6d 20 66 6c 6f 61 74 20 74 6f  rt from float to
26820 20 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 74 72 3e   integer.** <tr>
26830 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 3c 74  <td>  FLOAT   <t
26840 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64  d>   TEXT    <td
26850 3e 20 41 53 43 49 49 20 72 65 6e 64 65 72 69 6e  > ASCII renderin
26860 67 20 6f 66 20 74 68 65 20 66 6c 6f 61 74 0a 2a  g of the float.*
26870 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41  * <tr><td>  FLOA
26880 54 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20  T   <td>   BLOB 
26890 20 20 20 3c 74 64 3e 20 53 61 6d 65 20 61 73 20     <td> Same as 
268a0 46 4c 4f 41 54 2d 3e 54 45 58 54 0a 2a 2a 20 3c  FLOAT->TEXT.** <
268b0 74 72 3e 3c 74 64 3e 20 20 54 45 58 54 20 20 20  tr><td>  TEXT   
268c0 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20   <td> INTEGER   
268d0 3c 74 64 3e 20 55 73 65 20 61 74 6f 69 28 29 0a  <td> Use atoi().
268e0 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58  ** <tr><td>  TEX
268f0 54 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54  T    <td>  FLOAT
26900 20 20 20 20 3c 74 64 3e 20 55 73 65 20 61 74 6f      <td> Use ato
26910 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  f().** <tr><td> 
26920 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 20 20   TEXT    <td>   
26930 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 4e 6f 20  BLOB    <td> No 
26940 63 68 61 6e 67 65 0a 2a 2a 20 3c 74 72 3e 3c 74  change.** <tr><t
26950 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e  d>  BLOB    <td>
26960 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20   INTEGER   <td> 
26970 43 6f 6e 76 65 72 74 20 74 6f 20 54 45 58 54 20  Convert to TEXT 
26980 74 68 65 6e 20 75 73 65 20 61 74 6f 69 28 29 0a  then use atoi().
26990 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 42 4c 4f  ** <tr><td>  BLO
269a0 42 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54  B    <td>  FLOAT
269b0 20 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74      <td> Convert
269c0 20 74 6f 20 54 45 58 54 20 74 68 65 6e 20 75 73   to TEXT then us
269d0 65 20 61 74 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e  e atof().** <tr>
269e0 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74  <td>  BLOB    <t
269f0 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64  d>   TEXT    <td
26a00 3e 20 41 64 64 20 61 20 7a 65 72 6f 20 74 65 72  > Add a zero ter
26a10 6d 69 6e 61 74 6f 72 20 69 66 20 6e 65 65 64 65  minator if neede
26a20 64 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a  d.** </table>.**
26a30 20 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a   </blockquote>.*
26a40 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 61  *.** The table a
26a50 62 6f 76 65 20 6d 61 6b 65 73 20 72 65 66 65 72  bove makes refer
26a60 65 6e 63 65 20 74 6f 20 73 74 61 6e 64 61 72 64  ence to standard
26a70 20 43 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74   C library funct
26a80 69 6f 6e 73 20 61 74 6f 69 28 29 0a 2a 2a 20 61  ions atoi().** a
26a90 6e 64 20 61 74 6f 66 28 29 2e 20 20 53 51 4c 69  nd atof().  SQLi
26aa0 74 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 61 6c  te does not real
26ab0 6c 79 20 75 73 65 20 74 68 65 73 65 20 66 75 6e  ly use these fun
26ac0 63 74 69 6f 6e 73 2e 20 20 49 74 20 68 61 73 20  ctions.  It has 
26ad0 69 74 73 0a 2a 2a 20 6f 6e 20 65 71 75 61 76 61  its.** on equava
26ae0 6c 65 6e 74 20 69 6e 74 65 72 6e 61 6c 20 72 6f  lent internal ro
26af0 75 74 69 6e 65 73 2e 20 20 54 68 65 20 61 74 6f  utines.  The ato
26b00 69 28 29 20 61 6e 64 20 61 74 6f 66 28 29 20 6e  i() and atof() n
26b10 61 6d 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 64  ames are.** used
26b20 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 66 6f   in the table fo
26b30 72 20 62 72 65 76 69 74 79 20 61 6e 64 20 62 65  r brevity and be
26b40 63 61 75 73 65 20 74 68 65 79 20 61 72 65 20 66  cause they are f
26b50 61 6d 69 6c 69 61 72 20 74 6f 20 6d 6f 73 74 0a  amiliar to most.
26b60 2a 2a 20 43 20 70 72 6f 67 72 61 6d 6d 65 72 73  ** C programmers
26b70 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
26b80 74 20 77 68 65 6e 20 74 79 70 65 20 63 6f 6e 76  t when type conv
26b90 65 72 73 69 6f 6e 73 20 6f 63 63 75 72 2c 20 70  ersions occur, p
26ba0 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 64  ointers returned
26bb0 20 62 79 20 70 72 69 6f 72 0a 2a 2a 20 63 61 6c   by prior.** cal
26bc0 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f  ls to sqlite3_co
26bd0 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 73 71 6c  lumn_blob(), sql
26be0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
26bf0 28 29 2c 20 61 6e 64 2f 6f 72 0a 2a 2a 20 73 71  (), and/or.** sq
26c00 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
26c10 74 31 36 28 29 20 6d 61 79 20 62 65 20 69 6e 76  t16() may be inv
26c20 61 6c 69 64 61 74 65 64 2e 20 0a 2a 2a 20 54 79  alidated. .** Ty
26c30 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61  pe conversions a
26c40 6e 64 20 70 6f 69 6e 74 65 72 20 69 6e 76 61 6c  nd pointer inval
26c50 69 64 61 74 69 6f 6e 73 20 6d 69 67 68 74 20 6f  idations might o
26c60 63 63 75 72 0a 2a 2a 20 69 6e 20 74 68 65 20 66  ccur.** in the f
26c70 6f 6c 6c 6f 77 69 6e 67 20 63 61 73 65 73 3a 0a  ollowing cases:.
26c80 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c  **.** <ul>.** <l
26c90 69 3e 3c 70 3e 20 20 54 68 65 20 69 6e 69 74 69  i><p>  The initi
26ca0 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 61 20  al content is a 
26cb0 42 4c 4f 42 20 61 6e 64 20 73 71 6c 69 74 65 33  BLOB and sqlite3
26cc0 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 0a  _column_text() .
26cd0 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 73  **          or s
26ce0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
26cf0 78 74 31 36 28 29 20 69 73 20 63 61 6c 6c 65 64  xt16() is called
26d00 2e 20 20 41 20 7a 65 72 6f 2d 74 65 72 6d 69 6e  .  A zero-termin
26d10 61 74 6f 72 20 6d 69 67 68 74 0a 2a 2a 20 20 20  ator might.**   
26d20 20 20 20 20 20 20 20 6e 65 65 64 20 74 6f 20 62         need to b
26d30 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 73  e added to the s
26d40 74 72 69 6e 67 2e 3c 2f 70 3e 3c 2f 6c 69 3e 0a  tring.</p></li>.
26d50 2a 2a 0a 2a 2a 20 3c 6c 69 3e 3c 70 3e 20 20 54  **.** <li><p>  T
26d60 68 65 20 69 6e 69 74 69 61 6c 20 63 6f 6e 74 65  he initial conte
26d70 6e 74 20 69 73 20 55 54 46 2d 38 20 74 65 78 74  nt is UTF-8 text
26d80 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c   and sqlite3_col
26d90 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 20 6f 72  umn_bytes16() or
26da0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 71 6c  .**          sql
26db0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
26dc0 31 36 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20  16() is called. 
26dd0 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6d 75 73   The content mus
26de0 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 0a 2a  t be converted.*
26df0 2a 20 20 20 20 20 20 20 20 20 20 74 6f 20 55 54  *          to UT
26e00 46 2d 31 36 2e 3c 2f 70 3e 3c 2f 6c 69 3e 0a 2a  F-16.</p></li>.*
26e10 2a 0a 2a 2a 20 3c 6c 69 3e 3c 70 3e 20 20 54 68  *.** <li><p>  Th
26e20 65 20 69 6e 69 74 69 61 6c 20 63 6f 6e 74 65 6e  e initial conten
26e30 74 20 69 73 20 55 54 46 2d 31 36 20 74 65 78 74  t is UTF-16 text
26e40 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c   and sqlite3_col
26e50 75 6d 6e 5f 62 79 74 65 73 28 29 20 6f 72 0a 2a  umn_bytes() or.*
26e60 2a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  *          sqlit
26e70 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29  e3_column_text()
26e80 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 65   is called.  The
26e90 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62 65   content must be
26ea0 20 63 6f 6e 76 65 72 74 65 64 0a 2a 2a 20 20 20   converted.**   
26eb0 20 20 20 20 20 20 20 74 6f 20 55 54 46 2d 38 2e         to UTF-8.
26ec0 3c 2f 70 3e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75  </p></li>.** </u
26ed0 6c 3e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73  l>.**.** Convers
26ee0 69 6f 6e 73 20 62 65 74 77 65 65 6e 20 55 54 46  ions between UTF
26ef0 2d 31 36 62 65 20 61 6e 64 20 55 54 46 2d 31 36  -16be and UTF-16
26f00 6c 65 20 61 72 65 20 61 6c 77 61 79 73 20 64 6f  le are always do
26f10 6e 65 20 69 6e 20 70 6c 61 63 65 20 61 6e 64 20  ne in place and 
26f20 64 6f 0a 2a 2a 20 6e 6f 74 20 69 6e 76 61 6c 69  do.** not invali
26f30 64 61 74 65 20 61 20 70 72 69 6f 72 20 70 6f 69  date a prior poi
26f40 6e 74 65 72 2c 20 74 68 6f 75 67 68 20 6f 66 20  nter, though of 
26f50 63 6f 75 72 73 65 20 74 68 65 20 63 6f 6e 74 65  course the conte
26f60 6e 74 20 6f 66 20 74 68 65 20 62 75 66 66 65 72  nt of the buffer
26f70 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 70 72 69  .** that the pri
26f80 6f 72 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74  or pointer point
26f90 73 20 74 6f 20 77 69 6c 6c 20 68 61 76 65 20 62  s to will have b
26fa0 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20 20 4f  een modified.  O
26fb0 74 68 65 72 20 6b 69 6e 64 73 0a 2a 2a 20 6f 66  ther kinds.** of
26fc0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 61 72 65 20   conversion are 
26fd0 64 6f 6e 65 20 69 6e 20 70 6c 61 63 65 20 77 68  done in place wh
26fe0 65 6e 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c  en it is possibl
26ff0 65 2c 20 62 75 74 20 73 6f 6d 65 74 69 6d 65 20  e, but sometime 
27000 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 70 6f 73  it is.** not pos
27010 73 69 62 6c 65 20 61 6e 64 20 69 6e 20 74 68 6f  sible and in tho
27020 73 65 20 63 61 73 65 73 20 70 72 69 6f 72 20 70  se cases prior p
27030 6f 69 6e 74 65 72 73 20 61 72 65 20 69 6e 76 61  ointers are inva
27040 6c 69 64 61 74 65 64 2e 20 20 0a 2a 2a 0a 2a 2a  lidated.  .**.**
27050 20 54 68 65 20 73 61 66 65 73 74 20 61 6e 64 20   The safest and 
27060 65 61 73 69 65 73 74 20 74 6f 20 72 65 6d 65 6d  easiest to remem
27070 62 65 72 20 70 6f 6c 69 63 79 20 69 73 20 74 6f  ber policy is to
27080 20 69 6e 76 6f 6b 65 20 74 68 65 73 65 20 72 6f   invoke these ro
27090 75 74 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 6e 65  utines.** in one
270a0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
270b0 67 20 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 20 3c  g ways:.**.**  <
270c0 75 6c 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69  ul>.**  <li>sqli
270d0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28  te3_column_text(
270e0 29 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71  ) followed by sq
270f0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
27100 65 73 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c  es()</li>.**  <l
27110 69 3e 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  i>sqlite3_column
27120 5f 62 6c 6f 62 28 29 20 66 6f 6c 6c 6f 77 65 64  _blob() followed
27130 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   by sqlite3_colu
27140 6d 6e 5f 62 79 74 65 73 28 29 3c 2f 6c 69 3e 0a  mn_bytes()</li>.
27150 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f  **  <li>sqlite3_
27160 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20  column_text16() 
27170 66 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69  followed by sqli
27180 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
27190 31 36 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 2f  16()</li>.**  </
271a0 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68  ul>.**.** In oth
271b0 65 72 20 77 6f 72 64 73 2c 20 79 6f 75 20 73 68  er words, you sh
271c0 6f 75 6c 64 20 63 61 6c 6c 20 73 71 6c 69 74 65  ould call sqlite
271d0 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 2c  3_column_text(),
271e0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
271f0 62 6c 6f 62 28 29 2c 0a 2a 2a 20 6f 72 20 73 71  blob(),.** or sq
27200 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
27210 74 31 36 28 29 20 66 69 72 73 74 20 74 6f 20 66  t16() first to f
27220 6f 72 63 65 20 74 68 65 20 72 65 73 75 6c 74 20  orce the result 
27230 69 6e 74 6f 20 74 68 65 20 64 65 73 69 72 65 64  into the desired
27240 0a 2a 2a 20 66 6f 72 6d 61 74 2c 20 74 68 65 6e  .** format, then
27250 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f   invoke sqlite3_
27260 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 6f  column_bytes() o
27270 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  r sqlite3_column
27280 5f 62 79 74 65 73 31 36 28 29 20 74 6f 0a 2a 2a  _bytes16() to.**
27290 20 66 69 6e 64 20 74 68 65 20 73 69 7a 65 20 6f   find the size o
272a0 66 20 74 68 65 20 72 65 73 75 6c 74 2e 20 20 44  f the result.  D
272b0 6f 20 6e 6f 74 20 6d 69 78 20 63 61 6c 6c 20 74  o not mix call t
272c0 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  o sqlite3_column
272d0 5f 74 65 78 74 28 29 20 6f 72 0a 2a 2a 20 73 71  _text() or.** sq
272e0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
272f0 62 28 29 20 77 69 74 68 20 63 61 6c 6c 73 20 74  b() with calls t
27300 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  o sqlite3_column
27310 5f 62 79 74 65 73 31 36 28 29 2e 20 20 41 6e 64  _bytes16().  And
27320 20 64 6f 20 6e 6f 74 0a 2a 2a 20 6d 69 78 20 63   do not.** mix c
27330 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f  alls to sqlite3_
27340 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20  column_text16() 
27350 77 69 74 68 20 63 61 6c 6c 73 20 74 6f 20 73 71  with calls to sq
27360 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
27370 65 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  es()..**.** The 
27380 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65  pointers returne
27390 64 20 61 72 65 20 76 61 6c 69 64 20 75 6e 74 69  d are valid unti
273a0 6c 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73  l a type convers
273b0 69 6f 6e 20 6f 63 63 75 72 73 20 61 73 0a 2a 2a  ion occurs as.**
273c0 20 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65   described above
273d0 2c 20 6f 72 20 75 6e 74 69 6c 20 5b 73 71 6c 69  , or until [sqli
273e0 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b  te3_step()] or [
273f0 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d  sqlite3_reset()]
27400 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   or.** [sqlite3_
27410 66 69 6e 61 6c 69 7a 65 28 29 5d 20 69 73 20 63  finalize()] is c
27420 61 6c 6c 65 64 2e 20 20 54 68 65 20 6d 65 6d 6f  alled.  The memo
27430 72 79 20 73 70 61 63 65 20 75 73 65 64 20 74 6f  ry space used to
27440 20 68 6f 6c 64 20 73 74 72 69 6e 67 73 0a 2a 2a   hold strings.**
27450 20 61 6e 64 20 62 6c 6f 62 73 20 69 73 20 66 72   and blobs is fr
27460 65 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  eed automaticall
27470 79 2e 20 20 44 6f 20 3c 62 3e 6e 6f 74 3c 2f 62  y.  Do <b>not</b
27480 3e 20 70 61 73 73 20 74 68 65 20 70 6f 69 6e 74  > pass the point
27490 65 72 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20  ers returned.** 
274a0 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  [sqlite3_column_
274b0 62 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65  blob()], [sqlite
274c0 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 5d  3_column_text()]
274d0 2c 20 65 74 63 2e 20 69 6e 74 6f 20 0a 2a 2a 20  , etc. into .** 
274e0 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d  [sqlite3_free()]
274f0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d  ..**.** If a mem
27500 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65  ory allocation e
27510 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69  rror occurs duri
27520 6e 67 20 74 68 65 20 65 76 61 6c 75 61 74 69 6f  ng the evaluatio
27530 6e 20 6f 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74  n of any.** of t
27540 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2c 20 61  hese routines, a
27550 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 69   default value i
27560 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65  s returned.  The
27570 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 0a 2a   default value.*
27580 2a 20 69 73 20 65 69 74 68 65 72 20 74 68 65 20  * is either the 
27590 69 6e 74 65 67 65 72 20 30 2c 20 74 68 65 20 66  integer 0, the f
275a0 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75  loating point nu
275b0 6d 62 65 72 20 30 2e 30 2c 20 6f 72 20 61 20 4e  mber 0.0, or a N
275c0 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20  ULL.** pointer. 
275d0 20 53 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   Subsequent call
275e0 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72  s to [sqlite3_er
275f0 72 63 6f 64 65 28 29 5d 20 77 69 6c 6c 20 72 65  rcode()] will re
27600 74 75 72 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  turn.** [SQLITE_
27610 4e 4f 4d 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e  NOMEM]..**.** IN
27620 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20  VARIANTS:.**.** 
27630 7b 46 31 33 38 30 33 7d 20 54 68 65 20 5b 73 71  {F13803} The [sq
27640 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
27650 62 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63  b(S,N)] interfac
27660 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65 0a 2a  e converts the.*
27670 2a 20 20 20 20 20 20 20 20 20 20 4e 74 68 20 63  *          Nth c
27680 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 63 75 72  olumn in the cur
27690 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20  rent row of the 
276a0 72 65 73 75 6c 74 20 73 65 74 20 66 6f 72 0a 2a  result set for.*
276b0 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70  *          [prep
276c0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
276d0 53 20 69 6e 74 6f 20 61 20 62 6c 6f 62 20 61 6e  S into a blob an
276e0 64 20 74 68 65 6e 20 72 65 74 75 72 6e 73 20 61  d then returns a
276f0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 6f 69  .**          poi
27700 6e 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6e 76  nter to the conv
27710 65 72 74 65 64 20 76 61 6c 75 65 2e 0a 2a 2a 0a  erted value..**.
27720 2a 2a 20 7b 46 31 33 38 30 36 7d 20 54 68 65 20  ** {F13806} The 
27730 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  [sqlite3_column_
27740 62 79 74 65 73 28 53 2c 4e 29 5d 20 69 6e 74 65  bytes(S,N)] inte
27750 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68  rface returns th
27760 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 75  e.**          nu
27770 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
27780 20 74 68 65 20 62 6c 6f 62 20 6f 72 20 73 74 72   the blob or str
27790 69 6e 67 20 28 65 78 63 6c 75 73 69 76 65 20 6f  ing (exclusive o
277a0 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  f the.**        
277b0 20 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f    zero terminato
277c0 72 20 6f 6e 20 74 68 65 20 73 74 72 69 6e 67 29  r on the string)
277d0 20 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e   that was return
277e0 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 20 20 20  ed by the.**    
277f0 20 20 20 20 20 20 6d 6f 73 74 20 72 65 63 65 6e        most recen
27800 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  t call to [sqlit
27810 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 53  e3_column_blob(S
27820 2c 4e 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20  ,N)] or.**      
27830 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c      [sqlite3_col
27840 75 6d 6e 5f 74 65 78 74 28 53 2c 4e 29 5d 2e 0a  umn_text(S,N)]..
27850 2a 2a 0a 2a 2a 20 7b 46 31 33 38 30 39 7d 20 54  **.** {F13809} T
27860 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75  he [sqlite3_colu
27870 6d 6e 5f 62 79 74 65 73 31 36 28 53 2c 4e 29 5d  mn_bytes16(S,N)]
27880 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72   interface retur
27890 6e 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  ns the.**       
278a0 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74     number of byt
278b0 65 73 20 69 6e 20 74 68 65 20 73 74 72 69 6e 67  es in the string
278c0 20 28 65 78 63 6c 75 73 69 76 65 20 6f 66 20 74   (exclusive of t
278d0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 7a  he.**          z
278e0 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 6f  ero terminator o
278f0 6e 20 74 68 65 20 73 74 72 69 6e 67 29 20 74 68  n the string) th
27900 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 64 20  at was returned 
27910 62 79 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  by the.**       
27920 20 20 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63     most recent c
27930 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
27940 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 53 2c  column_text16(S,
27950 4e 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 33 38  N)]..**.** {F138
27960 31 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  12} The [sqlite3
27970 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 53  _column_double(S
27980 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63  ,N)] interface c
27990 6f 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20  onverts the.**  
279a0 20 20 20 20 20 20 20 20 4e 74 68 20 63 6f 6c 75          Nth colu
279b0 6d 6e 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e  mn in the curren
279c0 74 20 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73  t row of the res
279d0 75 6c 74 20 73 65 74 20 66 6f 72 0a 2a 2a 20 20  ult set for.**  
279e0 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 65          [prepare
279f0 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 69  d statement] S i
27a00 6e 74 6f 20 61 20 66 6c 6f 61 74 69 6e 67 20 70  nto a floating p
27a10 6f 69 6e 74 20 76 61 6c 75 65 20 61 6e 64 0a 2a  oint value and.*
27a20 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  *          retur
27a30 6e 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 61  ns a copy of tha
27a40 74 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 7b  t value..**.** {
27a50 46 31 33 38 31 35 7d 20 54 68 65 20 5b 73 71 6c  F13815} The [sql
27a60 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28  ite3_column_int(
27a70 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20  S,N)] interface 
27a80 63 6f 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a 20  converts the.** 
27a90 20 20 20 20 20 20 20 20 20 4e 74 68 20 63 6f 6c           Nth col
27aa0 75 6d 6e 20 69 6e 20 74 68 65 20 63 75 72 72 65  umn in the curre
27ab0 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 72 65  nt row of the re
27ac0 73 75 6c 74 20 73 65 74 20 66 6f 72 0a 2a 2a 20  sult set for.** 
27ad0 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 72           [prepar
27ae0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20  ed statement] S 
27af0 69 6e 74 6f 20 61 20 36 34 2d 62 69 74 20 73 69  into a 64-bit si
27b00 67 6e 65 64 20 69 6e 74 65 67 65 72 20 61 6e 64  gned integer and
27b10 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74  .**          ret
27b20 75 72 6e 73 20 74 68 65 20 6c 6f 77 65 72 20 33  urns the lower 3
27b30 32 20 62 69 74 73 20 6f 66 20 74 68 61 74 20 69  2 bits of that i
27b40 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 46  nteger..**.** {F
27b50 31 33 38 31 38 7d 20 54 68 65 20 5b 73 71 6c 69  13818} The [sqli
27b60 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34  te3_column_int64
27b70 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65  (S,N)] interface
27b80 20 63 6f 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a   converts the.**
27b90 20 20 20 20 20 20 20 20 20 20 4e 74 68 20 63 6f            Nth co
27ba0 6c 75 6d 6e 20 69 6e 20 74 68 65 20 63 75 72 72  lumn in the curr
27bb0 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 72  ent row of the r
27bc0 65 73 75 6c 74 20 73 65 74 20 66 6f 72 0a 2a 2a  esult set for.**
27bd0 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61            [prepa
27be0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53  red statement] S
27bf0 20 69 6e 74 6f 20 61 20 36 34 2d 62 69 74 20 73   into a 64-bit s
27c00 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 61 6e  igned integer an
27c10 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65  d.**          re
27c20 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 20  turns a copy of 
27c30 74 68 61 74 20 69 6e 74 65 67 65 72 2e 0a 2a 2a  that integer..**
27c40 0a 2a 2a 20 7b 46 31 33 38 32 31 7d 20 54 68 65  .** {F13821} The
27c50 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   [sqlite3_column
27c60 5f 74 65 78 74 28 53 2c 4e 29 5d 20 69 6e 74 65  _text(S,N)] inte
27c70 72 66 61 63 65 20 63 6f 6e 76 65 72 74 73 20 74  rface converts t
27c80 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 4e  he.**          N
27c90 74 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65  th column in the
27ca0 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20   current row of 
27cb0 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 66  the result set f
27cc0 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  or.**          [
27cd0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
27ce0 6e 74 5d 20 53 20 69 6e 74 6f 20 61 20 7a 65 72  nt] S into a zer
27cf0 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46  o-terminated UTF
27d00 2d 38 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  -8 .**          
27d10 73 74 72 69 6e 67 20 61 6e 64 20 72 65 74 75 72  string and retur
27d20 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
27d30 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a  that string..**.
27d40 2a 2a 20 7b 46 31 33 38 32 34 7d 20 54 68 65 20  ** {F13824} The 
27d50 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  [sqlite3_column_
27d60 74 65 78 74 31 36 28 53 2c 4e 29 5d 20 69 6e 74  text16(S,N)] int
27d70 65 72 66 61 63 65 20 63 6f 6e 76 65 72 74 73 20  erface converts 
27d80 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
27d90 4e 74 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68  Nth column in th
27da0 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66  e current row of
27db0 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
27dc0 66 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  for.**          
27dd0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
27de0 65 6e 74 5d 20 53 20 69 6e 74 6f 20 61 20 7a 65  ent] S into a ze
27df0 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 32 2d  ro-terminated 2-
27e00 62 79 74 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  byte.**         
27e10 20 61 6c 69 67 6e 65 64 20 55 54 46 2d 31 36 20   aligned UTF-16 
27e20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65  native byte orde
27e30 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74  r.**          st
27e40 72 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73  ring and returns
27e50 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
27e60 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a  at string..**.**
27e70 20 7b 46 31 33 38 32 37 7d 20 54 68 65 20 5b 73   {F13827} The [s
27e80 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
27e90 70 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61  pe(S,N)] interfa
27ea0 63 65 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20  ce returns.**   
27eb0 20 20 20 20 20 20 20 6f 6e 65 20 6f 66 20 5b 53         one of [S
27ec0 51 4c 49 54 45 5f 4e 55 4c 4c 5d 2c 20 5b 53 51  QLITE_NULL], [SQ
27ed0 4c 49 54 45 5f 49 4e 54 45 47 45 52 5d 2c 20 5b  LITE_INTEGER], [
27ee0 53 51 4c 49 54 45 5f 46 4c 4f 41 54 5d 2c 0a 2a  SQLITE_FLOAT],.*
27ef0 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49  *          [SQLI
27f00 54 45 5f 54 45 58 54 5d 2c 20 6f 72 20 5b 53 51  TE_TEXT], or [SQ
27f10 4c 49 54 45 5f 42 4c 4f 42 5d 20 61 73 20 61 70  LITE_BLOB] as ap
27f20 70 72 6f 70 72 69 61 74 65 20 66 6f 72 0a 2a 2a  propriate for.**
27f30 20 20 20 20 20 20 20 20 20 20 74 68 65 20 4e 74            the Nt
27f40 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20  h column in the 
27f50 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74  current row of t
27f60 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 66 6f  he result set fo
27f70 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70  r.**          [p
27f80 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
27f90 74 5d 20 53 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 33  t] S..**.** {F13
27fa0 38 33 30 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  830} The [sqlite
27fb0 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 53  3_column_value(S
27fc0 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72  ,N)] interface r
27fd0 65 74 75 72 6e 73 20 61 0a 2a 2a 20 20 20 20 20  eturns a.**     
27fe0 20 20 20 20 20 70 6f 69 6e 74 65 72 20 74 6f 20       pointer to 
27ff0 61 6e 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20  an [unprotected 
28000 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f  sqlite3_value] o
28010 62 6a 65 63 74 20 66 6f 72 20 74 68 65 0a 2a 2a  bject for the.**
28020 20 20 20 20 20 20 20 20 20 20 4e 74 68 20 63 6f            Nth co
28030 6c 75 6d 6e 20 69 6e 20 74 68 65 20 63 75 72 72  lumn in the curr
28040 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 72  ent row of the r
28050 65 73 75 6c 74 20 73 65 74 20 66 6f 72 0a 2a 2a  esult set for.**
28060 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61            [prepa
28070 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53  red statement] S
28080 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
28090 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
280a0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
280b0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
280c0 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45  nt iCol);.SQLITE
280d0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
280e0 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 71  _column_bytes(sq
280f0 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
28100 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41   iCol);.SQLITE_A
28110 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
28120 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 73 71  olumn_bytes16(sq
28130 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
28140 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41   iCol);.SQLITE_A
28150 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65  PI double sqlite
28160 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28  3_column_double(
28170 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
28180 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45  nt iCol);.SQLITE
28190 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
281a0 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c 69  _column_int(sqli
281b0 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69  te3_stmt*, int i
281c0 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  Col);.SQLITE_API
281d0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73   sqlite3_int64 s
281e0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
281f0 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  t64(sqlite3_stmt
28200 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51  *, int iCol);.SQ
28210 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 75  LITE_API const u
28220 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 71  nsigned char *sq
28230 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
28240 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  t(sqlite3_stmt*,
28250 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49   int iCol);.SQLI
28260 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
28270 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  d *sqlite3_colum
28280 6e 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 33  n_text16(sqlite3
28290 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c  _stmt*, int iCol
282a0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
282b0 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  t sqlite3_column
282c0 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74  _type(sqlite3_st
282d0 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a  mt*, int iCol);.
282e0 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
282f0 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65  e3_value *sqlite
28300 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 73  3_column_value(s
28310 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
28320 74 20 69 43 6f 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20  t iCol);../*.** 
28330 43 41 50 49 33 52 45 46 3a 20 44 65 73 74 72 6f  CAPI3REF: Destro
28340 79 20 41 20 50 72 65 70 61 72 65 64 20 53 74 61  y A Prepared Sta
28350 74 65 6d 65 6e 74 20 4f 62 6a 65 63 74 20 7b 46  tement Object {F
28360 31 33 33 30 30 7d 0a 2a 2a 0a 2a 2a 20 54 68 65  13300}.**.** The
28370 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
28380 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  e() function is 
28390 63 61 6c 6c 65 64 20 74 6f 20 64 65 6c 65 74 65  called to delete
283a0 20 61 20 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64   a .** [prepared
283b0 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 49 66 20   statement]. If 
283c0 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 77 61  the statement wa
283d0 73 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 73 75  s.** executed su
283e0 63 63 65 73 73 66 75 6c 6c 79 2c 20 6f 72 20 6e  ccessfully, or n
283f0 6f 74 20 65 78 65 63 75 74 65 64 20 61 74 20 61  ot executed at a
28400 6c 6c 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f  ll, then SQLITE_
28410 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  OK is returned..
28420 2a 2a 20 49 66 20 65 78 65 63 75 74 69 6f 6e 20  ** If execution 
28430 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  of the statement
28440 20 66 61 69 6c 65 64 20 74 68 65 6e 20 61 6e 20   failed then an 
28450 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d  .** [error code]
28460 20 6f 72 20 5b 65 78 74 65 6e 64 65 64 20 65 72   or [extended er
28470 72 6f 72 20 63 6f 64 65 5d 0a 2a 2a 20 69 73 20  ror code].** is 
28480 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 0a 2a 2a  returned. .**.**
28490 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61   This routine ca
284a0 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61  n be called at a
284b0 6e 79 20 70 6f 69 6e 74 20 64 75 72 69 6e 67 20  ny point during 
284c0 74 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66  the execution of
284d0 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65   the.** [prepare
284e0 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49  d statement].  I
284f0 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61  f the virtual ma
28500 63 68 69 6e 65 20 68 61 73 20 6e 6f 74 20 0a 2a  chine has not .*
28510 2a 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 63  * completed exec
28520 75 74 69 6f 6e 20 77 68 65 6e 20 74 68 69 73 20  ution when this 
28530 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
28540 64 2c 20 74 68 61 74 20 69 73 20 6c 69 6b 65 0a  d, that is like.
28550 2a 2a 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20  ** encountering 
28560 61 6e 20 65 72 72 6f 72 20 6f 72 20 61 6e 20 69  an error or an i
28570 6e 74 65 72 72 75 70 74 2e 20 20 28 53 65 65 20  nterrupt.  (See 
28580 5b 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75  [sqlite3_interru
28590 70 74 28 29 5d 2e 29 20 0a 2a 2a 20 49 6e 63 6f  pt()].) .** Inco
285a0 6d 70 6c 65 74 65 20 75 70 64 61 74 65 73 20 6d  mplete updates m
285b0 61 79 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63  ay be rolled bac
285c0 6b 20 61 6e 64 20 74 72 61 6e 73 61 63 74 69 6f  k and transactio
285d0 6e 73 20 63 61 6e 63 65 6c 6c 65 64 2c 20 20 0a  ns cancelled,  .
285e0 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20  ** depending on 
285f0 74 68 65 20 63 69 72 63 75 6d 73 74 61 6e 63 65  the circumstance
28600 73 2c 20 61 6e 64 20 74 68 65 20 0a 2a 2a 20 5b  s, and the .** [
28610 65 72 72 6f 72 20 63 6f 64 65 5d 20 72 65 74 75  error code] retu
28620 72 6e 65 64 20 77 69 6c 6c 20 62 65 20 5b 53 51  rned will be [SQ
28630 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a  LITE_ABORT]..**.
28640 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a  ** INVARIANTS:.*
28650 2a 0a 2a 2a 20 7b 46 31 31 33 30 32 7d 20 54 68  *.** {F11302} Th
28660 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  e [sqlite3_final
28670 69 7a 65 28 53 29 5d 20 69 6e 74 65 72 66 61 63  ize(S)] interfac
28680 65 20 64 65 73 74 72 6f 79 73 20 74 68 65 0a 2a  e destroys the.*
28690 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70  *          [prep
286a0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
286b0 53 20 61 6e 64 20 72 65 6c 65 61 73 65 73 20 61  S and releases a
286c0 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6d  ll.**          m
286d0 65 6d 6f 72 79 20 61 6e 64 20 66 69 6c 65 20 72  emory and file r
286e0 65 73 6f 75 72 63 65 73 20 68 65 6c 64 20 62 79  esources held by
286f0 20 74 68 61 74 20 6f 62 6a 65 63 74 2e 0a 2a 2a   that object..**
28700 0a 2a 2a 20 7b 46 31 31 33 30 34 7d 20 49 66 20  .** {F11304} If 
28710 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
28720 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
28730 5f 73 74 65 70 28 53 29 5d 20 66 6f 72 20 74 68  _step(S)] for th
28740 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70  e.**          [p
28750 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
28760 74 5d 20 53 20 72 65 74 75 72 6e 65 64 20 61 6e  t] S returned an
28770 20 65 72 72 6f 72 2c 0a 2a 2a 20 20 20 20 20 20   error,.**      
28780 20 20 20 20 74 68 65 6e 20 5b 73 71 6c 69 74 65      then [sqlite
28790 33 5f 66 69 6e 61 6c 69 7a 65 28 53 29 5d 20 72  3_finalize(S)] r
287a0 65 74 75 72 6e 73 20 74 68 61 74 20 73 61 6d 65  eturns that same
287b0 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54   error..*/.SQLIT
287c0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
287d0 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74  3_finalize(sqlit
287e0 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b  e3_stmt *pStmt);
287f0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
28800 3a 20 52 65 73 65 74 20 41 20 50 72 65 70 61 72  : Reset A Prepar
28810 65 64 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a  ed Statement Obj
28820 65 63 74 20 7b 46 31 33 33 33 30 7d 0a 2a 2a 0a  ect {F13330}.**.
28830 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72  ** The sqlite3_r
28840 65 73 65 74 28 29 20 66 75 6e 63 74 69 6f 6e 20  eset() function 
28850 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 73  is called to res
28860 65 74 20 61 20 0a 2a 2a 20 5b 70 72 65 70 61 72  et a .** [prepar
28870 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 6f 62  ed statement] ob
28880 6a 65 63 74 2e 0a 2a 2a 20 62 61 63 6b 20 74 6f  ject..** back to
28890 20 69 74 73 20 69 6e 69 74 69 61 6c 20 73 74 61   its initial sta
288a0 74 65 2c 20 72 65 61 64 79 20 74 6f 20 62 65 20  te, ready to be 
288b0 72 65 2d 65 78 65 63 75 74 65 64 2e 0a 2a 2a 20  re-executed..** 
288c0 41 6e 79 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  Any SQL statemen
288d0 74 20 76 61 72 69 61 62 6c 65 73 20 74 68 61 74  t variables that
288e0 20 68 61 64 20 76 61 6c 75 65 73 20 62 6f 75 6e   had values boun
288f0 64 20 74 6f 20 74 68 65 6d 20 75 73 69 6e 67 0a  d to them using.
28900 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  ** the [sqlite3_
28910 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 73 71 6c 69  bind_blob | sqli
28920 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 41 50 49  te3_bind_*() API
28930 5d 20 72 65 74 61 69 6e 20 74 68 65 69 72 20 76  ] retain their v
28940 61 6c 75 65 73 2e 0a 2a 2a 20 55 73 65 20 5b 73  alues..** Use [s
28950 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e  qlite3_clear_bin
28960 64 69 6e 67 73 28 29 5d 20 74 6f 20 72 65 73 65  dings()] to rese
28970 74 20 74 68 65 20 62 69 6e 64 69 6e 67 73 2e 0a  t the bindings..
28980 2a 2a 0a 2a 2a 20 7b 46 31 31 33 33 32 7d 20 54  **.** {F11332} T
28990 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65  he [sqlite3_rese
289a0 74 28 53 29 5d 20 69 6e 74 65 72 66 61 63 65 20  t(S)] interface 
289b0 72 65 73 65 74 73 20 74 68 65 20 5b 70 72 65 70  resets the [prep
289c0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
289d0 53 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62 61  S.**          ba
289e0 63 6b 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e  ck to the beginn
289f0 69 6e 67 20 6f 66 20 69 74 73 20 70 72 6f 67 72  ing of its progr
28a00 61 6d 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 31 33 33  am..**.** {F1133
28a10 34 7d 20 49 66 20 74 68 65 20 6d 6f 73 74 20 72  4} If the most r
28a20 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 5b 73  ecent call to [s
28a30 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 5d 20  qlite3_step(S)] 
28a40 66 6f 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 20  for .**         
28a50 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
28a60 6d 65 6e 74 5d 20 53 20 72 65 74 75 72 6e 65 64  ment] S returned
28a70 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 20 6f 72   [SQLITE_ROW] or
28a80 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 0a   [SQLITE_DONE],.
28a90 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 69  **          or i
28aa0 66 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  f [sqlite3_step(
28ab0 53 29 5d 20 68 61 73 20 6e 65 76 65 72 20 62 65  S)] has never be
28ac0 66 6f 72 65 20 62 65 65 6e 20 63 61 6c 6c 65 64  fore been called
28ad0 20 6f 6e 20 53 2c 0a 2a 2a 20 20 20 20 20 20 20   on S,.**       
28ae0 20 20 20 74 68 65 6e 20 5b 73 71 6c 69 74 65 33     then [sqlite3
28af0 5f 72 65 73 65 74 28 53 29 5d 20 72 65 74 75 72  _reset(S)] retur
28b00 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a  ns [SQLITE_OK]..
28b10 2a 2a 0a 2a 2a 20 7b 46 31 31 33 33 36 7d 20 49  **.** {F11336} I
28b20 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  f the most recen
28b30 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  t call to [sqlit
28b40 65 33 5f 73 74 65 70 28 53 29 5d 20 66 6f 72 0a  e3_step(S)] for.
28b50 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 65  **          [pre
28b60 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
28b70 20 53 20 69 6e 64 69 63 61 74 65 64 20 61 6e 20   S indicated an 
28b80 65 72 72 6f 72 2c 20 74 68 65 6e 0a 2a 2a 20 20  error, then.**  
28b90 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33          [sqlite3
28ba0 5f 72 65 73 65 74 28 53 29 5d 20 72 65 74 75 72  _reset(S)] retur
28bb0 6e 73 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74  ns an appropriat
28bc0 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a  e [error code]..
28bd0 2a 2a 0a 2a 2a 20 7b 46 31 31 33 33 38 7d 20 54  **.** {F11338} T
28be0 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65  he [sqlite3_rese
28bf0 74 28 53 29 5d 20 69 6e 74 65 72 66 61 63 65 20  t(S)] interface 
28c00 64 6f 65 73 20 6e 6f 74 20 63 68 61 6e 67 65 20  does not change 
28c10 74 68 65 20 76 61 6c 75 65 73 0a 2a 2a 20 20 20  the values.**   
28c20 20 20 20 20 20 20 20 6f 66 20 61 6e 79 20 5b 73         of any [s
28c30 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
28c40 7c 62 69 6e 64 69 6e 67 73 5d 20 6f 6e 20 5b 70  |bindings] on [p
28c50 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
28c60 74 5d 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  t] S..*/.SQLITE_
28c70 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
28c80 72 65 73 65 74 28 73 71 6c 69 74 65 33 5f 73 74  reset(sqlite3_st
28c90 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a  mt *pStmt);../*.
28ca0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 72 65  ** CAPI3REF: Cre
28cb0 61 74 65 20 4f 72 20 52 65 64 65 66 69 6e 65 20  ate Or Redefine 
28cc0 53 51 4c 20 46 75 6e 63 74 69 6f 6e 73 20 7b 46  SQL Functions {F
28cd0 31 36 31 30 30 7d 0a 2a 2a 20 4b 45 59 57 4f 52  16100}.** KEYWOR
28ce0 44 53 3a 20 7b 66 75 6e 63 74 69 6f 6e 20 63 72  DS: {function cr
28cf0 65 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 7d  eation routines}
28d00 20 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 74 77   .**.** These tw
28d10 6f 20 66 75 6e 63 74 69 6f 6e 73 20 28 63 6f 6c  o functions (col
28d20 6c 65 63 74 69 76 65 6c 79 20 6b 6e 6f 77 6e 20  lectively known 
28d30 61 73 0a 2a 2a 20 22 66 75 6e 63 74 69 6f 6e 20  as.** "function 
28d40 63 72 65 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65  creation routine
28d50 73 22 29 20 61 72 65 20 75 73 65 64 20 74 6f 20  s") are used to 
28d60 61 64 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  add SQL function
28d70 73 20 6f 72 20 61 67 67 72 65 67 61 74 65 73 0a  s or aggregates.
28d80 2a 2a 20 6f 72 20 74 6f 20 72 65 64 65 66 69 6e  ** or to redefin
28d90 65 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f  e the behavior o
28da0 66 20 65 78 69 73 74 69 6e 67 20 53 51 4c 20 66  f existing SQL f
28db0 75 6e 63 74 69 6f 6e 73 20 6f 72 20 61 67 67 72  unctions or aggr
28dc0 65 67 61 74 65 73 2e 20 20 54 68 65 0a 2a 2a 20  egates.  The.** 
28dd0 64 69 66 66 65 72 65 6e 63 65 20 6f 6e 6c 79 20  difference only 
28de0 62 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f 20  between the two 
28df0 69 73 20 74 68 61 74 20 74 68 65 20 73 65 63 6f  is that the seco
28e00 6e 64 20 70 61 72 61 6d 65 74 65 72 2c 20 74 68  nd parameter, th
28e10 65 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65  e.** name of the
28e20 20 28 73 63 61 6c 61 72 29 20 66 75 6e 63 74 69   (scalar) functi
28e30 6f 6e 20 6f 72 20 61 67 67 72 65 67 61 74 65 2c  on or aggregate,
28e40 20 69 73 20 65 6e 63 6f 64 65 64 20 69 6e 20 55   is encoded in U
28e50 54 46 2d 38 20 66 6f 72 0a 2a 2a 20 73 71 6c 69  TF-8 for.** sqli
28e60 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
28e70 69 6f 6e 28 29 20 61 6e 64 20 55 54 46 2d 31 36  ion() and UTF-16
28e80 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65   for sqlite3_cre
28e90 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29  ate_function16()
28ea0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  ..**.** The firs
28eb0 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74  t parameter is t
28ec0 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
28ed0 6e 65 63 74 69 6f 6e 5d 20 74 6f 20 77 68 69 63  nection] to whic
28ee0 68 20 74 68 65 20 53 51 4c 0a 2a 2a 20 66 75 6e  h the SQL.** fun
28ef0 63 74 69 6f 6e 20 69 73 20 74 6f 20 62 65 20 61  ction is to be a
28f00 64 64 65 64 2e 20 20 49 66 20 61 20 73 69 6e 67  dded.  If a sing
28f10 6c 65 0a 2a 2a 20 70 72 6f 67 72 61 6d 20 75 73  le.** program us
28f20 65 73 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  es more than one
28f30 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
28f40 63 74 69 6f 6e 5d 20 69 6e 74 65 72 6e 61 6c 6c  ction] internall
28f50 79 2c 20 74 68 65 6e 20 53 51 4c 0a 2a 2a 20 66  y, then SQL.** f
28f60 75 6e 63 74 69 6f 6e 73 20 6d 75 73 74 20 62 65  unctions must be
28f70 20 61 64 64 65 64 20 69 6e 64 69 76 69 64 75 61   added individua
28f80 6c 6c 79 20 74 6f 20 65 61 63 68 20 5b 64 61 74  lly to each [dat
28f90 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
28fa0 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63  ]..**.** The sec
28fb0 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73  ond parameter is
28fc0 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
28fd0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 6f   SQL function to
28fe0 20 62 65 20 63 72 65 61 74 65 64 0a 2a 2a 20 6f   be created.** o
28ff0 72 20 72 65 64 65 66 69 6e 65 64 2e 0a 2a 2a 20  r redefined..** 
29000 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68  The length of th
29010 65 20 6e 61 6d 65 20 69 73 20 6c 69 6d 69 74 65  e name is limite
29020 64 20 74 6f 20 32 35 35 20 62 79 74 65 73 2c 20  d to 255 bytes, 
29030 65 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68 65  exclusive of the
29040 20 0a 2a 2a 20 7a 65 72 6f 2d 74 65 72 6d 69 6e   .** zero-termin
29050 61 74 6f 72 2e 20 20 4e 6f 74 65 20 74 68 61 74  ator.  Note that
29060 20 74 68 65 20 6e 61 6d 65 20 6c 65 6e 67 74 68   the name length
29070 20 6c 69 6d 69 74 20 69 73 20 69 6e 20 62 79 74   limit is in byt
29080 65 73 2c 20 6e 6f 74 0a 2a 2a 20 63 68 61 72 61  es, not.** chara
29090 63 74 65 72 73 2e 20 20 41 6e 79 20 61 74 74 65  cters.  Any atte
290a0 6d 70 74 20 74 6f 20 63 72 65 61 74 65 20 61 20  mpt to create a 
290b0 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 20  function with a 
290c0 6c 6f 6e 67 65 72 20 6e 61 6d 65 0a 2a 2a 20 77  longer name.** w
290d0 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 6e  ill result in an
290e0 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 65 72   SQLITE_ERROR er
290f0 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74  ror..**.** The t
29100 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 69  hird parameter i
29110 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
29120 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 20 74  arguments that t
29130 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  he SQL function 
29140 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65 20  or.** aggregate 
29150 74 61 6b 65 73 2e 20 49 66 20 74 68 69 73 20 70  takes. If this p
29160 61 72 61 6d 65 74 65 72 20 69 73 20 6e 65 67 61  arameter is nega
29170 74 69 76 65 2c 20 74 68 65 6e 20 74 68 65 20 53  tive, then the S
29180 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a  QL function or.*
29190 2a 20 61 67 67 72 65 67 61 74 65 20 6d 61 79 20  * aggregate may 
291a0 74 61 6b 65 20 61 6e 79 20 6e 75 6d 62 65 72 20  take any number 
291b0 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a  of arguments..**
291c0 0a 2a 2a 20 54 68 65 20 66 6f 75 72 74 68 20 70  .** The fourth p
291d0 61 72 61 6d 65 74 65 72 2c 20 65 54 65 78 74 52  arameter, eTextR
291e0 65 70 2c 20 73 70 65 63 69 66 69 65 73 20 77 68  ep, specifies wh
291f0 61 74 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 55  at .** [SQLITE_U
29200 54 46 38 20 7c 20 74 65 78 74 20 65 6e 63 6f 64  TF8 | text encod
29210 69 6e 67 5d 20 74 68 69 73 20 53 51 4c 20 66 75  ing] this SQL fu
29220 6e 63 74 69 6f 6e 20 70 72 65 66 65 72 73 20 66  nction prefers f
29230 6f 72 0a 2a 2a 20 69 74 73 20 70 61 72 61 6d 65  or.** its parame
29240 74 65 72 73 2e 20 20 41 6e 79 20 53 51 4c 20 66  ters.  Any SQL f
29250 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e  unction implemen
29260 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65  tation should be
29270 20 61 62 6c 65 20 74 6f 20 77 6f 72 6b 0a 2a 2a   able to work.**
29280 20 77 6f 72 6b 20 77 69 74 68 20 55 54 46 2d 38   work with UTF-8
29290 2c 20 55 54 46 2d 31 36 6c 65 2c 20 6f 72 20 55  , UTF-16le, or U
292a0 54 46 2d 31 36 62 65 2e 20 20 42 75 74 20 73 6f  TF-16be.  But so
292b0 6d 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  me implementatio
292c0 6e 73 20 6d 61 79 20 62 65 0a 2a 2a 20 6d 6f 72  ns may be.** mor
292d0 65 20 65 66 66 69 63 69 65 6e 74 20 77 69 74 68  e efficient with
292e0 20 6f 6e 65 20 65 6e 63 6f 64 69 6e 67 20 74 68   one encoding th
292f0 61 6e 20 61 6e 6f 74 68 65 72 2e 20 20 49 74 20  an another.  It 
29300 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 0a 2a 2a  is allowed to.**
29310 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f   invoke sqlite3_
29320 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
29330 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65  ) or sqlite3_cre
29340 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29  ate_function16()
29350 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 74 69 6d   multiple.** tim
29360 65 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65  es with the same
29370 20 66 75 6e 63 74 69 6f 6e 20 62 75 74 20 77 69   function but wi
29380 74 68 20 64 69 66 66 65 72 65 6e 74 20 76 61 6c  th different val
29390 75 65 73 20 6f 66 20 65 54 65 78 74 52 65 70 2e  ues of eTextRep.
293a0 0a 2a 2a 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c  .** When multipl
293b0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
293c0 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75  s of the same fu
293d0 6e 63 74 69 6f 6e 20 61 72 65 20 61 76 61 69 6c  nction are avail
293e0 61 62 6c 65 2c 20 53 51 4c 69 74 65 0a 2a 2a 20  able, SQLite.** 
293f0 77 69 6c 6c 20 70 69 63 6b 20 74 68 65 20 6f 6e  will pick the on
29400 65 20 74 68 61 74 20 69 6e 76 6f 6c 76 65 73 20  e that involves 
29410 74 68 65 20 6c 65 61 73 74 20 61 6d 6f 75 6e 74  the least amount
29420 20 6f 66 20 64 61 74 61 20 63 6f 6e 76 65 72 73   of data convers
29430 69 6f 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65  ion..** If there
29440 20 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c   is only a singl
29450 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
29460 20 77 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20   which does not 
29470 63 61 72 65 20 77 68 61 74 0a 2a 2a 20 74 65 78  care what.** tex
29480 74 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 75 73  t encoding is us
29490 65 64 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 75  ed, then the fou
294a0 72 74 68 20 61 72 67 75 6d 65 6e 74 20 73 68 6f  rth argument sho
294b0 75 6c 64 20 62 65 0a 2a 2a 20 5b 53 51 4c 49 54  uld be.** [SQLIT
294c0 45 5f 41 4e 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68  E_ANY]..**.** Th
294d0 65 20 66 69 66 74 68 20 70 61 72 61 6d 65 74 65  e fifth paramete
294e0 72 20 69 73 20 61 6e 20 61 72 62 69 74 72 61 72  r is an arbitrar
294f0 79 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 65 20  y pointer.  The 
29500 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a  implementation.*
29510 2a 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f  * of the functio
29520 6e 20 63 61 6e 20 67 61 69 6e 20 61 63 63 65 73  n can gain acces
29530 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 65  s to this pointe
29540 72 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69  r using.** [sqli
29550 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 5d  te3_user_data()]
29560 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 76 65  ..**.** The seve
29570 6e 74 68 2c 20 65 69 67 68 74 68 20 61 6e 64 20  nth, eighth and 
29580 6e 69 6e 74 68 20 70 61 72 61 6d 65 74 65 72 73  ninth parameters
29590 2c 20 78 46 75 6e 63 2c 20 78 53 74 65 70 20 61  , xFunc, xStep a
295a0 6e 64 20 78 46 69 6e 61 6c 2c 20 61 72 65 0a 2a  nd xFinal, are.*
295b0 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 43 2d  * pointers to C-
295c0 6c 61 6e 67 75 61 67 65 20 66 75 6e 63 74 69 6f  language functio
295d0 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  ns that implemen
295e0 74 20 74 68 65 20 53 51 4c 0a 2a 2a 20 66 75 6e  t the SQL.** fun
295f0 63 74 69 6f 6e 20 6f 72 20 61 67 67 72 65 67 61  ction or aggrega
29600 74 65 2e 20 41 20 73 63 61 6c 61 72 20 53 51 4c  te. A scalar SQL
29610 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72   function requir
29620 65 73 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61  es an implementa
29630 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 78  tion of.** the x
29640 46 75 6e 63 20 63 61 6c 6c 62 61 63 6b 20 6f 6e  Func callback on
29650 6c 79 2c 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  ly, NULL pointer
29660 73 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73  s should be pass
29670 65 64 20 61 73 20 74 68 65 20 78 53 74 65 70 0a  ed as the xStep.
29680 2a 2a 20 61 6e 64 20 78 46 69 6e 61 6c 20 70 61  ** and xFinal pa
29690 72 61 6d 65 74 65 72 73 2e 20 41 6e 20 61 67 67  rameters. An agg
296a0 72 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74  regate SQL funct
296b0 69 6f 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20  ion requires an 
296c0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a  implementation.*
296d0 2a 20 6f 66 20 78 53 74 65 70 20 61 6e 64 20 78  * of xStep and x
296e0 46 69 6e 61 6c 20 61 6e 64 20 4e 55 4c 4c 20 73  Final and NULL s
296f0 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 20  hould be passed 
29700 66 6f 72 20 78 46 75 6e 63 2e 20 54 6f 20 64 65  for xFunc. To de
29710 6c 65 74 65 20 61 6e 0a 2a 2a 20 65 78 69 73 74  lete an.** exist
29720 69 6e 67 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ing SQL function
29730 20 6f 72 20 61 67 67 72 65 67 61 74 65 2c 20 70   or aggregate, p
29740 61 73 73 20 4e 55 4c 4c 20 66 6f 72 20 61 6c 6c  ass NULL for all
29750 20 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 0a   three function.
29760 2a 2a 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a  ** callback..**.
29770 2a 2a 20 49 74 20 69 73 20 70 65 72 6d 69 74 74  ** It is permitt
29780 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 20 6d  ed to register m
29790 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e  ultiple implemen
297a0 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73  tations of the s
297b0 61 6d 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73  ame.** functions
297c0 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e   with the same n
297d0 61 6d 65 20 62 75 74 20 77 69 74 68 20 65 69 74  ame but with eit
297e0 68 65 72 20 64 69 66 66 65 72 69 6e 67 20 6e 75  her differing nu
297f0 6d 62 65 72 73 20 6f 66 0a 2a 2a 20 61 72 67 75  mbers of.** argu
29800 6d 65 6e 74 73 20 6f 72 20 64 69 66 66 65 72 69  ments or differi
29810 6e 67 20 70 65 72 66 65 72 72 65 64 20 74 65 78  ng perferred tex
29820 74 20 65 6e 63 6f 64 69 6e 67 73 2e 20 20 53 51  t encodings.  SQ
29830 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 0a 2a 2a  Lite will use.**
29840 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
29850 69 6f 6e 20 6d 6f 73 74 20 63 6c 6f 73 65 6c 79  ion most closely
29860 20 6d 61 74 63 68 65 73 20 74 68 65 20 77 61 79   matches the way
29870 20 69 6e 20 77 68 69 63 68 20 74 68 65 0a 2a 2a   in which the.**
29880 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73   SQL function is
29890 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56   used..**.** INV
298a0 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b  ARIANTS:.**.** {
298b0 46 31 36 31 30 33 7d 20 54 68 65 20 5b 73 71 6c  F16103} The [sql
298c0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
298d0 74 69 6f 6e 31 36 28 29 5d 20 69 6e 74 65 72 66  tion16()] interf
298e0 61 63 65 20 62 65 68 61 76 65 73 20 65 78 61 63  ace behaves exac
298f0 74 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  tly.**          
29900 6c 69 6b 65 20 5b 73 71 6c 69 74 65 33 5f 63 72  like [sqlite3_cr
29910 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d  eate_function()]
29920 20 69 6e 20 65 76 65 72 79 20 77 61 79 20 65 78   in every way ex
29930 63 65 70 74 20 74 68 61 74 20 69 74 0a 2a 2a 20  cept that it.** 
29940 20 20 20 20 20 20 20 20 20 69 6e 74 65 72 70 72           interpr
29950 65 74 73 20 74 68 65 20 7a 46 75 6e 63 74 69 6f  ets the zFunctio
29960 6e 4e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 61  nName argument a
29970 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 7a 65  s.**          ze
29980 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54  ro-terminated UT
29990 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79 74 65  F-16 native byte
299a0 20 6f 72 64 65 72 20 69 6e 73 74 65 61 64 20 6f   order instead o
299b0 66 20 61 73 20 61 0a 2a 2a 20 20 20 20 20 20 20  f as a.**       
299c0 20 20 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74     zero-terminat
299d0 65 64 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20  ed UTF-8..**.** 
299e0 7b 46 31 36 31 30 36 7d 20 41 20 73 75 63 63 65  {F16106} A succe
299f0 73 73 66 75 6c 20 69 6e 76 6f 63 61 74 69 6f 6e  ssful invocation
29a00 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   of.**          
29a10 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65  the [sqlite3_cre
29a20 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 44 2c 58  ate_function(D,X
29a30 2c 4e 2c 45 2c 2e 2e 2e 29 5d 20 69 6e 74 65 72  ,N,E,...)] inter
29a40 66 61 63 65 20 72 65 67 69 73 74 65 72 73 0a 2a  face registers.*
29a50 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 72 65  *          or re
29a60 70 6c 61 63 65 73 20 63 61 6c 6c 62 61 63 6b 20  places callback 
29a70 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 5b 64 61  functions in [da
29a80 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
29a90 6e 5d 20 44 0a 2a 2a 20 20 20 20 20 20 20 20 20  n] D.**         
29aa0 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
29ab0 6e 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74  nt the SQL funct
29ac0 69 6f 6e 20 6e 61 6d 65 64 20 58 20 77 69 74 68  ion named X with
29ad0 20 4e 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a   N parameters.**
29ae0 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 68 61            and ha
29af0 76 69 6e 67 20 61 20 70 65 72 66 65 72 72 65 64  ving a perferred
29b00 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 6f   text encoding o
29b10 66 20 45 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 36 31  f E..**.** {F161
29b20 30 39 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c  09} A successful
29b30 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
29b40 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
29b50 6e 28 44 2c 58 2c 4e 2c 45 2c 50 2c 46 2c 53 2c  n(D,X,N,E,P,F,S,
29b60 4c 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  L)].**          
29b70 72 65 70 6c 61 63 65 73 20 74 68 65 20 50 2c 20  replaces the P, 
29b80 46 2c 20 53 2c 20 61 6e 64 20 4c 20 76 61 6c 75  F, S, and L valu
29b90 65 73 20 66 72 6f 6d 20 61 6e 79 20 70 72 69 6f  es from any prio
29ba0 72 20 63 61 6c 6c 73 20 77 69 74 68 0a 2a 2a 20  r calls with.** 
29bb0 20 20 20 20 20 20 20 20 20 74 68 65 20 73 61 6d           the sam
29bc0 65 20 44 2c 20 58 2c 20 4e 2c 20 61 6e 64 20 45  e D, X, N, and E
29bd0 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b   values..**.** {
29be0 46 31 36 31 31 32 7d 20 54 68 65 20 5b 73 71 6c  F16112} The [sql
29bf0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
29c00 74 69 6f 6e 28 44 2c 58 2c 2e 2e 2e 29 5d 20 69  tion(D,X,...)] i
29c10 6e 74 65 72 66 61 63 65 20 66 61 69 6c 73 20 77  nterface fails w
29c20 69 74 68 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ith.**          
29c30 61 20 72 65 74 75 72 6e 20 63 6f 64 65 20 6f 66  a return code of
29c40 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20   [SQLITE_ERROR] 
29c50 69 66 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74  if the SQL funct
29c60 69 6f 6e 20 6e 61 6d 65 20 58 20 69 73 0a 2a 2a  ion name X is.**
29c70 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 65 72            longer
29c80 20 74 68 61 6e 20 32 35 35 20 62 79 74 65 73 20   than 255 bytes 
29c90 65 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68 65  exclusive of the
29ca0 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72   zero terminator
29cb0 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 36 31 31 38 7d  ..**.** {F16118}
29cc0 20 45 69 74 68 65 72 20 46 20 6d 75 73 74 20 62   Either F must b
29cd0 65 20 4e 55 4c 4c 20 61 6e 64 20 53 20 61 6e 64  e NULL and S and
29ce0 20 4c 20 61 72 65 20 6e 6f 6e 2d 4e 55 4c 4c 20   L are non-NULL 
29cf0 6f 72 20 65 6c 73 65 20 46 0a 2a 2a 20 20 20 20  or else F.**    
29d00 20 20 20 20 20 20 69 73 20 6e 6f 6e 2d 4e 55 4c        is non-NUL
29d10 4c 20 61 6e 64 20 53 20 61 6e 64 20 4c 20 61 72  L and S and L ar
29d20 65 20 4e 55 4c 4c 2c 20 6f 74 68 65 72 77 69 73  e NULL, otherwis
29d30 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73  e.**          [s
29d40 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
29d50 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 50  nction(D,X,N,E,P
29d60 2c 46 2c 53 2c 4c 29 5d 20 72 65 74 75 72 6e 73  ,F,S,L)] returns
29d70 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 2e   [SQLITE_ERROR].
29d80 0a 2a 2a 0a 2a 2a 20 7b 46 31 36 31 32 31 7d 20  .**.** {F16121} 
29d90 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65  The [sqlite3_cre
29da0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 44 2c 2e  ate_function(D,.
29db0 2e 2e 29 5d 20 69 6e 74 65 72 66 61 63 65 20 66  ..)] interface f
29dc0 61 69 6c 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20  ails with an.** 
29dd0 20 20 20 20 20 20 20 20 20 65 72 72 6f 72 20 63           error c
29de0 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 42  ode of [SQLITE_B
29df0 55 53 59 5d 20 69 66 20 74 68 65 72 65 20 65 78  USY] if there ex
29e00 69 73 74 20 5b 70 72 65 70 61 72 65 64 20 73 74  ist [prepared st
29e10 61 74 65 6d 65 6e 74 73 5d 0a 2a 2a 20 20 20 20  atements].**    
29e20 20 20 20 20 20 20 61 73 73 6f 63 69 61 74 65 64        associated
29e30 20 77 69 74 68 20 74 68 65 20 5b 64 61 74 61 62   with the [datab
29e40 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
29e50 44 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 36 31 32 34  D..**.** {F16124
29e60 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63  } The [sqlite3_c
29e70 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 44  reate_function(D
29e80 2c 58 2c 4e 2c 2e 2e 2e 29 5d 20 69 6e 74 65 72  ,X,N,...)] inter
29e90 66 61 63 65 20 66 61 69 6c 73 20 77 69 74 68 20  face fails with 
29ea0 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 65  an.**          e
29eb0 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53 51  rror code of [SQ
29ec0 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 66 20 70  LITE_ERROR] if p
29ed0 61 72 61 6d 65 74 65 72 20 4e 20 28 73 70 65 63  arameter N (spec
29ee0 69 66 79 69 6e 67 20 74 68 65 20 6e 75 6d 62 65  ifying the numbe
29ef0 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 66  r.**          of
29f00 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68   arguments to th
29f10 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 62  e SQL function b
29f20 65 69 6e 67 20 72 65 67 69 73 74 65 72 65 64 29  eing registered)
29f30 20 69 73 20 6c 65 73 73 0a 2a 2a 20 20 20 20 20   is less.**     
29f40 20 20 20 20 20 74 68 61 6e 20 2d 31 20 6f 72 20       than -1 or 
29f50 67 72 65 61 74 65 72 20 74 68 61 6e 20 31 32 37  greater than 127
29f60 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 36 31 32 37 7d  ..**.** {F16127}
29f70 20 57 68 65 6e 20 4e 20 69 73 20 6e 6f 6e 2d 6e   When N is non-n
29f80 65 67 61 74 69 76 65 2c 20 74 68 65 20 5b 73 71  egative, the [sq
29f90 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
29fa0 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 2e 2e 2e 29  ction(D,X,N,...)
29fb0 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e  ].**          in
29fc0 74 65 72 66 61 63 65 20 63 61 75 73 65 73 20 63  terface causes c
29fd0 61 6c 6c 62 61 63 6b 73 20 74 6f 20 62 65 20 69  allbacks to be i
29fe0 6e 76 6f 6b 65 64 20 66 6f 72 20 74 68 65 20 53  nvoked for the S
29ff0 51 4c 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 20  QL function.**  
2a000 20 20 20 20 20 20 20 20 6e 61 6d 65 64 20 58 20          named X 
2a010 77 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  when the number 
2a020 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  of arguments to 
2a030 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  the SQL function
2a040 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   is.**          
2a050 65 78 61 63 74 6c 79 20 4e 2e 0a 2a 2a 0a 2a 2a  exactly N..**.**
2a060 20 7b 46 31 36 31 33 30 7d 20 57 68 65 6e 20 4e   {F16130} When N
2a070 20 69 73 20 2d 31 2c 20 74 68 65 20 5b 73 71 6c   is -1, the [sql
2a080 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
2a090 74 69 6f 6e 28 44 2c 58 2c 4e 2c 2e 2e 2e 29 5d  tion(D,X,N,...)]
2a0a0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e 74  .**          int
2a0b0 65 72 66 61 63 65 20 63 61 75 73 65 73 20 63 61  erface causes ca
2a0c0 6c 6c 62 61 63 6b 73 20 74 6f 20 62 65 20 69 6e  llbacks to be in
2a0d0 76 6f 6b 65 64 20 66 6f 72 20 74 68 65 20 53 51  voked for the SQ
2a0e0 4c 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 20 20  L function.**   
2a0f0 20 20 20 20 20 20 20 6e 61 6d 65 64 20 58 20 77         named X w
2a100 69 74 68 20 61 6e 79 20 6e 75 6d 62 65 72 20 6f  ith any number o
2a110 66 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a 0a  f arguments..**.
2a120 2a 2a 20 7b 46 31 36 31 33 33 7d 20 57 68 65 6e  ** {F16133} When
2a130 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74   calls to [sqlit
2a140 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
2a150 6f 6e 28 44 2c 58 2c 4e 2c 2e 2e 2e 29 5d 0a 2a  on(D,X,N,...)].*
2a160 2a 20 20 20 20 20 20 20 20 20 20 73 70 65 63 69  *          speci
2a170 66 79 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c  fy multiple impl
2a180 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74  ementations of t
2a190 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e  he same function
2a1a0 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61   X.**          a
2a1b0 6e 64 20 77 68 65 6e 20 6f 6e 65 20 69 6d 70 6c  nd when one impl
2a1c0 65 6d 65 6e 74 61 74 69 6f 6e 20 68 61 73 20 4e  ementation has N
2a1d0 3e 3d 30 20 61 6e 64 20 74 68 65 20 6f 74 68 65  >=0 and the othe
2a1e0 72 20 68 61 73 20 4e 3d 28 2d 31 29 0a 2a 2a 20  r has N=(-1).** 
2a1f0 20 20 20 20 20 20 20 20 20 74 68 65 20 69 6d 70           the imp
2a200 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 74 68  lementation with
2a210 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 4e 20 69 73   a non-zero N is
2a220 20 70 72 65 66 65 72 72 65 64 2e 0a 2a 2a 0a 2a   preferred..**.*
2a230 2a 20 7b 46 31 36 31 33 36 7d 20 57 68 65 6e 20  * {F16136} When 
2a240 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65  calls to [sqlite
2a250 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
2a260 6e 28 44 2c 58 2c 4e 2c 45 2c 2e 2e 2e 29 5d 0a  n(D,X,N,E,...)].
2a270 2a 2a 20 20 20 20 20 20 20 20 20 20 73 70 65 63  **          spec
2a280 69 66 79 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70  ify multiple imp
2a290 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20  lementations of 
2a2a0 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f  the same functio
2a2b0 6e 20 58 20 77 69 74 68 0a 2a 2a 20 20 20 20 20  n X with.**     
2a2c0 20 20 20 20 20 74 68 65 20 73 61 6d 65 20 6e 75       the same nu
2a2d0 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
2a2e0 73 20 4e 20 62 75 74 20 77 69 74 68 20 64 69 66  s N but with dif
2a2f0 66 65 72 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20  ferent.**       
2a300 20 20 20 65 6e 63 6f 64 69 6e 67 73 20 45 2c 20     encodings E, 
2a310 74 68 65 6e 20 74 68 65 20 69 6d 70 6c 65 6d 65  then the impleme
2a320 6e 74 61 74 69 6f 6e 20 77 68 65 72 65 20 45 20  ntation where E 
2a330 6d 61 74 63 68 65 73 20 74 68 65 0a 2a 2a 20 20  matches the.**  
2a340 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65          database
2a350 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 70 72 65   encoding is pre
2a360 66 65 72 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 46  ferred..**.** {F
2a370 31 36 31 33 39 7d 20 46 6f 72 20 61 6e 20 61 67  16139} For an ag
2a380 67 72 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63  gregate SQL func
2a390 74 69 6f 6e 20 63 72 65 61 74 65 64 20 75 73 69  tion created usi
2a3a0 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  ng.**          [
2a3b0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
2a3c0 75 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c  unction(D,X,N,E,
2a3d0 50 2c 30 2c 53 2c 4c 29 5d 20 74 68 65 20 66 69  P,0,S,L)] the fi
2a3e0 6e 69 61 6c 69 7a 65 72 0a 2a 2a 20 20 20 20 20  nializer.**     
2a3f0 20 20 20 20 20 66 75 6e 63 74 69 6f 6e 20 4c 20       function L 
2a400 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 69  will always be i
2a410 6e 76 6f 6b 65 64 20 65 78 61 63 74 6c 79 20 6f  nvoked exactly o
2a420 6e 63 65 20 69 66 20 74 68 65 0a 2a 2a 20 20 20  nce if the.**   
2a430 20 20 20 20 20 20 20 73 74 65 70 20 66 75 6e 63         step func
2a440 74 69 6f 6e 20 53 20 69 73 20 63 61 6c 6c 65 64  tion S is called
2a450 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69 6d   one or more tim
2a460 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 36 31 34  es..**.** {F1614
2a470 32 7d 20 57 68 65 6e 20 53 51 4c 69 74 65 20 69  2} When SQLite i
2a480 6e 76 6f 6b 65 73 20 65 69 74 68 65 72 20 74 68  nvokes either th
2a490 65 20 78 46 75 6e 63 20 6f 72 20 78 53 74 65 70  e xFunc or xStep
2a4a0 20 66 75 6e 63 74 69 6f 6e 20 6f 66 0a 2a 2a 20   function of.** 
2a4b0 20 20 20 20 20 20 20 20 20 61 6e 20 61 70 70 6c           an appl
2a4c0 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
2a4d0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 20  SQL function or 
2a4e0 61 67 67 72 65 67 61 74 65 20 63 72 65 61 74 65  aggregate create
2a4f0 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62 79  d.**          by
2a500 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
2a510 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 20 6f 72 20  _function()] or 
2a520 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
2a530 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 2c 0a 2a  function16()],.*
2a540 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 6e 20  *          then 
2a550 74 68 65 20 61 72 72 61 79 20 6f 66 20 5b 73 71  the array of [sq
2a560 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a  lite3_value] obj
2a570 65 63 74 73 20 70 61 73 73 65 64 20 61 73 20 74  ects passed as t
2a580 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  he.**          t
2a590 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 61  hird parameter a
2a5a0 72 65 20 61 6c 77 61 79 73 20 5b 70 72 6f 74 65  re always [prote
2a5b0 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c  cted sqlite3_val
2a5c0 75 65 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2f 0a  ue] objects..*/.
2a5d0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2a5e0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
2a5f0 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65  nction(.  sqlite
2a600 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63  3 *db,.  const c
2a610 68 61 72 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61  har *zFunctionNa
2a620 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a  me,.  int nArg,.
2a630 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 0a    int eTextRep,.
2a640 20 20 76 6f 69 64 20 2a 70 41 70 70 2c 0a 20 20    void *pApp,.  
2a650 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71  void (*xFunc)(sq
2a660 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
2a670 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
2a680 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53  **),.  void (*xS
2a690 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  tep)(sqlite3_con
2a6a0 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
2a6b0 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f  3_value**),.  vo
2a6c0 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c  id (*xFinal)(sql
2a6d0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29  ite3_context*).)
2a6e0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
2a6f0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2a700 66 75 6e 63 74 69 6f 6e 31 36 28 0a 20 20 73 71  function16(.  sq
2a710 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e  lite3 *db,.  con
2a720 73 74 20 76 6f 69 64 20 2a 7a 46 75 6e 63 74 69  st void *zFuncti
2a730 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41  onName,.  int nA
2a740 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78 74 52  rg,.  int eTextR
2a750 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 41 70 70  ep,.  void *pApp
2a760 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63  ,.  void (*xFunc
2a770 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
2a780 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
2a790 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20  alue**),.  void 
2a7a0 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65 33  (*xStep)(sqlite3
2a7b0 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71  _context*,int,sq
2a7c0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a  lite3_value**),.
2a7d0 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29    void (*xFinal)
2a7e0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2a7f0 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  *).);../*.** CAP
2a800 49 33 52 45 46 3a 20 54 65 78 74 20 45 6e 63 6f  I3REF: Text Enco
2a810 64 69 6e 67 73 20 7b 46 31 30 32 36 37 7d 0a 2a  dings {F10267}.*
2a820 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74  *.** These const
2a830 61 6e 74 20 64 65 66 69 6e 65 20 69 6e 74 65 67  ant define integ
2a840 65 72 20 63 6f 64 65 73 20 74 68 61 74 20 72 65  er codes that re
2a850 70 72 65 73 65 6e 74 20 74 68 65 20 76 61 72 69  present the vari
2a860 6f 75 73 0a 2a 2a 20 74 65 78 74 20 65 6e 63 6f  ous.** text enco
2a870 64 69 6e 67 73 20 73 75 70 70 6f 72 74 65 64 20  dings supported 
2a880 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 23 64  by SQLite..*/.#d
2a890 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46  efine SQLITE_UTF
2a8a0 38 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64  8           1.#d
2a8b0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46  efine SQLITE_UTF
2a8c0 31 36 4c 45 20 20 20 20 20 20 20 20 32 0a 23 64  16LE        2.#d
2a8d0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46  efine SQLITE_UTF
2a8e0 31 36 42 45 20 20 20 20 20 20 20 20 33 0a 23 64  16BE        3.#d
2a8f0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46  efine SQLITE_UTF
2a900 31 36 20 20 20 20 20 20 20 20 20 20 34 20 20 20  16          4   
2a910 20 2f 2a 20 55 73 65 20 6e 61 74 69 76 65 20 62   /* Use native b
2a920 79 74 65 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65  yte order */.#de
2a930 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 4e 59 20  fine SQLITE_ANY 
2a940 20 20 20 20 20 20 20 20 20 20 20 35 20 20 20 20             5    
2a950 2f 2a 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  /* sqlite3_creat
2a960 65 5f 66 75 6e 63 74 69 6f 6e 20 6f 6e 6c 79 20  e_function only 
2a970 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
2a980 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 20  E_UTF16_ALIGNED 
2a990 20 38 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33   8    /* sqlite3
2a9a0 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
2a9b0 6e 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  n only */../*.**
2a9c0 20 43 41 50 49 33 52 45 46 3a 20 4f 62 73 6f 6c   CAPI3REF: Obsol
2a9d0 65 74 65 20 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a  ete Functions.**
2a9e0 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69  .** These functi
2a9f0 6f 6e 73 20 61 72 65 20 61 6c 6c 20 6e 6f 77 20  ons are all now 
2aa00 6f 62 73 6f 6c 65 74 65 2e 20 20 49 6e 20 6f 72  obsolete.  In or
2aa10 64 65 72 20 74 6f 20 6d 61 69 6e 74 61 69 6e 0a  der to maintain.
2aa20 2a 2a 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d  ** backwards com
2aa30 70 61 74 69 62 69 6c 69 74 79 20 77 69 74 68 20  patibility with 
2aa40 6f 6c 64 65 72 20 63 6f 64 65 2c 20 77 65 20 63  older code, we c
2aa50 6f 6e 74 69 6e 75 65 20 74 6f 20 73 75 70 70 6f  ontinue to suppo
2aa60 72 74 0a 2a 2a 20 74 68 65 73 65 20 66 75 6e 63  rt.** these func
2aa70 74 69 6f 6e 73 2e 20 20 48 6f 77 65 76 65 72 2c  tions.  However,
2aa80 20 6e 65 77 20 64 65 76 65 6c 6f 70 6d 65 6e 74   new development
2aa90 20 70 72 6f 6a 65 63 74 73 20 73 68 6f 75 6c 64   projects should
2aaa0 20 61 76 6f 69 64 0a 2a 2a 20 74 68 65 20 75 73   avoid.** the us
2aab0 65 20 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74  e of these funct
2aac0 69 6f 6e 73 2e 20 20 54 6f 20 68 65 6c 70 20 65  ions.  To help e
2aad0 6e 63 6f 75 72 61 67 65 20 70 65 6f 70 6c 65 20  ncourage people 
2aae0 74 6f 20 61 76 6f 69 64 0a 2a 2a 20 75 73 69 6e  to avoid.** usin
2aaf0 67 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e  g these function
2ab00 73 2c 20 77 65 20 61 72 65 20 6e 6f 74 20 67 6f  s, we are not go
2ab10 69 6e 67 20 74 6f 20 74 65 6c 6c 20 79 6f 75 20  ing to tell you 
2ab20 77 61 6e 74 20 74 68 65 79 20 64 6f 2e 0a 2a 2f  want they do..*/
2ab30 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2ab40 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
2ab50 65 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f  e_count(sqlite3_
2ab60 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54  context*);.SQLIT
2ab70 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2ab80 33 5f 65 78 70 69 72 65 64 28 73 71 6c 69 74 65  3_expired(sqlite
2ab90 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49 54 45  3_stmt*);.SQLITE
2aba0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2abb0 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e  _transfer_bindin
2abc0 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  gs(sqlite3_stmt*
2abd0 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29  , sqlite3_stmt*)
2abe0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
2abf0 20 73 71 6c 69 74 65 33 5f 67 6c 6f 62 61 6c 5f   sqlite3_global_
2ac00 72 65 63 6f 76 65 72 28 76 6f 69 64 29 3b 0a 53  recover(void);.S
2ac10 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
2ac20 71 6c 69 74 65 33 5f 74 68 72 65 61 64 5f 63 6c  qlite3_thread_cl
2ac30 65 61 6e 75 70 28 76 6f 69 64 29 3b 0a 53 51 4c  eanup(void);.SQL
2ac40 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2ac50 74 65 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d  te3_memory_alarm
2ac60 28 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 73  (void(*)(void*,s
2ac70 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74  qlite3_int64,int
2ac80 29 2c 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f  ),void*,sqlite3_
2ac90 69 6e 74 36 34 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  int64);../*.** C
2aca0 41 50 49 33 52 45 46 3a 20 4f 62 74 61 69 6e 69  API3REF: Obtaini
2acb0 6e 67 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 20  ng SQL Function 
2acc0 50 61 72 61 6d 65 74 65 72 20 56 61 6c 75 65 73  Parameter Values
2acd0 20 7b 46 31 35 31 30 30 7d 0a 2a 2a 0a 2a 2a 20   {F15100}.**.** 
2ace0 54 68 65 20 43 2d 6c 61 6e 67 75 61 67 65 20 69  The C-language i
2acf0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
2ad00 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61   SQL functions a
2ad10 6e 64 20 61 67 67 72 65 67 61 74 65 73 20 75 73  nd aggregates us
2ad20 65 73 0a 2a 2a 20 74 68 69 73 20 73 65 74 20 6f  es.** this set o
2ad30 66 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74  f interface rout
2ad40 69 6e 65 73 20 74 6f 20 61 63 63 65 73 73 20 74  ines to access t
2ad50 68 65 20 70 61 72 61 6d 65 74 65 72 20 76 61 6c  he parameter val
2ad60 75 65 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 66 75  ues on.** the fu
2ad70 6e 63 74 69 6f 6e 20 6f 72 20 61 67 67 72 65 67  nction or aggreg
2ad80 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  ate..**.** The x
2ad90 46 75 6e 63 20 28 66 6f 72 20 73 63 61 6c 61 72  Func (for scalar
2ada0 20 66 75 6e 63 74 69 6f 6e 73 29 20 6f 72 20 78   functions) or x
2adb0 53 74 65 70 20 28 66 6f 72 20 61 67 67 72 65 67  Step (for aggreg
2adc0 61 74 65 73 29 20 70 61 72 61 6d 65 74 65 72 73  ates) parameters
2add0 0a 2a 2a 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  .** to [sqlite3_
2ade0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
2adf0 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
2ae00 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31  create_function1
2ae10 36 28 29 5d 0a 2a 2a 20 64 65 66 69 6e 65 20 63  6()].** define c
2ae20 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 69 6d  allbacks that im
2ae30 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20  plement the SQL 
2ae40 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67  functions and ag
2ae50 67 72 65 67 61 74 65 73 2e 0a 2a 2a 20 54 68 65  gregates..** The
2ae60 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74   4th parameter t
2ae70 6f 20 74 68 65 73 65 20 63 61 6c 6c 62 61 63 6b  o these callback
2ae80 73 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66  s is an array of
2ae90 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20   pointers to.** 
2aea0 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  [protected sqlit
2aeb0 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74  e3_value] object
2aec0 73 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65  s.  There is one
2aed0 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d   [sqlite3_value]
2aee0 20 6f 62 6a 65 63 74 20 66 6f 72 0a 2a 2a 20 65   object for.** e
2aef0 61 63 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ach parameter to
2af00 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f   the SQL functio
2af10 6e 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e  n.  These routin
2af20 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 0a 2a  es are used to.*
2af30 2a 20 65 78 74 72 61 63 74 20 76 61 6c 75 65 73  * extract values
2af40 20 66 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74   from the [sqlit
2af50 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74  e3_value] object
2af60 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  s..**.** These r
2af70 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6e 6c  outines work onl
2af80 79 20 77 69 74 68 20 5b 70 72 6f 74 65 63 74 65  y with [protecte
2af90 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d  d sqlite3_value]
2afa0 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 41 6e 79   objects..** Any
2afb0 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20   attempt to use 
2afc0 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f  these routines o
2afd0 6e 20 61 6e 20 5b 75 6e 70 72 6f 74 65 63 74 65  n an [unprotecte
2afe0 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d  d sqlite3_value]
2aff0 0a 2a 2a 20 6f 62 6a 65 63 74 20 72 65 73 75 6c  .** object resul
2b000 74 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20  ts in undefined 
2b010 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20  behavior..**.** 
2b020 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 77  These routines w
2b030 6f 72 6b 20 6a 75 73 74 20 6c 69 6b 65 20 74 68  ork just like th
2b040 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
2b050 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c  .** [sqlite3_col
2b060 75 6d 6e 5f 62 6c 6f 62 20 7c 20 73 71 6c 69 74  umn_blob | sqlit
2b070 65 33 5f 63 6f 6c 75 6d 6e 5f 2a 20 72 6f 75 74  e3_column_* rout
2b080 69 6e 65 73 5d 20 65 78 63 65 70 74 20 74 68 61  ines] except tha
2b090 74 20 0a 2a 2a 20 74 68 65 73 65 20 72 6f 75 74  t .** these rout
2b0a0 69 6e 65 73 20 74 61 6b 65 20 61 20 73 69 6e 67  ines take a sing
2b0b0 6c 65 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71  le [protected sq
2b0c0 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a  lite3_value] obj
2b0d0 65 63 74 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 69  ect pointer.** i
2b0e0 6e 73 74 65 61 64 20 6f 66 20 61 6e 20 5b 73 71  nstead of an [sq
2b0f0 6c 69 74 65 33 5f 73 74 6d 74 2a 5d 20 70 6f 69  lite3_stmt*] poi
2b100 6e 74 65 72 20 61 6e 64 20 61 6e 20 69 6e 74 65  nter and an inte
2b110 67 65 72 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65  ger column numbe
2b120 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  r..**.** The sql
2b130 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
2b140 36 28 29 20 69 6e 74 65 72 66 61 63 65 20 65 78  6() interface ex
2b150 74 72 61 63 74 73 20 61 20 55 54 46 31 36 20 73  tracts a UTF16 s
2b160 74 72 69 6e 67 0a 2a 2a 20 69 6e 20 74 68 65 20  tring.** in the 
2b170 6e 61 74 69 76 65 20 62 79 74 65 2d 6f 72 64 65  native byte-orde
2b180 72 20 6f 66 20 74 68 65 20 68 6f 73 74 20 6d 61  r of the host ma
2b190 63 68 69 6e 65 2e 20 20 54 68 65 0a 2a 2a 20 73  chine.  The.** s
2b1a0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
2b1b0 74 31 36 62 65 28 29 20 61 6e 64 20 73 71 6c 69  t16be() and sqli
2b1c0 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
2b1d0 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a  le() interfaces.
2b1e0 2a 2a 20 65 78 74 72 61 63 74 20 55 54 46 31 36  ** extract UTF16
2b1f0 20 73 74 72 69 6e 67 73 20 61 73 20 62 69 67 2d   strings as big-
2b200 65 6e 64 69 61 6e 20 61 6e 64 20 6c 69 74 74 6c  endian and littl
2b210 65 2d 65 6e 64 69 61 6e 20 72 65 73 70 65 63 74  e-endian respect
2b220 69 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ively..**.** The
2b230 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e   sqlite3_value_n
2b240 75 6d 65 72 69 63 5f 74 79 70 65 28 29 20 69 6e  umeric_type() in
2b250 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73  terface attempts
2b260 20 74 6f 20 61 70 70 6c 79 0a 2a 2a 20 6e 75 6d   to apply.** num
2b270 65 72 69 63 20 61 66 66 69 6e 69 74 79 20 74 6f  eric affinity to
2b280 20 74 68 65 20 76 61 6c 75 65 2e 20 20 54 68 69   the value.  Thi
2b290 73 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6e 20  s means that an 
2b2a0 61 74 74 65 6d 70 74 20 69 73 0a 2a 2a 20 6d 61  attempt is.** ma
2b2b0 64 65 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68  de to convert th
2b2c0 65 20 76 61 6c 75 65 20 74 6f 20 61 6e 20 69 6e  e value to an in
2b2d0 74 65 67 65 72 20 6f 72 20 66 6c 6f 61 74 69 6e  teger or floatin
2b2e0 67 20 70 6f 69 6e 74 2e 20 20 49 66 0a 2a 2a 20  g point.  If.** 
2b2f0 73 75 63 68 20 61 20 63 6f 6e 76 65 72 73 69 6f  such a conversio
2b300 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 20 77 69  n is possible wi
2b310 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66 20 69 6e  thout loss of in
2b320 66 6f 72 6d 61 74 69 6f 6e 20 28 69 6e 20 6f 74  formation (in ot
2b330 68 65 72 0a 2a 2a 20 77 6f 72 64 73 20 69 66 20  her.** words if 
2b340 74 68 65 20 76 61 6c 75 65 20 69 73 20 61 20 73  the value is a s
2b350 74 72 69 6e 67 20 74 68 61 74 20 6c 6f 6f 6b 73  tring that looks
2b360 20 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72 29 0a   like a number).
2b370 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 76  ** then the conv
2b380 65 72 73 69 6f 6e 20 69 73 20 64 6f 6e 65 2e 20  ersion is done. 
2b390 20 4f 74 68 65 72 77 69 73 65 20 6e 6f 20 63 6f   Otherwise no co
2b3a0 6e 76 65 72 73 69 6f 6e 20 6f 63 63 75 72 73 2e  nversion occurs.
2b3b0 20 20 54 68 65 20 0a 2a 2a 20 5b 53 51 4c 49 54    The .** [SQLIT
2b3c0 45 5f 49 4e 54 45 47 45 52 20 7c 20 64 61 74 61  E_INTEGER | data
2b3d0 74 79 70 65 5d 20 61 66 74 65 72 20 63 6f 6e 76  type] after conv
2b3e0 65 72 73 69 6f 6e 20 69 73 20 72 65 74 75 72 6e  ersion is return
2b3f0 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 6c 65 61 73 65  ed..**.** Please
2b400 20 70 61 79 20 70 61 72 74 69 63 75 6c 61 72 20   pay particular 
2b410 61 74 74 65 6e 74 69 6f 6e 20 74 6f 20 74 68 65  attention to the
2b420 20 66 61 63 74 20 74 68 61 74 20 74 68 65 20 70   fact that the p
2b430 6f 69 6e 74 65 72 20 74 68 61 74 0a 2a 2a 20 69  ointer that.** i
2b440 73 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20  s returned from 
2b450 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62  [sqlite3_value_b
2b460 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33  lob()], [sqlite3
2b470 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c 20  _value_text()], 
2b480 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76  or.** [sqlite3_v
2b490 61 6c 75 65 5f 74 65 78 74 31 36 28 29 5d 20 63  alue_text16()] c
2b4a0 61 6e 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65  an be invalidate
2b4b0 64 20 62 79 20 61 20 73 75 62 73 65 71 75 65 6e  d by a subsequen
2b4c0 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71  t call to.** [sq
2b4d0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
2b4e0 73 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76  s()], [sqlite3_v
2b4f0 61 6c 75 65 5f 62 79 74 65 73 31 36 28 29 5d 2c  alue_bytes16()],
2b500 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   [sqlite3_value_
2b510 74 65 78 74 28 29 5d 2c 0a 2a 2a 20 6f 72 20 5b  text()],.** or [
2b520 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2b530 78 74 31 36 28 29 5d 2e 20 20 0a 2a 2a 0a 2a 2a  xt16()].  .**.**
2b540 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
2b550 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 66  must be called f
2b560 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68 72  rom the same thr
2b570 65 61 64 20 61 73 0a 2a 2a 20 74 68 65 20 53 51  ead as.** the SQ
2b580 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20  L function that 
2b590 73 75 70 70 6c 69 65 64 20 74 68 65 20 5b 73 71  supplied the [sq
2b5a0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 5d 20 70 61  lite3_value*] pa
2b5b0 72 61 6d 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 0a  rameters..**.**.
2b5c0 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a  ** INVARIANTS:.*
2b5d0 2a 0a 2a 2a 20 7b 46 31 35 31 30 33 7d 20 54 68  *.** {F15103} Th
2b5e0 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  e [sqlite3_value
2b5f0 5f 62 6c 6f 62 28 56 29 5d 20 69 6e 74 65 72 66  _blob(V)] interf
2b600 61 63 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65  ace converts the
2b610 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72  .**          [pr
2b620 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
2b630 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 56 20  value] object V 
2b640 69 6e 74 6f 20 61 20 62 6c 6f 62 20 61 6e 64 20  into a blob and 
2b650 74 68 65 6e 20 72 65 74 75 72 6e 73 20 61 0a 2a  then returns a.*
2b660 2a 20 20 20 20 20 20 20 20 20 20 70 6f 69 6e 74  *          point
2b670 65 72 20 74 6f 20 74 68 65 20 63 6f 6e 76 65 72  er to the conver
2b680 74 65 64 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a  ted value..**.**
2b690 20 7b 46 31 35 31 30 36 7d 20 54 68 65 20 5b 73   {F15106} The [s
2b6a0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
2b6b0 65 73 28 56 29 5d 20 69 6e 74 65 72 66 61 63 65  es(V)] interface
2b6c0 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20   returns the.** 
2b6d0 20 20 20 20 20 20 20 20 20 6e 75 6d 62 65 72 20           number 
2b6e0 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20  of bytes in the 
2b6f0 62 6c 6f 62 20 6f 72 20 73 74 72 69 6e 67 20 28  blob or string (
2b700 65 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68 65  exclusive of the
2b710 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 7a 65 72  .**          zer
2b720 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 6f 6e 20  o terminator on 
2b730 74 68 65 20 73 74 72 69 6e 67 29 20 74 68 61 74  the string) that
2b740 20 77 61 73 20 72 65 74 75 72 6e 65 64 20 62 79   was returned by
2b750 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
2b760 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c   most recent cal
2b770 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 76 61  l to [sqlite3_va
2b780 6c 75 65 5f 62 6c 6f 62 28 56 29 5d 20 6f 72 0a  lue_blob(V)] or.
2b790 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
2b7a0 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
2b7b0 56 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 35 31  V)]..**.** {F151
2b7c0 30 39 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  09} The [sqlite3
2b7d0 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 56  _value_bytes16(V
2b7e0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  )] interface ret
2b7f0 75 72 6e 73 20 74 68 65 0a 2a 2a 20 20 20 20 20  urns the.**     
2b800 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 62       number of b
2b810 79 74 65 73 20 69 6e 20 74 68 65 20 73 74 72 69  ytes in the stri
2b820 6e 67 20 28 65 78 63 6c 75 73 69 76 65 20 6f 66  ng (exclusive of
2b830 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
2b840 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72   zero terminator
2b850 20 6f 6e 20 74 68 65 20 73 74 72 69 6e 67 29 20   on the string) 
2b860 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65  that was returne
2b870 64 20 62 79 20 74 68 65 0a 2a 2a 20 20 20 20 20  d by the.**     
2b880 20 20 20 20 20 6d 6f 73 74 20 72 65 63 65 6e 74       most recent
2b890 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
2b8a0 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 56  3_value_text16(V
2b8b0 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  )],.**          
2b8c0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  [sqlite3_value_t
2b8d0 65 78 74 31 36 62 65 28 56 29 5d 2c 20 6f 72 20  ext16be(V)], or 
2b8e0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  [sqlite3_value_t
2b8f0 65 78 74 31 36 6c 65 28 56 29 5d 2e 0a 2a 2a 0a  ext16le(V)]..**.
2b900 2a 2a 20 7b 46 31 35 31 31 32 7d 20 54 68 65 20  ** {F15112} The 
2b910 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64  [sqlite3_value_d
2b920 6f 75 62 6c 65 28 56 29 5d 20 69 6e 74 65 72 66  ouble(V)] interf
2b930 61 63 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65  ace converts the
2b940 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72  .**          [pr
2b950 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
2b960 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 56 20  value] object V 
2b970 69 6e 74 6f 20 61 20 66 6c 6f 61 74 69 6e 67 20  into a floating 
2b980 70 6f 69 6e 74 20 76 61 6c 75 65 20 61 6e 64 0a  point value and.
2b990 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  **          retu
2b9a0 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68  rns a copy of th
2b9b0 61 74 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20  at value..**.** 
2b9c0 7b 46 31 35 31 31 35 7d 20 54 68 65 20 5b 73 71  {F15115} The [sq
2b9d0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28  lite3_value_int(
2b9e0 56 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f  V)] interface co
2b9f0 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20  nverts the.**   
2ba00 20 20 20 20 20 20 20 5b 70 72 6f 74 65 63 74 65         [protecte
2ba10 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d  d sqlite3_value]
2ba20 20 6f 62 6a 65 63 74 20 56 20 69 6e 74 6f 20 61   object V into a
2ba30 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69   64-bit signed i
2ba40 6e 74 65 67 65 72 20 61 6e 64 0a 2a 2a 20 20 20  nteger and.**   
2ba50 20 20 20 20 20 20 20 72 65 74 75 72 6e 73 20 74         returns t
2ba60 68 65 20 6c 6f 77 65 72 20 33 32 20 62 69 74 73  he lower 32 bits
2ba70 20 6f 66 20 74 68 61 74 20 69 6e 74 65 67 65 72   of that integer
2ba80 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 35 31 31 38 7d  ..**.** {F15118}
2ba90 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61   The [sqlite3_va
2baa0 6c 75 65 5f 69 6e 74 36 34 28 56 29 5d 20 69 6e  lue_int64(V)] in
2bab0 74 65 72 66 61 63 65 20 63 6f 6e 76 65 72 74 73  terface converts
2bac0 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
2bad0 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69   [protected sqli
2bae0 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63  te3_value] objec
2baf0 74 20 56 20 69 6e 74 6f 20 61 20 36 34 2d 62 69  t V into a 64-bi
2bb00 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
2bb10 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20   and.**         
2bb20 20 72 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20   returns a copy 
2bb30 6f 66 20 74 68 61 74 20 69 6e 74 65 67 65 72 2e  of that integer.
2bb40 0a 2a 2a 0a 2a 2a 20 7b 46 31 35 31 32 31 7d 20  .**.** {F15121} 
2bb50 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c  The [sqlite3_val
2bb60 75 65 5f 74 65 78 74 28 56 29 5d 20 69 6e 74 65  ue_text(V)] inte
2bb70 72 66 61 63 65 20 63 6f 6e 76 65 72 74 73 20 74  rface converts t
2bb80 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  he.**          [
2bb90 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
2bba0 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20  3_value] object 
2bbb0 56 20 69 6e 74 6f 20 61 20 7a 65 72 6f 2d 74 65  V into a zero-te
2bbc0 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 0a  rminated UTF-8 .
2bbd0 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74 72 69  **          stri
2bbe0 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61  ng and returns a
2bbf0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74   pointer to that
2bc00 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b   string..**.** {
2bc10 46 31 35 31 32 34 7d 20 54 68 65 20 5b 73 71 6c  F15124} The [sql
2bc20 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
2bc30 36 28 56 29 5d 20 69 6e 74 65 72 66 61 63 65 20  6(V)] interface 
2bc40 63 6f 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a 20  converts the.** 
2bc50 20 20 20 20 20 20 20 20 20 5b 70 72 6f 74 65 63           [protec
2bc60 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ted sqlite3_valu
2bc70 65 5d 20 6f 62 6a 65 63 74 20 56 20 69 6e 74 6f  e] object V into
2bc80 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74   a zero-terminat
2bc90 65 64 20 32 2d 62 79 74 65 0a 2a 2a 20 20 20 20  ed 2-byte.**    
2bca0 20 20 20 20 20 20 61 6c 69 67 6e 65 64 20 55 54        aligned UT
2bcb0 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79 74 65  F-16 native byte
2bcc0 20 6f 72 64 65 72 0a 2a 2a 20 20 20 20 20 20 20   order.**       
2bcd0 20 20 20 73 74 72 69 6e 67 20 61 6e 64 20 72 65     string and re
2bce0 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
2bcf0 74 6f 20 74 68 61 74 20 73 74 72 69 6e 67 2e 0a  to that string..
2bd00 2a 2a 0a 2a 2a 20 7b 46 31 35 31 32 37 7d 20 54  **.** {F15127} T
2bd10 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  he [sqlite3_valu
2bd20 65 5f 74 65 78 74 31 36 62 65 28 56 29 5d 20 69  e_text16be(V)] i
2bd30 6e 74 65 72 66 61 63 65 20 63 6f 6e 76 65 72 74  nterface convert
2bd40 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  s the.**        
2bd50 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c    [protected sql
2bd60 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65  ite3_value] obje
2bd70 63 74 20 56 20 69 6e 74 6f 20 61 20 7a 65 72 6f  ct V into a zero
2bd80 2d 74 65 72 6d 69 6e 61 74 65 64 20 32 2d 62 79  -terminated 2-by
2bd90 74 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61  te.**          a
2bda0 6c 69 67 6e 65 64 20 55 54 46 2d 31 36 20 62 69  ligned UTF-16 bi
2bdb0 67 2d 65 6e 64 69 61 6e 0a 2a 2a 20 20 20 20 20  g-endian.**     
2bdc0 20 20 20 20 20 73 74 72 69 6e 67 20 61 6e 64 20       string and 
2bdd0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
2bde0 72 20 74 6f 20 74 68 61 74 20 73 74 72 69 6e 67  r to that string
2bdf0 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 35 31 33 30 7d  ..**.** {F15130}
2be00 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61   The [sqlite3_va
2be10 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 56 29 5d  lue_text16le(V)]
2be20 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e 76 65   interface conve
2be30 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  rts the.**      
2be40 20 20 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73      [protected s
2be50 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62  qlite3_value] ob
2be60 6a 65 63 74 20 56 20 69 6e 74 6f 20 61 20 7a 65  ject V into a ze
2be70 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 32 2d  ro-terminated 2-
2be80 62 79 74 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  byte.**         
2be90 20 61 6c 69 67 6e 65 64 20 55 54 46 2d 31 36 20   aligned UTF-16 
2bea0 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 0a 2a 2a  little-endian.**
2beb0 20 20 20 20 20 20 20 20 20 20 73 74 72 69 6e 67            string
2bec0 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70   and returns a p
2bed0 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74 20 73  ointer to that s
2bee0 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31  tring..**.** {F1
2bef0 35 31 33 33 7d 20 54 68 65 20 5b 73 71 6c 69 74  5133} The [sqlit
2bf00 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 56 29  e3_value_type(V)
2bf10 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75  ] interface retu
2bf20 72 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  rns.**          
2bf30 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 4e  one of [SQLITE_N
2bf40 55 4c 4c 5d 2c 20 5b 53 51 4c 49 54 45 5f 49 4e  ULL], [SQLITE_IN
2bf50 54 45 47 45 52 5d 2c 20 5b 53 51 4c 49 54 45 5f  TEGER], [SQLITE_
2bf60 46 4c 4f 41 54 5d 2c 0a 2a 2a 20 20 20 20 20 20  FLOAT],.**      
2bf70 20 20 20 20 5b 53 51 4c 49 54 45 5f 54 45 58 54      [SQLITE_TEXT
2bf80 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 42 4c  ], or [SQLITE_BL
2bf90 4f 42 5d 20 61 73 20 61 70 70 72 6f 70 72 69 61  OB] as appropria
2bfa0 74 65 20 66 6f 72 0a 2a 2a 20 20 20 20 20 20 20  te for.**       
2bfb0 20 20 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f     the [sqlite3_
2bfc0 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 56 2e  value] object V.
2bfd0 0a 2a 2a 0a 2a 2a 20 7b 46 31 35 31 33 36 7d 20  .**.** {F15136} 
2bfe0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c  The [sqlite3_val
2bff0 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28  ue_numeric_type(
2c000 56 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f  V)] interface co
2c010 6e 76 65 72 74 73 0a 2a 2a 20 20 20 20 20 20 20  nverts.**       
2c020 20 20 20 74 68 65 20 5b 70 72 6f 74 65 63 74 65     the [protecte
2c030 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d  d sqlite3_value]
2c040 20 6f 62 6a 65 63 74 20 56 20 69 6e 74 6f 20 65   object V into e
2c050 69 74 68 65 72 20 61 6e 20 69 6e 74 65 67 65 72  ither an integer
2c060 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   or.**          
2c070 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  a floating point
2c080 20 76 61 6c 75 65 20 69 66 20 69 74 20 63 61 6e   value if it can
2c090 20 64 6f 20 73 6f 20 77 69 74 68 6f 75 74 20 6c   do so without l
2c0a0 6f 73 73 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20  oss of.**       
2c0b0 20 20 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20     information, 
2c0c0 61 6e 64 20 72 65 74 75 72 6e 73 20 6f 6e 65 20  and returns one 
2c0d0 6f 66 20 5b 53 51 4c 49 54 45 5f 4e 55 4c 4c 5d  of [SQLITE_NULL]
2c0e0 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53  ,.**          [S
2c0f0 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 5d 2c 20  QLITE_INTEGER], 
2c100 5b 53 51 4c 49 54 45 5f 46 4c 4f 41 54 5d 2c 20  [SQLITE_FLOAT], 
2c110 5b 53 51 4c 49 54 45 5f 54 45 58 54 5d 2c 20 6f  [SQLITE_TEXT], o
2c120 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53  r.**          [S
2c130 51 4c 49 54 45 5f 42 4c 4f 42 5d 20 61 73 20 61  QLITE_BLOB] as a
2c140 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 0a 2a  ppropriate for.*
2c150 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 5b  *          the [
2c160 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
2c170 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20  3_value] object 
2c180 56 20 61 66 74 65 72 20 74 68 65 20 63 6f 6e 76  V after the conv
2c190 65 72 73 69 6f 6e 20 61 74 74 65 6d 70 74 2e 0a  ersion attempt..
2c1a0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
2c1b0 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
2c1c0 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 73 71 6c  3_value_blob(sql
2c1d0 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51  ite3_value*);.SQ
2c1e0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2c1f0 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
2c200 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
2c210 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
2c220 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
2c230 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 76  ytes16(sqlite3_v
2c240 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41  alue*);.SQLITE_A
2c250 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65  PI double sqlite
2c260 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 73  3_value_double(s
2c270 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
2c280 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2c290 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
2c2a0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
2c2b0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  ;.SQLITE_API sql
2c2c0 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74  ite3_int64 sqlit
2c2d0 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 73  e3_value_int64(s
2c2e0 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
2c2f0 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
2c300 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
2c310 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2c320 78 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  xt(sqlite3_value
2c330 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  *);.SQLITE_API c
2c340 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
2c350 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28  e3_value_text16(
2c360 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
2c370 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
2c380 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
2c390 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 73  value_text16le(s
2c3a0 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
2c3b0 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
2c3c0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76   void *sqlite3_v
2c3d0 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28 73 71  alue_text16be(sq
2c3e0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53  lite3_value*);.S
2c3f0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2c400 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
2c410 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
2c420 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
2c430 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e   sqlite3_value_n
2c440 75 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69  umeric_type(sqli
2c450 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 0a 2f 2a  te3_value*);../*
2c460 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 62  .** CAPI3REF: Ob
2c470 74 61 69 6e 20 41 67 67 72 65 67 61 74 65 20 46  tain Aggregate F
2c480 75 6e 63 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20  unction Context 
2c490 7b 46 31 36 32 31 30 7d 0a 2a 2a 0a 2a 2a 20 54  {F16210}.**.** T
2c4a0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
2c4b0 6e 20 6f 66 20 61 67 67 72 65 67 61 74 65 20 53  n of aggregate S
2c4c0 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65  QL functions use
2c4d0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f   this routine to
2c4e0 20 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 61 20 73   allocate.** a s
2c4f0 74 72 75 63 74 75 72 65 20 66 6f 72 20 73 74 6f  tructure for sto
2c500 72 69 6e 67 20 74 68 65 69 72 20 73 74 61 74 65  ring their state
2c510 2e 20 20 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .  .** The first
2c520 20 74 69 6d 65 20 74 68 65 20 73 71 6c 69 74 65   time the sqlite
2c530 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74  3_aggregate_cont
2c540 65 78 74 28 29 20 72 6f 75 74 69 6e 65 20 69 73  ext() routine is
2c550 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 66 6f  .** is called fo
2c560 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 61  r a particular a
2c570 67 67 72 65 67 61 74 65 2c 20 53 51 4c 69 74 65  ggregate, SQLite
2c580 20 61 6c 6c 6f 63 61 74 65 73 20 6e 42 79 74 65   allocates nByte
2c590 73 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 7a  s of memory.** z
2c5a0 65 72 6f 73 20 74 68 61 74 20 6d 65 6d 6f 72 79  eros that memory
2c5b0 2c 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20  , and returns a 
2c5c0 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 0a 2a  pointer to it..*
2c5d0 2a 20 4f 6e 20 73 65 63 6f 6e 64 20 61 6e 64 20  * On second and 
2c5e0 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73  subsequent calls
2c5f0 20 74 6f 20 73 71 6c 69 74 65 33 5f 61 67 67 72   to sqlite3_aggr
2c600 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29 0a  egate_context().
2c610 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20  ** for the same 
2c620 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
2c630 6f 6e 20 69 6e 64 65 78 2c 20 74 68 65 20 73 61  on index, the sa
2c640 6d 65 20 62 75 66 66 65 72 20 69 73 20 72 65 74  me buffer is ret
2c650 75 72 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 69 6d  urned..** The im
2c660 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20  plementation.** 
2c670 6f 66 20 74 68 65 20 61 67 67 72 65 67 61 74 65  of the aggregate
2c680 20 63 61 6e 20 75 73 65 20 74 68 65 20 72 65 74   can use the ret
2c690 75 72 6e 65 64 20 62 75 66 66 65 72 20 74 6f 20  urned buffer to 
2c6a0 61 63 63 75 6d 75 6c 61 74 65 20 64 61 74 61 2e  accumulate data.
2c6b0 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 61 75  .**.** SQLite au
2c6c0 74 6f 6d 61 74 69 63 61 6c 6c 79 20 66 72 65 65  tomatically free
2c6d0 73 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20  s the allocated 
2c6e0 62 75 66 66 65 72 20 77 68 65 6e 20 74 68 65 20  buffer when the 
2c6f0 61 67 67 72 65 67 61 74 65 0a 2a 2a 20 71 75 65  aggregate.** que
2c700 72 79 20 63 6f 6e 63 6c 75 64 65 73 2e 0a 2a 2a  ry concludes..**
2c710 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61  .** The first pa
2c720 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 62  rameter should b
2c730 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  e a copy of the 
2c740 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e  .** [sqlite3_con
2c750 74 65 78 74 20 7c 20 53 51 4c 20 66 75 6e 63 74  text | SQL funct
2c760 69 6f 6e 20 63 6f 6e 74 65 78 74 5d 20 74 68 61  ion context] tha
2c770 74 20 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a  t is the first.*
2c780 2a 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  * parameter to t
2c790 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74  he callback rout
2c7a0 69 6e 65 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ine that impleme
2c7b0 6e 74 73 20 74 68 65 20 61 67 67 72 65 67 61 74  nts the aggregat
2c7c0 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  e.** function..*
2c7d0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2c7e0 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64  e must be called
2c7f0 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 74   from the same t
2c800 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 0a 2a  hread in which.*
2c810 2a 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20  * the aggregate 
2c820 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  SQL function is 
2c830 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49  running..**.** I
2c840 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a  NVARIANTS:.**.**
2c850 20 7b 46 31 36 32 31 31 7d 20 54 68 65 20 66 69   {F16211} The fi
2c860 72 73 74 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  rst invocation o
2c870 66 20 5b 73 71 6c 69 74 65 33 5f 61 67 67 72 65  f [sqlite3_aggre
2c880 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 43 2c 4e  gate_context(C,N
2c890 29 5d 20 66 6f 72 0a 2a 2a 20 20 20 20 20 20 20  )] for.**       
2c8a0 20 20 20 61 20 70 61 72 74 69 63 75 6c 61 72 20     a particular 
2c8b0 69 6e 73 74 61 6e 63 65 20 6f 66 20 61 6e 20 61  instance of an a
2c8c0 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
2c8d0 6e 20 28 66 6f 72 20 61 20 70 61 72 74 69 63 75  n (for a particu
2c8e0 6c 61 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  lar.**          
2c8f0 63 6f 6e 74 65 78 74 20 43 29 20 63 61 75 73 65  context C) cause
2c900 73 20 53 51 4c 69 74 65 20 74 6f 20 61 6c 6c 6f  s SQLite to allo
2c910 63 61 74 69 6f 6e 20 4e 20 62 79 74 65 73 20 6f  cation N bytes o
2c920 66 20 6d 65 6d 6f 72 79 2c 0a 2a 2a 20 20 20 20  f memory,.**    
2c930 20 20 20 20 20 20 7a 65 72 6f 20 74 68 61 74 20        zero that 
2c940 6d 65 6d 6f 72 79 2c 20 61 6e 64 20 72 65 74 75  memory, and retu
2c950 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
2c960 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 65 64  the allocationed
2c970 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6d 65 6d  .**          mem
2c980 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 36 32  ory..**.** {F162
2c990 31 33 7d 20 49 66 20 61 20 6d 65 6d 6f 72 79 20  13} If a memory 
2c9a0 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72  allocation error
2c9b0 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 0a 2a   occurs during.*
2c9c0 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
2c9d0 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
2c9e0 6e 74 65 78 74 28 43 2c 4e 29 5d 20 74 68 65 6e  ntext(C,N)] then
2c9f0 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65   the function re
2ca00 74 75 72 6e 73 20 30 2e 0a 2a 2a 0a 2a 2a 20 7b  turns 0..**.** {
2ca10 46 31 36 32 31 35 7d 20 53 65 63 6f 6e 64 20 61  F16215} Second a
2ca20 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e  nd subsequent in
2ca30 76 6f 63 61 74 69 6f 6e 73 20 6f 66 0a 2a 2a 20  vocations of.** 
2ca40 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
2ca50 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74  3_aggregate_cont
2ca60 65 78 74 28 43 2c 4e 29 5d 20 66 6f 72 20 74 68  ext(C,N)] for th
2ca70 65 20 73 61 6d 65 20 63 6f 6e 74 65 78 74 20 70  e same context p
2ca80 6f 69 6e 74 65 72 20 43 0a 2a 2a 20 20 20 20 20  ointer C.**     
2ca90 20 20 20 20 20 69 67 6e 6f 72 65 20 74 68 65 20       ignore the 
2caa0 4e 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64 20  N parameter and 
2cab0 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  return a pointer
2cac0 20 74 6f 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20   to the same.** 
2cad0 20 20 20 20 20 20 20 20 20 62 6c 6f 63 6b 20 6f           block o
2cae0 66 20 6d 65 6d 6f 72 79 20 72 65 74 75 72 6e 65  f memory returne
2caf0 64 20 62 79 20 74 68 65 20 66 69 72 73 74 20 69  d by the first i
2cb00 6e 76 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  nvocation..**.**
2cb10 20 7b 46 31 36 32 31 37 7d 20 54 68 65 20 6d 65   {F16217} The me
2cb20 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 65 64 20 62  mory allocated b
2cb30 79 20 5b 73 71 6c 69 74 65 33 5f 61 67 67 72 65  y [sqlite3_aggre
2cb40 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 43 2c 4e  gate_context(C,N
2cb50 29 5d 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20  )] is.**        
2cb60 20 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20    automatically 
2cb70 66 72 65 65 64 20 6f 6e 20 74 68 65 20 6e 65 78  freed on the nex
2cb80 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  t call to [sqlit
2cb90 65 33 5f 72 65 73 65 74 28 29 5d 0a 2a 2a 20 20  e3_reset()].**  
2cba0 20 20 20 20 20 20 20 20 6f 72 20 5b 73 71 6c 69          or [sqli
2cbb0 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20  te3_finalize()] 
2cbc0 66 6f 72 20 74 68 65 20 5b 70 72 65 70 61 72 65  for the [prepare
2cbd0 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 63 6f 6e  d statement] con
2cbe0 74 61 69 6e 69 6e 67 0a 2a 2a 20 20 20 20 20 20  taining.**      
2cbf0 20 20 20 20 74 68 65 20 61 67 67 72 65 67 61 74      the aggregat
2cc00 65 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 6f 63  e function assoc
2cc10 69 61 74 65 64 20 77 69 74 68 20 63 6f 6e 74 65  iated with conte
2cc20 78 74 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  xt C..*/.SQLITE_
2cc30 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  API void *sqlite
2cc40 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74  3_aggregate_cont
2cc50 65 78 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ext(sqlite3_cont
2cc60 65 78 74 2a 2c 20 69 6e 74 20 6e 42 79 74 65 73  ext*, int nBytes
2cc70 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
2cc80 45 46 3a 20 55 73 65 72 20 44 61 74 61 20 46 6f  EF: User Data Fo
2cc90 72 20 46 75 6e 63 74 69 6f 6e 73 20 7b 46 31 36  r Functions {F16
2cca0 32 34 30 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  240}.**.** The s
2ccb0 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
2ccc0 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  () interface ret
2ccd0 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 0a 2a  urns a copy of.*
2cce0 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 68  * the pointer th
2ccf0 61 74 20 77 61 73 20 74 68 65 20 70 55 73 65 72  at was the pUser
2cd00 44 61 74 61 20 70 61 72 61 6d 65 74 65 72 20 28  Data parameter (
2cd10 74 68 65 20 35 74 68 20 70 61 72 61 6d 65 74 65  the 5th paramete
2cd20 72 29 0a 2a 2a 20 6f 66 20 74 68 65 20 74 68 65  r).** of the the
2cd30 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
2cd40 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 0a 2a 2a 20  _function()].** 
2cd50 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 65  and [sqlite3_cre
2cd60 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29  ate_function16()
2cd70 5d 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20  ] routines that 
2cd80 6f 72 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20 72 65  originally.** re
2cd90 67 69 73 74 65 72 65 64 20 74 68 65 20 61 70 70  gistered the app
2cda0 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64  lication defined
2cdb0 20 66 75 6e 63 74 69 6f 6e 2e 20 7b 45 4e 44 7d   function. {END}
2cdc0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
2cdd0 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c  ine must be call
2cde0 65 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65  ed from the same
2cdf0 20 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 68   thread in which
2ce00 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74  .** the applicat
2ce10 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
2ce20 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e  tion is running.
2ce30 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54  .**.** INVARIANT
2ce40 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31 36 32 34 33  S:.**.** {F16243
2ce50 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 75  } The [sqlite3_u
2ce60 73 65 72 5f 64 61 74 61 28 43 29 5d 20 69 6e 74  ser_data(C)] int
2ce70 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61  erface returns a
2ce80 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20   copy of the.** 
2ce90 20 20 20 20 20 20 20 20 20 50 20 70 6f 69 6e 74           P point
2cea0 65 72 20 66 72 6f 6d 20 74 68 65 20 5b 73 71 6c  er from the [sql
2ceb0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
2cec0 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 50 2c 46  tion(D,X,N,E,P,F
2ced0 2c 53 2c 4c 29 5d 0a 2a 2a 20 20 20 20 20 20 20  ,S,L)].**       
2cee0 20 20 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 63     or [sqlite3_c
2cef0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36  reate_function16
2cf00 28 44 2c 58 2c 4e 2c 45 2c 50 2c 46 2c 53 2c 4c  (D,X,N,E,P,F,S,L
2cf10 29 5d 20 63 61 6c 6c 20 74 68 61 74 0a 2a 2a 20  )] call that.** 
2cf20 20 20 20 20 20 20 20 20 20 72 65 67 69 73 74 65           registe
2cf30 72 65 64 20 74 68 65 20 53 51 4c 20 66 75 6e 63  red the SQL func
2cf40 74 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20  tion associated 
2cf50 77 69 74 68 20 0a 2a 2a 20 20 20 20 20 20 20 20  with .**        
2cf60 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65    [sqlite3_conte
2cf70 78 74 5d 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54 45  xt] C..*/.SQLITE
2cf80 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
2cf90 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71 6c  e3_user_data(sql
2cfa0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a  ite3_context*);.
2cfb0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
2cfc0 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63   Database Connec
2cfd0 74 69 6f 6e 20 46 6f 72 20 46 75 6e 63 74 69 6f  tion For Functio
2cfe0 6e 73 20 7b 46 31 36 32 35 30 7d 0a 2a 2a 0a 2a  ns {F16250}.**.*
2cff0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f  * The sqlite3_co
2d000 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
2d010 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75  ) interface retu
2d020 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a  rns a copy of.**
2d030 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20   the pointer to 
2d040 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  the [database co
2d050 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 68 65 20 31  nnection] (the 1
2d060 73 74 20 70 61 72 61 6d 65 74 65 72 29 0a 2a 2a  st parameter).**
2d070 20 6f 66 20 74 68 65 20 74 68 65 20 5b 73 71 6c   of the the [sql
2d080 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
2d090 74 69 6f 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b  tion()].** and [
2d0a0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
2d0b0 75 6e 63 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75  unction16()] rou
2d0c0 74 69 6e 65 73 20 74 68 61 74 20 6f 72 69 67 69  tines that origi
2d0d0 6e 61 6c 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65  nally.** registe
2d0e0 72 65 64 20 74 68 65 20 61 70 70 6c 69 63 61 74  red the applicat
2d0f0 69 6f 6e 20 64 65 66 69 6e 65 64 20 66 75 6e 63  ion defined func
2d100 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41  tion..**.** INVA
2d110 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46  RIANTS:.**.** {F
2d120 31 36 32 35 33 7d 20 54 68 65 20 5b 73 71 6c 69  16253} The [sqli
2d130 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
2d140 61 6e 64 6c 65 28 43 29 5d 20 69 6e 74 65 72 66  andle(C)] interf
2d150 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 63 6f  ace returns a co
2d160 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20  py of the.**    
2d170 20 20 20 20 20 20 44 20 70 6f 69 6e 74 65 72 20        D pointer 
2d180 66 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74 65  from the [sqlite
2d190 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
2d1a0 6e 28 44 2c 58 2c 4e 2c 45 2c 50 2c 46 2c 53 2c  n(D,X,N,E,P,F,S,
2d1b0 4c 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  L)].**          
2d1c0 6f 72 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61  or [sqlite3_crea
2d1d0 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 44 2c  te_function16(D,
2d1e0 58 2c 4e 2c 45 2c 50 2c 46 2c 53 2c 4c 29 5d 20  X,N,E,P,F,S,L)] 
2d1f0 63 61 6c 6c 20 74 68 61 74 0a 2a 2a 20 20 20 20  call that.**    
2d200 20 20 20 20 20 20 72 65 67 69 73 74 65 72 65 64        registered
2d210 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f   the SQL functio
2d220 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  n associated wit
2d230 68 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  h .**          [
2d240 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5d  sqlite3_context]
2d250 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50   C..*/.SQLITE_AP
2d260 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74  I sqlite3 *sqlit
2d270 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
2d280 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e  ndle(sqlite3_con
2d290 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  text*);../*.** C
2d2a0 41 50 49 33 52 45 46 3a 20 46 75 6e 63 74 69 6f  API3REF: Functio
2d2b0 6e 20 41 75 78 69 6c 69 61 72 79 20 44 61 74 61  n Auxiliary Data
2d2c0 20 7b 46 31 36 32 37 30 7d 0a 2a 2a 0a 2a 2a 20   {F16270}.**.** 
2d2d0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77  The following tw
2d2e0 6f 20 66 75 6e 63 74 69 6f 6e 73 20 6d 61 79 20  o functions may 
2d2f0 62 65 20 75 73 65 64 20 62 79 20 73 63 61 6c 61  be used by scala
2d300 72 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20  r SQL functions 
2d310 74 6f 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 20  to.** associate 
2d320 6d 65 74 61 2d 64 61 74 61 20 77 69 74 68 20 61  meta-data with a
2d330 72 67 75 6d 65 6e 74 20 76 61 6c 75 65 73 2e 20  rgument values. 
2d340 49 66 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75  If the same valu
2d350 65 20 69 73 20 70 61 73 73 65 64 20 74 6f 0a 2a  e is passed to.*
2d360 2a 20 6d 75 6c 74 69 70 6c 65 20 69 6e 76 6f 63  * multiple invoc
2d370 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61  ations of the sa
2d380 6d 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  me SQL function 
2d390 64 75 72 69 6e 67 20 71 75 65 72 79 20 65 78 65  during query exe
2d3a0 63 75 74 69 6f 6e 2c 20 75 6e 64 65 72 0a 2a 2a  cution, under.**
2d3b0 20 73 6f 6d 65 20 63 69 72 63 75 6d 73 74 61 6e   some circumstan
2d3c0 63 65 73 20 74 68 65 20 61 73 73 6f 63 69 61 74  ces the associat
2d3d0 65 64 20 6d 65 74 61 2d 64 61 74 61 20 6d 61 79  ed meta-data may
2d3e0 20 62 65 20 70 72 65 73 65 72 76 65 64 2e 20 54   be preserved. T
2d3f0 68 69 73 20 6d 61 79 0a 2a 2a 20 62 65 20 75 73  his may.** be us
2d400 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c  ed, for example,
2d410 20 74 6f 20 61 64 64 20 61 20 72 65 67 75 6c 61   to add a regula
2d420 72 2d 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 74  r-expression mat
2d430 63 68 69 6e 67 20 73 63 61 6c 61 72 0a 2a 2a 20  ching scalar.** 
2d440 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 63 6f  function. The co
2d450 6d 70 69 6c 65 64 20 76 65 72 73 69 6f 6e 20 6f  mpiled version o
2d460 66 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78  f the regular ex
2d470 70 72 65 73 73 69 6f 6e 20 69 73 20 73 74 6f 72  pression is stor
2d480 65 64 20 61 73 0a 2a 2a 20 6d 65 74 61 2d 64 61  ed as.** meta-da
2d490 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ta associated wi
2d4a0 74 68 20 74 68 65 20 53 51 4c 20 76 61 6c 75 65  th the SQL value
2d4b0 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 72   passed as the r
2d4c0 65 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f  egular expressio
2d4d0 6e 0a 2a 2a 20 70 61 74 74 65 72 6e 2e 20 20 54  n.** pattern.  T
2d4e0 68 65 20 63 6f 6d 70 69 6c 65 64 20 72 65 67 75  he compiled regu
2d4f0 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 63  lar expression c
2d500 61 6e 20 62 65 20 72 65 75 73 65 64 20 6f 6e 20  an be reused on 
2d510 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 69 6e 76 6f  multiple.** invo
2d520 63 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73  cations of the s
2d530 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 73 6f 20  ame function so 
2d540 74 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e 61  that the origina
2d550 6c 20 70 61 74 74 65 72 6e 20 73 74 72 69 6e 67  l pattern string
2d560 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65  .** does not nee
2d570 64 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c  d to be recompil
2d580 65 64 20 6f 6e 20 65 61 63 68 20 69 6e 76 6f 63  ed on each invoc
2d590 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ation..**.** The
2d5a0 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78   sqlite3_get_aux
2d5b0 64 61 74 61 28 29 20 69 6e 74 65 72 66 61 63 65  data() interface
2d5c0 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
2d5d0 65 72 20 74 6f 20 74 68 65 20 6d 65 74 61 2d 64  er to the meta-d
2d5e0 61 74 61 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65  ata.** associate
2d5f0 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33  d by the sqlite3
2d600 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 66  _set_auxdata() f
2d610 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65  unction with the
2d620 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 0a 2a 2a   Nth argument.**
2d630 20 76 61 6c 75 65 20 74 6f 20 74 68 65 20 61 70   value to the ap
2d640 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
2d650 64 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 49  d function..** I
2d660 66 20 6e 6f 20 6d 65 74 61 2d 64 61 74 61 20 68  f no meta-data h
2d670 61 73 20 62 65 65 6e 20 65 76 65 72 20 62 65 65  as been ever bee
2d680 6e 20 73 65 74 20 66 6f 72 20 74 68 65 20 4e 74  n set for the Nt
2d690 68 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6f 66  h.** argument of
2d6a0 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2c 20 6f   the function, o
2d6b0 72 20 69 66 20 74 68 65 20 63 6f 6f 72 65 73 70  r if the cooresp
2d6c0 6f 6e 64 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  onding function 
2d6d0 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 68 61 73  parameter.** has
2d6e0 20 63 68 61 6e 67 65 64 20 73 69 6e 63 65 20 74   changed since t
2d6f0 68 65 20 6d 65 74 61 2d 64 61 74 61 20 77 61 73  he meta-data was
2d700 20 73 65 74 2c 20 74 68 65 6e 20 73 71 6c 69 74   set, then sqlit
2d710 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 29  e3_get_auxdata()
2d720 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 4e 55  .** returns a NU
2d730 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a  LL pointer..**.*
2d740 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 65  * The sqlite3_se
2d750 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e 74 65  t_auxdata() inte
2d760 72 66 61 63 65 20 73 61 76 65 73 20 74 68 65 20  rface saves the 
2d770 6d 65 74 61 2d 64 61 74 61 0a 2a 2a 20 70 6f 69  meta-data.** poi
2d780 6e 74 65 64 20 74 6f 20 62 79 20 69 74 73 20 33  nted to by its 3
2d790 72 64 20 70 61 72 61 6d 65 74 65 72 20 61 73 20  rd parameter as 
2d7a0 74 68 65 20 6d 65 74 61 2d 64 61 74 61 20 66 6f  the meta-data fo
2d7b0 72 20 74 68 65 20 4e 2d 74 68 0a 2a 2a 20 61 72  r the N-th.** ar
2d7c0 67 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 70  gument of the ap
2d7d0 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
2d7e0 64 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 75 62  d function.  Sub
2d7f0 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73  sequent.** calls
2d800 20 74 6f 20 73 71 6c 69 74 65 33 5f 67 65 74 5f   to sqlite3_get_
2d810 61 75 78 64 61 74 61 28 29 20 6d 69 67 68 74 20  auxdata() might 
2d820 72 65 74 75 72 6e 20 74 68 69 73 20 64 61 74 61  return this data
2d830 2c 20 69 66 20 69 74 20 68 61 73 0a 2a 2a 20 6e  , if it has.** n
2d840 6f 74 20 62 65 65 6e 20 64 65 73 74 72 6f 79 65  ot been destroye
2d850 64 2e 20 0a 2a 2a 20 49 66 20 69 74 20 69 73 20  d. .** If it is 
2d860 6e 6f 74 20 4e 55 4c 4c 2c 20 53 51 4c 69 74 65  not NULL, SQLite
2d870 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65   will invoke the
2d880 20 64 65 73 74 72 75 63 74 6f 72 20 0a 2a 2a 20   destructor .** 
2d890 66 75 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 62  function given b
2d8a0 79 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65  y the 4th parame
2d8b0 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 73  ter to sqlite3_s
2d8c0 65 74 5f 61 75 78 64 61 74 61 28 29 20 6f 6e 0a  et_auxdata() on.
2d8d0 2a 2a 20 74 68 65 20 6d 65 74 61 2d 64 61 74 61  ** the meta-data
2d8e0 20 77 68 65 6e 20 74 68 65 20 63 6f 72 72 65 73   when the corres
2d8f0 70 6f 6e 64 69 6e 67 20 66 75 6e 63 74 69 6f 6e  ponding function
2d900 20 70 61 72 61 6d 65 74 65 72 20 63 68 61 6e 67   parameter chang
2d910 65 73 0a 2a 2a 20 6f 72 20 77 68 65 6e 20 74 68  es.** or when th
2d920 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
2d930 63 6f 6d 70 6c 65 74 65 73 2c 20 77 68 69 63 68  completes, which
2d940 65 76 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74  ever comes first
2d950 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69  ..**.** SQLite i
2d960 73 20 66 72 65 65 20 74 6f 20 63 61 6c 6c 20 74  s free to call t
2d970 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 61 6e  he destructor an
2d980 64 20 64 72 6f 70 20 6d 65 74 61 2d 64 61 74 61  d drop meta-data
2d990 20 6f 6e 0a 2a 2a 20 61 6e 79 20 70 61 72 61 6d   on.** any param
2d9a0 65 74 65 72 20 6f 66 20 61 6e 79 20 66 75 6e 63  eter of any func
2d9b0 74 69 6f 6e 20 61 74 20 61 6e 79 20 74 69 6d 65  tion at any time
2d9c0 2e 20 20 54 68 65 20 6f 6e 6c 79 20 67 75 61 72  .  The only guar
2d9d0 61 6e 74 65 65 0a 2a 2a 20 69 73 20 74 68 61 74  antee.** is that
2d9e0 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20   the destructor 
2d9f0 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 62  will be called b
2da00 65 66 6f 72 65 20 74 68 65 20 6d 65 74 61 64 61  efore the metada
2da10 74 61 20 69 73 0a 2a 2a 20 64 72 6f 70 70 65 64  ta is.** dropped
2da20 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 70 72 61 63 74  ..**.** In pract
2da30 69 63 65 2c 20 6d 65 74 61 2d 64 61 74 61 20 69  ice, meta-data i
2da40 73 20 70 72 65 73 65 72 76 65 64 20 62 65 74 77  s preserved betw
2da50 65 65 6e 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c  een function cal
2da60 6c 73 20 66 6f 72 0a 2a 2a 20 65 78 70 72 65 73  ls for.** expres
2da70 73 69 6f 6e 73 20 74 68 61 74 20 61 72 65 20 63  sions that are c
2da80 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f 6d 70 69  onstant at compi
2da90 6c 65 20 74 69 6d 65 2e 20 54 68 69 73 20 69 6e  le time. This in
2daa0 63 6c 75 64 65 73 20 6c 69 74 65 72 61 6c 0a 2a  cludes literal.*
2dab0 2a 20 76 61 6c 75 65 73 20 61 6e 64 20 53 51 4c  * values and SQL
2dac0 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2a 0a 2a   variables..**.*
2dad0 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
2dae0 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20   must be called 
2daf0 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68  from the same th
2db00 72 65 61 64 20 69 6e 20 77 68 69 63 68 0a 2a 2a  read in which.**
2db10 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f   the SQL functio
2db20 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a  n is running..**
2db30 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a  .** INVARIANTS:.
2db40 2a 2a 0a 2a 2a 20 7b 46 31 36 32 37 32 7d 20 54  **.** {F16272} T
2db50 68 65 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f  he [sqlite3_get_
2db60 61 75 78 64 61 74 61 28 43 2c 4e 29 5d 20 69 6e  auxdata(C,N)] in
2db70 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
2db80 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 20 20 20  a pointer.**    
2db90 20 20 20 20 20 20 74 6f 20 6d 65 74 61 64 61 74        to metadat
2dba0 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  a associated wit
2dbb0 68 20 74 68 65 20 4e 74 68 20 70 61 72 61 6d 65  h the Nth parame
2dbc0 74 65 72 20 6f 66 20 74 68 65 20 53 51 4c 20 66  ter of the SQL f
2dbd0 75 6e 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20  unction.**      
2dbe0 20 20 20 20 77 68 6f 73 65 20 63 6f 6e 74 65 78      whose contex
2dbf0 74 20 69 73 20 43 2c 20 6f 72 20 4e 55 4c 4c 20  t is C, or NULL 
2dc00 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d  if there is no m
2dc10 65 74 61 64 61 74 61 20 61 73 73 6f 63 69 61 74  etadata associat
2dc20 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77  ed.**          w
2dc30 69 74 68 20 74 68 61 74 20 70 61 72 61 6d 65 74  ith that paramet
2dc40 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 36 32 37  er..**.** {F1627
2dc50 34 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  4} The [sqlite3_
2dc60 73 65 74 5f 61 75 78 64 61 74 61 28 43 2c 4e 2c  set_auxdata(C,N,
2dc70 50 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 65 20  P,D)] interface 
2dc80 61 73 73 69 67 6e 73 20 61 20 6d 65 74 61 64 61  assigns a metada
2dc90 74 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70  ta.**          p
2dca0 6f 69 6e 74 65 72 20 50 20 74 6f 20 74 68 65 20  ointer P to the 
2dcb0 4e 74 68 20 70 61 72 61 6d 65 74 65 72 20 6f 66  Nth parameter of
2dcc0 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f   the SQL functio
2dcd0 6e 20 77 69 74 68 20 63 6f 6e 74 65 78 74 0a 2a  n with context.*
2dce0 2a 20 20 20 20 20 20 20 20 20 20 43 2e 0a 2a 2a  *          C..**
2dcf0 0a 2a 2a 20 7b 46 31 36 32 37 36 7d 20 53 51 4c  .** {F16276} SQL
2dd00 69 74 65 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20  ite will invoke 
2dd10 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 44  the destructor D
2dd20 20 77 69 74 68 20 61 20 73 69 6e 67 6c 65 20 61   with a single a
2dd30 72 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 20  rgument.**      
2dd40 20 20 20 20 77 68 69 63 68 20 69 73 20 74 68 65      which is the
2dd50 20 6d 65 74 61 64 61 74 61 20 70 6f 69 6e 74 65   metadata pointe
2dd60 72 20 50 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20  r P following a 
2dd70 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 20  call to.**      
2dd80 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 73 65 74      [sqlite3_set
2dd90 5f 61 75 78 64 61 74 61 28 43 2c 4e 2c 50 2c 44  _auxdata(C,N,P,D
2dda0 29 5d 20 77 68 65 6e 20 53 51 4c 69 74 65 20 63  )] when SQLite c
2ddb0 65 61 73 65 73 20 74 6f 20 68 6f 6c 64 0a 2a 2a  eases to hold.**
2ddc0 20 20 20 20 20 20 20 20 20 20 74 68 65 20 6d 65            the me
2ddd0 74 61 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 7b 46  tadata..**.** {F
2dde0 31 36 32 37 37 7d 20 53 51 4c 69 74 65 20 63 65  16277} SQLite ce
2ddf0 61 73 65 73 20 74 6f 20 68 6f 6c 64 20 6d 65 74  ases to hold met
2de00 61 64 61 74 61 20 66 6f 72 20 61 6e 20 53 51 4c  adata for an SQL
2de10 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d 65   function parame
2de20 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ter.**          
2de30 77 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 6f  when the value o
2de40 66 20 74 68 61 74 20 70 61 72 61 6d 65 74 65 72  f that parameter
2de50 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 0a 2a 2a 20   changes..**.** 
2de60 7b 46 31 36 32 37 38 7d 20 57 68 65 6e 20 5b 73  {F16278} When [s
2de70 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61  qlite3_set_auxda
2de80 74 61 28 43 2c 4e 2c 50 2c 44 29 5d 20 69 73 20  ta(C,N,P,D)] is 
2de90 69 6e 76 6f 6b 65 64 2c 20 74 68 65 20 64 65 73  invoked, the des
2dea0 74 72 75 63 74 6f 72 0a 2a 2a 20 20 20 20 20 20  tructor.**      
2deb0 20 20 20 20 69 73 20 63 61 6c 6c 65 64 20 66 6f      is called fo
2dec0 72 20 61 6e 79 20 70 72 69 6f 72 20 6d 65 74 61  r any prior meta
2ded0 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20  data associated 
2dee0 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 66 75  with the same fu
2def0 6e 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20  nction.**       
2df00 20 20 20 63 6f 6e 74 65 78 74 20 43 20 61 6e 64     context C and
2df10 20 70 61 72 61 6d 65 74 65 72 20 4e 2e 0a 2a 2a   parameter N..**
2df20 0a 2a 2a 20 7b 46 31 36 32 37 39 7d 20 53 51 4c  .** {F16279} SQL
2df30 69 74 65 20 77 69 6c 6c 20 63 61 6c 6c 20 64 65  ite will call de
2df40 73 74 72 75 63 74 6f 72 73 20 66 6f 72 20 61 6e  structors for an
2df50 79 20 6d 65 74 61 64 61 74 61 20 69 74 20 69 73  y metadata it is
2df60 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 20 20 20 20   holding.**     
2df70 20 20 20 20 20 69 6e 20 61 20 70 61 72 74 69 63       in a partic
2df80 75 6c 61 72 20 5b 70 72 65 70 61 72 65 64 20 73  ular [prepared s
2df90 74 61 74 65 6d 65 6e 74 5d 20 53 20 77 68 65 6e  tatement] S when
2dfa0 20 65 69 74 68 65 72 0a 2a 2a 20 20 20 20 20 20   either.**      
2dfb0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73      [sqlite3_res
2dfc0 65 74 28 53 29 5d 20 6f 72 20 5b 73 71 6c 69 74  et(S)] or [sqlit
2dfd0 65 33 5f 66 69 6e 61 6c 69 7a 65 28 53 29 5d 20  e3_finalize(S)] 
2dfe0 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51  is called..*/.SQ
2dff0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73  LITE_API void *s
2e000 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61  qlite3_get_auxda
2e010 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ta(sqlite3_conte
2e020 78 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c  xt*, int N);.SQL
2e030 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
2e040 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61  ite3_set_auxdata
2e050 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2e060 2a 2c 20 69 6e 74 20 4e 2c 20 76 6f 69 64 2a 2c  *, int N, void*,
2e070 20 76 6f 69 64 20 28 2a 29 28 76 6f 69 64 2a 29   void (*)(void*)
2e080 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  );.../*.** CAPI3
2e090 52 45 46 3a 20 43 6f 6e 73 74 61 6e 74 73 20 44  REF: Constants D
2e0a0 65 66 69 6e 69 6e 67 20 53 70 65 63 69 61 6c 20  efining Special 
2e0b0 44 65 73 74 72 75 63 74 6f 72 20 42 65 68 61 76  Destructor Behav
2e0c0 69 6f 72 20 7b 46 31 30 32 38 30 7d 0a 2a 2a 0a  ior {F10280}.**.
2e0d0 2a 2a 20 54 68 65 73 65 20 61 72 65 20 73 70 65  ** These are spe
2e0e0 63 69 61 6c 20 76 61 6c 75 65 20 66 6f 72 20 74  cial value for t
2e0f0 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 74 68  he destructor th
2e100 61 74 20 69 73 20 70 61 73 73 65 64 20 69 6e 20  at is passed in 
2e110 61 73 20 74 68 65 0a 2a 2a 20 66 69 6e 61 6c 20  as the.** final 
2e120 61 72 67 75 6d 65 6e 74 20 74 6f 20 72 6f 75 74  argument to rout
2e130 69 6e 65 73 20 6c 69 6b 65 20 5b 73 71 6c 69 74  ines like [sqlit
2e140 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 29  e3_result_blob()
2e150 5d 2e 20 20 49 66 20 74 68 65 20 64 65 73 74 72  ].  If the destr
2e160 75 63 74 6f 72 0a 2a 2a 20 61 72 67 75 6d 65 6e  uctor.** argumen
2e170 74 20 69 73 20 53 51 4c 49 54 45 5f 53 54 41 54  t is SQLITE_STAT
2e180 49 43 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61  IC, it means tha
2e190 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 70 6f  t the content po
2e1a0 69 6e 74 65 72 20 69 73 20 63 6f 6e 73 74 61 6e  inter is constan
2e1b0 74 0a 2a 2a 20 61 6e 64 20 77 69 6c 6c 20 6e 65  t.** and will ne
2e1c0 76 65 72 20 63 68 61 6e 67 65 2e 20 20 49 74 20  ver change.  It 
2e1d0 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f  does not need to
2e1e0 20 62 65 20 64 65 73 74 72 6f 79 65 64 2e 20 20   be destroyed.  
2e1f0 54 68 65 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 54  The .** SQLITE_T
2e200 52 41 4e 53 49 45 4e 54 20 76 61 6c 75 65 20 6d  RANSIENT value m
2e210 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 63 6f  eans that the co
2e220 6e 74 65 6e 74 20 77 69 6c 6c 20 6c 69 6b 65 6c  ntent will likel
2e230 79 20 63 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 74  y change in.** t
2e240 68 65 20 6e 65 61 72 20 66 75 74 75 72 65 20 61  he near future a
2e250 6e 64 20 74 68 61 74 20 53 51 4c 69 74 65 20 73  nd that SQLite s
2e260 68 6f 75 6c 64 20 6d 61 6b 65 20 69 74 73 20 6f  hould make its o
2e270 77 6e 20 70 72 69 76 61 74 65 20 63 6f 70 79 20  wn private copy 
2e280 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e  of.** the conten
2e290 74 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  t before returni
2e2a0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 79  ng..**.** The ty
2e2b0 70 65 64 65 66 20 69 73 20 6e 65 63 65 73 73 61  pedef is necessa
2e2c0 72 79 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e  ry to work aroun
2e2d0 64 20 70 72 6f 62 6c 65 6d 73 20 69 6e 20 63 65  d problems in ce
2e2e0 72 74 61 69 6e 0a 2a 2a 20 43 2b 2b 20 63 6f 6d  rtain.** C++ com
2e2f0 70 69 6c 65 72 73 2e 20 20 53 65 65 20 74 69 63  pilers.  See tic
2e300 6b 65 74 20 23 32 31 39 31 2e 0a 2a 2f 0a 74 79  ket #2191..*/.ty
2e310 70 65 64 65 66 20 76 6f 69 64 20 28 2a 73 71 6c  pedef void (*sql
2e320 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f  ite3_destructor_
2e330 74 79 70 65 29 28 76 6f 69 64 2a 29 3b 0a 23 64  type)(void*);.#d
2e340 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41  efine SQLITE_STA
2e350 54 49 43 20 20 20 20 20 20 28 28 73 71 6c 69 74  TIC      ((sqlit
2e360 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79  e3_destructor_ty
2e370 70 65 29 30 29 0a 23 64 65 66 69 6e 65 20 53 51  pe)0).#define SQ
2e380 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 20  LITE_TRANSIENT  
2e390 20 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72   ((sqlite3_destr
2e3a0 75 63 74 6f 72 5f 74 79 70 65 29 2d 31 29 0a 0a  uctor_type)-1)..
2e3b0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
2e3c0 53 65 74 74 69 6e 67 20 54 68 65 20 52 65 73 75  Setting The Resu
2e3d0 6c 74 20 4f 66 20 41 6e 20 53 51 4c 20 46 75 6e  lt Of An SQL Fun
2e3e0 63 74 69 6f 6e 20 7b 46 31 36 34 30 30 7d 0a 2a  ction {F16400}.*
2e3f0 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
2e400 6e 65 73 20 61 72 65 20 75 73 65 64 20 62 79 20  nes are used by 
2e410 74 68 65 20 78 46 75 6e 63 20 6f 72 20 78 46 69  the xFunc or xFi
2e420 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 74 68  nal callbacks th
2e430 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20  at.** implement 
2e440 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e  SQL functions an
2e450 64 20 61 67 67 72 65 67 61 74 65 73 2e 20 20 53  d aggregates.  S
2e460 65 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63  ee.** [sqlite3_c
2e470 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29  reate_function()
2e480 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63  ] and [sqlite3_c
2e490 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36  reate_function16
2e4a0 28 29 5d 0a 2a 2a 20 66 6f 72 20 61 64 64 69 74  ()].** for addit
2e4b0 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
2e4c0 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66  n..**.** These f
2e4d0 75 6e 63 74 69 6f 6e 73 20 77 6f 72 6b 20 76 65  unctions work ve
2e4e0 72 79 20 6d 75 63 68 20 6c 69 6b 65 20 74 68 65  ry much like the
2e4f0 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69   .** [sqlite3_bi
2e500 6e 64 5f 62 6c 6f 62 20 7c 20 73 71 6c 69 74 65  nd_blob | sqlite
2e510 33 5f 62 69 6e 64 5f 2a 5d 20 66 61 6d 69 6c 79  3_bind_*] family
2e520 20 6f 66 20 66 75 6e 63 74 69 6f 6e 73 20 75 73   of functions us
2e530 65 64 0a 2a 2a 20 74 6f 20 62 69 6e 64 20 76 61  ed.** to bind va
2e540 6c 75 65 73 20 74 6f 20 68 6f 73 74 20 70 61 72  lues to host par
2e550 61 6d 65 74 65 72 73 20 69 6e 20 70 72 65 70 61  ameters in prepa
2e560 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  red statements..
2e570 2a 2a 20 52 65 66 65 72 20 74 6f 20 74 68 65 0a  ** Refer to the.
2e580 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  ** [sqlite3_bind
2e590 5f 62 6c 6f 62 20 7c 20 73 71 6c 69 74 65 33 5f  _blob | sqlite3_
2e5a0 62 69 6e 64 5f 2a 20 64 6f 63 75 6d 65 6e 74 61  bind_* documenta
2e5b0 74 69 6f 6e 5d 20 66 6f 72 0a 2a 2a 20 61 64 64  tion] for.** add
2e5c0 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
2e5d0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ion..**.** The s
2e5e0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c  qlite3_result_bl
2e5f0 6f 62 28 29 20 69 6e 74 65 72 66 61 63 65 20 73  ob() interface s
2e600 65 74 73 20 74 68 65 20 72 65 73 75 6c 74 20 66  ets the result f
2e610 72 6f 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63  rom.** an applic
2e620 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 66 75  ation defined fu
2e630 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65  nction to be the
2e640 20 42 4c 4f 42 20 77 68 6f 73 65 20 63 6f 6e 74   BLOB whose cont
2e650 65 6e 74 20 69 73 20 70 6f 69 6e 74 65 64 0a 2a  ent is pointed.*
2e660 2a 20 74 6f 20 62 79 20 74 68 65 20 73 65 63 6f  * to by the seco
2e670 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64  nd parameter and
2e680 20 77 68 69 63 68 20 69 73 20 4e 20 62 79 74 65   which is N byte
2e690 73 20 6c 6f 6e 67 20 77 68 65 72 65 20 4e 20 69  s long where N i
2e6a0 73 20 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 70  s the.** third p
2e6b0 61 72 61 6d 65 74 65 72 2e 20 0a 2a 2a 20 54 68  arameter. .** Th
2e6c0 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
2e6d0 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 69 6e 65 72  _zeroblob() iner
2e6e0 66 61 63 65 73 20 73 65 74 20 74 68 65 20 72 65  faces set the re
2e6f0 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61  sult of.** the a
2e700 70 70 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e  pplication defin
2e710 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62  ed function to b
2e720 65 20 61 20 42 4c 4f 42 20 63 6f 6e 74 61 69 6e  e a BLOB contain
2e730 69 6e 67 20 61 6c 6c 20 7a 65 72 6f 0a 2a 2a 20  ing all zero.** 
2e740 62 79 74 65 73 20 61 6e 64 20 4e 20 62 79 74 65  bytes and N byte
2e750 73 20 69 6e 20 73 69 7a 65 2c 20 77 68 65 72 65  s in size, where
2e760 20 4e 20 69 73 20 74 68 65 20 76 61 6c 75 65 20   N is the value 
2e770 6f 66 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d  of the 2nd param
2e780 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  eter..**.** The 
2e790 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64  sqlite3_result_d
2e7a0 6f 75 62 6c 65 28 29 20 69 6e 74 65 72 66 61 63  ouble() interfac
2e7b0 65 20 73 65 74 73 20 74 68 65 20 72 65 73 75 6c  e sets the resul
2e7c0 74 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 61 70 70  t from.** an app
2e7d0 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64  lication defined
2e7e0 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20   function to be 
2e7f0 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  a floating point
2e800 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 65 64   value specified
2e810 0a 2a 2a 20 62 79 20 69 74 73 20 32 6e 64 20 61  .** by its 2nd a
2e820 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  rgument..**.** T
2e830 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  he sqlite3_resul
2e840 74 5f 65 72 72 6f 72 28 29 20 61 6e 64 20 73 71  t_error() and sq
2e850 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
2e860 6f 72 31 36 28 29 20 66 75 6e 63 74 69 6f 6e 73  or16() functions
2e870 0a 2a 2a 20 63 61 75 73 65 20 74 68 65 20 69 6d  .** cause the im
2e880 70 6c 65 6d 65 6e 74 65 64 20 53 51 4c 20 66 75  plemented SQL fu
2e890 6e 63 74 69 6f 6e 20 74 6f 20 74 68 72 6f 77 20  nction to throw 
2e8a0 61 6e 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a 2a  an exception..**
2e8b0 20 53 51 4c 69 74 65 20 75 73 65 73 20 74 68 65   SQLite uses the
2e8c0 20 73 74 72 69 6e 67 20 70 6f 69 6e 74 65 64 20   string pointed 
2e8d0 74 6f 20 62 79 20 74 68 65 0a 2a 2a 20 32 6e 64  to by the.** 2nd
2e8e0 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 73 71   parameter of sq
2e8f0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
2e900 6f 72 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f  or() or sqlite3_
2e910 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29  result_error16()
2e920 0a 2a 2a 20 61 73 20 74 68 65 20 74 65 78 74 20  .** as the text 
2e930 6f 66 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  of an error mess
2e940 61 67 65 2e 20 20 53 51 4c 69 74 65 20 69 6e 74  age.  SQLite int
2e950 65 72 70 72 65 74 73 20 74 68 65 20 65 72 72 6f  erprets the erro
2e960 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 73 74 72  r.** message str
2e970 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ing from sqlite3
2e980 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20  _result_error() 
2e990 61 73 20 55 54 46 38 2e 20 53 51 4c 69 74 65 0a  as UTF8. SQLite.
2e9a0 2a 2a 20 69 6e 74 65 72 70 72 65 74 73 20 74 68  ** interprets th
2e9b0 65 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 73 71  e string from sq
2e9c0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
2e9d0 6f 72 31 36 28 29 20 61 73 20 55 54 46 31 36 20  or16() as UTF16 
2e9e0 69 6e 20 6e 61 74 69 76 65 0a 2a 2a 20 62 79 74  in native.** byt
2e9f0 65 20 6f 72 64 65 72 2e 20 20 49 66 20 74 68 65  e order.  If the
2ea00 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72   third parameter
2ea10 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 75   to sqlite3_resu
2ea20 6c 74 5f 65 72 72 6f 72 28 29 0a 2a 2a 20 6f 72  lt_error().** or
2ea30 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2ea40 65 72 72 6f 72 31 36 28 29 20 69 73 20 6e 65 67  error16() is neg
2ea50 61 74 69 76 65 20 74 68 65 6e 20 53 51 4c 69 74  ative then SQLit
2ea60 65 20 74 61 6b 65 73 20 61 73 20 74 68 65 20 65  e takes as the e
2ea70 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20  rror.** message 
2ea80 61 6c 6c 20 74 65 78 74 20 75 70 20 74 68 72 6f  all text up thro
2ea90 75 67 68 20 74 68 65 20 66 69 72 73 74 20 7a 65  ugh the first ze
2eaa0 72 6f 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a  ro character..**
2eab0 20 49 66 20 74 68 65 20 74 68 69 72 64 20 70 61   If the third pa
2eac0 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
2ead0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
2eae0 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ) or.** sqlite3_
2eaf0 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29  result_error16()
2eb00 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65   is non-negative
2eb10 20 74 68 65 6e 20 53 51 4c 69 74 65 20 74 61 6b   then SQLite tak
2eb20 65 73 20 74 68 61 74 20 6d 61 6e 79 0a 2a 2a 20  es that many.** 
2eb30 62 79 74 65 73 20 28 6e 6f 74 20 63 68 61 72 61  bytes (not chara
2eb40 63 74 65 72 73 29 20 66 72 6f 6d 20 74 68 65 20  cters) from the 
2eb50 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61 73  2nd parameter as
2eb60 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   the error messa
2eb70 67 65 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ge..** The sqlit
2eb80 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
2eb90 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65  ) and sqlite3_re
2eba0 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a  sult_error16().*
2ebb0 2a 20 72 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20  * routines make 
2ebc0 61 20 63 6f 70 79 20 70 72 69 76 61 74 65 20 63  a copy private c
2ebd0 6f 70 79 20 6f 66 20 74 68 65 20 65 72 72 6f 72  opy of the error
2ebe0 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 62 65   message text be
2ebf0 66 6f 72 65 0a 2a 2a 20 74 68 65 79 20 72 65 74  fore.** they ret
2ec00 75 72 6e 2e 20 20 48 65 6e 63 65 2c 20 74 68 65  urn.  Hence, the
2ec10 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f   calling functio
2ec20 6e 20 63 61 6e 20 64 65 61 6c 6c 6f 63 61 74 65  n can deallocate
2ec30 20 6f 72 0a 2a 2a 20 6d 6f 64 69 66 79 20 74 68   or.** modify th
2ec40 65 20 74 65 78 74 20 61 66 74 65 72 20 74 68 65  e text after the
2ec50 79 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74  y return without
2ec60 20 68 61 72 6d 2e 0a 2a 2a 20 54 68 65 20 73 71   harm..** The sq
2ec70 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
2ec80 6f 72 5f 63 6f 64 65 28 29 20 66 75 6e 63 74 69  or_code() functi
2ec90 6f 6e 20 63 68 61 6e 67 65 73 20 74 68 65 20 65  on changes the e
2eca0 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 72 65 74  rror code.** ret
2ecb0 75 72 6e 65 64 20 62 79 20 53 51 4c 69 74 65 20  urned by SQLite 
2ecc0 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61  as a result of a
2ecd0 6e 20 65 72 72 6f 72 20 69 6e 20 61 20 66 75 6e  n error in a fun
2ece0 63 74 69 6f 6e 2e 20 20 42 79 20 64 65 66 61 75  ction.  By defau
2ecf0 6c 74 2c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72  lt,.** the error
2ed00 20 63 6f 64 65 20 69 73 20 53 51 4c 49 54 45 5f   code is SQLITE_
2ed10 45 52 52 4f 52 2e 20 20 41 20 73 75 62 73 65 71  ERROR.  A subseq
2ed20 75 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c  uent call to sql
2ed30 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
2ed40 72 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65  r().** or sqlite
2ed50 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36  3_result_error16
2ed60 28 29 20 72 65 73 65 74 73 20 74 68 65 20 65 72  () resets the er
2ed70 72 6f 72 20 63 6f 64 65 20 74 6f 20 53 51 4c 49  ror code to SQLI
2ed80 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20  TE_ERROR..**.** 
2ed90 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  The sqlite3_resu
2eda0 6c 74 5f 74 6f 6f 62 69 67 28 29 20 69 6e 74 65  lt_toobig() inte
2edb0 72 66 61 63 65 20 63 61 75 73 65 73 20 53 51 4c  rface causes SQL
2edc0 69 74 65 0a 2a 2a 20 74 6f 20 74 68 72 6f 77 20  ite.** to throw 
2edd0 61 6e 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74  an error indicat
2ede0 69 6e 67 20 74 68 61 74 20 61 20 73 74 72 69 6e  ing that a strin
2edf0 67 20 6f 72 20 42 4c 4f 42 20 69 73 20 74 6f 20  g or BLOB is to 
2ee00 6c 6f 6e 67 0a 2a 2a 20 74 6f 20 72 65 70 72 65  long.** to repre
2ee10 73 65 6e 74 2e 20 20 54 68 65 20 73 71 6c 69 74  sent.  The sqlit
2ee20 65 33 5f 72 65 73 75 6c 74 5f 6e 6f 6d 65 6d 28  e3_result_nomem(
2ee30 29 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 63  ) interface.** c
2ee40 61 75 73 65 73 20 53 51 4c 69 74 65 20 74 6f 20  auses SQLite to 
2ee50 74 68 72 6f 77 20 61 6e 20 65 78 63 65 70 74 69  throw an excepti
2ee60 6f 6e 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68  on indicating th
2ee70 61 74 20 74 68 65 20 61 0a 2a 2a 20 6d 65 6d 6f  at the a.** memo
2ee80 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61  ry allocation fa
2ee90 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  iled..**.** The 
2eea0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
2eeb0 6e 74 28 29 20 69 6e 74 65 72 66 61 63 65 20 73  nt() interface s
2eec0 65 74 73 20 74 68 65 20 72 65 74 75 72 6e 20 76  ets the return v
2eed0 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61  alue.** of the a
2eee0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
2eef0 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62  ed function to b
2ef00 65 20 74 68 65 20 33 32 2d 62 69 74 20 73 69 67  e the 32-bit sig
2ef10 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 76  ned integer.** v
2ef20 61 6c 75 65 20 67 69 76 65 6e 20 69 6e 20 74 68  alue given in th
2ef30 65 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a  e 2nd argument..
2ef40 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72  ** The sqlite3_r
2ef50 65 73 75 6c 74 5f 69 6e 74 36 34 28 29 20 69 6e  esult_int64() in
2ef60 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65  terface sets the
2ef70 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a   return value.**
2ef80 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 74   of the applicat
2ef90 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
2efa0 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 36  tion to be the 6
2efb0 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74  4-bit signed int
2efc0 65 67 65 72 0a 2a 2a 20 76 61 6c 75 65 20 67 69  eger.** value gi
2efd0 76 65 6e 20 69 6e 20 74 68 65 20 32 6e 64 20 61  ven in the 2nd a
2efe0 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  rgument..**.** T
2eff0 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  he sqlite3_resul
2f000 74 5f 6e 75 6c 6c 28 29 20 69 6e 74 65 72 66 61  t_null() interfa
2f010 63 65 20 73 65 74 73 20 74 68 65 20 72 65 74 75  ce sets the retu
2f020 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74  rn value.** of t
2f030 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  he application-d
2f040 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20  efined function 
2f050 74 6f 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  to be NULL..**.*
2f060 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65  * The sqlite3_re
2f070 73 75 6c 74 5f 74 65 78 74 28 29 2c 20 73 71 6c  sult_text(), sql
2f080 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
2f090 31 36 28 29 2c 20 0a 2a 2a 20 73 71 6c 69 74 65  16(), .** sqlite
2f0a0 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c  3_result_text16l
2f0b0 65 28 29 2c 20 61 6e 64 20 73 71 6c 69 74 65 33  e(), and sqlite3
2f0c0 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65  _result_text16be
2f0d0 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a  () interfaces.**
2f0e0 20 73 65 74 20 74 68 65 20 72 65 74 75 72 6e 20   set the return 
2f0f0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 61 70 70  value of the app
2f100 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
2f110 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 0a   function to be.
2f120 2a 2a 20 61 20 74 65 78 74 20 73 74 72 69 6e 67  ** a text string
2f130 20 77 68 69 63 68 20 69 73 20 72 65 70 72 65 73   which is repres
2f140 65 6e 74 65 64 20 61 73 20 55 54 46 2d 38 2c 20  ented as UTF-8, 
2f150 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79  UTF-16 native by
2f160 74 65 20 6f 72 64 65 72 2c 0a 2a 2a 20 55 54 46  te order,.** UTF
2f170 2d 31 36 20 6c 69 74 74 6c 65 20 65 6e 64 69 61  -16 little endia
2f180 6e 2c 20 6f 72 20 55 54 46 2d 31 36 20 62 69 67  n, or UTF-16 big
2f190 20 65 6e 64 69 61 6e 2c 20 72 65 73 70 65 63 74   endian, respect
2f1a0 69 76 65 6c 79 2e 0a 2a 2a 20 53 51 4c 69 74 65  ively..** SQLite
2f1b0 20 74 61 6b 65 73 20 74 68 65 20 74 65 78 74 20   takes the text 
2f1c0 72 65 73 75 6c 74 20 66 72 6f 6d 20 74 68 65 20  result from the 
2f1d0 61 70 70 6c 69 63 61 74 69 6f 6e 20 66 72 6f 6d  application from
2f1e0 0a 2a 2a 20 74 68 65 20 32 6e 64 20 70 61 72 61  .** the 2nd para
2f1f0 6d 65 74 65 72 20 6f 66 20 74 68 65 20 73 71 6c  meter of the sql
2f200 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
2f210 2a 20 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a  * interfaces..**
2f220 20 49 66 20 74 68 65 20 33 72 64 20 70 61 72 61   If the 3rd para
2f230 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c  meter to the sql
2f240 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
2f250 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20  * interfaces.** 
2f260 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65  is negative, the
2f270 6e 20 53 51 4c 69 74 65 20 74 61 6b 65 73 20 72  n SQLite takes r
2f280 65 73 75 6c 74 20 74 65 78 74 20 66 72 6f 6d 20  esult text from 
2f290 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65  the 2nd paramete
2f2a0 72 20 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68  r .** through th
2f2b0 65 20 66 69 72 73 74 20 7a 65 72 6f 20 63 68 61  e first zero cha
2f2c0 72 61 63 74 65 72 2e 0a 2a 2a 20 49 66 20 74 68  racter..** If th
2f2d0 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20  e 3rd parameter 
2f2e0 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72  to the sqlite3_r
2f2f0 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65  esult_text* inte
2f300 72 66 61 63 65 73 0a 2a 2a 20 69 73 20 6e 6f 6e  rfaces.** is non
2f310 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20  -negative, then 
2f320 61 73 20 6d 61 6e 79 20 62 79 74 65 73 20 28 6e  as many bytes (n
2f330 6f 74 20 63 68 61 72 61 63 74 65 72 73 29 20 6f  ot characters) o
2f340 66 20 74 68 65 20 74 65 78 74 0a 2a 2a 20 70 6f  f the text.** po
2f350 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 20  inted to by the 
2f360 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61 72  2nd parameter ar
2f370 65 20 74 61 6b 65 6e 20 61 73 20 74 68 65 20 61  e taken as the a
2f380 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
2f390 65 64 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72  ed.** function r
2f3a0 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65  esult..** If the
2f3b0 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74   4th parameter t
2f3c0 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65  o the sqlite3_re
2f3d0 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72  sult_text* inter
2f3e0 66 61 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69  faces.** or sqli
2f3f0 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20  te3_result_blob 
2f400 69 73 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f  is a non-NULL po
2f410 69 6e 74 65 72 2c 20 74 68 65 6e 20 53 51 4c 69  inter, then SQLi
2f420 74 65 20 63 61 6c 6c 73 20 74 68 61 74 0a 2a 2a  te calls that.**
2f430 20 66 75 6e 63 74 69 6f 6e 20 61 73 20 74 68 65   function as the
2f440 20 64 65 73 74 72 75 63 74 6f 72 20 6f 6e 20 74   destructor on t
2f450 68 65 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 20  he text or blob 
2f460 72 65 73 75 6c 74 20 77 68 65 6e 20 69 74 20 68  result when it h
2f470 61 73 0a 2a 2a 20 66 69 6e 69 73 68 65 64 20 75  as.** finished u
2f480 73 69 6e 67 20 74 68 61 74 20 72 65 73 75 6c 74  sing that result
2f490 2e 0a 2a 2a 20 49 66 20 74 68 65 20 34 74 68 20  ..** If the 4th 
2f4a0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65  parameter to the
2f4b0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2f4c0 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73  text* interfaces
2f4d0 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 72  .** or sqlite3_r
2f4e0 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73 20 74 68  esult_blob is th
2f4f0 65 20 73 70 65 63 69 61 6c 20 63 6f 6e 73 74 61  e special consta
2f500 6e 74 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  nt SQLITE_STATIC
2f510 2c 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65  , then.** SQLite
2f520 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68   assumes that th
2f530 65 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 20 72  e text or blob r
2f540 65 73 75 6c 74 20 69 73 20 63 6f 6e 73 74 61 6e  esult is constan
2f550 74 20 73 70 61 63 65 20 61 6e 64 0a 2a 2a 20 64  t space and.** d
2f560 6f 65 73 20 6e 6f 74 20 63 6f 70 79 20 74 68 65  oes not copy the
2f570 20 73 70 61 63 65 20 6f 72 20 63 61 6c 6c 20 61   space or call a
2f580 20 64 65 73 74 72 75 63 74 6f 72 20 77 68 65 6e   destructor when
2f590 20 69 74 20 68 61 73 0a 2a 2a 20 66 69 6e 69 73   it has.** finis
2f5a0 68 65 64 20 75 73 69 6e 67 20 74 68 61 74 20 72  hed using that r
2f5b0 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65  esult..** If the
2f5c0 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74   4th parameter t
2f5d0 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65  o the sqlite3_re
2f5e0 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72  sult_text* inter
2f5f0 66 61 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69  faces.** or sqli
2f600 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20  te3_result_blob 
2f610 69 73 20 74 68 65 20 73 70 65 63 69 61 6c 20 63  is the special c
2f620 6f 6e 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 54  onstant SQLITE_T
2f630 52 41 4e 53 49 45 4e 54 0a 2a 2a 20 74 68 65 6e  RANSIENT.** then
2f640 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 61 20   SQLite makes a 
2f650 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 73 75  copy of the resu
2f660 6c 74 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62  lt into space ob
2f670 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 66  tained from.** f
2f680 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  rom [sqlite3_mal
2f690 6c 6f 63 28 29 5d 20 62 65 66 6f 72 65 20 69 74  loc()] before it
2f6a0 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20   returns..**.** 
2f6b0 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  The sqlite3_resu
2f6c0 6c 74 5f 76 61 6c 75 65 28 29 20 69 6e 74 65 72  lt_value() inter
2f6d0 66 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65  face sets the re
2f6e0 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61  sult of.** the a
2f6f0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
2f700 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62  ed function to b
2f710 65 20 61 20 63 6f 70 79 20 74 68 65 0a 2a 2a 20  e a copy the.** 
2f720 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c  [unprotected sql
2f730 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65  ite3_value] obje
2f740 63 74 20 73 70 65 63 69 66 69 65 64 20 62 79 20  ct specified by 
2f750 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65  the 2nd paramete
2f760 72 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74  r.  The.** sqlit
2f770 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28  e3_result_value(
2f780 29 20 69 6e 74 65 72 66 61 63 65 20 6d 61 6b 65  ) interface make
2f790 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  s a copy of the 
2f7a0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 0a  [sqlite3_value].
2f7b0 2a 2a 20 73 6f 20 74 68 61 74 20 5b 73 71 6c 69  ** so that [sqli
2f7c0 74 65 33 5f 76 61 6c 75 65 5d 20 73 70 65 63 69  te3_value] speci
2f7d0 66 69 65 64 20 69 6e 20 74 68 65 20 70 61 72 61  fied in the para
2f7e0 6d 65 74 65 72 20 6d 61 79 20 63 68 61 6e 67 65  meter may change
2f7f0 20 6f 72 0a 2a 2a 20 62 65 20 64 65 61 6c 6c 6f   or.** be deallo
2f800 63 61 74 65 64 20 61 66 74 65 72 20 73 71 6c 69  cated after sqli
2f810 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65  te3_result_value
2f820 28 29 20 72 65 74 75 72 6e 73 20 77 69 74 68 6f  () returns witho
2f830 75 74 20 68 61 72 6d 2e 0a 2a 2a 20 41 20 5b 70  ut harm..** A [p
2f840 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33  rotected sqlite3
2f850 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 6d  _value] object m
2f860 61 79 20 61 6c 77 61 79 73 20 62 65 20 75 73 65  ay always be use
2f870 64 20 77 68 65 72 65 20 61 6e 0a 2a 2a 20 5b 75  d where an.** [u
2f880 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  nprotected sqlit
2f890 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74  e3_value] object
2f8a0 20 69 73 20 72 65 71 75 69 72 65 64 2c 20 73 6f   is required, so
2f8b0 20 65 69 74 68 65 72 0a 2a 2a 20 6b 69 6e 64 20   either.** kind 
2f8c0 6f 66 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  of [sqlite3_valu
2f8d0 65 5d 20 6f 62 6a 65 63 74 20 63 61 6e 20 62 65  e] object can be
2f8e0 20 75 73 65 64 20 77 69 74 68 20 74 68 69 73 20   used with this 
2f8f0 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a  interface..**.**
2f900 20 49 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e   If these routin
2f910 65 73 20 61 72 65 20 63 61 6c 6c 65 64 20 66 72  es are called fr
2f920 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 64 69  om within the di
2f930 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 20 0a  fferent thread .
2f940 2a 2a 20 74 68 61 6e 20 74 68 65 20 6f 6e 65 20  ** than the one 
2f950 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 61  containing the a
2f960 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
2f970 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74  ed function that
2f980 20 72 65 63 69 65 76 65 64 0a 2a 2a 20 74 68 65   recieved.** the
2f990 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   [sqlite3_contex
2f9a0 74 5d 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 20  t] pointer, the 
2f9b0 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e 64 65  results are unde
2f9c0 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56  fined..**.** INV
2f9d0 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b  ARIANTS:.**.** {
2f9e0 46 31 36 34 30 33 7d 20 54 68 65 20 64 65 66 61  F16403} The defa
2f9f0 75 6c 74 20 72 65 74 75 72 6e 20 76 61 6c 75 65  ult return value
2fa00 20 66 72 6f 6d 20 61 6e 79 20 53 51 4c 20 66 75   from any SQL fu
2fa10 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2e 0a  nction is NULL..
2fa20 2a 2a 0a 2a 2a 20 7b 46 31 36 34 30 36 7d 20 54  **.** {F16406} T
2fa30 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75  he [sqlite3_resu
2fa40 6c 74 5f 62 6c 6f 62 28 43 2c 56 2c 4e 2c 44 29  lt_blob(C,V,N,D)
2fa50 5d 20 69 6e 74 65 72 66 61 63 65 20 63 68 61 6e  ] interface chan
2fa60 67 65 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  ges the.**      
2fa70 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 75 65      return value
2fa80 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 43 20 74   of function C t
2fa90 6f 20 62 65 20 61 20 62 6c 6f 62 20 74 68 61 74  o be a blob that
2faa0 20 69 73 20 4e 20 62 79 74 65 73 0a 2a 2a 20 20   is N bytes.**  
2fab0 20 20 20 20 20 20 20 20 69 6e 20 6c 65 6e 67 74          in lengt
2fac0 68 20 61 6e 64 20 77 69 74 68 20 63 6f 6e 74 65  h and with conte
2fad0 6e 74 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  nt pointed to by
2fae0 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 36 34 30   V..**.** {F1640
2faf0 39 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  9} The [sqlite3_
2fb00 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 43 2c  result_double(C,
2fb10 56 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 68  V)] interface ch
2fb20 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 20 20 20  anges the.**    
2fb30 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c        return val
2fb40 75 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 43  ue of function C
2fb50 20 74 6f 20 62 65 20 74 68 65 20 66 6c 6f 61 74   to be the float
2fb60 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20  ing point value 
2fb70 56 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 36 34 31 32  V..**.** {F16412
2fb80 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72  } The [sqlite3_r
2fb90 65 73 75 6c 74 5f 65 72 72 6f 72 28 43 2c 56 2c  esult_error(C,V,
2fba0 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 68  N)] interface ch
2fbb0 61 6e 67 65 73 20 74 68 65 20 72 65 74 75 72 6e  anges the return
2fbc0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 76 61 6c  .**          val
2fbd0 75 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 43  ue of function C
2fbe0 20 74 6f 20 62 65 20 61 6e 20 65 78 63 65 70 74   to be an except
2fbf0 69 6f 6e 20 77 69 74 68 20 65 72 72 6f 72 20 63  ion with error c
2fc00 6f 64 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ode.**          
2fc10 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 61  [SQLITE_ERROR] a
2fc20 6e 64 20 61 20 55 54 46 38 20 65 72 72 6f 72 20  nd a UTF8 error 
2fc30 6d 65 73 73 61 67 65 20 63 6f 70 69 65 64 20 66  message copied f
2fc40 72 6f 6d 20 56 20 75 70 20 74 6f 20 74 68 65 0a  rom V up to the.
2fc50 2a 2a 20 20 20 20 20 20 20 20 20 20 66 69 72 73  **          firs
2fc60 74 20 7a 65 72 6f 20 62 79 74 65 20 6f 72 20 75  t zero byte or u
2fc70 6e 74 69 6c 20 4e 20 62 79 74 65 73 20 61 72 65  ntil N bytes are
2fc80 20 72 65 61 64 20 69 66 20 4e 20 69 73 20 70 6f   read if N is po
2fc90 73 69 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b 46  sitive..**.** {F
2fca0 31 36 34 31 35 7d 20 54 68 65 20 5b 73 71 6c 69  16415} The [sqli
2fcb0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
2fcc0 31 36 28 43 2c 56 2c 4e 29 5d 20 69 6e 74 65 72  16(C,V,N)] inter
2fcd0 66 61 63 65 20 63 68 61 6e 67 65 73 20 74 68 65  face changes the
2fce0 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20   return.**      
2fcf0 20 20 20 20 76 61 6c 75 65 20 6f 66 20 66 75 6e      value of fun
2fd00 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 61 6e  ction C to be an
2fd10 20 65 78 63 65 70 74 69 6f 6e 20 77 69 74 68 20   exception with 
2fd20 65 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 20 20  error code.**   
2fd30 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 45         [SQLITE_E
2fd40 52 52 4f 52 5d 20 61 6e 64 20 61 20 55 54 46 31  RROR] and a UTF1
2fd50 36 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72  6 native byte or
2fd60 64 65 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67  der error messag
2fd70 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f  e.**          co
2fd80 70 69 65 64 20 66 72 6f 6d 20 56 20 75 70 20 74  pied from V up t
2fd90 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f  o the first zero
2fda0 20 74 65 72 6d 69 6e 61 74 6f 72 20 6f 72 20 75   terminator or u
2fdb0 6e 74 69 6c 20 4e 20 62 79 74 65 73 0a 2a 2a 20  ntil N bytes.** 
2fdc0 20 20 20 20 20 20 20 20 20 61 72 65 20 72 65 61           are rea
2fdd0 64 20 69 66 20 4e 20 69 73 20 70 6f 73 69 74 69  d if N is positi
2fde0 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 36 34 31  ve..**.** {F1641
2fdf0 38 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  8} The [sqlite3_
2fe00 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f  result_error_too
2fe10 62 69 67 28 43 29 5d 20 69 6e 74 65 72 66 61 63  big(C)] interfac
2fe20 65 20 63 68 61 6e 67 65 73 20 74 68 65 20 72 65  e changes the re
2fe30 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  turn.**         
2fe40 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 66 75   value of the fu
2fe50 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 61  nction C to be a
2fe60 6e 20 65 78 63 65 70 74 69 6f 6e 20 77 69 74 68  n exception with
2fe70 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 20   error code.**  
2fe80 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f          [SQLITE_
2fe90 54 4f 4f 42 49 47 5d 20 61 6e 64 20 61 6e 20 61  TOOBIG] and an a
2fea0 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72  ppropriate error
2feb0 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20   message..**.** 
2fec0 7b 46 31 36 34 32 31 7d 20 54 68 65 20 5b 73 71  {F16421} The [sq
2fed0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
2fee0 6f 72 5f 6e 6f 6d 65 6d 28 43 29 5d 20 69 6e 74  or_nomem(C)] int
2fef0 65 72 66 61 63 65 20 63 68 61 6e 67 65 73 20 74  erface changes t
2ff00 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20  he return.**    
2ff10 20 20 20 20 20 20 76 61 6c 75 65 20 6f 66 20 74        value of t
2ff20 68 65 20 66 75 6e 63 74 69 6f 6e 20 43 20 74 6f  he function C to
2ff30 20 62 65 20 61 6e 20 65 78 63 65 70 74 69 6f 6e   be an exception
2ff40 20 77 69 74 68 20 65 72 72 6f 72 20 63 6f 64 65   with error code
2ff50 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51  .**          [SQ
2ff60 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 20 61 6e 64 20  LITE_NOMEM] and 
2ff70 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 65  an appropriate e
2ff80 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a  rror message..**
2ff90 0a 2a 2a 20 7b 46 31 36 34 32 34 7d 20 54 68 65  .** {F16424} The
2ffa0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74   [sqlite3_result
2ffb0 5f 65 72 72 6f 72 5f 63 6f 64 65 28 43 2c 45 29  _error_code(C,E)
2ffc0 5d 20 69 6e 74 65 72 66 61 63 65 20 63 68 61 6e  ] interface chan
2ffd0 67 65 73 20 74 68 65 20 72 65 74 75 72 6e 0a 2a  ges the return.*
2ffe0 2a 20 20 20 20 20 20 20 20 20 20 76 61 6c 75 65  *          value
2fff0 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
30000 20 43 20 74 6f 20 62 65 20 61 6e 20 65 78 63 65   C to be an exce
30010 70 74 69 6f 6e 20 77 69 74 68 20 65 72 72 6f 72  ption with error
30020 20 63 6f 64 65 20 45 2e 0a 2a 2a 20 20 20 20 20   code E..**     
30030 20 20 20 20 20 54 68 65 20 65 72 72 6f 72 20 6d       The error m
30040 65 73 73 61 67 65 20 74 65 78 74 20 69 73 20 75  essage text is u
30050 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20  nchanged..**.** 
30060 7b 46 31 36 34 32 37 7d 20 54 68 65 20 5b 73 71  {F16427} The [sq
30070 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
30080 28 43 2c 56 29 5d 20 69 6e 74 65 72 66 61 63 65  (C,V)] interface
30090 20 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20   changes the.** 
300a0 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
300b0 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f  value of functio
300c0 6e 20 43 20 74 6f 20 62 65 20 74 68 65 20 33 32  n C to be the 32
300d0 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76 61 6c  -bit integer val
300e0 75 65 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 36  ue V..**.** {F16
300f0 34 33 30 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  430} The [sqlite
30100 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 43  3_result_int64(C
30110 2c 56 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63  ,V)] interface c
30120 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 20 20  hanges the.**   
30130 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61         return va
30140 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20  lue of function 
30150 43 20 74 6f 20 62 65 20 74 68 65 20 36 34 2d 62  C to be the 64-b
30160 69 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  it integer value
30170 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 36 34 33   V..**.** {F1643
30180 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  3} The [sqlite3_
30190 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 43 29 5d 20  result_null(C)] 
301a0 69 6e 74 65 72 66 61 63 65 20 63 68 61 6e 67 65  interface change
301b0 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  s the.**        
301c0 20 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f    return value o
301d0 66 20 66 75 6e 63 74 69 6f 6e 20 43 20 74 6f 20  f function C to 
301e0 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 7b  be NULL..**.** {
301f0 46 31 36 34 33 36 7d 20 54 68 65 20 5b 73 71 6c  F16436} The [sql
30200 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
30210 28 43 2c 56 2c 4e 2c 44 29 5d 20 69 6e 74 65 72  (C,V,N,D)] inter
30220 66 61 63 65 20 63 68 61 6e 67 65 73 20 74 68 65  face changes the
30230 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74  .**          ret
30240 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 75 6e  urn value of fun
30250 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 74 68  ction C to be th
30260 65 20 55 54 46 38 20 73 74 72 69 6e 67 0a 2a 2a  e UTF8 string.**
30270 20 20 20 20 20 20 20 20 20 20 56 20 75 70 20 74            V up t
30280 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f  o the first zero
30290 20 69 66 20 4e 20 69 73 20 6e 65 67 61 74 69 76   if N is negativ
302a0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72  e.**          or
302b0 20 74 68 65 20 66 69 72 73 74 20 4e 20 62 79 74   the first N byt
302c0 65 73 20 6f 66 20 56 20 69 66 20 4e 20 69 73 20  es of V if N is 
302d0 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a 2a  non-negative..**
302e0 0a 2a 2a 20 7b 46 31 36 34 33 39 7d 20 54 68 65  .** {F16439} The
302f0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74   [sqlite3_result
30300 5f 74 65 78 74 31 36 28 43 2c 56 2c 4e 2c 44 29  _text16(C,V,N,D)
30310 5d 20 69 6e 74 65 72 66 61 63 65 20 63 68 61 6e  ] interface chan
30320 67 65 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  ges the.**      
30330 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 75 65      return value
30340 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 43 20 74   of function C t
30350 6f 20 62 65 20 74 68 65 20 55 54 46 31 36 20 6e  o be the UTF16 n
30360 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72  ative byte order
30370 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74 72  .**          str
30380 69 6e 67 20 56 20 75 70 20 74 6f 20 74 68 65 20  ing V up to the 
30390 66 69 72 73 74 20 7a 65 72 6f 20 69 66 20 4e 20  first zero if N 
303a0 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e  is.**          n
303b0 65 67 61 74 69 76 65 20 6f 72 20 74 68 65 20 66  egative or the f
303c0 69 72 73 74 20 4e 20 62 79 74 65 73 20 6f 66 20  irst N bytes of 
303d0 56 20 69 66 20 4e 20 69 73 20 6e 6f 6e 2d 6e 65  V if N is non-ne
303e0 67 61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b 46  gative..**.** {F
303f0 31 36 34 34 32 7d 20 54 68 65 20 5b 73 71 6c 69  16442} The [sqli
30400 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
30410 36 62 65 28 43 2c 56 2c 4e 2c 44 29 5d 20 69 6e  6be(C,V,N,D)] in
30420 74 65 72 66 61 63 65 20 63 68 61 6e 67 65 73 20  terface changes 
30430 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
30440 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20  return value of 
30450 66 75 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62 65  function C to be
30460 20 74 68 65 20 55 54 46 31 36 20 62 69 67 2d 65   the UTF16 big-e
30470 6e 64 69 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20  ndian.**        
30480 20 20 73 74 72 69 6e 67 20 56 20 75 70 20 74 6f    string V up to
30490 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f 20   the first zero 
304a0 69 66 20 4e 20 69 73 0a 2a 2a 20 20 20 20 20 20  if N is.**      
304b0 20 20 20 20 69 73 20 6e 65 67 61 74 69 76 65 20      is negative 
304c0 6f 72 20 74 68 65 20 66 69 72 73 74 20 4e 20 62  or the first N b
304d0 79 74 65 73 20 6f 72 20 56 20 69 66 20 4e 20 69  ytes or V if N i
304e0 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a  s non-negative..
304f0 2a 2a 0a 2a 2a 20 7b 46 31 36 34 34 35 7d 20 54  **.** {F16445} T
30500 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75  he [sqlite3_resu
30510 6c 74 5f 74 65 78 74 31 36 6c 65 28 43 2c 56 2c  lt_text16le(C,V,
30520 4e 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 65 20  N,D)] interface 
30530 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 20  changes the.**  
30540 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 76          return v
30550 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e  alue of function
30560 20 43 20 74 6f 20 62 65 20 74 68 65 20 55 54 46   C to be the UTF
30570 31 36 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e  16 little-endian
30580 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74 72  .**          str
30590 69 6e 67 20 56 20 75 70 20 74 6f 20 74 68 65 20  ing V up to the 
305a0 66 69 72 73 74 20 7a 65 72 6f 20 69 66 20 4e 20  first zero if N 
305b0 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e  is.**          n
305c0 65 67 61 74 69 76 65 20 6f 72 20 74 68 65 20 66  egative or the f
305d0 69 72 73 74 20 4e 20 62 79 74 65 73 20 6f 66 20  irst N bytes of 
305e0 56 20 69 66 20 4e 20 69 73 20 6e 6f 6e 2d 6e 65  V if N is non-ne
305f0 67 61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b 46  gative..**.** {F
30600 31 36 34 34 38 7d 20 54 68 65 20 5b 73 71 6c 69  16448} The [sqli
30610 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65  te3_result_value
30620 28 43 2c 56 29 5d 20 69 6e 74 65 72 66 61 63 65  (C,V)] interface
30630 20 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20   changes the.** 
30640 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
30650 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f  value of functio
30660 6e 20 43 20 74 6f 20 62 65 20 5b 75 6e 70 72 6f  n C to be [unpro
30670 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76  tected sqlite3_v
30680 61 6c 75 65 5d 0a 2a 2a 20 20 20 20 20 20 20 20  alue].**        
30690 20 20 6f 62 6a 65 63 74 20 56 2e 0a 2a 2a 0a 2a    object V..**.*
306a0 2a 20 7b 46 31 36 34 35 31 7d 20 54 68 65 20 5b  * {F16451} The [
306b0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a  sqlite3_result_z
306c0 65 72 6f 62 6c 6f 62 28 43 2c 4e 29 5d 20 69 6e  eroblob(C,N)] in
306d0 74 65 72 66 61 63 65 20 63 68 61 6e 67 65 73 20  terface changes 
306e0 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
306f0 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20  return value of 
30700 66 75 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62 65  function C to be
30710 20 61 6e 20 4e 2d 62 79 74 65 20 62 6c 6f 62 20   an N-byte blob 
30720 6f 66 20 61 6c 6c 20 7a 65 72 6f 73 2e 0a 2a 2a  of all zeros..**
30730 0a 2a 2a 20 7b 46 31 36 34 35 34 7d 20 54 68 65  .** {F16454} The
30740 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74   [sqlite3_result
30750 5f 65 72 72 6f 72 28 29 5d 20 61 6e 64 20 5b 73  _error()] and [s
30760 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
30770 72 6f 72 31 36 28 29 5d 0a 2a 2a 20 20 20 20 20  ror16()].**     
30780 20 20 20 20 20 69 6e 74 65 72 66 61 63 65 73 20       interfaces 
30790 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74  make a copy of t
307a0 68 65 69 72 20 65 72 72 6f 72 20 6d 65 73 73 61  heir error messa
307b0 67 65 20 73 74 72 69 6e 67 73 20 62 65 66 6f 72  ge strings befor
307c0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65  e.**          re
307d0 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b  turning..**.** {
307e0 46 31 36 34 35 37 7d 20 49 66 20 74 68 65 20 44  F16457} If the D
307f0 20 64 65 73 74 72 75 63 74 6f 72 20 70 61 72 61   destructor para
30800 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65  meter to [sqlite
30810 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 43 2c  3_result_blob(C,
30820 56 2c 4e 2c 44 29 5d 2c 0a 2a 2a 20 20 20 20 20  V,N,D)],.**     
30830 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 65       [sqlite3_re
30840 73 75 6c 74 5f 74 65 78 74 28 43 2c 56 2c 4e 2c  sult_text(C,V,N,
30850 44 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65  D)], [sqlite3_re
30860 73 75 6c 74 5f 74 65 78 74 31 36 28 43 2c 56 2c  sult_text16(C,V,
30870 4e 2c 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20  N,D)],.**       
30880 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75     [sqlite3_resu
30890 6c 74 5f 74 65 78 74 31 36 62 65 28 43 2c 56 2c  lt_text16be(C,V,
308a0 4e 2c 44 29 5d 2c 20 6f 72 0a 2a 2a 20 20 20 20  N,D)], or.**    
308b0 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 72        [sqlite3_r
308c0 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 28 43  esult_text16le(C
308d0 2c 56 2c 4e 2c 44 29 5d 20 69 73 20 74 68 65 20  ,V,N,D)] is the 
308e0 63 6f 6e 73 74 61 6e 74 20 5b 53 51 4c 49 54 45  constant [SQLITE
308f0 5f 53 54 41 54 49 43 5d 0a 2a 2a 20 20 20 20 20  _STATIC].**     
30900 20 20 20 20 20 74 68 65 6e 20 6e 6f 20 64 65 73       then no des
30910 74 72 75 63 74 6f 72 20 69 73 20 65 76 65 72 20  tructor is ever 
30920 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 70 6f  called on the po
30930 69 6e 74 65 72 20 56 20 61 6e 64 20 53 51 4c 69  inter V and SQLi
30940 74 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61  te.**          a
30950 73 73 75 6d 65 73 20 74 68 61 74 20 56 20 69 73  ssumes that V is
30960 20 69 6d 6d 75 74 61 62 6c 65 2e 0a 2a 2a 0a 2a   immutable..**.*
30970 2a 20 7b 46 31 36 34 36 30 7d 20 49 66 20 74 68  * {F16460} If th
30980 65 20 44 20 64 65 73 74 72 75 63 74 6f 72 20 70  e D destructor p
30990 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c  arameter to [sql
309a0 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
309b0 28 43 2c 56 2c 4e 2c 44 29 5d 2c 0a 2a 2a 20 20  (C,V,N,D)],.**  
309c0 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33          [sqlite3
309d0 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 43 2c 56  _result_text(C,V
309e0 2c 4e 2c 44 29 5d 2c 20 5b 73 71 6c 69 74 65 33  ,N,D)], [sqlite3
309f0 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 43  _result_text16(C
30a00 2c 56 2c 4e 2c 44 29 5d 2c 0a 2a 2a 20 20 20 20  ,V,N,D)],.**    
30a10 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 72        [sqlite3_r
30a20 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 43  esult_text16be(C
30a30 2c 56 2c 4e 2c 44 29 5d 2c 20 6f 72 0a 2a 2a 20  ,V,N,D)], or.** 
30a40 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
30a50 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c  3_result_text16l
30a60 65 28 43 2c 56 2c 4e 2c 44 29 5d 20 69 73 20 74  e(C,V,N,D)] is t
30a70 68 65 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 20  he constant.**  
30a80 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f          [SQLITE_
30a90 54 52 41 4e 53 49 45 4e 54 5d 20 74 68 65 6e 20  TRANSIENT] then 
30aa0 74 68 65 20 69 6e 74 65 72 66 61 63 65 73 20 6d  the interfaces m
30ab0 61 6b 65 73 20 61 20 63 6f 70 79 20 6f 66 20 74  akes a copy of t
30ac0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63  he.**          c
30ad0 6f 6e 74 65 6e 74 20 6f 66 20 56 20 61 6e 64 20  ontent of V and 
30ae0 72 65 74 61 69 6e 73 20 74 68 65 20 63 6f 70 79  retains the copy
30af0 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 36 34 36 33 7d  ..**.** {F16463}
30b00 20 49 66 20 74 68 65 20 44 20 64 65 73 74 72 75   If the D destru
30b10 63 74 6f 72 20 70 61 72 61 6d 65 74 65 72 20 74  ctor parameter t
30b20 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  o [sqlite3_resul
30b30 74 5f 62 6c 6f 62 28 43 2c 56 2c 4e 2c 44 29 5d  t_blob(C,V,N,D)]
30b40 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73  ,.**          [s
30b50 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
30b60 78 74 28 43 2c 56 2c 4e 2c 44 29 5d 2c 20 5b 73  xt(C,V,N,D)], [s
30b70 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
30b80 78 74 31 36 28 43 2c 56 2c 4e 2c 44 29 5d 2c 0a  xt16(C,V,N,D)],.
30b90 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
30ba0 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
30bb0 31 36 62 65 28 43 2c 56 2c 4e 2c 44 29 5d 2c 20  16be(C,V,N,D)], 
30bc0 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  or.**          [
30bd0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
30be0 65 78 74 31 36 6c 65 28 43 2c 56 2c 4e 2c 44 29  ext16le(C,V,N,D)
30bf0 5d 20 69 73 20 73 6f 6d 65 20 76 61 6c 75 65 20  ] is some value 
30c00 6f 74 68 65 72 20 74 68 61 6e 0a 2a 2a 20 20 20  other than.**   
30c10 20 20 20 20 20 20 20 74 68 65 20 63 6f 6e 73 74         the const
30c20 61 6e 74 73 20 5b 53 51 4c 49 54 45 5f 53 54 41  ants [SQLITE_STA
30c30 54 49 43 5d 20 61 6e 64 20 5b 53 51 4c 49 54 45  TIC] and [SQLITE
30c40 5f 54 52 41 4e 53 49 45 4e 54 5d 20 74 68 65 6e  _TRANSIENT] then
30c50 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 53 51   .**          SQ
30c60 4c 69 74 65 20 77 69 6c 6c 20 69 6e 76 6f 6b 65  Lite will invoke
30c70 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20   the destructor 
30c80 44 20 77 69 74 68 20 56 20 61 73 20 69 74 73 20  D with V as its 
30c90 6f 6e 6c 79 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  only argument.**
30ca0 20 20 20 20 20 20 20 20 20 20 77 68 65 6e 20 69            when i
30cb0 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 77  t has finished w
30cc0 69 74 68 20 74 68 65 20 56 20 76 61 6c 75 65 2e  ith the V value.
30cd0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
30ce0 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
30cf0 6c 74 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f  lt_blob(sqlite3_
30d00 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20  context*, const 
30d10 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64  void*, int, void
30d20 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c  (*)(void*));.SQL
30d30 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
30d40 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
30d50 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  le(sqlite3_conte
30d60 78 74 2a 2c 20 64 6f 75 62 6c 65 29 3b 0a 53 51  xt*, double);.SQ
30d70 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
30d80 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
30d90 6f 72 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  or(sqlite3_conte
30da0 78 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  xt*, const char*
30db0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41  , int);.SQLITE_A
30dc0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
30dd0 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 73  result_error16(s
30de0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
30df0 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e   const void*, in
30e00 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  t);.SQLITE_API v
30e10 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
30e20 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28  lt_error_toobig(
30e30 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
30e40 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  );.SQLITE_API vo
30e50 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
30e60 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71  t_error_nomem(sq
30e70 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b  lite3_context*);
30e80 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
30e90 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
30ea0 65 72 72 6f 72 5f 63 6f 64 65 28 73 71 6c 69 74  error_code(sqlit
30eb0 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74  e3_context*, int
30ec0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  );.SQLITE_API vo
30ed0 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
30ee0 74 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f  t_int(sqlite3_co
30ef0 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 51  ntext*, int);.SQ
30f00 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
30f10 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
30f20 36 34 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  64(sqlite3_conte
30f30 78 74 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74  xt*, sqlite3_int
30f40 36 34 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  64);.SQLITE_API 
30f50 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
30f60 75 6c 74 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33  ult_null(sqlite3
30f70 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49  _context*);.SQLI
30f80 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
30f90 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
30fa0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
30fb0 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69  , const char*, i
30fc0 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64  nt, void(*)(void
30fd0 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  *));.SQLITE_API 
30fe0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
30ff0 75 6c 74 5f 74 65 78 74 31 36 28 73 71 6c 69 74  ult_text16(sqlit
31000 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e  e3_context*, con
31010 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76  st void*, int, v
31020 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
31030 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
31040 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
31050 65 78 74 31 36 6c 65 28 73 71 6c 69 74 65 33 5f  ext16le(sqlite3_
31060 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20  context*, const 
31070 76 6f 69 64 2a 2c 20 69 6e 74 2c 76 6f 69 64 28  void*, int,void(
31080 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49  *)(void*));.SQLI
31090 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
310a0 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
310b0 36 62 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  6be(sqlite3_cont
310c0 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64  ext*, const void
310d0 2a 2c 20 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76  *, int,void(*)(v
310e0 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41  oid*));.SQLITE_A
310f0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
31100 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 73 71 6c  result_value(sql
31110 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 73  ite3_context*, s
31120 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
31130 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
31140 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a  sqlite3_result_z
31150 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f  eroblob(sqlite3_
31160 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 6e 29  context*, int n)
31170 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
31180 46 3a 20 44 65 66 69 6e 65 20 4e 65 77 20 43 6f  F: Define New Co
31190 6c 6c 61 74 69 6e 67 20 53 65 71 75 65 6e 63 65  llating Sequence
311a0 73 20 7b 46 31 36 36 30 30 7d 0a 2a 2a 0a 2a 2a  s {F16600}.**.**
311b0 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73   These functions
311c0 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 64 64   are used to add
311d0 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   new collation s
311e0 65 71 75 65 6e 63 65 73 20 74 6f 20 74 68 65 0a  equences to the.
311f0 2a 2a 20 5b 73 71 6c 69 74 65 33 2a 5d 20 68 61  ** [sqlite3*] ha
31200 6e 64 6c 65 20 73 70 65 63 69 66 69 65 64 20 61  ndle specified a
31210 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  s the first argu
31220 6d 65 6e 74 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65  ment. .**.** The
31230 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6e 65 77   name of the new
31240 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
31250 6e 63 65 20 69 73 20 73 70 65 63 69 66 69 65 64  nce is specified
31260 20 61 73 20 61 20 55 54 46 2d 38 20 73 74 72 69   as a UTF-8 stri
31270 6e 67 0a 2a 2a 20 66 6f 72 20 73 71 6c 69 74 65  ng.** for sqlite
31280 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
31290 6f 6e 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  on() and sqlite3
312a0 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
312b0 6e 5f 76 32 28 29 0a 2a 2a 20 61 6e 64 20 61 20  n_v2().** and a 
312c0 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 66 6f  UTF-16 string fo
312d0 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  r sqlite3_create
312e0 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 29 2e 20  _collation16(). 
312f0 49 6e 20 61 6c 6c 20 63 61 73 65 73 0a 2a 2a 20  In all cases.** 
31300 74 68 65 20 6e 61 6d 65 20 69 73 20 70 61 73 73  the name is pass
31310 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64  ed as the second
31320 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65   function argume
31330 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68  nt..**.** The th
31340 69 72 64 20 61 72 67 75 6d 65 6e 74 20 6d 61 79  ird argument may
31350 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 63   be one of the c
31360 6f 6e 73 74 61 6e 74 73 20 5b 53 51 4c 49 54 45  onstants [SQLITE
31370 5f 55 54 46 38 5d 2c 0a 2a 2a 20 5b 53 51 4c 49  _UTF8],.** [SQLI
31380 54 45 5f 55 54 46 31 36 4c 45 5d 20 6f 72 20 5b  TE_UTF16LE] or [
31390 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 5d 2c  SQLITE_UTF16BE],
313a0 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74   indicating that
313b0 20 74 68 65 20 75 73 65 72 2d 73 75 70 70 6c 69   the user-suppli
313c0 65 64 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 65 78  ed.** routine ex
313d0 70 65 63 74 73 20 74 6f 20 62 65 20 70 61 73 73  pects to be pass
313e0 65 64 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 73  ed pointers to s
313f0 74 72 69 6e 67 73 20 65 6e 63 6f 64 65 64 20 75  trings encoded u
31400 73 69 6e 67 20 55 54 46 2d 38 2c 0a 2a 2a 20 55  sing UTF-8,.** U
31410 54 46 2d 31 36 20 6c 69 74 74 6c 65 2d 65 6e 64  TF-16 little-end
31420 69 61 6e 20 6f 72 20 55 54 46 2d 31 36 20 62 69  ian or UTF-16 bi
31430 67 2d 65 6e 64 69 61 6e 20 72 65 73 70 65 63 74  g-endian respect
31440 69 76 65 6c 79 2e 20 54 68 65 0a 2a 2a 20 74 68  ively. The.** th
31450 69 72 64 20 61 72 67 75 6d 65 6e 74 20 6d 69 67  ird argument mig
31460 68 74 20 61 6c 73 6f 20 62 65 20 5b 53 51 4c 49  ht also be [SQLI
31470 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44  TE_UTF16_ALIGNED
31480 5d 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68  ] to indicate th
31490 61 74 0a 2a 2a 20 74 68 65 20 72 6f 75 74 69 6e  at.** the routin
314a0 65 20 65 78 70 65 63 74 73 20 70 6f 69 6e 74 65  e expects pointe
314b0 72 73 20 74 6f 20 31 36 2d 62 69 74 20 77 6f 72  rs to 16-bit wor
314c0 64 20 61 6c 69 67 6e 65 64 20 73 74 72 69 6e 67  d aligned string
314d0 73 0a 2a 2a 20 6f 66 20 55 54 46 31 36 20 69 6e  s.** of UTF16 in
314e0 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 65   the native byte
314f0 20 6f 72 64 65 72 20 6f 66 20 74 68 65 20 68 6f   order of the ho
31500 73 74 20 63 6f 6d 70 75 74 65 72 2e 0a 2a 2a 0a  st computer..**.
31510 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** A pointer to 
31520 74 68 65 20 75 73 65 72 20 73 75 70 70 6c 69 65  the user supplie
31530 64 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62  d routine must b
31540 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
31550 66 69 66 74 68 0a 2a 2a 20 61 72 67 75 6d 65 6e  fifth.** argumen
31560 74 2e 20 20 49 66 20 69 74 20 69 73 20 4e 55 4c  t.  If it is NUL
31570 4c 2c 20 74 68 69 73 20 69 73 20 74 68 65 20 73  L, this is the s
31580 61 6d 65 20 61 73 20 64 65 6c 65 74 69 6e 67 20  ame as deleting 
31590 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a  the collation.**
315a0 20 73 65 71 75 65 6e 63 65 20 28 73 6f 20 74 68   sequence (so th
315b0 61 74 20 53 51 4c 69 74 65 20 63 61 6e 6e 6f 74  at SQLite cannot
315c0 20 63 61 6c 6c 20 69 74 20 61 6e 79 6d 6f 72 65   call it anymore
315d0 29 2e 0a 2a 2a 20 45 61 63 68 20 74 69 6d 65 20  )..** Each time 
315e0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 0a  the application.
315f0 2a 2a 20 73 75 70 70 6c 69 65 64 20 66 75 6e 63  ** supplied func
31600 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 2c  tion is invoked,
31610 20 69 74 20 69 73 20 70 61 73 73 65 64 20 61 20   it is passed a 
31620 63 6f 70 79 20 6f 66 20 74 68 65 20 76 6f 69 64  copy of the void
31630 2a 20 70 61 73 73 65 64 20 61 73 0a 2a 2a 20 74  * passed as.** t
31640 68 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65  he fourth argume
31650 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 72  nt to sqlite3_cr
31660 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29  eate_collation()
31670 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63   or.** sqlite3_c
31680 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31  reate_collation1
31690 36 28 29 20 61 73 20 69 74 73 20 66 69 72 73 74  6() as its first
316a0 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a   parameter..**.*
316b0 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 6e 67 20  * The remaining 
316c0 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65  arguments to the
316d0 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 75 70   application-sup
316e0 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 61 72  plied routine ar
316f0 65 20 74 77 6f 20 73 74 72 69 6e 67 73 2c 0a 2a  e two strings,.*
31700 2a 20 65 61 63 68 20 72 65 70 72 65 73 65 6e 74  * each represent
31710 65 64 20 62 79 20 61 20 28 6c 65 6e 67 74 68 2c  ed by a (length,
31720 20 64 61 74 61 29 20 70 61 69 72 20 61 6e 64 20   data) pair and 
31730 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 20 65  encoded in the e
31740 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 68 61 74 20  ncoding.** that 
31750 77 61 73 20 70 61 73 73 65 64 20 61 73 20 74 68  was passed as th
31760 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  e third argument
31770 20 77 68 65 6e 20 74 68 65 20 63 6f 6c 6c 61 74   when the collat
31780 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 61 73  ion sequence was
31790 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 2e 20  .** registered. 
317a0 7b 45 4e 44 7d 20 54 68 65 20 61 70 70 6c 69 63  {END} The applic
317b0 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 63 6f  ation defined co
317c0 6c 6c 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20  llation routine 
317d0 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 72 6e  should.** return
317e0 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 20   negative, zero 
317f0 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66 0a 2a  or positive if.*
31800 2a 20 74 68 65 20 66 69 72 73 74 20 73 74 72 69  * the first stri
31810 6e 67 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c  ng is less than,
31820 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72   equal to, or gr
31830 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 73  eater than the s
31840 65 63 6f 6e 64 0a 2a 2a 20 73 74 72 69 6e 67 2e  econd.** string.
31850 20 69 2e 65 2e 20 28 53 54 52 49 4e 47 31 20 2d   i.e. (STRING1 -
31860 20 53 54 52 49 4e 47 32 29 2e 0a 2a 2a 0a 2a 2a   STRING2)..**.**
31870 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65   The sqlite3_cre
31880 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32  ate_collation_v2
31890 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 71  () works like sq
318a0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
318b0 6c 61 74 69 6f 6e 28 29 0a 2a 2a 20 65 78 63 61  lation().** exca
318c0 70 74 20 74 68 61 74 20 69 74 20 74 61 6b 65 73  pt that it takes
318d0 20 61 6e 20 65 78 74 72 61 20 61 72 67 75 6d 65   an extra argume
318e0 6e 74 20 77 68 69 63 68 20 69 73 20 61 20 64 65  nt which is a de
318f0 73 74 72 75 63 74 6f 72 20 66 6f 72 0a 2a 2a 20  structor for.** 
31900 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 2e 20 20  the collation.  
31910 54 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 69  The destructor i
31920 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68  s called when th
31930 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 69 73 0a 2a  e collation is.*
31940 2a 20 64 65 73 74 72 6f 79 65 64 20 61 6e 64 20  * destroyed and 
31950 69 73 20 70 61 73 73 65 64 20 61 20 63 6f 70 79  is passed a copy
31960 20 6f 66 20 74 68 65 20 66 6f 75 72 74 68 20 70   of the fourth p
31970 61 72 61 6d 65 74 65 72 20 76 6f 69 64 2a 20 70  arameter void* p
31980 6f 69 6e 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65  ointer.** of the
31990 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
319a0 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 2e 0a  collation_v2()..
319b0 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e 73 20 61 72  ** Collations ar
319c0 65 20 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e  e destroyed when
319d0 0a 2a 2a 20 74 68 65 79 20 61 72 65 20 6f 76 65  .** they are ove
319e0 72 72 69 64 64 65 6e 20 62 79 20 6c 61 74 65 72  rridden by later
319f0 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 63 6f   calls to the co
31a00 6c 6c 61 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e  llation creation
31a10 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6f 72   functions.** or
31a20 20 77 68 65 6e 20 74 68 65 20 5b 73 71 6c 69 74   when the [sqlit
31a30 65 33 2a 5d 20 64 61 74 61 62 61 73 65 20 68 61  e3*] database ha
31a40 6e 64 6c 65 20 69 73 20 63 6c 6f 73 65 64 20 75  ndle is closed u
31a50 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63 6c  sing [sqlite3_cl
31a60 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e  ose()]..**.** IN
31a70 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20  VARIANTS:.**.** 
31a80 7b 46 31 36 36 30 33 7d 20 41 20 73 75 63 63 65  {F16603} A succe
31a90 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 74 68  ssful call to th
31aa0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73  e.**          [s
31ab0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
31ac0 6c 6c 61 74 69 6f 6e 5f 76 32 28 42 2c 58 2c 45  llation_v2(B,X,E
31ad0 2c 50 2c 46 2c 44 29 5d 20 69 6e 74 65 72 66 61  ,P,F,D)] interfa
31ae0 63 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72  ce.**          r
31af0 65 67 69 73 74 65 72 73 20 66 75 6e 63 74 69 6f  egisters functio
31b00 6e 20 46 20 61 73 20 74 68 65 20 63 6f 6d 70 61  n F as the compa
31b10 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 75  rison function u
31b20 73 65 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20  sed to.**       
31b30 20 20 20 69 6d 70 6c 65 6d 65 6e 74 20 63 6f 6c     implement col
31b40 6c 61 74 69 6f 6e 20 58 20 6f 6e 20 5b 64 61 74  lation X on [dat
31b50 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
31b60 5d 20 42 20 66 6f 72 0a 2a 2a 20 20 20 20 20 20  ] B for.**      
31b70 20 20 20 20 64 61 74 61 62 61 73 65 73 20 68 61      databases ha
31b80 76 69 6e 67 20 65 6e 63 6f 64 69 6e 67 20 45 2e  ving encoding E.
31b90 0a 2a 2a 0a 2a 2a 20 7b 46 31 36 36 30 34 7d 20  .**.** {F16604} 
31ba0 53 51 4c 69 74 65 20 75 6e 64 65 72 73 74 61 6e  SQLite understan
31bb0 64 73 20 74 68 65 20 58 20 70 61 72 61 6d 65 74  ds the X paramet
31bc0 65 72 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20  er to.**        
31bd0 20 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74    [sqlite3_creat
31be0 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 42  e_collation_v2(B
31bf0 2c 58 2c 45 2c 50 2c 46 2c 44 29 5d 20 61 73 20  ,X,E,P,F,D)] as 
31c00 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  a zero-terminate
31c10 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 55 54  d.**          UT
31c20 46 2d 38 20 73 74 72 69 6e 67 20 69 6e 20 77 68  F-8 string in wh
31c30 69 63 68 20 63 61 73 65 20 69 73 20 69 67 6e 6f  ich case is igno
31c40 72 65 64 20 66 6f 72 20 41 53 43 49 49 20 63 68  red for ASCII ch
31c50 61 72 61 63 74 65 72 73 20 61 6e 64 0a 2a 2a 20  aracters and.** 
31c60 20 20 20 20 20 20 20 20 20 69 73 20 73 69 67 6e           is sign
31c70 69 66 69 63 61 6e 74 20 66 6f 72 20 6e 6f 6e 2d  ificant for non-
31c80 41 53 43 49 49 20 63 68 61 72 61 63 74 65 72 73  ASCII characters
31c90 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 36 36 30 36 7d  ..**.** {F16606}
31ca0 20 53 75 63 63 65 73 73 69 76 65 20 63 61 6c 6c   Successive call
31cb0 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 72  s to [sqlite3_cr
31cc0 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76  eate_collation_v
31cd0 32 28 42 2c 58 2c 45 2c 50 2c 46 2c 44 29 5d 0a  2(B,X,E,P,F,D)].
31ce0 2a 2a 20 20 20 20 20 20 20 20 20 20 77 69 74 68  **          with
31cf0 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 73   the same values
31d00 20 66 6f 72 20 42 2c 20 58 2c 20 61 6e 64 20 45   for B, X, and E
31d10 2c 20 6f 76 65 72 72 69 64 65 20 70 72 69 6f 72  , override prior
31d20 20 76 61 6c 75 65 73 0a 2a 2a 20 20 20 20 20 20   values.**      
31d30 20 20 20 20 6f 66 20 50 2c 20 46 2c 20 61 6e 64      of P, F, and
31d40 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 36 36 30   D..**.** {F1660
31d50 39 7d 20 54 68 65 20 64 65 73 74 72 75 63 74 6f  9} The destructo
31d60 72 20 44 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f  r D in [sqlite3_
31d70 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
31d80 5f 76 32 28 42 2c 58 2c 45 2c 50 2c 46 2c 44 29  _v2(B,X,E,P,F,D)
31d90 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73  ].**          is
31da0 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 69   not NULL then i
31db0 74 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 68  t is called with
31dc0 20 61 72 67 75 6d 65 6e 74 20 50 20 77 68 65 6e   argument P when
31dd0 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
31de0 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74   collating funct
31df0 69 6f 6e 20 69 73 20 64 72 6f 70 70 65 64 20 62  ion is dropped b
31e00 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20  y SQLite..**.** 
31e10 7b 46 31 36 36 31 32 7d 20 41 20 63 6f 6c 6c 61  {F16612} A colla
31e20 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69 73  ting function is
31e30 20 64 72 6f 70 70 65 64 20 77 68 65 6e 20 69 74   dropped when it
31e40 20 69 73 20 6f 76 65 72 6c 6f 61 64 65 64 2e 0a   is overloaded..
31e50 2a 2a 0a 2a 2a 20 7b 46 31 36 36 31 35 7d 20 41  **.** {F16615} A
31e60 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74   collating funct
31e70 69 6f 6e 20 69 73 20 64 72 6f 70 70 65 64 20 77  ion is dropped w
31e80 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
31e90 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 20   connection.**  
31ea0 20 20 20 20 20 20 20 20 69 73 20 63 6c 6f 73 65          is close
31eb0 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  d using [sqlite3
31ec0 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a  _close()]..**.**
31ed0 20 7b 46 31 36 36 31 38 7d 20 54 68 65 20 70 6f   {F16618} The po
31ee0 69 6e 74 65 72 20 50 20 69 6e 20 5b 73 71 6c 69  inter P in [sqli
31ef0 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
31f00 74 69 6f 6e 5f 76 32 28 42 2c 58 2c 45 2c 50 2c  tion_v2(B,X,E,P,
31f10 46 2c 44 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20  F,D)].**        
31f20 20 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f    is passed thro
31f30 75 67 68 20 61 73 20 74 68 65 20 66 69 72 73 74  ugh as the first
31f40 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68   parameter to th
31f50 65 20 63 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20  e comparison.** 
31f60 20 20 20 20 20 20 20 20 20 66 75 6e 63 74 69 6f           functio
31f70 6e 20 46 20 66 6f 72 20 61 6c 6c 20 73 75 62 73  n F for all subs
31f80 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f  equent invocatio
31f90 6e 73 20 6f 66 20 46 2e 0a 2a 2a 0a 2a 2a 20 7b  ns of F..**.** {
31fa0 46 31 36 36 32 31 7d 20 41 20 63 61 6c 6c 20 74  F16621} A call t
31fb0 6f 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74  o [sqlite3_creat
31fc0 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 42 2c 58 2c  e_collation(B,X,
31fd0 45 2c 50 2c 46 29 5d 20 69 73 20 65 78 61 63 74  E,P,F)] is exact
31fe0 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  ly.**          t
31ff0 68 65 20 73 61 6d 65 20 61 73 20 61 20 63 61 6c  he same as a cal
32000 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 72  l to [sqlite3_cr
32010 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76  eate_collation_v
32020 32 28 29 5d 20 77 69 74 68 0a 2a 2a 20 20 20 20  2()] with.**    
32030 20 20 20 20 20 20 74 68 65 20 73 61 6d 65 20 70        the same p
32040 61 72 61 6d 65 74 65 72 73 20 61 6e 64 20 61 20  arameters and a 
32050 4e 55 4c 4c 20 64 65 73 74 72 75 63 74 6f 72 2e  NULL destructor.
32060 0a 2a 2a 0a 2a 2a 20 7b 46 31 36 36 32 34 7d 20  .**.** {F16624} 
32070 46 6f 6c 6c 6f 77 69 6e 67 20 61 20 5b 73 71 6c  Following a [sql
32080 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
32090 61 74 69 6f 6e 5f 76 32 28 42 2c 58 2c 45 2c 50  ation_v2(B,X,E,P
320a0 2c 46 2c 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 20  ,F,D)],.**      
320b0 20 20 20 20 53 51 4c 69 74 65 20 75 73 65 73 20      SQLite uses 
320c0 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66  the comparison f
320d0 75 6e 63 74 69 6f 6e 20 46 20 66 6f 72 20 61 6c  unction F for al
320e0 6c 20 74 65 78 74 20 63 6f 6d 70 61 72 69 73 6f  l text compariso
320f0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 70  n.**          op
32100 65 72 61 74 69 6f 6e 73 20 6f 6e 20 5b 64 61 74  erations on [dat
32110 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
32120 5d 20 42 20 6f 6e 20 74 65 78 74 20 76 61 6c 75  ] B on text valu
32130 65 73 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20  es that.**      
32140 20 20 20 20 75 73 65 20 74 68 65 20 63 6f 6c 6c      use the coll
32150 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6e  ating sequence n
32160 61 6d 65 20 58 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31  ame X..**.** {F1
32170 36 36 32 37 7d 20 54 68 65 20 5b 73 71 6c 69 74  6627} The [sqlit
32180 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
32190 69 6f 6e 31 36 28 42 2c 58 2c 45 2c 50 2c 46 29  ion16(B,X,E,P,F)
321a0 5d 20 77 6f 72 6b 73 20 74 68 65 20 73 61 6d 65  ] works the same
321b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 73 20  .**          as 
321c0 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
321d0 63 6f 6c 6c 61 74 69 6f 6e 28 42 2c 58 2c 45 2c  collation(B,X,E,
321e0 50 2c 46 29 5d 20 65 78 63 65 70 74 20 74 68 61  P,F)] except tha
321f0 74 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  t the.**        
32200 20 20 63 6f 6c 6c 61 74 69 6f 6e 20 6e 61 6d 65    collation name
32210 20 58 20 69 73 20 75 6e 64 65 72 73 74 6f 6f 64   X is understood
32220 20 61 73 20 55 54 46 2d 31 36 20 69 6e 20 6e 61   as UTF-16 in na
32230 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 0a  tive byte order.
32240 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e 73 74  **          inst
32250 65 61 64 20 6f 66 20 55 54 46 2d 38 2e 0a 2a 2a  ead of UTF-8..**
32260 0a 2a 2a 20 7b 46 31 36 36 33 30 7d 20 57 68 65  .** {F16630} Whe
32270 6e 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6d 70 61  n multiple compa
32280 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20  rison functions 
32290 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 66 6f  are available fo
322a0 72 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 20 20  r the same.**   
322b0 20 20 20 20 20 20 20 63 6f 6c 6c 61 74 69 6e 67         collating
322c0 20 73 65 71 75 65 6e 63 65 2c 20 53 51 4c 69 74   sequence, SQLit
322d0 65 20 63 68 6f 6f 73 65 73 20 74 68 65 20 6f 6e  e chooses the on
322e0 65 20 77 68 6f 73 65 20 74 65 78 74 20 65 6e 63  e whose text enc
322f0 6f 64 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20  oding.**        
32300 20 20 72 65 71 75 69 72 65 73 20 74 68 65 20 6c    requires the l
32310 65 61 73 74 20 61 6d 6f 75 6e 74 20 6f 66 20 63  east amount of c
32320 6f 6e 76 65 72 73 69 6f 6e 20 66 72 6f 6d 20 74  onversion from t
32330 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 20 20  he default.**   
32340 20 20 20 20 20 20 20 74 65 78 74 20 65 6e 63 6f         text enco
32350 64 69 6e 67 20 6f 66 20 74 68 65 20 64 61 74 61  ding of the data
32360 62 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  base..*/.SQLITE_
32370 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
32380 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
32390 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20  (.  sqlite3*, . 
323a0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
323b0 6d 65 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 74  me, .  int eText
323c0 52 65 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20  Rep, .  void*,. 
323d0 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28   int(*xCompare)(
323e0 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
323f0 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
32400 76 6f 69 64 2a 29 0a 29 3b 0a 53 51 4c 49 54 45  void*).);.SQLITE
32410 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
32420 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
32430 6e 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 2a  n_v2(.  sqlite3*
32440 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
32450 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65  *zName, .  int e
32460 54 65 78 74 52 65 70 2c 20 0a 20 20 76 6f 69 64  TextRep, .  void
32470 2a 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61  *,.  int(*xCompa
32480 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  re)(void*,int,co
32490 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  nst void*,int,co
324a0 6e 73 74 20 76 6f 69 64 2a 29 2c 0a 20 20 76 6f  nst void*),.  vo
324b0 69 64 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f  id(*xDestroy)(vo
324c0 69 64 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41  id*).);.SQLITE_A
324d0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
324e0 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31  reate_collation1
324f0 36 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a  6(.  sqlite3*, .
32500 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
32510 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 54 65 78  ame, .  int eTex
32520 74 52 65 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a  tRep, .  void*,.
32530 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29    int(*xCompare)
32540 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74  (void*,int,const
32550 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74   void*,int,const
32560 20 76 6f 69 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a   void*).);../*.*
32570 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6c 6c  * CAPI3REF: Coll
32580 61 74 69 6f 6e 20 4e 65 65 64 65 64 20 43 61 6c  ation Needed Cal
32590 6c 62 61 63 6b 73 20 7b 46 31 36 37 30 30 7d 0a  lbacks {F16700}.
325a0 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 68  **.** To avoid h
325b0 61 76 69 6e 67 20 74 6f 20 72 65 67 69 73 74 65  aving to registe
325c0 72 20 61 6c 6c 20 63 6f 6c 6c 61 74 69 6f 6e 20  r all collation 
325d0 73 65 71 75 65 6e 63 65 73 20 62 65 66 6f 72 65  sequences before
325e0 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63   a database.** c
325f0 61 6e 20 62 65 20 75 73 65 64 2c 20 61 20 73 69  an be used, a si
32600 6e 67 6c 65 20 63 61 6c 6c 62 61 63 6b 20 66 75  ngle callback fu
32610 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 72 65  nction may be re
32620 67 69 73 74 65 72 65 64 20 77 69 74 68 20 74 68  gistered with th
32630 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 68 61  e.** database ha
32640 6e 64 6c 65 20 74 6f 20 62 65 20 63 61 6c 6c 65  ndle to be calle
32650 64 20 77 68 65 6e 65 76 65 72 20 61 6e 20 75 6e  d whenever an un
32660 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f  defined collatio
32670 6e 20 73 65 71 75 65 6e 63 65 20 69 73 0a 2a 2a  n sequence is.**
32680 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a   required..**.**
32690 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   If the function
326a0 20 69 73 20 72 65 67 69 73 74 65 72 65 64 20 75   is registered u
326b0 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33  sing the sqlite3
326c0 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  _collation_neede
326d0 64 28 29 20 41 50 49 2c 0a 2a 2a 20 74 68 65 6e  d() API,.** then
326e0 20 69 74 20 69 73 20 70 61 73 73 65 64 20 74 68   it is passed th
326f0 65 20 6e 61 6d 65 73 20 6f 66 20 75 6e 64 65 66  e names of undef
32700 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  ined collation s
32710 65 71 75 65 6e 63 65 73 20 61 73 20 73 74 72 69  equences as stri
32720 6e 67 73 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 69  ngs.** encoded i
32730 6e 20 55 54 46 2d 38 2e 20 7b 46 31 36 37 30 33  n UTF-8. {F16703
32740 7d 20 49 66 20 73 71 6c 69 74 65 33 5f 63 6f 6c  } If sqlite3_col
32750 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28  lation_needed16(
32760 29 20 69 73 20 75 73 65 64 2c 20 74 68 65 20 6e  ) is used, the n
32770 61 6d 65 73 0a 2a 2a 20 61 72 65 20 70 61 73 73  ames.** are pass
32780 65 64 20 61 73 20 55 54 46 2d 31 36 20 69 6e 20  ed as UTF-16 in 
32790 6d 61 63 68 69 6e 65 20 6e 61 74 69 76 65 20 62  machine native b
327a0 79 74 65 20 6f 72 64 65 72 2e 20 41 20 63 61 6c  yte order. A cal
327b0 6c 20 74 6f 20 65 69 74 68 65 72 0a 2a 2a 20 66  l to either.** f
327c0 75 6e 63 74 69 6f 6e 20 72 65 70 6c 61 63 65 73  unction replaces
327d0 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 63 61   any existing ca
327e0 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 57 68  llback..**.** Wh
327f0 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  en the callback 
32800 69 73 20 69 6e 76 6f 6b 65 64 2c 20 74 68 65 20  is invoked, the 
32810 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 70  first argument p
32820 61 73 73 65 64 20 69 73 20 61 20 63 6f 70 79 0a  assed is a copy.
32830 2a 2a 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64  ** of the second
32840 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c   argument to sql
32850 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
32860 65 65 64 65 64 28 29 20 6f 72 0a 2a 2a 20 73 71  eeded() or.** sq
32870 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  lite3_collation_
32880 6e 65 65 64 65 64 31 36 28 29 2e 20 20 54 68 65  needed16().  The
32890 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
328a0 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65   is the database
328b0 0a 2a 2a 20 68 61 6e 64 6c 65 2e 20 20 54 68 65  .** handle.  The
328c0 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20   third argument 
328d0 69 73 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54  is one of [SQLIT
328e0 45 5f 55 54 46 38 5d 2c 0a 2a 2a 20 5b 53 51 4c  E_UTF8],.** [SQL
328f0 49 54 45 5f 55 54 46 31 36 42 45 5d 2c 20 6f 72  ITE_UTF16BE], or
32900 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45   [SQLITE_UTF16LE
32910 5d 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68  ], indicating th
32920 65 20 6d 6f 73 74 0a 2a 2a 20 64 65 73 69 72 61  e most.** desira
32930 62 6c 65 20 66 6f 72 6d 20 6f 66 20 74 68 65 20  ble form of the 
32940 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
32950 63 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75  ce function requ
32960 69 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 6f 75  ired..** The fou
32970 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 73  rth parameter is
32980 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
32990 0a 2a 2a 20 72 65 71 75 69 72 65 64 20 63 6f 6c  .** required col
329a0 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 2e  lation sequence.
329b0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62  .**.** The callb
329c0 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f  ack function sho
329d0 75 6c 64 20 72 65 67 69 73 74 65 72 20 74 68 65  uld register the
329e0 20 64 65 73 69 72 65 64 20 63 6f 6c 6c 61 74 69   desired collati
329f0 6f 6e 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c  on using.** [sql
32a00 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
32a10 61 74 69 6f 6e 28 29 5d 2c 20 5b 73 71 6c 69 74  ation()], [sqlit
32a20 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
32a30 69 6f 6e 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20  ion16()], or.** 
32a40 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
32a50 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 5d 2e  collation_v2()].
32a60 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54  .**.** INVARIANT
32a70 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31 36 37 30 32  S:.**.** {F16702
32a80 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63  } A successful c
32a90 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
32aa0 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
32ab0 28 44 2c 50 2c 46 29 5d 0a 2a 2a 20 20 20 20 20  (D,P,F)].**     
32ac0 20 20 20 20 20 6f 72 20 5b 73 71 6c 69 74 65 33       or [sqlite3
32ad0 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  _collation_neede
32ae0 64 31 36 28 44 2c 50 2c 46 29 5d 20 63 61 75 73  d16(D,P,F)] caus
32af0 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  es.**          t
32b00 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
32b10 6e 65 63 74 69 6f 6e 5d 20 44 20 74 6f 20 69 6e  nection] D to in
32b20 76 6f 6b 65 20 63 61 6c 6c 62 61 63 6b 20 46 20  voke callback F 
32b30 77 69 74 68 20 66 69 72 73 74 0a 2a 2a 20 20 20  with first.**   
32b40 20 20 20 20 20 20 20 70 61 72 61 6d 65 74 65 72         parameter
32b50 20 50 20 77 68 65 6e 65 76 65 72 20 69 74 20 6e   P whenever it n
32b60 65 65 64 73 20 61 20 63 6f 6d 70 61 72 69 73 6f  eeds a compariso
32b70 6e 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61  n function for a
32b80 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6c  .**          col
32b90 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
32ba0 74 68 61 74 20 69 74 20 64 6f 65 73 20 6e 6f 74  that it does not
32bb0 20 6b 6e 6f 77 20 61 62 6f 75 74 2e 0a 2a 2a 0a   know about..**.
32bc0 2a 2a 20 7b 46 31 36 37 30 34 7d 20 45 61 63 68  ** {F16704} Each
32bd0 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c   successful call
32be0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c   to [sqlite3_col
32bf0 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 5d  lation_needed()]
32c00 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   or.**          
32c10 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69  [sqlite3_collati
32c20 6f 6e 5f 6e 65 65 64 65 64 31 36 28 29 5d 20 6f  on_needed16()] o
32c30 76 65 72 72 69 64 65 73 20 74 68 65 20 63 61 6c  verrides the cal
32c40 6c 62 61 63 6b 20 72 65 67 69 73 74 65 72 65 64  lback registered
32c50 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 6e 20  .**          on 
32c60 74 68 65 20 73 61 6d 65 20 5b 64 61 74 61 62 61  the same [databa
32c70 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 62  se connection] b
32c80 79 20 70 72 69 6f 72 20 63 61 6c 6c 73 20 74 6f  y prior calls to
32c90 20 65 69 74 68 65 72 0a 2a 2a 20 20 20 20 20 20   either.**      
32ca0 20 20 20 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a      interface..*
32cb0 2a 0a 2a 2a 20 7b 46 31 36 37 30 36 7d 20 54 68  *.** {F16706} Th
32cc0 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 72 65  e name of the re
32cd0 71 75 65 73 74 65 64 20 63 6f 6c 6c 61 74 69 6e  quested collatin
32ce0 67 20 66 75 6e 63 74 69 6f 6e 20 70 61 73 73 65  g function passe
32cf0 64 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20  d in the.**     
32d00 20 20 20 20 20 34 74 68 20 70 61 72 61 6d 65 74       4th paramet
32d10 65 72 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61  er to the callba
32d20 63 6b 20 69 73 20 69 6e 20 55 54 46 2d 38 20 69  ck is in UTF-8 i
32d30 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a  f the callback.*
32d40 2a 20 20 20 20 20 20 20 20 20 20 77 61 73 20 72  *          was r
32d50 65 67 69 73 74 65 72 65 64 20 75 73 69 6e 67 20  egistered using 
32d60 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69  [sqlite3_collati
32d70 6f 6e 5f 6e 65 65 64 65 64 28 29 5d 20 61 6e 64  on_needed()] and
32d80 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73 20  .**          is 
32d90 69 6e 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65  in UTF-16 native
32da0 20 62 79 74 65 20 6f 72 64 65 72 20 69 66 20 74   byte order if t
32db0 68 65 20 63 61 6c 6c 62 61 63 6b 20 77 61 73 0a  he callback was.
32dc0 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 67 69  **          regi
32dd0 73 74 65 72 65 64 20 75 73 69 6e 67 20 5b 73 71  stered using [sq
32de0 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  lite3_collation_
32df0 6e 65 65 64 65 64 31 36 28 29 5d 2e 0a 2a 2a 0a  needed16()]..**.
32e00 2a 2a 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ** .*/.SQLITE_AP
32e10 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f  I int sqlite3_co
32e20 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 0a  llation_needed(.
32e30 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 76    sqlite3*, .  v
32e40 6f 69 64 2a 2c 20 0a 20 20 76 6f 69 64 28 2a 29  oid*, .  void(*)
32e50 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c  (void*,sqlite3*,
32e60 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e  int eTextRep,con
32e70 73 74 20 63 68 61 72 2a 29 0a 29 3b 0a 53 51 4c  st char*).);.SQL
32e80 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
32e90 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65  te3_collation_ne
32ea0 65 64 65 64 31 36 28 0a 20 20 73 71 6c 69 74 65  eded16(.  sqlite
32eb0 33 2a 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20  3*, .  void*,.  
32ec0 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 73 71  void(*)(void*,sq
32ed0 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78 74  lite3*,int eText
32ee0 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29  Rep,const void*)
32ef0 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 70 65 63 69  .);../*.** Speci
32f00 66 79 20 74 68 65 20 6b 65 79 20 66 6f 72 20 61  fy the key for a
32f10 6e 20 65 6e 63 72 79 70 74 65 64 20 64 61 74 61  n encrypted data
32f20 62 61 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74  base.  This rout
32f30 69 6e 65 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a  ine should be.**
32f40 20 63 61 6c 6c 65 64 20 72 69 67 68 74 20 61 66   called right af
32f50 74 65 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  ter sqlite3_open
32f60 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  ()..**.** The co
32f70 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  de to implement 
32f80 74 68 69 73 20 41 50 49 20 69 73 20 6e 6f 74 20  this API is not 
32f90 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65  available in the
32fa0 20 70 75 62 6c 69 63 20 72 65 6c 65 61 73 65 0a   public release.
32fb0 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f  ** of SQLite..*/
32fc0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
32fd0 73 71 6c 69 74 65 33 5f 6b 65 79 28 0a 20 20 73  sqlite3_key(.  s
32fe0 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
32ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
33000 20 44 61 74 61 62 61 73 65 20 74 6f 20 62 65 20   Database to be 
33010 72 65 6b 65 79 65 64 20 2a 2f 0a 20 20 63 6f 6e  rekeyed */.  con
33020 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69  st void *pKey, i
33030 6e 74 20 6e 4b 65 79 20 20 20 20 20 2f 2a 20 54  nt nKey     /* T
33040 68 65 20 6b 65 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a  he key */.);../*
33050 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6b  .** Change the k
33060 65 79 20 6f 6e 20 61 6e 20 6f 70 65 6e 20 64 61  ey on an open da
33070 74 61 62 61 73 65 2e 20 20 49 66 20 74 68 65 20  tabase.  If the 
33080 63 75 72 72 65 6e 74 20 64 61 74 61 62 61 73 65  current database
33090 20 69 73 20 6e 6f 74 0a 2a 2a 20 65 6e 63 72 79   is not.** encry
330a0 70 74 65 64 2c 20 74 68 69 73 20 72 6f 75 74 69  pted, this routi
330b0 6e 65 20 77 69 6c 6c 20 65 6e 63 72 79 70 74 20  ne will encrypt 
330c0 69 74 2e 20 20 49 66 20 70 4e 65 77 3d 3d 30 20  it.  If pNew==0 
330d0 6f 72 20 6e 4e 65 77 3d 3d 30 2c 20 74 68 65 0a  or nNew==0, the.
330e0 2a 2a 20 64 61 74 61 62 61 73 65 20 69 73 20 64  ** database is d
330f0 65 63 72 79 70 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  ecrypted..**.** 
33100 54 68 65 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c  The code to impl
33110 65 6d 65 6e 74 20 74 68 69 73 20 41 50 49 20 69  ement this API i
33120 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20  s not available 
33130 69 6e 20 74 68 65 20 70 75 62 6c 69 63 20 72 65  in the public re
33140 6c 65 61 73 65 0a 2a 2a 20 6f 66 20 53 51 4c 69  lease.** of SQLi
33150 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  te..*/.SQLITE_AP
33160 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65  I int sqlite3_re
33170 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  key(.  sqlite3 *
33180 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
33190 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
331a0 65 20 74 6f 20 62 65 20 72 65 6b 65 79 65 64 20  e to be rekeyed 
331b0 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
331c0 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 20  *pKey, int nKey 
331d0 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 6b      /* The new k
331e0 65 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  ey */.);../*.** 
331f0 43 41 50 49 33 52 45 46 3a 20 20 53 75 73 70 65  CAPI3REF:  Suspe
33200 6e 64 20 45 78 65 63 75 74 69 6f 6e 20 46 6f 72  nd Execution For
33210 20 41 20 53 68 6f 72 74 20 54 69 6d 65 20 7b 46   A Short Time {F
33220 31 30 35 33 30 7d 0a 2a 2a 0a 2a 2a 20 54 68 65  10530}.**.** The
33230 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 28 29   sqlite3_sleep()
33240 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 63 61 75   function.** cau
33250 73 65 73 20 74 68 65 20 63 75 72 72 65 6e 74 20  ses the current 
33260 74 68 72 65 61 64 20 74 6f 20 73 75 73 70 65 6e  thread to suspen
33270 64 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 66  d execution.** f
33280 6f 72 20 61 74 20 6c 65 61 73 74 20 61 20 6e 75  or at least a nu
33290 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63  mber of millisec
332a0 6f 6e 64 73 20 73 70 65 63 69 66 69 65 64 20 69  onds specified i
332b0 6e 20 69 74 73 20 70 61 72 61 6d 65 74 65 72 2e  n its parameter.
332c0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70  .**.** If the op
332d0 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 64  erating system d
332e0 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20  oes not support 
332f0 73 6c 65 65 70 20 72 65 71 75 65 73 74 73 20 77  sleep requests w
33300 69 74 68 20 0a 2a 2a 20 6d 69 6c 6c 69 73 65 63  ith .** millisec
33310 6f 6e 64 20 74 69 6d 65 20 72 65 73 6f 6c 75 74  ond time resolut
33320 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 74 69  ion, then the ti
33330 6d 65 20 77 69 6c 6c 20 62 65 20 72 6f 75 6e 64  me will be round
33340 65 64 20 75 70 20 74 6f 20 0a 2a 2a 20 74 68 65  ed up to .** the
33350 20 6e 65 61 72 65 73 74 20 73 65 63 6f 6e 64 2e   nearest second.
33360 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d   The number of m
33370 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73  illiseconds of s
33380 6c 65 65 70 20 61 63 74 75 61 6c 6c 79 20 0a 2a  leep actually .*
33390 2a 20 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d  * requested from
333a0 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73   the operating s
333b0 79 73 74 65 6d 20 69 73 20 72 65 74 75 72 6e 65  ystem is returne
333c0 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  d..**.** SQLite 
333d0 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 69 73 20  implements this 
333e0 69 6e 74 65 72 66 61 63 65 20 62 79 20 63 61 6c  interface by cal
333f0 6c 69 6e 67 20 74 68 65 20 78 53 6c 65 65 70 28  ling the xSleep(
33400 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20 74  ).** method of t
33410 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71 6c 69  he default [sqli
33420 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e  te3_vfs] object.
33430 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54  .**.** INVARIANT
33440 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31 30 35 33 33  S:.**.** {F10533
33450 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73  } The [sqlite3_s
33460 6c 65 65 70 28 4d 29 5d 20 69 6e 74 65 72 66 61  leep(M)] interfa
33470 63 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 78  ce invokes the x
33480 53 6c 65 65 70 0a 2a 2a 20 20 20 20 20 20 20 20  Sleep.**        
33490 20 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20    method of the 
334a0 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33  default [sqlite3
334b0 5f 76 66 73 7c 56 46 53 5d 20 69 6e 20 6f 72 64  _vfs|VFS] in ord
334c0 65 72 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20  er to.**        
334d0 20 20 73 75 73 70 65 6e 64 20 65 78 65 63 75 74    suspend execut
334e0 69 6f 6e 20 6f 66 20 74 68 65 20 63 75 72 72 65  ion of the curre
334f0 6e 74 20 74 68 72 65 61 64 20 66 6f 72 20 61 74  nt thread for at
33500 20 6c 65 61 73 74 0a 2a 2a 20 20 20 20 20 20 20   least.**       
33510 20 20 20 4d 20 6d 69 6c 6c 69 73 65 63 6f 6e 64     M millisecond
33520 73 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 30 35 33 36  s..**.** {F10536
33530 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73  } The [sqlite3_s
33540 6c 65 65 70 28 4d 29 5d 20 69 6e 74 65 72 66 61  leep(M)] interfa
33550 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  ce returns the n
33560 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 20 20 20 20  umber of.**     
33570 20 20 20 20 20 6d 69 6c 6c 69 73 65 63 6f 6e 64       millisecond
33580 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74 75 61  s of sleep actua
33590 6c 6c 79 20 72 65 71 75 65 73 74 65 64 20 6f 66  lly requested of
335a0 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 0a 2a   the operating.*
335b0 2a 20 20 20 20 20 20 20 20 20 20 73 79 73 74 65  *          syste
335c0 6d 2c 20 77 68 69 63 68 20 6d 69 67 68 74 20 62  m, which might b
335d0 65 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68  e larger than th
335e0 65 20 70 61 72 61 6d 65 74 65 72 20 4d 2e 0a 2a  e parameter M..*
335f0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
33600 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 28 69   sqlite3_sleep(i
33610 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  nt);../*.** CAPI
33620 33 52 45 46 3a 20 20 4e 61 6d 65 20 4f 66 20 54  3REF:  Name Of T
33630 68 65 20 46 6f 6c 64 65 72 20 48 6f 6c 64 69 6e  he Folder Holdin
33640 67 20 54 65 6d 70 6f 72 61 72 79 20 46 69 6c 65  g Temporary File
33650 73 20 7b 46 31 30 33 31 30 7d 0a 2a 2a 0a 2a 2a  s {F10310}.**.**
33660 20 49 66 20 74 68 69 73 20 67 6c 6f 62 61 6c 20   If this global 
33670 76 61 72 69 61 62 6c 65 20 69 73 20 6d 61 64 65  variable is made
33680 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 20 73   to point to a s
33690 74 72 69 6e 67 20 77 68 69 63 68 20 69 73 0a 2a  tring which is.*
336a0 2a 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20  * the name of a 
336b0 66 6f 6c 64 65 72 20 28 61 2e 6b 61 2e 20 64 69  folder (a.ka. di
336c0 72 65 63 74 6f 72 79 29 2c 20 74 68 65 6e 20 61  rectory), then a
336d0 6c 6c 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c  ll temporary fil
336e0 65 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79  es.** created by
336f0 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20   SQLite will be 
33700 70 6c 61 63 65 64 20 69 6e 20 74 68 61 74 20 64  placed in that d
33710 69 72 65 63 74 6f 72 79 2e 20 20 49 66 20 74 68  irectory.  If th
33720 69 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69  is variable.** i
33730 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20  s NULL pointer, 
33740 74 68 65 6e 20 53 51 4c 69 74 65 20 64 6f 65 73  then SQLite does
33750 20 61 20 73 65 61 72 63 68 20 66 6f 72 20 61 6e   a search for an
33760 20 61 70 70 72 6f 70 72 69 61 74 65 20 74 65 6d   appropriate tem
33770 70 6f 72 61 72 79 0a 2a 2a 20 66 69 6c 65 20 64  porary.** file d
33780 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20  irectory..**.** 
33790 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74  It is not safe t
337a0 6f 20 6d 6f 64 69 66 79 20 74 68 69 73 20 76 61  o modify this va
337b0 72 69 61 62 6c 65 20 6f 6e 63 65 20 61 20 64 61  riable once a da
337c0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
337d0 6e 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 6f 70  n.** has been op
337e0 65 6e 65 64 2e 20 20 49 74 20 69 73 20 69 6e 74  ened.  It is int
337f0 65 6e 64 65 64 20 74 68 61 74 20 74 68 69 73 20  ended that this 
33800 76 61 72 69 61 62 6c 65 20 62 65 20 73 65 74 20  variable be set 
33810 6f 6e 63 65 0a 2a 2a 20 61 73 20 70 61 72 74 20  once.** as part 
33820 6f 66 20 70 72 6f 63 65 73 73 20 69 6e 69 74 69  of process initi
33830 61 6c 69 7a 61 74 69 6f 6e 20 61 6e 64 20 62 65  alization and be
33840 66 6f 72 65 20 61 6e 79 20 53 51 4c 69 74 65 20  fore any SQLite 
33850 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 72 6f 75  interface.** rou
33860 74 69 6e 65 73 20 68 61 76 65 20 62 65 65 6e 20  tines have been 
33870 63 61 6c 6c 20 61 6e 64 20 72 65 6d 61 69 6e 20  call and remain 
33880 75 6e 63 68 61 6e 67 65 64 20 74 68 65 72 65 61  unchanged therea
33890 66 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  fter..*/.SQLITE_
338a0 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65  API char *sqlite
338b0 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79  3_temp_directory
338c0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
338d0 46 3a 20 20 54 65 73 74 20 54 6f 20 53 65 65 20  F:  Test To See 
338e0 49 66 20 54 68 65 20 44 61 74 61 62 61 73 65 20  If The Database 
338f0 49 73 20 49 6e 20 41 75 74 6f 2d 43 6f 6d 6d 69  Is In Auto-Commi
33900 74 20 4d 6f 64 65 20 7b 46 31 32 39 33 30 7d 0a  t Mode {F12930}.
33910 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
33920 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74  3_get_autocommit
33930 28 29 20 69 6e 74 65 72 66 61 63 65 73 20 72 65  () interfaces re
33940 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 20 6f  turns non-zero o
33950 72 0a 2a 2a 20 7a 65 72 6f 20 69 66 20 74 68 65  r.** zero if the
33960 20 67 69 76 65 6e 20 64 61 74 61 62 61 73 65 20   given database 
33970 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 72  connection is or
33980 20 69 73 20 6e 6f 74 20 69 6e 20 61 75 74 6f 63   is not in autoc
33990 6f 6d 6d 69 74 20 6d 6f 64 65 2c 0a 2a 2a 20 72  ommit mode,.** r
339a0 65 73 70 65 63 74 69 76 65 6c 79 2e 20 20 20 41  espectively.   A
339b0 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69  utocommit mode i
339c0 73 20 6f 6e 0a 2a 2a 20 62 79 20 64 65 66 61 75  s on.** by defau
339d0 6c 74 2e 20 20 41 75 74 6f 63 6f 6d 6d 69 74 20  lt.  Autocommit 
339e0 6d 6f 64 65 20 69 73 20 64 69 73 61 62 6c 65 64  mode is disabled
339f0 20 62 79 20 61 20 5b 42 45 47 49 4e 5d 20 73 74   by a [BEGIN] st
33a00 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 41 75 74 6f  atement..** Auto
33a10 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 72  commit mode is r
33a20 65 65 6e 61 62 6c 65 64 20 62 79 20 61 20 5b 43  eenabled by a [C
33a30 4f 4d 4d 49 54 5d 20 6f 72 20 5b 52 4f 4c 4c 42  OMMIT] or [ROLLB
33a40 41 43 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63  ACK]..**.** If c
33a50 65 72 74 61 69 6e 20 6b 69 6e 64 73 20 6f 66 20  ertain kinds of 
33a60 65 72 72 6f 72 73 20 6f 63 63 75 72 20 6f 6e 20  errors occur on 
33a70 61 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68  a statement with
33a80 69 6e 20 61 20 6d 75 6c 74 69 2d 73 74 61 74 65  in a multi-state
33a90 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74  ment.** transact
33aa0 69 6f 6e 73 20 28 65 72 72 6f 72 73 20 69 6e 63  ions (errors inc
33ab0 6c 75 64 69 6e 67 20 5b 53 51 4c 49 54 45 5f 46  luding [SQLITE_F
33ac0 55 4c 4c 5d 2c 20 5b 53 51 4c 49 54 45 5f 49 4f  ULL], [SQLITE_IO
33ad0 45 52 52 5d 2c 20 0a 2a 2a 20 5b 53 51 4c 49 54  ERR], .** [SQLIT
33ae0 45 5f 4e 4f 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54  E_NOMEM], [SQLIT
33af0 45 5f 42 55 53 59 5d 2c 20 61 6e 64 20 5b 53 51  E_BUSY], and [SQ
33b00 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 29  LITE_INTERRUPT])
33b10 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 74 72 61   then the.** tra
33b20 6e 73 61 63 74 69 6f 6e 20 6d 69 67 68 74 20 62  nsaction might b
33b30 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 75  e rolled back au
33b40 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 54 68  tomatically.  Th
33b50 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f 0a 2a 2a  e only way to.**
33b60 20 66 69 6e 64 20 6f 75 74 20 69 66 20 53 51 4c   find out if SQL
33b70 69 74 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  ite automaticall
33b80 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68  y rolled back th
33b90 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 66  e transaction af
33ba0 74 65 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20  ter.** an error 
33bb0 69 73 20 74 6f 20 75 73 65 20 74 68 69 73 20 66  is to use this f
33bc0 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49  unction..**.** I
33bd0 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a  NVARIANTS:.**.**
33be0 20 7b 46 31 32 39 33 31 7d 20 54 68 65 20 5b 73   {F12931} The [s
33bf0 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63  qlite3_get_autoc
33c00 6f 6d 6d 69 74 28 44 29 5d 20 69 6e 74 65 72 66  ommit(D)] interf
33c10 61 63 65 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d  ace returns non-
33c20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 20 20 20 20 20  zero or.**      
33c30 20 20 20 20 7a 65 72 6f 20 69 66 20 74 68 65 20      zero if the 
33c40 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
33c50 74 69 6f 6e 5d 20 44 20 69 73 20 6f 72 20 69 73  tion] D is or is
33c60 20 6e 6f 74 20 69 6e 20 61 75 74 6f 63 6f 6d 6d   not in autocomm
33c70 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6d  it.**          m
33c80 6f 64 65 2c 20 72 65 73 70 65 63 74 69 76 65 6c  ode, respectivel
33c90 79 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 39 33 32  y..**.** {F12932
33ca0 7d 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64  } Autocommit mod
33cb0 65 20 69 73 20 6f 6e 20 62 79 20 64 65 66 61 75  e is on by defau
33cc0 6c 74 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 39 33  lt..**.** {F1293
33cd0 33 7d 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f  3} Autocommit mo
33ce0 64 65 20 69 73 20 64 69 73 61 62 6c 65 64 20 62  de is disabled b
33cf0 79 20 61 20 73 75 63 63 65 73 73 66 75 6c 20 5b  y a successful [
33d00 42 45 47 49 4e 5d 20 73 74 61 74 65 6d 65 6e 74  BEGIN] statement
33d10 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 39 33 34 7d  ..**.** {F12934}
33d20 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65   Autocommit mode
33d30 20 69 73 20 65 6e 61 62 6c 65 64 20 62 79 20 61   is enabled by a
33d40 20 73 75 63 63 65 73 73 66 75 6c 20 5b 43 4f 4d   successful [COM
33d50 4d 49 54 5d 20 6f 72 20 5b 52 4f 4c 4c 42 41 43  MIT] or [ROLLBAC
33d60 4b 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73  K].**          s
33d70 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 0a 2a 2a  tatement..** .**
33d80 0a 2a 2a 20 4c 49 4d 49 54 41 54 49 4f 4e 53 3a  .** LIMITATIONS:
33d90 0a 2a 2a 2a 0a 2a 2a 20 7b 55 31 32 39 33 36 7d  .***.** {U12936}
33da0 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65   If another thre
33db0 61 64 20 63 68 61 6e 67 65 73 20 74 68 65 20 61  ad changes the a
33dc0 75 74 6f 63 6f 6d 6d 69 74 20 73 74 61 74 75 73  utocommit status
33dd0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
33de0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e  .**          con
33df0 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68  nection while th
33e00 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 75  is routine is ru
33e10 6e 6e 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 20  nning, then the 
33e20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20  return value.** 
33e30 20 20 20 20 20 20 20 20 20 69 73 20 75 6e 64 65           is unde
33e40 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  fined..*/.SQLITE
33e50 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
33e60 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28  _get_autocommit(
33e70 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a  sqlite3*);../*.*
33e80 2a 20 43 41 50 49 33 52 45 46 3a 20 20 46 69 6e  * CAPI3REF:  Fin
33e90 64 20 54 68 65 20 44 61 74 61 62 61 73 65 20 48  d The Database H
33ea0 61 6e 64 6c 65 20 4f 66 20 41 20 50 72 65 70 61  andle Of A Prepa
33eb0 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 46  red Statement {F
33ec0 31 33 31 32 30 7d 0a 2a 2a 0a 2a 2a 20 54 68 65  13120}.**.** The
33ed0 20 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64   sqlite3_db_hand
33ee0 6c 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20  le interface.** 
33ef0 72 65 74 75 72 6e 73 20 74 68 65 20 5b 73 71 6c  returns the [sql
33f00 69 74 65 33 2a 5d 20 64 61 74 61 62 61 73 65 20  ite3*] database 
33f10 68 61 6e 64 6c 65 20 74 6f 20 77 68 69 63 68 20  handle to which 
33f20 61 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73  a.** [prepared s
33f30 74 61 74 65 6d 65 6e 74 5d 20 62 65 6c 6f 6e 67  tatement] belong
33f40 73 2e 0a 2a 2a 20 54 68 65 20 64 61 74 61 62 61  s..** The databa
33f50 73 65 20 68 61 6e 64 6c 65 20 72 65 74 75 72 6e  se handle return
33f60 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 64 62  ed by sqlite3_db
33f70 5f 68 61 6e 64 6c 65 0a 2a 2a 20 69 73 20 74 68  _handle.** is th
33f80 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20  e same database 
33f90 68 61 6e 64 6c 65 20 74 68 61 74 20 77 61 73 0a  handle that was.
33fa0 2a 2a 20 74 68 65 20 66 69 72 73 74 20 61 72 67  ** the first arg
33fb0 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 5b 73 71  ument to the [sq
33fc0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
33fd0 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72 69 61  ()] or its varia
33fe0 6e 74 73 0a 2a 2a 20 74 68 61 74 20 77 61 73 20  nts.** that was 
33ff0 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 74  used to create t
34000 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20  he statement in 
34010 74 68 65 20 66 69 72 73 74 20 70 6c 61 63 65 2e  the first place.
34020 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54  .**.** INVARIANT
34030 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31 33 31 32 33  S:.**.** {F13123
34040 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 64  } The [sqlite3_d
34050 62 5f 68 61 6e 64 6c 65 28 53 29 5d 20 69 6e 74  b_handle(S)] int
34060 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61  erface returns a
34070 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 20 20 20 20   pointer.**     
34080 20 20 20 20 20 74 6f 20 74 68 65 20 5b 64 61 74       to the [dat
34090 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
340a0 5d 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  ] associated wit
340b0 68 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70  h.**          [p
340c0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
340d0 74 5d 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  t] S..*/.SQLITE_
340e0 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c  API sqlite3 *sql
340f0 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73  ite3_db_handle(s
34100 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a  qlite3_stmt*);..
34110 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
34120 20 43 6f 6d 6d 69 74 20 41 6e 64 20 52 6f 6c 6c   Commit And Roll
34130 62 61 63 6b 20 4e 6f 74 69 66 69 63 61 74 69 6f  back Notificatio
34140 6e 20 43 61 6c 6c 62 61 63 6b 73 20 7b 46 31 32  n Callbacks {F12
34150 39 35 30 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  950}.**.** The s
34160 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f  qlite3_commit_ho
34170 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72  ok() interface r
34180 65 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62  egisters a callb
34190 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  ack.** function 
341a0 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68  to be invoked wh
341b0 65 6e 65 76 65 72 20 61 20 74 72 61 6e 73 61 63  enever a transac
341c0 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65  tion is committe
341d0 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61  d..** Any callba
341e0 63 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 76  ck set by a prev
341f0 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c  ious call to sql
34200 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b  ite3_commit_hook
34210 28 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61  ().** for the sa
34220 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  me database conn
34230 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69  ection is overri
34240 64 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 73 71 6c  dden..** The sql
34250 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f  ite3_rollback_ho
34260 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72  ok() interface r
34270 65 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62  egisters a callb
34280 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  ack.** function 
34290 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68  to be invoked wh
342a0 65 6e 65 76 65 72 20 61 20 74 72 61 6e 73 61 63  enever a transac
342b0 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65  tion is committe
342c0 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61  d..** Any callba
342d0 63 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 76  ck set by a prev
342e0 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c  ious call to sql
342f0 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b  ite3_commit_hook
34300 28 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61  ().** for the sa
34310 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  me database conn
34320 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69  ection is overri
34330 64 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 70 41 72  dden..** The pAr
34340 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20 70 61  g argument is pa
34350 73 73 65 64 20 74 68 72 6f 75 67 68 0a 2a 2a 20  ssed through.** 
34360 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e  to the callback.
34370 20 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63    If the callbac
34380 6b 20 6f 6e 20 61 20 63 6f 6d 6d 69 74 20 68 6f  k on a commit ho
34390 6f 6b 20 66 75 6e 63 74 69 6f 6e 20 0a 2a 2a 20  ok function .** 
343a0 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f  returns non-zero
343b0 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69  , then the commi
343c0 74 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20 69  t is converted i
343d0 6e 74 6f 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a  nto a rollback..
343e0 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72  **.** If another
343f0 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 70 72   function was pr
34400 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65  eviously registe
34410 72 65 64 2c 20 69 74 73 0a 2a 2a 20 70 41 72 67  red, its.** pArg
34420 20 76 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e   value is return
34430 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 4e  ed.  Otherwise N
34440 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ULL is returned.
34450 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 69  .**.** Registeri
34460 6e 67 20 61 20 4e 55 4c 4c 20 66 75 6e 63 74 69  ng a NULL functi
34470 6f 6e 20 64 69 73 61 62 6c 65 73 20 74 68 65 20  on disables the 
34480 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20  callback..**.** 
34490 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73  For the purposes
344a0 20 6f 66 20 74 68 69 73 20 41 50 49 2c 20 61 20   of this API, a 
344b0 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73  transaction is s
344c0 61 69 64 20 74 6f 20 68 61 76 65 20 62 65 65 6e  aid to have been
344d0 20 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b   .** rolled back
344e0 20 69 66 20 61 6e 20 65 78 70 6c 69 63 69 74 20   if an explicit 
344f0 22 52 4f 4c 4c 42 41 43 4b 22 20 73 74 61 74 65  "ROLLBACK" state
34500 6d 65 6e 74 20 69 73 20 65 78 65 63 75 74 65 64  ment is executed
34510 2c 20 6f 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72  , or.** an error
34520 20 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74 20 63   or constraint c
34530 61 75 73 65 73 20 61 6e 20 69 6d 70 6c 69 63 69  auses an implici
34540 74 20 72 6f 6c 6c 62 61 63 6b 20 74 6f 20 6f 63  t rollback to oc
34550 63 75 72 2e 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c  cur..** The roll
34560 62 61 63 6b 20 63 61 6c 6c 62 61 63 6b 20 69 73  back callback is
34570 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 69 66 20   not invoked if 
34580 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
34590 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  .** automaticall
345a0 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 62 65  y rolled back be
345b0 63 61 75 73 65 20 74 68 65 20 64 61 74 61 62 61  cause the databa
345c0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  se connection is
345d0 20 63 6c 6f 73 65 64 2e 0a 2a 2a 20 54 68 65 20   closed..** The 
345e0 72 6f 6c 6c 62 61 63 6b 20 63 61 6c 6c 62 61 63  rollback callbac
345f0 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64  k is not invoked
34600 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f   if a transactio
34610 6e 20 69 73 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62  n is.** rolled b
34620 61 63 6b 20 62 65 63 61 75 73 65 20 61 20 63 6f  ack because a co
34630 6d 6d 69 74 20 63 61 6c 6c 62 61 63 6b 20 72 65  mmit callback re
34640 74 75 72 6e 65 64 20 6e 6f 6e 2d 7a 65 72 6f 2e  turned non-zero.
34650 0a 2a 2a 20 3c 74 6f 64 6f 3e 20 43 68 65 63 6b  .** <todo> Check
34660 20 6f 6e 20 74 68 69 73 20 3c 2f 74 6f 64 6f 3e   on this </todo>
34670 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61 72 65  .**.** These are
34680 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e   experimental in
34690 74 65 72 66 61 63 65 73 20 61 6e 64 20 61 72 65  terfaces and are
346a0 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e   subject to chan
346b0 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49  ge..**.** INVARI
346c0 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31 32  ANTS:.**.** {F12
346d0 39 35 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  951} The [sqlite
346e0 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 44 2c  3_commit_hook(D,
346f0 46 2c 50 29 5d 20 69 6e 74 65 72 66 61 63 65 20  F,P)] interface 
34700 72 65 67 69 73 74 65 72 73 20 74 68 65 0a 2a 2a  registers the.**
34710 20 20 20 20 20 20 20 20 20 20 63 61 6c 6c 62 61            callba
34720 63 6b 20 66 75 6e 63 74 69 6f 6e 20 46 20 74 6f  ck function F to
34730 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 69 74 68   be invoked with
34740 20 61 72 67 75 6d 65 6e 74 20 50 20 77 68 65 6e   argument P when
34750 65 76 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  ever.**         
34760 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63   a transaction c
34770 6f 6d 6d 69 74 73 20 6f 6e 20 5b 64 61 74 61 62  ommits on [datab
34780 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
34790 44 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 39 35 32  D..**.** {F12952
347a0 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63  } The [sqlite3_c
347b0 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 44 2c 46 2c 50  ommit_hook(D,F,P
347c0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  )] interface ret
347d0 75 72 6e 73 20 74 68 65 20 50 0a 2a 2a 20 20 20  urns the P.**   
347e0 20 20 20 20 20 20 20 61 72 67 75 6d 65 6e 74 20         argument 
347f0 66 72 6f 6d 20 74 68 65 20 70 72 65 76 69 6f 75  from the previou
34800 73 20 63 61 6c 6c 20 77 69 74 68 20 74 68 65 20  s call with the 
34810 73 61 6d 65 20 0a 2a 2a 20 20 20 20 20 20 20 20  same .**        
34820 20 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e    [database conn
34830 65 63 74 69 6f 6e 20 5d 20 44 20 2c 20 6f 72 20  ection ] D , or 
34840 4e 55 4c 4c 20 6f 6e 20 74 68 65 20 66 69 72 73  NULL on the firs
34850 74 20 63 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20  t call.**       
34860 20 20 20 66 6f 72 20 61 20 70 61 72 74 69 63 75     for a particu
34870 6c 61 72 20 5b 64 61 74 61 62 61 73 65 20 63 6f  lar [database co
34880 6e 6e 65 63 74 69 6f 6e 5d 20 44 2e 0a 2a 2a 0a  nnection] D..**.
34890 2a 2a 20 7b 46 31 32 39 35 33 7d 20 45 61 63 68  ** {F12953} Each
348a0 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
348b0 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 5d  3_commit_hook()]
348c0 20 6f 76 65 72 77 72 69 74 65 73 20 74 68 65 20   overwrites the 
348d0 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20 20 20  callback.**     
348e0 20 20 20 20 20 72 65 67 69 73 74 65 72 65 64 20       registered 
348f0 62 79 20 70 72 69 6f 72 20 63 61 6c 6c 73 2e 0a  by prior calls..
34900 2a 2a 0a 2a 2a 20 7b 46 31 32 39 35 34 7d 20 49  **.** {F12954} I
34910 66 20 74 68 65 20 46 20 61 72 67 75 6d 65 6e 74  f the F argument
34920 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d   to [sqlite3_com
34930 6d 69 74 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d  mit_hook(D,F,P)]
34940 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 20 20 20 20   is NULL.**     
34950 20 20 20 20 20 74 68 65 6e 20 74 68 65 20 63 6f       then the co
34960 6d 6d 69 74 20 68 6f 6f 6b 20 63 61 6c 6c 62 61  mmit hook callba
34970 63 6b 20 69 73 20 63 61 6e 63 65 6c 6c 65 64 20  ck is cancelled 
34980 61 6e 64 20 6e 6f 20 63 61 6c 6c 62 61 63 6b 0a  and no callback.
34990 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73 20 69  **          is i
349a0 6e 76 6f 6b 65 64 20 77 68 65 6e 20 61 20 74 72  nvoked when a tr
349b0 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6d 6d 69 74  ansaction commit
349c0 73 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 39 35 35  s..**.** {F12955
349d0 7d 20 49 66 20 74 68 65 20 63 6f 6d 6d 69 74 20  } If the commit 
349e0 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73  callback returns
349f0 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74   non-zero then t
34a00 68 65 20 63 6f 6d 6d 69 74 20 69 73 0a 2a 2a 20  he commit is.** 
34a10 20 20 20 20 20 20 20 20 20 63 6f 6e 76 65 72 74           convert
34a20 65 64 20 69 6e 74 6f 20 61 20 72 6f 6c 6c 62 61  ed into a rollba
34a30 63 6b 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 39 36  ck..**.** {F1296
34a40 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  1} The [sqlite3_
34a50 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 44 2c  rollback_hook(D,
34a60 46 2c 50 29 5d 20 69 6e 74 65 72 66 61 63 65 20  F,P)] interface 
34a70 72 65 67 69 73 74 65 72 73 20 74 68 65 0a 2a 2a  registers the.**
34a80 20 20 20 20 20 20 20 20 20 20 63 61 6c 6c 62 61            callba
34a90 63 6b 20 66 75 6e 63 74 69 6f 6e 20 46 20 74 6f  ck function F to
34aa0 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 69 74 68   be invoked with
34ab0 20 61 72 67 75 6d 65 6e 74 20 50 20 77 68 65 6e   argument P when
34ac0 65 76 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  ever.**         
34ad0 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72   a transaction r
34ae0 6f 6c 6c 73 20 62 61 63 6b 20 6f 6e 20 5b 64 61  olls back on [da
34af0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
34b00 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32  n] D..**.** {F12
34b10 39 36 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  962} The [sqlite
34b20 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28  3_rollback_hook(
34b30 44 2c 46 2c 50 29 5d 20 69 6e 74 65 72 66 61 63  D,F,P)] interfac
34b40 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 50 0a  e returns the P.
34b50 2a 2a 20 20 20 20 20 20 20 20 20 20 61 72 67 75  **          argu
34b60 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 70 72  ment from the pr
34b70 65 76 69 6f 75 73 20 63 61 6c 6c 20 77 69 74 68  evious call with
34b80 20 74 68 65 20 73 61 6d 65 20 0a 2a 2a 20 20 20   the same .**   
34b90 20 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 65         [database
34ba0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 5d 20 44 20   connection ] D 
34bb0 2c 20 6f 72 20 4e 55 4c 4c 20 6f 6e 20 74 68 65  , or NULL on the
34bc0 20 66 69 72 73 74 20 63 61 6c 6c 0a 2a 2a 20 20   first call.**  
34bd0 20 20 20 20 20 20 20 20 66 6f 72 20 61 20 70 61          for a pa
34be0 72 74 69 63 75 6c 61 72 20 5b 64 61 74 61 62 61  rticular [databa
34bf0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44  se connection] D
34c00 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 39 36 33 7d  ..**.** {F12963}
34c10 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73   Each call to [s
34c20 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f  qlite3_rollback_
34c30 68 6f 6f 6b 28 29 5d 20 6f 76 65 72 77 72 69 74  hook()] overwrit
34c40 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a  es the callback.
34c50 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 67 69  **          regi
34c60 73 74 65 72 65 64 20 62 79 20 70 72 69 6f 72 20  stered by prior 
34c70 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31  calls..**.** {F1
34c80 32 39 36 34 7d 20 49 66 20 74 68 65 20 46 20 61  2964} If the F a
34c90 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c 69  rgument to [sqli
34ca0 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f  te3_rollback_hoo
34cb0 6b 28 44 2c 46 2c 50 29 5d 20 69 73 20 4e 55 4c  k(D,F,P)] is NUL
34cc0 4c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  L.**          th
34cd0 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  en the rollback 
34ce0 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b 20 69 73  hook callback is
34cf0 20 63 61 6e 63 65 6c 6c 65 64 20 61 6e 64 20 6e   cancelled and n
34d00 6f 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20  o callback.**   
34d10 20 20 20 20 20 20 20 69 73 20 69 6e 76 6f 6b 65         is invoke
34d20 64 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63  d when a transac
34d30 74 69 6f 6e 20 72 6f 6c 6c 73 20 62 61 63 6b 2e  tion rolls back.
34d40 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
34d50 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6d  oid *sqlite3_com
34d60 6d 69 74 5f 68 6f 6f 6b 28 73 71 6c 69 74 65 33  mit_hook(sqlite3
34d70 2a 2c 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29  *, int(*)(void*)
34d80 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45  , void*);.SQLITE
34d90 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
34da0 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b  e3_rollback_hook
34db0 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 28  (sqlite3*, void(
34dc0 2a 29 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69 64  *)(void *), void
34dd0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  *);../*.** CAPI3
34de0 52 45 46 3a 20 44 61 74 61 20 43 68 61 6e 67 65  REF: Data Change
34df0 20 4e 6f 74 69 66 69 63 61 74 69 6f 6e 20 43 61   Notification Ca
34e00 6c 6c 62 61 63 6b 73 20 7b 46 31 32 39 37 30 7d  llbacks {F12970}
34e10 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
34e20 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29  e3_update_hook()
34e30 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 72 65   interface.** re
34e40 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61  gisters a callba
34e50 63 6b 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68  ck function with
34e60 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
34e70 6e 6e 65 63 74 69 6f 6e 20 69 64 65 6e 74 69 66  nnection identif
34e80 69 65 64 20 62 79 20 74 68 65 20 0a 2a 2a 20 66  ied by the .** f
34e90 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
34ea0 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e   be invoked when
34eb0 65 76 65 72 20 61 20 72 6f 77 20 69 73 20 75 70  ever a row is up
34ec0 64 61 74 65 64 2c 20 69 6e 73 65 72 74 65 64 20  dated, inserted 
34ed0 6f 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 41  or deleted..** A
34ee0 6e 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20  ny callback set 
34ef0 62 79 20 61 20 70 72 65 76 69 6f 75 73 20 63 61  by a previous ca
34f00 6c 6c 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  ll to this funct
34f10 69 6f 6e 20 66 6f 72 20 74 68 65 20 73 61 6d 65  ion for the same
34f20 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f   .** database co
34f30 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72  nnection is over
34f40 72 69 64 64 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  ridden..**.** Th
34f50 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
34f60 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  t is a pointer t
34f70 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74  o the function t
34f80 6f 20 69 6e 76 6f 6b 65 20 77 68 65 6e 20 61 20  o invoke when a 
34f90 0a 2a 2a 20 72 6f 77 20 69 73 20 75 70 64 61 74  .** row is updat
34fa0 65 64 2c 20 69 6e 73 65 72 74 65 64 20 6f 72 20  ed, inserted or 
34fb0 64 65 6c 65 74 65 64 2e 20 0a 2a 2a 20 54 68 65  deleted. .** The
34fc0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
34fd0 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  to the callback 
34fe0 69 73 0a 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20  is.** a copy of 
34ff0 74 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65  the third argume
35000 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 70  nt to sqlite3_up
35010 64 61 74 65 5f 68 6f 6f 6b 28 29 2e 0a 2a 2a 20  date_hook()..** 
35020 54 68 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c 62  The second callb
35030 61 63 6b 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  ack .** argument
35040 20 69 73 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49   is one of [SQLI
35050 54 45 5f 49 4e 53 45 52 54 5d 2c 20 5b 53 51 4c  TE_INSERT], [SQL
35060 49 54 45 5f 44 45 4c 45 54 45 5d 20 6f 72 20 5b  ITE_DELETE] or [
35070 53 51 4c 49 54 45 5f 55 50 44 41 54 45 5d 2c 0a  SQLITE_UPDATE],.
35080 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20  ** depending on 
35090 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 68  the operation th
350a0 61 74 20 63 61 75 73 65 64 20 74 68 65 20 63 61  at caused the ca
350b0 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20 69 6e 76  llback to be inv
350c0 6f 6b 65 64 2e 0a 2a 2a 20 54 68 65 20 74 68 69  oked..** The thi
350d0 72 64 20 61 6e 64 20 0a 2a 2a 20 66 6f 75 72 74  rd and .** fourt
350e0 68 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74  h arguments to t
350f0 68 65 20 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74  he callback cont
35100 61 69 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  ain pointers to 
35110 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e 64  the database and
35120 20 0a 2a 2a 20 74 61 62 6c 65 20 6e 61 6d 65 20   .** table name 
35130 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 61  containing the a
35140 66 66 65 63 74 65 64 20 72 6f 77 2e 0a 2a 2a 20  ffected row..** 
35150 54 68 65 20 66 69 6e 61 6c 20 63 61 6c 6c 62 61  The final callba
35160 63 6b 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  ck parameter is 
35170 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64 20 6f 66  .** the rowid of
35180 20 74 68 65 20 72 6f 77 2e 0a 2a 2a 20 49 6e 20   the row..** In 
35190 74 68 65 20 63 61 73 65 20 6f 66 20 61 6e 20 75  the case of an u
351a0 70 64 61 74 65 2c 20 74 68 69 73 20 69 73 20 74  pdate, this is t
351b0 68 65 20 72 6f 77 69 64 20 61 66 74 65 72 20 0a  he rowid after .
351c0 2a 2a 20 74 68 65 20 75 70 64 61 74 65 20 74 61  ** the update ta
351d0 6b 65 73 20 70 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a  kes place..**.**
351e0 20 54 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b   The update hook
351f0 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20   is not invoked 
35200 77 68 65 6e 20 69 6e 74 65 72 6e 61 6c 20 73 79  when internal sy
35210 73 74 65 6d 20 74 61 62 6c 65 73 20 61 72 65 0a  stem tables are.
35220 2a 2a 20 6d 6f 64 69 66 69 65 64 20 28 69 2e 65  ** modified (i.e
35230 2e 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  . sqlite_master 
35240 61 6e 64 20 73 71 6c 69 74 65 5f 73 65 71 75 65  and sqlite_seque
35250 6e 63 65 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  nce)..**.** If a
35260 6e 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20  nother function 
35270 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 72  was previously r
35280 65 67 69 73 74 65 72 65 64 2c 20 69 74 73 20 70  egistered, its p
35290 41 72 67 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20  Arg value.** is 
352a0 72 65 74 75 72 6e 65 64 2e 20 20 4f 74 68 65 72  returned.  Other
352b0 77 69 73 65 20 4e 55 4c 4c 20 69 73 20 72 65 74  wise NULL is ret
352c0 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56  urned..**.** INV
352d0 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b  ARIANTS:.**.** {
352e0 46 31 32 39 37 31 7d 20 54 68 65 20 5b 73 71 6c  F12971} The [sql
352f0 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b  ite3_update_hook
35300 28 44 2c 46 2c 50 29 5d 20 69 6e 74 65 72 66 61  (D,F,P)] interfa
35310 63 65 20 63 61 75 73 65 73 20 63 61 6c 6c 62 61  ce causes callba
35320 63 6b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66  ck.**          f
35330 75 6e 63 74 69 6f 6e 20 46 20 74 6f 20 62 65 20  unction F to be 
35340 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 66 69 72  invoked with fir
35350 73 74 20 70 61 72 61 6d 65 74 65 72 20 50 20 77  st parameter P w
35360 68 65 6e 65 76 65 72 0a 2a 2a 20 20 20 20 20 20  henever.**      
35370 20 20 20 20 61 20 74 61 62 6c 65 20 72 6f 77 20      a table row 
35380 69 73 20 6d 6f 64 69 66 69 65 64 2c 20 69 6e 73  is modified, ins
35390 65 72 74 65 64 2c 20 6f 72 20 64 65 6c 65 74 65  erted, or delete
353a0 64 20 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  d on.**         
353b0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
353c0 63 74 69 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20  ction] D..**.** 
353d0 7b 46 31 32 39 37 33 7d 20 54 68 65 20 5b 73 71  {F12973} The [sq
353e0 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f  lite3_update_hoo
353f0 6b 28 44 2c 46 2c 50 29 5d 20 69 6e 74 65 72 66  k(D,F,P)] interf
35400 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ace returns the 
35410 76 61 6c 75 65 0a 2a 2a 20 20 20 20 20 20 20 20  value.**        
35420 20 20 6f 66 20 50 20 66 6f 72 20 74 68 65 20 70    of P for the p
35430 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 6f 6e 20  revious call on 
35440 74 68 65 20 73 61 6d 65 20 5b 64 61 74 61 62 61  the same [databa
35450 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44  se connection] D
35460 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72  ,.**          or
35470 20 4e 55 4c 4c 20 66 6f 72 20 74 68 65 20 66 69   NULL for the fi
35480 72 73 74 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20  rst call..**.** 
35490 7b 46 31 32 39 37 35 7d 20 49 66 20 74 68 65 20  {F12975} If the 
354a0 75 70 64 61 74 65 20 68 6f 6f 6b 20 63 61 6c 6c  update hook call
354b0 62 61 63 6b 20 46 20 69 6e 20 5b 73 71 6c 69 74  back F in [sqlit
354c0 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 44  e3_update_hook(D
354d0 2c 46 2c 50 29 5d 0a 2a 2a 20 20 20 20 20 20 20  ,F,P)].**       
354e0 20 20 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20     is NULL then 
354f0 74 68 65 20 6e 6f 20 75 70 64 61 74 65 20 63 61  the no update ca
35500 6c 6c 62 61 63 6b 73 20 61 72 65 20 6d 61 64 65  llbacks are made
35510 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 39 37 37 7d  ..**.** {F12977}
35520 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73   Each call to [s
35530 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f  qlite3_update_ho
35540 6f 6b 28 44 2c 46 2c 50 29 5d 20 6f 76 65 72 72  ok(D,F,P)] overr
35550 69 64 65 73 20 70 72 69 6f 72 20 63 61 6c 6c 73  ides prior calls
35560 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f 20  .**          to 
35570 74 68 65 20 73 61 6d 65 20 69 6e 74 65 72 66 61  the same interfa
35580 63 65 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 5b  ce on the same [
35590 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
355a0 69 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 46  ion] D..**.** {F
355b0 31 32 39 37 39 7d 20 54 68 65 20 75 70 64 61 74  12979} The updat
355c0 65 20 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b 20  e hook callback 
355d0 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 77  is not invoked w
355e0 68 65 6e 20 69 6e 74 65 72 6e 61 6c 20 73 79 73  hen internal sys
355f0 74 65 6d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  tem.**          
35600 74 61 62 6c 65 73 20 73 75 63 68 20 61 73 20 73  tables such as s
35610 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 61 6e 64  qlite_master and
35620 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65   sqlite_sequence
35630 20 61 72 65 20 6d 6f 64 69 66 69 65 64 2e 0a 2a   are modified..*
35640 2a 0a 2a 2a 20 7b 46 31 32 39 38 31 7d 20 54 68  *.** {F12981} Th
35650 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
35660 65 72 20 74 6f 20 74 68 65 20 75 70 64 61 74 65  er to the update
35670 20 63 61 6c 6c 62 61 63 6b 20 0a 2a 2a 20 20 20   callback .**   
35680 20 20 20 20 20 20 20 69 73 20 6f 6e 65 20 6f 66         is one of
35690 20 5b 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 5d   [SQLITE_INSERT]
356a0 2c 20 5b 53 51 4c 49 54 45 5f 44 45 4c 45 54 45  , [SQLITE_DELETE
356b0 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 50 44  ] or [SQLITE_UPD
356c0 41 54 45 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20  ATE],.**        
356d0 20 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74    depending on t
356e0 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61  he operation tha
356f0 74 20 63 61 75 73 65 64 20 74 68 65 20 63 61 6c  t caused the cal
35700 6c 62 61 63 6b 20 74 6f 20 62 65 20 69 6e 76 6f  lback to be invo
35710 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 39  ked..**.** {F129
35720 38 33 7d 20 54 68 65 20 74 68 69 72 64 20 61 6e  83} The third an
35730 64 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e  d fourth argumen
35740 74 73 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61  ts to the callba
35750 63 6b 20 63 6f 6e 74 61 69 6e 20 70 6f 69 6e 74  ck contain point
35760 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ers.**          
35770 74 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74  to zero-terminat
35780 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 73  ed UTF-8 strings
35790 20 77 68 69 63 68 20 61 72 65 20 74 68 65 20 6e   which are the n
357a0 61 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 20  ames of the.**  
357b0 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65          database
357c0 20 61 6e 64 20 74 61 62 6c 65 20 74 68 61 74 20   and table that 
357d0 69 73 20 62 65 69 6e 67 20 75 70 64 61 74 65 64  is being updated
357e0 2e 0a 0a 2a 2a 20 7b 46 31 32 39 38 35 7d 20 54  ...** {F12985} T
357f0 68 65 20 66 69 6e 61 6c 20 63 61 6c 6c 62 61 63  he final callbac
35800 6b 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74  k parameter is t
35810 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20  he rowid of the 
35820 72 6f 77 20 61 66 74 65 72 0a 2a 2a 20 20 20 20  row after.**    
35830 20 20 20 20 20 20 74 68 65 20 63 68 61 6e 67 65        the change
35840 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49   occurs..*/.SQLI
35850 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
35860 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b  ite3_update_hook
35870 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20  (.  sqlite3*, . 
35880 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a 2c   void(*)(void *,
35890 69 6e 74 20 2c 63 68 61 72 20 63 6f 6e 73 74 20  int ,char const 
358a0 2a 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c 73  *,char const *,s
358b0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 2c 0a 20  qlite3_int64),. 
358c0 20 76 6f 69 64 2a 0a 29 3b 0a 0a 2f 2a 0a 2a 2a   void*.);../*.**
358d0 20 43 41 50 49 33 52 45 46 3a 20 20 45 6e 61 62   CAPI3REF:  Enab
358e0 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 53 68  le Or Disable Sh
358f0 61 72 65 64 20 50 61 67 65 72 20 43 61 63 68 65  ared Pager Cache
35900 20 7b 46 31 30 33 33 30 7d 0a 2a 2a 0a 2a 2a 20   {F10330}.**.** 
35910 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 61  This routine ena
35920 62 6c 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73  bles or disables
35930 20 74 68 65 20 73 68 61 72 69 6e 67 20 6f 66 20   the sharing of 
35940 74 68 65 20 64 61 74 61 62 61 73 65 20 63 61 63  the database cac
35950 68 65 0a 2a 2a 20 61 6e 64 20 73 63 68 65 6d 61  he.** and schema
35960 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73   data structures
35970 20 62 65 74 77 65 65 6e 20 63 6f 6e 6e 65 63 74   between connect
35980 69 6f 6e 73 20 74 6f 20 74 68 65 20 73 61 6d 65  ions to the same
35990 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 53 68   database..** Sh
359a0 61 72 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64  aring is enabled
359b0 20 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74   if the argument
359c0 20 69 73 20 74 72 75 65 20 61 6e 64 20 64 69 73   is true and dis
359d0 61 62 6c 65 64 20 69 66 20 74 68 65 20 61 72 67  abled if the arg
359e0 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 66 61 6c 73  ument.** is fals
359f0 65 2e 0a 2a 2a 0a 2a 2a 20 43 61 63 68 65 20 73  e..**.** Cache s
35a00 68 61 72 69 6e 67 20 69 73 20 65 6e 61 62 6c 65  haring is enable
35a10 64 20 61 6e 64 20 64 69 73 61 62 6c 65 64 0a 2a  d and disabled.*
35a20 2a 20 66 6f 72 20 61 6e 20 65 6e 74 69 72 65 20  * for an entire 
35a30 70 72 6f 63 65 73 73 2e 20 7b 45 4e 44 7d 20 54  process. {END} T
35a40 68 69 73 20 69 73 20 61 20 63 68 61 6e 67 65 20  his is a change 
35a50 61 73 20 6f 66 20 53 51 4c 69 74 65 20 76 65 72  as of SQLite ver
35a60 73 69 6f 6e 20 33 2e 35 2e 30 2e 0a 2a 2a 20 49  sion 3.5.0..** I
35a70 6e 20 70 72 69 6f 72 20 76 65 72 73 69 6f 6e 73  n prior versions
35a80 20 6f 66 20 53 51 4c 69 74 65 2c 20 73 68 61 72   of SQLite, shar
35a90 69 6e 67 20 77 61 73 0a 2a 2a 20 65 6e 61 62 6c  ing was.** enabl
35aa0 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64 20 66  ed or disabled f
35ab0 6f 72 20 65 61 63 68 20 74 68 72 65 61 64 20 73  or each thread s
35ac0 65 70 61 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a  eparately..**.**
35ad0 20 54 68 65 20 63 61 63 68 65 20 73 68 61 72 69   The cache shari
35ae0 6e 67 20 6d 6f 64 65 20 73 65 74 20 62 79 20 74  ng mode set by t
35af0 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 65 66  his interface ef
35b00 66 65 63 74 73 20 61 6c 6c 20 73 75 62 73 65 71  fects all subseq
35b10 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f  uent.** calls to
35b20 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29   [sqlite3_open()
35b30 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  ], [sqlite3_open
35b40 5f 76 32 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c  _v2()], and [sql
35b50 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a  ite3_open16()]..
35b60 2a 2a 20 45 78 69 73 74 69 6e 67 20 64 61 74 61  ** Existing data
35b70 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
35b80 20 63 6f 6e 74 69 6e 75 65 20 75 73 65 20 74 68   continue use th
35b90 65 20 73 68 61 72 69 6e 67 20 6d 6f 64 65 0a 2a  e sharing mode.*
35ba0 2a 20 74 68 61 74 20 77 61 73 20 69 6e 20 65 66  * that was in ef
35bb0 66 65 63 74 20 61 74 20 74 68 65 20 74 69 6d 65  fect at the time
35bc0 20 74 68 65 79 20 77 65 72 65 20 6f 70 65 6e 65   they were opene
35bd0 64 2e 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c  d..**.** Virtual
35be0 20 74 61 62 6c 65 73 20 63 61 6e 6e 6f 74 20 62   tables cannot b
35bf0 65 20 75 73 65 64 20 77 69 74 68 20 61 20 73 68  e used with a sh
35c00 61 72 65 64 20 63 61 63 68 65 2e 20 20 20 57 68  ared cache.   Wh
35c10 65 6e 20 73 68 61 72 65 64 0a 2a 2a 20 63 61 63  en shared.** cac
35c20 68 65 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74  he is enabled, t
35c30 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61  he [sqlite3_crea
35c40 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 41 50 49  te_module()] API
35c50 20 75 73 65 64 20 74 6f 20 72 65 67 69 73 74 65   used to registe
35c60 72 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62  r.** virtual tab
35c70 6c 65 73 20 77 69 6c 6c 20 61 6c 77 61 79 73 20  les will always 
35c80 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e  return an error.
35c90 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
35ca0 69 6e 65 20 72 65 74 75 72 6e 73 20 5b 53 51 4c  ine returns [SQL
35cb0 49 54 45 5f 4f 4b 5d 20 69 66 20 73 68 61 72 65  ITE_OK] if share
35cc0 64 20 63 61 63 68 65 20 77 61 73 0a 2a 2a 20 65  d cache was.** e
35cd0 6e 61 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c  nabled or disabl
35ce0 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e  ed successfully.
35cf0 20 20 41 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65    An [error code
35d00 5d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64  ].** is returned
35d10 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a   otherwise..**.*
35d20 2a 20 53 68 61 72 65 64 20 63 61 63 68 65 20 69  * Shared cache i
35d30 73 20 64 69 73 61 62 6c 65 64 20 62 79 20 64 65  s disabled by de
35d40 66 61 75 6c 74 2e 20 42 75 74 20 74 68 69 73 20  fault. But this 
35d50 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69 6e 0a  might change in.
35d60 2a 2a 20 66 75 74 75 72 65 20 72 65 6c 65 61 73  ** future releas
35d70 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 41  es of SQLite.  A
35d80 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74  pplications that
35d90 20 63 61 72 65 20 61 62 6f 75 74 20 73 68 61 72   care about shar
35da0 65 64 0a 2a 2a 20 63 61 63 68 65 20 73 65 74 74  ed.** cache sett
35db0 69 6e 67 20 73 68 6f 75 6c 64 20 73 65 74 20 69  ing should set i
35dc0 74 20 65 78 70 6c 69 63 69 74 6c 79 2e 0a 2a 2a  t explicitly..**
35dd0 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a  .** INVARIANTS:.
35de0 2a 2a 20 0a 2a 2a 20 7b 46 31 30 33 33 31 7d 20  ** .** {F10331} 
35df0 41 20 73 75 63 63 65 73 73 66 75 6c 20 69 6e 76  A successful inv
35e00 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69  ocation of [sqli
35e10 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65  te3_enable_share
35e20 64 5f 63 61 63 68 65 28 42 29 5d 0a 2a 2a 20 20  d_cache(B)].**  
35e30 20 20 20 20 20 20 20 20 77 69 6c 6c 20 65 6e 61          will ena
35e40 62 6c 65 20 6f 72 20 64 69 73 61 62 6c 65 20 73  ble or disable s
35e50 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65  hared cache mode
35e60 20 66 6f 72 20 61 6e 79 20 73 75 62 73 65 71 75   for any subsequ
35e70 65 6e 74 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20  ently.**        
35e80 20 20 63 72 65 61 74 65 64 20 5b 64 61 74 61 62    created [datab
35e90 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
35ea0 69 6e 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63  in the same proc
35eb0 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 30 33  ess..**.** {F103
35ec0 33 36 7d 20 57 68 65 6e 20 73 68 61 72 65 64 20  36} When shared 
35ed0 63 61 63 68 65 20 69 73 20 65 6e 61 62 6c 65 64  cache is enabled
35ee0 2c 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63  , the [sqlite3_c
35ef0 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 0a  reate_module()].
35f00 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e 74 65  **          inte
35f10 72 66 61 63 65 20 77 69 6c 6c 20 61 6c 77 61 79  rface will alway
35f20 73 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  s return an erro
35f30 72 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 30 33 33 37  r..**.** {F10337
35f40 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 65  } The [sqlite3_e
35f50 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63  nable_shared_cac
35f60 68 65 28 42 29 5d 20 69 6e 74 65 72 66 61 63 65  he(B)] interface
35f70 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20   returns.**     
35f80 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d       [SQLITE_OK]
35f90 20 69 66 20 73 68 61 72 65 64 20 63 61 63 68 65   if shared cache
35fa0 20 77 61 73 20 65 6e 61 62 6c 65 64 20 6f 72 20   was enabled or 
35fb0 64 69 73 61 62 6c 65 64 20 73 75 63 63 65 73 73  disabled success
35fc0 66 75 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31  fully..**.** {F1
35fd0 30 33 33 39 7d 20 53 68 61 72 65 64 20 63 61 63  0339} Shared cac
35fe0 68 65 20 69 73 20 64 69 73 61 62 6c 65 64 20 62  he is disabled b
35ff0 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2f 0a 53 51  y default..*/.SQ
36000 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
36010 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72  ite3_enable_shar
36020 65 64 5f 63 61 63 68 65 28 69 6e 74 29 3b 0a 0a  ed_cache(int);..
36030 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
36040 20 41 74 74 65 6d 70 74 20 54 6f 20 46 72 65 65   Attempt To Free
36050 20 48 65 61 70 20 4d 65 6d 6f 72 79 20 7b 46 31   Heap Memory {F1
36060 37 33 34 30 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20  7340}.**.** The 
36070 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
36080 6d 65 6d 6f 72 79 28 29 20 69 6e 74 65 72 66 61  memory() interfa
36090 63 65 20 61 74 74 65 6d 70 74 73 20 74 6f 0a 2a  ce attempts to.*
360a0 2a 20 66 72 65 65 20 4e 20 62 79 74 65 73 20 6f  * free N bytes o
360b0 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 62 79  f heap memory by
360c0 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 6e 6f   deallocating no
360d0 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d 6f  n-essential memo
360e0 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e  ry.** allocation
360f0 73 20 68 65 6c 64 20 62 79 20 74 68 65 20 64 61  s held by the da
36100 74 61 62 61 73 65 20 6c 61 62 72 61 72 79 2e 20  tabase labrary. 
36110 7b 45 4e 44 7d 20 20 4d 65 6d 6f 72 79 20 75 73  {END}  Memory us
36120 65 64 0a 2a 2a 20 74 6f 20 63 61 63 68 65 20 64  ed.** to cache d
36130 61 74 61 62 61 73 65 20 70 61 67 65 73 20 74 6f  atabase pages to
36140 20 69 6d 70 72 6f 76 65 20 70 65 72 66 6f 72 6d   improve perform
36150 61 6e 63 65 20 69 73 20 61 6e 20 65 78 61 6d 70  ance is an examp
36160 6c 65 20 6f 66 0a 2a 2a 20 6e 6f 6e 2d 65 73 73  le of.** non-ess
36170 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 2e 20 20  ential memory.  
36180 53 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  Sqlite3_release_
36190 6d 65 6d 6f 72 79 28 29 20 72 65 74 75 72 6e 73  memory() returns
361a0 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  .** the number o
361b0 66 20 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79  f bytes actually
361c0 20 66 72 65 65 64 2c 20 77 68 69 63 68 20 6d 69   freed, which mi
361d0 67 68 74 20 62 65 20 6d 6f 72 65 20 6f 72 20 6c  ght be more or l
361e0 65 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20  ess.** than the 
361f0 61 6d 6f 75 6e 74 20 72 65 71 75 65 73 74 65 64  amount requested
36200 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e  ..**.** INVARIAN
36210 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31 37 33 34  TS:.**.** {F1734
36220 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  1} The [sqlite3_
36230 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 4e  release_memory(N
36240 29 5d 20 69 6e 74 65 72 66 61 63 65 20 61 74 74  )] interface att
36250 65 6d 70 74 73 20 74 6f 0a 2a 2a 20 20 20 20 20  empts to.**     
36260 20 20 20 20 20 66 72 65 65 20 4e 20 62 79 74 65       free N byte
36270 73 20 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72 79  s of heap memory
36280 20 62 79 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67   by deallocating
36290 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 0a 2a   non-essential.*
362a0 2a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 6f 72  *          memor
362b0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 68 65  y allocations he
362c0 6c 64 20 62 79 20 74 68 65 20 64 61 74 61 62 61  ld by the databa
362d0 73 65 20 6c 61 62 72 61 72 79 2e 0a 2a 2a 0a 2a  se labrary..**.*
362e0 2a 20 7b 46 31 36 33 34 32 7d 20 54 68 65 20 5b  * {F16342} The [
362f0 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
36300 6d 65 6d 6f 72 79 28 4e 29 5d 20 72 65 74 75 72  memory(N)] retur
36310 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a  ns the number.**
36320 20 20 20 20 20 20 20 20 20 20 6f 66 20 62 79 74            of byt
36330 65 73 20 61 63 74 75 61 6c 6c 79 20 66 72 65 65  es actually free
36340 64 2c 20 77 68 69 63 68 20 6d 69 67 68 74 20 62  d, which might b
36350 65 20 6d 6f 72 65 20 6f 72 20 6c 65 73 73 0a 2a  e more or less.*
36360 2a 20 20 20 20 20 20 20 20 20 20 74 68 61 6e 20  *          than 
36370 74 68 65 20 61 6d 6f 75 6e 74 20 72 65 71 75 65  the amount reque
36380 73 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  sted..*/.SQLITE_
36390 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
363a0 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 69  release_memory(i
363b0 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  nt);../*.** CAPI
363c0 33 52 45 46 3a 20 20 49 6d 70 6f 73 65 20 41 20  3REF:  Impose A 
363d0 4c 69 6d 69 74 20 4f 6e 20 48 65 61 70 20 53 69  Limit On Heap Si
363e0 7a 65 20 7b 46 31 37 33 35 30 7d 0a 2a 2a 0a 2a  ze {F17350}.**.*
363f0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6f  * The sqlite3_so
36400 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29 20  ft_heap_limit() 
36410 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 70 6c 61  interface.** pla
36420 63 65 73 20 61 20 22 73 6f 66 74 22 20 6c 69 6d  ces a "soft" lim
36430 69 74 20 6f 6e 20 74 68 65 20 61 6d 6f 75 6e 74  it on the amount
36440 20 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 20   of heap memory 
36450 74 68 61 74 20 6d 61 79 20 62 65 20 61 6c 6c 6f  that may be allo
36460 63 61 74 65 64 0a 2a 2a 20 62 79 20 53 51 4c 69  cated.** by SQLi
36470 74 65 2e 20 49 66 20 61 6e 20 69 6e 74 65 72 6e  te. If an intern
36480 61 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73  al allocation is
36490 20 72 65 71 75 65 73 74 65 64 20 0a 2a 2a 20 74   requested .** t
364a0 68 61 74 20 77 6f 75 6c 64 20 65 78 63 65 65 64  hat would exceed
364b0 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c   the soft heap l
364c0 69 6d 69 74 2c 20 5b 73 71 6c 69 74 65 33 5f 72  imit, [sqlite3_r
364d0 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d  elease_memory()]
364e0 20 69 73 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 6f   is.** invoked o
364f0 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69 6d 65 73  ne or more times
36500 20 74 6f 20 66 72 65 65 20 75 70 20 73 6f 6d 65   to free up some
36510 20 73 70 61 63 65 20 62 65 66 6f 72 65 20 74 68   space before th
36520 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20  e allocation.** 
36530 69 73 20 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a 20 54  is made..**.** T
36540 68 65 20 6c 69 6d 69 74 20 69 73 20 63 61 6c 6c  he limit is call
36550 65 64 20 22 73 6f 66 74 22 2c 20 62 65 63 61 75  ed "soft", becau
36560 73 65 20 69 66 0a 2a 2a 20 5b 73 71 6c 69 74 65  se if.** [sqlite
36570 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
36580 28 29 5d 20 63 61 6e 6e 6f 74 0a 2a 2a 20 66 72  ()] cannot.** fr
36590 65 65 20 73 75 66 66 69 63 69 65 6e 74 20 6d 65  ee sufficient me
365a0 6d 6f 72 79 20 74 6f 20 70 72 65 76 65 6e 74 20  mory to prevent 
365b0 74 68 65 20 6c 69 6d 69 74 20 66 72 6f 6d 20 62  the limit from b
365c0 65 69 6e 67 20 65 78 63 65 65 64 65 64 2c 0a 2a  eing exceeded,.*
365d0 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 73 20  * the memory is 
365e0 61 6c 6c 6f 63 61 74 65 64 20 61 6e 79 77 61 79  allocated anyway
365f0 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74   and the current
36600 20 6f 70 65 72 61 74 69 6f 6e 20 70 72 6f 63 65   operation proce
36610 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 67  eds..**.** A neg
36620 61 74 69 76 65 20 6f 72 20 7a 65 72 6f 20 76 61  ative or zero va
36630 6c 75 65 20 66 6f 72 20 4e 20 6d 65 61 6e 73 20  lue for N means 
36640 74 68 61 74 20 74 68 65 72 65 20 69 73 20 6e 6f  that there is no
36650 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74   soft heap limit
36660 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   and.** [sqlite3
36670 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28  _release_memory(
36680 29 5d 20 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 20  )] will only be 
36690 63 61 6c 6c 65 64 20 77 68 65 6e 20 6d 65 6d 6f  called when memo
366a0 72 79 20 69 73 20 65 78 68 61 75 73 74 65 64 2e  ry is exhausted.
366b0 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20  .** The default 
366c0 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 73 6f  value for the so
366d0 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 69 73  ft heap limit is
366e0 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c   zero..**.** SQL
366f0 69 74 65 20 6d 61 6b 65 73 20 61 20 62 65 73 74  ite makes a best
36700 20 65 66 66 6f 72 74 20 74 6f 20 68 6f 6e 6f 72   effort to honor
36710 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c   the soft heap l
36720 69 6d 69 74 2e 20 20 0a 2a 2a 20 42 75 74 20 69  imit.  .** But i
36730 66 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20  f the soft heap 
36740 6c 69 6d 69 74 20 63 61 6e 6e 6f 74 20 68 6f 6e  limit cannot hon
36750 6f 72 65 64 2c 20 65 78 65 63 75 74 69 6f 6e 20  ored, execution 
36760 77 69 6c 6c 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65  will.** continue
36770 20 77 69 74 68 6f 75 74 20 65 72 72 6f 72 20 6f   without error o
36780 72 20 6e 6f 74 69 66 69 63 61 74 69 6f 6e 2e 20  r notification. 
36790 20 54 68 69 73 20 69 73 20 77 68 79 20 74 68 65   This is why the
367a0 20 6c 69 6d 69 74 20 69 73 20 0a 2a 2a 20 63 61   limit is .** ca
367b0 6c 6c 65 64 20 61 20 22 73 6f 66 74 22 20 6c 69  lled a "soft" li
367c0 6d 69 74 2e 20 20 49 74 20 69 73 20 61 64 76 69  mit.  It is advi
367d0 73 6f 72 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a  sory only..**.**
367e0 20 50 72 69 6f 72 20 74 6f 20 53 51 4c 69 74 65   Prior to SQLite
367f0 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 2c 20   version 3.5.0, 
36800 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c  this routine onl
36810 79 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 74 68  y constrained th
36820 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f  e memory.** allo
36830 63 61 74 65 64 20 62 79 20 61 20 73 69 6e 67 6c  cated by a singl
36840 65 20 74 68 72 65 61 64 20 2d 20 74 68 65 20 73  e thread - the s
36850 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20 77 68  ame thread in wh
36860 69 63 68 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ich this routine
36870 0a 2a 2a 20 72 75 6e 73 2e 20 20 42 65 67 69 6e  .** runs.  Begin
36880 6e 69 6e 67 20 77 69 74 68 20 53 51 4c 69 74 65  ning with SQLite
36890 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 2c 20   version 3.5.0, 
368a0 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69  the soft heap li
368b0 6d 69 74 20 69 73 0a 2a 2a 20 61 70 70 6c 69 65  mit is.** applie
368c0 64 20 74 6f 20 61 6c 6c 20 74 68 72 65 61 64 73  d to all threads
368d0 2e 20 54 68 65 20 76 61 6c 75 65 20 73 70 65 63  . The value spec
368e0 69 66 69 65 64 20 66 6f 72 20 74 68 65 20 73 6f  ified for the so
368f0 66 74 20 68 65 61 70 20 6c 69 6d 69 74 0a 2a 2a  ft heap limit.**
36900 20 69 73 20 61 6e 20 75 70 70 65 72 20 62 6f 75   is an upper bou
36910 6e 64 20 6f 6e 20 74 68 65 20 74 6f 74 61 6c 20  nd on the total 
36920 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
36930 6e 20 66 6f 72 20 61 6c 6c 20 74 68 72 65 61 64  n for all thread
36940 73 2e 20 49 6e 0a 2a 2a 20 76 65 72 73 69 6f 6e  s. In.** version
36950 20 33 2e 35 2e 30 20 74 68 65 72 65 20 69 73 20   3.5.0 there is 
36960 6e 6f 20 6d 65 63 68 61 6e 69 73 6d 20 66 6f 72  no mechanism for
36970 20 6c 69 6d 69 74 69 6e 67 20 74 68 65 20 68 65   limiting the he
36980 61 70 20 75 73 61 67 65 20 66 6f 72 0a 2a 2a 20  ap usage for.** 
36990 69 6e 64 69 76 69 64 75 61 6c 20 74 68 72 65 61  individual threa
369a0 64 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49  ds..**.** INVARI
369b0 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31 36  ANTS:.**.** {F16
369c0 33 35 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  351} The [sqlite
369d0 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69  3_soft_heap_limi
369e0 74 28 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20  t(N)] interface 
369f0 70 6c 61 63 65 73 20 61 20 73 6f 66 74 20 6c 69  places a soft li
36a00 6d 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  mit.**          
36a10 6f 66 20 4e 20 62 79 74 65 73 20 6f 6e 20 74 68  of N bytes on th
36a20 65 20 61 6d 6f 75 6e 74 20 6f 66 20 68 65 61 70  e amount of heap
36a30 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 6d 61 79   memory that may
36a40 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a   be allocated.**
36a50 20 20 20 20 20 20 20 20 20 20 75 73 69 6e 67 20            using 
36a60 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
36a70 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72  )] or [sqlite3_r
36a80 65 61 6c 6c 6f 63 28 29 5d 20 61 74 20 61 6e 79  ealloc()] at any
36a90 20 70 6f 69 6e 74 0a 2a 2a 20 20 20 20 20 20 20   point.**       
36aa0 20 20 20 69 6e 20 74 69 6d 65 2e 0a 2a 2a 0a 2a     in time..**.*
36ab0 2a 20 7b 46 31 36 33 35 32 7d 20 49 66 20 61 20  * {F16352} If a 
36ac0 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
36ad0 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73  _malloc()] or [s
36ae0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29  qlite3_realloc()
36af0 5d 20 77 6f 75 6c 64 0a 2a 2a 20 20 20 20 20 20  ] would.**      
36b00 20 20 20 20 63 61 75 73 65 20 74 68 65 20 74 6f      cause the to
36b10 74 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 61 6c  tal amount of al
36b20 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 20 74  located memory t
36b30 6f 20 65 78 63 65 65 64 20 74 68 65 0a 2a 2a 20  o exceed the.** 
36b40 20 20 20 20 20 20 20 20 20 73 6f 66 74 20 68 65           soft he
36b50 61 70 20 6c 69 6d 69 74 2c 20 74 68 65 6e 20 5b  ap limit, then [
36b60 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
36b70 6d 65 6d 6f 72 79 28 29 5d 20 69 73 20 69 6e 76  memory()] is inv
36b80 6f 6b 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20  oked.**         
36b90 20 69 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 74   in an attempt t
36ba0 6f 20 72 65 64 75 63 65 20 74 68 65 20 6d 65 6d  o reduce the mem
36bb0 6f 72 79 20 75 73 61 67 65 20 70 72 69 6f 72 20  ory usage prior 
36bc0 74 6f 20 70 72 6f 63 65 65 64 69 6e 67 0a 2a 2a  to proceeding.**
36bd0 20 20 20 20 20 20 20 20 20 20 77 69 74 68 20 74            with t
36be0 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
36bf0 74 69 6f 6e 20 61 74 74 65 6d 70 74 2e 0a 2a 2a  tion attempt..**
36c00 0a 2a 2a 20 7b 46 31 36 33 35 33 7d 20 43 61 6c  .** {F16353} Cal
36c10 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d  ls to [sqlite3_m
36c20 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 71 6c  alloc()] or [sql
36c30 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 20  ite3_realloc()] 
36c40 74 68 61 74 20 74 72 69 67 67 65 72 0a 2a 2a 20  that trigger.** 
36c50 20 20 20 20 20 20 20 20 20 61 74 74 65 6d 70 74           attempt
36c60 73 20 74 6f 20 72 65 64 75 63 65 20 6d 65 6d 6f  s to reduce memo
36c70 72 79 20 75 73 61 67 65 20 74 68 72 6f 75 67 68  ry usage through
36c80 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c   the soft heap l
36c90 69 6d 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  imit.**         
36ca0 20 6d 65 63 68 61 6e 69 73 6d 20 63 6f 6e 74 69   mechanism conti
36cb0 6e 75 65 20 65 76 65 6e 20 69 66 20 74 68 65 20  nue even if the 
36cc0 61 74 74 65 6d 70 74 20 74 6f 20 72 65 64 75 63  attempt to reduc
36cd0 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 20  e memory.**     
36ce0 20 20 20 20 20 75 73 61 67 65 20 69 73 20 75 6e       usage is un
36cf0 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2a 0a 2a  successful..**.*
36d00 2a 20 7b 46 31 36 33 35 34 7d 20 41 20 6e 65 67  * {F16354} A neg
36d10 61 74 69 76 65 20 6f 72 20 7a 65 72 6f 20 76 61  ative or zero va
36d20 6c 75 65 20 66 6f 72 20 4e 20 69 6e 20 61 20 63  lue for N in a c
36d30 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20  all to.**       
36d40 20 20 20 5b 73 71 6c 69 74 65 33 5f 73 6f 66 74     [sqlite3_soft
36d50 5f 68 65 61 70 5f 6c 69 6d 69 74 28 4e 29 5d 20  _heap_limit(N)] 
36d60 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 72 65  means that there
36d70 20 69 73 20 6e 6f 20 73 6f 66 74 0a 2a 2a 20 20   is no soft.**  
36d80 20 20 20 20 20 20 20 20 68 65 61 70 20 6c 69 6d          heap lim
36d90 69 74 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  it and [sqlite3_
36da0 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29  release_memory()
36db0 5d 20 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 0a 2a  ] will only be.*
36dc0 2a 20 20 20 20 20 20 20 20 20 20 63 61 6c 6c 65  *          calle
36dd0 64 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20 69 73  d when memory is
36de0 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 78 68 61   completely exha
36df0 75 73 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31  usted..**.** {F1
36e00 36 33 35 35 7d 20 54 68 65 20 64 65 66 61 75 6c  6355} The defaul
36e10 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  t value for the 
36e20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20  soft heap limit 
36e30 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 7b  is zero..**.** {
36e40 46 31 36 33 35 38 7d 20 45 61 63 68 20 63 61 6c  F16358} Each cal
36e50 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 6f  l to [sqlite3_so
36e60 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 4e 29  ft_heap_limit(N)
36e70 5d 20 6f 76 65 72 72 69 64 65 73 20 74 68 65 0a  ] overrides the.
36e80 2a 2a 20 20 20 20 20 20 20 20 20 20 76 61 6c 75  **          valu
36e90 65 73 20 73 65 74 20 62 79 20 61 6c 6c 20 70 72  es set by all pr
36ea0 69 6f 72 20 63 61 6c 6c 73 2e 0a 2a 2f 0a 53 51  ior calls..*/.SQ
36eb0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
36ec0 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f  lite3_soft_heap_
36ed0 6c 69 6d 69 74 28 69 6e 74 29 3b 0a 0a 2f 2a 0a  limit(int);../*.
36ee0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 20 45 78  ** CAPI3REF:  Ex
36ef0 74 72 61 63 74 20 4d 65 74 61 64 61 74 61 20 41  tract Metadata A
36f00 62 6f 75 74 20 41 20 43 6f 6c 75 6d 6e 20 4f 66  bout A Column Of
36f10 20 41 20 54 61 62 6c 65 20 7b 46 31 32 38 35 30   A Table {F12850
36f20 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  }.**.** This rou
36f30 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73 20  tine.** returns 
36f40 6d 65 74 61 2d 64 61 74 61 20 61 62 6f 75 74 20  meta-data about 
36f50 61 20 73 70 65 63 69 66 69 63 20 63 6f 6c 75 6d  a specific colum
36f60 6e 20 6f 66 20 61 20 73 70 65 63 69 66 69 63 20  n of a specific 
36f70 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c  database.** tabl
36f80 65 20 61 63 63 65 73 73 69 62 6c 65 20 75 73 69  e accessible usi
36f90 6e 67 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f  ng the connectio
36fa0 6e 20 68 61 6e 64 6c 65 20 70 61 73 73 65 64 20  n handle passed 
36fb0 61 73 20 74 68 65 20 66 69 72 73 74 20 66 75 6e  as the first fun
36fc0 63 74 69 6f 6e 20 0a 2a 2a 20 61 72 67 75 6d 65  ction .** argume
36fd0 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  nt..**.** The co
36fe0 6c 75 6d 6e 20 69 73 20 69 64 65 6e 74 69 66 69  lumn is identifi
36ff0 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64  ed by the second
37000 2c 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72  , third and four
37010 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f  th parameters to
37020 20 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69   .** this functi
37030 6f 6e 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70  on. The second p
37040 61 72 61 6d 65 74 65 72 20 69 73 20 65 69 74 68  arameter is eith
37050 65 72 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  er the name of t
37060 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 28  he database.** (
37070 69 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65  i.e. "main", "te
37080 6d 70 22 20 6f 72 20 61 6e 20 61 74 74 61 63 68  mp" or an attach
37090 65 64 20 64 61 74 61 62 61 73 65 29 20 63 6f 6e  ed database) con
370a0 74 61 69 6e 69 6e 67 20 74 68 65 20 73 70 65 63  taining the spec
370b0 69 66 69 65 64 0a 2a 2a 20 74 61 62 6c 65 20 6f  ified.** table o
370c0 72 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 73  r NULL. If it is
370d0 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c 20   NULL, then all 
370e0 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
370f0 65 73 20 61 72 65 20 73 65 61 72 63 68 65 64 0a  es are searched.
37100 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  ** for the table
37110 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20   using the same 
37120 61 6c 67 6f 72 69 74 68 6d 20 61 73 20 74 68 65  algorithm as the
37130 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65   database engine
37140 20 75 73 65 73 20 74 6f 20 0a 2a 2a 20 72 65 73   uses to .** res
37150 6f 6c 76 65 20 75 6e 71 75 61 6c 69 66 69 65 64  olve unqualified
37160 20 74 61 62 6c 65 20 72 65 66 65 72 65 6e 63 65   table reference
37170 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69  s..**.** The thi
37180 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61  rd and fourth pa
37190 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73  rameters to this
371a0 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68   function are th
371b0 65 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75  e table and colu
371c0 6d 6e 20 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74  mn .** name of t
371d0 68 65 20 64 65 73 69 72 65 64 20 63 6f 6c 75 6d  he desired colum
371e0 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e  n, respectively.
371f0 20 4e 65 69 74 68 65 72 20 6f 66 20 74 68 65 73   Neither of thes
37200 65 20 70 61 72 61 6d 65 74 65 72 73 20 0a 2a 2a  e parameters .**
37210 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a   may be NULL..**
37220 0a 2a 2a 20 4d 65 74 61 20 69 6e 66 6f 72 6d 61  .** Meta informa
37230 74 69 6f 6e 20 69 73 20 72 65 74 75 72 6e 65 64  tion is returned
37240 20 62 79 20 77 72 69 74 69 6e 67 20 74 6f 20 74   by writing to t
37250 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69  he memory locati
37260 6f 6e 73 20 70 61 73 73 65 64 20 61 73 0a 2a 2a  ons passed as.**
37270 20 74 68 65 20 35 74 68 20 61 6e 64 20 73 75 62   the 5th and sub
37280 73 65 71 75 65 6e 74 20 70 61 72 61 6d 65 74 65  sequent paramete
37290 72 73 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  rs to this funct
372a0 69 6f 6e 2e 20 41 6e 79 20 6f 66 20 74 68 65 73  ion. Any of thes
372b0 65 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20  e .** arguments 
372c0 6d 61 79 20 62 65 20 4e 55 4c 4c 2c 20 69 6e 20  may be NULL, in 
372d0 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20 63  which case the c
372e0 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 65 6c 65  orresponding ele
372f0 6d 65 6e 74 20 6f 66 20 6d 65 74 61 20 0a 2a 2a  ment of meta .**
37300 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20   information is 
37310 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  ommitted..**.** 
37320 3c 70 72 65 3e 0a 2a 2a 20 50 61 72 61 6d 65 74  <pre>.** Paramet
37330 65 72 20 20 20 20 20 4f 75 74 70 75 74 20 54 79  er     Output Ty
37340 70 65 20 20 20 20 20 20 44 65 73 63 72 69 70 74  pe      Descript
37350 69 6f 6e 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  ion.** ---------
37360 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
37370 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a  ----------.**.**
37380 20 20 20 35 74 68 20 20 20 20 20 20 20 20 20 63     5th         c
37390 6f 6e 73 74 20 63 68 61 72 2a 20 20 20 20 20 20  onst char*      
373a0 44 61 74 61 20 74 79 70 65 0a 2a 2a 20 20 20 36  Data type.**   6
373b0 74 68 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74  th         const
373c0 20 63 68 61 72 2a 20 20 20 20 20 20 4e 61 6d 65   char*      Name
373d0 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   of the default 
373e0 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
373f0 63 65 20 0a 2a 2a 20 20 20 37 74 68 20 20 20 20  ce .**   7th    
37400 20 20 20 20 20 69 6e 74 20 20 20 20 20 20 20 20       int        
37410 20 20 20 20 20 20 54 72 75 65 20 69 66 20 74 68        True if th
37420 65 20 63 6f 6c 75 6d 6e 20 68 61 73 20 61 20 4e  e column has a N
37430 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69  OT NULL constrai
37440 6e 74 0a 2a 2a 20 20 20 38 74 68 20 20 20 20 20  nt.**   8th     
37450 20 20 20 20 69 6e 74 20 20 20 20 20 20 20 20 20      int         
37460 20 20 20 20 20 54 72 75 65 20 69 66 20 74 68 65       True if the
37470 20 63 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 20   column is part 
37480 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b  of the PRIMARY K
37490 45 59 0a 2a 2a 20 20 20 39 74 68 20 20 20 20 20  EY.**   9th     
374a0 20 20 20 20 69 6e 74 20 20 20 20 20 20 20 20 20      int         
374b0 20 20 20 20 20 54 72 75 65 20 69 66 20 74 68 65       True if the
374c0 20 63 6f 6c 75 6d 6e 20 69 73 20 41 55 54 4f 49   column is AUTOI
374d0 4e 43 52 45 4d 45 4e 54 0a 2a 2a 20 3c 2f 70 72  NCREMENT.** </pr
374e0 65 3e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20  e>.**.**.** The 
374f0 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 74  memory pointed t
37500 6f 20 62 79 20 74 68 65 20 63 68 61 72 61 63 74  o by the charact
37510 65 72 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75  er pointers retu
37520 72 6e 65 64 20 66 6f 72 20 74 68 65 20 0a 2a 2a  rned for the .**
37530 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70   declaration typ
37540 65 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20  e and collation 
37550 73 65 71 75 65 6e 63 65 20 69 73 20 76 61 6c 69  sequence is vali
37560 64 20 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68 65  d only until the
37570 20 6e 65 78 74 20 0a 2a 2a 20 63 61 6c 6c 20 74   next .** call t
37580 6f 20 61 6e 79 20 73 71 6c 69 74 65 20 41 50 49  o any sqlite API
37590 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
375a0 20 49 66 20 74 68 65 20 73 70 65 63 69 66 69 65   If the specifie
375b0 64 20 74 61 62 6c 65 20 69 73 20 61 63 74 75 61  d table is actua
375c0 6c 6c 79 20 61 20 76 69 65 77 2c 20 74 68 65 6e  lly a view, then
375d0 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74   an error is ret
375e0 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  urned..**.** If 
375f0 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63 6f  the specified co
37600 6c 75 6d 6e 20 69 73 20 22 72 6f 77 69 64 22 2c  lumn is "rowid",
37610 20 22 6f 69 64 22 20 6f 72 20 22 5f 72 6f 77 69   "oid" or "_rowi
37620 64 5f 22 20 61 6e 64 20 61 6e 20 0a 2a 2a 20 49  d_" and an .** I
37630 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
37640 45 59 20 63 6f 6c 75 6d 6e 20 68 61 73 20 62 65  EY column has be
37650 65 6e 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65  en explicitly de
37660 63 6c 61 72 65 64 2c 20 74 68 65 6e 20 74 68 65  clared, then the
37670 20 6f 75 74 70 75 74 20 0a 2a 2a 20 70 61 72 61   output .** para
37680 6d 65 74 65 72 73 20 61 72 65 20 73 65 74 20 66  meters are set f
37690 6f 72 20 74 68 65 20 65 78 70 6c 69 63 69 74 6c  or the explicitl
376a0 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d  y declared colum
376b0 6e 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  n. If there is n
376c0 6f 0a 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79 20  o.** explicitly 
376d0 64 65 63 6c 61 72 65 64 20 49 50 4b 20 63 6f 6c  declared IPK col
376e0 75 6d 6e 2c 20 74 68 65 6e 20 74 68 65 20 6f 75  umn, then the ou
376f0 74 70 75 74 20 70 61 72 61 6d 65 74 65 72 73 20  tput parameters 
37700 61 72 65 20 73 65 74 20 61 73 20 0a 2a 2a 20 66  are set as .** f
37710 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 70  ollows:.**.** <p
37720 72 65 3e 0a 2a 2a 20 20 20 20 20 64 61 74 61 20  re>.**     data 
37730 74 79 70 65 3a 20 22 49 4e 54 45 47 45 52 22 0a  type: "INTEGER".
37740 2a 2a 20 20 20 20 20 63 6f 6c 6c 61 74 69 6f 6e  **     collation
37750 20 73 65 71 75 65 6e 63 65 3a 20 22 42 49 4e 41   sequence: "BINA
37760 52 59 22 0a 2a 2a 20 20 20 20 20 6e 6f 74 20 6e  RY".**     not n
37770 75 6c 6c 3a 20 30 0a 2a 2a 20 20 20 20 20 70 72  ull: 0.**     pr
37780 69 6d 61 72 79 20 6b 65 79 3a 20 31 0a 2a 2a 20  imary key: 1.** 
37790 20 20 20 20 61 75 74 6f 20 69 6e 63 72 65 6d 65      auto increme
377a0 6e 74 3a 20 30 0a 2a 2a 20 3c 2f 70 72 65 3e 0a  nt: 0.** </pre>.
377b0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
377c0 69 6f 6e 20 6d 61 79 20 6c 6f 61 64 20 6f 6e 65  ion may load one
377d0 20 6f 72 20 6d 6f 72 65 20 73 63 68 65 6d 61 73   or more schemas
377e0 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 66   from database f
377f0 69 6c 65 73 2e 20 49 66 20 61 6e 0a 2a 2a 20 65  iles. If an.** e
37800 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69  rror occurs duri
37810 6e 67 20 74 68 69 73 20 70 72 6f 63 65 73 73 2c  ng this process,
37820 20 6f 72 20 69 66 20 74 68 65 20 72 65 71 75 65   or if the reque
37830 73 74 65 64 20 74 61 62 6c 65 20 6f 72 20 63 6f  sted table or co
37840 6c 75 6d 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62  lumn.** cannot b
37850 65 20 66 6f 75 6e 64 2c 20 61 6e 20 53 51 4c 49  e found, an SQLI
37860 54 45 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  TE error code is
37870 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e   returned and an
37880 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a   error message.*
37890 2a 20 6c 65 66 74 20 69 6e 20 74 68 65 20 64 61  * left in the da
378a0 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 28 74  tabase handle (t
378b0 6f 20 62 65 20 72 65 74 72 69 65 76 65 64 20 75  o be retrieved u
378c0 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 65 72 72  sing sqlite3_err
378d0 6d 73 67 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  msg())..**.** Th
378e0 69 73 20 41 50 49 20 69 73 20 6f 6e 6c 79 20 61  is API is only a
378f0 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 20  vailable if the 
37900 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70  library was comp
37910 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a  iled with the.**
37920 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43   SQLITE_ENABLE_C
37930 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 20 70  OLUMN_METADATA p
37940 72 65 70 72 6f 63 65 73 73 6f 72 20 73 79 6d 62  reprocessor symb
37950 6f 6c 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53  ol defined..*/.S
37960 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
37970 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c 75  lite3_table_colu
37980 6d 6e 5f 6d 65 74 61 64 61 74 61 28 0a 20 20 73  mn_metadata(.  s
37990 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
379a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
379b0 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20  nnection handle 
379c0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
379d0 2a 7a 44 62 4e 61 6d 65 2c 20 20 20 20 20 20 20  *zDbName,       
379e0 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 61 6d   /* Database nam
379f0 65 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63  e or NULL */.  c
37a00 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c  onst char *zTabl
37a10 65 4e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 54 61  eName,     /* Ta
37a20 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f  ble name */.  co
37a30 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d  nst char *zColum
37a40 6e 4e 61 6d 65 2c 20 20 20 20 2f 2a 20 43 6f 6c  nName,    /* Col
37a50 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 68  umn name */.  ch
37a60 61 72 20 63 6f 6e 73 74 20 2a 2a 70 7a 44 61 74  ar const **pzDat
37a70 61 54 79 70 65 2c 20 20 20 20 2f 2a 20 4f 55 54  aType,    /* OUT
37a80 50 55 54 3a 20 44 65 63 6c 61 72 65 64 20 64 61  PUT: Declared da
37a90 74 61 20 74 79 70 65 20 2a 2f 0a 20 20 63 68 61  ta type */.  cha
37aa0 72 20 63 6f 6e 73 74 20 2a 2a 70 7a 43 6f 6c 6c  r const **pzColl
37ab0 53 65 71 2c 20 20 20 20 20 2f 2a 20 4f 55 54 50  Seq,     /* OUTP
37ac0 55 54 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65  UT: Collation se
37ad0 71 75 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a 20  quence name */. 
37ae0 20 69 6e 74 20 2a 70 4e 6f 74 4e 75 6c 6c 2c 20   int *pNotNull, 
37af0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
37b00 4f 55 54 50 55 54 3a 20 54 72 75 65 20 69 66 20  OUTPUT: True if 
37b10 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61  NOT NULL constra
37b20 69 6e 74 20 65 78 69 73 74 73 20 2a 2f 0a 20 20  int exists */.  
37b30 69 6e 74 20 2a 70 50 72 69 6d 61 72 79 4b 65 79  int *pPrimaryKey
37b40 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  ,           /* O
37b50 55 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 63  UTPUT: True if c
37b60 6f 6c 75 6d 6e 20 70 61 72 74 20 6f 66 20 50 4b  olumn part of PK
37b70 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 41 75 74 6f   */.  int *pAuto
37b80 69 6e 63 20 20 20 20 20 20 20 20 20 20 20 20 20  inc             
37b90 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75    /* OUTPUT: Tru
37ba0 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 61  e if column is a
37bb0 75 74 6f 2d 69 6e 63 72 65 6d 65 6e 74 20 2a 2f  uto-increment */
37bc0 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .);../*.** CAPI3
37bd0 52 45 46 3a 20 4c 6f 61 64 20 41 6e 20 45 78 74  REF: Load An Ext
37be0 65 6e 73 69 6f 6e 20 7b 46 31 32 36 30 30 7d 0a  ension {F12600}.
37bf0 2a 2a 0a 2a 2a 20 7b 46 31 32 36 30 31 7d 20 54  **.** {F12601} T
37c00 68 65 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f  he sqlite3_load_
37c10 65 78 74 65 6e 73 69 6f 6e 28 29 20 69 6e 74 65  extension() inte
37c20 72 66 61 63 65 0a 2a 2a 20 61 74 74 65 6d 70 74  rface.** attempt
37c30 73 20 74 6f 20 6c 6f 61 64 20 61 6e 20 53 51 4c  s to load an SQL
37c40 69 74 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c 69  ite extension li
37c50 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 65 64 20  brary contained 
37c60 69 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 7a  in the file.** z
37c70 46 69 6c 65 2e 20 7b 46 31 32 36 30 32 7d 20 54  File. {F12602} T
37c80 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 69  he entry point i
37c90 73 20 7a 50 72 6f 63 2e 20 7b 46 31 32 36 30 33  s zProc. {F12603
37ca0 7d 20 7a 50 72 6f 63 20 6d 61 79 20 62 65 20 30  } zProc may be 0
37cb0 0a 2a 2a 20 69 6e 20 77 68 69 63 68 20 63 61 73  .** in which cas
37cc0 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  e the name of th
37cd0 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 64 65  e entry point de
37ce0 66 61 75 6c 74 73 0a 2a 2a 20 74 6f 20 22 73 71  faults.** to "sq
37cf0 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f  lite3_extension_
37d00 69 6e 69 74 22 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31  init"..**.** {F1
37d10 32 36 30 34 7d 20 54 68 65 20 73 71 6c 69 74 65  2604} The sqlite
37d20 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  3_load_extension
37d30 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 68 61  () interface sha
37d40 6c 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 5b 53 51  ll.** return [SQ
37d50 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63  LITE_OK] on succ
37d60 65 73 73 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f  ess and [SQLITE_
37d70 45 52 52 4f 52 5d 20 69 66 20 73 6f 6d 65 74 68  ERROR] if someth
37d80 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a  ing goes wrong..
37d90 2a 2a 0a 2a 2a 20 7b 46 31 32 36 30 35 7d 0a 2a  **.** {F12605}.*
37da0 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  * If an error oc
37db0 63 75 72 73 20 61 6e 64 20 70 7a 45 72 72 4d 73  curs and pzErrMs
37dc0 67 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e  g is not 0, then
37dd0 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   the.** sqlite3_
37de0 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29  load_extension()
37df0 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c   interface shall
37e00 20 61 74 74 65 6d 70 74 20 74 6f 20 66 69 6c 6c   attempt to fill
37e10 20 2a 70 7a 45 72 72 4d 73 67 20 77 69 74 68 20   *pzErrMsg with 
37e20 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67  .** error messag
37e30 65 20 74 65 78 74 20 73 74 6f 72 65 64 20 69 6e  e text stored in
37e40 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
37e50 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d   from [sqlite3_m
37e60 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2a 20 7b 45 4e  alloc()]..** {EN
37e70 44 7d 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20  D}  The calling 
37e80 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20  function should 
37e90 66 72 65 65 20 74 68 69 73 20 6d 65 6d 6f 72 79  free this memory
37ea0 0a 2a 2a 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b  .** by calling [
37eb0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e  sqlite3_free()].
37ec0 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 36 30 36 7d 0a  .**.** {F12606}.
37ed0 2a 2a 20 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61  ** Extension loa
37ee0 64 69 6e 67 20 6d 75 73 74 20 62 65 20 65 6e 61  ding must be ena
37ef0 62 6c 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69  bled using [sqli
37f00 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f  te3_enable_load_
37f10 65 78 74 65 6e 73 69 6f 6e 28 29 5d 0a 2a 2a 20  extension()].** 
37f20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67  prior to calling
37f30 20 74 68 69 73 20 41 50 49 20 6f 72 20 61 6e 20   this API or an 
37f40 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20 72 65  error will be re
37f50 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  turned..*/.SQLIT
37f60 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
37f70 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  3_load_extension
37f80 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
37f90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 61            /* Loa
37fa0 64 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20  d the extension 
37fb0 69 6e 74 6f 20 74 68 69 73 20 64 61 74 61 62 61  into this databa
37fc0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
37fd0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
37fe0 46 69 6c 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65  File,    /* Name
37ff0 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 6c   of the shared l
38000 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 69 6e  ibrary containin
38010 67 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20  g extension */. 
38020 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 72   const char *zPr
38030 6f 63 2c 20 20 20 20 2f 2a 20 45 6e 74 72 79 20  oc,    /* Entry 
38040 70 6f 69 6e 74 2e 20 20 44 65 72 69 76 65 64 20  point.  Derived 
38050 66 72 6f 6d 20 7a 46 69 6c 65 20 69 66 20 30 20  from zFile if 0 
38060 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72  */.  char **pzEr
38070 72 4d 73 67 20 20 20 20 20 20 20 2f 2a 20 50 75  rMsg       /* Pu
38080 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  t error message 
38090 68 65 72 65 20 69 66 20 6e 6f 74 20 30 20 2a 2f  here if not 0 */
380a0 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .);../*.** CAPI3
380b0 52 45 46 3a 20 20 45 6e 61 62 6c 65 20 4f 72 20  REF:  Enable Or 
380c0 44 69 73 61 62 6c 65 20 45 78 74 65 6e 73 69 6f  Disable Extensio
380d0 6e 20 4c 6f 61 64 69 6e 67 20 7b 46 31 32 36 32  n Loading {F1262
380e0 30 7d 0a 2a 2a 0a 2a 2a 20 53 6f 20 61 73 20 6e  0}.**.** So as n
380f0 6f 74 20 74 6f 20 6f 70 65 6e 20 73 65 63 75 72  ot to open secur
38100 69 74 79 20 68 6f 6c 65 73 20 69 6e 20 6f 6c 64  ity holes in old
38110 65 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  er applications 
38120 74 68 61 74 20 61 72 65 0a 2a 2a 20 75 6e 70 72  that are.** unpr
38130 65 70 61 72 65 64 20 74 6f 20 64 65 61 6c 20 77  epared to deal w
38140 69 74 68 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f  ith extension lo
38150 61 64 69 6e 67 2c 20 61 6e 64 20 61 73 20 61 20  ading, and as a 
38160 6d 65 61 6e 73 20 6f 66 20 64 69 73 61 62 6c 69  means of disabli
38170 6e 67 0a 2a 2a 20 65 78 74 65 6e 73 69 6f 6e 20  ng.** extension 
38180 6c 6f 61 64 69 6e 67 20 77 68 69 6c 65 20 65 76  loading while ev
38190 61 6c 75 61 74 69 6e 67 20 75 73 65 72 2d 65 6e  aluating user-en
381a0 74 65 72 65 64 20 53 51 4c 2c 20 74 68 65 20 66  tered SQL, the f
381b0 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 41 50 49 20  ollowing.** API 
381c0 69 73 20 70 72 6f 76 69 64 65 64 20 74 6f 20 74  is provided to t
381d0 75 72 6e 20 74 68 65 20 5b 73 71 6c 69 74 65 33  urn the [sqlite3
381e0 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28  _load_extension(
381f0 29 5d 20 6d 65 63 68 61 6e 69 73 6d 20 6f 6e 20  )] mechanism on 
38200 61 6e 64 0a 2a 2a 20 6f 66 66 2e 20 20 7b 46 31  and.** off.  {F1
38210 32 36 32 32 7d 20 49 74 20 69 73 20 6f 66 66 20  2622} It is off 
38220 62 79 20 64 65 66 61 75 6c 74 2e 20 7b 45 4e 44  by default. {END
38230 7d 20 53 65 65 20 74 69 63 6b 65 74 20 23 31 38  } See ticket #18
38240 36 33 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 36 32  63..**.** {F1262
38250 31 7d 20 43 61 6c 6c 20 74 68 65 20 73 71 6c 69  1} Call the sqli
38260 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f  te3_enable_load_
38270 65 78 74 65 6e 73 69 6f 6e 28 29 20 72 6f 75 74  extension() rout
38280 69 6e 65 0a 2a 2a 20 77 69 74 68 20 6f 6e 6f 66  ine.** with onof
38290 66 3d 3d 31 20 74 6f 20 74 75 72 6e 20 65 78 74  f==1 to turn ext
382a0 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 6f  ension loading o
382b0 6e 0a 2a 2a 20 61 6e 64 20 63 61 6c 6c 20 69 74  n.** and call it
382c0 20 77 69 74 68 20 6f 6e 6f 66 66 3d 3d 30 20 74   with onoff==0 t
382d0 6f 20 74 75 72 6e 20 69 74 20 62 61 63 6b 20 6f  o turn it back o
382e0 66 66 20 61 67 61 69 6e 2e 20 7b 45 4e 44 7d 0a  ff again. {END}.
382f0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
38300 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65  t sqlite3_enable
38310 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28  _load_extension(
38320 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
38330 20 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a 2a 2a 20   onoff);../*.** 
38340 43 41 50 49 33 52 45 46 3a 20 4d 61 6b 65 20 41  CAPI3REF: Make A
38350 72 72 61 6e 67 65 6d 65 6e 74 73 20 54 6f 20 41  rrangements To A
38360 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 4c 6f 61  utomatically Loa
38370 64 20 41 6e 20 45 78 74 65 6e 73 69 6f 6e 20 7b  d An Extension {
38380 46 31 32 36 34 30 7d 0a 2a 2a 0a 2a 2a 20 7b 46  F12640}.**.** {F
38390 31 32 36 34 31 7d 20 54 68 69 73 20 66 75 6e 63  12641} This func
383a0 74 69 6f 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72  tion.** register
383b0 73 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e 20 65  s an extension e
383c0 6e 74 72 79 20 70 6f 69 6e 74 20 74 68 61 74 20  ntry point that 
383d0 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  is automatically
383e0 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 77 68 65 6e   invoked.** when
383f0 65 76 65 72 20 61 20 6e 65 77 20 64 61 74 61 62  ever a new datab
38400 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  ase connection i
38410 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 0a 2a  s opened using.*
38420 2a 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  * [sqlite3_open(
38430 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  )], [sqlite3_ope
38440 6e 31 36 28 29 5d 2c 20 6f 72 20 5b 73 71 6c 69  n16()], or [sqli
38450 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2e 20  te3_open_v2()]. 
38460 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73  {END}.**.** This
38470 20 41 50 49 20 63 61 6e 20 62 65 20 69 6e 76 6f   API can be invo
38480 6b 65 64 20 61 74 20 70 72 6f 67 72 61 6d 20 73  ked at program s
38490 74 61 72 74 75 70 20 69 6e 20 6f 72 64 65 72 20  tartup in order 
384a0 74 6f 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6f  to register.** o
384b0 6e 65 20 6f 72 20 6d 6f 72 65 20 73 74 61 74 69  ne or more stati
384c0 63 61 6c 6c 79 20 6c 69 6e 6b 65 64 20 65 78 74  cally linked ext
384d0 65 6e 73 69 6f 6e 73 20 74 68 61 74 20 77 69 6c  ensions that wil
384e0 6c 20 62 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a  l be available.*
384f0 2a 20 74 6f 20 61 6c 6c 20 6e 65 77 20 64 61 74  * to all new dat
38500 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
38510 73 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 36 34 32  s..**.** {F12642
38520 7d 20 44 75 70 6c 69 63 61 74 65 20 65 78 74 65  } Duplicate exte
38530 6e 73 69 6f 6e 73 20 61 72 65 20 64 65 74 65 63  nsions are detec
38540 74 65 64 20 73 6f 20 63 61 6c 6c 69 6e 67 20 74  ted so calling t
38550 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 6c 74  his routine mult
38560 69 70 6c 65 0a 2a 2a 20 74 69 6d 65 73 20 77 69  iple.** times wi
38570 74 68 20 74 68 65 20 73 61 6d 65 20 65 78 74 65  th the same exte
38580 6e 73 69 6f 6e 20 69 73 20 68 61 72 6d 6c 65 73  nsion is harmles
38590 73 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 36 34 33  s..**.** {F12643
385a0 7d 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  } This routine s
385b0 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72 20  tores a pointer 
385c0 74 6f 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e  to the extension
385d0 20 69 6e 20 61 6e 20 61 72 72 61 79 0a 2a 2a 20   in an array.** 
385e0 74 68 61 74 20 69 73 20 6f 62 74 61 69 6e 65 64  that is obtained
385f0 20 66 72 6f 6d 20 73 71 6c 69 74 65 5f 6d 61 6c   from sqlite_mal
38600 6c 6f 63 28 29 2e 20 7b 45 4e 44 7d 20 49 66 20  loc(). {END} If 
38610 79 6f 75 20 72 75 6e 20 61 20 6d 65 6d 6f 72 79  you run a memory
38620 20 6c 65 61 6b 0a 2a 2a 20 63 68 65 63 6b 65 72   leak.** checker
38630 20 6f 6e 20 79 6f 75 72 20 70 72 6f 67 72 61 6d   on your program
38640 20 61 6e 64 20 69 74 20 72 65 70 6f 72 74 73 20   and it reports 
38650 61 20 6c 65 61 6b 20 62 65 63 61 75 73 65 20 6f  a leak because o
38660 66 20 74 68 69 73 0a 2a 2a 20 61 72 72 61 79 2c  f this.** array,
38670 20 74 68 65 6e 20 69 6e 76 6f 6b 65 20 5b 73 71   then invoke [sq
38680 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75 74 6f  lite3_reset_auto
38690 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 70 72  _extension()] pr
386a0 69 6f 72 0a 2a 2a 20 74 6f 20 73 68 75 74 64 6f  ior.** to shutdo
386b0 77 6e 20 74 6f 20 66 72 65 65 20 74 68 65 20 6d  wn to free the m
386c0 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31  emory..**.** {F1
386d0 32 36 34 34 7d 20 41 75 74 6f 6d 61 74 69 63 20  2644} Automatic 
386e0 65 78 74 65 6e 73 69 6f 6e 73 20 61 70 70 6c 79  extensions apply
386f0 20 61 63 72 6f 73 73 20 61 6c 6c 20 74 68 72 65   across all thre
38700 61 64 73 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a  ads. {END}.**.**
38710 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   This interface 
38720 69 73 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20  is experimental 
38730 61 6e 64 20 69 73 20 73 75 62 6a 65 63 74 20 74  and is subject t
38740 6f 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 72  o change or.** r
38750 65 6d 6f 76 61 6c 20 69 6e 20 66 75 74 75 72 65  emoval in future
38760 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c   releases of SQL
38770 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ite..*/.SQLITE_A
38780 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61  PI int sqlite3_a
38790 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f  uto_extension(vo
387a0 69 64 20 2a 78 45 6e 74 72 79 50 6f 69 6e 74 29  id *xEntryPoint)
387b0 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  ;.../*.** CAPI3R
387c0 45 46 3a 20 52 65 73 65 74 20 41 75 74 6f 6d 61  EF: Reset Automa
387d0 74 69 63 20 45 78 74 65 6e 73 69 6f 6e 20 4c 6f  tic Extension Lo
387e0 61 64 69 6e 67 20 7b 46 31 32 36 36 30 7d 0a 2a  ading {F12660}.*
387f0 2a 0a 2a 2a 20 7b 46 31 32 36 36 31 7d 20 54 68  *.** {F12661} Th
38800 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 69 73 61  is function disa
38810 62 6c 65 73 20 61 6c 6c 20 70 72 65 76 69 6f 75  bles all previou
38820 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 0a 2a  sly registered.*
38830 2a 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65  * automatic exte
38840 6e 73 69 6f 6e 73 2e 20 7b 45 4e 44 7d 20 20 54  nsions. {END}  T
38850 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 75  his.** routine u
38860 6e 64 6f 65 73 20 74 68 65 20 65 66 66 65 63 74  ndoes the effect
38870 20 6f 66 20 61 6c 6c 20 70 72 69 6f 72 20 5b 73   of all prior [s
38880 71 6c 69 74 65 33 5f 61 75 74 6f 5f 65 78 74 65  qlite3_auto_exte
38890 6e 73 69 6f 6e 28 29 5d 0a 2a 2a 20 63 61 6c 6c  nsion()].** call
388a0 73 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 32 36 36 32  s..**.** {F12662
388b0 7d 20 54 68 69 73 20 63 61 6c 6c 20 64 69 73 61  } This call disa
388c0 62 6c 65 64 20 61 75 74 6f 6d 61 74 69 63 20 65  bled automatic e
388d0 78 74 65 6e 73 69 6f 6e 73 20 69 6e 20 61 6c 6c  xtensions in all
388e0 20 74 68 72 65 61 64 73 2e 20 7b 45 4e 44 7d 0a   threads. {END}.
388f0 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72  **.** This inter
38900 66 61 63 65 20 69 73 20 65 78 70 65 72 69 6d 65  face is experime
38910 6e 74 61 6c 20 61 6e 64 20 69 73 20 73 75 62 6a  ntal and is subj
38920 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 6f 72  ect to change or
38930 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 69 6e 20 66  .** removal in f
38940 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f  uture releases o
38950 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c  f SQLite..*/.SQL
38960 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
38970 69 74 65 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f  ite3_reset_auto_
38980 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 29 3b  extension(void);
38990 0a 0a 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a 20 45 58 50  .../*.****** EXP
389a0 45 52 49 4d 45 4e 54 41 4c 20 2d 20 73 75 62 6a  ERIMENTAL - subj
389b0 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69  ect to change wi
389c0 74 68 6f 75 74 20 6e 6f 74 69 63 65 20 2a 2a 2a  thout notice ***
389d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
389e0 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20  * The interface 
389f0 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74  to the virtual-t
38a00 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 69  able mechanism i
38a10 73 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73  s currently cons
38a20 69 64 65 72 65 64 0a 2a 2a 20 74 6f 20 62 65 20  idered.** to be 
38a30 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20 20 54  experimental.  T
38a40 68 65 20 69 6e 74 65 72 66 61 63 65 20 6d 69 67  he interface mig
38a50 68 74 20 63 68 61 6e 67 65 20 69 6e 20 69 6e 63  ht change in inc
38a60 6f 6d 70 61 74 69 62 6c 65 20 77 61 79 73 2e 0a  ompatible ways..
38a70 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20  ** If this is a 
38a80 70 72 6f 62 6c 65 6d 20 66 6f 72 20 79 6f 75 2c  problem for you,
38a90 20 64 6f 20 6e 6f 74 20 75 73 65 20 74 68 65 20   do not use the 
38aa0 69 6e 74 65 72 66 61 63 65 20 61 74 20 74 68 69  interface at thi
38ab0 73 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 57 68  s time..**.** Wh
38ac0 65 6e 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74  en the virtual-t
38ad0 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 73  able mechanism s
38ae0 74 61 62 6c 69 7a 65 73 2c 20 77 65 20 77 69 6c  tablizes, we wil
38af0 6c 20 64 65 63 6c 61 72 65 20 74 68 65 0a 2a 2a  l declare the.**
38b00 20 69 6e 74 65 72 66 61 63 65 20 66 69 78 65 64   interface fixed
38b10 2c 20 73 75 70 70 6f 72 74 20 69 74 20 69 6e 64  , support it ind
38b20 65 66 69 6e 69 74 65 6c 79 2c 20 61 6e 64 20 72  efinitely, and r
38b30 65 6d 6f 76 65 20 74 68 69 73 20 63 6f 6d 6d 65  emove this comme
38b40 6e 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 74  nt..*/../*.** St
38b50 72 75 63 74 75 72 65 73 20 75 73 65 64 20 62 79  ructures used by
38b60 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
38b70 6c 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2f 0a  le interface.*/.
38b80 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
38b90 71 6c 69 74 65 33 5f 76 74 61 62 20 73 71 6c 69  qlite3_vtab sqli
38ba0 74 65 33 5f 76 74 61 62 3b 0a 74 79 70 65 64 65  te3_vtab;.typede
38bb0 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  f struct sqlite3
38bc0 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 71 6c 69  _index_info sqli
38bd0 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 3b 0a  te3_index_info;.
38be0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
38bf0 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
38c00 6f 72 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  or sqlite3_vtab_
38c10 63 75 72 73 6f 72 3b 0a 74 79 70 65 64 65 66 20  cursor;.typedef 
38c20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d  struct sqlite3_m
38c30 6f 64 75 6c 65 20 73 71 6c 69 74 65 33 5f 6d 6f  odule sqlite3_mo
38c40 64 75 6c 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  dule;../*.** CAP
38c50 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54  I3REF: Virtual T
38c60 61 62 6c 65 20 4f 62 6a 65 63 74 20 7b 46 31 38  able Object {F18
38c70 30 30 30 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  000}.** KEYWORDS
38c80 3a 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  : sqlite3_module
38c90 0a 2a 2a 0a 2a 2a 20 41 20 6d 6f 64 75 6c 65 20  .**.** A module 
38ca0 69 73 20 61 20 63 6c 61 73 73 20 6f 66 20 76 69  is a class of vi
38cb0 72 74 75 61 6c 20 74 61 62 6c 65 73 2e 20 20 45  rtual tables.  E
38cc0 61 63 68 20 6d 6f 64 75 6c 65 20 69 73 20 64 65  ach module is de
38cd0 66 69 6e 65 64 0a 2a 2a 20 62 79 20 61 6e 20 69  fined.** by an i
38ce0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
38cf0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
38d00 72 65 2e 20 20 54 68 69 73 20 73 74 72 75 63 74  re.  This struct
38d10 75 72 65 20 63 6f 6e 73 69 73 74 73 0a 2a 2a 20  ure consists.** 
38d20 6d 6f 73 74 6c 79 20 6f 66 20 6d 65 74 68 6f 64  mostly of method
38d30 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65  s for the module
38d40 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69  ..*/.struct sqli
38d50 74 65 33 5f 6d 6f 64 75 6c 65 20 7b 0a 20 20 69  te3_module {.  i
38d60 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20 20 69  nt iVersion;.  i
38d70 6e 74 20 28 2a 78 43 72 65 61 74 65 29 28 73 71  nt (*xCreate)(sq
38d80 6c 69 74 65 33 2a 2c 20 76 6f 69 64 20 2a 70 41  lite3*, void *pA
38d90 75 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ux,.            
38da0 20 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e     int argc, con
38db0 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61  st char *const*a
38dc0 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20  rgv,.           
38dd0 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62      sqlite3_vtab
38de0 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61 72 2a   **ppVTab, char*
38df0 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e  *);.  int (*xCon
38e00 6e 65 63 74 29 28 73 71 6c 69 74 65 33 2a 2c 20  nect)(sqlite3*, 
38e10 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 20 20  void *pAux,.    
38e20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 61             int a
38e30 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  rgc, const char 
38e40 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 20  *const*argv,.   
38e50 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
38e60 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 61  te3_vtab **ppVTa
38e70 62 2c 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e  b, char**);.  in
38e80 74 20 28 2a 78 42 65 73 74 49 6e 64 65 78 29 28  t (*xBestIndex)(
38e90 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
38ea0 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64  Tab, sqlite3_ind
38eb0 65 78 5f 69 6e 66 6f 2a 29 3b 0a 20 20 69 6e 74  ex_info*);.  int
38ec0 20 28 2a 78 44 69 73 63 6f 6e 6e 65 63 74 29 28   (*xDisconnect)(
38ed0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
38ee0 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44  Tab);.  int (*xD
38ef0 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f  estroy)(sqlite3_
38f00 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20  vtab *pVTab);.  
38f10 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c  int (*xOpen)(sql
38f20 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62  ite3_vtab *pVTab
38f30 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  , sqlite3_vtab_c
38f40 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72  ursor **ppCursor
38f50 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f 73  );.  int (*xClos
38f60 65 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  e)(sqlite3_vtab_
38f70 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20  cursor*);.  int 
38f80 28 2a 78 46 69 6c 74 65 72 29 28 73 71 6c 69 74  (*xFilter)(sqlit
38f90 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 2c  e3_vtab_cursor*,
38fa0 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f 6e   int idxNum, con
38fb0 73 74 20 63 68 61 72 20 2a 69 64 78 53 74 72 2c  st char *idxStr,
38fc0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
38fd0 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
38fe0 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29  e3_value **argv)
38ff0 3b 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78 74 29  ;.  int (*xNext)
39000 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75  (sqlite3_vtab_cu
39010 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  rsor*);.  int (*
39020 78 45 6f 66 29 28 73 71 6c 69 74 65 33 5f 76 74  xEof)(sqlite3_vt
39030 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69  ab_cursor*);.  i
39040 6e 74 20 28 2a 78 43 6f 6c 75 6d 6e 29 28 73 71  nt (*xColumn)(sq
39050 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
39060 72 2a 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  r*, sqlite3_cont
39070 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e  ext*, int);.  in
39080 74 20 28 2a 78 52 6f 77 69 64 29 28 73 71 6c 69  t (*xRowid)(sqli
39090 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a  te3_vtab_cursor*
390a0 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
390b0 2a 70 52 6f 77 69 64 29 3b 0a 20 20 69 6e 74 20  *pRowid);.  int 
390c0 28 2a 78 55 70 64 61 74 65 29 28 73 71 6c 69 74  (*xUpdate)(sqlit
390d0 65 33 5f 76 74 61 62 20 2a 2c 20 69 6e 74 2c 20  e3_vtab *, int, 
390e0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
390f0 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
39100 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 42 65 67  *);.  int (*xBeg
39110 69 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  in)(sqlite3_vtab
39120 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20   *pVTab);.  int 
39130 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74 65 33  (*xSync)(sqlite3
39140 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20  _vtab *pVTab);. 
39150 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 29 28   int (*xCommit)(
39160 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
39170 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52  Tab);.  int (*xR
39180 6f 6c 6c 62 61 63 6b 29 28 73 71 6c 69 74 65 33  ollback)(sqlite3
39190 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20  _vtab *pVTab);. 
391a0 20 69 6e 74 20 28 2a 78 46 69 6e 64 46 75 6e 63   int (*xFindFunc
391b0 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 5f 76 74  tion)(sqlite3_vt
391c0 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74 20 6e  ab *pVtab, int n
391d0 41 72 67 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  Arg, const char 
391e0 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  *zName,.        
391f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76                 v
39200 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29 28 73  oid (**pxFunc)(s
39210 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
39220 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
39230 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20 20 20 20  e**),.          
39240 20 20 20 20 20 20 20 20 20 20 20 20 20 76 6f 69               voi
39250 64 20 2a 2a 70 70 41 72 67 29 3b 0a 0a 20 20 69  d **ppArg);..  i
39260 6e 74 20 28 2a 78 52 65 6e 61 6d 65 29 28 73 71  nt (*xRename)(sq
39270 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
39280 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
39290 4e 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  New);.};../*.** 
392a0 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61  CAPI3REF: Virtua
392b0 6c 20 54 61 62 6c 65 20 49 6e 64 65 78 69 6e 67  l Table Indexing
392c0 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 7b 46 31   Information {F1
392d0 38 31 30 30 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44  8100}.** KEYWORD
392e0 53 3a 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  S: sqlite3_index
392f0 5f 69 6e 66 6f 0a 2a 2a 0a 2a 2a 20 54 68 65 20  _info.**.** The 
39300 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
39310 66 6f 20 73 74 72 75 63 74 75 72 65 20 61 6e 64  fo structure and
39320 20 69 74 73 20 73 75 62 73 74 72 75 63 74 75 72   its substructur
39330 65 73 20 69 73 20 75 73 65 64 20 74 6f 0a 2a 2a  es is used to.**
39340 20 70 61 73 73 20 69 6e 66 6f 72 6d 61 74 69 6f   pass informatio
39350 6e 20 69 6e 74 6f 20 61 6e 64 20 72 65 63 65 69  n into and recei
39360 76 65 20 74 68 65 20 72 65 70 6c 79 20 66 72 6f  ve the reply fro
39370 6d 20 74 68 65 20 78 42 65 73 74 49 6e 64 65 78  m the xBestIndex
39380 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20 61 6e  .** method of an
39390 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2e   sqlite3_module.
393a0 20 20 54 68 65 20 66 69 65 6c 64 73 20 75 6e 64    The fields und
393b0 65 72 20 2a 2a 49 6e 70 75 74 73 2a 2a 20 61 72  er **Inputs** ar
393c0 65 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 73 20  e the.** inputs 
393d0 74 6f 20 78 42 65 73 74 49 6e 64 65 78 20 61 6e  to xBestIndex an
393e0 64 20 61 72 65 20 72 65 61 64 2d 6f 6e 6c 79 2e  d are read-only.
393f0 20 20 78 42 65 73 74 49 6e 64 65 78 20 69 6e 73    xBestIndex ins
39400 65 72 74 73 20 69 74 73 0a 2a 2a 20 72 65 73 75  erts its.** resu
39410 6c 74 73 20 69 6e 74 6f 20 74 68 65 20 2a 2a 4f  lts into the **O
39420 75 74 70 75 74 73 2a 2a 20 66 69 65 6c 64 73 2e  utputs** fields.
39430 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 43 6f 6e 73  .**.** The aCons
39440 74 72 61 69 6e 74 5b 5d 20 61 72 72 61 79 20 72  traint[] array r
39450 65 63 6f 72 64 73 20 57 48 45 52 45 20 63 6c 61  ecords WHERE cla
39460 75 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  use constraints 
39470 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 72 6d 3a 0a  of the.** form:.
39480 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 6f  **.**         co
39490 6c 75 6d 6e 20 4f 50 20 65 78 70 72 0a 2a 2a 0a  lumn OP expr.**.
394a0 2a 2a 20 57 68 65 72 65 20 4f 50 20 69 73 20 3d  ** Where OP is =
394b0 2c 20 26 6c 74 3b 2c 20 26 6c 74 3b 3d 2c 20 26  , &lt;, &lt;=, &
394c0 67 74 3b 2c 20 6f 72 20 26 67 74 3b 3d 2e 20 20  gt;, or &gt;=.  
394d0 0a 2a 2a 20 54 68 65 20 70 61 72 74 69 63 75 6c  .** The particul
394e0 61 72 20 6f 70 65 72 61 74 6f 72 20 69 73 20 73  ar operator is s
394f0 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 61 43 6f 6e  tored.** in aCon
39500 73 74 72 61 69 6e 74 5b 5d 2e 6f 70 2e 20 20 54  straint[].op.  T
39510 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
39520 63 6f 6c 75 6d 6e 20 69 73 20 73 74 6f 72 65 64  column is stored
39530 20 69 6e 20 0a 2a 2a 20 61 43 6f 6e 73 74 72 61   in .** aConstra
39540 69 6e 74 5b 5d 2e 69 43 6f 6c 75 6d 6e 2e 20 20  int[].iColumn.  
39550 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 75 73  aConstraint[].us
39560 61 62 6c 65 20 69 73 20 54 52 55 45 20 69 66 20  able is TRUE if 
39570 74 68 65 0a 2a 2a 20 65 78 70 72 20 6f 6e 20 74  the.** expr on t
39580 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69  he right-hand si
39590 64 65 20 63 61 6e 20 62 65 20 65 76 61 6c 75 61  de can be evalua
395a0 74 65 64 20 28 61 6e 64 20 74 68 75 73 20 74 68  ted (and thus th
395b0 65 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20  e constraint.** 
395c0 69 73 20 75 73 61 62 6c 65 29 20 61 6e 64 20 66  is usable) and f
395d0 61 6c 73 65 20 69 66 20 69 74 20 63 61 6e 6e 6f  alse if it canno
395e0 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 74  t..**.** The opt
395f0 69 6d 69 7a 65 72 20 61 75 74 6f 6d 61 74 69 63  imizer automatic
39600 61 6c 6c 79 20 69 6e 76 65 72 74 73 20 74 65 72  ally inverts ter
39610 6d 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  ms of the form "
39620 65 78 70 72 20 4f 50 20 63 6f 6c 75 6d 6e 22 0a  expr OP column".
39630 2a 2a 20 61 6e 64 20 6d 61 6b 65 73 20 6f 74 68  ** and makes oth
39640 65 72 20 73 69 6d 70 6c 69 66 69 63 61 74 69 6f  er simplificatio
39650 6e 73 20 74 6f 20 74 68 65 20 57 48 45 52 45 20  ns to the WHERE 
39660 63 6c 61 75 73 65 20 69 6e 20 61 6e 20 61 74 74  clause in an att
39670 65 6d 70 74 20 74 6f 0a 2a 2a 20 67 65 74 20 61  empt to.** get a
39680 73 20 6d 61 6e 79 20 57 48 45 52 45 20 63 6c 61  s many WHERE cla
39690 75 73 65 20 74 65 72 6d 73 20 69 6e 74 6f 20 74  use terms into t
396a0 68 65 20 66 6f 72 6d 20 73 68 6f 77 6e 20 61 62  he form shown ab
396b0 6f 76 65 20 61 73 20 70 6f 73 73 69 62 6c 65 2e  ove as possible.
396c0 0a 2a 2a 20 54 68 65 20 61 43 6f 6e 73 74 72 61  .** The aConstra
396d0 69 6e 74 5b 5d 20 61 72 72 61 79 20 6f 6e 6c 79  int[] array only
396e0 20 72 65 70 6f 72 74 73 20 57 48 45 52 45 20 63   reports WHERE c
396f0 6c 61 75 73 65 20 74 65 72 6d 73 20 69 6e 20 74  lause terms in t
39700 68 65 20 63 6f 72 72 65 63 74 0a 2a 2a 20 66 6f  he correct.** fo
39710 72 6d 20 74 68 61 74 20 72 65 66 65 72 20 74 6f  rm that refer to
39720 20 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20   the particular 
39730 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 62 65  virtual table be
39740 69 6e 67 20 71 75 65 72 69 65 64 2e 0a 2a 2a 0a  ing queried..**.
39750 2a 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  ** Information a
39760 62 6f 75 74 20 74 68 65 20 4f 52 44 45 52 20 42  bout the ORDER B
39770 59 20 63 6c 61 75 73 65 20 69 73 20 73 74 6f 72  Y clause is stor
39780 65 64 20 69 6e 20 61 4f 72 64 65 72 42 79 5b 5d  ed in aOrderBy[]
39790 2e 0a 2a 2a 20 45 61 63 68 20 74 65 72 6d 20 6f  ..** Each term o
397a0 66 20 61 4f 72 64 65 72 42 79 20 72 65 63 6f 72  f aOrderBy recor
397b0 64 73 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  ds a column of t
397c0 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
397d0 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 42  se..**.** The xB
397e0 65 73 74 49 6e 64 65 78 20 6d 65 74 68 6f 64 20  estIndex method 
397f0 6d 75 73 74 20 66 69 6c 6c 20 61 43 6f 6e 73 74  must fill aConst
39800 72 61 69 6e 74 55 73 61 67 65 5b 5d 20 77 69 74  raintUsage[] wit
39810 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a  h information.**
39820 20 61 62 6f 75 74 20 77 68 61 74 20 70 61 72 61   about what para
39830 6d 65 74 65 72 73 20 74 6f 20 70 61 73 73 20 74  meters to pass t
39840 6f 20 78 46 69 6c 74 65 72 2e 20 20 49 66 20 61  o xFilter.  If a
39850 72 67 76 49 6e 64 65 78 3e 30 20 74 68 65 6e 0a  rgvIndex>0 then.
39860 2a 2a 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e  ** the right-han
39870 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 63 6f  d side of the co
39880 72 72 65 73 70 6f 6e 64 69 6e 67 20 61 43 6f 6e  rresponding aCon
39890 73 74 72 61 69 6e 74 5b 5d 20 69 73 20 65 76 61  straint[] is eva
398a0 6c 75 61 74 65 64 0a 2a 2a 20 61 6e 64 20 62 65  luated.** and be
398b0 63 6f 6d 65 73 20 74 68 65 20 61 72 67 76 49 6e  comes the argvIn
398c0 64 65 78 2d 74 68 20 65 6e 74 72 79 20 69 6e 20  dex-th entry in 
398d0 61 72 67 76 2e 20 20 49 66 20 61 43 6f 6e 73 74  argv.  If aConst
398e0 72 61 69 6e 74 55 73 61 67 65 5b 5d 2e 6f 6d 69  raintUsage[].omi
398f0 74 0a 2a 2a 20 69 73 20 74 72 75 65 2c 20 74 68  t.** is true, th
39900 65 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  en the constrain
39910 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20  t is assumed to 
39920 62 65 20 66 75 6c 6c 79 20 68 61 6e 64 6c 65 64  be fully handled
39930 20 62 79 20 74 68 65 0a 2a 2a 20 76 69 72 74 75   by the.** virtu
39940 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 69 73 20  al table and is 
39950 6e 6f 74 20 63 68 65 63 6b 65 64 20 61 67 61 69  not checked agai
39960 6e 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a  n by SQLite..**.
39970 2a 2a 20 54 68 65 20 69 64 78 4e 75 6d 20 61 6e  ** The idxNum an
39980 64 20 69 64 78 50 74 72 20 76 61 6c 75 65 73 20  d idxPtr values 
39990 61 72 65 20 72 65 63 6f 72 64 65 64 20 61 6e 64  are recorded and
399a0 20 70 61 73 73 65 64 20 69 6e 74 6f 20 78 46 69   passed into xFi
399b0 6c 74 65 72 2e 0a 2a 2a 20 73 71 6c 69 74 65 33  lter..** sqlite3
399c0 5f 66 72 65 65 28 29 20 69 73 20 75 73 65 64 20  _free() is used 
399d0 74 6f 20 66 72 65 65 20 69 64 78 50 74 72 20 69  to free idxPtr i
399e0 66 20 6e 65 65 64 54 6f 46 72 65 65 49 64 78 50  f needToFreeIdxP
399f0 74 72 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a  tr is true..**.*
39a00 2a 20 54 68 65 20 6f 72 64 65 72 42 79 43 6f 6e  * The orderByCon
39a10 73 75 6d 65 64 20 6d 65 61 6e 73 20 74 68 61 74  sumed means that
39a20 20 6f 75 74 70 75 74 20 66 72 6f 6d 20 78 46 69   output from xFi
39a30 6c 74 65 72 20 77 69 6c 6c 20 6f 63 63 75 72 20  lter will occur 
39a40 69 6e 0a 2a 2a 20 74 68 65 20 63 6f 72 72 65 63  in.** the correc
39a50 74 20 6f 72 64 65 72 20 74 6f 20 73 61 74 69 73  t order to satis
39a60 66 79 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  fy the ORDER BY 
39a70 63 6c 61 75 73 65 20 73 6f 20 74 68 61 74 20 6e  clause so that n
39a80 6f 20 73 65 70 61 72 61 74 65 0a 2a 2a 20 73 6f  o separate.** so
39a90 72 74 69 6e 67 20 73 74 65 70 20 69 73 20 72 65  rting step is re
39aa0 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  quired..**.** Th
39ab0 65 20 65 73 74 69 6d 61 74 65 64 43 6f 73 74 20  e estimatedCost 
39ac0 76 61 6c 75 65 20 69 73 20 61 6e 20 65 73 74 69  value is an esti
39ad0 6d 61 74 65 20 6f 66 20 74 68 65 20 63 6f 73 74  mate of the cost
39ae0 20 6f 66 20 64 6f 69 6e 67 20 74 68 65 0a 2a 2a   of doing the.**
39af0 20 70 61 72 74 69 63 75 6c 61 72 20 6c 6f 6f 6b   particular look
39b00 75 70 2e 20 20 41 20 66 75 6c 6c 20 73 63 61 6e  up.  A full scan
39b10 20 6f 66 20 61 20 74 61 62 6c 65 20 77 69 74 68   of a table with
39b20 20 4e 20 65 6e 74 72 69 65 73 20 73 68 6f 75 6c   N entries shoul
39b30 64 20 68 61 76 65 0a 2a 2a 20 61 20 63 6f 73 74  d have.** a cost
39b40 20 6f 66 20 4e 2e 20 20 41 20 62 69 6e 61 72 79   of N.  A binary
39b50 20 73 65 61 72 63 68 20 6f 66 20 61 20 74 61 62   search of a tab
39b60 6c 65 20 6f 66 20 4e 20 65 6e 74 72 69 65 73 20  le of N entries 
39b70 73 68 6f 75 6c 64 20 68 61 76 65 20 61 0a 2a 2a  should have a.**
39b80 20 63 6f 73 74 20 6f 66 20 61 70 70 72 6f 78 69   cost of approxi
39b90 6d 61 74 65 6c 79 20 6c 6f 67 28 4e 29 2e 0a 2a  mately log(N)..*
39ba0 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  /.struct sqlite3
39bb0 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 7b 0a 20 20  _index_info {.  
39bc0 2f 2a 20 49 6e 70 75 74 73 20 2a 2f 0a 20 20 69  /* Inputs */.  i
39bd0 6e 74 20 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20  nt nConstraint; 
39be0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
39bf0 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
39c00 6e 20 61 43 6f 6e 73 74 72 61 69 6e 74 20 2a 2f  n aConstraint */
39c10 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  .  struct sqlite
39c20 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69  3_index_constrai
39c30 6e 74 20 7b 0a 20 20 20 20 20 69 6e 74 20 69 43  nt {.     int iC
39c40 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20  olumn;          
39c50 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6f 6e      /* Column on
39c60 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20   left-hand side 
39c70 6f 66 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f  of constraint */
39c80 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  .     unsigned c
39c90 68 61 72 20 6f 70 3b 20 20 20 20 20 20 20 20 20  har op;         
39ca0 2f 2a 20 43 6f 6e 73 74 72 61 69 6e 74 20 6f 70  /* Constraint op
39cb0 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20 75  erator */.     u
39cc0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 75 73 61  nsigned char usa
39cd0 62 6c 65 3b 20 20 20 20 20 2f 2a 20 54 72 75 65  ble;     /* True
39ce0 20 69 66 20 74 68 69 73 20 63 6f 6e 73 74 72 61   if this constra
39cf0 69 6e 74 20 69 73 20 75 73 61 62 6c 65 20 2a 2f  int is usable */
39d00 0a 20 20 20 20 20 69 6e 74 20 69 54 65 72 6d 4f  .     int iTermO
39d10 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20  ffset;          
39d20 2f 2a 20 55 73 65 64 20 69 6e 74 65 72 6e 61 6c  /* Used internal
39d30 6c 79 20 2d 20 78 42 65 73 74 49 6e 64 65 78 20  ly - xBestIndex 
39d40 73 68 6f 75 6c 64 20 69 67 6e 6f 72 65 20 2a 2f  should ignore */
39d50 0a 20 20 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e  .  } *aConstrain
39d60 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
39d70 20 54 61 62 6c 65 20 6f 66 20 57 48 45 52 45 20   Table of WHERE 
39d80 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e  clause constrain
39d90 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 72 64  ts */.  int nOrd
39da0 65 72 42 79 3b 20 20 20 20 20 20 20 20 20 20 20  erBy;           
39db0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
39dc0 74 65 72 6d 73 20 69 6e 20 74 68 65 20 4f 52 44  terms in the ORD
39dd0 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a  ER BY clause */.
39de0 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33    struct sqlite3
39df0 5f 69 6e 64 65 78 5f 6f 72 64 65 72 62 79 20 7b  _index_orderby {
39e00 0a 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d  .     int iColum
39e10 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
39e20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  /* Column number
39e30 20 2a 2f 0a 20 20 20 20 20 75 6e 73 69 67 6e 65   */.     unsigne
39e40 64 20 63 68 61 72 20 64 65 73 63 3b 20 20 20 20  d char desc;    
39e50 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 44     /* True for D
39e60 45 53 43 2e 20 20 46 61 6c 73 65 20 66 6f 72 20  ESC.  False for 
39e70 41 53 43 2e 20 2a 2f 0a 20 20 7d 20 2a 61 4f 72  ASC. */.  } *aOr
39e80 64 65 72 42 79 3b 20 20 20 20 20 20 20 20 20 20  derBy;          
39e90 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45       /* The ORDE
39ea0 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 0a  R BY clause */..
39eb0 20 20 2f 2a 20 4f 75 74 70 75 74 73 20 2a 2f 0a    /* Outputs */.
39ec0 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33    struct sqlite3
39ed0 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e  _index_constrain
39ee0 74 5f 75 73 61 67 65 20 7b 0a 20 20 20 20 69 6e  t_usage {.    in
39ef0 74 20 61 72 67 76 49 6e 64 65 78 3b 20 20 20 20  t argvIndex;    
39f00 20 20 20 20 20 20 20 2f 2a 20 69 66 20 3e 30 2c         /* if >0,
39f10 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 70   constraint is p
39f20 61 72 74 20 6f 66 20 61 72 67 76 20 74 6f 20 78  art of argv to x
39f30 46 69 6c 74 65 72 20 2a 2f 0a 20 20 20 20 75 6e  Filter */.    un
39f40 73 69 67 6e 65 64 20 63 68 61 72 20 6f 6d 69 74  signed char omit
39f50 3b 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74  ;      /* Do not
39f60 20 63 6f 64 65 20 61 20 74 65 73 74 20 66 6f 72   code a test for
39f70 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74   this constraint
39f80 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6e 73 74 72   */.  } *aConstr
39f90 61 69 6e 74 55 73 61 67 65 3b 0a 20 20 69 6e 74  aintUsage;.  int
39fa0 20 69 64 78 4e 75 6d 3b 20 20 20 20 20 20 20 20   idxNum;        
39fb0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
39fc0 72 20 75 73 65 64 20 74 6f 20 69 64 65 6e 74 69  r used to identi
39fd0 66 79 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a  fy the index */.
39fe0 20 20 63 68 61 72 20 2a 69 64 78 53 74 72 3b 20    char *idxStr; 
39ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a000 53 74 72 69 6e 67 2c 20 70 6f 73 73 69 62 6c 79  String, possibly
3a010 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
3a020 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 2a 2f  qlite3_malloc */
3a030 0a 20 20 69 6e 74 20 6e 65 65 64 54 6f 46 72 65  .  int needToFre
3a040 65 49 64 78 53 74 72 3b 20 20 20 20 20 20 2f 2a  eIdxStr;      /*
3a050 20 46 72 65 65 20 69 64 78 53 74 72 20 75 73 69   Free idxStr usi
3a060 6e 67 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  ng sqlite3_free(
3a070 29 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 69  ) if true */.  i
3a080 6e 74 20 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d  nt orderByConsum
3a090 65 64 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ed;       /* Tru
3a0a0 65 20 69 66 20 6f 75 74 70 75 74 20 69 73 20 61  e if output is a
3a0b0 6c 72 65 61 64 79 20 6f 72 64 65 72 65 64 20 2a  lready ordered *
3a0c0 2f 0a 20 20 64 6f 75 62 6c 65 20 65 73 74 69 6d  /.  double estim
3a0d0 61 74 65 64 43 6f 73 74 3b 20 20 20 20 20 20 2f  atedCost;      /
3a0e0 2a 20 45 73 74 69 6d 61 74 65 64 20 63 6f 73 74  * Estimated cost
3a0f0 20 6f 66 20 75 73 69 6e 67 20 74 68 69 73 20 69   of using this i
3a100 6e 64 65 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69  ndex */.};.#defi
3a110 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  ne SQLITE_INDEX_
3a120 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20 20 20  CONSTRAINT_EQ   
3a130 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   2.#define SQLIT
3a140 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
3a150 4e 54 5f 47 54 20 20 20 20 34 0a 23 64 65 66 69  NT_GT    4.#defi
3a160 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  ne SQLITE_INDEX_
3a170 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 45 20 20 20  CONSTRAINT_LE   
3a180 20 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   8.#define SQLIT
3a190 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
3a1a0 4e 54 5f 4c 54 20 20 20 20 31 36 0a 23 64 65 66  NT_LT    16.#def
3a1b0 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58  ine SQLITE_INDEX
3a1c0 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 45 20 20  _CONSTRAINT_GE  
3a1d0 20 20 33 32 0a 23 64 65 66 69 6e 65 20 53 51 4c    32.#define SQL
3a1e0 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
3a1f0 41 49 4e 54 5f 4d 41 54 43 48 20 36 34 0a 0a 2f  AINT_MATCH 64../
3a200 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52  *.** CAPI3REF: R
3a210 65 67 69 73 74 65 72 20 41 20 56 69 72 74 75 61  egister A Virtua
3a220 6c 20 54 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e  l Table Implemen
3a230 74 61 74 69 6f 6e 20 7b 46 31 38 32 30 30 7d 0a  tation {F18200}.
3a240 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
3a250 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  ne is used to re
3a260 67 69 73 74 65 72 20 61 20 6e 65 77 20 6d 6f 64  gister a new mod
3a270 75 6c 65 20 6e 61 6d 65 20 77 69 74 68 20 61 6e  ule name with an
3a280 20 53 51 4c 69 74 65 0a 2a 2a 20 63 6f 6e 6e 65   SQLite.** conne
3a290 63 74 69 6f 6e 2e 20 20 4d 6f 64 75 6c 65 20 6e  ction.  Module n
3a2a0 61 6d 65 73 20 6d 75 73 74 20 62 65 20 72 65 67  ames must be reg
3a2b0 69 73 74 65 72 65 64 20 62 65 66 6f 72 65 20 63  istered before c
3a2c0 72 65 61 74 69 6e 67 20 6e 65 77 0a 2a 2a 20 76  reating new.** v
3a2d0 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6f 6e  irtual tables on
3a2e0 20 74 68 65 20 6d 6f 64 75 6c 65 2c 20 6f 72 20   the module, or 
3a2f0 62 65 66 6f 72 65 20 75 73 69 6e 67 20 70 72 65  before using pre
3a300 65 78 69 73 74 69 6e 67 20 76 69 72 74 75 61 6c  existing virtual
3a310 0a 2a 2a 20 74 61 62 6c 65 73 20 6f 66 20 74 68  .** tables of th
3a320 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c  e module..*/.SQL
3a330 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
3a340 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c  te3_create_modul
3a350 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e(.  sqlite3 *db
3a360 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
3a370 2f 2a 20 53 51 4c 69 74 65 20 63 6f 6e 6e 65 63  /* SQLite connec
3a380 74 69 6f 6e 20 74 6f 20 72 65 67 69 73 74 65 72  tion to register
3a390 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 2a 2f 0a   module with */.
3a3a0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
3a3b0 61 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ame,         /* 
3a3c0 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64 75  Name of the modu
3a3d0 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71  le */.  const sq
3a3e0 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 2c 20  lite3_module *, 
3a3f0 20 20 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66 6f     /* Methods fo
3a400 72 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a  r the module */.
3a410 20 20 76 6f 69 64 20 2a 20 20 20 20 20 20 20 20    void *        
3a420 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a430 43 6c 69 65 6e 74 20 64 61 74 61 20 66 6f 72 20  Client data for 
3a440 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74  xCreate/xConnect
3a450 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   */.);../*.** CA
3a460 50 49 33 52 45 46 3a 20 52 65 67 69 73 74 65 72  PI3REF: Register
3a470 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65   A Virtual Table
3a480 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
3a490 7b 46 31 38 32 31 30 7d 0a 2a 2a 0a 2a 2a 20 54  {F18210}.**.** T
3a4a0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69  his routine is i
3a4b0 64 65 6e 74 69 63 61 6c 20 74 6f 20 74 68 65 20  dentical to the 
3a4c0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
3a4d0 6f 64 75 6c 65 28 29 20 6d 65 74 68 6f 64 20 61  odule() method a
3a4e0 62 6f 76 65 2c 0a 2a 2a 20 65 78 63 65 70 74 20  bove,.** except 
3a4f0 74 68 61 74 20 69 74 20 61 6c 6c 6f 77 73 20 61  that it allows a
3a500 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63   destructor func
3a510 74 69 6f 6e 20 74 6f 20 62 65 20 73 70 65 63 69  tion to be speci
3a520 66 69 65 64 2e 20 49 74 20 69 73 0a 2a 2a 20 65  fied. It is.** e
3a530 76 65 6e 20 6d 6f 72 65 20 65 78 70 65 72 69 6d  ven more experim
3a540 65 6e 74 61 6c 20 74 68 61 6e 20 74 68 65 20 72  ental than the r
3a550 65 73 74 20 6f 66 20 74 68 65 20 76 69 72 74 75  est of the virtu
3a560 61 6c 20 74 61 62 6c 65 73 20 41 50 49 2e 0a 2a  al tables API..*
3a570 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
3a580 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
3a590 6d 6f 64 75 6c 65 5f 76 32 28 0a 20 20 73 71 6c  module_v2(.  sql
3a5a0 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
3a5b0 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74          /* SQLit
3a5c0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20  e connection to 
3a5d0 72 65 67 69 73 74 65 72 20 6d 6f 64 75 6c 65 20  register module 
3a5e0 77 69 74 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  with */.  const 
3a5f0 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20  char *zName,    
3a600 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
3a610 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20  the module */.  
3a620 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
3a630 64 75 6c 65 20 2a 2c 20 20 20 20 2f 2a 20 4d 65  dule *,    /* Me
3a640 74 68 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f  thods for the mo
3a650 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  dule */.  void *
3a660 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
3a670 20 20 20 20 20 2f 2a 20 43 6c 69 65 6e 74 20 64       /* Client d
3a680 61 74 61 20 66 6f 72 20 78 43 72 65 61 74 65 2f  ata for xCreate/
3a690 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20 76 6f  xConnect */.  vo
3a6a0 69 64 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f  id(*xDestroy)(vo
3a6b0 69 64 2a 29 20 20 20 20 20 2f 2a 20 4d 6f 64 75  id*)     /* Modu
3a6c0 6c 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75  le destructor fu
3a6d0 6e 63 74 69 6f 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a  nction */.);../*
3a6e0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69  .** CAPI3REF: Vi
3a6f0 72 74 75 61 6c 20 54 61 62 6c 65 20 49 6e 73 74  rtual Table Inst
3a700 61 6e 63 65 20 4f 62 6a 65 63 74 20 7b 46 31 38  ance Object {F18
3a710 30 31 30 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  010}.** KEYWORDS
3a720 3a 20 73 71 6c 69 74 65 33 5f 76 74 61 62 0a 2a  : sqlite3_vtab.*
3a730 2a 0a 2a 2a 20 45 76 65 72 79 20 6d 6f 64 75 6c  *.** Every modul
3a740 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
3a750 20 75 73 65 73 20 61 20 73 75 62 63 6c 61 73 73   uses a subclass
3a760 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
3a770 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 74  g structure.** t
3a780 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61 72  o describe a par
3a790 74 69 63 75 6c 61 72 20 69 6e 73 74 61 6e 63 65  ticular instance
3a7a0 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20   of the module. 
3a7b0 20 45 61 63 68 20 73 75 62 63 6c 61 73 73 20 77   Each subclass w
3a7c0 69 6c 6c 0a 2a 2a 20 62 65 20 74 61 69 6c 6f 72  ill.** be tailor
3a7d0 65 64 20 74 6f 20 74 68 65 20 73 70 65 63 69 66  ed to the specif
3a7e0 69 63 20 6e 65 65 64 73 20 6f 66 20 74 68 65 20  ic needs of the 
3a7f0 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  module implement
3a800 61 74 69 6f 6e 2e 20 20 20 54 68 65 0a 2a 2a 20  ation.   The.** 
3a810 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20  purpose of this 
3a820 73 75 70 65 72 63 6c 61 73 73 20 69 73 20 74 6f  superclass is to
3a830 20 64 65 66 69 6e 65 20 63 65 72 74 61 69 6e 20   define certain 
3a840 66 69 65 6c 64 73 20 74 68 61 74 20 61 72 65 20  fields that are 
3a850 63 6f 6d 6d 6f 6e 0a 2a 2a 20 74 6f 20 61 6c 6c  common.** to all
3a860 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e   module implemen
3a870 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56  tations..**.** V
3a880 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 65  irtual tables me
3a890 74 68 6f 64 73 20 63 61 6e 20 73 65 74 20 61 6e  thods can set an
3a8a0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62   error message b
3a8b0 79 20 61 73 73 69 67 6e 69 6e 67 20 61 0a 2a 2a  y assigning a.**
3a8c0 20 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64   string obtained
3a8d0 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 70   from sqlite3_mp
3a8e0 72 69 6e 74 66 28 29 20 74 6f 20 7a 45 72 72 4d  rintf() to zErrM
3a8f0 73 67 2e 20 20 54 68 65 20 6d 65 74 68 6f 64 20  sg.  The method 
3a900 73 68 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65 20 63  should.** take c
3a910 61 72 65 20 74 68 61 74 20 61 6e 79 20 70 72 69  are that any pri
3a920 6f 72 20 73 74 72 69 6e 67 20 69 73 20 66 72 65  or string is fre
3a930 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20  ed by a call to 
3a940 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 0a 2a  sqlite3_free().*
3a950 2a 20 70 72 69 6f 72 20 74 6f 20 61 73 73 69 67  * prior to assig
3a960 6e 69 6e 67 20 61 20 6e 65 77 20 73 74 72 69 6e  ning a new strin
3a970 67 20 74 6f 20 7a 45 72 72 4d 73 67 2e 20 20 41  g to zErrMsg.  A
3a980 66 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 6d  fter the error m
3a990 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 64 65 6c  essage.** is del
3a9a0 69 76 65 72 65 64 20 75 70 20 74 6f 20 74 68 65  ivered up to the
3a9b0 20 63 6c 69 65 6e 74 20 61 70 70 6c 69 63 61 74   client applicat
3a9c0 69 6f 6e 2c 20 74 68 65 20 73 74 72 69 6e 67 20  ion, the string 
3a9d0 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69  will be automati
3a9e0 63 61 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 20 62  cally.** freed b
3a9f0 79 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  y sqlite3_free()
3aa00 20 61 6e 64 20 74 68 65 20 7a 45 72 72 4d 73 67   and the zErrMsg
3aa10 20 66 69 65 6c 64 20 77 69 6c 6c 20 62 65 20 7a   field will be z
3aa20 65 72 6f 65 64 2e 20 20 4e 6f 74 65 0a 2a 2a 20  eroed.  Note.** 
3aa30 74 68 61 74 20 73 71 6c 69 74 65 33 5f 6d 70 72  that sqlite3_mpr
3aa40 69 6e 74 66 28 29 20 61 6e 64 20 73 71 6c 69 74  intf() and sqlit
3aa50 65 33 5f 66 72 65 65 28 29 20 61 72 65 20 75 73  e3_free() are us
3aa60 65 64 20 6f 6e 20 74 68 65 20 7a 45 72 72 4d 73  ed on the zErrMs
3aa70 67 20 66 69 65 6c 64 0a 2a 2a 20 73 69 6e 63 65  g field.** since
3aa80 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20   virtual tables 
3aa90 61 72 65 20 63 6f 6d 6d 6f 6e 6c 79 20 69 6d 70  are commonly imp
3aaa0 6c 65 6d 65 6e 74 65 64 20 69 6e 20 6c 6f 61 64  lemented in load
3aab0 61 62 6c 65 20 65 78 74 65 6e 73 69 6f 6e 73 20  able extensions 
3aac0 77 68 69 63 68 0a 2a 2a 20 64 6f 20 6e 6f 74 20  which.** do not 
3aad0 68 61 76 65 20 61 63 63 65 73 73 20 74 6f 20 73  have access to s
3aae0 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 29 20  qlite3MPrintf() 
3aaf0 6f 72 20 73 71 6c 69 74 65 33 46 72 65 65 28 29  or sqlite3Free()
3ab00 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69  ..*/.struct sqli
3ab10 74 65 33 5f 76 74 61 62 20 7b 0a 20 20 63 6f 6e  te3_vtab {.  con
3ab20 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
3ab30 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 2f 2a 20  e *pModule;  /* 
3ab40 54 68 65 20 6d 6f 64 75 6c 65 20 66 6f 72 20 74  The module for t
3ab50 68 69 73 20 76 69 72 74 75 61 6c 20 74 61 62 6c  his virtual tabl
3ab60 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b  e */.  int nRef;
3ab70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ab80 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 69         /* Used i
3ab90 6e 74 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 63  nternally */.  c
3aba0 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20  har *zErrMsg;   
3abb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3abc0 2a 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20  * Error message 
3abd0 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 70 72  from sqlite3_mpr
3abe0 69 6e 74 66 28 29 20 2a 2f 0a 20 20 2f 2a 20 56  intf() */.  /* V
3abf0 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70  irtual table imp
3ac00 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c  lementations wil
3ac10 6c 20 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20  l typically add 
3ac20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64  additional field
3ac30 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43  s */.};../*.** C
3ac40 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c  API3REF: Virtual
3ac50 20 54 61 62 6c 65 20 43 75 72 73 6f 72 20 4f 62   Table Cursor Ob
3ac60 6a 65 63 74 20 20 7b 46 31 38 30 32 30 7d 0a 2a  ject  {F18020}.*
3ac70 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69  * KEYWORDS: sqli
3ac80 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 0a  te3_vtab_cursor.
3ac90 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 6d 6f 64 75  **.** Every modu
3aca0 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  le implementatio
3acb0 6e 20 75 73 65 73 20 61 20 73 75 62 63 6c 61 73  n uses a subclas
3acc0 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  s of the followi
3acd0 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  ng structure.** 
3ace0 74 6f 20 64 65 73 63 72 69 62 65 20 63 75 72 73  to describe curs
3acf0 6f 72 73 20 74 68 61 74 20 70 6f 69 6e 74 20 69  ors that point i
3ad00 6e 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 20  nto the virtual 
3ad10 74 61 62 6c 65 20 61 6e 64 20 61 72 65 20 75 73  table and are us
3ad20 65 64 0a 2a 2a 20 74 6f 20 6c 6f 6f 70 20 74 68  ed.** to loop th
3ad30 72 6f 75 67 68 20 74 68 65 20 76 69 72 74 75 61  rough the virtua
3ad40 6c 20 74 61 62 6c 65 2e 20 20 43 75 72 73 6f 72  l table.  Cursor
3ad50 73 20 61 72 65 20 63 72 65 61 74 65 64 20 75 73  s are created us
3ad60 69 6e 67 20 74 68 65 0a 2a 2a 20 78 4f 70 65 6e  ing the.** xOpen
3ad70 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 6d   method of the m
3ad80 6f 64 75 6c 65 2e 20 20 45 61 63 68 20 6d 6f 64  odule.  Each mod
3ad90 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ule implementati
3ada0 6f 6e 20 77 69 6c 6c 20 64 65 66 69 6e 65 0a 2a  on will define.*
3adb0 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  * the content of
3adc0 20 61 20 63 75 72 73 6f 72 20 73 74 72 75 63 74   a cursor struct
3add0 75 72 65 20 74 6f 20 73 75 69 74 20 69 74 73 20  ure to suit its 
3ade0 6f 77 6e 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a  own needs..**.**
3adf0 20 54 68 69 73 20 73 75 70 65 72 63 6c 61 73 73   This superclass
3ae00 20 65 78 69 73 74 73 20 69 6e 20 6f 72 64 65 72   exists in order
3ae10 20 74 6f 20 64 65 66 69 6e 65 20 66 69 65 6c 64   to define field
3ae20 73 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 20  s of the cursor 
3ae30 74 68 61 74 0a 2a 2a 20 61 72 65 20 63 6f 6d 6d  that.** are comm
3ae40 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d 70 6c 65 6d  on to all implem
3ae50 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74  entations..*/.st
3ae60 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76 74 61  ruct sqlite3_vta
3ae70 62 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c  b_cursor {.  sql
3ae80 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
3ae90 3b 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61  ;      /* Virtua
3aea0 6c 20 74 61 62 6c 65 20 6f 66 20 74 68 69 73 20  l table of this 
3aeb0 63 75 72 73 6f 72 20 2a 2f 0a 20 20 2f 2a 20 56  cursor */.  /* V
3aec0 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70  irtual table imp
3aed0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c  lementations wil
3aee0 6c 20 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20  l typically add 
3aef0 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64  additional field
3af00 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43  s */.};../*.** C
3af10 41 50 49 33 52 45 46 3a 20 44 65 63 6c 61 72 65  API3REF: Declare
3af20 20 54 68 65 20 53 63 68 65 6d 61 20 4f 66 20 41   The Schema Of A
3af30 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 7b   Virtual Table {
3af40 46 31 38 32 38 30 7d 0a 2a 2a 0a 2a 2a 20 54 68  F18280}.**.** Th
3af50 65 20 78 43 72 65 61 74 65 20 61 6e 64 20 78 43  e xCreate and xC
3af60 6f 6e 6e 65 63 74 20 6d 65 74 68 6f 64 73 20 6f  onnect methods o
3af70 66 20 61 20 6d 6f 64 75 6c 65 20 75 73 65 20 74  f a module use t
3af80 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 41 50 49  he following API
3af90 0a 2a 2a 20 74 6f 20 64 65 63 6c 61 72 65 20 74  .** to declare t
3afa0 68 65 20 66 6f 72 6d 61 74 20 28 74 68 65 20 6e  he format (the n
3afb0 61 6d 65 73 20 61 6e 64 20 64 61 74 61 74 79 70  ames and datatyp
3afc0 65 73 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  es of the column
3afd0 73 29 20 6f 66 0a 2a 2a 20 74 68 65 20 76 69 72  s) of.** the vir
3afe0 74 75 61 6c 20 74 61 62 6c 65 73 20 74 68 65 79  tual tables they
3aff0 20 69 6d 70 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a 53   implement..*/.S
3b000 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
3b010 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74  lite3_declare_vt
3b020 61 62 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e  ab(sqlite3*, con
3b030 73 74 20 63 68 61 72 20 2a 7a 43 72 65 61 74 65  st char *zCreate
3b040 54 61 62 6c 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  Table);../*.** C
3b050 41 50 49 33 52 45 46 3a 20 4f 76 65 72 6c 6f 61  API3REF: Overloa
3b060 64 20 41 20 46 75 6e 63 74 69 6f 6e 20 46 6f 72  d A Function For
3b070 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65   A Virtual Table
3b080 20 7b 46 31 38 33 30 30 7d 0a 2a 2a 0a 2a 2a 20   {F18300}.**.** 
3b090 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 63  Virtual tables c
3b0a0 61 6e 20 70 72 6f 76 69 64 65 20 61 6c 74 65 72  an provide alter
3b0b0 6e 61 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e 74  native implement
3b0c0 61 74 69 6f 6e 73 20 6f 66 20 66 75 6e 63 74 69  ations of functi
3b0d0 6f 6e 73 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65  ons.** using the
3b0e0 20 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e 20 6d   xFindFunction m
3b0f0 65 74 68 6f 64 2e 20 20 42 75 74 20 67 6c 6f 62  ethod.  But glob
3b100 61 6c 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74  al versions of t
3b110 68 6f 73 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a  hose functions.*
3b120 2a 20 6d 75 73 74 20 65 78 69 73 74 20 69 6e 20  * must exist in 
3b130 6f 72 64 65 72 20 74 6f 20 62 65 20 6f 76 65 72  order to be over
3b140 6c 6f 61 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  loaded..**.** Th
3b150 69 73 20 41 50 49 20 6d 61 6b 65 73 20 73 75 72  is API makes sur
3b160 65 20 61 20 67 6c 6f 62 61 6c 20 76 65 72 73 69  e a global versi
3b170 6f 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e  on of a function
3b180 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c   with a particul
3b190 61 72 0a 2a 2a 20 6e 61 6d 65 20 61 6e 64 20 6e  ar.** name and n
3b1a0 75 6d 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74  umber of paramet
3b1b0 65 72 73 20 65 78 69 73 74 73 2e 20 20 49 66 20  ers exists.  If 
3b1c0 6e 6f 20 73 75 63 68 20 66 75 6e 63 74 69 6f 6e  no such function
3b1d0 20 65 78 69 73 74 73 0a 2a 2a 20 62 65 66 6f 72   exists.** befor
3b1e0 65 20 74 68 69 73 20 41 50 49 20 69 73 20 63 61  e this API is ca
3b1f0 6c 6c 65 64 2c 20 61 20 6e 65 77 20 66 75 6e 63  lled, a new func
3b200 74 69 6f 6e 20 69 73 20 63 72 65 61 74 65 64 2e  tion is created.
3b210 20 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61    The implementa
3b220 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 6e  tion.** of the n
3b230 65 77 20 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61  ew function alwa
3b240 79 73 20 63 61 75 73 65 73 20 61 6e 20 65 78 63  ys causes an exc
3b250 65 70 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 72  eption to be thr
3b260 6f 77 6e 2e 20 20 53 6f 0a 2a 2a 20 74 68 65 20  own.  So.** the 
3b270 6e 65 77 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  new function is 
3b280 6e 6f 74 20 67 6f 6f 64 20 66 6f 72 20 61 6e 79  not good for any
3b290 74 68 69 6e 67 20 62 79 20 69 74 73 65 6c 66 2e  thing by itself.
3b2a0 20 20 49 74 73 20 6f 6e 6c 79 0a 2a 2a 20 70 75    Its only.** pu
3b2b0 72 70 6f 73 65 20 69 73 20 74 6f 20 62 65 20 61  rpose is to be a
3b2c0 20 70 6c 61 63 65 2d 68 6f 6c 64 65 72 20 66 75   place-holder fu
3b2d0 6e 63 74 69 6f 6e 20 74 68 61 74 20 63 61 6e 20  nction that can 
3b2e0 62 65 20 6f 76 65 72 6c 6f 61 64 65 64 0a 2a 2a  be overloaded.**
3b2f0 20 62 79 20 76 69 72 74 75 61 6c 20 74 61 62 6c   by virtual tabl
3b300 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41  es..**.** This A
3b310 50 49 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6e  PI should be con
3b320 73 69 64 65 72 65 64 20 70 61 72 74 20 6f 66 20  sidered part of 
3b330 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
3b340 65 20 69 6e 74 65 72 66 61 63 65 2c 0a 2a 2a 20  e interface,.** 
3b350 77 68 69 63 68 20 69 73 20 65 78 70 65 72 69 6d  which is experim
3b360 65 6e 74 61 6c 20 61 6e 64 20 73 75 62 6a 65 63  ental and subjec
3b370 74 20 74 6f 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a  t to change..*/.
3b380 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3b390 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f  qlite3_overload_
3b3a0 66 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33  function(sqlite3
3b3b0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  *, const char *z
3b3c0 46 75 6e 63 4e 61 6d 65 2c 20 69 6e 74 20 6e 41  FuncName, int nA
3b3d0 72 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  rg);../*.** The 
3b3e0 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 65  interface to the
3b3f0 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d   virtual-table m
3b400 65 63 68 61 6e 69 73 6d 20 64 65 66 69 6e 65 64  echanism defined
3b410 20 61 62 6f 76 65 20 28 62 61 63 6b 20 75 70 0a   above (back up.
3b420 2a 2a 20 74 6f 20 61 20 63 6f 6d 6d 65 6e 74 20  ** to a comment 
3b430 72 65 6d 61 72 6b 61 62 6c 79 20 73 69 6d 69 6c  remarkably simil
3b440 61 72 20 74 6f 20 74 68 69 73 20 6f 6e 65 29 20  ar to this one) 
3b450 69 73 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e  is currently con
3b460 73 69 64 65 72 65 64 0a 2a 2a 20 74 6f 20 62 65  sidered.** to be
3b470 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20 20   experimental.  
3b480 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 6d 69  The interface mi
3b490 67 68 74 20 63 68 61 6e 67 65 20 69 6e 20 69 6e  ght change in in
3b4a0 63 6f 6d 70 61 74 69 62 6c 65 20 77 61 79 73 2e  compatible ways.
3b4b0 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61  .** If this is a
3b4c0 20 70 72 6f 62 6c 65 6d 20 66 6f 72 20 79 6f 75   problem for you
3b4d0 2c 20 64 6f 20 6e 6f 74 20 75 73 65 20 74 68 65  , do not use the
3b4e0 20 69 6e 74 65 72 66 61 63 65 20 61 74 20 74 68   interface at th
3b4f0 69 73 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 57  is time..**.** W
3b500 68 65 6e 20 74 68 65 20 76 69 72 74 75 61 6c 2d  hen the virtual-
3b510 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20  table mechanism 
3b520 73 74 61 62 69 6c 69 7a 65 73 2c 20 77 65 20 77  stabilizes, we w
3b530 69 6c 6c 20 64 65 63 6c 61 72 65 20 74 68 65 0a  ill declare the.
3b540 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 66 69 78  ** interface fix
3b550 65 64 2c 20 73 75 70 70 6f 72 74 20 69 74 20 69  ed, support it i
3b560 6e 64 65 66 69 6e 69 74 65 6c 79 2c 20 61 6e 64  ndefinitely, and
3b570 20 72 65 6d 6f 76 65 20 74 68 69 73 20 63 6f 6d   remove this com
3b580 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 20  ment..**.****** 
3b590 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 2d 20 73  EXPERIMENTAL - s
3b5a0 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65  ubject to change
3b5b0 20 77 69 74 68 6f 75 74 20 6e 6f 74 69 63 65 20   without notice 
3b5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
3b5d0 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  /../*.** CAPI3RE
3b5e0 46 3a 20 41 20 48 61 6e 64 6c 65 20 54 6f 20 41  F: A Handle To A
3b5f0 6e 20 4f 70 65 6e 20 42 4c 4f 42 20 7b 46 31 37  n Open BLOB {F17
3b600 38 30 30 7d 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e  800}.**.** An in
3b610 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f  stance of this o
3b620 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 73  bject represents
3b630 20 61 6e 20 6f 70 65 6e 20 42 4c 4f 42 20 6f 6e   an open BLOB on
3b640 20 77 68 69 63 68 0a 2a 2a 20 69 6e 63 72 65 6d   which.** increm
3b650 65 6e 74 61 6c 20 49 2f 4f 20 63 61 6e 20 62 65  ental I/O can be
3b660 20 70 72 65 66 6f 72 6d 65 64 2e 0a 2a 2a 20 4f   preformed..** O
3b670 62 6a 65 63 74 73 20 6f 66 20 74 68 69 73 20 74  bjects of this t
3b680 79 70 65 20 61 72 65 20 63 72 65 61 74 65 64 20  ype are created 
3b690 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62  by.** [sqlite3_b
3b6a0 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20  lob_open()] and 
3b6b0 64 65 73 74 72 6f 79 65 64 20 62 79 20 5b 73 71  destroyed by [sq
3b6c0 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65  lite3_blob_close
3b6d0 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c  ()]..** The [sql
3b6e0 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29  ite3_blob_read()
3b6f0 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62  ] and [sqlite3_b
3b700 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20 69 6e 74  lob_write()] int
3b710 65 72 66 61 63 65 73 0a 2a 2a 20 63 61 6e 20 62  erfaces.** can b
3b720 65 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 6f  e used to read o
3b730 72 20 77 72 69 74 65 20 73 6d 61 6c 6c 20 73 75  r write small su
3b740 62 73 65 63 74 69 6f 6e 73 20 6f 66 20 74 68 65  bsections of the
3b750 20 62 6c 6f 62 2e 0a 2a 2a 20 54 68 65 20 5b 73   blob..** The [s
3b760 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65  qlite3_blob_byte
3b770 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72  s()] interface r
3b780 65 74 75 72 6e 73 20 74 68 65 20 73 69 7a 65 20  eturns the size 
3b790 6f 66 20 74 68 65 0a 2a 2a 20 62 6c 6f 62 20 69  of the.** blob i
3b7a0 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 74 79 70 65  n bytes..*/.type
3b7b0 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
3b7c0 65 33 5f 62 6c 6f 62 20 73 71 6c 69 74 65 33 5f  e3_blob sqlite3_
3b7d0 62 6c 6f 62 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  blob;../*.** CAP
3b7e0 49 33 52 45 46 3a 20 4f 70 65 6e 20 41 20 42 4c  I3REF: Open A BL
3b7f0 4f 42 20 46 6f 72 20 49 6e 63 72 65 6d 65 6e 74  OB For Increment
3b800 61 6c 20 49 2f 4f 20 7b 46 31 37 38 31 30 7d 0a  al I/O {F17810}.
3b810 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72  **.** This inter
3b820 66 61 63 65 73 20 6f 70 65 6e 73 20 61 20 68 61  faces opens a ha
3b830 6e 64 6c 65 20 74 6f 20 74 68 65 20 62 6c 6f 62  ndle to the blob
3b840 20 6c 6f 63 61 74 65 64 0a 2a 2a 20 69 6e 20 72   located.** in r
3b850 6f 77 20 69 52 6f 77 2c 20 63 6f 6c 75 6d 6e 20  ow iRow, column 
3b860 7a 43 6f 6c 75 6d 6e 2c 20 74 61 62 6c 65 20 7a  zColumn, table z
3b870 54 61 62 6c 65 20 69 6e 20 64 61 74 61 62 61 73  Table in databas
3b880 65 20 7a 44 62 3b 0a 2a 2a 20 69 6e 20 6f 74 68  e zDb;.** in oth
3b890 65 72 20 77 6f 72 64 73 2c 20 20 74 68 65 20 73  er words,  the s
3b8a0 61 6d 65 20 62 6c 6f 62 20 74 68 61 74 20 77 6f  ame blob that wo
3b8b0 75 6c 64 20 62 65 20 73 65 6c 65 63 74 65 64 20  uld be selected 
3b8c0 62 79 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 3e 0a  by:.**.** <pre>.
3b8d0 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 7a 43  **     SELECT zC
3b8e0 6f 6c 75 6d 6e 20 46 52 4f 4d 20 7a 44 62 2e 7a  olumn FROM zDb.z
3b8f0 54 61 62 6c 65 20 57 48 45 52 45 20 72 6f 77 69  Table WHERE rowi
3b900 64 20 3d 20 69 52 6f 77 3b 0a 2a 2a 20 3c 2f 70  d = iRow;.** </p
3b910 72 65 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20  re> {END}.**.** 
3b920 49 66 20 74 68 65 20 66 6c 61 67 73 20 70 61 72  If the flags par
3b930 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65  ameter is non-ze
3b940 72 6f 2c 20 74 68 65 20 62 6c 6f 62 20 69 73 20  ro, the blob is 
3b950 6f 70 65 6e 65 64 20 66 6f 72 20 0a 2a 2a 20 72  opened for .** r
3b960 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 61 63  ead and write ac
3b970 63 65 73 73 2e 20 49 66 20 69 74 20 69 73 20 7a  cess. If it is z
3b980 65 72 6f 2c 20 74 68 65 20 62 6c 6f 62 20 69 73  ero, the blob is
3b990 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64   opened for read
3b9a0 20 0a 2a 2a 20 61 63 63 65 73 73 2e 0a 2a 2a 0a   .** access..**.
3b9b0 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  ** Note that the
3b9c0 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69   database name i
3b9d0 73 20 6e 6f 74 20 74 68 65 20 66 69 6c 65 6e 61  s not the filena
3b9e0 6d 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  me that contains
3b9f0 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
3ba00 20 62 75 74 20 72 61 74 68 65 72 20 74 68 65 20   but rather the 
3ba10 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 6f 66  symbolic name of
3ba20 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68   the database th
3ba30 61 74 0a 2a 2a 20 69 73 20 61 73 73 69 67 6e 65  at.** is assigne
3ba40 64 20 77 68 65 6e 20 74 68 65 20 64 61 74 61 62  d when the datab
3ba50 61 73 65 20 69 73 20 63 6f 6e 6e 65 63 74 65 64  ase is connected
3ba60 20 75 73 69 6e 67 20 5b 41 54 54 41 43 48 5d 2e   using [ATTACH].
3ba70 0a 2a 2a 20 46 6f 72 20 74 68 65 20 6d 61 69 6e  .** For the main
3ba80 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
3ba90 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d  the database nam
3baa0 65 20 69 73 20 22 6d 61 69 6e 22 2e 20 20 46 6f  e is "main".  Fo
3bab0 72 0a 2a 2a 20 54 45 4d 50 20 74 61 62 6c 65 73  r.** TEMP tables
3bac0 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e  , the database n
3bad0 61 6d 65 20 69 73 20 22 74 65 6d 70 22 2e 0a 2a  ame is "temp"..*
3bae0 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c  *.** On success,
3baf0 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 73 20   [SQLITE_OK] is 
3bb00 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65  returned and the
3bb10 20 6e 65 77 20 0a 2a 2a 20 5b 73 71 6c 69 74 65   new .** [sqlite
3bb20 33 5f 62 6c 6f 62 20 7c 20 62 6c 6f 62 20 68 61  3_blob | blob ha
3bb30 6e 64 6c 65 5d 20 69 73 20 77 72 69 74 74 65 6e  ndle] is written
3bb40 20 74 6f 20 2a 70 70 42 6c 6f 62 2e 20 0a 2a 2a   to *ppBlob. .**
3bb50 20 4f 74 68 65 72 77 69 73 65 20 61 6e 20 65 72   Otherwise an er
3bb60 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
3bb70 72 6e 65 64 20 61 6e 64 20 0a 2a 2a 20 61 6e 79  rned and .** any
3bb80 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 74   value written t
3bb90 6f 20 2a 70 70 42 6c 6f 62 20 73 68 6f 75 6c 64  o *ppBlob should
3bba0 20 6e 6f 74 20 62 65 20 75 73 65 64 20 62 79 20   not be used by 
3bbb0 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a 20 54  the caller..** T
3bbc0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 74  his function set
3bbd0 73 20 74 68 65 20 64 61 74 61 62 61 73 65 2d 68  s the database-h
3bbe0 61 6e 64 6c 65 20 65 72 72 6f 72 20 63 6f 64 65  andle error code
3bbf0 20 61 6e 64 20 6d 65 73 73 61 67 65 0a 2a 2a 20   and message.** 
3bc00 61 63 63 65 73 73 69 62 6c 65 20 76 69 61 20 5b  accessible via [
3bc10 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28  sqlite3_errcode(
3bc20 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
3bc30 65 72 72 6d 73 67 28 29 5d 2e 0a 2a 2a 20 0a 2a  errmsg()]..** .*
3bc40 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a  * INVARIANTS:.**
3bc50 0a 2a 2a 20 7b 46 31 37 38 31 33 7d 20 41 20 73  .** {F17813} A s
3bc60 75 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61  uccessful invoca
3bc70 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c  tion of the [sql
3bc80 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 44  ite3_blob_open(D
3bc90 2c 42 2c 54 2c 43 2c 52 2c 46 2c 50 29 5d 0a 2a  ,B,T,C,R,F,P)].*
3bca0 2a 20 20 20 20 20 20 20 20 20 20 69 6e 74 65 72  *          inter
3bcb0 66 61 63 65 20 6f 70 65 6e 73 20 61 6e 20 5b 73  face opens an [s
3bcc0 71 6c 69 74 65 33 5f 62 6c 6f 62 5d 20 6f 62 6a  qlite3_blob] obj
3bcd0 65 63 74 20 50 20 6f 6e 20 74 68 65 20 62 6c 6f  ect P on the blo
3bce0 62 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e  b.**          in
3bcf0 20 63 6f 6c 75 6d 6e 20 43 20 6f 66 20 74 61 62   column C of tab
3bd00 6c 65 20 54 20 69 6e 20 64 61 74 61 62 61 73 65  le T in database
3bd10 20 42 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 20   B on [database 
3bd20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 2e 0a 2a  connection] D..*
3bd30 2a 0a 2a 2a 20 7b 46 31 37 38 31 34 7d 20 41 20  *.** {F17814} A 
3bd40 73 75 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63  successful invoc
3bd50 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65  ation of [sqlite
3bd60 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 44 2c 2e 2e  3_blob_open(D,..
3bd70 2e 29 5d 20 73 74 61 72 74 73 0a 2a 2a 20 20 20  .)] starts.**   
3bd80 20 20 20 20 20 20 20 61 20 6e 65 77 20 74 72 61         a new tra
3bd90 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 5b 64 61 74  nsaction on [dat
3bda0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
3bdb0 5d 20 44 20 69 66 20 74 68 61 74 20 63 6f 6e 6e  ] D if that conn
3bdc0 65 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20  ection.**       
3bdd0 20 20 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64     is not alread
3bde0 79 20 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69  y in a transacti
3bdf0 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 37 38 31  on..**.** {F1781
3be00 36 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  6} The [sqlite3_
3be10 62 6c 6f 62 5f 6f 70 65 6e 28 44 2c 42 2c 54 2c  blob_open(D,B,T,
3be20 43 2c 52 2c 46 2c 50 29 5d 20 69 6e 74 65 72 66  C,R,F,P)] interf
3be30 61 63 65 20 6f 70 65 6e 73 20 74 68 65 20 62 6c  ace opens the bl
3be40 6f 62 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66  ob.**          f
3be50 6f 72 20 72 65 61 64 20 61 6e 64 20 77 72 69 74  or read and writ
3be60 65 20 61 63 63 65 73 73 20 69 66 20 61 6e 64 20  e access if and 
3be70 6f 6e 6c 79 20 69 66 20 74 68 65 20 46 20 70 61  only if the F pa
3be80 72 61 6d 65 74 65 72 0a 2a 2a 20 20 20 20 20 20  rameter.**      
3be90 20 20 20 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e      is non-zero.
3bea0 0a 2a 2a 0a 2a 2a 20 7b 46 31 37 38 31 39 7d 20  .**.** {F17819} 
3beb0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f  The [sqlite3_blo
3bec0 62 5f 6f 70 65 6e 28 29 5d 20 69 6e 74 65 72 66  b_open()] interf
3bed0 61 63 65 20 72 65 74 75 72 6e 73 20 5b 53 51 4c  ace returns [SQL
3bee0 49 54 45 5f 4f 4b 5d 20 6f 6e 20 0a 2a 2a 20 20  ITE_OK] on .**  
3bef0 20 20 20 20 20 20 20 20 73 75 63 63 65 73 73 20          success 
3bf00 61 6e 64 20 61 6e 20 61 70 70 72 6f 70 72 69 61  and an appropria
3bf10 74 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20  te [error code] 
3bf20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a  on failure..**.*
3bf30 2a 20 7b 46 31 37 38 32 31 7d 20 49 66 20 61 6e  * {F17821} If an
3bf40 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75   error occurs du
3bf50 72 69 6e 67 20 65 76 61 6c 75 61 74 69 6f 6e 20  ring evaluation 
3bf60 6f 66 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  of [sqlite3_blob
3bf70 5f 6f 70 65 6e 28 44 2c 2e 2e 2e 29 5d 0a 2a 2a  _open(D,...)].**
3bf80 20 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 73            then s
3bf90 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20  ubsequent calls 
3bfa0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63  to [sqlite3_errc
3bfb0 6f 64 65 28 44 29 5d 2c 0a 2a 2a 20 20 20 20 20  ode(D)],.**     
3bfc0 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 72       [sqlite3_er
3bfd0 72 6d 73 67 28 44 29 5d 2c 20 61 6e 64 20 5b 73  rmsg(D)], and [s
3bfe0 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28  qlite3_errmsg16(
3bff0 44 29 5d 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a  D)] will return.
3c000 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e 66 6f  **          info
3c010 72 6d 61 74 69 6f 6e 20 61 70 70 72 6f 70 72 61  rmation appropra
3c020 74 65 20 66 6f 72 20 74 68 61 74 20 65 72 72 6f  te for that erro
3c030 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  r..*/.SQLITE_API
3c040 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f   int sqlite3_blo
3c050 62 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65  b_open(.  sqlite
3c060 33 2a 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  3*,.  const char
3c070 20 2a 7a 44 62 2c 0a 20 20 63 6f 6e 73 74 20 63   *zDb,.  const c
3c080 68 61 72 20 2a 7a 54 61 62 6c 65 2c 0a 20 20 63  har *zTable,.  c
3c090 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75  onst char *zColu
3c0a0 6d 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  mn,.  sqlite3_in
3c0b0 74 36 34 20 69 52 6f 77 2c 0a 20 20 69 6e 74 20  t64 iRow,.  int 
3c0c0 66 6c 61 67 73 2c 0a 20 20 73 71 6c 69 74 65 33  flags,.  sqlite3
3c0d0 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f 62 0a 29  _blob **ppBlob.)
3c0e0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
3c0f0 46 3a 20 20 43 6c 6f 73 65 20 41 20 42 4c 4f 42  F:  Close A BLOB
3c100 20 48 61 6e 64 6c 65 20 7b 46 31 37 38 33 30 7d   Handle {F17830}
3c110 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e 20  .**.** Close an 
3c120 6f 70 65 6e 20 5b 73 71 6c 69 74 65 33 5f 62 6c  open [sqlite3_bl
3c130 6f 62 20 7c 20 62 6c 6f 62 20 68 61 6e 64 6c 65  ob | blob handle
3c140 5d 2e 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67  ]..**.** Closing
3c150 20 61 20 42 4c 4f 42 20 73 68 61 6c 6c 20 63 61   a BLOB shall ca
3c160 75 73 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  use the current 
3c170 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 63  transaction to c
3c180 6f 6d 6d 69 74 0a 2a 2a 20 69 66 20 74 68 65 72  ommit.** if ther
3c190 65 20 61 72 65 20 6e 6f 20 6f 74 68 65 72 20 42  e are no other B
3c1a0 4c 4f 42 73 2c 20 6e 6f 20 70 65 6e 64 69 6e 67  LOBs, no pending
3c1b0 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
3c1c0 65 6e 74 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a  ents, and the.**
3c1d0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
3c1e0 74 69 6f 6e 20 69 73 20 69 6e 20 61 75 74 6f 63  tion is in autoc
3c1f0 6f 6d 6d 69 74 20 6d 6f 64 65 2e 0a 2a 2a 20 49  ommit mode..** I
3c200 66 20 61 6e 79 20 77 72 69 74 65 73 20 77 65 72  f any writes wer
3c210 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20 42 4c  e made to the BL
3c220 4f 42 2c 20 74 68 65 79 20 6d 69 67 68 74 20 62  OB, they might b
3c230 65 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 0a  e held in cache.
3c240 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 63 6c 6f  ** until the clo
3c250 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 66 20  se operation if 
3c260 74 68 65 79 20 77 69 6c 6c 20 66 69 74 2e 20 7b  they will fit. {
3c270 45 4e 44 7d 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20  END}.** Closing 
3c280 74 68 65 20 42 4c 4f 42 20 6f 66 74 65 6e 20 66  the BLOB often f
3c290 6f 72 63 65 73 20 74 68 65 20 63 68 61 6e 67 65  orces the change
3c2a0 73 0a 2a 2a 20 6f 75 74 20 74 6f 20 64 69 73 6b  s.** out to disk
3c2b0 20 61 6e 64 20 73 6f 20 69 66 20 61 6e 79 20 49   and so if any I
3c2c0 2f 4f 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c  /O errors occur,
3c2d0 20 74 68 65 79 20 77 69 6c 6c 20 6c 69 6b 65 6c   they will likel
3c2e0 79 20 6f 63 63 75 72 0a 2a 2a 20 61 74 20 74 68  y occur.** at th
3c2f0 65 20 74 69 6d 65 20 77 68 65 6e 20 74 68 65 20  e time when the 
3c300 42 4c 4f 42 20 69 73 20 63 6c 6f 73 65 64 2e 20  BLOB is closed. 
3c310 20 7b 46 31 37 38 33 33 7d 20 41 6e 79 20 65 72   {F17833} Any er
3c320 72 6f 72 73 20 74 68 61 74 20 6f 63 63 75 72 20  rors that occur 
3c330 64 75 72 69 6e 67 0a 2a 2a 20 63 6c 6f 73 69 6e  during.** closin
3c340 67 20 61 72 65 20 72 65 70 6f 72 74 65 64 20 61  g are reported a
3c350 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 72 65 74  s a non-zero ret
3c360 75 72 6e 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a  urn value..**.**
3c370 20 54 68 65 20 42 4c 4f 42 20 69 73 20 63 6c 6f   The BLOB is clo
3c380 73 65 64 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61  sed unconditiona
3c390 6c 6c 79 2e 20 20 45 76 65 6e 20 69 66 20 74 68  lly.  Even if th
3c3a0 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
3c3b0 6e 73 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 63  ns.** an error c
3c3c0 6f 64 65 2c 20 74 68 65 20 42 4c 4f 42 20 69 73  ode, the BLOB is
3c3d0 20 73 74 69 6c 6c 20 63 6c 6f 73 65 64 2e 0a 2a   still closed..*
3c3e0 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
3c3f0 0a 2a 2a 0a 2a 2a 20 7b 46 31 37 38 33 33 7d 20  .**.** {F17833} 
3c400 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f  The [sqlite3_blo
3c410 62 5f 63 6c 6f 73 65 28 50 29 5d 20 69 6e 74 65  b_close(P)] inte
3c420 72 66 61 63 65 20 63 6c 6f 73 65 73 20 61 6e 0a  rface closes an.
3c430 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
3c440 69 74 65 33 5f 62 6c 6f 62 5d 20 6f 62 6a 65 63  ite3_blob] objec
3c450 74 20 50 20 70 72 65 76 69 6f 75 73 6c 79 20 6f  t P previously o
3c460 70 65 6e 65 64 20 75 73 69 6e 67 0a 2a 2a 20 20  pened using.**  
3c470 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33          [sqlite3
3c480 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 2e 0a 2a  _blob_open()]..*
3c490 2a 0a 2a 2a 20 7b 46 31 37 38 33 36 7d 20 43 6c  *.** {F17836} Cl
3c4a0 6f 73 69 6e 67 20 61 6e 20 5b 73 71 6c 69 74 65  osing an [sqlite
3c4b0 33 5f 62 6c 6f 62 5d 20 6f 62 6a 65 63 74 20 75  3_blob] object u
3c4c0 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20  sing.**         
3c4d0 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63   [sqlite3_blob_c
3c4e0 6c 6f 73 65 28 29 5d 20 73 68 61 6c 6c 20 63 61  lose()] shall ca
3c4f0 75 73 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  use the current 
3c500 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 0a 2a  transaction to.*
3c510 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6d 6d 69  *          commi
3c520 74 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6e  t if there are n
3c530 6f 20 6f 74 68 65 72 20 6f 70 65 6e 20 5b 73 71  o other open [sq
3c540 6c 69 74 65 33 5f 62 6c 6f 62 5d 20 6f 62 6a 65  lite3_blob] obje
3c550 63 74 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  cts.**          
3c560 6f 72 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  or [prepared sta
3c570 74 65 6d 65 6e 74 73 5d 20 6f 6e 20 74 68 65 20  tements] on the 
3c580 73 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63  same [database c
3c590 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64 0a 2a  onnection] and.*
3c5a0 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 5b  *          the [
3c5b0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3c5c0 69 6f 6e 5d 20 69 73 20 69 6e 0a 2a 2a 20 20 20  ion] is in.**   
3c5d0 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
3c5e0 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 20 7c  get_autocommit |
3c5f0 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65   autocommit mode
3c600 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 37 38 33 39  ]..**.** {F17839
3c610 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62  } The [sqlite3_b
3c620 6c 6f 62 5f 63 6c 6f 73 65 28 50 29 5d 20 69 6e  lob_close(P)] in
3c630 74 65 72 66 61 63 65 73 20 63 6c 6f 73 65 73 20  terfaces closes 
3c640 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 20 20 20  the .**         
3c650 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5d 20   [sqlite3_blob] 
3c660 6f 62 6a 65 63 74 20 50 20 75 6e 63 6f 6e 64 69  object P uncondi
3c670 74 69 6f 6e 61 6c 6c 79 2c 20 65 76 65 6e 20 69  tionally, even i
3c680 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73  f.**          [s
3c690 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73  qlite3_blob_clos
3c6a0 65 28 50 29 5d 20 72 65 74 75 72 6e 73 20 73 6f  e(P)] returns so
3c6b0 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68  mething other th
3c6c0 61 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a  an [SQLITE_OK]..
3c6d0 2a 2a 20 20 20 20 20 20 20 20 20 20 0a 2a 2f 0a  **          .*/.
3c6e0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3c6f0 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73  qlite3_blob_clos
3c700 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a  e(sqlite3_blob *
3c710 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
3c720 45 46 3a 20 20 52 65 74 75 72 6e 20 54 68 65 20  EF:  Return The 
3c730 53 69 7a 65 20 4f 66 20 41 6e 20 4f 70 65 6e 20  Size Of An Open 
3c740 42 4c 4f 42 20 7b 46 31 37 38 34 30 7d 0a 2a 2a  BLOB {F17840}.**
3c750 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73  .** Return the s
3c760 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20  ize in bytes of 
3c770 74 68 65 20 62 6c 6f 62 20 61 63 63 65 73 73 69  the blob accessi
3c780 62 6c 65 20 76 69 61 20 74 68 65 20 6f 70 65 6e  ble via the open
3c790 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 6c   .** [sqlite3_bl
3c7a0 6f 62 5d 20 6f 62 6a 65 63 74 20 69 6e 20 69 74  ob] object in it
3c7b0 73 20 6f 6e 6c 79 20 61 72 67 75 6d 65 6e 74 2e  s only argument.
3c7c0 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54  .**.** INVARIANT
3c7d0 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31 37 38 34 33  S:.**.** {F17843
3c7e0 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62  } The [sqlite3_b
3c7f0 6c 6f 62 5f 62 79 74 65 73 28 50 29 5d 20 69 6e  lob_bytes(P)] in
3c800 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
3c810 74 68 65 20 73 69 7a 65 0a 2a 2a 20 20 20 20 20  the size.**     
3c820 20 20 20 20 20 69 6e 20 62 79 74 65 73 20 6f 66       in bytes of
3c830 20 74 68 65 20 42 4c 4f 42 20 74 68 61 74 20 74   the BLOB that t
3c840 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  he [sqlite3_blob
3c850 5d 20 6f 62 6a 65 63 74 20 50 0a 2a 2a 20 20 20  ] object P.**   
3c860 20 20 20 20 20 20 20 72 65 66 65 72 73 20 74 6f         refers to
3c870 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
3c880 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  int sqlite3_blob
3c890 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 62  _bytes(sqlite3_b
3c8a0 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  lob *);../*.** C
3c8b0 41 50 49 33 52 45 46 3a 20 20 52 65 61 64 20 44  API3REF:  Read D
3c8c0 61 74 61 20 46 72 6f 6d 20 41 20 42 4c 4f 42 20  ata From A BLOB 
3c8d0 49 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 7b 46  Incrementally {F
3c8e0 31 37 38 35 30 7d 0a 2a 2a 0a 2a 2a 20 54 68 69  17850}.**.** Thi
3c8f0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
3c900 65 64 20 74 6f 20 72 65 61 64 20 64 61 74 61 20  ed to read data 
3c910 66 72 6f 6d 20 61 6e 20 6f 70 65 6e 20 0a 2a 2a  from an open .**
3c920 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 7c   [sqlite3_blob |
3c930 20 62 6c 6f 62 2d 68 61 6e 64 6c 65 5d 20 69 6e   blob-handle] in
3c940 74 6f 20 61 20 63 61 6c 6c 65 72 20 73 75 70 70  to a caller supp
3c950 6c 69 65 64 20 62 75 66 66 65 72 2e 0a 2a 2a 20  lied buffer..** 
3c960 4e 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20  N bytes of data 
3c970 61 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20  are copied into 
3c980 62 75 66 66 65 72 0a 2a 2a 20 5a 20 66 72 6f 6d  buffer.** Z from
3c990 20 74 68 65 20 6f 70 65 6e 20 62 6c 6f 62 2c 20   the open blob, 
3c9a0 73 74 61 72 74 69 6e 67 20 61 74 20 6f 66 66 73  starting at offs
3c9b0 65 74 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a  et iOffset..**.*
3c9c0 2a 20 49 66 20 6f 66 66 73 65 74 20 69 4f 66 66  * If offset iOff
3c9d0 73 65 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e  set is less than
3c9e0 20 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68   N bytes from th
3c9f0 65 20 65 6e 64 20 6f 66 20 74 68 65 20 62 6c 6f  e end of the blo
3ca00 62 2c 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45  b, .** [SQLITE_E
3ca10 52 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65  RROR] is returne
3ca20 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73  d and no data is
3ca30 20 72 65 61 64 2e 20 20 49 66 20 4e 20 6f 72 20   read.  If N or 
3ca40 69 4f 66 66 73 65 74 20 69 73 0a 2a 2a 20 6c 65  iOffset is.** le
3ca50 73 73 20 74 68 61 6e 20 7a 65 72 6f 20 5b 53 51  ss than zero [SQ
3ca60 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 72  LITE_ERROR] is r
3ca70 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64  eturned and no d
3ca80 61 74 61 20 69 73 20 72 65 61 64 2e 0a 2a 2a 0a  ata is read..**.
3ca90 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 53  ** On success, S
3caa0 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
3cab0 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c  rned. Otherwise,
3cac0 20 61 6e 20 0a 2a 2a 20 5b 65 72 72 6f 72 20 63   an .** [error c
3cad0 6f 64 65 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65  ode] or an [exte
3cae0 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d  nded error code]
3caf0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
3cb00 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a  .** INVARIANTS:.
3cb10 2a 2a 0a 2a 2a 20 7b 46 31 37 38 35 33 7d 20 54  **.** {F17853} T
3cb20 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  he [sqlite3_blob
3cb30 5f 72 65 61 64 28 50 2c 5a 2c 4e 2c 58 29 5d 20  _read(P,Z,N,X)] 
3cb40 69 6e 74 65 72 66 61 63 65 20 72 65 61 64 73 20  interface reads 
3cb50 4e 20 62 79 74 65 73 0a 2a 2a 20 20 20 20 20 20  N bytes.**      
3cb60 20 20 20 20 62 65 67 69 6e 6e 69 6e 67 20 61 74      beginning at
3cb70 20 6f 66 66 73 65 74 20 58 20 66 72 6f 6d 0a 2a   offset X from.*
3cb80 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 62  *          the b
3cb90 6c 6f 62 20 74 68 61 74 20 5b 73 71 6c 69 74 65  lob that [sqlite
3cba0 33 5f 62 6c 6f 62 5d 20 6f 62 6a 65 63 74 20 50  3_blob] object P
3cbb0 20 72 65 66 65 72 73 20 74 6f 0a 2a 2a 20 20 20   refers to.**   
3cbc0 20 20 20 20 20 20 20 61 6e 64 20 77 72 69 74 65         and write
3cbd0 73 20 74 68 6f 73 65 20 4e 20 62 79 74 65 73 20  s those N bytes 
3cbe0 69 6e 74 6f 20 62 75 66 66 65 72 20 5a 2e 0a 2a  into buffer Z..*
3cbf0 2a 0a 2a 2a 20 7b 46 31 37 38 35 36 7d 20 49 6e  *.** {F17856} In
3cc00 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72   [sqlite3_blob_r
3cc10 65 61 64 28 50 2c 5a 2c 4e 2c 58 29 5d 20 69 66  ead(P,Z,N,X)] if
3cc20 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
3cc30 20 62 6c 6f 62 0a 2a 2a 20 20 20 20 20 20 20 20   blob.**        
3cc40 20 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 4e    is less than N
3cc50 2b 58 20 62 79 74 65 73 2c 20 74 68 65 6e 20 74  +X bytes, then t
3cc60 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  he function retu
3cc70 72 6e 73 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f  rns [SQLITE_ERRO
3cc80 52 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61  R].**          a
3cc90 6e 64 20 6e 6f 74 68 69 6e 67 20 69 73 20 72 65  nd nothing is re
3cca0 61 64 20 66 72 6f 6d 20 74 68 65 20 62 6c 6f 62  ad from the blob
3ccb0 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 37 38 35 39 7d  ..**.** {F17859}
3ccc0 20 49 6e 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f   In [sqlite3_blo
3ccd0 62 5f 72 65 61 64 28 50 2c 5a 2c 4e 2c 58 29 5d  b_read(P,Z,N,X)]
3cce0 20 69 66 20 58 20 6f 72 20 4e 20 69 73 20 6c 65   if X or N is le
3ccf0 73 73 20 74 68 61 6e 20 7a 65 72 6f 0a 2a 2a 20  ss than zero.** 
3cd00 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 74 68           then th
3cd10 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  e function retur
3cd20 6e 73 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52  ns [SQLITE_ERROR
3cd30 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e  ].**          an
3cd40 64 20 6e 6f 74 68 69 6e 67 20 69 73 20 72 65 61  d nothing is rea
3cd50 64 20 66 72 6f 6d 20 74 68 65 20 62 6c 6f 62 2e  d from the blob.
3cd60 0a 2a 2a 0a 2a 2a 20 7b 46 31 37 38 36 32 7d 20  .**.** {F17862} 
3cd70 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f  The [sqlite3_blo
3cd80 62 5f 72 65 61 64 28 50 2c 5a 2c 4e 2c 58 29 5d  b_read(P,Z,N,X)]
3cd90 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72   interface retur
3cda0 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 0a 2a  ns [SQLITE_OK].*
3cdb0 2a 20 20 20 20 20 20 20 20 20 20 69 66 20 4e 20  *          if N 
3cdc0 62 79 74 65 73 20 77 68 65 72 65 20 73 75 63 63  bytes where succ
3cdd0 65 73 73 66 75 6c 6c 79 20 72 65 61 64 20 69 6e  essfully read in
3cde0 74 6f 20 62 75 66 66 65 72 20 5a 2e 0a 2a 2a 0a  to buffer Z..**.
3cdf0 2a 2a 20 7b 46 31 37 38 36 35 7d 20 49 66 20 74  ** {F17865} If t
3ce00 68 65 20 72 65 71 75 65 73 74 65 64 20 72 65 61  he requested rea
3ce10 64 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63  d could not be c
3ce20 6f 6d 70 6c 65 74 65 64 2c 0a 2a 2a 20 20 20 20  ompleted,.**    
3ce30 20 20 20 20 20 20 74 68 65 20 5b 73 71 6c 69 74        the [sqlit
3ce40 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 50 2c 5a  e3_blob_read(P,Z
3ce50 2c 4e 2c 58 29 5d 20 69 6e 74 65 72 66 61 63 65  ,N,X)] interface
3ce60 20 72 65 74 75 72 6e 73 20 61 6e 0a 2a 2a 20 20   returns an.**  
3ce70 20 20 20 20 20 20 20 20 61 70 70 72 6f 70 72 69          appropri
3ce80 61 74 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d  ate [error code]
3ce90 20 6f 72 20 5b 65 78 74 65 6e 64 65 64 20 65 72   or [extended er
3cea0 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a  ror code]..**.**
3ceb0 20 7b 46 31 37 38 36 38 7d 20 49 66 20 61 6e 20   {F17868} If an 
3cec0 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72  error occurs dur
3ced0 69 6e 67 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f  ing evaluation o
3cee0 66 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  f [sqlite3_blob_
3cef0 72 65 61 64 28 50 2c 2e 2e 2e 29 5d 0a 2a 2a 20  read(P,...)].** 
3cf00 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 73 75           then su
3cf10 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74  bsequent calls t
3cf20 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  o [sqlite3_errco
3cf30 64 65 28 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 20  de(D)],.**      
3cf40 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 72 72      [sqlite3_err
3cf50 6d 73 67 28 44 29 5d 2c 20 61 6e 64 20 5b 73 71  msg(D)], and [sq
3cf60 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 44  lite3_errmsg16(D
3cf70 29 5d 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a  )] will return.*
3cf80 2a 20 20 20 20 20 20 20 20 20 20 69 6e 66 6f 72  *          infor
3cf90 6d 61 74 69 6f 6e 20 61 70 70 72 6f 70 72 61 74  mation approprat
3cfa0 65 20 66 6f 72 20 74 68 61 74 20 65 72 72 6f 72  e for that error
3cfb0 2c 20 77 68 65 72 65 20 44 20 69 73 20 74 68 65  , where D is the
3cfc0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 64 61 74  .**          dat
3cfd0 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 68 61  abase handle tha
3cfe0 74 20 77 61 73 20 75 73 65 64 20 74 6f 20 6f 70  t was used to op
3cff0 65 6e 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20 50  en blob handle P
3d000 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
3d010 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  int sqlite3_blob
3d020 5f 72 65 61 64 28 73 71 6c 69 74 65 33 5f 62 6c  _read(sqlite3_bl
3d030 6f 62 20 2a 2c 20 76 6f 69 64 20 2a 5a 2c 20 69  ob *, void *Z, i
3d040 6e 74 20 4e 2c 20 69 6e 74 20 69 4f 66 66 73 65  nt N, int iOffse
3d050 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  t);../*.** CAPI3
3d060 52 45 46 3a 20 20 57 72 69 74 65 20 44 61 74 61  REF:  Write Data
3d070 20 49 6e 74 6f 20 41 20 42 4c 4f 42 20 49 6e 63   Into A BLOB Inc
3d080 72 65 6d 65 6e 74 61 6c 6c 79 20 7b 46 31 37 38  rementally {F178
3d090 37 30 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  70}.**.** This f
3d0a0 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
3d0b0 74 6f 20 77 72 69 74 65 20 64 61 74 61 20 69 6e  to write data in
3d0c0 74 6f 20 61 6e 20 6f 70 65 6e 20 0a 2a 2a 20 5b  to an open .** [
3d0d0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 7c 20 62  sqlite3_blob | b
3d0e0 6c 6f 62 2d 68 61 6e 64 6c 65 5d 20 66 72 6f 6d  lob-handle] from
3d0f0 20 61 20 75 73 65 72 20 73 75 70 70 6c 69 65 64   a user supplied
3d100 20 62 75 66 66 65 72 2e 0a 2a 2a 20 6e 20 62 79   buffer..** n by
3d110 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 20  tes of data are 
3d120 63 6f 70 69 65 64 20 66 72 6f 6d 20 74 68 65 20  copied from the 
3d130 62 75 66 66 65 72 0a 2a 2a 20 70 6f 69 6e 74 65  buffer.** pointe
3d140 64 20 74 6f 20 62 79 20 7a 20 69 6e 74 6f 20 74  d to by z into t
3d150 68 65 20 6f 70 65 6e 20 62 6c 6f 62 2c 20 73 74  he open blob, st
3d160 61 72 74 69 6e 67 20 61 74 20 6f 66 66 73 65 74  arting at offset
3d170 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20   iOffset..**.** 
3d180 49 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  If the [sqlite3_
3d190 62 6c 6f 62 20 7c 20 62 6c 6f 62 2d 68 61 6e 64  blob | blob-hand
3d1a0 6c 65 5d 20 70 61 73 73 65 64 20 61 73 20 74 68  le] passed as th
3d1b0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
3d1c0 0a 2a 2a 20 77 61 73 20 6e 6f 74 20 6f 70 65 6e  .** was not open
3d1d0 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67 20 28  ed for writing (
3d1e0 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65  the flags parame
3d1f0 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  ter to [sqlite3_
3d200 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 2a  blob_open()].***
3d210 20 77 61 73 20 7a 65 72 6f 29 2c 20 74 68 69 73   was zero), this
3d220 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
3d230 73 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e  s [SQLITE_READON
3d240 4c 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  LY]..**.** This 
3d250 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c  function may onl
3d260 79 20 6d 6f 64 69 66 79 20 74 68 65 20 63 6f 6e  y modify the con
3d270 74 65 6e 74 73 20 6f 66 20 74 68 65 20 62 6c 6f  tents of the blo
3d280 62 3b 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20  b; it is.** not 
3d290 70 6f 73 73 69 62 6c 65 20 74 6f 20 69 6e 63 72  possible to incr
3d2a0 65 61 73 65 20 74 68 65 20 73 69 7a 65 20 6f 66  ease the size of
3d2b0 20 61 20 62 6c 6f 62 20 75 73 69 6e 67 20 74 68   a blob using th
3d2c0 69 73 20 41 50 49 2e 0a 2a 2a 20 49 66 20 6f 66  is API..** If of
3d2d0 66 73 65 74 20 69 4f 66 66 73 65 74 20 69 73 20  fset iOffset is 
3d2e0 6c 65 73 73 20 74 68 61 6e 20 6e 20 62 79 74 65  less than n byte
3d2f0 73 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f  s from the end o
3d300 66 20 74 68 65 20 62 6c 6f 62 2c 20 0a 2a 2a 20  f the blob, .** 
3d310 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69  [SQLITE_ERROR] i
3d320 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e  s returned and n
3d330 6f 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65  o data is writte
3d340 6e 2e 20 20 49 66 20 6e 20 69 73 0a 2a 2a 20 6c  n.  If n is.** l
3d350 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 20 5b 53  ess than zero [S
3d360 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20  QLITE_ERROR] is 
3d370 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20  returned and no 
3d380 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 2e  data is written.
3d390 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73  .**.** On succes
3d3a0 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  s, SQLITE_OK is 
3d3b0 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
3d3c0 69 73 65 2c 20 61 6e 20 0a 2a 2a 20 5b 65 72 72  ise, an .** [err
3d3d0 6f 72 20 63 6f 64 65 5d 20 6f 72 20 61 6e 20 5b  or code] or an [
3d3e0 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63  extended error c
3d3f0 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64  ode] is returned
3d400 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e  ..**.** INVARIAN
3d410 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 46 31 37 38 37  TS:.**.** {F1787
3d420 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  3} The [sqlite3_
3d430 62 6c 6f 62 5f 77 72 69 74 65 28 50 2c 5a 2c 4e  blob_write(P,Z,N
3d440 2c 58 29 5d 20 69 6e 74 65 72 66 61 63 65 20 77  ,X)] interface w
3d450 72 69 74 65 73 20 4e 20 62 79 74 65 73 0a 2a 2a  rites N bytes.**
3d460 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d 20 62            from b
3d470 75 66 66 65 72 20 5a 20 69 6e 74 6f 0a 2a 2a 20  uffer Z into.** 
3d480 20 20 20 20 20 20 20 20 20 74 68 65 20 62 6c 6f           the blo
3d490 62 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f  b that [sqlite3_
3d4a0 62 6c 6f 62 5d 20 6f 62 6a 65 63 74 20 50 20 72  blob] object P r
3d4b0 65 66 65 72 73 20 74 6f 0a 2a 2a 20 20 20 20 20  efers to.**     
3d4c0 20 20 20 20 20 62 65 67 69 6e 6e 69 6e 67 20 61       beginning a
3d4d0 74 20 61 6e 20 6f 66 66 73 65 74 20 6f 66 20 58  t an offset of X
3d4e0 20 69 6e 74 6f 20 74 68 65 20 62 6c 6f 62 2e 0a   into the blob..
3d4f0 2a 2a 0a 2a 2a 20 7b 46 31 37 38 37 35 7d 20 54  **.** {F17875} T
3d500 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  he [sqlite3_blob
3d510 5f 77 72 69 74 65 28 50 2c 5a 2c 4e 2c 58 29 5d  _write(P,Z,N,X)]
3d520 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72   interface retur
3d530 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  ns.**          [
3d540 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d  SQLITE_READONLY]
3d550 20 69 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33   if the [sqlite3
3d560 5f 62 6c 6f 62 5d 20 6f 62 6a 65 63 74 20 50 20  _blob] object P 
3d570 77 61 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  was.**          
3d580 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70  [sqlite3_blob_op
3d590 65 6e 20 7c 20 6f 70 65 6e 65 64 5d 20 66 6f 72  en | opened] for
3d5a0 20 72 65 61 64 69 6e 67 20 6f 6e 6c 79 2e 0a 2a   reading only..*
3d5b0 2a 0a 2a 2a 20 7b 46 31 37 38 37 36 7d 20 49 6e  *.** {F17876} In
3d5c0 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77   [sqlite3_blob_w
3d5d0 72 69 74 65 28 50 2c 5a 2c 4e 2c 58 29 5d 20 69  rite(P,Z,N,X)] i
3d5e0 66 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  f the size of th
3d5f0 65 20 62 6c 6f 62 0a 2a 2a 20 20 20 20 20 20 20  e blob.**       
3d600 20 20 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20     is less than 
3d610 4e 2b 58 20 62 79 74 65 73 2c 20 74 68 65 6e 20  N+X bytes, then 
3d620 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  the function ret
3d630 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 45 52 52  urns [SQLITE_ERR
3d640 4f 52 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  OR].**          
3d650 61 6e 64 20 6e 6f 74 68 69 6e 67 20 69 73 20 77  and nothing is w
3d660 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20  ritten into the 
3d670 62 6c 6f 62 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 37  blob..**.** {F17
3d680 38 37 39 7d 20 49 6e 20 5b 73 71 6c 69 74 65 33  879} In [sqlite3
3d690 5f 62 6c 6f 62 5f 77 72 69 74 65 28 50 2c 5a 2c  _blob_write(P,Z,
3d6a0 4e 2c 58 29 5d 20 69 66 20 58 20 6f 72 20 4e 20  N,X)] if X or N 
3d6b0 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72  is less than zer
3d6c0 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  o.**          th
3d6d0 65 6e 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  en the function 
3d6e0 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f  returns [SQLITE_
3d6f0 45 52 52 4f 52 5d 0a 2a 2a 20 20 20 20 20 20 20  ERROR].**       
3d700 20 20 20 61 6e 64 20 6e 6f 74 68 69 6e 67 20 69     and nothing i
3d710 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  s written into t
3d720 68 65 20 62 6c 6f 62 2e 0a 2a 2a 0a 2a 2a 20 7b  he blob..**.** {
3d730 46 31 37 38 38 32 7d 20 54 68 65 20 5b 73 71 6c  F17882} The [sql
3d740 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28  ite3_blob_write(
3d750 50 2c 5a 2c 4e 2c 58 29 5d 20 69 6e 74 65 72 66  P,Z,N,X)] interf
3d760 61 63 65 20 72 65 74 75 72 6e 73 20 5b 53 51 4c  ace returns [SQL
3d770 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 20 20 20 20 20  ITE_OK].**      
3d780 20 20 20 20 69 66 20 4e 20 62 79 74 65 73 20 77      if N bytes w
3d790 68 65 72 65 20 73 75 63 63 65 73 73 66 75 6c 6c  here successfull
3d7a0 79 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 62  y written into b
3d7b0 6c 6f 62 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 37 38  lob..**.** {F178
3d7c0 38 35 7d 20 49 66 20 74 68 65 20 72 65 71 75 65  85} If the reque
3d7d0 73 74 65 64 20 77 72 69 74 65 20 63 6f 75 6c 64  sted write could
3d7e0 20 6e 6f 74 20 62 65 20 63 6f 6d 70 6c 65 74 65   not be complete
3d7f0 64 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  d,.**          t
3d800 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  he [sqlite3_blob
3d810 5f 77 72 69 74 65 28 50 2c 5a 2c 4e 2c 58 29 5d  _write(P,Z,N,X)]
3d820 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72   interface retur
3d830 6e 73 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20  ns an.**        
3d840 20 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b 65    appropriate [e
3d850 72 72 6f 72 20 63 6f 64 65 5d 20 6f 72 20 5b 65  rror code] or [e
3d860 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f  xtended error co
3d870 64 65 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 37 38  de]..**.** {F178
3d880 38 38 7d 20 49 66 20 61 6e 20 65 72 72 6f 72 20  88} If an error 
3d890 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 65 76  occurs during ev
3d8a0 61 6c 75 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c  aluation of [sql
3d8b0 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28  ite3_blob_write(
3d8c0 44 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 20 20 20 20  D,...)].**      
3d8d0 20 20 20 20 74 68 65 6e 20 73 75 62 73 65 71 75      then subsequ
3d8e0 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71  ent calls to [sq
3d8f0 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 44 29  lite3_errcode(D)
3d900 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  ],.**          [
3d910 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 44  sqlite3_errmsg(D
3d920 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  )], and [sqlite3
3d930 5f 65 72 72 6d 73 67 31 36 28 44 29 5d 20 77 69  _errmsg16(D)] wi
3d940 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20  ll return.**    
3d950 20 20 20 20 20 20 69 6e 66 6f 72 6d 61 74 69 6f        informatio
3d960 6e 20 61 70 70 72 6f 70 72 61 74 65 20 66 6f 72  n approprate for
3d970 20 74 68 61 74 20 65 72 72 6f 72 2e 0a 2a 2f 0a   that error..*/.
3d980 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3d990 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74  qlite3_blob_writ
3d9a0 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a  e(sqlite3_blob *
3d9b0 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c  , const void *z,
3d9c0 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 4f 66 66   int n, int iOff
3d9d0 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  set);../*.** CAP
3d9e0 49 33 52 45 46 3a 20 20 56 69 72 74 75 61 6c 20  I3REF:  Virtual 
3d9f0 46 69 6c 65 20 53 79 73 74 65 6d 20 4f 62 6a 65  File System Obje
3da00 63 74 73 20 7b 46 31 31 32 30 30 7d 0a 2a 2a 0a  cts {F11200}.**.
3da10 2a 2a 20 41 20 76 69 72 74 75 61 6c 20 66 69 6c  ** A virtual fil
3da20 65 73 79 73 74 65 6d 20 28 56 46 53 29 20 69 73  esystem (VFS) is
3da30 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 76 66 73   an [sqlite3_vfs
3da40 5d 20 6f 62 6a 65 63 74 0a 2a 2a 20 74 68 61 74  ] object.** that
3da50 20 53 51 4c 69 74 65 20 75 73 65 73 20 74 6f 20   SQLite uses to 
3da60 69 6e 74 65 72 61 63 74 0a 2a 2a 20 77 69 74 68  interact.** with
3da70 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   the underlying 
3da80 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
3da90 2e 20 20 4d 6f 73 74 20 53 51 4c 69 74 65 20 62  .  Most SQLite b
3daa0 75 69 6c 64 73 20 63 6f 6d 65 20 77 69 74 68 20  uilds come with 
3dab0 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 64 65 66 61  a.** single defa
3dac0 75 6c 74 20 56 46 53 20 74 68 61 74 20 69 73 20  ult VFS that is 
3dad0 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20  appropriate for 
3dae0 74 68 65 20 68 6f 73 74 20 63 6f 6d 70 75 74 65  the host compute
3daf0 72 2e 0a 2a 2a 20 4e 65 77 20 56 46 53 65 73 20  r..** New VFSes 
3db00 63 61 6e 20 62 65 20 72 65 67 69 73 74 65 72 65  can be registere
3db10 64 20 61 6e 64 20 65 78 69 73 74 69 6e 67 20 56  d and existing V
3db20 46 53 65 73 20 63 61 6e 20 62 65 20 75 6e 72 65  FSes can be unre
3db30 67 69 73 74 65 72 65 64 2e 0a 2a 2a 20 54 68 65  gistered..** The
3db40 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 72   following inter
3db50 66 61 63 65 73 20 61 72 65 20 70 72 6f 76 69 64  faces are provid
3db60 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  ed..**.** The sq
3db70 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 29  lite3_vfs_find()
3db80 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72   interface retur
3db90 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
3dba0 0a 2a 2a 20 61 20 56 46 53 20 67 69 76 65 6e 20  .** a VFS given 
3dbb0 69 74 73 20 6e 61 6d 65 2e 20 20 4e 61 6d 65 73  its name.  Names
3dbc0 20 61 72 65 20 63 61 73 65 20 73 65 6e 73 69 74   are case sensit
3dbd0 69 76 65 2e 0a 2a 2a 20 4e 61 6d 65 73 20 61 72  ive..** Names ar
3dbe0 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  e zero-terminate
3dbf0 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 73 2e  d UTF-8 strings.
3dc00 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20  .** If there is 
3dc10 6e 6f 20 6d 61 74 63 68 2c 20 61 20 4e 55 4c 4c  no match, a NULL
3dc20 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 69 73 20 72  .** pointer is r
3dc30 65 74 75 72 6e 65 64 2e 20 20 49 66 20 7a 56 66  eturned.  If zVf
3dc40 73 4e 61 6d 65 20 69 73 20 4e 55 4c 4c 20 74 68  sName is NULL th
3dc50 65 6e 20 74 68 65 20 64 65 66 61 75 6c 74 20 0a  en the default .
3dc60 2a 2a 20 56 46 53 20 69 73 20 72 65 74 75 72 6e  ** VFS is return
3dc70 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 56  ed. .**.** New V
3dc80 46 53 65 73 20 61 72 65 20 72 65 67 69 73 74 65  FSes are registe
3dc90 72 65 64 20 77 69 74 68 20 73 71 6c 69 74 65 33  red with sqlite3
3dca0 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 29 2e  _vfs_register().
3dcb0 0a 2a 2a 20 45 61 63 68 20 6e 65 77 20 56 46 53  .** Each new VFS
3dcc0 20 62 65 63 6f 6d 65 73 20 74 68 65 20 64 65 66   becomes the def
3dcd0 61 75 6c 74 20 56 46 53 20 69 66 20 74 68 65 20  ault VFS if the 
3dce0 6d 61 6b 65 44 66 6c 74 20 66 6c 61 67 20 69 73  makeDflt flag is
3dcf0 20 73 65 74 2e 0a 2a 2a 20 54 68 65 20 73 61 6d   set..** The sam
3dd00 65 20 56 46 53 20 63 61 6e 20 62 65 20 72 65 67  e VFS can be reg
3dd10 69 73 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65  istered multiple
3dd20 20 74 69 6d 65 73 20 77 69 74 68 6f 75 74 20 69   times without i
3dd30 6e 6a 75 72 79 2e 0a 2a 2a 20 54 6f 20 6d 61 6b  njury..** To mak
3dd40 65 20 61 6e 20 65 78 69 73 74 69 6e 67 20 56 46  e an existing VF
3dd50 53 20 69 6e 74 6f 20 74 68 65 20 64 65 66 61 75  S into the defau
3dd60 6c 74 20 56 46 53 2c 20 72 65 67 69 73 74 65 72  lt VFS, register
3dd70 20 69 74 20 61 67 61 69 6e 0a 2a 2a 20 77 69 74   it again.** wit
3dd80 68 20 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 66  h the makeDflt f
3dd90 6c 61 67 20 73 65 74 2e 20 20 49 66 20 74 77 6f  lag set.  If two
3dda0 20 64 69 66 66 65 72 65 6e 74 20 56 46 53 65 73   different VFSes
3ddb0 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 73 61 6d   with the.** sam
3ddc0 65 20 6e 61 6d 65 20 61 72 65 20 72 65 67 69 73  e name are regis
3ddd0 74 65 72 65 64 2c 20 74 68 65 20 62 65 68 61 76  tered, the behav
3dde0 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64  ior is undefined
3ddf0 2e 20 20 49 66 20 61 0a 2a 2a 20 56 46 53 20 69  .  If a.** VFS i
3de00 73 20 72 65 67 69 73 74 65 72 65 64 20 77 69 74  s registered wit
3de10 68 20 61 20 6e 61 6d 65 20 74 68 61 74 20 69 73  h a name that is
3de20 20 4e 55 4c 4c 20 6f 72 20 61 6e 20 65 6d 70 74   NULL or an empt
3de30 79 20 73 74 72 69 6e 67 2c 0a 2a 2a 20 74 68 65  y string,.** the
3de40 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69  n the behavior i
3de50 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20  s undefined..** 
3de60 0a 2a 2a 20 55 6e 72 65 67 69 73 74 65 72 20 61  .** Unregister a
3de70 20 56 46 53 20 77 69 74 68 20 74 68 65 20 73 71   VFS with the sq
3de80 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69  lite3_vfs_unregi
3de90 73 74 65 72 28 29 20 69 6e 74 65 72 66 61 63 65  ster() interface
3dea0 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 65 66 61  ..** If the defa
3deb0 75 6c 74 20 56 46 53 20 69 73 20 75 6e 72 65 67  ult VFS is unreg
3dec0 69 73 74 65 72 65 64 2c 20 61 6e 6f 74 68 65 72  istered, another
3ded0 20 56 46 53 20 69 73 20 63 68 6f 73 65 6e 20 61   VFS is chosen a
3dee0 73 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74  s.** the default
3def0 2e 20 20 54 68 65 20 63 68 6f 69 63 65 20 66 6f  .  The choice fo
3df00 72 20 74 68 65 20 6e 65 77 20 56 46 53 20 69 73  r the new VFS is
3df10 20 61 72 62 69 74 72 61 72 79 2e 0a 2a 2a 0a 2a   arbitrary..**.*
3df20 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a  * INVARIANTS:.**
3df30 0a 2a 2a 20 7b 46 31 31 32 30 33 7d 20 54 68 65  .** {F11203} The
3df40 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69   [sqlite3_vfs_fi
3df50 6e 64 28 4e 29 5d 20 69 6e 74 65 72 66 61 63 65  nd(N)] interface
3df60 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
3df70 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20  er to the.**    
3df80 20 20 20 20 20 20 72 65 67 69 73 74 65 72 65 64        registered
3df90 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f   [sqlite3_vfs] o
3dfa0 62 6a 65 63 74 20 77 68 6f 73 65 20 6e 61 6d 65  bject whose name
3dfb0 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68 65 73   exactly matches
3dfc0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65  .**          the
3dfd0 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
3dfe0 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 4e 2c   UTF-8 string N,
3dff0 20 6f 72 20 69 74 20 72 65 74 75 72 6e 73 20 4e   or it returns N
3e000 55 4c 4c 20 69 66 0a 2a 2a 20 20 20 20 20 20 20  ULL if.**       
3e010 20 20 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d     there is no m
3e020 61 74 63 68 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 31  atch..**.** {F11
3e030 32 30 36 7d 20 49 66 20 74 68 65 20 4e 20 70 61  206} If the N pa
3e040 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69  rameter to [sqli
3e050 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 4e 29 5d  te3_vfs_find(N)]
3e060 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 0a 2a 2a   is NULL then.**
3e070 20 20 20 20 20 20 20 20 20 20 74 68 65 20 66 75            the fu
3e080 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61  nction returns a
3e090 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
3e0a0 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33  default [sqlite3
3e0b0 5f 76 66 73 5d 0a 2a 2a 20 20 20 20 20 20 20 20  _vfs].**        
3e0c0 20 20 6f 62 6a 65 63 74 20 69 66 20 74 68 65 72    object if ther
3e0d0 65 20 69 73 20 6f 6e 65 2c 20 6f 72 20 4e 55 4c  e is one, or NUL
3e0e0 4c 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f  L if there is no
3e0f0 20 64 65 66 61 75 6c 74 20 0a 2a 2a 20 20 20 20   default .**    
3e100 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 76        [sqlite3_v
3e110 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a  fs] object..**.*
3e120 2a 20 7b 46 31 31 32 30 39 7d 20 54 68 65 20 5b  * {F11209} The [
3e130 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69  sqlite3_vfs_regi
3e140 73 74 65 72 28 50 2c 46 29 5d 20 69 6e 74 65 72  ster(P,F)] inter
3e150 66 61 63 65 20 72 65 67 69 73 74 65 72 73 20 74  face registers t
3e160 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77  he.**          w
3e170 65 6c 6c 2d 66 6f 72 6d 65 64 20 5b 73 71 6c 69  ell-formed [sqli
3e180 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 20  te3_vfs] object 
3e190 50 20 75 73 69 6e 67 20 74 68 65 20 6e 61 6d 65  P using the name
3e1a0 20 67 69 76 65 6e 0a 2a 2a 20 20 20 20 20 20 20   given.**       
3e1b0 20 20 20 62 79 20 74 68 65 20 7a 4e 61 6d 65 20     by the zName 
3e1c0 66 69 65 6c 64 20 6f 66 20 74 68 65 20 6f 62 6a  field of the obj
3e1d0 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31 31 32  ect..**.** {F112
3e1e0 31 32 7d 20 55 73 69 6e 67 20 74 68 65 20 5b 73  12} Using the [s
3e1f0 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73  qlite3_vfs_regis
3e200 74 65 72 28 50 2c 46 29 5d 20 69 6e 74 65 72 66  ter(P,F)] interf
3e210 61 63 65 20 74 6f 20 72 65 67 69 73 74 65 72 0a  ace to register.
3e220 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20  **          the 
3e230 73 61 6d 65 20 5b 73 71 6c 69 74 65 33 5f 76 66  same [sqlite3_vf
3e240 73 5d 20 6f 62 6a 65 63 74 20 6d 75 6c 74 69 70  s] object multip
3e250 6c 65 20 74 69 6d 65 73 20 69 73 20 61 20 68 61  le times is a ha
3e260 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2a  rmless no-op..**
3e270 0a 2a 2a 20 7b 46 31 31 32 31 35 7d 20 54 68 65  .** {F11215} The
3e280 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65   [sqlite3_vfs_re
3e290 67 69 73 74 65 72 28 50 2c 46 29 5d 20 69 6e 74  gister(P,F)] int
3e2a0 65 72 66 61 63 65 20 6d 61 6b 65 73 20 74 68 65  erface makes the
3e2b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65  .**          the
3e2c0 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f   [sqlite3_vfs] o
3e2d0 62 6a 65 63 74 20 50 20 74 68 65 20 64 65 66 61  bject P the defa
3e2e0 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73  ult [sqlite3_vfs
3e2f0 5d 20 6f 62 6a 65 63 74 0a 2a 2a 20 20 20 20 20  ] object.**     
3e300 20 20 20 20 20 69 66 20 46 20 69 73 20 6e 6f 6e       if F is non
3e310 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31  -zero..**.** {F1
3e320 31 32 31 38 7d 20 54 68 65 20 5b 73 71 6c 69 74  1218} The [sqlit
3e330 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65  e3_vfs_unregiste
3e340 72 28 50 29 5d 20 69 6e 74 65 72 66 61 63 65 20  r(P)] interface 
3e350 75 6e 72 65 67 69 73 74 65 72 73 20 74 68 65 0a  unregisters the.
3e360 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
3e370 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74  ite3_vfs] object
3e380 20 50 20 73 6f 20 74 68 61 74 20 69 74 20 69 73   P so that it is
3e390 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 74 75 72   no longer retur
3e3a0 6e 65 64 20 62 79 0a 2a 2a 20 20 20 20 20 20 20  ned by.**       
3e3b0 20 20 20 73 75 62 73 65 71 75 65 6e 74 20 63 61     subsequent ca
3e3c0 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  lls to [sqlite3_
3e3d0 76 66 73 5f 66 69 6e 64 28 29 5d 2e 0a 2a 2f 0a  vfs_find()]..*/.
3e3e0 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
3e3f0 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 5f  e3_vfs *sqlite3_
3e400 76 66 73 5f 66 69 6e 64 28 63 6f 6e 73 74 20 63  vfs_find(const c
3e410 68 61 72 20 2a 7a 56 66 73 4e 61 6d 65 29 3b 0a  har *zVfsName);.
3e420 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3e430 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73  qlite3_vfs_regis
3e440 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 2a  ter(sqlite3_vfs*
3e450 2c 20 69 6e 74 20 6d 61 6b 65 44 66 6c 74 29 3b  , int makeDflt);
3e460 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
3e470 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65  sqlite3_vfs_unre
3e480 67 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76  gister(sqlite3_v
3e490 66 73 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  fs*);../*.** CAP
3e4a0 49 33 52 45 46 3a 20 4d 75 74 65 78 65 73 20 7b  I3REF: Mutexes {
3e4b0 46 31 37 30 30 30 7d 0a 2a 2a 0a 2a 2a 20 54 68  F17000}.**.** Th
3e4c0 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 75 73  e SQLite core us
3e4d0 65 73 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  es these routine
3e4e0 73 20 66 6f 72 20 74 68 72 65 61 64 0a 2a 2a 20  s for thread.** 
3e4f0 73 79 6e 63 68 72 6f 6e 69 7a 61 74 69 6f 6e 2e  synchronization.
3e500 20 20 54 68 6f 75 67 68 20 74 68 65 79 20 61 72    Though they ar
3e510 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 69  e intended for i
3e520 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62  nternal.** use b
3e530 79 20 53 51 4c 69 74 65 2c 20 63 6f 64 65 20 74  y SQLite, code t
3e540 68 61 74 20 6c 69 6e 6b 73 20 61 67 61 69 6e 73  hat links agains
3e550 74 20 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20 70  t SQLite is.** p
3e560 65 72 6d 69 74 74 65 64 20 74 6f 20 75 73 65 20  ermitted to use 
3e570 61 6e 79 20 6f 66 20 74 68 65 73 65 20 72 6f 75  any of these rou
3e580 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tines..**.** The
3e590 20 53 51 4c 69 74 65 20 73 6f 75 72 63 65 20 63   SQLite source c
3e5a0 6f 64 65 20 63 6f 6e 74 61 69 6e 73 20 6d 75 6c  ode contains mul
3e5b0 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  tiple implementa
3e5c0 74 69 6f 6e 73 20 0a 2a 2a 20 6f 66 20 74 68 65  tions .** of the
3e5d0 73 65 20 6d 75 74 65 78 20 72 6f 75 74 69 6e 65  se mutex routine
3e5e0 73 2e 20 20 41 6e 20 61 70 70 72 6f 70 72 69 61  s.  An appropria
3e5f0 74 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  te implementatio
3e600 6e 0a 2a 2a 20 69 73 20 73 65 6c 65 63 74 65 64  n.** is selected
3e610 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 61   automatically a
3e620 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 20  t compile-time. 
3e630 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a   The following.*
3e640 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
3e650 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20  s are available 
3e660 69 6e 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f  in the SQLite co
3e670 72 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a  re:.**.** <ul>.*
3e680 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f  * <li>   SQLITE_
3e690 4d 55 54 45 58 5f 4f 53 32 0a 2a 2a 20 3c 6c 69  MUTEX_OS2.** <li
3e6a0 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  >   SQLITE_MUTEX
3e6b0 5f 50 54 48 52 45 41 44 0a 2a 2a 20 3c 6c 69 3e  _PTHREAD.** <li>
3e6c0 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f     SQLITE_MUTEX_
3e6d0 57 33 32 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51  W32.** <li>   SQ
3e6e0 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a  LITE_MUTEX_NOOP.
3e6f0 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54  ** </ul>.**.** T
3e700 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  he SQLITE_MUTEX_
3e710 4e 4f 4f 50 20 69 6d 70 6c 65 6d 65 6e 74 61 74  NOOP implementat
3e720 69 6f 6e 20 69 73 20 61 20 73 65 74 20 6f 66 20  ion is a set of 
3e730 72 6f 75 74 69 6e 65 73 20 0a 2a 2a 20 74 68 61  routines .** tha
3e740 74 20 64 6f 65 73 20 6e 6f 20 72 65 61 6c 20 6c  t does no real l
3e750 6f 63 6b 69 6e 67 20 61 6e 64 20 69 73 20 61 70  ocking and is ap
3e760 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 75 73  propriate for us
3e770 65 20 69 6e 20 0a 2a 2a 20 61 20 73 69 6e 67 6c  e in .** a singl
3e780 65 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c 69  e-threaded appli
3e790 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 53 51 4c  cation.  The SQL
3e7a0 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 2c 0a 2a  ITE_MUTEX_OS2,.*
3e7b0 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50  * SQLITE_MUTEX_P
3e7c0 54 48 52 45 41 44 2c 20 61 6e 64 20 53 51 4c 49  THREAD, and SQLI
3e7d0 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 69 6d 70  TE_MUTEX_W32 imp
3e7e0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20  lementations.** 
3e7f0 61 72 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  are appropriate 
3e800 66 6f 72 20 75 73 65 20 6f 6e 20 6f 73 2f 32 2c  for use on os/2,
3e810 20 75 6e 69 78 2c 20 61 6e 64 20 77 69 6e 64 6f   unix, and windo
3e820 77 73 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 53 51  ws..** .** If SQ
3e830 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64  Lite is compiled
3e840 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45   with the SQLITE
3e850 5f 4d 55 54 45 58 5f 41 50 50 44 45 46 20 70 72  _MUTEX_APPDEF pr
3e860 65 70 72 6f 63 65 73 73 6f 72 0a 2a 2a 20 6d 61  eprocessor.** ma
3e870 63 72 6f 20 64 65 66 69 6e 65 64 20 28 77 69 74  cro defined (wit
3e880 68 20 22 2d 44 53 51 4c 49 54 45 5f 4d 55 54 45  h "-DSQLITE_MUTE
3e890 58 5f 41 50 50 44 45 46 3d 31 22 29 2c 20 74 68  X_APPDEF=1"), th
3e8a0 65 6e 20 6e 6f 20 6d 75 74 65 78 0a 2a 2a 20 69  en no mutex.** i
3e8b0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73  mplementation is
3e8c0 20 69 6e 63 6c 75 64 65 64 20 77 69 74 68 20 74   included with t
3e8d0 68 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65  he library.  The
3e8e0 0a 2a 2a 20 6d 75 74 65 78 20 69 6e 74 65 72 66  .** mutex interf
3e8f0 61 63 65 20 72 6f 75 74 69 6e 65 73 20 64 65 66  ace routines def
3e900 69 6e 65 64 20 68 65 72 65 20 62 65 63 6f 6d 65  ined here become
3e910 20 65 78 74 65 72 6e 61 6c 0a 2a 2a 20 72 65 66   external.** ref
3e920 65 72 65 6e 63 65 73 20 69 6e 20 74 68 65 20 53  erences in the S
3e930 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 66 6f  QLite library fo
3e940 72 20 77 68 69 63 68 20 69 6d 70 6c 65 6d 65 6e  r which implemen
3e950 74 61 74 69 6f 6e 73 0a 2a 2a 20 6d 75 73 74 20  tations.** must 
3e960 62 65 20 70 72 6f 76 69 64 65 64 20 62 79 20 74  be provided by t
3e970 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20  he application. 
3e980 20 54 68 69 73 20 66 61 63 69 6c 69 74 79 20 61   This facility a
3e990 6c 6c 6f 77 73 20 61 6e 0a 2a 2a 20 61 70 70 6c  llows an.** appl
3e9a0 69 63 61 74 69 6f 6e 20 74 68 61 74 20 6c 69 6e  ication that lin
3e9b0 6b 73 20 61 67 61 69 6e 73 74 20 53 51 4c 69 74  ks against SQLit
3e9c0 65 20 74 6f 20 70 72 6f 76 69 64 65 20 69 74 73  e to provide its
3e9d0 20 6f 77 6e 20 6d 75 74 65 78 0a 2a 2a 20 69 6d   own mutex.** im
3e9e0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 74  plementation wit
3e9f0 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 6d  hout having to m
3ea00 6f 64 69 66 79 20 74 68 65 20 53 51 4c 69 74 65  odify the SQLite
3ea10 20 63 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 7b 46 31   core..**.** {F1
3ea20 37 30 31 31 7d 20 54 68 65 20 73 71 6c 69 74 65  7011} The sqlite
3ea30 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20  3_mutex_alloc() 
3ea40 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65  routine allocate
3ea50 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78  s a new.** mutex
3ea60 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70   and returns a p
3ea70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 7b 46  ointer to it. {F
3ea80 31 37 30 31 32 7d 20 49 66 20 69 74 20 72 65 74  17012} If it ret
3ea90 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61  urns NULL.** tha
3eaa0 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d  t means that a m
3eab0 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62  utex could not b
3eac0 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 46 31  e allocated. {F1
3ead0 37 30 31 33 7d 20 53 51 4c 69 74 65 0a 2a 2a 20  7013} SQLite.** 
3eae0 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20  will unwind its 
3eaf0 73 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e  stack and return
3eb00 20 61 6e 20 65 72 72 6f 72 2e 20 7b 46 31 37 30   an error. {F170
3eb10 31 34 7d 20 54 68 65 20 61 72 67 75 6d 65 6e 74  14} The argument
3eb20 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d  .** to sqlite3_m
3eb30 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20  utex_alloc() is 
3eb40 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74  one of these int
3eb50 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a  eger constants:.
3eb60 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c  **.** <ul>.** <l
3eb70 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
3eb80 5f 46 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53  _FAST.** <li>  S
3eb90 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
3eba0 52 53 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53  RSIVE.** <li>  S
3ebb0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
3ebc0 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69  IC_MASTER.** <li
3ebd0 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
3ebe0 53 54 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c  STATIC_MEM.** <l
3ebf0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
3ec00 5f 53 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20  _STATIC_MEM2.** 
3ec10 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
3ec20 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a  EX_STATIC_PRNG.*
3ec30 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
3ec40 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a  UTEX_STATIC_LRU.
3ec50 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
3ec60 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55  MUTEX_STATIC_LRU
3ec70 32 0a 2a 2a 20 3c 2f 75 6c 3e 20 7b 45 4e 44 7d  2.** </ul> {END}
3ec80 0a 2a 2a 0a 2a 2a 20 7b 46 31 37 30 31 35 7d 20  .**.** {F17015} 
3ec90 54 68 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f  The first two co
3eca0 6e 73 74 61 6e 74 73 20 63 61 75 73 65 20 73 71  nstants cause sq
3ecb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
3ecc0 63 28 29 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a  c() to create.**
3ecd0 20 61 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 54   a new mutex.  T
3ece0 68 65 20 6e 65 77 20 6d 75 74 65 78 20 69 73 20  he new mutex is 
3ecf0 72 65 63 75 72 73 69 76 65 20 77 68 65 6e 20 53  recursive when S
3ed00 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
3ed10 52 53 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 64  RSIVE.** is used
3ed20 20 62 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61   but not necessa
3ed30 72 69 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c  rily so when SQL
3ed40 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69  ITE_MUTEX_FAST i
3ed50 73 20 75 73 65 64 2e 20 7b 45 4e 44 7d 0a 2a 2a  s used. {END}.**
3ed60 20 54 68 65 20 6d 75 74 65 78 20 69 6d 70 6c 65   The mutex imple
3ed70 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e  mentation does n
3ed80 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20  ot need to make 
3ed90 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a  a distinction.**
3eda0 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54 45 5f   between SQLITE_
3edb0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20  MUTEX_RECURSIVE 
3edc0 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  and SQLITE_MUTEX
3edd0 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f 65 73  _FAST if it does
3ede0 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e  .** not want to.
3edf0 20 20 7b 46 31 37 30 31 36 7d 20 42 75 74 20 53    {F17016} But S
3ee00 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20  QLite will only 
3ee10 72 65 71 75 65 73 74 20 61 20 72 65 63 75 72 73  request a recurs
3ee20 69 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20  ive mutex in.** 
3ee30 63 61 73 65 73 20 77 68 65 72 65 20 69 74 20 72  cases where it r
3ee40 65 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e  eally needs one.
3ee50 20 20 7b 45 4e 44 7d 20 49 66 20 61 20 66 61 73    {END} If a fas
3ee60 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76  ter non-recursiv
3ee70 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65  e mutex.** imple
3ee80 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61  mentation is ava
3ee90 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f  ilable on the ho
3eea0 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65  st platform, the
3eeb0 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d   mutex subsystem
3eec0 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e  .** might return
3eed0 20 73 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e   such a mutex in
3eee0 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c   response to SQL
3eef0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a  ITE_MUTEX_FAST..
3ef00 2a 2a 0a 2a 2a 20 7b 46 31 37 30 31 37 7d 20 54  **.** {F17017} T
3ef10 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64  he other allowed
3ef20 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73   parameters to s
3ef30 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
3ef40 6f 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e  oc() each return
3ef50 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  .** a pointer to
3ef60 20 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69   a static preexi
3ef70 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 7b 45 4e  sting mutex. {EN
3ef80 44 7d 20 20 46 6f 75 72 20 73 74 61 74 69 63 20  D}  Four static 
3ef90 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 75  mutexes are.** u
3efa0 73 65 64 20 62 79 20 74 68 65 20 63 75 72 72 65  sed by the curre
3efb0 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51  nt version of SQ
3efc0 4c 69 74 65 2e 20 20 46 75 74 75 72 65 20 76 65  Lite.  Future ve
3efd0 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65  rsions of SQLite
3efe0 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 64 64 69  .** may add addi
3eff0 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75  tional static mu
3f000 74 65 78 65 73 2e 20 20 53 74 61 74 69 63 20 6d  texes.  Static m
3f010 75 74 65 78 65 73 20 61 72 65 20 66 6f 72 20 69  utexes are for i
3f020 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62  nternal.** use b
3f030 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20  y SQLite only.  
3f040 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61  Applications tha
3f050 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 75 74  t use SQLite mut
3f060 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75  exes should.** u
3f070 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61  se only the dyna
3f080 6d 69 63 20 6d 75 74 65 78 65 73 20 72 65 74 75  mic mutexes retu
3f090 72 6e 65 64 20 62 79 20 53 51 4c 49 54 45 5f 4d  rned by SQLITE_M
3f0a0 55 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20  UTEX_FAST or.** 
3f0b0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
3f0c0 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 7b 46  URSIVE..**.** {F
3f0d0 31 37 30 31 38 7d 20 4e 6f 74 65 20 74 68 61 74  17018} Note that
3f0e0 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 64   if one of the d
3f0f0 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 72  ynamic mutex par
3f100 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 5f  ameters (SQLITE_
3f110 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72  MUTEX_FAST.** or
3f120 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
3f130 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65 64  CURSIVE) is used
3f140 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75   then sqlite3_mu
3f150 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72  tex_alloc().** r
3f160 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72 65  eturns a differe
3f170 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72  nt mutex on ever
3f180 79 20 63 61 6c 6c 2e 20 20 7b 46 31 37 30 33 34  y call.  {F17034
3f190 7d 20 42 75 74 20 66 6f 72 20 74 68 65 20 73 74  } But for the st
3f1a0 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78 20 74  atic .** mutex t
3f1b0 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65 20 6d  ypes, the same m
3f1c0 75 74 65 78 20 69 73 20 72 65 74 75 72 6e 65 64  utex is returned
3f1d0 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 20 74   on every call t
3f1e0 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 20 73  hat has.** the s
3f1f0 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 72 2e  ame type number.
3f200 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 7b 46 31   {END}.**.** {F1
3f210 37 30 31 39 7d 20 54 68 65 20 73 71 6c 69 74 65  7019} The sqlite
3f220 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 29 20 72  3_mutex_free() r
3f230 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74  outine deallocat
3f240 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 0a  es a previously.
3f250 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 64 79 6e  ** allocated dyn
3f260 61 6d 69 63 20 6d 75 74 65 78 2e 20 7b 46 31 37  amic mutex. {F17
3f270 30 32 30 7d 20 53 51 4c 69 74 65 20 69 73 20 63  020} SQLite is c
3f280 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f  areful to deallo
3f290 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 64 79  cate every.** dy
3f2a0 6e 61 6d 69 63 20 6d 75 74 65 78 20 74 68 61 74  namic mutex that
3f2b0 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 20 7b   it allocates. {
3f2c0 55 31 37 30 32 31 7d 20 54 68 65 20 64 79 6e 61  U17021} The dyna
3f2d0 6d 69 63 20 6d 75 74 65 78 65 73 20 6d 75 73 74  mic mutexes must
3f2e0 20 6e 6f 74 20 62 65 20 69 6e 20 0a 2a 2a 20 75   not be in .** u
3f2f0 73 65 20 77 68 65 6e 20 74 68 65 79 20 61 72 65  se when they are
3f300 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 55   deallocated. {U
3f310 31 37 30 32 32 7d 20 41 74 74 65 6d 70 74 69 6e  17022} Attemptin
3f320 67 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20  g to deallocate 
3f330 61 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 65  a static.** mute
3f340 78 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64  x results in und
3f350 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e  efined behavior.
3f360 20 7b 46 31 37 30 32 33 7d 20 53 51 4c 69 74 65   {F17023} SQLite
3f370 20 6e 65 76 65 72 20 64 65 61 6c 6c 6f 63 61 74   never deallocat
3f380 65 73 0a 2a 2a 20 61 20 73 74 61 74 69 63 20 6d  es.** a static m
3f390 75 74 65 78 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a  utex. {END}.**.*
3f3a0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
3f3b0 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20  tex_enter() and 
3f3c0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
3f3d0 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74  y() routines att
3f3e0 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72  empt.** to enter
3f3f0 20 61 20 6d 75 74 65 78 2e 20 7b 46 31 37 30 32   a mutex. {F1702
3f400 34 7d 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68  4} If another th
3f410 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20  read is already 
3f420 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78  within the mutex
3f430 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74  ,.** sqlite3_mut
3f440 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20  ex_enter() will 
3f450 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65  block and sqlite
3f460 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69  3_mutex_try() wi
3f470 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c  ll return.** SQL
3f480 49 54 45 5f 42 55 53 59 2e 20 7b 46 31 37 30 32  ITE_BUSY. {F1702
3f490 35 7d 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f  5}  The sqlite3_
3f4a0 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65  mutex_try() inte
3f4b0 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 53 51  rface returns SQ
3f4c0 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20  LITE_OK.** upon 
3f4d0 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79  successful entry
3f4e0 2e 20 20 7b 46 31 37 30 32 36 7d 20 4d 75 74 65  .  {F17026} Mute
3f4f0 78 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e  xes created usin
3f500 67 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45  g.** SQLITE_MUTE
3f510 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e 20  X_RECURSIVE can 
3f520 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69  be entered multi
3f530 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68 65  ple times by the
3f540 20 73 61 6d 65 20 74 68 72 65 61 64 2e 0a 2a 2a   same thread..**
3f550 20 7b 46 31 37 30 32 37 7d 20 49 6e 20 73 75 63   {F17027} In suc
3f560 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20  h cases the,.** 
3f570 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 65 78  mutex must be ex
3f580 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 6e 75  ited an equal nu
3f590 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 62 65  mber of times be
3f5a0 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 68 72  fore another thr
3f5b0 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72  ead.** can enter
3f5c0 2e 20 20 7b 55 31 37 30 32 38 7d 20 49 66 20 74  .  {U17028} If t
3f5d0 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74  he same thread t
3f5e0 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e  ries to enter an
3f5f0 79 20 6f 74 68 65 72 0a 2a 2a 20 6b 69 6e 64 20  y other.** kind 
3f600 6f 66 20 6d 75 74 65 78 20 6d 6f 72 65 20 74 68  of mutex more th
3f610 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68  an once, the beh
3f620 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e  avior is undefin
3f630 65 64 2e 0a 2a 2a 20 7b 46 31 37 30 32 39 7d 20  ed..** {F17029} 
3f640 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65  SQLite will neve
3f650 72 20 65 78 68 69 62 69 74 0a 2a 2a 20 73 75 63  r exhibit.** suc
3f660 68 20 62 65 68 61 76 69 6f 72 20 69 6e 20 69 74  h behavior in it
3f670 73 20 6f 77 6e 20 75 73 65 20 6f 66 20 6d 75 74  s own use of mut
3f680 65 78 65 73 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a  exes. {END}.**.*
3f690 2a 20 53 6f 6d 65 20 73 79 73 74 65 6d 73 20 28  * Some systems (
3f6a0 65 78 3a 20 77 69 6e 64 6f 77 73 39 35 29 20 64  ex: windows95) d
3f6b0 6f 20 6e 6f 74 20 74 68 65 20 6f 70 65 72 61 74  o not the operat
3f6c0 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20  ion implemented 
3f6d0 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75  by.** sqlite3_mu
3f6e0 74 65 78 5f 74 72 79 28 29 2e 20 20 4f 6e 20 74  tex_try().  On t
3f6f0 68 6f 73 65 20 73 79 73 74 65 6d 73 2c 20 73 71  hose systems, sq
3f700 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
3f710 29 20 77 69 6c 6c 0a 2a 2a 20 61 6c 77 61 79 73  ) will.** always
3f720 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
3f730 55 53 59 2e 20 20 7b 46 31 37 30 33 30 7d 20 54  USY.  {F17030} T
3f740 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6f  he SQLite core o
3f750 6e 6c 79 20 65 76 65 72 20 75 73 65 73 0a 2a 2a  nly ever uses.**
3f760 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74   sqlite3_mutex_t
3f770 72 79 28 29 20 61 73 20 61 6e 20 6f 70 74 69 6d  ry() as an optim
3f780 69 7a 61 74 69 6f 6e 20 73 6f 20 74 68 69 73 20  ization so this 
3f790 69 73 20 61 63 63 65 70 74 61 62 6c 65 20 62 65  is acceptable be
3f7a0 68 61 76 69 6f 72 2e 20 7b 45 4e 44 7d 0a 2a 2a  havior. {END}.**
3f7b0 0a 2a 2a 20 7b 46 31 37 30 33 31 7d 20 54 68 65  .** {F17031} The
3f7c0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
3f7d0 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65  eave() routine e
3f7e0 78 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 61  xits a mutex tha
3f7f0 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75  t was.** previou
3f800 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20 74  sly entered by t
3f810 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20  he same thread. 
3f820 20 7b 55 31 37 30 33 32 7d 20 54 68 65 20 62 65   {U17032} The be
3f830 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64  havior.** is und
3f840 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75  efined if the mu
3f850 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65  tex is not curre
3f860 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20  ntly entered by 
3f870 74 68 65 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 74  the.** calling t
3f880 68 72 65 61 64 20 6f 72 20 69 73 20 6e 6f 74 20  hread or is not 
3f890 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61  currently alloca
3f8a0 74 65 64 2e 20 20 7b 46 31 37 30 33 33 7d 20 53  ted.  {F17033} S
3f8b0 51 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a 20 6e 65  QLite will.** ne
3f8c0 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 20 7b  ver do either. {
3f8d0 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  END}.**.** See a
3f8e0 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 6d 75  lso: [sqlite3_mu
3f8f0 74 65 78 5f 68 65 6c 64 28 29 5d 20 61 6e 64 20  tex_held()] and 
3f900 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e  [sqlite3_mutex_n
3f910 6f 74 68 65 6c 64 28 29 5d 2e 0a 2a 2f 0a 53 51  otheld()]..*/.SQ
3f920 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
3f930 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 5f  _mutex *sqlite3_
3f940 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 69 6e 74 29  mutex_alloc(int)
3f950 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ;.SQLITE_API voi
3f960 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
3f970 66 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  free(sqlite3_mut
3f980 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ex*);.SQLITE_API
3f990 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75   void sqlite3_mu
3f9a0 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65  tex_enter(sqlite
3f9b0 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 4c 49 54  3_mutex*);.SQLIT
3f9c0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3f9d0 33 5f 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69  3_mutex_try(sqli
3f9e0 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 4c  te3_mutex*);.SQL
3f9f0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
3fa00 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
3fa10 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29  (sqlite3_mutex*)
3fa20 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
3fa30 46 3a 20 4d 75 74 65 78 20 56 65 72 69 66 63 61  F: Mutex Verifca
3fa40 74 69 6f 6e 20 52 6f 75 74 69 6e 65 73 20 7b 46  tion Routines {F
3fa50 31 37 30 38 30 7d 0a 2a 2a 0a 2a 2a 20 54 68 65  17080}.**.** The
3fa60 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
3fa70 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65  eld() and sqlite
3fa80 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28  3_mutex_notheld(
3fa90 29 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72  ) routines.** ar
3faa0 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75  e intended for u
3fab0 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74  se inside assert
3fac0 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 7b  () statements. {
3fad0 46 31 37 30 38 31 7d 20 54 68 65 20 53 51 4c 69  F17081} The SQLi
3fae0 74 65 20 63 6f 72 65 0a 2a 2a 20 6e 65 76 65 72  te core.** never
3faf0 20 75 73 65 73 20 74 68 65 73 65 20 72 6f 75 74   uses these rout
3fb00 69 6e 65 73 20 65 78 63 65 70 74 20 69 6e 73 69  ines except insi
3fb10 64 65 20 61 6e 20 61 73 73 65 72 74 28 29 20 61  de an assert() a
3fb20 6e 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a  nd applications.
3fb30 2a 2a 20 61 72 65 20 61 64 76 69 73 65 64 20 74  ** are advised t
3fb40 6f 20 66 6f 6c 6c 6f 77 20 74 68 65 20 6c 65 61  o follow the lea
3fb50 64 20 6f 66 20 74 68 65 20 63 6f 72 65 2e 20 20  d of the core.  
3fb60 7b 46 31 37 30 38 32 7d 20 54 68 65 20 63 6f 72  {F17082} The cor
3fb70 65 20 6f 6e 6c 79 0a 2a 2a 20 70 72 6f 76 69 64  e only.** provid
3fb80 65 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  es implementatio
3fb90 6e 73 20 66 6f 72 20 74 68 65 73 65 20 72 6f 75  ns for these rou
3fba0 74 69 6e 65 73 20 77 68 65 6e 20 69 74 20 69 73  tines when it is
3fbb0 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 77 69 74   compiled.** wit
3fbc0 68 20 74 68 65 20 53 51 4c 49 54 45 5f 44 45 42  h the SQLITE_DEB
3fbd0 55 47 20 66 6c 61 67 2e 20 20 7b 55 31 37 30 38  UG flag.  {U1708
3fbe0 37 7d 20 45 78 74 65 72 6e 61 6c 20 6d 75 74 65  7} External mute
3fbf0 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  x implementation
3fc00 73 0a 2a 2a 20 61 72 65 20 6f 6e 6c 79 20 72 65  s.** are only re
3fc10 71 75 69 72 65 64 20 74 6f 20 70 72 6f 76 69 64  quired to provid
3fc20 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  e these routines
3fc30 20 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47   if SQLITE_DEBUG
3fc40 20 69 73 0a 2a 2a 20 64 65 66 69 6e 65 64 20 61   is.** defined a
3fc50 6e 64 20 69 66 20 4e 44 45 42 55 47 20 69 73 20  nd if NDEBUG is 
3fc60 6e 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a  not defined..**.
3fc70 2a 2a 20 7b 46 31 37 30 38 33 7d 20 54 68 65 73  ** {F17083} Thes
3fc80 65 20 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c  e routines shoul
3fc90 64 20 72 65 74 75 72 6e 20 74 72 75 65 20 69 66  d return true if
3fca0 20 74 68 65 20 6d 75 74 65 78 20 69 6e 20 74 68   the mutex in th
3fcb0 65 69 72 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  eir argument.** 
3fcc0 69 73 20 68 65 6c 64 20 6f 72 20 6e 6f 74 20 68  is held or not h
3fcd0 65 6c 64 2c 20 72 65 73 70 65 63 74 69 76 65 6c  eld, respectivel
3fce0 79 2c 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e  y, by the callin
3fcf0 67 20 74 68 72 65 61 64 2e 20 7b 45 4e 44 7d 0a  g thread. {END}.
3fd00 2a 2a 0a 2a 2a 20 7b 58 31 37 30 38 34 7d 20 54  **.** {X17084} T
3fd10 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
3fd20 6e 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65  n is not require
3fd30 64 20 74 6f 20 70 72 6f 76 69 64 65 64 20 76 65  d to provided ve
3fd40 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 73 65 0a  rsions of these.
3fd50 2a 2a 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74  ** routines that
3fd60 20 61 63 74 75 61 6c 6c 79 20 77 6f 72 6b 2e 0a   actually work..
3fd70 2a 2a 20 49 66 20 74 68 65 20 69 6d 70 6c 65 6d  ** If the implem
3fd80 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f  entation does no
3fd90 74 20 70 72 6f 76 69 64 65 20 77 6f 72 6b 69 6e  t provide workin
3fda0 67 0a 2a 2a 20 76 65 72 73 69 6f 6e 73 20 6f 66  g.** versions of
3fdb0 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2c   these routines,
3fdc0 20 69 74 20 73 68 6f 75 6c 64 20 61 74 20 6c 65   it should at le
3fdd0 61 73 74 20 70 72 6f 76 69 64 65 20 73 74 75 62  ast provide stub
3fde0 73 0a 2a 2a 20 74 68 61 74 20 61 6c 77 61 79 73  s.** that always
3fdf0 20 72 65 74 75 72 6e 20 74 72 75 65 20 73 6f 20   return true so 
3fe00 74 68 61 74 20 6f 6e 65 20 64 6f 65 73 20 6e 6f  that one does no
3fe10 74 20 67 65 74 20 73 70 75 72 69 6f 75 73 0a 2a  t get spurious.*
3fe20 2a 20 61 73 73 65 72 74 69 6f 6e 20 66 61 69 6c  * assertion fail
3fe30 75 72 65 73 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a  ures. {END}.**.*
3fe40 2a 20 7b 46 31 37 30 38 35 7d 20 49 66 20 74 68  * {F17085} If th
3fe50 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71  e argument to sq
3fe60 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
3fe70 28 29 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69  () is a NULL poi
3fe80 6e 74 65 72 20 74 68 65 6e 0a 2a 2a 20 74 68 65  nter then.** the
3fe90 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20   routine should 
3fea0 72 65 74 75 72 6e 20 31 2e 20 20 7b 45 4e 44 7d  return 1.  {END}
3feb0 20 54 68 69 73 20 73 65 65 6d 73 20 63 6f 75 6e   This seems coun
3fec0 74 65 72 2d 69 6e 74 75 69 74 69 76 65 20 73 69  ter-intuitive si
3fed0 6e 63 65 0a 2a 2a 20 63 6c 65 61 72 6c 79 20 74  nce.** clearly t
3fee0 68 65 20 6d 75 74 65 78 20 63 61 6e 6e 6f 74 20  he mutex cannot 
3fef0 62 65 20 68 65 6c 64 20 69 66 20 69 74 20 64 6f  be held if it do
3ff00 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 20 42  es not exist.  B
3ff10 75 74 20 74 68 65 0a 2a 2a 20 74 68 65 20 72 65  ut the.** the re
3ff20 61 73 6f 6e 20 74 68 65 20 6d 75 74 65 78 20 64  ason the mutex d
3ff30 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 73  oes not exist is
3ff40 20 62 65 63 61 75 73 65 20 74 68 65 20 62 75 69   because the bui
3ff50 6c 64 20 69 73 20 6e 6f 74 0a 2a 2a 20 75 73 69  ld is not.** usi
3ff60 6e 67 20 6d 75 74 65 78 65 73 2e 20 20 41 6e 64  ng mutexes.  And
3ff70 20 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20   we do not want 
3ff80 74 68 65 20 61 73 73 65 72 74 28 29 20 63 6f 6e  the assert() con
3ff90 74 61 69 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 63  taining the.** c
3ffa0 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d  all to sqlite3_m
3ffb0 75 74 65 78 5f 68 65 6c 64 28 29 20 74 6f 20 66  utex_held() to f
3ffc0 61 69 6c 2c 20 73 6f 20 61 20 6e 6f 6e 2d 7a 65  ail, so a non-ze
3ffd0 72 6f 20 72 65 74 75 72 6e 20 69 73 0a 2a 2a 20  ro return is.** 
3ffe0 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
3fff0 74 68 69 6e 67 20 74 6f 20 64 6f 2e 20 20 7b 46  thing to do.  {F
40000 31 37 30 38 36 7d 20 54 68 65 20 73 71 6c 69 74  17086} The sqlit
40010 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64  e3_mutex_notheld
40020 28 29 20 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  () .** interface
40030 20 73 68 6f 75 6c 64 20 61 6c 73 6f 20 72 65 74   should also ret
40040 75 72 6e 20 31 20 77 68 65 6e 20 67 69 76 65 6e  urn 1 when given
40050 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e   a NULL pointer.
40060 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
40070 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  nt sqlite3_mutex
40080 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75  _held(sqlite3_mu
40090 74 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  tex*);.SQLITE_AP
400a0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75  I int sqlite3_mu
400b0 74 65 78 5f 6e 6f 74 68 65 6c 64 28 73 71 6c 69  tex_notheld(sqli
400c0 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a  te3_mutex*);../*
400d0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75  .** CAPI3REF: Mu
400e0 74 65 78 20 54 79 70 65 73 20 7b 46 31 37 30 30  tex Types {F1700
400f0 31 7d 0a 2a 2a 0a 2a 2a 20 7b 46 31 37 30 30 32  1}.**.** {F17002
40100 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d  } The [sqlite3_m
40110 75 74 65 78 5f 61 6c 6c 6f 63 28 29 5d 20 69 6e  utex_alloc()] in
40120 74 65 72 66 61 63 65 20 74 61 6b 65 73 20 61 20  terface takes a 
40130 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 0a  single argument.
40140 2a 2a 20 77 68 69 63 68 20 69 73 20 6f 6e 65 20  ** which is one 
40150 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72  of these integer
40160 20 63 6f 6e 73 74 61 6e 74 73 2e 20 7b 45 4e 44   constants. {END
40170 7d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  }.*/.#define SQL
40180 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 20  ITE_MUTEX_FAST  
40190 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 64 65             0.#de
401a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45  fine SQLITE_MUTE
401b0 58 5f 52 45 43 55 52 53 49 56 45 20 20 20 20 20  X_RECURSIVE     
401c0 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c     1.#define SQL
401d0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
401e0 5f 4d 41 53 54 45 52 20 20 20 20 32 0a 23 64 65  _MASTER    2.#de
401f0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45  fine SQLITE_MUTE
40200 58 5f 53 54 41 54 49 43 5f 4d 45 4d 20 20 20 20  X_STATIC_MEM    
40210 20 20 20 33 20 20 2f 2a 20 73 71 6c 69 74 65 33     3  /* sqlite3
40220 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64 65  _malloc() */.#de
40230 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45  fine SQLITE_MUTE
40240 58 5f 53 54 41 54 49 43 5f 4d 45 4d 32 20 20 20  X_STATIC_MEM2   
40250 20 20 20 34 20 20 2f 2a 20 73 71 6c 69 74 65 33     4  /* sqlite3
40260 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28  _release_memory(
40270 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ) */.#define SQL
40280 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
40290 5f 50 52 4e 47 20 20 20 20 20 20 35 20 20 2f 2a  _PRNG      5  /*
402a0 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 28   sqlite3_random(
402b0 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ) */.#define SQL
402c0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
402d0 5f 4c 52 55 20 20 20 20 20 20 20 36 20 20 2f 2a  _LRU       6  /*
402e0 20 6c 72 75 20 70 61 67 65 20 6c 69 73 74 20 2a   lru page list *
402f0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
40300 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52  _MUTEX_STATIC_LR
40310 55 32 20 20 20 20 20 20 37 20 20 2f 2a 20 6c 72  U2      7  /* lr
40320 75 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 0a  u page list */..
40330 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
40340 4c 6f 77 2d 4c 65 76 65 6c 20 43 6f 6e 74 72 6f  Low-Level Contro
40350 6c 20 4f 66 20 44 61 74 61 62 61 73 65 20 46 69  l Of Database Fi
40360 6c 65 73 20 7b 46 31 31 33 30 30 7d 0a 2a 2a 0a  les {F11300}.**.
40370 2a 2a 20 7b 46 31 31 33 30 31 7d 20 54 68 65 20  ** {F11301} The 
40380 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f  [sqlite3_file_co
40390 6e 74 72 6f 6c 28 29 5d 20 69 6e 74 65 72 66 61  ntrol()] interfa
403a0 63 65 20 6d 61 6b 65 73 20 61 20 64 69 72 65 63  ce makes a direc
403b0 74 20 63 61 6c 6c 20 74 6f 20 74 68 65 0a 2a 2a  t call to the.**
403c0 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65   xFileControl me
403d0 74 68 6f 64 20 66 6f 72 20 74 68 65 20 5b 73 71  thod for the [sq
403e0 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
403f0 5d 20 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 61  ] object associa
40400 74 65 64 0a 2a 2a 20 77 69 74 68 20 61 20 70 61  ted.** with a pa
40410 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73  rticular databas
40420 65 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20  e identified by 
40430 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
40440 65 6e 74 2e 20 7b 46 31 31 33 30 32 7d 20 54 68  ent. {F11302} Th
40450 65 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65  e.** name of the
40460 20 64 61 74 61 62 61 73 65 20 69 73 20 74 68 65   database is the
40470 20 6e 61 6d 65 20 61 73 73 69 67 6e 65 64 20 74   name assigned t
40480 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62  o the database b
40490 79 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 65 66  y the.** <a href
404a0 3d 22 6c 61 6e 67 5f 61 74 74 61 63 68 2e 68 74  ="lang_attach.ht
404b0 6d 6c 22 3e 41 54 54 41 43 48 3c 2f 61 3e 20 53  ml">ATTACH</a> S
404c0 51 4c 20 63 6f 6d 6d 61 6e 64 20 74 68 61 74 20  QL command that 
404d0 6f 70 65 6e 65 64 20 74 68 65 0a 2a 2a 20 64 61  opened the.** da
404e0 74 61 62 61 73 65 2e 20 7b 46 31 31 33 30 33 7d  tabase. {F11303}
404f0 20 54 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20   To control the 
40500 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
40510 6c 65 2c 20 75 73 65 20 74 68 65 20 6e 61 6d 65  le, use the name
40520 20 22 6d 61 69 6e 22 0a 2a 2a 20 6f 72 20 61 20   "main".** or a 
40530 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 7b 46  NULL pointer. {F
40540 31 31 33 30 34 7d 20 54 68 65 20 74 68 69 72 64  11304} The third
40550 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61   and fourth para
40560 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 72  meters to this r
40570 6f 75 74 69 6e 65 0a 2a 2a 20 61 72 65 20 70 61  outine.** are pa
40580 73 73 65 64 20 64 69 72 65 63 74 6c 79 20 74 68  ssed directly th
40590 72 6f 75 67 68 20 74 6f 20 74 68 65 20 73 65 63  rough to the sec
405a0 6f 6e 64 20 61 6e 64 20 74 68 69 72 64 20 70 61  ond and third pa
405b0 72 61 6d 65 74 65 72 73 20 6f 66 0a 2a 2a 20 74  rameters of.** t
405c0 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20  he xFileControl 
405d0 6d 65 74 68 6f 64 2e 20 20 7b 46 31 31 33 30 35  method.  {F11305
405e0 7d 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c  } The return val
405f0 75 65 20 6f 66 20 74 68 65 20 78 46 69 6c 65 43  ue of the xFileC
40600 6f 6e 74 72 6f 6c 0a 2a 2a 20 6d 65 74 68 6f 64  ontrol.** method
40610 20 62 65 63 6f 6d 65 73 20 74 68 65 20 72 65 74   becomes the ret
40620 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 69  urn value of thi
40630 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a  s routine..**.**
40640 20 7b 46 31 31 33 30 36 7d 20 49 66 20 74 68 65   {F11306} If the
40650 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
40660 72 20 28 7a 44 62 4e 61 6d 65 29 20 64 6f 65 73  r (zDbName) does
40670 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20 6e   not match the n
40680 61 6d 65 20 6f 66 20 61 6e 79 0a 2a 2a 20 6f 70  ame of any.** op
40690 65 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  en database file
406a0 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 45 52  , then SQLITE_ER
406b0 52 4f 52 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ROR is returned.
406c0 20 7b 46 31 31 33 30 37 7d 20 54 68 69 73 20 65   {F11307} This e
406d0 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 73 20  rror.** code is 
406e0 6e 6f 74 20 72 65 6d 65 6d 62 65 72 65 64 20 61  not remembered a
406f0 6e 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 72  nd will not be r
40700 65 63 61 6c 6c 65 64 20 62 79 20 5b 73 71 6c 69  ecalled by [sqli
40710 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 0a 2a  te3_errcode()].*
40720 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 72  * or [sqlite3_er
40730 72 6d 73 67 28 29 5d 2e 20 7b 55 31 31 33 30 38  rmsg()]. {U11308
40740 7d 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  } The underlying
40750 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65   xFileControl me
40760 74 68 6f 64 20 6d 69 67 68 74 0a 2a 2a 20 61 6c  thod might.** al
40770 73 6f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  so return SQLITE
40780 5f 45 52 52 4f 52 2e 20 20 7b 55 31 31 33 30 39  _ERROR.  {U11309
40790 7d 20 54 68 65 72 65 20 69 73 20 6e 6f 20 77 61  } There is no wa
407a0 79 20 74 6f 20 64 69 73 74 69 6e 67 75 69 73 68  y to distinguish
407b0 20 62 65 74 77 65 65 6e 0a 2a 2a 20 61 6e 20 69   between.** an i
407c0 6e 63 6f 72 72 65 63 74 20 7a 44 62 4e 61 6d 65  ncorrect zDbName
407d0 20 61 6e 64 20 61 6e 20 53 51 4c 49 54 45 5f 45   and an SQLITE_E
407e0 52 52 4f 52 20 72 65 74 75 72 6e 20 66 72 6f 6d  RROR return from
407f0 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a   the underlying.
40800 2a 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20  ** xFileControl 
40810 6d 65 74 68 6f 64 2e 20 7b 45 4e 44 7d 0a 2a 2a  method. {END}.**
40820 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53  .** See also: [S
40830 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b  QLITE_FCNTL_LOCK
40840 53 54 41 54 45 5d 0a 2a 2f 0a 53 51 4c 49 54 45  STATE].*/.SQLITE
40850 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
40860 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 73 71  _file_control(sq
40870 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68  lite3*, const ch
40880 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 69 6e 74  ar *zDbName, int
40890 20 6f 70 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a   op, void*);../*
408a0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65  .** CAPI3REF: Te
408b0 73 74 69 6e 67 20 49 6e 74 65 72 66 61 63 65 20  sting Interface 
408c0 7b 46 31 31 34 30 30 7d 0a 2a 2a 0a 2a 2a 20 54  {F11400}.**.** T
408d0 68 65 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f  he sqlite3_test_
408e0 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 66  control() interf
408f0 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 72  ace is used to r
40900 65 61 64 20 6f 75 74 20 69 6e 74 65 72 6e 61 6c  ead out internal
40910 0a 2a 2a 20 73 74 61 74 65 20 6f 66 20 53 51 4c  .** state of SQL
40920 69 74 65 20 61 6e 64 20 74 6f 20 69 6e 6a 65 63  ite and to injec
40930 74 20 66 61 75 6c 74 73 20 69 6e 74 6f 20 53 51  t faults into SQ
40940 4c 69 74 65 20 66 6f 72 20 74 65 73 74 69 6e 67  Lite for testing
40950 0a 2a 2a 20 70 75 72 70 6f 73 65 73 2e 20 20 54  .** purposes.  T
40960 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  he first paramet
40970 65 72 20 61 20 6f 70 65 72 61 74 69 6f 6e 20 63  er a operation c
40980 6f 64 65 20 74 68 61 74 20 64 65 74 65 72 6d 69  ode that determi
40990 6e 65 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65  nes.** the numbe
409a0 72 2c 20 6d 65 61 6e 69 6e 67 2c 20 61 6e 64 20  r, meaning, and 
409b0 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 61 6c 6c  operation of all
409c0 20 73 75 62 73 65 71 75 65 6e 74 20 70 61 72 61   subsequent para
409d0 6d 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  meters..**.** Th
409e0 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  is interface is 
409f0 6e 6f 74 20 66 6f 72 20 75 73 65 20 62 79 20 61  not for use by a
40a00 70 70 6c 69 63 61 74 69 6f 6e 73 2e 20 20 49 74  pplications.  It
40a10 20 65 78 69 73 74 73 20 73 6f 6c 65 6c 79 0a 2a   exists solely.*
40a20 2a 20 66 6f 72 20 76 65 72 69 66 79 69 6e 67 20  * for verifying 
40a30 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72  the correct oper
40a40 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 53 51 4c  ation of the SQL
40a50 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20 44 65  ite library.  De
40a60 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20 68 6f  pending.** on ho
40a70 77 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62  w the SQLite lib
40a80 72 61 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 64  rary is compiled
40a90 2c 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65  , this interface
40aa0 20 6d 69 67 68 74 20 6e 6f 74 20 65 78 69 73 74   might not exist
40ab0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 74 61  ..**.** The deta
40ac0 69 6c 73 20 6f 66 20 74 68 65 20 6f 70 65 72 61  ils of the opera
40ad0 74 69 6f 6e 20 63 6f 64 65 73 2c 20 74 68 65 69  tion codes, thei
40ae0 72 20 6d 65 61 6e 69 6e 67 73 2c 20 74 68 65 20  r meanings, the 
40af0 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 68  parameters.** th
40b00 65 79 20 74 61 6b 65 2c 20 61 6e 64 20 77 68 61  ey take, and wha
40b10 74 20 74 68 65 79 20 64 6f 20 61 72 65 20 61 6c  t they do are al
40b20 6c 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61  l subject to cha
40b30 6e 67 65 20 77 69 74 68 6f 75 74 20 6e 6f 74 69  nge without noti
40b40 63 65 2e 0a 2a 2a 20 55 6e 6c 69 6b 65 20 6d 6f  ce..** Unlike mo
40b50 73 74 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65  st of the SQLite
40b60 20 41 50 49 2c 20 74 68 69 73 20 66 75 6e 63 74   API, this funct
40b70 69 6f 6e 20 69 73 20 6e 6f 74 20 67 75 61 72 61  ion is not guara
40b80 6e 74 65 65 64 20 74 6f 0a 2a 2a 20 6f 70 65 72  nteed to.** oper
40b90 61 74 65 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79  ate consistently
40ba0 20 66 72 6f 6d 20 6f 6e 65 20 72 65 6c 65 61 73   from one releas
40bb0 65 20 74 6f 20 74 68 65 20 6e 65 78 74 2e 0a 2a  e to the next..*
40bc0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
40bd0 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f   sqlite3_test_co
40be0 6e 74 72 6f 6c 28 69 6e 74 20 6f 70 2c 20 2e 2e  ntrol(int op, ..
40bf0 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .);../*.** CAPI3
40c00 52 45 46 3a 20 54 65 73 74 69 6e 67 20 49 6e 74  REF: Testing Int
40c10 65 72 66 61 63 65 20 4f 70 65 72 61 74 69 6f 6e  erface Operation
40c20 20 43 6f 64 65 73 20 7b 46 31 31 34 31 30 7d 0a   Codes {F11410}.
40c30 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73  **.** These cons
40c40 74 61 6e 74 73 20 61 72 65 20 74 68 65 20 76 61  tants are the va
40c50 6c 69 64 20 6f 70 65 72 61 74 69 6f 6e 20 63 6f  lid operation co
40c60 64 65 20 70 61 72 61 6d 65 74 65 72 73 20 75 73  de parameters us
40c70 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 72  ed.** as the fir
40c80 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 5b  st argument to [
40c90 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
40ca0 74 72 6f 6c 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54  trol()]..**.** T
40cb0 68 65 73 65 20 70 61 72 61 6d 65 74 65 72 73 20  hese parameters 
40cc0 61 6e 64 20 74 68 65 69 72 20 6d 65 61 6e 73 69  and their meansi
40cd0 6e 67 20 61 72 65 20 73 75 62 6a 65 63 74 20 74  ng are subject t
40ce0 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 77 69 74 68  o change.** with
40cf0 6f 75 74 20 6e 6f 74 69 63 65 2e 20 20 54 68 65  out notice.  The
40d00 73 65 20 76 61 6c 75 65 73 20 61 72 65 20 66 6f  se values are fo
40d10 72 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73  r testing purpos
40d20 65 73 20 6f 6e 6c 79 2e 0a 2a 2a 20 41 70 70 6c  es only..** Appl
40d30 69 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20  ications should 
40d40 6e 6f 74 20 75 73 65 20 61 6e 79 20 6f 66 20 74  not use any of t
40d50 68 65 73 65 20 70 61 72 61 6d 65 74 65 72 73 20  hese parameters 
40d60 6f 72 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74  or the.** [sqlit
40d70 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28  e3_test_control(
40d80 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f  )] interface..*/
40d90 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
40da0 54 45 53 54 43 54 52 4c 5f 46 41 55 4c 54 5f 43  TESTCTRL_FAULT_C
40db0 4f 4e 46 49 47 20 20 20 20 20 20 20 20 20 20 20  ONFIG           
40dc0 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    1.#define SQLI
40dd0 54 45 5f 54 45 53 54 43 54 52 4c 5f 46 41 55 4c  TE_TESTCTRL_FAUL
40de0 54 5f 46 41 49 4c 55 52 45 53 20 20 20 20 20 20  T_FAILURES      
40df0 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53       2.#define S
40e00 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 46  QLITE_TESTCTRL_F
40e10 41 55 4c 54 5f 42 45 4e 49 47 4e 5f 46 41 49 4c  AULT_BENIGN_FAIL
40e20 55 52 45 53 20 20 20 20 33 0a 23 64 65 66 69 6e  URES    3.#defin
40e30 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52  e SQLITE_TESTCTR
40e40 4c 5f 46 41 55 4c 54 5f 50 45 4e 44 49 4e 47 20  L_FAULT_PENDING 
40e50 20 20 20 20 20 20 20 20 20 20 20 34 0a 23 64 65             4.#de
40e60 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54  fine SQLITE_TEST
40e70 43 54 52 4c 5f 50 52 4e 47 5f 53 41 56 45 20 20  CTRL_PRNG_SAVE  
40e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a                5.
40e90 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54  #define SQLITE_T
40ea0 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53  ESTCTRL_PRNG_RES
40eb0 54 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 20  TORE            
40ec0 20 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   6.#define SQLIT
40ed0 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f  E_TESTCTRL_PRNG_
40ee0 52 45 53 45 54 20 20 20 20 20 20 20 20 20 20 20  RESET           
40ef0 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 53 51      7.#define SQ
40f00 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 42 49  LITE_TESTCTRL_BI
40f10 54 56 45 43 5f 54 45 53 54 20 20 20 20 20 20 20  TVEC_TEST       
40f20 20 20 20 20 20 20 20 38 0a 0a 0a 2f 2a 0a 2a 2a         8.../*.**
40f30 20 55 6e 64 6f 20 74 68 65 20 68 61 63 6b 20 74   Undo the hack t
40f40 68 61 74 20 63 6f 6e 76 65 72 74 73 20 66 6c 6f  hat converts flo
40f50 61 74 69 6e 67 20 70 6f 69 6e 74 20 74 79 70 65  ating point type
40f60 73 20 74 6f 20 69 6e 74 65 67 65 72 20 66 6f 72  s to integer for
40f70 0a 2a 2a 20 62 75 69 6c 64 73 20 6f 6e 20 70 72  .** builds on pr
40f80 6f 63 65 73 73 6f 72 73 20 77 69 74 68 6f 75 74  ocessors without
40f90 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20   floating point 
40fa0 73 75 70 70 6f 72 74 2e 0a 2a 2f 0a 23 69 66 64  support..*/.#ifd
40fb0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
40fc0 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20  LOATING_POINT.# 
40fd0 75 6e 64 65 66 20 64 6f 75 62 6c 65 0a 23 65 6e  undef double.#en
40fe0 64 69 66 0a 0a 23 69 66 20 30 0a 7d 20 20 2f 2a  dif..#if 0.}  /*
40ff0 20 45 6e 64 20 6f 66 20 74 68 65 20 27 65 78 74   End of the 'ext
41000 65 72 6e 20 22 43 22 27 20 62 6c 6f 63 6b 20 2a  ern "C"' block *
41010 2f 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  /.#endif.#endif.
41020 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
41030 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 2e   End of sqlite3.
41040 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
41050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
41060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
41070 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
41080 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
41090 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
410a0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
410b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
410c0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
410d0 20 49 6e 63 6c 75 64 65 20 68 61 73 68 2e 68 20   Include hash.h 
410e0 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
410f0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
41100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
41110 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
41120 20 42 65 67 69 6e 20 66 69 6c 65 20 68 61 73 68   Begin file hash
41130 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
41140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
41150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
41160 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
41170 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54  ember 22.**.** T
41180 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
41190 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
411a0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
411b0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
411c0 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
411d0 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
411e0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
411f0 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
41200 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
41210 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
41220 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
41230 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
41240 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
41250 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
41260 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
41270 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
41280 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
41290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
412a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
412b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
412c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
412d0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
412e0 69 73 20 74 68 65 20 68 65 61 64 65 72 20 66 69  is the header fi
412f0 6c 65 20 66 6f 72 20 74 68 65 20 67 65 6e 65 72  le for the gener
41300 69 63 20 68 61 73 68 2d 74 61 62 6c 65 20 69 6d  ic hash-table im
41310 70 6c 65 6d 65 6e 61 74 69 6f 6e 0a 2a 2a 20 75  plemenation.** u
41320 73 65 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a  sed in SQLite..*
41330 2a 0a 2a 2a 20 24 49 64 3a 20 68 61 73 68 2e 68  *.** $Id: hash.h
41340 2c 76 20 31 2e 31 31 20 32 30 30 37 2f 30 39 2f  ,v 1.11 2007/09/
41350 30 34 20 31 34 3a 33 31 3a 34 37 20 64 61 6e 69  04 14:31:47 dani
41360 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f  elk1977 Exp $.*/
41370 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45  .#ifndef _SQLITE
41380 5f 48 41 53 48 5f 48 5f 0a 23 64 65 66 69 6e 65  _HASH_H_.#define
41390 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f   _SQLITE_HASH_H_
413a0 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63  ../* Forward dec
413b0 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 73 74 72  larations of str
413c0 75 63 74 75 72 65 73 2e 20 2a 2f 0a 74 79 70 65  uctures. */.type
413d0 64 65 66 20 73 74 72 75 63 74 20 48 61 73 68 20  def struct Hash 
413e0 48 61 73 68 3b 0a 74 79 70 65 64 65 66 20 73 74  Hash;.typedef st
413f0 72 75 63 74 20 48 61 73 68 45 6c 65 6d 20 48 61  ruct HashElem Ha
41400 73 68 45 6c 65 6d 3b 0a 0a 2f 2a 20 41 20 63 6f  shElem;../* A co
41410 6d 70 6c 65 74 65 20 68 61 73 68 20 74 61 62 6c  mplete hash tabl
41420 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  e is an instance
41430 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
41440 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20  g structure..** 
41450 54 68 65 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66  The internals of
41460 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
41470 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f 20  are intended to 
41480 62 65 20 6f 70 61 71 75 65 20 2d 2d 20 63 6c 69  be opaque -- cli
41490 65 6e 74 0a 2a 2a 20 63 6f 64 65 20 73 68 6f 75  ent.** code shou
414a0 6c 64 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74  ld not attempt t
414b0 6f 20 61 63 63 65 73 73 20 6f 72 20 6d 6f 64 69  o access or modi
414c0 66 79 20 74 68 65 20 66 69 65 6c 64 73 20 6f 66  fy the fields of
414d0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 0a   this structure.
414e0 2a 2a 20 64 69 72 65 63 74 6c 79 2e 20 20 43 68  ** directly.  Ch
414f0 61 6e 67 65 20 74 68 69 73 20 73 74 72 75 63 74  ange this struct
41500 75 72 65 20 6f 6e 6c 79 20 62 79 20 75 73 69 6e  ure only by usin
41510 67 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 62  g the routines b
41520 65 6c 6f 77 2e 0a 2a 2a 20 48 6f 77 65 76 65 72  elow..** However
41530 2c 20 6d 61 6e 79 20 6f 66 20 74 68 65 20 22 70  , many of the "p
41540 72 6f 63 65 64 75 72 65 73 22 20 61 6e 64 20 22  rocedures" and "
41550 66 75 6e 63 74 69 6f 6e 73 22 20 66 6f 72 20 6d  functions" for m
41560 6f 64 69 66 79 69 6e 67 20 61 6e 64 0a 2a 2a 20  odifying and.** 
41570 61 63 63 65 73 73 69 6e 67 20 74 68 69 73 20 73  accessing this s
41580 74 72 75 63 74 75 72 65 20 61 72 65 20 72 65 61  tructure are rea
41590 6c 6c 79 20 6d 61 63 72 6f 73 2c 20 73 6f 20 77  lly macros, so w
415a0 65 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 20 6d  e can't really m
415b0 61 6b 65 0a 2a 2a 20 74 68 69 73 20 73 74 72 75  ake.** this stru
415c0 63 74 75 72 65 20 6f 70 61 71 75 65 2e 0a 2a 2f  cture opaque..*/
415d0 0a 73 74 72 75 63 74 20 48 61 73 68 20 7b 0a 20  .struct Hash {. 
415e0 20 63 68 61 72 20 6b 65 79 43 6c 61 73 73 3b 20   char keyClass; 
415f0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 49           /* SQLI
41600 54 45 5f 48 41 53 48 5f 49 4e 54 2c 20 5f 50 4f  TE_HASH_INT, _PO
41610 49 4e 54 45 52 2c 20 5f 53 54 52 49 4e 47 2c 20  INTER, _STRING, 
41620 5f 42 49 4e 41 52 59 20 2a 2f 0a 20 20 63 68 61  _BINARY */.  cha
41630 72 20 63 6f 70 79 4b 65 79 3b 20 20 20 20 20 20  r copyKey;      
41640 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
41650 63 6f 70 79 20 6f 66 20 6b 65 79 20 6d 61 64 65  copy of key made
41660 20 6f 6e 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20   on insert */.  
41670 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20  int count;      
41680 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
41690 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
416a0 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20  this table */.  
416b0 69 6e 74 20 68 74 73 69 7a 65 3b 20 20 20 20 20  int htsize;     
416c0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
416d0 72 20 6f 66 20 62 75 63 6b 65 74 73 20 69 6e 20  r of buckets in 
416e0 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a  the hash table *
416f0 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 66 69  /.  HashElem *fi
41700 72 73 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  rst;        /* T
41710 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74  he first element
41720 20 6f 66 20 74 68 65 20 61 72 72 61 79 20 2a 2f   of the array */
41730 0a 20 20 73 74 72 75 63 74 20 5f 68 74 20 7b 20  .  struct _ht { 
41740 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68             /* th
41750 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a  e hash table */.
41760 20 20 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20      int count;  
41770 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
41780 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
41790 73 20 77 69 74 68 20 74 68 69 73 20 68 61 73 68  s with this hash
417a0 20 2a 2f 0a 20 20 20 20 48 61 73 68 45 6c 65 6d   */.    HashElem
417b0 20 2a 63 68 61 69 6e 3b 20 20 20 20 20 20 20 20   *chain;        
417c0 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66   /* Pointer to f
417d0 69 72 73 74 20 65 6e 74 72 79 20 77 69 74 68 20  irst entry with 
417e0 74 68 69 73 20 68 61 73 68 20 2a 2f 0a 20 20 7d  this hash */.  }
417f0 20 2a 68 74 3b 0a 7d 3b 0a 0a 2f 2a 20 45 61 63   *ht;.};../* Eac
41800 68 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65  h element in the
41810 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 61   hash table is a
41820 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
41830 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20  e following .** 
41840 73 74 72 75 63 74 75 72 65 2e 20 20 41 6c 6c 20  structure.  All 
41850 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 73 74 6f  elements are sto
41860 72 65 64 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20  red on a single 
41870 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69  doubly-linked li
41880 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 67 61 69 6e 2c  st..**.** Again,
41890 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
418a0 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62  is intended to b
418b0 65 20 6f 70 61 71 75 65 2c 20 62 75 74 20 69 74  e opaque, but it
418c0 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 0a 2a 2a   can't really.**
418d0 20 62 65 20 6f 70 61 71 75 65 20 62 65 63 61 75   be opaque becau
418e0 73 65 20 69 74 20 69 73 20 75 73 65 64 20 62 79  se it is used by
418f0 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 73 74 72 75   macros..*/.stru
41900 63 74 20 48 61 73 68 45 6c 65 6d 20 7b 0a 20 20  ct HashElem {.  
41910 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78 74 2c 20  HashElem *next, 
41920 2a 70 72 65 76 3b 20 20 20 2f 2a 20 4e 65 78 74  *prev;   /* Next
41930 20 61 6e 64 20 70 72 65 76 69 6f 75 73 20 65 6c   and previous el
41940 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 74 61  ements in the ta
41950 62 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 64  ble */.  void *d
41960 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20  ata;            
41970 20 20 2f 2a 20 44 61 74 61 20 61 73 73 6f 63 69    /* Data associ
41980 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 65  ated with this e
41990 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 76 6f 69 64  lement */.  void
419a0 20 2a 70 4b 65 79 3b 20 69 6e 74 20 6e 4b 65 79   *pKey; int nKey
419b0 3b 20 20 20 20 2f 2a 20 4b 65 79 20 61 73 73 6f  ;    /* Key asso
419c0 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73  ciated with this
419d0 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a   element */.};..
419e0 2f 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20  /*.** There are 
419f0 34 20 64 69 66 66 65 72 65 6e 74 20 6d 6f 64 65  4 different mode
41a00 73 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 20 66  s of operation f
41a10 6f 72 20 61 20 68 61 73 68 20 74 61 62 6c 65 3a  or a hash table:
41a20 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f  .**.**   SQLITE_
41a30 48 41 53 48 5f 49 4e 54 20 20 20 20 20 20 20 20  HASH_INT        
41a40 20 6e 4b 65 79 20 69 73 20 75 73 65 64 20 61 73   nKey is used as
41a50 20 74 68 65 20 6b 65 79 20 61 6e 64 20 70 4b 65   the key and pKe
41a60 79 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a  y is ignored..**
41a70 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 48 41 53  .**   SQLITE_HAS
41a80 48 5f 50 4f 49 4e 54 45 52 20 20 20 20 20 70 4b  H_POINTER     pK
41a90 65 79 20 69 73 20 75 73 65 64 20 61 73 20 74 68  ey is used as th
41aa0 65 20 6b 65 79 20 61 6e 64 20 6e 4b 65 79 20 69  e key and nKey i
41ab0 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a  s ignored..**.**
41ac0 20 20 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 53     SQLITE_HASH_S
41ad0 54 52 49 4e 47 20 20 20 20 20 20 70 4b 65 79 20  TRING      pKey 
41ae0 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 74 72 69  points to a stri
41af0 6e 67 20 74 68 61 74 20 69 73 20 6e 4b 65 79 20  ng that is nKey 
41b00 62 79 74 65 73 20 6c 6f 6e 67 0a 2a 2a 20 20 20  bytes long.**   
41b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41b20 20 20 20 20 20 20 20 20 28 69 6e 63 6c 75 64 69          (includi
41b30 6e 67 20 74 68 65 20 6e 75 6c 6c 2d 74 65 72 6d  ng the null-term
41b40 69 6e 61 74 6f 72 2c 20 69 66 20 61 6e 79 29 2e  inator, if any).
41b50 20 20 43 61 73 65 0a 2a 2a 20 20 20 20 20 20 20    Case.**       
41b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41b70 20 20 20 20 69 73 20 69 67 6e 6f 72 65 64 20 69      is ignored i
41b80 6e 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a  n comparisons..*
41b90 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 48 41  *.**   SQLITE_HA
41ba0 53 48 5f 42 49 4e 41 52 59 20 20 20 20 20 20 70  SH_BINARY      p
41bb0 4b 65 79 20 70 6f 69 6e 74 73 20 74 6f 20 62 69  Key points to bi
41bc0 6e 61 72 79 20 64 61 74 61 20 6e 4b 65 79 20 62  nary data nKey b
41bd0 79 74 65 73 20 6c 6f 6e 67 2e 20 0a 2a 2a 20 20  ytes long. .**  
41be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41bf0 20 20 20 20 20 20 20 20 20 6d 65 6d 63 6d 70 28           memcmp(
41c00 29 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d  ) is used to com
41c10 70 61 72 65 20 6b 65 79 73 2e 0a 2a 2a 0a 2a 2a  pare keys..**.**
41c20 20 41 20 63 6f 70 79 20 6f 66 20 74 68 65 20 6b   A copy of the k
41c30 65 79 20 69 73 20 6d 61 64 65 20 66 6f 72 20 53  ey is made for S
41c40 51 4c 49 54 45 5f 48 41 53 48 5f 53 54 52 49 4e  QLITE_HASH_STRIN
41c50 47 20 61 6e 64 20 53 51 4c 49 54 45 5f 48 41 53  G and SQLITE_HAS
41c60 48 5f 42 49 4e 41 52 59 0a 2a 2a 20 69 66 20 74  H_BINARY.** if t
41c70 68 65 20 63 6f 70 79 4b 65 79 20 70 61 72 61 6d  he copyKey param
41c80 65 74 65 72 20 74 6f 20 48 61 73 68 49 6e 69 74  eter to HashInit
41c90 20 69 73 20 31 2e 20 20 0a 2a 2f 0a 2f 2a 20 23   is 1.  .*/./* #
41ca0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 48 41  define SQLITE_HA
41cb0 53 48 5f 49 4e 54 20 20 20 20 20 20 20 31 20 2f  SH_INT       1 /
41cc0 2f 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 2f 2a  / NOT USED */./*
41cd0 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f   #define SQLITE_
41ce0 48 41 53 48 5f 50 4f 49 4e 54 45 52 20 20 20 32  HASH_POINTER   2
41cf0 20 2f 2f 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a   // NOT USED */.
41d00 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 48  #define SQLITE_H
41d10 41 53 48 5f 53 54 52 49 4e 47 20 20 20 20 33 0a  ASH_STRING    3.
41d20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 48  #define SQLITE_H
41d30 41 53 48 5f 42 49 4e 41 52 59 20 20 20 20 34 0a  ASH_BINARY    4.
41d40 0a 2f 2a 0a 2a 2a 20 41 63 63 65 73 73 20 72 6f  ./*.** Access ro
41d50 75 74 69 6e 65 73 2e 20 20 54 6f 20 64 65 6c 65  utines.  To dele
41d60 74 65 2c 20 69 6e 73 65 72 74 20 61 20 4e 55 4c  te, insert a NUL
41d70 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51  L pointer..*/.SQ
41d80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
41d90 64 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69  d sqlite3HashIni
41da0 74 28 48 61 73 68 2a 2c 20 69 6e 74 20 6b 65 79  t(Hash*, int key
41db0 74 79 70 65 2c 20 69 6e 74 20 63 6f 70 79 4b 65  type, int copyKe
41dc0 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  y);.SQLITE_PRIVA
41dd0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
41de0 48 61 73 68 49 6e 73 65 72 74 28 48 61 73 68 2a  HashInsert(Hash*
41df0 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b  , const void *pK
41e00 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 76 6f  ey, int nKey, vo
41e10 69 64 20 2a 70 44 61 74 61 29 3b 0a 53 51 4c 49  id *pData);.SQLI
41e20 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
41e30 2a 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64  *sqlite3HashFind
41e40 28 63 6f 6e 73 74 20 48 61 73 68 2a 2c 20 63 6f  (const Hash*, co
41e50 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
41e60 69 6e 74 20 6e 4b 65 79 29 3b 0a 53 51 4c 49 54  int nKey);.SQLIT
41e70 45 5f 50 52 49 56 41 54 45 20 48 61 73 68 45 6c  E_PRIVATE HashEl
41e80 65 6d 20 2a 73 71 6c 69 74 65 33 48 61 73 68 46  em *sqlite3HashF
41e90 69 6e 64 45 6c 65 6d 28 63 6f 6e 73 74 20 48 61  indElem(const Ha
41ea0 73 68 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  sh*, const void 
41eb0 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29  *pKey, int nKey)
41ec0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
41ed0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73   void sqlite3Has
41ee0 68 43 6c 65 61 72 28 48 61 73 68 2a 29 3b 0a 0a  hClear(Hash*);..
41ef0 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72  /*.** Macros for
41f00 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c   looping over al
41f10 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 61 20  l elements of a 
41f20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68 65  hash table.  The
41f30 20 69 64 69 6f 6d 20 69 73 0a 2a 2a 20 6c 69 6b   idiom is.** lik
41f40 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
41f50 48 61 73 68 20 68 3b 0a 2a 2a 20 20 20 48 61 73  Hash h;.**   Has
41f60 68 45 6c 65 6d 20 2a 70 3b 0a 2a 2a 20 20 20 2e  hElem *p;.**   .
41f70 2e 2e 0a 2a 2a 20 20 20 66 6f 72 28 70 3d 73 71  ...**   for(p=sq
41f80 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26 68  liteHashFirst(&h
41f90 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65 48 61  ); p; p=sqliteHa
41fa0 73 68 4e 65 78 74 28 70 29 29 7b 0a 2a 2a 20 20  shNext(p)){.**  
41fb0 20 20 20 53 6f 6d 65 53 74 72 75 63 74 75 72 65     SomeStructure
41fc0 20 2a 70 44 61 74 61 20 3d 20 73 71 6c 69 74 65   *pData = sqlite
41fd0 48 61 73 68 44 61 74 61 28 70 29 3b 0a 2a 2a 20  HashData(p);.** 
41fe0 20 20 20 20 2f 2f 20 64 6f 20 73 6f 6d 65 74 68      // do someth
41ff0 69 6e 67 20 77 69 74 68 20 70 44 61 74 61 0a 2a  ing with pData.*
42000 2a 20 20 20 7d 0a 2a 2f 0a 23 64 65 66 69 6e 65  *   }.*/.#define
42010 20 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74   sqliteHashFirst
42020 28 48 29 20 20 28 28 48 29 2d 3e 66 69 72 73 74  (H)  ((H)->first
42030 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  ).#define sqlite
42040 48 61 73 68 4e 65 78 74 28 45 29 20 20 20 28 28  HashNext(E)   ((
42050 45 29 2d 3e 6e 65 78 74 29 0a 23 64 65 66 69 6e  E)->next).#defin
42060 65 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 61  e sqliteHashData
42070 28 45 29 20 20 20 28 28 45 29 2d 3e 64 61 74 61  (E)   ((E)->data
42080 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  ).#define sqlite
42090 48 61 73 68 4b 65 79 28 45 29 20 20 20 20 28 28  HashKey(E)    ((
420a0 45 29 2d 3e 70 4b 65 79 29 0a 23 64 65 66 69 6e  E)->pKey).#defin
420b0 65 20 73 71 6c 69 74 65 48 61 73 68 4b 65 79 73  e sqliteHashKeys
420c0 69 7a 65 28 45 29 20 28 28 45 29 2d 3e 6e 4b 65  ize(E) ((E)->nKe
420d0 79 29 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72  y)../*.** Number
420e0 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
420f0 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2f 0a 23   hash table.*/.#
42100 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73  define sqliteHas
42110 68 43 6f 75 6e 74 28 48 29 20 20 28 28 48 29 2d  hCount(H)  ((H)-
42120 3e 63 6f 75 6e 74 29 0a 0a 23 65 6e 64 69 66 20  >count)..#endif 
42130 2f 2a 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f  /* _SQLITE_HASH_
42140 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  H_ */../********
42150 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 61  ****** End of ha
42160 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  sh.h ***********
42170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42190 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
421a0 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
421b0 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
421c0 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74  off in sqliteInt
421d0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
421e0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
421f0 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70  ****** Include p
42200 61 72 73 65 2e 68 20 69 6e 20 74 68 65 20 6d 69  arse.h in the mi
42210 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e  ddle of sqliteIn
42220 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
42230 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
42240 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
42250 65 20 70 61 72 73 65 2e 68 20 2a 2a 2a 2a 2a 2a  e parse.h ******
42260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42280 2a 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 54  *****/.#define T
42290 4b 5f 53 45 4d 49 20 20 20 20 20 20 20 20 20 20  K_SEMI          
422a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
422b0 20 20 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45    1.#define TK_E
422c0 58 50 4c 41 49 4e 20 20 20 20 20 20 20 20 20 20  XPLAIN          
422d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
422e0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 51 55 45 52  .#define TK_QUER
422f0 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Y               
42300 20 20 20 20 20 20 20 20 20 20 20 20 33 0a 23 64              3.#d
42310 65 66 69 6e 65 20 54 4b 5f 50 4c 41 4e 20 20 20  efine TK_PLAN   
42320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42330 20 20 20 20 20 20 20 20 20 34 0a 23 64 65 66 69           4.#defi
42340 6e 65 20 54 4b 5f 42 45 47 49 4e 20 20 20 20 20  ne TK_BEGIN     
42350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42360 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20        5.#define 
42370 54 4b 5f 54 52 41 4e 53 41 43 54 49 4f 4e 20 20  TK_TRANSACTION  
42380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42390 20 20 20 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f     6.#define TK_
423a0 44 45 46 45 52 52 45 44 20 20 20 20 20 20 20 20  DEFERRED        
423b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
423c0 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4d 4d  7.#define TK_IMM
423d0 45 44 49 41 54 45 20 20 20 20 20 20 20 20 20 20  EDIATE          
423e0 20 20 20 20 20 20 20 20 20 20 20 20 20 38 0a 23               8.#
423f0 64 65 66 69 6e 65 20 54 4b 5f 45 58 43 4c 55 53  define TK_EXCLUS
42400 49 56 45 20 20 20 20 20 20 20 20 20 20 20 20 20  IVE             
42410 20 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 66            9.#def
42420 69 6e 65 20 54 4b 5f 43 4f 4d 4d 49 54 20 20 20  ine TK_COMMIT   
42430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42440 20 20 20 20 20 20 31 30 0a 23 64 65 66 69 6e 65        10.#define
42450 20 54 4b 5f 45 4e 44 20 20 20 20 20 20 20 20 20   TK_END         
42460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42470 20 20 20 31 31 0a 23 64 65 66 69 6e 65 20 54 4b     11.#define TK
42480 5f 52 4f 4c 4c 42 41 43 4b 20 20 20 20 20 20 20  _ROLLBACK       
42490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
424a0 31 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 52  12.#define TK_CR
424b0 45 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20  EATE            
424c0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 0a               13.
424d0 23 64 65 66 69 6e 65 20 54 4b 5f 54 41 42 4c 45  #define TK_TABLE
424e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
424f0 20 20 20 20 20 20 20 20 20 20 31 34 0a 23 64 65            14.#de
42500 66 69 6e 65 20 54 4b 5f 49 46 20 20 20 20 20 20  fine TK_IF      
42510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42520 20 20 20 20 20 20 20 31 35 0a 23 64 65 66 69 6e         15.#defin
42530 65 20 54 4b 5f 4e 4f 54 20 20 20 20 20 20 20 20  e TK_NOT        
42540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42550 20 20 20 20 31 36 0a 23 64 65 66 69 6e 65 20 54      16.#define T
42560 4b 5f 45 58 49 53 54 53 20 20 20 20 20 20 20 20  K_EXISTS        
42570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42580 20 31 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54   17.#define TK_T
42590 45 4d 50 20 20 20 20 20 20 20 20 20 20 20 20 20  EMP             
425a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 38                18
425b0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 50 20 20  .#define TK_LP  
425c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
425d0 20 20 20 20 20 20 20 20 20 20 20 31 39 0a 23 64             19.#d
425e0 65 66 69 6e 65 20 54 4b 5f 52 50 20 20 20 20 20  efine TK_RP     
425f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42600 20 20 20 20 20 20 20 20 32 30 0a 23 64 65 66 69          20.#defi
42610 6e 65 20 54 4b 5f 41 53 20 20 20 20 20 20 20 20  ne TK_AS        
42620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42630 20 20 20 20 20 32 31 0a 23 64 65 66 69 6e 65 20       21.#define 
42640 54 4b 5f 43 4f 4d 4d 41 20 20 20 20 20 20 20 20  TK_COMMA        
42650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42660 20 20 32 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f    22.#define TK_
42670 49 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ID              
42680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
42690 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 42 4f  3.#define TK_ABO
426a0 52 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  RT              
426b0 20 20 20 20 20 20 20 20 20 20 20 20 32 34 0a 23              24.#
426c0 64 65 66 69 6e 65 20 54 4b 5f 41 46 54 45 52 20  define TK_AFTER 
426d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
426e0 20 20 20 20 20 20 20 20 20 32 35 0a 23 64 65 66           25.#def
426f0 69 6e 65 20 54 4b 5f 41 4e 41 4c 59 5a 45 20 20  ine TK_ANALYZE  
42700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42710 20 20 20 20 20 20 32 36 0a 23 64 65 66 69 6e 65        26.#define
42720 20 54 4b 5f 41 53 43 20 20 20 20 20 20 20 20 20   TK_ASC         
42730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42740 20 20 20 32 37 0a 23 64 65 66 69 6e 65 20 54 4b     27.#define TK
42750 5f 41 54 54 41 43 48 20 20 20 20 20 20 20 20 20  _ATTACH         
42760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42770 32 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 45  28.#define TK_BE
42780 46 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 20  FORE            
42790 20 20 20 20 20 20 20 20 20 20 20 20 20 32 39 0a               29.
427a0 23 64 65 66 69 6e 65 20 54 4b 5f 43 41 53 43 41  #define TK_CASCA
427b0 44 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  DE              
427c0 20 20 20 20 20 20 20 20 20 20 33 30 0a 23 64 65            30.#de
427d0 66 69 6e 65 20 54 4b 5f 43 41 53 54 20 20 20 20  fine TK_CAST    
427e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
427f0 20 20 20 20 20 20 20 33 31 0a 23 64 65 66 69 6e         31.#defin
42800 65 20 54 4b 5f 43 4f 4e 46 4c 49 43 54 20 20 20  e TK_CONFLICT   
42810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42820 20 20 20 20 33 32 0a 23 64 65 66 69 6e 65 20 54      32.#define T
42830 4b 5f 44 41 54 41 42 41 53 45 20 20 20 20 20 20  K_DATABASE      
42840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42850 20 33 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44   33.#define TK_D
42860 45 53 43 20 20 20 20 20 20 20 20 20 20 20 20 20  ESC             
42870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 34                34
42880 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 54 41  .#define TK_DETA
42890 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20  CH              
428a0 20 20 20 20 20 20 20 20 20 20 20 33 35 0a 23 64             35.#d
428b0 65 66 69 6e 65 20 54 4b 5f 45 41 43 48 20 20 20  efine TK_EACH   
428c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
428d0 20 20 20 20 20 20 20 20 33 36 0a 23 64 65 66 69          36.#defi
428e0 6e 65 20 54 4b 5f 46 41 49 4c 20 20 20 20 20 20  ne TK_FAIL      
428f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42900 20 20 20 20 20 33 37 0a 23 64 65 66 69 6e 65 20       37.#define 
42910 54 4b 5f 46 4f 52 20 20 20 20 20 20 20 20 20 20  TK_FOR          
42920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42930 20 20 33 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f    38.#define TK_
42940 49 47 4e 4f 52 45 20 20 20 20 20 20 20 20 20 20  IGNORE          
42950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
42960 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 49  9.#define TK_INI
42970 54 49 41 4c 4c 59 20 20 20 20 20 20 20 20 20 20  TIALLY          
42980 20 20 20 20 20 20 20 20 20 20 20 20 34 30 0a 23              40.#
42990 64 65 66 69 6e 65 20 54 4b 5f 49 4e 53 54 45 41  define TK_INSTEA
429a0 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  D               
429b0 20 20 20 20 20 20 20 20 20 34 31 0a 23 64 65 66           41.#def
429c0 69 6e 65 20 54 4b 5f 4c 49 4b 45 5f 4b 57 20 20  ine TK_LIKE_KW  
429d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
429e0 20 20 20 20 20 20 34 32 0a 23 64 65 66 69 6e 65        42.#define
429f0 20 54 4b 5f 4d 41 54 43 48 20 20 20 20 20 20 20   TK_MATCH       
42a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42a10 20 20 20 34 33 0a 23 64 65 66 69 6e 65 20 54 4b     43.#define TK
42a20 5f 4b 45 59 20 20 20 20 20 20 20 20 20 20 20 20  _KEY            
42a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42a40 34 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 46  44.#define TK_OF
42a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42a60 20 20 20 20 20 20 20 20 20 20 20 20 20 34 35 0a               45.
42a70 23 64 65 66 69 6e 65 20 54 4b 5f 4f 46 46 53 45  #define TK_OFFSE
42a80 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
42a90 20 20 20 20 20 20 20 20 20 20 34 36 0a 23 64 65            46.#de
42aa0 66 69 6e 65 20 54 4b 5f 50 52 41 47 4d 41 20 20  fine TK_PRAGMA  
42ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42ac0 20 20 20 20 20 20 20 34 37 0a 23 64 65 66 69 6e         47.#defin
42ad0 65 20 54 4b 5f 52 41 49 53 45 20 20 20 20 20 20  e TK_RAISE      
42ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42af0 20 20 20 20 34 38 0a 23 64 65 66 69 6e 65 20 54      48.#define T
42b00 4b 5f 52 45 50 4c 41 43 45 20 20 20 20 20 20 20  K_REPLACE       
42b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42b20 20 34 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52   49.#define TK_R
42b30 45 53 54 52 49 43 54 20 20 20 20 20 20 20 20 20  ESTRICT         
42b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 30                50
42b50 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 4f 57 20  .#define TK_ROW 
42b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42b70 20 20 20 20 20 20 20 20 20 20 20 35 31 0a 23 64             51.#d
42b80 65 66 69 6e 65 20 54 4b 5f 54 52 49 47 47 45 52  efine TK_TRIGGER
42b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42ba0 20 20 20 20 20 20 20 20 35 32 0a 23 64 65 66 69          52.#defi
42bb0 6e 65 20 54 4b 5f 56 41 43 55 55 4d 20 20 20 20  ne TK_VACUUM    
42bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42bd0 20 20 20 20 20 35 33 0a 23 64 65 66 69 6e 65 20       53.#define 
42be0 54 4b 5f 56 49 45 57 20 20 20 20 20 20 20 20 20  TK_VIEW         
42bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42c00 20 20 35 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f    54.#define TK_
42c10 56 49 52 54 55 41 4c 20 20 20 20 20 20 20 20 20  VIRTUAL         
42c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
42c30 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 49  5.#define TK_REI
42c40 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 20 20  NDEX            
42c50 20 20 20 20 20 20 20 20 20 20 20 20 35 36 0a 23              56.#
42c60 64 65 66 69 6e 65 20 54 4b 5f 52 45 4e 41 4d 45  define TK_RENAME
42c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42c80 20 20 20 20 20 20 20 20 20 35 37 0a 23 64 65 66           57.#def
42c90 69 6e 65 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 20  ine TK_CTIME_KW 
42ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42cb0 20 20 20 20 20 20 35 38 0a 23 64 65 66 69 6e 65        58.#define
42cc0 20 54 4b 5f 41 4e 59 20 20 20 20 20 20 20 20 20   TK_ANY         
42cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42ce0 20 20 20 35 39 0a 23 64 65 66 69 6e 65 20 54 4b     59.#define TK
42cf0 5f 4f 52 20 20 20 20 20 20 20 20 20 20 20 20 20  _OR             
42d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42d10 36 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 4e  60.#define TK_AN
42d20 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  D               
42d30 20 20 20 20 20 20 20 20 20 20 20 20 20 36 31 0a               61.
42d40 23 64 65 66 69 6e 65 20 54 4b 5f 49 53 20 20 20  #define TK_IS   
42d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42d60 20 20 20 20 20 20 20 20 20 20 36 32 0a 23 64 65            62.#de
42d70 66 69 6e 65 20 54 4b 5f 42 45 54 57 45 45 4e 20  fine TK_BETWEEN 
42d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42d90 20 20 20 20 20 20 20 36 33 0a 23 64 65 66 69 6e         63.#defin
42da0 65 20 54 4b 5f 49 4e 20 20 20 20 20 20 20 20 20  e TK_IN         
42db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42dc0 20 20 20 20 36 34 0a 23 64 65 66 69 6e 65 20 54      64.#define T
42dd0 4b 5f 49 53 4e 55 4c 4c 20 20 20 20 20 20 20 20  K_ISNULL        
42de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42df0 20 36 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e   65.#define TK_N
42e00 4f 54 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20  OTNULL          
42e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 36                66
42e20 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 45 20 20  .#define TK_NE  
42e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42e40 20 20 20 20 20 20 20 20 20 20 20 36 37 0a 23 64             67.#d
42e50 65 66 69 6e 65 20 54 4b 5f 45 51 20 20 20 20 20  efine TK_EQ     
42e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42e70 20 20 20 20 20 20 20 20 36 38 0a 23 64 65 66 69          68.#defi
42e80 6e 65 20 54 4b 5f 47 54 20 20 20 20 20 20 20 20  ne TK_GT        
42e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42ea0 20 20 20 20 20 36 39 0a 23 64 65 66 69 6e 65 20       69.#define 
42eb0 54 4b 5f 4c 45 20 20 20 20 20 20 20 20 20 20 20  TK_LE           
42ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42ed0 20 20 37 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f    70.#define TK_
42ee0 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LT              
42ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
42f00 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 45 20  1.#define TK_GE 
42f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42f20 20 20 20 20 20 20 20 20 20 20 20 20 37 32 0a 23              72.#
42f30 64 65 66 69 6e 65 20 54 4b 5f 45 53 43 41 50 45  define TK_ESCAPE
42f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42f50 20 20 20 20 20 20 20 20 20 37 33 0a 23 64 65 66           73.#def
42f60 69 6e 65 20 54 4b 5f 42 49 54 41 4e 44 20 20 20  ine TK_BITAND   
42f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42f80 20 20 20 20 20 20 37 34 0a 23 64 65 66 69 6e 65        74.#define
42f90 20 54 4b 5f 42 49 54 4f 52 20 20 20 20 20 20 20   TK_BITOR       
42fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42fb0 20 20 20 37 35 0a 23 64 65 66 69 6e 65 20 54 4b     75.#define TK
42fc0 5f 4c 53 48 49 46 54 20 20 20 20 20 20 20 20 20  _LSHIFT         
42fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42fe0 37 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 53  76.#define TK_RS
42ff0 48 49 46 54 20 20 20 20 20 20 20 20 20 20 20 20  HIFT            
43000 20 20 20 20 20 20 20 20 20 20 20 20 20 37 37 0a               77.
43010 23 64 65 66 69 6e 65 20 54 4b 5f 50 4c 55 53 20  #define TK_PLUS 
43020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43030 20 20 20 20 20 20 20 20 20 20 37 38 0a 23 64 65            78.#de
43040 66 69 6e 65 20 54 4b 5f 4d 49 4e 55 53 20 20 20  fine TK_MINUS   
43050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43060 20 20 20 20 20 20 20 37 39 0a 23 64 65 66 69 6e         79.#defin
43070 65 20 54 4b 5f 53 54 41 52 20 20 20 20 20 20 20  e TK_STAR       
43080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43090 20 20 20 20 38 30 0a 23 64 65 66 69 6e 65 20 54      80.#define T
430a0 4b 5f 53 4c 41 53 48 20 20 20 20 20 20 20 20 20  K_SLASH         
430b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
430c0 20 38 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52   81.#define TK_R
430d0 45 4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20  EM              
430e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 32                82
430f0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 43  .#define TK_CONC
43100 41 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  AT              
43110 20 20 20 20 20 20 20 20 20 20 20 38 33 0a 23 64             83.#d
43120 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 4c 41 54 45  efine TK_COLLATE
43130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43140 20 20 20 20 20 20 20 20 38 34 0a 23 64 65 66 69          84.#defi
43150 6e 65 20 54 4b 5f 55 4d 49 4e 55 53 20 20 20 20  ne TK_UMINUS    
43160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43170 20 20 20 20 20 38 35 0a 23 64 65 66 69 6e 65 20       85.#define 
43180 54 4b 5f 55 50 4c 55 53 20 20 20 20 20 20 20 20  TK_UPLUS        
43190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
431a0 20 20 38 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f    86.#define TK_
431b0 42 49 54 4e 4f 54 20 20 20 20 20 20 20 20 20 20  BITNOT          
431c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38                 8
431d0 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 54 52  7.#define TK_STR
431e0 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 20  ING             
431f0 20 20 20 20 20 20 20 20 20 20 20 20 38 38 0a 23              88.#
43200 64 65 66 69 6e 65 20 54 4b 5f 4a 4f 49 4e 5f 4b  define TK_JOIN_K
43210 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  W               
43220 20 20 20 20 20 20 20 20 20 38 39 0a 23 64 65 66           89.#def
43230 69 6e 65 20 54 4b 5f 43 4f 4e 53 54 52 41 49 4e  ine TK_CONSTRAIN
43240 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
43250 20 20 20 20 20 20 39 30 0a 23 64 65 66 69 6e 65        90.#define
43260 20 54 4b 5f 44 45 46 41 55 4c 54 20 20 20 20 20   TK_DEFAULT     
43270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43280 20 20 20 39 31 0a 23 64 65 66 69 6e 65 20 54 4b     91.#define TK
43290 5f 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20  _NULL           
432a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
432b0 39 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 52  92.#define TK_PR
432c0 49 4d 41 52 59 20 20 20 20 20 20 20 20 20 20 20  IMARY           
432d0 20 20 20 20 20 20 20 20 20 20 20 20 20 39 33 0a               93.
432e0 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e 49 51 55  #define TK_UNIQU
432f0 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
43300 20 20 20 20 20 20 20 20 20 20 39 34 0a 23 64 65            94.#de
43310 66 69 6e 65 20 54 4b 5f 43 48 45 43 4b 20 20 20  fine TK_CHECK   
43320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43330 20 20 20 20 20 20 20 39 35 0a 23 64 65 66 69 6e         95.#defin
43340 65 20 54 4b 5f 52 45 46 45 52 45 4e 43 45 53 20  e TK_REFERENCES 
43350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43360 20 20 20 20 39 36 0a 23 64 65 66 69 6e 65 20 54      96.#define T
43370 4b 5f 41 55 54 4f 49 4e 43 52 20 20 20 20 20 20  K_AUTOINCR      
43380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43390 20 39 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f   97.#define TK_O
433a0 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  N               
433b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 38                98
433c0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 4c 45  .#define TK_DELE
433d0 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TE              
433e0 20 20 20 20 20 20 20 20 20 20 20 39 39 0a 23 64             99.#d
433f0 65 66 69 6e 65 20 54 4b 5f 55 50 44 41 54 45 20  efine TK_UPDATE 
43400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43410 20 20 20 20 20 20 20 20 31 30 30 0a 23 64 65 66          100.#def
43420 69 6e 65 20 54 4b 5f 49 4e 53 45 52 54 20 20 20  ine TK_INSERT   
43430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43440 20 20 20 20 20 20 31 30 31 0a 23 64 65 66 69 6e        101.#defin
43450 65 20 54 4b 5f 53 45 54 20 20 20 20 20 20 20 20  e TK_SET        
43460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43470 20 20 20 20 31 30 32 0a 23 64 65 66 69 6e 65 20      102.#define 
43480 54 4b 5f 44 45 46 45 52 52 41 42 4c 45 20 20 20  TK_DEFERRABLE   
43490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
434a0 20 20 31 30 33 0a 23 64 65 66 69 6e 65 20 54 4b    103.#define TK
434b0 5f 46 4f 52 45 49 47 4e 20 20 20 20 20 20 20 20  _FOREIGN        
434c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
434d0 31 30 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44  104.#define TK_D
434e0 52 4f 50 20 20 20 20 20 20 20 20 20 20 20 20 20  ROP             
434f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30                10
43500 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e 49  5.#define TK_UNI
43510 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ON              
43520 20 20 20 20 20 20 20 20 20 20 20 20 31 30 36 0a              106.
43530 23 64 65 66 69 6e 65 20 54 4b 5f 41 4c 4c 20 20  #define TK_ALL  
43540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43550 20 20 20 20 20 20 20 20 20 20 31 30 37 0a 23 64            107.#d
43560 65 66 69 6e 65 20 54 4b 5f 45 58 43 45 50 54 20  efine TK_EXCEPT 
43570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43580 20 20 20 20 20 20 20 20 31 30 38 0a 23 64 65 66          108.#def
43590 69 6e 65 20 54 4b 5f 49 4e 54 45 52 53 45 43 54  ine TK_INTERSECT
435a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
435b0 20 20 20 20 20 20 31 30 39 0a 23 64 65 66 69 6e        109.#defin
435c0 65 20 54 4b 5f 53 45 4c 45 43 54 20 20 20 20 20  e TK_SELECT     
435d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
435e0 20 20 20 20 31 31 30 0a 23 64 65 66 69 6e 65 20      110.#define 
435f0 54 4b 5f 44 49 53 54 49 4e 43 54 20 20 20 20 20  TK_DISTINCT     
43600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43610 20 20 31 31 31 0a 23 64 65 66 69 6e 65 20 54 4b    111.#define TK
43620 5f 44 4f 54 20 20 20 20 20 20 20 20 20 20 20 20  _DOT            
43630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43640 31 31 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46  112.#define TK_F
43650 52 4f 4d 20 20 20 20 20 20 20 20 20 20 20 20 20  ROM             
43660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31                11
43670 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4a 4f 49  3.#define TK_JOI
43680 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  N               
43690 20 20 20 20 20 20 20 20 20 20 20 20 31 31 34 0a              114.
436a0 23 64 65 66 69 6e 65 20 54 4b 5f 55 53 49 4e 47  #define TK_USING
436b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
436c0 20 20 20 20 20 20 20 20 20 20 31 31 35 0a 23 64            115.#d
436d0 65 66 69 6e 65 20 54 4b 5f 4f 52 44 45 52 20 20  efine TK_ORDER  
436e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
436f0 20 20 20 20 20 20 20 20 31 31 36 0a 23 64 65 66          116.#def
43700 69 6e 65 20 54 4b 5f 42 59 20 20 20 20 20 20 20  ine TK_BY       
43710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43720 20 20 20 20 20 20 31 31 37 0a 23 64 65 66 69 6e        117.#defin
43730 65 20 54 4b 5f 47 52 4f 55 50 20 20 20 20 20 20  e TK_GROUP      
43740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43750 20 20 20 20 31 31 38 0a 23 64 65 66 69 6e 65 20      118.#define 
43760 54 4b 5f 48 41 56 49 4e 47 20 20 20 20 20 20 20  TK_HAVING       
43770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43780 20 20 31 31 39 0a 23 64 65 66 69 6e 65 20 54 4b    119.#define TK
43790 5f 4c 49 4d 49 54 20 20 20 20 20 20 20 20 20 20  _LIMIT          
437a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
437b0 31 32 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 57  120.#define TK_W
437c0 48 45 52 45 20 20 20 20 20 20 20 20 20 20 20 20  HERE            
437d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
437e0 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 54  1.#define TK_INT
437f0 4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  O               
43800 20 20 20 20 20 20 20 20 20 20 20 20 31 32 32 0a              122.
43810 23 64 65 66 69 6e 65 20 54 4b 5f 56 41 4c 55 45  #define TK_VALUE
43820 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  S               
43830 20 20 20 20 20 20 20 20 20 20 31 32 33 0a 23 64            123.#d
43840 65 66 69 6e 65 20 54 4b 5f 49 4e 54 45 47 45 52  efine TK_INTEGER
43850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43860 20 20 20 20 20 20 20 20 31 32 34 0a 23 64 65 66          124.#def
43870 69 6e 65 20 54 4b 5f 46 4c 4f 41 54 20 20 20 20  ine TK_FLOAT    
43880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43890 20 20 20 20 20 20 31 32 35 0a 23 64 65 66 69 6e        125.#defin
438a0 65 20 54 4b 5f 42 4c 4f 42 20 20 20 20 20 20 20  e TK_BLOB       
438b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
438c0 20 20 20 20 31 32 36 0a 23 64 65 66 69 6e 65 20      126.#define 
438d0 54 4b 5f 52 45 47 49 53 54 45 52 20 20 20 20 20  TK_REGISTER     
438e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
438f0 20 20 31 32 37 0a 23 64 65 66 69 6e 65 20 54 4b    127.#define TK
43900 5f 56 41 52 49 41 42 4c 45 20 20 20 20 20 20 20  _VARIABLE       
43910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43920 31 32 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43  128.#define TK_C
43930 41 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ASE             
43940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
43950 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 57 48 45  9.#define TK_WHE
43960 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  N               
43970 20 20 20 20 20 20 20 20 20 20 20 20 31 33 30 0a              130.
43980 23 64 65 66 69 6e 65 20 54 4b 5f 54 48 45 4e 20  #define TK_THEN 
43990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
439a0 20 20 20 20 20 20 20 20 20 20 31 33 31 0a 23 64            131.#d
439b0 65 66 69 6e 65 20 54 4b 5f 45 4c 53 45 20 20 20  efine TK_ELSE   
439c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
439d0 20 20 20 20 20 20 20 20 31 33 32 0a 23 64 65 66          132.#def
439e0 69 6e 65 20 54 4b 5f 49 4e 44 45 58 20 20 20 20  ine TK_INDEX    
439f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43a00 20 20 20 20 20 20 31 33 33 0a 23 64 65 66 69 6e        133.#defin
43a10 65 20 54 4b 5f 41 4c 54 45 52 20 20 20 20 20 20  e TK_ALTER      
43a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43a30 20 20 20 20 31 33 34 0a 23 64 65 66 69 6e 65 20      134.#define 
43a40 54 4b 5f 54 4f 20 20 20 20 20 20 20 20 20 20 20  TK_TO           
43a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43a60 20 20 31 33 35 0a 23 64 65 66 69 6e 65 20 54 4b    135.#define TK
43a70 5f 41 44 44 20 20 20 20 20 20 20 20 20 20 20 20  _ADD            
43a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43a90 31 33 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43  136.#define TK_C
43aa0 4f 4c 55 4d 4e 4b 57 20 20 20 20 20 20 20 20 20  OLUMNKW         
43ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33                13
43ac0 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f  7.#define TK_TO_
43ad0 54 45 58 54 20 20 20 20 20 20 20 20 20 20 20 20  TEXT            
43ae0 20 20 20 20 20 20 20 20 20 20 20 20 31 33 38 0a              138.
43af0 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 42 4c  #define TK_TO_BL
43b00 4f 42 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OB              
43b10 20 20 20 20 20 20 20 20 20 20 31 33 39 0a 23 64            139.#d
43b20 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 4e 55 4d 45  efine TK_TO_NUME
43b30 52 49 43 20 20 20 20 20 20 20 20 20 20 20 20 20  RIC             
43b40 20 20 20 20 20 20 20 20 31 34 30 0a 23 64 65 66          140.#def
43b50 69 6e 65 20 54 4b 5f 54 4f 5f 49 4e 54 20 20 20  ine TK_TO_INT   
43b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43b70 20 20 20 20 20 20 31 34 31 0a 23 64 65 66 69 6e        141.#defin
43b80 65 20 54 4b 5f 54 4f 5f 52 45 41 4c 20 20 20 20  e TK_TO_REAL    
43b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43ba0 20 20 20 20 31 34 32 0a 23 64 65 66 69 6e 65 20      142.#define 
43bb0 54 4b 5f 45 4e 44 5f 4f 46 5f 46 49 4c 45 20 20  TK_END_OF_FILE  
43bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43bd0 20 20 31 34 33 0a 23 64 65 66 69 6e 65 20 54 4b    143.#define TK
43be0 5f 49 4c 4c 45 47 41 4c 20 20 20 20 20 20 20 20  _ILLEGAL        
43bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43c00 31 34 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53  144.#define TK_S
43c10 50 41 43 45 20 20 20 20 20 20 20 20 20 20 20 20  PACE            
43c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34                14
43c30 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e 43  5.#define TK_UNC
43c40 4c 4f 53 45 44 5f 53 54 52 49 4e 47 20 20 20 20  LOSED_STRING    
43c50 20 20 20 20 20 20 20 20 20 20 20 20 31 34 36 0a              146.
43c60 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d 45  #define TK_COMME
43c70 4e 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  NT              
43c80 20 20 20 20 20 20 20 20 20 20 31 34 37 0a 23 64            147.#d
43c90 65 66 69 6e 65 20 54 4b 5f 46 55 4e 43 54 49 4f  efine TK_FUNCTIO
43ca0 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  N               
43cb0 20 20 20 20 20 20 20 20 31 34 38 0a 23 64 65 66          148.#def
43cc0 69 6e 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20 20 20  ine TK_COLUMN   
43cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43ce0 20 20 20 20 20 20 31 34 39 0a 23 64 65 66 69 6e        149.#defin
43cf0 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f  e TK_AGG_FUNCTIO
43d00 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  N               
43d10 20 20 20 20 31 35 30 0a 23 64 65 66 69 6e 65 20      150.#define 
43d20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 20 20  TK_AGG_COLUMN   
43d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43d40 20 20 31 35 31 0a 23 64 65 66 69 6e 65 20 54 4b    151.#define TK
43d50 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 20 20 20 20  _CONST_FUNC     
43d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43d70 31 35 32 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  152../**********
43d80 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 72 73  **** End of pars
43d90 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
43da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43dc0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
43dd0 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
43de0 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
43df0 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68  f in sqliteInt.h
43e00 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
43e10 2a 2a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 73  ***/.#include <s
43e20 74 64 69 6f 2e 68 3e 0a 23 69 6e 63 6c 75 64 65  tdio.h>.#include
43e30 20 3c 73 74 64 6c 69 62 2e 68 3e 0a 23 69 6e 63   <stdlib.h>.#inc
43e40 6c 75 64 65 20 3c 73 74 72 69 6e 67 2e 68 3e 0a  lude <string.h>.
43e50 23 69 6e 63 6c 75 64 65 20 3c 61 73 73 65 72 74  #include <assert
43e60 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74  .h>.#include <st
43e70 64 64 65 66 2e 68 3e 0a 0a 2f 2a 0a 2a 2a 20 49  ddef.h>../*.** I
43e80 66 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20  f compiling for 
43e90 61 20 70 72 6f 63 65 73 73 6f 72 20 74 68 61 74  a processor that
43ea0 20 6c 61 63 6b 73 20 66 6c 6f 61 74 69 6e 67 20   lacks floating 
43eb0 70 6f 69 6e 74 20 73 75 70 70 6f 72 74 2c 0a 2a  point support,.*
43ec0 2a 20 73 75 62 73 74 69 74 75 74 65 20 69 6e 74  * substitute int
43ed0 65 67 65 72 20 66 6f 72 20 66 6c 6f 61 74 69 6e  eger for floatin
43ee0 67 2d 70 6f 69 6e 74 0a 2a 2f 0a 23 69 66 64 65  g-point.*/.#ifde
43ef0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
43f00 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64  OATING_POINT.# d
43f10 65 66 69 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c  efine double sql
43f20 69 74 65 5f 69 6e 74 36 34 0a 23 20 64 65 66 69  ite_int64.# defi
43f30 6e 65 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59  ne LONGDOUBLE_TY
43f40 50 45 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 0a  PE sqlite_int64.
43f50 23 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  # ifndef SQLITE_
43f60 42 49 47 5f 44 42 4c 0a 23 20 20 20 64 65 66 69  BIG_DBL.#   defi
43f70 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42  ne SQLITE_BIG_DB
43f80 4c 20 28 30 78 37 66 66 66 66 66 66 66 66 66 66  L (0x7ffffffffff
43f90 66 66 66 66 66 29 0a 23 20 65 6e 64 69 66 0a 23  fffff).# endif.#
43fa0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f   define SQLITE_O
43fb0 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e  MIT_DATETIME_FUN
43fc0 43 53 20 31 0a 23 20 64 65 66 69 6e 65 20 53 51  CS 1.# define SQ
43fd0 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20  LITE_OMIT_TRACE 
43fe0 31 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45  1.# undef SQLITE
43ff0 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f 36 34  _MIXED_ENDIAN_64
44000 42 49 54 5f 46 4c 4f 41 54 0a 23 65 6e 64 69 66  BIT_FLOAT.#endif
44010 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
44020 42 49 47 5f 44 42 4c 0a 23 20 64 65 66 69 6e 65  BIG_DBL.# define
44030 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 20   SQLITE_BIG_DBL 
44040 28 31 65 39 39 29 0a 23 65 6e 64 69 66 0a 0a 2f  (1e99).#endif../
44050 2a 0a 2a 2a 20 4f 4d 49 54 5f 54 45 4d 50 44 42  *.** OMIT_TEMPDB
44060 20 69 73 20 73 65 74 20 74 6f 20 31 20 69 66 20   is set to 1 if 
44070 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50  SQLITE_OMIT_TEMP
44080 44 42 20 69 73 20 64 65 66 69 6e 65 64 2c 20 6f  DB is defined, o
44090 72 20 30 0a 2a 2a 20 61 66 74 65 72 77 61 72 64  r 0.** afterward
440a0 2e 20 48 61 76 69 6e 67 20 74 68 69 73 20 6d 61  . Having this ma
440b0 63 72 6f 20 61 6c 6c 6f 77 73 20 75 73 20 74 6f  cro allows us to
440c0 20 63 61 75 73 65 20 74 68 65 20 43 20 63 6f 6d   cause the C com
440d0 70 69 6c 65 72 20 0a 2a 2a 20 74 6f 20 6f 6d 69  piler .** to omi
440e0 74 20 63 6f 64 65 20 75 73 65 64 20 62 79 20 54  t code used by T
440f0 45 4d 50 20 74 61 62 6c 65 73 20 77 69 74 68 6f  EMP tables witho
44100 75 74 20 6d 65 73 73 79 20 23 69 66 6e 64 65 66  ut messy #ifndef
44110 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a   statements..*/.
44120 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
44130 49 54 5f 54 45 4d 50 44 42 0a 23 64 65 66 69 6e  IT_TEMPDB.#defin
44140 65 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 31 0a  e OMIT_TEMPDB 1.
44150 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 4d  #else.#define OM
44160 49 54 5f 54 45 4d 50 44 42 20 30 0a 23 65 6e 64  IT_TEMPDB 0.#end
44170 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  if../*.** If the
44180 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f   following macro
44190 20 69 73 20 73 65 74 20 74 6f 20 31 2c 20 74 68   is set to 1, th
441a0 65 6e 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 61  en NULL values a
441b0 72 65 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a 2a  re considered.**
441c0 20 64 69 73 74 69 6e 63 74 20 77 68 65 6e 20 64   distinct when d
441d0 65 74 65 72 6d 69 6e 69 6e 67 20 77 68 65 74 68  etermining wheth
441e0 65 72 20 6f 72 20 6e 6f 74 20 74 77 6f 20 65 6e  er or not two en
441f0 74 72 69 65 73 20 61 72 65 20 74 68 65 20 73 61  tries are the sa
44200 6d 65 0a 2a 2a 20 69 6e 20 61 20 55 4e 49 51 55  me.** in a UNIQU
44210 45 20 69 6e 64 65 78 2e 20 20 54 68 69 73 20 69  E index.  This i
44220 73 20 74 68 65 20 77 61 79 20 50 6f 73 74 67 72  s the way Postgr
44230 65 53 51 4c 2c 20 4f 72 61 63 6c 65 2c 20 44 42  eSQL, Oracle, DB
44240 32 2c 20 4d 79 53 51 4c 2c 0a 2a 2a 20 4f 43 45  2, MySQL,.** OCE
44250 4c 4f 54 2c 20 61 6e 64 20 46 69 72 65 62 69 72  LOT, and Firebir
44260 64 20 61 6c 6c 20 77 6f 72 6b 2e 20 20 54 68 65  d all work.  The
44270 20 53 51 4c 39 32 20 73 70 65 63 20 65 78 70 6c   SQL92 spec expl
44280 69 63 69 74 6c 79 20 73 61 79 73 20 74 68 69 73  icitly says this
44290 0a 2a 2a 20 69 73 20 74 68 65 20 77 61 79 20 74  .** is the way t
442a0 68 69 6e 67 73 20 61 72 65 20 73 75 70 70 6f 73  hings are suppos
442b0 65 20 74 6f 20 77 6f 72 6b 2e 0a 2a 2a 0a 2a 2a  e to work..**.**
442c0 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   If the followin
442d0 67 20 6d 61 63 72 6f 20 69 73 20 73 65 74 20 74  g macro is set t
442e0 6f 20 30 2c 20 74 68 65 20 4e 55 4c 4c 73 20 61  o 0, the NULLs a
442f0 72 65 20 69 6e 64 69 73 74 69 6e 63 74 20 66 6f  re indistinct fo
44300 72 0a 2a 2a 20 61 20 55 4e 49 51 55 45 20 69 6e  r.** a UNIQUE in
44310 64 65 78 2e 20 20 49 6e 20 74 68 69 73 20 6d 6f  dex.  In this mo
44320 64 65 2c 20 79 6f 75 20 63 61 6e 20 6f 6e 6c 79  de, you can only
44330 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 20 4e   have a single N
44340 55 4c 4c 20 65 6e 74 72 79 0a 2a 2a 20 66 6f 72  ULL entry.** for
44350 20 61 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72   a column declar
44360 65 64 20 55 4e 49 51 55 45 2e 20 20 54 68 69 73  ed UNIQUE.  This
44370 20 69 73 20 74 68 65 20 77 61 79 20 49 6e 66 6f   is the way Info
44380 72 6d 69 78 20 61 6e 64 20 53 51 4c 20 53 65 72  rmix and SQL Ser
44390 76 65 72 0a 2a 2a 20 77 6f 72 6b 2e 0a 2a 2f 0a  ver.** work..*/.
443a0 23 64 65 66 69 6e 65 20 4e 55 4c 4c 5f 44 49 53  #define NULL_DIS
443b0 54 49 4e 43 54 5f 46 4f 52 5f 55 4e 49 51 55 45  TINCT_FOR_UNIQUE
443c0 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 66   1../*.** The "f
443d0 69 6c 65 20 66 6f 72 6d 61 74 22 20 6e 75 6d 62  ile format" numb
443e0 65 72 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72  er is an integer
443f0 20 74 68 61 74 20 69 73 20 69 6e 63 72 65 6d 65   that is increme
44400 6e 74 65 64 20 77 68 65 6e 65 76 65 72 0a 2a 2a  nted whenever.**
44410 20 74 68 65 20 56 44 42 45 2d 6c 65 76 65 6c 20   the VDBE-level 
44420 66 69 6c 65 20 66 6f 72 6d 61 74 20 63 68 61 6e  file format chan
44430 67 65 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  ges.  The follow
44440 69 6e 67 20 6d 61 63 72 6f 73 20 64 65 66 69 6e  ing macros defin
44450 65 20 74 68 65 0a 2a 2a 20 74 68 65 20 64 65 66  e the.** the def
44460 61 75 6c 74 20 66 69 6c 65 20 66 6f 72 6d 61 74  ault file format
44470 20 66 6f 72 20 6e 65 77 20 64 61 74 61 62 61 73   for new databas
44480 65 73 20 61 6e 64 20 74 68 65 20 6d 61 78 69 6d  es and the maxim
44490 75 6d 20 66 69 6c 65 20 66 6f 72 6d 61 74 0a 2a  um file format.*
444a0 2a 20 74 68 61 74 20 74 68 65 20 6c 69 62 72 61  * that the libra
444b0 72 79 20 63 61 6e 20 72 65 61 64 2e 0a 2a 2f 0a  ry can read..*/.
444c0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d  #define SQLITE_M
444d0 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20 34  AX_FILE_FORMAT 4
444e0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
444f0 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46 4f 52  DEFAULT_FILE_FOR
44500 4d 41 54 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  MAT.# define SQL
44510 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45  ITE_DEFAULT_FILE
44520 5f 46 4f 52 4d 41 54 20 31 0a 23 65 6e 64 69 66  _FORMAT 1.#endif
44530 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 76 69 64 65 20  ../*.** Provide 
44540 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20  a default value 
44550 66 6f 72 20 54 45 4d 50 5f 53 54 4f 52 45 20 69  for TEMP_STORE i
44560 6e 20 63 61 73 65 20 69 74 20 69 73 20 6e 6f 74  n case it is not
44570 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 6f 6e   specified.** on
44580 20 74 68 65 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e   the command-lin
44590 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 54 45 4d  e.*/.#ifndef TEM
445a0 50 5f 53 54 4f 52 45 0a 23 20 64 65 66 69 6e 65  P_STORE.# define
445b0 20 54 45 4d 50 5f 53 54 4f 52 45 20 31 0a 23 65   TEMP_STORE 1.#e
445c0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 43 43 20  ndif../*.** GCC 
445d0 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e 65 20  does not define 
445e0 74 68 65 20 6f 66 66 73 65 74 6f 66 28 29 20 6d  the offsetof() m
445f0 61 63 72 6f 20 73 6f 20 77 65 27 6c 6c 20 68 61  acro so we'll ha
44600 76 65 20 74 6f 20 64 6f 20 69 74 0a 2a 2a 20 6f  ve to do it.** o
44610 75 72 73 65 6c 76 65 73 2e 0a 2a 2f 0a 23 69 66  urselves..*/.#if
44620 6e 64 65 66 20 6f 66 66 73 65 74 6f 66 0a 23 64  ndef offsetof.#d
44630 65 66 69 6e 65 20 6f 66 66 73 65 74 6f 66 28 53  efine offsetof(S
44640 54 52 55 43 54 55 52 45 2c 46 49 45 4c 44 29 20  TRUCTURE,FIELD) 
44650 28 28 69 6e 74 29 28 28 63 68 61 72 2a 29 26 28  ((int)((char*)&(
44660 28 53 54 52 55 43 54 55 52 45 2a 29 30 29 2d 3e  (STRUCTURE*)0)->
44670 46 49 45 4c 44 29 29 0a 23 65 6e 64 69 66 0a 0a  FIELD)).#endif..
44680 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73  /*.** Check to s
44690 65 65 20 69 66 20 74 68 69 73 20 6d 61 63 68 69  ee if this machi
446a0 6e 65 20 75 73 65 73 20 45 42 43 44 49 43 2e 20  ne uses EBCDIC. 
446b0 20 28 59 65 73 2c 20 62 65 6c 69 65 76 65 20 69   (Yes, believe i
446c0 74 20 6f 72 0a 2a 2a 20 6e 6f 74 2c 20 74 68 65  t or.** not, the
446d0 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6d 61 63  re are still mac
446e0 68 69 6e 65 73 20 6f 75 74 20 74 68 65 72 65 20  hines out there 
446f0 74 68 61 74 20 75 73 65 20 45 42 43 44 49 43 2e  that use EBCDIC.
44700 29 0a 2a 2f 0a 23 69 66 20 27 41 27 20 3d 3d 20  ).*/.#if 'A' == 
44710 27 5c 33 30 31 27 0a 23 20 64 65 66 69 6e 65 20  '\301'.# define 
44720 53 51 4c 49 54 45 5f 45 42 43 44 49 43 20 31 0a  SQLITE_EBCDIC 1.
44730 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53  #else.# define S
44740 51 4c 49 54 45 5f 41 53 43 49 49 20 31 0a 23 65  QLITE_ASCII 1.#e
44750 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  ndif../*.** Inte
44760 67 65 72 73 20 6f 66 20 6b 6e 6f 77 6e 20 73 69  gers of known si
44770 7a 65 73 2e 20 20 54 68 65 73 65 20 74 79 70 65  zes.  These type
44780 64 65 66 73 20 6d 69 67 68 74 20 63 68 61 6e 67  defs might chang
44790 65 20 66 6f 72 20 61 72 63 68 69 74 65 63 74 75  e for architectu
447a0 72 65 73 0a 2a 2a 20 77 68 65 72 65 20 74 68 65  res.** where the
447b0 20 73 69 7a 65 73 20 76 65 72 79 2e 20 20 50 72   sizes very.  Pr
447c0 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f  eprocessor macro
447d0 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20  s are available 
447e0 73 6f 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 74  so that the.** t
447f0 79 70 65 73 20 63 61 6e 20 62 65 20 63 6f 6e 76  ypes can be conv
44800 65 6e 69 65 6e 74 6c 79 20 72 65 64 65 66 69 6e  eniently redefin
44810 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 79  ed at compile-ty
44820 70 65 2e 20 20 4c 69 6b 65 20 74 68 69 73 3a 0a  pe.  Like this:.
44830 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 63  **.**         cc
44840 20 27 2d 44 55 49 4e 54 50 54 52 5f 54 59 50 45   '-DUINTPTR_TYPE
44850 3d 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 27 20  =long long int' 
44860 2e 2e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 55  ....*/.#ifndef U
44870 49 4e 54 33 32 5f 54 59 50 45 0a 23 20 69 66 64  INT32_TYPE.# ifd
44880 65 66 20 48 41 56 45 5f 55 49 4e 54 33 32 5f 54  ef HAVE_UINT32_T
44890 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 33  .#  define UINT3
448a0 32 5f 54 59 50 45 20 75 69 6e 74 33 32 5f 74 0a  2_TYPE uint32_t.
448b0 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65  # else.#  define
448c0 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 6e 73   UINT32_TYPE uns
448d0 69 67 6e 65 64 20 69 6e 74 0a 23 20 65 6e 64 69  igned int.# endi
448e0 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  f.#endif.#ifndef
448f0 20 55 49 4e 54 31 36 5f 54 59 50 45 0a 23 20 69   UINT16_TYPE.# i
44900 66 64 65 66 20 48 41 56 45 5f 55 49 4e 54 31 36  fdef HAVE_UINT16
44910 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e  _T.#  define UIN
44920 54 31 36 5f 54 59 50 45 20 75 69 6e 74 31 36 5f  T16_TYPE uint16_
44930 74 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69  t.# else.#  defi
44940 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 45 20 75  ne UINT16_TYPE u
44950 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20 69 6e  nsigned short in
44960 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66  t.# endif.#endif
44970 0a 23 69 66 6e 64 65 66 20 49 4e 54 31 36 5f 54  .#ifndef INT16_T
44980 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45  YPE.# ifdef HAVE
44990 5f 49 4e 54 31 36 5f 54 0a 23 20 20 64 65 66 69  _INT16_T.#  defi
449a0 6e 65 20 49 4e 54 31 36 5f 54 59 50 45 20 69 6e  ne INT16_TYPE in
449b0 74 31 36 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20  t16_t.# else.#  
449c0 64 65 66 69 6e 65 20 49 4e 54 31 36 5f 54 59 50  define INT16_TYP
449d0 45 20 73 68 6f 72 74 20 69 6e 74 0a 23 20 65 6e  E short int.# en
449e0 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64  dif.#endif.#ifnd
449f0 65 66 20 55 49 4e 54 38 5f 54 59 50 45 0a 23 20  ef UINT8_TYPE.# 
44a00 69 66 64 65 66 20 48 41 56 45 5f 55 49 4e 54 38  ifdef HAVE_UINT8
44a10 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e  _T.#  define UIN
44a20 54 38 5f 54 59 50 45 20 75 69 6e 74 38 5f 74 0a  T8_TYPE uint8_t.
44a30 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65  # else.#  define
44a40 20 55 49 4e 54 38 5f 54 59 50 45 20 75 6e 73 69   UINT8_TYPE unsi
44a50 67 6e 65 64 20 63 68 61 72 0a 23 20 65 6e 64 69  gned char.# endi
44a60 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  f.#endif.#ifndef
44a70 20 49 4e 54 38 5f 54 59 50 45 0a 23 20 69 66 64   INT8_TYPE.# ifd
44a80 65 66 20 48 41 56 45 5f 49 4e 54 38 5f 54 0a 23  ef HAVE_INT8_T.#
44a90 20 20 64 65 66 69 6e 65 20 49 4e 54 38 5f 54 59    define INT8_TY
44aa0 50 45 20 69 6e 74 38 5f 74 0a 23 20 65 6c 73 65  PE int8_t.# else
44ab0 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54 38 5f  .#  define INT8_
44ac0 54 59 50 45 20 73 69 67 6e 65 64 20 63 68 61 72  TYPE signed char
44ad0 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  .# endif.#endif.
44ae0 23 69 66 6e 64 65 66 20 4c 4f 4e 47 44 4f 55 42  #ifndef LONGDOUB
44af0 4c 45 5f 54 59 50 45 0a 23 20 64 65 66 69 6e 65  LE_TYPE.# define
44b00 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45   LONGDOUBLE_TYPE
44b10 20 6c 6f 6e 67 20 64 6f 75 62 6c 65 0a 23 65 6e   long double.#en
44b20 64 69 66 0a 74 79 70 65 64 65 66 20 73 71 6c 69  dif.typedef sqli
44b30 74 65 5f 69 6e 74 36 34 20 69 36 34 3b 20 20 20  te_int64 i64;   
44b40 20 20 20 20 20 20 20 2f 2a 20 38 2d 62 79 74 65         /* 8-byte
44b50 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20   signed integer 
44b60 2a 2f 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74  */.typedef sqlit
44b70 65 5f 75 69 6e 74 36 34 20 75 36 34 3b 20 20 20  e_uint64 u64;   
44b80 20 20 20 20 20 20 2f 2a 20 38 2d 62 79 74 65 20        /* 8-byte 
44b90 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  unsigned integer
44ba0 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49 4e 54   */.typedef UINT
44bb0 33 32 5f 54 59 50 45 20 75 33 32 3b 20 20 20 20  32_TYPE u32;    
44bc0 20 20 20 20 20 20 20 2f 2a 20 34 2d 62 79 74 65         /* 4-byte
44bd0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65   unsigned intege
44be0 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49 4e  r */.typedef UIN
44bf0 54 31 36 5f 54 59 50 45 20 75 31 36 3b 20 20 20  T16_TYPE u16;   
44c00 20 20 20 20 20 20 20 20 2f 2a 20 32 2d 62 79 74          /* 2-byt
44c10 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67  e unsigned integ
44c20 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 49 4e  er */.typedef IN
44c30 54 31 36 5f 54 59 50 45 20 69 31 36 3b 20 20 20  T16_TYPE i16;   
44c40 20 20 20 20 20 20 20 20 20 2f 2a 20 32 2d 62 79           /* 2-by
44c50 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  te signed intege
44c60 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49 4e  r */.typedef UIN
44c70 54 38 5f 54 59 50 45 20 75 38 3b 20 20 20 20 20  T8_TYPE u8;     
44c80 20 20 20 20 20 20 20 20 2f 2a 20 31 2d 62 79 74          /* 1-byt
44c90 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67  e unsigned integ
44ca0 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49  er */.typedef UI
44cb0 4e 54 38 5f 54 59 50 45 20 69 38 3b 20 20 20 20  NT8_TYPE i8;    
44cc0 20 20 20 20 20 20 20 20 20 2f 2a 20 31 2d 62 79           /* 1-by
44cd0 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  te signed intege
44ce0 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72  r */../*.** Macr
44cf0 6f 73 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  os to determine 
44d00 77 68 65 74 68 65 72 20 74 68 65 20 6d 61 63 68  whether the mach
44d10 69 6e 65 20 69 73 20 62 69 67 20 6f 72 20 6c 69  ine is big or li
44d20 74 74 6c 65 20 65 6e 64 69 61 6e 2c 0a 2a 2a 20  ttle endian,.** 
44d30 65 76 61 6c 75 61 74 65 64 20 61 74 20 72 75 6e  evaluated at run
44d40 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  time..*/.#ifdef 
44d50 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54  SQLITE_AMALGAMAT
44d60 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ION.SQLITE_PRIVA
44d70 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73 71 6c  TE const int sql
44d80 69 74 65 33 6f 6e 65 3b 0a 23 65 6c 73 65 0a 53  ite3one;.#else.S
44d90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
44da0 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 65 33 6f  nst int sqlite3o
44db0 6e 65 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 64  ne;.#endif.#if d
44dc0 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20  efined(i386) || 
44dd0 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f  defined(__i386__
44de0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f  ) || defined(_M_
44df0 49 58 38 36 29 0a 23 20 64 65 66 69 6e 65 20 53  IX86).# define S
44e00 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 20  QLITE_BIGENDIAN 
44e10 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 53 51     0.# define SQ
44e20 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41  LITE_LITTLEENDIA
44e30 4e 20 31 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  N 1.# define SQL
44e40 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20  ITE_UTF16NATIVE 
44e50 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 0a   SQLITE_UTF16LE.
44e60 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53  #else.# define S
44e70 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 20  QLITE_BIGENDIAN 
44e80 20 20 20 28 2a 28 63 68 61 72 20 2a 29 28 26 73     (*(char *)(&s
44e90 71 6c 69 74 65 33 6f 6e 65 29 3d 3d 30 29 0a 23  qlite3one)==0).#
44ea0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c   define SQLITE_L
44eb0 49 54 54 4c 45 45 4e 44 49 41 4e 20 28 2a 28 63  ITTLEENDIAN (*(c
44ec0 68 61 72 20 2a 29 28 26 73 71 6c 69 74 65 33 6f  har *)(&sqlite3o
44ed0 6e 65 29 3d 3d 31 29 0a 23 20 64 65 66 69 6e 65  ne)==1).# define
44ee0 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
44ef0 49 56 45 20 28 53 51 4c 49 54 45 5f 42 49 47 45  IVE (SQLITE_BIGE
44f00 4e 44 49 41 4e 3f 53 51 4c 49 54 45 5f 55 54 46  NDIAN?SQLITE_UTF
44f10 31 36 42 45 3a 53 51 4c 49 54 45 5f 55 54 46 31  16BE:SQLITE_UTF1
44f20 36 4c 45 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  6LE).#endif../*.
44f30 2a 2a 20 43 6f 6e 73 74 61 6e 74 73 20 66 6f 72  ** Constants for
44f40 20 74 68 65 20 6c 61 72 67 65 73 74 20 61 6e 64   the largest and
44f50 20 73 6d 61 6c 6c 65 73 74 20 70 6f 73 73 69 62   smallest possib
44f60 6c 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64  le 64-bit signed
44f70 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 54 68   integers..** Th
44f80 65 73 65 20 6d 61 63 72 6f 73 20 61 72 65 20 64  ese macros are d
44f90 65 73 69 67 6e 65 64 20 74 6f 20 77 6f 72 6b 20  esigned to work 
44fa0 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 62 6f 74  correctly on bot
44fb0 68 20 33 32 2d 62 69 74 20 61 6e 64 20 36 34 2d  h 32-bit and 64-
44fc0 62 69 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 73  bit.** compilers
44fd0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4c 41 52  ..*/.#define LAR
44fe0 47 45 53 54 5f 49 4e 54 36 34 20 20 28 30 78 66  GEST_INT64  (0xf
44ff0 66 66 66 66 66 66 66 7c 28 28 28 69 36 34 29 30  fffffff|(((i64)0
45000 78 37 66 66 66 66 66 66 66 29 3c 3c 33 32 29 29  x7fffffff)<<32))
45010 0a 23 64 65 66 69 6e 65 20 53 4d 41 4c 4c 45 53  .#define SMALLES
45020 54 5f 49 4e 54 36 34 20 28 28 28 69 36 34 29 2d  T_INT64 (((i64)-
45030 31 29 20 2d 20 4c 41 52 47 45 53 54 5f 49 4e 54  1) - LARGEST_INT
45040 36 34 29 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  64)../*.** An in
45050 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
45060 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
45070 65 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f  e is used to sto
45080 72 65 20 74 68 65 20 62 75 73 79 2d 68 61 6e 64  re the busy-hand
45090 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20  ler.** callback 
450a0 66 6f 72 20 61 20 67 69 76 65 6e 20 73 71 6c 69  for a given sqli
450b0 74 65 20 68 61 6e 64 6c 65 2e 20 0a 2a 2a 0a 2a  te handle. .**.*
450c0 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 62 75 73  * The sqlite.bus
450d0 79 48 61 6e 64 6c 65 72 20 6d 65 6d 62 65 72 20  yHandler member 
450e0 6f 66 20 74 68 65 20 73 71 6c 69 74 65 20 73 74  of the sqlite st
450f0 72 75 63 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  ruct contains th
45100 65 20 62 75 73 79 0a 2a 2a 20 63 61 6c 6c 62 61  e busy.** callba
45110 63 6b 20 66 6f 72 20 74 68 65 20 64 61 74 61 62  ck for the datab
45120 61 73 65 20 68 61 6e 64 6c 65 2e 20 45 61 63 68  ase handle. Each
45130 20 70 61 67 65 72 20 6f 70 65 6e 65 64 20 76 69   pager opened vi
45140 61 20 74 68 65 20 73 71 6c 69 74 65 0a 2a 2a 20  a the sqlite.** 
45150 68 61 6e 64 6c 65 20 69 73 20 70 61 73 73 65 64  handle is passed
45160 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 73 71   a pointer to sq
45170 6c 69 74 65 2e 62 75 73 79 48 61 6e 64 6c 65 72  lite.busyHandler
45180 2e 20 54 68 65 20 62 75 73 79 2d 68 61 6e 64 6c  . The busy-handl
45190 65 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 69  er.** callback i
451a0 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 76 6f  s currently invo
451b0 6b 65 64 20 6f 6e 6c 79 20 66 72 6f 6d 20 77 69  ked only from wi
451c0 74 68 69 6e 20 70 61 67 65 72 2e 63 2e 0a 2a 2f  thin pager.c..*/
451d0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
451e0 42 75 73 79 48 61 6e 64 6c 65 72 20 42 75 73 79  BusyHandler Busy
451f0 48 61 6e 64 6c 65 72 3b 0a 73 74 72 75 63 74 20  Handler;.struct 
45200 42 75 73 79 48 61 6e 64 6c 65 72 20 7b 0a 20 20  BusyHandler {.  
45210 69 6e 74 20 28 2a 78 46 75 6e 63 29 28 76 6f 69  int (*xFunc)(voi
45220 64 20 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 54 68  d *,int);  /* Th
45230 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20  e busy callback 
45240 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 3b  */.  void *pArg;
45250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45260 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74 6f 20  /* First arg to 
45270 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f  busy callback */
45280 0a 20 20 69 6e 74 20 6e 42 75 73 79 3b 20 20 20  .  int nBusy;   
45290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
452a0 20 49 6e 63 72 65 6d 65 6e 74 65 64 20 77 69 74   Incremented wit
452b0 68 20 65 61 63 68 20 62 75 73 79 20 63 61 6c 6c  h each busy call
452c0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4e 61   */.};../*.** Na
452d0 6d 65 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72  me of the master
452e0 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e   database table.
452f0 20 20 54 68 65 20 6d 61 73 74 65 72 20 64 61 74    The master dat
45300 61 62 61 73 65 20 74 61 62 6c 65 0a 2a 2a 20 69  abase table.** i
45310 73 20 61 20 73 70 65 63 69 61 6c 20 74 61 62 6c  s a special tabl
45320 65 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65  e that holds the
45330 20 6e 61 6d 65 73 20 61 6e 64 20 61 74 74 72 69   names and attri
45340 62 75 74 65 73 20 6f 66 20 61 6c 6c 0a 2a 2a 20  butes of all.** 
45350 75 73 65 72 20 74 61 62 6c 65 73 20 61 6e 64 20  user tables and 
45360 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a 23 64 65 66  indices..*/.#def
45370 69 6e 65 20 4d 41 53 54 45 52 5f 4e 41 4d 45 20  ine MASTER_NAME 
45380 20 20 20 20 20 20 22 73 71 6c 69 74 65 5f 6d 61        "sqlite_ma
45390 73 74 65 72 22 0a 23 64 65 66 69 6e 65 20 54 45  ster".#define TE
453a0 4d 50 5f 4d 41 53 54 45 52 5f 4e 41 4d 45 20 20  MP_MASTER_NAME  
453b0 22 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73  "sqlite_temp_mas
453c0 74 65 72 22 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ter"../*.** The 
453d0 72 6f 6f 74 2d 70 61 67 65 20 6f 66 20 74 68 65  root-page of the
453e0 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 65   master database
453f0 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69   table..*/.#defi
45400 6e 65 20 4d 41 53 54 45 52 5f 52 4f 4f 54 20 20  ne MASTER_ROOT  
45410 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68       1../*.** Th
45420 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 73 63  e name of the sc
45430 68 65 6d 61 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23  hema table..*/.#
45440 64 65 66 69 6e 65 20 53 43 48 45 4d 41 5f 54 41  define SCHEMA_TA
45450 42 4c 45 28 78 29 20 20 28 28 21 4f 4d 49 54 5f  BLE(x)  ((!OMIT_
45460 54 45 4d 50 44 42 29 26 26 28 78 3d 3d 31 29 3f  TEMPDB)&&(x==1)?
45470 54 45 4d 50 5f 4d 41 53 54 45 52 5f 4e 41 4d 45  TEMP_MASTER_NAME
45480 3a 4d 41 53 54 45 52 5f 4e 41 4d 45 29 0a 0a 2f  :MASTER_NAME)../
45490 2a 0a 2a 2a 20 41 20 63 6f 6e 76 65 6e 69 65 6e  *.** A convenien
454a0 63 65 20 6d 61 63 72 6f 20 74 68 61 74 20 72 65  ce macro that re
454b0 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
454c0 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 0a   of elements in.
454d0 2a 2a 20 61 6e 20 61 72 72 61 79 2e 0a 2a 2f 0a  ** an array..*/.
454e0 23 64 65 66 69 6e 65 20 41 72 72 61 79 53 69 7a  #define ArraySiz
454f0 65 28 58 29 20 20 20 20 28 73 69 7a 65 6f 66 28  e(X)    (sizeof(
45500 58 29 2f 73 69 7a 65 6f 66 28 58 5b 30 5d 29 29  X)/sizeof(X[0]))
45510 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20  ../*.** Forward 
45520 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 73 74  references to st
45530 72 75 63 74 75 72 65 73 0a 2a 2f 0a 74 79 70 65  ructures.*/.type
45540 64 65 66 20 73 74 72 75 63 74 20 41 67 67 49 6e  def struct AggIn
45550 66 6f 20 41 67 67 49 6e 66 6f 3b 0a 74 79 70 65  fo AggInfo;.type
45560 64 65 66 20 73 74 72 75 63 74 20 41 75 74 68 43  def struct AuthC
45570 6f 6e 74 65 78 74 20 41 75 74 68 43 6f 6e 74 65  ontext AuthConte
45580 78 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  xt;.typedef stru
45590 63 74 20 42 69 74 76 65 63 20 42 69 74 76 65 63  ct Bitvec Bitvec
455a0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
455b0 20 43 6f 6c 6c 53 65 71 20 43 6f 6c 6c 53 65 71   CollSeq CollSeq
455c0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
455d0 20 43 6f 6c 75 6d 6e 20 43 6f 6c 75 6d 6e 3b 0a   Column Column;.
455e0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 44  typedef struct D
455f0 62 20 44 62 3b 0a 74 79 70 65 64 65 66 20 73 74  b Db;.typedef st
45600 72 75 63 74 20 53 63 68 65 6d 61 20 53 63 68 65  ruct Schema Sche
45610 6d 61 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  ma;.typedef stru
45620 63 74 20 45 78 70 72 20 45 78 70 72 3b 0a 74 79  ct Expr Expr;.ty
45630 70 65 64 65 66 20 73 74 72 75 63 74 20 45 78 70  pedef struct Exp
45640 72 4c 69 73 74 20 45 78 70 72 4c 69 73 74 3b 0a  rList ExprList;.
45650 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46  typedef struct F
45660 4b 65 79 20 46 4b 65 79 3b 0a 74 79 70 65 64 65  Key FKey;.typede
45670 66 20 73 74 72 75 63 74 20 46 75 6e 63 44 65 66  f struct FuncDef
45680 20 46 75 6e 63 44 65 66 3b 0a 74 79 70 65 64 65   FuncDef;.typede
45690 66 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 20  f struct IdList 
456a0 49 64 4c 69 73 74 3b 0a 74 79 70 65 64 65 66 20  IdList;.typedef 
456b0 73 74 72 75 63 74 20 49 6e 64 65 78 20 49 6e 64  struct Index Ind
456c0 65 78 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  ex;.typedef stru
456d0 63 74 20 4b 65 79 43 6c 61 73 73 20 4b 65 79 43  ct KeyClass KeyC
456e0 6c 61 73 73 3b 0a 74 79 70 65 64 65 66 20 73 74  lass;.typedef st
456f0 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 4b 65 79  ruct KeyInfo Key
45700 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74  Info;.typedef st
45710 72 75 63 74 20 4d 6f 64 75 6c 65 20 4d 6f 64 75  ruct Module Modu
45720 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  le;.typedef stru
45730 63 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 4e  ct NameContext N
45740 61 6d 65 43 6f 6e 74 65 78 74 3b 0a 74 79 70 65  ameContext;.type
45750 64 65 66 20 73 74 72 75 63 74 20 50 61 72 73 65  def struct Parse
45760 20 50 61 72 73 65 3b 0a 74 79 70 65 64 65 66 20   Parse;.typedef 
45770 73 74 72 75 63 74 20 53 65 6c 65 63 74 20 53 65  struct Select Se
45780 6c 65 63 74 3b 0a 74 79 70 65 64 65 66 20 73 74  lect;.typedef st
45790 72 75 63 74 20 53 72 63 4c 69 73 74 20 53 72 63  ruct SrcList Src
457a0 4c 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73 74  List;.typedef st
457b0 72 75 63 74 20 53 74 72 41 63 63 75 6d 20 53 74  ruct StrAccum St
457c0 72 41 63 63 75 6d 3b 0a 74 79 70 65 64 65 66 20  rAccum;.typedef 
457d0 73 74 72 75 63 74 20 54 61 62 6c 65 20 54 61 62  struct Table Tab
457e0 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  le;.typedef stru
457f0 63 74 20 54 61 62 6c 65 4c 6f 63 6b 20 54 61 62  ct TableLock Tab
45800 6c 65 4c 6f 63 6b 3b 0a 74 79 70 65 64 65 66 20  leLock;.typedef 
45810 73 74 72 75 63 74 20 54 6f 6b 65 6e 20 54 6f 6b  struct Token Tok
45820 65 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  en;.typedef stru
45830 63 74 20 54 72 69 67 67 65 72 53 74 61 63 6b 20  ct TriggerStack 
45840 54 72 69 67 67 65 72 53 74 61 63 6b 3b 0a 74 79  TriggerStack;.ty
45850 70 65 64 65 66 20 73 74 72 75 63 74 20 54 72 69  pedef struct Tri
45860 67 67 65 72 53 74 65 70 20 54 72 69 67 67 65 72  ggerStep Trigger
45870 53 74 65 70 3b 0a 74 79 70 65 64 65 66 20 73 74  Step;.typedef st
45880 72 75 63 74 20 54 72 69 67 67 65 72 20 54 72 69  ruct Trigger Tri
45890 67 67 65 72 3b 0a 74 79 70 65 64 65 66 20 73 74  gger;.typedef st
458a0 72 75 63 74 20 57 68 65 72 65 49 6e 66 6f 20 57  ruct WhereInfo W
458b0 68 65 72 65 49 6e 66 6f 3b 0a 74 79 70 65 64 65  hereInfo;.typede
458c0 66 20 73 74 72 75 63 74 20 57 68 65 72 65 4c 65  f struct WhereLe
458d0 76 65 6c 20 57 68 65 72 65 4c 65 76 65 6c 3b 0a  vel WhereLevel;.
458e0 0a 2f 2a 0a 2a 2a 20 44 65 66 65 72 20 73 6f 75  ./*.** Defer sou
458f0 72 63 69 6e 67 20 76 64 62 65 2e 68 20 61 6e 64  rcing vdbe.h and
45900 20 62 74 72 65 65 2e 68 20 75 6e 74 69 6c 20 61   btree.h until a
45910 66 74 65 72 20 74 68 65 20 22 75 38 22 20 61 6e  fter the "u8" an
45920 64 20 0a 2a 2a 20 22 42 75 73 79 48 61 6e 64 6c  d .** "BusyHandl
45930 65 72 22 20 74 79 70 65 64 65 66 73 2e 20 76 64  er" typedefs. vd
45940 62 65 2e 68 20 61 6c 73 6f 20 72 65 71 75 69 72  be.h also requir
45950 65 73 20 61 20 66 65 77 20 6f 66 20 74 68 65 20  es a few of the 
45960 6f 70 61 71 75 65 0a 2a 2a 20 70 6f 69 6e 74 65  opaque.** pointe
45970 72 20 74 79 70 65 73 20 28 69 2e 65 2e 20 46 75  r types (i.e. Fu
45980 6e 63 44 65 66 29 20 64 65 66 69 6e 65 64 20 61  ncDef) defined a
45990 62 6f 76 65 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  bove..*/./******
459a0 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
459b0 20 62 74 72 65 65 2e 68 20 69 6e 20 74 68 65 20   btree.h in the 
459c0 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65  middle of sqlite
459d0 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
459e0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
459f0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
45a00 69 6c 65 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a  ile btree.h ****
45a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45a30 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
45a40 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
45a50 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
45a60 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
45a70 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
45a80 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
45a90 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
45aa0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
45ab0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
45ac0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
45ad0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
45ae0 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
45af0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
45b00 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
45b10 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
45b20 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
45b30 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
45b40 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
45b50 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
45b60 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
45b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
45bb0 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66  ** This header f
45bc0 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20  ile defines the 
45bd0 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74  interface that t
45be0 68 65 20 73 71 6c 69 74 65 20 42 2d 54 72 65 65  he sqlite B-Tree
45bf0 20 66 69 6c 65 0a 2a 2a 20 73 75 62 73 79 73 74   file.** subsyst
45c00 65 6d 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74  em.  See comment
45c10 73 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20  s in the source 
45c20 63 6f 64 65 20 66 6f 72 20 61 20 64 65 74 61 69  code for a detai
45c30 6c 65 64 20 64 65 73 63 72 69 70 74 69 6f 6e 0a  led description.
45c40 2a 2a 20 6f 66 20 77 68 61 74 20 65 61 63 68 20  ** of what each 
45c50 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e  interface routin
45c60 65 20 64 6f 65 73 2e 0a 2a 2a 0a 2a 2a 20 40 28  e does..**.** @(
45c70 23 29 20 24 49 64 3a 20 62 74 72 65 65 2e 68 2c  #) $Id: btree.h,
45c80 76 20 31 2e 39 38 20 32 30 30 38 2f 30 34 2f 32  v 1.98 2008/04/2
45c90 36 20 31 33 3a 33 39 3a 34 37 20 64 72 68 20 45  6 13:39:47 drh E
45ca0 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  xp $.*/.#ifndef 
45cb0 5f 42 54 52 45 45 5f 48 5f 0a 23 64 65 66 69 6e  _BTREE_H_.#defin
45cc0 65 20 5f 42 54 52 45 45 5f 48 5f 0a 0a 2f 2a 20  e _BTREE_H_../* 
45cd0 54 4f 44 4f 3a 20 54 68 69 73 20 64 65 66 69 6e  TODO: This defin
45ce0 69 74 69 6f 6e 20 69 73 20 6a 75 73 74 20 69 6e  ition is just in
45cf0 63 6c 75 64 65 64 20 73 6f 20 6f 74 68 65 72 20  cluded so other 
45d00 6d 6f 64 75 6c 65 73 20 63 6f 6d 70 69 6c 65 2e  modules compile.
45d10 20 49 74 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20   It.** needs to 
45d20 62 65 20 72 65 76 69 73 69 74 65 64 2e 0a 2a 2f  be revisited..*/
45d30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
45d40 4e 5f 42 54 52 45 45 5f 4d 45 54 41 20 31 30 0a  N_BTREE_META 10.
45d50 0a 2f 2a 0a 2a 2a 20 49 66 20 64 65 66 69 6e 65  ./*.** If define
45d60 64 20 61 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 61  d as non-zero, a
45d70 75 74 6f 2d 76 61 63 75 75 6d 20 69 73 20 65 6e  uto-vacuum is en
45d80 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74  abled by default
45d90 2e 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a 20 69  . Otherwise.** i
45da0 74 20 6d 75 73 74 20 62 65 20 74 75 72 6e 65 64  t must be turned
45db0 20 6f 6e 20 66 6f 72 20 65 61 63 68 20 64 61 74   on for each dat
45dc0 61 62 61 73 65 20 75 73 69 6e 67 20 22 50 52 41  abase using "PRA
45dd0 47 4d 41 20 61 75 74 6f 5f 76 61 63 75 75 6d 20  GMA auto_vacuum 
45de0 3d 20 31 22 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  = 1"..*/.#ifndef
45df0 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
45e00 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 23 64 65  AUTOVACUUM.  #de
45e10 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41  fine SQLITE_DEFA
45e20 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 30  ULT_AUTOVACUUM 0
45e30 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65  .#endif..#define
45e40 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55   BTREE_AUTOVACUU
45e50 4d 5f 4e 4f 4e 45 20 30 20 20 20 20 20 20 20 20  M_NONE 0        
45e60 2f 2a 20 44 6f 20 6e 6f 74 20 64 6f 20 61 75 74  /* Do not do aut
45e70 6f 2d 76 61 63 75 75 6d 20 2a 2f 0a 23 64 65 66  o-vacuum */.#def
45e80 69 6e 65 20 42 54 52 45 45 5f 41 55 54 4f 56 41  ine BTREE_AUTOVA
45e90 43 55 55 4d 5f 46 55 4c 4c 20 31 20 20 20 20 20  CUUM_FULL 1     
45ea0 20 20 20 2f 2a 20 44 6f 20 66 75 6c 6c 20 61 75     /* Do full au
45eb0 74 6f 2d 76 61 63 75 75 6d 20 2a 2f 0a 23 64 65  to-vacuum */.#de
45ec0 66 69 6e 65 20 42 54 52 45 45 5f 41 55 54 4f 56  fine BTREE_AUTOV
45ed0 41 43 55 55 4d 5f 49 4e 43 52 20 32 20 20 20 20  ACUUM_INCR 2    
45ee0 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74      /* Increment
45ef0 61 6c 20 76 61 63 75 75 6d 20 2a 2f 0a 0a 2f 2a  al vacuum */../*
45f00 0a 2a 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c  .** Forward decl
45f10 61 72 61 74 69 6f 6e 73 20 6f 66 20 73 74 72 75  arations of stru
45f20 63 74 75 72 65 0a 2a 2f 0a 74 79 70 65 64 65 66  cture.*/.typedef
45f30 20 73 74 72 75 63 74 20 42 74 72 65 65 20 42 74   struct Btree Bt
45f40 72 65 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ree;.typedef str
45f50 75 63 74 20 42 74 43 75 72 73 6f 72 20 42 74 43  uct BtCursor BtC
45f60 75 72 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73  ursor;.typedef s
45f70 74 72 75 63 74 20 42 74 53 68 61 72 65 64 20 42  truct BtShared B
45f80 74 53 68 61 72 65 64 3b 0a 74 79 70 65 64 65 66  tShared;.typedef
45f90 20 73 74 72 75 63 74 20 42 74 72 65 65 4d 75 74   struct BtreeMut
45fa0 65 78 41 72 72 61 79 20 42 74 72 65 65 4d 75 74  exArray BtreeMut
45fb0 65 78 41 72 72 61 79 3b 0a 0a 2f 2a 0a 2a 2a 20  exArray;../*.** 
45fc0 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 72  This structure r
45fd0 65 63 6f 72 64 73 20 61 6c 6c 20 6f 66 20 74 68  ecords all of th
45fe0 65 20 42 74 72 65 65 73 20 74 68 61 74 20 6e 65  e Btrees that ne
45ff0 65 64 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 61 20  ed to hold.** a 
46000 6d 75 74 65 78 20 62 65 66 6f 72 65 20 77 65 20  mutex before we 
46010 65 6e 74 65 72 20 73 71 6c 69 74 65 33 56 64 62  enter sqlite3Vdb
46020 65 45 78 65 63 28 29 2e 20 20 54 68 65 20 42 74  eExec().  The Bt
46030 72 65 65 73 20 61 72 65 0a 2a 2a 20 61 72 65 20  rees are.** are 
46040 70 6c 61 63 65 64 20 69 6e 20 61 42 74 72 65 65  placed in aBtree
46050 5b 5d 20 69 6e 20 6f 72 64 65 72 20 6f 66 20 61  [] in order of a
46060 42 74 72 65 65 5b 5d 2d 3e 70 42 74 2e 20 20 54  Btree[]->pBt.  T
46070 68 61 74 20 77 61 79 2c 0a 2a 2a 20 77 65 20 63  hat way,.** we c
46080 61 6e 20 61 6c 77 61 79 73 20 6c 6f 63 6b 20 61  an always lock a
46090 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 6d 20 61  nd unlock them a
460a0 6c 6c 20 71 75 69 63 6b 6c 79 2e 0a 2a 2f 0a 73  ll quickly..*/.s
460b0 74 72 75 63 74 20 42 74 72 65 65 4d 75 74 65 78  truct BtreeMutex
460c0 41 72 72 61 79 20 7b 0a 20 20 69 6e 74 20 6e 4d  Array {.  int nM
460d0 75 74 65 78 3b 0a 20 20 42 74 72 65 65 20 2a 61  utex;.  Btree *a
460e0 42 74 72 65 65 5b 53 51 4c 49 54 45 5f 4d 41 58  Btree[SQLITE_MAX
460f0 5f 41 54 54 41 43 48 45 44 2b 31 5d 3b 0a 7d 3b  _ATTACHED+1];.};
46100 0a 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ...SQLITE_PRIVAT
46110 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
46120 65 65 4f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20  eeOpen(.  const 
46130 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c  char *zFilename,
46140 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61     /* Name of da
46150 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 6f  tabase file to o
46160 70 65 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  pen */.  sqlite3
46170 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
46180 20 20 2f 2a 20 41 73 73 6f 63 69 61 74 65 64 20    /* Associated 
46190 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
461a0 69 6f 6e 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a  ion */.  Btree *
461b0 2a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *,              
461c0 20 20 2f 2a 20 52 65 74 75 72 6e 20 6f 70 65 6e    /* Return open
461d0 20 42 74 72 65 65 2a 20 68 65 72 65 20 2a 2f 0a   Btree* here */.
461e0 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20    int flags,    
461f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c             /* Fl
46200 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73  ags */.  int vfs
46210 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20  Flags           
46220 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65    /* Flags passe
46230 64 20 74 68 72 6f 75 67 68 20 74 6f 20 56 46 53  d through to VFS
46240 20 6f 70 65 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 20   open */.);../* 
46250 54 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65  The flags parame
46260 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 42 74  ter to sqlite3Bt
46270 72 65 65 4f 70 65 6e 20 63 61 6e 20 62 65 20 74  reeOpen can be t
46280 68 65 20 62 69 74 77 69 73 65 20 6f 72 20 6f 66  he bitwise or of
46290 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e   the.** followin
462a0 67 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20  g values..**.** 
462b0 4e 4f 54 45 3a 20 20 54 68 65 73 65 20 76 61 6c  NOTE:  These val
462c0 75 65 73 20 6d 75 73 74 20 6d 61 74 63 68 20 74  ues must match t
462d0 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
462e0 20 50 41 47 45 52 5f 20 76 61 6c 75 65 73 20 69   PAGER_ values i
462f0 6e 0a 2a 2a 20 70 61 67 65 72 2e 68 2e 0a 2a 2f  n.** pager.h..*/
46300 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4f  .#define BTREE_O
46310 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 20 31 20 20  MIT_JOURNAL  1  
46320 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 65 20 6a 6f  /* Do not use jo
46330 75 72 6e 61 6c 2e 20 20 4e 6f 20 61 72 67 75 6d  urnal.  No argum
46340 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42  ent */.#define B
46350 54 52 45 45 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b  TREE_NO_READLOCK
46360 20 20 20 32 20 20 2f 2a 20 4f 6d 69 74 20 72 65     2  /* Omit re
46370 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 65 61 64 6f  adlocks on reado
46380 6e 6c 79 20 66 69 6c 65 73 20 2a 2f 0a 23 64 65  nly files */.#de
46390 66 69 6e 65 20 42 54 52 45 45 5f 4d 45 4d 4f 52  fine BTREE_MEMOR
463a0 59 20 20 20 20 20 20 20 20 34 20 20 2f 2a 20 49  Y        4  /* I
463b0 6e 2d 6d 65 6d 6f 72 79 20 44 42 2e 20 20 4e 6f  n-memory DB.  No
463c0 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 23 64 65   argument */.#de
463d0 66 69 6e 65 20 42 54 52 45 45 5f 52 45 41 44 4f  fine BTREE_READO
463e0 4e 4c 59 20 20 20 20 20 20 38 20 20 2f 2a 20 4f  NLY      8  /* O
463f0 70 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  pen the database
46400 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f   in read-only mo
46410 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54  de */.#define BT
46420 52 45 45 5f 52 45 41 44 57 52 49 54 45 20 20 20  REE_READWRITE   
46430 20 31 36 20 20 2f 2a 20 4f 70 65 6e 20 66 6f 72   16  /* Open for
46440 20 62 6f 74 68 20 72 65 61 64 69 6e 67 20 61 6e   both reading an
46450 64 20 77 72 69 74 69 6e 67 20 2a 2f 0a 23 64 65  d writing */.#de
46460 66 69 6e 65 20 42 54 52 45 45 5f 43 52 45 41 54  fine BTREE_CREAT
46470 45 20 20 20 20 20 20 20 33 32 20 20 2f 2a 20 43  E       32  /* C
46480 72 65 61 74 65 20 74 68 65 20 64 61 74 61 62 61  reate the databa
46490 73 65 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f  se if it does no
464a0 74 20 65 78 69 73 74 20 2a 2f 0a 0a 2f 2a 20 41  t exist */../* A
464b0 64 64 69 74 69 6f 6e 61 6c 20 76 61 6c 75 65 73  dditional values
464c0 20 66 6f 72 20 74 68 65 20 34 74 68 20 61 72 67   for the 4th arg
464d0 75 6d 65 6e 74 20 6f 66 20 73 71 6c 69 74 65 33  ument of sqlite3
464e0 42 74 72 65 65 4f 70 65 6e 20 74 68 61 74 0a 2a  BtreeOpen that.*
464f0 2a 20 61 72 65 20 6e 6f 74 20 61 73 73 6f 63 69  * are not associ
46500 61 74 65 64 20 77 69 74 68 20 50 41 47 45 52 5f  ated with PAGER_
46510 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 23 64 65 66   values..*/.#def
46520 69 6e 65 20 42 54 52 45 45 5f 50 52 49 56 41 54  ine BTREE_PRIVAT
46530 45 20 20 20 20 20 20 36 34 20 20 2f 2a 20 4e 65  E      64  /* Ne
46540 76 65 72 20 73 68 61 72 65 20 77 69 74 68 20 6f  ver share with o
46550 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  ther connections
46560 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56   */..SQLITE_PRIV
46570 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
46580 74 72 65 65 43 6c 6f 73 65 28 42 74 72 65 65 2a  treeClose(Btree*
46590 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
465a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
465b0 65 65 53 65 74 43 61 63 68 65 53 69 7a 65 28 42  eeSetCacheSize(B
465c0 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  tree*,int);.SQLI
465d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
465e0 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 53 61  qlite3BtreeSetSa
465f0 66 65 74 79 4c 65 76 65 6c 28 42 74 72 65 65 2a  fetyLevel(Btree*
46600 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ,int,int);.SQLIT
46610 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
46620 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63 44 69  lite3BtreeSyncDi
46630 73 61 62 6c 65 64 28 42 74 72 65 65 2a 29 3b 0a  sabled(Btree*);.
46640 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
46650 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  nt sqlite3BtreeS
46660 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 65  etPageSize(Btree
46670 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49  *,int,int);.SQLI
46680 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
46690 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61  qlite3BtreeGetPa
466a0 67 65 53 69 7a 65 28 42 74 72 65 65 2a 29 3b 0a  geSize(Btree*);.
466b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
466c0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  nt sqlite3BtreeM
466d0 61 78 50 61 67 65 43 6f 75 6e 74 28 42 74 72 65  axPageCount(Btre
466e0 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  e*,int);.SQLITE_
466f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
46700 74 65 33 42 74 72 65 65 47 65 74 52 65 73 65 72  te3BtreeGetReser
46710 76 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49  ve(Btree*);.SQLI
46720 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
46730 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 75  qlite3BtreeSetAu
46740 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a  toVacuum(Btree *
46750 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
46760 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
46770 65 33 42 74 72 65 65 47 65 74 41 75 74 6f 56 61  e3BtreeGetAutoVa
46780 63 75 75 6d 28 42 74 72 65 65 20 2a 29 3b 0a 53  cuum(Btree *);.S
46790 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
467a0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65  t sqlite3BtreeBe
467b0 67 69 6e 54 72 61 6e 73 28 42 74 72 65 65 2a 2c  ginTrans(Btree*,
467c0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
467d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
467e0 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
467f0 4f 6e 65 28 42 74 72 65 65 2a 2c 20 63 6f 6e 73  One(Btree*, cons
46800 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29  t char *zMaster)
46810 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
46820 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
46830 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28  eCommitPhaseTwo(
46840 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Btree*);.SQLITE_
46850 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
46860 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 42  te3BtreeCommit(B
46870 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  tree*);.SQLITE_P
46880 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
46890 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28  e3BtreeRollback(
468a0 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Btree*);.SQLITE_
468b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
468c0 74 65 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d  te3BtreeBeginStm
468d0 74 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54  t(Btree*);.SQLIT
468e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
468f0 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74  lite3BtreeCommit
46900 53 74 6d 74 28 42 74 72 65 65 2a 29 3b 0a 53 51  Stmt(Btree*);.SQ
46910 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
46920 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c   sqlite3BtreeRol
46930 6c 62 61 63 6b 53 74 6d 74 28 42 74 72 65 65 2a  lbackStmt(Btree*
46940 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
46950 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
46960 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 42 74  eeCreateTable(Bt
46970 72 65 65 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 20  ree*, int*, int 
46980 66 6c 61 67 73 29 3b 0a 53 51 4c 49 54 45 5f 50  flags);.SQLITE_P
46990 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
469a0 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73  e3BtreeIsInTrans
469b0 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
469c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
469d0 69 74 65 33 42 74 72 65 65 49 73 49 6e 53 74 6d  ite3BtreeIsInStm
469e0 74 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54  t(Btree*);.SQLIT
469f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
46a00 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 65  lite3BtreeIsInRe
46a10 61 64 54 72 61 6e 73 28 42 74 72 65 65 2a 29 3b  adTrans(Btree*);
46a20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
46a30 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72  void *sqlite3Btr
46a40 65 65 53 63 68 65 6d 61 28 42 74 72 65 65 20 2a  eeSchema(Btree *
46a50 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76  , int, void(*)(v
46a60 6f 69 64 20 2a 29 29 3b 0a 53 51 4c 49 54 45 5f  oid *));.SQLITE_
46a70 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
46a80 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f  te3BtreeSchemaLo
46a90 63 6b 65 64 28 42 74 72 65 65 20 2a 29 3b 0a 53  cked(Btree *);.S
46aa0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
46ab0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 6f  t sqlite3BtreeLo
46ac0 63 6b 54 61 62 6c 65 28 42 74 72 65 65 20 2a 2c  ckTable(Btree *,
46ad0 20 69 6e 74 2c 20 75 38 29 3b 0a 0a 53 51 4c 49   int, u8);..SQLI
46ae0 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
46af0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74   char *sqlite3Bt
46b00 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 42  reeGetFilename(B
46b10 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  tree *);.SQLITE_
46b20 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
46b30 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  ar *sqlite3Btree
46b40 47 65 74 44 69 72 6e 61 6d 65 28 42 74 72 65 65  GetDirname(Btree
46b50 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
46b60 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
46b70 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a  sqlite3BtreeGetJ
46b80 6f 75 72 6e 61 6c 6e 61 6d 65 28 42 74 72 65 65  ournalname(Btree
46b90 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
46ba0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
46bb0 74 72 65 65 43 6f 70 79 46 69 6c 65 28 42 74 72  treeCopyFile(Btr
46bc0 65 65 20 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a  ee *, Btree *);.
46bd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
46be0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
46bf0 49 6e 63 72 56 61 63 75 75 6d 28 42 74 72 65 65  IncrVacuum(Btree
46c00 20 2a 29 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c 61   *);../* The fla
46c10 67 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  gs parameter to 
46c20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61  sqlite3BtreeCrea
46c30 74 65 54 61 62 6c 65 20 63 61 6e 20 62 65 20 74  teTable can be t
46c40 68 65 20 62 69 74 77 69 73 65 20 4f 52 0a 2a 2a  he bitwise OR.**
46c50 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
46c60 67 20 66 6c 61 67 73 3a 0a 2a 2f 0a 23 64 65 66  g flags:.*/.#def
46c70 69 6e 65 20 42 54 52 45 45 5f 49 4e 54 4b 45 59  ine BTREE_INTKEY
46c80 20 20 20 20 20 31 20 20 20 20 2f 2a 20 54 61 62       1    /* Tab
46c90 6c 65 20 68 61 73 20 6f 6e 6c 79 20 36 34 2d 62  le has only 64-b
46ca0 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  it signed intege
46cb0 72 20 6b 65 79 73 20 2a 2f 0a 23 64 65 66 69 6e  r keys */.#defin
46cc0 65 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41  e BTREE_ZERODATA
46cd0 20 20 20 32 20 20 20 20 2f 2a 20 54 61 62 6c 65     2    /* Table
46ce0 20 68 61 73 20 6b 65 79 73 20 6f 6e 6c 79 20 2d   has keys only -
46cf0 20 6e 6f 20 64 61 74 61 20 2a 2f 0a 23 64 65 66   no data */.#def
46d00 69 6e 65 20 42 54 52 45 45 5f 4c 45 41 46 44 41  ine BTREE_LEAFDA
46d10 54 41 20 20 20 34 20 20 20 20 2f 2a 20 44 61 74  TA   4    /* Dat
46d20 61 20 73 74 6f 72 65 64 20 69 6e 20 6c 65 61 76  a stored in leav
46d30 65 73 20 6f 6e 6c 79 2e 20 20 49 6d 70 6c 69 65  es only.  Implie
46d40 73 20 49 4e 54 4b 45 59 20 2a 2f 0a 0a 53 51 4c  s INTKEY */..SQL
46d50 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
46d60 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70  sqlite3BtreeDrop
46d70 54 61 62 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e  Table(Btree*, in
46d80 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45  t, int*);.SQLITE
46d90 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
46da0 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 61  ite3BtreeClearTa
46db0 62 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 29  ble(Btree*, int)
46dc0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
46dd0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
46de0 65 47 65 74 4d 65 74 61 28 42 74 72 65 65 2a 2c  eGetMeta(Btree*,
46df0 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20 2a 70   int idx, u32 *p
46e00 56 61 6c 75 65 29 3b 0a 53 51 4c 49 54 45 5f 50  Value);.SQLITE_P
46e10 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
46e20 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74  e3BtreeUpdateMet
46e30 61 28 42 74 72 65 65 2a 2c 20 69 6e 74 20 69 64  a(Btree*, int id
46e40 78 2c 20 75 33 32 20 76 61 6c 75 65 29 3b 0a 53  x, u32 value);.S
46e50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
46e60 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 54  id sqlite3BtreeT
46e70 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 42 74  ripAllCursors(Bt
46e80 72 65 65 2a 2c 20 69 6e 74 29 3b 0a 0a 73 74 72  ree*, int);..str
46e90 75 63 74 20 55 6e 70 61 63 6b 65 64 52 65 63 6f  uct UnpackedReco
46ea0 72 64 3b 20 20 2f 2a 20 46 6f 72 77 61 72 64 20  rd;  /* Forward 
46eb0 64 65 63 6c 61 72 61 74 69 6f 6e 2e 20 20 44 65  declaration.  De
46ec0 66 69 6e 69 74 69 6f 6e 20 69 6e 20 76 64 62 65  finition in vdbe
46ed0 61 75 78 2e 63 2e 20 2a 2f 0a 0a 53 51 4c 49 54  aux.c. */..SQLIT
46ee0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
46ef0 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
46f00 28 0a 20 20 42 74 72 65 65 2a 2c 20 20 20 20 20  (.  Btree*,     
46f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46f20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 54 72 65           /* BTre
46f30 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62  e containing tab
46f40 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20  le to open */.  
46f50 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20  int iTable,     
46f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46f70 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
46f80 20 72 6f 6f 74 20 70 61 67 65 20 2a 2f 0a 20 20   root page */.  
46f90 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20  int wrFlag,     
46fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46fb0 20 20 20 20 20 2f 2a 20 31 20 66 6f 72 20 77 72       /* 1 for wr
46fc0 69 74 69 6e 67 2e 20 20 30 20 66 6f 72 20 72 65  iting.  0 for re
46fd0 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72  ad-only */.  str
46fe0 75 63 74 20 4b 65 79 49 6e 66 6f 2a 2c 20 20 20  uct KeyInfo*,   
46ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47000 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d    /* First argum
47010 65 6e 74 20 74 6f 20 63 6f 6d 70 61 72 65 20 66  ent to compare f
47020 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 43  unction */.  BtC
47030 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 20 20  ursor *pCursor  
47040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47050 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 77 72    /* Space to wr
47060 69 74 65 20 63 75 72 73 6f 72 20 73 74 72 75 63  ite cursor struc
47070 74 75 72 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54  ture */.);.SQLIT
47080 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
47090 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
470a0 53 69 7a 65 28 76 6f 69 64 29 3b 0a 0a 53 51 4c  Size(void);..SQL
470b0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
470c0 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73  sqlite3BtreeClos
470d0 65 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72  eCursor(BtCursor
470e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
470f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
47100 72 65 65 4d 6f 76 65 74 6f 28 0a 20 20 42 74 43  reeMoveto(.  BtC
47110 75 72 73 6f 72 2a 2c 0a 20 20 63 6f 6e 73 74 20  ursor*,.  const 
47120 76 6f 69 64 20 2a 70 4b 65 79 2c 0a 20 20 73 74  void *pKey,.  st
47130 72 75 63 74 20 55 6e 70 61 63 6b 65 64 52 65 63  ruct UnpackedRec
47140 6f 72 64 20 2a 70 55 6e 4b 65 79 2c 0a 20 20 69  ord *pUnKey,.  i
47150 36 34 20 6e 4b 65 79 2c 0a 20 20 69 6e 74 20 62  64 nKey,.  int b
47160 69 61 73 2c 0a 20 20 69 6e 74 20 2a 70 52 65 73  ias,.  int *pRes
47170 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .);.SQLITE_PRIVA
47180 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
47190 72 65 65 44 65 6c 65 74 65 28 42 74 43 75 72 73  reeDelete(BtCurs
471a0 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  or*);.SQLITE_PRI
471b0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
471c0 42 74 72 65 65 49 6e 73 65 72 74 28 42 74 43 75  BtreeInsert(BtCu
471d0 72 73 6f 72 2a 2c 20 63 6f 6e 73 74 20 76 6f 69  rsor*, const voi
471e0 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65  d *pKey, i64 nKe
471f0 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  y,.             
47200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47210 20 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20       const void 
47220 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74  *pData, int nDat
47230 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  a,.             
47240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47250 20 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f 2c 20       int nZero, 
47260 69 6e 74 20 62 69 61 73 29 3b 0a 53 51 4c 49 54  int bias);.SQLIT
47270 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
47280 6c 69 74 65 33 42 74 72 65 65 46 69 72 73 74 28  lite3BtreeFirst(
47290 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a  BtCursor*, int *
472a0 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52  pRes);.SQLITE_PR
472b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
472c0 33 42 74 72 65 65 4c 61 73 74 28 42 74 43 75 72  3BtreeLast(BtCur
472d0 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29  sor*, int *pRes)
472e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
472f0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
47300 65 4e 65 78 74 28 42 74 43 75 72 73 6f 72 2a 2c  eNext(BtCursor*,
47310 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c   int *pRes);.SQL
47320 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
47330 73 71 6c 69 74 65 33 42 74 72 65 65 45 6f 66 28  sqlite3BtreeEof(
47340 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49  BtCursor*);.SQLI
47350 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
47360 71 6c 69 74 65 33 42 74 72 65 65 46 6c 61 67 73  qlite3BtreeFlags
47370 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c  (BtCursor*);.SQL
47380 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
47390 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76  sqlite3BtreePrev
473a0 69 6f 75 73 28 42 74 43 75 72 73 6f 72 2a 2c 20  ious(BtCursor*, 
473b0 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49  int *pRes);.SQLI
473c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
473d0 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69  qlite3BtreeKeySi
473e0 7a 65 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 36  ze(BtCursor*, i6
473f0 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c 49 54  4 *pSize);.SQLIT
47400 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
47410 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28 42 74  lite3BtreeKey(Bt
47420 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66  Cursor*, u32 off
47430 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f  set, u32 amt, vo
47440 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  id*);.SQLITE_PRI
47450 56 41 54 45 20 73 71 6c 69 74 65 33 20 2a 73 71  VATE sqlite3 *sq
47460 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
47470 44 62 28 63 6f 6e 73 74 20 42 74 43 75 72 73 6f  Db(const BtCurso
47480 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
47490 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  ATE const void *
474a0 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 46  sqlite3BtreeKeyF
474b0 65 74 63 68 28 42 74 43 75 72 73 6f 72 2a 2c 20  etch(BtCursor*, 
474c0 69 6e 74 20 2a 70 41 6d 74 29 3b 0a 53 51 4c 49  int *pAmt);.SQLI
474d0 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
474e0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74   void *sqlite3Bt
474f0 72 65 65 44 61 74 61 46 65 74 63 68 28 42 74 43  reeDataFetch(BtC
47500 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 41 6d  ursor*, int *pAm
47510 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
47520 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
47530 72 65 65 44 61 74 61 53 69 7a 65 28 42 74 43 75  reeDataSize(BtCu
47540 72 73 6f 72 2a 2c 20 75 33 32 20 2a 70 53 69 7a  rsor*, u32 *pSiz
47550 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  e);.SQLITE_PRIVA
47560 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
47570 72 65 65 44 61 74 61 28 42 74 43 75 72 73 6f 72  reeData(BtCursor
47580 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75  *, u32 offset, u
47590 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a  32 amt, void*);.
475a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
475b0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72  char *sqlite3Btr
475c0 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 63 6b  eeIntegrityCheck
475d0 28 42 74 72 65 65 2a 2c 20 69 6e 74 20 2a 61 52  (Btree*, int *aR
475e0 6f 6f 74 2c 20 69 6e 74 20 6e 52 6f 6f 74 2c 20  oot, int nRoot, 
475f0 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49  int, int*);.SQLI
47600 54 45 5f 50 52 49 56 41 54 45 20 73 74 72 75 63  TE_PRIVATE struc
47610 74 20 50 61 67 65 72 20 2a 73 71 6c 69 74 65 33  t Pager *sqlite3
47620 42 74 72 65 65 50 61 67 65 72 28 42 74 72 65 65  BtreePager(Btree
47630 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  *);..SQLITE_PRIV
47640 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
47650 74 72 65 65 50 75 74 44 61 74 61 28 42 74 43 75  treePutData(BtCu
47660 72 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73 65  rsor*, u32 offse
47670 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64  t, u32 amt, void
47680 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
47690 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
476a0 74 72 65 65 43 61 63 68 65 4f 76 65 72 66 6c 6f  treeCacheOverflo
476b0 77 28 42 74 43 75 72 73 6f 72 20 2a 29 3b 0a 0a  w(BtCursor *);..
476c0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
476d0 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ST.SQLITE_PRIVAT
476e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
476f0 65 65 43 75 72 73 6f 72 49 6e 66 6f 28 42 74 43  eeCursorInfo(BtC
47700 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 2c 20 69 6e  ursor*, int*, in
47710 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
47720 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
47730 74 72 65 65 43 75 72 73 6f 72 4c 69 73 74 28 42  treeCursorList(B
47740 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  tree*);.SQLITE_P
47750 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
47760 65 33 42 74 72 65 65 50 61 67 65 44 75 6d 70 28  e3BtreePageDump(
47770 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20 69 6e 74  Btree*, int, int
47780 20 72 65 63 75 72 73 69 76 65 29 3b 0a 23 65 6e   recursive);.#en
47790 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65  dif../*.** If we
477a0 20 61 72 65 20 6e 6f 74 20 75 73 69 6e 67 20 73   are not using s
477b0 68 61 72 65 64 20 63 61 63 68 65 2c 20 74 68 65  hared cache, the
477c0 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65  n there is no ne
477d0 65 64 20 74 6f 0a 2a 2a 20 75 73 65 20 6d 75 74  ed to.** use mut
477e0 65 78 65 73 20 74 6f 20 61 63 63 65 73 73 20 74  exes to access t
477f0 68 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75  he BtShared stru
47800 63 74 75 72 65 73 2e 20 20 53 6f 20 6d 61 6b 65  ctures.  So make
47810 20 74 68 65 0a 2a 2a 20 45 6e 74 65 72 20 61 6e   the.** Enter an
47820 64 20 4c 65 61 76 65 20 70 72 6f 63 65 64 75 72  d Leave procedur
47830 65 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 69  es no-ops..*/.#i
47840 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
47850 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
47860 43 48 45 29 20 26 26 20 53 51 4c 49 54 45 5f 54  CHE) && SQLITE_T
47870 48 52 45 41 44 53 41 46 45 0a 53 51 4c 49 54 45  HREADSAFE.SQLITE
47880 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
47890 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
478a0 72 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54  r(Btree*);.SQLIT
478b0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
478c0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
478d0 76 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49  ve(Btree*);.SQLI
478e0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
478f0 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
47900 64 73 4d 75 74 65 78 28 42 74 72 65 65 2a 29 3b  dsMutex(Btree*);
47910 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
47920 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74    void sqlite3Bt
47930 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 42  reeEnterCursor(B
47940 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54  tCursor*);.SQLIT
47950 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
47960 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
47970 76 65 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f  veCursor(BtCurso
47980 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
47990 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
479a0 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28  e3BtreeEnterAll(
479b0 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54  sqlite3*);.SQLIT
479c0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
479d0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
479e0 76 65 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b  veAll(sqlite3*);
479f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
47a00 20 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72    int sqlite3Btr
47a10 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65  eeHoldsAllMutexe
47a20 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c  s(sqlite3*);.SQL
47a30 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
47a40 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  id sqlite3BtreeM
47a50 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 42  utexArrayEnter(B
47a60 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 2a 29  treeMutexArray*)
47a70 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
47a80 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42     void sqlite3B
47a90 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65  treeMutexArrayLe
47aa0 61 76 65 28 42 74 72 65 65 4d 75 74 65 78 41 72  ave(BtreeMutexAr
47ab0 72 61 79 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ray*);.SQLITE_PR
47ac0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
47ad0 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72  ite3BtreeMutexAr
47ae0 72 61 79 49 6e 73 65 72 74 28 42 74 72 65 65 4d  rayInsert(BtreeM
47af0 75 74 65 78 41 72 72 61 79 2a 2c 20 42 74 72 65  utexArray*, Btre
47b00 65 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  e*);.#else.# def
47b10 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65  ine sqlite3Btree
47b20 45 6e 74 65 72 28 58 29 0a 23 20 64 65 66 69 6e  Enter(X).# defin
47b30 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65  e sqlite3BtreeLe
47b40 61 76 65 28 58 29 0a 23 20 64 65 66 69 6e 65 20  ave(X).# define 
47b50 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
47b60 73 4d 75 74 65 78 28 58 29 20 31 0a 23 20 64 65  sMutex(X) 1.# de
47b70 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65  fine sqlite3Btre
47b80 65 45 6e 74 65 72 43 75 72 73 6f 72 28 58 29 0a  eEnterCursor(X).
47b90 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
47ba0 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f 72  BtreeLeaveCursor
47bb0 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  (X).# define sql
47bc0 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c  ite3BtreeEnterAl
47bd0 6c 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71  l(X).# define sq
47be0 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41  lite3BtreeLeaveA
47bf0 6c 6c 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73  ll(X).# define s
47c00 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
47c10 41 6c 6c 4d 75 74 65 78 65 73 28 58 29 20 31 0a  AllMutexes(X) 1.
47c20 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
47c30 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 45  BtreeMutexArrayE
47c40 6e 74 65 72 28 58 29 0a 23 20 64 65 66 69 6e 65  nter(X).# define
47c50 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74   sqlite3BtreeMut
47c60 65 78 41 72 72 61 79 4c 65 61 76 65 28 58 29 0a  exArrayLeave(X).
47c70 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
47c80 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 49  BtreeMutexArrayI
47c90 6e 73 65 72 74 28 58 2c 59 29 0a 23 65 6e 64 69  nsert(X,Y).#endi
47ca0 66 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 42  f...#endif /* _B
47cb0 54 52 45 45 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a  TREE_H_ */../***
47cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
47cd0 6f 66 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a  of btree.h *****
47ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
47d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
47d20 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
47d30 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69  left off in sqli
47d40 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
47d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
47d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
47d70 75 64 65 20 76 64 62 65 2e 68 20 69 6e 20 74 68  ude vdbe.h in th
47d80 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69  e middle of sqli
47d90 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
47da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
47db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
47dc0 6e 20 66 69 6c 65 20 76 64 62 65 2e 68 20 2a 2a  n file vdbe.h **
47dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
47e00 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
47e10 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
47e20 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
47e30 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
47e40 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
47e50 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
47e60 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
47e70 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
47e80 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
47e90 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
47ea0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
47eb0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
47ec0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
47ed0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
47ee0 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
47ef0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
47f00 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
47f10 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
47f20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
47f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47f70 2a 2a 0a 2a 2a 20 48 65 61 64 65 72 20 66 69 6c  **.** Header fil
47f80 65 20 66 6f 72 20 74 68 65 20 56 69 72 74 75 61  e for the Virtua
47f90 6c 20 44 61 74 61 42 61 73 65 20 45 6e 67 69 6e  l DataBase Engin
47fa0 65 20 28 56 44 42 45 29 0a 2a 2a 0a 2a 2a 20 54  e (VDBE).**.** T
47fb0 68 69 73 20 68 65 61 64 65 72 20 64 65 66 69 6e  his header defin
47fc0 65 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65  es the interface
47fd0 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 20   to the virtual 
47fe0 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 0a  database engine.
47ff0 2a 2a 20 6f 72 20 56 44 42 45 2e 20 20 54 68 65  ** or VDBE.  The
48000 20 56 44 42 45 20 69 6d 70 6c 65 6d 65 6e 74 73   VDBE implements
48010 20 61 6e 20 61 62 73 74 72 61 63 74 20 6d 61 63   an abstract mac
48020 68 69 6e 65 20 74 68 61 74 20 72 75 6e 73 20 61  hine that runs a
48030 0a 2a 2a 20 73 69 6d 70 6c 65 20 70 72 6f 67 72  .** simple progr
48040 61 6d 20 74 6f 20 61 63 63 65 73 73 20 61 6e 64  am to access and
48050 20 6d 6f 64 69 66 79 20 74 68 65 20 75 6e 64 65   modify the unde
48060 72 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 2e  rlying database.
48070 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65  .**.** $Id: vdbe
48080 2e 68 2c 76 20 31 2e 31 33 31 20 32 30 30 38 2f  .h,v 1.131 2008/
48090 30 35 2f 30 31 20 31 37 3a 30 33 3a 34 39 20 64  05/01 17:03:49 d
480a0 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e  rh Exp $.*/.#ifn
480b0 64 65 66 20 5f 53 51 4c 49 54 45 5f 56 44 42 45  def _SQLITE_VDBE
480c0 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c  _H_.#define _SQL
480d0 49 54 45 5f 56 44 42 45 5f 48 5f 0a 0a 2f 2a 0a  ITE_VDBE_H_../*.
480e0 2a 2a 20 41 20 73 69 6e 67 6c 65 20 56 44 42 45  ** A single VDBE
480f0 20 69 73 20 61 6e 20 6f 70 61 71 75 65 20 73 74   is an opaque st
48100 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 56  ructure named "V
48110 64 62 65 22 2e 20 20 4f 6e 6c 79 20 72 6f 75 74  dbe".  Only rout
48120 69 6e 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73  ines.** in the s
48130 6f 75 72 63 65 20 66 69 6c 65 20 73 71 6c 69 74  ource file sqlit
48140 65 56 64 62 65 2e 63 20 61 72 65 20 61 6c 6c 6f  eVdbe.c are allo
48150 77 65 64 20 74 6f 20 73 65 65 20 74 68 65 20 69  wed to see the i
48160 6e 73 69 64 65 73 0a 2a 2a 20 6f 66 20 74 68 69  nsides.** of thi
48170 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  s structure..*/.
48180 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56  typedef struct V
48190 64 62 65 20 56 64 62 65 3b 0a 0a 2f 2a 0a 2a 2a  dbe Vdbe;../*.**
481a0 20 54 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68   The names of th
481b0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 79 70 65  e following type
481c0 73 20 64 65 63 6c 61 72 65 64 20 69 6e 20 76 64  s declared in vd
481d0 62 65 49 6e 74 2e 68 20 61 72 65 20 72 65 71 75  beInt.h are requ
481e0 69 72 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20  ired.** for the 
481f0 56 64 62 65 4f 70 20 64 65 66 69 6e 69 74 69 6f  VdbeOp definitio
48200 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  n..*/.typedef st
48210 72 75 63 74 20 56 64 62 65 46 75 6e 63 20 56 64  ruct VdbeFunc Vd
48220 62 65 46 75 6e 63 3b 0a 74 79 70 65 64 65 66 20  beFunc;.typedef 
48230 73 74 72 75 63 74 20 4d 65 6d 20 4d 65 6d 3b 0a  struct Mem Mem;.
48240 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 55  typedef struct U
48250 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 55 6e  npackedRecord Un
48260 70 61 63 6b 65 64 52 65 63 6f 72 64 3b 0a 0a 2f  packedRecord;../
48270 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e  *.** A single in
48280 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 65  struction of the
48290 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
482a0 20 68 61 73 20 61 6e 20 6f 70 63 6f 64 65 0a 2a   has an opcode.*
482b0 2a 20 61 6e 64 20 61 73 20 6d 61 6e 79 20 61 73  * and as many as
482c0 20 74 68 72 65 65 20 6f 70 65 72 61 6e 64 73 2e   three operands.
482d0 20 20 54 68 65 20 69 6e 73 74 72 75 63 74 69 6f    The instructio
482e0 6e 20 69 73 20 72 65 63 6f 72 64 65 64 0a 2a 2a  n is recorded.**
482f0 20 61 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   as an instance 
48300 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
48310 20 73 74 72 75 63 74 75 72 65 3a 0a 2a 2f 0a 73   structure:.*/.s
48320 74 72 75 63 74 20 56 64 62 65 4f 70 20 7b 0a 20  truct VdbeOp {. 
48330 20 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20   u8 opcode;     
48340 20 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65       /* What ope
48350 72 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72  ration to perfor
48360 6d 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68  m */.  signed ch
48370 61 72 20 70 34 74 79 70 65 3b 20 2f 2a 20 4f 6e  ar p4type; /* On
48380 65 20 6f 66 20 74 68 65 20 50 34 5f 78 78 78 20  e of the P4_xxx 
48390 63 6f 6e 73 74 61 6e 74 73 20 66 6f 72 20 70 34  constants for p4
483a0 20 2a 2f 0a 20 20 75 38 20 6f 70 66 6c 61 67 73   */.  u8 opflags
483b0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74  ;         /* Not
483c0 20 63 75 72 72 65 6e 74 6c 79 20 75 73 65 64 20   currently used 
483d0 2a 2f 0a 20 20 75 38 20 70 35 3b 20 20 20 20 20  */.  u8 p5;     
483e0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 66 74           /* Fift
483f0 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61  h parameter is a
48400 6e 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 61  n unsigned chara
48410 63 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 31  cter */.  int p1
48420 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
48430 20 46 69 72 73 74 20 6f 70 65 72 61 6e 64 20 2a   First operand *
48440 2f 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20  /.  int p2;     
48450 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e          /* Secon
48460 64 20 70 61 72 61 6d 65 74 65 72 20 28 6f 66 74  d parameter (oft
48470 65 6e 20 74 68 65 20 6a 75 6d 70 20 64 65 73 74  en the jump dest
48480 69 6e 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 69 6e  ination) */.  in
48490 74 20 70 33 3b 20 20 20 20 20 20 20 20 20 20 20  t p3;           
484a0 20 20 2f 2a 20 54 68 65 20 74 68 69 72 64 20 70    /* The third p
484b0 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 75 6e  arameter */.  un
484c0 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 20 20  ion {           
484d0 20 20 2f 2a 20 66 6f 72 74 68 20 70 61 72 61 6d    /* forth param
484e0 65 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  eter */.    int 
484f0 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
48500 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61     /* Integer va
48510 6c 75 65 20 69 66 20 70 34 74 79 70 65 3d 3d 50  lue if p4type==P
48520 34 5f 49 4e 54 33 32 20 2a 2f 0a 20 20 20 20 76  4_INT32 */.    v
48530 6f 69 64 20 2a 70 3b 20 20 20 20 20 20 20 20 20  oid *p;         
48540 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 69 63        /* Generic
48550 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20   pointer */.    
48560 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20  char *z;        
48570 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
48580 72 20 74 6f 20 64 61 74 61 20 66 6f 72 20 73 74  r to data for st
48590 72 69 6e 67 20 28 63 68 61 72 20 61 72 72 61 79  ring (char array
485a0 29 20 74 79 70 65 73 20 2a 2f 0a 20 20 20 20 69  ) types */.    i
485b0 36 34 20 2a 70 49 36 34 3b 20 20 20 20 20 20 20  64 *pI64;       
485c0 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68        /* Used wh
485d0 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f  en p4type is P4_
485e0 49 4e 54 36 34 20 2a 2f 0a 20 20 20 20 64 6f 75  INT64 */.    dou
485f0 62 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20 20 20  ble *pReal;     
48600 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e      /* Used when
48610 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 52 45   p4type is P4_RE
48620 41 4c 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65  AL */.    FuncDe
48630 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20  f *pFunc;       
48640 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34   /* Used when p4
48650 74 79 70 65 20 69 73 20 50 34 5f 46 55 4e 43 44  type is P4_FUNCD
48660 45 46 20 2a 2f 0a 20 20 20 20 56 64 62 65 46 75  EF */.    VdbeFu
48670 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 3b 20 20  nc *pVdbeFunc;  
48680 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34   /* Used when p4
48690 74 79 70 65 20 69 73 20 50 34 5f 56 44 42 45 46  type is P4_VDBEF
486a0 55 4e 43 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53  UNC */.    CollS
486b0 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20  eq *pColl;      
486c0 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70    /* Used when p
486d0 34 74 79 70 65 20 69 73 20 50 34 5f 43 4f 4c 4c  4type is P4_COLL
486e0 53 45 51 20 2a 2f 0a 20 20 20 20 4d 65 6d 20 2a  SEQ */.    Mem *
486f0 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20  pMem;           
48700 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70    /* Used when p
48710 34 74 79 70 65 20 69 73 20 50 34 5f 4d 45 4d 20  4type is P4_MEM 
48720 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  */.    sqlite3_v
48730 74 61 62 20 2a 70 56 74 61 62 3b 20 20 20 2f 2a  tab *pVtab;   /*
48740 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70   Used when p4typ
48750 65 20 69 73 20 50 34 5f 56 54 41 42 20 2a 2f 0a  e is P4_VTAB */.
48760 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65      KeyInfo *pKe
48770 79 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 55 73  yInfo;     /* Us
48780 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69  ed when p4type i
48790 73 20 50 34 5f 4b 45 59 49 4e 46 4f 20 2a 2f 0a  s P4_KEYINFO */.
487a0 20 20 7d 20 70 34 3b 0a 23 69 66 64 65 66 20 53    } p4;.#ifdef S
487b0 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 63 68  QLITE_DEBUG.  ch
487c0 61 72 20 2a 7a 43 6f 6d 6d 65 6e 74 3b 20 20 20  ar *zComment;   
487d0 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 74 6f 20    /* Comment to 
487e0 69 6d 70 72 6f 76 65 20 72 65 61 64 61 62 69 6c  improve readabil
487f0 69 74 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69  ity */.#endif.#i
48800 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c  fdef VDBE_PROFIL
48810 45 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20  E.  int cnt;    
48820 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
48830 72 20 6f 66 20 74 69 6d 65 73 20 74 68 69 73 20  r of times this 
48840 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 61 73 20  instruction was 
48850 65 78 65 63 75 74 65 64 20 2a 2f 0a 20 20 6c 6f  executed */.  lo
48860 6e 67 20 6c 6f 6e 67 20 63 79 63 6c 65 73 3b 20  ng long cycles; 
48870 20 20 2f 2a 20 54 6f 74 61 6c 20 74 69 6d 65 20    /* Total time 
48880 73 70 65 6e 64 20 65 78 65 63 75 74 69 6e 67 20  spend executing 
48890 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  this instruction
488a0 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 74 79   */.#endif.};.ty
488b0 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62  pedef struct Vdb
488c0 65 4f 70 20 56 64 62 65 4f 70 3b 0a 0a 2f 2a 0a  eOp VdbeOp;../*.
488d0 2a 2a 20 41 20 73 6d 61 6c 6c 65 72 20 76 65 72  ** A smaller ver
488e0 73 69 6f 6e 20 6f 66 20 56 64 62 65 4f 70 20 75  sion of VdbeOp u
488f0 73 65 64 20 66 6f 72 20 74 68 65 20 56 64 62 65  sed for the Vdbe
48900 41 64 64 4f 70 4c 69 73 74 28 29 20 66 75 6e 63  AddOpList() func
48910 74 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20  tion because.** 
48920 69 74 20 74 61 6b 65 73 20 75 70 20 6c 65 73 73  it takes up less
48930 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 72 75 63   space..*/.struc
48940 74 20 56 64 62 65 4f 70 4c 69 73 74 20 7b 0a 20  t VdbeOpList {. 
48950 20 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20   u8 opcode;     
48960 20 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65       /* What ope
48970 72 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72  ration to perfor
48980 6d 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68  m */.  signed ch
48990 61 72 20 70 31 3b 20 20 20 20 20 2f 2a 20 46 69  ar p1;     /* Fi
489a0 72 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  rst operand */. 
489b0 20 73 69 67 6e 65 64 20 63 68 61 72 20 70 32 3b   signed char p2;
489c0 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70       /* Second p
489d0 61 72 61 6d 65 74 65 72 20 28 6f 66 74 65 6e 20  arameter (often 
489e0 74 68 65 20 6a 75 6d 70 20 64 65 73 74 69 6e 61  the jump destina
489f0 74 69 6f 6e 29 20 2a 2f 0a 20 20 73 69 67 6e 65  tion) */.  signe
48a00 64 20 63 68 61 72 20 70 33 3b 20 20 20 20 20 2f  d char p3;     /
48a10 2a 20 54 68 69 72 64 20 70 61 72 61 6d 65 74 65  * Third paramete
48a20 72 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20  r */.};.typedef 
48a30 73 74 72 75 63 74 20 56 64 62 65 4f 70 4c 69 73  struct VdbeOpLis
48a40 74 20 56 64 62 65 4f 70 4c 69 73 74 3b 0a 0a 2f  t VdbeOpList;../
48a50 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c  *.** Allowed val
48a60 75 65 73 20 6f 66 20 56 64 62 65 4f 70 2e 70 33  ues of VdbeOp.p3
48a70 74 79 70 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  type.*/.#define 
48a80 50 34 5f 4e 4f 54 55 53 45 44 20 20 20 20 30 20  P4_NOTUSED    0 
48a90 20 20 2f 2a 20 54 68 65 20 50 34 20 70 61 72 61    /* The P4 para
48aa0 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 75 73 65  meter is not use
48ab0 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f  d */.#define P4_
48ac0 44 59 4e 41 4d 49 43 20 20 28 2d 31 29 20 20 2f  DYNAMIC  (-1)  /
48ad0 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 73  * Pointer to a s
48ae0 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66  tring obtained f
48af0 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63  rom sqliteMalloc
48b00 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34  () */.#define P4
48b10 5f 53 54 41 54 49 43 20 20 20 28 2d 32 29 20 20  _STATIC   (-2)  
48b20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20  /* Pointer to a 
48b30 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f  static string */
48b40 0a 23 64 65 66 69 6e 65 20 50 34 5f 43 4f 4c 4c  .#define P4_COLL
48b50 53 45 51 20 20 28 2d 34 29 20 20 2f 2a 20 50 34  SEQ  (-4)  /* P4
48b60 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
48b70 20 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63   a CollSeq struc
48b80 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ture */.#define 
48b90 50 34 5f 46 55 4e 43 44 45 46 20 20 28 2d 35 29  P4_FUNCDEF  (-5)
48ba0 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69    /* P4 is a poi
48bb0 6e 74 65 72 20 74 6f 20 61 20 46 75 6e 63 44 65  nter to a FuncDe
48bc0 66 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23  f structure */.#
48bd0 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46  define P4_KEYINF
48be0 4f 20 20 28 2d 36 29 20 20 2f 2a 20 50 34 20 69  O  (-6)  /* P4 i
48bf0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
48c00 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75   KeyInfo structu
48c10 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34  re */.#define P4
48c20 5f 56 44 42 45 46 55 4e 43 20 28 2d 37 29 20 20  _VDBEFUNC (-7)  
48c30 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74  /* P4 is a point
48c40 65 72 20 74 6f 20 61 20 56 64 62 65 46 75 6e 63  er to a VdbeFunc
48c50 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64   structure */.#d
48c60 65 66 69 6e 65 20 50 34 5f 4d 45 4d 20 20 20 20  efine P4_MEM    
48c70 20 20 28 2d 38 29 20 20 2f 2a 20 50 34 20 69 73    (-8)  /* P4 is
48c80 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
48c90 4d 65 6d 2a 20 20 20 20 73 74 72 75 63 74 75 72  Mem*    structur
48ca0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f  e */.#define P4_
48cb0 54 52 41 4e 53 49 45 4e 54 20 28 2d 39 29 20 2f  TRANSIENT (-9) /
48cc0 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65  * P4 is a pointe
48cd0 72 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74  r to a transient
48ce0 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69   string */.#defi
48cf0 6e 65 20 50 34 5f 56 54 41 42 20 20 20 20 20 28  ne P4_VTAB     (
48d00 2d 31 30 29 20 2f 2a 20 50 34 20 69 73 20 61 20  -10) /* P4 is a 
48d10 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71  pointer to an sq
48d20 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63  lite3_vtab struc
48d30 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ture */.#define 
48d40 50 34 5f 4d 50 52 49 4e 54 46 20 20 28 2d 31 31  P4_MPRINTF  (-11
48d50 29 20 2f 2a 20 50 34 20 69 73 20 61 20 73 74 72  ) /* P4 is a str
48d60 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ing obtained fro
48d70 6d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  m sqlite3_mprint
48d80 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  f() */.#define P
48d90 34 5f 52 45 41 4c 20 20 20 20 20 28 2d 31 32 29  4_REAL     (-12)
48da0 20 2f 2a 20 50 34 20 69 73 20 61 20 36 34 2d 62   /* P4 is a 64-b
48db0 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  it floating poin
48dc0 74 20 76 61 6c 75 65 20 2a 2f 0a 23 64 65 66 69  t value */.#defi
48dd0 6e 65 20 50 34 5f 49 4e 54 36 34 20 20 20 20 28  ne P4_INT64    (
48de0 2d 31 33 29 20 2f 2a 20 50 34 20 69 73 20 61 20  -13) /* P4 is a 
48df0 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e  64-bit signed in
48e00 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65  teger */.#define
48e10 20 50 34 5f 49 4e 54 33 32 20 20 20 20 28 2d 31   P4_INT32    (-1
48e20 34 29 20 2f 2a 20 50 34 20 69 73 20 61 20 33 32  4) /* P4 is a 32
48e30 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65  -bit signed inte
48e40 67 65 72 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 20  ger */../* When 
48e50 61 64 64 69 6e 67 20 61 20 50 34 20 61 72 67 75  adding a P4 argu
48e60 6d 65 6e 74 20 75 73 69 6e 67 20 50 34 5f 4b 45  ment using P4_KE
48e70 59 49 4e 46 4f 2c 20 61 20 63 6f 70 79 20 6f 66  YINFO, a copy of
48e80 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72   the KeyInfo str
48e90 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 6d 61 64  ucture.** is mad
48ea0 65 2e 20 20 54 68 61 74 20 63 6f 70 79 20 69 73  e.  That copy is
48eb0 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20   freed when the 
48ec0 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a 65  Vdbe is finalize
48ed0 64 2e 20 20 42 75 74 20 69 66 20 74 68 65 0a 2a  d.  But if the.*
48ee0 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 50 34  * argument is P4
48ef0 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
48f00 2c 20 74 68 65 20 70 61 73 73 65 64 20 69 6e 20  , the passed in 
48f10 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 2e  pointer is used.
48f20 20 20 49 74 20 73 74 69 6c 6c 0a 2a 2a 20 67 65    It still.** ge
48f30 74 73 20 66 72 65 65 64 20 77 68 65 6e 20 74 68  ts freed when th
48f40 65 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69  e Vdbe is finali
48f50 7a 65 64 20 73 6f 20 69 74 20 73 74 69 6c 6c 20  zed so it still 
48f60 73 68 6f 75 6c 64 20 62 65 20 6f 62 74 61 69 6e  should be obtain
48f70 65 64 0a 2a 2a 20 66 72 6f 6d 20 61 20 73 69 6e  ed.** from a sin
48f80 67 6c 65 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63  gle sqliteMalloc
48f90 28 29 2e 20 20 42 75 74 20 6e 6f 20 63 6f 70 79  ().  But no copy
48fa0 20 69 73 20 6d 61 64 65 20 61 6e 64 20 74 68 65   is made and the
48fb0 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63   calling.** func
48fc0 74 69 6f 6e 20 73 68 6f 75 6c 64 20 2a 6e 6f 74  tion should *not
48fd0 2a 20 74 72 79 20 74 6f 20 66 72 65 65 20 74 68  * try to free th
48fe0 65 20 4b 65 79 49 6e 66 6f 2e 0a 2a 2f 0a 23 64  e KeyInfo..*/.#d
48ff0 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f  efine P4_KEYINFO
49000 5f 48 41 4e 44 4f 46 46 20 28 2d 39 29 0a 0a 2f  _HANDOFF (-9)../
49010 2a 0a 2a 2a 20 54 68 65 20 56 64 62 65 2e 61 43  *.** The Vdbe.aC
49020 6f 6c 4e 61 6d 65 20 61 72 72 61 79 20 63 6f 6e  olName array con
49030 74 61 69 6e 73 20 35 6e 20 4d 65 6d 20 73 74 72  tains 5n Mem str
49040 75 63 74 75 72 65 73 2c 20 77 68 65 72 65 20 6e  uctures, where n
49050 20 69 73 20 74 68 65 20 0a 2a 2a 20 6e 75 6d 62   is the .** numb
49060 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66  er of columns of
49070 20 64 61 74 61 20 72 65 74 75 72 6e 65 64 20 62   data returned b
49080 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2e  y the statement.
49090 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e  .*/.#define COLN
490a0 41 4d 45 5f 4e 41 4d 45 20 20 20 20 20 30 0a 23  AME_NAME     0.#
490b0 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44  define COLNAME_D
490c0 45 43 4c 54 59 50 45 20 31 0a 23 64 65 66 69 6e  ECLTYPE 1.#defin
490d0 65 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41  e COLNAME_DATABA
490e0 53 45 20 32 0a 23 64 65 66 69 6e 65 20 43 4f 4c  SE 2.#define COL
490f0 4e 41 4d 45 5f 54 41 42 4c 45 20 20 20 20 33 0a  NAME_TABLE    3.
49100 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f  #define COLNAME_
49110 43 4f 4c 55 4d 4e 20 20 20 34 0a 23 69 66 64 65  COLUMN   4.#ifde
49120 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
49130 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a  COLUMN_METADATA.
49140 23 20 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45  # define COLNAME
49150 5f 4e 20 20 20 20 20 20 20 20 35 20 20 20 20 20  _N        5     
49160 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 43 4f   /* Number of CO
49170 4c 4e 41 4d 45 5f 78 78 78 20 73 79 6d 62 6f 6c  LNAME_xxx symbol
49180 73 20 2a 2f 0a 23 65 6c 73 65 0a 23 20 69 66 64  s */.#else.# ifd
49190 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
491a0 45 43 4c 54 59 50 45 0a 23 20 20 20 64 65 66 69  ECLTYPE.#   defi
491b0 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20  ne COLNAME_N    
491c0 20 20 31 20 20 20 20 20 20 2f 2a 20 53 74 6f 72    1      /* Stor
491d0 65 20 6f 6e 6c 79 20 74 68 65 20 6e 61 6d 65 20  e only the name 
491e0 2a 2f 0a 23 20 65 6c 73 65 0a 23 20 20 20 64 65  */.# else.#   de
491f0 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20  fine COLNAME_N  
49200 20 20 20 20 32 20 20 20 20 20 20 2f 2a 20 53 74      2      /* St
49210 6f 72 65 20 74 68 65 20 6e 61 6d 65 20 61 6e 64  ore the name and
49220 20 64 65 63 6c 74 79 70 65 20 2a 2f 0a 23 20 65   decltype */.# e
49230 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  ndif.#endif../*.
49240 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
49250 20 6d 61 63 72 6f 20 63 6f 6e 76 65 72 74 73 20   macro converts 
49260 61 20 72 65 6c 61 74 69 76 65 20 61 64 64 72 65  a relative addre
49270 73 73 20 69 6e 20 74 68 65 20 70 32 20 66 69 65  ss in the p2 fie
49280 6c 64 0a 2a 2a 20 6f 66 20 61 20 56 64 62 65 4f  ld.** of a VdbeO
49290 70 20 73 74 72 75 63 74 75 72 65 20 69 6e 74 6f  p structure into
492a0 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62   a negative numb
492b0 65 72 20 73 6f 20 74 68 61 74 20 0a 2a 2a 20 73  er so that .** s
492c0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c  qlite3VdbeAddOpL
492d0 69 73 74 28 29 20 6b 6e 6f 77 73 20 74 68 61 74  ist() knows that
492e0 20 74 68 65 20 61 64 64 72 65 73 73 20 69 73 20   the address is 
492f0 72 65 6c 61 74 69 76 65 2e 20 20 43 61 6c 6c 69  relative.  Calli
49300 6e 67 0a 2a 2a 20 74 68 65 20 6d 61 63 72 6f 20  ng.** the macro 
49310 61 67 61 69 6e 20 72 65 73 74 6f 72 65 73 20 74  again restores t
49320 68 65 20 61 64 64 72 65 73 73 2e 0a 2a 2f 0a 23  he address..*/.#
49330 64 65 66 69 6e 65 20 41 44 44 52 28 58 29 20 20  define ADDR(X)  
49340 28 2d 31 2d 28 58 29 29 0a 0a 2f 2a 0a 2a 2a 20  (-1-(X))../*.** 
49350 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 73 63 61  The makefile sca
49360 6e 73 20 74 68 65 20 76 64 62 65 2e 63 20 73 6f  ns the vdbe.c so
49370 75 72 63 65 20 66 69 6c 65 20 61 6e 64 20 63 72  urce file and cr
49380 65 61 74 65 73 20 74 68 65 20 22 6f 70 63 6f 64  eates the "opcod
49390 65 73 2e 68 22 0a 2a 2a 20 68 65 61 64 65 72 20  es.h".** header 
493a0 66 69 6c 65 20 74 68 61 74 20 64 65 66 69 6e 65  file that define
493b0 73 20 61 20 6e 75 6d 62 65 72 20 66 6f 72 20 65  s a number for e
493c0 61 63 68 20 6f 70 63 6f 64 65 20 75 73 65 64 20  ach opcode used 
493d0 62 79 20 74 68 65 20 56 44 42 45 2e 0a 2a 2f 0a  by the VDBE..*/.
493e0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
493f0 49 6e 63 6c 75 64 65 20 6f 70 63 6f 64 65 73 2e  Include opcodes.
49400 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
49410 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a  of vdbe.h ******
49420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
49430 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
49440 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63 6f 64  Begin file opcod
49450 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  es.h ***********
49460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
49480 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79  /* Automatically
49490 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44 6f 20   generated.  Do 
494a0 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 53  not edit */./* S
494b0 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68  ee the mkopcodeh
494c0 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72 20  .awk script for 
494d0 64 65 74 61 69 6c 73 20 2a 2f 0a 23 64 65 66 69  details */.#defi
494e0 6e 65 20 4f 50 5f 56 4e 65 78 74 20 20 20 20 20  ne OP_VNext     
494f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49500 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65             1.#de
49510 66 69 6e 65 20 4f 50 5f 41 66 66 69 6e 69 74 79  fine OP_Affinity
49520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49530 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 23               2.#
49540 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6c 75 6d 6e  define OP_Column
49550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
49570 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 74 43  .#define OP_SetC
49580 6f 6f 6b 69 65 20 20 20 20 20 20 20 20 20 20 20  ookie           
49590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
495a0 20 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65   4.#define OP_Re
495b0 61 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  al              
495c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
495d0 20 31 32 35 20 20 20 2f 2a 20 73 61 6d 65 20 61   125   /* same a
495e0 73 20 54 4b 5f 46 4c 4f 41 54 20 20 20 20 2a 2f  s TK_FLOAT    */
495f0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 71 75  .#define OP_Sequ
49600 65 6e 63 65 20 20 20 20 20 20 20 20 20 20 20 20  ence            
49610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49620 20 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f   5.#define OP_Mo
49630 76 65 47 74 20 20 20 20 20 20 20 20 20 20 20 20  veGt            
49640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49650 20 20 20 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f     6.#define OP_
49660 47 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Ge              
49670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49680 20 20 20 20 37 32 20 20 20 2f 2a 20 73 61 6d 65      72   /* same
49690 20 61 73 20 54 4b 5f 47 45 20 20 20 20 20 20 20   as TK_GE       
496a0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f  */.#define OP_Ro
496b0 77 4b 65 79 20 20 20 20 20 20 20 20 20 20 20 20  wKey            
496c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
496d0 20 20 20 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f     7.#define OP_
496e0 53 43 6f 70 79 20 20 20 20 20 20 20 20 20 20 20  SCopy           
496f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49700 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20 4f       8.#define O
49710 50 5f 45 71 20 20 20 20 20 20 20 20 20 20 20 20  P_Eq            
49720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49730 20 20 20 20 20 20 36 38 20 20 20 2f 2a 20 73 61        68   /* sa
49740 6d 65 20 61 73 20 54 4b 5f 45 51 20 20 20 20 20  me as TK_EQ     
49750 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
49760 4f 70 65 6e 57 72 69 74 65 20 20 20 20 20 20 20  OpenWrite       
49770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49780 20 20 20 20 20 39 0a 23 64 65 66 69 6e 65 20 4f       9.#define O
49790 50 5f 4e 6f 74 4e 75 6c 6c 20 20 20 20 20 20 20  P_NotNull       
497a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
497b0 20 20 20 20 20 20 36 36 20 20 20 2f 2a 20 73 61        66   /* sa
497c0 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c  me as TK_NOTNULL
497d0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
497e0 49 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20  If              
497f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49800 20 20 20 20 31 30 0a 23 64 65 66 69 6e 65 20 4f      10.#define O
49810 50 5f 54 6f 49 6e 74 20 20 20 20 20 20 20 20 20  P_ToInt         
49820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49830 20 20 20 20 20 31 34 31 20 20 20 2f 2a 20 73 61       141   /* sa
49840 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 20  me as TK_TO_INT 
49850 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
49860 53 74 72 69 6e 67 38 20 20 20 20 20 20 20 20 20  String8         
49870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49880 20 20 20 20 38 38 20 20 20 2f 2a 20 73 61 6d 65      88   /* same
49890 20 61 73 20 54 4b 5f 53 54 52 49 4e 47 20 20 20   as TK_STRING   
498a0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 52  */.#define OP_VR
498b0 6f 77 69 64 20 20 20 20 20 20 20 20 20 20 20 20  owid            
498c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
498d0 20 20 31 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f    11.#define OP_
498e0 43 6f 6c 6c 53 65 71 20 20 20 20 20 20 20 20 20  CollSeq         
498f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49900 20 20 20 20 31 32 0a 23 64 65 66 69 6e 65 20 4f      12.#define O
49910 50 5f 4f 70 65 6e 52 65 61 64 20 20 20 20 20 20  P_OpenRead      
49920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49930 20 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e 65        13.#define
49940 20 4f 50 5f 45 78 70 69 72 65 20 20 20 20 20 20   OP_Expire      
49950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49960 20 20 20 20 20 20 20 20 31 34 0a 23 64 65 66 69          14.#defi
49970 6e 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74  ne OP_AutoCommit
49980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49990 20 20 20 20 20 20 20 20 20 20 31 35 0a 23 64 65            15.#de
499a0 66 69 6e 65 20 4f 50 5f 47 74 20 20 20 20 20 20  fine OP_Gt      
499b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
499c0 20 20 20 20 20 20 20 20 20 20 20 20 36 39 20 20              69  
499d0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 47   /* same as TK_G
499e0 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  T       */.#defi
499f0 6e 65 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43  ne OP_IntegrityC
49a00 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k               
49a10 20 20 20 20 20 20 20 20 20 20 31 37 0a 23 64 65            17.#de
49a20 66 69 6e 65 20 4f 50 5f 53 6f 72 74 20 20 20 20  fine OP_Sort    
49a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49a40 20 20 20 20 20 20 20 20 20 20 20 20 31 38 0a 23              18.#
49a50 64 65 66 69 6e 65 20 4f 50 5f 43 6f 70 79 20 20  define OP_Copy  
49a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 39                19
49a80 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 72 61 63  .#define OP_Trac
49a90 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
49aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49ab0 32 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 75  20.#define OP_Fu
49ac0 6e 63 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20  nction          
49ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49ae0 20 20 32 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f    21.#define OP_
49af0 49 66 4e 65 67 20 20 20 20 20 20 20 20 20 20 20  IfNeg           
49b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49b10 20 20 20 20 32 32 0a 23 64 65 66 69 6e 65 20 4f      22.#define O
49b20 50 5f 41 6e 64 20 20 20 20 20 20 20 20 20 20 20  P_And           
49b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49b40 20 20 20 20 20 20 36 31 20 20 20 2f 2a 20 73 61        61   /* sa
49b50 6d 65 20 61 73 20 54 4b 5f 41 4e 44 20 20 20 20  me as TK_AND    
49b60 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
49b70 53 75 62 74 72 61 63 74 20 20 20 20 20 20 20 20  Subtract        
49b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49b90 20 20 20 20 37 39 20 20 20 2f 2a 20 73 61 6d 65      79   /* same
49ba0 20 61 73 20 54 4b 5f 4d 49 4e 55 53 20 20 20 20   as TK_MINUS    
49bb0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f  */.#define OP_No
49bc0 6f 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20  op              
49bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49be0 20 20 32 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f    23.#define OP_
49bf0 52 65 74 75 72 6e 20 20 20 20 20 20 20 20 20 20  Return          
49c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49c10 20 20 20 20 32 34 0a 23 64 65 66 69 6e 65 20 4f      24.#define O
49c20 50 5f 52 65 6d 61 69 6e 64 65 72 20 20 20 20 20  P_Remainder     
49c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49c40 20 20 20 20 20 20 38 32 20 20 20 2f 2a 20 73 61        82   /* sa
49c50 6d 65 20 61 73 20 54 4b 5f 52 45 4d 20 20 20 20  me as TK_REM    
49c60 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
49c70 4e 65 77 52 6f 77 69 64 20 20 20 20 20 20 20 20  NewRowid        
49c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49c90 20 20 20 20 32 35 0a 23 64 65 66 69 6e 65 20 4f      25.#define O
49ca0 50 5f 4d 75 6c 74 69 70 6c 79 20 20 20 20 20 20  P_Multiply      
49cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49cc0 20 20 20 20 20 20 38 30 20 20 20 2f 2a 20 73 61        80   /* sa
49cd0 6d 65 20 61 73 20 54 4b 5f 53 54 41 52 20 20 20  me as TK_STAR   
49ce0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
49cf0 56 61 72 69 61 62 6c 65 20 20 20 20 20 20 20 20  Variable        
49d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49d10 20 20 20 20 32 36 0a 23 64 65 66 69 6e 65 20 4f      26.#define O
49d20 50 5f 53 74 72 69 6e 67 20 20 20 20 20 20 20 20  P_String        
49d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49d40 20 20 20 20 20 20 32 37 0a 23 64 65 66 69 6e 65        27.#define
49d50 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79   OP_RealAffinity
49d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49d70 20 20 20 20 20 20 20 20 32 38 0a 23 64 65 66 69          28.#defi
49d80 6e 65 20 4f 50 5f 56 52 65 6e 61 6d 65 20 20 20  ne OP_VRename   
49d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49da0 20 20 20 20 20 20 20 20 20 20 32 39 0a 23 64 65            29.#de
49db0 66 69 6e 65 20 4f 50 5f 50 61 72 73 65 53 63 68  fine OP_ParseSch
49dc0 65 6d 61 20 20 20 20 20 20 20 20 20 20 20 20 20  ema             
49dd0 20 20 20 20 20 20 20 20 20 20 20 20 33 30 0a 23              30.#
49de0 64 65 66 69 6e 65 20 4f 50 5f 56 4f 70 65 6e 20  define OP_VOpen 
49df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 31                31
49e10 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6c 6f 73  .#define OP_Clos
49e20 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
49e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49e40 33 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 72  32.#define OP_Cr
49e50 65 61 74 65 49 6e 64 65 78 20 20 20 20 20 20 20  eateIndex       
49e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49e70 20 20 33 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f    33.#define OP_
49e80 49 73 55 6e 69 71 75 65 20 20 20 20 20 20 20 20  IsUnique        
49e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49ea0 20 20 20 20 33 34 0a 23 64 65 66 69 6e 65 20 4f      34.#define O
49eb0 50 5f 4e 6f 74 46 6f 75 6e 64 20 20 20 20 20 20  P_NotFound      
49ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49ed0 20 20 20 20 20 20 33 35 0a 23 64 65 66 69 6e 65        35.#define
49ee0 20 4f 50 5f 49 6e 74 36 34 20 20 20 20 20 20 20   OP_Int64       
49ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49f00 20 20 20 20 20 20 20 20 33 36 0a 23 64 65 66 69          36.#defi
49f10 6e 65 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 20  ne OP_MustBeInt 
49f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49f30 20 20 20 20 20 20 20 20 20 20 33 37 0a 23 64 65            37.#de
49f40 66 69 6e 65 20 4f 50 5f 48 61 6c 74 20 20 20 20  fine OP_Halt    
49f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49f60 20 20 20 20 20 20 20 20 20 20 20 20 33 38 0a 23              38.#
49f70 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 69 64 20  define OP_Rowid 
49f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 39                39
49fa0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 4c  .#define OP_IdxL
49fb0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
49fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49fd0 34 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 64  40.#define OP_Ad
49fe0 64 49 6d 6d 20 20 20 20 20 20 20 20 20 20 20 20  dImm            
49ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a000 20 20 34 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f    41.#define OP_
4a010 53 74 61 74 65 6d 65 6e 74 20 20 20 20 20 20 20  Statement       
4a020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a030 20 20 20 20 34 32 0a 23 64 65 66 69 6e 65 20 4f      42.#define O
4a040 50 5f 52 6f 77 44 61 74 61 20 20 20 20 20 20 20  P_RowData       
4a050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a060 20 20 20 20 20 20 34 33 0a 23 64 65 66 69 6e 65        43.#define
4a070 20 4f 50 5f 4d 65 6d 4d 61 78 20 20 20 20 20 20   OP_MemMax      
4a080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a090 20 20 20 20 20 20 20 20 34 34 0a 23 64 65 66 69          44.#defi
4a0a0 6e 65 20 4f 50 5f 4f 72 20 20 20 20 20 20 20 20  ne OP_Or        
4a0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a0c0 20 20 20 20 20 20 20 20 20 20 36 30 20 20 20 2f            60   /
4a0d0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4f 52 20  * same as TK_OR 
4a0e0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
4a0f0 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 20 20   OP_NotExists   
4a100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a110 20 20 20 20 20 20 20 20 34 35 0a 23 64 65 66 69          45.#defi
4a120 6e 65 20 4f 50 5f 47 6f 73 75 62 20 20 20 20 20  ne OP_Gosub     
4a130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a140 20 20 20 20 20 20 20 20 20 20 34 36 0a 23 64 65            46.#de
4a150 66 69 6e 65 20 4f 50 5f 44 69 76 69 64 65 20 20  fine OP_Divide  
4a160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a170 20 20 20 20 20 20 20 20 20 20 20 20 38 31 20 20              81  
4a180 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53   /* same as TK_S
4a190 4c 41 53 48 20 20 20 20 2a 2f 0a 23 64 65 66 69  LASH    */.#defi
4a1a0 6e 65 20 4f 50 5f 49 6e 74 65 67 65 72 20 20 20  ne OP_Integer   
4a1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a1c0 20 20 20 20 20 20 20 20 20 20 34 37 0a 23 64 65            47.#de
4a1d0 66 69 6e 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 69  fine OP_ToNumeri
4a1e0 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c               
4a1f0 20 20 20 20 20 20 20 20 20 20 20 31 34 30 20 20             140  
4a200 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54   /* same as TK_T
4a210 4f 5f 4e 55 4d 45 52 49 43 2a 2f 0a 23 64 65 66  O_NUMERIC*/.#def
4a220 69 6e 65 20 4f 50 5f 50 72 65 76 20 20 20 20 20  ine OP_Prev     
4a230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a240 20 20 20 20 20 20 20 20 20 20 20 34 38 0a 23 64             48.#d
4a250 65 66 69 6e 65 20 4f 50 5f 43 6f 6e 63 61 74 20  efine OP_Concat 
4a260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a270 20 20 20 20 20 20 20 20 20 20 20 20 20 38 33 20               83 
4a280 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
4a290 43 4f 4e 43 41 54 20 20 20 2a 2f 0a 23 64 65 66  CONCAT   */.#def
4a2a0 69 6e 65 20 4f 50 5f 42 69 74 41 6e 64 20 20 20  ine OP_BitAnd   
4a2b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a2c0 20 20 20 20 20 20 20 20 20 20 20 37 34 20 20 20             74   
4a2d0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49  /* same as TK_BI
4a2e0 54 41 4e 44 20 20 20 2a 2f 0a 23 64 65 66 69 6e  TAND   */.#defin
4a2f0 65 20 4f 50 5f 56 43 6f 6c 75 6d 6e 20 20 20 20  e OP_VColumn    
4a300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a310 20 20 20 20 20 20 20 20 20 34 39 0a 23 64 65 66           49.#def
4a320 69 6e 65 20 4f 50 5f 43 72 65 61 74 65 54 61 62  ine OP_CreateTab
4a330 6c 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  le              
4a340 20 20 20 20 20 20 20 20 20 20 20 35 30 0a 23 64             50.#d
4a350 65 66 69 6e 65 20 4f 50 5f 4c 61 73 74 20 20 20  efine OP_Last   
4a360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a370 20 20 20 20 20 20 20 20 20 20 20 20 20 35 31 0a               51.
4a380 23 64 65 66 69 6e 65 20 4f 50 5f 49 73 4e 75 6c  #define OP_IsNul
4a390 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l               
4a3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
4a3b0 35 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  5   /* same as T
4a3c0 4b 5f 49 53 4e 55 4c 4c 20 20 20 2a 2f 0a 23 64  K_ISNULL   */.#d
4a3d0 65 66 69 6e 65 20 4f 50 5f 49 6e 63 72 56 61 63  efine OP_IncrVac
4a3e0 75 75 6d 20 20 20 20 20 20 20 20 20 20 20 20 20  uum             
4a3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 35 32 0a               52.
4a400 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 52 6f  #define OP_IdxRo
4a410 77 69 64 20 20 20 20 20 20 20 20 20 20 20 20 20  wid             
4a420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
4a430 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 68 69  3.#define OP_Shi
4a440 66 74 52 69 67 68 74 20 20 20 20 20 20 20 20 20  ftRight         
4a450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a460 20 37 37 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   77   /* same as
4a470 20 54 4b 5f 52 53 48 49 46 54 20 20 20 2a 2f 0a   TK_RSHIFT   */.
4a480 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 73 65 74  #define OP_Reset
4a490 43 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20 20  Count           
4a4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
4a4b0 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 69 66  4.#define OP_Fif
4a4c0 6f 57 72 69 74 65 20 20 20 20 20 20 20 20 20 20  oWrite          
4a4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a4e0 20 35 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43   55.#define OP_C
4a4f0 6f 6e 74 65 78 74 50 75 73 68 20 20 20 20 20 20  ontextPush      
4a500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a510 20 20 20 35 36 0a 23 64 65 66 69 6e 65 20 4f 50     56.#define OP
4a520 5f 44 72 6f 70 54 72 69 67 67 65 72 20 20 20 20  _DropTrigger    
4a530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a540 20 20 20 20 20 35 37 0a 23 64 65 66 69 6e 65 20       57.#define 
4a550 4f 50 5f 44 72 6f 70 49 6e 64 65 78 20 20 20 20  OP_DropIndex    
4a560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a570 20 20 20 20 20 20 20 35 38 0a 23 64 65 66 69 6e         58.#defin
4a580 65 20 4f 50 5f 49 64 78 47 45 20 20 20 20 20 20  e OP_IdxGE      
4a590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a5a0 20 20 20 20 20 20 20 20 20 35 39 0a 23 64 65 66           59.#def
4a5b0 69 6e 65 20 4f 50 5f 49 64 78 44 65 6c 65 74 65  ine OP_IdxDelete
4a5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a5d0 20 20 20 20 20 20 20 20 20 20 20 36 32 0a 23 64             62.#d
4a5e0 65 66 69 6e 65 20 4f 50 5f 56 61 63 75 75 6d 20  efine OP_Vacuum 
4a5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a600 20 20 20 20 20 20 20 20 20 20 20 20 20 36 33 0a               63.
4a610 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f 76 65 4c  #define OP_MoveL
4a620 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
4a630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
4a640 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 4e  4.#define OP_IfN
4a650 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ot              
4a660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a670 20 37 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44   73.#define OP_D
4a680 72 6f 70 54 61 62 6c 65 20 20 20 20 20 20 20 20  ropTable        
4a690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a6a0 20 20 20 38 34 0a 23 64 65 66 69 6e 65 20 4f 50     84.#define OP
4a6b0 5f 4d 61 6b 65 52 65 63 6f 72 64 20 20 20 20 20  _MakeRecord     
4a6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a6d0 20 20 20 20 20 38 35 0a 23 64 65 66 69 6e 65 20       85.#define 
4a6e0 4f 50 5f 54 6f 42 6c 6f 62 20 20 20 20 20 20 20  OP_ToBlob       
4a6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a700 20 20 20 20 20 20 31 33 39 20 20 20 2f 2a 20 73        139   /* s
4a710 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 42 4c 4f  ame as TK_TO_BLO
4a720 42 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  B  */.#define OP
4a730 5f 52 65 73 75 6c 74 52 6f 77 20 20 20 20 20 20  _ResultRow      
4a740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a750 20 20 20 20 20 38 36 0a 23 64 65 66 69 6e 65 20       86.#define 
4a760 4f 50 5f 44 65 6c 65 74 65 20 20 20 20 20 20 20  OP_Delete       
4a770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a780 20 20 20 20 20 20 20 38 39 0a 23 64 65 66 69 6e         89.#defin
4a790 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c 20 20 20  e OP_AggFinal   
4a7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a7b0 20 20 20 20 20 20 20 20 20 39 30 0a 23 64 65 66           90.#def
4a7c0 69 6e 65 20 4f 50 5f 53 68 69 66 74 4c 65 66 74  ine OP_ShiftLeft
4a7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a7e0 20 20 20 20 20 20 20 20 20 20 20 37 36 20 20 20             76   
4a7f0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 53  /* same as TK_LS
4a800 48 49 46 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e  HIFT   */.#defin
4a810 65 20 4f 50 5f 47 6f 74 6f 20 20 20 20 20 20 20  e OP_Goto       
4a820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a830 20 20 20 20 20 20 20 20 20 39 31 0a 23 64 65 66           91.#def
4a840 69 6e 65 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b  ine OP_TableLock
4a850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a860 20 20 20 20 20 20 20 20 20 20 20 39 32 0a 23 64             92.#d
4a870 65 66 69 6e 65 20 4f 50 5f 46 69 66 6f 52 65 61  efine OP_FifoRea
4a880 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
4a890 20 20 20 20 20 20 20 20 20 20 20 20 20 39 33 0a               93.
4a8a0 23 64 65 66 69 6e 65 20 4f 50 5f 43 6c 65 61 72  #define OP_Clear
4a8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
4a8d0 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f 76  4.#define OP_Mov
4a8e0 65 4c 74 20 20 20 20 20 20 20 20 20 20 20 20 20  eLt             
4a8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a900 20 39 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c   95.#define OP_L
4a910 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
4a920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a930 20 20 20 37 30 20 20 20 2f 2a 20 73 61 6d 65 20     70   /* same 
4a940 61 73 20 54 4b 5f 4c 45 20 20 20 20 20 20 20 2a  as TK_LE       *
4a950 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 65 72  /.#define OP_Ver
4a960 69 66 79 43 6f 6f 6b 69 65 20 20 20 20 20 20 20  ifyCookie       
4a970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a980 20 39 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41   96.#define OP_A
4a990 67 67 53 74 65 70 20 20 20 20 20 20 20 20 20 20  ggStep          
4a9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a9b0 20 20 20 39 37 0a 23 64 65 66 69 6e 65 20 4f 50     97.#define OP
4a9c0 5f 54 6f 54 65 78 74 20 20 20 20 20 20 20 20 20  _ToText         
4a9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a9e0 20 20 20 20 31 33 38 20 20 20 2f 2a 20 73 61 6d      138   /* sam
4a9f0 65 20 61 73 20 54 4b 5f 54 4f 5f 54 45 58 54 20  e as TK_TO_TEXT 
4aa00 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e   */.#define OP_N
4aa10 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ot              
4aa20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4aa30 20 20 20 31 36 20 20 20 2f 2a 20 73 61 6d 65 20     16   /* same 
4aa40 61 73 20 54 4b 5f 4e 4f 54 20 20 20 20 20 20 2a  as TK_NOT      *
4aa50 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 52  /.#define OP_ToR
4aa60 65 61 6c 20 20 20 20 20 20 20 20 20 20 20 20 20  eal             
4aa70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4aa80 31 34 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73  142   /* same as
4aa90 20 54 4b 5f 54 4f 5f 52 45 41 4c 20 20 2a 2f 0a   TK_TO_REAL  */.
4aaa0 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 74 4e 75  #define OP_SetNu
4aab0 6d 43 6f 6c 75 6d 6e 73 20 20 20 20 20 20 20 20  mColumns        
4aac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
4aad0 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 72 61  8.#define OP_Tra
4aae0 6e 73 61 63 74 69 6f 6e 20 20 20 20 20 20 20 20  nsaction        
4aaf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ab00 20 39 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56   99.#define OP_V
4ab10 46 69 6c 74 65 72 20 20 20 20 20 20 20 20 20 20  Filter          
4ab20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ab30 20 20 31 30 30 0a 23 64 65 66 69 6e 65 20 4f 50    100.#define OP
4ab40 5f 4e 65 20 20 20 20 20 20 20 20 20 20 20 20 20  _Ne             
4ab50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ab60 20 20 20 20 20 36 37 20 20 20 2f 2a 20 73 61 6d       67   /* sam
4ab70 65 20 61 73 20 54 4b 5f 4e 45 20 20 20 20 20 20  e as TK_NE      
4ab80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56   */.#define OP_V
4ab90 44 65 73 74 72 6f 79 20 20 20 20 20 20 20 20 20  Destroy         
4aba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4abb0 20 20 31 30 31 0a 23 64 65 66 69 6e 65 20 4f 50    101.#define OP
4abc0 5f 43 6f 6e 74 65 78 74 50 6f 70 20 20 20 20 20  _ContextPop     
4abd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4abe0 20 20 20 20 31 30 32 0a 23 64 65 66 69 6e 65 20      102.#define 
4abf0 4f 50 5f 42 69 74 4f 72 20 20 20 20 20 20 20 20  OP_BitOr        
4ac00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ac10 20 20 20 20 20 20 20 37 35 20 20 20 2f 2a 20 73         75   /* s
4ac20 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4f 52 20  ame as TK_BITOR 
4ac30 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
4ac40 5f 4e 65 78 74 20 20 20 20 20 20 20 20 20 20 20  _Next           
4ac50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ac60 20 20 20 20 31 30 33 0a 23 64 65 66 69 6e 65 20      103.#define 
4ac70 4f 50 5f 49 64 78 49 6e 73 65 72 74 20 20 20 20  OP_IdxInsert    
4ac80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ac90 20 20 20 20 20 20 31 30 34 0a 23 64 65 66 69 6e        104.#defin
4aca0 65 20 4f 50 5f 4c 74 20 20 20 20 20 20 20 20 20  e OP_Lt         
4acb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4acc0 20 20 20 20 20 20 20 20 20 37 31 20 20 20 2f 2a           71   /*
4acd0 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 54 20 20   same as TK_LT  
4ace0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
4acf0 4f 50 5f 49 6e 73 65 72 74 20 20 20 20 20 20 20  OP_Insert       
4ad00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ad10 20 20 20 20 20 20 31 30 35 0a 23 64 65 66 69 6e        105.#defin
4ad20 65 20 4f 50 5f 44 65 73 74 72 6f 79 20 20 20 20  e OP_Destroy    
4ad30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ad40 20 20 20 20 20 20 20 20 31 30 36 0a 23 64 65 66          106.#def
4ad50 69 6e 65 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69  ine OP_ReadCooki
4ad60 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
4ad70 20 20 20 20 20 20 20 20 20 20 31 30 37 0a 23 64            107.#d
4ad80 65 66 69 6e 65 20 4f 50 5f 46 6f 72 63 65 49 6e  efine OP_ForceIn
4ad90 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
4ada0 20 20 20 20 20 20 20 20 20 20 20 20 31 30 38 0a              108.
4adb0 23 64 65 66 69 6e 65 20 4f 50 5f 4c 6f 61 64 41  #define OP_LoadA
4adc0 6e 61 6c 79 73 69 73 20 20 20 20 20 20 20 20 20  nalysis         
4add0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30                10
4ade0 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 45 78 70  9.#define OP_Exp
4adf0 6c 61 69 6e 20 20 20 20 20 20 20 20 20 20 20 20  lain            
4ae00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ae10 31 31 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f  110.#define OP_O
4ae20 70 65 6e 50 73 65 75 64 6f 20 20 20 20 20 20 20  penPseudo       
4ae30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ae40 20 20 31 31 31 0a 23 64 65 66 69 6e 65 20 4f 50    111.#define OP
4ae50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 20  _OpenEphemeral  
4ae60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ae70 20 20 20 20 31 31 32 0a 23 64 65 66 69 6e 65 20      112.#define 
4ae80 4f 50 5f 4e 75 6c 6c 20 20 20 20 20 20 20 20 20  OP_Null         
4ae90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4aea0 20 20 20 20 20 20 31 31 33 0a 23 64 65 66 69 6e        113.#defin
4aeb0 65 20 4f 50 5f 4d 6f 76 65 20 20 20 20 20 20 20  e OP_Move       
4aec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4aed0 20 20 20 20 20 20 20 20 31 31 34 0a 23 64 65 66          114.#def
4aee0 69 6e 65 20 4f 50 5f 42 6c 6f 62 20 20 20 20 20  ine OP_Blob     
4aef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4af00 20 20 20 20 20 20 20 20 20 20 31 31 35 0a 23 64            115.#d
4af10 65 66 69 6e 65 20 4f 50 5f 41 64 64 20 20 20 20  efine OP_Add    
4af20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4af30 20 20 20 20 20 20 20 20 20 20 20 20 20 37 38 20               78 
4af40 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
4af50 50 4c 55 53 20 20 20 20 20 2a 2f 0a 23 64 65 66  PLUS     */.#def
4af60 69 6e 65 20 4f 50 5f 52 65 77 69 6e 64 20 20 20  ine OP_Rewind   
4af70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4af80 20 20 20 20 20 20 20 20 20 20 31 31 36 0a 23 64            116.#d
4af90 65 66 69 6e 65 20 4f 50 5f 4d 6f 76 65 47 65 20  efine OP_MoveGe 
4afa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4afb0 20 20 20 20 20 20 20 20 20 20 20 20 31 31 37 0a              117.
4afc0 23 64 65 66 69 6e 65 20 4f 50 5f 56 42 65 67 69  #define OP_VBegi
4afd0 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
4afe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31                11
4aff0 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 55 70  8.#define OP_VUp
4b000 64 61 74 65 20 20 20 20 20 20 20 20 20 20 20 20  date            
4b010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b020 31 31 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49  119.#define OP_I
4b030 66 5a 65 72 6f 20 20 20 20 20 20 20 20 20 20 20  fZero           
4b040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b050 20 20 31 32 30 0a 23 64 65 66 69 6e 65 20 4f 50    120.#define OP
4b060 5f 42 69 74 4e 6f 74 20 20 20 20 20 20 20 20 20  _BitNot         
4b070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b080 20 20 20 20 20 38 37 20 20 20 2f 2a 20 73 61 6d       87   /* sam
4b090 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f 54 20 20  e as TK_BITNOT  
4b0a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56   */.#define OP_V
4b0b0 43 72 65 61 74 65 20 20 20 20 20 20 20 20 20 20  Create          
4b0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b0d0 20 20 31 32 31 0a 23 64 65 66 69 6e 65 20 4f 50    121.#define OP
4b0e0 5f 46 6f 75 6e 64 20 20 20 20 20 20 20 20 20 20  _Found          
4b0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b100 20 20 20 20 31 32 32 0a 23 64 65 66 69 6e 65 20      122.#define 
4b110 4f 50 5f 49 66 50 6f 73 20 20 20 20 20 20 20 20  OP_IfPos        
4b120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b130 20 20 20 20 20 20 31 32 33 0a 23 64 65 66 69 6e        123.#defin
4b140 65 20 4f 50 5f 4e 75 6c 6c 52 6f 77 20 20 20 20  e OP_NullRow    
4b150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b160 20 20 20 20 20 20 20 20 31 32 34 0a 0a 2f 2a 20          124../* 
4b170 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70  The following op
4b180 63 6f 64 65 20 76 61 6c 75 65 73 20 61 72 65 20  code values are 
4b190 6e 65 76 65 72 20 75 73 65 64 20 2a 2f 0a 23 64  never used */.#d
4b1a0 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64  efine OP_NotUsed
4b1b0 5f 31 32 36 20 20 20 20 20 20 20 20 20 20 20 20  _126            
4b1c0 20 20 20 20 20 20 20 20 20 20 20 20 31 32 36 0a              126.
4b1d0 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73  #define OP_NotUs
4b1e0 65 64 5f 31 32 37 20 20 20 20 20 20 20 20 20 20  ed_127          
4b1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
4b200 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74  7.#define OP_Not
4b210 55 73 65 64 5f 31 32 38 20 20 20 20 20 20 20 20  Used_128        
4b220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b230 31 32 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e  128.#define OP_N
4b240 6f 74 55 73 65 64 5f 31 32 39 20 20 20 20 20 20  otUsed_129      
4b250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b260 20 20 31 32 39 0a 23 64 65 66 69 6e 65 20 4f 50    129.#define OP
4b270 5f 4e 6f 74 55 73 65 64 5f 31 33 30 20 20 20 20  _NotUsed_130    
4b280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b290 20 20 20 20 31 33 30 0a 23 64 65 66 69 6e 65 20      130.#define 
4b2a0 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 31 20 20  OP_NotUsed_131  
4b2b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b2c0 20 20 20 20 20 20 31 33 31 0a 23 64 65 66 69 6e        131.#defin
4b2d0 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 32  e OP_NotUsed_132
4b2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b2f0 20 20 20 20 20 20 20 20 31 33 32 0a 23 64 65 66          132.#def
4b300 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31  ine OP_NotUsed_1
4b310 33 33 20 20 20 20 20 20 20 20 20 20 20 20 20 20  33              
4b320 20 20 20 20 20 20 20 20 20 20 31 33 33 0a 23 64            133.#d
4b330 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64  efine OP_NotUsed
4b340 5f 31 33 34 20 20 20 20 20 20 20 20 20 20 20 20  _134            
4b350 20 20 20 20 20 20 20 20 20 20 20 20 31 33 34 0a              134.
4b360 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73  #define OP_NotUs
4b370 65 64 5f 31 33 35 20 20 20 20 20 20 20 20 20 20  ed_135          
4b380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33                13
4b390 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74  5.#define OP_Not
4b3a0 55 73 65 64 5f 31 33 36 20 20 20 20 20 20 20 20  Used_136        
4b3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b3c0 31 33 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e  136.#define OP_N
4b3d0 6f 74 55 73 65 64 5f 31 33 37 20 20 20 20 20 20  otUsed_137      
4b3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b3f0 20 20 31 33 37 0a 0a 0a 2f 2a 20 50 72 6f 70 65    137.../* Prope
4b400 72 74 69 65 73 20 73 75 63 68 20 61 73 20 22 6f  rties such as "o
4b410 75 74 32 22 20 6f 72 20 22 6a 75 6d 70 22 20 74  ut2" or "jump" t
4b420 68 61 74 20 61 72 65 20 73 70 65 63 69 66 69 65  hat are specifie
4b430 64 20 69 6e 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73  d in.** comments
4b440 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22   following the "
4b450 63 61 73 65 22 20 66 6f 72 20 65 61 63 68 20 6f  case" for each o
4b460 70 63 6f 64 65 20 69 6e 20 74 68 65 20 76 64 62  pcode in the vdb
4b470 65 2e 63 0a 2a 2a 20 61 72 65 20 65 6e 63 6f 64  e.c.** are encod
4b480 65 64 20 69 6e 74 6f 20 62 69 74 76 65 63 74 6f  ed into bitvecto
4b490 72 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  rs as follows:.*
4b4a0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f  /.#define OPFLG_
4b4b0 4a 55 4d 50 20 20 20 20 20 20 20 20 20 20 20 20  JUMP            
4b4c0 30 78 30 30 30 31 20 20 2f 2a 20 6a 75 6d 70 3a  0x0001  /* jump:
4b4d0 20 20 50 32 20 68 6f 6c 64 73 20 6a 6d 70 20 74    P2 holds jmp t
4b4e0 61 72 67 65 74 20 2a 2f 0a 23 64 65 66 69 6e 65  arget */.#define
4b4f0 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50 52 45 52   OPFLG_OUT2_PRER
4b500 45 4c 45 41 53 45 20 30 78 30 30 30 32 20 20 2f  ELEASE 0x0002  /
4b510 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73  * out2-prereleas
4b520 65 3a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  e: */.#define OP
4b530 46 4c 47 5f 49 4e 31 20 20 20 20 20 20 20 20 20  FLG_IN1         
4b540 20 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 69      0x0004  /* i
4b550 6e 31 3a 20 20 20 50 31 20 69 73 20 61 6e 20 69  n1:   P1 is an i
4b560 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  nput */.#define 
4b570 4f 50 46 4c 47 5f 49 4e 32 20 20 20 20 20 20 20  OPFLG_IN2       
4b580 20 20 20 20 20 20 30 78 30 30 30 38 20 20 2f 2a        0x0008  /*
4b590 20 69 6e 32 3a 20 20 20 50 32 20 69 73 20 61 6e   in2:   P2 is an
4b5a0 20 69 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e   input */.#defin
4b5b0 65 20 4f 50 46 4c 47 5f 49 4e 33 20 20 20 20 20  e OPFLG_IN3     
4b5c0 20 20 20 20 20 20 20 20 30 78 30 30 31 30 20 20          0x0010  
4b5d0 2f 2a 20 69 6e 33 3a 20 20 20 50 33 20 69 73 20  /* in3:   P3 is 
4b5e0 61 6e 20 69 6e 70 75 74 20 2a 2f 0a 23 64 65 66  an input */.#def
4b5f0 69 6e 65 20 4f 50 46 4c 47 5f 4f 55 54 33 20 20  ine OPFLG_OUT3  
4b600 20 20 20 20 20 20 20 20 20 20 30 78 30 30 32 30            0x0020
4b610 20 20 2f 2a 20 6f 75 74 33 3a 20 20 50 33 20 69    /* out3:  P3 i
4b620 73 20 61 6e 20 6f 75 74 70 75 74 20 2a 2f 0a 23  s an output */.#
4b630 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 49  define OPFLG_INI
4b640 54 49 41 4c 49 5a 45 52 20 7b 5c 0a 2f 2a 20 20  TIALIZER {\./*  
4b650 20 30 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 31   0 */ 0x00, 0x01
4b660 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
4b670 31 30 2c 20 30 78 30 32 2c 20 30 78 31 31 2c 20  10, 0x02, 0x11, 
4b680 30 78 30 30 2c 5c 0a 2f 2a 20 20 20 38 20 2a 2f  0x00,\./*   8 */
4b690 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
4b6a0 35 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30  5, 0x02, 0x00, 0
4b6b0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
4b6c0 5c 0a 2f 2a 20 20 31 36 20 2a 2f 20 30 78 30 34  \./*  16 */ 0x04
4b6d0 2c 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78  , 0x00, 0x01, 0x
4b6e0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
4b6f0 30 78 30 35 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20  0x05, 0x00,\./* 
4b700 20 32 34 20 2a 2f 20 30 78 30 30 2c 20 30 78 30   24 */ 0x00, 0x0
4b710 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30  2, 0x02, 0x02, 0
4b720 78 30 34 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x04, 0x00, 0x00,
4b730 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 33 32 20 2a   0x00,\./*  32 *
4b740 2f 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78  / 0x00, 0x02, 0x
4b750 31 31 2c 20 30 78 31 31 2c 20 30 78 30 32 2c 20  11, 0x11, 0x02, 
4b760 30 78 30 35 2c 20 30 78 30 30 2c 20 30 78 30 32  0x05, 0x00, 0x02
4b770 2c 5c 0a 2f 2a 20 20 34 30 20 2a 2f 20 30 78 31  ,\./*  40 */ 0x1
4b780 31 2c 20 30 78 30 34 2c 20 30 78 30 30 2c 20 30  1, 0x04, 0x00, 0
4b790 78 30 30 2c 20 30 78 30 63 2c 20 30 78 31 31 2c  x00, 0x0c, 0x11,
4b7a0 20 30 78 30 31 2c 20 30 78 30 32 2c 5c 0a 2f 2a   0x01, 0x02,\./*
4b7b0 20 20 34 38 20 2a 2f 20 30 78 30 31 2c 20 30 78    48 */ 0x01, 0x
4b7c0 30 30 2c 20 30 78 30 32 2c 20 30 78 30 31 2c 20  00, 0x02, 0x01, 
4b7d0 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 30  0x01, 0x02, 0x00
4b7e0 2c 20 30 78 30 34 2c 5c 0a 2f 2a 20 20 35 36 20  , 0x04,\./*  56 
4b7f0 2a 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  */ 0x00, 0x00, 0
4b800 78 30 30 2c 20 30 78 31 31 2c 20 30 78 32 63 2c  x00, 0x11, 0x2c,
4b810 20 30 78 32 63 2c 20 30 78 30 30 2c 20 30 78 30   0x2c, 0x00, 0x0
4b820 30 2c 5c 0a 2f 2a 20 20 36 34 20 2a 2f 20 30 78  0,\./*  64 */ 0x
4b830 31 31 2c 20 30 78 30 35 2c 20 30 78 30 35 2c 20  11, 0x05, 0x05, 
4b840 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78 31 35  0x15, 0x15, 0x15
4b850 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 5c 0a 2f  , 0x15, 0x15,\./
4b860 2a 20 20 37 32 20 2a 2f 20 30 78 31 35 2c 20 30  *  72 */ 0x15, 0
4b870 78 30 35 2c 20 30 78 32 63 2c 20 30 78 32 63 2c  x05, 0x2c, 0x2c,
4b880 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32   0x2c, 0x2c, 0x2
4b890 63 2c 20 30 78 32 63 2c 5c 0a 2f 2a 20 20 38 30  c, 0x2c,\./*  80
4b8a0 20 2a 2f 20 30 78 32 63 2c 20 30 78 32 63 2c 20   */ 0x2c, 0x2c, 
4b8b0 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 30 30  0x2c, 0x2c, 0x00
4b8c0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
4b8d0 30 34 2c 5c 0a 2f 2a 20 20 38 38 20 2a 2f 20 30  04,\./*  88 */ 0
4b8e0 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x02, 0x00, 0x00,
4b8f0 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30   0x01, 0x00, 0x0
4b900 31 2c 20 30 78 30 30 2c 20 30 78 31 31 2c 5c 0a  1, 0x00, 0x11,\.
4b910 2f 2a 20 20 39 36 20 2a 2f 20 30 78 30 30 2c 20  /*  96 */ 0x00, 
4b920 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
4b930 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78  , 0x01, 0x00, 0x
4b940 30 30 2c 20 30 78 30 31 2c 5c 0a 2f 2a 20 31 30  00, 0x01,\./* 10
4b950 34 20 2a 2f 20 30 78 30 38 2c 20 30 78 30 30 2c  4 */ 0x08, 0x00,
4b960 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30   0x02, 0x02, 0x0
4b970 35 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  5, 0x00, 0x00, 0
4b980 78 30 30 2c 5c 0a 2f 2a 20 31 31 32 20 2a 2f 20  x00,\./* 112 */ 
4b990 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 30 30  0x00, 0x02, 0x00
4b9a0 2c 20 30 78 30 32 2c 20 30 78 30 31 2c 20 30 78  , 0x02, 0x01, 0x
4b9b0 31 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 5c  11, 0x00, 0x00,\
4b9c0 0a 2f 2a 20 31 32 30 20 2a 2f 20 30 78 30 35 2c  ./* 120 */ 0x05,
4b9d0 20 30 78 30 30 2c 20 30 78 31 31 2c 20 30 78 30   0x00, 0x11, 0x0
4b9e0 35 2c 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30  5, 0x00, 0x02, 0
4b9f0 78 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 31  x00, 0x00,\./* 1
4ba00 32 38 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 30  28 */ 0x00, 0x00
4ba10 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
4ba20 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
4ba30 30 78 30 30 2c 5c 0a 2f 2a 20 31 33 36 20 2a 2f  0x00,\./* 136 */
4ba40 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
4ba50 34 2c 20 30 78 30 34 2c 20 30 78 30 34 2c 20 30  4, 0x04, 0x04, 0
4ba60 78 30 34 2c 20 30 78 30 34 2c 7d 0a 0a 2f 2a 2a  x04, 0x04,}../**
4ba70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
4ba80 20 6f 66 20 6f 70 63 6f 64 65 73 2e 68 20 2a 2a   of opcodes.h **
4ba90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4baa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4bab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
4bac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
4bad0 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
4bae0 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 76 64 62   left off in vdb
4baf0 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
4bb00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
4bb10 0a 2a 2a 20 50 72 6f 74 6f 74 79 70 65 73 20 66  .** Prototypes f
4bb20 6f 72 20 74 68 65 20 56 44 42 45 20 69 6e 74 65  or the VDBE inte
4bb30 72 66 61 63 65 2e 20 20 53 65 65 20 63 6f 6d 6d  rface.  See comm
4bb40 65 6e 74 73 20 6f 6e 20 74 68 65 20 69 6d 70 6c  ents on the impl
4bb50 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 66 6f  ementation.** fo
4bb60 72 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20  r a description 
4bb70 6f 66 20 77 68 61 74 20 65 61 63 68 20 6f 66 20  of what each of 
4bb80 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 64  these routines d
4bb90 6f 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  oes..*/.SQLITE_P
4bba0 52 49 56 41 54 45 20 56 64 62 65 20 2a 73 71 6c  RIVATE Vdbe *sql
4bbb0 69 74 65 33 56 64 62 65 43 72 65 61 74 65 28 73  ite3VdbeCreate(s
4bbc0 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45  qlite3*);.SQLITE
4bbd0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4bbe0 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 56  ite3VdbeAddOp0(V
4bbf0 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  dbe*,int);.SQLIT
4bc00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4bc10 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
4bc20 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a  Vdbe*,int,int);.
4bc30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4bc40 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
4bc50 64 4f 70 32 28 56 64 62 65 2a 2c 69 6e 74 2c 69  dOp2(Vdbe*,int,i
4bc60 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  nt,int);.SQLITE_
4bc70 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4bc80 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 56 64  te3VdbeAddOp3(Vd
4bc90 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c  be*,int,int,int,
4bca0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
4bcb0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4bcc0 56 64 62 65 41 64 64 4f 70 34 28 56 64 62 65 2a  VdbeAddOp4(Vdbe*
4bcd0 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  ,int,int,int,int
4bce0 2c 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34  ,const char *zP4
4bcf0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
4bd00 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4bd10 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 56  3VdbeAddOpList(V
4bd20 64 62 65 2a 2c 20 69 6e 74 20 6e 4f 70 2c 20 56  dbe*, int nOp, V
4bd30 64 62 65 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20  dbeOpList const 
4bd40 2a 61 4f 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52  *aOp);.SQLITE_PR
4bd50 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
4bd60 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 56  e3VdbeChangeP1(V
4bd70 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20  dbe*, int addr, 
4bd80 69 6e 74 20 50 31 29 3b 0a 53 51 4c 49 54 45 5f  int P1);.SQLITE_
4bd90 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
4bda0 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32  ite3VdbeChangeP2
4bdb0 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72  (Vdbe*, int addr
4bdc0 2c 20 69 6e 74 20 50 32 29 3b 0a 53 51 4c 49 54  , int P2);.SQLIT
4bdd0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
4bde0 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
4bdf0 50 33 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64  P3(Vdbe*, int ad
4be00 64 72 2c 20 69 6e 74 20 50 33 29 3b 0a 53 51 4c  dr, int P3);.SQL
4be10 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
4be20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
4be30 67 65 50 35 28 56 64 62 65 2a 2c 20 75 38 20 50  geP5(Vdbe*, u8 P
4be40 35 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  5);.SQLITE_PRIVA
4be50 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
4be60 64 62 65 4a 75 6d 70 48 65 72 65 28 56 64 62 65  dbeJumpHere(Vdbe
4be70 2a 2c 20 69 6e 74 20 61 64 64 72 29 3b 0a 53 51  *, int addr);.SQ
4be80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
4be90 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61  d sqlite3VdbeCha
4bea0 6e 67 65 54 6f 4e 6f 6f 70 28 56 64 62 65 2a 2c  ngeToNoop(Vdbe*,
4beb0 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 4e   int addr, int N
4bec0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4bed0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
4bee0 62 65 43 68 61 6e 67 65 50 34 28 56 64 62 65 2a  beChangeP4(Vdbe*
4bef0 2c 20 69 6e 74 20 61 64 64 72 2c 20 63 6f 6e 73  , int addr, cons
4bf00 74 20 63 68 61 72 20 2a 7a 50 34 2c 20 69 6e 74  t char *zP4, int
4bf10 20 4e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   N);.SQLITE_PRIV
4bf20 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
4bf30 56 64 62 65 55 73 65 73 42 74 72 65 65 28 56 64  VdbeUsesBtree(Vd
4bf40 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  be*, int);.SQLIT
4bf50 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 4f 70  E_PRIVATE VdbeOp
4bf60 20 2a 73 71 6c 69 74 65 33 56 64 62 65 47 65 74   *sqlite3VdbeGet
4bf70 4f 70 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a  Op(Vdbe*, int);.
4bf80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4bf90 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61  nt sqlite3VdbeMa
4bfa0 6b 65 4c 61 62 65 6c 28 56 64 62 65 2a 29 3b 0a  keLabel(Vdbe*);.
4bfb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
4bfc0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44  oid sqlite3VdbeD
4bfd0 65 6c 65 74 65 28 56 64 62 65 2a 29 3b 0a 53 51  elete(Vdbe*);.SQ
4bfe0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
4bff0 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  d sqlite3VdbeMak
4c000 65 52 65 61 64 79 28 56 64 62 65 2a 2c 69 6e 74  eReady(Vdbe*,int
4c010 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53  ,int,int,int);.S
4c020 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4c030 74 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e  t sqlite3VdbeFin
4c040 61 6c 69 7a 65 28 56 64 62 65 2a 29 3b 0a 53 51  alize(Vdbe*);.SQ
4c050 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
4c060 64 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73  d sqlite3VdbeRes
4c070 6f 6c 76 65 4c 61 62 65 6c 28 56 64 62 65 2a 2c  olveLabel(Vdbe*,
4c080 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
4c090 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4c0a0 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
4c0b0 28 56 64 62 65 2a 29 3b 0a 23 69 66 64 65 66 20  (Vdbe*);.#ifdef 
4c0c0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c  SQLITE_DEBUG.SQL
4c0d0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
4c0e0 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 54 72  id sqlite3VdbeTr
4c0f0 61 63 65 28 56 64 62 65 2a 2c 46 49 4c 45 2a 29  ace(Vdbe*,FILE*)
4c100 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f  ;.#endif.SQLITE_
4c110 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
4c120 69 74 65 33 56 64 62 65 52 65 73 65 74 53 74 65  ite3VdbeResetSte
4c130 70 52 65 73 75 6c 74 28 56 64 62 65 2a 29 3b 0a  pResult(Vdbe*);.
4c140 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4c150 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  nt sqlite3VdbeRe
4c160 73 65 74 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b  set(Vdbe*, int);
4c170 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4c180 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
4c190 53 65 74 4e 75 6d 43 6f 6c 73 28 56 64 62 65 2a  SetNumCols(Vdbe*
4c1a0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
4c1b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4c1c0 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
4c1d0 56 64 62 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c  Vdbe*, int, int,
4c1e0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69   const char *, i
4c1f0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
4c200 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
4c210 56 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65 73  VdbeCountChanges
4c220 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  (Vdbe*);.SQLITE_
4c230 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 20  PRIVATE sqlite3 
4c240 2a 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 56  *sqlite3VdbeDb(V
4c250 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  dbe*);.SQLITE_PR
4c260 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
4c270 65 33 56 64 62 65 53 65 74 53 71 6c 28 56 64 62  e3VdbeSetSql(Vdb
4c280 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  e*, const char *
4c290 7a 2c 20 69 6e 74 20 6e 29 3b 0a 53 51 4c 49 54  z, int n);.SQLIT
4c2a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
4c2b0 71 6c 69 74 65 33 56 64 62 65 53 77 61 70 28 56  qlite3VdbeSwap(V
4c2c0 64 62 65 2a 2c 56 64 62 65 2a 29 3b 0a 0a 23 69  dbe*,Vdbe*);..#i
4c2d0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
4c2e0 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45  LE_MEMORY_MANAGE
4c2f0 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56  MENT.SQLITE_PRIV
4c300 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
4c310 64 62 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79  dbeReleaseMemory
4c320 28 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 53 51  (int);.#endif.SQ
4c330 4c 49 54 45 5f 50 52 49 56 41 54 45 20 55 6e 70  LITE_PRIVATE Unp
4c340 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 73 71 6c  ackedRecord *sql
4c350 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e  ite3VdbeRecordUn
4c360 70 61 63 6b 28 4b 65 79 49 6e 66 6f 2a 2c 69 6e  pack(KeyInfo*,in
4c370 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 76 6f  t,const void*,vo
4c380 69 64 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  id*,int);.SQLITE
4c390 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
4c3a0 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55  lite3VdbeDeleteU
4c3b0 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 55 6e  npackedRecord(Un
4c3c0 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0a  packedRecord*);.
4c3d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4c3e0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  nt sqlite3VdbeRe
4c3f0 63 6f 72 64 43 6f 6d 70 61 72 65 28 69 6e 74 2c  cordCompare(int,
4c400 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 55 6e 70 61  const void*,Unpa
4c410 63 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0a 0a 0a  ckedRecord*);...
4c420 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53  #ifndef NDEBUG.S
4c430 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
4c440 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
4c450 43 6f 6d 6d 65 6e 74 28 56 64 62 65 2a 2c 20 63  Comment(Vdbe*, c
4c460 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29  onst char*, ...)
4c470 3b 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 43  ;.# define VdbeC
4c480 6f 6d 6d 65 6e 74 28 58 29 20 20 73 71 6c 69 74  omment(X)  sqlit
4c490 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74 20 58 0a  e3VdbeComment X.
4c4a0 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 56  #else.# define V
4c4b0 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 65  dbeComment(X).#e
4c4c0 6e 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ndif..#endif../*
4c4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
4c4e0 64 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a  d of vdbe.h ****
4c4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4c500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4c510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
4c520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f  ************* Co
4c530 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77  ntinuing where w
4c540 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71  e left off in sq
4c550 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  liteInt.h ******
4c560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
4c570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
4c580 63 6c 75 64 65 20 70 61 67 65 72 2e 68 20 69 6e  clude pager.h in
4c590 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73   the middle of s
4c5a0 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a  qliteInt.h *****
4c5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
4c5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
4c5d0 67 69 6e 20 66 69 6c 65 20 70 61 67 65 72 2e 68  gin file pager.h
4c5e0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
4c5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4c600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
4c610 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
4c620 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
4c630 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
4c640 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
4c650 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
4c660 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
4c670 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
4c680 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
4c690 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
4c6a0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
4c6b0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
4c6c0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
4c6d0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
4c6e0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
4c6f0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
4c700 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
4c710 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
4c720 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
4c730 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
4c740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4c750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4c760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4c770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4c780 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61  ****.** This hea
4c790 64 65 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73  der file defines
4c7a0 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74   the interface t
4c7b0 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 20 70  hat the sqlite p
4c7c0 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 73 75 62  age cache.** sub
4c7d0 73 79 73 74 65 6d 2e 20 20 54 68 65 20 70 61 67  system.  The pag
4c7e0 65 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65  e cache subsyste
4c7f0 6d 20 72 65 61 64 73 20 61 6e 64 20 77 72 69 74  m reads and writ
4c800 65 73 20 61 20 66 69 6c 65 20 61 20 70 61 67 65  es a file a page
4c810 0a 2a 2a 20 61 74 20 61 20 74 69 6d 65 20 61 6e  .** at a time an
4c820 64 20 70 72 6f 76 69 64 65 73 20 61 20 6a 6f 75  d provides a jou
4c830 72 6e 61 6c 20 66 6f 72 20 72 6f 6c 6c 62 61 63  rnal for rollbac
4c840 6b 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49  k..**.** @(#) $I
4c850 64 3a 20 70 61 67 65 72 2e 68 2c 76 20 31 2e 37  d: pager.h,v 1.7
4c860 32 20 32 30 30 38 2f 30 35 2f 30 31 20 31 37 3a  2 2008/05/01 17:
4c870 30 33 3a 34 39 20 64 72 68 20 45 78 70 20 24 0a  03:49 drh Exp $.
4c880 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50 41 47  */..#ifndef _PAG
4c890 45 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 50  ER_H_.#define _P
4c8a0 41 47 45 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54  AGER_H_../*.** T
4c8b0 68 65 20 74 79 70 65 20 75 73 65 64 20 74 6f 20  he type used to 
4c8c0 72 65 70 72 65 73 65 6e 74 20 61 20 70 61 67 65  represent a page
4c8d0 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 20 66 69   number.  The fi
4c8e0 72 73 74 20 70 61 67 65 20 69 6e 20 61 20 66 69  rst page in a fi
4c8f0 6c 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20  le.** is called 
4c900 70 61 67 65 20 31 2e 20 20 30 20 69 73 20 75 73  page 1.  0 is us
4c910 65 64 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20  ed to represent 
4c920 22 6e 6f 74 20 61 20 70 61 67 65 22 2e 0a 2a 2f  "not a page"..*/
4c930 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65  .typedef unsigne
4c940 64 20 69 6e 74 20 50 67 6e 6f 3b 0a 0a 2f 2a 0a  d int Pgno;../*.
4c950 2a 2a 20 45 61 63 68 20 6f 70 65 6e 20 66 69 6c  ** Each open fil
4c960 65 20 69 73 20 6d 61 6e 61 67 65 64 20 62 79 20  e is managed by 
4c970 61 20 73 65 70 61 72 61 74 65 20 69 6e 73 74 61  a separate insta
4c980 6e 63 65 20 6f 66 20 74 68 65 20 22 50 61 67 65  nce of the "Page
4c990 72 22 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  r" structure..*/
4c9a0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4c9b0 50 61 67 65 72 20 50 61 67 65 72 3b 0a 0a 2f 2a  Pager Pager;../*
4c9c0 0a 2a 2a 20 48 61 6e 64 6c 65 20 74 79 70 65 20  .** Handle type 
4c9d0 66 6f 72 20 70 61 67 65 73 2e 0a 2a 2f 0a 74 79  for pages..*/.ty
4c9e0 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67 48  pedef struct PgH
4c9f0 64 72 20 44 62 50 61 67 65 3b 0a 0a 2f 2a 0a 2a  dr DbPage;../*.*
4ca00 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73  * Allowed values
4ca10 20 66 6f 72 20 74 68 65 20 66 6c 61 67 73 20 70   for the flags p
4ca20 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69  arameter to sqli
4ca30 74 65 33 50 61 67 65 72 4f 70 65 6e 28 29 2e 0a  te3PagerOpen()..
4ca40 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 54 68 69 73  **.** NOTE: This
4ca50 20 76 61 6c 75 65 73 20 6d 75 73 74 20 6d 61 74   values must mat
4ca60 63 68 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  ch the correspon
4ca70 64 69 6e 67 20 42 54 52 45 45 5f 20 76 61 6c 75  ding BTREE_ valu
4ca80 65 73 20 69 6e 20 62 74 72 65 65 2e 68 2e 0a 2a  es in btree.h..*
4ca90 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f  /.#define PAGER_
4caa0 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 20 30 78  OMIT_JOURNAL  0x
4cab0 30 30 30 31 20 20 20 20 2f 2a 20 44 6f 20 6e 6f  0001    /* Do no
4cac0 74 20 75 73 65 20 61 20 72 6f 6c 6c 62 61 63 6b  t use a rollback
4cad0 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64 65 66   journal */.#def
4cae0 69 6e 65 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41  ine PAGER_NO_REA
4caf0 44 4c 4f 43 4b 20 20 20 30 78 30 30 30 32 20 20  DLOCK   0x0002  
4cb00 20 20 2f 2a 20 4f 6d 69 74 20 72 65 61 64 6c 6f    /* Omit readlo
4cb10 63 6b 73 20 6f 6e 20 72 65 61 64 6f 6e 6c 79 20  cks on readonly 
4cb20 66 69 6c 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  files */../*.** 
4cb30 56 61 6c 69 64 20 76 61 6c 75 65 73 20 66 6f 72  Valid values for
4cb40 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
4cb50 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 50  ment to sqlite3P
4cb60 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28  agerLockingMode(
4cb70 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41  )..*/.#define PA
4cb80 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
4cb90 51 55 45 52 59 20 20 20 20 20 20 2d 31 0a 23 64  QUERY      -1.#d
4cba0 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b  efine PAGER_LOCK
4cbb0 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 20  INGMODE_NORMAL  
4cbc0 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 50 41      0.#define PA
4cbd0 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
4cbe0 45 58 43 4c 55 53 49 56 45 20 20 20 31 0a 0a 2f  EXCLUSIVE   1../
4cbf0 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61 6c 75 65  *.** Valid value
4cc00 73 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64  s for the second
4cc10 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c   argument to sql
4cc20 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c  ite3PagerJournal
4cc30 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69  Mode()..*/.#defi
4cc40 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  ne PAGER_JOURNAL
4cc50 4d 4f 44 45 5f 51 55 45 52 59 20 20 20 20 20 20  MODE_QUERY      
4cc60 2d 31 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  -1.#define PAGER
4cc70 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c  _JOURNALMODE_DEL
4cc80 45 54 45 20 20 20 20 20 20 30 20 20 20 2f 2a 20  ETE      0   /* 
4cc90 43 6f 6d 6d 69 74 20 62 79 20 64 65 6c 65 74 69  Commit by deleti
4cca0 6e 67 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ng journal file 
4ccb0 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  */.#define PAGER
4ccc0 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52  _JOURNALMODE_PER
4ccd0 53 49 53 54 20 20 20 20 20 31 20 20 20 2f 2a 20  SIST     1   /* 
4cce0 43 6f 6d 6d 69 74 20 62 79 20 7a 65 72 6f 69 6e  Commit by zeroin
4ccf0 67 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  g journal header
4cd00 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45   */.#define PAGE
4cd10 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46  R_JOURNALMODE_OF
4cd20 46 20 20 20 20 20 20 20 20 20 32 20 20 20 2f 2a  F         2   /*
4cd30 20 4a 6f 75 72 6e 61 6c 20 6f 6d 69 74 74 65 64   Journal omitted
4cd40 2e 20 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 65  .  */../*.** See
4cd50 20 73 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6d   source code com
4cd60 6d 65 6e 74 73 20 66 6f 72 20 61 20 64 65 74 61  ments for a deta
4cd70 69 6c 65 64 20 64 65 73 63 72 69 70 74 69 6f 6e  iled description
4cd80 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
4cd90 67 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 3a 0a 2a  g.** routines:.*
4cda0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
4cdb0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
4cdc0 72 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66  rOpen(sqlite3_vf
4cdd0 73 20 2a 2c 20 50 61 67 65 72 20 2a 2a 70 70 50  s *, Pager **ppP
4cde0 61 67 65 72 2c 20 63 6f 6e 73 74 20 63 68 61 72  ager, const char
4cdf0 2a 2c 20 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b  *, int,int,int);
4ce00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4ce10 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
4ce20 72 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28  rSetBusyhandler(
4ce30 50 61 67 65 72 2a 2c 20 42 75 73 79 48 61 6e 64  Pager*, BusyHand
4ce40 6c 65 72 20 2a 70 42 75 73 79 48 61 6e 64 6c 65  ler *pBusyHandle
4ce50 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  r);.SQLITE_PRIVA
4ce60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
4ce70 61 67 65 72 53 65 74 44 65 73 74 72 75 63 74 6f  agerSetDestructo
4ce80 72 28 50 61 67 65 72 2a 2c 20 76 6f 69 64 28 2a  r(Pager*, void(*
4ce90 29 28 44 62 50 61 67 65 2a 2c 69 6e 74 29 29 3b  )(DbPage*,int));
4cea0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4ceb0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
4cec0 72 53 65 74 52 65 69 6e 69 74 65 72 28 50 61 67  rSetReiniter(Pag
4ced0 65 72 2a 2c 20 76 6f 69 64 28 2a 29 28 44 62 50  er*, void(*)(DbP
4cee0 61 67 65 2a 2c 69 6e 74 29 29 3b 0a 53 51 4c 49  age*,int));.SQLI
4cef0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4cf00 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61  qlite3PagerSetPa
4cf10 67 65 73 69 7a 65 28 50 61 67 65 72 2a 2c 20 75  gesize(Pager*, u
4cf20 31 36 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  16*);.SQLITE_PRI
4cf30 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4cf40 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e  PagerMaxPageCoun
4cf50 74 28 50 61 67 65 72 2a 2c 20 69 6e 74 29 3b 0a  t(Pager*, int);.
4cf60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4cf70 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52  nt sqlite3PagerR
4cf80 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 50 61  eadFileheader(Pa
4cf90 67 65 72 2a 2c 20 69 6e 74 2c 20 75 6e 73 69 67  ger*, int, unsig
4cfa0 6e 65 64 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  ned char*);.SQLI
4cfb0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
4cfc0 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43  sqlite3PagerSetC
4cfd0 61 63 68 65 73 69 7a 65 28 50 61 67 65 72 2a 2c  achesize(Pager*,
4cfe0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
4cff0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4d000 33 50 61 67 65 72 43 6c 6f 73 65 28 50 61 67 65  3PagerClose(Page
4d010 72 20 2a 70 50 61 67 65 72 29 3b 0a 53 51 4c 49  r *pPager);.SQLI
4d020 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4d030 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69  qlite3PagerAcqui
4d040 72 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  re(Pager *pPager
4d050 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 44 62 50  , Pgno pgno, DbP
4d060 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 69 6e  age **ppPage, in
4d070 74 20 63 6c 72 46 6c 61 67 29 3b 0a 23 64 65 66  t clrFlag);.#def
4d080 69 6e 65 20 73 71 6c 69 74 65 33 50 61 67 65 72  ine sqlite3Pager
4d090 47 65 74 28 41 2c 42 2c 43 29 20 73 71 6c 69 74  Get(A,B,C) sqlit
4d0a0 65 33 50 61 67 65 72 41 63 71 75 69 72 65 28 41  e3PagerAcquire(A
4d0b0 2c 42 2c 43 2c 30 29 0a 53 51 4c 49 54 45 5f 50  ,B,C,0).SQLITE_P
4d0c0 52 49 56 41 54 45 20 44 62 50 61 67 65 20 2a 73  RIVATE DbPage *s
4d0d0 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75  qlite3PagerLooku
4d0e0 70 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  p(Pager *pPager,
4d0f0 20 50 67 6e 6f 20 70 67 6e 6f 29 3b 0a 53 51 4c   Pgno pgno);.SQL
4d100 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4d110 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 28  sqlite3PagerRef(
4d120 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45  DbPage*);.SQLITE
4d130 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4d140 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 44  ite3PagerUnref(D
4d150 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  bPage*);.SQLITE_
4d160 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4d170 74 65 33 50 61 67 65 72 57 72 69 74 65 28 44 62  te3PagerWrite(Db
4d180 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Page*);.SQLITE_P
4d190 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4d1a0 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
4d1b0 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  (Pager*);.SQLITE
4d1c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4d1d0 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74  ite3PagerTruncat
4d1e0 65 28 50 61 67 65 72 2a 2c 50 67 6e 6f 29 3b 0a  e(Pager*,Pgno);.
4d1f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4d200 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 42  nt sqlite3PagerB
4d210 65 67 69 6e 28 44 62 50 61 67 65 2a 2c 20 69 6e  egin(DbPage*, in
4d220 74 20 65 78 46 6c 61 67 29 3b 0a 53 51 4c 49 54  t exFlag);.SQLIT
4d230 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4d240 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74  lite3PagerCommit
4d250 50 68 61 73 65 4f 6e 65 28 50 61 67 65 72 2a 2c  PhaseOne(Pager*,
4d260 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73  const char *zMas
4d270 74 65 72 2c 20 50 67 6e 6f 2c 20 69 6e 74 29 3b  ter, Pgno, int);
4d280 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4d290 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
4d2a0 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 50  CommitPhaseTwo(P
4d2b0 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ager*);.SQLITE_P
4d2c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4d2d0 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28  e3PagerRollback(
4d2e0 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  Pager*);.SQLITE_
4d2f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4d300 74 65 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e  te3PagerIsreadon
4d310 6c 79 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49  ly(Pager*);.SQLI
4d320 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4d330 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d 74 42  qlite3PagerStmtB
4d340 65 67 69 6e 28 50 61 67 65 72 2a 29 3b 0a 53 51  egin(Pager*);.SQ
4d350 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4d360 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d   sqlite3PagerStm
4d370 74 43 6f 6d 6d 69 74 28 50 61 67 65 72 2a 29 3b  tCommit(Pager*);
4d380 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4d390 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
4d3a0 53 74 6d 74 52 6f 6c 6c 62 61 63 6b 28 50 61 67  StmtRollback(Pag
4d3b0 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  er*);.SQLITE_PRI
4d3c0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
4d3d0 33 50 61 67 65 72 44 6f 6e 74 52 6f 6c 6c 62 61  3PagerDontRollba
4d3e0 63 6b 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c  ck(DbPage*);.SQL
4d3f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
4d400 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e   sqlite3PagerDon
4d410 74 57 72 69 74 65 28 44 62 50 61 67 65 2a 29 3b  tWrite(DbPage*);
4d420 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4d430 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
4d440 52 65 66 63 6f 75 6e 74 28 50 61 67 65 72 2a 29  Refcount(Pager*)
4d450 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4d460 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67   void sqlite3Pag
4d470 65 72 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c  erSetSafetyLevel
4d480 28 50 61 67 65 72 2a 2c 69 6e 74 2c 69 6e 74 29  (Pager*,int,int)
4d490 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4d4a0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
4d4b0 69 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d  ite3PagerFilenam
4d4c0 65 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54  e(Pager*);.SQLIT
4d4d0 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
4d4e0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c  sqlite3_vfs *sql
4d4f0 69 74 65 33 50 61 67 65 72 56 66 73 28 50 61 67  ite3PagerVfs(Pag
4d500 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  er*);.SQLITE_PRI
4d510 56 41 54 45 20 73 71 6c 69 74 65 33 5f 66 69 6c  VATE sqlite3_fil
4d520 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46  e *sqlite3PagerF
4d530 69 6c 65 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c  ile(Pager*);.SQL
4d540 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
4d550 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50  t char *sqlite3P
4d560 61 67 65 72 44 69 72 6e 61 6d 65 28 50 61 67 65  agerDirname(Page
4d570 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
4d580 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
4d590 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72  sqlite3PagerJour
4d5a0 6e 61 6c 6e 61 6d 65 28 50 61 67 65 72 2a 29 3b  nalname(Pager*);
4d5b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4d5c0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
4d5d0 4e 6f 73 79 6e 63 28 50 61 67 65 72 2a 29 3b 0a  Nosync(Pager*);.
4d5e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4d5f0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d  nt sqlite3PagerM
4d600 6f 76 65 70 61 67 65 28 50 61 67 65 72 2a 2c 44  ovepage(Pager*,D
4d610 62 50 61 67 65 2a 2c 50 67 6e 6f 29 3b 0a 53 51  bPage*,Pgno);.SQ
4d620 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
4d630 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47  d *sqlite3PagerG
4d640 65 74 44 61 74 61 28 44 62 50 61 67 65 20 2a 29  etData(DbPage *)
4d650 3b 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ; .SQLITE_PRIVAT
4d660 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50  E void *sqlite3P
4d670 61 67 65 72 47 65 74 45 78 74 72 61 28 44 62 50  agerGetExtra(DbP
4d680 61 67 65 20 2a 29 3b 20 0a 53 51 4c 49 54 45 5f  age *); .SQLITE_
4d690 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4d6a0 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d  te3PagerLockingM
4d6b0 6f 64 65 28 50 61 67 65 72 20 2a 2c 20 69 6e 74  ode(Pager *, int
4d6c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4d6d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
4d6e0 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 61  erJournalMode(Pa
4d6f0 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  ger *, int);.SQL
4d700 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
4d710 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 54 65   *sqlite3PagerTe
4d720 6d 70 53 70 61 63 65 28 50 61 67 65 72 2a 29 3b  mpSpace(Pager*);
4d730 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4d740 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
4d750 53 79 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67  Sync(Pager *pPag
4d760 65 72 29 3b 0a 0a 23 69 66 20 64 65 66 69 6e 65  er);..#if define
4d770 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
4d780 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e  MEMORY_MANAGEMEN
4d790 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  T) && !defined(S
4d7a0 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49  QLITE_OMIT_DISKI
4d7b0 4f 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  O).SQLITE_PRIVAT
4d7c0 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 50  E   int sqlite3P
4d7d0 61 67 65 72 52 65 6c 65 61 73 65 4d 65 6d 6f 72  agerReleaseMemor
4d7e0 79 28 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 0a  y(int);.#endif..
4d7f0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41  #ifdef SQLITE_HA
4d800 53 5f 43 4f 44 45 43 0a 53 51 4c 49 54 45 5f 50  S_CODEC.SQLITE_P
4d810 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
4d820 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 6f 64  lite3PagerSetCod
4d830 65 63 28 50 61 67 65 72 2a 2c 76 6f 69 64 2a 28  ec(Pager*,void*(
4d840 2a 29 28 76 6f 69 64 2a 2c 76 6f 69 64 2a 2c 50  *)(void*,void*,P
4d850 67 6e 6f 2c 69 6e 74 29 2c 76 6f 69 64 2a 29 3b  gno,int),void*);
4d860 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65  .#endif..#if !de
4d870 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c  fined(NDEBUG) ||
4d880 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
4d890 54 45 53 54 29 0a 53 51 4c 49 54 45 5f 50 52 49  TEST).SQLITE_PRI
4d8a0 56 41 54 45 20 20 20 50 67 6e 6f 20 73 71 6c 69  VATE   Pgno sqli
4d8b0 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62  te3PagerPagenumb
4d8c0 65 72 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c  er(DbPage*);.SQL
4d8d0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
4d8e0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  t sqlite3PagerIs
4d8f0 77 72 69 74 65 61 62 6c 65 28 44 62 50 61 67 65  writeable(DbPage
4d900 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  *);.#endif..#ifd
4d910 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
4d920 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
4d930 69 6e 74 20 2a 73 71 6c 69 74 65 33 50 61 67 65  int *sqlite3Page
4d940 72 53 74 61 74 73 28 50 61 67 65 72 2a 29 3b 0a  rStats(Pager*);.
4d950 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
4d960 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67   void sqlite3Pag
4d970 65 72 52 65 66 64 75 6d 70 28 50 61 67 65 72 2a  erRefdump(Pager*
4d980 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65  );.#endif..#ifde
4d990 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 76 6f  f SQLITE_TEST.vo
4d9a0 69 64 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c  id disable_simul
4d9b0 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76  ated_io_errors(v
4d9c0 6f 69 64 29 3b 0a 76 6f 69 64 20 65 6e 61 62 6c  oid);.void enabl
4d9d0 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65  e_simulated_io_e
4d9e0 72 72 6f 72 73 28 76 6f 69 64 29 3b 0a 23 65 6c  rrors(void);.#el
4d9f0 73 65 0a 23 20 64 65 66 69 6e 65 20 64 69 73 61  se.# define disa
4da00 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f  ble_simulated_io
4da10 5f 65 72 72 6f 72 73 28 29 0a 23 20 64 65 66 69  _errors().# defi
4da20 6e 65 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61  ne enable_simula
4da30 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a  ted_io_errors().
4da40 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f  #endif..#endif /
4da50 2a 20 5f 50 41 47 45 52 5f 48 5f 20 2a 2f 0a 0a  * _PAGER_H_ */..
4da60 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
4da70 45 6e 64 20 6f 66 20 70 61 67 65 72 2e 68 20 2a  End of pager.h *
4da80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4da90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4daa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
4dab0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
4dac0 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
4dad0 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
4dae0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
4daf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
4db00 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
4db10 20 49 6e 63 6c 75 64 65 20 6f 73 2e 68 20 69 6e   Include os.h in
4db20 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73   the middle of s
4db30 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a  qliteInt.h *****
4db40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
4db50 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
4db60 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 2e 68   Begin file os.h
4db70 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
4db80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4db90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
4dba0 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
4dbb0 65 6d 62 65 72 20 31 36 0a 2a 2a 0a 2a 2a 20 54  ember 16.**.** T
4dbc0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
4dbd0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
4dbe0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
4dbf0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
4dc00 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
4dc10 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
4dc20 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
4dc30 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
4dc40 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
4dc50 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
4dc60 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
4dc70 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
4dc80 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
4dc90 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
4dca0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
4dcb0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
4dcc0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
4dcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dd00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dd10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
4dd20 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66  ** This header f
4dd30 69 6c 65 20 28 74 6f 67 65 74 68 65 72 20 77 69  ile (together wi
4dd40 74 68 20 69 73 20 63 6f 6d 70 61 6e 69 6f 6e 20  th is companion 
4dd50 43 20 73 6f 75 72 63 65 2d 63 6f 64 65 20 66 69  C source-code fi
4dd60 6c 65 0a 2a 2a 20 22 6f 73 2e 63 22 29 20 61 74  le.** "os.c") at
4dd70 74 65 6d 70 74 20 74 6f 20 61 62 73 74 72 61 63  tempt to abstrac
4dd80 74 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  t the underlying
4dd90 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
4dda0 6d 20 73 6f 20 74 68 61 74 0a 2a 2a 20 74 68 65  m so that.** the
4ddb0 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20   SQLite library 
4ddc0 77 69 6c 6c 20 77 6f 72 6b 20 6f 6e 20 62 6f 74  will work on bot
4ddd0 68 20 50 4f 53 49 58 20 61 6e 64 20 77 69 6e 64  h POSIX and wind
4dde0 6f 77 73 20 73 79 73 74 65 6d 73 2e 0a 2a 2a 0a  ows systems..**.
4ddf0 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66  ** This header f
4de00 69 6c 65 20 69 73 20 23 69 6e 63 6c 75 64 65 2d  ile is #include-
4de10 65 64 20 62 79 20 73 71 6c 69 74 65 49 6e 74 2e  ed by sqliteInt.
4de20 68 20 61 6e 64 20 74 68 75 73 20 65 6e 64 73 20  h and thus ends 
4de30 75 70 0a 2a 2a 20 62 65 69 6e 67 20 69 6e 63 6c  up.** being incl
4de40 75 64 65 64 20 62 79 20 65 76 65 72 79 20 73 6f  uded by every so
4de50 75 72 63 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69  urce file..*/.#i
4de60 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 5f 4f 53  fndef _SQLITE_OS
4de70 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c  _H_.#define _SQL
4de80 49 54 45 5f 4f 53 5f 48 5f 0a 0a 2f 2a 0a 2a 2a  ITE_OS_H_../*.**
4de90 20 46 69 67 75 72 65 20 6f 75 74 20 69 66 20 77   Figure out if w
4dea0 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69  e are dealing wi
4deb0 74 68 20 55 6e 69 78 2c 20 57 69 6e 64 6f 77 73  th Unix, Windows
4dec0 2c 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 0a  , or some other.
4ded0 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  ** operating sys
4dee0 74 65 6d 2e 20 20 41 66 74 65 72 20 74 68 65 20  tem.  After the 
4def0 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20  following block 
4df00 6f 66 20 70 72 65 70 72 6f 63 65 73 73 20 6d 61  of preprocess ma
4df10 63 72 6f 73 2c 0a 2a 2a 20 61 6c 6c 20 6f 66 20  cros,.** all of 
4df20 4f 53 5f 55 4e 49 58 2c 20 4f 53 5f 57 49 4e 2c  OS_UNIX, OS_WIN,
4df30 20 4f 53 5f 4f 53 32 2c 20 61 6e 64 20 4f 53 5f   OS_OS2, and OS_
4df40 4f 54 48 45 52 20 77 69 6c 6c 20 64 65 66 69 6e  OTHER will defin
4df50 65 64 20 74 6f 20 65 69 74 68 65 72 0a 2a 2a 20  ed to either.** 
4df60 31 20 6f 72 20 30 2e 20 20 4f 6e 65 20 6f 66 20  1 or 0.  One of 
4df70 74 68 65 20 66 6f 75 72 20 77 69 6c 6c 20 62 65  the four will be
4df80 20 31 2e 20 20 54 68 65 20 6f 74 68 65 72 20 74   1.  The other t
4df90 68 72 65 65 20 77 69 6c 6c 20 62 65 20 30 2e 0a  hree will be 0..
4dfa0 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 4f  */.#if defined(O
4dfb0 53 5f 4f 54 48 45 52 29 0a 23 20 69 66 20 4f 53  S_OTHER).# if OS
4dfc0 5f 4f 54 48 45 52 3d 3d 31 0a 23 20 20 20 75 6e  _OTHER==1.#   un
4dfd0 64 65 66 20 4f 53 5f 55 4e 49 58 0a 23 20 20 20  def OS_UNIX.#   
4dfe0 64 65 66 69 6e 65 20 4f 53 5f 55 4e 49 58 20 30  define OS_UNIX 0
4dff0 0a 23 20 20 20 75 6e 64 65 66 20 4f 53 5f 57 49  .#   undef OS_WI
4e000 4e 0a 23 20 20 20 64 65 66 69 6e 65 20 4f 53 5f  N.#   define OS_
4e010 57 49 4e 20 30 0a 23 20 20 20 75 6e 64 65 66 20  WIN 0.#   undef 
4e020 4f 53 5f 4f 53 32 0a 23 20 20 20 64 65 66 69 6e  OS_OS2.#   defin
4e030 65 20 4f 53 5f 4f 53 32 20 30 0a 23 20 65 6c 73  e OS_OS2 0.# els
4e040 65 0a 23 20 20 20 75 6e 64 65 66 20 4f 53 5f 4f  e.#   undef OS_O
4e050 54 48 45 52 0a 23 20 65 6e 64 69 66 0a 23 65 6e  THER.# endif.#en
4e060 64 69 66 0a 23 69 66 20 21 64 65 66 69 6e 65 64  dif.#if !defined
4e070 28 4f 53 5f 55 4e 49 58 29 20 26 26 20 21 64 65  (OS_UNIX) && !de
4e080 66 69 6e 65 64 28 4f 53 5f 4f 54 48 45 52 29 0a  fined(OS_OTHER).
4e090 23 20 64 65 66 69 6e 65 20 4f 53 5f 4f 54 48 45  # define OS_OTHE
4e0a0 52 20 30 0a 23 20 69 66 6e 64 65 66 20 4f 53 5f  R 0.# ifndef OS_
4e0b0 57 49 4e 0a 23 20 20 20 69 66 20 64 65 66 69 6e  WIN.#   if defin
4e0c0 65 64 28 5f 57 49 4e 33 32 29 20 7c 7c 20 64 65  ed(_WIN32) || de
4e0d0 66 69 6e 65 64 28 57 49 4e 33 32 29 20 7c 7c 20  fined(WIN32) || 
4e0e0 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e  defined(__CYGWIN
4e0f0 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  __) || defined(_
4e100 5f 4d 49 4e 47 57 33 32 5f 5f 29 20 7c 7c 20 64  _MINGW32__) || d
4e110 65 66 69 6e 65 64 28 5f 5f 42 4f 52 4c 41 4e 44  efined(__BORLAND
4e120 43 5f 5f 29 0a 23 20 20 20 20 20 64 65 66 69 6e  C__).#     defin
4e130 65 20 4f 53 5f 57 49 4e 20 31 0a 23 20 20 20 20  e OS_WIN 1.#    
4e140 20 64 65 66 69 6e 65 20 4f 53 5f 55 4e 49 58 20   define OS_UNIX 
4e150 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 4f  0.#     define O
4e160 53 5f 4f 53 32 20 30 0a 23 20 20 20 65 6c 69 66  S_OS2 0.#   elif
4e170 20 64 65 66 69 6e 65 64 28 5f 5f 45 4d 58 5f 5f   defined(__EMX__
4e180 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53  ) || defined(_OS
4e190 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 4f 53  2) || defined(OS
4e1a0 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f  2) || defined(_O
4e1b0 53 32 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  S2_) || defined(
4e1c0 5f 5f 4f 53 32 5f 5f 29 0a 23 20 20 20 20 20 64  __OS2__).#     d
4e1d0 65 66 69 6e 65 20 4f 53 5f 57 49 4e 20 30 0a 23  efine OS_WIN 0.#
4e1e0 20 20 20 20 20 64 65 66 69 6e 65 20 4f 53 5f 55       define OS_U
4e1f0 4e 49 58 20 30 0a 23 20 20 20 20 20 64 65 66 69  NIX 0.#     defi
4e200 6e 65 20 4f 53 5f 4f 53 32 20 31 0a 23 20 20 20  ne OS_OS2 1.#   
4e210 65 6c 73 65 0a 23 20 20 20 20 20 64 65 66 69 6e  else.#     defin
4e220 65 20 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20 20  e OS_WIN 0.#    
4e230 20 64 65 66 69 6e 65 20 4f 53 5f 55 4e 49 58 20   define OS_UNIX 
4e240 31 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 4f  1.#     define O
4e250 53 5f 4f 53 32 20 30 0a 23 20 20 65 6e 64 69 66  S_OS2 0.#  endif
4e260 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e  .# else.#  defin
4e270 65 20 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 64  e OS_UNIX 0.#  d
4e280 65 66 69 6e 65 20 4f 53 5f 4f 53 32 20 30 0a 23  efine OS_OS2 0.#
4e290 20 65 6e 64 69 66 0a 23 65 6c 73 65 0a 23 20 69   endif.#else.# i
4e2a0 66 6e 64 65 66 20 4f 53 5f 57 49 4e 0a 23 20 20  fndef OS_WIN.#  
4e2b0 64 65 66 69 6e 65 20 4f 53 5f 57 49 4e 20 30 0a  define OS_WIN 0.
4e2c0 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a  # endif.#endif..
4e2d0 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65 20 74  ../*.** Define t
4e2e0 68 65 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65 20  he maximum size 
4e2f0 6f 66 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66  of a temporary f
4e300 69 6c 65 6e 61 6d 65 0a 2a 2f 0a 23 69 66 20 4f  ilename.*/.#if O
4e310 53 5f 57 49 4e 0a 23 20 69 6e 63 6c 75 64 65 20  S_WIN.# include 
4e320 3c 77 69 6e 64 6f 77 73 2e 68 3e 0a 23 20 64 65  <windows.h>.# de
4e330 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50  fine SQLITE_TEMP
4e340 4e 41 4d 45 5f 53 49 5a 45 20 28 4d 41 58 5f 50  NAME_SIZE (MAX_P
4e350 41 54 48 2b 35 30 29 0a 23 65 6c 69 66 20 4f 53  ATH+50).#elif OS
4e360 5f 4f 53 32 0a 23 20 69 66 20 28 5f 5f 47 4e 55  _OS2.# if (__GNU
4e370 43 5f 5f 20 3e 20 33 20 7c 7c 20 5f 5f 47 4e 55  C__ > 3 || __GNU
4e380 43 5f 5f 20 3d 3d 20 33 20 26 26 20 5f 5f 47 4e  C__ == 3 && __GN
4e390 55 43 5f 4d 49 4e 4f 52 5f 5f 20 3e 3d 20 33 29  UC_MINOR__ >= 3)
4e3a0 20 26 26 20 64 65 66 69 6e 65 64 28 4f 53 32 5f   && defined(OS2_
4e3b0 48 49 47 48 5f 4d 45 4d 4f 52 59 29 0a 23 20 20  HIGH_MEMORY).#  
4e3c0 69 6e 63 6c 75 64 65 20 3c 6f 73 32 73 61 66 65  include <os2safe
4e3d0 2e 68 3e 20 2f 2a 20 68 61 73 20 74 6f 20 62 65  .h> /* has to be
4e3e0 20 69 6e 63 6c 75 64 65 64 20 62 65 66 6f 72 65   included before
4e3f0 20 6f 73 32 2e 68 20 66 6f 72 20 6c 69 6e 6b 69   os2.h for linki
4e400 6e 67 20 74 6f 20 77 6f 72 6b 20 2a 2f 0a 23 20  ng to work */.# 
4e410 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 20 49  endif.# define I
4e420 4e 43 4c 5f 44 4f 53 44 41 54 45 54 49 4d 45 0a  NCL_DOSDATETIME.
4e430 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f  # define INCL_DO
4e440 53 46 49 4c 45 4d 47 52 0a 23 20 64 65 66 69 6e  SFILEMGR.# defin
4e450 65 20 49 4e 43 4c 5f 44 4f 53 45 52 52 4f 52 53  e INCL_DOSERRORS
4e460 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44  .# define INCL_D
4e470 4f 53 4d 49 53 43 0a 23 20 64 65 66 69 6e 65 20  OSMISC.# define 
4e480 49 4e 43 4c 5f 44 4f 53 50 52 4f 43 45 53 53 0a  INCL_DOSPROCESS.
4e490 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f  # define INCL_DO
4e4a0 53 4d 4f 44 55 4c 45 4d 47 52 0a 23 20 64 65 66  SMODULEMGR.# def
4e4b0 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 53 45 4d 41  ine INCL_DOSSEMA
4e4c0 50 48 4f 52 45 53 0a 23 20 69 6e 63 6c 75 64 65  PHORES.# include
4e4d0 20 3c 6f 73 32 2e 68 3e 0a 23 20 69 6e 63 6c 75   <os2.h>.# inclu
4e4e0 64 65 20 3c 75 63 6f 6e 76 2e 68 3e 0a 23 20 64  de <uconv.h>.# d
4e4f0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d  efine SQLITE_TEM
4e500 50 4e 41 4d 45 5f 53 49 5a 45 20 28 43 43 48 4d  PNAME_SIZE (CCHM
4e510 41 58 50 41 54 48 43 4f 4d 50 29 0a 23 65 6c 73  AXPATHCOMP).#els
4e520 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  e.# define SQLIT
4e530 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20  E_TEMPNAME_SIZE 
4e540 32 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 49  200.#endif../* I
4e550 66 20 74 68 65 20 53 45 54 5f 46 55 4c 4c 53 59  f the SET_FULLSY
4e560 4e 43 20 6d 61 63 72 6f 20 69 73 20 6e 6f 74 20  NC macro is not 
4e570 64 65 66 69 6e 65 64 20 61 62 6f 76 65 2c 20 74  defined above, t
4e580 68 65 6e 20 6d 61 6b 65 20 69 74 0a 2a 2a 20 61  hen make it.** a
4e590 20 6e 6f 2d 6f 70 0a 2a 2f 0a 23 69 66 6e 64 65   no-op.*/.#ifnde
4e5a0 66 20 53 45 54 5f 46 55 4c 4c 53 59 4e 43 0a 23  f SET_FULLSYNC.#
4e5b0 20 64 65 66 69 6e 65 20 53 45 54 5f 46 55 4c 4c   define SET_FULL
4e5c0 53 59 4e 43 28 78 2c 79 29 0a 23 65 6e 64 69 66  SYNC(x,y).#endif
4e5d0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61  ../*.** The defa
4e5e0 75 6c 74 20 73 69 7a 65 20 6f 66 20 61 20 64 69  ult size of a di
4e5f0 73 6b 20 73 65 63 74 6f 72 0a 2a 2f 0a 23 69 66  sk sector.*/.#if
4e600 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41  ndef SQLITE_DEFA
4e610 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 0a  ULT_SECTOR_SIZE.
4e620 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
4e630 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53  DEFAULT_SECTOR_S
4e640 49 5a 45 20 35 31 32 0a 23 65 6e 64 69 66 0a 0a  IZE 512.#endif..
4e650 2f 2a 0a 2a 2a 20 54 65 6d 70 6f 72 61 72 79 20  /*.** Temporary 
4e660 66 69 6c 65 73 20 61 72 65 20 6e 61 6d 65 64 20  files are named 
4e670 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 74 68  starting with th
4e680 69 73 20 70 72 65 66 69 78 20 66 6f 6c 6c 6f 77  is prefix follow
4e690 65 64 20 62 79 20 31 36 20 72 61 6e 64 6f 6d 0a  ed by 16 random.
4e6a0 2a 2a 20 61 6c 70 68 61 6e 75 6d 65 72 69 63 20  ** alphanumeric 
4e6b0 63 68 61 72 61 63 74 65 72 73 2c 20 61 6e 64 20  characters, and 
4e6c0 6e 6f 20 66 69 6c 65 20 65 78 74 65 6e 73 69 6f  no file extensio
4e6d0 6e 2e 20 54 68 65 79 20 61 72 65 20 73 74 6f 72  n. They are stor
4e6e0 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 4f 53 27  ed in the.** OS'
4e6f0 73 20 73 74 61 6e 64 61 72 64 20 74 65 6d 70 6f  s standard tempo
4e700 72 61 72 79 20 66 69 6c 65 20 64 69 72 65 63 74  rary file direct
4e710 6f 72 79 2c 20 61 6e 64 20 61 72 65 20 64 65 6c  ory, and are del
4e720 65 74 65 64 20 70 72 69 6f 72 20 74 6f 20 65 78  eted prior to ex
4e730 69 74 2e 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65  it..** If sqlite
4e740 20 69 73 20 62 65 69 6e 67 20 65 6d 62 65 64 64   is being embedd
4e750 65 64 20 69 6e 20 61 6e 6f 74 68 65 72 20 70 72  ed in another pr
4e760 6f 67 72 61 6d 2c 20 79 6f 75 20 6d 61 79 20 77  ogram, you may w
4e770 69 73 68 20 74 6f 20 63 68 61 6e 67 65 20 74 68  ish to change th
4e780 65 0a 2a 2a 20 70 72 65 66 69 78 20 74 6f 20 72  e.** prefix to r
4e790 65 66 6c 65 63 74 20 79 6f 75 72 20 70 72 6f 67  eflect your prog
4e7a0 72 61 6d 27 73 20 6e 61 6d 65 2c 20 73 6f 20 74  ram's name, so t
4e7b0 68 61 74 20 69 66 20 79 6f 75 72 20 70 72 6f 67  hat if your prog
4e7c0 72 61 6d 20 65 78 69 74 73 0a 2a 2a 20 70 72 65  ram exits.** pre
4e7d0 6d 61 74 75 72 65 6c 79 2c 20 6f 6c 64 20 74 65  maturely, old te
4e7e0 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 63 61  mporary files ca
4e7f0 6e 20 62 65 20 65 61 73 69 6c 79 20 69 64 65 6e  n be easily iden
4e800 74 69 66 69 65 64 2e 20 54 68 69 73 20 63 61 6e  tified. This can
4e810 20 62 65 20 64 6f 6e 65 0a 2a 2a 20 75 73 69 6e   be done.** usin
4e820 67 20 2d 44 53 51 4c 49 54 45 5f 54 45 4d 50 5f  g -DSQLITE_TEMP_
4e830 46 49 4c 45 5f 50 52 45 46 49 58 3d 6d 79 70 72  FILE_PREFIX=mypr
4e840 65 66 69 78 5f 20 6f 6e 20 74 68 65 20 63 6f 6d  efix_ on the com
4e850 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 6c 69  piler command li
4e860 6e 65 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 36 2d 31  ne..**.** 2006-1
4e870 30 2d 33 31 3a 20 20 54 68 65 20 64 65 66 61 75  0-31:  The defau
4e880 6c 74 20 70 72 65 66 69 78 20 75 73 65 64 20 74  lt prefix used t
4e890 6f 20 62 65 20 22 73 71 6c 69 74 65 5f 22 2e 20  o be "sqlite_". 
4e8a0 20 42 75 74 20 74 68 65 6e 0a 2a 2a 20 4d 63 61   But then.** Mca
4e8b0 66 65 65 20 73 74 61 72 74 65 64 20 75 73 69 6e  fee started usin
4e8c0 67 20 53 51 4c 69 74 65 20 69 6e 20 74 68 65 69  g SQLite in thei
4e8d0 72 20 61 6e 74 69 2d 76 69 72 75 73 20 70 72 6f  r anti-virus pro
4e8e0 64 75 63 74 20 61 6e 64 20 69 74 0a 2a 2a 20 73  duct and it.** s
4e8f0 74 61 72 74 65 64 20 70 75 74 74 69 6e 67 20 66  tarted putting f
4e900 69 6c 65 73 20 77 69 74 68 20 74 68 65 20 22 73  iles with the "s
4e910 71 6c 69 74 65 22 20 6e 61 6d 65 20 69 6e 20 74  qlite" name in t
4e920 68 65 20 63 3a 2f 74 65 6d 70 20 66 6f 6c 64 65  he c:/temp folde
4e930 72 2e 0a 2a 2a 20 54 68 69 73 20 61 6e 6e 6f 79  r..** This annoy
4e940 65 64 20 6d 61 6e 79 20 77 69 6e 64 6f 77 73 20  ed many windows 
4e950 75 73 65 72 73 2e 20 20 54 68 6f 73 65 20 75 73  users.  Those us
4e960 65 72 73 20 77 6f 75 6c 64 20 74 68 65 6e 20 64  ers would then d
4e970 6f 20 61 20 0a 2a 2a 20 47 6f 6f 67 6c 65 20 73  o a .** Google s
4e980 65 61 72 63 68 20 66 6f 72 20 22 73 71 6c 69 74  earch for "sqlit
4e990 65 22 2c 20 66 69 6e 64 20 74 68 65 20 74 65 6c  e", find the tel
4e9a0 65 70 68 6f 6e 65 20 6e 75 6d 62 65 72 73 20 6f  ephone numbers o
4e9b0 66 20 74 68 65 0a 2a 2a 20 64 65 76 65 6c 6f 70  f the.** develop
4e9c0 65 72 73 20 61 6e 64 20 63 61 6c 6c 20 74 6f 20  ers and call to 
4e9d0 77 61 6b 65 20 74 68 65 6d 20 75 70 20 61 74 20  wake them up at 
4e9e0 6e 69 67 68 74 20 61 6e 64 20 63 6f 6d 70 6c 61  night and compla
4e9f0 69 6e 2e 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20  in..** For this 
4ea00 72 65 61 73 6f 6e 2c 20 74 68 65 20 64 65 66 61  reason, the defa
4ea10 75 6c 74 20 6e 61 6d 65 20 70 72 65 66 69 78 20  ult name prefix 
4ea20 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20 62 65  is changed to be
4ea30 20 22 73 71 6c 69 74 65 22 20 0a 2a 2a 20 73 70   "sqlite" .** sp
4ea40 65 6c 6c 65 64 20 62 61 63 6b 77 61 72 64 73 2e  elled backwards.
4ea50 20 20 53 6f 20 74 68 65 20 74 65 6d 70 20 66 69    So the temp fi
4ea60 6c 65 73 20 61 72 65 20 73 74 69 6c 6c 20 69 64  les are still id
4ea70 65 6e 74 69 66 69 65 64 2c 20 62 75 74 0a 2a 2a  entified, but.**
4ea80 20 61 6e 79 62 6f 64 79 20 73 6d 61 72 74 20 65   anybody smart e
4ea90 6e 6f 75 67 68 20 74 6f 20 66 69 67 75 72 65 20  nough to figure 
4eaa0 6f 75 74 20 74 68 65 20 63 6f 64 65 20 69 73 20  out the code is 
4eab0 61 6c 73 6f 20 6c 69 6b 65 6c 79 20 73 6d 61 72  also likely smar
4eac0 74 0a 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20 6b  t.** enough to k
4ead0 6e 6f 77 20 74 68 61 74 20 63 61 6c 6c 69 6e 67  now that calling
4eae0 20 74 68 65 20 64 65 76 65 6c 6f 70 65 72 20 77   the developer w
4eaf0 69 6c 6c 20 6e 6f 74 20 68 65 6c 70 20 67 65 74  ill not help get
4eb00 20 72 69 64 0a 2a 2a 20 6f 66 20 74 68 65 20 66   rid.** of the f
4eb10 69 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ile..*/.#ifndef 
4eb20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45  SQLITE_TEMP_FILE
4eb30 5f 50 52 45 46 49 58 0a 23 20 64 65 66 69 6e 65  _PREFIX.# define
4eb40 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c   SQLITE_TEMP_FIL
4eb50 45 5f 50 52 45 46 49 58 20 22 65 74 69 6c 71 73  E_PREFIX "etilqs
4eb60 5f 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  _".#endif../*.**
4eb70 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76   The following v
4eb80 61 6c 75 65 73 20 6d 61 79 20 62 65 20 70 61 73  alues may be pas
4eb90 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  sed as the secon
4eba0 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a  d argument to.**
4ebb0 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29   sqlite3OsLock()
4ebc0 2e 20 54 68 65 20 76 61 72 69 6f 75 73 20 6c 6f  . The various lo
4ebd0 63 6b 73 20 65 78 68 69 62 69 74 20 74 68 65 20  cks exhibit the 
4ebe0 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 6d 61 6e 74  following semant
4ebf0 69 63 73 3a 0a 2a 2a 0a 2a 2a 20 53 48 41 52 45  ics:.**.** SHARE
4ec00 44 3a 20 20 20 20 41 6e 79 20 6e 75 6d 62 65 72  D:    Any number
4ec10 20 6f 66 20 70 72 6f 63 65 73 73 65 73 20 6d 61   of processes ma
4ec20 79 20 68 6f 6c 64 20 61 20 53 48 41 52 45 44 20  y hold a SHARED 
4ec30 6c 6f 63 6b 20 73 69 6d 75 6c 74 61 6e 65 6f 75  lock simultaneou
4ec40 73 6c 79 2e 0a 2a 2a 20 52 45 53 45 52 56 45 44  sly..** RESERVED
4ec50 3a 20 20 41 20 73 69 6e 67 6c 65 20 70 72 6f 63  :  A single proc
4ec60 65 73 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 52  ess may hold a R
4ec70 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20  ESERVED lock on 
4ec80 61 20 66 69 6c 65 20 61 74 0a 2a 2a 20 20 20 20  a file at.**    
4ec90 20 20 20 20 20 20 20 20 61 6e 79 20 74 69 6d 65          any time
4eca0 2e 20 4f 74 68 65 72 20 70 72 6f 63 65 73 73 65  . Other processe
4ecb0 73 20 6d 61 79 20 68 6f 6c 64 20 61 6e 64 20 6f  s may hold and o
4ecc0 62 74 61 69 6e 20 6e 65 77 20 53 48 41 52 45 44  btain new SHARED
4ecd0 20 6c 6f 63 6b 73 2e 0a 2a 2a 20 50 45 4e 44 49   locks..** PENDI
4ece0 4e 47 3a 20 20 20 41 20 73 69 6e 67 6c 65 20 70  NG:   A single p
4ecf0 72 6f 63 65 73 73 20 6d 61 79 20 68 6f 6c 64 20  rocess may hold 
4ed00 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f  a PENDING lock o
4ed10 6e 20 61 20 66 69 6c 65 20 61 74 0a 2a 2a 20 20  n a file at.**  
4ed20 20 20 20 20 20 20 20 20 20 20 61 6e 79 20 6f 6e            any on
4ed30 65 20 74 69 6d 65 2e 20 45 78 69 73 74 69 6e 67  e time. Existing
4ed40 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61   SHARED locks ma
4ed50 79 20 70 65 72 73 69 73 74 2c 20 62 75 74 20 6e  y persist, but n
4ed60 6f 20 6e 65 77 0a 2a 2a 20 20 20 20 20 20 20 20  o new.**        
4ed70 20 20 20 20 53 48 41 52 45 44 20 6c 6f 63 6b 73      SHARED locks
4ed80 20 6d 61 79 20 62 65 20 6f 62 74 61 69 6e 65 64   may be obtained
4ed90 20 62 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73   by other proces
4eda0 73 65 73 2e 0a 2a 2a 20 45 58 43 4c 55 53 49 56  ses..** EXCLUSIV
4edb0 45 3a 20 41 6e 20 45 58 43 4c 55 53 49 56 45 20  E: An EXCLUSIVE 
4edc0 6c 6f 63 6b 20 70 72 65 63 6c 75 64 65 73 20 61  lock precludes a
4edd0 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 73 2e 0a  ll other locks..
4ede0 2a 2a 0a 2a 2a 20 50 45 4e 44 49 4e 47 5f 4c 4f  **.** PENDING_LO
4edf0 43 4b 20 6d 61 79 20 6e 6f 74 20 62 65 20 70 61  CK may not be pa
4ee00 73 73 65 64 20 64 69 72 65 63 74 6c 79 20 74 6f  ssed directly to
4ee10 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29   sqlite3OsLock()
4ee20 2e 20 49 6e 73 74 65 61 64 2c 20 61 0a 2a 2a 20  . Instead, a.** 
4ee30 70 72 6f 63 65 73 73 20 74 68 61 74 20 72 65 71  process that req
4ee40 75 65 73 74 73 20 61 6e 20 45 58 43 4c 55 53 49  uests an EXCLUSI
4ee50 56 45 20 6c 6f 63 6b 20 6d 61 79 20 61 63 74 75  VE lock may actu
4ee60 61 6c 6c 79 20 6f 62 74 61 69 6e 20 61 20 50 45  ally obtain a PE
4ee70 4e 44 49 4e 47 0a 2a 2a 20 6c 6f 63 6b 2e 20 54  NDING.** lock. T
4ee80 68 69 73 20 63 61 6e 20 62 65 20 75 70 67 72 61  his can be upgra
4ee90 64 65 64 20 74 6f 20 61 6e 20 45 58 43 4c 55 53  ded to an EXCLUS
4eea0 49 56 45 20 6c 6f 63 6b 20 62 79 20 61 20 73 75  IVE lock by a su
4eeb0 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f  bsequent call to
4eec0 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63  .** sqlite3OsLoc
4eed0 6b 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  k()..*/.#define 
4eee0 4e 4f 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20 20  NO_LOCK         
4eef0 30 0a 23 64 65 66 69 6e 65 20 53 48 41 52 45 44  0.#define SHARED
4ef00 5f 4c 4f 43 4b 20 20 20 20 20 31 0a 23 64 65 66  _LOCK     1.#def
4ef10 69 6e 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43  ine RESERVED_LOC
4ef20 4b 20 20 20 32 0a 23 64 65 66 69 6e 65 20 50 45  K   2.#define PE
4ef30 4e 44 49 4e 47 5f 4c 4f 43 4b 20 20 20 20 33 0a  NDING_LOCK    3.
4ef40 23 64 65 66 69 6e 65 20 45 58 43 4c 55 53 49 56  #define EXCLUSIV
4ef50 45 5f 4c 4f 43 4b 20 20 34 0a 0a 2f 2a 0a 2a 2a  E_LOCK  4../*.**
4ef60 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 20 4e 6f   File Locking No
4ef70 74 65 73 3a 20 20 28 4d 6f 73 74 6c 79 20 61 62  tes:  (Mostly ab
4ef80 6f 75 74 20 77 69 6e 64 6f 77 73 20 62 75 74 20  out windows but 
4ef90 61 6c 73 6f 20 73 6f 6d 65 20 69 6e 66 6f 20 66  also some info f
4efa0 6f 72 20 55 6e 69 78 29 0a 2a 2a 0a 2a 2a 20 57  or Unix).**.** W
4efb0 65 20 63 61 6e 6e 6f 74 20 75 73 65 20 4c 6f 63  e cannot use Loc
4efc0 6b 46 69 6c 65 45 78 28 29 20 6f 72 20 55 6e 6c  kFileEx() or Unl
4efd0 6f 63 6b 46 69 6c 65 45 78 28 29 20 6f 6e 20 57  ockFileEx() on W
4efe0 69 6e 39 35 2f 39 38 2f 4d 45 20 62 65 63 61 75  in95/98/ME becau
4eff0 73 65 0a 2a 2a 20 74 68 6f 73 65 20 66 75 6e 63  se.** those func
4f000 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 61 76  tions are not av
4f010 61 69 6c 61 62 6c 65 2e 20 20 53 6f 20 77 65 20  ailable.  So we 
4f020 75 73 65 20 6f 6e 6c 79 20 4c 6f 63 6b 46 69 6c  use only LockFil
4f030 65 28 29 20 61 6e 64 0a 2a 2a 20 55 6e 6c 6f 63  e() and.** Unloc
4f040 6b 46 69 6c 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 4c  kFile()..**.** L
4f050 6f 63 6b 46 69 6c 65 28 29 20 70 72 65 76 65 6e  ockFile() preven
4f060 74 73 20 6e 6f 74 20 6a 75 73 74 20 77 72 69 74  ts not just writ
4f070 69 6e 67 20 62 75 74 20 61 6c 73 6f 20 72 65 61  ing but also rea
4f080 64 69 6e 67 20 62 79 20 6f 74 68 65 72 20 70 72  ding by other pr
4f090 6f 63 65 73 73 65 73 2e 0a 2a 2a 20 41 20 53 48  ocesses..** A SH
4f0a0 41 52 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62 74  ARED_LOCK is obt
4f0b0 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67  ained by locking
4f0c0 20 61 20 73 69 6e 67 6c 65 20 72 61 6e 64 6f 6d   a single random
4f0d0 6c 79 2d 63 68 6f 73 65 6e 20 0a 2a 2a 20 62 79  ly-chosen .** by
4f0e0 74 65 20 6f 75 74 20 6f 66 20 61 20 73 70 65 63  te out of a spec
4f0f0 69 66 69 63 20 72 61 6e 67 65 20 6f 66 20 62 79  ific range of by
4f100 74 65 73 2e 20 54 68 65 20 6c 6f 63 6b 20 62 79  tes. The lock by
4f110 74 65 20 69 73 20 6f 62 74 61 69 6e 65 64 20 61  te is obtained a
4f120 74 20 0a 2a 2a 20 72 61 6e 64 6f 6d 20 73 6f 20  t .** random so 
4f130 74 77 6f 20 73 65 70 61 72 61 74 65 20 72 65 61  two separate rea
4f140 64 65 72 73 20 63 61 6e 20 70 72 6f 62 61 62 6c  ders can probabl
4f150 79 20 61 63 63 65 73 73 20 74 68 65 20 66 69 6c  y access the fil
4f160 65 20 61 74 20 74 68 65 20 0a 2a 2a 20 73 61 6d  e at the .** sam
4f170 65 20 74 69 6d 65 2c 20 75 6e 6c 65 73 73 20 74  e time, unless t
4f180 68 65 79 20 61 72 65 20 75 6e 6c 75 63 6b 79 20  hey are unlucky 
4f190 61 6e 64 20 63 68 6f 6f 73 65 20 74 68 65 20 73  and choose the s
4f1a0 61 6d 65 20 6c 6f 63 6b 20 62 79 74 65 2e 0a 2a  ame lock byte..*
4f1b0 2a 20 41 6e 20 45 58 43 4c 55 53 49 56 45 5f 4c  * An EXCLUSIVE_L
4f1c0 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20  OCK is obtained 
4f1d0 62 79 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20 62  by locking all b
4f1e0 79 74 65 73 20 69 6e 20 74 68 65 20 72 61 6e 67  ytes in the rang
4f1f0 65 2e 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20  e..** There can 
4f200 6f 6e 6c 79 20 62 65 20 6f 6e 65 20 77 72 69 74  only be one writ
4f210 65 72 2e 20 20 41 20 52 45 53 45 52 56 45 44 5f  er.  A RESERVED_
4f220 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64  LOCK is obtained
4f230 20 62 79 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 61   by locking.** a
4f240 20 73 69 6e 67 6c 65 20 62 79 74 65 20 6f 66 20   single byte of 
4f250 74 68 65 20 66 69 6c 65 20 74 68 61 74 20 69 73  the file that is
4f260 20 64 65 73 69 67 6e 61 74 65 64 20 61 73 20 74   designated as t
4f270 68 65 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b  he reserved lock
4f280 20 62 79 74 65 2e 0a 2a 2a 20 41 20 50 45 4e 44   byte..** A PEND
4f290 49 4e 47 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61  ING_LOCK is obta
4f2a0 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20  ined by locking 
4f2b0 61 20 64 65 73 69 67 6e 61 74 65 64 20 62 79 74  a designated byt
4f2c0 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  e different from
4f2d0 0a 2a 2a 20 74 68 65 20 52 45 53 45 52 56 45 44  .** the RESERVED
4f2e0 5f 4c 4f 43 4b 20 62 79 74 65 2e 0a 2a 2a 0a 2a  _LOCK byte..**.*
4f2f0 2a 20 4f 6e 20 57 69 6e 4e 54 2f 32 4b 2f 58 50  * On WinNT/2K/XP
4f300 20 73 79 73 74 65 6d 73 2c 20 4c 6f 63 6b 46 69   systems, LockFi
4f310 6c 65 45 78 28 29 20 61 6e 64 20 55 6e 6c 6f 63  leEx() and Unloc
4f320 6b 46 69 6c 65 45 78 28 29 20 61 72 65 20 61 76  kFileEx() are av
4f330 61 69 6c 61 62 6c 65 2c 0a 2a 2a 20 77 68 69 63  ailable,.** whic
4f340 68 20 6d 65 61 6e 73 20 77 65 20 63 61 6e 20 75  h means we can u
4f350 73 65 20 72 65 61 64 65 72 2f 77 72 69 74 65 72  se reader/writer
4f360 20 6c 6f 63 6b 73 2e 20 20 57 68 65 6e 20 72 65   locks.  When re
4f370 61 64 65 72 2f 77 72 69 74 65 72 20 6c 6f 63 6b  ader/writer lock
4f380 73 0a 2a 2a 20 61 72 65 20 75 73 65 64 2c 20 74  s.** are used, t
4f390 68 65 20 6c 6f 63 6b 20 69 73 20 70 6c 61 63 65  he lock is place
4f3a0 64 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 72 61  d on the same ra
4f3b0 6e 67 65 20 6f 66 20 62 79 74 65 73 20 74 68 61  nge of bytes tha
4f3c0 74 20 69 73 20 75 73 65 64 0a 2a 2a 20 66 6f 72  t is used.** for
4f3d0 20 70 72 6f 62 61 62 69 6c 69 73 74 69 63 20 6c   probabilistic l
4f3e0 6f 63 6b 69 6e 67 20 69 6e 20 57 69 6e 39 35 2f  ocking in Win95/
4f3f0 39 38 2f 4d 45 2e 20 20 48 65 6e 63 65 2c 20 74  98/ME.  Hence, t
4f400 68 65 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d  he locking schem
4f410 65 0a 2a 2a 20 77 69 6c 6c 20 73 75 70 70 6f 72  e.** will suppor
4f420 74 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 57 69  t two or more Wi
4f430 6e 39 35 20 72 65 61 64 65 72 73 20 6f 72 20 74  n95 readers or t
4f440 77 6f 20 6f 72 20 6d 6f 72 65 20 57 69 6e 4e 54  wo or more WinNT
4f450 20 72 65 61 64 65 72 73 2e 0a 2a 2a 20 42 75 74   readers..** But
4f460 20 61 20 73 69 6e 67 6c 65 20 57 69 6e 39 35 20   a single Win95 
4f470 72 65 61 64 65 72 20 77 69 6c 6c 20 6c 6f 63 6b  reader will lock
4f480 20 6f 75 74 20 61 6c 6c 20 57 69 6e 4e 54 20 72   out all WinNT r
4f490 65 61 64 65 72 73 20 61 6e 64 20 61 20 73 69 6e  eaders and a sin
4f4a0 67 6c 65 0a 2a 2a 20 57 69 6e 4e 54 20 72 65 61  gle.** WinNT rea
4f4b0 64 65 72 20 77 69 6c 6c 20 6c 6f 63 6b 20 6f 75  der will lock ou
4f4c0 74 20 61 6c 6c 20 6f 74 68 65 72 20 57 69 6e 39  t all other Win9
4f4d0 35 20 72 65 61 64 65 72 73 2e 0a 2a 2a 0a 2a 2a  5 readers..**.**
4f4e0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 23   The following #
4f4f0 64 65 66 69 6e 65 73 20 73 70 65 63 69 66 79 20  defines specify 
4f500 74 68 65 20 72 61 6e 67 65 20 6f 66 20 62 79 74  the range of byt
4f510 65 73 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b  es used for lock
4f520 69 6e 67 2e 0a 2a 2a 20 53 48 41 52 45 44 5f 53  ing..** SHARED_S
4f530 49 5a 45 20 69 73 20 74 68 65 20 6e 75 6d 62 65  IZE is the numbe
4f540 72 20 6f 66 20 62 79 74 65 73 20 61 76 61 69 6c  r of bytes avail
4f550 61 62 6c 65 20 69 6e 20 74 68 65 20 70 6f 6f 6c  able in the pool
4f560 20 66 72 6f 6d 20 77 68 69 63 68 0a 2a 2a 20 61   from which.** a
4f570 20 72 61 6e 64 6f 6d 20 62 79 74 65 20 69 73 20   random byte is 
4f580 73 65 6c 65 63 74 65 64 20 66 6f 72 20 61 20 73  selected for a s
4f590 68 61 72 65 64 20 6c 6f 63 6b 2e 20 20 54 68 65  hared lock.  The
4f5a0 20 70 6f 6f 6c 20 6f 66 20 62 79 74 65 73 20 66   pool of bytes f
4f5b0 6f 72 0a 2a 2a 20 73 68 61 72 65 64 20 6c 6f 63  or.** shared loc
4f5c0 6b 73 20 62 65 67 69 6e 73 20 61 74 20 53 48 41  ks begins at SHA
4f5d0 52 45 44 5f 46 49 52 53 54 2e 20 0a 2a 2a 0a 2a  RED_FIRST. .**.*
4f5e0 2a 20 54 68 65 73 65 20 23 64 65 66 69 6e 65 73  * These #defines
4f5f0 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 69   are available i
4f600 6e 20 73 71 6c 69 74 65 5f 61 75 78 2e 68 20 73  n sqlite_aux.h s
4f610 6f 20 74 68 61 74 20 61 64 61 70 74 6f 72 73 20  o that adaptors 
4f620 66 6f 72 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6e  for.** connectin
4f630 67 20 53 51 4c 69 74 65 20 74 6f 20 6f 74 68 65  g SQLite to othe
4f640 72 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  r operating syst
4f650 65 6d 73 20 63 61 6e 20 75 73 65 20 74 68 65 20  ems can use the 
4f660 73 61 6d 65 20 62 79 74 65 0a 2a 2a 20 72 61 6e  same byte.** ran
4f670 67 65 73 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e  ges for locking.
4f680 20 20 49 6e 20 70 61 72 74 69 63 75 6c 61 72 2c    In particular,
4f690 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69 6e   the same lockin
4f6a0 67 20 73 74 72 61 74 65 67 79 20 61 6e 64 0a 2a  g strategy and.*
4f6b0 2a 20 62 79 74 65 20 72 61 6e 67 65 73 20 61 72  * byte ranges ar
4f6c0 65 20 75 73 65 64 20 66 6f 72 20 55 6e 69 78 2e  e used for Unix.
4f6d0 20 20 54 68 69 73 20 6c 65 61 76 65 73 20 6f 70    This leaves op
4f6e0 65 6e 20 74 68 65 20 70 6f 73 73 69 62 6c 69 74  en the possiblit
4f6f0 79 20 6f 66 20 68 61 76 69 6e 67 0a 2a 2a 20 63  y of having.** c
4f700 6c 69 65 6e 74 73 20 6f 6e 20 77 69 6e 39 35 2c  lients on win95,
4f710 20 77 69 6e 4e 54 2c 20 61 6e 64 20 75 6e 69 78   winNT, and unix
4f720 20 61 6c 6c 20 74 61 6c 6b 69 6e 67 20 74 6f 20   all talking to 
4f730 74 68 65 20 73 61 6d 65 20 73 68 61 72 65 64 20  the same shared 
4f740 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 61 6c 6c 20  file.** and all 
4f750 6c 6f 63 6b 69 6e 67 20 63 6f 72 72 65 63 74 6c  locking correctl
4f760 79 2e 20 20 54 6f 20 64 6f 20 73 6f 20 77 6f 75  y.  To do so wou
4f770 6c 64 20 72 65 71 75 69 72 65 20 74 68 61 74 20  ld require that 
4f780 73 61 6d 62 61 20 28 6f 72 20 77 68 61 74 65 76  samba (or whatev
4f790 65 72 0a 2a 2a 20 74 6f 6f 6c 20 69 73 20 62 65  er.** tool is be
4f7a0 69 6e 67 20 75 73 65 64 20 66 6f 72 20 66 69 6c  ing used for fil
4f7b0 65 20 73 68 61 72 69 6e 67 29 20 69 6d 70 6c 65  e sharing) imple
4f7c0 6d 65 6e 74 73 20 6c 6f 63 6b 73 20 63 6f 72 72  ments locks corr
4f7d0 65 63 74 6c 79 20 62 65 74 77 65 65 6e 0a 2a 2a  ectly between.**
4f7e0 20 77 69 6e 64 6f 77 73 20 61 6e 64 20 75 6e 69   windows and uni
4f7f0 78 2e 20 20 49 27 6d 20 67 75 65 73 73 69 6e 67  x.  I'm guessing
4f800 20 74 68 61 74 20 69 73 6e 27 74 20 6c 69 6b 65   that isn't like
4f810 6c 79 20 74 6f 20 68 61 70 70 65 6e 2c 20 62 75  ly to happen, bu
4f820 74 20 62 79 0a 2a 2a 20 75 73 69 6e 67 20 74 68  t by.** using th
4f830 65 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 72  e same locking r
4f840 61 6e 67 65 20 77 65 20 61 72 65 20 61 74 20 6c  ange we are at l
4f850 65 61 73 74 20 6f 70 65 6e 20 74 6f 20 74 68 65  east open to the
4f860 20 70 6f 73 73 69 62 69 6c 69 74 79 2e 0a 2a 2a   possibility..**
4f870 0a 2a 2a 20 4c 6f 63 6b 69 6e 67 20 69 6e 20 77  .** Locking in w
4f880 69 6e 64 6f 77 73 20 69 73 20 6d 61 6e 64 69 74  indows is mandit
4f890 6f 72 79 2e 20 20 46 6f 72 20 74 68 69 73 20 72  ory.  For this r
4f8a0 65 61 73 6f 6e 2c 20 77 65 20 63 61 6e 6e 6f 74  eason, we cannot
4f8b0 20 73 74 6f 72 65 0a 2a 2a 20 61 63 74 75 61 6c   store.** actual
4f8c0 20 64 61 74 61 20 69 6e 20 74 68 65 20 62 79 74   data in the byt
4f8d0 65 73 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b  es used for lock
4f8e0 69 6e 67 2e 20 20 54 68 65 20 70 61 67 65 72 20  ing.  The pager 
4f8f0 6e 65 76 65 72 20 61 6c 6c 6f 63 61 74 65 73 0a  never allocates.
4f900 2a 2a 20 74 68 65 20 70 61 67 65 73 20 69 6e 76  ** the pages inv
4f910 6f 6c 76 65 64 20 69 6e 20 6c 6f 63 6b 69 6e 67  olved in locking
4f920 20 74 68 65 72 65 66 6f 72 65 2e 20 20 53 48 41   therefore.  SHA
4f930 52 45 44 5f 53 49 5a 45 20 69 73 20 73 65 6c 65  RED_SIZE is sele
4f940 63 74 65 64 20 73 6f 0a 2a 2a 20 74 68 61 74 20  cted so.** that 
4f950 61 6c 6c 20 6c 6f 63 6b 73 20 77 69 6c 6c 20 66  all locks will f
4f960 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70  it on a single p
4f970 61 67 65 20 65 76 65 6e 20 61 74 20 74 68 65 20  age even at the 
4f980 6d 69 6e 69 6d 75 6d 20 70 61 67 65 20 73 69 7a  minimum page siz
4f990 65 2e 0a 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59  e..** PENDING_BY
4f9a0 54 45 20 64 65 66 69 6e 65 73 20 74 68 65 20 62  TE defines the b
4f9b0 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20  eginning of the 
4f9c0 6c 6f 63 6b 73 2e 20 20 42 79 20 64 65 66 61 75  locks.  By defau
4f9d0 6c 74 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 0a  lt PENDING_BYTE.
4f9e0 2a 2a 20 69 73 20 73 65 74 20 68 69 67 68 20 73  ** is set high s
4f9f0 6f 20 74 68 61 74 20 77 65 20 64 6f 6e 27 74 20  o that we don't 
4fa00 68 61 76 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65  have to allocate
4fa10 20 61 6e 20 75 6e 75 73 65 64 20 70 61 67 65 20   an unused page 
4fa20 65 78 63 65 70 74 0a 2a 2a 20 66 6f 72 20 76 65  except.** for ve
4fa30 72 79 20 6c 61 72 67 65 20 64 61 74 61 62 61 73  ry large databas
4fa40 65 73 2e 20 20 42 75 74 20 6f 6e 65 20 73 68 6f  es.  But one sho
4fa50 75 6c 64 20 74 65 73 74 20 74 68 65 20 70 61 67  uld test the pag
4fa60 65 20 73 6b 69 70 70 69 6e 67 20 6c 6f 67 69 63  e skipping logic
4fa70 20 0a 2a 2a 20 62 79 20 73 65 74 74 69 6e 67 20   .** by setting 
4fa80 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 6c 6f 77  PENDING_BYTE low
4fa90 20 61 6e 64 20 72 75 6e 6e 69 6e 67 20 74 68 65   and running the
4faa0 20 65 6e 74 69 72 65 20 72 65 67 72 65 73 73 69   entire regressi
4fab0 6f 6e 20 73 75 69 74 65 2e 0a 2a 2a 0a 2a 2a 20  on suite..**.** 
4fac0 43 68 61 6e 67 69 6e 67 20 74 68 65 20 76 61 6c  Changing the val
4fad0 75 65 20 6f 66 20 50 45 4e 44 49 4e 47 5f 42 59  ue of PENDING_BY
4fae0 54 45 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20  TE results in a 
4faf0 73 75 62 74 6c 79 20 69 6e 63 6f 6d 70 61 74 69  subtly incompati
4fb00 62 6c 65 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 6d  ble.** file form
4fb10 61 74 2e 20 20 44 65 70 65 6e 64 69 6e 67 20 6f  at.  Depending o
4fb20 6e 20 68 6f 77 20 69 74 20 69 73 20 63 68 61 6e  n how it is chan
4fb30 67 65 64 2c 20 79 6f 75 20 6d 69 67 68 74 20 6e  ged, you might n
4fb40 6f 74 20 6e 6f 74 69 63 65 0a 2a 2a 20 74 68 65  ot notice.** the
4fb50 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c 69 74 79   incompatibility
4fb60 20 72 69 67 68 74 20 61 77 61 79 2c 20 65 76 65   right away, eve
4fb70 6e 20 72 75 6e 6e 69 6e 67 20 61 20 66 75 6c 6c  n running a full
4fb80 20 72 65 67 72 65 73 73 69 6f 6e 20 74 65 73 74   regression test
4fb90 2e 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74  ..** The default
4fba0 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 50 45 4e   location of PEN
4fbb0 44 49 4e 47 5f 42 59 54 45 20 69 73 20 74 68 65  DING_BYTE is the
4fbc0 20 66 69 72 73 74 20 62 79 74 65 20 70 61 73 74   first byte past
4fbd0 20 74 68 65 0a 2a 2a 20 31 47 42 20 62 6f 75 6e   the.** 1GB boun
4fbe0 64 61 72 79 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 6e  dary..**.*/.#ifn
4fbf0 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
4fc00 23 64 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f  #define PENDING_
4fc10 42 59 54 45 20 20 20 20 20 20 30 78 34 30 30 30  BYTE      0x4000
4fc20 30 30 30 30 20 20 2f 2a 20 46 69 72 73 74 20 62  0000  /* First b
4fc30 79 74 65 20 70 61 73 74 20 74 68 65 20 31 47 42  yte past the 1GB
4fc40 20 62 6f 75 6e 64 61 72 79 20 2a 2f 0a 23 65 6c   boundary */.#el
4fc50 73 65 0a 53 51 4c 49 54 45 5f 41 50 49 20 65 78  se.SQLITE_API ex
4fc60 74 65 72 6e 20 75 6e 73 69 67 6e 65 64 20 69 6e  tern unsigned in
4fc70 74 20 73 71 6c 69 74 65 33 5f 70 65 6e 64 69 6e  t sqlite3_pendin
4fc80 67 5f 62 79 74 65 3b 0a 23 64 65 66 69 6e 65 20  g_byte;.#define 
4fc90 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 73 71 6c  PENDING_BYTE sql
4fca0 69 74 65 33 5f 70 65 6e 64 69 6e 67 5f 62 79 74  ite3_pending_byt
4fcb0 65 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e  e.#endif..#defin
4fcc0 65 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 20  e RESERVED_BYTE 
4fcd0 20 20 20 20 28 50 45 4e 44 49 4e 47 5f 42 59 54      (PENDING_BYT
4fce0 45 2b 31 29 0a 23 64 65 66 69 6e 65 20 53 48 41  E+1).#define SHA
4fcf0 52 45 44 5f 46 49 52 53 54 20 20 20 20 20 20 28  RED_FIRST      (
4fd00 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 32 29 0a  PENDING_BYTE+2).
4fd10 23 64 65 66 69 6e 65 20 53 48 41 52 45 44 5f 53  #define SHARED_S
4fd20 49 5a 45 20 20 20 20 20 20 20 35 31 30 0a 0a 2f  IZE       510../
4fd30 2a 20 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20  * .** Functions 
4fd40 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 73 71  for accessing sq
4fd50 6c 69 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f  lite3_file metho
4fd60 64 73 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ds .*/.SQLITE_PR
4fd70 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4fd80 33 4f 73 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  3OsClose(sqlite3
4fd90 5f 66 69 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  _file*);.SQLITE_
4fda0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4fdb0 74 65 33 4f 73 52 65 61 64 28 73 71 6c 69 74 65  te3OsRead(sqlite
4fdc0 33 5f 66 69 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20  3_file*, void*, 
4fdd0 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66  int amt, i64 off
4fde0 73 65 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  set);.SQLITE_PRI
4fdf0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4fe00 4f 73 57 72 69 74 65 28 73 71 6c 69 74 65 33 5f  OsWrite(sqlite3_
4fe10 66 69 6c 65 2a 2c 20 63 6f 6e 73 74 20 76 6f 69  file*, const voi
4fe20 64 2a 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 34  d*, int amt, i64
4fe30 20 6f 66 66 73 65 74 29 3b 0a 53 51 4c 49 54 45   offset);.SQLITE
4fe40 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4fe50 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 73  ite3OsTruncate(s
4fe60 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 36  qlite3_file*, i6
4fe70 34 20 73 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f  4 size);.SQLITE_
4fe80 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4fe90 74 65 33 4f 73 53 79 6e 63 28 73 71 6c 69 74 65  te3OsSync(sqlite
4fea0 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53  3_file*, int);.S
4feb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4fec0 74 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53  t sqlite3OsFileS
4fed0 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ize(sqlite3_file
4fee0 2a 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a  *, i64 *pSize);.
4fef0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4ff00 6e 74 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b  nt sqlite3OsLock
4ff10 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20  (sqlite3_file*, 
4ff20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
4ff30 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4ff40 4f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33  OsUnlock(sqlite3
4ff50 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51  _file*, int);.SQ
4ff60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4ff70 20 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52   sqlite3OsCheckR
4ff80 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69  eservedLock(sqli
4ff90 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 3b 0a 53  te3_file *id);.S
4ffa0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4ffb0 74 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43  t sqlite3OsFileC
4ffc0 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66  ontrol(sqlite3_f
4ffd0 69 6c 65 2a 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b  ile*,int,void*);
4ffe0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4fff0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 65 63  int sqlite3OsSec
50000 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f  torSize(sqlite3_
50010 66 69 6c 65 20 2a 69 64 29 3b 0a 53 51 4c 49 54  file *id);.SQLIT
50020 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
50030 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61  lite3OsDeviceCha
50040 72 61 63 74 65 72 69 73 74 69 63 73 28 73 71 6c  racteristics(sql
50050 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 3b 0a  ite3_file *id);.
50060 0a 2f 2a 20 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e  ./* .** Function
50070 73 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20  s for accessing 
50080 73 71 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 68  sqlite3_vfs meth
50090 6f 64 73 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ods .*/.SQLITE_P
500a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
500b0 65 33 4f 73 4f 70 65 6e 28 73 71 6c 69 74 65 33  e3OsOpen(sqlite3
500c0 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68  _vfs *, const ch
500d0 61 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f 66 69  ar *, sqlite3_fi
500e0 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 29  le*, int, int *)
500f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
50100 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65   int sqlite3OsDe
50110 6c 65 74 65 28 73 71 6c 69 74 65 33 5f 76 66 73  lete(sqlite3_vfs
50120 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   *, const char *
50130 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
50140 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
50150 65 33 4f 73 41 63 63 65 73 73 28 73 71 6c 69 74  e3OsAccess(sqlit
50160 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20  e3_vfs *, const 
50170 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51  char *, int);.SQ
50180 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
50190 20 73 71 6c 69 74 65 33 4f 73 47 65 74 54 65 6d   sqlite3OsGetTem
501a0 70 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 76 66  pname(sqlite3_vf
501b0 73 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a  s *, int, char *
501c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
501d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46  E int sqlite3OsF
501e0 75 6c 6c 50 61 74 68 6e 61 6d 65 28 73 71 6c 69  ullPathname(sqli
501f0 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74  te3_vfs *, const
50200 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 63 68   char *, int, ch
50210 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ar *);.SQLITE_PR
50220 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
50230 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73 71 6c 69  te3OsDlOpen(sqli
50240 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74  te3_vfs *, const
50250 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45   char *);.SQLITE
50260 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
50270 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 28 73  lite3OsDlError(s
50280 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e  qlite3_vfs *, in
50290 74 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49  t, char *);.SQLI
502a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
502b0 2a 73 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28  *sqlite3OsDlSym(
502c0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 76  sqlite3_vfs *, v
502d0 6f 69 64 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61  oid *, const cha
502e0 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  r *);.SQLITE_PRI
502f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
50300 33 4f 73 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74  3OsDlClose(sqlit
50310 65 33 5f 76 66 73 20 2a 2c 20 76 6f 69 64 20 2a  e3_vfs *, void *
50320 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
50330 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52  E int sqlite3OsR
50340 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65  andomness(sqlite
50350 33 5f 76 66 73 20 2a 2c 20 69 6e 74 2c 20 63 68  3_vfs *, int, ch
50360 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ar *);.SQLITE_PR
50370 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
50380 33 4f 73 53 6c 65 65 70 28 73 71 6c 69 74 65 33  3OsSleep(sqlite3
50390 5f 76 66 73 20 2a 2c 20 69 6e 74 29 3b 0a 53 51  _vfs *, int);.SQ
503a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
503b0 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e   sqlite3OsCurren
503c0 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66  tTime(sqlite3_vf
503d0 73 20 2a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a 0a  s *, double*);..
503e0 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 6e 69 65 6e 63  /*.** Convenienc
503f0 65 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20  e functions for 
50400 6f 70 65 6e 69 6e 67 20 61 6e 64 20 63 6c 6f 73  opening and clos
50410 69 6e 67 20 66 69 6c 65 73 20 75 73 69 6e 67 20  ing files using 
50420 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  .** sqlite3_mall
50430 6f 63 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 73  oc() to obtain s
50440 70 61 63 65 20 66 6f 72 20 74 68 65 20 66 69 6c  pace for the fil
50450 65 2d 68 61 6e 64 6c 65 20 73 74 72 75 63 74 75  e-handle structu
50460 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  re..*/.SQLITE_PR
50470 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
50480 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 73 71  3OsOpenMalloc(sq
50490 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e  lite3_vfs *, con
504a0 73 74 20 63 68 61 72 20 2a 2c 20 73 71 6c 69 74  st char *, sqlit
504b0 65 33 5f 66 69 6c 65 20 2a 2a 2c 20 69 6e 74 2c  e3_file **, int,
504c0 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  int*);.SQLITE_PR
504d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
504e0 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 73 71 6c  3OsCloseFree(sql
504f0 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a 2f  ite3_file *);../
50500 2a 0a 2a 2a 20 45 61 63 68 20 4f 53 2d 73 70 65  *.** Each OS-spe
50510 63 69 66 69 63 20 62 61 63 6b 65 6e 64 20 64 65  cific backend de
50520 66 69 6e 65 73 20 61 6e 20 69 6e 73 74 61 6e 63  fines an instanc
50530 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
50540 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20  ng.** structure 
50550 66 6f 72 20 72 65 74 75 72 6e 69 6e 67 20 61 20  for returning a 
50560 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73 20 73  pointer to its s
50570 71 6c 69 74 65 33 5f 76 66 73 2e 20 20 49 66 20  qlite3_vfs.  If 
50580 4f 53 5f 4f 54 48 45 52 0a 2a 2a 20 69 73 20 64  OS_OTHER.** is d
50590 65 66 69 6e 65 64 20 28 6d 65 61 6e 69 6e 67 20  efined (meaning 
505a0 74 68 61 74 20 74 68 65 20 61 70 70 6c 69 63 61  that the applica
505b0 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 4f 53 20  tion-defined OS 
505c0 69 6e 74 65 72 66 61 63 65 20 6c 61 79 65 72 0a  interface layer.
505d0 2a 2a 20 69 73 20 75 73 65 64 29 20 74 68 65 6e  ** is used) then
505e0 20 74 68 65 72 65 20 69 73 20 6e 6f 20 64 65 66   there is no def
505f0 61 75 6c 74 20 56 46 53 2e 20 20 20 54 68 65 20  ault VFS.   The 
50600 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74  application must
50610 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 6f 6e 65  .** register one
50620 20 6f 72 20 6d 6f 72 65 20 56 46 53 20 73 74 72   or more VFS str
50630 75 63 74 75 72 65 73 20 75 73 69 6e 67 20 73 71  uctures using sq
50640 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74  lite3_vfs_regist
50650 65 72 28 29 0a 2a 2a 20 62 65 66 6f 72 65 20 61  er().** before a
50660 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 75 73 65  ttempting to use
50670 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49   SQLite..*/.SQLI
50680 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74  TE_PRIVATE sqlit
50690 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 4f  e3_vfs *sqlite3O
506a0 73 44 65 66 61 75 6c 74 56 66 73 28 76 6f 69 64  sDefaultVfs(void
506b0 29 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53  );..#endif /* _S
506c0 51 4c 49 54 45 5f 4f 53 5f 48 5f 20 2a 2f 0a 0a  QLITE_OS_H_ */..
506d0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
506e0 45 6e 64 20 6f 66 20 6f 73 2e 68 20 2a 2a 2a 2a  End of os.h ****
506f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
50720 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
50730 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
50740 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
50750 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
50760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
50770 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
50780 49 6e 63 6c 75 64 65 20 6d 75 74 65 78 2e 68 20  Include mutex.h 
50790 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
507a0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
507b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
507c0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
507d0 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78  Begin file mutex
507e0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
507f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
50810 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73  /*.** 2007 Augus
50820 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  t 28.**.** The a
50830 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
50840 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
50850 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
50860 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
50870 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
50880 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
50890 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
508a0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
508b0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
508c0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
508d0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
508e0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
508f0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
50900 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
50910 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
50920 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
50930 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
50940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50980 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ***.**.** This f
50990 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ile contains the
509a0 20 63 6f 6d 6d 6f 6e 20 68 65 61 64 65 72 20 66   common header f
509b0 6f 72 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70  or all mutex imp
509c0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a  lementations..**
509d0 20 54 68 65 20 73 71 6c 69 74 65 49 6e 74 2e 68   The sqliteInt.h
509e0 20 68 65 61 64 65 72 20 23 69 6e 63 6c 75 64 65   header #include
509f0 73 20 74 68 69 73 20 66 69 6c 65 20 73 6f 20 74  s this file so t
50a00 68 61 74 20 69 74 20 69 73 20 61 76 61 69 6c 61  hat it is availa
50a10 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20 73 6f  ble.** to all so
50a20 75 72 63 65 20 66 69 6c 65 73 2e 20 20 57 65 20  urce files.  We 
50a30 62 72 65 61 6b 20 69 74 20 6f 75 74 20 69 6e 20  break it out in 
50a40 61 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b 65 65  an effort to kee
50a50 70 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 62 65  p the code.** be
50a60 74 74 65 72 20 6f 72 67 61 6e 69 7a 65 64 2e 0a  tter organized..
50a70 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20 73 6f 75  **.** NOTE:  sou
50a80 72 63 65 20 66 69 6c 65 73 20 73 68 6f 75 6c 64  rce files should
50a90 20 2a 6e 6f 74 2a 20 23 69 6e 63 6c 75 64 65 20   *not* #include 
50aa0 74 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65  this header file
50ab0 20 64 69 72 65 63 74 6c 79 2e 0a 2a 2a 20 53 6f   directly..** So
50ac0 75 72 63 65 20 66 69 6c 65 73 20 73 68 6f 75 6c  urce files shoul
50ad0 64 20 23 69 6e 63 6c 75 64 65 20 74 68 65 20 73  d #include the s
50ae0 71 6c 69 74 65 49 6e 74 2e 68 20 66 69 6c 65 20  qliteInt.h file 
50af0 61 6e 64 20 6c 65 74 20 74 68 61 74 20 66 69 6c  and let that fil
50b00 65 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 74 68 69  e.** include thi
50b10 73 20 6f 6e 65 20 69 6e 64 69 72 65 63 74 6c 79  s one indirectly
50b20 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74  ..**.** $Id: mut
50b30 65 78 2e 68 2c 76 20 31 2e 32 20 32 30 30 37 2f  ex.h,v 1.2 2007/
50b40 30 38 2f 33 30 20 31 34 3a 31 30 3a 33 30 20 64  08/30 14:10:30 d
50b50 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 23 69  rh Exp $.*/...#i
50b60 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45  fdef SQLITE_MUTE
50b70 58 5f 41 50 50 44 45 46 0a 2f 2a 0a 2a 2a 20 49  X_APPDEF./*.** I
50b80 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 41  f SQLITE_MUTEX_A
50b90 50 50 44 45 46 20 69 73 20 64 65 66 69 6e 65 64  PPDEF is defined
50ba0 2c 20 74 68 65 6e 20 74 68 69 73 20 77 68 6f 6c  , then this whol
50bb0 65 20 6d 6f 64 75 6c 65 20 69 73 0a 2a 2a 20 6f  e module is.** o
50bc0 6d 69 74 74 65 64 20 61 6e 64 20 65 71 75 69 76  mitted and equiv
50bd0 61 6c 65 6e 74 20 66 75 6e 63 74 69 6f 6e 61 6c  alent functional
50be0 69 74 79 20 6d 75 73 74 20 62 65 20 70 72 6f 76  ity must be prov
50bf0 69 64 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 61  ided by the.** a
50c00 70 70 6c 69 63 61 74 69 6f 6e 20 74 68 61 74 20  pplication that 
50c10 6c 69 6e 6b 73 20 61 67 61 69 6e 73 74 20 74 68  links against th
50c20 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79  e SQLite library
50c30 2e 0a 2a 2f 0a 23 65 6c 73 65 0a 2f 2a 0a 2a 2a  ..*/.#else./*.**
50c40 20 46 69 67 75 72 65 20 6f 75 74 20 77 68 61 74   Figure out what
50c50 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20   version of the 
50c60 63 6f 64 65 20 74 6f 20 75 73 65 2e 20 20 54 68  code to use.  Th
50c70 65 20 63 68 6f 69 63 65 73 20 61 72 65 0a 2a 2a  e choices are.**
50c80 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54  .**   SQLITE_MUT
50c90 45 58 5f 4e 4f 4f 50 20 20 20 20 20 20 20 20 20  EX_NOOP         
50ca0 46 6f 72 20 73 69 6e 67 6c 65 2d 74 68 72 65 61  For single-threa
50cb0 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73  ded applications
50cc0 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20   that.**        
50cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ce0 20 20 20 20 20 64 6f 20 6e 6f 74 20 64 65 73 69       do not desi
50cf0 72 65 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e  re error checkin
50d00 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54  g..**.**   SQLIT
50d10 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 5f 44 45 42  E_MUTEX_NOOP_DEB
50d20 55 47 20 20 20 46 6f 72 20 73 69 6e 67 6c 65 2d  UG   For single-
50d30 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61  threaded applica
50d40 74 69 6f 6e 73 20 77 69 74 68 0a 2a 2a 20 20 20  tions with.**   
50d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d60 20 20 20 20 20 20 20 20 20 20 65 72 72 6f 72 20            error 
50d70 63 68 65 63 6b 69 6e 67 20 74 6f 20 68 65 6c 70  checking to help
50d80 20 76 65 72 69 66 79 20 74 68 61 74 20 6d 75 74   verify that mut
50d90 65 78 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  exes.**         
50da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50db0 20 20 20 20 61 72 65 20 62 65 69 6e 67 20 75 73      are being us
50dc0 65 64 20 63 6f 72 72 65 63 74 6c 79 20 65 76 65  ed correctly eve
50dd0 6e 20 74 68 6f 75 67 68 20 74 68 65 79 0a 2a 2a  n though they.**
50de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50df0 20 20 20 20 20 20 20 20 20 20 20 20 20 61 72 65               are
50e00 20 6e 6f 74 20 6e 65 65 64 65 64 2e 20 20 55 73   not needed.  Us
50e10 65 64 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 44  ed when SQLITE_D
50e20 45 42 55 47 20 69 73 0a 2a 2a 20 20 20 20 20 20  EBUG is.**      
50e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e40 20 20 20 20 20 20 20 64 65 66 69 6e 65 64 20 6f         defined o
50e50 6e 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65  n single-threade
50e60 64 20 62 75 69 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20  d builds..**.** 
50e70 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50    SQLITE_MUTEX_P
50e80 54 48 52 45 41 44 53 20 20 20 20 20 46 6f 72 20  THREADS     For 
50e90 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61  multi-threaded a
50ea0 70 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20 55  pplications on U
50eb0 6e 69 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c  nix..**.**   SQL
50ec0 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 20 20  ITE_MUTEX_W32   
50ed0 20 20 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69         For multi
50ee0 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63  -threaded applic
50ef0 61 74 69 6f 6e 73 20 6f 6e 20 57 69 6e 33 32 2e  ations on Win32.
50f00 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f  .**.**   SQLITE_
50f10 4d 55 54 45 58 5f 4f 53 32 20 20 20 20 20 20 20  MUTEX_OS2       
50f20 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68 72     For multi-thr
50f30 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f  eaded applicatio
50f40 6e 73 20 6f 6e 20 4f 53 2f 32 2e 0a 2a 2f 0a 23  ns on OS/2..*/.#
50f50 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55  define SQLITE_MU
50f60 54 45 58 5f 4e 4f 4f 50 20 31 20 20 20 2f 2a 20  TEX_NOOP 1   /* 
50f70 54 68 65 20 64 65 66 61 75 6c 74 20 2a 2f 0a 23  The default */.#
50f80 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
50f90 45 5f 44 45 42 55 47 29 20 26 26 20 21 53 51 4c  E_DEBUG) && !SQL
50fa0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23  ITE_THREADSAFE.#
50fb0 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 55   undef SQLITE_MU
50fc0 54 45 58 5f 4e 4f 4f 50 0a 23 20 64 65 66 69 6e  TEX_NOOP.# defin
50fd0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e  e SQLITE_MUTEX_N
50fe0 4f 4f 50 5f 44 45 42 55 47 0a 23 65 6e 64 69 66  OOP_DEBUG.#endif
50ff0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
51000 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20  ITE_MUTEX_NOOP) 
51010 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  && SQLITE_THREAD
51020 53 41 46 45 20 26 26 20 4f 53 5f 55 4e 49 58 0a  SAFE && OS_UNIX.
51030 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  # undef SQLITE_M
51040 55 54 45 58 5f 4e 4f 4f 50 0a 23 20 64 65 66 69  UTEX_NOOP.# defi
51050 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ne SQLITE_MUTEX_
51060 50 54 48 52 45 41 44 53 0a 23 65 6e 64 69 66 0a  PTHREADS.#endif.
51070 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
51080 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26  TE_MUTEX_NOOP) &
51090 26 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  & SQLITE_THREADS
510a0 41 46 45 20 26 26 20 4f 53 5f 57 49 4e 0a 23 20  AFE && OS_WIN.# 
510b0 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54  undef SQLITE_MUT
510c0 45 58 5f 4e 4f 4f 50 0a 23 20 64 65 66 69 6e 65  EX_NOOP.# define
510d0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33   SQLITE_MUTEX_W3
510e0 32 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66  2.#endif.#if def
510f0 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45  ined(SQLITE_MUTE
51100 58 5f 4e 4f 4f 50 29 20 26 26 20 53 51 4c 49 54  X_NOOP) && SQLIT
51110 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20  E_THREADSAFE && 
51120 4f 53 5f 4f 53 32 0a 23 20 75 6e 64 65 66 20 53  OS_OS2.# undef S
51130 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50  QLITE_MUTEX_NOOP
51140 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
51150 5f 4d 55 54 45 58 5f 4f 53 32 0a 23 65 6e 64 69  _MUTEX_OS2.#endi
51160 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
51170 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a 2f 2a 0a 2a  _MUTEX_NOOP./*.*
51180 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 6e  * If this is a n
51190 6f 2d 6f 70 20 69 6d 70 6c 65 6d 65 6e 74 61 74  o-op implementat
511a0 69 6f 6e 2c 20 69 6d 70 6c 65 6d 65 6e 74 20 65  ion, implement e
511b0 76 65 72 79 74 68 69 6e 67 20 61 73 20 6d 61 63  verything as mac
511c0 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ros..*/.#define 
511d0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
511e0 6c 6f 63 28 58 29 20 20 20 20 28 28 73 71 6c 69  loc(X)    ((sqli
511f0 74 65 33 5f 6d 75 74 65 78 2a 29 38 29 0a 23 64  te3_mutex*)8).#d
51200 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75  efine sqlite3_mu
51210 74 65 78 5f 66 72 65 65 28 58 29 0a 23 64 65 66  tex_free(X).#def
51220 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ine sqlite3_mute
51230 78 5f 65 6e 74 65 72 28 58 29 0a 23 64 65 66 69  x_enter(X).#defi
51240 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
51250 5f 74 72 79 28 58 29 20 20 20 20 20 20 53 51 4c  _try(X)      SQL
51260 49 54 45 5f 4f 4b 0a 23 64 65 66 69 6e 65 20 73  ITE_OK.#define s
51270 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
51280 76 65 28 58 29 0a 23 64 65 66 69 6e 65 20 73 71  ve(X).#define sq
51290 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
512a0 28 58 29 20 20 20 20 20 31 0a 23 64 65 66 69 6e  (X)     1.#defin
512b0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
512c0 6e 6f 74 68 65 6c 64 28 58 29 20 20 31 0a 23 65  notheld(X)  1.#e
512d0 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  ndif..#endif /* 
512e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 41 50 50  SQLITE_MUTEX_APP
512f0 44 45 46 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  DEF */../*******
51300 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d  ******* End of m
51310 75 74 65 78 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  utex.h *********
51320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51340 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
51350 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
51360 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
51370 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e   off in sqliteIn
51380 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
51390 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20  ******/.../*.** 
513a0 45 61 63 68 20 64 61 74 61 62 61 73 65 20 66 69  Each database fi
513b0 6c 65 20 74 6f 20 62 65 20 61 63 63 65 73 73 65  le to be accesse
513c0 64 20 62 79 20 74 68 65 20 73 79 73 74 65 6d 20  d by the system 
513d0 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a  is an instance.*
513e0 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * of the followi
513f0 6e 67 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  ng structure.  T
51400 68 65 72 65 20 61 72 65 20 6e 6f 72 6d 61 6c 6c  here are normall
51410 79 20 74 77 6f 20 6f 66 20 74 68 65 73 65 20 73  y two of these s
51420 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 69 6e 20  tructures.** in 
51430 74 68 65 20 73 71 6c 69 74 65 2e 61 44 62 5b 5d  the sqlite.aDb[]
51440 20 61 72 72 61 79 2e 20 20 61 44 62 5b 30 5d 20   array.  aDb[0] 
51450 69 73 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  is the main data
51460 62 61 73 65 20 66 69 6c 65 20 61 6e 64 0a 2a 2a  base file and.**
51470 20 61 44 62 5b 31 5d 20 69 73 20 74 68 65 20 64   aDb[1] is the d
51480 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 65  atabase file use
51490 64 20 74 6f 20 68 6f 6c 64 20 74 65 6d 70 6f 72  d to hold tempor
514a0 61 72 79 20 74 61 62 6c 65 73 2e 20 20 41 64 64  ary tables.  Add
514b0 69 74 69 6f 6e 61 6c 0a 2a 2a 20 64 61 74 61 62  itional.** datab
514c0 61 73 65 73 20 6d 61 79 20 62 65 20 61 74 74 61  ases may be atta
514d0 63 68 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ched..*/.struct 
514e0 44 62 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61  Db {.  char *zNa
514f0 6d 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  me;         /* N
51500 61 6d 65 20 6f 66 20 74 68 69 73 20 64 61 74 61  ame of this data
51510 62 61 73 65 20 2a 2f 0a 20 20 42 74 72 65 65 20  base */.  Btree 
51520 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 2f  *pBt;          /
51530 2a 20 54 68 65 20 42 2a 54 72 65 65 20 73 74 72  * The B*Tree str
51540 75 63 74 75 72 65 20 66 6f 72 20 74 68 69 73 20  ucture for this 
51550 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
51560 0a 20 20 75 38 20 69 6e 54 72 61 6e 73 3b 20 20  .  u8 inTrans;  
51570 20 20 20 20 20 20 20 20 2f 2a 20 30 3a 20 6e 6f          /* 0: no
51580 74 20 77 72 69 74 61 62 6c 65 2e 20 20 31 3a 20  t writable.  1: 
51590 54 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 32 3a  Transaction.  2:
515a0 20 43 68 65 63 6b 70 6f 69 6e 74 20 2a 2f 0a 20   Checkpoint */. 
515b0 20 75 38 20 73 61 66 65 74 79 5f 6c 65 76 65 6c   u8 safety_level
515c0 3b 20 20 20 20 20 2f 2a 20 48 6f 77 20 61 67 67  ;     /* How agg
515d0 72 65 73 73 69 76 65 20 61 74 20 73 79 6e 63 68  ressive at synch
515e0 69 6e 67 20 64 61 74 61 20 74 6f 20 64 69 73 6b  ing data to disk
515f0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78   */.  void *pAux
51600 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
51610 2f 2a 20 41 75 78 69 6c 69 61 72 79 20 64 61 74  /* Auxiliary dat
51620 61 2e 20 20 55 73 75 61 6c 6c 79 20 4e 55 4c 4c  a.  Usually NULL
51630 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 72   */.  void (*xFr
51640 65 65 41 75 78 29 28 76 6f 69 64 2a 29 3b 20 20  eeAux)(void*);  
51650 2f 2a 20 52 6f 75 74 69 6e 65 20 74 6f 20 66 72  /* Routine to fr
51660 65 65 20 70 41 75 78 20 2a 2f 0a 20 20 53 63 68  ee pAux */.  Sch
51670 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20  ema *pSchema;   
51680 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
51690 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
516a0 28 70 6f 73 73 69 62 6c 79 20 73 68 61 72 65 64  (possibly shared
516b0 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  ) */.};../*.** A
516c0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
516d0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
516e0 63 74 75 72 65 20 73 74 6f 72 65 73 20 61 20 64  cture stores a d
516f0 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 0a  atabase schema..
51700 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61  **.** If there a
51710 72 65 20 6e 6f 20 76 69 72 74 75 61 6c 20 74 61  re no virtual ta
51720 62 6c 65 73 20 63 6f 6e 66 69 67 75 72 65 64 20  bles configured 
51730 69 6e 20 74 68 69 73 20 73 63 68 65 6d 61 2c 20  in this schema, 
51740 74 68 65 0a 2a 2a 20 53 63 68 65 6d 61 2e 64 62  the.** Schema.db
51750 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74   variable is set
51760 20 74 6f 20 4e 55 4c 4c 2e 20 41 66 74 65 72 20   to NULL. After 
51770 74 68 65 20 66 69 72 73 74 20 76 69 72 74 75 61  the first virtua
51780 6c 20 74 61 62 6c 65 0a 2a 2a 20 68 61 73 20 62  l table.** has b
51790 65 65 6e 20 61 64 64 65 64 2c 20 69 74 20 69 73  een added, it is
517a0 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f   set to point to
517b0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
517c0 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 75 73 65  nnection .** use
517d0 64 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20  d to create the 
517e0 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f 6e 63 65  connection. Once
517f0 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
51800 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 61 64 64   has been.** add
51810 65 64 20 74 6f 20 74 68 65 20 53 63 68 65 6d 61  ed to the Schema
51820 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 74   structure and t
51830 68 65 20 53 63 68 65 6d 61 2e 64 62 20 76 61 72  he Schema.db var
51840 69 61 62 6c 65 20 70 6f 70 75 6c 61 74 65 64 2c  iable populated,
51850 20 0a 2a 2a 20 6f 6e 6c 79 20 74 68 61 74 20 64   .** only that d
51860 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
51870 6f 6e 20 6d 61 79 20 75 73 65 20 74 68 65 20 53  on may use the S
51880 63 68 65 6d 61 20 74 6f 20 70 72 65 70 61 72 65  chema to prepare
51890 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 2e   .** statements.
518a0 0a 2a 2f 0a 73 74 72 75 63 74 20 53 63 68 65 6d  .*/.struct Schem
518b0 61 20 7b 0a 20 20 69 6e 74 20 73 63 68 65 6d 61  a {.  int schema
518c0 5f 63 6f 6f 6b 69 65 3b 20 20 20 2f 2a 20 44 61  _cookie;   /* Da
518d0 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 76 65  tabase schema ve
518e0 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 66 6f 72  rsion number for
518f0 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20   this file */.  
51900 48 61 73 68 20 74 62 6c 48 61 73 68 3b 20 20 20  Hash tblHash;   
51910 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74 61 62 6c       /* All tabl
51920 65 73 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61  es indexed by na
51930 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20 69 64 78  me */.  Hash idx
51940 48 61 73 68 3b 20 20 20 20 20 20 20 20 2f 2a 20  Hash;        /* 
51950 41 6c 6c 20 28 6e 61 6d 65 64 29 20 69 6e 64 69  All (named) indi
51960 63 65 73 20 69 6e 64 65 78 65 64 20 62 79 20 6e  ces indexed by n
51970 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20 74 72  ame */.  Hash tr
51980 69 67 48 61 73 68 3b 20 20 20 20 20 20 20 2f 2a  igHash;       /*
51990 20 41 6c 6c 20 74 72 69 67 67 65 72 73 20 69 6e   All triggers in
519a0 64 65 78 65 64 20 62 79 20 6e 61 6d 65 20 2a 2f  dexed by name */
519b0 0a 20 20 48 61 73 68 20 61 46 4b 65 79 3b 20 20  .  Hash aFKey;  
519c0 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 65 69          /* Forei
519d0 67 6e 20 6b 65 79 73 20 69 6e 64 65 78 65 64 20  gn keys indexed 
519e0 62 79 20 74 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20  by to-table */. 
519f0 20 54 61 62 6c 65 20 2a 70 53 65 71 54 61 62 3b   Table *pSeqTab;
51a00 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 71 6c        /* The sql
51a10 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62  ite_sequence tab
51a20 6c 65 20 75 73 65 64 20 62 79 20 41 55 54 4f 49  le used by AUTOI
51a30 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 20 20 75 38  NCREMENT */.  u8
51a40 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 20 20 20   file_format;   
51a50 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 66 6f 72     /* Schema for
51a60 6d 61 74 20 76 65 72 73 69 6f 6e 20 66 6f 72 20  mat version for 
51a70 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 75  this file */.  u
51a80 38 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20 20  8 enc;          
51a90 20 20 20 20 2f 2a 20 54 65 78 74 20 65 6e 63 6f      /* Text enco
51aa0 64 69 6e 67 20 75 73 65 64 20 62 79 20 74 68 69  ding used by thi
51ab0 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  s database */.  
51ac0 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20  u16 flags;      
51ad0 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 61 73       /* Flags as
51ae0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
51af0 69 73 20 73 63 68 65 6d 61 20 2a 2f 0a 20 20 69  is schema */.  i
51b00 6e 74 20 63 61 63 68 65 5f 73 69 7a 65 3b 20 20  nt cache_size;  
51b10 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
51b20 20 70 61 67 65 73 20 74 6f 20 75 73 65 20 69 6e   pages to use in
51b30 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 23 69   the cache */.#i
51b40 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
51b50 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
51b60 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20   sqlite3 *db;   
51b70 20 20 20 20 20 20 2f 2a 20 22 4f 77 6e 65 72 22        /* "Owner"
51b80 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 53 65 65   connection. See
51b90 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 20 2a   comment above *
51ba0 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a  /.#endif.};../*.
51bb0 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20  ** These macros 
51bc0 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 74  can be used to t
51bd0 65 73 74 2c 20 73 65 74 2c 20 6f 72 20 63 6c 65  est, set, or cle
51be0 61 72 20 62 69 74 73 20 69 6e 20 74 68 65 20 0a  ar bits in the .
51bf0 2a 2a 20 44 62 2e 66 6c 61 67 73 20 66 69 65 6c  ** Db.flags fiel
51c00 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 62  d..*/.#define Db
51c10 48 61 73 50 72 6f 70 65 72 74 79 28 44 2c 49 2c  HasProperty(D,I,
51c20 50 29 20 20 20 20 20 28 28 28 44 29 2d 3e 61 44  P)     (((D)->aD
51c30 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c  b[I].pSchema->fl
51c40 61 67 73 26 28 50 29 29 3d 3d 28 50 29 29 0a 23  ags&(P))==(P)).#
51c50 64 65 66 69 6e 65 20 44 62 48 61 73 41 6e 79 50  define DbHasAnyP
51c60 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20  roperty(D,I,P)  
51c70 28 28 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53  (((D)->aDb[I].pS
51c80 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 28 50 29  chema->flags&(P)
51c90 29 21 3d 30 29 0a 23 64 65 66 69 6e 65 20 44 62  )!=0).#define Db
51ca0 53 65 74 50 72 6f 70 65 72 74 79 28 44 2c 49 2c  SetProperty(D,I,
51cb0 50 29 20 20 20 20 20 28 44 29 2d 3e 61 44 62 5b  P)     (D)->aDb[
51cc0 49 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67  I].pSchema->flag
51cd0 73 7c 3d 28 50 29 0a 23 64 65 66 69 6e 65 20 44  s|=(P).#define D
51ce0 62 43 6c 65 61 72 50 72 6f 70 65 72 74 79 28 44  bClearProperty(D
51cf0 2c 49 2c 50 29 20 20 20 28 44 29 2d 3e 61 44 62  ,I,P)   (D)->aDb
51d00 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61  [I].pSchema->fla
51d10 67 73 26 3d 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20  gs&=~(P)../*.** 
51d20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66  Allowed values f
51d30 6f 72 20 74 68 65 20 44 42 2e 66 6c 61 67 73 20  or the DB.flags 
51d40 66 69 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  field..**.** The
51d50 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64   DB_SchemaLoaded
51d60 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 66 74   flag is set aft
51d70 65 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20  er the database 
51d80 73 63 68 65 6d 61 20 68 61 73 20 62 65 65 6e 0a  schema has been.
51d90 2a 2a 20 72 65 61 64 20 69 6e 74 6f 20 69 6e 74  ** read into int
51da0 65 72 6e 61 6c 20 68 61 73 68 20 74 61 62 6c 65  ernal hash table
51db0 73 2e 0a 2a 2a 0a 2a 2a 20 44 42 5f 55 6e 72 65  s..**.** DB_Unre
51dc0 73 65 74 56 69 65 77 73 20 6d 65 61 6e 73 20 74  setViews means t
51dd0 68 61 74 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  hat one or more 
51de0 76 69 65 77 73 20 68 61 76 65 20 63 6f 6c 75 6d  views have colum
51df0 6e 20 6e 61 6d 65 73 20 74 68 61 74 0a 2a 2a 20  n names that.** 
51e00 68 61 76 65 20 62 65 65 6e 20 66 69 6c 6c 65 64  have been filled
51e10 20 6f 75 74 2e 20 20 49 66 20 74 68 65 20 73 63   out.  If the sc
51e20 68 65 6d 61 20 63 68 61 6e 67 65 73 2c 20 74 68  hema changes, th
51e30 65 73 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  ese column names
51e40 20 6d 69 67 68 74 0a 2a 2a 20 63 68 61 6e 67 65   might.** change
51e50 73 20 61 6e 64 20 73 6f 20 74 68 65 20 76 69 65  s and so the vie
51e60 77 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20 62  w will need to b
51e70 65 20 72 65 73 65 74 2e 0a 2a 2f 0a 23 64 65 66  e reset..*/.#def
51e80 69 6e 65 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61  ine DB_SchemaLoa
51e90 64 65 64 20 20 20 20 30 78 30 30 30 31 20 20 2f  ded    0x0001  /
51ea0 2a 20 54 68 65 20 73 63 68 65 6d 61 20 68 61 73  * The schema has
51eb0 20 62 65 65 6e 20 6c 6f 61 64 65 64 20 2a 2f 0a   been loaded */.
51ec0 23 64 65 66 69 6e 65 20 44 42 5f 55 6e 72 65 73  #define DB_Unres
51ed0 65 74 56 69 65 77 73 20 20 20 20 30 78 30 30 30  etViews    0x000
51ee0 32 20 20 2f 2a 20 53 6f 6d 65 20 76 69 65 77 73  2  /* Some views
51ef0 20 68 61 76 65 20 64 65 66 69 6e 65 64 20 63 6f   have defined co
51f00 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a 2f 0a 23 64  lumn names */.#d
51f10 65 66 69 6e 65 20 44 42 5f 45 6d 70 74 79 20 20  efine DB_Empty  
51f20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 34 20           0x0004 
51f30 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 69 73 20   /* The file is 
51f40 65 6d 70 74 79 20 28 6c 65 6e 67 74 68 20 30 20  empty (length 0 
51f50 62 79 74 65 73 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  bytes) */../*.**
51f60 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64   The number of d
51f70 69 66 66 65 72 65 6e 74 20 6b 69 6e 64 73 20 6f  ifferent kinds o
51f80 66 20 74 68 69 6e 67 73 20 74 68 61 74 20 63 61  f things that ca
51f90 6e 20 62 65 20 6c 69 6d 69 74 65 64 0a 2a 2a 20  n be limited.** 
51fa0 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65  using the sqlite
51fb0 33 5f 6c 69 6d 69 74 28 29 20 69 6e 74 65 72 66  3_limit() interf
51fc0 61 63 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ace..*/.#define 
51fd0 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d 49 54 20 28  SQLITE_N_LIMIT (
51fe0 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52  SQLITE_LIMIT_VAR
51ff0 49 41 42 4c 45 5f 4e 55 4d 42 45 52 2b 31 29 0a  IABLE_NUMBER+1).
52000 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61 74 61  ./*.** Each data
52010 62 61 73 65 20 69 73 20 61 6e 20 69 6e 73 74 61  base is an insta
52020 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
52030 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a  wing structure..
52040 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
52050 2e 6c 61 73 74 52 6f 77 69 64 20 72 65 63 6f 72  .lastRowid recor
52060 64 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65  ds the last inse
52070 72 74 20 72 6f 77 69 64 20 67 65 6e 65 72 61 74  rt rowid generat
52080 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 65  ed by an.** inse
52090 72 74 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49  rt statement.  I
520a0 6e 73 65 72 74 73 20 6f 6e 20 76 69 65 77 73 20  nserts on views 
520b0 64 6f 20 6e 6f 74 20 61 66 66 65 63 74 20 69 74  do not affect it
520c0 73 20 76 61 6c 75 65 2e 20 20 45 61 63 68 0a 2a  s value.  Each.*
520d0 2a 20 74 72 69 67 67 65 72 20 68 61 73 20 69 74  * trigger has it
520e0 73 20 6f 77 6e 20 63 6f 6e 74 65 78 74 2c 20 73  s own context, s
520f0 6f 20 74 68 61 74 20 6c 61 73 74 52 6f 77 69 64  o that lastRowid
52100 20 63 61 6e 20 62 65 20 75 70 64 61 74 65 64 20   can be updated 
52110 69 6e 73 69 64 65 0a 2a 2a 20 74 72 69 67 67 65  inside.** trigge
52120 72 73 20 61 73 20 75 73 75 61 6c 2e 20 20 54 68  rs as usual.  Th
52130 65 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65  e previous value
52140 20 77 69 6c 6c 20 62 65 20 72 65 73 74 6f 72 65   will be restore
52150 64 20 6f 6e 63 65 20 74 68 65 20 74 72 69 67 67  d once the trigg
52160 65 72 0a 2a 2a 20 65 78 69 74 73 2e 20 20 55 70  er.** exits.  Up
52170 6f 6e 20 65 6e 74 65 72 69 6e 67 20 61 20 62 65  on entering a be
52180 66 6f 72 65 20 6f 72 20 69 6e 73 74 65 61 64 20  fore or instead 
52190 6f 66 20 74 72 69 67 67 65 72 2c 20 6c 61 73 74  of trigger, last
521a0 52 6f 77 69 64 20 69 73 20 6e 6f 0a 2a 2a 20 6c  Rowid is no.** l
521b0 6f 6e 67 65 72 20 28 73 69 6e 63 65 20 61 66 74  onger (since aft
521c0 65 72 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 31  er version 2.8.1
521d0 32 29 20 72 65 73 65 74 20 74 6f 20 2d 31 2e 0a  2) reset to -1..
521e0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
521f0 2e 6e 43 68 61 6e 67 65 20 64 6f 65 73 20 6e 6f  .nChange does no
52200 74 20 63 6f 75 6e 74 20 63 68 61 6e 67 65 73 20  t count changes 
52210 77 69 74 68 69 6e 20 74 72 69 67 67 65 72 73 20  within triggers 
52220 61 6e 64 20 6b 65 65 70 73 20 6e 6f 0a 2a 2a 20  and keeps no.** 
52230 63 6f 6e 74 65 78 74 2e 20 20 49 74 20 69 73 20  context.  It is 
52240 72 65 73 65 74 20 61 74 20 73 74 61 72 74 20 6f  reset at start o
52250 66 20 73 71 6c 69 74 65 33 5f 65 78 65 63 2e 0a  f sqlite3_exec..
52260 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6c 73  ** The sqlite.ls
52270 43 68 61 6e 67 65 20 72 65 70 72 65 73 65 6e 74  Change represent
52280 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
52290 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20  changes made by 
522a0 74 68 65 20 6c 61 73 74 0a 2a 2a 20 69 6e 73 65  the last.** inse
522b0 72 74 2c 20 75 70 64 61 74 65 2c 20 6f 72 20 64  rt, update, or d
522c0 65 6c 65 74 65 20 73 74 61 74 65 6d 65 6e 74 2e  elete statement.
522d0 20 20 49 74 20 72 65 6d 61 69 6e 73 20 63 6f 6e    It remains con
522e0 73 74 61 6e 74 20 74 68 72 6f 75 67 68 6f 75 74  stant throughout
522f0 20 74 68 65 0a 2a 2a 20 6c 65 6e 67 74 68 20 6f   the.** length o
52300 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 61 6e  f a statement an
52310 64 20 69 73 20 74 68 65 6e 20 75 70 64 61 74 65  d is then update
52320 64 20 62 79 20 4f 50 5f 53 65 74 43 6f 75 6e 74  d by OP_SetCount
52330 73 2e 20 20 49 74 20 6b 65 65 70 73 20 61 0a 2a  s.  It keeps a.*
52340 2a 20 63 6f 6e 74 65 78 74 20 73 74 61 63 6b 20  * context stack 
52350 6a 75 73 74 20 6c 69 6b 65 20 6c 61 73 74 52 6f  just like lastRo
52360 77 69 64 20 73 6f 20 74 68 61 74 20 74 68 65 20  wid so that the 
52370 63 6f 75 6e 74 20 6f 66 20 63 68 61 6e 67 65 73  count of changes
52380 0a 2a 2a 20 77 69 74 68 69 6e 20 61 20 74 72 69  .** within a tri
52390 67 67 65 72 20 69 73 20 6e 6f 74 20 73 65 65 6e  gger is not seen
523a0 20 6f 75 74 73 69 64 65 20 74 68 65 20 74 72 69   outside the tri
523b0 67 67 65 72 2e 20 20 43 68 61 6e 67 65 73 20 74  gger.  Changes t
523c0 6f 20 76 69 65 77 73 20 64 6f 20 6e 6f 74 0a 2a  o views do not.*
523d0 2a 20 61 66 66 65 63 74 20 74 68 65 20 76 61 6c  * affect the val
523e0 75 65 20 6f 66 20 6c 73 43 68 61 6e 67 65 2e 0a  ue of lsChange..
523f0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 63 73  ** The sqlite.cs
52400 43 68 61 6e 67 65 20 6b 65 65 70 73 20 74 72 61  Change keeps tra
52410 63 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72  ck of the number
52420 20 6f 66 20 63 75 72 72 65 6e 74 20 63 68 61 6e   of current chan
52430 67 65 73 20 28 73 69 6e 63 65 0a 2a 2a 20 74 68  ges (since.** th
52440 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74  e last statement
52450 29 20 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f  ) and is used to
52460 20 75 70 64 61 74 65 20 73 71 6c 69 74 65 5f 6c   update sqlite_l
52470 73 43 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 54  sChange..**.** T
52480 68 65 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62  he member variab
52490 6c 65 73 20 73 71 6c 69 74 65 2e 65 72 72 43 6f  les sqlite.errCo
524a0 64 65 2c 20 73 71 6c 69 74 65 2e 7a 45 72 72 4d  de, sqlite.zErrM
524b0 73 67 20 61 6e 64 20 73 71 6c 69 74 65 2e 7a 45  sg and sqlite.zE
524c0 72 72 4d 73 67 31 36 0a 2a 2a 20 73 74 6f 72 65  rrMsg16.** store
524d0 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
524e0 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 2c   error code and,
524f0 20 69 66 20 61 70 70 6c 69 63 61 62 6c 65 2c 20   if applicable, 
52500 73 74 72 69 6e 67 2e 20 54 68 65 0a 2a 2a 20 69  string. The.** i
52510 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e  nternal function
52520 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 20   sqlite3Error() 
52530 69 73 20 75 73 65 64 20 74 6f 20 73 65 74 20 74  is used to set t
52540 68 65 73 65 20 76 61 72 69 61 62 6c 65 73 0a 2a  hese variables.*
52550 2a 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e 0a  * consistently..
52560 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  */.struct sqlite
52570 33 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  3 {.  sqlite3_vf
52580 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20  s *pVfs;        
52590 20 20 20 20 2f 2a 20 4f 53 20 49 6e 74 65 72 66      /* OS Interf
525a0 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 62  ace */.  int nDb
525b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
525c0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
525d0 20 6f 66 20 62 61 63 6b 65 6e 64 73 20 63 75 72   of backends cur
525e0 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f  rently in use */
525f0 0a 20 20 44 62 20 2a 61 44 62 3b 20 20 20 20 20  .  Db *aDb;     
52600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52610 20 2f 2a 20 41 6c 6c 20 62 61 63 6b 65 6e 64 73   /* All backends
52620 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b   */.  int flags;
52630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52640 20 20 20 20 2f 2a 20 4d 69 73 63 65 6c 6c 61 6e      /* Miscellan
52650 6f 75 73 20 66 6c 61 67 73 2e 20 53 65 65 20 62  ous flags. See b
52660 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 6f 70  elow */.  int op
52670 65 6e 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20  enFlags;        
52680 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73          /* Flags
52690 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74   passed to sqlit
526a0 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a  e3_vfs.xOpen() *
526b0 2f 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65 3b  /.  int errCode;
526c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
526d0 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74    /* Most recent
526e0 20 65 72 72 6f 72 20 63 6f 64 65 20 28 53 51 4c   error code (SQL
526f0 49 54 45 5f 2a 29 20 2a 2f 0a 20 20 69 6e 74 20  ITE_*) */.  int 
52700 65 72 72 4d 61 73 6b 3b 20 20 20 20 20 20 20 20  errMask;        
52710 20 20 20 20 20 20 20 20 20 20 2f 2a 20 26 20 72            /* & r
52720 65 73 75 6c 74 20 63 6f 64 65 73 20 77 69 74 68  esult codes with
52730 20 74 68 69 73 20 62 65 66 6f 72 65 20 72 65 74   this before ret
52740 75 72 6e 69 6e 67 20 2a 2f 0a 20 20 75 38 20 61  urning */.  u8 a
52750 75 74 6f 43 6f 6d 6d 69 74 3b 20 20 20 20 20 20  utoCommit;      
52760 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
52770 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61   auto-commit fla
52780 67 2e 20 2a 2f 0a 20 20 75 38 20 74 65 6d 70 5f  g. */.  u8 temp_
52790 73 74 6f 72 65 3b 20 20 20 20 20 20 20 20 20 20  store;          
527a0 20 20 20 20 20 20 2f 2a 20 31 3a 20 66 69 6c 65        /* 1: file
527b0 20 32 3a 20 6d 65 6d 6f 72 79 20 30 3a 20 64 65   2: memory 0: de
527c0 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6d 61  fault */.  u8 ma
527d0 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20 20 20 20  llocFailed;     
527e0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
527f0 20 69 66 20 77 65 20 68 61 76 65 20 73 65 65 6e   if we have seen
52800 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72   a malloc failur
52810 65 20 2a 2f 0a 20 20 75 38 20 64 66 6c 74 4c 6f  e */.  u8 dfltLo
52820 63 6b 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20  ckMode;         
52830 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20       /* Default 
52840 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f 72  locking-mode for
52850 20 61 74 74 61 63 68 65 64 20 64 62 73 20 2a 2f   attached dbs */
52860 0a 20 20 75 38 20 64 66 6c 74 4a 6f 75 72 6e 61  .  u8 dfltJourna
52870 6c 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20  lMode;          
52880 20 2f 2a 20 44 65 66 61 75 6c 74 20 6a 6f 75 72   /* Default jour
52890 6e 61 6c 20 6d 6f 64 65 20 66 6f 72 20 61 74 74  nal mode for att
528a0 61 63 68 65 64 20 64 62 73 20 2a 2f 0a 20 20 73  ached dbs */.  s
528b0 69 67 6e 65 64 20 63 68 61 72 20 6e 65 78 74 41  igned char nextA
528c0 75 74 6f 76 61 63 3b 20 20 20 20 20 20 2f 2a 20  utovac;      /* 
528d0 41 75 74 6f 76 61 63 20 73 65 74 74 69 6e 67 20  Autovac setting 
528e0 61 66 74 65 72 20 56 41 43 55 55 4d 20 69 66 20  after VACUUM if 
528f0 3e 3d 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 78  >=0 */.  int nex
52900 74 50 61 67 65 73 69 7a 65 3b 20 20 20 20 20 20  tPagesize;      
52910 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 73 69         /* Pagesi
52920 7a 65 20 61 66 74 65 72 20 56 41 43 55 55 4d 20  ze after VACUUM 
52930 69 66 20 3e 30 20 2a 2f 0a 20 20 69 6e 74 20 6e  if >0 */.  int n
52940 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20  Table;          
52950 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
52960 65 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20  er of tables in 
52970 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  the database */.
52980 20 20 43 6f 6c 6c 53 65 71 20 2a 70 44 66 6c 74    CollSeq *pDflt
52990 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  Coll;           
529a0 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 63  /* The default c
529b0 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
529c0 65 20 28 42 49 4e 41 52 59 29 20 2a 2f 0a 20 20  e (BINARY) */.  
529d0 69 36 34 20 6c 61 73 74 52 6f 77 69 64 3b 20 20  i64 lastRowid;  
529e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
529f0 20 52 4f 57 49 44 20 6f 66 20 6d 6f 73 74 20 72   ROWID of most r
52a00 65 63 65 6e 74 20 69 6e 73 65 72 74 20 28 73 65  ecent insert (se
52a10 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 69 36  e above) */.  i6
52a20 34 20 70 72 69 6f 72 4e 65 77 52 6f 77 69 64 3b  4 priorNewRowid;
52a30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
52a40 61 73 74 20 72 61 6e 64 6f 6d 6c 79 20 67 65 6e  ast randomly gen
52a50 65 72 61 74 65 64 20 52 4f 57 49 44 20 2a 2f 0a  erated ROWID */.
52a60 20 20 69 6e 74 20 6d 61 67 69 63 3b 20 20 20 20    int magic;    
52a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52a80 2f 2a 20 4d 61 67 69 63 20 6e 75 6d 62 65 72 20  /* Magic number 
52a90 66 6f 72 20 64 65 74 65 63 74 20 6c 69 62 72 61  for detect libra
52aa0 72 79 20 6d 69 73 75 73 65 20 2a 2f 0a 20 20 69  ry misuse */.  i
52ab0 6e 74 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20  nt nChange;     
52ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
52ad0 56 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62  Value returned b
52ae0 79 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65  y sqlite3_change
52af0 73 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f  s() */.  int nTo
52b00 74 61 6c 43 68 61 6e 67 65 3b 20 20 20 20 20 20  talChange;      
52b10 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20         /* Value 
52b20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69  returned by sqli
52b30 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65  te3_total_change
52b40 73 28 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  s() */.  sqlite3
52b50 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20  _mutex *mutex;  
52b60 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63         /* Connec
52b70 74 69 6f 6e 20 6d 75 74 65 78 20 2a 2f 0a 20 20  tion mutex */.  
52b80 69 6e 74 20 61 4c 69 6d 69 74 5b 53 51 4c 49 54  int aLimit[SQLIT
52b90 45 5f 4e 5f 4c 49 4d 49 54 5d 3b 20 20 20 2f 2a  E_N_LIMIT];   /*
52ba0 20 4c 69 6d 69 74 73 20 2a 2f 0a 20 20 73 74 72   Limits */.  str
52bb0 75 63 74 20 73 71 6c 69 74 65 33 49 6e 69 74 49  uct sqlite3InitI
52bc0 6e 66 6f 20 7b 20 20 20 20 20 20 2f 2a 20 49 6e  nfo {      /* In
52bd0 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 64  formation used d
52be0 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61  uring initializa
52bf0 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20  tion */.    int 
52c00 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
52c10 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 6e 20          /* When 
52c20 62 61 63 6b 20 69 73 20 62 65 69 6e 67 20 69 6e  back is being in
52c30 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 20  itialized */.   
52c40 20 69 6e 74 20 6e 65 77 54 6e 75 6d 3b 20 20 20   int newTnum;   
52c50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
52c60 52 6f 6f 74 70 61 67 65 20 6f 66 20 74 61 62 6c  Rootpage of tabl
52c70 65 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69  e being initiali
52c80 7a 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 62 75  zed */.    u8 bu
52c90 73 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  sy;             
52ca0 20 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 69         /* TRUE i
52cb0 66 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 69 74  f currently init
52cc0 69 61 6c 69 7a 69 6e 67 20 2a 2f 0a 20 20 7d 20  ializing */.  } 
52cd0 69 6e 69 74 3b 0a 20 20 69 6e 74 20 6e 45 78 74  init;.  int nExt
52ce0 65 6e 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20  ension;         
52cf0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
52d00 6f 66 20 6c 6f 61 64 65 64 20 65 78 74 65 6e 73  of loaded extens
52d10 69 6f 6e 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  ions */.  void *
52d20 2a 61 45 78 74 65 6e 73 69 6f 6e 3b 20 20 20 20  *aExtension;    
52d30 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79          /* Array
52d40 20 6f 66 20 73 68 61 72 65 64 20 6c 69 62 72 61   of shared libra
52d50 72 61 79 20 68 61 6e 64 6c 65 73 20 2a 2f 0a 20  ray handles */. 
52d60 20 73 74 72 75 63 74 20 56 64 62 65 20 2a 70 56   struct Vdbe *pV
52d70 64 62 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f  dbe;           /
52d80 2a 20 4c 69 73 74 20 6f 66 20 61 63 74 69 76 65  * List of active
52d90 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
52da0 73 20 2a 2f 0a 20 20 69 6e 74 20 61 63 74 69 76  s */.  int activ
52db0 65 56 64 62 65 43 6e 74 3b 20 20 20 20 20 20 20  eVdbeCnt;       
52dc0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
52dd0 66 20 76 64 62 65 73 20 63 75 72 72 65 6e 74 6c  f vdbes currentl
52de0 79 20 65 78 65 63 75 74 69 6e 67 20 2a 2f 0a 20  y executing */. 
52df0 20 76 6f 69 64 20 28 2a 78 54 72 61 63 65 29 28   void (*xTrace)(
52e00 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72  void*,const char
52e10 2a 29 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72  *);        /* Tr
52e20 61 63 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  ace function */.
52e30 20 20 76 6f 69 64 20 2a 70 54 72 61 63 65 41 72    void *pTraceAr
52e40 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
52e50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
52e60 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 74  rgument to the t
52e70 72 61 63 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  race function */
52e80 0a 20 20 76 6f 69 64 20 28 2a 78 50 72 6f 66 69  .  void (*xProfi
52e90 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20  le)(void*,const 
52ea0 63 68 61 72 2a 2c 75 36 34 29 3b 20 20 2f 2a 20  char*,u64);  /* 
52eb0 50 72 6f 66 69 6c 69 6e 67 20 66 75 6e 63 74 69  Profiling functi
52ec0 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 50  on */.  void *pP
52ed0 72 6f 66 69 6c 65 41 72 67 3b 20 20 20 20 20 20  rofileArg;      
52ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52ef0 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f    /* Argument to
52f00 20 70 72 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f   profile functio
52f10 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6f  n */.  void *pCo
52f20 6d 6d 69 74 41 72 67 3b 20 20 20 20 20 20 20 20  mmitArg;        
52f30 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75           /* Argu
52f40 6d 65 6e 74 20 74 6f 20 78 43 6f 6d 6d 69 74 43  ment to xCommitC
52f50 61 6c 6c 62 61 63 6b 28 29 20 2a 2f 20 20 20 0a  allback() */   .
52f60 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 43    int (*xCommitC
52f70 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 29 3b  allback)(void*);
52f80 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 64 20 61      /* Invoked a
52f90 74 20 65 76 65 72 79 20 63 6f 6d 6d 69 74 2e 20  t every commit. 
52fa0 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 52 6f 6c 6c  */.  void *pRoll
52fb0 62 61 63 6b 41 72 67 3b 20 20 20 20 20 20 20 20  backArg;        
52fc0 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65         /* Argume
52fd0 6e 74 20 74 6f 20 78 52 6f 6c 6c 62 61 63 6b 43  nt to xRollbackC
52fe0 61 6c 6c 62 61 63 6b 28 29 20 2a 2f 20 20 20 0a  allback() */   .
52ff0 20 20 76 6f 69 64 20 28 2a 78 52 6f 6c 6c 62 61    void (*xRollba
53000 63 6b 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64  ckCallback)(void
53010 2a 29 3b 20 2f 2a 20 49 6e 76 6f 6b 65 64 20 61  *); /* Invoked a
53020 74 20 65 76 65 72 79 20 63 6f 6d 6d 69 74 2e 20  t every commit. 
53030 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 70 64 61  */.  void *pUpda
53040 74 65 41 72 67 3b 0a 20 20 76 6f 69 64 20 28 2a  teArg;.  void (*
53050 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 29  xUpdateCallback)
53060 28 76 6f 69 64 2a 2c 69 6e 74 2c 20 63 6f 6e 73  (void*,int, cons
53070 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t char*,const ch
53080 61 72 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34  ar*,sqlite_int64
53090 29 3b 0a 20 20 76 6f 69 64 28 2a 78 43 6f 6c 6c  );.  void(*xColl
530a0 4e 65 65 64 65 64 29 28 76 6f 69 64 2a 2c 73 71  Needed)(void*,sq
530b0 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78 74  lite3*,int eText
530c0 52 65 70 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  Rep,const char*)
530d0 3b 0a 20 20 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e  ;.  void(*xCollN
530e0 65 65 64 65 64 31 36 29 28 76 6f 69 64 2a 2c 73  eeded16)(void*,s
530f0 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78  qlite3*,int eTex
53100 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  tRep,const void*
53110 29 3b 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6c 6c  );.  void *pColl
53120 4e 65 65 64 65 64 41 72 67 3b 0a 20 20 73 71 6c  NeededArg;.  sql
53130 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 45 72 72  ite3_value *pErr
53140 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f  ;          /* Mo
53150 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20  st recent error 
53160 6d 65 73 73 61 67 65 20 2a 2f 0a 20 20 63 68 61  message */.  cha
53170 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20  r *zErrMsg;     
53180 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f             /* Mo
53190 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20  st recent error 
531a0 6d 65 73 73 61 67 65 20 28 55 54 46 2d 38 20 65  message (UTF-8 e
531b0 6e 63 6f 64 65 64 29 20 2a 2f 0a 20 20 63 68 61  ncoded) */.  cha
531c0 72 20 2a 7a 45 72 72 4d 73 67 31 36 3b 20 20 20  r *zErrMsg16;   
531d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f             /* Mo
531e0 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20  st recent error 
531f0 6d 65 73 73 61 67 65 20 28 55 54 46 2d 31 36 20  message (UTF-16 
53200 65 6e 63 6f 64 65 64 29 20 2a 2f 0a 20 20 75 6e  encoded) */.  un
53210 69 6f 6e 20 7b 0a 20 20 20 20 69 6e 74 20 69 73  ion {.    int is
53220 49 6e 74 65 72 72 75 70 74 65 64 3b 20 20 20 20  Interrupted;    
53230 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
53240 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
53250 70 74 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c  pt has been call
53260 65 64 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65  ed */.    double
53270 20 6e 6f 74 55 73 65 64 31 3b 20 20 20 20 20 20   notUsed1;      
53280 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 72 20        /* Spacer 
53290 2a 2f 0a 20 20 7d 20 75 31 3b 0a 23 69 66 6e 64  */.  } u1;.#ifnd
532a0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
532b0 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69  UTHORIZATION.  i
532c0 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f 69 64  nt (*xAuth)(void
532d0 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72  *,int,const char
532e0 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  *,const char*,co
532f0 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
53300 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20  char*);.        
53310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53320 20 20 20 20 20 20 20 20 2f 2a 20 41 63 63 65 73          /* Acces
53330 73 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  s authorization 
53340 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f  function */.  vo
53350 69 64 20 2a 70 41 75 74 68 41 72 67 3b 20 20 20  id *pAuthArg;   
53360 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31              /* 1
53370 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  st argument to t
53380 68 65 20 61 63 63 65 73 73 20 61 75 74 68 20 66  he access auth f
53390 75 6e 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69  unction */.#endi
533a0 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  f.#ifndef SQLITE
533b0 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43  _OMIT_PROGRESS_C
533c0 41 4c 4c 42 41 43 4b 0a 20 20 69 6e 74 20 28 2a  ALLBACK.  int (*
533d0 78 50 72 6f 67 72 65 73 73 29 28 76 6f 69 64 20  xProgress)(void 
533e0 2a 29 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 70  *);     /* The p
533f0 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b  rogress callback
53400 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 50 72 6f   */.  void *pPro
53410 67 72 65 73 73 41 72 67 3b 20 20 20 20 20 20 20  gressArg;       
53420 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20      /* Argument 
53430 74 6f 20 74 68 65 20 70 72 6f 67 72 65 73 73 20  to the progress 
53440 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e  callback */.  in
53450 74 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 3b 20  t nProgressOps; 
53460 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
53470 75 6d 62 65 72 20 6f 66 20 6f 70 63 6f 64 65 73  umber of opcodes
53480 20 66 6f 72 20 70 72 6f 67 72 65 73 73 20 63 61   for progress ca
53490 6c 6c 62 61 63 6b 20 2a 2f 0a 23 65 6e 64 69 66  llback */.#endif
534a0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
534b0 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
534c0 45 0a 20 20 48 61 73 68 20 61 4d 6f 64 75 6c 65  E.  Hash aModule
534d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
534e0 20 20 2f 2a 20 70 6f 70 75 6c 61 74 65 64 20 62    /* populated b
534f0 79 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  y sqlite3_create
53500 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20 54  _module() */.  T
53510 61 62 6c 65 20 2a 70 56 54 61 62 3b 20 20 20 20  able *pVTab;    
53520 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
53530 76 74 61 62 20 77 69 74 68 20 61 63 74 69 76 65  vtab with active
53540 20 43 6f 6e 6e 65 63 74 2f 43 72 65 61 74 65 20   Connect/Create 
53550 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 71 6c 69  method */.  sqli
53560 74 65 33 5f 76 74 61 62 20 2a 2a 61 56 54 72 61  te3_vtab **aVTra
53570 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20 56 69 72  ns;       /* Vir
53580 74 75 61 6c 20 74 61 62 6c 65 73 20 77 69 74 68  tual tables with
53590 20 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f   open transactio
535a0 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 54 72  ns */.  int nVTr
535b0 61 6e 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ans;            
535c0 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74        /* Allocat
535d0 65 64 20 73 69 7a 65 20 6f 66 20 61 56 54 72 61  ed size of aVTra
535e0 6e 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 48  ns */.#endif.  H
535f0 61 73 68 20 61 46 75 6e 63 3b 20 20 20 20 20 20  ash aFunc;      
53600 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
53610 41 6c 6c 20 66 75 6e 63 74 69 6f 6e 73 20 74 68  All functions th
53620 61 74 20 63 61 6e 20 62 65 20 69 6e 20 53 51 4c  at can be in SQL
53630 20 65 78 70 72 73 20 2a 2f 0a 20 20 48 61 73 68   exprs */.  Hash
53640 20 61 43 6f 6c 6c 53 65 71 3b 20 20 20 20 20 20   aCollSeq;      
53650 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c            /* All
53660 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
53670 6e 63 65 73 20 2a 2f 0a 20 20 42 75 73 79 48 61  nces */.  BusyHa
53680 6e 64 6c 65 72 20 62 75 73 79 48 61 6e 64 6c 65  ndler busyHandle
53690 72 3b 20 20 20 20 20 20 2f 2a 20 42 75 73 79 20  r;      /* Busy 
536a0 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e  callback */.  in
536b0 74 20 62 75 73 79 54 69 6d 65 6f 75 74 3b 20 20  t busyTimeout;  
536c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
536d0 75 73 79 20 68 61 6e 64 6c 65 72 20 74 69 6d 65  usy handler time
536e0 6f 75 74 2c 20 69 6e 20 6d 73 65 63 20 2a 2f 0a  out, in msec */.
536f0 20 20 44 62 20 61 44 62 53 74 61 74 69 63 5b 32    Db aDbStatic[2
53700 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ];              
53710 2f 2a 20 53 74 61 74 69 63 20 73 70 61 63 65 20  /* Static space 
53720 66 6f 72 20 74 68 65 20 32 20 64 65 66 61 75 6c  for the 2 defaul
53730 74 20 62 61 63 6b 65 6e 64 73 20 2a 2f 0a 23 69  t backends */.#i
53740 66 64 65 66 20 53 51 4c 49 54 45 5f 53 53 45 0a  fdef SQLITE_SSE.
53750 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
53760 70 46 65 74 63 68 3b 20 20 20 20 20 20 20 20 20  pFetch;         
53770 2f 2a 20 55 73 65 64 20 62 79 20 53 53 45 20 74  /* Used by SSE t
53780 6f 20 66 65 74 63 68 20 73 74 6f 72 65 64 20 73  o fetch stored s
53790 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 23 65 6e  tatements */.#en
537a0 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20  dif.};../*.** A 
537b0 6d 61 63 72 6f 20 74 6f 20 64 69 73 63 6f 76 65  macro to discove
537c0 72 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f  r the encoding o
537d0 66 20 61 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f  f a database..*/
537e0 0a 23 64 65 66 69 6e 65 20 45 4e 43 28 64 62 29  .#define ENC(db)
537f0 20 28 28 64 62 29 2d 3e 61 44 62 5b 30 5d 2e 70   ((db)->aDb[0].p
53800 53 63 68 65 6d 61 2d 3e 65 6e 63 29 0a 0a 2f 2a  Schema->enc)../*
53810 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c  .** Possible val
53820 75 65 73 20 66 6f 72 20 74 68 65 20 73 71 6c 69  ues for the sqli
53830 74 65 2e 66 6c 61 67 73 20 61 6e 64 20 6f 72 20  te.flags and or 
53840 44 62 2e 66 6c 61 67 73 20 66 69 65 6c 64 73 2e  Db.flags fields.
53850 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 71 6c 69 74 65  .**.** On sqlite
53860 2e 66 6c 61 67 73 2c 20 74 68 65 20 53 51 4c 49  .flags, the SQLI
53870 54 45 5f 49 6e 54 72 61 6e 73 20 76 61 6c 75 65  TE_InTrans value
53880 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 68   means that we h
53890 61 76 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20  ave.** executed 
538a0 61 20 42 45 47 49 4e 2e 20 20 4f 6e 20 44 62 2e  a BEGIN.  On Db.
538b0 66 6c 61 67 73 2c 20 53 51 4c 49 54 45 5f 49 6e  flags, SQLITE_In
538c0 54 72 61 6e 73 20 6d 65 61 6e 73 20 61 20 73 74  Trans means a st
538d0 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73  atement.** trans
538e0 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65  action is active
538f0 20 6f 6e 20 74 68 61 74 20 70 61 72 74 69 63 75   on that particu
53900 6c 61 72 20 64 61 74 61 62 61 73 65 20 66 69 6c  lar database fil
53910 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  e..*/.#define SQ
53920 4c 49 54 45 5f 56 64 62 65 54 72 61 63 65 20 20  LITE_VdbeTrace  
53930 20 20 20 20 30 78 30 30 30 30 30 30 30 31 20 20      0x00000001  
53940 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 61 63 65  /* True to trace
53950 20 56 44 42 45 20 65 78 65 63 75 74 69 6f 6e 20   VDBE execution 
53960 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
53970 45 5f 49 6e 54 72 61 6e 73 20 20 20 20 20 20 20  E_InTrans       
53980 20 30 78 30 30 30 30 30 30 30 38 20 20 2f 2a 20   0x00000008  /* 
53990 54 72 75 65 20 69 66 20 69 6e 20 61 20 74 72 61  True if in a tra
539a0 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66  nsaction */.#def
539b0 69 6e 65 20 53 51 4c 49 54 45 5f 49 6e 74 65 72  ine SQLITE_Inter
539c0 6e 43 68 61 6e 67 65 73 20 20 30 78 30 30 30 30  nChanges  0x0000
539d0 30 30 31 30 20 20 2f 2a 20 55 6e 63 6f 6d 6d 69  0010  /* Uncommi
539e0 74 74 65 64 20 48 61 73 68 20 74 61 62 6c 65 20  tted Hash table 
539f0 63 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69  changes */.#defi
53a00 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f  ne SQLITE_FullCo
53a10 6c 4e 61 6d 65 73 20 20 20 30 78 30 30 30 30 30  lNames   0x00000
53a20 30 32 30 20 20 2f 2a 20 53 68 6f 77 20 66 75 6c  020  /* Show ful
53a30 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f  l column names o
53a40 6e 20 53 45 4c 45 43 54 20 2a 2f 0a 23 64 65 66  n SELECT */.#def
53a50 69 6e 65 20 53 51 4c 49 54 45 5f 53 68 6f 72 74  ine SQLITE_Short
53a60 43 6f 6c 4e 61 6d 65 73 20 20 30 78 30 30 30 30  ColNames  0x0000
53a70 30 30 34 30 20 20 2f 2a 20 53 68 6f 77 20 73 68  0040  /* Show sh
53a80 6f 72 74 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65  ort columns name
53a90 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  s */.#define SQL
53aa0 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 20 20  ITE_CountRows   
53ab0 20 20 20 30 78 30 30 30 30 30 30 38 30 20 20 2f     0x00000080  /
53ac0 2a 20 43 6f 75 6e 74 20 72 6f 77 73 20 63 68 61  * Count rows cha
53ad0 6e 67 65 64 20 62 79 20 49 4e 53 45 52 54 2c 20  nged by INSERT, 
53ae0 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  */.             
53af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53b00 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
53b10 20 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44    DELETE, or UPD
53b20 41 54 45 20 61 6e 64 20 72 65 74 75 72 6e 20 2a  ATE and return *
53b30 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /.              
53b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53b50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20              /*  
53b60 20 74 68 65 20 63 6f 75 6e 74 20 75 73 69 6e 67   the count using
53b70 20 61 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a   a callback. */.
53b80 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e  #define SQLITE_N
53b90 75 6c 6c 43 61 6c 6c 62 61 63 6b 20 20 20 30 78  ullCallback   0x
53ba0 30 30 30 30 30 31 30 30 20 20 2f 2a 20 49 6e 76  00000100  /* Inv
53bb0 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  oke the callback
53bc0 20 6f 6e 63 65 20 69 66 20 74 68 65 20 2a 2f 0a   once if the */.
53bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53bf0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 72            /*   r
53c00 65 73 75 6c 74 20 73 65 74 20 69 73 20 65 6d 70  esult set is emp
53c10 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ty */.#define SQ
53c20 4c 49 54 45 5f 53 71 6c 54 72 61 63 65 20 20 20  LITE_SqlTrace   
53c30 20 20 20 20 30 78 30 30 30 30 30 32 30 30 20 20      0x00000200  
53c40 2f 2a 20 44 65 62 75 67 20 70 72 69 6e 74 20 53  /* Debug print S
53c50 51 4c 20 61 73 20 69 74 20 65 78 65 63 75 74 65  QL as it execute
53c60 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  s */.#define SQL
53c70 49 54 45 5f 56 64 62 65 4c 69 73 74 69 6e 67 20  ITE_VdbeListing 
53c80 20 20 20 30 78 30 30 30 30 30 34 30 30 20 20 2f     0x00000400  /
53c90 2a 20 44 65 62 75 67 20 6c 69 73 74 69 6e 67 73  * Debug listings
53ca0 20 6f 66 20 56 44 42 45 20 70 72 6f 67 72 61 6d   of VDBE program
53cb0 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  s */.#define SQL
53cc0 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61 20  ITE_WriteSchema 
53cd0 20 20 20 30 78 30 30 30 30 30 38 30 30 20 20 2f     0x00000800  /
53ce0 2a 20 4f 4b 20 74 6f 20 75 70 64 61 74 65 20 53  * OK to update S
53cf0 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 2a 2f 0a  QLITE_MASTER */.
53d00 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e  #define SQLITE_N
53d10 6f 52 65 61 64 6c 6f 63 6b 20 20 20 20 20 30 78  oReadlock     0x
53d20 30 30 30 30 31 30 30 30 20 20 2f 2a 20 52 65 61  00001000  /* Rea
53d30 64 6c 6f 63 6b 73 20 61 72 65 20 6f 6d 69 74 74  dlocks are omitt
53d40 65 64 20 77 68 65 6e 20 0a 20 20 20 20 20 20 20  ed when .       
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 20 20 20 20 20                  
53d70 20 20 20 2a 2a 20 61 63 63 65 73 73 69 6e 67 20     ** accessing 
53d80 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61  read-only databa
53d90 73 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ses */.#define S
53da0 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63  QLITE_IgnoreChec
53db0 6b 73 20 20 20 30 78 30 30 30 30 32 30 30 30 20  ks   0x00002000 
53dc0 20 2f 2a 20 44 6f 20 6e 6f 74 20 65 6e 66 6f 72   /* Do not enfor
53dd0 63 65 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61  ce check constra
53de0 69 6e 74 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ints */.#define 
53df0 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d  SQLITE_ReadUncom
53e00 6d 69 74 74 65 64 20 30 78 30 30 30 30 34 30 30  mitted 0x0000400
53e10 30 20 2f 2a 20 46 6f 72 20 73 68 61 72 65 64 2d  0 /* For shared-
53e20 63 61 63 68 65 20 6d 6f 64 65 20 2a 2f 0a 23 64  cache mode */.#d
53e30 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 65 67  efine SQLITE_Leg
53e40 61 63 79 46 69 6c 65 46 6d 74 20 20 30 78 30 30  acyFileFmt  0x00
53e50 30 30 38 30 30 30 20 20 2f 2a 20 43 72 65 61 74  008000  /* Creat
53e60 65 20 6e 65 77 20 64 61 74 61 62 61 73 65 73 20  e new databases 
53e70 69 6e 20 66 6f 72 6d 61 74 20 31 20 2a 2f 0a 23  in format 1 */.#
53e80 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 75  define SQLITE_Fu
53e90 6c 6c 46 53 79 6e 63 20 20 20 20 20 20 30 78 30  llFSync      0x0
53ea0 30 30 31 30 30 30 30 20 20 2f 2a 20 55 73 65 20  0010000  /* Use 
53eb0 66 75 6c 6c 20 66 73 79 6e 63 20 6f 6e 20 74 68  full fsync on th
53ec0 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 23 64 65  e backend */.#de
53ed0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 6f 61 64  fine SQLITE_Load
53ee0 45 78 74 65 6e 73 69 6f 6e 20 20 30 78 30 30 30  Extension  0x000
53ef0 32 30 30 30 30 20 20 2f 2a 20 45 6e 61 62 6c 65  20000  /* Enable
53f00 20 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 20   load_extension 
53f10 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  */..#define SQLI
53f20 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 20  TE_RecoveryMode 
53f30 20 20 30 78 30 30 30 34 30 30 30 30 20 20 2f 2a    0x00040000  /*
53f40 20 49 67 6e 6f 72 65 20 73 63 68 65 6d 61 20 65   Ignore schema e
53f50 72 72 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65  rrors */.#define
53f60 20 53 51 4c 49 54 45 5f 53 68 61 72 65 64 43 61   SQLITE_SharedCa
53f70 63 68 65 20 20 20 20 30 78 30 30 30 38 30 30 30  che    0x0008000
53f80 30 20 20 2f 2a 20 43 61 63 68 65 20 73 68 61 72  0  /* Cache shar
53f90 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 20 2a  ing is enabled *
53fa0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
53fb0 5f 56 74 61 62 20 20 20 20 20 20 20 20 20 20 20  _Vtab           
53fc0 30 78 30 30 31 30 30 30 30 30 20 20 2f 2a 20 54  0x00100000  /* T
53fd0 68 65 72 65 20 65 78 69 73 74 73 20 61 20 76 69  here exists a vi
53fe0 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 0a  rtual table */..
53ff0 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76  /*.** Possible v
54000 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 71  alues for the sq
54010 6c 69 74 65 2e 6d 61 67 69 63 20 66 69 65 6c 64  lite.magic field
54020 2e 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 73  ..** The numbers
54030 20 61 72 65 20 6f 62 74 61 69 6e 65 64 20 61 74   are obtained at
54040 20 72 61 6e 64 6f 6d 20 61 6e 64 20 68 61 76 65   random and have
54050 20 6e 6f 20 73 70 65 63 69 61 6c 20 6d 65 61 6e   no special mean
54060 69 6e 67 2c 20 6f 74 68 65 72 0a 2a 2a 20 74 68  ing, other.** th
54070 61 6e 20 62 65 69 6e 67 20 64 69 73 74 69 6e 63  an being distinc
54080 74 20 66 72 6f 6d 20 6f 6e 65 20 61 6e 6f 74 68  t from one anoth
54090 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  er..*/.#define S
540a0 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e  QLITE_MAGIC_OPEN
540b0 20 20 20 20 20 30 78 61 30 32 39 61 36 39 37 20       0xa029a697 
540c0 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 73 20   /* Database is 
540d0 6f 70 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  open */.#define 
540e0 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 43 4c 4f  SQLITE_MAGIC_CLO
540f0 53 45 44 20 20 20 30 78 39 66 33 63 32 64 33 33  SED   0x9f3c2d33
54100 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 73    /* Database is
54110 20 63 6c 6f 73 65 64 20 2a 2f 0a 23 64 65 66 69   closed */.#defi
54120 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  ne SQLITE_MAGIC_
54130 53 49 43 4b 20 20 20 20 20 30 78 34 62 37 37 31  SICK     0x4b771
54140 32 39 30 20 20 2f 2a 20 45 72 72 6f 72 20 61 6e  290  /* Error an
54150 64 20 61 77 61 69 74 69 6e 67 20 63 6c 6f 73 65  d awaiting close
54160 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
54170 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 20 20  TE_MAGIC_BUSY   
54180 20 20 30 78 66 30 33 62 37 39 30 36 20 20 2f 2a    0xf03b7906  /*
54190 20 44 61 74 61 62 61 73 65 20 63 75 72 72 65 6e   Database curren
541a0 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a 23 64  tly in use */.#d
541b0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47  efine SQLITE_MAG
541c0 49 43 5f 45 52 52 4f 52 20 20 20 20 30 78 62 35  IC_ERROR    0xb5
541d0 33 35 37 39 33 30 20 20 2f 2a 20 41 6e 20 53 51  357930  /* An SQ
541e0 4c 49 54 45 5f 4d 49 53 55 53 45 20 65 72 72 6f  LITE_MISUSE erro
541f0 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 0a 2f  r occurred */../
54200 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 20 66 75  *.** Each SQL fu
54210 6e 63 74 69 6f 6e 20 69 73 20 64 65 66 69 6e 65  nction is define
54220 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65  d by an instance
54230 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
54240 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20  g.** structure. 
54250 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
54260 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20  is structure is 
54270 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71  stored in the sq
54280 6c 69 74 65 2e 61 46 75 6e 63 0a 2a 2a 20 68 61  lite.aFunc.** ha
54290 73 68 20 74 61 62 6c 65 2e 20 20 57 68 65 6e 20  sh table.  When 
542a0 6d 75 6c 74 69 70 6c 65 20 66 75 6e 63 74 69 6f  multiple functio
542b0 6e 73 20 68 61 76 65 20 74 68 65 20 73 61 6d 65  ns have the same
542c0 20 6e 61 6d 65 2c 20 74 68 65 20 68 61 73 68 20   name, the hash 
542d0 74 61 62 6c 65 0a 2a 2a 20 70 6f 69 6e 74 73 20  table.** points 
542e0 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74  to a linked list
542f0 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 74   of these struct
54300 75 72 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ures..*/.struct 
54310 46 75 6e 63 44 65 66 20 7b 0a 20 20 69 31 36 20  FuncDef {.  i16 
54320 6e 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20  nArg;           
54330 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72   /* Number of ar
54340 67 75 6d 65 6e 74 73 2e 20 20 2d 31 20 6d 65 61  guments.  -1 mea
54350 6e 73 20 75 6e 6c 69 6d 69 74 65 64 20 2a 2f 0a  ns unlimited */.
54360 20 20 75 38 20 69 50 72 65 66 45 6e 63 3b 20 20    u8 iPrefEnc;  
54370 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66 65 72         /* Prefer
54380 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  red text encodin
54390 67 20 28 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  g (SQLITE_UTF8, 
543a0 31 36 4c 45 2c 20 31 36 42 45 29 20 2a 2f 0a 20  16LE, 16BE) */. 
543b0 20 75 38 20 6e 65 65 64 43 6f 6c 6c 53 65 71 3b   u8 needCollSeq;
543c0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
543d0 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43   sqlite3GetFuncC
543e0 6f 6c 6c 53 65 71 28 29 20 6d 69 67 68 74 20 62  ollSeq() might b
543f0 65 20 63 61 6c 6c 65 64 20 2a 2f 0a 20 20 75 38  e called */.  u8
54400 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
54410 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69     /* Some combi
54420 6e 61 74 69 6f 6e 20 6f 66 20 53 51 4c 49 54 45  nation of SQLITE
54430 5f 46 55 4e 43 5f 2a 20 2a 2f 0a 20 20 76 6f 69  _FUNC_* */.  voi
54440 64 20 2a 70 55 73 65 72 44 61 74 61 3b 20 20 20  d *pUserData;   
54450 20 20 2f 2a 20 55 73 65 72 20 64 61 74 61 20 70    /* User data p
54460 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 46 75  arameter */.  Fu
54470 6e 63 44 65 66 20 2a 70 4e 65 78 74 3b 20 20 20  ncDef *pNext;   
54480 20 20 20 2f 2a 20 4e 65 78 74 20 66 75 6e 63 74     /* Next funct
54490 69 6f 6e 20 77 69 74 68 20 73 61 6d 65 20 6e 61  ion with same na
544a0 6d 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  me */.  void (*x
544b0 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f  Func)(sqlite3_co
544c0 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
544d0 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20  e3_value**); /* 
544e0 52 65 67 75 6c 61 72 20 66 75 6e 63 74 69 6f 6e  Regular function
544f0 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 53 74   */.  void (*xSt
54500 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ep)(sqlite3_cont
54510 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
54520 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 41 67  _value**); /* Ag
54530 67 72 65 67 61 74 65 20 73 74 65 70 20 2a 2f 0a  gregate step */.
54540 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 69    void (*xFinali
54550 7a 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ze)(sqlite3_cont
54560 65 78 74 2a 29 3b 20 20 20 20 20 20 20 20 20 20  ext*);          
54570 20 20 20 20 20 20 2f 2a 20 41 67 67 72 65 67 61        /* Aggrega
54580 74 65 20 66 69 6e 69 61 6c 69 7a 65 72 20 2a 2f  te finializer */
54590 0a 20 20 63 68 61 72 20 7a 4e 61 6d 65 5b 31 5d  .  char zName[1]
545a0 3b 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 6e  ;       /* SQL n
545b0 61 6d 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74  ame of the funct
545c0 69 6f 6e 2e 20 20 4d 55 53 54 20 42 45 20 4c 41  ion.  MUST BE LA
545d0 53 54 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ST */.};../*.** 
545e0 45 61 63 68 20 53 51 4c 69 74 65 20 6d 6f 64 75  Each SQLite modu
545f0 6c 65 20 28 76 69 72 74 75 61 6c 20 74 61 62 6c  le (virtual tabl
54600 65 20 64 65 66 69 6e 69 74 69 6f 6e 29 20 69 73  e definition) is
54610 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e 0a 2a   defined by an.*
54620 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  * instance of th
54630 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
54640 63 74 75 72 65 2c 20 73 74 6f 72 65 64 20 69 6e  cture, stored in
54650 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 4d 6f   the sqlite3.aMo
54660 64 75 6c 65 0a 2a 2a 20 68 61 73 68 20 74 61 62  dule.** hash tab
54670 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 6f  le..*/.struct Mo
54680 64 75 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73  dule {.  const s
54690 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
546a0 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20 20 2f 2a  Module;       /*
546b0 20 43 61 6c 6c 62 61 63 6b 20 70 6f 69 6e 74 65   Callback pointe
546c0 72 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  rs */.  const ch
546d0 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20  ar *zName;      
546e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
546f0 4e 61 6d 65 20 70 61 73 73 65 64 20 74 6f 20 63  Name passed to c
54700 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a  reate_module() *
54710 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20  /.  void *pAux; 
54720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54730 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41 75 78           /* pAux
54740 20 70 61 73 73 65 64 20 74 6f 20 63 72 65 61 74   passed to creat
54750 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20  e_module() */.  
54760 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29  void (*xDestroy)
54770 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 20 20 20  (void *);       
54780 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64       /* Module d
54790 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69  estructor functi
547a0 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  on */.};../*.** 
547b0 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20  Possible values 
547c0 66 6f 72 20 46 75 6e 63 44 65 66 2e 66 6c 61 67  for FuncDef.flag
547d0 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  s.*/.#define SQL
547e0 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 20 20  ITE_FUNC_LIKE   
547f0 30 78 30 31 20 20 2f 2a 20 43 61 6e 64 69 64 61  0x01  /* Candida
54800 74 65 20 66 6f 72 20 74 68 65 20 4c 49 4b 45 20  te for the LIKE 
54810 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 2a 2f 0a  optimization */.
54820 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46  #define SQLITE_F
54830 55 4e 43 5f 43 41 53 45 20 20 20 30 78 30 32 20  UNC_CASE   0x02 
54840 20 2f 2a 20 43 61 73 65 2d 73 65 6e 73 69 74 69   /* Case-sensiti
54850 76 65 20 4c 49 4b 45 2d 74 79 70 65 20 66 75 6e  ve LIKE-type fun
54860 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65  ction */.#define
54870 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48   SQLITE_FUNC_EPH
54880 45 4d 20 20 30 78 30 34 20 20 2f 2a 20 45 70 68  EM  0x04  /* Eph
54890 65 72 6d 65 72 61 6c 2e 20 20 44 65 6c 65 74 65  ermeral.  Delete
548a0 20 77 69 74 68 20 56 44 42 45 20 2a 2f 0a 0a 2f   with VDBE */../
548b0 2a 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  *.** information
548c0 20 61 62 6f 75 74 20 65 61 63 68 20 63 6f 6c 75   about each colu
548d0 6d 6e 20 6f 66 20 61 6e 20 53 51 4c 20 74 61 62  mn of an SQL tab
548e0 6c 65 20 69 73 20 68 65 6c 64 20 69 6e 20 61 6e  le is held in an
548f0 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20   instance.** of 
54900 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a  this structure..
54910 2a 2f 0a 73 74 72 75 63 74 20 43 6f 6c 75 6d 6e  */.struct Column
54920 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65   {.  char *zName
54930 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  ;     /* Name of
54940 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a   this column */.
54950 20 20 45 78 70 72 20 2a 70 44 66 6c 74 3b 20 20    Expr *pDflt;  
54960 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 76 61     /* Default va
54970 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75  lue of this colu
54980 6d 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54  mn */.  char *zT
54990 79 70 65 3b 20 20 20 20 20 2f 2a 20 44 61 74 61  ype;     /* Data
549a0 20 74 79 70 65 20 66 6f 72 20 74 68 69 73 20 63   type for this c
549b0 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68 61 72 20  olumn */.  char 
549c0 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 2f 2a 20 43  *zColl;     /* C
549d0 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
549e0 65 2e 20 20 49 66 20 4e 55 4c 4c 2c 20 75 73 65  e.  If NULL, use
549f0 20 74 68 65 20 64 65 66 61 75 6c 74 20 2a 2f 0a   the default */.
54a00 20 20 75 38 20 6e 6f 74 4e 75 6c 6c 3b 20 20 20    u8 notNull;   
54a10 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
54a20 65 72 65 20 69 73 20 61 20 4e 4f 54 20 4e 55 4c  ere is a NOT NUL
54a30 4c 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a  L constraint */.
54a40 20 20 75 38 20 69 73 50 72 69 6d 4b 65 79 3b 20    u8 isPrimKey; 
54a50 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
54a60 69 73 20 63 6f 6c 75 6d 6e 20 69 73 20 70 61 72  is column is par
54a70 74 20 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59  t of the PRIMARY
54a80 20 4b 45 59 20 2a 2f 0a 20 20 63 68 61 72 20 61   KEY */.  char a
54a90 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 4f 6e  ffinity;   /* On
54aa0 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f  e of the SQLITE_
54ab0 41 46 46 5f 2e 2e 2e 20 76 61 6c 75 65 73 20 2a  AFF_... values *
54ac0 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
54ad0 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
54ae0 4c 45 0a 20 20 75 38 20 69 73 48 69 64 64 65 6e  LE.  u8 isHidden
54af0 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  ;     /* True if
54b00 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 69 73 20   this column is 
54b10 27 68 69 64 64 65 6e 27 20 2a 2f 0a 23 65 6e 64  'hidden' */.#end
54b20 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 22  if.};../*.** A "
54b30 43 6f 6c 6c 61 74 69 6e 67 20 53 65 71 75 65 6e  Collating Sequen
54b40 63 65 22 20 69 73 20 64 65 66 69 6e 65 64 20 62  ce" is defined b
54b50 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  y an instance of
54b60 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a   the following.*
54b70 2a 20 73 74 72 75 63 74 75 72 65 2e 20 43 6f 6e  * structure. Con
54b80 63 65 70 74 75 61 6c 6c 79 2c 20 61 20 63 6f 6c  ceptually, a col
54b90 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
54ba0 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 6e 61  consists of a na
54bb0 6d 65 20 61 6e 64 0a 2a 2a 20 61 20 63 6f 6d 70  me and.** a comp
54bc0 61 72 69 73 6f 6e 20 72 6f 75 74 69 6e 65 20 74  arison routine t
54bd0 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 20  hat defines the 
54be0 6f 72 64 65 72 20 6f 66 20 74 68 61 74 20 73 65  order of that se
54bf0 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  quence..**.** Th
54c00 65 72 65 20 6d 61 79 20 74 77 6f 20 73 65 70 65  ere may two sepe
54c10 72 61 74 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  rate implementat
54c20 69 6f 6e 73 20 6f 66 20 74 68 65 20 63 6f 6c 6c  ions of the coll
54c30 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 20  ation function, 
54c40 6f 6e 65 0a 2a 2a 20 74 68 61 74 20 70 72 6f 63  one.** that proc
54c50 65 73 73 65 73 20 74 65 78 74 20 69 6e 20 55 54  esses text in UT
54c60 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 28 43 6f  F-8 encoding (Co
54c70 6c 6c 53 65 71 2e 78 43 6d 70 29 20 61 6e 64 20  llSeq.xCmp) and 
54c80 61 6e 6f 74 68 65 72 20 74 68 61 74 0a 2a 2a 20  another that.** 
54c90 70 72 6f 63 65 73 73 65 73 20 74 65 78 74 20 65  processes text e
54ca0 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 31 36  ncoded in UTF-16
54cb0 20 28 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 31 36   (CollSeq.xCmp16
54cc0 29 2c 20 75 73 69 6e 67 20 74 68 65 20 6d 61 63  ), using the mac
54cd0 68 69 6e 65 0a 2a 2a 20 6e 61 74 69 76 65 20 62  hine.** native b
54ce0 79 74 65 20 6f 72 64 65 72 2e 20 57 68 65 6e 20  yte order. When 
54cf0 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  a collation sequ
54d00 65 6e 63 65 20 69 73 20 69 6e 76 6f 6b 65 64 2c  ence is invoked,
54d10 20 53 51 4c 69 74 65 20 73 65 6c 65 63 74 73 0a   SQLite selects.
54d20 2a 2a 20 74 68 65 20 76 65 72 73 69 6f 6e 20 74  ** the version t
54d30 68 61 74 20 77 69 6c 6c 20 72 65 71 75 69 72 65  hat will require
54d40 20 74 68 65 20 6c 65 61 73 74 20 65 78 70 65 6e   the least expen
54d50 73 69 76 65 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a  sive encoding.**
54d60 20 74 72 61 6e 73 6c 61 74 69 6f 6e 73 2c 20 69   translations, i
54d70 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  f any..**.** The
54d80 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65 72 20 6d   CollSeq.pUser m
54d90 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65 20 69  ember variable i
54da0 73 20 61 6e 20 65 78 74 72 61 20 70 61 72 61 6d  s an extra param
54db0 65 74 65 72 20 74 68 61 74 20 70 61 73 73 65 64  eter that passed
54dc0 20 69 6e 0a 2a 2a 20 61 73 20 74 68 65 20 66 69   in.** as the fi
54dd0 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
54de0 74 68 65 20 55 54 46 2d 38 20 63 6f 6d 70 61 72  the UTF-8 compar
54df0 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 20 78  ison function, x
54e00 43 6d 70 2e 0a 2a 2a 20 43 6f 6c 6c 53 65 71 2e  Cmp..** CollSeq.
54e10 70 55 73 65 72 31 36 20 69 73 20 74 68 65 20 65  pUser16 is the e
54e20 71 75 69 76 61 6c 65 6e 74 20 66 6f 72 20 74 68  quivalent for th
54e30 65 20 55 54 46 2d 31 36 20 63 6f 6d 70 61 72 69  e UTF-16 compari
54e40 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a  son function,.**
54e50 20 78 43 6d 70 31 36 2e 0a 2a 2a 0a 2a 2a 20 49   xCmp16..**.** I
54e60 66 20 62 6f 74 68 20 43 6f 6c 6c 53 65 71 2e 78  f both CollSeq.x
54e70 43 6d 70 20 61 6e 64 20 43 6f 6c 6c 53 65 71 2e  Cmp and CollSeq.
54e80 78 43 6d 70 31 36 20 61 72 65 20 4e 55 4c 4c 2c  xCmp16 are NULL,
54e90 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74   it means that t
54ea0 68 65 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20  he.** collating 
54eb0 73 65 71 75 65 6e 63 65 20 69 73 20 75 6e 64 65  sequence is unde
54ec0 66 69 6e 65 64 2e 20 20 49 6e 64 69 63 65 73 20  fined.  Indices 
54ed0 62 75 69 6c 74 20 6f 6e 20 61 6e 20 75 6e 64 65  built on an unde
54ee0 66 69 6e 65 64 0a 2a 2a 20 63 6f 6c 6c 61 74 69  fined.** collati
54ef0 6e 67 20 73 65 71 75 65 6e 63 65 20 6d 61 79 20  ng sequence may 
54f00 6e 6f 74 20 62 65 20 72 65 61 64 20 6f 72 20 77  not be read or w
54f10 72 69 74 74 65 6e 2e 0a 2a 2f 0a 73 74 72 75 63  ritten..*/.struc
54f20 74 20 43 6f 6c 6c 53 65 71 20 7b 0a 20 20 63 68  t CollSeq {.  ch
54f30 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20  ar *zName;      
54f40 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
54f50 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  he collating seq
54f60 75 65 6e 63 65 2c 20 55 54 46 2d 38 20 65 6e 63  uence, UTF-8 enc
54f70 6f 64 65 64 20 2a 2f 0a 20 20 75 38 20 65 6e 63  oded */.  u8 enc
54f80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
54f90 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69 6e 67  /* Text encoding
54fa0 20 68 61 6e 64 6c 65 64 20 62 79 20 78 43 6d 70   handled by xCmp
54fb0 28 29 20 2a 2f 0a 20 20 75 38 20 74 79 70 65 3b  () */.  u8 type;
54fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
54fd0 20 4f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49   One of the SQLI
54fe0 54 45 5f 43 4f 4c 4c 5f 2e 2e 2e 20 76 61 6c 75  TE_COLL_... valu
54ff0 65 73 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 76 6f  es below */.  vo
55000 69 64 20 2a 70 55 73 65 72 3b 20 20 20 20 20 20  id *pUser;      
55010 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67      /* First arg
55020 75 6d 65 6e 74 20 74 6f 20 78 43 6d 70 28 29 20  ument to xCmp() 
55030 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6d 70 29  */.  int (*xCmp)
55040 28 76 6f 69 64 2a 2c 69 6e 74 2c 20 63 6f 6e 73  (void*,int, cons
55050 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 63 6f  t void*, int, co
55060 6e 73 74 20 76 6f 69 64 2a 29 3b 0a 20 20 76 6f  nst void*);.  vo
55070 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a  id (*xDel)(void*
55080 29 3b 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f  );  /* Destructo
55090 72 20 66 6f 72 20 70 55 73 65 72 20 2a 2f 0a 7d  r for pUser */.}
550a0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64  ;../*.** Allowed
550b0 20 76 61 6c 75 65 73 20 6f 66 20 43 6f 6c 6c 53   values of CollS
550c0 65 71 20 66 6c 61 67 73 3a 0a 2a 2f 0a 23 64 65  eq flags:.*/.#de
550d0 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c  fine SQLITE_COLL
550e0 5f 42 49 4e 41 52 59 20 20 31 20 20 2f 2a 20 54  _BINARY  1  /* T
550f0 68 65 20 64 65 66 61 75 6c 74 20 6d 65 6d 63 6d  he default memcm
55100 70 28 29 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  p() collating se
55110 71 75 65 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e  quence */.#defin
55120 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e 4f  e SQLITE_COLL_NO
55130 43 41 53 45 20 20 32 20 20 2f 2a 20 54 68 65 20  CASE  2  /* The 
55140 62 75 69 6c 74 2d 69 6e 20 4e 4f 43 41 53 45 20  built-in NOCASE 
55150 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
55160 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ce */.#define SQ
55170 4c 49 54 45 5f 43 4f 4c 4c 5f 52 45 56 45 52 53  LITE_COLL_REVERS
55180 45 20 33 20 20 2f 2a 20 54 68 65 20 62 75 69 6c  E 3  /* The buil
55190 74 2d 69 6e 20 52 45 56 45 52 53 45 20 63 6f 6c  t-in REVERSE col
551a0 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
551b0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
551c0 45 5f 43 4f 4c 4c 5f 55 53 45 52 20 20 20 20 30  E_COLL_USER    0
551d0 20 20 2f 2a 20 41 6e 79 20 6f 74 68 65 72 20 75    /* Any other u
551e0 73 65 72 2d 64 65 66 69 6e 65 64 20 63 6f 6c 6c  ser-defined coll
551f0 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a  ating sequence *
55200 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 6f 72 74 20  /../*.** A sort 
55210 6f 72 64 65 72 20 63 61 6e 20 62 65 20 65 69 74  order can be eit
55220 68 65 72 20 41 53 43 20 6f 72 20 44 45 53 43 2e  her ASC or DESC.
55230 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
55240 54 45 5f 53 4f 5f 41 53 43 20 20 20 20 20 20 20  TE_SO_ASC       
55250 30 20 20 2f 2a 20 53 6f 72 74 20 69 6e 20 61 73  0  /* Sort in as
55260 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a 2f  cending order */
55270 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
55280 53 4f 5f 44 45 53 43 20 20 20 20 20 20 31 20 20  SO_DESC      1  
55290 2f 2a 20 53 6f 72 74 20 69 6e 20 61 73 63 65 6e  /* Sort in ascen
552a0 64 69 6e 67 20 6f 72 64 65 72 20 2a 2f 0a 0a 2f  ding order */../
552b0 2a 0a 2a 2a 20 43 6f 6c 75 6d 6e 20 61 66 66 69  *.** Column affi
552c0 6e 69 74 79 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a  nity types..**.*
552d0 2a 20 54 68 65 73 65 20 75 73 65 64 20 74 6f 20  * These used to 
552e0 68 61 76 65 20 6d 6e 65 6d 6f 6e 69 63 20 6e 61  have mnemonic na
552f0 6d 65 20 6c 69 6b 65 20 27 69 27 20 66 6f 72 20  me like 'i' for 
55300 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47  SQLITE_AFF_INTEG
55310 45 52 20 61 6e 64 0a 2a 2a 20 27 74 27 20 66 6f  ER and.** 't' fo
55320 72 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  r SQLITE_AFF_TEX
55330 54 2e 20 20 42 75 74 20 77 65 20 63 61 6e 20 73  T.  But we can s
55340 61 76 65 20 61 20 6c 69 74 74 6c 65 20 73 70 61  ave a little spa
55350 63 65 20 61 6e 64 20 69 6d 70 72 6f 76 65 0a 2a  ce and improve.*
55360 2a 20 74 68 65 20 73 70 65 65 64 20 61 20 6c 69  * the speed a li
55370 74 74 6c 65 20 62 79 20 6e 75 6d 62 65 72 20 74  ttle by number t
55380 68 65 20 76 61 6c 75 65 73 20 63 6f 6e 73 65 63  he values consec
55390 75 74 69 76 65 6c 79 2e 20 20 0a 2a 2a 0a 2a 2a  utively.  .**.**
553a0 20 42 75 74 20 72 61 74 68 65 72 20 74 68 61 6e   But rather than
553b0 20 73 74 61 72 74 20 77 69 74 68 20 30 20 6f 72   start with 0 or
553c0 20 31 2c 20 77 65 20 62 65 67 69 6e 20 77 69 74   1, we begin wit
553d0 68 20 27 61 27 2e 20 20 54 68 61 74 20 77 61 79  h 'a'.  That way
553e0 2c 0a 2a 2a 20 77 68 65 6e 20 6d 75 6c 74 69 70  ,.** when multip
553f0 6c 65 20 61 66 66 69 6e 69 74 79 20 74 79 70 65  le affinity type
55400 73 20 61 72 65 20 63 6f 6e 63 61 74 65 6e 61 74  s are concatenat
55410 65 64 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67  ed into a string
55420 20 61 6e 64 0a 2a 2a 20 75 73 65 64 20 61 73 20   and.** used as 
55430 74 68 65 20 50 34 20 6f 70 65 72 61 6e 64 2c 20  the P4 operand, 
55440 74 68 65 79 20 77 69 6c 6c 20 62 65 20 6d 6f 72  they will be mor
55450 65 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a  e readable..**.*
55460 2a 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74  * Note also that
55470 20 74 68 65 20 6e 75 6d 65 72 69 63 20 74 79 70   the numeric typ
55480 65 73 20 61 72 65 20 67 72 6f 75 70 65 64 20 74  es are grouped t
55490 6f 67 65 74 68 65 72 20 73 6f 20 74 68 61 74 20  ogether so that 
554a0 74 65 73 74 69 6e 67 0a 2a 2a 20 66 6f 72 20 61  testing.** for a
554b0 20 6e 75 6d 65 72 69 63 20 74 79 70 65 20 69 73   numeric type is
554c0 20 61 20 73 69 6e 67 6c 65 20 63 6f 6d 70 61 72   a single compar
554d0 69 73 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ison..*/.#define
554e0 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
554f0 20 20 20 20 20 27 61 27 0a 23 64 65 66 69 6e 65       'a'.#define
55500 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45   SQLITE_AFF_NONE
55510 20 20 20 20 20 27 62 27 0a 23 64 65 66 69 6e 65       'b'.#define
55520 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45   SQLITE_AFF_NUME
55530 52 49 43 20 20 27 63 27 0a 23 64 65 66 69 6e 65  RIC  'c'.#define
55540 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45   SQLITE_AFF_INTE
55550 47 45 52 20 20 27 64 27 0a 23 64 65 66 69 6e 65  GER  'd'.#define
55560 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c   SQLITE_AFF_REAL
55570 20 20 20 20 20 27 65 27 0a 0a 23 64 65 66 69 6e       'e'..#defin
55580 65 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72  e sqlite3IsNumer
55590 69 63 41 66 66 69 6e 69 74 79 28 58 29 20 20 28  icAffinity(X)  (
555a0 28 58 29 3e 3d 53 51 4c 49 54 45 5f 41 46 46 5f  (X)>=SQLITE_AFF_
555b0 4e 55 4d 45 52 49 43 29 0a 0a 2f 2a 0a 2a 2a 20  NUMERIC)../*.** 
555c0 54 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d  The SQLITE_AFF_M
555d0 41 53 4b 20 76 61 6c 75 65 73 20 6d 61 73 6b 73  ASK values masks
555e0 20 6f 66 66 20 74 68 65 20 73 69 67 6e 69 66 69   off the signifi
555f0 63 61 6e 74 20 62 69 74 73 20 6f 66 20 61 6e 0a  cant bits of an.
55600 2a 2a 20 61 66 66 69 6e 69 74 79 20 76 61 6c 75  ** affinity valu
55610 65 2e 20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  e. .*/.#define S
55620 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 20  QLITE_AFF_MASK  
55630 20 20 20 30 78 36 37 0a 0a 2f 2a 0a 2a 2a 20 41     0x67../*.** A
55640 64 64 69 74 69 6f 6e 61 6c 20 62 69 74 20 76 61  dditional bit va
55650 6c 75 65 73 20 74 68 61 74 20 63 61 6e 20 62 65  lues that can be
55660 20 4f 52 65 64 20 77 69 74 68 20 61 6e 20 61 66   ORed with an af
55670 66 69 6e 69 74 79 20 77 69 74 68 6f 75 74 0a 2a  finity without.*
55680 2a 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 61  * changing the a
55690 66 66 69 6e 69 74 79 2e 0a 2a 2f 0a 23 64 65 66  ffinity..*/.#def
556a0 69 6e 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  ine SQLITE_JUMPI
556b0 46 4e 55 4c 4c 20 20 20 30 78 30 38 20 20 2f 2a  FNULL   0x08  /*
556c0 20 6a 75 6d 70 73 20 69 66 20 65 69 74 68 65 72   jumps if either
556d0 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c   operand is NULL
556e0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
556f0 54 45 5f 4e 55 4c 4c 45 51 55 41 4c 20 20 20 20  TE_NULLEQUAL    
55700 30 78 31 30 20 20 2f 2a 20 63 6f 6d 70 61 72 65  0x10  /* compare
55710 20 4e 55 4c 4c 73 20 65 71 75 61 6c 20 2a 2f 0a   NULLs equal */.
55720 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
55730 54 4f 52 45 50 32 20 20 20 20 20 20 30 78 38 30  TOREP2      0x80
55740 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c    /* Store resul
55750 74 20 69 6e 20 72 65 67 5b 50 32 5d 20 72 61 74  t in reg[P2] rat
55760 68 65 72 20 74 68 61 6e 20 6a 75 6d 70 20 2a 2f  her than jump */
55770 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c  ../*.** Each SQL
55780 20 74 61 62 6c 65 20 69 73 20 72 65 70 72 65 73   table is repres
55790 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20  ented in memory 
557a0 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  by an instance o
557b0 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69  f the.** followi
557c0 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ng structure..**
557d0 0a 2a 2a 20 54 61 62 6c 65 2e 7a 4e 61 6d 65 20  .** Table.zName 
557e0 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
557f0 68 65 20 74 61 62 6c 65 2e 20 20 54 68 65 20 63  he table.  The c
55800 61 73 65 20 6f 66 20 74 68 65 20 6f 72 69 67 69  ase of the origi
55810 6e 61 6c 0a 2a 2a 20 43 52 45 41 54 45 20 54 41  nal.** CREATE TA
55820 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73  BLE statement is
55830 20 73 74 6f 72 65 64 2c 20 62 75 74 20 63 61 73   stored, but cas
55840 65 20 69 73 20 6e 6f 74 20 73 69 67 6e 69 66 69  e is not signifi
55850 63 61 6e 74 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70  cant for.** comp
55860 61 72 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54  arisons..**.** T
55870 61 62 6c 65 2e 6e 43 6f 6c 20 69 73 20 74 68 65  able.nCol is the
55880 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
55890 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65  ns in this table
558a0 2e 20 20 54 61 62 6c 65 2e 61 43 6f 6c 20 69 73  .  Table.aCol is
558b0 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f   a.** pointer to
558c0 20 61 6e 20 61 72 72 61 79 20 6f 66 20 43 6f 6c   an array of Col
558d0 75 6d 6e 20 73 74 72 75 63 74 75 72 65 73 2c 20  umn structures, 
558e0 6f 6e 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c  one for each col
558f0 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  umn..**.** If th
55900 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20 49  e table has an I
55910 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
55920 45 59 2c 20 74 68 65 6e 20 54 61 62 6c 65 2e 69  EY, then Table.i
55930 50 4b 65 79 20 69 73 20 74 68 65 20 69 6e 64 65  PKey is the inde
55940 78 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75  x of.** the colu
55950 6d 6e 20 74 68 61 74 20 69 73 20 74 68 61 74 20  mn that is that 
55960 6b 65 79 2e 20 20 20 4f 74 68 65 72 77 69 73 65  key.   Otherwise
55970 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20   Table.iPKey is 
55980 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f 74 65 0a  negative.  Note.
55990 2a 2a 20 74 68 61 74 20 74 68 65 20 64 61 74 61  ** that the data
559a0 74 79 70 65 20 6f 66 20 74 68 65 20 50 52 49 4d  type of the PRIM
559b0 41 52 59 20 4b 45 59 20 6d 75 73 74 20 62 65 20  ARY KEY must be 
559c0 49 4e 54 45 47 45 52 20 66 6f 72 20 74 68 69 73  INTEGER for this
559d0 20 66 69 65 6c 64 20 74 6f 0a 2a 2a 20 62 65 20   field to.** be 
559e0 73 65 74 2e 20 20 41 6e 20 49 4e 54 45 47 45 52  set.  An INTEGER
559f0 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 73 20   PRIMARY KEY is 
55a00 75 73 65 64 20 61 73 20 74 68 65 20 72 6f 77 69  used as the rowi
55a10 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 6f  d for each row o
55a20 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 2e 20  f.** the table. 
55a30 20 49 66 20 61 20 74 61 62 6c 65 20 68 61 73 20   If a table has 
55a40 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  no INTEGER PRIMA
55a50 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 61 20 72  RY KEY, then a r
55a60 61 6e 64 6f 6d 20 72 6f 77 69 64 0a 2a 2a 20 69  andom rowid.** i
55a70 73 20 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20  s generated for 
55a80 65 61 63 68 20 72 6f 77 20 6f 66 20 74 68 65 20  each row of the 
55a90 74 61 62 6c 65 2e 20 20 54 61 62 6c 65 2e 68 61  table.  Table.ha
55aa0 73 50 72 69 6d 4b 65 79 20 69 73 20 74 72 75 65  sPrimKey is true
55ab0 20 69 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65   if.** the table
55ac0 20 68 61 73 20 61 6e 79 20 50 52 49 4d 41 52 59   has any PRIMARY
55ad0 20 4b 45 59 2c 20 49 4e 54 45 47 45 52 20 6f 72   KEY, INTEGER or
55ae0 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a   otherwise..**.*
55af0 2a 20 54 61 62 6c 65 2e 74 6e 75 6d 20 69 73 20  * Table.tnum is 
55b00 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  the page number 
55b10 66 6f 72 20 74 68 65 20 72 6f 6f 74 20 42 54 72  for the root BTr
55b20 65 65 20 70 61 67 65 20 6f 66 20 74 68 65 20 74  ee page of the t
55b30 61 62 6c 65 20 69 6e 20 74 68 65 0a 2a 2a 20 64  able in the.** d
55b40 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49  atabase file.  I
55b50 66 20 54 61 62 6c 65 2e 69 44 62 20 69 73 20 74  f Table.iDb is t
55b60 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
55b70 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 62  database table b
55b80 61 63 6b 65 6e 64 0a 2a 2a 20 69 6e 20 73 71 6c  ackend.** in sql
55b90 69 74 65 2e 61 44 62 5b 5d 2e 20 20 30 20 69 73  ite.aDb[].  0 is
55ba0 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61   for the main da
55bb0 74 61 62 61 73 65 20 61 6e 64 20 31 20 69 73 20  tabase and 1 is 
55bc0 66 6f 72 20 74 68 65 20 66 69 6c 65 20 74 68 61  for the file tha
55bd0 74 0a 2a 2a 20 68 6f 6c 64 73 20 74 65 6d 70 6f  t.** holds tempo
55be0 72 61 72 79 20 74 61 62 6c 65 73 20 61 6e 64 20  rary tables and 
55bf0 69 6e 64 69 63 65 73 2e 20 20 49 66 20 54 61 62  indices.  If Tab
55c00 6c 65 2e 69 73 45 70 68 65 6d 0a 2a 2a 20 69 73  le.isEphem.** is
55c10 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20   true, then the 
55c20 74 61 62 6c 65 20 69 73 20 73 74 6f 72 65 64 20  table is stored 
55c30 69 6e 20 61 20 66 69 6c 65 20 74 68 61 74 20 69  in a file that i
55c40 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  s automatically 
55c50 64 65 6c 65 74 65 64 0a 2a 2a 20 77 68 65 6e 20  deleted.** when 
55c60 74 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20  the VDBE cursor 
55c70 74 6f 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  to the table is 
55c80 63 6c 6f 73 65 64 2e 20 20 49 6e 20 74 68 69 73  closed.  In this
55c90 20 63 61 73 65 20 54 61 62 6c 65 2e 74 6e 75 6d   case Table.tnum
55ca0 20 0a 2a 2a 20 72 65 66 65 72 73 20 56 44 42 45   .** refers VDBE
55cb0 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 74   cursor number t
55cc0 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 74 61  hat holds the ta
55cd0 62 6c 65 20 6f 70 65 6e 2c 20 6e 6f 74 20 74 6f  ble open, not to
55ce0 20 74 68 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67   the root.** pag
55cf0 65 20 6e 75 6d 62 65 72 2e 20 20 54 72 61 6e 73  e number.  Trans
55d00 69 65 6e 74 20 74 61 62 6c 65 73 20 61 72 65 20  ient tables are 
55d10 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65  used to hold the
55d20 20 72 65 73 75 6c 74 73 20 6f 66 20 61 0a 2a 2a   results of a.**
55d30 20 73 75 62 2d 71 75 65 72 79 20 74 68 61 74 20   sub-query that 
55d40 61 70 70 65 61 72 73 20 69 6e 73 74 65 61 64 20  appears instead 
55d50 6f 66 20 61 20 72 65 61 6c 20 74 61 62 6c 65 20  of a real table 
55d60 6e 61 6d 65 20 69 6e 20 74 68 65 20 46 52 4f 4d  name in the FROM
55d70 20 63 6c 61 75 73 65 20 0a 2a 2a 20 6f 66 20 61   clause .** of a
55d80 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
55d90 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 54 61 62  t..*/.struct Tab
55da0 6c 65 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61  le {.  char *zNa
55db0 6d 65 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  me;     /* Name 
55dc0 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  of the table */.
55dd0 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20    int nCol;     
55de0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
55df0 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 20  columns in this 
55e00 74 61 62 6c 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d  table */.  Colum
55e10 6e 20 2a 61 43 6f 6c 3b 20 20 20 20 2f 2a 20 49  n *aCol;    /* I
55e20 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
55e30 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a   each column */.
55e40 20 20 69 6e 74 20 69 50 4b 65 79 3b 20 20 20 20    int iPKey;    
55e50 20 20 20 2f 2a 20 49 66 20 6e 6f 74 20 6c 65 73     /* If not les
55e60 73 20 74 68 65 6e 20 30 2c 20 75 73 65 20 61 43  s then 0, use aC
55e70 6f 6c 5b 69 50 4b 65 79 5d 20 61 73 20 74 68 65  ol[iPKey] as the
55e80 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a   primary key */.
55e90 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b    Index *pIndex;
55ea0 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 53 51     /* List of SQ
55eb0 4c 20 69 6e 64 65 78 65 73 20 6f 6e 20 74 68 69  L indexes on thi
55ec0 73 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 69 6e  s table. */.  in
55ed0 74 20 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 2f  t tnum;        /
55ee0 2a 20 52 6f 6f 74 20 42 54 72 65 65 20 6e 6f 64  * Root BTree nod
55ef0 65 20 66 6f 72 20 74 68 69 73 20 74 61 62 6c 65  e for this table
55f00 20 28 73 65 65 20 6e 6f 74 65 20 61 62 6f 76 65   (see note above
55f10 29 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  ) */.  Select *p
55f20 53 65 6c 65 63 74 3b 20 2f 2a 20 4e 55 4c 4c 20  Select; /* NULL 
55f30 66 6f 72 20 74 61 62 6c 65 73 2e 20 20 50 6f 69  for tables.  Poi
55f40 6e 74 73 20 74 6f 20 64 65 66 69 6e 69 74 69 6f  nts to definitio
55f50 6e 20 69 66 20 61 20 76 69 65 77 2e 20 2a 2f 0a  n if a view. */.
55f60 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20    int nRef;     
55f70 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
55f80 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68  f pointers to th
55f90 69 73 20 54 61 62 6c 65 20 2a 2f 0a 20 20 54 72  is Table */.  Tr
55fa0 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b  igger *pTrigger;
55fb0 20 2f 2a 20 4c 69 73 74 20 6f 66 20 53 51 4c 20   /* List of SQL 
55fc0 74 72 69 67 67 65 72 73 20 6f 6e 20 74 68 69 73  triggers on this
55fd0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 46 4b 65 79   table */.  FKey
55fe0 20 2a 70 46 4b 65 79 3b 20 20 20 20 20 20 20 2f   *pFKey;       /
55ff0 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66  * Linked list of
56000 20 61 6c 6c 20 66 6f 72 65 69 67 6e 20 6b 65 79   all foreign key
56010 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20  s in this table 
56020 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41  */.  char *zColA
56030 66 66 3b 20 20 20 20 20 2f 2a 20 53 74 72 69 6e  ff;     /* Strin
56040 67 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 61  g defining the a
56050 66 66 69 6e 69 74 79 20 6f 66 20 65 61 63 68 20  ffinity of each 
56060 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 69 66 6e 64 65  column */.#ifnde
56070 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48  f SQLITE_OMIT_CH
56080 45 43 4b 0a 20 20 45 78 70 72 20 2a 70 43 68 65  ECK.  Expr *pChe
56090 63 6b 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ck;      /* The 
560a0 41 4e 44 20 6f 66 20 61 6c 6c 20 43 48 45 43 4b  AND of all CHECK
560b0 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a   constraints */.
560c0 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53  #endif.#ifndef S
560d0 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52  QLITE_OMIT_ALTER
560e0 54 41 42 4c 45 0a 20 20 69 6e 74 20 61 64 64 43  TABLE.  int addC
560f0 6f 6c 4f 66 66 73 65 74 3b 20 20 2f 2a 20 4f 66  olOffset;  /* Of
56100 66 73 65 74 20 69 6e 20 43 52 45 41 54 45 20 54  fset in CREATE T
56110 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 74  ABLE statement t
56120 6f 20 61 64 64 20 61 20 6e 65 77 20 63 6f 6c 75  o add a new colu
56130 6d 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75  mn */.#endif.  u
56140 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20  8 readOnly;     
56150 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20  /* True if this 
56160 74 61 62 6c 65 20 73 68 6f 75 6c 64 20 6e 6f 74  table should not
56170 20 62 65 20 77 72 69 74 74 65 6e 20 62 79 20 74   be written by t
56180 68 65 20 75 73 65 72 20 2a 2f 0a 20 20 75 38 20  he user */.  u8 
56190 69 73 45 70 68 65 6d 3b 20 20 20 20 20 20 2f 2a  isEphem;      /*
561a0 20 54 72 75 65 20 69 66 20 63 72 65 61 74 65 64   True if created
561b0 20 75 73 69 6e 67 20 4f 50 5f 4f 70 65 6e 45 70   using OP_OpenEp
561c0 68 65 72 6d 65 72 61 6c 20 2a 2f 0a 20 20 75 38  hermeral */.  u8
561d0 20 68 61 73 50 72 69 6d 4b 65 79 3b 20 20 20 2f   hasPrimKey;   /
561e0 2a 20 54 72 75 65 20 69 66 20 74 68 65 72 65 20  * True if there 
561f0 65 78 69 73 74 73 20 61 20 70 72 69 6d 61 72 79  exists a primary
56200 20 6b 65 79 20 2a 2f 0a 20 20 75 38 20 6b 65 79   key */.  u8 key
56210 43 6f 6e 66 3b 20 20 20 20 20 20 2f 2a 20 57 68  Conf;      /* Wh
56220 61 74 20 74 6f 20 64 6f 20 69 6e 20 63 61 73 65  at to do in case
56230 20 6f 66 20 75 6e 69 71 75 65 6e 65 73 73 20 63   of uniqueness c
56240 6f 6e 66 6c 69 63 74 20 6f 6e 20 69 50 4b 65 79  onflict on iPKey
56250 20 2a 2f 0a 20 20 75 38 20 61 75 74 6f 49 6e 63   */.  u8 autoInc
56260 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  ;      /* True i
56270 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 70 72  f the integer pr
56280 69 6d 61 72 79 20 6b 65 79 20 69 73 20 61 75 74  imary key is aut
56290 6f 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a 23 69  oincrement */.#i
562a0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
562b0 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
562c0 20 75 38 20 69 73 56 69 72 74 75 61 6c 3b 20 20   u8 isVirtual;  
562d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
562e0 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20  ue if this is a 
562f0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f  virtual table */
56300 0a 20 20 75 38 20 69 73 43 6f 6d 6d 69 74 3b 20  .  u8 isCommit; 
56310 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
56320 54 72 75 65 20 6f 6e 63 65 20 74 68 65 20 43 52  True once the CR
56330 45 41 54 45 20 54 41 42 4c 45 20 68 61 73 20 62  EATE TABLE has b
56340 65 65 6e 20 63 6f 6d 6d 69 74 74 65 64 20 2a 2f  een committed */
56350 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b  .  Module *pMod;
56360 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
56370 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 69  Pointer to the i
56380 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
56390 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20   the module */. 
563a0 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
563b0 56 74 61 62 3b 20 20 20 20 20 20 2f 2a 20 50 6f  Vtab;      /* Po
563c0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 6f 64  inter to the mod
563d0 75 6c 65 20 69 6e 73 74 61 6e 63 65 20 2a 2f 0a  ule instance */.
563e0 20 20 69 6e 74 20 6e 4d 6f 64 75 6c 65 41 72 67    int nModuleArg
563f0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
56400 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
56410 74 73 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65  ts to the module
56420 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 4d   */.  char **azM
56430 6f 64 75 6c 65 41 72 67 3b 20 20 20 20 20 20 20  oduleArg;       
56440 2f 2a 20 54 65 78 74 20 6f 66 20 61 6c 6c 20 6d  /* Text of all m
56450 6f 64 75 6c 65 20 61 72 67 73 2e 20 5b 30 5d 20  odule args. [0] 
56460 69 73 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 2a  is module name *
56470 2f 0a 23 65 6e 64 69 66 0a 20 20 53 63 68 65 6d  /.#endif.  Schem
56480 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 20  a *pSchema;     
56490 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 74       /* Schema t
564a0 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 69  hat contains thi
564b0 73 20 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f  s table */.};../
564c0 2a 0a 2a 2a 20 54 65 73 74 20 74 6f 20 73 65 65  *.** Test to see
564d0 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
564e0 61 20 74 61 62 6c 65 20 69 73 20 61 20 76 69 72  a table is a vir
564f0 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69  tual table.  Thi
56500 73 20 69 73 0a 2a 2a 20 64 6f 6e 65 20 61 73 20  s is.** done as 
56510 61 20 6d 61 63 72 6f 20 73 6f 20 74 68 61 74 20  a macro so that 
56520 69 74 20 77 69 6c 6c 20 62 65 20 6f 70 74 69 6d  it will be optim
56530 69 7a 65 64 20 6f 75 74 20 77 68 65 6e 20 76 69  ized out when vi
56540 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 73  rtual.** table s
56550 75 70 70 6f 72 74 20 69 73 20 6f 6d 69 74 74 65  upport is omitte
56560 64 20 66 72 6f 6d 20 74 68 65 20 62 75 69 6c 64  d from the build
56570 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
56580 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
56590 54 41 42 4c 45 0a 23 20 20 64 65 66 69 6e 65 20  TABLE.#  define 
565a0 49 73 56 69 72 74 75 61 6c 28 58 29 20 20 20 20  IsVirtual(X)    
565b0 20 20 28 28 58 29 2d 3e 69 73 56 69 72 74 75 61    ((X)->isVirtua
565c0 6c 29 0a 23 20 20 64 65 66 69 6e 65 20 49 73 48  l).#  define IsH
565d0 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 28  iddenColumn(X) (
565e0 28 58 29 2d 3e 69 73 48 69 64 64 65 6e 29 0a 23  (X)->isHidden).#
565f0 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49  else.#  define I
56600 73 56 69 72 74 75 61 6c 28 58 29 20 20 20 20 20  sVirtual(X)     
56610 20 30 0a 23 20 20 64 65 66 69 6e 65 20 49 73 48   0.#  define IsH
56620 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 30  iddenColumn(X) 0
56630 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45  .#endif../*.** E
56640 61 63 68 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  ach foreign key 
56650 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 61 6e  constraint is an
56660 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
56670 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
56680 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 66 6f  ture..**.** A fo
56690 72 65 69 67 6e 20 6b 65 79 20 69 73 20 61 73 73  reign key is ass
566a0 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 77 6f  ociated with two
566b0 20 74 61 62 6c 65 73 2e 20 20 54 68 65 20 22 66   tables.  The "f
566c0 72 6f 6d 22 20 74 61 62 6c 65 20 69 73 0a 2a 2a  rom" table is.**
566d0 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20   the table that 
566e0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 52 45 46  contains the REF
566f0 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 74  ERENCES clause t
56700 68 61 74 20 63 72 65 61 74 65 73 20 74 68 65 20  hat creates the 
56710 66 6f 72 65 69 67 6e 0a 2a 2a 20 6b 65 79 2e 20  foreign.** key. 
56720 20 54 68 65 20 22 74 6f 22 20 74 61 62 6c 65 20   The "to" table 
56730 69 73 20 74 68 65 20 74 61 62 6c 65 20 74 68 61  is the table tha
56740 74 20 69 73 20 6e 61 6d 65 64 20 69 6e 20 74 68  t is named in th
56750 65 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61  e REFERENCES cla
56760 75 73 65 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72  use..** Consider
56770 20 74 68 69 73 20 65 78 61 6d 70 6c 65 3a 0a 2a   this example:.*
56780 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20  *.**     CREATE 
56790 54 41 42 4c 45 20 65 78 31 28 0a 2a 2a 20 20 20  TABLE ex1(.**   
567a0 20 20 20 20 61 20 49 4e 54 45 47 45 52 20 50 52      a INTEGER PR
567b0 49 4d 41 52 59 20 4b 45 59 2c 0a 2a 2a 20 20 20  IMARY KEY,.**   
567c0 20 20 20 20 62 20 49 4e 54 45 47 45 52 20 43 4f      b INTEGER CO
567d0 4e 53 54 52 41 49 4e 54 20 66 6b 31 20 52 45 46  NSTRAINT fk1 REF
567e0 45 52 45 4e 43 45 53 20 65 78 32 28 78 29 0a 2a  ERENCES ex2(x).*
567f0 2a 20 20 20 20 20 29 3b 0a 2a 2a 0a 2a 2a 20 46  *     );.**.** F
56800 6f 72 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 22  or foreign key "
56810 66 6b 31 22 2c 20 74 68 65 20 66 72 6f 6d 2d 74  fk1", the from-t
56820 61 62 6c 65 20 69 73 20 22 65 78 31 22 20 61 6e  able is "ex1" an
56830 64 20 74 68 65 20 74 6f 2d 74 61 62 6c 65 20 69  d the to-table i
56840 73 20 22 65 78 32 22 2e 0a 2a 2a 0a 2a 2a 20 45  s "ex2"..**.** E
56850 61 63 68 20 52 45 46 45 52 45 4e 43 45 53 20 63  ach REFERENCES c
56860 6c 61 75 73 65 20 67 65 6e 65 72 61 74 65 73 20  lause generates 
56870 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
56880 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
56890 75 63 74 75 72 65 0a 2a 2a 20 77 68 69 63 68 20  ucture.** which 
568a0 69 73 20 61 74 74 61 63 68 65 64 20 74 6f 20 74  is attached to t
568b0 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65 2e 20 20  he from-table.  
568c0 54 68 65 20 74 6f 2d 74 61 62 6c 65 20 6e 65 65  The to-table nee
568d0 64 20 6e 6f 74 20 65 78 69 73 74 20 77 68 65 6e  d not exist when
568e0 0a 2a 2a 20 74 68 65 20 66 72 6f 6d 2d 74 61 62  .** the from-tab
568f0 6c 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 20  le is created.  
56900 54 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66  The existance of
56910 20 74 68 65 20 74 6f 2d 74 61 62 6c 65 20 69 73   the to-table is
56920 20 6e 6f 74 20 63 68 65 63 6b 65 64 0a 2a 2a 20   not checked.** 
56930 75 6e 74 69 6c 20 61 6e 20 61 74 74 65 6d 70 74  until an attempt
56940 20 69 73 20 6d 61 64 65 20 74 6f 20 69 6e 73 65   is made to inse
56950 72 74 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65  rt data into the
56960 20 66 72 6f 6d 2d 74 61 62 6c 65 2e 0a 2a 2a 0a   from-table..**.
56970 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 61 46  ** The sqlite.aF
56980 4b 65 79 20 68 61 73 68 20 74 61 62 6c 65 20 73  Key hash table s
56990 74 6f 72 65 73 20 70 6f 69 6e 74 65 72 73 20 74  tores pointers t
569a0 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  o this structure
569b0 0a 2a 2a 20 67 69 76 65 6e 20 74 68 65 20 6e 61  .** given the na
569c0 6d 65 20 6f 66 20 61 20 74 6f 2d 74 61 62 6c 65  me of a to-table
569d0 2e 20 20 46 6f 72 20 65 61 63 68 20 74 6f 2d 74  .  For each to-t
569e0 61 62 6c 65 2c 20 61 6c 6c 20 66 6f 72 65 69 67  able, all foreig
569f0 6e 20 6b 65 79 73 0a 2a 2a 20 61 73 73 6f 63 69  n keys.** associ
56a00 61 74 65 64 20 77 69 74 68 20 74 68 61 74 20 74  ated with that t
56a10 61 62 6c 65 20 61 72 65 20 6f 6e 20 61 20 6c 69  able are on a li
56a20 6e 6b 65 64 20 6c 69 73 74 20 75 73 69 6e 67 20  nked list using 
56a30 74 68 65 20 46 4b 65 79 2e 70 4e 65 78 74 54 6f  the FKey.pNextTo
56a40 0a 2a 2a 20 66 69 65 6c 64 2e 0a 2a 2f 0a 73 74  .** field..*/.st
56a50 72 75 63 74 20 46 4b 65 79 20 7b 0a 20 20 54 61  ruct FKey {.  Ta
56a60 62 6c 65 20 2a 70 46 72 6f 6d 3b 20 20 20 20 20  ble *pFrom;     
56a70 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68 61  /* The table tha
56a80 74 20 63 6f 6e 73 74 61 69 6e 73 20 74 68 65 20  t constains the 
56a90 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73  REFERENCES claus
56aa0 65 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65  e */.  FKey *pNe
56ab0 78 74 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74  xtFrom;  /* Next
56ac0 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e 20   foreign key in 
56ad0 70 46 72 6f 6d 20 2a 2f 0a 20 20 63 68 61 72 20  pFrom */.  char 
56ae0 2a 7a 54 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20  *zTo;        /* 
56af0 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 74 68  Name of table th
56b00 61 74 20 74 68 65 20 6b 65 79 20 70 6f 69 6e 74  at the key point
56b10 73 20 74 6f 20 2a 2f 0a 20 20 46 4b 65 79 20 2a  s to */.  FKey *
56b20 70 4e 65 78 74 54 6f 3b 20 20 20 20 2f 2a 20 4e  pNextTo;    /* N
56b30 65 78 74 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  ext foreign key 
56b40 74 68 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 7a  that points to z
56b50 54 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c  To */.  int nCol
56b60 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ;         /* Num
56b70 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
56b80 6e 20 74 68 69 73 20 6b 65 79 20 2a 2f 0a 20 20  n this key */.  
56b90 73 74 72 75 63 74 20 73 43 6f 6c 4d 61 70 20 7b  struct sColMap {
56ba0 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 6f 66 20    /* Mapping of 
56bb0 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 46 72 6f 6d  columns in pFrom
56bc0 20 74 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 7a   to columns in z
56bd0 54 6f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 46  To */.    int iF
56be0 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  rom;         /* 
56bf0 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20  Index of column 
56c00 69 6e 20 70 46 72 6f 6d 20 2a 2f 0a 20 20 20 20  in pFrom */.    
56c10 63 68 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20  char *zCol;     
56c20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 63 6f     /* Name of co
56c30 6c 75 6d 6e 20 69 6e 20 7a 54 6f 2e 20 20 49 66  lumn in zTo.  If
56c40 20 30 20 75 73 65 20 50 52 49 4d 41 52 59 20 4b   0 use PRIMARY K
56c50 45 59 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b  EY */.  } *aCol;
56c60 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
56c70 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20   entry for each 
56c80 6f 66 20 6e 43 6f 6c 20 63 6f 6c 75 6d 6e 20 73  of nCol column s
56c90 20 2a 2f 0a 20 20 75 38 20 69 73 44 65 66 65 72   */.  u8 isDefer
56ca0 72 65 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20  red;    /* True 
56cb0 69 66 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68  if constraint ch
56cc0 65 63 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72  ecking is deferr
56cd0 65 64 20 74 69 6c 6c 20 43 4f 4d 4d 49 54 20 2a  ed till COMMIT *
56ce0 2f 0a 20 20 75 38 20 75 70 64 61 74 65 43 6f 6e  /.  u8 updateCon
56cf0 66 3b 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20  f;    /* How to 
56d00 72 65 73 6f 6c 76 65 20 63 6f 6e 66 6c 69 63 74  resolve conflict
56d10 73 20 74 68 61 74 20 6f 63 63 75 72 20 6f 6e 20  s that occur on 
56d20 55 50 44 41 54 45 20 2a 2f 0a 20 20 75 38 20 64  UPDATE */.  u8 d
56d30 65 6c 65 74 65 43 6f 6e 66 3b 20 20 20 20 2f 2a  eleteConf;    /*
56d40 20 48 6f 77 20 74 6f 20 72 65 73 6f 6c 76 65 20   How to resolve 
56d50 63 6f 6e 66 6c 69 63 74 73 20 74 68 61 74 20 6f  conflicts that o
56d60 63 63 75 72 20 6f 6e 20 44 45 4c 45 54 45 20 2a  ccur on DELETE *
56d70 2f 0a 20 20 75 38 20 69 6e 73 65 72 74 43 6f 6e  /.  u8 insertCon
56d80 66 3b 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20  f;    /* How to 
56d90 72 65 73 6f 6c 76 65 20 63 6f 6e 66 6c 69 63 74  resolve conflict
56da0 73 20 74 68 61 74 20 6f 63 63 75 72 20 6f 6e 20  s that occur on 
56db0 49 4e 53 45 52 54 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  INSERT */.};../*
56dc0 0a 2a 2a 20 53 51 4c 69 74 65 20 73 75 70 70 6f  .** SQLite suppo
56dd0 72 74 73 20 6d 61 6e 79 20 64 69 66 66 65 72 65  rts many differe
56de0 6e 74 20 77 61 79 73 20 74 6f 20 72 65 73 6f 6c  nt ways to resol
56df0 76 65 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a  ve a constraint.
56e00 2a 2a 20 65 72 72 6f 72 2e 20 20 52 4f 4c 4c 42  ** error.  ROLLB
56e10 41 43 4b 20 70 72 6f 63 65 73 73 69 6e 67 20 6d  ACK processing m
56e20 65 61 6e 73 20 74 68 61 74 20 61 20 63 6f 6e 73  eans that a cons
56e30 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e  traint violation
56e40 0a 2a 2a 20 63 61 75 73 65 73 20 74 68 65 20 6f  .** causes the o
56e50 70 65 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63  peration in proc
56e60 65 73 73 20 74 6f 20 66 61 69 6c 20 61 6e 64 20  ess to fail and 
56e70 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
56e80 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 74  transaction.** t
56e90 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b  o be rolled back
56ea0 2e 20 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73  .  ABORT process
56eb0 69 6e 67 20 6d 65 61 6e 73 20 74 68 65 20 6f 70  ing means the op
56ec0 65 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65  eration in proce
56ed0 73 73 0a 2a 2a 20 66 61 69 6c 73 20 61 6e 64 20  ss.** fails and 
56ee0 61 6e 79 20 70 72 69 6f 72 20 63 68 61 6e 67 65  any prior change
56ef0 73 20 66 72 6f 6d 20 74 68 61 74 20 6f 6e 65 20  s from that one 
56f00 6f 70 65 72 61 74 69 6f 6e 20 61 72 65 20 62 61  operation are ba
56f10 63 6b 65 64 20 6f 75 74 2c 0a 2a 2a 20 62 75 74  cked out,.** but
56f20 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
56f30 20 69 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62   is not rolled b
56f40 61 63 6b 2e 20 20 46 41 49 4c 20 70 72 6f 63 65  ack.  FAIL proce
56f50 73 73 69 6e 67 20 6d 65 61 6e 73 20 74 68 61 74  ssing means that
56f60 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61 74 69 6f  .** the operatio
56f70 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 20 73 74  n in progress st
56f80 6f 70 73 20 61 6e 64 20 72 65 74 75 72 6e 73 20  ops and returns 
56f90 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20  an error code.  
56fa0 42 75 74 20 70 72 69 6f 72 0a 2a 2a 20 63 68 61  But prior.** cha
56fb0 6e 67 65 73 20 64 75 65 20 74 6f 20 74 68 65 20  nges due to the 
56fc0 73 61 6d 65 20 6f 70 65 72 61 74 69 6f 6e 20 61  same operation a
56fd0 72 65 20 6e 6f 74 20 62 61 63 6b 65 64 20 6f 75  re not backed ou
56fe0 74 20 61 6e 64 20 6e 6f 20 72 6f 6c 6c 62 61 63  t and no rollbac
56ff0 6b 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 49 47  k.** occurs.  IG
57000 4e 4f 52 45 20 6d 65 61 6e 73 20 74 68 61 74 20  NORE means that 
57010 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 72  the particular r
57020 6f 77 20 74 68 61 74 20 63 61 75 73 65 64 20 74  ow that caused t
57030 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a  he constraint.**
57040 20 65 72 72 6f 72 20 69 73 20 6e 6f 74 20 69 6e   error is not in
57050 73 65 72 74 65 64 20 6f 72 20 75 70 64 61 74 65  serted or update
57060 64 2e 20 20 50 72 6f 63 65 73 73 69 6e 67 20 63  d.  Processing c
57070 6f 6e 74 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20  ontinues and no 
57080 65 72 72 6f 72 0a 2a 2a 20 69 73 20 72 65 74 75  error.** is retu
57090 72 6e 65 64 2e 20 20 52 45 50 4c 41 43 45 20 6d  rned.  REPLACE m
570a0 65 61 6e 73 20 74 68 61 74 20 70 72 65 65 78 69  eans that preexi
570b0 73 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 72  sting database r
570c0 6f 77 73 20 74 68 61 74 20 63 61 75 73 65 64 0a  ows that caused.
570d0 2a 2a 20 61 20 55 4e 49 51 55 45 20 63 6f 6e 73  ** a UNIQUE cons
570e0 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e  traint violation
570f0 20 61 72 65 20 72 65 6d 6f 76 65 64 20 73 6f 20   are removed so 
57100 74 68 61 74 20 74 68 65 20 6e 65 77 20 69 6e 73  that the new ins
57110 65 72 74 20 6f 72 0a 2a 2a 20 75 70 64 61 74 65  ert or.** update
57120 20 63 61 6e 20 70 72 6f 63 65 65 64 2e 20 20 50   can proceed.  P
57130 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74 69 6e  rocessing contin
57140 75 65 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f 72  ues and no error
57150 20 69 73 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a   is reported..**
57160 0a 2a 2a 20 52 45 53 54 52 49 43 54 2c 20 53 45  .** RESTRICT, SE
57170 54 4e 55 4c 4c 2c 20 61 6e 64 20 43 41 53 43 41  TNULL, and CASCA
57180 44 45 20 61 63 74 69 6f 6e 73 20 61 70 70 6c 79  DE actions apply
57190 20 6f 6e 6c 79 20 74 6f 20 66 6f 72 65 69 67 6e   only to foreign
571a0 20 6b 65 79 73 2e 0a 2a 2a 20 52 45 53 54 52 49   keys..** RESTRI
571b0 43 54 20 69 73 20 74 68 65 20 73 61 6d 65 20 61  CT is the same a
571c0 73 20 41 42 4f 52 54 20 66 6f 72 20 49 4d 4d 45  s ABORT for IMME
571d0 44 49 41 54 45 20 66 6f 72 65 69 67 6e 20 6b 65  DIATE foreign ke
571e0 79 73 20 61 6e 64 20 74 68 65 0a 2a 2a 20 73 61  ys and the.** sa
571f0 6d 65 20 61 73 20 52 4f 4c 4c 42 41 43 4b 20 66  me as ROLLBACK f
57200 6f 72 20 44 45 46 45 52 52 45 44 20 6b 65 79 73  or DEFERRED keys
57210 2e 20 20 53 45 54 4e 55 4c 4c 20 6d 65 61 6e 73  .  SETNULL means
57220 20 74 68 61 74 20 74 68 65 20 66 6f 72 65 69 67   that the foreig
57230 6e 0a 2a 2a 20 6b 65 79 20 69 73 20 73 65 74 20  n.** key is set 
57240 74 6f 20 4e 55 4c 4c 2e 20 20 43 41 53 43 41 44  to NULL.  CASCAD
57250 45 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 44  E means that a D
57260 45 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 20  ELETE or UPDATE 
57270 6f 66 20 74 68 65 0a 2a 2a 20 72 65 66 65 72 65  of the.** refere
57280 6e 63 65 64 20 74 61 62 6c 65 20 72 6f 77 20 69  nced table row i
57290 73 20 70 72 6f 70 61 67 61 74 65 64 20 69 6e 74  s propagated int
572a0 6f 20 74 68 65 20 72 6f 77 20 74 68 61 74 20 68  o the row that h
572b0 6f 6c 64 73 20 74 68 65 0a 2a 2a 20 66 6f 72 65  olds the.** fore
572c0 69 67 6e 20 6b 65 79 2e 0a 2a 2a 20 0a 2a 2a 20  ign key..** .** 
572d0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79  The following sy
572e0 6d 62 6f 6c 69 63 20 76 61 6c 75 65 73 20 61 72  mbolic values ar
572f0 65 20 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64  e used to record
57300 20 77 68 69 63 68 20 74 79 70 65 0a 2a 2a 20 6f   which type.** o
57310 66 20 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65  f action to take
57320 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f  ..*/.#define OE_
57330 4e 6f 6e 65 20 20 20 20 20 30 20 20 20 2f 2a 20  None     0   /* 
57340 54 68 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73  There is no cons
57350 74 72 61 69 6e 74 20 74 6f 20 63 68 65 63 6b 20  traint to check 
57360 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 52 6f  */.#define OE_Ro
57370 6c 6c 62 61 63 6b 20 31 20 20 20 2f 2a 20 46 61  llback 1   /* Fa
57380 69 6c 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  il the operation
57390 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68   and rollback th
573a0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f  e transaction */
573b0 0a 23 64 65 66 69 6e 65 20 4f 45 5f 41 62 6f 72  .#define OE_Abor
573c0 74 20 20 20 20 32 20 20 20 2f 2a 20 42 61 63 6b  t    2   /* Back
573d0 20 6f 75 74 20 63 68 61 6e 67 65 73 20 62 75 74   out changes but
573e0 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20   do no rollback 
573f0 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23  transaction */.#
57400 64 65 66 69 6e 65 20 4f 45 5f 46 61 69 6c 20 20  define OE_Fail  
57410 20 20 20 33 20 20 20 2f 2a 20 53 74 6f 70 20 74     3   /* Stop t
57420 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 62 75 74  he operation but
57430 20 6c 65 61 76 65 20 61 6c 6c 20 70 72 69 6f 72   leave all prior
57440 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66   changes */.#def
57450 69 6e 65 20 4f 45 5f 49 67 6e 6f 72 65 20 20 20  ine OE_Ignore   
57460 34 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68  4   /* Ignore th
57470 65 20 65 72 72 6f 72 2e 20 44 6f 20 6e 6f 74 20  e error. Do not 
57480 64 6f 20 74 68 65 20 49 4e 53 45 52 54 20 6f 72  do the INSERT or
57490 20 55 50 44 41 54 45 20 2a 2f 0a 23 64 65 66 69   UPDATE */.#defi
574a0 6e 65 20 4f 45 5f 52 65 70 6c 61 63 65 20 20 35  ne OE_Replace  5
574b0 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 65 78 69     /* Delete exi
574c0 73 74 69 6e 67 20 72 65 63 6f 72 64 2c 20 74 68  sting record, th
574d0 65 6e 20 64 6f 20 49 4e 53 45 52 54 20 6f 72 20  en do INSERT or 
574e0 55 50 44 41 54 45 20 2a 2f 0a 0a 23 64 65 66 69  UPDATE */..#defi
574f0 6e 65 20 4f 45 5f 52 65 73 74 72 69 63 74 20 36  ne OE_Restrict 6
57500 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 20 66     /* OE_Abort f
57510 6f 72 20 49 4d 4d 45 44 49 41 54 45 2c 20 4f 45  or IMMEDIATE, OE
57520 5f 52 6f 6c 6c 62 61 63 6b 20 66 6f 72 20 44 45  _Rollback for DE
57530 46 45 52 52 45 44 20 2a 2f 0a 23 64 65 66 69 6e  FERRED */.#defin
57540 65 20 4f 45 5f 53 65 74 4e 75 6c 6c 20 20 37 20  e OE_SetNull  7 
57550 20 20 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72    /* Set the for
57560 65 69 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74  eign key value t
57570 6f 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e  o NULL */.#defin
57580 65 20 4f 45 5f 53 65 74 44 66 6c 74 20 20 38 20  e OE_SetDflt  8 
57590 20 20 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72    /* Set the for
575a0 65 69 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74  eign key value t
575b0 6f 20 69 74 73 20 64 65 66 61 75 6c 74 20 2a 2f  o its default */
575c0 0a 23 64 65 66 69 6e 65 20 4f 45 5f 43 61 73 63  .#define OE_Casc
575d0 61 64 65 20 20 39 20 20 20 2f 2a 20 43 61 73 63  ade  9   /* Casc
575e0 61 64 65 20 74 68 65 20 63 68 61 6e 67 65 73 20  ade the changes 
575f0 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 4f 45 5f 44  */..#define OE_D
57600 65 66 61 75 6c 74 20 20 39 39 20 20 2f 2a 20 44  efault  99  /* D
57610 6f 20 77 68 61 74 65 76 65 72 20 74 68 65 20 64  o whatever the d
57620 65 66 61 75 6c 74 20 61 63 74 69 6f 6e 20 69 73  efault action is
57630 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69   */.../*.** An i
57640 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
57650 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
57660 72 65 20 69 73 20 70 61 73 73 65 64 20 61 73 20  re is passed as 
57670 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67  the first.** arg
57680 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33  ument to sqlite3
57690 56 64 62 65 4b 65 79 43 6f 6d 70 61 72 65 20 61  VdbeKeyCompare a
576a0 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f  nd is used to co
576b0 6e 74 72 6f 6c 20 74 68 65 20 0a 2a 2a 20 63 6f  ntrol the .** co
576c0 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74 68 65 20  mparison of the 
576d0 74 77 6f 20 69 6e 64 65 78 20 6b 65 79 73 2e 0a  two index keys..
576e0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4b 65 79  **.** If the Key
576f0 49 6e 66 6f 2e 69 6e 63 72 4b 65 79 20 76 61 6c  Info.incrKey val
57700 75 65 20 69 73 20 74 72 75 65 20 61 6e 64 20 74  ue is true and t
57710 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 77 6f  he comparison wo
57720 75 6c 64 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65  uld.** otherwise
57730 20 62 65 20 65 71 75 61 6c 2c 20 74 68 65 6e 20   be equal, then 
57740 72 65 74 75 72 6e 20 61 20 72 65 73 75 6c 74 20  return a result 
57750 61 73 20 69 66 20 74 68 65 20 73 65 63 6f 6e 64  as if the second
57760 20 6b 65 79 0a 2a 2a 20 77 65 72 65 20 6c 61 72   key.** were lar
57770 67 65 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4b  ger..*/.struct K
57780 65 79 49 6e 66 6f 20 7b 0a 20 20 73 71 6c 69 74  eyInfo {.  sqlit
57790 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 2f  e3 *db;        /
577a0 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
577b0 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 75  onnection */.  u
577c0 38 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20 20  8 enc;          
577d0 20 20 20 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64     /* Text encod
577e0 69 6e 67 20 2d 20 6f 6e 65 20 6f 66 20 74 68 65  ing - one of the
577f0 20 54 45 58 54 5f 55 74 66 2a 20 76 61 6c 75 65   TEXT_Utf* value
57800 73 20 2a 2f 0a 20 20 75 38 20 69 6e 63 72 4b 65  s */.  u8 incrKe
57810 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  y;         /* In
57820 63 72 65 61 73 65 20 32 6e 64 20 6b 65 79 20 62  crease 2nd key b
57830 79 20 65 70 73 69 6c 6f 6e 20 62 65 66 6f 72 65  y epsilon before
57840 20 63 6f 6d 70 61 72 69 73 6f 6e 20 2a 2f 0a 20   comparison */. 
57850 20 75 38 20 70 72 65 66 69 78 49 73 45 71 75 61   u8 prefixIsEqua
57860 6c 3b 20 20 20 2f 2a 20 54 72 65 61 74 20 61 20  l;   /* Treat a 
57870 70 72 65 66 69 78 20 61 73 20 65 71 75 61 6c 20  prefix as equal 
57880 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b  */.  int nField;
57890 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
578a0 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e  er of entries in
578b0 20 61 43 6f 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38   aColl[] */.  u8
578c0 20 2a 61 53 6f 72 74 4f 72 64 65 72 3b 20 20 20   *aSortOrder;   
578d0 20 20 2f 2a 20 49 66 20 64 65 66 69 6e 65 64 20    /* If defined 
578e0 61 6e 20 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d  an aSortOrder[i]
578f0 20 69 73 20 74 72 75 65 2c 20 73 6f 72 74 20 44   is true, sort D
57900 45 53 43 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71  ESC */.  CollSeq
57910 20 2a 61 43 6f 6c 6c 5b 31 5d 3b 20 20 2f 2a 20   *aColl[1];  /* 
57920 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  Collating sequen
57930 63 65 20 66 6f 72 20 65 61 63 68 20 74 65 72 6d  ce for each term
57940 20 6f 66 20 74 68 65 20 6b 65 79 20 2a 2f 0a 7d   of the key */.}
57950 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51  ;../*.** Each SQ
57960 4c 20 69 6e 64 65 78 20 69 73 20 72 65 70 72 65  L index is repre
57970 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79  sented in memory
57980 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e   by an.** instan
57990 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
579a0 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ing structure..*
579b0 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73  *.** The columns
579c0 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 68   of the table th
579d0 61 74 20 61 72 65 20 74 6f 20 62 65 20 69 6e 64  at are to be ind
579e0 65 78 65 64 20 61 72 65 20 64 65 73 63 72 69 62  exed are describ
579f0 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 61 69 43  ed.** by the aiC
57a00 6f 6c 75 6d 6e 5b 5d 20 66 69 65 6c 64 20 6f 66  olumn[] field of
57a10 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e   this structure.
57a20 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 73    For example, s
57a30 75 70 70 6f 73 65 0a 2a 2a 20 77 65 20 68 61 76  uppose.** we hav
57a40 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  e the following 
57a50 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 3a  table and index:
57a60 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54  .**.**     CREAT
57a70 45 20 54 41 42 4c 45 20 45 78 31 28 63 31 20 69  E TABLE Ex1(c1 i
57a80 6e 74 2c 20 63 32 20 69 6e 74 2c 20 63 33 20 74  nt, c2 int, c3 t
57a90 65 78 74 29 3b 0a 2a 2a 20 20 20 20 20 43 52 45  ext);.**     CRE
57aa0 41 54 45 20 49 4e 44 45 58 20 45 78 32 20 4f 4e  ATE INDEX Ex2 ON
57ab0 20 45 78 31 28 63 33 2c 63 31 29 3b 0a 2a 2a 0a   Ex1(c3,c1);.**.
57ac0 2a 2a 20 49 6e 20 74 68 65 20 54 61 62 6c 65 20  ** In the Table 
57ad0 73 74 72 75 63 74 75 72 65 20 64 65 73 63 72 69  structure descri
57ae0 62 69 6e 67 20 45 78 31 2c 20 6e 43 6f 6c 3d 3d  bing Ex1, nCol==
57af0 33 20 62 65 63 61 75 73 65 20 74 68 65 72 65 20  3 because there 
57b00 61 72 65 0a 2a 2a 20 74 68 72 65 65 20 63 6f 6c  are.** three col
57b10 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c  umns in the tabl
57b20 65 2e 20 20 49 6e 20 74 68 65 20 49 6e 64 65 78  e.  In the Index
57b30 20 73 74 72 75 63 74 75 72 65 20 64 65 73 63 72   structure descr
57b40 69 62 69 6e 67 0a 2a 2a 20 45 78 32 2c 20 6e 43  ibing.** Ex2, nC
57b50 6f 6c 75 6d 6e 3d 3d 32 20 73 69 6e 63 65 20 32  olumn==2 since 2
57b60 20 6f 66 20 74 68 65 20 33 20 63 6f 6c 75 6d 6e   of the 3 column
57b70 73 20 6f 66 20 45 78 31 20 61 72 65 20 69 6e 64  s of Ex1 are ind
57b80 65 78 65 64 2e 0a 2a 2a 20 54 68 65 20 76 61 6c  exed..** The val
57b90 75 65 20 6f 66 20 61 69 43 6f 6c 75 6d 6e 20 69  ue of aiColumn i
57ba0 73 20 7b 32 2c 20 30 7d 2e 20 20 61 69 43 6f 6c  s {2, 0}.  aiCol
57bb0 75 6d 6e 5b 30 5d 3d 3d 32 20 62 65 63 61 75 73  umn[0]==2 becaus
57bc0 65 20 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 20  e the .** first 
57bd0 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 69 6e 64  column to be ind
57be0 65 78 65 64 20 28 63 33 29 20 68 61 73 20 61 6e  exed (c3) has an
57bf0 20 69 6e 64 65 78 20 6f 66 20 32 20 69 6e 20 45   index of 2 in E
57c00 78 31 2e 61 43 6f 6c 5b 5d 2e 0a 2a 2a 20 54 68  x1.aCol[]..** Th
57c10 65 20 73 65 63 6f 6e 64 20 63 6f 6c 75 6d 6e 20  e second column 
57c20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 28 63  to be indexed (c
57c30 31 29 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20  1) has an index 
57c40 6f 66 20 30 20 69 6e 0a 2a 2a 20 45 78 31 2e 61  of 0 in.** Ex1.a
57c50 43 6f 6c 5b 5d 2c 20 68 65 6e 63 65 20 45 78 32  Col[], hence Ex2
57c60 2e 61 69 43 6f 6c 75 6d 6e 5b 31 5d 3d 3d 30 2e  .aiColumn[1]==0.
57c70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 6e 64 65 78  .**.** The Index
57c80 2e 6f 6e 45 72 72 6f 72 20 66 69 65 6c 64 20 64  .onError field d
57c90 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65  etermines whethe
57ca0 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e 64  r or not the ind
57cb0 65 78 65 64 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20  exed columns.** 
57cc0 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65 20 61  must be unique a
57cd0 6e 64 20 77 68 61 74 20 74 6f 20 64 6f 20 69 66  nd what to do if
57ce0 20 74 68 65 79 20 61 72 65 20 6e 6f 74 2e 20 20   they are not.  
57cf0 57 68 65 6e 20 49 6e 64 65 78 2e 6f 6e 45 72 72  When Index.onErr
57d00 6f 72 3d 4f 45 5f 4e 6f 6e 65 2c 0a 2a 2a 20 69  or=OE_None,.** i
57d10 74 20 6d 65 61 6e 73 20 74 68 69 73 20 69 73 20  t means this is 
57d20 6e 6f 74 20 61 20 75 6e 69 71 75 65 20 69 6e 64  not a unique ind
57d30 65 78 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69  ex.  Otherwise i
57d40 74 20 69 73 20 61 20 75 6e 69 71 75 65 20 69 6e  t is a unique in
57d50 64 65 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20 76  dex.** and the v
57d60 61 6c 75 65 20 6f 66 20 49 6e 64 65 78 2e 6f 6e  alue of Index.on
57d70 45 72 72 6f 72 20 69 6e 64 69 63 61 74 65 20 74  Error indicate t
57d80 68 65 20 77 68 69 63 68 20 63 6f 6e 66 6c 69 63  he which conflic
57d90 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 0a 2a 2a  t resolution .**
57da0 20 61 6c 67 6f 72 69 74 68 6d 20 74 6f 20 65 6d   algorithm to em
57db0 70 6c 6f 79 20 77 68 65 6e 65 76 65 72 20 61 6e  ploy whenever an
57dc0 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
57dd0 20 74 6f 20 69 6e 73 65 72 74 20 61 20 6e 6f 6e   to insert a non
57de0 2d 75 6e 69 71 75 65 0a 2a 2a 20 65 6c 65 6d 65  -unique.** eleme
57df0 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49 6e  nt..*/.struct In
57e00 64 65 78 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e  dex {.  char *zN
57e10 61 6d 65 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ame;     /* Name
57e20 20 6f 66 20 74 68 69 73 20 69 6e 64 65 78 20 2a   of this index *
57e30 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b  /.  int nColumn;
57e40 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
57e50 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
57e60 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20 74   table used by t
57e70 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69  his index */.  i
57e80 6e 74 20 2a 61 69 43 6f 6c 75 6d 6e 3b 20 20 20  nt *aiColumn;   
57e90 2f 2a 20 57 68 69 63 68 20 63 6f 6c 75 6d 6e 73  /* Which columns
57ea0 20 61 72 65 20 75 73 65 64 20 62 79 20 74 68 69   are used by thi
57eb0 73 20 69 6e 64 65 78 2e 20 20 31 73 74 20 69 73  s index.  1st is
57ec0 20 30 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64   0 */.  unsigned
57ed0 20 2a 61 69 52 6f 77 45 73 74 3b 20 2f 2a 20 52   *aiRowEst; /* R
57ee0 65 73 75 6c 74 20 6f 66 20 41 4e 41 4c 59 5a 45  esult of ANALYZE
57ef0 3a 20 45 73 74 2e 20 72 6f 77 73 20 73 65 6c 65  : Est. rows sele
57f00 63 74 65 64 20 62 79 20 65 61 63 68 20 63 6f 6c  cted by each col
57f10 75 6d 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  umn */.  Table *
57f20 70 54 61 62 6c 65 3b 20 20 20 2f 2a 20 54 68 65  pTable;   /* The
57f30 20 53 51 4c 20 74 61 62 6c 65 20 62 65 69 6e 67   SQL table being
57f40 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e   indexed */.  in
57f50 74 20 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 2f  t tnum;        /
57f60 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e  * Page containin
57f70 67 20 72 6f 6f 74 20 6f 66 20 74 68 69 73 20 69  g root of this i
57f80 6e 64 65 78 20 69 6e 20 64 61 74 61 62 61 73 65  ndex in database
57f90 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 6f 6e   file */.  u8 on
57fa0 45 72 72 6f 72 3b 20 20 20 20 20 20 2f 2a 20 4f  Error;      /* O
57fb0 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f  E_Abort, OE_Igno
57fc0 72 65 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20  re, OE_Replace, 
57fd0 6f 72 20 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a 20 20  or OE_None */.  
57fe0 75 38 20 61 75 74 6f 49 6e 64 65 78 3b 20 20 20  u8 autoIndex;   
57ff0 20 2f 2a 20 54 72 75 65 20 69 66 20 69 73 20 61   /* True if is a
58000 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65  utomatically cre
58010 61 74 65 64 20 28 65 78 3a 20 62 79 20 55 4e 49  ated (ex: by UNI
58020 51 55 45 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a  QUE) */.  char *
58030 7a 43 6f 6c 41 66 66 3b 20 20 20 2f 2a 20 53 74  zColAff;   /* St
58040 72 69 6e 67 20 64 65 66 69 6e 69 6e 67 20 74 68  ring defining th
58050 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 65 61  e affinity of ea
58060 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49  ch column */.  I
58070 6e 64 65 78 20 2a 70 4e 65 78 74 3b 20 20 20 20  ndex *pNext;    
58080 2f 2a 20 54 68 65 20 6e 65 78 74 20 69 6e 64 65  /* The next inde
58090 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  x associated wit
580a0 68 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65  h the same table
580b0 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53   */.  Schema *pS
580c0 63 68 65 6d 61 3b 20 2f 2a 20 53 63 68 65 6d 61  chema; /* Schema
580d0 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69 73   containing this
580e0 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 2a   index */.  u8 *
580f0 61 53 6f 72 74 4f 72 64 65 72 3b 20 20 2f 2a 20  aSortOrder;  /* 
58100 41 72 72 61 79 20 6f 66 20 73 69 7a 65 20 49 6e  Array of size In
58110 64 65 78 2e 6e 43 6f 6c 75 6d 6e 2e 20 54 72 75  dex.nColumn. Tru
58120 65 3d 3d 44 45 53 43 2c 20 46 61 6c 73 65 3d 3d  e==DESC, False==
58130 41 53 43 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  ASC */.  char **
58140 61 7a 43 6f 6c 6c 3b 20 20 20 2f 2a 20 41 72 72  azColl;   /* Arr
58150 61 79 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20  ay of collation 
58160 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 73 20 66  sequence names f
58170 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 7d 3b 0a 0a  or index */.};..
58180 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 6f 6b 65 6e  /*.** Each token
58190 20 63 6f 6d 69 6e 67 20 6f 75 74 20 6f 66 20 74   coming out of t
581a0 68 65 20 6c 65 78 65 72 20 69 73 20 61 6e 20 69  he lexer is an i
581b0 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68  nstance of.** th
581c0 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  is structure.  T
581d0 6f 6b 65 6e 73 20 61 72 65 20 61 6c 73 6f 20 75  okens are also u
581e0 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61  sed as part of a
581f0 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a  n expression..**
58200 0a 2a 2a 20 4e 6f 74 65 20 69 66 20 54 6f 6b 65  .** Note if Toke
58210 6e 2e 7a 3d 3d 30 20 74 68 65 6e 20 54 6f 6b 65  n.z==0 then Toke
58220 6e 2e 64 79 6e 20 61 6e 64 20 54 6f 6b 65 6e 2e  n.dyn and Token.
58230 6e 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20  n are undefined 
58240 61 6e 64 0a 2a 2a 20 6d 61 79 20 63 6f 6e 74 61  and.** may conta
58250 69 6e 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 73  in random values
58260 2e 20 20 44 6f 20 6e 6f 74 20 6d 61 6b 65 20 61  .  Do not make a
58270 6e 79 20 61 73 73 75 70 74 69 6f 6e 73 20 61 62  ny assuptions ab
58280 6f 75 74 20 54 6f 6b 65 6e 2e 64 79 6e 0a 2a 2a  out Token.dyn.**
58290 20 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 77 68 65   and Token.n whe
582a0 6e 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 2e 0a 2a 2f  n Token.z==0..*/
582b0 0a 73 74 72 75 63 74 20 54 6f 6b 65 6e 20 7b 0a  .struct Token {.
582c0 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
582d0 20 63 68 61 72 20 2a 7a 3b 20 2f 2a 20 54 65 78   char *z; /* Tex
582e0 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 2e 20  t of the token. 
582f0 20 4e 6f 74 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e   Not NULL-termin
58300 61 74 65 64 21 20 2a 2f 0a 20 20 75 6e 73 69 67  ated! */.  unsig
58310 6e 65 64 20 64 79 6e 20 20 3a 20 31 3b 20 20 20  ned dyn  : 1;   
58320 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 6d     /* True for m
58330 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 2c 20  alloced memory, 
58340 66 61 6c 73 65 20 66 6f 72 20 73 74 61 74 69 63  false for static
58350 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6e   */.  unsigned n
58360 20 20 20 20 3a 20 33 31 3b 20 20 20 20 20 2f 2a      : 31;     /*
58370 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61   Number of chara
58380 63 74 65 72 73 20 69 6e 20 74 68 69 73 20 74 6f  cters in this to
58390 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ken */.};../*.**
583a0 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
583b0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63  this structure c
583c0 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74  ontains informat
583d0 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65  ion needed to ge
583e0 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66  nerate.** code f
583f0 6f 72 20 61 20 53 45 4c 45 43 54 20 74 68 61 74  or a SELECT that
58400 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67   contains aggreg
58410 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a  ate functions..*
58420 2a 0a 2a 2a 20 49 66 20 45 78 70 72 2e 6f 70 3d  *.** If Expr.op=
58430 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 6f  =TK_AGG_COLUMN o
58440 72 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f  r TK_AGG_FUNCTIO
58450 4e 20 74 68 65 6e 20 45 78 70 72 2e 70 41 67 67  N then Expr.pAgg
58460 49 6e 66 6f 20 69 73 20 61 0a 2a 2a 20 70 6f 69  Info is a.** poi
58470 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 72  nter to this str
58480 75 63 74 75 72 65 2e 20 20 54 68 65 20 45 78 70  ucture.  The Exp
58490 72 2e 69 43 6f 6c 75 6d 6e 20 66 69 65 6c 64 20  r.iColumn field 
584a0 69 73 20 74 68 65 20 69 6e 64 65 78 20 69 6e 0a  is the index in.
584b0 2a 2a 20 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b  ** AggInfo.aCol[
584c0 5d 20 6f 72 20 41 67 67 49 6e 66 6f 2e 61 46 75  ] or AggInfo.aFu
584d0 6e 63 5b 5d 20 6f 66 20 69 6e 66 6f 72 6d 61 74  nc[] of informat
584e0 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65  ion needed to ge
584f0 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66  nerate.** code f
58500 6f 72 20 74 68 61 74 20 6e 6f 64 65 2e 0a 2a 2a  or that node..**
58510 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 70 47 72 6f  .** AggInfo.pGro
58520 75 70 42 79 20 61 6e 64 20 41 67 67 49 6e 66 6f  upBy and AggInfo
58530 2e 61 46 75 6e 63 2e 70 45 78 70 72 20 70 6f 69  .aFunc.pExpr poi
58540 6e 74 20 74 6f 20 66 69 65 6c 64 73 20 77 69 74  nt to fields wit
58550 68 69 6e 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69  hin the.** origi
58560 6e 61 6c 20 53 65 6c 65 63 74 20 73 74 72 75 63  nal Select struc
58570 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69  ture that descri
58580 62 65 73 20 74 68 65 20 53 45 4c 45 43 54 20 73  bes the SELECT s
58590 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 73 65  tatement.  These
585a0 0a 2a 2a 20 66 69 65 6c 64 73 20 64 6f 20 6e 6f  .** fields do no
585b0 74 20 6e 65 65 64 20 74 6f 20 62 65 20 66 72 65  t need to be fre
585c0 65 64 20 77 68 65 6e 20 64 65 61 6c 6c 6f 63 61  ed when dealloca
585d0 74 69 6e 67 20 74 68 65 20 41 67 67 49 6e 66 6f  ting the AggInfo
585e0 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73   structure..*/.s
585f0 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20 7b 0a  truct AggInfo {.
58600 20 20 75 38 20 64 69 72 65 63 74 4d 6f 64 65 3b    u8 directMode;
58610 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72            /* Dir
58620 65 63 74 20 72 65 6e 64 65 72 69 6e 67 20 6d 6f  ect rendering mo
58630 64 65 20 6d 65 61 6e 73 20 74 61 6b 65 20 64 61  de means take da
58640 74 61 20 64 69 72 65 63 74 6c 79 0a 20 20 20 20  ta directly.    
58650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58660 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 73 6f        ** from so
58670 75 72 63 65 20 74 61 62 6c 65 73 20 72 61 74 68  urce tables rath
58680 65 72 20 74 68 61 6e 20 66 72 6f 6d 20 61 63 63  er than from acc
58690 75 6d 75 6c 61 74 6f 72 73 20 2a 2f 0a 20 20 75  umulators */.  u
586a0 38 20 75 73 65 53 6f 72 74 69 6e 67 49 64 78 3b  8 useSortingIdx;
586b0 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 69 72         /* In dir
586c0 65 63 74 20 6d 6f 64 65 2c 20 72 65 66 65 72 65  ect mode, refere
586d0 6e 63 65 20 74 68 65 20 73 6f 72 74 69 6e 67 20  nce the sorting 
586e0 69 6e 64 65 78 20 72 61 74 68 65 72 0a 20 20 20  index rather.   
586f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58700 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e 20 74         ** than t
58710 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20  he source table 
58720 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 69 6e 67  */.  int sorting
58730 49 64 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Idx;         /* 
58740 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66  Cursor number of
58750 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64   the sorting ind
58760 65 78 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ex */.  ExprList
58770 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20 20   *pGroupBy;     
58780 2f 2a 20 54 68 65 20 67 72 6f 75 70 20 62 79 20  /* The group by 
58790 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20  clause */.  int 
587a0 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 3b 20  nSortingColumn; 
587b0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
587c0 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
587d0 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f  sorting index */
587e0 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66  .  struct AggInf
587f0 6f 5f 63 6f 6c 20 7b 20 20 20 20 2f 2a 20 46 6f  o_col {    /* Fo
58800 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 75 73  r each column us
58810 65 64 20 69 6e 20 73 6f 75 72 63 65 20 74 61 62  ed in source tab
58820 6c 65 73 20 2a 2f 0a 20 20 20 20 54 61 62 6c 65  les */.    Table
58830 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20   *pTab;         
58840 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 74 61      /* Source ta
58850 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ble */.    int i
58860 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20  Table;          
58870 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75      /* Cursor nu
58880 6d 62 65 72 20 6f 66 20 74 68 65 20 73 6f 75 72  mber of the sour
58890 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  ce table */.    
588a0 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20  int iColumn;    
588b0 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75           /* Colu
588c0 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 69 6e  mn number within
588d0 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c   the source tabl
588e0 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53 6f  e */.    int iSo
588f0 72 74 65 72 43 6f 6c 75 6d 6e 3b 20 20 20 20 20  rterColumn;     
58900 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62    /* Column numb
58910 65 72 20 69 6e 20 74 68 65 20 73 6f 72 74 69 6e  er in the sortin
58920 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69  g index */.    i
58930 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20  nt iMem;        
58940 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72          /* Memor
58950 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 20  y location that 
58960 61 63 74 73 20 61 73 20 61 63 63 75 6d 75 6c 61  acts as accumula
58970 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70 72 20  tor */.    Expr 
58980 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20  *pExpr;         
58990 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69 67 69      /* The origi
589a0 6e 61 6c 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  nal expression *
589b0 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 0a 20 20 69  /.  } *aCol;.  i
589c0 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20  nt nColumn;     
589d0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
589e0 20 6f 66 20 75 73 65 64 20 65 6e 74 72 69 65 73   of used entries
589f0 20 69 6e 20 61 43 6f 6c 5b 5d 20 2a 2f 0a 20 20   in aCol[] */.  
58a00 69 6e 74 20 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63  int nColumnAlloc
58a10 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ;       /* Numbe
58a20 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63  r of slots alloc
58a30 61 74 65 64 20 66 6f 72 20 61 43 6f 6c 5b 5d 20  ated for aCol[] 
58a40 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63 75 6d 75  */.  int nAccumu
58a50 6c 61 74 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20  lator;       /* 
58a60 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  Number of column
58a70 73 20 74 68 61 74 20 73 68 6f 77 20 74 68 72 6f  s that show thro
58a80 75 67 68 20 74 6f 20 74 68 65 20 6f 75 74 70 75  ugh to the outpu
58a90 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t..             
58aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
58ab0 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d  Additional colum
58ac0 6e 73 20 61 72 65 20 75 73 65 64 20 6f 6e 6c 79  ns are used only
58ad0 20 61 73 20 70 61 72 61 6d 65 74 65 72 73 20 74   as parameters t
58ae0 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o.              
58af0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61              ** a
58b00 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
58b10 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41  ns */.  struct A
58b20 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b 20 20 20  ggInfo_func {   
58b30 2f 2a 20 46 6f 72 20 65 61 63 68 20 61 67 67 72  /* For each aggr
58b40 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a  egate function *
58b50 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70  /.    Expr *pExp
58b60 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  r;             /
58b70 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 65 6e 63  * Expression enc
58b80 6f 64 69 6e 67 20 74 68 65 20 66 75 6e 63 74 69  oding the functi
58b90 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65  on */.    FuncDe
58ba0 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20  f *pFunc;       
58bb0 20 20 20 2f 2a 20 54 68 65 20 61 67 67 72 65 67     /* The aggreg
58bc0 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  ate function imp
58bd0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20  lementation */. 
58be0 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20     int iMem;    
58bf0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
58c00 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74  emory location t
58c10 68 61 74 20 61 63 74 73 20 61 73 20 61 63 63 75  hat acts as accu
58c20 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 69  mulator */.    i
58c30 6e 74 20 69 44 69 73 74 69 6e 63 74 3b 20 20 20  nt iDistinct;   
58c40 20 20 20 20 20 20 20 20 2f 2a 20 45 70 68 65 72          /* Epher
58c50 6d 65 72 61 6c 20 74 61 62 6c 65 20 75 73 65 64  meral table used
58c60 20 74 6f 20 65 6e 66 6f 72 63 65 20 44 49 53 54   to enforce DIST
58c70 49 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a 61 46 75  INCT */.  } *aFu
58c80 6e 63 3b 0a 20 20 69 6e 74 20 6e 46 75 6e 63 3b  nc;.  int nFunc;
58c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
58ca0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
58cb0 65 73 20 69 6e 20 61 46 75 6e 63 5b 5d 20 2a 2f  es in aFunc[] */
58cc0 0a 20 20 69 6e 74 20 6e 46 75 6e 63 41 6c 6c 6f  .  int nFuncAllo
58cd0 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  c;         /* Nu
58ce0 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c  mber of slots al
58cf0 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 46 75 6e  located for aFun
58d00 63 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  c[] */.};../*.**
58d10 20 45 61 63 68 20 6e 6f 64 65 20 6f 66 20 61 6e   Each node of an
58d20 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74   expression in t
58d30 68 65 20 70 61 72 73 65 20 74 72 65 65 20 69 73  he parse tree is
58d40 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20   an instance.** 
58d50 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
58d60 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 6f 70  e..**.** Expr.op
58d70 20 69 73 20 74 68 65 20 6f 70 63 6f 64 65 2e 20   is the opcode. 
58d80 20 54 68 65 20 69 6e 74 65 67 65 72 20 70 61 72   The integer par
58d90 73 65 72 20 74 6f 6b 65 6e 20 63 6f 64 65 73 20  ser token codes 
58da0 61 72 65 20 72 65 75 73 65 64 0a 2a 2a 20 61 73  are reused.** as
58db0 20 6f 70 63 6f 64 65 73 20 68 65 72 65 2e 20 20   opcodes here.  
58dc0 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65  For example, the
58dd0 20 70 61 72 73 65 72 20 64 65 66 69 6e 65 73 20   parser defines 
58de0 54 4b 5f 47 45 20 74 6f 20 62 65 20 61 6e 20 69  TK_GE to be an i
58df0 6e 74 65 67 65 72 0a 2a 2a 20 63 6f 64 65 20 72  nteger.** code r
58e00 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20  epresenting the 
58e10 22 3e 3d 22 20 6f 70 65 72 61 74 6f 72 2e 20 20  ">=" operator.  
58e20 54 68 69 73 20 73 61 6d 65 20 69 6e 74 65 67 65  This same intege
58e30 72 20 63 6f 64 65 20 69 73 20 72 65 75 73 65 64  r code is reused
58e40 0a 2a 2a 20 74 6f 20 72 65 70 72 65 73 65 6e 74  .** to represent
58e50 20 74 68 65 20 67 72 65 61 74 65 72 2d 74 68 61   the greater-tha
58e60 6e 2d 6f 72 2d 65 71 75 61 6c 2d 74 6f 20 6f 70  n-or-equal-to op
58e70 65 72 61 74 6f 72 20 69 6e 20 74 68 65 20 65 78  erator in the ex
58e80 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65  pression.** tree
58e90 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 70 52 69  ..**.** Expr.pRi
58ea0 67 68 74 20 61 6e 64 20 45 78 70 72 2e 70 4c 65  ght and Expr.pLe
58eb0 66 74 20 61 72 65 20 73 75 62 65 78 70 72 65 73  ft are subexpres
58ec0 73 69 6f 6e 73 2e 20 20 45 78 70 72 2e 70 4c 69  sions.  Expr.pLi
58ed0 73 74 20 69 73 20 61 20 6c 69 73 74 0a 2a 2a 20  st is a list.** 
58ee0 6f 66 20 61 72 67 75 6d 65 6e 74 20 69 66 20 74  of argument if t
58ef0 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
58f00 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a   a function..**.
58f10 2a 2a 20 45 78 70 72 2e 74 6f 6b 65 6e 20 69 73  ** Expr.token is
58f20 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74 6f   the operator to
58f30 6b 65 6e 20 66 6f 72 20 74 68 69 73 20 6e 6f 64  ken for this nod
58f40 65 2e 20 20 46 6f 72 20 73 6f 6d 65 20 65 78 70  e.  For some exp
58f50 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 74 68 61 74  ressions.** that
58f60 20 68 61 76 65 20 73 75 62 65 78 70 72 65 73 73   have subexpress
58f70 69 6f 6e 73 2c 20 45 78 70 72 2e 74 6f 6b 65 6e  ions, Expr.token
58f80 20 63 61 6e 20 62 65 20 74 68 65 20 63 6f 6d 70   can be the comp
58f90 6c 65 74 65 20 74 65 78 74 20 74 68 61 74 20 67  lete text that g
58fa0 61 76 65 0a 2a 2a 20 72 69 73 65 20 74 6f 20 74  ave.** rise to t
58fb0 68 65 20 45 78 70 72 2e 20 20 49 6e 20 74 68 65  he Expr.  In the
58fc0 20 6c 61 74 74 65 72 20 63 61 73 65 2c 20 74 68   latter case, th
58fd0 65 20 74 6f 6b 65 6e 20 69 73 20 6d 61 72 6b 65  e token is marke
58fe0 64 20 61 73 20 62 65 69 6e 67 0a 2a 2a 20 61 20  d as being.** a 
58ff0 63 6f 6d 70 6f 75 6e 64 20 74 6f 6b 65 6e 2e 0a  compound token..
59000 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 72 65 73 73  **.** An express
59010 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ion of the form 
59020 49 44 20 6f 72 20 49 44 2e 49 44 20 72 65 66 65  ID or ID.ID refe
59030 72 73 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 69  rs to a column i
59040 6e 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20 46 6f  n a table..** Fo
59050 72 20 73 75 63 68 20 65 78 70 72 65 73 73 69 6f  r such expressio
59060 6e 73 2c 20 45 78 70 72 2e 6f 70 20 69 73 20 73  ns, Expr.op is s
59070 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20  et to TK_COLUMN 
59080 61 6e 64 20 45 78 70 72 2e 69 54 61 62 6c 65 20  and Expr.iTable 
59090 69 73 0a 2a 2a 20 74 68 65 20 69 6e 74 65 67 65  is.** the intege
590a0 72 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  r cursor number 
590b0 6f 66 20 61 20 56 44 42 45 20 63 75 72 73 6f 72  of a VDBE cursor
590c0 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 61   pointing to tha
590d0 74 20 74 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 45  t table and.** E
590e0 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 69 73 20 74  xpr.iColumn is t
590f0 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  he column number
59100 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69   for the specifi
59110 63 20 63 6f 6c 75 6d 6e 2e 20 20 49 66 20 74 68  c column.  If th
59120 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20  e.** expression 
59130 69 73 20 75 73 65 64 20 61 73 20 61 20 72 65 73  is used as a res
59140 75 6c 74 20 69 6e 20 61 6e 20 61 67 67 72 65 67  ult in an aggreg
59150 61 74 65 20 53 45 4c 45 43 54 2c 20 74 68 65 6e  ate SELECT, then
59160 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 69 73   the.** value is
59170 20 61 6c 73 6f 20 73 74 6f 72 65 64 20 69 6e 20   also stored in 
59180 74 68 65 20 45 78 70 72 2e 69 41 67 67 20 63 6f  the Expr.iAgg co
59190 6c 75 6d 6e 20 69 6e 20 74 68 65 20 61 67 67 72  lumn in the aggr
591a0 65 67 61 74 65 20 73 6f 20 74 68 61 74 0a 2a 2a  egate so that.**
591b0 20 69 74 20 63 61 6e 20 62 65 20 61 63 63 65 73   it can be acces
591c0 73 65 64 20 61 66 74 65 72 20 61 6c 6c 20 61 67  sed after all ag
591d0 67 72 65 67 61 74 65 73 20 61 72 65 20 63 6f 6d  gregates are com
591e0 70 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  puted..**.** If 
591f0 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
59200 73 20 61 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68  s a function, th
59210 65 20 45 78 70 72 2e 69 54 61 62 6c 65 20 69 73  e Expr.iTable is
59220 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 64 65   an integer code
59230 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 69 6e 67  .** representing
59240 20 77 68 69 63 68 20 66 75 6e 63 74 69 6f 6e 2e   which function.
59250 20 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73    If the express
59260 69 6f 6e 20 69 73 20 61 6e 20 75 6e 62 6f 75 6e  ion is an unboun
59270 64 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61  d variable.** ma
59280 72 6b 65 72 20 28 61 20 71 75 65 73 74 69 6f 6e  rker (a question
59290 20 6d 61 72 6b 20 63 68 61 72 61 63 74 65 72 20   mark character 
592a0 27 3f 27 20 69 6e 20 74 68 65 20 6f 72 69 67 69  '?' in the origi
592b0 6e 61 6c 20 53 51 4c 29 20 74 68 65 6e 20 74 68  nal SQL) then th
592c0 65 0a 2a 2a 20 45 78 70 72 2e 69 54 61 62 6c 65  e.** Expr.iTable
592d0 20 68 6f 6c 64 73 20 74 68 65 20 69 6e 64 65 78   holds the index
592e0 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74   number for that
592f0 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a   variable..**.**
59300 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69   If the expressi
59310 6f 6e 20 69 73 20 61 20 73 75 62 71 75 65 72 79  on is a subquery
59320 20 74 68 65 6e 20 45 78 70 72 2e 69 43 6f 6c 75   then Expr.iColu
59330 6d 6e 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65  mn holds an inte
59340 67 65 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20  ger.** register 
59350 6e 75 6d 62 65 72 20 63 6f 6e 74 61 69 6e 69 6e  number containin
59360 67 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  g the result of 
59370 74 68 65 20 73 75 62 71 75 65 72 79 2e 20 20 49  the subquery.  I
59380 66 20 74 68 65 0a 2a 2a 20 73 75 62 71 75 65 72  f the.** subquer
59390 79 20 67 69 76 65 73 20 61 20 63 6f 6e 73 74 61  y gives a consta
593a0 6e 74 20 72 65 73 75 6c 74 2c 20 74 68 65 6e 20  nt result, then 
593b0 69 54 61 62 6c 65 20 69 73 20 2d 31 2e 20 20 49  iTable is -1.  I
593c0 66 20 74 68 65 20 73 75 62 71 75 65 72 79 0a 2a  f the subquery.*
593d0 2a 20 67 69 76 65 73 20 61 20 64 69 66 66 65 72  * gives a differ
593e0 65 6e 74 20 61 6e 73 77 65 72 20 61 74 20 64 69  ent answer at di
593f0 66 66 65 72 65 6e 74 20 74 69 6d 65 73 20 64 75  fferent times du
59400 72 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 70  ring statement p
59410 72 6f 63 65 73 73 69 6e 67 0a 2a 2a 20 74 68 65  rocessing.** the
59420 6e 20 69 54 61 62 6c 65 20 69 73 20 74 68 65 20  n iTable is the 
59430 61 64 64 72 65 73 73 20 6f 66 20 61 20 73 75 62  address of a sub
59440 72 6f 75 74 69 6e 65 20 74 68 61 74 20 63 6f 6d  routine that com
59450 70 75 74 65 73 20 74 68 65 20 73 75 62 71 75 65  putes the subque
59460 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 45 78  ry..**.** The Ex
59470 70 72 2e 70 53 65 6c 65 63 74 20 66 69 65 6c 64  pr.pSelect field
59480 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 53 45 4c   points to a SEL
59490 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ECT statement.  
594a0 54 68 65 20 53 45 4c 45 43 54 20 6d 69 67 68 74  The SELECT might
594b0 0a 2a 2a 20 62 65 20 74 68 65 20 72 69 67 68 74  .** be the right
594c0 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 6e 20 49   operand of an I
594d0 4e 20 6f 70 65 72 61 74 6f 72 2e 20 20 4f 72 2c  N operator.  Or,
594e0 20 69 66 20 61 20 73 63 61 6c 61 72 20 53 45 4c   if a scalar SEL
594f0 45 43 54 20 61 70 70 65 61 72 73 0a 2a 2a 20 69  ECT appears.** i
59500 6e 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  n an expression 
59510 74 68 65 20 6f 70 63 6f 64 65 20 69 73 20 54 4b  the opcode is TK
59520 5f 53 45 4c 45 43 54 20 61 6e 64 20 45 78 70 72  _SELECT and Expr
59530 2e 70 53 65 6c 65 63 74 20 69 73 20 74 68 65 20  .pSelect is the 
59540 6f 6e 6c 79 0a 2a 2a 20 6f 70 65 72 61 6e 64 2e  only.** operand.
59550 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 78  .**.** If the Ex
59560 70 72 20 69 73 20 6f 66 20 74 79 70 65 20 4f 50  pr is of type OP
59570 5f 43 6f 6c 75 6d 6e 2c 20 61 6e 64 20 74 68 65  _Column, and the
59580 20 74 61 62 6c 65 20 69 74 20 69 73 20 73 65 6c   table it is sel
59590 65 63 74 69 6e 67 20 66 72 6f 6d 0a 2a 2a 20 69  ecting from.** i
595a0 73 20 61 20 64 69 73 6b 20 74 61 62 6c 65 20 6f  s a disk table o
595b0 72 20 74 68 65 20 22 6f 6c 64 2e 2a 22 20 70 73  r the "old.*" ps
595c0 65 75 64 6f 2d 74 61 62 6c 65 2c 20 74 68 65 6e  eudo-table, then
595d0 20 70 54 61 62 20 70 6f 69 6e 74 73 20 74 6f 20   pTab points to 
595e0 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e  the.** correspon
595f0 64 69 6e 67 20 74 61 62 6c 65 20 64 65 66 69 6e  ding table defin
59600 69 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74  ition..*/.struct
59610 20 45 78 70 72 20 7b 0a 20 20 75 38 20 6f 70 3b   Expr {.  u8 op;
59620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59630 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 70 65   /* Operation pe
59640 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69 73 20  rformed by this 
59650 6e 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 61  node */.  char a
59660 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20 20  ffinity;        
59670 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79   /* The affinity
59680 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f   of the column o
59690 72 20 30 20 69 66 20 6e 6f 74 20 61 20 63 6f 6c  r 0 if not a col
596a0 75 6d 6e 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61  umn */.  u16 fla
596b0 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  gs;             
596c0 2f 2a 20 56 61 72 69 6f 75 73 20 66 6c 61 67 73  /* Various flags
596d0 2e 20 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0a  .  See below */.
596e0 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
596f0 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
59700 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 6f  collation type o
59710 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20  f the column or 
59720 30 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65  0 */.  Expr *pLe
59730 66 74 2c 20 2a 70 52 69 67 68 74 3b 20 20 2f 2a  ft, *pRight;  /*
59740 20 4c 65 66 74 20 61 6e 64 20 72 69 67 68 74 20   Left and right 
59750 73 75 62 6e 6f 64 65 73 20 2a 2f 0a 20 20 45 78  subnodes */.  Ex
59760 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 20 20  prList *pList;  
59770 20 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f       /* A list o
59780 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 75 73  f expressions us
59790 65 64 20 61 73 20 66 75 6e 63 74 69 6f 6e 20 61  ed as function a
597a0 72 67 75 6d 65 6e 74 73 0a 20 20 20 20 20 20 20  rguments.       
597b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
597c0 20 20 2a 2a 20 6f 72 20 69 6e 20 22 3c 65 78 70    ** or in "<exp
597d0 72 3e 20 49 4e 20 28 3c 65 78 70 72 2d 6c 69 73  r> IN (<expr-lis
597e0 74 29 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 74  t)" */.  Token t
597f0 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20  oken;           
59800 2f 2a 20 41 6e 20 6f 70 65 72 61 6e 64 20 74 6f  /* An operand to
59810 6b 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73  ken */.  Token s
59820 70 61 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  pan;            
59830 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74  /* Complete text
59840 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
59850 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62  on */.  int iTab
59860 6c 65 2c 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 2f  le, iColumn;   /
59870 2a 20 57 68 65 6e 20 6f 70 3d 3d 54 4b 5f 43 4f  * When op==TK_CO
59880 4c 55 4d 4e 2c 20 74 68 65 6e 20 74 68 69 73 20  LUMN, then this 
59890 65 78 70 72 20 6e 6f 64 65 20 6d 65 61 6e 73 20  expr node means 
598a0 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 20 20  the.            
598b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
598c0 69 43 6f 6c 75 6d 6e 2d 74 68 20 66 69 65 6c 64  iColumn-th field
598d0 20 6f 66 20 74 68 65 20 69 54 61 62 6c 65 2d 74   of the iTable-t
598e0 68 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 41 67  h table. */.  Ag
598f0 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 3b  gInfo *pAggInfo;
59900 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20       /* Used by 
59910 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e  TK_AGG_COLUMN an
59920 64 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f  d TK_AGG_FUNCTIO
59930 4e 20 2a 2f 0a 20 20 69 6e 74 20 69 41 67 67 3b  N */.  int iAgg;
59940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
59950 20 57 68 69 63 68 20 65 6e 74 72 79 20 69 6e 20   Which entry in 
59960 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d  pAggInfo->aCol[]
59970 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d 20 2a 2f   or ->aFunc[] */
59980 0a 20 20 69 6e 74 20 69 52 69 67 68 74 4a 6f 69  .  int iRightJoi
59990 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20 49 66 20  nTable;   /* If 
599a0 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20 74 68 65  EP_FromJoin, the
599b0 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20   right table of 
599c0 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 53 65  the join */.  Se
599d0 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20  lect *pSelect;  
599e0 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65       /* When the
599f0 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61   expression is a
59a00 20 73 75 62 2d 73 65 6c 65 63 74 2e 20 20 41 6c   sub-select.  Al
59a10 73 6f 20 74 68 65 0a 20 20 20 20 20 20 20 20 20  so the.         
59a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59a30 2a 2a 20 72 69 67 68 74 20 73 69 64 65 20 6f 66  ** right side of
59a40 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 3c 73 65   "<expr> IN (<se
59a50 6c 65 63 74 3e 29 22 20 2a 2f 0a 20 20 54 61 62  lect>)" */.  Tab
59a60 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20  le *pTab;       
59a70 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 66 6f 72      /* Table for
59a80 20 4f 50 5f 43 6f 6c 75 6d 6e 20 65 78 70 72 65   OP_Column expre
59a90 73 73 69 6f 6e 73 2e 20 2a 2f 0a 2f 2a 20 20 53  ssions. */./*  S
59aa0 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20  chema *pSchema; 
59ab0 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  */.#if defined(S
59ac0 51 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 53  QLITE_TEST) || S
59ad0 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44  QLITE_MAX_EXPR_D
59ae0 45 50 54 48 3e 30 0a 20 20 69 6e 74 20 6e 48 65  EPTH>0.  int nHe
59af0 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20  ight;           
59b00 2f 2a 20 48 65 69 67 68 74 20 6f 66 20 74 68 65  /* Height of the
59b10 20 74 72 65 65 20 68 65 61 64 65 64 20 62 79 20   tree headed by 
59b20 74 68 69 73 20 6e 6f 64 65 20 2a 2f 0a 23 65 6e  this node */.#en
59b30 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68  dif.};../*.** Th
59b40 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20  e following are 
59b50 74 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20  the meanings of 
59b60 62 69 74 73 20 69 6e 20 74 68 65 20 45 78 70 72  bits in the Expr
59b70 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f  .flags field..*/
59b80 0a 23 64 65 66 69 6e 65 20 45 50 5f 46 72 6f 6d  .#define EP_From
59b90 4a 6f 69 6e 20 20 20 30 78 30 30 30 31 20 20 2f  Join   0x0001  /
59ba0 2a 20 4f 72 69 67 69 6e 61 74 65 64 20 69 6e 20  * Originated in 
59bb0 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75  ON or USING clau
59bc0 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a  se of a join */.
59bd0 23 64 65 66 69 6e 65 20 45 50 5f 41 67 67 20 20  #define EP_Agg  
59be0 20 20 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a        0x0002  /*
59bf0 20 43 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72   Contains one or
59c00 20 6d 6f 72 65 20 61 67 67 72 65 67 61 74 65 20   more aggregate 
59c10 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 64 65  functions */.#de
59c20 66 69 6e 65 20 45 50 5f 52 65 73 6f 6c 76 65 64  fine EP_Resolved
59c30 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 49 44     0x0004  /* ID
59c40 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f  s have been reso
59c50 6c 76 65 64 20 74 6f 20 43 4f 4c 55 4d 4e 73 20  lved to COLUMNs 
59c60 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 45 72  */.#define EP_Er
59c70 72 6f 72 20 20 20 20 20 20 30 78 30 30 30 38 20  ror      0x0008 
59c80 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 63   /* Expression c
59c90 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d  ontains one or m
59ca0 6f 72 65 20 65 72 72 6f 72 73 20 2a 2f 0a 23 64  ore errors */.#d
59cb0 65 66 69 6e 65 20 45 50 5f 44 69 73 74 69 6e 63  efine EP_Distinc
59cc0 74 20 20 20 30 78 30 30 31 30 20 20 2f 2a 20 41  t   0x0010  /* A
59cd0 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
59ce0 6e 20 77 69 74 68 20 44 49 53 54 49 4e 43 54 20  n with DISTINCT 
59cf0 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65 66 69  keyword */.#defi
59d00 6e 65 20 45 50 5f 56 61 72 53 65 6c 65 63 74 20  ne EP_VarSelect 
59d10 20 30 78 30 30 32 30 20 20 2f 2a 20 70 53 65 6c   0x0020  /* pSel
59d20 65 63 74 20 69 73 20 63 6f 72 72 65 6c 61 74 65  ect is correlate
59d30 64 2c 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 20  d, not constant 
59d40 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 44 65  */.#define EP_De
59d50 71 75 6f 74 65 64 20 20 20 30 78 30 30 34 30 20  quoted   0x0040 
59d60 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
59d70 73 74 72 69 6e 67 20 68 61 73 20 62 65 65 6e 20  string has been 
59d80 64 65 71 75 6f 74 65 64 20 2a 2f 0a 23 64 65 66  dequoted */.#def
59d90 69 6e 65 20 45 50 5f 49 6e 66 69 78 46 75 6e 63  ine EP_InfixFunc
59da0 20 20 30 78 30 30 38 30 20 20 2f 2a 20 54 72 75    0x0080  /* Tru
59db0 65 20 66 6f 72 20 61 6e 20 69 6e 66 69 78 20 66  e for an infix f
59dc0 75 6e 63 74 69 6f 6e 3a 20 4c 49 4b 45 2c 20 47  unction: LIKE, G
59dd0 4c 4f 42 2c 20 65 74 63 20 2a 2f 0a 23 64 65 66  LOB, etc */.#def
59de0 69 6e 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74  ine EP_ExpCollat
59df0 65 20 30 78 30 31 30 30 20 20 2f 2a 20 43 6f 6c  e 0x0100  /* Col
59e00 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
59e10 73 70 65 63 69 66 69 65 64 20 65 78 70 6c 69 63  specified explic
59e20 69 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  itly */.#define 
59e30 45 50 5f 41 6e 79 41 66 66 20 20 20 20 20 30 78  EP_AnyAff     0x
59e40 30 32 30 30 20 20 2f 2a 20 43 61 6e 20 74 61 6b  0200  /* Can tak
59e50 65 20 61 20 63 61 63 68 65 64 20 63 6f 6c 75 6d  e a cached colum
59e60 6e 20 6f 66 20 61 6e 79 20 61 66 66 69 6e 69 74  n of any affinit
59e70 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f  y */.#define EP_
59e80 46 69 78 65 64 44 65 73 74 20 20 30 78 30 34 30  FixedDest  0x040
59e90 30 20 20 2f 2a 20 52 65 73 75 6c 74 20 6e 65 65  0  /* Result nee
59ea0 64 65 64 20 69 6e 20 61 20 73 70 65 63 69 66 69  ded in a specifi
59eb0 63 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 0a 2f  c register */../
59ec0 2a 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f  *.** These macro
59ed0 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  s can be used to
59ee0 20 74 65 73 74 2c 20 73 65 74 2c 20 6f 72 20 63   test, set, or c
59ef0 6c 65 61 72 20 62 69 74 73 20 69 6e 20 74 68 65  lear bits in the
59f00 20 0a 2a 2a 20 45 78 70 72 2e 66 6c 61 67 73 20   .** Expr.flags 
59f10 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e  field..*/.#defin
59f20 65 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  e ExprHasPropert
59f30 79 28 45 2c 50 29 20 20 20 20 20 28 28 28 45 29  y(E,P)     (((E)
59f40 2d 3e 66 6c 61 67 73 26 28 50 29 29 3d 3d 28 50  ->flags&(P))==(P
59f50 29 29 0a 23 64 65 66 69 6e 65 20 45 78 70 72 48  )).#define ExprH
59f60 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 45 2c  asAnyProperty(E,
59f70 50 29 20 20 28 28 28 45 29 2d 3e 66 6c 61 67 73  P)  (((E)->flags
59f80 26 28 50 29 29 21 3d 30 29 0a 23 64 65 66 69 6e  &(P))!=0).#defin
59f90 65 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74  e ExprSetPropert
59fa0 79 28 45 2c 50 29 20 20 20 20 20 28 45 29 2d 3e  y(E,P)     (E)->
59fb0 66 6c 61 67 73 7c 3d 28 50 29 0a 23 64 65 66 69  flags|=(P).#defi
59fc0 6e 65 20 45 78 70 72 43 6c 65 61 72 50 72 6f 70  ne ExprClearProp
59fd0 65 72 74 79 28 45 2c 50 29 20 20 20 28 45 29 2d  erty(E,P)   (E)-
59fe0 3e 66 6c 61 67 73 26 3d 7e 28 50 29 0a 0a 2f 2a  >flags&=~(P)../*
59ff0 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66 20 65 78  .** A list of ex
5a000 70 72 65 73 73 69 6f 6e 73 2e 20 20 45 61 63 68  pressions.  Each
5a010 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 79 20   expression may 
5a020 6f 70 74 69 6f 6e 61 6c 6c 79 20 68 61 76 65 20  optionally have 
5a030 61 0a 2a 2a 20 6e 61 6d 65 2e 20 20 41 6e 20 65  a.** name.  An e
5a040 78 70 72 2f 6e 61 6d 65 20 63 6f 6d 62 69 6e 61  xpr/name combina
5a050 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64  tion can be used
5a060 20 69 6e 20 73 65 76 65 72 61 6c 20 77 61 79 73   in several ways
5a070 2c 20 73 75 63 68 0a 2a 2a 20 61 73 20 74 68 65  , such.** as the
5a080 20 6c 69 73 74 20 6f 66 20 22 65 78 70 72 20 41   list of "expr A
5a090 53 20 49 44 22 20 66 69 65 6c 64 73 20 66 6f 6c  S ID" fields fol
5a0a0 6c 6f 77 69 6e 67 20 61 20 22 53 45 4c 45 43 54  lowing a "SELECT
5a0b0 22 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 6c  " or in the.** l
5a0c0 69 73 74 20 6f 66 20 22 49 44 20 3d 20 65 78 70  ist of "ID = exp
5a0d0 72 22 20 69 74 65 6d 73 20 69 6e 20 61 6e 20 55  r" items in an U
5a0e0 50 44 41 54 45 2e 20 20 41 20 6c 69 73 74 20 6f  PDATE.  A list o
5a0f0 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 63 61  f expressions ca
5a100 6e 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 75 73 65  n.** also be use
5a110 64 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e  d as the argumen
5a120 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2c  t to a function,
5a130 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74   in which case t
5a140 68 65 20 61 2e 7a 4e 61 6d 65 0a 2a 2a 20 66 69  he a.zName.** fi
5a150 65 6c 64 20 69 73 20 6e 6f 74 20 75 73 65 64 2e  eld is not used.
5a160 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78 70 72 4c  .*/.struct ExprL
5a170 69 73 74 20 7b 0a 20 20 69 6e 74 20 6e 45 78 70  ist {.  int nExp
5a180 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  r;             /
5a190 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 70 72  * Number of expr
5a1a0 65 73 73 69 6f 6e 73 20 6f 6e 20 74 68 65 20 6c  essions on the l
5a1b0 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c  ist */.  int nAl
5a1c0 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  loc;            
5a1d0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
5a1e0 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62  ries allocated b
5a1f0 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 69 45  elow */.  int iE
5a200 43 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20  Cursor;         
5a210 20 2f 2a 20 56 44 42 45 20 43 75 72 73 6f 72 20   /* VDBE Cursor 
5a220 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
5a230 74 68 69 73 20 45 78 70 72 4c 69 73 74 20 2a 2f  this ExprList */
5a240 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69  .  struct ExprLi
5a250 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20 45 78  st_item {.    Ex
5a260 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20  pr *pExpr;      
5a270 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 69 73 74       /* The list
5a280 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20   of expressions 
5a290 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 61  */.    char *zNa
5a2a0 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  me;           /*
5a2b0 20 54 6f 6b 65 6e 20 61 73 73 6f 63 69 61 74 65   Token associate
5a2c0 64 20 77 69 74 68 20 74 68 69 73 20 65 78 70 72  d with this expr
5a2d0 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 75 38  ession */.    u8
5a2e0 20 73 6f 72 74 4f 72 64 65 72 3b 20 20 20 20 20   sortOrder;     
5a2f0 20 20 20 20 20 2f 2a 20 31 20 66 6f 72 20 44 45       /* 1 for DE
5a300 53 43 20 6f 72 20 30 20 66 6f 72 20 41 53 43 20  SC or 0 for ASC 
5a310 2a 2f 0a 20 20 20 20 75 38 20 69 73 41 67 67 3b  */.    u8 isAgg;
5a320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5a330 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73   True if this is
5a340 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 6c 69   an aggregate li
5a350 6b 65 20 63 6f 75 6e 74 28 2a 29 20 2a 2f 0a 20  ke count(*) */. 
5a360 20 20 20 75 38 20 64 6f 6e 65 3b 20 20 20 20 20     u8 done;     
5a370 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 66            /* A f
5a380 6c 61 67 20 74 6f 20 69 6e 64 69 63 61 74 65 20  lag to indicate 
5a390 77 68 65 6e 20 70 72 6f 63 65 73 73 69 6e 67 20  when processing 
5a3a0 69 73 20 66 69 6e 69 73 68 65 64 20 2a 2f 0a 20  is finished */. 
5a3b0 20 7d 20 2a 61 3b 20 20 20 20 20 20 20 20 20 20   } *a;          
5a3c0 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65          /* One e
5a3d0 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 65 78  ntry for each ex
5a3e0 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a  pression */.};..
5a3f0 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
5a400 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  e of this struct
5a410 75 72 65 20 63 61 6e 20 68 6f 6c 64 20 61 20 73  ure can hold a s
5a420 69 6d 70 6c 65 20 6c 69 73 74 20 6f 66 20 69 64  imple list of id
5a430 65 6e 74 69 66 69 65 72 73 2c 0a 2a 2a 20 73 75  entifiers,.** su
5a440 63 68 20 61 73 20 74 68 65 20 6c 69 73 74 20 22  ch as the list "
5a450 61 2c 62 2c 63 22 20 69 6e 20 74 68 65 20 66 6f  a,b,c" in the fo
5a460 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e  llowing statemen
5a470 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 49  ts:.**.**      I
5a480 4e 53 45 52 54 20 49 4e 54 4f 20 74 28 61 2c 62  NSERT INTO t(a,b
5a490 2c 63 29 20 56 41 4c 55 45 53 20 2e 2e 2e 3b 0a  ,c) VALUES ...;.
5a4a0 2a 2a 20 20 20 20 20 20 43 52 45 41 54 45 20 49  **      CREATE I
5a4b0 4e 44 45 58 20 69 64 78 20 4f 4e 20 74 28 61 2c  NDEX idx ON t(a,
5a4c0 62 2c 63 29 3b 0a 2a 2a 20 20 20 20 20 20 43 52  b,c);.**      CR
5a4d0 45 41 54 45 20 54 52 49 47 47 45 52 20 74 72 69  EATE TRIGGER tri
5a4e0 67 20 42 45 46 4f 52 45 20 55 50 44 41 54 45 20  g BEFORE UPDATE 
5a4f0 4f 4e 20 74 28 61 2c 62 2c 63 29 20 2e 2e 2e 3b  ON t(a,b,c) ...;
5a500 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 64 4c 69 73  .**.** The IdLis
5a510 74 2e 61 2e 69 64 78 20 66 69 65 6c 64 20 69 73  t.a.idx field is
5a520 20 75 73 65 64 20 77 68 65 6e 20 74 68 65 20 49   used when the I
5a530 64 4c 69 73 74 20 72 65 70 72 65 73 65 6e 74 73  dList represents
5a540 20 74 68 65 20 6c 69 73 74 20 6f 66 0a 2a 2a 20   the list of.** 
5a550 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 66 74  column names aft
5a560 65 72 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 20  er a table name 
5a570 69 6e 20 61 6e 20 49 4e 53 45 52 54 20 73 74 61  in an INSERT sta
5a580 74 65 6d 65 6e 74 2e 20 20 49 6e 20 74 68 65 20  tement.  In the 
5a590 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20  statement.**.** 
5a5a0 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
5a5b0 74 28 61 2c 62 2c 63 29 20 2e 2e 2e 0a 2a 2a 0a  t(a,b,c) ....**.
5a5c0 2a 2a 20 49 66 20 22 61 22 20 69 73 20 74 68 65  ** If "a" is the
5a5d0 20 6b 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20   k-th column of 
5a5e0 74 61 62 6c 65 20 22 74 22 2c 20 74 68 65 6e 20  table "t", then 
5a5f0 49 64 4c 69 73 74 2e 61 5b 30 5d 2e 69 64 78 3d  IdList.a[0].idx=
5a600 3d 6b 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49 64  =k..*/.struct Id
5a610 4c 69 73 74 20 7b 0a 20 20 73 74 72 75 63 74 20  List {.  struct 
5a620 49 64 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20  IdList_item {.  
5a630 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
5a640 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
5a650 68 65 20 69 64 65 6e 74 69 66 69 65 72 20 2a 2f  he identifier */
5a660 0a 20 20 20 20 69 6e 74 20 69 64 78 3b 20 20 20  .    int idx;   
5a670 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
5a680 69 6e 20 73 6f 6d 65 20 54 61 62 6c 65 2e 61 43  in some Table.aC
5a690 6f 6c 5b 5d 20 6f 66 20 61 20 63 6f 6c 75 6d 6e  ol[] of a column
5a6a0 20 6e 61 6d 65 64 20 7a 4e 61 6d 65 20 2a 2f 0a   named zName */.
5a6b0 20 20 7d 20 2a 61 3b 0a 20 20 69 6e 74 20 6e 49    } *a;.  int nI
5a6c0 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  d;         /* Nu
5a6d0 6d 62 65 72 20 6f 66 20 69 64 65 6e 74 69 66 69  mber of identifi
5a6e0 65 72 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 20  ers on the list 
5a6f0 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b  */.  int nAlloc;
5a700 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
5a710 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63  of entries alloc
5a720 61 74 65 64 20 66 6f 72 20 61 5b 5d 20 62 65 6c  ated for a[] bel
5a730 6f 77 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ow */.};../*.** 
5a740 54 68 65 20 62 69 74 6d 61 73 6b 20 64 61 74 61  The bitmask data
5a750 74 79 70 65 20 64 65 66 69 6e 65 64 20 62 65 6c  type defined bel
5a760 6f 77 20 69 73 20 75 73 65 64 20 66 6f 72 20 76  ow is used for v
5a770 61 72 69 6f 75 73 20 6f 70 74 69 6d 69 7a 61 74  arious optimizat
5a780 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e  ions..**.** Chan
5a790 67 69 6e 67 20 74 68 69 73 20 66 72 6f 6d 20 61  ging this from a
5a7a0 20 36 34 2d 62 69 74 20 74 6f 20 61 20 33 32 2d   64-bit to a 32-
5a7b0 62 69 74 20 74 79 70 65 20 6c 69 6d 69 74 73 20  bit type limits 
5a7c0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  the number of.**
5a7d0 20 74 61 62 6c 65 73 20 69 6e 20 61 20 6a 6f 69   tables in a joi
5a7e0 6e 20 74 6f 20 33 32 20 69 6e 73 74 65 61 64 20  n to 32 instead 
5a7f0 6f 66 20 36 34 2e 20 20 42 75 74 20 69 74 20 61  of 64.  But it a
5a800 6c 73 6f 20 72 65 64 75 63 65 73 20 74 68 65 20  lso reduces the 
5a810 73 69 7a 65 0a 2a 2a 20 6f 66 20 74 68 65 20 6c  size.** of the l
5a820 69 62 72 61 72 79 20 62 79 20 37 33 38 20 62 79  ibrary by 738 by
5a830 74 65 73 20 6f 6e 20 69 78 38 36 2e 0a 2a 2f 0a  tes on ix86..*/.
5a840 74 79 70 65 64 65 66 20 75 36 34 20 42 69 74 6d  typedef u64 Bitm
5a850 61 73 6b 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ask;../*.** The 
5a860 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
5a870 75 72 65 20 64 65 73 63 72 69 62 65 73 20 74 68  ure describes th
5a880 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66  e FROM clause of
5a890 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
5a8a0 65 6e 74 2e 0a 2a 2a 20 45 61 63 68 20 74 61 62  ent..** Each tab
5a8b0 6c 65 20 6f 72 20 73 75 62 71 75 65 72 79 20 69  le or subquery i
5a8c0 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
5a8d0 65 20 69 73 20 61 20 73 65 70 61 72 61 74 65 20  e is a separate 
5a8e0 65 6c 65 6d 65 6e 74 20 6f 66 0a 2a 2a 20 74 68  element of.** th
5a8f0 65 20 53 72 63 4c 69 73 74 2e 61 5b 5d 20 61 72  e SrcList.a[] ar
5a900 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20  ray..**.** With 
5a910 74 68 65 20 61 64 64 69 74 69 6f 6e 20 6f 66 20  the addition of 
5a920 6d 75 6c 74 69 70 6c 65 20 64 61 74 61 62 61 73  multiple databas
5a930 65 20 73 75 70 70 6f 72 74 2c 20 74 68 65 20 66  e support, the f
5a940 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
5a950 72 65 0a 2a 2a 20 63 61 6e 20 61 6c 73 6f 20 62  re.** can also b
5a960 65 20 75 73 65 64 20 74 6f 20 64 65 73 63 72 69  e used to descri
5a970 62 65 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  be a particular 
5a980 74 61 62 6c 65 20 73 75 63 68 20 61 73 20 74 68  table such as th
5a990 65 20 74 61 62 6c 65 20 74 68 61 74 0a 2a 2a 20  e table that.** 
5a9a0 69 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61  is modified by a
5a9b0 6e 20 49 4e 53 45 52 54 2c 20 44 45 4c 45 54 45  n INSERT, DELETE
5a9c0 2c 20 6f 72 20 55 50 44 41 54 45 20 73 74 61 74  , or UPDATE stat
5a9d0 65 6d 65 6e 74 2e 20 20 49 6e 20 73 74 61 6e 64  ement.  In stand
5a9e0 61 72 64 20 53 51 4c 2c 0a 2a 2a 20 73 75 63 68  ard SQL,.** such
5a9f0 20 61 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65   a table must be
5aa00 20 61 20 73 69 6d 70 6c 65 20 6e 61 6d 65 3a 20   a simple name: 
5aa10 49 44 2e 20 20 42 75 74 20 69 6e 20 53 51 4c 69  ID.  But in SQLi
5aa20 74 65 2c 20 74 68 65 20 74 61 62 6c 65 20 63 61  te, the table ca
5aa30 6e 0a 2a 2a 20 6e 6f 77 20 62 65 20 69 64 65 6e  n.** now be iden
5aa40 74 69 66 69 65 64 20 62 79 20 61 20 64 61 74 61  tified by a data
5aa50 62 61 73 65 20 6e 61 6d 65 2c 20 61 20 64 6f 74  base name, a dot
5aa60 2c 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65  , then the table
5aa70 20 6e 61 6d 65 3a 20 49 44 2e 49 44 2e 0a 2a 2a   name: ID.ID..**
5aa80 0a 2a 2a 20 54 68 65 20 6a 6f 69 6e 74 79 70 65  .** The jointype
5aa90 20 73 74 61 72 74 73 20 6f 75 74 20 73 68 6f 77   starts out show
5aaa0 69 6e 67 20 74 68 65 20 6a 6f 69 6e 20 74 79 70  ing the join typ
5aab0 65 20 62 65 74 77 65 65 6e 20 74 68 65 20 63 75  e between the cu
5aac0 72 72 65 6e 74 20 74 61 62 6c 65 0a 2a 2a 20 61  rrent table.** a
5aad0 6e 64 20 74 68 65 20 6e 65 78 74 20 74 61 62 6c  nd the next tabl
5aae0 65 20 6f 6e 20 74 68 65 20 6c 69 73 74 2e 20 20  e on the list.  
5aaf0 54 68 65 20 70 61 72 73 65 72 20 62 75 69 6c 64  The parser build
5ab00 73 20 74 68 65 20 6c 69 73 74 20 74 68 69 73 20  s the list this 
5ab10 77 61 79 2e 0a 2a 2a 20 42 75 74 20 73 71 6c 69  way..** But sqli
5ab20 74 65 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a  te3SrcListShiftJ
5ab30 6f 69 6e 54 79 70 65 28 29 20 6c 61 74 65 72 20  oinType() later 
5ab40 73 68 69 66 74 73 20 74 68 65 20 6a 6f 69 6e 74  shifts the joint
5ab50 79 70 65 73 20 73 6f 20 74 68 61 74 20 65 61 63  ypes so that eac
5ab60 68 0a 2a 2a 20 6a 6f 69 6e 74 79 70 65 20 65 78  h.** jointype ex
5ab70 70 72 65 73 73 65 73 20 74 68 65 20 6a 6f 69 6e  presses the join
5ab80 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 61 62   between the tab
5ab90 6c 65 20 61 6e 64 20 74 68 65 20 70 72 65 76 69  le and the previ
5aba0 6f 75 73 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74  ous table..*/.st
5abb0 72 75 63 74 20 53 72 63 4c 69 73 74 20 7b 0a 20  ruct SrcList {. 
5abc0 20 69 31 36 20 6e 53 72 63 3b 20 20 20 20 20 20   i16 nSrc;      
5abd0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74    /* Number of t
5abe0 61 62 6c 65 73 20 6f 72 20 73 75 62 71 75 65 72  ables or subquer
5abf0 69 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ies in the FROM 
5ac00 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 31 36 20  clause */.  i16 
5ac10 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20  nAlloc;      /* 
5ac20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
5ac30 73 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 61  s allocated in a
5ac40 5b 5d 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 73 74  [] below */.  st
5ac50 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
5ac60 6d 20 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 44  m {.    char *zD
5ac70 61 74 61 62 61 73 65 3b 20 20 2f 2a 20 4e 61 6d  atabase;  /* Nam
5ac80 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 68 6f  e of database ho
5ac90 6c 64 69 6e 67 20 74 68 69 73 20 74 61 62 6c 65  lding this table
5aca0 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e   */.    char *zN
5acb0 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d  ame;      /* Nam
5acc0 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a  e of the table *
5acd0 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 41 6c 69  /.    char *zAli
5ace0 61 73 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 22  as;     /* The "
5acf0 42 22 20 70 61 72 74 20 6f 66 20 61 20 22 41 20  B" part of a "A 
5ad00 41 53 20 42 22 20 70 68 72 61 73 65 2e 20 20 7a  AS B" phrase.  z
5ad10 4e 61 6d 65 20 69 73 20 74 68 65 20 22 41 22 20  Name is the "A" 
5ad20 2a 2f 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  */.    Table *pT
5ad30 61 62 3b 20 20 20 20 20 20 2f 2a 20 41 6e 20 53  ab;      /* An S
5ad40 51 4c 20 74 61 62 6c 65 20 63 6f 72 72 65 73 70  QL table corresp
5ad50 6f 6e 64 69 6e 67 20 74 6f 20 7a 4e 61 6d 65 20  onding to zName 
5ad60 2a 2f 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70  */.    Select *p
5ad70 53 65 6c 65 63 74 3b 20 20 2f 2a 20 41 20 53 45  Select;  /* A SE
5ad80 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 75  LECT statement u
5ad90 73 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20  sed in place of 
5ada0 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a  a table name */.
5adb0 20 20 20 20 75 38 20 69 73 50 6f 70 75 6c 61 74      u8 isPopulat
5adc0 65 64 3b 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61  ed;   /* Tempora
5add0 72 79 20 74 61 62 6c 65 20 61 73 73 6f 63 69 61  ry table associa
5ade0 74 65 64 20 77 69 74 68 20 53 45 4c 45 43 54 20  ted with SELECT 
5adf0 69 73 20 70 6f 70 75 6c 61 74 65 64 20 2a 2f 0a  is populated */.
5ae00 20 20 20 20 75 38 20 6a 6f 69 6e 74 79 70 65 3b      u8 jointype;
5ae10 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66        /* Type of
5ae20 20 6a 6f 69 6e 20 62 65 74 77 65 65 6e 20 74 68   join between th
5ae30 69 73 20 61 62 6c 65 20 61 6e 64 20 74 68 65 20  is able and the 
5ae40 70 72 65 76 69 6f 75 73 20 2a 2f 0a 20 20 20 20  previous */.    
5ae50 69 6e 74 20 69 43 75 72 73 6f 72 3b 20 20 20 20  int iCursor;    
5ae60 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75    /* The VDBE cu
5ae70 72 73 6f 72 20 6e 75 6d 62 65 72 20 75 73 65 64  rsor number used
5ae80 20 74 6f 20 61 63 63 65 73 73 20 74 68 69 73 20   to access this 
5ae90 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 45 78 70  table */.    Exp
5aea0 72 20 2a 70 4f 6e 3b 20 20 20 20 20 20 20 20 2f  r *pOn;        /
5aeb0 2a 20 54 68 65 20 4f 4e 20 63 6c 61 75 73 65 20  * The ON clause 
5aec0 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 20  of a join */.   
5aed0 20 49 64 4c 69 73 74 20 2a 70 55 73 69 6e 67 3b   IdList *pUsing;
5aee0 20 20 20 2f 2a 20 54 68 65 20 55 53 49 4e 47 20     /* The USING 
5aef0 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e  clause of a join
5af00 20 2a 2f 0a 20 20 20 20 42 69 74 6d 61 73 6b 20   */.    Bitmask 
5af10 63 6f 6c 55 73 65 64 3b 20 20 2f 2a 20 42 69 74  colUsed;  /* Bit
5af20 20 4e 20 28 31 3c 3c 4e 29 20 73 65 74 20 69 66   N (1<<N) set if
5af30 20 63 6f 6c 75 6d 6e 20 4e 20 6f 72 20 70 54 61   column N or pTa
5af40 62 20 69 73 20 75 73 65 64 20 2a 2f 0a 20 20 7d  b is used */.  }
5af50 20 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 20   a[1];          
5af60 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20     /* One entry 
5af70 66 6f 72 20 65 61 63 68 20 69 64 65 6e 74 69 66  for each identif
5af80 69 65 72 20 6f 6e 20 74 68 65 20 6c 69 73 74 20  ier on the list 
5af90 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72  */.};../*.** Per
5afa0 6d 69 74 74 65 64 20 76 61 6c 75 65 73 20 6f 66  mitted values of
5afb0 20 74 68 65 20 53 72 63 4c 69 73 74 2e 61 2e 6a   the SrcList.a.j
5afc0 6f 69 6e 74 79 70 65 20 66 69 65 6c 64 0a 2a 2f  ointype field.*/
5afd0 0a 23 64 65 66 69 6e 65 20 4a 54 5f 49 4e 4e 45  .#define JT_INNE
5afe0 52 20 20 20 20 20 30 78 30 30 30 31 20 20 20 20  R     0x0001    
5aff0 2f 2a 20 41 6e 79 20 6b 69 6e 64 20 6f 66 20 69  /* Any kind of i
5b000 6e 6e 65 72 20 6f 72 20 63 72 6f 73 73 20 6a 6f  nner or cross jo
5b010 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54  in */.#define JT
5b020 5f 43 52 4f 53 53 20 20 20 20 20 30 78 30 30 30  _CROSS     0x000
5b030 32 20 20 20 20 2f 2a 20 45 78 70 6c 69 63 69 74  2    /* Explicit
5b040 20 75 73 65 20 6f 66 20 74 68 65 20 43 52 4f 53   use of the CROS
5b050 53 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65  S keyword */.#de
5b060 66 69 6e 65 20 4a 54 5f 4e 41 54 55 52 41 4c 20  fine JT_NATURAL 
5b070 20 20 30 78 30 30 30 34 20 20 20 20 2f 2a 20 54    0x0004    /* T
5b080 72 75 65 20 66 6f 72 20 61 20 22 6e 61 74 75 72  rue for a "natur
5b090 61 6c 22 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66  al" join */.#def
5b0a0 69 6e 65 20 4a 54 5f 4c 45 46 54 20 20 20 20 20  ine JT_LEFT     
5b0b0 20 30 78 30 30 30 38 20 20 20 20 2f 2a 20 4c 65   0x0008    /* Le
5b0c0 66 74 20 6f 75 74 65 72 20 6a 6f 69 6e 20 2a 2f  ft outer join */
5b0d0 0a 23 64 65 66 69 6e 65 20 4a 54 5f 52 49 47 48  .#define JT_RIGH
5b0e0 54 20 20 20 20 20 30 78 30 30 31 30 20 20 20 20  T     0x0010    
5b0f0 2f 2a 20 52 69 67 68 74 20 6f 75 74 65 72 20 6a  /* Right outer j
5b100 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a  oin */.#define J
5b110 54 5f 4f 55 54 45 52 20 20 20 20 20 30 78 30 30  T_OUTER     0x00
5b120 32 30 20 20 20 20 2f 2a 20 54 68 65 20 22 4f 55  20    /* The "OU
5b130 54 45 52 22 20 6b 65 79 77 6f 72 64 20 69 73 20  TER" keyword is 
5b140 70 72 65 73 65 6e 74 20 2a 2f 0a 23 64 65 66 69  present */.#defi
5b150 6e 65 20 4a 54 5f 45 52 52 4f 52 20 20 20 20 20  ne JT_ERROR     
5b160 30 78 30 30 34 30 20 20 20 20 2f 2a 20 75 6e 6b  0x0040    /* unk
5b170 6e 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70 6f 72  nown or unsuppor
5b180 74 65 64 20 6a 6f 69 6e 20 74 79 70 65 20 2a 2f  ted join type */
5b190 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 65 61 63 68  ../*.** For each
5b1a0 20 6e 65 73 74 65 64 20 6c 6f 6f 70 20 69 6e 20   nested loop in 
5b1b0 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 69  a WHERE clause i
5b1c0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 74  mplementation, t
5b1d0 68 65 20 57 68 65 72 65 49 6e 66 6f 0a 2a 2a 20  he WhereInfo.** 
5b1e0 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
5b1f0 6e 73 20 61 20 73 69 6e 67 6c 65 20 69 6e 73 74  ns a single inst
5b200 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
5b210 75 63 74 75 72 65 2e 20 20 54 68 69 73 20 73 74  ucture.  This st
5b220 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 69 6e  ructure.** is in
5b230 74 65 6e 64 65 64 20 74 6f 20 62 65 20 70 72 69  tended to be pri
5b240 76 61 74 65 20 74 68 65 20 74 68 65 20 77 68 65  vate the the whe
5b250 72 65 2e 63 20 6d 6f 64 75 6c 65 20 61 6e 64 20  re.c module and 
5b260 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a  should not be.**
5b270 20 61 63 63 65 73 73 20 6f 72 20 6d 6f 64 69 66   access or modif
5b280 69 65 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64  ied by other mod
5b290 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ules..**.** The 
5b2a0 70 49 64 78 49 6e 66 6f 20 61 6e 64 20 70 42 65  pIdxInfo and pBe
5b2b0 73 74 49 64 78 20 66 69 65 6c 64 73 20 61 72 65  stIdx fields are
5b2c0 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 70 69   used to help pi
5b2d0 63 6b 20 74 68 65 20 62 65 73 74 0a 2a 2a 20 69  ck the best.** i
5b2e0 6e 64 65 78 20 6f 6e 20 61 20 76 69 72 74 75 61  ndex on a virtua
5b2f0 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20 70 49  l table.  The pI
5b300 64 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72 20 63  dxInfo pointer c
5b310 6f 6e 74 61 69 6e 73 20 69 6e 64 65 78 69 6e 67  ontains indexing
5b320 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  .** information 
5b330 66 6f 72 20 74 68 65 20 69 2d 74 68 20 74 61 62  for the i-th tab
5b340 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  le in the FROM c
5b350 6c 61 75 73 65 20 62 65 66 6f 72 65 20 72 65 6f  lause before reo
5b360 72 64 65 72 69 6e 67 2e 0a 2a 2a 20 41 6c 6c 20  rdering..** All 
5b370 74 68 65 20 70 49 64 78 49 6e 66 6f 20 70 6f 69  the pIdxInfo poi
5b380 6e 74 65 72 73 20 61 72 65 20 66 72 65 65 64 20  nters are freed 
5b390 62 79 20 77 68 65 72 65 49 6e 66 6f 46 72 65 65  by whereInfoFree
5b3a0 28 29 20 69 6e 20 77 68 65 72 65 2e 63 2e 0a 2a  () in where.c..*
5b3b0 2a 20 54 68 65 20 70 42 65 73 74 49 64 78 20 70  * The pBestIdx p
5b3c0 6f 69 6e 74 65 72 20 69 73 20 61 20 63 6f 70 79  ointer is a copy
5b3d0 20 6f 66 20 70 49 64 78 49 6e 66 6f 20 66 6f 72   of pIdxInfo for
5b3e0 20 74 68 65 20 69 2d 74 68 20 74 61 62 6c 65 20   the i-th table 
5b3f0 61 66 74 65 72 0a 2a 2a 20 46 52 4f 4d 20 63 6c  after.** FROM cl
5b400 61 75 73 65 20 6f 72 64 65 72 69 6e 67 2e 20 20  ause ordering.  
5b410 54 68 69 73 20 69 73 20 61 20 6c 69 74 74 6c 65  This is a little
5b420 20 63 6f 6e 66 75 73 69 6e 67 20 73 6f 20 49 20   confusing so I 
5b430 77 69 6c 6c 20 72 65 70 65 61 74 0a 2a 2a 20 69  will repeat.** i
5b440 74 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 77  t in different w
5b450 6f 72 64 73 2e 20 20 57 68 65 72 65 49 6e 66 6f  ords.  WhereInfo
5b460 2e 61 5b 69 5d 2e 70 49 64 78 49 6e 66 6f 20 69  .a[i].pIdxInfo i
5b470 73 20 69 6e 64 65 78 20 69 6e 66 6f 72 6d 61 74  s index informat
5b480 69 6f 6e 20 0a 2a 2a 20 66 6f 72 20 57 68 65 72  ion .** for Wher
5b490 65 49 6e 66 6f 2e 70 54 61 62 4c 69 73 74 2e 61  eInfo.pTabList.a
5b4a0 5b 69 5d 2e 20 20 57 68 65 72 65 49 6e 66 6f 2e  [i].  WhereInfo.
5b4b0 61 5b 69 5d 2e 70 42 65 73 74 49 6e 66 6f 20 69  a[i].pBestInfo i
5b4c0 73 20 74 68 65 0a 2a 2a 20 69 6e 64 65 78 20 69  s the.** index i
5b4d0 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74  nformation for t
5b4e0 68 65 20 69 2d 74 68 20 6c 6f 6f 70 20 6f 66 20  he i-th loop of 
5b4f0 74 68 65 20 6a 6f 69 6e 2e 20 20 70 42 65 73 74  the join.  pBest
5b500 49 6e 66 6f 20 69 73 20 61 6c 77 61 79 73 0a 2a  Info is always.*
5b510 2a 20 65 69 74 68 65 72 20 4e 55 4c 4c 20 6f 72  * either NULL or
5b520 20 61 20 63 6f 70 79 20 6f 66 20 73 6f 6d 65 20   a copy of some 
5b530 70 49 64 78 49 6e 66 6f 2e 20 20 53 6f 20 66 6f  pIdxInfo.  So fo
5b540 72 20 63 6c 65 61 6e 75 70 20 69 74 20 69 73 20  r cleanup it is 
5b550 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 20 74  .** sufficient t
5b560 6f 20 66 72 65 65 20 61 6c 6c 20 6f 66 20 74 68  o free all of th
5b570 65 20 70 49 64 78 49 6e 66 6f 20 70 6f 69 6e 74  e pIdxInfo point
5b580 65 72 73 2e 0a 2a 2a 20 0a 2a 2f 0a 73 74 72 75  ers..** .*/.stru
5b590 63 74 20 57 68 65 72 65 4c 65 76 65 6c 20 7b 0a  ct WhereLevel {.
5b5a0 20 20 69 6e 74 20 69 46 72 6f 6d 3b 20 20 20 20    int iFrom;    
5b5b0 20 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63 68          /* Which
5b5c0 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 46 52   entry in the FR
5b5d0 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69  OM clause */.  i
5b5e0 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20  nt flags;       
5b5f0 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 61 73       /* Flags as
5b600 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
5b610 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 69 6e  is level */.  in
5b620 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 20  t iMem;         
5b630 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6d 65 6d      /* First mem
5b640 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 62 79  ory cell used by
5b650 20 74 68 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20   this level */. 
5b660 20 69 6e 74 20 69 4c 65 66 74 4a 6f 69 6e 3b 20   int iLeftJoin; 
5b670 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79         /* Memory
5b680 20 63 65 6c 6c 20 75 73 65 64 20 74 6f 20 69 6d   cell used to im
5b690 70 6c 65 6d 65 6e 74 20 4c 45 46 54 20 4f 55 54  plement LEFT OUT
5b6a0 45 52 20 4a 4f 49 4e 20 2a 2f 0a 20 20 49 6e 64  ER JOIN */.  Ind
5b6b0 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20  ex *pIdx;       
5b6c0 20 20 20 2f 2a 20 49 6e 64 65 78 20 75 73 65 64     /* Index used
5b6d0 2e 20 20 4e 55 4c 4c 20 69 66 20 6e 6f 20 69 6e  .  NULL if no in
5b6e0 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61  dex */.  int iTa
5b6f0 62 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 2f  bCur;          /
5b700 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f  * The VDBE curso
5b710 72 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73  r used to access
5b720 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20   the table */.  
5b730 69 6e 74 20 69 49 64 78 43 75 72 3b 20 20 20 20  int iIdxCur;    
5b740 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42        /* The VDB
5b750 45 20 63 75 72 73 6f 72 20 75 73 65 64 20 74 6f  E cursor used to
5b760 20 61 63 65 73 73 73 20 70 49 64 78 20 2a 2f 0a   acesss pIdx */.
5b770 20 20 69 6e 74 20 62 72 6b 3b 20 20 20 20 20 20    int brk;      
5b780 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20          /* Jump 
5b790 68 65 72 65 20 74 6f 20 62 72 65 61 6b 20 6f 75  here to break ou
5b7a0 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f  t of the loop */
5b7b0 0a 20 20 69 6e 74 20 6e 78 74 3b 20 20 20 20 20  .  int nxt;     
5b7c0 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70           /* Jump
5b7d0 20 68 65 72 65 20 74 6f 20 73 74 61 72 74 20 74   here to start t
5b7e0 68 65 20 6e 65 78 74 20 49 4e 20 63 6f 6d 62 69  he next IN combi
5b7f0 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  nation */.  int 
5b800 63 6f 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  cont;           
5b810 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
5b820 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20  o continue with 
5b830 74 68 65 20 6e 65 78 74 20 6c 6f 6f 70 20 63 79  the next loop cy
5b840 63 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70  cle */.  int top
5b850 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
5b860 2a 20 46 69 72 73 74 20 69 6e 73 74 72 75 63 74  * First instruct
5b870 69 6f 6e 20 6f 66 20 69 6e 74 65 72 69 6f 72 20  ion of interior 
5b880 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20  of the loop */. 
5b890 20 69 6e 74 20 6f 70 2c 20 70 31 2c 20 70 32 3b   int op, p1, p2;
5b8a0 20 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65         /* Opcode
5b8b0 20 75 73 65 64 20 74 6f 20 74 65 72 6d 69 6e 61   used to termina
5b8c0 74 65 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20  te the loop */. 
5b8d0 20 69 6e 74 20 6e 45 71 3b 20 20 20 20 20 20 20   int nEq;       
5b8e0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
5b8f0 20 6f 66 20 3d 3d 20 6f 72 20 49 4e 20 63 6f 6e   of == or IN con
5b900 73 74 72 61 69 6e 74 73 20 6f 6e 20 74 68 69 73  straints on this
5b910 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e   loop */.  int n
5b920 49 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  In;             
5b930 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 49 4e   /* Number of IN
5b940 20 6f 70 65 72 61 74 6f 72 73 20 63 6f 6e 73 74   operators const
5b950 72 61 69 6e 69 6e 67 20 74 68 69 73 20 6c 6f 6f  raining this loo
5b960 70 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 49 6e  p */.  struct In
5b970 4c 6f 6f 70 20 7b 0a 20 20 20 20 69 6e 74 20 69  Loop {.    int i
5b980 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  Cur;            
5b990 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75    /* The VDBE cu
5b9a0 72 73 6f 72 20 75 73 65 64 20 62 79 20 74 68 69  rsor used by thi
5b9b0 73 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f  s IN operator */
5b9c0 0a 20 20 20 20 69 6e 74 20 74 6f 70 41 64 64 72  .    int topAddr
5b9d0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
5b9e0 6f 70 20 6f 66 20 74 68 65 20 49 4e 20 6c 6f 6f  op of the IN loo
5b9f0 70 20 2a 2f 0a 20 20 7d 20 2a 61 49 6e 4c 6f 6f  p */.  } *aInLoo
5ba00 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  p;           /* 
5ba10 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  Information abou
5ba20 74 20 65 61 63 68 20 6e 65 73 74 65 64 20 49 4e  t each nested IN
5ba30 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 73   operator */.  s
5ba40 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
5ba50 6f 20 2a 70 42 65 73 74 49 64 78 3b 20 20 2f 2a  o *pBestIdx;  /*
5ba60 20 49 6e 64 65 78 20 69 6e 66 6f 72 6d 61 74 69   Index informati
5ba70 6f 6e 20 66 6f 72 20 74 68 69 73 20 6c 65 76 65  on for this leve
5ba80 6c 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 66  l */..  /* The f
5ba90 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 20 69  ollowing field i
5baa0 73 20 72 65 61 6c 6c 79 20 6e 6f 74 20 70 61 72  s really not par
5bab0 74 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74  t of the current
5bac0 20 6c 65 76 65 6c 2e 20 20 42 75 74 0a 20 20 2a   level.  But.  *
5bad0 2a 20 77 65 20 6e 65 65 64 20 61 20 70 6c 61 63  * we need a plac
5bae0 65 20 74 6f 20 63 61 63 68 65 20 69 6e 64 65 78  e to cache index
5baf0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72   information for
5bb00 20 65 61 63 68 20 74 61 62 6c 65 20 69 6e 20 74   each table in t
5bb10 68 65 0a 20 20 2a 2a 20 46 52 4f 4d 20 63 6c 61  he.  ** FROM cla
5bb20 75 73 65 20 61 6e 64 20 74 68 65 20 57 68 65 72  use and the Wher
5bb30 65 4c 65 76 65 6c 20 73 74 72 75 63 74 75 72 65  eLevel structure
5bb40 20 69 73 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74   is a convenient
5bb50 20 70 6c 61 63 65 2e 0a 20 20 2a 2f 0a 20 20 73   place..  */.  s
5bb60 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
5bb70 6f 20 2a 70 49 64 78 49 6e 66 6f 3b 20 20 2f 2a  o *pIdxInfo;  /*
5bb80 20 49 6e 64 65 78 20 69 6e 66 6f 20 66 6f 72 20   Index info for 
5bb90 6e 2d 74 68 20 73 6f 75 72 63 65 20 74 61 62 6c  n-th source tabl
5bba0 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46  e */.};../*.** F
5bbb0 6c 61 67 73 20 61 70 70 72 6f 70 72 69 61 74 65  lags appropriate
5bbc0 20 66 6f 72 20 74 68 65 20 77 66 6c 61 67 73 20   for the wflags 
5bbd0 70 61 72 61 6d 65 74 65 72 20 6f 66 20 73 71 6c  parameter of sql
5bbe0 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
5bbf0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  ..*/.#define WHE
5bc00 52 45 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41  RE_ORDERBY_NORMA
5bc10 4c 20 20 20 20 20 30 20 20 20 2f 2a 20 4e 6f 2d  L     0   /* No-
5bc20 6f 70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48  op */.#define WH
5bc30 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 20  ERE_ORDERBY_MIN 
5bc40 20 20 20 20 20 20 20 31 20 20 20 2f 2a 20 4f 52         1   /* OR
5bc50 44 45 52 20 42 59 20 70 72 6f 63 65 73 73 69 6e  DER BY processin
5bc60 67 20 66 6f 72 20 6d 69 6e 28 29 20 66 75 6e 63  g for min() func
5bc70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52   */.#define WHER
5bc80 45 5f 4f 52 44 45 52 42 59 5f 4d 41 58 20 20 20  E_ORDERBY_MAX   
5bc90 20 20 20 20 20 32 20 20 20 2f 2a 20 4f 52 44 45       2   /* ORDE
5bca0 52 20 42 59 20 70 72 6f 63 65 73 73 69 6e 67 20  R BY processing 
5bcb0 66 6f 72 20 6d 61 78 28 29 20 66 75 6e 63 20 2a  for max() func *
5bcc0 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
5bcd0 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 45 44 20  ONEPASS_DESIRED 
5bce0 20 20 20 34 20 20 20 2f 2a 20 57 61 6e 74 20 74     4   /* Want t
5bcf0 6f 20 64 6f 20 6f 6e 65 2d 70 61 73 73 20 55 50  o do one-pass UP
5bd00 44 41 54 45 2f 44 45 4c 45 54 45 20 2a 2f 0a 0a  DATE/DELETE */..
5bd10 2f 2a 0a 2a 2a 20 54 68 65 20 57 48 45 52 45 20  /*.** The WHERE 
5bd20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e  clause processin
5bd30 67 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 77  g routine has tw
5bd40 6f 20 68 61 6c 76 65 73 2e 20 20 54 68 65 0a 2a  o halves.  The.*
5bd50 2a 20 66 69 72 73 74 20 70 61 72 74 20 64 6f 65  * first part doe
5bd60 73 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  s the start of t
5bd70 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 61 6e  he WHERE loop an
5bd80 64 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20  d the second.** 
5bd90 68 61 6c 66 20 64 6f 65 73 20 74 68 65 20 74 61  half does the ta
5bda0 69 6c 20 6f 66 20 74 68 65 20 57 48 45 52 45 20  il of the WHERE 
5bdb0 6c 6f 6f 70 2e 20 20 41 6e 20 69 6e 73 74 61 6e  loop.  An instan
5bdc0 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 74  ce of.** this st
5bdd0 72 75 63 74 75 72 65 20 69 73 20 72 65 74 75 72  ructure is retur
5bde0 6e 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74  ned by the first
5bdf0 20 68 61 6c 66 20 61 6e 64 20 70 61 73 73 65 64   half and passed
5be00 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 65 63  .** into the sec
5be10 6f 6e 64 20 68 61 6c 66 20 74 6f 20 67 69 76 65  ond half to give
5be20 20 73 6f 6d 65 20 63 6f 6e 74 69 6e 75 69 74 79   some continuity
5be30 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72  ..*/.struct Wher
5be40 65 49 6e 66 6f 20 7b 0a 20 20 50 61 72 73 65 20  eInfo {.  Parse 
5be50 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 2f  *pParse;       /
5be60 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f  * Parsing and co
5be70 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  de generating co
5be80 6e 74 65 78 74 20 2a 2f 0a 20 20 75 38 20 6f 6b  ntext */.  u8 ok
5be90 4f 6e 65 50 61 73 73 3b 20 20 20 20 20 20 20 20  OnePass;        
5bea0 2f 2a 20 4f 6b 20 74 6f 20 75 73 65 20 6f 6e 65  /* Ok to use one
5beb0 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 20  -pass algorithm 
5bec0 66 6f 72 20 55 50 44 41 54 45 20 6f 72 20 44 45  for UPDATE or DE
5bed0 4c 45 54 45 20 2a 2f 0a 20 20 53 72 63 4c 69 73  LETE */.  SrcLis
5bee0 74 20 2a 70 54 61 62 4c 69 73 74 3b 20 20 20 2f  t *pTabList;   /
5bef0 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73  * List of tables
5bf00 20 69 6e 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a   in the join */.
5bf10 20 20 69 6e 74 20 69 54 6f 70 3b 20 20 20 20 20    int iTop;     
5bf20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 65         /* The ve
5bf30 72 79 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  ry beginning of 
5bf40 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 2a  the WHERE loop *
5bf50 2f 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75  /.  int iContinu
5bf60 65 3b 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70  e;       /* Jump
5bf70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75   here to continu
5bf80 65 20 77 69 74 68 20 6e 65 78 74 20 72 65 63 6f  e with next reco
5bf90 72 64 20 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65  rd */.  int iBre
5bfa0 61 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ak;          /* 
5bfb0 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 62 72 65  Jump here to bre
5bfc0 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f  ak out of the lo
5bfd0 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 65 76  op */.  int nLev
5bfe0 65 6c 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  el;          /* 
5bff0 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64  Number of nested
5c000 20 6c 6f 6f 70 20 2a 2f 0a 20 20 73 71 6c 69 74   loop */.  sqlit
5c010 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 2a  e3_index_info **
5c020 61 70 49 6e 66 6f 3b 20 20 2f 2a 20 41 72 72 61  apInfo;  /* Arra
5c030 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f  y of pointers to
5c040 20 69 6e 64 65 78 20 69 6e 66 6f 20 73 74 72 75   index info stru
5c050 63 74 75 72 65 73 20 2a 2f 0a 20 20 57 68 65 72  ctures */.  Wher
5c060 65 4c 65 76 65 6c 20 61 5b 31 5d 3b 20 20 20 20  eLevel a[1];    
5c070 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
5c080 61 62 6f 75 74 20 65 61 63 68 20 6e 65 73 74 20  about each nest 
5c090 6c 6f 6f 70 20 69 6e 20 74 68 65 20 57 48 45 52  loop in the WHER
5c0a0 45 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  E */.};../*.** A
5c0b0 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 64 65 66   NameContext def
5c0c0 69 6e 65 73 20 61 20 63 6f 6e 74 65 78 74 20 69  ines a context i
5c0d0 6e 20 77 68 69 63 68 20 74 6f 20 72 65 73 6f 6c  n which to resol
5c0e0 76 65 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c  ve table and col
5c0f0 75 6d 6e 0a 2a 2a 20 6e 61 6d 65 73 2e 20 20 54  umn.** names.  T
5c100 68 65 20 63 6f 6e 74 65 78 74 20 63 6f 6e 73 69  he context consi
5c110 73 74 73 20 6f 66 20 61 20 6c 69 73 74 20 6f 66  sts of a list of
5c120 20 74 61 62 6c 65 73 20 28 74 68 65 20 70 53 72   tables (the pSr
5c130 63 4c 69 73 74 29 20 66 69 65 6c 64 20 61 6e 64  cList) field and
5c140 0a 2a 2a 20 61 20 6c 69 73 74 20 6f 66 20 6e 61  .** a list of na
5c150 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 28  med expression (
5c160 70 45 4c 69 73 74 29 2e 20 20 54 68 65 20 6e 61  pEList).  The na
5c170 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 6c  med expression l
5c180 69 73 74 20 6d 61 79 0a 2a 2a 20 62 65 20 4e 55  ist may.** be NU
5c190 4c 4c 2e 20 20 54 68 65 20 70 53 72 63 20 63 6f  LL.  The pSrc co
5c1a0 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65  rresponds to the
5c1b0 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
5c1c0 61 20 53 45 4c 45 43 54 20 6f 72 0a 2a 2a 20 74  a SELECT or.** t
5c1d0 6f 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e  o the table bein
5c1e0 67 20 6f 70 65 72 61 74 65 64 20 6f 6e 20 62 79  g operated on by
5c1f0 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c   INSERT, UPDATE,
5c200 20 6f 72 20 44 45 4c 45 54 45 2e 20 20 54 68 65   or DELETE.  The
5c210 0a 2a 2a 20 70 45 4c 69 73 74 20 63 6f 72 72 65  .** pEList corre
5c220 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 72 65  sponds to the re
5c230 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20 53 45  sult set of a SE
5c240 4c 45 43 54 20 61 6e 64 20 69 73 20 4e 55 4c 4c  LECT and is NULL
5c250 20 66 6f 72 0a 2a 2a 20 6f 74 68 65 72 20 73 74   for.** other st
5c260 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20  atements..**.** 
5c270 4e 61 6d 65 43 6f 6e 74 65 78 74 73 20 63 61 6e  NameContexts can
5c280 20 62 65 20 6e 65 73 74 65 64 2e 20 20 57 68 65   be nested.  Whe
5c290 6e 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65  n resolving name
5c2a0 73 2c 20 74 68 65 20 69 6e 6e 65 72 2d 6d 6f 73  s, the inner-mos
5c2b0 74 20 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73  t .** context is
5c2c0 20 73 65 61 72 63 68 65 64 20 66 69 72 73 74 2e   searched first.
5c2d0 20 20 49 66 20 6e 6f 20 6d 61 74 63 68 20 69 73    If no match is
5c2e0 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e 65 78 74   found, the next
5c2f0 20 6f 75 74 65 72 0a 2a 2a 20 63 6f 6e 74 65 78   outer.** contex
5c300 74 20 69 73 20 63 68 65 63 6b 65 64 2e 20 20 49  t is checked.  I
5c310 66 20 74 68 65 72 65 20 69 73 20 73 74 69 6c 6c  f there is still
5c320 20 6e 6f 20 6d 61 74 63 68 2c 20 74 68 65 20 6e   no match, the n
5c330 65 78 74 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 69  ext context.** i
5c340 73 20 63 68 65 63 6b 65 64 2e 20 20 54 68 69 73  s checked.  This
5c350 20 70 72 6f 63 65 73 73 20 63 6f 6e 74 69 6e 75   process continu
5c360 65 73 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20  es until either 
5c370 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64  a match is found
5c380 0a 2a 2a 20 6f 72 20 61 6c 6c 20 63 6f 6e 74 65  .** or all conte
5c390 78 74 73 20 61 72 65 20 63 68 65 63 6b 2e 20 20  xts are check.  
5c3a0 57 68 65 6e 20 61 20 6d 61 74 63 68 20 69 73 20  When a match is 
5c3b0 66 6f 75 6e 64 2c 20 74 68 65 20 6e 52 65 66 20  found, the nRef 
5c3c0 6d 65 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 68 65  member of.** the
5c3d0 20 63 6f 6e 74 65 78 74 20 63 6f 6e 74 61 69 6e   context contain
5c3e0 69 6e 67 20 74 68 65 20 6d 61 74 63 68 20 69 73  ing the match is
5c3f0 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e 20 0a 2a   incremented. .*
5c400 2a 0a 2a 2a 20 45 61 63 68 20 73 75 62 71 75 65  *.** Each subque
5c410 72 79 20 67 65 74 73 20 61 20 6e 65 77 20 4e 61  ry gets a new Na
5c420 6d 65 43 6f 6e 74 65 78 74 2e 20 20 54 68 65 20  meContext.  The 
5c430 70 4e 65 78 74 20 66 69 65 6c 64 20 70 6f 69 6e  pNext field poin
5c440 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 4e 61 6d  ts to the.** Nam
5c450 65 43 6f 6e 74 65 78 74 20 69 6e 20 74 68 65 20  eContext in the 
5c460 70 61 72 65 6e 74 20 71 75 65 72 79 2e 20 20 54  parent query.  T
5c470 68 75 73 20 74 68 65 20 70 72 6f 63 65 73 73 20  hus the process 
5c480 6f 66 20 73 63 61 6e 6e 69 6e 67 20 74 68 65 0a  of scanning the.
5c490 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 6c  ** NameContext l
5c4a0 69 73 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20  ist corresponds 
5c4b0 74 6f 20 73 65 61 72 63 68 69 6e 67 20 74 68 72  to searching thr
5c4c0 6f 75 67 68 20 73 75 63 63 65 73 73 69 76 65 6c  ough successivel
5c4d0 79 20 6f 75 74 65 72 0a 2a 2a 20 73 75 62 71 75  y outer.** subqu
5c4e0 65 72 69 65 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f  eries looking fo
5c4f0 72 20 61 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73 74  r a match..*/.st
5c500 72 75 63 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74  ruct NameContext
5c510 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72   {.  Parse *pPar
5c520 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  se;       /* The
5c530 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 53 72 63   parser */.  Src
5c540 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 3b 20  List *pSrcList; 
5c550 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65    /* One or more
5c560 20 74 61 62 6c 65 73 20 75 73 65 64 20 74 6f 20   tables used to 
5c570 72 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 2a 2f  resolve names */
5c580 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
5c590 69 73 74 3b 20 20 20 20 2f 2a 20 4f 70 74 69 6f  ist;    /* Optio
5c5a0 6e 61 6c 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65  nal list of name
5c5b0 64 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f  d expressions */
5c5c0 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20  .  int nRef;    
5c5d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
5c5e0 72 20 6f 66 20 6e 61 6d 65 73 20 72 65 73 6f 6c  r of names resol
5c5f0 76 65 64 20 62 79 20 74 68 69 73 20 63 6f 6e 74  ved by this cont
5c600 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72  ext */.  int nEr
5c610 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  r;            /*
5c620 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72   Number of error
5c630 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 77 68  s encountered wh
5c640 69 6c 65 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61  ile resolving na
5c650 6d 65 73 20 2a 2f 0a 20 20 75 38 20 61 6c 6c 6f  mes */.  u8 allo
5c660 77 41 67 67 3b 20 20 20 20 20 20 20 20 20 2f 2a  wAgg;         /*
5c670 20 41 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   Aggregate funct
5c680 69 6f 6e 73 20 61 6c 6c 6f 77 65 64 20 68 65 72  ions allowed her
5c690 65 20 2a 2f 0a 20 20 75 38 20 68 61 73 41 67 67  e */.  u8 hasAgg
5c6a0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
5c6b0 72 75 65 20 69 66 20 61 67 67 72 65 67 61 74 65  rue if aggregate
5c6c0 73 20 61 72 65 20 73 65 65 6e 20 2a 2f 0a 20 20  s are seen */.  
5c6d0 75 38 20 69 73 43 68 65 63 6b 3b 20 20 20 20 20  u8 isCheck;     
5c6e0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
5c6f0 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20  resolving names 
5c700 69 6e 20 61 20 43 48 45 43 4b 20 63 6f 6e 73 74  in a CHECK const
5c710 72 61 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e  raint */.  int n
5c720 44 65 70 74 68 3b 20 20 20 20 20 20 20 20 20 20  Depth;          
5c730 2f 2a 20 44 65 70 74 68 20 6f 66 20 73 75 62 71  /* Depth of subq
5c740 75 65 72 79 20 72 65 63 75 72 73 69 6f 6e 2e 20  uery recursion. 
5c750 31 20 66 6f 72 20 6e 6f 20 72 65 63 75 72 73 69  1 for no recursi
5c760 6f 6e 20 2a 2f 0a 20 20 41 67 67 49 6e 66 6f 20  on */.  AggInfo 
5c770 2a 70 41 67 67 49 6e 66 6f 3b 20 20 20 2f 2a 20  *pAggInfo;   /* 
5c780 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  Information abou
5c790 74 20 61 67 67 72 65 67 61 74 65 73 20 61 74 20  t aggregates at 
5c7a0 74 68 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 20  this level */.  
5c7b0 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 65  NameContext *pNe
5c7c0 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 6f 75 74  xt;  /* Next out
5c7d0 65 72 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 2e  er name context.
5c7e0 20 20 4e 55 4c 4c 20 66 6f 72 20 6f 75 74 65 72    NULL for outer
5c7f0 6d 6f 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  most */.};../*.*
5c800 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
5c810 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
5c820 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
5c830 73 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  s all informatio
5c840 6e 0a 2a 2a 20 6e 65 65 64 65 64 20 74 6f 20 67  n.** needed to g
5c850 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
5c860 20 61 20 73 69 6e 67 6c 65 20 53 45 4c 45 43 54   a single SELECT
5c870 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a   statement..**.*
5c880 2a 20 6e 4c 69 6d 69 74 20 69 73 20 73 65 74 20  * nLimit is set 
5c890 74 6f 20 2d 31 20 69 66 20 74 68 65 72 65 20 69  to -1 if there i
5c8a0 73 20 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75 73  s no LIMIT claus
5c8b0 65 2e 20 20 6e 4f 66 66 73 65 74 20 69 73 20 73  e.  nOffset is s
5c8c0 65 74 20 74 6f 20 30 2e 0a 2a 2a 20 49 66 20 74  et to 0..** If t
5c8d0 68 65 72 65 20 69 73 20 61 20 4c 49 4d 49 54 20  here is a LIMIT 
5c8e0 63 6c 61 75 73 65 2c 20 74 68 65 20 70 61 72 73  clause, the pars
5c8f0 65 72 20 73 65 74 73 20 6e 4c 69 6d 69 74 20 74  er sets nLimit t
5c900 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  o the value of t
5c910 68 65 0a 2a 2a 20 6c 69 6d 69 74 20 61 6e 64 20  he.** limit and 
5c920 6e 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 76  nOffset to the v
5c930 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 66 66 73  alue of the offs
5c940 65 74 20 28 6f 72 20 30 20 69 66 20 74 68 65 72  et (or 0 if ther
5c950 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 6f 66 66 73  e is not.** offs
5c960 65 74 29 2e 20 20 42 75 74 20 6c 61 74 65 72 20  et).  But later 
5c970 6f 6e 2c 20 6e 4c 69 6d 69 74 20 61 6e 64 20 6e  on, nLimit and n
5c980 4f 66 66 73 65 74 20 62 65 63 6f 6d 65 20 74 68  Offset become th
5c990 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  e memory locatio
5c9a0 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 56 44 42  ns.** in the VDB
5c9b0 45 20 74 68 61 74 20 72 65 63 6f 72 64 20 74 68  E that record th
5c9c0 65 20 6c 69 6d 69 74 20 61 6e 64 20 6f 66 66 73  e limit and offs
5c9d0 65 74 20 63 6f 75 6e 74 65 72 73 2e 0a 2a 2a 0a  et counters..**.
5c9e0 2a 2a 20 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b  ** addrOpenEphm[
5c9f0 5d 20 65 6e 74 72 69 65 73 20 63 6f 6e 74 61 69  ] entries contai
5ca00 6e 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66  n the address of
5ca10 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
5ca20 6c 20 6f 70 63 6f 64 65 73 2e 0a 2a 2a 20 54 68  l opcodes..** Th
5ca30 65 73 65 20 61 64 64 72 65 73 73 65 73 20 6d 75  ese addresses mu
5ca40 73 74 20 62 65 20 73 74 6f 72 65 64 20 73 6f 20  st be stored so 
5ca50 74 68 61 74 20 77 65 20 63 61 6e 20 67 6f 20 62  that we can go b
5ca60 61 63 6b 20 61 6e 64 20 66 69 6c 6c 20 69 6e 0a  ack and fill in.
5ca70 2a 2a 20 74 68 65 20 50 34 5f 4b 45 59 49 4e 46  ** the P4_KEYINF
5ca80 4f 20 61 6e 64 20 50 32 20 70 61 72 61 6d 65 74  O and P2 paramet
5ca90 65 72 73 20 6c 61 74 65 72 2e 20 20 4e 65 69 74  ers later.  Neit
5caa0 68 65 72 20 74 68 65 20 4b 65 79 49 6e 66 6f 20  her the KeyInfo 
5cab0 6e 6f 72 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65  nor.** the numbe
5cac0 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
5cad0 50 32 20 63 61 6e 20 62 65 20 63 6f 6d 70 75 74  P2 can be comput
5cae0 65 64 20 61 74 20 74 68 65 20 73 61 6d 65 20 74  ed at the same t
5caf0 69 6d 65 0a 2a 2a 20 61 73 20 74 68 65 20 4f 50  ime.** as the OP
5cb00 5f 4f 70 65 6e 45 70 68 6d 20 69 6e 73 74 72 75  _OpenEphm instru
5cb10 63 74 69 6f 6e 20 69 73 20 63 6f 64 65 64 20 62  ction is coded b
5cb20 65 63 61 75 73 65 20 6e 6f 74 0a 2a 2a 20 65 6e  ecause not.** en
5cb30 6f 75 67 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ough information
5cb40 20 61 62 6f 75 74 20 74 68 65 20 63 6f 6d 70 6f   about the compo
5cb50 75 6e 64 20 71 75 65 72 79 20 69 73 20 6b 6e 6f  und query is kno
5cb60 77 6e 20 61 74 20 74 68 61 74 20 70 6f 69 6e 74  wn at that point
5cb70 2e 0a 2a 2a 20 54 68 65 20 4b 65 79 49 6e 66 6f  ..** The KeyInfo
5cb80 20 66 6f 72 20 61 64 64 72 4f 70 65 6e 54 72 61   for addrOpenTra
5cb90 6e 5b 30 5d 20 61 6e 64 20 5b 31 5d 20 63 6f 6e  n[0] and [1] con
5cba0 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 20  tains collating 
5cbb0 73 65 71 75 65 6e 63 65 73 0a 2a 2a 20 66 6f 72  sequences.** for
5cbc0 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e   the result set.
5cbd0 20 20 54 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f    The KeyInfo fo
5cbe0 72 20 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b 32  r addrOpenTran[2
5cbf0 5d 20 63 6f 6e 74 61 69 6e 73 20 63 6f 6c 6c 61  ] contains colla
5cc00 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65  ting.** sequence
5cc10 73 20 66 6f 72 20 74 68 65 20 4f 52 44 45 52 20  s for the ORDER 
5cc20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73 74  BY clause..*/.st
5cc30 72 75 63 74 20 53 65 6c 65 63 74 20 7b 0a 20 20  ruct Select {.  
5cc40 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
5cc50 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69  ;      /* The fi
5cc60 65 6c 64 73 20 6f 66 20 74 68 65 20 72 65 73 75  elds of the resu
5cc70 6c 74 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20  lt */.  u8 op;  
5cc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5cc90 2a 20 4f 6e 65 20 6f 66 3a 20 54 4b 5f 55 4e 49  * One of: TK_UNI
5cca0 4f 4e 20 54 4b 5f 41 4c 4c 20 54 4b 5f 49 4e 54  ON TK_ALL TK_INT
5ccb0 45 52 53 45 43 54 20 54 4b 5f 45 58 43 45 50 54  ERSECT TK_EXCEPT
5ccc0 20 2a 2f 0a 20 20 75 38 20 69 73 44 69 73 74 69   */.  u8 isDisti
5ccd0 6e 63 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  nct;         /* 
5cce0 54 72 75 65 20 69 66 20 74 68 65 20 44 49 53 54  True if the DIST
5ccf0 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 69 73 20  INCT keyword is 
5cd00 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 75 38 20  present */.  u8 
5cd10 69 73 52 65 73 6f 6c 76 65 64 3b 20 20 20 20 20  isResolved;     
5cd20 20 20 20 20 2f 2a 20 54 72 75 65 20 6f 6e 63 65      /* True once
5cd30 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 52 65   sqlite3SelectRe
5cd40 73 6f 6c 76 65 28 29 20 68 61 73 20 72 75 6e 2e  solve() has run.
5cd50 20 2a 2f 0a 20 20 75 38 20 69 73 41 67 67 3b 20   */.  u8 isAgg; 
5cd60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5cd70 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20  True if this is 
5cd80 61 6e 20 61 67 67 72 65 67 61 74 65 20 71 75 65  an aggregate que
5cd90 72 79 20 2a 2f 0a 20 20 75 38 20 75 73 65 73 45  ry */.  u8 usesE
5cda0 70 68 6d 3b 20 20 20 20 20 20 20 20 20 20 20 2f  phm;           /
5cdb0 2a 20 54 72 75 65 20 69 66 20 75 73 65 73 20 61  * True if uses a
5cdc0 6e 20 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20  n OpenEphemeral 
5cdd0 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 75 38 20 64  opcode */.  u8 d
5cde0 69 73 61 6c 6c 6f 77 4f 72 64 65 72 42 79 3b 20  isallowOrderBy; 
5cdf0 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c     /* Do not all
5ce00 6f 77 20 61 6e 20 4f 52 44 45 52 20 42 59 20 74  ow an ORDER BY t
5ce10 6f 20 62 65 20 61 74 74 61 63 68 65 64 20 69 66  o be attached if
5ce20 20 54 52 55 45 20 2a 2f 0a 20 20 63 68 61 72 20   TRUE */.  char 
5ce30 61 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20  affinity;       
5ce40 20 20 2f 2a 20 4d 61 6b 65 52 65 63 6f 72 64 20    /* MakeRecord 
5ce50 77 69 74 68 20 74 68 69 73 20 61 66 66 69 6e 69  with this affini
5ce60 74 79 20 66 6f 72 20 53 52 54 5f 53 65 74 20 2a  ty for SRT_Set *
5ce70 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  /.  SrcList *pSr
5ce80 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  c;         /* Th
5ce90 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f  e FROM clause */
5cea0 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b  .  Expr *pWhere;
5ceb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
5cec0 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
5ced0 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 47 72  .  ExprList *pGr
5cee0 6f 75 70 42 79 3b 20 20 20 20 2f 2a 20 54 68 65  oupBy;    /* The
5cef0 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65   GROUP BY clause
5cf00 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 48 61 76   */.  Expr *pHav
5cf10 69 6e 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ing;         /* 
5cf20 54 68 65 20 48 41 56 49 4e 47 20 63 6c 61 75 73  The HAVING claus
5cf30 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  e */.  ExprList 
5cf40 2a 70 4f 72 64 65 72 42 79 3b 20 20 20 20 2f 2a  *pOrderBy;    /*
5cf50 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   The ORDER BY cl
5cf60 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74  ause */.  Select
5cf70 20 2a 70 50 72 69 6f 72 3b 20 20 20 20 20 20 20   *pPrior;       
5cf80 20 2f 2a 20 50 72 69 6f 72 20 73 65 6c 65 63 74   /* Prior select
5cf90 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73   in a compound s
5cfa0 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20  elect statement 
5cfb0 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e 65  */.  Select *pNe
5cfc0 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  xt;         /* N
5cfd0 65 78 74 20 73 65 6c 65 63 74 20 74 6f 20 74 68  ext select to th
5cfe0 65 20 6c 65 66 74 20 69 6e 20 61 20 63 6f 6d 70  e left in a comp
5cff0 6f 75 6e 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74  ound */.  Select
5d000 20 2a 70 52 69 67 68 74 6d 6f 73 74 3b 20 20 20   *pRightmost;   
5d010 20 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 74 20 73   /* Right-most s
5d020 65 6c 65 63 74 20 69 6e 20 61 20 63 6f 6d 70 6f  elect in a compo
5d030 75 6e 64 20 73 65 6c 65 63 74 20 73 74 61 74 65  und select state
5d040 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a  ment */.  Expr *
5d050 70 4c 69 6d 69 74 3b 20 20 20 20 20 20 20 20 20  pLimit;         
5d060 20 2f 2a 20 4c 49 4d 49 54 20 65 78 70 72 65 73   /* LIMIT expres
5d070 73 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73  sion. NULL means
5d080 20 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0a 20 20   not used. */.  
5d090 45 78 70 72 20 2a 70 4f 66 66 73 65 74 3b 20 20  Expr *pOffset;  
5d0a0 20 20 20 20 20 20 20 2f 2a 20 4f 46 46 53 45 54         /* OFFSET
5d0b0 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e 55 4c   expression. NUL
5d0c0 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73 65 64  L means not used
5d0d0 2e 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 69 6d 69  . */.  int iLimi
5d0e0 74 2c 20 69 4f 66 66 73 65 74 3b 20 20 20 2f 2a  t, iOffset;   /*
5d0f0 20 4d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72   Memory register
5d100 73 20 68 6f 6c 64 69 6e 67 20 4c 49 4d 49 54 20  s holding LIMIT 
5d110 26 20 4f 46 46 53 45 54 20 63 6f 75 6e 74 65 72  & OFFSET counter
5d120 73 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4f  s */.  int addrO
5d130 70 65 6e 45 70 68 6d 5b 33 5d 3b 20 20 20 2f 2a  penEphm[3];   /*
5d140 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 20 6f 70   OP_OpenEphem op
5d150 63 6f 64 65 73 20 72 65 6c 61 74 65 64 20 74 6f  codes related to
5d160 20 74 68 69 73 20 73 65 6c 65 63 74 20 2a 2f 0a   this select */.
5d170 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65  };../*.** The re
5d180 73 75 6c 74 73 20 6f 66 20 61 20 73 65 6c 65 63  sults of a selec
5d190 74 20 63 61 6e 20 62 65 20 64 69 73 74 72 69 62  t can be distrib
5d1a0 75 74 65 64 20 69 6e 20 73 65 76 65 72 61 6c 20  uted in several 
5d1b0 77 61 79 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ways..*/.#define
5d1c0 20 53 52 54 5f 55 6e 69 6f 6e 20 20 20 20 20 20   SRT_Union      
5d1d0 20 20 31 20 20 2f 2a 20 53 74 6f 72 65 20 72 65    1  /* Store re
5d1e0 73 75 6c 74 20 61 73 20 6b 65 79 73 20 69 6e 20  sult as keys in 
5d1f0 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66  an index */.#def
5d200 69 6e 65 20 53 52 54 5f 45 78 63 65 70 74 20 20  ine SRT_Except  
5d210 20 20 20 20 20 32 20 20 2f 2a 20 52 65 6d 6f 76       2  /* Remov
5d220 65 20 72 65 73 75 6c 74 20 66 72 6f 6d 20 61 20  e result from a 
5d230 55 4e 49 4f 4e 20 69 6e 64 65 78 20 2a 2f 0a 23  UNION index */.#
5d240 64 65 66 69 6e 65 20 53 52 54 5f 45 78 69 73 74  define SRT_Exist
5d250 73 20 20 20 20 20 20 20 33 20 20 2f 2a 20 53 74  s       3  /* St
5d260 6f 72 65 20 31 20 69 66 20 74 68 65 20 72 65 73  ore 1 if the res
5d270 75 6c 74 20 69 73 20 6e 6f 74 20 65 6d 70 74 79  ult is not empty
5d280 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f   */.#define SRT_
5d290 44 69 73 63 61 72 64 20 20 20 20 20 20 34 20 20  Discard      4  
5d2a0 2f 2a 20 44 6f 20 6e 6f 74 20 73 61 76 65 20 74  /* Do not save t
5d2b0 68 65 20 72 65 73 75 6c 74 73 20 61 6e 79 77 68  he results anywh
5d2c0 65 72 65 20 2a 2f 0a 0a 2f 2a 20 54 68 65 20 4f  ere */../* The O
5d2d0 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69  RDER BY clause i
5d2e0 73 20 69 67 6e 6f 72 65 64 20 66 6f 72 20 61 6c  s ignored for al
5d2f0 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 2a  l of the above *
5d300 2f 0a 23 64 65 66 69 6e 65 20 49 67 6e 6f 72 61  /.#define Ignora
5d310 62 6c 65 4f 72 64 65 72 62 79 28 58 29 20 28 28  bleOrderby(X) ((
5d320 58 2d 3e 65 44 65 73 74 29 3c 3d 53 52 54 5f 44  X->eDest)<=SRT_D
5d330 69 73 63 61 72 64 29 0a 0a 23 64 65 66 69 6e 65  iscard)..#define
5d340 20 53 52 54 5f 43 61 6c 6c 62 61 63 6b 20 20 20   SRT_Callback   
5d350 20 20 35 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 61    5  /* Invoke a
5d360 20 63 61 6c 6c 62 61 63 6b 20 77 69 74 68 20 65   callback with e
5d370 61 63 68 20 72 6f 77 20 6f 66 20 72 65 73 75 6c  ach row of resul
5d380 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54  t */.#define SRT
5d390 5f 4d 65 6d 20 20 20 20 20 20 20 20 20 20 36 20  _Mem          6 
5d3a0 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74   /* Store result
5d3b0 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c   in a memory cel
5d3c0 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54  l */.#define SRT
5d3d0 5f 53 65 74 20 20 20 20 20 20 20 20 20 20 37 20  _Set          7 
5d3e0 20 2f 2a 20 53 74 6f 72 65 20 6e 6f 6e 2d 6e 75   /* Store non-nu
5d3f0 6c 6c 20 72 65 73 75 6c 74 73 20 61 73 20 6b 65  ll results as ke
5d400 79 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 20 2a  ys in an index *
5d410 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 54 61  /.#define SRT_Ta
5d420 62 6c 65 20 20 20 20 20 20 20 20 38 20 20 2f 2a  ble        8  /*
5d430 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 61 73   Store result as
5d440 20 64 61 74 61 20 77 69 74 68 20 61 6e 20 61 75   data with an au
5d450 74 6f 6d 61 74 69 63 20 72 6f 77 69 64 20 2a 2f  tomatic rowid */
5d460 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 45 70 68  .#define SRT_Eph
5d470 65 6d 54 61 62 20 20 20 20 20 39 20 20 2f 2a 20  emTab     9  /* 
5d480 43 72 65 61 74 65 20 74 72 61 6e 73 69 65 6e 74  Create transient
5d490 20 74 61 62 20 61 6e 64 20 73 74 6f 72 65 20 6c   tab and store l
5d4a0 69 6b 65 20 53 52 54 5f 54 61 62 6c 65 20 2a 2f  ike SRT_Table */
5d4b0 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 53 75 62  .#define SRT_Sub
5d4c0 72 6f 75 74 69 6e 65 20 20 31 30 20 20 2f 2a 20  routine  10  /* 
5d4d0 43 61 6c 6c 20 61 20 73 75 62 72 6f 75 74 69 6e  Call a subroutin
5d4e0 65 20 74 6f 20 68 61 6e 64 6c 65 20 72 65 73 75  e to handle resu
5d4f0 6c 74 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20  lts */../*.** A 
5d500 73 74 72 75 63 74 75 72 65 20 75 73 65 64 20 74  structure used t
5d510 6f 20 63 75 73 74 6f 6d 69 7a 65 20 74 68 65 20  o customize the 
5d520 62 65 68 61 76 69 6f 75 72 20 6f 66 20 73 71 6c  behaviour of sql
5d530 69 74 65 33 53 65 6c 65 63 74 28 29 2e 20 53 65  ite3Select(). Se
5d540 65 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20 61 62  e.** comments ab
5d550 6f 76 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ove sqlite3Selec
5d560 74 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73 2e  t() for details.
5d570 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
5d580 63 74 20 53 65 6c 65 63 74 44 65 73 74 20 53 65  ct SelectDest Se
5d590 6c 65 63 74 44 65 73 74 3b 0a 73 74 72 75 63 74  lectDest;.struct
5d5a0 20 53 65 6c 65 63 74 44 65 73 74 20 7b 0a 20 20   SelectDest {.  
5d5b0 75 38 20 65 44 65 73 74 3b 20 20 20 20 20 20 20  u8 eDest;       
5d5c0 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69 73 70    /* How to disp
5d5d0 6f 73 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c  ose of the resul
5d5e0 74 73 20 2a 2f 0a 20 20 75 38 20 61 66 66 69 6e  ts */.  u8 affin
5d5f0 69 74 79 3b 20 20 20 20 20 20 2f 2a 20 41 66 66  ity;      /* Aff
5d600 69 6e 69 74 79 20 75 73 65 64 20 77 68 65 6e 20  inity used when 
5d610 65 44 65 73 74 3d 3d 53 52 54 5f 53 65 74 20 2a  eDest==SRT_Set *
5d620 2f 0a 20 20 69 6e 74 20 69 50 61 72 6d 3b 20 20  /.  int iParm;  
5d630 20 20 20 20 20 20 2f 2a 20 41 20 70 61 72 61 6d        /* A param
5d640 65 74 65 72 20 75 73 65 64 20 62 79 20 74 68 65  eter used by the
5d650 20 65 44 65 73 74 20 64 69 73 70 6f 73 61 6c 20   eDest disposal 
5d660 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 69 6e 74 20  method */.  int 
5d670 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a  iMem;         /*
5d680 20 42 61 73 65 20 72 65 67 69 73 74 65 72 20 77   Base register w
5d690 68 65 72 65 20 72 65 73 75 6c 74 73 20 61 72 65  here results are
5d6a0 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e   written */.  in
5d6b0 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20  t nMem;         
5d6c0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 67  /* Number of reg
5d6d0 69 73 74 65 72 73 20 61 6c 6c 6f 63 61 74 65 64  isters allocated
5d6e0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   */.};../*.** An
5d6f0 20 53 51 4c 20 70 61 72 73 65 72 20 63 6f 6e 74   SQL parser cont
5d700 65 78 74 2e 20 20 41 20 63 6f 70 79 20 6f 66 20  ext.  A copy of 
5d710 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69  this structure i
5d720 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68  s passed through
5d730 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 72 20 61  .** the parser a
5d740 6e 64 20 64 6f 77 6e 20 69 6e 74 6f 20 61 6c 6c  nd down into all
5d750 20 74 68 65 20 70 61 72 73 65 72 20 61 63 74 69   the parser acti
5d760 6f 6e 20 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72  on routine in or
5d770 64 65 72 20 74 6f 0a 2a 2a 20 63 61 72 72 79 20  der to.** carry 
5d780 61 72 6f 75 6e 64 20 69 6e 66 6f 72 6d 61 74 69  around informati
5d790 6f 6e 20 74 68 61 74 20 69 73 20 67 6c 6f 62 61  on that is globa
5d7a0 6c 20 74 6f 20 74 68 65 20 65 6e 74 69 72 65 20  l to the entire 
5d7b0 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  parse..**.** The
5d7c0 20 73 74 72 75 63 74 75 72 65 20 69 73 20 64 69   structure is di
5d7d0 76 69 64 65 64 20 69 6e 74 6f 20 74 77 6f 20 70  vided into two p
5d7e0 61 72 74 73 2e 20 20 57 68 65 6e 20 74 68 65 20  arts.  When the 
5d7f0 70 61 72 73 65 72 20 61 6e 64 20 63 6f 64 65 0a  parser and code.
5d800 2a 2a 20 67 65 6e 65 72 61 74 65 20 63 61 6c 6c  ** generate call
5d810 20 74 68 65 6d 73 65 6c 76 65 73 20 72 65 63 75   themselves recu
5d820 72 73 69 76 65 6c 79 2c 20 74 68 65 20 66 69 72  rsively, the fir
5d830 73 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 73  st part of the s
5d840 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 63  tructure.** is c
5d850 6f 6e 73 74 61 6e 74 20 62 75 74 20 74 68 65 20  onstant but the 
5d860 73 65 63 6f 6e 64 20 70 61 72 74 20 69 73 20 72  second part is r
5d870 65 73 65 74 20 61 74 20 74 68 65 20 62 65 67 69  eset at the begi
5d880 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 20 6f 66  nning and end of
5d890 0a 2a 2a 20 65 61 63 68 20 72 65 63 75 72 73 69  .** each recursi
5d8a0 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 54  on..**.** The nT
5d8b0 61 62 6c 65 4c 6f 63 6b 20 61 6e 64 20 61 54 61  ableLock and aTa
5d8c0 62 6c 65 4c 6f 63 6b 20 76 61 72 69 61 62 6c 65  bleLock variable
5d8d0 73 20 61 72 65 20 6f 6e 6c 79 20 75 73 65 64 20  s are only used 
5d8e0 69 66 20 74 68 65 20 73 68 61 72 65 64 2d 63 61  if the shared-ca
5d8f0 63 68 65 20 0a 2a 2a 20 66 65 61 74 75 72 65 20  che .** feature 
5d900 69 73 20 65 6e 61 62 6c 65 64 20 28 69 66 20 73  is enabled (if s
5d910 71 6c 69 74 65 33 54 73 64 28 29 2d 3e 75 73 65  qlite3Tsd()->use
5d920 53 68 61 72 65 64 44 61 74 61 20 69 73 20 74 72  SharedData is tr
5d930 75 65 29 2e 20 54 68 65 79 20 61 72 65 0a 2a 2a  ue). They are.**
5d940 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74   used to store t
5d950 68 65 20 73 65 74 20 6f 66 20 74 61 62 6c 65 2d  he set of table-
5d960 6c 6f 63 6b 73 20 72 65 71 75 69 72 65 64 20 62  locks required b
5d970 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  y the statement 
5d980 62 65 69 6e 67 0a 2a 2a 20 63 6f 6d 70 69 6c 65  being.** compile
5d990 64 2e 20 46 75 6e 63 74 69 6f 6e 20 73 71 6c 69  d. Function sqli
5d9a0 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29 20 69  te3TableLock() i
5d9b0 73 20 75 73 65 64 20 74 6f 20 61 64 64 20 65 6e  s used to add en
5d9c0 74 72 69 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20  tries to the.** 
5d9d0 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  list..*/.struct 
5d9e0 50 61 72 73 65 20 7b 0a 20 20 73 71 6c 69 74 65  Parse {.  sqlite
5d9f0 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 2f  3 *db;         /
5da00 2a 20 54 68 65 20 6d 61 69 6e 20 64 61 74 61 62  * The main datab
5da10 61 73 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ase structure */
5da20 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
5da30 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
5da40 6e 20 63 6f 64 65 20 66 72 6f 6d 20 65 78 65 63  n code from exec
5da50 75 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72 20  ution */.  char 
5da60 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20  *zErrMsg;       
5da70 2f 2a 20 41 6e 20 65 72 72 6f 72 20 6d 65 73 73  /* An error mess
5da80 61 67 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a 70  age */.  Vdbe *p
5da90 56 64 62 65 3b 20 20 20 20 20 20 20 20 20 2f 2a  Vdbe;         /*
5daa0 20 41 6e 20 65 6e 67 69 6e 65 20 66 6f 72 20 65   An engine for e
5dab0 78 65 63 75 74 69 6e 67 20 64 61 74 61 62 61 73  xecuting databas
5dac0 65 20 62 79 74 65 63 6f 64 65 20 2a 2f 0a 20 20  e bytecode */.  
5dad0 75 38 20 63 6f 6c 4e 61 6d 65 73 53 65 74 3b 20  u8 colNamesSet; 
5dae0 20 20 20 20 20 2f 2a 20 54 52 55 45 20 61 66 74       /* TRUE aft
5daf0 65 72 20 4f 50 5f 43 6f 6c 75 6d 6e 4e 61 6d 65  er OP_ColumnName
5db00 20 68 61 73 20 62 65 65 6e 20 69 73 73 75 65 64   has been issued
5db10 20 74 6f 20 70 56 64 62 65 20 2a 2f 0a 20 20 75   to pVdbe */.  u
5db20 38 20 6e 61 6d 65 43 6c 61 73 68 3b 20 20 20 20  8 nameClash;    
5db30 20 20 20 20 2f 2a 20 41 20 70 65 72 6d 61 6e 65      /* A permane
5db40 6e 74 20 74 61 62 6c 65 20 6e 61 6d 65 20 63 6c  nt table name cl
5db50 61 73 68 65 73 20 77 69 74 68 20 74 65 6d 70 20  ashes with temp 
5db60 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  table name */.  
5db70 75 38 20 63 68 65 63 6b 53 63 68 65 6d 61 3b 20  u8 checkSchema; 
5db80 20 20 20 20 20 2f 2a 20 43 61 75 73 65 73 20 73       /* Causes s
5db90 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63 68 65  chema cookie che
5dba0 63 6b 20 61 66 74 65 72 20 61 6e 20 65 72 72 6f  ck after an erro
5dbb0 72 20 2a 2f 0a 20 20 75 38 20 6e 65 73 74 65 64  r */.  u8 nested
5dbc0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
5dbd0 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20  umber of nested 
5dbe0 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 70 61 72  calls to the par
5dbf0 73 65 72 2f 63 6f 64 65 20 67 65 6e 65 72 61 74  ser/code generat
5dc00 6f 72 20 2a 2f 0a 20 20 75 38 20 70 61 72 73 65  or */.  u8 parse
5dc10 45 72 72 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20  Error;       /* 
5dc20 54 72 75 65 20 61 66 74 65 72 20 61 20 70 61 72  True after a par
5dc30 73 69 6e 67 20 65 72 72 6f 72 2e 20 20 54 69 63  sing error.  Tic
5dc40 6b 65 74 20 23 31 37 39 34 20 2a 2f 0a 20 20 75  ket #1794 */.  u
5dc50 38 20 6e 54 65 6d 70 52 65 67 3b 20 20 20 20 20  8 nTempReg;     
5dc60 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
5dc70 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73   temporary regis
5dc80 74 65 72 73 20 69 6e 20 61 54 65 6d 70 52 65 67  ters in aTempReg
5dc90 5b 5d 20 2a 2f 0a 20 20 75 38 20 6e 54 65 6d 70  [] */.  u8 nTemp
5dca0 49 6e 55 73 65 3b 20 20 20 20 20 20 20 2f 2a 20  InUse;       /* 
5dcb0 4e 75 6d 62 65 72 20 6f 66 20 61 54 65 6d 70 52  Number of aTempR
5dcc0 65 67 5b 5d 20 63 75 72 72 65 6e 74 6c 79 20 63  eg[] currently c
5dcd0 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a 20 20  hecked out */.  
5dce0 69 6e 74 20 61 54 65 6d 70 52 65 67 5b 38 5d 3b  int aTempReg[8];
5dcf0 20 20 20 20 20 2f 2a 20 48 6f 6c 64 69 6e 67 20       /* Holding 
5dd00 61 72 65 61 20 66 6f 72 20 74 65 6d 70 6f 72 61  area for tempora
5dd10 72 79 20 72 65 67 69 73 74 65 72 73 20 2a 2f 0a  ry registers */.
5dd20 20 20 69 6e 74 20 6e 52 61 6e 67 65 52 65 67 3b    int nRangeReg;
5dd30 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
5dd40 66 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20  f the temporary 
5dd50 72 65 67 69 73 74 65 72 20 62 6c 6f 63 6b 20 2a  register block *
5dd60 2f 0a 20 20 69 6e 74 20 69 52 61 6e 67 65 52 65  /.  int iRangeRe
5dd70 67 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73  g;       /* Firs
5dd80 74 20 72 65 67 69 73 74 65 72 20 69 6e 20 74 65  t register in te
5dd90 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72  mporary register
5dda0 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20   block */.  int 
5ddb0 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20 20 20  nErr;           
5ddc0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 72   /* Number of er
5ddd0 72 6f 72 73 20 73 65 65 6e 20 2a 2f 0a 20 20 69  rors seen */.  i
5dde0 6e 74 20 6e 54 61 62 3b 20 20 20 20 20 20 20 20  nt nTab;        
5ddf0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
5de00 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f   previously allo
5de10 63 61 74 65 64 20 56 44 42 45 20 63 75 72 73 6f  cated VDBE curso
5de20 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d  rs */.  int nMem
5de30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
5de40 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79  Number of memory
5de50 20 63 65 6c 6c 73 20 75 73 65 64 20 73 6f 20 66   cells used so f
5de60 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 65 74  ar */.  int nSet
5de70 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
5de80 4e 75 6d 62 65 72 20 6f 66 20 73 65 74 73 20 75  Number of sets u
5de90 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20  sed so far */.  
5dea0 69 6e 74 20 63 6b 42 61 73 65 3b 20 20 20 20 20  int ckBase;     
5deb0 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72 65 67       /* Base reg
5dec0 69 73 74 65 72 20 6f 66 20 64 61 74 61 20 64 75  ister of data du
5ded0 72 69 6e 67 20 63 68 65 63 6b 20 63 6f 6e 73 74  ring check const
5dee0 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20  raints */.  int 
5def0 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3b  disableColCache;
5df00 20 2f 2a 20 54 72 75 65 20 74 6f 20 64 69 73 61   /* True to disa
5df10 62 6c 65 20 61 64 64 69 6e 67 20 74 6f 20 63 6f  ble adding to co
5df20 6c 75 6d 6e 20 63 61 63 68 65 20 2a 2f 0a 20 20  lumn cache */.  
5df30 69 6e 74 20 6e 43 6f 6c 43 61 63 68 65 3b 20 20  int nColCache;  
5df40 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
5df50 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65  f entries in the
5df60 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 2a 2f   column cache */
5df70 0a 20 20 69 6e 74 20 69 43 6f 6c 43 61 63 68 65  .  int iColCache
5df80 3b 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20  ;       /* Next 
5df90 65 6e 74 72 79 20 6f 66 20 74 68 65 20 63 61 63  entry of the cac
5dfa0 68 65 20 74 6f 20 72 65 70 6c 61 63 65 20 2a 2f  he to replace */
5dfb0 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61  .  struct yColCa
5dfc0 63 68 65 20 7b 0a 20 20 20 20 69 6e 74 20 69 54  che {.    int iT
5dfd0 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20  able;           
5dfe0 2f 2a 20 54 61 62 6c 65 20 63 75 72 73 6f 72 20  /* Table cursor 
5dff0 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 69 6e  number */.    in
5e000 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20  t iColumn;      
5e010 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 6f 6c      /* Table col
5e020 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  umn number */.  
5e030 20 20 63 68 61 72 20 61 66 66 43 68 61 6e 67 65    char affChange
5e040 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  ;       /* True 
5e050 69 66 20 74 68 69 73 20 72 65 67 69 73 74 65 72  if this register
5e060 20 68 61 73 20 68 61 64 20 61 6e 20 61 66 66 69   has had an affi
5e070 6e 69 74 79 20 63 68 61 6e 67 65 20 2a 2f 0a 20  nity change */. 
5e080 20 20 20 69 6e 74 20 69 52 65 67 3b 20 20 20 20     int iReg;    
5e090 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69           /* Regi
5e0a0 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 76 61 6c  ster holding val
5e0b0 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d  ue of this colum
5e0c0 6e 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c 43 61 63  n */.  } aColCac
5e0d0 68 65 5b 31 30 5d 3b 20 20 20 20 20 2f 2a 20 4f  he[10];     /* O
5e0e0 6e 65 20 66 6f 72 20 65 61 63 68 20 76 61 6c 69  ne for each vali
5e0f0 64 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 65  d column cache e
5e100 6e 74 72 79 20 2a 2f 0a 20 20 75 33 32 20 77 72  ntry */.  u32 wr
5e110 69 74 65 4d 61 73 6b 3b 20 20 20 20 20 20 20 2f  iteMask;       /
5e120 2a 20 53 74 61 72 74 20 61 20 77 72 69 74 65 20  * Start a write 
5e130 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74  transaction on t
5e140 68 65 73 65 20 64 61 74 61 62 61 73 65 73 20 2a  hese databases *
5e150 2f 0a 20 20 75 33 32 20 63 6f 6f 6b 69 65 4d 61  /.  u32 cookieMa
5e160 73 6b 3b 20 20 20 20 20 20 2f 2a 20 42 69 74 6d  sk;      /* Bitm
5e170 61 73 6b 20 6f 66 20 73 63 68 65 6d 61 20 76 65  ask of schema ve
5e180 72 69 66 69 65 64 20 64 61 74 61 62 61 73 65 73  rified databases
5e190 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b 69 65   */.  int cookie
5e1a0 47 6f 74 6f 3b 20 20 20 20 20 20 2f 2a 20 41 64  Goto;      /* Ad
5e1b0 64 72 65 73 73 20 6f 66 20 4f 50 5f 47 6f 74 6f  dress of OP_Goto
5e1c0 20 74 6f 20 63 6f 6f 6b 69 65 20 76 65 72 69 66   to cookie verif
5e1d0 69 65 72 20 73 75 62 72 6f 75 74 69 6e 65 20 2a  ier subroutine *
5e1e0 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b 69 65 56 61  /.  int cookieVa
5e1f0 6c 75 65 5b 53 51 4c 49 54 45 5f 4d 41 58 5f 41  lue[SQLITE_MAX_A
5e200 54 54 41 43 48 45 44 2b 32 5d 3b 20 20 2f 2a 20  TTACHED+2];  /* 
5e210 56 61 6c 75 65 73 20 6f 66 20 63 6f 6f 6b 69 65  Values of cookie
5e220 73 20 74 6f 20 76 65 72 69 66 79 20 2a 2f 0a 23  s to verify */.#
5e230 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
5e240 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
5e250 20 20 69 6e 74 20 6e 54 61 62 6c 65 4c 6f 63 6b    int nTableLock
5e260 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ;        /* Numb
5e270 65 72 20 6f 66 20 6c 6f 63 6b 73 20 69 6e 20 61  er of locks in a
5e280 54 61 62 6c 65 4c 6f 63 6b 20 2a 2f 0a 20 20 54  TableLock */.  T
5e290 61 62 6c 65 4c 6f 63 6b 20 2a 61 54 61 62 6c 65  ableLock *aTable
5e2a0 4c 6f 63 6b 3b 20 2f 2a 20 52 65 71 75 69 72 65  Lock; /* Require
5e2b0 64 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20 66 6f  d table locks fo
5e2c0 72 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d  r shared-cache m
5e2d0 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20  ode */.#endif.  
5e2e0 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 20 20 20  int regRowid;   
5e2f0 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
5e300 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69 64 20 6f   holding rowid o
5e310 66 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 65  f CREATE TABLE e
5e320 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20 72 65  ntry */.  int re
5e330 67 52 6f 6f 74 3b 20 20 20 20 20 20 20 20 20 2f  gRoot;         /
5e340 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69  * Register holdi
5e350 6e 67 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d  ng root page num
5e360 62 65 72 20 66 6f 72 20 6e 65 77 20 6f 62 6a 65  ber for new obje
5e370 63 74 73 20 2a 2f 0a 0a 20 20 2f 2a 20 41 62 6f  cts */..  /* Abo
5e380 76 65 20 69 73 20 63 6f 6e 73 74 61 6e 74 20 62  ve is constant b
5e390 65 74 77 65 65 6e 20 72 65 63 75 72 73 69 6f 6e  etween recursion
5e3a0 73 2e 20 20 42 65 6c 6f 77 20 69 73 20 72 65 73  s.  Below is res
5e3b0 65 74 20 62 65 66 6f 72 65 20 61 6e 64 20 61 66  et before and af
5e3c0 74 65 72 0a 20 20 2a 2a 20 65 61 63 68 20 72 65  ter.  ** each re
5e3d0 63 75 72 73 69 6f 6e 20 2a 2f 0a 0a 20 20 69 6e  cursion */..  in
5e3e0 74 20 6e 56 61 72 3b 20 20 20 20 20 20 20 20 20  t nVar;         
5e3f0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
5e400 27 3f 27 20 76 61 72 69 61 62 6c 65 73 20 73 65  '?' variables se
5e410 65 6e 20 69 6e 20 74 68 65 20 53 51 4c 20 73 6f  en in the SQL so
5e420 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 56   far */.  int nV
5e430 61 72 45 78 70 72 3b 20 20 20 20 20 20 20 20 2f  arExpr;        /
5e440 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 65 64  * Number of used
5e450 20 73 6c 6f 74 73 20 69 6e 20 61 70 56 61 72 45   slots in apVarE
5e460 78 70 72 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e  xpr[] */.  int n
5e470 56 61 72 45 78 70 72 41 6c 6c 6f 63 3b 20 20 20  VarExprAlloc;   
5e480 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 6c 6c  /* Number of all
5e490 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 69 6e 20  ocated slots in 
5e4a0 61 70 56 61 72 45 78 70 72 5b 5d 20 2a 2f 0a 20  apVarExpr[] */. 
5e4b0 20 45 78 70 72 20 2a 2a 61 70 56 61 72 45 78 70   Expr **apVarExp
5e4c0 72 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72  r;    /* Pointer
5e4d0 73 20 74 6f 20 3a 61 61 61 20 61 6e 64 20 24 61  s to :aaa and $a
5e4e0 61 61 61 20 77 69 6c 64 63 61 72 64 20 65 78 70  aaa wildcard exp
5e4f0 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 75 38  ressions */.  u8
5e500 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 20 20 20   explain;       
5e510 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
5e520 65 20 45 58 50 4c 41 49 4e 20 66 6c 61 67 20 69  e EXPLAIN flag i
5e530 73 20 66 6f 75 6e 64 20 6f 6e 20 74 68 65 20 71  s found on the q
5e540 75 65 72 79 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  uery */.  Token 
5e550 73 45 72 72 54 6f 6b 65 6e 3b 20 20 20 20 20 2f  sErrToken;     /
5e560 2a 20 54 68 65 20 74 6f 6b 65 6e 20 61 74 20 77  * The token at w
5e570 68 69 63 68 20 74 68 65 20 65 72 72 6f 72 20 6f  hich the error o
5e580 63 63 75 72 72 65 64 20 2a 2f 0a 20 20 54 6f 6b  ccurred */.  Tok
5e590 65 6e 20 73 4e 61 6d 65 54 6f 6b 65 6e 3b 20 20  en sNameToken;  
5e5a0 20 20 2f 2a 20 54 6f 6b 65 6e 20 77 69 74 68 20    /* Token with 
5e5b0 75 6e 71 75 61 6c 69 66 69 65 64 20 73 63 68 65  unqualified sche
5e5c0 6d 61 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a  ma object name *
5e5d0 2f 0a 20 20 54 6f 6b 65 6e 20 73 4c 61 73 74 54  /.  Token sLastT
5e5e0 6f 6b 65 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20  oken;    /* The 
5e5f0 6c 61 73 74 20 74 6f 6b 65 6e 20 70 61 72 73 65  last token parse
5e600 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  d */.  const cha
5e610 72 20 2a 7a 53 71 6c 3b 20 20 20 20 2f 2a 20 41  r *zSql;    /* A
5e620 6c 6c 20 53 51 4c 20 74 65 78 74 20 2a 2f 0a 20  ll SQL text */. 
5e630 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61   const char *zTa
5e640 69 6c 3b 20 20 20 2f 2a 20 41 6c 6c 20 53 51 4c  il;   /* All SQL
5e650 20 74 65 78 74 20 70 61 73 74 20 74 68 65 20 6c   text past the l
5e660 61 73 74 20 73 65 6d 69 63 6f 6c 6f 6e 20 70 61  ast semicolon pa
5e670 72 73 65 64 20 2a 2f 0a 20 20 54 61 62 6c 65 20  rsed */.  Table 
5e680 2a 70 4e 65 77 54 61 62 6c 65 3b 20 20 20 20 2f  *pNewTable;    /
5e690 2a 20 41 20 74 61 62 6c 65 20 62 65 69 6e 67 20  * A table being 
5e6a0 63 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 43  constructed by C
5e6b0 52 45 41 54 45 20 54 41 42 4c 45 20 2a 2f 0a 20  REATE TABLE */. 
5e6c0 20 54 72 69 67 67 65 72 20 2a 70 4e 65 77 54 72   Trigger *pNewTr
5e6d0 69 67 67 65 72 3b 20 20 20 20 20 2f 2a 20 54 72  igger;     /* Tr
5e6e0 69 67 67 65 72 20 75 6e 64 65 72 20 63 6f 6e 73  igger under cons
5e6f0 74 72 75 63 74 20 62 79 20 61 20 43 52 45 41 54  truct by a CREAT
5e700 45 20 54 52 49 47 47 45 52 20 2a 2f 0a 20 20 54  E TRIGGER */.  T
5e710 72 69 67 67 65 72 53 74 61 63 6b 20 2a 74 72 69  riggerStack *tri
5e720 67 53 74 61 63 6b 3b 20 20 2f 2a 20 54 72 69 67  gStack;  /* Trig
5e730 67 65 72 20 61 63 74 69 6f 6e 73 20 62 65 69 6e  ger actions bein
5e740 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20 63 6f 6e  g coded */.  con
5e750 73 74 20 63 68 61 72 20 2a 7a 41 75 74 68 43 6f  st char *zAuthCo
5e760 6e 74 65 78 74 3b 20 2f 2a 20 54 68 65 20 36 74  ntext; /* The 6t
5e770 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 64  h parameter to d
5e780 62 2d 3e 78 41 75 74 68 20 63 61 6c 6c 62 61 63  b->xAuth callbac
5e790 6b 73 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  ks */.#ifndef SQ
5e7a0 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
5e7b0 4c 54 41 42 4c 45 0a 20 20 54 6f 6b 65 6e 20 73  LTABLE.  Token s
5e7c0 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Arg;            
5e7d0 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 20      /* Complete 
5e7e0 74 65 78 74 20 6f 66 20 61 20 6d 6f 64 75 6c 65  text of a module
5e7f0 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 20 20 75   argument */.  u
5e800 38 20 64 65 63 6c 61 72 65 56 74 61 62 3b 20 20  8 declareVtab;  
5e810 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
5e820 65 20 69 66 20 69 6e 73 69 64 65 20 73 71 6c 69  e if inside sqli
5e830 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62  te3_declare_vtab
5e840 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 74 61  () */.  int nVta
5e850 62 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20  bLock;          
5e860 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
5e870 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 74  virtual tables t
5e880 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 54 61 62 6c  o lock */.  Tabl
5e890 65 20 2a 2a 61 70 56 74 61 62 4c 6f 63 6b 3b 20  e **apVtabLock; 
5e8a0 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
5e8b0 72 20 74 6f 20 76 69 72 74 75 61 6c 20 74 61 62  r to virtual tab
5e8c0 6c 65 73 20 6e 65 65 64 69 6e 67 20 6c 6f 63 6b  les needing lock
5e8d0 69 6e 67 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69  ing */.#endif.#i
5e8e0 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
5e8f0 5f 54 45 53 54 29 20 7c 7c 20 53 51 4c 49 54 45  _TEST) || SQLITE
5e900 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e  _MAX_EXPR_DEPTH>
5e910 30 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 3b  0.  int nHeight;
5e920 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
5e930 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 68  xpression tree h
5e940 65 69 67 68 74 20 6f 66 20 63 75 72 72 65 6e 74  eight of current
5e950 20 73 75 62 2d 73 65 6c 65 63 74 20 2a 2f 0a 23   sub-select */.#
5e960 65 6e 64 69 66 0a 7d 3b 0a 0a 23 69 66 64 65 66  endif.};..#ifdef
5e970 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
5e980 54 55 41 4c 54 41 42 4c 45 0a 20 20 23 64 65 66  TUALTABLE.  #def
5e990 69 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56  ine IN_DECLARE_V
5e9a0 54 41 42 20 30 0a 23 65 6c 73 65 0a 20 20 23 64  TAB 0.#else.  #d
5e9b0 65 66 69 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45  efine IN_DECLARE
5e9c0 5f 56 54 41 42 20 28 70 50 61 72 73 65 2d 3e 64  _VTAB (pParse->d
5e9d0 65 63 6c 61 72 65 56 74 61 62 29 0a 23 65 6e 64  eclareVtab).#end
5e9e0 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  if../*.** An ins
5e9f0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
5ea00 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
5ea10 20 63 61 6e 20 62 65 20 64 65 63 6c 61 72 65 64   can be declared
5ea20 20 6f 6e 20 61 20 73 74 61 63 6b 20 61 6e 64 20   on a stack and 
5ea30 75 73 65 64 0a 2a 2a 20 74 6f 20 73 61 76 65 20  used.** to save 
5ea40 74 68 65 20 50 61 72 73 65 2e 7a 41 75 74 68 43  the Parse.zAuthC
5ea50 6f 6e 74 65 78 74 20 76 61 6c 75 65 20 73 6f 20  ontext value so 
5ea60 74 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 72  that it can be r
5ea70 65 73 74 6f 72 65 64 20 6c 61 74 65 72 2e 0a 2a  estored later..*
5ea80 2f 0a 73 74 72 75 63 74 20 41 75 74 68 43 6f 6e  /.struct AuthCon
5ea90 74 65 78 74 20 7b 0a 20 20 63 6f 6e 73 74 20 63  text {.  const c
5eaa0 68 61 72 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78  har *zAuthContex
5eab0 74 3b 20 20 20 2f 2a 20 50 75 74 20 73 61 76 65  t;   /* Put save
5eac0 64 20 50 61 72 73 65 2e 7a 41 75 74 68 43 6f 6e  d Parse.zAuthCon
5ead0 74 65 78 74 20 68 65 72 65 20 2a 2f 0a 20 20 50  text here */.  P
5eae0 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20  arse *pParse;   
5eaf0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
5eb00 65 20 50 61 72 73 65 20 73 74 72 75 63 74 75 72  e Parse structur
5eb10 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42  e */.};../*.** B
5eb20 69 74 66 69 65 6c 64 20 66 6c 61 67 73 20 66 6f  itfield flags fo
5eb30 72 20 50 32 20 76 61 6c 75 65 20 69 6e 20 4f 50  r P2 value in OP
5eb40 5f 49 6e 73 65 72 74 20 61 6e 64 20 4f 50 5f 44  _Insert and OP_D
5eb50 65 6c 65 74 65 0a 2a 2f 0a 23 64 65 66 69 6e 65  elete.*/.#define
5eb60 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20   OPFLAG_NCHANGE 
5eb70 20 20 31 20 20 20 20 2f 2a 20 53 65 74 20 74 6f    1    /* Set to
5eb80 20 75 70 64 61 74 65 20 64 62 2d 3e 6e 43 68 61   update db->nCha
5eb90 6e 67 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  nge */.#define O
5eba0 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 20  PFLAG_LASTROWID 
5ebb0 32 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 75  2    /* Set to u
5ebc0 70 64 61 74 65 20 64 62 2d 3e 6c 61 73 74 52 6f  pdate db->lastRo
5ebd0 77 69 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  wid */.#define O
5ebe0 50 46 4c 41 47 5f 49 53 55 50 44 41 54 45 20 20  PFLAG_ISUPDATE  
5ebf0 34 20 20 20 20 2f 2a 20 54 68 69 73 20 4f 50 5f  4    /* This OP_
5ec00 49 6e 73 65 72 74 20 69 73 20 61 6e 20 73 71 6c  Insert is an sql
5ec10 20 55 50 44 41 54 45 20 2a 2f 0a 23 64 65 66 69   UPDATE */.#defi
5ec20 6e 65 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44  ne OPFLAG_APPEND
5ec30 20 20 20 20 38 20 20 20 20 2f 2a 20 54 68 69 73      8    /* This
5ec40 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65   is likely to be
5ec50 20 61 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 0a 2f   an append */../
5ec60 2a 0a 20 2a 20 45 61 63 68 20 74 72 69 67 67 65  *. * Each trigge
5ec70 72 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65  r present in the
5ec80 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
5ec90 20 69 73 20 73 74 6f 72 65 64 20 61 73 20 61 6e   is stored as an
5eca0 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 20 2a 20   instance of. * 
5ecb0 73 74 72 75 63 74 20 54 72 69 67 67 65 72 2e 20  struct Trigger. 
5ecc0 0a 20 2a 0a 20 2a 20 50 6f 69 6e 74 65 72 73 20  . *. * Pointers 
5ecd0 74 6f 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20  to instances of 
5ece0 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20 61  struct Trigger a
5ecf0 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 77 6f  re stored in two
5ed00 20 77 61 79 73 2e 0a 20 2a 20 31 2e 20 49 6e 20   ways.. * 1. In 
5ed10 74 68 65 20 22 74 72 69 67 48 61 73 68 22 20 68  the "trigHash" h
5ed20 61 73 68 20 74 61 62 6c 65 20 28 70 61 72 74 20  ash table (part 
5ed30 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 2a 20  of the sqlite3* 
5ed40 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 20  that represents 
5ed50 74 68 65 20 0a 20 2a 20 20 20 20 64 61 74 61 62  the . *    datab
5ed60 61 73 65 29 2e 20 54 68 69 73 20 61 6c 6c 6f 77  ase). This allow
5ed70 73 20 54 72 69 67 67 65 72 20 73 74 72 75 63 74  s Trigger struct
5ed80 75 72 65 73 20 74 6f 20 62 65 20 72 65 74 72 69  ures to be retri
5ed90 65 76 65 64 20 62 79 20 6e 61 6d 65 2e 0a 20 2a  eved by name.. *
5eda0 20 32 2e 20 41 6c 6c 20 74 72 69 67 67 65 72 73   2. All triggers
5edb0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
5edc0 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20   a single table 
5edd0 66 6f 72 6d 20 61 20 6c 69 6e 6b 65 64 20 6c 69  form a linked li
5ede0 73 74 2c 20 75 73 69 6e 67 20 74 68 65 0a 20 2a  st, using the. *
5edf0 20 20 20 20 70 4e 65 78 74 20 6d 65 6d 62 65 72      pNext member
5ee00 20 6f 66 20 73 74 72 75 63 74 20 54 72 69 67 67   of struct Trigg
5ee10 65 72 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  er. A pointer to
5ee20 20 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65   the first eleme
5ee30 6e 74 20 6f 66 20 74 68 65 0a 20 2a 20 20 20 20  nt of the. *    
5ee40 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 73  linked list is s
5ee50 74 6f 72 65 64 20 61 73 20 74 68 65 20 22 70 54  tored as the "pT
5ee60 72 69 67 67 65 72 22 20 6d 65 6d 62 65 72 20 6f  rigger" member o
5ee70 66 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64  f the associated
5ee80 0a 20 2a 20 20 20 20 73 74 72 75 63 74 20 54 61  . *    struct Ta
5ee90 62 6c 65 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 22  ble.. *. * The "
5eea0 73 74 65 70 5f 6c 69 73 74 22 20 6d 65 6d 62 65  step_list" membe
5eeb0 72 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  r points to the 
5eec0 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66  first element of
5eed0 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 0a 20   a linked list. 
5eee0 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  * containing the
5eef0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20   SQL statements 
5ef00 73 70 65 63 69 66 69 65 64 20 61 73 20 74 68 65  specified as the
5ef10 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d   trigger program
5ef20 2e 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54 72 69  .. */.struct Tri
5ef30 67 67 65 72 20 7b 0a 20 20 63 68 61 72 20 2a 6e  gger {.  char *n
5ef40 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ame;            
5ef50 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20   /* The name of 
5ef60 74 68 65 20 74 72 69 67 67 65 72 20 20 20 20 20  the trigger     
5ef70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ef80 20 20 20 2a 2f 0a 20 20 63 68 61 72 20 2a 74 61     */.  char *ta
5ef90 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ble;            
5efa0 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 72 20  /* The table or 
5efb0 76 69 65 77 20 74 6f 20 77 68 69 63 68 20 74 68  view to which th
5efc0 65 20 74 72 69 67 67 65 72 20 61 70 70 6c 69 65  e trigger applie
5efd0 73 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20  s */.  u8 op;   
5efe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5eff0 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45  * One of TK_DELE
5f000 54 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 54  TE, TK_UPDATE, T
5f010 4b 5f 49 4e 53 45 52 54 20 20 20 20 20 20 20 20  K_INSERT        
5f020 20 2a 2f 0a 20 20 75 38 20 74 72 5f 74 6d 3b 20   */.  u8 tr_tm; 
5f030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5f040 20 4f 6e 65 20 6f 66 20 54 52 49 47 47 45 52 5f   One of TRIGGER_
5f050 42 45 46 4f 52 45 2c 20 54 52 49 47 47 45 52 5f  BEFORE, TRIGGER_
5f060 41 46 54 45 52 20 2a 2f 0a 20 20 45 78 70 72 20  AFTER */.  Expr 
5f070 2a 70 57 68 65 6e 3b 20 20 20 20 20 20 20 20 20  *pWhen;         
5f080 20 20 20 2f 2a 20 54 68 65 20 57 48 45 4e 20 63     /* The WHEN c
5f090 6c 61 75 73 65 20 6f 66 20 74 68 65 20 65 78 70  lause of the exp
5f0a0 72 65 73 69 6f 6e 20 28 6d 61 79 20 62 65 20 4e  resion (may be N
5f0b0 55 4c 4c 29 20 2a 2f 0a 20 20 49 64 4c 69 73 74  ULL) */.  IdList
5f0c0 20 2a 70 43 6f 6c 75 6d 6e 73 3b 20 20 20 20 20   *pColumns;     
5f0d0 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
5f0e0 61 6e 20 55 50 44 41 54 45 20 4f 46 20 3c 63 6f  an UPDATE OF <co
5f0f0 6c 75 6d 6e 2d 6c 69 73 74 3e 20 74 72 69 67 67  lumn-list> trigg
5f100 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  er,.            
5f110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5f120 20 74 68 65 20 3c 63 6f 6c 75 6d 6e 2d 6c 69 73   the <column-lis
5f130 74 3e 20 69 73 20 73 74 6f 72 65 64 20 68 65 72  t> is stored her
5f140 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 6e 61 6d  e */.  Token nam
5f150 65 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 2f  eToken;        /
5f160 2a 20 54 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 69  * Token containi
5f170 6e 67 20 7a 4e 61 6d 65 2e 20 55 73 65 20 64 75  ng zName. Use du
5f180 72 69 6e 67 20 70 61 72 73 69 6e 67 20 6f 6e 6c  ring parsing onl
5f190 79 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70  y */.  Schema *p
5f1a0 53 63 68 65 6d 61 3b 20 20 20 20 20 20 20 20 2f  Schema;        /
5f1b0 2a 20 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e  * Schema contain
5f1c0 69 6e 67 20 74 68 65 20 74 72 69 67 67 65 72 20  ing the trigger 
5f1d0 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 54 61  */.  Schema *pTa
5f1e0 62 53 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20  bSchema;     /* 
5f1f0 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e  Schema containin
5f200 67 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20  g the table */. 
5f210 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 74   TriggerStep *st
5f220 65 70 5f 6c 69 73 74 3b 20 2f 2a 20 4c 69 6e 6b  ep_list; /* Link
5f230 20 6c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72   list of trigger
5f240 20 70 72 6f 67 72 61 6d 20 73 74 65 70 73 20 20   program steps  
5f250 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20             */.  
5f260 54 72 69 67 67 65 72 20 2a 70 4e 65 78 74 3b 20  Trigger *pNext; 
5f270 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20          /* Next 
5f280 74 72 69 67 67 65 72 20 61 73 73 6f 63 69 61 74  trigger associat
5f290 65 64 20 77 69 74 68 20 74 68 65 20 74 61 62 6c  ed with the tabl
5f2a0 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  e */.};../*.** A
5f2b0 20 74 72 69 67 67 65 72 20 69 73 20 65 69 74 68   trigger is eith
5f2c0 65 72 20 61 20 42 45 46 4f 52 45 20 6f 72 20 61  er a BEFORE or a
5f2d0 6e 20 41 46 54 45 52 20 74 72 69 67 67 65 72 2e  n AFTER trigger.
5f2e0 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
5f2f0 63 6f 6e 73 74 61 6e 74 73 0a 2a 2a 20 64 65 74  constants.** det
5f300 65 72 6d 69 6e 65 20 77 68 69 63 68 2e 20 0a 2a  ermine which. .*
5f310 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72  *.** If there ar
5f320 65 20 6d 75 6c 74 69 70 6c 65 20 74 72 69 67 67  e multiple trigg
5f330 65 72 73 2c 20 79 6f 75 20 6d 69 67 68 74 20 6f  ers, you might o
5f340 66 20 73 6f 6d 65 20 42 45 46 4f 52 45 20 61 6e  f some BEFORE an
5f350 64 20 73 6f 6d 65 20 41 46 54 45 52 2e 0a 2a 2a  d some AFTER..**
5f360 20 49 6e 20 74 68 61 74 20 63 61 73 65 73 2c 20   In that cases, 
5f370 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 62 65  the constants be
5f380 6c 6f 77 20 63 61 6e 20 62 65 20 4f 52 65 64 20  low can be ORed 
5f390 74 6f 67 65 74 68 65 72 2e 0a 2a 2f 0a 23 64 65  together..*/.#de
5f3a0 66 69 6e 65 20 54 52 49 47 47 45 52 5f 42 45 46  fine TRIGGER_BEF
5f3b0 4f 52 45 20 20 31 0a 23 64 65 66 69 6e 65 20 54  ORE  1.#define T
5f3c0 52 49 47 47 45 52 5f 41 46 54 45 52 20 20 20 32  RIGGER_AFTER   2
5f3d0 0a 0a 2f 2a 0a 20 2a 20 41 6e 20 69 6e 73 74 61  ../*. * An insta
5f3e0 6e 63 65 20 6f 66 20 73 74 72 75 63 74 20 54 72  nce of struct Tr
5f3f0 69 67 67 65 72 53 74 65 70 20 69 73 20 75 73 65  iggerStep is use
5f400 64 20 74 6f 20 73 74 6f 72 65 20 61 20 73 69 6e  d to store a sin
5f410 67 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  gle SQL statemen
5f420 74 0a 20 2a 20 74 68 61 74 20 69 73 20 61 20 70  t. * that is a p
5f430 61 72 74 20 6f 66 20 61 20 74 72 69 67 67 65 72  art of a trigger
5f440 2d 70 72 6f 67 72 61 6d 2e 20 0a 20 2a 0a 20 2a  -program. . *. *
5f450 20 49 6e 73 74 61 6e 63 65 73 20 6f 66 20 73 74   Instances of st
5f460 72 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70  ruct TriggerStep
5f470 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 61   are stored in a
5f480 20 73 69 6e 67 6c 79 20 6c 69 6e 6b 65 64 20 6c   singly linked l
5f490 69 73 74 20 28 6c 69 6e 6b 65 64 0a 20 2a 20 75  ist (linked. * u
5f4a0 73 69 6e 67 20 74 68 65 20 22 70 4e 65 78 74 22  sing the "pNext"
5f4b0 20 6d 65 6d 62 65 72 29 20 72 65 66 65 72 65 6e   member) referen
5f4c0 63 65 64 20 62 79 20 74 68 65 20 22 73 74 65 70  ced by the "step
5f4d0 5f 6c 69 73 74 22 20 6d 65 6d 62 65 72 20 6f 66  _list" member of
5f4e0 20 74 68 65 20 0a 20 2a 20 61 73 73 6f 63 69 61   the . * associa
5f4f0 74 65 64 20 73 74 72 75 63 74 20 54 72 69 67 67  ted struct Trigg
5f500 65 72 20 69 6e 73 74 61 6e 63 65 2e 20 54 68 65  er instance. The
5f510 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f   first element o
5f520 66 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73  f the linked lis
5f530 74 20 69 73 0a 20 2a 20 74 68 65 20 66 69 72 73  t is. * the firs
5f540 74 20 73 74 65 70 20 6f 66 20 74 68 65 20 74 72  t step of the tr
5f550 69 67 67 65 72 2d 70 72 6f 67 72 61 6d 2e 0a 20  igger-program.. 
5f560 2a 20 0a 20 2a 20 54 68 65 20 22 6f 70 22 20 6d  * . * The "op" m
5f570 65 6d 62 65 72 20 69 6e 64 69 63 61 74 65 73 20  ember indicates 
5f580 77 68 65 74 68 65 72 20 74 68 69 73 20 69 73 20  whether this is 
5f590 61 20 22 44 45 4c 45 54 45 22 2c 20 22 49 4e 53  a "DELETE", "INS
5f5a0 45 52 54 22 2c 20 22 55 50 44 41 54 45 22 20 6f  ERT", "UPDATE" o
5f5b0 72 0a 20 2a 20 22 53 45 4c 45 43 54 22 20 73 74  r. * "SELECT" st
5f5c0 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 6d 65 61  atement. The mea
5f5d0 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 6f 74 68  nings of the oth
5f5e0 65 72 20 6d 65 6d 62 65 72 73 20 69 73 20 64 65  er members is de
5f5f0 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20  termined by the 
5f600 0a 20 2a 20 76 61 6c 75 65 20 6f 66 20 22 6f 70  . * value of "op
5f610 22 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 2a  " as follows:. *
5f620 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 49 4e  . * (op == TK_IN
5f630 53 45 52 54 29 0a 20 2a 20 6f 72 63 6f 6e 66 20  SERT). * orconf 
5f640 20 20 20 2d 3e 20 73 74 6f 72 65 73 20 74 68 65     -> stores the
5f650 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67   ON CONFLICT alg
5f660 6f 72 69 74 68 6d 0a 20 2a 20 70 53 65 6c 65 63  orithm. * pSelec
5f670 74 20 20 20 2d 3e 20 49 66 20 74 68 69 73 20 69  t   -> If this i
5f680 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f  s an INSERT INTO
5f690 20 2e 2e 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20   ... SELECT ... 
5f6a0 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a  statement, then.
5f6b0 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   *              
5f6c0 74 68 69 73 20 73 74 6f 72 65 73 20 61 20 70 6f  this stores a po
5f6d0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 53 45 4c  inter to the SEL
5f6e0 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 4f  ECT statement. O
5f6f0 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20  therwise NULL.. 
5f700 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41  * target    -> A
5f710 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74   token holding t
5f720 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
5f730 61 62 6c 65 20 74 6f 20 69 6e 73 65 72 74 20 69  able to insert i
5f740 6e 74 6f 2e 0a 20 2a 20 70 45 78 70 72 4c 69 73  nto.. * pExprLis
5f750 74 20 2d 3e 20 49 66 20 74 68 69 73 20 69 73 20  t -> If this is 
5f760 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e  an INSERT INTO .
5f770 2e 2e 20 56 41 4c 55 45 53 20 2e 2e 2e 20 73 74  .. VALUES ... st
5f780 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a 20 2a  atement, then. *
5f790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68                th
5f7a0 69 73 20 73 74 6f 72 65 73 20 76 61 6c 75 65 73  is stores values
5f7b0 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 2e   to be inserted.
5f7c0 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e   Otherwise NULL.
5f7d0 0a 20 2a 20 70 49 64 4c 69 73 74 20 20 20 2d 3e  . * pIdList   ->
5f7e0 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 49   If this is an I
5f7f0 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 28  NSERT INTO ... (
5f800 3c 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 3e 29 20  <column-names>) 
5f810 56 41 4c 55 45 53 20 2e 2e 2e 20 0a 20 2a 20 20  VALUES ... . *  
5f820 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61 74              stat
5f830 65 6d 65 6e 74 2c 20 74 68 65 6e 20 74 68 69 73  ement, then this
5f840 20 73 74 6f 72 65 73 20 74 68 65 20 63 6f 6c 75   stores the colu
5f850 6d 6e 2d 6e 61 6d 65 73 20 74 6f 20 62 65 0a 20  mn-names to be. 
5f860 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69  *              i
5f870 6e 73 65 72 74 65 64 20 69 6e 74 6f 2e 0a 20 2a  nserted into.. *
5f880 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 44 45  . * (op == TK_DE
5f890 4c 45 54 45 29 0a 20 2a 20 74 61 72 67 65 74 20  LETE). * target 
5f8a0 20 20 20 2d 3e 20 41 20 74 6f 6b 65 6e 20 68 6f     -> A token ho
5f8b0 6c 64 69 6e 67 20 74 68 65 20 6e 61 6d 65 20 6f  lding the name o
5f8c0 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 64  f the table to d
5f8d0 65 6c 65 74 65 20 66 72 6f 6d 2e 0a 20 2a 20 70  elete from.. * p
5f8e0 57 68 65 72 65 20 20 20 20 2d 3e 20 54 68 65 20  Where    -> The 
5f8f0 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20  WHERE clause of 
5f900 74 68 65 20 44 45 4c 45 54 45 20 73 74 61 74 65  the DELETE state
5f910 6d 65 6e 74 20 69 66 20 6f 6e 65 20 69 73 20 73  ment if one is s
5f920 70 65 63 69 66 69 65 64 2e 0a 20 2a 20 20 20 20  pecified.. *    
5f930 20 20 20 20 20 20 20 20 20 20 4f 74 68 65 72 77            Otherw
5f940 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 0a 20 2a  ise NULL.. * . *
5f950 20 28 6f 70 20 3d 3d 20 54 4b 5f 55 50 44 41 54   (op == TK_UPDAT
5f960 45 29 0a 20 2a 20 74 61 72 67 65 74 20 20 20 20  E). * target    
5f970 2d 3e 20 41 20 74 6f 6b 65 6e 20 68 6f 6c 64 69  -> A token holdi
5f980 6e 67 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  ng the name of t
5f990 68 65 20 74 61 62 6c 65 20 74 6f 20 75 70 64 61  he table to upda
5f9a0 74 65 20 72 6f 77 73 20 6f 66 2e 0a 20 2a 20 70  te rows of.. * p
5f9b0 57 68 65 72 65 20 20 20 20 2d 3e 20 54 68 65 20  Where    -> The 
5f9c0 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20  WHERE clause of 
5f9d0 74 68 65 20 55 50 44 41 54 45 20 73 74 61 74 65  the UPDATE state
5f9e0 6d 65 6e 74 20 69 66 20 6f 6e 65 20 69 73 20 73  ment if one is s
5f9f0 70 65 63 69 66 69 65 64 2e 0a 20 2a 20 20 20 20  pecified.. *    
5fa00 20 20 20 20 20 20 20 20 20 20 4f 74 68 65 72 77            Otherw
5fa10 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 45 78  ise NULL.. * pEx
5fa20 70 72 4c 69 73 74 20 2d 3e 20 41 20 6c 69 73 74  prList -> A list
5fa30 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20   of the columns 
5fa40 74 6f 20 75 70 64 61 74 65 20 61 6e 64 20 74 68  to update and th
5fa50 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 6f  e expressions to
5fa60 20 75 70 64 61 74 65 0a 20 2a 20 20 20 20 20 20   update. *      
5fa70 20 20 20 20 20 20 20 20 74 68 65 6d 20 74 6f 2e          them to.
5fa80 20 53 65 65 20 73 71 6c 69 74 65 33 55 70 64 61   See sqlite3Upda
5fa90 74 65 28 29 20 64 6f 63 75 6d 65 6e 74 61 74 69  te() documentati
5faa0 6f 6e 20 6f 66 20 22 70 43 68 61 6e 67 65 73 22  on of "pChanges"
5fab0 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  . *             
5fac0 20 61 72 67 75 6d 65 6e 74 2e 0a 20 2a 20 0a 20   argument.. * . 
5fad0 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67 65  */.struct Trigge
5fae0 72 53 74 65 70 20 7b 0a 20 20 69 6e 74 20 6f 70  rStep {.  int op
5faf0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
5fb00 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45  * One of TK_DELE
5fb10 54 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 54  TE, TK_UPDATE, T
5fb20 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 53 45 4c  K_INSERT, TK_SEL
5fb30 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63  ECT */.  int orc
5fb40 6f 6e 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  onf;          /*
5fb50 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 65 74 63   OE_Rollback etc
5fb60 2e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a  . */.  Trigger *
5fb70 70 54 72 69 67 3b 20 20 20 20 20 20 2f 2a 20 54  pTrig;      /* T
5fb80 68 65 20 74 72 69 67 67 65 72 20 74 68 61 74 20  he trigger that 
5fb90 74 68 69 73 20 73 74 65 70 20 69 73 20 61 20 70  this step is a p
5fba0 61 72 74 20 6f 66 20 2a 2f 0a 0a 20 20 53 65 6c  art of */..  Sel
5fbb0 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 20  ect *pSelect;   
5fbc0 20 20 2f 2a 20 56 61 6c 69 64 20 66 6f 72 20 53    /* Valid for S
5fbd0 45 4c 45 43 54 20 61 6e 64 20 73 6f 6d 65 74 69  ELECT and someti
5fbe0 6d 65 73 20 0a 20 20 20 20 20 20 20 20 20 20 20  mes .           
5fbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 49                 I
5fc00 4e 53 45 52 54 20 73 74 65 70 73 20 28 77 68 65  NSERT steps (whe
5fc10 6e 20 70 45 78 70 72 4c 69 73 74 20 3d 3d 20 30  n pExprList == 0
5fc20 29 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 74 61 72  ) */.  Token tar
5fc30 67 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 56  get;        /* V
5fc40 61 6c 69 64 20 66 6f 72 20 44 45 4c 45 54 45 2c  alid for DELETE,
5fc50 20 55 50 44 41 54 45 2c 20 49 4e 53 45 52 54 20   UPDATE, INSERT 
5fc60 73 74 65 70 73 20 2a 2f 0a 20 20 45 78 70 72 20  steps */.  Expr 
5fc70 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20 20 20  *pWhere;        
5fc80 2f 2a 20 56 61 6c 69 64 20 66 6f 72 20 44 45 4c  /* Valid for DEL
5fc90 45 54 45 2c 20 55 50 44 41 54 45 20 73 74 65 70  ETE, UPDATE step
5fca0 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  s */.  ExprList 
5fcb0 2a 70 45 78 70 72 4c 69 73 74 3b 20 2f 2a 20 56  *pExprList; /* V
5fcc0 61 6c 69 64 20 66 6f 72 20 55 50 44 41 54 45 20  alid for UPDATE 
5fcd0 73 74 61 74 65 6d 65 6e 74 73 20 61 6e 64 20 73  statements and s
5fce0 6f 6d 65 74 69 6d 65 73 20 0a 20 20 20 20 20 20  ometimes .      
5fcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5fd00 20 20 20 20 20 49 4e 53 45 52 54 20 73 74 65 70       INSERT step
5fd10 73 20 28 77 68 65 6e 20 70 53 65 6c 65 63 74 20  s (when pSelect 
5fd20 3d 3d 20 30 29 20 20 20 20 20 20 20 20 20 2a 2f  == 0)         */
5fd30 0a 20 20 49 64 4c 69 73 74 20 2a 70 49 64 4c 69  .  IdList *pIdLi
5fd40 73 74 3b 20 20 20 20 20 2f 2a 20 56 61 6c 69 64  st;     /* Valid
5fd50 20 66 6f 72 20 49 4e 53 45 52 54 20 73 74 61 74   for INSERT stat
5fd60 65 6d 65 6e 74 73 20 6f 6e 6c 79 20 2a 2f 0a 20  ements only */. 
5fd70 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 4e   TriggerStep *pN
5fd80 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 69 6e  ext;  /* Next in
5fd90 20 74 68 65 20 6c 69 6e 6b 2d 6c 69 73 74 20 2a   the link-list *
5fda0 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20  /.  TriggerStep 
5fdb0 2a 70 4c 61 73 74 3b 20 20 2f 2a 20 4c 61 73 74  *pLast;  /* Last
5fdc0 20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69 6e 6b   element in link
5fdd0 2d 6c 69 73 74 2e 20 56 61 6c 69 64 20 66 6f 72  -list. Valid for
5fde0 20 31 73 74 20 65 6c 65 6d 20 6f 6e 6c 79 20 2a   1st elem only *
5fdf0 2f 0a 7d 3b 0a 0a 2f 2a 0a 20 2a 20 41 6e 20 69  /.};../*. * An i
5fe00 6e 73 74 61 6e 63 65 20 6f 66 20 73 74 72 75 63  nstance of struc
5fe10 74 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 73  t TriggerStack s
5fe20 74 6f 72 65 73 20 69 6e 66 6f 72 6d 61 74 69 6f  tores informatio
5fe30 6e 20 72 65 71 75 69 72 65 64 20 64 75 72 69 6e  n required durin
5fe40 67 20 63 6f 64 65 0a 20 2a 20 67 65 6e 65 72 61  g code. * genera
5fe50 74 69 6f 6e 20 6f 66 20 61 20 73 69 6e 67 6c 65  tion of a single
5fe60 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d   trigger program
5fe70 2e 20 57 68 69 6c 65 20 74 68 65 20 74 72 69 67  . While the trig
5fe80 67 65 72 20 70 72 6f 67 72 61 6d 20 69 73 20 62  ger program is b
5fe90 65 69 6e 67 0a 20 2a 20 63 6f 64 65 64 2c 20 69  eing. * coded, i
5fea0 74 73 20 61 73 73 6f 63 69 61 74 65 64 20 54 72  ts associated Tr
5feb0 69 67 67 65 72 53 74 61 63 6b 20 69 6e 73 74 61  iggerStack insta
5fec0 6e 63 65 20 69 73 20 70 6f 69 6e 74 65 64 20 74  nce is pointed t
5fed0 6f 20 62 79 20 74 68 65 0a 20 2a 20 22 70 54 72  o by the. * "pTr
5fee0 69 67 67 65 72 53 74 61 63 6b 22 20 6d 65 6d 62  iggerStack" memb
5fef0 65 72 20 6f 66 20 74 68 65 20 50 61 72 73 65 20  er of the Parse 
5ff00 73 74 72 75 63 74 75 72 65 2e 0a 20 2a 0a 20 2a  structure.. *. *
5ff10 20 54 68 65 20 70 54 61 62 20 6d 65 6d 62 65 72   The pTab member
5ff20 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 74   points to the t
5ff30 61 62 6c 65 20 74 68 61 74 20 74 72 69 67 67 65  able that trigge
5ff40 72 73 20 61 72 65 20 62 65 69 6e 67 20 63 6f 64  rs are being cod
5ff50 65 64 20 6f 6e 2e 20 54 68 65 20 0a 20 2a 20 6e  ed on. The . * n
5ff60 65 77 49 64 78 20 6d 65 6d 62 65 72 20 63 6f 6e  ewIdx member con
5ff70 74 61 69 6e 73 20 74 68 65 20 69 6e 64 65 78 20  tains the index 
5ff80 6f 66 20 74 68 65 20 76 64 62 65 20 63 75 72 73  of the vdbe curs
5ff90 6f 72 20 74 68 61 74 20 70 6f 69 6e 74 73 20 61  or that points a
5ffa0 74 20 74 68 65 20 74 65 6d 70 0a 20 2a 20 74 61  t the temp. * ta
5ffb0 62 6c 65 20 74 68 61 74 20 73 74 6f 72 65 73 20  ble that stores 
5ffc0 74 68 65 20 6e 65 77 2e 2a 20 72 65 66 65 72 65  the new.* refere
5ffd0 6e 63 65 73 2e 20 49 66 20 6e 65 77 2e 2a 20 72  nces. If new.* r
5ffe0 65 66 65 72 65 6e 63 65 73 20 61 72 65 20 6e 6f  eferences are no
5fff0 74 20 76 61 6c 69 64 0a 20 2a 20 66 6f 72 20 74  t valid. * for t
60000 68 65 20 74 72 69 67 67 65 72 20 62 65 69 6e 67  he trigger being
60010 20 63 6f 64 65 64 20 28 66 6f 72 20 65 78 61 6d   coded (for exam
60020 70 6c 65 20 61 6e 20 4f 4e 20 44 45 4c 45 54 45  ple an ON DELETE
60030 20 74 72 69 67 67 65 72 29 2c 20 74 68 65 6e 20   trigger), then 
60040 6e 65 77 49 64 78 0a 20 2a 20 69 73 20 73 65 74  newIdx. * is set
60050 20 74 6f 20 2d 31 2e 20 54 68 65 20 6f 6c 64 49   to -1. The oldI
60060 64 78 20 6d 65 6d 62 65 72 20 69 73 20 61 6e 61  dx member is ana
60070 6c 6f 67 6f 75 73 20 74 6f 20 6e 65 77 49 64 78  logous to newIdx
60080 2c 20 66 6f 72 20 6f 6c 64 2e 2a 20 72 65 66 65  , for old.* refe
60090 72 65 6e 63 65 73 2e 0a 20 2a 0a 20 2a 20 54 68  rences.. *. * Th
600a0 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f  e ON CONFLICT po
600b0 6c 69 63 79 20 74 6f 20 62 65 20 75 73 65 64 20  licy to be used 
600c0 66 6f 72 20 74 68 65 20 74 72 69 67 67 65 72 20  for the trigger 
600d0 70 72 6f 67 72 61 6d 20 73 74 65 70 73 20 69 73  program steps is
600e0 20 73 74 6f 72 65 64 20 0a 20 2a 20 61 73 20 74   stored . * as t
600f0 68 65 20 6f 72 63 6f 6e 66 20 6d 65 6d 62 65 72  he orconf member
60100 2e 20 49 66 20 74 68 69 73 20 69 73 20 4f 45 5f  . If this is OE_
60110 44 65 66 61 75 6c 74 2c 20 74 68 65 6e 20 74 68  Default, then th
60120 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c  e ON CONFLICT cl
60130 61 75 73 65 20 0a 20 2a 20 73 70 65 63 69 66 69  ause . * specifi
60140 65 64 20 66 6f 72 20 69 6e 64 69 76 69 64 75 61  ed for individua
60150 6c 20 74 72 69 67 67 65 72 73 20 73 74 65 70 73  l triggers steps
60160 20 69 73 20 75 73 65 64 2e 0a 20 2a 0a 20 2a 20   is used.. *. * 
60170 73 74 72 75 63 74 20 54 72 69 67 67 65 72 53 74  struct TriggerSt
60180 61 63 6b 20 68 61 73 20 61 20 22 70 4e 65 78 74  ack has a "pNext
60190 22 20 6d 65 6d 62 65 72 2c 20 74 6f 20 61 6c 6c  " member, to all
601a0 6f 77 20 6c 69 6e 6b 65 64 20 6c 69 73 74 73 20  ow linked lists 
601b0 74 6f 20 62 65 0a 20 2a 20 63 6f 6e 73 74 72 75  to be. * constru
601c0 63 74 65 64 2e 20 57 68 65 6e 20 63 6f 64 69 6e  cted. When codin
601d0 67 20 6e 65 73 74 65 64 20 74 72 69 67 67 65 72  g nested trigger
601e0 73 20 28 74 72 69 67 67 65 72 73 20 66 69 72 65  s (triggers fire
601f0 64 20 62 79 20 6f 74 68 65 72 20 74 72 69 67 67  d by other trigg
60200 65 72 73 29 0a 20 2a 20 65 61 63 68 20 6e 65 73  ers). * each nes
60210 74 65 64 20 74 72 69 67 67 65 72 20 73 74 6f 72  ted trigger stor
60220 65 73 20 69 74 73 20 70 61 72 65 6e 74 20 74 72  es its parent tr
60230 69 67 67 65 72 27 73 20 54 72 69 67 67 65 72 53  igger's TriggerS
60240 74 61 63 6b 20 61 73 20 74 68 65 20 22 70 4e 65  tack as the "pNe
60250 78 74 22 20 0a 20 2a 20 70 6f 69 6e 74 65 72 2e  xt" . * pointer.
60260 20 4f 6e 63 65 20 74 68 65 20 6e 65 73 74 65 64   Once the nested
60270 20 74 72 69 67 67 65 72 20 68 61 73 20 62 65 65   trigger has bee
60280 6e 20 63 6f 64 65 64 2c 20 74 68 65 20 70 4e 65  n coded, the pNe
60290 78 74 20 76 61 6c 75 65 20 69 73 20 72 65 73 74  xt value is rest
602a0 6f 72 65 64 0a 20 2a 20 74 6f 20 74 68 65 20 70  ored. * to the p
602b0 54 72 69 67 67 65 72 53 74 61 63 6b 20 6d 65 6d  TriggerStack mem
602c0 62 65 72 20 6f 66 20 74 68 65 20 50 61 72 73 65  ber of the Parse
602d0 20 73 74 75 63 74 75 72 65 20 61 6e 64 20 63 6f   stucture and co
602e0 64 69 6e 67 20 6f 66 20 74 68 65 20 70 61 72 65  ding of the pare
602f0 6e 74 0a 20 2a 20 74 72 69 67 67 65 72 20 63 6f  nt. * trigger co
60300 6e 74 69 6e 75 65 73 2e 0a 20 2a 0a 20 2a 20 42  ntinues.. *. * B
60310 65 66 6f 72 65 20 61 20 6e 65 73 74 65 64 20 74  efore a nested t
60320 72 69 67 67 65 72 20 69 73 20 63 6f 64 65 64 2c  rigger is coded,
60330 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74   the linked list
60340 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74   pointed to by t
60350 68 65 20 0a 20 2a 20 70 54 72 69 67 67 65 72 53  he . * pTriggerS
60360 74 61 63 6b 20 69 73 20 73 63 61 6e 6e 65 64 20  tack is scanned 
60370 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74  to ensure that t
60380 68 65 20 74 72 69 67 67 65 72 20 69 73 20 6e 6f  he trigger is no
60390 74 20 61 62 6f 75 74 20 74 6f 20 62 65 20 63 6f  t about to be co
603a0 64 65 64 0a 20 2a 20 72 65 63 75 72 73 69 76 65  ded. * recursive
603b0 6c 79 2e 20 49 66 20 74 68 69 73 20 63 6f 6e 64  ly. If this cond
603c0 69 74 69 6f 6e 20 69 73 20 64 65 74 65 63 74 65  ition is detecte
603d0 64 2c 20 74 68 65 20 6e 65 73 74 65 64 20 74 72  d, the nested tr
603e0 69 67 67 65 72 20 69 73 20 6e 6f 74 20 63 6f 64  igger is not cod
603f0 65 64 2e 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54  ed.. */.struct T
60400 72 69 67 67 65 72 53 74 61 63 6b 20 7b 0a 20 20  riggerStack {.  
60410 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20  Table *pTab;    
60420 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 68       /* Table th
60430 61 74 20 74 72 69 67 67 65 72 73 20 61 72 65 20  at triggers are 
60440 63 75 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20  currently being 
60450 63 6f 64 65 64 20 6f 6e 20 2a 2f 0a 20 20 69 6e  coded on */.  in
60460 74 20 6e 65 77 49 64 78 3b 20 20 20 20 20 20 20  t newIdx;       
60470 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 76     /* Index of v
60480 64 62 65 20 63 75 72 73 6f 72 20 74 6f 20 22 6e  dbe cursor to "n
60490 65 77 22 20 74 65 6d 70 20 74 61 62 6c 65 20 2a  ew" temp table *
604a0 2f 0a 20 20 69 6e 74 20 6f 6c 64 49 64 78 3b 20  /.  int oldIdx; 
604b0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
604c0 78 20 6f 66 20 76 64 62 65 20 63 75 72 73 6f 72  x of vdbe cursor
604d0 20 74 6f 20 22 6f 6c 64 22 20 74 65 6d 70 20 74   to "old" temp t
604e0 61 62 6c 65 20 2a 2f 0a 20 20 75 33 32 20 6e 65  able */.  u32 ne
604f0 77 43 6f 6c 4d 61 73 6b 3b 0a 20 20 75 33 32 20  wColMask;.  u32 
60500 6f 6c 64 43 6f 6c 4d 61 73 6b 3b 0a 20 20 69 6e  oldColMask;.  in
60510 74 20 6f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20  t orconf;       
60520 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6f 72     /* Current or
60530 63 6f 6e 66 20 70 6f 6c 69 63 79 20 2a 2f 0a 20  conf policy */. 
60540 20 69 6e 74 20 69 67 6e 6f 72 65 4a 75 6d 70 3b   int ignoreJump;
60550 20 20 20 20 20 20 2f 2a 20 77 68 65 72 65 20 74        /* where t
60560 6f 20 6a 75 6d 70 20 74 6f 20 66 6f 72 20 61 20  o jump to for a 
60570 52 41 49 53 45 28 49 47 4e 4f 52 45 29 20 2a 2f  RAISE(IGNORE) */
60580 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
60590 67 67 65 72 3b 20 20 20 2f 2a 20 54 68 65 20 74  gger;   /* The t
605a0 72 69 67 67 65 72 20 63 75 72 72 65 6e 74 6c 79  rigger currently
605b0 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a   being coded */.
605c0 20 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 2a    TriggerStack *
605d0 70 4e 65 78 74 3b 20 2f 2a 20 4e 65 78 74 20 74  pNext; /* Next t
605e0 72 69 67 67 65 72 20 64 6f 77 6e 20 6f 6e 20 74  rigger down on t
605f0 68 65 20 74 72 69 67 67 65 72 20 73 74 61 63 6b  he trigger stack
60600 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68   */.};../*.** Th
60610 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
60620 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 69  cture contains i
60630 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20  nformation used 
60640 62 79 20 74 68 65 20 73 71 6c 69 74 65 46 69 78  by the sqliteFix
60650 2e 2e 2e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20  ....** routines 
60660 61 73 20 74 68 65 79 20 77 61 6c 6b 20 74 68 65  as they walk the
60670 20 70 61 72 73 65 20 74 72 65 65 20 74 6f 20 6d   parse tree to m
60680 61 6b 65 20 64 61 74 61 62 61 73 65 20 72 65 66  ake database ref
60690 65 72 65 6e 63 65 73 0a 2a 2a 20 65 78 70 6c 69  erences.** expli
606a0 63 69 74 2e 20 20 0a 2a 2f 0a 74 79 70 65 64 65  cit.  .*/.typede
606b0 66 20 73 74 72 75 63 74 20 44 62 46 69 78 65 72  f struct DbFixer
606c0 20 44 62 46 69 78 65 72 3b 0a 73 74 72 75 63 74   DbFixer;.struct
606d0 20 44 62 46 69 78 65 72 20 7b 0a 20 20 50 61 72   DbFixer {.  Par
606e0 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20  se *pParse;     
606f0 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20   /* The parsing 
60700 63 6f 6e 74 65 78 74 2e 20 20 45 72 72 6f 72 20  context.  Error 
60710 6d 65 73 73 61 67 65 73 20 77 72 69 74 74 65 6e  messages written
60720 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74   here */.  const
60730 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 2f   char *zDb;    /
60740 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20  * Make sure all 
60750 6f 62 6a 65 63 74 73 20 61 72 65 20 63 6f 6e 74  objects are cont
60760 61 69 6e 65 64 20 69 6e 20 74 68 69 73 20 64 61  ained in this da
60770 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73  tabase */.  cons
60780 74 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 20  t char *zType;  
60790 2f 2a 20 54 79 70 65 20 6f 66 20 74 68 65 20 63  /* Type of the c
607a0 6f 6e 74 61 69 6e 65 72 20 2d 20 75 73 65 64 20  ontainer - used 
607b0 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67  for error messag
607c0 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f  es */.  const To
607d0 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 2f 2a 20 4e  ken *pName; /* N
607e0 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 61  ame of the conta
607f0 69 6e 65 72 20 2d 20 75 73 65 64 20 66 6f 72 20  iner - used for 
60800 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a  error messages *
60810 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f  /.};../*.** An o
60820 62 6a 65 63 74 65 64 20 75 73 65 64 20 74 6f 20  bjected used to 
60830 61 63 63 75 6d 75 6c 61 74 65 20 74 68 65 20 74  accumulate the t
60840 65 78 74 20 6f 66 20 61 20 73 74 72 69 6e 67 20  ext of a string 
60850 77 68 65 72 65 20 77 65 0a 2a 2a 20 64 6f 20 6e  where we.** do n
60860 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 6b  ot necessarily k
60870 6e 6f 77 20 68 6f 77 20 62 69 67 20 74 68 65 20  now how big the 
60880 73 74 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 69  string will be i
60890 6e 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74  n the end..*/.st
608a0 72 75 63 74 20 53 74 72 41 63 63 75 6d 20 7b 0a  ruct StrAccum {.
608b0 20 20 63 68 61 72 20 2a 7a 42 61 73 65 3b 20 20    char *zBase;  
608c0 20 20 20 2f 2a 20 41 20 62 61 73 65 20 61 6c 6c     /* A base all
608d0 6f 63 61 74 69 6f 6e 2e 20 20 4e 6f 74 20 66 72  ocation.  Not fr
608e0 6f 6d 20 6d 61 6c 6c 6f 63 2e 20 2a 2f 0a 20 20  om malloc. */.  
608f0 63 68 61 72 20 2a 7a 54 65 78 74 3b 20 20 20 20  char *zText;    
60900 20 2f 2a 20 54 68 65 20 73 74 72 69 6e 67 20 63   /* The string c
60910 6f 6c 6c 65 63 74 65 64 20 73 6f 20 66 61 72 20  ollected so far 
60920 2a 2f 0a 20 20 69 6e 74 20 20 6e 43 68 61 72 3b  */.  int  nChar;
60930 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
60940 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 73 6f  of the string so
60950 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 20 6e   far */.  int  n
60960 41 6c 6c 6f 63 3b 20 20 20 20 20 2f 2a 20 41 6d  Alloc;     /* Am
60970 6f 75 6e 74 20 6f 66 20 73 70 61 63 65 20 61 6c  ount of space al
60980 6c 6f 63 61 74 65 64 20 69 6e 20 7a 54 65 78 74  located in zText
60990 20 2a 2f 0a 20 20 69 6e 74 20 20 6d 78 41 6c 6c   */.  int  mxAll
609a0 6f 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 61  oc;        /* Ma
609b0 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 74  ximum allowed st
609c0 72 69 6e 67 20 6c 65 6e 67 74 68 20 2a 2f 0a 20  ring length */. 
609d0 20 75 38 20 20 20 6d 61 6c 6c 6f 63 46 61 69 6c   u8   mallocFail
609e0 65 64 3b 20 20 20 2f 2a 20 42 65 63 6f 6d 65 73  ed;   /* Becomes
609f0 20 74 72 75 65 20 69 66 20 61 6e 79 20 6d 65 6d   true if any mem
60a00 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  ory allocation f
60a10 61 69 6c 73 20 2a 2f 0a 20 20 75 38 20 20 20 75  ails */.  u8   u
60a20 73 65 4d 61 6c 6c 6f 63 3b 20 20 20 20 20 20 2f  seMalloc;      /
60a30 2a 20 54 72 75 65 20 69 66 20 7a 54 65 78 74 20  * True if zText 
60a40 69 73 20 65 6e 6c 61 72 67 61 62 6c 65 20 75 73  is enlargable us
60a50 69 6e 67 20 72 65 61 6c 6c 6f 63 20 2a 2f 0a 20  ing realloc */. 
60a60 20 75 38 20 20 20 74 6f 6f 42 69 67 3b 20 20 20   u8   tooBig;   
60a70 20 20 20 20 20 20 2f 2a 20 42 65 63 6f 6d 65 73        /* Becomes
60a80 20 74 72 75 65 20 69 66 20 73 74 72 69 6e 67 20   true if string 
60a90 73 69 7a 65 20 65 78 63 65 65 64 73 20 6c 69 6d  size exceeds lim
60aa0 69 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  its */.};../*.**
60ab0 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
60ac0 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20  is structure is 
60ad0 75 73 65 64 20 74 6f 20 63 6f 6d 6d 75 6e 69 63  used to communic
60ae0 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a  ate information.
60af0 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 49  ** from sqlite3I
60b00 6e 69 74 20 61 6e 64 20 4f 50 5f 50 61 72 73 65  nit and OP_Parse
60b10 53 63 68 65 6d 61 20 69 6e 74 6f 20 74 68 65 20  Schema into the 
60b20 73 71 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62  sqlite3InitCallb
60b30 61 63 6b 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ack..*/.typedef 
60b40 73 74 72 75 63 74 20 7b 0a 20 20 73 71 6c 69 74  struct {.  sqlit
60b50 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 2f  e3 *db;        /
60b60 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 62  * The database b
60b70 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65 64  eing initialized
60b80 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20   */.  int iDb;  
60b90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30 20 66            /* 0 f
60ba0 6f 72 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  or main database
60bb0 2e 20 20 31 20 66 6f 72 20 54 45 4d 50 2c 20 32  .  1 for TEMP, 2
60bc0 2e 2e 20 66 6f 72 20 41 54 54 41 43 48 65 64 20  .. for ATTACHed 
60bd0 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72  */.  char **pzEr
60be0 72 4d 73 67 3b 20 20 20 20 2f 2a 20 45 72 72 6f  rMsg;    /* Erro
60bf0 72 20 6d 65 73 73 61 67 65 20 73 74 6f 72 65 64  r message stored
60c00 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72   here */.  int r
60c10 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  c;             /
60c20 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 73 74  * Result code st
60c30 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 7d 20 49  ored here */.} I
60c40 6e 69 74 44 61 74 61 3b 0a 0a 2f 2a 0a 2a 2a 20  nitData;../*.** 
60c50 41 73 73 75 6d 69 6e 67 20 7a 49 6e 20 70 6f 69  Assuming zIn poi
60c60 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 74  nts to the first
60c70 20 62 79 74 65 20 6f 66 20 61 20 55 54 46 2d 38   byte of a UTF-8
60c80 20 63 68 61 72 61 63 74 65 72 2c 0a 2a 2a 20 61   character,.** a
60c90 64 76 61 6e 63 65 20 7a 49 6e 20 74 6f 20 70 6f  dvance zIn to po
60ca0 69 6e 74 20 74 6f 20 74 68 65 20 66 69 72 73 74  int to the first
60cb0 20 62 79 74 65 20 6f 66 20 74 68 65 20 6e 65 78   byte of the nex
60cc0 74 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65  t UTF-8 characte
60cd0 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  r..*/.#define SQ
60ce0 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a  LITE_SKIP_UTF8(z
60cf0 49 6e 29 20 7b 20 20 20 20 20 20 20 20 20 20 20  In) {           
60d00 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
60d10 20 69 66 28 20 28 2a 28 7a 49 6e 2b 2b 29 29 3e   if( (*(zIn++))>
60d20 3d 30 78 63 30 20 29 7b 20 20 20 20 20 20 20 20  =0xc0 ){        
60d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
60d40 20 20 20 20 20 20 5c 0a 20 20 20 20 77 68 69 6c        \.    whil
60d50 65 28 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 29  e( (*zIn & 0xc0)
60d60 3d 3d 30 78 38 30 20 29 7b 20 7a 49 6e 2b 2b 3b  ==0x80 ){ zIn++;
60d70 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 5c   }             \
60d80 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20  .  }            
60d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
60da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
60db0 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 2f 2a 0a          \.}../*.
60dc0 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 43 4f  ** The SQLITE_CO
60dd0 52 52 55 50 54 5f 42 4b 50 54 20 6d 61 63 72 6f  RRUPT_BKPT macro
60de0 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61   can be either a
60df0 20 63 6f 6e 73 74 61 6e 74 20 28 66 6f 72 20 70   constant (for p
60e00 72 6f 64 75 63 74 69 6f 6e 0a 2a 2a 20 62 75 69  roduction.** bui
60e10 6c 64 73 29 20 6f 72 20 61 20 66 75 6e 63 74 69  lds) or a functi
60e20 6f 6e 20 63 61 6c 6c 20 28 66 6f 72 20 64 65 62  on call (for deb
60e30 75 67 67 69 6e 67 29 2e 20 20 49 66 20 69 74 20  ugging).  If it 
60e40 69 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 63 61  is a function ca
60e50 6c 6c 2c 0a 2a 2a 20 69 74 20 61 6c 6c 6f 77 73  ll,.** it allows
60e60 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74 6f   the operator to
60e70 20 73 65 74 20 61 20 62 72 65 61 6b 70 6f 69 6e   set a breakpoin
60e80 74 20 61 74 20 74 68 65 20 73 70 6f 74 20 77 68  t at the spot wh
60e90 65 72 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  ere database.** 
60ea0 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 66 69  corruption is fi
60eb0 72 73 74 20 64 65 74 65 63 74 65 64 2e 0a 2a 2f  rst detected..*/
60ec0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
60ed0 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56  EBUG.SQLITE_PRIV
60ee0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
60ef0 33 43 6f 72 72 75 70 74 28 76 6f 69 64 29 3b 0a  3Corrupt(void);.
60f00 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
60f10 43 4f 52 52 55 50 54 5f 42 4b 50 54 20 73 71 6c  CORRUPT_BKPT sql
60f20 69 74 65 33 43 6f 72 72 75 70 74 28 29 0a 23 20  ite3Corrupt().# 
60f30 64 65 66 69 6e 65 20 44 45 42 55 47 4f 4e 4c 59  define DEBUGONLY
60f40 28 58 29 20 20 20 20 20 20 20 20 58 0a 23 65 6c  (X)        X.#el
60f50 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  se.# define SQLI
60f60 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20  TE_CORRUPT_BKPT 
60f70 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 0a 23  SQLITE_CORRUPT.#
60f80 20 64 65 66 69 6e 65 20 44 45 42 55 47 4f 4e 4c   define DEBUGONL
60f90 59 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  Y(X).#endif../*.
60fa0 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e 63  ** Internal func
60fb0 74 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65 73 0a  tion prototypes.
60fc0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
60fd0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72  E int sqlite3Str
60fe0 49 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20  ICmp(const char 
60ff0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  *, const char *)
61000 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
61010 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 4e   int sqlite3StrN
61020 49 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20  ICmp(const char 
61030 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  *, const char *,
61040 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
61050 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
61060 33 49 73 4e 75 6d 62 65 72 28 63 6f 6e 73 74 20  3IsNumber(const 
61070 63 68 61 72 2a 2c 20 69 6e 74 2a 2c 20 75 38 29  char*, int*, u8)
61080 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
61090 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d  E void *sqlite3M
610a0 61 6c 6c 6f 63 5a 65 72 6f 28 75 6e 73 69 67 6e  allocZero(unsign
610b0 65 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ed);.SQLITE_PRIV
610c0 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
610d0 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71  3DbMallocZero(sq
610e0 6c 69 74 65 33 2a 2c 20 75 6e 73 69 67 6e 65 64  lite3*, unsigned
610f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
61100 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44  E void *sqlite3D
61110 62 4d 61 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74  bMallocRaw(sqlit
61120 65 33 2a 2c 20 75 6e 73 69 67 6e 65 64 29 3b 0a  e3*, unsigned);.
61130 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
61140 68 61 72 20 2a 73 71 6c 69 74 65 33 53 74 72 44  har *sqlite3StrD
61150 75 70 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  up(const char*);
61160 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
61170 63 68 61 72 20 2a 73 71 6c 69 74 65 33 53 74 72  char *sqlite3Str
61180 4e 44 75 70 28 63 6f 6e 73 74 20 63 68 61 72 2a  NDup(const char*
61190 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
611a0 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
611b0 69 74 65 33 44 62 53 74 72 44 75 70 28 73 71 6c  ite3DbStrDup(sql
611c0 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ite3*,const char
611d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
611e0 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
611f0 44 62 53 74 72 4e 44 75 70 28 73 71 6c 69 74 65  DbStrNDup(sqlite
61200 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  3*,const char*, 
61210 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
61220 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
61230 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65  e3DbReallocOrFre
61240 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69  e(sqlite3 *, voi
61250 64 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  d *, int);.SQLIT
61260 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
61270 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
61280 28 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64  (sqlite3 *, void
61290 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45   *, int);.SQLITE
612a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
612b0 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 76  ite3MallocSize(v
612c0 6f 69 64 20 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f  oid *);..SQLITE_
612d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
612e0 74 65 33 49 73 4e 61 4e 28 64 6f 75 62 6c 65 29  te3IsNaN(double)
612f0 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
61300 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4d  E char *sqlite3M
61310 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33 2a 2c  Printf(sqlite3*,
61320 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e  const char*, ...
61330 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
61340 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 56  E char *sqlite3V
61350 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33 2a  MPrintf(sqlite3*
61360 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 61  ,const char*, va
61370 5f 6c 69 73 74 29 3b 0a 23 69 66 20 64 65 66 69  _list);.#if defi
61380 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29  ned(SQLITE_TEST)
61390 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
613a0 54 45 5f 44 45 42 55 47 29 0a 53 51 4c 49 54 45  TE_DEBUG).SQLITE
613b0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
613c0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
613d0 74 66 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  tf(const char*, 
613e0 2e 2e 2e 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66  ...);.#endif.#if
613f0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
61400 54 45 53 54 29 0a 53 51 4c 49 54 45 5f 50 52 49  TEST).SQLITE_PRI
61410 56 41 54 45 20 20 20 76 6f 69 64 20 2a 73 71 6c  VATE   void *sql
61420 69 74 65 33 54 65 78 74 54 6f 50 74 72 28 63 6f  ite3TextToPtr(co
61430 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64  nst char*);.#end
61440 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  if.SQLITE_PRIVAT
61450 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65  E void sqlite3Se
61460 74 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 2c  tString(char **,
61470 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52   ...);.SQLITE_PR
61480 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
61490 65 33 45 72 72 6f 72 4d 73 67 28 50 61 72 73 65  e3ErrorMsg(Parse
614a0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  *, const char*, 
614b0 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ...);.SQLITE_PRI
614c0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
614d0 33 45 72 72 6f 72 43 6c 65 61 72 28 50 61 72 73  3ErrorClear(Pars
614e0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
614f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
61500 44 65 71 75 6f 74 65 28 63 68 61 72 2a 29 3b 0a  Dequote(char*);.
61510 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
61520 6f 69 64 20 73 71 6c 69 74 65 33 44 65 71 75 6f  oid sqlite3Dequo
61530 74 65 45 78 70 72 28 73 71 6c 69 74 65 33 2a 2c  teExpr(sqlite3*,
61540 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
61550 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
61560 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65 28 63  te3KeywordCode(c
61570 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
61580 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  ar*, int);.SQLIT
61590 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
615a0 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72 28 50  lite3RunParser(P
615b0 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61  arse*, const cha
615c0 72 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51  r*, char **);.SQ
615d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
615e0 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43  d sqlite3FinishC
615f0 6f 64 69 6e 67 28 50 61 72 73 65 2a 29 3b 0a 53  oding(Parse*);.S
61600 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
61610 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  t sqlite3GetTemp
61620 52 65 67 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c  Reg(Parse*);.SQL
61630 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
61640 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
61650 65 6d 70 52 65 67 28 50 61 72 73 65 2a 2c 69 6e  empReg(Parse*,in
61660 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
61670 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65  TE int sqlite3Ge
61680 74 54 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65  tTempRange(Parse
61690 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  *,int);.SQLITE_P
616a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
616b0 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61  te3ReleaseTempRa
616c0 6e 67 65 28 50 61 72 73 65 2a 2c 69 6e 74 2c 69  nge(Parse*,int,i
616d0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
616e0 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
616f0 33 45 78 70 72 28 73 71 6c 69 74 65 33 2a 2c 20  3Expr(sqlite3*, 
61700 69 6e 74 2c 20 45 78 70 72 2a 2c 20 45 78 70 72  int, Expr*, Expr
61710 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29  *, const Token*)
61720 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
61730 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 50 45   Expr *sqlite3PE
61740 78 70 72 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c  xpr(Parse*, int,
61750 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 2c 20 63   Expr*, Expr*, c
61760 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51  onst Token*);.SQ
61770 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
61780 72 20 2a 73 71 6c 69 74 65 33 52 65 67 69 73 74  r *sqlite3Regist
61790 65 72 45 78 70 72 28 50 61 72 73 65 2a 2c 54 6f  erExpr(Parse*,To
617a0 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
617b0 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
617c0 74 65 33 45 78 70 72 41 6e 64 28 73 71 6c 69 74  te3ExprAnd(sqlit
617d0 65 33 2a 2c 45 78 70 72 2a 2c 20 45 78 70 72 2a  e3*,Expr*, Expr*
617e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
617f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
61800 70 72 53 70 61 6e 28 45 78 70 72 2a 2c 54 6f 6b  prSpan(Expr*,Tok
61810 65 6e 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c  en*,Token*);.SQL
61820 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
61830 20 2a 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e   *sqlite3ExprFun
61840 63 74 69 6f 6e 28 50 61 72 73 65 2a 2c 45 78 70  ction(Parse*,Exp
61850 72 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b  rList*, Token*);
61860 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
61870 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
61880 41 73 73 69 67 6e 56 61 72 4e 75 6d 62 65 72 28  AssignVarNumber(
61890 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a  Parse*, Expr*);.
618a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
618b0 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 44  oid sqlite3ExprD
618c0 65 6c 65 74 65 28 45 78 70 72 2a 29 3b 0a 53 51  elete(Expr*);.SQ
618d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
618e0 72 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 45 78  rList *sqlite3Ex
618f0 70 72 4c 69 73 74 41 70 70 65 6e 64 28 50 61 72  prListAppend(Par
61900 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 45 78  se*,ExprList*,Ex
61910 70 72 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c  pr*,Token*);.SQL
61920 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
61930 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
61940 44 65 6c 65 74 65 28 45 78 70 72 4c 69 73 74 2a  Delete(ExprList*
61950 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
61960 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69  E int sqlite3Ini
61970 74 28 73 71 6c 69 74 65 33 2a 2c 20 63 68 61 72  t(sqlite3*, char
61980 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
61990 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
619a0 6e 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64  nitCallback(void
619b0 2a 2c 20 69 6e 74 2c 20 63 68 61 72 2a 2a 2c 20  *, int, char**, 
619c0 63 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f  char**);.SQLITE_
619d0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
619e0 69 74 65 33 50 72 61 67 6d 61 28 50 61 72 73 65  ite3Pragma(Parse
619f0 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c  *,Token*,Token*,
61a00 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c  Token*,int);.SQL
61a10 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
61a20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
61a30 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71 6c 69  ernalSchema(sqli
61a40 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  te3*, int);.SQLI
61a50 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
61a60 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72 73  sqlite3BeginPars
61a70 65 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53  e(Parse*,int);.S
61a80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
61a90 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74  id sqlite3Commit
61aa0 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 73 28  InternalChanges(
61ab0 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54  sqlite3*);.SQLIT
61ac0 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 20  E_PRIVATE Table 
61ad0 2a 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65  *sqlite3ResultSe
61ae0 74 4f 66 53 65 6c 65 63 74 28 50 61 72 73 65 2a  tOfSelect(Parse*
61af0 2c 63 68 61 72 2a 2c 53 65 6c 65 63 74 2a 29 3b  ,char*,Select*);
61b00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
61b10 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65 6e  void sqlite3Open
61b20 4d 61 73 74 65 72 54 61 62 6c 65 28 50 61 72 73  MasterTable(Pars
61b30 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  e *, int);.SQLIT
61b40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
61b50 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65  qlite3StartTable
61b60 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54  (Parse*,Token*,T
61b70 6f 6b 65 6e 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e  oken*,int,int,in
61b80 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  t,int);.SQLITE_P
61b90 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
61ba0 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72  te3AddColumn(Par
61bb0 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c  se*,Token*);.SQL
61bc0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
61bd0 20 73 71 6c 69 74 65 33 41 64 64 4e 6f 74 4e 75   sqlite3AddNotNu
61be0 6c 6c 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b  ll(Parse*, int);
61bf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
61c00 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 50  void sqlite3AddP
61c10 72 69 6d 61 72 79 4b 65 79 28 50 61 72 73 65 2a  rimaryKey(Parse*
61c20 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74  , ExprList*, int
61c30 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  , int, int);.SQL
61c40 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
61c50 20 73 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b   sqlite3AddCheck
61c60 43 6f 6e 73 74 72 61 69 6e 74 28 50 61 72 73 65  Constraint(Parse
61c70 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54  *, Expr*);.SQLIT
61c80 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
61c90 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54  qlite3AddColumnT
61ca0 79 70 65 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e  ype(Parse*,Token
61cb0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
61cc0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
61cd0 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28 50  ddDefaultValue(P
61ce0 61 72 73 65 2a 2c 45 78 70 72 2a 29 3b 0a 53 51  arse*,Expr*);.SQ
61cf0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
61d00 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c  d sqlite3AddColl
61d10 61 74 65 54 79 70 65 28 50 61 72 73 65 2a 2c 20  ateType(Parse*, 
61d20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
61d30 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
61d40 69 74 65 33 45 6e 64 54 61 62 6c 65 28 50 61 72  ite3EndTable(Par
61d50 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e  se*,Token*,Token
61d60 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 0a 53 51 4c  *,Select*);..SQL
61d70 49 54 45 5f 50 52 49 56 41 54 45 20 42 69 74 76  ITE_PRIVATE Bitv
61d80 65 63 20 2a 73 71 6c 69 74 65 33 42 69 74 76 65  ec *sqlite3Bitve
61d90 63 43 72 65 61 74 65 28 75 33 32 29 3b 0a 53 51  cCreate(u32);.SQ
61da0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
61db0 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65   sqlite3BitvecTe
61dc0 73 74 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29  st(Bitvec*, u32)
61dd0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
61de0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76   int sqlite3Bitv
61df0 65 63 53 65 74 28 42 69 74 76 65 63 2a 2c 20 75  ecSet(Bitvec*, u
61e00 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  32);.SQLITE_PRIV
61e10 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
61e20 42 69 74 76 65 63 43 6c 65 61 72 28 42 69 74 76  BitvecClear(Bitv
61e30 65 63 2a 2c 20 75 33 32 29 3b 0a 53 51 4c 49 54  ec*, u32);.SQLIT
61e40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
61e50 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74  qlite3BitvecDest
61e60 72 6f 79 28 42 69 74 76 65 63 2a 29 3b 0a 53 51  roy(Bitvec*);.SQ
61e70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
61e80 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 42 75   sqlite3BitvecBu
61e90 69 6c 74 69 6e 54 65 73 74 28 69 6e 74 2c 69 6e  iltinTest(int,in
61ea0 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49  t*);..SQLITE_PRI
61eb0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
61ec0 33 43 72 65 61 74 65 56 69 65 77 28 50 61 72 73  3CreateView(Pars
61ed0 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a  e*,Token*,Token*
61ee0 2c 54 6f 6b 65 6e 2a 2c 53 65 6c 65 63 74 2a 2c  ,Token*,Select*,
61ef0 69 6e 74 2c 69 6e 74 29 3b 0a 0a 23 69 66 20 21  int,int);..#if !
61f00 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
61f10 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65  MIT_VIEW) || !de
61f20 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
61f30 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 0a  T_VIRTUALTABLE).
61f40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
61f50 20 69 6e 74 20 73 71 6c 69 74 65 33 56 69 65 77   int sqlite3View
61f60 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50  GetColumnNames(P
61f70 61 72 73 65 2a 2c 54 61 62 6c 65 2a 29 3b 0a 23  arse*,Table*);.#
61f80 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71  else.# define sq
61f90 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75  lite3ViewGetColu
61fa0 6d 6e 4e 61 6d 65 73 28 41 2c 42 29 20 30 0a 23  mnNames(A,B) 0.#
61fb0 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52  endif..SQLITE_PR
61fc0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
61fd0 65 33 44 72 6f 70 54 61 62 6c 65 28 50 61 72 73  e3DropTable(Pars
61fe0 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e  e*, SrcList*, in
61ff0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
62000 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
62010 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28  ite3DeleteTable(
62020 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Table*);.SQLITE_
62030 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
62040 69 74 65 33 49 6e 73 65 72 74 28 50 61 72 73 65  ite3Insert(Parse
62050 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70  *, SrcList*, Exp
62060 72 4c 69 73 74 2a 2c 20 53 65 6c 65 63 74 2a 2c  rList*, Select*,
62070 20 49 64 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a   IdList*, int);.
62080 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
62090 6f 69 64 20 2a 73 71 6c 69 74 65 33 41 72 72 61  oid *sqlite3Arra
620a0 79 41 6c 6c 6f 63 61 74 65 28 73 71 6c 69 74 65  yAllocate(sqlite
620b0 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74  3*,void*,int,int
620c0 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29  ,int*,int*,int*)
620d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
620e0 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33   IdList *sqlite3
620f0 49 64 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c  IdListAppend(sql
62100 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 2c 20  ite3*, IdList*, 
62110 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
62120 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
62130 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28 49  te3IdListIndex(I
62140 64 4c 69 73 74 2a 2c 63 6f 6e 73 74 20 63 68 61  dList*,const cha
62150 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
62160 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c  ATE SrcList *sql
62170 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
62180 64 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c  d(sqlite3*, SrcL
62190 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f  ist*, Token*, To
621a0 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
621b0 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73  IVATE SrcList *s
621c0 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
621d0 65 6e 64 46 72 6f 6d 54 65 72 6d 28 50 61 72 73  endFromTerm(Pars
621e0 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f  e*, SrcList*, To
621f0 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f  ken*, Token*, To
62200 6b 65 6e 2a 2c 0a 20 20 20 20 20 20 20 20 20 20  ken*,.          
62210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62220 20 20 20 20 20 20 20 20 20 20 20 20 53 65 6c 65              Sele
62230 63 74 2a 2c 20 45 78 70 72 2a 2c 20 49 64 4c 69  ct*, Expr*, IdLi
62240 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
62250 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
62260 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69  3SrcListShiftJoi
62270 6e 54 79 70 65 28 53 72 63 4c 69 73 74 2a 29 3b  nType(SrcList*);
62280 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
62290 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c  void sqlite3SrcL
622a0 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73  istAssignCursors
622b0 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74  (Parse*, SrcList
622c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
622d0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49  TE void sqlite3I
622e0 64 4c 69 73 74 44 65 6c 65 74 65 28 49 64 4c 69  dListDelete(IdLi
622f0 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
62300 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
62310 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 53  3SrcListDelete(S
62320 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45  rcList*);.SQLITE
62330 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
62340 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78  lite3CreateIndex
62350 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54  (Parse*,Token*,T
62360 6f 6b 65 6e 2a 2c 53 72 63 4c 69 73 74 2a 2c 45  oken*,SrcList*,E
62370 78 70 72 4c 69 73 74 2a 2c 69 6e 74 2c 54 6f 6b  xprList*,int,Tok
62380 65 6e 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20  en*,.           
62390 20 20 20 20 20 20 20 20 20 20 20 20 20 54 6f 6b               Tok
623a0 65 6e 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  en*, int, int);.
623b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
623c0 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 49  oid sqlite3DropI
623d0 6e 64 65 78 28 50 61 72 73 65 2a 2c 20 53 72 63  ndex(Parse*, Src
623e0 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  List*, int);.SQL
623f0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
62400 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 50 61  sqlite3Select(Pa
62410 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 53  rse*, Select*, S
62420 65 6c 65 63 74 44 65 73 74 2a 2c 20 53 65 6c 65  electDest*, Sele
62430 63 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 2c 20  ct*, int, int*, 
62440 63 68 61 72 20 2a 61 66 66 29 3b 0a 53 51 4c 49  char *aff);.SQLI
62450 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63  TE_PRIVATE Selec
62460 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74  t *sqlite3Select
62470 4e 65 77 28 50 61 72 73 65 2a 2c 45 78 70 72 4c  New(Parse*,ExprL
62480 69 73 74 2a 2c 53 72 63 4c 69 73 74 2a 2c 45 78  ist*,SrcList*,Ex
62490 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 0a 20  pr*,ExprList*,. 
624a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
624b0 20 20 20 20 20 20 20 20 45 78 70 72 2a 2c 45 78          Expr*,Ex
624c0 70 72 4c 69 73 74 2a 2c 69 6e 74 2c 45 78 70 72  prList*,int,Expr
624d0 2a 2c 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  *,Expr*);.SQLITE
624e0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
624f0 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
62500 65 28 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49  e(Select*);.SQLI
62510 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65  TE_PRIVATE Table
62520 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74   *sqlite3SrcList
62530 4c 6f 6f 6b 75 70 28 50 61 72 73 65 2a 2c 20 53  Lookup(Parse*, S
62540 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45  rcList*);.SQLITE
62550 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
62560 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 50  ite3IsReadOnly(P
62570 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69  arse*, Table*, i
62580 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
62590 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
625a0 4f 70 65 6e 54 61 62 6c 65 28 50 61 72 73 65 2a  OpenTable(Parse*
625b0 2c 20 69 6e 74 20 69 43 75 72 2c 20 69 6e 74 20  , int iCur, int 
625c0 69 44 62 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74  iDb, Table*, int
625d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
625e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65  E void sqlite3De
625f0 6c 65 74 65 46 72 6f 6d 28 50 61 72 73 65 2a 2c  leteFrom(Parse*,
62600 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 2a   SrcList*, Expr*
62610 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
62620 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 70  E void sqlite3Up
62630 64 61 74 65 28 50 61 72 73 65 2a 2c 20 53 72 63  date(Parse*, Src
62640 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a  List*, ExprList*
62650 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 53  , Expr*, int);.S
62660 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 57 68  QLITE_PRIVATE Wh
62670 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33  ereInfo *sqlite3
62680 57 68 65 72 65 42 65 67 69 6e 28 50 61 72 73 65  WhereBegin(Parse
62690 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70  *, SrcList*, Exp
626a0 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2a 2c 20  r*, ExprList**, 
626b0 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  u8);.SQLITE_PRIV
626c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
626d0 57 68 65 72 65 45 6e 64 28 57 68 65 72 65 49 6e  WhereEnd(WhereIn
626e0 66 6f 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  fo*);.SQLITE_PRI
626f0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
62700 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d  ExprCodeGetColum
62710 6e 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a  n(Parse*, Table*
62720 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 2c  , int, int, int,
62730 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
62740 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
62750 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 50  e3ExprCodeMove(P
62760 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29  arse*, int, int)
62770 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
62780 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
62790 72 43 6c 65 61 72 43 6f 6c 75 6d 6e 43 61 63 68  rClearColumnCach
627a0 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a  e(Parse*, int);.
627b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
627c0 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
627d0 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e  acheAffinityChan
627e0 67 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20  ge(Parse*, int, 
627f0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
62800 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
62810 45 78 70 72 57 72 69 74 61 62 6c 65 52 65 67 69  ExprWritableRegi
62820 73 74 65 72 28 50 61 72 73 65 2a 2c 69 6e 74 2c  ster(Parse*,int,
62830 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
62840 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
62850 33 45 78 70 72 48 61 72 64 43 6f 70 79 28 50 61  3ExprHardCopy(Pa
62860 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53  rse*,int,int);.S
62870 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
62880 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  t sqlite3ExprCod
62890 65 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c  e(Parse*, Expr*,
628a0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
628b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
628c0 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 50 61  3ExprCodeTemp(Pa
628d0 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74  rse*, Expr*, int
628e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
628f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78  TE int sqlite3Ex
62900 70 72 43 6f 64 65 54 61 72 67 65 74 28 50 61 72  prCodeTarget(Par
62910 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29  se*, Expr*, int)
62920 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
62930 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
62940 43 6f 64 65 41 6e 64 43 61 63 68 65 28 50 61 72  CodeAndCache(Par
62950 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29  se*, Expr*, int)
62960 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
62970 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
62980 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28 50  rCodeConstants(P
62990 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53  arse*, Expr*);.S
629a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
629b0 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  t sqlite3ExprCod
629c0 65 45 78 70 72 4c 69 73 74 28 50 61 72 73 65 2a  eExprList(Parse*
629d0 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74  , ExprList*, int
629e0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
629f0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
62a00 74 65 33 45 78 70 72 49 66 54 72 75 65 28 50 61  te3ExprIfTrue(Pa
62a10 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74  rse*, Expr*, int
62a20 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
62a30 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
62a40 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 50  te3ExprIfFalse(P
62a50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e  arse*, Expr*, in
62a60 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
62a70 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73  PRIVATE Table *s
62a80 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
62a90 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
62aa0 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  har*, const char
62ab0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
62ac0 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65  TE Table *sqlite
62ad0 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 50 61 72  3LocateTable(Par
62ae0 73 65 2a 2c 69 6e 74 20 69 73 56 69 65 77 2c 63  se*,int isView,c
62af0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73  onst char*, cons
62b00 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45  t char*);.SQLITE
62b10 5f 50 52 49 56 41 54 45 20 49 6e 64 65 78 20 2a  _PRIVATE Index *
62b20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78  sqlite3FindIndex
62b30 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
62b40 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61  char*, const cha
62b50 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
62b60 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
62b70 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54  UnlinkAndDeleteT
62b80 61 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c 69 6e  able(sqlite3*,in
62b90 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  t,const char*);.
62ba0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
62bb0 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e  oid sqlite3Unlin
62bc0 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 28  kAndDeleteIndex(
62bd0 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e  sqlite3*,int,con
62be0 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54  st char*);.SQLIT
62bf0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
62c00 71 6c 69 74 65 33 56 61 63 75 75 6d 28 50 61 72  qlite3Vacuum(Par
62c10 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  se*);.SQLITE_PRI
62c20 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
62c30 52 75 6e 56 61 63 75 75 6d 28 63 68 61 72 2a 2a  RunVacuum(char**
62c40 2c 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c  , sqlite3*);.SQL
62c50 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
62c60 20 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f   *sqlite3NameFro
62c70 6d 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33 2a 2c  mToken(sqlite3*,
62c80 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45   Token*);.SQLITE
62c90 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
62ca0 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28  ite3ExprCompare(
62cb0 45 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53  Expr*, Expr*);.S
62cc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
62cd0 74 20 73 71 6c 69 74 65 33 45 78 70 72 52 65 73  t sqlite3ExprRes
62ce0 6f 6c 76 65 4e 61 6d 65 73 28 4e 61 6d 65 43 6f  olveNames(NameCo
62cf0 6e 74 65 78 74 20 2a 2c 20 45 78 70 72 20 2a 29  ntext *, Expr *)
62d00 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
62d10 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
62d20 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74  rAnalyzeAggregat
62d30 65 73 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c  es(NameContext*,
62d40 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
62d50 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
62d60 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41  ite3ExprAnalyzeA
62d70 67 67 4c 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65  ggList(NameConte
62d80 78 74 2a 2c 45 78 70 72 4c 69 73 74 2a 29 3b 0a  xt*,ExprList*);.
62d90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56  SQLITE_PRIVATE V
62da0 64 62 65 20 2a 73 71 6c 69 74 65 33 47 65 74 56  dbe *sqlite3GetV
62db0 64 62 65 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c  dbe(Parse*);.SQL
62dc0 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
62dd0 20 2a 73 71 6c 69 74 65 33 43 72 65 61 74 65 49   *sqlite3CreateI
62de0 64 45 78 70 72 28 50 61 72 73 65 20 2a 2c 20 63  dExpr(Parse *, c
62df0 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c  onst char*);.SQL
62e00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
62e10 20 73 71 6c 69 74 65 33 50 72 6e 67 53 61 76 65   sqlite3PrngSave
62e20 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c  State(void);.SQL
62e30 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
62e40 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 74   sqlite3PrngRest
62e50 6f 72 65 53 74 61 74 65 28 76 6f 69 64 29 3b 0a  oreState(void);.
62e60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
62e70 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52  oid sqlite3PrngR
62e80 65 73 65 74 53 74 61 74 65 28 76 6f 69 64 29 3b  esetState(void);
62e90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
62ea0 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c  void sqlite3Roll
62eb0 62 61 63 6b 41 6c 6c 28 73 71 6c 69 74 65 33 2a  backAll(sqlite3*
62ec0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
62ed0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  E void sqlite3Co
62ee0 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 50  deVerifySchema(P
62ef0 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  arse*, int);.SQL
62f00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
62f10 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 61   sqlite3BeginTra
62f20 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 2c  nsaction(Parse*,
62f30 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
62f40 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
62f50 65 33 43 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74  e3CommitTransact
62f60 69 6f 6e 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c  ion(Parse*);.SQL
62f70 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
62f80 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b   sqlite3Rollback
62f90 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73  Transaction(Pars
62fa0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
62fb0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
62fc0 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 78  xprIsConstant(Ex
62fd0 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
62fe0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
62ff0 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f  ExprIsConstantNo
63000 74 4a 6f 69 6e 28 45 78 70 72 2a 29 3b 0a 53 51  tJoin(Expr*);.SQ
63010 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
63020 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f   sqlite3ExprIsCo
63030 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e  nstantOrFunction
63040 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  (Expr*);.SQLITE_
63050 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
63060 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72  te3ExprIsInteger
63070 28 45 78 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53  (Expr*, int*);.S
63080 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
63090 74 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64  t sqlite3IsRowid
630a0 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53  (const char*);.S
630b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
630c0 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61  id sqlite3Genera
630d0 74 65 52 6f 77 44 65 6c 65 74 65 28 50 61 72 73  teRowDelete(Pars
630e0 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c  e*, Table*, int,
630f0 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
63100 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
63110 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52  sqlite3GenerateR
63120 6f 77 49 6e 64 65 78 44 65 6c 65 74 65 28 50 61  owIndexDelete(Pa
63130 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e  rse*, Table*, in
63140 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45  t, int*);.SQLITE
63150 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
63160 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65  ite3GenerateInde
63170 78 4b 65 79 28 50 61 72 73 65 2a 2c 20 49 6e 64  xKey(Parse*, Ind
63180 65 78 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69  ex*, int, int, i
63190 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
631a0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
631b0 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69  GenerateConstrai
631c0 6e 74 43 68 65 63 6b 73 28 50 61 72 73 65 2a 2c  ntChecks(Parse*,
631d0 54 61 62 6c 65 2a 2c 69 6e 74 2c 69 6e 74 2c 0a  Table*,int,int,.
631e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
631f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63200 20 20 20 20 20 69 6e 74 2a 2c 69 6e 74 2c 69 6e       int*,int,in
63210 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49  t,int,int);.SQLI
63220 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
63230 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65 49  sqlite3CompleteI
63240 6e 73 65 72 74 69 6f 6e 28 50 61 72 73 65 2a 2c  nsertion(Parse*,
63250 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e   Table*, int, in
63260 74 2c 20 69 6e 74 2a 2c 69 6e 74 2c 69 6e 74 2c  t, int*,int,int,
63270 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  int,int);.SQLITE
63280 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
63290 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64  ite3OpenTableAnd
632a0 49 6e 64 69 63 65 73 28 50 61 72 73 65 2a 2c 20  Indices(Parse*, 
632b0 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74  Table*, int, int
632c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
632d0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65  E void sqlite3Be
632e0 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
632f0 6e 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69  n(Parse*, int, i
63300 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
63310 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
63320 33 45 78 70 72 44 75 70 28 73 71 6c 69 74 65 33  3ExprDup(sqlite3
63330 2a 2c 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  *,Expr*);.SQLITE
63340 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
63350 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70 79 28 73  lite3TokenCopy(s
63360 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 20  qlite3*,Token*, 
63370 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
63380 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 74  PRIVATE ExprList
63390 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73   *sqlite3ExprLis
633a0 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 45 78  tDup(sqlite3*,Ex
633b0 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45  prList*);.SQLITE
633c0 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73 74  _PRIVATE SrcList
633d0 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74   *sqlite3SrcList
633e0 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 53 72 63  Dup(sqlite3*,Src
633f0 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  List*);.SQLITE_P
63400 52 49 56 41 54 45 20 49 64 4c 69 73 74 20 2a 73  RIVATE IdList *s
63410 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70 28  qlite3IdListDup(
63420 73 71 6c 69 74 65 33 2a 2c 49 64 4c 69 73 74 2a  sqlite3*,IdList*
63430 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
63440 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65  E Select *sqlite
63450 33 53 65 6c 65 63 74 44 75 70 28 73 71 6c 69 74  3SelectDup(sqlit
63460 65 33 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51  e3*,Select*);.SQ
63470 4c 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e  LITE_PRIVATE Fun
63480 63 44 65 66 20 2a 73 71 6c 69 74 65 33 46 69 6e  cDef *sqlite3Fin
63490 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65  dFunction(sqlite
634a0 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69  3*,const char*,i
634b0 6e 74 2c 69 6e 74 2c 75 38 2c 69 6e 74 29 3b 0a  nt,int,u8,int);.
634c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
634d0 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73  oid sqlite3Regis
634e0 74 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74 69  terBuiltinFuncti
634f0 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53  ons(sqlite3*);.S
63500 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
63510 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74  id sqlite3Regist
63520 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69  erDateTimeFuncti
63530 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23  ons(sqlite3*);.#
63540 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
63550 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  UG.SQLITE_PRIVAT
63560 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 53  E   int sqlite3S
63570 61 66 65 74 79 4f 6e 28 73 71 6c 69 74 65 33 2a  afetyOn(sqlite3*
63580 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
63590 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 53  E   int sqlite3S
635a0 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74 65 33  afetyOff(sqlite3
635b0 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  *);.#else.# defi
635c0 6e 65 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  ne sqlite3Safety
635d0 4f 6e 28 41 29 20 30 0a 23 20 64 65 66 69 6e 65  On(A) 0.# define
635e0 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
635f0 66 28 41 29 20 30 0a 23 65 6e 64 69 66 0a 53 51  f(A) 0.#endif.SQ
63600 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
63610 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68   sqlite3SafetyCh
63620 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33 2a 29 3b  eckOk(sqlite3*);
63630 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
63640 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74  int sqlite3Safet
63650 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 73  yCheckSickOrOk(s
63660 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45  qlite3*);.SQLITE
63670 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
63680 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69  lite3ChangeCooki
63690 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a  e(Parse*, int);.
636a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
636b0 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 74 65 72  oid sqlite3Mater
636c0 69 61 6c 69 7a 65 56 69 65 77 28 50 61 72 73 65  ializeView(Parse
636d0 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 45 78 70 72  *, Select*, Expr
636e0 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69 66 6e 64 65  *, int);..#ifnde
636f0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
63700 49 47 47 45 52 0a 53 51 4c 49 54 45 5f 50 52 49  IGGER.SQLITE_PRI
63710 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
63720 74 65 33 42 65 67 69 6e 54 72 69 67 67 65 72 28  te3BeginTrigger(
63730 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 54  Parse*, Token*,T
63740 6f 6b 65 6e 2a 2c 69 6e 74 2c 69 6e 74 2c 49 64  oken*,int,int,Id
63750 4c 69 73 74 2a 2c 53 72 63 4c 69 73 74 2a 2c 0a  List*,SrcList*,.
63760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63770 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72 2a             Expr*
63780 2c 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ,int, int);.SQLI
63790 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
637a0 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 54  d sqlite3FinishT
637b0 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 54  rigger(Parse*, T
637c0 72 69 67 67 65 72 53 74 65 70 2a 2c 20 54 6f 6b  riggerStep*, Tok
637d0 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  en*);.SQLITE_PRI
637e0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
637f0 74 65 33 44 72 6f 70 54 72 69 67 67 65 72 28 50  te3DropTrigger(P
63800 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c  arse*, SrcList*,
63810 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
63820 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
63830 69 74 65 33 44 72 6f 70 54 72 69 67 67 65 72 50  ite3DropTriggerP
63840 74 72 28 50 61 72 73 65 2a 2c 20 54 72 69 67 67  tr(Parse*, Trigg
63850 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  er*);.SQLITE_PRI
63860 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
63870 65 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28  e3TriggersExist(
63880 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20  Parse*, Table*, 
63890 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b  int, ExprList*);
638a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
638b0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 64    int sqlite3Cod
638c0 65 52 6f 77 54 72 69 67 67 65 72 28 50 61 72 73  eRowTrigger(Pars
638d0 65 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73  e*, int, ExprLis
638e0 74 2a 2c 20 69 6e 74 2c 20 54 61 62 6c 65 20 2a  t*, int, Table *
638f0 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 0a 20 20 20  , int, int, .   
63900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63910 20 20 20 20 20 20 20 20 69 6e 74 2c 20 69 6e 74          int, int
63920 2c 20 75 33 32 2a 2c 20 75 33 32 2a 29 3b 0a 20  , u32*, u32*);. 
63930 20 76 6f 69 64 20 73 71 6c 69 74 65 56 69 65 77   void sqliteView
63940 54 72 69 67 67 65 72 73 28 50 61 72 73 65 2a 2c  Triggers(Parse*,
63950 20 54 61 62 6c 65 2a 2c 20 45 78 70 72 2a 2c 20   Table*, Expr*, 
63960 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b  int, ExprList*);
63970 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
63980 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65    void sqlite3De
63990 6c 65 74 65 54 72 69 67 67 65 72 53 74 65 70 28  leteTriggerStep(
639a0 54 72 69 67 67 65 72 53 74 65 70 2a 29 3b 0a 53  TriggerStep*);.S
639b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
639c0 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c  TriggerStep *sql
639d0 69 74 65 33 54 72 69 67 67 65 72 53 65 6c 65 63  ite3TriggerSelec
639e0 74 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c 53  tStep(sqlite3*,S
639f0 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  elect*);.SQLITE_
63a00 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65  PRIVATE   Trigge
63a10 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72  rStep *sqlite3Tr
63a20 69 67 67 65 72 49 6e 73 65 72 74 53 74 65 70 28  iggerInsertStep(
63a30 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c  sqlite3*,Token*,
63a40 20 49 64 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20   IdList*,.      
63a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63a70 20 20 45 78 70 72 4c 69 73 74 2a 2c 53 65 6c 65    ExprList*,Sele
63a80 63 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  ct*,int);.SQLITE
63a90 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67  _PRIVATE   Trigg
63aa0 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54  erStep *sqlite3T
63ab0 72 69 67 67 65 72 55 70 64 61 74 65 53 74 65 70  riggerUpdateStep
63ac0 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a  (sqlite3*,Token*
63ad0 2c 45 78 70 72 4c 69 73 74 2a 2c 20 45 78 70 72  ,ExprList*, Expr
63ae0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
63af0 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65  PRIVATE   Trigge
63b00 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72  rStep *sqlite3Tr
63b10 69 67 67 65 72 44 65 6c 65 74 65 53 74 65 70 28  iggerDeleteStep(
63b20 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c  sqlite3*,Token*,
63b30 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
63b40 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
63b50 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67  qlite3DeleteTrig
63b60 67 65 72 28 54 72 69 67 67 65 72 2a 29 3b 0a 53  ger(Trigger*);.S
63b70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
63b80 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69  void sqlite3Unli
63b90 6e 6b 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67  nkAndDeleteTrigg
63ba0 65 72 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c  er(sqlite3*,int,
63bb0 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 65  const char*);.#e
63bc0 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  lse.# define sql
63bd0 69 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73  ite3TriggersExis
63be0 74 28 41 2c 42 2c 43 2c 44 2c 45 2c 46 29 20 30  t(A,B,C,D,E,F) 0
63bf0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
63c00 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 41  3DeleteTrigger(A
63c10 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ).# define sqlit
63c20 65 33 44 72 6f 70 54 72 69 67 67 65 72 50 74 72  e3DropTriggerPtr
63c30 28 41 2c 42 29 0a 23 20 64 65 66 69 6e 65 20 73  (A,B).# define s
63c40 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44  qlite3UnlinkAndD
63c50 65 6c 65 74 65 54 72 69 67 67 65 72 28 41 2c 42  eleteTrigger(A,B
63c60 2c 43 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  ,C).# define sql
63c70 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67  ite3CodeRowTrigg
63c80 65 72 28 41 2c 42 2c 43 2c 44 2c 45 2c 46 2c 47  er(A,B,C,D,E,F,G
63c90 2c 48 2c 49 2c 4a 2c 4b 29 20 30 0a 23 65 6e 64  ,H,I,J,K) 0.#end
63ca0 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  if..SQLITE_PRIVA
63cb0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f  TE int sqlite3Jo
63cc0 69 6e 54 79 70 65 28 50 61 72 73 65 2a 2c 20 54  inType(Parse*, T
63cd0 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54  oken*, Token*, T
63ce0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
63cf0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
63d00 74 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e  te3CreateForeign
63d10 4b 65 79 28 50 61 72 73 65 2a 2c 20 45 78 70 72  Key(Parse*, Expr
63d20 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 45  List*, Token*, E
63d30 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a  xprList*, int);.
63d40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
63d50 6f 69 64 20 73 71 6c 69 74 65 33 44 65 66 65 72  oid sqlite3Defer
63d60 46 6f 72 65 69 67 6e 4b 65 79 28 50 61 72 73 65  ForeignKey(Parse
63d70 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 6e 64 65 66  *, int);.#ifndef
63d80 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
63d90 48 4f 52 49 5a 41 54 49 4f 4e 0a 53 51 4c 49 54  HORIZATION.SQLIT
63da0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
63db0 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64   sqlite3AuthRead
63dc0 28 50 61 72 73 65 2a 2c 45 78 70 72 2a 2c 53 63  (Parse*,Expr*,Sc
63dd0 68 65 6d 61 2a 2c 53 72 63 4c 69 73 74 2a 29 3b  hema*,SrcList*);
63de0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
63df0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75 74    int sqlite3Aut
63e00 68 43 68 65 63 6b 28 50 61 72 73 65 2a 2c 69 6e  hCheck(Parse*,in
63e10 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  t, const char*, 
63e20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e  const char*, con
63e30 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54  st char*);.SQLIT
63e40 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
63e50 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74   sqlite3AuthCont
63e60 65 78 74 50 75 73 68 28 50 61 72 73 65 2a 2c 20  extPush(Parse*, 
63e70 41 75 74 68 43 6f 6e 74 65 78 74 2a 2c 20 63 6f  AuthContext*, co
63e80 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
63e90 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
63ea0 64 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e  d sqlite3AuthCon
63eb0 74 65 78 74 50 6f 70 28 41 75 74 68 43 6f 6e 74  textPop(AuthCont
63ec0 65 78 74 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64  ext*);.#else.# d
63ed0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74  efine sqlite3Aut
63ee0 68 52 65 61 64 28 61 2c 62 2c 63 2c 64 29 0a 23  hRead(a,b,c,d).#
63ef0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41   define sqlite3A
63f00 75 74 68 43 68 65 63 6b 28 61 2c 62 2c 63 2c 64  uthCheck(a,b,c,d
63f10 2c 65 29 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b  ,e)    SQLITE_OK
63f20 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
63f30 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68  3AuthContextPush
63f40 28 61 2c 62 2c 63 29 0a 23 20 64 65 66 69 6e 65  (a,b,c).# define
63f50 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74   sqlite3AuthCont
63f60 65 78 74 50 6f 70 28 61 29 20 20 28 28 76 6f 69  extPop(a)  ((voi
63f70 64 29 28 61 29 29 0a 23 65 6e 64 69 66 0a 53 51  d)(a)).#endif.SQ
63f80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
63f90 64 20 73 71 6c 69 74 65 33 41 74 74 61 63 68 28  d sqlite3Attach(
63fa0 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 45  Parse*, Expr*, E
63fb0 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51  xpr*, Expr*);.SQ
63fc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
63fd0 64 20 73 71 6c 69 74 65 33 44 65 74 61 63 68 28  d sqlite3Detach(
63fe0 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a  Parse*, Expr*);.
63ff0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
64000 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46  nt sqlite3BtreeF
64010 61 63 74 6f 72 79 28 63 6f 6e 73 74 20 73 71 6c  actory(const sql
64020 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
64030 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c  char *zFilename,
64040 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
64050 20 20 20 20 20 20 20 20 69 6e 74 20 6f 6d 69 74          int omit
64060 4a 6f 75 72 6e 61 6c 2c 20 69 6e 74 20 6e 43 61  Journal, int nCa
64070 63 68 65 2c 20 69 6e 74 20 66 6c 61 67 73 2c 20  che, int flags, 
64080 42 74 72 65 65 20 2a 2a 70 70 42 74 72 65 65 29  Btree **ppBtree)
64090 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
640a0 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 49   int sqlite3FixI
640b0 6e 69 74 28 44 62 46 69 78 65 72 2a 2c 20 50 61  nit(DbFixer*, Pa
640c0 72 73 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74  rse*, int, const
640d0 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 54 6f   char*, const To
640e0 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
640f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
64100 33 46 69 78 53 72 63 4c 69 73 74 28 44 62 46 69  3FixSrcList(DbFi
64110 78 65 72 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b  xer*, SrcList*);
64120 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
64130 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 65  int sqlite3FixSe
64140 6c 65 63 74 28 44 62 46 69 78 65 72 2a 2c 20 53  lect(DbFixer*, S
64150 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  elect*);.SQLITE_
64160 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
64170 74 65 33 46 69 78 45 78 70 72 28 44 62 46 69 78  te3FixExpr(DbFix
64180 65 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c  er*, Expr*);.SQL
64190 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
641a0 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69  sqlite3FixExprLi
641b0 73 74 28 44 62 46 69 78 65 72 2a 2c 20 45 78 70  st(DbFixer*, Exp
641c0 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  rList*);.SQLITE_
641d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
641e0 74 65 33 46 69 78 54 72 69 67 67 65 72 53 74 65  te3FixTriggerSte
641f0 70 28 44 62 46 69 78 65 72 2a 2c 20 54 72 69 67  p(DbFixer*, Trig
64200 67 65 72 53 74 65 70 2a 29 3b 0a 53 51 4c 49 54  gerStep*);.SQLIT
64210 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
64220 6c 69 74 65 33 41 74 6f 46 28 63 6f 6e 73 74 20  lite3AtoF(const 
64230 63 68 61 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 2a  char *z, double*
64240 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68  );.SQLITE_API ch
64250 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 6e 70 72  ar *sqlite3_snpr
64260 69 6e 74 66 28 69 6e 74 2c 63 68 61 72 2a 2c 63  intf(int,char*,c
64270 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b  onst char*,...);
64280 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
64290 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 49 6e  int sqlite3GetIn
642a0 74 33 32 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  t32(const char *
642b0 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  , int*);.SQLITE_
642c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
642d0 74 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28  te3FitsIn64Bits(
642e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e  const char *, in
642f0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
64300 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74  TE int sqlite3Ut
64310 66 31 36 42 79 74 65 4c 65 6e 28 63 6f 6e 73 74  f16ByteLen(const
64320 20 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e   void *pData, in
64330 74 20 6e 43 68 61 72 29 3b 0a 53 51 4c 49 54 45  t nChar);.SQLITE
64340 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
64350 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28  ite3Utf8CharLen(
64360 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
64370 61 2c 20 69 6e 74 20 6e 42 79 74 65 29 3b 0a 53  a, int nByte);.S
64380 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
64390 74 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  t sqlite3Utf8Rea
643a0 64 28 63 6f 6e 73 74 20 75 38 2a 2c 20 63 6f 6e  d(const u8*, con
643b0 73 74 20 75 38 2a 2c 20 63 6f 6e 73 74 20 75 38  st u8*, const u8
643c0 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74  **);../*.** Rout
643d0 69 6e 65 73 20 74 6f 20 72 65 61 64 20 61 6e 64  ines to read and
643e0 20 77 72 69 74 65 20 76 61 72 69 61 62 6c 65 2d   write variable-
643f0 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 2e  length integers.
64400 20 20 54 68 65 73 65 20 75 73 65 64 20 74 6f 0a    These used to.
64410 2a 2a 20 62 65 20 64 65 66 69 6e 65 64 20 6c 6f  ** be defined lo
64420 63 61 6c 6c 79 2c 20 62 75 74 20 6e 6f 77 20 77  cally, but now w
64430 65 20 75 73 65 20 74 68 65 20 76 61 72 69 6e 74  e use the varint
64440 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 65   routines in the
64450 20 75 74 69 6c 2e 63 0a 2a 2a 20 66 69 6c 65 2e   util.c.** file.
64460 20 20 43 6f 64 65 20 73 68 6f 75 6c 64 20 75 73    Code should us
64470 65 20 74 68 65 20 4d 41 43 52 4f 20 66 6f 72 6d  e the MACRO form
64480 73 20 62 65 6c 6f 77 2c 20 61 73 20 74 68 65 20  s below, as the 
64490 56 61 72 69 6e 74 33 32 20 76 65 72 73 69 6f 6e  Varint32 version
644a0 73 0a 2a 2a 20 61 72 65 20 63 6f 64 65 64 20 74  s.** are coded t
644b0 6f 20 61 73 73 75 6d 65 20 74 68 65 20 73 69 6e  o assume the sin
644c0 67 6c 65 20 62 79 74 65 20 63 61 73 65 20 69 73  gle byte case is
644d0 20 61 6c 72 65 61 64 79 20 68 61 6e 64 6c 65 64   already handled
644e0 20 28 77 68 69 63 68 20 0a 2a 2a 20 74 68 65 20   (which .** the 
644f0 4d 41 43 52 4f 20 66 6f 72 6d 20 64 6f 65 73 29  MACRO form does)
64500 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
64510 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
64520 75 74 56 61 72 69 6e 74 28 75 6e 73 69 67 6e 65  utVarint(unsigne
64530 64 20 63 68 61 72 2a 2c 20 75 36 34 29 3b 0a 53  d char*, u64);.S
64540 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
64550 74 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69  t sqlite3PutVari
64560 6e 74 33 32 28 75 6e 73 69 67 6e 65 64 20 63 68  nt32(unsigned ch
64570 61 72 2a 2c 20 75 33 32 29 3b 0a 53 51 4c 49 54  ar*, u32);.SQLIT
64580 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
64590 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 63  lite3GetVarint(c
645a0 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
645b0 61 72 20 2a 2c 20 75 36 34 20 2a 29 3b 0a 53 51  ar *, u64 *);.SQ
645c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
645d0 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e   sqlite3GetVarin
645e0 74 33 32 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e  t32(const unsign
645f0 65 64 20 63 68 61 72 20 2a 2c 20 75 33 32 20 2a  ed char *, u32 *
64600 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
64610 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 72  E int sqlite3Var
64620 69 6e 74 4c 65 6e 28 75 36 34 20 76 29 3b 0a 0a  intLen(u64 v);..
64630 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61 64 65 72  /*.** The header
64640 20 6f 66 20 61 20 72 65 63 6f 72 64 20 63 6f 6e   of a record con
64650 73 69 73 74 73 20 6f 66 20 61 20 73 65 71 75 65  sists of a seque
64660 6e 63 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e  nce variable-len
64670 67 74 68 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a  gth integers..**
64680 20 54 68 65 73 65 20 69 6e 74 65 67 65 72 73 20   These integers 
64690 61 72 65 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79  are almost alway
646a0 73 20 73 6d 61 6c 6c 20 61 6e 64 20 61 72 65 20  s small and are 
646b0 65 6e 63 6f 64 65 64 20 61 73 20 61 20 73 69 6e  encoded as a sin
646c0 67 6c 65 20 62 79 74 65 2e 0a 2a 2a 20 54 68 65  gle byte..** The
646d0 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f   following macro
646e0 73 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65  s take advantage
646f0 20 74 68 69 73 20 66 61 63 74 20 74 6f 20 70 72   this fact to pr
64700 6f 76 69 64 65 20 61 20 66 61 73 74 20 65 6e 63  ovide a fast enc
64710 6f 64 65 0a 2a 2a 20 61 6e 64 20 64 65 63 6f 64  ode.** and decod
64720 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72  e of the integer
64730 73 20 69 6e 20 61 20 72 65 63 6f 72 64 20 68 65  s in a record he
64740 61 64 65 72 2e 20 20 49 74 20 69 73 20 66 61 73  ader.  It is fas
64750 74 65 72 20 66 6f 72 20 74 68 65 20 63 6f 6d 6d  ter for the comm
64760 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 65 72 65  on.** case where
64770 20 74 68 65 20 69 6e 74 65 67 65 72 20 69 73 20   the integer is 
64780 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 20 20  a single byte.  
64790 49 74 20 69 73 20 61 20 6c 69 74 74 6c 65 20 73  It is a little s
647a0 6c 6f 77 65 72 20 77 68 65 6e 20 74 68 65 0a 2a  lower when the.*
647b0 2a 20 69 6e 74 65 67 65 72 20 69 73 20 74 77 6f  * integer is two
647c0 20 6f 72 20 6d 6f 72 65 20 62 79 74 65 73 2e 20   or more bytes. 
647d0 20 42 75 74 20 6f 76 65 72 61 6c 6c 20 69 74 20   But overall it 
647e0 69 73 20 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a  is faster..**.**
647f0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65   The following e
64800 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20 65  xpressions are e
64810 71 75 69 76 61 6c 65 6e 74 3a 0a 2a 2a 0a 2a 2a  quivalent:.**.**
64820 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74 65 33       x = sqlite3
64830 47 65 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20  GetVarint32( A, 
64840 26 42 20 29 3b 0a 2a 2a 20 20 20 20 20 78 20 3d  &B );.**     x =
64850 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e   sqlite3PutVarin
64860 74 33 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 0a  t32( A, B );.**.
64870 2a 2a 20 20 20 20 20 78 20 3d 20 67 65 74 56 61  **     x = getVa
64880 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29 3b 0a  rint32( A, B );.
64890 2a 2a 20 20 20 20 20 78 20 3d 20 70 75 74 56 61  **     x = putVa
648a0 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29 3b 0a  rint32( A, B );.
648b0 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65  **.*/.#define ge
648c0 74 56 61 72 69 6e 74 33 32 28 41 2c 42 29 20 20  tVarint32(A,B)  
648d0 28 28 2a 28 41 29 3c 28 75 6e 73 69 67 6e 65 64  ((*(A)<(unsigned
648e0 20 63 68 61 72 29 30 78 38 30 29 20 3f 20 28 28   char)0x80) ? ((
648f0 42 29 20 3d 20 28 75 33 32 29 2a 28 41 29 29 2c  B) = (u32)*(A)),
64900 31 20 3a 20 73 71 6c 69 74 65 33 47 65 74 56 61  1 : sqlite3GetVa
64910 72 69 6e 74 33 32 28 28 41 29 2c 20 26 28 42 29  rint32((A), &(B)
64920 29 29 0a 23 64 65 66 69 6e 65 20 70 75 74 56 61  )).#define putVa
64930 72 69 6e 74 33 32 28 41 2c 42 29 20 20 28 28 28  rint32(A,B)  (((
64940 42 29 3c 28 75 33 32 29 30 78 38 30 29 20 3f 20  B)<(u32)0x80) ? 
64950 28 2a 28 41 29 20 3d 20 28 75 6e 73 69 67 6e 65  (*(A) = (unsigne
64960 64 20 63 68 61 72 29 28 42 29 29 2c 31 20 3a 20  d char)(B)),1 : 
64970 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74  sqlite3PutVarint
64980 33 32 28 28 41 29 2c 20 28 42 29 29 29 0a 23 64  32((A), (B))).#d
64990 65 66 69 6e 65 20 67 65 74 56 61 72 69 6e 74 20  efine getVarint 
649a0 20 20 20 73 71 6c 69 74 65 33 47 65 74 56 61 72     sqlite3GetVar
649b0 69 6e 74 0a 23 64 65 66 69 6e 65 20 70 75 74 56  int.#define putV
649c0 61 72 69 6e 74 20 20 20 20 73 71 6c 69 74 65 33  arint    sqlite3
649d0 50 75 74 56 61 72 69 6e 74 0a 0a 0a 53 51 4c 49  PutVarint...SQLI
649e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
649f0 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69  sqlite3IndexAffi
64a00 6e 69 74 79 53 74 72 28 56 64 62 65 20 2a 2c 20  nityStr(Vdbe *, 
64a10 49 6e 64 65 78 20 2a 29 3b 0a 53 51 4c 49 54 45  Index *);.SQLITE
64a20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
64a30 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69  lite3TableAffini
64a40 74 79 53 74 72 28 56 64 62 65 20 2a 2c 20 54 61  tyStr(Vdbe *, Ta
64a50 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ble *);.SQLITE_P
64a60 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69  RIVATE char sqli
64a70 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69  te3CompareAffini
64a80 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20  ty(Expr *pExpr, 
64a90 63 68 61 72 20 61 66 66 32 29 3b 0a 53 51 4c 49  char aff2);.SQLI
64aa0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
64ab0 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e  qlite3IndexAffin
64ac0 69 74 79 4f 6b 28 45 78 70 72 20 2a 70 45 78 70  ityOk(Expr *pExp
64ad0 72 2c 20 63 68 61 72 20 69 64 78 5f 61 66 66 69  r, char idx_affi
64ae0 6e 69 74 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52  nity);.SQLITE_PR
64af0 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74  IVATE char sqlit
64b00 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 45  e3ExprAffinity(E
64b10 78 70 72 20 2a 70 45 78 70 72 29 3b 0a 53 51 4c  xpr *pExpr);.SQL
64b20 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
64b30 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 63 6f  sqlite3Atoi64(co
64b40 6e 73 74 20 63 68 61 72 2a 2c 20 69 36 34 2a 29  nst char*, i64*)
64b50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
64b60 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72   void sqlite3Err
64b70 6f 72 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  or(sqlite3*, int
64b80 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e  , const char*,..
64b90 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .);.SQLITE_PRIVA
64ba0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
64bb0 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65  HexToBlob(sqlite
64bc0 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  3*, const char *
64bd0 7a 2c 20 69 6e 74 20 6e 29 3b 0a 53 51 4c 49 54  z, int n);.SQLIT
64be0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
64bf0 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65  lite3TwoPartName
64c00 28 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65 6e 20  (Parse *, Token 
64c10 2a 2c 20 54 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65  *, Token *, Toke
64c20 6e 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  n **);.SQLITE_PR
64c30 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72  IVATE const char
64c40 20 2a 73 71 6c 69 74 65 33 45 72 72 53 74 72 28   *sqlite3ErrStr(
64c50 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
64c60 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
64c70 52 65 61 64 53 63 68 65 6d 61 28 50 61 72 73 65  ReadSchema(Parse
64c80 20 2a 70 50 61 72 73 65 29 3b 0a 53 51 4c 49 54   *pParse);.SQLIT
64c90 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65  E_PRIVATE CollSe
64ca0 71 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 43 6f  q *sqlite3FindCo
64cb0 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 2a 2c 75  llSeq(sqlite3*,u
64cc0 38 20 65 6e 63 2c 20 63 6f 6e 73 74 20 63 68 61  8 enc, const cha
64cd0 72 20 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51  r *,int,int);.SQ
64ce0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c  LITE_PRIVATE Col
64cf0 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 4c 6f 63  lSeq *sqlite3Loc
64d00 61 74 65 43 6f 6c 6c 53 65 71 28 50 61 72 73 65  ateCollSeq(Parse
64d10 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20   *pParse, const 
64d20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74  char *zName, int
64d30 20 6e 4e 61 6d 65 29 3b 0a 53 51 4c 49 54 45 5f   nName);.SQLITE_
64d40 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20  PRIVATE CollSeq 
64d50 2a 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c  *sqlite3ExprColl
64d60 53 65 71 28 50 61 72 73 65 20 2a 70 50 61 72 73  Seq(Parse *pPars
64d70 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 3b  e, Expr *pExpr);
64d80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
64d90 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70  Expr *sqlite3Exp
64da0 72 53 65 74 43 6f 6c 6c 28 50 61 72 73 65 20 2a  rSetColl(Parse *
64db0 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 2c 20  pParse, Expr *, 
64dc0 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45  Token *);.SQLITE
64dd0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
64de0 69 74 65 33 43 68 65 63 6b 43 6f 6c 6c 53 65 71  ite3CheckCollSeq
64df0 28 50 61 72 73 65 20 2a 2c 20 43 6f 6c 6c 53 65  (Parse *, CollSe
64e00 71 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  q *);.SQLITE_PRI
64e10 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
64e20 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28  CheckObjectName(
64e30 50 61 72 73 65 20 2a 2c 20 63 6f 6e 73 74 20 63  Parse *, const c
64e40 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  har *);.SQLITE_P
64e50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
64e60 74 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 65  te3VdbeSetChange
64e70 73 28 73 71 6c 69 74 65 33 20 2a 2c 20 69 6e 74  s(sqlite3 *, int
64e80 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
64e90 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  TE const void *s
64ea0 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28  qlite3ValueText(
64eb0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20  sqlite3_value*, 
64ec0 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  u8);.SQLITE_PRIV
64ed0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
64ee0 61 6c 75 65 42 79 74 65 73 28 73 71 6c 69 74 65  alueBytes(sqlite
64ef0 33 5f 76 61 6c 75 65 2a 2c 20 75 38 29 3b 0a 53  3_value*, u8);.S
64f00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
64f10 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53  id sqlite3ValueS
64f20 65 74 53 74 72 28 73 71 6c 69 74 65 33 5f 76 61  etStr(sqlite3_va
64f30 6c 75 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74  lue*, int, const
64f40 20 76 6f 69 64 20 2a 2c 75 38 2c 20 0a 20 20 20   void *,u8, .   
64f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64f60 20 20 20 20 20 76 6f 69 64 28 2a 29 28 76 6f 69       void(*)(voi
64f70 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  d*));.SQLITE_PRI
64f80 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
64f90 33 56 61 6c 75 65 46 72 65 65 28 73 71 6c 69 74  3ValueFree(sqlit
64fa0 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49  e3_value*);.SQLI
64fb0 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74  TE_PRIVATE sqlit
64fc0 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65  e3_value *sqlite
64fd0 33 56 61 6c 75 65 4e 65 77 28 73 71 6c 69 74 65  3ValueNew(sqlite
64fe0 33 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  3 *);.SQLITE_PRI
64ff0 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
65000 65 33 55 74 66 31 36 74 6f 38 28 73 71 6c 69 74  e3Utf16to8(sqlit
65010 65 33 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64  e3 *, const void
65020 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
65030 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
65040 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72  te3ValueFromExpr
65050 28 73 71 6c 69 74 65 33 20 2a 2c 20 45 78 70 72  (sqlite3 *, Expr
65060 20 2a 2c 20 75 38 2c 20 75 38 2c 20 73 71 6c 69   *, u8, u8, sqli
65070 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 3b 0a 53  te3_value **);.S
65080 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
65090 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41  id sqlite3ValueA
650a0 70 70 6c 79 41 66 66 69 6e 69 74 79 28 73 71 6c  pplyAffinity(sql
650b0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2c 20 75 38  ite3_value *, u8
650c0 2c 20 75 38 29 3b 0a 23 69 66 6e 64 65 66 20 53  , u8);.#ifndef S
650d0 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49  QLITE_AMALGAMATI
650e0 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ON.SQLITE_PRIVAT
650f0 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  E const unsigned
65100 20 63 68 61 72 20 73 71 6c 69 74 65 33 55 70 70   char sqlite3Upp
65110 65 72 54 6f 4c 6f 77 65 72 5b 5d 3b 0a 23 65 6e  erToLower[];.#en
65120 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  dif.SQLITE_PRIVA
65130 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
65140 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 44 62 2a  ootPageMoved(Db*
65150 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  , int, int);.SQL
65160 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
65170 20 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28   sqlite3Reindex(
65180 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20  Parse*, Token*, 
65190 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
651a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
651b0 69 74 65 33 41 6c 74 65 72 46 75 6e 63 74 69 6f  ite3AlterFunctio
651c0 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51  ns(sqlite3*);.SQ
651d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
651e0 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 52 65  d sqlite3AlterRe
651f0 6e 61 6d 65 54 61 62 6c 65 28 50 61 72 73 65 2a  nameTable(Parse*
65200 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65  , SrcList*, Toke
65210 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
65220 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47  ATE int sqlite3G
65230 65 74 54 6f 6b 65 6e 28 63 6f 6e 73 74 20 75 6e  etToken(const un
65240 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c 20 69  signed char *, i
65250 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  nt *);.SQLITE_PR
65260 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
65270 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 50 61  e3NestedParse(Pa
65280 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  rse*, const char
65290 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f  *, ...);.SQLITE_
652a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
652b0 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72  ite3ExpirePrepar
652c0 65 64 53 74 61 74 65 6d 65 6e 74 73 28 73 71 6c  edStatements(sql
652d0 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ite3*);.SQLITE_P
652e0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
652f0 74 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74  te3CodeSubselect
65300 28 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a  (Parse *, Expr *
65310 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
65320 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c  E int sqlite3Sel
65330 65 63 74 52 65 73 6f 6c 76 65 28 50 61 72 73 65  ectResolve(Parse
65340 20 2a 2c 20 53 65 6c 65 63 74 20 2a 2c 20 4e 61   *, Select *, Na
65350 6d 65 43 6f 6e 74 65 78 74 20 2a 29 3b 0a 53 51  meContext *);.SQ
65360 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
65370 64 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44  d sqlite3ColumnD
65380 65 66 61 75 6c 74 28 56 64 62 65 20 2a 2c 20 54  efault(Vdbe *, T
65390 61 62 6c 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51  able *, int);.SQ
653a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
653b0 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 69  d sqlite3AlterFi
653c0 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 50 61  nishAddColumn(Pa
653d0 72 73 65 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b  rse *, Token *);
653e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
653f0 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65  void sqlite3Alte
65400 72 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d 6e 28  rBeginAddColumn(
65410 50 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74  Parse *, SrcList
65420 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
65430 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c  ATE CollSeq *sql
65440 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 73  ite3GetCollSeq(s
65450 71 6c 69 74 65 33 2a 2c 20 43 6f 6c 6c 53 65 71  qlite3*, CollSeq
65460 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   *, const char *
65470 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
65480 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69  RIVATE char sqli
65490 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 28  te3AffinityType(
654a0 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53  const Token*);.S
654b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
654c0 69 64 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a  id sqlite3Analyz
654d0 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a  e(Parse*, Token*
654e0 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  , Token*);.SQLIT
654f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
65500 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75 73 79 48  lite3InvokeBusyH
65510 61 6e 64 6c 65 72 28 42 75 73 79 48 61 6e 64 6c  andler(BusyHandl
65520 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  er*);.SQLITE_PRI
65530 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
65540 46 69 6e 64 44 62 28 73 71 6c 69 74 65 33 2a 2c  FindDb(sqlite3*,
65550 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45   Token*);.SQLITE
65560 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
65570 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64  ite3AnalysisLoad
65580 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 69 44  (sqlite3*,int iD
65590 42 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  B);.SQLITE_PRIVA
655a0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
655b0 65 66 61 75 6c 74 52 6f 77 45 73 74 28 49 6e 64  efaultRowEst(Ind
655c0 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ex*);.SQLITE_PRI
655d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
655e0 33 52 65 67 69 73 74 65 72 4c 69 6b 65 46 75 6e  3RegisterLikeFun
655f0 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 2c  ctions(sqlite3*,
65600 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
65610 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
65620 33 49 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 28  3IsLikeFunction(
65630 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 69  sqlite3*,Expr*,i
65640 6e 74 2a 2c 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nt*,char*);.SQLI
65650 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
65660 73 71 6c 69 74 65 33 41 74 74 61 63 68 46 75 6e  sqlite3AttachFun
65670 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a  ctions(sqlite3 *
65680 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
65690 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 69  E void sqlite3Mi
656a0 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74 28  nimumFileFormat(
656b0 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74  Parse*, int, int
656c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
656d0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63  E void sqlite3Sc
656e0 68 65 6d 61 46 72 65 65 28 76 6f 69 64 20 2a 29  hemaFree(void *)
656f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
65700 20 53 63 68 65 6d 61 20 2a 73 71 6c 69 74 65 33   Schema *sqlite3
65710 53 63 68 65 6d 61 47 65 74 28 73 71 6c 69 74 65  SchemaGet(sqlite
65720 33 20 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a 53  3 *, Btree *);.S
65730 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
65740 74 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  t sqlite3SchemaT
65750 6f 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a  oIndex(sqlite3 *
65760 64 62 2c 20 53 63 68 65 6d 61 20 2a 29 3b 0a 53  db, Schema *);.S
65770 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 4b 65  QLITE_PRIVATE Ke
65780 79 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 49 6e  yInfo *sqlite3In
65790 64 65 78 4b 65 79 69 6e 66 6f 28 50 61 72 73 65  dexKeyinfo(Parse
657a0 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b 0a 53 51   *, Index *);.SQ
657b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
657c0 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75   sqlite3CreateFu
657d0 6e 63 28 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f  nc(sqlite3 *, co
657e0 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c  nst char *, int,
657f0 20 69 6e 74 2c 20 76 6f 69 64 20 2a 2c 20 0a 20   int, void *, . 
65800 20 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 65   void (*)(sqlite
65810 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
65820 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29  qlite3_value **)
65830 2c 0a 20 20 76 6f 69 64 20 28 2a 29 28 73 71 6c  ,.  void (*)(sql
65840 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
65850 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  t,sqlite3_value 
65860 2a 2a 29 2c 20 76 6f 69 64 20 28 2a 29 28 73 71  **), void (*)(sq
65870 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 29  lite3_context*))
65880 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
65890 20 69 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45   int sqlite3ApiE
658a0 78 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  xit(sqlite3 *db,
658b0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
658c0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
658d0 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73  3OpenTempDatabas
658e0 65 28 50 61 72 73 65 20 2a 29 3b 0a 0a 53 51 4c  e(Parse *);..SQL
658f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
65900 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
65910 41 70 70 65 6e 64 28 53 74 72 41 63 63 75 6d 2a  Append(StrAccum*
65920 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74  ,const char*,int
65930 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
65940 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 53  E char *sqlite3S
65950 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 53 74  trAccumFinish(St
65960 72 41 63 63 75 6d 2a 29 3b 0a 53 51 4c 49 54 45  rAccum*);.SQLITE
65970 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
65980 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 73  lite3StrAccumRes
65990 65 74 28 53 74 72 41 63 63 75 6d 2a 29 3b 0a 53  et(StrAccum*);.S
659a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
659b0 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  id sqlite3Select
659c0 44 65 73 74 49 6e 69 74 28 53 65 6c 65 63 74 44  DestInit(SelectD
659d0 65 73 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 0a  est*,int,int);..
659e0 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66  /*.** The interf
659f0 61 63 65 20 74 6f 20 74 68 65 20 4c 45 4d 4f 4e  ace to the LEMON
65a00 2d 67 65 6e 65 72 61 74 65 64 20 70 61 72 73 65  -generated parse
65a10 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  r.*/.SQLITE_PRIV
65a20 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
65a30 33 50 61 72 73 65 72 41 6c 6c 6f 63 28 76 6f 69  3ParserAlloc(voi
65a40 64 2a 28 2a 29 28 73 69 7a 65 5f 74 29 29 3b 0a  d*(*)(size_t));.
65a50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
65a60 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65  oid sqlite3Parse
65a70 72 46 72 65 65 28 76 6f 69 64 2a 2c 20 76 6f 69  rFree(void*, voi
65a80 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51  d(*)(void*));.SQ
65a90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
65aa0 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 28  d sqlite3Parser(
65ab0 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 54 6f 6b 65  void*, int, Toke
65ac0 6e 2c 20 50 61 72 73 65 2a 29 3b 0a 0a 53 51 4c  n, Parse*);..SQL
65ad0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
65ae0 73 71 6c 69 74 65 33 41 75 74 6f 4c 6f 61 64 45  sqlite3AutoLoadE
65af0 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65  xtensions(sqlite
65b00 33 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  3*);.#ifndef SQL
65b10 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58  ITE_OMIT_LOAD_EX
65b20 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f 50  TENSION.SQLITE_P
65b30 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
65b40 6c 69 74 65 33 43 6c 6f 73 65 45 78 74 65 6e 73  lite3CloseExtens
65b50 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a  ions(sqlite3*);.
65b60 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73  #else.# define s
65b70 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74 65 6e  qlite3CloseExten
65b80 73 69 6f 6e 73 28 58 29 0a 23 65 6e 64 69 66 0a  sions(X).#endif.
65b90 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
65ba0 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
65bb0 45 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  E.SQLITE_PRIVATE
65bc0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54     void sqlite3T
65bd0 61 62 6c 65 4c 6f 63 6b 28 50 61 72 73 65 20 2a  ableLock(Parse *
65be0 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 75 38 2c 20  , int, int, u8, 
65bf0 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 23  const char *);.#
65c00 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73  else.  #define s
65c10 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28  qlite3TableLock(
65c20 76 2c 77 2c 78 2c 79 2c 7a 29 0a 23 65 6e 64 69  v,w,x,y,z).#endi
65c30 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
65c40 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49  _TEST.SQLITE_PRI
65c50 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
65c60 65 33 55 74 66 38 54 6f 38 28 75 6e 73 69 67 6e  e3Utf8To8(unsign
65c70 65 64 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69  ed char*);.#endi
65c80 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
65c90 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
65ca0 4c 45 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c  LE.#  define sql
65cb0 69 74 65 33 56 74 61 62 43 6c 65 61 72 28 58 29  ite3VtabClear(X)
65cc0 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74  .#  define sqlit
65cd0 65 33 56 74 61 62 53 79 6e 63 28 58 2c 59 29 20  e3VtabSync(X,Y) 
65ce0 28 59 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71  (Y).#  define sq
65cf0 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63  lite3VtabRollbac
65d00 6b 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73  k(X).#  define s
65d10 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74  qlite3VtabCommit
65d20 28 58 29 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45  (X).#else.SQLITE
65d30 5f 50 52 49 56 41 54 45 20 20 20 20 76 6f 69 64  _PRIVATE    void
65d40 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65 61   sqlite3VtabClea
65d50 72 28 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54  r(Table*);.SQLIT
65d60 45 5f 50 52 49 56 41 54 45 20 20 20 20 69 6e 74  E_PRIVATE    int
65d70 20 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63   sqlite3VtabSync
65d80 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
65d90 74 20 72 63 29 3b 0a 53 51 4c 49 54 45 5f 50 52  t rc);.SQLITE_PR
65da0 49 56 41 54 45 20 20 20 20 69 6e 74 20 73 71 6c  IVATE    int sql
65db0 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b  ite3VtabRollback
65dc0 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53  (sqlite3 *db);.S
65dd0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
65de0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62   int sqlite3Vtab
65df0 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a  Commit(sqlite3 *
65e00 64 62 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49  db);.#endif.SQLI
65e10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
65e20 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b 65 57  sqlite3VtabMakeW
65e30 72 69 74 61 62 6c 65 28 50 61 72 73 65 2a 2c 54  ritable(Parse*,T
65e40 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  able*);.SQLITE_P
65e50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
65e60 74 65 33 56 74 61 62 4c 6f 63 6b 28 73 71 6c 69  te3VtabLock(sqli
65e70 74 65 33 5f 76 74 61 62 2a 29 3b 0a 53 51 4c 49  te3_vtab*);.SQLI
65e80 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
65e90 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63  sqlite3VtabUnloc
65ea0 6b 28 73 71 6c 69 74 65 33 2a 2c 20 73 71 6c 69  k(sqlite3*, sqli
65eb0 74 65 33 5f 76 74 61 62 2a 29 3b 0a 53 51 4c 49  te3_vtab*);.SQLI
65ec0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
65ed0 73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e  sqlite3VtabBegin
65ee0 50 61 72 73 65 28 50 61 72 73 65 2a 2c 20 54 6f  Parse(Parse*, To
65ef0 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f  ken*, Token*, To
65f00 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
65f10 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
65f20 65 33 56 74 61 62 46 69 6e 69 73 68 50 61 72 73  e3VtabFinishPars
65f30 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a  e(Parse*, Token*
65f40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
65f50 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
65f60 61 62 41 72 67 49 6e 69 74 28 50 61 72 73 65 2a  abArgInit(Parse*
65f70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
65f80 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
65f90 61 62 41 72 67 45 78 74 65 6e 64 28 50 61 72 73  abArgExtend(Pars
65fa0 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c  e*, Token*);.SQL
65fb0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
65fc0 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43  sqlite3VtabCallC
65fd0 72 65 61 74 65 28 73 71 6c 69 74 65 33 2a 2c 20  reate(sqlite3*, 
65fe0 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  int, const char 
65ff0 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c  *, char **);.SQL
66000 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
66010 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43  sqlite3VtabCallC
66020 6f 6e 6e 65 63 74 28 50 61 72 73 65 2a 2c 20 54  onnect(Parse*, T
66030 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  able*);.SQLITE_P
66040 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
66050 65 33 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f  e3VtabCallDestro
66060 79 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c  y(sqlite3*, int,
66070 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a   const char *);.
66080 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
66090 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 42 65  nt sqlite3VtabBe
660a0 67 69 6e 28 73 71 6c 69 74 65 33 20 2a 2c 20 73  gin(sqlite3 *, s
660b0 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29 3b 0a  qlite3_vtab *);.
660c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 46  SQLITE_PRIVATE F
660d0 75 6e 63 44 65 66 20 2a 73 71 6c 69 74 65 33 56  uncDef *sqlite3V
660e0 74 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74  tabOverloadFunct
660f0 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 2c 46 75  ion(sqlite3 *,Fu
66100 6e 63 44 65 66 2a 2c 20 69 6e 74 20 6e 41 72 67  ncDef*, int nArg
66110 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
66120 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
66130 6c 69 74 65 33 49 6e 76 61 6c 69 64 46 75 6e 63  lite3InvalidFunc
66140 74 69 6f 6e 28 73 71 6c 69 74 65 33 5f 63 6f 6e  tion(sqlite3_con
66150 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
66160 33 5f 76 61 6c 75 65 2a 2a 29 3b 0a 53 51 4c 49  3_value**);.SQLI
66170 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
66180 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28  qlite3Reprepare(
66190 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Vdbe*);.SQLITE_P
661a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
661b0 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b  te3ExprListCheck
661c0 4c 65 6e 67 74 68 28 50 61 72 73 65 2a 2c 20 45  Length(Parse*, E
661d0 78 70 72 4c 69 73 74 2a 2c 20 63 6f 6e 73 74 20  xprList*, const 
661e0 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  char*);.SQLITE_P
661f0 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a  RIVATE CollSeq *
66200 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d  sqlite3BinaryCom
66210 70 61 72 65 43 6f 6c 6c 53 65 71 28 50 61 72 73  pareCollSeq(Pars
66220 65 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70  e *, Expr *, Exp
66230 72 20 2a 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 76  r *);.../*.** Av
66240 61 69 6c 61 62 6c 65 20 66 61 75 6c 74 20 69 6e  ailable fault in
66250 6a 65 63 74 6f 72 73 2e 20 20 53 68 6f 75 6c 64  jectors.  Should
66260 20 62 65 20 6e 75 6d 62 65 72 65 64 20 62 65 67   be numbered beg
66270 69 6e 6e 69 6e 67 20 77 69 74 68 20 30 2e 0a 2a  inning with 0..*
66280 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
66290 5f 46 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 4d  _FAULTINJECTOR_M
662a0 41 4c 4c 4f 43 20 20 20 20 20 30 0a 23 64 65 66  ALLOC     0.#def
662b0 69 6e 65 20 53 51 4c 49 54 45 5f 46 41 55 4c 54  ine SQLITE_FAULT
662c0 49 4e 4a 45 43 54 4f 52 5f 43 4f 55 4e 54 20 20  INJECTOR_COUNT  
662d0 20 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65      1../*.** The
662e0 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68   interface to th
662f0 65 20 66 61 75 6c 74 20 69 6e 6a 65 63 74 6f 72  e fault injector
66300 20 73 75 62 73 79 73 74 65 6d 2e 20 20 49 66 20   subsystem.  If 
66310 74 68 65 20 66 61 75 6c 74 20 69 6e 6a 65 63 74  the fault inject
66320 6f 72 0a 2a 2a 20 6d 65 63 68 61 6e 69 73 6d 20  or.** mechanism 
66330 69 73 20 64 69 73 61 62 6c 65 64 20 61 74 20 63  is disabled at c
66340 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 74 68 65 6e  ompile-time then
66350 20 73 65 74 20 75 70 20 6d 61 63 72 6f 73 20 73   set up macros s
66360 6f 20 74 68 61 74 20 6e 6f 0a 2a 2a 20 75 6e 6e  o that no.** unn
66370 65 63 65 73 73 61 72 79 20 63 6f 64 65 20 69 73  ecessary code is
66380 20 67 65 6e 65 72 61 74 65 64 2e 0a 2a 2f 0a 23   generated..*/.#
66390 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
663a0 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a  IT_BUILTIN_TEST.
663b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
663c0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 61 75   void sqlite3Fau
663d0 6c 74 43 6f 6e 66 69 67 28 69 6e 74 2c 69 6e 74  ltConfig(int,int
663e0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
663f0 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
66400 74 65 33 46 61 75 6c 74 46 61 69 6c 75 72 65 73  te3FaultFailures
66410 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  (int);.SQLITE_PR
66420 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
66430 74 65 33 46 61 75 6c 74 42 65 6e 69 67 6e 46 61  te3FaultBenignFa
66440 69 6c 75 72 65 73 28 69 6e 74 29 3b 0a 53 51 4c  ilures(int);.SQL
66450 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
66460 74 20 73 71 6c 69 74 65 33 46 61 75 6c 74 50 65  t sqlite3FaultPe
66470 6e 64 69 6e 67 28 69 6e 74 29 3b 0a 53 51 4c 49  nding(int);.SQLI
66480 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
66490 64 20 73 71 6c 69 74 65 33 46 61 75 6c 74 42 65  d sqlite3FaultBe
664a0 67 69 6e 42 65 6e 69 67 6e 28 69 6e 74 29 3b 0a  ginBenign(int);.
664b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
664c0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 61 75   void sqlite3Fau
664d0 6c 74 45 6e 64 42 65 6e 69 67 6e 28 69 6e 74 29  ltEndBenign(int)
664e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
664f0 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 46 61     int sqlite3Fa
66500 75 6c 74 53 74 65 70 28 69 6e 74 29 3b 0a 23 65  ultStep(int);.#e
66510 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  lse.# define sql
66520 69 74 65 33 46 61 75 6c 74 43 6f 6e 66 69 67 28  ite3FaultConfig(
66530 41 2c 42 2c 43 29 0a 23 20 64 65 66 69 6e 65 20  A,B,C).# define 
66540 73 71 6c 69 74 65 33 46 61 75 6c 74 46 61 69 6c  sqlite3FaultFail
66550 75 72 65 73 28 41 29 20 20 20 20 20 20 20 20 20  ures(A)         
66560 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
66570 65 33 46 61 75 6c 74 42 65 6e 69 67 6e 46 61 69  e3FaultBenignFai
66580 6c 75 72 65 73 28 41 29 20 20 20 30 0a 23 20 64  lures(A)   0.# d
66590 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 61 75  efine sqlite3Fau
665a0 6c 74 50 65 6e 64 69 6e 67 28 41 29 20 20 20 20  ltPending(A)    
665b0 20 20 20 20 20 20 28 2d 31 29 0a 23 20 64 65 66        (-1).# def
665c0 69 6e 65 20 73 71 6c 69 74 65 33 46 61 75 6c 74  ine sqlite3Fault
665d0 42 65 67 69 6e 42 65 6e 69 67 6e 28 41 29 0a 23  BeginBenign(A).#
665e0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46   define sqlite3F
665f0 61 75 6c 74 45 6e 64 42 65 6e 69 67 6e 28 41 29  aultEndBenign(A)
66600 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
66610 33 46 61 75 6c 74 53 74 65 70 28 41 29 20 20 20  3FaultStep(A)   
66620 20 20 20 20 20 20 20 20 20 20 30 0a 23 65 6e 64            0.#end
66630 69 66 0a 20 20 0a 20 20 0a 0a 23 64 65 66 69 6e  if.  .  ..#defin
66640 65 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44  e IN_INDEX_ROWID
66650 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65             1.#de
66660 66 69 6e 65 20 49 4e 5f 49 4e 44 45 58 5f 45 50  fine IN_INDEX_EP
66670 48 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a  H             2.
66680 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e 44 45 58  #define IN_INDEX
66690 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20  _INDEX          
666a0 20 33 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54   3.SQLITE_PRIVAT
666b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 6e  E int sqlite3Fin
666c0 64 49 6e 49 6e 64 65 78 28 50 61 72 73 65 20 2a  dInIndex(Parse *
666d0 2c 20 45 78 70 72 20 2a 2c 20 69 6e 74 29 3b 0a  , Expr *, int);.
666e0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
666f0 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49  NABLE_ATOMIC_WRI
66700 54 45 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  TE.SQLITE_PRIVAT
66710 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a  E   int sqlite3J
66720 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74  ournalOpen(sqlit
66730 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20  e3_vfs *, const 
66740 63 68 61 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f  char *, sqlite3_
66750 66 69 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74  file *, int, int
66760 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
66770 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a  E   int sqlite3J
66780 6f 75 72 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74  ournalSize(sqlit
66790 65 33 5f 76 66 73 20 2a 29 3b 0a 53 51 4c 49 54  e3_vfs *);.SQLIT
667a0 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
667b0 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72  sqlite3JournalCr
667c0 65 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  eate(sqlite3_fil
667d0 65 20 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64  e *);.#else.  #d
667e0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4a 6f 75  efine sqlite3Jou
667f0 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 29 20 28  rnalSize(pVfs) (
66800 28 70 56 66 73 29 2d 3e 73 7a 4f 73 46 69 6c 65  (pVfs)->szOsFile
66810 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 64 65  ).#endif..#if de
66820 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53  fined(SQLITE_TES
66830 54 29 20 7c 7c 20 53 51 4c 49 54 45 5f 4d 41 58  T) || SQLITE_MAX
66840 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 53 51  _EXPR_DEPTH>0.SQ
66850 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
66860 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 53  oid sqlite3ExprS
66870 65 74 48 65 69 67 68 74 28 45 78 70 72 20 2a 29  etHeight(Expr *)
66880 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
66890 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65     int sqlite3Se
668a0 6c 65 63 74 45 78 70 72 48 65 69 67 68 74 28 53  lectExprHeight(S
668b0 65 6c 65 63 74 20 2a 29 3b 0a 23 65 6c 73 65 0a  elect *);.#else.
668c0 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65    #define sqlite
668d0 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28 78  3ExprSetHeight(x
668e0 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45  ).#endif..SQLITE
668f0 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c  _PRIVATE u32 sql
66900 69 74 65 33 47 65 74 34 62 79 74 65 28 63 6f 6e  ite3Get4byte(con
66910 73 74 20 75 38 2a 29 3b 0a 53 51 4c 49 54 45 5f  st u8*);.SQLITE_
66920 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
66930 69 74 65 33 50 75 74 34 62 79 74 65 28 75 38 2a  ite3Put4byte(u8*
66940 2c 20 75 33 32 29 3b 0a 0a 23 69 66 64 65 66 20  , u32);..#ifdef 
66950 53 51 4c 49 54 45 5f 53 53 45 0a 23 69 6e 63 6c  SQLITE_SSE.#incl
66960 75 64 65 20 22 73 73 65 49 6e 74 2e 68 22 0a 23  ude "sseInt.h".#
66970 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
66980 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54  LITE_DEBUG.SQLIT
66990 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
669a0 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 72   sqlite3ParserTr
669b0 61 63 65 28 46 49 4c 45 2a 2c 20 63 68 61 72 20  ace(FILE*, char 
669c0 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  *);.#endif../*.*
669d0 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f  * If the SQLITE_
669e0 45 4e 41 42 4c 45 20 49 4f 54 52 41 43 45 20 65  ENABLE IOTRACE e
669f0 78 69 73 74 73 20 74 68 65 6e 20 74 68 65 20 67  xists then the g
66a00 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 0a 2a  lobal variable.*
66a10 2a 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65  * sqlite3IoTrace
66a20 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
66a30 20 61 20 70 72 69 6e 74 66 2d 6c 69 6b 65 20 72   a printf-like r
66a40 6f 75 74 69 6e 65 20 75 73 65 64 20 74 6f 0a 2a  outine used to.*
66a50 2a 20 70 72 69 6e 74 20 49 2f 4f 20 74 72 61 63  * print I/O trac
66a60 69 6e 67 20 6d 65 73 73 61 67 65 73 2e 20 0a 2a  ing messages. .*
66a70 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
66a80 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 0a 23  ENABLE_IOTRACE.#
66a90 20 64 65 66 69 6e 65 20 49 4f 54 52 41 43 45 28   define IOTRACE(
66aa0 41 29 20 20 69 66 28 20 73 71 6c 69 74 65 33 49  A)  if( sqlite3I
66ab0 6f 54 72 61 63 65 20 29 7b 20 73 71 6c 69 74 65  oTrace ){ sqlite
66ac0 33 49 6f 54 72 61 63 65 20 41 3b 20 7d 0a 53 51  3IoTrace A; }.SQ
66ad0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
66ae0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49  oid sqlite3VdbeI
66af0 4f 54 72 61 63 65 53 71 6c 28 56 64 62 65 2a 29  OTraceSql(Vdbe*)
66b00 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
66b10 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 49   void (*sqlite3I
66b20 6f 54 72 61 63 65 29 28 63 6f 6e 73 74 20 63 68  oTrace)(const ch
66b30 61 72 2a 2c 2e 2e 2e 29 3b 0a 23 65 6c 73 65 0a  ar*,...);.#else.
66b40 23 20 64 65 66 69 6e 65 20 49 4f 54 52 41 43 45  # define IOTRACE
66b50 28 41 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  (A).# define sql
66b60 69 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 53  ite3VdbeIOTraceS
66b70 71 6c 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65  ql(X).#endif..#e
66b80 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif../*********
66b90 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c  ***** End of sql
66ba0 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
66bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
66bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
66bd0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
66be0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
66bf0 20 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   date.c ********
66c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
66c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
66c20 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33  ****/./*.** 2003
66c30 20 4f 63 74 6f 62 65 72 20 33 31 0a 2a 2a 0a 2a   October 31.**.*
66c40 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
66c50 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
66c60 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
66c70 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
66c80 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
66c90 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
66ca0 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
66cb0 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
66cc0 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
66cd0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
66ce0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
66cf0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
66d00 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
66d10 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
66d20 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
66d30 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
66d40 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
66d50 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
66d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
66d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
66d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
66d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
66da0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
66db0 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73   the C functions
66dc0 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
66dd0 64 61 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a 2a  date and time.**
66de0 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 53   functions for S
66df0 51 4c 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54  QLite.  .**.** T
66e00 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e 65  here is only one
66e10 20 65 78 70 6f 72 74 65 64 20 73 79 6d 62 6f 6c   exported symbol
66e20 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 2d 20   in this file - 
66e30 74 68 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  the function.** 
66e40 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 44  sqlite3RegisterD
66e50 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73  ateTimeFunctions
66e60 28 29 20 66 6f 75 6e 64 20 61 74 20 74 68 65 20  () found at the 
66e70 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20 66 69  bottom of the fi
66e80 6c 65 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72  le..** All other
66e90 20 63 6f 64 65 20 68 61 73 20 66 69 6c 65 20 73   code has file s
66ea0 63 6f 70 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  cope..**.** $Id:
66eb0 20 64 61 74 65 2e 63 2c 76 20 31 2e 37 39 20 32   date.c,v 1.79 2
66ec0 30 30 38 2f 30 33 2f 32 30 20 31 34 3a 30 33 3a  008/03/20 14:03:
66ed0 32 39 20 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a  29 drh Exp $.**.
66ee0 2a 2a 20 53 51 4c 69 74 65 20 70 72 6f 63 65 73  ** SQLite proces
66ef0 73 65 73 20 61 6c 6c 20 74 69 6d 65 73 20 61 6e  ses all times an
66f00 64 20 64 61 74 65 73 20 61 73 20 4a 75 6c 69 61  d dates as Julia
66f10 6e 20 44 61 79 20 6e 75 6d 62 65 72 73 2e 20 20  n Day numbers.  
66f20 54 68 65 0a 2a 2a 20 64 61 74 65 73 20 61 6e 64  The.** dates and
66f30 20 74 69 6d 65 73 20 61 72 65 20 73 74 6f 72 65   times are store
66f40 64 20 61 73 20 74 68 65 20 6e 75 6d 62 65 72 20  d as the number 
66f50 6f 66 20 64 61 79 73 20 73 69 6e 63 65 20 6e 6f  of days since no
66f60 6f 6e 0a 2a 2a 20 69 6e 20 47 72 65 65 6e 77 69  on.** in Greenwi
66f70 63 68 20 6f 6e 20 4e 6f 76 65 6d 62 65 72 20 32  ch on November 2
66f80 34 2c 20 34 37 31 34 20 42 2e 43 2e 20 61 63 63  4, 4714 B.C. acc
66f90 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 47 72  ording to the Gr
66fa0 65 67 6f 72 69 61 6e 0a 2a 2a 20 63 61 6c 65 6e  egorian.** calen
66fb0 64 61 72 20 73 79 73 74 65 6d 2e 20 0a 2a 2a 0a  dar system. .**.
66fc0 2a 2a 20 31 39 37 30 2d 30 31 2d 30 31 20 30 30  ** 1970-01-01 00
66fd0 3a 30 30 3a 30 30 20 69 73 20 4a 44 20 32 34 34  :00:00 is JD 244
66fe0 30 35 38 37 2e 35 0a 2a 2a 20 32 30 30 30 2d 30  0587.5.** 2000-0
66ff0 31 2d 30 31 20 30 30 3a 30 30 3a 30 30 20 69 73  1-01 00:00:00 is
67000 20 4a 44 20 32 34 35 31 35 34 34 2e 35 0a 2a 2a   JD 2451544.5.**
67010 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65  .** This impleme
67020 6e 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20 79  ntion requires y
67030 65 61 72 73 20 74 6f 20 62 65 20 65 78 70 72 65  ears to be expre
67040 73 73 65 64 20 61 73 20 61 20 34 2d 64 69 67 69  ssed as a 4-digi
67050 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 77 68 69 63  t number.** whic
67060 68 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 6c  h means that onl
67070 79 20 64 61 74 65 73 20 62 65 74 77 65 65 6e 20  y dates between 
67080 30 30 30 30 2d 30 31 2d 30 31 20 61 6e 64 20 39  0000-01-01 and 9
67090 39 39 39 2d 31 32 2d 33 31 20 63 61 6e 0a 2a 2a  999-12-31 can.**
670a0 20 62 65 20 72 65 70 72 65 73 65 6e 74 65 64 2c   be represented,
670b0 20 65 76 65 6e 20 74 68 6f 75 67 68 20 6a 75 6c   even though jul
670c0 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 73 20  ian day numbers 
670d0 61 6c 6c 6f 77 20 61 20 6d 75 63 68 20 77 69 64  allow a much wid
670e0 65 72 0a 2a 2a 20 72 61 6e 67 65 20 6f 66 20 64  er.** range of d
670f0 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ates..**.** The 
67100 47 72 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64  Gregorian calend
67110 61 72 20 73 79 73 74 65 6d 20 69 73 20 75 73 65  ar system is use
67120 64 20 66 6f 72 20 61 6c 6c 20 64 61 74 65 73 20  d for all dates 
67130 61 6e 64 20 74 69 6d 65 73 2c 0a 2a 2a 20 65 76  and times,.** ev
67140 65 6e 20 74 68 6f 73 65 20 74 68 61 74 20 70 72  en those that pr
67150 65 64 61 74 65 20 74 68 65 20 47 72 65 67 6f 72  edate the Gregor
67160 69 61 6e 20 63 61 6c 65 6e 64 61 72 2e 20 20 48  ian calendar.  H
67170 69 73 74 6f 72 69 61 6e 73 20 75 73 75 61 6c 6c  istorians usuall
67180 79 0a 2a 2a 20 75 73 65 20 74 68 65 20 4a 75 6c  y.** use the Jul
67190 69 61 6e 20 63 61 6c 65 6e 64 61 72 20 66 6f 72  ian calendar for
671a0 20 64 61 74 65 73 20 70 72 69 6f 72 20 74 6f 20   dates prior to 
671b0 31 35 38 32 2d 31 30 2d 31 35 20 61 6e 64 20 66  1582-10-15 and f
671c0 6f 72 20 73 6f 6d 65 0a 2a 2a 20 64 61 74 65 73  or some.** dates
671d0 20 61 66 74 65 72 77 61 72 64 73 2c 20 64 65 70   afterwards, dep
671e0 65 6e 64 69 6e 67 20 6f 6e 20 6c 6f 63 61 6c 65  ending on locale
671f0 2e 20 20 42 65 77 61 72 65 20 6f 66 20 74 68 69  .  Beware of thi
67200 73 20 64 69 66 66 65 72 65 6e 63 65 2e 0a 2a 2a  s difference..**
67210 0a 2a 2a 20 54 68 65 20 63 6f 6e 76 65 72 73 69  .** The conversi
67220 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72  on algorithms ar
67230 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 61  e implemented ba
67240 73 65 64 20 6f 6e 20 64 65 73 63 72 69 70 74 69  sed on descripti
67250 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 66 6f  ons.** in the fo
67260 6c 6c 6f 77 69 6e 67 20 74 65 78 74 3a 0a 2a 2a  llowing text:.**
67270 0a 2a 2a 20 20 20 20 20 20 4a 65 61 6e 20 4d 65  .**      Jean Me
67280 65 75 73 0a 2a 2a 20 20 20 20 20 20 41 73 74 72  eus.**      Astr
67290 6f 6e 6f 6d 69 63 61 6c 20 41 6c 67 6f 72 69 74  onomical Algorit
672a0 68 6d 73 2c 20 32 6e 64 20 45 64 69 74 69 6f 6e  hms, 2nd Edition
672b0 2c 20 31 39 39 38 0a 2a 2a 20 20 20 20 20 20 49  , 1998.**      I
672c0 53 42 4d 20 30 2d 39 34 33 33 39 36 2d 36 31 2d  SBM 0-943396-61-
672d0 31 0a 2a 2a 20 20 20 20 20 20 57 69 6c 6c 6d 61  1.**      Willma
672e0 6e 6e 2d 42 65 6c 6c 2c 20 49 6e 63 0a 2a 2a 20  nn-Bell, Inc.** 
672f0 20 20 20 20 20 52 69 63 68 6d 6f 6e 64 2c 20 56       Richmond, V
67300 69 72 67 69 6e 69 61 20 28 55 53 41 29 0a 2a 2f  irginia (USA).*/
67310 0a 23 69 6e 63 6c 75 64 65 20 3c 63 74 79 70 65  .#include <ctype
67320 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 74 69  .h>.#include <ti
67330 6d 65 2e 68 3e 0a 0a 23 69 66 6e 64 65 66 20 53  me.h>..#ifndef S
67340 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54  QLITE_OMIT_DATET
67350 49 4d 45 5f 46 55 4e 43 53 0a 0a 2f 2a 0a 2a 2a  IME_FUNCS../*.**
67360 20 41 20 73 74 72 75 63 74 75 72 65 20 66 6f 72   A structure for
67370 20 68 6f 6c 64 69 6e 67 20 61 20 73 69 6e 67 6c   holding a singl
67380 65 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 2e  e date and time.
67390 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
673a0 63 74 20 44 61 74 65 54 69 6d 65 20 44 61 74 65  ct DateTime Date
673b0 54 69 6d 65 3b 0a 73 74 72 75 63 74 20 44 61 74  Time;.struct Dat
673c0 65 54 69 6d 65 20 7b 0a 20 20 64 6f 75 62 6c 65  eTime {.  double
673d0 20 72 4a 44 3b 20 20 20 20 20 20 2f 2a 20 54 68   rJD;      /* Th
673e0 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d  e julian day num
673f0 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20 59 2c 20  ber */.  int Y, 
67400 4d 2c 20 44 3b 20 20 20 20 20 2f 2a 20 59 65 61  M, D;     /* Yea
67410 72 2c 20 6d 6f 6e 74 68 2c 20 61 6e 64 20 64 61  r, month, and da
67420 79 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 6d 3b  y */.  int h, m;
67430 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 75 72 20          /* Hour 
67440 61 6e 64 20 6d 69 6e 75 74 65 73 20 2a 2f 0a 20  and minutes */. 
67450 20 69 6e 74 20 74 7a 3b 20 20 20 20 20 20 20 20   int tz;        
67460 20 20 2f 2a 20 54 69 6d 65 7a 6f 6e 65 20 6f 66    /* Timezone of
67470 66 73 65 74 20 69 6e 20 6d 69 6e 75 74 65 73 20  fset in minutes 
67480 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 73 3b 20 20  */.  double s;  
67490 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 73        /* Seconds
674a0 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64   */.  char valid
674b0 59 4d 44 3b 20 20 20 2f 2a 20 54 72 75 65 20 69  YMD;   /* True i
674c0 66 20 59 2c 4d 2c 44 20 61 72 65 20 76 61 6c 69  f Y,M,D are vali
674d0 64 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c 69  d */.  char vali
674e0 64 48 4d 53 3b 20 20 20 2f 2a 20 54 72 75 65 20  dHMS;   /* True 
674f0 69 66 20 68 2c 6d 2c 73 20 61 72 65 20 76 61 6c  if h,m,s are val
67500 69 64 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c  id */.  char val
67510 69 64 4a 44 3b 20 20 20 20 2f 2a 20 54 72 75 65  idJD;    /* True
67520 20 69 66 20 72 4a 44 20 69 73 20 76 61 6c 69 64   if rJD is valid
67530 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64   */.  char valid
67540 54 5a 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69  TZ;    /* True i
67550 66 20 74 7a 20 69 73 20 76 61 6c 69 64 20 2a 2f  f tz is valid */
67560 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  .};.../*.** Conv
67570 65 72 74 20 7a 44 61 74 65 20 69 6e 74 6f 20 6f  ert zDate into o
67580 6e 65 20 6f 72 20 6d 6f 72 65 20 69 6e 74 65 67  ne or more integ
67590 65 72 73 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c  ers.  Additional
675a0 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 63 6f   arguments.** co
675b0 6d 65 20 69 6e 20 67 72 6f 75 70 73 20 6f 66 20  me in groups of 
675c0 35 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  5 as follows:.**
675d0 0a 2a 2a 20 20 20 20 20 20 20 4e 20 20 20 20 20  .**       N     
675e0 20 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 67 69    number of digi
675f0 74 73 20 69 6e 20 74 68 65 20 69 6e 74 65 67 65  ts in the intege
67600 72 0a 2a 2a 20 20 20 20 20 20 20 6d 69 6e 20 20  r.**       min  
67610 20 20 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77     minimum allow
67620 65 64 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  ed value of the 
67630 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20  integer.**      
67640 20 6d 61 78 20 20 20 20 20 6d 61 78 69 6d 75 6d   max     maximum
67650 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f   allowed value o
67660 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a  f the integer.**
67670 20 20 20 20 20 20 20 6e 65 78 74 43 20 20 20 66         nextC   f
67680 69 72 73 74 20 63 68 61 72 61 63 74 65 72 20 61  irst character a
67690 66 74 65 72 20 74 68 65 20 69 6e 74 65 67 65 72  fter the integer
676a0 0a 2a 2a 20 20 20 20 20 20 20 70 56 61 6c 20 20  .**       pVal  
676b0 20 20 77 68 65 72 65 20 74 6f 20 77 72 69 74 65    where to write
676c0 20 74 68 65 20 69 6e 74 65 67 65 72 73 20 76 61   the integers va
676d0 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65  lue..**.** Conve
676e0 72 73 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20  rsions continue 
676f0 75 6e 74 69 6c 20 6f 6e 65 20 77 69 74 68 20 6e  until one with n
67700 65 78 74 43 3d 3d 30 20 69 73 20 65 6e 63 6f 75  extC==0 is encou
67710 6e 74 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66  ntered..** The f
67720 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
67730 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 75  the number of su
67740 63 63 65 73 73 66 75 6c 20 63 6f 6e 76 65 72 73  ccessful convers
67750 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ions..*/.static 
67760 69 6e 74 20 67 65 74 44 69 67 69 74 73 28 63 6f  int getDigits(co
67770 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c  nst char *zDate,
67780 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74   ...){.  va_list
67790 20 61 70 3b 0a 20 20 69 6e 74 20 76 61 6c 3b 0a   ap;.  int val;.
677a0 20 20 69 6e 74 20 4e 3b 0a 20 20 69 6e 74 20 6d    int N;.  int m
677b0 69 6e 3b 0a 20 20 69 6e 74 20 6d 61 78 3b 0a 20  in;.  int max;. 
677c0 20 69 6e 74 20 6e 65 78 74 43 3b 0a 20 20 69 6e   int nextC;.  in
677d0 74 20 2a 70 56 61 6c 3b 0a 20 20 69 6e 74 20 63  t *pVal;.  int c
677e0 6e 74 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61  nt = 0;.  va_sta
677f0 72 74 28 61 70 2c 20 7a 44 61 74 65 29 3b 0a 20  rt(ap, zDate);. 
67800 20 64 6f 7b 0a 20 20 20 20 4e 20 3d 20 76 61 5f   do{.    N = va_
67810 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20  arg(ap, int);.  
67820 20 20 6d 69 6e 20 3d 20 76 61 5f 61 72 67 28 61    min = va_arg(a
67830 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 6d 61 78  p, int);.    max
67840 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
67850 74 29 3b 0a 20 20 20 20 6e 65 78 74 43 20 3d 20  t);.    nextC = 
67860 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b  va_arg(ap, int);
67870 0a 20 20 20 20 70 56 61 6c 20 3d 20 76 61 5f 61  .    pVal = va_a
67880 72 67 28 61 70 2c 20 69 6e 74 2a 29 3b 0a 20 20  rg(ap, int*);.  
67890 20 20 76 61 6c 20 3d 20 30 3b 0a 20 20 20 20 77    val = 0;.    w
678a0 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20  hile( N-- ){.   
678b0 20 20 20 69 66 28 20 21 69 73 64 69 67 69 74 28     if( !isdigit(
678c0 2a 28 75 38 2a 29 7a 44 61 74 65 29 20 29 7b 0a  *(u8*)zDate) ){.
678d0 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64          goto end
678e0 5f 67 65 74 44 69 67 69 74 73 3b 0a 20 20 20 20  _getDigits;.    
678f0 20 20 7d 0a 20 20 20 20 20 20 76 61 6c 20 3d 20    }.      val = 
67900 76 61 6c 2a 31 30 20 2b 20 2a 7a 44 61 74 65 20  val*10 + *zDate 
67910 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 7a 44 61  - '0';.      zDa
67920 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  te++;.    }.    
67930 69 66 28 20 76 61 6c 3c 6d 69 6e 20 7c 7c 20 76  if( val<min || v
67940 61 6c 3e 6d 61 78 20 7c 7c 20 28 6e 65 78 74 43  al>max || (nextC
67950 21 3d 30 20 26 26 20 6e 65 78 74 43 21 3d 2a 7a  !=0 && nextC!=*z
67960 44 61 74 65 29 20 29 7b 0a 20 20 20 20 20 20 67  Date) ){.      g
67970 6f 74 6f 20 65 6e 64 5f 67 65 74 44 69 67 69 74  oto end_getDigit
67980 73 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 56  s;.    }.    *pV
67990 61 6c 20 3d 20 76 61 6c 3b 0a 20 20 20 20 7a 44  al = val;.    zD
679a0 61 74 65 2b 2b 3b 0a 20 20 20 20 63 6e 74 2b 2b  ate++;.    cnt++
679b0 3b 0a 20 20 7d 77 68 69 6c 65 28 20 6e 65 78 74  ;.  }while( next
679c0 43 20 29 3b 0a 65 6e 64 5f 67 65 74 44 69 67 69  C );.end_getDigi
679d0 74 73 3a 0a 20 20 76 61 5f 65 6e 64 28 61 70 29  ts:.  va_end(ap)
679e0 3b 0a 20 20 72 65 74 75 72 6e 20 63 6e 74 3b 0a  ;.  return cnt;.
679f0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 65  }../*.** Read te
67a00 78 74 20 66 72 6f 6d 20 7a 5b 5d 20 61 6e 64 20  xt from z[] and 
67a10 63 6f 6e 76 65 72 74 20 69 6e 74 6f 20 61 20 66  convert into a f
67a20 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75  loating point nu
67a30 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a 2a  mber.  Return.**
67a40 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64   the number of d
67a50 69 67 69 74 73 20 63 6f 6e 76 65 72 74 65 64 2e  igits converted.
67a60 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 56  .*/.#define getV
67a70 61 6c 75 65 20 73 71 6c 69 74 65 33 41 74 6f 46  alue sqlite3AtoF
67a80 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 61 20  ../*.** Parse a 
67a90 74 69 6d 65 7a 6f 6e 65 20 65 78 74 65 6e 73 69  timezone extensi
67aa0 6f 6e 20 6f 6e 20 74 68 65 20 65 6e 64 20 6f 66  on on the end of
67ab0 20 61 20 64 61 74 65 2d 74 69 6d 65 2e 0a 2a 2a   a date-time..**
67ac0 20 54 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69   The extension i
67ad0 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a  s of the form:.*
67ae0 2a 0a 2a 2a 20 20 20 20 20 20 20 20 28 2b 2f 2d  *.**        (+/-
67af0 29 48 48 3a 4d 4d 0a 2a 2a 0a 2a 2a 20 4f 72 20  )HH:MM.**.** Or 
67b00 74 68 65 20 22 7a 75 6c 75 22 20 6e 6f 74 61 74  the "zulu" notat
67b10 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  ion:.**.**      
67b20 20 20 5a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65    Z.**.** If the
67b30 20 70 61 72 73 65 20 69 73 20 73 75 63 63 65 73   parse is succes
67b40 73 66 75 6c 2c 20 77 72 69 74 65 20 74 68 65 20  sful, write the 
67b50 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6e 75 74 65  number of minute
67b60 73 0a 2a 2a 20 6f 66 20 63 68 61 6e 67 65 20 69  s.** of change i
67b70 6e 20 70 2d 3e 74 7a 20 61 6e 64 20 72 65 74 75  n p->tz and retu
67b80 72 6e 20 30 2e 20 20 49 66 20 61 20 70 61 72 73  rn 0.  If a pars
67b90 65 72 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  er error occurs,
67ba0 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a  .** return non-z
67bb0 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 69 73  ero..**.** A mis
67bc0 73 69 6e 67 20 73 70 65 63 69 66 69 65 72 20 69  sing specifier i
67bd0 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65 64  s not considered
67be0 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74   an error..*/.st
67bf0 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 54 69  atic int parseTi
67c00 6d 65 7a 6f 6e 65 28 63 6f 6e 73 74 20 63 68 61  mezone(const cha
67c10 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69  r *zDate, DateTi
67c20 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 73 67  me *p){.  int sg
67c30 6e 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 48 72  n = 0;.  int nHr
67c40 2c 20 6e 4d 6e 3b 0a 20 20 69 6e 74 20 63 3b 0a  , nMn;.  int c;.
67c50 20 20 77 68 69 6c 65 28 20 69 73 73 70 61 63 65    while( isspace
67c60 28 2a 28 75 38 2a 29 7a 44 61 74 65 29 20 29 7b  (*(u8*)zDate) ){
67c70 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 70 2d   zDate++; }.  p-
67c80 3e 74 7a 20 3d 20 30 3b 0a 20 20 63 20 3d 20 2a  >tz = 0;.  c = *
67c90 7a 44 61 74 65 3b 0a 20 20 69 66 28 20 63 3d 3d  zDate;.  if( c==
67ca0 27 2d 27 20 29 7b 0a 20 20 20 20 73 67 6e 20 3d  '-' ){.    sgn =
67cb0 20 2d 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28   -1;.  }else if(
67cc0 20 63 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 73   c=='+' ){.    s
67cd0 67 6e 20 3d 20 2b 31 3b 0a 20 20 7d 65 6c 73 65  gn = +1;.  }else
67ce0 20 69 66 28 20 63 3d 3d 27 5a 27 20 7c 7c 20 63   if( c=='Z' || c
67cf0 3d 3d 27 7a 27 20 29 7b 0a 20 20 20 20 7a 44 61  =='z' ){.    zDa
67d00 74 65 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 7a  te++;.    goto z
67d10 75 6c 75 5f 74 69 6d 65 3b 0a 20 20 7d 65 6c 73  ulu_time;.  }els
67d20 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 63 21  e{.    return c!
67d30 3d 30 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 65 2b  =0;.  }.  zDate+
67d40 2b 3b 0a 20 20 69 66 28 20 67 65 74 44 69 67 69  +;.  if( getDigi
67d50 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 30 2c 20  ts(zDate, 2, 0, 
67d60 31 34 2c 20 27 3a 27 2c 20 26 6e 48 72 2c 20 32  14, ':', &nHr, 2
67d70 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 6e 4d 6e  , 0, 59, 0, &nMn
67d80 29 21 3d 32 20 29 7b 0a 20 20 20 20 72 65 74 75  )!=2 ){.    retu
67d90 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 61 74  rn 1;.  }.  zDat
67da0 65 20 2b 3d 20 35 3b 0a 20 20 70 2d 3e 74 7a 20  e += 5;.  p->tz 
67db0 3d 20 73 67 6e 2a 28 6e 4d 6e 20 2b 20 6e 48 72  = sgn*(nMn + nHr
67dc0 2a 36 30 29 3b 0a 7a 75 6c 75 5f 74 69 6d 65 3a  *60);.zulu_time:
67dd0 0a 20 20 77 68 69 6c 65 28 20 69 73 73 70 61 63  .  while( isspac
67de0 65 28 2a 28 75 38 2a 29 7a 44 61 74 65 29 20 29  e(*(u8*)zDate) )
67df0 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 72  { zDate++; }.  r
67e00 65 74 75 72 6e 20 2a 7a 44 61 74 65 21 3d 30 3b  eturn *zDate!=0;
67e10 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20  .}../*.** Parse 
67e20 74 69 6d 65 73 20 6f 66 20 74 68 65 20 66 6f 72  times of the for
67e30 6d 20 48 48 3a 4d 4d 20 6f 72 20 48 48 3a 4d 4d  m HH:MM or HH:MM
67e40 3a 53 53 20 6f 72 20 48 48 3a 4d 4d 3a 53 53 2e  :SS or HH:MM:SS.
67e50 46 46 46 46 2e 0a 2a 2a 20 54 68 65 20 48 48 2c  FFFF..** The HH,
67e60 20 4d 4d 2c 20 61 6e 64 20 53 53 20 6d 75 73 74   MM, and SS must
67e70 20 65 61 63 68 20 62 65 20 65 78 61 63 74 6c 79   each be exactly
67e80 20 32 20 64 69 67 69 74 73 2e 20 20 54 68 65 0a   2 digits.  The.
67e90 2a 2a 20 66 72 61 63 74 69 6f 6e 61 6c 20 73 65  ** fractional se
67ea0 63 6f 6e 64 73 20 46 46 46 46 20 63 61 6e 20 62  conds FFFF can b
67eb0 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 64 69  e one or more di
67ec0 67 69 74 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  gits..**.** Retu
67ed0 72 6e 20 31 20 69 66 20 74 68 65 72 65 20 69 73  rn 1 if there is
67ee0 20 61 20 70 61 72 73 69 6e 67 20 65 72 72 6f 72   a parsing error
67ef0 20 61 6e 64 20 30 20 6f 6e 20 73 75 63 63 65 73   and 0 on succes
67f00 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
67f10 20 70 61 72 73 65 48 68 4d 6d 53 73 28 63 6f 6e   parseHhMmSs(con
67f20 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20  st char *zDate, 
67f30 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20  DateTime *p){.  
67f40 69 6e 74 20 68 2c 20 6d 2c 20 73 3b 0a 20 20 64  int h, m, s;.  d
67f50 6f 75 62 6c 65 20 6d 73 20 3d 20 30 2e 30 3b 0a  ouble ms = 0.0;.
67f60 20 20 69 66 28 20 67 65 74 44 69 67 69 74 73 28    if( getDigits(
67f70 7a 44 61 74 65 2c 20 32 2c 20 30 2c 20 32 34 2c  zDate, 2, 0, 24,
67f80 20 27 3a 27 2c 20 26 68 2c 20 32 2c 20 30 2c 20   ':', &h, 2, 0, 
67f90 35 39 2c 20 30 2c 20 26 6d 29 21 3d 32 20 29 7b  59, 0, &m)!=2 ){
67fa0 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
67fb0 20 7d 0a 20 20 7a 44 61 74 65 20 2b 3d 20 35 3b   }.  zDate += 5;
67fc0 0a 20 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d 27  .  if( *zDate=='
67fd0 3a 27 20 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b  :' ){.    zDate+
67fe0 2b 3b 0a 20 20 20 20 69 66 28 20 67 65 74 44 69  +;.    if( getDi
67ff0 67 69 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 30  gits(zDate, 2, 0
68000 2c 20 35 39 2c 20 30 2c 20 26 73 29 21 3d 31 20  , 59, 0, &s)!=1 
68010 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
68020 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 44 61  1;.    }.    zDa
68030 74 65 20 2b 3d 20 32 3b 0a 20 20 20 20 69 66 28  te += 2;.    if(
68040 20 2a 7a 44 61 74 65 3d 3d 27 2e 27 20 26 26 20   *zDate=='.' && 
68050 69 73 64 69 67 69 74 28 28 75 38 29 7a 44 61 74  isdigit((u8)zDat
68060 65 5b 31 5d 29 20 29 7b 0a 20 20 20 20 20 20 64  e[1]) ){.      d
68070 6f 75 62 6c 65 20 72 53 63 61 6c 65 20 3d 20 31  ouble rScale = 1
68080 2e 30 3b 0a 20 20 20 20 20 20 7a 44 61 74 65 2b  .0;.      zDate+
68090 2b 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  +;.      while( 
680a0 69 73 64 69 67 69 74 28 2a 28 75 38 2a 29 7a 44  isdigit(*(u8*)zD
680b0 61 74 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ate) ){.        
680c0 6d 73 20 3d 20 6d 73 2a 31 30 2e 30 20 2b 20 2a  ms = ms*10.0 + *
680d0 7a 44 61 74 65 20 2d 20 27 30 27 3b 0a 20 20 20  zDate - '0';.   
680e0 20 20 20 20 20 72 53 63 61 6c 65 20 2a 3d 20 31       rScale *= 1
680f0 30 2e 30 3b 0a 20 20 20 20 20 20 20 20 7a 44 61  0.0;.        zDa
68100 74 65 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  te++;.      }.  
68110 20 20 20 20 6d 73 20 2f 3d 20 72 53 63 61 6c 65      ms /= rScale
68120 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
68130 0a 20 20 20 20 73 20 3d 20 30 3b 0a 20 20 7d 0a  .    s = 0;.  }.
68140 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30    p->validJD = 0
68150 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20  ;.  p->validHMS 
68160 3d 20 31 3b 0a 20 20 70 2d 3e 68 20 3d 20 68 3b  = 1;.  p->h = h;
68170 0a 20 20 70 2d 3e 6d 20 3d 20 6d 3b 0a 20 20 70  .  p->m = m;.  p
68180 2d 3e 73 20 3d 20 73 20 2b 20 6d 73 3b 0a 20 20  ->s = s + ms;.  
68190 69 66 28 20 70 61 72 73 65 54 69 6d 65 7a 6f 6e  if( parseTimezon
681a0 65 28 7a 44 61 74 65 2c 20 70 29 20 29 20 72 65  e(zDate, p) ) re
681b0 74 75 72 6e 20 31 3b 0a 20 20 70 2d 3e 76 61 6c  turn 1;.  p->val
681c0 69 64 54 5a 20 3d 20 70 2d 3e 74 7a 21 3d 30 3b  idTZ = p->tz!=0;
681d0 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
681e0 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 66 72  /*.** Convert fr
681f0 6f 6d 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48  om YYYY-MM-DD HH
68200 3a 4d 4d 3a 53 53 20 74 6f 20 6a 75 6c 69 61 6e  :MM:SS to julian
68210 20 64 61 79 2e 20 20 57 65 20 61 6c 77 61 79 73   day.  We always
68220 20 61 73 73 75 6d 65 0a 2a 2a 20 74 68 61 74 20   assume.** that 
68230 74 68 65 20 59 59 59 59 2d 4d 4d 2d 44 44 20 69  the YYYY-MM-DD i
68240 73 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74  s according to t
68250 68 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61 6c  he Gregorian cal
68260 65 6e 64 61 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 66  endar..**.** Ref
68270 65 72 65 6e 63 65 3a 20 20 4d 65 65 75 73 20 70  erence:  Meeus p
68280 61 67 65 20 36 31 0a 2a 2f 0a 73 74 61 74 69 63  age 61.*/.static
68290 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 4a 44 28   void computeJD(
682a0 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20  DateTime *p){.  
682b0 69 6e 74 20 59 2c 20 4d 2c 20 44 2c 20 41 2c 20  int Y, M, D, A, 
682c0 42 2c 20 58 31 2c 20 58 32 3b 0a 0a 20 20 69 66  B, X1, X2;..  if
682d0 28 20 70 2d 3e 76 61 6c 69 64 4a 44 20 29 20 72  ( p->validJD ) r
682e0 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e  eturn;.  if( p->
682f0 76 61 6c 69 64 59 4d 44 20 29 7b 0a 20 20 20 20  validYMD ){.    
68300 59 20 3d 20 70 2d 3e 59 3b 0a 20 20 20 20 4d 20  Y = p->Y;.    M 
68310 3d 20 70 2d 3e 4d 3b 0a 20 20 20 20 44 20 3d 20  = p->M;.    D = 
68320 70 2d 3e 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  p->D;.  }else{. 
68330 20 20 20 59 20 3d 20 32 30 30 30 3b 20 20 2f 2a     Y = 2000;  /*
68340 20 49 66 20 6e 6f 20 59 4d 44 20 73 70 65 63 69   If no YMD speci
68350 66 69 65 64 2c 20 61 73 73 75 6d 65 20 32 30 30  fied, assume 200
68360 30 2d 4a 61 6e 2d 30 31 20 2a 2f 0a 20 20 20 20  0-Jan-01 */.    
68370 4d 20 3d 20 31 3b 0a 20 20 20 20 44 20 3d 20 31  M = 1;.    D = 1
68380 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4d 3c 3d 32  ;.  }.  if( M<=2
68390 20 29 7b 0a 20 20 20 20 59 2d 2d 3b 0a 20 20 20   ){.    Y--;.   
683a0 20 4d 20 2b 3d 20 31 32 3b 0a 20 20 7d 0a 20 20   M += 12;.  }.  
683b0 41 20 3d 20 59 2f 31 30 30 3b 0a 20 20 42 20 3d  A = Y/100;.  B =
683c0 20 32 20 2d 20 41 20 2b 20 28 41 2f 34 29 3b 0a   2 - A + (A/4);.
683d0 20 20 58 31 20 3d 20 33 36 35 2e 32 35 2a 28 59    X1 = 365.25*(Y
683e0 2b 34 37 31 36 29 3b 0a 20 20 58 32 20 3d 20 33  +4716);.  X2 = 3
683f0 30 2e 36 30 30 31 2a 28 4d 2b 31 29 3b 0a 20 20  0.6001*(M+1);.  
68400 70 2d 3e 72 4a 44 20 3d 20 58 31 20 2b 20 58 32  p->rJD = X1 + X2
68410 20 2b 20 44 20 2b 20 42 20 2d 20 31 35 32 34 2e   + D + B - 1524.
68420 35 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20  5;.  p->validJD 
68430 3d 20 31 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61  = 1;.  if( p->va
68440 6c 69 64 48 4d 53 20 29 7b 0a 20 20 20 20 70 2d  lidHMS ){.    p-
68450 3e 72 4a 44 20 2b 3d 20 28 70 2d 3e 68 2a 33 36  >rJD += (p->h*36
68460 30 30 2e 30 20 2b 20 70 2d 3e 6d 2a 36 30 2e 30  00.0 + p->m*60.0
68470 20 2b 20 70 2d 3e 73 29 2f 38 36 34 30 30 2e 30   + p->s)/86400.0
68480 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 76 61 6c  ;.    if( p->val
68490 69 64 54 5a 20 29 7b 0a 20 20 20 20 20 20 70 2d  idTZ ){.      p-
684a0 3e 72 4a 44 20 2d 3d 20 70 2d 3e 74 7a 2a 36 30  >rJD -= p->tz*60
684b0 2f 38 36 34 30 30 2e 30 3b 0a 20 20 20 20 20 20  /86400.0;.      
684c0 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 30 3b  p->validYMD = 0;
684d0 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 48  .      p->validH
684e0 4d 53 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d  MS = 0;.      p-
684f0 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20  >validTZ = 0;.  
68500 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
68510 20 50 61 72 73 65 20 64 61 74 65 73 20 6f 66 20   Parse dates of 
68520 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20  the form.**.**  
68530 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48     YYYY-MM-DD HH
68540 3a 4d 4d 3a 53 53 2e 46 46 46 0a 2a 2a 20 20 20  :MM:SS.FFF.**   
68550 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a    YYYY-MM-DD HH:
68560 4d 4d 3a 53 53 0a 2a 2a 20 20 20 20 20 59 59 59  MM:SS.**     YYY
68570 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 0a 2a 2a  Y-MM-DD HH:MM.**
68580 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 0a       YYYY-MM-DD.
68590 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20  **.** Write the 
685a0 72 65 73 75 6c 74 20 69 6e 74 6f 20 74 68 65 20  result into the 
685b0 44 61 74 65 54 69 6d 65 20 73 74 72 75 63 74 75  DateTime structu
685c0 72 65 20 61 6e 64 20 72 65 74 75 72 6e 20 30 0a  re and return 0.
685d0 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e  ** on success an
685e0 64 20 31 20 69 66 20 74 68 65 20 69 6e 70 75 74  d 1 if the input
685f0 20 73 74 72 69 6e 67 20 69 73 20 6e 6f 74 20 61   string is not a
68600 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20   well-formed.** 
68610 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  date..*/.static 
68620 69 6e 74 20 70 61 72 73 65 59 79 79 79 4d 6d 44  int parseYyyyMmD
68630 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44  d(const char *zD
68640 61 74 65 2c 20 44 61 74 65 54 69 6d 65 20 2a 70  ate, DateTime *p
68650 29 7b 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44  ){.  int Y, M, D
68660 2c 20 6e 65 67 3b 0a 0a 20 20 69 66 28 20 7a 44  , neg;..  if( zD
68670 61 74 65 5b 30 5d 3d 3d 27 2d 27 20 29 7b 0a 20  ate[0]=='-' ){. 
68680 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20     zDate++;.    
68690 6e 65 67 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  neg = 1;.  }else
686a0 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20  {.    neg = 0;. 
686b0 20 7d 0a 20 20 69 66 28 20 67 65 74 44 69 67 69   }.  if( getDigi
686c0 74 73 28 7a 44 61 74 65 2c 34 2c 30 2c 39 39 39  ts(zDate,4,0,999
686d0 39 2c 27 2d 27 2c 26 59 2c 32 2c 31 2c 31 32 2c  9,'-',&Y,2,1,12,
686e0 27 2d 27 2c 26 4d 2c 32 2c 31 2c 33 31 2c 30 2c  '-',&M,2,1,31,0,
686f0 26 44 29 21 3d 33 20 29 7b 0a 20 20 20 20 72 65  &D)!=3 ){.    re
68700 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44  turn 1;.  }.  zD
68710 61 74 65 20 2b 3d 20 31 30 3b 0a 20 20 77 68 69  ate += 10;.  whi
68720 6c 65 28 20 69 73 73 70 61 63 65 28 2a 28 75 38  le( isspace(*(u8
68730 2a 29 7a 44 61 74 65 29 20 7c 7c 20 27 54 27 3d  *)zDate) || 'T'=
68740 3d 2a 28 75 38 2a 29 7a 44 61 74 65 20 29 7b 20  =*(u8*)zDate ){ 
68750 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 69 66 28  zDate++; }.  if(
68760 20 70 61 72 73 65 48 68 4d 6d 53 73 28 7a 44 61   parseHhMmSs(zDa
68770 74 65 2c 20 70 29 3d 3d 30 20 29 7b 0a 20 20 20  te, p)==0 ){.   
68780 20 2f 2a 20 57 65 20 67 6f 74 20 74 68 65 20 74   /* We got the t
68790 69 6d 65 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69  ime */.  }else i
687a0 66 28 20 2a 7a 44 61 74 65 3d 3d 30 20 29 7b 0a  f( *zDate==0 ){.
687b0 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20      p->validHMS 
687c0 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 0;.  }else{.  
687d0 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
687e0 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30    p->validJD = 0
687f0 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20  ;.  p->validYMD 
68800 3d 20 31 3b 0a 20 20 70 2d 3e 59 20 3d 20 6e 65  = 1;.  p->Y = ne
68810 67 20 3f 20 2d 59 20 3a 20 59 3b 0a 20 20 70 2d  g ? -Y : Y;.  p-
68820 3e 4d 20 3d 20 4d 3b 0a 20 20 70 2d 3e 44 20 3d  >M = M;.  p->D =
68830 20 44 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c   D;.  if( p->val
68840 69 64 54 5a 20 29 7b 0a 20 20 20 20 63 6f 6d 70  idTZ ){.    comp
68850 75 74 65 4a 44 28 70 29 3b 0a 20 20 7d 0a 20 20  uteJD(p);.  }.  
68860 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
68870 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 70 61  ** Attempt to pa
68880 72 73 65 20 74 68 65 20 67 69 76 65 6e 20 73 74  rse the given st
68890 72 69 6e 67 20 69 6e 74 6f 20 61 20 4a 75 6c 69  ring into a Juli
688a0 61 6e 20 44 61 79 20 4e 75 6d 62 65 72 2e 20 20  an Day Number.  
688b0 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 6e 75  Return.** the nu
688c0 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 0a  mber of errors..
688d0 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  **.** The follow
688e0 69 6e 67 20 61 72 65 20 61 63 63 65 70 74 61 62  ing are acceptab
688f0 6c 65 20 66 6f 72 6d 73 20 66 6f 72 20 74 68 65  le forms for the
68900 20 69 6e 70 75 74 20 73 74 72 69 6e 67 3a 0a 2a   input string:.*
68910 2a 0a 2a 2a 20 20 20 20 20 20 59 59 59 59 2d 4d  *.**      YYYY-M
68920 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 2e 46 46  M-DD HH:MM:SS.FF
68930 46 20 20 2b 2f 2d 48 48 3a 4d 4d 0a 2a 2a 20 20  F  +/-HH:MM.**  
68940 20 20 20 20 44 44 44 44 2e 44 44 20 0a 2a 2a 20      DDDD.DD .** 
68950 20 20 20 20 20 6e 6f 77 0a 2a 2a 0a 2a 2a 20 49       now.**.** I
68960 6e 20 74 68 65 20 66 69 72 73 74 20 66 6f 72 6d  n the first form
68970 2c 20 74 68 65 20 2b 2f 2d 48 48 3a 4d 4d 20 69  , the +/-HH:MM i
68980 73 20 61 6c 77 61 79 73 20 6f 70 74 69 6f 6e 61  s always optiona
68990 6c 2e 20 20 54 68 65 20 66 72 61 63 74 69 6f 6e  l.  The fraction
689a0 61 6c 0a 2a 2a 20 73 65 63 6f 6e 64 73 20 65 78  al.** seconds ex
689b0 74 65 6e 73 69 6f 6e 20 28 74 68 65 20 22 2e 46  tension (the ".F
689c0 46 46 22 29 20 69 73 20 6f 70 74 69 6f 6e 61 6c  FF") is optional
689d0 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 73 20 70  .  The seconds p
689e0 6f 72 74 69 6f 6e 0a 2a 2a 20 28 22 3a 53 53 2e  ortion.** (":SS.
689f0 46 46 46 22 29 20 69 73 20 6f 70 74 69 6f 6e 2e  FFF") is option.
68a00 20 20 54 68 65 20 79 65 61 72 20 61 6e 64 20 64    The year and d
68a10 61 74 65 20 63 61 6e 20 62 65 20 6f 6d 69 74 74  ate can be omitt
68a20 65 64 20 61 73 20 6c 6f 6e 67 0a 2a 2a 20 61 73  ed as long.** as
68a30 20 74 68 65 72 65 20 69 73 20 61 20 74 69 6d 65   there is a time
68a40 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20 74 69   string.  The ti
68a50 6d 65 20 73 74 72 69 6e 67 20 63 61 6e 20 62 65  me string can be
68a60 20 6f 6d 69 74 74 65 64 20 61 73 20 6c 6f 6e 67   omitted as long
68a70 0a 2a 2a 20 61 73 20 74 68 65 72 65 20 69 73 20  .** as there is 
68a80 61 20 79 65 61 72 20 61 6e 64 20 64 61 74 65 2e  a year and date.
68a90 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
68aa0 61 72 73 65 44 61 74 65 4f 72 54 69 6d 65 28 0a  arseDateOrTime(.
68ab0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
68ac0 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 63  t *context, .  c
68ad0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65  onst char *zDate
68ae0 2c 20 0a 20 20 44 61 74 65 54 69 6d 65 20 2a 70  , .  DateTime *p
68af0 0a 29 7b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20  .){.  memset(p, 
68b00 30 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a  0, sizeof(*p));.
68b10 20 20 69 66 28 20 70 61 72 73 65 59 79 79 79 4d    if( parseYyyyM
68b20 6d 44 64 28 7a 44 61 74 65 2c 70 29 3d 3d 30 20  mDd(zDate,p)==0 
68b30 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
68b40 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 61 72  .  }else if( par
68b50 73 65 48 68 4d 6d 53 73 28 7a 44 61 74 65 2c 20  seHhMmSs(zDate, 
68b60 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  p)==0 ){.    ret
68b70 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69  urn 0;.  }else i
68b80 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
68b90 70 28 7a 44 61 74 65 2c 22 6e 6f 77 22 29 3d 3d  p(zDate,"now")==
68ba0 30 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 72  0){.    double r
68bb0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64  ;.    sqlite3 *d
68bc0 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  b = sqlite3_cont
68bd0 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
68be0 6e 74 65 78 74 29 3b 0a 20 20 20 20 73 71 6c 69  ntext);.    sqli
68bf0 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65  te3OsCurrentTime
68c00 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 29 3b 0a  (db->pVfs, &r);.
68c10 20 20 20 20 70 2d 3e 72 4a 44 20 3d 20 72 3b 0a      p->rJD = r;.
68c20 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d      p->validJD =
68c30 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   1;.    return 0
68c40 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71  ;.  }else if( sq
68c50 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 7a 44  lite3IsNumber(zD
68c60 61 74 65 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55  ate, 0, SQLITE_U
68c70 54 46 38 29 20 29 7b 0a 20 20 20 20 67 65 74 56  TF8) ){.    getV
68c80 61 6c 75 65 28 7a 44 61 74 65 2c 20 26 70 2d 3e  alue(zDate, &p->
68c90 72 4a 44 29 3b 0a 20 20 20 20 70 2d 3e 76 61 6c  rJD);.    p->val
68ca0 69 64 4a 44 20 3d 20 31 3b 0a 20 20 20 20 72 65  idJD = 1;.    re
68cb0 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65  turn 0;.  }.  re
68cc0 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 1;.}../*.**
68cd0 20 43 6f 6d 70 75 74 65 20 74 68 65 20 59 65 61   Compute the Yea
68ce0 72 2c 20 4d 6f 6e 74 68 2c 20 61 6e 64 20 44 61  r, Month, and Da
68cf0 79 20 66 72 6f 6d 20 74 68 65 20 6a 75 6c 69 61  y from the julia
68d00 6e 20 64 61 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f  n day number..*/
68d10 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d  .static void com
68d20 70 75 74 65 59 4d 44 28 44 61 74 65 54 69 6d 65  puteYMD(DateTime
68d30 20 2a 70 29 7b 0a 20 20 69 6e 74 20 5a 2c 20 41   *p){.  int Z, A
68d40 2c 20 42 2c 20 43 2c 20 44 2c 20 45 2c 20 58 31  , B, C, D, E, X1
68d50 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64  ;.  if( p->valid
68d60 59 4d 44 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  YMD ) return;.  
68d70 69 66 28 20 21 70 2d 3e 76 61 6c 69 64 4a 44 20  if( !p->validJD 
68d80 29 7b 0a 20 20 20 20 70 2d 3e 59 20 3d 20 32 30  ){.    p->Y = 20
68d90 30 30 3b 0a 20 20 20 20 70 2d 3e 4d 20 3d 20 31  00;.    p->M = 1
68da0 3b 0a 20 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a  ;.    p->D = 1;.
68db0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 5a 20 3d    }else{.    Z =
68dc0 20 70 2d 3e 72 4a 44 20 2b 20 30 2e 35 3b 0a 20   p->rJD + 0.5;. 
68dd0 20 20 20 41 20 3d 20 28 5a 20 2d 20 31 38 36 37     A = (Z - 1867
68de0 32 31 36 2e 32 35 29 2f 33 36 35 32 34 2e 32 35  216.25)/36524.25
68df0 3b 0a 20 20 20 20 41 20 3d 20 5a 20 2b 20 31 20  ;.    A = Z + 1 
68e00 2b 20 41 20 2d 20 28 41 2f 34 29 3b 0a 20 20 20  + A - (A/4);.   
68e10 20 42 20 3d 20 41 20 2b 20 31 35 32 34 3b 0a 20   B = A + 1524;. 
68e20 20 20 20 43 20 3d 20 28 42 20 2d 20 31 32 32 2e     C = (B - 122.
68e30 31 29 2f 33 36 35 2e 32 35 3b 0a 20 20 20 20 44  1)/365.25;.    D
68e40 20 3d 20 33 36 35 2e 32 35 2a 43 3b 0a 20 20 20   = 365.25*C;.   
68e50 20 45 20 3d 20 28 42 2d 44 29 2f 33 30 2e 36 30   E = (B-D)/30.60
68e60 30 31 3b 0a 20 20 20 20 58 31 20 3d 20 33 30 2e  01;.    X1 = 30.
68e70 36 30 30 31 2a 45 3b 0a 20 20 20 20 70 2d 3e 44  6001*E;.    p->D
68e80 20 3d 20 42 20 2d 20 44 20 2d 20 58 31 3b 0a 20   = B - D - X1;. 
68e90 20 20 20 70 2d 3e 4d 20 3d 20 45 3c 31 34 20 3f     p->M = E<14 ?
68ea0 20 45 2d 31 20 3a 20 45 2d 31 33 3b 0a 20 20 20   E-1 : E-13;.   
68eb0 20 70 2d 3e 59 20 3d 20 70 2d 3e 4d 3e 32 20 3f   p->Y = p->M>2 ?
68ec0 20 43 20 2d 20 34 37 31 36 20 3a 20 43 20 2d 20   C - 4716 : C - 
68ed0 34 37 31 35 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76  4715;.  }.  p->v
68ee0 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 7d 0a 0a  alidYMD = 1;.}..
68ef0 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68  /*.** Compute th
68f00 65 20 48 6f 75 72 2c 20 4d 69 6e 75 74 65 2c 20  e Hour, Minute, 
68f10 61 6e 64 20 53 65 63 6f 6e 64 73 20 66 72 6f 6d  and Seconds from
68f20 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20   the julian day 
68f30 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  number..*/.stati
68f40 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 48 4d  c void computeHM
68f50 53 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a  S(DateTime *p){.
68f60 20 20 69 6e 74 20 5a 2c 20 73 3b 0a 20 20 69 66    int Z, s;.  if
68f70 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 29 20  ( p->validHMS ) 
68f80 72 65 74 75 72 6e 3b 0a 20 20 63 6f 6d 70 75 74  return;.  comput
68f90 65 4a 44 28 70 29 3b 0a 20 20 5a 20 3d 20 70 2d  eJD(p);.  Z = p-
68fa0 3e 72 4a 44 20 2b 20 30 2e 35 3b 0a 20 20 73 20  >rJD + 0.5;.  s 
68fb0 3d 20 28 70 2d 3e 72 4a 44 20 2b 20 30 2e 35 20  = (p->rJD + 0.5 
68fc0 2d 20 5a 29 2a 38 36 34 30 30 30 30 30 2e 30 20  - Z)*86400000.0 
68fd0 2b 20 30 2e 35 3b 0a 20 20 70 2d 3e 73 20 3d 20  + 0.5;.  p->s = 
68fe0 30 2e 30 30 31 2a 73 3b 0a 20 20 73 20 3d 20 70  0.001*s;.  s = p
68ff0 2d 3e 73 3b 0a 20 20 70 2d 3e 73 20 2d 3d 20 73  ->s;.  p->s -= s
69000 3b 0a 20 20 70 2d 3e 68 20 3d 20 73 2f 33 36 30  ;.  p->h = s/360
69010 30 3b 0a 20 20 73 20 2d 3d 20 70 2d 3e 68 2a 33  0;.  s -= p->h*3
69020 36 30 30 3b 0a 20 20 70 2d 3e 6d 20 3d 20 73 2f  600;.  p->m = s/
69030 36 30 3b 0a 20 20 70 2d 3e 73 20 2b 3d 20 73 20  60;.  p->s += s 
69040 2d 20 70 2d 3e 6d 2a 36 30 3b 0a 20 20 70 2d 3e  - p->m*60;.  p->
69050 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 7d 0a  validHMS = 1;.}.
69060 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 62  ./*.** Compute b
69070 6f 74 68 20 59 4d 44 20 61 6e 64 20 48 4d 53 0a  oth YMD and HMS.
69080 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
69090 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 44 61  omputeYMD_HMS(Da
690a0 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 63 6f  teTime *p){.  co
690b0 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20 20 63  mputeYMD(p);.  c
690c0 6f 6d 70 75 74 65 48 4d 53 28 70 29 3b 0a 7d 0a  omputeHMS(p);.}.
690d0 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65  ./*.** Clear the
690e0 20 59 4d 44 20 61 6e 64 20 48 4d 53 20 61 6e 64   YMD and HMS and
690f0 20 74 68 65 20 54 5a 0a 2a 2f 0a 73 74 61 74 69   the TZ.*/.stati
69100 63 20 76 6f 69 64 20 63 6c 65 61 72 59 4d 44 5f  c void clearYMD_
69110 48 4d 53 5f 54 5a 28 44 61 74 65 54 69 6d 65 20  HMS_TZ(DateTime 
69120 2a 70 29 7b 0a 20 20 70 2d 3e 76 61 6c 69 64 59  *p){.  p->validY
69130 4d 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c  MD = 0;.  p->val
69140 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 70 2d 3e  idHMS = 0;.  p->
69150 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 7d 0a 0a  validTZ = 0;.}..
69160 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68  /*.** Compute th
69170 65 20 64 69 66 66 65 72 65 6e 63 65 20 28 69 6e  e difference (in
69180 20 64 61 79 73 29 20 62 65 74 77 65 65 6e 20 6c   days) between l
69190 6f 63 61 6c 74 69 6d 65 20 61 6e 64 20 55 54 43  ocaltime and UTC
691a0 20 28 61 2e 6b 2e 61 2e 20 47 4d 54 29 0a 2a 2a   (a.k.a. GMT).**
691b0 20 66 6f 72 20 74 68 65 20 74 69 6d 65 20 76 61   for the time va
691c0 6c 75 65 20 70 20 77 68 65 72 65 20 70 20 69 73  lue p where p is
691d0 20 69 6e 20 55 54 43 2e 0a 2a 2f 0a 73 74 61 74   in UTC..*/.stat
691e0 69 63 20 64 6f 75 62 6c 65 20 6c 6f 63 61 6c 74  ic double localt
691f0 69 6d 65 4f 66 66 73 65 74 28 44 61 74 65 54 69  imeOffset(DateTi
69200 6d 65 20 2a 70 29 7b 0a 20 20 44 61 74 65 54 69  me *p){.  DateTi
69210 6d 65 20 78 2c 20 79 3b 0a 20 20 74 69 6d 65 5f  me x, y;.  time_
69220 74 20 74 3b 0a 20 20 78 20 3d 20 2a 70 3b 0a 20  t t;.  x = *p;. 
69230 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28   computeYMD_HMS(
69240 26 78 29 3b 0a 20 20 69 66 28 20 78 2e 59 3c 31  &x);.  if( x.Y<1
69250 39 37 31 20 7c 7c 20 78 2e 59 3e 3d 32 30 33 38  971 || x.Y>=2038
69260 20 29 7b 0a 20 20 20 20 78 2e 59 20 3d 20 32 30   ){.    x.Y = 20
69270 30 30 3b 0a 20 20 20 20 78 2e 4d 20 3d 20 31 3b  00;.    x.M = 1;
69280 0a 20 20 20 20 78 2e 44 20 3d 20 31 3b 0a 20 20  .    x.D = 1;.  
69290 20 20 78 2e 68 20 3d 20 30 3b 0a 20 20 20 20 78    x.h = 0;.    x
692a0 2e 6d 20 3d 20 30 3b 0a 20 20 20 20 78 2e 73 20  .m = 0;.    x.s 
692b0 3d 20 30 2e 30 3b 0a 20 20 7d 20 65 6c 73 65 20  = 0.0;.  } else 
692c0 7b 0a 20 20 20 20 69 6e 74 20 73 20 3d 20 78 2e  {.    int s = x.
692d0 73 20 2b 20 30 2e 35 3b 0a 20 20 20 20 78 2e 73  s + 0.5;.    x.s
692e0 20 3d 20 73 3b 0a 20 20 7d 0a 20 20 78 2e 74 7a   = s;.  }.  x.tz
692f0 20 3d 20 30 3b 0a 20 20 78 2e 76 61 6c 69 64 4a   = 0;.  x.validJ
69300 44 20 3d 20 30 3b 0a 20 20 63 6f 6d 70 75 74 65  D = 0;.  compute
69310 4a 44 28 26 78 29 3b 0a 20 20 74 20 3d 20 28 78  JD(&x);.  t = (x
69320 2e 72 4a 44 2d 32 34 34 30 35 38 37 2e 35 29 2a  .rJD-2440587.5)*
69330 38 36 34 30 30 2e 30 20 2b 20 30 2e 35 3b 0a 23  86400.0 + 0.5;.#
69340 69 66 64 65 66 20 48 41 56 45 5f 4c 4f 43 41 4c  ifdef HAVE_LOCAL
69350 54 49 4d 45 5f 52 0a 20 20 7b 0a 20 20 20 20 73  TIME_R.  {.    s
69360 74 72 75 63 74 20 74 6d 20 73 4c 6f 63 61 6c 3b  truct tm sLocal;
69370 0a 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65 5f 72  .    localtime_r
69380 28 26 74 2c 20 26 73 4c 6f 63 61 6c 29 3b 0a 20  (&t, &sLocal);. 
69390 20 20 20 79 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e     y.Y = sLocal.
693a0 74 6d 5f 79 65 61 72 20 2b 20 31 39 30 30 3b 0a  tm_year + 1900;.
693b0 20 20 20 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c      y.M = sLocal
693c0 2e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20  .tm_mon + 1;.   
693d0 20 79 2e 44 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d   y.D = sLocal.tm
693e0 5f 6d 64 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d  _mday;.    y.h =
693f0 20 73 4c 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b   sLocal.tm_hour;
69400 0a 20 20 20 20 79 2e 6d 20 3d 20 73 4c 6f 63 61  .    y.m = sLoca
69410 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e  l.tm_min;.    y.
69420 73 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65  s = sLocal.tm_se
69430 63 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 7b  c;.  }.#else.  {
69440 0a 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 2a  .    struct tm *
69450 70 54 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  pTm;.    sqlite3
69460 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c  _mutex_enter(sql
69470 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
69480 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
69490 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20  ATIC_MASTER));. 
694a0 20 20 20 70 54 6d 20 3d 20 6c 6f 63 61 6c 74 69     pTm = localti
694b0 6d 65 28 26 74 29 3b 0a 20 20 20 20 79 2e 59 20  me(&t);.    y.Y 
694c0 3d 20 70 54 6d 2d 3e 74 6d 5f 79 65 61 72 20 2b  = pTm->tm_year +
694d0 20 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d 20 3d   1900;.    y.M =
694e0 20 70 54 6d 2d 3e 74 6d 5f 6d 6f 6e 20 2b 20 31   pTm->tm_mon + 1
694f0 3b 0a 20 20 20 20 79 2e 44 20 3d 20 70 54 6d 2d  ;.    y.D = pTm-
69500 3e 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 20 79 2e  >tm_mday;.    y.
69510 68 20 3d 20 70 54 6d 2d 3e 74 6d 5f 68 6f 75 72  h = pTm->tm_hour
69520 3b 0a 20 20 20 20 79 2e 6d 20 3d 20 70 54 6d 2d  ;.    y.m = pTm-
69530 3e 74 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e 73  >tm_min;.    y.s
69540 20 3d 20 70 54 6d 2d 3e 74 6d 5f 73 65 63 3b 0a   = pTm->tm_sec;.
69550 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
69560 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 5f  x_leave(sqlite3_
69570 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49  mutex_alloc(SQLI
69580 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
69590 4d 41 53 54 45 52 29 29 3b 0a 20 20 7d 0a 23 65  MASTER));.  }.#e
695a0 6e 64 69 66 0a 20 20 79 2e 76 61 6c 69 64 59 4d  ndif.  y.validYM
695b0 44 20 3d 20 31 3b 0a 20 20 79 2e 76 61 6c 69 64  D = 1;.  y.valid
695c0 48 4d 53 20 3d 20 31 3b 0a 20 20 79 2e 76 61 6c  HMS = 1;.  y.val
695d0 69 64 4a 44 20 3d 20 30 3b 0a 20 20 79 2e 76 61  idJD = 0;.  y.va
695e0 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 63 6f 6d  lidTZ = 0;.  com
695f0 70 75 74 65 4a 44 28 26 79 29 3b 0a 20 20 72 65  puteJD(&y);.  re
69600 74 75 72 6e 20 79 2e 72 4a 44 20 2d 20 78 2e 72  turn y.rJD - x.r
69610 4a 44 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f  JD;.}../*.** Pro
69620 63 65 73 73 20 61 20 6d 6f 64 69 66 69 65 72 20  cess a modifier 
69630 74 6f 20 61 20 64 61 74 65 2d 74 69 6d 65 20 73  to a date-time s
69640 74 61 6d 70 2e 20 20 54 68 65 20 6d 6f 64 69 66  tamp.  The modif
69650 69 65 72 73 20 61 72 65 0a 2a 2a 20 61 73 20 66  iers are.** as f
69660 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ollows:.**.**   
69670 20 20 4e 4e 4e 20 64 61 79 73 0a 2a 2a 20 20 20    NNN days.**   
69680 20 20 4e 4e 4e 20 68 6f 75 72 73 0a 2a 2a 20 20    NNN hours.**  
69690 20 20 20 4e 4e 4e 20 6d 69 6e 75 74 65 73 0a 2a     NNN minutes.*
696a0 2a 20 20 20 20 20 4e 4e 4e 2e 4e 4e 4e 4e 20 73  *     NNN.NNNN s
696b0 65 63 6f 6e 64 73 0a 2a 2a 20 20 20 20 20 4e 4e  econds.**     NN
696c0 4e 20 6d 6f 6e 74 68 73 0a 2a 2a 20 20 20 20 20  N months.**     
696d0 4e 4e 4e 20 79 65 61 72 73 0a 2a 2a 20 20 20 20  NNN years.**    
696e0 20 73 74 61 72 74 20 6f 66 20 6d 6f 6e 74 68 0a   start of month.
696f0 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66 20  **     start of 
69700 79 65 61 72 0a 2a 2a 20 20 20 20 20 73 74 61 72  year.**     star
69710 74 20 6f 66 20 77 65 65 6b 0a 2a 2a 20 20 20 20  t of week.**    
69720 20 73 74 61 72 74 20 6f 66 20 64 61 79 0a 2a 2a   start of day.**
69730 20 20 20 20 20 77 65 65 6b 64 61 79 20 4e 0a 2a       weekday N.*
69740 2a 20 20 20 20 20 75 6e 69 78 65 70 6f 63 68 0a  *     unixepoch.
69750 2a 2a 20 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65  **     localtime
69760 0a 2a 2a 20 20 20 20 20 75 74 63 0a 2a 2a 0a 2a  .**     utc.**.*
69770 2a 20 52 65 74 75 72 6e 20 30 20 6f 6e 20 73 75  * Return 0 on su
69780 63 63 65 73 73 20 61 6e 64 20 31 20 69 66 20 74  ccess and 1 if t
69790 68 65 72 65 20 69 73 20 61 6e 79 20 6b 69 6e 64  here is any kind
697a0 20 6f 66 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74   of error..*/.st
697b0 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 4d 6f  atic int parseMo
697c0 64 69 66 69 65 72 28 63 6f 6e 73 74 20 63 68 61  difier(const cha
697d0 72 20 2a 7a 4d 6f 64 2c 20 44 61 74 65 54 69 6d  r *zMod, DateTim
697e0 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20  e *p){.  int rc 
697f0 3d 20 31 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20  = 1;.  int n;.  
69800 64 6f 75 62 6c 65 20 72 3b 0a 20 20 63 68 61 72  double r;.  char
69810 20 2a 7a 2c 20 7a 42 75 66 5b 33 30 5d 3b 0a 20   *z, zBuf[30];. 
69820 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 66 6f 72   z = zBuf;.  for
69830 28 6e 3d 30 3b 20 6e 3c 73 69 7a 65 6f 66 28 7a  (n=0; n<sizeof(z
69840 42 75 66 29 2d 31 20 26 26 20 7a 4d 6f 64 5b 6e  Buf)-1 && zMod[n
69850 5d 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 7a 5b 6e  ]; n++){.    z[n
69860 5d 20 3d 20 74 6f 6c 6f 77 65 72 28 7a 4d 6f 64  ] = tolower(zMod
69870 5b 6e 5d 29 3b 0a 20 20 7d 0a 20 20 7a 5b 6e 5d  [n]);.  }.  z[n]
69880 20 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 20   = 0;.  switch( 
69890 7a 5b 30 5d 20 29 7b 0a 20 20 20 20 63 61 73 65  z[0] ){.    case
698a0 20 27 6c 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a   'l': {.      /*
698b0 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 20 20      localtime.  
698c0 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
698d0 41 73 73 75 6d 69 6e 67 20 74 68 65 20 63 75 72  Assuming the cur
698e0 72 65 6e 74 20 74 69 6d 65 20 76 61 6c 75 65 20  rent time value 
698f0 69 73 20 55 54 43 20 28 61 2e 6b 2e 61 2e 20 47  is UTC (a.k.a. G
69900 4d 54 29 2c 20 73 68 69 66 74 20 69 74 20 74 6f  MT), shift it to
69910 0a 20 20 20 20 20 20 2a 2a 20 73 68 6f 77 20 6c  .      ** show l
69920 6f 63 61 6c 20 74 69 6d 65 2e 0a 20 20 20 20 20  ocal time..     
69930 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74   */.      if( st
69940 72 63 6d 70 28 7a 2c 20 22 6c 6f 63 61 6c 74 69  rcmp(z, "localti
69950 6d 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  me")==0 ){.     
69960 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b     computeJD(p);
69970 0a 20 20 20 20 20 20 20 20 70 2d 3e 72 4a 44 20  .        p->rJD 
69980 2b 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73  += localtimeOffs
69990 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63  et(p);.        c
699a0 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70  learYMD_HMS_TZ(p
699b0 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
699c0 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
699d0 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
699e0 20 20 63 61 73 65 20 27 75 27 3a 20 7b 0a 20 20    case 'u': {.  
699f0 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a 20      /*.      ** 
69a00 20 20 20 75 6e 69 78 65 70 6f 63 68 0a 20 20 20     unixepoch.   
69a10 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54     **.      ** T
69a20 72 65 61 74 20 74 68 65 20 63 75 72 72 65 6e 74  reat the current
69a30 20 76 61 6c 75 65 20 6f 66 20 70 2d 3e 72 4a 44   value of p->rJD
69a40 20 61 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   as the number o
69a50 66 0a 20 20 20 20 20 20 2a 2a 20 73 65 63 6f 6e  f.      ** secon
69a60 64 73 20 73 69 6e 63 65 20 31 39 37 30 2e 20 20  ds since 1970.  
69a70 43 6f 6e 76 65 72 74 20 74 6f 20 61 20 72 65 61  Convert to a rea
69a80 6c 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d  l julian day num
69a90 62 65 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ber..      */.  
69aa0 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a      if( strcmp(z
69ab0 2c 20 22 75 6e 69 78 65 70 6f 63 68 22 29 3d 3d  , "unixepoch")==
69ac0 30 20 26 26 20 70 2d 3e 76 61 6c 69 64 4a 44 20  0 && p->validJD 
69ad0 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 72 4a  ){.        p->rJ
69ae0 44 20 3d 20 70 2d 3e 72 4a 44 2f 38 36 34 30 30  D = p->rJD/86400
69af0 2e 30 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a  .0 + 2440587.5;.
69b00 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44          clearYMD
69b10 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20  _HMS_TZ(p);.    
69b20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20      rc = 0;.    
69b30 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63    }else if( strc
69b40 6d 70 28 7a 2c 20 22 75 74 63 22 29 3d 3d 30 20  mp(z, "utc")==0 
69b50 29 7b 0a 20 20 20 20 20 20 20 20 64 6f 75 62 6c  ){.        doubl
69b60 65 20 63 31 3b 0a 20 20 20 20 20 20 20 20 63 6f  e c1;.        co
69b70 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 20  mputeJD(p);.    
69b80 20 20 20 20 63 31 20 3d 20 6c 6f 63 61 6c 74 69      c1 = localti
69b90 6d 65 4f 66 66 73 65 74 28 70 29 3b 0a 20 20 20  meOffset(p);.   
69ba0 20 20 20 20 20 70 2d 3e 72 4a 44 20 2d 3d 20 63       p->rJD -= c
69bb0 31 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72  1;.        clear
69bc0 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20  YMD_HMS_TZ(p);. 
69bd0 20 20 20 20 20 20 20 70 2d 3e 72 4a 44 20 2b 3d         p->rJD +=
69be0 20 63 31 20 2d 20 6c 6f 63 61 6c 74 69 6d 65 4f   c1 - localtimeO
69bf0 66 66 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20  ffset(p);.      
69c00 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20    rc = 0;.      
69c10 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
69c20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 77     }.    case 'w
69c30 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20  ': {.      /*.  
69c40 20 20 20 20 2a 2a 20 20 20 20 77 65 65 6b 64 61      **    weekda
69c50 79 20 4e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  y N.      **.   
69c60 20 20 20 2a 2a 20 4d 6f 76 65 20 74 68 65 20 64     ** Move the d
69c70 61 74 65 20 74 6f 20 74 68 65 20 73 61 6d 65 20  ate to the same 
69c80 74 69 6d 65 20 6f 6e 20 74 68 65 20 6e 65 78 74  time on the next
69c90 20 6f 63 63 75 72 72 65 6e 63 65 20 6f 66 0a 20   occurrence of. 
69ca0 20 20 20 20 20 2a 2a 20 77 65 65 6b 64 61 79 20       ** weekday 
69cb0 4e 20 77 68 65 72 65 20 30 3d 3d 53 75 6e 64 61  N where 0==Sunda
69cc0 79 2c 20 31 3d 3d 4d 6f 6e 64 61 79 2c 20 61 6e  y, 1==Monday, an
69cd0 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 49 66 20  d so forth.  If 
69ce0 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 74  the.      ** dat
69cf0 65 20 69 73 20 61 6c 72 65 61 64 79 20 6f 6e 20  e is already on 
69d00 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
69d10 77 65 65 6b 64 61 79 2c 20 74 68 69 73 20 69 73  weekday, this is
69d20 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 20 20 20 20   a no-op..      
69d30 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74 72  */.      if( str
69d40 6e 63 6d 70 28 7a 2c 20 22 77 65 65 6b 64 61 79  ncmp(z, "weekday
69d50 20 22 2c 20 38 29 3d 3d 30 20 26 26 20 67 65 74   ", 8)==0 && get
69d60 56 61 6c 75 65 28 26 7a 5b 38 5d 2c 26 72 29 3e  Value(&z[8],&r)>
69d70 30 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0.              
69d80 20 20 20 26 26 20 28 6e 3d 72 29 3d 3d 72 20 26     && (n=r)==r &
69d90 26 20 6e 3e 3d 30 20 26 26 20 72 3c 37 20 29 7b  & n>=0 && r<7 ){
69da0 0a 20 20 20 20 20 20 20 20 69 6e 74 20 5a 3b 0a  .        int Z;.
69db0 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 59          computeY
69dc0 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 20 20  MD_HMS(p);.     
69dd0 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20     p->validTZ = 
69de0 30 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61  0;.        p->va
69df0 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20  lidJD = 0;.     
69e00 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b     computeJD(p);
69e10 0a 20 20 20 20 20 20 20 20 5a 20 3d 20 70 2d 3e  .        Z = p->
69e20 72 4a 44 20 2b 20 31 2e 35 3b 0a 20 20 20 20 20  rJD + 1.5;.     
69e30 20 20 20 5a 20 25 3d 20 37 3b 0a 20 20 20 20 20     Z %= 7;.     
69e40 20 20 20 69 66 28 20 5a 3e 6e 20 29 20 5a 20 2d     if( Z>n ) Z -
69e50 3d 20 37 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  = 7;.        p->
69e60 72 4a 44 20 2b 3d 20 6e 20 2d 20 5a 3b 0a 20 20  rJD += n - Z;.  
69e70 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48        clearYMD_H
69e80 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20  MS_TZ(p);.      
69e90 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20    rc = 0;.      
69ea0 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
69eb0 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 73     }.    case 's
69ec0 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20  ': {.      /*.  
69ed0 20 20 20 20 2a 2a 20 20 20 20 73 74 61 72 74 20      **    start 
69ee0 6f 66 20 54 54 54 54 54 0a 20 20 20 20 20 20 2a  of TTTTT.      *
69ef0 2a 0a 20 20 20 20 20 20 2a 2a 20 4d 6f 76 65 20  *.      ** Move 
69f00 74 68 65 20 64 61 74 65 20 62 61 63 6b 77 61 72  the date backwar
69f10 64 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e  ds to the beginn
69f20 69 6e 67 20 6f 66 20 74 68 65 20 63 75 72 72 65  ing of the curre
69f30 6e 74 20 64 61 79 2c 0a 20 20 20 20 20 20 2a 2a  nt day,.      **
69f40 20 6f 72 20 6d 6f 6e 74 68 20 6f 72 20 79 65 61   or month or yea
69f50 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  r..      */.    
69f60 20 20 69 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c    if( strncmp(z,
69f70 20 22 73 74 61 72 74 20 6f 66 20 22 2c 20 39 29   "start of ", 9)
69f80 21 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  !=0 ) break;.   
69f90 20 20 20 7a 20 2b 3d 20 39 3b 0a 20 20 20 20 20     z += 9;.     
69fa0 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a   computeYMD(p);.
69fb0 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d        p->validHM
69fc0 53 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 2d 3e  S = 1;.      p->
69fd0 68 20 3d 20 70 2d 3e 6d 20 3d 20 30 3b 0a 20 20  h = p->m = 0;.  
69fe0 20 20 20 20 70 2d 3e 73 20 3d 20 30 2e 30 3b 0a      p->s = 0.0;.
69ff0 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a        p->validTZ
6a000 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76   = 0;.      p->v
6a010 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20  alidJD = 0;.    
6a020 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 22    if( strcmp(z,"
6a030 6d 6f 6e 74 68 22 29 3d 3d 30 20 29 7b 0a 20 20  month")==0 ){.  
6a040 20 20 20 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a        p->D = 1;.
6a050 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a          rc = 0;.
6a060 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
6a070 73 74 72 63 6d 70 28 7a 2c 22 79 65 61 72 22 29  strcmp(z,"year")
6a080 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63  ==0 ){.        c
6a090 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20 20  omputeYMD(p);.  
6a0a0 20 20 20 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a        p->M = 1;.
6a0b0 20 20 20 20 20 20 20 20 70 2d 3e 44 20 3d 20 31          p->D = 1
6a0c0 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30  ;.        rc = 0
6a0d0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
6a0e0 28 20 73 74 72 63 6d 70 28 7a 2c 22 64 61 79 22  ( strcmp(z,"day"
6a0f0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
6a100 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  rc = 0;.      }.
6a110 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
6a120 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2b 27 3a   }.    case '+':
6a130 0a 20 20 20 20 63 61 73 65 20 27 2d 27 3a 0a 20  .    case '-':. 
6a140 20 20 20 63 61 73 65 20 27 30 27 3a 0a 20 20 20     case '0':.   
6a150 20 63 61 73 65 20 27 31 27 3a 0a 20 20 20 20 63   case '1':.    c
6a160 61 73 65 20 27 32 27 3a 0a 20 20 20 20 63 61 73  ase '2':.    cas
6a170 65 20 27 33 27 3a 0a 20 20 20 20 63 61 73 65 20  e '3':.    case 
6a180 27 34 27 3a 0a 20 20 20 20 63 61 73 65 20 27 35  '4':.    case '5
6a190 27 3a 0a 20 20 20 20 63 61 73 65 20 27 36 27 3a  ':.    case '6':
6a1a0 0a 20 20 20 20 63 61 73 65 20 27 37 27 3a 0a 20  .    case '7':. 
6a1b0 20 20 20 63 61 73 65 20 27 38 27 3a 0a 20 20 20     case '8':.   
6a1c0 20 63 61 73 65 20 27 39 27 3a 20 7b 0a 20 20 20   case '9': {.   
6a1d0 20 20 20 6e 20 3d 20 67 65 74 56 61 6c 75 65 28     n = getValue(
6a1e0 7a 2c 20 26 72 29 3b 0a 20 20 20 20 20 20 61 73  z, &r);.      as
6a1f0 73 65 72 74 28 20 6e 3e 3d 31 20 29 3b 0a 20 20  sert( n>=1 );.  
6a200 20 20 20 20 69 66 28 20 7a 5b 6e 5d 3d 3d 27 3a      if( z[n]==':
6a210 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ' ){.        /* 
6a220 41 20 6d 6f 64 69 66 69 65 72 20 6f 66 20 74 68  A modifier of th
6a230 65 20 66 6f 72 6d 20 28 2b 7c 2d 29 48 48 3a 4d  e form (+|-)HH:M
6a240 4d 3a 53 53 2e 46 46 46 20 61 64 64 73 20 28 6f  M:SS.FFF adds (o
6a250 72 20 73 75 62 74 72 61 63 74 73 29 20 74 68 65  r subtracts) the
6a260 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 70 65 63  .        ** spec
6a270 69 66 69 65 64 20 6e 75 6d 62 65 72 20 6f 66 20  ified number of 
6a280 68 6f 75 72 73 2c 20 6d 69 6e 75 74 65 73 2c 20  hours, minutes, 
6a290 73 65 63 6f 6e 64 73 2c 20 61 6e 64 20 66 72 61  seconds, and fra
6a2a0 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 0a  ctional seconds.
6a2b0 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68          ** to th
6a2c0 65 20 74 69 6d 65 2e 20 20 54 68 65 20 22 2e 46  e time.  The ".F
6a2d0 46 46 22 20 6d 61 79 20 62 65 20 6f 6d 69 74 74  FF" may be omitt
6a2e0 65 64 2e 20 20 54 68 65 20 22 3a 53 53 2e 46 46  ed.  The ":SS.FF
6a2f0 46 22 20 6d 61 79 20 62 65 0a 20 20 20 20 20 20  F" may be.      
6a300 20 20 2a 2a 20 6f 6d 69 74 74 65 64 2e 0a 20 20    ** omitted..  
6a310 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
6a320 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 20   const char *z2 
6a330 3d 20 7a 3b 0a 20 20 20 20 20 20 20 20 44 61 74  = z;.        Dat
6a340 65 54 69 6d 65 20 74 78 3b 0a 20 20 20 20 20 20  eTime tx;.      
6a350 20 20 69 6e 74 20 64 61 79 3b 0a 20 20 20 20 20    int day;.     
6a360 20 20 20 69 66 28 20 21 69 73 64 69 67 69 74 28     if( !isdigit(
6a370 2a 28 75 38 2a 29 7a 32 29 20 29 20 7a 32 2b 2b  *(u8*)z2) ) z2++
6a380 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74  ;.        memset
6a390 28 26 74 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28  (&tx, 0, sizeof(
6a3a0 74 78 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66  tx));.        if
6a3b0 28 20 70 61 72 73 65 48 68 4d 6d 53 73 28 7a 32  ( parseHhMmSs(z2
6a3c0 2c 20 26 74 78 29 20 29 20 62 72 65 61 6b 3b 0a  , &tx) ) break;.
6a3d0 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a          computeJ
6a3e0 44 28 26 74 78 29 3b 0a 20 20 20 20 20 20 20 20  D(&tx);.        
6a3f0 74 78 2e 72 4a 44 20 2d 3d 20 30 2e 35 3b 0a 20  tx.rJD -= 0.5;. 
6a400 20 20 20 20 20 20 20 64 61 79 20 3d 20 28 69 6e         day = (in
6a410 74 29 74 78 2e 72 4a 44 3b 0a 20 20 20 20 20 20  t)tx.rJD;.      
6a420 20 20 74 78 2e 72 4a 44 20 2d 3d 20 64 61 79 3b    tx.rJD -= day;
6a430 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b 30  .        if( z[0
6a440 5d 3d 3d 27 2d 27 20 29 20 74 78 2e 72 4a 44 20  ]=='-' ) tx.rJD 
6a450 3d 20 2d 74 78 2e 72 4a 44 3b 0a 20 20 20 20 20  = -tx.rJD;.     
6a460 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b     computeJD(p);
6a470 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d  .        clearYM
6a480 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20  D_HMS_TZ(p);.   
6a490 20 20 20 20 20 70 2d 3e 72 4a 44 20 2b 3d 20 74       p->rJD += t
6a4a0 78 2e 72 4a 44 3b 0a 20 20 20 20 20 20 20 20 72  x.rJD;.        r
6a4b0 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62  c = 0;.        b
6a4c0 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
6a4d0 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a 20 20 20 20      z += n;.    
6a4e0 20 20 77 68 69 6c 65 28 20 69 73 73 70 61 63 65    while( isspace
6a4f0 28 2a 28 75 38 2a 29 7a 29 20 29 20 7a 2b 2b 3b  (*(u8*)z) ) z++;
6a500 0a 20 20 20 20 20 20 6e 20 3d 20 73 74 72 6c 65  .      n = strle
6a510 6e 28 7a 29 3b 0a 20 20 20 20 20 20 69 66 28 20  n(z);.      if( 
6a520 6e 3e 31 30 20 7c 7c 20 6e 3c 33 20 29 20 62 72  n>10 || n<3 ) br
6a530 65 61 6b 3b 0a 20 20 20 20 20 20 69 66 28 20 7a  eak;.      if( z
6a540 5b 6e 2d 31 5d 3d 3d 27 73 27 20 29 7b 20 7a 5b  [n-1]=='s' ){ z[
6a550 6e 2d 31 5d 20 3d 20 30 3b 20 6e 2d 2d 3b 20 7d  n-1] = 0; n--; }
6a560 0a 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44  .      computeJD
6a570 28 70 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  (p);.      rc = 
6a580 30 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3d 3d  0;.      if( n==
6a590 33 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 64  3 && strcmp(z,"d
6a5a0 61 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ay")==0 ){.     
6a5b0 20 20 20 70 2d 3e 72 4a 44 20 2b 3d 20 72 3b 0a     p->rJD += r;.
6a5c0 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
6a5d0 6e 3d 3d 34 20 26 26 20 73 74 72 63 6d 70 28 7a  n==4 && strcmp(z
6a5e0 2c 22 68 6f 75 72 22 29 3d 3d 30 20 29 7b 0a 20  ,"hour")==0 ){. 
6a5f0 20 20 20 20 20 20 20 70 2d 3e 72 4a 44 20 2b 3d         p->rJD +=
6a600 20 72 2f 32 34 2e 30 3b 0a 20 20 20 20 20 20 7d   r/24.0;.      }
6a610 65 6c 73 65 20 69 66 28 20 6e 3d 3d 36 20 26 26  else if( n==6 &&
6a620 20 73 74 72 63 6d 70 28 7a 2c 22 6d 69 6e 75 74   strcmp(z,"minut
6a630 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  e")==0 ){.      
6a640 20 20 70 2d 3e 72 4a 44 20 2b 3d 20 72 2f 28 32    p->rJD += r/(2
6a650 34 2e 30 2a 36 30 2e 30 29 3b 0a 20 20 20 20 20  4.0*60.0);.     
6a660 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 36 20   }else if( n==6 
6a670 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 73 65 63  && strcmp(z,"sec
6a680 6f 6e 64 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ond")==0 ){.    
6a690 20 20 20 20 70 2d 3e 72 4a 44 20 2b 3d 20 72 2f      p->rJD += r/
6a6a0 28 32 34 2e 30 2a 36 30 2e 30 2a 36 30 2e 30 29  (24.0*60.0*60.0)
6a6b0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
6a6c0 28 20 6e 3d 3d 35 20 26 26 20 73 74 72 63 6d 70  ( n==5 && strcmp
6a6d0 28 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20 29  (z,"month")==0 )
6a6e0 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78 2c  {.        int x,
6a6f0 20 79 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70   y;.        comp
6a700 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20  uteYMD_HMS(p);. 
6a710 20 20 20 20 20 20 20 70 2d 3e 4d 20 2b 3d 20 72         p->M += r
6a720 3b 0a 20 20 20 20 20 20 20 20 78 20 3d 20 70 2d  ;.        x = p-
6a730 3e 4d 3e 30 20 3f 20 28 70 2d 3e 4d 2d 31 29 2f  >M>0 ? (p->M-1)/
6a740 31 32 20 3a 20 28 70 2d 3e 4d 2d 31 32 29 2f 31  12 : (p->M-12)/1
6a750 32 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 59 20  2;.        p->Y 
6a760 2b 3d 20 78 3b 0a 20 20 20 20 20 20 20 20 70 2d  += x;.        p-
6a770 3e 4d 20 2d 3d 20 78 2a 31 32 3b 0a 20 20 20 20  >M -= x*12;.    
6a780 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d      p->validJD =
6a790 20 30 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70   0;.        comp
6a7a0 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20  uteJD(p);.      
6a7b0 20 20 79 20 3d 20 72 3b 0a 20 20 20 20 20 20 20    y = r;.       
6a7c0 20 69 66 28 20 79 21 3d 72 20 29 7b 0a 20 20 20   if( y!=r ){.   
6a7d0 20 20 20 20 20 20 20 70 2d 3e 72 4a 44 20 2b 3d         p->rJD +=
6a7e0 20 28 72 20 2d 20 79 29 2a 33 30 2e 30 3b 0a 20   (r - y)*30.0;. 
6a7f0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
6a800 65 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20 26 26  else if( n==4 &&
6a810 20 73 74 72 63 6d 70 28 7a 2c 22 79 65 61 72 22   strcmp(z,"year"
6a820 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
6a830 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 70  computeYMD_HMS(p
6a840 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 59 20  );.        p->Y 
6a850 2b 3d 20 72 3b 0a 20 20 20 20 20 20 20 20 70 2d  += r;.        p-
6a860 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20  >validJD = 0;.  
6a870 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28        computeJD(
6a880 70 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  p);.      }else{
6a890 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 31 3b  .        rc = 1;
6a8a0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
6a8b0 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70  learYMD_HMS_TZ(p
6a8c0 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
6a8d0 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
6a8e0 74 3a 20 7b 0a 20 20 20 20 20 20 62 72 65 61 6b  t: {.      break
6a8f0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
6a900 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
6a910 2a 20 50 72 6f 63 65 73 73 20 74 69 6d 65 20 66  * Process time f
6a920 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74  unction argument
6a930 73 2e 20 20 61 72 67 76 5b 30 5d 20 69 73 20 61  s.  argv[0] is a
6a940 20 64 61 74 65 2d 74 69 6d 65 20 73 74 61 6d 70   date-time stamp
6a950 2e 0a 2a 2a 20 61 72 67 76 5b 31 5d 20 61 6e 64  ..** argv[1] and
6a960 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 6d   following are m
6a970 6f 64 69 66 69 65 72 73 2e 20 20 50 61 72 73 65  odifiers.  Parse
6a980 20 74 68 65 6d 20 61 6c 6c 20 61 6e 64 20 77 72   them all and wr
6a990 69 74 65 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c  ite.** the resul
6a9a0 74 69 6e 67 20 74 69 6d 65 20 69 6e 74 6f 20 74  ting time into t
6a9b0 68 65 20 44 61 74 65 54 69 6d 65 20 73 74 72 75  he DateTime stru
6a9c0 63 74 75 72 65 20 70 2e 20 20 52 65 74 75 72 6e  cture p.  Return
6a9d0 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73   0.** on success
6a9e0 20 61 6e 64 20 31 20 69 66 20 74 68 65 72 65 20   and 1 if there 
6a9f0 61 72 65 20 61 6e 79 20 65 72 72 6f 72 73 2e 0a  are any errors..
6aa00 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61  **.** If there a
6aa10 72 65 20 7a 65 72 6f 20 70 61 72 61 6d 65 74 65  re zero paramete
6aa20 72 73 20 28 69 66 20 65 76 65 6e 20 61 72 67 76  rs (if even argv
6aa30 5b 30 5d 20 69 73 20 75 6e 64 65 66 69 6e 65 64  [0] is undefined
6aa40 29 0a 2a 2a 20 74 68 65 6e 20 61 73 73 75 6d 65  ).** then assume
6aa50 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65   a default value
6aa60 20 6f 66 20 22 6e 6f 77 22 20 66 6f 72 20 61 72   of "now" for ar
6aa70 67 76 5b 30 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63  gv[0]..*/.static
6aa80 20 69 6e 74 20 69 73 44 61 74 65 28 0a 20 20 73   int isDate(.  s
6aa90 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
6aaa0 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e 74 20  context, .  int 
6aab0 61 72 67 63 2c 20 0a 20 20 73 71 6c 69 74 65 33  argc, .  sqlite3
6aac0 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 2c 20 0a  _value **argv, .
6aad0 20 20 44 61 74 65 54 69 6d 65 20 2a 70 0a 29 7b    DateTime *p.){
6aae0 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e 73  .  int i;.  cons
6aaf0 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
6ab00 2a 7a 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  *z;.  static con
6ab10 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
6ab20 20 7a 44 66 6c 74 5b 5d 20 3d 20 22 6e 6f 77 22   zDflt[] = "now"
6ab30 3b 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 30 20  ;.  if( argc==0 
6ab40 29 7b 0a 20 20 20 20 7a 20 3d 20 7a 44 66 6c 74  ){.    z = zDflt
6ab50 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a  ;.  }else{.    z
6ab60 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
6ab70 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
6ab80 20 20 7d 0a 20 20 69 66 28 20 21 7a 20 7c 7c 20    }.  if( !z || 
6ab90 70 61 72 73 65 44 61 74 65 4f 72 54 69 6d 65 28  parseDateOrTime(
6aba0 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29  context, (char*)
6abb0 7a 2c 20 70 29 20 29 7b 0a 20 20 20 20 72 65 74  z, p) ){.    ret
6abc0 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 66 6f 72  urn 1;.  }.  for
6abd0 28 69 3d 31 3b 20 69 3c 61 72 67 63 3b 20 69 2b  (i=1; i<argc; i+
6abe0 2b 29 7b 0a 20 20 20 20 69 66 28 20 28 7a 20 3d  +){.    if( (z =
6abf0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
6ac00 65 78 74 28 61 72 67 76 5b 69 5d 29 29 3d 3d 30  ext(argv[i]))==0
6ac10 20 7c 7c 20 70 61 72 73 65 4d 6f 64 69 66 69 65   || parseModifie
6ac20 72 28 28 63 68 61 72 2a 29 7a 2c 20 70 29 20 29  r((char*)z, p) )
6ac30 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
6ac40 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
6ac50 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  turn 0;.}.../*.*
6ac60 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
6ac70 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65  routines impleme
6ac80 6e 74 20 74 68 65 20 76 61 72 69 6f 75 73 20 64  nt the various d
6ac90 61 74 65 20 61 6e 64 20 74 69 6d 65 20 66 75 6e  ate and time fun
6aca0 63 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 53 51 4c  ctions.** of SQL
6acb0 69 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 20  ite..*/../*.**  
6acc0 20 20 6a 75 6c 69 61 6e 64 61 79 28 20 54 49 4d    julianday( TIM
6acd0 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f  ESTRING, MOD, MO
6ace0 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65  D, ...).**.** Re
6acf0 74 75 72 6e 20 74 68 65 20 6a 75 6c 69 61 6e 20  turn the julian 
6ad00 64 61 79 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  day number of th
6ad10 65 20 64 61 74 65 20 73 70 65 63 69 66 69 65 64  e date specified
6ad20 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74   in the argument
6ad30 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  s.*/.static void
6ad40 20 6a 75 6c 69 61 6e 64 61 79 46 75 6e 63 28 0a   juliandayFunc(.
6ad50 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
6ad60 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
6ad70 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
6ad80 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
6ad90 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 3b 0a  {.  DateTime x;.
6ada0 20 20 69 66 28 20 69 73 44 61 74 65 28 63 6f 6e    if( isDate(con
6adb0 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76  text, argc, argv
6adc0 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20  , &x)==0 ){.    
6add0 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a 20  computeJD(&x);. 
6ade0 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
6adf0 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74  t_double(context
6ae00 2c 20 78 2e 72 4a 44 29 3b 0a 20 20 7d 0a 7d 0a  , x.rJD);.  }.}.
6ae10 0a 2f 2a 0a 2a 2a 20 20 20 20 64 61 74 65 74 69  ./*.**    dateti
6ae20 6d 65 28 20 54 49 4d 45 53 54 52 49 4e 47 2c 20  me( TIMESTRING, 
6ae30 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a  MOD, MOD, ...).*
6ae40 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 59 59 59 59  *.** Return YYYY
6ae50 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 0a  -MM-DD HH:MM:SS.
6ae60 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  */.static void d
6ae70 61 74 65 74 69 6d 65 46 75 6e 63 28 0a 20 20 73  atetimeFunc(.  s
6ae80 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
6ae90 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
6aea0 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
6aeb0 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
6aec0 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69   DateTime x;.  i
6aed0 66 28 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78  f( isDate(contex
6aee0 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26  t, argc, argv, &
6aef0 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61  x)==0 ){.    cha
6af00 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20  r zBuf[100];.   
6af10 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28   computeYMD_HMS(
6af20 26 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  &x);.    sqlite3
6af30 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66  _snprintf(sizeof
6af40 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25  (zBuf), zBuf, "%
6af50 30 34 64 2d 25 30 32 64 2d 25 30 32 64 20 25 30  04d-%02d-%02d %0
6af60 32 64 3a 25 30 32 64 3a 25 30 32 64 22 2c 0a 20  2d:%02d:%02d",. 
6af70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6af80 20 20 20 20 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e      x.Y, x.M, x.
6af90 44 2c 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e  D, x.h, x.m, (in
6afa0 74 29 28 78 2e 73 29 29 3b 0a 20 20 20 20 73 71  t)(x.s));.    sq
6afb0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
6afc0 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c  t(context, zBuf,
6afd0 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e   -1, SQLITE_TRAN
6afe0 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  SIENT);.  }.}../
6aff0 2a 0a 2a 2a 20 20 20 20 74 69 6d 65 28 20 54 49  *.**    time( TI
6b000 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d  MESTRING, MOD, M
6b010 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52  OD, ...).**.** R
6b020 65 74 75 72 6e 20 48 48 3a 4d 4d 3a 53 53 0a 2a  eturn HH:MM:SS.*
6b030 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 69  /.static void ti
6b040 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  meFunc(.  sqlite
6b050 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
6b060 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
6b070 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
6b080 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65  **argv.){.  Date
6b090 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73  Time x;.  if( is
6b0a0 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72  Date(context, ar
6b0b0 67 63 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30  gc, argv, &x)==0
6b0c0 20 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75   ){.    char zBu
6b0d0 66 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70  f[100];.    comp
6b0e0 75 74 65 48 4d 53 28 26 78 29 3b 0a 20 20 20 20  uteHMS(&x);.    
6b0f0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
6b100 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a  (sizeof(zBuf), z
6b110 42 75 66 2c 20 22 25 30 32 64 3a 25 30 32 64 3a  Buf, "%02d:%02d:
6b120 25 30 32 64 22 2c 20 78 2e 68 2c 20 78 2e 6d 2c  %02d", x.h, x.m,
6b130 20 28 69 6e 74 29 78 2e 73 29 3b 0a 20 20 20 20   (int)x.s);.    
6b140 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
6b150 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75  ext(context, zBu
6b160 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52  f, -1, SQLITE_TR
6b170 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a  ANSIENT);.  }.}.
6b180 0a 2f 2a 0a 2a 2a 20 20 20 20 64 61 74 65 28 20  ./*.**    date( 
6b190 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c  TIMESTRING, MOD,
6b1a0 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a   MOD, ...).**.**
6b1b0 20 52 65 74 75 72 6e 20 59 59 59 59 2d 4d 4d 2d   Return YYYY-MM-
6b1c0 44 44 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  DD.*/.static voi
6b1d0 64 20 64 61 74 65 46 75 6e 63 28 0a 20 20 73 71  d dateFunc(.  sq
6b1e0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
6b1f0 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
6b200 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
6b210 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
6b220 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66  DateTime x;.  if
6b230 28 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74  ( isDate(context
6b240 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78  , argc, argv, &x
6b250 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72  )==0 ){.    char
6b260 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20   zBuf[100];.    
6b270 63 6f 6d 70 75 74 65 59 4d 44 28 26 78 29 3b 0a  computeYMD(&x);.
6b280 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
6b290 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66  intf(sizeof(zBuf
6b2a0 29 2c 20 7a 42 75 66 2c 20 22 25 30 34 64 2d 25  ), zBuf, "%04d-%
6b2b0 30 32 64 2d 25 30 32 64 22 2c 20 78 2e 59 2c 20  02d-%02d", x.Y, 
6b2c0 78 2e 4d 2c 20 78 2e 44 29 3b 0a 20 20 20 20 73  x.M, x.D);.    s
6b2d0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
6b2e0 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66  xt(context, zBuf
6b2f0 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41  , -1, SQLITE_TRA
6b300 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a  NSIENT);.  }.}..
6b310 2f 2a 0a 2a 2a 20 20 20 20 73 74 72 66 74 69 6d  /*.**    strftim
6b320 65 28 20 46 4f 52 4d 41 54 2c 20 54 49 4d 45 53  e( FORMAT, TIMES
6b330 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c  TRING, MOD, MOD,
6b340 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75   ...).**.** Retu
6b350 72 6e 20 61 20 73 74 72 69 6e 67 20 64 65 73 63  rn a string desc
6b360 72 69 62 65 64 20 62 79 20 46 4f 52 4d 41 54 2e  ribed by FORMAT.
6b370 20 20 43 6f 6e 76 65 72 73 69 6f 6e 73 20 61 73    Conversions as
6b380 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
6b390 20 20 25 64 20 20 64 61 79 20 6f 66 20 6d 6f 6e    %d  day of mon
6b3a0 74 68 0a 2a 2a 20 20 20 25 66 20 20 2a 2a 20 66  th.**   %f  ** f
6b3b0 72 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64  ractional second
6b3c0 73 20 20 53 53 2e 53 53 53 0a 2a 2a 20 20 20 25  s  SS.SSS.**   %
6b3d0 48 20 20 68 6f 75 72 20 30 30 2d 32 34 0a 2a 2a  H  hour 00-24.**
6b3e0 20 20 20 25 6a 20 20 64 61 79 20 6f 66 20 79 65     %j  day of ye
6b3f0 61 72 20 30 30 30 2d 33 36 36 0a 2a 2a 20 20 20  ar 000-366.**   
6b400 25 4a 20 20 2a 2a 20 4a 75 6c 69 61 6e 20 64 61  %J  ** Julian da
6b410 79 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 25 6d  y number.**   %m
6b420 20 20 6d 6f 6e 74 68 20 30 31 2d 31 32 0a 2a 2a    month 01-12.**
6b430 20 20 20 25 4d 20 20 6d 69 6e 75 74 65 20 30 30     %M  minute 00
6b440 2d 35 39 0a 2a 2a 20 20 20 25 73 20 20 73 65 63  -59.**   %s  sec
6b450 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37 30 2d  onds since 1970-
6b460 30 31 2d 30 31 0a 2a 2a 20 20 20 25 53 20 20 73  01-01.**   %S  s
6b470 65 63 6f 6e 64 73 20 30 30 2d 35 39 0a 2a 2a 20  econds 00-59.** 
6b480 20 20 25 77 20 20 64 61 79 20 6f 66 20 77 65 65    %w  day of wee
6b490 6b 20 30 2d 36 20 20 73 75 6e 64 61 79 3d 3d 30  k 0-6  sunday==0
6b4a0 0a 2a 2a 20 20 20 25 57 20 20 77 65 65 6b 20 6f  .**   %W  week o
6b4b0 66 20 79 65 61 72 20 30 30 2d 35 33 0a 2a 2a 20  f year 00-53.** 
6b4c0 20 20 25 59 20 20 79 65 61 72 20 30 30 30 30 2d    %Y  year 0000-
6b4d0 39 39 39 39 0a 2a 2a 20 20 20 25 25 20 20 25 0a  9999.**   %%  %.
6b4e0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
6b4f0 74 72 66 74 69 6d 65 46 75 6e 63 28 0a 20 20 73  trftimeFunc(.  s
6b500 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
6b510 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
6b520 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
6b530 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
6b540 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 75   DateTime x;.  u
6b550 36 34 20 6e 3b 0a 20 20 69 6e 74 20 69 2c 20 6a  64 n;.  int i, j
6b560 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 63  ;.  char *z;.  c
6b570 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6d 74 20  onst char *zFmt 
6b580 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73  = (const char*)s
6b590 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
6b5a0 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 63 68  t(argv[0]);.  ch
6b5b0 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20  ar zBuf[100];.  
6b5c0 69 66 28 20 7a 46 6d 74 3d 3d 30 20 7c 7c 20 69  if( zFmt==0 || i
6b5d0 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61  sDate(context, a
6b5e0 72 67 63 2d 31 2c 20 61 72 67 76 2b 31 2c 20 26  rgc-1, argv+1, &
6b5f0 78 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66  x) ) return;.  f
6b600 6f 72 28 69 3d 30 2c 20 6e 3d 31 3b 20 7a 46 6d  or(i=0, n=1; zFm
6b610 74 5b 69 5d 3b 20 69 2b 2b 2c 20 6e 2b 2b 29 7b  t[i]; i++, n++){
6b620 0a 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69 5d  .    if( zFmt[i]
6b630 3d 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20 73  =='%' ){.      s
6b640 77 69 74 63 68 28 20 7a 46 6d 74 5b 69 2b 31 5d  witch( zFmt[i+1]
6b650 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73 65   ){.        case
6b660 20 27 64 27 3a 0a 20 20 20 20 20 20 20 20 63 61   'd':.        ca
6b670 73 65 20 27 48 27 3a 0a 20 20 20 20 20 20 20 20  se 'H':.        
6b680 63 61 73 65 20 27 6d 27 3a 0a 20 20 20 20 20 20  case 'm':.      
6b690 20 20 63 61 73 65 20 27 4d 27 3a 0a 20 20 20 20    case 'M':.    
6b6a0 20 20 20 20 63 61 73 65 20 27 53 27 3a 0a 20 20      case 'S':.  
6b6b0 20 20 20 20 20 20 63 61 73 65 20 27 57 27 3a 0a        case 'W':.
6b6c0 20 20 20 20 20 20 20 20 20 20 6e 2b 2b 3b 0a 20            n++;. 
6b6d0 20 20 20 20 20 20 20 20 20 2f 2a 20 66 61 6c 6c           /* fall
6b6e0 20 74 68 72 75 20 2a 2f 0a 20 20 20 20 20 20 20   thru */.       
6b6f0 20 63 61 73 65 20 27 77 27 3a 0a 20 20 20 20 20   case 'w':.     
6b700 20 20 20 63 61 73 65 20 27 25 27 3a 0a 20 20 20     case '%':.   
6b710 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
6b720 20 20 20 20 20 20 63 61 73 65 20 27 66 27 3a 0a        case 'f':.
6b730 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 38            n += 8
6b740 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
6b750 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k;.        case 
6b760 27 6a 27 3a 0a 20 20 20 20 20 20 20 20 20 20 6e  'j':.          n
6b770 20 2b 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20   += 3;.         
6b780 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
6b790 63 61 73 65 20 27 59 27 3a 0a 20 20 20 20 20 20  case 'Y':.      
6b7a0 20 20 20 20 6e 20 2b 3d 20 38 3b 0a 20 20 20 20      n += 8;.    
6b7b0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
6b7c0 20 20 20 20 20 63 61 73 65 20 27 73 27 3a 0a 20       case 's':. 
6b7d0 20 20 20 20 20 20 20 63 61 73 65 20 27 4a 27 3a         case 'J':
6b7e0 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20  .          n += 
6b7f0 35 30 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72  50;.          br
6b800 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 64 65 66  eak;.        def
6b810 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 20 20  ault:.          
6b820 72 65 74 75 72 6e 3b 20 20 2f 2a 20 45 52 52 4f  return;  /* ERRO
6b830 52 2e 20 20 72 65 74 75 72 6e 20 61 20 4e 55 4c  R.  return a NUL
6b840 4c 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20  L */.      }.   
6b850 20 20 20 69 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20     i++;.    }.  
6b860 7d 0a 20 20 69 66 28 20 6e 3c 73 69 7a 65 6f 66  }.  if( n<sizeof
6b870 28 7a 42 75 66 29 20 29 7b 0a 20 20 20 20 7a 20  (zBuf) ){.    z 
6b880 3d 20 7a 42 75 66 3b 0a 20 20 7d 65 6c 73 65 20  = zBuf;.  }else 
6b890 69 66 28 20 6e 3e 73 71 6c 69 74 65 33 5f 63 6f  if( n>sqlite3_co
6b8a0 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
6b8b0 63 6f 6e 74 65 78 74 29 2d 3e 61 4c 69 6d 69 74  context)->aLimit
6b8c0 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45  [SQLITE_LIMIT_LE
6b8d0 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 73 71 6c  NGTH] ){.    sql
6b8e0 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
6b8f0 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74  r_toobig(context
6b900 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
6b910 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20   }else{.    z = 
6b920 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20  sqlite3_malloc( 
6b930 6e 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d  n );.    if( z==
6b940 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
6b950 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
6b960 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a  nomem(context);.
6b970 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
6b980 20 20 7d 0a 20 20 7d 0a 20 20 63 6f 6d 70 75 74    }.  }.  comput
6b990 65 4a 44 28 26 78 29 3b 0a 20 20 63 6f 6d 70 75  eJD(&x);.  compu
6b9a0 74 65 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a 20  teYMD_HMS(&x);. 
6b9b0 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 7a 46 6d 74   for(i=j=0; zFmt
6b9c0 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  [i]; i++){.    i
6b9d0 66 28 20 7a 46 6d 74 5b 69 5d 21 3d 27 25 27 20  f( zFmt[i]!='%' 
6b9e0 29 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20  ){.      z[j++] 
6b9f0 3d 20 7a 46 6d 74 5b 69 5d 3b 0a 20 20 20 20 7d  = zFmt[i];.    }
6ba00 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 2b 2b 3b  else{.      i++;
6ba10 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 20 7a  .      switch( z
6ba20 46 6d 74 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20  Fmt[i] ){.      
6ba30 20 20 63 61 73 65 20 27 64 27 3a 20 20 73 71 6c    case 'd':  sql
6ba40 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c  ite3_snprintf(3,
6ba50 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e   &z[j],"%02d",x.
6ba60 44 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b  D); j+=2; break;
6ba70 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 66  .        case 'f
6ba80 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 64  ': {.          d
6ba90 6f 75 62 6c 65 20 73 20 3d 20 78 2e 73 3b 0a 20  ouble s = x.s;. 
6baa0 20 20 20 20 20 20 20 20 20 69 66 28 20 73 3e 35           if( s>5
6bab0 39 2e 39 39 39 20 29 20 73 20 3d 20 35 39 2e 39  9.999 ) s = 59.9
6bac0 39 39 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  99;.          sq
6bad0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 37  lite3_snprintf(7
6bae0 2c 20 26 7a 5b 6a 5d 2c 22 25 30 36 2e 33 66 22  , &z[j],"%06.3f"
6baf0 2c 20 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , s);.          
6bb00 6a 20 2b 3d 20 73 74 72 6c 65 6e 28 26 7a 5b 6a  j += strlen(&z[j
6bb10 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72  ]);.          br
6bb20 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
6bb30 20 20 20 20 20 20 20 63 61 73 65 20 27 48 27 3a         case 'H':
6bb40 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
6bb50 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32  tf(3, &z[j],"%02
6bb60 64 22 2c 78 2e 68 29 3b 20 6a 2b 3d 32 3b 20 62  d",x.h); j+=2; b
6bb70 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61  reak;.        ca
6bb80 73 65 20 27 57 27 3a 20 2f 2a 20 46 61 6c 6c 20  se 'W': /* Fall 
6bb90 74 68 72 75 20 2a 2f 0a 20 20 20 20 20 20 20 20  thru */.        
6bba0 63 61 73 65 20 27 6a 27 3a 20 7b 0a 20 20 20 20  case 'j': {.    
6bbb0 20 20 20 20 20 20 69 6e 74 20 6e 44 61 79 3b 20        int nDay; 
6bbc0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
6bbd0 75 6d 62 65 72 20 6f 66 20 64 61 79 73 20 73 69  umber of days si
6bbe0 6e 63 65 20 31 73 74 20 64 61 79 20 6f 66 20 79  nce 1st day of y
6bbf0 65 61 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  ear */.         
6bc00 20 44 61 74 65 54 69 6d 65 20 79 20 3d 20 78 3b   DateTime y = x;
6bc10 0a 20 20 20 20 20 20 20 20 20 20 79 2e 76 61 6c  .          y.val
6bc20 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20  idJD = 0;.      
6bc30 20 20 20 20 79 2e 4d 20 3d 20 31 3b 0a 20 20 20      y.M = 1;.   
6bc40 20 20 20 20 20 20 20 79 2e 44 20 3d 20 31 3b 0a         y.D = 1;.
6bc50 20 20 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74            comput
6bc60 65 4a 44 28 26 79 29 3b 0a 20 20 20 20 20 20 20  eJD(&y);.       
6bc70 20 20 20 6e 44 61 79 20 3d 20 78 2e 72 4a 44 20     nDay = x.rJD 
6bc80 2d 20 79 2e 72 4a 44 20 2b 20 30 2e 35 3b 0a 20  - y.rJD + 0.5;. 
6bc90 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 46 6d           if( zFm
6bca0 74 5b 69 5d 3d 3d 27 57 27 20 29 7b 0a 20 20 20  t[i]=='W' ){.   
6bcb0 20 20 20 20 20 20 20 20 20 69 6e 74 20 77 64 3b           int wd;
6bcc0 20 20 20 2f 2a 20 30 3d 4d 6f 6e 64 61 79 2c 20     /* 0=Monday, 
6bcd0 31 3d 54 75 65 73 64 61 79 2c 20 2e 2e 2e 20 36  1=Tuesday, ... 6
6bce0 3d 53 75 6e 64 61 79 20 2a 2f 0a 20 20 20 20 20  =Sunday */.     
6bcf0 20 20 20 20 20 20 20 77 64 20 3d 20 28 28 69 6e         wd = ((in
6bd00 74 29 28 78 2e 72 4a 44 2b 30 2e 35 29 29 20 25  t)(x.rJD+0.5)) %
6bd10 20 37 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   7;.            
6bd20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
6bd30 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22  (3, &z[j],"%02d"
6bd40 2c 28 6e 44 61 79 2b 37 2d 77 64 29 2f 37 29 3b  ,(nDay+7-wd)/7);
6bd50 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a 20 2b  .            j +
6bd60 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  = 2;.          }
6bd70 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
6bd80 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
6bd90 74 66 28 34 2c 20 26 7a 5b 6a 5d 2c 22 25 30 33  tf(4, &z[j],"%03
6bda0 64 22 2c 6e 44 61 79 2b 31 29 3b 0a 20 20 20 20  d",nDay+1);.    
6bdb0 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 33 3b 0a          j += 3;.
6bdc0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
6bdd0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
6bde0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63       }.        c
6bdf0 61 73 65 20 27 4a 27 3a 20 7b 0a 20 20 20 20 20  ase 'J': {.     
6be00 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
6be10 72 69 6e 74 66 28 32 30 2c 20 26 7a 5b 6a 5d 2c  rintf(20, &z[j],
6be20 22 25 2e 31 36 67 22 2c 78 2e 72 4a 44 29 3b 0a  "%.16g",x.rJD);.
6be30 20 20 20 20 20 20 20 20 20 20 6a 2b 3d 73 74 72            j+=str
6be40 6c 65 6e 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20  len(&z[j]);.    
6be50 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
6be60 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63       }.        c
6be70 61 73 65 20 27 6d 27 3a 20 20 73 71 6c 69 74 65  ase 'm':  sqlite
6be80 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a  3_snprintf(3, &z
6be90 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 4d 29 3b  [j],"%02d",x.M);
6bea0 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20   j+=2; break;.  
6beb0 20 20 20 20 20 20 63 61 73 65 20 27 4d 27 3a 20        case 'M': 
6bec0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
6bed0 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64  f(3, &z[j],"%02d
6bee0 22 2c 78 2e 6d 29 3b 20 6a 2b 3d 32 3b 20 62 72  ",x.m); j+=2; br
6bef0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
6bf00 65 20 27 73 27 3a 20 7b 0a 20 20 20 20 20 20 20  e 's': {.       
6bf10 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
6bf20 6e 74 66 28 33 30 2c 26 7a 5b 6a 5d 2c 22 25 64  ntf(30,&z[j],"%d
6bf30 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
6bf40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69                (i
6bf50 6e 74 29 28 28 78 2e 72 4a 44 2d 32 34 34 30 35  nt)((x.rJD-24405
6bf60 38 37 2e 35 29 2a 38 36 34 30 30 2e 30 20 2b 20  87.5)*86400.0 + 
6bf70 30 2e 35 29 29 3b 0a 20 20 20 20 20 20 20 20 20  0.5));.         
6bf80 20 6a 20 2b 3d 20 73 74 72 6c 65 6e 28 26 7a 5b   j += strlen(&z[
6bf90 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62  j]);.          b
6bfa0 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
6bfb0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 53 27          case 'S'
6bfc0 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  :  sqlite3_snpri
6bfd0 6e 74 66 28 33 2c 26 7a 5b 6a 5d 2c 22 25 30 32  ntf(3,&z[j],"%02
6bfe0 64 22 2c 28 69 6e 74 29 78 2e 73 29 3b 20 6a 2b  d",(int)x.s); j+
6bff0 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  =2; break;.     
6c000 20 20 20 63 61 73 65 20 27 77 27 3a 20 20 7a 5b     case 'w':  z[
6c010 6a 2b 2b 5d 20 3d 20 28 28 28 69 6e 74 29 28 78  j++] = (((int)(x
6c020 2e 72 4a 44 2b 31 2e 35 29 29 20 25 20 37 29 20  .rJD+1.5)) % 7) 
6c030 2b 20 27 30 27 3b 20 62 72 65 61 6b 3b 0a 20 20  + '0'; break;.  
6c040 20 20 20 20 20 20 63 61 73 65 20 27 59 27 3a 20        case 'Y': 
6c050 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
6c060 66 28 35 2c 26 7a 5b 6a 5d 2c 22 25 30 34 64 22  f(5,&z[j],"%04d"
6c070 2c 78 2e 59 29 3b 20 6a 2b 3d 73 74 72 6c 65 6e  ,x.Y); j+=strlen
6c080 28 26 7a 5b 6a 5d 29 3b 62 72 65 61 6b 3b 0a 20  (&z[j]);break;. 
6c090 20 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20         default: 
6c0a0 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 25 27 3b 20    z[j++] = '%'; 
6c0b0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
6c0c0 20 20 20 7d 0a 20 20 7d 0a 20 20 7a 5b 6a 5d 20     }.  }.  z[j] 
6c0d0 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72  = 0;.  sqlite3_r
6c0e0 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
6c0f0 78 74 2c 20 7a 2c 20 2d 31 2c 0a 20 20 20 20 20  xt, z, -1,.     
6c100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c110 20 7a 3d 3d 7a 42 75 66 20 3f 20 53 51 4c 49 54   z==zBuf ? SQLIT
6c120 45 5f 54 52 41 4e 53 49 45 4e 54 20 3a 20 73 71  E_TRANSIENT : sq
6c130 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 7d 0a 0a  lite3_free);.}..
6c140 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 74 69  /*.** current_ti
6c150 6d 65 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  me().**.** This 
6c160 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
6c170 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20   the same value 
6c180 61 73 20 74 69 6d 65 28 27 6e 6f 77 27 29 2e 0a  as time('now')..
6c190 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
6c1a0 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69  timeFunc(.  sqli
6c1b0 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
6c1c0 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
6c1d0 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
6c1e0 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 74 69  e **argv.){.  ti
6c1f0 6d 65 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20  meFunc(context, 
6c200 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  0, 0);.}../*.** 
6c210 63 75 72 72 65 6e 74 5f 64 61 74 65 28 29 0a 2a  current_date().*
6c220 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
6c230 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73  on returns the s
6c240 61 6d 65 20 76 61 6c 75 65 20 61 73 20 64 61 74  ame value as dat
6c250 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61  e('now')..*/.sta
6c260 74 69 63 20 76 6f 69 64 20 63 64 61 74 65 46 75  tic void cdateFu
6c270 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
6c280 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
6c290 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
6c2a0 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
6c2b0 67 76 0a 29 7b 0a 20 20 64 61 74 65 46 75 6e 63  gv.){.  dateFunc
6c2c0 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b  (context, 0, 0);
6c2d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e  .}../*.** curren
6c2e0 74 5f 74 69 6d 65 73 74 61 6d 70 28 29 0a 2a 2a  t_timestamp().**
6c2f0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
6c300 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 61  n returns the sa
6c310 6d 65 20 76 61 6c 75 65 20 61 73 20 64 61 74 65  me value as date
6c320 74 69 6d 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a  time('now')..*/.
6c330 73 74 61 74 69 63 20 76 6f 69 64 20 63 74 69 6d  static void ctim
6c340 65 73 74 61 6d 70 46 75 6e 63 28 0a 20 20 73 71  estampFunc(.  sq
6c350 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
6c360 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
6c370 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
6c380 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
6c390 64 61 74 65 74 69 6d 65 46 75 6e 63 28 63 6f 6e  datetimeFunc(con
6c3a0 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 23  text, 0, 0);.}.#
6c3b0 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
6c3c0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41  d(SQLITE_OMIT_DA
6c3d0 54 45 54 49 4d 45 5f 46 55 4e 43 53 29 20 2a 2f  TETIME_FUNCS) */
6c3e0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
6c3f0 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55  OMIT_DATETIME_FU
6c400 4e 43 53 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  NCS./*.** If the
6c410 20 6c 69 62 72 61 72 79 20 69 73 20 63 6f 6d 70   library is comp
6c420 69 6c 65 64 20 74 6f 20 6f 6d 69 74 20 74 68 65  iled to omit the
6c430 20 66 75 6c 6c 2d 73 63 61 6c 65 20 64 61 74 65   full-scale date
6c440 20 61 6e 64 20 74 69 6d 65 0a 2a 2a 20 68 61 6e   and time.** han
6c450 64 6c 69 6e 67 20 28 74 6f 20 67 65 74 20 61 20  dling (to get a 
6c460 73 6d 61 6c 6c 65 72 20 62 69 6e 61 72 79 29 2c  smaller binary),
6c470 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   the following m
6c480 69 6e 69 6d 61 6c 20 76 65 72 73 69 6f 6e 0a 2a  inimal version.*
6c490 2a 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f  * of the functio
6c4a0 6e 73 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 28  ns current_time(
6c4b0 29 2c 20 63 75 72 72 65 6e 74 5f 64 61 74 65 28  ), current_date(
6c4c0 29 20 61 6e 64 20 63 75 72 72 65 6e 74 5f 74 69  ) and current_ti
6c4d0 6d 65 73 74 61 6d 70 28 29 0a 2a 2a 20 61 72 65  mestamp().** are
6c4e0 20 69 6e 63 6c 75 64 65 64 20 69 6e 73 74 65 61   included instea
6c4f0 64 2e 20 54 68 69 73 20 69 73 20 74 6f 20 73 75  d. This is to su
6c500 70 70 6f 72 74 20 63 6f 6c 75 6d 6e 20 64 65 63  pport column dec
6c510 6c 61 72 61 74 69 6f 6e 73 20 74 68 61 74 0a 2a  larations that.*
6c520 2a 20 69 6e 63 6c 75 64 65 20 22 44 45 46 41 55  * include "DEFAU
6c530 4c 54 20 43 55 52 52 45 4e 54 5f 54 49 4d 45 22  LT CURRENT_TIME"
6c540 20 65 74 63 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73   etc..**.** This
6c550 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 73 20 74   function uses t
6c560 68 65 20 43 2d 6c 69 62 72 61 72 79 20 66 75 6e  he C-library fun
6c570 63 74 69 6f 6e 73 20 74 69 6d 65 28 29 2c 20 67  ctions time(), g
6c580 6d 74 69 6d 65 28 29 0a 2a 2a 20 61 6e 64 20 73  mtime().** and s
6c590 74 72 66 74 69 6d 65 28 29 2e 20 54 68 65 20 66  trftime(). The f
6c5a0 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 74 6f 20  ormat string to 
6c5b0 70 61 73 73 20 74 6f 20 73 74 72 66 74 69 6d 65  pass to strftime
6c5c0 28 29 20 69 73 20 73 75 70 70 6c 69 65 64 0a 2a  () is supplied.*
6c5d0 2a 20 61 73 20 74 68 65 20 75 73 65 72 2d 64 61  * as the user-da
6c5e0 74 61 20 66 6f 72 20 74 68 65 20 66 75 6e 63 74  ta for the funct
6c5f0 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
6c600 6f 69 64 20 63 75 72 72 65 6e 74 54 69 6d 65 46  oid currentTimeF
6c610 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
6c620 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
6c630 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
6c640 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
6c650 72 67 76 0a 29 7b 0a 20 20 74 69 6d 65 5f 74 20  rgv.){.  time_t 
6c660 74 3b 0a 20 20 63 68 61 72 20 2a 7a 46 6f 72 6d  t;.  char *zForm
6c670 61 74 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c  at = (char *)sql
6c680 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63  ite3_user_data(c
6c690 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 74  ontext);.  sqlit
6c6a0 65 33 20 2a 64 62 3b 0a 20 20 64 6f 75 62 6c 65  e3 *db;.  double
6c6b0 20 72 54 3b 0a 20 20 63 68 61 72 20 7a 42 75 66   rT;.  char zBuf
6c6c0 5b 32 30 5d 3b 0a 0a 20 20 64 62 20 3d 20 73 71  [20];..  db = sq
6c6d0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
6c6e0 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29  _handle(context)
6c6f0 3b 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 75 72  ;.  sqlite3OsCur
6c700 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 66  rentTime(db->pVf
6c710 73 2c 20 26 72 54 29 3b 0a 20 20 74 20 3d 20 38  s, &rT);.  t = 8
6c720 36 34 30 30 2e 30 2a 28 72 54 20 2d 20 32 34 34  6400.0*(rT - 244
6c730 30 35 38 37 2e 35 29 20 2b 20 30 2e 35 3b 0a 23  0587.5) + 0.5;.#
6c740 69 66 64 65 66 20 48 41 56 45 5f 47 4d 54 49 4d  ifdef HAVE_GMTIM
6c750 45 5f 52 0a 20 20 7b 0a 20 20 20 20 73 74 72 75  E_R.  {.    stru
6c760 63 74 20 74 6d 20 73 4e 6f 77 3b 0a 20 20 20 20  ct tm sNow;.    
6c770 67 6d 74 69 6d 65 5f 72 28 26 74 2c 20 26 73 4e  gmtime_r(&t, &sN
6c780 6f 77 29 3b 0a 20 20 20 20 73 74 72 66 74 69 6d  ow);.    strftim
6c790 65 28 7a 42 75 66 2c 20 32 30 2c 20 7a 46 6f 72  e(zBuf, 20, zFor
6c7a0 6d 61 74 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 7d  mat, &sNow);.  }
6c7b0 0a 23 65 6c 73 65 0a 20 20 7b 0a 20 20 20 20 73  .#else.  {.    s
6c7c0 74 72 75 63 74 20 74 6d 20 2a 70 54 6d 3b 0a 20  truct tm *pTm;. 
6c7d0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
6c7e0 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d  _enter(sqlite3_m
6c7f0 75 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54  utex_alloc(SQLIT
6c800 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
6c810 41 53 54 45 52 29 29 3b 0a 20 20 20 20 70 54 6d  ASTER));.    pTm
6c820 20 3d 20 67 6d 74 69 6d 65 28 26 74 29 3b 0a 20   = gmtime(&t);. 
6c830 20 20 20 73 74 72 66 74 69 6d 65 28 7a 42 75 66     strftime(zBuf
6c840 2c 20 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 70  , 20, zFormat, p
6c850 54 6d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Tm);.    sqlite3
6c860 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c  _mutex_leave(sql
6c870 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
6c880 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
6c890 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20  ATIC_MASTER));. 
6c8a0 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c   }.#endif..  sql
6c8b0 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
6c8c0 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20  (context, zBuf, 
6c8d0 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  -1, SQLITE_TRANS
6c8e0 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  IENT);.}.#endif.
6c8f0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
6c900 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20  tion registered 
6c910 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 65  all of the above
6c920 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 61 73 20   C functions as 
6c930 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73  SQL.** functions
6c940 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62  .  This should b
6c950 65 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69  e the only routi
6c960 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  ne in this file 
6c970 77 69 74 68 0a 2a 2a 20 65 78 74 65 72 6e 61 6c  with.** external
6c980 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2f 0a 53 51 4c   linkage..*/.SQL
6c990 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6c9a0 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72   sqlite3Register
6c9b0 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e  DateTimeFunction
6c9c0 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  s(sqlite3 *db){.
6c9d0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
6c9e0 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e  MIT_DATETIME_FUN
6c9f0 43 53 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  CS.  static cons
6ca00 74 20 73 74 72 75 63 74 20 7b 0a 20 20 20 20 20  t struct {.     
6ca10 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 20  char *zName;.   
6ca20 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 20 20    int nArg;.    
6ca30 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73   void (*xFunc)(s
6ca40 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
6ca50 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
6ca60 65 2a 2a 29 3b 0a 20 20 7d 20 61 46 75 6e 63 73  e**);.  } aFuncs
6ca70 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22 6a 75  [] = {.    { "ju
6ca80 6c 69 61 6e 64 61 79 22 2c 20 2d 31 2c 20 6a 75  lianday", -1, ju
6ca90 6c 69 61 6e 64 61 79 46 75 6e 63 20 20 20 7d 2c  liandayFunc   },
6caa0 0a 20 20 20 20 7b 20 22 64 61 74 65 22 2c 20 20  .    { "date",  
6cab0 20 20 20 20 2d 31 2c 20 64 61 74 65 46 75 6e 63      -1, dateFunc
6cac0 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b          },.    {
6cad0 20 22 74 69 6d 65 22 2c 20 20 20 20 20 20 2d 31   "time",      -1
6cae0 2c 20 74 69 6d 65 46 75 6e 63 20 20 20 20 20 20  , timeFunc      
6caf0 20 20 7d 2c 0a 20 20 20 20 7b 20 22 64 61 74 65    },.    { "date
6cb00 74 69 6d 65 22 2c 20 20 2d 31 2c 20 64 61 74 65  time",  -1, date
6cb10 74 69 6d 65 46 75 6e 63 20 20 20 20 7d 2c 0a 20  timeFunc    },. 
6cb20 20 20 20 7b 20 22 73 74 72 66 74 69 6d 65 22 2c     { "strftime",
6cb30 20 20 2d 31 2c 20 73 74 72 66 74 69 6d 65 46 75    -1, strftimeFu
6cb40 6e 63 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22  nc    },.    { "
6cb50 63 75 72 72 65 6e 74 5f 74 69 6d 65 22 2c 20 20  current_time",  
6cb60 20 20 20 20 20 30 2c 20 63 74 69 6d 65 46 75 6e       0, ctimeFun
6cb70 63 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20  c      },.    { 
6cb80 22 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 61  "current_timesta
6cb90 6d 70 22 2c 20 20 30 2c 20 63 74 69 6d 65 73 74  mp",  0, ctimest
6cba0 61 6d 70 46 75 6e 63 20 7d 2c 0a 20 20 20 20 7b  ampFunc },.    {
6cbb0 20 22 63 75 72 72 65 6e 74 5f 64 61 74 65 22 2c   "current_date",
6cbc0 20 20 20 20 20 20 20 30 2c 20 63 64 61 74 65 46         0, cdateF
6cbd0 75 6e 63 20 20 20 20 20 20 7d 2c 0a 20 20 7d 3b  unc      },.  };
6cbe0 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 66 6f 72  .  int i;..  for
6cbf0 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61  (i=0; i<sizeof(a
6cc00 46 75 6e 63 73 29 2f 73 69 7a 65 6f 66 28 61 46  Funcs)/sizeof(aF
6cc10 75 6e 63 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a  uncs[0]); i++){.
6cc20 20 20 20 20 73 71 6c 69 74 65 33 43 72 65 61 74      sqlite3Creat
6cc30 65 46 75 6e 63 28 64 62 2c 20 61 46 75 6e 63 73  eFunc(db, aFuncs
6cc40 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 61 46 75 6e 63  [i].zName, aFunc
6cc50 73 5b 69 5d 2e 6e 41 72 67 2c 0a 20 20 20 20 20  s[i].nArg,.     
6cc60 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20     SQLITE_UTF8, 
6cc70 30 2c 20 61 46 75 6e 63 73 5b 69 5d 2e 78 46 75  0, aFuncs[i].xFu
6cc80 6e 63 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 23  nc, 0, 0);.  }.#
6cc90 65 6c 73 65 0a 20 20 73 74 61 74 69 63 20 63 6f  else.  static co
6cca0 6e 73 74 20 73 74 72 75 63 74 20 7b 0a 20 20 20  nst struct {.   
6ccb0 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20    char *zName;. 
6ccc0 20 20 20 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61      char *zForma
6ccd0 74 3b 0a 20 20 7d 20 61 46 75 6e 63 73 5b 5d 20  t;.  } aFuncs[] 
6cce0 3d 20 7b 0a 20 20 20 20 7b 20 22 63 75 72 72 65  = {.    { "curre
6ccf0 6e 74 5f 74 69 6d 65 22 2c 20 22 25 48 3a 25 4d  nt_time", "%H:%M
6cd00 3a 25 53 22 20 7d 2c 0a 20 20 20 20 7b 20 22 63  :%S" },.    { "c
6cd10 75 72 72 65 6e 74 5f 64 61 74 65 22 2c 20 22 25  urrent_date", "%
6cd20 59 2d 25 6d 2d 25 64 22 20 7d 2c 0a 20 20 20 20  Y-%m-%d" },.    
6cd30 7b 20 22 63 75 72 72 65 6e 74 5f 74 69 6d 65 73  { "current_times
6cd40 74 61 6d 70 22 2c 20 22 25 59 2d 25 6d 2d 25 64  tamp", "%Y-%m-%d
6cd50 20 25 48 3a 25 4d 3a 25 53 22 20 7d 0a 20 20 7d   %H:%M:%S" }.  }
6cd60 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 66 6f  ;.  int i;..  fo
6cd70 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28  r(i=0; i<sizeof(
6cd80 61 46 75 6e 63 73 29 2f 73 69 7a 65 6f 66 28 61  aFuncs)/sizeof(a
6cd90 46 75 6e 63 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b  Funcs[0]); i++){
6cda0 0a 20 20 20 20 73 71 6c 69 74 65 33 43 72 65 61  .    sqlite3Crea
6cdb0 74 65 46 75 6e 63 28 64 62 2c 20 61 46 75 6e 63  teFunc(db, aFunc
6cdc0 73 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 30 2c 20 53  s[i].zName, 0, S
6cdd0 51 4c 49 54 45 5f 55 54 46 38 2c 20 0a 20 20 20  QLITE_UTF8, .   
6cde0 20 20 20 20 20 61 46 75 6e 63 73 5b 69 5d 2e 7a       aFuncs[i].z
6cdf0 46 6f 72 6d 61 74 2c 20 63 75 72 72 65 6e 74 54  Format, currentT
6ce00 69 6d 65 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a  imeFunc, 0, 0);.
6ce10 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a    }.#endif.}../*
6ce20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
6ce30 64 20 6f 66 20 64 61 74 65 2e 63 20 2a 2a 2a 2a  d of date.c ****
6ce40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6ce50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6ce60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
6ce70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
6ce80 67 69 6e 20 66 69 6c 65 20 6f 73 2e 63 20 2a 2a  gin file os.c **
6ce90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6cea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6ceb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
6cec0 0a 2a 2a 20 32 30 30 35 20 4e 6f 76 65 6d 62 65  .** 2005 Novembe
6ced0 72 20 32 39 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 29.**.** The a
6cee0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
6cef0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
6cf00 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
6cf10 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
6cf20 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
6cf30 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
6cf40 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
6cf50 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
6cf60 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
6cf70 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
6cf80 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
6cf90 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
6cfa0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
6cfb0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
6cfc0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
6cfd0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
6cfe0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
6cff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6d000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6d010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6d020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6d030 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
6d040 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
6d050 73 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20 63  s OS interface c
6d060 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d  ode that is comm
6d070 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a 20 61 72 63  on to all.** arc
6d080 68 69 74 65 63 74 75 72 65 73 2e 0a 2a 2f 0a 23  hitectures..*/.#
6d090 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 4f  define _SQLITE_O
6d0a0 53 5f 43 5f 20 31 0a 23 75 6e 64 65 66 20 5f 53  S_C_ 1.#undef _S
6d0b0 51 4c 49 54 45 5f 4f 53 5f 43 5f 0a 0a 2f 2a 0a  QLITE_OS_C_../*.
6d0c0 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 53  ** The default S
6d0d0 51 4c 69 74 65 20 73 71 6c 69 74 65 33 5f 76 66  QLite sqlite3_vf
6d0e0 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  s implementation
6d0f0 73 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74  s do not allocat
6d100 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 28 61 63 74  e.** memory (act
6d110 75 61 6c 6c 79 2c 20 6f 73 5f 75 6e 69 78 2e 63  ually, os_unix.c
6d120 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 73 6d 61   allocates a sma
6d130 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d  ll amount of mem
6d140 6f 72 79 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68  ory.** from with
6d150 69 6e 20 4f 73 4f 70 65 6e 28 29 29 2c 20 62 75  in OsOpen()), bu
6d160 74 20 73 6f 6d 65 20 74 68 69 72 64 2d 70 61 72  t some third-par
6d170 74 79 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ty implementatio
6d180 6e 73 20 6d 61 79 2e 0a 2a 2a 20 53 6f 20 77 65  ns may..** So we
6d190 20 74 65 73 74 20 74 68 65 20 65 66 66 65 63 74   test the effect
6d1a0 73 20 6f 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20  s of a malloc() 
6d1b0 66 61 69 6c 69 6e 67 20 61 6e 64 20 74 68 65 20  failing and the 
6d1c0 73 71 6c 69 74 65 33 4f 73 58 58 58 28 29 0a 2a  sqlite3OsXXX().*
6d1d0 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  * function retur
6d1e0 6e 69 6e 67 20 53 51 4c 49 54 45 5f 49 4f 45 52  ning SQLITE_IOER
6d1f0 52 5f 4e 4f 4d 45 4d 20 75 73 69 6e 67 20 74 68  R_NOMEM using th
6d200 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54  e DO_OS_MALLOC_T
6d210 45 53 54 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a  EST macro..**.**
6d220 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   The following f
6d230 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 69 6e 73  unctions are ins
6d240 74 72 75 6d 65 6e 74 65 64 20 66 6f 72 20 6d 61  trumented for ma
6d250 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20 0a  lloc() failure .
6d260 2a 2a 20 74 65 73 74 69 6e 67 3a 0a 2a 2a 0a 2a  ** testing:.**.*
6d270 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f  *     sqlite3OsO
6d280 70 65 6e 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c  pen().**     sql
6d290 69 74 65 33 4f 73 52 65 61 64 28 29 0a 2a 2a 20  ite3OsRead().** 
6d2a0 20 20 20 20 73 71 6c 69 74 65 33 4f 73 57 72 69      sqlite3OsWri
6d2b0 74 65 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69  te().**     sqli
6d2c0 74 65 33 4f 73 53 79 6e 63 28 29 0a 2a 2a 20 20  te3OsSync().**  
6d2d0 20 20 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b     sqlite3OsLock
6d2e0 28 29 0a 2a 2a 0a 2a 2f 0a 23 69 66 20 64 65 66  ().**.*/.#if def
6d2f0 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
6d300 29 20 26 26 20 28 4f 53 5f 57 49 4e 3d 3d 30 29  ) && (OS_WIN==0)
6d310 0a 20 20 23 64 65 66 69 6e 65 20 44 4f 5f 4f 53  .  #define DO_OS
6d320 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 20 69 66 20  _MALLOC_TEST if 
6d330 28 31 29 20 7b 20 20 20 20 20 20 20 20 20 20 20  (1) {           
6d340 20 5c 0a 20 20 20 20 76 6f 69 64 20 2a 70 54 73   \.    void *pTs
6d350 74 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33  tAlloc = sqlite3
6d360 5f 6d 61 6c 6c 6f 63 28 31 30 29 3b 20 20 20 20  _malloc(10);    
6d370 20 20 20 5c 0a 20 20 20 20 69 66 20 28 21 70 54     \.    if (!pT
6d380 73 74 41 6c 6c 6f 63 29 20 72 65 74 75 72 6e 20  stAlloc) return 
6d390 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d  SQLITE_IOERR_NOM
6d3a0 45 4d 3b 20 20 5c 0a 20 20 20 20 73 71 6c 69 74  EM;  \.    sqlit
6d3b0 65 33 5f 66 72 65 65 28 70 54 73 74 41 6c 6c 6f  e3_free(pTstAllo
6d3c0 63 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  c);             
6d3d0 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65 6c         \.  }.#el
6d3e0 73 65 0a 20 20 23 64 65 66 69 6e 65 20 44 4f 5f  se.  #define DO_
6d3f0 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 0a 23  OS_MALLOC_TEST.#
6d400 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
6d410 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69   following routi
6d420 6e 65 73 20 61 72 65 20 63 6f 6e 76 65 6e 69 65  nes are convenie
6d430 6e 63 65 20 77 72 61 70 70 65 72 73 20 61 72 6f  nce wrappers aro
6d440 75 6e 64 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f  und methods.** o
6d450 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69  f the sqlite3_fi
6d460 6c 65 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73  le object.  This
6d470 20 69 73 20 6d 6f 73 74 6c 79 20 6a 75 73 74 20   is mostly just 
6d480 73 79 6e 74 61 63 74 69 63 20 73 75 67 61 72 2e  syntactic sugar.
6d490 20 41 6c 6c 0a 2a 2a 20 6f 66 20 74 68 69 73 20   All.** of this 
6d4a0 77 6f 75 6c 64 20 62 65 20 63 6f 6d 70 6c 65 74  would be complet
6d4b0 65 6c 79 20 61 75 74 6f 6d 61 74 69 63 20 69 66  ely automatic if
6d4c0 20 53 51 4c 69 74 65 20 77 65 72 65 20 63 6f 64   SQLite were cod
6d4d0 65 64 20 75 73 69 6e 67 0a 2a 2a 20 43 2b 2b 20  ed using.** C++ 
6d4e0 69 6e 73 74 65 61 64 20 6f 66 20 70 6c 61 69 6e  instead of plain
6d4f0 20 6f 6c 64 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54   old C..*/.SQLIT
6d500 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6d510 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 73 71 6c  lite3OsClose(sql
6d520 69 74 65 33 5f 66 69 6c 65 20 2a 70 49 64 29 7b  ite3_file *pId){
6d530 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
6d540 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 49 64  TE_OK;.  if( pId
6d550 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20  ->pMethods ){.  
6d560 20 20 72 63 20 3d 20 70 49 64 2d 3e 70 4d 65 74    rc = pId->pMet
6d570 68 6f 64 73 2d 3e 78 43 6c 6f 73 65 28 70 49 64  hods->xClose(pId
6d580 29 3b 0a 20 20 20 20 70 49 64 2d 3e 70 4d 65 74  );.    pId->pMet
6d590 68 6f 64 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  hods = 0;.  }.  
6d5a0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c  return rc;.}.SQL
6d5b0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6d5c0 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 73 71  sqlite3OsRead(sq
6d5d0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
6d5e0 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20  void *pBuf, int 
6d5f0 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29  amt, i64 offset)
6d600 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43  {.  DO_OS_MALLOC
6d610 5f 54 45 53 54 3b 0a 20 20 72 65 74 75 72 6e 20  _TEST;.  return 
6d620 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 52  id->pMethods->xR
6d630 65 61 64 28 69 64 2c 20 70 42 75 66 2c 20 61 6d  ead(id, pBuf, am
6d640 74 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 53 51  t, offset);.}.SQ
6d650 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6d660 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
6d670 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
6d680 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42  , const void *pB
6d690 75 66 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 34  uf, int amt, i64
6d6a0 20 6f 66 66 73 65 74 29 7b 0a 20 20 44 4f 5f 4f   offset){.  DO_O
6d6b0 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20  S_MALLOC_TEST;. 
6d6c0 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74   return id->pMet
6d6d0 68 6f 64 73 2d 3e 78 57 72 69 74 65 28 69 64 2c  hods->xWrite(id,
6d6e0 20 70 42 75 66 2c 20 61 6d 74 2c 20 6f 66 66 73   pBuf, amt, offs
6d6f0 65 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  et);.}.SQLITE_PR
6d700 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6d710 33 4f 73 54 72 75 6e 63 61 74 65 28 73 71 6c 69  3OsTruncate(sqli
6d720 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36  te3_file *id, i6
6d730 34 20 73 69 7a 65 29 7b 0a 20 20 72 65 74 75 72  4 size){.  retur
6d740 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e  n id->pMethods->
6d750 78 54 72 75 6e 63 61 74 65 28 69 64 2c 20 73 69  xTruncate(id, si
6d760 7a 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  ze);.}.SQLITE_PR
6d770 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6d780 33 4f 73 53 79 6e 63 28 73 71 6c 69 74 65 33 5f  3OsSync(sqlite3_
6d790 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c  file *id, int fl
6d7a0 61 67 73 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41  ags){.  DO_OS_MA
6d7b0 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 74  LLOC_TEST;.  ret
6d7c0 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73  urn id->pMethods
6d7d0 2d 3e 78 53 79 6e 63 28 69 64 2c 20 66 6c 61 67  ->xSync(id, flag
6d7e0 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  s);.}.SQLITE_PRI
6d7f0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6d800 4f 73 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74  OsFileSize(sqlit
6d810 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34  e3_file *id, i64
6d820 20 2a 70 53 69 7a 65 29 7b 0a 20 20 72 65 74 75   *pSize){.  retu
6d830 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d  rn id->pMethods-
6d840 3e 78 46 69 6c 65 53 69 7a 65 28 69 64 2c 20 70  >xFileSize(id, p
6d850 53 69 7a 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  Size);.}.SQLITE_
6d860 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6d870 74 65 33 4f 73 4c 6f 63 6b 28 73 71 6c 69 74 65  te3OsLock(sqlite
6d880 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
6d890 6c 6f 63 6b 54 79 70 65 29 7b 0a 20 20 44 4f 5f  lockType){.  DO_
6d8a0 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a  OS_MALLOC_TEST;.
6d8b0 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65    return id->pMe
6d8c0 74 68 6f 64 73 2d 3e 78 4c 6f 63 6b 28 69 64 2c  thods->xLock(id,
6d8d0 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 7d 0a 53 51   lockType);.}.SQ
6d8e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6d8f0 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b   sqlite3OsUnlock
6d900 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
6d910 64 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65 29  d, int lockType)
6d920 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70  {.  return id->p
6d930 4d 65 74 68 6f 64 73 2d 3e 78 55 6e 6c 6f 63 6b  Methods->xUnlock
6d940 28 69 64 2c 20 6c 6f 63 6b 54 79 70 65 29 3b 0a  (id, lockType);.
6d950 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
6d960 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 68   int sqlite3OsCh
6d970 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
6d980 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
6d990 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e  ){.  return id->
6d9a0 70 4d 65 74 68 6f 64 73 2d 3e 78 43 68 65 63 6b  pMethods->xCheck
6d9b0 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 69 64 29  ReservedLock(id)
6d9c0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
6d9d0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
6d9e0 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69  FileControl(sqli
6d9f0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
6da00 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67  t op, void *pArg
6da10 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e  ){.  return id->
6da20 70 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65 43  pMethods->xFileC
6da30 6f 6e 74 72 6f 6c 28 69 64 2c 6f 70 2c 70 41 72  ontrol(id,op,pAr
6da40 67 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  g);.}.SQLITE_PRI
6da50 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6da60 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c  OsSectorSize(sql
6da70 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a  ite3_file *id){.
6da80 20 20 69 6e 74 20 28 2a 78 53 65 63 74 6f 72 53    int (*xSectorS
6da90 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c  ize)(sqlite3_fil
6daa0 65 2a 29 20 3d 20 69 64 2d 3e 70 4d 65 74 68 6f  e*) = id->pMetho
6dab0 64 73 2d 3e 78 53 65 63 74 6f 72 53 69 7a 65 3b  ds->xSectorSize;
6dac0 0a 20 20 72 65 74 75 72 6e 20 28 78 53 65 63 74  .  return (xSect
6dad0 6f 72 53 69 7a 65 20 3f 20 78 53 65 63 74 6f 72  orSize ? xSector
6dae0 53 69 7a 65 28 69 64 29 20 3a 20 53 51 4c 49 54  Size(id) : SQLIT
6daf0 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52  E_DEFAULT_SECTOR
6db00 5f 53 49 5a 45 29 3b 0a 7d 0a 53 51 4c 49 54 45  _SIZE);.}.SQLITE
6db10 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6db20 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72  ite3OsDeviceChar
6db30 61 63 74 65 72 69 73 74 69 63 73 28 73 71 6c 69  acteristics(sqli
6db40 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20  te3_file *id){. 
6db50 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74   return id->pMet
6db60 68 6f 64 73 2d 3e 78 44 65 76 69 63 65 43 68 61  hods->xDeviceCha
6db70 72 61 63 74 65 72 69 73 74 69 63 73 28 69 64 29  racteristics(id)
6db80 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e  ;.}../*.** The n
6db90 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75  ext group of rou
6dba0 74 69 6e 65 73 20 61 72 65 20 63 6f 6e 76 65 6e  tines are conven
6dbb0 69 65 6e 63 65 20 77 72 61 70 70 65 72 73 20 61  ience wrappers a
6dbc0 72 6f 75 6e 64 20 74 68 65 0a 2a 2a 20 56 46 53  round the.** VFS
6dbd0 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c   methods..*/.SQL
6dbe0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6dbf0 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 0a 20  sqlite3OsOpen(. 
6dc00 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
6dc10 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  fs, .  const cha
6dc20 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 73 71 6c  r *zPath, .  sql
6dc30 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65  ite3_file *pFile
6dc40 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20  , .  int flags, 
6dc50 0a 20 20 69 6e 74 20 2a 70 46 6c 61 67 73 4f 75  .  int *pFlagsOu
6dc60 74 0a 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c  t.){.  DO_OS_MAL
6dc70 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 74 75  LOC_TEST;.  retu
6dc80 72 6e 20 70 56 66 73 2d 3e 78 4f 70 65 6e 28 70  rn pVfs->xOpen(p
6dc90 56 66 73 2c 20 7a 50 61 74 68 2c 20 70 46 69 6c  Vfs, zPath, pFil
6dca0 65 2c 20 66 6c 61 67 73 2c 20 70 46 6c 61 67 73  e, flags, pFlags
6dcb0 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  Out);.}.SQLITE_P
6dcc0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6dcd0 65 33 4f 73 44 65 6c 65 74 65 28 73 71 6c 69 74  e3OsDelete(sqlit
6dce0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63 6f  e3_vfs *pVfs, co
6dcf0 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c  nst char *zPath,
6dd00 20 69 6e 74 20 64 69 72 53 79 6e 63 29 7b 0a 20   int dirSync){. 
6dd10 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 44   return pVfs->xD
6dd20 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 50 61 74  elete(pVfs, zPat
6dd30 68 2c 20 64 69 72 53 79 6e 63 29 3b 0a 7d 0a 53  h, dirSync);.}.S
6dd40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6dd50 74 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73  t sqlite3OsAcces
6dd60 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  s(sqlite3_vfs *p
6dd70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  Vfs, const char 
6dd80 2a 7a 50 61 74 68 2c 20 69 6e 74 20 66 6c 61 67  *zPath, int flag
6dd90 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 23 69  s){.  int rc;.#i
6dda0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
6ddb0 0a 20 20 76 6f 69 64 20 2a 70 54 73 74 41 6c 6c  .  void *pTstAll
6ddc0 6f 63 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  oc = sqlite3_mal
6ddd0 6c 6f 63 28 31 30 29 3b 0a 20 20 69 66 20 28 21  loc(10);.  if (!
6dde0 70 54 73 74 41 6c 6c 6f 63 29 20 72 65 74 75 72  pTstAlloc) retur
6ddf0 6e 20 2d 31 3b 0a 20 20 73 71 6c 69 74 65 33 5f  n -1;.  sqlite3_
6de00 66 72 65 65 28 70 54 73 74 41 6c 6c 6f 63 29 3b  free(pTstAlloc);
6de10 0a 23 65 6e 64 69 66 0a 20 20 72 63 20 3d 20 70  .#endif.  rc = p
6de20 56 66 73 2d 3e 78 41 63 63 65 73 73 28 70 56 66  Vfs->xAccess(pVf
6de30 73 2c 20 7a 50 61 74 68 2c 20 66 6c 61 67 73 29  s, zPath, flags)
6de40 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
6de50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6de60 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 47 65 74  int sqlite3OsGet
6de70 54 65 6d 70 6e 61 6d 65 28 73 71 6c 69 74 65 33  Tempname(sqlite3
6de80 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20  _vfs *pVfs, int 
6de90 6e 42 75 66 4f 75 74 2c 20 63 68 61 72 20 2a 7a  nBufOut, char *z
6dea0 42 75 66 4f 75 74 29 7b 0a 20 20 72 65 74 75 72  BufOut){.  retur
6deb0 6e 20 70 56 66 73 2d 3e 78 47 65 74 54 65 6d 70  n pVfs->xGetTemp
6dec0 6e 61 6d 65 28 70 56 66 73 2c 20 6e 42 75 66 4f  name(pVfs, nBufO
6ded0 75 74 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a  ut, zBufOut);.}.
6dee0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6def0 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c  nt sqlite3OsFull
6df00 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69  Pathname(.  sqli
6df10 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a  te3_vfs *pVfs, .
6df20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50    const char *zP
6df30 61 74 68 2c 20 0a 20 20 69 6e 74 20 6e 50 61 74  ath, .  int nPat
6df40 68 4f 75 74 2c 20 0a 20 20 63 68 61 72 20 2a 7a  hOut, .  char *z
6df50 50 61 74 68 4f 75 74 0a 29 7b 0a 20 20 72 65 74  PathOut.){.  ret
6df60 75 72 6e 20 70 56 66 73 2d 3e 78 46 75 6c 6c 50  urn pVfs->xFullP
6df70 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 50  athname(pVfs, zP
6df80 61 74 68 2c 20 6e 50 61 74 68 4f 75 74 2c 20 7a  ath, nPathOut, z
6df90 50 61 74 68 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49  PathOut);.}.SQLI
6dfa0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6dfb0 2a 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e  *sqlite3OsDlOpen
6dfc0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
6dfd0 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  fs, const char *
6dfe0 7a 50 61 74 68 29 7b 0a 20 20 72 65 74 75 72 6e  zPath){.  return
6dff0 20 70 56 66 73 2d 3e 78 44 6c 4f 70 65 6e 28 70   pVfs->xDlOpen(p
6e000 56 66 73 2c 20 7a 50 61 74 68 29 3b 0a 7d 0a 53  Vfs, zPath);.}.S
6e010 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6e020 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72  id sqlite3OsDlEr
6e030 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ror(sqlite3_vfs 
6e040 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 79 74 65  *pVfs, int nByte
6e050 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29  , char *zBufOut)
6e060 7b 0a 20 20 70 56 66 73 2d 3e 78 44 6c 45 72 72  {.  pVfs->xDlErr
6e070 6f 72 28 70 56 66 73 2c 20 6e 42 79 74 65 2c 20  or(pVfs, nByte, 
6e080 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49  zBufOut);.}.SQLI
6e090 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6e0a0 2a 73 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28  *sqlite3OsDlSym(
6e0b0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
6e0c0 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65  s, void *pHandle
6e0d0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53  , const char *zS
6e0e0 79 6d 62 6f 6c 29 7b 0a 20 20 72 65 74 75 72 6e  ymbol){.  return
6e0f0 20 70 56 66 73 2d 3e 78 44 6c 53 79 6d 28 70 56   pVfs->xDlSym(pV
6e100 66 73 2c 20 70 48 61 6e 64 6c 65 2c 20 7a 53 79  fs, pHandle, zSy
6e110 6d 62 6f 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  mbol);.}.SQLITE_
6e120 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6e130 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28 73 71  ite3OsDlClose(sq
6e140 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
6e150 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b   void *pHandle){
6e160 0a 20 20 70 56 66 73 2d 3e 78 44 6c 43 6c 6f 73  .  pVfs->xDlClos
6e170 65 28 70 56 66 73 2c 20 70 48 61 6e 64 6c 65 29  e(pVfs, pHandle)
6e180 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
6e190 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
6e1a0 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74  Randomness(sqlit
6e1b0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e  e3_vfs *pVfs, in
6e1c0 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a  t nByte, char *z
6e1d0 42 75 66 4f 75 74 29 7b 0a 20 20 72 65 74 75 72  BufOut){.  retur
6e1e0 6e 20 70 56 66 73 2d 3e 78 52 61 6e 64 6f 6d 6e  n pVfs->xRandomn
6e1f0 65 73 73 28 70 56 66 73 2c 20 6e 42 79 74 65 2c  ess(pVfs, nByte,
6e200 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a 53 51 4c   zBufOut);.}.SQL
6e210 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6e220 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 73  sqlite3OsSleep(s
6e230 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
6e240 2c 20 69 6e 74 20 6e 4d 69 63 72 6f 29 7b 0a 20  , int nMicro){. 
6e250 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 53   return pVfs->xS
6e260 6c 65 65 70 28 70 56 66 73 2c 20 6e 4d 69 63 72  leep(pVfs, nMicr
6e270 6f 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  o);.}.SQLITE_PRI
6e280 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6e290 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 73 71  OsCurrentTime(sq
6e2a0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
6e2b0 20 64 6f 75 62 6c 65 20 2a 70 54 69 6d 65 4f 75   double *pTimeOu
6e2c0 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66  t){.  return pVf
6e2d0 73 2d 3e 78 43 75 72 72 65 6e 74 54 69 6d 65 28  s->xCurrentTime(
6e2e0 70 56 66 73 2c 20 70 54 69 6d 65 4f 75 74 29 3b  pVfs, pTimeOut);
6e2f0 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .}..SQLITE_PRIVA
6e300 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
6e310 4f 70 65 6e 4d 61 6c 6c 6f 63 28 0a 20 20 73 71  OpenMalloc(.  sq
6e320 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
6e330 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   .  const char *
6e340 7a 46 69 6c 65 2c 20 0a 20 20 73 71 6c 69 74 65  zFile, .  sqlite
6e350 33 5f 66 69 6c 65 20 2a 2a 70 70 46 69 6c 65 2c  3_file **ppFile,
6e360 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20   .  int flags,. 
6e370 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 0a   int *pOutFlags.
6e380 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
6e390 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 71  LITE_NOMEM;.  sq
6e3a0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c  lite3_file *pFil
6e3b0 65 3b 0a 20 20 70 46 69 6c 65 20 3d 20 28 73 71  e;.  pFile = (sq
6e3c0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c  lite3_file *)sql
6e3d0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 70 56 66 73  ite3_malloc(pVfs
6e3e0 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a 20 20 69  ->szOsFile);.  i
6e3f0 66 28 20 70 46 69 6c 65 20 29 7b 0a 20 20 20 20  f( pFile ){.    
6e400 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70  rc = sqlite3OsOp
6e410 65 6e 28 70 56 66 73 2c 20 7a 46 69 6c 65 2c 20  en(pVfs, zFile, 
6e420 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 70 4f  pFile, flags, pO
6e430 75 74 46 6c 61 67 73 29 3b 0a 20 20 20 20 69 66  utFlags);.    if
6e440 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
6e450 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
6e460 5f 66 72 65 65 28 70 46 69 6c 65 29 3b 0a 20 20  _free(pFile);.  
6e470 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a    }else{.      *
6e480 70 70 46 69 6c 65 20 3d 20 70 46 69 6c 65 3b 0a  ppFile = pFile;.
6e490 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
6e4a0 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f  rn rc;.}.SQLITE_
6e4b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6e4c0 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 73  te3OsCloseFree(s
6e4d0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69  qlite3_file *pFi
6e4e0 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  le){.  int rc = 
6e4f0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73  SQLITE_OK;.  ass
6e500 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20  ert( pFile );.  
6e510 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 43 6c  rc = sqlite3OsCl
6e520 6f 73 65 28 70 46 69 6c 65 29 3b 0a 20 20 73 71  ose(pFile);.  sq
6e530 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65  lite3_free(pFile
6e540 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
6e550 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6c 69 73  }../*.** The lis
6e560 74 20 6f 66 20 61 6c 6c 20 72 65 67 69 73 74 65  t of all registe
6e570 72 65 64 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e  red VFS implemen
6e580 74 61 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 6c  tations.  This l
6e590 69 73 74 20 69 73 0a 2a 2a 20 69 6e 69 74 69 61  ist is.** initia
6e5a0 6c 69 7a 65 64 20 74 6f 20 74 68 65 20 73 69 6e  lized to the sin
6e5b0 67 6c 65 20 56 46 53 20 72 65 74 75 72 6e 65 64  gle VFS returned
6e5c0 20 62 79 20 73 71 6c 69 74 65 33 4f 73 44 65 66   by sqlite3OsDef
6e5d0 61 75 6c 74 56 66 73 28 29 0a 2a 2a 20 75 70 6f  aultVfs().** upo
6e5e0 6e 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c  n the first call
6e5f0 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 66 73 5f   to sqlite3_vfs_
6e600 66 69 6e 64 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  find()..*/.stati
6e610 63 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 76  c sqlite3_vfs *v
6e620 66 73 4c 69 73 74 20 3d 20 30 3b 0a 0a 2f 2a 0a  fsList = 0;../*.
6e630 2a 2a 20 4c 6f 63 61 74 65 20 61 20 56 46 53 20  ** Locate a VFS 
6e640 62 79 20 6e 61 6d 65 2e 20 20 49 66 20 6e 6f 20  by name.  If no 
6e650 6e 61 6d 65 20 69 73 20 67 69 76 65 6e 2c 20 73  name is given, s
6e660 69 6d 70 6c 79 20 72 65 74 75 72 6e 20 74 68 65  imply return the
6e670 0a 2a 2a 20 66 69 72 73 74 20 56 46 53 20 6f 6e  .** first VFS on
6e680 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51   the list..*/.SQ
6e690 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
6e6a0 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 5f 76 66  _vfs *sqlite3_vf
6e6b0 73 5f 66 69 6e 64 28 63 6f 6e 73 74 20 63 68 61  s_find(const cha
6e6c0 72 20 2a 7a 56 66 73 29 7b 0a 23 69 66 6e 64 65  r *zVfs){.#ifnde
6e6d0 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e  f SQLITE_MUTEX_N
6e6e0 4f 4f 50 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  OOP.  sqlite3_mu
6e6f0 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c  tex *mutex = sql
6e700 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
6e710 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
6e720 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65  ATIC_MASTER);.#e
6e730 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 76  ndif.  sqlite3_v
6e740 66 73 20 2a 70 56 66 73 20 3d 20 30 3b 0a 20 20  fs *pVfs = 0;.  
6e750 73 74 61 74 69 63 20 69 6e 74 20 69 73 49 6e 69  static int isIni
6e760 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  t = 0;.  sqlite3
6e770 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74  _mutex_enter(mut
6e780 65 78 29 3b 0a 20 20 69 66 28 20 21 69 73 49 6e  ex);.  if( !isIn
6e790 69 74 20 29 7b 0a 20 20 20 20 76 66 73 4c 69 73  it ){.    vfsLis
6e7a0 74 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 66  t = sqlite3OsDef
6e7b0 61 75 6c 74 56 66 73 28 29 3b 0a 20 20 20 20 69  aultVfs();.    i
6e7c0 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a 20  sInit = 1;.  }. 
6e7d0 20 66 6f 72 28 70 56 66 73 20 3d 20 76 66 73 4c   for(pVfs = vfsL
6e7e0 69 73 74 3b 20 70 56 66 73 3b 20 70 56 66 73 3d  ist; pVfs; pVfs=
6e7f0 70 56 66 73 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pVfs->pNext){.  
6e800 20 20 69 66 28 20 7a 56 66 73 3d 3d 30 20 29 20    if( zVfs==0 ) 
6e810 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73  break;.    if( s
6e820 74 72 63 6d 70 28 7a 56 66 73 2c 20 70 56 66 73  trcmp(zVfs, pVfs
6e830 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72  ->zName)==0 ) br
6e840 65 61 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  eak;.  }.  sqlit
6e850 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
6e860 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
6e870 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55  pVfs;.}../*.** U
6e880 6e 6c 69 6e 6b 20 61 20 56 46 53 20 66 72 6f 6d  nlink a VFS from
6e890 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74   the linked list
6e8a0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
6e8b0 76 66 73 55 6e 6c 69 6e 6b 28 73 71 6c 69 74 65  vfsUnlink(sqlite
6e8c0 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 20 20  3_vfs *pVfs){.  
6e8d0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
6e8e0 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74  mutex_held(sqlit
6e8f0 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53  e3_mutex_alloc(S
6e900 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
6e910 49 43 5f 4d 41 53 54 45 52 29 29 20 29 3b 0a 20  IC_MASTER)) );. 
6e920 20 69 66 28 20 70 56 66 73 3d 3d 30 20 29 7b 0a   if( pVfs==0 ){.
6e930 20 20 20 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a      /* No-op */.
6e940 20 20 7d 65 6c 73 65 20 69 66 28 20 76 66 73 4c    }else if( vfsL
6e950 69 73 74 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20  ist==pVfs ){.   
6e960 20 76 66 73 4c 69 73 74 20 3d 20 70 56 66 73 2d   vfsList = pVfs-
6e970 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 20  >pNext;.  }else 
6e980 69 66 28 20 76 66 73 4c 69 73 74 20 29 7b 0a 20  if( vfsList ){. 
6e990 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a     sqlite3_vfs *
6e9a0 70 20 3d 20 76 66 73 4c 69 73 74 3b 0a 20 20 20  p = vfsList;.   
6e9b0 20 77 68 69 6c 65 28 20 70 2d 3e 70 4e 65 78 74   while( p->pNext
6e9c0 20 26 26 20 70 2d 3e 70 4e 65 78 74 21 3d 70 56   && p->pNext!=pV
6e9d0 66 73 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20  fs ){.      p = 
6e9e0 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a  p->pNext;.    }.
6e9f0 20 20 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 74      if( p->pNext
6ea00 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20  ==pVfs ){.      
6ea10 70 2d 3e 70 4e 65 78 74 20 3d 20 70 56 66 73 2d  p->pNext = pVfs-
6ea20 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20  >pNext;.    }.  
6ea30 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73  }.}../*.** Regis
6ea40 74 65 72 20 61 20 56 46 53 20 77 69 74 68 20 74  ter a VFS with t
6ea50 68 65 20 73 79 73 74 65 6d 2e 20 20 49 74 20 69  he system.  It i
6ea60 73 20 68 61 72 6d 6c 65 73 73 20 74 6f 20 72 65  s harmless to re
6ea70 67 69 73 74 65 72 20 74 68 65 20 73 61 6d 65 0a  gister the same.
6ea80 2a 2a 20 56 46 53 20 6d 75 6c 74 69 70 6c 65 20  ** VFS multiple 
6ea90 74 69 6d 65 73 2e 20 20 54 68 65 20 6e 65 77 20  times.  The new 
6eaa0 56 46 53 20 62 65 63 6f 6d 65 73 20 74 68 65 20  VFS becomes the 
6eab0 64 65 66 61 75 6c 74 20 69 66 20 6d 61 6b 65 44  default if makeD
6eac0 66 6c 74 20 69 73 0a 2a 2a 20 74 72 75 65 2e 0a  flt is.** true..
6ead0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
6eae0 74 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65  t sqlite3_vfs_re
6eaf0 67 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76  gister(sqlite3_v
6eb00 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d 61  fs *pVfs, int ma
6eb10 6b 65 44 66 6c 74 29 7b 0a 23 69 66 6e 64 65 66  keDflt){.#ifndef
6eb20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f   SQLITE_MUTEX_NO
6eb30 4f 50 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  OP.  sqlite3_mut
6eb40 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69  ex *mutex = sqli
6eb50 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
6eb60 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
6eb70 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e  TIC_MASTER);.#en
6eb80 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  dif.  sqlite3_vf
6eb90 73 5f 66 69 6e 64 28 30 29 3b 20 20 2f 2a 20 4d  s_find(0);  /* M
6eba0 61 6b 65 20 73 75 72 65 20 77 65 20 61 72 65 20  ake sure we are 
6ebb0 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20  initialized */. 
6ebc0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
6ebd0 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 76  nter(mutex);.  v
6ebe0 66 73 55 6e 6c 69 6e 6b 28 70 56 66 73 29 3b 0a  fsUnlink(pVfs);.
6ebf0 20 20 69 66 28 20 6d 61 6b 65 44 66 6c 74 20 7c    if( makeDflt |
6ec00 7c 20 76 66 73 4c 69 73 74 3d 3d 30 20 29 7b 0a  | vfsList==0 ){.
6ec10 20 20 20 20 70 56 66 73 2d 3e 70 4e 65 78 74 20      pVfs->pNext 
6ec20 3d 20 76 66 73 4c 69 73 74 3b 0a 20 20 20 20 76  = vfsList;.    v
6ec30 66 73 4c 69 73 74 20 3d 20 70 56 66 73 3b 0a 20  fsList = pVfs;. 
6ec40 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 56 66 73   }else{.    pVfs
6ec50 2d 3e 70 4e 65 78 74 20 3d 20 76 66 73 4c 69 73  ->pNext = vfsLis
6ec60 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 76 66  t->pNext;.    vf
6ec70 73 4c 69 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70  sList->pNext = p
6ec80 56 66 73 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  Vfs;.  }.  asser
6ec90 74 28 76 66 73 4c 69 73 74 29 3b 0a 20 20 73 71  t(vfsList);.  sq
6eca0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
6ecb0 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  e(mutex);.  retu
6ecc0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
6ecd0 0a 2f 2a 0a 2a 2a 20 55 6e 72 65 67 69 73 74 65  ./*.** Unregiste
6ece0 72 20 61 20 56 46 53 20 73 6f 20 74 68 61 74 20  r a VFS so that 
6ecf0 69 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20  it is no longer 
6ed00 61 63 63 65 73 73 69 62 6c 65 2e 0a 2a 2f 0a 53  accessible..*/.S
6ed10 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
6ed20 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69  lite3_vfs_unregi
6ed30 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73  ster(sqlite3_vfs
6ed40 20 2a 70 56 66 73 29 7b 0a 23 69 66 6e 64 65 66   *pVfs){.#ifndef
6ed50 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f   SQLITE_MUTEX_NO
6ed60 4f 50 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  OP.  sqlite3_mut
6ed70 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69  ex *mutex = sqli
6ed80 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
6ed90 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
6eda0 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e  TIC_MASTER);.#en
6edb0 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  dif.  sqlite3_mu
6edc0 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29  tex_enter(mutex)
6edd0 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70 56  ;.  vfsUnlink(pV
6ede0 66 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  fs);.  sqlite3_m
6edf0 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78  utex_leave(mutex
6ee00 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
6ee10 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
6ee20 50 72 6f 76 69 64 65 20 61 20 64 65 66 61 75 6c  Provide a defaul
6ee30 74 20 73 71 6c 69 74 65 33 4f 73 44 65 66 61 75  t sqlite3OsDefau
6ee40 6c 74 56 66 73 28 29 20 69 6d 70 6c 65 6d 65 6e  ltVfs() implemen
6ee50 74 61 74 69 6f 6e 20 69 6e 20 74 68 65 0a 2a 2a  tation in the.**
6ee60 20 63 61 73 65 73 20 77 68 65 72 65 20 6e 6f 6e   cases where non
6ee70 65 20 6f 66 20 74 68 65 20 73 74 61 6e 64 61 72  e of the standar
6ee80 64 20 62 61 63 6b 65 6e 64 73 20 61 72 65 20 75  d backends are u
6ee90 73 65 64 2e 0a 2a 2f 0a 23 69 66 20 21 4f 53 5f  sed..*/.#if !OS_
6eea0 55 4e 49 58 20 26 26 20 21 4f 53 5f 57 49 4e 20  UNIX && !OS_WIN 
6eeb0 26 26 20 21 4f 53 5f 4f 53 32 0a 53 51 4c 49 54  && !OS_OS2.SQLIT
6eec0 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65  E_PRIVATE sqlite
6eed0 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 4f 73  3_vfs *sqlite3Os
6eee0 44 65 66 61 75 6c 74 56 66 73 28 76 6f 69 64 29  DefaultVfs(void)
6eef0 7b 20 72 65 74 75 72 6e 20 30 3b 20 7d 0a 23 65  { return 0; }.#e
6ef00 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif../*********
6ef10 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 2e  ***** End of os.
6ef20 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
6ef30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6ef40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6ef50 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
6ef60 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
6ef70 20 66 61 75 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a   fault.c *******
6ef80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6ef90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6efa0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38  ****/./*.** 2008
6efb0 20 4a 61 6e 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68   Jan 22.**.** Th
6efc0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
6efd0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
6efe0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
6eff0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
6f000 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
6f010 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
6f020 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
6f030 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
6f040 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
6f050 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
6f060 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
6f070 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
6f080 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
6f090 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
6f0a0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
6f0b0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
6f0c0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
6f0d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6f0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6f0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6f100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6f110 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
6f120 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64  ile contains cod
6f130 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61  e to implement a
6f140 20 66 61 75 6c 74 2d 69 6e 6a 65 63 74 6f 72 20   fault-injector 
6f150 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74  used for.** test
6f160 69 6e 67 20 61 6e 64 20 76 65 72 69 66 69 63 61  ing and verifica
6f170 74 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 0a  tion of SQLite..
6f180 2a 2a 0a 2a 2a 20 53 75 62 73 79 73 74 65 6d 73  **.** Subsystems
6f190 20 77 69 74 68 69 6e 20 53 51 4c 69 74 65 20 63   within SQLite c
6f1a0 61 6e 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 46  an call sqlite3F
6f1b0 61 75 6c 74 53 74 65 70 28 29 20 74 6f 20 73 65  aultStep() to se
6f1c0 65 20 69 66 0a 2a 2a 20 74 68 65 79 20 73 68 6f  e if.** they sho
6f1d0 75 6c 64 20 73 69 6d 75 6c 61 74 65 20 61 20 66  uld simulate a f
6f1e0 61 75 6c 74 2e 20 20 73 71 6c 69 74 65 33 46 61  ault.  sqlite3Fa
6f1f0 75 6c 74 53 74 65 70 28 29 20 6e 6f 72 6d 61 6c  ultStep() normal
6f200 6c 79 20 72 65 74 75 72 6e 73 0a 2a 2a 20 7a 65  ly returns.** ze
6f210 72 6f 20 62 75 74 20 77 69 6c 6c 20 72 65 74 75  ro but will retu
6f220 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61  rn non-zero if a
6f230 20 66 61 75 6c 74 20 73 68 6f 75 6c 64 20 62 65   fault should be
6f240 20 73 69 6d 75 6c 61 74 65 64 2e 0a 2a 2a 20 46   simulated..** F
6f250 61 75 6c 74 20 69 6e 6a 65 63 74 6f 72 73 20 63  ault injectors c
6f260 61 6e 20 62 65 20 75 73 65 64 2c 20 66 6f 72 20  an be used, for 
6f270 65 78 61 6d 70 6c 65 2c 20 74 6f 20 73 69 6d 75  example, to simu
6f280 6c 61 74 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61  late memory.** a
6f290 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 75 72  llocation failur
6f2a0 65 73 20 6f 72 20 49 2f 4f 20 65 72 72 6f 72 73  es or I/O errors
6f2b0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 61 75 6c  ..**.** The faul
6f2c0 74 20 69 6e 6a 65 63 74 6f 72 20 69 73 20 6f 6d  t injector is om
6f2d0 69 74 74 65 64 20 66 72 6f 6d 20 74 68 65 20 63  itted from the c
6f2e0 6f 64 65 20 69 66 20 53 51 4c 69 74 65 20 69 73  ode if SQLite is
6f2f0 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 77 69 74  .** compiled wit
6f300 68 20 2d 44 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  h -DSQLITE_OMIT_
6f310 42 55 49 4c 54 49 4e 5f 54 45 53 54 3d 31 2e 20  BUILTIN_TEST=1. 
6f320 20 54 68 65 72 65 20 69 73 20 61 20 76 65 72 79   There is a very
6f330 0a 2a 2a 20 73 6d 61 6c 6c 20 70 65 72 66 6f 72  .** small perfor
6f340 6d 61 6e 63 65 20 68 69 74 20 66 6f 72 20 6c 65  mance hit for le
6f350 61 76 69 6e 67 20 74 68 65 20 66 61 75 6c 74 20  aving the fault 
6f360 69 6e 6a 65 63 74 6f 72 20 69 6e 20 74 68 65 20  injector in the 
6f370 63 6f 64 65 2e 0a 2a 2a 20 43 6f 6d 6d 65 72 69  code..** Commeri
6f380 63 61 6c 20 70 72 6f 64 75 63 74 73 20 77 69 6c  cal products wil
6f390 6c 20 70 72 6f 62 61 62 6c 79 20 77 61 6e 74 20  l probably want 
6f3a0 74 6f 20 6f 6d 69 74 20 74 68 65 20 66 61 75 6c  to omit the faul
6f3b0 74 20 69 6e 6a 65 63 74 6f 72 0a 2a 2a 20 66 72  t injector.** fr
6f3c0 6f 6d 20 70 72 6f 64 75 63 74 69 6f 6e 20 62 75  om production bu
6f3d0 69 6c 64 73 2e 20 20 42 75 74 20 73 61 66 65 74  ilds.  But safet
6f3e0 79 2d 63 72 69 74 69 63 61 6c 20 73 79 73 74 65  y-critical syste
6f3f0 6d 73 20 77 68 6f 20 77 6f 72 6b 0a 2a 2a 20 75  ms who work.** u
6f400 6e 64 65 72 20 74 68 65 20 6d 6f 74 74 6f 20 22  nder the motto "
6f410 66 6c 79 20 77 68 61 74 20 79 6f 75 20 74 65 73  fly what you tes
6f420 74 20 61 6e 64 20 74 65 73 74 20 77 68 61 74 20  t and test what 
6f430 79 6f 75 20 66 6c 79 22 20 6d 61 79 0a 2a 2a 20  you fly" may.** 
6f440 63 68 6f 6f 73 65 20 74 6f 20 6c 65 61 76 65 20  choose to leave 
6f450 74 68 65 20 66 61 75 6c 74 20 69 6e 6a 65 63 74  the fault inject
6f460 6f 72 20 65 6e 61 62 6c 65 64 20 65 76 65 6e 20  or enabled even 
6f470 69 6e 20 70 72 6f 64 75 63 74 69 6f 6e 2e 0a 2a  in production..*
6f480 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
6f490 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54  E_OMIT_BUILTIN_T
6f4a0 45 53 54 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 72 65  EST../*.** There
6f4b0 20 63 61 6e 20 62 65 20 76 61 72 69 6f 75 73 20   can be various 
6f4c0 6b 69 6e 64 73 20 6f 66 20 66 61 75 6c 74 73 2e  kinds of faults.
6f4d0 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74    For example, t
6f4e0 68 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a 20 61  here can be.** a
6f4f0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
6f500 6f 6e 20 66 61 69 6c 75 72 65 2e 20 20 4f 72 20  on failure.  Or 
6f510 61 6e 20 49 2f 4f 20 66 61 69 6c 75 72 65 2e 20  an I/O failure. 
6f520 20 46 6f 72 20 65 61 63 68 20 64 69 66 66 65 72   For each differ
6f530 65 6e 74 0a 2a 2a 20 66 61 75 6c 74 20 74 79 70  ent.** fault typ
6f540 65 2c 20 74 68 65 72 65 20 69 73 20 61 20 73 65  e, there is a se
6f550 70 61 72 61 74 65 20 46 61 75 6c 74 49 6e 6a 65  parate FaultInje
6f560 63 74 6f 72 20 73 74 72 75 63 74 75 72 65 20 74  ctor structure t
6f570 6f 20 6b 65 65 70 20 74 72 61 63 6b 0a 2a 2a 20  o keep track.** 
6f580 6f 66 20 74 68 65 20 73 74 61 74 75 73 20 6f 66  of the status of
6f590 20 74 68 61 74 20 66 61 75 6c 74 2e 0a 2a 2f 0a   that fault..*/.
6f5a0 73 74 61 74 69 63 20 73 74 72 75 63 74 20 46 61  static struct Fa
6f5b0 75 6c 74 49 6e 6a 65 63 74 6f 72 20 7b 0a 20 20  ultInjector {.  
6f5c0 69 6e 74 20 69 43 6f 75 6e 74 64 6f 77 6e 3b 20  int iCountdown; 
6f5d0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70    /* Number of p
6f5e0 65 6e 64 69 6e 67 20 73 75 63 63 65 73 73 65 73  ending successes
6f5f0 20 62 65 66 6f 72 65 20 77 65 20 68 69 74 20 61   before we hit a
6f600 20 66 61 69 6c 75 72 65 20 2a 2f 0a 20 20 69 6e   failure */.  in
6f610 74 20 6e 52 65 70 65 61 74 3b 20 20 20 20 20 20  t nRepeat;      
6f620 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d  /* Number of tim
6f630 65 73 20 74 6f 20 72 65 70 65 61 74 20 74 68 65  es to repeat the
6f640 20 66 61 69 6c 75 72 65 20 2a 2f 0a 20 20 69 6e   failure */.  in
6f650 74 20 6e 42 65 6e 69 67 6e 3b 20 20 20 20 20 20  t nBenign;      
6f660 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 65 6e  /* Number of ben
6f670 69 67 6e 20 66 61 69 6c 75 72 65 73 20 73 65 65  ign failures see
6f680 6e 20 73 69 6e 63 65 20 6c 61 73 74 20 63 6f 6e  n since last con
6f690 66 69 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 61  fig */.  int nFa
6f6a0 69 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  il;        /* Nu
6f6b0 6d 62 65 72 20 6f 66 20 66 61 69 6c 75 72 65 73  mber of failures
6f6c0 20 73 65 65 6e 20 73 69 6e 63 65 20 6c 61 73 74   seen since last
6f6d0 20 63 6f 6e 66 69 67 20 2a 2f 0a 20 20 75 38 20   config */.  u8 
6f6e0 65 6e 61 62 6c 65 3b 20 20 20 20 20 20 20 20 2f  enable;        /
6f6f0 2a 20 54 72 75 65 20 69 66 20 65 6e 61 62 6c 65  * True if enable
6f700 64 20 2a 2f 0a 20 20 69 31 36 20 62 65 6e 69 67  d */.  i16 benig
6f710 6e 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 69  n;       /* Posi
6f720 74 69 76 65 20 69 66 20 6e 65 78 74 20 66 61 69  tive if next fai
6f730 6c 75 72 65 20 77 69 6c 6c 20 62 65 20 62 65 6e  lure will be ben
6f740 69 67 6e 20 2a 2f 0a 7d 20 61 46 61 75 6c 74 5b  ign */.} aFault[
6f750 53 51 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45  SQLITE_FAULTINJE
6f760 43 54 4f 52 5f 43 4f 55 4e 54 5d 3b 0a 0a 2f 2a  CTOR_COUNT];../*
6f770 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
6f780 20 63 6f 6e 66 69 67 75 72 65 73 20 61 6e 64 20   configures and 
6f790 65 6e 61 62 6c 65 73 20 61 20 66 61 75 6c 74 20  enables a fault 
6f7a0 69 6e 6a 65 63 74 6f 72 2e 20 20 41 66 74 65 72  injector.  After
6f7b0 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 69 73  .** calling this
6f7c0 20 72 6f 75 74 69 6e 65 2c 20 61 46 61 75 6c 74   routine, aFault
6f7d0 53 74 65 70 28 29 20 77 69 6c 6c 20 72 65 74 75  Step() will retu
6f7e0 72 6e 20 66 61 6c 73 65 20 28 7a 65 72 6f 29 0a  rn false (zero).
6f7f0 2a 2a 20 6e 44 65 6c 61 79 20 74 69 6d 65 73 2c  ** nDelay times,
6f800 20 74 68 65 6e 20 69 74 20 77 69 6c 6c 20 72 65   then it will re
6f810 74 75 72 6e 20 74 72 75 65 20 6e 52 65 70 65 61  turn true nRepea
6f820 74 20 74 69 6d 65 73 2c 0a 2a 2a 20 74 68 65 6e  t times,.** then
6f830 20 69 74 20 77 69 6c 6c 20 61 67 61 69 6e 20 62   it will again b
6f840 65 67 69 6e 20 72 65 74 75 72 6e 69 6e 67 20 66  egin returning f
6f850 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  alse..*/.SQLITE_
6f860 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6f870 69 74 65 33 46 61 75 6c 74 43 6f 6e 66 69 67 28  ite3FaultConfig(
6f880 69 6e 74 20 69 64 2c 20 69 6e 74 20 6e 44 65 6c  int id, int nDel
6f890 61 79 2c 20 69 6e 74 20 6e 52 65 70 65 61 74 29  ay, int nRepeat)
6f8a0 7b 0a 20 20 61 73 73 65 72 74 28 20 69 64 3e 3d  {.  assert( id>=
6f8b0 30 20 26 26 20 69 64 3c 53 51 4c 49 54 45 5f 46  0 && id<SQLITE_F
6f8c0 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 43 4f 55  AULTINJECTOR_COU
6f8d0 4e 54 20 29 3b 0a 20 20 61 46 61 75 6c 74 5b 69  NT );.  aFault[i
6f8e0 64 5d 2e 69 43 6f 75 6e 74 64 6f 77 6e 20 3d 20  d].iCountdown = 
6f8f0 6e 44 65 6c 61 79 3b 0a 20 20 61 46 61 75 6c 74  nDelay;.  aFault
6f900 5b 69 64 5d 2e 6e 52 65 70 65 61 74 20 3d 20 6e  [id].nRepeat = n
6f910 52 65 70 65 61 74 3b 0a 20 20 61 46 61 75 6c 74  Repeat;.  aFault
6f920 5b 69 64 5d 2e 6e 42 65 6e 69 67 6e 20 3d 20 30  [id].nBenign = 0
6f930 3b 0a 20 20 61 46 61 75 6c 74 5b 69 64 5d 2e 6e  ;.  aFault[id].n
6f940 46 61 69 6c 20 3d 20 30 3b 0a 20 20 61 46 61 75  Fail = 0;.  aFau
6f950 6c 74 5b 69 64 5d 2e 65 6e 61 62 6c 65 20 3d 20  lt[id].enable = 
6f960 6e 44 65 6c 61 79 3e 3d 30 3b 0a 20 20 61 46 61  nDelay>=0;.  aFa
6f970 75 6c 74 5b 69 64 5d 2e 62 65 6e 69 67 6e 20 3d  ult[id].benign =
6f980 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74   0;.}../*.** Ret
6f990 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
6f9a0 66 20 66 61 75 6c 74 73 20 28 62 6f 74 68 20 68  f faults (both h
6f9b0 61 72 64 20 61 6e 64 20 62 65 6e 69 67 6e 20 66  ard and benign f
6f9c0 61 75 6c 74 73 29 20 74 68 61 74 20 68 61 76 65  aults) that have
6f9d0 0a 2a 2a 20 6f 63 63 75 72 72 65 64 20 73 69 6e  .** occurred sin
6f9e0 63 65 20 74 68 65 20 69 6e 6a 65 63 74 6f 72 20  ce the injector 
6f9f0 77 61 73 20 6c 61 73 74 20 63 6f 6e 66 69 67 75  was last configu
6fa00 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  red..*/.SQLITE_P
6fa10 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6fa20 65 33 46 61 75 6c 74 46 61 69 6c 75 72 65 73 28  e3FaultFailures(
6fa30 69 6e 74 20 69 64 29 7b 0a 20 20 61 73 73 65 72  int id){.  asser
6fa40 74 28 20 69 64 3e 3d 30 20 26 26 20 69 64 3c 53  t( id>=0 && id<S
6fa50 51 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43  QLITE_FAULTINJEC
6fa60 54 4f 52 5f 43 4f 55 4e 54 20 29 3b 0a 20 20 72  TOR_COUNT );.  r
6fa70 65 74 75 72 6e 20 61 46 61 75 6c 74 5b 69 64 5d  eturn aFault[id]
6fa80 2e 6e 46 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  .nFail;.}../*.**
6fa90 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
6faa0 65 72 20 6f 66 20 62 65 6e 69 67 6e 20 66 61 75  er of benign fau
6fab0 6c 74 73 20 74 68 61 74 20 68 61 76 65 20 6f 63  lts that have oc
6fac0 63 75 72 72 65 64 20 73 69 6e 63 65 20 74 68 65  curred since the
6fad0 0a 2a 2a 20 69 6e 6a 65 63 74 6f 72 20 77 61 73  .** injector was
6fae0 20 6c 61 73 74 20 63 6f 6e 66 69 67 75 72 65 64   last configured
6faf0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
6fb00 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
6fb10 61 75 6c 74 42 65 6e 69 67 6e 46 61 69 6c 75 72  aultBenignFailur
6fb20 65 73 28 69 6e 74 20 69 64 29 7b 0a 20 20 61 73  es(int id){.  as
6fb30 73 65 72 74 28 20 69 64 3e 3d 30 20 26 26 20 69  sert( id>=0 && i
6fb40 64 3c 53 51 4c 49 54 45 5f 46 41 55 4c 54 49 4e  d<SQLITE_FAULTIN
6fb50 4a 45 43 54 4f 52 5f 43 4f 55 4e 54 20 29 3b 0a  JECTOR_COUNT );.
6fb60 20 20 72 65 74 75 72 6e 20 61 46 61 75 6c 74 5b    return aFault[
6fb70 69 64 5d 2e 6e 42 65 6e 69 67 6e 3b 0a 7d 0a 0a  id].nBenign;.}..
6fb80 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
6fb90 20 6e 75 6d 62 65 72 20 6f 66 20 73 75 63 63 65   number of succe
6fba0 73 73 65 73 20 74 68 61 74 20 77 69 6c 6c 20 6f  sses that will o
6fbb0 63 63 75 72 20 62 65 66 6f 72 65 20 74 68 65 20  ccur before the 
6fbc0 6e 65 78 74 20 66 61 69 6c 75 72 65 2e 0a 2a 2a  next failure..**
6fbd0 20 49 66 20 6e 6f 20 66 61 69 6c 75 72 65 73 20   If no failures 
6fbe0 61 72 65 20 73 63 68 65 64 75 6c 65 64 2c 20 72  are scheduled, r
6fbf0 65 74 75 72 6e 20 2d 31 2e 0a 2a 2f 0a 53 51 4c  eturn -1..*/.SQL
6fc00 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6fc10 73 71 6c 69 74 65 33 46 61 75 6c 74 50 65 6e 64  sqlite3FaultPend
6fc20 69 6e 67 28 69 6e 74 20 69 64 29 7b 0a 20 20 61  ing(int id){.  a
6fc30 73 73 65 72 74 28 20 69 64 3e 3d 30 20 26 26 20  ssert( id>=0 && 
6fc40 69 64 3c 53 51 4c 49 54 45 5f 46 41 55 4c 54 49  id<SQLITE_FAULTI
6fc50 4e 4a 45 43 54 4f 52 5f 43 4f 55 4e 54 20 29 3b  NJECTOR_COUNT );
6fc60 0a 20 20 69 66 28 20 61 46 61 75 6c 74 5b 69 64  .  if( aFault[id
6fc70 5d 2e 65 6e 61 62 6c 65 20 29 7b 0a 20 20 20 20  ].enable ){.    
6fc80 72 65 74 75 72 6e 20 61 46 61 75 6c 74 5b 69 64  return aFault[id
6fc90 5d 2e 69 43 6f 75 6e 74 64 6f 77 6e 3b 0a 20 20  ].iCountdown;.  
6fca0 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
6fcb0 6e 20 2d 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20  n -1;.  }.}../* 
6fcc0 0a 2a 2a 20 41 66 74 65 72 20 74 68 69 73 20 72  .** After this r
6fcd0 6f 75 74 69 6e 65 20 63 61 75 73 65 73 20 73 75  outine causes su
6fce0 62 73 65 71 75 65 6e 74 20 66 61 75 6c 74 73 20  bsequent faults 
6fcf0 74 6f 20 62 65 20 65 69 74 68 65 72 20 62 65 6e  to be either ben
6fd00 69 67 6e 0a 2a 2a 20 6f 72 20 68 61 72 64 20 28  ign.** or hard (
6fd10 6e 6f 74 20 62 65 6e 69 67 6e 29 2c 20 61 63 63  not benign), acc
6fd20 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 22 65  ording to the "e
6fd30 6e 61 62 6c 65 22 20 70 61 72 61 6d 65 74 65 72  nable" parameter
6fd40 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 66 61 75  ..**.** Most fau
6fd50 6c 74 73 20 61 72 65 20 68 61 72 64 2e 20 20 49  lts are hard.  I
6fd60 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 6d  n other words, m
6fd70 6f 73 74 20 66 61 75 6c 74 73 20 63 61 75 73 65  ost faults cause
6fd80 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 74 6f 20  .** an error to 
6fd90 62 65 20 70 72 6f 70 61 67 61 74 65 64 20 62 61  be propagated ba
6fda0 63 6b 20 75 70 20 74 6f 20 74 68 65 20 61 70 70  ck up to the app
6fdb0 6c 69 63 61 74 69 6f 6e 20 69 6e 74 65 72 66 61  lication interfa
6fdc0 63 65 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20  ce..** However, 
6fdd0 73 6f 6d 65 74 69 6d 65 73 20 61 20 66 61 75 6c  sometimes a faul
6fde0 74 20 69 73 20 65 61 73 69 6c 79 20 72 65 63 6f  t is easily reco
6fdf0 76 65 72 61 62 6c 65 2e 20 20 46 6f 72 20 65 78  verable.  For ex
6fe00 61 6d 70 6c 65 2c 0a 2a 2a 20 69 66 20 61 20 6d  ample,.** if a m
6fe10 61 6c 6c 6f 63 20 66 61 69 6c 73 20 77 68 69 6c  alloc fails whil
6fe20 65 20 72 65 73 69 7a 69 6e 67 20 61 20 68 61 73  e resizing a has
6fe30 68 20 74 61 62 6c 65 2c 20 74 68 69 73 20 69 73  h table, this is
6fe40 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 72   completely.** r
6fe50 65 63 6f 76 65 72 61 62 6c 65 20 73 69 6d 70 6c  ecoverable simpl
6fe60 79 20 62 79 20 6e 6f 74 20 63 61 72 72 79 69 6e  y by not carryin
6fe70 67 20 6f 75 74 20 74 68 65 20 72 65 73 69 7a 65  g out the resize
6fe80 2e 20 20 54 68 65 20 68 61 73 68 20 74 61 62 6c  .  The hash tabl
6fe90 65 0a 2a 2a 20 77 69 6c 6c 20 63 6f 6e 74 69 6e  e.** will contin
6fea0 75 65 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20 6e  ue to function n
6feb0 6f 72 6d 61 6c 6c 79 2e 20 20 53 6f 20 61 20 6d  ormally.  So a m
6fec0 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 64 75  alloc failure du
6fed0 72 69 6e 67 0a 2a 2a 20 61 20 68 61 73 68 20 74  ring.** a hash t
6fee0 61 62 6c 65 20 72 65 73 69 7a 65 20 69 73 20 61  able resize is a
6fef0 20 62 65 6e 69 67 6e 20 66 61 75 6c 74 2e 20 20   benign fault.  
6ff00 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
6ff10 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
6ff20 61 75 6c 74 42 65 67 69 6e 42 65 6e 69 67 6e 28  aultBeginBenign(
6ff30 69 6e 74 20 69 64 29 7b 0a 20 20 69 66 28 20 69  int id){.  if( i
6ff40 64 3c 30 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  d<0 ){.    for(i
6ff50 64 3d 30 3b 20 69 64 3c 53 51 4c 49 54 45 5f 46  d=0; id<SQLITE_F
6ff60 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 43 4f 55  AULTINJECTOR_COU
6ff70 4e 54 3b 20 69 64 2b 2b 29 7b 0a 20 20 20 20 20  NT; id++){.     
6ff80 20 61 46 61 75 6c 74 5b 69 64 5d 2e 62 65 6e 69   aFault[id].beni
6ff90 67 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  gn++;.    }.  }e
6ffa0 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
6ffb0 20 69 64 3e 3d 30 20 26 26 20 69 64 3c 53 51 4c   id>=0 && id<SQL
6ffc0 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43 54 4f  ITE_FAULTINJECTO
6ffd0 52 5f 43 4f 55 4e 54 20 29 3b 0a 20 20 20 20 61  R_COUNT );.    a
6ffe0 46 61 75 6c 74 5b 69 64 5d 2e 62 65 6e 69 67 6e  Fault[id].benign
6fff0 2b 2b 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45  ++;.  }.}.SQLITE
70000 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
70010 6c 69 74 65 33 46 61 75 6c 74 45 6e 64 42 65 6e  lite3FaultEndBen
70020 69 67 6e 28 69 6e 74 20 69 64 29 7b 0a 20 20 69  ign(int id){.  i
70030 66 28 20 69 64 3c 30 20 29 7b 0a 20 20 20 20 66  f( id<0 ){.    f
70040 6f 72 28 69 64 3d 30 3b 20 69 64 3c 53 51 4c 49  or(id=0; id<SQLI
70050 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43 54 4f 52  TE_FAULTINJECTOR
70060 5f 43 4f 55 4e 54 3b 20 69 64 2b 2b 29 7b 0a 20  _COUNT; id++){. 
70070 20 20 20 20 20 61 73 73 65 72 74 28 20 61 46 61       assert( aFa
70080 75 6c 74 5b 69 64 5d 2e 62 65 6e 69 67 6e 3e 30  ult[id].benign>0
70090 20 29 3b 0a 20 20 20 20 20 20 61 46 61 75 6c 74   );.      aFault
700a0 5b 69 64 5d 2e 62 65 6e 69 67 6e 2d 2d 3b 0a 20  [id].benign--;. 
700b0 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
700c0 20 20 61 73 73 65 72 74 28 20 69 64 3e 3d 30 20    assert( id>=0 
700d0 26 26 20 69 64 3c 53 51 4c 49 54 45 5f 46 41 55  && id<SQLITE_FAU
700e0 4c 54 49 4e 4a 45 43 54 4f 52 5f 43 4f 55 4e 54  LTINJECTOR_COUNT
700f0 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
70100 61 46 61 75 6c 74 5b 69 64 5d 2e 62 65 6e 69 67  aFault[id].benig
70110 6e 3e 30 20 29 3b 0a 20 20 20 20 61 46 61 75 6c  n>0 );.    aFaul
70120 74 5b 69 64 5d 2e 62 65 6e 69 67 6e 2d 2d 3b 0a  t[id].benign--;.
70130 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69    }.}../*.** Thi
70140 73 20 72 6f 75 74 69 6e 65 20 65 78 69 73 74 73  s routine exists
70150 20 61 73 20 61 20 70 6c 61 63 65 20 74 6f 20 73   as a place to s
70160 65 74 20 61 20 62 72 65 61 6b 70 6f 69 6e 74 20  et a breakpoint 
70170 74 68 61 74 20 77 69 6c 6c 0a 2a 2a 20 66 69 72  that will.** fir
70180 65 20 6f 6e 20 61 6e 79 20 73 69 6d 75 6c 61 74  e on any simulat
70190 65 64 20 66 61 75 6c 74 2e 0a 2a 2f 0a 73 74 61  ed fault..*/.sta
701a0 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
701b0 46 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73  Fault(void){.  s
701c0 74 61 74 69 63 20 69 6e 74 20 63 6e 74 20 3d 20  tatic int cnt = 
701d0 30 3b 0a 20 20 63 6e 74 2b 2b 3b 0a 7d 0a 0a 0a  0;.  cnt++;.}...
701e0 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73  /*.** Check to s
701f0 65 65 20 69 66 20 61 20 66 61 75 6c 74 20 73 68  ee if a fault sh
70200 6f 75 6c 64 20 62 65 20 73 69 6d 75 6c 61 74 65  ould be simulate
70210 64 2e 20 20 52 65 74 75 72 6e 20 74 72 75 65 20  d.  Return true 
70220 74 6f 20 73 69 6d 75 6c 61 74 65 0a 2a 2a 20 74  to simulate.** t
70230 68 65 20 66 61 75 6c 74 2e 20 20 52 65 74 75 72  he fault.  Retur
70240 6e 20 66 61 6c 73 65 20 69 66 20 74 68 65 20 66  n false if the f
70250 61 75 6c 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20  ault should not 
70260 62 65 20 73 69 6d 75 6c 61 74 65 64 2e 0a 2a 2f  be simulated..*/
70270 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
70280 69 6e 74 20 73 71 6c 69 74 65 33 46 61 75 6c 74  int sqlite3Fault
70290 53 74 65 70 28 69 6e 74 20 69 64 29 7b 0a 20 20  Step(int id){.  
702a0 61 73 73 65 72 74 28 20 69 64 3e 3d 30 20 26 26  assert( id>=0 &&
702b0 20 69 64 3c 53 51 4c 49 54 45 5f 46 41 55 4c 54   id<SQLITE_FAULT
702c0 49 4e 4a 45 43 54 4f 52 5f 43 4f 55 4e 54 20 29  INJECTOR_COUNT )
702d0 3b 0a 20 20 69 66 28 20 6c 69 6b 65 6c 79 28 21  ;.  if( likely(!
702e0 61 46 61 75 6c 74 5b 69 64 5d 2e 65 6e 61 62 6c  aFault[id].enabl
702f0 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  e) ){.    return
70300 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 61 46   0;.  }.  if( aF
70310 61 75 6c 74 5b 69 64 5d 2e 69 43 6f 75 6e 74 64  ault[id].iCountd
70320 6f 77 6e 3e 30 20 29 7b 0a 20 20 20 20 61 46 61  own>0 ){.    aFa
70330 75 6c 74 5b 69 64 5d 2e 69 43 6f 75 6e 74 64 6f  ult[id].iCountdo
70340 77 6e 2d 2d 3b 0a 20 20 20 20 72 65 74 75 72 6e  wn--;.    return
70350 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   0;.  }.  sqlite
70360 33 46 61 75 6c 74 28 29 3b 0a 20 20 61 46 61 75  3Fault();.  aFau
70370 6c 74 5b 69 64 5d 2e 6e 46 61 69 6c 2b 2b 3b 0a  lt[id].nFail++;.
70380 20 20 69 66 28 20 61 46 61 75 6c 74 5b 69 64 5d    if( aFault[id]
70390 2e 62 65 6e 69 67 6e 3e 30 20 29 7b 0a 20 20 20  .benign>0 ){.   
703a0 20 61 46 61 75 6c 74 5b 69 64 5d 2e 6e 42 65 6e   aFault[id].nBen
703b0 69 67 6e 2b 2b 3b 0a 20 20 7d 0a 20 20 61 46 61  ign++;.  }.  aFa
703c0 75 6c 74 5b 69 64 5d 2e 6e 52 65 70 65 61 74 2d  ult[id].nRepeat-
703d0 2d 3b 0a 20 20 69 66 28 20 61 46 61 75 6c 74 5b  -;.  if( aFault[
703e0 69 64 5d 2e 6e 52 65 70 65 61 74 3c 3d 30 20 29  id].nRepeat<=0 )
703f0 7b 0a 20 20 20 20 61 46 61 75 6c 74 5b 69 64 5d  {.    aFault[id]
70400 2e 65 6e 61 62 6c 65 20 3d 20 30 3b 0a 20 20 7d  .enable = 0;.  }
70410 0a 20 20 72 65 74 75 72 6e 20 31 3b 20 20 0a 7d  .  return 1;  .}
70420 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
70430 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f  TE_OMIT_BUILTIN_
70440 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  TEST */../******
70450 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
70460 66 61 75 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  fault.c ********
70470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
70480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
70490 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
704a0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
704b0 69 6c 65 20 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a  ile mem1.c *****
704c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
704d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
704e0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
704f0 30 30 37 20 41 75 67 75 73 74 20 31 34 0a 2a 2a  007 August 14.**
70500 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
70510 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
70520 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
70530 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
70540 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
70550 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
70560 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
70570 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
70580 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
70590 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
705a0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
705b0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
705c0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
705d0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
705e0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
705f0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
70600 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
70610 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
70620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
70630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
70640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
70650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
70660 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
70670 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f  ns the C functio
70680 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  ns that implemen
70690 74 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c  t a memory.** al
706a0 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
706b0 65 6d 20 66 6f 72 20 75 73 65 20 62 79 20 53 51  em for use by SQ
706c0 4c 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 24 49  Lite.  .**.** $I
706d0 64 3a 20 6d 65 6d 31 2e 63 2c 76 20 31 2e 31 37  d: mem1.c,v 1.17
706e0 20 32 30 30 38 2f 30 33 2f 31 38 20 30 30 3a 30   2008/03/18 00:0
706f0 37 3a 31 31 20 64 72 68 20 45 78 70 20 24 0a 2a  7:11 drh Exp $.*
70700 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65  /../*.** This ve
70710 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d  rsion of the mem
70720 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73  ory allocator is
70730 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20 49   the default.  I
70740 74 20 69 73 0a 2a 2a 20 75 73 65 64 20 77 68 65  t is.** used whe
70750 6e 20 6e 6f 20 6f 74 68 65 72 20 6d 65 6d 6f 72  n no other memor
70760 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 73  y allocator is s
70770 70 65 63 69 66 69 65 64 20 75 73 69 6e 67 20 63  pecified using c
70780 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6d  ompile-time.** m
70790 61 63 72 6f 73 2e 0a 2a 2f 0a 23 69 66 64 65 66  acros..*/.#ifdef
707a0 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d   SQLITE_SYSTEM_M
707b0 41 4c 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  ALLOC../*.** All
707c0 20 6f 66 20 74 68 65 20 73 74 61 74 69 63 20 76   of the static v
707d0 61 72 69 61 62 6c 65 73 20 75 73 65 64 20 62 79  ariables used by
707e0 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 72 65   this module are
707f0 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 69 6e   collected.** in
70800 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74 72 75  to a single stru
70810 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d 65 6d  cture named "mem
70820 22 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20 6b  ".  This is to k
70830 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 61 74 69  eep the.** stati
70840 63 20 76 61 72 69 61 62 6c 65 73 20 6f 72 67 61  c variables orga
70850 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 72 65 64  nized and to red
70860 75 63 65 20 6e 61 6d 65 73 70 61 63 65 20 70 6f  uce namespace po
70870 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e 20  llution.** when 
70880 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20 63  this module is c
70890 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 6f 74 68  ombined with oth
708a0 65 72 20 69 6e 20 74 68 65 20 61 6d 61 6c 67 61  er in the amalga
708b0 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  mation..*/.stati
708c0 63 20 73 74 72 75 63 74 20 7b 0a 20 20 2f 2a 0a  c struct {.  /*.
708d0 20 20 2a 2a 20 54 68 65 20 61 6c 61 72 6d 20 63    ** The alarm c
708e0 61 6c 6c 62 61 63 6b 20 61 6e 64 20 69 74 73 20  allback and its 
708f0 61 72 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 20  arguments.  The 
70900 6d 65 6d 2e 6d 75 74 65 78 20 6c 6f 63 6b 20 77  mem.mutex lock w
70910 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 68 65 6c 64  ill.  ** be held
70920 20 77 68 69 6c 65 20 74 68 65 20 63 61 6c 6c 62   while the callb
70930 61 63 6b 20 69 73 20 72 75 6e 6e 69 6e 67 2e 20  ack is running. 
70940 20 52 65 63 75 72 73 69 76 65 20 63 61 6c 6c 73   Recursive calls
70950 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 20 6d   into.  ** the m
70960 65 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d 20  emory subsystem 
70970 61 72 65 20 61 6c 6c 6f 77 65 64 2c 20 62 75 74  are allowed, but
70980 20 6e 6f 20 6e 65 77 20 63 61 6c 6c 62 61 63 6b   no new callback
70990 73 20 77 69 6c 6c 20 62 65 0a 20 20 2a 2a 20 69  s will be.  ** i
709a0 73 73 75 65 64 2e 20 20 54 68 65 20 61 6c 61 72  ssued.  The alar
709b0 6d 42 75 73 79 20 76 61 72 69 61 62 6c 65 20 69  mBusy variable i
709c0 73 20 73 65 74 20 74 6f 20 70 72 65 76 65 6e 74  s set to prevent
709d0 20 72 65 63 75 72 73 69 76 65 0a 20 20 2a 2a 20   recursive.  ** 
709e0 63 61 6c 6c 62 61 63 6b 73 2e 0a 20 20 2a 2f 0a  callbacks..  */.
709f0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
70a00 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 3b 0a  alarmThreshold;.
70a10 20 20 76 6f 69 64 20 28 2a 61 6c 61 72 6d 43 61    void (*alarmCa
70a20 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 20 73  llback)(void*, s
70a30 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74  qlite3_int64,int
70a40 29 3b 0a 20 20 76 6f 69 64 20 2a 61 6c 61 72 6d  );.  void *alarm
70a50 41 72 67 3b 0a 20 20 69 6e 74 20 61 6c 61 72 6d  Arg;.  int alarm
70a60 42 75 73 79 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20  Busy;.  .  /*.  
70a70 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74  ** Mutex to cont
70a80 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68  rol access to th
70a90 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
70aa0 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20  ion subsystem.. 
70ab0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   */.  sqlite3_mu
70ac0 74 65 78 20 2a 6d 75 74 65 78 3b 0a 20 20 0a 20  tex *mutex;.  . 
70ad0 20 2f 2a 0a 20 20 2a 2a 20 43 75 72 72 65 6e 74   /*.  ** Current
70ae0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 20   allocation and 
70af0 68 69 67 68 2d 77 61 74 65 72 20 6d 61 72 6b 2e  high-water mark.
70b00 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  .  */.  sqlite3_
70b10 69 6e 74 36 34 20 6e 6f 77 55 73 65 64 3b 0a 20  int64 nowUsed;. 
70b20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6d   sqlite3_int64 m
70b30 78 55 73 65 64 3b 0a 20 20 0a 20 0a 7d 20 6d 65  xUsed;.  . .} me
70b40 6d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20  m;../*.** Enter 
70b50 74 68 65 20 6d 75 74 65 78 20 6d 65 6d 2e 6d 75  the mutex mem.mu
70b60 74 65 78 2e 20 41 6c 6c 6f 63 61 74 65 20 69 74  tex. Allocate it
70b70 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c   if it is not al
70b80 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 2e  ready allocated.
70b90 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
70ba0 65 6e 74 65 72 4d 65 6d 28 76 6f 69 64 29 7b 0a  enterMem(void){.
70bb0 20 20 69 66 28 20 6d 65 6d 2e 6d 75 74 65 78 3d    if( mem.mutex=
70bc0 3d 30 20 29 7b 0a 20 20 20 20 6d 65 6d 2e 6d 75  =0 ){.    mem.mu
70bd0 74 65 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 75  tex = sqlite3_mu
70be0 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45  tex_alloc(SQLITE
70bf0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45  _MUTEX_STATIC_ME
70c00 4d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  M);.  }.  sqlite
70c10 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65  3_mutex_enter(me
70c20 6d 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a  m.mutex);.}../*.
70c30 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d  ** Return the am
70c40 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63  ount of memory c
70c50 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64  urrently checked
70c60 20 6f 75 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   out..*/.SQLITE_
70c70 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  API sqlite3_int6
70c80 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79  4 sqlite3_memory
70c90 5f 75 73 65 64 28 76 6f 69 64 29 7b 0a 20 20 73  _used(void){.  s
70ca0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 3b 0a  qlite3_int64 n;.
70cb0 20 20 65 6e 74 65 72 4d 65 6d 28 29 3b 0a 20 20    enterMem();.  
70cc0 6e 20 3d 20 6d 65 6d 2e 6e 6f 77 55 73 65 64 3b  n = mem.nowUsed;
70cd0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
70ce0 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78  _leave(mem.mutex
70cf0 29 3b 20 20 0a 20 20 72 65 74 75 72 6e 20 6e 3b  );  .  return n;
70d00 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
70d10 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f   the maximum amo
70d20 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 74 68  unt of memory th
70d30 61 74 20 68 61 73 20 65 76 65 72 20 62 65 65 6e  at has ever been
70d40 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75 74 20  .** checked out 
70d50 73 69 6e 63 65 20 65 69 74 68 65 72 20 74 68 65  since either the
70d60 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
70d70 69 73 20 70 72 6f 63 65 73 73 0a 2a 2a 20 6f 72  is process.** or
70d80 20 73 69 6e 63 65 20 74 68 65 20 6d 6f 73 74 20   since the most 
70d90 72 65 63 65 6e 74 20 72 65 73 65 74 2e 0a 2a 2f  recent reset..*/
70da0 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
70db0 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  te3_int64 sqlite
70dc0 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74  3_memory_highwat
70dd0 65 72 28 69 6e 74 20 72 65 73 65 74 46 6c 61 67  er(int resetFlag
70de0 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  ){.  sqlite3_int
70df0 36 34 20 6e 3b 0a 20 20 65 6e 74 65 72 4d 65 6d  64 n;.  enterMem
70e00 28 29 3b 0a 20 20 6e 20 3d 20 6d 65 6d 2e 6d 78  ();.  n = mem.mx
70e10 55 73 65 64 3b 0a 20 20 69 66 28 20 72 65 73 65  Used;.  if( rese
70e20 74 46 6c 61 67 20 29 7b 0a 20 20 20 20 6d 65 6d  tFlag ){.    mem
70e30 2e 6d 78 55 73 65 64 20 3d 20 6d 65 6d 2e 6e 6f  .mxUsed = mem.no
70e40 77 55 73 65 64 3b 0a 20 20 7d 0a 20 20 73 71 6c  wUsed;.  }.  sql
70e50 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
70e60 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 20 20 0a 20  (mem.mutex);  . 
70e70 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a   return n;.}../*
70e80 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 61  .** Change the a
70e90 6c 61 72 6d 20 63 61 6c 6c 62 61 63 6b 0a 2a 2f  larm callback.*/
70ea0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
70eb0 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 61  sqlite3_memory_a
70ec0 6c 61 72 6d 28 0a 20 20 76 6f 69 64 28 2a 78 43  larm(.  void(*xC
70ed0 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 20 2a 70  allback)(void *p
70ee0 41 72 67 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74  Arg, sqlite3_int
70ef0 36 34 20 75 73 65 64 2c 69 6e 74 20 4e 29 2c 0a  64 used,int N),.
70f00 20 20 76 6f 69 64 20 2a 70 41 72 67 2c 0a 20 20    void *pArg,.  
70f10 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 54  sqlite3_int64 iT
70f20 68 72 65 73 68 6f 6c 64 0a 29 7b 0a 20 20 65 6e  hreshold.){.  en
70f30 74 65 72 4d 65 6d 28 29 3b 0a 20 20 6d 65 6d 2e  terMem();.  mem.
70f40 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 3d 20  alarmCallback = 
70f50 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6d 65 6d  xCallback;.  mem
70f60 2e 61 6c 61 72 6d 41 72 67 20 3d 20 70 41 72 67  .alarmArg = pArg
70f70 3b 0a 20 20 6d 65 6d 2e 61 6c 61 72 6d 54 68 72  ;.  mem.alarmThr
70f80 65 73 68 6f 6c 64 20 3d 20 69 54 68 72 65 73 68  eshold = iThresh
70f90 6f 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  old;.  sqlite3_m
70fa0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d  utex_leave(mem.m
70fb0 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
70fc0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
70fd0 0a 2a 2a 20 54 72 69 67 67 65 72 20 74 68 65 20  .** Trigger the 
70fe0 61 6c 61 72 6d 20 0a 2a 2f 0a 73 74 61 74 69 63  alarm .*/.static
70ff0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d   void sqlite3Mem
71000 73 79 73 41 6c 61 72 6d 28 69 6e 74 20 6e 42 79  sysAlarm(int nBy
71010 74 65 29 7b 0a 20 20 76 6f 69 64 20 28 2a 78 43  te){.  void (*xC
71020 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 73  allback)(void*,s
71030 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74  qlite3_int64,int
71040 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  );.  sqlite3_int
71050 36 34 20 6e 6f 77 55 73 65 64 3b 0a 20 20 76 6f  64 nowUsed;.  vo
71060 69 64 20 2a 70 41 72 67 3b 0a 20 20 69 66 28 20  id *pArg;.  if( 
71070 6d 65 6d 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63  mem.alarmCallbac
71080 6b 3d 3d 30 20 7c 7c 20 6d 65 6d 2e 61 6c 61 72  k==0 || mem.alar
71090 6d 42 75 73 79 20 20 29 20 72 65 74 75 72 6e 3b  mBusy  ) return;
710a0 0a 20 20 6d 65 6d 2e 61 6c 61 72 6d 42 75 73 79  .  mem.alarmBusy
710b0 20 3d 20 31 3b 0a 20 20 78 43 61 6c 6c 62 61 63   = 1;.  xCallbac
710c0 6b 20 3d 20 6d 65 6d 2e 61 6c 61 72 6d 43 61 6c  k = mem.alarmCal
710d0 6c 62 61 63 6b 3b 0a 20 20 6e 6f 77 55 73 65 64  lback;.  nowUsed
710e0 20 3d 20 6d 65 6d 2e 6e 6f 77 55 73 65 64 3b 0a   = mem.nowUsed;.
710f0 20 20 70 41 72 67 20 3d 20 6d 65 6d 2e 61 6c 61    pArg = mem.ala
71100 72 6d 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33  rmArg;.  sqlite3
71110 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d  _mutex_leave(mem
71120 2e 6d 75 74 65 78 29 3b 0a 20 20 78 43 61 6c 6c  .mutex);.  xCall
71130 62 61 63 6b 28 70 41 72 67 2c 20 6e 6f 77 55 73  back(pArg, nowUs
71140 65 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 73 71  ed, nByte);.  sq
71150 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
71160 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20  r(mem.mutex);.  
71170 6d 65 6d 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20  mem.alarmBusy = 
71180 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  0;.}../*.** Allo
71190 63 61 74 65 20 6e 42 79 74 65 73 20 6f 66 20 6d  cate nBytes of m
711a0 65 6d 6f 72 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f  emory.*/.SQLITE_
711b0 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  API void *sqlite
711c0 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79  3_malloc(int nBy
711d0 74 65 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  tes){.  sqlite3_
711e0 69 6e 74 36 34 20 2a 70 20 3d 20 30 3b 0a 20 20  int64 *p = 0;.  
711f0 69 66 28 20 6e 42 79 74 65 73 3e 30 20 29 7b 0a  if( nBytes>0 ){.
71200 20 20 20 20 65 6e 74 65 72 4d 65 6d 28 29 3b 0a      enterMem();.
71210 20 20 20 20 69 66 28 20 6d 65 6d 2e 61 6c 61 72      if( mem.alar
71220 6d 43 61 6c 6c 62 61 63 6b 21 3d 30 20 26 26 20  mCallback!=0 && 
71230 6d 65 6d 2e 6e 6f 77 55 73 65 64 2b 6e 42 79 74  mem.nowUsed+nByt
71240 65 73 3e 3d 6d 65 6d 2e 61 6c 61 72 6d 54 68 72  es>=mem.alarmThr
71250 65 73 68 6f 6c 64 20 29 7b 0a 20 20 20 20 20 20  eshold ){.      
71260 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 41 6c 61  sqlite3MemsysAla
71270 72 6d 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20  rm(nBytes);.    
71280 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  }.    if( sqlite
71290 33 46 61 75 6c 74 53 74 65 70 28 53 51 4c 49 54  3FaultStep(SQLIT
712a0 45 5f 46 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f  E_FAULTINJECTOR_
712b0 4d 41 4c 4c 4f 43 29 20 29 7b 0a 20 20 20 20 20  MALLOC) ){.     
712c0 20 70 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73   p = 0;.    }els
712d0 65 7b 0a 20 20 20 20 20 20 70 20 3d 20 6d 61 6c  e{.      p = mal
712e0 6c 6f 63 28 6e 42 79 74 65 73 2b 38 29 3b 0a 20  loc(nBytes+8);. 
712f0 20 20 20 20 20 69 66 28 20 70 3d 3d 30 20 29 7b       if( p==0 ){
71300 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
71310 4d 65 6d 73 79 73 41 6c 61 72 6d 28 6e 42 79 74  MemsysAlarm(nByt
71320 65 73 29 3b 0a 20 20 20 20 20 20 20 20 70 20 3d  es);.        p =
71330 20 6d 61 6c 6c 6f 63 28 6e 42 79 74 65 73 2b 38   malloc(nBytes+8
71340 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
71350 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20  .    if( p ){.  
71360 20 20 20 20 70 5b 30 5d 20 3d 20 6e 42 79 74 65      p[0] = nByte
71370 73 3b 0a 20 20 20 20 20 20 70 2b 2b 3b 0a 20 20  s;.      p++;.  
71380 20 20 20 20 6d 65 6d 2e 6e 6f 77 55 73 65 64 20      mem.nowUsed 
71390 2b 3d 20 6e 42 79 74 65 73 3b 0a 20 20 20 20 20  += nBytes;.     
713a0 20 69 66 28 20 6d 65 6d 2e 6e 6f 77 55 73 65 64   if( mem.nowUsed
713b0 3e 6d 65 6d 2e 6d 78 55 73 65 64 20 29 7b 0a 20  >mem.mxUsed ){. 
713c0 20 20 20 20 20 20 20 6d 65 6d 2e 6d 78 55 73 65         mem.mxUse
713d0 64 20 3d 20 6d 65 6d 2e 6e 6f 77 55 73 65 64 3b  d = mem.nowUsed;
713e0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
713f0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
71400 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78  _leave(mem.mutex
71410 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
71420 28 76 6f 69 64 2a 29 70 3b 20 0a 7d 0a 0a 2f 2a  (void*)p; .}../*
71430 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 2e  .** Free memory.
71440 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
71450 6f 69 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65  oid sqlite3_free
71460 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a  (void *pPrior){.
71470 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
71480 2a 70 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b  *p;.  int nByte;
71490 0a 20 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30  .  if( pPrior==0
714a0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
714b0 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6d 65    }.  assert( me
714c0 6d 2e 6d 75 74 65 78 21 3d 30 20 29 3b 0a 20 20  m.mutex!=0 );.  
714d0 70 20 3d 20 70 50 72 69 6f 72 3b 0a 20 20 70 2d  p = pPrior;.  p-
714e0 2d 3b 0a 20 20 6e 42 79 74 65 20 3d 20 28 69 6e  -;.  nByte = (in
714f0 74 29 2a 70 3b 0a 20 20 73 71 6c 69 74 65 33 5f  t)*p;.  sqlite3_
71500 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e  mutex_enter(mem.
71510 6d 75 74 65 78 29 3b 0a 20 20 6d 65 6d 2e 6e 6f  mutex);.  mem.no
71520 77 55 73 65 64 20 2d 3d 20 6e 42 79 74 65 3b 0a  wUsed -= nByte;.
71530 20 20 66 72 65 65 28 70 29 3b 0a 20 20 73 71 6c    free(p);.  sql
71540 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
71550 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 20 20 0a 7d  (mem.mutex);  .}
71560 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
71570 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
71580 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 20  es allocated at 
71590 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  p..*/.SQLITE_PRI
715a0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
715b0 4d 61 6c 6c 6f 63 53 69 7a 65 28 76 6f 69 64 20  MallocSize(void 
715c0 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69  *p){.  sqlite3_i
715d0 6e 74 36 34 20 2a 70 49 6e 74 3b 0a 20 20 69 66  nt64 *pInt;.  if
715e0 28 20 21 70 20 29 20 72 65 74 75 72 6e 20 30 3b  ( !p ) return 0;
715f0 0a 20 20 70 49 6e 74 20 3d 20 70 3b 0a 20 20 72  .  pInt = p;.  r
71600 65 74 75 72 6e 20 70 49 6e 74 5b 2d 31 5d 3b 0a  eturn pInt[-1];.
71610 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  }../*.** Change 
71620 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65  the size of an e
71630 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61  xisting memory a
71640 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 53 51 4c  llocation.*/.SQL
71650 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71  ITE_API void *sq
71660 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 76 6f  lite3_realloc(vo
71670 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20  id *pPrior, int 
71680 6e 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e  nBytes){.  int n
71690 4f 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69  Old;.  sqlite3_i
716a0 6e 74 36 34 20 2a 70 3b 0a 20 20 69 66 28 20 70  nt64 *p;.  if( p
716b0 50 72 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20  Prior==0 ){.    
716c0 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d  return sqlite3_m
716d0 61 6c 6c 6f 63 28 6e 42 79 74 65 73 29 3b 0a 20  alloc(nBytes);. 
716e0 20 7d 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c   }.  if( nBytes<
716f0 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
71700 33 5f 66 72 65 65 28 70 50 72 69 6f 72 29 3b 0a  3_free(pPrior);.
71710 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
71720 7d 0a 20 20 70 20 3d 20 70 50 72 69 6f 72 3b 0a  }.  p = pPrior;.
71730 20 20 70 2d 2d 3b 0a 20 20 6e 4f 6c 64 20 3d 20    p--;.  nOld = 
71740 28 69 6e 74 29 70 5b 30 5d 3b 0a 20 20 61 73 73  (int)p[0];.  ass
71750 65 72 74 28 20 6d 65 6d 2e 6d 75 74 65 78 21 3d  ert( mem.mutex!=
71760 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  0 );.  sqlite3_m
71770 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d  utex_enter(mem.m
71780 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6d 65 6d  utex);.  if( mem
71790 2e 6e 6f 77 55 73 65 64 2b 6e 42 79 74 65 73 2d  .nowUsed+nBytes-
717a0 6e 4f 6c 64 3e 3d 6d 65 6d 2e 61 6c 61 72 6d 54  nOld>=mem.alarmT
717b0 68 72 65 73 68 6f 6c 64 20 29 7b 0a 20 20 20 20  hreshold ){.    
717c0 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 41 6c 61  sqlite3MemsysAla
717d0 72 6d 28 6e 42 79 74 65 73 2d 6e 4f 6c 64 29 3b  rm(nBytes-nOld);
717e0 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74  .  }.  if( sqlit
717f0 65 33 46 61 75 6c 74 53 74 65 70 28 53 51 4c 49  e3FaultStep(SQLI
71800 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43 54 4f 52  TE_FAULTINJECTOR
71810 5f 4d 41 4c 4c 4f 43 29 20 29 7b 0a 20 20 20 20  _MALLOC) ){.    
71820 70 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  p = 0;.  }else{.
71830 20 20 20 20 70 20 3d 20 72 65 61 6c 6c 6f 63 28      p = realloc(
71840 70 2c 20 6e 42 79 74 65 73 2b 38 29 3b 0a 20 20  p, nBytes+8);.  
71850 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20    if( p==0 ){.  
71860 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 73 79      sqlite3Memsy
71870 73 41 6c 61 72 6d 28 6e 42 79 74 65 73 29 3b 0a  sAlarm(nBytes);.
71880 20 20 20 20 20 20 70 20 3d 20 70 50 72 69 6f 72        p = pPrior
71890 3b 0a 20 20 20 20 20 20 70 2d 2d 3b 0a 20 20 20  ;.      p--;.   
718a0 20 20 20 70 20 3d 20 72 65 61 6c 6c 6f 63 28 70     p = realloc(p
718b0 2c 20 6e 42 79 74 65 73 2b 38 29 3b 0a 20 20 20  , nBytes+8);.   
718c0 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 20 29   }.  }.  if( p )
718d0 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 6e 42 79  {.    p[0] = nBy
718e0 74 65 73 3b 0a 20 20 20 20 70 2b 2b 3b 0a 20 20  tes;.    p++;.  
718f0 20 20 6d 65 6d 2e 6e 6f 77 55 73 65 64 20 2b 3d    mem.nowUsed +=
71900 20 6e 42 79 74 65 73 2d 6e 4f 6c 64 3b 0a 20 20   nBytes-nOld;.  
71910 20 20 69 66 28 20 6d 65 6d 2e 6e 6f 77 55 73 65    if( mem.nowUse
71920 64 3e 6d 65 6d 2e 6d 78 55 73 65 64 20 29 7b 0a  d>mem.mxUsed ){.
71930 20 20 20 20 20 20 6d 65 6d 2e 6d 78 55 73 65 64        mem.mxUsed
71940 20 3d 20 6d 65 6d 2e 6e 6f 77 55 73 65 64 3b 0a   = mem.nowUsed;.
71950 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69      }.  }.  sqli
71960 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
71970 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65  mem.mutex);.  re
71980 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b 0a 7d  turn (void*)p;.}
71990 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
719a0 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43  TE_SYSTEM_MALLOC
719b0 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
719c0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 31  **** End of mem1
719d0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
719e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
719f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
71a00 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
71a10 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
71a20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mem2.c *********
71a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
71a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
71a50 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20  ***/./*.** 2007 
71a60 41 75 67 75 73 74 20 31 35 0a 2a 2a 0a 2a 2a 20  August 15.**.** 
71a70 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
71a80 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
71a90 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
71aa0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
71ab0 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
71ac0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
71ad0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
71ae0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
71af0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
71b00 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
71b10 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
71b20 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
71b30 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
71b40 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
71b50 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
71b60 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
71b70 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
71b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
71b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
71ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
71bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
71bc0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
71bd0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74   file contains t
71be0 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74  he C functions t
71bf0 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20  hat implement a 
71c00 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61  memory.** alloca
71c10 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 66  tion subsystem f
71c20 6f 72 20 75 73 65 20 62 79 20 53 51 4c 69 74 65  or use by SQLite
71c30 2e 20 20 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d  .  .**.** $Id: m
71c40 65 6d 32 2e 63 2c 76 20 31 2e 32 36 20 32 30 30  em2.c,v 1.26 200
71c50 38 2f 30 34 2f 31 30 20 31 34 3a 35 37 3a 32 35  8/04/10 14:57:25
71c60 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f   drh Exp $.*/../
71c70 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f  *.** This versio
71c80 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20  n of the memory 
71c90 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 75 73 65  allocator is use
71ca0 64 20 6f 6e 6c 79 20 69 66 20 74 68 65 0a 2a 2a  d only if the.**
71cb0 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47   SQLITE_MEMDEBUG
71cc0 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65   macro is define
71cd0 64 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  d.*/.#ifdef SQLI
71ce0 54 45 5f 4d 45 4d 44 45 42 55 47 0a 0a 2f 2a 0a  TE_MEMDEBUG../*.
71cf0 2a 2a 20 54 68 65 20 62 61 63 6b 74 72 61 63 65  ** The backtrace
71d00 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20 69   functionality i
71d10 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65  s only available
71d20 20 77 69 74 68 20 47 4c 49 42 43 0a 2a 2f 0a 23   with GLIBC.*/.#
71d30 69 66 64 65 66 20 5f 5f 47 4c 49 42 43 5f 5f 0a  ifdef __GLIBC__.
71d40 20 20 65 78 74 65 72 6e 20 69 6e 74 20 62 61 63    extern int bac
71d50 6b 74 72 61 63 65 28 76 6f 69 64 2a 2a 2c 69 6e  ktrace(void**,in
71d60 74 29 3b 0a 20 20 65 78 74 65 72 6e 20 76 6f 69  t);.  extern voi
71d70 64 20 62 61 63 6b 74 72 61 63 65 5f 73 79 6d 62  d backtrace_symb
71d80 6f 6c 73 5f 66 64 28 76 6f 69 64 2a 63 6f 6e 73  ols_fd(void*cons
71d90 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 23 65 6c  t*,int,int);.#el
71da0 73 65 0a 23 20 64 65 66 69 6e 65 20 62 61 63 6b  se.# define back
71db0 74 72 61 63 65 28 41 2c 42 29 20 30 0a 23 20 64  trace(A,B) 0.# d
71dc0 65 66 69 6e 65 20 62 61 63 6b 74 72 61 63 65 5f  efine backtrace_
71dd0 73 79 6d 62 6f 6c 73 5f 66 64 28 41 2c 42 2c 43  symbols_fd(A,B,C
71de0 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
71df0 45 61 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  Each memory allo
71e00 63 61 74 69 6f 6e 20 6c 6f 6f 6b 73 20 6c 69 6b  cation looks lik
71e10 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 2d  e this:.**.**  -
71e20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
71e30 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
71e40 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
71e50 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
71e60 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 7c 20 54 69  -------.**  | Ti
71e70 74 6c 65 20 7c 20 20 62 61 63 6b 74 72 61 63 65  tle |  backtrace
71e80 20 70 6f 69 6e 74 65 72 73 20 7c 20 20 4d 65 6d   pointers |  Mem
71e90 42 6c 6f 63 6b 48 64 72 20 7c 20 20 61 6c 6c 6f  BlockHdr |  allo
71ea0 63 61 74 69 6f 6e 20 7c 20 20 45 6e 64 47 75 61  cation |  EndGua
71eb0 72 64 20 7c 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d  rd |.**  -------
71ec0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
71ed0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
71ee0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
71ef0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
71f00 2d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c  -.**.** The appl
71f10 69 63 61 74 69 6f 6e 20 63 6f 64 65 20 73 65 65  ication code see
71f20 73 20 6f 6e 6c 79 20 61 20 70 6f 69 6e 74 65 72  s only a pointer
71f30 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 69   to the allocati
71f40 6f 6e 2e 20 20 57 65 20 68 61 76 65 0a 2a 2a 20  on.  We have.** 
71f50 74 6f 20 62 61 63 6b 20 75 70 20 66 72 6f 6d 20  to back up from 
71f60 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70  the allocation p
71f70 6f 69 6e 74 65 72 20 74 6f 20 66 69 6e 64 20 74  ointer to find t
71f80 68 65 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e 20  he MemBlockHdr. 
71f90 20 54 68 65 0a 2a 2a 20 4d 65 6d 42 6c 6f 63 6b   The.** MemBlock
71fa0 48 64 72 20 74 65 6c 6c 73 20 75 73 20 74 68 65  Hdr tells us the
71fb0 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c   size of the all
71fc0 6f 63 61 74 69 6f 6e 20 61 6e 64 20 74 68 65 20  ocation and the 
71fd0 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 61 63  number of.** bac
71fe0 6b 74 72 61 63 65 20 70 6f 69 6e 74 65 72 73 2e  ktrace pointers.
71ff0 20 20 54 68 65 72 65 20 69 73 20 61 6c 73 6f 20    There is also 
72000 61 20 67 75 61 72 64 20 77 6f 72 64 20 61 74 20  a guard word at 
72010 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a  the end of the.*
72020 2a 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e 0a 2a  * MemBlockHdr..*
72030 2f 0a 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63  /.struct MemBloc
72040 6b 48 64 72 20 7b 0a 20 20 69 36 34 20 69 53 69  kHdr {.  i64 iSi
72050 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
72060 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
72070 53 69 7a 65 20 6f 66 20 74 68 69 73 20 61 6c 6c  Size of this all
72080 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72  ocation */.  str
72090 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20  uct MemBlockHdr 
720a0 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65 76 3b 20  *pNext, *pPrev; 
720b0 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20   /* Linked list 
720c0 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d  of all unfreed m
720d0 65 6d 6f 72 79 20 2a 2f 0a 20 20 63 68 61 72 20  emory */.  char 
720e0 6e 42 61 63 6b 74 72 61 63 65 3b 20 20 20 20 20  nBacktrace;     
720f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
72100 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 61 63 6b  * Number of back
72110 74 72 61 63 65 73 20 6f 6e 20 74 68 69 73 20 61  traces on this a
72120 6c 6c 6f 63 20 2a 2f 0a 20 20 63 68 61 72 20 6e  lloc */.  char n
72130 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b 20  BacktraceSlots; 
72140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
72150 20 41 76 61 69 6c 61 62 6c 65 20 62 61 63 6b 74   Available backt
72160 72 61 63 65 20 73 6c 6f 74 73 20 2a 2f 0a 20 20  race slots */.  
72170 73 68 6f 72 74 20 6e 54 69 74 6c 65 3b 20 20 20  short nTitle;   
72180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72190 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20      /* Bytes of 
721a0 74 69 74 6c 65 3b 20 69 6e 63 6c 75 64 65 73 20  title; includes 
721b0 27 5c 30 27 20 2a 2f 0a 20 20 69 6e 74 20 69 46  '\0' */.  int iF
721c0 6f 72 65 47 75 61 72 64 3b 20 20 20 20 20 20 20  oreGuard;       
721d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
721e0 20 47 75 61 72 64 20 77 6f 72 64 20 66 6f 72 20   Guard word for 
721f0 73 61 6e 69 74 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  sanity */.};../*
72200 0a 2a 2a 20 47 75 61 72 64 20 77 6f 72 64 73 0a  .** Guard words.
72210 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4f 52 45 47  */.#define FOREG
72220 55 41 52 44 20 30 78 38 30 46 35 45 31 35 33 0a  UARD 0x80F5E153.
72230 23 64 65 66 69 6e 65 20 52 45 41 52 47 55 41 52  #define REARGUAR
72240 44 20 30 78 45 34 36 37 36 42 35 33 0a 0a 2f 2a  D 0xE4676B53../*
72250 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61  .** Number of ma
72260 6c 6c 6f 63 20 73 69 7a 65 20 69 6e 63 72 65 6d  lloc size increm
72270 65 6e 74 73 20 74 6f 20 74 72 61 63 6b 2e 0a 2a  ents to track..*
72280 2f 0a 23 64 65 66 69 6e 65 20 4e 43 53 49 5a 45  /.#define NCSIZE
72290 20 20 31 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 41 6c    1000../*.** Al
722a0 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 63 20  l of the static 
722b0 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 20 62  variables used b
722c0 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 72  y this module ar
722d0 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 69  e collected.** i
722e0 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74 72  nto a single str
722f0 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d 65  ucture named "me
72300 6d 22 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20  m".  This is to 
72310 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 61 74  keep the.** stat
72320 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f 72 67  ic variables org
72330 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 72 65  anized and to re
72340 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65 20 70  duce namespace p
72350 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e  ollution.** when
72360 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20   this module is 
72370 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 6f 74  combined with ot
72380 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61 6c 67  her in the amalg
72390 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  amation..*/.stat
723a0 69 63 20 73 74 72 75 63 74 20 7b 0a 20 20 2f 2a  ic struct {.  /*
723b0 0a 20 20 2a 2a 20 54 68 65 20 61 6c 61 72 6d 20  .  ** The alarm 
723c0 63 61 6c 6c 62 61 63 6b 20 61 6e 64 20 69 74 73  callback and its
723d0 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 54 68 65   arguments.  The
723e0 20 6d 65 6d 2e 6d 75 74 65 78 20 6c 6f 63 6b 20   mem.mutex lock 
723f0 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 68 65 6c  will.  ** be hel
72400 64 20 77 68 69 6c 65 20 74 68 65 20 63 61 6c 6c  d while the call
72410 62 61 63 6b 20 69 73 20 72 75 6e 6e 69 6e 67 2e  back is running.
72420 20 20 52 65 63 75 72 73 69 76 65 20 63 61 6c 6c    Recursive call
72430 73 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 20  s into.  ** the 
72440 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d  memory subsystem
72450 20 61 72 65 20 61 6c 6c 6f 77 65 64 2c 20 62 75   are allowed, bu
72460 74 20 6e 6f 20 6e 65 77 20 63 61 6c 6c 62 61 63  t no new callbac
72470 6b 73 20 77 69 6c 6c 20 62 65 0a 20 20 2a 2a 20  ks will be.  ** 
72480 69 73 73 75 65 64 2e 20 20 54 68 65 20 61 6c 61  issued.  The ala
72490 72 6d 42 75 73 79 20 76 61 72 69 61 62 6c 65 20  rmBusy variable 
724a0 69 73 20 73 65 74 20 74 6f 20 70 72 65 76 65 6e  is set to preven
724b0 74 20 72 65 63 75 72 73 69 76 65 0a 20 20 2a 2a  t recursive.  **
724c0 20 63 61 6c 6c 62 61 63 6b 73 2e 0a 20 20 2a 2f   callbacks..  */
724d0 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
724e0 20 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 3b   alarmThreshold;
724f0 0a 20 20 76 6f 69 64 20 28 2a 61 6c 61 72 6d 43  .  void (*alarmC
72500 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 20  allback)(void*, 
72510 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 69  sqlite3_int64, i
72520 6e 74 29 3b 0a 20 20 76 6f 69 64 20 2a 61 6c 61  nt);.  void *ala
72530 72 6d 41 72 67 3b 0a 20 20 69 6e 74 20 61 6c 61  rmArg;.  int ala
72540 72 6d 42 75 73 79 3b 0a 20 20 0a 20 20 2f 2a 0a  rmBusy;.  .  /*.
72550 20 20 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f    ** Mutex to co
72560 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20  ntrol access to 
72570 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
72580 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e  ation subsystem.
72590 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  .  */.  sqlite3_
725a0 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 20 20  mutex *mutex;.  
725b0 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 75 72 72 65  .  /*.  ** Curre
725c0 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e  nt allocation an
725d0 64 20 68 69 67 68 2d 77 61 74 65 72 20 6d 61 72  d high-water mar
725e0 6b 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  k..  */.  sqlite
725f0 33 5f 69 6e 74 36 34 20 6e 6f 77 55 73 65 64 3b  3_int64 nowUsed;
72600 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
72610 20 6d 78 55 73 65 64 3b 0a 20 20 0a 20 20 2f 2a   mxUsed;.  .  /*
72620 0a 20 20 2a 2a 20 48 65 61 64 20 61 6e 64 20 74  .  ** Head and t
72630 61 69 6c 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20  ail of a linked 
72640 6c 69 73 74 20 6f 66 20 61 6c 6c 20 6f 75 74 73  list of all outs
72650 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69  tanding allocati
72660 6f 6e 73 0a 20 20 2a 2f 0a 20 20 73 74 72 75 63  ons.  */.  struc
72670 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70  t MemBlockHdr *p
72680 46 69 72 73 74 3b 0a 20 20 73 74 72 75 63 74 20  First;.  struct 
72690 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 4c 61  MemBlockHdr *pLa
726a0 73 74 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a  st;.  .  /*.  **
726b0 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c   The number of l
726c0 65 76 65 6c 73 20 6f 66 20 62 61 63 6b 74 72 61  evels of backtra
726d0 63 65 20 74 6f 20 73 61 76 65 20 69 6e 20 6e 65  ce to save in ne
726e0 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20  w allocations.. 
726f0 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b 74   */.  int nBackt
72700 72 61 63 65 3b 0a 20 20 76 6f 69 64 20 28 2a 78  race;.  void (*x
72710 42 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20  Backtrace)(int, 
72720 69 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 3b 0a 0a  int, void **);..
72730 20 20 2f 2a 0a 20 20 2a 2a 20 54 69 74 6c 65 20    /*.  ** Title 
72740 74 65 78 74 20 74 6f 20 69 6e 73 65 72 74 20 69  text to insert i
72750 6e 20 66 72 6f 6e 74 20 6f 66 20 65 61 63 68 20  n front of each 
72760 62 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 6e 74  block.  */.  int
72770 20 6e 54 69 74 6c 65 3b 20 20 20 20 20 20 20 20   nTitle;        
72780 2f 2a 20 42 79 74 65 73 20 6f 66 20 7a 54 69 74  /* Bytes of zTit
72790 6c 65 20 74 6f 20 73 61 76 65 2e 20 20 49 6e 63  le to save.  Inc
727a0 6c 75 64 65 73 20 27 5c 30 27 20 61 6e 64 20 70  ludes '\0' and p
727b0 61 64 64 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72  adding */.  char
727c0 20 7a 54 69 74 6c 65 5b 31 30 30 5d 3b 20 20 2f   zTitle[100];  /
727d0 2a 20 54 68 65 20 74 69 74 6c 65 20 74 65 78 74  * The title text
727e0 20 2a 2f 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20   */..  /* .  ** 
727f0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 44 69 73  sqlite3MallocDis
72800 61 6c 6c 6f 77 28 29 20 69 6e 63 72 65 6d 65 6e  allow() incremen
72810 74 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ts the following
72820 20 63 6f 75 6e 74 65 72 2e 0a 20 20 2a 2a 20 73   counter..  ** s
72830 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c 6f  qlite3MallocAllo
72840 77 28 29 20 64 65 63 72 65 6d 65 6e 74 73 20 69  w() decrements i
72850 74 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 64 69  t..  */.  int di
72860 73 61 6c 6c 6f 77 3b 20 2f 2a 20 44 6f 20 6e 6f  sallow; /* Do no
72870 74 20 61 6c 6c 6f 77 20 6d 65 6d 6f 72 79 20 61  t allow memory a
72880 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 0a 20 20  llocation */..  
72890 2f 2a 0a 20 20 2a 2a 20 47 61 74 68 65 72 20 73  /*.  ** Gather s
728a0 74 61 74 69 73 74 69 63 73 20 6f 6e 20 74 68 65  tatistics on the
728b0 20 73 69 7a 65 73 20 6f 66 20 6d 65 6d 6f 72 79   sizes of memory
728c0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20   allocations..  
728d0 2a 2a 20 73 69 7a 65 43 6e 74 5b 69 5d 20 69 73  ** sizeCnt[i] is
728e0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61   the number of a
728f0 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70  llocation attemp
72900 74 73 20 6f 66 20 69 2a 38 0a 20 20 2a 2a 20 62  ts of i*8.  ** b
72910 79 74 65 73 2e 20 20 69 3d 3d 4e 43 53 49 5a 45  ytes.  i==NCSIZE
72920 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
72930 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74  f allocation att
72940 65 6d 70 74 73 20 66 6f 72 0a 20 20 2a 2a 20 73  empts for.  ** s
72950 69 7a 65 73 20 6d 6f 72 65 20 74 68 61 6e 20 4e  izes more than N
72960 43 53 49 5a 45 2a 38 20 62 79 74 65 73 2e 0a 20  CSIZE*8 bytes.. 
72970 20 2a 2f 0a 20 20 69 6e 74 20 73 69 7a 65 43 6e   */.  int sizeCn
72980 74 5b 4e 43 53 49 5a 45 5d 3b 0a 0a 7d 20 6d 65  t[NCSIZE];..} me
72990 6d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72  m;.../*.** Enter
729a0 20 74 68 65 20 6d 75 74 65 78 20 6d 65 6d 2e 6d   the mutex mem.m
729b0 75 74 65 78 2e 20 41 6c 6c 6f 63 61 74 65 20 69  utex. Allocate i
729c0 74 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20 61  t if it is not a
729d0 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 64  lready allocated
729e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
729f0 20 65 6e 74 65 72 4d 65 6d 28 76 6f 69 64 29 7b   enterMem(void){
72a00 0a 20 20 69 66 28 20 6d 65 6d 2e 6d 75 74 65 78  .  if( mem.mutex
72a10 3d 3d 30 20 29 7b 0a 20 20 20 20 6d 65 6d 2e 6d  ==0 ){.    mem.m
72a20 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d  utex = sqlite3_m
72a30 75 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54  utex_alloc(SQLIT
72a40 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
72a50 45 4d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  EM);.  }.  sqlit
72a60 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
72a70 65 6d 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 2f 2a  em.mutex);.}../*
72a80 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 61  .** Return the a
72a90 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20  mount of memory 
72aa0 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65  currently checke
72ab0 64 20 6f 75 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  d out..*/.SQLITE
72ac0 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74  _API sqlite3_int
72ad0 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72  64 sqlite3_memor
72ae0 79 5f 75 73 65 64 28 76 6f 69 64 29 7b 0a 20 20  y_used(void){.  
72af0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 3b  sqlite3_int64 n;
72b00 0a 20 20 65 6e 74 65 72 4d 65 6d 28 29 3b 0a 20  .  enterMem();. 
72b10 20 6e 20 3d 20 6d 65 6d 2e 6e 6f 77 55 73 65 64   n = mem.nowUsed
72b20 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
72b30 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65  x_leave(mem.mute
72b40 78 29 3b 20 20 0a 20 20 72 65 74 75 72 6e 20 6e  x);  .  return n
72b50 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
72b60 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d  n the maximum am
72b70 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 74  ount of memory t
72b80 68 61 74 20 68 61 73 20 65 76 65 72 20 62 65 65  hat has ever bee
72b90 6e 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75 74  n.** checked out
72ba0 20 73 69 6e 63 65 20 65 69 74 68 65 72 20 74 68   since either th
72bb0 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74  e beginning of t
72bc0 68 69 73 20 70 72 6f 63 65 73 73 0a 2a 2a 20 6f  his process.** o
72bd0 72 20 73 69 6e 63 65 20 74 68 65 20 6d 6f 73 74  r since the most
72be0 20 72 65 63 65 6e 74 20 72 65 73 65 74 2e 0a 2a   recent reset..*
72bf0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  /.SQLITE_API sql
72c00 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74  ite3_int64 sqlit
72c10 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61  e3_memory_highwa
72c20 74 65 72 28 69 6e 74 20 72 65 73 65 74 46 6c 61  ter(int resetFla
72c30 67 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  g){.  sqlite3_in
72c40 74 36 34 20 6e 3b 0a 20 20 65 6e 74 65 72 4d 65  t64 n;.  enterMe
72c50 6d 28 29 3b 0a 20 20 6e 20 3d 20 6d 65 6d 2e 6d  m();.  n = mem.m
72c60 78 55 73 65 64 3b 0a 20 20 69 66 28 20 72 65 73  xUsed;.  if( res
72c70 65 74 46 6c 61 67 20 29 7b 0a 20 20 20 20 6d 65  etFlag ){.    me
72c80 6d 2e 6d 78 55 73 65 64 20 3d 20 6d 65 6d 2e 6e  m.mxUsed = mem.n
72c90 6f 77 55 73 65 64 3b 0a 20 20 7d 0a 20 20 73 71  owUsed;.  }.  sq
72ca0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
72cb0 65 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 20 20 0a  e(mem.mutex);  .
72cc0 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f    return n;.}../
72cd0 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
72ce0 61 6c 61 72 6d 20 63 61 6c 6c 62 61 63 6b 0a 2a  alarm callback.*
72cf0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
72d00 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f   sqlite3_memory_
72d10 61 6c 61 72 6d 28 0a 20 20 76 6f 69 64 28 2a 78  alarm(.  void(*x
72d20 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 20 2a  Callback)(void *
72d30 70 41 72 67 2c 20 73 71 6c 69 74 65 33 5f 69 6e  pArg, sqlite3_in
72d40 74 36 34 20 75 73 65 64 2c 20 69 6e 74 20 4e 29  t64 used, int N)
72d50 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 2c 0a  ,.  void *pArg,.
72d60 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
72d70 69 54 68 72 65 73 68 6f 6c 64 0a 29 7b 0a 20 20  iThreshold.){.  
72d80 65 6e 74 65 72 4d 65 6d 28 29 3b 0a 20 20 6d 65  enterMem();.  me
72d90 6d 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20  m.alarmCallback 
72da0 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6d  = xCallback;.  m
72db0 65 6d 2e 61 6c 61 72 6d 41 72 67 20 3d 20 70 41  em.alarmArg = pA
72dc0 72 67 3b 0a 20 20 6d 65 6d 2e 61 6c 61 72 6d 54  rg;.  mem.alarmT
72dd0 68 72 65 73 68 6f 6c 64 20 3d 20 69 54 68 72 65  hreshold = iThre
72de0 73 68 6f 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33  shold;.  sqlite3
72df0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d  _mutex_leave(mem
72e00 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  .mutex);.  retur
72e10 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
72e20 2f 2a 0a 2a 2a 20 54 72 69 67 67 65 72 20 74 68  /*.** Trigger th
72e30 65 20 61 6c 61 72 6d 20 0a 2a 2f 0a 73 74 61 74  e alarm .*/.stat
72e40 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d  ic void sqlite3M
72e50 65 6d 73 79 73 41 6c 61 72 6d 28 69 6e 74 20 6e  emsysAlarm(int n
72e60 42 79 74 65 29 7b 0a 20 20 76 6f 69 64 20 28 2a  Byte){.  void (*
72e70 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a  xCallback)(void*
72e80 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69  ,sqlite3_int64,i
72e90 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69  nt);.  sqlite3_i
72ea0 6e 74 36 34 20 6e 6f 77 55 73 65 64 3b 0a 20 20  nt64 nowUsed;.  
72eb0 76 6f 69 64 20 2a 70 41 72 67 3b 0a 20 20 69 66  void *pArg;.  if
72ec0 28 20 6d 65 6d 2e 61 6c 61 72 6d 43 61 6c 6c 62  ( mem.alarmCallb
72ed0 61 63 6b 3d 3d 30 20 7c 7c 20 6d 65 6d 2e 61 6c  ack==0 || mem.al
72ee0 61 72 6d 42 75 73 79 20 20 29 20 72 65 74 75 72  armBusy  ) retur
72ef0 6e 3b 0a 20 20 6d 65 6d 2e 61 6c 61 72 6d 42 75  n;.  mem.alarmBu
72f00 73 79 20 3d 20 31 3b 0a 20 20 78 43 61 6c 6c 62  sy = 1;.  xCallb
72f10 61 63 6b 20 3d 20 6d 65 6d 2e 61 6c 61 72 6d 43  ack = mem.alarmC
72f20 61 6c 6c 62 61 63 6b 3b 0a 20 20 6e 6f 77 55 73  allback;.  nowUs
72f30 65 64 20 3d 20 6d 65 6d 2e 6e 6f 77 55 73 65 64  ed = mem.nowUsed
72f40 3b 0a 20 20 70 41 72 67 20 3d 20 6d 65 6d 2e 61  ;.  pArg = mem.a
72f50 6c 61 72 6d 41 72 67 3b 0a 20 20 73 71 6c 69 74  larmArg;.  sqlit
72f60 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
72f70 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 78 43 61  em.mutex);.  xCa
72f80 6c 6c 62 61 63 6b 28 70 41 72 67 2c 20 6e 6f 77  llback(pArg, now
72f90 55 73 65 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20  Used, nByte);.  
72fa0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
72fb0 74 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a  ter(mem.mutex);.
72fc0 20 20 6d 65 6d 2e 61 6c 61 72 6d 42 75 73 79 20    mem.alarmBusy 
72fd0 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69  = 0;.}../*.** Gi
72fe0 76 65 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f  ven an allocatio
72ff0 6e 2c 20 66 69 6e 64 20 74 68 65 20 4d 65 6d 42  n, find the MemB
73000 6c 6f 63 6b 48 64 72 20 66 6f 72 20 74 68 61 74  lockHdr for that
73010 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a   allocation..**.
73020 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
73030 63 68 65 63 6b 73 20 74 68 65 20 67 75 61 72 64  checks the guard
73040 73 20 61 74 20 65 69 74 68 65 72 20 65 6e 64 20  s at either end 
73050 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f  of the allocatio
73060 6e 20 61 6e 64 0a 2a 2a 20 69 66 20 74 68 65 79  n and.** if they
73070 20 61 72 65 20 69 6e 63 6f 72 72 65 63 74 20 69   are incorrect i
73080 74 20 61 73 73 65 72 74 73 2e 0a 2a 2f 0a 73 74  t asserts..*/.st
73090 61 74 69 63 20 73 74 72 75 63 74 20 4d 65 6d 42  atic struct MemB
730a0 6c 6f 63 6b 48 64 72 20 2a 73 71 6c 69 74 65 33  lockHdr *sqlite3
730b0 4d 65 6d 73 79 73 47 65 74 48 65 61 64 65 72 28  MemsysGetHeader(
730c0 76 6f 69 64 20 2a 70 41 6c 6c 6f 63 61 74 69 6f  void *pAllocatio
730d0 6e 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d  n){.  struct Mem
730e0 42 6c 6f 63 6b 48 64 72 20 2a 70 3b 0a 20 20 69  BlockHdr *p;.  i
730f0 6e 74 20 2a 70 49 6e 74 3b 0a 20 20 75 38 20 2a  nt *pInt;.  u8 *
73100 70 55 38 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65  pU8;.  int nRese
73110 72 76 65 3b 0a 0a 20 20 70 20 3d 20 28 73 74 72  rve;..  p = (str
73120 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a  uct MemBlockHdr*
73130 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20  )pAllocation;.  
73140 70 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  p--;.  assert( p
73150 2d 3e 69 46 6f 72 65 47 75 61 72 64 3d 3d 46 4f  ->iForeGuard==FO
73160 52 45 47 55 41 52 44 20 29 3b 0a 20 20 6e 52 65  REGUARD );.  nRe
73170 73 65 72 76 65 20 3d 20 28 70 2d 3e 69 53 69 7a  serve = (p->iSiz
73180 65 2b 37 29 26 7e 37 3b 0a 20 20 70 49 6e 74 20  e+7)&~7;.  pInt 
73190 3d 20 28 69 6e 74 2a 29 70 41 6c 6c 6f 63 61 74  = (int*)pAllocat
731a0 69 6f 6e 3b 0a 20 20 70 55 38 20 3d 20 28 75 38  ion;.  pU8 = (u8
731b0 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20  *)pAllocation;. 
731c0 20 61 73 73 65 72 74 28 20 70 49 6e 74 5b 6e 52   assert( pInt[nR
731d0 65 73 65 72 76 65 2f 73 69 7a 65 6f 66 28 69 6e  eserve/sizeof(in
731e0 74 29 5d 3d 3d 52 45 41 52 47 55 41 52 44 20 29  t)]==REARGUARD )
731f0 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6e 52 65  ;.  assert( (nRe
73200 73 65 72 76 65 2d 30 29 3c 3d 70 2d 3e 69 53 69  serve-0)<=p->iSi
73210 7a 65 20 7c 7c 20 70 55 38 5b 6e 52 65 73 65 72  ze || pU8[nReser
73220 76 65 2d 31 5d 3d 3d 30 78 36 35 20 29 3b 0a 20  ve-1]==0x65 );. 
73230 20 61 73 73 65 72 74 28 20 28 6e 52 65 73 65 72   assert( (nReser
73240 76 65 2d 31 29 3c 3d 70 2d 3e 69 53 69 7a 65 20  ve-1)<=p->iSize 
73250 7c 7c 20 70 55 38 5b 6e 52 65 73 65 72 76 65 2d  || pU8[nReserve-
73260 32 5d 3d 3d 30 78 36 35 20 29 3b 0a 20 20 61 73  2]==0x65 );.  as
73270 73 65 72 74 28 20 28 6e 52 65 73 65 72 76 65 2d  sert( (nReserve-
73280 32 29 3c 3d 70 2d 3e 69 53 69 7a 65 20 7c 7c 20  2)<=p->iSize || 
73290 70 55 38 5b 6e 52 65 73 65 72 76 65 2d 33 5d 3d  pU8[nReserve-3]=
732a0 3d 30 78 36 35 20 29 3b 0a 20 20 72 65 74 75 72  =0x65 );.  retur
732b0 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  n p;.}../*.** Re
732c0 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
732d0 6f 66 20 62 79 74 65 73 20 63 75 72 72 65 6e 74  of bytes current
732e0 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 20  ly allocated at 
732f0 61 64 64 72 65 73 73 20 70 2e 0a 2a 2f 0a 53 51  address p..*/.SQ
73300 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
73310 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69   sqlite3MallocSi
73320 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 73  ze(void *p){.  s
73330 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64  truct MemBlockHd
73340 72 20 2a 70 48 64 72 3b 0a 20 20 69 66 28 20 21  r *pHdr;.  if( !
73350 70 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  p ){.    return 
73360 30 3b 0a 20 20 7d 0a 20 20 70 48 64 72 20 3d 20  0;.  }.  pHdr = 
73370 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74  sqlite3MemsysGet
73380 48 65 61 64 65 72 28 70 29 3b 0a 20 20 72 65 74  Header(p);.  ret
73390 75 72 6e 20 70 48 64 72 2d 3e 69 53 69 7a 65 3b  urn pHdr->iSize;
733a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  .}../*.** Alloca
733b0 74 65 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f  te nByte bytes o
733c0 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 53 51 4c  f memory..*/.SQL
733d0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71  ITE_API void *sq
733e0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74  lite3_malloc(int
733f0 20 6e 42 79 74 65 29 7b 0a 20 20 73 74 72 75 63   nByte){.  struc
73400 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70  t MemBlockHdr *p
73410 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42  Hdr;.  void **pB
73420 74 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20  t;.  char *z;.  
73430 69 6e 74 20 2a 70 49 6e 74 3b 0a 20 20 76 6f 69  int *pInt;.  voi
73440 64 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20  d *p = 0;.  int 
73450 74 6f 74 61 6c 53 69 7a 65 3b 0a 0a 20 20 69 66  totalSize;..  if
73460 28 20 6e 42 79 74 65 3e 30 20 29 7b 0a 20 20 20  ( nByte>0 ){.   
73470 20 69 6e 74 20 6e 52 65 73 65 72 76 65 3b 0a 20   int nReserve;. 
73480 20 20 20 65 6e 74 65 72 4d 65 6d 28 29 3b 0a 20     enterMem();. 
73490 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 64     assert( mem.d
734a0 69 73 61 6c 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20  isallow==0 );.  
734b0 20 20 69 66 28 20 6d 65 6d 2e 61 6c 61 72 6d 43    if( mem.alarmC
734c0 61 6c 6c 62 61 63 6b 21 3d 30 20 26 26 20 6d 65  allback!=0 && me
734d0 6d 2e 6e 6f 77 55 73 65 64 2b 6e 42 79 74 65 3e  m.nowUsed+nByte>
734e0 3d 6d 65 6d 2e 61 6c 61 72 6d 54 68 72 65 73 68  =mem.alarmThresh
734f0 6f 6c 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  old ){.      sql
73500 69 74 65 33 4d 65 6d 73 79 73 41 6c 61 72 6d 28  ite3MemsysAlarm(
73510 6e 42 79 74 65 29 3b 0a 20 20 20 20 7d 0a 20 20  nByte);.    }.  
73520 20 20 6e 52 65 73 65 72 76 65 20 3d 20 28 6e 42    nReserve = (nB
73530 79 74 65 2b 37 29 26 7e 37 3b 0a 20 20 20 20 69  yte+7)&~7;.    i
73540 66 28 20 6e 52 65 73 65 72 76 65 2f 38 3e 4e 43  f( nReserve/8>NC
73550 53 49 5a 45 2d 31 20 29 7b 0a 20 20 20 20 20 20  SIZE-1 ){.      
73560 6d 65 6d 2e 73 69 7a 65 43 6e 74 5b 4e 43 53 49  mem.sizeCnt[NCSI
73570 5a 45 2d 31 5d 2b 2b 3b 0a 20 20 20 20 7d 65 6c  ZE-1]++;.    }el
73580 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 73 69  se{.      mem.si
73590 7a 65 43 6e 74 5b 6e 52 65 73 65 72 76 65 2f 38  zeCnt[nReserve/8
735a0 5d 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 74  ]++;.    }.    t
735b0 6f 74 61 6c 53 69 7a 65 20 3d 20 6e 52 65 73 65  otalSize = nRese
735c0 72 76 65 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48  rve + sizeof(*pH
735d0 64 72 29 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74  dr) + sizeof(int
735e0 29 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ) +.            
735f0 20 20 20 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72       mem.nBacktr
73600 61 63 65 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a  ace*sizeof(void*
73610 29 20 2b 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a  ) + mem.nTitle;.
73620 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
73630 61 75 6c 74 53 74 65 70 28 53 51 4c 49 54 45 5f  aultStep(SQLITE_
73640 46 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 4d 41  FAULTINJECTOR_MA
73650 4c 4c 4f 43 29 20 29 7b 0a 20 20 20 20 20 20 70  LLOC) ){.      p
73660 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 0;.    }else{
73670 0a 20 20 20 20 20 20 70 20 3d 20 6d 61 6c 6c 6f  .      p = mallo
73680 63 28 74 6f 74 61 6c 53 69 7a 65 29 3b 0a 20 20  c(totalSize);.  
73690 20 20 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a      if( p==0 ){.
736a0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d          sqlite3M
736b0 65 6d 73 79 73 41 6c 61 72 6d 28 6e 42 79 74 65  emsysAlarm(nByte
736c0 29 3b 0a 20 20 20 20 20 20 20 20 70 20 3d 20 6d  );.        p = m
736d0 61 6c 6c 6f 63 28 74 6f 74 61 6c 53 69 7a 65 29  alloc(totalSize)
736e0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
736f0 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20      if( p ){.   
73700 20 20 20 7a 20 3d 20 70 3b 0a 20 20 20 20 20 20     z = p;.      
73710 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 26 7a  pBt = (void**)&z
73720 5b 6d 65 6d 2e 6e 54 69 74 6c 65 5d 3b 0a 20 20  [mem.nTitle];.  
73730 20 20 20 20 70 48 64 72 20 3d 20 28 73 74 72 75      pHdr = (stru
73740 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29  ct MemBlockHdr*)
73750 26 70 42 74 5b 6d 65 6d 2e 6e 42 61 63 6b 74 72  &pBt[mem.nBacktr
73760 61 63 65 5d 3b 0a 20 20 20 20 20 20 70 48 64 72  ace];.      pHdr
73770 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20  ->pNext = 0;.   
73780 20 20 20 70 48 64 72 2d 3e 70 50 72 65 76 20 3d     pHdr->pPrev =
73790 20 6d 65 6d 2e 70 4c 61 73 74 3b 0a 20 20 20 20   mem.pLast;.    
737a0 20 20 69 66 28 20 6d 65 6d 2e 70 4c 61 73 74 20    if( mem.pLast 
737b0 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 2e 70  ){.        mem.p
737c0 4c 61 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 48  Last->pNext = pH
737d0 64 72 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  dr;.      }else{
737e0 0a 20 20 20 20 20 20 20 20 6d 65 6d 2e 70 46 69  .        mem.pFi
737f0 72 73 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20  rst = pHdr;.    
73800 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d 2e 70 4c    }.      mem.pL
73810 61 73 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20  ast = pHdr;.    
73820 20 20 70 48 64 72 2d 3e 69 46 6f 72 65 47 75 61    pHdr->iForeGua
73830 72 64 20 3d 20 46 4f 52 45 47 55 41 52 44 3b 0a  rd = FOREGUARD;.
73840 20 20 20 20 20 20 70 48 64 72 2d 3e 6e 42 61 63        pHdr->nBac
73850 6b 74 72 61 63 65 53 6c 6f 74 73 20 3d 20 6d 65  ktraceSlots = me
73860 6d 2e 6e 42 61 63 6b 74 72 61 63 65 3b 0a 20 20  m.nBacktrace;.  
73870 20 20 20 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65      pHdr->nTitle
73880 20 3d 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a 20   = mem.nTitle;. 
73890 20 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 42 61       if( mem.nBa
738a0 63 6b 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20  cktrace ){.     
738b0 20 20 20 76 6f 69 64 20 2a 61 41 64 64 72 5b 34     void *aAddr[4
738c0 30 5d 3b 0a 20 20 20 20 20 20 20 20 70 48 64 72  0];.        pHdr
738d0 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20 3d 20 62  ->nBacktrace = b
738e0 61 63 6b 74 72 61 63 65 28 61 41 64 64 72 2c 20  acktrace(aAddr, 
738f0 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 2b 31  mem.nBacktrace+1
73900 29 2d 31 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d  )-1;.        mem
73910 63 70 79 28 70 42 74 2c 20 26 61 41 64 64 72 5b  cpy(pBt, &aAddr[
73920 31 5d 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74  1], pHdr->nBackt
73930 72 61 63 65 2a 73 69 7a 65 6f 66 28 76 6f 69 64  race*sizeof(void
73940 2a 29 29 3b 0a 09 69 66 28 20 6d 65 6d 2e 78 42  *));..if( mem.xB
73950 61 63 6b 74 72 61 63 65 20 29 7b 0a 20 20 20 20  acktrace ){.    
73960 20 20 20 20 20 20 6d 65 6d 2e 78 42 61 63 6b 74        mem.xBackt
73970 72 61 63 65 28 6e 42 79 74 65 2c 20 70 48 64 72  race(nByte, pHdr
73980 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2d 31 2c 20  ->nBacktrace-1, 
73990 26 61 41 64 64 72 5b 31 5d 29 3b 0a 09 7d 0a 20  &aAddr[1]);..}. 
739a0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
739b0 20 20 20 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74      pHdr->nBackt
739c0 72 61 63 65 20 3d 20 30 3b 0a 20 20 20 20 20 20  race = 0;.      
739d0 7d 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d 2e  }.      if( mem.
739e0 6e 54 69 74 6c 65 20 29 7b 0a 20 20 20 20 20 20  nTitle ){.      
739f0 20 20 6d 65 6d 63 70 79 28 7a 2c 20 6d 65 6d 2e    memcpy(z, mem.
73a00 7a 54 69 74 6c 65 2c 20 6d 65 6d 2e 6e 54 69 74  zTitle, mem.nTit
73a10 6c 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  le);.      }.   
73a20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 20 3d     pHdr->iSize =
73a30 20 6e 42 79 74 65 3b 0a 20 20 20 20 20 20 70 49   nByte;.      pI
73a40 6e 74 20 3d 20 28 69 6e 74 2a 29 26 70 48 64 72  nt = (int*)&pHdr
73a50 5b 31 5d 3b 0a 20 20 20 20 20 20 70 49 6e 74 5b  [1];.      pInt[
73a60 6e 52 65 73 65 72 76 65 2f 73 69 7a 65 6f 66 28  nReserve/sizeof(
73a70 69 6e 74 29 5d 20 3d 20 52 45 41 52 47 55 41 52  int)] = REARGUAR
73a80 44 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28  D;.      memset(
73a90 70 49 6e 74 2c 20 30 78 36 35 2c 20 6e 52 65 73  pInt, 0x65, nRes
73aa0 65 72 76 65 29 3b 0a 20 20 20 20 20 20 6d 65 6d  erve);.      mem
73ab0 2e 6e 6f 77 55 73 65 64 20 2b 3d 20 6e 42 79 74  .nowUsed += nByt
73ac0 65 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d  e;.      if( mem
73ad0 2e 6e 6f 77 55 73 65 64 3e 6d 65 6d 2e 6d 78 55  .nowUsed>mem.mxU
73ae0 73 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 6d  sed ){.        m
73af0 65 6d 2e 6d 78 55 73 65 64 20 3d 20 6d 65 6d 2e  em.mxUsed = mem.
73b00 6e 6f 77 55 73 65 64 3b 0a 20 20 20 20 20 20 7d  nowUsed;.      }
73b10 0a 20 20 20 20 20 20 70 20 3d 20 28 76 6f 69 64  .      p = (void
73b20 2a 29 70 49 6e 74 3b 0a 20 20 20 20 7d 0a 20 20  *)pInt;.    }.  
73b30 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
73b40 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29  leave(mem.mutex)
73b50 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
73b60 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65  ; .}../*.** Free
73b70 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49   memory..*/.SQLI
73b80 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
73b90 74 65 33 5f 66 72 65 65 28 76 6f 69 64 20 2a 70  te3_free(void *p
73ba0 50 72 69 6f 72 29 7b 0a 20 20 73 74 72 75 63 74  Prior){.  struct
73bb0 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48   MemBlockHdr *pH
73bc0 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74  dr;.  void **pBt
73bd0 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69  ;.  char *z;.  i
73be0 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a  f( pPrior==0 ){.
73bf0 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
73c00 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 6d 75    assert( mem.mu
73c10 74 65 78 21 3d 30 20 29 3b 0a 20 20 70 48 64 72  tex!=0 );.  pHdr
73c20 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73   = sqlite3Memsys
73c30 47 65 74 48 65 61 64 65 72 28 70 50 72 69 6f 72  GetHeader(pPrior
73c40 29 3b 0a 20 20 70 42 74 20 3d 20 28 76 6f 69 64  );.  pBt = (void
73c50 2a 2a 29 70 48 64 72 3b 0a 20 20 70 42 74 20 2d  **)pHdr;.  pBt -
73c60 3d 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61  = pHdr->nBacktra
73c70 63 65 53 6c 6f 74 73 3b 0a 20 20 73 71 6c 69 74  ceSlots;.  sqlit
73c80 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
73c90 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 6d 65 6d  em.mutex);.  mem
73ca0 2e 6e 6f 77 55 73 65 64 20 2d 3d 20 70 48 64 72  .nowUsed -= pHdr
73cb0 2d 3e 69 53 69 7a 65 3b 0a 20 20 69 66 28 20 70  ->iSize;.  if( p
73cc0 48 64 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20  Hdr->pPrev ){.  
73cd0 20 20 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e    assert( pHdr->
73ce0 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 48  pPrev->pNext==pH
73cf0 64 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e  dr );.    pHdr->
73d00 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70  pPrev->pNext = p
73d10 48 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65  Hdr->pNext;.  }e
73d20 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
73d30 20 6d 65 6d 2e 70 46 69 72 73 74 3d 3d 70 48 64   mem.pFirst==pHd
73d40 72 20 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 46 69  r );.    mem.pFi
73d50 72 73 74 20 3d 20 70 48 64 72 2d 3e 70 4e 65 78  rst = pHdr->pNex
73d60 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 64  t;.  }.  if( pHd
73d70 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20  r->pNext ){.    
73d80 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e 70 4e  assert( pHdr->pN
73d90 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 48 64 72  ext->pPrev==pHdr
73da0 20 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 4e   );.    pHdr->pN
73db0 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 48 64  ext->pPrev = pHd
73dc0 72 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 65 6c 73  r->pPrev;.  }els
73dd0 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d  e{.    assert( m
73de0 65 6d 2e 70 4c 61 73 74 3d 3d 70 48 64 72 20 29  em.pLast==pHdr )
73df0 3b 0a 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74 20  ;.    mem.pLast 
73e00 3d 20 70 48 64 72 2d 3e 70 50 72 65 76 3b 0a 20  = pHdr->pPrev;. 
73e10 20 7d 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a 29   }.  z = (char*)
73e20 70 42 74 3b 0a 20 20 7a 20 2d 3d 20 70 48 64 72  pBt;.  z -= pHdr
73e30 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 6d 65 6d 73  ->nTitle;.  mems
73e40 65 74 28 7a 2c 20 30 78 32 62 2c 20 73 69 7a 65  et(z, 0x2b, size
73e50 6f 66 28 76 6f 69 64 2a 29 2a 70 48 64 72 2d 3e  of(void*)*pHdr->
73e60 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 20  nBacktraceSlots 
73e70 2b 20 73 69 7a 65 6f 66 28 2a 70 48 64 72 29 20  + sizeof(*pHdr) 
73e80 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  +.              
73e90 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 20      pHdr->iSize 
73ea0 2b 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b 20  + sizeof(int) + 
73eb0 70 48 64 72 2d 3e 6e 54 69 74 6c 65 29 3b 0a 20  pHdr->nTitle);. 
73ec0 20 66 72 65 65 28 7a 29 3b 0a 20 20 73 71 6c 69   free(z);.  sqli
73ed0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
73ee0 6d 65 6d 2e 6d 75 74 65 78 29 3b 20 20 0a 7d 0a  mem.mutex);  .}.
73ef0 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
73f00 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69  e size of an exi
73f10 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c  sting memory all
73f20 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46  ocation..**.** F
73f30 6f 72 20 74 68 69 73 20 64 65 62 75 67 67 69 6e  or this debuggin
73f40 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  g implementation
73f50 2c 20 77 65 20 2a 61 6c 77 61 79 73 2a 20 6d 61  , we *always* ma
73f60 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  ke a copy of the
73f70 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  .** allocation i
73f80 6e 74 6f 20 61 20 6e 65 77 20 70 6c 61 63 65 20  nto a new place 
73f90 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 49 6e 20 74  in memory.  In t
73fa0 68 69 73 20 77 61 79 2c 20 69 66 20 74 68 65 20  his way, if the 
73fb0 0a 2a 2a 20 68 69 67 68 65 72 20 6c 65 76 65 6c  .** higher level
73fc0 20 63 6f 64 65 20 69 73 20 75 73 69 6e 67 20 70   code is using p
73fd0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6f 6c  ointer to the ol
73fe0 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 74  d allocation, it
73ff0 20 69 73 20 0a 2a 2a 20 6d 75 63 68 20 6d 6f 72   is .** much mor
74000 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62 72 65 61  e likely to brea
74010 6b 20 61 6e 64 20 77 65 20 61 72 65 20 6d 75 63  k and we are muc
74020 68 20 6d 6f 72 65 20 6c 69 6b 69 6e 67 20 74 6f  h more liking to
74030 20 66 69 6e 64 0a 2a 2a 20 74 68 65 20 65 72 72   find.** the err
74040 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  or..*/.SQLITE_AP
74050 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  I void *sqlite3_
74060 72 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50  realloc(void *pP
74070 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29  rior, int nByte)
74080 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c  {.  struct MemBl
74090 6f 63 6b 48 64 72 20 2a 70 4f 6c 64 48 64 72 3b  ockHdr *pOldHdr;
740a0 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20  .  void *pNew;. 
740b0 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29   if( pPrior==0 )
740c0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c  {.    return sql
740d0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74  ite3_malloc(nByt
740e0 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42  e);.  }.  if( nB
740f0 79 74 65 3c 3d 30 20 29 7b 0a 20 20 20 20 73 71  yte<=0 ){.    sq
74100 6c 69 74 65 33 5f 66 72 65 65 28 70 50 72 69 6f  lite3_free(pPrio
74110 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  r);.    return 0
74120 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
74130 6d 65 6d 2e 64 69 73 61 6c 6c 6f 77 3d 3d 30 20  mem.disallow==0 
74140 29 3b 0a 20 20 70 4f 6c 64 48 64 72 20 3d 20 73  );.  pOldHdr = s
74150 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48  qlite3MemsysGetH
74160 65 61 64 65 72 28 70 50 72 69 6f 72 29 3b 0a 20  eader(pPrior);. 
74170 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f   pNew = sqlite3_
74180 6d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20  malloc(nByte);. 
74190 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20   if( pNew ){.   
741a0 20 6d 65 6d 63 70 79 28 70 4e 65 77 2c 20 70 50   memcpy(pNew, pP
741b0 72 69 6f 72 2c 20 6e 42 79 74 65 3c 70 4f 6c 64  rior, nByte<pOld
741c0 48 64 72 2d 3e 69 53 69 7a 65 20 3f 20 6e 42 79  Hdr->iSize ? nBy
741d0 74 65 20 3a 20 70 4f 6c 64 48 64 72 2d 3e 69 53  te : pOldHdr->iS
741e0 69 7a 65 29 3b 0a 20 20 20 20 69 66 28 20 6e 42  ize);.    if( nB
741f0 79 74 65 3e 70 4f 6c 64 48 64 72 2d 3e 69 53 69  yte>pOldHdr->iSi
74200 7a 65 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73  ze ){.      mems
74210 65 74 28 26 28 28 63 68 61 72 2a 29 70 4e 65 77  et(&((char*)pNew
74220 29 5b 70 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65  )[pOldHdr->iSize
74230 5d 2c 20 30 78 32 62 2c 20 6e 42 79 74 65 20 2d  ], 0x2b, nByte -
74240 20 70 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 29   pOldHdr->iSize)
74250 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
74260 74 65 33 5f 66 72 65 65 28 70 50 72 69 6f 72 29  te3_free(pPrior)
74270 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
74280 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65  New;.}../*.** Se
74290 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
742a0 62 61 63 6b 74 72 61 63 65 20 6c 65 76 65 6c 73  backtrace levels
742b0 20 6b 65 70 74 20 66 6f 72 20 65 61 63 68 20 61   kept for each a
742c0 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 20 41 20  llocation..** A 
742d0 76 61 6c 75 65 20 6f 66 20 7a 65 72 6f 20 74 75  value of zero tu
742e0 72 6e 73 20 6f 66 20 62 61 63 6b 74 72 61 63 69  rns of backtraci
742f0 6e 67 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 20  ng.  The number 
74300 69 73 20 61 6c 77 61 79 73 20 72 6f 75 6e 64 65  is always rounde
74310 64 0a 2a 2a 20 75 70 20 74 6f 20 61 20 6d 75 6c  d.** up to a mul
74320 74 69 70 6c 65 20 6f 66 20 32 2e 0a 2a 2f 0a 53  tiple of 2..*/.S
74330 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
74340 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62  id sqlite3Memdeb
74350 75 67 42 61 63 6b 74 72 61 63 65 28 69 6e 74 20  ugBacktrace(int 
74360 64 65 70 74 68 29 7b 0a 20 20 69 66 28 20 64 65  depth){.  if( de
74370 70 74 68 3c 30 20 29 7b 20 64 65 70 74 68 20 3d  pth<0 ){ depth =
74380 20 30 3b 20 7d 0a 20 20 69 66 28 20 64 65 70 74   0; }.  if( dept
74390 68 3e 32 30 20 29 7b 20 64 65 70 74 68 20 3d 20  h>20 ){ depth = 
743a0 32 30 3b 20 7d 0a 20 20 64 65 70 74 68 20 3d 20  20; }.  depth = 
743b0 28 64 65 70 74 68 2b 31 29 26 30 78 66 65 3b 0a  (depth+1)&0xfe;.
743c0 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65    mem.nBacktrace
743d0 20 3d 20 64 65 70 74 68 3b 0a 7d 0a 0a 53 51 4c   = depth;.}..SQL
743e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
743f0 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67   sqlite3Memdebug
74400 42 61 63 6b 74 72 61 63 65 43 61 6c 6c 62 61 63  BacktraceCallbac
74410 6b 28 76 6f 69 64 20 28 2a 78 42 61 63 6b 74 72  k(void (*xBacktr
74420 61 63 65 29 28 69 6e 74 2c 20 69 6e 74 2c 20 76  ace)(int, int, v
74430 6f 69 64 20 2a 2a 29 29 7b 0a 20 20 6d 65 6d 2e  oid **)){.  mem.
74440 78 42 61 63 6b 74 72 61 63 65 20 3d 20 78 42 61  xBacktrace = xBa
74450 63 6b 74 72 61 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  cktrace;.}../*.*
74460 2a 20 53 65 74 20 74 68 65 20 74 69 74 6c 65 20  * Set the title 
74470 73 74 72 69 6e 67 20 66 6f 72 20 73 75 62 73 65  string for subse
74480 71 75 65 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e  quent allocation
74490 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
744a0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
744b0 33 4d 65 6d 64 65 62 75 67 53 65 74 74 69 74 6c  3MemdebugSettitl
744c0 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54  e(const char *zT
744d0 69 74 6c 65 29 7b 0a 20 20 69 6e 74 20 6e 20 3d  itle){.  int n =
744e0 20 73 74 72 6c 65 6e 28 7a 54 69 74 6c 65 29 20   strlen(zTitle) 
744f0 2b 20 31 3b 0a 20 20 65 6e 74 65 72 4d 65 6d 28  + 1;.  enterMem(
74500 29 3b 0a 20 20 69 66 28 20 6e 3e 3d 73 69 7a 65  );.  if( n>=size
74510 6f 66 28 6d 65 6d 2e 7a 54 69 74 6c 65 29 20 29  of(mem.zTitle) )
74520 20 6e 20 3d 20 73 69 7a 65 6f 66 28 6d 65 6d 2e   n = sizeof(mem.
74530 7a 54 69 74 6c 65 29 2d 31 3b 0a 20 20 6d 65 6d  zTitle)-1;.  mem
74540 63 70 79 28 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20  cpy(mem.zTitle, 
74550 7a 54 69 74 6c 65 2c 20 6e 29 3b 0a 20 20 6d 65  zTitle, n);.  me
74560 6d 2e 7a 54 69 74 6c 65 5b 6e 5d 20 3d 20 30 3b  m.zTitle[n] = 0;
74570 0a 20 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 3d 20  .  mem.nTitle = 
74580 28 6e 2b 37 29 26 7e 37 3b 0a 20 20 73 71 6c 69  (n+7)&~7;.  sqli
74590 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
745a0 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 53  mem.mutex);.}..S
745b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
745c0 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62  id sqlite3Memdeb
745d0 75 67 53 79 6e 63 28 29 7b 0a 20 20 73 74 72 75  ugSync(){.  stru
745e0 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a  ct MemBlockHdr *
745f0 70 48 64 72 3b 0a 20 20 66 6f 72 28 70 48 64 72  pHdr;.  for(pHdr
74600 3d 6d 65 6d 2e 70 46 69 72 73 74 3b 20 70 48 64  =mem.pFirst; pHd
74610 72 3b 20 70 48 64 72 3d 70 48 64 72 2d 3e 70 4e  r; pHdr=pHdr->pN
74620 65 78 74 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a  ext){.    void *
74630 2a 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70  *pBt = (void**)p
74640 48 64 72 3b 0a 20 20 20 20 70 42 74 20 2d 3d 20  Hdr;.    pBt -= 
74650 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65  pHdr->nBacktrace
74660 53 6c 6f 74 73 3b 0a 20 20 20 20 6d 65 6d 2e 78  Slots;.    mem.x
74670 42 61 63 6b 74 72 61 63 65 28 70 48 64 72 2d 3e  Backtrace(pHdr->
74680 69 53 69 7a 65 2c 20 70 48 64 72 2d 3e 6e 42 61  iSize, pHdr->nBa
74690 63 6b 74 72 61 63 65 2d 31 2c 20 26 70 42 74 5b  cktrace-1, &pBt[
746a0 31 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  1]);.  }.}../*.*
746b0 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20  * Open the file 
746c0 69 6e 64 69 63 61 74 65 64 20 61 6e 64 20 77 72  indicated and wr
746d0 69 74 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c  ite a log of all
746e0 20 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20   unfreed memory 
746f0 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20  .** allocations 
74700 69 6e 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a  into that log..*
74710 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
74720 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d   void sqlite3Mem
74730 64 65 62 75 67 44 75 6d 70 28 63 6f 6e 73 74 20  debugDump(const 
74740 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29  char *zFilename)
74750 7b 0a 20 20 46 49 4c 45 20 2a 6f 75 74 3b 0a 20  {.  FILE *out;. 
74760 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b   struct MemBlock
74770 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 76 6f 69  Hdr *pHdr;.  voi
74780 64 20 2a 2a 70 42 74 3b 0a 20 20 69 6e 74 20 69  d **pBt;.  int i
74790 3b 0a 20 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28  ;.  out = fopen(
747a0 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 77 22 29 3b  zFilename, "w");
747b0 0a 20 20 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b  .  if( out==0 ){
747c0 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64  .    fprintf(std
747d0 65 72 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 20  err, "** Unable 
747e0 74 6f 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72 79  to output memory
747f0 20 64 65 62 75 67 20 6f 75 74 70 75 74 20 6c 6f   debug output lo
74800 67 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20  g: %s **\n",.   
74810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
74820 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20   zFilename);.   
74830 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66   return;.  }.  f
74840 6f 72 28 70 48 64 72 3d 6d 65 6d 2e 70 46 69 72  or(pHdr=mem.pFir
74850 73 74 3b 20 70 48 64 72 3b 20 70 48 64 72 3d 70  st; pHdr; pHdr=p
74860 48 64 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Hdr->pNext){.   
74870 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 68 61 72   char *z = (char
74880 2a 29 70 48 64 72 3b 0a 20 20 20 20 7a 20 2d 3d  *)pHdr;.    z -=
74890 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63   pHdr->nBacktrac
748a0 65 53 6c 6f 74 73 2a 73 69 7a 65 6f 66 28 76 6f  eSlots*sizeof(vo
748b0 69 64 2a 29 20 2b 20 70 48 64 72 2d 3e 6e 54 69  id*) + pHdr->nTi
748c0 74 6c 65 3b 0a 20 20 20 20 66 70 72 69 6e 74 66  tle;.    fprintf
748d0 28 6f 75 74 2c 20 22 2a 2a 2a 2a 20 25 6c 6c 64  (out, "**** %lld
748e0 20 62 79 74 65 73 20 61 74 20 25 70 20 66 72 6f   bytes at %p fro
748f0 6d 20 25 73 20 2a 2a 2a 2a 5c 6e 22 2c 20 0a 20  m %s ****\n", . 
74900 20 20 20 20 20 20 20 20 20 20 20 70 48 64 72 2d             pHdr-
74910 3e 69 53 69 7a 65 2c 20 26 70 48 64 72 5b 31 5d  >iSize, &pHdr[1]
74920 2c 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 20 3f  , pHdr->nTitle ?
74930 20 7a 20 3a 20 22 3f 3f 3f 22 29 3b 0a 20 20 20   z : "???");.   
74940 20 69 66 28 20 70 48 64 72 2d 3e 6e 42 61 63 6b   if( pHdr->nBack
74950 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 66  trace ){.      f
74960 66 6c 75 73 68 28 6f 75 74 29 3b 0a 20 20 20 20  flush(out);.    
74970 20 20 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29    pBt = (void**)
74980 70 48 64 72 3b 0a 20 20 20 20 20 20 70 42 74 20  pHdr;.      pBt 
74990 2d 3d 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72  -= pHdr->nBacktr
749a0 61 63 65 53 6c 6f 74 73 3b 0a 20 20 20 20 20 20  aceSlots;.      
749b0 62 61 63 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c  backtrace_symbol
749c0 73 5f 66 64 28 70 42 74 2c 20 70 48 64 72 2d 3e  s_fd(pBt, pHdr->
749d0 6e 42 61 63 6b 74 72 61 63 65 2c 20 66 69 6c 65  nBacktrace, file
749e0 6e 6f 28 6f 75 74 29 29 3b 0a 20 20 20 20 20 20  no(out));.      
749f0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e  fprintf(out, "\n
74a00 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ");.    }.  }.  
74a10 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 43 4f  fprintf(out, "CO
74a20 55 4e 54 53 3a 5c 6e 22 29 3b 0a 20 20 66 6f 72  UNTS:\n");.  for
74a30 28 69 3d 30 3b 20 69 3c 4e 43 53 49 5a 45 2d 31  (i=0; i<NCSIZE-1
74a40 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
74a50 6d 65 6d 2e 73 69 7a 65 43 6e 74 5b 69 5d 20 29  mem.sizeCnt[i] )
74a60 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28  {.      fprintf(
74a70 6f 75 74 2c 20 22 20 20 20 25 33 64 3a 20 25 64  out, "   %3d: %d
74a80 5c 6e 22 2c 20 69 2a 38 2b 38 2c 20 6d 65 6d 2e  \n", i*8+8, mem.
74a90 73 69 7a 65 43 6e 74 5b 69 5d 29 3b 0a 20 20 20  sizeCnt[i]);.   
74aa0 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6d 65 6d   }.  }.  if( mem
74ab0 2e 73 69 7a 65 43 6e 74 5b 4e 43 53 49 5a 45 2d  .sizeCnt[NCSIZE-
74ac0 31 5d 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74  1] ){.    fprint
74ad0 66 28 6f 75 74 2c 20 22 20 20 3e 25 33 64 3a 20  f(out, "  >%3d: 
74ae0 25 64 5c 6e 22 2c 20 4e 43 53 49 5a 45 2a 38 2c  %d\n", NCSIZE*8,
74af0 20 6d 65 6d 2e 73 69 7a 65 43 6e 74 5b 4e 43 53   mem.sizeCnt[NCS
74b00 49 5a 45 2d 31 5d 29 3b 0a 20 20 7d 0a 20 20 66  IZE-1]);.  }.  f
74b10 63 6c 6f 73 65 28 6f 75 74 29 3b 0a 7d 0a 0a 2f  close(out);.}../
74b20 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
74b30 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20  number of times 
74b40 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
74b50 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64   has been called
74b60 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
74b70 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  ATE int sqlite3M
74b80 65 6d 64 65 62 75 67 4d 61 6c 6c 6f 63 43 6f 75  emdebugMallocCou
74b90 6e 74 28 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  nt(){.  int i;. 
74ba0 20 69 6e 74 20 6e 54 6f 74 61 6c 20 3d 20 30 3b   int nTotal = 0;
74bb0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43  .  for(i=0; i<NC
74bc0 53 49 5a 45 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  SIZE; i++){.    
74bd0 6e 54 6f 74 61 6c 20 2b 3d 20 6d 65 6d 2e 73 69  nTotal += mem.si
74be0 7a 65 43 6e 74 5b 69 5d 3b 0a 20 20 7d 0a 20 20  zeCnt[i];.  }.  
74bf0 72 65 74 75 72 6e 20 6e 54 6f 74 61 6c 3b 0a 7d  return nTotal;.}
74c00 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  ...#endif /* SQL
74c10 49 54 45 5f 4d 45 4d 44 45 42 55 47 20 2a 2f 0a  ITE_MEMDEBUG */.
74c20 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
74c30 20 45 6e 64 20 6f 66 20 6d 65 6d 32 2e 63 20 2a   End of mem2.c *
74c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
74c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
74c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
74c70 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
74c80 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 33   Begin file mem3
74c90 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
74ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
74cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
74cc0 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4f 63 74 6f  ./*.** 2007 Octo
74cd0 62 65 72 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 14.**.** The
74ce0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
74cf0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
74d00 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
74d10 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
74d20 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
74d30 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
74d40 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
74d50 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
74d60 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
74d70 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
74d80 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
74d90 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
74da0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
74db0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
74dc0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
74dd0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
74de0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
74df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
74e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
74e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
74e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
74e30 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
74e40 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  le contains the 
74e50 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  C functions that
74e60 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 6d 65 6d   implement a mem
74e70 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f  ory.** allocatio
74e80 6e 20 73 75 62 73 79 73 74 65 6d 20 66 6f 72 20  n subsystem for 
74e90 75 73 65 20 62 79 20 53 51 4c 69 74 65 2e 20 0a  use by SQLite. .
74ea0 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69  **.** This versi
74eb0 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79  on of the memory
74ec0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73   allocation subs
74ed0 79 73 74 65 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a  ystem omits all.
74ee0 2a 2a 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63  ** use of malloc
74ef0 28 29 2e 20 20 41 6c 6c 20 64 79 6e 61 6d 69 63  ().  All dynamic
74f00 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 61 62 6c 65  ally allocatable
74f10 20 6d 65 6d 6f 72 79 20 69 73 0a 2a 2a 20 63 6f   memory is.** co
74f20 6e 74 61 69 6e 65 64 20 69 6e 20 61 20 73 74 61  ntained in a sta
74f30 74 69 63 20 61 72 72 61 79 2c 20 6d 65 6d 2e 61  tic array, mem.a
74f40 50 6f 6f 6c 5b 5d 2e 20 20 54 68 65 20 73 69 7a  Pool[].  The siz
74f50 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 66 69 78  e of this.** fix
74f60 65 64 20 6d 65 6d 6f 72 79 20 70 6f 6f 6c 20 69  ed memory pool i
74f70 73 20 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f  s SQLITE_MEMORY_
74f80 53 49 5a 45 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a  SIZE bytes..**.*
74f90 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f  * This version o
74fa0 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  f the memory all
74fb0 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65  ocation subsyste
74fc0 6d 20 69 73 20 75 73 65 64 20 69 66 0a 2a 2a 20  m is used if.** 
74fd0 61 6e 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49  and only if SQLI
74fe0 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20 69  TE_MEMORY_SIZE i
74ff0 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a  s defined..**.**
75000 20 24 49 64 3a 20 6d 65 6d 33 2e 63 2c 76 20 31   $Id: mem3.c,v 1
75010 2e 31 32 20 32 30 30 38 2f 30 32 2f 31 39 20 31  .12 2008/02/19 1
75020 35 3a 31 35 3a 31 36 20 64 72 68 20 45 78 70 20  5:15:16 drh Exp 
75030 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  $.*/../*.** This
75040 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20   version of the 
75050 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
75060 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 77 68   is used only wh
75070 65 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 45  en .** SQLITE_ME
75080 4d 4f 52 59 5f 53 49 5a 45 20 69 73 20 64 65 66  MORY_SIZE is def
75090 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  ined..*/.#ifdef 
750a0 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49  SQLITE_MEMORY_SI
750b0 5a 45 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75  ZE../*.** Maximu
750c0 6d 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d 33 42  m size (in Mem3B
750d0 6c 6f 63 6b 73 29 20 6f 66 20 61 20 22 73 6d 61  locks) of a "sma
750e0 6c 6c 22 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 23 64  ll" chunk..*/.#d
750f0 65 66 69 6e 65 20 4d 58 5f 53 4d 41 4c 4c 20 31  efine MX_SMALL 1
75100 30 0a 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72  0.../*.** Number
75110 20 6f 66 20 66 72 65 65 6c 69 73 74 20 68 61 73   of freelist has
75120 68 20 73 6c 6f 74 73 0a 2a 2f 0a 23 64 65 66 69  h slots.*/.#defi
75130 6e 65 20 4e 5f 48 41 53 48 20 20 36 31 0a 0a 2f  ne N_HASH  61../
75140 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 72 79 20 61 6c  *.** A memory al
75150 6c 6f 63 61 74 69 6f 6e 20 28 61 6c 73 6f 20 63  location (also c
75160 61 6c 6c 65 64 20 61 20 22 63 68 75 6e 6b 22 29  alled a "chunk")
75170 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 77 6f   consists of two
75180 20 6f 72 20 0a 2a 2a 20 6d 6f 72 65 20 62 6c 6f   or .** more blo
75190 63 6b 73 20 77 68 65 72 65 20 65 61 63 68 20 62  cks where each b
751a0 6c 6f 63 6b 20 69 73 20 38 20 62 79 74 65 73 2e  lock is 8 bytes.
751b0 20 20 54 68 65 20 66 69 72 73 74 20 38 20 62 79    The first 8 by
751c0 74 65 73 20 61 72 65 20 0a 2a 2a 20 61 20 68 65  tes are .** a he
751d0 61 64 65 72 20 74 68 61 74 20 69 73 20 6e 6f 74  ader that is not
751e0 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65   returned to the
751f0 20 75 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 63   user..**.** A c
75200 68 75 6e 6b 20 69 73 20 74 77 6f 20 6f 72 20 6d  hunk is two or m
75210 6f 72 65 20 62 6c 6f 63 6b 73 20 74 68 61 74 20  ore blocks that 
75220 69 73 20 65 69 74 68 65 72 20 63 68 65 63 6b 65  is either checke
75230 64 20 6f 75 74 20 6f 72 0a 2a 2a 20 66 72 65 65  d out or.** free
75240 2e 20 20 54 68 65 20 66 69 72 73 74 20 62 6c 6f  .  The first blo
75250 63 6b 20 68 61 73 20 66 6f 72 6d 61 74 20 75 2e  ck has format u.
75260 68 64 72 2e 20 20 75 2e 68 64 72 2e 73 69 7a 65  hdr.  u.hdr.size
75270 34 78 20 69 73 20 34 20 74 69 6d 65 73 20 74 68  4x is 4 times th
75280 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68 65  e.** size of the
75290 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 62   allocation in b
752a0 6c 6f 63 6b 73 20 69 66 20 74 68 65 20 61 6c 6c  locks if the all
752b0 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 2e  ocation is free.
752c0 0a 2a 2a 20 54 68 65 20 75 2e 68 64 72 2e 73 69  .** The u.hdr.si
752d0 7a 65 34 78 26 31 20 62 69 74 20 69 73 20 74 72  ze4x&1 bit is tr
752e0 75 65 20 69 66 20 74 68 65 20 63 68 75 6e 6b 20  ue if the chunk 
752f0 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 20 61  is checked out a
75300 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 74  nd.** false if t
75310 68 65 20 63 68 75 6e 6b 20 69 73 20 6f 6e 20 74  he chunk is on t
75320 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 54 68  he freelist.  Th
75330 65 20 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 32  e u.hdr.size4x&2
75340 20 62 69 74 0a 2a 2a 20 69 73 20 74 72 75 65 20   bit.** is true 
75350 69 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  if the previous 
75360 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 64  chunk is checked
75370 20 6f 75 74 20 61 6e 64 20 66 61 6c 73 65 20 69   out and false i
75380 66 20 74 68 65 0a 2a 2a 20 70 72 65 76 69 6f 75  f the.** previou
75390 73 20 63 68 75 6e 6b 20 69 73 20 66 72 65 65 2e  s chunk is free.
753a0 20 20 54 68 65 20 75 2e 68 64 72 2e 70 72 65 76    The u.hdr.prev
753b0 53 69 7a 65 20 66 69 65 6c 64 20 69 73 20 74 68  Size field is th
753c0 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 74 68 65  e size of.** the
753d0 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20   previous chunk 
753e0 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20 74 68 65  in blocks if the
753f0 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20   previous chunk 
75400 69 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 66 72 65  is on the.** fre
75410 65 6c 69 73 74 2e 20 49 66 20 74 68 65 20 70 72  elist. If the pr
75420 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 20  evious chunk is 
75430 63 68 65 63 6b 65 64 20 6f 75 74 2c 20 74 68 65  checked out, the
75440 6e 0a 2a 2a 20 75 2e 68 64 72 2e 70 72 65 76 53  n.** u.hdr.prevS
75450 69 7a 65 20 63 61 6e 20 62 65 20 70 61 72 74 20  ize can be part 
75460 6f 66 20 74 68 65 20 64 61 74 61 20 66 6f 72 20  of the data for 
75470 74 68 61 74 20 63 68 75 6e 6b 20 61 6e 64 20 73  that chunk and s
75480 68 6f 75 6c 64 0a 2a 2a 20 6e 6f 74 20 62 65 20  hould.** not be 
75490 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e  read or written.
754a0 0a 2a 2a 0a 2a 2a 20 57 65 20 6f 66 74 65 6e 20  .**.** We often 
754b0 69 64 65 6e 74 69 66 79 20 61 20 63 68 75 6e 6b  identify a chunk
754c0 20 62 79 20 69 74 73 20 69 6e 64 65 78 20 69 6e   by its index in
754d0 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 5d 2e 20 20 57   mem.aPool[].  W
754e0 68 65 6e 0a 2a 2a 20 74 68 69 73 20 69 73 20 64  hen.** this is d
754f0 6f 6e 65 2c 20 74 68 65 20 63 68 75 6e 6b 20 69  one, the chunk i
75500 6e 64 65 78 20 72 65 66 65 72 73 20 74 6f 20 74  ndex refers to t
75510 68 65 20 73 65 63 6f 6e 64 20 62 6c 6f 63 6b 20  he second block 
75520 6f 66 0a 2a 2a 20 74 68 65 20 63 68 75 6e 6b 2e  of.** the chunk.
75530 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 74    In this way, t
75540 68 65 20 66 69 72 73 74 20 63 68 75 6e 6b 20 68  he first chunk h
75550 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31  as an index of 1
75560 2e 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 69 6e 64  ..** A chunk ind
75570 65 78 20 6f 66 20 30 20 6d 65 61 6e 73 20 22 6e  ex of 0 means "n
75580 6f 20 73 75 63 68 20 63 68 75 6e 6b 22 20 61 6e  o such chunk" an
75590 64 20 69 73 20 74 68 65 20 65 71 75 69 76 61 6c  d is the equival
755a0 65 6e 74 0a 2a 2a 20 6f 66 20 61 20 4e 55 4c 4c  ent.** of a NULL
755b0 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   pointer..**.** 
755c0 54 68 65 20 73 65 63 6f 6e 64 20 62 6c 6f 63 6b  The second block
755d0 20 6f 66 20 66 72 65 65 20 63 68 75 6e 6b 73 20   of free chunks 
755e0 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 75  is of the form u
755f0 2e 6c 69 73 74 2e 20 20 54 68 65 0a 2a 2a 20 74  .list.  The.** t
75600 77 6f 20 66 69 65 6c 64 73 20 66 6f 72 6d 20 61  wo fields form a
75610 20 64 6f 75 62 6c 65 2d 6c 69 6e 6b 65 64 20 6c   double-linked l
75620 69 73 74 20 6f 66 20 63 68 75 6e 6b 73 20 6f 66  ist of chunks of
75630 20 72 65 6c 61 74 65 64 20 73 69 7a 65 73 2e 0a   related sizes..
75640 2a 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74  ** Pointers to t
75650 68 65 20 68 65 61 64 20 6f 66 20 74 68 65 20 6c  he head of the l
75660 69 73 74 20 61 72 65 20 73 74 6f 72 65 64 20 69  ist are stored i
75670 6e 20 6d 65 6d 2e 61 69 53 6d 61 6c 6c 5b 5d 20  n mem.aiSmall[] 
75680 0a 2a 2a 20 66 6f 72 20 73 6d 61 6c 6c 65 72 20  .** for smaller 
75690 63 68 75 6e 6b 73 20 61 6e 64 20 6d 65 6d 2e 61  chunks and mem.a
756a0 69 48 61 73 68 5b 5d 20 66 6f 72 20 6c 61 72 67  iHash[] for larg
756b0 65 72 20 63 68 75 6e 6b 73 2e 0a 2a 2a 0a 2a 2a  er chunks..**.**
756c0 20 54 68 65 20 73 65 63 6f 6e 64 20 62 6c 6f 63   The second bloc
756d0 6b 20 6f 66 20 61 20 63 68 75 6e 6b 20 69 73 20  k of a chunk is 
756e0 75 73 65 72 20 64 61 74 61 20 69 66 20 74 68 65  user data if the
756f0 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65   chunk is checke
75700 64 20 0a 2a 2a 20 6f 75 74 2e 20 20 49 66 20 61  d .** out.  If a
75710 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65   chunk is checke
75720 64 20 6f 75 74 2c 20 74 68 65 20 75 73 65 72 20  d out, the user 
75730 64 61 74 61 20 6d 61 79 20 65 78 74 65 6e 64 20  data may extend 
75740 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 75 2e 68 64  into.** the u.hd
75750 72 2e 70 72 65 76 53 69 7a 65 20 76 61 6c 75 65  r.prevSize value
75760 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
75770 67 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 74 79 70 65  g chunk..*/.type
75780 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d 33 42  def struct Mem3B
75790 6c 6f 63 6b 20 4d 65 6d 33 42 6c 6f 63 6b 3b 0a  lock Mem3Block;.
757a0 73 74 72 75 63 74 20 4d 65 6d 33 42 6c 6f 63 6b  struct Mem3Block
757b0 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20   {.  union {.   
757c0 20 73 74 72 75 63 74 20 7b 0a 20 20 20 20 20 20   struct {.      
757d0 75 33 32 20 70 72 65 76 53 69 7a 65 3b 20 20 20  u32 prevSize;   
757e0 2f 2a 20 53 69 7a 65 20 6f 66 20 70 72 65 76 69  /* Size of previ
757f0 6f 75 73 20 63 68 75 6e 6b 20 69 6e 20 4d 65 6d  ous chunk in Mem
75800 33 42 6c 6f 63 6b 20 65 6c 65 6d 65 6e 74 73 20  3Block elements 
75810 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 73 69 7a  */.      u32 siz
75820 65 34 78 3b 20 20 20 20 20 2f 2a 20 34 78 20 74  e4x;     /* 4x t
75830 68 65 20 73 69 7a 65 20 6f 66 20 63 75 72 72 65  he size of curre
75840 6e 74 20 63 68 75 6e 6b 20 69 6e 20 4d 65 6d 33  nt chunk in Mem3
75850 42 6c 6f 63 6b 20 65 6c 65 6d 65 6e 74 73 20 2a  Block elements *
75860 2f 0a 20 20 20 20 7d 20 68 64 72 3b 0a 20 20 20  /.    } hdr;.   
75870 20 73 74 72 75 63 74 20 7b 0a 20 20 20 20 20 20   struct {.      
75880 75 33 32 20 6e 65 78 74 3b 20 20 20 20 20 20 20  u32 next;       
75890 2f 2a 20 49 6e 64 65 78 20 69 6e 20 6d 65 6d 2e  /* Index in mem.
758a0 61 50 6f 6f 6c 5b 5d 20 6f 66 20 6e 65 78 74 20  aPool[] of next 
758b0 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 20  free chunk */.  
758c0 20 20 20 20 75 33 32 20 70 72 65 76 3b 20 20 20      u32 prev;   
758d0 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20      /* Index in 
758e0 6d 65 6d 2e 61 50 6f 6f 6c 5b 5d 20 6f 66 20 70  mem.aPool[] of p
758f0 72 65 76 69 6f 75 73 20 66 72 65 65 20 63 68 75  revious free chu
75900 6e 6b 20 2a 2f 0a 20 20 20 20 7d 20 6c 69 73 74  nk */.    } list
75910 3b 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a  ;.  } u;.};../*.
75920 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 73 74  ** All of the st
75930 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 75  atic variables u
75940 73 65 64 20 62 79 20 74 68 69 73 20 6d 6f 64 75  sed by this modu
75950 6c 65 20 61 72 65 20 63 6f 6c 6c 65 63 74 65 64  le are collected
75960 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c  .** into a singl
75970 65 20 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65  e structure name
75980 64 20 22 6d 65 6d 22 2e 20 20 54 68 69 73 20 69  d "mem".  This i
75990 73 20 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a  s to keep the.**
759a0 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65   static variable
759b0 73 20 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64 20  s organized and 
759c0 74 6f 20 72 65 64 75 63 65 20 6e 61 6d 65 73 70  to reduce namesp
759d0 61 63 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a  ace pollution.**
759e0 20 77 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c   when this modul
759f0 65 20 69 73 20 63 6f 6d 62 69 6e 65 64 20 77 69  e is combined wi
75a00 74 68 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20  th other in the 
75a10 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f  amalgamation..*/
75a20 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 7b  .static struct {
75a30 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 72 75 65 20  .  /*.  ** True 
75a40 69 66 20 77 65 20 61 72 65 20 65 76 61 6c 75 61  if we are evalua
75a50 74 69 6e 67 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d  ting an out-of-m
75a60 65 6d 6f 72 79 20 63 61 6c 6c 62 61 63 6b 2e 0a  emory callback..
75a70 20 20 2a 2f 0a 20 20 69 6e 74 20 61 6c 61 72 6d    */.  int alarm
75a80 42 75 73 79 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20  Busy;.  .  /*.  
75a90 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74  ** Mutex to cont
75aa0 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68  rol access to th
75ab0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
75ac0 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20  ion subsystem.. 
75ad0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   */.  sqlite3_mu
75ae0 74 65 78 20 2a 6d 75 74 65 78 3b 0a 20 20 0a 20  tex *mutex;.  . 
75af0 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6d 69 6e   /*.  ** The min
75b00 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 66  imum amount of f
75b10 72 65 65 20 73 70 61 63 65 20 74 68 61 74 20 77  ree space that w
75b20 65 20 68 61 76 65 20 73 65 65 6e 2e 0a 20 20 2a  e have seen..  *
75b30 2f 0a 20 20 75 33 32 20 6d 6e 4d 61 73 74 65 72  /.  u32 mnMaster
75b40 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 69 4d 61  ;..  /*.  ** iMa
75b50 73 74 65 72 20 69 73 20 74 68 65 20 69 6e 64 65  ster is the inde
75b60 78 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20  x of the master 
75b70 63 68 75 6e 6b 2e 20 20 4d 6f 73 74 20 6e 65 77  chunk.  Most new
75b80 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 2a   allocations.  *
75b90 2a 20 6f 63 63 75 72 20 6f 66 66 20 6f 66 20 74  * occur off of t
75ba0 68 69 73 20 63 68 75 6e 6b 2e 20 20 73 7a 4d 61  his chunk.  szMa
75bb0 73 74 65 72 20 69 73 20 74 68 65 20 73 69 7a 65  ster is the size
75bc0 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29   (in Mem3Blocks)
75bd0 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 63 75 72  .  ** of the cur
75be0 72 65 6e 74 20 6d 61 73 74 65 72 2e 20 20 69 4d  rent master.  iM
75bf0 61 73 74 65 72 20 69 73 20 30 20 69 66 20 74 68  aster is 0 if th
75c00 65 72 65 20 69 73 20 6e 6f 74 20 6d 61 73 74 65  ere is not maste
75c10 72 20 63 68 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68  r chunk..  ** Th
75c20 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 69  e master chunk i
75c30 73 20 6e 6f 74 20 69 6e 20 65 69 74 68 65 72 20  s not in either 
75c40 74 68 65 20 61 69 48 61 73 68 5b 5d 20 6f 72 20  the aiHash[] or 
75c50 61 69 53 6d 61 6c 6c 5b 5d 2e 0a 20 20 2a 2f 0a  aiSmall[]..  */.
75c60 20 20 75 33 32 20 69 4d 61 73 74 65 72 3b 0a 20    u32 iMaster;. 
75c70 20 75 33 32 20 73 7a 4d 61 73 74 65 72 3b 0a 0a   u32 szMaster;..
75c80 20 20 2f 2a 0a 20 20 2a 2a 20 41 72 72 61 79 20    /*.  ** Array 
75c90 6f 66 20 6c 69 73 74 73 20 6f 66 20 66 72 65 65  of lists of free
75ca0 20 62 6c 6f 63 6b 73 20 61 63 63 6f 72 64 69 6e   blocks accordin
75cb0 67 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b 20 73  g to the block s
75cc0 69 7a 65 20 0a 20 20 2a 2a 20 66 6f 72 20 73 6d  ize .  ** for sm
75cd0 61 6c 6c 65 72 20 63 68 75 6e 6b 73 2c 20 6f 72  aller chunks, or
75ce0 20 61 20 68 61 73 68 20 6f 6e 20 74 68 65 20 62   a hash on the b
75cf0 6c 6f 63 6b 20 73 69 7a 65 20 66 6f 72 20 6c 61  lock size for la
75d00 72 67 65 72 0a 20 20 2a 2a 20 63 68 75 6e 6b 73  rger.  ** chunks
75d10 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 61 69 53  ..  */.  u32 aiS
75d20 6d 61 6c 6c 5b 4d 58 5f 53 4d 41 4c 4c 2d 31 5d  mall[MX_SMALL-1]
75d30 3b 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 73  ;   /* For sizes
75d40 20 32 20 74 68 72 6f 75 67 68 20 4d 58 5f 53 4d   2 through MX_SM
75d50 41 4c 4c 2c 20 69 6e 63 6c 75 73 69 76 65 20 2a  ALL, inclusive *
75d60 2f 0a 20 20 75 33 32 20 61 69 48 61 73 68 5b 4e  /.  u32 aiHash[N
75d70 5f 48 41 53 48 5d 3b 20 20 20 20 20 20 20 20 2f  _HASH];        /
75d80 2a 20 46 6f 72 20 73 69 7a 65 73 20 4d 58 5f 53  * For sizes MX_S
75d90 4d 41 4c 4c 2b 31 20 61 6e 64 20 6c 61 72 67 65  MALL+1 and large
75da0 72 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  r */..  /*.  ** 
75db0 4d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65  Memory available
75dc0 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a   for allocation.
75dd0 20 20 2a 2f 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b    */.  Mem3Block
75de0 20 61 50 6f 6f 6c 5b 53 51 4c 49 54 45 5f 4d 45   aPool[SQLITE_ME
75df0 4d 4f 52 59 5f 53 49 5a 45 2f 73 69 7a 65 6f 66  MORY_SIZE/sizeof
75e00 28 4d 65 6d 33 42 6c 6f 63 6b 29 2b 32 5d 3b 0a  (Mem3Block)+2];.
75e10 7d 20 6d 65 6d 3b 0a 0a 2f 2a 0a 2a 2a 20 55 6e  } mem;../*.** Un
75e20 6c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61  link the chunk a
75e30 74 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 69 5d 20 66  t mem.aPool[i] f
75e40 72 6f 6d 20 6c 69 73 74 20 69 74 20 69 73 20 63  rom list it is c
75e50 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 2e 20  urrently.** on. 
75e60 20 2a 70 52 6f 6f 74 20 69 73 20 74 68 65 20 6c   *pRoot is the l
75e70 69 73 74 20 74 68 61 74 20 69 20 69 73 20 61 20  ist that i is a 
75e80 6d 65 6d 62 65 72 20 6f 66 2e 0a 2a 2f 0a 73 74  member of..*/.st
75e90 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73  atic void memsys
75ea0 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28  3UnlinkFromList(
75eb0 75 33 32 20 69 2c 20 75 33 32 20 2a 70 52 6f 6f  u32 i, u32 *pRoo
75ec0 74 29 7b 0a 20 20 75 33 32 20 6e 65 78 74 20 3d  t){.  u32 next =
75ed0 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e   mem.aPool[i].u.
75ee0 6c 69 73 74 2e 6e 65 78 74 3b 0a 20 20 75 33 32  list.next;.  u32
75ef0 20 70 72 65 76 20 3d 20 6d 65 6d 2e 61 50 6f 6f   prev = mem.aPoo
75f00 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76  l[i].u.list.prev
75f10 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
75f20 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d  te3_mutex_held(m
75f30 65 6d 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69  em.mutex) );.  i
75f40 66 28 20 70 72 65 76 3d 3d 30 20 29 7b 0a 20 20  f( prev==0 ){.  
75f50 20 20 2a 70 52 6f 6f 74 20 3d 20 6e 65 78 74 3b    *pRoot = next;
75f60 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65  .  }else{.    me
75f70 6d 2e 61 50 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e  m.aPool[prev].u.
75f80 6c 69 73 74 2e 6e 65 78 74 20 3d 20 6e 65 78 74  list.next = next
75f90 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 78 74  ;.  }.  if( next
75fa0 20 29 7b 0a 20 20 20 20 6d 65 6d 2e 61 50 6f 6f   ){.    mem.aPoo
75fb0 6c 5b 6e 65 78 74 5d 2e 75 2e 6c 69 73 74 2e 70  l[next].u.list.p
75fc0 72 65 76 20 3d 20 70 72 65 76 3b 0a 20 20 7d 0a  rev = prev;.  }.
75fd0 20 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 69 5d 2e 75    mem.aPool[i].u
75fe0 2e 6c 69 73 74 2e 6e 65 78 74 20 3d 20 30 3b 0a  .list.next = 0;.
75ff0 20 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 69 5d 2e 75    mem.aPool[i].u
76000 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 30 3b 0a  .list.prev = 0;.
76010 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20  }../*.** Unlink 
76020 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69 6e 64  the chunk at ind
76030 65 78 20 69 20 66 72 6f 6d 20 0a 2a 2a 20 77 68  ex i from .** wh
76040 61 74 65 76 65 72 20 6c 69 73 74 20 69 73 20 63  atever list is c
76050 75 72 72 65 6e 74 6c 79 20 61 20 6d 65 6d 62 65  urrently a membe
76060 72 20 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  r of..*/.static 
76070 76 6f 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69  void memsys3Unli
76080 6e 6b 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32  nk(u32 i){.  u32
76090 20 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61   size, hash;.  a
760a0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
760b0 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 2e 6d 75  utex_held(mem.mu
760c0 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
760d0 28 20 28 6d 65 6d 2e 61 50 6f 6f 6c 5b 69 2d 31  ( (mem.aPool[i-1
760e0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26  ].u.hdr.size4x &
760f0 20 31 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65   1)==0 );.  asse
76100 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 73 69  rt( i>=1 );.  si
76110 7a 65 20 3d 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 69  ze = mem.aPool[i
76120 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
76130 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69  /4;.  assert( si
76140 7a 65 3d 3d 6d 65 6d 2e 61 50 6f 6f 6c 5b 69 2b  ze==mem.aPool[i+
76150 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72  size-1].u.hdr.pr
76160 65 76 53 69 7a 65 20 29 3b 0a 20 20 61 73 73 65  evSize );.  asse
76170 72 74 28 20 73 69 7a 65 3e 3d 32 20 29 3b 0a 20  rt( size>=2 );. 
76180 20 69 66 28 20 73 69 7a 65 20 3c 3d 20 4d 58 5f   if( size <= MX_
76190 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 20 6d 65 6d  SMALL ){.    mem
761a0 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69  sys3UnlinkFromLi
761b0 73 74 28 69 2c 20 26 6d 65 6d 2e 61 69 53 6d 61  st(i, &mem.aiSma
761c0 6c 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a 20 20 7d  ll[size-2]);.  }
761d0 65 6c 73 65 7b 0a 20 20 20 20 68 61 73 68 20 3d  else{.    hash =
761e0 20 73 69 7a 65 20 25 20 4e 5f 48 41 53 48 3b 0a   size % N_HASH;.
761f0 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e      memsys3Unlin
76200 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65  kFromList(i, &me
76210 6d 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 29 3b  m.aiHash[hash]);
76220 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69  .  }.}../*.** Li
76230 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20  nk the chunk at 
76240 6d 65 6d 2e 61 50 6f 6f 6c 5b 69 5d 20 73 6f 20  mem.aPool[i] so 
76250 74 68 61 74 20 69 73 20 6f 6e 20 74 68 65 20 6c  that is on the l
76260 69 73 74 20 72 6f 6f 74 65 64 0a 2a 2a 20 61 74  ist rooted.** at
76270 20 2a 70 52 6f 6f 74 2e 0a 2a 2f 0a 73 74 61 74   *pRoot..*/.stat
76280 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c  ic void memsys3L
76290 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 75 33 32 20  inkIntoList(u32 
762a0 69 2c 20 75 33 32 20 2a 70 52 6f 6f 74 29 7b 0a  i, u32 *pRoot){.
762b0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
762c0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d  3_mutex_held(mem
762d0 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6d 65 6d  .mutex) );.  mem
762e0 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74  .aPool[i].u.list
762f0 2e 6e 65 78 74 20 3d 20 2a 70 52 6f 6f 74 3b 0a  .next = *pRoot;.
76300 20 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 69 5d 2e 75    mem.aPool[i].u
76310 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 30 3b 0a  .list.prev = 0;.
76320 20 20 69 66 28 20 2a 70 52 6f 6f 74 20 29 7b 0a    if( *pRoot ){.
76330 20 20 20 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 2a 70      mem.aPool[*p
76340 52 6f 6f 74 5d 2e 75 2e 6c 69 73 74 2e 70 72 65  Root].u.list.pre
76350 76 20 3d 20 69 3b 0a 20 20 7d 0a 20 20 2a 70 52  v = i;.  }.  *pR
76360 6f 6f 74 20 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a  oot = i;.}../*.*
76370 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b  * Link the chunk
76380 20 61 74 20 69 6e 64 65 78 20 69 20 69 6e 74 6f   at index i into
76390 20 65 69 74 68 65 72 20 74 68 65 20 61 70 70 72   either the appr
763a0 6f 70 72 69 61 74 65 0a 2a 2a 20 73 6d 61 6c 6c  opriate.** small
763b0 20 63 68 75 6e 6b 20 6c 69 73 74 2c 20 6f 72 20   chunk list, or 
763c0 69 6e 74 6f 20 74 68 65 20 6c 61 72 67 65 20 63  into the large c
763d0 68 75 6e 6b 20 68 61 73 68 20 74 61 62 6c 65 2e  hunk hash table.
763e0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
763f0 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 75 33 32 20  memsys3Link(u32 
76400 69 29 7b 0a 20 20 75 33 32 20 73 69 7a 65 2c 20  i){.  u32 size, 
76410 68 61 73 68 3b 0a 20 20 61 73 73 65 72 74 28 20  hash;.  assert( 
76420 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
76430 6c 64 28 6d 65 6d 2e 6d 75 74 65 78 29 20 29 3b  ld(mem.mutex) );
76440 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 31 20  .  assert( i>=1 
76450 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65  );.  assert( (me
76460 6d 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68  m.aPool[i-1].u.h
76470 64 72 2e 73 69 7a 65 34 78 20 26 20 31 29 3d 3d  dr.size4x & 1)==
76480 30 20 29 3b 0a 20 20 73 69 7a 65 20 3d 20 6d 65  0 );.  size = me
76490 6d 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68  m.aPool[i-1].u.h
764a0 64 72 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61  dr.size4x/4;.  a
764b0 73 73 65 72 74 28 20 73 69 7a 65 3d 3d 6d 65 6d  ssert( size==mem
764c0 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d  .aPool[i+size-1]
764d0 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20  .u.hdr.prevSize 
764e0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a  );.  assert( siz
764f0 65 3e 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 69  e>=2 );.  if( si
76500 7a 65 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29  ze <= MX_SMALL )
76510 7b 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e  {.    memsys3Lin
76520 6b 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65  kIntoList(i, &me
76530 6d 2e 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32  m.aiSmall[size-2
76540 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ]);.  }else{.   
76550 20 68 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e   hash = size % N
76560 5f 48 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79  _HASH;.    memsy
76570 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 69  s3LinkIntoList(i
76580 2c 20 26 6d 65 6d 2e 61 69 48 61 73 68 5b 68 61  , &mem.aiHash[ha
76590 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  sh]);.  }.}../*.
765a0 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74  ** Enter the mut
765b0 65 78 20 6d 65 6d 2e 6d 75 74 65 78 2e 20 41 6c  ex mem.mutex. Al
765c0 6c 6f 63 61 74 65 20 69 74 20 69 66 20 69 74 20  locate it if it 
765d0 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 61  is not already a
765e0 6c 6c 6f 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  llocated..**.** 
765f0 41 6c 73 6f 3a 20 20 49 6e 69 74 69 61 6c 69 7a  Also:  Initializ
76600 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  e the memory all
76610 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65  ocation subsyste
76620 6d 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65  m the first time
76630 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
76640 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73   is called..*/.s
76650 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79  tatic void memsy
76660 73 33 45 6e 74 65 72 28 76 6f 69 64 29 7b 0a 20  s3Enter(void){. 
76670 20 69 66 28 20 6d 65 6d 2e 6d 75 74 65 78 3d 3d   if( mem.mutex==
76680 30 20 29 7b 0a 20 20 20 20 6d 65 6d 2e 6d 75 74  0 ){.    mem.mut
76690 65 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 75 74  ex = sqlite3_mut
766a0 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  ex_alloc(SQLITE_
766b0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d  MUTEX_STATIC_MEM
766c0 29 3b 0a 20 20 20 20 6d 65 6d 2e 61 50 6f 6f 6c  );.    mem.aPool
766d0 5b 30 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  [0].u.hdr.size4x
766e0 20 3d 20 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59   = SQLITE_MEMORY
766f0 5f 53 49 5a 45 2f 32 20 2b 20 32 3b 0a 20 20 20  _SIZE/2 + 2;.   
76700 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 53 51 4c 49 54   mem.aPool[SQLIT
76710 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 2f 38 5d  E_MEMORY_SIZE/8]
76720 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20  .u.hdr.prevSize 
76730 3d 20 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f  = SQLITE_MEMORY_
76740 53 49 5a 45 2f 38 3b 0a 20 20 20 20 6d 65 6d 2e  SIZE/8;.    mem.
76750 61 50 6f 6f 6c 5b 53 51 4c 49 54 45 5f 4d 45 4d  aPool[SQLITE_MEM
76760 4f 52 59 5f 53 49 5a 45 2f 38 5d 2e 75 2e 68 64  ORY_SIZE/8].u.hd
76770 72 2e 73 69 7a 65 34 78 20 3d 20 31 3b 0a 20 20  r.size4x = 1;.  
76780 20 20 6d 65 6d 2e 69 4d 61 73 74 65 72 20 3d 20    mem.iMaster = 
76790 31 3b 0a 20 20 20 20 6d 65 6d 2e 73 7a 4d 61 73  1;.    mem.szMas
767a0 74 65 72 20 3d 20 53 51 4c 49 54 45 5f 4d 45 4d  ter = SQLITE_MEM
767b0 4f 52 59 5f 53 49 5a 45 2f 38 3b 0a 20 20 20 20  ORY_SIZE/8;.    
767c0 6d 65 6d 2e 6d 6e 4d 61 73 74 65 72 20 3d 20 6d  mem.mnMaster = m
767d0 65 6d 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 7d  em.szMaster;.  }
767e0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
767f0 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65 78  _enter(mem.mutex
76800 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
76810 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66  rn the amount of
76820 20 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e 74 6c   memory currentl
76830 79 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 0a 2a  y checked out..*
76840 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  /.SQLITE_API sql
76850 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74  ite3_int64 sqlit
76860 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 76  e3_memory_used(v
76870 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  oid){.  sqlite3_
76880 69 6e 74 36 34 20 6e 3b 0a 20 20 6d 65 6d 73 79  int64 n;.  memsy
76890 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 6e 20 3d  s3Enter();.  n =
768a0 20 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53   SQLITE_MEMORY_S
768b0 49 5a 45 20 2d 20 6d 65 6d 2e 73 7a 4d 61 73 74  IZE - mem.szMast
768c0 65 72 2a 38 3b 0a 20 20 73 71 6c 69 74 65 33 5f  er*8;.  sqlite3_
768d0 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e  mutex_leave(mem.
768e0 6d 75 74 65 78 29 3b 20 20 0a 20 20 72 65 74 75  mutex);  .  retu
768f0 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  rn n;.}../*.** R
76900 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75  eturn the maximu
76910 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f  m amount of memo
76920 72 79 20 74 68 61 74 20 68 61 73 20 65 76 65 72  ry that has ever
76930 20 62 65 65 6e 0a 2a 2a 20 63 68 65 63 6b 65 64   been.** checked
76940 20 6f 75 74 20 73 69 6e 63 65 20 65 69 74 68 65   out since eithe
76950 72 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  r the beginning 
76960 6f 66 20 74 68 69 73 20 70 72 6f 63 65 73 73 0a  of this process.
76970 2a 2a 20 6f 72 20 73 69 6e 63 65 20 74 68 65 20  ** or since the 
76980 6d 6f 73 74 20 72 65 63 65 6e 74 20 72 65 73 65  most recent rese
76990 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
769a0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73   sqlite3_int64 s
769b0 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69  qlite3_memory_hi
769c0 67 68 77 61 74 65 72 28 69 6e 74 20 72 65 73 65  ghwater(int rese
769d0 74 46 6c 61 67 29 7b 0a 20 20 73 71 6c 69 74 65  tFlag){.  sqlite
769e0 33 5f 69 6e 74 36 34 20 6e 3b 0a 20 20 6d 65 6d  3_int64 n;.  mem
769f0 73 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 6e  sys3Enter();.  n
76a00 20 3d 20 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59   = SQLITE_MEMORY
76a10 5f 53 49 5a 45 20 2d 20 6d 65 6d 2e 6d 6e 4d 61  _SIZE - mem.mnMa
76a20 73 74 65 72 2a 38 3b 0a 20 20 69 66 28 20 72 65  ster*8;.  if( re
76a30 73 65 74 46 6c 61 67 20 29 7b 0a 20 20 20 20 6d  setFlag ){.    m
76a40 65 6d 2e 6d 6e 4d 61 73 74 65 72 20 3d 20 6d 65  em.mnMaster = me
76a50 6d 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 7d 0a  m.szMaster;.  }.
76a60 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
76a70 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29  leave(mem.mutex)
76a80 3b 20 20 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a  ;  .  return n;.
76a90 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  }../*.** Change 
76aa0 74 68 65 20 61 6c 61 72 6d 20 63 61 6c 6c 62 61  the alarm callba
76ab0 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  ck..**.** This i
76ac0 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 74 68  s a no-op for th
76ad0 65 20 73 74 61 74 69 63 20 6d 65 6d 6f 72 79 20  e static memory 
76ae0 61 6c 6c 6f 63 61 74 6f 72 2e 20 20 54 68 65 20  allocator.  The 
76af0 70 75 72 70 6f 73 65 0a 2a 2a 20 6f 66 20 74 68  purpose.** of th
76b00 65 20 6d 65 6d 6f 72 79 20 61 6c 61 72 6d 20 69  e memory alarm i
76b10 73 20 74 6f 20 73 75 70 70 6f 72 74 20 73 71 6c  s to support sql
76b20 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c  ite3_soft_heap_l
76b30 69 6d 69 74 28 29 2e 0a 2a 2a 20 42 75 74 20 77  imit()..** But w
76b40 69 74 68 20 74 68 69 73 20 6d 65 6d 6f 72 79 20  ith this memory 
76b50 61 6c 6c 6f 63 61 74 6f 72 2c 20 74 68 65 20 73  allocator, the s
76b60 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 20 69  oft_heap_limit i
76b70 73 20 72 65 61 6c 6c 79 0a 2a 2a 20 61 20 68 61  s really.** a ha
76b80 72 64 20 6c 69 6d 69 74 20 74 68 61 74 20 69 73  rd limit that is
76b90 20 66 69 78 65 64 20 61 74 20 53 51 4c 49 54 45   fixed at SQLITE
76ba0 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 2e 0a 2a 2f  _MEMORY_SIZE..*/
76bb0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
76bc0 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 61  sqlite3_memory_a
76bd0 6c 61 72 6d 28 0a 20 20 76 6f 69 64 28 2a 78 43  larm(.  void(*xC
76be0 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 20 2a 70  allback)(void *p
76bf0 41 72 67 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74  Arg, sqlite3_int
76c00 36 34 20 75 73 65 64 2c 69 6e 74 20 4e 29 2c 0a  64 used,int N),.
76c10 20 20 76 6f 69 64 20 2a 70 41 72 67 2c 0a 20 20    void *pArg,.  
76c20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 54  sqlite3_int64 iT
76c30 68 72 65 73 68 6f 6c 64 0a 29 7b 0a 20 20 72 65  hreshold.){.  re
76c40 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
76c50 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64 20  }../*.** Called 
76c60 77 68 65 6e 20 77 65 20 61 72 65 20 75 6e 61 62  when we are unab
76c70 6c 65 20 74 6f 20 73 61 74 69 73 66 79 20 61 6e  le to satisfy an
76c80 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 6e   allocation of n
76c90 42 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  Bytes..*/.static
76ca0 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4f 75 74   void memsys3Out
76cb0 4f 66 4d 65 6d 6f 72 79 28 69 6e 74 20 6e 42 79  OfMemory(int nBy
76cc0 74 65 29 7b 0a 20 20 69 66 28 20 21 6d 65 6d 2e  te){.  if( !mem.
76cd0 61 6c 61 72 6d 42 75 73 79 20 29 7b 0a 20 20 20  alarmBusy ){.   
76ce0 20 6d 65 6d 2e 61 6c 61 72 6d 42 75 73 79 20 3d   mem.alarmBusy =
76cf0 20 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   1;.    assert( 
76d00 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
76d10 6c 64 28 6d 65 6d 2e 6d 75 74 65 78 29 20 29 3b  ld(mem.mutex) );
76d20 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
76d30 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74  ex_leave(mem.mut
76d40 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ex);.    sqlite3
76d50 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28  _release_memory(
76d60 6e 42 79 74 65 29 3b 0a 20 20 20 20 73 71 6c 69  nByte);.    sqli
76d70 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
76d80 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20  mem.mutex);.    
76d90 6d 65 6d 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20  mem.alarmBusy = 
76da0 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  0;.  }.}../*.** 
76db0 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20  Return the size 
76dc0 6f 66 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e  of an outstandin
76dd0 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e  g allocation, in
76de0 20 62 79 74 65 73 2e 20 20 54 68 65 0a 2a 2a 20   bytes.  The.** 
76df0 73 69 7a 65 20 72 65 74 75 72 6e 65 64 20 6f 6d  size returned om
76e00 69 74 73 20 74 68 65 20 38 2d 62 79 74 65 20 68  its the 8-byte h
76e10 65 61 64 65 72 20 6f 76 65 72 68 65 61 64 2e 20  eader overhead. 
76e20 20 54 68 69 73 20 6f 6e 6c 79 0a 2a 2a 20 77 6f   This only.** wo
76e30 72 6b 73 20 66 6f 72 20 63 68 75 6e 6b 73 20 74  rks for chunks t
76e40 68 61 74 20 61 72 65 20 63 75 72 72 65 6e 74 6c  hat are currentl
76e50 79 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 0a 2a  y checked out..*
76e60 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
76e70 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c   int sqlite3Mall
76e80 6f 63 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b  ocSize(void *p){
76e90 0a 20 20 69 6e 74 20 69 53 69 7a 65 20 3d 20 30  .  int iSize = 0
76ea0 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  ;.  if( p ){.   
76eb0 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 70 42 6c 6f   Mem3Block *pBlo
76ec0 63 6b 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 2a  ck = (Mem3Block*
76ed0 29 70 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  )p;.    assert( 
76ee0 28 70 42 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e 68 64  (pBlock[-1].u.hd
76ef0 72 2e 73 69 7a 65 34 78 26 31 29 21 3d 30 20 29  r.size4x&1)!=0 )
76f00 3b 0a 20 20 20 20 69 53 69 7a 65 20 3d 20 28 70  ;.    iSize = (p
76f10 42 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e 68 64 72 2e  Block[-1].u.hdr.
76f20 73 69 7a 65 34 78 26 7e 33 29 2a 32 20 2d 20 34  size4x&~3)*2 - 4
76f30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69  ;.  }.  return i
76f40 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  Size;.}../*.** C
76f50 68 75 6e 6b 20 69 20 69 73 20 61 20 66 72 65 65  hunk i is a free
76f60 20 63 68 75 6e 6b 20 74 68 61 74 20 68 61 73 20   chunk that has 
76f70 62 65 65 6e 20 75 6e 6c 69 6e 6b 65 64 2e 20 20  been unlinked.  
76f80 41 64 6a 75 73 74 20 69 74 73 20 0a 2a 2a 20 73  Adjust its .** s
76f90 69 7a 65 20 70 61 72 61 6d 65 74 65 72 73 20 66  ize parameters f
76fa0 6f 72 20 63 68 65 63 6b 2d 6f 75 74 20 61 6e 64  or check-out and
76fb0 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   return a pointe
76fc0 72 20 74 6f 20 74 68 65 20 0a 2a 2a 20 75 73 65  r to the .** use
76fd0 72 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65  r portion of the
76fe0 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 73 74 61 74 69   chunk..*/.stati
76ff0 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33 43  c void *memsys3C
77000 68 65 63 6b 6f 75 74 28 75 33 32 20 69 2c 20 69  heckout(u32 i, i
77010 6e 74 20 6e 42 6c 6f 63 6b 29 7b 0a 20 20 75 33  nt nBlock){.  u3
77020 32 20 78 3b 0a 20 20 61 73 73 65 72 74 28 20 73  2 x;.  assert( s
77030 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
77040 64 28 6d 65 6d 2e 6d 75 74 65 78 29 20 29 3b 0a  d(mem.mutex) );.
77050 20 20 61 73 73 65 72 74 28 20 69 3e 3d 31 20 29    assert( i>=1 )
77060 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e  ;.  assert( mem.
77070 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72  aPool[i-1].u.hdr
77080 2e 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63  .size4x/4==nBloc
77090 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d  k );.  assert( m
770a0 65 6d 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63  em.aPool[i+nBloc
770b0 6b 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53  k-1].u.hdr.prevS
770c0 69 7a 65 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20  ize==nBlock );. 
770d0 20 78 20 3d 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 69   x = mem.aPool[i
770e0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
770f0 3b 0a 20 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 69 2d  ;.  mem.aPool[i-
77100 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  1].u.hdr.size4x 
77110 3d 20 6e 42 6c 6f 63 6b 2a 34 20 7c 20 31 20 7c  = nBlock*4 | 1 |
77120 20 28 78 26 32 29 3b 0a 20 20 6d 65 6d 2e 61 50   (x&2);.  mem.aP
77130 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e  ool[i+nBlock-1].
77140 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d  u.hdr.prevSize =
77150 20 6e 42 6c 6f 63 6b 3b 0a 20 20 6d 65 6d 2e 61   nBlock;.  mem.a
77160 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d  Pool[i+nBlock-1]
77170 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d  .u.hdr.size4x |=
77180 20 32 3b 0a 20 20 72 65 74 75 72 6e 20 26 6d 65   2;.  return &me
77190 6d 2e 61 50 6f 6f 6c 5b 69 5d 3b 0a 7d 0a 0a 2f  m.aPool[i];.}../
771a0 2a 0a 2a 2a 20 43 61 72 76 65 20 61 20 70 69 65  *.** Carve a pie
771b0 63 65 20 6f 66 66 20 6f 66 20 74 68 65 20 65 6e  ce off of the en
771c0 64 20 6f 66 20 74 68 65 20 6d 65 6d 2e 69 4d 61  d of the mem.iMa
771d0 73 74 65 72 20 66 72 65 65 20 63 68 75 6e 6b 2e  ster free chunk.
771e0 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69  .** Return a poi
771f0 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20  nter to the new 
77200 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 4f 72 2c  allocation.  Or,
77210 20 69 66 20 74 68 65 20 6d 61 73 74 65 72 20 63   if the master c
77220 68 75 6e 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c  hunk.** is not l
77230 61 72 67 65 20 65 6e 6f 75 67 68 2c 20 72 65 74  arge enough, ret
77240 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63  urn 0..*/.static
77250 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33 46 72   void *memsys3Fr
77260 6f 6d 4d 61 73 74 65 72 28 69 6e 74 20 6e 42 6c  omMaster(int nBl
77270 6f 63 6b 29 7b 0a 20 20 61 73 73 65 72 74 28 20  ock){.  assert( 
77280 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
77290 6c 64 28 6d 65 6d 2e 6d 75 74 65 78 29 20 29 3b  ld(mem.mutex) );
772a0 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 73  .  assert( mem.s
772b0 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20  zMaster>=nBlock 
772c0 29 3b 0a 20 20 69 66 28 20 6e 42 6c 6f 63 6b 3e  );.  if( nBlock>
772d0 3d 6d 65 6d 2e 73 7a 4d 61 73 74 65 72 2d 31 20  =mem.szMaster-1 
772e0 29 7b 0a 20 20 20 20 2f 2a 20 55 73 65 20 74 68  ){.    /* Use th
772f0 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65 72 20  e entire master 
77300 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 20 3d  */.    void *p =
77310 20 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75 74   memsys3Checkout
77320 28 6d 65 6d 2e 69 4d 61 73 74 65 72 2c 20 6d 65  (mem.iMaster, me
77330 6d 2e 73 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20  m.szMaster);.   
77340 20 6d 65 6d 2e 69 4d 61 73 74 65 72 20 3d 20 30   mem.iMaster = 0
77350 3b 0a 20 20 20 20 6d 65 6d 2e 73 7a 4d 61 73 74  ;.    mem.szMast
77360 65 72 20 3d 20 30 3b 0a 20 20 20 20 6d 65 6d 2e  er = 0;.    mem.
77370 6d 6e 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20  mnMaster = 0;.  
77380 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 7d 65    return p;.  }e
77390 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 53 70 6c 69  lse{.    /* Spli
773a0 74 20 74 68 65 20 6d 61 73 74 65 72 20 62 6c 6f  t the master blo
773b0 63 6b 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  ck.  Return the 
773c0 74 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 75 33 32  tail. */.    u32
773d0 20 6e 65 77 69 2c 20 78 3b 0a 20 20 20 20 6e 65   newi, x;.    ne
773e0 77 69 20 3d 20 6d 65 6d 2e 69 4d 61 73 74 65 72  wi = mem.iMaster
773f0 20 2b 20 6d 65 6d 2e 73 7a 4d 61 73 74 65 72 20   + mem.szMaster 
77400 2d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 20 20 61 73  - nBlock;.    as
77410 73 65 72 74 28 20 6e 65 77 69 20 3e 20 6d 65 6d  sert( newi > mem
77420 2e 69 4d 61 73 74 65 72 2b 31 20 29 3b 0a 20 20  .iMaster+1 );.  
77430 20 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 6d 65 6d 2e    mem.aPool[mem.
77440 69 4d 61 73 74 65 72 2b 6d 65 6d 2e 73 7a 4d 61  iMaster+mem.szMa
77450 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72  ster-1].u.hdr.pr
77460 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f 63 6b 3b  evSize = nBlock;
77470 0a 20 20 20 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 6d  .    mem.aPool[m
77480 65 6d 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 2e 73  em.iMaster+mem.s
77490 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72  zMaster-1].u.hdr
774a0 2e 73 69 7a 65 34 78 20 7c 3d 20 32 3b 0a 20 20  .size4x |= 2;.  
774b0 20 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 6e 65 77 69    mem.aPool[newi
774c0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
774d0 20 3d 20 6e 42 6c 6f 63 6b 2a 34 20 2b 20 31 3b   = nBlock*4 + 1;
774e0 0a 20 20 20 20 6d 65 6d 2e 73 7a 4d 61 73 74 65  .    mem.szMaste
774f0 72 20 2d 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 20  r -= nBlock;.   
77500 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 6e 65 77 69 2d   mem.aPool[newi-
77510 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a  1].u.hdr.prevSiz
77520 65 20 3d 20 6d 65 6d 2e 73 7a 4d 61 73 74 65 72  e = mem.szMaster
77530 3b 0a 20 20 20 20 78 20 3d 20 6d 65 6d 2e 61 50  ;.    x = mem.aP
77540 6f 6f 6c 5b 6d 65 6d 2e 69 4d 61 73 74 65 72 2d  ool[mem.iMaster-
77550 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  1].u.hdr.size4x 
77560 26 20 32 3b 0a 20 20 20 20 6d 65 6d 2e 61 50 6f  & 2;.    mem.aPo
77570 6f 6c 5b 6d 65 6d 2e 69 4d 61 73 74 65 72 2d 31  ol[mem.iMaster-1
77580 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d  ].u.hdr.size4x =
77590 20 6d 65 6d 2e 73 7a 4d 61 73 74 65 72 2a 34 20   mem.szMaster*4 
775a0 7c 20 78 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d  | x;.    if( mem
775b0 2e 73 7a 4d 61 73 74 65 72 20 3c 20 6d 65 6d 2e  .szMaster < mem.
775c0 6d 6e 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20  mnMaster ){.    
775d0 20 20 6d 65 6d 2e 6d 6e 4d 61 73 74 65 72 20 3d    mem.mnMaster =
775e0 20 6d 65 6d 2e 73 7a 4d 61 73 74 65 72 3b 0a 20   mem.szMaster;. 
775f0 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
77600 28 76 6f 69 64 2a 29 26 6d 65 6d 2e 61 50 6f 6f  (void*)&mem.aPoo
77610 6c 5b 6e 65 77 69 5d 3b 0a 20 20 7d 0a 7d 0a 0a  l[newi];.  }.}..
77620 2f 2a 0a 2a 2a 20 2a 70 52 6f 6f 74 20 69 73 20  /*.** *pRoot is 
77630 74 68 65 20 68 65 61 64 20 6f 66 20 61 20 6c 69  the head of a li
77640 73 74 20 6f 66 20 66 72 65 65 20 63 68 75 6e 6b  st of free chunk
77650 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 73 69  s of the same si
77660 7a 65 0a 2a 2a 20 6f 72 20 73 61 6d 65 20 73 69  ze.** or same si
77670 7a 65 20 68 61 73 68 2e 20 20 49 6e 20 6f 74 68  ze hash.  In oth
77680 65 72 20 77 6f 72 64 73 2c 20 2a 70 52 6f 6f 74  er words, *pRoot
77690 20 69 73 20 61 6e 20 65 6e 74 72 79 20 69 6e 20   is an entry in 
776a0 65 69 74 68 65 72 0a 2a 2a 20 6d 65 6d 2e 61 69  either.** mem.ai
776b0 53 6d 61 6c 6c 5b 5d 20 6f 72 20 6d 65 6d 2e 61  Small[] or mem.a
776c0 69 48 61 73 68 5b 5d 2e 20 20 0a 2a 2a 0a 2a 2a  iHash[].  .**.**
776d0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 78   This routine ex
776e0 61 6d 69 6e 65 73 20 61 6c 6c 20 65 6e 74 72 69  amines all entri
776f0 65 73 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20  es on the given 
77700 6c 69 73 74 20 61 6e 64 20 74 72 69 65 73 0a 2a  list and tries.*
77710 2a 20 74 6f 20 63 6f 61 6c 65 73 63 65 20 65 61  * to coalesce ea
77720 63 68 20 65 6e 74 72 69 65 73 20 77 69 74 68 20  ch entries with 
77730 61 64 6a 61 63 65 6e 74 20 66 72 65 65 20 63 68  adjacent free ch
77740 75 6e 6b 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66  unks.  .**.** If
77750 20 69 74 20 73 65 65 73 20 61 20 63 68 75 6e 6b   it sees a chunk
77760 20 74 68 61 74 20 69 73 20 6c 61 72 67 65 72 20   that is larger 
77770 74 68 61 6e 20 6d 65 6d 2e 69 4d 61 73 74 65 72  than mem.iMaster
77780 2c 20 69 74 20 72 65 70 6c 61 63 65 73 20 0a 2a  , it replaces .*
77790 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 65  * the current me
777a0 6d 2e 69 4d 61 73 74 65 72 20 77 69 74 68 20 74  m.iMaster with t
777b0 68 65 20 6e 65 77 20 6c 61 72 67 65 72 20 63 68  he new larger ch
777c0 75 6e 6b 2e 20 20 49 6e 20 6f 72 64 65 72 20 66  unk.  In order f
777d0 6f 72 0a 2a 2a 20 74 68 69 73 20 6d 65 6d 2e 69  or.** this mem.i
777e0 4d 61 73 74 65 72 20 72 65 70 6c 61 63 65 6d 65  Master replaceme
777f0 6e 74 20 74 6f 20 77 6f 72 6b 2c 20 74 68 65 20  nt to work, the 
77800 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 6d 75 73  master chunk mus
77810 74 20 62 65 0a 2a 2a 20 6c 69 6e 6b 65 64 20 69  t be.** linked i
77820 6e 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 62  nto the hash tab
77830 6c 65 73 2e 20 20 54 68 61 74 20 69 73 20 6e 6f  les.  That is no
77840 74 20 74 68 65 20 6e 6f 72 6d 61 6c 20 73 74 61  t the normal sta
77850 74 65 20 6f 66 0a 2a 2a 20 61 66 66 61 69 72 73  te of.** affairs
77860 2c 20 6f 66 20 63 6f 75 72 73 65 2e 20 20 54 68  , of course.  Th
77870 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e  e calling routin
77880 65 20 6d 75 73 74 20 6c 69 6e 6b 20 74 68 65 20  e must link the 
77890 6d 61 73 74 65 72 0a 2a 2a 20 63 68 75 6e 6b 20  master.** chunk 
778a0 62 65 66 6f 72 65 20 69 6e 76 6f 6b 69 6e 67 20  before invoking 
778b0 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 74 68  this routine, th
778c0 65 6e 20 6d 75 73 74 20 75 6e 6c 69 6e 6b 20 74  en must unlink t
778d0 68 65 20 28 70 6f 73 73 69 62 6c 79 0a 2a 2a 20  he (possibly.** 
778e0 63 68 61 6e 67 65 64 29 20 6d 61 73 74 65 72 20  changed) master 
778f0 63 68 75 6e 6b 20 6f 6e 63 65 20 74 68 69 73 20  chunk once this 
77900 72 6f 75 74 69 6e 65 20 68 61 73 20 66 69 6e 69  routine has fini
77910 73 68 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  shed..*/.static 
77920 76 6f 69 64 20 6d 65 6d 73 79 73 33 4d 65 72 67  void memsys3Merg
77930 65 28 75 33 32 20 2a 70 52 6f 6f 74 29 7b 0a 20  e(u32 *pRoot){. 
77940 20 75 33 32 20 69 4e 65 78 74 2c 20 70 72 65 76   u32 iNext, prev
77950 2c 20 73 69 7a 65 2c 20 69 2c 20 78 3b 0a 0a 20  , size, i, x;.. 
77960 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
77970 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 2e  _mutex_held(mem.
77980 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28  mutex) );.  for(
77990 69 3d 2a 70 52 6f 6f 74 3b 20 69 3e 30 3b 20 69  i=*pRoot; i>0; i
779a0 3d 69 4e 65 78 74 29 7b 0a 20 20 20 20 69 4e 65  =iNext){.    iNe
779b0 78 74 20 3d 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 69  xt = mem.aPool[i
779c0 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 3b 0a 20  ].u.list.next;. 
779d0 20 20 20 73 69 7a 65 20 3d 20 6d 65 6d 2e 61 50     size = mem.aP
779e0 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73  ool[i-1].u.hdr.s
779f0 69 7a 65 34 78 3b 0a 20 20 20 20 61 73 73 65 72  ize4x;.    asser
77a00 74 28 20 28 73 69 7a 65 26 31 29 3d 3d 30 20 29  t( (size&1)==0 )
77a10 3b 0a 20 20 20 20 69 66 28 20 28 73 69 7a 65 26  ;.    if( (size&
77a20 32 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d  2)==0 ){.      m
77a30 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d  emsys3UnlinkFrom
77a40 4c 69 73 74 28 69 2c 20 70 52 6f 6f 74 29 3b 0a  List(i, pRoot);.
77a50 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 20        assert( i 
77a60 3e 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 69 2d 31 5d  > mem.aPool[i-1]
77a70 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20  .u.hdr.prevSize 
77a80 29 3b 0a 20 20 20 20 20 20 70 72 65 76 20 3d 20  );.      prev = 
77a90 69 20 2d 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 69 2d  i - mem.aPool[i-
77aa0 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a  1].u.hdr.prevSiz
77ab0 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 72 65  e;.      if( pre
77ac0 76 3d 3d 69 4e 65 78 74 20 29 7b 0a 20 20 20 20  v==iNext ){.    
77ad0 20 20 20 20 69 4e 65 78 74 20 3d 20 6d 65 6d 2e      iNext = mem.
77ae0 61 50 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69  aPool[prev].u.li
77af0 73 74 2e 6e 65 78 74 3b 0a 20 20 20 20 20 20 7d  st.next;.      }
77b00 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e  .      memsys3Un
77b10 6c 69 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20  link(prev);.    
77b20 20 20 73 69 7a 65 20 3d 20 69 20 2b 20 73 69 7a    size = i + siz
77b30 65 2f 34 20 2d 20 70 72 65 76 3b 0a 20 20 20 20  e/4 - prev;.    
77b40 20 20 78 20 3d 20 6d 65 6d 2e 61 50 6f 6f 6c 5b    x = mem.aPool[
77b50 70 72 65 76 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  prev-1].u.hdr.si
77b60 7a 65 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20  ze4x & 2;.      
77b70 6d 65 6d 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d 31  mem.aPool[prev-1
77b80 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d  ].u.hdr.size4x =
77b90 20 73 69 7a 65 2a 34 20 7c 20 78 3b 0a 20 20 20   size*4 | x;.   
77ba0 20 20 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 70 72 65     mem.aPool[pre
77bb0 76 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e  v+size-1].u.hdr.
77bc0 70 72 65 76 53 69 7a 65 20 3d 20 73 69 7a 65 3b  prevSize = size;
77bd0 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 4c 69  .      memsys3Li
77be0 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20 20  nk(prev);.      
77bf0 69 20 3d 20 70 72 65 76 3b 0a 20 20 20 20 7d 65  i = prev;.    }e
77c00 6c 73 65 7b 0a 20 20 20 20 20 20 73 69 7a 65 20  lse{.      size 
77c10 2f 3d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20  /= 4;.    }.    
77c20 69 66 28 20 73 69 7a 65 3e 6d 65 6d 2e 73 7a 4d  if( size>mem.szM
77c30 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d  aster ){.      m
77c40 65 6d 2e 69 4d 61 73 74 65 72 20 3d 20 69 3b 0a  em.iMaster = i;.
77c50 20 20 20 20 20 20 6d 65 6d 2e 73 7a 4d 61 73 74        mem.szMast
77c60 65 72 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 7d  er = size;.    }
77c70 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
77c80 74 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 20  turn a block of 
77c90 6d 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61  memory of at lea
77ca0 73 74 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a  st nBytes in siz
77cb0 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c  e..** Return NUL
77cc0 4c 20 69 66 20 75 6e 61 62 6c 65 2e 0a 2a 2f 0a  L if unable..*/.
77cd0 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d  static void *mem
77ce0 73 79 73 33 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e  sys3Malloc(int n
77cf0 42 79 74 65 29 7b 0a 20 20 75 33 32 20 69 3b 0a  Byte){.  u32 i;.
77d00 20 20 69 6e 74 20 6e 42 6c 6f 63 6b 3b 0a 20 20    int nBlock;.  
77d10 69 6e 74 20 74 6f 46 72 65 65 3b 0a 0a 20 20 61  int toFree;..  a
77d20 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
77d30 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 2e 6d 75  utex_held(mem.mu
77d40 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
77d50 28 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f  ( sizeof(Mem3Blo
77d60 63 6b 29 3d 3d 38 20 29 3b 0a 20 20 69 66 28 20  ck)==8 );.  if( 
77d70 6e 42 79 74 65 3c 3d 31 32 20 29 7b 0a 20 20 20  nByte<=12 ){.   
77d80 20 6e 42 6c 6f 63 6b 20 3d 20 32 3b 0a 20 20 7d   nBlock = 2;.  }
77d90 65 6c 73 65 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b  else{.    nBlock
77da0 20 3d 20 28 6e 42 79 74 65 20 2b 20 31 31 29 2f   = (nByte + 11)/
77db0 38 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  8;.  }.  assert(
77dc0 20 6e 42 6c 6f 63 6b 20 3e 3d 20 32 20 29 3b 0a   nBlock >= 2 );.
77dd0 0a 20 20 2f 2a 20 53 54 45 50 20 31 3a 0a 20 20  .  /* STEP 1:.  
77de0 2a 2a 20 4c 6f 6f 6b 20 66 6f 72 20 61 6e 20 65  ** Look for an e
77df0 6e 74 72 79 20 6f 66 20 74 68 65 20 63 6f 72 72  ntry of the corr
77e00 65 63 74 20 73 69 7a 65 20 69 6e 20 65 69 74 68  ect size in eith
77e10 65 72 20 74 68 65 20 73 6d 61 6c 6c 0a 20 20 2a  er the small.  *
77e20 2a 20 63 68 75 6e 6b 20 74 61 62 6c 65 20 6f 72  * chunk table or
77e30 20 69 6e 20 74 68 65 20 6c 61 72 67 65 20 63 68   in the large ch
77e40 75 6e 6b 20 68 61 73 68 20 74 61 62 6c 65 2e 20  unk hash table. 
77e50 20 54 68 69 73 20 69 73 0a 20 20 2a 2a 20 73 75   This is.  ** su
77e60 63 63 65 73 73 66 75 6c 20 6d 6f 73 74 20 6f 66  ccessful most of
77e70 20 74 68 65 20 74 69 6d 65 20 28 61 62 6f 75 74   the time (about
77e80 20 39 20 74 69 6d 65 73 20 6f 75 74 20 6f 66 20   9 times out of 
77e90 31 30 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  10)..  */.  if( 
77ea0 6e 42 6c 6f 63 6b 20 3c 3d 20 4d 58 5f 53 4d 41  nBlock <= MX_SMA
77eb0 4c 4c 20 29 7b 0a 20 20 20 20 69 20 3d 20 6d 65  LL ){.    i = me
77ec0 6d 2e 61 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b  m.aiSmall[nBlock
77ed0 2d 32 5d 3b 0a 20 20 20 20 69 66 28 20 69 3e 30  -2];.    if( i>0
77ee0 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73   ){.      memsys
77ef0 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28  3UnlinkFromList(
77f00 69 2c 20 26 6d 65 6d 2e 61 69 53 6d 61 6c 6c 5b  i, &mem.aiSmall[
77f10 6e 42 6c 6f 63 6b 2d 32 5d 29 3b 0a 20 20 20 20  nBlock-2]);.    
77f20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33    return memsys3
77f30 43 68 65 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f  Checkout(i, nBlo
77f40 63 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  ck);.    }.  }el
77f50 73 65 7b 0a 20 20 20 20 69 6e 74 20 68 61 73 68  se{.    int hash
77f60 20 3d 20 6e 42 6c 6f 63 6b 20 25 20 4e 5f 48 41   = nBlock % N_HA
77f70 53 48 3b 0a 20 20 20 20 66 6f 72 28 69 3d 6d 65  SH;.    for(i=me
77f80 6d 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 3b 20  m.aiHash[hash]; 
77f90 69 3e 30 3b 20 69 3d 6d 65 6d 2e 61 50 6f 6f 6c  i>0; i=mem.aPool
77fa0 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29  [i].u.list.next)
77fb0 7b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d 2e  {.      if( mem.
77fc0 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72  aPool[i-1].u.hdr
77fd0 2e 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63  .size4x/4==nBloc
77fe0 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d  k ){.        mem
77ff0 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69  sys3UnlinkFromLi
78000 73 74 28 69 2c 20 26 6d 65 6d 2e 61 69 48 61 73  st(i, &mem.aiHas
78010 68 5b 68 61 73 68 5d 29 3b 0a 20 20 20 20 20 20  h[hash]);.      
78020 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33    return memsys3
78030 43 68 65 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f  Checkout(i, nBlo
78040 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ck);.      }.   
78050 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 54 45   }.  }..  /* STE
78060 50 20 32 3a 0a 20 20 2a 2a 20 54 72 79 20 74 6f  P 2:.  ** Try to
78070 20 73 61 74 69 73 66 79 20 74 68 65 20 61 6c 6c   satisfy the all
78080 6f 63 61 74 69 6f 6e 20 62 79 20 63 61 72 76 69  ocation by carvi
78090 6e 67 20 61 20 70 69 65 63 65 20 6f 66 66 20 6f  ng a piece off o
780a0 66 20 74 68 65 20 65 6e 64 0a 20 20 2a 2a 20 6f  f the end.  ** o
780b0 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 68 75  f the master chu
780c0 6e 6b 2e 20 20 54 68 69 73 20 73 74 65 70 20 75  nk.  This step u
780d0 73 75 61 6c 6c 79 20 77 6f 72 6b 73 20 69 66 20  sually works if 
780e0 73 74 65 70 20 31 20 66 61 69 6c 73 2e 0a 20 20  step 1 fails..  
780f0 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 2e 73 7a 4d  */.  if( mem.szM
78100 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b  aster>=nBlock ){
78110 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73  .    return mems
78120 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 6e 42  ys3FromMaster(nB
78130 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f  lock);.  }...  /
78140 2a 20 53 54 45 50 20 33 3a 20 20 0a 20 20 2a 2a  * STEP 3:  .  **
78150 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68   Loop through th
78160 65 20 65 6e 74 69 72 65 20 6d 65 6d 6f 72 79 20  e entire memory 
78170 70 6f 6f 6c 2e 20 20 43 6f 61 6c 65 73 63 65 20  pool.  Coalesce 
78180 61 64 6a 61 63 65 6e 74 20 66 72 65 65 0a 20 20  adjacent free.  
78190 2a 2a 20 63 68 75 6e 6b 73 2e 20 20 52 65 63 6f  ** chunks.  Reco
781a0 6d 70 75 74 65 20 74 68 65 20 6d 61 73 74 65 72  mpute the master
781b0 20 63 68 75 6e 6b 20 61 73 20 74 68 65 20 6c 61   chunk as the la
781c0 72 67 65 73 74 20 66 72 65 65 20 63 68 75 6e 6b  rgest free chunk
781d0 2e 0a 20 20 2a 2a 20 54 68 65 6e 20 74 72 79 20  ..  ** Then try 
781e0 61 67 61 69 6e 20 74 6f 20 73 61 74 69 73 66 79  again to satisfy
781f0 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   the allocation 
78200 62 79 20 63 61 72 76 69 6e 67 20 61 20 70 69 65  by carving a pie
78210 63 65 20 6f 66 66 0a 20 20 2a 2a 20 6f 66 20 74  ce off.  ** of t
78220 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61  he end of the ma
78230 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 54 68 69  ster chunk.  Thi
78240 73 20 73 74 65 70 20 68 61 70 70 65 6e 73 20 76  s step happens v
78250 65 72 79 0a 20 20 2a 2a 20 72 61 72 65 6c 79 20  ery.  ** rarely 
78260 28 77 65 20 68 6f 70 65 21 29 0a 20 20 2a 2f 0a  (we hope!).  */.
78270 20 20 66 6f 72 28 74 6f 46 72 65 65 3d 6e 42 6c    for(toFree=nBl
78280 6f 63 6b 2a 31 36 3b 20 74 6f 46 72 65 65 3c 53  ock*16; toFree<S
78290 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a  QLITE_MEMORY_SIZ
782a0 45 2a 32 3b 20 74 6f 46 72 65 65 20 2a 3d 20 32  E*2; toFree *= 2
782b0 29 7b 0a 20 20 20 20 6d 65 6d 73 79 73 33 4f 75  ){.    memsys3Ou
782c0 74 4f 66 4d 65 6d 6f 72 79 28 74 6f 46 72 65 65  tOfMemory(toFree
782d0 29 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 69  );.    if( mem.i
782e0 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20  Master ){.      
782f0 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 6d 65 6d 2e  memsys3Link(mem.
78300 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20  iMaster);.      
78310 6d 65 6d 2e 69 4d 61 73 74 65 72 20 3d 20 30 3b  mem.iMaster = 0;
78320 0a 20 20 20 20 20 20 6d 65 6d 2e 73 7a 4d 61 73  .      mem.szMas
78330 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  ter = 0;.    }. 
78340 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f     for(i=0; i<N_
78350 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  HASH; i++){.    
78360 20 20 6d 65 6d 73 79 73 33 4d 65 72 67 65 28 26    memsys3Merge(&
78370 6d 65 6d 2e 61 69 48 61 73 68 5b 69 5d 29 3b 0a  mem.aiHash[i]);.
78380 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d      }.    for(i=
78390 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b  0; i<MX_SMALL-1;
783a0 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6d 65 6d   i++){.      mem
783b0 73 79 73 33 4d 65 72 67 65 28 26 6d 65 6d 2e 61  sys3Merge(&mem.a
783c0 69 53 6d 61 6c 6c 5b 69 5d 29 3b 0a 20 20 20 20  iSmall[i]);.    
783d0 7d 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 73 7a  }.    if( mem.sz
783e0 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20  Master ){.      
783f0 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65  memsys3Unlink(me
78400 6d 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  m.iMaster);.    
78410 20 20 69 66 28 20 6d 65 6d 2e 73 7a 4d 61 73 74    if( mem.szMast
78420 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b 0a 20 20  er>=nBlock ){.  
78430 20 20 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d        return mem
78440 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 6e  sys3FromMaster(n
78450 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a  Block);.      }.
78460 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
78470 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 61  If none of the a
78480 62 6f 76 65 20 77 6f 72 6b 65 64 2c 20 74 68 65  bove worked, the
78490 6e 20 77 65 20 66 61 69 6c 2e 20 2a 2f 0a 20 20  n we fail. */.  
784a0 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
784b0 2a 2a 20 46 72 65 65 20 61 6e 20 6f 75 74 73 74  ** Free an outst
784c0 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c  anding memory al
784d0 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69  location..*/.voi
784e0 64 20 6d 65 6d 73 79 73 33 46 72 65 65 28 76 6f  d memsys3Free(vo
784f0 69 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 4d 65 6d  id *pOld){.  Mem
78500 33 42 6c 6f 63 6b 20 2a 70 20 3d 20 28 4d 65 6d  3Block *p = (Mem
78510 33 42 6c 6f 63 6b 2a 29 70 4f 6c 64 3b 0a 20 20  3Block*)pOld;.  
78520 69 6e 74 20 69 3b 0a 20 20 75 33 32 20 73 69 7a  int i;.  u32 siz
78530 65 2c 20 78 3b 0a 20 20 61 73 73 65 72 74 28 20  e, x;.  assert( 
78540 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
78550 6c 64 28 6d 65 6d 2e 6d 75 74 65 78 29 20 29 3b  ld(mem.mutex) );
78560 0a 20 20 61 73 73 65 72 74 28 20 70 3e 6d 65 6d  .  assert( p>mem
78570 2e 61 50 6f 6f 6c 20 26 26 20 70 3c 26 6d 65 6d  .aPool && p<&mem
78580 2e 61 50 6f 6f 6c 5b 53 51 4c 49 54 45 5f 4d 45  .aPool[SQLITE_ME
78590 4d 4f 52 59 5f 53 49 5a 45 2f 38 5d 20 29 3b 0a  MORY_SIZE/8] );.
785a0 20 20 69 20 3d 20 70 20 2d 20 6d 65 6d 2e 61 50    i = p - mem.aP
785b0 6f 6f 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 28  ool;.  assert( (
785c0 6d 65 6d 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75  mem.aPool[i-1].u
785d0 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 29 3d 3d  .hdr.size4x&1)==
785e0 31 20 29 3b 0a 20 20 73 69 7a 65 20 3d 20 6d 65  1 );.  size = me
785f0 6d 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68  m.aPool[i-1].u.h
78600 64 72 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61  dr.size4x/4;.  a
78610 73 73 65 72 74 28 20 69 2b 73 69 7a 65 3c 3d 53  ssert( i+size<=S
78620 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a  QLITE_MEMORY_SIZ
78630 45 2f 38 2b 31 20 29 3b 0a 20 20 6d 65 6d 2e 61  E/8+1 );.  mem.a
78640 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e  Pool[i-1].u.hdr.
78650 73 69 7a 65 34 78 20 26 3d 20 7e 31 3b 0a 20 20  size4x &= ~1;.  
78660 6d 65 6d 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65  mem.aPool[i+size
78670 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69  -1].u.hdr.prevSi
78680 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 6d 65 6d  ze = size;.  mem
78690 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d  .aPool[i+size-1]
786a0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 3d  .u.hdr.size4x &=
786b0 20 7e 32 3b 0a 20 20 6d 65 6d 73 79 73 33 4c 69   ~2;.  memsys3Li
786c0 6e 6b 28 69 29 3b 0a 0a 20 20 2f 2a 20 54 72 79  nk(i);..  /* Try
786d0 20 74 6f 20 65 78 70 61 6e 64 20 74 68 65 20 6d   to expand the m
786e0 61 73 74 65 72 20 75 73 69 6e 67 20 74 68 65 20  aster using the 
786f0 6e 65 77 6c 79 20 66 72 65 65 64 20 63 68 75 6e  newly freed chun
78700 6b 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 2e 69  k */.  if( mem.i
78710 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 77 68  Master ){.    wh
78720 69 6c 65 28 20 28 6d 65 6d 2e 61 50 6f 6f 6c 5b  ile( (mem.aPool[
78730 6d 65 6d 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75  mem.iMaster-1].u
78740 2e 68 64 72 2e 73 69 7a 65 34 78 26 32 29 3d 3d  .hdr.size4x&2)==
78750 30 20 29 7b 0a 20 20 20 20 20 20 73 69 7a 65 20  0 ){.      size 
78760 3d 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 6d 65 6d 2e  = mem.aPool[mem.
78770 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72  iMaster-1].u.hdr
78780 2e 70 72 65 76 53 69 7a 65 3b 0a 20 20 20 20 20  .prevSize;.     
78790 20 6d 65 6d 2e 69 4d 61 73 74 65 72 20 2d 3d 20   mem.iMaster -= 
787a0 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 2e  size;.      mem.
787b0 73 7a 4d 61 73 74 65 72 20 2b 3d 20 73 69 7a 65  szMaster += size
787c0 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55  ;.      memsys3U
787d0 6e 6c 69 6e 6b 28 6d 65 6d 2e 69 4d 61 73 74 65  nlink(mem.iMaste
787e0 72 29 3b 0a 20 20 20 20 20 20 78 20 3d 20 6d 65  r);.      x = me
787f0 6d 2e 61 50 6f 6f 6c 5b 6d 65 6d 2e 69 4d 61 73  m.aPool[mem.iMas
78800 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  ter-1].u.hdr.siz
78810 65 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d  e4x & 2;.      m
78820 65 6d 2e 61 50 6f 6f 6c 5b 6d 65 6d 2e 69 4d 61  em.aPool[mem.iMa
78830 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ster-1].u.hdr.si
78840 7a 65 34 78 20 3d 20 6d 65 6d 2e 73 7a 4d 61 73  ze4x = mem.szMas
78850 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20  ter*4 | x;.     
78860 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 6d 65 6d 2e 69   mem.aPool[mem.i
78870 4d 61 73 74 65 72 2b 6d 65 6d 2e 73 7a 4d 61 73  Master+mem.szMas
78880 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65  ter-1].u.hdr.pre
78890 76 53 69 7a 65 20 3d 20 6d 65 6d 2e 73 7a 4d 61  vSize = mem.szMa
788a0 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ster;.    }.    
788b0 78 20 3d 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 6d 65  x = mem.aPool[me
788c0 6d 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68  m.iMaster-1].u.h
788d0 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a 20  dr.size4x & 2;. 
788e0 20 20 20 77 68 69 6c 65 28 20 28 6d 65 6d 2e 61     while( (mem.a
788f0 50 6f 6f 6c 5b 6d 65 6d 2e 69 4d 61 73 74 65 72  Pool[mem.iMaster
78900 2b 6d 65 6d 2e 73 7a 4d 61 73 74 65 72 2d 31 5d  +mem.szMaster-1]
78910 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 29  .u.hdr.size4x&1)
78920 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  ==0 ){.      mem
78930 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d 2e 69  sys3Unlink(mem.i
78940 4d 61 73 74 65 72 2b 6d 65 6d 2e 73 7a 4d 61 73  Master+mem.szMas
78950 74 65 72 29 3b 0a 20 20 20 20 20 20 6d 65 6d 2e  ter);.      mem.
78960 73 7a 4d 61 73 74 65 72 20 2b 3d 20 6d 65 6d 2e  szMaster += mem.
78970 61 50 6f 6f 6c 5b 6d 65 6d 2e 69 4d 61 73 74 65  aPool[mem.iMaste
78980 72 2b 6d 65 6d 2e 73 7a 4d 61 73 74 65 72 2d 31  r+mem.szMaster-1
78990 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34  ].u.hdr.size4x/4
789a0 3b 0a 20 20 20 20 20 20 6d 65 6d 2e 61 50 6f 6f  ;.      mem.aPoo
789b0 6c 5b 6d 65 6d 2e 69 4d 61 73 74 65 72 2d 31 5d  l[mem.iMaster-1]
789c0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20  .u.hdr.size4x = 
789d0 6d 65 6d 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c  mem.szMaster*4 |
789e0 20 78 3b 0a 20 20 20 20 20 20 6d 65 6d 2e 61 50   x;.      mem.aP
789f0 6f 6f 6c 5b 6d 65 6d 2e 69 4d 61 73 74 65 72 2b  ool[mem.iMaster+
78a00 6d 65 6d 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e  mem.szMaster-1].
78a10 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d  u.hdr.prevSize =
78a20 20 6d 65 6d 2e 73 7a 4d 61 73 74 65 72 3b 0a 20   mem.szMaster;. 
78a30 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
78a40 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65  * Allocate nByte
78a50 73 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2f 0a 53  s of memory.*/.S
78a60 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
78a70 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69  sqlite3_malloc(i
78a80 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 73 71  nt nBytes){.  sq
78a90 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d  lite3_int64 *p =
78aa0 20 30 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73   0;.  if( nBytes
78ab0 3e 30 20 29 7b 0a 20 20 20 20 6d 65 6d 73 79 73  >0 ){.    memsys
78ac0 33 45 6e 74 65 72 28 29 3b 0a 20 20 20 20 70 20  3Enter();.    p 
78ad0 3d 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 28  = memsys3Malloc(
78ae0 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 73 71 6c  nBytes);.    sql
78af0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
78b00 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 7d  (mem.mutex);.  }
78b10 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a  .  return (void*
78b20 29 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72  )p; .}../*.** Fr
78b30 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 53 51  ee memory..*/.SQ
78b40 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
78b50 6c 69 74 65 33 5f 66 72 65 65 28 76 6f 69 64 20  lite3_free(void 
78b60 2a 70 50 72 69 6f 72 29 7b 0a 20 20 69 66 28 20  *pPrior){.  if( 
78b70 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20  pPrior==0 ){.   
78b80 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61   return;.  }.  a
78b90 73 73 65 72 74 28 20 6d 65 6d 2e 6d 75 74 65 78  ssert( mem.mutex
78ba0 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  !=0 );.  sqlite3
78bb0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d  _mutex_enter(mem
78bc0 2e 6d 75 74 65 78 29 3b 0a 20 20 6d 65 6d 73 79  .mutex);.  memsy
78bd0 73 33 46 72 65 65 28 70 50 72 69 6f 72 29 3b 0a  s3Free(pPrior);.
78be0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
78bf0 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29  leave(mem.mutex)
78c00 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61  ;  .}../*.** Cha
78c10 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20  nge the size of 
78c20 61 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f  an existing memo
78c30 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f  ry allocation.*/
78c40 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
78c50 20 2a 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f   *sqlite3_reallo
78c60 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20  c(void *pPrior, 
78c70 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 69  int nBytes){.  i
78c80 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 6f 69 64 20  nt nOld;.  void 
78c90 2a 70 3b 0a 20 20 69 66 28 20 70 50 72 69 6f 72  *p;.  if( pPrior
78ca0 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
78cb0 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  n sqlite3_malloc
78cc0 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a 20 20  (nBytes);.  }.  
78cd0 69 66 28 20 6e 42 79 74 65 73 3c 3d 30 20 29 7b  if( nBytes<=0 ){
78ce0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
78cf0 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 20 20 72  e(pPrior);.    r
78d00 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61  eturn 0;.  }.  a
78d10 73 73 65 72 74 28 20 6d 65 6d 2e 6d 75 74 65 78  ssert( mem.mutex
78d20 21 3d 30 20 29 3b 0a 20 20 6e 4f 6c 64 20 3d 20  !=0 );.  nOld = 
78d30 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a  sqlite3MallocSiz
78d40 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 69 66 28  e(pPrior);.  if(
78d50 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64 20 26 26   nBytes<=nOld &&
78d60 20 6e 42 79 74 65 73 3e 3d 6e 4f 6c 64 2d 31 32   nBytes>=nOld-12
78d70 38 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  8 ){.    return 
78d80 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 73 71  pPrior;.  }.  sq
78d90 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
78da0 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20  r(mem.mutex);.  
78db0 70 20 3d 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f  p = memsys3Mallo
78dc0 63 28 6e 42 79 74 65 73 29 3b 0a 20 20 69 66 28  c(nBytes);.  if(
78dd0 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 4f   p ){.    if( nO
78de0 6c 64 3c 6e 42 79 74 65 73 20 29 7b 0a 20 20 20  ld<nBytes ){.   
78df0 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 50 72     memcpy(p, pPr
78e00 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20 20 20  ior, nOld);.    
78e10 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d  }else{.      mem
78e20 63 70 79 28 70 2c 20 70 50 72 69 6f 72 2c 20 6e  cpy(p, pPrior, n
78e30 42 79 74 65 73 29 3b 0a 20 20 20 20 7d 0a 20 20  Bytes);.    }.  
78e40 20 20 6d 65 6d 73 79 73 33 46 72 65 65 28 70 50    memsys3Free(pP
78e50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  rior);.  }.  sql
78e60 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
78e70 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 72  (mem.mutex);.  r
78e80 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
78e90 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20  * Open the file 
78ea0 69 6e 64 69 63 61 74 65 64 20 61 6e 64 20 77 72  indicated and wr
78eb0 69 74 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c  ite a log of all
78ec0 20 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20   unfreed memory 
78ed0 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20  .** allocations 
78ee0 69 6e 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a  into that log..*
78ef0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
78f00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d   void sqlite3Mem
78f10 64 65 62 75 67 44 75 6d 70 28 63 6f 6e 73 74 20  debugDump(const 
78f20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29  char *zFilename)
78f30 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  {.#ifdef SQLITE_
78f40 44 45 42 55 47 0a 20 20 46 49 4c 45 20 2a 6f 75  DEBUG.  FILE *ou
78f50 74 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20  t;.  int i, j;. 
78f60 20 75 33 32 20 73 69 7a 65 3b 0a 20 20 69 66 28   u32 size;.  if(
78f70 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c   zFilename==0 ||
78f80 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 30   zFilename[0]==0
78f90 20 29 7b 0a 20 20 20 20 6f 75 74 20 3d 20 73 74   ){.    out = st
78fa0 64 6f 75 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  dout;.  }else{. 
78fb0 20 20 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a     out = fopen(z
78fc0 46 69 6c 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a  Filename, "w");.
78fd0 20 20 20 20 69 66 28 20 6f 75 74 3d 3d 30 20 29      if( out==0 )
78fe0 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28  {.      fprintf(
78ff0 73 74 64 65 72 72 2c 20 22 2a 2a 20 55 6e 61 62  stderr, "** Unab
79000 6c 65 20 74 6f 20 6f 75 74 70 75 74 20 6d 65 6d  le to output mem
79010 6f 72 79 20 64 65 62 75 67 20 6f 75 74 70 75 74  ory debug output
79020 20 6c 6f 67 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0a   log: %s **\n",.
79030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
79040 20 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29        zFilename)
79050 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
79060 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73      }.  }.  mems
79070 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 66 70  ys3Enter();.  fp
79080 72 69 6e 74 66 28 6f 75 74 2c 20 22 43 48 55 4e  rintf(out, "CHUN
79090 4b 53 3a 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69  KS:\n");.  for(i
790a0 3d 31 3b 20 69 3c 3d 53 51 4c 49 54 45 5f 4d 45  =1; i<=SQLITE_ME
790b0 4d 4f 52 59 5f 53 49 5a 45 2f 38 3b 20 69 2b 3d  MORY_SIZE/8; i+=
790c0 73 69 7a 65 2f 34 29 7b 0a 20 20 20 20 73 69 7a  size/4){.    siz
790d0 65 20 3d 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 69 2d  e = mem.aPool[i-
790e0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b  1].u.hdr.size4x;
790f0 0a 20 20 20 20 69 66 28 20 73 69 7a 65 2f 34 3c  .    if( size/4<
79100 3d 31 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69  =1 ){.      fpri
79110 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 73 69 7a  ntf(out, "%p siz
79120 65 20 65 72 72 6f 72 5c 6e 22 2c 20 26 6d 65 6d  e error\n", &mem
79130 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20  .aPool[i]);.    
79140 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20    assert( 0 );. 
79150 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
79160 7d 0a 20 20 20 20 69 66 28 20 28 73 69 7a 65 26  }.    if( (size&
79170 31 29 3d 3d 30 20 26 26 20 6d 65 6d 2e 61 50 6f  1)==0 && mem.aPo
79180 6f 6c 5b 69 2b 73 69 7a 65 2f 34 2d 31 5d 2e 75  ol[i+size/4-1].u
79190 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 21 3d 73  .hdr.prevSize!=s
791a0 69 7a 65 2f 34 20 29 7b 0a 20 20 20 20 20 20 66  ize/4 ){.      f
791b0 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20  printf(out, "%p 
791c0 74 61 69 6c 20 73 69 7a 65 20 64 6f 65 73 20 6e  tail size does n
791d0 6f 74 20 6d 61 74 63 68 5c 6e 22 2c 20 26 6d 65  ot match\n", &me
791e0 6d 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20  m.aPool[i]);.   
791f0 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a     assert( 0 );.
79200 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
79210 20 7d 0a 20 20 20 20 69 66 28 20 28 28 6d 65 6d   }.    if( ((mem
79220 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f 34 2d  .aPool[i+size/4-
79230 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26  1].u.hdr.size4x&
79240 32 29 3e 3e 31 29 21 3d 28 73 69 7a 65 26 31 29  2)>>1)!=(size&1)
79250 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74   ){.      fprint
79260 66 28 6f 75 74 2c 20 22 25 70 20 74 61 69 6c 20  f(out, "%p tail 
79270 63 68 65 63 6b 6f 75 74 20 62 69 74 20 69 73 20  checkout bit is 
79280 69 6e 63 6f 72 72 65 63 74 5c 6e 22 2c 20 26 6d  incorrect\n", &m
79290 65 6d 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20  em.aPool[i]);.  
792a0 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b      assert( 0 );
792b0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
792c0 20 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65    }.    if( size
792d0 26 31 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69  &1 ){.      fpri
792e0 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 25 36 64  ntf(out, "%p %6d
792f0 20 62 79 74 65 73 20 63 68 65 63 6b 65 64 20 6f   bytes checked o
79300 75 74 5c 6e 22 2c 20 26 6d 65 6d 2e 61 50 6f 6f  ut\n", &mem.aPoo
79310 6c 5b 69 5d 2c 20 28 73 69 7a 65 2f 34 29 2a 38  l[i], (size/4)*8
79320 2d 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  -8);.    }else{.
79330 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75        fprintf(ou
79340 74 2c 20 22 25 70 20 25 36 64 20 62 79 74 65 73  t, "%p %6d bytes
79350 20 66 72 65 65 25 73 5c 6e 22 2c 20 26 6d 65 6d   free%s\n", &mem
79360 2e 61 50 6f 6f 6c 5b 69 5d 2c 20 28 73 69 7a 65  .aPool[i], (size
79370 2f 34 29 2a 38 2d 38 2c 0a 20 20 20 20 20 20 20  /4)*8-8,.       
79380 20 20 20 20 20 20 20 20 20 20 20 69 3d 3d 6d 65             i==me
79390 6d 2e 69 4d 61 73 74 65 72 20 3f 20 22 20 2a 2a  m.iMaster ? " **
793a0 6d 61 73 74 65 72 2a 2a 22 20 3a 20 22 22 29 3b  master**" : "");
793b0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72  .    }.  }.  for
793c0 28 69 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c  (i=0; i<MX_SMALL
793d0 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  -1; i++){.    if
793e0 28 20 6d 65 6d 2e 61 69 53 6d 61 6c 6c 5b 69 5d  ( mem.aiSmall[i]
793f0 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==0 ) continue;.
79400 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c      fprintf(out,
79410 20 22 73 6d 61 6c 6c 28 25 32 64 29 3a 22 2c 20   "small(%2d):", 
79420 69 29 3b 0a 20 20 20 20 66 6f 72 28 6a 20 3d 20  i);.    for(j = 
79430 6d 65 6d 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3b 20  mem.aiSmall[i]; 
79440 6a 3e 30 3b 20 6a 3d 6d 65 6d 2e 61 50 6f 6f 6c  j>0; j=mem.aPool
79450 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29  [j].u.list.next)
79460 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28  {.      fprintf(
79470 6f 75 74 2c 20 22 20 25 70 28 25 64 29 22 2c 20  out, " %p(%d)", 
79480 26 6d 65 6d 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a 20  &mem.aPool[j],. 
79490 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6d 65               (me
794a0 6d 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75 2e 68  m.aPool[j-1].u.h
794b0 64 72 2e 73 69 7a 65 34 78 2f 34 29 2a 38 2d 38  dr.size4x/4)*8-8
794c0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 70 72  );.    }.    fpr
794d0 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b  intf(out, "\n");
794e0 20 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b   .  }.  for(i=0;
794f0 20 69 3c 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b   i<N_HASH; i++){
79500 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 61 69 48  .    if( mem.aiH
79510 61 73 68 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74  ash[i]==0 ) cont
79520 69 6e 75 65 3b 0a 20 20 20 20 66 70 72 69 6e 74  inue;.    fprint
79530 66 28 6f 75 74 2c 20 22 68 61 73 68 28 25 32 64  f(out, "hash(%2d
79540 29 3a 22 2c 20 69 29 3b 0a 20 20 20 20 66 6f 72  ):", i);.    for
79550 28 6a 20 3d 20 6d 65 6d 2e 61 69 48 61 73 68 5b  (j = mem.aiHash[
79560 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65 6d 2e 61  i]; j>0; j=mem.a
79570 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e  Pool[j].u.list.n
79580 65 78 74 29 7b 0a 20 20 20 20 20 20 66 70 72 69  ext){.      fpri
79590 6e 74 66 28 6f 75 74 2c 20 22 20 25 70 28 25 64  ntf(out, " %p(%d
795a0 29 22 2c 20 26 6d 65 6d 2e 61 50 6f 6f 6c 5b 6a  )", &mem.aPool[j
795b0 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ],.             
795c0 20 28 6d 65 6d 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d   (mem.aPool[j-1]
795d0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 29  .u.hdr.size4x/4)
795e0 2a 38 2d 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20  *8-8);.    }.   
795f0 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c   fprintf(out, "\
79600 6e 22 29 3b 20 0a 20 20 7d 0a 20 20 66 70 72 69  n"); .  }.  fpri
79610 6e 74 66 28 6f 75 74 2c 20 22 6d 61 73 74 65 72  ntf(out, "master
79620 3d 25 64 5c 6e 22 2c 20 6d 65 6d 2e 69 4d 61 73  =%d\n", mem.iMas
79630 74 65 72 29 3b 0a 20 20 66 70 72 69 6e 74 66 28  ter);.  fprintf(
79640 6f 75 74 2c 20 22 6e 6f 77 55 73 65 64 3d 25 64  out, "nowUsed=%d
79650 5c 6e 22 2c 20 53 51 4c 49 54 45 5f 4d 45 4d 4f  \n", SQLITE_MEMO
79660 52 59 5f 53 49 5a 45 20 2d 20 6d 65 6d 2e 73 7a  RY_SIZE - mem.sz
79670 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20 66 70 72  Master*8);.  fpr
79680 69 6e 74 66 28 6f 75 74 2c 20 22 6d 78 55 73 65  intf(out, "mxUse
79690 64 3d 25 64 5c 6e 22 2c 20 53 51 4c 49 54 45 5f  d=%d\n", SQLITE_
796a0 4d 45 4d 4f 52 59 5f 53 49 5a 45 20 2d 20 6d 65  MEMORY_SIZE - me
796b0 6d 2e 6d 6e 4d 61 73 74 65 72 2a 38 29 3b 0a 20  m.mnMaster*8);. 
796c0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
796d0 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29 3b  eave(mem.mutex);
796e0 0a 20 20 69 66 28 20 6f 75 74 3d 3d 73 74 64 6f  .  if( out==stdo
796f0 75 74 20 29 7b 0a 20 20 20 20 66 66 6c 75 73 68  ut ){.    fflush
79700 28 73 74 64 6f 75 74 29 3b 0a 20 20 7d 65 6c 73  (stdout);.  }els
79710 65 7b 0a 20 20 20 20 66 63 6c 6f 73 65 28 6f 75  e{.    fclose(ou
79720 74 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d  t);.  }.#endif.}
79730 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51  ...#endif /* !SQ
79740 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45  LITE_MEMORY_SIZE
79750 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
79760 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 33  **** End of mem3
79770 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
79780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
797a0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
797b0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
797c0 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mem5.c *********
797d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
797e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
797f0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20  ***/./*.** 2007 
79800 4f 63 74 6f 62 65 72 20 31 34 0a 2a 2a 0a 2a 2a  October 14.**.**
79810 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
79820 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
79830 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
79840 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
79850 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
79860 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
79870 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
79880 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
79890 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
798a0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
798b0 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
798c0 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
798d0 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
798e0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
798f0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
79900 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
79910 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
79920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79960 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
79970 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
79980 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20  the C functions 
79990 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 61  that implement a
799a0 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63   memory.** alloc
799b0 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20  ation subsystem 
799c0 66 6f 72 20 75 73 65 20 62 79 20 53 51 4c 69 74  for use by SQLit
799d0 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76  e. .**.** This v
799e0 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65  ersion of the me
799f0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
79a00 73 75 62 73 79 73 74 65 6d 20 6f 6d 69 74 73 20  subsystem omits 
79a10 61 6c 6c 0a 2a 2a 20 75 73 65 20 6f 66 20 6d 61  all.** use of ma
79a20 6c 6c 6f 63 28 29 2e 20 20 41 6c 6c 20 64 79 6e  lloc().  All dyn
79a30 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74  amically allocat
79a40 61 62 6c 65 20 6d 65 6d 6f 72 79 20 69 73 0a 2a  able memory is.*
79a50 2a 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 61  * contained in a
79a60 20 73 74 61 74 69 63 20 61 72 72 61 79 2c 20 6d   static array, m
79a70 65 6d 2e 61 50 6f 6f 6c 5b 5d 2e 20 20 54 68 65  em.aPool[].  The
79a80 20 73 69 7a 65 20 6f 66 20 74 68 69 73 0a 2a 2a   size of this.**
79a90 20 66 69 78 65 64 20 6d 65 6d 6f 72 79 20 70 6f   fixed memory po
79aa0 6f 6c 20 69 73 20 53 51 4c 49 54 45 5f 50 4f 57  ol is SQLITE_POW
79ab0 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20 62 79  2_MEMORY_SIZE by
79ac0 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  tes..**.** This 
79ad0 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d  version of the m
79ae0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
79af0 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 75 73   subsystem is us
79b00 65 64 20 69 66 0a 2a 2a 20 61 6e 64 20 6f 6e 6c  ed if.** and onl
79b10 79 20 69 66 20 53 51 4c 49 54 45 5f 50 4f 57 32  y if SQLITE_POW2
79b20 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20 69 73 20  _MEMORY_SIZE is 
79b30 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 24  defined..**.** $
79b40 49 64 3a 20 6d 65 6d 35 2e 63 2c 76 20 31 2e 34  Id: mem5.c,v 1.4
79b50 20 32 30 30 38 2f 30 32 2f 31 39 20 31 35 3a 31   2008/02/19 15:1
79b60 35 3a 31 36 20 64 72 68 20 45 78 70 20 24 0a 2a  5:16 drh Exp $.*
79b70 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65  /../*.** This ve
79b80 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d  rsion of the mem
79b90 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73  ory allocator is
79ba0 20 75 73 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20   used only when 
79bb0 0a 2a 2a 20 53 51 4c 49 54 45 5f 50 4f 57 32 5f  .** SQLITE_POW2_
79bc0 4d 45 4d 4f 52 59 5f 53 49 5a 45 20 69 73 20 64  MEMORY_SIZE is d
79bd0 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 64 65  efined..*/.#ifde
79be0 66 20 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d 45  f SQLITE_POW2_ME
79bf0 4d 4f 52 59 5f 53 49 5a 45 0a 0a 2f 2a 0a 2a 2a  MORY_SIZE../*.**
79c00 20 4c 6f 67 32 20 6f 66 20 74 68 65 20 6d 69 6e   Log2 of the min
79c10 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 6e 20  imum size of an 
79c20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 46 6f 72  allocation.  For
79c30 20 65 78 61 6d 70 6c 65 2c 20 69 66 0a 2a 2a 20   example, if.** 
79c40 34 20 74 68 65 6e 20 61 6c 6c 20 61 6c 6c 6f 63  4 then all alloc
79c50 61 74 69 6f 6e 73 20 77 69 6c 6c 20 62 65 20 72  ations will be r
79c60 6f 75 6e 64 65 64 20 75 70 20 74 6f 20 61 74 20  ounded up to at 
79c70 6c 65 61 73 74 20 31 36 20 62 79 74 65 73 2e 0a  least 16 bytes..
79c80 2a 2a 20 49 66 20 35 20 74 68 65 6e 20 61 6c 6c  ** If 5 then all
79c90 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 77 69 6c   allocations wil
79ca0 6c 20 62 65 20 72 6f 75 6e 64 65 64 20 75 70 20  l be rounded up 
79cb0 74 6f 20 61 74 20 6c 65 61 73 74 20 33 32 20 62  to at least 32 b
79cc0 79 74 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  ytes..*/.#ifndef
79cd0 20 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4c 4f 47   SQLITE_POW2_LOG
79ce0 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  MIN.# define SQL
79cf0 49 54 45 5f 50 4f 57 32 5f 4c 4f 47 4d 49 4e 20  ITE_POW2_LOGMIN 
79d00 36 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65  6.#endif.#define
79d10 20 50 4f 57 32 5f 4d 49 4e 20 28 31 3c 3c 53 51   POW2_MIN (1<<SQ
79d20 4c 49 54 45 5f 50 4f 57 32 5f 4c 4f 47 4d 49 4e  LITE_POW2_LOGMIN
79d30 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 67 32 20 6f 66  )../*.** Log2 of
79d40 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 73 69 7a   the maximum siz
79d50 65 20 6f 66 20 61 6e 20 61 6c 6c 6f 63 61 74 69  e of an allocati
79d60 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  on..*/.#ifndef S
79d70 51 4c 49 54 45 5f 50 4f 57 32 5f 4c 4f 47 4d 41  QLITE_POW2_LOGMA
79d80 58 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  X.# define SQLIT
79d90 45 5f 50 4f 57 32 5f 4c 4f 47 4d 41 58 20 31 38  E_POW2_LOGMAX 18
79da0 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20  .#endif.#define 
79db0 50 4f 57 32 5f 4d 41 58 20 28 28 28 75 6e 73 69  POW2_MAX (((unsi
79dc0 67 6e 65 64 20 69 6e 74 29 31 29 3c 3c 53 51 4c  gned int)1)<<SQL
79dd0 49 54 45 5f 50 4f 57 32 5f 4c 4f 47 4d 41 58 29  ITE_POW2_LOGMAX)
79de0 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f  ../*.** Number o
79df0 66 20 64 69 73 74 69 6e 63 74 20 61 6c 6c 6f 63  f distinct alloc
79e00 61 74 69 6f 6e 20 73 69 7a 65 73 2e 0a 2a 2f 0a  ation sizes..*/.
79e10 23 64 65 66 69 6e 65 20 4e 53 49 5a 45 20 28 53  #define NSIZE (S
79e20 51 4c 49 54 45 5f 50 4f 57 32 5f 4c 4f 47 4d 41  QLITE_POW2_LOGMA
79e30 58 20 2d 20 53 51 4c 49 54 45 5f 50 4f 57 32 5f  X - SQLITE_POW2_
79e40 4c 4f 47 4d 49 4e 20 2b 20 31 29 0a 0a 2f 2a 0a  LOGMIN + 1)../*.
79e50 2a 2a 20 41 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c  ** A minimum all
79e60 6f 63 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e  ocation is an in
79e70 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
79e80 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
79e90 65 2e 0a 2a 2a 20 4c 61 72 67 65 72 20 61 6c 6c  e..** Larger all
79ea0 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 61 6e 20  ocations are an 
79eb0 61 72 72 61 79 20 6f 66 20 74 68 65 73 65 20 73  array of these s
79ec0 74 72 75 63 74 75 72 65 73 20 77 68 65 72 65 20  tructures where 
79ed0 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74  the.** size of t
79ee0 68 65 20 61 72 72 61 79 20 69 73 20 61 20 70 6f  he array is a po
79ef0 77 65 72 20 6f 66 20 32 2e 0a 2a 2f 0a 74 79 70  wer of 2..*/.typ
79f00 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d 35  edef struct Mem5
79f10 42 6c 6f 63 6b 20 4d 65 6d 35 42 6c 6f 63 6b 3b  Block Mem5Block;
79f20 0a 73 74 72 75 63 74 20 4d 65 6d 35 42 6c 6f 63  .struct Mem5Bloc
79f30 6b 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20  k {.  union {.  
79f40 20 20 63 68 61 72 20 61 44 61 74 61 5b 50 4f 57    char aData[POW
79f50 32 5f 4d 49 4e 5d 3b 0a 20 20 20 20 73 74 72 75  2_MIN];.    stru
79f60 63 74 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e  ct {.      int n
79f70 65 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e  ext;       /* In
79f80 64 65 78 20 69 6e 20 6d 65 6d 2e 61 50 6f 6f 6c  dex in mem.aPool
79f90 5b 5d 20 6f 66 20 6e 65 78 74 20 66 72 65 65 20  [] of next free 
79fa0 63 68 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 69  chunk */.      i
79fb0 6e 74 20 70 72 65 76 3b 20 20 20 20 20 20 20 2f  nt prev;       /
79fc0 2a 20 49 6e 64 65 78 20 69 6e 20 6d 65 6d 2e 61  * Index in mem.a
79fd0 50 6f 6f 6c 5b 5d 20 6f 66 20 70 72 65 76 69 6f  Pool[] of previo
79fe0 75 73 20 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f  us free chunk */
79ff0 0a 20 20 20 20 7d 20 6c 69 73 74 3b 0a 20 20 7d  .    } list;.  }
7a000 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4e 75   u;.};../*.** Nu
7a010 6d 62 65 72 20 6f 66 20 62 6c 6f 63 6b 73 20 6f  mber of blocks o
7a020 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62  f memory availab
7a030 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f  le for allocatio
7a040 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 42  n..*/.#define NB
7a050 4c 4f 43 4b 20 28 53 51 4c 49 54 45 5f 50 4f 57  LOCK (SQLITE_POW
7a060 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 2f 50 4f  2_MEMORY_SIZE/PO
7a070 57 32 5f 4d 49 4e 29 0a 0a 2f 2a 0a 2a 2a 20 54  W2_MIN)../*.** T
7a080 68 65 20 73 69 7a 65 20 69 6e 20 62 6c 6f 63 6b  he size in block
7a090 73 20 6f 66 20 61 6e 20 50 4f 57 32 5f 4d 41 58  s of an POW2_MAX
7a0a0 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 23   allocation.*/.#
7a0b0 64 65 66 69 6e 65 20 53 5a 5f 4d 41 58 20 28 31  define SZ_MAX (1
7a0c0 3c 3c 28 4e 53 49 5a 45 2d 31 29 29 0a 0a 2f 2a  <<(NSIZE-1))../*
7a0d0 0a 2a 2a 20 4d 61 73 6b 73 20 75 73 65 64 20 66  .** Masks used f
7a0e0 6f 72 20 6d 65 6d 2e 61 43 74 72 6c 5b 5d 20 65  or mem.aCtrl[] e
7a0f0 6c 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 23 64 65 66  lements..*/.#def
7a100 69 6e 65 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45  ine CTRL_LOGSIZE
7a110 20 20 30 78 31 66 20 20 20 20 2f 2a 20 4c 6f 67    0x1f    /* Log
7a120 32 20 53 69 7a 65 20 6f 66 20 74 68 69 73 20 62  2 Size of this b
7a130 6c 6f 63 6b 20 72 65 6c 61 74 69 76 65 20 74 6f  lock relative to
7a140 20 50 4f 57 32 5f 4d 49 4e 20 2a 2f 0a 23 64 65   POW2_MIN */.#de
7a150 66 69 6e 65 20 43 54 52 4c 5f 46 52 45 45 20 20  fine CTRL_FREE  
7a160 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20 54 72     0x20    /* Tr
7a170 75 65 20 69 66 20 6e 6f 74 20 63 68 65 63 6b 65  ue if not checke
7a180 64 20 6f 75 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  d out */../*.** 
7a190 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69  All of the stati
7a1a0 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64  c variables used
7a1b0 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20   by this module 
7a1c0 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a  are collected.**
7a1d0 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73   into a single s
7a1e0 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22  tructure named "
7a1f0 6d 65 6d 22 2e 20 20 54 68 69 73 20 69 73 20 74  mem".  This is t
7a200 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74  o keep the.** st
7a210 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f  atic variables o
7a220 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20  rganized and to 
7a230 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65  reduce namespace
7a240 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68   pollution.** wh
7a250 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69  en this module i
7a260 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20  s combined with 
7a270 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61  other in the ama
7a280 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74  lgamation..*/.st
7a290 61 74 69 63 20 73 74 72 75 63 74 20 7b 0a 20 20  atic struct {.  
7a2a0 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 61 6c 61 72  /*.  ** The alar
7a2b0 6d 20 63 61 6c 6c 62 61 63 6b 20 61 6e 64 20 69  m callback and i
7a2c0 74 73 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 54  ts arguments.  T
7a2d0 68 65 20 6d 65 6d 2e 6d 75 74 65 78 20 6c 6f 63  he mem.mutex loc
7a2e0 6b 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 68  k will.  ** be h
7a2f0 65 6c 64 20 77 68 69 6c 65 20 74 68 65 20 63 61  eld while the ca
7a300 6c 6c 62 61 63 6b 20 69 73 20 72 75 6e 6e 69 6e  llback is runnin
7a310 67 2e 20 20 52 65 63 75 72 73 69 76 65 20 63 61  g.  Recursive ca
7a320 6c 6c 73 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68  lls into.  ** th
7a330 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74  e memory subsyst
7a340 65 6d 20 61 72 65 20 61 6c 6c 6f 77 65 64 2c 20  em are allowed, 
7a350 62 75 74 20 6e 6f 20 6e 65 77 20 63 61 6c 6c 62  but no new callb
7a360 61 63 6b 73 20 77 69 6c 6c 20 62 65 0a 20 20 2a  acks will be.  *
7a370 2a 20 69 73 73 75 65 64 2e 20 20 54 68 65 20 61  * issued.  The a
7a380 6c 61 72 6d 42 75 73 79 20 76 61 72 69 61 62 6c  larmBusy variabl
7a390 65 20 69 73 20 73 65 74 20 74 6f 20 70 72 65 76  e is set to prev
7a3a0 65 6e 74 20 72 65 63 75 72 73 69 76 65 0a 20 20  ent recursive.  
7a3b0 2a 2a 20 63 61 6c 6c 62 61 63 6b 73 2e 0a 20 20  ** callbacks..  
7a3c0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
7a3d0 36 34 20 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c  64 alarmThreshol
7a3e0 64 3b 0a 20 20 76 6f 69 64 20 28 2a 61 6c 61 72  d;.  void (*alar
7a3f0 6d 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a  mCallback)(void*
7a400 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c  , sqlite3_int64,
7a410 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 2a 61 6c  int);.  void *al
7a420 61 72 6d 41 72 67 3b 0a 20 20 69 6e 74 20 61 6c  armArg;.  int al
7a430 61 72 6d 42 75 73 79 3b 0a 20 20 0a 20 20 2f 2a  armBusy;.  .  /*
7a440 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63  .  ** Mutex to c
7a450 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 74 6f  ontrol access to
7a460 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
7a470 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d  cation subsystem
7a480 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
7a490 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 0a  _mutex *mutex;..
7a4a0 20 20 2f 2a 0a 20 20 2a 2a 20 50 65 72 66 6f 72    /*.  ** Perfor
7a4b0 6d 61 6e 63 65 20 73 74 61 74 69 73 74 69 63 73  mance statistics
7a4c0 0a 20 20 2a 2f 0a 20 20 75 36 34 20 6e 41 6c 6c  .  */.  u64 nAll
7a4d0 6f 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  oc;         /* T
7a4e0 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63  otal number of c
7a4f0 61 6c 6c 73 20 74 6f 20 6d 61 6c 6c 6f 63 20 2a  alls to malloc *
7a500 2f 0a 20 20 75 36 34 20 74 6f 74 61 6c 41 6c 6c  /.  u64 totalAll
7a510 6f 63 3b 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c  oc;     /* Total
7a520 20 6f 66 20 61 6c 6c 20 6d 61 6c 6c 6f 63 20 63   of all malloc c
7a530 61 6c 6c 73 20 2d 20 69 6e 63 6c 75 64 65 73 20  alls - includes 
7a540 69 6e 74 65 72 6e 61 6c 20 66 72 61 67 20 2a 2f  internal frag */
7a550 0a 20 20 75 36 34 20 74 6f 74 61 6c 45 78 63 65  .  u64 totalExce
7a560 73 73 3b 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20  ss;    /* Total 
7a570 69 6e 74 65 72 6e 61 6c 20 66 72 61 67 6d 65 6e  internal fragmen
7a580 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 75 33 32 20  tation */.  u32 
7a590 63 75 72 72 65 6e 74 4f 75 74 3b 20 20 20 20 20  currentOut;     
7a5a0 2f 2a 20 43 75 72 72 65 6e 74 20 63 68 65 63 6b  /* Current check
7a5b0 6f 75 74 2c 20 69 6e 63 6c 75 64 69 6e 67 20 69  out, including i
7a5c0 6e 74 65 72 6e 61 6c 20 66 72 61 67 6d 65 6e 74  nternal fragment
7a5d0 61 74 69 6f 6e 20 2a 2f 0a 20 20 75 33 32 20 63  ation */.  u32 c
7a5e0 75 72 72 65 6e 74 43 6f 75 6e 74 3b 20 20 20 2f  urrentCount;   /
7a5f0 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d 62 65 72  * Current number
7a600 20 6f 66 20 64 69 73 74 69 6e 63 74 20 63 68 65   of distinct che
7a610 63 6b 6f 75 74 73 20 2a 2f 0a 20 20 75 33 32 20  ckouts */.  u32 
7a620 6d 61 78 4f 75 74 3b 20 20 20 20 20 20 20 20 20  maxOut;         
7a630 2f 2a 20 4d 61 78 69 6d 75 6d 20 69 6e 73 74 61  /* Maximum insta
7a640 6e 74 61 6e 65 6f 75 73 20 63 75 72 72 65 6e 74  ntaneous current
7a650 4f 75 74 20 2a 2f 0a 20 20 75 33 32 20 6d 61 78  Out */.  u32 max
7a660 43 6f 75 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20  Count;       /* 
7a670 4d 61 78 69 6d 75 6d 20 69 6e 73 74 61 6e 74 61  Maximum instanta
7a680 6e 65 6f 75 73 20 63 75 72 72 65 6e 74 43 6f 75  neous currentCou
7a690 6e 74 20 2a 2f 0a 20 20 75 33 32 20 6d 61 78 52  nt */.  u32 maxR
7a6a0 65 71 75 65 73 74 3b 20 20 20 20 20 2f 2a 20 4c  equest;     /* L
7a6b0 61 72 67 65 73 74 20 61 6c 6c 6f 63 61 74 69 6f  argest allocatio
7a6c0 6e 20 28 65 78 63 6c 75 73 69 76 65 20 6f 66 20  n (exclusive of 
7a6d0 69 6e 74 65 72 6e 61 6c 20 66 72 61 67 29 20 2a  internal frag) *
7a6e0 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4c  /.  .  /*.  ** L
7a6f0 69 73 74 73 20 6f 66 20 66 72 65 65 20 62 6c 6f  ists of free blo
7a700 63 6b 73 20 6f 66 20 76 61 72 69 6f 75 73 20 73  cks of various s
7a710 69 7a 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74  izes..  */.  int
7a720 20 61 69 46 72 65 65 6c 69 73 74 5b 4e 53 49 5a   aiFreelist[NSIZ
7a730 45 5d 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53  E];..  /*.  ** S
7a740 70 61 63 65 20 66 6f 72 20 74 72 61 63 6b 69 6e  pace for trackin
7a750 67 20 77 68 69 63 68 20 62 6c 6f 63 6b 73 20 61  g which blocks a
7a760 72 65 20 63 68 65 63 6b 65 64 20 6f 75 74 20 61  re checked out a
7a770 6e 64 20 74 68 65 20 73 69 7a 65 0a 20 20 2a 2a  nd the size.  **
7a780 20 6f 66 20 65 61 63 68 20 62 6c 6f 63 6b 2e 20   of each block. 
7a790 20 4f 6e 65 20 62 79 74 65 20 70 65 72 20 62 6c   One byte per bl
7a7a0 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 75 38 20 61  ock..  */.  u8 a
7a7b0 43 74 72 6c 5b 4e 42 4c 4f 43 4b 5d 3b 0a 0a 20  Ctrl[NBLOCK];.. 
7a7c0 20 2f 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20   /*.  ** Memory 
7a7d0 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c  available for al
7a7e0 6c 6f 63 61 74 69 6f 6e 0a 20 20 2a 2f 0a 20 20  location.  */.  
7a7f0 4d 65 6d 35 42 6c 6f 63 6b 20 61 50 6f 6f 6c 5b  Mem5Block aPool[
7a800 4e 42 4c 4f 43 4b 5d 3b 0a 7d 20 6d 65 6d 3b 0a  NBLOCK];.} mem;.
7a810 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68  ./*.** Unlink th
7a820 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 2e 61  e chunk at mem.a
7a830 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 73  Pool[i] from lis
7a840 74 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c  t it is currentl
7a850 79 0a 2a 2a 20 6f 6e 2e 20 20 49 74 20 73 68 6f  y.** on.  It sho
7a860 75 6c 64 20 62 65 20 66 6f 75 6e 64 20 6f 6e 20  uld be found on 
7a870 6d 65 6d 2e 61 69 46 72 65 65 6c 69 73 74 5b 69  mem.aiFreelist[i
7a880 4c 6f 67 73 69 7a 65 5d 2e 0a 2a 2f 0a 73 74 61  Logsize]..*/.sta
7a890 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35  tic void memsys5
7a8a0 55 6e 6c 69 6e 6b 28 69 6e 74 20 69 2c 20 69 6e  Unlink(int i, in
7a8b0 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 69  t iLogsize){.  i
7a8c0 6e 74 20 6e 65 78 74 2c 20 70 72 65 76 3b 0a 20  nt next, prev;. 
7a8d0 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26   assert( i>=0 &&
7a8e0 20 69 3c 4e 42 4c 4f 43 4b 20 29 3b 0a 20 20 61   i<NBLOCK );.  a
7a8f0 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65 3e  ssert( iLogsize>
7a900 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c 4e  =0 && iLogsize<N
7a910 53 49 5a 45 20 29 3b 0a 20 20 61 73 73 65 72 74  SIZE );.  assert
7a920 28 20 28 6d 65 6d 2e 61 43 74 72 6c 5b 69 5d 20  ( (mem.aCtrl[i] 
7a930 26 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d  & CTRL_LOGSIZE)=
7a940 3d 69 4c 6f 67 73 69 7a 65 20 29 3b 0a 20 20 61  =iLogsize );.  a
7a950 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
7a960 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 2e 6d 75  utex_held(mem.mu
7a970 74 65 78 29 20 29 3b 0a 0a 20 20 6e 65 78 74 20  tex) );..  next 
7a980 3d 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 69 5d 2e 75  = mem.aPool[i].u
7a990 2e 6c 69 73 74 2e 6e 65 78 74 3b 0a 20 20 70 72  .list.next;.  pr
7a9a0 65 76 20 3d 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 69  ev = mem.aPool[i
7a9b0 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 3b 0a 20  ].u.list.prev;. 
7a9c0 20 69 66 28 20 70 72 65 76 3c 30 20 29 7b 0a 20   if( prev<0 ){. 
7a9d0 20 20 20 6d 65 6d 2e 61 69 46 72 65 65 6c 69 73     mem.aiFreelis
7a9e0 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 3d 20 6e 65  t[iLogsize] = ne
7a9f0 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  xt;.  }else{.   
7aa00 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 70 72 65 76 5d   mem.aPool[prev]
7aa10 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 20 3d 20 6e  .u.list.next = n
7aa20 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e  ext;.  }.  if( n
7aa30 65 78 74 3e 3d 30 20 29 7b 0a 20 20 20 20 6d 65  ext>=0 ){.    me
7aa40 6d 2e 61 50 6f 6f 6c 5b 6e 65 78 74 5d 2e 75 2e  m.aPool[next].u.
7aa50 6c 69 73 74 2e 70 72 65 76 20 3d 20 70 72 65 76  list.prev = prev
7aa60 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  ;.  }.}../*.** L
7aa70 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74  ink the chunk at
7aa80 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 69 5d 20 73 6f   mem.aPool[i] so
7aa90 20 74 68 61 74 20 69 73 20 6f 6e 20 74 68 65 20   that is on the 
7aaa0 69 4c 6f 67 73 69 7a 65 0a 2a 2a 20 66 72 65 65  iLogsize.** free
7aab0 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63   list..*/.static
7aac0 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 4c 69 6e   void memsys5Lin
7aad0 6b 28 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c 6f  k(int i, int iLo
7aae0 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 78 3b  gsize){.  int x;
7aaf0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
7ab00 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65  e3_mutex_held(me
7ab10 6d 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  m.mutex) );.  as
7ab20 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
7ab30 4e 42 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65  NBLOCK );.  asse
7ab40 72 74 28 20 69 4c 6f 67 73 69 7a 65 3e 3d 30 20  rt( iLogsize>=0 
7ab50 26 26 20 69 4c 6f 67 73 69 7a 65 3c 4e 53 49 5a  && iLogsize<NSIZ
7ab60 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  E );.  assert( (
7ab70 6d 65 6d 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43  mem.aCtrl[i] & C
7ab80 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c  TRL_LOGSIZE)==iL
7ab90 6f 67 73 69 7a 65 20 29 3b 0a 0a 20 20 6d 65 6d  ogsize );..  mem
7aba0 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74  .aPool[i].u.list
7abb0 2e 6e 65 78 74 20 3d 20 78 20 3d 20 6d 65 6d 2e  .next = x = mem.
7abc0 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73  aiFreelist[iLogs
7abd0 69 7a 65 5d 3b 0a 20 20 6d 65 6d 2e 61 50 6f 6f  ize];.  mem.aPoo
7abe0 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76  l[i].u.list.prev
7abf0 20 3d 20 2d 31 3b 0a 20 20 69 66 28 20 78 3e 3d   = -1;.  if( x>=
7ac00 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  0 ){.    assert(
7ac10 20 78 3c 4e 42 4c 4f 43 4b 20 29 3b 0a 20 20 20   x<NBLOCK );.   
7ac20 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 78 5d 2e 75 2e   mem.aPool[x].u.
7ac30 6c 69 73 74 2e 70 72 65 76 20 3d 20 69 3b 0a 20  list.prev = i;. 
7ac40 20 7d 0a 20 20 6d 65 6d 2e 61 69 46 72 65 65 6c   }.  mem.aiFreel
7ac50 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 3d 20  ist[iLogsize] = 
7ac60 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65  i;.}../*.** Ente
7ac70 72 20 74 68 65 20 6d 75 74 65 78 20 6d 65 6d 2e  r the mutex mem.
7ac80 6d 75 74 65 78 2e 20 41 6c 6c 6f 63 61 74 65 20  mutex. Allocate 
7ac90 69 74 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20  it if it is not 
7aca0 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65  already allocate
7acb0 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 3a 20 20  d..**.** Also:  
7acc0 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d  Initialize the m
7acd0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
7ace0 20 73 75 62 73 79 73 74 65 6d 20 74 68 65 20 66   subsystem the f
7acf0 69 72 73 74 20 74 69 6d 65 0a 2a 2a 20 74 68 69  irst time.** thi
7ad00 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
7ad10 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  led..*/.static v
7ad20 6f 69 64 20 6d 65 6d 73 79 73 35 45 6e 74 65 72  oid memsys5Enter
7ad30 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 6d 65  (void){.  if( me
7ad40 6d 2e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20  m.mutex==0 ){.  
7ad50 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 61 73 73    int i;.    ass
7ad60 65 72 74 28 20 73 69 7a 65 6f 66 28 4d 65 6d 35  ert( sizeof(Mem5
7ad70 42 6c 6f 63 6b 29 3d 3d 50 4f 57 32 5f 4d 49 4e  Block)==POW2_MIN
7ad80 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
7ad90 28 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d  (SQLITE_POW2_MEM
7ada0 4f 52 59 5f 53 49 5a 45 20 25 20 50 4f 57 32 5f  ORY_SIZE % POW2_
7adb0 4d 41 58 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61  MAX)==0 );.    a
7adc0 73 73 65 72 74 28 20 53 51 4c 49 54 45 5f 50 4f  ssert( SQLITE_PO
7add0 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 3e 3d  W2_MEMORY_SIZE>=
7ade0 50 4f 57 32 5f 4d 41 58 20 29 3b 0a 20 20 20 20  POW2_MAX );.    
7adf0 6d 65 6d 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69  mem.mutex = sqli
7ae00 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
7ae10 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
7ae20 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 20 20 73 71  TIC_MEM);.    sq
7ae30 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
7ae40 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20  r(mem.mutex);.  
7ae50 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 53 49    for(i=0; i<NSI
7ae60 5a 45 3b 20 69 2b 2b 29 20 6d 65 6d 2e 61 69 46  ZE; i++) mem.aiF
7ae70 72 65 65 6c 69 73 74 5b 69 5d 20 3d 20 2d 31 3b  reelist[i] = -1;
7ae80 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
7ae90 3d 4e 42 4c 4f 43 4b 2d 53 5a 5f 4d 41 58 3b 20  =NBLOCK-SZ_MAX; 
7aea0 69 20 2b 3d 20 53 5a 5f 4d 41 58 29 7b 0a 20 20  i += SZ_MAX){.  
7aeb0 20 20 20 20 6d 65 6d 2e 61 43 74 72 6c 5b 69 5d      mem.aCtrl[i]
7aec0 20 3d 20 28 4e 53 49 5a 45 2d 31 29 20 7c 20 43   = (NSIZE-1) | C
7aed0 54 52 4c 5f 46 52 45 45 3b 0a 20 20 20 20 20 20  TRL_FREE;.      
7aee0 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 2c 20 4e  memsys5Link(i, N
7aef0 53 49 5a 45 2d 31 29 3b 0a 20 20 20 20 7d 0a 20  SIZE-1);.    }. 
7af00 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
7af10 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
7af20 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a  mem.mutex);.  }.
7af30 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
7af40 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65  the amount of me
7af50 6d 6f 72 79 20 63 75 72 72 65 6e 74 6c 79 20 63  mory currently c
7af60 68 65 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 53  hecked out..*/.S
7af70 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
7af80 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f  3_int64 sqlite3_
7af90 6d 65 6d 6f 72 79 5f 75 73 65 64 28 76 6f 69 64  memory_used(void
7afa0 29 7b 0a 20 20 72 65 74 75 72 6e 20 6d 65 6d 2e  ){.  return mem.
7afb0 63 75 72 72 65 6e 74 4f 75 74 3b 0a 7d 0a 0a 2f  currentOut;.}../
7afc0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
7afd0 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f  maximum amount o
7afe0 66 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 68 61  f memory that ha
7aff0 73 20 65 76 65 72 20 62 65 65 6e 0a 2a 2a 20 63  s ever been.** c
7b000 68 65 63 6b 65 64 20 6f 75 74 20 73 69 6e 63 65  hecked out since
7b010 20 65 69 74 68 65 72 20 74 68 65 20 62 65 67 69   either the begi
7b020 6e 6e 69 6e 67 20 6f 66 20 74 68 69 73 20 70 72  nning of this pr
7b030 6f 63 65 73 73 0a 2a 2a 20 6f 72 20 73 69 6e 63  ocess.** or sinc
7b040 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  e the most recen
7b050 74 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49  t reset..*/.SQLI
7b060 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69  TE_API sqlite3_i
7b070 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d  nt64 sqlite3_mem
7b080 6f 72 79 5f 68 69 67 68 77 61 74 65 72 28 69 6e  ory_highwater(in
7b090 74 20 72 65 73 65 74 46 6c 61 67 29 7b 0a 20 20  t resetFlag){.  
7b0a0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 3b  sqlite3_int64 n;
7b0b0 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28  .  memsys5Enter(
7b0c0 29 3b 0a 20 20 6e 20 3d 20 6d 65 6d 2e 6d 61 78  );.  n = mem.max
7b0d0 4f 75 74 3b 0a 20 20 69 66 28 20 72 65 73 65 74  Out;.  if( reset
7b0e0 46 6c 61 67 20 29 7b 0a 20 20 20 20 6d 65 6d 2e  Flag ){.    mem.
7b0f0 6d 61 78 4f 75 74 20 3d 20 6d 65 6d 2e 63 75 72  maxOut = mem.cur
7b100 72 65 6e 74 4f 75 74 3b 0a 20 20 7d 0a 20 20 73  rentOut;.  }.  s
7b110 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
7b120 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 20 20  ve(mem.mutex);  
7b130 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a  .  return n;.}..
7b140 0a 2f 2a 0a 2a 2a 20 54 72 69 67 67 65 72 20 74  ./*.** Trigger t
7b150 68 65 20 61 6c 61 72 6d 20 0a 2a 2f 0a 73 74 61  he alarm .*/.sta
7b160 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35  tic void memsys5
7b170 41 6c 61 72 6d 28 69 6e 74 20 6e 42 79 74 65 29  Alarm(int nByte)
7b180 7b 0a 20 20 76 6f 69 64 20 28 2a 78 43 61 6c 6c  {.  void (*xCall
7b190 62 61 63 6b 29 28 76 6f 69 64 2a 2c 73 71 6c 69  back)(void*,sqli
7b1a0 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74 29 3b 0a  te3_int64,int);.
7b1b0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
7b1c0 6e 6f 77 55 73 65 64 3b 0a 20 20 76 6f 69 64 20  nowUsed;.  void 
7b1d0 2a 70 41 72 67 3b 0a 20 20 69 66 28 20 6d 65 6d  *pArg;.  if( mem
7b1e0 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 3d 3d  .alarmCallback==
7b1f0 30 20 7c 7c 20 6d 65 6d 2e 61 6c 61 72 6d 42 75  0 || mem.alarmBu
7b200 73 79 20 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  sy  ) return;.  
7b210 6d 65 6d 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20  mem.alarmBusy = 
7b220 31 3b 0a 20 20 78 43 61 6c 6c 62 61 63 6b 20 3d  1;.  xCallback =
7b230 20 6d 65 6d 2e 61 6c 61 72 6d 43 61 6c 6c 62 61   mem.alarmCallba
7b240 63 6b 3b 0a 20 20 6e 6f 77 55 73 65 64 20 3d 20  ck;.  nowUsed = 
7b250 6d 65 6d 2e 63 75 72 72 65 6e 74 4f 75 74 3b 0a  mem.currentOut;.
7b260 20 20 70 41 72 67 20 3d 20 6d 65 6d 2e 61 6c 61    pArg = mem.ala
7b270 72 6d 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33  rmArg;.  sqlite3
7b280 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d  _mutex_leave(mem
7b290 2e 6d 75 74 65 78 29 3b 0a 20 20 78 43 61 6c 6c  .mutex);.  xCall
7b2a0 62 61 63 6b 28 70 41 72 67 2c 20 6e 6f 77 55 73  back(pArg, nowUs
7b2b0 65 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 73 71  ed, nByte);.  sq
7b2c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
7b2d0 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20  r(mem.mutex);.  
7b2e0 6d 65 6d 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20  mem.alarmBusy = 
7b2f0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  0;.}../*.** Chan
7b300 67 65 20 74 68 65 20 61 6c 61 72 6d 20 63 61 6c  ge the alarm cal
7b310 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  lback..**.** Thi
7b320 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72  s is a no-op for
7b330 20 74 68 65 20 73 74 61 74 69 63 20 6d 65 6d 6f   the static memo
7b340 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e 20 20 54  ry allocator.  T
7b350 68 65 20 70 75 72 70 6f 73 65 0a 2a 2a 20 6f 66  he purpose.** of
7b360 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 61 72   the memory alar
7b370 6d 20 69 73 20 74 6f 20 73 75 70 70 6f 72 74 20  m is to support 
7b380 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61  sqlite3_soft_hea
7b390 70 5f 6c 69 6d 69 74 28 29 2e 0a 2a 2a 20 42 75  p_limit()..** Bu
7b3a0 74 20 77 69 74 68 20 74 68 69 73 20 6d 65 6d 6f  t with this memo
7b3b0 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2c 20 74 68  ry allocator, th
7b3c0 65 20 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69  e soft_heap_limi
7b3d0 74 20 69 73 20 72 65 61 6c 6c 79 0a 2a 2a 20 61  t is really.** a
7b3e0 20 68 61 72 64 20 6c 69 6d 69 74 20 74 68 61 74   hard limit that
7b3f0 20 69 73 20 66 69 78 65 64 20 61 74 20 53 51 4c   is fixed at SQL
7b400 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f  ITE_POW2_MEMORY_
7b410 53 49 5a 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  SIZE..*/.SQLITE_
7b420 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
7b430 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d 28 0a 20 20  memory_alarm(.  
7b440 76 6f 69 64 28 2a 78 43 61 6c 6c 62 61 63 6b 29  void(*xCallback)
7b450 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 73 71 6c  (void *pArg, sql
7b460 69 74 65 33 5f 69 6e 74 36 34 20 75 73 65 64 2c  ite3_int64 used,
7b470 69 6e 74 20 4e 29 2c 0a 20 20 76 6f 69 64 20 2a  int N),.  void *
7b480 70 41 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f  pArg,.  sqlite3_
7b490 69 6e 74 36 34 20 69 54 68 72 65 73 68 6f 6c 64  int64 iThreshold
7b4a0 0a 29 7b 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74  .){.  memsys5Ent
7b4b0 65 72 28 29 3b 0a 20 20 6d 65 6d 2e 61 6c 61 72  er();.  mem.alar
7b4c0 6d 43 61 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c  mCallback = xCal
7b4d0 6c 62 61 63 6b 3b 0a 20 20 6d 65 6d 2e 61 6c 61  lback;.  mem.ala
7b4e0 72 6d 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20  rmArg = pArg;.  
7b4f0 6d 65 6d 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f  mem.alarmThresho
7b500 6c 64 20 3d 20 69 54 68 72 65 73 68 6f 6c 64 3b  ld = iThreshold;
7b510 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
7b520 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78  _leave(mem.mutex
7b530 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
7b540 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
7b550 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20  Return the size 
7b560 6f 66 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e  of an outstandin
7b570 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e  g allocation, in
7b580 20 62 79 74 65 73 2e 20 20 54 68 65 0a 2a 2a 20   bytes.  The.** 
7b590 73 69 7a 65 20 72 65 74 75 72 6e 65 64 20 6f 6d  size returned om
7b5a0 69 74 73 20 74 68 65 20 38 2d 62 79 74 65 20 68  its the 8-byte h
7b5b0 65 61 64 65 72 20 6f 76 65 72 68 65 61 64 2e 20  eader overhead. 
7b5c0 20 54 68 69 73 20 6f 6e 6c 79 0a 2a 2a 20 77 6f   This only.** wo
7b5d0 72 6b 73 20 66 6f 72 20 63 68 75 6e 6b 73 20 74  rks for chunks t
7b5e0 68 61 74 20 61 72 65 20 63 75 72 72 65 6e 74 6c  hat are currentl
7b5f0 79 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 0a 2a  y checked out..*
7b600 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
7b610 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c   int sqlite3Mall
7b620 6f 63 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b  ocSize(void *p){
7b630 0a 20 20 69 6e 74 20 69 53 69 7a 65 20 3d 20 30  .  int iSize = 0
7b640 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  ;.  if( p ){.   
7b650 20 69 6e 74 20 69 20 3d 20 28 28 4d 65 6d 35 42   int i = ((Mem5B
7b660 6c 6f 63 6b 2a 29 70 29 20 2d 20 6d 65 6d 2e 61  lock*)p) - mem.a
7b670 50 6f 6f 6c 3b 0a 20 20 20 20 61 73 73 65 72 74  Pool;.    assert
7b680 28 20 69 3e 3d 30 20 26 26 20 69 3c 4e 42 4c 4f  ( i>=0 && i<NBLO
7b690 43 4b 20 29 3b 0a 20 20 20 20 69 53 69 7a 65 20  CK );.    iSize 
7b6a0 3d 20 31 20 3c 3c 20 28 28 6d 65 6d 2e 61 43 74  = 1 << ((mem.aCt
7b6b0 72 6c 5b 69 5d 26 43 54 52 4c 5f 4c 4f 47 53 49  rl[i]&CTRL_LOGSI
7b6c0 5a 45 29 20 2b 20 53 51 4c 49 54 45 5f 50 4f 57  ZE) + SQLITE_POW
7b6d0 32 5f 4c 4f 47 4d 49 4e 29 3b 0a 20 20 7d 0a 20  2_LOGMIN);.  }. 
7b6e0 20 72 65 74 75 72 6e 20 69 53 69 7a 65 3b 0a 7d   return iSize;.}
7b6f0 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65  ../*.** Find the
7b700 20 66 69 72 73 74 20 65 6e 74 72 79 20 6f 6e 20   first entry on 
7b710 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 4c 6f  the freelist iLo
7b720 67 73 69 7a 65 2e 20 20 55 6e 6c 69 6e 6b 20 74  gsize.  Unlink t
7b730 68 61 74 0a 2a 2a 20 65 6e 74 72 79 20 61 6e 64  hat.** entry and
7b740 20 72 65 74 75 72 6e 20 69 74 73 20 69 6e 64 65   return its inde
7b750 78 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  x. .*/.static in
7b760 74 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 46  t memsys5UnlinkF
7b770 69 72 73 74 28 69 6e 74 20 69 4c 6f 67 73 69 7a  irst(int iLogsiz
7b780 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  e){.  int i;.  i
7b790 6e 74 20 69 46 69 72 73 74 3b 0a 0a 20 20 61 73  nt iFirst;..  as
7b7a0 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65 3e 3d  sert( iLogsize>=
7b7b0 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c 4e 53  0 && iLogsize<NS
7b7c0 49 5a 45 20 29 3b 0a 20 20 69 20 3d 20 69 46 69  IZE );.  i = iFi
7b7d0 72 73 74 20 3d 20 6d 65 6d 2e 61 69 46 72 65 65  rst = mem.aiFree
7b7e0 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 3b 0a  list[iLogsize];.
7b7f0 20 20 61 73 73 65 72 74 28 20 69 46 69 72 73 74    assert( iFirst
7b800 3e 3d 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 20  >=0 );.  while( 
7b810 69 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 69  i>0 ){.    if( i
7b820 3c 69 46 69 72 73 74 20 29 20 69 46 69 72 73 74  <iFirst ) iFirst
7b830 20 3d 20 69 3b 0a 20 20 20 20 69 20 3d 20 6d 65   = i;.    i = me
7b840 6d 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73  m.aPool[i].u.lis
7b850 74 2e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 6d 65  t.next;.  }.  me
7b860 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 46 69 72  msys5Unlink(iFir
7b870 73 74 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a 20  st, iLogsize);. 
7b880 20 72 65 74 75 72 6e 20 69 46 69 72 73 74 3b 0a   return iFirst;.
7b890 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
7b8a0 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72  a block of memor
7b8b0 79 20 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 42  y of at least nB
7b8c0 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a  ytes in size..**
7b8d0 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20   Return NULL if 
7b8e0 75 6e 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  unable..*/.stati
7b8f0 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 4d  c void *memsys5M
7b900 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29  alloc(int nByte)
7b910 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  {.  int i;      
7b920 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
7b930 20 61 20 6d 65 6d 2e 61 50 6f 6f 6c 5b 5d 20 73   a mem.aPool[] s
7b940 6c 6f 74 20 2a 2f 0a 20 20 69 6e 74 20 69 42 69  lot */.  int iBi
7b950 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64  n;        /* Ind
7b960 65 78 20 69 6e 74 6f 20 6d 65 6d 2e 61 69 46 72  ex into mem.aiFr
7b970 65 65 6c 69 73 74 5b 5d 20 2a 2f 0a 20 20 69 6e  eelist[] */.  in
7b980 74 20 69 46 75 6c 6c 53 7a 3b 20 20 20 20 20 2f  t iFullSz;     /
7b990 2a 20 53 69 7a 65 20 6f 66 20 61 6c 6c 6f 63 61  * Size of alloca
7b9a0 74 69 6f 6e 20 72 6f 75 6e 64 65 64 20 75 70 20  tion rounded up 
7b9b0 74 6f 20 70 6f 77 65 72 20 6f 66 20 32 20 2a 2f  to power of 2 */
7b9c0 0a 20 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 3b  .  int iLogsize;
7b9d0 20 20 20 20 2f 2a 20 4c 6f 67 32 20 6f 66 20 69      /* Log2 of i
7b9e0 46 75 6c 6c 53 7a 2f 50 4f 57 32 5f 4d 49 4e 20  FullSz/POW2_MIN 
7b9f0 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  */..  assert( sq
7ba00 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
7ba10 28 6d 65 6d 2e 6d 75 74 65 78 29 20 29 3b 0a 0a  (mem.mutex) );..
7ba20 20 20 2f 2a 20 4b 65 65 70 20 74 72 61 63 6b 20    /* Keep track 
7ba30 6f 66 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61  of the maximum a
7ba40 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73  llocation reques
7ba50 74 2e 20 20 45 76 65 6e 20 75 6e 66 75 6c 66 69  t.  Even unfulfi
7ba60 6c 6c 65 64 0a 20 20 2a 2a 20 72 65 71 75 65 73  lled.  ** reques
7ba70 74 73 20 61 72 65 20 63 6f 75 6e 74 65 64 20 2a  ts are counted *
7ba80 2f 0a 20 20 69 66 28 20 6e 42 79 74 65 3e 6d 65  /.  if( nByte>me
7ba90 6d 2e 6d 61 78 52 65 71 75 65 73 74 20 29 7b 0a  m.maxRequest ){.
7baa0 20 20 20 20 6d 65 6d 2e 6d 61 78 52 65 71 75 65      mem.maxReque
7bab0 73 74 20 3d 20 6e 42 79 74 65 3b 0a 20 20 7d 0a  st = nByte;.  }.
7bac0 0a 20 20 2f 2a 20 53 69 6d 75 6c 61 74 65 20 61  .  /* Simulate a
7bad0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
7bae0 6f 6e 20 66 61 75 6c 74 20 2a 2f 0a 20 20 69 66  on fault */.  if
7baf0 28 20 73 71 6c 69 74 65 33 46 61 75 6c 74 53 74  ( sqlite3FaultSt
7bb00 65 70 28 53 51 4c 49 54 45 5f 46 41 55 4c 54 49  ep(SQLITE_FAULTI
7bb10 4e 4a 45 43 54 4f 52 5f 4d 41 4c 4c 4f 43 29 20  NJECTOR_MALLOC) 
7bb20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f  ) return 0;..  /
7bb30 2a 20 52 6f 75 6e 64 20 6e 42 79 74 65 20 75 70  * Round nByte up
7bb40 20 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c   to the next val
7bb50 69 64 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20  id power of two 
7bb60 2a 2f 0a 20 20 69 66 28 20 6e 42 79 74 65 3e 50  */.  if( nByte>P
7bb70 4f 57 32 5f 4d 41 58 20 29 20 72 65 74 75 72 6e  OW2_MAX ) return
7bb80 20 30 3b 0a 20 20 66 6f 72 28 69 46 75 6c 6c 53   0;.  for(iFullS
7bb90 7a 3d 50 4f 57 32 5f 4d 49 4e 2c 20 69 4c 6f 67  z=POW2_MIN, iLog
7bba0 73 69 7a 65 3d 30 3b 20 69 46 75 6c 6c 53 7a 3c  size=0; iFullSz<
7bbb0 6e 42 79 74 65 3b 20 69 46 75 6c 6c 53 7a 20 2a  nByte; iFullSz *
7bbc0 3d 20 32 2c 20 69 4c 6f 67 73 69 7a 65 2b 2b 29  = 2, iLogsize++)
7bbd0 7b 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 77  {}..  /* If we w
7bbe0 69 6c 6c 20 62 65 20 6f 76 65 72 20 74 68 65 20  ill be over the 
7bbf0 6d 65 6d 6f 72 79 20 61 6c 61 72 6d 20 74 68 72  memory alarm thr
7bc00 65 73 68 6f 6c 64 20 61 66 74 65 72 20 74 68 69  eshold after thi
7bc10 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 0a 20 20  s allocation,.  
7bc20 2a 2a 20 74 68 65 6e 20 74 72 69 67 67 65 72 20  ** then trigger 
7bc30 74 68 65 20 6d 65 6d 6f 72 79 20 6f 76 65 72 66  the memory overf
7bc40 6c 6f 77 20 61 6c 61 72 6d 20 2a 2f 0a 20 20 69  low alarm */.  i
7bc50 66 28 20 6d 65 6d 2e 61 6c 61 72 6d 43 61 6c 6c  f( mem.alarmCall
7bc60 62 61 63 6b 21 3d 30 20 26 26 20 6d 65 6d 2e 63  back!=0 && mem.c
7bc70 75 72 72 65 6e 74 4f 75 74 2b 69 46 75 6c 6c 53  urrentOut+iFullS
7bc80 7a 3e 3d 6d 65 6d 2e 61 6c 61 72 6d 54 68 72 65  z>=mem.alarmThre
7bc90 73 68 6f 6c 64 20 29 7b 0a 20 20 20 20 6d 65 6d  shold ){.    mem
7bca0 73 79 73 35 41 6c 61 72 6d 28 69 46 75 6c 6c 53  sys5Alarm(iFullS
7bcb0 7a 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61  z);.  }..  /* Ma
7bcc0 6b 65 20 73 75 72 65 20 6d 65 6d 2e 61 69 46 72  ke sure mem.aiFr
7bcd0 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d  eelist[iLogsize]
7bce0 20 63 6f 6e 74 61 69 6e 73 20 61 74 20 6c 65 61   contains at lea
7bcf0 73 74 20 6f 6e 65 20 66 72 65 65 0a 20 20 2a 2a  st one free.  **
7bd00 20 62 6c 6f 63 6b 2e 20 20 49 66 20 6e 6f 74 2c   block.  If not,
7bd10 20 74 68 65 6e 20 73 70 6c 69 74 20 61 20 62 6c   then split a bl
7bd20 6f 63 6b 20 6f 66 20 74 68 65 20 6e 65 78 74 20  ock of the next 
7bd30 6c 61 72 67 65 72 20 70 6f 77 65 72 20 6f 66 0a  larger power of.
7bd40 20 20 2a 2a 20 74 77 6f 20 69 6e 20 6f 72 64 65    ** two in orde
7bd50 72 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65  r to create a ne
7bd60 77 20 66 72 65 65 20 62 6c 6f 63 6b 20 6f 66 20  w free block of 
7bd70 73 69 7a 65 20 69 4c 6f 67 73 69 7a 65 2e 0a 20  size iLogsize.. 
7bd80 20 2a 2f 0a 20 20 66 6f 72 28 69 42 69 6e 3d 69   */.  for(iBin=i
7bd90 4c 6f 67 73 69 7a 65 3b 20 6d 65 6d 2e 61 69 46  Logsize; mem.aiF
7bda0 72 65 65 6c 69 73 74 5b 69 42 69 6e 5d 3c 30 20  reelist[iBin]<0 
7bdb0 26 26 20 69 42 69 6e 3c 4e 53 49 5a 45 3b 20 69  && iBin<NSIZE; i
7bdc0 42 69 6e 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 69  Bin++){}.  if( i
7bdd0 42 69 6e 3e 3d 4e 53 49 5a 45 20 29 20 72 65 74  Bin>=NSIZE ) ret
7bde0 75 72 6e 20 30 3b 0a 20 20 69 20 3d 20 6d 65 6d  urn 0;.  i = mem
7bdf0 73 79 73 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28  sys5UnlinkFirst(
7be00 69 42 69 6e 29 3b 0a 20 20 77 68 69 6c 65 28 20  iBin);.  while( 
7be10 69 42 69 6e 3e 69 4c 6f 67 73 69 7a 65 20 29 7b  iBin>iLogsize ){
7be20 0a 20 20 20 20 69 6e 74 20 6e 65 77 53 69 7a 65  .    int newSize
7be30 3b 0a 0a 20 20 20 20 69 42 69 6e 2d 2d 3b 0a 20  ;..    iBin--;. 
7be40 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 31 20 3c     newSize = 1 <
7be50 3c 20 69 42 69 6e 3b 0a 20 20 20 20 6d 65 6d 2e  < iBin;.    mem.
7be60 61 43 74 72 6c 5b 69 2b 6e 65 77 53 69 7a 65 5d  aCtrl[i+newSize]
7be70 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69   = CTRL_FREE | i
7be80 42 69 6e 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35  Bin;.    memsys5
7be90 4c 69 6e 6b 28 69 2b 6e 65 77 53 69 7a 65 2c 20  Link(i+newSize, 
7bea0 69 42 69 6e 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d  iBin);.  }.  mem
7beb0 2e 61 43 74 72 6c 5b 69 5d 20 3d 20 69 4c 6f 67  .aCtrl[i] = iLog
7bec0 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 55 70 64 61  size;..  /* Upda
7bed0 74 65 20 61 6c 6c 6f 63 61 74 6f 72 20 70 65 72  te allocator per
7bee0 66 6f 72 6d 61 6e 63 65 20 73 74 61 74 69 73 74  formance statist
7bef0 69 63 73 2e 20 2a 2f 0a 20 20 6d 65 6d 2e 6e 41  ics. */.  mem.nA
7bf00 6c 6c 6f 63 2b 2b 3b 0a 20 20 6d 65 6d 2e 74 6f  lloc++;.  mem.to
7bf10 74 61 6c 41 6c 6c 6f 63 20 2b 3d 20 69 46 75 6c  talAlloc += iFul
7bf20 6c 53 7a 3b 0a 20 20 6d 65 6d 2e 74 6f 74 61 6c  lSz;.  mem.total
7bf30 45 78 63 65 73 73 20 2b 3d 20 69 46 75 6c 6c 53  Excess += iFullS
7bf40 7a 20 2d 20 6e 42 79 74 65 3b 0a 20 20 6d 65 6d  z - nByte;.  mem
7bf50 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 2b 2b 3b  .currentCount++;
7bf60 0a 20 20 6d 65 6d 2e 63 75 72 72 65 6e 74 4f 75  .  mem.currentOu
7bf70 74 20 2b 3d 20 69 46 75 6c 6c 53 7a 3b 0a 20 20  t += iFullSz;.  
7bf80 69 66 28 20 6d 65 6d 2e 6d 61 78 43 6f 75 6e 74  if( mem.maxCount
7bf90 3c 6d 65 6d 2e 63 75 72 72 65 6e 74 43 6f 75 6e  <mem.currentCoun
7bfa0 74 20 29 20 6d 65 6d 2e 6d 61 78 43 6f 75 6e 74  t ) mem.maxCount
7bfb0 20 3d 20 6d 65 6d 2e 63 75 72 72 65 6e 74 43 6f   = mem.currentCo
7bfc0 75 6e 74 3b 0a 20 20 69 66 28 20 6d 65 6d 2e 6d  unt;.  if( mem.m
7bfd0 61 78 4f 75 74 3c 6d 65 6d 2e 63 75 72 72 65 6e  axOut<mem.curren
7bfe0 74 4f 75 74 20 29 20 6d 65 6d 2e 6d 61 78 4f 75  tOut ) mem.maxOu
7bff0 74 20 3d 20 6d 65 6d 2e 63 75 72 72 65 6e 74 4f  t = mem.currentO
7c000 75 74 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e  ut;..  /* Return
7c010 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
7c020 65 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f  e allocated memo
7c030 72 79 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  ry. */.  return 
7c040 28 76 6f 69 64 2a 29 26 6d 65 6d 2e 61 50 6f 6f  (void*)&mem.aPoo
7c050 6c 5b 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  l[i];.}../*.** F
7c060 72 65 65 20 61 6e 20 6f 75 74 73 74 61 6e 64 69  ree an outstandi
7c070 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ng memory alloca
7c080 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65  tion..*/.void me
7c090 6d 73 79 73 35 46 72 65 65 28 76 6f 69 64 20 2a  msys5Free(void *
7c0a0 70 4f 6c 64 29 7b 0a 20 20 75 33 32 20 73 69 7a  pOld){.  u32 siz
7c0b0 65 2c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 69  e, iLogsize;.  i
7c0c0 6e 74 20 69 3b 0a 0a 20 20 69 20 3d 20 28 28 4d  nt i;..  i = ((M
7c0d0 65 6d 35 42 6c 6f 63 6b 2a 29 70 4f 6c 64 29 20  em5Block*)pOld) 
7c0e0 2d 20 6d 65 6d 2e 61 50 6f 6f 6c 3b 0a 20 20 61  - mem.aPool;.  a
7c0f0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
7c100 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 2e 6d 75  utex_held(mem.mu
7c110 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
7c120 28 20 69 3e 3d 30 20 26 26 20 69 3c 4e 42 4c 4f  ( i>=0 && i<NBLO
7c130 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  CK );.  assert( 
7c140 28 6d 65 6d 2e 61 43 74 72 6c 5b 69 5d 20 26 20  (mem.aCtrl[i] & 
7c150 43 54 52 4c 5f 46 52 45 45 29 3d 3d 30 20 29 3b  CTRL_FREE)==0 );
7c160 0a 20 20 69 4c 6f 67 73 69 7a 65 20 3d 20 6d 65  .  iLogsize = me
7c170 6d 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43 54 52  m.aCtrl[i] & CTR
7c180 4c 5f 4c 4f 47 53 49 5a 45 3b 0a 20 20 73 69 7a  L_LOGSIZE;.  siz
7c190 65 20 3d 20 31 3c 3c 69 4c 6f 67 73 69 7a 65 3b  e = 1<<iLogsize;
7c1a0 0a 20 20 61 73 73 65 72 74 28 20 69 2b 73 69 7a  .  assert( i+siz
7c1b0 65 2d 31 3c 4e 42 4c 4f 43 4b 20 29 3b 0a 20 20  e-1<NBLOCK );.  
7c1c0 6d 65 6d 2e 61 43 74 72 6c 5b 69 5d 20 7c 3d 20  mem.aCtrl[i] |= 
7c1d0 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20 6d 65 6d  CTRL_FREE;.  mem
7c1e0 2e 61 43 74 72 6c 5b 69 2b 73 69 7a 65 2d 31 5d  .aCtrl[i+size-1]
7c1f0 20 7c 3d 20 43 54 52 4c 5f 46 52 45 45 3b 0a 20   |= CTRL_FREE;. 
7c200 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 63 75 72   assert( mem.cur
7c210 72 65 6e 74 43 6f 75 6e 74 3e 30 20 29 3b 0a 20  rentCount>0 );. 
7c220 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 63 75 72   assert( mem.cur
7c230 72 65 6e 74 4f 75 74 3e 3d 30 20 29 3b 0a 20 20  rentOut>=0 );.  
7c240 6d 65 6d 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74  mem.currentCount
7c250 2d 2d 3b 0a 20 20 6d 65 6d 2e 63 75 72 72 65 6e  --;.  mem.curren
7c260 74 4f 75 74 20 2d 3d 20 73 69 7a 65 2a 50 4f 57  tOut -= size*POW
7c270 32 5f 4d 49 4e 3b 0a 20 20 61 73 73 65 72 74 28  2_MIN;.  assert(
7c280 20 6d 65 6d 2e 63 75 72 72 65 6e 74 4f 75 74 3e   mem.currentOut>
7c290 30 20 7c 7c 20 6d 65 6d 2e 63 75 72 72 65 6e 74  0 || mem.current
7c2a0 43 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 61 73  Count==0 );.  as
7c2b0 73 65 72 74 28 20 6d 65 6d 2e 63 75 72 72 65 6e  sert( mem.curren
7c2c0 74 43 6f 75 6e 74 3e 30 20 7c 7c 20 6d 65 6d 2e  tCount>0 || mem.
7c2d0 63 75 72 72 65 6e 74 4f 75 74 3d 3d 30 20 29 3b  currentOut==0 );
7c2e0 0a 0a 20 20 6d 65 6d 2e 61 43 74 72 6c 5b 69 5d  ..  mem.aCtrl[i]
7c2f0 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69   = CTRL_FREE | i
7c300 4c 6f 67 73 69 7a 65 3b 0a 20 20 77 68 69 6c 65  Logsize;.  while
7c310 28 20 69 4c 6f 67 73 69 7a 65 3c 4e 53 49 5a 45  ( iLogsize<NSIZE
7c320 2d 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 42  -1 ){.    int iB
7c330 75 64 64 79 3b 0a 0a 20 20 20 20 69 66 28 20 28  uddy;..    if( (
7c340 69 3e 3e 69 4c 6f 67 73 69 7a 65 29 20 26 20 31  i>>iLogsize) & 1
7c350 20 29 7b 0a 20 20 20 20 20 20 69 42 75 64 64 79   ){.      iBuddy
7c360 20 3d 20 69 20 2d 20 73 69 7a 65 3b 0a 20 20 20   = i - size;.   
7c370 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 42   }else{.      iB
7c380 75 64 64 79 20 3d 20 69 20 2b 20 73 69 7a 65 3b  uddy = i + size;
7c390 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
7c3a0 74 28 20 69 42 75 64 64 79 3e 3d 30 20 26 26 20  t( iBuddy>=0 && 
7c3b0 69 42 75 64 64 79 3c 4e 42 4c 4f 43 4b 20 29 3b  iBuddy<NBLOCK );
7c3c0 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 61 43 74  .    if( mem.aCt
7c3d0 72 6c 5b 69 42 75 64 64 79 5d 21 3d 28 43 54 52  rl[iBuddy]!=(CTR
7c3e0 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a  L_FREE | iLogsiz
7c3f0 65 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  e) ) break;.    
7c400 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 42  memsys5Unlink(iB
7c410 75 64 64 79 2c 20 69 4c 6f 67 73 69 7a 65 29 3b  uddy, iLogsize);
7c420 0a 20 20 20 20 69 4c 6f 67 73 69 7a 65 2b 2b 3b  .    iLogsize++;
7c430 0a 20 20 20 20 69 66 28 20 69 42 75 64 64 79 3c  .    if( iBuddy<
7c440 69 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 61  i ){.      mem.a
7c450 43 74 72 6c 5b 69 42 75 64 64 79 5d 20 3d 20 43  Ctrl[iBuddy] = C
7c460 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73  TRL_FREE | iLogs
7c470 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 2e 61  ize;.      mem.a
7c480 43 74 72 6c 5b 69 5d 20 3d 20 30 3b 0a 20 20 20  Ctrl[i] = 0;.   
7c490 20 20 20 69 20 3d 20 69 42 75 64 64 79 3b 0a 20     i = iBuddy;. 
7c4a0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
7c4b0 6d 65 6d 2e 61 43 74 72 6c 5b 69 5d 20 3d 20 43  mem.aCtrl[i] = C
7c4c0 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73  TRL_FREE | iLogs
7c4d0 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 2e 61  ize;.      mem.a
7c4e0 43 74 72 6c 5b 69 42 75 64 64 79 5d 20 3d 20 30  Ctrl[iBuddy] = 0
7c4f0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 69 7a 65  ;.    }.    size
7c500 20 2a 3d 20 32 3b 0a 20 20 7d 0a 20 20 6d 65 6d   *= 2;.  }.  mem
7c510 73 79 73 35 4c 69 6e 6b 28 69 2c 20 69 4c 6f 67  sys5Link(i, iLog
7c520 73 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  size);.}../*.** 
7c530 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 73 20  Allocate nBytes 
7c540 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2f 0a 53 51 4c  of memory.*/.SQL
7c550 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71  ITE_API void *sq
7c560 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74  lite3_malloc(int
7c570 20 6e 42 79 74 65 73 29 7b 0a 20 20 73 71 6c 69   nBytes){.  sqli
7c580 74 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20 30  te3_int64 *p = 0
7c590 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3e 30  ;.  if( nBytes>0
7c5a0 20 29 7b 0a 20 20 20 20 6d 65 6d 73 79 73 35 45   ){.    memsys5E
7c5b0 6e 74 65 72 28 29 3b 0a 20 20 20 20 70 20 3d 20  nter();.    p = 
7c5c0 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 28 6e 42  memsys5Malloc(nB
7c5d0 79 74 65 73 29 3b 0a 20 20 20 20 73 71 6c 69 74  ytes);.    sqlit
7c5e0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
7c5f0 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20  em.mutex);.  }. 
7c600 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70   return (void*)p
7c610 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65  ; .}../*.** Free
7c620 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49   memory..*/.SQLI
7c630 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
7c640 74 65 33 5f 66 72 65 65 28 76 6f 69 64 20 2a 70  te3_free(void *p
7c650 50 72 69 6f 72 29 7b 0a 20 20 69 66 28 20 70 50  Prior){.  if( pP
7c660 72 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72  rior==0 ){.    r
7c670 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73  eturn;.  }.  ass
7c680 65 72 74 28 20 6d 65 6d 2e 6d 75 74 65 78 21 3d  ert( mem.mutex!=
7c690 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  0 );.  sqlite3_m
7c6a0 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d  utex_enter(mem.m
7c6b0 75 74 65 78 29 3b 0a 20 20 6d 65 6d 73 79 73 35  utex);.  memsys5
7c6c0 46 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20  Free(pPrior);.  
7c6d0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
7c6e0 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 20  ave(mem.mutex); 
7c6f0 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67   .}../*.** Chang
7c700 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e  e the size of an
7c710 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79   existing memory
7c720 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 53   allocation.*/.S
7c730 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
7c740 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
7c750 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e  void *pPrior, in
7c760 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 69 6e 74  t nBytes){.  int
7c770 20 6e 4f 6c 64 3b 0a 20 20 76 6f 69 64 20 2a 70   nOld;.  void *p
7c780 3b 0a 20 20 69 66 28 20 70 50 72 69 6f 72 3d 3d  ;.  if( pPrior==
7c790 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
7c7a0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
7c7b0 42 79 74 65 73 29 3b 0a 20 20 7d 0a 20 20 69 66  Bytes);.  }.  if
7c7c0 28 20 6e 42 79 74 65 73 3c 3d 30 20 29 7b 0a 20  ( nBytes<=0 ){. 
7c7d0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
7c7e0 70 50 72 69 6f 72 29 3b 0a 20 20 20 20 72 65 74  pPrior);.    ret
7c7f0 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73  urn 0;.  }.  ass
7c800 65 72 74 28 20 6d 65 6d 2e 6d 75 74 65 78 21 3d  ert( mem.mutex!=
7c810 30 20 29 3b 0a 20 20 6e 4f 6c 64 20 3d 20 73 71  0 );.  nOld = sq
7c820 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28  lite3MallocSize(
7c830 70 50 72 69 6f 72 29 3b 0a 20 20 69 66 28 20 6e  pPrior);.  if( n
7c840 42 79 74 65 73 3c 3d 6e 4f 6c 64 20 29 7b 0a 20  Bytes<=nOld ){. 
7c850 20 20 20 72 65 74 75 72 6e 20 70 50 72 69 6f 72     return pPrior
7c860 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
7c870 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e  mutex_enter(mem.
7c880 6d 75 74 65 78 29 3b 0a 20 20 70 20 3d 20 6d 65  mutex);.  p = me
7c890 6d 73 79 73 35 4d 61 6c 6c 6f 63 28 6e 42 79 74  msys5Malloc(nByt
7c8a0 65 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a  es);.  if( p ){.
7c8b0 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 50      memcpy(p, pP
7c8c0 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20 20  rior, nOld);.   
7c8d0 20 6d 65 6d 73 79 73 35 46 72 65 65 28 70 50 72   memsys5Free(pPr
7c8e0 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  ior);.  }.  sqli
7c8f0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
7c900 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65  mem.mutex);.  re
7c910 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
7c920 20 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 69   Open the file i
7c930 6e 64 69 63 61 74 65 64 20 61 6e 64 20 77 72 69  ndicated and wri
7c940 74 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20  te a log of all 
7c950 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a  unfreed memory .
7c960 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69  ** allocations i
7c970 6e 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f  nto that log..*/
7c980 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7c990 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64  void sqlite3Memd
7c9a0 65 62 75 67 44 75 6d 70 28 63 6f 6e 73 74 20 63  ebugDump(const c
7c9b0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b  har *zFilename){
7c9c0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
7c9d0 45 42 55 47 0a 20 20 46 49 4c 45 20 2a 6f 75 74  EBUG.  FILE *out
7c9e0 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 3b  ;.  int i, j, n;
7c9f0 0a 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d  ..  if( zFilenam
7ca00 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d  e==0 || zFilenam
7ca10 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 6f  e[0]==0 ){.    o
7ca20 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7d  ut = stdout;.  }
7ca30 65 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20 3d 20  else{.    out = 
7ca40 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c  fopen(zFilename,
7ca50 20 22 77 22 29 3b 0a 20 20 20 20 69 66 28 20 6f   "w");.    if( o
7ca60 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66  ut==0 ){.      f
7ca70 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22  printf(stderr, "
7ca80 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74  ** Unable to out
7ca90 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 75 67  put memory debug
7caa0 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 73 20   output log: %s 
7cab0 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  **\n",.         
7cac0 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 69               zFi
7cad0 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72  lename);.      r
7cae0 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d  eturn;.    }.  }
7caf0 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28  .  memsys5Enter(
7cb00 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
7cb10 4e 53 49 5a 45 3b 20 69 2b 2b 29 7b 0a 20 20 20  NSIZE; i++){.   
7cb20 20 66 6f 72 28 6e 3d 30 2c 20 6a 3d 6d 65 6d 2e   for(n=0, j=mem.
7cb30 61 69 46 72 65 65 6c 69 73 74 5b 69 5d 3b 20 6a  aiFreelist[i]; j
7cb40 3e 3d 30 3b 20 6a 20 3d 20 6d 65 6d 2e 61 50 6f  >=0; j = mem.aPo
7cb50 6f 6c 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78  ol[j].u.list.nex
7cb60 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20 66 70  t, n++){}.    fp
7cb70 72 69 6e 74 66 28 6f 75 74 2c 20 22 66 72 65 65  rintf(out, "free
7cb80 6c 69 73 74 20 69 74 65 6d 73 20 6f 66 20 73 69  list items of si
7cb90 7a 65 20 25 64 3a 20 25 64 5c 6e 22 2c 20 50 4f  ze %d: %d\n", PO
7cba0 57 32 5f 4d 49 4e 20 3c 3c 20 69 2c 20 6e 29 3b  W2_MIN << i, n);
7cbb0 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74 66 28 6f  .  }.  fprintf(o
7cbc0 75 74 2c 20 22 6d 65 6d 2e 6e 41 6c 6c 6f 63 20  ut, "mem.nAlloc 
7cbd0 20 20 20 20 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c        = %llu\n",
7cbe0 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 29 3b 0a 20 20   mem.nAlloc);.  
7cbf0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65  fprintf(out, "me
7cc00 6d 2e 74 6f 74 61 6c 41 6c 6c 6f 63 20 20 20 3d  m.totalAlloc   =
7cc10 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 2e 74 6f   %llu\n", mem.to
7cc20 74 61 6c 41 6c 6c 6f 63 29 3b 0a 20 20 66 70 72  talAlloc);.  fpr
7cc30 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 2e 74  intf(out, "mem.t
7cc40 6f 74 61 6c 45 78 63 65 73 73 20 20 3d 20 25 6c  otalExcess  = %l
7cc50 6c 75 5c 6e 22 2c 20 6d 65 6d 2e 74 6f 74 61 6c  lu\n", mem.total
7cc60 45 78 63 65 73 73 29 3b 0a 20 20 66 70 72 69 6e  Excess);.  fprin
7cc70 74 66 28 6f 75 74 2c 20 22 6d 65 6d 2e 63 75 72  tf(out, "mem.cur
7cc80 72 65 6e 74 4f 75 74 20 20 20 3d 20 25 75 5c 6e  rentOut   = %u\n
7cc90 22 2c 20 6d 65 6d 2e 63 75 72 72 65 6e 74 4f 75  ", mem.currentOu
7cca0 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75  t);.  fprintf(ou
7ccb0 74 2c 20 22 6d 65 6d 2e 63 75 72 72 65 6e 74 43  t, "mem.currentC
7ccc0 6f 75 6e 74 20 3d 20 25 75 5c 6e 22 2c 20 6d 65  ount = %u\n", me
7ccd0 6d 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 29 3b  m.currentCount);
7cce0 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20  .  fprintf(out, 
7ccf0 22 6d 65 6d 2e 6d 61 78 4f 75 74 20 20 20 20 20  "mem.maxOut     
7cd00 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 2e 6d    = %u\n", mem.m
7cd10 61 78 4f 75 74 29 3b 0a 20 20 66 70 72 69 6e 74  axOut);.  fprint
7cd20 66 28 6f 75 74 2c 20 22 6d 65 6d 2e 6d 61 78 43  f(out, "mem.maxC
7cd30 6f 75 6e 74 20 20 20 20 20 3d 20 25 75 5c 6e 22  ount     = %u\n"
7cd40 2c 20 6d 65 6d 2e 6d 61 78 43 6f 75 6e 74 29 3b  , mem.maxCount);
7cd50 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20  .  fprintf(out, 
7cd60 22 6d 65 6d 2e 6d 61 78 52 65 71 75 65 73 74 20  "mem.maxRequest 
7cd70 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 2e 6d    = %u\n", mem.m
7cd80 61 78 52 65 71 75 65 73 74 29 3b 0a 20 20 73 71  axRequest);.  sq
7cd90 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
7cda0 65 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20  e(mem.mutex);.  
7cdb0 69 66 28 20 6f 75 74 3d 3d 73 74 64 6f 75 74 20  if( out==stdout 
7cdc0 29 7b 0a 20 20 20 20 66 66 6c 75 73 68 28 73 74  ){.    fflush(st
7cdd0 64 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  dout);.  }else{.
7cde0 20 20 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b      fclose(out);
7cdf0 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 0a  .  }.#endif.}...
7ce00 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54  #endif /* !SQLIT
7ce10 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 49  E_POW2_MEMORY_SI
7ce20 5a 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ZE */../********
7ce30 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65  ****** End of me
7ce40 6d 35 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  m5.c ***********
7ce50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7ce60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7ce70 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
7ce80 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
7ce90 65 20 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a  e mutex.c ******
7cea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7ceb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7cec0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
7ced0 37 20 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a 2a  7 August 14.**.*
7cee0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
7cef0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
7cf00 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
7cf10 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
7cf20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
7cf30 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
7cf40 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
7cf50 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
7cf60 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
7cf70 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
7cf80 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
7cf90 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
7cfa0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
7cfb0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
7cfc0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
7cfd0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
7cfe0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
7cff0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
7d000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
7d040 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
7d050 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73   the C functions
7d060 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
7d070 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 54  mutexes..**.** T
7d080 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
7d090 6e 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 64  n in this file d
7d0a0 6f 65 73 20 6e 6f 74 20 70 72 6f 76 69 64 65 20  oes not provide 
7d0b0 61 6e 79 20 6d 75 74 75 61 6c 0a 2a 2a 20 65 78  any mutual.** ex
7d0c0 63 6c 75 73 69 6f 6e 20 61 6e 64 20 69 73 20 74  clusion and is t
7d0d0 68 75 73 20 73 75 69 74 61 62 6c 65 20 66 6f 72  hus suitable for
7d0e0 20 75 73 65 20 6f 6e 6c 79 20 69 6e 20 61 70 70   use only in app
7d0f0 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 74 68 61  lications.** tha
7d100 74 20 75 73 65 20 53 51 4c 69 74 65 20 69 6e 20  t use SQLite in 
7d110 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 2e  a single thread.
7d120 20 20 42 75 74 20 74 68 69 73 20 69 6d 70 6c 65    But this imple
7d130 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 64 6f 65  mentation.** doe
7d140 73 20 64 6f 20 61 20 6c 6f 74 20 6f 66 20 65 72  s do a lot of er
7d150 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20  ror checking on 
7d160 6d 75 74 65 78 65 73 20 74 6f 20 6d 61 6b 65 20  mutexes to make 
7d170 73 75 72 65 20 74 68 65 79 0a 2a 2a 20 61 72 65  sure they.** are
7d180 20 63 61 6c 6c 65 64 20 63 6f 72 72 65 63 74 6c   called correctl
7d190 79 20 61 6e 64 20 61 74 20 61 70 70 72 6f 70 72  y and at appropr
7d1a0 69 61 74 65 20 74 69 6d 65 73 2e 20 20 48 65 6e  iate times.  Hen
7d1b0 63 65 2c 20 74 68 69 73 0a 2a 2a 20 69 6d 70 6c  ce, this.** impl
7d1c0 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 73 75  ementation is su
7d1d0 69 74 61 62 6c 65 20 66 6f 72 20 74 65 73 74 69  itable for testi
7d1e0 6e 67 2e 0a 2a 2a 20 64 65 62 75 67 67 69 6e 67  ng..** debugging
7d1f0 20 70 75 72 70 6f 73 65 73 0a 2a 2a 0a 2a 2a 20   purposes.**.** 
7d200 24 49 64 3a 20 6d 75 74 65 78 2e 63 2c 76 20 31  $Id: mutex.c,v 1
7d210 2e 31 37 20 32 30 30 38 2f 30 33 2f 32 36 20 31  .17 2008/03/26 1
7d220 38 3a 33 34 3a 34 33 20 64 61 6e 69 65 6c 6b 31  8:34:43 danielk1
7d230 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69  977 Exp $.*/..#i
7d240 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45  fdef SQLITE_MUTE
7d250 58 5f 4e 4f 4f 50 5f 44 45 42 55 47 0a 2f 2a 0a  X_NOOP_DEBUG./*.
7d260 2a 2a 20 49 6e 20 74 68 69 73 20 69 6d 70 6c 65  ** In this imple
7d270 6d 65 6e 74 61 74 69 6f 6e 2c 20 6d 75 74 65 78  mentation, mutex
7d280 65 73 20 64 6f 20 6e 6f 74 20 70 72 6f 76 69 64  es do not provid
7d290 65 20 61 6e 79 20 6d 75 74 75 61 6c 20 65 78 63  e any mutual exc
7d2a0 6c 75 73 69 6f 6e 2e 0a 2a 2a 20 42 75 74 20 74  lusion..** But t
7d2b0 68 65 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e  he error checkin
7d2c0 67 20 69 73 20 70 72 6f 76 69 64 65 64 2e 20 20  g is provided.  
7d2d0 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74  This implementat
7d2e0 69 6f 6e 20 69 73 20 75 73 65 66 75 6c 0a 2a 2a  ion is useful.**
7d2f0 20 66 6f 72 20 74 65 73 74 20 70 75 72 70 6f 73   for test purpos
7d300 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  es..*/../*.** Th
7d310 65 20 6d 75 74 65 78 20 6f 62 6a 65 63 74 0a 2a  e mutex object.*
7d320 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  /.struct sqlite3
7d330 5f 6d 75 74 65 78 20 7b 0a 20 20 69 6e 74 20 69  _mutex {.  int i
7d340 64 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 75  d;     /* The mu
7d350 74 65 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e  tex type */.  in
7d360 74 20 63 6e 74 3b 20 20 20 20 2f 2a 20 4e 75 6d  t cnt;    /* Num
7d370 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 77  ber of entries w
7d380 69 74 68 6f 75 74 20 61 20 6d 61 74 63 68 69 6e  ithout a matchin
7d390 67 20 6c 65 61 76 65 20 2a 2f 0a 7d 3b 0a 0a 2f  g leave */.};../
7d3a0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
7d3b0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72  _mutex_alloc() r
7d3c0 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73  outine allocates
7d3d0 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20   a new.** mutex 
7d3e0 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f  and returns a po
7d3f0 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66  inter to it.  If
7d400 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c   it returns NULL
7d410 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74  .** that means t
7d420 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c  hat a mutex coul
7d430 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74  d not be allocat
7d440 65 64 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ed. .*/.SQLITE_A
7d450 50 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  PI sqlite3_mutex
7d460 20 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f   *sqlite3_mutex_
7d470 61 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 0a 20  alloc(int id){. 
7d480 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f   static sqlite3_
7d490 6d 75 74 65 78 20 61 53 74 61 74 69 63 5b 36 5d  mutex aStatic[6]
7d4a0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
7d4b0 78 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 73  x *pNew = 0;.  s
7d4c0 77 69 74 63 68 28 20 69 64 20 29 7b 0a 20 20 20  witch( id ){.   
7d4d0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54   case SQLITE_MUT
7d4e0 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 61 73  EX_FAST:.    cas
7d4f0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  e SQLITE_MUTEX_R
7d500 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 20  ECURSIVE: {.    
7d510 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
7d520 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a  _malloc(sizeof(*
7d530 70 4e 65 77 29 29 3b 0a 20 20 20 20 20 20 69 66  pNew));.      if
7d540 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20  ( pNew ){.      
7d550 20 20 70 4e 65 77 2d 3e 69 64 20 3d 20 69 64 3b    pNew->id = id;
7d560 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 63  .        pNew->c
7d570 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  nt = 0;.      }.
7d580 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
7d590 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20   }.    default: 
7d5a0 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
7d5b0 69 64 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20 20  id-2 >= 0 );.   
7d5c0 20 20 20 61 73 73 65 72 74 28 20 69 64 2d 32 20     assert( id-2 
7d5d0 3c 20 73 69 7a 65 6f 66 28 61 53 74 61 74 69 63  < sizeof(aStatic
7d5e0 29 2f 73 69 7a 65 6f 66 28 61 53 74 61 74 69 63  )/sizeof(aStatic
7d5f0 5b 30 5d 29 20 29 3b 0a 20 20 20 20 20 20 70 4e  [0]) );.      pN
7d600 65 77 20 3d 20 26 61 53 74 61 74 69 63 5b 69 64  ew = &aStatic[id
7d610 2d 32 5d 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d  -2];.      pNew-
7d620 3e 69 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20  >id = id;.      
7d630 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
7d640 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a  .  return pNew;.
7d650 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
7d660 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65  utine deallocate
7d670 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 20 61  s a previously a
7d680 6c 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 0a  llocated mutex..
7d690 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
7d6a0 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  id sqlite3_mutex
7d6b0 5f 66 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75  _free(sqlite3_mu
7d6c0 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  tex *p){.  asser
7d6d0 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74  t( p );.  assert
7d6e0 28 20 70 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a 20  ( p->cnt==0 );. 
7d6f0 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d   assert( p->id==
7d700 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
7d710 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  T || p->id==SQLI
7d720 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
7d730 56 45 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  VE );.  sqlite3_
7d740 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  free(p);.}../*.*
7d750 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
7d760 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20  tex_enter() and 
7d770 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
7d780 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74  y() routines att
7d790 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72  empt.** to enter
7d7a0 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e   a mutex.  If an
7d7b0 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20  other thread is 
7d7c0 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 74  already within t
7d7d0 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c  he mutex,.** sql
7d7e0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
7d7f0 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e  () will block an
7d800 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
7d810 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72  try() will retur
7d820 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59  n.** SQLITE_BUSY
7d830 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  .  The sqlite3_m
7d840 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72  utex_try() inter
7d850 66 61 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c  face returns SQL
7d860 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73  ITE_OK.** upon s
7d870 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e  uccessful entry.
7d880 20 20 4d 75 74 65 78 65 73 20 63 72 65 61 74 65    Mutexes create
7d890 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d  d using SQLITE_M
7d8a0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63  UTEX_RECURSIVE c
7d8b0 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64  an.** be entered
7d8c0 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20   multiple times 
7d8d0 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  by the same thre
7d8e0 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73  ad.  In such cas
7d8f0 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78  es the,.** mutex
7d900 20 6d 75 73 74 20 62 65 20 65 78 69 74 65 64 20   must be exited 
7d910 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20  an equal number 
7d920 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20  of times before 
7d930 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a  another thread.*
7d940 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66  * can enter.  If
7d950 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
7d960 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20   tries to enter 
7d970 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f  any other kind o
7d980 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20  f mutex.** more 
7d990 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62  than once, the b
7d9a0 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66  ehavior is undef
7d9b0 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ined..*/.SQLITE_
7d9c0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
7d9d0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c  _mutex_enter(sql
7d9e0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
7d9f0 20 20 61 73 73 65 72 74 28 20 70 20 29 3b 0a 20    assert( p );. 
7da00 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d   assert( p->id==
7da10 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
7da20 55 52 53 49 56 45 20 7c 7c 20 73 71 6c 69 74 65  URSIVE || sqlite
7da30 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28  3_mutex_notheld(
7da40 70 29 20 29 3b 0a 20 20 70 2d 3e 63 6e 74 2b 2b  p) );.  p->cnt++
7da50 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ;.}.SQLITE_API i
7da60 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  nt sqlite3_mutex
7da70 5f 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74  _try(sqlite3_mut
7da80 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  ex *p){.  assert
7da90 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ( p );.  assert(
7daa0 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d   p->id==SQLITE_M
7dab0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c  UTEX_RECURSIVE |
7dac0 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
7dad0 6e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20  notheld(p) );.  
7dae0 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 72 65 74 75  p->cnt++;.  retu
7daf0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
7db00 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
7db10 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29  e3_mutex_leave()
7db20 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61   routine exits a
7db30 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a   mutex that was.
7db40 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e  ** previously en
7db50 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d  tered by the sam
7db60 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62  e thread.  The b
7db70 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e  ehavior.** is un
7db80 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d  defined if the m
7db90 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72  utex is not curr
7dba0 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72  ently entered or
7dbb0 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65  .** is not curre
7dbc0 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20  ntly allocated. 
7dbd0 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76   SQLite will nev
7dbe0 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f  er do either..*/
7dbf0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
7dc00 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
7dc10 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  eave(sqlite3_mut
7dc20 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  ex *p){.  assert
7dc30 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ( p );.  assert(
7dc40 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
7dc50 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 63  eld(p) );.  p->c
7dc60 6e 74 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20  nt--;.  assert( 
7dc70 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
7dc80 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c  TEX_RECURSIVE ||
7dc90 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e   sqlite3_mutex_n
7dca0 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 7d 0a 0a  otheld(p) );.}..
7dcb0 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  /*.** The sqlite
7dcc0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61  3_mutex_held() a
7dcd0 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  nd sqlite3_mutex
7dce0 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69  _notheld() routi
7dcf0 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64  ne are.** intend
7dd00 65 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64  ed for use insid
7dd10 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65  e assert() state
7dd20 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ments..*/.SQLITE
7dd30 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
7dd40 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69  _mutex_held(sqli
7dd50 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
7dd60 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20   return p==0 || 
7dd70 70 2d 3e 63 6e 74 3e 30 3b 0a 7d 0a 53 51 4c 49  p->cnt>0;.}.SQLI
7dd80 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
7dd90 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64  e3_mutex_notheld
7dda0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
7ddb0 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d  p){.  return p==
7ddc0 30 20 7c 7c 20 70 2d 3e 63 6e 74 3d 3d 30 3b 0a  0 || p->cnt==0;.
7ddd0 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
7dde0 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 5f 44 45  TE_MUTEX_NOOP_DE
7ddf0 42 55 47 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  BUG */../*******
7de00 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d  ******* End of m
7de10 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  utex.c *********
7de20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7de30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7de40 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
7de50 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
7de60 6c 65 20 6d 75 74 65 78 5f 6f 73 32 2e 63 20 2a  le mutex_os2.c *
7de70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7de80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7de90 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
7dea0 30 37 20 41 75 67 75 73 74 20 32 38 0a 2a 2a 0a  07 August 28.**.
7deb0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
7dec0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
7ded0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
7dee0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
7def0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
7df00 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
7df10 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
7df20 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
7df30 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
7df40 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
7df50 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
7df60 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
7df70 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
7df80 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
7df90 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
7dfa0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
7dfb0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
7dfc0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
7dfd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7dfe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7dff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7e000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
7e010 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
7e020 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e  s the C function
7e030 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  s that implement
7e040 20 6d 75 74 65 78 65 73 20 66 6f 72 20 4f 53 2f   mutexes for OS/
7e050 32 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74  2.**.** $Id: mut
7e060 65 78 5f 6f 73 32 2e 63 2c 76 20 31 2e 36 20 32  ex_os2.c,v 1.6 2
7e070 30 30 38 2f 30 33 2f 32 36 20 31 38 3a 33 34 3a  008/03/26 18:34:
7e080 34 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45  43 danielk1977 E
7e090 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  xp $.*/../*.** T
7e0a0 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20  he code in this 
7e0b0 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75 73 65  file is only use
7e0c0 64 20 69 66 20 53 51 4c 49 54 45 5f 4d 55 54 45  d if SQLITE_MUTE
7e0d0 58 5f 4f 53 32 20 69 73 20 64 65 66 69 6e 65 64  X_OS2 is defined
7e0e0 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 6d 75 74  ..** See the mut
7e0f0 65 78 2e 68 20 66 69 6c 65 20 66 6f 72 20 64 65  ex.h file for de
7e100 74 61 69 6c 73 2e 0a 2a 2f 0a 23 69 66 64 65 66  tails..*/.#ifdef
7e110 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53   SQLITE_MUTEX_OS
7e120 32 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  2../************
7e130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4f 53 2f 32 20  ********** OS/2 
7e140 4d 75 74 65 78 20 49 6d 70 6c 65 6d 65 6e 74 61  Mutex Implementa
7e150 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tion ***********
7e160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
7e170 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74  * This implement
7e180 61 74 69 6f 6e 20 6f 66 20 6d 75 74 65 78 65 73  ation of mutexes
7e190 20 69 73 20 62 75 69 6c 74 20 75 73 69 6e 67 20   is built using 
7e1a0 74 68 65 20 4f 53 2f 32 20 41 50 49 2e 0a 2a 2f  the OS/2 API..*/
7e1b0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65  ../*.** The mute
7e1c0 78 20 6f 62 6a 65 63 74 0a 2a 2a 20 45 61 63 68  x object.** Each
7e1d0 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78   recursive mutex
7e1e0 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   is an instance 
7e1f0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
7e200 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73   structure..*/.s
7e210 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75  truct sqlite3_mu
7e220 74 65 78 20 7b 0a 20 20 48 4d 54 58 20 6d 75 74  tex {.  HMTX mut
7e230 65 78 3b 20 20 20 20 20 20 20 2f 2a 20 4d 75 74  ex;       /* Mut
7e240 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74  ex controlling t
7e250 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74  he lock */.  int
7e260 20 20 69 64 3b 20 20 20 20 20 20 20 20 20 20 2f    id;          /
7e270 2a 20 4d 75 74 65 78 20 74 79 70 65 20 2a 2f 0a  * Mutex type */.
7e280 20 20 69 6e 74 20 20 6e 52 65 66 3b 20 20 20 20    int  nRef;    
7e290 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
7e2a0 20 72 65 66 65 72 65 6e 63 65 73 20 2a 2f 0a 20   references */. 
7e2b0 20 54 49 44 20 20 6f 77 6e 65 72 3b 20 20 20 20   TID  owner;    
7e2c0 20 20 20 2f 2a 20 54 68 72 65 61 64 20 68 6f 6c     /* Thread hol
7e2d0 64 69 6e 67 20 74 68 69 73 20 6d 75 74 65 78 20  ding this mutex 
7e2e0 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 4f  */.};..#define O
7e2f0 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c  S2_MUTEX_INITIAL
7e300 49 5a 45 52 20 20 20 30 2c 30 2c 30 2c 30 0a 0a  IZER   0,0,0,0..
7e310 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  /*.** The sqlite
7e320 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20  3_mutex_alloc() 
7e330 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65  routine allocate
7e340 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78  s a new.** mutex
7e350 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70   and returns a p
7e360 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49  ointer to it.  I
7e370 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c  f it returns NUL
7e380 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20  L.** that means 
7e390 74 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75  that a mutex cou
7e3a0 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61  ld not be alloca
7e3b0 74 65 64 2e 20 0a 2a 2a 20 53 51 4c 69 74 65 20  ted. .** SQLite 
7e3c0 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20  will unwind its 
7e3d0 73 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e  stack and return
7e3e0 20 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20   an error.  The 
7e3f0 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73  argument.** to s
7e400 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
7e410 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 74  oc() is one of t
7e420 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e  hese integer con
7e430 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75  stants:.**.** <u
7e440 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  l>.** <li>  SQLI
7e450 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 20 20  TE_MUTEX_FAST   
7e460 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 2a 2a              0.**
7e470 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
7e480 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 20 20  TEX_RECURSIVE   
7e490 20 20 20 20 20 20 20 31 0a 2a 2a 20 3c 6c 69 3e         1.** <li>
7e4a0 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
7e4b0 54 41 54 49 43 5f 4d 41 53 54 45 52 20 20 20 20  TATIC_MASTER    
7e4c0 20 20 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c    2.** <li>  SQL
7e4d0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
7e4e0 5f 4d 45 4d 20 20 20 20 20 20 20 20 20 33 0a 2a  _MEM         3.*
7e4f0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
7e500 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47  UTEX_STATIC_PRNG
7e510 20 20 20 20 20 20 20 20 34 0a 2a 2a 20 3c 2f 75          4.** </u
7e520 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72  l>.**.** The fir
7e530 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73  st two constants
7e540 20 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d   cause sqlite3_m
7e550 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20  utex_alloc() to 
7e560 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20  create.** a new 
7e570 6d 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20  mutex.  The new 
7e580 6d 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69  mutex is recursi
7e590 76 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d  ve when SQLITE_M
7e5a0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a  UTEX_RECURSIVE.*
7e5b0 2a 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f  * is used but no
7e5c0 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f  t necessarily so
7e5d0 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54   when SQLITE_MUT
7e5e0 45 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e  EX_FAST is used.
7e5f0 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69 6d  .** The mutex im
7e600 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65  plementation doe
7e610 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61  s not need to ma
7e620 6b 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e  ke a distinction
7e630 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c 49  .** between SQLI
7e640 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
7e650 56 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55  VE and SQLITE_MU
7e660 54 45 58 5f 46 41 53 54 20 69 66 20 69 74 20 64  TEX_FAST if it d
7e670 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20  oes.** not want 
7e680 74 6f 2e 20 20 42 75 74 20 53 51 4c 69 74 65 20  to.  But SQLite 
7e690 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73  will only reques
7e6a0 74 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75  t a recursive mu
7e6b0 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20  tex in.** cases 
7e6c0 77 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20  where it really 
7e6d0 6e 65 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 61  needs one.  If a
7e6e0 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75   faster non-recu
7e6f0 72 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69  rsive mutex.** i
7e700 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73  mplementation is
7e710 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68   available on th
7e720 65 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c  e host platform,
7e730 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79   the mutex subsy
7e740 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65  stem.** might re
7e750 74 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65  turn such a mute
7e760 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f  x in response to
7e770 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
7e780 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74  ST..**.** The ot
7e790 68 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61  her allowed para
7e7a0 6d 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65  meters to sqlite
7e7b0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20  3_mutex_alloc() 
7e7c0 65 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61  each return.** a
7e7d0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74   pointer to a st
7e7e0 61 74 69 63 20 70 72 65 65 78 69 73 74 69 6e 67  atic preexisting
7e7f0 20 6d 75 74 65 78 2e 20 20 54 68 72 65 65 20 73   mutex.  Three s
7e800 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 72  tatic mutexes ar
7e810 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65  e.** used by the
7e820 20 63 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e   current version
7e830 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 46 75 74   of SQLite.  Fut
7e840 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  ure versions of 
7e850 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64  SQLite.** may ad
7e860 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61  d additional sta
7e870 74 69 63 20 6d 75 74 65 78 65 73 2e 20 20 53 74  tic mutexes.  St
7e880 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65  atic mutexes are
7e890 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a   for internal.**
7e8a0 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 20 6f   use by SQLite o
7e8b0 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 69 6f  nly.  Applicatio
7e8c0 6e 73 20 74 68 61 74 20 75 73 65 20 53 51 4c 69  ns that use SQLi
7e8d0 74 65 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c  te mutexes shoul
7e8e0 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68  d.** use only th
7e8f0 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65  e dynamic mutexe
7e900 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 51  s returned by SQ
7e910 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20  LITE_MUTEX_FAST 
7e920 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54  or.** SQLITE_MUT
7e930 45 58 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a  EX_RECURSIVE..**
7e940 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66  .** Note that if
7e950 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 79 6e 61   one of the dyna
7e960 6d 69 63 20 6d 75 74 65 78 20 70 61 72 61 6d 65  mic mutex parame
7e970 74 65 72 73 20 28 53 51 4c 49 54 45 5f 4d 55 54  ters (SQLITE_MUT
7e980 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51  EX_FAST.** or SQ
7e990 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
7e9a0 53 49 56 45 29 20 69 73 20 75 73 65 64 20 74 68  SIVE) is used th
7e9b0 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  en sqlite3_mutex
7e9c0 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75  _alloc().** retu
7e9d0 72 6e 73 20 61 20 64 69 66 66 65 72 65 6e 74 20  rns a different 
7e9e0 6d 75 74 65 78 20 6f 6e 20 65 76 65 72 79 20 63  mutex on every c
7e9f0 61 6c 6c 2e 20 20 42 75 74 20 66 6f 72 20 74 68  all.  But for th
7ea00 65 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 65  e static.** mute
7ea10 78 20 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d  x types, the sam
7ea20 65 20 6d 75 74 65 78 20 69 73 20 72 65 74 75 72  e mutex is retur
7ea30 6e 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c  ned on every cal
7ea40 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68  l that has.** th
7ea50 65 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62  e same type numb
7ea60 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  er..*/.SQLITE_AP
7ea70 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  I sqlite3_mutex 
7ea80 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61  *sqlite3_mutex_a
7ea90 6c 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b  lloc(int iType){
7eaa0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
7eab0 20 2a 70 20 3d 20 4e 55 4c 4c 3b 0a 20 20 73 77   *p = NULL;.  sw
7eac0 69 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20  itch( iType ){. 
7ead0 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d     case SQLITE_M
7eae0 55 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63  UTEX_FAST:.    c
7eaf0 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ase SQLITE_MUTEX
7eb00 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20  _RECURSIVE: {.  
7eb10 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d      p = sqlite3M
7eb20 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f  allocZero( sizeo
7eb30 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69  f(*p) );.      i
7eb40 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20  f( p ){.        
7eb50 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20  p->id = iType;. 
7eb60 20 20 20 20 20 20 20 69 66 28 20 44 6f 73 43 72         if( DosCr
7eb70 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20 30 2c  eateMutexSem( 0,
7eb80 20 26 70 2d 3e 6d 75 74 65 78 2c 20 30 2c 20 46   &p->mutex, 0, F
7eb90 41 4c 53 45 20 29 20 21 3d 20 4e 4f 5f 45 52 52  ALSE ) != NO_ERR
7eba0 4f 52 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OR ){.          
7ebb0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 20 70 20  sqlite3_free( p 
7ebc0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 20 3d  );.          p =
7ebd0 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 7d   NULL;.        }
7ebe0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
7ebf0 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
7ec00 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
7ec10 20 73 74 61 74 69 63 20 76 6f 6c 61 74 69 6c 65   static volatile
7ec20 20 69 6e 74 20 69 73 49 6e 69 74 20 3d 20 30 3b   int isInit = 0;
7ec30 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 73 71  .      static sq
7ec40 6c 69 74 65 33 5f 6d 75 74 65 78 20 73 74 61 74  lite3_mutex stat
7ec50 69 63 4d 75 74 65 78 65 73 5b 5d 20 3d 20 7b 0a  icMutexes[] = {.
7ec60 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55          { OS2_MU
7ec70 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c  TEX_INITIALIZER,
7ec80 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53   },.        { OS
7ec90 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  2_MUTEX_INITIALI
7eca0 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20  ZER, },.        
7ecb0 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54  { OS2_MUTEX_INIT
7ecc0 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20  IALIZER, },.    
7ecd0 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f      { OS2_MUTEX_
7ece0 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a  INITIALIZER, },.
7ecf0 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55          { OS2_MU
7ed00 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c  TEX_INITIALIZER,
7ed10 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53   },.        { OS
7ed20 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  2_MUTEX_INITIALI
7ed30 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 7d 3b  ZER, },.      };
7ed40 0a 20 20 20 20 20 20 69 66 20 28 20 21 69 73 49  .      if ( !isI
7ed50 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 41  nit ){.        A
7ed60 50 49 52 45 54 20 72 63 3b 0a 20 20 20 20 20 20  PIRET rc;.      
7ed70 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 20    PTIB ptib;.   
7ed80 20 20 20 20 20 50 50 49 42 20 70 70 69 62 3b 0a       PPIB ppib;.
7ed90 20 20 20 20 20 20 20 20 48 4d 54 58 20 6d 75 74          HMTX mut
7eda0 65 78 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72  ex;.        char
7edb0 20 6e 61 6d 65 5b 33 32 5d 3b 0a 20 20 20 20 20   name[32];.     
7edc0 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f     DosGetInfoBlo
7edd0 63 6b 73 28 20 26 70 74 69 62 2c 20 26 70 70 69  cks( &ptib, &ppi
7ede0 62 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  b );.        sql
7edf0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 73  ite3_snprintf( s
7ee00 69 7a 65 6f 66 28 6e 61 6d 65 29 2c 20 6e 61 6d  izeof(name), nam
7ee10 65 2c 20 22 5c 5c 53 45 4d 33 32 5c 5c 53 51 4c  e, "\\SEM32\\SQL
7ee20 49 54 45 25 30 34 78 22 2c 0a 20 20 20 20 20 20  ITE%04x",.      
7ee30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7ee40 20 20 20 20 70 70 69 62 2d 3e 70 69 62 5f 75 6c      ppib->pib_ul
7ee50 70 69 64 20 29 3b 0a 20 20 20 20 20 20 20 20 77  pid );.        w
7ee60 68 69 6c 65 28 20 21 69 73 49 6e 69 74 20 29 7b  hile( !isInit ){
7ee70 0a 20 20 20 20 20 20 20 20 20 20 6d 75 74 65 78  .          mutex
7ee80 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
7ee90 72 63 20 3d 20 44 6f 73 43 72 65 61 74 65 4d 75  rc = DosCreateMu
7eea0 74 65 78 53 65 6d 28 20 6e 61 6d 65 2c 20 26 6d  texSem( name, &m
7eeb0 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 29 3b  utex, 0, FALSE);
7eec0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72  .          if( r
7eed0 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b  c == NO_ERROR ){
7eee0 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74  .            int
7eef0 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   i;.            
7ef00 69 66 28 20 21 69 73 49 6e 69 74 20 29 7b 0a 20  if( !isInit ){. 
7ef10 20 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72               for
7ef20 28 20 69 20 3d 20 30 3b 20 69 20 3c 20 73 69 7a  ( i = 0; i < siz
7ef30 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 65  eof(staticMutexe
7ef40 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 74 69 63  s)/sizeof(static
7ef50 4d 75 74 65 78 65 73 5b 30 5d 29 3b 20 69 2b 2b  Mutexes[0]); i++
7ef60 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
7ef70 20 20 20 20 44 6f 73 43 72 65 61 74 65 4d 75 74      DosCreateMut
7ef80 65 78 53 65 6d 28 20 30 2c 20 26 73 74 61 74 69  exSem( 0, &stati
7ef90 63 4d 75 74 65 78 65 73 5b 69 5d 2e 6d 75 74 65  cMutexes[i].mute
7efa0 78 2c 20 30 2c 20 46 41 4c 53 45 20 29 3b 0a 20  x, 0, FALSE );. 
7efb0 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
7efc0 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 49               isI
7efd0 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  nit = 1;.       
7efe0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
7eff0 20 20 20 44 6f 73 43 6c 6f 73 65 4d 75 74 65 78     DosCloseMutex
7f000 53 65 6d 28 20 6d 75 74 65 78 20 29 3b 0a 20 20  Sem( mutex );.  
7f010 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
7f020 28 20 72 63 20 3d 3d 20 45 52 52 4f 52 5f 44 55  ( rc == ERROR_DU
7f030 50 4c 49 43 41 54 45 5f 4e 41 4d 45 20 29 7b 0a  PLICATE_NAME ){.
7f040 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 53              DosS
7f050 6c 65 65 70 28 20 31 20 29 3b 0a 20 20 20 20 20  leep( 1 );.     
7f060 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
7f070 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70          return p
7f080 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
7f090 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
7f0a0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 54        assert( iT
7f0b0 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20  ype-2 >= 0 );.  
7f0c0 20 20 20 20 61 73 73 65 72 74 28 20 69 54 79 70      assert( iTyp
7f0d0 65 2d 32 20 3c 20 73 69 7a 65 6f 66 28 73 74 61  e-2 < sizeof(sta
7f0e0 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69 7a 65  ticMutexes)/size
7f0f0 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73  of(staticMutexes
7f100 5b 30 5d 29 20 29 3b 0a 20 20 20 20 20 20 70 20  [0]) );.      p 
7f110 3d 20 26 73 74 61 74 69 63 4d 75 74 65 78 65 73  = &staticMutexes
7f120 5b 69 54 79 70 65 2d 32 5d 3b 0a 20 20 20 20 20  [iType-2];.     
7f130 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a   p->id = iType;.
7f140 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
7f150 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
7f160 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  p;.}.../*.** Thi
7f170 73 20 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f  s routine deallo
7f180 63 61 74 65 73 20 61 20 70 72 65 76 69 6f 75 73  cates a previous
7f190 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75 74  ly allocated mut
7f1a0 65 78 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 69 73  ex..** SQLite is
7f1b0 20 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c   careful to deal
7f1c0 6c 6f 63 61 74 65 20 65 76 65 72 79 20 6d 75 74  locate every mut
7f1d0 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 63  ex that it alloc
7f1e0 61 74 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ates..*/.SQLITE_
7f1f0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
7f200 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 71 6c 69  _mutex_free(sqli
7f210 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
7f220 20 61 73 73 65 72 74 28 20 70 20 29 3b 0a 20 20   assert( p );.  
7f230 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d  assert( p->nRef=
7f240 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
7f250 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
7f260 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69  TEX_FAST || p->i
7f270 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
7f280 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 44  RECURSIVE );.  D
7f290 6f 73 43 6c 6f 73 65 4d 75 74 65 78 53 65 6d 28  osCloseMutexSem(
7f2a0 20 70 2d 3e 6d 75 74 65 78 20 29 3b 0a 20 20 73   p->mutex );.  s
7f2b0 71 6c 69 74 65 33 5f 66 72 65 65 28 20 70 20 29  qlite3_free( p )
7f2c0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  ;.}../*.** The s
7f2d0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
7f2e0 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  er() and sqlite3
7f2f0 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75  _mutex_try() rou
7f300 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a  tines attempt.**
7f310 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65   to enter a mute
7f320 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74  x.  If another t
7f330 68 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79  hread is already
7f340 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65   within the mute
7f350 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75  x,.** sqlite3_mu
7f360 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c  tex_enter() will
7f370 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74   block and sqlit
7f380 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77  e3_mutex_try() w
7f390 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51  ill return.** SQ
7f3a0 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20  LITE_BUSY.  The 
7f3b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
7f3c0 79 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65  y() interface re
7f3d0 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a  turns SQLITE_OK.
7f3e0 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66  ** upon successf
7f3f0 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78  ul entry.  Mutex
7f400 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  es created using
7f410 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
7f420 43 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62  CURSIVE can.** b
7f430 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70  e entered multip
7f440 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20  le times by the 
7f450 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e  same thread.  In
7f460 20 73 75 63 68 20 63 61 73 65 73 20 74 68 65 2c   such cases the,
7f470 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62  .** mutex must b
7f480 65 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 61  e exited an equa
7f490 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65  l number of time
7f4a0 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72  s before another
7f4b0 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65   thread.** can e
7f4c0 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 73 61  nter.  If the sa
7f4d0 6d 65 20 74 68 72 65 61 64 20 74 72 69 65 73 20  me thread tries 
7f4e0 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68  to enter any oth
7f4f0 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78  er kind of mutex
7f500 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e  .** more than on
7f510 63 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72  ce, the behavior
7f520 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a   is undefined..*
7f530 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
7f540 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
7f550 65 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75  enter(sqlite3_mu
7f560 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20 74  tex *p){.  TID t
7f570 69 64 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65 72  id;.  PID holder
7f580 31 3b 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65  1;.  ULONG holde
7f590 72 32 3b 0a 20 20 61 73 73 65 72 74 28 20 70 20  r2;.  assert( p 
7f5a0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
7f5b0 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
7f5c0 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 73 71  _RECURSIVE || sq
7f5d0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68  lite3_mutex_noth
7f5e0 65 6c 64 28 70 29 20 29 3b 0a 20 20 44 6f 73 52  eld(p) );.  DosR
7f5f0 65 71 75 65 73 74 4d 75 74 65 78 53 65 6d 28 70  equestMutexSem(p
7f600 2d 3e 6d 75 74 65 78 2c 20 53 45 4d 5f 49 4e 44  ->mutex, SEM_IND
7f610 45 46 49 4e 49 54 45 5f 57 41 49 54 29 3b 0a 20  EFINITE_WAIT);. 
7f620 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65   DosQueryMutexSe
7f630 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c  m(p->mutex, &hol
7f640 64 65 72 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c  der1, &tid, &hol
7f650 64 65 72 32 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65  der2);.  p->owne
7f660 72 20 3d 20 74 69 64 3b 0a 20 20 70 2d 3e 6e 52  r = tid;.  p->nR
7f670 65 66 2b 2b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  ef++;.}.SQLITE_A
7f680 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d  PI int sqlite3_m
7f690 75 74 65 78 5f 74 72 79 28 73 71 6c 69 74 65 33  utex_try(sqlite3
7f6a0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e  _mutex *p){.  in
7f6b0 74 20 72 63 3b 0a 20 20 54 49 44 20 74 69 64 3b  t rc;.  TID tid;
7f6c0 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a  .  PID holder1;.
7f6d0 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b    ULONG holder2;
7f6e0 0a 20 20 61 73 73 65 72 74 28 20 70 20 29 3b 0a  .  assert( p );.
7f6f0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d    assert( p->id=
7f700 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45  =SQLITE_MUTEX_RE
7f710 43 55 52 53 49 56 45 20 7c 7c 20 73 71 6c 69 74  CURSIVE || sqlit
7f720 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64  e3_mutex_notheld
7f730 28 70 29 20 29 3b 0a 20 20 69 66 28 20 44 6f 73  (p) );.  if( Dos
7f740 52 65 71 75 65 73 74 4d 75 74 65 78 53 65 6d 28  RequestMutexSem(
7f750 70 2d 3e 6d 75 74 65 78 2c 20 53 45 4d 5f 49 4d  p->mutex, SEM_IM
7f760 4d 45 44 49 41 54 45 5f 52 45 54 55 52 4e 29 20  MEDIATE_RETURN) 
7f770 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 20 7b 0a 20  == NO_ERROR) {. 
7f780 20 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78     DosQueryMutex
7f790 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 68  Sem(p->mutex, &h
7f7a0 6f 6c 64 65 72 31 2c 20 26 74 69 64 2c 20 26 68  older1, &tid, &h
7f7b0 6f 6c 64 65 72 32 29 3b 0a 20 20 20 20 70 2d 3e  older2);.    p->
7f7c0 6f 77 6e 65 72 20 3d 20 74 69 64 3b 0a 20 20 20  owner = tid;.   
7f7d0 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20   p->nRef++;.    
7f7e0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
7f7f0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 72    } else {.    r
7f800 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  c = SQLITE_BUSY;
7f810 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
7f820 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  c;.}../*.** The 
7f830 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
7f840 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78  ave() routine ex
7f850 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74  its a mutex that
7f860 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73   was.** previous
7f870 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20 74 68  ly entered by th
7f880 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20  e same thread.  
7f890 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20  The behavior.** 
7f8a0 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20  is undefined if 
7f8b0 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74  the mutex is not
7f8c0 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72   currently enter
7f8d0 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20  ed or.** is not 
7f8e0 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61  currently alloca
7f8f0 74 65 64 2e 20 20 53 51 4c 69 74 65 20 77 69 6c  ted.  SQLite wil
7f900 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65  l never do eithe
7f910 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  r..*/.SQLITE_API
7f920 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75   void sqlite3_mu
7f930 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65  tex_leave(sqlite
7f940 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54  3_mutex *p){.  T
7f950 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 68 6f  ID tid;.  PID ho
7f960 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e 47 20 68  lder1;.  ULONG h
7f970 6f 6c 64 65 72 32 3b 0a 20 20 61 73 73 65 72 74  older2;.  assert
7f980 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20  ( p->nRef>0 );. 
7f990 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65   DosQueryMutexSe
7f9a0 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c  m(p->mutex, &hol
7f9b0 64 65 72 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c  der1, &tid, &hol
7f9c0 64 65 72 32 29 3b 0a 20 20 61 73 73 65 72 74 28  der2);.  assert(
7f9d0 20 70 2d 3e 6f 77 6e 65 72 3d 3d 74 69 64 20 29   p->owner==tid )
7f9e0 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20  ;.  p->nRef--;. 
7f9f0 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66   assert( p->nRef
7fa00 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51  ==0 || p->id==SQ
7fa10 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
7fa20 53 49 56 45 20 29 3b 0a 20 20 44 6f 73 52 65 6c  SIVE );.  DosRel
7fa30 65 61 73 65 4d 75 74 65 78 53 65 6d 28 70 2d 3e  easeMutexSem(p->
7fa40 6d 75 74 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  mutex);.}../*.**
7fa50 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
7fa60 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71  ex_held() and sq
7fa70 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68  lite3_mutex_noth
7fa80 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72  eld() routine ar
7fa90 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f  e.** intended fo
7faa0 72 20 75 73 65 20 69 6e 73 69 64 65 20 61 73 73  r use inside ass
7fab0 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73  ert() statements
7fac0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
7fad0 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  int sqlite3_mute
7fae0 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d  x_held(sqlite3_m
7faf0 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20  utex *p){.  TID 
7fb00 74 69 64 3b 0a 20 20 50 49 44 20 70 69 64 3b 0a  tid;.  PID pid;.
7fb10 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b    ULONG ulCount;
7fb20 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20  .  PTIB ptib;.  
7fb30 69 66 28 20 70 21 3d 30 20 29 20 7b 0a 20 20 20  if( p!=0 ) {.   
7fb40 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65   DosQueryMutexSe
7fb50 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 70 69 64  m(p->mutex, &pid
7fb60 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f 75 6e 74  , &tid, &ulCount
7fb70 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20  );.  } else {.  
7fb80 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63    DosGetInfoBloc
7fb90 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b  ks(&ptib, NULL);
7fba0 0a 20 20 20 20 74 69 64 20 3d 20 70 74 69 62 2d  .    tid = ptib-
7fbb0 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32  >tib_ptib2->tib2
7fbc0 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 20 72 65  _ultid;.  }.  re
7fbd0 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 28 70 2d  turn p==0 || (p-
7fbe0 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 2d 3e 6f  >nRef!=0 && p->o
7fbf0 77 6e 65 72 3d 3d 74 69 64 29 3b 0a 7d 0a 53 51  wner==tid);.}.SQ
7fc00 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
7fc10 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65  ite3_mutex_nothe
7fc20 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ld(sqlite3_mutex
7fc30 20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b   *p){.  TID tid;
7fc40 0a 20 20 50 49 44 20 70 69 64 3b 0a 20 20 55 4c  .  PID pid;.  UL
7fc50 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b 0a 20 20 50  ONG ulCount;.  P
7fc60 54 49 42 20 70 74 69 62 3b 0a 20 20 69 66 28 20  TIB ptib;.  if( 
7fc70 70 21 3d 20 30 20 29 20 7b 0a 20 20 20 20 44 6f  p!= 0 ) {.    Do
7fc80 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70  sQueryMutexSem(p
7fc90 2d 3e 6d 75 74 65 78 2c 20 26 70 69 64 2c 20 26  ->mutex, &pid, &
7fca0 74 69 64 2c 20 26 75 6c 43 6f 75 6e 74 29 3b 0a  tid, &ulCount);.
7fcb0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 44    } else {.    D
7fcc0 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28  osGetInfoBlocks(
7fcd0 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20  &ptib, NULL);.  
7fce0 20 20 74 69 64 20 3d 20 70 74 69 62 2d 3e 74 69    tid = ptib->ti
7fcf0 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c  b_ptib2->tib2_ul
7fd00 74 69 64 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  tid;.  }.  retur
7fd10 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 52 65  n p==0 || p->nRe
7fd20 66 3d 3d 30 20 7c 7c 20 70 2d 3e 6f 77 6e 65 72  f==0 || p->owner
7fd30 21 3d 74 69 64 3b 0a 7d 0a 23 65 6e 64 69 66 20  !=tid;.}.#endif 
7fd40 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  /* SQLITE_MUTEX_
7fd50 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  OS2 */../*******
7fd60 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d  ******* End of m
7fd70 75 74 65 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a  utex_os2.c *****
7fd80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7fd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7fda0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
7fdb0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
7fdc0 6c 65 20 6d 75 74 65 78 5f 75 6e 69 78 2e 63 20  le mutex_unix.c 
7fdd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7fde0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7fdf0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
7fe00 30 37 20 41 75 67 75 73 74 20 32 38 0a 2a 2a 0a  07 August 28.**.
7fe10 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
7fe20 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
7fe30 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
7fe40 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
7fe50 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
7fe60 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
7fe70 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
7fe80 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
7fe90 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
7fea0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
7feb0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
7fec0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
7fed0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
7fee0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
7fef0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
7ff00 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
7ff10 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
7ff20 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
7ff30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7ff40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7ff50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7ff60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
7ff70 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
7ff80 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e  s the C function
7ff90 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  s that implement
7ffa0 20 6d 75 74 65 78 65 73 20 66 6f 72 20 70 74 68   mutexes for pth
7ffb0 72 65 61 64 73 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  reads.**.** $Id:
7ffc0 20 6d 75 74 65 78 5f 75 6e 69 78 2e 63 2c 76 20   mutex_unix.c,v 
7ffd0 31 2e 37 20 32 30 30 38 2f 30 33 2f 32 39 20 31  1.7 2008/03/29 1
7ffe0 32 3a 34 37 3a 32 37 20 72 73 65 20 45 78 70 20  2:47:27 rse Exp 
7fff0 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  $.*/../*.** The 
80000 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c  code in this fil
80010 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69  e is only used i
80020 66 20 77 65 20 61 72 65 20 63 6f 6d 70 69 6c 69  f we are compili
80030 6e 67 20 74 68 72 65 61 64 73 61 66 65 0a 2a 2a  ng threadsafe.**
80040 20 75 6e 64 65 72 20 75 6e 69 78 20 77 69 74 68   under unix with
80050 20 70 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a   pthreads..**.**
80060 20 4e 6f 74 65 20 74 68 61 74 20 74 68 69 73 20   Note that this 
80070 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 72  implementation r
80080 65 71 75 69 72 65 73 20 61 20 76 65 72 73 69 6f  equires a versio
80090 6e 20 6f 66 20 70 74 68 72 65 61 64 73 20 74 68  n of pthreads th
800a0 61 74 0a 2a 2a 20 73 75 70 70 6f 72 74 73 20 72  at.** supports r
800b0 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73  ecursive mutexes
800c0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
800d0 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44  TE_MUTEX_PTHREAD
800e0 53 0a 0a 23 69 6e 63 6c 75 64 65 20 3c 70 74 68  S..#include <pth
800f0 72 65 61 64 2e 68 3e 0a 0a 0a 2f 2a 0a 2a 2a 20  read.h>.../*.** 
80100 45 61 63 68 20 72 65 63 75 72 73 69 76 65 20 6d  Each recursive m
80110 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61  utex is an insta
80120 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
80130 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a  wing structure..
80140 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  */.struct sqlite
80150 33 5f 6d 75 74 65 78 20 7b 0a 20 20 70 74 68 72  3_mutex {.  pthr
80160 65 61 64 5f 6d 75 74 65 78 5f 74 20 6d 75 74 65  ead_mutex_t mute
80170 78 3b 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20  x;     /* Mutex 
80180 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20  controlling the 
80190 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 64  lock */.  int id
801a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
801b0 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79       /* Mutex ty
801c0 70 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66  pe */.  int nRef
801d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
801e0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
801f0 65 6e 74 72 61 6e 63 65 73 20 2a 2f 0a 20 20 70  entrances */.  p
80200 74 68 72 65 61 64 5f 74 20 6f 77 6e 65 72 3b 20  thread_t owner; 
80210 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 72            /* Thr
80220 65 61 64 20 74 68 61 74 20 69 73 20 77 69 74 68  ead that is with
80230 69 6e 20 74 68 69 73 20 6d 75 74 65 78 20 2a 2f  in this mutex */
80240 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
80250 45 42 55 47 0a 20 20 69 6e 74 20 74 72 61 63 65  EBUG.  int trace
80260 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
80270 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 61    /* True to tra
80280 63 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 65  ce changes */.#e
80290 6e 64 69 66 0a 7d 3b 0a 23 69 66 64 65 66 20 53  ndif.};.#ifdef S
802a0 51 4c 49 54 45 5f 44 45 42 55 47 0a 23 64 65 66  QLITE_DEBUG.#def
802b0 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 45  ine SQLITE3_MUTE
802c0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20  X_INITIALIZER { 
802d0 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 4e  PTHREAD_MUTEX_IN
802e0 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30 2c  ITIALIZER, 0, 0,
802f0 20 28 70 74 68 72 65 61 64 5f 74 29 30 2c 20 30   (pthread_t)0, 0
80300 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65   }.#else.#define
80310 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49   SQLITE3_MUTEX_I
80320 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20 50 54 48  NITIALIZER { PTH
80330 52 45 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54 49  READ_MUTEX_INITI
80340 41 4c 49 5a 45 52 2c 20 30 2c 20 30 2c 20 28 70  ALIZER, 0, 0, (p
80350 74 68 72 65 61 64 5f 74 29 30 20 7d 0a 23 65 6e  thread_t)0 }.#en
80360 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  dif../*.** The s
80370 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
80380 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c  oc() routine all
80390 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20  ocates a new.** 
803a0 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e  mutex and return
803b0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  s a pointer to i
803c0 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 6e  t.  If it return
803d0 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d  s NULL.** that m
803e0 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65  eans that a mute
803f0 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61  x could not be a
80400 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74  llocated.  SQLit
80410 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e 64  e.** will unwind
80420 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72   its stack and r
80430 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20  eturn an error. 
80440 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a   The argument.**
80450 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   to sqlite3_mute
80460 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65  x_alloc() is one
80470 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65   of these intege
80480 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a  r constants:.**.
80490 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20  ** <ul>.** <li> 
804a0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
804b0 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  ST.** <li>  SQLI
804c0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
804d0 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  VE.** <li>  SQLI
804e0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
804f0 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20  MASTER.** <li>  
80500 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
80510 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20  TIC_MEM.** <li> 
80520 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
80530 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69  ATIC_MEM2.** <li
80540 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
80550 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c  STATIC_PRNG.** <
80560 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
80570 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a 20  X_STATIC_LRU.** 
80580 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  </ul>.**.** The 
80590 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 61  first two consta
805a0 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 74 65  nts cause sqlite
805b0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20  3_mutex_alloc() 
805c0 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e  to create.** a n
805d0 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20 6e  ew mutex.  The n
805e0 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63 75  ew mutex is recu
805f0 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49 54  rsive when SQLIT
80600 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
80610 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75 74  E.** is used but
80620 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79   not necessarily
80630 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 5f   so when SQLITE_
80640 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75 73  MUTEX_FAST is us
80650 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78  ed..** The mutex
80660 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
80670 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f  does not need to
80680 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74   make a distinct
80690 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53  ion.** between S
806a0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
806b0 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45  RSIVE and SQLITE
806c0 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20 69  _MUTEX_FAST if i
806d0 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61  t does.** not wa
806e0 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 4c 69  nt to.  But SQLi
806f0 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71  te will only req
80700 75 65 73 74 20 61 20 72 65 63 75 72 73 69 76 65  uest a recursive
80710 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73   mutex in.** cas
80720 65 73 20 77 68 65 72 65 20 69 74 20 72 65 61 6c  es where it real
80730 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 49  ly needs one.  I
80740 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72  f a faster non-r
80750 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a 2a  ecursive mutex.*
80760 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
80770 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e   is available on
80780 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66 6f   the host platfo
80790 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73 75  rm, the mutex su
807a0 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74  bsystem.** might
807b0 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d   return such a m
807c0 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65  utex in response
807d0 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58   to SQLITE_MUTEX
807e0 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  _FAST..**.** The
807f0 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 70   other allowed p
80800 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71 6c  arameters to sql
80810 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
80820 28 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a 2a  () each return.*
80830 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  * a pointer to a
80840 20 73 74 61 74 69 63 20 70 72 65 65 78 69 73 74   static preexist
80850 69 6e 67 20 6d 75 74 65 78 2e 20 20 54 68 72 65  ing mutex.  Thre
80860 65 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73  e static mutexes
80870 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20   are.** used by 
80880 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73  the current vers
80890 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20  ion of SQLite.  
808a0 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20  Future versions 
808b0 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79  of SQLite.** may
808c0 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20   add additional 
808d0 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20  static mutexes. 
808e0 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20   Static mutexes 
808f0 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c  are for internal
80900 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74  .** use by SQLit
80910 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61  e only.  Applica
80920 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53  tions that use S
80930 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68  QLite mutexes sh
80940 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79  ould.** use only
80950 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74   the dynamic mut
80960 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79  exes returned by
80970 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
80980 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f  ST or.** SQLITE_
80990 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e  MUTEX_RECURSIVE.
809a0 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  .**.** Note that
809b0 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 64   if one of the d
809c0 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 72  ynamic mutex par
809d0 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 5f  ameters (SQLITE_
809e0 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72  MUTEX_FAST.** or
809f0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
80a00 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65 64  CURSIVE) is used
80a10 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75   then sqlite3_mu
80a20 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72  tex_alloc().** r
80a30 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72 65  eturns a differe
80a40 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72  nt mutex on ever
80a50 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f 72  y call.  But for
80a60 20 74 68 65 20 73 74 61 74 69 63 20 0a 2a 2a 20   the static .** 
80a70 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65  mutex types, the
80a80 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72   same mutex is r
80a90 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79  eturned on every
80aa0 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a   call that has.*
80ab0 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20  * the same type 
80ac0 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  number..*/.SQLIT
80ad0 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 6d 75  E_API sqlite3_mu
80ae0 74 65 78 20 2a 73 71 6c 69 74 65 33 5f 6d 75 74  tex *sqlite3_mut
80af0 65 78 5f 61 6c 6c 6f 63 28 69 6e 74 20 69 54 79  ex_alloc(int iTy
80b00 70 65 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71  pe){.  static sq
80b10 6c 69 74 65 33 5f 6d 75 74 65 78 20 73 74 61 74  lite3_mutex stat
80b20 69 63 4d 75 74 65 78 65 73 5b 5d 20 3d 20 7b 0a  icMutexes[] = {.
80b30 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45      SQLITE3_MUTE
80b40 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20  X_INITIALIZER,. 
80b50 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58     SQLITE3_MUTEX
80b60 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20 20  _INITIALIZER,.  
80b70 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f    SQLITE3_MUTEX_
80b80 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20 20 20  INITIALIZER,.   
80b90 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49   SQLITE3_MUTEX_I
80ba0 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20  NITIALIZER,.    
80bb0 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e  SQLITE3_MUTEX_IN
80bc0 49 54 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53  ITIALIZER,.    S
80bd0 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49  QLITE3_MUTEX_INI
80be0 54 49 41 4c 49 5a 45 52 0a 20 20 7d 3b 0a 20 20  TIALIZER.  };.  
80bf0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
80c00 3b 0a 20 20 73 77 69 74 63 68 28 20 69 54 79 70  ;.  switch( iTyp
80c10 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  e ){.    case SQ
80c20 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
80c30 53 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 20  SIVE: {.      p 
80c40 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a  = sqlite3MallocZ
80c50 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20  ero( sizeof(*p) 
80c60 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20 29  );.      if( p )
80c70 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  {.#ifdef SQLITE_
80c80 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 53  HOMEGROWN_RECURS
80c90 49 56 45 5f 4d 55 54 45 58 0a 20 20 20 20 20 20  IVE_MUTEX.      
80ca0 20 20 2f 2a 20 49 66 20 72 65 63 75 72 73 69 76    /* If recursiv
80cb0 65 20 6d 75 74 65 78 65 73 20 61 72 65 20 6e 6f  e mutexes are no
80cc0 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 77 65 20  t available, we 
80cd0 77 69 6c 6c 20 68 61 76 65 20 74 6f 0a 20 20 20  will have to.   
80ce0 20 20 20 20 20 2a 2a 20 62 75 69 6c 64 20 6f 75       ** build ou
80cf0 72 20 6f 77 6e 2e 20 20 53 65 65 20 62 65 6c 6f  r own.  See belo
80d00 77 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 74  w. */.        pt
80d10 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e 69 74  hread_mutex_init
80d20 28 26 70 2d 3e 6d 75 74 65 78 2c 20 30 29 3b 0a  (&p->mutex, 0);.
80d30 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 2f 2a  #else.        /*
80d40 20 55 73 65 20 61 20 72 65 63 75 72 73 69 76 65   Use a recursive
80d50 20 6d 75 74 65 78 20 69 66 20 69 74 20 69 73 20   mutex if it is 
80d60 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 20 20 20  available */.   
80d70 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74       pthread_mut
80d80 65 78 61 74 74 72 5f 74 20 72 65 63 75 72 73 69  exattr_t recursi
80d90 76 65 41 74 74 72 3b 0a 20 20 20 20 20 20 20 20  veAttr;.        
80da0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74  pthread_mutexatt
80db0 72 5f 69 6e 69 74 28 26 72 65 63 75 72 73 69 76  r_init(&recursiv
80dc0 65 41 74 74 72 29 3b 0a 20 20 20 20 20 20 20 20  eAttr);.        
80dd0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74  pthread_mutexatt
80de0 72 5f 73 65 74 74 79 70 65 28 26 72 65 63 75 72  r_settype(&recur
80df0 73 69 76 65 41 74 74 72 2c 20 50 54 48 52 45 41  siveAttr, PTHREA
80e00 44 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  D_MUTEX_RECURSIV
80e10 45 29 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72  E);.        pthr
80e20 65 61 64 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26  ead_mutex_init(&
80e30 70 2d 3e 6d 75 74 65 78 2c 20 26 72 65 63 75 72  p->mutex, &recur
80e40 73 69 76 65 41 74 74 72 29 3b 0a 20 20 20 20 20  siveAttr);.     
80e50 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78     pthread_mutex
80e60 61 74 74 72 5f 64 65 73 74 72 6f 79 28 26 72 65  attr_destroy(&re
80e70 63 75 72 73 69 76 65 41 74 74 72 29 3b 0a 23 65  cursiveAttr);.#e
80e80 6e 64 69 66 0a 20 20 20 20 20 20 20 20 70 2d 3e  ndif.        p->
80e90 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20  id = iType;.    
80ea0 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
80eb0 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
80ec0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
80ed0 54 3a 20 7b 0a 20 20 20 20 20 20 70 20 3d 20 73  T: {.      p = s
80ee0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
80ef0 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a  ( sizeof(*p) );.
80f00 20 20 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20        if( p ){. 
80f10 20 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69         p->id = i
80f20 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 70 74  Type;.        pt
80f30 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e 69 74  hread_mutex_init
80f40 28 26 70 2d 3e 6d 75 74 65 78 2c 20 30 29 3b 0a  (&p->mutex, 0);.
80f50 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
80f60 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  eak;.    }.    d
80f70 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
80f80 61 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20  assert( iType-2 
80f90 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61 73  >= 0 );.      as
80fa0 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3c 20  sert( iType-2 < 
80fb0 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74  sizeof(staticMut
80fc0 65 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61  exes)/sizeof(sta
80fd0 74 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 20 29  ticMutexes[0]) )
80fe0 3b 0a 20 20 20 20 20 20 70 20 3d 20 26 73 74 61  ;.      p = &sta
80ff0 74 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65  ticMutexes[iType
81000 2d 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64  -2];.      p->id
81010 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20   = iType;.      
81020 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
81030 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
81040 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
81050 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20  ine deallocates 
81060 61 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20  a previously.** 
81070 61 6c 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e  allocated mutex.
81080 20 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 65    SQLite is care
81090 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74  ful to deallocat
810a0 65 20 65 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78  e every.** mutex
810b0 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 74   that it allocat
810c0 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  es..*/.SQLITE_AP
810d0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d  I void sqlite3_m
810e0 75 74 65 78 5f 66 72 65 65 28 73 71 6c 69 74 65  utex_free(sqlite
810f0 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61  3_mutex *p){.  a
81100 73 73 65 72 74 28 20 70 20 29 3b 0a 20 20 61 73  ssert( p );.  as
81110 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30  sert( p->nRef==0
81120 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
81130 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45  >id==SQLITE_MUTE
81140 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d  X_FAST || p->id=
81150 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45  =SQLITE_MUTEX_RE
81160 43 55 52 53 49 56 45 20 29 3b 0a 20 20 70 74 68  CURSIVE );.  pth
81170 72 65 61 64 5f 6d 75 74 65 78 5f 64 65 73 74 72  read_mutex_destr
81180 6f 79 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20  oy(&p->mutex);. 
81190 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29   sqlite3_free(p)
811a0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  ;.}../*.** The s
811b0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
811c0 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  er() and sqlite3
811d0 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75  _mutex_try() rou
811e0 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a  tines attempt.**
811f0 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65   to enter a mute
81200 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74  x.  If another t
81210 68 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79  hread is already
81220 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65   within the mute
81230 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75  x,.** sqlite3_mu
81240 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c  tex_enter() will
81250 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74   block and sqlit
81260 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77  e3_mutex_try() w
81270 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51  ill return.** SQ
81280 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20  LITE_BUSY.  The 
81290 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
812a0 79 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65  y() interface re
812b0 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a  turns SQLITE_OK.
812c0 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66  ** upon successf
812d0 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78  ul entry.  Mutex
812e0 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  es created using
812f0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
81300 43 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62  CURSIVE can.** b
81310 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70  e entered multip
81320 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20  le times by the 
81330 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e  same thread.  In
81340 20 73 75 63 68 20 63 61 73 65 73 20 74 68 65 2c   such cases the,
81350 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62  .** mutex must b
81360 65 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 61  e exited an equa
81370 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65  l number of time
81380 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72  s before another
81390 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65   thread.** can e
813a0 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 73 61  nter.  If the sa
813b0 6d 65 20 74 68 72 65 61 64 20 74 72 69 65 73 20  me thread tries 
813c0 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68  to enter any oth
813d0 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78  er kind of mutex
813e0 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e  .** more than on
813f0 63 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72  ce, the behavior
81400 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a   is undefined..*
81410 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
81420 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
81430 65 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75  enter(sqlite3_mu
81440 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  tex *p){.  asser
81450 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74  t( p );.  assert
81460 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f  ( p->id==SQLITE_
81470 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20  MUTEX_RECURSIVE 
81480 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
81490 5f 6e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 0a  _notheld(p) );..
814a0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f  #ifdef SQLITE_HO
814b0 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56  MEGROWN_RECURSIV
814c0 45 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49 66 20  E_MUTEX.  /* If 
814d0 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65  recursive mutexe
814e0 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61  s are not availa
814f0 62 6c 65 2c 20 74 68 65 6e 20 77 65 20 68 61 76  ble, then we hav
81500 65 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a 20 6f  e to grow.  ** o
81510 75 72 20 6f 77 6e 2e 20 20 54 68 69 73 20 69 6d  ur own.  This im
81520 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73  plementation ass
81530 75 6d 65 73 20 74 68 61 74 20 70 74 68 72 65 61  umes that pthrea
81540 64 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a 20 69  d_equal().  ** i
81550 73 20 61 74 6f 6d 69 63 20 2d 20 74 68 61 74 20  s atomic - that 
81560 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 65 63  it cannot be dec
81570 65 69 76 65 64 20 69 6e 74 6f 20 74 68 69 6e 6b  eived into think
81580 69 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20 61 6e  ing self.  ** an
81590 64 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65 20 65  d p->owner are e
815a0 71 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e 65 72  qual if p->owner
815b0 20 63 68 61 6e 67 65 73 20 62 65 74 77 65 65 6e   changes between
815c0 20 74 77 6f 20 76 61 6c 75 65 73 0a 20 20 2a 2a   two values.  **
815d0 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 65 71   that are not eq
815e0 75 61 6c 20 74 6f 20 73 65 6c 66 20 77 68 69 6c  ual to self whil
815f0 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  e the comparison
81600 20 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 63 65   is taking place
81610 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d 70 6c  ..  ** This impl
81620 65 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73 6f 20  ementation also 
81630 61 73 73 75 6d 65 73 20 61 20 63 6f 68 65 72 65  assumes a cohere
81640 6e 74 20 63 61 63 68 65 20 2d 20 74 68 61 74 20  nt cache - that 
81650 0a 20 20 2a 2a 20 73 65 70 61 72 61 74 65 20 70  .  ** separate p
81660 72 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f 74 20  rocesses cannot 
81670 72 65 61 64 20 64 69 66 66 65 72 65 6e 74 20 76  read different v
81680 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 20 73  alues from the s
81690 61 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65 73 73  ame.  ** address
816a0 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   at the same tim
816b0 65 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 66  e.  If either of
816c0 20 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 64 69   these two condi
816d0 74 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65 20 6e  tions.  ** are n
816e0 6f 74 20 6d 65 74 2c 20 74 68 65 6e 20 74 68 65  ot met, then the
816f0 20 6d 75 74 65 78 65 73 20 77 69 6c 6c 20 66 61   mutexes will fa
81700 69 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d 73 20  il and problems 
81710 77 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20 20 2a  will result..  *
81720 2f 0a 20 20 7b 0a 20 20 20 20 70 74 68 72 65 61  /.  {.    pthrea
81730 64 5f 74 20 73 65 6c 66 20 3d 20 70 74 68 72 65  d_t self = pthre
81740 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 69  ad_self();.    i
81750 66 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26 26 20  f( p->nRef>0 && 
81760 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d  pthread_equal(p-
81770 3e 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20 29 7b  >owner, self) ){
81780 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b  .      p->nRef++
81790 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
817a0 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78     pthread_mutex
817b0 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29  _lock(&p->mutex)
817c0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
817d0 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20  p->nRef==0 );.  
817e0 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 73      p->owner = s
817f0 65 6c 66 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 52  elf;.      p->nR
81800 65 66 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  ef = 1;.    }.  
81810 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 55 73 65  }.#else.  /* Use
81820 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 72 65   the built-in re
81830 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 20  cursive mutexes 
81840 69 66 20 74 68 65 79 20 61 72 65 20 61 76 61 69  if they are avai
81850 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 70 74  lable..  */.  pt
81860 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b  hread_mutex_lock
81870 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70  (&p->mutex);.  p
81880 2d 3e 6f 77 6e 65 72 20 3d 20 70 74 68 72 65 61  ->owner = pthrea
81890 64 5f 73 65 6c 66 28 29 3b 0a 20 20 70 2d 3e 6e  d_self();.  p->n
818a0 52 65 66 2b 2b 3b 0a 23 65 6e 64 69 66 0a 0a 23  Ref++;.#endif..#
818b0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
818c0 55 47 0a 20 20 69 66 28 20 70 2d 3e 74 72 61 63  UG.  if( p->trac
818d0 65 20 29 7b 0a 20 20 20 20 70 72 69 6e 74 66 28  e ){.    printf(
818e0 22 65 6e 74 65 72 20 6d 75 74 65 78 20 25 70 20  "enter mutex %p 
818f0 28 25 64 29 20 77 69 74 68 20 6e 52 65 66 3d 25  (%d) with nRef=%
81900 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63  d\n", p, p->trac
81910 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d  e, p->nRef);.  }
81920 0a 23 65 6e 64 69 66 0a 7d 0a 53 51 4c 49 54 45  .#endif.}.SQLITE
81930 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
81940 5f 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69 74  _mutex_try(sqlit
81950 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
81960 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74  int rc;.  assert
81970 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ( p );.  assert(
81980 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d   p->id==SQLITE_M
81990 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c  UTEX_RECURSIVE |
819a0 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
819b0 6e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 0a 23  notheld(p) );..#
819c0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d  ifdef SQLITE_HOM
819d0 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45  EGROWN_RECURSIVE
819e0 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49 66 20 72  _MUTEX.  /* If r
819f0 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73  ecursive mutexes
81a00 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62   are not availab
81a10 6c 65 2c 20 74 68 65 6e 20 77 65 20 68 61 76 65  le, then we have
81a20 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a 20 6f 75   to grow.  ** ou
81a30 72 20 6f 77 6e 2e 20 20 54 68 69 73 20 69 6d 70  r own.  This imp
81a40 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 75  lementation assu
81a50 6d 65 73 20 74 68 61 74 20 70 74 68 72 65 61 64  mes that pthread
81a60 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a 20 69 73  _equal().  ** is
81a70 20 61 74 6f 6d 69 63 20 2d 20 74 68 61 74 20 69   atomic - that i
81a80 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 65 63 65  t cannot be dece
81a90 69 76 65 64 20 69 6e 74 6f 20 74 68 69 6e 6b 69  ived into thinki
81aa0 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20 61 6e 64  ng self.  ** and
81ab0 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65 20 65 71   p->owner are eq
81ac0 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e 65 72 20  ual if p->owner 
81ad0 63 68 61 6e 67 65 73 20 62 65 74 77 65 65 6e 20  changes between 
81ae0 74 77 6f 20 76 61 6c 75 65 73 0a 20 20 2a 2a 20  two values.  ** 
81af0 74 68 61 74 20 61 72 65 20 6e 6f 74 20 65 71 75  that are not equ
81b00 61 6c 20 74 6f 20 73 65 6c 66 20 77 68 69 6c 65  al to self while
81b10 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20   the comparison 
81b20 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 63 65 2e  is taking place.
81b30 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65  .  ** This imple
81b40 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73 6f 20 61  mentation also a
81b50 73 73 75 6d 65 73 20 61 20 63 6f 68 65 72 65 6e  ssumes a coheren
81b60 74 20 63 61 63 68 65 20 2d 20 74 68 61 74 20 0a  t cache - that .
81b70 20 20 2a 2a 20 73 65 70 61 72 61 74 65 20 70 72    ** separate pr
81b80 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f 74 20 72  ocesses cannot r
81b90 65 61 64 20 64 69 66 66 65 72 65 6e 74 20 76 61  ead different va
81ba0 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 20 73 61  lues from the sa
81bb0 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65 73 73 20  me.  ** address 
81bc0 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65  at the same time
81bd0 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 66 20  .  If either of 
81be0 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 64 69 74  these two condit
81bf0 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65 20 6e 6f  ions.  ** are no
81c00 74 20 6d 65 74 2c 20 74 68 65 6e 20 74 68 65 20  t met, then the 
81c10 6d 75 74 65 78 65 73 20 77 69 6c 6c 20 66 61 69  mutexes will fai
81c20 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d 73 20 77  l and problems w
81c30 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20 20 2a 2f  ill result..  */
81c40 0a 20 20 7b 0a 20 20 20 20 70 74 68 72 65 61 64  .  {.    pthread
81c50 5f 74 20 73 65 6c 66 20 3d 20 70 74 68 72 65 61  _t self = pthrea
81c60 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 69 66  d_self();.    if
81c70 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26 26 20 70  ( p->nRef>0 && p
81c80 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e  thread_equal(p->
81c90 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20 29 7b 0a  owner, self) ){.
81ca0 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b        p->nRef++;
81cb0 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
81cc0 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 65  TE_OK;.    }else
81cd0 20 69 66 28 20 70 74 68 72 65 61 64 5f 6d 75 74   if( pthread_mut
81ce0 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65  ex_lock(&p->mute
81cf0 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61  x)==0 ){.      a
81d00 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d  ssert( p->nRef==
81d10 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f 77  0 );.      p->ow
81d20 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20 20  ner = self;.    
81d30 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20    p->nRef = 1;. 
81d40 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
81d50 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  _OK;.    }else{.
81d60 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
81d70 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20  E_BUSY;.    }.  
81d80 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 55 73 65  }.#else.  /* Use
81d90 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 72 65   the built-in re
81da0 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 20  cursive mutexes 
81db0 69 66 20 74 68 65 79 20 61 72 65 20 61 76 61 69  if they are avai
81dc0 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  lable..  */.  if
81dd0 28 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f  ( pthread_mutex_
81de0 74 72 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65  trylock(&p->mute
81df0 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e  x)==0 ){.    p->
81e00 6f 77 6e 65 72 20 3d 20 70 74 68 72 65 61 64 5f  owner = pthread_
81e10 73 65 6c 66 28 29 3b 0a 20 20 20 20 70 2d 3e 6e  self();.    p->n
81e20 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20  Ref++;.    rc = 
81e30 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c  SQLITE_OK;.  }el
81e40 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  se{.    rc = SQL
81e50 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 23 65  ITE_BUSY;.  }.#e
81e60 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
81e70 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20  ITE_DEBUG.  if( 
81e80 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
81e90 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20   p->trace ){.   
81ea0 20 70 72 69 6e 74 66 28 22 65 6e 74 65 72 20 6d   printf("enter m
81eb0 75 74 65 78 20 25 70 20 28 25 64 29 20 77 69 74  utex %p (%d) wit
81ec0 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c  h nRef=%d\n", p,
81ed0 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52   p->trace, p->nR
81ee0 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ef);.  }.#endif.
81ef0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
81f00 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  /*.** The sqlite
81f10 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20  3_mutex_leave() 
81f20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20  routine exits a 
81f30 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a  mutex that was.*
81f40 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74  * previously ent
81f50 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65  ered by the same
81f60 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 65   thread.  The be
81f70 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64  havior.** is und
81f80 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75  efined if the mu
81f90 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65  tex is not curre
81fa0 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 0a  ntly entered or.
81fb0 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e  ** is not curren
81fc0 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20  tly allocated.  
81fd0 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65  SQLite will neve
81fe0 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a  r do either..*/.
81ff0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
82000 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
82010 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  ave(sqlite3_mute
82020 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  x *p){.  assert(
82030 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20   p );.  assert( 
82040 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
82050 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 6e 52  ld(p) );.  p->nR
82060 65 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20  ef--;.  assert( 
82070 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d  p->nRef==0 || p-
82080 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45  >id==SQLITE_MUTE
82090 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 0a  X_RECURSIVE );..
820a0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f  #ifdef SQLITE_HO
820b0 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56  MEGROWN_RECURSIV
820c0 45 5f 4d 55 54 45 58 0a 20 20 69 66 28 20 70 2d  E_MUTEX.  if( p-
820d0 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20  >nRef==0 ){.    
820e0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e  pthread_mutex_un
820f0 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b  lock(&p->mutex);
82100 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 70 74 68  .  }.#else.  pth
82110 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63  read_mutex_unloc
82120 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 23 65  k(&p->mutex);.#e
82130 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
82140 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20  ITE_DEBUG.  if( 
82150 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20  p->trace ){.    
82160 70 72 69 6e 74 66 28 22 6c 65 61 76 65 20 6d 75  printf("leave mu
82170 74 65 78 20 25 70 20 28 25 64 29 20 77 69 74 68  tex %p (%d) with
82180 20 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20   nRef=%d\n", p, 
82190 70 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65  p->trace, p->nRe
821a0 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d  f);.  }.#endif.}
821b0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ../*.** The sqli
821c0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29  te3_mutex_held()
821d0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74   and sqlite3_mut
821e0 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75  ex_notheld() rou
821f0 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65  tine are.** inte
82200 6e 64 65 64 20 66 6f 72 20 75 73 65 20 6f 6e 6c  nded for use onl
82210 79 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28  y inside assert(
82220 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 4f  ) statements.  O
82230 6e 20 73 6f 6d 65 20 70 6c 61 74 66 6f 72 6d 73  n some platforms
82240 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 69 67 68 74  ,.** there might
82250 20 62 65 20 72 61 63 65 20 63 6f 6e 64 69 74 69   be race conditi
82260 6f 6e 73 20 74 68 61 74 20 63 61 6e 20 63 61 75  ons that can cau
82270 73 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  se these routine
82280 73 20 74 6f 0a 2a 2a 20 64 65 6c 69 76 65 72 20  s to.** deliver 
82290 69 6e 63 6f 72 72 65 63 74 20 72 65 73 75 6c 74  incorrect result
822a0 73 2e 20 20 49 6e 20 70 61 72 74 69 63 75 6c 61  s.  In particula
822b0 72 2c 20 69 66 20 70 74 68 72 65 61 64 5f 65 71  r, if pthread_eq
822c0 75 61 6c 28 29 20 69 73 0a 2a 2a 20 6e 6f 74 20  ual() is.** not 
822d0 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65 72 61 74  an atomic operat
822e0 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 73 65 20  ion, then these 
822f0 72 6f 75 74 69 6e 65 73 20 6d 69 67 68 74 20 64  routines might d
82300 65 6c 69 76 65 72 79 0a 2a 2a 20 69 6e 63 6f 72  elivery.** incor
82310 72 65 63 74 20 72 65 73 75 6c 74 73 2e 20 20 4f  rect results.  O
82320 6e 20 6d 6f 73 74 20 70 6c 61 74 66 6f 72 6d 73  n most platforms
82330 2c 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28  , pthread_equal(
82340 29 20 69 73 20 61 20 0a 2a 2a 20 63 6f 6d 70 61  ) is a .** compa
82350 72 69 73 6f 6e 20 6f 66 20 74 77 6f 20 69 6e 74  rison of two int
82360 65 67 65 72 73 20 61 6e 64 20 69 73 20 74 68 65  egers and is the
82370 72 65 66 6f 72 65 20 61 74 6f 6d 69 63 2e 20 20  refore atomic.  
82380 42 75 74 20 77 65 20 61 72 65 0a 2a 2a 20 74 6f  But we are.** to
82390 6c 64 20 74 68 61 74 20 48 50 55 58 20 69 73 20  ld that HPUX is 
823a0 6e 6f 74 20 73 75 63 68 20 61 20 70 6c 61 74 66  not such a platf
823b0 6f 72 6d 2e 20 20 49 66 20 73 6f 2c 20 74 68 65  orm.  If so, the
823c0 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  n these routines
823d0 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 61 6c 77  .** will not alw
823e0 61 79 73 20 77 6f 72 6b 20 63 6f 72 72 65 63 74  ays work correct
823f0 6c 79 20 6f 6e 20 48 50 55 58 2e 0a 2a 2a 0a 2a  ly on HPUX..**.*
82400 2a 20 4f 6e 20 74 68 6f 73 65 20 70 6c 61 74 66  * On those platf
82410 6f 72 6d 73 20 77 68 65 72 65 20 70 74 68 72 65  orms where pthre
82420 61 64 5f 65 71 75 61 6c 28 29 20 69 73 20 6e 6f  ad_equal() is no
82430 74 20 61 74 6f 6d 69 63 2c 20 53 51 4c 69 74 65  t atomic, SQLite
82440 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 6f  .** should be co
82450 6d 70 69 6c 65 64 20 77 69 74 68 6f 75 74 20 2d  mpiled without -
82460 44 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61 6e  DSQLITE_DEBUG an
82470 64 20 77 69 74 68 20 2d 44 4e 44 45 42 55 47 20  d with -DNDEBUG 
82480 74 6f 0a 2a 2a 20 6d 61 6b 65 20 73 75 72 65 20  to.** make sure 
82490 6e 6f 20 61 73 73 65 72 74 28 29 20 73 74 61 74  no assert() stat
824a0 65 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c 75  ements are evalu
824b0 61 74 65 64 20 61 6e 64 20 68 65 6e 63 65 20 74  ated and hence t
824c0 68 65 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 73  hese.** routines
824d0 20 61 72 65 20 6e 65 76 65 72 20 63 61 6c 6c 65   are never calle
824e0 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44  d..*/.#ifndef ND
824f0 45 42 55 47 0a 53 51 4c 49 54 45 5f 41 50 49 20  EBUG.SQLITE_API 
82500 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  int sqlite3_mute
82510 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d  x_held(sqlite3_m
82520 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75  utex *p){.  retu
82530 72 6e 20 70 3d 3d 30 20 7c 7c 20 28 70 2d 3e 6e  rn p==0 || (p->n
82540 52 65 66 21 3d 30 20 26 26 20 70 74 68 72 65 61  Ref!=0 && pthrea
82550 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72  d_equal(p->owner
82560 2c 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29  , pthread_self()
82570 29 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  ));.}.SQLITE_API
82580 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74   int sqlite3_mut
82590 65 78 5f 6e 6f 74 68 65 6c 64 28 73 71 6c 69 74  ex_notheld(sqlit
825a0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
825b0 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70  return p==0 || p
825c0 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 74 68  ->nRef==0 || pth
825d0 72 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77  read_equal(p->ow
825e0 6e 65 72 2c 20 70 74 68 72 65 61 64 5f 73 65 6c  ner, pthread_sel
825f0 66 28 29 29 3d 3d 30 3b 0a 7d 0a 23 65 6e 64 69  f())==0;.}.#endi
82600 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  f.#endif /* SQLI
82610 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44  TE_MUTEX_PTHREAD
82620 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
82630 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65  **** End of mute
82640 78 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a  x_unix.c *******
82650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82670 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
82680 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
82690 6d 75 74 65 78 5f 77 33 32 2e 63 20 2a 2a 2a 2a  mutex_w32.c ****
826a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
826b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
826c0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20  ***/./*.** 2007 
826d0 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20  August 14.**.** 
826e0 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
826f0 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
82700 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
82710 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
82720 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
82730 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
82740 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
82750 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
82760 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
82770 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
82780 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
82790 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
827a0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
827b0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
827c0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
827d0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
827e0 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
827f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82830 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
82840 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74   file contains t
82850 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74  he C functions t
82860 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75  hat implement mu
82870 74 65 78 65 73 20 66 6f 72 20 77 69 6e 33 32 0a  texes for win32.
82880 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78  **.** $Id: mutex
82890 5f 77 33 32 2e 63 2c 76 20 31 2e 36 20 32 30 30  _w32.c,v 1.6 200
828a0 38 2f 30 33 2f 32 36 20 31 38 3a 33 34 3a 34 33  8/03/26 18:34:43
828b0 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70   danielk1977 Exp
828c0 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65   $.*/../*.** The
828d0 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69   code in this fi
828e0 6c 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20  le is only used 
828f0 69 66 20 77 65 20 61 72 65 20 63 6f 6d 70 69 6c  if we are compil
82900 69 6e 67 20 6d 75 6c 74 69 74 68 72 65 61 64 65  ing multithreade
82910 64 0a 2a 2a 20 6f 6e 20 61 20 77 69 6e 33 32 20  d.** on a win32 
82920 73 79 73 74 65 6d 2e 0a 2a 2f 0a 23 69 66 64 65  system..*/.#ifde
82930 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57  f SQLITE_MUTEX_W
82940 33 32 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 72  32../*.** Each r
82950 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69  ecursive mutex i
82960 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  s an instance of
82970 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
82980 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72  tructure..*/.str
82990 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  uct sqlite3_mute
829a0 78 20 7b 0a 20 20 43 52 49 54 49 43 41 4c 5f 53  x {.  CRITICAL_S
829b0 45 43 54 49 4f 4e 20 6d 75 74 65 78 3b 20 20 20  ECTION mutex;   
829c0 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f   /* Mutex contro
829d0 6c 6c 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a  lling the lock *
829e0 2f 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20 20  /.  int id;     
829f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
82a00 2a 20 4d 75 74 65 78 20 74 79 70 65 20 2a 2f 0a  * Mutex type */.
82a10 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20    int nRef;     
82a20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
82a30 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 65 72 61  Number of entera
82a40 6e 63 65 73 20 2a 2f 0a 20 20 44 57 4f 52 44 20  nces */.  DWORD 
82a50 6f 77 6e 65 72 3b 20 20 20 20 20 20 20 20 20 20  owner;          
82a60 20 20 20 20 20 2f 2a 20 54 68 72 65 61 64 20 68       /* Thread h
82a70 6f 6c 64 69 6e 67 20 74 68 69 73 20 6d 75 74 65  olding this mute
82a80 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52  x */.};../*.** R
82a90 65 74 75 72 6e 20 74 72 75 65 20 28 6e 6f 6e 2d  eturn true (non-
82aa0 7a 65 72 6f 29 20 69 66 20 77 65 20 61 72 65 20  zero) if we are 
82ab0 72 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20 57 69  running under Wi
82ac0 6e 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69 6e 58  nNT, Win2K, WinX
82ad0 50 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45 2e 20  P,.** or WinCE. 
82ae0 20 52 65 74 75 72 6e 20 66 61 6c 73 65 20 28 7a   Return false (z
82af0 65 72 6f 29 20 66 6f 72 20 57 69 6e 39 35 2c 20  ero) for Win95, 
82b00 57 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d 45 2e  Win98, or WinME.
82b10 0a 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73 20 61  .**.** Here is a
82b20 6e 20 69 6e 74 65 72 65 73 74 69 6e 67 20 6f 62  n interesting ob
82b30 73 65 72 76 61 74 69 6f 6e 3a 20 20 57 69 6e 39  servation:  Win9
82b40 35 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20 57 69  5, Win98, and Wi
82b50 6e 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68 65 20  nME lack.** the 
82b60 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49  LockFileEx() API
82b70 2e 20 20 42 75 74 20 77 65 20 63 61 6e 20 73 74  .  But we can st
82b80 69 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79 20 6c  ill statically l
82b90 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 61 74  ink against that
82ba0 0a 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e 67 20  .** API as long 
82bb0 61 73 20 77 65 20 64 6f 6e 27 74 20 63 61 6c 6c  as we don't call
82bc0 20 69 74 20 77 69 6e 20 72 75 6e 6e 69 6e 67 20   it win running 
82bd0 57 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 41 20  Win95/98/ME.  A 
82be0 63 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 69 73 20  call to.** this 
82bf0 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
82c00 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20  to determine if 
82c10 74 68 65 20 68 6f 73 74 20 69 73 20 57 69 6e 39  the host is Win9
82c20 35 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a 20 57 69  5/98/ME or.** Wi
82c30 6e 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 74 68 61  nNT/2K/XP so tha
82c40 74 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77  t we will know w
82c50 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 77 65  hether or not we
82c60 20 63 61 6e 20 73 61 66 65 6c 79 20 63 61 6c 6c   can safely call
82c70 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65  .** the LockFile
82c80 45 78 28 29 20 41 50 49 2e 0a 2a 2f 0a 23 69 66  Ex() API..*/.#if
82c90 20 4f 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69   OS_WINCE.# defi
82ca0 6e 65 20 6d 75 74 65 78 49 73 4e 54 28 29 20 20  ne mutexIsNT()  
82cb0 28 31 29 0a 23 65 6c 73 65 0a 20 20 73 74 61 74  (1).#else.  stat
82cc0 69 63 20 69 6e 74 20 6d 75 74 65 78 49 73 4e 54  ic int mutexIsNT
82cd0 28 76 6f 69 64 29 7b 0a 20 20 20 20 73 74 61 74  (void){.    stat
82ce0 69 63 20 69 6e 74 20 6f 73 54 79 70 65 20 3d 20  ic int osType = 
82cf0 30 3b 0a 20 20 20 20 69 66 28 20 6f 73 54 79 70  0;.    if( osTyp
82d00 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 4f 53  e==0 ){.      OS
82d10 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73 49 6e 66  VERSIONINFO sInf
82d20 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66 6f 2e 64  o;.      sInfo.d
82d30 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66 6f 53 69  wOSVersionInfoSi
82d40 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73 49 6e 66  ze = sizeof(sInf
82d50 6f 29 3b 0a 20 20 20 20 20 20 47 65 74 56 65 72  o);.      GetVer
82d60 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f 29 3b 0a  sionEx(&sInfo);.
82d70 20 20 20 20 20 20 6f 73 54 79 70 65 20 3d 20 73        osType = s
82d80 49 6e 66 6f 2e 64 77 50 6c 61 74 66 6f 72 6d 49  Info.dwPlatformI
82d90 64 3d 3d 56 45 52 5f 50 4c 41 54 46 4f 52 4d 5f  d==VER_PLATFORM_
82da0 57 49 4e 33 32 5f 4e 54 20 3f 20 32 20 3a 20 31  WIN32_NT ? 2 : 1
82db0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
82dc0 72 6e 20 6f 73 54 79 70 65 3d 3d 32 3b 0a 20 20  rn osType==2;.  
82dd0 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 57  }.#endif /* OS_W
82de0 49 4e 43 45 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20  INCE */.../*.** 
82df0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
82e00 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e  x_alloc() routin
82e10 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65  e allocates a ne
82e20 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72  w.** mutex and r
82e30 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
82e40 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72   to it.  If it r
82e50 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74  eturns NULL.** t
82e60 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61  hat means that a
82e70 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74   mutex could not
82e80 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20   be allocated.  
82e90 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 75  SQLite.** will u
82ea0 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20  nwind its stack 
82eb0 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72  and return an er
82ec0 72 6f 72 2e 20 20 54 68 65 20 61 72 67 75 6d 65  ror.  The argume
82ed0 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33  nt.** to sqlite3
82ee0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69  _mutex_alloc() i
82ef0 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69  s one of these i
82f00 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73  nteger constants
82f10 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  :.**.** <ul>.** 
82f20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
82f30 45 58 5f 46 41 53 54 20 20 20 20 20 20 20 20 20  EX_FAST         
82f40 20 20 20 20 20 20 30 0a 2a 2a 20 3c 6c 69 3e 20        0.** <li> 
82f50 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
82f60 43 55 52 53 49 56 45 20 20 20 20 20 20 20 20 20  CURSIVE         
82f70 20 31 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49   1.** <li>  SQLI
82f80 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
82f90 4d 41 53 54 45 52 20 20 20 20 20 20 32 0a 2a 2a  MASTER      2.**
82fa0 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
82fb0 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 20 20  TEX_STATIC_MEM  
82fc0 20 20 20 20 20 20 20 33 0a 2a 2a 20 3c 6c 69 3e         3.** <li>
82fd0 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
82fe0 54 41 54 49 43 5f 50 52 4e 47 20 20 20 20 20 20  TATIC_PRNG      
82ff0 20 20 34 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a    4.** </ul>.**.
83000 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 77 6f  ** The first two
83010 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 65   constants cause
83020 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
83030 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74 65  lloc() to create
83040 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e  .** a new mutex.
83050 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20    The new mutex 
83060 69 73 20 72 65 63 75 72 73 69 76 65 20 77 68 65  is recursive whe
83070 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  n SQLITE_MUTEX_R
83080 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 75  ECURSIVE.** is u
83090 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65  sed but not nece
830a0 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20  ssarily so when 
830b0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
830c0 54 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20 54 68  T is used..** Th
830d0 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e  e mutex implemen
830e0 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20  tation does not 
830f0 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64  need to make a d
83100 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65  istinction.** be
83110 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54  tween SQLITE_MUT
83120 45 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64  EX_RECURSIVE and
83130 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
83140 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a  ST if it does.**
83150 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 42   not want to.  B
83160 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f  ut SQLite will o
83170 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20 72 65  nly request a re
83180 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e  cursive mutex in
83190 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20  .** cases where 
831a0 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20  it really needs 
831b0 6f 6e 65 2e 20 20 49 66 20 61 20 66 61 73 74 65  one.  If a faste
831c0 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20  r non-recursive 
831d0 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65  mutex.** impleme
831e0 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c  ntation is avail
831f0 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 74  able on the host
83200 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d   platform, the m
83210 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a 2a  utex subsystem.*
83220 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 73  * might return s
83230 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20 72  uch a mutex in r
83240 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54  esponse to SQLIT
83250 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a  E_MUTEX_FAST..**
83260 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72 20 61 6c  .** The other al
83270 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 73  lowed parameters
83280 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   to sqlite3_mute
83290 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72  x_alloc() each r
832a0 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74  eturn.** a point
832b0 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 70  er to a static p
832c0 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78  reexisting mutex
832d0 2e 20 20 54 68 72 65 65 20 73 74 61 74 69 63 20  .  Three static 
832e0 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 75  mutexes are.** u
832f0 73 65 64 20 62 79 20 74 68 65 20 63 75 72 72 65  sed by the curre
83300 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51  nt version of SQ
83310 4c 69 74 65 2e 20 20 46 75 74 75 72 65 20 76 65  Lite.  Future ve
83320 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65  rsions of SQLite
83330 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 64 64 69  .** may add addi
83340 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75  tional static mu
83350 74 65 78 65 73 2e 20 20 53 74 61 74 69 63 20 6d  texes.  Static m
83360 75 74 65 78 65 73 20 61 72 65 20 66 6f 72 20 69  utexes are for i
83370 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62  nternal.** use b
83380 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20  y SQLite only.  
83390 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61  Applications tha
833a0 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 75 74  t use SQLite mut
833b0 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75  exes should.** u
833c0 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61  se only the dyna
833d0 6d 69 63 20 6d 75 74 65 78 65 73 20 72 65 74 75  mic mutexes retu
833e0 72 6e 65 64 20 62 79 20 53 51 4c 49 54 45 5f 4d  rned by SQLITE_M
833f0 55 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20  UTEX_FAST or.** 
83400 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
83410 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  URSIVE..**.** No
83420 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f  te that if one o
83430 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75  f the dynamic mu
83440 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 28  tex parameters (
83450 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
83460 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d  T.** or SQLITE_M
83470 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 20  UTEX_RECURSIVE) 
83480 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c  is used then sql
83490 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
834a0 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20  ().** returns a 
834b0 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20  different mutex 
834c0 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20  on every call.  
834d0 42 75 74 20 66 6f 72 20 74 68 65 20 73 74 61 74  But for the stat
834e0 69 63 20 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70  ic .** mutex typ
834f0 65 73 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74  es, the same mut
83500 65 78 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f  ex is returned o
83510 6e 20 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61  n every call tha
83520 74 20 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d  t has.** the sam
83530 65 20 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a  e type number..*
83540 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  /.SQLITE_API sql
83550 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69  ite3_mutex *sqli
83560 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
83570 69 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73 71  int iType){.  sq
83580 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 3b 0a  lite3_mutex *p;.
83590 0a 20 20 73 77 69 74 63 68 28 20 69 54 79 70 65  .  switch( iType
835a0 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c   ){.    case SQL
835b0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a 0a  ITE_MUTEX_FAST:.
835c0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
835d0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a  MUTEX_RECURSIVE:
835e0 20 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c   {.      p = sql
835f0 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20  ite3MallocZero( 
83600 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20  sizeof(*p) );.  
83610 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20      if( p ){.   
83620 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79       p->id = iTy
83630 70 65 3b 0a 20 20 20 20 20 20 20 20 49 6e 69 74  pe;.        Init
83640 69 61 6c 69 7a 65 43 72 69 74 69 63 61 6c 53 65  ializeCriticalSe
83650 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29  ction(&p->mutex)
83660 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
83670 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
83680 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
83690 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33    static sqlite3
836a0 5f 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 74  _mutex staticMut
836b0 65 78 65 73 5b 36 5d 3b 0a 20 20 20 20 20 20 73  exes[6];.      s
836c0 74 61 74 69 63 20 69 6e 74 20 69 73 49 6e 69 74  tatic int isInit
836d0 20 3d 20 30 3b 0a 20 20 20 20 20 20 77 68 69 6c   = 0;.      whil
836e0 65 28 20 21 69 73 49 6e 69 74 20 29 7b 0a 20 20  e( !isInit ){.  
836f0 20 20 20 20 20 20 73 74 61 74 69 63 20 6c 6f 6e        static lon
83700 67 20 6c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20  g lock = 0;.    
83710 20 20 20 20 69 66 28 20 49 6e 74 65 72 6c 6f 63      if( Interloc
83720 6b 65 64 49 6e 63 72 65 6d 65 6e 74 28 26 6c 6f  kedIncrement(&lo
83730 63 6b 29 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  ck)==1 ){.      
83740 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20      int i;.     
83750 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
83760 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74  sizeof(staticMut
83770 65 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61  exes)/sizeof(sta
83780 74 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 3b 20  ticMutexes[0]); 
83790 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
837a0 20 20 49 6e 69 74 69 61 6c 69 7a 65 43 72 69 74    InitializeCrit
837b0 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 73 74 61  icalSection(&sta
837c0 74 69 63 4d 75 74 65 78 65 73 5b 69 5d 2e 6d 75  ticMutexes[i].mu
837d0 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20 20 20  tex);.          
837e0 7d 0a 20 20 20 20 20 20 20 20 20 20 69 73 49 6e  }.          isIn
837f0 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  it = 1;.        
83800 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
83810 20 53 6c 65 65 70 28 31 29 3b 0a 20 20 20 20 20   Sleep(1);.     
83820 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
83830 20 20 20 61 73 73 65 72 74 28 20 69 54 79 70 65     assert( iType
83840 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20  -2 >= 0 );.     
83850 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d 32   assert( iType-2
83860 20 3c 20 73 69 7a 65 6f 66 28 73 74 61 74 69 63   < sizeof(static
83870 4d 75 74 65 78 65 73 29 2f 73 69 7a 65 6f 66 28  Mutexes)/sizeof(
83880 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 30 5d  staticMutexes[0]
83890 29 20 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 26  ) );.      p = &
838a0 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69 54  staticMutexes[iT
838b0 79 70 65 2d 32 5d 3b 0a 20 20 20 20 20 20 70 2d  ype-2];.      p-
838c0 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20  >id = iType;.   
838d0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
838e0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a    }.  return p;.
838f0 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  }.../*.** This r
83900 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74  outine deallocat
83910 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 0a  es a previously.
83920 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75 74  ** allocated mut
83930 65 78 2e 20 20 53 51 4c 69 74 65 20 69 73 20 63  ex.  SQLite is c
83940 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f  areful to deallo
83950 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 6d 75  cate every.** mu
83960 74 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f  tex that it allo
83970 63 61 74 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cates..*/.SQLITE
83980 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
83990 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 71 6c  3_mutex_free(sql
839a0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
839b0 20 20 61 73 73 65 72 74 28 20 70 20 29 3b 0a 20    assert( p );. 
839c0 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66   assert( p->nRef
839d0 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
839e0 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d   p->id==SQLITE_M
839f0 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e  UTEX_FAST || p->
83a00 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
83a10 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20  _RECURSIVE );.  
83a20 44 65 6c 65 74 65 43 72 69 74 69 63 61 6c 53 65  DeleteCriticalSe
83a30 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29  ction(&p->mutex)
83a40 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
83a50 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  (p);.}../*.** Th
83a60 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
83a70 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69  enter() and sqli
83a80 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20  te3_mutex_try() 
83a90 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74  routines attempt
83aa0 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d  .** to enter a m
83ab0 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65  utex.  If anothe
83ac0 72 20 74 68 72 65 61 64 20 69 73 20 61 6c 72 65  r thread is alre
83ad0 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d  ady within the m
83ae0 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33  utex,.** sqlite3
83af0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77  _mutex_enter() w
83b00 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71  ill block and sq
83b10 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
83b20 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a  ) will return.**
83b30 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54   SQLITE_BUSY.  T
83b40 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
83b50 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 65  _try() interface
83b60 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
83b70 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65  OK.** upon succe
83b80 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75  ssful entry.  Mu
83b90 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73  texes created us
83ba0 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ing SQLITE_MUTEX
83bb0 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e 0a 2a  _RECURSIVE can.*
83bc0 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c  * be entered mul
83bd0 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74  tiple times by t
83be0 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20  he same thread. 
83bf0 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 20 74   In such cases t
83c00 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73  he,.** mutex mus
83c10 74 20 62 65 20 65 78 69 74 65 64 20 61 6e 20 65  t be exited an e
83c20 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74  qual number of t
83c30 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74  imes before anot
83c40 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61  her thread.** ca
83c50 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65  n enter.  If the
83c60 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 69   same thread tri
83c70 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20  es to enter any 
83c80 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75  other kind of mu
83c90 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e  tex.** more than
83ca0 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76   once, the behav
83cb0 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64  ior is undefined
83cc0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
83cd0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74  void sqlite3_mut
83ce0 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33  ex_enter(sqlite3
83cf0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73  _mutex *p){.  as
83d00 73 65 72 74 28 20 70 20 29 3b 0a 20 20 61 73 73  sert( p );.  ass
83d10 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  ert( p->id==SQLI
83d20 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
83d30 56 45 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75  VE || sqlite3_mu
83d40 74 65 78 5f 6e 6f 74 68 65 6c 64 28 70 29 20 29  tex_notheld(p) )
83d50 3b 0a 20 20 45 6e 74 65 72 43 72 69 74 69 63 61  ;.  EnterCritica
83d60 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74  lSection(&p->mut
83d70 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20  ex);.  p->owner 
83d80 3d 20 47 65 74 43 75 72 72 65 6e 74 54 68 72 65  = GetCurrentThre
83d90 61 64 49 64 28 29 3b 20 0a 20 20 70 2d 3e 6e 52  adId(); .  p->nR
83da0 65 66 2b 2b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  ef++;.}.SQLITE_A
83db0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d  PI int sqlite3_m
83dc0 75 74 65 78 5f 74 72 79 28 73 71 6c 69 74 65 33  utex_try(sqlite3
83dd0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e  _mutex *p){.  in
83de0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55  t rc = SQLITE_BU
83df0 53 59 3b 0a 20 20 61 73 73 65 72 74 28 20 70 20  SY;.  assert( p 
83e00 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
83e10 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
83e20 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 73 71  _RECURSIVE || sq
83e30 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68  lite3_mutex_noth
83e40 65 6c 64 28 70 29 20 29 3b 0a 20 20 2f 2a 0a 20  eld(p) );.  /*. 
83e50 20 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f   ** The sqlite3_
83e60 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74  mutex_try() rout
83e70 69 6e 65 20 69 73 20 76 65 72 79 20 72 61 72 65  ine is very rare
83e80 6c 79 20 75 73 65 64 2c 20 61 6e 64 20 77 68 65  ly used, and whe
83e90 6e 20 69 74 0a 20 20 2a 2a 20 69 73 20 75 73 65  n it.  ** is use
83ea0 64 20 69 74 20 69 73 20 6d 65 72 65 6c 79 20 61  d it is merely a
83eb0 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20  n optimization. 
83ec0 20 53 6f 20 69 74 20 69 73 20 4f 4b 20 66 6f 72   So it is OK for
83ed0 20 69 74 20 74 6f 20 61 6c 77 61 79 73 0a 20 20   it to always.  
83ee0 2a 2a 20 66 61 69 6c 2e 20 20 0a 20 20 2a 2a 0a  ** fail.  .  **.
83ef0 20 20 2a 2a 20 54 68 65 20 54 72 79 45 6e 74 65    ** The TryEnte
83f00 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e  rCriticalSection
83f10 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  () interface is 
83f20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f  only available o
83f30 6e 20 57 69 6e 4e 54 2e 0a 20 20 2a 2a 20 41 6e  n WinNT..  ** An
83f40 64 20 73 6f 6d 65 20 77 69 6e 64 6f 77 73 20 63  d some windows c
83f50 6f 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69  ompilers complai
83f60 6e 20 69 66 20 79 6f 75 20 74 72 79 20 74 6f 20  n if you try to 
83f70 75 73 65 20 69 74 20 77 69 74 68 6f 75 74 0a 20  use it without. 
83f80 20 2a 2a 20 66 69 72 73 74 20 64 6f 69 6e 67 20   ** first doing 
83f90 73 6f 6d 65 20 23 64 65 66 69 6e 65 73 20 74 68  some #defines th
83fa0 61 74 20 70 72 65 76 65 6e 74 20 53 51 4c 69 74  at prevent SQLit
83fb0 65 20 66 72 6f 6d 20 62 75 69 6c 64 69 6e 67 20  e from building 
83fc0 6f 6e 20 57 69 6e 39 38 2e 0a 20 20 2a 2a 20 46  on Win98..  ** F
83fd0 6f 72 20 74 68 61 74 20 72 65 61 73 6f 6e 2c 20  or that reason, 
83fe0 77 65 20 77 69 6c 6c 20 6f 6d 69 74 20 74 68 69  we will omit thi
83ff0 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 66  s optimization f
84000 6f 72 20 6e 6f 77 2e 20 20 53 65 65 0a 20 20 2a  or now.  See.  *
84010 2a 20 74 69 63 6b 65 74 20 23 32 36 38 35 2e 0a  * ticket #2685..
84020 20 20 2a 2f 0a 23 69 66 20 30 0a 20 20 69 66 28    */.#if 0.  if(
84030 20 6d 75 74 65 78 49 73 4e 54 28 29 20 26 26 20   mutexIsNT() && 
84040 54 72 79 45 6e 74 65 72 43 72 69 74 69 63 61 6c  TryEnterCritical
84050 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65  Section(&p->mute
84060 78 29 20 29 7b 0a 20 20 20 20 70 2d 3e 6f 77 6e  x) ){.    p->own
84070 65 72 20 3d 20 47 65 74 43 75 72 72 65 6e 74 54  er = GetCurrentT
84080 68 72 65 61 64 49 64 28 29 3b 0a 20 20 20 20 70  hreadId();.    p
84090 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63  ->nRef++;.    rc
840a0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
840b0 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72  }.#endif.  retur
840c0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
840d0 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
840e0 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65  _leave() routine
840f0 20 65 78 69 74 73 20 61 20 6d 75 74 65 78 20 74   exits a mutex t
84100 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 69  hat was.** previ
84110 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79  ously entered by
84120 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
84130 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a  .  The behavior.
84140 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20  ** is undefined 
84150 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20  if the mutex is 
84160 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e  not currently en
84170 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e  tered or.** is n
84180 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c  ot currently all
84190 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 20  ocated.  SQLite 
841a0 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65 69  will never do ei
841b0 74 68 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ther..*/.SQLITE_
841c0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
841d0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c  _mutex_leave(sql
841e0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
841f0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65    assert( p->nRe
84200 66 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  f>0 );.  assert(
84210 20 70 2d 3e 6f 77 6e 65 72 3d 3d 47 65 74 43 75   p->owner==GetCu
84220 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 29 20  rrentThreadId() 
84230 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a  );.  p->nRef--;.
84240 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65    assert( p->nRe
84250 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53  f==0 || p->id==S
84260 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
84270 52 53 49 56 45 20 29 3b 0a 20 20 4c 65 61 76 65  RSIVE );.  Leave
84280 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28  CriticalSection(
84290 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 2f  &p->mutex);.}../
842a0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
842b0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e  _mutex_held() an
842c0 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
842d0 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e  notheld() routin
842e0 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65  e are.** intende
842f0 64 20 66 6f 72 20 75 73 65 20 6f 6e 6c 79 20 69  d for use only i
84300 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73  nside assert() s
84310 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51  tatements..*/.SQ
84320 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
84330 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
84340 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
84350 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30  ){.  return p==0
84360 20 7c 7c 20 28 70 2d 3e 6e 52 65 66 21 3d 30 20   || (p->nRef!=0 
84370 26 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d 47 65 74  && p->owner==Get
84380 43 75 72 72 65 6e 74 54 68 72 65 61 64 49 64 28  CurrentThreadId(
84390 29 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  ));.}.SQLITE_API
843a0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74   int sqlite3_mut
843b0 65 78 5f 6e 6f 74 68 65 6c 64 28 73 71 6c 69 74  ex_notheld(sqlit
843c0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
843d0 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70  return p==0 || p
843e0 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e  ->nRef==0 || p->
843f0 6f 77 6e 65 72 21 3d 47 65 74 43 75 72 72 65 6e  owner!=GetCurren
84400 74 54 68 72 65 61 64 49 64 28 29 3b 0a 7d 0a 23  tThreadId();.}.#
84410 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
84420 4d 55 54 45 58 5f 57 33 32 20 2a 2f 0a 0a 2f 2a  MUTEX_W32 */../*
84430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
84440 64 20 6f 66 20 6d 75 74 65 78 5f 77 33 32 2e 63  d of mutex_w32.c
84450 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
84460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
84480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
84490 67 69 6e 20 66 69 6c 65 20 6d 61 6c 6c 6f 63 2e  gin file malloc.
844a0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
844b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
844c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
844d0 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
844e0 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
844f0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
84500 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
84510 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
84520 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
84530 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
84540 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
84550 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
84560 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
84570 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
84580 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
84590 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
845a0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
845b0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
845c0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
845d0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
845e0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
845f0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
84600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84640 2a 2a 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20 61  ****.** Memory a
84650 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69  llocation functi
84660 6f 6e 73 20 75 73 65 64 20 74 68 72 6f 75 67 68  ons used through
84670 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a 2a 0a 2a  out sqlite..**.*
84680 2a 0a 2a 2a 20 24 49 64 3a 20 6d 61 6c 6c 6f 63  *.** $Id: malloc
84690 2e 63 2c 76 20 31 2e 31 35 20 32 30 30 38 2f 30  .c,v 1.15 2008/0
846a0 33 2f 32 36 20 31 38 3a 33 34 3a 34 33 20 64 61  3/26 18:34:43 da
846b0 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a  nielk1977 Exp $.
846c0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  */../*.** This r
846d0 6f 75 74 69 6e 65 20 72 75 6e 73 20 77 68 65 6e  outine runs when
846e0 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
846f0 63 61 74 6f 72 20 73 65 65 73 20 74 68 61 74 20  cator sees that 
84700 74 68 65 0a 2a 2a 20 74 6f 74 61 6c 20 6d 65 6d  the.** total mem
84710 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  ory allocation i
84720 73 20 61 62 6f 75 74 20 74 6f 20 65 78 63 65 65  s about to excee
84730 64 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 0a  d the soft heap.
84740 2a 2a 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 73 74 61  ** limit..*/.sta
84750 74 69 63 20 76 6f 69 64 20 73 6f 66 74 48 65 61  tic void softHea
84760 70 4c 69 6d 69 74 45 6e 66 6f 72 63 65 72 28 0a  pLimitEnforcer(.
84770 20 20 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 2c    void *NotUsed,
84780 20 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36   .  sqlite3_int6
84790 34 20 69 6e 55 73 65 2c 0a 20 20 69 6e 74 20 61  4 inUse,.  int a
847a0 6c 6c 6f 63 53 69 7a 65 0a 29 7b 0a 20 20 73 71  llocSize.){.  sq
847b0 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65  lite3_release_me
847c0 6d 6f 72 79 28 61 6c 6c 6f 63 53 69 7a 65 29 3b  mory(allocSize);
847d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
847e0 65 20 73 6f 66 74 20 68 65 61 70 2d 73 69 7a 65  e soft heap-size
847f0 20 6c 69 6d 69 74 20 66 6f 72 20 74 68 65 20 63   limit for the c
84800 75 72 72 65 6e 74 20 74 68 72 65 61 64 2e 20 50  urrent thread. P
84810 61 73 73 69 6e 67 20 61 0a 2a 2a 20 7a 65 72 6f  assing a.** zero
84820 20 6f 72 20 6e 65 67 61 74 69 76 65 20 76 61 6c   or negative val
84830 75 65 20 69 6e 64 69 63 61 74 65 73 20 6e 6f 20  ue indicates no 
84840 6c 69 6d 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  limit..*/.SQLITE
84850 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
84860 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69  3_soft_heap_limi
84870 74 28 69 6e 74 20 6e 29 7b 0a 20 20 73 71 6c 69  t(int n){.  sqli
84880 74 65 33 5f 75 69 6e 74 36 34 20 69 4c 69 6d 69  te3_uint64 iLimi
84890 74 3b 0a 20 20 69 6e 74 20 6f 76 65 72 61 67 65  t;.  int overage
848a0 3b 0a 20 20 69 66 28 20 6e 3c 30 20 29 7b 0a 20  ;.  if( n<0 ){. 
848b0 20 20 20 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20     iLimit = 0;. 
848c0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 4c 69 6d   }else{.    iLim
848d0 69 74 20 3d 20 6e 3b 0a 20 20 7d 0a 20 20 69 66  it = n;.  }.  if
848e0 28 20 69 4c 69 6d 69 74 3e 30 20 29 7b 0a 20 20  ( iLimit>0 ){.  
848f0 20 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79    sqlite3_memory
84900 5f 61 6c 61 72 6d 28 73 6f 66 74 48 65 61 70 4c  _alarm(softHeapL
84910 69 6d 69 74 45 6e 66 6f 72 63 65 72 2c 20 30 2c  imitEnforcer, 0,
84920 20 69 4c 69 6d 69 74 29 3b 0a 20 20 7d 65 6c 73   iLimit);.  }els
84930 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  e{.    sqlite3_m
84940 65 6d 6f 72 79 5f 61 6c 61 72 6d 28 30 2c 20 30  emory_alarm(0, 0
84950 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 6f 76 65 72  , 0);.  }.  over
84960 61 67 65 20 3d 20 73 71 6c 69 74 65 33 5f 6d 65  age = sqlite3_me
84970 6d 6f 72 79 5f 75 73 65 64 28 29 20 2d 20 6e 3b  mory_used() - n;
84980 0a 20 20 69 66 28 20 6f 76 65 72 61 67 65 3e 30  .  if( overage>0
84990 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
849a0 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 6f  release_memory(o
849b0 76 65 72 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a  verage);.  }.}..
849c0 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 6d 65  /*.** Release me
849d0 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 53 51 4c  mory held by SQL
849e0 69 74 65 20 69 6e 73 74 61 6e 63 65 73 20 63 72  ite instances cr
849f0 65 61 74 65 64 20 62 79 20 74 68 65 20 63 75 72  eated by the cur
84a00 72 65 6e 74 20 74 68 72 65 61 64 2e 0a 2a 2f 0a  rent thread..*/.
84a10 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
84a20 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d  qlite3_release_m
84a30 65 6d 6f 72 79 28 69 6e 74 20 6e 29 7b 0a 23 69  emory(int n){.#i
84a40 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
84a50 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45  LE_MEMORY_MANAGE
84a60 4d 45 4e 54 0a 20 20 69 6e 74 20 6e 52 65 74 20  MENT.  int nRet 
84a70 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 6c  = sqlite3VdbeRel
84a80 65 61 73 65 4d 65 6d 6f 72 79 28 6e 29 3b 0a 20  easeMemory(n);. 
84a90 20 6e 52 65 74 20 2b 3d 20 73 71 6c 69 74 65 33   nRet += sqlite3
84aa0 50 61 67 65 72 52 65 6c 65 61 73 65 4d 65 6d 6f  PagerReleaseMemo
84ab0 72 79 28 6e 2d 6e 52 65 74 29 3b 0a 20 20 72 65  ry(n-nRet);.  re
84ac0 74 75 72 6e 20 6e 52 65 74 3b 0a 23 65 6c 73 65  turn nRet;.#else
84ad0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
84ae0 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 0a  _OK;.#endif.}...
84af0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61  /*.** Allocate a
84b00 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 0a  nd zero memory..
84b10 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */ .SQLITE_PRIVA
84b20 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
84b30 4d 61 6c 6c 6f 63 5a 65 72 6f 28 75 6e 73 69 67  MallocZero(unsig
84b40 6e 65 64 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a  ned n){.  void *
84b50 70 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  p = sqlite3_mall
84b60 6f 63 28 6e 29 3b 0a 20 20 69 66 28 20 70 20 29  oc(n);.  if( p )
84b70 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20  {.    memset(p, 
84b80 30 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74  0, n);.  }.  ret
84b90 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn p;.}../*.** 
84ba0 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a 65 72  Allocate and zer
84bb0 6f 20 6d 65 6d 6f 72 79 2e 20 20 49 66 20 74 68  o memory.  If th
84bc0 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69  e allocation fai
84bd0 6c 73 2c 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20  ls, make.** the 
84be0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61  mallocFailed fla
84bf0 67 20 69 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74  g in the connect
84c00 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a  ion pointer..*/.
84c10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
84c20 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61  oid *sqlite3DbMa
84c30 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c 69 74 65 33  llocZero(sqlite3
84c40 20 2a 64 62 2c 20 75 6e 73 69 67 6e 65 64 20 6e   *db, unsigned n
84c50 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73  ){.  void *p = s
84c60 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
84c70 77 28 64 62 2c 20 6e 29 3b 0a 20 20 69 66 28 20  w(db, n);.  if( 
84c80 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28  p ){.    memset(
84c90 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20  p, 0, n);.  }.  
84ca0 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
84cb0 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20  ** Allocate and 
84cc0 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 20 20 49 66  zero memory.  If
84cd0 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   the allocation 
84ce0 66 61 69 6c 73 2c 20 6d 61 6b 65 0a 2a 2a 20 74  fails, make.** t
84cf0 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  he mallocFailed 
84d00 66 6c 61 67 20 69 6e 20 74 68 65 20 63 6f 6e 6e  flag in the conn
84d10 65 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a  ection pointer..
84d20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
84d30 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44  E void *sqlite3D
84d40 62 4d 61 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74  bMallocRaw(sqlit
84d50 65 33 20 2a 64 62 2c 20 75 6e 73 69 67 6e 65 64  e3 *db, unsigned
84d60 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d   n){.  void *p =
84d70 20 30 3b 0a 20 20 69 66 28 20 21 64 62 20 7c 7c   0;.  if( !db ||
84d80 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
84d90 64 3d 3d 30 20 29 7b 0a 20 20 20 20 70 20 3d 20  d==0 ){.    p = 
84da0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
84db0 29 3b 0a 20 20 20 20 69 66 28 20 21 70 20 26 26  );.    if( !p &&
84dc0 20 64 62 20 29 7b 0a 20 20 20 20 20 20 64 62 2d   db ){.      db-
84dd0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
84de0 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  1;.    }.  }.  r
84df0 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
84e00 2a 20 52 65 73 69 7a 65 20 74 68 65 20 62 6c 6f  * Resize the blo
84e10 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 70 6f 69  ck of memory poi
84e20 6e 74 65 64 20 74 6f 20 62 79 20 70 20 74 6f 20  nted to by p to 
84e30 6e 20 62 79 74 65 73 2e 20 49 66 20 74 68 65 0a  n bytes. If the.
84e40 2a 2a 20 72 65 73 69 7a 65 20 66 61 69 6c 73 2c  ** resize fails,
84e50 20 73 65 74 20 74 68 65 20 6d 61 6c 6c 6f 63 46   set the mallocF
84e60 61 69 6c 65 64 20 66 6c 61 67 20 69 6e 74 68 65  ailed flag inthe
84e70 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a 65   connection obje
84e80 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ct..*/.SQLITE_PR
84e90 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
84ea0 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 73 71 6c  te3DbRealloc(sql
84eb0 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a  ite3 *db, void *
84ec0 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69  p, int n){.  voi
84ed0 64 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 69  d *pNew = 0;.  i
84ee0 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
84ef0 6c 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e  led==0 ){.    pN
84f00 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61  ew = sqlite3_rea
84f10 6c 6c 6f 63 28 70 2c 20 6e 29 3b 0a 20 20 20 20  lloc(p, n);.    
84f20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20  if( !pNew ){.   
84f30 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
84f40 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  led = 1;.    }. 
84f50 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77   }.  return pNew
84f60 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d  ;.}../*.** Attem
84f70 70 74 20 74 6f 20 72 65 61 6c 6c 6f 63 61 74 65  pt to reallocate
84f80 20 70 2e 20 20 49 66 20 74 68 65 20 72 65 61 6c   p.  If the real
84f90 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20  location fails, 
84fa0 74 68 65 6e 20 66 72 65 65 20 70 0a 2a 2a 20 61  then free p.** a
84fb0 6e 64 20 73 65 74 20 74 68 65 20 6d 61 6c 6c 6f  nd set the mallo
84fc0 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e 20  cFailed flag in 
84fd0 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
84fe0 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  nection..*/.SQLI
84ff0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
85000 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f  *sqlite3DbReallo
85010 63 4f 72 46 72 65 65 28 73 71 6c 69 74 65 33 20  cOrFree(sqlite3 
85020 2a 64 62 2c 20 76 6f 69 64 20 2a 70 2c 20 69 6e  *db, void *p, in
85030 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4e  t n){.  void *pN
85040 65 77 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c  ew;.  pNew = sql
85050 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62  ite3DbRealloc(db
85060 2c 20 70 2c 20 6e 29 3b 0a 20 20 69 66 28 20 21  , p, n);.  if( !
85070 70 4e 65 77 20 29 7b 0a 20 20 20 20 73 71 6c 69  pNew ){.    sqli
85080 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d  te3_free(p);.  }
85090 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a  .  return pNew;.
850a0 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20  }../*.** Make a 
850b0 63 6f 70 79 20 6f 66 20 61 20 73 74 72 69 6e 67  copy of a string
850c0 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69   in memory obtai
850d0 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d  ned from sqliteM
850e0 61 6c 6c 6f 63 28 29 2e 20 54 68 65 73 65 20 0a  alloc(). These .
850f0 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 63 61 6c  ** functions cal
85100 6c 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 52  l sqlite3MallocR
85110 61 77 28 29 20 64 69 72 65 63 74 6c 79 20 69 6e  aw() directly in
85120 73 74 65 61 64 20 6f 66 20 73 71 6c 69 74 65 4d  stead of sqliteM
85130 61 6c 6c 6f 63 28 29 2e 20 54 68 69 73 0a 2a 2a  alloc(). This.**
85140 20 69 73 20 62 65 63 61 75 73 65 20 77 68 65 6e   is because when
85150 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 67 69 6e   memory debuggin
85160 67 20 69 73 20 74 75 72 6e 65 64 20 6f 6e 2c 20  g is turned on, 
85170 74 68 65 73 65 20 74 77 6f 20 66 75 6e 63 74 69  these two functi
85180 6f 6e 73 20 61 72 65 20 0a 2a 2a 20 63 61 6c 6c  ons are .** call
85190 65 64 20 76 69 61 20 6d 61 63 72 6f 73 20 74 68  ed via macros th
851a0 61 74 20 72 65 63 6f 72 64 20 74 68 65 20 63 75  at record the cu
851b0 72 72 65 6e 74 20 66 69 6c 65 20 61 6e 64 20 6c  rrent file and l
851c0 69 6e 65 20 6e 75 6d 62 65 72 20 69 6e 20 74 68  ine number in th
851d0 65 0a 2a 2a 20 54 68 72 65 61 64 44 61 74 61 20  e.** ThreadData 
851e0 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51  structure..*/.SQ
851f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
85200 72 20 2a 73 71 6c 69 74 65 33 53 74 72 44 75 70  r *sqlite3StrDup
85210 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b  (const char *z){
85220 0a 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20  .  char *zNew;. 
85230 20 69 6e 74 20 6e 3b 0a 20 20 69 66 28 20 7a 3d   int n;.  if( z=
85240 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
85250 20 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 29 2b 31   n = strlen(z)+1
85260 3b 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74  ;.  zNew = sqlit
85270 65 33 5f 6d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20  e3_malloc(n);.  
85280 69 66 28 20 7a 4e 65 77 20 29 20 6d 65 6d 63 70  if( zNew ) memcp
85290 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29 3b 0a 20  y(zNew, z, n);. 
852a0 20 72 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a   return zNew;.}.
852b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
852c0 68 61 72 20 2a 73 71 6c 69 74 65 33 53 74 72 4e  har *sqlite3StrN
852d0 44 75 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  Dup(const char *
852e0 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61  z, int n){.  cha
852f0 72 20 2a 7a 4e 65 77 3b 0a 20 20 69 66 28 20 7a  r *zNew;.  if( z
85300 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
85310 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    zNew = sqlite3
85320 5f 6d 61 6c 6c 6f 63 28 6e 2b 31 29 3b 0a 20 20  _malloc(n+1);.  
85330 69 66 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20  if( zNew ){.    
85340 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20 7a 2c 20  memcpy(zNew, z, 
85350 6e 29 3b 0a 20 20 20 20 7a 4e 65 77 5b 6e 5d 20  n);.    zNew[n] 
85360 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 0;.  }.  retur
85370 6e 20 7a 4e 65 77 3b 0a 7d 0a 0a 53 51 4c 49 54  n zNew;.}..SQLIT
85380 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a  E_PRIVATE char *
85390 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
853a0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e  sqlite3 *db, con
853b0 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63  st char *z){.  c
853c0 68 61 72 20 2a 7a 4e 65 77 20 3d 20 73 71 6c 69  har *zNew = sqli
853d0 74 65 33 53 74 72 44 75 70 28 7a 29 3b 0a 20 20  te3StrDup(z);.  
853e0 69 66 28 20 7a 20 26 26 20 21 7a 4e 65 77 20 29  if( z && !zNew )
853f0 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63  {.    db->malloc
85400 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a  Failed = 1;.  }.
85410 20 20 72 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d    return zNew;.}
85420 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
85430 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62 53  char *sqlite3DbS
85440 74 72 4e 44 75 70 28 73 71 6c 69 74 65 33 20 2a  trNDup(sqlite3 *
85450 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
85460 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61  z, int n){.  cha
85470 72 20 2a 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65  r *zNew = sqlite
85480 33 53 74 72 4e 44 75 70 28 7a 2c 20 6e 29 3b 0a  3StrNDup(z, n);.
85490 20 20 69 66 28 20 7a 20 26 26 20 21 7a 4e 65 77    if( z && !zNew
854a0 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c   ){.    db->mall
854b0 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
854c0 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e 65 77 3b  }.  return zNew;
854d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  .}../*.** Create
854e0 20 61 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 74   a string from t
854f0 68 65 20 32 6e 64 20 61 6e 64 20 73 75 62 73 65  he 2nd and subse
85500 71 75 65 6e 74 20 61 72 67 75 6d 65 6e 74 73 20  quent arguments 
85510 28 75 70 20 74 6f 20 74 68 65 0a 2a 2a 20 66 69  (up to the.** fi
85520 72 73 74 20 4e 55 4c 4c 20 61 72 67 75 6d 65 6e  rst NULL argumen
85530 74 29 2c 20 73 74 6f 72 65 20 74 68 65 20 73 74  t), store the st
85540 72 69 6e 67 20 69 6e 20 6d 65 6d 6f 72 79 20 6f  ring in memory o
85550 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20  btained from.** 
85560 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 61  sqliteMalloc() a
85570 6e 64 20 6d 61 6b 65 20 74 68 65 20 70 6f 69 6e  nd make the poin
85580 74 65 72 20 69 6e 64 69 63 61 74 65 64 20 62 79  ter indicated by
85590 20 74 68 65 20 31 73 74 20 61 72 67 75 6d 65 6e   the 1st argumen
855a0 74 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 74 68  t.** point to th
855b0 61 74 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20  at string.  The 
855c0 31 73 74 20 61 72 67 75 6d 65 6e 74 20 6d 75 73  1st argument mus
855d0 74 20 65 69 74 68 65 72 20 62 65 20 4e 55 4c 4c  t either be NULL
855e0 20 6f 72 20 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f   or .** point to
855f0 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
85600 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c   from sqliteMall
85610 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  oc()..*/.SQLITE_
85620 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
85630 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 63 68  ite3SetString(ch
85640 61 72 20 2a 2a 70 7a 2c 20 2e 2e 2e 29 7b 0a 20  ar **pz, ...){. 
85650 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69   va_list ap;.  i
85660 6e 74 20 6e 42 79 74 65 3b 0a 20 20 63 6f 6e 73  nt nByte;.  cons
85670 74 20 63 68 61 72 20 2a 7a 3b 0a 20 20 63 68 61  t char *z;.  cha
85680 72 20 2a 7a 52 65 73 75 6c 74 3b 0a 0a 20 20 61  r *zResult;..  a
85690 73 73 65 72 74 28 20 70 7a 21 3d 30 20 29 3b 0a  ssert( pz!=0 );.
856a0 20 20 6e 42 79 74 65 20 3d 20 31 3b 0a 20 20 76    nByte = 1;.  v
856b0 61 5f 73 74 61 72 74 28 61 70 2c 20 70 7a 29 3b  a_start(ap, pz);
856c0 0a 20 20 77 68 69 6c 65 28 20 28 7a 20 3d 20 76  .  while( (z = v
856d0 61 5f 61 72 67 28 61 70 2c 20 63 6f 6e 73 74 20  a_arg(ap, const 
856e0 63 68 61 72 2a 29 29 21 3d 30 20 29 7b 0a 20 20  char*))!=0 ){.  
856f0 20 20 6e 42 79 74 65 20 2b 3d 20 73 74 72 6c 65    nByte += strle
85700 6e 28 7a 29 3b 0a 20 20 7d 0a 20 20 76 61 5f 65  n(z);.  }.  va_e
85710 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 65  nd(ap);.  sqlite
85720 33 5f 66 72 65 65 28 2a 70 7a 29 3b 0a 20 20 2a  3_free(*pz);.  *
85730 70 7a 20 3d 20 7a 52 65 73 75 6c 74 20 3d 20 73  pz = zResult = s
85740 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42  qlite3_malloc(nB
85750 79 74 65 29 3b 0a 20 20 69 66 28 20 7a 52 65 73  yte);.  if( zRes
85760 75 6c 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ult==0 ){.    re
85770 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 2a 7a 52 65  turn;.  }.  *zRe
85780 73 75 6c 74 20 3d 20 30 3b 0a 20 20 76 61 5f 73  sult = 0;.  va_s
85790 74 61 72 74 28 61 70 2c 20 70 7a 29 3b 0a 20 20  tart(ap, pz);.  
857a0 77 68 69 6c 65 28 20 28 7a 20 3d 20 76 61 5f 61  while( (z = va_a
857b0 72 67 28 61 70 2c 20 63 6f 6e 73 74 20 63 68 61  rg(ap, const cha
857c0 72 2a 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 69  r*))!=0 ){.    i
857d0 6e 74 20 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 29  nt n = strlen(z)
857e0 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 52 65  ;.    memcpy(zRe
857f0 73 75 6c 74 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20  sult, z, n);.   
85800 20 7a 52 65 73 75 6c 74 20 2b 3d 20 6e 3b 0a 20   zResult += n;. 
85810 20 7d 0a 20 20 7a 52 65 73 75 6c 74 5b 30 5d 20   }.  zResult[0] 
85820 3d 20 30 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70  = 0;.  va_end(ap
85830 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  );.}.../*.** Thi
85840 73 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20  s function must 
85850 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65  be called before
85860 20 65 78 69 74 69 6e 67 20 61 6e 79 20 41 50 49   exiting any API
85870 20 66 75 6e 63 74 69 6f 6e 20 28 69 2e 65 2e 20   function (i.e. 
85880 0a 2a 2a 20 72 65 74 75 72 6e 69 6e 67 20 63 6f  .** returning co
85890 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20 75 73 65  ntrol to the use
858a0 72 29 20 74 68 61 74 20 68 61 73 20 63 61 6c 6c  r) that has call
858b0 65 64 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  ed sqlite3_mallo
858c0 63 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  c or.** sqlite3_
858d0 72 65 61 6c 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 20 54  realloc..**.** T
858e0 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75  he returned valu
858f0 65 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 61 20  e is normally a 
85900 63 6f 70 79 20 6f 66 20 74 68 65 20 73 65 63 6f  copy of the seco
85910 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  nd argument to t
85920 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e  his.** function.
85930 20 48 6f 77 65 76 65 72 2c 20 69 66 20 61 20 6d   However, if a m
85940 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20  alloc() failure 
85950 68 61 73 20 6f 63 63 75 72 65 64 20 73 69 6e 63  has occured sinc
85960 65 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a 2a  e the previous.*
85970 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 53 51 4c  * invocation SQL
85980 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74  ITE_NOMEM is ret
85990 75 72 6e 65 64 20 69 6e 73 74 65 61 64 2e 20 0a  urned instead. .
859a0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 72  **.** If the fir
859b0 73 74 20 61 72 67 75 6d 65 6e 74 2c 20 64 62 2c  st argument, db,
859c0 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e 64   is not NULL and
859d0 20 61 20 6d 61 6c 6c 6f 63 28 29 20 65 72 72 6f   a malloc() erro
859e0 72 20 68 61 73 20 6f 63 63 75 72 65 64 2c 0a 2a  r has occured,.*
859f0 2a 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 6e 65  * then the conne
85a00 63 74 69 6f 6e 20 65 72 72 6f 72 2d 63 6f 64 65  ction error-code
85a10 20 28 74 68 65 20 76 61 6c 75 65 20 72 65 74 75   (the value retu
85a20 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  rned by sqlite3_
85a30 65 72 72 63 6f 64 65 28 29 29 0a 2a 2a 20 69 73  errcode()).** is
85a40 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4e   set to SQLITE_N
85a50 4f 4d 45 4d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  OMEM..*/.SQLITE_
85a60 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
85a70 74 65 33 41 70 69 45 78 69 74 28 73 71 6c 69 74  te3ApiExit(sqlit
85a80 65 33 2a 20 64 62 2c 20 69 6e 74 20 72 63 29 7b  e3* db, int rc){
85a90 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 62 20  .  /* If the db 
85aa0 68 61 6e 64 6c 65 20 69 73 20 6e 6f 74 20 4e 55  handle is not NU
85ab0 4c 4c 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 74  LL, then we must
85ac0 20 68 6f 6c 64 20 74 68 65 20 63 6f 6e 6e 65 63   hold the connec
85ad0 74 69 6f 6e 20 68 61 6e 64 6c 65 0a 20 20 2a 2a  tion handle.  **
85ae0 20 6d 75 74 65 78 20 68 65 72 65 2e 20 4f 74 68   mutex here. Oth
85af0 65 72 77 69 73 65 20 74 68 65 20 72 65 61 64 20  erwise the read 
85b00 28 61 6e 64 20 70 6f 73 73 69 62 6c 65 20 77 72  (and possible wr
85b10 69 74 65 29 20 6f 66 20 64 62 2d 3e 6d 61 6c 6c  ite) of db->mall
85b20 6f 63 46 61 69 6c 65 64 20 0a 20 20 2a 2a 20 69  ocFailed .  ** i
85b30 73 20 75 6e 73 61 66 65 2c 20 61 73 20 69 73 20  s unsafe, as is 
85b40 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  the call to sqli
85b50 74 65 33 45 72 72 6f 72 28 29 2e 0a 20 20 2a 2f  te3Error()..  */
85b60 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 20 7c  .  assert( !db |
85b70 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
85b80 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
85b90 29 3b 0a 20 20 69 66 28 20 64 62 20 26 26 20 64  );.  if( db && d
85ba0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
85bb0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
85bc0 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4e  ror(db, SQLITE_N
85bd0 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20 20 64 62  OMEM, 0);.    db
85be0 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
85bf0 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   0;.    rc = SQL
85c00 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
85c10 20 72 65 74 75 72 6e 20 72 63 20 26 20 28 64 62   return rc & (db
85c20 20 3f 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20 3a   ? db->errMask :
85c30 20 30 78 66 66 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a   0xff);.}../****
85c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
85c50 66 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a 2a 2a 2a  f malloc.c *****
85c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
85c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
85ca0 20 66 69 6c 65 20 70 72 69 6e 74 66 2e 63 20 2a   file printf.c *
85cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
85ce0 20 54 68 65 20 22 70 72 69 6e 74 66 22 20 63 6f   The "printf" co
85cf0 64 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20  de that follows 
85d00 64 61 74 65 73 20 66 72 6f 6d 20 74 68 65 20 31  dates from the 1
85d10 39 38 30 27 73 2e 20 20 49 74 20 69 73 20 69 6e  980's.  It is in
85d20 0a 2a 2a 20 74 68 65 20 70 75 62 6c 69 63 20 64  .** the public d
85d30 6f 6d 61 69 6e 2e 20 20 54 68 65 20 6f 72 69 67  omain.  The orig
85d40 69 6e 61 6c 20 63 6f 6d 6d 65 6e 74 73 20 61 72  inal comments ar
85d50 65 20 69 6e 63 6c 75 64 65 64 20 68 65 72 65 20  e included here 
85d60 66 6f 72 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 6e  for.** completen
85d70 65 73 73 2e 20 20 54 68 65 79 20 61 72 65 20 76  ess.  They are v
85d80 65 72 79 20 6f 75 74 2d 6f 66 2d 64 61 74 65 20  ery out-of-date 
85d90 62 75 74 20 6d 69 67 68 74 20 62 65 20 75 73 65  but might be use
85da0 66 75 6c 20 61 73 0a 2a 2a 20 61 6e 20 68 69 73  ful as.** an his
85db0 74 6f 72 69 63 61 6c 20 72 65 66 65 72 65 6e 63  torical referenc
85dc0 65 2e 20 20 4d 6f 73 74 20 6f 66 20 74 68 65 20  e.  Most of the 
85dd0 22 65 6e 68 61 6e 63 65 6d 65 6e 74 73 22 20 68  "enhancements" h
85de0 61 76 65 20 62 65 65 6e 20 62 61 63 6b 65 64 0a  ave been backed.
85df0 2a 2a 20 6f 75 74 20 73 6f 20 74 68 61 74 20 74  ** out so that t
85e00 68 65 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79  he functionality
85e10 20 69 73 20 6e 6f 77 20 74 68 65 20 73 61 6d 65   is now the same
85e20 20 61 73 20 73 74 61 6e 64 61 72 64 20 70 72 69   as standard pri
85e30 6e 74 66 28 29 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a  ntf()..**.******
85e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85e80 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ****.**.** The f
85e90 6f 6c 6c 6f 77 69 6e 67 20 6d 6f 64 75 6c 65 73  ollowing modules
85ea0 20 69 73 20 61 6e 20 65 6e 68 61 6e 63 65 64 20   is an enhanced 
85eb0 72 65 70 6c 61 63 65 6d 65 6e 74 20 66 6f 72 20  replacement for 
85ec0 74 68 65 20 22 70 72 69 6e 74 66 22 20 73 75 62  the "printf" sub
85ed0 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 66 6f 75 6e  routines.** foun
85ee0 64 20 69 6e 20 74 68 65 20 73 74 61 6e 64 61 72  d in the standar
85ef0 64 20 43 20 6c 69 62 72 61 72 79 2e 20 20 54 68  d C library.  Th
85f00 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 68 61  e following enha
85f10 6e 63 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20  ncements are.** 
85f20 73 75 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a  supported:.**.**
85f30 20 20 20 20 20 20 2b 20 20 41 64 64 69 74 69 6f        +  Additio
85f40 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20  nal functions.  
85f50 54 68 65 20 73 74 61 6e 64 61 72 64 20 73 65 74  The standard set
85f60 20 6f 66 20 22 70 72 69 6e 74 66 22 20 66 75 6e   of "printf" fun
85f70 63 74 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 20 20  ctions.**       
85f80 20 20 69 6e 63 6c 75 64 65 73 20 70 72 69 6e 74    includes print
85f90 66 2c 20 66 70 72 69 6e 74 66 2c 20 73 70 72 69  f, fprintf, spri
85fa0 6e 74 66 2c 20 76 70 72 69 6e 74 66 2c 20 76 66  ntf, vprintf, vf
85fb0 70 72 69 6e 74 66 2c 20 61 6e 64 0a 2a 2a 20 20  printf, and.**  
85fc0 20 20 20 20 20 20 20 76 73 70 72 69 6e 74 66 2e         vsprintf.
85fd0 20 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 61 64    This module ad
85fe0 64 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ds the following
85ff0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  :.**.**         
86000 20 20 2a 20 20 73 6e 70 72 69 6e 74 66 20 2d 2d    *  snprintf --
86010 20 57 6f 72 6b 73 20 6c 69 6b 65 20 73 70 72 69   Works like spri
86020 6e 74 66 2c 20 62 75 74 20 68 61 73 20 61 6e 20  ntf, but has an 
86030 65 78 74 72 61 20 61 72 67 75 6d 65 6e 74 0a 2a  extra argument.*
86040 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
86050 20 20 20 20 20 20 20 20 20 20 20 77 68 69 63 68             which
86060 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20   is the size of 
86070 74 68 65 20 62 75 66 66 65 72 20 77 72 69 74 74  the buffer writt
86080 65 6e 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  en to..**.**    
86090 20 20 20 20 20 20 20 2a 20 20 6d 70 72 69 6e 74         *  mprint
860a0 66 20 2d 2d 20 20 53 69 6d 69 6c 61 72 20 74 6f  f --  Similar to
860b0 20 73 70 72 69 6e 74 66 2e 20 20 57 72 69 74 65   sprintf.  Write
860c0 73 20 6f 75 74 70 75 74 20 74 6f 20 6d 65 6d 6f  s output to memo
860d0 72 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ry.**           
860e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f                 o
860f0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c  btained from mal
86100 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  loc..**.**      
86110 20 20 20 20 20 2a 20 20 78 70 72 69 6e 74 66 20       *  xprintf 
86120 2d 2d 20 20 43 61 6c 6c 73 20 61 20 66 75 6e 63  --  Calls a func
86130 74 69 6f 6e 20 74 6f 20 64 69 73 70 6f 73 65 20  tion to dispose 
86140 6f 66 20 6f 75 74 70 75 74 2e 0a 2a 2a 0a 2a 2a  of output..**.**
86150 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 6e 70             *  np
86160 72 69 6e 74 66 20 2d 2d 20 20 4e 6f 20 6f 75 74  rintf --  No out
86170 70 75 74 2c 20 62 75 74 20 72 65 74 75 72 6e 73  put, but returns
86180 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
86190 68 61 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20  haracters.**    
861a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
861b0 20 20 20 20 20 20 74 68 61 74 20 77 6f 75 6c 64        that would
861c0 20 68 61 76 65 20 62 65 65 6e 20 6f 75 74 70 75   have been outpu
861d0 74 20 62 79 20 70 72 69 6e 74 66 2e 0a 2a 2a 0a  t by printf..**.
861e0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20  **           *  
861f0 41 20 76 2d 20 76 65 72 73 69 6f 6e 20 28 65 78  A v- version (ex
86200 3a 20 76 73 6e 70 72 69 6e 74 66 29 20 6f 66 20  : vsnprintf) of 
86210 65 76 65 72 79 20 66 75 6e 63 74 69 6f 6e 20 69  every function i
86220 73 20 61 6c 73 6f 0a 2a 2a 20 20 20 20 20 20 20  s also.**       
86230 20 20 20 20 20 20 20 73 75 70 70 6c 69 65 64 2e         supplied.
86240 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41  .**.**      +  A
86250 20 66 65 77 20 65 78 74 65 6e 73 69 6f 6e 73 20   few extensions 
86260 74 6f 20 74 68 65 20 66 6f 72 6d 61 74 74 69 6e  to the formattin
86270 67 20 6e 6f 74 61 74 69 6f 6e 20 61 72 65 20 73  g notation are s
86280 75 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20  upported:.**.** 
86290 20 20 20 20 20 20 20 20 20 20 2a 20 20 54 68 65            *  The
862a0 20 22 3d 22 20 66 6c 61 67 20 28 73 69 6d 69 6c   "=" flag (simil
862b0 61 72 20 74 6f 20 22 2d 22 29 20 63 61 75 73 65  ar to "-") cause
862c0 73 20 74 68 65 20 6f 75 74 70 75 74 20 74 6f 20  s the output to 
862d0 62 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  be.**           
862e0 20 20 20 62 65 20 63 65 6e 74 65 72 65 64 20 69     be centered i
862f0 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  n the appropriat
86300 65 6c 79 20 73 69 7a 65 64 20 66 69 65 6c 64 2e  ely sized field.
86310 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .**.**          
86320 20 2a 20 20 54 68 65 20 25 62 20 66 69 65 6c 64   *  The %b field
86330 20 6f 75 74 70 75 74 73 20 61 6e 20 69 6e 74 65   outputs an inte
86340 67 65 72 20 69 6e 20 62 69 6e 61 72 79 20 6e 6f  ger in binary no
86350 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20  tation..**.**   
86360 20 20 20 20 20 20 20 20 2a 20 20 54 68 65 20 25          *  The %
86370 63 20 66 69 65 6c 64 20 6e 6f 77 20 61 63 63 65  c field now acce
86380 70 74 73 20 61 20 70 72 65 63 69 73 69 6f 6e 2e  pts a precision.
86390 20 20 54 68 65 20 63 68 61 72 61 63 74 65 72 20    The character 
863a0 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20 20 20 20  output.**       
863b0 20 20 20 20 20 20 20 69 73 20 72 65 70 65 61 74         is repeat
863c0 65 64 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72  ed by the number
863d0 20 6f 66 20 74 69 6d 65 73 20 74 68 65 20 70 72   of times the pr
863e0 65 63 69 73 69 6f 6e 20 73 70 65 63 69 66 69 65  ecision specifie
863f0 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  s..**.**        
86400 20 20 20 2a 20 20 54 68 65 20 25 27 20 66 69 65     *  The %' fie
86410 6c 64 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 63  ld works like %c
86420 2c 20 62 75 74 20 74 61 6b 65 73 20 61 73 20 69  , but takes as i
86430 74 73 20 63 68 61 72 61 63 74 65 72 20 74 68 65  ts character the
86440 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
86450 20 6e 65 78 74 20 63 68 61 72 61 63 74 65 72 20   next character 
86460 6f 66 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74  of the format st
86470 72 69 6e 67 2c 20 69 6e 73 74 65 61 64 20 6f 66  ring, instead of
86480 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 20 20 20   the next.**    
86490 20 20 20 20 20 20 20 20 20 20 61 72 67 75 6d 65            argume
864a0 6e 74 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65  nt.  For example
864b0 2c 20 20 70 72 69 6e 74 66 28 22 25 2e 37 38 27  ,  printf("%.78'
864c0 2d 22 29 20 20 70 72 69 6e 74 73 20 37 38 20 6d  -")  prints 78 m
864d0 69 6e 75 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  inus.**         
864e0 20 20 20 20 20 73 69 67 6e 73 2c 20 74 68 65 20       signs, the 
864f0 73 61 6d 65 20 61 73 20 20 70 72 69 6e 74 66 28  same as  printf(
86500 22 25 2e 37 38 63 22 2c 27 2d 27 29 2e 0a 2a 2a  "%.78c",'-')..**
86510 0a 2a 2a 20 20 20 20 20 20 2b 20 20 57 68 65 6e  .**      +  When
86520 20 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e 67 20   compiled using 
86530 47 43 43 20 6f 6e 20 61 20 53 50 41 52 43 2c 20  GCC on a SPARC, 
86540 74 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20  this version of 
86550 70 72 69 6e 74 66 20 69 73 0a 2a 2a 20 20 20 20  printf is.**    
86560 20 20 20 20 20 66 61 73 74 65 72 20 74 68 61 6e       faster than
86570 20 74 68 65 20 6c 69 62 72 61 72 79 20 70 72 69   the library pri
86580 6e 74 66 20 66 6f 72 20 53 55 4e 20 4f 53 20 34  ntf for SUN OS 4
86590 2e 31 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b  .1..**.**      +
865a0 20 20 41 6c 6c 20 66 75 6e 63 74 69 6f 6e 73 20    All functions 
865b0 61 72 65 20 66 75 6c 6c 79 20 72 65 65 6e 74 72  are fully reentr
865c0 61 6e 74 2e 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a  ant..**.*/../*.*
865d0 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70  * Conversion typ
865e0 65 73 20 66 61 6c 6c 20 69 6e 74 6f 20 76 61 72  es fall into var
865f0 69 6f 75 73 20 63 61 74 65 67 6f 72 69 65 73 20  ious categories 
86600 61 73 20 64 65 66 69 6e 65 64 20 62 79 20 74 68  as defined by th
86610 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 65  e.** following e
86620 6e 75 6d 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 23  numeration..*/.#
86630 64 65 66 69 6e 65 20 65 74 52 41 44 49 58 20 20  define etRADIX  
86640 20 20 20 20 20 31 20 2f 2a 20 49 6e 74 65 67 65       1 /* Intege
86650 72 20 74 79 70 65 73 2e 20 20 25 64 2c 20 25 78  r types.  %d, %x
86660 2c 20 25 6f 2c 20 61 6e 64 20 73 6f 20 66 6f 72  , %o, and so for
86670 74 68 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74  th */.#define et
86680 46 4c 4f 41 54 20 20 20 20 20 20 20 32 20 2f 2a  FLOAT       2 /*
86690 20 46 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 2e   Floating point.
866a0 20 20 25 66 20 2a 2f 0a 23 64 65 66 69 6e 65 20    %f */.#define 
866b0 65 74 45 58 50 20 20 20 20 20 20 20 20 20 33 20  etEXP         3 
866c0 2f 2a 20 45 78 70 6f 6e 65 6e 74 69 6f 6e 61 6c  /* Exponentional
866d0 20 6e 6f 74 61 74 69 6f 6e 2e 20 25 65 20 61 6e   notation. %e an
866e0 64 20 25 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20  d %E */.#define 
866f0 65 74 47 45 4e 45 52 49 43 20 20 20 20 20 34 20  etGENERIC     4 
86700 2f 2a 20 46 6c 6f 61 74 69 6e 67 20 6f 72 20 65  /* Floating or e
86710 78 70 6f 6e 65 6e 74 69 61 6c 2c 20 64 65 70 65  xponential, depe
86720 6e 64 69 6e 67 20 6f 6e 20 65 78 70 6f 6e 65 6e  nding on exponen
86730 74 2e 20 25 67 20 2a 2f 0a 23 64 65 66 69 6e 65  t. %g */.#define
86740 20 65 74 53 49 5a 45 20 20 20 20 20 20 20 20 35   etSIZE        5
86750 20 2f 2a 20 52 65 74 75 72 6e 20 6e 75 6d 62 65   /* Return numbe
86760 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  r of characters 
86770 70 72 6f 63 65 73 73 65 64 20 73 6f 20 66 61 72  processed so far
86780 2e 20 25 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  . %n */.#define 
86790 65 74 53 54 52 49 4e 47 20 20 20 20 20 20 36 20  etSTRING      6 
867a0 2f 2a 20 53 74 72 69 6e 67 73 2e 20 25 73 20 2a  /* Strings. %s *
867b0 2f 0a 23 64 65 66 69 6e 65 20 65 74 44 59 4e 53  /.#define etDYNS
867c0 54 52 49 4e 47 20 20 20 37 20 2f 2a 20 44 79 6e  TRING   7 /* Dyn
867d0 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74  amically allocat
867e0 65 64 20 73 74 72 69 6e 67 73 2e 20 25 7a 20 2a  ed strings. %z *
867f0 2f 0a 23 64 65 66 69 6e 65 20 65 74 50 45 52 43  /.#define etPERC
86800 45 4e 54 20 20 20 20 20 38 20 2f 2a 20 50 65 72  ENT     8 /* Per
86810 63 65 6e 74 20 73 79 6d 62 6f 6c 2e 20 25 25 20  cent symbol. %% 
86820 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 43 48 41  */.#define etCHA
86830 52 58 20 20 20 20 20 20 20 39 20 2f 2a 20 43 68  RX       9 /* Ch
86840 61 72 61 63 74 65 72 73 2e 20 25 63 20 2a 2f 0a  aracters. %c */.
86850 2f 2a 20 54 68 65 20 72 65 73 74 20 61 72 65 20  /* The rest are 
86860 65 78 74 65 6e 73 69 6f 6e 73 2c 20 6e 6f 74 20  extensions, not 
86870 6e 6f 72 6d 61 6c 6c 79 20 66 6f 75 6e 64 20 69  normally found i
86880 6e 20 70 72 69 6e 74 66 28 29 20 2a 2f 0a 23 64  n printf() */.#d
86890 65 66 69 6e 65 20 65 74 43 48 41 52 4c 49 54 20  efine etCHARLIT 
868a0 20 20 20 31 30 20 2f 2a 20 4c 69 74 65 72 61 6c     10 /* Literal
868b0 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 25 27   characters.  %'
868c0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 51   */.#define etSQ
868d0 4c 45 53 43 41 50 45 20 20 31 31 20 2f 2a 20 53  LESCAPE  11 /* S
868e0 74 72 69 6e 67 73 20 77 69 74 68 20 27 5c 27 27  trings with '\''
868f0 20 64 6f 75 62 6c 65 64 2e 20 20 25 71 20 2a 2f   doubled.  %q */
86900 0a 23 64 65 66 69 6e 65 20 65 74 53 51 4c 45 53  .#define etSQLES
86910 43 41 50 45 32 20 31 32 20 2f 2a 20 53 74 72 69  CAPE2 12 /* Stri
86920 6e 67 73 20 77 69 74 68 20 27 5c 27 27 20 64 6f  ngs with '\'' do
86930 75 62 6c 65 64 20 61 6e 64 20 65 6e 63 6c 6f 73  ubled and enclos
86940 65 64 20 69 6e 20 27 27 2c 0a 20 20 20 20 20 20  ed in '',.      
86950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
86960 20 20 20 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72      NULL pointer
86970 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 53 51  s replaced by SQ
86980 4c 20 4e 55 4c 4c 2e 20 20 25 51 20 2a 2f 0a 23  L NULL.  %Q */.#
86990 64 65 66 69 6e 65 20 65 74 54 4f 4b 45 4e 20 20  define etTOKEN  
869a0 20 20 20 20 31 33 20 2f 2a 20 61 20 70 6f 69 6e      13 /* a poin
869b0 74 65 72 20 74 6f 20 61 20 54 6f 6b 65 6e 20 73  ter to a Token s
869c0 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66  tructure */.#def
869d0 69 6e 65 20 65 74 53 52 43 4c 49 53 54 20 20 20  ine etSRCLIST   
869e0 20 31 34 20 2f 2a 20 61 20 70 6f 69 6e 74 65 72   14 /* a pointer
869f0 20 74 6f 20 61 20 53 72 63 4c 69 73 74 20 2a 2f   to a SrcList */
86a00 0a 23 64 65 66 69 6e 65 20 65 74 50 4f 49 4e 54  .#define etPOINT
86a10 45 52 20 20 20 20 31 35 20 2f 2a 20 54 68 65 20  ER    15 /* The 
86a20 25 70 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f  %p conversion */
86a30 0a 23 64 65 66 69 6e 65 20 65 74 53 51 4c 45 53  .#define etSQLES
86a40 43 41 50 45 33 20 31 36 20 2f 2a 20 25 77 20 2d  CAPE3 16 /* %w -
86a50 3e 20 53 74 72 69 6e 67 73 20 77 69 74 68 20 27  > Strings with '
86a60 5c 22 27 20 64 6f 75 62 6c 65 64 20 2a 2f 0a 23  \"' doubled */.#
86a70 64 65 66 69 6e 65 20 65 74 4f 52 44 49 4e 41 4c  define etORDINAL
86a80 20 20 20 20 31 37 20 2f 2a 20 25 72 20 2d 3e 20      17 /* %r -> 
86a90 31 73 74 2c 20 32 6e 64 2c 20 33 72 64 2c 20 34  1st, 2nd, 3rd, 4
86aa0 74 68 2c 20 65 74 63 2e 20 20 45 6e 67 6c 69 73  th, etc.  Englis
86ab0 68 20 6f 6e 6c 79 20 2a 2f 0a 0a 0a 2f 2a 0a 2a  h only */.../*.*
86ac0 2a 20 41 6e 20 22 65 74 42 79 74 65 22 20 69 73  * An "etByte" is
86ad0 20 61 6e 20 38 2d 62 69 74 20 75 6e 73 69 67 6e   an 8-bit unsign
86ae0 65 64 20 76 61 6c 75 65 2e 0a 2a 2f 0a 74 79 70  ed value..*/.typ
86af0 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20 63 68  edef unsigned ch
86b00 61 72 20 65 74 42 79 74 65 3b 0a 0a 2f 2a 0a 2a  ar etByte;../*.*
86b10 2a 20 45 61 63 68 20 62 75 69 6c 74 69 6e 20 63  * Each builtin c
86b20 6f 6e 76 65 72 73 69 6f 6e 20 63 68 61 72 61 63  onversion charac
86b30 74 65 72 20 28 65 78 3a 20 74 68 65 20 27 64 27  ter (ex: the 'd'
86b40 20 69 6e 20 22 25 64 22 29 20 69 73 20 64 65 73   in "%d") is des
86b50 63 72 69 62 65 64 0a 2a 2a 20 62 79 20 61 6e 20  cribed.** by an 
86b60 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
86b70 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
86b80 75 72 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ure.*/.typedef s
86b90 74 72 75 63 74 20 65 74 5f 69 6e 66 6f 20 7b 20  truct et_info { 
86ba0 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e    /* Information
86bb0 20 61 62 6f 75 74 20 65 61 63 68 20 66 6f 72 6d   about each form
86bc0 61 74 20 66 69 65 6c 64 20 2a 2f 0a 20 20 63 68  at field */.  ch
86bd0 61 72 20 66 6d 74 74 79 70 65 3b 20 20 20 20 20  ar fmttype;     
86be0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 6f         /* The fo
86bf0 72 6d 61 74 20 66 69 65 6c 64 20 63 6f 64 65 20  rmat field code 
86c00 6c 65 74 74 65 72 20 2a 2f 0a 20 20 65 74 42 79  letter */.  etBy
86c10 74 65 20 62 61 73 65 3b 20 20 20 20 20 20 20 20  te base;        
86c20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 61 73 65       /* The base
86c30 20 66 6f 72 20 72 61 64 69 78 20 63 6f 6e 76 65   for radix conve
86c40 72 73 69 6f 6e 20 2a 2f 0a 20 20 65 74 42 79 74  rsion */.  etByt
86c50 65 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20  e flags;        
86c60 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f      /* One or mo
86c70 72 65 20 6f 66 20 46 4c 41 47 5f 20 63 6f 6e 73  re of FLAG_ cons
86c80 74 61 6e 74 73 20 62 65 6c 6f 77 20 2a 2f 0a 20  tants below */. 
86c90 20 65 74 42 79 74 65 20 74 79 70 65 3b 20 20 20   etByte type;   
86ca0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e            /* Con
86cb0 76 65 72 73 69 6f 6e 20 70 61 72 61 64 69 67 6d  version paradigm
86cc0 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 63 68 61   */.  etByte cha
86cd0 72 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f  rset;          /
86ce0 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 44  * Offset into aD
86cf0 69 67 69 74 73 5b 5d 20 6f 66 20 74 68 65 20 64  igits[] of the d
86d00 69 67 69 74 73 20 73 74 72 69 6e 67 20 2a 2f 0a  igits string */.
86d10 20 20 65 74 42 79 74 65 20 70 72 65 66 69 78 3b    etByte prefix;
86d20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66             /* Of
86d30 66 73 65 74 20 69 6e 74 6f 20 61 50 72 65 66 69  fset into aPrefi
86d40 78 5b 5d 20 6f 66 20 74 68 65 20 70 72 65 66 69  x[] of the prefi
86d50 78 20 73 74 72 69 6e 67 20 2a 2f 0a 7d 20 65 74  x string */.} et
86d60 5f 69 6e 66 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c  _info;../*.** Al
86d70 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72  lowed values for
86d80 20 65 74 5f 69 6e 66 6f 2e 66 6c 61 67 73 0a 2a   et_info.flags.*
86d90 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 53  /.#define FLAG_S
86da0 49 47 4e 45 44 20 20 31 20 20 20 20 20 2f 2a 20  IGNED  1     /* 
86db0 54 72 75 65 20 69 66 20 74 68 65 20 76 61 6c 75  True if the valu
86dc0 65 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 73 20  e to convert is 
86dd0 73 69 67 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e  signed */.#defin
86de0 65 20 46 4c 41 47 5f 49 4e 54 45 52 4e 20 20 32  e FLAG_INTERN  2
86df0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
86e00 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65  for internal use
86e10 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65   only */.#define
86e20 20 46 4c 41 47 5f 53 54 52 49 4e 47 20 20 34 20   FLAG_STRING  4 
86e30 20 20 20 20 2f 2a 20 41 6c 6c 6f 77 20 69 6e 66      /* Allow inf
86e40 69 6e 69 74 79 20 70 72 65 63 69 73 69 6f 6e 20  inity precision 
86e50 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  */.../*.** The f
86e60 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 69  ollowing table i
86e70 73 20 73 65 61 72 63 68 65 64 20 6c 69 6e 65 61  s searched linea
86e80 72 6c 79 2c 20 73 6f 20 69 74 20 69 73 20 67 6f  rly, so it is go
86e90 6f 64 20 74 6f 20 70 75 74 20 74 68 65 0a 2a 2a  od to put the.**
86ea0 20 6d 6f 73 74 20 66 72 65 71 75 65 6e 74 6c 79   most frequently
86eb0 20 75 73 65 64 20 63 6f 6e 76 65 72 73 69 6f 6e   used conversion
86ec0 20 74 79 70 65 73 20 66 69 72 73 74 2e 0a 2a 2f   types first..*/
86ed0 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68  .static const ch
86ee0 61 72 20 61 44 69 67 69 74 73 5b 5d 20 3d 20 22  ar aDigits[] = "
86ef0 30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46  0123456789ABCDEF
86f00 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66  0123456789abcdef
86f10 22 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  ";.static const 
86f20 63 68 61 72 20 61 50 72 65 66 69 78 5b 5d 20 3d  char aPrefix[] =
86f30 20 22 2d 78 30 5c 30 30 30 58 30 22 3b 0a 73 74   "-x0\000X0";.st
86f40 61 74 69 63 20 63 6f 6e 73 74 20 65 74 5f 69 6e  atic const et_in
86f50 66 6f 20 66 6d 74 69 6e 66 6f 5b 5d 20 3d 20 7b  fo fmtinfo[] = {
86f60 0a 20 20 7b 20 20 27 64 27 2c 20 31 30 2c 20 31  .  {  'd', 10, 1
86f70 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20  , etRADIX,      
86f80 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 73  0,  0 },.  {  's
86f90 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 54 52 49  ',  0, 4, etSTRI
86fa0 4e 47 2c 20 20 20 20 20 30 2c 20 20 30 20 7d 2c  NG,     0,  0 },
86fb0 0a 20 20 7b 20 20 27 67 27 2c 20 20 30 2c 20 31  .  {  'g',  0, 1
86fc0 2c 20 65 74 47 45 4e 45 52 49 43 2c 20 20 20 20  , etGENERIC,    
86fd0 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27 7a  30, 0 },.  {  'z
86fe0 27 2c 20 20 30 2c 20 34 2c 20 65 74 44 59 4e 53  ',  0, 4, etDYNS
86ff0 54 52 49 4e 47 2c 20 20 30 2c 20 20 30 20 7d 2c  TRING,  0,  0 },
87000 0a 20 20 7b 20 20 27 71 27 2c 20 20 30 2c 20 34  .  {  'q',  0, 4
87010 2c 20 65 74 53 51 4c 45 53 43 41 50 45 2c 20 20  , etSQLESCAPE,  
87020 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 51  0,  0 },.  {  'Q
87030 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45  ',  0, 4, etSQLE
87040 53 43 41 50 45 32 2c 20 30 2c 20 20 30 20 7d 2c  SCAPE2, 0,  0 },
87050 0a 20 20 7b 20 20 27 77 27 2c 20 20 30 2c 20 34  .  {  'w',  0, 4
87060 2c 20 65 74 53 51 4c 45 53 43 41 50 45 33 2c 20  , etSQLESCAPE3, 
87070 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 63  0,  0 },.  {  'c
87080 27 2c 20 20 30 2c 20 30 2c 20 65 74 43 48 41 52  ',  0, 0, etCHAR
87090 58 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c  X,      0,  0 },
870a0 0a 20 20 7b 20 20 27 6f 27 2c 20 20 38 2c 20 30  .  {  'o',  8, 0
870b0 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20  , etRADIX,      
870c0 30 2c 20 20 32 20 7d 2c 0a 20 20 7b 20 20 27 75  0,  2 },.  {  'u
870d0 27 2c 20 31 30 2c 20 30 2c 20 65 74 52 41 44 49  ', 10, 0, etRADI
870e0 58 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c  X,      0,  0 },
870f0 0a 20 20 7b 20 20 27 78 27 2c 20 31 36 2c 20 30  .  {  'x', 16, 0
87100 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20  , etRADIX,      
87110 31 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 20 27 58  16, 1 },.  {  'X
87120 27 2c 20 31 36 2c 20 30 2c 20 65 74 52 41 44 49  ', 16, 0, etRADI
87130 58 2c 20 20 20 20 20 20 30 2c 20 20 34 20 7d 2c  X,      0,  4 },
87140 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
87150 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
87160 49 4e 54 0a 20 20 7b 20 20 27 66 27 2c 20 20 30  INT.  {  'f',  0
87170 2c 20 31 2c 20 65 74 46 4c 4f 41 54 2c 20 20 20  , 1, etFLOAT,   
87180 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20     0,  0 },.  { 
87190 20 27 65 27 2c 20 20 30 2c 20 31 2c 20 65 74 45   'e',  0, 1, etE
871a0 58 50 2c 20 20 20 20 20 20 20 20 33 30 2c 20 30  XP,        30, 0
871b0 20 7d 2c 0a 20 20 7b 20 20 27 45 27 2c 20 20 30   },.  {  'E',  0
871c0 2c 20 31 2c 20 65 74 45 58 50 2c 20 20 20 20 20  , 1, etEXP,     
871d0 20 20 20 31 34 2c 20 30 20 7d 2c 0a 20 20 7b 20     14, 0 },.  { 
871e0 20 27 47 27 2c 20 20 30 2c 20 31 2c 20 65 74 47   'G',  0, 1, etG
871f0 45 4e 45 52 49 43 2c 20 20 20 20 31 34 2c 20 30  ENERIC,    14, 0
87200 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 20 7b 20 20   },.#endif.  {  
87210 27 69 27 2c 20 31 30 2c 20 31 2c 20 65 74 52 41  'i', 10, 1, etRA
87220 44 49 58 2c 20 20 20 20 20 20 30 2c 20 20 30 20  DIX,      0,  0 
87230 7d 2c 0a 20 20 7b 20 20 27 6e 27 2c 20 20 30 2c  },.  {  'n',  0,
87240 20 30 2c 20 65 74 53 49 5a 45 2c 20 20 20 20 20   0, etSIZE,     
87250 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20    0,  0 },.  {  
87260 27 25 27 2c 20 20 30 2c 20 30 2c 20 65 74 50 45  '%',  0, 0, etPE
87270 52 43 45 4e 54 2c 20 20 20 20 30 2c 20 20 30 20  RCENT,    0,  0 
87280 7d 2c 0a 20 20 7b 20 20 27 70 27 2c 20 31 36 2c  },.  {  'p', 16,
87290 20 30 2c 20 65 74 50 4f 49 4e 54 45 52 2c 20 20   0, etPOINTER,  
872a0 20 20 30 2c 20 20 31 20 7d 2c 0a 20 20 7b 20 20    0,  1 },.  {  
872b0 27 54 27 2c 20 20 30 2c 20 32 2c 20 65 74 54 4f  'T',  0, 2, etTO
872c0 4b 45 4e 2c 20 20 20 20 20 20 30 2c 20 20 30 20  KEN,      0,  0 
872d0 7d 2c 0a 20 20 7b 20 20 27 53 27 2c 20 20 30 2c  },.  {  'S',  0,
872e0 20 32 2c 20 65 74 53 52 43 4c 49 53 54 2c 20 20   2, etSRCLIST,  
872f0 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20    0,  0 },.  {  
87300 27 72 27 2c 20 31 30 2c 20 33 2c 20 65 74 4f 52  'r', 10, 3, etOR
87310 44 49 4e 41 4c 2c 20 20 20 20 30 2c 20 20 30 20  DINAL,    0,  0 
87320 7d 2c 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 65 74  },.};.#define et
87330 4e 49 4e 46 4f 20 20 28 73 69 7a 65 6f 66 28 66  NINFO  (sizeof(f
87340 6d 74 69 6e 66 6f 29 2f 73 69 7a 65 6f 66 28 66  mtinfo)/sizeof(f
87350 6d 74 69 6e 66 6f 5b 30 5d 29 29 0a 0a 2f 2a 0a  mtinfo[0]))../*.
87360 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ** If SQLITE_OMI
87370 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
87380 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65   is defined, the
87390 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66 6c  n none of the fl
873a0 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20  oating point.** 
873b0 63 6f 6e 76 65 72 73 69 6f 6e 73 20 77 69 6c 6c  conversions will
873c0 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 69 66 6e 64 65   work..*/.#ifnde
873d0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
873e0 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 2f 2a 0a  OATING_POINT./*.
873f0 2a 2a 20 22 2a 76 61 6c 22 20 69 73 20 61 20 64  ** "*val" is a d
87400 6f 75 62 6c 65 20 73 75 63 68 20 74 68 61 74 20  ouble such that 
87410 30 2e 31 20 3c 3d 20 2a 76 61 6c 20 3c 20 31 30  0.1 <= *val < 10
87420 2e 30 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  .0.** Return the
87430 20 61 73 63 69 69 20 63 6f 64 65 20 66 6f 72 20   ascii code for 
87440 74 68 65 20 6c 65 61 64 69 6e 67 20 64 69 67 69  the leading digi
87450 74 20 6f 66 20 2a 76 61 6c 2c 20 74 68 65 6e 0a  t of *val, then.
87460 2a 2a 20 6d 75 6c 74 69 70 6c 79 20 22 2a 76 61  ** multiply "*va
87470 6c 22 20 62 79 20 31 30 2e 30 20 74 6f 20 72 65  l" by 10.0 to re
87480 6e 6f 72 6d 61 6c 69 7a 65 2e 0a 2a 2a 0a 2a 2a  normalize..**.**
87490 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a 20 20 20 20   Example:.**    
874a0 20 69 6e 70 75 74 3a 20 20 20 20 20 2a 76 61 6c   input:     *val
874b0 20 3d 20 33 2e 31 34 31 35 39 0a 2a 2a 20 20 20   = 3.14159.**   
874c0 20 20 6f 75 74 70 75 74 3a 20 20 20 20 2a 76 61    output:    *va
874d0 6c 20 3d 20 31 2e 34 31 35 39 20 20 20 20 66 75  l = 1.4159    fu
874e0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 20 3d 20  nction return = 
874f0 27 33 27 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  '3'.**.** The co
87500 75 6e 74 65 72 20 2a 63 6e 74 20 69 73 20 69 6e  unter *cnt is in
87510 63 72 65 6d 65 6e 74 65 64 20 65 61 63 68 20 74  cremented each t
87520 69 6d 65 2e 20 20 41 66 74 65 72 20 63 6f 75 6e  ime.  After coun
87530 74 65 72 20 65 78 63 65 65 64 73 0a 2a 2a 20 31  ter exceeds.** 1
87540 36 20 28 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  6 (the number of
87550 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67   significant dig
87560 69 74 73 20 69 6e 20 61 20 36 34 2d 62 69 74 20  its in a 64-bit 
87570 66 6c 6f 61 74 29 20 27 30 27 20 69 73 0a 2a 2a  float) '0' is.**
87580 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 65 64   always returned
87590 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
875a0 65 74 5f 67 65 74 64 69 67 69 74 28 4c 4f 4e 47  et_getdigit(LONG
875b0 44 4f 55 42 4c 45 5f 54 59 50 45 20 2a 76 61 6c  DOUBLE_TYPE *val
875c0 2c 20 69 6e 74 20 2a 63 6e 74 29 7b 0a 20 20 69  , int *cnt){.  i
875d0 6e 74 20 64 69 67 69 74 3b 0a 20 20 4c 4f 4e 47  nt digit;.  LONG
875e0 44 4f 55 42 4c 45 5f 54 59 50 45 20 64 3b 0a 20  DOUBLE_TYPE d;. 
875f0 20 69 66 28 20 28 2a 63 6e 74 29 2b 2b 20 3e 3d   if( (*cnt)++ >=
87600 20 31 36 20 29 20 72 65 74 75 72 6e 20 27 30 27   16 ) return '0'
87610 3b 0a 20 20 64 69 67 69 74 20 3d 20 28 69 6e 74  ;.  digit = (int
87620 29 2a 76 61 6c 3b 0a 20 20 64 20 3d 20 64 69 67  )*val;.  d = dig
87630 69 74 3b 0a 20 20 64 69 67 69 74 20 2b 3d 20 27  it;.  digit += '
87640 30 27 3b 0a 20 20 2a 76 61 6c 20 3d 20 28 2a 76  0';.  *val = (*v
87650 61 6c 20 2d 20 64 29 2a 31 30 2e 30 3b 0a 20 20  al - d)*10.0;.  
87660 72 65 74 75 72 6e 20 64 69 67 69 74 3b 0a 7d 0a  return digit;.}.
87670 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
87680 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
87690 4f 49 4e 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41  OINT */../*.** A
876a0 70 70 65 6e 64 20 4e 20 73 70 61 63 65 20 63 68  ppend N space ch
876b0 61 72 61 63 74 65 72 73 20 74 6f 20 74 68 65 20  aracters to the 
876c0 67 69 76 65 6e 20 73 74 72 69 6e 67 20 62 75 66  given string buf
876d0 66 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  fer..*/.static v
876e0 6f 69 64 20 61 70 70 65 6e 64 53 70 61 63 65 28  oid appendSpace(
876f0 53 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d  StrAccum *pAccum
87700 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 73 74 61 74  , int N){.  stat
87710 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 53  ic const char zS
87720 70 61 63 65 73 5b 5d 20 3d 20 22 20 20 20 20 20  paces[] = "     
87730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
87740 20 20 20 20 20 20 20 20 22 3b 0a 20 20 77 68 69          ";.  whi
87750 6c 65 28 20 4e 3e 3d 73 69 7a 65 6f 66 28 7a 53  le( N>=sizeof(zS
87760 70 61 63 65 73 29 2d 31 20 29 7b 0a 20 20 20 20  paces)-1 ){.    
87770 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41  sqlite3StrAccumA
87780 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a 53  ppend(pAccum, zS
87790 70 61 63 65 73 2c 20 73 69 7a 65 6f 66 28 7a 53  paces, sizeof(zS
877a0 70 61 63 65 73 29 2d 31 29 3b 0a 20 20 20 20 4e  paces)-1);.    N
877b0 20 2d 3d 20 73 69 7a 65 6f 66 28 7a 53 70 61 63   -= sizeof(zSpac
877c0 65 73 29 2d 31 3b 0a 20 20 7d 0a 20 20 69 66 28  es)-1;.  }.  if(
877d0 20 4e 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69   N>0 ){.    sqli
877e0 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
877f0 64 28 70 41 63 63 75 6d 2c 20 7a 53 70 61 63 65  d(pAccum, zSpace
87800 73 2c 20 4e 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  s, N);.  }.}../*
87810 0a 2a 2a 20 4f 6e 20 6d 61 63 68 69 6e 65 73 20  .** On machines 
87820 77 69 74 68 20 61 20 73 6d 61 6c 6c 20 73 74 61  with a small sta
87830 63 6b 20 73 69 7a 65 2c 20 79 6f 75 20 63 61 6e  ck size, you can
87840 20 72 65 64 65 66 69 6e 65 20 74 68 65 0a 2a 2a   redefine the.**
87850 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55   SQLITE_PRINT_BU
87860 46 5f 53 49 5a 45 20 74 6f 20 62 65 20 6c 65 73  F_SIZE to be les
87870 73 20 74 68 61 6e 20 33 35 30 2e 20 20 42 75 74  s than 350.  But
87880 20 62 65 77 61 72 65 20 2d 20 66 6f 72 0a 2a 2a   beware - for.**
87890 20 73 6d 61 6c 6c 65 72 20 76 61 6c 75 65 73 20   smaller values 
878a0 73 6f 6d 65 20 25 66 20 63 6f 6e 76 65 72 73 69  some %f conversi
878b0 6f 6e 73 20 6d 61 79 20 67 6f 20 69 6e 74 6f 20  ons may go into 
878c0 61 6e 20 69 6e 66 69 6e 69 74 65 20 6c 6f 6f 70  an infinite loop
878d0 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
878e0 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49  ITE_PRINT_BUF_SI
878f0 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  ZE.# define SQLI
87900 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a  TE_PRINT_BUF_SIZ
87910 45 20 33 35 30 0a 23 65 6e 64 69 66 0a 23 64 65  E 350.#endif.#de
87920 66 69 6e 65 20 65 74 42 55 46 53 49 5a 45 20 53  fine etBUFSIZE S
87930 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f  QLITE_PRINT_BUF_
87940 53 49 5a 45 20 20 2f 2a 20 53 69 7a 65 20 6f 66  SIZE  /* Size of
87950 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66   the output buff
87960 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  er */../*.** The
87970 20 72 6f 6f 74 20 70 72 6f 67 72 61 6d 2e 20 20   root program.  
87980 41 6c 6c 20 76 61 72 69 61 74 69 6f 6e 73 20 63  All variations c
87990 61 6c 6c 20 74 68 69 73 20 63 6f 72 65 2e 0a 2a  all this core..*
879a0 2a 0a 2a 2a 20 49 4e 50 55 54 53 3a 0a 2a 2a 20  *.** INPUTS:.** 
879b0 20 20 66 75 6e 63 20 20 20 54 68 69 73 20 69 73    func   This is
879c0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
879d0 66 75 6e 63 74 69 6f 6e 20 74 61 6b 69 6e 67 20  function taking 
879e0 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 0a  three arguments.
879f0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 31 2e  **            1.
87a00 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   A pointer to an
87a10 79 74 68 69 6e 67 2e 20 20 53 61 6d 65 20 61 73  ything.  Same as
87a20 20 74 68 65 20 22 61 72 67 22 20 70 61 72 61 6d   the "arg" param
87a30 65 74 65 72 2e 0a 2a 2a 20 20 20 20 20 20 20 20  eter..**        
87a40 20 20 20 20 32 2e 20 41 20 70 6f 69 6e 74 65 72      2. A pointer
87a50 20 74 6f 20 74 68 65 20 6c 69 73 74 20 6f 66 20   to the list of 
87a60 63 68 61 72 61 63 74 65 72 73 20 74 6f 20 62 65  characters to be
87a70 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20 20 20   output.**      
87a80 20 20 20 20 20 20 20 20 20 28 4e 6f 74 65 2c 20           (Note, 
87a90 74 68 69 73 20 6c 69 73 74 20 69 73 20 4e 4f 54  this list is NOT
87aa0 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 74 65 64   null terminated
87ab0 2e 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  .).**           
87ac0 20 33 2e 20 41 6e 20 69 6e 74 65 67 65 72 20 6e   3. An integer n
87ad0 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
87ae0 65 72 73 20 74 6f 20 62 65 20 6f 75 74 70 75 74  ers to be output
87af0 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  ..**            
87b00 20 20 20 28 4e 6f 74 65 3a 20 54 68 69 73 20 6e     (Note: This n
87b10 75 6d 62 65 72 20 6d 69 67 68 74 20 62 65 20 7a  umber might be z
87b20 65 72 6f 2e 29 0a 2a 2a 0a 2a 2a 20 20 20 61 72  ero.).**.**   ar
87b30 67 20 20 20 20 54 68 69 73 20 69 73 20 74 68 65  g    This is the
87b40 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 79 74   pointer to anyt
87b50 68 69 6e 67 20 77 68 69 63 68 20 77 69 6c 6c 20  hing which will 
87b60 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65  be passed as the
87b70 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 69 72  .**          fir
87b80 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 22  st argument to "
87b90 66 75 6e 63 22 2e 20 20 55 73 65 20 69 74 20 66  func".  Use it f
87ba0 6f 72 20 77 68 61 74 65 76 65 72 20 79 6f 75 20  or whatever you 
87bb0 6c 69 6b 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 66 6d  like..**.**   fm
87bc0 74 20 20 20 20 54 68 69 73 20 69 73 20 74 68 65  t    This is the
87bd0 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 2c 20   format string, 
87be0 61 73 20 69 6e 20 74 68 65 20 75 73 75 61 6c 20  as in the usual 
87bf0 70 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 61  print..**.**   a
87c00 70 20 20 20 20 20 54 68 69 73 20 69 73 20 61 20  p     This is a 
87c10 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 6c 69 73  pointer to a lis
87c20 74 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20  t of arguments. 
87c30 20 53 61 6d 65 20 61 73 20 69 6e 0a 2a 2a 20 20   Same as in.**  
87c40 20 20 20 20 20 20 20 20 76 66 70 72 69 6e 74 2e          vfprint.
87c50 0a 2a 2a 0a 2a 2a 20 4f 55 54 50 55 54 53 3a 0a  .**.** OUTPUTS:.
87c60 2a 2a 20 20 20 20 20 20 20 20 20 20 54 68 65 20  **          The 
87c70 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
87c80 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  the total number
87c90 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 73   of characters s
87ca0 65 6e 74 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20  ent to.**       
87cb0 20 20 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20     the function 
87cc0 22 66 75 6e 63 22 2e 20 20 52 65 74 75 72 6e 73  "func".  Returns
87cd0 20 2d 31 20 6f 6e 20 61 20 65 72 72 6f 72 2e 0a   -1 on a error..
87ce0 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
87cf0 74 68 65 20 6f 72 64 65 72 20 69 6e 20 77 68 69  the order in whi
87d00 63 68 20 61 75 74 6f 6d 61 74 69 63 20 76 61 72  ch automatic var
87d10 69 61 62 6c 65 73 20 61 72 65 20 64 65 63 6c 61  iables are decla
87d20 72 65 64 20 62 65 6c 6f 77 0a 2a 2a 20 73 65 65  red below.** see
87d30 6d 73 20 74 6f 20 6d 61 6b 65 20 61 20 62 69 67  ms to make a big
87d40 20 64 69 66 66 65 72 65 6e 63 65 20 69 6e 20 64   difference in d
87d50 65 74 65 72 6d 69 6e 69 6e 67 20 68 6f 77 20 66  etermining how f
87d60 61 73 74 20 74 68 69 73 20 62 65 61 73 74 0a 2a  ast this beast.*
87d70 2a 20 77 69 6c 6c 20 72 75 6e 2e 0a 2a 2f 0a 73  * will run..*/.s
87d80 74 61 74 69 63 20 76 6f 69 64 20 76 78 70 72 69  tatic void vxpri
87d90 6e 74 66 28 0a 20 20 53 74 72 41 63 63 75 6d 20  ntf(.  StrAccum 
87da0 2a 70 41 63 63 75 6d 2c 20 20 20 20 20 20 20 20  *pAccum,        
87db0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 63 63            /* Acc
87dc0 75 6d 75 6c 61 74 65 20 72 65 73 75 6c 74 73 20  umulate results 
87dd0 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 75 73  here */.  int us
87de0 65 45 78 74 65 6e 64 65 64 2c 20 20 20 20 20 20  eExtended,      
87df0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
87e00 41 6c 6c 6f 77 20 65 78 74 65 6e 64 65 64 20 25  Allow extended %
87e10 2d 63 6f 6e 76 65 72 73 69 6f 6e 73 20 2a 2f 0a  -conversions */.
87e20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 6d    const char *fm
87e30 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
87e40 20 20 20 20 20 2f 2a 20 46 6f 72 6d 61 74 20 73       /* Format s
87e50 74 72 69 6e 67 20 2a 2f 0a 20 20 76 61 5f 6c 69  tring */.  va_li
87e60 73 74 20 61 70 20 20 20 20 20 20 20 20 20 20 20  st ap           
87e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
87e80 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 29 7b   arguments */.){
87e90 0a 20 20 69 6e 74 20 63 3b 20 20 20 20 20 20 20  .  int c;       
87ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
87eb0 20 4e 65 78 74 20 63 68 61 72 61 63 74 65 72 20   Next character 
87ec0 69 6e 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74  in the format st
87ed0 72 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 2a  ring */.  char *
87ee0 62 75 66 70 74 3b 20 20 20 20 20 20 20 20 20 20  bufpt;          
87ef0 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
87f00 74 6f 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f  to the conversio
87f10 6e 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e  n buffer */.  in
87f20 74 20 70 72 65 63 69 73 69 6f 6e 3b 20 20 20 20  t precision;    
87f30 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 63           /* Prec
87f40 69 73 69 6f 6e 20 6f 66 20 74 68 65 20 63 75 72  ision of the cur
87f50 72 65 6e 74 20 66 69 65 6c 64 20 2a 2f 0a 20 20  rent field */.  
87f60 69 6e 74 20 6c 65 6e 67 74 68 3b 20 20 20 20 20  int length;     
87f70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
87f80 6e 67 74 68 20 6f 66 20 74 68 65 20 66 69 65 6c  ngth of the fiel
87f90 64 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 3b 20  d */.  int idx; 
87fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
87fb0 20 20 2f 2a 20 41 20 67 65 6e 65 72 61 6c 20 70    /* A general p
87fc0 75 72 70 6f 73 65 20 6c 6f 6f 70 20 63 6f 75 6e  urpose loop coun
87fd0 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 77 69 64  ter */.  int wid
87fe0 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  th;             
87ff0 20 20 20 20 2f 2a 20 57 69 64 74 68 20 6f 66 20      /* Width of 
88000 74 68 65 20 63 75 72 72 65 6e 74 20 66 69 65 6c  the current fiel
88010 64 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c  d */.  etByte fl
88020 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 3b 20  ag_leftjustify; 
88030 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 2d 22    /* True if "-"
88040 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74   flag is present
88050 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61   */.  etByte fla
88060 67 5f 70 6c 75 73 73 69 67 6e 3b 20 20 20 20 20  g_plussign;     
88070 20 2f 2a 20 54 72 75 65 20 69 66 20 22 2b 22 20   /* True if "+" 
88080 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 20  flag is present 
88090 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67  */.  etByte flag
880a0 5f 62 6c 61 6e 6b 73 69 67 6e 3b 20 20 20 20 20  _blanksign;     
880b0 2f 2a 20 54 72 75 65 20 69 66 20 22 20 22 20 66  /* True if " " f
880c0 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 20 2a  lag is present *
880d0 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f  /.  etByte flag_
880e0 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 3b 20 2f  alternateform; /
880f0 2a 20 54 72 75 65 20 69 66 20 22 23 22 20 66 6c  * True if "#" fl
88100 61 67 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f  ag is present */
88110 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 61  .  etByte flag_a
88120 6c 74 66 6f 72 6d 32 3b 20 20 20 20 20 20 2f 2a  ltform2;      /*
88130 20 54 72 75 65 20 69 66 20 22 21 22 20 66 6c 61   True if "!" fla
88140 67 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a  g is present */.
88150 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 7a 65    etByte flag_ze
88160 72 6f 70 61 64 3b 20 20 20 20 20 20 20 2f 2a 20  ropad;       /* 
88170 54 72 75 65 20 69 66 20 66 69 65 6c 64 20 77 69  True if field wi
88180 64 74 68 20 63 6f 6e 73 74 61 6e 74 20 73 74 61  dth constant sta
88190 72 74 73 20 77 69 74 68 20 7a 65 72 6f 20 2a 2f  rts with zero */
881a0 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 6c  .  etByte flag_l
881b0 6f 6e 67 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ong;          /*
881c0 20 54 72 75 65 20 69 66 20 22 6c 22 20 66 6c 61   True if "l" fla
881d0 67 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a  g is present */.
881e0 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 6c 6f    etByte flag_lo
881f0 6e 67 6c 6f 6e 67 3b 20 20 20 20 20 20 2f 2a 20  nglong;      /* 
88200 54 72 75 65 20 69 66 20 74 68 65 20 22 6c 6c 22  True if the "ll"
88210 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74   flag is present
88220 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 64 6f 6e   */.  etByte don
88230 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
88240 20 2f 2a 20 4c 6f 6f 70 20 74 65 72 6d 69 6e 61   /* Loop termina
88250 74 69 6f 6e 20 66 6c 61 67 20 2a 2f 0a 20 20 73  tion flag */.  s
88260 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 6c 6f 6e  qlite_uint64 lon
88270 67 76 61 6c 75 65 3b 20 20 20 2f 2a 20 56 61 6c  gvalue;   /* Val
88280 75 65 20 66 6f 72 20 69 6e 74 65 67 65 72 20 74  ue for integer t
88290 79 70 65 73 20 2a 2f 0a 20 20 4c 4f 4e 47 44 4f  ypes */.  LONGDO
882a0 55 42 4c 45 5f 54 59 50 45 20 72 65 61 6c 76 61  UBLE_TYPE realva
882b0 6c 75 65 3b 20 2f 2a 20 56 61 6c 75 65 20 66 6f  lue; /* Value fo
882c0 72 20 72 65 61 6c 20 74 79 70 65 73 20 2a 2f 0a  r real types */.
882d0 20 20 63 6f 6e 73 74 20 65 74 5f 69 6e 66 6f 20    const et_info 
882e0 2a 69 6e 66 6f 70 3b 20 20 20 20 20 20 2f 2a 20  *infop;      /* 
882f0 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61  Pointer to the a
88300 70 70 72 6f 70 72 69 61 74 65 20 69 6e 66 6f 20  ppropriate info 
88310 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 63  structure */.  c
88320 68 61 72 20 62 75 66 5b 65 74 42 55 46 53 49 5a  har buf[etBUFSIZ
88330 45 5d 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  E];       /* Con
88340 76 65 72 73 69 6f 6e 20 62 75 66 66 65 72 20 2a  version buffer *
88350 2f 0a 20 20 63 68 61 72 20 70 72 65 66 69 78 3b  /.  char prefix;
88360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
88370 2a 20 50 72 65 66 69 78 20 63 68 61 72 61 63 74  * Prefix charact
88380 65 72 2e 20 20 22 2b 22 20 6f 72 20 22 2d 22 20  er.  "+" or "-" 
88390 6f 72 20 22 20 22 20 6f 72 20 27 5c 30 27 2e 20  or " " or '\0'. 
883a0 2a 2f 0a 20 20 65 74 42 79 74 65 20 65 72 72 6f  */.  etByte erro
883b0 72 66 6c 61 67 20 3d 20 30 3b 20 20 20 20 20 20  rflag = 0;      
883c0 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20 65 72  /* True if an er
883d0 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  ror is encounter
883e0 65 64 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 78  ed */.  etByte x
883f0 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20  type;           
88400 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e     /* Conversion
88410 20 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20 20 63   paradigm */.  c
88420 68 61 72 20 2a 7a 45 78 74 72 61 3b 20 20 20 20  har *zExtra;    
88430 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74            /* Ext
88440 72 61 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 66  ra memory used f
88450 6f 72 20 65 74 54 43 4c 45 53 43 41 50 45 20 63  or etTCLESCAPE c
88460 6f 6e 76 65 72 73 69 6f 6e 73 20 2a 2f 0a 23 69  onversions */.#i
88470 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
88480 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
88490 0a 20 20 69 6e 74 20 20 65 78 70 2c 20 65 32 3b  .  int  exp, e2;
884a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
884b0 20 65 78 70 6f 6e 65 6e 74 20 6f 66 20 72 65 61   exponent of rea
884c0 6c 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20 64  l numbers */.  d
884d0 6f 75 62 6c 65 20 72 6f 75 6e 64 65 72 3b 20 20  ouble rounder;  
884e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
884f0 64 20 66 6f 72 20 72 6f 75 6e 64 69 6e 67 20 66  d for rounding f
88500 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61  loating point va
88510 6c 75 65 73 20 2a 2f 0a 20 20 65 74 42 79 74 65  lues */.  etByte
88520 20 66 6c 61 67 5f 64 70 3b 20 20 20 20 20 20 20   flag_dp;       
88530 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
88540 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 73 68  decimal point sh
88550 6f 75 6c 64 20 62 65 20 73 68 6f 77 6e 20 2a 2f  ould be shown */
88560 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 72  .  etByte flag_r
88570 74 7a 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  tz;           /*
88580 20 54 72 75 65 20 69 66 20 74 72 61 69 6c 69 6e   True if trailin
88590 67 20 7a 65 72 6f 73 20 73 68 6f 75 6c 64 20 62  g zeros should b
885a0 65 20 72 65 6d 6f 76 65 64 20 2a 2f 0a 20 20 65  e removed */.  e
885b0 74 42 79 74 65 20 66 6c 61 67 5f 65 78 70 3b 20  tByte flag_exp; 
885c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
885d0 65 20 74 6f 20 66 6f 72 63 65 20 64 69 73 70 6c  e to force displ
885e0 61 79 20 6f 66 20 74 68 65 20 65 78 70 6f 6e 65  ay of the expone
885f0 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 73 64 3b  nt */.  int nsd;
88600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
88610 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
88620 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69  significant digi
88630 74 73 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a 23  ts returned */.#
88640 65 6e 64 69 66 0a 0a 20 20 6c 65 6e 67 74 68 20  endif..  length 
88650 3d 20 30 3b 0a 20 20 62 75 66 70 74 20 3d 20 30  = 0;.  bufpt = 0
88660 3b 0a 20 20 66 6f 72 28 3b 20 28 63 3d 28 2a 66  ;.  for(; (c=(*f
88670 6d 74 29 29 21 3d 30 3b 20 2b 2b 66 6d 74 29 7b  mt))!=0; ++fmt){
88680 0a 20 20 20 20 69 66 28 20 63 21 3d 27 25 27 20  .    if( c!='%' 
88690 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 6d 74  ){.      int amt
886a0 3b 0a 20 20 20 20 20 20 62 75 66 70 74 20 3d 20  ;.      bufpt = 
886b0 28 63 68 61 72 20 2a 29 66 6d 74 3b 0a 20 20 20  (char *)fmt;.   
886c0 20 20 20 61 6d 74 20 3d 20 31 3b 0a 20 20 20 20     amt = 1;.    
886d0 20 20 77 68 69 6c 65 28 20 28 63 3d 28 2a 2b 2b    while( (c=(*++
886e0 66 6d 74 29 29 21 3d 27 25 27 20 26 26 20 63 21  fmt))!='%' && c!
886f0 3d 30 20 29 20 61 6d 74 2b 2b 3b 0a 20 20 20 20  =0 ) amt++;.    
88700 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75    sqlite3StrAccu
88710 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20  mAppend(pAccum, 
88720 62 75 66 70 74 2c 20 61 6d 74 29 3b 0a 20 20 20  bufpt, amt);.   
88730 20 20 20 69 66 28 20 63 3d 3d 30 20 29 20 62 72     if( c==0 ) br
88740 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  eak;.    }.    i
88750 66 28 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 3d  f( (c=(*++fmt))=
88760 3d 30 20 29 7b 0a 20 20 20 20 20 20 65 72 72 6f  =0 ){.      erro
88770 72 66 6c 61 67 20 3d 20 31 3b 0a 20 20 20 20 20  rflag = 1;.     
88780 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
88790 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 22  Append(pAccum, "
887a0 25 22 2c 20 31 29 3b 0a 20 20 20 20 20 20 62 72  %", 1);.      br
887b0 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f  eak;.    }.    /
887c0 2a 20 46 69 6e 64 20 6f 75 74 20 77 68 61 74 20  * Find out what 
887d0 66 6c 61 67 73 20 61 72 65 20 70 72 65 73 65 6e  flags are presen
887e0 74 20 2a 2f 0a 20 20 20 20 66 6c 61 67 5f 6c 65  t */.    flag_le
887f0 66 74 6a 75 73 74 69 66 79 20 3d 20 66 6c 61 67  ftjustify = flag
88800 5f 70 6c 75 73 73 69 67 6e 20 3d 20 66 6c 61 67  _plussign = flag
88810 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 0a 20 20  _blanksign = .  
88820 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74     flag_alternat
88830 65 66 6f 72 6d 20 3d 20 66 6c 61 67 5f 61 6c 74  eform = flag_alt
88840 66 6f 72 6d 32 20 3d 20 66 6c 61 67 5f 7a 65 72  form2 = flag_zer
88850 6f 70 61 64 20 3d 20 30 3b 0a 20 20 20 20 64 6f  opad = 0;.    do
88860 6e 65 20 3d 20 30 3b 0a 20 20 20 20 64 6f 7b 0a  ne = 0;.    do{.
88870 20 20 20 20 20 20 73 77 69 74 63 68 28 20 63 20        switch( c 
88880 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  ){.        case 
88890 27 2d 27 3a 20 20 20 66 6c 61 67 5f 6c 65 66 74  '-':   flag_left
888a0 6a 75 73 74 69 66 79 20 3d 20 31 3b 20 20 20 20  justify = 1;    
888b0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
888c0 63 61 73 65 20 27 2b 27 3a 20 20 20 66 6c 61 67  case '+':   flag
888d0 5f 70 6c 75 73 73 69 67 6e 20 3d 20 31 3b 20 20  _plussign = 1;  
888e0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
888f0 20 20 20 20 20 63 61 73 65 20 27 20 27 3a 20 20       case ' ':  
88900 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20   flag_blanksign 
88910 3d 20 31 3b 20 20 20 20 20 20 20 62 72 65 61 6b  = 1;       break
88920 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  ;.        case '
88930 23 27 3a 20 20 20 66 6c 61 67 5f 61 6c 74 65 72  #':   flag_alter
88940 6e 61 74 65 66 6f 72 6d 20 3d 20 31 3b 20 20 20  nateform = 1;   
88950 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63  break;.        c
88960 61 73 65 20 27 21 27 3a 20 20 20 66 6c 61 67 5f  ase '!':   flag_
88970 61 6c 74 66 6f 72 6d 32 20 3d 20 31 3b 20 20 20  altform2 = 1;   
88980 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
88990 20 20 20 20 63 61 73 65 20 27 30 27 3a 20 20 20      case '0':   
889a0 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 3d 20 31  flag_zeropad = 1
889b0 3b 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;         break;
889c0 0a 20 20 20 20 20 20 20 20 64 65 66 61 75 6c 74  .        default
889d0 3a 20 20 20 20 64 6f 6e 65 20 3d 20 31 3b 20 20  :    done = 1;  
889e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
889f0 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
88a00 20 20 7d 77 68 69 6c 65 28 20 21 64 6f 6e 65 20    }while( !done 
88a10 26 26 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 21  && (c=(*++fmt))!
88a20 3d 30 20 29 3b 0a 20 20 20 20 2f 2a 20 47 65 74  =0 );.    /* Get
88a30 20 74 68 65 20 66 69 65 6c 64 20 77 69 64 74 68   the field width
88a40 20 2a 2f 0a 20 20 20 20 77 69 64 74 68 20 3d 20   */.    width = 
88a50 30 3b 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 2a  0;.    if( c=='*
88a60 27 20 29 7b 0a 20 20 20 20 20 20 77 69 64 74 68  ' ){.      width
88a70 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74   = va_arg(ap,int
88a80 29 3b 0a 20 20 20 20 20 20 69 66 28 20 77 69 64  );.      if( wid
88a90 74 68 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20  th<0 ){.        
88aa0 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79  flag_leftjustify
88ab0 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 77 69   = 1;.        wi
88ac0 64 74 68 20 3d 20 2d 77 69 64 74 68 3b 0a 20 20  dth = -width;.  
88ad0 20 20 20 20 7d 0a 20 20 20 20 20 20 63 20 3d 20      }.      c = 
88ae0 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 7d 65 6c 73  *++fmt;.    }els
88af0 65 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  e{.      while( 
88b00 63 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27  c>='0' && c<='9'
88b10 20 29 7b 0a 20 20 20 20 20 20 20 20 77 69 64 74   ){.        widt
88b20 68 20 3d 20 77 69 64 74 68 2a 31 30 20 2b 20 63  h = width*10 + c
88b30 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20   - '0';.        
88b40 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20  c = *++fmt;.    
88b50 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
88b60 28 20 77 69 64 74 68 20 3e 20 65 74 42 55 46 53  ( width > etBUFS
88b70 49 5a 45 2d 31 30 20 29 7b 0a 20 20 20 20 20 20  IZE-10 ){.      
88b80 77 69 64 74 68 20 3d 20 65 74 42 55 46 53 49 5a  width = etBUFSIZ
88b90 45 2d 31 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20  E-10;.    }.    
88ba0 2f 2a 20 47 65 74 20 74 68 65 20 70 72 65 63 69  /* Get the preci
88bb0 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 28 20  sion */.    if( 
88bc0 63 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 20 20  c=='.' ){.      
88bd0 70 72 65 63 69 73 69 6f 6e 20 3d 20 30 3b 0a 20  precision = 0;. 
88be0 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b       c = *++fmt;
88bf0 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 2a  .      if( c=='*
88c00 27 20 29 7b 0a 20 20 20 20 20 20 20 20 70 72 65  ' ){.        pre
88c10 63 69 73 69 6f 6e 20 3d 20 76 61 5f 61 72 67 28  cision = va_arg(
88c20 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 20  ap,int);.       
88c30 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3c 30   if( precision<0
88c40 20 29 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 2d   ) precision = -
88c50 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20 20 20 20  precision;.     
88c60 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20     c = *++fmt;. 
88c70 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
88c80 20 20 20 20 77 68 69 6c 65 28 20 63 3e 3d 27 30      while( c>='0
88c90 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b 0a 20  ' && c<='9' ){. 
88ca0 20 20 20 20 20 20 20 20 20 70 72 65 63 69 73 69           precisi
88cb0 6f 6e 20 3d 20 70 72 65 63 69 73 69 6f 6e 2a 31  on = precision*1
88cc0 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 20  0 + c - '0';.   
88cd0 20 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d         c = *++fm
88ce0 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  t;.        }.   
88cf0 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
88d00 20 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20        precision 
88d10 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  = -1;.    }.    
88d20 2f 2a 20 47 65 74 20 74 68 65 20 63 6f 6e 76 65  /* Get the conve
88d30 72 73 69 6f 6e 20 74 79 70 65 20 6d 6f 64 69 66  rsion type modif
88d40 69 65 72 20 2a 2f 0a 20 20 20 20 69 66 28 20 63  ier */.    if( c
88d50 3d 3d 27 6c 27 20 29 7b 0a 20 20 20 20 20 20 66  =='l' ){.      f
88d60 6c 61 67 5f 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20  lag_long = 1;.  
88d70 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a      c = *++fmt;.
88d80 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 6c 27        if( c=='l'
88d90 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67   ){.        flag
88da0 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 31 3b 0a 20  _longlong = 1;. 
88db0 20 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d         c = *++fm
88dc0 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  t;.      }else{.
88dd0 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e          flag_lon
88de0 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20  glong = 0;.     
88df0 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
88e00 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20      flag_long = 
88e10 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20  flag_longlong = 
88e20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20  0;.    }.    /* 
88e30 46 65 74 63 68 20 74 68 65 20 69 6e 66 6f 20 65  Fetch the info e
88e40 6e 74 72 79 20 66 6f 72 20 74 68 65 20 66 69 65  ntry for the fie
88e50 6c 64 20 2a 2f 0a 20 20 20 20 69 6e 66 6f 70 20  ld */.    infop 
88e60 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 64 78  = 0;.    for(idx
88e70 3d 30 3b 20 69 64 78 3c 65 74 4e 49 4e 46 4f 3b  =0; idx<etNINFO;
88e80 20 69 64 78 2b 2b 29 7b 0a 20 20 20 20 20 20 69   idx++){.      i
88e90 66 28 20 63 3d 3d 66 6d 74 69 6e 66 6f 5b 69 64  f( c==fmtinfo[id
88ea0 78 5d 2e 66 6d 74 74 79 70 65 20 29 7b 0a 20 20  x].fmttype ){.  
88eb0 20 20 20 20 20 20 69 6e 66 6f 70 20 3d 20 26 66        infop = &f
88ec0 6d 74 69 6e 66 6f 5b 69 64 78 5d 3b 0a 20 20 20  mtinfo[idx];.   
88ed0 20 20 20 20 20 69 66 28 20 75 73 65 45 78 74 65       if( useExte
88ee0 6e 64 65 64 20 7c 7c 20 28 69 6e 66 6f 70 2d 3e  nded || (infop->
88ef0 66 6c 61 67 73 20 26 20 46 4c 41 47 5f 49 4e 54  flags & FLAG_INT
88f00 45 52 4e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ERN)==0 ){.     
88f10 20 20 20 20 20 78 74 79 70 65 20 3d 20 69 6e 66       xtype = inf
88f20 6f 70 2d 3e 74 79 70 65 3b 0a 20 20 20 20 20 20  op->type;.      
88f30 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
88f40 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20     return;.     
88f50 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65     }.        bre
88f60 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
88f70 7d 0a 20 20 20 20 7a 45 78 74 72 61 20 3d 20 30  }.    zExtra = 0
88f80 3b 0a 20 20 20 20 69 66 28 20 69 6e 66 6f 70 3d  ;.    if( infop=
88f90 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =0 ){.      retu
88fa0 72 6e 3b 0a 20 20 20 20 7d 0a 0a 0a 20 20 20 20  rn;.    }...    
88fb0 2f 2a 20 4c 69 6d 69 74 20 74 68 65 20 70 72 65  /* Limit the pre
88fc0 63 69 73 69 6f 6e 20 74 6f 20 70 72 65 76 65 6e  cision to preven
88fd0 74 20 6f 76 65 72 66 6c 6f 77 69 6e 67 20 62 75  t overflowing bu
88fe0 66 5b 5d 20 64 75 72 69 6e 67 20 63 6f 6e 76 65  f[] during conve
88ff0 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 28  rsion */.    if(
89000 20 70 72 65 63 69 73 69 6f 6e 3e 65 74 42 55 46   precision>etBUF
89010 53 49 5a 45 2d 34 30 20 26 26 20 28 69 6e 66 6f  SIZE-40 && (info
89020 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f  p->flags & FLAG_
89030 53 54 52 49 4e 47 29 3d 3d 30 20 29 7b 0a 20 20  STRING)==0 ){.  
89040 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20      precision = 
89050 65 74 42 55 46 53 49 5a 45 2d 34 30 3b 0a 20 20  etBUFSIZE-40;.  
89060 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20    }..    /*.    
89070 2a 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74  ** At this point
89080 2c 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20  , variables are 
89090 69 6e 69 74 69 61 6c 69 7a 65 64 20 61 73 20 66  initialized as f
890a0 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a 20  ollows:.    **. 
890b0 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74     **   flag_alt
890c0 65 72 6e 61 74 65 66 6f 72 6d 20 20 20 20 20 20  ernateform      
890d0 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27 23      TRUE if a '#
890e0 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20  ' is present..  
890f0 20 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 66    **   flag_altf
89100 6f 72 6d 32 20 20 20 20 20 20 20 20 20 20 20 20  orm2            
89110 20 20 20 54 52 55 45 20 69 66 20 61 20 27 21 27     TRUE if a '!'
89120 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20   is present..   
89130 20 2a 2a 20 20 20 66 6c 61 67 5f 70 6c 75 73 73   **   flag_pluss
89140 69 67 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  ign             
89150 20 20 54 52 55 45 20 69 66 20 61 20 27 2b 27 20    TRUE if a '+' 
89160 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20  is present..    
89170 2a 2a 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75  **   flag_leftju
89180 73 74 69 66 79 20 20 20 20 20 20 20 20 20 20 20  stify           
89190 20 54 52 55 45 20 69 66 20 61 20 27 2d 27 20 69   TRUE if a '-' i
891a0 73 20 70 72 65 73 65 6e 74 20 6f 72 20 69 66 20  s present or if 
891b0 74 68 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  the.    **      
891c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
891d0 20 20 20 20 20 20 20 20 20 66 69 65 6c 64 20 77           field w
891e0 69 64 74 68 20 77 61 73 20 6e 65 67 61 74 69 76  idth was negativ
891f0 65 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67  e..    **   flag
89200 5f 7a 65 72 6f 70 61 64 20 20 20 20 20 20 20 20  _zeropad        
89210 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20          TRUE if 
89220 74 68 65 20 77 69 64 74 68 20 62 65 67 61 6e 20  the width began 
89230 77 69 74 68 20 30 2e 0a 20 20 20 20 2a 2a 20 20  with 0..    **  
89240 20 66 6c 61 67 5f 6c 6f 6e 67 20 20 20 20 20 20   flag_long      
89250 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55               TRU
89260 45 20 69 66 20 74 68 65 20 6c 65 74 74 65 72 20  E if the letter 
89270 27 6c 27 20 28 65 6c 6c 29 20 70 72 65 66 69 78  'l' (ell) prefix
89280 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  ed.    **       
89290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
892a0 20 20 20 20 20 20 20 20 74 68 65 20 63 6f 6e 76          the conv
892b0 65 72 73 69 6f 6e 20 63 68 61 72 61 63 74 65 72  ersion character
892c0 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f  ..    **   flag_
892d0 6c 6f 6e 67 6c 6f 6e 67 20 20 20 20 20 20 20 20  longlong        
892e0 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 74         TRUE if t
892f0 68 65 20 6c 65 74 74 65 72 20 27 6c 6c 27 20 28  he letter 'll' (
89300 65 6c 6c 20 65 6c 6c 29 20 70 72 65 66 69 78 65  ell ell) prefixe
89310 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  d.    **        
89320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
89330 20 20 20 20 20 20 20 74 68 65 20 63 6f 6e 76 65         the conve
89340 72 73 69 6f 6e 20 63 68 61 72 61 63 74 65 72 2e  rsion character.
89350 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 62  .    **   flag_b
89360 6c 61 6e 6b 73 69 67 6e 20 20 20 20 20 20 20 20  lanksign        
89370 20 20 20 20 20 20 54 52 55 45 20 69 66 20 61 20        TRUE if a 
89380 27 20 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a  ' ' is present..
89390 20 20 20 20 2a 2a 20 20 20 77 69 64 74 68 20 20      **   width  
893a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
893b0 20 20 20 20 20 54 68 65 20 73 70 65 63 69 66 69       The specifi
893c0 65 64 20 66 69 65 6c 64 20 77 69 64 74 68 2e 20  ed field width. 
893d0 20 54 68 69 73 20 69 73 0a 20 20 20 20 2a 2a 20   This is.    ** 
893e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
893f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6c                al
89400 77 61 79 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76  ways non-negativ
89410 65 2e 20 20 5a 65 72 6f 20 69 73 20 74 68 65 20  e.  Zero is the 
89420 64 65 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2a 20  default..    ** 
89430 20 20 70 72 65 63 69 73 69 6f 6e 20 20 20 20 20    precision     
89440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68                Th
89450 65 20 73 70 65 63 69 66 69 65 64 20 70 72 65 63  e specified prec
89460 69 73 69 6f 6e 2e 20 20 54 68 65 20 64 65 66 61  ision.  The defa
89470 75 6c 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  ult.    **      
89480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
89490 20 20 20 20 20 20 20 20 20 69 73 20 2d 31 2e 0a           is -1..
894a0 20 20 20 20 2a 2a 20 20 20 78 74 79 70 65 20 20      **   xtype  
894b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
894c0 20 20 20 20 20 54 68 65 20 63 6c 61 73 73 20 6f       The class o
894d0 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  f the conversion
894e0 2e 0a 20 20 20 20 2a 2a 20 20 20 69 6e 66 6f 70  ..    **   infop
894f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
89500 20 20 20 20 20 20 20 50 6f 69 6e 74 65 72 20 74         Pointer t
89510 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  o the appropriat
89520 65 20 69 6e 66 6f 20 73 74 72 75 63 74 2e 0a 20  e info struct.. 
89530 20 20 20 2a 2f 0a 20 20 20 20 73 77 69 74 63 68     */.    switch
89540 28 20 78 74 79 70 65 20 29 7b 0a 20 20 20 20 20  ( xtype ){.     
89550 20 63 61 73 65 20 65 74 50 4f 49 4e 54 45 52 3a   case etPOINTER:
89560 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f  .        flag_lo
89570 6e 67 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28  nglong = sizeof(
89580 63 68 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 69  char*)==sizeof(i
89590 36 34 29 3b 0a 20 20 20 20 20 20 20 20 66 6c 61  64);.        fla
895a0 67 5f 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28  g_long = sizeof(
895b0 63 68 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 6c  char*)==sizeof(l
895c0 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ong int);.      
895d0 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67    /* Fall throug
895e0 68 20 69 6e 74 6f 20 74 68 65 20 6e 65 78 74 20  h into the next 
895f0 63 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 63 61  case */.      ca
89600 73 65 20 65 74 4f 52 44 49 4e 41 4c 3a 0a 20 20  se etORDINAL:.  
89610 20 20 20 20 63 61 73 65 20 65 74 52 41 44 49 58      case etRADIX
89620 3a 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 6e  :.        if( in
89630 66 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41  fop->flags & FLA
89640 47 5f 53 49 47 4e 45 44 20 29 7b 0a 20 20 20 20  G_SIGNED ){.    
89650 20 20 20 20 20 20 69 36 34 20 76 3b 0a 20 20 20        i64 v;.   
89660 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f         if( flag_
89670 6c 6f 6e 67 6c 6f 6e 67 20 29 20 20 20 76 20 3d  longlong )   v =
89680 20 76 61 5f 61 72 67 28 61 70 2c 69 36 34 29 3b   va_arg(ap,i64);
89690 0a 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20  .          else 
896a0 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 20 29 20  if( flag_long ) 
896b0 20 76 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 6c   v = va_arg(ap,l
896c0 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ong int);.      
896d0 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 20 20      else        
896e0 20 20 20 20 20 20 20 20 20 20 76 20 3d 20 76 61            v = va
896f0 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20  _arg(ap,int);.  
89700 20 20 20 20 20 20 20 20 69 66 28 20 76 3c 30 20          if( v<0 
89710 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c  ){.            l
89720 6f 6e 67 76 61 6c 75 65 20 3d 20 2d 76 3b 0a 20  ongvalue = -v;. 
89730 20 20 20 20 20 20 20 20 20 20 20 70 72 65 66 69             prefi
89740 78 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 20  x = '-';.       
89750 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
89760 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20        longvalue 
89770 3d 20 76 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = v;.           
89780 20 69 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 69   if( flag_plussi
89790 67 6e 20 29 20 20 20 20 20 20 20 20 70 72 65 66  gn )        pref
897a0 69 78 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 20  ix = '+';.      
897b0 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 66        else if( f
897c0 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20  lag_blanksign ) 
897d0 20 70 72 65 66 69 78 20 3d 20 27 20 27 3b 0a 20   prefix = ' ';. 
897e0 20 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20             else 
897f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
89800 20 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 30        prefix = 0
89810 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
89820 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
89830 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f         if( flag_
89840 6c 6f 6e 67 6c 6f 6e 67 20 29 20 20 20 6c 6f 6e  longlong )   lon
89850 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 67 28  gvalue = va_arg(
89860 61 70 2c 75 36 34 29 3b 0a 20 20 20 20 20 20 20  ap,u64);.       
89870 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67     else if( flag
89880 5f 6c 6f 6e 67 20 29 20 20 6c 6f 6e 67 76 61 6c  _long )  longval
89890 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75  ue = va_arg(ap,u
898a0 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 69 6e 74  nsigned long int
898b0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 65 6c 73  );.          els
898c0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
898d0 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76     longvalue = v
898e0 61 5f 61 72 67 28 61 70 2c 75 6e 73 69 67 6e 65  a_arg(ap,unsigne
898f0 64 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20  d int);.        
89900 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20    prefix = 0;.  
89910 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
89920 69 66 28 20 6c 6f 6e 67 76 61 6c 75 65 3d 3d 30  if( longvalue==0
89930 20 29 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74   ) flag_alternat
89940 65 66 6f 72 6d 20 3d 20 30 3b 0a 20 20 20 20 20  eform = 0;.     
89950 20 20 20 69 66 28 20 66 6c 61 67 5f 7a 65 72 6f     if( flag_zero
89960 70 61 64 20 26 26 20 70 72 65 63 69 73 69 6f 6e  pad && precision
89970 3c 77 69 64 74 68 2d 28 70 72 65 66 69 78 21 3d  <width-(prefix!=
89980 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  0) ){.          
89990 70 72 65 63 69 73 69 6f 6e 20 3d 20 77 69 64 74  precision = widt
899a0 68 2d 28 70 72 65 66 69 78 21 3d 30 29 3b 0a 20  h-(prefix!=0);. 
899b0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
899c0 20 62 75 66 70 74 20 3d 20 26 62 75 66 5b 65 74   bufpt = &buf[et
899d0 42 55 46 53 49 5a 45 2d 31 5d 3b 0a 20 20 20 20  BUFSIZE-1];.    
899e0 20 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65      if( xtype==e
899f0 74 4f 52 44 49 4e 41 4c 20 29 7b 0a 20 20 20 20  tORDINAL ){.    
89a00 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e        static con
89a10 73 74 20 63 68 61 72 20 7a 4f 72 64 5b 5d 20 3d  st char zOrd[] =
89a20 20 22 74 68 73 74 6e 64 72 64 22 3b 0a 20 20 20   "thstndrd";.   
89a30 20 20 20 20 20 20 20 69 6e 74 20 78 20 3d 20 6c         int x = l
89a40 6f 6e 67 76 61 6c 75 65 20 25 20 31 30 3b 0a 20  ongvalue % 10;. 
89a50 20 20 20 20 20 20 20 20 20 69 66 28 20 78 3e 3d           if( x>=
89a60 34 20 7c 7c 20 28 6c 6f 6e 67 76 61 6c 75 65 2f  4 || (longvalue/
89a70 31 30 29 25 31 30 3d 3d 31 20 29 7b 0a 20 20 20  10)%10==1 ){.   
89a80 20 20 20 20 20 20 20 20 20 78 20 3d 20 30 3b 0a           x = 0;.
89a90 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
89aa0 20 20 20 20 20 20 62 75 66 5b 65 74 42 55 46 53        buf[etBUFS
89ab0 49 5a 45 2d 33 5d 20 3d 20 7a 4f 72 64 5b 78 2a  IZE-3] = zOrd[x*
89ac0 32 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 62 75  2];.          bu
89ad0 66 5b 65 74 42 55 46 53 49 5a 45 2d 32 5d 20 3d  f[etBUFSIZE-2] =
89ae0 20 7a 4f 72 64 5b 78 2a 32 2b 31 5d 3b 0a 20 20   zOrd[x*2+1];.  
89af0 20 20 20 20 20 20 20 20 62 75 66 70 74 20 2d 3d          bufpt -=
89b00 20 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   2;.        }.  
89b10 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20        {.        
89b20 20 20 72 65 67 69 73 74 65 72 20 63 6f 6e 73 74    register const
89b30 20 63 68 61 72 20 2a 63 73 65 74 3b 20 20 20 20   char *cset;    
89b40 20 20 2f 2a 20 55 73 65 20 72 65 67 69 73 74 65    /* Use registe
89b50 72 73 20 66 6f 72 20 73 70 65 65 64 20 2a 2f 0a  rs for speed */.
89b60 20 20 20 20 20 20 20 20 20 20 72 65 67 69 73 74            regist
89b70 65 72 20 69 6e 74 20 62 61 73 65 3b 0a 20 20 20  er int base;.   
89b80 20 20 20 20 20 20 20 63 73 65 74 20 3d 20 26 61         cset = &a
89b90 44 69 67 69 74 73 5b 69 6e 66 6f 70 2d 3e 63 68  Digits[infop->ch
89ba0 61 72 73 65 74 5d 3b 0a 20 20 20 20 20 20 20 20  arset];.        
89bb0 20 20 62 61 73 65 20 3d 20 69 6e 66 6f 70 2d 3e    base = infop->
89bc0 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20  base;.          
89bd0 64 6f 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  do{             
89be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
89bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
89c00 20 43 6f 6e 76 65 72 74 20 74 6f 20 61 73 63 69   Convert to asci
89c10 69 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  i */.           
89c20 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 63 73   *(--bufpt) = cs
89c30 65 74 5b 6c 6f 6e 67 76 61 6c 75 65 25 62 61 73  et[longvalue%bas
89c40 65 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  e];.            
89c50 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 6c 6f 6e 67  longvalue = long
89c60 76 61 6c 75 65 2f 62 61 73 65 3b 0a 20 20 20 20  value/base;.    
89c70 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 6c 6f        }while( lo
89c80 6e 67 76 61 6c 75 65 3e 30 20 29 3b 0a 20 20 20  ngvalue>0 );.   
89c90 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6c       }.        l
89ca0 65 6e 67 74 68 20 3d 20 26 62 75 66 5b 65 74 42  ength = &buf[etB
89cb0 55 46 53 49 5a 45 2d 31 5d 2d 62 75 66 70 74 3b  UFSIZE-1]-bufpt;
89cc0 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 64 78  .        for(idx
89cd0 3d 70 72 65 63 69 73 69 6f 6e 2d 6c 65 6e 67 74  =precision-lengt
89ce0 68 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 29  h; idx>0; idx--)
89cf0 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d  {.          *(--
89d00 62 75 66 70 74 29 20 3d 20 27 30 27 3b 20 20 20  bufpt) = '0';   
89d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
89d20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 5a 65 72            /* Zer
89d30 6f 20 70 61 64 20 2a 2f 0a 20 20 20 20 20 20 20  o pad */.       
89d40 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70   }.        if( p
89d50 72 65 66 69 78 20 29 20 2a 28 2d 2d 62 75 66 70  refix ) *(--bufp
89d60 74 29 20 3d 20 70 72 65 66 69 78 3b 20 20 20 20  t) = prefix;    
89d70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
89d80 64 20 73 69 67 6e 20 2a 2f 0a 20 20 20 20 20 20  d sign */.      
89d90 20 20 69 66 28 20 66 6c 61 67 5f 61 6c 74 65 72    if( flag_alter
89da0 6e 61 74 65 66 6f 72 6d 20 26 26 20 69 6e 66 6f  nateform && info
89db0 70 2d 3e 70 72 65 66 69 78 20 29 7b 20 20 20 20  p->prefix ){    
89dc0 20 20 2f 2a 20 41 64 64 20 22 30 22 20 6f 72 20    /* Add "0" or 
89dd0 22 30 78 22 20 2a 2f 0a 20 20 20 20 20 20 20 20  "0x" */.        
89de0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 72    const char *pr
89df0 65 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61  e;.          cha
89e00 72 20 78 3b 0a 20 20 20 20 20 20 20 20 20 20 70  r x;.          p
89e10 72 65 20 3d 20 26 61 50 72 65 66 69 78 5b 69 6e  re = &aPrefix[in
89e20 66 6f 70 2d 3e 70 72 65 66 69 78 5d 3b 0a 20 20  fop->prefix];.  
89e30 20 20 20 20 20 20 20 20 69 66 28 20 2a 62 75 66          if( *buf
89e40 70 74 21 3d 70 72 65 5b 30 5d 20 29 7b 0a 20 20  pt!=pre[0] ){.  
89e50 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20            for(; 
89e60 28 78 3d 28 2a 70 72 65 29 29 21 3d 30 3b 20 70  (x=(*pre))!=0; p
89e70 72 65 2b 2b 29 20 2a 28 2d 2d 62 75 66 70 74 29  re++) *(--bufpt)
89e80 20 3d 20 78 3b 0a 20 20 20 20 20 20 20 20 20 20   = x;.          
89e90 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
89ea0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 26 62 75      length = &bu
89eb0 66 5b 65 74 42 55 46 53 49 5a 45 2d 31 5d 2d 62  f[etBUFSIZE-1]-b
89ec0 75 66 70 74 3b 0a 20 20 20 20 20 20 20 20 62 72  ufpt;.        br
89ed0 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
89ee0 65 74 46 4c 4f 41 54 3a 0a 20 20 20 20 20 20 63  etFLOAT:.      c
89ef0 61 73 65 20 65 74 45 58 50 3a 0a 20 20 20 20 20  ase etEXP:.     
89f00 20 63 61 73 65 20 65 74 47 45 4e 45 52 49 43 3a   case etGENERIC:
89f10 0a 20 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c  .        realval
89f20 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 64  ue = va_arg(ap,d
89f30 6f 75 62 6c 65 29 3b 0a 23 69 66 6e 64 65 66 20  ouble);.#ifndef 
89f40 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
89f50 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 20  TING_POINT.     
89f60 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e     if( precision
89f70 3c 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 3d  <0 ) precision =
89f80 20 36 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53   6;         /* S
89f90 65 74 20 64 65 66 61 75 6c 74 20 70 72 65 63 69  et default preci
89fa0 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20  sion */.        
89fb0 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 65 74  if( precision>et
89fc0 42 55 46 53 49 5a 45 2f 32 2d 31 30 20 29 20 70  BUFSIZE/2-10 ) p
89fd0 72 65 63 69 73 69 6f 6e 20 3d 20 65 74 42 55 46  recision = etBUF
89fe0 53 49 5a 45 2f 32 2d 31 30 3b 0a 20 20 20 20 20  SIZE/2-10;.     
89ff0 20 20 20 69 66 28 20 72 65 61 6c 76 61 6c 75 65     if( realvalue
8a000 3c 30 2e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  <0.0 ){.        
8a010 20 20 72 65 61 6c 76 61 6c 75 65 20 3d 20 2d 72    realvalue = -r
8a020 65 61 6c 76 61 6c 75 65 3b 0a 20 20 20 20 20 20  ealvalue;.      
8a030 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 2d 27      prefix = '-'
8a040 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
8a050 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66  .          if( f
8a060 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 29 20 20  lag_plussign )  
8a070 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d          prefix =
8a080 20 27 2b 27 3b 0a 20 20 20 20 20 20 20 20 20 20   '+';.          
8a090 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 62 6c  else if( flag_bl
8a0a0 61 6e 6b 73 69 67 6e 20 29 20 20 20 20 70 72 65  anksign )    pre
8a0b0 66 69 78 20 3d 20 27 20 27 3b 0a 20 20 20 20 20  fix = ' ';.     
8a0c0 20 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 20       else       
8a0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8a0e0 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20    prefix = 0;.  
8a0f0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
8a100 69 66 28 20 78 74 79 70 65 3d 3d 65 74 47 45 4e  if( xtype==etGEN
8a110 45 52 49 43 20 26 26 20 70 72 65 63 69 73 69 6f  ERIC && precisio
8a120 6e 3e 30 20 29 20 70 72 65 63 69 73 69 6f 6e 2d  n>0 ) precision-
8a130 2d 3b 0a 23 69 66 20 30 0a 20 20 20 20 20 20 20  -;.#if 0.       
8a140 20 2f 2a 20 52 6f 75 6e 64 69 6e 67 20 77 6f 72   /* Rounding wor
8a150 6b 73 20 6c 69 6b 65 20 42 53 44 20 77 68 65 6e  ks like BSD when
8a160 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 30 2e   the constant 0.
8a170 34 39 39 39 20 69 73 20 75 73 65 64 2e 20 20 57  4999 is used.  W
8a180 69 65 72 64 21 20 2a 2f 0a 20 20 20 20 20 20 20  ierd! */.       
8a190 20 66 6f 72 28 69 64 78 3d 70 72 65 63 69 73 69   for(idx=precisi
8a1a0 6f 6e 2c 20 72 6f 75 6e 64 65 72 3d 30 2e 34 39  on, rounder=0.49
8a1b0 39 39 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d  99; idx>0; idx--
8a1c0 2c 20 72 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 3b  , rounder*=0.1);
8a1d0 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 2f  .#else.        /
8a1e0 2a 20 49 74 20 6d 61 6b 65 73 20 6d 6f 72 65 20  * It makes more 
8a1f0 73 65 6e 73 65 20 74 6f 20 75 73 65 20 30 2e 35  sense to use 0.5
8a200 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28   */.        for(
8a210 69 64 78 3d 70 72 65 63 69 73 69 6f 6e 2c 20 72  idx=precision, r
8a220 6f 75 6e 64 65 72 3d 30 2e 35 3b 20 69 64 78 3e  ounder=0.5; idx>
8a230 30 3b 20 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65  0; idx--, rounde
8a240 72 2a 3d 30 2e 31 29 7b 7d 0a 23 65 6e 64 69 66  r*=0.1){}.#endif
8a250 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 74 79  .        if( xty
8a260 70 65 3d 3d 65 74 46 4c 4f 41 54 20 29 20 72 65  pe==etFLOAT ) re
8a270 61 6c 76 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64  alvalue += round
8a280 65 72 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e  er;.        /* N
8a290 6f 72 6d 61 6c 69 7a 65 20 72 65 61 6c 76 61 6c  ormalize realval
8a2a0 75 65 20 74 6f 20 77 69 74 68 69 6e 20 31 30 2e  ue to within 10.
8a2b0 30 20 3e 20 72 65 61 6c 76 61 6c 75 65 20 3e 3d  0 > realvalue >=
8a2c0 20 31 2e 30 20 2a 2f 0a 20 20 20 20 20 20 20 20   1.0 */.        
8a2d0 65 78 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  exp = 0;.       
8a2e0 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61   if( sqlite3IsNa
8a2f0 4e 28 72 65 61 6c 76 61 6c 75 65 29 20 29 7b 0a  N(realvalue) ){.
8a300 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20            bufpt 
8a310 3d 20 22 4e 61 4e 22 3b 0a 20 20 20 20 20 20 20  = "NaN";.       
8a320 20 20 20 6c 65 6e 67 74 68 20 3d 20 33 3b 0a 20     length = 3;. 
8a330 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
8a340 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
8a350 20 20 69 66 28 20 72 65 61 6c 76 61 6c 75 65 3e    if( realvalue>
8a360 30 2e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  0.0 ){.         
8a370 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75   while( realvalu
8a380 65 3e 3d 31 65 33 32 20 26 26 20 65 78 70 3c 3d  e>=1e32 && exp<=
8a390 33 35 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65  350 ){ realvalue
8a3a0 20 2a 3d 20 31 65 2d 33 32 3b 20 65 78 70 2b 3d   *= 1e-32; exp+=
8a3b0 33 32 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 20  32; }.          
8a3c0 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65  while( realvalue
8a3d0 3e 3d 31 65 38 20 26 26 20 65 78 70 3c 3d 33 35  >=1e8 && exp<=35
8a3e0 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a  0 ){ realvalue *
8a3f0 3d 20 31 65 2d 38 3b 20 65 78 70 2b 3d 38 3b 20  = 1e-8; exp+=8; 
8a400 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c  }.          whil
8a410 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30  e( realvalue>=10
8a420 2e 30 20 26 26 20 65 78 70 3c 3d 33 35 30 20 29  .0 && exp<=350 )
8a430 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 30  { realvalue *= 0
8a440 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 20  .1; exp++; }.   
8a450 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 65         while( re
8a460 61 6c 76 61 6c 75 65 3c 31 65 2d 38 20 26 26 20  alvalue<1e-8 && 
8a470 65 78 70 3e 3d 2d 33 35 30 20 29 7b 20 72 65 61  exp>=-350 ){ rea
8a480 6c 76 61 6c 75 65 20 2a 3d 20 31 65 38 3b 20 65  lvalue *= 1e8; e
8a490 78 70 2d 3d 38 3b 20 7d 0a 20 20 20 20 20 20 20  xp-=8; }.       
8a4a0 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61     while( realva
8a4b0 6c 75 65 3c 31 2e 30 20 26 26 20 65 78 70 3e 3d  lue<1.0 && exp>=
8a4c0 2d 33 35 30 20 29 7b 20 72 65 61 6c 76 61 6c 75  -350 ){ realvalu
8a4d0 65 20 2a 3d 20 31 30 2e 30 3b 20 65 78 70 2d 2d  e *= 10.0; exp--
8a4e0 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66  ; }.          if
8a4f0 28 20 65 78 70 3e 33 35 30 20 7c 7c 20 65 78 70  ( exp>350 || exp
8a500 3c 2d 33 35 30 20 29 7b 0a 20 20 20 20 20 20 20  <-350 ){.       
8a510 20 20 20 20 20 69 66 28 20 70 72 65 66 69 78 3d       if( prefix=
8a520 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='-' ){.        
8a530 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 2d        bufpt = "-
8a540 49 6e 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20  Inf";.          
8a550 20 20 7d 65 6c 73 65 20 69 66 28 20 70 72 65 66    }else if( pref
8a560 69 78 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 20  ix=='+' ){.     
8a570 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d           bufpt =
8a580 20 22 2b 49 6e 66 22 3b 0a 20 20 20 20 20 20 20   "+Inf";.       
8a590 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
8a5a0 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20            bufpt 
8a5b0 3d 20 22 49 6e 66 22 3b 0a 20 20 20 20 20 20 20  = "Inf";.       
8a5c0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
8a5d0 20 20 20 6c 65 6e 67 74 68 20 3d 20 73 74 72 6c     length = strl
8a5e0 65 6e 28 62 75 66 70 74 29 3b 0a 20 20 20 20 20  en(bufpt);.     
8a5f0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
8a600 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
8a610 20 20 7d 0a 20 20 20 20 20 20 20 20 62 75 66 70    }.        bufp
8a620 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 20  t = buf;.       
8a630 20 2f 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 49   /*.        ** I
8a640 66 20 74 68 65 20 66 69 65 6c 64 20 74 79 70 65  f the field type
8a650 20 69 73 20 65 74 47 45 4e 45 52 49 43 2c 20 74   is etGENERIC, t
8a660 68 65 6e 20 63 6f 6e 76 65 72 74 20 74 6f 20 65  hen convert to e
8a670 69 74 68 65 72 20 65 74 45 58 50 0a 20 20 20 20  ither etEXP.    
8a680 20 20 20 20 2a 2a 20 6f 72 20 65 74 46 4c 4f 41      ** or etFLOA
8a690 54 2c 20 61 73 20 61 70 70 72 6f 70 72 69 61 74  T, as appropriat
8a6a0 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  e..        */.  
8a6b0 20 20 20 20 20 20 66 6c 61 67 5f 65 78 70 20 3d        flag_exp =
8a6c0 20 78 74 79 70 65 3d 3d 65 74 45 58 50 3b 0a 20   xtype==etEXP;. 
8a6d0 20 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65         if( xtype
8a6e0 21 3d 65 74 46 4c 4f 41 54 20 29 7b 0a 20 20 20  !=etFLOAT ){.   
8a6f0 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75 65         realvalue
8a700 20 2b 3d 20 72 6f 75 6e 64 65 72 3b 0a 20 20 20   += rounder;.   
8a710 20 20 20 20 20 20 20 69 66 28 20 72 65 61 6c 76         if( realv
8a720 61 6c 75 65 3e 3d 31 30 2e 30 20 29 7b 20 72 65  alue>=10.0 ){ re
8a730 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 3b 20  alvalue *= 0.1; 
8a740 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20  exp++; }.       
8a750 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 78   }.        if( x
8a760 74 79 70 65 3d 3d 65 74 47 45 4e 45 52 49 43 20  type==etGENERIC 
8a770 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c 61  ){.          fla
8a780 67 5f 72 74 7a 20 3d 20 21 66 6c 61 67 5f 61 6c  g_rtz = !flag_al
8a790 74 65 72 6e 61 74 65 66 6f 72 6d 3b 0a 20 20 20  ternateform;.   
8a7a0 20 20 20 20 20 20 20 69 66 28 20 65 78 70 3c 2d         if( exp<-
8a7b0 34 20 7c 7c 20 65 78 70 3e 70 72 65 63 69 73 69  4 || exp>precisi
8a7c0 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  on ){.          
8a7d0 20 20 78 74 79 70 65 20 3d 20 65 74 45 58 50 3b    xtype = etEXP;
8a7e0 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
8a7f0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 72  {.            pr
8a800 65 63 69 73 69 6f 6e 20 3d 20 70 72 65 63 69 73  ecision = precis
8a810 69 6f 6e 20 2d 20 65 78 70 3b 0a 20 20 20 20 20  ion - exp;.     
8a820 20 20 20 20 20 20 20 78 74 79 70 65 20 3d 20 65         xtype = e
8a830 74 46 4c 4f 41 54 3b 0a 20 20 20 20 20 20 20 20  tFLOAT;.        
8a840 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73    }.        }els
8a850 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c 61  e{.          fla
8a860 67 5f 72 74 7a 20 3d 20 30 3b 0a 20 20 20 20 20  g_rtz = 0;.     
8a870 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
8a880 20 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 7b   xtype==etEXP ){
8a890 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 3d 20  .          e2 = 
8a8a0 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  0;.        }else
8a8b0 7b 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 3d  {.          e2 =
8a8c0 20 65 78 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a   exp;.        }.
8a8d0 20 20 20 20 20 20 20 20 6e 73 64 20 3d 20 30 3b          nsd = 0;
8a8e0 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 64 70  .        flag_dp
8a8f0 20 3d 20 28 70 72 65 63 69 73 69 6f 6e 3e 30 29   = (precision>0)
8a900 20 7c 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74   | flag_alternat
8a910 65 66 6f 72 6d 20 7c 20 66 6c 61 67 5f 61 6c 74  eform | flag_alt
8a920 66 6f 72 6d 32 3b 0a 20 20 20 20 20 20 20 20 2f  form2;.        /
8a930 2a 20 54 68 65 20 73 69 67 6e 20 69 6e 20 66 72  * The sign in fr
8a940 6f 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  ont of the numbe
8a950 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28  r */.        if(
8a960 20 70 72 65 66 69 78 20 29 7b 0a 20 20 20 20 20   prefix ){.     
8a970 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20       *(bufpt++) 
8a980 3d 20 70 72 65 66 69 78 3b 0a 20 20 20 20 20 20  = prefix;.      
8a990 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 44    }.        /* D
8a9a0 69 67 69 74 73 20 70 72 69 6f 72 20 74 6f 20 74  igits prior to t
8a9b0 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74  he decimal point
8a9c0 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
8a9d0 65 32 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20  e2<0 ){.        
8a9e0 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27    *(bufpt++) = '
8a9f0 30 27 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  0';.        }els
8aa00 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72  e{.          for
8aa10 28 3b 20 65 32 3e 3d 30 3b 20 65 32 2d 2d 29 7b  (; e2>=0; e2--){
8aa20 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 62  .            *(b
8aa30 75 66 70 74 2b 2b 29 20 3d 20 65 74 5f 67 65 74  ufpt++) = et_get
8aa40 64 69 67 69 74 28 26 72 65 61 6c 76 61 6c 75 65  digit(&realvalue
8aa50 2c 26 6e 73 64 29 3b 0a 20 20 20 20 20 20 20 20  ,&nsd);.        
8aa60 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
8aa70 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 65 63        /* The dec
8aa80 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20  imal point */.  
8aa90 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 64        if( flag_d
8aaa0 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a  p ){.          *
8aab0 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 2e 27 3b  (bufpt++) = '.';
8aac0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
8aad0 20 20 20 2f 2a 20 22 30 22 20 64 69 67 69 74 73     /* "0" digits
8aae0 20 61 66 74 65 72 20 74 68 65 20 64 65 63 69 6d   after the decim
8aaf0 61 6c 20 70 6f 69 6e 74 20 62 75 74 20 62 65 66  al point but bef
8ab00 6f 72 65 20 74 68 65 20 66 69 72 73 74 0a 20 20  ore the first.  
8ab10 20 20 20 20 20 20 2a 2a 20 73 69 67 6e 69 66 69        ** signifi
8ab20 63 61 6e 74 20 64 69 67 69 74 20 6f 66 20 74 68  cant digit of th
8ab30 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20  e number */.    
8ab40 20 20 20 20 66 6f 72 28 65 32 2b 2b 3b 20 65 32      for(e2++; e2
8ab50 3c 30 20 26 26 20 70 72 65 63 69 73 69 6f 6e 3e  <0 && precision>
8ab60 30 3b 20 70 72 65 63 69 73 69 6f 6e 2d 2d 2c 20  0; precision--, 
8ab70 65 32 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20  e2++){.         
8ab80 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 30   *(bufpt++) = '0
8ab90 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  ';.        }.   
8aba0 20 20 20 20 20 2f 2a 20 53 69 67 6e 69 66 69 63       /* Signific
8abb0 61 6e 74 20 64 69 67 69 74 73 20 61 66 74 65 72  ant digits after
8abc0 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69   the decimal poi
8abd0 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 77 68  nt */.        wh
8abe0 69 6c 65 28 20 28 70 72 65 63 69 73 69 6f 6e 2d  ile( (precision-
8abf0 2d 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  -)>0 ){.        
8ac00 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 65    *(bufpt++) = e
8ac10 74 5f 67 65 74 64 69 67 69 74 28 26 72 65 61 6c  t_getdigit(&real
8ac20 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20 20 20  value,&nsd);.   
8ac30 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f       }.        /
8ac40 2a 20 52 65 6d 6f 76 65 20 74 72 61 69 6c 69 6e  * Remove trailin
8ac50 67 20 7a 65 72 6f 73 20 61 6e 64 20 74 68 65 20  g zeros and the 
8ac60 22 2e 22 20 69 66 20 6e 6f 20 64 69 67 69 74 73  "." if no digits
8ac70 20 66 6f 6c 6c 6f 77 20 74 68 65 20 22 2e 22 20   follow the "." 
8ac80 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 66  */.        if( f
8ac90 6c 61 67 5f 72 74 7a 20 26 26 20 66 6c 61 67 5f  lag_rtz && flag_
8aca0 64 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  dp ){.          
8acb0 77 68 69 6c 65 28 20 62 75 66 70 74 5b 2d 31 5d  while( bufpt[-1]
8acc0 3d 3d 27 30 27 20 29 20 2a 28 2d 2d 62 75 66 70  =='0' ) *(--bufp
8acd0 74 29 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  t) = 0;.        
8ace0 20 20 61 73 73 65 72 74 28 20 62 75 66 70 74 3e    assert( bufpt>
8acf0 62 75 66 20 29 3b 0a 20 20 20 20 20 20 20 20 20  buf );.         
8ad00 20 69 66 28 20 62 75 66 70 74 5b 2d 31 5d 3d 3d   if( bufpt[-1]==
8ad10 27 2e 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20  '.' ){.         
8ad20 20 20 20 69 66 28 20 66 6c 61 67 5f 61 6c 74 66     if( flag_altf
8ad30 6f 72 6d 32 20 29 7b 0a 20 20 20 20 20 20 20 20  orm2 ){.        
8ad40 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29        *(bufpt++)
8ad50 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20   = '0';.        
8ad60 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
8ad70 20 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75 66           *(--buf
8ad80 70 74 29 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  pt) = 0;.       
8ad90 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
8ada0 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
8adb0 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20       /* Add the 
8adc0 22 65 4e 4e 4e 22 20 73 75 66 66 69 78 20 2a 2f  "eNNN" suffix */
8add0 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61  .        if( fla
8ade0 67 5f 65 78 70 20 7c 7c 20 28 78 74 79 70 65 3d  g_exp || (xtype=
8adf0 3d 65 74 45 58 50 20 26 26 20 65 78 70 29 20 29  =etEXP && exp) )
8ae00 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75  {.          *(bu
8ae10 66 70 74 2b 2b 29 20 3d 20 61 44 69 67 69 74 73  fpt++) = aDigits
8ae20 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d  [infop->charset]
8ae30 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
8ae40 65 78 70 3c 30 20 29 7b 0a 20 20 20 20 20 20 20  exp<0 ){.       
8ae50 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20       *(bufpt++) 
8ae60 3d 20 27 2d 27 3b 20 65 78 70 20 3d 20 2d 65 78  = '-'; exp = -ex
8ae70 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  p;.          }el
8ae80 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
8ae90 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 2b 27  *(bufpt++) = '+'
8aea0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
8aeb0 20 20 20 20 20 20 20 20 69 66 28 20 65 78 70 3e          if( exp>
8aec0 3d 31 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20  =100 ){.        
8aed0 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d      *(bufpt++) =
8aee0 20 28 65 78 70 2f 31 30 30 29 2b 27 30 27 3b 20   (exp/100)+'0'; 
8aef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
8af00 2a 20 31 30 30 27 73 20 64 69 67 69 74 20 2a 2f  * 100's digit */
8af10 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 78 70  .            exp
8af20 20 25 3d 20 31 30 30 3b 0a 20 20 20 20 20 20 20   %= 100;.       
8af30 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 2a     }.          *
8af40 28 62 75 66 70 74 2b 2b 29 20 3d 20 65 78 70 2f  (bufpt++) = exp/
8af50 31 30 2b 27 30 27 3b 20 20 20 20 20 20 20 20 20  10+'0';         
8af60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31              /* 1
8af70 30 27 73 20 64 69 67 69 74 20 2a 2f 0a 20 20 20  0's digit */.   
8af80 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b         *(bufpt++
8af90 29 20 3d 20 65 78 70 25 31 30 2b 27 30 27 3b 20  ) = exp%10+'0'; 
8afa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8afb0 20 20 20 20 2f 2a 20 31 27 73 20 64 69 67 69 74      /* 1's digit
8afc0 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   */.        }.  
8afd0 20 20 20 20 20 20 2a 62 75 66 70 74 20 3d 20 30        *bufpt = 0
8afe0 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ;..        /* Th
8aff0 65 20 63 6f 6e 76 65 72 74 65 64 20 6e 75 6d 62  e converted numb
8b000 65 72 20 69 73 20 69 6e 20 62 75 66 5b 5d 20 61  er is in buf[] a
8b010 6e 64 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74  nd zero terminat
8b020 65 64 2e 20 4f 75 74 70 75 74 20 69 74 2e 0a 20  ed. Output it.. 
8b030 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74         ** Note t
8b040 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 69  hat the number i
8b050 73 20 69 6e 20 74 68 65 20 75 73 75 61 6c 20 6f  s in the usual o
8b060 72 64 65 72 2c 20 6e 6f 74 20 72 65 76 65 72 73  rder, not revers
8b070 65 64 20 61 73 20 77 69 74 68 0a 20 20 20 20 20  ed as with.     
8b080 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 63 6f     ** integer co
8b090 6e 76 65 72 73 69 6f 6e 73 2e 20 2a 2f 0a 20 20  nversions. */.  
8b0a0 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 62        length = b
8b0b0 75 66 70 74 2d 62 75 66 3b 0a 20 20 20 20 20 20  ufpt-buf;.      
8b0c0 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 0a    bufpt = buf;..
8b0d0 20 20 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69          /* Speci
8b0e0 61 6c 20 63 61 73 65 3a 20 20 41 64 64 20 6c 65  al case:  Add le
8b0f0 61 64 69 6e 67 20 7a 65 72 6f 73 20 69 66 20 74  ading zeros if t
8b100 68 65 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20  he flag_zeropad 
8b110 66 6c 61 67 20 69 73 0a 20 20 20 20 20 20 20 20  flag is.        
8b120 2a 2a 20 73 65 74 20 61 6e 64 20 77 65 20 61 72  ** set and we ar
8b130 65 20 6e 6f 74 20 6c 65 66 74 20 6a 75 73 74 69  e not left justi
8b140 66 69 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20  fied */.        
8b150 69 66 28 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64  if( flag_zeropad
8b160 20 26 26 20 21 66 6c 61 67 5f 6c 65 66 74 6a 75   && !flag_leftju
8b170 73 74 69 66 79 20 26 26 20 6c 65 6e 67 74 68 20  stify && length 
8b180 3c 20 77 69 64 74 68 29 7b 0a 20 20 20 20 20 20  < width){.      
8b190 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20      int i;.     
8b1a0 20 20 20 20 20 69 6e 74 20 6e 50 61 64 20 3d 20       int nPad = 
8b1b0 77 69 64 74 68 20 2d 20 6c 65 6e 67 74 68 3b 0a  width - length;.
8b1c0 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d            for(i=
8b1d0 77 69 64 74 68 3b 20 69 3e 3d 6e 50 61 64 3b 20  width; i>=nPad; 
8b1e0 69 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i--){.          
8b1f0 20 20 62 75 66 70 74 5b 69 5d 20 3d 20 62 75 66    bufpt[i] = buf
8b200 70 74 5b 69 2d 6e 50 61 64 5d 3b 0a 20 20 20 20  pt[i-nPad];.    
8b210 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
8b220 20 20 69 20 3d 20 70 72 65 66 69 78 21 3d 30 3b    i = prefix!=0;
8b230 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65  .          while
8b240 28 20 6e 50 61 64 2d 2d 20 29 20 62 75 66 70 74  ( nPad-- ) bufpt
8b250 5b 69 2b 2b 5d 20 3d 20 27 30 27 3b 0a 20 20 20  [i++] = '0';.   
8b260 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20         length = 
8b270 77 69 64 74 68 3b 0a 20 20 20 20 20 20 20 20 7d  width;.        }
8b280 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20  .#endif.        
8b290 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
8b2a0 65 20 65 74 53 49 5a 45 3a 0a 20 20 20 20 20 20  e etSIZE:.      
8b2b0 20 20 2a 28 76 61 5f 61 72 67 28 61 70 2c 69 6e    *(va_arg(ap,in
8b2c0 74 2a 29 29 20 3d 20 70 41 63 63 75 6d 2d 3e 6e  t*)) = pAccum->n
8b2d0 43 68 61 72 3b 0a 20 20 20 20 20 20 20 20 6c 65  Char;.        le
8b2e0 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 30  ngth = width = 0
8b2f0 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
8b300 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 50 45  .      case etPE
8b310 52 43 45 4e 54 3a 0a 20 20 20 20 20 20 20 20 62  RCENT:.        b
8b320 75 66 5b 30 5d 20 3d 20 27 25 27 3b 0a 20 20 20  uf[0] = '%';.   
8b330 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 75 66       bufpt = buf
8b340 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68  ;.        length
8b350 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 62 72   = 1;.        br
8b360 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
8b370 65 74 43 48 41 52 4c 49 54 3a 0a 20 20 20 20 20  etCHARLIT:.     
8b380 20 63 61 73 65 20 65 74 43 48 41 52 58 3a 0a 20   case etCHARX:. 
8b390 20 20 20 20 20 20 20 63 20 3d 20 62 75 66 5b 30         c = buf[0
8b3a0 5d 20 3d 20 28 78 74 79 70 65 3d 3d 65 74 43 48  ] = (xtype==etCH
8b3b0 41 52 58 20 3f 20 76 61 5f 61 72 67 28 61 70 2c  ARX ? va_arg(ap,
8b3c0 69 6e 74 29 20 3a 20 2a 2b 2b 66 6d 74 29 3b 0a  int) : *++fmt);.
8b3d0 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65 63          if( prec
8b3e0 69 73 69 6f 6e 3e 3d 30 20 29 7b 0a 20 20 20 20  ision>=0 ){.    
8b3f0 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 31 3b        for(idx=1;
8b400 20 69 64 78 3c 70 72 65 63 69 73 69 6f 6e 3b 20   idx<precision; 
8b410 69 64 78 2b 2b 29 20 62 75 66 5b 69 64 78 5d 20  idx++) buf[idx] 
8b420 3d 20 63 3b 0a 20 20 20 20 20 20 20 20 20 20 6c  = c;.          l
8b430 65 6e 67 74 68 20 3d 20 70 72 65 63 69 73 69 6f  ength = precisio
8b440 6e 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  n;.        }else
8b450 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67  {.          leng
8b460 74 68 20 3d 31 3b 0a 20 20 20 20 20 20 20 20 7d  th =1;.        }
8b470 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d  .        bufpt =
8b480 20 62 75 66 3b 0a 20 20 20 20 20 20 20 20 62 72   buf;.        br
8b490 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
8b4a0 65 74 53 54 52 49 4e 47 3a 0a 20 20 20 20 20 20  etSTRING:.      
8b4b0 63 61 73 65 20 65 74 44 59 4e 53 54 52 49 4e 47  case etDYNSTRING
8b4c0 3a 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 20  :.        bufpt 
8b4d0 3d 20 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72  = va_arg(ap,char
8b4e0 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  *);.        if( 
8b4f0 62 75 66 70 74 3d 3d 30 20 29 7b 0a 20 20 20 20  bufpt==0 ){.    
8b500 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 22        bufpt = ""
8b510 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20  ;.        }else 
8b520 69 66 28 20 78 74 79 70 65 3d 3d 65 74 44 59 4e  if( xtype==etDYN
8b530 53 54 52 49 4e 47 20 29 7b 0a 20 20 20 20 20 20  STRING ){.      
8b540 20 20 20 20 7a 45 78 74 72 61 20 3d 20 62 75 66      zExtra = buf
8b550 70 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  pt;.        }.  
8b560 20 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73        if( precis
8b570 69 6f 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  ion>=0 ){.      
8b580 20 20 20 20 66 6f 72 28 6c 65 6e 67 74 68 3d 30      for(length=0
8b590 3b 20 6c 65 6e 67 74 68 3c 70 72 65 63 69 73 69  ; length<precisi
8b5a0 6f 6e 20 26 26 20 62 75 66 70 74 5b 6c 65 6e 67  on && bufpt[leng
8b5b0 74 68 5d 3b 20 6c 65 6e 67 74 68 2b 2b 29 7b 7d  th]; length++){}
8b5c0 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
8b5d0 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68            length
8b5e0 20 3d 20 73 74 72 6c 65 6e 28 62 75 66 70 74 29   = strlen(bufpt)
8b5f0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
8b600 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
8b610 20 63 61 73 65 20 65 74 53 51 4c 45 53 43 41 50   case etSQLESCAP
8b620 45 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 74  E:.      case et
8b630 53 51 4c 45 53 43 41 50 45 32 3a 0a 20 20 20 20  SQLESCAPE2:.    
8b640 20 20 63 61 73 65 20 65 74 53 51 4c 45 53 43 41    case etSQLESCA
8b650 50 45 33 3a 20 7b 0a 20 20 20 20 20 20 20 20 69  PE3: {.        i
8b660 6e 74 20 69 2c 20 6a 2c 20 6e 2c 20 63 68 2c 20  nt i, j, n, ch, 
8b670 69 73 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20  isnull;.        
8b680 69 6e 74 20 6e 65 65 64 51 75 6f 74 65 3b 0a 20  int needQuote;. 
8b690 20 20 20 20 20 20 20 63 68 61 72 20 71 20 3d 20         char q = 
8b6a0 28 28 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 53  ((xtype==etSQLES
8b6b0 43 41 50 45 33 29 3f 27 22 27 3a 27 5c 27 27 29  CAPE3)?'"':'\'')
8b6c0 3b 20 20 20 2f 2a 20 51 75 6f 74 65 20 63 68 61  ;   /* Quote cha
8b6d0 72 61 63 74 65 72 20 2a 2f 0a 20 20 20 20 20 20  racter */.      
8b6e0 20 20 63 68 61 72 20 2a 65 73 63 61 72 67 20 3d    char *escarg =
8b6f0 20 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a   va_arg(ap,char*
8b700 29 3b 0a 20 20 20 20 20 20 20 20 69 73 6e 75 6c  );.        isnul
8b710 6c 20 3d 20 65 73 63 61 72 67 3d 3d 30 3b 0a 20  l = escarg==0;. 
8b720 20 20 20 20 20 20 20 69 66 28 20 69 73 6e 75 6c         if( isnul
8b730 6c 20 29 20 65 73 63 61 72 67 20 3d 20 28 78 74  l ) escarg = (xt
8b740 79 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45  ype==etSQLESCAPE
8b750 32 20 3f 20 22 4e 55 4c 4c 22 20 3a 20 22 28 4e  2 ? "NULL" : "(N
8b760 55 4c 4c 29 22 29 3b 0a 20 20 20 20 20 20 20 20  ULL)");.        
8b770 66 6f 72 28 69 3d 6e 3d 30 3b 20 28 63 68 3d 65  for(i=n=0; (ch=e
8b780 73 63 61 72 67 5b 69 5d 29 21 3d 30 3b 20 69 2b  scarg[i])!=0; i+
8b790 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
8b7a0 28 20 63 68 3d 3d 71 20 29 20 20 6e 2b 2b 3b 0a  ( ch==q )  n++;.
8b7b0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
8b7c0 20 20 6e 65 65 64 51 75 6f 74 65 20 3d 20 21 69    needQuote = !i
8b7d0 73 6e 75 6c 6c 20 26 26 20 78 74 79 70 65 3d 3d  snull && xtype==
8b7e0 65 74 53 51 4c 45 53 43 41 50 45 32 3b 0a 20 20  etSQLESCAPE2;.  
8b7f0 20 20 20 20 20 20 6e 20 2b 3d 20 69 20 2b 20 31        n += i + 1
8b800 20 2b 20 6e 65 65 64 51 75 6f 74 65 2a 32 3b 0a   + needQuote*2;.
8b810 20 20 20 20 20 20 20 20 69 66 28 20 6e 3e 65 74          if( n>et
8b820 42 55 46 53 49 5a 45 20 29 7b 0a 20 20 20 20 20  BUFSIZE ){.     
8b830 20 20 20 20 20 62 75 66 70 74 20 3d 20 7a 45 78       bufpt = zEx
8b840 74 72 61 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  tra = sqlite3_ma
8b850 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 20 20 20  lloc( n );.     
8b860 20 20 20 20 20 69 66 28 20 62 75 66 70 74 3d 3d       if( bufpt==
8b870 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  0 ) return;.    
8b880 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
8b890 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 75 66       bufpt = buf
8b8a0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
8b8b0 20 20 20 20 6a 20 3d 20 30 3b 0a 20 20 20 20 20      j = 0;.     
8b8c0 20 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65     if( needQuote
8b8d0 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20   ) bufpt[j++] = 
8b8e0 71 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  q;.        for(i
8b8f0 3d 30 3b 20 28 63 68 3d 65 73 63 61 72 67 5b 69  =0; (ch=escarg[i
8b900 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20  ])!=0; i++){.   
8b910 20 20 20 20 20 20 20 62 75 66 70 74 5b 6a 2b 2b         bufpt[j++
8b920 5d 20 3d 20 63 68 3b 0a 20 20 20 20 20 20 20 20  ] = ch;.        
8b930 20 20 69 66 28 20 63 68 3d 3d 71 20 29 20 62 75    if( ch==q ) bu
8b940 66 70 74 5b 6a 2b 2b 5d 20 3d 20 63 68 3b 0a 20  fpt[j++] = ch;. 
8b950 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
8b960 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29   if( needQuote )
8b970 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b   bufpt[j++] = q;
8b980 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 5b 6a  .        bufpt[j
8b990 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 6c  ] = 0;.        l
8b9a0 65 6e 67 74 68 20 3d 20 6a 3b 0a 20 20 20 20 20  ength = j;.     
8b9b0 20 20 20 2f 2a 20 54 68 65 20 70 72 65 63 69 73     /* The precis
8b9c0 69 6f 6e 20 69 73 20 69 67 6e 6f 72 65 64 20 6f  ion is ignored o
8b9d0 6e 20 25 71 20 61 6e 64 20 25 51 20 2a 2f 0a 20  n %q and %Q */. 
8b9e0 20 20 20 20 20 20 20 2f 2a 20 69 66 28 20 70 72         /* if( pr
8b9f0 65 63 69 73 69 6f 6e 3e 3d 30 20 26 26 20 70 72  ecision>=0 && pr
8ba00 65 63 69 73 69 6f 6e 3c 6c 65 6e 67 74 68 20 29  ecision<length )
8ba10 20 6c 65 6e 67 74 68 20 3d 20 70 72 65 63 69 73   length = precis
8ba20 69 6f 6e 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20  ion; */.        
8ba30 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
8ba40 20 20 20 20 20 63 61 73 65 20 65 74 54 4f 4b 45       case etTOKE
8ba50 4e 3a 20 7b 0a 20 20 20 20 20 20 20 20 54 6f 6b  N: {.        Tok
8ba60 65 6e 20 2a 70 54 6f 6b 65 6e 20 3d 20 76 61 5f  en *pToken = va_
8ba70 61 72 67 28 61 70 2c 20 54 6f 6b 65 6e 2a 29 3b  arg(ap, Token*);
8ba80 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54 6f  .        if( pTo
8ba90 6b 65 6e 20 26 26 20 70 54 6f 6b 65 6e 2d 3e 7a  ken && pToken->z
8baa0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
8bab0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70  lite3StrAccumApp
8bac0 65 6e 64 28 70 41 63 63 75 6d 2c 20 28 63 6f 6e  end(pAccum, (con
8bad0 73 74 20 63 68 61 72 2a 29 70 54 6f 6b 65 6e 2d  st char*)pToken-
8bae0 3e 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 3b 0a  >z, pToken->n);.
8baf0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
8bb00 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 68    length = width
8bb10 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72   = 0;.        br
8bb20 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
8bb30 20 20 20 63 61 73 65 20 65 74 53 52 43 4c 49 53     case etSRCLIS
8bb40 54 3a 20 7b 0a 20 20 20 20 20 20 20 20 53 72 63  T: {.        Src
8bb50 4c 69 73 74 20 2a 70 53 72 63 20 3d 20 76 61 5f  List *pSrc = va_
8bb60 61 72 67 28 61 70 2c 20 53 72 63 4c 69 73 74 2a  arg(ap, SrcList*
8bb70 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6b  );.        int k
8bb80 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
8bb90 74 29 3b 0a 20 20 20 20 20 20 20 20 73 74 72 75  t);.        stru
8bba0 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
8bbb0 2a 70 49 74 65 6d 20 3d 20 26 70 53 72 63 2d 3e  *pItem = &pSrc->
8bbc0 61 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 61 73  a[k];.        as
8bbd0 73 65 72 74 28 20 6b 3e 3d 30 20 26 26 20 6b 3c  sert( k>=0 && k<
8bbe0 70 53 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 20 20  pSrc->nSrc );.  
8bbf0 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d        if( pItem-
8bc00 3e 7a 44 61 74 61 62 61 73 65 20 26 26 20 70 49  >zDatabase && pI
8bc10 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 5b 30  tem->zDatabase[0
8bc20 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  ] ){.          s
8bc30 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70  qlite3StrAccumAp
8bc40 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 70 49 74  pend(pAccum, pIt
8bc50 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 2c 20 2d  em->zDatabase, -
8bc60 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  1);.          sq
8bc70 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70  lite3StrAccumApp
8bc80 65 6e 64 28 70 41 63 63 75 6d 2c 20 22 2e 22 2c  end(pAccum, ".",
8bc90 20 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   1);.        }. 
8bca0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74         sqlite3St
8bcb0 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63  rAccumAppend(pAc
8bcc0 63 75 6d 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d  cum, pItem->zNam
8bcd0 65 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20  e, -1);.        
8bce0 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d  length = width =
8bcf0 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61   0;.        brea
8bd00 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
8bd10 2f 2a 20 45 6e 64 20 73 77 69 74 63 68 20 6f 76  /* End switch ov
8bd20 65 72 20 74 68 65 20 66 6f 72 6d 61 74 20 74 79  er the format ty
8bd30 70 65 20 2a 2f 0a 20 20 20 20 2f 2a 0a 20 20 20  pe */.    /*.   
8bd40 20 2a 2a 20 54 68 65 20 74 65 78 74 20 6f 66 20   ** The text of 
8bd50 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69  the conversion i
8bd60 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  s pointed to by 
8bd70 22 62 75 66 70 74 22 20 61 6e 64 20 69 73 0a 20  "bufpt" and is. 
8bd80 20 20 20 2a 2a 20 22 6c 65 6e 67 74 68 22 20 63     ** "length" c
8bd90 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20  haracters long. 
8bda0 20 54 68 65 20 66 69 65 6c 64 20 77 69 64 74 68   The field width
8bdb0 20 69 73 20 22 77 69 64 74 68 22 2e 20 20 44 6f   is "width".  Do
8bdc0 0a 20 20 20 20 2a 2a 20 74 68 65 20 6f 75 74 70  .    ** the outp
8bdd0 75 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ut..    */.    i
8bde0 66 28 20 21 66 6c 61 67 5f 6c 65 66 74 6a 75 73  f( !flag_leftjus
8bdf0 74 69 66 79 20 29 7b 0a 20 20 20 20 20 20 72 65  tify ){.      re
8be00 67 69 73 74 65 72 20 69 6e 74 20 6e 73 70 61 63  gister int nspac
8be10 65 3b 0a 20 20 20 20 20 20 6e 73 70 61 63 65 20  e;.      nspace 
8be20 3d 20 77 69 64 74 68 2d 6c 65 6e 67 74 68 3b 0a  = width-length;.
8be30 20 20 20 20 20 20 69 66 28 20 6e 73 70 61 63 65        if( nspace
8be40 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 70  >0 ){.        ap
8be50 70 65 6e 64 53 70 61 63 65 28 70 41 63 63 75 6d  pendSpace(pAccum
8be60 2c 20 6e 73 70 61 63 65 29 3b 0a 20 20 20 20 20  , nspace);.     
8be70 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
8be80 20 6c 65 6e 67 74 68 3e 30 20 29 7b 0a 20 20 20   length>0 ){.   
8be90 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
8bea0 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c  umAppend(pAccum,
8beb0 20 62 75 66 70 74 2c 20 6c 65 6e 67 74 68 29 3b   bufpt, length);
8bec0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 66  .    }.    if( f
8bed0 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20  lag_leftjustify 
8bee0 29 7b 0a 20 20 20 20 20 20 72 65 67 69 73 74 65  ){.      registe
8bef0 72 20 69 6e 74 20 6e 73 70 61 63 65 3b 0a 20 20  r int nspace;.  
8bf00 20 20 20 20 6e 73 70 61 63 65 20 3d 20 77 69 64      nspace = wid
8bf10 74 68 2d 6c 65 6e 67 74 68 3b 0a 20 20 20 20 20  th-length;.     
8bf20 20 69 66 28 20 6e 73 70 61 63 65 3e 30 20 29 7b   if( nspace>0 ){
8bf30 0a 20 20 20 20 20 20 20 20 61 70 70 65 6e 64 53  .        appendS
8bf40 70 61 63 65 28 70 41 63 63 75 6d 2c 20 6e 73 70  pace(pAccum, nsp
8bf50 61 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ace);.      }.  
8bf60 20 20 7d 0a 20 20 20 20 69 66 28 20 7a 45 78 74    }.    if( zExt
8bf70 72 61 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ra ){.      sqli
8bf80 74 65 33 5f 66 72 65 65 28 7a 45 78 74 72 61 29  te3_free(zExtra)
8bf90 3b 0a 20 20 20 20 7d 0a 20 20 7d 2f 2a 20 45 6e  ;.    }.  }/* En
8bfa0 64 20 66 6f 72 20 6c 6f 6f 70 20 6f 76 65 72 20  d for loop over 
8bfb0 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e  the format strin
8bfc0 67 20 2a 2f 0a 7d 20 2f 2a 20 45 6e 64 20 6f 66  g */.} /* End of
8bfd0 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a   function */../*
8bfe0 0a 2a 2a 20 41 70 70 65 6e 64 20 4e 20 62 79 74  .** Append N byt
8bff0 65 73 20 6f 66 20 74 65 78 74 20 66 72 6f 6d 20  es of text from 
8c000 7a 20 74 6f 20 74 68 65 20 53 74 72 41 63 63 75  z to the StrAccu
8c010 6d 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c  m object..*/.SQL
8c020 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
8c030 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
8c040 41 70 70 65 6e 64 28 53 74 72 41 63 63 75 6d 20  Append(StrAccum 
8c050 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  *p, const char *
8c060 7a 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 69 66 28  z, int N){.  if(
8c070 20 70 2d 3e 74 6f 6f 42 69 67 20 7c 20 70 2d 3e   p->tooBig | p->
8c080 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
8c090 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
8c0a0 20 20 69 66 28 20 4e 3c 30 20 29 7b 0a 20 20 20    if( N<0 ){.   
8c0b0 20 4e 20 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a   N = strlen(z);.
8c0c0 20 20 7d 0a 20 20 69 66 28 20 4e 3d 3d 30 20 29    }.  if( N==0 )
8c0d0 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
8c0e0 7d 0a 20 20 69 66 28 20 70 2d 3e 6e 43 68 61 72  }.  if( p->nChar
8c0f0 2b 4e 20 3e 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20  +N >= p->nAlloc 
8c100 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 65  ){.    char *zNe
8c110 77 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 75  w;.    if( !p->u
8c120 73 65 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20  seMalloc ){.    
8c130 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 31 3b    p->tooBig = 1;
8c140 0a 20 20 20 20 20 20 4e 20 3d 20 70 2d 3e 6e 41  .      N = p->nA
8c150 6c 6c 6f 63 20 2d 20 70 2d 3e 6e 43 68 61 72 20  lloc - p->nChar 
8c160 2d 20 31 3b 0a 20 20 20 20 20 20 69 66 28 20 4e  - 1;.      if( N
8c170 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  <=0 ){.        r
8c180 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
8c190 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
8c1a0 69 36 34 20 73 7a 4e 65 77 20 3d 20 70 2d 3e 6e  i64 szNew = p->n
8c1b0 41 6c 6c 6f 63 3b 0a 20 20 20 20 20 20 73 7a 4e  Alloc;.      szN
8c1c0 65 77 20 2b 3d 20 4e 20 2b 20 31 3b 0a 20 20 20  ew += N + 1;.   
8c1d0 20 20 20 69 66 28 20 73 7a 4e 65 77 20 3e 20 70     if( szNew > p
8c1e0 2d 3e 6d 78 41 6c 6c 6f 63 20 29 7b 0a 20 20 20  ->mxAlloc ){.   
8c1f0 20 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d       p->nAlloc =
8c200 20 70 2d 3e 6d 78 41 6c 6c 6f 63 3b 0a 20 20 20   p->mxAlloc;.   
8c210 20 20 20 20 20 69 66 28 20 28 28 69 36 34 29 70       if( ((i64)p
8c220 2d 3e 6e 43 68 61 72 29 2b 28 28 69 36 34 29 4e  ->nChar)+((i64)N
8c230 29 20 3e 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29  ) >= p->nAlloc )
8c240 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
8c250 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74  te3StrAccumReset
8c260 28 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70  (p);.          p
8c270 2d 3e 74 6f 6f 42 69 67 20 3d 20 31 3b 0a 20 20  ->tooBig = 1;.  
8c280 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a          return;.
8c290 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
8c2a0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
8c2b0 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 73 7a 4e 65 77  ->nAlloc = szNew
8c2c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
8c2d0 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zNew = sqlite3_m
8c2e0 61 6c 6c 6f 63 28 20 70 2d 3e 6e 41 6c 6c 6f 63  alloc( p->nAlloc
8c2f0 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 4e   );.      if( zN
8c300 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65  ew ){.        me
8c310 6d 63 70 79 28 7a 4e 65 77 2c 20 70 2d 3e 7a 54  mcpy(zNew, p->zT
8c320 65 78 74 2c 20 70 2d 3e 6e 43 68 61 72 29 3b 0a  ext, p->nChar);.
8c330 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
8c340 74 72 41 63 63 75 6d 52 65 73 65 74 28 70 29 3b  trAccumReset(p);
8c350 0a 20 20 20 20 20 20 20 20 70 2d 3e 7a 54 65 78  .        p->zTex
8c360 74 20 3d 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20  t = zNew;.      
8c370 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
8c380 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
8c390 20 31 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   1;.        sqli
8c3a0 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74  te3StrAccumReset
8c3b0 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  (p);.        ret
8c3c0 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
8c3d0 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28   }.  }.  memcpy(
8c3e0 26 70 2d 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68  &p->zText[p->nCh
8c3f0 61 72 5d 2c 20 7a 2c 20 4e 29 3b 0a 20 20 70 2d  ar], z, N);.  p-
8c400 3e 6e 43 68 61 72 20 2b 3d 20 4e 3b 0a 7d 0a 0a  >nChar += N;.}..
8c410 2f 2a 0a 2a 2a 20 46 69 6e 69 73 68 20 6f 66 66  /*.** Finish off
8c420 20 61 20 73 74 72 69 6e 67 20 62 79 20 6d 61 6b   a string by mak
8c430 69 6e 67 20 73 75 72 65 20 69 74 20 69 73 20 7a  ing sure it is z
8c440 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a  ero-terminated..
8c450 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
8c460 74 65 72 20 74 6f 20 74 68 65 20 72 65 73 75 6c  ter to the resul
8c470 74 69 6e 67 20 73 74 72 69 6e 67 2e 20 20 52 65  ting string.  Re
8c480 74 75 72 6e 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70  turn a NULL.** p
8c490 6f 69 6e 74 65 72 20 69 66 20 61 6e 79 20 6b 69  ointer if any ki
8c4a0 6e 64 20 6f 66 20 65 72 72 6f 72 20 77 61 73 20  nd of error was 
8c4b0 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2f 0a  encountered..*/.
8c4c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
8c4d0 68 61 72 20 2a 73 71 6c 69 74 65 33 53 74 72 41  har *sqlite3StrA
8c4e0 63 63 75 6d 46 69 6e 69 73 68 28 53 74 72 41 63  ccumFinish(StrAc
8c4f0 63 75 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70  cum *p){.  if( p
8c500 2d 3e 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 70  ->zText ){.    p
8c510 2d 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72  ->zText[p->nChar
8c520 5d 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70  ] = 0;.    if( p
8c530 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 26 26 20 70  ->useMalloc && p
8c540 2d 3e 7a 54 65 78 74 3d 3d 70 2d 3e 7a 42 61 73  ->zText==p->zBas
8c550 65 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 7a 54  e ){.      p->zT
8c560 65 78 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  ext = sqlite3_ma
8c570 6c 6c 6f 63 28 20 70 2d 3e 6e 43 68 61 72 2b 31  lloc( p->nChar+1
8c580 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d   );.      if( p-
8c590 3e 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 20 20  >zText ){.      
8c5a0 20 20 6d 65 6d 63 70 79 28 70 2d 3e 7a 54 65 78    memcpy(p->zTex
8c5b0 74 2c 20 70 2d 3e 7a 42 61 73 65 2c 20 70 2d 3e  t, p->zBase, p->
8c5c0 6e 43 68 61 72 2b 31 29 3b 0a 20 20 20 20 20 20  nChar+1);.      
8c5d0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
8c5e0 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
8c5f0 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
8c600 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  }.  }.  return p
8c610 2d 3e 7a 54 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  ->zText;.}../*.*
8c620 2a 20 52 65 73 65 74 20 61 6e 20 53 74 72 41 63  * Reset an StrAc
8c630 63 75 6d 20 73 74 72 69 6e 67 2e 20 20 52 65 63  cum string.  Rec
8c640 6c 61 69 6d 20 61 6c 6c 20 6d 61 6c 6c 6f 63 65  laim all malloce
8c650 64 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 53 51 4c  d memory..*/.SQL
8c660 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
8c670 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
8c680 52 65 73 65 74 28 53 74 72 41 63 63 75 6d 20 2a  Reset(StrAccum *
8c690 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 7a 54 65  p){.  if( p->zTe
8c6a0 78 74 21 3d 70 2d 3e 7a 42 61 73 65 20 29 7b 0a  xt!=p->zBase ){.
8c6b0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
8c6c0 28 70 2d 3e 7a 54 65 78 74 29 3b 0a 20 20 20 20  (p->zText);.    
8c6d0 70 2d 3e 7a 54 65 78 74 20 3d 20 30 3b 0a 20 20  p->zText = 0;.  
8c6e0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69  }.}../*.** Initi
8c6f0 61 6c 69 7a 65 20 61 20 73 74 72 69 6e 67 20 61  alize a string a
8c700 63 63 75 6d 75 6c 61 74 6f 72 0a 2a 2f 0a 73 74  ccumulator.*/.st
8c710 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65  atic void sqlite
8c720 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 53 74  3StrAccumInit(St
8c730 72 41 63 63 75 6d 20 2a 70 2c 20 63 68 61 72 20  rAccum *p, char 
8c740 2a 7a 42 61 73 65 2c 20 69 6e 74 20 6e 2c 20 69  *zBase, int n, i
8c750 6e 74 20 6d 78 29 7b 0a 20 20 70 2d 3e 7a 54 65  nt mx){.  p->zTe
8c760 78 74 20 3d 20 70 2d 3e 7a 42 61 73 65 20 3d 20  xt = p->zBase = 
8c770 7a 42 61 73 65 3b 0a 20 20 70 2d 3e 6e 43 68 61  zBase;.  p->nCha
8c780 72 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 41 6c 6c  r = 0;.  p->nAll
8c790 6f 63 20 3d 20 6e 3b 0a 20 20 70 2d 3e 6d 78 41  oc = n;.  p->mxA
8c7a0 6c 6c 6f 63 20 3d 20 6d 78 3b 0a 20 20 70 2d 3e  lloc = mx;.  p->
8c7b0 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 31 3b 0a 20  useMalloc = 1;. 
8c7c0 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 30 3b 0a   p->tooBig = 0;.
8c7d0 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65    p->mallocFaile
8c7e0 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  d = 0;.}../*.** 
8c7f0 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72  Print into memor
8c800 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  y obtained from 
8c810 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20  sqliteMalloc(). 
8c820 20 55 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61   Use the interna
8c830 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f  l.** %-conversio
8c840 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f  n extensions..*/
8c850 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
8c860 63 68 61 72 20 2a 73 71 6c 69 74 65 33 56 4d 50  char *sqlite3VMP
8c870 72 69 6e 74 66 28 73 71 6c 69 74 65 33 20 2a 64  rintf(sqlite3 *d
8c880 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
8c890 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73 74 20  Format, va_list 
8c8a0 61 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a  ap){.  char *z;.
8c8b0 20 20 63 68 61 72 20 7a 42 61 73 65 5b 53 51 4c    char zBase[SQL
8c8c0 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49  ITE_PRINT_BUF_SI
8c8d0 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63 75 6d 20  ZE];.  StrAccum 
8c8e0 61 63 63 3b 0a 20 20 73 71 6c 69 74 65 33 53 74  acc;.  sqlite3St
8c8f0 72 41 63 63 75 6d 49 6e 69 74 28 26 61 63 63 2c  rAccumInit(&acc,
8c900 20 7a 42 61 73 65 2c 20 73 69 7a 65 6f 66 28 7a   zBase, sizeof(z
8c910 42 61 73 65 29 2c 0a 20 20 20 20 20 20 20 20 20  Base),.         
8c920 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62 20               db 
8c930 3f 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  ? db->aLimit[SQL
8c940 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
8c950 5d 20 3a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c  ] : SQLITE_MAX_L
8c960 45 4e 47 54 48 29 3b 0a 20 20 76 78 70 72 69 6e  ENGTH);.  vxprin
8c970 74 66 28 26 61 63 63 2c 20 31 2c 20 7a 46 6f 72  tf(&acc, 1, zFor
8c980 6d 61 74 2c 20 61 70 29 3b 0a 20 20 7a 20 3d 20  mat, ap);.  z = 
8c990 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46  sqlite3StrAccumF
8c9a0 69 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 69  inish(&acc);.  i
8c9b0 66 28 20 61 63 63 2e 6d 61 6c 6c 6f 63 46 61 69  f( acc.mallocFai
8c9c0 6c 65 64 20 26 26 20 64 62 20 29 7b 0a 20 20 20  led && db ){.   
8c9d0 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
8c9e0 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74  d = 1;.  }.  ret
8c9f0 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn z;.}../*.** 
8ca00 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72  Print into memor
8ca10 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  y obtained from 
8ca20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20  sqliteMalloc(). 
8ca30 20 55 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61   Use the interna
8ca40 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f  l.** %-conversio
8ca50 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f  n extensions..*/
8ca60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
8ca70 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4d 50 72  char *sqlite3MPr
8ca80 69 6e 74 66 28 73 71 6c 69 74 65 33 20 2a 64 62  intf(sqlite3 *db
8ca90 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
8caa0 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76  ormat, ...){.  v
8cab0 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61  a_list ap;.  cha
8cac0 72 20 2a 7a 3b 0a 20 20 76 61 5f 73 74 61 72 74  r *z;.  va_start
8cad0 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20  (ap, zFormat);. 
8cae0 20 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72   z = sqlite3VMPr
8caf0 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74  intf(db, zFormat
8cb00 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28  , ap);.  va_end(
8cb10 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b  ap);.  return z;
8cb20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20  .}../*.** Print 
8cb30 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61  into memory obta
8cb40 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
8cb50 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 20 4f 6d 69  3_malloc().  Omi
8cb60 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a  t the internal.*
8cb70 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65  * %-conversion e
8cb80 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51  xtensions..*/.SQ
8cb90 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73  LITE_API char *s
8cba0 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28  qlite3_vmprintf(
8cbb0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
8cbc0 6d 61 74 2c 20 76 61 5f 6c 69 73 74 20 61 70 29  mat, va_list ap)
8cbd0 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 63  {.  char *z;.  c
8cbe0 68 61 72 20 7a 42 61 73 65 5b 53 51 4c 49 54 45  har zBase[SQLITE
8cbf0 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 5d  _PRINT_BUF_SIZE]
8cc00 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63 63  ;.  StrAccum acc
8cc10 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63  ;.  sqlite3StrAc
8cc20 63 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42  cumInit(&acc, zB
8cc30 61 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61 73  ase, sizeof(zBas
8cc40 65 29 2c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c  e), SQLITE_MAX_L
8cc50 45 4e 47 54 48 29 3b 0a 20 20 76 78 70 72 69 6e  ENGTH);.  vxprin
8cc60 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72  tf(&acc, 0, zFor
8cc70 6d 61 74 2c 20 61 70 29 3b 0a 20 20 7a 20 3d 20  mat, ap);.  z = 
8cc80 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46  sqlite3StrAccumF
8cc90 69 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 72  inish(&acc);.  r
8cca0 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn z;.}../*.*
8ccb0 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d  * Print into mem
8ccc0 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
8ccd0 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  m sqlite3_malloc
8cce0 28 29 28 29 2e 20 20 4f 6d 69 74 20 74 68 65 20  ()().  Omit the 
8ccf0 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f  internal.** %-co
8cd00 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e 73 69  nversion extensi
8cd10 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ons..*/.SQLITE_A
8cd20 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  PI char *sqlite3
8cd30 5f 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 63  _mprintf(const c
8cd40 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e  har *zFormat, ..
8cd50 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70  .){.  va_list ap
8cd60 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76  ;.  char *z;.  v
8cd70 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72  a_start(ap, zFor
8cd80 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69  mat);.  z = sqli
8cd90 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 7a 46 6f  te3_vmprintf(zFo
8cda0 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f  rmat, ap);.  va_
8cdb0 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 75 72  end(ap);.  retur
8cdc0 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 73 71  n z;.}../*.** sq
8cdd0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29  lite3_snprintf()
8cde0 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 6e 70 72   works like snpr
8cdf0 69 6e 74 66 28 29 20 65 78 63 65 70 74 20 74 68  intf() except th
8ce00 61 74 20 69 74 20 69 67 6e 6f 72 65 73 20 74 68  at it ignores th
8ce10 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c 6f 63  e.** current loc
8ce20 61 6c 65 20 73 65 74 74 69 6e 67 73 2e 20 20 54  ale settings.  T
8ce30 68 69 73 20 69 73 20 69 6d 70 6f 72 74 61 6e 74  his is important
8ce40 20 66 6f 72 20 53 51 4c 69 74 65 20 62 65 63 61   for SQLite beca
8ce50 75 73 65 20 77 65 0a 2a 2a 20 61 72 65 20 6e 6f  use we.** are no
8ce60 74 20 61 62 6c 65 20 74 6f 20 75 73 65 20 61 20  t able to use a 
8ce70 22 2c 22 20 61 73 20 74 68 65 20 64 65 63 69 6d  "," as the decim
8ce80 61 6c 20 70 6f 69 6e 74 20 69 6e 20 70 6c 61 63  al point in plac
8ce90 65 20 6f 66 20 22 2e 22 20 61 73 0a 2a 2a 20 73  e of "." as.** s
8cea0 70 65 63 69 66 69 65 64 20 62 79 20 73 6f 6d 65  pecified by some
8ceb0 20 6c 6f 63 61 6c 65 73 2e 0a 2a 2f 0a 53 51 4c   locales..*/.SQL
8cec0 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71  ITE_API char *sq
8ced0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 69  lite3_snprintf(i
8cee0 6e 74 20 6e 2c 20 63 68 61 72 20 2a 7a 42 75 66  nt n, char *zBuf
8cef0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
8cf00 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 63  ormat, ...){.  c
8cf10 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 6c 69 73  har *z;.  va_lis
8cf20 74 20 61 70 3b 0a 20 20 53 74 72 41 63 63 75 6d  t ap;.  StrAccum
8cf30 20 61 63 63 3b 0a 0a 20 20 69 66 28 20 6e 3c 3d   acc;..  if( n<=
8cf40 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
8cf50 7a 42 75 66 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  zBuf;.  }.  sqli
8cf60 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28  te3StrAccumInit(
8cf70 26 61 63 63 2c 20 7a 42 75 66 2c 20 6e 2c 20 30  &acc, zBuf, n, 0
8cf80 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61 6c 6c  );.  acc.useMall
8cf90 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61  oc = 0;.  va_sta
8cfa0 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29 3b 0a  rt(ap,zFormat);.
8cfb0 20 20 76 78 70 72 69 6e 74 66 28 26 61 63 63 2c    vxprintf(&acc,
8cfc0 20 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29   0, zFormat, ap)
8cfd0 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a  ;.  va_end(ap);.
8cfe0 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 72    z = sqlite3Str
8cff0 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63  AccumFinish(&acc
8d000 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d  );.  return z;.}
8d010 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  ..#if defined(SQ
8d020 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 64 65  LITE_TEST) || de
8d030 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
8d040 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  UG) || defined(S
8d050 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 0a  QLITE_MEMDEBUG).
8d060 2f 2a 0a 2a 2a 20 41 20 76 65 72 73 69 6f 6e 20  /*.** A version 
8d070 6f 66 20 70 72 69 6e 74 66 28 29 20 74 68 61 74  of printf() that
8d080 20 75 6e 64 65 72 73 74 61 6e 64 73 20 25 6c 6c   understands %ll
8d090 64 2e 20 20 55 73 65 64 20 66 6f 72 20 64 65 62  d.  Used for deb
8d0a0 75 67 67 69 6e 67 2e 0a 2a 2a 20 54 68 65 20 70  ugging..** The p
8d0b0 72 69 6e 74 66 28 29 20 62 75 69 6c 74 20 69 6e  rintf() built in
8d0c0 74 6f 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73  to some versions
8d0d0 20 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f 65 73   of windows does
8d0e0 20 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 20   not understand 
8d0f0 25 6c 6c 64 0a 2a 2a 20 61 6e 64 20 73 65 67 66  %lld.** and segf
8d100 61 75 6c 74 73 20 69 66 20 79 6f 75 20 67 69 76  aults if you giv
8d110 65 20 69 74 20 61 20 6c 6f 6e 67 20 6c 6f 6e 67  e it a long long
8d120 20 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   int..*/.SQLITE_
8d130 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
8d140 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
8d150 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
8d160 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f  mat, ...){.  va_
8d170 6c 69 73 74 20 61 70 3b 0a 20 20 53 74 72 41 63  list ap;.  StrAc
8d180 63 75 6d 20 61 63 63 3b 0a 20 20 63 68 61 72 20  cum acc;.  char 
8d190 7a 42 75 66 5b 35 30 30 5d 3b 0a 20 20 73 71 6c  zBuf[500];.  sql
8d1a0 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74  ite3StrAccumInit
8d1b0 28 26 61 63 63 2c 20 7a 42 75 66 2c 20 73 69 7a  (&acc, zBuf, siz
8d1c0 65 6f 66 28 7a 42 75 66 29 2c 20 30 29 3b 0a 20  eof(zBuf), 0);. 
8d1d0 20 61 63 63 2e 75 73 65 4d 61 6c 6c 6f 63 20 3d   acc.useMalloc =
8d1e0 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61   0;.  va_start(a
8d1f0 70 2c 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 76 78  p,zFormat);.  vx
8d200 70 72 69 6e 74 66 28 26 61 63 63 2c 20 30 2c 20  printf(&acc, 0, 
8d210 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20  zFormat, ap);.  
8d220 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 73 71  va_end(ap);.  sq
8d230 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e  lite3StrAccumFin
8d240 69 73 68 28 26 61 63 63 29 3b 0a 20 20 66 70 72  ish(&acc);.  fpr
8d250 69 6e 74 66 28 73 74 64 6f 75 74 2c 22 25 73 22  intf(stdout,"%s"
8d260 2c 20 7a 42 75 66 29 3b 0a 20 20 66 66 6c 75 73  , zBuf);.  fflus
8d270 68 28 73 74 64 6f 75 74 29 3b 0a 7d 0a 23 65 6e  h(stdout);.}.#en
8d280 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif../**********
8d290 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 72 69 6e  **** End of prin
8d2a0 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tf.c ***********
8d2b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d2c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d2d0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
8d2e0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
8d2f0 72 61 6e 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a  random.c *******
8d300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d320 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
8d330 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a  September 15.**.
8d340 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
8d350 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
8d360 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
8d370 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
8d380 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
8d390 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
8d3a0 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
8d3b0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
8d3c0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
8d3d0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
8d3e0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
8d3f0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
8d400 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
8d410 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
8d420 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
8d430 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
8d440 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
8d450 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
8d460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
8d4a0 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
8d4b0 73 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d  s code to implem
8d4c0 65 6e 74 20 61 20 70 73 65 75 64 6f 2d 72 61 6e  ent a pseudo-ran
8d4d0 64 6f 6d 20 6e 75 6d 62 65 72 0a 2a 2a 20 67 65  dom number.** ge
8d4e0 6e 65 72 61 74 6f 72 20 28 50 52 4e 47 29 20 66  nerator (PRNG) f
8d4f0 6f 72 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a  or SQLite..**.**
8d500 20 52 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 73 20   Random numbers 
8d510 61 72 65 20 75 73 65 64 20 62 79 20 73 6f 6d 65  are used by some
8d520 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
8d530 20 62 61 63 6b 65 6e 64 73 20 69 6e 20 6f 72 64   backends in ord
8d540 65 72 0a 2a 2a 20 74 6f 20 67 65 6e 65 72 61 74  er.** to generat
8d550 65 20 72 61 6e 64 6f 6d 20 69 6e 74 65 67 65 72  e random integer
8d560 20 6b 65 79 73 20 66 6f 72 20 74 61 62 6c 65 73   keys for tables
8d570 20 6f 72 20 72 61 6e 64 6f 6d 20 66 69 6c 65 6e   or random filen
8d580 61 6d 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  ames..**.** $Id:
8d590 20 72 61 6e 64 6f 6d 2e 63 2c 76 20 31 2e 32 33   random.c,v 1.23
8d5a0 20 32 30 30 38 2f 30 33 2f 32 31 20 31 36 3a 34   2008/03/21 16:4
8d5b0 35 3a 34 37 20 64 72 68 20 45 78 70 20 24 0a 2a  5:47 drh Exp $.*
8d5c0 2f 0a 0a 0a 2f 2a 20 41 6c 6c 20 74 68 72 65 61  /.../* All threa
8d5d0 64 73 20 73 68 61 72 65 20 61 20 73 69 6e 67 6c  ds share a singl
8d5e0 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20  e random number 
8d5f0 67 65 6e 65 72 61 74 6f 72 2e 0a 2a 2a 20 54 68  generator..** Th
8d600 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20  is structure is 
8d610 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74  the current stat
8d620 65 20 6f 66 20 74 68 65 20 67 65 6e 65 72 61 74  e of the generat
8d630 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74  or..*/.static st
8d640 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67  ruct sqlite3Prng
8d650 54 79 70 65 20 7b 0a 20 20 75 6e 73 69 67 6e 65  Type {.  unsigne
8d660 64 20 63 68 61 72 20 69 73 49 6e 69 74 3b 20 20  d char isInit;  
8d670 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
8d680 69 66 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a  if initialized *
8d690 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
8d6a0 72 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20  r i, j;         
8d6b0 20 20 20 2f 2a 20 53 74 61 74 65 20 76 61 72 69     /* State vari
8d6c0 61 62 6c 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67  ables */.  unsig
8d6d0 6e 65 64 20 63 68 61 72 20 73 5b 32 35 36 5d 3b  ned char s[256];
8d6e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61            /* Sta
8d6f0 74 65 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a  te variables */.
8d700 7d 20 73 71 6c 69 74 65 33 50 72 6e 67 3b 0a 0a  } sqlite3Prng;..
8d710 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 73 69 6e 67  /*.** Get a sing
8d720 6c 65 20 38 2d 62 69 74 20 72 61 6e 64 6f 6d 20  le 8-bit random 
8d730 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 52  value from the R
8d740 43 34 20 50 52 4e 47 2e 20 20 54 68 65 20 4d 75  C4 PRNG.  The Mu
8d750 74 65 78 0a 2a 2a 20 6d 75 73 74 20 62 65 20 68  tex.** must be h
8d760 65 6c 64 20 77 68 69 6c 65 20 65 78 65 63 75 74  eld while execut
8d770 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ing this routine
8d780 2e 0a 2a 2a 0a 2a 2a 20 57 68 79 20 6e 6f 74 20  ..**.** Why not 
8d790 6a 75 73 74 20 75 73 65 20 61 20 6c 69 62 72 61  just use a libra
8d7a0 72 79 20 72 61 6e 64 6f 6d 20 67 65 6e 65 72 61  ry random genera
8d7b0 74 6f 72 20 6c 69 6b 65 20 6c 72 61 6e 64 34 38  tor like lrand48
8d7c0 28 29 20 66 6f 72 20 74 68 69 73 3f 0a 2a 2a 20  () for this?.** 
8d7d0 42 65 63 61 75 73 65 20 74 68 65 20 4f 50 5f 4e  Because the OP_N
8d7e0 65 77 52 6f 77 69 64 20 6f 70 63 6f 64 65 20 69  ewRowid opcode i
8d7f0 6e 20 74 68 65 20 56 44 42 45 20 64 65 70 65 6e  n the VDBE depen
8d800 64 73 20 6f 6e 20 68 61 76 69 6e 67 20 61 20 76  ds on having a v
8d810 65 72 79 0a 2a 2a 20 67 6f 6f 64 20 73 6f 75 72  ery.** good sour
8d820 63 65 20 6f 66 20 72 61 6e 64 6f 6d 20 6e 75 6d  ce of random num
8d830 62 65 72 73 2e 20 20 54 68 65 20 6c 72 61 6e 64  bers.  The lrand
8d840 34 38 28 29 20 6c 69 62 72 61 72 79 20 66 75 6e  48() library fun
8d850 63 74 69 6f 6e 20 6d 61 79 0a 2a 2a 20 77 65 6c  ction may.** wel
8d860 6c 20 62 65 20 67 6f 6f 64 20 65 6e 6f 75 67 68  l be good enough
8d870 2e 20 20 42 75 74 20 6d 61 79 62 65 20 6e 6f 74  .  But maybe not
8d880 2e 20 20 4f 72 20 6d 61 79 62 65 20 6c 72 61 6e  .  Or maybe lran
8d890 64 34 38 28 29 20 68 61 73 20 73 6f 6d 65 0a 2a  d48() has some.*
8d8a0 2a 20 73 75 62 74 6c 65 20 70 72 6f 62 6c 65 6d  * subtle problem
8d8b0 73 20 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d  s on some system
8d8c0 73 20 74 68 61 74 20 63 6f 75 6c 64 20 63 61 75  s that could cau
8d8d0 73 65 20 70 72 6f 62 6c 65 6d 73 2e 20 20 49 74  se problems.  It
8d8e0 20 69 73 20 68 61 72 64 0a 2a 2a 20 74 6f 20 6b   is hard.** to k
8d8f0 6e 6f 77 2e 20 20 54 6f 20 6d 69 6e 69 6d 69 7a  now.  To minimiz
8d900 65 20 74 68 65 20 72 69 73 6b 20 6f 66 20 70 72  e the risk of pr
8d910 6f 62 6c 65 6d 73 20 64 75 65 20 74 6f 20 62 61  oblems due to ba
8d920 64 20 6c 72 61 6e 64 34 38 28 29 0a 2a 2a 20 69  d lrand48().** i
8d930 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2c 20  mplementations, 
8d940 53 51 4c 69 74 65 20 75 73 65 73 20 74 68 69 73  SQLite uses this
8d950 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67   random number g
8d960 65 6e 65 72 61 74 6f 72 20 62 61 73 65 64 0a 2a  enerator based.*
8d970 2a 20 6f 6e 20 52 43 34 2c 20 77 68 69 63 68 20  * on RC4, which 
8d980 77 65 20 6b 6e 6f 77 20 77 6f 72 6b 73 20 76 65  we know works ve
8d990 72 79 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 28  ry well..**.** (
8d9a0 4c 61 74 65 72 29 3a 20 20 41 63 74 75 61 6c 6c  Later):  Actuall
8d9b0 79 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 64  y, OP_NewRowid d
8d9c0 6f 65 73 20 6e 6f 74 20 64 65 70 65 6e 64 20 6f  oes not depend o
8d9d0 6e 20 61 20 67 6f 6f 64 20 73 6f 75 72 63 65 20  n a good source 
8d9e0 6f 66 0a 2a 2a 20 72 61 6e 64 6f 6d 6e 65 73 73  of.** randomness
8d9f0 20 61 6e 79 20 6d 6f 72 65 2e 20 20 42 75 74 20   any more.  But 
8da00 77 65 20 77 69 6c 6c 20 6c 65 61 76 65 20 74 68  we will leave th
8da10 69 73 20 63 6f 64 65 20 69 6e 20 61 6c 6c 20 74  is code in all t
8da20 68 65 20 73 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74  he same..*/.stat
8da30 69 63 20 69 6e 74 20 72 61 6e 64 6f 6d 42 79 74  ic int randomByt
8da40 65 28 76 6f 69 64 29 7b 0a 20 20 75 6e 73 69 67  e(void){.  unsig
8da50 6e 65 64 20 63 68 61 72 20 74 3b 0a 0a 0a 20 20  ned char t;...  
8da60 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  /* Initialize th
8da70 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 72  e state of the r
8da80 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e  andom number gen
8da90 65 72 61 74 6f 72 20 6f 6e 63 65 2c 0a 20 20 2a  erator once,.  *
8daa0 2a 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65  * the first time
8dab0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
8dac0 20 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 73 65   called.  The se
8dad0 65 64 20 76 61 6c 75 65 20 64 6f 65 73 0a 20 20  ed value does.  
8dae0 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 63  ** not need to c
8daf0 6f 6e 74 61 69 6e 20 61 20 6c 6f 74 20 6f 66 20  ontain a lot of 
8db00 72 61 6e 64 6f 6d 6e 65 73 73 20 73 69 6e 63 65  randomness since
8db10 20 77 65 20 61 72 65 20 6e 6f 74 0a 20 20 2a 2a   we are not.  **
8db20 20 74 72 79 69 6e 67 20 74 6f 20 64 6f 20 73 65   trying to do se
8db30 63 75 72 65 20 65 6e 63 72 79 70 74 69 6f 6e 20  cure encryption 
8db40 6f 72 20 61 6e 79 74 68 69 6e 67 20 6c 69 6b 65  or anything like
8db50 20 74 68 61 74 2e 2e 2e 0a 20 20 2a 2a 0a 20 20   that....  **.  
8db60 2a 2a 20 4e 6f 74 68 69 6e 67 20 69 6e 20 74 68  ** Nothing in th
8db70 69 73 20 66 69 6c 65 20 6f 72 20 61 6e 79 77 68  is file or anywh
8db80 65 72 65 20 65 6c 73 65 20 69 6e 20 53 51 4c 69  ere else in SQLi
8db90 74 65 20 64 6f 65 73 20 61 6e 79 20 6b 69 6e 64  te does any kind
8dba0 20 6f 66 0a 20 20 2a 2a 20 65 6e 63 72 79 70 74   of.  ** encrypt
8dbb0 69 6f 6e 2e 20 20 54 68 65 20 52 43 34 20 61 6c  ion.  The RC4 al
8dbc0 67 6f 72 69 74 68 6d 20 69 73 20 62 65 69 6e 67  gorithm is being
8dbd0 20 75 73 65 64 20 61 73 20 61 20 50 52 4e 47 20   used as a PRNG 
8dbe0 28 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 0a 20  (pseudo-random. 
8dbf0 20 2a 2a 20 6e 75 6d 62 65 72 20 67 65 6e 65 72   ** number gener
8dc00 61 74 6f 72 29 20 6e 6f 74 20 61 73 20 61 6e 20  ator) not as an 
8dc10 65 6e 63 72 79 70 74 69 6f 6e 20 64 65 76 69 63  encryption devic
8dc20 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 73  e..  */.  if( !s
8dc30 71 6c 69 74 65 33 50 72 6e 67 2e 69 73 49 6e 69  qlite3Prng.isIni
8dc40 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  t ){.    int i;.
8dc50 20 20 20 20 63 68 61 72 20 6b 5b 32 35 36 5d 3b      char k[256];
8dc60 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72 6e 67  .    sqlite3Prng
8dc70 2e 6a 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69  .j = 0;.    sqli
8dc80 74 65 33 50 72 6e 67 2e 69 20 3d 20 30 3b 0a 20  te3Prng.i = 0;. 
8dc90 20 20 20 73 71 6c 69 74 65 33 4f 73 52 61 6e 64     sqlite3OsRand
8dca0 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76  omness(sqlite3_v
8dcb0 66 73 5f 66 69 6e 64 28 30 29 2c 20 32 35 36 2c  fs_find(0), 256,
8dcc0 20 6b 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30   k);.    for(i=0
8dcd0 3b 20 69 3c 32 35 36 3b 20 69 2b 2b 29 7b 0a 20  ; i<256; i++){. 
8dce0 20 20 20 20 20 73 71 6c 69 74 65 33 50 72 6e 67       sqlite3Prng
8dcf0 2e 73 5b 69 5d 20 3d 20 69 3b 0a 20 20 20 20 7d  .s[i] = i;.    }
8dd00 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
8dd10 32 35 36 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  256; i++){.     
8dd20 20 73 71 6c 69 74 65 33 50 72 6e 67 2e 6a 20 2b   sqlite3Prng.j +
8dd30 3d 20 73 71 6c 69 74 65 33 50 72 6e 67 2e 73 5b  = sqlite3Prng.s[
8dd40 69 5d 20 2b 20 6b 5b 69 5d 3b 0a 20 20 20 20 20  i] + k[i];.     
8dd50 20 74 20 3d 20 73 71 6c 69 74 65 33 50 72 6e 67   t = sqlite3Prng
8dd60 2e 73 5b 73 71 6c 69 74 65 33 50 72 6e 67 2e 6a  .s[sqlite3Prng.j
8dd70 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ];.      sqlite3
8dd80 50 72 6e 67 2e 73 5b 73 71 6c 69 74 65 33 50 72  Prng.s[sqlite3Pr
8dd90 6e 67 2e 6a 5d 20 3d 20 73 71 6c 69 74 65 33 50  ng.j] = sqlite3P
8dda0 72 6e 67 2e 73 5b 69 5d 3b 0a 20 20 20 20 20 20  rng.s[i];.      
8ddb0 73 71 6c 69 74 65 33 50 72 6e 67 2e 73 5b 69 5d  sqlite3Prng.s[i]
8ddc0 20 3d 20 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = t;.    }.    
8ddd0 73 71 6c 69 74 65 33 50 72 6e 67 2e 69 73 49 6e  sqlite3Prng.isIn
8dde0 69 74 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f  it = 1;.  }..  /
8ddf0 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 64 20 72  * Generate and r
8de00 65 74 75 72 6e 20 73 69 6e 67 6c 65 20 72 61 6e  eturn single ran
8de10 64 6f 6d 20 62 79 74 65 0a 20 20 2a 2f 0a 20 20  dom byte.  */.  
8de20 73 71 6c 69 74 65 33 50 72 6e 67 2e 69 2b 2b 3b  sqlite3Prng.i++;
8de30 0a 20 20 74 20 3d 20 73 71 6c 69 74 65 33 50 72  .  t = sqlite3Pr
8de40 6e 67 2e 73 5b 73 71 6c 69 74 65 33 50 72 6e 67  ng.s[sqlite3Prng
8de50 2e 69 5d 3b 0a 20 20 73 71 6c 69 74 65 33 50 72  .i];.  sqlite3Pr
8de60 6e 67 2e 6a 20 2b 3d 20 74 3b 0a 20 20 73 71 6c  ng.j += t;.  sql
8de70 69 74 65 33 50 72 6e 67 2e 73 5b 73 71 6c 69 74  ite3Prng.s[sqlit
8de80 65 33 50 72 6e 67 2e 69 5d 20 3d 20 73 71 6c 69  e3Prng.i] = sqli
8de90 74 65 33 50 72 6e 67 2e 73 5b 73 71 6c 69 74 65  te3Prng.s[sqlite
8dea0 33 50 72 6e 67 2e 6a 5d 3b 0a 20 20 73 71 6c 69  3Prng.j];.  sqli
8deb0 74 65 33 50 72 6e 67 2e 73 5b 73 71 6c 69 74 65  te3Prng.s[sqlite
8dec0 33 50 72 6e 67 2e 6a 5d 20 3d 20 74 3b 0a 20 20  3Prng.j] = t;.  
8ded0 74 20 2b 3d 20 73 71 6c 69 74 65 33 50 72 6e 67  t += sqlite3Prng
8dee0 2e 73 5b 73 71 6c 69 74 65 33 50 72 6e 67 2e 69  .s[sqlite3Prng.i
8def0 5d 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ];.  return sqli
8df00 74 65 33 50 72 6e 67 2e 73 5b 74 5d 3b 0a 7d 0a  te3Prng.s[t];.}.
8df10 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 20  ./*.** Return N 
8df20 72 61 6e 64 6f 6d 20 62 79 74 65 73 2e 0a 2a 2f  random bytes..*/
8df30 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
8df40 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
8df50 65 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20  ess(int N, void 
8df60 2a 70 42 75 66 29 7b 0a 20 20 75 6e 73 69 67 6e  *pBuf){.  unsign
8df70 65 64 20 63 68 61 72 20 2a 7a 42 75 66 20 3d 20  ed char *zBuf = 
8df80 70 42 75 66 3b 0a 20 20 73 74 61 74 69 63 20 73  pBuf;.  static s
8df90 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
8dfa0 74 65 78 20 3d 20 30 3b 0a 20 20 69 66 28 20 6d  tex = 0;.  if( m
8dfb0 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 6d  utex==0 ){.    m
8dfc0 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d  utex = sqlite3_m
8dfd0 75 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54  utex_alloc(SQLIT
8dfe0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50  E_MUTEX_STATIC_P
8dff0 52 4e 47 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  RNG);.  }.  sqli
8e000 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
8e010 6d 75 74 65 78 29 3b 0a 20 20 77 68 69 6c 65 28  mutex);.  while(
8e020 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 2a 28 7a 42   N-- ){.    *(zB
8e030 75 66 2b 2b 29 20 3d 20 72 61 6e 64 6f 6d 42 79  uf++) = randomBy
8e040 74 65 28 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  te();.  }.  sqli
8e050 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
8e060 6d 75 74 65 78 29 3b 0a 7d 0a 0a 23 69 66 6e 64  mutex);.}..#ifnd
8e070 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  ef SQLITE_OMIT_B
8e080 55 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a  UILTIN_TEST./*.*
8e090 2a 20 46 6f 72 20 74 65 73 74 69 6e 67 20 70 75  * For testing pu
8e0a0 72 70 6f 73 65 73 2c 20 77 65 20 73 6f 6d 65 74  rposes, we somet
8e0b0 69 6d 65 73 20 77 61 6e 74 20 74 6f 20 70 72 65  imes want to pre
8e0c0 73 65 72 76 65 20 74 68 65 20 73 74 61 74 65 20  serve the state 
8e0d0 6f 66 0a 2a 2a 20 50 52 4e 47 20 61 6e 64 20 72  of.** PRNG and r
8e0e0 65 73 74 6f 72 65 20 74 68 65 20 50 52 4e 47 20  estore the PRNG 
8e0f0 74 6f 20 69 74 73 20 73 61 76 65 64 20 73 74 61  to its saved sta
8e100 74 65 20 61 74 20 61 20 6c 61 74 65 72 20 74 69  te at a later ti
8e110 6d 65 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  me..** The sqlit
8e120 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28  e3_test_control(
8e130 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 6c 6c  ) interface call
8e140 73 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  s these routines
8e150 20 74 6f 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 20 74   to.** control t
8e160 68 65 20 50 52 4e 47 2e 0a 2a 2f 0a 73 74 61 74  he PRNG..*/.stat
8e170 69 63 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ic struct sqlite
8e180 33 50 72 6e 67 54 79 70 65 20 73 71 6c 69 74 65  3PrngType sqlite
8e190 33 53 61 76 65 64 50 72 6e 67 3b 0a 53 51 4c 49  3SavedPrng;.SQLI
8e1a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
8e1b0 73 71 6c 69 74 65 33 50 72 6e 67 53 61 76 65 53  sqlite3PrngSaveS
8e1c0 74 61 74 65 28 76 6f 69 64 29 7b 0a 20 20 6d 65  tate(void){.  me
8e1d0 6d 63 70 79 28 26 73 71 6c 69 74 65 33 53 61 76  mcpy(&sqlite3Sav
8e1e0 65 64 50 72 6e 67 2c 20 26 73 71 6c 69 74 65 33  edPrng, &sqlite3
8e1f0 50 72 6e 67 2c 20 73 69 7a 65 6f 66 28 73 71 6c  Prng, sizeof(sql
8e200 69 74 65 33 50 72 6e 67 29 29 3b 0a 7d 0a 53 51  ite3Prng));.}.SQ
8e210 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
8e220 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73  d sqlite3PrngRes
8e230 74 6f 72 65 53 74 61 74 65 28 76 6f 69 64 29 7b  toreState(void){
8e240 0a 20 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74  .  memcpy(&sqlit
8e250 65 33 50 72 6e 67 2c 20 26 73 71 6c 69 74 65 33  e3Prng, &sqlite3
8e260 53 61 76 65 64 50 72 6e 67 2c 20 73 69 7a 65 6f  SavedPrng, sizeo
8e270 66 28 73 71 6c 69 74 65 33 50 72 6e 67 29 29 3b  f(sqlite3Prng));
8e280 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
8e290 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72  E void sqlite3Pr
8e2a0 6e 67 52 65 73 65 74 53 74 61 74 65 28 76 6f 69  ngResetState(voi
8e2b0 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 72 6e  d){.  sqlite3Prn
8e2c0 67 2e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 7d 0a  g.isInit = 0;.}.
8e2d0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
8e2e0 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45  _OMIT_BUILTIN_TE
8e2f0 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ST */../********
8e300 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 61  ****** End of ra
8e310 6e 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndom.c *********
8e320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e340 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
8e350 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
8e360 65 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  e utf.c ********
8e370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e390 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
8e3a0 34 20 41 70 72 69 6c 20 31 33 0a 2a 2a 0a 2a 2a  4 April 13.**.**
8e3b0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
8e3c0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
8e3d0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
8e3e0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
8e3f0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
8e400 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
8e410 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
8e420 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
8e430 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
8e440 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
8e450 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
8e460 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
8e470 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
8e480 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
8e490 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
8e4a0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
8e4b0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
8e4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e500 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
8e510 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
8e520 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 74 6f  routines used to
8e530 20 74 72 61 6e 73 6c 61 74 65 20 62 65 74 77 65   translate betwe
8e540 65 6e 20 55 54 46 2d 38 2c 20 0a 2a 2a 20 55 54  en UTF-8, .** UT
8e550 46 2d 31 36 2c 20 55 54 46 2d 31 36 42 45 2c 20  F-16, UTF-16BE, 
8e560 61 6e 64 20 55 54 46 2d 31 36 4c 45 2e 0a 2a 2a  and UTF-16LE..**
8e570 0a 2a 2a 20 24 49 64 3a 20 75 74 66 2e 63 2c 76  .** $Id: utf.c,v
8e580 20 31 2e 36 31 20 32 30 30 38 2f 30 33 2f 32 38   1.61 2008/03/28
8e590 20 31 35 3a 34 34 3a 31 30 20 64 61 6e 69 65 6c   15:44:10 daniel
8e5a0 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2a 0a 2a  k1977 Exp $.**.*
8e5b0 2a 20 4e 6f 74 65 73 20 6f 6e 20 55 54 46 2d 38  * Notes on UTF-8
8e5c0 3a 0a 2a 2a 0a 2a 2a 20 20 20 42 79 74 65 2d 30  :.**.**   Byte-0
8e5d0 20 20 20 20 42 79 74 65 2d 31 20 20 20 20 42 79      Byte-1    By
8e5e0 74 65 2d 32 20 20 20 20 42 79 74 65 2d 33 20 20  te-2    Byte-3  
8e5f0 20 20 56 61 6c 75 65 0a 2a 2a 20 20 30 78 78 78    Value.**  0xxx
8e600 78 78 78 78 20 20 20 20 20 20 20 20 20 20 20 20  xxxx            
8e610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e620 20 20 20 20 20 30 30 30 30 30 30 30 30 20 30 30       00000000 00
8e630 30 30 30 30 30 30 20 30 78 78 78 78 78 78 78 0a  000000 0xxxxxxx.
8e640 2a 2a 20 20 31 31 30 79 79 79 79 79 20 20 31 30  **  110yyyyy  10
8e650 78 78 78 78 78 78 20 20 20 20 20 20 20 20 20 20  xxxxxx          
8e660 20 20 20 20 20 20 20 20 20 20 20 20 20 30 30 30               000
8e670 30 30 30 30 30 20 30 30 30 30 30 79 79 79 20 79  00000 00000yyy y
8e680 79 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 31 30  yxxxxxx.**  1110
8e690 7a 7a 7a 7a 20 20 31 30 79 79 79 79 79 79 20 20  zzzz  10yyyyyy  
8e6a0 31 30 78 78 78 78 78 78 20 20 20 20 20 20 20 20  10xxxxxx        
8e6b0 20 20 20 20 20 30 30 30 30 30 30 30 30 20 7a 7a       00000000 zz
8e6c0 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 0a  zzyyyy yyxxxxxx.
8e6d0 2a 2a 20 20 31 31 31 31 30 75 75 75 20 20 31 30  **  11110uuu  10
8e6e0 75 75 7a 7a 7a 7a 20 20 31 30 79 79 79 79 79 79  uuzzzz  10yyyyyy
8e6f0 20 20 31 30 78 78 78 78 78 78 20 20 20 30 30 30    10xxxxxx   000
8e700 75 75 75 75 75 20 7a 7a 7a 7a 79 79 79 79 20 79  uuuuu zzzzyyyy y
8e710 79 78 78 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a  yxxxxxx.**.**.**
8e720 20 4e 6f 74 65 73 20 6f 6e 20 55 54 46 2d 31 36   Notes on UTF-16
8e730 3a 20 20 28 77 69 74 68 20 77 77 77 77 2b 31 3d  :  (with wwww+1=
8e740 3d 75 75 75 75 75 29 0a 2a 2a 0a 2a 2a 20 20 20  =uuuuu).**.**   
8e750 20 20 20 57 6f 72 64 2d 30 20 20 20 20 20 20 20     Word-0       
8e760 20 20 20 20 20 20 20 20 57 6f 72 64 2d 31 20 20          Word-1  
8e770 20 20 20 20 20 20 20 20 56 61 6c 75 65 0a 2a 2a          Value.**
8e780 20 20 31 31 30 31 31 30 77 77 20 77 77 7a 7a 7a    110110ww wwzzz
8e790 7a 79 79 20 20 20 31 31 30 31 31 31 79 79 20 79  zyy   110111yy y
8e7a0 79 78 78 78 78 78 78 20 20 20 20 30 30 30 75 75  yxxxxxx    000uu
8e7b0 75 75 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78  uuu zzzzyyyy yyx
8e7c0 78 78 78 78 78 0a 2a 2a 20 20 7a 7a 7a 7a 79 79  xxxxx.**  zzzzyy
8e7d0 79 79 20 79 79 78 78 78 78 78 78 20 20 20 20 20  yy yyxxxxxx     
8e7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e7f0 20 20 20 30 30 30 30 30 30 30 30 20 7a 7a 7a 7a     00000000 zzzz
8e800 79 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a  yyyy yyxxxxxx.**
8e810 0a 2a 2a 0a 2a 2a 20 42 4f 4d 20 6f 72 20 42 79  .**.** BOM or By
8e820 74 65 20 4f 72 64 65 72 20 4d 61 72 6b 3a 0a 2a  te Order Mark:.*
8e830 2a 20 20 20 20 20 30 78 66 66 20 30 78 66 65 20  *     0xff 0xfe 
8e840 20 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20    little-endian 
8e850 75 74 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a 2a  utf-16 follows.*
8e860 2a 20 20 20 20 20 30 78 66 65 20 30 78 66 66 20  *     0xfe 0xff 
8e870 20 20 62 69 67 2d 65 6e 64 69 61 6e 20 75 74 66    big-endian utf
8e880 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 0a 2a  -16 follows.**.*
8e890 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
8e8a0 2a 20 49 6e 63 6c 75 64 65 20 76 64 62 65 49 6e  * Include vdbeIn
8e8b0 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  t.h in the middl
8e8c0 65 20 6f 66 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a  e of utf.c *****
8e8d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e8e0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
8e8f0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62  * Begin file vdb
8e900 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
8e910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e930 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 65 70  /./*.** 2003 Sep
8e940 74 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 54  tember 6.**.** T
8e950 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
8e960 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
8e970 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
8e980 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
8e990 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
8e9a0 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
8e9b0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
8e9c0 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
8e9d0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
8e9e0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
8e9f0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
8ea00 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
8ea10 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
8ea20 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
8ea30 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
8ea40 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
8ea50 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
8ea60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ea70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ea80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ea90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8eaa0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
8eab0 69 73 20 74 68 65 20 68 65 61 64 65 72 20 66 69  is the header fi
8eac0 6c 65 20 66 6f 72 20 69 6e 66 6f 72 6d 61 74 69  le for informati
8ead0 6f 6e 20 74 68 61 74 20 69 73 20 70 72 69 76 61  on that is priva
8eae0 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 56 44 42  te to the.** VDB
8eaf0 45 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61  E.  This informa
8eb00 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 61 6c 6c  tion used to all
8eb10 20 62 65 20 61 74 20 74 68 65 20 74 6f 70 20 6f   be at the top o
8eb20 66 20 74 68 65 20 73 69 6e 67 6c 65 0a 2a 2a 20  f the single.** 
8eb30 73 6f 75 72 63 65 20 63 6f 64 65 20 66 69 6c 65  source code file
8eb40 20 22 76 64 62 65 2e 63 22 2e 20 20 57 68 65 6e   "vdbe.c".  When
8eb50 20 74 68 61 74 20 66 69 6c 65 20 62 65 63 61 6d   that file becam
8eb60 65 20 74 6f 6f 20 62 69 67 20 28 6f 76 65 72 0a  e too big (over.
8eb70 2a 2a 20 36 30 30 30 20 6c 69 6e 65 73 20 6c 6f  ** 6000 lines lo
8eb80 6e 67 29 20 69 74 20 77 61 73 20 73 70 6c 69 74  ng) it was split
8eb90 20 75 70 20 69 6e 74 6f 20 73 65 76 65 72 61 6c   up into several
8eba0 20 73 6d 61 6c 6c 65 72 20 66 69 6c 65 73 20 61   smaller files a
8ebb0 6e 64 0a 2a 2a 20 74 68 69 73 20 68 65 61 64 65  nd.** this heade
8ebc0 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 61  r information wa
8ebd0 73 20 66 61 63 74 6f 72 65 64 20 6f 75 74 2e 0a  s factored out..
8ebe0 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 56 44 42 45  */.#ifndef _VDBE
8ebf0 49 4e 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f  INT_H_.#define _
8ec00 56 44 42 45 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a 2a  VDBEINT_H_../*.*
8ec10 2a 20 69 6e 74 54 6f 4b 65 79 28 29 20 61 6e 64  * intToKey() and
8ec20 20 6b 65 79 54 6f 49 6e 74 28 29 20 75 73 65 64   keyToInt() used
8ec30 20 74 6f 20 74 72 61 6e 73 66 6f 72 6d 20 74 68   to transform th
8ec40 65 20 72 6f 77 69 64 2e 20 20 42 75 74 20 77 69  e rowid.  But wi
8ec50 74 68 0a 2a 2a 20 74 68 65 20 6c 61 74 65 73 74  th.** the latest
8ec60 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65   versions of the
8ec70 20 64 65 73 69 67 6e 20 74 68 65 79 20 61 72 65   design they are
8ec80 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 64 65 66   no-ops..*/.#def
8ec90 69 6e 65 20 6b 65 79 54 6f 49 6e 74 28 58 29 20  ine keyToInt(X) 
8eca0 20 20 28 58 29 0a 23 64 65 66 69 6e 65 20 69 6e    (X).#define in
8ecb0 74 54 6f 4b 65 79 28 58 29 20 20 20 28 58 29 0a  tToKey(X)   (X).
8ecc0 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 20 69 73 20 74  ../*.** SQL is t
8ecd0 72 61 6e 73 6c 61 74 65 64 20 69 6e 74 6f 20 61  ranslated into a
8ece0 20 73 65 71 75 65 6e 63 65 20 6f 66 20 69 6e 73   sequence of ins
8ecf0 74 72 75 63 74 69 6f 6e 73 20 74 6f 20 62 65 0a  tructions to be.
8ed00 2a 2a 20 65 78 65 63 75 74 65 64 20 62 79 20 61  ** executed by a
8ed10 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
8ed20 2e 20 20 45 61 63 68 20 69 6e 73 74 72 75 63 74  .  Each instruct
8ed30 69 6f 6e 20 69 73 20 61 6e 20 69 6e 73 74 61 6e  ion is an instan
8ed40 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c  ce.** of the fol
8ed50 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
8ed60 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
8ed70 75 63 74 20 56 64 62 65 4f 70 20 4f 70 3b 0a 0a  uct VdbeOp Op;..
8ed80 2f 2a 0a 2a 2a 20 42 6f 6f 6c 65 61 6e 20 76 61  /*.** Boolean va
8ed90 6c 75 65 73 0a 2a 2f 0a 74 79 70 65 64 65 66 20  lues.*/.typedef 
8eda0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 42 6f  unsigned char Bo
8edb0 6f 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 75 72  ol;../*.** A cur
8edc0 73 6f 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72  sor is a pointer
8edd0 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 42   into a single B
8ede0 54 72 65 65 20 77 69 74 68 69 6e 20 61 20 64 61  Tree within a da
8edf0 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20  tabase file..** 
8ee00 54 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 73  The cursor can s
8ee10 65 65 6b 20 74 6f 20 61 20 42 54 72 65 65 20 65  eek to a BTree e
8ee20 6e 74 72 79 20 77 69 74 68 20 61 20 70 61 72 74  ntry with a part
8ee30 69 63 75 6c 61 72 20 6b 65 79 2c 20 6f 72 0a 2a  icular key, or.*
8ee40 2a 20 6c 6f 6f 70 20 6f 76 65 72 20 61 6c 6c 20  * loop over all 
8ee50 65 6e 74 72 69 65 73 20 6f 66 20 74 68 65 20 42  entries of the B
8ee60 74 72 65 65 2e 20 20 59 6f 75 20 63 61 6e 20 61  tree.  You can a
8ee70 6c 73 6f 20 69 6e 73 65 72 74 20 6e 65 77 20 42  lso insert new B
8ee80 54 72 65 65 0a 2a 2a 20 65 6e 74 72 69 65 73 20  Tree.** entries 
8ee90 6f 72 20 72 65 74 72 69 65 76 65 20 74 68 65 20  or retrieve the 
8eea0 6b 65 79 20 6f 72 20 64 61 74 61 20 66 72 6f 6d  key or data from
8eeb0 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20   the entry that 
8eec0 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73  the cursor.** is
8eed0 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
8eee0 69 6e 67 20 74 6f 2e 0a 2a 2a 20 0a 2a 2a 20 45  ing to..** .** E
8eef0 76 65 72 79 20 63 75 72 73 6f 72 20 74 68 61 74  very cursor that
8ef00 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63   the virtual mac
8ef10 68 69 6e 65 20 68 61 73 20 6f 70 65 6e 20 69 73  hine has open is
8ef20 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20   represented by 
8ef30 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f  an.** instance o
8ef40 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
8ef50 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a  structure..**.**
8ef60 20 49 66 20 74 68 65 20 43 75 72 73 6f 72 2e 69   If the Cursor.i
8ef70 73 54 72 69 67 67 65 72 52 6f 77 20 66 6c 61 67  sTriggerRow flag
8ef80 20 69 73 20 73 65 74 20 69 74 20 6d 65 61 6e 73   is set it means
8ef90 20 74 68 61 74 20 74 68 69 73 20 63 75 72 73 6f   that this curso
8efa0 72 20 69 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 61  r is.** really a
8efb0 20 73 69 6e 67 6c 65 20 72 6f 77 20 74 68 61 74   single row that
8efc0 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 65 20   represents the 
8efd0 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 65 75 64  NEW or OLD pseud
8efe0 6f 2d 74 61 62 6c 65 20 6f 66 0a 2a 2a 20 61 20  o-table of.** a 
8eff0 72 6f 77 20 74 72 69 67 67 65 72 2e 20 20 54 68  row trigger.  Th
8f000 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 72  e data for the r
8f010 6f 77 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  ow is stored in 
8f020 43 75 72 73 6f 72 2e 70 44 61 74 61 20 61 6e 64  Cursor.pData and
8f030 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64 20 69 73  .** the rowid is
8f040 20 69 6e 20 43 75 72 73 6f 72 2e 69 4b 65 79 2e   in Cursor.iKey.
8f050 0a 2a 2f 0a 73 74 72 75 63 74 20 43 75 72 73 6f  .*/.struct Curso
8f060 72 20 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a  r {.  BtCursor *
8f070 70 43 75 72 73 6f 72 3b 20 20 20 20 2f 2a 20 54  pCursor;    /* T
8f080 68 65 20 63 75 72 73 6f 72 20 73 74 72 75 63 74  he cursor struct
8f090 75 72 65 20 6f 66 20 74 68 65 20 62 61 63 6b 65  ure of the backe
8f0a0 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b  nd */.  int iDb;
8f0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
8f0c0 20 49 6e 64 65 78 20 6f 66 20 63 75 72 73 6f 72   Index of cursor
8f0d0 20 64 61 74 61 62 61 73 65 20 69 6e 20 64 62 2d   database in db-
8f0e0 3e 61 44 62 5b 5d 20 28 6f 72 20 2d 31 29 20 2a  >aDb[] (or -1) *
8f0f0 2f 0a 20 20 69 36 34 20 6c 61 73 74 52 6f 77 69  /.  i64 lastRowi
8f100 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73  d;        /* Las
8f110 74 20 72 6f 77 69 64 20 66 72 6f 6d 20 61 20 4e  t rowid from a N
8f120 65 78 74 20 6f 72 20 4e 65 78 74 49 64 78 20 6f  ext or NextIdx o
8f130 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 36  peration */.  i6
8f140 34 20 6e 65 78 74 52 6f 77 69 64 3b 20 20 20 20  4 nextRowid;    
8f150 20 20 20 20 2f 2a 20 4e 65 78 74 20 72 6f 77 69      /* Next rowi
8f160 64 20 72 65 74 75 72 6e 65 64 20 62 79 20 4f 50  d returned by OP
8f170 5f 4e 65 77 52 6f 77 69 64 20 2a 2f 0a 20 20 42  _NewRowid */.  B
8f180 6f 6f 6c 20 7a 65 72 6f 65 64 3b 20 20 20 20 20  ool zeroed;     
8f190 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
8f1a0 7a 65 72 6f 65 64 20 6f 75 74 20 61 6e 64 20 72  zeroed out and r
8f1b0 65 61 64 79 20 66 6f 72 20 72 65 75 73 65 20 2a  eady for reuse *
8f1c0 2f 0a 20 20 42 6f 6f 6c 20 72 6f 77 69 64 49 73  /.  Bool rowidIs
8f1d0 56 61 6c 69 64 3b 20 20 20 20 2f 2a 20 54 72 75  Valid;    /* Tru
8f1e0 65 20 69 66 20 6c 61 73 74 52 6f 77 69 64 20 69  e if lastRowid i
8f1f0 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 42 6f 6f  s valid */.  Boo
8f200 6c 20 61 74 46 69 72 73 74 3b 20 20 20 20 20 20  l atFirst;      
8f210 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 6f     /* True if po
8f220 69 6e 74 69 6e 67 20 74 6f 20 66 69 72 73 74 20  inting to first 
8f230 65 6e 74 72 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20  entry */.  Bool 
8f240 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 3b 20  useRandomRowid; 
8f250 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 6e 65 77   /* Generate new
8f260 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 73 20   record numbers 
8f270 73 65 6d 69 2d 72 61 6e 64 6f 6d 6c 79 20 2a 2f  semi-randomly */
8f280 0a 20 20 42 6f 6f 6c 20 6e 75 6c 6c 52 6f 77 3b  .  Bool nullRow;
8f290 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
8f2a0 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20   if pointing to 
8f2b0 61 20 72 6f 77 20 77 69 74 68 20 6e 6f 20 64 61  a row with no da
8f2c0 74 61 20 2a 2f 0a 20 20 42 6f 6f 6c 20 6e 65 78  ta */.  Bool nex
8f2d0 74 52 6f 77 69 64 56 61 6c 69 64 3b 20 20 2f 2a  tRowidValid;  /*
8f2e0 20 54 72 75 65 20 69 66 20 74 68 65 20 6e 65 78   True if the nex
8f2f0 74 52 6f 77 69 64 20 66 69 65 6c 64 20 69 73 20  tRowid field is 
8f300 76 61 6c 69 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20  valid */.  Bool 
8f310 70 73 65 75 64 6f 54 61 62 6c 65 3b 20 20 20 20  pseudoTable;    
8f320 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20 4e 45   /* This is a NE
8f330 57 20 6f 72 20 4f 4c 44 20 70 73 65 75 64 6f 2d  W or OLD pseudo-
8f340 74 61 62 6c 65 73 20 6f 66 20 61 20 74 72 69 67  tables of a trig
8f350 67 65 72 20 2a 2f 0a 20 20 42 6f 6f 6c 20 65 70  ger */.  Bool ep
8f360 68 65 6d 50 73 65 75 64 6f 54 61 62 6c 65 3b 0a  hemPseudoTable;.
8f370 20 20 42 6f 6f 6c 20 64 65 66 65 72 72 65 64 4d    Bool deferredM
8f380 6f 76 65 74 6f 3b 20 20 2f 2a 20 41 20 63 61 6c  oveto;  /* A cal
8f390 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65  l to sqlite3Btre
8f3a0 65 4d 6f 76 65 74 6f 28 29 20 69 73 20 6e 65 65  eMoveto() is nee
8f3b0 64 65 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73  ded */.  Bool is
8f3c0 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 2f  Table;         /
8f3d0 2a 20 54 72 75 65 20 69 66 20 61 20 74 61 62 6c  * True if a tabl
8f3e0 65 20 72 65 71 75 69 72 69 6e 67 20 69 6e 74 65  e requiring inte
8f3f0 67 65 72 20 6b 65 79 73 20 2a 2f 0a 20 20 42 6f  ger keys */.  Bo
8f400 6f 6c 20 69 73 49 6e 64 65 78 3b 20 20 20 20 20  ol isIndex;     
8f410 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
8f420 6e 20 69 6e 64 65 78 20 63 6f 6e 74 61 69 6e 69  n index containi
8f430 6e 67 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e  ng keys only - n
8f440 6f 20 64 61 74 61 20 2a 2f 0a 20 20 75 38 20 62  o data */.  u8 b
8f450 6f 67 75 73 49 6e 63 72 4b 65 79 3b 20 20 20 20  ogusIncrKey;    
8f460 20 20 2f 2a 20 53 6f 6d 65 74 68 69 6e 67 20 66    /* Something f
8f470 6f 72 20 70 49 6e 63 72 4b 65 79 20 74 6f 20 70  or pIncrKey to p
8f480 6f 69 6e 74 20 74 6f 20 69 66 20 70 4b 65 79 49  oint to if pKeyI
8f490 6e 66 6f 3d 3d 30 20 2a 2f 0a 20 20 69 36 34 20  nfo==0 */.  i64 
8f4a0 6d 6f 76 65 74 6f 54 61 72 67 65 74 3b 20 20 20  movetoTarget;   
8f4b0 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f    /* Argument to
8f4c0 20 74 68 65 20 64 65 66 65 72 72 65 64 20 73 71   the deferred sq
8f4d0 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f  lite3BtreeMoveto
8f4e0 28 29 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70  () */.  Btree *p
8f4f0 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  Bt;           /*
8f500 20 53 65 70 61 72 61 74 65 20 66 69 6c 65 20 68   Separate file h
8f510 6f 6c 64 69 6e 67 20 74 65 6d 70 6f 72 61 72 79  olding temporary
8f520 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
8f530 6e 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20  nData;          
8f540 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
8f550 79 74 65 73 20 69 6e 20 70 44 61 74 61 20 2a 2f  ytes in pData */
8f560 0a 20 20 63 68 61 72 20 2a 70 44 61 74 61 3b 20  .  char *pData; 
8f570 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
8f580 20 66 6f 72 20 61 20 4e 45 57 20 6f 72 20 4f 4c   for a NEW or OL
8f590 44 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 2a  D pseudo-table *
8f5a0 2f 0a 20 20 69 36 34 20 69 4b 65 79 3b 20 20 20  /.  i64 iKey;   
8f5b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65 79            /* Key
8f5c0 20 66 6f 72 20 74 68 65 20 4e 45 57 20 6f 72 20   for the NEW or 
8f5d0 4f 4c 44 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  OLD pseudo-table
8f5e0 20 72 6f 77 20 2a 2f 0a 20 20 75 38 20 2a 70 49   row */.  u8 *pI
8f5f0 6e 63 72 4b 65 79 3b 20 20 20 20 20 20 20 20 20  ncrKey;         
8f600 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 4b  /* Pointer to pK
8f610 65 79 49 6e 66 6f 2d 3e 69 6e 63 72 4b 65 79 20  eyInfo->incrKey 
8f620 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  */.  KeyInfo *pK
8f630 65 79 49 6e 66 6f 3b 20 20 20 20 2f 2a 20 49 6e  eyInfo;    /* In
8f640 66 6f 20 61 62 6f 75 74 20 69 6e 64 65 78 20 6b  fo about index k
8f650 65 79 73 20 6e 65 65 64 65 64 20 62 79 20 69 6e  eys needed by in
8f660 64 65 78 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20  dex cursors */. 
8f670 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20   int nField;    
8f680 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
8f690 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68   of fields in th
8f6a0 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 36  e header */.  i6
8f6b0 34 20 73 65 71 43 6f 75 6e 74 3b 20 20 20 20 20  4 seqCount;     
8f6c0 20 20 20 20 2f 2a 20 53 65 71 75 65 6e 63 65 20      /* Sequence 
8f6d0 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 73 71 6c  counter */.  sql
8f6e0 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
8f6f0 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 20 20   *pVtabCursor;  
8f700 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 66 6f  /* The cursor fo
8f710 72 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  r a virtual tabl
8f720 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c  e */.  const sql
8f730 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
8f740 64 75 6c 65 3b 20 20 20 20 20 2f 2a 20 4d 6f 64  dule;     /* Mod
8f750 75 6c 65 20 66 6f 72 20 63 75 72 73 6f 72 20 70  ule for cursor p
8f760 56 74 61 62 43 75 72 73 6f 72 20 2a 2f 0a 0a 20  VtabCursor */.. 
8f770 20 2f 2a 20 43 61 63 68 65 64 20 69 6e 66 6f 72   /* Cached infor
8f780 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mation about the
8f790 20 68 65 61 64 65 72 20 66 6f 72 20 74 68 65 20   header for the 
8f7a0 64 61 74 61 20 72 65 63 6f 72 64 20 74 68 61 74  data record that
8f7b0 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 73 6f 72   the.  ** cursor
8f7c0 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f   is currently po
8f7d0 69 6e 74 69 6e 67 20 74 6f 2e 20 20 4f 6e 6c 79  inting to.  Only
8f7e0 20 76 61 6c 69 64 20 69 66 20 63 61 63 68 65 56   valid if cacheV
8f7f0 61 6c 69 64 20 69 73 20 74 72 75 65 2e 0a 20 20  alid is true..  
8f800 2a 2a 20 61 52 6f 77 20 6d 69 67 68 74 20 70 6f  ** aRow might po
8f810 69 6e 74 20 74 6f 20 28 65 70 68 65 6d 65 72 61  int to (ephemera
8f820 6c 29 20 64 61 74 61 20 66 6f 72 20 74 68 65 20  l) data for the 
8f830 63 75 72 72 65 6e 74 20 72 6f 77 2c 20 6f 72 20  current row, or 
8f840 69 74 20 6d 69 67 68 74 0a 20 20 2a 2a 20 62 65  it might.  ** be
8f850 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 69 6e   NULL..  */.  in
8f860 74 20 63 61 63 68 65 53 74 61 74 75 73 3b 20 20  t cacheStatus;  
8f870 20 20 20 20 2f 2a 20 43 61 63 68 65 20 69 73 20      /* Cache is 
8f880 76 61 6c 69 64 20 69 66 20 74 68 69 73 20 6d 61  valid if this ma
8f890 74 63 68 65 73 20 56 64 62 65 2e 63 61 63 68 65  tches Vdbe.cache
8f8a0 43 74 72 20 2a 2f 0a 20 20 69 6e 74 20 70 61 79  Ctr */.  int pay
8f8b0 6c 6f 61 64 53 69 7a 65 3b 20 20 20 20 20 20 2f  loadSize;      /
8f8c0 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  * Total number o
8f8d0 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 72  f bytes in the r
8f8e0 65 63 6f 72 64 20 2a 2f 0a 20 20 75 33 32 20 2a  ecord */.  u32 *
8f8f0 61 54 79 70 65 3b 20 20 20 20 20 20 20 20 20 20  aType;          
8f900 20 2f 2a 20 54 79 70 65 20 76 61 6c 75 65 73 20   /* Type values 
8f910 66 6f 72 20 61 6c 6c 20 65 6e 74 72 69 65 73 20  for all entries 
8f920 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f  in the record */
8f930 0a 20 20 75 33 32 20 2a 61 4f 66 66 73 65 74 3b  .  u32 *aOffset;
8f940 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68           /* Cach
8f950 65 64 20 6f 66 66 73 65 74 73 20 74 6f 20 74 68  ed offsets to th
8f960 65 20 73 74 61 72 74 20 6f 66 20 65 61 63 68 20  e start of each 
8f970 63 6f 6c 75 6d 6e 73 20 64 61 74 61 20 2a 2f 0a  columns data */.
8f980 20 20 75 38 20 2a 61 52 6f 77 3b 20 20 20 20 20    u8 *aRow;     
8f990 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20          /* Data 
8f9a0 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
8f9b0 72 6f 77 2c 20 69 66 20 61 6c 6c 20 6f 6e 20 6f  row, if all on o
8f9c0 6e 65 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 74 79  ne page */.};.ty
8f9d0 70 65 64 65 66 20 73 74 72 75 63 74 20 43 75 72  pedef struct Cur
8f9e0 73 6f 72 20 43 75 72 73 6f 72 3b 0a 0a 2f 2a 0a  sor Cursor;../*.
8f9f0 2a 2a 20 41 20 76 61 6c 75 65 20 66 6f 72 20 43  ** A value for C
8fa00 75 72 73 6f 72 2e 63 61 63 68 65 56 61 6c 69 64  ursor.cacheValid
8fa10 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 65 20   that means the 
8fa20 63 61 63 68 65 20 69 73 20 61 6c 77 61 79 73 20  cache is always 
8fa30 69 6e 76 61 6c 69 64 2e 0a 2a 2f 0a 23 64 65 66  invalid..*/.#def
8fa40 69 6e 65 20 43 41 43 48 45 5f 53 54 41 4c 45 20  ine CACHE_STALE 
8fa50 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61  0../*.** Interna
8fa60 6c 6c 79 2c 20 74 68 65 20 76 64 62 65 20 6d 61  lly, the vdbe ma
8fa70 6e 69 70 75 6c 61 74 65 73 20 6e 65 61 72 6c 79  nipulates nearly
8fa80 20 61 6c 6c 20 53 51 4c 20 76 61 6c 75 65 73 20   all SQL values 
8fa90 61 73 20 4d 65 6d 0a 2a 2a 20 73 74 72 75 63 74  as Mem.** struct
8faa0 75 72 65 73 2e 20 45 61 63 68 20 4d 65 6d 20 73  ures. Each Mem s
8fab0 74 72 75 63 74 20 6d 61 79 20 63 61 63 68 65 20  truct may cache 
8fac0 6d 75 6c 74 69 70 6c 65 20 72 65 70 72 65 73 65  multiple represe
8fad0 6e 74 61 74 69 6f 6e 73 20 28 73 74 72 69 6e 67  ntations (string
8fae0 2c 0a 2a 2a 20 69 6e 74 65 67 65 72 20 65 74 63  ,.** integer etc
8faf0 2e 29 20 6f 66 20 74 68 65 20 73 61 6d 65 20 76  .) of the same v
8fb00 61 6c 75 65 2e 20 20 41 20 76 61 6c 75 65 20 28  alue.  A value (
8fb10 61 6e 64 20 74 68 65 72 65 66 6f 72 65 20 4d 65  and therefore Me
8fb20 6d 20 73 74 72 75 63 74 75 72 65 29 0a 2a 2a 20  m structure).** 
8fb30 68 61 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  has the followin
8fb40 67 20 70 72 6f 70 65 72 74 69 65 73 3a 0a 2a 2a  g properties:.**
8fb50 0a 2a 2a 20 45 61 63 68 20 76 61 6c 75 65 20 68  .** Each value h
8fb60 61 73 20 61 20 6d 61 6e 69 66 65 73 74 20 74 79  as a manifest ty
8fb70 70 65 2e 20 54 68 65 20 6d 61 6e 69 66 65 73 74  pe. The manifest
8fb80 20 74 79 70 65 20 6f 66 20 74 68 65 20 76 61 6c   type of the val
8fb90 75 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20  ue stored.** in 
8fba0 61 20 4d 65 6d 20 73 74 72 75 63 74 20 69 73 20  a Mem struct is 
8fbb0 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20  returned by the 
8fbc0 4d 65 6d 54 79 70 65 28 4d 65 6d 2a 29 20 6d 61  MemType(Mem*) ma
8fbd0 63 72 6f 2e 20 54 68 65 20 74 79 70 65 20 69 73  cro. The type is
8fbe0 0a 2a 2a 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54  .** one of SQLIT
8fbf0 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 5f 49  E_NULL, SQLITE_I
8fc00 4e 54 45 47 45 52 2c 20 53 51 4c 49 54 45 5f 52  NTEGER, SQLITE_R
8fc10 45 41 4c 2c 20 53 51 4c 49 54 45 5f 54 45 58 54  EAL, SQLITE_TEXT
8fc20 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 4c   or.** SQLITE_BL
8fc30 4f 42 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65  OB..*/.struct Me
8fc40 6d 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20  m {.  union {.  
8fc50 20 20 69 36 34 20 69 3b 20 20 20 20 20 20 20 20    i64 i;        
8fc60 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72        /* Integer
8fc70 20 76 61 6c 75 65 2e 20 4f 72 20 46 75 6e 63 44   value. Or FuncD
8fc80 65 66 2a 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d  ef* when flags==
8fc90 4d 45 4d 5f 41 67 67 20 2a 2f 0a 20 20 20 20 46  MEM_Agg */.    F
8fca0 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 20 20 20  uncDef *pDef;   
8fcb0 20 20 20 2f 2a 20 55 73 65 64 20 6f 6e 6c 79 20     /* Used only 
8fcc0 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f  when flags==MEM_
8fcd0 41 67 67 20 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20  Agg */.  } u;.  
8fce0 64 6f 75 62 6c 65 20 72 3b 20 20 20 20 20 20 20  double r;       
8fcf0 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c 75      /* Real valu
8fd00 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  e */.  sqlite3 *
8fd10 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68  db;        /* Th
8fd20 65 20 61 73 73 6f 63 69 61 74 65 64 20 64 61 74  e associated dat
8fd30 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
8fd40 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20 20   */.  char *z;  
8fd50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72            /* Str
8fd60 69 6e 67 20 6f 72 20 42 4c 4f 42 20 76 61 6c 75  ing or BLOB valu
8fd70 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20  e */.  int n;   
8fd80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
8fd90 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65  mber of characte
8fda0 72 73 20 69 6e 20 73 74 72 69 6e 67 20 76 61 6c  rs in string val
8fdb0 75 65 2c 20 65 78 63 6c 75 64 69 6e 67 20 27 5c  ue, excluding '\
8fdc0 30 27 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67  0' */.  u16 flag
8fdd0 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  s;          /* S
8fde0 6f 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20  ome combination 
8fdf0 6f 66 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 4d 45 4d  of MEM_Null, MEM
8fe00 5f 53 74 72 2c 20 4d 45 4d 5f 44 79 6e 2c 20 65  _Str, MEM_Dyn, e
8fe10 74 63 2e 20 2a 2f 0a 20 20 75 38 20 20 74 79 70  tc. */.  u8  typ
8fe20 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e;           /* 
8fe30 4f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 4e 55  One of SQLITE_NU
8fe40 4c 4c 2c 20 53 51 4c 49 54 45 5f 54 45 58 54 2c  LL, SQLITE_TEXT,
8fe50 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 2c   SQLITE_INTEGER,
8fe60 20 65 74 63 20 2a 2f 0a 20 20 75 38 20 20 65 6e   etc */.  u8  en
8fe70 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  c;            /*
8fe80 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51   SQLITE_UTF8, SQ
8fe90 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53 51  LITE_UTF16BE, SQ
8fea0 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 2a 2f 0a  LITE_UTF16LE */.
8feb0 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76    void (*xDel)(v
8fec0 6f 69 64 20 2a 29 3b 20 20 2f 2a 20 49 66 20 6e  oid *);  /* If n
8fed0 6f 74 20 6e 75 6c 6c 2c 20 63 61 6c 6c 20 74 68  ot null, call th
8fee0 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 64  is function to d
8fef0 65 6c 65 74 65 20 4d 65 6d 2e 7a 20 2a 2f 0a 20  elete Mem.z */. 
8ff00 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20   char *zMalloc; 
8ff10 20 20 20 20 20 2f 2a 20 44 79 6e 61 6d 69 63 20       /* Dynamic 
8ff20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64  buffer allocated
8ff30 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c   by sqlite3_mall
8ff40 6f 63 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 4f  oc() */.};../* O
8ff50 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68  ne or more of th
8ff60 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67  e following flag
8ff70 73 20 61 72 65 20 73 65 74 20 74 6f 20 69 6e 64  s are set to ind
8ff80 69 63 61 74 65 20 74 68 65 20 76 61 6c 69 64 4f  icate the validO
8ff90 4b 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74  K.** representat
8ffa0 69 6f 6e 73 20 6f 66 20 74 68 65 20 76 61 6c 75  ions of the valu
8ffb0 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  e stored in the 
8ffc0 4d 65 6d 20 73 74 72 75 63 74 2e 0a 2a 2a 0a 2a  Mem struct..**.*
8ffd0 2a 20 49 66 20 74 68 65 20 4d 45 4d 5f 4e 75 6c  * If the MEM_Nul
8ffe0 6c 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74  l flag is set, t
8fff0 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73  hen the value is
90000 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 76 61 6c   an SQL NULL val
90010 75 65 2e 0a 2a 2a 20 4e 6f 20 6f 74 68 65 72 20  ue..** No other 
90020 66 6c 61 67 73 20 6d 61 79 20 62 65 20 73 65 74  flags may be set
90030 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a   in this case..*
90040 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45 4d 5f  *.** If the MEM_
90050 53 74 72 20 66 6c 61 67 20 69 73 20 73 65 74 20  Str flag is set 
90060 74 68 65 6e 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74  then Mem.z point
90070 73 20 61 74 20 61 20 73 74 72 69 6e 67 20 72 65  s at a string re
90080 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a  presentation..**
90090 20 55 73 75 61 6c 6c 79 20 74 68 69 73 20 69 73   Usually this is
900a0 20 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 20   encoded in the 
900b0 73 61 6d 65 20 75 6e 69 63 6f 64 65 20 65 6e 63  same unicode enc
900c0 6f 64 69 6e 67 20 61 73 20 74 68 65 20 6d 61 69  oding as the mai
900d0 6e 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 28 73  n.** database (s
900e0 65 65 20 62 65 6c 6f 77 20 66 6f 72 20 65 78 63  ee below for exc
900f0 65 70 74 69 6f 6e 73 29 2e 20 49 66 20 74 68 65  eptions). If the
90100 20 4d 45 4d 5f 54 65 72 6d 20 66 6c 61 67 20 69   MEM_Term flag i
90110 73 20 61 6c 73 6f 0a 2a 2a 20 73 65 74 2c 20 74  s also.** set, t
90120 68 65 6e 20 74 68 65 20 73 74 72 69 6e 67 20 69  hen the string i
90130 73 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64  s nul terminated
90140 2e 20 54 68 65 20 4d 45 4d 5f 49 6e 74 20 61 6e  . The MEM_Int an
90150 64 20 4d 45 4d 5f 52 65 61 6c 20 0a 2a 2a 20 66  d MEM_Real .** f
90160 6c 61 67 73 20 6d 61 79 20 63 6f 65 78 69 73 74  lags may coexist
90170 20 77 69 74 68 20 74 68 65 20 4d 45 4d 5f 53 74   with the MEM_St
90180 72 20 66 6c 61 67 2e 0a 2a 2a 0a 2a 2a 20 4d 75  r flag..**.** Mu
90190 6c 74 69 70 6c 65 20 6f 66 20 74 68 65 73 65 20  ltiple of these 
901a0 76 61 6c 75 65 73 20 63 61 6e 20 61 70 70 65 61  values can appea
901b0 72 20 69 6e 20 4d 65 6d 2e 66 6c 61 67 73 2e 20  r in Mem.flags. 
901c0 20 42 75 74 20 6f 6e 6c 79 20 6f 6e 65 0a 2a 2a   But only one.**
901d0 20 61 74 20 61 20 74 69 6d 65 20 63 61 6e 20 61   at a time can a
901e0 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e 74 79 70  ppear in Mem.typ
901f0 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45  e..*/.#define ME
90200 4d 5f 4e 75 6c 6c 20 20 20 20 20 20 30 78 30 30  M_Null      0x00
90210 30 31 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73  01   /* Value is
90220 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65   NULL */.#define
90230 20 4d 45 4d 5f 53 74 72 20 20 20 20 20 20 20 30   MEM_Str       0
90240 78 30 30 30 32 20 20 20 2f 2a 20 56 61 6c 75 65  x0002   /* Value
90250 20 69 73 20 61 20 73 74 72 69 6e 67 20 2a 2f 0a   is a string */.
90260 23 64 65 66 69 6e 65 20 4d 45 4d 5f 49 6e 74 20  #define MEM_Int 
90270 20 20 20 20 20 20 30 78 30 30 30 34 20 20 20 2f        0x0004   /
90280 2a 20 56 61 6c 75 65 20 69 73 20 61 6e 20 69 6e  * Value is an in
90290 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65  teger */.#define
902a0 20 4d 45 4d 5f 52 65 61 6c 20 20 20 20 20 20 30   MEM_Real      0
902b0 78 30 30 30 38 20 20 20 2f 2a 20 56 61 6c 75 65  x0008   /* Value
902c0 20 69 73 20 61 20 72 65 61 6c 20 6e 75 6d 62 65   is a real numbe
902d0 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d  r */.#define MEM
902e0 5f 42 6c 6f 62 20 20 20 20 20 20 30 78 30 30 31  _Blob      0x001
902f0 30 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20  0   /* Value is 
90300 61 20 42 4c 4f 42 20 2a 2f 0a 0a 23 64 65 66 69  a BLOB */..#defi
90310 6e 65 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61  ne MemSetTypeFla
90320 67 28 70 2c 20 66 29 20 5c 0a 20 20 28 28 70 29  g(p, f) \.  ((p)
90330 2d 3e 66 6c 61 67 73 20 3d 20 28 28 70 29 2d 3e  ->flags = ((p)->
90340 66 6c 61 67 73 26 7e 28 4d 45 4d 5f 49 6e 74 7c  flags&~(MEM_Int|
90350 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 4e 75 6c  MEM_Real|MEM_Nul
90360 6c 7c 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53  l|MEM_Blob|MEM_S
90370 74 72 29 29 7c 66 29 0a 0a 2f 2a 20 57 68 65 6e  tr))|f)../* When
90380 65 76 65 72 20 4d 65 6d 20 63 6f 6e 74 61 69 6e  ever Mem contain
90390 73 20 61 20 76 61 6c 69 64 20 73 74 72 69 6e 67  s a valid string
903a0 20 6f 72 20 62 6c 6f 62 20 72 65 70 72 65 73 65   or blob represe
903b0 6e 74 61 74 69 6f 6e 2c 20 6f 6e 65 20 6f 66 0a  ntation, one of.
903c0 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** the following
903d0 20 66 6c 61 67 73 20 6d 75 73 74 20 62 65 20 73   flags must be s
903e0 65 74 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  et to determine 
903f0 74 68 65 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67  the memory manag
90400 65 6d 65 6e 74 0a 2a 2a 20 70 6f 6c 69 63 79 20  ement.** policy 
90410 66 6f 72 20 4d 65 6d 2e 7a 2e 20 20 54 68 65 20  for Mem.z.  The 
90420 4d 45 4d 5f 54 65 72 6d 20 66 6c 61 67 20 74 65  MEM_Term flag te
90430 6c 6c 73 20 75 73 20 77 68 65 74 68 65 72 20 6f  lls us whether o
90440 72 20 6e 6f 74 20 74 68 65 0a 2a 2a 20 73 74 72  r not the.** str
90450 69 6e 67 20 69 73 20 5c 30 30 30 20 6f 72 20 5c  ing is \000 or \
90460 75 30 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64  u0000 terminated
90470 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f  .*/.#define MEM_
90480 54 65 72 6d 20 20 20 20 20 20 30 78 30 30 32 30  Term      0x0020
90490 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 72 65 70     /* String rep
904a0 20 69 73 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74   is nul terminat
904b0 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45  ed */.#define ME
904c0 4d 5f 44 79 6e 20 20 20 20 20 20 20 30 78 30 30  M_Dyn       0x00
904d0 34 30 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20  40   /* Need to 
904e0 63 61 6c 6c 20 73 71 6c 69 74 65 46 72 65 65 28  call sqliteFree(
904f0 29 20 6f 6e 20 4d 65 6d 2e 7a 20 2a 2f 0a 23 64  ) on Mem.z */.#d
90500 65 66 69 6e 65 20 4d 45 4d 5f 53 74 61 74 69 63  efine MEM_Static
90510 20 20 20 20 30 78 30 30 38 30 20 20 20 2f 2a 20      0x0080   /* 
90520 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 6f 20  Mem.z points to 
90530 61 20 73 74 61 74 69 63 20 73 74 72 69 6e 67 20  a static string 
90540 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 45  */.#define MEM_E
90550 70 68 65 6d 20 20 20 20 20 30 78 30 31 30 30 20  phem     0x0100 
90560 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74    /* Mem.z point
90570 73 20 74 6f 20 61 6e 20 65 70 68 65 6d 65 72 61  s to an ephemera
90580 6c 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66  l string */.#def
90590 69 6e 65 20 4d 45 4d 5f 41 67 67 20 20 20 20 20  ine MEM_Agg     
905a0 20 20 30 78 30 34 30 30 20 20 20 2f 2a 20 4d 65    0x0400   /* Me
905b0 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e  m.z points to an
905c0 20 61 67 67 20 66 75 6e 63 74 69 6f 6e 20 63 6f   agg function co
905d0 6e 74 65 78 74 20 2a 2f 0a 23 64 65 66 69 6e 65  ntext */.#define
905e0 20 4d 45 4d 5f 5a 65 72 6f 20 20 20 20 20 20 30   MEM_Zero      0
905f0 78 30 38 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 69  x0800   /* Mem.i
90600 20 63 6f 6e 74 61 69 6e 73 20 63 6f 75 6e 74 20   contains count 
90610 6f 66 20 30 73 20 61 70 70 65 6e 64 65 64 20 74  of 0s appended t
90620 6f 20 62 6c 6f 62 20 2a 2f 0a 0a 23 69 66 64 65  o blob */..#ifde
90630 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
90640 43 52 42 4c 4f 42 0a 20 20 23 75 6e 64 65 66 20  CRBLOB.  #undef 
90650 4d 45 4d 5f 5a 65 72 6f 0a 20 20 23 64 65 66 69  MEM_Zero.  #defi
90660 6e 65 20 4d 45 4d 5f 5a 65 72 6f 20 30 78 30 30  ne MEM_Zero 0x00
90670 30 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 20 41  00.#endif.../* A
90680 20 56 64 62 65 46 75 6e 63 20 69 73 20 6a 75 73   VdbeFunc is jus
90690 74 20 61 20 46 75 6e 63 44 65 66 20 28 64 65 66  t a FuncDef (def
906a0 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 49 6e  ined in sqliteIn
906b0 74 2e 68 29 20 74 68 61 74 20 63 6f 6e 74 61 69  t.h) that contai
906c0 6e 73 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c  ns.** additional
906d0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
906e0 75 74 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66  ut auxiliary inf
906f0 6f 72 6d 61 74 69 6f 6e 20 62 6f 75 6e 64 20 74  ormation bound t
90700 6f 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 6f  o arguments.** o
90710 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20  f the function. 
90720 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f   This is used to
90730 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 73   implement the s
90740 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61  qlite3_get_auxda
90750 74 61 28 29 0a 2a 2a 20 61 6e 64 20 73 71 6c 69  ta().** and sqli
90760 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28  te3_set_auxdata(
90770 29 20 41 50 49 73 2e 20 20 54 68 65 20 22 61 75  ) APIs.  The "au
90780 78 64 61 74 61 22 20 69 73 20 73 6f 6d 65 20 61  xdata" is some a
90790 75 78 69 6c 69 61 72 79 20 64 61 74 61 0a 2a 2a  uxiliary data.**
907a0 20 74 68 61 74 20 63 61 6e 20 62 65 20 61 73 73   that can be ass
907b0 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 63  ociated with a c
907c0 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d 65 6e 74  onstant argument
907d0 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20   to a function. 
907e0 20 54 68 69 73 0a 2a 2a 20 61 6c 6c 6f 77 73 20   This.** allows 
907f0 66 75 6e 63 74 69 6f 6e 73 20 73 75 63 68 20 61  functions such a
90800 73 20 22 72 65 67 65 78 70 22 20 74 6f 20 63 6f  s "regexp" to co
90810 6d 70 69 6c 65 20 74 68 65 69 72 20 63 6f 6e 73  mpile their cons
90820 74 61 6e 74 20 72 65 67 75 6c 61 72 0a 2a 2a 20  tant regular.** 
90830 65 78 70 72 65 73 73 69 6f 6e 20 61 72 67 75 6d  expression argum
90840 65 6e 74 20 6f 6e 63 65 20 61 6e 64 20 72 65 75  ent once and reu
90850 73 65 64 20 74 68 65 20 63 6f 6d 70 69 6c 65 64  sed the compiled
90860 20 63 6f 64 65 20 66 6f 72 20 6d 75 6c 74 69 70   code for multip
90870 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e  le.** invocation
90880 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62  s..*/.struct Vdb
90890 65 46 75 6e 63 20 7b 0a 20 20 46 75 6e 63 44 65  eFunc {.  FuncDe
908a0 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20  f *pFunc;       
908b0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
908c0 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65  efinition of the
908d0 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 69   function */.  i
908e0 6e 74 20 6e 41 75 78 3b 20 20 20 20 20 20 20 20  nt nAux;        
908f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
90900 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
90910 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20  s allocated for 
90920 61 70 41 75 78 5b 5d 20 2a 2f 0a 20 20 73 74 72  apAux[] */.  str
90930 75 63 74 20 41 75 78 44 61 74 61 20 7b 0a 20 20  uct AuxData {.  
90940 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20 20 20    void *pAux;   
90950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
90960 2f 2a 20 41 75 78 20 64 61 74 61 20 66 6f 72 20  /* Aux data for 
90970 74 68 65 20 69 2d 74 68 20 61 72 67 75 6d 65 6e  the i-th argumen
90980 74 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 28 2a  t */.    void (*
90990 78 44 65 6c 65 74 65 29 28 76 6f 69 64 20 2a 29  xDelete)(void *)
909a0 3b 20 20 20 20 20 20 2f 2a 20 44 65 73 74 72 75  ;      /* Destru
909b0 63 74 6f 72 20 66 6f 72 20 74 68 65 20 61 75 78  ctor for the aux
909c0 20 64 61 74 61 20 2a 2f 0a 20 20 7d 20 61 70 41   data */.  } apA
909d0 75 78 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 20  ux[1];          
909e0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
909f0 73 6c 6f 74 20 66 6f 72 20 65 61 63 68 20 66 75  slot for each fu
90a00 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20  nction argument 
90a10 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  */.};../*.** The
90a20 20 22 63 6f 6e 74 65 78 74 22 20 61 72 67 75 6d   "context" argum
90a30 65 6e 74 20 66 6f 72 20 61 20 69 6e 73 74 61 6c  ent for a instal
90a40 6c 61 62 6c 65 20 66 75 6e 63 74 69 6f 6e 2e 20  lable function. 
90a50 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   A pointer to an
90a60 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  .** instance of 
90a70 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69  this structure i
90a80 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  s the first argu
90a90 6d 65 6e 74 20 74 6f 20 74 68 65 20 72 6f 75 74  ment to the rout
90aa0 69 6e 65 73 20 75 73 65 64 0a 2a 2a 20 69 6d 70  ines used.** imp
90ab0 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20 66  lement the SQL f
90ac0 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20  unctions..**.** 
90ad0 54 68 65 72 65 20 69 73 20 61 20 74 79 70 65 64  There is a typed
90ae0 65 66 20 66 6f 72 20 74 68 69 73 20 73 74 72 75  ef for this stru
90af0 63 74 75 72 65 20 69 6e 20 73 71 6c 69 74 65 2e  cture in sqlite.
90b00 68 2e 20 20 53 6f 20 61 6c 6c 20 72 6f 75 74 69  h.  So all routi
90b10 6e 65 73 2c 0a 2a 2a 20 65 76 65 6e 20 74 68 65  nes,.** even the
90b20 20 70 75 62 6c 69 63 20 69 6e 74 65 72 66 61 63   public interfac
90b30 65 20 74 6f 20 53 51 4c 69 74 65 2c 20 63 61 6e  e to SQLite, can
90b40 20 75 73 65 20 61 20 70 6f 69 6e 74 65 72 20 74   use a pointer t
90b50 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  o this structure
90b60 2e 0a 2a 2a 20 42 75 74 20 74 68 69 73 20 66 69  ..** But this fi
90b70 6c 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 70  le is the only p
90b80 6c 61 63 65 20 77 68 65 72 65 20 74 68 65 20 69  lace where the i
90b90 6e 74 65 72 6e 61 6c 20 64 65 74 61 69 6c 73 20  nternal details 
90ba0 6f 66 20 74 68 69 73 0a 2a 2a 20 73 74 72 75 63  of this.** struc
90bb0 74 75 72 65 20 61 72 65 20 6b 6e 6f 77 6e 2e 0a  ture are known..
90bc0 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63  **.** This struc
90bd0 74 75 72 65 20 69 73 20 64 65 66 69 6e 65 64 20  ture is defined 
90be0 69 6e 73 69 64 65 20 6f 66 20 76 64 62 65 49 6e  inside of vdbeIn
90bf0 74 2e 68 20 62 65 63 61 75 73 65 20 69 74 20 75  t.h because it u
90c00 73 65 73 20 73 75 62 73 74 72 75 63 74 75 72 65  ses substructure
90c10 73 0a 2a 2a 20 28 4d 65 6d 29 20 77 68 69 63 68  s.** (Mem) which
90c20 20 61 72 65 20 6f 6e 6c 79 20 64 65 66 69 6e 65   are only define
90c30 64 20 74 68 65 72 65 2e 0a 2a 2f 0a 73 74 72 75  d there..*/.stru
90c40 63 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ct sqlite3_conte
90c50 78 74 20 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a  xt {.  FuncDef *
90c60 70 46 75 6e 63 3b 20 20 20 20 20 20 20 2f 2a 20  pFunc;       /* 
90c70 50 6f 69 6e 74 65 72 20 74 6f 20 66 75 6e 63 74  Pointer to funct
90c80 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  ion information.
90c90 20 20 4d 55 53 54 20 42 45 20 46 49 52 53 54 20    MUST BE FIRST 
90ca0 2a 2f 0a 20 20 56 64 62 65 46 75 6e 63 20 2a 70  */.  VdbeFunc *p
90cb0 56 64 62 65 46 75 6e 63 3b 20 20 2f 2a 20 41 75  VdbeFunc;  /* Au
90cc0 78 69 6c 61 72 79 20 64 61 74 61 2c 20 69 66 20  xilary data, if 
90cd0 63 72 65 61 74 65 64 2e 20 2a 2f 0a 20 20 4d 65  created. */.  Me
90ce0 6d 20 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  m s;            
90cf0 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74 75 72      /* The retur
90d00 6e 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65  n value is store
90d10 64 20 68 65 72 65 20 2a 2f 0a 20 20 4d 65 6d 20  d here */.  Mem 
90d20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20  *pMem;          
90d30 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c    /* Memory cell
90d40 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 61   used to store a
90d50 67 67 72 65 67 61 74 65 20 63 6f 6e 74 65 78 74  ggregate context
90d60 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 72 72 6f   */.  int isErro
90d70 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45  r;          /* E
90d80 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e  rror code return
90d90 65 64 20 62 79 20 74 68 65 20 66 75 6e 63 74 69  ed by the functi
90da0 6f 6e 2e 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71  on. */.  CollSeq
90db0 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 2f   *pColl;       /
90dc0 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  * Collating sequ
90dd0 65 6e 63 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  ence */.};../*.*
90de0 2a 20 41 20 53 65 74 20 73 74 72 75 63 74 75 72  * A Set structur
90df0 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 71 75  e is used for qu
90e00 69 63 6b 20 74 65 73 74 69 6e 67 20 74 6f 20 73  ick testing to s
90e10 65 65 20 69 66 20 61 20 76 61 6c 75 65 0a 2a 2a  ee if a value.**
90e20 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 73 6d   is part of a sm
90e30 61 6c 6c 20 73 65 74 2e 20 20 53 65 74 73 20 61  all set.  Sets a
90e40 72 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  re used to imple
90e50 6d 65 6e 74 20 63 6f 64 65 20 6c 69 6b 65 0a 2a  ment code like.*
90e60 2a 20 74 68 69 73 3a 0a 2a 2a 20 20 20 20 20 20  * this:.**      
90e70 20 20 20 20 20 20 78 2e 79 20 49 4e 20 28 27 68        x.y IN ('h
90e80 69 27 2c 27 68 6f 6f 27 2c 27 68 75 6d 27 29 0a  i','hoo','hum').
90e90 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
90ea0 74 20 53 65 74 20 53 65 74 3b 0a 73 74 72 75 63  t Set Set;.struc
90eb0 74 20 53 65 74 20 7b 0a 20 20 48 61 73 68 20 68  t Set {.  Hash h
90ec0 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  ash;            
90ed0 20 2f 2a 20 41 20 73 65 74 20 69 73 20 6a 75 73   /* A set is jus
90ee0 74 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 2a  t a hash table *
90ef0 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 72  /.  HashElem *pr
90f00 65 76 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 72  ev;        /* Pr
90f10 65 76 69 6f 75 73 6c 79 20 61 63 63 65 73 73 65  eviously accesse
90f20 64 20 68 61 73 68 20 65 6c 65 6d 65 6e 20 2a 2f  d hash elemen */
90f30 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 46 69 66  .};../*.** A Fif
90f40 6f 50 61 67 65 20 73 74 72 75 63 74 75 72 65 20  oPage structure 
90f50 68 6f 6c 64 73 20 61 20 73 69 6e 67 6c 65 20 70  holds a single p
90f60 61 67 65 20 6f 66 20 76 61 6c 76 65 73 2e 20 20  age of valves.  
90f70 50 61 67 65 73 20 61 72 65 20 61 72 72 61 6e 67  Pages are arrang
90f80 65 64 0a 2a 2a 20 69 6e 20 61 20 6c 69 73 74 2e  ed.** in a list.
90f90 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
90fa0 63 74 20 46 69 66 6f 50 61 67 65 20 46 69 66 6f  ct FifoPage Fifo
90fb0 50 61 67 65 3b 0a 73 74 72 75 63 74 20 46 69 66  Page;.struct Fif
90fc0 6f 50 61 67 65 20 7b 0a 20 20 69 6e 74 20 6e 53  oPage {.  int nS
90fd0 6c 6f 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  lot;         /* 
90fe0 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
90ff0 73 20 61 53 6c 6f 74 5b 5d 20 2a 2f 0a 20 20 69  s aSlot[] */.  i
91000 6e 74 20 69 57 72 69 74 65 3b 20 20 20 20 20 20  nt iWrite;      
91010 20 20 2f 2a 20 50 75 73 68 20 74 68 65 20 6e 65    /* Push the ne
91020 78 74 20 76 61 6c 75 65 20 69 6e 74 6f 20 74 68  xt value into th
91030 69 73 20 65 6e 74 72 79 20 69 6e 20 61 53 6c 6f  is entry in aSlo
91040 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65  t[] */.  int iRe
91050 61 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52  ad;         /* R
91060 65 61 64 20 74 68 65 20 6e 65 78 74 20 76 61 6c  ead the next val
91070 75 65 20 66 72 6f 6d 20 74 68 69 73 20 65 6e 74  ue from this ent
91080 72 79 20 69 6e 20 61 53 6c 6f 74 5b 5d 20 2a 2f  ry in aSlot[] */
91090 0a 20 20 46 69 66 6f 50 61 67 65 20 2a 70 4e 65  .  FifoPage *pNe
910a0 78 74 3b 20 20 20 2f 2a 20 4e 65 78 74 20 70 61  xt;   /* Next pa
910b0 67 65 20 69 6e 20 74 68 65 20 66 69 66 6f 20 2a  ge in the fifo *
910c0 2f 0a 20 20 69 36 34 20 61 53 6c 6f 74 5b 31 5d  /.  i64 aSlot[1]
910d0 3b 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 72  ;      /* One or
910e0 20 6d 6f 72 65 20 73 6c 6f 74 73 20 66 6f 72 20   more slots for 
910f0 72 6f 77 69 64 20 76 61 6c 75 65 73 20 2a 2f 0a  rowid values */.
91100 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 46 69  };../*.** The Fi
91110 66 6f 20 73 74 72 75 63 74 75 72 65 20 69 73 20  fo structure is 
91120 74 79 70 65 64 65 66 2d 65 64 20 69 6e 20 76 64  typedef-ed in vd
91130 62 65 49 6e 74 2e 68 2e 20 20 42 75 74 20 74 68  beInt.h.  But th
91140 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
91150 0a 2a 2a 20 6f 66 20 74 68 61 74 20 73 74 72 75  .** of that stru
91160 63 74 75 72 65 20 69 73 20 70 72 69 76 61 74 65  cture is private
91170 20 74 6f 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a   to this file..*
91180 2a 0a 2a 2a 20 54 68 65 20 46 69 66 6f 20 73 74  *.** The Fifo st
91190 72 75 63 74 75 72 65 20 64 65 73 63 72 69 62 65  ructure describe
911a0 73 20 74 68 65 20 65 6e 74 69 72 65 20 66 69 66  s the entire fif
911b0 6f 2e 20 20 0a 2a 2f 0a 74 79 70 65 64 65 66 20  o.  .*/.typedef 
911c0 73 74 72 75 63 74 20 46 69 66 6f 20 46 69 66 6f  struct Fifo Fifo
911d0 3b 0a 73 74 72 75 63 74 20 46 69 66 6f 20 7b 0a  ;.struct Fifo {.
911e0 20 20 69 6e 74 20 6e 45 6e 74 72 79 3b 20 20 20    int nEntry;   
911f0 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e        /* Total n
91200 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
91210 20 2a 2f 0a 20 20 46 69 66 6f 50 61 67 65 20 2a   */.  FifoPage *
91220 70 46 69 72 73 74 3b 20 20 20 2f 2a 20 46 69 72  pFirst;   /* Fir
91230 73 74 20 70 61 67 65 20 6f 6e 20 74 68 65 20 6c  st page on the l
91240 69 73 74 20 2a 2f 0a 20 20 46 69 66 6f 50 61 67  ist */.  FifoPag
91250 65 20 2a 70 4c 61 73 74 3b 20 20 20 20 2f 2a 20  e *pLast;    /* 
91260 4c 61 73 74 20 70 61 67 65 20 6f 6e 20 74 68 65  Last page on the
91270 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a   list */.};../*.
91280 2a 2a 20 41 20 43 6f 6e 74 65 78 74 20 73 74 6f  ** A Context sto
91290 72 65 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73  res the last ins
912a0 65 72 74 20 72 6f 77 69 64 2c 20 74 68 65 20 6c  ert rowid, the l
912b0 61 73 74 20 73 74 61 74 65 6d 65 6e 74 20 63 68  ast statement ch
912c0 61 6e 67 65 20 63 6f 75 6e 74 2c 0a 2a 2a 20 61  ange count,.** a
912d0 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  nd the current s
912e0 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 20  tatement change 
912f0 63 6f 75 6e 74 20 28 69 2e 65 2e 20 63 68 61 6e  count (i.e. chan
91300 67 65 73 20 73 69 6e 63 65 20 6c 61 73 74 20 73  ges since last s
91310 74 61 74 65 6d 65 6e 74 29 2e 0a 2a 2a 20 54 68  tatement)..** Th
91320 65 20 63 75 72 72 65 6e 74 20 6b 65 79 6c 69 73  e current keylis
91330 74 20 69 73 20 61 6c 73 6f 20 73 74 6f 72 65 64  t is also stored
91340 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 78 74 2e   in the context.
91350 0a 2a 2a 20 45 6c 65 6d 65 6e 74 73 20 6f 66 20  .** Elements of 
91360 43 6f 6e 74 65 78 74 20 73 74 72 75 63 74 75 72  Context structur
91370 65 20 74 79 70 65 20 6d 61 6b 65 20 75 70 20 74  e type make up t
91380 68 65 20 43 6f 6e 74 65 78 74 53 74 61 63 6b 2c  he ContextStack,
91390 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 75 70 64   which is.** upd
913a0 61 74 65 64 20 62 79 20 74 68 65 20 43 6f 6e 74  ated by the Cont
913b0 65 78 74 50 75 73 68 20 61 6e 64 20 43 6f 6e 74  extPush and Cont
913c0 65 78 74 50 6f 70 20 6f 70 63 6f 64 65 73 20 28  extPop opcodes (
913d0 75 73 65 64 20 62 79 20 74 72 69 67 67 65 72 73  used by triggers
913e0 29 2e 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 78  )..** The contex
913f0 74 20 69 73 20 70 75 73 68 65 64 20 62 65 66 6f  t is pushed befo
91400 72 65 20 65 78 65 63 75 74 69 6e 67 20 61 20 74  re executing a t
91410 72 69 67 67 65 72 20 61 20 70 6f 70 70 65 64 20  rigger a popped 
91420 77 68 65 6e 20 74 68 65 0a 2a 2a 20 74 72 69 67  when the.** trig
91430 67 65 72 20 66 69 6e 69 73 68 65 73 2e 0a 2a 2f  ger finishes..*/
91440 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
91450 43 6f 6e 74 65 78 74 20 43 6f 6e 74 65 78 74 3b  Context Context;
91460 0a 73 74 72 75 63 74 20 43 6f 6e 74 65 78 74 20  .struct Context 
91470 7b 0a 20 20 69 36 34 20 6c 61 73 74 52 6f 77 69  {.  i64 lastRowi
91480 64 3b 20 20 20 20 2f 2a 20 4c 61 73 74 20 69 6e  d;    /* Last in
91490 73 65 72 74 20 72 6f 77 69 64 20 28 73 71 6c 69  sert rowid (sqli
914a0 74 65 33 2e 6c 61 73 74 52 6f 77 69 64 29 20 2a  te3.lastRowid) *
914b0 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b  /.  int nChange;
914c0 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 6d 65        /* Stateme
914d0 6e 74 20 63 68 61 6e 67 65 73 20 28 56 64 62 65  nt changes (Vdbe
914e0 2e 6e 43 68 61 6e 67 65 73 29 20 20 20 20 20 2a  .nChanges)     *
914f0 2f 0a 20 20 46 69 66 6f 20 73 46 69 66 6f 3b 20  /.  Fifo sFifo; 
91500 20 20 20 20 20 20 2f 2a 20 52 65 63 6f 72 64 73        /* Records
91510 20 74 68 61 74 20 77 69 6c 6c 20 70 61 72 74 69   that will parti
91520 63 69 70 61 74 65 20 69 6e 20 61 20 44 45 4c 45  cipate in a DELE
91530 54 45 20 6f 72 20 55 50 44 41 54 45 20 2a 2f 0a  TE or UPDATE */.
91540 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  };../*.** An ins
91550 74 61 6e 63 65 20 6f 66 20 74 68 65 20 76 69 72  tance of the vir
91560 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 54  tual machine.  T
91570 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63 6f  his structure co
91580 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6d 70 6c  ntains the compl
91590 65 74 65 0a 2a 2a 20 73 74 61 74 65 20 6f 66 20  ete.** state of 
915a0 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68  the virtual mach
915b0 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22  ine..**.** The "
915c0 73 71 6c 69 74 65 33 5f 73 74 6d 74 22 20 73 74  sqlite3_stmt" st
915d0 72 75 63 74 75 72 65 20 70 6f 69 6e 74 65 72 20  ructure pointer 
915e0 74 68 61 74 20 69 73 20 72 65 74 75 72 6e 65 64  that is returned
915f0 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70   by sqlite3_comp
91600 69 6c 65 28 29 0a 2a 2a 20 69 73 20 72 65 61 6c  ile().** is real
91610 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ly a pointer to 
91620 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
91630 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  his structure..*
91640 2a 0a 2a 2a 20 54 68 65 20 56 64 62 65 2e 69 6e  *.** The Vdbe.in
91650 56 74 61 62 4d 65 74 68 6f 64 20 76 61 72 69 61  VtabMethod varia
91660 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20 6e 6f  ble is set to no
91670 6e 2d 7a 65 72 6f 20 66 6f 72 20 74 68 65 20 64  n-zero for the d
91680 75 72 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 61 6e  uration of.** an
91690 79 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  y virtual table 
916a0 6d 65 74 68 6f 64 20 69 6e 76 6f 63 61 74 69 6f  method invocatio
916b0 6e 73 20 6d 61 64 65 20 62 79 20 74 68 65 20 76  ns made by the v
916c0 64 62 65 20 70 72 6f 67 72 61 6d 2e 20 49 74 20  dbe program. It 
916d0 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 32 20 66  is.** set to 2 f
916e0 6f 72 20 78 44 65 73 74 72 6f 79 20 6d 65 74 68  or xDestroy meth
916f0 6f 64 20 63 61 6c 6c 73 20 61 6e 64 20 31 20 66  od calls and 1 f
91700 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 6d 65 74  or all other met
91710 68 6f 64 73 2e 20 54 68 69 73 0a 2a 2a 20 76 61  hods. This.** va
91720 72 69 61 62 6c 65 20 69 73 20 75 73 65 64 20 66  riable is used f
91730 6f 72 20 74 77 6f 20 70 75 72 70 6f 73 65 73 3a  or two purposes:
91740 20 74 6f 20 61 6c 6c 6f 77 20 78 44 65 73 74 72   to allow xDestr
91750 6f 79 20 6d 65 74 68 6f 64 73 20 74 6f 20 65 78  oy methods to ex
91760 65 63 75 74 65 0a 2a 2a 20 22 44 52 4f 50 20 54  ecute.** "DROP T
91770 41 42 4c 45 22 20 73 74 61 74 65 6d 65 6e 74 73  ABLE" statements
91780 20 61 6e 64 20 74 6f 20 70 72 65 76 65 6e 74 20   and to prevent 
91790 73 6f 6d 65 20 6e 61 73 74 79 20 73 69 64 65 20  some nasty side 
917a0 65 66 66 65 63 74 73 20 6f 66 0a 2a 2a 20 6d 61  effects of.** ma
917b0 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 77 68 65  lloc failure whe
917c0 6e 20 53 51 4c 69 74 65 20 69 73 20 69 6e 76 6f  n SQLite is invo
917d0 6b 65 64 20 72 65 63 75 72 73 69 76 65 6c 79 20  ked recursively 
917e0 62 79 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  by a virtual tab
917f0 6c 65 20 0a 2a 2a 20 6d 65 74 68 6f 64 20 66 75  le .** method fu
91800 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63  nction..*/.struc
91810 74 20 56 64 62 65 20 7b 0a 20 20 73 71 6c 69 74  t Vdbe {.  sqlit
91820 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 2f  e3 *db;        /
91830 2a 20 54 68 65 20 77 68 6f 6c 65 20 64 61 74 61  * The whole data
91840 62 61 73 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a  base */.  Vdbe *
91850 70 50 72 65 76 2c 2a 70 4e 65 78 74 3b 20 2f 2a  pPrev,*pNext; /*
91860 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20   Linked list of 
91870 56 44 42 45 73 20 77 69 74 68 20 74 68 65 20 73  VDBEs with the s
91880 61 6d 65 20 56 64 62 65 2e 64 62 20 2a 2f 0a 20  ame Vdbe.db */. 
91890 20 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20 20   int nOp;       
918a0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
918b0 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 69  f instructions i
918c0 6e 20 74 68 65 20 70 72 6f 67 72 61 6d 20 2a 2f  n the program */
918d0 0a 20 20 69 6e 74 20 6e 4f 70 41 6c 6c 6f 63 3b  .  int nOpAlloc;
918e0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
918f0 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 61   of slots alloca
91900 74 65 64 20 66 6f 72 20 61 4f 70 5b 5d 20 2a 2f  ted for aOp[] */
91910 0a 20 20 4f 70 20 2a 61 4f 70 3b 20 20 20 20 20  .  Op *aOp;     
91920 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20         /* Space 
91930 74 6f 20 68 6f 6c 64 20 74 68 65 20 76 69 72 74  to hold the virt
91940 75 61 6c 20 6d 61 63 68 69 6e 65 27 73 20 70 72  ual machine's pr
91950 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e  ogram */.  int n
91960 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 20 20 2f  Label;         /
91970 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 61 62 65  * Number of labe
91980 6c 73 20 75 73 65 64 20 2a 2f 0a 20 20 69 6e 74  ls used */.  int
91990 20 6e 4c 61 62 65 6c 41 6c 6c 6f 63 3b 20 20 20   nLabelAlloc;   
919a0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c   /* Number of sl
919b0 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e  ots allocated in
919c0 20 61 4c 61 62 65 6c 5b 5d 20 2a 2f 0a 20 20 69   aLabel[] */.  i
919d0 6e 74 20 2a 61 4c 61 62 65 6c 3b 20 20 20 20 20  nt *aLabel;     
919e0 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 68     /* Space to h
919f0 6f 6c 64 20 74 68 65 20 6c 61 62 65 6c 73 20 2a  old the labels *
91a00 2f 0a 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b  /.  Mem **apArg;
91a10 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d          /* Argum
91a20 65 6e 74 73 20 74 6f 20 63 75 72 72 65 6e 74 6c  ents to currentl
91a30 79 20 65 78 65 63 75 74 69 6e 67 20 75 73 65 72  y executing user
91a40 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 4d   function */.  M
91a50 65 6d 20 2a 61 43 6f 6c 4e 61 6d 65 3b 20 20 20  em *aColName;   
91a60 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d     /* Column nam
91a70 65 73 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a  es to return */.
91a80 20 20 69 6e 74 20 6e 43 75 72 73 6f 72 3b 20 20    int nCursor;  
91a90 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
91aa0 6f 66 20 73 6c 6f 74 73 20 69 6e 20 61 70 43 73  of slots in apCs
91ab0 72 5b 5d 20 2a 2f 0a 20 20 43 75 72 73 6f 72 20  r[] */.  Cursor 
91ac0 2a 2a 61 70 43 73 72 3b 20 20 20 20 20 2f 2a 20  **apCsr;     /* 
91ad0 4f 6e 65 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74  One element of t
91ae0 68 69 73 20 61 72 72 61 79 20 66 6f 72 20 65 61  his array for ea
91af0 63 68 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 2a  ch open cursor *
91b00 2f 0a 20 20 69 6e 74 20 6e 56 61 72 3b 20 20 20  /.  int nVar;   
91b10 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
91b20 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
91b30 61 56 61 72 5b 5d 20 2a 2f 0a 20 20 4d 65 6d 20  aVar[] */.  Mem 
91b40 2a 61 56 61 72 3b 20 20 20 20 20 20 20 20 20 20  *aVar;          
91b50 2f 2a 20 56 61 6c 75 65 73 20 66 6f 72 20 74 68  /* Values for th
91b60 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 6f 70  e OP_Variable op
91b70 63 6f 64 65 2e 20 2a 2f 0a 20 20 63 68 61 72 20  code. */.  char 
91b80 2a 2a 61 7a 56 61 72 3b 20 20 20 20 20 20 20 2f  **azVar;       /
91b90 2a 20 4e 61 6d 65 20 6f 66 20 76 61 72 69 61 62  * Name of variab
91ba0 6c 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 6b 56  les */.  int okV
91bb0 61 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ar;          /* 
91bc0 54 72 75 65 20 69 66 20 61 7a 56 61 72 5b 5d 20  True if azVar[] 
91bd0 68 61 73 20 62 65 65 6e 20 69 6e 69 74 69 61 6c  has been initial
91be0 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6d 61  ized */.  int ma
91bf0 67 69 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  gic;            
91c00 20 20 2f 2a 20 4d 61 67 69 63 20 6e 75 6d 62 65    /* Magic numbe
91c10 72 20 66 6f 72 20 73 61 6e 69 74 79 20 63 68 65  r for sanity che
91c20 63 6b 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e  cking */.  int n
91c30 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  Mem;            
91c40 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
91c50 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73  memory locations
91c60 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63   currently alloc
91c70 61 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61  ated */.  Mem *a
91c80 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  Mem;            
91c90 20 20 2f 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20    /* The memory 
91ca0 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69  locations */.  i
91cb0 6e 74 20 6e 43 61 6c 6c 62 61 63 6b 3b 20 20 20  nt nCallback;   
91cc0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
91cd0 20 6f 66 20 63 61 6c 6c 62 61 63 6b 73 20 69 6e   of callbacks in
91ce0 76 6f 6b 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a  voked so far */.
91cf0 20 20 69 6e 74 20 63 61 63 68 65 43 74 72 3b 20    int cacheCtr; 
91d00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
91d10 73 6f 72 20 72 6f 77 20 63 61 63 68 65 20 67 65  sor row cache ge
91d20 6e 65 72 61 74 69 6f 6e 20 63 6f 75 6e 74 65 72  neration counter
91d30 20 2a 2f 0a 20 20 46 69 66 6f 20 73 46 69 66 6f   */.  Fifo sFifo
91d40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
91d50 20 41 20 6c 69 73 74 20 6f 66 20 52 4f 57 49 44   A list of ROWID
91d60 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6e 74 65  s */.  int conte
91d70 78 74 53 74 61 63 6b 54 6f 70 3b 20 20 20 20 2f  xtStackTop;    /
91d80 2a 20 49 6e 64 65 78 20 6f 66 20 74 6f 70 20 65  * Index of top e
91d90 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 63 6f  lement in the co
91da0 6e 74 65 78 74 20 73 74 61 63 6b 20 2a 2f 0a 20  ntext stack */. 
91db0 20 69 6e 74 20 63 6f 6e 74 65 78 74 53 74 61 63   int contextStac
91dc0 6b 44 65 70 74 68 3b 20 20 2f 2a 20 54 68 65 20  kDepth;  /* The 
91dd0 73 69 7a 65 20 6f 66 20 74 68 65 20 22 63 6f 6e  size of the "con
91de0 74 65 78 74 22 20 73 74 61 63 6b 20 2a 2f 0a 20  text" stack */. 
91df0 20 43 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78   Context *contex
91e00 74 53 74 61 63 6b 3b 20 20 2f 2a 20 53 74 61 63  tStack;  /* Stac
91e10 6b 20 75 73 65 64 20 62 79 20 6f 70 63 6f 64 65  k used by opcode
91e20 73 20 43 6f 6e 74 65 78 74 50 75 73 68 20 26 20  s ContextPush & 
91e30 43 6f 6e 74 65 78 74 50 6f 70 2a 2f 0a 20 20 69  ContextPop*/.  i
91e40 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20 20  nt pc;          
91e50 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72         /* The pr
91e60 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 2a 2f  ogram counter */
91e70 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
91e80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
91e90 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f  lue to return */
91ea0 0a 20 20 75 6e 73 69 67 6e 65 64 20 75 6e 69 71  .  unsigned uniq
91eb0 75 65 43 6e 74 3b 20 20 20 20 20 2f 2a 20 55 73  ueCnt;     /* Us
91ec0 65 64 20 62 79 20 4f 50 5f 4d 61 6b 65 52 65 63  ed by OP_MakeRec
91ed0 6f 72 64 20 77 68 65 6e 20 50 32 21 3d 30 20 2a  ord when P2!=0 *
91ee0 2f 0a 20 20 69 6e 74 20 65 72 72 6f 72 41 63 74  /.  int errorAct
91ef0 69 6f 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 52  ion;        /* R
91f00 65 63 6f 76 65 72 79 20 61 63 74 69 6f 6e 20 74  ecovery action t
91f10 6f 20 64 6f 20 69 6e 20 63 61 73 65 20 6f 66 20  o do in case of 
91f20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 69 6e  an error */.  in
91f30 74 20 69 6e 54 65 6d 70 54 72 61 6e 73 3b 20 20  t inTempTrans;  
91f40 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
91f50 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 20 69   temp database i
91f60 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 65 64 20  s transactioned 
91f70 2a 2f 0a 20 20 69 6e 74 20 72 65 74 75 72 6e 53  */.  int returnS
91f80 74 61 63 6b 5b 32 35 5d 3b 20 20 20 20 2f 2a 20  tack[25];    /* 
91f90 52 65 74 75 72 6e 20 61 64 64 72 65 73 73 20 73  Return address s
91fa0 74 61 63 6b 20 66 6f 72 20 4f 50 5f 47 6f 73 75  tack for OP_Gosu
91fb0 62 20 26 20 4f 50 5f 52 65 74 75 72 6e 20 2a 2f  b & OP_Return */
91fc0 0a 20 20 69 6e 74 20 72 65 74 75 72 6e 44 65 70  .  int returnDep
91fd0 74 68 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65  th;        /* Ne
91fe0 78 74 20 75 6e 75 73 65 64 20 65 6c 65 6d 65 6e  xt unused elemen
91ff0 74 20 69 6e 20 72 65 74 75 72 6e 53 74 61 63 6b  t in returnStack
92000 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 73  [] */.  int nRes
92010 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20  Column;         
92020 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  /* Number of col
92030 75 6d 6e 73 20 69 6e 20 6f 6e 65 20 72 6f 77 20  umns in one row 
92040 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  of the result se
92050 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a  t */.  char **az
92060 52 65 73 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 2f  ResColumn;     /
92070 2a 20 56 61 6c 75 65 73 20 66 6f 72 20 6f 6e 65  * Values for one
92080 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 20 2a   row of result *
92090 2f 20 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d  / .  char *zErrM
920a0 73 67 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  sg;          /* 
920b0 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 72  Error message wr
920c0 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20  itten here */.  
920d0 4d 65 6d 20 2a 70 52 65 73 75 6c 74 53 65 74 3b  Mem *pResultSet;
920e0 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
920f0 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f  er to an array o
92100 66 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 75  f results */.  u
92110 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 20 20  8 explain;      
92120 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
92130 66 20 45 58 50 4c 41 49 4e 20 70 72 65 73 65 6e  f EXPLAIN presen
92140 74 20 6f 6e 20 53 51 4c 20 63 6f 6d 6d 61 6e 64  t on SQL command
92150 20 2a 2f 0a 20 20 75 38 20 63 68 61 6e 67 65 43   */.  u8 changeC
92160 6e 74 4f 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a  ntOn;         /*
92170 20 54 72 75 65 20 74 6f 20 75 70 64 61 74 65 20   True to update 
92180 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74  the change-count
92190 65 72 20 2a 2f 0a 20 20 75 38 20 61 62 6f 72 74  er */.  u8 abort
921a0 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ed;             
921b0 2f 2a 20 54 72 75 65 20 69 66 20 52 4f 4c 4c 42  /* True if ROLLB
921c0 41 43 4b 20 69 6e 20 61 6e 6f 74 68 65 72 20 56  ACK in another V
921d0 4d 20 63 61 75 73 65 73 20 61 6e 20 61 62 6f 72  M causes an abor
921e0 74 20 2a 2f 0a 20 20 75 38 20 65 78 70 69 72 65  t */.  u8 expire
921f0 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  d;             /
92200 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 56 4d  * True if the VM
92210 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 63   needs to be rec
92220 6f 6d 70 69 6c 65 64 20 2a 2f 0a 20 20 75 38 20  ompiled */.  u8 
92230 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d  minWriteFileForm
92240 61 74 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20  at;  /* Minimum 
92250 66 69 6c 65 20 66 6f 72 6d 61 74 20 66 6f 72 20  file format for 
92260 77 72 69 74 61 62 6c 65 20 64 61 74 61 62 61 73  writable databas
92270 65 20 66 69 6c 65 73 20 2a 2f 0a 20 20 75 38 20  e files */.  u8 
92280 69 6e 56 74 61 62 4d 65 74 68 6f 64 3b 20 20 20  inVtabMethod;   
92290 20 20 20 20 20 2f 2a 20 53 65 65 20 63 6f 6d 6d       /* See comm
922a0 65 6e 74 73 20 61 62 6f 76 65 20 2a 2f 0a 20 20  ents above */.  
922b0 69 6e 74 20 6e 43 68 61 6e 67 65 3b 20 20 20 20  int nChange;    
922c0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
922d0 72 20 6f 66 20 64 62 20 63 68 61 6e 67 65 73 20  r of db changes 
922e0 6d 61 64 65 20 73 69 6e 63 65 20 6c 61 73 74 20  made since last 
922f0 72 65 73 65 74 20 2a 2f 0a 20 20 69 36 34 20 73  reset */.  i64 s
92300 74 61 72 74 54 69 6d 65 3b 20 20 20 20 20 20 20  tartTime;       
92310 20 20 20 2f 2a 20 54 69 6d 65 20 77 68 65 6e 20     /* Time when 
92320 71 75 65 72 79 20 73 74 61 72 74 65 64 20 2d 20  query started - 
92330 75 73 65 64 20 66 6f 72 20 70 72 6f 66 69 6c 69  used for profili
92340 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 74 72 65  ng */.  int btre
92350 65 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20  eMask;          
92360 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 64 62  /* Bitmask of db
92370 2d 3e 61 44 62 5b 5d 20 65 6e 74 72 69 65 73 20  ->aDb[] entries 
92380 72 65 66 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20  referenced */.  
92390 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 20  BtreeMutexArray 
923a0 61 4d 75 74 65 78 3b 20 2f 2a 20 41 6e 20 61 72  aMutex; /* An ar
923b0 72 61 79 20 6f 66 20 42 74 72 65 65 20 75 73 65  ray of Btree use
923c0 64 20 68 65 72 65 20 61 6e 64 20 6e 65 65 64 69  d here and needi
923d0 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 69 6e  ng locks */.  in
923e0 74 20 6e 53 71 6c 3b 20 20 20 20 20 20 20 20 20  t nSql;         
923f0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
92400 20 62 79 74 65 73 20 69 6e 20 7a 53 71 6c 20 2a   bytes in zSql *
92410 2f 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20  /.  char *zSql; 
92420 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78            /* Tex
92430 74 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61  t of the SQL sta
92440 74 65 6d 65 6e 74 20 74 68 61 74 20 67 65 6e 65  tement that gene
92450 72 61 74 65 64 20 74 68 69 73 20 2a 2f 0a 23 69  rated this */.#i
92460 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
92470 47 0a 20 20 46 49 4c 45 20 2a 74 72 61 63 65 3b  G.  FILE *trace;
92480 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
92490 20 61 6e 20 65 78 65 63 75 74 69 6f 6e 20 74 72   an execution tr
924a0 61 63 65 20 68 65 72 65 2c 20 69 66 20 6e 6f 74  ace here, if not
924b0 20 4e 55 4c 4c 20 2a 2f 0a 23 65 6e 64 69 66 0a   NULL */.#endif.
924c0 20 20 69 6e 74 20 6f 70 65 6e 65 64 53 74 61 74    int openedStat
924d0 65 6d 65 6e 74 3b 20 20 2f 2a 20 54 72 75 65 20  ement;  /* True 
924e0 69 66 20 74 68 69 73 20 56 4d 20 68 61 73 20 6f  if this VM has o
924f0 70 65 6e 65 64 20 61 20 73 74 61 74 65 6d 65 6e  pened a statemen
92500 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 69 66  t journal */.#if
92510 64 65 66 20 53 51 4c 49 54 45 5f 53 53 45 0a 20  def SQLITE_SSE. 
92520 20 69 6e 74 20 66 65 74 63 68 49 64 3b 20 20 20   int fetchId;   
92530 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 6d         /* Statem
92540 65 6e 74 20 6e 75 6d 62 65 72 20 75 73 65 64 20  ent number used 
92550 62 79 20 73 71 6c 69 74 65 33 5f 66 65 74 63 68  by sqlite3_fetch
92560 5f 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20  _statement */.  
92570 69 6e 74 20 6c 72 75 3b 20 20 20 20 20 20 20 20  int lru;        
92580 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72        /* Counter
92590 20 75 73 65 64 20 66 6f 72 20 4c 52 55 20 63 61   used for LRU ca
925a0 63 68 65 20 72 65 70 6c 61 63 65 6d 65 6e 74 20  che replacement 
925b0 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  */.#endif.#ifdef
925c0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
925d0 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54  EMORY_MANAGEMENT
925e0 0a 20 20 56 64 62 65 20 2a 70 4c 72 75 50 72 65  .  Vdbe *pLruPre
925f0 76 3b 0a 20 20 56 64 62 65 20 2a 70 4c 72 75 4e  v;.  Vdbe *pLruN
92600 65 78 74 3b 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a  ext;.#endif.};..
92610 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
92620 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
92630 6e 67 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c  ng structure hol
92640 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  ds information a
92650 62 6f 75 74 20 61 0a 2a 2a 20 73 69 6e 67 6c 65  bout a.** single
92660 20 69 6e 64 65 78 20 72 65 63 6f 72 64 20 74 68   index record th
92670 61 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  at has already b
92680 65 65 6e 20 70 61 72 73 65 64 20 6f 75 74 20 69  een parsed out i
92690 6e 74 6f 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a  nto individual.*
926a0 2a 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20  * values..**.** 
926b0 41 20 72 65 63 6f 72 64 20 69 73 20 61 6e 20 6f  A record is an o
926c0 62 6a 65 63 74 20 74 68 61 74 20 63 6f 6e 74 61  bject that conta
926d0 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  ins one or more 
926e0 66 69 65 6c 64 73 20 6f 66 20 64 61 74 61 2e 0a  fields of data..
926f0 2a 2a 20 52 65 63 6f 72 64 73 20 61 72 65 20 75  ** Records are u
92700 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65  sed to store the
92710 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 74 61   content of a ta
92720 62 6c 65 20 72 6f 77 20 61 6e 64 20 74 6f 20 73  ble row and to s
92730 74 6f 72 65 0a 2a 2a 20 74 68 65 20 6b 65 79 20  tore.** the key 
92740 6f 66 20 61 6e 20 69 6e 64 65 78 2e 20 20 41 20  of an index.  A 
92750 62 6c 6f 62 20 65 6e 63 6f 64 69 6e 67 20 6f 66  blob encoding of
92760 20 61 20 72 65 63 6f 72 64 20 69 73 20 63 72 65   a record is cre
92770 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 4f  ated by.** the O
92780 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63  P_MakeRecord opc
92790 6f 64 65 20 6f 66 20 74 68 65 20 56 44 42 45 20  ode of the VDBE 
927a0 61 6e 64 20 69 73 20 64 69 73 61 73 73 65 6d 62  and is disassemb
927b0 6c 69 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 4f  lied by the.** O
927c0 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e  P_Column opcode.
927d0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75  .**.** This stru
927e0 63 74 75 72 65 20 68 6f 6c 64 73 20 61 20 72 65  cture holds a re
927f0 63 6f 72 64 20 74 68 61 74 20 68 61 73 20 61 6c  cord that has al
92800 72 65 61 64 79 20 62 65 65 6e 20 64 69 73 61 73  ready been disas
92810 73 65 6d 62 6c 65 64 0a 2a 2a 20 69 6e 74 6f 20  sembled.** into 
92820 69 74 73 20 63 6f 6e 73 74 69 74 75 74 65 6e 74  its constitutent
92830 20 66 69 65 6c 64 73 2e 0a 2a 2f 0a 73 74 72 75   fields..*/.stru
92840 63 74 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  ct UnpackedRecor
92850 64 20 7b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70  d {.  KeyInfo *p
92860 4b 65 79 49 6e 66 6f 3b 20 20 2f 2a 20 43 6f 6c  KeyInfo;  /* Col
92870 6c 61 74 69 6f 6e 20 61 6e 64 20 73 6f 72 74 2d  lation and sort-
92880 6f 72 64 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f  order informatio
92890 6e 20 2a 2f 0a 20 20 75 31 36 20 6e 46 69 65 6c  n */.  u16 nFiel
928a0 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  d;         /* Nu
928b0 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
928c0 69 6e 20 61 70 4d 65 6d 5b 5d 20 2a 2f 0a 20 20  in apMem[] */.  
928d0 75 38 20 6e 65 65 64 46 72 65 65 3b 20 20 20 20  u8 needFree;    
928e0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6d      /* True if m
928f0 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
92900 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  rom sqlite3_mall
92910 6f 63 28 29 20 2a 2f 0a 20 20 75 38 20 6e 65 65  oc() */.  u8 nee
92920 64 44 65 73 74 72 6f 79 3b 20 20 20 20 20 2f 2a  dDestroy;     /*
92930 20 54 72 75 65 20 69 66 20 61 70 4d 65 6d 5b 5d   True if apMem[]
92940 73 20 73 68 6f 75 6c 64 20 62 65 20 64 65 73 74  s should be dest
92950 72 6f 79 65 64 20 6f 6e 20 63 6c 6f 73 65 20 2a  royed on close *
92960 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20  /.  Mem *aMem;  
92970 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
92980 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  s */.};../*.** T
92990 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65  he following are
929a0 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20   allowed values 
929b0 66 6f 72 20 56 64 62 65 2e 6d 61 67 69 63 0a 2a  for Vdbe.magic.*
929c0 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d  /.#define VDBE_M
929d0 41 47 49 43 5f 49 4e 49 54 20 20 20 20 20 30 78  AGIC_INIT     0x
929e0 32 36 62 63 65 61 61 35 20 20 20 20 2f 2a 20 42  26bceaa5    /* B
929f0 75 69 6c 64 69 6e 67 20 61 20 56 44 42 45 20 70  uilding a VDBE p
92a00 72 6f 67 72 61 6d 20 2a 2f 0a 23 64 65 66 69 6e  rogram */.#defin
92a10 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e  e VDBE_MAGIC_RUN
92a20 20 20 20 20 20 20 30 78 62 64 66 32 30 64 61 33        0xbdf20da3
92a30 20 20 20 20 2f 2a 20 56 44 42 45 20 69 73 20 72      /* VDBE is r
92a40 65 61 64 79 20 74 6f 20 65 78 65 63 75 74 65 20  eady to execute 
92a50 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f  */.#define VDBE_
92a60 4d 41 47 49 43 5f 48 41 4c 54 20 20 20 20 20 30  MAGIC_HALT     0
92a70 78 35 31 39 63 32 39 37 33 20 20 20 20 2f 2a 20  x519c2973    /* 
92a80 56 44 42 45 20 68 61 73 20 63 6f 6d 70 6c 65 74  VDBE has complet
92a90 65 64 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f 0a  ed execution */.
92aa0 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d 41 47  #define VDBE_MAG
92ab0 49 43 5f 44 45 41 44 20 20 20 20 20 30 78 62 36  IC_DEAD     0xb6
92ac0 30 36 63 33 63 38 20 20 20 20 2f 2a 20 54 68 65  06c3c8    /* The
92ad0 20 56 44 42 45 20 68 61 73 20 62 65 65 6e 20 64   VDBE has been d
92ae0 65 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a 2f  eallocated */../
92af0 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 70 72  *.** Function pr
92b00 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51 4c 49  ototypes.*/.SQLI
92b10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
92b20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43  sqlite3VdbeFreeC
92b30 75 72 73 6f 72 28 56 64 62 65 20 2a 2c 20 43 75  ursor(Vdbe *, Cu
92b40 72 73 6f 72 2a 29 3b 0a 76 6f 69 64 20 73 71 6c  rsor*);.void sql
92b50 69 74 65 56 64 62 65 50 6f 70 53 74 61 63 6b 28  iteVdbePopStack(
92b60 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  Vdbe*,int);.SQLI
92b70 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
92b80 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72  qlite3VdbeCursor
92b90 4d 6f 76 65 74 6f 28 43 75 72 73 6f 72 2a 29 3b  Moveto(Cursor*);
92ba0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
92bb0 49 54 45 5f 44 45 42 55 47 29 20 7c 7c 20 64 65  ITE_DEBUG) || de
92bc0 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49  fined(VDBE_PROFI
92bd0 4c 45 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  LE).SQLITE_PRIVA
92be0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
92bf0 64 62 65 50 72 69 6e 74 4f 70 28 46 49 4c 45 2a  dbePrintOp(FILE*
92c00 2c 20 69 6e 74 2c 20 4f 70 2a 29 3b 0a 23 65 6e  , int, Op*);.#en
92c10 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  dif.SQLITE_PRIVA
92c20 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
92c30 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28  beSerialTypeLen(
92c40 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  u32);.SQLITE_PRI
92c50 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33  VATE u32 sqlite3
92c60 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 4d  VdbeSerialType(M
92c70 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  em*, int);.SQLIT
92c80 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
92c90 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 50  lite3VdbeSerialP
92ca0 75 74 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  ut(unsigned char
92cb0 2a 2c 20 69 6e 74 2c 20 4d 65 6d 2a 2c 20 69 6e  *, int, Mem*, in
92cc0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
92cd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
92ce0 62 65 53 65 72 69 61 6c 47 65 74 28 63 6f 6e 73  beSerialGet(cons
92cf0 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a  t unsigned char*
92d00 2c 20 75 33 32 2c 20 4d 65 6d 2a 29 3b 0a 53 51  , u32, Mem*);.SQ
92d10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
92d20 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c  d sqlite3VdbeDel
92d30 65 74 65 41 75 78 44 61 74 61 28 56 64 62 65 46  eteAuxData(VdbeF
92d40 75 6e 63 2a 2c 20 69 6e 74 29 3b 0a 0a 69 6e 74  unc*, int);..int
92d50 20 73 71 6c 69 74 65 32 42 74 72 65 65 4b 65 79   sqlite2BtreeKey
92d60 43 6f 6d 70 61 72 65 28 42 74 43 75 72 73 6f 72  Compare(BtCursor
92d70 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a   *, const void *
92d80 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 20  , int, int, int 
92d90 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
92da0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
92db0 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 28  beIdxKeyCompare(
92dc0 43 75 72 73 6f 72 2a 2c 55 6e 70 61 63 6b 65 64  Cursor*,Unpacked
92dd0 52 65 63 6f 72 64 20 2a 2c 69 6e 74 2c 63 6f 6e  Record *,int,con
92de0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
92df0 2a 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  *,int*);.SQLITE_
92e00 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
92e10 74 65 33 56 64 62 65 49 64 78 52 6f 77 69 64 28  te3VdbeIdxRowid(
92e20 42 74 43 75 72 73 6f 72 20 2a 2c 20 69 36 34 20  BtCursor *, i64 
92e30 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
92e40 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65  TE int sqlite3Me
92e50 6d 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 4d  mCompare(const M
92e60 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 2c  em*, const Mem*,
92e70 20 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 2a 29   const CollSeq*)
92e80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
92e90 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
92ea0 49 64 78 52 6f 77 69 64 4c 65 6e 28 63 6f 6e 73  IdxRowidLen(cons
92eb0 74 20 75 38 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  t u8*);.SQLITE_P
92ec0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
92ed0 65 33 56 64 62 65 45 78 65 63 28 56 64 62 65 2a  e3VdbeExec(Vdbe*
92ee0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
92ef0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
92f00 65 4c 69 73 74 28 56 64 62 65 2a 29 3b 0a 53 51  eList(Vdbe*);.SQ
92f10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
92f20 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74   sqlite3VdbeHalt
92f30 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  (Vdbe*);.SQLITE_
92f40 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
92f50 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63  te3VdbeChangeEnc
92f60 6f 64 69 6e 67 28 4d 65 6d 20 2a 2c 20 69 6e 74  oding(Mem *, int
92f70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
92f80 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
92f90 65 4d 65 6d 54 6f 6f 42 69 67 28 4d 65 6d 2a 29  eMemTooBig(Mem*)
92fa0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
92fb0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
92fc0 4d 65 6d 43 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f  MemCopy(Mem*, co
92fd0 6e 73 74 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54  nst Mem*);.SQLIT
92fe0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
92ff0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61  qlite3VdbeMemSha
93000 6c 6c 6f 77 43 6f 70 79 28 4d 65 6d 2a 2c 20 63  llowCopy(Mem*, c
93010 6f 6e 73 74 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b  onst Mem*, int);
93020 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
93030 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
93040 4d 65 6d 4d 6f 76 65 28 4d 65 6d 2a 2c 20 4d 65  MemMove(Mem*, Me
93050 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
93060 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
93070 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61  dbeMemNulTermina
93080 74 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45  te(Mem*);.SQLITE
93090 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
930a0 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
930b0 72 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 63 68  r(Mem*, const ch
930c0 61 72 2a 2c 20 69 6e 74 2c 20 75 38 2c 20 76 6f  ar*, int, u8, vo
930d0 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53  id(*)(void*));.S
930e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
930f0 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  id sqlite3VdbeMe
93100 6d 53 65 74 49 6e 74 36 34 28 4d 65 6d 2a 2c 20  mSetInt64(Mem*, 
93110 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  i64);.SQLITE_PRI
93120 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
93130 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c  3VdbeMemSetDoubl
93140 65 28 4d 65 6d 2a 2c 20 64 6f 75 62 6c 65 29 3b  e(Mem*, double);
93150 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
93160 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
93170 4d 65 6d 53 65 74 4e 75 6c 6c 28 4d 65 6d 2a 29  MemSetNull(Mem*)
93180 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
93190 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
931a0 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 28  eMemSetZeroBlob(
931b0 4d 65 6d 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  Mem*,int);.SQLIT
931c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
931d0 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65  lite3VdbeMemMake
931e0 57 72 69 74 65 61 62 6c 65 28 4d 65 6d 2a 29 3b  Writeable(Mem*);
931f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
93200 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
93210 65 6d 44 79 6e 61 6d 69 63 69 66 79 28 4d 65 6d  emDynamicify(Mem
93220 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
93230 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
93240 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 4d  beMemStringify(M
93250 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  em*, int);.SQLIT
93260 45 5f 50 52 49 56 41 54 45 20 69 36 34 20 73 71  E_PRIVATE i64 sq
93270 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75  lite3VdbeIntValu
93280 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f  e(Mem*);.SQLITE_
93290 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
932a0 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65  te3VdbeMemIntege
932b0 72 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49  rify(Mem*);.SQLI
932c0 54 45 5f 50 52 49 56 41 54 45 20 64 6f 75 62 6c  TE_PRIVATE doubl
932d0 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61  e sqlite3VdbeRea
932e0 6c 56 61 6c 75 65 28 4d 65 6d 2a 29 3b 0a 53 51  lValue(Mem*);.SQ
932f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
93300 64 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74  d sqlite3VdbeInt
93310 65 67 65 72 41 66 66 69 6e 69 74 79 28 4d 65 6d  egerAffinity(Mem
93320 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
93330 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
93340 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 4d 65 6d  beMemRealify(Mem
93350 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
93360 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
93370 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d 65  beMemNumerify(Me
93380 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
93390 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
933a0 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28  dbeMemFromBtree(
933b0 42 74 43 75 72 73 6f 72 2a 2c 69 6e 74 2c 69 6e  BtCursor*,int,in
933c0 74 2c 69 6e 74 2c 4d 65 6d 2a 29 3b 0a 53 51 4c  t,int,Mem*);.SQL
933d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
933e0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
933f0 65 6c 65 61 73 65 28 4d 65 6d 20 2a 70 29 3b 0a  elease(Mem *p);.
93400 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
93410 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d  oid sqlite3VdbeM
93420 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61  emReleaseExterna
93430 6c 28 4d 65 6d 20 2a 70 29 3b 0a 53 51 4c 49 54  l(Mem *p);.SQLIT
93440 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
93450 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61  lite3VdbeMemFina
93460 6c 69 7a 65 28 4d 65 6d 2a 2c 20 46 75 6e 63 44  lize(Mem*, FuncD
93470 65 66 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ef*);.SQLITE_PRI
93480 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
93490 2a 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61  *sqlite3OpcodeNa
934a0 6d 65 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  me(int);.SQLITE_
934b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
934c0 74 65 33 56 64 62 65 4f 70 63 6f 64 65 48 61 73  te3VdbeOpcodeHas
934d0 50 72 6f 70 65 72 74 79 28 69 6e 74 2c 20 69 6e  Property(int, in
934e0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
934f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
93500 62 65 4d 65 6d 47 72 6f 77 28 4d 65 6d 20 2a 70  beMemGrow(Mem *p
93510 4d 65 6d 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20  Mem, int n, int 
93520 70 72 65 73 65 72 76 65 29 3b 0a 23 69 66 64 65  preserve);.#ifde
93530 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
93540 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e  MEMORY_MANAGEMEN
93550 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
93560 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
93570 52 65 6c 65 61 73 65 42 75 66 66 65 72 73 28 56  ReleaseBuffers(V
93580 64 62 65 20 2a 70 29 3b 0a 23 65 6e 64 69 66 0a  dbe *p);.#endif.
93590 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
935a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
935b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
935c0 65 4d 65 6d 53 61 6e 69 74 79 28 4d 65 6d 2a 29  eMemSanity(Mem*)
935d0 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f  ;.#endif.SQLITE_
935e0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
935f0 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c  te3VdbeMemTransl
93600 61 74 65 28 4d 65 6d 2a 2c 20 75 38 29 3b 0a 23  ate(Mem*, u8);.#
93610 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
93620 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  UG.SQLITE_PRIVAT
93630 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
93640 56 64 62 65 50 72 69 6e 74 53 71 6c 28 56 64 62  VdbePrintSql(Vdb
93650 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
93660 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
93670 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50  e3VdbeMemPrettyP
93680 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20  rint(Mem *pMem, 
93690 63 68 61 72 20 2a 7a 42 75 66 29 3b 0a 23 65 6e  char *zBuf);.#en
936a0 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  dif.SQLITE_PRIVA
936b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
936c0 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 4d  beMemHandleBom(M
936d0 65 6d 20 2a 70 4d 65 6d 29 3b 0a 53 51 4c 49 54  em *pMem);.SQLIT
936e0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
936f0 71 6c 69 74 65 33 56 64 62 65 46 69 66 6f 49 6e  qlite3VdbeFifoIn
93700 69 74 28 46 69 66 6f 2a 29 3b 0a 53 51 4c 49 54  it(Fifo*);.SQLIT
93710 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
93720 6c 69 74 65 33 56 64 62 65 46 69 66 6f 50 75 73  lite3VdbeFifoPus
93730 68 28 46 69 66 6f 2a 2c 20 69 36 34 29 3b 0a 53  h(Fifo*, i64);.S
93740 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
93750 74 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 66  t sqlite3VdbeFif
93760 6f 50 6f 70 28 46 69 66 6f 2a 2c 20 69 36 34 2a  oPop(Fifo*, i64*
93770 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
93780 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
93790 62 65 46 69 66 6f 43 6c 65 61 72 28 46 69 66 6f  beFifoClear(Fifo
937a0 2a 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  *);..#ifndef SQL
937b0 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f  ITE_OMIT_INCRBLO
937c0 42 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  B.SQLITE_PRIVATE
937d0 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64     int sqlite3Vd
937e0 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28  beMemExpandBlob(
937f0 4d 65 6d 20 2a 29 3b 0a 23 65 6c 73 65 0a 20 20  Mem *);.#else.  
93800 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56  #define sqlite3V
93810 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62  dbeMemExpandBlob
93820 28 78 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 65  (x) SQLITE_OK.#e
93830 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  ndif..#endif /* 
93840 21 64 65 66 69 6e 65 64 28 5f 56 44 42 45 49 4e  !defined(_VDBEIN
93850 54 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  T_H_) */../*****
93860 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
93870 20 76 64 62 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a   vdbeInt.h *****
93880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
938a0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
938b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
938c0 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
938d0 66 74 20 6f 66 66 20 69 6e 20 75 74 66 2e 63 20  ft off in utf.c 
938e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
938f0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ********/../*.**
93900 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
93910 6f 6e 73 74 61 6e 74 20 76 61 6c 75 65 20 69 73  onstant value is
93920 20 75 73 65 64 20 62 79 20 74 68 65 20 53 51 4c   used by the SQL
93930 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 20 61 6e  ITE_BIGENDIAN an
93940 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4c 49 54 54  d.** SQLITE_LITT
93950 4c 45 45 4e 44 49 41 4e 20 6d 61 63 72 6f 73 2e  LEENDIAN macros.
93960 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
93970 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73 71 6c  TE const int sql
93980 69 74 65 33 6f 6e 65 20 3d 20 31 3b 0a 0a 2f 2a  ite3one = 1;../*
93990 0a 2a 2a 20 54 68 69 73 20 6c 6f 6f 6b 75 70 20  .** This lookup 
939a0 74 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f  table is used to
939b0 20 68 65 6c 70 20 64 65 63 6f 64 65 20 74 68 65   help decode the
939c0 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 0a 2a   first byte of.*
939d0 2a 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 20 55  * a multi-byte U
939e0 54 46 38 20 63 68 61 72 61 63 74 65 72 2e 0a 2a  TF8 character..*
939f0 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75  /.static const u
93a00 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c  nsigned char sql
93a10 69 74 65 33 55 74 66 54 72 61 6e 73 31 5b 5d 20  ite3UtfTrans1[] 
93a20 3d 20 7b 0a 20 20 30 78 30 30 2c 20 30 78 30 31  = {.  0x00, 0x01
93a30 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78  , 0x02, 0x03, 0x
93a40 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20  04, 0x05, 0x06, 
93a50 30 78 30 37 2c 0a 20 20 30 78 30 38 2c 20 30 78  0x07,.  0x08, 0x
93a60 30 39 2c 20 30 78 30 61 2c 20 30 78 30 62 2c 20  09, 0x0a, 0x0b, 
93a70 30 78 30 63 2c 20 30 78 30 64 2c 20 30 78 30 65  0x0c, 0x0d, 0x0e
93a80 2c 20 30 78 30 66 2c 0a 20 20 30 78 31 30 2c 20  , 0x0f,.  0x10, 
93a90 30 78 31 31 2c 20 30 78 31 32 2c 20 30 78 31 33  0x11, 0x12, 0x13
93aa0 2c 20 30 78 31 34 2c 20 30 78 31 35 2c 20 30 78  , 0x14, 0x15, 0x
93ab0 31 36 2c 20 30 78 31 37 2c 0a 20 20 30 78 31 38  16, 0x17,.  0x18
93ac0 2c 20 30 78 31 39 2c 20 30 78 31 61 2c 20 30 78  , 0x19, 0x1a, 0x
93ad0 31 62 2c 20 30 78 31 63 2c 20 30 78 31 64 2c 20  1b, 0x1c, 0x1d, 
93ae0 30 78 31 65 2c 20 30 78 31 66 2c 0a 20 20 30 78  0x1e, 0x1f,.  0x
93af0 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20  00, 0x01, 0x02, 
93b00 30 78 30 33 2c 20 30 78 30 34 2c 20 30 78 30 35  0x03, 0x04, 0x05
93b10 2c 20 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 20  , 0x06, 0x07,.  
93b20 30 78 30 38 2c 20 30 78 30 39 2c 20 30 78 30 61  0x08, 0x09, 0x0a
93b30 2c 20 30 78 30 62 2c 20 30 78 30 63 2c 20 30 78  , 0x0b, 0x0c, 0x
93b40 30 64 2c 20 30 78 30 65 2c 20 30 78 30 66 2c 0a  0d, 0x0e, 0x0f,.
93b50 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78    0x00, 0x01, 0x
93b60 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34 2c 20  02, 0x03, 0x04, 
93b70 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78 30 37  0x05, 0x06, 0x07
93b80 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20  ,.  0x00, 0x01, 
93b90 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 30  0x02, 0x03, 0x00
93ba0 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78  , 0x01, 0x00, 0x
93bb0 30 30 2c 0a 7d 3b 0a 0a 0a 23 64 65 66 69 6e 65  00,.};...#define
93bc0 20 57 52 49 54 45 5f 55 54 46 38 28 7a 4f 75 74   WRITE_UTF8(zOut
93bd0 2c 20 63 29 20 7b 20 20 20 20 20 20 20 20 20 20  , c) {          
93be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
93bf0 5c 0a 20 20 69 66 28 20 63 3c 30 78 30 30 30 38  \.  if( c<0x0008
93c00 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  0 ){            
93c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
93c20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
93c30 7a 4f 75 74 2b 2b 20 3d 20 28 63 26 30 78 46 46  zOut++ = (c&0xFF
93c40 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
93c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
93c60 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20    \.  }         
93c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
93c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
93c90 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 65             \.  e
93ca0 6c 73 65 20 69 66 28 20 63 3c 30 78 30 30 38 30  lse if( c<0x0080
93cb0 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  0 ){            
93cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
93cd0 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b      \.    *zOut+
93ce0 2b 20 3d 20 30 78 43 30 20 2b 20 28 28 63 3e 3e  + = 0xC0 + ((c>>
93cf0 36 29 26 30 78 31 46 29 3b 20 20 20 20 20 20 20  6)&0x1F);       
93d00 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
93d10 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38     *zOut++ = 0x8
93d20 30 20 2b 20 28 63 20 26 20 30 78 33 46 29 3b 20  0 + (c & 0x3F); 
93d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
93d40 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20        \.  }     
93d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
93d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
93d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
93d80 0a 20 20 65 6c 73 65 20 69 66 28 20 63 3c 30 78  .  else if( c<0x
93d90 31 30 30 30 30 20 29 7b 20 20 20 20 20 20 20 20  10000 ){        
93da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
93db0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a          \.    *z
93dc0 4f 75 74 2b 2b 20 3d 20 30 78 45 30 20 2b 20 28  Out++ = 0xE0 + (
93dd0 28 63 3e 3e 31 32 29 26 30 78 30 46 29 3b 20 20  (c>>12)&0x0F);  
93de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
93df0 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
93e00 20 30 78 38 30 20 2b 20 28 28 63 3e 3e 36 29 20   0x80 + ((c>>6) 
93e10 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20  & 0x3F);        
93e20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
93e30 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b  *zOut++ = 0x80 +
93e40 20 28 63 20 26 20 30 78 33 46 29 3b 20 20 20 20   (c & 0x3F);    
93e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
93e60 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20     \.  }else{   
93e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
93e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
93e90 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
93ea0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 46 30    *zOut++ = 0xF0
93eb0 20 2b 20 28 28 63 3e 3e 31 38 29 20 26 20 30 78   + ((c>>18) & 0x
93ec0 30 37 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  07);            
93ed0 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74       \.    *zOut
93ee0 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 28 63 3e  ++ = 0x80 + ((c>
93ef0 3e 31 32 29 20 26 20 30 78 33 46 29 3b 20 20 20  >12) & 0x3F);   
93f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
93f10 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78      *zOut++ = 0x
93f20 38 30 20 2b 20 28 28 63 3e 3e 36 29 20 26 20 30  80 + ((c>>6) & 0
93f30 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20  x3F);           
93f40 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f         \.    *zO
93f50 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 63  ut++ = 0x80 + (c
93f60 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20   & 0x3F);       
93f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
93f80 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 20  \.  }           
93f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
93fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
93fb0 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64           \.}..#d
93fc0 65 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46 31  efine WRITE_UTF1
93fd0 36 4c 45 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20  6LE(zOut, c) {  
93fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
93ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
94000 20 20 69 66 28 20 63 3c 3d 30 78 46 46 46 46 20    if( c<=0xFFFF 
94010 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ){              
94020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94040 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
94050 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20  (c&0x00FF);     
94060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94080 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
94090 3d 20 28 28 63 3e 3e 38 29 26 30 78 30 30 46 46  = ((c>>8)&0x00FF
940a0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
940b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
940c0 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20      \.  }else{  
940d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
940e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
940f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94100 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75        \.    *zOu
94110 74 2b 2b 20 3d 20 28 28 28 63 3e 3e 31 30 29 26  t++ = (((c>>10)&
94120 30 78 30 30 33 46 29 20 2b 20 28 28 28 63 2d 30  0x003F) + (((c-0
94130 78 31 30 30 30 30 29 3e 3e 31 30 29 26 30 78 30  x10000)>>10)&0x0
94140 30 43 30 29 29 3b 20 20 5c 0a 20 20 20 20 2a 7a  0C0));  \.    *z
94150 4f 75 74 2b 2b 20 3d 20 28 30 78 30 30 44 38 20  Out++ = (0x00D8 
94160 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 29 3e  + (((c-0x10000)>
94170 3e 31 38 29 26 30 78 30 33 29 29 3b 20 20 20 20  >18)&0x03));    
94180 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
94190 2a 7a 4f 75 74 2b 2b 20 3d 20 28 63 26 30 78 30  *zOut++ = (c&0x0
941a0 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 20  0FF);           
941b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
941c0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
941d0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 30 78 30    *zOut++ = (0x0
941e0 30 44 43 20 2b 20 28 28 63 3e 3e 38 29 26 30 78  0DC + ((c>>8)&0x
941f0 30 33 29 29 3b 20 20 20 20 20 20 20 20 20 20 20  03));           
94200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
94210 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20    }             
94220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94250 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 57 52 49  \.}..#define WRI
94260 54 45 5f 55 54 46 31 36 42 45 28 7a 4f 75 74 2c  TE_UTF16BE(zOut,
94270 20 63 29 20 7b 20 20 20 20 20 20 20 20 20 20 20   c) {           
94280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94290 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c 3d       \.  if( c<=
942a0 30 78 46 46 46 46 20 29 7b 20 20 20 20 20 20 20  0xFFFF ){       
942b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
942c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
942d0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f         \.    *zO
942e0 75 74 2b 2b 20 3d 20 28 28 63 3e 3e 38 29 26 30  ut++ = ((c>>8)&0
942f0 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20  x00FF);         
94300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94310 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
94320 7a 4f 75 74 2b 2b 20 3d 20 28 63 26 30 78 30 30  zOut++ = (c&0x00
94330 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  FF);            
94340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94350 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d             \.  }
94360 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20  else{           
94370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94390 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
943a0 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 30 78     *zOut++ = (0x
943b0 30 30 44 38 20 2b 20 28 28 28 63 2d 30 78 31 30  00D8 + (((c-0x10
943c0 30 30 30 29 3e 3e 31 38 29 26 30 78 30 33 29 29  000)>>18)&0x03))
943d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c  ;              \
943e0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28  .    *zOut++ = (
943f0 28 28 63 3e 3e 31 30 29 26 30 78 30 30 33 46 29  ((c>>10)&0x003F)
94400 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 29   + (((c-0x10000)
94410 3e 3e 31 30 29 26 30 78 30 30 43 30 29 29 3b 20  >>10)&0x00C0)); 
94420 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
94430 20 28 30 78 30 30 44 43 20 2b 20 28 28 63 3e 3e   (0x00DC + ((c>>
94440 38 29 26 30 78 30 33 29 29 3b 20 20 20 20 20 20  8)&0x03));      
94450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94460 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
94470 20 3d 20 28 63 26 30 78 30 30 46 46 29 3b 20 20   = (c&0x00FF);  
94480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
944a0 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20       \.  }      
944b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
944c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
944d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
944e0 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66         \.}..#def
944f0 69 6e 65 20 52 45 41 44 5f 55 54 46 31 36 4c 45  ine READ_UTF16LE
94500 28 7a 49 6e 2c 20 63 29 7b 20 20 20 20 20 20 20  (zIn, c){       
94510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94530 20 20 5c 0a 20 20 63 20 3d 20 28 2a 7a 49 6e 2b    \.  c = (*zIn+
94540 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  +);             
94550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94570 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20            \.  c 
94580 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29  += ((*zIn++)<<8)
94590 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
945a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
945b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
945c0 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 44    \.  if( c>=0xD
945d0 38 30 30 20 26 26 20 63 3c 30 78 45 30 30 30 20  800 && c<0xE000 
945e0 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ){              
945f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94600 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69           \.    i
94610 6e 74 20 63 32 20 3d 20 28 2a 7a 49 6e 2b 2b 29  nt c2 = (*zIn++)
94620 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
94630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94650 20 5c 0a 20 20 20 20 63 32 20 2b 3d 20 28 28 2a   \.    c2 += ((*
94660 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20  zIn++)<<8);     
94670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94690 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63           \.    c
946a0 20 3d 20 28 63 32 26 30 78 30 33 46 46 29 20 2b   = (c2&0x03FF) +
946b0 20 28 28 63 26 30 78 30 30 33 46 29 3c 3c 31 30   ((c&0x003F)<<10
946c0 29 20 2b 20 28 28 28 63 26 30 78 30 33 43 30 29  ) + (((c&0x03C0)
946d0 2b 30 78 30 30 34 30 29 3c 3c 31 30 29 3b 20 20  +0x0040)<<10);  
946e0 20 5c 0a 20 20 20 20 69 66 28 20 28 63 20 26 20   \.    if( (c & 
946f0 30 78 46 46 46 46 30 30 30 30 29 3d 3d 30 20 29  0xFFFF0000)==0 )
94700 20 63 20 3d 20 30 78 46 46 46 44 3b 20 20 20 20   c = 0xFFFD;    
94710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94720 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20           \.  }  
94730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94770 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 52 45   \.}..#define RE
94780 41 44 5f 55 54 46 31 36 42 45 28 7a 49 6e 2c 20  AD_UTF16BE(zIn, 
94790 63 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  c){             
947a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
947b0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
947c0 63 20 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38  c = ((*zIn++)<<8
947d0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
947e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
947f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94800 20 20 20 20 5c 0a 20 20 63 20 2b 3d 20 28 2a 7a      \.  c += (*z
94810 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20  In++);          
94820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94840 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
94850 69 66 28 20 63 3e 3d 30 78 44 38 30 30 20 26 26  if( c>=0xD800 &&
94860 20 63 3c 30 78 45 30 30 30 20 29 7b 20 20 20 20   c<0xE000 ){    
94870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94890 20 20 20 5c 0a 20 20 20 20 69 6e 74 20 63 32 20     \.    int c2 
948a0 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b  = ((*zIn++)<<8);
948b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
948c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
948d0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
948e0 20 63 32 20 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b   c2 += (*zIn++);
948f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94920 20 20 20 5c 0a 20 20 20 20 63 20 3d 20 28 63 32     \.    c = (c2
94930 26 30 78 30 33 46 46 29 20 2b 20 28 28 63 26 30  &0x03FF) + ((c&0
94940 78 30 30 33 46 29 3c 3c 31 30 29 20 2b 20 28 28  x003F)<<10) + ((
94950 28 63 26 30 78 30 33 43 30 29 2b 30 78 30 30 34  (c&0x03C0)+0x004
94960 30 29 3c 3c 31 30 29 3b 20 20 20 5c 0a 20 20 20  0)<<10);   \.   
94970 20 69 66 28 20 28 63 20 26 20 30 78 46 46 46 46   if( (c & 0xFFFF
94980 30 30 30 30 29 3d 3d 30 20 29 20 63 20 3d 20 30  0000)==0 ) c = 0
94990 78 46 46 46 44 3b 20 20 20 20 20 20 20 20 20 20  xFFFD;          
949a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
949b0 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20     \.  }        
949c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
949d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
949e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
949f0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a             \.}..
94a00 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20  /*.** Translate 
94a10 61 20 73 69 6e 67 6c 65 20 55 54 46 2d 38 20 63  a single UTF-8 c
94a20 68 61 72 61 63 74 65 72 2e 20 20 52 65 74 75 72  haracter.  Retur
94a30 6e 20 74 68 65 20 75 6e 69 63 6f 64 65 20 76 61  n the unicode va
94a40 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 44 75 72 69 6e  lue..**.** Durin
94a50 67 20 74 72 61 6e 73 6c 61 74 69 6f 6e 2c 20 61  g translation, a
94a60 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 20 62  ssume that the b
94a70 79 74 65 20 74 68 61 74 20 7a 54 65 72 6d 20 70  yte that zTerm p
94a80 6f 69 6e 74 73 0a 2a 2a 20 69 73 20 61 20 30 78  oints.** is a 0x
94a90 30 30 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20  00..**.** Write 
94aa0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
94ab0 20 6e 65 78 74 20 75 6e 72 65 61 64 20 62 79 74   next unread byt
94ac0 65 20 62 61 63 6b 20 69 6e 74 6f 20 2a 70 7a 4e  e back into *pzN
94ad0 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73  ext..**.** Notes
94ae0 20 4f 6e 20 49 6e 76 61 6c 69 64 20 55 54 46 2d   On Invalid UTF-
94af0 38 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69  8:.**.**  *  Thi
94b00 73 20 72 6f 75 74 69 6e 65 20 6e 65 76 65 72 20  s routine never 
94b10 61 6c 6c 6f 77 73 20 61 20 37 2d 62 69 74 20 63  allows a 7-bit c
94b20 68 61 72 61 63 74 65 72 20 28 30 78 30 30 20 74  haracter (0x00 t
94b30 68 72 6f 75 67 68 20 30 78 37 66 29 20 74 6f 0a  hrough 0x7f) to.
94b40 2a 2a 20 20 20 20 20 62 65 20 65 6e 63 6f 64 65  **     be encode
94b50 64 20 61 73 20 61 20 6d 75 6c 74 69 2d 62 79 74  d as a multi-byt
94b60 65 20 63 68 61 72 61 63 74 65 72 2e 20 20 41 6e  e character.  An
94b70 79 20 6d 75 6c 74 69 2d 62 79 74 65 20 63 68 61  y multi-byte cha
94b80 72 61 63 74 65 72 20 74 68 61 74 0a 2a 2a 20 20  racter that.**  
94b90 20 20 20 61 74 74 65 6d 70 74 73 20 74 6f 20 65     attempts to e
94ba0 6e 63 6f 64 65 20 61 20 76 61 6c 75 65 20 62 65  ncode a value be
94bb0 74 77 65 65 6e 20 30 78 30 30 20 61 6e 64 20 30  tween 0x00 and 0
94bc0 78 37 66 20 69 73 20 72 65 6e 64 65 72 65 64 20  x7f is rendered 
94bd0 61 73 20 30 78 66 66 66 64 2e 0a 2a 2a 0a 2a 2a  as 0xfffd..**.**
94be0 20 20 2a 20 20 54 68 69 73 20 72 6f 75 74 69 6e    *  This routin
94bf0 65 20 6e 65 76 65 72 20 61 6c 6c 6f 77 73 20 61  e never allows a
94c00 20 55 54 46 31 36 20 73 75 72 72 6f 67 61 74 65   UTF16 surrogate
94c10 20 76 61 6c 75 65 20 74 6f 20 62 65 20 65 6e 63   value to be enc
94c20 6f 64 65 64 2e 0a 2a 2a 20 20 20 20 20 49 66 20  oded..**     If 
94c30 61 20 6d 75 6c 74 69 2d 62 79 74 65 20 63 68 61  a multi-byte cha
94c40 72 61 63 74 65 72 20 61 74 74 65 6d 70 74 73 20  racter attempts 
94c50 74 6f 20 65 6e 63 6f 64 65 20 61 20 76 61 6c 75  to encode a valu
94c60 65 20 62 65 74 77 65 65 6e 0a 2a 2a 20 20 20 20  e between.**    
94c70 20 30 78 64 38 30 30 20 61 6e 64 20 30 78 65 30   0xd800 and 0xe0
94c80 30 30 20 74 68 65 6e 20 69 74 20 69 73 20 72 65  00 then it is re
94c90 6e 64 65 72 65 64 20 61 73 20 30 78 66 66 66 64  ndered as 0xfffd
94ca0 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 42 79 74 65  ..**.**  *  Byte
94cb0 73 20 69 6e 20 74 68 65 20 72 61 6e 67 65 20 6f  s in the range o
94cc0 66 20 30 78 38 30 20 74 68 72 6f 75 67 68 20 30  f 0x80 through 0
94cd0 78 62 66 20 77 68 69 63 68 20 6f 63 63 75 72 20  xbf which occur 
94ce0 61 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20  as the first.** 
94cf0 20 20 20 20 62 79 74 65 20 6f 66 20 61 20 63 68      byte of a ch
94d00 61 72 61 63 74 65 72 20 61 72 65 20 69 6e 74 65  aracter are inte
94d10 72 70 72 65 74 65 64 20 61 73 20 73 69 6e 67 6c  rpreted as singl
94d20 65 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72  e-byte character
94d30 73 0a 2a 2a 20 20 20 20 20 61 6e 64 20 72 65 6e  s.**     and ren
94d40 64 65 72 65 64 20 61 73 20 74 68 65 6d 73 65 6c  dered as themsel
94d50 76 65 73 20 65 76 65 6e 20 74 68 6f 75 67 68 20  ves even though 
94d60 74 68 65 79 20 61 72 65 20 74 65 63 68 6e 69 63  they are technic
94d70 61 6c 6c 79 0a 2a 2a 20 20 20 20 20 69 6e 76 61  ally.**     inva
94d80 6c 69 64 20 63 68 61 72 61 63 74 65 72 73 2e 0a  lid characters..
94d90 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73 20 72  **.**  *  This r
94da0 6f 75 74 69 6e 65 20 61 63 63 65 70 74 73 20 61  outine accepts a
94db0 6e 20 69 6e 66 69 6e 69 74 65 20 6e 75 6d 62 65  n infinite numbe
94dc0 72 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 55  r of different U
94dd0 54 46 38 20 65 6e 63 6f 64 69 6e 67 73 0a 2a 2a  TF8 encodings.**
94de0 20 20 20 20 20 66 6f 72 20 75 6e 69 63 6f 64 65       for unicode
94df0 20 76 61 6c 75 65 73 20 30 78 38 30 20 61 6e 64   values 0x80 and
94e00 20 67 72 65 61 74 65 72 2e 20 20 49 74 20 64 6f   greater.  It do
94e10 20 6e 6f 74 20 63 68 61 6e 67 65 20 6f 76 65 72   not change over
94e20 2d 6c 65 6e 67 74 68 0a 2a 2a 20 20 20 20 20 65  -length.**     e
94e30 6e 63 6f 64 69 6e 67 73 20 74 6f 20 30 78 66 66  ncodings to 0xff
94e40 66 64 20 61 73 20 73 6f 6d 65 20 73 79 73 74 65  fd as some syste
94e50 6d 73 20 72 65 63 6f 6d 6d 65 6e 64 2e 0a 2a 2f  ms recommend..*/
94e60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
94e70 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 52  int sqlite3Utf8R
94e80 65 61 64 28 0a 20 20 63 6f 6e 73 74 20 75 6e 73  ead(.  const uns
94e90 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 2c 20 20  igned char *z,  
94ea0 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
94eb0 62 79 74 65 20 6f 66 20 55 54 46 2d 38 20 63 68  byte of UTF-8 ch
94ec0 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 63 6f 6e  aracter */.  con
94ed0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
94ee0 20 2a 7a 54 65 72 6d 2c 20 20 20 20 20 2f 2a 20   *zTerm,     /* 
94ef0 50 72 65 74 65 6e 64 20 74 68 69 73 20 62 79 74  Pretend this byt
94f00 65 20 69 73 20 30 78 30 30 20 2a 2f 0a 20 20 63  e is 0x00 */.  c
94f10 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
94f20 61 72 20 2a 2a 70 7a 4e 65 78 74 20 20 20 20 2f  ar **pzNext    /
94f30 2a 20 57 72 69 74 65 20 66 69 72 73 74 20 62 79  * Write first by
94f40 74 65 20 70 61 73 74 20 55 54 46 2d 38 20 63 68  te past UTF-8 ch
94f50 61 72 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  ar here */.){.  
94f60 69 6e 74 20 63 20 3d 20 2a 28 7a 2b 2b 29 3b 0a  int c = *(z++);.
94f70 20 20 69 66 28 20 63 3e 3d 30 78 63 30 20 29 7b    if( c>=0xc0 ){
94f80 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33  .    c = sqlite3
94f90 55 74 66 54 72 61 6e 73 31 5b 63 2d 30 78 63 30  UtfTrans1[c-0xc0
94fa0 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 7a 21  ];.    while( z!
94fb0 3d 7a 54 65 72 6d 20 26 26 20 28 2a 7a 20 26 20  =zTerm && (*z & 
94fc0 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b 0a 20  0xc0)==0x80 ){. 
94fd0 20 20 20 20 20 63 20 3d 20 28 63 3c 3c 36 29 20       c = (c<<6) 
94fe0 2b 20 28 30 78 33 66 20 26 20 2a 28 7a 2b 2b 29  + (0x3f & *(z++)
94ff0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
95000 20 63 3c 30 78 38 30 0a 20 20 20 20 20 20 20 20   c<0x80.        
95010 7c 7c 20 28 63 26 30 78 46 46 46 46 46 38 30 30  || (c&0xFFFFF800
95020 29 3d 3d 30 78 44 38 30 30 0a 20 20 20 20 20 20  )==0xD800.      
95030 20 20 7c 7c 20 28 63 26 30 78 46 46 46 46 46 46    || (c&0xFFFFFF
95040 46 45 29 3d 3d 30 78 46 46 46 45 20 29 7b 20 20  FE)==0xFFFE ){  
95050 63 20 3d 20 30 78 46 46 46 44 3b 20 7d 0a 20 20  c = 0xFFFD; }.  
95060 7d 0a 20 20 2a 70 7a 4e 65 78 74 20 3d 20 7a 3b  }.  *pzNext = z;
95070 0a 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a  .  return c;.}..
95080 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 54  ../*.** If the T
95090 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 20 6d  RANSLATE_TRACE m
950a0 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 2c  acro is defined,
950b0 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 65 61   the value of ea
950c0 63 68 20 4d 65 6d 20 69 73 0a 2a 2a 20 70 72 69  ch Mem is.** pri
950d0 6e 74 65 64 20 6f 6e 20 73 74 64 65 72 72 20 6f  nted on stderr o
950e0 6e 20 74 68 65 20 77 61 79 20 69 6e 74 6f 20 61  n the way into a
950f0 6e 64 20 6f 75 74 20 6f 66 20 73 71 6c 69 74 65  nd out of sqlite
95100 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74  3VdbeMemTranslat
95110 65 28 29 2e 0a 2a 2f 20 0a 2f 2a 20 23 64 65 66  e()..*/ ./* #def
95120 69 6e 65 20 54 52 41 4e 53 4c 41 54 45 5f 54 52  ine TRANSLATE_TR
95130 41 43 45 20 31 20 2a 2f 0a 0a 23 69 66 6e 64 65  ACE 1 */..#ifnde
95140 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
95150 46 31 36 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  F16./*.** This r
95160 6f 75 74 69 6e 65 20 74 72 61 6e 73 66 6f 72 6d  outine transform
95170 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 74  s the internal t
95180 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 65  ext encoding use
95190 64 20 62 79 20 70 4d 65 6d 20 74 6f 0a 2a 2a 20  d by pMem to.** 
951a0 64 65 73 69 72 65 64 45 6e 63 2e 20 49 74 20 69  desiredEnc. It i
951b0 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20 74 68  s an error if th
951c0 65 20 73 74 72 69 6e 67 20 69 73 20 61 6c 72 65  e string is alre
951d0 61 64 79 20 6f 66 20 74 68 65 20 64 65 73 69 72  ady of the desir
951e0 65 64 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 2c 20  ed.** encoding, 
951f0 6f 72 20 69 66 20 2a 70 4d 65 6d 20 64 6f 65 73  or if *pMem does
95200 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 73   not contain a s
95210 74 72 69 6e 67 20 76 61 6c 75 65 2e 0a 2a 2f 0a  tring value..*/.
95220 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
95230 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
95240 6d 54 72 61 6e 73 6c 61 74 65 28 4d 65 6d 20 2a  mTranslate(Mem *
95250 70 4d 65 6d 2c 20 75 38 20 64 65 73 69 72 65 64  pMem, u8 desired
95260 45 6e 63 29 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b  Enc){.  int len;
95270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
95280 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c      /* Maximum l
95290 65 6e 67 74 68 20 6f 66 20 6f 75 74 70 75 74 20  ength of output 
952a0 73 74 72 69 6e 67 20 69 6e 20 62 79 74 65 73 20  string in bytes 
952b0 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
952c0 61 72 20 2a 7a 4f 75 74 3b 20 20 20 20 20 20 20  ar *zOut;       
952d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75             /* Ou
952e0 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 20  tput buffer */. 
952f0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
95300 7a 49 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  zIn;            
95310 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20         /* Input 
95320 69 74 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75 6e  iterator */.  un
95330 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 54 65  signed char *zTe
95340 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rm;             
95350 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 69 6e      /* End of in
95360 70 75 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  put */.  unsigne
95370 64 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20  d char *z;      
95380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
95390 2a 20 4f 75 74 70 75 74 20 69 74 65 72 61 74 6f  * Output iterato
953a0 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  r */.  unsigned 
953b0 69 6e 74 20 63 3b 0a 0a 20 20 61 73 73 65 72 74  int c;..  assert
953c0 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
953d0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
953e0 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
953f0 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
95400 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45  ( pMem->flags&ME
95410 4d 5f 53 74 72 20 29 3b 0a 20 20 61 73 73 65 72  M_Str );.  asser
95420 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65  t( pMem->enc!=de
95430 73 69 72 65 64 45 6e 63 20 29 3b 0a 20 20 61 73  siredEnc );.  as
95440 73 65 72 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 21  sert( pMem->enc!
95450 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
95460 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a 0a 23  pMem->n>=0 );..#
95470 69 66 20 64 65 66 69 6e 65 64 28 54 52 41 4e 53  if defined(TRANS
95480 4c 41 54 45 5f 54 52 41 43 45 29 20 26 26 20 64  LATE_TRACE) && d
95490 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
954a0 42 55 47 29 0a 20 20 7b 0a 20 20 20 20 63 68 61  BUG).  {.    cha
954b0 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20  r zBuf[100];.   
954c0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50   sqlite3VdbeMemP
954d0 72 65 74 74 79 50 72 69 6e 74 28 70 4d 65 6d 2c  rettyPrint(pMem,
954e0 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 70 72 69   zBuf);.    fpri
954f0 6e 74 66 28 73 74 64 65 72 72 2c 20 22 49 4e 50  ntf(stderr, "INP
95500 55 54 3a 20 20 25 73 5c 6e 22 2c 20 7a 42 75 66  UT:  %s\n", zBuf
95510 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  );.  }.#endif.. 
95520 20 2f 2a 20 49 66 20 74 68 65 20 74 72 61 6e 73   /* If the trans
95530 6c 61 74 69 6f 6e 20 69 73 20 62 65 74 77 65 65  lation is betwee
95540 6e 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 20  n UTF-16 little 
95550 61 6e 64 20 62 69 67 20 65 6e 64 69 61 6e 2c 20  and big endian, 
95560 74 68 65 6e 20 0a 20 20 2a 2a 20 61 6c 6c 20 74  then .  ** all t
95570 68 61 74 20 69 73 20 72 65 71 75 69 72 65 64 20  hat is required 
95580 69 73 20 74 6f 20 73 77 61 70 20 74 68 65 20 62  is to swap the b
95590 79 74 65 20 6f 72 64 65 72 2e 20 54 68 69 73 20  yte order. This 
955a0 63 61 73 65 20 69 73 20 68 61 6e 64 6c 65 64 0a  case is handled.
955b0 20 20 2a 2a 20 64 69 66 66 65 72 65 6e 74 6c 79    ** differently
955c0 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 73   from the others
955d0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4d 65  ..  */.  if( pMe
955e0 6d 2d 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55  m->enc!=SQLITE_U
955f0 54 46 38 20 26 26 20 64 65 73 69 72 65 64 45 6e  TF8 && desiredEn
95600 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29  c!=SQLITE_UTF8 )
95610 7b 0a 20 20 20 20 75 38 20 74 65 6d 70 3b 0a 20  {.    u8 temp;. 
95620 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 72     int rc;.    r
95630 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
95640 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28  emMakeWriteable(
95650 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 72  pMem);.    if( r
95660 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
95670 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63        assert( rc
95680 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
95690 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
956a0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
956b0 20 7d 0a 20 20 20 20 7a 49 6e 20 3d 20 28 75 38   }.    zIn = (u8
956c0 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 7a  *)pMem->z;.    z
956d0 54 65 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d  Term = &zIn[pMem
956e0 2d 3e 6e 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28  ->n];.    while(
956f0 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20   zIn<zTerm ){.  
95700 20 20 20 20 74 65 6d 70 20 3d 20 2a 7a 49 6e 3b      temp = *zIn;
95710 0a 20 20 20 20 20 20 2a 7a 49 6e 20 3d 20 2a 28  .      *zIn = *(
95720 7a 49 6e 2b 31 29 3b 0a 20 20 20 20 20 20 7a 49  zIn+1);.      zI
95730 6e 2b 2b 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 2b  n++;.      *zIn+
95740 2b 20 3d 20 74 65 6d 70 3b 0a 20 20 20 20 7d 0a  + = temp;.    }.
95750 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20      pMem->enc = 
95760 64 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 20 20  desiredEnc;.    
95770 67 6f 74 6f 20 74 72 61 6e 73 6c 61 74 65 5f 6f  goto translate_o
95780 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65  ut;.  }..  /* Se
95790 74 20 6c 65 6e 20 74 6f 20 74 68 65 20 6d 61 78  t len to the max
957a0 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 62  imum number of b
957b0 79 74 65 73 20 72 65 71 75 69 72 65 64 20 69 6e  ytes required in
957c0 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66   the output buff
957d0 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20 64 65 73  er. */.  if( des
957e0 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f  iredEnc==SQLITE_
957f0 55 54 46 38 20 29 7b 0a 20 20 20 20 2f 2a 20 57  UTF8 ){.    /* W
95800 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20 66  hen converting f
95810 72 6f 6d 20 55 54 46 2d 31 36 2c 20 74 68 65 20  rom UTF-16, the 
95820 6d 61 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 72  maximum growth r
95830 65 73 75 6c 74 73 20 66 72 6f 6d 0a 20 20 20 20  esults from.    
95840 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6e 67 20 61  ** translating a
95850 20 32 2d 62 79 74 65 20 63 68 61 72 61 63 74 65   2-byte characte
95860 72 20 74 6f 20 61 20 34 2d 62 79 74 65 20 55 54  r to a 4-byte UT
95870 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e 0a 20  F-8 character.. 
95880 20 20 20 2a 2a 20 41 20 73 69 6e 67 6c 65 20 62     ** A single b
95890 79 74 65 20 69 73 20 72 65 71 75 69 72 65 64 20  yte is required 
958a0 66 6f 72 20 74 68 65 20 6f 75 74 70 75 74 20 73  for the output s
958b0 74 72 69 6e 67 0a 20 20 20 20 2a 2a 20 6e 75 6c  tring.    ** nul
958c0 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20  -terminator..   
958d0 20 2a 2f 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d   */.    len = pM
958e0 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 31 3b 0a 20  em->n * 2 + 1;. 
958f0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 57   }else{.    /* W
95900 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20 66  hen converting f
95910 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 55 54 46  rom UTF-8 to UTF
95920 2d 31 36 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  -16 the maximum 
95930 67 72 6f 77 74 68 20 69 73 20 63 61 75 73 65 64  growth is caused
95940 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 61 20 31  .    ** when a 1
95950 2d 62 79 74 65 20 55 54 46 2d 38 20 63 68 61 72  -byte UTF-8 char
95960 61 63 74 65 72 20 69 73 20 74 72 61 6e 73 6c 61  acter is transla
95970 74 65 64 20 69 6e 74 6f 20 61 20 32 2d 62 79 74  ted into a 2-byt
95980 65 20 55 54 46 2d 31 36 0a 20 20 20 20 2a 2a 20  e UTF-16.    ** 
95990 63 68 61 72 61 63 74 65 72 2e 20 54 77 6f 20 62  character. Two b
959a0 79 74 65 73 20 61 72 65 20 72 65 71 75 69 72 65  ytes are require
959b0 64 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74 20  d in the output 
959c0 62 75 66 66 65 72 20 66 6f 72 20 74 68 65 0a 20  buffer for the. 
959d0 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e     ** nul-termin
959e0 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ator..    */.   
959f0 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2a   len = pMem->n *
95a00 20 32 20 2b 20 32 3b 0a 20 20 7d 0a 0a 20 20 2f   2 + 2;.  }..  /
95a10 2a 20 53 65 74 20 7a 49 6e 20 74 6f 20 70 6f 69  * Set zIn to poi
95a20 6e 74 20 61 74 20 74 68 65 20 73 74 61 72 74 20  nt at the start 
95a30 6f 66 20 74 68 65 20 69 6e 70 75 74 20 62 75 66  of the input buf
95a40 66 65 72 20 61 6e 64 20 7a 54 65 72 6d 20 74 6f  fer and zTerm to
95a50 20 70 6f 69 6e 74 20 31 0a 20 20 2a 2a 20 62 79   point 1.  ** by
95a60 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 2e  te past the end.
95a70 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 72 69 61  .  **.  ** Varia
95a80 62 6c 65 20 7a 4f 75 74 20 69 73 20 73 65 74 20  ble zOut is set 
95a90 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20  to point at the 
95aa0 6f 75 74 70 75 74 20 62 75 66 66 65 72 2c 20 73  output buffer, s
95ab0 70 61 63 65 20 6f 62 74 61 69 6e 65 64 0a 20 20  pace obtained.  
95ac0 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  ** from sqlite3_
95ad0 6d 61 6c 6c 6f 63 28 29 2e 0a 20 20 2a 2f 0a 20  malloc()..  */. 
95ae0 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d 65 6d   zIn = (u8*)pMem
95af0 2d 3e 7a 3b 0a 20 20 7a 54 65 72 6d 20 3d 20 26  ->z;.  zTerm = &
95b00 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 5d 3b 0a 20 20  zIn[pMem->n];.  
95b10 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 44 62  zOut = sqlite3Db
95b20 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d 2d 3e  MallocRaw(pMem->
95b30 64 62 2c 20 6c 65 6e 29 3b 0a 20 20 69 66 28 20  db, len);.  if( 
95b40 21 7a 4f 75 74 20 29 7b 0a 20 20 20 20 72 65 74  !zOut ){.    ret
95b50 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
95b60 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 7a 4f 75 74  ;.  }.  z = zOut
95b70 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65  ;..  if( pMem->e
95b80 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  nc==SQLITE_UTF8 
95b90 29 7b 0a 20 20 20 20 69 66 28 20 64 65 73 69 72  ){.    if( desir
95ba0 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  edEnc==SQLITE_UT
95bb0 46 31 36 4c 45 20 29 7b 0a 20 20 20 20 20 20 2f  F16LE ){.      /
95bc0 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54 46 2d 31  * UTF-8 -> UTF-1
95bd0 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20  6 Little-endian 
95be0 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  */.      while( 
95bf0 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20  zIn<zTerm ){.   
95c00 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33       c = sqlite3
95c10 55 74 66 38 52 65 61 64 28 7a 49 6e 2c 20 7a 54  Utf8Read(zIn, zT
95c20 65 72 6d 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a  erm, (const u8**
95c30 29 26 7a 49 6e 29 3b 0a 20 20 20 20 20 20 20 20  )&zIn);.        
95c40 57 52 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c  WRITE_UTF16LE(z,
95c50 20 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   c);.      }.   
95c60 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73   }else{.      as
95c70 73 65 72 74 28 20 64 65 73 69 72 65 64 45 6e 63  sert( desiredEnc
95c80 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  ==SQLITE_UTF16BE
95c90 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 55 54 46   );.      /* UTF
95ca0 2d 38 20 2d 3e 20 55 54 46 2d 31 36 20 42 69 67  -8 -> UTF-16 Big
95cb0 2d 65 6e 64 69 61 6e 20 2a 2f 0a 20 20 20 20 20  -endian */.     
95cc0 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72   while( zIn<zTer
95cd0 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 20 3d  m ){.        c =
95ce0 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
95cf0 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f  (zIn, zTerm, (co
95d00 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 0a  nst u8**)&zIn);.
95d10 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54          WRITE_UT
95d20 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20 20 20  F16BE(z, c);.   
95d30 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70     }.    }.    p
95d40 4d 65 6d 2d 3e 6e 20 3d 20 7a 20 2d 20 7a 4f 75  Mem->n = z - zOu
95d50 74 3b 0a 20 20 20 20 2a 7a 2b 2b 20 3d 20 30 3b  t;.    *z++ = 0;
95d60 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
95d70 73 65 72 74 28 20 64 65 73 69 72 65 64 45 6e 63  sert( desiredEnc
95d80 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 3b  ==SQLITE_UTF8 );
95d90 0a 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65  .    if( pMem->e
95da0 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  nc==SQLITE_UTF16
95db0 4c 45 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55  LE ){.      /* U
95dc0 54 46 2d 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64  TF-16 Little-end
95dd0 69 61 6e 20 2d 3e 20 55 54 46 2d 38 20 2a 2f 0a  ian -> UTF-8 */.
95de0 20 20 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e        while( zIn
95df0 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20  <zTerm ){.      
95e00 20 20 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a    READ_UTF16LE(z
95e10 49 6e 2c 20 63 29 3b 20 0a 20 20 20 20 20 20 20  In, c); .       
95e20 20 57 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 63   WRITE_UTF8(z, c
95e30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
95e40 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 55  else{.      /* U
95e50 54 46 2d 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64  TF-16 Little-end
95e60 69 61 6e 20 2d 3e 20 55 54 46 2d 38 20 2a 2f 0a  ian -> UTF-8 */.
95e70 20 20 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e        while( zIn
95e80 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20  <zTerm ){.      
95e90 20 20 52 45 41 44 5f 55 54 46 31 36 42 45 28 7a    READ_UTF16BE(z
95ea0 49 6e 2c 20 63 29 3b 20 0a 20 20 20 20 20 20 20  In, c); .       
95eb0 20 57 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 63   WRITE_UTF8(z, c
95ec0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
95ed0 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 7a  .    pMem->n = z
95ee0 20 2d 20 7a 4f 75 74 3b 0a 20 20 7d 0a 20 20 2a   - zOut;.  }.  *
95ef0 7a 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28  z = 0;.  assert(
95f00 20 28 70 4d 65 6d 2d 3e 6e 2b 28 64 65 73 69 72   (pMem->n+(desir
95f10 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  edEnc==SQLITE_UT
95f20 46 38 3f 31 3a 32 29 29 3c 3d 6c 65 6e 20 29 3b  F8?1:2))<=len );
95f30 0a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ..  sqlite3VdbeM
95f40 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b  emRelease(pMem);
95f50 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  .  pMem->flags &
95f60 3d 20 7e 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d  = ~(MEM_Static|M
95f70 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d  EM_Dyn|MEM_Ephem
95f80 29 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d  );.  pMem->enc =
95f90 20 64 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 70   desiredEnc;.  p
95fa0 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 4d  Mem->flags |= (M
95fb0 45 4d 5f 54 65 72 6d 7c 4d 45 4d 5f 44 79 6e 29  EM_Term|MEM_Dyn)
95fc0 3b 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63  ;.  pMem->z = (c
95fd0 68 61 72 2a 29 7a 4f 75 74 3b 0a 20 20 70 4d 65  har*)zOut;.  pMe
95fe0 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65  m->zMalloc = pMe
95ff0 6d 2d 3e 7a 3b 0a 0a 74 72 61 6e 73 6c 61 74 65  m->z;..translate
96000 5f 6f 75 74 3a 0a 23 69 66 20 64 65 66 69 6e 65  _out:.#if define
96010 64 28 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43  d(TRANSLATE_TRAC
96020 45 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51  E) && defined(SQ
96030 4c 49 54 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a  LITE_DEBUG).  {.
96040 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30      char zBuf[10
96050 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0];.    sqlite3V
96060 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e  dbeMemPrettyPrin
96070 74 28 70 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20  t(pMem, zBuf);. 
96080 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72     fprintf(stder
96090 72 2c 20 22 4f 55 54 50 55 54 3a 20 25 73 5c 6e  r, "OUTPUT: %s\n
960a0 22 2c 20 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65  ", zBuf);.  }.#e
960b0 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51  ndif.  return SQ
960c0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
960d0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
960e0 68 65 63 6b 73 20 66 6f 72 20 61 20 62 79 74 65  hecks for a byte
960f0 2d 6f 72 64 65 72 20 6d 61 72 6b 20 61 74 20 74  -order mark at t
96100 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
96110 74 68 65 20 0a 2a 2a 20 55 54 46 2d 31 36 20 73  the .** UTF-16 s
96120 74 72 69 6e 67 20 73 74 6f 72 65 64 20 69 6e 20  tring stored in 
96130 2a 70 4d 65 6d 2e 20 49 66 20 6f 6e 65 20 69 73  *pMem. If one is
96140 20 70 72 65 73 65 6e 74 2c 20 69 74 20 69 73 20   present, it is 
96150 72 65 6d 6f 76 65 64 20 61 6e 64 0a 2a 2a 20 74  removed and.** t
96160 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74  he encoding of t
96170 68 65 20 4d 65 6d 20 61 64 6a 75 73 74 65 64 2e  he Mem adjusted.
96180 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f   This routine do
96190 65 73 20 6e 6f 74 20 64 6f 20 61 6e 79 0a 2a 2a  es not do any.**
961a0 20 62 79 74 65 2d 73 77 61 70 70 69 6e 67 2c 20   byte-swapping, 
961b0 69 74 20 6a 75 73 74 20 73 65 74 73 20 4d 65 6d  it just sets Mem
961c0 2e 65 6e 63 20 61 70 70 72 6f 70 72 69 61 74 65  .enc appropriate
961d0 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c  ly..**.** The al
961e0 6c 6f 63 61 74 69 6f 6e 20 28 73 74 61 74 69 63  location (static
961f0 2c 20 64 79 6e 61 6d 69 63 20 65 74 63 2e 29 20  , dynamic etc.) 
96200 61 6e 64 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20  and encoding of 
96210 74 68 65 20 4d 65 6d 20 6d 61 79 20 62 65 0a 2a  the Mem may be.*
96220 2a 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69  * changed by thi
96230 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53  s function..*/.S
96240 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
96250 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
96260 48 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70  HandleBom(Mem *p
96270 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  Mem){.  int rc =
96280 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38   SQLITE_OK;.  u8
96290 20 62 6f 6d 20 3d 20 30 3b 0a 0a 20 20 69 66 28   bom = 0;..  if(
962a0 20 70 4d 65 6d 2d 3e 6e 3c 30 20 7c 7c 20 70 4d   pMem->n<0 || pM
962b0 65 6d 2d 3e 6e 3e 31 20 29 7b 0a 20 20 20 20 75  em->n>1 ){.    u
962c0 38 20 62 31 20 3d 20 2a 28 75 38 20 2a 29 70 4d  8 b1 = *(u8 *)pM
962d0 65 6d 2d 3e 7a 3b 0a 20 20 20 20 75 38 20 62 32  em->z;.    u8 b2
962e0 20 3d 20 2a 28 28 28 75 38 20 2a 29 70 4d 65 6d   = *(((u8 *)pMem
962f0 2d 3e 7a 29 20 2b 20 31 29 3b 0a 20 20 20 20 69  ->z) + 1);.    i
96300 66 28 20 62 31 3d 3d 30 78 46 45 20 26 26 20 62  f( b1==0xFE && b
96310 32 3d 3d 30 78 46 46 20 29 7b 0a 20 20 20 20 20  2==0xFF ){.     
96320 20 62 6f 6d 20 3d 20 53 51 4c 49 54 45 5f 55 54   bom = SQLITE_UT
96330 46 31 36 42 45 3b 0a 20 20 20 20 7d 0a 20 20 20  F16BE;.    }.   
96340 20 69 66 28 20 62 31 3d 3d 30 78 46 46 20 26 26   if( b1==0xFF &&
96350 20 62 32 3d 3d 30 78 46 45 20 29 7b 0a 20 20 20   b2==0xFE ){.   
96360 20 20 20 62 6f 6d 20 3d 20 53 51 4c 49 54 45 5f     bom = SQLITE_
96370 55 54 46 31 36 4c 45 3b 0a 20 20 20 20 7d 0a 20  UTF16LE;.    }. 
96380 20 7d 0a 20 20 0a 20 20 69 66 28 20 62 6f 6d 20   }.  .  if( bom 
96390 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
963a0 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72  te3VdbeMemMakeWr
963b0 69 74 65 61 62 6c 65 28 70 4d 65 6d 29 3b 0a 20  iteable(pMem);. 
963c0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
963d0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 4d  E_OK ){.      pM
963e0 65 6d 2d 3e 6e 20 2d 3d 20 32 3b 0a 20 20 20 20  em->n -= 2;.    
963f0 20 20 6d 65 6d 6d 6f 76 65 28 70 4d 65 6d 2d 3e    memmove(pMem->
96400 7a 2c 20 26 70 4d 65 6d 2d 3e 7a 5b 32 5d 2c 20  z, &pMem->z[2], 
96410 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 20 20  pMem->n);.      
96420 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d  pMem->z[pMem->n]
96430 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 70   = '\0';.      p
96440 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31  Mem->z[pMem->n+1
96450 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20  ] = '\0';.      
96460 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d  pMem->flags |= M
96470 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 70  EM_Term;.      p
96480 4d 65 6d 2d 3e 65 6e 63 20 3d 20 62 6f 6d 3b 0a  Mem->enc = bom;.
96490 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
964a0 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20  rn rc;.}.#endif 
964b0 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  /* SQLITE_OMIT_U
964c0 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 70  TF16 */../*.** p
964d0 5a 20 69 73 20 61 20 55 54 46 2d 38 20 65 6e 63  Z is a UTF-8 enc
964e0 6f 64 65 64 20 75 6e 69 63 6f 64 65 20 73 74 72  oded unicode str
964f0 69 6e 67 2e 20 49 66 20 6e 42 79 74 65 20 69 73  ing. If nByte is
96500 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c   less than zero,
96510 0a 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 6e  .** return the n
96520 75 6d 62 65 72 20 6f 66 20 75 6e 69 63 6f 64 65  umber of unicode
96530 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 70   characters in p
96540 5a 20 75 70 20 74 6f 20 28 62 75 74 20 6e 6f 74  Z up to (but not
96550 20 69 6e 63 6c 75 64 69 6e 67 29 0a 2a 2a 20 74   including).** t
96560 68 65 20 66 69 72 73 74 20 30 78 30 30 20 62 79  he first 0x00 by
96570 74 65 2e 20 49 66 20 6e 42 79 74 65 20 69 73 20  te. If nByte is 
96580 6e 6f 74 20 6c 65 73 73 20 74 68 61 6e 20 7a 65  not less than ze
96590 72 6f 2c 20 72 65 74 75 72 6e 20 74 68 65 0a 2a  ro, return the.*
965a0 2a 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 63  * number of unic
965b0 6f 64 65 20 63 68 61 72 61 63 74 65 72 73 20 69  ode characters i
965c0 6e 20 74 68 65 20 66 69 72 73 74 20 6e 42 79 74  n the first nByt
965d0 65 20 6f 66 20 70 5a 20 28 6f 72 20 75 70 20 74  e of pZ (or up t
965e0 6f 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20  o .** the first 
965f0 30 78 30 30 2c 20 77 68 69 63 68 65 76 65 72 20  0x00, whichever 
96600 63 6f 6d 65 73 20 66 69 72 73 74 29 2e 0a 2a 2f  comes first)..*/
96610 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
96620 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 43  int sqlite3Utf8C
96630 68 61 72 4c 65 6e 28 63 6f 6e 73 74 20 63 68 61  harLen(const cha
96640 72 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 42 79 74  r *zIn, int nByt
96650 65 29 7b 0a 20 20 69 6e 74 20 72 20 3d 20 30 3b  e){.  int r = 0;
96660 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 20 3d  .  const u8 *z =
96670 20 28 63 6f 6e 73 74 20 75 38 2a 29 7a 49 6e 3b   (const u8*)zIn;
96680 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 54 65  .  const u8 *zTe
96690 72 6d 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3e  rm;.  if( nByte>
966a0 3d 30 20 29 7b 0a 20 20 20 20 7a 54 65 72 6d 20  =0 ){.    zTerm 
966b0 3d 20 26 7a 5b 6e 42 79 74 65 5d 3b 0a 20 20 7d  = &z[nByte];.  }
966c0 65 6c 73 65 7b 0a 20 20 20 20 7a 54 65 72 6d 20  else{.    zTerm 
966d0 3d 20 28 63 6f 6e 73 74 20 75 38 2a 29 28 2d 31  = (const u8*)(-1
966e0 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  );.  }.  assert(
966f0 20 7a 3c 3d 7a 54 65 72 6d 20 29 3b 0a 20 20 77   z<=zTerm );.  w
96700 68 69 6c 65 28 20 2a 7a 21 3d 30 20 26 26 20 7a  hile( *z!=0 && z
96710 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 53 51  <zTerm ){.    SQ
96720 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a  LITE_SKIP_UTF8(z
96730 29 3b 0a 20 20 20 20 72 2b 2b 3b 0a 20 20 7d 0a  );.    r++;.  }.
96740 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a 0a 2f    return r;.}../
96750 2a 20 54 68 69 73 20 74 65 73 74 20 66 75 6e 63  * This test func
96760 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 75 72 72  tion is not curr
96770 65 6e 74 6c 79 20 75 73 65 64 20 62 79 20 74 68  ently used by th
96780 65 20 61 75 74 6f 6d 61 74 65 64 20 74 65 73 74  e automated test
96790 2d 73 75 69 74 65 2e 20 0a 2a 2a 20 48 65 6e 63  -suite. .** Henc
967a0 65 20 69 74 20 69 73 20 6f 6e 6c 79 20 61 76 61  e it is only ava
967b0 69 6c 61 62 6c 65 20 69 6e 20 64 65 62 75 67 20  ilable in debug 
967c0 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 64  builds..*/.#if d
967d0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45  efined(SQLITE_TE
967e0 53 54 29 20 26 26 20 64 65 66 69 6e 65 64 28 53  ST) && defined(S
967f0 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a  QLITE_DEBUG)./*.
96800 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20 55 54 46  ** Translate UTF
96810 2d 38 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2a 0a  -8 to UTF-8..**.
96820 2a 2a 20 54 68 69 73 20 68 61 73 20 74 68 65 20  ** This has the 
96830 65 66 66 65 63 74 20 6f 66 20 6d 61 6b 69 6e 67  effect of making
96840 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 73   sure that the s
96850 74 72 69 6e 67 20 69 73 20 77 65 6c 6c 2d 66 6f  tring is well-fo
96860 72 6d 65 64 0a 2a 2a 20 55 54 46 2d 38 2e 20 20  rmed.** UTF-8.  
96870 4d 69 73 63 6f 64 65 64 20 63 68 61 72 61 63 74  Miscoded charact
96880 65 72 73 20 61 72 65 20 72 65 6d 6f 76 65 64 2e  ers are removed.
96890 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 72 61 6e 73  .**.** The trans
968a0 6c 61 74 69 6f 6e 20 69 73 20 64 6f 6e 65 20 69  lation is done i
968b0 6e 2d 70 6c 61 63 65 20 28 73 69 6e 63 65 20 69  n-place (since i
968c0 74 20 69 73 20 69 6d 70 6f 73 73 69 62 6c 65 20  t is impossible 
968d0 66 6f 72 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65  for the.** corre
968e0 63 74 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e  ct UTF-8 encodin
968f0 67 20 74 6f 20 62 65 20 6c 6f 6e 67 65 72 20 74  g to be longer t
96900 68 61 6e 20 61 20 6d 61 6c 66 6f 72 6d 65 64 20  han a malformed 
96910 65 6e 63 6f 64 69 6e 67 29 2e 0a 2a 2f 0a 53 51  encoding)..*/.SQ
96920 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
96930 20 73 71 6c 69 74 65 33 55 74 66 38 54 6f 38 28   sqlite3Utf8To8(
96940 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
96950 49 6e 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  In){.  unsigned 
96960 63 68 61 72 20 2a 7a 4f 75 74 20 3d 20 7a 49 6e  char *zOut = zIn
96970 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ;.  unsigned cha
96980 72 20 2a 7a 53 74 61 72 74 20 3d 20 7a 49 6e 3b  r *zStart = zIn;
96990 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
969a0 20 2a 7a 54 65 72 6d 3b 0a 20 20 75 33 32 20 63   *zTerm;.  u32 c
969b0 3b 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e 5b  ;..  while( zIn[
969c0 30 5d 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 71  0] ){.    c = sq
969d0 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49  lite3Utf8Read(zI
969e0 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74  n, zTerm, (const
969f0 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 0a 20 20 20   u8**)&zIn);.   
96a00 20 69 66 28 20 63 21 3d 30 78 66 66 66 64 20 29   if( c!=0xfffd )
96a10 7b 0a 20 20 20 20 20 20 57 52 49 54 45 5f 55 54  {.      WRITE_UT
96a20 46 38 28 7a 4f 75 74 2c 20 63 29 3b 0a 20 20 20  F8(zOut, c);.   
96a30 20 7d 0a 20 20 7d 0a 20 20 2a 7a 4f 75 74 20 3d   }.  }.  *zOut =
96a40 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 7a 4f 75   0;.  return zOu
96a50 74 20 2d 20 7a 53 74 61 72 74 3b 0a 7d 0a 23 65  t - zStart;.}.#e
96a60 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
96a70 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
96a80 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20  /*.** Convert a 
96a90 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 69 6e  UTF-16 string in
96aa0 20 74 68 65 20 6e 61 74 69 76 65 20 65 6e 63 6f   the native enco
96ab0 64 69 6e 67 20 69 6e 74 6f 20 61 20 55 54 46 2d  ding into a UTF-
96ac0 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 4d 65 6d  8 string..** Mem
96ad0 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ory to hold the 
96ae0 55 54 46 2d 38 20 73 74 72 69 6e 67 20 69 73 20  UTF-8 string is 
96af0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
96b00 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 61 6e 64  lite3_malloc and
96b10 20 6d 75 73 74 0a 2a 2a 20 62 65 20 66 72 65 65   must.** be free
96b20 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67  d by the calling
96b30 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
96b40 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65   NULL is returne
96b50 64 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e  d if there is an
96b60 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f   allocation erro
96b70 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
96b80 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
96b90 65 33 55 74 66 31 36 74 6f 38 28 73 71 6c 69 74  e3Utf16to8(sqlit
96ba0 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 76 6f  e3 *db, const vo
96bb0 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 42 79 74 65  id *z, int nByte
96bc0 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d 65  ){.  Mem m;.  me
96bd0 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65  mset(&m, 0, size
96be0 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20 3d  of(m));.  m.db =
96bf0 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 64   db;.  sqlite3Vd
96c00 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c 20  beMemSetStr(&m, 
96c10 7a 2c 20 6e 42 79 74 65 2c 20 53 51 4c 49 54 45  z, nByte, SQLITE
96c20 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 53 51  _UTF16NATIVE, SQ
96c30 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
96c40 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
96c50 65 45 6e 63 6f 64 69 6e 67 28 26 6d 2c 20 53 51  eEncoding(&m, SQ
96c60 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 69 66  LITE_UTF8);.  if
96c70 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
96c80 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ed ){.    sqlite
96c90 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
96ca0 26 6d 29 3b 0a 20 20 20 20 6d 2e 7a 20 3d 20 30  &m);.    m.z = 0
96cb0 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
96cc0 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54  (m.flags & MEM_T
96cd0 65 72 6d 29 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d  erm)!=0 || db->m
96ce0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
96cf0 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c 61 67   assert( (m.flag
96d00 73 20 26 20 4d 45 4d 5f 53 74 72 29 21 3d 30 20  s & MEM_Str)!=0 
96d10 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
96d20 6c 65 64 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  led );.  return 
96d30 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44  (m.flags & MEM_D
96d40 79 6e 29 21 3d 30 20 3f 20 6d 2e 7a 20 3a 20 73  yn)!=0 ? m.z : s
96d50 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
96d60 62 2c 20 6d 2e 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  b, m.z);.}../*.*
96d70 2a 20 70 5a 20 69 73 20 61 20 55 54 46 2d 31 36  * pZ is a UTF-16
96d80 20 65 6e 63 6f 64 65 64 20 75 6e 69 63 6f 64 65   encoded unicode
96d90 20 73 74 72 69 6e 67 2e 20 49 66 20 6e 43 68 61   string. If nCha
96da0 72 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a  r is less than z
96db0 65 72 6f 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 74  ero,.** return t
96dc0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
96dd0 65 73 20 75 70 20 74 6f 20 28 62 75 74 20 6e 6f  es up to (but no
96de0 74 20 69 6e 63 6c 75 64 69 6e 67 29 2c 20 74 68  t including), th
96df0 65 20 66 69 72 73 74 20 70 61 69 72 0a 2a 2a 20  e first pair.** 
96e00 6f 66 20 63 6f 6e 73 65 63 75 74 69 76 65 20 30  of consecutive 0
96e10 78 30 30 20 62 79 74 65 73 20 69 6e 20 70 5a 2e  x00 bytes in pZ.
96e20 20 49 66 20 6e 43 68 61 72 20 69 73 20 6e 6f 74   If nChar is not
96e30 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c   less than zero,
96e40 0a 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e 20  .** then return 
96e50 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
96e60 74 65 73 20 69 6e 20 74 68 65 20 66 69 72 73 74  tes in the first
96e70 20 6e 43 68 61 72 20 75 6e 69 63 6f 64 65 20 63   nChar unicode c
96e80 68 61 72 61 63 74 65 72 73 0a 2a 2a 20 69 6e 20  haracters.** in 
96e90 70 5a 20 28 6f 72 20 75 70 20 75 6e 74 69 6c 20  pZ (or up until 
96ea0 74 68 65 20 66 69 72 73 74 20 70 61 69 72 20 6f  the first pair o
96eb0 66 20 30 78 30 30 20 62 79 74 65 73 2c 20 77 68  f 0x00 bytes, wh
96ec0 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20 66 69  ichever comes fi
96ed0 72 73 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rst)..*/.SQLITE_
96ee0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
96ef0 74 65 33 55 74 66 31 36 42 79 74 65 4c 65 6e 28  te3Utf16ByteLen(
96f00 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 49 6e 2c  const void *zIn,
96f10 20 69 6e 74 20 6e 43 68 61 72 29 7b 0a 20 20 75   int nChar){.  u
96f20 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 20 3d 20  nsigned int c = 
96f30 31 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20  1;.  char const 
96f40 2a 7a 20 3d 20 7a 49 6e 3b 0a 20 20 69 6e 74 20  *z = zIn;.  int 
96f50 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 53 51 4c  n = 0;.  if( SQL
96f60 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3d  ITE_UTF16NATIVE=
96f70 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20  =SQLITE_UTF16BE 
96f80 29 7b 0a 20 20 20 20 2f 2a 20 55 73 69 6e 67 20  ){.    /* Using 
96f90 61 6e 20 22 69 66 20 28 53 51 4c 49 54 45 5f 55  an "if (SQLITE_U
96fa0 54 46 31 36 4e 41 54 49 56 45 3d 3d 53 51 4c 49  TF16NATIVE==SQLI
96fb0 54 45 5f 55 54 46 31 36 42 45 29 22 20 63 6f 6e  TE_UTF16BE)" con
96fc0 73 74 72 75 63 74 20 68 65 72 65 0a 20 20 20 20  struct here.    
96fd0 2a 2a 20 61 6e 64 20 69 6e 20 6f 74 68 65 72 20  ** and in other 
96fe0 70 61 72 74 73 20 6f 66 20 74 68 69 73 20 66 69  parts of this fi
96ff0 6c 65 20 6d 65 61 6e 73 20 74 68 61 74 20 61 74  le means that at
97000 20 6f 6e 65 20 62 72 61 6e 63 68 20 77 69 6c 6c   one branch will
97010 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 62 65 20 63  .    ** not be c
97020 6f 76 65 72 65 64 20 62 79 20 63 6f 76 65 72 61  overed by covera
97030 67 65 20 74 65 73 74 69 6e 67 20 6f 6e 20 61 6e  ge testing on an
97040 79 20 73 69 6e 67 6c 65 20 68 6f 73 74 2e 20 42  y single host. B
97050 75 74 20 63 6f 76 65 72 61 67 65 0a 20 20 20 20  ut coverage.    
97060 2a 2a 20 77 69 6c 6c 20 62 65 20 63 6f 6d 70 6c  ** will be compl
97070 65 74 65 20 69 66 20 74 68 65 20 74 65 73 74 73  ete if the tests
97080 20 61 72 65 20 72 75 6e 20 6f 6e 20 62 6f 74 68   are run on both
97090 20 61 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e   a little-endian
970a0 20 61 6e 64 20 0a 20 20 20 20 2a 2a 20 62 69 67   and .    ** big
970b0 2d 65 6e 64 69 61 6e 20 68 6f 73 74 2e 20 42 65  -endian host. Be
970c0 63 61 75 73 65 20 62 6f 74 68 20 74 68 65 20 55  cause both the U
970d0 54 46 31 36 4e 41 54 49 56 45 20 61 6e 64 20 53  TF16NATIVE and S
970e0 51 4c 49 54 45 5f 55 54 46 31 36 42 45 0a 20 20  QLITE_UTF16BE.  
970f0 20 20 2a 2a 20 6d 61 63 72 6f 73 20 61 72 65 20    ** macros are 
97100 63 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f 6d 70  constant at comp
97110 69 6c 65 20 74 69 6d 65 20 74 68 65 20 63 6f 6d  ile time the com
97120 70 69 6c 65 72 20 63 61 6e 20 64 65 74 65 72 6d  piler can determ
97130 69 6e 65 0a 20 20 20 20 2a 2a 20 77 68 69 63 68  ine.    ** which
97140 20 62 72 61 6e 63 68 20 77 69 6c 6c 20 62 65 20   branch will be 
97150 66 6f 6c 6c 6f 77 65 64 2e 20 49 74 20 69 73 20  followed. It is 
97160 74 68 65 72 65 66 6f 72 65 20 61 73 73 75 6d 65  therefore assume
97170 64 20 74 68 61 74 20 6e 6f 20 72 75 6e 74 69 6d  d that no runtim
97180 65 0a 20 20 20 20 2a 2a 20 70 65 6e 61 6c 74 79  e.    ** penalty
97190 20 69 73 20 70 61 69 64 20 66 6f 72 20 74 68 69   is paid for thi
971a0 73 20 22 69 66 22 20 73 74 61 74 65 6d 65 6e 74  s "if" statement
971b0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68 69  ..    */.    whi
971c0 6c 65 28 20 63 20 26 26 20 28 28 6e 43 68 61 72  le( c && ((nChar
971d0 3c 30 29 20 7c 7c 20 6e 3c 6e 43 68 61 72 29 20  <0) || n<nChar) 
971e0 29 7b 0a 20 20 20 20 20 20 52 45 41 44 5f 55 54  ){.      READ_UT
971f0 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20 20 20  F16BE(z, c);.   
97200 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20     n++;.    }.  
97210 7d 65 6c 73 65 7b 0a 20 20 20 20 77 68 69 6c 65  }else{.    while
97220 28 20 63 20 26 26 20 28 28 6e 43 68 61 72 3c 30  ( c && ((nChar<0
97230 29 20 7c 7c 20 6e 3c 6e 43 68 61 72 29 20 29 7b  ) || n<nChar) ){
97240 0a 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31  .      READ_UTF1
97250 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20  6LE(z, c);.     
97260 20 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   n++;.    }.  }.
97270 20 20 72 65 74 75 72 6e 20 28 7a 2d 28 63 68 61    return (z-(cha
97280 72 20 63 6f 6e 73 74 20 2a 29 7a 49 6e 29 2d 28  r const *)zIn)-(
97290 28 63 3d 3d 30 29 3f 32 3a 30 29 3b 0a 7d 0a 0a  (c==0)?2:0);.}..
972a0 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
972b0 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 54  TE_TEST)./*.** T
972c0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
972d0 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 54  alled from the T
972e0 43 4c 20 74 65 73 74 20 66 75 6e 63 74 69 6f 6e  CL test function
972f0 20 22 74 72 61 6e 73 6c 61 74 65 5f 73 65 6c 66   "translate_self
97300 74 65 73 74 22 2e 0a 2a 2a 20 49 74 20 63 68 65  test"..** It che
97310 63 6b 73 20 74 68 61 74 20 74 68 65 20 70 72 69  cks that the pri
97320 6d 69 74 69 76 65 73 20 66 6f 72 20 73 65 72 69  mitives for seri
97330 61 6c 69 7a 69 6e 67 20 61 6e 64 20 64 65 73 65  alizing and dese
97340 72 69 61 6c 69 7a 69 6e 67 0a 2a 2a 20 63 68 61  rializing.** cha
97350 72 61 63 74 65 72 73 20 69 6e 20 65 61 63 68 20  racters in each 
97360 65 6e 63 6f 64 69 6e 67 20 61 72 65 20 69 6e 76  encoding are inv
97370 65 72 73 65 73 20 6f 66 20 65 61 63 68 20 6f 74  erses of each ot
97380 68 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  her..*/.SQLITE_P
97390 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
973a0 74 65 33 55 74 66 53 65 6c 66 54 65 73 74 28 29  te3UtfSelfTest()
973b0 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  {.  unsigned int
973c0 20 69 2c 20 74 3b 0a 20 20 75 6e 73 69 67 6e 65   i, t;.  unsigne
973d0 64 20 63 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b  d char zBuf[20];
973e0 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
973f0 20 2a 7a 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20   *z;.  unsigned 
97400 63 68 61 72 20 2a 7a 54 65 72 6d 3b 0a 20 20 69  char *zTerm;.  i
97410 6e 74 20 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64  nt n;.  unsigned
97420 20 69 6e 74 20 63 3b 0a 0a 20 20 66 6f 72 28 69   int c;..  for(i
97430 3d 30 3b 20 69 3c 30 78 30 30 31 31 30 30 30 30  =0; i<0x00110000
97440 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 7a 20 3d 20  ; i++){.    z = 
97450 7a 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f  zBuf;.    WRITE_
97460 55 54 46 38 28 7a 2c 20 69 29 3b 0a 20 20 20 20  UTF8(z, i);.    
97470 6e 20 3d 20 7a 2d 7a 42 75 66 3b 0a 20 20 20 20  n = z-zBuf;.    
97480 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 54  z[0] = 0;.    zT
97490 65 72 6d 20 3d 20 7a 3b 0a 20 20 20 20 7a 20 3d  erm = z;.    z =
974a0 20 7a 42 75 66 3b 0a 20 20 20 20 63 20 3d 20 73   zBuf;.    c = s
974b0 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
974c0 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 20  , zTerm, (const 
974d0 75 38 2a 2a 29 26 7a 29 3b 0a 20 20 20 20 74 20  u8**)&z);.    t 
974e0 3d 20 69 3b 0a 20 20 20 20 69 66 28 20 69 3e 3d  = i;.    if( i>=
974f0 30 78 44 38 30 30 20 26 26 20 69 3c 3d 30 78 44  0xD800 && i<=0xD
97500 46 46 46 20 29 20 74 20 3d 20 30 78 46 46 46 44  FFF ) t = 0xFFFD
97510 3b 0a 20 20 20 20 69 66 28 20 28 69 26 30 78 46  ;.    if( (i&0xF
97520 46 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46 45  FFFFFFE)==0xFFFE
97530 20 29 20 74 20 3d 20 30 78 46 46 46 44 3b 0a 20   ) t = 0xFFFD;. 
97540 20 20 20 61 73 73 65 72 74 28 20 63 3d 3d 74 20     assert( c==t 
97550 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  );.    assert( (
97560 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20  z-zBuf)==n );.  
97570 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 30  }.  for(i=0; i<0
97580 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b 29 7b  x00110000; i++){
97590 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78 44 38  .    if( i>=0xD8
975a0 30 30 20 26 26 20 69 3c 30 78 45 30 30 30 20 29  00 && i<0xE000 )
975b0 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7a   continue;.    z
975c0 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 52 49   = zBuf;.    WRI
975d0 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 69 29  TE_UTF16LE(z, i)
975e0 3b 0a 20 20 20 20 6e 20 3d 20 7a 2d 7a 42 75 66  ;.    n = z-zBuf
975f0 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a  ;.    z[0] = 0;.
97600 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20      z = zBuf;.  
97610 20 20 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a    READ_UTF16LE(z
97620 2c 20 63 29 3b 0a 20 20 20 20 61 73 73 65 72 74  , c);.    assert
97630 28 20 63 3d 3d 69 20 29 3b 0a 20 20 20 20 61 73  ( c==i );.    as
97640 73 65 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d 3d  sert( (z-zBuf)==
97650 6e 20 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  n );.  }.  for(i
97660 3d 30 3b 20 69 3c 30 78 30 30 31 31 30 30 30 30  =0; i<0x00110000
97670 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
97680 69 3e 3d 30 78 44 38 30 30 20 26 26 20 69 3c 30  i>=0xD800 && i<0
97690 78 45 30 30 30 20 29 20 63 6f 6e 74 69 6e 75 65  xE000 ) continue
976a0 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a  ;.    z = zBuf;.
976b0 20 20 20 20 57 52 49 54 45 5f 55 54 46 31 36 42      WRITE_UTF16B
976c0 45 28 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20 3d  E(z, i);.    n =
976d0 20 7a 2d 7a 42 75 66 3b 0a 20 20 20 20 7a 5b 30   z-zBuf;.    z[0
976e0 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a  ] = 0;.    z = z
976f0 42 75 66 3b 0a 20 20 20 20 52 45 41 44 5f 55 54  Buf;.    READ_UT
97700 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20 20 20  F16BE(z, c);.   
97710 20 61 73 73 65 72 74 28 20 63 3d 3d 69 20 29 3b   assert( c==i );
97720 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d  .    assert( (z-
97730 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a  zBuf)==n );.  }.
97740 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
97750 54 45 5f 54 45 53 54 20 2a 2f 0a 23 65 6e 64 69  TE_TEST */.#endi
97760 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
97770 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _UTF16 */../****
97780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
97790 66 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  f utf.c ********
977a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
977b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
977c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
977d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
977e0 20 66 69 6c 65 20 75 74 69 6c 2e 63 20 2a 2a 2a   file util.c ***
977f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
97820 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
97830 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
97840 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
97850 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
97860 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
97870 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
97880 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
97890 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
978a0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
978b0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
978c0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
978d0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
978e0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
978f0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
97900 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
97910 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
97920 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
97930 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
97940 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
97950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97990 2a 0a 2a 2a 20 55 74 69 6c 69 74 79 20 66 75 6e  *.** Utility fun
979a0 63 74 69 6f 6e 73 20 75 73 65 64 20 74 68 72 6f  ctions used thro
979b0 75 67 68 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a  ughout sqlite..*
979c0 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
979d0 6f 6e 74 61 69 6e 73 20 66 75 6e 63 74 69 6f 6e  ontains function
979e0 73 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6e 67  s for allocating
979f0 20 6d 65 6d 6f 72 79 2c 20 63 6f 6d 70 61 72 69   memory, compari
97a00 6e 67 0a 2a 2a 20 73 74 72 69 6e 67 73 2c 20 61  ng.** strings, a
97a10 6e 64 20 73 74 75 66 66 20 6c 69 6b 65 20 74 68  nd stuff like th
97a20 61 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 75  at..**.** $Id: u
97a30 74 69 6c 2e 63 2c 76 20 31 2e 32 32 39 20 32 30  til.c,v 1.229 20
97a40 30 38 2f 30 35 2f 31 33 20 31 36 3a 34 31 3a 35  08/05/13 16:41:5
97a50 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  0 drh Exp $.*/..
97a60 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  ./*.** Return tr
97a70 75 65 20 69 66 20 74 68 65 20 66 6c 6f 61 74 69  ue if the floati
97a80 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 69  ng point value i
97a90 73 20 4e 6f 74 20 61 20 4e 75 6d 62 65 72 2e 0a  s Not a Number..
97aa0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
97ab0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e  E int sqlite3IsN
97ac0 61 4e 28 64 6f 75 62 6c 65 20 78 29 7b 0a 20 20  aN(double x){.  
97ad0 2f 2a 20 54 68 69 73 20 4e 61 4e 20 74 65 73 74  /* This NaN test
97ae0 20 73 6f 6d 65 74 69 6d 65 73 20 66 61 69 6c 73   sometimes fails
97af0 20 69 66 20 63 6f 6d 70 69 6c 65 64 20 6f 6e 20   if compiled on 
97b00 47 43 43 20 77 69 74 68 20 2d 66 66 61 73 74 2d  GCC with -ffast-
97b10 6d 61 74 68 2e 0a 20 20 2a 2a 20 4f 6e 20 74 68  math..  ** On th
97b20 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 74 68  e other hand, th
97b30 65 20 75 73 65 20 6f 66 20 2d 66 66 61 73 74 2d  e use of -ffast-
97b40 6d 61 74 68 20 63 6f 6d 65 73 20 77 69 74 68 20  math comes with 
97b50 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20  the following.  
97b60 2a 2a 20 77 61 72 6e 69 6e 67 3a 0a 20 20 2a 2a  ** warning:.  **
97b70 0a 20 20 2a 2a 20 20 20 20 20 20 54 68 69 73 20  .  **      This 
97b80 6f 70 74 69 6f 6e 20 5b 2d 66 66 61 73 74 2d 6d  option [-ffast-m
97b90 61 74 68 5d 20 73 68 6f 75 6c 64 20 6e 65 76 65  ath] should neve
97ba0 72 20 62 65 20 74 75 72 6e 65 64 20 6f 6e 20 62  r be turned on b
97bb0 79 20 61 6e 79 0a 20 20 2a 2a 20 20 20 20 20 20  y any.  **      
97bc0 2d 4f 20 6f 70 74 69 6f 6e 20 73 69 6e 63 65 20  -O option since 
97bd0 69 74 20 63 61 6e 20 72 65 73 75 6c 74 20 69 6e  it can result in
97be0 20 69 6e 63 6f 72 72 65 63 74 20 6f 75 74 70 75   incorrect outpu
97bf0 74 20 66 6f 72 20 70 72 6f 67 72 61 6d 73 0a 20  t for programs. 
97c00 20 2a 2a 20 20 20 20 20 20 77 68 69 63 68 20 64   **      which d
97c10 65 70 65 6e 64 20 6f 6e 20 61 6e 20 65 78 61 63  epend on an exac
97c20 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  t implementation
97c30 20 6f 66 20 49 45 45 45 20 6f 72 20 49 53 4f 20   of IEEE or ISO 
97c40 0a 20 20 2a 2a 20 20 20 20 20 20 72 75 6c 65 73  .  **      rules
97c50 2f 73 70 65 63 69 66 69 63 61 74 69 6f 6e 73 20  /specifications 
97c60 66 6f 72 20 6d 61 74 68 20 66 75 6e 63 74 69 6f  for math functio
97c70 6e 73 2e 0a 20 20 2a 2f 0a 20 20 76 6f 6c 61 74  ns..  */.  volat
97c80 69 6c 65 20 64 6f 75 62 6c 65 20 79 20 3d 20 78  ile double y = x
97c90 3b 0a 20 20 72 65 74 75 72 6e 20 78 21 3d 79 3b  ;.  return x!=y;
97ca0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
97cb0 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72  e most recent er
97cc0 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 65 72 72  ror code and err
97cd0 6f 72 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68  or string for th
97ce0 65 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64  e sqlite.** hand
97cf0 6c 65 20 22 64 62 22 2e 20 54 68 65 20 65 72 72  le "db". The err
97d00 6f 72 20 63 6f 64 65 20 69 73 20 73 65 74 20 74  or code is set t
97d10 6f 20 22 65 72 72 5f 63 6f 64 65 22 2e 0a 2a 2a  o "err_code"..**
97d20 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e 6f 74  .** If it is not
97d30 20 4e 55 4c 4c 2c 20 73 74 72 69 6e 67 20 7a 46   NULL, string zF
97d40 6f 72 6d 61 74 20 73 70 65 63 69 66 69 65 73 20  ormat specifies 
97d50 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68  the format of th
97d60 65 0a 2a 2a 20 65 72 72 6f 72 20 73 74 72 69 6e  e.** error strin
97d70 67 20 69 6e 20 74 68 65 20 73 74 79 6c 65 20 6f  g in the style o
97d80 66 20 74 68 65 20 70 72 69 6e 74 66 20 66 75 6e  f the printf fun
97d90 63 74 69 6f 6e 73 3a 20 54 68 65 20 66 6f 6c 6c  ctions: The foll
97da0 6f 77 69 6e 67 0a 2a 2a 20 66 6f 72 6d 61 74 20  owing.** format 
97db0 63 68 61 72 61 63 74 65 72 73 20 61 72 65 20 61  characters are a
97dc0 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20  llowed:.**.**   
97dd0 20 20 20 25 73 20 20 20 20 20 20 49 6e 73 65 72     %s      Inser
97de0 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 20 20  t a string.**   
97df0 20 20 20 25 7a 20 20 20 20 20 20 41 20 73 74 72     %z      A str
97e00 69 6e 67 20 74 68 61 74 20 73 68 6f 75 6c 64 20  ing that should 
97e10 62 65 20 66 72 65 65 64 20 61 66 74 65 72 20 75  be freed after u
97e20 73 65 0a 2a 2a 20 20 20 20 20 20 25 64 20 20 20  se.**      %d   
97e30 20 20 20 49 6e 73 65 72 74 20 61 6e 20 69 6e 74     Insert an int
97e40 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 25 54 20  eger.**      %T 
97e50 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 74 6f       Insert a to
97e60 6b 65 6e 0a 2a 2a 20 20 20 20 20 20 25 53 20 20  ken.**      %S  
97e70 20 20 20 20 49 6e 73 65 72 74 20 74 68 65 20 66      Insert the f
97e80 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20  irst element of 
97e90 61 20 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20  a SrcList.**.** 
97ea0 7a 46 6f 72 6d 61 74 20 61 6e 64 20 61 6e 79 20  zFormat and any 
97eb0 73 74 72 69 6e 67 20 74 6f 6b 65 6e 73 20 74 68  string tokens th
97ec0 61 74 20 66 6f 6c 6c 6f 77 20 69 74 20 61 72 65  at follow it are
97ed0 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 0a 2a   assumed to be.*
97ee0 2a 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46  * encoded in UTF
97ef0 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 63 6c 65  -8..**.** To cle
97f00 61 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  ar the most rece
97f10 6e 74 20 65 72 72 6f 72 20 66 6f 72 20 73 71 6c  nt error for sql
97f20 69 74 65 20 68 61 6e 64 6c 65 20 22 64 62 22 2c  ite handle "db",
97f30 20 73 71 6c 69 74 65 33 45 72 72 6f 72 0a 2a 2a   sqlite3Error.**
97f40 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65   should be calle
97f50 64 20 77 69 74 68 20 65 72 72 5f 63 6f 64 65 20  d with err_code 
97f60 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b  set to SQLITE_OK
97f70 20 61 6e 64 20 7a 46 6f 72 6d 61 74 20 73 65 74   and zFormat set
97f80 0a 2a 2a 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a  .** to NULL..*/.
97f90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
97fa0 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72  oid sqlite3Error
97fb0 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
97fc0 74 20 65 72 72 5f 63 6f 64 65 2c 20 63 6f 6e 73  t err_code, cons
97fd0 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c  t char *zFormat,
97fe0 20 2e 2e 2e 29 7b 0a 20 20 69 66 28 20 64 62 20   ...){.  if( db 
97ff0 26 26 20 28 64 62 2d 3e 70 45 72 72 20 7c 7c 20  && (db->pErr || 
98000 28 64 62 2d 3e 70 45 72 72 20 3d 20 73 71 6c 69  (db->pErr = sqli
98010 74 65 33 56 61 6c 75 65 4e 65 77 28 64 62 29 29  te3ValueNew(db))
98020 21 3d 30 29 20 29 7b 0a 20 20 20 20 64 62 2d 3e  !=0) ){.    db->
98030 65 72 72 43 6f 64 65 20 3d 20 65 72 72 5f 63 6f  errCode = err_co
98040 64 65 3b 0a 20 20 20 20 69 66 28 20 7a 46 6f 72  de;.    if( zFor
98050 6d 61 74 20 29 7b 0a 20 20 20 20 20 20 63 68 61  mat ){.      cha
98060 72 20 2a 7a 3b 0a 20 20 20 20 20 20 76 61 5f 6c  r *z;.      va_l
98070 69 73 74 20 61 70 3b 0a 20 20 20 20 20 20 76 61  ist ap;.      va
98080 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d  _start(ap, zForm
98090 61 74 29 3b 0a 20 20 20 20 20 20 7a 20 3d 20 73  at);.      z = s
980a0 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64  qlite3VMPrintf(d
980b0 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b  b, zFormat, ap);
980c0 0a 20 20 20 20 20 20 76 61 5f 65 6e 64 28 61 70  .      va_end(ap
980d0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
980e0 56 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e  ValueSetStr(db->
980f0 70 45 72 72 2c 20 2d 31 2c 20 7a 2c 20 53 51 4c  pErr, -1, z, SQL
98100 49 54 45 5f 55 54 46 38 2c 20 73 71 6c 69 74 65  ITE_UTF8, sqlite
98110 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 7d 65 6c  3_free);.    }el
98120 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
98130 33 56 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d  3ValueSetStr(db-
98140 3e 70 45 72 72 2c 20 30 2c 20 30 2c 20 53 51 4c  >pErr, 0, 0, SQL
98150 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45  ITE_UTF8, SQLITE
98160 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d 0a  _STATIC);.    }.
98170 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64    }.}../*.** Add
98180 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
98190 65 20 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72  e to pParse->zEr
981a0 72 4d 73 67 20 61 6e 64 20 69 6e 63 72 65 6d 65  rMsg and increme
981b0 6e 74 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2e  nt pParse->nErr.
981c0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
981d0 67 20 66 6f 72 6d 61 74 74 69 6e 67 20 63 68 61  g formatting cha
981e0 72 61 63 74 65 72 73 20 61 72 65 20 61 6c 6c 6f  racters are allo
981f0 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  wed:.**.**      
98200 25 73 20 20 20 20 20 20 49 6e 73 65 72 74 20 61  %s      Insert a
98210 20 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20   string.**      
98220 25 7a 20 20 20 20 20 20 41 20 73 74 72 69 6e 67  %z      A string
98230 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20   that should be 
98240 66 72 65 65 64 20 61 66 74 65 72 20 75 73 65 0a  freed after use.
98250 2a 2a 20 20 20 20 20 20 25 64 20 20 20 20 20 20  **      %d      
98260 49 6e 73 65 72 74 20 61 6e 20 69 6e 74 65 67 65  Insert an intege
98270 72 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 20 20  r.**      %T    
98280 20 20 49 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e    Insert a token
98290 0a 2a 2a 20 20 20 20 20 20 25 53 20 20 20 20 20  .**      %S     
982a0 20 49 6e 73 65 72 74 20 74 68 65 20 66 69 72 73   Insert the firs
982b0 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53  t element of a S
982c0 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 54 68 69  rcList.**.** Thi
982d0 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c  s function shoul
982e0 64 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 70  d be used to rep
982f0 6f 72 74 20 61 6e 79 20 65 72 72 6f 72 20 74 68  ort any error th
98300 61 74 20 6f 63 63 75 72 73 20 77 68 69 6c 73 74  at occurs whilst
98310 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 61 6e  .** compiling an
98320 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 28   SQL statement (
98330 69 2e 65 2e 20 77 69 74 68 69 6e 20 73 71 6c 69  i.e. within sqli
98340 74 65 33 5f 70 72 65 70 61 72 65 28 29 29 2e 20  te3_prepare()). 
98350 54 68 65 0a 2a 2a 20 6c 61 73 74 20 74 68 69 6e  The.** last thin
98360 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 72  g the sqlite3_pr
98370 65 70 61 72 65 28 29 20 66 75 6e 63 74 69 6f 6e  epare() function
98380 20 64 6f 65 73 20 69 73 20 63 6f 70 79 20 74 68   does is copy th
98390 65 20 65 72 72 6f 72 0a 2a 2a 20 73 74 6f 72 65  e error.** store
983a0 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  d by this functi
983b0 6f 6e 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61  on into the data
983c0 62 61 73 65 20 68 61 6e 64 6c 65 20 75 73 69 6e  base handle usin
983d0 67 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29  g sqlite3Error()
983e0 2e 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 73 71  ..** Function sq
983f0 6c 69 74 65 33 45 72 72 6f 72 28 29 20 73 68 6f  lite3Error() sho
98400 75 6c 64 20 62 65 20 75 73 65 64 20 64 75 72 69  uld be used duri
98410 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 65 78 65  ng statement exe
98420 63 75 74 69 6f 6e 0a 2a 2a 20 28 73 71 6c 69 74  cution.** (sqlit
98430 65 33 5f 73 74 65 70 28 29 20 65 74 63 2e 29 2e  e3_step() etc.).
98440 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
98450 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
98460 72 72 6f 72 4d 73 67 28 50 61 72 73 65 20 2a 70  rrorMsg(Parse *p
98470 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61  Parse, const cha
98480 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29  r *zFormat, ...)
98490 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a  {.  va_list ap;.
984a0 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b    pParse->nErr++
984b0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
984c0 28 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67  (pParse->zErrMsg
984d0 29 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70  );.  va_start(ap
984e0 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 70 50  , zFormat);.  pP
984f0 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  arse->zErrMsg = 
98500 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28  sqlite3VMPrintf(
98510 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 46 6f 72  pParse->db, zFor
98520 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65  mat, ap);.  va_e
98530 6e 64 28 61 70 29 3b 0a 20 20 69 66 28 20 70 50  nd(ap);.  if( pP
98540 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45  arse->rc==SQLITE
98550 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 72 73  _OK ){.    pPars
98560 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45  e->rc = SQLITE_E
98570 52 52 4f 52 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  RROR;.  }.}../*.
98580 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 65 72 72  ** Clear the err
98590 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50  or message in pP
985a0 61 72 73 65 2c 20 69 66 20 61 6e 79 0a 2a 2f 0a  arse, if any.*/.
985b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
985c0 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72  oid sqlite3Error
985d0 43 6c 65 61 72 28 50 61 72 73 65 20 2a 70 50 61  Clear(Parse *pPa
985e0 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  rse){.  sqlite3_
985f0 66 72 65 65 28 70 50 61 72 73 65 2d 3e 7a 45 72  free(pParse->zEr
98600 72 4d 73 67 29 3b 0a 20 20 70 50 61 72 73 65 2d  rMsg);.  pParse-
98610 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  >zErrMsg = 0;.  
98620 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 3d 20 30  pParse->nErr = 0
98630 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  ;.}../*.** Conve
98640 72 74 20 61 6e 20 53 51 4c 2d 73 74 79 6c 65 20  rt an SQL-style 
98650 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 69 6e  quoted string in
98660 74 6f 20 61 20 6e 6f 72 6d 61 6c 20 73 74 72 69  to a normal stri
98670 6e 67 20 62 79 20 72 65 6d 6f 76 69 6e 67 0a 2a  ng by removing.*
98680 2a 20 74 68 65 20 71 75 6f 74 65 20 63 68 61 72  * the quote char
98690 61 63 74 65 72 73 2e 20 20 54 68 65 20 63 6f 6e  acters.  The con
986a0 76 65 72 73 69 6f 6e 20 69 73 20 64 6f 6e 65 20  version is done 
986b0 69 6e 2d 70 6c 61 63 65 2e 20 20 49 66 20 74 68  in-place.  If th
986c0 65 0a 2a 2a 20 69 6e 70 75 74 20 64 6f 65 73 20  e.** input does 
986d0 6e 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20 61  not begin with a
986e0 20 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72   quote character
986f0 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74  , then this rout
98700 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f  ine.** is a no-o
98710 70 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 32 2d 46 65  p..**.** 2002-Fe
98720 62 2d 31 34 3a 20 54 68 69 73 20 72 6f 75 74 69  b-14: This routi
98730 6e 65 20 69 73 20 65 78 74 65 6e 64 65 64 20 74  ne is extended t
98740 6f 20 72 65 6d 6f 76 65 20 4d 53 2d 41 63 63 65  o remove MS-Acce
98750 73 73 20 73 74 79 6c 65 0a 2a 2a 20 62 72 61 63  ss style.** brac
98760 6b 65 74 73 20 66 72 6f 6d 20 61 72 6f 75 6e 64  kets from around
98770 20 69 64 65 6e 74 69 66 65 72 73 2e 20 20 46 6f   identifers.  Fo
98780 72 20 65 78 61 6d 70 6c 65 3a 20 20 22 5b 61 2d  r example:  "[a-
98790 62 2d 63 5d 22 20 62 65 63 6f 6d 65 73 0a 2a 2a  b-c]" becomes.**
987a0 20 22 61 2d 62 2d 63 22 2e 0a 2a 2f 0a 53 51 4c   "a-b-c"..*/.SQL
987b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
987c0 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28   sqlite3Dequote(
987d0 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74 20  char *z){.  int 
987e0 71 75 6f 74 65 3b 0a 20 20 69 6e 74 20 69 2c 20  quote;.  int i, 
987f0 6a 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 20  j;.  if( z==0 ) 
98800 72 65 74 75 72 6e 3b 0a 20 20 71 75 6f 74 65 20  return;.  quote 
98810 3d 20 7a 5b 30 5d 3b 0a 20 20 73 77 69 74 63 68  = z[0];.  switch
98820 28 20 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 63  ( quote ){.    c
98830 61 73 65 20 27 5c 27 27 3a 20 20 62 72 65 61 6b  ase '\'':  break
98840 3b 0a 20 20 20 20 63 61 73 65 20 27 22 27 3a 20  ;.    case '"': 
98850 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
98860 65 20 27 60 27 3a 20 20 20 62 72 65 61 6b 3b 20  e '`':   break; 
98870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
98880 2a 20 46 6f 72 20 4d 79 53 51 4c 20 63 6f 6d 70  * For MySQL comp
98890 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20  atibility */.   
988a0 20 63 61 73 65 20 27 5b 27 3a 20 20 20 71 75 6f   case '[':   quo
988b0 74 65 20 3d 20 27 5d 27 3b 20 20 62 72 65 61 6b  te = ']';  break
988c0 3b 20 20 2f 2a 20 46 6f 72 20 4d 53 20 53 71 6c  ;  /* For MS Sql
988d0 53 65 72 76 65 72 20 63 6f 6d 70 61 74 69 62 69  Server compatibi
988e0 6c 69 74 79 20 2a 2f 0a 20 20 20 20 64 65 66 61  lity */.    defa
988f0 75 6c 74 3a 20 20 20 20 72 65 74 75 72 6e 3b 0a  ult:    return;.
98900 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 2c 20 6a    }.  for(i=1, j
98910 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29 7b 0a  =0; z[i]; i++){.
98920 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 71 75      if( z[i]==qu
98930 6f 74 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ote ){.      if(
98940 20 7a 5b 69 2b 31 5d 3d 3d 71 75 6f 74 65 20 29   z[i+1]==quote )
98950 7b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d  {.        z[j++]
98960 20 3d 20 71 75 6f 74 65 3b 0a 20 20 20 20 20 20   = quote;.      
98970 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c    i++;.      }el
98980 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b  se{.        z[j+
98990 2b 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  +] = 0;.        
989a0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
989b0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
989c0 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20  z[j++] = z[i];. 
989d0 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41     }.  }.}../* A
989e0 6e 20 61 72 72 61 79 20 74 6f 20 6d 61 70 20 61  n array to map a
989f0 6c 6c 20 75 70 70 65 72 2d 63 61 73 65 20 63 68  ll upper-case ch
98a00 61 72 61 63 74 65 72 73 20 69 6e 74 6f 20 74 68  aracters into th
98a10 65 69 72 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  eir correspondin
98a20 67 0a 2a 2a 20 6c 6f 77 65 72 2d 63 61 73 65 20  g.** lower-case 
98a30 63 68 61 72 61 63 74 65 72 2e 20 0a 2a 2f 0a 53  character. .*/.S
98a40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
98a50 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
98a60 72 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f  r sqlite3UpperTo
98a70 4c 6f 77 65 72 5b 5d 20 3d 20 7b 0a 23 69 66 64  Lower[] = {.#ifd
98a80 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a  ef SQLITE_ASCII.
98a90 20 20 20 20 20 20 30 2c 20 20 31 2c 20 20 32 2c        0,  1,  2,
98aa0 20 20 33 2c 20 20 34 2c 20 20 35 2c 20 20 36 2c    3,  4,  5,  6,
98ab0 20 20 37 2c 20 20 38 2c 20 20 39 2c 20 31 30 2c    7,  8,  9, 10,
98ac0 20 31 31 2c 20 31 32 2c 20 31 33 2c 20 31 34 2c   11, 12, 13, 14,
98ad0 20 31 35 2c 20 31 36 2c 20 31 37 2c 0a 20 20 20   15, 16, 17,.   
98ae0 20 20 31 38 2c 20 31 39 2c 20 32 30 2c 20 32 31    18, 19, 20, 21
98af0 2c 20 32 32 2c 20 32 33 2c 20 32 34 2c 20 32 35  , 22, 23, 24, 25
98b00 2c 20 32 36 2c 20 32 37 2c 20 32 38 2c 20 32 39  , 26, 27, 28, 29
98b10 2c 20 33 30 2c 20 33 31 2c 20 33 32 2c 20 33 33  , 30, 31, 32, 33
98b20 2c 20 33 34 2c 20 33 35 2c 0a 20 20 20 20 20 33  , 34, 35,.     3
98b30 36 2c 20 33 37 2c 20 33 38 2c 20 33 39 2c 20 34  6, 37, 38, 39, 4
98b40 30 2c 20 34 31 2c 20 34 32 2c 20 34 33 2c 20 34  0, 41, 42, 43, 4
98b50 34 2c 20 34 35 2c 20 34 36 2c 20 34 37 2c 20 34  4, 45, 46, 47, 4
98b60 38 2c 20 34 39 2c 20 35 30 2c 20 35 31 2c 20 35  8, 49, 50, 51, 5
98b70 32 2c 20 35 33 2c 0a 20 20 20 20 20 35 34 2c 20  2, 53,.     54, 
98b80 35 35 2c 20 35 36 2c 20 35 37 2c 20 35 38 2c 20  55, 56, 57, 58, 
98b90 35 39 2c 20 36 30 2c 20 36 31 2c 20 36 32 2c 20  59, 60, 61, 62, 
98ba0 36 33 2c 20 36 34 2c 20 39 37 2c 20 39 38 2c 20  63, 64, 97, 98, 
98bb0 39 39 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c 31  99,100,101,102,1
98bc0 30 33 2c 0a 20 20 20 20 31 30 34 2c 31 30 35 2c  03,.    104,105,
98bd0 31 30 36 2c 31 30 37 2c 31 30 38 2c 31 30 39 2c  106,107,108,109,
98be0 31 31 30 2c 31 31 31 2c 31 31 32 2c 31 31 33 2c  110,111,112,113,
98bf0 31 31 34 2c 31 31 35 2c 31 31 36 2c 31 31 37 2c  114,115,116,117,
98c00 31 31 38 2c 31 31 39 2c 31 32 30 2c 31 32 31 2c  118,119,120,121,
98c10 0a 20 20 20 20 31 32 32 2c 20 39 31 2c 20 39 32  .    122, 91, 92
98c20 2c 20 39 33 2c 20 39 34 2c 20 39 35 2c 20 39 36  , 93, 94, 95, 96
98c30 2c 20 39 37 2c 20 39 38 2c 20 39 39 2c 31 30 30  , 97, 98, 99,100
98c40 2c 31 30 31 2c 31 30 32 2c 31 30 33 2c 31 30 34  ,101,102,103,104
98c50 2c 31 30 35 2c 31 30 36 2c 31 30 37 2c 0a 20 20  ,105,106,107,.  
98c60 20 20 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31    108,109,110,11
98c70 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 31 31  1,112,113,114,11
98c80 35 2c 31 31 36 2c 31 31 37 2c 31 31 38 2c 31 31  5,116,117,118,11
98c90 39 2c 31 32 30 2c 31 32 31 2c 31 32 32 2c 31 32  9,120,121,122,12
98ca0 33 2c 31 32 34 2c 31 32 35 2c 0a 20 20 20 20 31  3,124,125,.    1
98cb0 32 36 2c 31 32 37 2c 31 32 38 2c 31 32 39 2c 31  26,127,128,129,1
98cc0 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c 31  30,131,132,133,1
98cd0 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c 31  34,135,136,137,1
98ce0 33 38 2c 31 33 39 2c 31 34 30 2c 31 34 31 2c 31  38,139,140,141,1
98cf0 34 32 2c 31 34 33 2c 0a 20 20 20 20 31 34 34 2c  42,143,.    144,
98d00 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c  145,146,147,148,
98d10 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c  149,150,151,152,
98d20 31 35 33 2c 31 35 34 2c 31 35 35 2c 31 35 36 2c  153,154,155,156,
98d30 31 35 37 2c 31 35 38 2c 31 35 39 2c 31 36 30 2c  157,158,159,160,
98d40 31 36 31 2c 0a 20 20 20 20 31 36 32 2c 31 36 33  161,.    162,163
98d50 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c 31 36 37  ,164,165,166,167
98d60 2c 31 36 38 2c 31 36 39 2c 31 37 30 2c 31 37 31  ,168,169,170,171
98d70 2c 31 37 32 2c 31 37 33 2c 31 37 34 2c 31 37 35  ,172,173,174,175
98d80 2c 31 37 36 2c 31 37 37 2c 31 37 38 2c 31 37 39  ,176,177,178,179
98d90 2c 0a 20 20 20 20 31 38 30 2c 31 38 31 2c 31 38  ,.    180,181,18
98da0 32 2c 31 38 33 2c 31 38 34 2c 31 38 35 2c 31 38  2,183,184,185,18
98db0 36 2c 31 38 37 2c 31 38 38 2c 31 38 39 2c 31 39  6,187,188,189,19
98dc0 30 2c 31 39 31 2c 31 39 32 2c 31 39 33 2c 31 39  0,191,192,193,19
98dd0 34 2c 31 39 35 2c 31 39 36 2c 31 39 37 2c 0a 20  4,195,196,197,. 
98de0 20 20 20 31 39 38 2c 31 39 39 2c 32 30 30 2c 32     198,199,200,2
98df0 30 31 2c 32 30 32 2c 32 30 33 2c 32 30 34 2c 32  01,202,203,204,2
98e00 30 35 2c 32 30 36 2c 32 30 37 2c 32 30 38 2c 32  05,206,207,208,2
98e10 30 39 2c 32 31 30 2c 32 31 31 2c 32 31 32 2c 32  09,210,211,212,2
98e20 31 33 2c 32 31 34 2c 32 31 35 2c 0a 20 20 20 20  13,214,215,.    
98e30 32 31 36 2c 32 31 37 2c 32 31 38 2c 32 31 39 2c  216,217,218,219,
98e40 32 32 30 2c 32 32 31 2c 32 32 32 2c 32 32 33 2c  220,221,222,223,
98e50 32 32 34 2c 32 32 35 2c 32 32 36 2c 32 32 37 2c  224,225,226,227,
98e60 32 32 38 2c 32 32 39 2c 32 33 30 2c 32 33 31 2c  228,229,230,231,
98e70 32 33 32 2c 32 33 33 2c 0a 20 20 20 20 32 33 34  232,233,.    234
98e80 2c 32 33 35 2c 32 33 36 2c 32 33 37 2c 32 33 38  ,235,236,237,238
98e90 2c 32 33 39 2c 32 34 30 2c 32 34 31 2c 32 34 32  ,239,240,241,242
98ea0 2c 32 34 33 2c 32 34 34 2c 32 34 35 2c 32 34 36  ,243,244,245,246
98eb0 2c 32 34 37 2c 32 34 38 2c 32 34 39 2c 32 35 30  ,247,248,249,250
98ec0 2c 32 35 31 2c 0a 20 20 20 20 32 35 32 2c 32 35  ,251,.    252,25
98ed0 33 2c 32 35 34 2c 32 35 35 0a 23 65 6e 64 69 66  3,254,255.#endif
98ee0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
98ef0 42 43 44 49 43 0a 20 20 20 20 20 20 30 2c 20 20  BCDIC.      0,  
98f00 31 2c 20 20 32 2c 20 20 33 2c 20 20 34 2c 20 20  1,  2,  3,  4,  
98f10 35 2c 20 20 36 2c 20 20 37 2c 20 20 38 2c 20 20  5,  6,  7,  8,  
98f20 39 2c 20 31 30 2c 20 31 31 2c 20 31 32 2c 20 31  9, 10, 11, 12, 1
98f30 33 2c 20 31 34 2c 20 31 35 2c 20 2f 2a 20 30 78  3, 14, 15, /* 0x
98f40 20 2a 2f 0a 20 20 20 20 20 31 36 2c 20 31 37 2c   */.     16, 17,
98f50 20 31 38 2c 20 31 39 2c 20 32 30 2c 20 32 31 2c   18, 19, 20, 21,
98f60 20 32 32 2c 20 32 33 2c 20 32 34 2c 20 32 35 2c   22, 23, 24, 25,
98f70 20 32 36 2c 20 32 37 2c 20 32 38 2c 20 32 39 2c   26, 27, 28, 29,
98f80 20 33 30 2c 20 33 31 2c 20 2f 2a 20 31 78 20 2a   30, 31, /* 1x *
98f90 2f 0a 20 20 20 20 20 33 32 2c 20 33 33 2c 20 33  /.     32, 33, 3
98fa0 34 2c 20 33 35 2c 20 33 36 2c 20 33 37 2c 20 33  4, 35, 36, 37, 3
98fb0 38 2c 20 33 39 2c 20 34 30 2c 20 34 31 2c 20 34  8, 39, 40, 41, 4
98fc0 32 2c 20 34 33 2c 20 34 34 2c 20 34 35 2c 20 34  2, 43, 44, 45, 4
98fd0 36 2c 20 34 37 2c 20 2f 2a 20 32 78 20 2a 2f 0a  6, 47, /* 2x */.
98fe0 20 20 20 20 20 34 38 2c 20 34 39 2c 20 35 30 2c       48, 49, 50,
98ff0 20 35 31 2c 20 35 32 2c 20 35 33 2c 20 35 34 2c   51, 52, 53, 54,
99000 20 35 35 2c 20 35 36 2c 20 35 37 2c 20 35 38 2c   55, 56, 57, 58,
99010 20 35 39 2c 20 36 30 2c 20 36 31 2c 20 36 32 2c   59, 60, 61, 62,
99020 20 36 33 2c 20 2f 2a 20 33 78 20 2a 2f 0a 20 20   63, /* 3x */.  
99030 20 20 20 36 34 2c 20 36 35 2c 20 36 36 2c 20 36     64, 65, 66, 6
99040 37 2c 20 36 38 2c 20 36 39 2c 20 37 30 2c 20 37  7, 68, 69, 70, 7
99050 31 2c 20 37 32 2c 20 37 33 2c 20 37 34 2c 20 37  1, 72, 73, 74, 7
99060 35 2c 20 37 36 2c 20 37 37 2c 20 37 38 2c 20 37  5, 76, 77, 78, 7
99070 39 2c 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20  9, /* 4x */.    
99080 20 38 30 2c 20 38 31 2c 20 38 32 2c 20 38 33 2c   80, 81, 82, 83,
99090 20 38 34 2c 20 38 35 2c 20 38 36 2c 20 38 37 2c   84, 85, 86, 87,
990a0 20 38 38 2c 20 38 39 2c 20 39 30 2c 20 39 31 2c   88, 89, 90, 91,
990b0 20 39 32 2c 20 39 33 2c 20 39 34 2c 20 39 35 2c   92, 93, 94, 95,
990c0 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20 20 39   /* 5x */.     9
990d0 36 2c 20 39 37 2c 20 36 36 2c 20 36 37 2c 20 36  6, 97, 66, 67, 6
990e0 38 2c 20 36 39 2c 20 37 30 2c 20 37 31 2c 20 37  8, 69, 70, 71, 7
990f0 32 2c 20 37 33 2c 31 30 36 2c 31 30 37 2c 31 30  2, 73,106,107,10
99100 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 20 2f  8,109,110,111, /
99110 2a 20 36 78 20 2a 2f 0a 20 20 20 20 31 31 32 2c  * 6x */.    112,
99120 20 38 31 2c 20 38 32 2c 20 38 33 2c 20 38 34 2c   81, 82, 83, 84,
99130 20 38 35 2c 20 38 36 2c 20 38 37 2c 20 38 38 2c   85, 86, 87, 88,
99140 20 38 39 2c 31 32 32 2c 31 32 33 2c 31 32 34 2c   89,122,123,124,
99150 31 32 35 2c 31 32 36 2c 31 32 37 2c 20 2f 2a 20  125,126,127, /* 
99160 37 78 20 2a 2f 0a 20 20 20 20 31 32 38 2c 31 32  7x */.    128,12
99170 39 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c 31 33  9,130,131,132,13
99180 33 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c 31 33  3,134,135,136,13
99190 37 2c 31 33 38 2c 31 33 39 2c 31 34 30 2c 31 34  7,138,139,140,14
991a0 31 2c 31 34 32 2c 31 34 33 2c 20 2f 2a 20 38 78  1,142,143, /* 8x
991b0 20 2a 2f 0a 20 20 20 20 31 34 34 2c 31 34 35 2c   */.    144,145,
991c0 31 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c  146,147,148,149,
991d0 31 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c  150,151,152,153,
991e0 31 35 34 2c 31 35 35 2c 31 35 36 2c 31 35 37 2c  154,155,156,157,
991f0 31 35 36 2c 31 35 39 2c 20 2f 2a 20 39 78 20 2a  156,159, /* 9x *
99200 2f 0a 20 20 20 20 31 36 30 2c 31 36 31 2c 31 36  /.    160,161,16
99210 32 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 31 36  2,163,164,165,16
99220 36 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 31 37  6,167,168,169,17
99230 30 2c 31 37 31 2c 31 34 30 2c 31 34 31 2c 31 34  0,171,140,141,14
99240 32 2c 31 37 35 2c 20 2f 2a 20 41 78 20 2a 2f 0a  2,175, /* Ax */.
99250 20 20 20 20 31 37 36 2c 31 37 37 2c 31 37 38 2c      176,177,178,
99260 31 37 39 2c 31 38 30 2c 31 38 31 2c 31 38 32 2c  179,180,181,182,
99270 31 38 33 2c 31 38 34 2c 31 38 35 2c 31 38 36 2c  183,184,185,186,
99280 31 38 37 2c 31 38 38 2c 31 38 39 2c 31 39 30 2c  187,188,189,190,
99290 31 39 31 2c 20 2f 2a 20 42 78 20 2a 2f 0a 20 20  191, /* Bx */.  
992a0 20 20 31 39 32 2c 31 32 39 2c 31 33 30 2c 31 33    192,129,130,13
992b0 31 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c 31 33  1,132,133,134,13
992c0 35 2c 31 33 36 2c 31 33 37 2c 32 30 32 2c 32 30  5,136,137,202,20
992d0 33 2c 32 30 34 2c 32 30 35 2c 32 30 36 2c 32 30  3,204,205,206,20
992e0 37 2c 20 2f 2a 20 43 78 20 2a 2f 0a 20 20 20 20  7, /* Cx */.    
992f0 32 30 38 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c  208,145,146,147,
99300 31 34 38 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c  148,149,150,151,
99310 31 35 32 2c 31 35 33 2c 32 31 38 2c 32 31 39 2c  152,153,218,219,
99320 32 32 30 2c 32 32 31 2c 32 32 32 2c 32 32 33 2c  220,221,222,223,
99330 20 2f 2a 20 44 78 20 2a 2f 0a 20 20 20 20 32 32   /* Dx */.    22
99340 34 2c 32 32 35 2c 31 36 32 2c 31 36 33 2c 31 36  4,225,162,163,16
99350 34 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 36  4,165,166,167,16
99360 38 2c 31 36 39 2c 32 33 32 2c 32 30 33 2c 32 30  8,169,232,203,20
99370 34 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c 20 2f  4,205,206,207, /
99380 2a 20 45 78 20 2a 2f 0a 20 20 20 20 32 33 39 2c  * Ex */.    239,
99390 32 34 30 2c 32 34 31 2c 32 34 32 2c 32 34 33 2c  240,241,242,243,
993a0 32 34 34 2c 32 34 35 2c 32 34 36 2c 32 34 37 2c  244,245,246,247,
993b0 32 34 38 2c 32 34 39 2c 32 31 39 2c 32 32 30 2c  248,249,219,220,
993c0 32 32 31 2c 32 32 32 2c 32 35 35 2c 20 2f 2a 20  221,222,255, /* 
993d0 46 78 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a  Fx */.#endif.};.
993e0 23 64 65 66 69 6e 65 20 55 70 70 65 72 54 6f 4c  #define UpperToL
993f0 6f 77 65 72 20 73 71 6c 69 74 65 33 55 70 70 65  ower sqlite3Uppe
99400 72 54 6f 4c 6f 77 65 72 0a 0a 2f 2a 0a 2a 2a 20  rToLower../*.** 
99410 53 6f 6d 65 20 73 79 73 74 65 6d 73 20 68 61 76  Some systems hav
99420 65 20 73 74 72 69 63 6d 70 28 29 2e 20 20 4f 74  e stricmp().  Ot
99430 68 65 72 73 20 68 61 76 65 20 73 74 72 63 61 73  hers have strcas
99440 65 63 6d 70 28 29 2e 20 20 42 65 63 61 75 73 65  ecmp().  Because
99450 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20  .** there is no 
99460 63 6f 6e 73 69 73 74 65 6e 63 79 2c 20 77 65 20  consistency, we 
99470 77 69 6c 6c 20 64 65 66 69 6e 65 20 6f 75 72 20  will define our 
99480 6f 77 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  own..*/.SQLITE_P
99490 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
994a0 65 33 53 74 72 49 43 6d 70 28 63 6f 6e 73 74 20  e3StrICmp(const 
994b0 63 68 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e  char *zLeft, con
994c0 73 74 20 63 68 61 72 20 2a 7a 52 69 67 68 74 29  st char *zRight)
994d0 7b 0a 20 20 72 65 67 69 73 74 65 72 20 75 6e 73  {.  register uns
994e0 69 67 6e 65 64 20 63 68 61 72 20 2a 61 2c 20 2a  igned char *a, *
994f0 62 3b 0a 20 20 61 20 3d 20 28 75 6e 73 69 67 6e  b;.  a = (unsign
99500 65 64 20 63 68 61 72 20 2a 29 7a 4c 65 66 74 3b  ed char *)zLeft;
99510 0a 20 20 62 20 3d 20 28 75 6e 73 69 67 6e 65 64  .  b = (unsigned
99520 20 63 68 61 72 20 2a 29 7a 52 69 67 68 74 3b 0a   char *)zRight;.
99530 20 20 77 68 69 6c 65 28 20 2a 61 21 3d 30 20 26    while( *a!=0 &
99540 26 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a  & UpperToLower[*
99550 61 5d 3d 3d 55 70 70 65 72 54 6f 4c 6f 77 65 72  a]==UpperToLower
99560 5b 2a 62 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b  [*b]){ a++; b++;
99570 20 7d 0a 20 20 72 65 74 75 72 6e 20 55 70 70 65   }.  return Uppe
99580 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20 55  rToLower[*a] - U
99590 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 3b  pperToLower[*b];
995a0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
995b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72  E int sqlite3Str
995c0 4e 49 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72  NICmp(const char
995d0 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63   *zLeft, const c
995e0 68 61 72 20 2a 7a 52 69 67 68 74 2c 20 69 6e 74  har *zRight, int
995f0 20 4e 29 7b 0a 20 20 72 65 67 69 73 74 65 72 20   N){.  register 
99600 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61  unsigned char *a
99610 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75 6e 73  , *b;.  a = (uns
99620 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 4c 65  igned char *)zLe
99630 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73 69 67  ft;.  b = (unsig
99640 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69 67 68  ned char *)zRigh
99650 74 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20  t;.  while( N-- 
99660 3e 20 30 20 26 26 20 2a 61 21 3d 30 20 26 26 20  > 0 && *a!=0 && 
99670 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d  UpperToLower[*a]
99680 3d 3d 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a  ==UpperToLower[*
99690 62 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b 20 7d  b]){ a++; b++; }
996a0 0a 20 20 72 65 74 75 72 6e 20 4e 3c 30 20 3f 20  .  return N<0 ? 
996b0 30 20 3a 20 55 70 70 65 72 54 6f 4c 6f 77 65 72  0 : UpperToLower
996c0 5b 2a 61 5d 20 2d 20 55 70 70 65 72 54 6f 4c 6f  [*a] - UpperToLo
996d0 77 65 72 5b 2a 62 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a  wer[*b];.}../*.*
996e0 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66  * Return TRUE if
996f0 20 7a 20 69 73 20 61 20 70 75 72 65 20 6e 75 6d   z is a pure num
99700 65 72 69 63 20 73 74 72 69 6e 67 2e 20 20 52 65  eric string.  Re
99710 74 75 72 6e 20 46 41 4c 53 45 20 69 66 20 74 68  turn FALSE if th
99720 65 0a 2a 2a 20 73 74 72 69 6e 67 20 63 6f 6e 74  e.** string cont
99730 61 69 6e 73 20 61 6e 79 20 63 68 61 72 61 63 74  ains any charact
99740 65 72 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20  er which is not 
99750 70 61 72 74 20 6f 66 20 61 20 6e 75 6d 62 65 72  part of a number
99760 2e 20 49 66 0a 2a 2a 20 74 68 65 20 73 74 72 69  . If.** the stri
99770 6e 67 20 69 73 20 6e 75 6d 65 72 69 63 20 61 6e  ng is numeric an
99780 64 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 27  d contains the '
99790 2e 27 20 63 68 61 72 61 63 74 65 72 2c 20 73 65  .' character, se
997a0 74 20 2a 72 65 61 6c 6e 75 6d 0a 2a 2a 20 74 6f  t *realnum.** to
997b0 20 54 52 55 45 20 28 6f 74 68 65 72 77 69 73 65   TRUE (otherwise
997c0 20 46 41 4c 53 45 29 2e 0a 2a 2a 0a 2a 2a 20 41   FALSE)..**.** A
997d0 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20 69  n empty string i
997e0 73 20 63 6f 6e 73 69 64 65 72 65 64 20 6e 6f 6e  s considered non
997f0 2d 6e 75 6d 65 72 69 63 2e 0a 2a 2f 0a 53 51 4c  -numeric..*/.SQL
99800 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
99810 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28  sqlite3IsNumber(
99820 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69  const char *z, i
99830 6e 74 20 2a 72 65 61 6c 6e 75 6d 2c 20 75 38 20  nt *realnum, u8 
99840 65 6e 63 29 7b 0a 20 20 69 6e 74 20 69 6e 63 72  enc){.  int incr
99850 20 3d 20 28 65 6e 63 3d 3d 53 51 4c 49 54 45 5f   = (enc==SQLITE_
99860 55 54 46 38 3f 31 3a 32 29 3b 0a 20 20 69 66 28  UTF8?1:2);.  if(
99870 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46   enc==SQLITE_UTF
99880 31 36 42 45 20 29 20 7a 2b 2b 3b 0a 20 20 69 66  16BE ) z++;.  if
99890 28 20 2a 7a 3d 3d 27 2d 27 20 7c 7c 20 2a 7a 3d  ( *z=='-' || *z=
998a0 3d 27 2b 27 20 29 20 7a 20 2b 3d 20 69 6e 63 72  ='+' ) z += incr
998b0 3b 0a 20 20 69 66 28 20 21 69 73 64 69 67 69 74  ;.  if( !isdigit
998c0 28 2a 28 75 38 2a 29 7a 29 20 29 7b 0a 20 20 20  (*(u8*)z) ){.   
998d0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
998e0 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 69 66   z += incr;.  if
998f0 28 20 72 65 61 6c 6e 75 6d 20 29 20 2a 72 65 61  ( realnum ) *rea
99900 6c 6e 75 6d 20 3d 20 30 3b 0a 20 20 77 68 69 6c  lnum = 0;.  whil
99910 65 28 20 69 73 64 69 67 69 74 28 2a 28 75 38 2a  e( isdigit(*(u8*
99920 29 7a 29 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72  )z) ){ z += incr
99930 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2e  ; }.  if( *z=='.
99940 27 20 29 7b 0a 20 20 20 20 7a 20 2b 3d 20 69 6e  ' ){.    z += in
99950 63 72 3b 0a 20 20 20 20 69 66 28 20 21 69 73 64  cr;.    if( !isd
99960 69 67 69 74 28 2a 28 75 38 2a 29 7a 29 20 29 20  igit(*(u8*)z) ) 
99970 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 77 68  return 0;.    wh
99980 69 6c 65 28 20 69 73 64 69 67 69 74 28 2a 28 75  ile( isdigit(*(u
99990 38 2a 29 7a 29 20 29 7b 20 7a 20 2b 3d 20 69 6e  8*)z) ){ z += in
999a0 63 72 3b 20 7d 0a 20 20 20 20 69 66 28 20 72 65  cr; }.    if( re
999b0 61 6c 6e 75 6d 20 29 20 2a 72 65 61 6c 6e 75 6d  alnum ) *realnum
999c0 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 1;.  }.  if( 
999d0 2a 7a 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27  *z=='e' || *z=='
999e0 45 27 20 29 7b 0a 20 20 20 20 7a 20 2b 3d 20 69  E' ){.    z += i
999f0 6e 63 72 3b 0a 20 20 20 20 69 66 28 20 2a 7a 3d  ncr;.    if( *z=
99a00 3d 27 2b 27 20 7c 7c 20 2a 7a 3d 3d 27 2d 27 20  ='+' || *z=='-' 
99a10 29 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20  ) z += incr;.   
99a20 20 69 66 28 20 21 69 73 64 69 67 69 74 28 2a 28   if( !isdigit(*(
99a30 75 38 2a 29 7a 29 20 29 20 72 65 74 75 72 6e 20  u8*)z) ) return 
99a40 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 73  0;.    while( is
99a50 64 69 67 69 74 28 2a 28 75 38 2a 29 7a 29 20 29  digit(*(u8*)z) )
99a60 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20  { z += incr; }. 
99a70 20 20 20 69 66 28 20 72 65 61 6c 6e 75 6d 20 29     if( realnum )
99a80 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20   *realnum = 1;. 
99a90 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a 3d 3d   }.  return *z==
99aa0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  0;.}../*.** The 
99ab0 73 74 72 69 6e 67 20 7a 5b 5d 20 69 73 20 61 6e  string z[] is an
99ac0 20 61 73 63 69 69 20 72 65 70 72 65 73 65 6e 74   ascii represent
99ad0 61 74 69 6f 6e 20 6f 66 20 61 20 72 65 61 6c 20  ation of a real 
99ae0 6e 75 6d 62 65 72 2e 0a 2a 2a 20 43 6f 6e 76 65  number..** Conve
99af0 72 74 20 74 68 69 73 20 73 74 72 69 6e 67 20 74  rt this string t
99b00 6f 20 61 20 64 6f 75 62 6c 65 2e 0a 2a 2a 0a 2a  o a double..**.*
99b10 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
99b20 73 73 75 6d 65 73 20 74 68 61 74 20 7a 5b 5d 20  ssumes that z[] 
99b30 72 65 61 6c 6c 79 20 69 73 20 61 20 76 61 6c 69  really is a vali
99b40 64 20 6e 75 6d 62 65 72 2e 20 20 49 66 20 69 74  d number.  If it
99b50 0a 2a 2a 20 69 73 20 6e 6f 74 2c 20 74 68 65 20  .** is not, the 
99b60 72 65 73 75 6c 74 20 69 73 20 75 6e 64 65 66 69  result is undefi
99b70 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ned..**.** This 
99b80 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
99b90 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 6c  instead of the l
99ba0 69 62 72 61 72 79 20 61 74 6f 66 28 29 20 66 75  ibrary atof() fu
99bb0 6e 63 74 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a  nction because.*
99bc0 2a 20 74 68 65 20 6c 69 62 72 61 72 79 20 61 74  * the library at
99bd0 6f 66 28 29 20 6d 69 67 68 74 20 77 61 6e 74 20  of() might want 
99be0 74 6f 20 75 73 65 20 22 2c 22 20 61 73 20 74 68  to use "," as th
99bf0 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20  e decimal point 
99c00 69 6e 73 74 65 61 64 0a 2a 2a 20 6f 66 20 22 2e  instead.** of ".
99c10 22 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 68  " depending on h
99c20 6f 77 20 6c 6f 63 61 6c 65 20 69 73 20 73 65 74  ow locale is set
99c30 2e 20 20 42 75 74 20 74 68 61 74 20 77 6f 75 6c  .  But that woul
99c40 64 20 63 61 75 73 65 20 70 72 6f 62 6c 65 6d 73  d cause problems
99c50 0a 2a 2a 20 66 6f 72 20 53 51 4c 2e 20 20 53 6f  .** for SQL.  So
99c60 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c   this routine al
99c70 77 61 79 73 20 75 73 65 73 20 22 2e 22 20 72 65  ways uses "." re
99c80 67 61 72 64 6c 65 73 73 20 6f 66 20 6c 6f 63 61  gardless of loca
99c90 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
99ca0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
99cb0 33 41 74 6f 46 28 63 6f 6e 73 74 20 63 68 61 72  3AtoF(const char
99cc0 20 2a 7a 2c 20 64 6f 75 62 6c 65 20 2a 70 52 65   *z, double *pRe
99cd0 73 75 6c 74 29 7b 0a 23 69 66 6e 64 65 66 20 53  sult){.#ifndef S
99ce0 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
99cf0 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 69 6e 74 20  ING_POINT.  int 
99d00 73 69 67 6e 20 3d 20 31 3b 0a 20 20 63 6f 6e 73  sign = 1;.  cons
99d10 74 20 63 68 61 72 20 2a 7a 42 65 67 69 6e 20 3d  t char *zBegin =
99d20 20 7a 3b 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c 45   z;.  LONGDOUBLE
99d30 5f 54 59 50 45 20 76 31 20 3d 20 30 2e 30 3b 0a  _TYPE v1 = 0.0;.
99d40 20 20 69 6e 74 20 6e 53 69 67 6e 69 66 69 63 61    int nSignifica
99d50 6e 74 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28  nt = 0;.  while(
99d60 20 69 73 73 70 61 63 65 28 2a 28 75 38 2a 29 7a   isspace(*(u8*)z
99d70 29 20 29 20 7a 2b 2b 3b 0a 20 20 69 66 28 20 2a  ) ) z++;.  if( *
99d80 7a 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 73 69  z=='-' ){.    si
99d90 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 7a 2b 2b  gn = -1;.    z++
99da0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a  ;.  }else if( *z
99db0 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 7a 2b 2b  =='+' ){.    z++
99dc0 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 7a  ;.  }.  while( z
99dd0 5b 30 5d 3d 3d 27 30 27 20 29 7b 0a 20 20 20 20  [0]=='0' ){.    
99de0 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65  z++;.  }.  while
99df0 28 20 69 73 64 69 67 69 74 28 2a 28 75 38 2a 29  ( isdigit(*(u8*)
99e00 7a 29 20 29 7b 0a 20 20 20 20 76 31 20 3d 20 76  z) ){.    v1 = v
99e10 31 2a 31 30 2e 30 20 2b 20 28 2a 7a 20 2d 20 27  1*10.0 + (*z - '
99e20 30 27 29 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20  0');.    z++;.  
99e30 20 20 6e 53 69 67 6e 69 66 69 63 61 6e 74 2b 2b    nSignificant++
99e40 3b 0a 20 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d  ;.  }.  if( *z==
99e50 27 2e 27 20 29 7b 0a 20 20 20 20 4c 4f 4e 47 44  '.' ){.    LONGD
99e60 4f 55 42 4c 45 5f 54 59 50 45 20 64 69 76 69 73  OUBLE_TYPE divis
99e70 6f 72 20 3d 20 31 2e 30 3b 0a 20 20 20 20 7a 2b  or = 1.0;.    z+
99e80 2b 3b 0a 20 20 20 20 69 66 28 20 6e 53 69 67 6e  +;.    if( nSign
99e90 69 66 69 63 61 6e 74 3d 3d 30 20 29 7b 0a 20 20  ificant==0 ){.  
99ea0 20 20 20 20 77 68 69 6c 65 28 20 7a 5b 30 5d 3d      while( z[0]=
99eb0 3d 27 30 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='0' ){.        
99ec0 64 69 76 69 73 6f 72 20 2a 3d 20 31 30 2e 30 3b  divisor *= 10.0;
99ed0 0a 20 20 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20  .        z++;.  
99ee0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
99ef0 77 68 69 6c 65 28 20 69 73 64 69 67 69 74 28 2a  while( isdigit(*
99f00 28 75 38 2a 29 7a 29 20 29 7b 0a 20 20 20 20 20  (u8*)z) ){.     
99f10 20 69 66 28 20 6e 53 69 67 6e 69 66 69 63 61 6e   if( nSignifican
99f20 74 3c 31 38 20 29 7b 0a 20 20 20 20 20 20 20 20  t<18 ){.        
99f30 76 31 20 3d 20 76 31 2a 31 30 2e 30 20 2b 20 28  v1 = v1*10.0 + (
99f40 2a 7a 20 2d 20 27 30 27 29 3b 0a 20 20 20 20 20  *z - '0');.     
99f50 20 20 20 64 69 76 69 73 6f 72 20 2a 3d 20 31 30     divisor *= 10
99f60 2e 30 3b 0a 20 20 20 20 20 20 20 20 6e 53 69 67  .0;.        nSig
99f70 6e 69 66 69 63 61 6e 74 2b 2b 3b 0a 20 20 20 20  nificant++;.    
99f80 20 20 7d 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20    }.      z++;. 
99f90 20 20 20 7d 0a 20 20 20 20 76 31 20 2f 3d 20 64     }.    v1 /= d
99fa0 69 76 69 73 6f 72 3b 0a 20 20 7d 0a 20 20 69 66  ivisor;.  }.  if
99fb0 28 20 2a 7a 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d  ( *z=='e' || *z=
99fc0 3d 27 45 27 20 29 7b 0a 20 20 20 20 69 6e 74 20  ='E' ){.    int 
99fd0 65 73 69 67 6e 20 3d 20 31 3b 0a 20 20 20 20 69  esign = 1;.    i
99fe0 6e 74 20 65 76 61 6c 20 3d 20 30 3b 0a 20 20 20  nt eval = 0;.   
99ff0 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45   LONGDOUBLE_TYPE
9a000 20 73 63 61 6c 65 20 3d 20 31 2e 30 3b 0a 20 20   scale = 1.0;.  
9a010 20 20 7a 2b 2b 3b 0a 20 20 20 20 69 66 28 20 2a    z++;.    if( *
9a020 7a 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20  z=='-' ){.      
9a030 65 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 20  esign = -1;.    
9a040 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65    z++;.    }else
9a050 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 29 7b 0a   if( *z=='+' ){.
9a060 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d        z++;.    }
9a070 0a 20 20 20 20 77 68 69 6c 65 28 20 69 73 64 69  .    while( isdi
9a080 67 69 74 28 2a 28 75 38 2a 29 7a 29 20 29 7b 0a  git(*(u8*)z) ){.
9a090 20 20 20 20 20 20 65 76 61 6c 20 3d 20 65 76 61        eval = eva
9a0a0 6c 2a 31 30 20 2b 20 2a 7a 20 2d 20 27 30 27 3b  l*10 + *z - '0';
9a0b0 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20  .      z++;.    
9a0c0 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 65 76 61  }.    while( eva
9a0d0 6c 3e 3d 36 34 20 29 7b 20 73 63 61 6c 65 20 2a  l>=64 ){ scale *
9a0e0 3d 20 31 2e 30 65 2b 36 34 3b 20 65 76 61 6c 20  = 1.0e+64; eval 
9a0f0 2d 3d 20 36 34 3b 20 7d 0a 20 20 20 20 77 68 69  -= 64; }.    whi
9a100 6c 65 28 20 65 76 61 6c 3e 3d 31 36 20 29 7b 20  le( eval>=16 ){ 
9a110 73 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 36  scale *= 1.0e+16
9a120 3b 20 65 76 61 6c 20 2d 3d 20 31 36 3b 20 7d 0a  ; eval -= 16; }.
9a130 20 20 20 20 77 68 69 6c 65 28 20 65 76 61 6c 3e      while( eval>
9a140 3d 34 20 29 7b 20 73 63 61 6c 65 20 2a 3d 20 31  =4 ){ scale *= 1
9a150 2e 30 65 2b 34 3b 20 65 76 61 6c 20 2d 3d 20 34  .0e+4; eval -= 4
9a160 3b 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 65  ; }.    while( e
9a170 76 61 6c 3e 3d 31 20 29 7b 20 73 63 61 6c 65 20  val>=1 ){ scale 
9a180 2a 3d 20 31 2e 30 65 2b 31 3b 20 65 76 61 6c 20  *= 1.0e+1; eval 
9a190 2d 3d 20 31 3b 20 7d 0a 20 20 20 20 69 66 28 20  -= 1; }.    if( 
9a1a0 65 73 69 67 6e 3c 30 20 29 7b 0a 20 20 20 20 20  esign<0 ){.     
9a1b0 20 76 31 20 2f 3d 20 73 63 61 6c 65 3b 0a 20 20   v1 /= scale;.  
9a1c0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 76    }else{.      v
9a1d0 31 20 2a 3d 20 73 63 61 6c 65 3b 0a 20 20 20 20  1 *= scale;.    
9a1e0 7d 0a 20 20 7d 0a 20 20 2a 70 52 65 73 75 6c 74  }.  }.  *pResult
9a1f0 20 3d 20 73 69 67 6e 3c 30 20 3f 20 2d 76 31 20   = sign<0 ? -v1 
9a200 3a 20 76 31 3b 0a 20 20 72 65 74 75 72 6e 20 7a  : v1;.  return z
9a210 20 2d 20 7a 42 65 67 69 6e 3b 0a 23 65 6c 73 65   - zBegin;.#else
9a220 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
9a230 33 41 74 6f 69 36 34 28 7a 2c 20 70 52 65 73 75  3Atoi64(z, pResu
9a240 6c 74 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53  lt);.#endif /* S
9a250 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
9a260 49 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 7d 0a 0a  ING_POINT */.}..
9a270 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68  /*.** Compare th
9a280 65 20 31 39 2d 63 68 61 72 61 63 74 65 72 20 73  e 19-character s
9a290 74 72 69 6e 67 20 7a 4e 75 6d 20 61 67 61 69 6e  tring zNum again
9a2a0 73 74 20 74 68 65 20 74 65 78 74 20 72 65 70 72  st the text repr
9a2b0 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 76 61  esentation.** va
9a2c0 6c 75 65 20 32 5e 36 33 3a 20 20 39 32 32 33 33  lue 2^63:  92233
9a2d0 37 32 30 33 36 38 35 34 37 37 35 38 30 38 2e 20  72036854775808. 
9a2e0 20 52 65 74 75 72 6e 20 6e 65 67 61 74 69 76 65   Return negative
9a2f0 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 6f 73 69 74  , zero, or posit
9a300 69 76 65 0a 2a 2a 20 69 66 20 7a 4e 75 6d 20 69  ive.** if zNum i
9a310 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75  s less than, equ
9a320 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65  al to, or greate
9a330 72 20 74 68 61 6e 20 74 68 65 20 73 74 72 69 6e  r than the strin
9a340 67 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 69 6b 65 20  g..**.** Unlike 
9a350 6d 65 6d 63 6d 70 28 29 20 74 68 69 73 20 72 6f  memcmp() this ro
9a360 75 74 69 6e 65 20 69 73 20 67 75 61 72 61 6e 74  utine is guarant
9a370 65 65 64 20 74 6f 20 72 65 74 75 72 6e 20 74 68  eed to return th
9a380 65 20 64 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20  e difference.** 
9a390 69 6e 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66  in the values of
9a3a0 20 74 68 65 20 6c 61 73 74 20 64 69 67 69 74 20   the last digit 
9a3b0 69 66 20 74 68 65 20 6f 6e 6c 79 20 64 69 66 66  if the only diff
9a3c0 65 72 65 6e 63 65 20 69 73 20 69 6e 20 74 68 65  erence is in the
9a3d0 0a 2a 2a 20 6c 61 73 74 20 64 69 67 69 74 2e 20  .** last digit. 
9a3e0 20 53 6f 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65   So, for example
9a3f0 2c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 63 6f 6d  ,.**.**      com
9a400 70 61 72 65 32 70 6f 77 36 33 28 22 39 32 32 33  pare2pow63("9223
9a410 33 37 32 30 33 36 38 35 34 37 37 35 38 30 30 22  372036854775800"
9a420 29 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 72 65 74  ).**.** will ret
9a430 75 72 6e 20 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69  urn -8..*/.stati
9a440 63 20 69 6e 74 20 63 6f 6d 70 61 72 65 32 70 6f  c int compare2po
9a450 77 36 33 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  w63(const char *
9a460 7a 4e 75 6d 29 7b 0a 20 20 69 6e 74 20 63 3b 0a  zNum){.  int c;.
9a470 20 20 63 20 3d 20 6d 65 6d 63 6d 70 28 7a 4e 75    c = memcmp(zNu
9a480 6d 2c 22 39 32 32 33 33 37 32 30 33 36 38 35 34  m,"9223372036854
9a490 37 37 35 38 30 22 2c 31 38 29 3b 0a 20 20 69 66  77580",18);.  if
9a4a0 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 63 20  ( c==0 ){.    c 
9a4b0 3d 20 7a 4e 75 6d 5b 31 38 5d 20 2d 20 27 38 27  = zNum[18] - '8'
9a4c0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63  ;.  }.  return c
9a4d0 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ;.}.../*.** Retu
9a4e0 72 6e 20 54 52 55 45 20 69 66 20 7a 4e 75 6d 20  rn TRUE if zNum 
9a4f0 69 73 20 61 20 36 34 2d 62 69 74 20 73 69 67 6e  is a 64-bit sign
9a500 65 64 20 69 6e 74 65 67 65 72 20 61 6e 64 20 77  ed integer and w
9a510 72 69 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c 75  rite.** the valu
9a520 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72  e of the integer
9a530 20 69 6e 74 6f 20 2a 70 4e 75 6d 2e 20 20 49 66   into *pNum.  If
9a540 20 7a 4e 75 6d 20 69 73 20 6e 6f 74 20 61 6e 20   zNum is not an 
9a550 69 6e 74 65 67 65 72 0a 2a 2a 20 6f 72 20 69 73  integer.** or is
9a560 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61 74   an integer that
9a570 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 74 6f   is too large to
9a580 20 62 65 20 65 78 70 72 65 73 73 65 64 20 77 69   be expressed wi
9a590 74 68 20 36 34 20 62 69 74 73 2c 0a 2a 2a 20 74  th 64 bits,.** t
9a5a0 68 65 6e 20 72 65 74 75 72 6e 20 66 61 6c 73 65  hen return false
9a5b0 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69  ..**.** When thi
9a5c0 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 6f 72  s routine was or
9a5d0 69 67 69 6e 61 6c 6c 79 20 77 72 69 74 74 65 6e  iginally written
9a5e0 20 69 74 20 64 65 61 6c 74 20 77 69 74 68 20 6f   it dealt with o
9a5f0 6e 6c 79 0a 2a 2a 20 33 32 2d 62 69 74 20 6e 75  nly.** 32-bit nu
9a600 6d 62 65 72 73 2e 20 20 41 74 20 74 68 61 74 20  mbers.  At that 
9a610 74 69 6d 65 2c 20 69 74 20 77 61 73 20 6d 75 63  time, it was muc
9a620 68 20 66 61 73 74 65 72 20 74 68 61 6e 20 74 68  h faster than th
9a630 65 0a 2a 2a 20 61 74 6f 69 28 29 20 6c 69 62 72  e.** atoi() libr
9a640 61 72 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 52  ary routine in R
9a650 65 64 48 61 74 20 37 2e 32 2e 0a 2a 2f 0a 53 51  edHat 7.2..*/.SQ
9a660 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
9a670 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 63   sqlite3Atoi64(c
9a680 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 2c  onst char *zNum,
9a690 20 69 36 34 20 2a 70 4e 75 6d 29 7b 0a 20 20 69   i64 *pNum){.  i
9a6a0 36 34 20 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20  64 v = 0;.  int 
9a6b0 6e 65 67 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b  neg;.  int i, c;
9a6c0 0a 20 20 77 68 69 6c 65 28 20 69 73 73 70 61 63  .  while( isspac
9a6d0 65 28 2a 28 75 38 2a 29 7a 4e 75 6d 29 20 29 20  e(*(u8*)zNum) ) 
9a6e0 7a 4e 75 6d 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a  zNum++;.  if( *z
9a6f0 4e 75 6d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20  Num=='-' ){.    
9a700 6e 65 67 20 3d 20 31 3b 0a 20 20 20 20 7a 4e 75  neg = 1;.    zNu
9a710 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  m++;.  }else if(
9a720 20 2a 7a 4e 75 6d 3d 3d 27 2b 27 20 29 7b 0a 20   *zNum=='+' ){. 
9a730 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20 20 20 20     neg = 0;.    
9a740 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b  zNum++;.  }else{
9a750 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20 20  .    neg = 0;.  
9a760 7d 0a 20 20 77 68 69 6c 65 28 20 7a 4e 75 6d 5b  }.  while( zNum[
9a770 30 5d 3d 3d 27 30 27 20 29 7b 20 7a 4e 75 6d 2b  0]=='0' ){ zNum+
9a780 2b 3b 20 7d 20 2f 2a 20 53 6b 69 70 20 6f 76 65  +; } /* Skip ove
9a790 72 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73 2e  r leading zeros.
9a7a0 20 54 69 63 6b 65 74 20 23 32 34 35 34 20 2a 2f   Ticket #2454 */
9a7b0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 28 63 3d 7a  .  for(i=0; (c=z
9a7c0 4e 75 6d 5b 69 5d 29 3e 3d 27 30 27 20 26 26 20  Num[i])>='0' && 
9a7d0 63 3c 3d 27 39 27 3b 20 69 2b 2b 29 7b 0a 20 20  c<='9'; i++){.  
9a7e0 20 20 76 20 3d 20 76 2a 31 30 20 2b 20 63 20 2d    v = v*10 + c -
9a7f0 20 27 30 27 3b 0a 20 20 7d 0a 20 20 2a 70 4e 75   '0';.  }.  *pNu
9a800 6d 20 3d 20 6e 65 67 20 3f 20 2d 76 20 3a 20 76  m = neg ? -v : v
9a810 3b 0a 20 20 69 66 28 20 63 21 3d 30 20 7c 7c 20  ;.  if( c!=0 || 
9a820 69 3d 3d 30 20 7c 7c 20 69 3e 31 39 20 29 7b 0a  i==0 || i>19 ){.
9a830 20 20 20 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 65      /* zNum is e
9a840 6d 70 74 79 20 6f 72 20 63 6f 6e 74 61 69 6e 73  mpty or contains
9a850 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 20 74 65 78   non-numeric tex
9a860 74 20 6f 72 20 69 73 20 6c 6f 6e 67 65 72 0a 20  t or is longer. 
9a870 20 20 20 2a 2a 20 74 68 61 6e 20 31 39 20 64 69     ** than 19 di
9a880 67 69 74 73 20 28 74 68 75 73 20 67 75 61 72 61  gits (thus guara
9a890 6e 74 69 6e 67 20 74 68 61 74 20 69 74 20 69 73  nting that it is
9a8a0 20 74 6f 6f 20 6c 61 72 67 65 29 20 2a 2f 0a 20   too large) */. 
9a8b0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
9a8c0 65 6c 73 65 20 69 66 28 20 69 3c 31 39 20 29 7b  else if( i<19 ){
9a8d0 0a 20 20 20 20 2f 2a 20 4c 65 73 73 20 74 68 61  .    /* Less tha
9a8e0 6e 20 31 39 20 64 69 67 69 74 73 2c 20 73 6f 20  n 19 digits, so 
9a8f0 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 69 74 20  we know that it 
9a900 66 69 74 73 20 69 6e 20 36 34 20 62 69 74 73 20  fits in 64 bits 
9a910 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  */.    return 1;
9a920 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
9a930 20 31 39 2d 64 69 67 69 74 20 6e 75 6d 62 65 72   19-digit number
9a940 73 20 6d 75 73 74 20 62 65 20 6e 6f 20 6c 61 72  s must be no lar
9a950 67 65 72 20 74 68 61 6e 20 39 32 32 33 33 37 32  ger than 9223372
9a960 30 33 36 38 35 34 37 37 35 38 30 37 20 69 66 20  036854775807 if 
9a970 70 6f 73 69 74 69 76 65 0a 20 20 20 20 2a 2a 20  positive.    ** 
9a980 6f 72 20 39 32 32 33 33 37 32 30 33 36 38 35 34  or 9223372036854
9a990 37 37 35 38 30 38 20 69 66 20 6e 65 67 61 74 69  775808 if negati
9a9a0 76 65 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 39  ve.  Note that 9
9a9b0 32 32 33 33 37 32 30 33 36 38 35 34 36 36 35 38  2233720368546658
9a9c0 30 38 0a 20 20 20 20 2a 2a 20 69 73 20 32 5e 36  08.    ** is 2^6
9a9d0 33 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  3. */.    return
9a9e0 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 7a   compare2pow63(z
9a9f0 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d 0a  Num)<neg;.  }.}.
9aa00 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e  ./*.** The strin
9aa10 67 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74  g zNum represent
9aa20 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 54  s an integer.  T
9aa30 68 65 72 65 20 6d 69 67 68 74 20 62 65 20 73 6f  here might be so
9aa40 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 69 6e 66 6f  me other.** info
9aa50 72 6d 61 74 69 6f 6e 20 66 6f 6c 6c 6f 77 69 6e  rmation followin
9aa60 67 20 74 68 65 20 69 6e 74 65 67 65 72 20 74 6f  g the integer to
9aa70 6f 2c 20 62 75 74 20 74 68 61 74 20 70 61 72 74  o, but that part
9aa80 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20   is ignored..** 
9aa90 49 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 74  If the integer t
9aaa0 68 61 74 20 74 68 65 20 70 72 65 66 69 78 20 6f  hat the prefix o
9aab0 66 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74  f zNum represent
9aac0 73 20 77 69 6c 6c 20 66 69 74 20 69 6e 20 61 0a  s will fit in a.
9aad0 2a 2a 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64  ** 64-bit signed
9aae0 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e   integer, return
9aaf0 20 54 52 55 45 2e 20 20 4f 74 68 65 72 77 69 73   TRUE.  Otherwis
9ab00 65 20 72 65 74 75 72 6e 20 46 41 4c 53 45 2e 0a  e return FALSE..
9ab10 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
9ab20 6e 65 20 72 65 74 75 72 6e 73 20 46 41 4c 53 45  ne returns FALSE
9ab30 20 66 6f 72 20 74 68 65 20 73 74 72 69 6e 67 20   for the string 
9ab40 2d 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37  -922337203685477
9ab50 35 38 30 38 20 65 76 65 6e 20 74 68 61 74 0a 2a  5808 even that.*
9ab60 2a 20 74 68 61 74 20 6e 75 6d 62 65 72 20 77 69  * that number wi
9ab70 6c 6c 2c 20 69 6e 20 74 68 65 6f 72 79 20 66 69  ll, in theory fi
9ab80 74 20 69 6e 20 61 20 36 34 2d 62 69 74 20 69 6e  t in a 64-bit in
9ab90 74 65 67 65 72 2e 20 20 50 6f 73 69 74 69 76 65  teger.  Positive
9aba0 0a 2a 2a 20 39 32 32 33 33 37 33 30 33 36 38 35  .** 922337303685
9abb0 34 37 37 35 38 30 38 20 77 69 6c 6c 20 6e 6f 74  4775808 will not
9abc0 20 66 69 74 20 69 6e 20 36 34 20 62 69 74 73 2e   fit in 64 bits.
9abd0 20 20 53 6f 20 69 74 20 73 65 65 6d 73 20 73 61    So it seems sa
9abe0 66 65 72 20 74 6f 20 72 65 74 75 72 6e 0a 2a 2a  fer to return.**
9abf0 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54   false..*/.SQLIT
9ac00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
9ac10 6c 69 74 65 33 46 69 74 73 49 6e 36 34 42 69 74  lite3FitsIn64Bit
9ac20 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  s(const char *zN
9ac30 75 6d 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67 29  um, int negFlag)
9ac40 7b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20  {.  int i, c;.  
9ac50 69 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 20 20 69  int neg = 0;.  i
9ac60 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2d 27 20 29 7b  f( *zNum=='-' ){
9ac70 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20  .    neg = 1;.  
9ac80 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73    zNum++;.  }els
9ac90 65 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2b 27  e if( *zNum=='+'
9aca0 20 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a   ){.    zNum++;.
9acb0 20 20 7d 0a 20 20 69 66 28 20 6e 65 67 46 6c 61    }.  if( negFla
9acc0 67 20 29 20 6e 65 67 20 3d 20 31 2d 6e 65 67 3b  g ) neg = 1-neg;
9acd0 0a 20 20 77 68 69 6c 65 28 20 2a 7a 4e 75 6d 3d  .  while( *zNum=
9ace0 3d 27 30 27 20 29 7b 0a 20 20 20 20 7a 4e 75 6d  ='0' ){.    zNum
9acf0 2b 2b 3b 20 20 20 2f 2a 20 53 6b 69 70 20 6c 65  ++;   /* Skip le
9ad00 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 20 54 69  ading zeros.  Ti
9ad10 63 6b 65 74 20 23 32 34 35 34 20 2a 2f 0a 20 20  cket #2454 */.  
9ad20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 28 63 3d  }.  for(i=0; (c=
9ad30 7a 4e 75 6d 5b 69 5d 29 3e 3d 27 30 27 20 26 26  zNum[i])>='0' &&
9ad40 20 63 3c 3d 27 39 27 3b 20 69 2b 2b 29 7b 7d 0a   c<='9'; i++){}.
9ad50 20 20 69 66 28 20 69 3c 31 39 20 29 7b 0a 20 20    if( i<19 ){.  
9ad60 20 20 2f 2a 20 47 75 61 72 61 6e 74 65 65 64 20    /* Guaranteed 
9ad70 74 6f 20 66 69 74 20 69 66 20 6c 65 73 73 20 74  to fit if less t
9ad80 68 61 6e 20 31 39 20 64 69 67 69 74 73 20 2a 2f  han 19 digits */
9ad90 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
9ada0 20 7d 65 6c 73 65 20 69 66 28 20 69 3e 31 39 20   }else if( i>19 
9adb0 29 7b 0a 20 20 20 20 2f 2a 20 47 75 61 72 61 6e  ){.    /* Guaran
9adc0 74 65 65 64 20 74 6f 20 62 65 20 74 6f 6f 20 62  teed to be too b
9add0 69 67 20 69 66 20 67 72 65 61 74 65 72 20 74 68  ig if greater th
9ade0 61 6e 20 31 39 20 64 69 67 69 74 73 20 2a 2f 0a  an 19 digits */.
9adf0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
9ae00 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f  }else{.    /* Co
9ae10 6d 70 61 72 65 20 61 67 61 69 6e 73 74 20 32 5e  mpare against 2^
9ae20 36 33 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  63. */.    retur
9ae30 6e 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28  n compare2pow63(
9ae40 7a 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d  zNum)<neg;.  }.}
9ae50 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 7a 4e 75 6d 20  ../*.** If zNum 
9ae60 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 69 6e  represents an in
9ae70 74 65 67 65 72 20 74 68 61 74 20 77 69 6c 6c 20  teger that will 
9ae80 66 69 74 20 69 6e 20 33 32 2d 62 69 74 73 2c 20  fit in 32-bits, 
9ae90 74 68 65 6e 20 73 65 74 0a 2a 2a 20 2a 70 56 61  then set.** *pVa
9aea0 6c 75 65 20 74 6f 20 74 68 61 74 20 69 6e 74 65  lue to that inte
9aeb0 67 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20 74  ger and return t
9aec0 72 75 65 2e 20 20 4f 74 68 65 72 77 69 73 65 20  rue.  Otherwise 
9aed0 72 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2a  return false..**
9aee0 0a 2a 2a 20 41 6e 79 20 6e 6f 6e 2d 6e 75 6d 65  .** Any non-nume
9aef0 72 69 63 20 63 68 61 72 61 63 74 65 72 73 20 74  ric characters t
9af00 68 61 74 20 66 6f 6c 6c 6f 77 69 6e 67 20 7a 4e  hat following zN
9af10 75 6d 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a  um are ignored..
9af20 2a 2a 20 54 68 69 73 20 69 73 20 64 69 66 66 65  ** This is diffe
9af30 72 65 6e 74 20 66 72 6f 6d 20 73 71 6c 69 74 65  rent from sqlite
9af40 33 41 74 6f 69 36 34 28 29 20 77 68 69 63 68 20  3Atoi64() which 
9af50 72 65 71 75 69 72 65 73 20 74 68 65 0a 2a 2a 20  requires the.** 
9af60 69 6e 70 75 74 20 6e 75 6d 62 65 72 20 74 6f 20  input number to 
9af70 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74  be zero-terminat
9af80 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
9af90 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
9afa0 33 47 65 74 49 6e 74 33 32 28 63 6f 6e 73 74 20  3GetInt32(const 
9afb0 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 6e 74 20  char *zNum, int 
9afc0 2a 70 56 61 6c 75 65 29 7b 0a 20 20 73 71 6c 69  *pValue){.  sqli
9afd0 74 65 5f 69 6e 74 36 34 20 76 20 3d 20 30 3b 0a  te_int64 v = 0;.
9afe0 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 69 6e    int i, c;.  in
9aff0 74 20 6e 65 67 20 3d 20 30 3b 0a 20 20 69 66 28  t neg = 0;.  if(
9b000 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2d 27 20 29 7b   zNum[0]=='-' ){
9b010 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20  .    neg = 1;.  
9b020 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73    zNum++;.  }els
9b030 65 20 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27  e if( zNum[0]=='
9b040 2b 27 20 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b  +' ){.    zNum++
9b050 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 7a  ;.  }.  while( z
9b060 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29 20 7a 4e  Num[0]=='0' ) zN
9b070 75 6d 2b 2b 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  um++;.  for(i=0;
9b080 20 69 3c 31 31 20 26 26 20 28 63 20 3d 20 7a 4e   i<11 && (c = zN
9b090 75 6d 5b 69 5d 20 2d 20 27 30 27 29 3e 3d 30 20  um[i] - '0')>=0 
9b0a0 26 26 20 63 3c 3d 39 3b 20 69 2b 2b 29 7b 0a 20  && c<=9; i++){. 
9b0b0 20 20 20 76 20 3d 20 76 2a 31 30 20 2b 20 63 3b     v = v*10 + c;
9b0c0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 6c  .  }..  /* The l
9b0d0 6f 6e 67 65 73 74 20 64 65 63 69 6d 61 6c 20 72  ongest decimal r
9b0e0 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
9b0f0 20 61 20 33 32 20 62 69 74 20 69 6e 74 65 67 65   a 32 bit intege
9b100 72 20 69 73 20 31 30 20 64 69 67 69 74 73 3a 0a  r is 10 digits:.
9b110 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20    **.  **       
9b120 20 20 20 20 20 20 31 32 33 34 35 36 37 38 39 30        1234567890
9b130 0a 20 20 2a 2a 20 20 20 20 20 32 5e 33 31 20 2d  .  **     2^31 -
9b140 3e 20 32 31 34 37 34 38 33 36 34 38 0a 20 20 2a  > 2147483648.  *
9b150 2f 0a 20 20 69 66 28 20 69 3e 31 30 20 29 7b 0a  /.  if( i>10 ){.
9b160 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
9b170 7d 0a 20 20 69 66 28 20 76 2d 6e 65 67 3e 32 31  }.  if( v-neg>21
9b180 34 37 34 38 33 36 34 37 20 29 7b 0a 20 20 20 20  47483647 ){.    
9b190 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
9b1a0 69 66 28 20 6e 65 67 20 29 7b 0a 20 20 20 20 76  if( neg ){.    v
9b1b0 20 3d 20 2d 76 3b 0a 20 20 7d 0a 20 20 2a 70 56   = -v;.  }.  *pV
9b1c0 61 6c 75 65 20 3d 20 28 69 6e 74 29 76 3b 0a 20  alue = (int)v;. 
9b1d0 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a   return 1;.}../*
9b1e0 0a 2a 2a 20 54 68 65 20 76 61 72 69 61 62 6c 65  .** The variable
9b1f0 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20  -length integer 
9b200 65 6e 63 6f 64 69 6e 67 20 69 73 20 61 73 20 66  encoding is as f
9b210 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 4b 45  ollows:.**.** KE
9b220 59 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 41 20  Y:.**         A 
9b230 3d 20 30 78 78 78 78 78 78 78 20 20 20 20 37 20  = 0xxxxxxx    7 
9b240 62 69 74 73 20 6f 66 20 64 61 74 61 20 61 6e 64  bits of data and
9b250 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a   one flag bit.**
9b260 20 20 20 20 20 20 20 20 20 42 20 3d 20 31 78 78           B = 1xx
9b270 78 78 78 78 78 20 20 20 20 37 20 62 69 74 73 20  xxxxx    7 bits 
9b280 6f 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65 20  of data and one 
9b290 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20 20 20 20  flag bit.**     
9b2a0 20 20 20 20 43 20 3d 20 78 78 78 78 78 78 78 78      C = xxxxxxxx
9b2b0 20 20 20 20 38 20 62 69 74 73 20 6f 66 20 64 61      8 bits of da
9b2c0 74 61 0a 2a 2a 0a 2a 2a 20 20 37 20 62 69 74 73  ta.**.**  7 bits
9b2d0 20 2d 20 41 0a 2a 2a 20 31 34 20 62 69 74 73 20   - A.** 14 bits 
9b2e0 2d 20 42 41 0a 2a 2a 20 32 31 20 62 69 74 73 20  - BA.** 21 bits 
9b2f0 2d 20 42 42 41 0a 2a 2a 20 32 38 20 62 69 74 73  - BBA.** 28 bits
9b300 20 2d 20 42 42 42 41 0a 2a 2a 20 33 35 20 62 69   - BBBA.** 35 bi
9b310 74 73 20 2d 20 42 42 42 42 41 0a 2a 2a 20 34 32  ts - BBBBA.** 42
9b320 20 62 69 74 73 20 2d 20 42 42 42 42 42 41 0a 2a   bits - BBBBBA.*
9b330 2a 20 34 39 20 62 69 74 73 20 2d 20 42 42 42 42  * 49 bits - BBBB
9b340 42 42 41 0a 2a 2a 20 35 36 20 62 69 74 73 20 2d  BBA.** 56 bits -
9b350 20 42 42 42 42 42 42 42 41 0a 2a 2a 20 36 34 20   BBBBBBBA.** 64 
9b360 62 69 74 73 20 2d 20 42 42 42 42 42 42 42 42 43  bits - BBBBBBBBC
9b370 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  .*/../*.** Write
9b380 20 61 20 36 34 2d 62 69 74 20 76 61 72 69 61 62   a 64-bit variab
9b390 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65  le-length intege
9b3a0 72 20 74 6f 20 6d 65 6d 6f 72 79 20 73 74 61 72  r to memory star
9b3b0 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a  ting at p[0]..**
9b3c0 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 64   The length of d
9b3d0 61 74 61 20 77 72 69 74 65 20 77 69 6c 6c 20 62  ata write will b
9b3e0 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20  e between 1 and 
9b3f0 39 20 62 79 74 65 73 2e 20 20 54 68 65 20 6e 75  9 bytes.  The nu
9b400 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73  mber.** of bytes
9b410 20 77 72 69 74 74 65 6e 20 69 73 20 72 65 74 75   written is retu
9b420 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 61  rned..**.** A va
9b430 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e  riable-length in
9b440 74 65 67 65 72 20 63 6f 6e 73 69 73 74 73 20 6f  teger consists o
9b450 66 20 74 68 65 20 6c 6f 77 65 72 20 37 20 62 69  f the lower 7 bi
9b460 74 73 20 6f 66 20 65 61 63 68 20 62 79 74 65 0a  ts of each byte.
9b470 2a 2a 20 66 6f 72 20 61 6c 6c 20 62 79 74 65 73  ** for all bytes
9b480 20 74 68 61 74 20 68 61 76 65 20 74 68 65 20 38   that have the 8
9b490 74 68 20 62 69 74 20 73 65 74 20 61 6e 64 20 6f  th bit set and o
9b4a0 6e 65 20 62 79 74 65 20 77 69 74 68 20 74 68 65  ne byte with the
9b4b0 20 38 74 68 0a 2a 2a 20 62 69 74 20 63 6c 65 61   8th.** bit clea
9b4c0 72 2e 20 20 45 78 63 65 70 74 2c 20 69 66 20 77  r.  Except, if w
9b4d0 65 20 67 65 74 20 74 6f 20 74 68 65 20 39 74 68  e get to the 9th
9b4e0 20 62 79 74 65 2c 20 69 74 20 73 74 6f 72 65 73   byte, it stores
9b4f0 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 38 20 62   the full.** 8 b
9b500 69 74 73 20 61 6e 64 20 69 73 20 74 68 65 20 6c  its and is the l
9b510 61 73 74 20 62 79 74 65 2e 0a 2a 2f 0a 53 51 4c  ast byte..*/.SQL
9b520 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
9b530 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74  sqlite3PutVarint
9b540 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
9b550 70 2c 20 75 36 34 20 76 29 7b 0a 20 20 69 6e 74  p, u64 v){.  int
9b560 20 69 2c 20 6a 2c 20 6e 3b 0a 20 20 75 38 20 62   i, j, n;.  u8 b
9b570 75 66 5b 31 30 5d 3b 0a 20 20 69 66 28 20 76 20  uf[10];.  if( v 
9b580 26 20 28 28 28 75 36 34 29 30 78 66 66 30 30 30  & (((u64)0xff000
9b590 30 30 30 29 3c 3c 33 32 29 20 29 7b 0a 20 20 20  000)<<32) ){.   
9b5a0 20 70 5b 38 5d 20 3d 20 76 3b 0a 20 20 20 20 76   p[8] = v;.    v
9b5b0 20 3e 3e 3d 20 38 3b 0a 20 20 20 20 66 6f 72 28   >>= 8;.    for(
9b5c0 69 3d 37 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b  i=7; i>=0; i--){
9b5d0 0a 20 20 20 20 20 20 70 5b 69 5d 20 3d 20 28 76  .      p[i] = (v
9b5e0 20 26 20 30 78 37 66 29 20 7c 20 30 78 38 30 3b   & 0x7f) | 0x80;
9b5f0 0a 20 20 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0a  .      v >>= 7;.
9b600 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
9b610 20 39 3b 0a 20 20 7d 20 20 20 20 0a 20 20 6e 20   9;.  }    .  n 
9b620 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 62  = 0;.  do{.    b
9b630 75 66 5b 6e 2b 2b 5d 20 3d 20 28 76 20 26 20 30  uf[n++] = (v & 0
9b640 78 37 66 29 20 7c 20 30 78 38 30 3b 0a 20 20 20  x7f) | 0x80;.   
9b650 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69   v >>= 7;.  }whi
9b660 6c 65 28 20 76 21 3d 30 20 29 3b 0a 20 20 62 75  le( v!=0 );.  bu
9b670 66 5b 30 5d 20 26 3d 20 30 78 37 66 3b 0a 20 20  f[0] &= 0x7f;.  
9b680 61 73 73 65 72 74 28 20 6e 3c 3d 39 20 29 3b 0a  assert( n<=9 );.
9b690 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 6e 2d 31    for(i=0, j=n-1
9b6a0 3b 20 6a 3e 3d 30 3b 20 6a 2d 2d 2c 20 69 2b 2b  ; j>=0; j--, i++
9b6b0 29 7b 0a 20 20 20 20 70 5b 69 5d 20 3d 20 62 75  ){.    p[i] = bu
9b6c0 66 5b 6a 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75  f[j];.  }.  retu
9b6d0 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  rn n;.}../*.** T
9b6e0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
9b6f0 20 66 61 73 74 65 72 20 76 65 72 73 69 6f 6e 20   faster version 
9b700 6f 66 20 73 71 6c 69 74 65 33 50 75 74 56 61 72  of sqlite3PutVar
9b710 69 6e 74 28 29 20 74 68 61 74 20 6f 6e 6c 79 0a  int() that only.
9b720 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 33 32 2d  ** works for 32-
9b730 62 69 74 20 70 6f 73 69 74 69 76 65 20 69 6e 74  bit positive int
9b740 65 67 65 72 73 20 61 6e 64 20 77 68 69 63 68 20  egers and which 
9b750 69 73 20 6f 70 74 69 6d 69 7a 65 64 20 66 6f 72  is optimized for
9b760 0a 2a 2a 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63  .** the common c
9b770 61 73 65 20 6f 66 20 73 6d 61 6c 6c 20 69 6e 74  ase of small int
9b780 65 67 65 72 73 2e 20 20 41 20 4d 41 43 52 4f 20  egers.  A MACRO 
9b790 76 65 72 73 69 6f 6e 2c 20 70 75 74 56 61 72 69  version, putVari
9b7a0 6e 74 33 32 2c 0a 2a 2a 20 69 73 20 70 72 6f 76  nt32,.** is prov
9b7b0 69 64 65 64 20 77 68 69 63 68 20 69 6e 6c 69 6e  ided which inlin
9b7c0 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 79  es the single-by
9b7d0 74 65 20 63 61 73 65 2e 20 20 41 6c 6c 20 63 6f  te case.  All co
9b7e0 64 65 20 73 68 6f 75 6c 64 20 75 73 65 0a 2a 2a  de should use.**
9b7f0 20 74 68 65 20 4d 41 43 52 4f 20 76 65 72 73 69   the MACRO versi
9b800 6f 6e 20 61 73 20 74 68 69 73 20 66 75 6e 63 74  on as this funct
9b810 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 65 20  ion assumes the 
9b820 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65  single-byte case
9b830 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20   has.** already 
9b840 62 65 65 6e 20 68 61 6e 64 6c 65 64 2e 0a 2a 2f  been handled..*/
9b850 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
9b860 69 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56 61  int sqlite3PutVa
9b870 72 69 6e 74 33 32 28 75 6e 73 69 67 6e 65 64 20  rint32(unsigned 
9b880 63 68 61 72 20 2a 70 2c 20 75 33 32 20 76 29 7b  char *p, u32 v){
9b890 0a 23 69 66 6e 64 65 66 20 70 75 74 56 61 72 69  .#ifndef putVari
9b8a0 6e 74 33 32 0a 20 20 69 66 28 20 28 76 20 26 20  nt32.  if( (v & 
9b8b0 7e 30 78 37 66 29 3d 3d 30 20 29 7b 0a 20 20 20  ~0x7f)==0 ){.   
9b8c0 20 70 5b 30 5d 20 3d 20 76 3b 0a 20 20 20 20 72   p[0] = v;.    r
9b8d0 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e  eturn 1;.  }.#en
9b8e0 64 69 66 0a 20 20 69 66 28 20 28 76 20 26 20 7e  dif.  if( (v & ~
9b8f0 30 78 33 66 66 66 29 3d 3d 30 20 29 7b 0a 20 20  0x3fff)==0 ){.  
9b900 20 20 70 5b 30 5d 20 3d 20 28 76 3e 3e 37 29 20    p[0] = (v>>7) 
9b910 7c 20 30 78 38 30 3b 0a 20 20 20 20 70 5b 31 5d  | 0x80;.    p[1]
9b920 20 3d 20 76 20 26 20 30 78 37 66 3b 0a 20 20 20   = v & 0x7f;.   
9b930 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 20   return 2;.  }. 
9b940 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50   return sqlite3P
9b950 75 74 56 61 72 69 6e 74 28 70 2c 20 76 29 3b 0a  utVarint(p, v);.
9b960 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20  }../*.** Read a 
9b970 36 34 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d  64-bit variable-
9b980 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 66  length integer f
9b990 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 61 72 74  rom memory start
9b9a0 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20  ing at p[0]..** 
9b9b0 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
9b9c0 72 20 6f 66 20 62 79 74 65 73 20 72 65 61 64 2e  r of bytes read.
9b9d0 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 73    The value is s
9b9e0 74 6f 72 65 64 20 69 6e 20 2a 76 2e 0a 2a 2f 0a  tored in *v..*/.
9b9f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
9ba00 6e 74 20 73 71 6c 69 74 65 33 47 65 74 56 61 72  nt sqlite3GetVar
9ba10 69 6e 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e  int(const unsign
9ba20 65 64 20 63 68 61 72 20 2a 70 2c 20 75 36 34 20  ed char *p, u64 
9ba30 2a 76 29 7b 0a 20 20 75 33 32 20 61 2c 62 2c 73  *v){.  u32 a,b,s
9ba40 3b 0a 0a 20 20 61 20 3d 20 2a 70 3b 0a 20 20 2f  ;..  a = *p;.  /
9ba50 2f 20 61 3a 20 70 30 20 28 75 6e 6d 61 73 6b 65  / a: p0 (unmaske
9ba60 64 29 0a 20 20 69 66 20 28 21 28 61 26 30 78 38  d).  if (!(a&0x8
9ba70 30 29 29 0a 20 20 7b 0a 20 20 20 20 2a 76 20 3d  0)).  {.    *v =
9ba80 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31   a;.    return 1
9ba90 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20  ;.  }..  p++;.  
9baa0 62 20 3d 20 2a 70 3b 0a 20 20 2f 2f 20 62 3a 20  b = *p;.  // b: 
9bab0 70 31 20 28 75 6e 6d 61 73 6b 65 64 29 0a 20 20  p1 (unmasked).  
9bac0 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a 20  if (!(b&0x80)). 
9bad0 20 7b 0a 20 20 20 20 61 20 26 3d 20 30 78 37 66   {.    a &= 0x7f
9bae0 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a  ;.    a = a<<7;.
9baf0 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20      a |= b;.    
9bb00 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 75  *v = a;.    retu
9bb10 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b  rn 2;.  }..  p++
9bb20 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20  ;.  a = a<<14;. 
9bb30 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2f 20 61   a |= *p;.  // a
9bb40 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 75  : p0<<14 | p2 (u
9bb50 6e 6d 61 73 6b 65 64 29 0a 20 20 69 66 20 28 21  nmasked).  if (!
9bb60 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20  (a&0x80)).  {.  
9bb70 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34    a &= (0x7f<<14
9bb80 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20  )|(0x7f);.    b 
9bb90 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 62 20 3d  &= 0x7f;.    b =
9bba0 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20   b<<7;.    a |= 
9bbb0 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20  b;.    *v = a;. 
9bbc0 20 20 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 7d     return 3;.  }
9bbd0 0a 0a 20 20 2f 2f 20 43 53 45 31 20 66 72 6f 6d  ..  // CSE1 from
9bbe0 20 62 65 6c 6f 77 0a 20 20 61 20 26 3d 20 28 30   below.  a &= (0
9bbf0 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
9bc00 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c  .  p++;.  b = b<
9bc10 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a  <14;.  b |= *p;.
9bc20 20 20 2f 2f 20 62 3a 20 70 31 3c 3c 31 34 20 7c    // b: p1<<14 |
9bc30 20 70 33 20 28 75 6e 6d 61 73 6b 65 64 29 0a 20   p3 (unmasked). 
9bc40 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a   if (!(b&0x80)).
9bc50 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28 30 78    {.    b &= (0x
9bc60 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a  7f<<14)|(0x7f);.
9bc70 20 20 20 20 2f 2f 20 6d 6f 76 65 64 20 43 53 45      // moved CSE
9bc80 31 20 75 70 0a 20 20 20 20 2f 2f 20 61 20 26 3d  1 up.    // a &=
9bc90 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37   (0x7f<<14)|(0x7
9bca0 66 29 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37  f);.    a = a<<7
9bcb0 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20  ;.    a |= b;.  
9bcc0 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65    *v = a;.    re
9bcd0 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20 2f  turn 4;.  }..  /
9bce0 2f 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32  / a: p0<<14 | p2
9bcf0 20 28 6d 61 73 6b 65 64 29 0a 20 20 2f 2f 20 62   (masked).  // b
9bd00 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75  : p1<<14 | p3 (u
9bd10 6e 6d 61 73 6b 65 64 29 0a 20 20 2f 2f 20 31 3a  nmasked).  // 1:
9bd20 73 61 76 65 20 6f 66 66 20 70 30 3c 3c 32 31 20  save off p0<<21 
9bd30 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37  | p1<<14 | p2<<7
9bd40 20 7c 20 70 33 20 28 6d 61 73 6b 65 64 29 0a 20   | p3 (masked). 
9bd50 20 2f 2f 20 6d 6f 76 65 64 20 43 53 45 31 20 75   // moved CSE1 u
9bd60 70 0a 20 20 2f 2f 20 61 20 26 3d 20 28 30 78 37  p.  // a &= (0x7
9bd70 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20  f<<14)|(0x7f);. 
9bd80 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29   b &= (0x7f<<14)
9bd90 7c 28 30 78 37 66 29 3b 0a 20 20 73 20 3d 20 61  |(0x7f);.  s = a
9bda0 3b 0a 20 20 2f 2f 20 73 3a 20 70 30 3c 3c 31 34  ;.  // s: p0<<14
9bdb0 20 7c 20 70 32 20 28 6d 61 73 6b 65 64 29 0a 0a   | p2 (masked)..
9bdc0 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c    p++;.  a = a<<
9bdd0 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20  14;.  a |= *p;. 
9bde0 20 2f 2f 20 61 3a 20 70 30 3c 3c 32 38 20 7c 20   // a: p0<<28 | 
9bdf0 70 32 3c 3c 31 34 20 7c 20 70 34 20 28 75 6e 6d  p2<<14 | p4 (unm
9be00 61 73 6b 65 64 29 0a 20 20 69 66 20 28 21 28 61  asked).  if (!(a
9be10 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20  &0x80)).  {.    
9be20 2f 2f 20 77 65 20 63 61 6e 20 73 6b 69 70 20 74  // we can skip t
9be30 68 65 73 65 20 63 61 75 73 65 20 74 68 65 79 20  hese cause they 
9be40 77 65 72 65 20 28 65 66 66 65 63 74 69 76 65 6c  were (effectivel
9be50 79 29 20 64 6f 6e 65 20 61 62 6f 76 65 20 69 6e  y) done above in
9be60 20 63 61 6c 63 27 69 6e 67 20 73 0a 20 20 20 20   calc'ing s.    
9be70 2f 2f 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32  // a &= (0x7f<<2
9be80 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30  8)|(0x7f<<14)|(0
9be90 78 37 66 29 3b 0a 20 20 20 20 2f 2f 20 62 20 26  x7f);.    // b &
9bea0 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78  = (0x7f<<14)|(0x
9beb0 37 66 29 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c  7f);.    b = b<<
9bec0 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20  7;.    a |= b;. 
9bed0 20 20 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 20     s = s>>18;.  
9bee0 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c    *v = ((u64)s)<
9bef0 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74  <32 | a;.    ret
9bf00 75 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f 2f  urn 5;.  }..  //
9bf10 20 32 3a 73 61 76 65 20 6f 66 66 20 70 30 3c 3c   2:save off p0<<
9bf20 32 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32  21 | p1<<14 | p2
9bf30 3c 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 64  <<7 | p3 (masked
9bf40 29 0a 20 20 73 20 3d 20 73 3c 3c 37 3b 0a 20 20  ).  s = s<<7;.  
9bf50 73 20 7c 3d 20 62 3b 0a 20 20 2f 2f 20 73 3a 20  s |= b;.  // s: 
9bf60 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 34 20  p0<<21 | p1<<14 
9bf70 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 6d 61  | p2<<7 | p3 (ma
9bf80 73 6b 65 64 29 0a 0a 20 20 70 2b 2b 3b 0a 20 20  sked)..  p++;.  
9bf90 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c  b = b<<14;.  b |
9bfa0 3d 20 2a 70 3b 0a 20 20 2f 2f 20 62 3a 20 70 31  = *p;.  // b: p1
9bfb0 3c 3c 32 38 20 7c 20 70 33 3c 3c 31 34 20 7c 20  <<28 | p3<<14 | 
9bfc0 70 35 20 28 75 6e 6d 61 73 6b 65 64 29 0a 20 20  p5 (unmasked).  
9bfd0 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a 20  if (!(b&0x80)). 
9bfe0 20 7b 0a 20 20 20 20 2f 2f 20 77 65 20 63 61 6e   {.    // we can
9bff0 20 73 6b 69 70 20 74 68 69 73 20 63 61 75 73 65   skip this cause
9c000 20 69 74 20 77 61 73 20 28 65 66 66 65 63 74 69   it was (effecti
9c010 76 65 6c 79 29 20 64 6f 6e 65 20 61 62 6f 76 65  vely) done above
9c020 20 69 6e 20 63 61 6c 63 27 69 6e 67 20 73 0a 20   in calc'ing s. 
9c030 20 20 20 2f 2f 20 62 20 26 3d 20 28 30 78 37 66     // b &= (0x7f
9c040 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 29  <<28)|(0x7f<<14)
9c050 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20 26  |(0x7f);.    a &
9c060 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78  = (0x7f<<14)|(0x
9c070 37 66 29 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c  7f);.    a = a<<
9c080 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20  7;.    a |= b;. 
9c090 20 20 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 20     s = s>>18;.  
9c0a0 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c    *v = ((u64)s)<
9c0b0 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74  <32 | a;.    ret
9c0c0 75 72 6e 20 36 3b 0a 20 20 7d 0a 0a 20 20 70 2b  urn 6;.  }..  p+
9c0d0 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a  +;.  a = a<<14;.
9c0e0 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2f 20    a |= *p;.  // 
9c0f0 61 3a 20 70 32 3c 3c 32 38 20 7c 20 70 34 3c 3c  a: p2<<28 | p4<<
9c100 31 34 20 7c 20 70 36 20 28 75 6e 6d 61 73 6b 65  14 | p6 (unmaske
9c110 64 29 0a 20 20 69 66 20 28 21 28 61 26 30 78 38  d).  if (!(a&0x8
9c120 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d  0)).  {.    a &=
9c130 20 28 30 78 37 66 3c 3c 32 38 29 7c 28 30 78 37   (0x7f<<28)|(0x7
9c140 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20  f<<14)|(0x7f);. 
9c150 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31     b &= (0x7f<<1
9c160 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62  4)|(0x7f);.    b
9c170 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c   = b<<7;.    a |
9c180 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e  = b;.    s = s>>
9c190 31 31 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75  11;.    *v = ((u
9c1a0 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20  64)s)<<32 | a;. 
9c1b0 20 20 20 72 65 74 75 72 6e 20 37 3b 0a 20 20 7d     return 7;.  }
9c1c0 0a 0a 20 20 2f 2f 20 43 53 45 32 20 66 72 6f 6d  ..  // CSE2 from
9c1d0 20 62 65 6c 6f 77 0a 20 20 61 20 26 3d 20 28 30   below.  a &= (0
9c1e0 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
9c1f0 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c  .  p++;.  b = b<
9c200 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a  <14;.  b |= *p;.
9c210 20 20 2f 2f 20 62 3a 20 70 33 3c 3c 32 38 20 7c    // b: p3<<28 |
9c220 20 70 35 3c 3c 31 34 20 7c 20 70 37 20 28 75 6e   p5<<14 | p7 (un
9c230 6d 61 73 6b 65 64 29 0a 20 20 69 66 20 28 21 28  masked).  if (!(
9c240 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20  b&0x80)).  {.   
9c250 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 32 38 29   b &= (0x7f<<28)
9c260 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37  |(0x7f<<14)|(0x7
9c270 66 29 3b 0a 20 20 20 20 2f 2f 20 6d 6f 76 65 64  f);.    // moved
9c280 20 43 53 45 32 20 75 70 0a 20 20 20 20 2f 2f 20   CSE2 up.    // 
9c290 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c  a &= (0x7f<<14)|
9c2a0 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20 3d 20  (0x7f);.    a = 
9c2b0 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62  a<<7;.    a |= b
9c2c0 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 34 3b 0a  ;.    s = s>>4;.
9c2d0 20 20 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73      *v = ((u64)s
9c2e0 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72  )<<32 | a;.    r
9c2f0 65 74 75 72 6e 20 38 3b 0a 20 20 7d 0a 0a 20 20  eturn 8;.  }..  
9c300 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 35  p++;.  a = a<<15
9c310 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f  ;.  a |= *p;.  /
9c320 2f 20 61 3a 20 70 34 3c 3c 32 39 20 7c 20 70 36  / a: p4<<29 | p6
9c330 3c 3c 31 35 20 7c 20 70 38 20 28 75 6e 6d 61 73  <<15 | p8 (unmas
9c340 6b 65 64 29 0a 0a 20 20 2f 2f 20 6d 6f 76 65 64  ked)..  // moved
9c350 20 43 53 45 32 20 75 70 0a 20 20 2f 2f 20 61 20   CSE2 up.  // a 
9c360 26 3d 20 28 30 78 37 66 3c 3c 32 39 29 7c 28 30  &= (0x7f<<29)|(0
9c370 78 37 66 3c 3c 31 35 29 7c 28 30 78 66 66 29 3b  x7f<<15)|(0xff);
9c380 0a 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31  .  b &= (0x7f<<1
9c390 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 62 20 3d  4)|(0x7f);.  b =
9c3a0 20 62 3c 3c 38 3b 0a 20 20 61 20 7c 3d 20 62 3b   b<<8;.  a |= b;
9c3b0 0a 0a 20 20 73 20 3d 20 73 3c 3c 34 3b 0a 20 20  ..  s = s<<4;.  
9c3c0 62 20 3d 20 70 5b 2d 34 5d 3b 0a 20 20 62 20 26  b = p[-4];.  b &
9c3d0 3d 20 30 78 37 66 3b 0a 20 20 62 20 3d 20 62 3e  = 0x7f;.  b = b>
9c3e0 3e 33 3b 0a 20 20 73 20 7c 3d 20 62 3b 0a 0a 20  >3;.  s |= b;.. 
9c3f0 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c   *v = ((u64)s)<<
9c400 33 32 20 7c 20 61 3b 0a 0a 20 20 72 65 74 75 72  32 | a;..  retur
9c410 6e 20 39 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  n 9;.}../*.** Re
9c420 61 64 20 61 20 33 32 2d 62 69 74 20 76 61 72 69  ad a 32-bit vari
9c430 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65  able-length inte
9c440 67 65 72 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20  ger from memory 
9c450 73 74 61 72 74 69 6e 67 20 61 74 20 70 5b 30 5d  starting at p[0]
9c460 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ..** Return the 
9c470 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
9c480 72 65 61 64 2e 20 20 54 68 65 20 76 61 6c 75 65  read.  The value
9c490 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 2a 76   is stored in *v
9c4a0 2e 0a 2a 2a 20 41 20 4d 41 43 52 4f 20 76 65 72  ..** A MACRO ver
9c4b0 73 69 6f 6e 2c 20 67 65 74 56 61 72 69 6e 74 33  sion, getVarint3
9c4c0 32 2c 20 69 73 20 70 72 6f 76 69 64 65 64 20 77  2, is provided w
9c4d0 68 69 63 68 20 69 6e 6c 69 6e 65 73 20 74 68 65  hich inlines the
9c4e0 20 0a 2a 2a 20 73 69 6e 67 6c 65 2d 62 79 74 65   .** single-byte
9c4f0 20 63 61 73 65 2e 20 20 41 6c 6c 20 63 6f 64 65   case.  All code
9c500 20 73 68 6f 75 6c 64 20 75 73 65 20 74 68 65 20   should use the 
9c510 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 20 61 73  MACRO version as
9c520 20 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69   .** this functi
9c530 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 65 20 73  on assumes the s
9c540 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 20  ingle-byte case 
9c550 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
9c560 20 68 61 6e 64 6c 65 64 2e 0a 2a 2f 0a 53 51 4c   handled..*/.SQL
9c570 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
9c580 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74  sqlite3GetVarint
9c590 33 32 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  32(const unsigne
9c5a0 64 20 63 68 61 72 20 2a 70 2c 20 75 33 32 20 2a  d char *p, u32 *
9c5b0 76 29 7b 0a 20 20 75 33 32 20 61 2c 62 3b 0a 0a  v){.  u32 a,b;..
9c5c0 20 20 61 20 3d 20 2a 70 3b 0a 20 20 2f 2f 20 61    a = *p;.  // a
9c5d0 3a 20 70 30 20 28 75 6e 6d 61 73 6b 65 64 29 0a  : p0 (unmasked).
9c5e0 23 69 66 6e 64 65 66 20 67 65 74 56 61 72 69 6e  #ifndef getVarin
9c5f0 74 33 32 0a 20 20 69 66 20 28 21 28 61 26 30 78  t32.  if (!(a&0x
9c600 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2a 76 20  80)).  {.    *v 
9c610 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = a;.    return 
9c620 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  1;.  }.#endif.. 
9c630 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a   p++;.  b = *p;.
9c640 20 20 2f 2f 20 62 3a 20 70 31 20 28 75 6e 6d 61    // b: p1 (unma
9c650 73 6b 65 64 29 0a 20 20 69 66 20 28 21 28 62 26  sked).  if (!(b&
9c660 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61  0x80)).  {.    a
9c670 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 61 20   &= 0x7f;.    a 
9c680 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d  = a<<7;.    *v =
9c690 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75   a | b;.    retu
9c6a0 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b  rn 2;.  }..  p++
9c6b0 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20  ;.  a = a<<14;. 
9c6c0 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2f 20 61   a |= *p;.  // a
9c6d0 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 75  : p0<<14 | p2 (u
9c6e0 6e 6d 61 73 6b 65 64 29 0a 20 20 69 66 20 28 21  nmasked).  if (!
9c6f0 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20  (a&0x80)).  {.  
9c700 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34    a &= (0x7f<<14
9c710 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20  )|(0x7f);.    b 
9c720 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 62 20 3d  &= 0x7f;.    b =
9c730 20 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20   b<<7;.    *v = 
9c740 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72  a | b;.    retur
9c750 6e 20 33 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b  n 3;.  }..  p++;
9c760 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20  .  b = b<<14;.  
9c770 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2f 20 62 3a  b |= *p;.  // b:
9c780 20 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 6e   p1<<14 | p3 (un
9c790 6d 61 73 6b 65 64 29 0a 20 20 69 66 20 28 21 28  masked).  if (!(
9c7a0 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20  b&0x80)).  {.   
9c7b0 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29   b &= (0x7f<<14)
9c7c0 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20 26  |(0x7f);.    a &
9c7d0 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78  = (0x7f<<14)|(0x
9c7e0 37 66 29 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c  7f);.    a = a<<
9c7f0 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c 20  7;.    *v = a | 
9c800 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 34 3b  b;.    return 4;
9c810 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61  .  }..  p++;.  a
9c820 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d   = a<<14;.  a |=
9c830 20 2a 70 3b 0a 20 20 2f 2f 20 61 3a 20 70 30 3c   *p;.  // a: p0<
9c840 3c 32 38 20 7c 20 70 32 3c 3c 31 34 20 7c 20 70  <28 | p2<<14 | p
9c850 34 20 28 75 6e 6d 61 73 6b 65 64 29 0a 20 20 69  4 (unmasked).  i
9c860 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20  f (!(a&0x80)).  
9c870 7b 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37 66  {.    a &= (0x7f
9c880 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 29  <<28)|(0x7f<<14)
9c890 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20 26  |(0x7f);.    b &
9c8a0 3d 20 28 30 78 37 66 3c 3c 32 38 29 7c 28 30 78  = (0x7f<<28)|(0x
9c8b0 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a  7f<<14)|(0x7f);.
9c8c0 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20      b = b<<7;.  
9c8d0 20 20 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20    *v = a | b;.  
9c8e0 20 20 72 65 74 75 72 6e 20 35 3b 0a 20 20 7d 0a    return 5;.  }.
9c8f0 0a 20 20 2f 2a 20 57 65 20 63 61 6e 20 6f 6e 6c  .  /* We can onl
9c900 79 20 72 65 61 63 68 20 74 68 69 73 20 70 6f 69  y reach this poi
9c910 6e 74 20 77 68 65 6e 20 72 65 61 64 69 6e 67 20  nt when reading 
9c920 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61  a corrupt databa
9c930 73 65 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 20 49  se.  ** file.  I
9c940 6e 20 74 68 61 74 20 63 61 73 65 20 77 65 20 61  n that case we a
9c950 72 65 20 6e 6f 74 20 69 6e 20 61 6e 79 20 68 75  re not in any hu
9c960 72 72 79 2e 20 20 55 73 65 20 74 68 65 20 28 72  rry.  Use the (r
9c970 65 6c 61 74 69 76 65 6c 79 0a 20 20 2a 2a 20 73  elatively.  ** s
9c980 6c 6f 77 29 20 67 65 6e 65 72 61 6c 2d 70 75 72  low) general-pur
9c990 70 6f 73 65 20 73 71 6c 69 74 65 33 47 65 74 56  pose sqlite3GetV
9c9a0 61 72 69 6e 74 28 29 20 72 6f 75 74 69 6e 65 20  arint() routine 
9c9b0 74 6f 20 65 78 74 72 61 63 74 20 74 68 65 0a 20  to extract the. 
9c9c0 20 2a 2a 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20   ** value. */.  
9c9d0 7b 0a 20 20 20 20 75 36 34 20 76 36 34 3b 0a 20  {.    u64 v64;. 
9c9e0 20 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 20 20 70     int n;..    p
9c9f0 20 2d 3d 20 34 3b 0a 20 20 20 20 6e 20 3d 20 73   -= 4;.    n = s
9ca00 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28  qlite3GetVarint(
9ca10 70 2c 20 26 76 36 34 29 3b 0a 20 20 20 20 61 73  p, &v64);.    as
9ca20 73 65 72 74 28 20 6e 3e 35 20 26 26 20 6e 3c 3d  sert( n>5 && n<=
9ca30 39 20 29 3b 0a 20 20 20 20 2a 76 20 3d 20 28 75  9 );.    *v = (u
9ca40 33 32 29 76 36 34 3b 0a 20 20 20 20 72 65 74 75  32)v64;.    retu
9ca50 72 6e 20 6e 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  rn n;.  }.}../*.
9ca60 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
9ca70 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 68  mber of bytes th
9ca80 61 74 20 77 69 6c 6c 20 62 65 20 6e 65 65 64 65  at will be neede
9ca90 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 67  d to store the g
9caa0 69 76 65 6e 0a 2a 2a 20 36 34 2d 62 69 74 20 69  iven.** 64-bit i
9cab0 6e 74 65 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  nteger..*/.SQLIT
9cac0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
9cad0 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75  lite3VarintLen(u
9cae0 36 34 20 76 29 7b 0a 20 20 69 6e 74 20 69 20 3d  64 v){.  int i =
9caf0 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 69 2b   0;.  do{.    i+
9cb00 2b 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0a  +;.    v >>= 7;.
9cb10 20 20 7d 77 68 69 6c 65 28 20 76 21 3d 30 20 26    }while( v!=0 &
9cb20 26 20 69 3c 39 20 29 3b 0a 20 20 72 65 74 75 72  & i<9 );.  retur
9cb30 6e 20 69 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52  n i;.}.../*.** R
9cb40 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 20 66  ead or write a f
9cb50 6f 75 72 2d 62 79 74 65 20 62 69 67 2d 65 6e 64  our-byte big-end
9cb60 69 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75  ian integer valu
9cb70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
9cb80 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33  VATE u32 sqlite3
9cb90 47 65 74 34 62 79 74 65 28 63 6f 6e 73 74 20 75  Get4byte(const u
9cba0 38 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20  8 *p){.  return 
9cbb0 28 70 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 70 5b  (p[0]<<24) | (p[
9cbc0 31 5d 3c 3c 31 36 29 20 7c 20 28 70 5b 32 5d 3c  1]<<16) | (p[2]<
9cbd0 3c 38 29 20 7c 20 70 5b 33 5d 3b 0a 7d 0a 53 51  <8) | p[3];.}.SQ
9cbe0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
9cbf0 64 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74  d sqlite3Put4byt
9cc00 65 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  e(unsigned char 
9cc10 2a 70 2c 20 75 33 32 20 76 29 7b 0a 20 20 70 5b  *p, u32 v){.  p[
9cc20 30 5d 20 3d 20 76 3e 3e 32 34 3b 0a 20 20 70 5b  0] = v>>24;.  p[
9cc30 31 5d 20 3d 20 76 3e 3e 31 36 3b 0a 20 20 70 5b  1] = v>>16;.  p[
9cc40 32 5d 20 3d 20 76 3e 3e 38 3b 0a 20 20 70 5b 33  2] = v>>8;.  p[3
9cc50 5d 20 3d 20 76 3b 0a 7d 0a 0a 0a 0a 23 69 66 20  ] = v;.}....#if 
9cc60 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
9cc70 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41  OMIT_BLOB_LITERA
9cc80 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  L) || defined(SQ
9cc90 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 29 0a  LITE_HAS_CODEC).
9cca0 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20  /*.** Translate 
9ccb0 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 6f 66  a single byte of
9ccc0 20 48 65 78 20 69 6e 74 6f 20 61 6e 20 69 6e 74   Hex into an int
9ccd0 65 67 65 72 2e 0a 2a 2a 20 54 68 69 73 20 72 6f  eger..** This ro
9cce0 75 74 69 6e 65 6e 20 6f 6e 6c 79 20 77 6f 72 6b  utinen only work
9ccf0 73 20 69 66 20 68 20 72 65 61 6c 6c 79 20 69 73  s if h really is
9cd00 20 61 20 76 61 6c 69 64 20 68 65 78 61 64 65 63   a valid hexadec
9cd10 69 6d 61 6c 0a 2a 2a 20 63 68 61 72 61 63 74 65  imal.** characte
9cd20 72 3a 20 20 30 2e 2e 39 61 2e 2e 66 41 2e 2e 46  r:  0..9a..fA..F
9cd30 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68  .*/.static int h
9cd40 65 78 54 6f 49 6e 74 28 69 6e 74 20 68 29 7b 0a  exToInt(int h){.
9cd50 20 20 61 73 73 65 72 74 28 20 28 68 3e 3d 27 30    assert( (h>='0
9cd60 27 20 26 26 20 68 3c 3d 27 39 27 29 20 7c 7c 20  ' && h<='9') || 
9cd70 20 28 68 3e 3d 27 61 27 20 26 26 20 68 3c 3d 27   (h>='a' && h<='
9cd80 66 27 29 20 7c 7c 20 20 28 68 3e 3d 27 41 27 20  f') ||  (h>='A' 
9cd90 26 26 20 68 3c 3d 27 46 27 29 20 29 3b 0a 23 69  && h<='F') );.#i
9cda0 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49  fdef SQLITE_ASCI
9cdb0 49 0a 20 20 68 20 2b 3d 20 39 2a 28 31 26 28 68  I.  h += 9*(1&(h
9cdc0 3e 3e 36 29 29 3b 0a 23 65 6e 64 69 66 0a 23 69  >>6));.#endif.#i
9cdd0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44  fdef SQLITE_EBCD
9cde0 49 43 0a 20 20 68 20 2b 3d 20 39 2a 28 31 26 7e  IC.  h += 9*(1&~
9cdf0 28 68 3e 3e 34 29 29 3b 0a 23 65 6e 64 69 66 0a  (h>>4));.#endif.
9ce00 20 20 72 65 74 75 72 6e 20 68 20 26 20 30 78 66    return h & 0xf
9ce10 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53  ;.}.#endif /* !S
9ce20 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f  QLITE_OMIT_BLOB_
9ce30 4c 49 54 45 52 41 4c 20 7c 7c 20 53 51 4c 49 54  LITERAL || SQLIT
9ce40 45 5f 48 41 53 5f 43 4f 44 45 43 20 2a 2f 0a 0a  E_HAS_CODEC */..
9ce50 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
9ce60 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49  ITE_OMIT_BLOB_LI
9ce70 54 45 52 41 4c 29 20 7c 7c 20 64 65 66 69 6e 65  TERAL) || define
9ce80 64 28 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44  d(SQLITE_HAS_COD
9ce90 45 43 29 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  EC)./*.** Conver
9cea0 74 20 61 20 42 4c 4f 42 20 6c 69 74 65 72 61 6c  t a BLOB literal
9ceb0 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 78 27   of the form "x'
9cec0 68 68 68 68 68 68 27 22 20 69 6e 74 6f 20 69 74  hhhhhh'" into it
9ced0 73 20 62 69 6e 61 72 79 0a 2a 2a 20 76 61 6c 75  s binary.** valu
9cee0 65 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69  e.  Return a poi
9cef0 6e 74 65 72 20 74 6f 20 69 74 73 20 62 69 6e 61  nter to its bina
9cf00 72 79 20 76 61 6c 75 65 2e 20 20 53 70 61 63 65  ry value.  Space
9cf10 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a 20   to hold the.** 
9cf20 62 69 6e 61 72 79 20 76 61 6c 75 65 20 68 61 73  binary value has
9cf30 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 66   been obtained f
9cf40 72 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d  rom malloc and m
9cf50 75 73 74 20 62 65 20 66 72 65 65 64 20 62 79 0a  ust be freed by.
9cf60 2a 2a 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 72  ** the calling r
9cf70 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54  outine..*/.SQLIT
9cf80 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
9cf90 73 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62  sqlite3HexToBlob
9cfa0 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
9cfb0 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
9cfc0 20 6e 29 7b 0a 20 20 63 68 61 72 20 2a 7a 42 6c   n){.  char *zBl
9cfd0 6f 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20  ob;.  int i;..  
9cfe0 7a 42 6c 6f 62 20 3d 20 28 63 68 61 72 20 2a 29  zBlob = (char *)
9cff0 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
9d000 61 77 28 64 62 2c 20 6e 2f 32 20 2b 20 31 29 3b  aw(db, n/2 + 1);
9d010 0a 20 20 6e 2d 2d 3b 0a 20 20 69 66 28 20 7a 42  .  n--;.  if( zB
9d020 6c 6f 62 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  lob ){.    for(i
9d030 3d 30 3b 20 69 3c 6e 3b 20 69 2b 3d 32 29 7b 0a  =0; i<n; i+=2){.
9d040 20 20 20 20 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d        zBlob[i/2]
9d050 20 3d 20 28 68 65 78 54 6f 49 6e 74 28 7a 5b 69   = (hexToInt(z[i
9d060 5d 29 3c 3c 34 29 20 7c 20 68 65 78 54 6f 49 6e  ])<<4) | hexToIn
9d070 74 28 7a 5b 69 2b 31 5d 29 3b 0a 20 20 20 20 7d  t(z[i+1]);.    }
9d080 0a 20 20 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d 20  .    zBlob[i/2] 
9d090 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 0;.  }.  retur
9d0a0 6e 20 7a 42 6c 6f 62 3b 0a 7d 0a 23 65 6e 64 69  n zBlob;.}.#endi
9d0b0 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49  f /* !SQLITE_OMI
9d0c0 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c  T_BLOB_LITERAL |
9d0d0 7c 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44  | SQLITE_HAS_COD
9d0e0 45 43 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68  EC */.../*.** Ch
9d0f0 61 6e 67 65 20 74 68 65 20 73 71 6c 69 74 65 2e  ange the sqlite.
9d100 6d 61 67 69 63 20 66 72 6f 6d 20 53 51 4c 49 54  magic from SQLIT
9d110 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 74 6f 20  E_MAGIC_OPEN to 
9d120 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53  SQLITE_MAGIC_BUS
9d130 59 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20  Y..** Return an 
9d140 65 72 72 6f 72 20 28 6e 6f 6e 2d 7a 65 72 6f 29  error (non-zero)
9d150 20 69 66 20 74 68 65 20 6d 61 67 69 63 20 77 61   if the magic wa
9d160 73 20 6e 6f 74 20 53 51 4c 49 54 45 5f 4d 41 47  s not SQLITE_MAG
9d170 49 43 5f 4f 50 45 4e 0a 2a 2a 20 77 68 65 6e 20  IC_OPEN.** when 
9d180 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
9d190 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  called..**.** Th
9d1a0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
9d1b0 6c 6c 65 64 20 77 68 65 6e 20 65 6e 74 65 72 69  lled when enteri
9d1c0 6e 67 20 61 6e 20 53 51 4c 69 74 65 20 41 50 49  ng an SQLite API
9d1d0 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 4d 41  .  The SQLITE_MA
9d1e0 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20 76 61 6c 75  GIC_OPEN.** valu
9d1f0 65 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74  e indicates that
9d200 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
9d210 6e 6e 65 63 74 69 6f 6e 20 70 61 73 73 65 64 20  nnection passed 
9d220 69 6e 74 6f 20 74 68 65 20 41 50 49 20 69 73 0a  into the API is.
9d230 2a 2a 20 6f 70 65 6e 20 61 6e 64 20 69 73 20 6e  ** open and is n
9d240 6f 74 20 62 65 69 6e 67 20 75 73 65 64 20 62 79  ot being used by
9d250 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 2e   another thread.
9d260 20 20 42 79 20 63 68 61 6e 67 69 6e 67 20 74 68    By changing th
9d270 65 20 76 61 6c 75 65 0a 2a 2a 20 74 6f 20 53 51  e value.** to SQ
9d280 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20  LITE_MAGIC_BUSY 
9d290 77 65 20 69 6e 64 69 63 61 74 65 20 74 68 61 74  we indicate that
9d2a0 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20   the connection 
9d2b0 69 73 20 69 6e 20 75 73 65 2e 0a 2a 2a 20 73 71  is in use..** sq
9d2c0 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 29  lite3SafetyOff()
9d2d0 20 62 65 6c 6f 77 20 77 69 6c 6c 20 63 68 61 6e   below will chan
9d2e0 67 65 20 74 68 65 20 76 61 6c 75 65 20 62 61 63  ge the value bac
9d2f0 6b 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49  k to SQLITE_MAGI
9d300 43 5f 4f 50 45 4e 0a 2a 2a 20 77 68 65 6e 20 74  C_OPEN.** when t
9d310 68 65 20 41 50 49 20 65 78 69 74 73 2e 20 0a 2a  he API exits. .*
9d320 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
9d330 65 20 69 73 20 61 20 61 74 74 65 6d 70 74 20 74  e is a attempt t
9d340 6f 20 64 65 74 65 63 74 20 69 66 20 74 77 6f 20  o detect if two 
9d350 74 68 72 65 61 64 73 20 75 73 65 20 74 68 65 0a  threads use the.
9d360 2a 2a 20 73 61 6d 65 20 73 71 6c 69 74 65 2a 20  ** same sqlite* 
9d370 70 6f 69 6e 74 65 72 20 61 74 20 74 68 65 20 73  pointer at the s
9d380 61 6d 65 20 74 69 6d 65 2e 20 20 54 68 65 72 65  ame time.  There
9d390 20 69 73 20 61 20 72 61 63 65 20 0a 2a 2a 20 63   is a race .** c
9d3a0 6f 6e 64 69 74 69 6f 6e 20 73 6f 20 69 74 20 69  ondition so it i
9d3b0 73 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20  s possible that 
9d3c0 74 68 65 20 65 72 72 6f 72 20 69 73 20 6e 6f 74  the error is not
9d3d0 20 64 65 74 65 63 74 65 64 2e 0a 2a 2a 20 42 75   detected..** Bu
9d3e0 74 20 75 73 75 61 6c 6c 79 20 74 68 65 20 70 72  t usually the pr
9d3f0 6f 62 6c 65 6d 20 77 69 6c 6c 20 62 65 20 73 65  oblem will be se
9d400 65 6e 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20  en.  The result 
9d410 77 69 6c 6c 20 62 65 20 61 6e 0a 2a 2a 20 65 72  will be an.** er
9d420 72 6f 72 20 77 68 69 63 68 20 63 61 6e 20 62 65  ror which can be
9d430 20 75 73 65 64 20 74 6f 20 64 65 62 75 67 20 74   used to debug t
9d440 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74  he application t
9d450 68 61 74 20 69 73 0a 2a 2a 20 75 73 69 6e 67 20  hat is.** using 
9d460 53 51 4c 69 74 65 20 69 6e 63 6f 72 72 65 63 74  SQLite incorrect
9d470 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74  ly..**.** Ticket
9d480 20 23 32 30 32 3a 20 20 49 66 20 64 62 2d 3e 6d   #202:  If db->m
9d490 61 67 69 63 20 69 73 20 6e 6f 74 20 61 20 76 61  agic is not a va
9d4a0 6c 69 64 20 6f 70 65 6e 20 76 61 6c 75 65 2c 20  lid open value, 
9d4b0 74 61 6b 65 20 63 61 72 65 20 6e 6f 74 0a 2a 2a  take care not.**
9d4c0 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 64   to modify the d
9d4d0 62 20 73 74 72 75 63 74 75 72 65 20 61 74 20 61  b structure at a
9d4e0 6c 6c 2e 20 20 49 74 20 63 6f 75 6c 64 20 62 65  ll.  It could be
9d4f0 20 74 68 61 74 20 64 62 20 69 73 20 61 20 73 74   that db is a st
9d500 61 6c 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20  ale.** pointer. 
9d510 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
9d520 20 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68 61   it could be tha
9d530 74 20 74 68 65 72 65 20 68 61 73 20 62 65 65 6e  t there has been
9d540 20 61 20 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c   a prior.** call
9d550 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73   to sqlite3_clos
9d560 65 28 64 62 29 20 61 6e 64 20 64 62 20 68 61 73  e(db) and db has
9d570 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65   been deallocate
9d580 64 2e 20 20 41 6e 64 20 77 65 20 64 6f 0a 2a 2a  d.  And we do.**
9d590 20 6e 6f 74 20 77 61 6e 74 20 74 6f 20 77 72 69   not want to wri
9d5a0 74 65 20 69 6e 74 6f 20 64 65 61 6c 6c 6f 63 61  te into dealloca
9d5b0 74 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 23  ted memory..*/.#
9d5c0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
9d5d0 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  UG.SQLITE_PRIVAT
9d5e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66  E int sqlite3Saf
9d5f0 65 74 79 4f 6e 28 73 71 6c 69 74 65 33 20 2a 64  etyOn(sqlite3 *d
9d600 62 29 7b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  b){.  if( db->ma
9d610 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49  gic==SQLITE_MAGI
9d620 43 5f 4f 50 45 4e 20 29 7b 0a 20 20 20 20 64 62  C_OPEN ){.    db
9d630 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45  ->magic = SQLITE
9d640 5f 4d 41 47 49 43 5f 42 55 53 59 3b 0a 20 20 20  _MAGIC_BUSY;.   
9d650 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
9d660 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
9d670 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 72 65  mutex) );.    re
9d680 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20  turn 0;.  }else 
9d690 69 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53  if( db->magic==S
9d6a0 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59  QLITE_MAGIC_BUSY
9d6b0 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69   ){.    db->magi
9d6c0 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  c = SQLITE_MAGIC
9d6d0 5f 45 52 52 4f 52 3b 0a 20 20 20 20 64 62 2d 3e  _ERROR;.    db->
9d6e0 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64  u1.isInterrupted
9d6f0 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 1;.  }.  retu
9d700 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  rn 1;.}.#endif..
9d710 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
9d720 20 6d 61 67 69 63 20 66 72 6f 6d 20 53 51 4c 49   magic from SQLI
9d730 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 74 6f  TE_MAGIC_BUSY to
9d740 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50   SQLITE_MAGIC_OP
9d750 45 4e 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e  EN..** Return an
9d760 20 65 72 72 6f 72 20 28 6e 6f 6e 2d 7a 65 72 6f   error (non-zero
9d770 29 20 69 66 20 74 68 65 20 6d 61 67 69 63 20 77  ) if the magic w
9d780 61 73 20 6e 6f 74 20 53 51 4c 49 54 45 5f 4d 41  as not SQLITE_MA
9d790 47 49 43 5f 42 55 53 59 0a 2a 2a 20 77 68 65 6e  GIC_BUSY.** when
9d7a0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
9d7b0 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 66 64   called..*/.#ifd
9d7c0 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
9d7d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
9d7e0 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  nt sqlite3Safety
9d7f0 4f 66 66 28 73 71 6c 69 74 65 33 20 2a 64 62 29  Off(sqlite3 *db)
9d800 7b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 67 69  {.  if( db->magi
9d810 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  c==SQLITE_MAGIC_
9d820 42 55 53 59 20 29 7b 0a 20 20 20 20 64 62 2d 3e  BUSY ){.    db->
9d830 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d  magic = SQLITE_M
9d840 41 47 49 43 5f 4f 50 45 4e 3b 0a 20 20 20 20 61  AGIC_OPEN;.    a
9d850 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
9d860 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75  utex_held(db->mu
9d870 74 65 78 29 20 29 3b 0a 20 20 20 20 72 65 74 75  tex) );.    retu
9d880 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  rn 0;.  }else{. 
9d890 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53     db->magic = S
9d8a0 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f  QLITE_MAGIC_ERRO
9d8b0 52 3b 0a 20 20 20 20 64 62 2d 3e 75 31 2e 69 73  R;.    db->u1.is
9d8c0 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20 31 3b  Interrupted = 1;
9d8d0 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
9d8e0 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a   }.}.#endif../*.
9d8f0 2a 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65  ** Check to make
9d900 20 73 75 72 65 20 77 65 20 68 61 76 65 20 61 20   sure we have a 
9d910 76 61 6c 69 64 20 64 62 20 70 6f 69 6e 74 65 72  valid db pointer
9d920 2e 20 20 54 68 69 73 20 74 65 73 74 20 69 73 20  .  This test is 
9d930 6e 6f 74 0a 2a 2a 20 66 6f 6f 6c 70 72 6f 6f 66  not.** foolproof
9d940 20 62 75 74 20 69 74 20 64 6f 65 73 20 70 72 6f   but it does pro
9d950 76 69 64 65 20 73 6f 6d 65 20 6d 65 61 73 75 72  vide some measur
9d960 65 20 6f 66 20 70 72 6f 74 65 63 74 69 6f 6e 20  e of protection 
9d970 61 67 61 69 6e 73 74 0a 2a 2a 20 6d 69 73 75 73  against.** misus
9d980 65 20 6f 66 20 74 68 65 20 69 6e 74 65 72 66 61  e of the interfa
9d990 63 65 20 73 75 63 68 20 61 73 20 70 61 73 73 69  ce such as passi
9d9a0 6e 67 20 69 6e 20 64 62 20 70 6f 69 6e 74 65 72  ng in db pointer
9d9b0 73 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 4e 55  s that are.** NU
9d9c0 4c 4c 20 6f 72 20 77 68 69 63 68 20 68 61 76 65  LL or which have
9d9d0 20 62 65 65 6e 20 70 72 65 76 69 6f 75 73 6c 79   been previously
9d9e0 20 63 6c 6f 73 65 64 2e 20 20 49 66 20 74 68 69   closed.  If thi
9d9f0 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
9da00 73 0a 2a 2a 20 31 20 69 74 20 6d 65 61 6e 73 20  s.** 1 it means 
9da10 74 68 61 74 20 74 68 65 20 64 62 20 70 6f 69 6e  that the db poin
9da20 74 65 72 20 69 73 20 76 61 6c 69 64 20 61 6e 64  ter is valid and
9da30 20 30 20 69 66 20 69 74 20 73 68 6f 75 6c 64 20   0 if it should 
9da40 6e 6f 74 20 62 65 0a 2a 2a 20 64 65 72 65 66 65  not be.** derefe
9da50 72 65 6e 63 65 64 20 66 6f 72 20 61 6e 79 20 72  renced for any r
9da60 65 61 73 6f 6e 2e 20 20 54 68 65 20 63 61 6c 6c  eason.  The call
9da70 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f  ing function sho
9da80 75 6c 64 20 69 6e 76 6f 6b 65 0a 2a 2a 20 53 51  uld invoke.** SQ
9da90 4c 49 54 45 5f 4d 49 53 55 53 45 20 69 6d 6d 65  LITE_MISUSE imme
9daa0 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 73  diately..**.** s
9dab0 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63  qlite3SafetyChec
9dac0 6b 4f 6b 28 29 20 72 65 71 75 69 72 65 73 20 74  kOk() requires t
9dad0 68 61 74 20 74 68 65 20 64 62 20 70 6f 69 6e 74  hat the db point
9dae0 65 72 20 62 65 20 76 61 6c 69 64 20 66 6f 72 0a  er be valid for.
9daf0 2a 2a 20 75 73 65 2e 20 20 73 71 6c 69 74 65 33  ** use.  sqlite3
9db00 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f  SafetyCheckSickO
9db10 72 4f 6b 28 29 20 61 6c 6c 6f 77 73 20 61 20 64  rOk() allows a d
9db20 62 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 66  b pointer that f
9db30 61 69 6c 65 64 20 74 6f 0a 2a 2a 20 6f 70 65 6e  ailed to.** open
9db40 20 70 72 6f 70 65 72 6c 79 20 61 6e 64 20 69 73   properly and is
9db50 20 6e 6f 74 20 66 69 74 20 66 6f 72 20 67 65 6e   not fit for gen
9db60 65 72 61 6c 20 75 73 65 20 62 75 74 20 77 68 69  eral use but whi
9db70 63 68 20 63 61 6e 20 62 65 0a 2a 2a 20 75 73 65  ch can be.** use
9db80 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74  d as an argument
9db90 20 74 6f 20 73 71 6c 69 74 65 33 5f 65 72 72 6d   to sqlite3_errm
9dba0 73 67 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f  sg() or sqlite3_
9dbb0 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49  close()..*/.SQLI
9dbc0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
9dbd0 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63  qlite3SafetyChec
9dbe0 6b 4f 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29  kOk(sqlite3 *db)
9dbf0 7b 0a 20 20 69 6e 74 20 6d 61 67 69 63 3b 0a 20  {.  int magic;. 
9dc00 20 69 66 28 20 64 62 3d 3d 30 20 29 20 72 65 74   if( db==0 ) ret
9dc10 75 72 6e 20 30 3b 0a 20 20 6d 61 67 69 63 20 3d  urn 0;.  magic =
9dc20 20 64 62 2d 3e 6d 61 67 69 63 3b 0a 20 20 69 66   db->magic;.  if
9dc30 28 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f  ( magic!=SQLITE_
9dc40 4d 41 47 49 43 5f 4f 50 45 4e 20 26 26 0a 20 20  MAGIC_OPEN &&.  
9dc50 20 20 20 20 6d 61 67 69 63 21 3d 53 51 4c 49 54      magic!=SQLIT
9dc60 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 20 72  E_MAGIC_BUSY ) r
9dc70 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 75 72  eturn 0;.  retur
9dc80 6e 20 31 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  n 1;.}.SQLITE_PR
9dc90 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
9dca0 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b  3SafetyCheckSick
9dcb0 4f 72 4f 6b 28 73 71 6c 69 74 65 33 20 2a 64 62  OrOk(sqlite3 *db
9dcc0 29 7b 0a 20 20 69 6e 74 20 6d 61 67 69 63 3b 0a  ){.  int magic;.
9dcd0 20 20 69 66 28 20 64 62 3d 3d 30 20 29 20 72 65    if( db==0 ) re
9dce0 74 75 72 6e 20 30 3b 0a 20 20 6d 61 67 69 63 20  turn 0;.  magic 
9dcf0 3d 20 64 62 2d 3e 6d 61 67 69 63 3b 0a 20 20 69  = db->magic;.  i
9dd00 66 28 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45  f( magic!=SQLITE
9dd10 5f 4d 41 47 49 43 5f 53 49 43 4b 20 26 26 0a 20  _MAGIC_SICK &&. 
9dd20 20 20 20 20 20 6d 61 67 69 63 21 3d 53 51 4c 49       magic!=SQLI
9dd30 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 26 26  TE_MAGIC_OPEN &&
9dd40 0a 20 20 20 20 20 20 6d 61 67 69 63 21 3d 53 51  .      magic!=SQ
9dd50 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20  LITE_MAGIC_BUSY 
9dd60 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65  ) return 0;.  re
9dd70 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  turn 1;.}../****
9dd80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
9dd90 66 20 75 74 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f util.c *******
9dda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9ddb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9ddc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
9ddd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
9dde0 20 66 69 6c 65 20 68 61 73 68 2e 63 20 2a 2a 2a   file hash.c ***
9ddf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9de00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9de10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
9de20 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
9de30 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  22.**.** The aut
9de40 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
9de50 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
9de60 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
9de70 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
9de80 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
9de90 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
9dea0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
9deb0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
9dec0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
9ded0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
9dee0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
9def0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
9df00 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
9df10 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
9df20 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
9df30 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
9df40 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
9df50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9df60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9df70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9df80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9df90 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65  *.** This is the
9dfa0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
9dfb0 6f 66 20 67 65 6e 65 72 69 63 20 68 61 73 68 2d  of generic hash-
9dfc0 74 61 62 6c 65 73 0a 2a 2a 20 75 73 65 64 20 69  tables.** used i
9dfd0 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20  n SQLite..**.** 
9dfe0 24 49 64 3a 20 68 61 73 68 2e 63 2c 76 20 31 2e  $Id: hash.c,v 1.
9dff0 32 38 20 32 30 30 38 2f 30 35 2f 31 33 20 31 33  28 2008/05/13 13
9e000 3a 32 37 3a 33 34 20 64 72 68 20 45 78 70 20 24  :27:34 drh Exp $
9e010 0a 2a 2f 0a 0a 2f 2a 20 54 75 72 6e 20 62 75 6c  .*/../* Turn bul
9e020 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f 20 61 20  k memory into a 
9e030 68 61 73 68 20 74 61 62 6c 65 20 6f 62 6a 65 63  hash table objec
9e040 74 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e  t by initializin
9e050 67 20 74 68 65 0a 2a 2a 20 66 69 65 6c 64 73 20  g the.** fields 
9e060 6f 66 20 74 68 65 20 48 61 73 68 20 73 74 72 75  of the Hash stru
9e070 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e  cture..**.** "pN
9e080 65 77 22 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ew" is a pointer
9e090 20 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 62   to the hash tab
9e0a0 6c 65 20 74 68 61 74 20 69 73 20 74 6f 20 62 65  le that is to be
9e0b0 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a   initialized..**
9e0c0 20 6b 65 79 43 6c 61 73 73 20 69 73 20 6f 6e 65   keyClass is one
9e0d0 20 6f 66 20 74 68 65 20 63 6f 6e 73 74 61 6e 74   of the constant
9e0e0 73 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 49 4e  s SQLITE_HASH_IN
9e0f0 54 2c 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 50  T, SQLITE_HASH_P
9e100 4f 49 4e 54 45 52 2c 0a 2a 2a 20 53 51 4c 49 54  OINTER,.** SQLIT
9e110 45 5f 48 41 53 48 5f 42 49 4e 41 52 59 2c 20 6f  E_HASH_BINARY, o
9e120 72 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 53 54  r SQLITE_HASH_ST
9e130 52 49 4e 47 2e 20 20 54 68 65 20 76 61 6c 75 65  RING.  The value
9e140 20 6f 66 20 6b 65 79 43 6c 61 73 73 20 0a 2a 2a   of keyClass .**
9e150 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 61 74   determines what
9e160 20 6b 69 6e 64 20 6f 66 20 6b 65 79 20 74 68 65   kind of key the
9e170 20 68 61 73 68 20 74 61 62 6c 65 20 77 69 6c 6c   hash table will
9e180 20 75 73 65 2e 20 20 22 63 6f 70 79 4b 65 79 22   use.  "copyKey"
9e190 20 69 73 0a 2a 2a 20 74 72 75 65 20 69 66 20 74   is.** true if t
9e1a0 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 73 68  he hash table sh
9e1b0 6f 75 6c 64 20 6d 61 6b 65 20 69 74 73 20 6f 77  ould make its ow
9e1c0 6e 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f  n private copy o
9e1d0 66 20 6b 65 79 73 20 61 6e 64 0a 2a 2a 20 66 61  f keys and.** fa
9e1e0 6c 73 65 20 69 66 20 69 74 20 73 68 6f 75 6c 64  lse if it should
9e1f0 20 6a 75 73 74 20 75 73 65 20 74 68 65 20 73 75   just use the su
9e200 70 70 6c 69 65 64 20 70 6f 69 6e 74 65 72 2e 20  pplied pointer. 
9e210 20 43 6f 70 79 4b 65 79 20 6f 6e 6c 79 20 6d 61   CopyKey only ma
9e220 6b 65 73 0a 2a 2a 20 73 65 6e 73 65 20 66 6f 72  kes.** sense for
9e230 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 53 54 52   SQLITE_HASH_STR
9e240 49 4e 47 20 61 6e 64 20 53 51 4c 49 54 45 5f 48  ING and SQLITE_H
9e250 41 53 48 5f 42 49 4e 41 52 59 20 61 6e 64 20 69  ASH_BINARY and i
9e260 73 20 69 67 6e 6f 72 65 64 0a 2a 2a 20 66 6f 72  s ignored.** for
9e270 20 6f 74 68 65 72 20 6b 65 79 20 63 6c 61 73 73   other key class
9e280 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  es..*/.SQLITE_PR
9e290 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
9e2a0 65 33 48 61 73 68 49 6e 69 74 28 48 61 73 68 20  e3HashInit(Hash 
9e2b0 2a 70 4e 65 77 2c 20 69 6e 74 20 6b 65 79 43 6c  *pNew, int keyCl
9e2c0 61 73 73 2c 20 69 6e 74 20 63 6f 70 79 4b 65 79  ass, int copyKey
9e2d0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65  ){.  assert( pNe
9e2e0 77 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  w!=0 );.  assert
9e2f0 28 20 6b 65 79 43 6c 61 73 73 3e 3d 53 51 4c 49  ( keyClass>=SQLI
9e300 54 45 5f 48 41 53 48 5f 53 54 52 49 4e 47 20 26  TE_HASH_STRING &
9e310 26 20 6b 65 79 43 6c 61 73 73 3c 3d 53 51 4c 49  & keyClass<=SQLI
9e320 54 45 5f 48 41 53 48 5f 42 49 4e 41 52 59 20 29  TE_HASH_BINARY )
9e330 3b 0a 20 20 70 4e 65 77 2d 3e 6b 65 79 43 6c 61  ;.  pNew->keyCla
9e340 73 73 20 3d 20 6b 65 79 43 6c 61 73 73 3b 0a 23  ss = keyClass;.#
9e350 69 66 20 30 0a 20 20 69 66 28 20 6b 65 79 43 6c  if 0.  if( keyCl
9e360 61 73 73 3d 3d 53 51 4c 49 54 45 5f 48 41 53 48  ass==SQLITE_HASH
9e370 5f 50 4f 49 4e 54 45 52 20 7c 7c 20 6b 65 79 43  _POINTER || keyC
9e380 6c 61 73 73 3d 3d 53 51 4c 49 54 45 5f 48 41 53  lass==SQLITE_HAS
9e390 48 5f 49 4e 54 20 29 20 63 6f 70 79 4b 65 79 20  H_INT ) copyKey 
9e3a0 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 70 4e  = 0;.#endif.  pN
9e3b0 65 77 2d 3e 63 6f 70 79 4b 65 79 20 3d 20 63 6f  ew->copyKey = co
9e3c0 70 79 4b 65 79 3b 0a 20 20 70 4e 65 77 2d 3e 66  pyKey;.  pNew->f
9e3d0 69 72 73 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77  irst = 0;.  pNew
9e3e0 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 70  ->count = 0;.  p
9e3f0 4e 65 77 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b  New->htsize = 0;
9e400 0a 20 20 70 4e 65 77 2d 3e 68 74 20 3d 20 30 3b  .  pNew->ht = 0;
9e410 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 6c  .}../* Remove al
9e420 6c 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 61  l entries from a
9e430 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 52 65   hash table.  Re
9e440 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79  claim all memory
9e450 2e 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20 72  ..** Call this r
9e460 6f 75 74 69 6e 65 20 74 6f 20 64 65 6c 65 74 65  outine to delete
9e470 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f 72   a hash table or
9e480 20 74 6f 20 72 65 73 65 74 20 61 20 68 61 73 68   to reset a hash
9e490 20 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 74 68 65   table.** to the
9e4a0 20 65 6d 70 74 79 20 73 74 61 74 65 2e 0a 2a 2f   empty state..*/
9e4b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
9e4c0 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68  void sqlite3Hash
9e4d0 43 6c 65 61 72 28 48 61 73 68 20 2a 70 48 29 7b  Clear(Hash *pH){
9e4e0 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65  .  HashElem *ele
9e4f0 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f  m;         /* Fo
9e500 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61  r looping over a
9e510 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74  ll elements of t
9e520 68 65 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 61  he table */..  a
9e530 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0a  ssert( pH!=0 );.
9e540 20 20 65 6c 65 6d 20 3d 20 70 48 2d 3e 66 69 72    elem = pH->fir
9e550 73 74 3b 0a 20 20 70 48 2d 3e 66 69 72 73 74 20  st;.  pH->first 
9e560 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  = 0;.  sqlite3_f
9e570 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70  ree(pH->ht);.  p
9e580 48 2d 3e 68 74 20 3d 20 30 3b 0a 20 20 70 48 2d  H->ht = 0;.  pH-
9e590 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 77  >htsize = 0;.  w
9e5a0 68 69 6c 65 28 20 65 6c 65 6d 20 29 7b 0a 20 20  hile( elem ){.  
9e5b0 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78 74    HashElem *next
9e5c0 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65  _elem = elem->ne
9e5d0 78 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d 3e  xt;.    if( pH->
9e5e0 63 6f 70 79 4b 65 79 20 26 26 20 65 6c 65 6d 2d  copyKey && elem-
9e5f0 3e 70 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 73  >pKey ){.      s
9e600 71 6c 69 74 65 33 5f 66 72 65 65 28 65 6c 65 6d  qlite3_free(elem
9e610 2d 3e 70 4b 65 79 29 3b 0a 20 20 20 20 7d 0a 20  ->pKey);.    }. 
9e620 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
9e630 65 6c 65 6d 29 3b 0a 20 20 20 20 65 6c 65 6d 20  elem);.    elem 
9e640 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0a 20 20 7d  = next_elem;.  }
9e650 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20 3d 20 30  .  pH->count = 0
9e660 3b 0a 7d 0a 0a 23 69 66 20 30 20 2f 2a 20 4e 4f  ;.}..#if 0 /* NO
9e670 54 20 55 53 45 44 20 2a 2f 0a 2f 2a 0a 2a 2a 20  T USED */./*.** 
9e680 48 61 73 68 20 61 6e 64 20 63 6f 6d 70 61 72 69  Hash and compari
9e690 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 77 68  son functions wh
9e6a0 65 6e 20 74 68 65 20 6d 6f 64 65 20 69 73 20 53  en the mode is S
9e6b0 51 4c 49 54 45 5f 48 41 53 48 5f 49 4e 54 0a 2a  QLITE_HASH_INT.*
9e6c0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 74  /.static int int
9e6d0 48 61 73 68 28 63 6f 6e 73 74 20 76 6f 69 64 20  Hash(const void 
9e6e0 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29  *pKey, int nKey)
9e6f0 7b 0a 20 20 72 65 74 75 72 6e 20 6e 4b 65 79 20  {.  return nKey 
9e700 5e 20 28 6e 4b 65 79 3c 3c 38 29 20 5e 20 28 6e  ^ (nKey<<8) ^ (n
9e710 4b 65 79 3e 3e 38 29 3b 0a 7d 0a 73 74 61 74 69  Key>>8);.}.stati
9e720 63 20 69 6e 74 20 69 6e 74 43 6f 6d 70 61 72 65  c int intCompare
9e730 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65  (const void *pKe
9e740 79 31 2c 20 69 6e 74 20 6e 31 2c 20 63 6f 6e 73  y1, int n1, cons
9e750 74 20 76 6f 69 64 20 2a 70 4b 65 79 32 2c 20 69  t void *pKey2, i
9e760 6e 74 20 6e 32 29 7b 0a 20 20 72 65 74 75 72 6e  nt n2){.  return
9e770 20 6e 32 20 2d 20 6e 31 3b 0a 7d 0a 23 65 6e 64   n2 - n1;.}.#end
9e780 69 66 0a 0a 23 69 66 20 30 20 2f 2a 20 4e 4f 54  if..#if 0 /* NOT
9e790 20 55 53 45 44 20 2a 2f 0a 2f 2a 0a 2a 2a 20 48   USED */./*.** H
9e7a0 61 73 68 20 61 6e 64 20 63 6f 6d 70 61 72 69 73  ash and comparis
9e7b0 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 77 68 65  on functions whe
9e7c0 6e 20 74 68 65 20 6d 6f 64 65 20 69 73 20 53 51  n the mode is SQ
9e7d0 4c 49 54 45 5f 48 41 53 48 5f 50 4f 49 4e 54 45  LITE_HASH_POINTE
9e7e0 52 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  R.*/.static int 
9e7f0 70 74 72 48 61 73 68 28 63 6f 6e 73 74 20 76 6f  ptrHash(const vo
9e800 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b  id *pKey, int nK
9e810 65 79 29 7b 0a 20 20 75 70 74 72 20 78 20 3d 20  ey){.  uptr x = 
9e820 41 64 64 72 28 70 4b 65 79 29 3b 0a 20 20 72 65  Addr(pKey);.  re
9e830 74 75 72 6e 20 78 20 5e 20 28 78 3c 3c 38 29 20  turn x ^ (x<<8) 
9e840 5e 20 28 78 3e 3e 38 29 3b 0a 7d 0a 73 74 61 74  ^ (x>>8);.}.stat
9e850 69 63 20 69 6e 74 20 70 74 72 43 6f 6d 70 61 72  ic int ptrCompar
9e860 65 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b  e(const void *pK
9e870 65 79 31 2c 20 69 6e 74 20 6e 31 2c 20 63 6f 6e  ey1, int n1, con
9e880 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32 2c 20  st void *pKey2, 
9e890 69 6e 74 20 6e 32 29 7b 0a 20 20 69 66 28 20 70  int n2){.  if( p
9e8a0 4b 65 79 31 3d 3d 70 4b 65 79 32 20 29 20 72 65  Key1==pKey2 ) re
9e8b0 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 4b  turn 0;.  if( pK
9e8c0 65 79 31 3c 70 4b 65 79 32 20 29 20 72 65 74 75  ey1<pKey2 ) retu
9e8d0 72 6e 20 2d 31 3b 0a 20 20 72 65 74 75 72 6e 20  rn -1;.  return 
9e8e0 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  1;.}.#endif../*.
9e8f0 2a 2a 20 48 61 73 68 20 61 6e 64 20 63 6f 6d 70  ** Hash and comp
9e900 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 73  arison functions
9e910 20 77 68 65 6e 20 74 68 65 20 6d 6f 64 65 20 69   when the mode i
9e920 73 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 53 54  s SQLITE_HASH_ST
9e930 52 49 4e 47 0a 2a 2f 0a 73 74 61 74 69 63 20 69  RING.*/.static i
9e940 6e 74 20 73 74 72 48 61 73 68 28 63 6f 6e 73 74  nt strHash(const
9e950 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74   void *pKey, int
9e960 20 6e 4b 65 79 29 7b 0a 20 20 63 6f 6e 73 74 20   nKey){.  const 
9e970 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e 73 74  char *z = (const
9e980 20 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a 20 20   char *)pKey;.  
9e990 69 6e 74 20 68 20 3d 20 30 3b 0a 20 20 69 66 28  int h = 0;.  if(
9e9a0 20 6e 4b 65 79 3c 3d 30 20 29 20 6e 4b 65 79 20   nKey<=0 ) nKey 
9e9b0 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20 77  = strlen(z);.  w
9e9c0 68 69 6c 65 28 20 6e 4b 65 79 20 3e 20 30 20 20  hile( nKey > 0  
9e9d0 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c 33  ){.    h = (h<<3
9e9e0 29 20 5e 20 68 20 5e 20 73 71 6c 69 74 65 33 55  ) ^ h ^ sqlite3U
9e9f0 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73  pperToLower[(uns
9ea00 69 67 6e 65 64 20 63 68 61 72 29 2a 7a 2b 2b 5d  igned char)*z++]
9ea10 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d 3b 0a 20 20  ;.    nKey--;.  
9ea20 7d 0a 20 20 72 65 74 75 72 6e 20 68 20 26 20 30  }.  return h & 0
9ea30 78 37 66 66 66 66 66 66 66 3b 0a 7d 0a 73 74 61  x7fffffff;.}.sta
9ea40 74 69 63 20 69 6e 74 20 73 74 72 43 6f 6d 70 61  tic int strCompa
9ea50 72 65 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  re(const void *p
9ea60 4b 65 79 31 2c 20 69 6e 74 20 6e 31 2c 20 63 6f  Key1, int n1, co
9ea70 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32 2c  nst void *pKey2,
9ea80 20 69 6e 74 20 6e 32 29 7b 0a 20 20 69 66 28 20   int n2){.  if( 
9ea90 6e 31 21 3d 6e 32 20 29 20 72 65 74 75 72 6e 20  n1!=n2 ) return 
9eaa0 31 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  1;.  return sqli
9eab0 74 65 33 53 74 72 4e 49 43 6d 70 28 28 63 6f 6e  te3StrNICmp((con
9eac0 73 74 20 63 68 61 72 2a 29 70 4b 65 79 31 2c 28  st char*)pKey1,(
9ead0 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 4b 65 79  const char*)pKey
9eae0 32 2c 6e 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  2,n1);.}../*.** 
9eaf0 48 61 73 68 20 61 6e 64 20 63 6f 6d 70 61 72 69  Hash and compari
9eb00 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 77 68  son functions wh
9eb10 65 6e 20 74 68 65 20 6d 6f 64 65 20 69 73 20 53  en the mode is S
9eb20 51 4c 49 54 45 5f 48 41 53 48 5f 42 49 4e 41 52  QLITE_HASH_BINAR
9eb30 59 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  Y.*/.static int 
9eb40 62 69 6e 48 61 73 68 28 63 6f 6e 73 74 20 76 6f  binHash(const vo
9eb50 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b  id *pKey, int nK
9eb60 65 79 29 7b 0a 20 20 69 6e 74 20 68 20 3d 20 30  ey){.  int h = 0
9eb70 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
9eb80 7a 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  z = (const char 
9eb90 2a 29 70 4b 65 79 3b 0a 20 20 77 68 69 6c 65 28  *)pKey;.  while(
9eba0 20 6e 4b 65 79 2d 2d 20 3e 20 30 20 29 7b 0a 20   nKey-- > 0 ){. 
9ebb0 20 20 20 68 20 3d 20 28 68 3c 3c 33 29 20 5e 20     h = (h<<3) ^ 
9ebc0 68 20 5e 20 2a 28 7a 2b 2b 29 3b 0a 20 20 7d 0a  h ^ *(z++);.  }.
9ebd0 20 20 72 65 74 75 72 6e 20 68 20 26 20 30 78 37    return h & 0x7
9ebe0 66 66 66 66 66 66 66 3b 0a 7d 0a 73 74 61 74 69  fffffff;.}.stati
9ebf0 63 20 69 6e 74 20 62 69 6e 43 6f 6d 70 61 72 65  c int binCompare
9ec00 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65  (const void *pKe
9ec10 79 31 2c 20 69 6e 74 20 6e 31 2c 20 63 6f 6e 73  y1, int n1, cons
9ec20 74 20 76 6f 69 64 20 2a 70 4b 65 79 32 2c 20 69  t void *pKey2, i
9ec30 6e 74 20 6e 32 29 7b 0a 20 20 69 66 28 20 6e 31  nt n2){.  if( n1
9ec40 21 3d 6e 32 20 29 20 72 65 74 75 72 6e 20 31 3b  !=n2 ) return 1;
9ec50 0a 20 20 72 65 74 75 72 6e 20 6d 65 6d 63 6d 70  .  return memcmp
9ec60 28 70 4b 65 79 31 2c 70 4b 65 79 32 2c 6e 31 29  (pKey1,pKey2,n1)
9ec70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
9ec80 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
9ec90 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 68  he appropriate h
9eca0 61 73 68 20 66 75 6e 63 74 69 6f 6e 20 67 69 76  ash function giv
9ecb0 65 6e 20 74 68 65 20 6b 65 79 20 63 6c 61 73 73  en the key class
9ecc0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 20 73 79  ..**.** The C sy
9ecd0 6e 74 61 78 20 69 6e 20 74 68 69 73 20 66 75 6e  ntax in this fun
9ece0 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e  ction definition
9ecf0 20 6d 61 79 20 62 65 20 75 6e 66 61 6d 69 6c 61   may be unfamila
9ed00 72 20 74 6f 20 73 6f 6d 65 20 0a 2a 2a 20 70 72  r to some .** pr
9ed10 6f 67 72 61 6d 6d 65 72 73 2c 20 73 6f 20 77 65  ogrammers, so we
9ed20 20 70 72 6f 76 69 64 65 20 74 68 65 20 66 6f 6c   provide the fol
9ed30 6c 6f 77 69 6e 67 20 61 64 64 69 74 69 6f 6e 61  lowing additiona
9ed40 6c 20 65 78 70 6c 61 6e 61 74 69 6f 6e 3a 0a 2a  l explanation:.*
9ed50 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66  *.** The name of
9ed60 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73   the function is
9ed70 20 22 68 61 73 68 46 75 6e 63 74 69 6f 6e 22 2e   "hashFunction".
9ed80 20 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 74    The function t
9ed90 61 6b 65 73 20 61 0a 2a 2a 20 73 69 6e 67 6c 65  akes a.** single
9eda0 20 70 61 72 61 6d 65 74 65 72 20 22 6b 65 79 43   parameter "keyC
9edb0 6c 61 73 73 22 2e 20 20 54 68 65 20 72 65 74 75  lass".  The retu
9edc0 72 6e 20 76 61 6c 75 65 20 6f 66 20 68 61 73 68  rn value of hash
9edd0 46 75 6e 63 74 69 6f 6e 28 29 0a 2a 2a 20 69 73  Function().** is
9ede0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
9edf0 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20  other function. 
9ee00 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 74   Specifically, t
9ee10 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a  he return value.
9ee20 2a 2a 20 6f 66 20 68 61 73 68 46 75 6e 63 74 69  ** of hashFuncti
9ee30 6f 6e 28 29 20 69 73 20 61 20 70 6f 69 6e 74 65  on() is a pointe
9ee40 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20  r to a function 
9ee50 74 68 61 74 20 74 61 6b 65 73 20 74 77 6f 20 70  that takes two p
9ee60 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 77 69 74  arameters.** wit
9ee70 68 20 74 79 70 65 73 20 22 63 6f 6e 73 74 20 76  h types "const v
9ee80 6f 69 64 2a 22 20 61 6e 64 20 22 69 6e 74 22 20  oid*" and "int" 
9ee90 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e 20 22  and returns an "
9eea0 69 6e 74 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  int"..*/.static 
9eeb0 69 6e 74 20 28 2a 68 61 73 68 46 75 6e 63 74 69  int (*hashFuncti
9eec0 6f 6e 28 69 6e 74 20 6b 65 79 43 6c 61 73 73 29  on(int keyClass)
9eed0 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  )(const void*,in
9eee0 74 29 7b 0a 23 69 66 20 30 20 20 2f 2a 20 48 41  t){.#if 0  /* HA
9eef0 53 48 5f 49 4e 54 20 61 6e 64 20 48 41 53 48 5f  SH_INT and HASH_
9ef00 50 4f 49 4e 54 45 52 20 61 72 65 20 6e 65 76 65  POINTER are neve
9ef10 72 20 75 73 65 64 20 2a 2f 0a 20 20 73 77 69 74  r used */.  swit
9ef20 63 68 28 20 6b 65 79 43 6c 61 73 73 20 29 7b 0a  ch( keyClass ){.
9ef30 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
9ef40 48 41 53 48 5f 49 4e 54 3a 20 20 20 20 20 72 65  HASH_INT:     re
9ef50 74 75 72 6e 20 26 69 6e 74 48 61 73 68 3b 0a 20  turn &intHash;. 
9ef60 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 48     case SQLITE_H
9ef70 41 53 48 5f 50 4f 49 4e 54 45 52 3a 20 72 65 74  ASH_POINTER: ret
9ef80 75 72 6e 20 26 70 74 72 48 61 73 68 3b 0a 20 20  urn &ptrHash;.  
9ef90 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 48 41    case SQLITE_HA
9efa0 53 48 5f 53 54 52 49 4e 47 3a 20 20 72 65 74 75  SH_STRING:  retu
9efb0 72 6e 20 26 73 74 72 48 61 73 68 3b 0a 20 20 20  rn &strHash;.   
9efc0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 48 41 53   case SQLITE_HAS
9efd0 48 5f 42 49 4e 41 52 59 3a 20 20 72 65 74 75 72  H_BINARY:  retur
9efe0 6e 20 26 62 69 6e 48 61 73 68 3b 3b 0a 20 20 20  n &binHash;;.   
9eff0 20 64 65 66 61 75 6c 74 3a 20 62 72 65 61 6b 3b   default: break;
9f000 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
9f010 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 6b 65 79  .#else.  if( key
9f020 43 6c 61 73 73 3d 3d 53 51 4c 49 54 45 5f 48 41  Class==SQLITE_HA
9f030 53 48 5f 53 54 52 49 4e 47 20 29 7b 0a 20 20 20  SH_STRING ){.   
9f040 20 72 65 74 75 72 6e 20 26 73 74 72 48 61 73 68   return &strHash
9f050 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
9f060 73 73 65 72 74 28 20 6b 65 79 43 6c 61 73 73 3d  ssert( keyClass=
9f070 3d 53 51 4c 49 54 45 5f 48 41 53 48 5f 42 49 4e  =SQLITE_HASH_BIN
9f080 41 52 59 20 29 3b 0a 20 20 20 20 72 65 74 75 72  ARY );.    retur
9f090 6e 20 26 62 69 6e 48 61 73 68 3b 0a 20 20 7d 0a  n &binHash;.  }.
9f0a0 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
9f0b0 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
9f0c0 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69   to the appropri
9f0d0 61 74 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f  ate hash functio
9f0e0 6e 20 67 69 76 65 6e 20 74 68 65 20 6b 65 79 20  n given the key 
9f0f0 63 6c 61 73 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  class..**.** For
9f100 20 68 65 6c 70 20 69 6e 20 69 6e 74 65 72 70 72   help in interpr
9f110 65 74 65 64 20 74 68 65 20 6f 62 73 63 75 72 65  eted the obscure
9f120 20 43 20 63 6f 64 65 20 69 6e 20 74 68 65 20 66   C code in the f
9f130 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69  unction definiti
9f140 6f 6e 2c 0a 2a 2a 20 73 65 65 20 74 68 65 20 68  on,.** see the h
9f150 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e  eader comment on
9f160 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 66 75   the previous fu
9f170 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  nction..*/.stati
9f180 63 20 69 6e 74 20 28 2a 63 6f 6d 70 61 72 65 46  c int (*compareF
9f190 75 6e 63 74 69 6f 6e 28 69 6e 74 20 6b 65 79 43  unction(int keyC
9f1a0 6c 61 73 73 29 29 28 63 6f 6e 73 74 20 76 6f 69  lass))(const voi
9f1b0 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
9f1c0 64 2a 2c 69 6e 74 29 7b 0a 23 69 66 20 30 20 2f  d*,int){.#if 0 /
9f1d0 2a 20 48 41 53 48 5f 49 4e 54 20 61 6e 64 20 48  * HASH_INT and H
9f1e0 41 53 48 5f 50 4f 49 4e 54 45 52 20 61 72 65 20  ASH_POINTER are 
9f1f0 6e 65 76 65 72 20 75 73 65 64 20 2a 2f 0a 20 20  never used */.  
9f200 73 77 69 74 63 68 28 20 6b 65 79 43 6c 61 73 73  switch( keyClass
9f210 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c   ){.    case SQL
9f220 49 54 45 5f 48 41 53 48 5f 49 4e 54 3a 20 20 20  ITE_HASH_INT:   
9f230 20 20 72 65 74 75 72 6e 20 26 69 6e 74 43 6f 6d    return &intCom
9f240 70 61 72 65 3b 0a 20 20 20 20 63 61 73 65 20 53  pare;.    case S
9f250 51 4c 49 54 45 5f 48 41 53 48 5f 50 4f 49 4e 54  QLITE_HASH_POINT
9f260 45 52 3a 20 72 65 74 75 72 6e 20 26 70 74 72 43  ER: return &ptrC
9f270 6f 6d 70 61 72 65 3b 0a 20 20 20 20 63 61 73 65  ompare;.    case
9f280 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 53 54 52   SQLITE_HASH_STR
9f290 49 4e 47 3a 20 20 72 65 74 75 72 6e 20 26 73 74  ING:  return &st
9f2a0 72 43 6f 6d 70 61 72 65 3b 0a 20 20 20 20 63 61  rCompare;.    ca
9f2b0 73 65 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 42  se SQLITE_HASH_B
9f2c0 49 4e 41 52 59 3a 20 20 72 65 74 75 72 6e 20 26  INARY:  return &
9f2d0 62 69 6e 43 6f 6d 70 61 72 65 3b 0a 20 20 20 20  binCompare;.    
9f2e0 64 65 66 61 75 6c 74 3a 20 62 72 65 61 6b 3b 0a  default: break;.
9f2f0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
9f300 23 65 6c 73 65 0a 20 20 69 66 28 20 6b 65 79 43  #else.  if( keyC
9f310 6c 61 73 73 3d 3d 53 51 4c 49 54 45 5f 48 41 53  lass==SQLITE_HAS
9f320 48 5f 53 54 52 49 4e 47 20 29 7b 0a 20 20 20 20  H_STRING ){.    
9f330 72 65 74 75 72 6e 20 26 73 74 72 43 6f 6d 70 61  return &strCompa
9f340 72 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  re;.  }else{.   
9f350 20 61 73 73 65 72 74 28 20 6b 65 79 43 6c 61 73   assert( keyClas
9f360 73 3d 3d 53 51 4c 49 54 45 5f 48 41 53 48 5f 42  s==SQLITE_HASH_B
9f370 49 4e 41 52 59 20 29 3b 0a 20 20 20 20 72 65 74  INARY );.    ret
9f380 75 72 6e 20 26 62 69 6e 43 6f 6d 70 61 72 65 3b  urn &binCompare;
9f390 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  .  }.#endif.}../
9f3a0 2a 20 4c 69 6e 6b 20 61 6e 20 65 6c 65 6d 65 6e  * Link an elemen
9f3b0 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20  t into the hash 
9f3c0 74 61 62 6c 65 0a 2a 2f 0a 73 74 61 74 69 63 20  table.*/.static 
9f3d0 76 6f 69 64 20 69 6e 73 65 72 74 45 6c 65 6d 65  void insertEleme
9f3e0 6e 74 28 0a 20 20 48 61 73 68 20 2a 70 48 2c 20  nt(.  Hash *pH, 
9f3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
9f400 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 68 61 73  The complete has
9f410 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 74 72  h table */.  str
9f420 75 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 2c  uct _ht *pEntry,
9f430 20 20 20 20 2f 2a 20 54 68 65 20 65 6e 74 72 79      /* The entry
9f440 20 69 6e 74 6f 20 77 68 69 63 68 20 70 4e 65 77   into which pNew
9f450 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a   is inserted */.
9f460 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 4e 65 77    HashElem *pNew
9f470 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
9f480 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 69 6e  element to be in
9f490 73 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 48  serted */.){.  H
9f4a0 61 73 68 45 6c 65 6d 20 2a 70 48 65 61 64 3b 20  ashElem *pHead; 
9f4b0 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 65        /* First e
9f4c0 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64 79 20 69  lement already i
9f4d0 6e 20 70 45 6e 74 72 79 20 2a 2f 0a 20 20 70 48  n pEntry */.  pH
9f4e0 65 61 64 20 3d 20 70 45 6e 74 72 79 2d 3e 63 68  ead = pEntry->ch
9f4f0 61 69 6e 3b 0a 20 20 69 66 28 20 70 48 65 61 64  ain;.  if( pHead
9f500 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65   ){.    pNew->ne
9f510 78 74 20 3d 20 70 48 65 61 64 3b 0a 20 20 20 20  xt = pHead;.    
9f520 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20 70 48 65  pNew->prev = pHe
9f530 61 64 2d 3e 70 72 65 76 3b 0a 20 20 20 20 69 66  ad->prev;.    if
9f540 28 20 70 48 65 61 64 2d 3e 70 72 65 76 20 29 7b  ( pHead->prev ){
9f550 20 70 48 65 61 64 2d 3e 70 72 65 76 2d 3e 6e 65   pHead->prev->ne
9f560 78 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20  xt = pNew; }.   
9f570 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 20 20   else           
9f580 20 20 7b 20 70 48 2d 3e 66 69 72 73 74 20 3d 20    { pH->first = 
9f590 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70 48 65 61  pNew; }.    pHea
9f5a0 64 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 0a  d->prev = pNew;.
9f5b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65    }else{.    pNe
9f5c0 77 2d 3e 6e 65 78 74 20 3d 20 70 48 2d 3e 66 69  w->next = pH->fi
9f5d0 72 73 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d  rst;.    if( pH-
9f5e0 3e 66 69 72 73 74 20 29 7b 20 70 48 2d 3e 66 69  >first ){ pH->fi
9f5f0 72 73 74 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77  rst->prev = pNew
9f600 3b 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72  ; }.    pNew->pr
9f610 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 48 2d 3e  ev = 0;.    pH->
9f620 66 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20  first = pNew;.  
9f630 7d 0a 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e  }.  pEntry->coun
9f640 74 2b 2b 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 63  t++;.  pEntry->c
9f650 68 61 69 6e 20 3d 20 70 4e 65 77 3b 0a 7d 0a 0a  hain = pNew;.}..
9f660 0a 2f 2a 20 52 65 73 69 7a 65 20 74 68 65 20 68  ./* Resize the h
9f670 61 73 68 20 74 61 62 6c 65 20 73 6f 20 74 68 61  ash table so tha
9f680 74 20 69 74 20 63 61 6e 74 61 69 6e 73 20 22 6e  t it cantains "n
9f690 65 77 5f 73 69 7a 65 22 20 62 75 63 6b 65 74 73  ew_size" buckets
9f6a0 2e 0a 2a 2a 20 22 6e 65 77 5f 73 69 7a 65 22 20  ..** "new_size" 
9f6b0 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20  must be a power 
9f6c0 6f 66 20 32 2e 20 20 54 68 65 20 68 61 73 68 20  of 2.  The hash 
9f6d0 74 61 62 6c 65 20 6d 69 67 68 74 20 66 61 69 6c  table might fail
9f6e0 20 0a 2a 2a 20 74 6f 20 72 65 73 69 7a 65 20 69   .** to resize i
9f6f0 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  f sqlite3_malloc
9f700 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61  () fails..*/.sta
9f710 74 69 63 20 76 6f 69 64 20 72 65 68 61 73 68 28  tic void rehash(
9f720 48 61 73 68 20 2a 70 48 2c 20 69 6e 74 20 6e 65  Hash *pH, int ne
9f730 77 5f 73 69 7a 65 29 7b 0a 20 20 73 74 72 75 63  w_size){.  struc
9f740 74 20 5f 68 74 20 2a 6e 65 77 5f 68 74 3b 20 20  t _ht *new_ht;  
9f750 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
9f760 20 6e 65 77 20 68 61 73 68 20 74 61 62 6c 65 20   new hash table 
9f770 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65  */.  HashElem *e
9f780 6c 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65 6d 3b  lem, *next_elem;
9f790 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69      /* For loopi
9f7a0 6e 67 20 6f 76 65 72 20 65 78 69 73 74 69 6e 67  ng over existing
9f7b0 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69   elements */.  i
9f7c0 6e 74 20 28 2a 78 48 61 73 68 29 28 63 6f 6e 73  nt (*xHash)(cons
9f7d0 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 2f 2a  t void*,int); /*
9f7e0 20 54 68 65 20 68 61 73 68 20 66 75 6e 63 74 69   The hash functi
9f7f0 6f 6e 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51  on */..#ifdef SQ
9f800 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54  LITE_MALLOC_SOFT
9f810 5f 4c 49 4d 49 54 0a 20 20 69 66 28 20 6e 65 77  _LIMIT.  if( new
9f820 5f 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72  _size*sizeof(str
9f830 75 63 74 20 5f 68 74 29 3e 53 51 4c 49 54 45 5f  uct _ht)>SQLITE_
9f840 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49  MALLOC_SOFT_LIMI
9f850 54 20 29 7b 0a 20 20 20 20 6e 65 77 5f 73 69 7a  T ){.    new_siz
9f860 65 20 3d 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f  e = SQLITE_MALLO
9f870 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 2f 73 69 7a  C_SOFT_LIMIT/siz
9f880 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 3b  eof(struct _ht);
9f890 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 77 5f 73  .  }.  if( new_s
9f8a0 69 7a 65 3d 3d 70 48 2d 3e 68 74 73 69 7a 65 20  ize==pH->htsize 
9f8b0 29 20 72 65 74 75 72 6e 3b 0a 23 65 6e 64 69 66  ) return;.#endif
9f8c0 0a 0a 20 20 2f 2a 20 54 68 65 72 65 20 69 73 20  ..  /* There is 
9f8d0 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  a call to sqlite
9f8e0 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 6e 73 69 64  3_malloc() insid
9f8f0 65 20 72 65 68 61 73 68 28 29 2e 20 49 66 20 74  e rehash(). If t
9f900 68 65 72 65 20 69 73 0a 20 20 2a 2a 20 61 6c 72  here is.  ** alr
9f910 65 61 64 79 20 61 6e 20 61 6c 6c 6f 63 61 74 69  eady an allocati
9f920 6f 6e 20 61 74 20 70 48 2d 3e 68 74 2c 20 74 68  on at pH->ht, th
9f930 65 6e 20 69 66 20 74 68 69 73 20 6d 61 6c 6c 6f  en if this mallo
9f940 63 28 29 20 66 61 69 6c 73 20 69 74 0a 20 20 2a  c() fails it.  *
9f950 2a 20 69 73 20 62 65 6e 69 67 6e 20 28 73 69 6e  * is benign (sin
9f960 63 65 20 66 61 69 6c 69 6e 67 20 74 6f 20 72 65  ce failing to re
9f970 73 69 7a 65 20 61 20 68 61 73 68 20 74 61 62 6c  size a hash tabl
9f980 65 20 69 73 20 61 20 70 65 72 66 6f 72 6d 61 6e  e is a performan
9f990 63 65 0a 20 20 2a 2a 20 68 69 74 20 6f 6e 6c 79  ce.  ** hit only
9f9a0 2c 20 6e 6f 74 20 61 20 66 61 74 61 6c 20 65 72  , not a fatal er
9f9b0 72 6f 72 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ror)..  */.  if(
9f9c0 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30 20 29 20   pH->htsize>0 ) 
9f9d0 73 71 6c 69 74 65 33 46 61 75 6c 74 42 65 67 69  sqlite3FaultBegi
9f9e0 6e 42 65 6e 69 67 6e 28 53 51 4c 49 54 45 5f 46  nBenign(SQLITE_F
9f9f0 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 4d 41 4c  AULTINJECTOR_MAL
9fa00 4c 4f 43 29 3b 0a 20 20 6e 65 77 5f 68 74 20 3d  LOC);.  new_ht =
9fa10 20 28 73 74 72 75 63 74 20 5f 68 74 20 2a 29 73   (struct _ht *)s
9fa20 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
9fa30 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a 65 6f  ( new_size*sizeo
9fa40 66 28 73 74 72 75 63 74 20 5f 68 74 29 20 29 3b  f(struct _ht) );
9fa50 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a  .  if( pH->htsiz
9fa60 65 3e 30 20 29 20 73 71 6c 69 74 65 33 46 61 75  e>0 ) sqlite3Fau
9fa70 6c 74 45 6e 64 42 65 6e 69 67 6e 28 53 51 4c 49  ltEndBenign(SQLI
9fa80 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43 54 4f 52  TE_FAULTINJECTOR
9fa90 5f 4d 41 4c 4c 4f 43 29 3b 0a 0a 20 20 69 66 28  _MALLOC);..  if(
9faa0 20 6e 65 77 5f 68 74 3d 3d 30 20 29 20 72 65 74   new_ht==0 ) ret
9fab0 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  urn;.  sqlite3_f
9fac0 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70  ree(pH->ht);.  p
9fad0 48 2d 3e 68 74 20 3d 20 6e 65 77 5f 68 74 3b 0a  H->ht = new_ht;.
9fae0 20 20 70 48 2d 3e 68 74 73 69 7a 65 20 3d 20 6e    pH->htsize = n
9faf0 65 77 5f 73 69 7a 65 3b 0a 20 20 78 48 61 73 68  ew_size;.  xHash
9fb00 20 3d 20 68 61 73 68 46 75 6e 63 74 69 6f 6e 28   = hashFunction(
9fb10 70 48 2d 3e 6b 65 79 43 6c 61 73 73 29 3b 0a 20  pH->keyClass);. 
9fb20 20 66 6f 72 28 65 6c 65 6d 3d 70 48 2d 3e 66 69   for(elem=pH->fi
9fb30 72 73 74 2c 20 70 48 2d 3e 66 69 72 73 74 3d 30  rst, pH->first=0
9fb40 3b 20 65 6c 65 6d 3b 20 65 6c 65 6d 20 3d 20 6e  ; elem; elem = n
9fb50 65 78 74 5f 65 6c 65 6d 29 7b 0a 20 20 20 20 69  ext_elem){.    i
9fb60 6e 74 20 68 20 3d 20 28 2a 78 48 61 73 68 29 28  nt h = (*xHash)(
9fb70 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d  elem->pKey, elem
9fb80 2d 3e 6e 4b 65 79 29 20 26 20 28 6e 65 77 5f 73  ->nKey) & (new_s
9fb90 69 7a 65 2d 31 29 3b 0a 20 20 20 20 6e 65 78 74  ize-1);.    next
9fba0 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65  _elem = elem->ne
9fbb0 78 74 3b 0a 20 20 20 20 69 6e 73 65 72 74 45 6c  xt;.    insertEl
9fbc0 65 6d 65 6e 74 28 70 48 2c 20 26 6e 65 77 5f 68  ement(pH, &new_h
9fbd0 74 5b 68 5d 2c 20 65 6c 65 6d 29 3b 0a 20 20 7d  t[h], elem);.  }
9fbe0 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63  .}../* This func
9fbf0 74 69 6f 6e 20 28 66 6f 72 20 69 6e 74 65 72 6e  tion (for intern
9fc00 61 6c 20 75 73 65 20 6f 6e 6c 79 29 20 6c 6f 63  al use only) loc
9fc10 61 74 65 73 20 61 6e 20 65 6c 65 6d 65 6e 74 20  ates an element 
9fc20 69 6e 20 61 6e 0a 2a 2a 20 68 61 73 68 20 74 61  in an.** hash ta
9fc30 62 6c 65 20 74 68 61 74 20 6d 61 74 63 68 65 73  ble that matches
9fc40 20 74 68 65 20 67 69 76 65 6e 20 6b 65 79 2e 20   the given key. 
9fc50 20 54 68 65 20 68 61 73 68 20 66 6f 72 20 74 68   The hash for th
9fc60 69 73 20 6b 65 79 20 68 61 73 0a 2a 2a 20 61 6c  is key has.** al
9fc70 72 65 61 64 79 20 62 65 65 6e 20 63 6f 6d 70 75  ready been compu
9fc80 74 65 64 20 61 6e 64 20 69 73 20 70 61 73 73 65  ted and is passe
9fc90 64 20 61 73 20 74 68 65 20 34 74 68 20 70 61 72  d as the 4th par
9fca0 61 6d 65 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  ameter..*/.stati
9fcb0 63 20 48 61 73 68 45 6c 65 6d 20 2a 66 69 6e 64  c HashElem *find
9fcc0 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68  ElementGivenHash
9fcd0 28 0a 20 20 63 6f 6e 73 74 20 48 61 73 68 20 2a  (.  const Hash *
9fce0 70 48 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 70  pH,     /* The p
9fcf0 48 20 74 6f 20 62 65 20 73 65 61 72 63 68 65 64  H to be searched
9fd00 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
9fd10 20 2a 70 4b 65 79 2c 20 20 20 2f 2a 20 54 68 65   *pKey,   /* The
9fd20 20 6b 65 79 20 77 65 20 61 72 65 20 73 65 61 72   key we are sear
9fd30 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0a 20 20 69  ching for */.  i
9fd40 6e 74 20 6e 4b 65 79 2c 0a 20 20 69 6e 74 20 68  nt nKey,.  int h
9fd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
9fd60 2a 20 54 68 65 20 68 61 73 68 20 66 6f 72 20 74  * The hash for t
9fd70 68 69 73 20 6b 65 79 2e 20 2a 2f 0a 29 7b 0a 20  his key. */.){. 
9fd80 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b   HashElem *elem;
9fd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9fda0 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20  /* Used to loop 
9fdb0 74 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74  thru the element
9fdc0 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 63   list */.  int c
9fdd0 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  ount;           
9fde0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
9fdf0 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
9fe00 6c 65 66 74 20 74 6f 20 74 65 73 74 20 2a 2f 0a  left to test */.
9fe10 20 20 69 6e 74 20 28 2a 78 43 6f 6d 70 61 72 65    int (*xCompare
9fe20 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  )(const void*,in
9fe30 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  t,const void*,in
9fe40 74 29 3b 20 20 2f 2a 20 63 6f 6d 70 61 72 69 73  t);  /* comparis
9fe50 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a  on function */..
9fe60 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a    if( pH->ht ){.
9fe70 20 20 20 20 73 74 72 75 63 74 20 5f 68 74 20 2a      struct _ht *
9fe80 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74  pEntry = &pH->ht
9fe90 5b 68 5d 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20  [h];.    elem = 
9fea0 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3b 0a 20  pEntry->chain;. 
9feb0 20 20 20 63 6f 75 6e 74 20 3d 20 70 45 6e 74 72     count = pEntr
9fec0 79 2d 3e 63 6f 75 6e 74 3b 0a 20 20 20 20 78 43  y->count;.    xC
9fed0 6f 6d 70 61 72 65 20 3d 20 63 6f 6d 70 61 72 65  ompare = compare
9fee0 46 75 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79  Function(pH->key
9fef0 43 6c 61 73 73 29 3b 0a 20 20 20 20 77 68 69 6c  Class);.    whil
9ff00 65 28 20 63 6f 75 6e 74 2d 2d 20 26 26 20 65 6c  e( count-- && el
9ff10 65 6d 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  em ){.      if( 
9ff20 28 2a 78 43 6f 6d 70 61 72 65 29 28 65 6c 65 6d  (*xCompare)(elem
9ff30 2d 3e 70 4b 65 79 2c 65 6c 65 6d 2d 3e 6e 4b 65  ->pKey,elem->nKe
9ff40 79 2c 70 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30 20  y,pKey,nKey)==0 
9ff50 29 7b 20 0a 20 20 20 20 20 20 20 20 72 65 74 75  ){ .        retu
9ff60 72 6e 20 65 6c 65 6d 3b 0a 20 20 20 20 20 20 7d  rn elem;.      }
9ff70 0a 20 20 20 20 20 20 65 6c 65 6d 20 3d 20 65 6c  .      elem = el
9ff80 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 7d 0a  em->next;.    }.
9ff90 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
9ffa0 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 20 73  }../* Remove a s
9ffb0 69 6e 67 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d  ingle entry from
9ffc0 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
9ffd0 67 69 76 65 6e 20 61 20 70 6f 69 6e 74 65 72 20  given a pointer 
9ffe0 74 6f 20 74 68 61 74 0a 2a 2a 20 65 6c 65 6d 65  to that.** eleme
9fff0 6e 74 20 61 6e 64 20 61 20 68 61 73 68 20 6f 6e  nt and a hash on
a0000 20 74 68 65 20 65 6c 65 6d 65 6e 74 27 73 20 6b   the element's k
a0010 65 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ey..*/.static vo
a0020 69 64 20 72 65 6d 6f 76 65 45 6c 65 6d 65 6e 74  id removeElement
a0030 47 69 76 65 6e 48 61 73 68 28 0a 20 20 48 61 73  GivenHash(.  Has
a0040 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20 20 2f  h *pH,         /
a0050 2a 20 54 68 65 20 70 48 20 63 6f 6e 74 61 69 6e  * The pH contain
a0060 69 6e 67 20 22 65 6c 65 6d 22 20 2a 2f 0a 20 20  ing "elem" */.  
a0070 48 61 73 68 45 6c 65 6d 2a 20 65 6c 65 6d 2c 20  HashElem* elem, 
a0080 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74    /* The element
a0090 20 74 6f 20 62 65 20 72 65 6d 6f 76 65 64 20 66   to be removed f
a00a0 72 6f 6d 20 74 68 65 20 70 48 20 2a 2f 0a 20 20  rom the pH */.  
a00b0 69 6e 74 20 68 20 20 20 20 20 20 20 20 20 20 20  int h           
a00c0 20 20 2f 2a 20 48 61 73 68 20 76 61 6c 75 65 20    /* Hash value 
a00d0 66 6f 72 20 74 68 65 20 65 6c 65 6d 65 6e 74 20  for the element 
a00e0 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 5f  */.){.  struct _
a00f0 68 74 20 2a 70 45 6e 74 72 79 3b 0a 20 20 69 66  ht *pEntry;.  if
a0100 28 20 65 6c 65 6d 2d 3e 70 72 65 76 20 29 7b 0a  ( elem->prev ){.
a0110 20 20 20 20 65 6c 65 6d 2d 3e 70 72 65 76 2d 3e      elem->prev->
a0120 6e 65 78 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78  next = elem->nex
a0130 74 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  t; .  }else{.   
a0140 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 65 6c 65   pH->first = ele
a0150 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69  m->next;.  }.  i
a0160 66 28 20 65 6c 65 6d 2d 3e 6e 65 78 74 20 29 7b  f( elem->next ){
a0170 0a 20 20 20 20 65 6c 65 6d 2d 3e 6e 65 78 74 2d  .    elem->next-
a0180 3e 70 72 65 76 20 3d 20 65 6c 65 6d 2d 3e 70 72  >prev = elem->pr
a0190 65 76 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79  ev;.  }.  pEntry
a01a0 20 3d 20 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20   = &pH->ht[h];. 
a01b0 20 69 66 28 20 70 45 6e 74 72 79 2d 3e 63 68 61   if( pEntry->cha
a01c0 69 6e 3d 3d 65 6c 65 6d 20 29 7b 0a 20 20 20 20  in==elem ){.    
a01d0 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20  pEntry->chain = 
a01e0 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a  elem->next;.  }.
a01f0 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d    pEntry->count-
a0200 2d 3b 0a 20 20 69 66 28 20 70 45 6e 74 72 79 2d  -;.  if( pEntry-
a0210 3e 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20 20 20  >count<=0 ){.   
a0220 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d   pEntry->chain =
a0230 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48   0;.  }.  if( pH
a0240 2d 3e 63 6f 70 79 4b 65 79 20 29 7b 0a 20 20 20  ->copyKey ){.   
a0250 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 65 6c   sqlite3_free(el
a0260 65 6d 2d 3e 70 4b 65 79 29 3b 0a 20 20 7d 0a 20  em->pKey);.  }. 
a0270 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 20 65   sqlite3_free( e
a0280 6c 65 6d 20 29 3b 0a 20 20 70 48 2d 3e 63 6f 75  lem );.  pH->cou
a0290 6e 74 2d 2d 3b 0a 20 20 69 66 28 20 70 48 2d 3e  nt--;.  if( pH->
a02a0 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20  count<=0 ){.    
a02b0 61 73 73 65 72 74 28 20 70 48 2d 3e 66 69 72 73  assert( pH->firs
a02c0 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  t==0 );.    asse
a02d0 72 74 28 20 70 48 2d 3e 63 6f 75 6e 74 3d 3d 30  rt( pH->count==0
a02e0 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 48   );.    sqlite3H
a02f0 61 73 68 43 6c 65 61 72 28 70 48 29 3b 0a 20 20  ashClear(pH);.  
a0300 7d 0a 7d 0a 0a 2f 2a 20 41 74 74 65 6d 70 74 20  }.}../* Attempt 
a0310 74 6f 20 6c 6f 63 61 74 65 20 61 6e 20 65 6c 65  to locate an ele
a0320 6d 65 6e 74 20 6f 66 20 74 68 65 20 68 61 73 68  ment of the hash
a0330 20 74 61 62 6c 65 20 70 48 20 77 69 74 68 20 61   table pH with a
a0340 20 6b 65 79 0a 2a 2a 20 74 68 61 74 20 6d 61 74   key.** that mat
a0350 63 68 65 73 20 70 4b 65 79 2c 6e 4b 65 79 2e 20  ches pKey,nKey. 
a0360 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
a0370 72 20 74 6f 20 74 68 65 20 63 6f 72 72 65 73 70  r to the corresp
a0380 6f 6e 64 69 6e 67 20 0a 2a 2a 20 48 61 73 68 45  onding .** HashE
a0390 6c 65 6d 20 73 74 72 75 63 74 75 72 65 20 66 6f  lem structure fo
a03a0 72 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 69  r this element i
a03b0 66 20 69 74 20 69 73 20 66 6f 75 6e 64 2c 20 6f  f it is found, o
a03c0 72 20 4e 55 4c 4c 0a 2a 2a 20 6f 74 68 65 72 77  r NULL.** otherw
a03d0 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ise..*/.SQLITE_P
a03e0 52 49 56 41 54 45 20 48 61 73 68 45 6c 65 6d 20  RIVATE HashElem 
a03f0 2a 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64  *sqlite3HashFind
a0400 45 6c 65 6d 28 63 6f 6e 73 74 20 48 61 73 68 20  Elem(const Hash 
a0410 2a 70 48 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  *pH, const void 
a0420 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29  *pKey, int nKey)
a0430 7b 0a 20 20 69 6e 74 20 68 3b 20 20 20 20 20 20  {.  int h;      
a0440 20 20 20 20 20 20 20 2f 2a 20 41 20 68 61 73 68         /* A hash
a0450 20 6f 6e 20 6b 65 79 20 2a 2f 0a 20 20 48 61 73   on key */.  Has
a0460 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20  hElem *elem;    
a0470 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74  /* The element t
a0480 68 61 74 20 6d 61 74 63 68 65 73 20 6b 65 79 20  hat matches key 
a0490 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 48 61 73 68  */.  int (*xHash
a04a0 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  )(const void*,in
a04b0 74 29 3b 20 20 2f 2a 20 54 68 65 20 68 61 73 68  t);  /* The hash
a04c0 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20   function */..  
a04d0 69 66 28 20 70 48 3d 3d 30 20 7c 7c 20 70 48 2d  if( pH==0 || pH-
a04e0 3e 68 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  >ht==0 ) return 
a04f0 30 3b 0a 20 20 78 48 61 73 68 20 3d 20 68 61 73  0;.  xHash = has
a0500 68 46 75 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b 65  hFunction(pH->ke
a0510 79 43 6c 61 73 73 29 3b 0a 20 20 61 73 73 65 72  yClass);.  asser
a0520 74 28 20 78 48 61 73 68 21 3d 30 20 29 3b 0a 20  t( xHash!=0 );. 
a0530 20 68 20 3d 20 28 2a 78 48 61 73 68 29 28 70 4b   h = (*xHash)(pK
a0540 65 79 2c 6e 4b 65 79 29 3b 0a 20 20 65 6c 65 6d  ey,nKey);.  elem
a0550 20 3d 20 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69   = findElementGi
a0560 76 65 6e 48 61 73 68 28 70 48 2c 70 4b 65 79 2c  venHash(pH,pKey,
a0570 6e 4b 65 79 2c 20 68 20 25 20 70 48 2d 3e 68 74  nKey, h % pH->ht
a0580 73 69 7a 65 29 3b 0a 20 20 72 65 74 75 72 6e 20  size);.  return 
a0590 65 6c 65 6d 3b 0a 7d 0a 0a 2f 2a 20 41 74 74 65  elem;.}../* Atte
a05a0 6d 70 74 20 74 6f 20 6c 6f 63 61 74 65 20 61 6e  mpt to locate an
a05b0 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20   element of the 
a05c0 68 61 73 68 20 74 61 62 6c 65 20 70 48 20 77 69  hash table pH wi
a05d0 74 68 20 61 20 6b 65 79 0a 2a 2a 20 74 68 61 74  th a key.** that
a05e0 20 6d 61 74 63 68 65 73 20 70 4b 65 79 2c 6e 4b   matches pKey,nK
a05f0 65 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  ey.  Return the 
a0600 64 61 74 61 20 66 6f 72 20 74 68 69 73 20 65 6c  data for this el
a0610 65 6d 65 6e 74 20 69 66 20 69 74 20 69 73 0a 2a  ement if it is.*
a0620 2a 20 66 6f 75 6e 64 2c 20 6f 72 20 4e 55 4c 4c  * found, or NULL
a0630 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   if there is no 
a0640 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45  match..*/.SQLITE
a0650 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
a0660 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 63  qlite3HashFind(c
a0670 6f 6e 73 74 20 48 61 73 68 20 2a 70 48 2c 20 63  onst Hash *pH, c
a0680 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c  onst void *pKey,
a0690 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 48 61   int nKey){.  Ha
a06a0 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20  shElem *elem;   
a06b0 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20   /* The element 
a06c0 74 68 61 74 20 6d 61 74 63 68 65 73 20 6b 65 79  that matches key
a06d0 20 2a 2f 0a 20 20 65 6c 65 6d 20 3d 20 73 71 6c   */.  elem = sql
a06e0 69 74 65 33 48 61 73 68 46 69 6e 64 45 6c 65 6d  ite3HashFindElem
a06f0 28 70 48 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 29  (pH, pKey, nKey)
a0700 3b 0a 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 20  ;.  return elem 
a0710 3f 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3a 20 30  ? elem->data : 0
a0720 3b 0a 7d 0a 0a 2f 2a 20 49 6e 73 65 72 74 20 61  ;.}../* Insert a
a0730 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74  n element into t
a0740 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 48  he hash table pH
a0750 2e 20 20 54 68 65 20 6b 65 79 20 69 73 20 70 4b  .  The key is pK
a0760 65 79 2c 6e 4b 65 79 0a 2a 2a 20 61 6e 64 20 74  ey,nKey.** and t
a0770 68 65 20 64 61 74 61 20 69 73 20 22 64 61 74 61  he data is "data
a0780 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65  "..**.** If no e
a0790 6c 65 6d 65 6e 74 20 65 78 69 73 74 73 20 77 69  lement exists wi
a07a0 74 68 20 61 20 6d 61 74 63 68 69 6e 67 20 6b 65  th a matching ke
a07b0 79 2c 20 74 68 65 6e 20 61 20 6e 65 77 0a 2a 2a  y, then a new.**
a07c0 20 65 6c 65 6d 65 6e 74 20 69 73 20 63 72 65 61   element is crea
a07d0 74 65 64 2e 20 20 41 20 63 6f 70 79 20 6f 66 20  ted.  A copy of 
a07e0 74 68 65 20 6b 65 79 20 69 73 20 6d 61 64 65 20  the key is made 
a07f0 69 66 20 74 68 65 20 63 6f 70 79 4b 65 79 0a 2a  if the copyKey.*
a0800 2a 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 20  * flag is set.  
a0810 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64  NULL is returned
a0820 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68  ..**.** If anoth
a0830 65 72 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61  er element alrea
a0840 64 79 20 65 78 69 73 74 73 20 77 69 74 68 20 74  dy exists with t
a0850 68 65 20 73 61 6d 65 20 6b 65 79 2c 20 74 68 65  he same key, the
a0860 6e 20 74 68 65 0a 2a 2a 20 6e 65 77 20 64 61 74  n the.** new dat
a0870 61 20 72 65 70 6c 61 63 65 73 20 74 68 65 20 6f  a replaces the o
a0880 6c 64 20 64 61 74 61 20 61 6e 64 20 74 68 65 20  ld data and the 
a0890 6f 6c 64 20 64 61 74 61 20 69 73 20 72 65 74 75  old data is retu
a08a0 72 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 6b 65 79  rned..** The key
a08b0 20 69 73 20 6e 6f 74 20 63 6f 70 69 65 64 20 69   is not copied i
a08c0 6e 20 74 68 69 73 20 69 6e 73 74 61 6e 63 65 2e  n this instance.
a08d0 20 20 49 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61    If a malloc fa
a08e0 69 6c 73 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65  ils, then.** the
a08f0 20 6e 65 77 20 64 61 74 61 20 69 73 20 72 65 74   new data is ret
a0900 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 68 61  urned and the ha
a0910 73 68 20 74 61 62 6c 65 20 69 73 20 75 6e 63 68  sh table is unch
a0920 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  anged..**.** If 
a0930 74 68 65 20 22 64 61 74 61 22 20 70 61 72 61 6d  the "data" param
a0940 65 74 65 72 20 74 6f 20 74 68 69 73 20 66 75 6e  eter to this fun
a0950 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 74  ction is NULL, t
a0960 68 65 6e 20 74 68 65 0a 2a 2a 20 65 6c 65 6d 65  hen the.** eleme
a0970 6e 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  nt corresponding
a0980 20 74 6f 20 22 6b 65 79 22 20 69 73 20 72 65 6d   to "key" is rem
a0990 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 68 61  oved from the ha
a09a0 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c  sh table..*/.SQL
a09b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
a09c0 20 2a 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73   *sqlite3HashIns
a09d0 65 72 74 28 48 61 73 68 20 2a 70 48 2c 20 63 6f  ert(Hash *pH, co
a09e0 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
a09f0 69 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 64 20 2a  int nKey, void *
a0a00 64 61 74 61 29 7b 0a 20 20 69 6e 74 20 68 72 61  data){.  int hra
a0a10 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  w;             /
a0a20 2a 20 52 61 77 20 68 61 73 68 20 76 61 6c 75 65  * Raw hash value
a0a30 20 6f 66 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20   of the key */. 
a0a40 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20   int h;         
a0a50 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 68 61         /* the ha
a0a60 73 68 20 6f 66 20 74 68 65 20 6b 65 79 20 6d 6f  sh of the key mo
a0a70 64 75 6c 6f 20 68 61 73 68 20 74 61 62 6c 65 20  dulo hash table 
a0a80 73 69 7a 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c  size */.  HashEl
a0a90 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20  em *elem;       
a0aa0 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20  /* Used to loop 
a0ab0 74 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74  thru the element
a0ac0 20 6c 69 73 74 20 2a 2f 0a 20 20 48 61 73 68 45   list */.  HashE
a0ad0 6c 65 6d 20 2a 6e 65 77 5f 65 6c 65 6d 3b 20 20  lem *new_elem;  
a0ae0 20 2f 2a 20 4e 65 77 20 65 6c 65 6d 65 6e 74 20   /* New element 
a0af0 61 64 64 65 64 20 74 6f 20 74 68 65 20 70 48 20  added to the pH 
a0b00 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 48 61 73 68  */.  int (*xHash
a0b10 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  )(const void*,in
a0b20 74 29 3b 20 20 2f 2a 20 54 68 65 20 68 61 73 68  t);  /* The hash
a0b30 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20   function */..  
a0b40 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b  assert( pH!=0 );
a0b50 0a 20 20 78 48 61 73 68 20 3d 20 68 61 73 68 46  .  xHash = hashF
a0b60 75 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43  unction(pH->keyC
a0b70 6c 61 73 73 29 3b 0a 20 20 61 73 73 65 72 74 28  lass);.  assert(
a0b80 20 78 48 61 73 68 21 3d 30 20 29 3b 0a 20 20 68   xHash!=0 );.  h
a0b90 72 61 77 20 3d 20 28 2a 78 48 61 73 68 29 28 70  raw = (*xHash)(p
a0ba0 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 69 66  Key, nKey);.  if
a0bb0 28 20 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a  ( pH->htsize ){.
a0bc0 20 20 20 20 68 20 3d 20 68 72 61 77 20 25 20 70      h = hraw % p
a0bd0 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 20 20 65  H->htsize;.    e
a0be0 6c 65 6d 20 3d 20 66 69 6e 64 45 6c 65 6d 65 6e  lem = findElemen
a0bf0 74 47 69 76 65 6e 48 61 73 68 28 70 48 2c 70 4b  tGivenHash(pH,pK
a0c00 65 79 2c 6e 4b 65 79 2c 68 29 3b 0a 20 20 20 20  ey,nKey,h);.    
a0c10 69 66 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20  if( elem ){.    
a0c20 20 20 76 6f 69 64 20 2a 6f 6c 64 5f 64 61 74 61    void *old_data
a0c30 20 3d 20 65 6c 65 6d 2d 3e 64 61 74 61 3b 0a 20   = elem->data;. 
a0c40 20 20 20 20 20 69 66 28 20 64 61 74 61 3d 3d 30       if( data==0
a0c50 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6d 6f   ){.        remo
a0c60 76 65 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61  veElementGivenHa
a0c70 73 68 28 70 48 2c 65 6c 65 6d 2c 68 29 3b 0a 20  sh(pH,elem,h);. 
a0c80 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
a0c90 20 20 20 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3d      elem->data =
a0ca0 20 64 61 74 61 3b 0a 20 20 20 20 20 20 20 20 69   data;.        i
a0cb0 66 28 20 21 70 48 2d 3e 63 6f 70 79 4b 65 79 20  f( !pH->copyKey 
a0cc0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 65 6c 65  ){.          ele
a0cd0 6d 2d 3e 70 4b 65 79 20 3d 20 28 76 6f 69 64 20  m->pKey = (void 
a0ce0 2a 29 70 4b 65 79 3b 0a 20 20 20 20 20 20 20 20  *)pKey;.        
a0cf0 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  }.        assert
a0d00 28 6e 4b 65 79 3d 3d 65 6c 65 6d 2d 3e 6e 4b 65  (nKey==elem->nKe
a0d10 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  y);.      }.    
a0d20 20 20 72 65 74 75 72 6e 20 6f 6c 64 5f 64 61 74    return old_dat
a0d30 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  a;.    }.  }.  i
a0d40 66 28 20 64 61 74 61 3d 3d 30 20 29 20 72 65 74  f( data==0 ) ret
a0d50 75 72 6e 20 30 3b 0a 20 20 6e 65 77 5f 65 6c 65  urn 0;.  new_ele
a0d60 6d 20 3d 20 28 48 61 73 68 45 6c 65 6d 2a 29 73  m = (HashElem*)s
a0d70 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73  qlite3_malloc( s
a0d80 69 7a 65 6f 66 28 48 61 73 68 45 6c 65 6d 29 20  izeof(HashElem) 
a0d90 29 3b 0a 20 20 69 66 28 20 6e 65 77 5f 65 6c 65  );.  if( new_ele
a0da0 6d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 64 61  m==0 ) return da
a0db0 74 61 3b 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f  ta;.  if( pH->co
a0dc0 70 79 4b 65 79 20 26 26 20 70 4b 65 79 21 3d 30  pyKey && pKey!=0
a0dd0 20 29 7b 0a 20 20 20 20 6e 65 77 5f 65 6c 65 6d   ){.    new_elem
a0de0 2d 3e 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33  ->pKey = sqlite3
a0df0 5f 6d 61 6c 6c 6f 63 28 20 6e 4b 65 79 20 29 3b  _malloc( nKey );
a0e00 0a 20 20 20 20 69 66 28 20 6e 65 77 5f 65 6c 65  .    if( new_ele
a0e10 6d 2d 3e 70 4b 65 79 3d 3d 30 20 29 7b 0a 20 20  m->pKey==0 ){.  
a0e20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
a0e30 28 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 20 20  (new_elem);.    
a0e40 20 20 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20    return data;. 
a0e50 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28     }.    memcpy(
a0e60 28 76 6f 69 64 2a 29 6e 65 77 5f 65 6c 65 6d 2d  (void*)new_elem-
a0e70 3e 70 4b 65 79 2c 20 70 4b 65 79 2c 20 6e 4b 65  >pKey, pKey, nKe
a0e80 79 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  y);.  }else{.   
a0e90 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20   new_elem->pKey 
a0ea0 3d 20 28 76 6f 69 64 2a 29 70 4b 65 79 3b 0a 20  = (void*)pKey;. 
a0eb0 20 7d 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 6e   }.  new_elem->n
a0ec0 4b 65 79 20 3d 20 6e 4b 65 79 3b 0a 20 20 70 48  Key = nKey;.  pH
a0ed0 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 69 66 28  ->count++;.  if(
a0ee0 20 70 48 2d 3e 68 74 73 69 7a 65 3d 3d 30 20 29   pH->htsize==0 )
a0ef0 7b 0a 20 20 20 20 72 65 68 61 73 68 28 70 48 2c  {.    rehash(pH,
a0f00 20 31 32 38 2f 73 69 7a 65 6f 66 28 70 48 2d 3e   128/sizeof(pH->
a0f10 68 74 5b 30 5d 29 29 3b 0a 20 20 20 20 69 66 28  ht[0]));.    if(
a0f20 20 70 48 2d 3e 68 74 73 69 7a 65 3d 3d 30 20 29   pH->htsize==0 )
a0f30 7b 0a 20 20 20 20 20 20 70 48 2d 3e 63 6f 75 6e  {.      pH->coun
a0f40 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28  t = 0;.      if(
a0f50 20 70 48 2d 3e 63 6f 70 79 4b 65 79 20 29 7b 0a   pH->copyKey ){.
a0f60 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
a0f70 66 72 65 65 28 6e 65 77 5f 65 6c 65 6d 2d 3e 70  free(new_elem->p
a0f80 4b 65 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Key);.      }.  
a0f90 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
a0fa0 28 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 20 20  (new_elem);.    
a0fb0 20 20 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20    return data;. 
a0fc0 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70     }.  }.  if( p
a0fd0 48 2d 3e 63 6f 75 6e 74 20 3e 20 70 48 2d 3e 68  H->count > pH->h
a0fe0 74 73 69 7a 65 20 29 7b 0a 20 20 20 20 72 65 68  tsize ){.    reh
a0ff0 61 73 68 28 70 48 2c 70 48 2d 3e 68 74 73 69 7a  ash(pH,pH->htsiz
a1000 65 2a 32 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65  e*2);.  }.  asse
a1010 72 74 28 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30  rt( pH->htsize>0
a1020 20 29 3b 0a 20 20 68 20 3d 20 68 72 61 77 20 25   );.  h = hraw %
a1030 20 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 69   pH->htsize;.  i
a1040 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c  nsertElement(pH,
a1050 20 26 70 48 2d 3e 68 74 5b 68 5d 2c 20 6e 65 77   &pH->ht[h], new
a1060 5f 65 6c 65 6d 29 3b 0a 20 20 6e 65 77 5f 65 6c  _elem);.  new_el
a1070 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 3b  em->data = data;
a1080 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
a1090 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
a10a0 45 6e 64 20 6f 66 20 68 61 73 68 2e 63 20 2a 2a  End of hash.c **
a10b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a10c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a10d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
a10e0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
a10f0 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63 6f 64  Begin file opcod
a1100 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  es.c ***********
a1110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a1120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
a1130 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79  /* Automatically
a1140 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44 6f 20   generated.  Do 
a1150 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 53  not edit */./* S
a1160 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 63  ee the mkopcodec
a1170 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72 20  .awk script for 
a1180 64 65 74 61 69 6c 73 2e 20 2a 2f 0a 23 69 66 20  details. */.#if 
a1190 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
a11a0 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20 7c 7c  OMIT_EXPLAIN) ||
a11b0 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47   !defined(NDEBUG
a11c0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 42  ) || defined(VDB
a11d0 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65  E_PROFILE) || de
a11e0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
a11f0 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  UG).SQLITE_PRIVA
a1200 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  TE const char *s
a1210 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65  qlite3OpcodeName
a1220 28 69 6e 74 20 69 29 7b 0a 20 73 74 61 74 69 63  (int i){. static
a1230 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e   const char *con
a1240 73 74 20 61 7a 4e 61 6d 65 5b 5d 20 3d 20 7b 20  st azName[] = { 
a1250 22 3f 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 31  "?",.     /*   1
a1260 20 2a 2f 20 22 56 4e 65 78 74 22 2c 0a 20 20 20   */ "VNext",.   
a1270 20 20 2f 2a 20 20 20 32 20 2a 2f 20 22 41 66 66    /*   2 */ "Aff
a1280 69 6e 69 74 79 22 2c 0a 20 20 20 20 20 2f 2a 20  inity",.     /* 
a1290 20 20 33 20 2a 2f 20 22 43 6f 6c 75 6d 6e 22 2c    3 */ "Column",
a12a0 0a 20 20 20 20 20 2f 2a 20 20 20 34 20 2a 2f 20  .     /*   4 */ 
a12b0 22 53 65 74 43 6f 6f 6b 69 65 22 2c 0a 20 20 20  "SetCookie",.   
a12c0 20 20 2f 2a 20 20 20 35 20 2a 2f 20 22 53 65 71    /*   5 */ "Seq
a12d0 75 65 6e 63 65 22 2c 0a 20 20 20 20 20 2f 2a 20  uence",.     /* 
a12e0 20 20 36 20 2a 2f 20 22 4d 6f 76 65 47 74 22 2c    6 */ "MoveGt",
a12f0 0a 20 20 20 20 20 2f 2a 20 20 20 37 20 2a 2f 20  .     /*   7 */ 
a1300 22 52 6f 77 4b 65 79 22 2c 0a 20 20 20 20 20 2f  "RowKey",.     /
a1310 2a 20 20 20 38 20 2a 2f 20 22 53 43 6f 70 79 22  *   8 */ "SCopy"
a1320 2c 0a 20 20 20 20 20 2f 2a 20 20 20 39 20 2a 2f  ,.     /*   9 */
a1330 20 22 4f 70 65 6e 57 72 69 74 65 22 2c 0a 20 20   "OpenWrite",.  
a1340 20 20 20 2f 2a 20 20 31 30 20 2a 2f 20 22 49 66     /*  10 */ "If
a1350 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 31 20 2a  ",.     /*  11 *
a1360 2f 20 22 56 52 6f 77 69 64 22 2c 0a 20 20 20 20  / "VRowid",.    
a1370 20 2f 2a 20 20 31 32 20 2a 2f 20 22 43 6f 6c 6c   /*  12 */ "Coll
a1380 53 65 71 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31  Seq",.     /*  1
a1390 33 20 2a 2f 20 22 4f 70 65 6e 52 65 61 64 22 2c  3 */ "OpenRead",
a13a0 0a 20 20 20 20 20 2f 2a 20 20 31 34 20 2a 2f 20  .     /*  14 */ 
a13b0 22 45 78 70 69 72 65 22 2c 0a 20 20 20 20 20 2f  "Expire",.     /
a13c0 2a 20 20 31 35 20 2a 2f 20 22 41 75 74 6f 43 6f  *  15 */ "AutoCo
a13d0 6d 6d 69 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  mmit",.     /*  
a13e0 31 36 20 2a 2f 20 22 4e 6f 74 22 2c 0a 20 20 20  16 */ "Not",.   
a13f0 20 20 2f 2a 20 20 31 37 20 2a 2f 20 22 49 6e 74    /*  17 */ "Int
a1400 65 67 72 69 74 79 43 6b 22 2c 0a 20 20 20 20 20  egrityCk",.     
a1410 2f 2a 20 20 31 38 20 2a 2f 20 22 53 6f 72 74 22  /*  18 */ "Sort"
a1420 2c 0a 20 20 20 20 20 2f 2a 20 20 31 39 20 2a 2f  ,.     /*  19 */
a1430 20 22 43 6f 70 79 22 2c 0a 20 20 20 20 20 2f 2a   "Copy",.     /*
a1440 20 20 32 30 20 2a 2f 20 22 54 72 61 63 65 22 2c    20 */ "Trace",
a1450 0a 20 20 20 20 20 2f 2a 20 20 32 31 20 2a 2f 20  .     /*  21 */ 
a1460 22 46 75 6e 63 74 69 6f 6e 22 2c 0a 20 20 20 20  "Function",.    
a1470 20 2f 2a 20 20 32 32 20 2a 2f 20 22 49 66 4e 65   /*  22 */ "IfNe
a1480 67 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 33 20  g",.     /*  23 
a1490 2a 2f 20 22 4e 6f 6f 70 22 2c 0a 20 20 20 20 20  */ "Noop",.     
a14a0 2f 2a 20 20 32 34 20 2a 2f 20 22 52 65 74 75 72  /*  24 */ "Retur
a14b0 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 35 20  n",.     /*  25 
a14c0 2a 2f 20 22 4e 65 77 52 6f 77 69 64 22 2c 0a 20  */ "NewRowid",. 
a14d0 20 20 20 20 2f 2a 20 20 32 36 20 2a 2f 20 22 56      /*  26 */ "V
a14e0 61 72 69 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f  ariable",.     /
a14f0 2a 20 20 32 37 20 2a 2f 20 22 53 74 72 69 6e 67  *  27 */ "String
a1500 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 38 20 2a  ",.     /*  28 *
a1510 2f 20 22 52 65 61 6c 41 66 66 69 6e 69 74 79 22  / "RealAffinity"
a1520 2c 0a 20 20 20 20 20 2f 2a 20 20 32 39 20 2a 2f  ,.     /*  29 */
a1530 20 22 56 52 65 6e 61 6d 65 22 2c 0a 20 20 20 20   "VRename",.    
a1540 20 2f 2a 20 20 33 30 20 2a 2f 20 22 50 61 72 73   /*  30 */ "Pars
a1550 65 53 63 68 65 6d 61 22 2c 0a 20 20 20 20 20 2f  eSchema",.     /
a1560 2a 20 20 33 31 20 2a 2f 20 22 56 4f 70 65 6e 22  *  31 */ "VOpen"
a1570 2c 0a 20 20 20 20 20 2f 2a 20 20 33 32 20 2a 2f  ,.     /*  32 */
a1580 20 22 43 6c 6f 73 65 22 2c 0a 20 20 20 20 20 2f   "Close",.     /
a1590 2a 20 20 33 33 20 2a 2f 20 22 43 72 65 61 74 65  *  33 */ "Create
a15a0 49 6e 64 65 78 22 2c 0a 20 20 20 20 20 2f 2a 20  Index",.     /* 
a15b0 20 33 34 20 2a 2f 20 22 49 73 55 6e 69 71 75 65   34 */ "IsUnique
a15c0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 35 20 2a  ",.     /*  35 *
a15d0 2f 20 22 4e 6f 74 46 6f 75 6e 64 22 2c 0a 20 20  / "NotFound",.  
a15e0 20 20 20 2f 2a 20 20 33 36 20 2a 2f 20 22 49 6e     /*  36 */ "In
a15f0 74 36 34 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33  t64",.     /*  3
a1600 37 20 2a 2f 20 22 4d 75 73 74 42 65 49 6e 74 22  7 */ "MustBeInt"
a1610 2c 0a 20 20 20 20 20 2f 2a 20 20 33 38 20 2a 2f  ,.     /*  38 */
a1620 20 22 48 61 6c 74 22 2c 0a 20 20 20 20 20 2f 2a   "Halt",.     /*
a1630 20 20 33 39 20 2a 2f 20 22 52 6f 77 69 64 22 2c    39 */ "Rowid",
a1640 0a 20 20 20 20 20 2f 2a 20 20 34 30 20 2a 2f 20  .     /*  40 */ 
a1650 22 49 64 78 4c 54 22 2c 0a 20 20 20 20 20 2f 2a  "IdxLT",.     /*
a1660 20 20 34 31 20 2a 2f 20 22 41 64 64 49 6d 6d 22    41 */ "AddImm"
a1670 2c 0a 20 20 20 20 20 2f 2a 20 20 34 32 20 2a 2f  ,.     /*  42 */
a1680 20 22 53 74 61 74 65 6d 65 6e 74 22 2c 0a 20 20   "Statement",.  
a1690 20 20 20 2f 2a 20 20 34 33 20 2a 2f 20 22 52 6f     /*  43 */ "Ro
a16a0 77 44 61 74 61 22 2c 0a 20 20 20 20 20 2f 2a 20  wData",.     /* 
a16b0 20 34 34 20 2a 2f 20 22 4d 65 6d 4d 61 78 22 2c   44 */ "MemMax",
a16c0 0a 20 20 20 20 20 2f 2a 20 20 34 35 20 2a 2f 20  .     /*  45 */ 
a16d0 22 4e 6f 74 45 78 69 73 74 73 22 2c 0a 20 20 20  "NotExists",.   
a16e0 20 20 2f 2a 20 20 34 36 20 2a 2f 20 22 47 6f 73    /*  46 */ "Gos
a16f0 75 62 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 37  ub",.     /*  47
a1700 20 2a 2f 20 22 49 6e 74 65 67 65 72 22 2c 0a 20   */ "Integer",. 
a1710 20 20 20 20 2f 2a 20 20 34 38 20 2a 2f 20 22 50      /*  48 */ "P
a1720 72 65 76 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34  rev",.     /*  4
a1730 39 20 2a 2f 20 22 56 43 6f 6c 75 6d 6e 22 2c 0a  9 */ "VColumn",.
a1740 20 20 20 20 20 2f 2a 20 20 35 30 20 2a 2f 20 22       /*  50 */ "
a1750 43 72 65 61 74 65 54 61 62 6c 65 22 2c 0a 20 20  CreateTable",.  
a1760 20 20 20 2f 2a 20 20 35 31 20 2a 2f 20 22 4c 61     /*  51 */ "La
a1770 73 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 32  st",.     /*  52
a1780 20 2a 2f 20 22 49 6e 63 72 56 61 63 75 75 6d 22   */ "IncrVacuum"
a1790 2c 0a 20 20 20 20 20 2f 2a 20 20 35 33 20 2a 2f  ,.     /*  53 */
a17a0 20 22 49 64 78 52 6f 77 69 64 22 2c 0a 20 20 20   "IdxRowid",.   
a17b0 20 20 2f 2a 20 20 35 34 20 2a 2f 20 22 52 65 73    /*  54 */ "Res
a17c0 65 74 43 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f  etCount",.     /
a17d0 2a 20 20 35 35 20 2a 2f 20 22 46 69 66 6f 57 72  *  55 */ "FifoWr
a17e0 69 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35  ite",.     /*  5
a17f0 36 20 2a 2f 20 22 43 6f 6e 74 65 78 74 50 75 73  6 */ "ContextPus
a1800 68 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 37 20  h",.     /*  57 
a1810 2a 2f 20 22 44 72 6f 70 54 72 69 67 67 65 72 22  */ "DropTrigger"
a1820 2c 0a 20 20 20 20 20 2f 2a 20 20 35 38 20 2a 2f  ,.     /*  58 */
a1830 20 22 44 72 6f 70 49 6e 64 65 78 22 2c 0a 20 20   "DropIndex",.  
a1840 20 20 20 2f 2a 20 20 35 39 20 2a 2f 20 22 49 64     /*  59 */ "Id
a1850 78 47 45 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36  xGE",.     /*  6
a1860 30 20 2a 2f 20 22 4f 72 22 2c 0a 20 20 20 20 20  0 */ "Or",.     
a1870 2f 2a 20 20 36 31 20 2a 2f 20 22 41 6e 64 22 2c  /*  61 */ "And",
a1880 0a 20 20 20 20 20 2f 2a 20 20 36 32 20 2a 2f 20  .     /*  62 */ 
a1890 22 49 64 78 44 65 6c 65 74 65 22 2c 0a 20 20 20  "IdxDelete",.   
a18a0 20 20 2f 2a 20 20 36 33 20 2a 2f 20 22 56 61 63    /*  63 */ "Vac
a18b0 75 75 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36  uum",.     /*  6
a18c0 34 20 2a 2f 20 22 4d 6f 76 65 4c 65 22 2c 0a 20  4 */ "MoveLe",. 
a18d0 20 20 20 20 2f 2a 20 20 36 35 20 2a 2f 20 22 49      /*  65 */ "I
a18e0 73 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20  sNull",.     /* 
a18f0 20 36 36 20 2a 2f 20 22 4e 6f 74 4e 75 6c 6c 22   66 */ "NotNull"
a1900 2c 0a 20 20 20 20 20 2f 2a 20 20 36 37 20 2a 2f  ,.     /*  67 */
a1910 20 22 4e 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20   "Ne",.     /*  
a1920 36 38 20 2a 2f 20 22 45 71 22 2c 0a 20 20 20 20  68 */ "Eq",.    
a1930 20 2f 2a 20 20 36 39 20 2a 2f 20 22 47 74 22 2c   /*  69 */ "Gt",
a1940 0a 20 20 20 20 20 2f 2a 20 20 37 30 20 2a 2f 20  .     /*  70 */ 
a1950 22 4c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37  "Le",.     /*  7
a1960 31 20 2a 2f 20 22 4c 74 22 2c 0a 20 20 20 20 20  1 */ "Lt",.     
a1970 2f 2a 20 20 37 32 20 2a 2f 20 22 47 65 22 2c 0a  /*  72 */ "Ge",.
a1980 20 20 20 20 20 2f 2a 20 20 37 33 20 2a 2f 20 22       /*  73 */ "
a1990 49 66 4e 6f 74 22 2c 0a 20 20 20 20 20 2f 2a 20  IfNot",.     /* 
a19a0 20 37 34 20 2a 2f 20 22 42 69 74 41 6e 64 22 2c   74 */ "BitAnd",
a19b0 0a 20 20 20 20 20 2f 2a 20 20 37 35 20 2a 2f 20  .     /*  75 */ 
a19c0 22 42 69 74 4f 72 22 2c 0a 20 20 20 20 20 2f 2a  "BitOr",.     /*
a19d0 20 20 37 36 20 2a 2f 20 22 53 68 69 66 74 4c 65    76 */ "ShiftLe
a19e0 66 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 37  ft",.     /*  77
a19f0 20 2a 2f 20 22 53 68 69 66 74 52 69 67 68 74 22   */ "ShiftRight"
a1a00 2c 0a 20 20 20 20 20 2f 2a 20 20 37 38 20 2a 2f  ,.     /*  78 */
a1a10 20 22 41 64 64 22 2c 0a 20 20 20 20 20 2f 2a 20   "Add",.     /* 
a1a20 20 37 39 20 2a 2f 20 22 53 75 62 74 72 61 63 74   79 */ "Subtract
a1a30 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 30 20 2a  ",.     /*  80 *
a1a40 2f 20 22 4d 75 6c 74 69 70 6c 79 22 2c 0a 20 20  / "Multiply",.  
a1a50 20 20 20 2f 2a 20 20 38 31 20 2a 2f 20 22 44 69     /*  81 */ "Di
a1a60 76 69 64 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20  vide",.     /*  
a1a70 38 32 20 2a 2f 20 22 52 65 6d 61 69 6e 64 65 72  82 */ "Remainder
a1a80 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 33 20 2a  ",.     /*  83 *
a1a90 2f 20 22 43 6f 6e 63 61 74 22 2c 0a 20 20 20 20  / "Concat",.    
a1aa0 20 2f 2a 20 20 38 34 20 2a 2f 20 22 44 72 6f 70   /*  84 */ "Drop
a1ab0 54 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a 20  Table",.     /* 
a1ac0 20 38 35 20 2a 2f 20 22 4d 61 6b 65 52 65 63 6f   85 */ "MakeReco
a1ad0 72 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 36  rd",.     /*  86
a1ae0 20 2a 2f 20 22 52 65 73 75 6c 74 52 6f 77 22 2c   */ "ResultRow",
a1af0 0a 20 20 20 20 20 2f 2a 20 20 38 37 20 2a 2f 20  .     /*  87 */ 
a1b00 22 42 69 74 4e 6f 74 22 2c 0a 20 20 20 20 20 2f  "BitNot",.     /
a1b10 2a 20 20 38 38 20 2a 2f 20 22 53 74 72 69 6e 67  *  88 */ "String
a1b20 38 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 39 20  8",.     /*  89 
a1b30 2a 2f 20 22 44 65 6c 65 74 65 22 2c 0a 20 20 20  */ "Delete",.   
a1b40 20 20 2f 2a 20 20 39 30 20 2a 2f 20 22 41 67 67    /*  90 */ "Agg
a1b50 46 69 6e 61 6c 22 2c 0a 20 20 20 20 20 2f 2a 20  Final",.     /* 
a1b60 20 39 31 20 2a 2f 20 22 47 6f 74 6f 22 2c 0a 20   91 */ "Goto",. 
a1b70 20 20 20 20 2f 2a 20 20 39 32 20 2a 2f 20 22 54      /*  92 */ "T
a1b80 61 62 6c 65 4c 6f 63 6b 22 2c 0a 20 20 20 20 20  ableLock",.     
a1b90 2f 2a 20 20 39 33 20 2a 2f 20 22 46 69 66 6f 52  /*  93 */ "FifoR
a1ba0 65 61 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39  ead",.     /*  9
a1bb0 34 20 2a 2f 20 22 43 6c 65 61 72 22 2c 0a 20 20  4 */ "Clear",.  
a1bc0 20 20 20 2f 2a 20 20 39 35 20 2a 2f 20 22 4d 6f     /*  95 */ "Mo
a1bd0 76 65 4c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  veLt",.     /*  
a1be0 39 36 20 2a 2f 20 22 56 65 72 69 66 79 43 6f 6f  96 */ "VerifyCoo
a1bf0 6b 69 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39  kie",.     /*  9
a1c00 37 20 2a 2f 20 22 41 67 67 53 74 65 70 22 2c 0a  7 */ "AggStep",.
a1c10 20 20 20 20 20 2f 2a 20 20 39 38 20 2a 2f 20 22       /*  98 */ "
a1c20 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 22 2c 0a  SetNumColumns",.
a1c30 20 20 20 20 20 2f 2a 20 20 39 39 20 2a 2f 20 22       /*  99 */ "
a1c40 54 72 61 6e 73 61 63 74 69 6f 6e 22 2c 0a 20 20  Transaction",.  
a1c50 20 20 20 2f 2a 20 31 30 30 20 2a 2f 20 22 56 46     /* 100 */ "VF
a1c60 69 6c 74 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20  ilter",.     /* 
a1c70 31 30 31 20 2a 2f 20 22 56 44 65 73 74 72 6f 79  101 */ "VDestroy
a1c80 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 32 20 2a  ",.     /* 102 *
a1c90 2f 20 22 43 6f 6e 74 65 78 74 50 6f 70 22 2c 0a  / "ContextPop",.
a1ca0 20 20 20 20 20 2f 2a 20 31 30 33 20 2a 2f 20 22       /* 103 */ "
a1cb0 4e 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31  Next",.     /* 1
a1cc0 30 34 20 2a 2f 20 22 49 64 78 49 6e 73 65 72 74  04 */ "IdxInsert
a1cd0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 35 20 2a  ",.     /* 105 *
a1ce0 2f 20 22 49 6e 73 65 72 74 22 2c 0a 20 20 20 20  / "Insert",.    
a1cf0 20 2f 2a 20 31 30 36 20 2a 2f 20 22 44 65 73 74   /* 106 */ "Dest
a1d00 72 6f 79 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30  roy",.     /* 10
a1d10 37 20 2a 2f 20 22 52 65 61 64 43 6f 6f 6b 69 65  7 */ "ReadCookie
a1d20 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 38 20 2a  ",.     /* 108 *
a1d30 2f 20 22 46 6f 72 63 65 49 6e 74 22 2c 0a 20 20  / "ForceInt",.  
a1d40 20 20 20 2f 2a 20 31 30 39 20 2a 2f 20 22 4c 6f     /* 109 */ "Lo
a1d50 61 64 41 6e 61 6c 79 73 69 73 22 2c 0a 20 20 20  adAnalysis",.   
a1d60 20 20 2f 2a 20 31 31 30 20 2a 2f 20 22 45 78 70    /* 110 */ "Exp
a1d70 6c 61 69 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31  lain",.     /* 1
a1d80 31 31 20 2a 2f 20 22 4f 70 65 6e 50 73 65 75 64  11 */ "OpenPseud
a1d90 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 32 20  o",.     /* 112 
a1da0 2a 2f 20 22 4f 70 65 6e 45 70 68 65 6d 65 72 61  */ "OpenEphemera
a1db0 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 33 20  l",.     /* 113 
a1dc0 2a 2f 20 22 4e 75 6c 6c 22 2c 0a 20 20 20 20 20  */ "Null",.     
a1dd0 2f 2a 20 31 31 34 20 2a 2f 20 22 4d 6f 76 65 22  /* 114 */ "Move"
a1de0 2c 0a 20 20 20 20 20 2f 2a 20 31 31 35 20 2a 2f  ,.     /* 115 */
a1df0 20 22 42 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a   "Blob",.     /*
a1e00 20 31 31 36 20 2a 2f 20 22 52 65 77 69 6e 64 22   116 */ "Rewind"
a1e10 2c 0a 20 20 20 20 20 2f 2a 20 31 31 37 20 2a 2f  ,.     /* 117 */
a1e20 20 22 4d 6f 76 65 47 65 22 2c 0a 20 20 20 20 20   "MoveGe",.     
a1e30 2f 2a 20 31 31 38 20 2a 2f 20 22 56 42 65 67 69  /* 118 */ "VBegi
a1e40 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 39 20  n",.     /* 119 
a1e50 2a 2f 20 22 56 55 70 64 61 74 65 22 2c 0a 20 20  */ "VUpdate",.  
a1e60 20 20 20 2f 2a 20 31 32 30 20 2a 2f 20 22 49 66     /* 120 */ "If
a1e70 5a 65 72 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 31  Zero",.     /* 1
a1e80 32 31 20 2a 2f 20 22 56 43 72 65 61 74 65 22 2c  21 */ "VCreate",
a1e90 0a 20 20 20 20 20 2f 2a 20 31 32 32 20 2a 2f 20  .     /* 122 */ 
a1ea0 22 46 6f 75 6e 64 22 2c 0a 20 20 20 20 20 2f 2a  "Found",.     /*
a1eb0 20 31 32 33 20 2a 2f 20 22 49 66 50 6f 73 22 2c   123 */ "IfPos",
a1ec0 0a 20 20 20 20 20 2f 2a 20 31 32 34 20 2a 2f 20  .     /* 124 */ 
a1ed0 22 4e 75 6c 6c 52 6f 77 22 2c 0a 20 20 20 20 20  "NullRow",.     
a1ee0 2f 2a 20 31 32 35 20 2a 2f 20 22 52 65 61 6c 22  /* 125 */ "Real"
a1ef0 2c 0a 20 20 20 20 20 2f 2a 20 31 32 36 20 2a 2f  ,.     /* 126 */
a1f00 20 22 4e 6f 74 55 73 65 64 5f 31 32 36 22 2c 0a   "NotUsed_126",.
a1f10 20 20 20 20 20 2f 2a 20 31 32 37 20 2a 2f 20 22       /* 127 */ "
a1f20 4e 6f 74 55 73 65 64 5f 31 32 37 22 2c 0a 20 20  NotUsed_127",.  
a1f30 20 20 20 2f 2a 20 31 32 38 20 2a 2f 20 22 4e 6f     /* 128 */ "No
a1f40 74 55 73 65 64 5f 31 32 38 22 2c 0a 20 20 20 20  tUsed_128",.    
a1f50 20 2f 2a 20 31 32 39 20 2a 2f 20 22 4e 6f 74 55   /* 129 */ "NotU
a1f60 73 65 64 5f 31 32 39 22 2c 0a 20 20 20 20 20 2f  sed_129",.     /
a1f70 2a 20 31 33 30 20 2a 2f 20 22 4e 6f 74 55 73 65  * 130 */ "NotUse
a1f80 64 5f 31 33 30 22 2c 0a 20 20 20 20 20 2f 2a 20  d_130",.     /* 
a1f90 31 33 31 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f  131 */ "NotUsed_
a1fa0 31 33 31 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33  131",.     /* 13
a1fb0 32 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33  2 */ "NotUsed_13
a1fc0 32 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 33 20  2",.     /* 133 
a1fd0 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 33 22  */ "NotUsed_133"
a1fe0 2c 0a 20 20 20 20 20 2f 2a 20 31 33 34 20 2a 2f  ,.     /* 134 */
a1ff0 20 22 4e 6f 74 55 73 65 64 5f 31 33 34 22 2c 0a   "NotUsed_134",.
a2000 20 20 20 20 20 2f 2a 20 31 33 35 20 2a 2f 20 22       /* 135 */ "
a2010 4e 6f 74 55 73 65 64 5f 31 33 35 22 2c 0a 20 20  NotUsed_135",.  
a2020 20 20 20 2f 2a 20 31 33 36 20 2a 2f 20 22 4e 6f     /* 136 */ "No
a2030 74 55 73 65 64 5f 31 33 36 22 2c 0a 20 20 20 20  tUsed_136",.    
a2040 20 2f 2a 20 31 33 37 20 2a 2f 20 22 4e 6f 74 55   /* 137 */ "NotU
a2050 73 65 64 5f 31 33 37 22 2c 0a 20 20 20 20 20 2f  sed_137",.     /
a2060 2a 20 31 33 38 20 2a 2f 20 22 54 6f 54 65 78 74  * 138 */ "ToText
a2070 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 39 20 2a  ",.     /* 139 *
a2080 2f 20 22 54 6f 42 6c 6f 62 22 2c 0a 20 20 20 20  / "ToBlob",.    
a2090 20 2f 2a 20 31 34 30 20 2a 2f 20 22 54 6f 4e 75   /* 140 */ "ToNu
a20a0 6d 65 72 69 63 22 2c 0a 20 20 20 20 20 2f 2a 20  meric",.     /* 
a20b0 31 34 31 20 2a 2f 20 22 54 6f 49 6e 74 22 2c 0a  141 */ "ToInt",.
a20c0 20 20 20 20 20 2f 2a 20 31 34 32 20 2a 2f 20 22       /* 142 */ "
a20d0 54 6f 52 65 61 6c 22 2c 0a 20 20 7d 3b 0a 20 20  ToReal",.  };.  
a20e0 72 65 74 75 72 6e 20 61 7a 4e 61 6d 65 5b 69 5d  return azName[i]
a20f0 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a  ;.}.#endif../***
a2100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
a2110 6f 66 20 6f 70 63 6f 64 65 73 2e 63 20 2a 2a 2a  of opcodes.c ***
a2120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
a2150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
a2160 6e 20 66 69 6c 65 20 6f 73 5f 6f 73 32 2e 63 20  n file os_os2.c 
a2170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
a21a0 2a 20 32 30 30 36 20 46 65 62 20 31 34 0a 2a 2a  * 2006 Feb 14.**
a21b0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
a21c0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
a21d0 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
a21e0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
a21f0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
a2200 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
a2210 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
a2220 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
a2230 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
a2240 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
a2250 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
a2260 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
a2270 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
a2280 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
a2290 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
a22a0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
a22b0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
a22c0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
a22d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a22e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a22f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2310 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
a2320 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
a2330 74 68 61 74 20 69 73 20 73 70 65 63 69 66 69 63  that is specific
a2340 20 74 6f 20 4f 53 2f 32 2e 0a 2a 2f 0a 0a 0a 23   to OS/2..*/...#
a2350 69 66 20 4f 53 5f 4f 53 32 0a 0a 2f 2a 0a 2a 2a  if OS_OS2../*.**
a2360 20 41 20 4e 6f 74 65 20 41 62 6f 75 74 20 4d 65   A Note About Me
a2370 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69 6f 6e 3a  mory Allocation:
a2380 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 64 72 69 76  .**.** This driv
a2390 65 72 20 75 73 65 73 20 6d 61 6c 6c 6f 63 28 29  er uses malloc()
a23a0 2f 66 72 65 65 28 29 20 64 69 72 65 63 74 6c 79  /free() directly
a23b0 20 72 61 74 68 65 72 20 74 68 61 6e 20 67 6f 69   rather than goi
a23c0 6e 67 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 68  ng through.** th
a23d0 65 20 53 51 4c 69 74 65 2d 77 72 61 70 70 65 72  e SQLite-wrapper
a23e0 73 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  s sqlite3_malloc
a23f0 28 29 2f 73 71 6c 69 74 65 33 5f 66 72 65 65 28  ()/sqlite3_free(
a2400 29 2e 20 20 54 68 6f 73 65 20 77 72 61 70 70 65  ).  Those wrappe
a2410 72 73 0a 2a 2a 20 61 72 65 20 64 65 73 69 67 6e  rs.** are design
a2420 65 64 20 66 6f 72 20 75 73 65 20 6f 6e 20 65 6d  ed for use on em
a2430 62 65 64 64 65 64 20 73 79 73 74 65 6d 73 20 77  bedded systems w
a2440 68 65 72 65 20 6d 65 6d 6f 72 79 20 69 73 20 73  here memory is s
a2450 63 61 72 63 65 20 61 6e 64 0a 2a 2a 20 6d 61 6c  carce and.** mal
a2460 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 68 61 70  loc failures hap
a2470 70 65 6e 20 66 72 65 71 75 65 6e 74 6c 79 2e 20  pen frequently. 
a2480 20 4f 53 2f 32 20 64 6f 65 73 20 6e 6f 74 20 74   OS/2 does not t
a2490 79 70 69 63 61 6c 6c 79 20 72 75 6e 20 6f 6e 0a  ypically run on.
a24a0 2a 2a 20 65 6d 62 65 64 64 65 64 20 73 79 73 74  ** embedded syst
a24b0 65 6d 73 2c 20 61 6e 64 20 77 68 65 6e 20 69 74  ems, and when it
a24c0 20 64 6f 65 73 20 74 68 65 20 64 65 76 65 6c 6f   does the develo
a24d0 70 65 72 73 20 6e 6f 72 6d 61 6c 6c 79 20 68 61  pers normally ha
a24e0 76 65 20 62 69 67 67 65 72 0a 2a 2a 20 70 72 6f  ve bigger.** pro
a24f0 62 6c 65 6d 73 20 74 6f 20 77 6f 72 72 79 20 61  blems to worry a
a2500 62 6f 75 74 20 74 68 61 6e 20 72 75 6e 6e 69 6e  bout than runnin
a2510 67 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e  g out of memory.
a2520 20 20 53 6f 20 74 68 65 72 65 20 69 73 20 6e 6f    So there is no
a2530 74 0a 2a 2a 20 61 20 63 6f 6d 70 65 6c 6c 69 6e  t.** a compellin
a2540 67 20 6e 65 65 64 20 74 6f 20 75 73 65 20 74 68  g need to use th
a2550 65 20 77 72 61 70 70 65 72 73 2e 0a 2a 2a 0a 2a  e wrappers..**.*
a2560 2a 20 42 75 74 20 74 68 65 72 65 20 69 73 20 61  * But there is a
a2570 20 67 6f 6f 64 20 72 65 61 73 6f 6e 20 74 6f 20   good reason to 
a2580 6e 6f 74 20 75 73 65 20 74 68 65 20 77 72 61 70  not use the wrap
a2590 70 65 72 73 2e 20 20 49 66 20 77 65 20 75 73 65  pers.  If we use
a25a0 20 74 68 65 0a 2a 2a 20 77 72 61 70 70 65 72 73   the.** wrappers
a25b0 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 67 65   then we will ge
a25c0 74 20 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c  t simulated mall
a25d0 6f 63 28 29 20 66 61 69 6c 75 72 65 73 20 77 69  oc() failures wi
a25e0 74 68 69 6e 20 74 68 69 73 0a 2a 2a 20 64 72 69  thin this.** dri
a25f0 76 65 72 2e 20 20 41 6e 64 20 74 68 61 74 20 63  ver.  And that c
a2600 61 75 73 65 73 20 61 6c 6c 20 6b 69 6e 64 73 20  auses all kinds 
a2610 6f 66 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20  of problems for 
a2620 6f 75 72 20 74 65 73 74 73 2e 20 20 57 65 0a 2a  our tests.  We.*
a2630 2a 20 63 6f 75 6c 64 20 65 6e 68 61 6e 63 65 20  * could enhance 
a2640 53 51 4c 69 74 65 20 74 6f 20 64 65 61 6c 20 77  SQLite to deal w
a2650 69 74 68 20 73 69 6d 75 6c 61 74 65 64 20 6d 61  ith simulated ma
a2660 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 77 69  lloc failures wi
a2670 74 68 69 6e 0a 2a 2a 20 74 68 65 20 4f 53 20 64  thin.** the OS d
a2680 72 69 76 65 72 2c 20 62 75 74 20 74 68 65 20 63  river, but the c
a2690 6f 64 65 20 74 6f 20 64 65 61 6c 20 77 69 74 68  ode to deal with
a26a0 20 74 68 6f 73 65 20 66 61 69 6c 75 72 65 20 77   those failure w
a26b0 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 62 65 20 65  ould not.** be e
a26c0 78 65 72 63 69 73 65 64 20 6f 6e 20 4c 69 6e 75  xercised on Linu
a26d0 78 20 28 77 68 69 63 68 20 64 6f 65 73 20 6e 6f  x (which does no
a26e0 74 20 6e 65 65 64 20 74 6f 20 6d 61 6c 6c 6f 63  t need to malloc
a26f0 28 29 20 69 6e 20 74 68 65 20 64 72 69 76 65 72  () in the driver
a2700 29 0a 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20 77  ).** and so we w
a2710 6f 75 6c 64 20 68 61 76 65 20 64 69 66 66 69 63  ould have diffic
a2720 75 6c 74 79 20 77 72 69 74 69 6e 67 20 63 6f 76  ulty writing cov
a2730 65 72 61 67 65 20 74 65 73 74 73 20 66 6f 72 20  erage tests for 
a2740 74 68 61 74 0a 2a 2a 20 63 6f 64 65 2e 20 20 42  that.** code.  B
a2750 65 74 74 65 72 20 74 6f 20 6c 65 61 76 65 20 74  etter to leave t
a2760 68 65 20 63 6f 64 65 20 6f 75 74 2c 20 77 65 20  he code out, we 
a2770 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  think..**.** The
a2780 20 70 6f 69 6e 74 20 6f 66 20 74 68 69 73 20 64   point of this d
a2790 69 73 63 75 73 73 69 6f 6e 20 69 73 20 61 73 20  iscussion is as 
a27a0 66 6f 6c 6c 6f 77 73 3a 20 20 57 68 65 6e 20 63  follows:  When c
a27b0 72 65 61 74 69 6e 67 20 61 20 6e 65 77 0a 2a 2a  reating a new.**
a27c0 20 4f 53 20 6c 61 79 65 72 20 66 6f 72 20 61 6e   OS layer for an
a27d0 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d   embedded system
a27e0 2c 20 69 66 20 79 6f 75 20 75 73 65 20 74 68 69  , if you use thi
a27f0 73 20 66 69 6c 65 20 61 73 20 61 6e 20 65 78 61  s file as an exa
a2800 6d 70 6c 65 2c 0a 2a 2a 20 61 76 6f 69 64 20 74  mple,.** avoid t
a2810 68 65 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63  he use of malloc
a2820 28 29 2f 66 72 65 65 28 29 2e 20 20 54 68 6f 73  ()/free().  Thos
a2830 65 20 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20  e routines work 
a2840 6f 6b 20 6f 6e 20 4f 53 2f 32 0a 2a 2a 20 64 65  ok on OS/2.** de
a2850 73 6b 74 6f 70 73 20 62 75 74 20 6e 6f 74 20 73  sktops but not s
a2860 6f 20 77 65 6c 6c 20 69 6e 20 65 6d 62 65 64 64  o well in embedd
a2870 65 64 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 0a  ed systems..*/..
a2880 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 75 73 65  /*.** Macros use
a2890 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77  d to determine w
a28a0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f  hether or not to
a28b0 20 75 73 65 20 74 68 72 65 61 64 73 2e 0a 2a 2f   use threads..*/
a28c0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
a28d0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 29 20  ITE_THREADSAFE) 
a28e0 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  && SQLITE_THREAD
a28f0 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 53 51  SAFE.# define SQ
a2900 4c 49 54 45 5f 4f 53 32 5f 54 48 52 45 41 44 53  LITE_OS2_THREADS
a2910 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a   1.#endif../*.**
a2920 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 20 74 68   Include code th
a2930 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20  at is common to 
a2940 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73  all os_*.c files
a2950 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .*/./***********
a2960 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63  *** Include os_c
a2970 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d  ommon.h in the m
a2980 69 64 64 6c 65 20 6f 66 20 6f 73 5f 6f 73 32 2e  iddle of os_os2.
a2990 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
a29a0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
a29b0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f  *** Begin file o
a29c0 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a  s_common.h *****
a29d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a29e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a29f0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d  **/./*.** 2004 M
a2a00 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ay 22.**.** The 
a2a10 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
a2a20 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
a2a30 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
a2a40 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
a2a50 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
a2a60 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
a2a70 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
a2a80 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
a2a90 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
a2aa0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
a2ab0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
a2ac0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
a2ad0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
a2ae0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
a2af0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
a2b00 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
a2b10 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
a2b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
a2b70 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
a2b80 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64 20 61 20  ns macros and a 
a2b90 6c 69 74 74 6c 65 20 62 69 74 20 6f 66 20 63 6f  little bit of co
a2ba0 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f  de that is commo
a2bb0 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74  n to.** all of t
a2bc0 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63  he platform-spec
a2bd0 69 66 69 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a  ific files (os_*
a2be0 2e 63 29 20 61 6e 64 20 69 73 20 23 69 6e 63 6c  .c) and is #incl
a2bf0 75 64 65 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a  uded into those.
a2c00 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  ** files..**.** 
a2c10 54 68 69 73 20 66 69 6c 65 20 73 68 6f 75 6c 64  This file should
a2c20 20 62 65 20 23 69 6e 63 6c 75 64 65 64 20 62 79   be #included by
a2c30 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65   the os_*.c file
a2c40 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e  s only.  It is n
a2c50 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20  ot a.** general 
a2c60 70 75 72 70 6f 73 65 20 68 65 61 64 65 72 20 66  purpose header f
a2c70 69 6c 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41  ile..*/../*.** A
a2c80 74 20 6c 65 61 73 74 20 74 77 6f 20 62 75 67 73  t least two bugs
a2c90 20 68 61 76 65 20 73 6c 69 70 70 65 64 20 69 6e   have slipped in
a2ca0 20 62 65 63 61 75 73 65 20 77 65 20 63 68 61 6e   because we chan
a2cb0 67 65 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44  ged the MEMORY_D
a2cc0 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f  EBUG.** macro to
a2cd0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61 6e   SQLITE_DEBUG an
a2ce0 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b  d some older mak
a2cf0 65 66 69 6c 65 73 20 68 61 76 65 20 6e 6f 74 20  efiles have not 
a2d00 79 65 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20  yet made the.** 
a2d10 73 77 69 74 63 68 2e 20 20 54 68 65 20 66 6f 6c  switch.  The fol
a2d20 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73 68 6f 75  lowing code shou
a2d30 6c 64 20 63 61 74 63 68 20 74 68 69 73 20 70 72  ld catch this pr
a2d40 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65  oblem at compile
a2d50 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66  -time..*/.#ifdef
a2d60 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20   MEMORY_DEBUG.# 
a2d70 65 72 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52  error "The MEMOR
a2d80 59 5f 44 45 42 55 47 20 6d 61 63 72 6f 20 69 73  Y_DEBUG macro is
a2d90 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55 73 65 20   obsolete.  Use 
a2da0 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 6e 73  SQLITE_DEBUG ins
a2db0 74 65 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 0a  tead.".#endif...
a2dc0 2f 2a 0a 20 2a 20 57 68 65 6e 20 74 65 73 74 69  /*. * When testi
a2dd0 6e 67 2c 20 74 68 69 73 20 67 6c 6f 62 61 6c 20  ng, this global 
a2de0 76 61 72 69 61 62 6c 65 20 73 74 6f 72 65 73 20  variable stores 
a2df0 74 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20  the location of 
a2e00 74 68 65 0a 20 2a 20 70 65 6e 64 69 6e 67 2d 62  the. * pending-b
a2e10 79 74 65 20 69 6e 20 74 68 65 20 64 61 74 61 62  yte in the datab
a2e20 61 73 65 20 66 69 6c 65 2e 0a 20 2a 2f 0a 23 69  ase file.. */.#i
a2e30 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
a2e40 0a 53 51 4c 49 54 45 5f 41 50 49 20 75 6e 73 69  .SQLITE_API unsi
a2e50 67 6e 65 64 20 69 6e 74 20 73 71 6c 69 74 65 33  gned int sqlite3
a2e60 5f 70 65 6e 64 69 6e 67 5f 62 79 74 65 20 3d 20  _pending_byte = 
a2e70 30 78 34 30 30 30 30 30 30 30 3b 0a 23 65 6e 64  0x40000000;.#end
a2e80 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
a2e90 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50  E_DEBUG.SQLITE_P
a2ea0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
a2eb0 65 33 4f 53 54 72 61 63 65 20 3d 20 30 3b 0a 23  e3OSTrace = 0;.#
a2ec0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28  define OSTRACE1(
a2ed0 58 29 20 20 20 20 20 20 20 20 20 69 66 28 20 73  X)         if( s
a2ee0 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20  qlite3OSTrace ) 
a2ef0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
a2f00 74 66 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53  tf(X).#define OS
a2f10 54 52 41 43 45 32 28 58 2c 59 29 20 20 20 20 20  TRACE2(X,Y)     
a2f20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54    if( sqlite3OST
a2f30 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65  race ) sqlite3De
a2f40 62 75 67 50 72 69 6e 74 66 28 58 2c 59 29 0a 23  bugPrintf(X,Y).#
a2f50 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28  define OSTRACE3(
a2f60 58 2c 59 2c 5a 29 20 20 20 20 20 69 66 28 20 73  X,Y,Z)     if( s
a2f70 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20  qlite3OSTrace ) 
a2f80 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
a2f90 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e  tf(X,Y,Z).#defin
a2fa0 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a  e OSTRACE4(X,Y,Z
a2fb0 2c 41 29 20 20 20 69 66 28 20 73 71 6c 69 74 65  ,A)   if( sqlite
a2fc0 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74  3OSTrace ) sqlit
a2fd0 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
a2fe0 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f  Y,Z,A).#define O
a2ff0 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c  STRACE5(X,Y,Z,A,
a3000 42 29 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53  B) if( sqlite3OS
a3010 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44  Trace ) sqlite3D
a3020 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a  ebugPrintf(X,Y,Z
a3030 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,A,B).#define OS
a3040 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42  TRACE6(X,Y,Z,A,B
a3050 2c 43 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c  ,C) \.    if(sql
a3060 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c  ite3OSTrace) sql
a3070 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
a3080 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65  X,Y,Z,A,B,C).#de
a3090 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c  fine OSTRACE7(X,
a30a0 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a 20  Y,Z,A,B,C,D) \. 
a30b0 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54     if(sqlite3OST
a30c0 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62  race) sqlite3Deb
a30d0 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41  ugPrintf(X,Y,Z,A
a30e0 2c 42 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64  ,B,C,D).#else.#d
a30f0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58  efine OSTRACE1(X
a3100 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
a3110 45 32 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20  E2(X,Y).#define 
a3120 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a  OSTRACE3(X,Y,Z).
a3130 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34  #define OSTRACE4
a3140 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e  (X,Y,Z,A).#defin
a3150 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a  e OSTRACE5(X,Y,Z
a3160 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,A,B).#define OS
a3170 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42  TRACE6(X,Y,Z,A,B
a3180 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,C).#define OSTR
a3190 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  ACE7(X,Y,Z,A,B,C
a31a0 2c 44 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ,D).#endif../*.*
a31b0 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 70 65 72  * Macros for per
a31c0 66 6f 72 6d 61 6e 63 65 20 74 72 61 63 69 6e 67  formance tracing
a31d0 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e  .  Normally turn
a31e0 65 64 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f  ed off.  Only wo
a31f0 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68  rks.** on i486 h
a3200 61 72 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64  ardware..*/.#ifd
a3210 65 66 20 53 51 4c 49 54 45 5f 50 45 52 46 4f 52  ef SQLITE_PERFOR
a3220 4d 41 4e 43 45 5f 54 52 41 43 45 0a 5f 5f 69 6e  MANCE_TRACE.__in
a3230 6c 69 6e 65 5f 5f 20 75 6e 73 69 67 6e 65 64 20  line__ unsigned 
a3240 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 20 68 77  long long int hw
a3250 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 75 6e  time(void){.  un
a3260 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
a3270 20 69 6e 74 20 78 3b 0a 20 20 5f 5f 61 73 6d 5f   int x;.  __asm_
a3280 5f 28 22 72 64 74 73 63 5c 6e 5c 74 22 0a 20 20  _("rdtsc\n\t".  
a3290 20 20 20 20 20 20 20 20 22 6d 6f 76 20 25 25 65          "mov %%e
a32a0 64 78 2c 20 25 25 65 63 78 5c 6e 5c 74 22 0a 20  dx, %%ecx\n\t". 
a32b0 20 20 20 20 20 20 20 20 20 3a 22 3d 41 22 20 28           :"=A" (
a32c0 78 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 78 3b  x));.  return x;
a32d0 0a 7d 0a 73 74 61 74 69 63 20 75 6e 73 69 67 6e  .}.static unsign
a32e0 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74  ed long long int
a32f0 20 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 69 63   g_start;.static
a3300 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 65 6c   unsigned int el
a3310 61 70 73 65 3b 0a 23 64 65 66 69 6e 65 20 54 49  apse;.#define TI
a3320 4d 45 52 5f 53 54 41 52 54 20 20 20 20 20 20 20  MER_START       
a3330 67 5f 73 74 61 72 74 3d 68 77 74 69 6d 65 28 29  g_start=hwtime()
a3340 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45  .#define TIMER_E
a3350 4e 44 20 20 20 20 20 20 20 20 20 65 6c 61 70 73  ND         elaps
a3360 65 3d 68 77 74 69 6d 65 28 29 2d 67 5f 73 74 61  e=hwtime()-g_sta
a3370 72 74 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52  rt.#define TIMER
a3380 5f 45 4c 41 50 53 45 44 20 20 20 20 20 65 6c 61  _ELAPSED     ela
a3390 70 73 65 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e  pse.#else.#defin
a33a0 65 20 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 64  e TIMER_START.#d
a33b0 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a  efine TIMER_END.
a33c0 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c  #define TIMER_EL
a33d0 41 50 53 45 44 20 20 20 20 20 30 0a 23 65 6e 64  APSED     0.#end
a33e0 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20  if../*.** If we 
a33f0 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 74 68 65  compile with the
a3400 20 53 51 4c 49 54 45 5f 54 45 53 54 20 6d 61 63   SQLITE_TEST mac
a3410 72 6f 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65  ro set, then the
a3420 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b   following block
a3430 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c  .** of code will
a3440 20 67 69 76 65 20 75 73 20 74 68 65 20 61 62 69   give us the abi
a3450 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65  lity to simulate
a3460 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f   a disk I/O erro
a3470 72 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 75  r.  This.** is u
a3480 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20  sed for testing 
a3490 74 68 65 20 49 2f 4f 20 72 65 63 6f 76 65 72 79  the I/O recovery
a34a0 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65   logic..*/.#ifde
a34b0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
a34c0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
a34d0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69  ite3_io_error_hi
a34e0 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  t = 0;          
a34f0 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65    /* Total numbe
a3500 72 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72 73 20  r of I/O Errors 
a3510 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
a3520 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  t sqlite3_io_err
a3530 6f 72 5f 68 61 72 64 68 69 74 20 3d 20 30 3b 20  or_hardhit = 0; 
a3540 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
a3550 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65   of non-benign e
a3560 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f  rrors */.SQLITE_
a3570 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
a3580 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67  io_error_pending
a3590 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
a35a0 43 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69  Count down to fi
a35b0 72 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f  rst I/O error */
a35c0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
a35d0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
a35e0 5f 70 65 72 73 69 73 74 20 3d 20 30 3b 20 20 20  _persist = 0;   
a35f0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
a3600 49 2f 4f 20 65 72 72 6f 72 73 20 70 65 72 73 69  I/O errors persi
a3610 73 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  st */.SQLITE_API
a3620 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f   int sqlite3_io_
a3630 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30  error_benign = 0
a3640 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ;         /* Tru
a3650 65 20 69 66 20 65 72 72 6f 72 73 20 61 72 65 20  e if errors are 
a3660 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45  benign */.SQLITE
a3670 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
a3680 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e  _diskfull_pendin
a3690 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50  g = 0;.SQLITE_AP
a36a0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69  I int sqlite3_di
a36b0 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66  skfull = 0;.#def
a36c0 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ine SimulateIOEr
a36d0 72 6f 72 42 65 6e 69 67 6e 28 58 29 20 73 71 6c  rorBenign(X) sql
a36e0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65  ite3_io_error_be
a36f0 6e 69 67 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65  nign=(X).#define
a3700 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
a3710 28 43 4f 44 45 29 20 20 5c 0a 20 20 69 66 28 20  (CODE)  \.  if( 
a3720 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f  (sqlite3_io_erro
a3730 72 5f 70 65 72 73 69 73 74 20 26 26 20 73 71 6c  r_persist && sql
a3740 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69  ite3_io_error_hi
a3750 74 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73  t) \.       || s
a3760 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
a3770 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29  pending-- == 1 )
a3780 20 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20    \.            
a3790 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28    { local_ioerr(
a37a0 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69  ); CODE; }.stati
a37b0 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65  c void local_ioe
a37c0 72 72 28 29 7b 0a 20 20 49 4f 54 52 41 43 45 28  rr(){.  IOTRACE(
a37d0 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20  ("IOERR\n"));.  
a37e0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
a37f0 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73  _hit++;.  if( !s
a3800 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
a3810 62 65 6e 69 67 6e 20 29 20 73 71 6c 69 74 65 33  benign ) sqlite3
a3820 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69  _io_error_hardhi
a3830 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53  t++;.}.#define S
a3840 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45  imulateDiskfullE
a3850 72 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20 20 20  rror(CODE) \.   
a3860 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b  if( sqlite3_disk
a3870 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20  full_pending ){ 
a3880 5c 0a 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  \.     if( sqlit
a3890 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64  e3_diskfull_pend
a38a0 69 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20  ing == 1 ){ \.  
a38b0 20 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72       local_ioerr
a38c0 28 29 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c  (); \.       sql
a38d0 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20  ite3_diskfull = 
a38e0 31 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69  1; \.       sqli
a38f0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74  te3_io_error_hit
a3900 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 43   = 1; \.       C
a3910 4f 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73  ODE; \.     }els
a3920 65 7b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69  e{ \.       sqli
a3930 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e  te3_diskfull_pen
a3940 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d  ding--; \.     }
a3950 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64   \.   }.#else.#d
a3960 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f  efine SimulateIO
a3970 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23  ErrorBenign(X).#
a3980 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49  define SimulateI
a3990 4f 45 72 72 6f 72 28 41 29 0a 23 64 65 66 69 6e  OError(A).#defin
a39a0 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75  e SimulateDiskfu
a39b0 6c 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e 64 69  llError(A).#endi
a39c0 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65  f../*.** When te
a39d0 73 74 69 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f  sting, keep a co
a39e0 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  unt of the numbe
a39f0 72 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e  r of open files.
a3a00 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
a3a10 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50  E_TEST.SQLITE_AP
a3a20 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70  I int sqlite3_op
a3a30 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20  en_file_count = 
a3a40 30 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43  0;.#define OpenC
a3a50 6f 75 6e 74 65 72 28 58 29 20 20 73 71 6c 69 74  ounter(X)  sqlit
a3a60 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75  e3_open_file_cou
a3a70 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64  nt+=(X).#else.#d
a3a80 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65  efine OpenCounte
a3a90 72 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a  r(X).#endif../**
a3aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
a3ab0 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20   of os_common.h 
a3ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
a3af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
a3b00 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
a3b10 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f   left off in os_
a3b20 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  os2.c **********
a3b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
a3b40 0a 2a 2a 20 54 68 65 20 6f 73 32 46 69 6c 65 20  .** The os2File 
a3b50 73 74 72 75 63 74 75 72 65 20 69 73 20 73 75 62  structure is sub
a3b60 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33  class of sqlite3
a3b70 5f 66 69 6c 65 20 73 70 65 63 69 66 69 63 20 66  _file specific f
a3b80 6f 72 20 74 68 65 20 4f 53 2f 32 0a 2a 2a 20 70  or the OS/2.** p
a3b90 72 6f 74 61 62 69 6c 69 74 79 20 6c 61 79 65 72  rotability layer
a3ba0 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
a3bb0 75 63 74 20 6f 73 32 46 69 6c 65 20 6f 73 32 46  uct os2File os2F
a3bc0 69 6c 65 3b 0a 73 74 72 75 63 74 20 6f 73 32 46  ile;.struct os2F
a3bd0 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71  ile {.  const sq
a3be0 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
a3bf0 20 2a 70 4d 65 74 68 6f 64 3b 20 20 2f 2a 20 41   *pMethod;  /* A
a3c00 6c 77 61 79 73 20 74 68 65 20 66 69 72 73 74 20  lways the first 
a3c10 65 6e 74 72 79 20 2a 2f 0a 20 20 48 46 49 4c 45  entry */.  HFILE
a3c20 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   h;             
a3c30 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66       /* Handle f
a3c40 6f 72 20 61 63 63 65 73 73 69 6e 67 20 74 68 65  or accessing the
a3c50 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 2a   file */.  char*
a3c60 20 70 61 74 68 54 6f 44 65 6c 3b 20 20 20 20 20   pathToDel;     
a3c70 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
a3c80 66 69 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 6f  file to delete o
a3c90 6e 20 63 6c 6f 73 65 2c 20 4e 55 4c 4c 20 69 66  n close, NULL if
a3ca0 20 6e 6f 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e   not */.  unsign
a3cb0 65 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65  ed char locktype
a3cc0 3b 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 6c  ;   /* Type of l
a3cd0 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 20 68 65  ock currently he
a3ce0 6c 64 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20  ld on this file 
a3cf0 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 4c  */.};..#define L
a3d00 4f 43 4b 5f 54 49 4d 45 4f 55 54 20 31 30 4c 20  OCK_TIMEOUT 10L 
a3d10 2f 2a 20 74 68 65 20 64 65 66 61 75 6c 74 20 6c  /* the default l
a3d20 6f 63 6b 69 6e 67 20 74 69 6d 65 6f 75 74 20 2a  ocking timeout *
a3d30 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
a3d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3d80 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 72  *.** The next gr
a3d90 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 20  oup of routines 
a3da0 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 49 2f  implement the I/
a3db0 4f 20 6d 65 74 68 6f 64 73 20 73 70 65 63 69 66  O methods specif
a3dc0 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 73 71  ied.** by the sq
a3dd0 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
a3de0 20 6f 62 6a 65 63 74 2e 0a 2a 2a 2a 2a 2a 2a 2a   object..*******
a3df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3e30 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
a3e40 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2f  Close a file..*/
a3e50 0a 69 6e 74 20 6f 73 32 43 6c 6f 73 65 28 20 73  .int os2Close( s
a3e60 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 20  qlite3_file *id 
a3e70 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d  ){.  APIRET rc =
a3e80 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f 73 32   NO_ERROR;.  os2
a3e90 46 69 6c 65 20 2a 70 46 69 6c 65 3b 0a 20 20 69  File *pFile;.  i
a3ea0 66 28 20 69 64 20 26 26 20 28 70 46 69 6c 65 20  f( id && (pFile 
a3eb0 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 20  = (os2File*)id) 
a3ec0 21 3d 20 30 20 29 7b 0a 20 20 20 20 4f 53 54 52  != 0 ){.    OSTR
a3ed0 41 43 45 32 28 20 22 43 4c 4f 53 45 20 25 64 5c  ACE2( "CLOSE %d\
a3ee0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a  n", pFile->h );.
a3ef0 20 20 20 20 72 63 20 3d 20 44 6f 73 43 6c 6f 73      rc = DosClos
a3f00 65 28 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20  e( pFile->h );. 
a3f10 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
a3f20 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20  pe = NO_LOCK;.  
a3f30 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 61 74    if( pFile->pat
a3f40 68 54 6f 44 65 6c 20 21 3d 20 4e 55 4c 4c 20 29  hToDel != NULL )
a3f50 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 44 6f 73  {.      rc = Dos
a3f60 46 6f 72 63 65 44 65 6c 65 74 65 28 20 28 50 53  ForceDelete( (PS
a3f70 5a 29 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44  Z)pFile->pathToD
a3f80 65 6c 20 29 3b 0a 20 20 20 20 20 20 66 72 65 65  el );.      free
a3f90 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44  ( pFile->pathToD
a3fa0 65 6c 20 29 3b 0a 20 20 20 20 20 20 70 46 69 6c  el );.      pFil
a3fb0 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 3d 20 4e  e->pathToDel = N
a3fc0 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ULL;.    }.    i
a3fd0 64 20 3d 20 30 3b 0a 20 20 20 20 4f 70 65 6e 43  d = 0;.    OpenC
a3fe0 6f 75 6e 74 65 72 28 20 2d 31 20 29 3b 0a 20 20  ounter( -1 );.  
a3ff0 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 20 3d  }..  return rc =
a4000 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c  = NO_ERROR ? SQL
a4010 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f  ITE_OK : SQLITE_
a4020 49 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  IOERR;.}../*.** 
a4030 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61  Read data from a
a4040 20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 66   file into a buf
a4050 66 65 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c  fer.  Return SQL
a4060 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a  ITE_OK if all.**
a4070 20 62 79 74 65 73 20 77 65 72 65 20 72 65 61 64   bytes were read
a4080 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6e   successfully an
a4090 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 69  d SQLITE_IOERR i
a40a0 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a  f anything goes.
a40b0 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 69 6e 74  ** wrong..*/.int
a40c0 20 6f 73 32 52 65 61 64 28 0a 20 20 73 71 6c 69   os2Read(.  sqli
a40d0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20  te3_file *id,   
a40e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
a40f0 69 6c 65 20 74 6f 20 72 65 61 64 20 66 72 6f 6d  ile to read from
a4100 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 66   */.  void *pBuf
a4110 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
a4120 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63        /* Write c
a4130 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68 69 73  ontent into this
a4140 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74   buffer */.  int
a4150 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20   amt,           
a4160 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a4170 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
a4180 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c  to read */.  sql
a4190 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65  ite3_int64 offse
a41a0 74 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t            /* 
a41b0 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61 74  Begin reading at
a41c0 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a   this offset */.
a41d0 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65 4c  ){.  ULONG fileL
a41e0 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20 20  ocation = 0L;.  
a41f0 55 4c 4f 4e 47 20 67 6f 74 3b 0a 20 20 6f 73 32  ULONG got;.  os2
a4200 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f  File *pFile = (o
a4210 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 73  s2File*)id;.  as
a4220 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20  sert( id!=0 );. 
a4230 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
a4240 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ( return SQLITE_
a4250 49 4f 45 52 52 5f 52 45 41 44 20 29 3b 0a 20 20  IOERR_READ );.  
a4260 4f 53 54 52 41 43 45 33 28 20 22 52 45 41 44 20  OSTRACE3( "READ 
a4270 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70  %d lock=%d\n", p
a4280 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e  File->h, pFile->
a4290 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 69 66  locktype );.  if
a42a0 28 20 44 6f 73 53 65 74 46 69 6c 65 50 74 72 28  ( DosSetFilePtr(
a42b0 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 66 73 65 74  pFile->h, offset
a42c0 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c 20 26 66  , FILE_BEGIN, &f
a42d0 69 6c 65 4c 6f 63 61 74 69 6f 6e 29 20 21 3d 20  ileLocation) != 
a42e0 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
a42f0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
a4300 45 52 52 3b 0a 20 20 7d 0a 20 20 69 66 28 20 44  ERR;.  }.  if( D
a4310 6f 73 52 65 61 64 28 20 70 46 69 6c 65 2d 3e 68  osRead( pFile->h
a4320 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 26 67 6f  , pBuf, amt, &go
a4330 74 20 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20  t ) != NO_ERROR 
a4340 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
a4350 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b  LITE_IOERR_READ;
a4360 0a 20 20 7d 0a 20 20 69 66 28 20 67 6f 74 20 3d  .  }.  if( got =
a4370 3d 20 28 55 4c 4f 4e 47 29 61 6d 74 20 29 0a 20  = (ULONG)amt ). 
a4380 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
a4390 5f 4f 4b 3b 0a 20 20 65 6c 73 65 20 7b 0a 20 20  _OK;.  else {.  
a43a0 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72    memset(&((char
a43b0 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c  *)pBuf)[got], 0,
a43c0 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72   amt-got);.    r
a43d0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
a43e0 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20  RR_SHORT_READ;. 
a43f0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74   }.}../*.** Writ
a4400 65 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75  e data from a bu
a4410 66 66 65 72 20 69 6e 74 6f 20 61 20 66 69 6c 65  ffer into a file
a4420 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  .  Return SQLITE
a4430 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a  _OK on success.*
a4440 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20  * or some other 
a4450 65 72 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61  error code on fa
a4460 69 6c 75 72 65 2e 0a 2a 2f 0a 69 6e 74 20 6f 73  ilure..*/.int os
a4470 32 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65  2Write(.  sqlite
a4480 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20  3_file *id,     
a4490 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c            /* Fil
a44a0 65 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20  e to write into 
a44b0 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
a44c0 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 20  *pBuf,          
a44d0 20 20 20 20 20 2f 2a 20 54 68 65 20 62 79 74 65       /* The byte
a44e0 73 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20  s to be written 
a44f0 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20  */.  int amt,   
a4500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a4510 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
a4520 66 20 62 79 74 65 73 20 74 6f 20 77 72 69 74 65  f bytes to write
a4530 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e   */.  sqlite3_in
a4540 74 36 34 20 6f 66 66 73 65 74 20 20 20 20 20 20  t64 offset      
a4550 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20        /* Offset 
a4560 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 74 6f  into the file to
a4570 20 62 65 67 69 6e 20 77 72 69 74 69 6e 67 20 61   begin writing a
a4580 74 20 2a 2f 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20  t */.){.  ULONG 
a4590 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 20 3d 20 30  fileLocation = 0
a45a0 4c 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d  L;.  APIRET rc =
a45b0 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 55 4c 4f   NO_ERROR;.  ULO
a45c0 4e 47 20 77 72 6f 74 65 3b 0a 20 20 6f 73 32 46  NG wrote;.  os2F
a45d0 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73  ile *pFile = (os
a45e0 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 73 73  2File*)id;.  ass
a45f0 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20  ert( id!=0 );.  
a4600 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
a4610 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
a4620 4f 45 52 52 5f 57 52 49 54 45 20 29 3b 0a 20 20  OERR_WRITE );.  
a4630 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c  SimulateDiskfull
a4640 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
a4650 4c 49 54 45 5f 46 55 4c 4c 20 29 3b 0a 20 20 4f  LITE_FULL );.  O
a4660 53 54 52 41 43 45 33 28 20 22 57 52 49 54 45 20  STRACE3( "WRITE 
a4670 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70  %d lock=%d\n", p
a4680 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e  File->h, pFile->
a4690 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 69 66  locktype );.  if
a46a0 28 20 44 6f 73 53 65 74 46 69 6c 65 50 74 72 28  ( DosSetFilePtr(
a46b0 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 66 73 65 74  pFile->h, offset
a46c0 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c 20 26 66  , FILE_BEGIN, &f
a46d0 69 6c 65 4c 6f 63 61 74 69 6f 6e 29 20 21 3d 20  ileLocation) != 
a46e0 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
a46f0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
a4700 45 52 52 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  ERR;.  }.  asser
a4710 74 28 20 61 6d 74 3e 30 20 29 3b 0a 20 20 77 68  t( amt>0 );.  wh
a4720 69 6c 65 28 20 61 6d 74 20 3e 20 30 20 26 26 0a  ile( amt > 0 &&.
a4730 20 20 20 20 20 20 20 20 20 28 20 72 63 20 3d 20           ( rc = 
a4740 44 6f 73 57 72 69 74 65 28 20 70 46 69 6c 65 2d  DosWrite( pFile-
a4750 3e 68 2c 20 28 50 56 4f 49 44 29 70 42 75 66 2c  >h, (PVOID)pBuf,
a4760 20 61 6d 74 2c 20 26 77 72 6f 74 65 20 29 20 29   amt, &wrote ) )
a4770 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 26 26 0a   == NO_ERROR &&.
a4780 20 20 20 20 20 20 20 20 20 77 72 6f 74 65 20 3e           wrote >
a4790 20 30 0a 20 20 29 7b 0a 20 20 20 20 61 6d 74 20   0.  ){.    amt 
a47a0 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 42  -= wrote;.    pB
a47b0 75 66 20 3d 20 26 28 28 63 68 61 72 2a 29 70 42  uf = &((char*)pB
a47c0 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a  uf)[wrote];.  }.
a47d0 0a 20 20 72 65 74 75 72 6e 20 28 20 72 63 20 21  .  return ( rc !
a47e0 3d 20 4e 4f 5f 45 52 52 4f 52 20 7c 7c 20 61 6d  = NO_ERROR || am
a47f0 74 20 3e 20 28 69 6e 74 29 77 72 6f 74 65 20 29  t > (int)wrote )
a4800 20 3f 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 3a   ? SQLITE_FULL :
a4810 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
a4820 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 61 6e  *.** Truncate an
a4830 20 6f 70 65 6e 20 66 69 6c 65 20 74 6f 20 61 20   open file to a 
a4840 73 70 65 63 69 66 69 65 64 20 73 69 7a 65 0a 2a  specified size.*
a4850 2f 0a 69 6e 74 20 6f 73 32 54 72 75 6e 63 61 74  /.int os2Truncat
a4860 65 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  e( sqlite3_file 
a4870 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 65 20 29  *id, i64 nByte )
a4880 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20  {.  APIRET rc = 
a4890 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f 73 32 46  NO_ERROR;.  os2F
a48a0 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73  ile *pFile = (os
a48b0 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 4f 53 54  2File*)id;.  OST
a48c0 52 41 43 45 33 28 20 22 54 52 55 4e 43 41 54 45  RACE3( "TRUNCATE
a48d0 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 46 69   %d %lld\n", pFi
a48e0 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b 0a  le->h, nByte );.
a48f0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
a4900 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
a4910 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 20  _IOERR_TRUNCATE 
a4920 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 53 65 74  );.  rc = DosSet
a4930 46 69 6c 65 53 69 7a 65 28 20 70 46 69 6c 65 2d  FileSize( pFile-
a4940 3e 68 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20 72  >h, nByte );.  r
a4950 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45  eturn rc == NO_E
a4960 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b  RROR ? SQLITE_OK
a4970 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b   : SQLITE_IOERR;
a4980 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .}..#ifdef SQLIT
a4990 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75  E_TEST./*.** Cou
a49a0 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  nt the number of
a49b0 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e   fullsyncs and n
a49c0 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54 68  ormal syncs.  Th
a49d0 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 74 65  is is used to te
a49e0 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e 63 73  st.** that syncs
a49f0 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73 20 61   and fullsyncs a
a4a00 72 65 20 6f 63 63 75 72 69 6e 67 20 61 74 20 74  re occuring at t
a4a10 68 65 20 72 69 67 68 74 20 74 69 6d 65 73 2e 0a  he right times..
a4a20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
a4a30 74 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63  t sqlite3_sync_c
a4a40 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45  ount = 0;.SQLITE
a4a50 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
a4a60 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20  _fullsync_count 
a4a70 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  = 0;.#endif../*.
a4a80 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c  ** Make sure all
a4a90 20 77 72 69 74 65 73 20 74 6f 20 61 20 70 61 72   writes to a par
a4aa0 74 69 63 75 6c 61 72 20 66 69 6c 65 20 61 72 65  ticular file are
a4ab0 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f 20 64 69   committed to di
a4ac0 73 6b 2e 0a 2a 2f 0a 69 6e 74 20 6f 73 32 53 79  sk..*/.int os2Sy
a4ad0 6e 63 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  nc( sqlite3_file
a4ae0 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 20   *id, int flags 
a4af0 29 7b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46  ){.  os2File *pF
a4b00 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29  ile = (os2File*)
a4b10 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20  id;.  OSTRACE3( 
a4b20 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b 3d 25 64  "SYNC %d lock=%d
a4b30 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70  \n", pFile->h, p
a4b40 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29  File->locktype )
a4b50 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
a4b60 54 45 53 54 0a 20 20 69 66 28 20 66 6c 61 67 73  TEST.  if( flags
a4b70 20 26 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46   & SQLITE_SYNC_F
a4b80 55 4c 4c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ULL){.    sqlite
a4b90 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74  3_fullsync_count
a4ba0 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ++;.  }.  sqlite
a4bb0 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a  3_sync_count++;.
a4bc0 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
a4bd0 44 6f 73 52 65 73 65 74 42 75 66 66 65 72 28 20  DosResetBuffer( 
a4be0 70 46 69 6c 65 2d 3e 68 20 29 20 3d 3d 20 4e 4f  pFile->h ) == NO
a4bf0 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f  _ERROR ? SQLITE_
a4c00 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52  OK : SQLITE_IOER
a4c10 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65  R;.}../*.** Dete
a4c20 72 6d 69 6e 65 20 74 68 65 20 63 75 72 72 65 6e  rmine the curren
a4c30 74 20 73 69 7a 65 20 6f 66 20 61 20 66 69 6c 65  t size of a file
a4c40 20 69 6e 20 62 79 74 65 73 0a 2a 2f 0a 69 6e 74   in bytes.*/.int
a4c50 20 6f 73 32 46 69 6c 65 53 69 7a 65 28 20 73 71   os2FileSize( sq
a4c60 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
a4c70 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70  sqlite3_int64 *p
a4c80 53 69 7a 65 20 29 7b 0a 20 20 41 50 49 52 45 54  Size ){.  APIRET
a4c90 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a   rc = NO_ERROR;.
a4ca0 20 20 46 49 4c 45 53 54 41 54 55 53 33 20 66 73    FILESTATUS3 fs
a4cb0 74 73 33 46 69 6c 65 49 6e 66 6f 3b 0a 20 20 6d  ts3FileInfo;.  m
a4cc0 65 6d 73 65 74 28 26 66 73 74 73 33 46 69 6c 65  emset(&fsts3File
a4cd0 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Info, 0, sizeof(
a4ce0 66 73 74 73 33 46 69 6c 65 49 6e 66 6f 29 29 3b  fsts3FileInfo));
a4cf0 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30  .  assert( id!=0
a4d00 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f   );.  SimulateIO
a4d10 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
a4d20 4c 49 54 45 5f 49 4f 45 52 52 20 29 3b 0a 20 20  LITE_IOERR );.  
a4d30 72 63 20 3d 20 44 6f 73 51 75 65 72 79 46 69 6c  rc = DosQueryFil
a4d40 65 49 6e 66 6f 28 20 28 28 6f 73 32 46 69 6c 65  eInfo( ((os2File
a4d50 2a 29 69 64 29 2d 3e 68 2c 20 46 49 4c 5f 53 54  *)id)->h, FIL_ST
a4d60 41 4e 44 41 52 44 2c 20 26 66 73 74 73 33 46 69  ANDARD, &fsts3Fi
a4d70 6c 65 49 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 46  leInfo, sizeof(F
a4d80 49 4c 45 53 54 41 54 55 53 33 29 20 29 3b 0a 20  ILESTATUS3) );. 
a4d90 20 69 66 28 20 72 63 20 3d 3d 20 4e 4f 5f 45 52   if( rc == NO_ER
a4da0 52 4f 52 20 29 7b 0a 20 20 20 20 2a 70 53 69 7a  ROR ){.    *pSiz
a4db0 65 20 3d 20 66 73 74 73 33 46 69 6c 65 49 6e 66  e = fsts3FileInf
a4dc0 6f 2e 63 62 46 69 6c 65 3b 0a 20 20 20 20 72 65  o.cbFile;.    re
a4dd0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
a4de0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
a4df0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
a4e00 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ;.  }.}../*.** A
a4e10 63 71 75 69 72 65 20 61 20 72 65 61 64 65 72 20  cquire a reader 
a4e20 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lock..*/.static 
a4e30 69 6e 74 20 67 65 74 52 65 61 64 4c 6f 63 6b 28  int getReadLock(
a4e40 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20   os2File *pFile 
a4e50 29 7b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c  ){.  FILELOCK  L
a4e60 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20  ockArea,.       
a4e70 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b       UnlockArea;
a4e80 0a 20 20 41 50 49 52 45 54 20 72 65 73 3b 0a 20  .  APIRET res;. 
a4e90 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65   memset(&LockAre
a4ea0 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63  a, 0, sizeof(Loc
a4eb0 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65  kArea));.  memse
a4ec0 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30  t(&UnlockArea, 0
a4ed0 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41  , sizeof(UnlockA
a4ee0 72 65 61 29 29 3b 0a 20 20 4c 6f 63 6b 41 72 65  rea));.  LockAre
a4ef0 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48 41 52  a.lOffset = SHAR
a4f00 45 44 5f 46 49 52 53 54 3b 0a 20 20 4c 6f 63 6b  ED_FIRST;.  Lock
a4f10 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48  Area.lRange = SH
a4f20 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 55 6e 6c  ARED_SIZE;.  Unl
a4f30 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
a4f40 3d 20 30 4c 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72  = 0L;.  UnlockAr
a4f50 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a  ea.lRange = 0L;.
a4f60 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69    res = DosSetFi
a4f70 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e  leLocks( pFile->
a4f80 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20  h, &UnlockArea, 
a4f90 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f  &LockArea, LOCK_
a4fa0 54 49 4d 45 4f 55 54 2c 20 31 4c 20 29 3b 0a 20  TIMEOUT, 1L );. 
a4fb0 20 4f 53 54 52 41 43 45 33 28 20 22 47 45 54 52   OSTRACE3( "GETR
a4fc0 45 41 44 4c 4f 43 4b 20 25 64 20 72 65 73 3d 25  EADLOCK %d res=%
a4fd0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
a4fe0 72 65 73 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  res );.  return 
a4ff0 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e  res;.}../*.** Un
a5000 64 6f 20 61 20 72 65 61 64 6c 6f 63 6b 0a 2a 2f  do a readlock.*/
a5010 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 6c 6f  .static int unlo
a5020 63 6b 52 65 61 64 4c 6f 63 6b 28 20 6f 73 32 46  ckReadLock( os2F
a5030 69 6c 65 20 2a 69 64 20 29 7b 0a 20 20 46 49 4c  ile *id ){.  FIL
a5040 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c  ELOCK  LockArea,
a5050 0a 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c  .            Unl
a5060 6f 63 6b 41 72 65 61 3b 0a 20 20 41 50 49 52 45  ockArea;.  APIRE
a5070 54 20 72 65 73 3b 0a 20 20 6d 65 6d 73 65 74 28  T res;.  memset(
a5080 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69  &LockArea, 0, si
a5090 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b  zeof(LockArea));
a50a0 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63  .  memset(&Unloc
a50b0 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66  kArea, 0, sizeof
a50c0 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20  (UnlockArea));. 
a50d0 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65   LockArea.lOffse
a50e0 74 20 3d 20 30 4c 3b 0a 20 20 4c 6f 63 6b 41 72  t = 0L;.  LockAr
a50f0 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a  ea.lRange = 0L;.
a5100 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66    UnlockArea.lOf
a5110 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49  fset = SHARED_FI
a5120 52 53 54 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65  RST;.  UnlockAre
a5130 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45  a.lRange = SHARE
a5140 44 5f 53 49 5a 45 3b 0a 20 20 72 65 73 20 3d 20  D_SIZE;.  res = 
a5150 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28  DosSetFileLocks(
a5160 20 69 64 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41   id->h, &UnlockA
a5170 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20  rea, &LockArea, 
a5180 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 31 4c  LOCK_TIMEOUT, 1L
a5190 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20   );.  OSTRACE3( 
a51a0 22 55 4e 4c 4f 43 4b 2d 52 45 41 44 4c 4f 43 4b  "UNLOCK-READLOCK
a51b0 20 66 69 6c 65 20 68 61 6e 64 6c 65 3d 25 64 20   file handle=%d 
a51c0 72 65 73 3d 25 64 3f 5c 6e 22 2c 20 69 64 2d 3e  res=%d?\n", id->
a51d0 68 2c 20 72 65 73 20 29 3b 0a 20 20 72 65 74 75  h, res );.  retu
a51e0 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rn res;.}../*.**
a51f0 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77   Lock the file w
a5200 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65  ith the lock spe
a5210 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65  cified by parame
a5220 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f  ter locktype - o
a5230 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c  ne.** of the fol
a5240 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  lowing:.**.**   
a5250 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43    (1) SHARED_LOC
a5260 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53  K.**     (2) RES
a5270 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  ERVED_LOCK.**   
a5280 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f    (3) PENDING_LO
a5290 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58  CK.**     (4) EX
a52a0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a  CLUSIVE_LOCK.**.
a52b0 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65  ** Sometimes whe
a52c0 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65  n requesting one
a52d0 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64   lock state, add
a52e0 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61  itional lock sta
a52f0 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72  tes.** are inser
a5300 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20  ted in between. 
a5310 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67   The locking mig
a5320 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f  ht fail on one o
a5330 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74  f the later.** t
a5340 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69  ransitions leavi
a5350 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74  ng the lock stat
a5360 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  e different from
a5370 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64   what it started
a5380 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68   but.** still sh
a5390 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e  ort of its goal.
a53a0 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
a53b0 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20  chart shows the 
a53c0 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73  allowed.** trans
a53d0 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69  itions and the i
a53e0 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64  nserted intermed
a53f0 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a  iate states:.**.
a5400 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d  **    UNLOCKED -
a5410 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53  > SHARED.**    S
a5420 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45  HARED -> RESERVE
a5430 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  D.**    SHARED -
a5440 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45  > (PENDING) -> E
a5450 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52  XCLUSIVE.**    R
a5460 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44  ESERVED -> (PEND
a5470 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56  ING) -> EXCLUSIV
a5480 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20  E.**    PENDING 
a5490 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a  -> EXCLUSIVE.**.
a54a0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
a54b0 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61  will only increa
a54c0 73 65 20 61 20 6c 6f 63 6b 2e 20 20 54 68 65 20  se a lock.  The 
a54d0 6f 73 32 55 6e 6c 6f 63 6b 28 29 20 72 6f 75 74  os2Unlock() rout
a54e0 69 6e 65 0a 2a 2a 20 65 72 61 73 65 73 20 61 6c  ine.** erases al
a54f0 6c 20 6c 6f 63 6b 73 20 61 74 20 6f 6e 63 65 20  l locks at once 
a5500 61 6e 64 20 72 65 74 75 72 6e 73 20 75 73 20 69  and returns us i
a5510 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 6c 6f  mmediately to lo
a5520 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a  cking level 0..*
a5530 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73  * It is not poss
a5540 69 62 6c 65 20 74 6f 20 6c 6f 77 65 72 20 74 68  ible to lower th
a5550 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
a5560 6f 6e 65 20 73 74 65 70 20 61 74 20 61 20 74 69  one step at a ti
a5570 6d 65 2e 20 20 59 6f 75 0a 2a 2a 20 6d 75 73 74  me.  You.** must
a5580 20 67 6f 20 73 74 72 61 69 67 68 74 20 74 6f 20   go straight to 
a5590 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30 2e  locking level 0.
a55a0 0a 2a 2f 0a 69 6e 74 20 6f 73 32 4c 6f 63 6b 28  .*/.int os2Lock(
a55b0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69   sqlite3_file *i
a55c0 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 20  d, int locktype 
a55d0 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
a55e0 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 2f  LITE_OK;       /
a55f0 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72  * Return code fr
a5600 6f 6d 20 73 75 62 72 6f 75 74 69 6e 65 73 20 2a  om subroutines *
a5610 2f 0a 20 20 41 50 49 52 45 54 20 72 65 73 20 3d  /.  APIRET res =
a5620 20 4e 4f 5f 45 52 52 4f 52 3b 20 20 20 20 2f 2a   NO_ERROR;    /*
a5630 20 52 65 73 75 6c 74 20 6f 66 20 61 6e 20 4f 53   Result of an OS
a5640 2f 32 20 6c 6f 63 6b 20 63 61 6c 6c 20 2a 2f 0a  /2 lock call */.
a5650 20 20 69 6e 74 20 6e 65 77 4c 6f 63 6b 74 79 70    int newLocktyp
a5660 65 3b 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20  e;       /* Set 
a5670 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
a5680 74 6f 20 74 68 69 73 20 76 61 6c 75 65 20 62 65  to this value be
a5690 66 6f 72 65 20 65 78 69 74 69 6e 67 20 2a 2f 0a  fore exiting */.
a56a0 20 20 69 6e 74 20 67 6f 74 50 65 6e 64 69 6e 67    int gotPending
a56b0 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54 72 75 65  Lock = 0;/* True
a56c0 20 69 66 20 77 65 20 61 63 71 75 69 72 65 64 20   if we acquired 
a56d0 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74  a PENDING lock t
a56e0 68 69 73 20 74 69 6d 65 20 2a 2f 0a 20 20 46 49  his time */.  FI
a56f0 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61  LELOCK  LockArea
a5700 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 55 6e  ,.            Un
a5710 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 6f 73 32 46  lockArea;.  os2F
a5720 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73  ile *pFile = (os
a5730 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 6d 65 6d  2File*)id;.  mem
a5740 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30  set(&LockArea, 0
a5750 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65  , sizeof(LockAre
a5760 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55  a));.  memset(&U
a5770 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69  nlockArea, 0, si
a5780 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29  zeof(UnlockArea)
a5790 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69  );.  assert( pFi
a57a0 6c 65 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41  le!=0 );.  OSTRA
a57b0 43 45 34 28 20 22 4c 4f 43 4b 20 25 64 20 25 64  CE4( "LOCK %d %d
a57c0 20 77 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c   was %d\n", pFil
a57d0 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20  e->h, locktype, 
a57e0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
a57f0 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72  );..  /* If ther
a5800 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c  e is already a l
a5810 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65  ock of this type
a5820 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63   or more restric
a5830 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a  tive on the.  **
a5840 20 6f 73 32 46 69 6c 65 2c 20 64 6f 20 6e 6f 74   os2File, do not
a5850 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20  hing. Don't use 
a5860 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78  the end_lock: ex
a5870 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a  it path, as.  **
a5880 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
a5890 6e 74 65 72 28 29 20 68 61 73 6e 27 74 20 62 65  nter() hasn't be
a58a0 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20  en called yet.. 
a58b0 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d   */.  if( pFile-
a58c0 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74  >locktype>=lockt
a58d0 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 41  ype ){.    OSTRA
a58e0 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 25 64  CE3( "LOCK %d %d
a58f0 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 65 6c   ok (already hel
a5900 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  d)\n", pFile->h,
a5910 20 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20   locktype );.   
a5920 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
a5930 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b  K;.  }..  /* Mak
a5940 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69  e sure the locki
a5950 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63  ng sequence is c
a5960 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73  orrect.  */.  as
a5970 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63  sert( pFile->loc
a5980 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c  ktype!=NO_LOCK |
a5990 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  | locktype==SHAR
a59a0 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73  ED_LOCK );.  ass
a59b0 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50  ert( locktype!=P
a59c0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20  ENDING_LOCK );. 
a59d0 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
a59e0 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e!=RESERVED_LOCK
a59f0 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74   || pFile->lockt
a5a00 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
a5a10 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74   );..  /* Lock t
a5a20 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20  he PENDING_LOCK 
a5a30 62 79 74 65 20 69 66 20 77 65 20 6e 65 65 64 20  byte if we need 
a5a40 74 6f 20 61 63 71 75 69 72 65 20 61 20 50 45 4e  to acquire a PEN
a5a50 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 20 2a  DING lock or.  *
a5a60 2a 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e  * a SHARED lock.
a5a70 20 20 49 66 20 77 65 20 61 72 65 20 61 63 71 75    If we are acqu
a5a80 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c  iring a SHARED l
a5a90 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 69 73 69  ock, the acquisi
a5aa0 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 68 65  tion of.  ** the
a5ab0 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79   PENDING_LOCK by
a5ac0 74 65 20 69 73 20 74 65 6d 70 6f 72 61 72 79 2e  te is temporary.
a5ad0 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 6b 74  .  */.  newLockt
a5ae0 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63  ype = pFile->loc
a5af0 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 70 46 69  ktype;.  if( pFi
a5b00 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f  le->locktype==NO
a5b10 5f 4c 4f 43 4b 0a 20 20 20 20 20 20 7c 7c 20 28  _LOCK.      || (
a5b20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53  locktype==EXCLUS
a5b30 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c  IVE_LOCK && pFil
a5b40 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53  e->locktype==RES
a5b50 45 52 56 45 44 5f 4c 4f 43 4b 29 0a 20 20 29 7b  ERVED_LOCK).  ){
a5b60 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f  .    LockArea.lO
a5b70 66 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47 5f  ffset = PENDING_
a5b80 42 59 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72  BYTE;.    LockAr
a5b90 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a  ea.lRange = 1L;.
a5ba0 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c      UnlockArea.l
a5bb0 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20  Offset = 0L;.   
a5bc0 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e   UnlockArea.lRan
a5bd0 67 65 20 3d 20 30 4c 3b 0a 0a 20 20 20 20 2f 2a  ge = 0L;..    /*
a5be0 20 77 61 69 74 20 6c 6f 6e 67 65 72 20 74 68 61   wait longer tha
a5bf0 6e 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 20 68  n LOCK_TIMEOUT h
a5c00 65 72 65 20 6e 6f 74 20 74 6f 20 68 61 76 65 20  ere not to have 
a5c10 74 6f 20 74 72 79 20 6d 75 6c 74 69 70 6c 65 20  to try multiple 
a5c20 74 69 6d 65 73 20 2a 2f 0a 20 20 20 20 72 65 73  times */.    res
a5c30 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63   = DosSetFileLoc
a5c40 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55  ks( pFile->h, &U
a5c50 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b  nlockArea, &Lock
a5c60 41 72 65 61 2c 20 31 30 30 4c 2c 20 30 4c 20 29  Area, 100L, 0L )
a5c70 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 3d 3d  ;.    if( res ==
a5c80 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
a5c90 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63     gotPendingLoc
a5ca0 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 4f 53 54  k = 1;.      OST
a5cb0 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20  RACE3( "LOCK %d 
a5cc0 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 62 6f 6f  pending lock boo
a5cd0 6c 65 61 6e 20 73 65 74 2e 20 20 72 65 73 3d 25  lean set.  res=%
a5ce0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
a5cf0 72 65 73 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  res );.    }.  }
a5d00 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61  ..  /* Acquire a
a5d10 20 73 68 61 72 65 64 20 6c 6f 63 6b 0a 20 20 2a   shared lock.  *
a5d20 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65  /.  if( locktype
a5d30 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26  ==SHARED_LOCK &&
a5d40 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52   res == NO_ERROR
a5d50 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
a5d60 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
a5d70 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20  =NO_LOCK );.    
a5d80 72 65 73 20 3d 20 67 65 74 52 65 61 64 4c 6f 63  res = getReadLoc
a5d90 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 69 66  k(pFile);.    if
a5da0 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f  ( res == NO_ERRO
a5db0 52 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f  R ){.      newLo
a5dc0 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f  cktype = SHARED_
a5dd0 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  LOCK;.    }.    
a5de0 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20  OSTRACE3( "LOCK 
a5df0 25 64 20 61 63 71 75 69 72 65 20 73 68 61 72 65  %d acquire share
a5e00 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c 6e  d lock. res=%d\n
a5e10 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73  ", pFile->h, res
a5e20 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63   );.  }..  /* Ac
a5e30 71 75 69 72 65 20 61 20 52 45 53 45 52 56 45 44  quire a RESERVED
a5e40 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28   lock.  */.  if(
a5e50 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52   locktype==RESER
a5e60 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20  VED_LOCK && res 
a5e70 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20  == NO_ERROR ){. 
a5e80 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65     assert( pFile
a5e90 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  ->locktype==SHAR
a5ea0 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 4c  ED_LOCK );.    L
a5eb0 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
a5ec0 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b  = RESERVED_BYTE;
a5ed0 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52  .    LockArea.lR
a5ee0 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55  ange = 1L;.    U
a5ef0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65  nlockArea.lOffse
a5f00 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f  t = 0L;.    Unlo
a5f10 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20  ckArea.lRange = 
a5f20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f  0L;.    res = Do
a5f30 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70  sSetFileLocks( p
a5f40 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b  File->h, &Unlock
a5f50 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c  Area, &LockArea,
a5f60 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30   LOCK_TIMEOUT, 0
a5f70 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73  L );.    if( res
a5f80 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   == NO_ERROR ){.
a5f90 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70        newLocktyp
a5fa0 65 20 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f 43  e = RESERVED_LOC
a5fb0 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 54  K;.    }.    OST
a5fc0 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20  RACE3( "LOCK %d 
a5fd0 61 63 71 75 69 72 65 20 72 65 73 65 72 76 65 64  acquire reserved
a5fe0 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c 6e 22   lock. res=%d\n"
a5ff0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20  , pFile->h, res 
a6000 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71  );.  }..  /* Acq
a6010 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c  uire a PENDING l
a6020 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c  ock.  */.  if( l
a6030 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49  ocktype==EXCLUSI
a6040 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d  VE_LOCK && res =
a6050 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
a6060 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20    newLocktype = 
a6070 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20  PENDING_LOCK;.  
a6080 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b    gotPendingLock
a6090 20 3d 20 30 3b 0a 20 20 20 20 4f 53 54 52 41 43   = 0;.    OSTRAC
a60a0 45 32 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 71  E2( "LOCK %d acq
a60b0 75 69 72 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63  uire pending loc
a60c0 6b 2e 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20  k. pending lock 
a60d0 62 6f 6f 6c 65 61 6e 20 75 6e 73 65 74 2e 5c 6e  boolean unset.\n
a60e0 22 2c 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20  ", pFile->h );. 
a60f0 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65   }..  /* Acquire
a6100 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   an EXCLUSIVE lo
a6110 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f  ck.  */.  if( lo
a6120 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56  cktype==EXCLUSIV
a6130 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d  E_LOCK && res ==
a6140 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
a6150 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e   assert( pFile->
a6160 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52 45 44  locktype>=SHARED
a6170 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73  _LOCK );.    res
a6180 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63   = unlockReadLoc
a6190 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53  k(pFile);.    OS
a61a0 54 52 41 43 45 32 28 20 22 75 6e 72 65 61 64 6c  TRACE2( "unreadl
a61b0 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73  ock = %d\n", res
a61c0 20 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61   );.    LockArea
a61d0 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45  .lOffset = SHARE
a61e0 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 4c 6f 63  D_FIRST;.    Loc
a61f0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53  kArea.lRange = S
a6200 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20  HARED_SIZE;.    
a6210 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73  UnlockArea.lOffs
a6220 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c  et = 0L;.    Unl
a6230 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d  ockArea.lRange =
a6240 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44   0L;.    res = D
a6250 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20  osSetFileLocks( 
a6260 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63  pFile->h, &Unloc
a6270 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61  kArea, &LockArea
a6280 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20  , LOCK_TIMEOUT, 
a6290 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65  0L );.    if( re
a62a0 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b  s == NO_ERROR ){
a62b0 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79  .      newLockty
a62c0 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c  pe = EXCLUSIVE_L
a62d0 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  OCK;.    }else{.
a62e0 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 20        OSTRACE2( 
a62f0 22 4f 53 2f 32 20 65 72 72 6f 72 2d 63 6f 64 65  "OS/2 error-code
a6300 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20 29 3b   = %d\n", res );
a6310 0a 20 20 20 20 20 20 67 65 74 52 65 61 64 4c 6f  .      getReadLo
a6320 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d  ck(pFile);.    }
a6330 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22  .    OSTRACE3( "
a6340 4c 4f 43 4b 20 25 64 20 61 63 71 75 69 72 65 20  LOCK %d acquire 
a6350 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20  exclusive lock. 
a6360 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c   res=%d\n", pFil
a6370 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d  e->h, res );.  }
a6380 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65  ..  /* If we are
a6390 20 68 6f 6c 64 69 6e 67 20 61 20 50 45 4e 44 49   holding a PENDI
a63a0 4e 47 20 6c 6f 63 6b 20 74 68 61 74 20 6f 75 67  NG lock that oug
a63b0 68 74 20 74 6f 20 62 65 20 72 65 6c 65 61 73 65  ht to be release
a63c0 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 6c  d, then.  ** rel
a63d0 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a  ease it now..  *
a63e0 2f 0a 20 20 69 66 28 20 67 6f 74 50 65 6e 64 69  /.  if( gotPendi
a63f0 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79  ngLock && lockty
a6400 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe==SHARED_LOCK 
a6410 29 7b 0a 20 20 20 20 69 6e 74 20 72 3b 0a 20 20  ){.    int r;.  
a6420 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73    LockArea.lOffs
a6430 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63  et = 0L;.    Loc
a6440 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
a6450 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65  L;.    UnlockAre
a6460 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44  a.lOffset = PEND
a6470 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e  ING_BYTE;.    Un
a6480 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  lockArea.lRange 
a6490 3d 20 31 4c 3b 0a 20 20 20 20 72 20 3d 20 44 6f  = 1L;.    r = Do
a64a0 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70  sSetFileLocks( p
a64b0 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b  File->h, &Unlock
a64c0 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c  Area, &LockArea,
a64d0 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30   LOCK_TIMEOUT, 0
a64e0 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45  L );.    OSTRACE
a64f0 33 28 20 22 4c 4f 43 4b 20 25 64 20 75 6e 6c 6f  3( "LOCK %d unlo
a6500 63 6b 69 6e 67 20 70 65 6e 64 69 6e 67 2f 69 73  cking pending/is
a6510 20 73 68 61 72 65 64 2e 20 72 3d 25 64 5c 6e 22   shared. r=%d\n"
a6520 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 20 29 3b  , pFile->h, r );
a6530 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74  .  }..  /* Updat
a6540 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74  e the state of t
a6550 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 65 6c 64  he lock has held
a6560 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 65 73   in the file des
a6570 63 72 69 70 74 6f 72 20 74 68 65 6e 0a 20 20 2a  criptor then.  *
a6580 2a 20 72 65 74 75 72 6e 20 74 68 65 20 61 70 70  * return the app
a6590 72 6f 70 72 69 61 74 65 20 72 65 73 75 6c 74 20  ropriate result 
a65a0 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  code..  */.  if(
a65b0 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52   res == NO_ERROR
a65c0 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
a65d0 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b  ITE_OK;.  }else{
a65e0 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28 20 22  .    OSTRACE4( "
a65f0 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25 64 20 74  LOCK FAILED %d t
a6600 72 79 69 6e 67 20 66 6f 72 20 25 64 20 62 75 74  rying for %d but
a6610 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70 46 69 6c   got %d\n", pFil
a6620 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20  e->h,.          
a6630 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20 6e 65      locktype, ne
a6640 77 4c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20  wLocktype );.   
a6650 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
a6660 59 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e  Y;.  }.  pFile->
a6670 6c 6f 63 6b 74 79 70 65 20 3d 20 6e 65 77 4c 6f  locktype = newLo
a6680 63 6b 74 79 70 65 3b 0a 20 20 4f 53 54 52 41 43  cktype;.  OSTRAC
a6690 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 6e 6f 77  E3( "LOCK %d now
a66a0 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68   %d\n", pFile->h
a66b0 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  , pFile->locktyp
a66c0 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  e );.  return rc
a66d0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
a66e0 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69  routine checks i
a66f0 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53  f there is a RES
a6700 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20  ERVED lock held 
a6710 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64  on the specified
a6720 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73  .** file by this
a6730 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72   or any other pr
a6740 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61  ocess. If such a
a6750 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 72   lock is held, r
a6760 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72  eturn.** non-zer
a6770 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 7a 65 72  o, otherwise zer
a6780 6f 2e 0a 2a 2f 0a 69 6e 74 20 6f 73 32 43 68 65  o..*/.int os2Che
a6790 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 20  ckReservedLock( 
a67a0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
a67b0 20 29 7b 0a 20 20 69 6e 74 20 72 20 3d 20 30 3b   ){.  int r = 0;
a67c0 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c  .  os2File *pFil
a67d0 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64  e = (os2File*)id
a67e0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  ;.  assert( pFil
a67f0 65 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 46  e!=0 );.  if( pF
a6800 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52  ile->locktype>=R
a6810 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a  ESERVED_LOCK ){.
a6820 20 20 20 20 72 20 3d 20 31 3b 0a 20 20 20 20 4f      r = 1;.    O
a6830 53 54 52 41 43 45 33 28 20 22 54 45 53 54 20 57  STRACE3( "TEST W
a6840 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 6c 6f  R-LOCK %d %d (lo
a6850 63 61 6c 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  cal)\n", pFile->
a6860 68 2c 20 72 20 29 3b 0a 20 20 7d 65 6c 73 65 7b  h, r );.  }else{
a6870 0a 20 20 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c  .    FILELOCK  L
a6880 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20  ockArea,.       
a6890 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65         UnlockAre
a68a0 61 3b 0a 20 20 20 20 41 50 49 52 45 54 20 72 63  a;.    APIRET rc
a68b0 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 20   = NO_ERROR;.   
a68c0 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65   memset(&LockAre
a68d0 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63  a, 0, sizeof(Loc
a68e0 6b 41 72 65 61 29 29 3b 0a 20 20 20 20 6d 65 6d  kArea));.    mem
a68f0 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c  set(&UnlockArea,
a6900 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63   0, sizeof(Unloc
a6910 6b 41 72 65 61 29 29 3b 0a 20 20 20 20 4c 6f 63  kArea));.    Loc
a6920 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20  kArea.lOffset = 
a6930 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20  RESERVED_BYTE;. 
a6940 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e     LockArea.lRan
a6950 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55 6e 6c  ge = 1L;.    Unl
a6960 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
a6970 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b  = 0L;.    Unlock
a6980 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c  Area.lRange = 0L
a6990 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 53 65  ;.    rc = DosSe
a69a0 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c  tFileLocks( pFil
a69b0 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65  e->h, &UnlockAre
a69c0 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f  a, &LockArea, LO
a69d0 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29  CK_TIMEOUT, 0L )
a69e0 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 20  ;.    OSTRACE3( 
a69f0 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64  "TEST WR-LOCK %d
a6a00 20 6c 6f 63 6b 20 72 65 73 65 72 76 65 64 20 62   lock reserved b
a6a10 79 74 65 20 72 63 3d 25 64 5c 6e 22 2c 20 70 46  yte rc=%d\n", pF
a6a20 69 6c 65 2d 3e 68 2c 20 72 63 20 29 3b 0a 20 20  ile->h, rc );.  
a6a30 20 20 69 66 28 20 72 63 20 3d 3d 20 4e 4f 5f 45    if( rc == NO_E
a6a40 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 41 50  RROR ){.      AP
a6a50 49 52 45 54 20 72 63 75 20 3d 20 4e 4f 5f 45 52  IRET rcu = NO_ER
a6a60 52 4f 52 3b 20 2f 2a 20 72 65 74 75 72 6e 20 63  ROR; /* return c
a6a70 6f 64 65 20 66 6f 72 20 75 6e 6c 6f 63 6b 69 6e  ode for unlockin
a6a80 67 20 2a 2f 0a 20 20 20 20 20 20 4c 6f 63 6b 41  g */.      LockA
a6a90 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c  rea.lOffset = 0L
a6aa0 3b 0a 20 20 20 20 20 20 4c 6f 63 6b 41 72 65 61  ;.      LockArea
a6ab0 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20  .lRange = 0L;.  
a6ac0 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c      UnlockArea.l
a6ad0 4f 66 66 73 65 74 20 3d 20 52 45 53 45 52 56 45  Offset = RESERVE
a6ae0 44 5f 42 59 54 45 3b 0a 20 20 20 20 20 20 55 6e  D_BYTE;.      Un
a6af0 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  lockArea.lRange 
a6b00 3d 20 31 4c 3b 0a 20 20 20 20 20 20 72 63 75 20  = 1L;.      rcu 
a6b10 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b  = DosSetFileLock
a6b20 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e  s( pFile->h, &Un
a6b30 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41  lockArea, &LockA
a6b40 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55  rea, LOCK_TIMEOU
a6b50 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 20 20 4f  T, 0L );.      O
a6b60 53 54 52 41 43 45 33 28 20 22 54 45 53 54 20 57  STRACE3( "TEST W
a6b70 52 2d 4c 4f 43 4b 20 25 64 20 75 6e 6c 6f 63 6b  R-LOCK %d unlock
a6b80 20 72 65 73 65 72 76 65 64 20 62 79 74 65 20 72   reserved byte r
a6b90 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  =%d\n", pFile->h
a6ba0 2c 20 72 63 75 20 29 3b 0a 20 20 20 20 7d 0a 20  , rcu );.    }. 
a6bb0 20 20 20 72 20 3d 20 21 28 72 63 20 3d 3d 20 4e     r = !(rc == N
a6bc0 4f 5f 45 52 52 4f 52 29 3b 0a 20 20 20 20 4f 53  O_ERROR);.    OS
a6bd0 54 52 41 43 45 33 28 20 22 54 45 53 54 20 57 52  TRACE3( "TEST WR
a6be0 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 72 65 6d  -LOCK %d %d (rem
a6bf0 6f 74 65 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  ote)\n", pFile->
a6c00 68 2c 20 72 20 29 3b 0a 20 20 7d 0a 20 20 72 65  h, r );.  }.  re
a6c10 74 75 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn r;.}../*.**
a6c20 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69   Lower the locki
a6c30 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65  ng level on file
a6c40 20 64 65 73 63 72 69 70 74 6f 72 20 69 64 20 74   descriptor id t
a6c50 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63  o locktype.  loc
a6c60 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65  ktype.** must be
a6c70 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20   either NO_LOCK 
a6c80 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a  or SHARED_LOCK..
a6c90 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63  **.** If the loc
a6ca0 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68  king level of th
a6cb0 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
a6cc0 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 20  r is already at 
a6cd0 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20  or below.** the 
a6ce0 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e  requested lockin
a6cf0 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f  g level, this ro
a6d00 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70  utine is a no-op
a6d10 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f  ..**.** It is no
a6d20 74 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74  t possible for t
a6d30 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 66  his routine to f
a6d40 61 69 6c 20 69 66 20 74 68 65 20 73 65 63 6f 6e  ail if the secon
a6d50 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73  d argument.** is
a6d60 20 4e 4f 5f 4c 4f 43 4b 2e 20 20 49 66 20 74 68   NO_LOCK.  If th
a6d70 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
a6d80 74 20 69 73 20 53 48 41 52 45 44 5f 4c 4f 43 4b  t is SHARED_LOCK
a6d90 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
a6da0 6e 65 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75  ne.** might retu
a6db0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b  rn SQLITE_IOERR;
a6dc0 0a 2a 2f 0a 69 6e 74 20 6f 73 32 55 6e 6c 6f 63  .*/.int os2Unloc
a6dd0 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  k( sqlite3_file 
a6de0 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70  *id, int locktyp
a6df0 65 20 29 7b 0a 20 20 69 6e 74 20 74 79 70 65 3b  e ){.  int type;
a6e00 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c  .  os2File *pFil
a6e10 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64  e = (os2File*)id
a6e20 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20  ;.  APIRET rc = 
a6e30 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 41 50 49  SQLITE_OK;.  API
a6e40 52 45 54 20 72 65 73 20 3d 20 4e 4f 5f 45 52 52  RET res = NO_ERR
a6e50 4f 52 3b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20  OR;.  FILELOCK  
a6e60 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20  LockArea,.      
a6e70 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61        UnlockArea
a6e80 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b  ;.  memset(&Lock
a6e90 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Area, 0, sizeof(
a6ea0 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65  LockArea));.  me
a6eb0 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61  mset(&UnlockArea
a6ec0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f  , 0, sizeof(Unlo
a6ed0 63 6b 41 72 65 61 29 29 3b 0a 20 20 61 73 73 65  ckArea));.  asse
a6ee0 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a  rt( pFile!=0 );.
a6ef0 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79    assert( lockty
a6f00 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe<=SHARED_LOCK 
a6f10 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22  );.  OSTRACE4( "
a6f20 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20  UNLOCK %d to %d 
a6f30 77 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65  was %d\n", pFile
a6f40 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70  ->h, locktype, p
a6f50 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29  File->locktype )
a6f60 3b 0a 20 20 74 79 70 65 20 3d 20 70 46 69 6c 65  ;.  type = pFile
a6f70 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66  ->locktype;.  if
a6f80 28 20 74 79 70 65 3e 3d 45 58 43 4c 55 53 49 56  ( type>=EXCLUSIV
a6f90 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c 6f  E_LOCK ){.    Lo
a6fa0 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d  ckArea.lOffset =
a6fb0 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65   0L;.    LockAre
a6fc0 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20  a.lRange = 0L;. 
a6fd0 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f     UnlockArea.lO
a6fe0 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46  ffset = SHARED_F
a6ff0 49 52 53 54 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b  IRST;.    Unlock
a7000 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48  Area.lRange = SH
a7010 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 72  ARED_SIZE;.    r
a7020 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c  es = DosSetFileL
a7030 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20  ocks( pFile->h, 
a7040 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f  &UnlockArea, &Lo
a7050 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d  ckArea, LOCK_TIM
a7060 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20  EOUT, 0L );.    
a7070 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43  OSTRACE3( "UNLOC
a7080 4b 20 25 64 20 65 78 63 6c 75 73 69 76 65 20 6c  K %d exclusive l
a7090 6f 63 6b 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70  ock res=%d\n", p
a70a0 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a  File->h, res );.
a70b0 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65      if( locktype
a70c0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26  ==SHARED_LOCK &&
a70d0 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69   getReadLock(pFi
a70e0 6c 65 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20  le) != NO_ERROR 
a70f0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73  ){.      /* This
a7100 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 61   should never ha
a7110 70 70 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c 64  ppen.  We should
a7120 20 61 6c 77 61 79 73 20 62 65 20 61 62 6c 65 20   always be able 
a7130 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 63  to.      ** reac
a7140 71 75 69 72 65 20 74 68 65 20 72 65 61 64 20 6c  quire the read l
a7150 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 4f 53 54  ock */.      OST
a7160 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25  RACE3( "UNLOCK %
a7170 64 20 74 6f 20 25 64 20 67 65 74 52 65 61 64 4c  d to %d getReadL
a7180 6f 63 6b 28 29 20 66 61 69 6c 65 64 5c 6e 22 2c  ock() failed\n",
a7190 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74   pFile->h, lockt
a71a0 79 70 65 20 29 3b 0a 20 20 20 20 20 20 72 63 20  ype );.      rc 
a71b0 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55  = SQLITE_IOERR_U
a71c0 4e 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d  NLOCK;.    }.  }
a71d0 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 52 45 53  .  if( type>=RES
a71e0 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20  ERVED_LOCK ){.  
a71f0 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73    LockArea.lOffs
a7200 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63  et = 0L;.    Loc
a7210 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
a7220 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65  L;.    UnlockAre
a7230 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45  a.lOffset = RESE
a7240 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 55  RVED_BYTE;.    U
a7250 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
a7260 20 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d   = 1L;.    res =
a7270 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73   DosSetFileLocks
a7280 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c  ( pFile->h, &Unl
a7290 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72  ockArea, &LockAr
a72a0 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54  ea, LOCK_TIMEOUT
a72b0 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52  , 0L );.    OSTR
a72c0 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64  ACE3( "UNLOCK %d
a72d0 20 72 65 73 65 72 76 65 64 20 72 65 73 3d 25 64   reserved res=%d
a72e0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
a72f0 65 73 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  es );.  }.  if( 
a7300 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43  locktype==NO_LOC
a7310 4b 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52 45  K && type>=SHARE
a7320 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65  D_LOCK ){.    re
a7330 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f  s = unlockReadLo
a7340 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f  ck(pFile);.    O
a7350 53 54 52 41 43 45 35 28 20 22 55 4e 4c 4f 43 4b  STRACE5( "UNLOCK
a7360 20 25 64 20 69 73 20 25 64 20 77 61 6e 74 20 25   %d is %d want %
a7370 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69  d res=%d\n", pFi
a7380 6c 65 2d 3e 68 2c 20 74 79 70 65 2c 20 6c 6f 63  le->h, type, loc
a7390 6b 74 79 70 65 2c 20 72 65 73 20 29 3b 0a 20 20  ktype, res );.  
a73a0 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 50 45  }.  if( type>=PE
a73b0 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20  NDING_LOCK ){.  
a73c0 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73    LockArea.lOffs
a73d0 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63  et = 0L;.    Loc
a73e0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
a73f0 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65  L;.    UnlockAre
a7400 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44  a.lOffset = PEND
a7410 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e  ING_BYTE;.    Un
a7420 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  lockArea.lRange 
a7430 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20  = 1L;.    res = 
a7440 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28  DosSetFileLocks(
a7450 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f   pFile->h, &Unlo
a7460 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65  ckArea, &LockAre
a7470 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c  a, LOCK_TIMEOUT,
a7480 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41   0L );.    OSTRA
a7490 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20  CE3( "UNLOCK %d 
a74a0 70 65 6e 64 69 6e 67 20 72 65 73 3d 25 64 5c 6e  pending res=%d\n
a74b0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73  ", pFile->h, res
a74c0 20 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d   );.  }.  pFile-
a74d0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b  >locktype = lock
a74e0 74 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45 33  type;.  OSTRACE3
a74f0 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 6e 6f 77  ( "UNLOCK %d now
a7500 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68   %d\n", pFile->h
a7510 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  , pFile->locktyp
a7520 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  e );.  return rc
a7530 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72  ;.}../*.** Contr
a7540 6f 6c 20 61 6e 64 20 71 75 65 72 79 20 6f 66 20  ol and query of 
a7550 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 68 61  the open file ha
a7560 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ndle..*/.static 
a7570 69 6e 74 20 6f 73 32 46 69 6c 65 43 6f 6e 74 72  int os2FileContr
a7580 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ol(sqlite3_file 
a7590 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69  *id, int op, voi
a75a0 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74  d *pArg){.  swit
a75b0 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61  ch( op ){.    ca
a75c0 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f  se SQLITE_FCNTL_
a75d0 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20  LOCKSTATE: {.   
a75e0 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d     *(int*)pArg =
a75f0 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d   ((os2File*)id)-
a7600 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20  >locktype;.     
a7610 20 4f 53 54 52 41 43 45 33 28 20 22 46 43 4e 54   OSTRACE3( "FCNT
a7620 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 25 64 20 6c  L_LOCKSTATE %d l
a7630 6f 63 6b 3d 25 64 5c 6e 22 2c 20 28 28 6f 73 32  ock=%d\n", ((os2
a7640 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28 28  File*)id)->h, ((
a7650 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f  os2File*)id)->lo
a7660 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 20 20  cktype );.      
a7670 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
a7680 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
a7690 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
a76a0 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  R;.}../*.** Retu
a76b0 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 69  rn the sector si
a76c0 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74  ze in bytes of t
a76d0 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c  he underlying bl
a76e0 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a  ock device for.*
a76f0 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  * the specified 
a7700 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c  file. This is al
a7710 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 20  most always 512 
a7720 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62  bytes, but may b
a7730 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20  e.** larger for 
a7740 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a  some devices..**
a7750 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20  .** SQLite code 
a7760 61 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e  assumes this fun
a7770 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69  ction cannot fai
a7780 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d  l. It also assum
a7790 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77  es that.** if tw
a77a0 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 61  o files are crea
a77b0 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20  ted in the same 
a77c0 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65  file-system dire
a77d0 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61  ctory (i.e..** a
a77e0 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 74   database and it
a77f0 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20  s journal file) 
a7800 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 20  that the sector 
a7810 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65  size will be the
a7820 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74  .** same for bot
a7830 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  h..*/.static int
a7840 20 6f 73 32 53 65 63 74 6f 72 53 69 7a 65 28 73   os2SectorSize(s
a7850 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
a7860 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  {.  return SQLIT
a7870 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52  E_DEFAULT_SECTOR
a7880 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  _SIZE;.}../*.** 
a7890 52 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72 20  Return a vector 
a78a0 6f 66 20 64 65 76 69 63 65 20 63 68 61 72 61 63  of device charac
a78b0 74 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 74  teristics..*/.st
a78c0 61 74 69 63 20 69 6e 74 20 6f 73 32 44 65 76 69  atic int os2Devi
a78d0 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
a78e0 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  s(sqlite3_file *
a78f0 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b  id){.  return 0;
a7900 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72  .}../*.** Helper
a7910 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f 6e   function to con
a7920 76 65 72 74 20 55 54 46 2d 38 20 66 69 6c 65 6e  vert UTF-8 filen
a7930 61 6d 65 73 20 74 6f 20 6c 6f 63 61 6c 20 4f 53  ames to local OS
a7940 2f 32 20 63 6f 64 65 70 61 67 65 2e 0a 2a 2a 20  /2 codepage..** 
a7950 54 68 65 20 74 77 6f 2d 73 74 65 70 20 70 72 6f  The two-step pro
a7960 63 65 73 73 3a 20 66 69 72 73 74 20 63 6f 6e 76  cess: first conv
a7970 65 72 74 20 74 68 65 20 69 6e 63 6f 6d 69 6e 67  ert the incoming
a7980 20 55 54 46 2d 38 20 73 74 72 69 6e 67 0a 2a 2a   UTF-8 string.**
a7990 20 69 6e 74 6f 20 55 43 53 2d 32 20 61 6e 64 20   into UCS-2 and 
a79a0 74 68 65 6e 20 66 72 6f 6d 20 55 43 53 2d 32 20  then from UCS-2 
a79b0 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 63  to the current c
a79c0 6f 64 65 70 61 67 65 2e 0a 2a 2a 20 54 68 65 20  odepage..** The 
a79d0 72 65 74 75 72 6e 65 64 20 63 68 61 72 20 70 6f  returned char po
a79e0 69 6e 74 65 72 20 68 61 73 20 74 6f 20 62 65 20  inter has to be 
a79f0 66 72 65 65 64 2e 0a 2a 2f 0a 63 68 61 72 20 2a  freed..*/.char *
a7a00 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68 54  convertUtf8PathT
a7a10 6f 43 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  oCp(const char *
a7a20 69 6e 29 0a 7b 0a 20 20 55 63 6f 6e 76 4f 62 6a  in).{.  UconvObj
a7a30 65 63 74 20 75 63 6f 6e 76 3b 0a 20 20 55 6e 69  ect uconv;.  Uni
a7a40 43 68 61 72 20 75 63 73 55 74 66 38 43 70 5b 31  Char ucsUtf8Cp[1
a7a50 32 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 74 65  2],.          te
a7a60 6d 70 50 61 74 68 5b 43 43 48 4d 41 58 50 41 54  mpPath[CCHMAXPAT
a7a70 48 5d 3b 0a 20 20 63 68 61 72 20 2a 6f 75 74 3b  H];.  char *out;
a7a80 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 0a  .  int rc = 0;..
a7a90 20 20 6f 75 74 20 3d 20 28 63 68 61 72 20 2a 29    out = (char *)
a7aa0 63 61 6c 6c 6f 63 28 43 43 48 4d 41 58 50 41 54  calloc(CCHMAXPAT
a7ab0 48 2c 20 31 29 3b 0a 0a 20 20 2f 2a 20 64 65 74  H, 1);..  /* det
a7ac0 65 72 6d 69 6e 65 20 73 74 72 69 6e 67 20 66 6f  ermine string fo
a7ad0 72 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  r the conversion
a7ae0 20 6f 66 20 55 54 46 2d 38 20 77 68 69 63 68 20   of UTF-8 which 
a7af0 69 73 20 43 50 31 32 30 38 20 2a 2f 0a 20 20 72  is CP1208 */.  r
a7b00 63 20 3d 20 55 6e 69 4d 61 70 43 70 54 6f 55 63  c = UniMapCpToUc
a7b10 73 43 70 28 31 32 30 38 2c 20 75 63 73 55 74 66  sCp(1208, ucsUtf
a7b20 38 43 70 2c 20 31 32 29 3b 0a 20 20 72 63 20 3d  8Cp, 12);.  rc =
a7b30 20 55 6e 69 43 72 65 61 74 65 55 63 6f 6e 76 4f   UniCreateUconvO
a7b40 62 6a 65 63 74 28 75 63 73 55 74 66 38 43 70 2c  bject(ucsUtf8Cp,
a7b50 20 26 75 63 6f 6e 76 29 3b 0a 20 20 72 63 20 3d   &uconv);.  rc =
a7b60 20 55 6e 69 53 74 72 54 6f 55 63 73 28 75 63 6f   UniStrToUcs(uco
a7b70 6e 76 2c 20 74 65 6d 70 50 61 74 68 2c 20 28 63  nv, tempPath, (c
a7b80 68 61 72 20 2a 29 69 6e 2c 20 43 43 48 4d 41 58  har *)in, CCHMAX
a7b90 50 41 54 48 29 3b 0a 20 20 72 63 20 3d 20 55 6e  PATH);.  rc = Un
a7ba0 69 46 72 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74  iFreeUconvObject
a7bb0 28 75 63 6f 6e 76 29 3b 0a 0a 20 20 2f 2a 20 63  (uconv);..  /* c
a7bc0 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72 20 63 75  onversion for cu
a7bd0 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20 77  rrent codepage w
a7be0 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 64  hich can be used
a7bf0 20 66 6f 72 20 70 61 74 68 73 20 2a 2f 0a 20 20   for paths */.  
a7c00 72 63 20 3d 20 55 6e 69 43 72 65 61 74 65 55 63  rc = UniCreateUc
a7c10 6f 6e 76 4f 62 6a 65 63 74 28 28 55 6e 69 43 68  onvObject((UniCh
a7c20 61 72 20 2a 29 4c 22 40 70 61 74 68 3d 79 65 73  ar *)L"@path=yes
a7c30 22 2c 20 26 75 63 6f 6e 76 29 3b 0a 20 20 72 63  ", &uconv);.  rc
a7c40 20 3d 20 55 6e 69 53 74 72 46 72 6f 6d 55 63 73   = UniStrFromUcs
a7c50 28 75 63 6f 6e 76 2c 20 6f 75 74 2c 20 74 65 6d  (uconv, out, tem
a7c60 70 50 61 74 68 2c 20 43 43 48 4d 41 58 50 41 54  pPath, CCHMAXPAT
a7c70 48 29 3b 0a 20 20 72 63 20 3d 20 55 6e 69 46 72  H);.  rc = UniFr
a7c80 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74 28 75 63  eeUconvObject(uc
a7c90 6f 6e 76 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  onv);..  return 
a7ca0 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65  out;.}../*.** He
a7cb0 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f  lper function to
a7cc0 20 63 6f 6e 76 65 72 74 20 66 69 6c 65 6e 61 6d   convert filenam
a7cd0 65 73 20 66 72 6f 6d 20 6c 6f 63 61 6c 20 63 6f  es from local co
a7ce0 64 65 70 61 67 65 20 74 6f 20 55 54 46 2d 38 2e  depage to UTF-8.
a7cf0 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 65 70  .** The two-step
a7d00 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73 74 20   process: first 
a7d10 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e 63 6f  convert the inco
a7d20 6d 69 6e 67 20 63 6f 64 65 70 61 67 65 2d 73 70  ming codepage-sp
a7d30 65 63 69 66 69 63 0a 2a 2a 20 73 74 72 69 6e 67  ecific.** string
a7d40 20 69 6e 74 6f 20 55 43 53 2d 32 20 61 6e 64 20   into UCS-2 and 
a7d50 74 68 65 6e 20 66 72 6f 6d 20 55 43 53 2d 32 20  then from UCS-2 
a7d60 74 6f 20 74 68 65 20 63 6f 64 65 70 61 67 65 20  to the codepage 
a7d70 6f 66 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65  of UTF-8..** The
a7d80 20 72 65 74 75 72 6e 65 64 20 63 68 61 72 20 70   returned char p
a7d90 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20 62 65  ointer has to be
a7da0 20 66 72 65 65 64 2e 0a 2a 2f 0a 63 68 61 72 20   freed..*/.char 
a7db0 2a 63 6f 6e 76 65 72 74 43 70 50 61 74 68 54 6f  *convertCpPathTo
a7dc0 55 74 66 38 28 63 6f 6e 73 74 20 63 68 61 72 20  Utf8(const char 
a7dd0 2a 69 6e 29 0a 7b 0a 20 20 55 63 6f 6e 76 4f 62  *in).{.  UconvOb
a7de0 6a 65 63 74 20 75 63 6f 6e 76 3b 0a 20 20 55 6e  ject uconv;.  Un
a7df0 69 43 68 61 72 20 75 63 73 55 74 66 38 43 70 5b  iChar ucsUtf8Cp[
a7e00 31 32 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 74  12],.          t
a7e10 65 6d 70 50 61 74 68 5b 43 43 48 4d 41 58 50 41  empPath[CCHMAXPA
a7e20 54 48 5d 3b 0a 20 20 63 68 61 72 20 2a 6f 75 74  TH];.  char *out
a7e30 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a  ;.  int rc = 0;.
a7e40 0a 20 20 6f 75 74 20 3d 20 28 63 68 61 72 20 2a  .  out = (char *
a7e50 29 63 61 6c 6c 6f 63 28 43 43 48 4d 41 58 50 41  )calloc(CCHMAXPA
a7e60 54 48 2c 20 31 29 3b 0a 0a 20 20 2f 2a 20 63 6f  TH, 1);..  /* co
a7e70 6e 76 65 72 73 69 6f 6e 20 66 6f 72 20 63 75 72  nversion for cur
a7e80 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20 77 68  rent codepage wh
a7e90 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 64 20  ich can be used 
a7ea0 66 6f 72 20 70 61 74 68 73 20 2a 2f 0a 20 20 72  for paths */.  r
a7eb0 63 20 3d 20 55 6e 69 43 72 65 61 74 65 55 63 6f  c = UniCreateUco
a7ec0 6e 76 4f 62 6a 65 63 74 28 28 55 6e 69 43 68 61  nvObject((UniCha
a7ed0 72 20 2a 29 4c 22 40 70 61 74 68 3d 79 65 73 22  r *)L"@path=yes"
a7ee0 2c 20 26 75 63 6f 6e 76 29 3b 0a 20 20 72 63 20  , &uconv);.  rc 
a7ef0 3d 20 55 6e 69 53 74 72 54 6f 55 63 73 28 75 63  = UniStrToUcs(uc
a7f00 6f 6e 76 2c 20 74 65 6d 70 50 61 74 68 2c 20 28  onv, tempPath, (
a7f10 63 68 61 72 20 2a 29 69 6e 2c 20 43 43 48 4d 41  char *)in, CCHMA
a7f20 58 50 41 54 48 29 3b 0a 20 20 72 63 20 3d 20 55  XPATH);.  rc = U
a7f30 6e 69 46 72 65 65 55 63 6f 6e 76 4f 62 6a 65 63  niFreeUconvObjec
a7f40 74 28 75 63 6f 6e 76 29 3b 0a 0a 20 20 2f 2a 20  t(uconv);..  /* 
a7f50 64 65 74 65 72 6d 69 6e 65 20 73 74 72 69 6e 67  determine string
a7f60 20 66 6f 72 20 74 68 65 20 63 6f 6e 76 65 72 73   for the convers
a7f70 69 6f 6e 20 6f 66 20 55 54 46 2d 38 20 77 68 69  ion of UTF-8 whi
a7f80 63 68 20 69 73 20 43 50 31 32 30 38 20 2a 2f 0a  ch is CP1208 */.
a7f90 20 20 72 63 20 3d 20 55 6e 69 4d 61 70 43 70 54    rc = UniMapCpT
a7fa0 6f 55 63 73 43 70 28 31 32 30 38 2c 20 75 63 73  oUcsCp(1208, ucs
a7fb0 55 74 66 38 43 70 2c 20 31 32 29 3b 0a 20 20 72  Utf8Cp, 12);.  r
a7fc0 63 20 3d 20 55 6e 69 43 72 65 61 74 65 55 63 6f  c = UniCreateUco
a7fd0 6e 76 4f 62 6a 65 63 74 28 75 63 73 55 74 66 38  nvObject(ucsUtf8
a7fe0 43 70 2c 20 26 75 63 6f 6e 76 29 3b 0a 20 20 72  Cp, &uconv);.  r
a7ff0 63 20 3d 20 55 6e 69 53 74 72 46 72 6f 6d 55 63  c = UniStrFromUc
a8000 73 28 75 63 6f 6e 76 2c 20 6f 75 74 2c 20 74 65  s(uconv, out, te
a8010 6d 70 50 61 74 68 2c 20 43 43 48 4d 41 58 50 41  mpPath, CCHMAXPA
a8020 54 48 29 3b 0a 20 20 72 63 20 3d 20 55 6e 69 46  TH);.  rc = UniF
a8030 72 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74 28 75  reeUconvObject(u
a8040 63 6f 6e 76 29 3b 0a 0a 20 20 72 65 74 75 72 6e  conv);..  return
a8050 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54   out;.}../*.** T
a8060 68 69 73 20 76 65 63 74 6f 72 20 64 65 66 69 6e  his vector defin
a8070 65 73 20 61 6c 6c 20 74 68 65 20 6d 65 74 68 6f  es all the metho
a8080 64 73 20 74 68 61 74 20 63 61 6e 20 6f 70 65 72  ds that can oper
a8090 61 74 65 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c  ate on an.** sql
a80a0 69 74 65 33 5f 66 69 6c 65 20 66 6f 72 20 6f 73  ite3_file for os
a80b0 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  2..*/.static con
a80c0 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  st sqlite3_io_me
a80d0 74 68 6f 64 73 20 6f 73 32 49 6f 4d 65 74 68 6f  thods os2IoMetho
a80e0 64 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 20  d = {.  1,      
a80f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a8100 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f    /* iVersion */
a8110 0a 20 20 6f 73 32 43 6c 6f 73 65 2c 0a 20 20 6f  .  os2Close,.  o
a8120 73 32 52 65 61 64 2c 0a 20 20 6f 73 32 57 72 69  s2Read,.  os2Wri
a8130 74 65 2c 0a 20 20 6f 73 32 54 72 75 6e 63 61 74  te,.  os2Truncat
a8140 65 2c 0a 20 20 6f 73 32 53 79 6e 63 2c 0a 20 20  e,.  os2Sync,.  
a8150 6f 73 32 46 69 6c 65 53 69 7a 65 2c 0a 20 20 6f  os2FileSize,.  o
a8160 73 32 4c 6f 63 6b 2c 0a 20 20 6f 73 32 55 6e 6c  s2Lock,.  os2Unl
a8170 6f 63 6b 2c 0a 20 20 6f 73 32 43 68 65 63 6b 52  ock,.  os2CheckR
a8180 65 73 65 72 76 65 64 4c 6f 63 6b 2c 0a 20 20 6f  eservedLock,.  o
a8190 73 32 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c 0a 20  s2FileControl,. 
a81a0 20 6f 73 32 53 65 63 74 6f 72 53 69 7a 65 2c 0a   os2SectorSize,.
a81b0 20 20 6f 73 32 44 65 76 69 63 65 43 68 61 72 61    os2DeviceChara
a81c0 63 74 65 72 69 73 74 69 63 73 0a 7d 3b 0a 0a 2f  cteristics.};../
a81d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a81e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a81f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48  ***********.** H
a8220 65 72 65 20 65 6e 64 73 20 74 68 65 20 49 2f 4f  ere ends the I/O
a8230 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 66 6f   methods that fo
a8240 72 6d 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  rm the sqlite3_i
a8250 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  o_methods object
a8260 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74  ..**.** The next
a8270 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 69   block of code i
a8280 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 56 46  mplements the VF
a8290 53 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 2a 2a 2a  S methods..*****
a82a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a82b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a82c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a82d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a82e0 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
a82f0 4f 70 65 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a  Open a file..*/.
a8300 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4f 70  static int os2Op
a8310 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  en(.  sqlite3_vf
a8320 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20  s *pVfs,        
a8330 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20      /* Not used 
a8340 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
a8350 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20  *zName,         
a8360 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
a8370 65 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c 69  e file */.  sqli
a8380 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20  te3_file *id,   
a8390 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
a83a0 74 65 20 74 68 65 20 53 51 4c 69 74 65 20 66 69  te the SQLite fi
a83b0 6c 65 20 68 61 6e 64 6c 65 20 68 65 72 65 20 2a  le handle here *
a83c0 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  /.  int flags,  
a83d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a83e0 20 20 2f 2a 20 4f 70 65 6e 20 6d 6f 64 65 20 66    /* Open mode f
a83f0 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  lags */.  int *p
a8400 4f 75 74 46 6c 61 67 73 20 20 20 20 20 20 20 20  OutFlags        
a8410 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75          /* Statu
a8420 73 20 72 65 74 75 72 6e 20 66 6c 61 67 73 20 2a  s return flags *
a8430 2f 0a 29 7b 0a 20 20 48 46 49 4c 45 20 68 3b 0a  /.){.  HFILE h;.
a8440 20 20 55 4c 4f 4e 47 20 75 6c 46 69 6c 65 41 74    ULONG ulFileAt
a8450 74 72 69 62 75 74 65 20 3d 20 30 3b 0a 20 20 55  tribute = 0;.  U
a8460 4c 4f 4e 47 20 75 6c 4f 70 65 6e 46 6c 61 67 73  LONG ulOpenFlags
a8470 20 3d 20 30 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c   = 0;.  ULONG ul
a8480 4f 70 65 6e 4d 6f 64 65 20 3d 20 30 3b 0a 20 20  OpenMode = 0;.  
a8490 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  os2File *pFile =
a84a0 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20   (os2File*)id;. 
a84b0 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f   APIRET rc = NO_
a84c0 45 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 75  ERROR;.  ULONG u
a84d0 6c 41 63 74 69 6f 6e 3b 0a 0a 20 20 6d 65 6d 73  lAction;..  mems
a84e0 65 74 28 20 70 46 69 6c 65 2c 20 30 2c 20 73 69  et( pFile, 0, si
a84f0 7a 65 6f 66 28 2a 70 46 69 6c 65 29 20 29 3b 0a  zeof(*pFile) );.
a8500 0a 20 20 4f 53 54 52 41 43 45 32 28 20 22 4f 50  .  OSTRACE2( "OP
a8510 45 4e 20 77 61 6e 74 20 25 64 5c 6e 22 2c 20 66  EN want %d\n", f
a8520 6c 61 67 73 20 29 3b 0a 0a 20 20 2f 2f 75 6c 4f  lags );..  //ulO
a8530 70 65 6e 4d 6f 64 65 20 3d 20 66 6c 61 67 73 20  penMode = flags 
a8540 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  & SQLITE_OPEN_RE
a8550 41 44 57 52 49 54 45 20 3f 20 4f 50 45 4e 5f 41  ADWRITE ? OPEN_A
a8560 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 20  CCESS_READWRITE 
a8570 3a 20 4f 50 45 4e 5f 41 43 43 45 53 53 5f 52 45  : OPEN_ACCESS_RE
a8580 41 44 4f 4e 4c 59 3b 0a 20 20 69 66 28 20 66 6c  ADONLY;.  if( fl
a8590 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
a85a0 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20  N_READWRITE ){. 
a85b0 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d     ulOpenMode |=
a85c0 20 4f 50 45 4e 5f 41 43 43 45 53 53 5f 52 45 41   OPEN_ACCESS_REA
a85d0 44 57 52 49 54 45 3b 0a 20 20 20 20 4f 53 54 52  DWRITE;.    OSTR
a85e0 41 43 45 31 28 20 22 4f 50 45 4e 20 72 65 61 64  ACE1( "OPEN read
a85f0 2f 77 72 69 74 65 5c 6e 22 20 29 3b 0a 20 20 7d  /write\n" );.  }
a8600 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65 6e  else{.    ulOpen
a8610 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 41 43 43  Mode |= OPEN_ACC
a8620 45 53 53 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20  ESS_READONLY;.  
a8630 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45    OSTRACE1( "OPE
a8640 4e 20 72 65 61 64 20 6f 6e 6c 79 5c 6e 22 20 29  N read only\n" )
a8650 3b 0a 20 20 7d 0a 0a 20 20 2f 2f 75 6c 4f 70 65  ;.  }..  //ulOpe
a8660 6e 46 6c 61 67 73 20 3d 20 66 6c 61 67 73 20 26  nFlags = flags &
a8670 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45   SQLITE_OPEN_CRE
a8680 41 54 45 20 3f 20 4f 50 45 4e 5f 41 43 54 49 4f  ATE ? OPEN_ACTIO
a8690 4e 5f 43 52 45 41 54 45 5f 49 46 5f 4e 45 57 20  N_CREATE_IF_NEW 
a86a0 3a 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 46 41  : OPEN_ACTION_FA
a86b0 49 4c 5f 49 46 5f 4e 45 57 3b 0a 20 20 69 66 28  IL_IF_NEW;.  if(
a86c0 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f   flags & SQLITE_
a86d0 4f 50 45 4e 5f 43 52 45 41 54 45 20 29 7b 0a 20  OPEN_CREATE ){. 
a86e0 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20 7c     ulOpenFlags |
a86f0 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50  = OPEN_ACTION_OP
a8700 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c 20 4f  EN_IF_EXISTS | O
a8710 50 45 4e 5f 41 43 54 49 4f 4e 5f 43 52 45 41 54  PEN_ACTION_CREAT
a8720 45 5f 49 46 5f 4e 45 57 3b 0a 20 20 20 20 4f 53  E_IF_NEW;.    OS
a8730 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 6f 70  TRACE1( "OPEN op
a8740 65 6e 20 6e 65 77 2f 63 72 65 61 74 65 5c 6e 22  en new/create\n"
a8750 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   );.  }else{.   
a8760 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20 7c 3d 20   ulOpenFlags |= 
a8770 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50 45 4e  OPEN_ACTION_OPEN
a8780 5f 49 46 5f 45 58 49 53 54 53 20 7c 20 4f 50 45  _IF_EXISTS | OPE
a8790 4e 5f 41 43 54 49 4f 4e 5f 46 41 49 4c 5f 49 46  N_ACTION_FAIL_IF
a87a0 5f 4e 45 57 3b 0a 20 20 20 20 4f 53 54 52 41 43  _NEW;.    OSTRAC
a87b0 45 31 28 20 22 4f 50 45 4e 20 6f 70 65 6e 20 65  E1( "OPEN open e
a87c0 78 69 73 74 69 6e 67 5c 6e 22 20 29 3b 0a 20 20  xisting\n" );.  
a87d0 7d 0a 0a 20 20 2f 2f 75 6c 4f 70 65 6e 4d 6f 64  }..  //ulOpenMod
a87e0 65 20 7c 3d 20 66 6c 61 67 73 20 26 20 53 51 4c  e |= flags & SQL
a87f0 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42  ITE_OPEN_MAIN_DB
a8800 20 3f 20 4f 50 45 4e 5f 53 48 41 52 45 5f 44 45   ? OPEN_SHARE_DE
a8810 4e 59 4e 4f 4e 45 20 3a 20 4f 50 45 4e 5f 53 48  NYNONE : OPEN_SH
a8820 41 52 45 5f 44 45 4e 59 57 52 49 54 45 3b 0a 20  ARE_DENYWRITE;. 
a8830 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c   if( flags & SQL
a8840 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42  ITE_OPEN_MAIN_DB
a8850 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f   ){.    ulOpenMo
a8860 64 65 20 7c 3d 20 4f 50 45 4e 5f 53 48 41 52 45  de |= OPEN_SHARE
a8870 5f 44 45 4e 59 4e 4f 4e 45 3b 0a 20 20 20 20 4f  _DENYNONE;.    O
a8880 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 73  STRACE1( "OPEN s
a8890 68 61 72 65 20 72 65 61 64 2f 77 72 69 74 65 5c  hare read/write\
a88a0 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  n" );.  }else{. 
a88b0 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d     ulOpenMode |=
a88c0 20 4f 50 45 4e 5f 53 48 41 52 45 5f 44 45 4e 59   OPEN_SHARE_DENY
a88d0 57 52 49 54 45 3b 0a 20 20 20 20 4f 53 54 52 41  WRITE;.    OSTRA
a88e0 43 45 31 28 20 22 4f 50 45 4e 20 73 68 61 72 65  CE1( "OPEN share
a88f0 20 72 65 61 64 20 6f 6e 6c 79 5c 6e 22 20 29 3b   read only\n" );
a8900 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67  .  }..  if( flag
a8910 73 20 26 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e  s & (SQLITE_OPEN
a8920 5f 54 45 4d 50 5f 44 42 20 7c 20 53 51 4c 49 54  _TEMP_DB | SQLIT
a8930 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52  E_OPEN_TEMP_JOUR
a8940 4e 41 4c 0a 20 20 20 20 20 20 20 20 20 20 20 20  NAL.            
a8950 20 20 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e     | SQLITE_OPEN
a8960 5f 53 55 42 4a 4f 55 52 4e 41 4c 29 20 29 7b 0a  _SUBJOURNAL) ){.
a8970 20 20 20 20 63 68 61 72 20 70 61 74 68 55 74 66      char pathUtf
a8980 38 5b 43 43 48 4d 41 58 50 41 54 48 5d 3b 0a 20  8[CCHMAXPATH];. 
a8990 20 20 20 2f 2f 75 6c 46 69 6c 65 41 74 74 72 69     //ulFileAttri
a89a0 62 75 74 65 20 3d 20 46 49 4c 45 5f 48 49 44 44  bute = FILE_HIDD
a89b0 45 4e 3b 20 20 2f 2f 66 6f 72 20 64 65 62 75 67  EN;  //for debug
a89c0 67 69 6e 67 2c 20 77 65 20 77 61 6e 74 20 74 6f  ging, we want to
a89d0 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73   make sure it is
a89e0 20 64 65 6c 65 74 65 64 0a 20 20 20 20 75 6c 46   deleted.    ulF
a89f0 69 6c 65 41 74 74 72 69 62 75 74 65 20 3d 20 46  ileAttribute = F
a8a00 49 4c 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 20 20  ILE_NORMAL;.    
a8a10 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74  sqlite3OsFullPat
a8a20 68 6e 61 6d 65 28 20 70 56 66 73 2c 20 7a 4e 61  hname( pVfs, zNa
a8a30 6d 65 2c 20 43 43 48 4d 41 58 50 41 54 48 2c 20  me, CCHMAXPATH, 
a8a40 70 61 74 68 55 74 66 38 20 29 3b 0a 20 20 20 20  pathUtf8 );.    
a8a50 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c  pFile->pathToDel
a8a60 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61   = convertUtf8Pa
a8a70 74 68 54 6f 43 70 28 20 70 61 74 68 55 74 66 38  thToCp( pathUtf8
a8a80 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31   );.    OSTRACE1
a8a90 28 20 22 4f 50 45 4e 20 68 69 64 64 65 6e 2f 64  ( "OPEN hidden/d
a8aa0 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65 20 66  elete on close f
a8ab0 69 6c 65 20 61 74 74 72 69 62 75 74 65 73 5c 6e  ile attributes\n
a8ac0 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  " );.  }else{.  
a8ad0 20 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74    ulFileAttribut
a8ae0 65 20 3d 20 46 49 4c 45 5f 41 52 43 48 49 56 45  e = FILE_ARCHIVE
a8af0 44 20 7c 20 46 49 4c 45 5f 4e 4f 52 4d 41 4c 3b  D | FILE_NORMAL;
a8b00 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 68  .    pFile->path
a8b10 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 20  ToDel = NULL;.  
a8b20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45    OSTRACE1( "OPE
a8b30 4e 20 6e 6f 72 6d 61 6c 20 66 69 6c 65 20 61 74  N normal file at
a8b40 74 72 69 62 75 74 65 5c 6e 22 20 29 3b 0a 20 20  tribute\n" );.  
a8b50 7d 0a 0a 20 20 2f 2a 20 61 6c 77 61 79 73 20 6f  }..  /* always o
a8b60 70 65 6e 20 69 6e 20 72 61 6e 64 6f 6d 20 61 63  pen in random ac
a8b70 63 65 73 73 20 6d 6f 64 65 20 66 6f 72 20 70 6f  cess mode for po
a8b80 73 73 69 62 6c 79 20 62 65 74 74 65 72 20 73 70  ssibly better sp
a8b90 65 65 64 20 2a 2f 0a 20 20 75 6c 4f 70 65 6e 4d  eed */.  ulOpenM
a8ba0 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47  ode |= OPEN_FLAG
a8bb0 53 5f 52 41 4e 44 4f 4d 3b 0a 20 20 75 6c 4f 70  S_RANDOM;.  ulOp
a8bc0 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46  enMode |= OPEN_F
a8bd0 4c 41 47 53 5f 46 41 49 4c 5f 4f 4e 5f 45 52 52  LAGS_FAIL_ON_ERR
a8be0 4f 52 3b 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64 65  OR;.  ulOpenMode
a8bf0 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 4e   |= OPEN_FLAGS_N
a8c00 4f 49 4e 48 45 52 49 54 3b 0a 0a 20 20 63 68 61  OINHERIT;..  cha
a8c10 72 20 2a 7a 4e 61 6d 65 43 70 20 3d 20 63 6f 6e  r *zNameCp = con
a8c20 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70  vertUtf8PathToCp
a8c30 28 20 7a 4e 61 6d 65 20 29 3b 0a 20 20 72 63 20  ( zName );.  rc 
a8c40 3d 20 44 6f 73 4f 70 65 6e 28 20 28 50 53 5a 29  = DosOpen( (PSZ)
a8c50 7a 4e 61 6d 65 43 70 2c 0a 20 20 20 20 20 20 20  zNameCp,.       
a8c60 20 20 20 20 20 20 20 20 20 26 68 2c 0a 20 20 20           &h,.   
a8c70 20 20 20 20 20 20 20 20 20 20 20 20 20 26 75 6c               &ul
a8c80 41 63 74 69 6f 6e 2c 0a 20 20 20 20 20 20 20 20  Action,.        
a8c90 20 20 20 20 20 20 20 20 30 4c 2c 0a 20 20 20 20          0L,.    
a8ca0 20 20 20 20 20 20 20 20 20 20 20 20 75 6c 46 69              ulFi
a8cb0 6c 65 41 74 74 72 69 62 75 74 65 2c 0a 20 20 20  leAttribute,.   
a8cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 75 6c 4f               ulO
a8cd0 70 65 6e 46 6c 61 67 73 2c 0a 20 20 20 20 20 20  penFlags,.      
a8ce0 20 20 20 20 20 20 20 20 20 20 75 6c 4f 70 65 6e            ulOpen
a8cf0 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20 20 20 20  Mode,.          
a8d00 20 20 20 20 20 20 28 50 45 41 4f 50 32 29 4e 55        (PEAOP2)NU
a8d10 4c 4c 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 4e  LL );.  free( zN
a8d20 61 6d 65 43 70 20 29 3b 0a 20 20 69 66 28 20 72  ameCp );.  if( r
a8d30 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b  c != NO_ERROR ){
a8d40 0a 20 20 20 20 4f 53 54 52 41 43 45 37 28 20 22  .    OSTRACE7( "
a8d50 4f 50 45 4e 20 49 6e 76 61 6c 69 64 20 68 61 6e  OPEN Invalid han
a8d60 64 6c 65 20 72 63 3d 25 64 3a 20 7a 4e 61 6d 65  dle rc=%d: zName
a8d70 3d 25 73 2c 20 75 6c 41 63 74 69 6f 6e 3d 25 23  =%s, ulAction=%#
a8d80 6c 78 2c 20 75 6c 41 74 74 72 3d 25 23 6c 78 2c  lx, ulAttr=%#lx,
a8d90 20 75 6c 46 6c 61 67 73 3d 25 23 6c 78 2c 20 75   ulFlags=%#lx, u
a8da0 6c 4d 6f 64 65 3d 25 23 6c 78 5c 6e 22 2c 0a 20  lMode=%#lx\n",. 
a8db0 20 20 20 20 20 20 20 20 20 20 20 20 20 72 63 2c               rc,
a8dc0 20 7a 4e 61 6d 65 2c 20 75 6c 41 63 74 69 6f 6e   zName, ulAction
a8dd0 2c 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74  , ulFileAttribut
a8de0 65 2c 20 75 6c 4f 70 65 6e 46 6c 61 67 73 2c 20  e, ulOpenFlags, 
a8df0 75 6c 4f 70 65 6e 4d 6f 64 65 20 29 3b 0a 20 20  ulOpenMode );.  
a8e00 20 20 66 72 65 65 28 20 70 46 69 6c 65 2d 3e 70    free( pFile->p
a8e10 61 74 68 54 6f 44 65 6c 20 29 3b 0a 20 20 20 20  athToDel );.    
a8e20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c  pFile->pathToDel
a8e30 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 66 28   = NULL;.    if(
a8e40 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f   flags & SQLITE_
a8e50 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29  OPEN_READWRITE )
a8e60 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32  {.      OSTRACE2
a8e70 28 20 22 4f 50 45 4e 20 25 64 20 49 6e 76 61 6c  ( "OPEN %d Inval
a8e80 69 64 20 68 61 6e 64 6c 65 5c 6e 22 2c 20 28 28  id handle\n", ((
a8e90 66 6c 61 67 73 20 7c 20 53 51 4c 49 54 45 5f 4f  flags | SQLITE_O
a8ea0 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 20 26 20  PEN_READONLY) & 
a8eb0 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41  ~SQLITE_OPEN_REA
a8ec0 44 57 52 49 54 45 29 20 29 3b 0a 20 20 20 20 20  DWRITE) );.     
a8ed0 20 72 65 74 75 72 6e 20 6f 73 32 4f 70 65 6e 28   return os2Open(
a8ee0 20 30 2c 20 7a 4e 61 6d 65 2c 20 69 64 2c 0a 20   0, zName, id,. 
a8ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a8f00 20 20 20 20 20 28 28 66 6c 61 67 73 20 7c 20 53       ((flags | S
a8f10 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f  QLITE_OPEN_READO
a8f20 4e 4c 59 29 20 26 20 7e 53 51 4c 49 54 45 5f 4f  NLY) & ~SQLITE_O
a8f30 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29 2c 0a  PEN_READWRITE),.
a8f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a8f50 20 20 20 20 20 20 70 4f 75 74 46 6c 61 67 73 20        pOutFlags 
a8f60 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
a8f70 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
a8f80 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20  E_CANTOPEN;.    
a8f90 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4f 75  }.  }..  if( pOu
a8fa0 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 2a 70  tFlags ){.    *p
a8fb0 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c 61 67 73  OutFlags = flags
a8fc0 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52   & SQLITE_OPEN_R
a8fd0 45 41 44 57 52 49 54 45 20 3f 20 53 51 4c 49 54  EADWRITE ? SQLIT
a8fe0 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
a8ff0 20 3a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52   : SQLITE_OPEN_R
a9000 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20  EADONLY;.  }..  
a9010 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d  pFile->pMethod =
a9020 20 26 6f 73 32 49 6f 4d 65 74 68 6f 64 3b 0a 20   &os2IoMethod;. 
a9030 20 70 46 69 6c 65 2d 3e 68 20 3d 20 68 3b 0a 20   pFile->h = h;. 
a9040 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29   OpenCounter(+1)
a9050 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 4f  ;.  OSTRACE3( "O
a9060 50 45 4e 20 25 64 20 70 4f 75 74 46 6c 61 67 73  PEN %d pOutFlags
a9070 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  =%d\n", pFile->h
a9080 2c 20 70 4f 75 74 46 6c 61 67 73 20 29 3b 0a 20  , pOutFlags );. 
a9090 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
a90a0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65  K;.}../*.** Dele
a90b0 74 65 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c  te the named fil
a90c0 65 2e 0a 2a 2f 0a 69 6e 74 20 6f 73 32 44 65 6c  e..*/.int os2Del
a90d0 65 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  ete(.  sqlite3_v
a90e0 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20  fs *pVfs,       
a90f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a9100 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 32   Not used on os2
a9110 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
a9120 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20   *zFilename,    
a9130 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a9140 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20  Name of file to 
a9150 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20  delete */.  int 
a9160 73 79 6e 63 44 69 72 20 20 20 20 20 20 20 20 20  syncDir         
a9170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a9180 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f     /* Not used o
a9190 6e 20 6f 73 32 20 2a 2f 0a 29 7b 0a 20 20 41 50  n os2 */.){.  AP
a91a0 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52  IRET rc = NO_ERR
a91b0 4f 52 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f  OR;.  SimulateIO
a91c0 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c  Error(return SQL
a91d0 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45  ITE_IOERR_DELETE
a91e0 29 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65  );.  char *zFile
a91f0 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 74  nameCp = convert
a9200 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a 46  Utf8PathToCp( zF
a9210 69 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 72 63 20  ilename );.  rc 
a9220 3d 20 44 6f 73 44 65 6c 65 74 65 28 20 28 50 53  = DosDelete( (PS
a9230 5a 29 7a 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b  Z)zFilenameCp );
a9240 0a 20 20 66 72 65 65 28 20 7a 46 69 6c 65 6e 61  .  free( zFilena
a9250 6d 65 43 70 20 29 3b 0a 20 20 4f 53 54 52 41 43  meCp );.  OSTRAC
a9260 45 32 28 20 22 44 45 4c 45 54 45 20 5c 22 25 73  E2( "DELETE \"%s
a9270 5c 22 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65  \"\n", zFilename
a9280 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20   );.  return rc 
a9290 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51  == NO_ERROR ? SQ
a92a0 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45  LITE_OK : SQLITE
a92b0 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  _IOERR;.}../*.**
a92c0 20 43 68 65 63 6b 20 74 68 65 20 65 78 69 73 74   Check the exist
a92d0 61 6e 63 65 20 61 6e 64 20 73 74 61 74 75 73 20  ance and status 
a92e0 6f 66 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74  of a file..*/.st
a92f0 61 74 69 63 20 69 6e 74 20 6f 73 32 41 63 63 65  atic int os2Acce
a9300 73 73 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  ss(.  sqlite3_vf
a9310 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20  s *pVfs,        
a9320 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f  /* Not used on o
a9330 73 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  s2 */.  const ch
a9340 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20  ar *zFilename,  
a9350 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c    /* Name of fil
a9360 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20  e to check */.  
a9370 69 6e 74 20 66 6c 61 67 73 20 20 20 20 20 20 20  int flags       
a9380 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 79 70            /* Typ
a9390 65 20 6f 66 20 74 65 73 74 20 74 6f 20 6d 61 6b  e of test to mak
a93a0 65 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a  e on this file *
a93b0 2f 0a 29 7b 0a 20 20 46 49 4c 45 53 54 41 54 55  /.){.  FILESTATU
a93c0 53 33 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e  S3 fsts3ConfigIn
a93d0 66 6f 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20  fo;.  APIRET rc 
a93e0 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 0a 20 20 6d  = NO_ERROR;..  m
a93f0 65 6d 73 65 74 28 20 26 66 73 74 73 33 43 6f 6e  emset( &fsts3Con
a9400 66 69 67 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65  figInfo, 0, size
a9410 6f 66 28 66 73 74 73 33 43 6f 6e 66 69 67 49 6e  of(fsts3ConfigIn
a9420 66 6f 29 20 29 3b 0a 20 20 63 68 61 72 20 2a 7a  fo) );.  char *z
a9430 46 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e  FilenameCp = con
a9440 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70  vertUtf8PathToCp
a9450 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 20  ( zFilename );. 
a9460 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50 61   rc = DosQueryPa
a9470 74 68 49 6e 66 6f 28 20 28 50 53 5a 29 7a 46 69  thInfo( (PSZ)zFi
a9480 6c 65 6e 61 6d 65 43 70 2c 20 46 49 4c 5f 53 54  lenameCp, FIL_ST
a9490 41 4e 44 41 52 44 2c 0a 20 20 20 20 20 20 20 20  ANDARD,.        
a94a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a94b0 20 26 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66   &fsts3ConfigInf
a94c0 6f 2c 20 73 69 7a 65 6f 66 28 46 49 4c 45 53 54  o, sizeof(FILEST
a94d0 41 54 55 53 33 29 20 29 3b 0a 20 20 66 72 65 65  ATUS3) );.  free
a94e0 28 20 7a 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b  ( zFilenameCp );
a94f0 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22 41 43  .  OSTRACE4( "AC
a9500 43 45 53 53 20 66 73 74 73 33 43 6f 6e 66 69 67  CESS fsts3Config
a9510 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 3d 25 64  Info.attrFile=%d
a9520 20 66 6c 61 67 73 3d 25 64 20 72 63 3d 25 64 5c   flags=%d rc=%d\
a9530 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
a9540 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e  fsts3ConfigInfo.
a9550 61 74 74 72 46 69 6c 65 2c 20 66 6c 61 67 73 2c  attrFile, flags,
a9560 20 72 63 20 29 3b 0a 20 20 73 77 69 74 63 68 28   rc );.  switch(
a9570 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61   flags ){.    ca
a9580 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  se SQLITE_ACCESS
a9590 5f 52 45 41 44 3a 0a 20 20 20 20 63 61 73 65 20  _READ:.    case 
a95a0 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58  SQLITE_ACCESS_EX
a95b0 49 53 54 53 3a 0a 20 20 20 20 20 20 72 63 20 3d  ISTS:.      rc =
a95c0 20 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52   (rc == NO_ERROR
a95d0 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45  );.      OSTRACE
a95e0 33 28 20 22 41 43 43 45 53 53 20 25 73 20 61 63  3( "ACCESS %s ac
a95f0 63 65 73 73 20 6f 66 20 72 65 61 64 20 61 6e 64  cess of read and
a9600 20 65 78 69 73 74 73 20 20 72 63 3d 25 64 5c 6e   exists  rc=%d\n
a9610 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 63  ", zFilename, rc
a9620 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   );.      break;
a9630 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
a9640 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54  _ACCESS_READWRIT
a9650 45 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 28 66  E:.      rc = (f
a9660 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e 61  sts3ConfigInfo.a
a9670 74 74 72 46 69 6c 65 20 26 20 46 49 4c 45 5f 52  ttrFile & FILE_R
a9680 45 41 44 4f 4e 4c 59 29 20 3d 3d 20 30 3b 0a 20  EADONLY) == 0;. 
a9690 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22       OSTRACE3( "
a96a0 41 43 43 45 53 53 20 25 73 20 61 63 63 65 73 73  ACCESS %s access
a96b0 20 6f 66 20 72 65 61 64 2f 77 72 69 74 65 20 20   of read/write  
a96c0 72 63 3d 25 64 5c 6e 22 2c 20 7a 46 69 6c 65 6e  rc=%d\n", zFilen
a96d0 61 6d 65 2c 20 72 63 20 29 3b 0a 20 20 20 20 20  ame, rc );.     
a96e0 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61   break;.    defa
a96f0 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 73 65 72  ult:.      asser
a9700 74 28 20 21 22 49 6e 76 61 6c 69 64 20 66 6c 61  t( !"Invalid fla
a9710 67 73 20 61 72 67 75 6d 65 6e 74 22 20 29 3b 0a  gs argument" );.
a9720 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
a9730 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  .}.../*.** Creat
a9740 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69  e a temporary fi
a9750 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e  le name in zBuf.
a9760 20 20 7a 42 75 66 20 6d 75 73 74 20 62 65 20 62    zBuf must be b
a9770 69 67 20 65 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20  ig enough to.** 
a9780 68 6f 6c 64 20 61 74 20 70 56 66 73 2d 3e 6d 78  hold at pVfs->mx
a9790 50 61 74 68 6e 61 6d 65 20 63 68 61 72 61 63 74  Pathname charact
a97a0 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ers..*/.static i
a97b0 6e 74 20 6f 73 32 47 65 74 54 65 6d 70 6e 61 6d  nt os2GetTempnam
a97c0 65 28 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  e( sqlite3_vfs *
a97d0 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20  pVfs, int nBuf, 
a97e0 63 68 61 72 20 2a 7a 42 75 66 20 29 7b 0a 20 20  char *zBuf ){.  
a97f0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
a9800 69 67 6e 65 64 20 63 68 61 72 20 7a 43 68 61 72  igned char zChar
a9810 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63 64 65  s[] =.    "abcde
a9820 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75  fghijklmnopqrstu
a9830 76 77 78 79 7a 22 0a 20 20 20 20 22 41 42 43 44  vwxyz".    "ABCD
a9840 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54  EFGHIJKLMNOPQRST
a9850 55 56 57 58 59 5a 22 0a 20 20 20 20 22 30 31 32  UVWXYZ".    "012
a9860 33 34 35 36 37 38 39 22 3b 0a 20 20 69 6e 74 20  3456789";.  int 
a9870 69 2c 20 6a 3b 0a 20 20 63 68 61 72 20 7a 54 65  i, j;.  char zTe
a9880 6d 70 50 61 74 68 42 75 66 5b 33 5d 3b 0a 20 20  mpPathBuf[3];.  
a9890 50 53 5a 20 7a 54 65 6d 70 50 61 74 68 20 3d 20  PSZ zTempPath = 
a98a0 28 50 53 5a 29 26 7a 54 65 6d 70 50 61 74 68 42  (PSZ)&zTempPathB
a98b0 75 66 3b 0a 20 20 63 68 61 72 20 2a 7a 54 65 6d  uf;.  char *zTem
a98c0 70 50 61 74 68 55 54 46 3b 0a 20 20 69 66 28 20  pPathUTF;.  if( 
a98d0 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 53 5a  DosScanEnv( (PSZ
a98e0 29 22 54 45 4d 50 22 2c 20 26 7a 54 65 6d 70 50  )"TEMP", &zTempP
a98f0 61 74 68 20 29 20 29 7b 0a 20 20 20 20 69 66 28  ath ) ){.    if(
a9900 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 53   DosScanEnv( (PS
a9910 5a 29 22 54 4d 50 22 2c 20 26 7a 54 65 6d 70 50  Z)"TMP", &zTempP
a9920 61 74 68 20 29 20 29 7b 0a 20 20 20 20 20 20 69  ath ) ){.      i
a9930 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28  f( DosScanEnv( (
a9940 50 53 5a 29 22 54 4d 50 44 49 52 22 2c 20 26 7a  PSZ)"TMPDIR", &z
a9950 54 65 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20  TempPath ) ){.  
a9960 20 20 20 20 20 20 20 20 20 55 4c 4f 4e 47 20 75           ULONG u
a9970 6c 44 72 69 76 65 4e 75 6d 20 3d 20 30 2c 20 75  lDriveNum = 0, u
a9980 6c 44 72 69 76 65 4d 61 70 20 3d 20 30 3b 0a 20  lDriveMap = 0;. 
a9990 20 20 20 20 20 20 20 20 20 20 44 6f 73 51 75 65            DosQue
a99a0 72 79 43 75 72 72 65 6e 74 44 69 73 6b 28 20 26  ryCurrentDisk( &
a99b0 75 6c 44 72 69 76 65 4e 75 6d 2c 20 26 75 6c 44  ulDriveNum, &ulD
a99c0 72 69 76 65 4d 61 70 20 29 3b 0a 20 20 20 20 20  riveMap );.     
a99d0 20 20 20 20 20 20 73 70 72 69 6e 74 66 28 20 28        sprintf( (
a99e0 63 68 61 72 2a 29 7a 54 65 6d 70 50 61 74 68 2c  char*)zTempPath,
a99f0 20 22 25 63 3a 22 2c 20 28 63 68 61 72 29 28 20   "%c:", (char)( 
a9a00 27 41 27 20 2b 20 75 6c 44 72 69 76 65 4e 75 6d  'A' + ulDriveNum
a9a10 20 2d 20 31 20 29 20 29 3b 0a 20 20 20 20 20 20   - 1 ) );.      
a9a20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a  }.    }.  }.  /*
a9a30 20 73 74 72 69 70 20 6f 66 66 20 61 20 74 72 61   strip off a tra
a9a40 69 6c 69 6e 67 20 73 6c 61 73 68 65 73 20 6f 72  iling slashes or
a9a50 20 62 61 63 6b 73 6c 61 73 68 65 73 2c 20 6f 74   backslashes, ot
a9a60 68 65 72 77 69 73 65 20 77 65 20 77 6f 75 6c 64  herwise we would
a9a70 20 67 65 74 20 2a 0a 20 20 20 2a 20 6d 75 6c 74   get *.   * mult
a9a80 69 70 6c 65 20 28 62 61 63 6b 29 73 6c 61 73 68  iple (back)slash
a9a90 65 73 20 77 68 69 63 68 20 63 61 75 73 65 73 20  es which causes 
a9aa0 44 6f 73 4f 70 65 6e 28 29 20 74 6f 20 66 61 69  DosOpen() to fai
a9ab0 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l               
a9ac0 2a 2f 0a 20 20 6a 20 3d 20 73 74 72 6c 65 6e 28  */.  j = strlen(
a9ad0 7a 54 65 6d 70 50 61 74 68 29 3b 0a 20 20 77 68  zTempPath);.  wh
a9ae0 69 6c 65 28 20 6a 20 3e 20 30 20 26 26 20 28 20  ile( j > 0 && ( 
a9af0 7a 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d  zTempPath[j-1] =
a9b00 3d 20 27 5c 5c 27 20 7c 7c 20 7a 54 65 6d 70 50  = '\\' || zTempP
a9b10 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 2f 27 20  ath[j-1] == '/' 
a9b20 29 20 29 7b 0a 20 20 20 20 6a 2d 2d 3b 0a 20 20  ) ){.    j--;.  
a9b30 7d 0a 20 20 7a 54 65 6d 70 50 61 74 68 5b 6a 5d  }.  zTempPath[j]
a9b40 20 3d 20 27 5c 30 27 3b 0a 20 20 7a 54 65 6d 70   = '\0';.  zTemp
a9b50 50 61 74 68 55 54 46 20 3d 20 63 6f 6e 76 65 72  PathUTF = conver
a9b60 74 43 70 50 61 74 68 54 6f 55 74 66 38 28 20 7a  tCpPathToUtf8( z
a9b70 54 65 6d 70 50 61 74 68 20 29 3b 0a 20 20 73 71  TempPath );.  sq
a9b80 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20  lite3_snprintf( 
a9b90 6e 42 75 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20  nBuf-30, zBuf,. 
a9ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a9bb0 20 20 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f     "%s\\"SQLITE_
a9bc0 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58  TEMP_FILE_PREFIX
a9bd0 2c 20 7a 54 65 6d 70 50 61 74 68 55 54 46 20 29  , zTempPathUTF )
a9be0 3b 0a 20 20 66 72 65 65 28 20 7a 54 65 6d 70 50  ;.  free( zTempP
a9bf0 61 74 68 55 54 46 20 29 3b 0a 20 20 6a 20 3d 20  athUTF );.  j = 
a9c00 73 74 72 6c 65 6e 28 20 7a 42 75 66 20 29 3b 0a  strlen( zBuf );.
a9c10 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d    sqlite3_random
a9c20 6e 65 73 73 28 20 32 30 2c 20 26 7a 42 75 66 5b  ness( 20, &zBuf[
a9c30 6a 5d 20 29 3b 0a 20 20 66 6f 72 28 20 69 20 3d  j] );.  for( i =
a9c40 20 30 3b 20 69 20 3c 20 32 30 3b 20 69 2b 2b 2c   0; i < 20; i++,
a9c50 20 6a 2b 2b 20 29 7b 0a 20 20 20 20 7a 42 75 66   j++ ){.    zBuf
a9c60 5b 6a 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 61  [j] = (char)zCha
a9c70 72 73 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 63  rs[ ((unsigned c
a9c80 68 61 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69  har)zBuf[j])%(si
a9c90 7a 65 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 20  zeof(zChars)-1) 
a9ca0 5d 3b 0a 20 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d  ];.  }.  zBuf[j]
a9cb0 20 3d 20 30 3b 0a 20 20 4f 53 54 52 41 43 45 32   = 0;.  OSTRACE2
a9cc0 28 20 22 54 45 4d 50 20 46 49 4c 45 4e 41 4d 45  ( "TEMP FILENAME
a9cd0 3a 20 25 73 5c 6e 22 2c 20 7a 42 75 66 20 29 3b  : %s\n", zBuf );
a9ce0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
a9cf0 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54  _OK;.}.../*.** T
a9d00 75 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70  urn a relative p
a9d10 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66  athname into a f
a9d20 75 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20 57  ull pathname.  W
a9d30 72 69 74 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a  rite the full.**
a9d40 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a   pathname into z
a9d50 46 75 6c 6c 5b 5d 2e 20 20 7a 46 75 6c 6c 5b 5d  Full[].  zFull[]
a9d60 20 77 69 6c 6c 20 62 65 20 61 74 20 6c 65 61 73   will be at leas
a9d70 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61  t pVfs->mxPathna
a9d80 6d 65 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 73  me.** bytes in s
a9d90 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ize..*/.static i
a9da0 6e 74 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61  nt os2FullPathna
a9db0 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  me(.  sqlite3_vf
a9dc0 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20  s *pVfs,        
a9dd0 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
a9de0 76 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20  vfs object */.  
a9df0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 6c  const char *zRel
a9e00 61 74 69 76 65 2c 20 20 20 20 20 20 2f 2a 20 50  ative,      /* P
a9e10 6f 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76 65  ossibly relative
a9e20 20 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a 20   input path */. 
a9e30 20 69 6e 74 20 6e 46 75 6c 6c 2c 20 20 20 20 20   int nFull,     
a9e40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a9e50 53 69 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62  Size of output b
a9e60 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a  uffer in bytes *
a9e70 2f 0a 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 20  /.  char *zFull 
a9e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a9e90 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65 72  /* Output buffer
a9ea0 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a   */.){.  char *z
a9eb0 52 65 6c 61 74 69 76 65 43 70 20 3d 20 63 6f 6e  RelativeCp = con
a9ec0 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70  vertUtf8PathToCp
a9ed0 28 20 7a 52 65 6c 61 74 69 76 65 20 29 3b 0a 20  ( zRelative );. 
a9ee0 20 63 68 61 72 20 7a 46 75 6c 6c 43 70 5b 43 43   char zFullCp[CC
a9ef0 48 4d 41 58 50 41 54 48 5d 3b 0a 20 20 63 68 61  HMAXPATH];.  cha
a9f00 72 20 2a 7a 46 75 6c 6c 55 54 46 3b 0a 20 20 41  r *zFullUTF;.  A
a9f10 50 49 52 45 54 20 72 63 20 3d 20 44 6f 73 51 75  PIRET rc = DosQu
a9f20 65 72 79 50 61 74 68 49 6e 66 6f 28 20 7a 52 65  eryPathInfo( zRe
a9f30 6c 61 74 69 76 65 43 70 2c 20 46 49 4c 5f 51 55  lativeCp, FIL_QU
a9f40 45 52 59 46 55 4c 4c 4e 41 4d 45 2c 20 7a 46 75  ERYFULLNAME, zFu
a9f50 6c 6c 43 70 2c 0a 20 20 20 20 20 20 20 20 20 20  llCp,.          
a9f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a9f70 20 20 20 20 20 20 43 43 48 4d 41 58 50 41 54 48        CCHMAXPATH
a9f80 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 52 65 6c   );.  free( zRel
a9f90 61 74 69 76 65 43 70 20 29 3b 0a 20 20 7a 46 75  ativeCp );.  zFu
a9fa0 6c 6c 55 54 46 20 3d 20 63 6f 6e 76 65 72 74 43  llUTF = convertC
a9fb0 70 50 61 74 68 54 6f 55 74 66 38 28 20 7a 46 75  pPathToUtf8( zFu
a9fc0 6c 6c 43 70 20 29 3b 0a 20 20 73 71 6c 69 74 65  llCp );.  sqlite
a9fd0 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e 46 75 6c  3_snprintf( nFul
a9fe0 6c 2c 20 7a 46 75 6c 6c 2c 20 7a 46 75 6c 6c 55  l, zFull, zFullU
a9ff0 54 46 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 46  TF );.  free( zF
aa000 75 6c 6c 55 54 46 20 29 3b 0a 20 20 72 65 74 75  ullUTF );.  retu
aa010 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f  rn rc == NO_ERRO
aa020 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20  R ? SQLITE_OK : 
aa030 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a  SQLITE_IOERR;.}.
aa040 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
aa050 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53  OMIT_LOAD_EXTENS
aa060 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66  ION./*.** Interf
aa070 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67  aces for opening
aa080 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 72   a shared librar
aa090 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79  y, finding entry
aa0a0 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69   points.** withi
aa0b0 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62  n the shared lib
aa0c0 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e  rary, and closin
aa0d0 67 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62  g the shared lib
aa0e0 72 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49  rary..*/./*.** I
aa0f0 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70  nterfaces for op
aa100 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c  ening a shared l
aa110 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20  ibrary, finding 
aa120 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20  entry points.** 
aa130 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 72 65  within the share
aa140 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63  d library, and c
aa150 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72 65  losing the share
aa160 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 73 74  d library..*/.st
aa170 61 74 69 63 20 76 6f 69 64 20 2a 6f 73 32 44 6c  atic void *os2Dl
aa180 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73  Open(sqlite3_vfs
aa190 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68   *pVfs, const ch
aa1a0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a  ar *zFilename){.
aa1b0 20 20 55 43 48 41 52 20 6c 6f 61 64 45 72 72 5b    UCHAR loadErr[
aa1c0 32 35 36 5d 3b 0a 20 20 48 4d 4f 44 55 4c 45 20  256];.  HMODULE 
aa1d0 68 6d 6f 64 3b 0a 20 20 41 50 49 52 45 54 20 72  hmod;.  APIRET r
aa1e0 63 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65  c;.  char *zFile
aa1f0 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 74  nameCp = convert
aa200 55 74 66 38 50 61 74 68 54 6f 43 70 28 7a 46 69  Utf8PathToCp(zFi
aa210 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 63 20 3d 20  lename);.  rc = 
aa220 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65 28 28 50  DosLoadModule((P
aa230 53 5a 29 6c 6f 61 64 45 72 72 2c 20 73 69 7a 65  SZ)loadErr, size
aa240 6f 66 28 6c 6f 61 64 45 72 72 29 2c 20 7a 46 69  of(loadErr), zFi
aa250 6c 65 6e 61 6d 65 43 70 2c 20 26 68 6d 6f 64 29  lenameCp, &hmod)
aa260 3b 0a 20 20 66 72 65 65 28 7a 46 69 6c 65 6e 61  ;.  free(zFilena
aa270 6d 65 43 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  meCp);.  return 
aa280 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f  rc != NO_ERROR ?
aa290 20 30 20 3a 20 28 76 6f 69 64 2a 29 68 6d 6f 64   0 : (void*)hmod
aa2a0 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 41 20 6e 6f 2d 6f  ;.}./*.** A no-o
aa2b0 70 20 73 69 6e 63 65 20 74 68 65 20 65 72 72 6f  p since the erro
aa2c0 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
aa2d0 65 64 20 6f 6e 20 74 68 65 20 44 6f 73 4c 6f 61  ed on the DosLoa
aa2e0 64 4d 6f 64 75 6c 65 20 63 61 6c 6c 2e 0a 2a 2a  dModule call..**
aa2f0 20 6f 73 32 44 6c 6f 70 65 6e 20 72 65 74 75 72   os2Dlopen retur
aa300 6e 73 20 7a 65 72 6f 20 69 66 20 44 6f 73 4c 6f  ns zero if DosLo
aa310 61 64 4d 6f 64 75 6c 65 20 69 73 20 6e 6f 74 20  adModule is not 
aa320 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2f 0a 73  successful..*/.s
aa330 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32 44 6c  tatic void os2Dl
aa340 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66  Error(sqlite3_vf
aa350 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75  s *pVfs, int nBu
aa360 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74  f, char *zBufOut
aa370 29 7b 0a 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0a 7d  ){./* no-op */.}
aa380 0a 76 6f 69 64 20 2a 6f 73 32 44 6c 53 79 6d 28  .void *os2DlSym(
aa390 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
aa3a0 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65  s, void *pHandle
aa3b0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53  , const char *zS
aa3c0 79 6d 62 6f 6c 29 7b 0a 20 20 50 46 4e 20 70 66  ymbol){.  PFN pf
aa3d0 6e 3b 0a 20 20 41 50 49 52 45 54 20 72 63 3b 0a  n;.  APIRET rc;.
aa3e0 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50    rc = DosQueryP
aa3f0 72 6f 63 41 64 64 72 28 28 48 4d 4f 44 55 4c 45  rocAddr((HMODULE
aa400 29 70 48 61 6e 64 6c 65 2c 20 30 4c 2c 20 7a 53  )pHandle, 0L, zS
aa410 79 6d 62 6f 6c 2c 20 26 70 66 6e 29 3b 0a 20 20  ymbol, &pfn);.  
aa420 69 66 28 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52  if( rc != NO_ERR
aa430 4f 52 20 29 7b 0a 20 20 20 20 2f 2a 20 69 66 20  OR ){.    /* if 
aa440 74 68 65 20 73 79 6d 62 6f 6c 20 69 74 73 65 6c  the symbol itsel
aa450 66 20 77 61 73 20 6e 6f 74 20 66 6f 75 6e 64 2c  f was not found,
aa460 20 73 65 61 72 63 68 20 61 67 61 69 6e 20 66 6f   search again fo
aa470 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20 20 20  r the same.     
aa480 2a 20 73 79 6d 62 6f 6c 20 77 69 74 68 20 61 6e  * symbol with an
aa490 20 65 78 74 72 61 20 75 6e 64 65 72 73 63 6f 72   extra underscor
aa4a0 65 2c 20 74 68 61 74 20 6d 69 67 68 74 20 62 65  e, that might be
aa4b0 20 6e 65 65 64 65 64 20 64 65 70 65 6e 64 69 6e   needed dependin
aa4c0 67 0a 20 20 20 20 20 2a 20 6f 6e 20 74 68 65 20  g.     * on the 
aa4d0 63 61 6c 6c 69 6e 67 20 63 6f 6e 76 65 6e 74 69  calling conventi
aa4e0 6f 6e 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 5f  on */.    char _
aa4f0 7a 53 79 6d 62 6f 6c 5b 32 35 36 5d 20 3d 20 22  zSymbol[256] = "
aa500 5f 22 3b 0a 20 20 20 20 73 74 72 6e 63 61 74 28  _";.    strncat(
aa510 5f 7a 53 79 6d 62 6f 6c 2c 20 7a 53 79 6d 62 6f  _zSymbol, zSymbo
aa520 6c 2c 20 32 35 35 29 3b 0a 20 20 20 20 72 63 20  l, 255);.    rc 
aa530 3d 20 44 6f 73 51 75 65 72 79 50 72 6f 63 41 64  = DosQueryProcAd
aa540 64 72 28 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e  dr((HMODULE)pHan
aa550 64 6c 65 2c 20 30 4c 2c 20 5f 7a 53 79 6d 62 6f  dle, 0L, _zSymbo
aa560 6c 2c 20 26 70 66 6e 29 3b 0a 20 20 7d 0a 20 20  l, &pfn);.  }.  
aa570 72 65 74 75 72 6e 20 72 63 20 21 3d 20 4e 4f 5f  return rc != NO_
aa580 45 52 52 4f 52 20 3f 20 30 20 3a 20 28 76 6f 69  ERROR ? 0 : (voi
aa590 64 2a 29 70 66 6e 3b 0a 7d 0a 76 6f 69 64 20 6f  d*)pfn;.}.void o
aa5a0 73 32 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65  s2DlClose(sqlite
aa5b0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69  3_vfs *pVfs, voi
aa5c0 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 44  d *pHandle){.  D
aa5d0 6f 73 46 72 65 65 4d 6f 64 75 6c 65 28 28 48 4d  osFreeModule((HM
aa5e0 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a  ODULE)pHandle);.
aa5f0 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 51  }.#else /* if SQ
aa600 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45  LITE_OMIT_LOAD_E
aa610 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 69  XTENSION is defi
aa620 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e  ned: */.  #defin
aa630 65 20 6f 73 32 44 6c 4f 70 65 6e 20 30 0a 20 20  e os2DlOpen 0.  
aa640 23 64 65 66 69 6e 65 20 6f 73 32 44 6c 45 72 72  #define os2DlErr
aa650 6f 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20 6f  or 0.  #define o
aa660 73 32 44 6c 53 79 6d 20 30 0a 20 20 23 64 65 66  s2DlSym 0.  #def
aa670 69 6e 65 20 6f 73 32 44 6c 43 6c 6f 73 65 20 30  ine os2DlClose 0
aa680 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
aa690 57 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75 66  Write up to nBuf
aa6a0 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d   bytes of random
aa6b0 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e 0a  ness into zBuf..
aa6c0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  */.static int os
aa6d0 32 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69  2Randomness(sqli
aa6e0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69  te3_vfs *pVfs, i
aa6f0 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a  nt nBuf, char *z
aa700 42 75 66 20 29 7b 0a 20 20 55 4c 4f 4e 47 20 73  Buf ){.  ULONG s
aa710 69 7a 65 6f 66 55 4c 6f 6e 67 20 3d 20 73 69 7a  izeofULong = siz
aa720 65 6f 66 28 55 4c 4f 4e 47 29 3b 0a 20 20 69 6e  eof(ULONG);.  in
aa730 74 20 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 73  t n = 0;.  if( s
aa740 69 7a 65 6f 66 28 44 41 54 45 54 49 4d 45 29 20  izeof(DATETIME) 
aa750 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20  <= nBuf - n ){. 
aa760 20 20 20 44 41 54 45 54 49 4d 45 20 78 3b 0a 20     DATETIME x;. 
aa770 20 20 20 44 6f 73 47 65 74 44 61 74 65 54 69 6d     DosGetDateTim
aa780 65 28 26 78 29 3b 0a 20 20 20 20 6d 65 6d 63 70  e(&x);.    memcp
aa790 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 78 2c 20  y(&zBuf[n], &x, 
aa7a0 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20 20  sizeof(x));.    
aa7b0 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 78 29 3b 0a  n += sizeof(x);.
aa7c0 20 20 7d 0a 0a 20 20 69 66 28 20 73 69 7a 65 6f    }..  if( sizeo
aa7d0 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d  fULong <= nBuf -
aa7e0 20 6e 20 29 7b 0a 20 20 20 20 50 50 49 42 20 70   n ){.    PPIB p
aa7f0 70 69 62 3b 0a 20 20 20 20 44 6f 73 47 65 74 49  pib;.    DosGetI
aa800 6e 66 6f 42 6c 6f 63 6b 73 28 4e 55 4c 4c 2c 20  nfoBlocks(NULL, 
aa810 26 70 70 69 62 29 3b 0a 20 20 20 20 6d 65 6d 63  &ppib);.    memc
aa820 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 70 70  py(&zBuf[n], &pp
aa830 69 62 2d 3e 70 69 62 5f 75 6c 70 69 64 2c 20 73  ib->pib_ulpid, s
aa840 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20  izeofULong);.   
aa850 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e   n += sizeofULon
aa860 67 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 69  g;.  }..  if( si
aa870 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75  zeofULong <= nBu
aa880 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 50 54 49  f - n ){.    PTI
aa890 42 20 70 74 69 62 3b 0a 20 20 20 20 44 6f 73 47  B ptib;.    DosG
aa8a0 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 26 70 74  etInfoBlocks(&pt
aa8b0 69 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 6d  ib, NULL);.    m
aa8c0 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20  emcpy(&zBuf[n], 
aa8d0 26 70 74 69 62 2d 3e 74 69 62 5f 70 74 69 62 32  &ptib->tib_ptib2
aa8e0 2d 3e 74 69 62 32 5f 75 6c 74 69 64 2c 20 73 69  ->tib2_ultid, si
aa8f0 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20  zeofULong);.    
aa900 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67  n += sizeofULong
aa910 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69 66 20 77  ;.  }..  /* if w
aa920 65 20 73 74 69 6c 6c 20 68 61 76 65 6e 27 74 20  e still haven't 
aa930 66 69 6c 6c 65 64 20 74 68 65 20 62 75 66 66 65  filled the buffe
aa940 72 20 79 65 74 20 74 68 65 20 66 6f 6c 6c 6f 77  r yet the follow
aa950 69 6e 67 20 77 69 6c 6c 20 2a 2f 0a 20 20 2f 2a  ing will */.  /*
aa960 20 67 72 61 62 20 65 76 65 72 79 74 68 69 6e 67   grab everything
aa970 20 6f 6e 63 65 20 69 6e 73 74 65 61 64 20 6f 66   once instead of
aa980 20 6d 61 6b 69 6e 67 20 73 65 76 65 72 61 6c 20   making several 
aa990 63 61 6c 6c 73 20 66 6f 72 20 61 20 73 69 6e 67  calls for a sing
aa9a0 6c 65 20 69 74 65 6d 20 2a 2f 0a 20 20 69 66 28  le item */.  if(
aa9b0 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20   sizeofULong <= 
aa9c0 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20  nBuf - n ){.    
aa9d0 55 4c 4f 4e 47 20 75 6c 53 79 73 49 6e 66 6f 5b  ULONG ulSysInfo[
aa9e0 51 53 56 5f 4d 41 58 5d 3b 0a 20 20 20 20 44 6f  QSV_MAX];.    Do
aa9f0 73 51 75 65 72 79 53 79 73 49 6e 66 6f 28 31 4c  sQuerySysInfo(1L
aaa00 2c 20 51 53 56 5f 4d 41 58 2c 20 75 6c 53 79 73  , QSV_MAX, ulSys
aaa10 49 6e 66 6f 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e  Info, sizeofULon
aaa20 67 20 2a 20 51 53 56 5f 4d 41 58 29 3b 0a 0a 20  g * QSV_MAX);.. 
aaa30 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b     memcpy(&zBuf[
aaa40 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51  n], &ulSysInfo[Q
aaa50 53 56 5f 4d 53 5f 43 4f 55 4e 54 20 2d 20 31 5d  SV_MS_COUNT - 1]
aaa60 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a  , sizeofULong);.
aaa70 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55      n += sizeofU
aaa80 4c 6f 6e 67 3b 0a 0a 20 20 20 20 69 66 28 20 73  Long;..    if( s
aaa90 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42  izeofULong <= nB
aaaa0 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20 20  uf - n ){.      
aaab0 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c  memcpy(&zBuf[n],
aaac0 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f   &ulSysInfo[QSV_
aaad0 54 49 4d 45 52 5f 49 4e 54 45 52 56 41 4c 20 2d  TIMER_INTERVAL -
aaae0 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67   1], sizeofULong
aaaf0 29 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 69  );.      n += si
aab00 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d  zeofULong;.    }
aab10 0a 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55  .    if( sizeofU
aab20 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e  Long <= nBuf - n
aab30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   ){.      memcpy
aab40 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79  (&zBuf[n], &ulSy
aab50 73 49 6e 66 6f 5b 51 53 56 5f 54 49 4d 45 5f 4c  sInfo[QSV_TIME_L
aab60 4f 57 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55  OW - 1], sizeofU
aab70 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b  Long);.      n +
aab80 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20  = sizeofULong;. 
aab90 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a     }.    if( siz
aaba0 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66  eofULong <= nBuf
aabb0 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65   - n ){.      me
aabc0 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26  mcpy(&zBuf[n], &
aabd0 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 54 49  ulSysInfo[QSV_TI
aabe0 4d 45 5f 48 49 47 48 20 2d 20 31 5d 2c 20 73 69  ME_HIGH - 1], si
aabf0 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20  zeofULong);.    
aac00 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f    n += sizeofULo
aac10 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ng;.    }.    if
aac20 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d  ( sizeofULong <=
aac30 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20   nBuf - n ){.   
aac40 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b     memcpy(&zBuf[
aac50 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51  n], &ulSysInfo[Q
aac60 53 56 5f 54 4f 54 41 56 41 49 4c 4d 45 4d 20 2d  SV_TOTAVAILMEM -
aac70 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67   1], sizeofULong
aac80 29 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 69  );.      n += si
aac90 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d  zeofULong;.    }
aaca0 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 6e  .  }..  return n
aacb0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70  ;.}../*.** Sleep
aacc0 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 20 77 68   for a little wh
aacd0 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65  ile.  Return the
aace0 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20   amount of time 
aacf0 73 6c 65 70 74 2e 0a 2a 2a 20 54 68 65 20 61 72  slept..** The ar
aad00 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 75  gument is the nu
aad10 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63  mber of microsec
aad20 6f 6e 64 73 20 77 65 20 77 61 6e 74 20 74 6f 20  onds we want to 
aad30 73 6c 65 65 70 2e 0a 2a 2a 20 54 68 65 20 72 65  sleep..** The re
aad40 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68  turn value is th
aad50 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72  e number of micr
aad60 6f 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65  oseconds of slee
aad70 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65  p actually.** re
aad80 71 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68 65  quested from the
aad90 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72   underlying oper
aada0 61 74 69 6e 67 20 73 79 73 74 65 6d 2c 20 61 20  ating system, a 
aadb0 6e 75 6d 62 65 72 20 77 68 69 63 68 0a 2a 2a 20  number which.** 
aadc0 6d 69 67 68 74 20 62 65 20 67 72 65 61 74 65 72  might be greater
aadd0 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
aade0 6f 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2c 20  o the argument, 
aadf0 62 75 74 20 6e 6f 74 20 6c 65 73 73 0a 2a 2a 20  but not less.** 
aae00 74 68 61 6e 20 74 68 65 20 61 72 67 75 6d 65 6e  than the argumen
aae10 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
aae20 20 6f 73 32 53 6c 65 65 70 28 20 73 71 6c 69 74   os2Sleep( sqlit
aae30 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e  e3_vfs *pVfs, in
aae40 74 20 6d 69 63 72 6f 73 65 63 20 29 7b 0a 20 20  t microsec ){.  
aae50 44 6f 73 53 6c 65 65 70 28 20 28 6d 69 63 72 6f  DosSleep( (micro
aae60 73 65 63 2f 31 30 30 30 29 20 29 3b 0a 20 20 72  sec/1000) );.  r
aae70 65 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 3b 0a  eturn microsec;.
aae80 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  }../*.** The fol
aae90 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c  lowing variable,
aaea0 20 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e   if set to a non
aaeb0 2d 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63  -zero value, bec
aaec0 6f 6d 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a  omes the result.
aaed0 2a 2a 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d  ** returned from
aaee0 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e   sqlite3OsCurren
aaef0 74 54 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69  tTime().  This i
aaf00 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69  s used for testi
aaf10 6e 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ng..*/.#ifdef SQ
aaf20 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
aaf30 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
aaf40 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20  _current_time = 
aaf50 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  0;.#endif../*.**
aaf60 20 46 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e   Find the curren
aaf70 74 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65  t time (in Unive
aaf80 72 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64  rsal Coordinated
aaf90 20 54 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74   Time).  Write t
aafa0 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69  he.** current ti
aafb0 6d 65 20 61 6e 64 20 64 61 74 65 20 61 73 20 61  me and date as a
aafc0 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62   Julian Day numb
aafd0 65 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61  er into *prNow a
aafe0 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20  nd.** return 0. 
aaff0 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65   Return 1 if the
ab000 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63   time and date c
ab010 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a  annot be found..
ab020 2a 2f 0a 69 6e 74 20 6f 73 32 43 75 72 72 65 6e  */.int os2Curren
ab030 74 54 69 6d 65 28 20 73 71 6c 69 74 65 33 5f 76  tTime( sqlite3_v
ab040 66 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65  fs *pVfs, double
ab050 20 2a 70 72 4e 6f 77 20 29 7b 0a 20 20 64 6f 75   *prNow ){.  dou
ab060 62 6c 65 20 6e 6f 77 3b 0a 20 20 53 48 4f 52 54  ble now;.  SHORT
ab070 20 6d 69 6e 75 74 65 3b 20 2f 2a 20 6e 65 65 64   minute; /* need
ab080 73 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20  s to be able to 
ab090 63 6f 70 65 20 77 69 74 68 20 6e 65 67 61 74 69  cope with negati
ab0a0 76 65 20 74 69 6d 65 7a 6f 6e 65 20 6f 66 66 73  ve timezone offs
ab0b0 65 74 20 2a 2f 0a 20 20 55 53 48 4f 52 54 20 73  et */.  USHORT s
ab0c0 65 63 6f 6e 64 2c 20 68 6f 75 72 2c 0a 20 20 20  econd, hour,.   
ab0d0 20 20 20 20 20 20 64 61 79 2c 20 6d 6f 6e 74 68        day, month
ab0e0 2c 20 79 65 61 72 3b 0a 20 20 44 41 54 45 54 49  , year;.  DATETI
ab0f0 4d 45 20 64 74 3b 0a 20 20 44 6f 73 47 65 74 44  ME dt;.  DosGetD
ab100 61 74 65 54 69 6d 65 28 20 26 64 74 20 29 3b 0a  ateTime( &dt );.
ab110 20 20 73 65 63 6f 6e 64 20 3d 20 28 55 53 48 4f    second = (USHO
ab120 52 54 29 64 74 2e 73 65 63 6f 6e 64 73 3b 0a 20  RT)dt.seconds;. 
ab130 20 6d 69 6e 75 74 65 20 3d 20 28 53 48 4f 52 54   minute = (SHORT
ab140 29 64 74 2e 6d 69 6e 75 74 65 73 20 2b 20 64 74  )dt.minutes + dt
ab150 2e 74 69 6d 65 7a 6f 6e 65 3b 0a 20 20 68 6f 75  .timezone;.  hou
ab160 72 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 68  r = (USHORT)dt.h
ab170 6f 75 72 73 3b 0a 20 20 64 61 79 20 3d 20 28 55  ours;.  day = (U
ab180 53 48 4f 52 54 29 64 74 2e 64 61 79 3b 0a 20 20  SHORT)dt.day;.  
ab190 6d 6f 6e 74 68 20 3d 20 28 55 53 48 4f 52 54 29  month = (USHORT)
ab1a0 64 74 2e 6d 6f 6e 74 68 3b 0a 20 20 79 65 61 72  dt.month;.  year
ab1b0 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 79 65   = (USHORT)dt.ye
ab1c0 61 72 3b 0a 0a 20 20 2f 2a 20 43 61 6c 63 75 6c  ar;..  /* Calcul
ab1d0 61 74 69 6f 6e 73 20 66 72 6f 6d 20 68 74 74 70  ations from http
ab1e0 3a 2f 2f 77 77 77 2e 61 73 74 72 6f 2e 6b 65 65  ://www.astro.kee
ab1f0 6c 65 2e 61 63 2e 75 6b 2f 7e 72 6e 6f 2f 41 73  le.ac.uk/~rno/As
ab200 74 72 6f 6e 6f 6d 79 2f 68 6a 64 2e 68 74 6d 6c  tronomy/hjd.html
ab210 0a 20 20 20 20 20 68 74 74 70 3a 2f 2f 77 77 77  .     http://www
ab220 2e 61 73 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e  .astro.keele.ac.
ab230 75 6b 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d  uk/~rno/Astronom
ab240 79 2f 68 6a 64 2d 30 2e 31 2e 63 20 2a 2f 0a 20  y/hjd-0.1.c */. 
ab250 20 2f 2a 20 43 61 6c 63 75 6c 61 74 65 20 74 68   /* Calculate th
ab260 65 20 4a 75 6c 69 61 6e 20 64 61 79 73 20 2a 2f  e Julian days */
ab270 0a 20 20 6e 6f 77 20 3d 20 64 61 79 20 2d 20 33  .  now = day - 3
ab280 32 30 37 36 20 2b 0a 20 20 20 20 31 34 36 31 2a  2076 +.    1461*
ab290 28 79 65 61 72 20 2b 20 34 38 30 30 20 2b 20 28  (year + 4800 + (
ab2a0 6d 6f 6e 74 68 20 2d 20 31 34 29 2f 31 32 29 2f  month - 14)/12)/
ab2b0 34 20 2b 0a 20 20 20 20 33 36 37 2a 28 6d 6f 6e  4 +.    367*(mon
ab2c0 74 68 20 2d 20 32 20 2d 20 28 6d 6f 6e 74 68 20  th - 2 - (month 
ab2d0 2d 20 31 34 29 2f 31 32 2a 31 32 29 2f 31 32 20  - 14)/12*12)/12 
ab2e0 2d 0a 20 20 20 20 33 2a 28 28 79 65 61 72 20 2b  -.    3*((year +
ab2f0 20 34 39 30 30 20 2b 20 28 6d 6f 6e 74 68 20 2d   4900 + (month -
ab300 20 31 34 29 2f 31 32 29 2f 31 30 30 29 2f 34 3b   14)/12)/100)/4;
ab310 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 66  ..  /* Add the f
ab320 72 61 63 74 69 6f 6e 61 6c 20 68 6f 75 72 73 2c  ractional hours,
ab330 20 6d 69 6e 73 20 61 6e 64 20 73 65 63 6f 6e 64   mins and second
ab340 73 20 2a 2f 0a 20 20 6e 6f 77 20 2b 3d 20 28 68  s */.  now += (h
ab350 6f 75 72 20 2b 20 31 32 2e 30 29 2f 32 34 2e 30  our + 12.0)/24.0
ab360 3b 0a 20 20 6e 6f 77 20 2b 3d 20 6d 69 6e 75 74  ;.  now += minut
ab370 65 2f 31 34 34 30 2e 30 3b 0a 20 20 6e 6f 77 20  e/1440.0;.  now 
ab380 2b 3d 20 73 65 63 6f 6e 64 2f 38 36 34 30 30 2e  += second/86400.
ab390 30 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 6e 6f  0;.  *prNow = no
ab3a0 77 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  w;.#ifdef SQLITE
ab3b0 5f 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69  _TEST.  if( sqli
ab3c0 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65  te3_current_time
ab3d0 20 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d   ){.    *prNow =
ab3e0 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74   sqlite3_current
ab3f0 5f 74 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20  _time/86400.0 + 
ab400 32 34 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23  2440587.5;.  }.#
ab410 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 30  endif.  return 0
ab420 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
ab430 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
ab440 68 65 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c  he sqlite3Defaul
ab450 74 56 66 73 20 73 74 72 75 63 74 75 72 65 2e 20  tVfs structure. 
ab460 20 20 57 65 20 75 73 65 0a 2a 2a 20 61 20 66 75    We use.** a fu
ab470 6e 63 74 69 6f 6e 20 72 61 74 68 65 72 20 74 68  nction rather th
ab480 61 6e 20 67 69 76 65 20 74 68 65 20 73 74 72 75  an give the stru
ab490 63 74 75 72 65 20 67 6c 6f 62 61 6c 20 73 63 6f  cture global sco
ab4a0 70 65 20 62 65 63 61 75 73 65 0a 2a 2a 20 73 6f  pe because.** so
ab4b0 6d 65 20 63 6f 6d 70 69 6c 65 72 73 20 28 4d 53  me compilers (MS
ab4c0 56 43 29 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77  VC) do not allow
ab4d0 20 66 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61   forward declara
ab4e0 74 69 6f 6e 73 20 6f 66 0a 2a 2a 20 69 6e 69 74  tions of.** init
ab4f0 69 61 6c 69 7a 65 64 20 73 74 72 75 63 74 75 72  ialized structur
ab500 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  es..*/.SQLITE_PR
ab510 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76 66  IVATE sqlite3_vf
ab520 73 20 2a 73 71 6c 69 74 65 33 4f 73 44 65 66 61  s *sqlite3OsDefa
ab530 75 6c 74 56 66 73 28 76 6f 69 64 29 7b 0a 20 20  ultVfs(void){.  
ab540 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76  static sqlite3_v
ab550 66 73 20 6f 73 32 56 66 73 20 3d 20 7b 0a 20 20  fs os2Vfs = {.  
ab560 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20    1,            
ab570 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e       /* iVersion
ab580 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 6f   */.    sizeof(o
ab590 73 32 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73 7a  s2File),   /* sz
ab5a0 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 43 43  OsFile */.    CC
ab5b0 48 4d 41 58 50 41 54 48 2c 20 20 20 20 20 20 20  HMAXPATH,       
ab5c0 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a   /* mxPathname *
ab5d0 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  /.    0,        
ab5e0 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78           /* pNex
ab5f0 74 20 2a 2f 0a 20 20 20 20 22 6f 73 32 22 2c 20  t */.    "os2", 
ab600 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a              /* z
ab610 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20  Name */.    0,  
ab620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
ab630 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 0a 20  * pAppData */.. 
ab640 20 20 20 6f 73 32 4f 70 65 6e 2c 20 20 20 20 20     os2Open,     
ab650 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2a        /* xOpen *
ab660 2f 0a 20 20 20 20 6f 73 32 44 65 6c 65 74 65 2c  /.    os2Delete,
ab670 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 6c           /* xDel
ab680 65 74 65 20 2a 2f 0a 20 20 20 20 6f 73 32 41 63  ete */.    os2Ac
ab690 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 2f 2a  cess,         /*
ab6a0 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20 20   xAccess */.    
ab6b0 6f 73 32 47 65 74 54 65 6d 70 6e 61 6d 65 2c 20  os2GetTempname, 
ab6c0 20 20 20 2f 2a 20 78 47 65 74 54 65 6d 70 6e 61     /* xGetTempna
ab6d0 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 46 75 6c  me */.    os2Ful
ab6e0 6c 50 61 74 68 6e 61 6d 65 2c 20 20 20 2f 2a 20  lPathname,   /* 
ab6f0 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 2a 2f  xFullPathname */
ab700 0a 20 20 20 20 6f 73 32 44 6c 4f 70 65 6e 2c 20  .    os2DlOpen, 
ab710 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 4f 70          /* xDlOp
ab720 65 6e 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 45  en */.    os2DlE
ab730 72 72 6f 72 2c 20 20 20 20 20 20 20 20 2f 2a 20  rror,        /* 
ab740 78 44 6c 45 72 72 6f 72 20 2a 2f 0a 20 20 20 20  xDlError */.    
ab750 6f 73 32 44 6c 53 79 6d 2c 20 20 20 20 20 20 20  os2DlSym,       
ab760 20 20 20 2f 2a 20 78 44 6c 53 79 6d 20 2a 2f 0a     /* xDlSym */.
ab770 20 20 20 20 6f 73 32 44 6c 43 6c 6f 73 65 2c 20      os2DlClose, 
ab780 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 43 6c 6f         /* xDlClo
ab790 73 65 20 2a 2f 0a 20 20 20 20 6f 73 32 52 61 6e  se */.    os2Ran
ab7a0 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20 2f 2a 20  domness,     /* 
ab7b0 78 52 61 6e 64 6f 6d 6e 65 73 73 20 2a 2f 0a 20  xRandomness */. 
ab7c0 20 20 20 6f 73 32 53 6c 65 65 70 2c 20 20 20 20     os2Sleep,    
ab7d0 20 20 20 20 20 20 2f 2a 20 78 53 6c 65 65 70 20        /* xSleep 
ab7e0 2a 2f 0a 20 20 20 20 6f 73 32 43 75 72 72 65 6e  */.    os2Curren
ab7f0 74 54 69 6d 65 20 20 20 20 20 2f 2a 20 78 43 75  tTime     /* xCu
ab800 72 72 65 6e 74 54 69 6d 65 20 2a 2f 0a 20 20 7d  rrentTime */.  }
ab810 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 6f 73 32  ;..  return &os2
ab820 56 66 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  Vfs;.}..#endif /
ab830 2a 20 4f 53 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a  * OS_OS2 */../**
ab840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
ab850 20 6f 66 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a   of os_os2.c ***
ab860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ab870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ab880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
ab890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
ab8a0 69 6e 20 66 69 6c 65 20 6f 73 5f 75 6e 69 78 2e  in file os_unix.
ab8b0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
ab8c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ab8d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
ab8e0 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a  ** 2004 May 22.*
ab8f0 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
ab900 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
ab910 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
ab920 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
ab930 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
ab940 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
ab950 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
ab960 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
ab970 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
ab980 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
ab990 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
ab9a0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
ab9b0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
ab9c0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
ab9d0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
ab9e0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
ab9f0 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
aba00 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
aba10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aba20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aba30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aba40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aba50 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
aba60 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
aba70 20 74 68 61 74 20 69 73 20 73 70 65 63 69 66 69   that is specifi
aba80 63 20 74 6f 20 55 6e 69 78 20 73 79 73 74 65 6d  c to Unix system
aba90 73 2e 0a 2a 2f 0a 23 69 66 20 4f 53 5f 55 4e 49  s..*/.#if OS_UNI
abaa0 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  X              /
abab0 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20 75  * This file is u
abac0 73 65 64 20 6f 6e 20 75 6e 69 78 20 6f 6e 6c 79  sed on unix only
abad0 20 2a 2f 0a 0a 2f 2a 20 23 64 65 66 69 6e 65 20   */../* #define 
abae0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
abaf0 43 4b 49 4e 47 5f 53 54 59 4c 45 20 30 20 2a 2f  CKING_STYLE 0 */
abb00 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64  ../*.** These #d
abb10 65 66 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e  efines should en
abb20 61 62 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73  able >2GB file s
abb30 75 70 70 6f 72 74 20 6f 6e 20 50 6f 73 69 78 20  upport on Posix 
abb40 69 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c  if the.** underl
abb50 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73  ying operating s
abb60 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69  ystem supports i
abb70 74 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61  t.  If the OS la
abb80 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c  cks.** large fil
abb90 65 20 73 75 70 70 6f 72 74 2c 20 74 68 65 73 65  e support, these
abba0 20 73 68 6f 75 6c 64 20 62 65 20 6e 6f 2d 6f 70   should be no-op
abbb0 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 67 65 20 66  s..**.** Large f
abbc0 69 6c 65 20 73 75 70 70 6f 72 74 20 63 61 6e 20  ile support can 
abbd0 62 65 20 64 69 73 61 62 6c 65 64 20 75 73 69 6e  be disabled usin
abbe0 67 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 44  g the -DSQLITE_D
abbf0 49 53 41 42 4c 45 5f 4c 46 53 20 73 77 69 74 63  ISABLE_LFS switc
abc00 68 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d 70  h.** on the comp
abc10 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e  iler command lin
abc20 65 2e 20 20 54 68 69 73 20 69 73 20 6e 65 63 65  e.  This is nece
abc30 73 73 61 72 79 20 69 66 20 79 6f 75 20 61 72 65  ssary if you are
abc40 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 6f 6e   compiling.** on
abc50 20 61 20 72 65 63 65 6e 74 20 6d 61 63 68 69 6e   a recent machin
abc60 65 20 28 65 78 3a 20 52 65 64 48 61 74 20 37 2e  e (ex: RedHat 7.
abc70 32 29 20 62 75 74 20 79 6f 75 20 77 61 6e 74 20  2) but you want 
abc80 79 6f 75 72 20 63 6f 64 65 20 74 6f 20 77 6f 72  your code to wor
abc90 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f 6c 64 65 72  k.** on an older
abca0 20 6d 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65   machine (ex: Re
abcb0 64 48 61 74 20 36 2e 30 29 2e 20 20 49 66 20 79  dHat 6.0).  If y
abcc0 6f 75 20 63 6f 6d 70 69 6c 65 20 6f 6e 20 52 65  ou compile on Re
abcd0 64 48 61 74 20 37 2e 32 0a 2a 2a 20 77 69 74 68  dHat 7.2.** with
abce0 6f 75 74 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c  out this option,
abcf0 20 4c 46 53 20 69 73 20 65 6e 61 62 6c 65 2e 20   LFS is enable. 
abd00 20 42 75 74 20 4c 46 53 20 64 6f 65 73 20 6e 6f   But LFS does no
abd10 74 20 65 78 69 73 74 20 69 6e 20 74 68 65 20 6b  t exist in the k
abd20 65 72 6e 65 6c 0a 2a 2a 20 69 6e 20 52 65 64 48  ernel.** in RedH
abd30 61 74 20 36 2e 30 2c 20 73 6f 20 74 68 65 20 63  at 6.0, so the c
abd40 6f 64 65 20 77 6f 6e 27 74 20 77 6f 72 6b 2e 20  ode won't work. 
abd50 20 48 65 6e 63 65 2c 20 66 6f 72 20 6d 61 78 69   Hence, for maxi
abd60 6d 75 6d 20 62 69 6e 61 72 79 0a 2a 2a 20 70 6f  mum binary.** po
abd70 72 74 61 62 69 6c 69 74 79 20 79 6f 75 20 73 68  rtability you sh
abd80 6f 75 6c 64 20 6f 6d 69 74 20 4c 46 53 2e 0a 2a  ould omit LFS..*
abd90 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
abda0 5f 44 49 53 41 42 4c 45 5f 4c 46 53 0a 23 20 64  _DISABLE_LFS.# d
abdb0 65 66 69 6e 65 20 5f 4c 41 52 47 45 5f 46 49 4c  efine _LARGE_FIL
abdc0 45 20 20 20 20 20 20 20 31 0a 23 20 69 66 6e 64  E       1.# ifnd
abdd0 65 66 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f  ef _FILE_OFFSET_
abde0 42 49 54 53 0a 23 20 20 20 64 65 66 69 6e 65 20  BITS.#   define 
abdf0 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54  _FILE_OFFSET_BIT
abe00 53 20 36 34 0a 23 20 65 6e 64 69 66 0a 23 20 64  S 64.# endif.# d
abe10 65 66 69 6e 65 20 5f 4c 41 52 47 45 46 49 4c 45  efine _LARGEFILE
abe20 5f 53 4f 55 52 43 45 20 31 0a 23 65 6e 64 69 66  _SOURCE 1.#endif
abe30 0a 0a 2f 2a 0a 2a 2a 20 73 74 61 6e 64 61 72 64  ../*.** standard
abe40 20 69 6e 63 6c 75 64 65 20 66 69 6c 65 73 2e 0a   include files..
abe50 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73  */.#include <sys
abe60 2f 74 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c 75  /types.h>.#inclu
abe70 64 65 20 3c 73 79 73 2f 73 74 61 74 2e 68 3e 0a  de <sys/stat.h>.
abe80 23 69 6e 63 6c 75 64 65 20 3c 66 63 6e 74 6c 2e  #include <fcntl.
abe90 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69  h>.#include <uni
abea0 73 74 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20  std.h>.#include 
abeb0 3c 73 79 73 2f 74 69 6d 65 2e 68 3e 0a 23 69 6e  <sys/time.h>.#in
abec0 63 6c 75 64 65 20 3c 65 72 72 6e 6f 2e 68 3e 0a  clude <errno.h>.
abed0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
abee0 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
abef0 4c 45 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73  LE.#include <sys
abf00 2f 69 6f 63 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75  /ioctl.h>.#inclu
abf10 64 65 20 3c 73 79 73 2f 70 61 72 61 6d 2e 68 3e  de <sys/param.h>
abf20 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 6d  .#include <sys/m
abf30 6f 75 6e 74 2e 68 3e 0a 23 65 6e 64 69 66 20 2f  ount.h>.#endif /
abf40 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  * SQLITE_ENABLE_
abf50 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f  LOCKING_STYLE */
abf60 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72  ../*.** If we ar
abf70 65 20 74 6f 20 62 65 20 74 68 72 65 61 64 2d 73  e to be thread-s
abf80 61 66 65 2c 20 69 6e 63 6c 75 64 65 20 74 68 65  afe, include the
abf90 20 70 74 68 72 65 61 64 73 20 68 65 61 64 65 72   pthreads header
abfa0 20 61 6e 64 20 64 65 66 69 6e 65 0a 2a 2a 20 74   and define.** t
abfb0 68 65 20 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54  he SQLITE_UNIX_T
abfc0 48 52 45 41 44 53 20 6d 61 63 72 6f 2e 0a 2a 2f  HREADS macro..*/
abfd0 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
abfe0 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20  ADSAFE.# define 
abff0 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45  SQLITE_UNIX_THRE
ac000 41 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ADS 1.#endif../*
ac010 0a 2a 2a 20 44 65 66 61 75 6c 74 20 70 65 72 6d  .** Default perm
ac020 69 73 73 69 6f 6e 73 20 77 68 65 6e 20 63 72 65  issions when cre
ac030 61 74 69 6e 67 20 61 20 6e 65 77 20 66 69 6c 65  ating a new file
ac040 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
ac050 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f  TE_DEFAULT_FILE_
ac060 50 45 52 4d 49 53 53 49 4f 4e 53 0a 23 20 64 65  PERMISSIONS.# de
ac070 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41  fine SQLITE_DEFA
ac080 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53  ULT_FILE_PERMISS
ac090 49 4f 4e 53 20 30 36 34 34 0a 23 65 6e 64 69 66  IONS 0644.#endif
ac0a0 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20  ../*.** Maximum 
ac0b0 73 75 70 70 6f 72 74 65 64 20 70 61 74 68 2d 6c  supported path-l
ac0c0 65 6e 67 74 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ength..*/.#defin
ac0d0 65 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20 35  e MAX_PATHNAME 5
ac0e0 31 32 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 75  12.../*.** The u
ac0f0 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 72  nixFile structur
ac100 65 20 69 73 20 73 75 62 63 6c 61 73 73 20 6f 66  e is subclass of
ac110 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 70   sqlite3_file sp
ac120 65 63 69 66 69 63 20 66 6f 72 20 74 68 65 20 75  ecific for the u
ac130 6e 69 78 0a 2a 2a 20 70 72 6f 74 61 62 69 6c 69  nix.** protabili
ac140 74 79 20 6c 61 79 65 72 2e 0a 2a 2f 0a 74 79 70  ty layer..*/.typ
ac150 65 64 65 66 20 73 74 72 75 63 74 20 75 6e 69 78  edef struct unix
ac160 46 69 6c 65 20 75 6e 69 78 46 69 6c 65 3b 0a 73  File unixFile;.s
ac170 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 20 7b  truct unixFile {
ac180 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  .  sqlite3_io_me
ac190 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70 4d 65  thods const *pMe
ac1a0 74 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 61 79 73  thod;  /* Always
ac1b0 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79   the first entry
ac1c0 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
ac1d0 45 5f 54 45 53 54 0a 20 20 2f 2a 20 49 6e 20 74  E_TEST.  /* In t
ac1e0 65 73 74 20 6d 6f 64 65 2c 20 69 6e 63 72 65 61  est mode, increa
ac1f0 73 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  se the size of t
ac200 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 20  his structure a 
ac210 62 69 74 20 73 6f 20 74 68 61 74 20 0a 20 20 2a  bit so that .  *
ac220 2a 20 69 74 20 69 73 20 6c 61 72 67 65 72 20 74  * it is larger t
ac230 68 61 6e 20 74 68 65 20 73 74 72 75 63 74 20 43  han the struct C
ac240 72 61 73 68 46 69 6c 65 20 64 65 66 69 6e 65 64  rashFile defined
ac250 20 69 6e 20 74 65 73 74 36 2e 63 2e 0a 20 20 2a   in test6.c..  *
ac260 2f 0a 20 20 63 68 61 72 20 61 50 61 64 64 69 6e  /.  char aPaddin
ac270 67 5b 33 32 5d 3b 0a 23 65 6e 64 69 66 0a 20 20  g[32];.#endif.  
ac280 73 74 72 75 63 74 20 6f 70 65 6e 43 6e 74 20 2a  struct openCnt *
ac290 70 4f 70 65 6e 3b 20 20 20 20 2f 2a 20 49 6e 66  pOpen;    /* Inf
ac2a0 6f 20 61 62 6f 75 74 20 61 6c 6c 20 6f 70 65 6e  o about all open
ac2b0 20 66 64 27 73 20 6f 6e 20 74 68 69 73 20 69 6e   fd's on this in
ac2c0 6f 64 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ode */.  struct 
ac2d0 6c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 3b  lockInfo *pLock;
ac2e0 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f 75 74     /* Info about
ac2f0 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 69 73 20 69   locks on this i
ac300 6e 6f 64 65 20 2a 2f 0a 23 69 66 64 65 66 20 53  node */.#ifdef S
ac310 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
ac320 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 76 6f 69  KING_STYLE.  voi
ac330 64 20 2a 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  d *lockingContex
ac340 74 3b 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 69 6e  t;     /* Lockin
ac350 67 20 73 74 79 6c 65 20 73 70 65 63 69 66 69 63  g style specific
ac360 20 73 74 61 74 65 20 2a 2f 0a 23 65 6e 64 69 66   state */.#endif
ac370 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   /* SQLITE_ENABL
ac380 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
ac390 2a 2f 0a 20 20 69 6e 74 20 68 3b 20 20 20 20 20  */.  int h;     
ac3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
ac3b0 2a 20 54 68 65 20 66 69 6c 65 20 64 65 73 63 72  * The file descr
ac3c0 69 70 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67  iptor */.  unsig
ac3d0 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70  ned char locktyp
ac3e0 65 3b 20 20 20 2f 2a 20 54 68 65 20 74 79 70 65  e;   /* The type
ac3f0 20 6f 66 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e   of lock held on
ac400 20 74 68 69 73 20 66 64 20 2a 2f 0a 20 20 69 6e   this fd */.  in
ac410 74 20 64 69 72 66 64 3b 20 20 20 20 20 20 20 20  t dirfd;        
ac420 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20          /* File 
ac430 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 74  descriptor for t
ac440 68 65 20 64 69 72 65 63 74 6f 72 79 20 2a 2f 0a  he directory */.
ac450 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
ac460 44 53 41 46 45 0a 20 20 70 74 68 72 65 61 64 5f  DSAFE.  pthread_
ac470 74 20 74 69 64 3b 20 20 20 20 20 20 20 20 20 20  t tid;          
ac480 20 20 2f 2a 20 54 68 65 20 74 68 72 65 61 64 20    /* The thread 
ac490 74 68 61 74 20 22 6f 77 6e 73 22 20 74 68 69 73  that "owns" this
ac4a0 20 75 6e 69 78 46 69 6c 65 20 2a 2f 0a 23 65 6e   unixFile */.#en
ac4b0 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6e  dif.};../*.** In
ac4c0 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 20  clude code that 
ac4d0 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c  is common to all
ac4e0 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f   os_*.c files.*/
ac4f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
ac500 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d   Include os_comm
ac510 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  on.h in the midd
ac520 6c 65 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 20  le of os_unix.c 
ac530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
ac540 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
ac550 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63   Begin file os_c
ac560 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  ommon.h ********
ac570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ac580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
ac590 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20  ./*.** 2004 May 
ac5a0 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  22.**.** The aut
ac5b0 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
ac5c0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
ac5d0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
ac5e0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
ac5f0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
ac600 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
ac610 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
ac620 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
ac630 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
ac640 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
ac650 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
ac660 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
ac670 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
ac680 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
ac690 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
ac6a0 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
ac6b0 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
ac6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ac6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ac6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ac6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ac700 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
ac710 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
ac720 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 74  macros and a lit
ac730 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 20  tle bit of code 
ac740 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74  that is common t
ac750 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20  o.** all of the 
ac760 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 69  platform-specifi
ac770 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29  c files (os_*.c)
ac780 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 65   and is #include
ac790 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20  d into those.** 
ac7a0 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  files..**.** Thi
ac7b0 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65  s file should be
ac7c0 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 68   #included by th
ac7d0 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f  e os_*.c files o
ac7e0 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 20  nly.  It is not 
ac7f0 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 72  a.** general pur
ac800 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c 65  pose header file
ac810 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c  ..*/../*.** At l
ac820 65 61 73 74 20 74 77 6f 20 62 75 67 73 20 68 61  east two bugs ha
ac830 76 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62 65  ve slipped in be
ac840 63 61 75 73 65 20 77 65 20 63 68 61 6e 67 65 64  cause we changed
ac850 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55   the MEMORY_DEBU
ac860 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51  G.** macro to SQ
ac870 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20 73  LITE_DEBUG and s
ac880 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69  ome older makefi
ac890 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65 74  les have not yet
ac8a0 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77 69   made the.** swi
ac8b0 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  tch.  The follow
ac8c0 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64 20  ing code should 
ac8d0 63 61 74 63 68 20 74 68 69 73 20 70 72 6f 62 6c  catch this probl
ac8e0 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69  em at compile-ti
ac8f0 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45  me..*/.#ifdef ME
ac900 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72 72  MORY_DEBUG.# err
ac910 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44  or "The MEMORY_D
ac920 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f 62  EBUG macro is ob
ac930 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51 4c  solete.  Use SQL
ac940 49 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65 61  ITE_DEBUG instea
ac950 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  d.".#endif.../*.
ac960 20 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c   * When testing,
ac970 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72   this global var
ac980 69 61 62 6c 65 20 73 74 6f 72 65 73 20 74 68 65  iable stores the
ac990 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65   location of the
ac9a0 0a 20 2a 20 70 65 6e 64 69 6e 67 2d 62 79 74 65  . * pending-byte
ac9b0 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
ac9c0 20 66 69 6c 65 2e 0a 20 2a 2f 0a 23 69 66 64 65   file.. */.#ifde
ac9d0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
ac9e0 4c 49 54 45 5f 41 50 49 20 75 6e 73 69 67 6e 65  LITE_API unsigne
ac9f0 64 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 65  d int sqlite3_pe
aca00 6e 64 69 6e 67 5f 62 79 74 65 20 3d 20 30 78 34  nding_byte = 0x4
aca10 30 30 30 30 30 30 30 3b 0a 23 65 6e 64 69 66 0a  0000000;.#endif.
aca20 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
aca30 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56  EBUG.SQLITE_PRIV
aca40 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
aca50 53 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 65 66  STrace = 0;.#def
aca60 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 20  ine OSTRACE1(X) 
aca70 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
aca80 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c  te3OSTrace ) sql
aca90 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
acaa0 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  X).#define OSTRA
acab0 43 45 32 28 58 2c 59 29 20 20 20 20 20 20 20 69  CE2(X,Y)       i
acac0 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63  f( sqlite3OSTrac
acad0 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  e ) sqlite3Debug
acae0 50 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 66  Printf(X,Y).#def
acaf0 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59  ine OSTRACE3(X,Y
acb00 2c 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c 69  ,Z)     if( sqli
acb10 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c  te3OSTrace ) sql
acb20 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
acb30 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f  X,Y,Z).#define O
acb40 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29  STRACE4(X,Y,Z,A)
acb50 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53     if( sqlite3OS
acb60 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44  Trace ) sqlite3D
acb70 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a  ebugPrintf(X,Y,Z
acb80 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,A).#define OSTR
acb90 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 20  ACE5(X,Y,Z,A,B) 
acba0 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61  if( sqlite3OSTra
acbb0 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75  ce ) sqlite3Debu
acbc0 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c  gPrintf(X,Y,Z,A,
acbd0 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  B).#define OSTRA
acbe0 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29  CE6(X,Y,Z,A,B,C)
acbf0 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65   \.    if(sqlite
acc00 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65  3OSTrace) sqlite
acc10 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59  3DebugPrintf(X,Y
acc20 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e  ,Z,A,B,C).#defin
acc30 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a  e OSTRACE7(X,Y,Z
acc40 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20 20  ,A,B,C,D) \.    
acc50 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63  if(sqlite3OSTrac
acc60 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50  e) sqlite3DebugP
acc70 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c  rintf(X,Y,Z,A,B,
acc80 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66 69  C,D).#else.#defi
acc90 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a 23  ne OSTRACE1(X).#
acca0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28  define OSTRACE2(
accb0 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  X,Y).#define OST
accc0 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64 65  RACE3(X,Y,Z).#de
accd0 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c  fine OSTRACE4(X,
acce0 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f  Y,Z,A).#define O
accf0 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c  STRACE5(X,Y,Z,A,
acd00 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  B).#define OSTRA
acd10 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29  CE6(X,Y,Z,A,B,C)
acd20 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
acd30 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29  7(X,Y,Z,A,B,C,D)
acd40 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d  .#endif../*.** M
acd50 61 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f 72  acros for perfor
acd60 6d 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20 20  mance tracing.  
acd70 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20  Normally turned 
acd80 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b 73  off.  Only works
acd90 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72 64  .** on i486 hard
acda0 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  ware..*/.#ifdef 
acdb0 53 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41 4e  SQLITE_PERFORMAN
acdc0 43 45 5f 54 52 41 43 45 0a 5f 5f 69 6e 6c 69 6e  CE_TRACE.__inlin
acdd0 65 5f 5f 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  e__ unsigned lon
acde0 67 20 6c 6f 6e 67 20 69 6e 74 20 68 77 74 69 6d  g long int hwtim
acdf0 65 28 76 6f 69 64 29 7b 0a 20 20 75 6e 73 69 67  e(void){.  unsig
ace00 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e  ned long long in
ace10 74 20 78 3b 0a 20 20 5f 5f 61 73 6d 5f 5f 28 22  t x;.  __asm__("
ace20 72 64 74 73 63 5c 6e 5c 74 22 0a 20 20 20 20 20  rdtsc\n\t".     
ace30 20 20 20 20 20 22 6d 6f 76 20 25 25 65 64 78 2c       "mov %%edx,
ace40 20 25 25 65 63 78 5c 6e 5c 74 22 0a 20 20 20 20   %%ecx\n\t".    
ace50 20 20 20 20 20 20 3a 22 3d 41 22 20 28 78 29 29        :"=A" (x))
ace60 3b 0a 20 20 72 65 74 75 72 6e 20 78 3b 0a 7d 0a  ;.  return x;.}.
ace70 73 74 61 74 69 63 20 75 6e 73 69 67 6e 65 64 20  static unsigned 
ace80 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 20 67 5f  long long int g_
ace90 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20 75 6e  start;.static un
acea0 73 69 67 6e 65 64 20 69 6e 74 20 65 6c 61 70 73  signed int elaps
aceb0 65 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52  e;.#define TIMER
acec0 5f 53 54 41 52 54 20 20 20 20 20 20 20 67 5f 73  _START       g_s
aced0 74 61 72 74 3d 68 77 74 69 6d 65 28 29 0a 23 64  tart=hwtime().#d
acee0 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20  efine TIMER_END 
acef0 20 20 20 20 20 20 20 20 65 6c 61 70 73 65 3d 68          elapse=h
acf00 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a  wtime()-g_start.
acf10 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c  #define TIMER_EL
acf20 41 50 53 45 44 20 20 20 20 20 65 6c 61 70 73 65  APSED     elapse
acf30 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 54  .#else.#define T
acf40 49 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66 69  IMER_START.#defi
acf50 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64 65  ne TIMER_END.#de
acf60 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53  fine TIMER_ELAPS
acf70 45 44 20 20 20 20 20 30 0a 23 65 6e 64 69 66 0a  ED     0.#endif.
acf80 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d  ./*.** If we com
acf90 70 69 6c 65 20 77 69 74 68 20 74 68 65 20 53 51  pile with the SQ
acfa0 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72 6f 20  LITE_TEST macro 
acfb0 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 66 6f  set, then the fo
acfc0 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a  llowing block.**
acfd0 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 67 69   of code will gi
acfe0 76 65 20 75 73 20 74 68 65 20 61 62 69 6c 69 74  ve us the abilit
acff0 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 20  y to simulate a 
ad000 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20  disk I/O error. 
ad010 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64   This.** is used
ad020 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74 68 65   for testing the
ad030 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f   I/O recovery lo
ad040 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  gic..*/.#ifdef S
ad050 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
ad060 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
ad070 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d  3_io_error_hit =
ad080 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f   0;            /
ad090 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  * Total number o
ad0a0 66 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0a  f I/O Errors */.
ad0b0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
ad0c0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
ad0d0 68 61 72 64 68 69 74 20 3d 20 30 3b 20 20 20 20  hardhit = 0;    
ad0e0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
ad0f0 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f   non-benign erro
ad100 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  rs */.SQLITE_API
ad110 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f   int sqlite3_io_
ad120 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20  error_pending = 
ad130 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75  0;        /* Cou
ad140 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69 72 73 74  nt down to first
ad150 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 51   I/O error */.SQ
ad160 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
ad170 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65  ite3_io_error_pe
ad180 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20  rsist = 0;      
ad190 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49 2f 4f    /* True if I/O
ad1a0 20 65 72 72 6f 72 73 20 70 65 72 73 69 73 74 20   errors persist 
ad1b0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
ad1c0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  t sqlite3_io_err
ad1d0 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20  or_benign = 0;  
ad1e0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
ad1f0 66 20 65 72 72 6f 72 73 20 61 72 65 20 62 65 6e  f errors are ben
ad200 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ign */.SQLITE_AP
ad210 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69  I int sqlite3_di
ad220 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d  skfull_pending =
ad230 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   0;.SQLITE_API i
ad240 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66  nt sqlite3_diskf
ad250 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65  ull = 0;.#define
ad260 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
ad270 42 65 6e 69 67 6e 28 58 29 20 73 71 6c 69 74 65  Benign(X) sqlite
ad280 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67  3_io_error_benig
ad290 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69  n=(X).#define Si
ad2a0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 43 4f  mulateIOError(CO
ad2b0 44 45 29 20 20 5c 0a 20 20 69 66 28 20 28 73 71  DE)  \.  if( (sq
ad2c0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
ad2d0 65 72 73 69 73 74 20 26 26 20 73 71 6c 69 74 65  ersist && sqlite
ad2e0 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20  3_io_error_hit) 
ad2f0 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69  \.       || sqli
ad300 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e  te3_io_error_pen
ad310 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 20 5c  ding-- == 1 )  \
ad320 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7b  .              {
ad330 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20   local_ioerr(); 
ad340 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63 20 76  CODE; }.static v
ad350 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28  oid local_ioerr(
ad360 29 7b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 49  ){.  IOTRACE(("I
ad370 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73 71 6c  OERR\n"));.  sql
ad380 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69  ite3_io_error_hi
ad390 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71 6c 69  t++;.  if( !sqli
ad3a0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e  te3_io_error_ben
ad3b0 69 67 6e 20 29 20 73 71 6c 69 74 65 33 5f 69 6f  ign ) sqlite3_io
ad3c0 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 2b 2b  _error_hardhit++
ad3d0 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69 6d 75  ;.}.#define Simu
ad3e0 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f  lateDiskfullErro
ad3f0 72 28 43 4f 44 45 29 20 5c 0a 20 20 20 69 66 28  r(CODE) \.   if(
ad400 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c   sqlite3_diskful
ad410 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20  l_pending ){ \. 
ad420 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f      if( sqlite3_
ad430 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67  diskfull_pending
ad440 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20 20 20   == 1 ){ \.     
ad450 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b    local_ioerr();
ad460 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65   \.       sqlite
ad470 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20  3_diskfull = 1; 
ad480 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  \.       sqlite3
ad490 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20  _io_error_hit = 
ad4a0 31 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f 44 45  1; \.       CODE
ad4b0 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20  ; \.     }else{ 
ad4c0 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  \.       sqlite3
ad4d0 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e  _diskfull_pendin
ad4e0 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a  g--; \.     } \.
ad4f0 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69     }.#else.#defi
ad500 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  ne SimulateIOErr
ad510 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23 64 65 66  orBenign(X).#def
ad520 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ine SimulateIOEr
ad530 72 6f 72 28 41 29 0a 23 64 65 66 69 6e 65 20 53  ror(A).#define S
ad540 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45  imulateDiskfullE
ad550 72 72 6f 72 28 41 29 0a 23 65 6e 64 69 66 0a 0a  rror(A).#endif..
ad560 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73 74 69  /*.** When testi
ad570 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75 6e 74  ng, keep a count
ad580 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   of the number o
ad590 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f  f open files..*/
ad5a0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
ad5b0 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  EST.SQLITE_API i
ad5c0 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  nt sqlite3_open_
ad5d0 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a  file_count = 0;.
ad5e0 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e  #define OpenCoun
ad5f0 74 65 72 28 58 29 20 20 73 71 6c 69 74 65 33 5f  ter(X)  sqlite3_
ad600 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b  open_file_count+
ad610 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 65 66 69  =(X).#else.#defi
ad620 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58  ne OpenCounter(X
ad630 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  ).#endif../*****
ad640 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
ad650 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a   os_common.h ***
ad660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ad670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ad680 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
ad690 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
ad6a0 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
ad6b0 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 75 6e 69  ft off in os_uni
ad6c0 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  x.c ************
ad6d0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ********/../*.**
ad6e0 20 44 65 66 69 6e 65 20 76 61 72 69 6f 75 73 20   Define various 
ad6f0 6d 61 63 72 6f 73 20 74 68 61 74 20 61 72 65 20  macros that are 
ad700 6d 69 73 73 69 6e 67 20 66 72 6f 6d 20 73 6f 6d  missing from som
ad710 65 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69  e systems..*/.#i
ad720 66 6e 64 65 66 20 4f 5f 4c 41 52 47 45 46 49 4c  fndef O_LARGEFIL
ad730 45 0a 23 20 64 65 66 69 6e 65 20 4f 5f 4c 41 52  E.# define O_LAR
ad740 47 45 46 49 4c 45 20 30 0a 23 65 6e 64 69 66 0a  GEFILE 0.#endif.
ad750 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 49  #ifdef SQLITE_DI
ad760 53 41 42 4c 45 5f 4c 46 53 0a 23 20 75 6e 64 65  SABLE_LFS.# unde
ad770 66 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20  f O_LARGEFILE.# 
ad780 64 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49  define O_LARGEFI
ad790 4c 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e  LE 0.#endif.#ifn
ad7a0 64 65 66 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23  def O_NOFOLLOW.#
ad7b0 20 64 65 66 69 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c   define O_NOFOLL
ad7c0 4f 57 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e  OW 0.#endif.#ifn
ad7d0 64 65 66 20 4f 5f 42 49 4e 41 52 59 0a 23 20 64  def O_BINARY.# d
ad7e0 65 66 69 6e 65 20 4f 5f 42 49 4e 41 52 59 20 30  efine O_BINARY 0
ad7f0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
ad800 68 65 20 44 4a 47 50 50 20 63 6f 6d 70 69 6c 65  he DJGPP compile
ad810 72 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 6c 6f  r environment lo
ad820 6f 6b 73 20 6d 6f 73 74 6c 79 20 6c 69 6b 65 20  oks mostly like 
ad830 55 6e 69 78 2c 20 62 75 74 20 69 74 0a 2a 2a 20  Unix, but it.** 
ad840 6c 61 63 6b 73 20 74 68 65 20 66 63 6e 74 6c 28  lacks the fcntl(
ad850 29 20 73 79 73 74 65 6d 20 63 61 6c 6c 2e 20 20  ) system call.  
ad860 53 6f 20 72 65 64 65 66 69 6e 65 20 66 63 6e 74  So redefine fcnt
ad870 6c 28 29 20 74 6f 20 62 65 20 73 6f 6d 65 74 68  l() to be someth
ad880 69 6e 67 0a 2a 2a 20 74 68 61 74 20 61 6c 77 61  ing.** that alwa
ad890 79 73 20 73 75 63 63 65 65 64 73 2e 20 20 54 68  ys succeeds.  Th
ad8a0 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 6c 6f  is means that lo
ad8b0 63 6b 69 6e 67 20 64 6f 65 73 20 6e 6f 74 20 6f  cking does not o
ad8c0 63 63 75 72 20 75 6e 64 65 72 0a 2a 2a 20 44 4a  ccur under.** DJ
ad8d0 47 50 50 2e 20 20 42 75 74 20 69 74 20 69 73 20  GPP.  But it is 
ad8e0 44 4f 53 20 2d 20 77 68 61 74 20 64 69 64 20 79  DOS - what did y
ad8f0 6f 75 20 65 78 70 65 63 74 3f 0a 2a 2f 0a 23 69  ou expect?.*/.#i
ad900 66 64 65 66 20 5f 5f 44 4a 47 50 50 5f 5f 0a 23  fdef __DJGPP__.#
ad910 20 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 41 2c   define fcntl(A,
ad920 42 2c 43 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f  B,C) 0.#endif../
ad930 2a 0a 2a 2a 20 54 68 65 20 74 68 72 65 61 64 69  *.** The threadi
ad940 64 20 6d 61 63 72 6f 20 72 65 73 6f 6c 76 65 73  d macro resolves
ad950 20 74 6f 20 74 68 65 20 74 68 72 65 61 64 2d 69   to the thread-i
ad960 64 20 6f 72 20 74 6f 20 30 2e 20 20 55 73 65 64  d or to 0.  Used
ad970 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20   for.** testing 
ad980 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e  and debugging on
ad990 6c 79 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54  ly..*/.#if SQLIT
ad9a0 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23 64 65  E_THREADSAFE.#de
ad9b0 66 69 6e 65 20 74 68 72 65 61 64 69 64 20 70 74  fine threadid pt
ad9c0 68 72 65 61 64 5f 73 65 6c 66 28 29 0a 23 65 6c  hread_self().#el
ad9d0 73 65 0a 23 64 65 66 69 6e 65 20 74 68 72 65 61  se.#define threa
ad9e0 64 69 64 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a  did 0.#endif../*
ad9f0 0a 2a 2a 20 53 65 74 20 6f 72 20 63 68 65 63 6b  .** Set or check
ada00 20 74 68 65 20 75 6e 69 78 46 69 6c 65 2e 74 69   the unixFile.ti
ada10 64 20 66 69 65 6c 64 2e 20 20 54 68 69 73 20 66  d field.  This f
ada20 69 65 6c 64 20 69 73 20 73 65 74 20 77 68 65 6e  ield is set when
ada30 20 61 6e 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20   an unixFile.** 
ada40 69 73 20 66 69 72 73 74 20 6f 70 65 6e 65 64 2e  is first opened.
ada50 20 20 41 6c 6c 20 73 75 62 73 65 71 75 65 6e 74    All subsequent
ada60 20 75 73 65 73 20 6f 66 20 74 68 65 20 75 6e 69   uses of the uni
ada70 78 46 69 6c 65 20 76 65 72 69 66 79 20 74 68 61  xFile verify tha
ada80 74 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 74 68  t the.** same th
ada90 72 65 61 64 20 69 73 20 6f 70 65 72 61 74 69 6e  read is operatin
adaa0 67 20 6f 6e 20 74 68 65 20 75 6e 69 78 46 69 6c  g on the unixFil
adab0 65 2e 20 20 53 6f 6d 65 20 6f 70 65 72 61 74 69  e.  Some operati
adac0 6e 67 20 73 79 73 74 65 6d 73 20 64 6f 0a 2a 2a  ng systems do.**
adad0 20 6e 6f 74 20 61 6c 6c 6f 77 20 6c 6f 63 6b 73   not allow locks
adae0 20 74 6f 20 62 65 20 6f 76 65 72 72 69 64 64 65   to be overridde
adaf0 6e 20 62 79 20 6f 74 68 65 72 20 74 68 72 65 61  n by other threa
adb00 64 73 20 61 6e 64 20 74 68 61 74 20 72 65 73 74  ds and that rest
adb10 72 69 63 74 69 6f 6e 0a 2a 2a 20 6d 65 61 6e 73  riction.** means
adb20 20 74 68 61 74 20 73 71 6c 69 74 65 33 2a 20 64   that sqlite3* d
adb30 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 73 20  atabase handles 
adb40 63 61 6e 6e 6f 74 20 62 65 20 6d 6f 76 65 64 20  cannot be moved 
adb50 66 72 6f 6d 20 6f 6e 65 20 74 68 72 65 61 64 0a  from one thread.
adb60 2a 2a 20 74 6f 20 61 6e 6f 74 68 65 72 2e 20 20  ** to another.  
adb70 54 68 69 73 20 6c 6f 67 69 63 20 6d 61 6b 65 73  This logic makes
adb80 20 73 75 72 65 20 61 20 75 73 65 72 20 64 6f 65   sure a user doe
adb90 73 20 6e 6f 74 20 74 72 79 20 74 6f 20 64 6f 20  s not try to do 
adba0 74 68 61 74 0a 2a 2a 20 62 79 20 6d 69 73 74 61  that.** by mista
adbb0 6b 65 2e 0a 2a 2a 0a 2a 2a 20 56 65 72 73 69 6f  ke..**.** Versio
adbc0 6e 20 33 2e 33 2e 31 20 28 32 30 30 36 2d 30 31  n 3.3.1 (2006-01
adbd0 2d 31 35 29 3a 20 20 75 6e 69 78 46 69 6c 65 20  -15):  unixFile 
adbe0 63 61 6e 20 62 65 20 6d 6f 76 65 64 20 66 72 6f  can be moved fro
adbf0 6d 20 6f 6e 65 20 74 68 72 65 61 64 20 74 6f 0a  m one thread to.
adc00 2a 2a 20 61 6e 6f 74 68 65 72 20 61 73 20 6c 6f  ** another as lo
adc10 6e 67 20 61 73 20 77 65 20 61 72 65 20 72 75 6e  ng as we are run
adc20 6e 69 6e 67 20 6f 6e 20 61 20 73 79 73 74 65 6d  ning on a system
adc30 20 74 68 61 74 20 73 75 70 70 6f 72 74 73 20 74   that supports t
adc40 68 72 65 61 64 73 0a 2a 2a 20 6f 76 65 72 72 69  hreads.** overri
adc50 64 69 6e 67 20 65 61 63 68 20 6f 74 68 65 72 73  ding each others
adc60 20 6c 6f 63 6b 73 20 28 77 68 69 63 68 20 6e 6f   locks (which no
adc70 77 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f  w the most commo
adc80 6e 20 62 65 68 61 76 69 6f 72 29 0a 2a 2a 20 6f  n behavior).** o
adc90 72 20 69 66 20 6e 6f 20 6c 6f 63 6b 73 20 61 72  r if no locks ar
adca0 65 20 68 65 6c 64 2e 20 20 42 75 74 20 74 68 65  e held.  But the
adcb0 20 75 6e 69 78 46 69 6c 65 2e 70 4c 6f 63 6b 20   unixFile.pLock 
adcc0 66 69 65 6c 64 20 6e 65 65 64 73 20 74 6f 20 62  field needs to b
adcd0 65 0a 2a 2a 20 72 65 63 6f 6d 70 75 74 65 64 20  e.** recomputed 
adce0 62 65 63 61 75 73 65 20 69 74 73 20 6b 65 79 20  because its key 
adcf0 69 6e 63 6c 75 64 65 73 20 74 68 65 20 74 68 72  includes the thr
add00 65 61 64 2d 69 64 2e 20 20 53 65 65 20 74 68 65  ead-id.  See the
add10 20 0a 2a 2a 20 74 72 61 6e 73 66 65 72 4f 77 6e   .** transferOwn
add20 65 72 73 68 69 70 28 29 20 66 75 6e 63 74 69 6f  ership() functio
add30 6e 20 62 65 6c 6f 77 20 66 6f 72 20 61 64 64 69  n below for addi
add40 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69  tional informati
add50 6f 6e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  on.*/.#if SQLITE
add60 5f 54 48 52 45 41 44 53 41 46 45 0a 23 20 64 65  _THREADSAFE.# de
add70 66 69 6e 65 20 53 45 54 5f 54 48 52 45 41 44 49  fine SET_THREADI
add80 44 28 58 29 20 20 20 28 58 29 2d 3e 74 69 64 20  D(X)   (X)->tid 
add90 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29  = pthread_self()
adda0 0a 23 20 64 65 66 69 6e 65 20 43 48 45 43 4b 5f  .# define CHECK_
addb0 54 48 52 45 41 44 49 44 28 58 29 20 28 74 68 72  THREADID(X) (thr
addc0 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68  eadsOverrideEach
addd0 4f 74 68 65 72 73 4c 6f 63 6b 73 3d 3d 30 20 26  OthersLocks==0 &
adde0 26 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  & \.            
addf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ade00 21 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 28  !pthread_equal((
ade10 58 29 2d 3e 74 69 64 2c 20 70 74 68 72 65 61 64  X)->tid, pthread
ade20 5f 73 65 6c 66 28 29 29 29 0a 23 65 6c 73 65 0a  _self())).#else.
ade30 23 20 64 65 66 69 6e 65 20 53 45 54 5f 54 48 52  # define SET_THR
ade40 45 41 44 49 44 28 58 29 0a 23 20 64 65 66 69 6e  EADID(X).# defin
ade50 65 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44  e CHECK_THREADID
ade60 28 58 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a  (X) 0.#endif../*
ade70 0a 2a 2a 20 48 65 72 65 20 69 73 20 74 68 65 20  .** Here is the 
ade80 64 69 72 74 20 6f 6e 20 50 4f 53 49 58 20 61 64  dirt on POSIX ad
ade90 76 69 73 6f 72 79 20 6c 6f 63 6b 73 3a 20 20 41  visory locks:  A
adea0 4e 53 49 20 53 54 44 20 31 30 30 33 2e 31 20 28  NSI STD 1003.1 (
adeb0 31 39 39 36 29 0a 2a 2a 20 73 65 63 74 69 6f 6e  1996).** section
adec0 20 36 2e 35 2e 32 2e 32 20 6c 69 6e 65 73 20 34   6.5.2.2 lines 4
aded0 38 33 20 74 68 72 6f 75 67 68 20 34 39 30 20 73  83 through 490 s
adee0 70 65 63 69 66 79 20 74 68 61 74 20 77 68 65 6e  pecify that when
adef0 20 61 20 70 72 6f 63 65 73 73 0a 2a 2a 20 73 65   a process.** se
adf00 74 73 20 6f 72 20 63 6c 65 61 72 73 20 61 20 6c  ts or clears a l
adf10 6f 63 6b 2c 20 74 68 61 74 20 6f 70 65 72 61 74  ock, that operat
adf20 69 6f 6e 20 6f 76 65 72 72 69 64 65 73 20 61 6e  ion overrides an
adf30 79 20 70 72 69 6f 72 20 6c 6f 63 6b 73 20 73 65  y prior locks se
adf40 74 0a 2a 2a 20 62 79 20 74 68 65 20 73 61 6d 65  t.** by the same
adf50 20 70 72 6f 63 65 73 73 2e 20 20 49 74 20 64 6f   process.  It do
adf60 65 73 20 6e 6f 74 20 65 78 70 6c 69 63 69 74 6c  es not explicitl
adf70 79 20 73 61 79 20 73 6f 2c 20 62 75 74 20 74 68  y say so, but th
adf80 69 73 20 69 6d 70 6c 69 65 73 0a 2a 2a 20 74 68  is implies.** th
adf90 61 74 20 69 74 20 6f 76 65 72 72 69 64 65 73 20  at it overrides 
adfa0 6c 6f 63 6b 73 20 73 65 74 20 62 79 20 74 68 65  locks set by the
adfb0 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20 75 73   same process us
adfc0 69 6e 67 20 61 20 64 69 66 66 65 72 65 6e 74 0a  ing a different.
adfd0 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ** file descript
adfe0 6f 72 2e 20 20 43 6f 6e 73 69 64 65 72 20 74 68  or.  Consider th
adff0 69 73 20 74 65 73 74 20 63 61 73 65 3a 0a 2a 2a  is test case:.**
ae000 0a 2a 2a 20 20 20 20 20 20 20 69 6e 74 20 66 64  .**       int fd
ae010 31 20 3d 20 6f 70 65 6e 28 22 2e 2f 66 69 6c 65  1 = open("./file
ae020 31 22 2c 20 4f 5f 52 44 57 52 7c 4f 5f 43 52 45  1", O_RDWR|O_CRE
ae030 41 54 2c 20 30 36 34 34 29 3b 0a 2a 2a 20 20 20  AT, 0644);.**   
ae040 20 20 20 20 69 6e 74 20 66 64 32 20 3d 20 6f 70      int fd2 = op
ae050 65 6e 28 22 2e 2f 66 69 6c 65 32 22 2c 20 4f 5f  en("./file2", O_
ae060 52 44 57 52 7c 4f 5f 43 52 45 41 54 2c 20 30 36  RDWR|O_CREAT, 06
ae070 34 34 29 3b 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f  44);.**.** Suppo
ae080 73 65 20 2e 2f 66 69 6c 65 31 20 61 6e 64 20 2e  se ./file1 and .
ae090 2f 66 69 6c 65 32 20 61 72 65 20 72 65 61 6c 6c  /file2 are reall
ae0a0 79 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20  y the same file 
ae0b0 28 62 65 63 61 75 73 65 0a 2a 2a 20 6f 6e 65 20  (because.** one 
ae0c0 69 73 20 61 20 68 61 72 64 20 6f 72 20 73 79 6d  is a hard or sym
ae0d0 62 6f 6c 69 63 20 6c 69 6e 6b 20 74 6f 20 74 68  bolic link to th
ae0e0 65 20 6f 74 68 65 72 29 20 74 68 65 6e 20 69 66  e other) then if
ae0f0 20 79 6f 75 20 73 65 74 0a 2a 2a 20 61 6e 20 65   you set.** an e
ae100 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e  xclusive lock on
ae110 20 66 64 31 2c 20 74 68 65 6e 20 74 72 79 20 74   fd1, then try t
ae120 6f 20 67 65 74 20 61 6e 20 65 78 63 6c 75 73 69  o get an exclusi
ae130 76 65 20 6c 6f 63 6b 0a 2a 2a 20 6f 6e 20 66 64  ve lock.** on fd
ae140 32 2c 20 69 74 20 77 6f 72 6b 73 2e 20 20 49 20  2, it works.  I 
ae150 77 6f 75 6c 64 20 68 61 76 65 20 65 78 70 65 63  would have expec
ae160 74 65 64 20 74 68 65 20 73 65 63 6f 6e 64 20 6c  ted the second l
ae170 6f 63 6b 20 74 6f 0a 2a 2a 20 66 61 69 6c 20 73  ock to.** fail s
ae180 69 6e 63 65 20 74 68 65 72 65 20 77 61 73 20 61  ince there was a
ae190 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 6e  lready a lock on
ae1a0 20 74 68 65 20 66 69 6c 65 20 64 75 65 20 74 6f   the file due to
ae1b0 20 66 64 31 2e 0a 2a 2a 20 42 75 74 20 6e 6f 74   fd1..** But not
ae1c0 20 73 6f 2e 20 20 53 69 6e 63 65 20 62 6f 74 68   so.  Since both
ae1d0 20 6c 6f 63 6b 73 20 63 61 6d 65 20 66 72 6f 6d   locks came from
ae1e0 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73   the same proces
ae1f0 73 2c 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64  s, the.** second
ae200 20 6f 76 65 72 72 69 64 65 73 20 74 68 65 20 66   overrides the f
ae210 69 72 73 74 2c 20 65 76 65 6e 20 74 68 6f 75 67  irst, even thoug
ae220 68 20 74 68 65 79 20 77 65 72 65 20 6f 6e 20 64  h they were on d
ae230 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 69 6c 65  ifferent.** file
ae240 20 64 65 73 63 72 69 70 74 6f 72 73 20 6f 70 65   descriptors ope
ae250 6e 65 64 20 6f 6e 20 64 69 66 66 65 72 65 6e 74  ned on different
ae260 20 66 69 6c 65 20 6e 61 6d 65 73 2e 0a 2a 2a 0a   file names..**.
ae270 2a 2a 20 42 75 6d 6d 65 72 2e 20 20 49 66 20 79  ** Bummer.  If y
ae280 6f 75 20 61 73 6b 20 6d 65 2c 20 74 68 69 73 20  ou ask me, this 
ae290 69 73 20 62 72 6f 6b 65 6e 2e 20 20 42 61 64 6c  is broken.  Badl
ae2a0 79 20 62 72 6f 6b 65 6e 2e 20 20 49 74 20 6d 65  y broken.  It me
ae2b0 61 6e 73 0a 2a 2a 20 74 68 61 74 20 77 65 20 63  ans.** that we c
ae2c0 61 6e 6e 6f 74 20 75 73 65 20 50 4f 53 49 58 20  annot use POSIX 
ae2d0 6c 6f 63 6b 73 20 74 6f 20 73 79 6e 63 68 72 6f  locks to synchro
ae2e0 6e 69 7a 65 20 66 69 6c 65 20 61 63 63 65 73 73  nize file access
ae2f0 20 61 6d 6f 6e 67 0a 2a 2a 20 63 6f 6d 70 65 74   among.** compet
ae300 69 6e 67 20 74 68 72 65 61 64 73 20 6f 66 20 74  ing threads of t
ae310 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e  he same process.
ae320 20 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20 77 69    POSIX locks wi
ae330 6c 6c 20 77 6f 72 6b 20 66 69 6e 65 0a 2a 2a 20  ll work fine.** 
ae340 74 6f 20 73 79 6e 63 68 72 6f 6e 69 7a 65 20 61  to synchronize a
ae350 63 63 65 73 73 20 66 6f 72 20 74 68 72 65 61 64  ccess for thread
ae360 73 20 69 6e 20 73 65 70 61 72 61 74 65 20 70 72  s in separate pr
ae370 6f 63 65 73 73 65 73 2c 20 62 75 74 20 6e 6f 74  ocesses, but not
ae380 0a 2a 2a 20 74 68 72 65 61 64 73 20 77 69 74 68  .** threads with
ae390 69 6e 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63  in the same proc
ae3a0 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 77 6f  ess..**.** To wo
ae3b0 72 6b 20 61 72 6f 75 6e 64 20 74 68 65 20 70 72  rk around the pr
ae3c0 6f 62 6c 65 6d 2c 20 53 51 4c 69 74 65 20 68 61  oblem, SQLite ha
ae3d0 73 20 74 6f 20 6d 61 6e 61 67 65 20 66 69 6c 65  s to manage file
ae3e0 20 6c 6f 63 6b 73 20 69 6e 74 65 72 6e 61 6c 6c   locks internall
ae3f0 79 0a 2a 2a 20 6f 6e 20 69 74 73 20 6f 77 6e 2e  y.** on its own.
ae400 20 20 57 68 65 6e 65 76 65 72 20 61 20 6e 65 77    Whenever a new
ae410 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65   database is ope
ae420 6e 65 64 2c 20 77 65 20 68 61 76 65 20 74 6f 20  ned, we have to 
ae430 66 69 6e 64 20 74 68 65 0a 2a 2a 20 73 70 65 63  find the.** spec
ae440 69 66 69 63 20 69 6e 6f 64 65 20 6f 66 20 74 68  ific inode of th
ae450 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
ae460 28 74 68 65 20 69 6e 6f 64 65 20 69 73 20 64 65  (the inode is de
ae470 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 0a  termined by the.
ae480 2a 2a 20 73 74 5f 64 65 76 20 61 6e 64 20 73 74  ** st_dev and st
ae490 5f 69 6e 6f 20 66 69 65 6c 64 73 20 6f 66 20 74  _ino fields of t
ae4a0 68 65 20 73 74 61 74 20 73 74 72 75 63 74 75 72  he stat structur
ae4b0 65 20 74 68 61 74 20 66 73 74 61 74 28 29 20 66  e that fstat() f
ae4c0 69 6c 6c 73 20 69 6e 29 0a 2a 2a 20 61 6e 64 20  ills in).** and 
ae4d0 63 68 65 63 6b 20 66 6f 72 20 6c 6f 63 6b 73 20  check for locks 
ae4e0 61 6c 72 65 61 64 79 20 65 78 69 73 74 69 6e 67  already existing
ae4f0 20 6f 6e 20 74 68 61 74 20 69 6e 6f 64 65 2e 20   on that inode. 
ae500 20 57 68 65 6e 20 6c 6f 63 6b 73 20 61 72 65 0a   When locks are.
ae510 2a 2a 20 63 72 65 61 74 65 64 20 6f 72 20 72 65  ** created or re
ae520 6d 6f 76 65 64 2c 20 77 65 20 68 61 76 65 20 74  moved, we have t
ae530 6f 20 6c 6f 6f 6b 20 61 74 20 6f 75 72 20 6f 77  o look at our ow
ae540 6e 20 69 6e 74 65 72 6e 61 6c 20 72 65 63 6f 72  n internal recor
ae550 64 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b  d of the.** lock
ae560 73 20 74 6f 20 73 65 65 20 69 66 20 61 6e 6f 74  s to see if anot
ae570 68 65 72 20 74 68 72 65 61 64 20 68 61 73 20 70  her thread has p
ae580 72 65 76 69 6f 75 73 6c 79 20 73 65 74 20 61 20  reviously set a 
ae590 6c 6f 63 6b 20 6f 6e 20 74 68 61 74 20 73 61 6d  lock on that sam
ae5a0 65 0a 2a 2a 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a  e.** inode..**.*
ae5b0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 66 69  * The sqlite3_fi
ae5c0 6c 65 20 73 74 72 75 63 74 75 72 65 20 66 6f 72  le structure for
ae5d0 20 50 4f 53 49 58 20 69 73 20 6e 6f 20 6c 6f 6e   POSIX is no lon
ae5e0 67 65 72 20 6a 75 73 74 20 61 6e 20 69 6e 74 65  ger just an inte
ae5f0 67 65 72 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63  ger file.** desc
ae600 72 69 70 74 6f 72 2e 20 20 49 74 20 69 73 20 6e  riptor.  It is n
ae610 6f 77 20 61 20 73 74 72 75 63 74 75 72 65 20 74  ow a structure t
ae620 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 69 6e  hat holds the in
ae630 74 65 67 65 72 20 66 69 6c 65 0a 2a 2a 20 64 65  teger file.** de
ae640 73 63 72 69 70 74 6f 72 20 61 6e 64 20 61 20 70  scriptor and a p
ae650 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 72 75  ointer to a stru
ae660 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72  cture that descr
ae670 69 62 65 73 20 74 68 65 20 69 6e 74 65 72 6e 61  ibes the interna
ae680 6c 0a 2a 2a 20 6c 6f 63 6b 73 20 6f 6e 20 74 68  l.** locks on th
ae690 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
ae6a0 69 6e 6f 64 65 2e 20 20 54 68 65 72 65 20 69 73  inode.  There is
ae6b0 20 6f 6e 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72   one locking str
ae6c0 75 63 74 75 72 65 0a 2a 2a 20 70 65 72 20 69 6e  ucture.** per in
ae6d0 6f 64 65 2c 20 73 6f 20 69 66 20 74 68 65 20 73  ode, so if the s
ae6e0 61 6d 65 20 69 6e 6f 64 65 20 69 73 20 6f 70 65  ame inode is ope
ae6f0 6e 65 64 20 74 77 69 63 65 2c 20 62 6f 74 68 20  ned twice, both 
ae700 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75  unixFile structu
ae710 72 65 73 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20  res.** point to 
ae720 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67  the same locking
ae730 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65   structure.  The
ae740 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75   locking structu
ae750 72 65 20 6b 65 65 70 73 0a 2a 2a 20 61 20 72 65  re keeps.** a re
ae760 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 28 73  ference count (s
ae770 6f 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77  o we will know w
ae780 68 65 6e 20 74 6f 20 64 65 6c 65 74 65 20 69 74  hen to delete it
ae790 29 20 61 6e 64 20 61 20 22 63 6e 74 22 0a 2a 2a  ) and a "cnt".**
ae7a0 20 66 69 65 6c 64 20 74 68 61 74 20 74 65 6c 6c   field that tell
ae7b0 73 20 75 73 20 69 74 73 20 69 6e 74 65 72 6e 61  s us its interna
ae7c0 6c 20 6c 6f 63 6b 20 73 74 61 74 75 73 2e 20 20  l lock status.  
ae7d0 63 6e 74 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65  cnt==0 means the
ae7e0 0a 2a 2a 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f  .** file is unlo
ae7f0 63 6b 65 64 2e 20 20 63 6e 74 3d 3d 2d 31 20 6d  cked.  cnt==-1 m
ae800 65 61 6e 73 20 74 68 65 20 66 69 6c 65 20 68 61  eans the file ha
ae810 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  s an exclusive l
ae820 6f 63 6b 2e 0a 2a 2a 20 63 6e 74 3e 30 20 6d 65  ock..** cnt>0 me
ae830 61 6e 73 20 74 68 65 72 65 20 61 72 65 20 63 6e  ans there are cn
ae840 74 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 6f  t shared locks o
ae850 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  n the file..**.*
ae860 2a 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f  * Any attempt to
ae870 20 6c 6f 63 6b 20 6f 72 20 75 6e 6c 6f 63 6b 20   lock or unlock 
ae880 61 20 66 69 6c 65 20 66 69 72 73 74 20 63 68 65  a file first che
ae890 63 6b 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 0a  cks the locking.
ae8a0 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  ** structure.  T
ae8b0 68 65 20 66 63 6e 74 6c 28 29 20 73 79 73 74 65  he fcntl() syste
ae8c0 6d 20 63 61 6c 6c 20 69 73 20 6f 6e 6c 79 20 69  m call is only i
ae8d0 6e 76 6f 6b 65 64 20 74 6f 20 73 65 74 20 61 20  nvoked to set a 
ae8e0 0a 2a 2a 20 50 4f 53 49 58 20 6c 6f 63 6b 20 69  .** POSIX lock i
ae8f0 66 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6c  f the internal l
ae900 6f 63 6b 20 73 74 72 75 63 74 75 72 65 20 74 72  ock structure tr
ae910 61 6e 73 69 74 69 6f 6e 73 20 62 65 74 77 65 65  ansitions betwee
ae920 6e 0a 2a 2a 20 61 20 6c 6f 63 6b 65 64 20 61 6e  n.** a locked an
ae930 64 20 61 6e 20 75 6e 6c 6f 63 6b 65 64 20 73 74  d an unlocked st
ae940 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 34 2d  ate..**.** 2004-
ae950 4a 61 6e 2d 31 31 3a 0a 2a 2a 20 4d 6f 72 65 20  Jan-11:.** More 
ae960 72 65 63 65 6e 74 20 64 69 73 63 6f 76 65 72 69  recent discoveri
ae970 65 73 20 61 62 6f 75 74 20 50 4f 53 49 58 20 61  es about POSIX a
ae980 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 2e 20 20  dvisory locks.  
ae990 28 54 68 65 20 6d 6f 72 65 0a 2a 2a 20 49 20 64  (The more.** I d
ae9a0 69 73 63 6f 76 65 72 2c 20 74 68 65 20 6d 6f 72  iscover, the mor
ae9b0 65 20 49 20 72 65 61 6c 69 7a 65 20 74 68 65 20  e I realize the 
ae9c0 61 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79  a POSIX advisory
ae9d0 20 6c 6f 63 6b 73 20 61 72 65 0a 2a 2a 20 61 6e   locks are.** an
ae9e0 20 61 62 6f 6d 69 6e 61 74 69 6f 6e 2e 29 0a 2a   abomination.).*
ae9f0 2a 0a 2a 2a 20 49 66 20 79 6f 75 20 63 6c 6f 73  *.** If you clos
aea00 65 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 70  e a file descrip
aea10 74 6f 72 20 74 68 61 74 20 70 6f 69 6e 74 73 20  tor that points 
aea20 74 6f 20 61 20 66 69 6c 65 20 74 68 61 74 20 68  to a file that h
aea30 61 73 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 61 6c 6c  as locks,.** all
aea40 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 61 74 20 66   locks on that f
aea50 69 6c 65 20 74 68 61 74 20 61 72 65 20 6f 77 6e  ile that are own
aea60 65 64 20 62 79 20 74 68 65 20 63 75 72 72 65 6e  ed by the curren
aea70 74 20 70 72 6f 63 65 73 73 20 61 72 65 0a 2a 2a  t process are.**
aea80 20 72 65 6c 65 61 73 65 64 2e 20 20 54 6f 20 77   released.  To w
aea90 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 69 73 20  ork around this 
aeaa0 70 72 6f 62 6c 65 6d 2c 20 65 61 63 68 20 75 6e  problem, each un
aeab0 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 72 65  ixFile structure
aeac0 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 70   contains.** a p
aead0 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 6f 70 65  ointer to an ope
aeae0 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 2e 20  nCnt structure. 
aeaf0 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 6f 70   There is one op
aeb00 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 0a  enCnt structure.
aeb10 2a 2a 20 70 65 72 20 6f 70 65 6e 20 69 6e 6f 64  ** per open inod
aeb20 65 2c 20 77 68 69 63 68 20 6d 65 61 6e 73 20 74  e, which means t
aeb30 68 61 74 20 6d 75 6c 74 69 70 6c 65 20 75 6e 69  hat multiple uni
aeb40 78 46 69 6c 65 20 63 61 6e 20 70 6f 69 6e 74 20  xFile can point 
aeb50 74 6f 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 6f  to a single.** o
aeb60 70 65 6e 43 6e 74 2e 20 20 57 68 65 6e 20 61 6e  penCnt.  When an
aeb70 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
aeb80 20 74 6f 20 63 6c 6f 73 65 20 61 6e 20 75 6e 69   to close an uni
aeb90 78 46 69 6c 65 2c 20 69 66 20 74 68 65 72 65 20  xFile, if there 
aeba0 61 72 65 0a 2a 2a 20 6f 74 68 65 72 20 75 6e 69  are.** other uni
aebb0 78 46 69 6c 65 20 6f 70 65 6e 20 6f 6e 20 74 68  xFile open on th
aebc0 65 20 73 61 6d 65 20 69 6e 6f 64 65 20 74 68 61  e same inode tha
aebd0 74 20 61 72 65 20 68 6f 6c 64 69 6e 67 20 6c 6f  t are holding lo
aebe0 63 6b 73 2c 20 74 68 65 20 63 61 6c 6c 0a 2a 2a  cks, the call.**
aebf0 20 74 6f 20 63 6c 6f 73 65 28 29 20 74 68 65 20   to close() the 
aec00 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
aec10 69 73 20 64 65 66 65 72 72 65 64 20 75 6e 74 69  is deferred unti
aec20 6c 20 61 6c 6c 20 6f 66 20 74 68 65 20 6c 6f 63  l all of the loc
aec30 6b 73 20 63 6c 65 61 72 2e 0a 2a 2a 20 54 68 65  ks clear..** The
aec40 20 6f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75   openCnt structu
aec50 72 65 20 6b 65 65 70 73 20 61 20 6c 69 73 74 20  re keeps a list 
aec60 6f 66 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  of file descript
aec70 6f 72 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f  ors that need to
aec80 0a 2a 2a 20 62 65 20 63 6c 6f 73 65 64 20 61 6e  .** be closed an
aec90 64 20 74 68 61 74 20 6c 69 73 74 20 69 73 20 77  d that list is w
aeca0 61 6c 6b 65 64 20 28 61 6e 64 20 63 6c 65 61 72  alked (and clear
aecb0 65 64 29 20 77 68 65 6e 20 74 68 65 20 6c 61 73  ed) when the las
aecc0 74 20 6c 6f 63 6b 0a 2a 2a 20 63 6c 65 61 72 73  t lock.** clears
aecd0 2e 0a 2a 2a 0a 2a 2a 20 46 69 72 73 74 2c 20 75  ..**.** First, u
aece0 6e 64 65 72 20 4c 69 6e 75 78 20 74 68 72 65 61  nder Linux threa
aecf0 64 73 2c 20 62 65 63 61 75 73 65 20 65 61 63 68  ds, because each
aed00 20 74 68 72 65 61 64 20 68 61 73 20 61 20 73 65   thread has a se
aed10 70 61 72 61 74 65 0a 2a 2a 20 70 72 6f 63 65 73  parate.** proces
aed20 73 20 49 44 2c 20 6c 6f 63 6b 20 6f 70 65 72 61  s ID, lock opera
aed30 74 69 6f 6e 73 20 69 6e 20 6f 6e 65 20 74 68 72  tions in one thr
aed40 65 61 64 20 64 6f 20 6e 6f 74 20 6f 76 65 72 72  ead do not overr
aed50 69 64 65 20 6c 6f 63 6b 73 0a 2a 2a 20 74 6f 20  ide locks.** to 
aed60 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20 69 6e  the same file in
aed70 20 6f 74 68 65 72 20 74 68 72 65 61 64 73 2e 20   other threads. 
aed80 20 4c 69 6e 75 78 20 74 68 72 65 61 64 73 20 62   Linux threads b
aed90 65 68 61 76 65 20 6c 69 6b 65 0a 2a 2a 20 73 65  ehave like.** se
aeda0 70 61 72 61 74 65 20 70 72 6f 63 65 73 73 65 73  parate processes
aedb0 20 69 6e 20 74 68 69 73 20 72 65 73 70 65 63 74   in this respect
aedc0 2e 20 20 42 75 74 2c 20 69 66 20 79 6f 75 20 63  .  But, if you c
aedd0 6c 6f 73 65 20 61 20 66 69 6c 65 0a 2a 2a 20 64  lose a file.** d
aede0 65 73 63 72 69 70 74 6f 72 20 69 6e 20 6c 69 6e  escriptor in lin
aedf0 75 78 20 74 68 72 65 61 64 73 2c 20 61 6c 6c 20  ux threads, all 
aee00 6c 6f 63 6b 73 20 61 72 65 20 63 6c 65 61 72 65  locks are cleare
aee10 64 2c 20 65 76 65 6e 20 6c 6f 63 6b 73 0a 2a 2a  d, even locks.**
aee20 20 6f 6e 20 6f 74 68 65 72 20 74 68 72 65 61 64   on other thread
aee30 73 20 61 6e 64 20 65 76 65 6e 20 74 68 6f 75 67  s and even thoug
aee40 68 20 74 68 65 20 6f 74 68 65 72 20 74 68 72 65  h the other thre
aee50 61 64 73 20 68 61 76 65 20 64 69 66 66 65 72 65  ads have differe
aee60 6e 74 0a 2a 2a 20 70 72 6f 63 65 73 73 20 49 44  nt.** process ID
aee70 73 2e 20 20 4c 69 6e 75 78 20 74 68 72 65 61 64  s.  Linux thread
aee80 73 20 69 73 20 69 6e 63 6f 6e 73 69 73 74 65 6e  s is inconsisten
aee90 74 20 69 6e 20 74 68 69 73 20 72 65 73 70 65 63  t in this respec
aeea0 74 2e 0a 2a 2a 20 28 49 27 6d 20 62 65 67 69 6e  t..** (I'm begin
aeeb0 6e 69 6e 67 20 74 6f 20 74 68 69 6e 6b 20 74 68  ning to think th
aeec0 61 74 20 6c 69 6e 75 78 20 74 68 72 65 61 64 73  at linux threads
aeed0 20 69 73 20 61 6e 20 61 62 6f 6d 69 6e 61 74 69   is an abominati
aeee0 6f 6e 20 74 6f 6f 2e 29 0a 2a 2a 20 54 68 65 20  on too.).** The 
aeef0 63 6f 6e 73 65 71 75 65 6e 63 65 20 6f 66 20 74  consequence of t
aef00 68 69 73 20 61 6c 6c 20 69 73 20 74 68 61 74 20  his all is that 
aef10 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 66  the hash table f
aef20 6f 72 20 74 68 65 20 6c 6f 63 6b 49 6e 66 6f 0a  or the lockInfo.
aef30 2a 2a 20 73 74 72 75 63 74 75 72 65 20 68 61 73  ** structure has
aef40 20 74 6f 20 69 6e 63 6c 75 64 65 20 74 68 65 20   to include the 
aef50 70 72 6f 63 65 73 73 20 69 64 20 61 73 20 70 61  process id as pa
aef60 72 74 20 6f 66 20 69 74 73 20 6b 65 79 20 62 65  rt of its key be
aef70 63 61 75 73 65 0a 2a 2a 20 6c 6f 63 6b 73 20 69  cause.** locks i
aef80 6e 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65  n different thre
aef90 61 64 73 20 61 72 65 20 74 72 65 61 74 65 64 20  ads are treated 
aefa0 61 73 20 64 69 73 74 69 6e 63 74 2e 20 20 42 75  as distinct.  Bu
aefb0 74 20 74 68 65 20 0a 2a 2a 20 6f 70 65 6e 43 6e  t the .** openCn
aefc0 74 20 73 74 72 75 63 74 75 72 65 20 73 68 6f 75  t structure shou
aefd0 6c 64 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 74  ld not include t
aefe0 68 65 20 70 72 6f 63 65 73 73 20 69 64 20 69 6e  he process id in
aeff0 20 69 74 73 0a 2a 2a 20 6b 65 79 20 62 65 63 61   its.** key beca
af000 75 73 65 20 63 6c 6f 73 65 28 29 20 63 6c 65 61  use close() clea
af010 72 73 20 6c 6f 63 6b 20 6f 6e 20 61 6c 6c 20 74  rs lock on all t
af020 68 72 65 61 64 73 2c 20 6e 6f 74 20 6a 75 73 74  hreads, not just
af030 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20   the current.** 
af040 74 68 72 65 61 64 2e 20 20 57 65 72 65 20 69 74  thread.  Were it
af050 20 6e 6f 74 20 66 6f 72 20 74 68 69 73 20 67 6f   not for this go
af060 6f 66 69 6e 65 73 73 20 69 6e 20 6c 69 6e 75 78  ofiness in linux
af070 20 74 68 72 65 61 64 73 2c 20 77 65 20 63 6f 75   threads, we cou
af080 6c 64 0a 2a 2a 20 63 6f 6d 62 69 6e 65 20 74 68  ld.** combine th
af090 65 20 6c 6f 63 6b 49 6e 66 6f 20 61 6e 64 20 6f  e lockInfo and o
af0a0 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65  penCnt structure
af0b0 73 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20  s into a single 
af0c0 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a  structure..**.**
af0d0 20 32 30 30 34 2d 4a 75 6e 2d 32 38 3a 0a 2a 2a   2004-Jun-28:.**
af0e0 20 4f 6e 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e   On some version
af0f0 73 20 6f 66 20 6c 69 6e 75 78 2c 20 74 68 72 65  s of linux, thre
af100 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65  ads can override
af110 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63   each others loc
af120 6b 73 2e 0a 2a 2a 20 4f 6e 20 6f 74 68 65 72 73  ks..** On others
af130 20 6e 6f 74 2e 20 20 53 6f 6d 65 74 69 6d 65 73   not.  Sometimes
af140 20 79 6f 75 20 63 61 6e 20 63 68 61 6e 67 65 20   you can change 
af150 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f 6e 20  the behavior on 
af160 74 68 65 20 73 61 6d 65 0a 2a 2a 20 73 79 73 74  the same.** syst
af170 65 6d 20 62 79 20 73 65 74 74 69 6e 67 20 74 68  em by setting th
af180 65 20 4c 44 5f 41 53 53 55 4d 45 5f 4b 45 52 4e  e LD_ASSUME_KERN
af190 45 4c 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 76  EL environment v
af1a0 61 72 69 61 62 6c 65 2e 20 20 54 68 65 0a 2a 2a  ariable.  The.**
af1b0 20 50 4f 53 49 58 20 73 74 61 6e 64 61 72 64 20   POSIX standard 
af1c0 69 73 20 73 69 6c 65 6e 74 20 61 73 20 74 6f 20  is silent as to 
af1d0 77 68 69 63 68 20 62 65 68 61 76 69 6f 72 20 69  which behavior i
af1e0 73 20 63 6f 72 72 65 63 74 2c 20 61 73 20 66 61  s correct, as fa
af1f0 72 0a 2a 2a 20 61 73 20 49 20 63 61 6e 20 74 65  r.** as I can te
af200 6c 6c 2c 20 73 6f 20 6f 74 68 65 72 20 76 65 72  ll, so other ver
af210 73 69 6f 6e 73 20 6f 66 20 75 6e 69 78 20 6d 69  sions of unix mi
af220 67 68 74 20 73 68 6f 77 20 74 68 65 20 73 61 6d  ght show the sam
af230 65 0a 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e  e.** inconsisten
af240 63 79 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f  cy.  There is no
af250 20 6c 69 74 74 6c 65 20 64 6f 75 62 74 20 69 6e   little doubt in
af260 20 6d 79 20 6d 69 6e 64 20 74 68 61 74 20 70 6f   my mind that po
af270 73 69 78 0a 2a 2a 20 61 64 76 69 73 6f 72 79 20  six.** advisory 
af280 6c 6f 63 6b 73 20 61 6e 64 20 6c 69 6e 75 78 20  locks and linux 
af290 74 68 72 65 61 64 73 20 61 72 65 20 70 72 6f 66  threads are prof
af2a0 6f 75 6e 64 6c 79 20 62 72 6f 6b 65 6e 2e 0a 2a  oundly broken..*
af2b0 2a 0a 2a 2a 20 54 6f 20 77 6f 72 6b 20 61 72 6f  *.** To work aro
af2c0 75 6e 64 20 74 68 65 20 69 6e 63 6f 6e 73 69 73  und the inconsis
af2d0 74 65 6e 63 69 65 73 2c 20 77 65 20 68 61 76 65  tencies, we have
af2e0 20 74 6f 20 74 65 73 74 20 61 74 20 72 75 6e 74   to test at runt
af2f0 69 6d 65 20 0a 2a 2a 20 77 68 65 74 68 65 72 20  ime .** whether 
af300 6f 72 20 6e 6f 74 20 74 68 72 65 61 64 73 20 63  or not threads c
af310 61 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63 68  an override each
af320 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e 20 20   others locks.  
af330 54 68 69 73 20 74 65 73 74 0a 2a 2a 20 69 73 20  This test.** is 
af340 72 75 6e 20 6f 6e 63 65 2c 20 74 68 65 20 66 69  run once, the fi
af350 72 73 74 20 74 69 6d 65 20 61 6e 79 20 6c 6f 63  rst time any loc
af360 6b 20 69 73 20 61 74 74 65 6d 70 74 65 64 2e 20  k is attempted. 
af370 20 41 20 73 74 61 74 69 63 20 0a 2a 2a 20 76 61   A static .** va
af380 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f  riable is set to
af390 20 72 65 63 6f 72 64 20 74 68 65 20 72 65 73 75   record the resu
af3a0 6c 74 73 20 6f 66 20 74 68 69 73 20 74 65 73 74  lts of this test
af3b0 20 66 6f 72 20 66 75 74 75 72 65 0a 2a 2a 20 75   for future.** u
af3c0 73 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e  se..*/../*.** An
af3d0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
af3e0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
af3f0 74 75 72 65 20 73 65 72 76 65 73 20 61 73 20 74  ture serves as t
af400 68 65 20 6b 65 79 20 75 73 65 64 0a 2a 2a 20 74  he key used.** t
af410 6f 20 6c 6f 63 61 74 65 20 61 20 70 61 72 74 69  o locate a parti
af420 63 75 6c 61 72 20 6c 6f 63 6b 49 6e 66 6f 20 73  cular lockInfo s
af430 74 72 75 63 74 75 72 65 20 67 69 76 65 6e 20 69  tructure given i
af440 74 73 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  ts inode..**.** 
af450 49 66 20 74 68 72 65 61 64 73 20 63 61 6e 6e 6f  If threads canno
af460 74 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20  t override each 
af470 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2c 20 74 68  others locks, th
af480 65 6e 20 77 65 20 73 65 74 20 74 68 65 0a 2a 2a  en we set the.**
af490 20 6c 6f 63 6b 4b 65 79 2e 74 69 64 20 66 69 65   lockKey.tid fie
af4a0 6c 64 20 74 6f 20 74 68 65 20 74 68 72 65 61 64  ld to the thread
af4b0 20 49 44 2e 20 20 49 66 20 74 68 72 65 61 64 73   ID.  If threads
af4c0 20 63 61 6e 20 6f 76 65 72 72 69 64 65 0a 2a 2a   can override.**
af4d0 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63   each others loc
af4e0 6b 73 20 74 68 65 6e 20 74 69 64 20 69 73 20 61  ks then tid is a
af4f0 6c 77 61 79 73 20 73 65 74 20 74 6f 20 7a 65 72  lways set to zer
af500 6f 2e 20 20 74 69 64 20 69 73 20 6f 6d 69 74 74  o.  tid is omitt
af510 65 64 0a 2a 2a 20 69 66 20 77 65 20 63 6f 6d 70  ed.** if we comp
af520 69 6c 65 20 77 69 74 68 6f 75 74 20 74 68 72 65  ile without thre
af530 61 64 69 6e 67 20 73 75 70 70 6f 72 74 2e 0a 2a  ading support..*
af540 2f 0a 73 74 72 75 63 74 20 6c 6f 63 6b 4b 65 79  /.struct lockKey
af550 20 7b 0a 20 20 64 65 76 5f 74 20 64 65 76 3b 20   {.  dev_t dev; 
af560 20 20 20 20 20 20 2f 2a 20 44 65 76 69 63 65 20        /* Device 
af570 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 6f 5f  number */.  ino_
af580 74 20 69 6e 6f 3b 20 20 20 20 20 20 20 2f 2a 20  t ino;       /* 
af590 49 6e 6f 64 65 20 6e 75 6d 62 65 72 20 2a 2f 0a  Inode number */.
af5a0 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
af5b0 44 53 41 46 45 0a 20 20 70 74 68 72 65 61 64 5f  DSAFE.  pthread_
af5c0 74 20 74 69 64 3b 20 20 20 2f 2a 20 54 68 72 65  t tid;   /* Thre
af5d0 61 64 20 49 44 20 6f 72 20 7a 65 72 6f 20 69 66  ad ID or zero if
af5e0 20 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65   threads can ove
af5f0 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72  rride each other
af600 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f   */.#endif.};../
af610 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
af620 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
af630 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61  g structure is a
af640 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63  llocated for eac
af650 68 20 6f 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 20  h open.** inode 
af660 6f 6e 20 65 61 63 68 20 74 68 72 65 61 64 20 77  on each thread w
af670 69 74 68 20 61 20 64 69 66 66 65 72 65 6e 74 20  ith a different 
af680 70 72 6f 63 65 73 73 20 49 44 2e 20 20 28 54 68  process ID.  (Th
af690 72 65 61 64 73 20 68 61 76 65 0a 2a 2a 20 64 69  reads have.** di
af6a0 66 66 65 72 65 6e 74 20 70 72 6f 63 65 73 73 20  fferent process 
af6b0 49 44 73 20 6f 6e 20 6c 69 6e 75 78 2c 20 62 75  IDs on linux, bu
af6c0 74 20 6e 6f 74 20 6f 6e 20 6d 6f 73 74 20 6f 74  t not on most ot
af6d0 68 65 72 20 75 6e 69 78 65 73 2e 29 0a 2a 2a 0a  her unixes.).**.
af6e0 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e 6f 64  ** A single inod
af6f0 65 20 63 61 6e 20 68 61 76 65 20 6d 75 6c 74 69  e can have multi
af700 70 6c 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  ple file descrip
af710 74 6f 72 73 2c 20 73 6f 20 65 61 63 68 20 75 6e  tors, so each un
af720 69 78 46 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74  ixFile.** struct
af730 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70  ure contains a p
af740 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73  ointer to an ins
af750 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62  tance of this ob
af760 6a 65 63 74 20 61 6e 64 20 74 68 69 73 0a 2a 2a  ject and this.**
af770 20 6f 62 6a 65 63 74 20 6b 65 65 70 73 20 61 20   object keeps a 
af780 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d  count of the num
af790 62 65 72 20 6f 66 20 75 6e 69 78 46 69 6c 65 20  ber of unixFile 
af7a0 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69 74 2e 0a  pointing to it..
af7b0 2a 2f 0a 73 74 72 75 63 74 20 6c 6f 63 6b 49 6e  */.struct lockIn
af7c0 66 6f 20 7b 0a 20 20 73 74 72 75 63 74 20 6c 6f  fo {.  struct lo
af7d0 63 6b 4b 65 79 20 6b 65 79 3b 20 20 2f 2a 20 54  ckKey key;  /* T
af7e0 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f  he lookup key */
af7f0 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20  .  int cnt;     
af800 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
af810 72 20 6f 66 20 53 48 41 52 45 44 20 6c 6f 63 6b  r of SHARED lock
af820 73 20 68 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20  s held */.  int 
af830 6c 6f 63 6b 74 79 70 65 3b 20 20 20 20 20 20 20  locktype;       
af840 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 48 41 52 45   /* One of SHARE
af850 44 5f 4c 4f 43 4b 2c 20 52 45 53 45 52 56 45 44  D_LOCK, RESERVED
af860 5f 4c 4f 43 4b 20 65 74 63 2e 20 2a 2f 0a 20 20  _LOCK etc. */.  
af870 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20  int nRef;       
af880 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
af890 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68  f pointers to th
af8a0 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  is structure */.
af8b0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  };../*.** An ins
af8c0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
af8d0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
af8e0 20 73 65 72 76 65 73 20 61 73 20 74 68 65 20 6b   serves as the k
af8f0 65 79 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f  ey used.** to lo
af900 63 61 74 65 20 61 20 70 61 72 74 69 63 75 6c 61  cate a particula
af910 72 20 6f 70 65 6e 43 6e 74 20 73 74 72 75 63 74  r openCnt struct
af920 75 72 65 20 67 69 76 65 6e 20 69 74 73 20 69 6e  ure given its in
af930 6f 64 65 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73  ode.  This.** is
af940 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
af950 20 6c 6f 63 6b 4b 65 79 20 65 78 63 65 70 74 20   lockKey except 
af960 74 68 61 74 20 74 68 65 20 74 68 72 65 61 64 20  that the thread 
af970 49 44 20 69 73 20 6f 6d 69 74 74 65 64 2e 0a 2a  ID is omitted..*
af980 2f 0a 73 74 72 75 63 74 20 6f 70 65 6e 4b 65 79  /.struct openKey
af990 20 7b 0a 20 20 64 65 76 5f 74 20 64 65 76 3b 20   {.  dev_t dev; 
af9a0 20 20 2f 2a 20 44 65 76 69 63 65 20 6e 75 6d 62    /* Device numb
af9b0 65 72 20 2a 2f 0a 20 20 69 6e 6f 5f 74 20 69 6e  er */.  ino_t in
af9c0 6f 3b 20 20 20 2f 2a 20 49 6e 6f 64 65 20 6e 75  o;   /* Inode nu
af9d0 6d 62 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  mber */.};../*.*
af9e0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
af9f0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
afa00 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f  tructure is allo
afa10 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 6f  cated for each o
afa20 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 54  pen.** inode.  T
afa30 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6b 65  his structure ke
afa40 65 70 73 20 74 72 61 63 6b 20 6f 66 20 74 68 65  eps track of the
afa50 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73   number of locks
afa60 20 6f 6e 20 74 68 61 74 0a 2a 2a 20 69 6e 6f 64   on that.** inod
afa70 65 2e 20 20 49 66 20 61 20 63 6c 6f 73 65 20 69  e.  If a close i
afa80 73 20 61 74 74 65 6d 70 74 65 64 20 61 67 61 69  s attempted agai
afa90 6e 73 74 20 61 6e 20 69 6e 6f 64 65 20 74 68 61  nst an inode tha
afaa0 74 20 69 73 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20  t is holding.** 
afab0 6c 6f 63 6b 73 2c 20 74 68 65 20 63 6c 6f 73 65  locks, the close
afac0 20 69 73 20 64 65 66 65 72 72 65 64 20 75 6e 74   is deferred unt
afad0 69 6c 20 61 6c 6c 20 6c 6f 63 6b 73 20 63 6c 65  il all locks cle
afae0 61 72 20 62 79 20 61 64 64 69 6e 67 20 74 68 65  ar by adding the
afaf0 0a 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70  .** file descrip
afb00 74 6f 72 20 74 6f 20 62 65 20 63 6c 6f 73 65 64  tor to be closed
afb10 20 74 6f 20 74 68 65 20 70 65 6e 64 69 6e 67 20   to the pending 
afb20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  list..*/.struct 
afb30 6f 70 65 6e 43 6e 74 20 7b 0a 20 20 73 74 72 75  openCnt {.  stru
afb40 63 74 20 6f 70 65 6e 4b 65 79 20 6b 65 79 3b 20  ct openKey key; 
afb50 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b 75 70 20    /* The lookup 
afb60 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65  key */.  int nRe
afb70 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  f;             /
afb80 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e  * Number of poin
afb90 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 72  ters to this str
afba0 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74 20  ucture */.  int 
afbb0 6e 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20  nLock;          
afbc0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f    /* Number of o
afbd0 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73  utstanding locks
afbe0 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 65 6e 64 69   */.  int nPendi
afbf0 6e 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ng;         /* N
afc00 75 6d 62 65 72 20 6f 66 20 70 65 6e 64 69 6e 67  umber of pending
afc10 20 63 6c 6f 73 65 28 29 20 6f 70 65 72 61 74 69   close() operati
afc20 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 50  ons */.  int *aP
afc30 65 6e 64 69 6e 67 3b 20 20 20 20 20 20 20 20 2f  ending;        /
afc40 2a 20 4d 61 6c 6c 6f 63 65 64 20 73 70 61 63 65  * Malloced space
afc50 20 68 6f 6c 64 69 6e 67 20 66 64 27 73 20 61 77   holding fd's aw
afc60 61 69 74 69 6e 67 20 61 20 63 6c 6f 73 65 28 29  aiting a close()
afc70 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 0a 2a 2a 20 54   */.};../* .** T
afc80 68 65 73 65 20 68 61 73 68 20 74 61 62 6c 65 73  hese hash tables
afc90 20 6d 61 70 20 69 6e 6f 64 65 73 20 61 6e 64 20   map inodes and 
afca0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73  file descriptors
afcb0 20 28 72 65 61 6c 6c 79 2c 20 6c 6f 63 6b 4b 65   (really, lockKe
afcc0 79 20 61 6e 64 0a 2a 2a 20 6f 70 65 6e 4b 65 79  y and.** openKey
afcd0 20 73 74 72 75 63 74 75 72 65 73 29 20 69 6e 74   structures) int
afce0 6f 20 6c 6f 63 6b 49 6e 66 6f 20 61 6e 64 20 6f  o lockInfo and o
afcf0 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65  penCnt structure
afd00 73 2e 20 20 41 63 63 65 73 73 20 74 6f 20 0a 2a  s.  Access to .*
afd10 2a 20 74 68 65 73 65 20 68 61 73 68 20 74 61 62  * these hash tab
afd20 6c 65 73 20 6d 75 73 74 20 62 65 20 70 72 6f 74  les must be prot
afd30 65 63 74 65 64 20 62 79 20 61 20 6d 75 74 65 78  ected by a mutex
afd40 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 48 61 73 68  ..*/.static Hash
afd50 20 6c 6f 63 6b 48 61 73 68 20 3d 20 7b 53 51 4c   lockHash = {SQL
afd60 49 54 45 5f 48 41 53 48 5f 42 49 4e 41 52 59 2c  ITE_HASH_BINARY,
afd70 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 7d 3b   0, 0, 0, 0, 0};
afd80 0a 73 74 61 74 69 63 20 48 61 73 68 20 6f 70 65  .static Hash ope
afd90 6e 48 61 73 68 20 3d 20 7b 53 51 4c 49 54 45 5f  nHash = {SQLITE_
afda0 48 41 53 48 5f 42 49 4e 41 52 59 2c 20 30 2c 20  HASH_BINARY, 0, 
afdb0 30 2c 20 30 2c 20 30 2c 20 30 7d 3b 0a 0a 23 69  0, 0, 0, 0};..#i
afdc0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
afdd0 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
afde0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6c 6f 63 6b 69  ./*.** The locki
afdf0 6e 67 20 73 74 79 6c 65 73 20 61 72 65 20 61 73  ng styles are as
afe00 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
afe10 65 20 64 69 66 66 65 72 65 6e 74 20 66 69 6c 65  e different file
afe20 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 63 61 70 61   locking.** capa
afe30 62 69 6c 69 74 69 65 73 20 73 75 70 70 6f 72 74  bilities support
afe40 65 64 20 62 79 20 64 69 66 66 65 72 65 6e 74 20  ed by different 
afe50 66 69 6c 65 20 73 79 73 74 65 6d 73 2e 20 20 0a  file systems.  .
afe60 2a 2a 0a 2a 2a 20 50 4f 53 49 58 20 6c 6f 63 6b  **.** POSIX lock
afe70 69 6e 67 20 73 74 79 6c 65 20 66 75 6c 6c 79 20  ing style fully 
afe80 73 75 70 70 6f 72 74 73 20 73 68 61 72 65 64 20  supports shared 
afe90 61 6e 64 20 65 78 63 6c 75 73 69 76 65 20 62 79  and exclusive by
afea0 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 20 0a  te-range locks .
afeb0 2a 2a 20 41 44 50 20 6c 6f 63 6b 69 6e 67 20 6f  ** ADP locking o
afec0 6e 6c 79 20 73 75 70 70 6f 72 74 73 20 65 78 63  nly supports exc
afed0 6c 75 73 69 76 65 20 62 79 74 65 2d 72 61 6e 67  lusive byte-rang
afee0 65 20 6c 6f 63 6b 73 0a 2a 2a 20 46 4c 4f 43 4b  e locks.** FLOCK
afef0 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 73 20 61   only supports a
aff00 20 73 69 6e 67 6c 65 20 66 69 6c 65 2d 67 6c 6f   single file-glo
aff10 62 61 6c 20 65 78 63 6c 75 73 69 76 65 20 6c 6f  bal exclusive lo
aff20 63 6b 0a 2a 2a 20 44 4f 54 4c 4f 43 4b 20 69 73  ck.** DOTLOCK is
aff30 6e 27 74 20 61 20 74 72 75 65 20 6c 6f 63 6b 69  n't a true locki
aff40 6e 67 20 73 74 79 6c 65 2c 20 69 74 20 72 65 66  ng style, it ref
aff50 65 72 73 20 74 6f 20 74 68 65 20 75 73 65 20 6f  ers to the use o
aff60 66 20 61 20 73 70 65 63 69 61 6c 0a 2a 2a 20 20  f a special.**  
aff70 20 66 69 6c 65 20 6e 61 6d 65 64 20 74 68 65 20   file named the 
aff80 73 61 6d 65 20 61 73 20 74 68 65 20 64 61 74 61  same as the data
aff90 62 61 73 65 20 66 69 6c 65 20 77 69 74 68 20 61  base file with a
affa0 20 27 2e 6c 6f 63 6b 27 20 65 78 74 65 6e 73 69   '.lock' extensi
affb0 6f 6e 2c 20 74 68 69 73 0a 2a 2a 20 20 20 63 61  on, this.**   ca
affc0 6e 20 62 65 20 75 73 65 64 20 6f 6e 20 66 69 6c  n be used on fil
affd0 65 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 64  e systems that d
affe0 6f 20 6e 6f 74 20 6f 66 66 65 72 20 61 6e 79 20  o not offer any 
afff0 72 65 6c 69 61 62 6c 65 20 66 69 6c 65 20 6c 6f  reliable file lo
b0000 63 6b 69 6e 67 0a 2a 2a 20 4e 4f 20 6c 6f 63 6b  cking.** NO lock
b0010 69 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 20 6e  ing means that n
b0020 6f 20 6c 6f 63 6b 69 6e 67 20 77 69 6c 6c 20 62  o locking will b
b0030 65 20 61 74 74 65 6d 70 74 65 64 2c 20 74 68 69  e attempted, thi
b0040 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 66  s is only used f
b0050 6f 72 0a 2a 2a 20 20 20 72 65 61 64 2d 6f 6e 6c  or.**   read-onl
b0060 79 20 66 69 6c 65 20 73 79 73 74 65 6d 73 20 63  y file systems c
b0070 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 55 4e 53 55  urrently.** UNSU
b0080 50 50 4f 52 54 45 44 20 6d 65 61 6e 73 20 74 68  PPORTED means th
b0090 61 74 20 6e 6f 20 6c 6f 63 6b 69 6e 67 20 77 69  at no locking wi
b00a0 6c 6c 20 62 65 20 61 74 74 65 6d 70 74 65 64 2c  ll be attempted,
b00b0 20 74 68 69 73 20 69 73 20 6f 6e 6c 79 20 75 73   this is only us
b00c0 65 64 20 66 6f 72 0a 2a 2a 20 20 20 66 69 6c 65  ed for.**   file
b00d0 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 61 72   systems that ar
b00e0 65 20 6b 6e 6f 77 6e 20 74 6f 20 62 65 20 75 6e  e known to be un
b00f0 73 75 70 70 6f 72 74 65 64 0a 2a 2f 0a 74 79 70  supported.*/.typ
b0100 65 64 65 66 20 65 6e 75 6d 20 7b 0a 20 20 70 6f  edef enum {.  po
b0110 73 69 78 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20  sixLockingStyle 
b0120 3d 20 30 2c 20 20 20 20 20 20 20 2f 2a 20 73 74  = 0,       /* st
b0130 61 6e 64 61 72 64 20 70 6f 73 69 78 2d 61 64 76  andard posix-adv
b0140 69 73 6f 72 79 20 6c 6f 63 6b 73 20 2a 2f 0a 20  isory locks */. 
b0150 20 61 66 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65   afpLockingStyle
b0160 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
b0170 20 75 73 65 20 61 66 70 20 6c 6f 63 6b 73 20 2a   use afp locks *
b0180 2f 0a 20 20 66 6c 6f 63 6b 4c 6f 63 6b 69 6e 67  /.  flockLocking
b0190 53 74 79 6c 65 2c 20 20 20 20 20 20 20 20 20 20  Style,          
b01a0 20 2f 2a 20 75 73 65 20 66 6c 6f 63 6b 28 29 20   /* use flock() 
b01b0 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b  */.  dotlockLock
b01c0 69 6e 67 53 74 79 6c 65 2c 20 20 20 20 20 20 20  ingStyle,       
b01d0 20 20 2f 2a 20 75 73 65 20 3c 66 69 6c 65 3e 2e    /* use <file>.
b01e0 6c 6f 63 6b 20 66 69 6c 65 73 20 2a 2f 0a 20 20  lock files */.  
b01f0 6e 6f 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 2c 20  noLockingStyle, 
b0200 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
b0210 75 73 65 66 75 6c 20 66 6f 72 20 72 65 61 64 2d  useful for read-
b0220 6f 6e 6c 79 20 66 69 6c 65 20 73 79 73 74 65 6d  only file system
b0230 20 2a 2f 0a 20 20 75 6e 73 75 70 70 6f 72 74 65   */.  unsupporte
b0240 64 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 20 20  dLockingStyle   
b0250 20 20 20 2f 2a 20 69 6e 64 69 63 61 74 65 73 20     /* indicates 
b0260 75 6e 73 75 70 70 6f 72 74 65 64 20 66 69 6c 65  unsupported file
b0270 20 73 79 73 74 65 6d 20 2a 2f 0a 7d 20 73 71 6c   system */.} sql
b0280 69 74 65 33 4c 6f 63 6b 69 6e 67 53 74 79 6c 65  ite3LockingStyle
b0290 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ;.#endif /* SQLI
b02a0 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
b02b0 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a  G_STYLE */../*.*
b02c0 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f  * Helper functio
b02d0 6e 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e 64  ns to obtain and
b02e0 20 72 65 6c 69 6e 71 75 69 73 68 20 74 68 65 20   relinquish the 
b02f0 67 6c 6f 62 61 6c 20 6d 75 74 65 78 2e 0a 2a 2f  global mutex..*/
b0300 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 6e 74  .static void ent
b0310 65 72 4d 75 74 65 78 28 29 7b 0a 20 20 73 71 6c  erMutex(){.  sql
b0320 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
b0330 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61  (sqlite3_mutex_a
b0340 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
b0350 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
b0360 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  );.}.static void
b0370 20 6c 65 61 76 65 4d 75 74 65 78 28 29 7b 0a 20   leaveMutex(){. 
b0380 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
b0390 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  eave(sqlite3_mut
b03a0 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  ex_alloc(SQLITE_
b03b0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
b03c0 54 45 52 29 29 3b 0a 7d 0a 0a 23 69 66 20 53 51  TER));.}..#if SQ
b03d0 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a  LITE_THREADSAFE.
b03e0 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 61 72 69 61  /*.** This varia
b03f0 62 6c 65 20 72 65 63 6f 72 64 73 20 77 68 65 74  ble records whet
b0400 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61  her or not threa
b0410 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20  ds can override 
b0420 65 61 63 68 20 6f 74 68 65 72 73 0a 2a 2a 20 6c  each others.** l
b0430 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30  ocks..**.**    0
b0440 3a 20 20 4e 6f 2e 20 20 54 68 72 65 61 64 73 20  :  No.  Threads 
b0450 63 61 6e 6e 6f 74 20 6f 76 65 72 72 69 64 65 20  cannot override 
b0460 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b  each others lock
b0470 73 2e 0a 2a 2a 20 20 20 20 31 3a 20 20 59 65 73  s..**    1:  Yes
b0480 2e 20 20 54 68 72 65 61 64 73 20 63 61 6e 20 6f  .  Threads can o
b0490 76 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 68  verride each oth
b04a0 65 72 73 20 6c 6f 63 6b 73 2e 0a 2a 2a 20 20 20  ers locks..**   
b04b0 2d 31 3a 20 20 57 65 20 64 6f 6e 27 74 20 6b 6e  -1:  We don't kn
b04c0 6f 77 20 79 65 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  ow yet..**.** On
b04d0 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c 20 77   some systems, w
b04e0 65 20 6b 6e 6f 77 20 61 74 20 63 6f 6d 70 69 6c  e know at compil
b04f0 65 2d 74 69 6d 65 20 69 66 20 74 68 72 65 61 64  e-time if thread
b0500 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65  s can override e
b0510 61 63 68 0a 2a 2a 20 6f 74 68 65 72 73 20 6c 6f  ach.** others lo
b0520 63 6b 73 2e 20 20 4f 6e 20 74 68 6f 73 65 20 73  cks.  On those s
b0530 79 73 74 65 6d 73 2c 20 74 68 65 20 53 51 4c 49  ystems, the SQLI
b0540 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49  TE_THREAD_OVERRI
b0550 44 45 5f 4c 4f 43 4b 20 6d 61 63 72 6f 0a 2a 2a  DE_LOCK macro.**
b0560 20 77 69 6c 6c 20 62 65 20 73 65 74 20 61 70 70   will be set app
b0570 72 6f 70 72 69 61 74 65 6c 79 2e 20 20 4f 6e 20  ropriately.  On 
b0580 6f 74 68 65 72 20 73 79 73 74 65 6d 73 2c 20 77  other systems, w
b0590 65 20 68 61 76 65 20 74 6f 20 63 68 65 63 6b 20  e have to check 
b05a0 61 74 0a 2a 2a 20 72 75 6e 74 69 6d 65 2e 20 20  at.** runtime.  
b05b0 4f 6e 20 74 68 65 73 65 20 6c 61 74 74 65 72 20  On these latter 
b05c0 73 79 73 74 65 6d 73 2c 20 53 51 4c 54 49 45 5f  systems, SQLTIE_
b05d0 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f  THREAD_OVERRIDE_
b05e0 4c 4f 43 4b 20 69 73 0a 2a 2a 20 75 6e 64 65 66  LOCK is.** undef
b05f0 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ined..**.** This
b0600 20 76 61 72 69 61 62 6c 65 20 6e 6f 72 6d 61 6c   variable normal
b0610 6c 79 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70  ly has file scop
b0620 65 20 6f 6e 6c 79 2e 20 20 42 75 74 20 64 75 72  e only.  But dur
b0630 69 6e 67 20 74 65 73 74 69 6e 67 2c 20 77 65 20  ing testing, we 
b0640 6d 61 6b 65 0a 2a 2a 20 69 74 20 61 20 67 6c 6f  make.** it a glo
b0650 62 61 6c 20 73 6f 20 74 68 61 74 20 74 68 65 20  bal so that the 
b0660 74 65 73 74 20 63 6f 64 65 20 63 61 6e 20 63 68  test code can ch
b0670 61 6e 67 65 20 69 74 73 20 76 61 6c 75 65 20 69  ange its value i
b0680 6e 20 6f 72 64 65 72 20 74 6f 20 76 65 72 69 66  n order to verif
b0690 79 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 72 69  y.** that the ri
b06a0 67 68 74 20 73 74 75 66 66 20 68 61 70 70 65 6e  ght stuff happen
b06b0 73 20 69 6e 20 65 69 74 68 65 72 20 63 61 73 65  s in either case
b06c0 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
b06d0 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52  ITE_THREAD_OVERR
b06e0 49 44 45 5f 4c 4f 43 4b 0a 23 20 64 65 66 69 6e  IDE_LOCK.# defin
b06f0 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f  e SQLITE_THREAD_
b0700 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 20 2d 31  OVERRIDE_LOCK -1
b0710 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
b0720 51 4c 49 54 45 5f 54 45 53 54 0a 69 6e 74 20 74  QLITE_TEST.int t
b0730 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 61  hreadsOverrideEa
b0740 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 3d 20  chOthersLocks = 
b0750 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56  SQLITE_THREAD_OV
b0760 45 52 52 49 44 45 5f 4c 4f 43 4b 3b 0a 23 65 6c  ERRIDE_LOCK;.#el
b0770 73 65 0a 73 74 61 74 69 63 20 69 6e 74 20 74 68  se.static int th
b0780 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63  readsOverrideEac
b0790 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 3d 20 53  hOthersLocks = S
b07a0 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45  QLITE_THREAD_OVE
b07b0 52 52 49 44 45 5f 4c 4f 43 4b 3b 0a 23 65 6e 64  RRIDE_LOCK;.#end
b07c0 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73  if../*.** This s
b07d0 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69  tructure holds i
b07e0 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73 73 65  nformation passe
b07f0 64 20 69 6e 74 6f 20 69 6e 64 69 76 69 64 75 61  d into individua
b0800 6c 20 74 65 73 74 0a 2a 2a 20 74 68 72 65 61 64  l test.** thread
b0810 73 20 62 79 20 74 68 65 20 74 65 73 74 54 68 72  s by the testThr
b0820 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69  eadLockingBehavi
b0830 6f 72 28 29 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f  or() routine..*/
b0840 0a 73 74 72 75 63 74 20 74 68 72 65 61 64 54 65  .struct threadTe
b0850 73 74 44 61 74 61 20 7b 0a 20 20 69 6e 74 20 66  stData {.  int f
b0860 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
b0870 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 62 65 20    /* File to be 
b0880 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 73 74 72 75  locked */.  stru
b0890 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 20 20  ct flock lock;  
b08a0 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 6b 69 6e     /* The lockin
b08b0 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20  g operation */. 
b08c0 20 69 6e 74 20 72 65 73 75 6c 74 3b 20 20 20 20   int result;    
b08d0 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c          /* Resul
b08e0 74 20 6f 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67  t of the locking
b08f0 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 7d 3b   operation */.};
b0900 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
b0910 4c 4f 43 4b 5f 54 52 41 43 45 0a 2f 2a 0a 2a 2a  LOCK_TRACE./*.**
b0920 20 50 72 69 6e 74 20 6f 75 74 20 69 6e 66 6f 72   Print out infor
b0930 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 6c 6c  mation about all
b0940 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69   locking operati
b0950 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ons..**.** This 
b0960 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
b0970 66 6f 72 20 74 72 6f 75 62 6c 65 73 68 6f 6f 74  for troubleshoot
b0980 69 6e 67 20 6c 6f 63 6b 73 20 6f 6e 20 6d 75 6c  ing locks on mul
b0990 74 69 74 68 72 65 61 64 65 64 0a 2a 2a 20 70 6c  tithreaded.** pl
b09a0 61 74 66 6f 72 6d 73 2e 20 20 45 6e 61 62 6c 65  atforms.  Enable
b09b0 20 62 79 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69   by compiling wi
b09c0 74 68 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f  th the -DSQLITE_
b09d0 4c 4f 43 4b 5f 54 52 41 43 45 0a 2a 2a 20 63 6f  LOCK_TRACE.** co
b09e0 6d 6d 61 6e 64 2d 6c 69 6e 65 20 6f 70 74 69 6f  mmand-line optio
b09f0 6e 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65  n on the compile
b0a00 72 2e 20 20 54 68 69 73 20 63 6f 64 65 20 69 73  r.  This code is
b0a10 20 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a 20 74 75 72   normally.** tur
b0a20 6e 65 64 20 6f 66 66 2e 0a 2a 2f 0a 73 74 61 74  ned off..*/.stat
b0a30 69 63 20 69 6e 74 20 6c 6f 63 6b 54 72 61 63 65  ic int lockTrace
b0a40 28 69 6e 74 20 66 64 2c 20 69 6e 74 20 6f 70 2c  (int fd, int op,
b0a50 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 2a 70   struct flock *p
b0a60 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4f 70 4e 61  ){.  char *zOpNa
b0a70 6d 65 2c 20 2a 7a 54 79 70 65 3b 0a 20 20 69 6e  me, *zType;.  in
b0a80 74 20 73 3b 0a 20 20 69 6e 74 20 73 61 76 65 64  t s;.  int saved
b0a90 45 72 72 6e 6f 3b 0a 20 20 69 66 28 20 6f 70 3d  Errno;.  if( op=
b0aa0 3d 46 5f 47 45 54 4c 4b 20 29 7b 0a 20 20 20 20  =F_GETLK ){.    
b0ab0 7a 4f 70 4e 61 6d 65 20 3d 20 22 47 45 54 4c 4b  zOpName = "GETLK
b0ac0 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f  ";.  }else if( o
b0ad0 70 3d 3d 46 5f 53 45 54 4c 4b 20 29 7b 0a 20 20  p==F_SETLK ){.  
b0ae0 20 20 7a 4f 70 4e 61 6d 65 20 3d 20 22 53 45 54    zOpName = "SET
b0af0 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  LK";.  }else{.  
b0b00 20 20 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20    s = fcntl(fd, 
b0b10 6f 70 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c 69  op, p);.    sqli
b0b20 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
b0b30 66 63 6e 74 6c 20 75 6e 6b 6e 6f 77 6e 20 25 64  fcntl unknown %d
b0b40 20 25 64 20 25 64 5c 6e 22 2c 20 66 64 2c 20 6f   %d %d\n", fd, o
b0b50 70 2c 20 73 29 3b 0a 20 20 20 20 72 65 74 75 72  p, s);.    retur
b0b60 6e 20 73 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  n s;.  }.  if( p
b0b70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43  ->l_type==F_RDLC
b0b80 4b 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d  K ){.    zType =
b0b90 20 22 52 44 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73   "RDLCK";.  }els
b0ba0 65 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d  e if( p->l_type=
b0bb0 3d 46 5f 57 52 4c 43 4b 20 29 7b 0a 20 20 20 20  =F_WRLCK ){.    
b0bc0 7a 54 79 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b  zType = "WRLCK";
b0bd0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e  .  }else if( p->
b0be0 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20  l_type==F_UNLCK 
b0bf0 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22  ){.    zType = "
b0c00 55 4e 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b  UNLCK";.  }else{
b0c10 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29  .    assert( 0 )
b0c20 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
b0c30 70 2d 3e 6c 5f 77 68 65 6e 63 65 3d 3d 53 45 45  p->l_whence==SEE
b0c40 4b 5f 53 45 54 20 29 3b 0a 20 20 73 20 3d 20 66  K_SET );.  s = f
b0c50 63 6e 74 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b  cntl(fd, op, p);
b0c60 0a 20 20 73 61 76 65 64 45 72 72 6e 6f 20 3d 20  .  savedErrno = 
b0c70 65 72 72 6e 6f 3b 0a 20 20 73 71 6c 69 74 65 33  errno;.  sqlite3
b0c80 44 65 62 75 67 50 72 69 6e 74 66 28 22 66 63 6e  DebugPrintf("fcn
b0c90 74 6c 20 25 64 20 25 64 20 25 73 20 25 73 20 25  tl %d %d %s %s %
b0ca0 64 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 0a 20  d %d %d %d\n",. 
b0cb0 20 20 20 20 74 68 72 65 61 64 69 64 2c 20 66 64      threadid, fd
b0cc0 2c 20 7a 4f 70 4e 61 6d 65 2c 20 7a 54 79 70 65  , zOpName, zType
b0cd0 2c 20 28 69 6e 74 29 70 2d 3e 6c 5f 73 74 61 72  , (int)p->l_star
b0ce0 74 2c 20 28 69 6e 74 29 70 2d 3e 6c 5f 6c 65 6e  t, (int)p->l_len
b0cf0 2c 0a 20 20 20 20 20 28 69 6e 74 29 70 2d 3e 6c  ,.     (int)p->l
b0d00 5f 70 69 64 2c 20 73 29 3b 0a 20 20 69 66 28 20  _pid, s);.  if( 
b0d10 73 3d 3d 28 2d 31 29 20 26 26 20 6f 70 3d 3d 46  s==(-1) && op==F
b0d20 5f 53 45 54 4c 4b 20 26 26 20 28 70 2d 3e 6c 5f  _SETLK && (p->l_
b0d30 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b 20 7c 7c  type==F_RDLCK ||
b0d40 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52   p->l_type==F_WR
b0d50 4c 43 4b 29 20 29 7b 0a 20 20 20 20 73 74 72 75  LCK) ){.    stru
b0d60 63 74 20 66 6c 6f 63 6b 20 6c 32 3b 0a 20 20 20  ct flock l2;.   
b0d70 20 6c 32 20 3d 20 2a 70 3b 0a 20 20 20 20 66 63   l2 = *p;.    fc
b0d80 6e 74 6c 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c  ntl(fd, F_GETLK,
b0d90 20 26 6c 32 29 3b 0a 20 20 20 20 69 66 28 20 6c   &l2);.    if( l
b0da0 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43  2.l_type==F_RDLC
b0db0 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54 79 70 65  K ){.      zType
b0dc0 20 3d 20 22 52 44 4c 43 4b 22 3b 0a 20 20 20 20   = "RDLCK";.    
b0dd0 7d 65 6c 73 65 20 69 66 28 20 6c 32 2e 6c 5f 74  }else if( l2.l_t
b0de0 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b 0a  ype==F_WRLCK ){.
b0df0 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 57        zType = "W
b0e00 52 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65  RLCK";.    }else
b0e10 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d   if( l2.l_type==
b0e20 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 20  F_UNLCK ){.     
b0e30 20 7a 54 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22   zType = "UNLCK"
b0e40 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
b0e50 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a     assert( 0 );.
b0e60 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
b0e70 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 66 63  3DebugPrintf("fc
b0e80 6e 74 6c 2d 66 61 69 6c 75 72 65 2d 72 65 61 73  ntl-failure-reas
b0e90 6f 6e 3a 20 25 73 20 25 64 20 25 64 20 25 64 5c  on: %s %d %d %d\
b0ea0 6e 22 2c 0a 20 20 20 20 20 20 20 7a 54 79 70 65  n",.       zType
b0eb0 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 73 74 61 72  , (int)l2.l_star
b0ec0 74 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 6c 65 6e  t, (int)l2.l_len
b0ed0 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 70 69 64 29  , (int)l2.l_pid)
b0ee0 3b 0a 20 20 7d 0a 20 20 65 72 72 6e 6f 20 3d 20  ;.  }.  errno = 
b0ef0 73 61 76 65 64 45 72 72 6e 6f 3b 0a 20 20 72 65  savedErrno;.  re
b0f00 74 75 72 6e 20 73 3b 0a 7d 0a 23 64 65 66 69 6e  turn s;.}.#defin
b0f10 65 20 66 63 6e 74 6c 20 6c 6f 63 6b 54 72 61 63  e fcntl lockTrac
b0f20 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  e.#endif /* SQLI
b0f30 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 20 2a 2f  TE_LOCK_TRACE */
b0f40 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 65 73 74  ../*.** The test
b0f50 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68  ThreadLockingBeh
b0f60 61 76 69 6f 72 28 29 20 72 6f 75 74 69 6e 65 20  avior() routine 
b0f70 6c 61 75 6e 63 68 65 73 20 74 77 6f 20 73 65 70  launches two sep
b0f80 61 72 61 74 65 0a 2a 2a 20 74 68 72 65 61 64 73  arate.** threads
b0f90 20 6f 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65   on this routine
b0fa0 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
b0fb0 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 6f 63 6b  attempts to lock
b0fc0 20 61 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72   a file.** descr
b0fd0 69 70 74 6f 72 20 74 68 65 6e 20 72 65 74 75 72  iptor then retur
b0fe0 6e 73 2e 20 20 54 68 65 20 73 75 63 63 65 73 73  ns.  The success
b0ff0 20 6f 72 20 66 61 69 6c 75 72 65 20 6f 66 20 74   or failure of t
b1000 68 61 74 20 61 74 74 65 6d 70 74 0a 2a 2a 20 61  hat attempt.** a
b1010 6c 6c 6f 77 73 20 74 68 65 20 74 65 73 74 54 68  llows the testTh
b1020 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76  readLockingBehav
b1030 69 6f 72 28 29 20 70 72 6f 63 65 64 75 72 65 20  ior() procedure 
b1040 74 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20  to determine.** 
b1050 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
b1060 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72 72  hreads can overr
b1070 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 20  ide each others 
b1080 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  locks..*/.static
b1090 20 76 6f 69 64 20 2a 74 68 72 65 61 64 4c 6f 63   void *threadLoc
b10a0 6b 69 6e 67 54 65 73 74 28 76 6f 69 64 20 2a 70  kingTest(void *p
b10b0 41 72 67 29 7b 0a 20 20 73 74 72 75 63 74 20 74  Arg){.  struct t
b10c0 68 72 65 61 64 54 65 73 74 44 61 74 61 20 2a 70  hreadTestData *p
b10d0 44 61 74 61 20 3d 20 28 73 74 72 75 63 74 20 74  Data = (struct t
b10e0 68 72 65 61 64 54 65 73 74 44 61 74 61 2a 29 70  hreadTestData*)p
b10f0 41 72 67 3b 0a 20 20 70 44 61 74 61 2d 3e 72 65  Arg;.  pData->re
b1100 73 75 6c 74 20 3d 20 66 63 6e 74 6c 28 70 44 61  sult = fcntl(pDa
b1110 74 61 2d 3e 66 64 2c 20 46 5f 53 45 54 4c 4b 2c  ta->fd, F_SETLK,
b1120 20 26 70 44 61 74 61 2d 3e 6c 6f 63 6b 29 3b 0a   &pData->lock);.
b1130 20 20 72 65 74 75 72 6e 20 70 41 72 67 3b 0a 7d    return pArg;.}
b1140 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 72 6f  ../*.** This pro
b1150 63 65 64 75 72 65 20 61 74 74 65 6d 70 74 73 20  cedure attempts 
b1160 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65  to determine whe
b1170 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65  ther or not thre
b1180 61 64 73 0a 2a 2a 20 63 61 6e 20 6f 76 65 72 72  ads.** can overr
b1190 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 20  ide each others 
b11a0 6c 6f 63 6b 73 20 74 68 65 6e 20 73 65 74 73 20  locks then sets 
b11b0 74 68 65 20 0a 2a 2a 20 74 68 72 65 61 64 73 4f  the .** threadsO
b11c0 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72  verrideEachOther
b11d0 73 4c 6f 63 6b 73 20 76 61 72 69 61 62 6c 65 20  sLocks variable 
b11e0 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a  appropriately..*
b11f0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 65  /.static void te
b1200 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42  stThreadLockingB
b1210 65 68 61 76 69 6f 72 28 69 6e 74 20 66 64 5f 6f  ehavior(int fd_o
b1220 72 69 67 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a  rig){.  int fd;.
b1230 20 20 73 74 72 75 63 74 20 74 68 72 65 61 64 54    struct threadT
b1240 65 73 74 44 61 74 61 20 64 5b 32 5d 3b 0a 20 20  estData d[2];.  
b1250 70 74 68 72 65 61 64 5f 74 20 74 5b 32 5d 3b 0a  pthread_t t[2];.
b1260 0a 20 20 66 64 20 3d 20 64 75 70 28 66 64 5f 6f  .  fd = dup(fd_o
b1270 72 69 67 29 3b 0a 20 20 69 66 28 20 66 64 3c 30  rig);.  if( fd<0
b1280 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d   ) return;.  mem
b1290 73 65 74 28 64 2c 20 30 2c 20 73 69 7a 65 6f 66  set(d, 0, sizeof
b12a0 28 64 29 29 3b 0a 20 20 64 5b 30 5d 2e 66 64 20  (d));.  d[0].fd 
b12b0 3d 20 66 64 3b 0a 20 20 64 5b 30 5d 2e 6c 6f 63  = fd;.  d[0].loc
b12c0 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c  k.l_type = F_RDL
b12d0 43 4b 3b 0a 20 20 64 5b 30 5d 2e 6c 6f 63 6b 2e  CK;.  d[0].lock.
b12e0 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 64 5b 30  l_len = 1;.  d[0
b12f0 5d 2e 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d  ].lock.l_start =
b1300 20 30 3b 0a 20 20 64 5b 30 5d 2e 6c 6f 63 6b 2e   0;.  d[0].lock.
b1310 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f  l_whence = SEEK_
b1320 53 45 54 3b 0a 20 20 64 5b 31 5d 20 3d 20 64 5b  SET;.  d[1] = d[
b1330 30 5d 3b 0a 20 20 64 5b 31 5d 2e 6c 6f 63 6b 2e  0];.  d[1].lock.
b1340 6c 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b  l_type = F_WRLCK
b1350 3b 0a 20 20 70 74 68 72 65 61 64 5f 63 72 65 61  ;.  pthread_crea
b1360 74 65 28 26 74 5b 30 5d 2c 20 30 2c 20 74 68 72  te(&t[0], 0, thr
b1370 65 61 64 4c 6f 63 6b 69 6e 67 54 65 73 74 2c 20  eadLockingTest, 
b1380 26 64 5b 30 5d 29 3b 0a 20 20 70 74 68 72 65 61  &d[0]);.  pthrea
b1390 64 5f 63 72 65 61 74 65 28 26 74 5b 31 5d 2c 20  d_create(&t[1], 
b13a0 30 2c 20 74 68 72 65 61 64 4c 6f 63 6b 69 6e 67  0, threadLocking
b13b0 54 65 73 74 2c 20 26 64 5b 31 5d 29 3b 0a 20 20  Test, &d[1]);.  
b13c0 70 74 68 72 65 61 64 5f 6a 6f 69 6e 28 74 5b 30  pthread_join(t[0
b13d0 5d 2c 20 30 29 3b 0a 20 20 70 74 68 72 65 61 64  ], 0);.  pthread
b13e0 5f 6a 6f 69 6e 28 74 5b 31 5d 2c 20 30 29 3b 0a  _join(t[1], 0);.
b13f0 20 20 63 6c 6f 73 65 28 66 64 29 3b 0a 20 20 74    close(fd);.  t
b1400 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 61  hreadsOverrideEa
b1410 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 3d 20  chOthersLocks = 
b1420 20 64 5b 30 5d 2e 72 65 73 75 6c 74 3d 3d 30 20   d[0].result==0 
b1430 26 26 20 64 5b 31 5d 2e 72 65 73 75 6c 74 3d 3d  && d[1].result==
b1440 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  0;.}.#endif /* S
b1450 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
b1460 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61   */../*.** Relea
b1470 73 65 20 61 20 6c 6f 63 6b 49 6e 66 6f 20 73 74  se a lockInfo st
b1480 72 75 63 74 75 72 65 20 70 72 65 76 69 6f 75 73  ructure previous
b1490 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  ly allocated by 
b14a0 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e 0a  findLockInfo()..
b14b0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  */.static void r
b14c0 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 73  eleaseLockInfo(s
b14d0 74 72 75 63 74 20 6c 6f 63 6b 49 6e 66 6f 20 2a  truct lockInfo *
b14e0 70 4c 6f 63 6b 29 7b 0a 20 20 69 66 20 28 70 4c  pLock){.  if (pL
b14f0 6f 63 6b 20 3d 3d 20 4e 55 4c 4c 29 0a 20 20 20  ock == NULL).   
b1500 20 72 65 74 75 72 6e 3b 0a 20 20 70 4c 6f 63 6b   return;.  pLock
b1510 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20  ->nRef--;.  if( 
b1520 70 4c 6f 63 6b 2d 3e 6e 52 65 66 3d 3d 30 20 29  pLock->nRef==0 )
b1530 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61 73  {.    sqlite3Has
b1540 68 49 6e 73 65 72 74 28 26 6c 6f 63 6b 48 61 73  hInsert(&lockHas
b1550 68 2c 20 26 70 4c 6f 63 6b 2d 3e 6b 65 79 2c 20  h, &pLock->key, 
b1560 73 69 7a 65 6f 66 28 70 4c 6f 63 6b 2d 3e 6b 65  sizeof(pLock->ke
b1570 79 29 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69  y), 0);.    sqli
b1580 74 65 33 5f 66 72 65 65 28 70 4c 6f 63 6b 29 3b  te3_free(pLock);
b1590 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
b15a0 6c 65 61 73 65 20 61 20 6f 70 65 6e 43 6e 74 20  lease a openCnt 
b15b0 73 74 72 75 63 74 75 72 65 20 70 72 65 76 69 6f  structure previo
b15c0 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 62  usly allocated b
b15d0 79 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29  y findLockInfo()
b15e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
b15f0 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28   releaseOpenCnt(
b1600 73 74 72 75 63 74 20 6f 70 65 6e 43 6e 74 20 2a  struct openCnt *
b1610 70 4f 70 65 6e 29 7b 0a 20 20 69 66 20 28 70 4f  pOpen){.  if (pO
b1620 70 65 6e 20 3d 3d 20 4e 55 4c 4c 29 0a 20 20 20  pen == NULL).   
b1630 20 72 65 74 75 72 6e 3b 0a 20 20 70 4f 70 65 6e   return;.  pOpen
b1640 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20  ->nRef--;.  if( 
b1650 70 4f 70 65 6e 2d 3e 6e 52 65 66 3d 3d 30 20 29  pOpen->nRef==0 )
b1660 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61 73  {.    sqlite3Has
b1670 68 49 6e 73 65 72 74 28 26 6f 70 65 6e 48 61 73  hInsert(&openHas
b1680 68 2c 20 26 70 4f 70 65 6e 2d 3e 6b 65 79 2c 20  h, &pOpen->key, 
b1690 73 69 7a 65 6f 66 28 70 4f 70 65 6e 2d 3e 6b 65  sizeof(pOpen->ke
b16a0 79 29 2c 20 30 29 3b 0a 20 20 20 20 66 72 65 65  y), 0);.    free
b16b0 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67  (pOpen->aPending
b16c0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
b16d0 72 65 65 28 70 4f 70 65 6e 29 3b 0a 20 20 7d 0a  ree(pOpen);.  }.
b16e0 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
b16f0 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
b1700 53 54 59 4c 45 0a 2f 2a 0a 2a 2a 20 54 65 73 74  STYLE./*.** Test
b1710 73 20 61 20 62 79 74 65 2d 72 61 6e 67 65 20 6c  s a byte-range l
b1720 6f 63 6b 69 6e 67 20 71 75 65 72 79 20 74 6f 20  ocking query to 
b1730 73 65 65 20 69 66 20 62 79 74 65 20 72 61 6e 67  see if byte rang
b1740 65 20 6c 6f 63 6b 73 20 61 72 65 20 0a 2a 2a 20  e locks are .** 
b1750 73 75 70 70 6f 72 74 65 64 2c 20 69 66 20 6e 6f  supported, if no
b1760 74 20 77 65 20 66 61 6c 6c 20 62 61 63 6b 20 74  t we fall back t
b1770 6f 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67  o dotlockLocking
b1780 53 74 79 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  Style..*/.static
b1790 20 73 71 6c 69 74 65 33 4c 6f 63 6b 69 6e 67 53   sqlite3LockingS
b17a0 74 79 6c 65 20 73 71 6c 69 74 65 33 54 65 73 74  tyle sqlite3Test
b17b0 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 28 0a 20 20  LockingStyle(.  
b17c0 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65  const char *file
b17d0 50 61 74 68 2c 20 0a 20 20 69 6e 74 20 66 64 0a  Path, .  int fd.
b17e0 29 7b 0a 20 20 2f 2a 20 74 65 73 74 20 62 79 74  ){.  /* test byt
b17f0 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 20 75 73 69  e-range lock usi
b1800 6e 67 20 66 63 6e 74 6c 20 2a 2f 0a 20 20 73 74  ng fcntl */.  st
b1810 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 49  ruct flock lockI
b1820 6e 66 6f 3b 0a 20 20 0a 20 20 6c 6f 63 6b 49 6e  nfo;.  .  lockIn
b1830 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20  fo.l_len = 1;.  
b1840 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 74 61 72 74  lockInfo.l_start
b1850 20 3d 20 30 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f   = 0;.  lockInfo
b1860 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b  .l_whence = SEEK
b1870 5f 53 45 54 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f  _SET;.  lockInfo
b1880 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43  .l_type = F_RDLC
b1890 4b 3b 0a 20 20 0a 20 20 69 66 28 20 66 63 6e 74  K;.  .  if( fcnt
b18a0 6c 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26  l(fd, F_GETLK, &
b18b0 6c 6f 63 6b 49 6e 66 6f 29 21 3d 2d 31 20 29 20  lockInfo)!=-1 ) 
b18c0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 6f 73  {.    return pos
b18d0 69 78 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a  ixLockingStyle;.
b18e0 20 20 7d 20 0a 20 20 0a 20 20 2f 2a 20 74 65 73    } .  .  /* tes
b18f0 74 69 6e 67 20 66 6f 72 20 66 6c 6f 63 6b 20 63  ting for flock c
b1900 61 6e 20 67 69 76 65 20 66 61 6c 73 65 20 70 6f  an give false po
b1910 73 69 74 69 76 65 73 2e 20 20 53 6f 20 69 66 20  sitives.  So if 
b1920 69 66 20 74 68 65 20 61 62 6f 76 65 20 74 65 73  if the above tes
b1930 74 0a 20 20 2a 2a 20 66 61 69 6c 73 2c 20 74 68  t.  ** fails, th
b1940 65 6e 20 77 65 20 66 61 6c 6c 20 62 61 63 6b 20  en we fall back 
b1950 74 6f 20 75 73 69 6e 67 20 64 6f 74 2d 6c 6f 63  to using dot-loc
b1960 6b 20 73 74 79 6c 65 20 6c 6f 63 6b 69 6e 67 2e  k style locking.
b1970 0a 20 20 2a 2f 20 20 0a 20 20 72 65 74 75 72 6e  .  */  .  return
b1980 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 53   dotlockLockingS
b1990 74 79 6c 65 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20  tyle;.}../* .** 
b19a0 45 78 61 6d 69 6e 65 73 20 74 68 65 20 66 5f 66  Examines the f_f
b19b0 73 74 79 70 65 6e 61 6d 65 20 65 6e 74 72 79 20  stypename entry 
b19c0 69 6e 20 74 68 65 20 73 74 61 74 66 73 20 73 74  in the statfs st
b19d0 72 75 63 74 75 72 65 20 61 73 20 72 65 74 75 72  ructure as retur
b19e0 6e 65 64 20 62 79 20 0a 2a 2a 20 73 74 61 74 28  ned by .** stat(
b19f0 29 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 73  ) for the file s
b1a00 79 73 74 65 6d 20 68 6f 73 74 69 6e 67 20 74 68  ystem hosting th
b1a10 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
b1a20 20 61 73 73 69 67 6e 73 20 74 68 65 20 0a 2a 2a   assigns the .**
b1a30 20 61 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63   appropriate loc
b1a40 6b 69 6e 67 20 73 74 79 6c 65 20 62 61 73 65 64  king style based
b1a50 20 6f 6e 20 69 74 73 20 76 61 6c 75 65 2e 20 20   on its value.  
b1a60 54 68 65 73 65 20 76 61 6c 75 65 73 20 61 6e 64  These values and
b1a70 20 0a 2a 2a 20 61 73 73 69 67 6e 6d 65 6e 74 73   .** assignments
b1a80 20 61 72 65 20 62 61 73 65 64 20 6f 6e 20 44 61   are based on Da
b1a90 72 77 69 6e 2f 4f 53 58 20 62 65 68 61 76 69 6f  rwin/OSX behavio
b1aa0 72 20 61 6e 64 20 68 61 76 65 20 6e 6f 74 20 62  r and have not b
b1ab0 65 65 6e 20 74 65 73 74 65 64 20 6f 6e 20 0a 2a  een tested on .*
b1ac0 2a 20 6f 74 68 65 72 20 73 79 73 74 65 6d 73 2e  * other systems.
b1ad0 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74  .*/.static sqlit
b1ae0 65 33 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 73  e3LockingStyle s
b1af0 71 6c 69 74 65 33 44 65 74 65 63 74 4c 6f 63 6b  qlite3DetectLock
b1b00 69 6e 67 53 74 79 6c 65 28 0a 20 20 63 6f 6e 73  ingStyle(.  cons
b1b10 74 20 63 68 61 72 20 2a 66 69 6c 65 50 61 74 68  t char *filePath
b1b20 2c 20 0a 20 20 69 6e 74 20 66 64 0a 29 7b 0a 0a  , .  int fd.){..
b1b30 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 46 49  #ifdef SQLITE_FI
b1b40 58 45 44 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  XED_LOCKING_STYL
b1b50 45 0a 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69  E.  return (sqli
b1b60 74 65 33 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 29  te3LockingStyle)
b1b70 53 51 4c 49 54 45 5f 46 49 58 45 44 5f 4c 4f 43  SQLITE_FIXED_LOC
b1b80 4b 49 4e 47 5f 53 54 59 4c 45 3b 0a 23 65 6c 73  KING_STYLE;.#els
b1b90 65 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 66  e.  struct statf
b1ba0 73 20 66 73 49 6e 66 6f 3b 0a 0a 20 20 69 66 28  s fsInfo;..  if(
b1bb0 20 73 74 61 74 66 73 28 66 69 6c 65 50 61 74 68   statfs(filePath
b1bc0 2c 20 26 66 73 49 6e 66 6f 29 20 3d 3d 20 2d 31  , &fsInfo) == -1
b1bd0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73   ){.    return s
b1be0 71 6c 69 74 65 33 54 65 73 74 4c 6f 63 6b 69 6e  qlite3TestLockin
b1bf0 67 53 74 79 6c 65 28 66 69 6c 65 50 61 74 68 2c  gStyle(filePath,
b1c00 20 66 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20   fd);.  }.  if( 
b1c10 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67 73 20 26  fsInfo.f_flags &
b1c20 20 4d 4e 54 5f 52 44 4f 4e 4c 59 20 29 7b 0a 20   MNT_RDONLY ){. 
b1c30 20 20 20 72 65 74 75 72 6e 20 6e 6f 4c 6f 63 6b     return noLock
b1c40 69 6e 67 53 74 79 6c 65 3b 0a 20 20 7d 0a 20 20  ingStyle;.  }.  
b1c50 69 66 28 20 73 74 72 63 6d 70 28 66 73 49 6e 66  if( strcmp(fsInf
b1c60 6f 2e 66 5f 66 73 74 79 70 65 6e 61 6d 65 2c 20  o.f_fstypename, 
b1c70 22 68 66 73 22 29 3d 3d 30 20 7c 7c 0a 20 20 20  "hfs")==0 ||.   
b1c80 20 20 20 73 74 72 63 6d 70 28 66 73 49 6e 66 6f     strcmp(fsInfo
b1c90 2e 66 5f 66 73 74 79 70 65 6e 61 6d 65 2c 20 22  .f_fstypename, "
b1ca0 75 66 73 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ufs")==0 ){.    
b1cb0 72 65 74 75 72 6e 20 70 6f 73 69 78 4c 6f 63 6b  return posixLock
b1cc0 69 6e 67 53 74 79 6c 65 3b 0a 20 20 7d 0a 20 20  ingStyle;.  }.  
b1cd0 69 66 28 20 73 74 72 63 6d 70 28 66 73 49 6e 66  if( strcmp(fsInf
b1ce0 6f 2e 66 5f 66 73 74 79 70 65 6e 61 6d 65 2c 20  o.f_fstypename, 
b1cf0 22 61 66 70 66 73 22 29 3d 3d 30 20 29 7b 0a 20  "afpfs")==0 ){. 
b1d00 20 20 20 72 65 74 75 72 6e 20 61 66 70 4c 6f 63     return afpLoc
b1d10 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20 7d 0a 20  kingStyle;.  }. 
b1d20 20 69 66 28 20 73 74 72 63 6d 70 28 66 73 49 6e   if( strcmp(fsIn
b1d30 66 6f 2e 66 5f 66 73 74 79 70 65 6e 61 6d 65 2c  fo.f_fstypename,
b1d40 20 22 6e 66 73 22 29 3d 3d 30 20 29 7b 0a 20 20   "nfs")==0 ){.  
b1d50 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
b1d60 54 65 73 74 4c 6f 63 6b 69 6e 67 53 74 79 6c 65  TestLockingStyle
b1d70 28 66 69 6c 65 50 61 74 68 2c 20 66 64 29 3b 0a  (filePath, fd);.
b1d80 20 20 7d 0a 20 20 69 66 28 20 73 74 72 63 6d 70    }.  if( strcmp
b1d90 28 66 73 49 6e 66 6f 2e 66 5f 66 73 74 79 70 65  (fsInfo.f_fstype
b1da0 6e 61 6d 65 2c 20 22 73 6d 62 66 73 22 29 3d 3d  name, "smbfs")==
b1db0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
b1dc0 66 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 53 74 79 6c  flockLockingStyl
b1dd0 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 74 72  e;.  }.  if( str
b1de0 63 6d 70 28 66 73 49 6e 66 6f 2e 66 5f 66 73 74  cmp(fsInfo.f_fst
b1df0 79 70 65 6e 61 6d 65 2c 20 22 6d 73 64 6f 73 22  ypename, "msdos"
b1e00 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  )==0 ){.    retu
b1e10 72 6e 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e  rn dotlockLockin
b1e20 67 53 74 79 6c 65 3b 0a 20 20 7d 0a 20 20 69 66  gStyle;.  }.  if
b1e30 28 20 73 74 72 63 6d 70 28 66 73 49 6e 66 6f 2e  ( strcmp(fsInfo.
b1e40 66 5f 66 73 74 79 70 65 6e 61 6d 65 2c 20 22 77  f_fstypename, "w
b1e50 65 62 64 61 76 22 29 3d 3d 30 20 29 7b 0a 20 20  ebdav")==0 ){.  
b1e60 20 20 72 65 74 75 72 6e 20 75 6e 73 75 70 70 6f    return unsuppo
b1e70 72 74 65 64 4c 6f 63 6b 69 6e 67 53 74 79 6c 65  rtedLockingStyle
b1e80 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73  ;.  }.  return s
b1e90 71 6c 69 74 65 33 54 65 73 74 4c 6f 63 6b 69 6e  qlite3TestLockin
b1ea0 67 53 74 79 6c 65 28 66 69 6c 65 50 61 74 68 2c  gStyle(filePath,
b1eb0 20 66 64 29 3b 20 20 0a 23 65 6e 64 69 66 20 2f   fd);  .#endif /
b1ec0 2a 20 53 51 4c 49 54 45 5f 46 49 58 45 44 5f 4c  * SQLITE_FIXED_L
b1ed0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a  OCKING_STYLE */.
b1ee0 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
b1ef0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
b1f00 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a  NG_STYLE */../*.
b1f10 2a 2a 20 47 69 76 65 6e 20 61 20 66 69 6c 65 20  ** Given a file 
b1f20 64 65 73 63 72 69 70 74 6f 72 2c 20 6c 6f 63 61  descriptor, loca
b1f30 74 65 20 6c 6f 63 6b 49 6e 66 6f 20 61 6e 64 20  te lockInfo and 
b1f40 6f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72  openCnt structur
b1f50 65 73 20 74 68 61 74 0a 2a 2a 20 64 65 73 63 72  es that.** descr
b1f60 69 62 65 73 20 74 68 61 74 20 66 69 6c 65 20 64  ibes that file d
b1f70 65 73 63 72 69 70 74 6f 72 2e 20 20 43 72 65 61  escriptor.  Crea
b1f80 74 65 20 6e 65 77 20 6f 6e 65 73 20 69 66 20 6e  te new ones if n
b1f90 65 63 65 73 73 61 72 79 2e 20 20 54 68 65 0a 2a  ecessary.  The.*
b1fa0 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73 20  * return values 
b1fb0 6d 69 67 68 74 20 62 65 20 75 6e 69 6e 69 74 69  might be uniniti
b1fc0 61 6c 69 7a 65 64 20 69 66 20 61 6e 20 65 72 72  alized if an err
b1fd0 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a  or occurs..**.**
b1fe0 20 52 65 74 75 72 6e 20 61 6e 20 61 70 70 72 6f   Return an appro
b1ff0 70 72 69 61 74 65 20 65 72 72 6f 72 20 63 6f 64  priate error cod
b2000 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
b2010 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 0a 20   findLockInfo(. 
b2020 20 69 6e 74 20 66 64 2c 20 20 20 20 20 20 20 20   int fd,        
b2030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
b2040 20 54 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   The file descri
b2050 70 74 6f 72 20 75 73 65 64 20 69 6e 20 74 68 65  ptor used in the
b2060 20 6b 65 79 20 2a 2f 0a 20 20 73 74 72 75 63 74   key */.  struct
b2070 20 6c 6f 63 6b 49 6e 66 6f 20 2a 2a 70 70 4c 6f   lockInfo **ppLo
b2080 63 6b 2c 20 20 20 20 2f 2a 20 52 65 74 75 72 6e  ck,    /* Return
b2090 20 74 68 65 20 6c 6f 63 6b 49 6e 66 6f 20 73 74   the lockInfo st
b20a0 72 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a  ructure here */.
b20b0 20 20 73 74 72 75 63 74 20 6f 70 65 6e 43 6e 74    struct openCnt
b20c0 20 2a 2a 70 70 4f 70 65 6e 20 20 20 20 20 20 2f   **ppOpen      /
b20d0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6f 70 65  * Return the ope
b20e0 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 68  nCnt structure h
b20f0 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ere */.){.  int 
b2100 72 63 3b 0a 20 20 73 74 72 75 63 74 20 6c 6f 63  rc;.  struct loc
b2110 6b 4b 65 79 20 6b 65 79 31 3b 0a 20 20 73 74 72  kKey key1;.  str
b2120 75 63 74 20 6f 70 65 6e 4b 65 79 20 6b 65 79 32  uct openKey key2
b2130 3b 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 20  ;.  struct stat 
b2140 73 74 61 74 62 75 66 3b 0a 20 20 73 74 72 75 63  statbuf;.  struc
b2150 74 20 6c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63  t lockInfo *pLoc
b2160 6b 3b 0a 20 20 73 74 72 75 63 74 20 6f 70 65 6e  k;.  struct open
b2170 43 6e 74 20 2a 70 4f 70 65 6e 3b 0a 20 20 72 63  Cnt *pOpen;.  rc
b2180 20 3d 20 66 73 74 61 74 28 66 64 2c 20 26 73 74   = fstat(fd, &st
b2190 61 74 62 75 66 29 3b 0a 20 20 69 66 28 20 72 63  atbuf);.  if( rc
b21a0 21 3d 30 20 29 7b 0a 23 69 66 64 65 66 20 45 4f  !=0 ){.#ifdef EO
b21b0 56 45 52 46 4c 4f 57 0a 20 20 20 20 69 66 28 20  VERFLOW.    if( 
b21c0 65 72 72 6e 6f 3d 3d 45 4f 56 45 52 46 4c 4f 57  errno==EOVERFLOW
b21d0 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
b21e0 5f 4e 4f 4c 46 53 3b 0a 23 65 6e 64 69 66 0a 20  _NOLFS;.#endif. 
b21f0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
b2200 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 0a 20 20 6d  _IOERR;.  }..  m
b2210 65 6d 73 65 74 28 26 6b 65 79 31 2c 20 30 2c 20  emset(&key1, 0, 
b2220 73 69 7a 65 6f 66 28 6b 65 79 31 29 29 3b 0a 20  sizeof(key1));. 
b2230 20 6b 65 79 31 2e 64 65 76 20 3d 20 73 74 61 74   key1.dev = stat
b2240 62 75 66 2e 73 74 5f 64 65 76 3b 0a 20 20 6b 65  buf.st_dev;.  ke
b2250 79 31 2e 69 6e 6f 20 3d 20 73 74 61 74 62 75 66  y1.ino = statbuf
b2260 2e 73 74 5f 69 6e 6f 3b 0a 23 69 66 20 53 51 4c  .st_ino;.#if SQL
b2270 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20  ITE_THREADSAFE. 
b2280 20 69 66 28 20 74 68 72 65 61 64 73 4f 76 65 72   if( threadsOver
b2290 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f  rideEachOthersLo
b22a0 63 6b 73 3c 30 20 29 7b 0a 20 20 20 20 74 65 73  cks<0 ){.    tes
b22b0 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65  tThreadLockingBe
b22c0 68 61 76 69 6f 72 28 66 64 29 3b 0a 20 20 7d 0a  havior(fd);.  }.
b22d0 20 20 6b 65 79 31 2e 74 69 64 20 3d 20 74 68 72    key1.tid = thr
b22e0 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68  eadsOverrideEach
b22f0 4f 74 68 65 72 73 4c 6f 63 6b 73 20 3f 20 30 20  OthersLocks ? 0 
b2300 3a 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29  : pthread_self()
b2310 3b 0a 23 65 6e 64 69 66 0a 20 20 6d 65 6d 73 65  ;.#endif.  memse
b2320 74 28 26 6b 65 79 32 2c 20 30 2c 20 73 69 7a 65  t(&key2, 0, size
b2330 6f 66 28 6b 65 79 32 29 29 3b 0a 20 20 6b 65 79  of(key2));.  key
b2340 32 2e 64 65 76 20 3d 20 73 74 61 74 62 75 66 2e  2.dev = statbuf.
b2350 73 74 5f 64 65 76 3b 0a 20 20 6b 65 79 32 2e 69  st_dev;.  key2.i
b2360 6e 6f 20 3d 20 73 74 61 74 62 75 66 2e 73 74 5f  no = statbuf.st_
b2370 69 6e 6f 3b 0a 20 20 70 4c 6f 63 6b 20 3d 20 28  ino;.  pLock = (
b2380 73 74 72 75 63 74 20 6c 6f 63 6b 49 6e 66 6f 2a  struct lockInfo*
b2390 29 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64  )sqlite3HashFind
b23a0 28 26 6c 6f 63 6b 48 61 73 68 2c 20 26 6b 65 79  (&lockHash, &key
b23b0 31 2c 20 73 69 7a 65 6f 66 28 6b 65 79 31 29 29  1, sizeof(key1))
b23c0 3b 0a 20 20 69 66 28 20 70 4c 6f 63 6b 3d 3d 30  ;.  if( pLock==0
b23d0 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 6c   ){.    struct l
b23e0 6f 63 6b 49 6e 66 6f 20 2a 70 4f 6c 64 3b 0a 20  ockInfo *pOld;. 
b23f0 20 20 20 70 4c 6f 63 6b 20 3d 20 73 71 6c 69 74     pLock = sqlit
b2400 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f  e3_malloc( sizeo
b2410 66 28 2a 70 4c 6f 63 6b 29 20 29 3b 0a 20 20 20  f(*pLock) );.   
b2420 20 69 66 28 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b   if( pLock==0 ){
b2430 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
b2440 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
b2450 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f  goto exit_findlo
b2460 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 7d 0a 20 20  ckinfo;.    }.  
b2470 20 20 70 4c 6f 63 6b 2d 3e 6b 65 79 20 3d 20 6b    pLock->key = k
b2480 65 79 31 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e  ey1;.    pLock->
b2490 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 70 4c  nRef = 1;.    pL
b24a0 6f 63 6b 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 20  ock->cnt = 0;.  
b24b0 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70    pLock->locktyp
b24c0 65 20 3d 20 30 3b 0a 20 20 20 20 70 4f 6c 64 20  e = 0;.    pOld 
b24d0 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73  = sqlite3HashIns
b24e0 65 72 74 28 26 6c 6f 63 6b 48 61 73 68 2c 20 26  ert(&lockHash, &
b24f0 70 4c 6f 63 6b 2d 3e 6b 65 79 2c 20 73 69 7a 65  pLock->key, size
b2500 6f 66 28 6b 65 79 31 29 2c 20 70 4c 6f 63 6b 29  of(key1), pLock)
b2510 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c 64 21 3d  ;.    if( pOld!=
b2520 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  0 ){.      asser
b2530 74 28 20 70 4f 6c 64 3d 3d 70 4c 6f 63 6b 20 29  t( pOld==pLock )
b2540 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
b2550 66 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 20 20  free(pLock);.   
b2560 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
b2570 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f  OMEM;.      goto
b2580 20 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69 6e   exit_findlockin
b2590 66 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  fo;.    }.  }els
b25a0 65 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52  e{.    pLock->nR
b25b0 65 66 2b 2b 3b 0a 20 20 7d 0a 20 20 2a 70 70 4c  ef++;.  }.  *ppL
b25c0 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 69  ock = pLock;.  i
b25d0 66 28 20 70 70 4f 70 65 6e 21 3d 30 20 29 7b 0a  f( ppOpen!=0 ){.
b25e0 20 20 20 20 70 4f 70 65 6e 20 3d 20 28 73 74 72      pOpen = (str
b25f0 75 63 74 20 6f 70 65 6e 43 6e 74 2a 29 73 71 6c  uct openCnt*)sql
b2600 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 6f 70  ite3HashFind(&op
b2610 65 6e 48 61 73 68 2c 20 26 6b 65 79 32 2c 20 73  enHash, &key2, s
b2620 69 7a 65 6f 66 28 6b 65 79 32 29 29 3b 0a 20 20  izeof(key2));.  
b2630 20 20 69 66 28 20 70 4f 70 65 6e 3d 3d 30 20 29    if( pOpen==0 )
b2640 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 6f  {.      struct o
b2650 70 65 6e 43 6e 74 20 2a 70 4f 6c 64 3b 0a 20 20  penCnt *pOld;.  
b2660 20 20 20 20 70 4f 70 65 6e 20 3d 20 73 71 6c 69      pOpen = sqli
b2670 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65  te3_malloc( size
b2680 6f 66 28 2a 70 4f 70 65 6e 29 20 29 3b 0a 20 20  of(*pOpen) );.  
b2690 20 20 20 20 69 66 28 20 70 4f 70 65 6e 3d 3d 30      if( pOpen==0
b26a0 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65   ){.        rele
b26b0 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 4c 6f 63  aseLockInfo(pLoc
b26c0 6b 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  k);.        rc =
b26d0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
b26e0 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74         goto exit
b26f0 5f 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3b 0a 20  _findlockinfo;. 
b2700 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4f 70       }.      pOp
b2710 65 6e 2d 3e 6b 65 79 20 3d 20 6b 65 79 32 3b 0a  en->key = key2;.
b2720 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65        pOpen->nRe
b2730 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4f 70  f = 1;.      pOp
b2740 65 6e 2d 3e 6e 4c 6f 63 6b 20 3d 20 30 3b 0a 20  en->nLock = 0;. 
b2750 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e       pOpen->nPen
b2760 64 69 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20  ding = 0;.      
b2770 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 20  pOpen->aPending 
b2780 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f 6c 64 20  = 0;.      pOld 
b2790 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73  = sqlite3HashIns
b27a0 65 72 74 28 26 6f 70 65 6e 48 61 73 68 2c 20 26  ert(&openHash, &
b27b0 70 4f 70 65 6e 2d 3e 6b 65 79 2c 20 73 69 7a 65  pOpen->key, size
b27c0 6f 66 28 6b 65 79 32 29 2c 20 70 4f 70 65 6e 29  of(key2), pOpen)
b27d0 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 6c 64  ;.      if( pOld
b27e0 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61  !=0 ){.        a
b27f0 73 73 65 72 74 28 20 70 4f 6c 64 3d 3d 70 4f 70  ssert( pOld==pOp
b2800 65 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71  en );.        sq
b2810 6c 69 74 65 33 5f 66 72 65 65 28 70 4f 70 65 6e  lite3_free(pOpen
b2820 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61  );.        relea
b2830 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 4c 6f 63 6b  seLockInfo(pLock
b2840 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
b2850 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
b2860 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f        goto exit_
b2870 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 20  findlockinfo;.  
b2880 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
b2890 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52  .      pOpen->nR
b28a0 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ef++;.    }.    
b28b0 2a 70 70 4f 70 65 6e 20 3d 20 70 4f 70 65 6e 3b  *ppOpen = pOpen;
b28c0 0a 20 20 7d 0a 0a 65 78 69 74 5f 66 69 6e 64 6c  .  }..exit_findl
b28d0 6f 63 6b 69 6e 66 6f 3a 0a 20 20 72 65 74 75 72  ockinfo:.  retur
b28e0 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 64 65 66 20  n rc;.}..#ifdef 
b28f0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a  SQLITE_DEBUG./*.
b2900 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69  ** Helper functi
b2910 6f 6e 20 66 6f 72 20 70 72 69 6e 74 69 6e 67 20  on for printing 
b2920 6f 75 74 20 74 72 61 63 65 20 69 6e 66 6f 72 6d  out trace inform
b2930 61 74 69 6f 6e 20 66 72 6f 6d 20 64 65 62 75 67  ation from debug
b2940 67 69 6e 67 0a 2a 2a 20 62 69 6e 61 72 69 65 73  ging.** binaries
b2950 2e 20 54 68 69 73 20 72 65 74 75 72 6e 73 20 74  . This returns t
b2960 68 65 20 73 74 72 69 6e 67 20 72 65 70 72 65 73  he string repres
b2970 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  etation of the s
b2980 75 70 70 6c 69 65 64 0a 2a 2a 20 69 6e 74 65 67  upplied.** integ
b2990 65 72 20 6c 6f 63 6b 2d 74 79 70 65 2e 0a 2a 2f  er lock-type..*/
b29a0 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68  .static const ch
b29b0 61 72 20 2a 6c 6f 63 6b 74 79 70 65 4e 61 6d 65  ar *locktypeName
b29c0 28 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a  (int locktype){.
b29d0 20 20 73 77 69 74 63 68 28 20 6c 6f 63 6b 74 79    switch( lockty
b29e0 70 65 20 29 7b 0a 20 20 63 61 73 65 20 4e 4f 5f  pe ){.  case NO_
b29f0 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 4e 4f  LOCK: return "NO
b2a00 4e 45 22 3b 0a 20 20 63 61 73 65 20 53 48 41 52  NE";.  case SHAR
b2a10 45 44 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20  ED_LOCK: return 
b2a20 22 53 48 41 52 45 44 22 3b 0a 20 20 63 61 73 65  "SHARED";.  case
b2a30 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 20   RESERVED_LOCK: 
b2a40 72 65 74 75 72 6e 20 22 52 45 53 45 52 56 45 44  return "RESERVED
b2a50 22 3b 0a 20 20 63 61 73 65 20 50 45 4e 44 49 4e  ";.  case PENDIN
b2a60 47 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22  G_LOCK: return "
b2a70 50 45 4e 44 49 4e 47 22 3b 0a 20 20 63 61 73 65  PENDING";.  case
b2a80 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a   EXCLUSIVE_LOCK:
b2a90 20 72 65 74 75 72 6e 20 22 45 58 43 4c 55 53 49   return "EXCLUSI
b2aa0 56 45 22 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  VE";.  }.  retur
b2ab0 6e 20 22 45 52 52 4f 52 22 3b 0a 7d 0a 23 65 6e  n "ERROR";.}.#en
b2ac0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65  dif../*.** If we
b2ad0 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 69   are currently i
b2ae0 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 74 68  n a different th
b2af0 72 65 61 64 20 74 68 61 6e 20 74 68 65 20 74 68  read than the th
b2b00 72 65 61 64 20 74 68 61 74 20 74 68 65 0a 2a 2a  read that the.**
b2b10 20 75 6e 69 78 46 69 6c 65 20 61 72 67 75 6d 65   unixFile argume
b2b20 6e 74 20 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 74  nt belongs to, t
b2b30 68 65 6e 20 74 72 61 6e 73 66 65 72 20 6f 77 6e  hen transfer own
b2b40 65 72 73 68 69 70 20 6f 66 20 74 68 65 20 75 6e  ership of the un
b2b50 69 78 46 69 6c 65 0a 2a 2a 20 6f 76 65 72 20 74  ixFile.** over t
b2b60 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68  o the current th
b2b70 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 75 6e  read..**.** A un
b2b80 69 78 46 69 6c 65 20 69 73 20 6f 6e 6c 79 20 6f  ixFile is only o
b2b90 77 6e 65 64 20 62 79 20 61 20 74 68 72 65 61 64  wned by a thread
b2ba0 20 6f 6e 20 73 79 73 74 65 6d 73 20 77 68 65 72   on systems wher
b2bb0 65 20 6f 6e 65 20 74 68 72 65 61 64 20 69 73 0a  e one thread is.
b2bc0 2a 2a 20 75 6e 61 62 6c 65 20 74 6f 20 6f 76 65  ** unable to ove
b2bd0 72 72 69 64 65 20 6c 6f 63 6b 73 20 63 72 65 61  rride locks crea
b2be0 74 65 64 20 62 79 20 61 20 64 69 66 66 65 72 65  ted by a differe
b2bf0 6e 74 20 74 68 72 65 61 64 2e 20 20 52 65 64 48  nt thread.  RedH
b2c00 61 74 39 20 69 73 0a 2a 2a 20 61 6e 20 65 78 61  at9 is.** an exa
b2c10 6d 70 6c 65 20 6f 66 20 73 75 63 68 20 61 20 73  mple of such a s
b2c20 79 73 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 4f 77 6e  ystem..**.** Own
b2c30 65 72 73 68 69 70 20 74 72 61 6e 73 66 65 72 20  ership transfer 
b2c40 69 73 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20  is only allowed 
b2c50 69 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 20  if the unixFile 
b2c60 69 73 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 6c  is currently unl
b2c70 6f 63 6b 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65  ocked..** If the
b2c80 20 75 6e 69 78 46 69 6c 65 20 69 73 20 6c 6f 63   unixFile is loc
b2c90 6b 65 64 20 61 6e 64 20 61 6e 20 6f 77 6e 65 72  ked and an owner
b2ca0 73 68 69 70 20 69 73 20 77 72 6f 6e 67 2c 20 74  ship is wrong, t
b2cb0 68 65 6e 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51  hen return.** SQ
b2cc0 4c 49 54 45 5f 4d 49 53 55 53 45 2e 20 20 53 51  LITE_MISUSE.  SQ
b2cd0 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
b2ce0 6e 65 64 20 69 66 20 65 76 65 72 79 74 68 69 6e  ned if everythin
b2cf0 67 20 77 6f 72 6b 73 2e 0a 2a 2f 0a 23 69 66 20  g works..*/.#if 
b2d00 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
b2d10 45 0a 73 74 61 74 69 63 20 69 6e 74 20 74 72 61  E.static int tra
b2d20 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28 75  nsferOwnership(u
b2d30 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b  nixFile *pFile){
b2d40 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 70 74 68  .  int rc;.  pth
b2d50 72 65 61 64 5f 74 20 68 53 65 6c 66 3b 0a 20 20  read_t hSelf;.  
b2d60 69 66 28 20 74 68 72 65 61 64 73 4f 76 65 72 72  if( threadsOverr
b2d70 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63  ideEachOthersLoc
b2d80 6b 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 77 6e  ks ){.    /* Own
b2d90 65 72 73 68 69 70 20 74 72 61 6e 73 66 65 72 73  ership transfers
b2da0 20 6e 6f 74 20 6e 65 65 64 65 64 20 6f 6e 20 74   not needed on t
b2db0 68 69 73 20 73 79 73 74 65 6d 20 2a 2f 0a 20 20  his system */.  
b2dc0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
b2dd0 4f 4b 3b 0a 20 20 7d 0a 20 20 68 53 65 6c 66 20  OK;.  }.  hSelf 
b2de0 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29  = pthread_self()
b2df0 3b 0a 20 20 69 66 28 20 70 74 68 72 65 61 64 5f  ;.  if( pthread_
b2e00 65 71 75 61 6c 28 70 46 69 6c 65 2d 3e 74 69 64  equal(pFile->tid
b2e10 2c 20 68 53 65 6c 66 29 20 29 7b 0a 20 20 20 20  , hSelf) ){.    
b2e20 2f 2a 20 57 65 20 61 72 65 20 73 74 69 6c 6c 20  /* We are still 
b2e30 69 6e 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  in the same thre
b2e40 61 64 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41 43  ad */.    OSTRAC
b2e50 45 31 28 22 4e 6f 2d 74 72 61 6e 73 66 65 72 2c  E1("No-transfer,
b2e60 20 73 61 6d 65 20 74 68 72 65 61 64 5c 6e 22 29   same thread\n")
b2e70 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
b2e80 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66  ITE_OK;.  }.  if
b2e90 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
b2ea0 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20  e!=NO_LOCK ){.  
b2eb0 20 20 2f 2a 20 57 65 20 63 61 6e 6e 6f 74 20 63    /* We cannot c
b2ec0 68 61 6e 67 65 20 6f 77 6e 65 72 73 68 69 70 20  hange ownership 
b2ed0 77 68 69 6c 65 20 77 65 20 61 72 65 20 68 6f 6c  while we are hol
b2ee0 64 69 6e 67 20 61 20 6c 6f 63 6b 21 20 2a 2f 0a  ding a lock! */.
b2ef0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
b2f00 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
b2f10 4f 53 54 52 41 43 45 34 28 22 54 72 61 6e 73 66  OSTRACE4("Transf
b2f20 65 72 20 6f 77 6e 65 72 73 68 69 70 20 6f 66 20  er ownership of 
b2f30 25 64 20 66 72 6f 6d 20 25 64 20 74 6f 20 25 64  %d from %d to %d
b2f40 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
b2f50 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65   pFile->h, pFile
b2f60 2d 3e 74 69 64 2c 20 68 53 65 6c 66 29 3b 0a 20  ->tid, hSelf);. 
b2f70 20 70 46 69 6c 65 2d 3e 74 69 64 20 3d 20 68 53   pFile->tid = hS
b2f80 65 6c 66 3b 0a 20 20 69 66 20 28 70 46 69 6c 65  elf;.  if (pFile
b2f90 2d 3e 70 4c 6f 63 6b 20 21 3d 20 4e 55 4c 4c 29  ->pLock != NULL)
b2fa0 20 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 4c 6f   {.    releaseLo
b2fb0 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 4c  ckInfo(pFile->pL
b2fc0 6f 63 6b 29 3b 0a 20 20 20 20 72 63 20 3d 20 66  ock);.    rc = f
b2fd0 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c  indLockInfo(pFil
b2fe0 65 2d 3e 68 2c 20 26 70 46 69 6c 65 2d 3e 70 4c  e->h, &pFile->pL
b2ff0 6f 63 6b 2c 20 30 29 3b 0a 20 20 20 20 4f 53 54  ock, 0);.    OST
b3000 52 41 43 45 35 28 22 4c 4f 43 4b 20 20 20 20 25  RACE5("LOCK    %
b3010 64 20 69 73 20 6e 6f 77 20 25 73 28 25 73 2c 25  d is now %s(%s,%
b3020 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  d)\n", pFile->h,
b3030 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b  .           lock
b3040 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e  typeName(pFile->
b3050 6c 6f 63 6b 74 79 70 65 29 2c 0a 20 20 20 20 20  locktype),.     
b3060 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61        locktypeNa
b3070 6d 65 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d  me(pFile->pLock-
b3080 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 70 46 69 6c  >locktype), pFil
b3090 65 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e 74 29 3b 0a  e->pLock->cnt);.
b30a0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
b30b0 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65   } else {.    re
b30c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
b30d0 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a    }.}.#else.  /*
b30e0 20 4f 6e 20 73 69 6e 67 6c 65 2d 74 68 72 65 61   On single-threa
b30f0 64 65 64 20 62 75 69 6c 64 73 2c 20 6f 77 6e 65  ded builds, owne
b3100 72 73 68 69 70 20 74 72 61 6e 73 66 65 72 20 69  rship transfer i
b3110 73 20 61 20 6e 6f 2d 6f 70 20 2a 2f 0a 23 20 64  s a no-op */.# d
b3120 65 66 69 6e 65 20 74 72 61 6e 73 66 65 72 4f 77  efine transferOw
b3130 6e 65 72 73 68 69 70 28 58 29 20 53 51 4c 49 54  nership(X) SQLIT
b3140 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  E_OK.#endif../*.
b3150 2a 2a 20 53 65 65 6b 20 74 6f 20 74 68 65 20 6f  ** Seek to the o
b3160 66 66 73 65 74 20 70 61 73 73 65 64 20 61 73 20  ffset passed as 
b3170 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
b3180 65 6e 74 2c 20 74 68 65 6e 20 72 65 61 64 20 63  ent, then read c
b3190 6e 74 20 0a 2a 2a 20 62 79 74 65 73 20 69 6e 74  nt .** bytes int
b31a0 6f 20 70 42 75 66 2e 20 52 65 74 75 72 6e 20 74  o pBuf. Return t
b31b0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
b31c0 65 73 20 61 63 74 75 61 6c 6c 79 20 72 65 61 64  es actually read
b31d0 2e 0a 2a 2a 0a 2a 2a 20 4e 42 3a 20 20 49 66 20  ..**.** NB:  If 
b31e0 79 6f 75 20 64 65 66 69 6e 65 20 55 53 45 5f 50  you define USE_P
b31f0 52 45 41 44 20 6f 72 20 55 53 45 5f 50 52 45 41  READ or USE_PREA
b3200 44 36 34 2c 20 74 68 65 6e 20 69 74 20 6d 69 67  D64, then it mig
b3210 68 74 20 61 6c 73 6f 0a 2a 2a 20 62 65 20 6e 65  ht also.** be ne
b3220 63 65 73 73 61 72 79 20 74 6f 20 64 65 66 69 6e  cessary to defin
b3230 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20  e _XOPEN_SOURCE 
b3240 74 6f 20 62 65 20 35 30 30 2e 20 20 54 68 69 73  to be 500.  This
b3250 20 76 61 72 69 65 73 20 66 72 6f 6d 0a 2a 2a 20   varies from.** 
b3260 6f 6e 65 20 73 79 73 74 65 6d 20 74 6f 20 61 6e  one system to an
b3270 6f 74 68 65 72 2e 20 20 53 69 6e 63 65 20 53 51  other.  Since SQ
b3280 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 64 65  Lite does not de
b3290 66 69 6e 65 20 55 53 45 5f 50 52 45 41 44 0a 2a  fine USE_PREAD.*
b32a0 2a 20 61 6e 79 20 61 6e 79 20 66 6f 72 6d 20 62  * any any form b
b32b0 79 20 64 65 66 61 75 6c 74 2c 20 77 65 20 77 69  y default, we wi
b32c0 6c 6c 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74  ll not attempt t
b32d0 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f  o define _XOPEN_
b32e0 53 4f 55 52 43 45 2e 0a 2a 2a 20 53 65 65 20 74  SOURCE..** See t
b32f0 69 63 6b 65 74 73 20 23 32 37 34 31 20 61 6e 64  ickets #2741 and
b3300 20 23 32 36 38 31 2e 0a 2a 2f 0a 73 74 61 74 69   #2681..*/.stati
b3310 63 20 69 6e 74 20 73 65 65 6b 41 6e 64 52 65 61  c int seekAndRea
b3320 64 28 75 6e 69 78 46 69 6c 65 20 2a 69 64 2c 20  d(unixFile *id, 
b3330 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66  sqlite3_int64 of
b3340 66 73 65 74 2c 20 76 6f 69 64 20 2a 70 42 75 66  fset, void *pBuf
b3350 2c 20 69 6e 74 20 63 6e 74 29 7b 0a 20 20 69 6e  , int cnt){.  in
b3360 74 20 67 6f 74 3b 0a 20 20 69 36 34 20 6e 65 77  t got;.  i64 new
b3370 4f 66 66 73 65 74 3b 0a 20 20 54 49 4d 45 52 5f  Offset;.  TIMER_
b3380 53 54 41 52 54 3b 0a 23 69 66 20 64 65 66 69 6e  START;.#if defin
b3390 65 64 28 55 53 45 5f 50 52 45 41 44 29 0a 20 20  ed(USE_PREAD).  
b33a0 67 6f 74 20 3d 20 70 72 65 61 64 28 69 64 2d 3e  got = pread(id->
b33b0 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66  h, pBuf, cnt, of
b33c0 66 73 65 74 29 3b 0a 20 20 53 69 6d 75 6c 61 74  fset);.  Simulat
b33d0 65 49 4f 45 72 72 6f 72 28 20 67 6f 74 20 3d 20  eIOError( got = 
b33e0 2d 31 20 29 3b 0a 23 65 6c 69 66 20 64 65 66 69  -1 );.#elif defi
b33f0 6e 65 64 28 55 53 45 5f 50 52 45 41 44 36 34 29  ned(USE_PREAD64)
b3400 0a 20 20 67 6f 74 20 3d 20 70 72 65 61 64 36 34  .  got = pread64
b3410 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e  (id->h, pBuf, cn
b3420 74 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 53 69  t, offset);.  Si
b3430 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 67  mulateIOError( g
b3440 6f 74 20 3d 20 2d 31 20 29 3b 0a 23 65 6c 73 65  ot = -1 );.#else
b3450 0a 20 20 6e 65 77 4f 66 66 73 65 74 20 3d 20 6c  .  newOffset = l
b3460 73 65 65 6b 28 69 64 2d 3e 68 2c 20 6f 66 66 73  seek(id->h, offs
b3470 65 74 2c 20 53 45 45 4b 5f 53 45 54 29 3b 0a 20  et, SEEK_SET);. 
b3480 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
b3490 28 20 6e 65 77 4f 66 66 73 65 74 2d 2d 20 29 3b  ( newOffset-- );
b34a0 0a 20 20 69 66 28 20 6e 65 77 4f 66 66 73 65 74  .  if( newOffset
b34b0 21 3d 6f 66 66 73 65 74 20 29 7b 0a 20 20 20 20  !=offset ){.    
b34c0 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20  return -1;.  }. 
b34d0 20 67 6f 74 20 3d 20 72 65 61 64 28 69 64 2d 3e   got = read(id->
b34e0 68 2c 20 70 42 75 66 2c 20 63 6e 74 29 3b 0a 23  h, pBuf, cnt);.#
b34f0 65 6e 64 69 66 0a 20 20 54 49 4d 45 52 5f 45 4e  endif.  TIMER_EN
b3500 44 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 52  D;.  OSTRACE5("R
b3510 45 41 44 20 20 20 20 25 2d 33 64 20 25 35 64 20  EAD    %-3d %5d 
b3520 25 37 6c 6c 64 20 25 64 5c 6e 22 2c 20 69 64 2d  %7lld %d\n", id-
b3530 3e 68 2c 20 67 6f 74 2c 20 6f 66 66 73 65 74 2c  >h, got, offset,
b3540 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b   TIMER_ELAPSED);
b3550 0a 20 20 72 65 74 75 72 6e 20 67 6f 74 3b 0a 7d  .  return got;.}
b3560 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74  ../*.** Read dat
b3570 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e  a from a file in
b3580 74 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52 65  to a buffer.  Re
b3590 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69  turn SQLITE_OK i
b35a0 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77  f all.** bytes w
b35b0 65 72 65 20 72 65 61 64 20 73 75 63 63 65 73 73  ere read success
b35c0 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 45  fully and SQLITE
b35d0 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68 69  _IOERR if anythi
b35e0 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67  ng goes.** wrong
b35f0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
b3600 75 6e 69 78 52 65 61 64 28 0a 20 20 73 71 6c 69  unixRead(.  sqli
b3610 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 0a 20  te3_file *id, . 
b3620 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 0a 20 20   void *pBuf, .  
b3630 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71 6c 69 74  int amt,.  sqlit
b3640 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 0a  e3_int64 offset.
b3650 29 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20  ){.  int got;.  
b3660 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20  assert( id );.  
b3670 67 6f 74 20 3d 20 73 65 65 6b 41 6e 64 52 65 61  got = seekAndRea
b3680 64 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 2c  d((unixFile*)id,
b3690 20 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20 61   offset, pBuf, a
b36a0 6d 74 29 3b 0a 20 20 69 66 28 20 67 6f 74 3d 3d  mt);.  if( got==
b36b0 61 6d 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  amt ){.    retur
b36c0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
b36d0 65 6c 73 65 20 69 66 28 20 67 6f 74 3c 30 20 29  else if( got<0 )
b36e0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
b36f0 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0a  ITE_IOERR_READ;.
b3700 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d    }else{.    mem
b3710 73 65 74 28 26 28 28 63 68 61 72 2a 29 70 42 75  set(&((char*)pBu
b3720 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d 74 2d  f)[got], 0, amt-
b3730 67 6f 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  got);.    return
b3740 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48   SQLITE_IOERR_SH
b3750 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 0a 7d 0a  ORT_READ;.  }.}.
b3760 0a 2f 2a 0a 2a 2a 20 53 65 65 6b 20 74 6f 20 74  ./*.** Seek to t
b3770 68 65 20 6f 66 66 73 65 74 20 69 6e 20 69 64 2d  he offset in id-
b3780 3e 6f 66 66 73 65 74 20 74 68 65 6e 20 72 65 61  >offset then rea
b3790 64 20 63 6e 74 20 62 79 74 65 73 20 69 6e 74 6f  d cnt bytes into
b37a0 20 70 42 75 66 2e 0a 2a 2a 20 52 65 74 75 72 6e   pBuf..** Return
b37b0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
b37c0 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20 72 65  ytes actually re
b37d0 61 64 2e 20 20 55 70 64 61 74 65 20 74 68 65 20  ad.  Update the 
b37e0 6f 66 66 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69  offset..*/.stati
b37f0 63 20 69 6e 74 20 73 65 65 6b 41 6e 64 57 72 69  c int seekAndWri
b3800 74 65 28 75 6e 69 78 46 69 6c 65 20 2a 69 64 2c  te(unixFile *id,
b3810 20 69 36 34 20 6f 66 66 73 65 74 2c 20 63 6f 6e   i64 offset, con
b3820 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69  st void *pBuf, i
b3830 6e 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20 67  nt cnt){.  int g
b3840 6f 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66 66  ot;.  i64 newOff
b3850 73 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54 41  set;.  TIMER_STA
b3860 52 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28  RT;.#if defined(
b3870 55 53 45 5f 50 52 45 41 44 29 0a 20 20 67 6f 74  USE_PREAD).  got
b3880 20 3d 20 70 77 72 69 74 65 28 69 64 2d 3e 68 2c   = pwrite(id->h,
b3890 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73   pBuf, cnt, offs
b38a0 65 74 29 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e  et);.#elif defin
b38b0 65 64 28 55 53 45 5f 50 52 45 41 44 36 34 29 0a  ed(USE_PREAD64).
b38c0 20 20 67 6f 74 20 3d 20 70 77 72 69 74 65 36 34    got = pwrite64
b38d0 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e  (id->h, pBuf, cn
b38e0 74 2c 20 6f 66 66 73 65 74 29 3b 0a 23 65 6c 73  t, offset);.#els
b38f0 65 0a 20 20 6e 65 77 4f 66 66 73 65 74 20 3d 20  e.  newOffset = 
b3900 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20 6f 66 66  lseek(id->h, off
b3910 73 65 74 2c 20 53 45 45 4b 5f 53 45 54 29 3b 0a  set, SEEK_SET);.
b3920 20 20 69 66 28 20 6e 65 77 4f 66 66 73 65 74 21    if( newOffset!
b3930 3d 6f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 72  =offset ){.    r
b3940 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20  eturn -1;.  }.  
b3950 67 6f 74 20 3d 20 77 72 69 74 65 28 69 64 2d 3e  got = write(id->
b3960 68 2c 20 70 42 75 66 2c 20 63 6e 74 29 3b 0a 23  h, pBuf, cnt);.#
b3970 65 6e 64 69 66 0a 20 20 54 49 4d 45 52 5f 45 4e  endif.  TIMER_EN
b3980 44 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 57  D;.  OSTRACE5("W
b3990 52 49 54 45 20 20 20 25 2d 33 64 20 25 35 64 20  RITE   %-3d %5d 
b39a0 25 37 6c 6c 64 20 25 64 5c 6e 22 2c 20 69 64 2d  %7lld %d\n", id-
b39b0 3e 68 2c 20 67 6f 74 2c 20 6f 66 66 73 65 74 2c  >h, got, offset,
b39c0 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b   TIMER_ELAPSED);
b39d0 0a 20 20 72 65 74 75 72 6e 20 67 6f 74 3b 0a 7d  .  return got;.}
b39e0 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64  .../*.** Write d
b39f0 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65  ata from a buffe
b3a00 72 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20  r into a file.  
b3a10 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
b3a20 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f   on success.** o
b3a30 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72  r some other err
b3a40 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75  or code on failu
b3a50 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  re..*/.static in
b3a60 74 20 75 6e 69 78 57 72 69 74 65 28 0a 20 20 73  t unixWrite(.  s
b3a70 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
b3a80 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a   .  const void *
b3a90 70 42 75 66 2c 20 0a 20 20 69 6e 74 20 61 6d 74  pBuf, .  int amt
b3aa0 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ,.  sqlite3_int6
b3ab0 34 20 6f 66 66 73 65 74 20 0a 29 7b 0a 20 20 69  4 offset .){.  i
b3ac0 6e 74 20 77 72 6f 74 65 20 3d 20 30 3b 0a 20 20  nt wrote = 0;.  
b3ad0 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20  assert( id );.  
b3ae0 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20 29 3b  assert( amt>0 );
b3af0 0a 20 20 77 68 69 6c 65 28 20 61 6d 74 3e 30 20  .  while( amt>0 
b3b00 26 26 20 28 77 72 6f 74 65 20 3d 20 73 65 65 6b  && (wrote = seek
b3b10 41 6e 64 57 72 69 74 65 28 28 75 6e 69 78 46 69  AndWrite((unixFi
b3b20 6c 65 2a 29 69 64 2c 20 6f 66 66 73 65 74 2c 20  le*)id, offset, 
b3b30 70 42 75 66 2c 20 61 6d 74 29 29 3e 30 20 29 7b  pBuf, amt))>0 ){
b3b40 0a 20 20 20 20 61 6d 74 20 2d 3d 20 77 72 6f 74  .    amt -= wrot
b3b50 65 3b 0a 20 20 20 20 6f 66 66 73 65 74 20 2b 3d  e;.    offset +=
b3b60 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 42 75 66   wrote;.    pBuf
b3b70 20 3d 20 26 28 28 63 68 61 72 2a 29 70 42 75 66   = &((char*)pBuf
b3b80 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 20 20  )[wrote];.  }.  
b3b90 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
b3ba0 28 20 77 72 6f 74 65 3d 28 2d 31 29 2c 20 61 6d  ( wrote=(-1), am
b3bb0 74 3d 31 20 29 29 3b 0a 20 20 53 69 6d 75 6c 61  t=1 ));.  Simula
b3bc0 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28  teDiskfullError(
b3bd0 28 20 77 72 6f 74 65 3d 30 2c 20 61 6d 74 3d 31  ( wrote=0, amt=1
b3be0 20 29 29 3b 0a 20 20 69 66 28 20 61 6d 74 3e 30   ));.  if( amt>0
b3bf0 20 29 7b 0a 20 20 20 20 69 66 28 20 77 72 6f 74   ){.    if( wrot
b3c00 65 3c 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74  e<0 ){.      ret
b3c10 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
b3c20 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73  _WRITE;.    }els
b3c30 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  e{.      return 
b3c40 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20  SQLITE_FULL;.   
b3c50 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
b3c60 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69  SQLITE_OK;.}..#i
b3c70 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
b3c80 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65  ./*.** Count the
b3c90 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 73   number of fulls
b3ca0 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c 20  yncs and normal 
b3cb0 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69 73 20  syncs.  This is 
b3cc0 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20  used to test.** 
b3cd0 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64 20 66  that syncs and f
b3ce0 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f 63 63  ullsyncs are occ
b3cf0 75 72 69 6e 67 20 61 74 20 74 68 65 20 72 69 67  uring at the rig
b3d00 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c  ht times..*/.SQL
b3d10 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
b3d20 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 20 3d  te3_sync_count =
b3d30 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   0;.SQLITE_API i
b3d40 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73  nt sqlite3_fulls
b3d50 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23  ync_count = 0;.#
b3d60 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 55 73 65  endif../*.** Use
b3d70 20 74 68 65 20 66 64 61 74 61 73 79 6e 63 28 29   the fdatasync()
b3d80 20 41 50 49 20 6f 6e 6c 79 20 69 66 20 74 68 65   API only if the
b3d90 20 48 41 56 45 5f 46 44 41 54 41 53 59 4e 43 20   HAVE_FDATASYNC 
b3da0 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64  macro is defined
b3db0 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 75  ..** Otherwise u
b3dc0 73 65 20 66 73 79 6e 63 28 29 20 69 6e 20 69 74  se fsync() in it
b3dd0 73 20 70 6c 61 63 65 2e 0a 2a 2f 0a 23 69 66 6e  s place..*/.#ifn
b3de0 64 65 66 20 48 41 56 45 5f 46 44 41 54 41 53 59  def HAVE_FDATASY
b3df0 4e 43 0a 23 20 64 65 66 69 6e 65 20 66 64 61 74  NC.# define fdat
b3e00 61 73 79 6e 63 20 66 73 79 6e 63 0a 23 65 6e 64  async fsync.#end
b3e10 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65  if../*.** Define
b3e20 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20   HAVE_FULLFSYNC 
b3e30 74 6f 20 30 20 6f 72 20 31 20 64 65 70 65 6e 64  to 0 or 1 depend
b3e40 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 20 6f  ing on whether o
b3e50 72 20 6e 6f 74 0a 2a 2a 20 74 68 65 20 46 5f 46  r not.** the F_F
b3e60 55 4c 4c 46 53 59 4e 43 20 6d 61 63 72 6f 20 69  ULLFSYNC macro i
b3e70 73 20 64 65 66 69 6e 65 64 2e 20 20 46 5f 46 55  s defined.  F_FU
b3e80 4c 4c 46 53 59 4e 43 20 69 73 20 63 75 72 72 65  LLFSYNC is curre
b3e90 6e 74 6c 79 0a 2a 2a 20 6f 6e 6c 79 20 61 76 61  ntly.** only ava
b3ea0 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 20 4f 53  ilable on Mac OS
b3eb0 20 58 2e 20 20 42 75 74 20 74 68 61 74 20 63 6f   X.  But that co
b3ec0 75 6c 64 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a 23  uld change..*/.#
b3ed0 69 66 64 65 66 20 46 5f 46 55 4c 4c 46 53 59 4e  ifdef F_FULLFSYN
b3ee0 43 0a 23 20 64 65 66 69 6e 65 20 48 41 56 45 5f  C.# define HAVE_
b3ef0 46 55 4c 4c 46 53 59 4e 43 20 31 0a 23 65 6c 73  FULLFSYNC 1.#els
b3f00 65 0a 23 20 64 65 66 69 6e 65 20 48 41 56 45 5f  e.# define HAVE_
b3f10 46 55 4c 4c 46 53 59 4e 43 20 30 0a 23 65 6e 64  FULLFSYNC 0.#end
b3f20 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  if.../*.** The f
b3f30 73 79 6e 63 28 29 20 73 79 73 74 65 6d 20 63 61  sync() system ca
b3f40 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b  ll does not work
b3f50 20 61 73 20 61 64 76 65 72 74 69 73 65 64 20 6f   as advertised o
b3f60 6e 20 6d 61 6e 79 0a 2a 2a 20 75 6e 69 78 20 73  n many.** unix s
b3f70 79 73 74 65 6d 73 2e 20 20 54 68 65 20 66 6f 6c  ystems.  The fol
b3f80 6c 6f 77 69 6e 67 20 70 72 6f 63 65 64 75 72 65  lowing procedure
b3f90 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74 20 74   is an attempt t
b3fa0 6f 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 77 6f 72  o make.** it wor
b3fb0 6b 20 62 65 74 74 65 72 2e 0a 2a 2a 0a 2a 2a 20  k better..**.** 
b3fc0 54 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59  The SQLITE_NO_SY
b3fd0 4e 43 20 6d 61 63 72 6f 20 64 69 73 61 62 6c 65  NC macro disable
b3fe0 73 20 61 6c 6c 20 66 73 79 6e 63 28 29 73 2e 20  s all fsync()s. 
b3ff0 20 54 68 69 73 20 69 73 20 75 73 65 66 75 6c 0a   This is useful.
b4000 2a 2a 20 66 6f 72 20 74 65 73 74 69 6e 67 20 77  ** for testing w
b4010 68 65 6e 20 77 65 20 77 61 6e 74 20 74 6f 20 72  hen we want to r
b4020 75 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20 74  un through the t
b4030 65 73 74 20 73 75 69 74 65 20 71 75 69 63 6b 6c  est suite quickl
b4040 79 2e 0a 2a 2a 20 59 6f 75 20 61 72 65 20 73 74  y..** You are st
b4050 72 6f 6e 67 6c 79 20 61 64 76 69 73 65 64 20 2a  rongly advised *
b4060 6e 6f 74 2a 20 74 6f 20 64 65 70 6c 6f 79 20 77  not* to deploy w
b4070 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59  ith SQLITE_NO_SY
b4080 4e 43 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c 20 68  NC.** enabled, h
b4090 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 20 77 69  owever, since wi
b40a0 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e  th SQLITE_NO_SYN
b40b0 43 20 65 6e 61 62 6c 65 64 2c 20 61 6e 20 4f 53  C enabled, an OS
b40c0 20 63 72 61 73 68 0a 2a 2a 20 6f 72 20 70 6f 77   crash.** or pow
b40d0 65 72 20 66 61 69 6c 75 72 65 20 77 69 6c 6c 20  er failure will 
b40e0 6c 69 6b 65 6c 79 20 63 6f 72 72 75 70 74 20 74  likely corrupt t
b40f0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
b4100 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
b4110 66 75 6c 6c 5f 66 73 79 6e 63 28 69 6e 74 20 66  full_fsync(int f
b4120 64 2c 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63 2c  d, int fullSync,
b4130 20 69 6e 74 20 64 61 74 61 4f 6e 6c 79 29 7b 0a   int dataOnly){.
b4140 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20    int rc;..  /* 
b4150 52 65 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 65  Record the numbe
b4160 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20  r of times that 
b4170 77 65 20 64 6f 20 61 20 6e 6f 72 6d 61 6c 20 66  we do a normal f
b4180 73 79 6e 63 28 29 20 61 6e 64 20 0a 20 20 2a 2a  sync() and .  **
b4190 20 46 55 4c 4c 53 59 4e 43 2e 20 20 54 68 69 73   FULLSYNC.  This
b41a0 20 69 73 20 75 73 65 64 20 64 75 72 69 6e 67 20   is used during 
b41b0 74 65 73 74 69 6e 67 20 74 6f 20 76 65 72 69 66  testing to verif
b41c0 79 20 74 68 61 74 20 74 68 69 73 20 70 72 6f 63  y that this proc
b41d0 65 64 75 72 65 0a 20 20 2a 2a 20 67 65 74 73 20  edure.  ** gets 
b41e0 63 61 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20  called with the 
b41f0 63 6f 72 72 65 63 74 20 61 72 67 75 6d 65 6e 74  correct argument
b4200 73 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53  s..  */.#ifdef S
b4210 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28  QLITE_TEST.  if(
b4220 20 66 75 6c 6c 53 79 6e 63 20 29 20 73 71 6c 69   fullSync ) sqli
b4230 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75  te3_fullsync_cou
b4240 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 5f  nt++;.  sqlite3_
b4250 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65  sync_count++;.#e
b4260 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 65  ndif..  /* If we
b4270 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74   compiled with t
b4280 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e  he SQLITE_NO_SYN
b4290 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e  C flag, then syn
b42a0 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e  cing is a.  ** n
b42b0 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66  o-op.  */.#ifdef
b42c0 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a   SQLITE_NO_SYNC.
b42d0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
b42e0 3b 0a 23 65 6c 73 65 0a 0a 23 69 66 20 48 41 56  ;.#else..#if HAV
b42f0 45 5f 46 55 4c 4c 46 53 59 4e 43 0a 20 20 69 66  E_FULLFSYNC.  if
b4300 28 20 66 75 6c 6c 53 79 6e 63 20 29 7b 0a 20 20  ( fullSync ){.  
b4310 20 20 72 63 20 3d 20 66 63 6e 74 6c 28 66 64 2c    rc = fcntl(fd,
b4320 20 46 5f 46 55 4c 4c 46 53 59 4e 43 2c 20 30 29   F_FULLFSYNC, 0)
b4330 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
b4340 63 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 2f 2a 20  c = 1;.  }.  /* 
b4350 49 66 20 74 68 65 20 46 55 4c 4c 46 53 59 4e 43  If the FULLFSYNC
b4360 20 66 61 69 6c 65 64 2c 20 66 61 6c 6c 20 62 61   failed, fall ba
b4370 63 6b 20 74 6f 20 61 74 74 65 6d 70 74 69 6e 67  ck to attempting
b4380 20 61 6e 20 66 73 79 6e 63 28 29 2e 0a 20 20 20   an fsync()..   
b4390 2a 20 49 74 20 73 68 6f 75 6c 64 6e 27 74 20 62  * It shouldn't b
b43a0 65 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 66  e possible for f
b43b0 75 6c 6c 66 73 79 6e 63 20 74 6f 20 66 61 69 6c  ullfsync to fail
b43c0 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 0a 20   on the local . 
b43d0 20 20 2a 20 66 69 6c 65 20 73 79 73 74 65 6d 20    * file system 
b43e0 28 6f 6e 20 4f 53 58 29 2c 20 73 6f 20 66 61 69  (on OSX), so fai
b43f0 6c 75 72 65 20 69 6e 64 69 63 61 74 65 73 20 74  lure indicates t
b4400 68 61 74 20 46 55 4c 4c 46 53 59 4e 43 0a 20 20  hat FULLFSYNC.  
b4410 20 2a 20 69 73 6e 27 74 20 73 75 70 70 6f 72 74   * isn't support
b4420 65 64 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65  ed for this file
b4430 20 73 79 73 74 65 6d 2e 20 53 6f 2c 20 61 74 74   system. So, att
b4440 65 6d 70 74 20 61 6e 20 66 73 79 6e 63 20 0a 20  empt an fsync . 
b4450 20 20 2a 20 61 6e 64 20 28 66 6f 72 20 6e 6f 77    * and (for now
b4460 29 20 69 67 6e 6f 72 65 20 74 68 65 20 6f 76 65  ) ignore the ove
b4470 72 68 65 61 64 20 6f 66 20 61 20 73 75 70 65 72  rhead of a super
b4480 66 6c 75 6f 75 73 20 66 63 6e 74 6c 20 63 61 6c  fluous fcntl cal
b4490 6c 2e 20 20 0a 20 20 20 2a 20 49 74 27 64 20 62  l.  .   * It'd b
b44a0 65 20 62 65 74 74 65 72 20 74 6f 20 64 65 74 65  e better to dete
b44b0 63 74 20 66 75 6c 6c 66 73 79 6e 63 20 73 75 70  ct fullfsync sup
b44c0 70 6f 72 74 20 6f 6e 63 65 20 61 6e 64 20 61 76  port once and av
b44d0 6f 69 64 20 0a 20 20 20 2a 20 74 68 65 20 66 63  oid .   * the fc
b44e0 6e 74 6c 20 63 61 6c 6c 20 65 76 65 72 79 20 74  ntl call every t
b44f0 69 6d 65 20 73 79 6e 63 20 69 73 20 63 61 6c 6c  ime sync is call
b4500 65 64 2e 0a 20 20 20 2a 2f 0a 20 20 69 66 28 20  ed..   */.  if( 
b4510 72 63 20 29 20 72 63 20 3d 20 66 73 79 6e 63 28  rc ) rc = fsync(
b4520 66 64 29 3b 0a 0a 23 65 6c 73 65 20 0a 20 20 69  fd);..#else .  i
b4530 66 28 20 64 61 74 61 4f 6e 6c 79 20 29 7b 0a 20  f( dataOnly ){. 
b4540 20 20 20 72 63 20 3d 20 66 64 61 74 61 73 79 6e     rc = fdatasyn
b4550 63 28 66 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  c(fd);.  }else{.
b4560 20 20 20 20 72 63 20 3d 20 66 73 79 6e 63 28 66      rc = fsync(f
b4570 64 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f  d);.  }.#endif /
b4580 2a 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43  * HAVE_FULLFSYNC
b4590 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65   */.#endif /* de
b45a0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4e 4f 5f  fined(SQLITE_NO_
b45b0 53 59 4e 43 29 20 2a 2f 0a 0a 20 20 72 65 74 75  SYNC) */..  retu
b45c0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
b45d0 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 77 72  Make sure all wr
b45e0 69 74 65 73 20 74 6f 20 61 20 70 61 72 74 69 63  ites to a partic
b45f0 75 6c 61 72 20 66 69 6c 65 20 61 72 65 20 63 6f  ular file are co
b4600 6d 6d 69 74 74 65 64 20 74 6f 20 64 69 73 6b 2e  mmitted to disk.
b4610 0a 2a 2a 0a 2a 2a 20 49 66 20 64 61 74 61 4f 6e  .**.** If dataOn
b4620 6c 79 3d 3d 30 20 74 68 65 6e 20 62 6f 74 68 20  ly==0 then both 
b4630 74 68 65 20 66 69 6c 65 20 69 74 73 65 6c 66 20  the file itself 
b4640 61 6e 64 20 69 74 73 20 6d 65 74 61 64 61 74 61  and its metadata
b4650 20 28 66 69 6c 65 0a 2a 2a 20 73 69 7a 65 2c 20   (file.** size, 
b4660 61 63 63 65 73 73 20 74 69 6d 65 2c 20 65 74 63  access time, etc
b4670 29 20 61 72 65 20 73 79 6e 63 65 64 2e 20 20 49  ) are synced.  I
b4680 66 20 64 61 74 61 4f 6e 6c 79 21 3d 30 20 74 68  f dataOnly!=0 th
b4690 65 6e 20 6f 6e 6c 79 20 74 68 65 0a 2a 2a 20 66  en only the.** f
b46a0 69 6c 65 20 64 61 74 61 20 69 73 20 73 79 6e 63  ile data is sync
b46b0 65 64 2e 0a 2a 2a 0a 2a 2a 20 55 6e 64 65 72 20  ed..**.** Under 
b46c0 55 6e 69 78 2c 20 61 6c 73 6f 20 6d 61 6b 65 20  Unix, also make 
b46d0 73 75 72 65 20 74 68 61 74 20 74 68 65 20 64 69  sure that the di
b46e0 72 65 63 74 6f 72 79 20 65 6e 74 72 79 20 66 6f  rectory entry fo
b46f0 72 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 68 61  r the file.** ha
b4700 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 20 62  s been created b
b4710 79 20 66 73 79 6e 63 2d 69 6e 67 20 74 68 65 20  y fsync-ing the 
b4720 64 69 72 65 63 74 6f 72 79 20 74 68 61 74 20 63  directory that c
b4730 6f 6e 74 61 69 6e 73 20 74 68 65 20 66 69 6c 65  ontains the file
b4740 2e 0a 2a 2a 20 49 66 20 77 65 20 64 6f 20 6e 6f  ..** If we do no
b4750 74 20 64 6f 20 74 68 69 73 20 61 6e 64 20 77 65  t do this and we
b4760 20 65 6e 63 6f 75 6e 74 65 72 20 61 20 70 6f 77   encounter a pow
b4770 65 72 20 66 61 69 6c 75 72 65 2c 20 74 68 65 20  er failure, the 
b4780 64 69 72 65 63 74 6f 72 79 0a 2a 2a 20 65 6e 74  directory.** ent
b4790 72 79 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e  ry for the journ
b47a0 61 6c 20 6d 69 67 68 74 20 6e 6f 74 20 65 78 69  al might not exi
b47b0 73 74 20 61 66 74 65 72 20 77 65 20 72 65 62 6f  st after we rebo
b47c0 6f 74 2e 20 20 54 68 65 20 6e 65 78 74 0a 2a 2a  ot.  The next.**
b47d0 20 53 51 4c 69 74 65 20 74 6f 20 61 63 63 65 73   SQLite to acces
b47e0 73 20 74 68 65 20 66 69 6c 65 20 77 69 6c 6c 20  s the file will 
b47f0 6e 6f 74 20 6b 6e 6f 77 20 74 68 61 74 20 74 68  not know that th
b4800 65 20 6a 6f 75 72 6e 61 6c 20 65 78 69 73 74 73  e journal exists
b4810 20 28 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 65   (because.** the
b4820 20 64 69 72 65 63 74 6f 72 79 20 65 6e 74 72 79   directory entry
b4830 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c   for the journal
b4840 20 77 61 73 20 6e 65 76 65 72 20 63 72 65 61 74   was never creat
b4850 65 64 29 20 61 6e 64 20 74 68 65 20 74 72 61 6e  ed) and the tran
b4860 73 61 63 74 69 6f 6e 0a 2a 2a 20 77 69 6c 6c 20  saction.** will 
b4870 6e 6f 74 20 72 6f 6c 6c 20 62 61 63 6b 20 2d 20  not roll back - 
b4880 70 6f 73 73 69 62 6c 79 20 6c 65 61 64 69 6e 67  possibly leading
b4890 20 74 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72   to database cor
b48a0 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  ruption..*/.stat
b48b0 69 63 20 69 6e 74 20 75 6e 69 78 53 79 6e 63 28  ic int unixSync(
b48c0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
b48d0 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20  , int flags){.  
b48e0 69 6e 74 20 72 63 3b 0a 20 20 75 6e 69 78 46 69  int rc;.  unixFi
b48f0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
b4900 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 69 6e  xFile*)id;..  in
b4910 74 20 69 73 44 61 74 61 4f 6e 6c 79 20 3d 20 28  t isDataOnly = (
b4920 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 53 59 4e  flags&SQLITE_SYN
b4930 43 5f 44 41 54 41 4f 4e 4c 59 29 3b 0a 20 20 69  C_DATAONLY);.  i
b4940 6e 74 20 69 73 46 75 6c 6c 73 79 6e 63 20 3d 20  nt isFullsync = 
b4950 28 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51  (flags&0x0F)==SQ
b4960 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 3b 0a  LITE_SYNC_FULL;.
b4970 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74  .  /* Check that
b4980 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 53   one of SQLITE_S
b4990 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f 72 20 46 55  YNC_NORMAL or FU
b49a0 4c 4c 20 77 61 73 20 70 61 73 73 65 64 20 2a 2f  LL was passed */
b49b0 0a 20 20 61 73 73 65 72 74 28 28 66 6c 61 67 73  .  assert((flags
b49c0 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53  &0x0F)==SQLITE_S
b49d0 59 4e 43 5f 4e 4f 52 4d 41 4c 0a 20 20 20 20 20  YNC_NORMAL.     
b49e0 20 7c 7c 20 28 66 6c 61 67 73 26 30 78 30 46 29   || (flags&0x0F)
b49f0 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55  ==SQLITE_SYNC_FU
b4a00 4c 4c 0a 20 20 29 3b 0a 0a 20 20 61 73 73 65 72  LL.  );..  asser
b4a10 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53  t( pFile );.  OS
b4a20 54 52 41 43 45 32 28 22 53 59 4e 43 20 20 20 20  TRACE2("SYNC    
b4a30 25 2d 33 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  %-3d\n", pFile->
b4a40 68 29 3b 0a 20 20 72 63 20 3d 20 66 75 6c 6c 5f  h);.  rc = full_
b4a50 66 73 79 6e 63 28 70 46 69 6c 65 2d 3e 68 2c 20  fsync(pFile->h, 
b4a60 69 73 46 75 6c 6c 73 79 6e 63 2c 20 69 73 44 61  isFullsync, isDa
b4a70 74 61 4f 6e 6c 79 29 3b 0a 20 20 53 69 6d 75 6c  taOnly);.  Simul
b4a80 61 74 65 49 4f 45 72 72 6f 72 28 20 72 63 3d 31  ateIOError( rc=1
b4a90 20 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a   );.  if( rc ){.
b4aa0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
b4ab0 45 5f 49 4f 45 52 52 5f 46 53 59 4e 43 3b 0a 20  E_IOERR_FSYNC;. 
b4ac0 20 7d 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e   }.  if( pFile->
b4ad0 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20  dirfd>=0 ){.    
b4ae0 4f 53 54 52 41 43 45 34 28 22 44 49 52 53 59 4e  OSTRACE4("DIRSYN
b4af0 43 20 25 2d 33 64 20 28 68 61 76 65 5f 66 75 6c  C %-3d (have_ful
b4b00 6c 66 73 79 6e 63 3d 25 64 20 66 75 6c 6c 73 79  lfsync=%d fullsy
b4b10 6e 63 3d 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65  nc=%d)\n", pFile
b4b20 2d 3e 64 69 72 66 64 2c 0a 20 20 20 20 20 20 20  ->dirfd,.       
b4b30 20 20 20 20 20 48 41 56 45 5f 46 55 4c 4c 46 53       HAVE_FULLFS
b4b40 59 4e 43 2c 20 69 73 46 75 6c 6c 73 79 6e 63 29  YNC, isFullsync)
b4b50 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
b4b60 5f 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43  _DISABLE_DIRSYNC
b4b70 0a 20 20 20 20 2f 2a 20 54 68 65 20 64 69 72 65  .    /* The dire
b4b80 63 74 6f 72 79 20 73 79 6e 63 20 69 73 20 6f 6e  ctory sync is on
b4b90 6c 79 20 61 74 74 65 6d 70 74 65 64 20 69 66 20  ly attempted if 
b4ba0 66 75 6c 6c 5f 66 73 79 6e 63 20 69 73 0a 20 20  full_fsync is.  
b4bb0 20 20 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66 20    ** turned off 
b4bc0 6f 72 20 75 6e 61 76 61 69 6c 61 62 6c 65 2e 20  or unavailable. 
b4bd0 20 49 66 20 61 20 66 75 6c 6c 5f 66 73 79 6e 63   If a full_fsync
b4be0 20 6f 63 63 75 72 72 65 64 20 61 62 6f 76 65 2c   occurred above,
b4bf0 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65  .    ** then the
b4c00 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20   directory sync 
b4c10 69 73 20 73 75 70 65 72 66 6c 75 6f 75 73 2e 0a  is superfluous..
b4c20 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28      */.    if( (
b4c30 21 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20  !HAVE_FULLFSYNC 
b4c40 7c 7c 20 21 69 73 46 75 6c 6c 73 79 6e 63 29 20  || !isFullsync) 
b4c50 26 26 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46  && full_fsync(pF
b4c60 69 6c 65 2d 3e 64 69 72 66 64 2c 30 2c 30 29 20  ile->dirfd,0,0) 
b4c70 29 7b 0a 20 20 20 20 20 20 20 2f 2a 0a 20 20 20  ){.       /*.   
b4c80 20 20 20 20 2a 2a 20 57 65 20 68 61 76 65 20 72      ** We have r
b4c90 65 63 65 69 76 65 64 20 6d 75 6c 74 69 70 6c 65  eceived multiple
b4ca0 20 72 65 70 6f 72 74 73 20 6f 66 20 66 73 79 6e   reports of fsyn
b4cb0 63 28 29 20 72 65 74 75 72 6e 69 6e 67 0a 20 20  c() returning.  
b4cc0 20 20 20 20 20 2a 2a 20 65 72 72 6f 72 73 20 77       ** errors w
b4cd0 68 65 6e 20 61 70 70 6c 69 65 64 20 74 6f 20 64  hen applied to d
b4ce0 69 72 65 63 74 6f 72 69 65 73 20 6f 6e 20 63 65  irectories on ce
b4cf0 72 74 61 69 6e 20 66 69 6c 65 20 73 79 73 74 65  rtain file syste
b4d00 6d 73 2e 0a 20 20 20 20 20 20 20 2a 2a 20 41 20  ms..       ** A 
b4d10 66 61 69 6c 65 64 20 64 69 72 65 63 74 6f 72 79  failed directory
b4d20 20 73 79 6e 63 20 69 73 20 6e 6f 74 20 61 20 62   sync is not a b
b4d30 69 67 20 64 65 61 6c 2e 20 20 53 6f 20 69 74 20  ig deal.  So it 
b4d40 73 65 65 6d 73 0a 20 20 20 20 20 20 20 2a 2a 20  seems.       ** 
b4d50 62 65 74 74 65 72 20 74 6f 20 69 67 6e 6f 72 65  better to ignore
b4d60 20 74 68 65 20 65 72 72 6f 72 2e 20 20 54 69 63   the error.  Tic
b4d70 6b 65 74 20 23 31 36 35 37 0a 20 20 20 20 20 20  ket #1657.      
b4d80 20 2a 2f 0a 20 20 20 20 20 20 20 2f 2a 20 72 65   */.       /* re
b4d90 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
b4da0 52 3b 20 2a 2f 0a 20 20 20 20 7d 0a 23 65 6e 64  R; */.    }.#end
b4db0 69 66 0a 20 20 20 20 63 6c 6f 73 65 28 70 46 69  if.    close(pFi
b4dc0 6c 65 2d 3e 64 69 72 66 64 29 3b 20 20 2f 2a 20  le->dirfd);  /* 
b4dd0 4f 6e 6c 79 20 6e 65 65 64 20 74 6f 20 73 79 6e  Only need to syn
b4de0 63 20 6f 6e 63 65 2c 20 73 6f 20 63 6c 6f 73 65  c once, so close
b4df0 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 2a   the directory *
b4e00 2f 0a 20 20 20 20 70 46 69 6c 65 2d 3e 64 69 72  /.    pFile->dir
b4e10 66 64 20 3d 20 2d 31 3b 20 20 20 20 2f 2a 20 77  fd = -1;    /* w
b4e20 68 65 6e 20 77 65 20 61 72 65 20 64 6f 6e 65 2e  hen we are done.
b4e30 20 2a 2f 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   */.  }.  return
b4e40 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
b4e50 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 61 6e  *.** Truncate an
b4e60 20 6f 70 65 6e 20 66 69 6c 65 20 74 6f 20 61 20   open file to a 
b4e70 73 70 65 63 69 66 69 65 64 20 73 69 7a 65 0a 2a  specified size.*
b4e80 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
b4e90 78 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65  xTruncate(sqlite
b4ea0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20  3_file *id, i64 
b4eb0 6e 42 79 74 65 29 7b 0a 20 20 69 6e 74 20 72 63  nByte){.  int rc
b4ec0 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 20 29  ;.  assert( id )
b4ed0 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
b4ee0 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49  ror( return SQLI
b4ef0 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54  TE_IOERR_TRUNCAT
b4f00 45 20 29 3b 0a 20 20 72 63 20 3d 20 66 74 72 75  E );.  rc = ftru
b4f10 6e 63 61 74 65 28 28 28 75 6e 69 78 46 69 6c 65  ncate(((unixFile
b4f20 2a 29 69 64 29 2d 3e 68 2c 20 28 6f 66 66 5f 74  *)id)->h, (off_t
b4f30 29 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 72  )nByte);.  if( r
b4f40 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  c ){.    return 
b4f50 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55  SQLITE_IOERR_TRU
b4f60 4e 43 41 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a  NCATE;.  }else{.
b4f70 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
b4f80 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  E_OK;.  }.}../*.
b4f90 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65  ** Determine the
b4fa0 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66   current size of
b4fb0 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73   a file in bytes
b4fc0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75  .*/.static int u
b4fd0 6e 69 78 46 69 6c 65 53 69 7a 65 28 73 71 6c 69  nixFileSize(sqli
b4fe0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36  te3_file *id, i6
b4ff0 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74  4 *pSize){.  int
b5000 20 72 63 3b 0a 20 20 73 74 72 75 63 74 20 73 74   rc;.  struct st
b5010 61 74 20 62 75 66 3b 0a 20 20 61 73 73 65 72 74  at buf;.  assert
b5020 28 20 69 64 20 29 3b 0a 20 20 72 63 20 3d 20 66  ( id );.  rc = f
b5030 73 74 61 74 28 28 28 75 6e 69 78 46 69 6c 65 2a  stat(((unixFile*
b5040 29 69 64 29 2d 3e 68 2c 20 26 62 75 66 29 3b 0a  )id)->h, &buf);.
b5050 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
b5060 72 28 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 28  r( rc=1 );.  if(
b5070 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 72 65   rc!=0 ){.    re
b5080 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
b5090 52 5f 46 53 54 41 54 3b 0a 20 20 7d 0a 20 20 2a  R_FSTAT;.  }.  *
b50a0 70 53 69 7a 65 20 3d 20 62 75 66 2e 73 74 5f 73  pSize = buf.st_s
b50b0 69 7a 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ize;.  return SQ
b50c0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
b50d0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
b50e0 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69  hecks if there i
b50f0 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  s a RESERVED loc
b5100 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70  k held on the sp
b5110 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20  ecified.** file 
b5120 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f  by this or any o
b5130 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66  ther process. If
b5140 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20   such a lock is 
b5150 68 65 6c 64 2c 20 72 65 74 75 72 6e 0a 2a 2a 20  held, return.** 
b5160 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 74 68  non-zero.  If th
b5170 65 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b  e file is unlock
b5180 65 64 20 6f 72 20 68 6f 6c 64 73 20 6f 6e 6c 79  ed or holds only
b5190 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 2c 20 74   SHARED locks, t
b51a0 68 65 6e 0a 2a 2a 20 72 65 74 75 72 6e 20 7a 65  hen.** return ze
b51b0 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ro..*/.static in
b51c0 74 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65 72  t unixCheckReser
b51d0 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  vedLock(sqlite3_
b51e0 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74  file *id){.  int
b51f0 20 72 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69   r = 0;.  unixFi
b5200 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
b5210 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73  xFile*)id;..  as
b5220 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20  sert( pFile );. 
b5230 20 65 6e 74 65 72 4d 75 74 65 78 28 29 3b 20 2f   enterMutex(); /
b5240 2a 20 42 65 63 61 75 73 65 20 70 46 69 6c 65 2d  * Because pFile-
b5250 3e 70 4c 6f 63 6b 20 69 73 20 73 68 61 72 65 64  >pLock is shared
b5260 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 20   across threads 
b5270 2a 2f 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69  */..  /* Check i
b5280 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 74 68  f a thread in th
b5290 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73  is process holds
b52a0 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a   such a lock */.
b52b0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4c 6f    if( pFile->pLo
b52c0 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41  ck->locktype>SHA
b52d0 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  RED_LOCK ){.    
b52e0 72 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  r = 1;.  }..  /*
b52f0 20 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69   Otherwise see i
b5300 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f  f some other pro
b5310 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 0a 20  cess holds it.. 
b5320 20 2a 2f 0a 20 20 69 66 28 20 21 72 20 29 7b 0a   */.  if( !r ){.
b5330 20 20 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b      struct flock
b5340 20 6c 6f 63 6b 3b 0a 20 20 20 20 6c 6f 63 6b 2e   lock;.    lock.
b5350 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f  l_whence = SEEK_
b5360 53 45 54 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f  SET;.    lock.l_
b5370 73 74 61 72 74 20 3d 20 52 45 53 45 52 56 45 44  start = RESERVED
b5380 5f 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e  _BYTE;.    lock.
b5390 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 20 20 6c  l_len = 1;.    l
b53a0 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57  ock.l_type = F_W
b53b0 52 4c 43 4b 3b 0a 20 20 20 20 66 63 6e 74 6c 28  RLCK;.    fcntl(
b53c0 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 47 45 54 4c  pFile->h, F_GETL
b53d0 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 69  K, &lock);.    i
b53e0 66 28 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 21 3d  f( lock.l_type!=
b53f0 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 20  F_UNLCK ){.     
b5400 20 72 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20   r = 1;.    }.  
b5410 7d 0a 20 20 0a 20 20 6c 65 61 76 65 4d 75 74 65  }.  .  leaveMute
b5420 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28  x();.  OSTRACE3(
b5430 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64  "TEST WR-LOCK %d
b5440 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68   %d\n", pFile->h
b5450 2c 20 72 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  , r);..  return 
b5460 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b  r;.}../*.** Lock
b5470 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74   the file with t
b5480 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65  he lock specifie
b5490 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c  d by parameter l
b54a0 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a  ocktype - one.**
b54b0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
b54c0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29  g:.**.**     (1)
b54d0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20   SHARED_LOCK.** 
b54e0 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44      (2) RESERVED
b54f0 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29  _LOCK.**     (3)
b5500 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a   PENDING_LOCK.**
b5510 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49       (4) EXCLUSI
b5520 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f  VE_LOCK.**.** So
b5530 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71  metimes when req
b5540 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b  uesting one lock
b5550 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e   state, addition
b5560 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a  al lock states.*
b5570 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69  * are inserted i
b5580 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20  n between.  The 
b5590 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61  locking might fa
b55a0 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65  il on one of the
b55b0 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69   later.** transi
b55c0 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68  tions leaving th
b55d0 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66  e lock state dif
b55e0 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74  ferent from what
b55f0 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a   it started but.
b5600 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f  ** still short o
b5610 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65  f its goal.  The
b5620 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74   following chart
b5630 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77   shows the allow
b5640 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e  ed.** transition
b5650 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74  s and the insert
b5660 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  ed intermediate 
b5670 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  states:.**.**   
b5680 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41   UNLOCKED -> SHA
b5690 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44  RED.**    SHARED
b56a0 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20   -> RESERVED.** 
b56b0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45     SHARED -> (PE
b56c0 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53  NDING) -> EXCLUS
b56d0 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56  IVE.**    RESERV
b56e0 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20  ED -> (PENDING) 
b56f0 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20  -> EXCLUSIVE.** 
b5700 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58     PENDING -> EX
b5710 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68  CLUSIVE.**.** Th
b5720 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
b5730 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20  only increase a 
b5740 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73  lock.  Use the s
b5750 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29  qlite3OsUnlock()
b5760 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c  .** routine to l
b5770 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c  ower a locking l
b5780 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  evel..*/.static 
b5790 69 6e 74 20 75 6e 69 78 4c 6f 63 6b 28 73 71 6c  int unixLock(sql
b57a0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
b57b0 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20  nt locktype){.  
b57c0 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  /* The following
b57d0 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 69   describes the i
b57e0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
b57f0 20 74 68 65 20 76 61 72 69 6f 75 73 20 6c 6f 63   the various loc
b5800 6b 73 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f 63 6b  ks and.  ** lock
b5810 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 69 6e 20   transitions in 
b5820 74 65 72 6d 73 20 6f 66 20 74 68 65 20 50 4f 53  terms of the POS
b5830 49 58 20 61 64 76 69 73 6f 72 79 20 73 68 61 72  IX advisory shar
b5840 65 64 20 61 6e 64 20 65 78 63 6c 75 73 69 76 65  ed and exclusive
b5850 0a 20 20 2a 2a 20 6c 6f 63 6b 20 70 72 69 6d 69  .  ** lock primi
b5860 74 69 76 65 73 20 28 63 61 6c 6c 65 64 20 72 65  tives (called re
b5870 61 64 2d 6c 6f 63 6b 73 20 61 6e 64 20 77 72 69  ad-locks and wri
b5880 74 65 2d 6c 6f 63 6b 73 20 62 65 6c 6f 77 2c 20  te-locks below, 
b5890 74 6f 20 61 76 6f 69 64 0a 20 20 2a 2a 20 63 6f  to avoid.  ** co
b58a0 6e 66 75 73 69 6f 6e 20 77 69 74 68 20 53 51 4c  nfusion with SQL
b58b0 69 74 65 20 6c 6f 63 6b 20 6e 61 6d 65 73 29 2e  ite lock names).
b58c0 20 54 68 65 20 61 6c 67 6f 72 69 74 68 6d 73 20   The algorithms 
b58d0 61 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 0a  are complicated.
b58e0 20 20 2a 2a 20 73 6c 69 67 68 74 6c 79 20 69 6e    ** slightly in
b58f0 20 6f 72 64 65 72 20 74 6f 20 62 65 20 63 6f 6d   order to be com
b5900 70 61 74 69 62 6c 65 20 77 69 74 68 20 77 69 6e  patible with win
b5910 64 6f 77 73 20 73 79 73 74 65 6d 73 20 73 69 6d  dows systems sim
b5920 75 6c 74 61 6e 65 6f 75 73 6c 79 0a 20 20 2a 2a  ultaneously.  **
b5930 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 73   accessing the s
b5940 61 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  ame database fil
b5950 65 2c 20 69 6e 20 63 61 73 65 20 74 68 61 74 20  e, in case that 
b5960 69 73 20 65 76 65 72 20 72 65 71 75 69 72 65 64  is ever required
b5970 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53 79 6d 62  ..  **.  ** Symb
b5980 6f 6c 73 20 64 65 66 69 6e 65 64 20 69 6e 20 6f  ols defined in o
b5990 73 2e 68 20 69 6e 64 65 6e 74 69 66 79 20 74 68  s.h indentify th
b59a0 65 20 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27  e 'pending byte'
b59b0 20 61 6e 64 20 74 68 65 20 27 72 65 73 65 72 76   and the 'reserv
b59c0 65 64 0a 20 20 2a 2a 20 62 79 74 65 27 2c 20 65  ed.  ** byte', e
b59d0 61 63 68 20 73 69 6e 67 6c 65 20 62 79 74 65 73  ach single bytes
b59e0 20 61 74 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f   at well known o
b59f0 66 66 73 65 74 73 2c 20 61 6e 64 20 74 68 65 20  ffsets, and the 
b5a00 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a  'shared byte.  *
b5a10 2a 20 72 61 6e 67 65 27 2c 20 61 20 72 61 6e 67  * range', a rang
b5a20 65 20 6f 66 20 35 31 30 20 62 79 74 65 73 20 61  e of 510 bytes a
b5a30 74 20 61 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f  t a well known o
b5a40 66 66 73 65 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  ffset..  **.  **
b5a50 20 54 6f 20 6f 62 74 61 69 6e 20 61 20 53 48 41   To obtain a SHA
b5a60 52 45 44 20 6c 6f 63 6b 2c 20 61 20 72 65 61 64  RED lock, a read
b5a70 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65  -lock is obtaine
b5a80 64 20 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e  d on the 'pendin
b5a90 67 0a 20 20 2a 2a 20 62 79 74 65 27 2e 20 20 49  g.  ** byte'.  I
b5aa0 66 20 74 68 69 73 20 69 73 20 73 75 63 63 65 73  f this is succes
b5ab0 73 66 75 6c 2c 20 61 20 72 61 6e 64 6f 6d 20 62  sful, a random b
b5ac0 79 74 65 20 66 72 6f 6d 20 74 68 65 20 27 73 68  yte from the 'sh
b5ad0 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72  ared byte.  ** r
b5ae0 61 6e 67 65 27 20 69 73 20 72 65 61 64 2d 6c 6f  ange' is read-lo
b5af0 63 6b 65 64 20 61 6e 64 20 74 68 65 20 6c 6f 63  cked and the loc
b5b00 6b 20 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e  k on the 'pendin
b5b10 67 20 62 79 74 65 27 20 72 65 6c 65 61 73 65 64  g byte' released
b5b20 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72  ..  **.  ** A pr
b5b30 6f 63 65 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f  ocess may only o
b5b40 62 74 61 69 6e 20 61 20 52 45 53 45 52 56 45 44  btain a RESERVED
b5b50 20 6c 6f 63 6b 20 61 66 74 65 72 20 69 74 20 68   lock after it h
b5b60 61 73 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b  as a SHARED lock
b5b70 2e 0a 20 20 2a 2a 20 41 20 52 45 53 45 52 56 45  ..  ** A RESERVE
b5b80 44 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d  D lock is implem
b5b90 65 6e 74 65 64 20 62 79 20 67 72 61 62 62 69 6e  ented by grabbin
b5ba0 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f  g a write-lock o
b5bb0 6e 20 74 68 65 0a 20 20 2a 2a 20 27 72 65 73 65  n the.  ** 'rese
b5bc0 72 76 65 64 20 62 79 74 65 27 2e 20 0a 20 20 2a  rved byte'. .  *
b5bd0 2a 0a 20 20 2a 2a 20 41 20 70 72 6f 63 65 73 73  *.  ** A process
b5be0 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69 6e   may only obtain
b5bf0 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20   a PENDING lock 
b5c00 61 66 74 65 72 20 69 74 20 68 61 73 20 6f 62 74  after it has obt
b5c10 61 69 6e 65 64 20 61 0a 20 20 2a 2a 20 53 48 41  ained a.  ** SHA
b5c20 52 45 44 20 6c 6f 63 6b 2e 20 41 20 50 45 4e 44  RED lock. A PEND
b5c30 49 4e 47 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c  ING lock is impl
b5c40 65 6d 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69  emented by obtai
b5c50 6e 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63  ning a write-loc
b5c60 6b 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 27 70  k.  ** on the 'p
b5c70 65 6e 64 69 6e 67 20 62 79 74 65 27 2e 20 54 68  ending byte'. Th
b5c80 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 20  is ensures that 
b5c90 6e 6f 20 6e 65 77 20 53 48 41 52 45 44 20 6c 6f  no new SHARED lo
b5ca0 63 6b 73 20 63 61 6e 20 62 65 0a 20 20 2a 2a 20  cks can be.  ** 
b5cb0 6f 62 74 61 69 6e 65 64 2c 20 62 75 74 20 65 78  obtained, but ex
b5cc0 69 73 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f  isting SHARED lo
b5cd0 63 6b 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 20  cks are allowed 
b5ce0 74 6f 20 70 65 72 73 69 73 74 2e 20 41 20 70 72  to persist. A pr
b5cf0 6f 63 65 73 73 0a 20 20 2a 2a 20 64 6f 65 73 20  ocess.  ** does 
b5d00 6e 6f 74 20 68 61 76 65 20 74 6f 20 6f 62 74 61  not have to obta
b5d10 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  in a RESERVED lo
b5d20 63 6b 20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f  ck on the way to
b5d30 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e   a PENDING lock.
b5d40 0a 20 20 2a 2a 20 54 68 69 73 20 70 72 6f 70 65  .  ** This prope
b5d50 72 74 79 20 69 73 20 75 73 65 64 20 62 79 20 74  rty is used by t
b5d60 68 65 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72  he algorithm for
b5d70 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20   rolling back a 
b5d80 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a  journal file.  *
b5d90 2a 20 61 66 74 65 72 20 61 20 63 72 61 73 68 2e  * after a crash.
b5da0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 45 58  .  **.  ** An EX
b5db0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2c 20 6f 62  CLUSIVE lock, ob
b5dc0 74 61 69 6e 65 64 20 61 66 74 65 72 20 61 20 50  tained after a P
b5dd0 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 68  ENDING lock is h
b5de0 65 6c 64 2c 20 69 73 0a 20 20 2a 2a 20 69 6d 70  eld, is.  ** imp
b5df0 6c 65 6d 65 6e 74 65 64 20 62 79 20 6f 62 74 61  lemented by obta
b5e00 69 6e 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f  ining a write-lo
b5e10 63 6b 20 6f 6e 20 74 68 65 20 65 6e 74 69 72 65  ck on the entire
b5e20 20 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 20   'shared byte.  
b5e30 2a 2a 20 72 61 6e 67 65 27 2e 20 53 69 6e 63 65  ** range'. Since
b5e40 20 61 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 73   all other locks
b5e50 20 72 65 71 75 69 72 65 20 61 20 72 65 61 64 2d   require a read-
b5e60 6c 6f 63 6b 20 6f 6e 20 6f 6e 65 20 6f 66 20 74  lock on one of t
b5e70 68 65 20 62 79 74 65 73 0a 20 20 2a 2a 20 77 69  he bytes.  ** wi
b5e80 74 68 69 6e 20 74 68 69 73 20 72 61 6e 67 65 2c  thin this range,
b5e90 20 74 68 69 73 20 65 6e 73 75 72 65 73 20 74 68   this ensures th
b5ea0 61 74 20 6e 6f 20 6f 74 68 65 72 20 6c 6f 63 6b  at no other lock
b5eb0 73 20 61 72 65 20 68 65 6c 64 20 6f 6e 20 74 68  s are held on th
b5ec0 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e  e.  ** database.
b5ed0 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20   .  **.  ** The 
b5ee0 72 65 61 73 6f 6e 20 61 20 73 69 6e 67 6c 65 20  reason a single 
b5ef0 62 79 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 75  byte cannot be u
b5f00 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74  sed instead of t
b5f10 68 65 20 27 73 68 61 72 65 64 20 62 79 74 65 0a  he 'shared byte.
b5f20 20 20 2a 2a 20 72 61 6e 67 65 27 20 69 73 20 74    ** range' is t
b5f30 68 61 74 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e  hat some version
b5f40 73 20 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f 20  s of windows do 
b5f50 6e 6f 74 20 73 75 70 70 6f 72 74 20 72 65 61 64  not support read
b5f60 2d 6c 6f 63 6b 73 2e 20 42 79 0a 20 20 2a 2a 20  -locks. By.  ** 
b5f70 6c 6f 63 6b 69 6e 67 20 61 20 72 61 6e 64 6f 6d  locking a random
b5f80 20 62 79 74 65 20 66 72 6f 6d 20 61 20 72 61 6e   byte from a ran
b5f90 67 65 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20 53  ge, concurrent S
b5fa0 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20  HARED locks may 
b5fb0 65 78 69 73 74 0a 20 20 2a 2a 20 65 76 65 6e 20  exist.  ** even 
b5fc0 69 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 70  if the locking p
b5fd0 72 69 6d 69 74 69 76 65 20 75 73 65 64 20 69 73  rimitive used is
b5fe0 20 61 6c 77 61 79 73 20 61 20 77 72 69 74 65 2d   always a write-
b5ff0 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74  lock..  */.  int
b6000 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
b6010 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
b6020 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
b6030 69 64 3b 0a 20 20 73 74 72 75 63 74 20 6c 6f 63  id;.  struct loc
b6040 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 20 3d 20 70  kInfo *pLock = p
b6050 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 73  File->pLock;.  s
b6060 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b  truct flock lock
b6070 3b 0a 20 20 69 6e 74 20 73 3b 0a 0a 20 20 61 73  ;.  int s;..  as
b6080 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20  sert( pFile );. 
b6090 20 4f 53 54 52 41 43 45 37 28 22 4c 4f 43 4b 20   OSTRACE7("LOCK 
b60a0 20 20 20 25 64 20 25 73 20 77 61 73 20 25 73 28     %d %s was %s(
b60b0 25 73 2c 25 64 29 20 70 69 64 3d 25 64 5c 6e 22  %s,%d) pid=%d\n"
b60c0 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20  , pFile->h,.    
b60d0 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c    locktypeName(l
b60e0 6f 63 6b 74 79 70 65 29 2c 20 6c 6f 63 6b 74 79  ocktype), lockty
b60f0 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f  peName(pFile->lo
b6100 63 6b 74 79 70 65 29 2c 0a 20 20 20 20 20 20 6c  cktype),.      l
b6110 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 4c 6f 63  ocktypeName(pLoc
b6120 6b 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 70 4c  k->locktype), pL
b6130 6f 63 6b 2d 3e 63 6e 74 20 2c 20 67 65 74 70 69  ock->cnt , getpi
b6140 64 28 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  d());..  /* If t
b6150 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20  here is already 
b6160 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74  a lock of this t
b6170 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74  ype or more rest
b6180 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20  rictive on the. 
b6190 20 2a 2a 20 75 6e 69 78 46 69 6c 65 2c 20 64 6f   ** unixFile, do
b61a0 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20   nothing. Don't 
b61b0 75 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b  use the end_lock
b61c0 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a  : exit path, as.
b61d0 20 20 2a 2a 20 65 6e 74 65 72 4d 75 74 65 78 28    ** enterMutex(
b61e0 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61  ) hasn't been ca
b61f0 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20  lled yet..  */. 
b6200 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
b6210 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29  type>=locktype )
b6220 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 22  {.    OSTRACE3("
b6230 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 6f 6b  LOCK    %d %s ok
b6240 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64 29 5c   (already held)\
b6250 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20  n", pFile->h,.  
b6260 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79            lockty
b6270 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29  peName(locktype)
b6280 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
b6290 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
b62a0 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  /* Make sure the
b62b0 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e 63   locking sequenc
b62c0 65 20 69 73 20 63 6f 72 72 65 63 74 0a 20 20 2a  e is correct.  *
b62d0 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  /.  assert( pFil
b62e0 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f  e->locktype!=NO_
b62f0 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65  LOCK || locktype
b6300 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b  ==SHARED_LOCK );
b6310 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
b6320 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43  ype!=PENDING_LOC
b6330 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c  K );.  assert( l
b6340 6f 63 6b 74 79 70 65 21 3d 52 45 53 45 52 56 45  ocktype!=RESERVE
b6350 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d  D_LOCK || pFile-
b6360 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45  >locktype==SHARE
b6370 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20  D_LOCK );..  /* 
b6380 54 68 69 73 20 6d 75 74 65 78 20 69 73 20 6e 65  This mutex is ne
b6390 65 64 65 64 20 62 65 63 61 75 73 65 20 70 46 69  eded because pFi
b63a0 6c 65 2d 3e 70 4c 6f 63 6b 20 69 73 20 73 68 61  le->pLock is sha
b63b0 72 65 64 20 61 63 72 6f 73 73 20 74 68 72 65 61  red across threa
b63c0 64 73 0a 20 20 2a 2f 0a 20 20 65 6e 74 65 72 4d  ds.  */.  enterM
b63d0 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61  utex();..  /* Ma
b63e0 6b 65 20 73 75 72 65 20 74 68 65 20 63 75 72 72  ke sure the curr
b63f0 65 6e 74 20 74 68 72 65 61 64 20 6f 77 6e 73 20  ent thread owns 
b6400 74 68 65 20 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a  the pFile..  */.
b6410 20 20 72 63 20 3d 20 74 72 61 6e 73 66 65 72 4f    rc = transferO
b6420 77 6e 65 72 73 68 69 70 28 70 46 69 6c 65 29 3b  wnership(pFile);
b6430 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
b6440 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 6c 65 61 76  E_OK ){.    leav
b6450 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65  eMutex();.    re
b6460 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70  turn rc;.  }.  p
b6470 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c  Lock = pFile->pL
b6480 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 73 6f  ock;..  /* If so
b6490 6d 65 20 74 68 72 65 61 64 20 75 73 69 6e 67 20  me thread using 
b64a0 74 68 69 73 20 50 49 44 20 68 61 73 20 61 20 6c  this PID has a l
b64b0 6f 63 6b 20 76 69 61 20 61 20 64 69 66 66 65 72  ock via a differ
b64c0 65 6e 74 20 75 6e 69 78 46 69 6c 65 2a 0a 20 20  ent unixFile*.  
b64d0 2a 2a 20 68 61 6e 64 6c 65 20 74 68 61 74 20 70  ** handle that p
b64e0 72 65 63 6c 75 64 65 73 20 74 68 65 20 72 65 71  recludes the req
b64f0 75 65 73 74 65 64 20 6c 6f 63 6b 2c 20 72 65 74  uested lock, ret
b6500 75 72 6e 20 42 55 53 59 2e 0a 20 20 2a 2f 0a 20  urn BUSY..  */. 
b6510 20 69 66 28 20 28 70 46 69 6c 65 2d 3e 6c 6f 63   if( (pFile->loc
b6520 6b 74 79 70 65 21 3d 70 4c 6f 63 6b 2d 3e 6c 6f  ktype!=pLock->lo
b6530 63 6b 74 79 70 65 20 26 26 20 0a 20 20 20 20 20  cktype && .     
b6540 20 20 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63       (pLock->loc
b6550 6b 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c  ktype>=PENDING_L
b6560 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3e  OCK || locktype>
b6570 53 48 41 52 45 44 5f 4c 4f 43 4b 29 29 0a 20 20  SHARED_LOCK)).  
b6580 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
b6590 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f 74  TE_BUSY;.    got
b65a0 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a  o end_lock;.  }.
b65b0 0a 20 20 2f 2a 20 49 66 20 61 20 53 48 41 52 45  .  /* If a SHARE
b65c0 44 20 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73  D lock is reques
b65d0 74 65 64 2c 20 61 6e 64 20 73 6f 6d 65 20 74 68  ted, and some th
b65e0 72 65 61 64 20 75 73 69 6e 67 20 74 68 69 73 20  read using this 
b65f0 50 49 44 20 61 6c 72 65 61 64 79 0a 20 20 2a 2a  PID already.  **
b6600 20 68 61 73 20 61 20 53 48 41 52 45 44 20 6f 72   has a SHARED or
b6610 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c 20   RESERVED lock, 
b6620 74 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 72  then increment r
b6630 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 73 20  eference counts 
b6640 61 6e 64 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20  and.  ** return 
b6650 53 51 4c 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2f 0a  SQLITE_OK..  */.
b6660 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d    if( locktype==
b6670 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 0a  SHARED_LOCK && .
b6680 20 20 20 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f        (pLock->lo
b6690 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
b66a0 4f 43 4b 20 7c 7c 20 70 4c 6f 63 6b 2d 3e 6c 6f  OCK || pLock->lo
b66b0 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44  cktype==RESERVED
b66c0 5f 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 20 61 73  _LOCK) ){.    as
b66d0 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3d 3d  sert( locktype==
b66e0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20  SHARED_LOCK );. 
b66f0 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65     assert( pFile
b6700 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29 3b  ->locktype==0 );
b6710 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f  .    assert( pLo
b6720 63 6b 2d 3e 63 6e 74 3e 30 20 29 3b 0a 20 20 20  ck->cnt>0 );.   
b6730 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
b6740 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a   = SHARED_LOCK;.
b6750 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2b 2b      pLock->cnt++
b6760 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70  ;.    pFile->pOp
b6770 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20  en->nLock++;.   
b6780 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a   goto end_lock;.
b6790 20 20 7d 0a 0a 20 20 6c 6f 63 6b 2e 6c 5f 6c 65    }..  lock.l_le
b67a0 6e 20 3d 20 31 4c 3b 0a 0a 20 20 6c 6f 63 6b 2e  n = 1L;..  lock.
b67b0 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f  l_whence = SEEK_
b67c0 53 45 54 3b 0a 0a 20 20 2f 2a 20 41 20 50 45 4e  SET;..  /* A PEN
b67d0 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65  DING lock is nee
b67e0 64 65 64 20 62 65 66 6f 72 65 20 61 63 71 75 69  ded before acqui
b67f0 72 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f  ring a SHARED lo
b6800 63 6b 20 61 6e 64 20 62 65 66 6f 72 65 0a 20 20  ck and before.  
b6810 2a 2a 20 61 63 71 75 69 72 69 6e 67 20 61 6e 20  ** acquiring an 
b6820 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20  EXCLUSIVE lock. 
b6830 20 46 6f 72 20 74 68 65 20 53 48 41 52 45 44 20   For the SHARED 
b6840 6c 6f 63 6b 2c 20 74 68 65 20 50 45 4e 44 49 4e  lock, the PENDIN
b6850 47 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72  G will.  ** be r
b6860 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20  eleased..  */.  
b6870 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  if( locktype==SH
b6880 41 52 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20  ARED_LOCK .     
b6890 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45   || (locktype==E
b68a0 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26  XCLUSIVE_LOCK &&
b68b0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
b68c0 3c 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20  <PENDING_LOCK). 
b68d0 20 29 7b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74   ){.    lock.l_t
b68e0 79 70 65 20 3d 20 28 6c 6f 63 6b 74 79 70 65 3d  ype = (locktype=
b68f0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 3f 46 5f 52  =SHARED_LOCK?F_R
b6900 44 4c 43 4b 3a 46 5f 57 52 4c 43 4b 29 3b 0a 20  DLCK:F_WRLCK);. 
b6910 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20     lock.l_start 
b6920 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a  = PENDING_BYTE;.
b6930 20 20 20 20 73 20 3d 20 66 63 6e 74 6c 28 70 46      s = fcntl(pF
b6940 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c  ile->h, F_SETLK,
b6950 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28   &lock);.    if(
b6960 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20   s==(-1) ){.    
b6970 20 20 72 63 20 3d 20 28 65 72 72 6e 6f 3d 3d 45    rc = (errno==E
b6980 49 4e 56 41 4c 29 20 3f 20 53 51 4c 49 54 45 5f  INVAL) ? SQLITE_
b6990 4e 4f 4c 46 53 20 3a 20 53 51 4c 49 54 45 5f 42  NOLFS : SQLITE_B
b69a0 55 53 59 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  USY;.      goto 
b69b0 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a  end_lock;.    }.
b69c0 20 20 7d 0a 0a 0a 20 20 2f 2a 20 49 66 20 63 6f    }...  /* If co
b69d0 6e 74 72 6f 6c 20 67 65 74 73 20 74 6f 20 74 68  ntrol gets to th
b69e0 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61  is point, then a
b69f0 63 74 75 61 6c 6c 79 20 67 6f 20 61 68 65 61 64  ctually go ahead
b6a00 20 61 6e 64 20 6d 61 6b 65 0a 20 20 2a 2a 20 6f   and make.  ** o
b6a10 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
b6a20 63 61 6c 6c 73 20 66 6f 72 20 74 68 65 20 73 70  calls for the sp
b6a30 65 63 69 66 69 65 64 20 6c 6f 63 6b 2e 0a 20 20  ecified lock..  
b6a40 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70  */.  if( locktyp
b6a50 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e==SHARED_LOCK )
b6a60 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c  {.    assert( pL
b6a70 6f 63 6b 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a 20  ock->cnt==0 );. 
b6a80 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b     assert( pLock
b6a90 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29 3b  ->locktype==0 );
b6aa0 0a 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74  ..    /* Now get
b6ab0 20 74 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 2a   the read-lock *
b6ac0 2f 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61  /.    lock.l_sta
b6ad0 72 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53  rt = SHARED_FIRS
b6ae0 54 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65  T;.    lock.l_le
b6af0 6e 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b  n = SHARED_SIZE;
b6b00 0a 20 20 20 20 73 20 3d 20 66 63 6e 74 6c 28 70  .    s = fcntl(p
b6b10 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b  File->h, F_SETLK
b6b20 2c 20 26 6c 6f 63 6b 29 3b 0a 0a 20 20 20 20 2f  , &lock);..    /
b6b30 2a 20 44 72 6f 70 20 74 68 65 20 74 65 6d 70 6f  * Drop the tempo
b6b40 72 61 72 79 20 50 45 4e 44 49 4e 47 20 6c 6f 63  rary PENDING loc
b6b50 6b 20 2a 2f 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f  k */.    lock.l_
b6b60 73 74 61 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f  start = PENDING_
b6b70 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c  BYTE;.    lock.l
b6b80 5f 6c 65 6e 20 3d 20 31 4c 3b 0a 20 20 20 20 6c  _len = 1L;.    l
b6b90 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55  ock.l_type = F_U
b6ba0 4e 4c 43 4b 3b 0a 20 20 20 20 69 66 28 20 66 63  NLCK;.    if( fc
b6bb0 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f  ntl(pFile->h, F_
b6bc0 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 30  SETLK, &lock)!=0
b6bd0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
b6be0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f  QLITE_IOERR_UNLO
b6bf0 43 4b 3b 20 20 2f 2a 20 54 68 69 73 20 73 68 6f  CK;  /* This sho
b6c00 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e  uld never happen
b6c10 20 2a 2f 0a 20 20 20 20 20 20 67 6f 74 6f 20 65   */.      goto e
b6c20 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20  nd_lock;.    }. 
b6c30 20 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 29     if( s==(-1) )
b6c40 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 28 65 72  {.      rc = (er
b6c50 72 6e 6f 3d 3d 45 49 4e 56 41 4c 29 20 3f 20 53  rno==EINVAL) ? S
b6c60 51 4c 49 54 45 5f 4e 4f 4c 46 53 20 3a 20 53 51  QLITE_NOLFS : SQ
b6c70 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d  LITE_BUSY;.    }
b6c80 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 69 6c  else{.      pFil
b6c90 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48  e->locktype = SH
b6ca0 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20  ARED_LOCK;.     
b6cb0 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e   pFile->pOpen->n
b6cc0 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 20 20 70 4c  Lock++;.      pL
b6cd0 6f 63 6b 2d 3e 63 6e 74 20 3d 20 31 3b 0a 20 20  ock->cnt = 1;.  
b6ce0 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
b6cf0 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53  locktype==EXCLUS
b6d00 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 4c 6f 63  IVE_LOCK && pLoc
b6d10 6b 2d 3e 63 6e 74 3e 31 20 29 7b 0a 20 20 20 20  k->cnt>1 ){.    
b6d20 2f 2a 20 57 65 20 61 72 65 20 74 72 79 69 6e 67  /* We are trying
b6d30 20 66 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 76   for an exclusiv
b6d40 65 20 6c 6f 63 6b 20 62 75 74 20 61 6e 6f 74 68  e lock but anoth
b6d50 65 72 20 74 68 72 65 61 64 20 69 6e 20 74 68 69  er thread in thi
b6d60 73 0a 20 20 20 20 2a 2a 20 73 61 6d 65 20 70 72  s.    ** same pr
b6d70 6f 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20 68  ocess is still h
b6d80 6f 6c 64 69 6e 67 20 61 20 73 68 61 72 65 64 20  olding a shared 
b6d90 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 20 20 72 63 20  lock. */.    rc 
b6da0 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  = SQLITE_BUSY;. 
b6db0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54   }else{.    /* T
b6dc0 68 65 20 72 65 71 75 65 73 74 20 77 61 73 20 66  he request was f
b6dd0 6f 72 20 61 20 52 45 53 45 52 56 45 44 20 6f 72  or a RESERVED or
b6de0 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e   EXCLUSIVE lock.
b6df0 20 20 49 74 20 69 73 0a 20 20 20 20 2a 2a 20 61    It is.    ** a
b6e00 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 72  ssumed that ther
b6e10 65 20 69 73 20 61 20 53 48 41 52 45 44 20 6f 72  e is a SHARED or
b6e20 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e   greater lock on
b6e30 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a   the file.    **
b6e40 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f   already..    */
b6e50 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 21 3d  .    assert( 0!=
b6e60 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
b6e70 29 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79  );.    lock.l_ty
b6e80 70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 20  pe = F_WRLCK;.  
b6e90 20 20 73 77 69 74 63 68 28 20 6c 6f 63 6b 74 79    switch( lockty
b6ea0 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65  pe ){.      case
b6eb0 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 0a   RESERVED_LOCK:.
b6ec0 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73          lock.l_s
b6ed0 74 61 72 74 20 3d 20 52 45 53 45 52 56 45 44 5f  tart = RESERVED_
b6ee0 42 59 54 45 3b 0a 20 20 20 20 20 20 20 20 62 72  BYTE;.        br
b6ef0 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
b6f00 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a 0a  EXCLUSIVE_LOCK:.
b6f10 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73          lock.l_s
b6f20 74 61 72 74 20 3d 20 53 48 41 52 45 44 5f 46 49  tart = SHARED_FI
b6f30 52 53 54 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63  RST;.        loc
b6f40 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 45 44  k.l_len = SHARED
b6f50 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20 20 20 62  _SIZE;.        b
b6f60 72 65 61 6b 3b 0a 20 20 20 20 20 20 64 65 66 61  reak;.      defa
b6f70 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 61 73 73  ult:.        ass
b6f80 65 72 74 28 30 29 3b 0a 20 20 20 20 7d 0a 20 20  ert(0);.    }.  
b6f90 20 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c    s = fcntl(pFil
b6fa0 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26  e->h, F_SETLK, &
b6fb0 6c 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 73  lock);.    if( s
b6fc0 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  ==(-1) ){.      
b6fd0 72 63 20 3d 20 28 65 72 72 6e 6f 3d 3d 45 49 4e  rc = (errno==EIN
b6fe0 56 41 4c 29 20 3f 20 53 51 4c 49 54 45 5f 4e 4f  VAL) ? SQLITE_NO
b6ff0 4c 46 53 20 3a 20 53 51 4c 49 54 45 5f 42 55 53  LFS : SQLITE_BUS
b7000 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a  Y;.    }.  }.  .
b7010 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
b7020 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65  _OK ){.    pFile
b7030 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63  ->locktype = loc
b7040 6b 74 79 70 65 3b 0a 20 20 20 20 70 4c 6f 63 6b  ktype;.    pLock
b7050 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63  ->locktype = loc
b7060 6b 74 79 70 65 3b 0a 20 20 7d 65 6c 73 65 20 69  ktype;.  }else i
b7070 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43  f( locktype==EXC
b7080 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20  LUSIVE_LOCK ){. 
b7090 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
b70a0 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43  pe = PENDING_LOC
b70b0 4b 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f  K;.    pLock->lo
b70c0 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47  cktype = PENDING
b70d0 5f 4c 4f 43 4b 3b 0a 20 20 7d 0a 0a 65 6e 64 5f  _LOCK;.  }..end_
b70e0 6c 6f 63 6b 3a 0a 20 20 6c 65 61 76 65 4d 75 74  lock:.  leaveMut
b70f0 65 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45 34  ex();.  OSTRACE4
b7100 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20  ("LOCK    %d %s 
b7110 25 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %s\n", pFile->h,
b7120 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f   locktypeName(lo
b7130 63 6b 74 79 70 65 29 2c 20 0a 20 20 20 20 20 20  cktype), .      
b7140 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20  rc==SQLITE_OK ? 
b7150 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29  "ok" : "failed")
b7160 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
b7170 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68  ../*.** Lower th
b7180 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
b7190 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  on file descript
b71a0 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b  or pFile to lock
b71b0 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a  type.  locktype.
b71c0 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65  ** must be eithe
b71d0 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41  r NO_LOCK or SHA
b71e0 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20  RED_LOCK..**.** 
b71f0 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  If the locking l
b7200 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65  evel of the file
b7210 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61   descriptor is a
b7220 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c  lready at or bel
b7230 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73  ow.** the reques
b7240 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  ted locking leve
b7250 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  l, this routine 
b7260 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73  is a no-op..*/.s
b7270 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 55 6e  tatic int unixUn
b7280 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  lock(sqlite3_fil
b7290 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74  e *id, int lockt
b72a0 79 70 65 29 7b 0a 20 20 73 74 72 75 63 74 20 6c  ype){.  struct l
b72b0 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 3b 0a  ockInfo *pLock;.
b72c0 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c    struct flock l
b72d0 6f 63 6b 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ock;.  int rc = 
b72e0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69  SQLITE_OK;.  uni
b72f0 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
b7300 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20  unixFile*)id;.  
b7310 69 6e 74 20 68 3b 0a 0a 20 20 61 73 73 65 72 74  int h;..  assert
b7320 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54  ( pFile );.  OST
b7330 52 41 43 45 37 28 22 55 4e 4c 4f 43 4b 20 20 25  RACE7("UNLOCK  %
b7340 64 20 25 64 20 77 61 73 20 25 64 28 25 64 2c 25  d %d was %d(%d,%
b7350 64 29 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46  d) pid=%d\n", pF
b7360 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65  ile->h, locktype
b7370 2c 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  ,.      pFile->l
b7380 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e  ocktype, pFile->
b7390 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 2c  pLock->locktype,
b73a0 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 63   pFile->pLock->c
b73b0 6e 74 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a  nt, getpid());..
b73c0 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79    assert( lockty
b73d0 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe<=SHARED_LOCK 
b73e0 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  );.  if( pFile->
b73f0 6c 6f 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79  locktype<=lockty
b7400 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  pe ){.    return
b7410 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
b7420 20 20 69 66 28 20 43 48 45 43 4b 5f 54 48 52 45    if( CHECK_THRE
b7430 41 44 49 44 28 70 46 69 6c 65 29 20 29 7b 0a 20  ADID(pFile) ){. 
b7440 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
b7450 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 65  _MISUSE;.  }.  e
b7460 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 68  nterMutex();.  h
b7470 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 70   = pFile->h;.  p
b7480 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c  Lock = pFile->pL
b7490 6f 63 6b 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ock;.  assert( p
b74a0 4c 6f 63 6b 2d 3e 63 6e 74 21 3d 30 20 29 3b 0a  Lock->cnt!=0 );.
b74b0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63    if( pFile->loc
b74c0 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43  ktype>SHARED_LOC
b74d0 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  K ){.    assert(
b74e0 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65   pLock->locktype
b74f0 3d 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ==pFile->locktyp
b7500 65 20 29 3b 0a 20 20 20 20 53 69 6d 75 6c 61 74  e );.    Simulat
b7510 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 31  eIOErrorBenign(1
b7520 29 3b 0a 20 20 20 20 53 69 6d 75 6c 61 74 65 49  );.    SimulateI
b7530 4f 45 72 72 6f 72 28 20 68 3d 28 2d 31 29 20 29  OError( h=(-1) )
b7540 0a 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  .    SimulateIOE
b7550 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 20  rrorBenign(0);. 
b7560 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d     if( locktype=
b7570 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a  =SHARED_LOCK ){.
b7580 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70        lock.l_typ
b7590 65 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 20  e = F_RDLCK;.   
b75a0 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65     lock.l_whence
b75b0 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20   = SEEK_SET;.   
b75c0 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20     lock.l_start 
b75d0 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a  = SHARED_FIRST;.
b75e0 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e        lock.l_len
b75f0 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a   = SHARED_SIZE;.
b7600 20 20 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28        if( fcntl(
b7610 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63  h, F_SETLK, &loc
b7620 6b 29 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20  k)==(-1) ){.    
b7630 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
b7640 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 3b 0a 20 20  IOERR_RDLOCK;.  
b7650 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
b7660 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f  lock.l_type = F_
b7670 55 4e 4c 43 4b 3b 0a 20 20 20 20 6c 6f 63 6b 2e  UNLCK;.    lock.
b7680 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f  l_whence = SEEK_
b7690 53 45 54 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f  SET;.    lock.l_
b76a0 73 74 61 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f  start = PENDING_
b76b0 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c  BYTE;.    lock.l
b76c0 5f 6c 65 6e 20 3d 20 32 4c 3b 20 20 61 73 73 65  _len = 2L;  asse
b76d0 72 74 28 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  rt( PENDING_BYTE
b76e0 2b 31 3d 3d 52 45 53 45 52 56 45 44 5f 42 59 54  +1==RESERVED_BYT
b76f0 45 20 29 3b 0a 20 20 20 20 69 66 28 20 66 63 6e  E );.    if( fcn
b7700 74 6c 28 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26  tl(h, F_SETLK, &
b7710 6c 6f 63 6b 29 21 3d 28 2d 31 29 20 29 7b 0a 20  lock)!=(-1) ){. 
b7720 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b       pLock->lock
b7730 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f  type = SHARED_LO
b7740 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  CK;.    }else{. 
b7750 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
b7760 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 0a 20  _IOERR_UNLOCK;. 
b7770 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6c     }.  }.  if( l
b7780 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b  ocktype==NO_LOCK
b7790 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 6f   ){.    struct o
b77a0 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 0a 0a  penCnt *pOpen;..
b77b0 20 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74      /* Decrement
b77c0 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b   the shared lock
b77d0 20 63 6f 75 6e 74 65 72 2e 20 20 52 65 6c 65 61   counter.  Relea
b77e0 73 65 20 74 68 65 20 6c 6f 63 6b 20 75 73 69 6e  se the lock usin
b77f0 67 20 61 6e 0a 20 20 20 20 2a 2a 20 4f 53 20 63  g an.    ** OS c
b7800 61 6c 6c 20 6f 6e 6c 79 20 77 68 65 6e 20 61 6c  all only when al
b7810 6c 20 74 68 72 65 61 64 73 20 69 6e 20 74 68 69  l threads in thi
b7820 73 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20 68  s same process h
b7830 61 76 65 20 72 65 6c 65 61 73 65 64 0a 20 20 20  ave released.   
b7840 20 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e 0a 20 20   ** the lock..  
b7850 20 20 2a 2f 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e    */.    pLock->
b7860 63 6e 74 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70  cnt--;.    if( p
b7870 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d 30 20 29 7b 0a  Lock->cnt==0 ){.
b7880 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70        lock.l_typ
b7890 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20  e = F_UNLCK;.   
b78a0 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65     lock.l_whence
b78b0 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20   = SEEK_SET;.   
b78c0 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20     lock.l_start 
b78d0 3d 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30  = lock.l_len = 0
b78e0 4c 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74  L;.      Simulat
b78f0 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 31  eIOErrorBenign(1
b7900 29 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74  );.      Simulat
b7910 65 49 4f 45 72 72 6f 72 28 20 68 3d 28 2d 31 29  eIOError( h=(-1)
b7920 20 29 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74   ).      Simulat
b7930 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30  eIOErrorBenign(0
b7940 29 3b 0a 20 20 20 20 20 20 69 66 28 20 66 63 6e  );.      if( fcn
b7950 74 6c 28 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26  tl(h, F_SETLK, &
b7960 6c 6f 63 6b 29 21 3d 28 2d 31 29 20 29 7b 0a 20  lock)!=(-1) ){. 
b7970 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f         pLock->lo
b7980 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b  cktype = NO_LOCK
b7990 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
b79a0 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
b79b0 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b  TE_IOERR_UNLOCK;
b79c0 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e  .        pLock->
b79d0 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  cnt = 1;.      }
b79e0 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44  .    }..    /* D
b79f0 65 63 72 65 6d 65 6e 74 20 74 68 65 20 63 6f 75  ecrement the cou
b7a00 6e 74 20 6f 66 20 6c 6f 63 6b 73 20 61 67 61 69  nt of locks agai
b7a10 6e 73 74 20 74 68 69 73 20 73 61 6d 65 20 66 69  nst this same fi
b7a20 6c 65 2e 20 20 57 68 65 6e 20 74 68 65 0a 20 20  le.  When the.  
b7a30 20 20 2a 2a 20 63 6f 75 6e 74 20 72 65 61 63 68    ** count reach
b7a40 65 73 20 7a 65 72 6f 2c 20 63 6c 6f 73 65 20 61  es zero, close a
b7a50 6e 79 20 6f 74 68 65 72 20 66 69 6c 65 20 64 65  ny other file de
b7a60 73 63 72 69 70 74 6f 72 73 20 77 68 6f 73 65 20  scriptors whose 
b7a70 63 6c 6f 73 65 0a 20 20 20 20 2a 2a 20 77 61 73  close.    ** was
b7a80 20 64 65 66 65 72 72 65 64 20 62 65 63 61 75 73   deferred becaus
b7a90 65 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e 67  e of outstanding
b7aa0 20 6c 6f 63 6b 73 2e 0a 20 20 20 20 2a 2f 0a 20   locks..    */. 
b7ab0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
b7ac0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 4f  E_OK ){.      pO
b7ad0 70 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70  pen = pFile->pOp
b7ae0 65 6e 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d  en;.      pOpen-
b7af0 3e 6e 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 20 20  >nLock--;.      
b7b00 61 73 73 65 72 74 28 20 70 4f 70 65 6e 2d 3e 6e  assert( pOpen->n
b7b10 4c 6f 63 6b 3e 3d 30 20 29 3b 0a 20 20 20 20 20  Lock>=0 );.     
b7b20 20 69 66 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63   if( pOpen->nLoc
b7b30 6b 3d 3d 30 20 26 26 20 70 4f 70 65 6e 2d 3e 6e  k==0 && pOpen->n
b7b40 50 65 6e 64 69 6e 67 3e 30 20 29 7b 0a 20 20 20  Pending>0 ){.   
b7b50 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20       int i;.    
b7b60 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
b7b70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 3b 20  Open->nPending; 
b7b80 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
b7b90 63 6c 6f 73 65 28 70 4f 70 65 6e 2d 3e 61 50 65  close(pOpen->aPe
b7ba0 6e 64 69 6e 67 5b 69 5d 29 3b 0a 20 20 20 20 20  nding[i]);.     
b7bb0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 66 72 65     }.        fre
b7bc0 65 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e  e(pOpen->aPendin
b7bd0 67 29 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 65  g);.        pOpe
b7be0 6e 2d 3e 6e 50 65 6e 64 69 6e 67 20 3d 20 30 3b  n->nPending = 0;
b7bf0 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e  .        pOpen->
b7c00 61 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20  aPending = 0;.  
b7c10 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
b7c20 20 20 6c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a    leaveMutex();.
b7c30 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
b7c40 5f 4f 4b 20 29 20 70 46 69 6c 65 2d 3e 6c 6f 63  _OK ) pFile->loc
b7c50 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65  ktype = locktype
b7c60 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
b7c70 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20  ../*.** Close a 
b7c80 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
b7c90 69 6e 74 20 75 6e 69 78 43 6c 6f 73 65 28 73 71  int unixClose(sq
b7ca0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b  lite3_file *id){
b7cb0 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
b7cc0 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a  le = (unixFile *
b7cd0 29 69 64 3b 0a 20 20 69 66 28 20 21 70 46 69 6c  )id;.  if( !pFil
b7ce0 65 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  e ) return SQLIT
b7cf0 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 55 6e 6c 6f  E_OK;.  unixUnlo
b7d00 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b  ck(id, NO_LOCK);
b7d10 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 64 69  .  if( pFile->di
b7d20 72 66 64 3e 3d 30 20 29 20 63 6c 6f 73 65 28 70  rfd>=0 ) close(p
b7d30 46 69 6c 65 2d 3e 64 69 72 66 64 29 3b 0a 20 20  File->dirfd);.  
b7d40 70 46 69 6c 65 2d 3e 64 69 72 66 64 20 3d 20 2d  pFile->dirfd = -
b7d50 31 3b 0a 20 20 65 6e 74 65 72 4d 75 74 65 78 28  1;.  enterMutex(
b7d60 29 3b 0a 0a 20 20 69 66 28 20 70 46 69 6c 65 2d  );..  if( pFile-
b7d70 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 20 29 7b  >pOpen->nLock ){
b7d80 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65  .    /* If there
b7d90 20 61 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67   are outstanding
b7da0 20 6c 6f 63 6b 73 2c 20 64 6f 20 6e 6f 74 20 61   locks, do not a
b7db0 63 74 75 61 6c 6c 79 20 63 6c 6f 73 65 20 74 68  ctually close th
b7dc0 65 20 66 69 6c 65 20 6a 75 73 74 0a 20 20 20 20  e file just.    
b7dd0 2a 2a 20 79 65 74 20 62 65 63 61 75 73 65 20 74  ** yet because t
b7de0 68 61 74 20 77 6f 75 6c 64 20 63 6c 65 61 72 20  hat would clear 
b7df0 74 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 20 49 6e  those locks.  In
b7e00 73 74 65 61 64 2c 20 61 64 64 20 74 68 65 20 66  stead, add the f
b7e10 69 6c 65 0a 20 20 20 20 2a 2a 20 64 65 73 63 72  ile.    ** descr
b7e20 69 70 74 6f 72 20 74 6f 20 70 4f 70 65 6e 2d 3e  iptor to pOpen->
b7e30 61 50 65 6e 64 69 6e 67 2e 20 20 49 74 20 77 69  aPending.  It wi
b7e40 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61  ll be automatica
b7e50 6c 6c 79 20 63 6c 6f 73 65 64 20 77 68 65 6e 0a  lly closed when.
b7e60 20 20 20 20 2a 2a 20 74 68 65 20 6c 61 73 74 20      ** the last 
b7e70 6c 6f 63 6b 20 69 73 20 63 6c 65 61 72 65 64 2e  lock is cleared.
b7e80 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20  .    */.    int 
b7e90 2a 61 4e 65 77 3b 0a 20 20 20 20 73 74 72 75 63  *aNew;.    struc
b7ea0 74 20 6f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e  t openCnt *pOpen
b7eb0 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b   = pFile->pOpen;
b7ec0 0a 20 20 20 20 61 4e 65 77 20 3d 20 72 65 61 6c  .    aNew = real
b7ed0 6c 6f 63 28 20 70 4f 70 65 6e 2d 3e 61 50 65 6e  loc( pOpen->aPen
b7ee0 64 69 6e 67 2c 20 28 70 4f 70 65 6e 2d 3e 6e 50  ding, (pOpen->nP
b7ef0 65 6e 64 69 6e 67 2b 31 29 2a 73 69 7a 65 6f 66  ending+1)*sizeof
b7f00 28 69 6e 74 29 20 29 3b 0a 20 20 20 20 69 66 28  (int) );.    if(
b7f10 20 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20   aNew==0 ){.    
b7f20 20 20 2f 2a 20 49 66 20 61 20 6d 61 6c 6c 6f 63    /* If a malloc
b7f30 20 66 61 69 6c 73 2c 20 6a 75 73 74 20 6c 65 61   fails, just lea
b7f40 6b 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72  k the file descr
b7f50 69 70 74 6f 72 20 2a 2f 0a 20 20 20 20 7d 65 6c  iptor */.    }el
b7f60 73 65 7b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d  se{.      pOpen-
b7f70 3e 61 50 65 6e 64 69 6e 67 20 3d 20 61 4e 65 77  >aPending = aNew
b7f80 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61  ;.      pOpen->a
b7f90 50 65 6e 64 69 6e 67 5b 70 4f 70 65 6e 2d 3e 6e  Pending[pOpen->n
b7fa0 50 65 6e 64 69 6e 67 5d 20 3d 20 70 46 69 6c 65  Pending] = pFile
b7fb0 2d 3e 68 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e  ->h;.      pOpen
b7fc0 2d 3e 6e 50 65 6e 64 69 6e 67 2b 2b 3b 0a 20 20  ->nPending++;.  
b7fd0 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
b7fe0 20 2f 2a 20 54 68 65 72 65 20 61 72 65 20 6e 6f   /* There are no
b7ff0 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63   outstanding loc
b8000 6b 73 20 73 6f 20 77 65 20 63 61 6e 20 63 6c 6f  ks so we can clo
b8010 73 65 20 74 68 65 20 66 69 6c 65 20 69 6d 6d 65  se the file imme
b8020 64 69 61 74 65 6c 79 20 2a 2f 0a 20 20 20 20 63  diately */.    c
b8030 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 3b 0a  lose(pFile->h);.
b8040 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 4c 6f 63    }.  releaseLoc
b8050 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f  kInfo(pFile->pLo
b8060 63 6b 29 3b 0a 20 20 72 65 6c 65 61 73 65 4f 70  ck);.  releaseOp
b8070 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70  enCnt(pFile->pOp
b8080 65 6e 29 3b 0a 0a 20 20 6c 65 61 76 65 4d 75 74  en);..  leaveMut
b8090 65 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45 32  ex();.  OSTRACE2
b80a0 28 22 43 4c 4f 53 45 20 20 20 25 2d 33 64 5c 6e  ("CLOSE   %-3d\n
b80b0 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20  ", pFile->h);.  
b80c0 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b  OpenCounter(-1);
b80d0 0a 20 20 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c  .  memset(pFile,
b80e0 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78 46   0, sizeof(unixF
b80f0 69 6c 65 29 29 3b 0a 20 20 72 65 74 75 72 6e 20  ile));.  return 
b8100 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23  SQLITE_OK;.}...#
b8110 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
b8120 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
b8130 45 0a 23 70 72 61 67 6d 61 20 6d 61 72 6b 20 41  E.#pragma mark A
b8140 46 50 20 53 75 70 70 6f 72 74 0a 0a 2f 2a 0a 20  FP Support../*. 
b8150 2a 2a 20 54 68 65 20 61 66 70 4c 6f 63 6b 69 6e  ** The afpLockin
b8160 67 43 6f 6e 74 65 78 74 20 73 74 72 75 63 74 75  gContext structu
b8170 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20  re contains all 
b8180 61 66 70 20 6c 6f 63 6b 20 73 70 65 63 69 66 69  afp lock specifi
b8190 63 20 73 74 61 74 65 0a 20 2a 2f 0a 74 79 70 65  c state. */.type
b81a0 64 65 66 20 73 74 72 75 63 74 20 61 66 70 4c 6f  def struct afpLo
b81b0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 61 66 70  ckingContext afp
b81c0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a  LockingContext;.
b81d0 73 74 72 75 63 74 20 61 66 70 4c 6f 63 6b 69 6e  struct afpLockin
b81e0 67 43 6f 6e 74 65 78 74 20 7b 0a 20 20 75 6e 73  gContext {.  uns
b81f0 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20  igned long long 
b8200 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 3b 0a  sharedLockByte;.
b8210 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69    const char *fi
b8220 6c 65 50 61 74 68 3b 0a 7d 3b 0a 0a 73 74 72 75  lePath;.};..stru
b8230 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b  ct ByteRangeLock
b8240 50 42 32 0a 7b 0a 20 20 75 6e 73 69 67 6e 65 64  PB2.{.  unsigned
b8250 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65   long long offse
b8260 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 6f 66 66  t;        /* off
b8270 73 65 74 20 74 6f 20 66 69 72 73 74 20 62 79 74  set to first byt
b8280 65 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75  e to lock */.  u
b8290 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
b82a0 67 20 6c 65 6e 67 74 68 3b 20 20 20 20 20 20 20  g length;       
b82b0 20 2f 2a 20 6e 62 72 20 6f 66 20 62 79 74 65 73   /* nbr of bytes
b82c0 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e   to lock */.  un
b82d0 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
b82e0 20 72 65 74 52 61 6e 67 65 53 74 61 72 74 3b 20   retRangeStart; 
b82f0 2f 2a 20 6e 62 72 20 6f 66 20 31 73 74 20 62 79  /* nbr of 1st by
b8300 74 65 20 6c 6f 63 6b 65 64 20 69 66 20 73 75 63  te locked if suc
b8310 63 65 73 73 66 75 6c 20 2a 2f 0a 20 20 75 6e 73  cessful */.  uns
b8320 69 67 6e 65 64 20 63 68 61 72 20 75 6e 4c 6f 63  igned char unLoc
b8330 6b 46 6c 61 67 3b 20 20 20 20 20 20 20 20 20 2f  kFlag;         /
b8340 2a 20 31 20 3d 20 75 6e 6c 6f 63 6b 2c 20 30 20  * 1 = unlock, 0 
b8350 3d 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69  = lock */.  unsi
b8360 67 6e 65 64 20 63 68 61 72 20 73 74 61 72 74 45  gned char startE
b8370 6e 64 46 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a  ndFlag;       /*
b8380 20 31 3d 72 65 6c 20 74 6f 20 65 6e 64 20 6f 66   1=rel to end of
b8390 20 66 6f 72 6b 2c 20 30 3d 72 65 6c 20 74 6f 20   fork, 0=rel to 
b83a0 73 74 61 72 74 20 2a 2f 0a 20 20 69 6e 74 20 66  start */.  int f
b83b0 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
b83c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
b83d0 66 69 6c 65 20 64 65 73 63 20 74 6f 20 61 73 73  file desc to ass
b83e0 6f 63 20 74 68 69 73 20 6c 6f 63 6b 20 77 69 74  oc this lock wit
b83f0 68 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65  h */.};..#define
b8400 20 61 66 70 66 73 42 79 74 65 52 61 6e 67 65 4c   afpfsByteRangeL
b8410 6f 63 6b 32 46 53 43 54 4c 20 20 20 20 20 20 20  ock2FSCTL       
b8420 20 5f 49 4f 57 52 28 27 7a 27 2c 20 32 33 2c 20   _IOWR('z', 23, 
b8430 73 74 72 75 63 74 20 42 79 74 65 52 61 6e 67 65  struct ByteRange
b8440 4c 6f 63 6b 50 42 32 29 0a 0a 2f 2a 20 0a 2a 2a  LockPB2)../* .**
b8450 20 52 65 74 75 72 6e 20 30 20 6f 6e 20 73 75 63   Return 0 on suc
b8460 63 65 73 73 2c 20 31 20 6f 6e 20 66 61 69 6c 75  cess, 1 on failu
b8470 72 65 2e 20 20 54 6f 20 6d 61 74 63 68 20 74 68  re.  To match th
b8480 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68  e behavior of th
b8490 65 20 0a 2a 2a 20 6e 6f 72 6d 61 6c 20 70 6f 73  e .** normal pos
b84a0 69 78 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20  ix file locking 
b84b0 28 75 73 65 64 20 69 6e 20 75 6e 69 78 4c 6f 63  (used in unixLoc
b84c0 6b 20 66 6f 72 20 65 78 61 6d 70 6c 65 29 2c 20  k for example), 
b84d0 77 65 20 73 68 6f 75 6c 64 20 0a 2a 2a 20 70 72  we should .** pr
b84e0 6f 76 69 64 65 20 27 72 69 63 68 65 72 27 20 72  ovide 'richer' r
b84f0 65 74 75 72 6e 20 63 6f 64 65 73 20 2d 20 73 70  eturn codes - sp
b8500 65 63 69 66 69 63 61 6c 6c 79 20 74 6f 20 64 69  ecifically to di
b8510 66 66 65 72 65 6e 74 69 61 74 65 20 62 65 74 77  fferentiate betw
b8520 65 65 6e 0a 2a 2a 20 27 66 69 6c 65 20 62 75 73  een.** 'file bus
b8530 79 27 20 61 6e 64 20 27 66 69 6c 65 20 73 79 73  y' and 'file sys
b8540 74 65 6d 20 65 72 72 6f 72 27 20 72 65 73 75 6c  tem error' resul
b8550 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ts..*/.static in
b8560 74 20 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28  t _AFPFSSetLock(
b8570 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  .  const char *p
b8580 61 74 68 2c 20 0a 20 20 69 6e 74 20 66 64 2c 20  ath, .  int fd, 
b8590 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67  .  unsigned long
b85a0 20 6c 6f 6e 67 20 6f 66 66 73 65 74 2c 20 0a 20   long offset, . 
b85b0 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c   unsigned long l
b85c0 6f 6e 67 20 6c 65 6e 67 74 68 2c 20 0a 20 20 69  ong length, .  i
b85d0 6e 74 20 73 65 74 4c 6f 63 6b 46 6c 61 67 0a 29  nt setLockFlag.)
b85e0 7b 0a 20 20 73 74 72 75 63 74 20 42 79 74 65 52  {.  struct ByteR
b85f0 61 6e 67 65 4c 6f 63 6b 50 42 32 20 20 20 20 20  angeLockPB2     
b8600 20 20 70 62 3b 0a 20 20 69 6e 74 20 20 20 20 20    pb;.  int     
b8610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b8620 65 72 72 3b 0a 20 20 0a 20 20 70 62 2e 75 6e 4c  err;.  .  pb.unL
b8630 6f 63 6b 46 6c 61 67 20 3d 20 73 65 74 4c 6f 63  ockFlag = setLoc
b8640 6b 46 6c 61 67 20 3f 20 30 20 3a 20 31 3b 0a 20  kFlag ? 0 : 1;. 
b8650 20 70 62 2e 73 74 61 72 74 45 6e 64 46 6c 61 67   pb.startEndFlag
b8660 20 3d 20 30 3b 0a 20 20 70 62 2e 6f 66 66 73 65   = 0;.  pb.offse
b8670 74 20 3d 20 6f 66 66 73 65 74 3b 0a 20 20 70 62  t = offset;.  pb
b8680 2e 6c 65 6e 67 74 68 20 3d 20 6c 65 6e 67 74 68  .length = length
b8690 3b 20 0a 20 20 70 62 2e 66 64 20 3d 20 66 64 3b  ; .  pb.fd = fd;
b86a0 0a 20 20 4f 53 54 52 41 43 45 35 28 22 41 46 50  .  OSTRACE5("AFP
b86b0 4c 4f 43 4b 20 73 65 74 74 69 6e 67 20 6c 6f 63  LOCK setting loc
b86c0 6b 20 25 73 20 66 6f 72 20 25 64 20 69 6e 20 72  k %s for %d in r
b86d0 61 6e 67 65 20 25 6c 6c 78 3a 25 6c 6c 78 5c 6e  ange %llx:%llx\n
b86e0 22 2c 20 0a 20 20 20 20 28 73 65 74 4c 6f 63 6b  ", .    (setLock
b86f0 46 6c 61 67 3f 22 4f 4e 22 3a 22 4f 46 46 22 29  Flag?"ON":"OFF")
b8700 2c 20 66 64 2c 20 6f 66 66 73 65 74 2c 20 6c 65  , fd, offset, le
b8710 6e 67 74 68 29 3b 0a 20 20 65 72 72 20 3d 20 66  ngth);.  err = f
b8720 73 63 74 6c 28 70 61 74 68 2c 20 61 66 70 66 73  sctl(path, afpfs
b8730 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 32 46 53  ByteRangeLock2FS
b8740 43 54 4c 2c 20 26 70 62 2c 20 30 29 3b 0a 20 20  CTL, &pb, 0);.  
b8750 69 66 20 28 20 65 72 72 3d 3d 2d 31 20 29 20 7b  if ( err==-1 ) {
b8760 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28 22 41  .    OSTRACE4("A
b8770 46 50 4c 4f 43 4b 20 66 61 69 6c 65 64 20 74 6f  FPLOCK failed to
b8780 20 66 73 63 74 6c 28 29 20 27 25 73 27 20 25 64   fsctl() '%s' %d
b8790 20 25 73 5c 6e 22 2c 20 70 61 74 68 2c 20 65 72   %s\n", path, er
b87a0 72 6e 6f 2c 20 0a 20 20 20 20 20 20 73 74 72 65  rno, .      stre
b87b0 72 72 6f 72 28 65 72 72 6e 6f 29 29 3b 0a 20 20  rror(errno));.  
b87c0 20 20 72 65 74 75 72 6e 20 31 3b 20 2f 2a 20 65    return 1; /* e
b87d0 72 72 6f 72 20 2a 2f 0a 20 20 7d 20 65 6c 73 65  rror */.  } else
b87e0 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b   {.    return 0;
b87f0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 20 2a 2a 20 54  .  }.}../*. ** T
b8800 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63  his routine chec
b8810 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61  ks if there is a
b8820 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68   RESERVED lock h
b8830 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69  eld on the speci
b8840 66 69 65 64 0a 20 2a 2a 20 66 69 6c 65 20 62 79  fied. ** file by
b8850 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68   this or any oth
b8860 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73  er process. If s
b8870 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65  uch a lock is he
b8880 6c 64 2c 20 72 65 74 75 72 6e 0a 20 2a 2a 20 6e  ld, return. ** n
b8890 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 74 68 65  on-zero.  If the
b88a0 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65   file is unlocke
b88b0 64 20 6f 72 20 68 6f 6c 64 73 20 6f 6e 6c 79 20  d or holds only 
b88c0 53 48 41 52 45 44 20 6c 6f 63 6b 73 2c 20 74 68  SHARED locks, th
b88d0 65 6e 0a 20 2a 2a 20 72 65 74 75 72 6e 20 7a 65  en. ** return ze
b88e0 72 6f 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 20 69  ro.. */.static i
b88f0 6e 74 20 61 66 70 55 6e 69 78 43 68 65 63 6b 52  nt afpUnixCheckR
b8900 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69  eservedLock(sqli
b8910 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20  te3_file *id){. 
b8920 20 69 6e 74 20 72 20 3d 20 30 3b 0a 20 20 75 6e   int r = 0;.  un
b8930 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
b8940 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20  (unixFile*)id;. 
b8950 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c   .  assert( pFil
b8960 65 20 29 3b 20 0a 20 20 61 66 70 4c 6f 63 6b 69  e ); .  afpLocki
b8970 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  ngContext *conte
b8980 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b 69 6e 67  xt = (afpLocking
b8990 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 6c 65  Context *) pFile
b89a0 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
b89b0 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20  ;.  .  /* Check 
b89c0 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 74  if a thread in t
b89d0 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64  his process hold
b89e0 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f  s such a lock */
b89f0 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
b8a00 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f  cktype>SHARED_LO
b8a10 43 4b 20 29 7b 0a 20 20 20 20 72 20 3d 20 31 3b  CK ){.    r = 1;
b8a20 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 4f 74 68  .  }.  .  /* Oth
b8a30 65 72 77 69 73 65 20 73 65 65 20 69 66 20 73 6f  erwise see if so
b8a40 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  me other process
b8a50 20 68 6f 6c 64 73 20 69 74 2e 0a 20 20 20 2a 2f   holds it..   */
b8a60 0a 20 20 69 66 20 28 20 21 72 20 29 20 7b 0a 20  .  if ( !r ) {. 
b8a70 20 20 20 2f 2a 20 6c 6f 63 6b 20 74 68 65 20 62     /* lock the b
b8a80 79 74 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 66  yte */.    int f
b8a90 61 69 6c 65 64 20 3d 20 5f 41 46 50 46 53 53 65  ailed = _AFPFSSe
b8aa0 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66  tLock(context->f
b8ab0 69 6c 65 50 61 74 68 2c 20 70 46 69 6c 65 2d 3e  ilePath, pFile->
b8ac0 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45  h, RESERVED_BYTE
b8ad0 2c 20 31 2c 31 29 3b 20 20 0a 20 20 20 20 69 66  , 1,1);  .    if
b8ae0 20 28 66 61 69 6c 65 64 29 20 7b 0a 20 20 20 20   (failed) {.    
b8af0 20 20 2f 2a 20 69 66 20 77 65 20 66 61 69 6c 65    /* if we faile
b8b00 64 20 74 6f 20 67 65 74 20 74 68 65 20 6c 6f 63  d to get the loc
b8b10 6b 20 74 68 65 6e 20 73 6f 6d 65 6f 6e 65 20 65  k then someone e
b8b20 6c 73 65 20 6d 75 73 74 20 68 61 76 65 20 69 74  lse must have it
b8b30 20 2a 2f 0a 20 20 20 20 20 20 72 20 3d 20 31 3b   */.      r = 1;
b8b40 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20  .    } else {.  
b8b50 20 20 20 20 2f 2a 20 69 66 20 77 65 20 73 75 63      /* if we suc
b8b60 63 65 65 64 65 64 20 69 6e 20 74 61 6b 69 6e 67  ceeded in taking
b8b70 20 74 68 65 20 72 65 73 65 72 76 65 64 20 6c 6f   the reserved lo
b8b80 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 20 74 6f  ck, unlock it to
b8b90 20 72 65 73 74 6f 72 65 0a 20 20 20 20 20 20 2a   restore.      *
b8ba0 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 73  * the original s
b8bb0 74 61 74 65 20 2a 2f 0a 20 20 20 20 20 20 5f 41  tate */.      _A
b8bc0 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74  FPFSSetLock(cont
b8bd0 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70  ext->filePath, p
b8be0 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45  File->h, RESERVE
b8bf0 44 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20  D_BYTE, 1, 0);. 
b8c00 20 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 54 52 41     }.  }.  OSTRA
b8c10 43 45 33 28 22 54 45 53 54 20 57 52 2d 4c 4f 43  CE3("TEST WR-LOC
b8c20 4b 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c  K %d %d\n", pFil
b8c30 65 2d 3e 68 2c 20 72 29 3b 0a 20 20 0a 20 20 72  e->h, r);.  .  r
b8c40 65 74 75 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a 20 41  eturn r;.}../* A
b8c50 46 50 2d 73 74 79 6c 65 20 6c 6f 63 6b 69 6e 67  FP-style locking
b8c60 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 62   following the b
b8c70 65 68 61 76 69 6f 72 20 6f 66 20 75 6e 69 78 4c  ehavior of unixL
b8c80 6f 63 6b 2c 20 73 65 65 20 74 68 65 20 75 6e 69  ock, see the uni
b8c90 78 4c 6f 63 6b 20 0a 2a 2a 20 66 75 6e 63 74 69  xLock .** functi
b8ca0 6f 6e 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20  on comments for 
b8cb0 64 65 74 61 69 6c 73 20 6f 66 20 6c 6f 63 6b 20  details of lock 
b8cc0 6d 61 6e 61 67 65 6d 65 6e 74 2e 20 2a 2f 0a 73  management. */.s
b8cd0 74 61 74 69 63 20 69 6e 74 20 61 66 70 55 6e 69  tatic int afpUni
b8ce0 78 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  xLock(sqlite3_fi
b8cf0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b  le *id, int lock
b8d00 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 20  type){.  int rc 
b8d10 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75  = SQLITE_OK;.  u
b8d20 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
b8d30 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
b8d40 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74    afpLockingCont
b8d50 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 28  ext *context = (
b8d60 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  afpLockingContex
b8d70 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  t *) pFile->lock
b8d80 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 69 6e  ingContext;.  in
b8d90 74 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b  t gotPendingLock
b8da0 20 3d 20 30 3b 0a 20 20 0a 20 20 61 73 73 65 72   = 0;.  .  asser
b8db0 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53  t( pFile );.  OS
b8dc0 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 20 20 20  TRACE5("LOCK    
b8dd0 25 64 20 25 73 20 77 61 73 20 25 73 20 70 69 64  %d %s was %s pid
b8de0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  =%d\n", pFile->h
b8df0 2c 0a 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74  ,.         lockt
b8e00 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65  ypeName(locktype
b8e10 29 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28  ), locktypeName(
b8e20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29  pFile->locktype)
b8e30 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20  , getpid());..  
b8e40 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61  /* If there is a
b8e50 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66  lready a lock of
b8e60 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d 6f   this type or mo
b8e70 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20 6f  re restrictive o
b8e80 6e 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 78 46  n the.  ** unixF
b8e90 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e  ile, do nothing.
b8ea0 20 44 6f 6e 27 74 20 75 73 65 20 74 68 65 20 61   Don't use the a
b8eb0 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69  fp_end_lock: exi
b8ec0 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20  t path, as.  ** 
b8ed0 65 6e 74 65 72 4d 75 74 65 78 28 29 20 68 61 73  enterMutex() has
b8ee0 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20  n't been called 
b8ef0 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  yet..  */.  if( 
b8f00 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e  pFile->locktype>
b8f10 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20  =locktype ){.   
b8f20 20 4f 53 54 52 41 43 45 33 28 22 4c 4f 43 4b 20   OSTRACE3("LOCK 
b8f30 20 20 20 25 64 20 25 73 20 6f 6b 20 28 61 6c 72     %d %s ok (alr
b8f40 65 61 64 79 20 68 65 6c 64 29 5c 6e 22 2c 20 70  eady held)\n", p
b8f50 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20  File->h,.       
b8f60 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65      locktypeName
b8f70 28 6c 6f 63 6b 74 79 70 65 29 29 3b 0a 20 20 20  (locktype));.   
b8f80 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
b8f90 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b  K;.  }..  /* Mak
b8fa0 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69  e sure the locki
b8fb0 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63  ng sequence is c
b8fc0 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73  orrect.  */.  as
b8fd0 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63  sert( pFile->loc
b8fe0 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c  ktype!=NO_LOCK |
b8ff0 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  | locktype==SHAR
b9000 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73  ED_LOCK );.  ass
b9010 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50  ert( locktype!=P
b9020 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20  ENDING_LOCK );. 
b9030 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
b9040 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e!=RESERVED_LOCK
b9050 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74   || pFile->lockt
b9060 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
b9070 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 54 68 69 73   );.  .  /* This
b9080 20 6d 75 74 65 78 20 69 73 20 6e 65 65 64 65 64   mutex is needed
b9090 20 62 65 63 61 75 73 65 20 70 46 69 6c 65 2d 3e   because pFile->
b90a0 70 4c 6f 63 6b 20 69 73 20 73 68 61 72 65 64 20  pLock is shared 
b90b0 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 0a 20  across threads. 
b90c0 20 2a 2f 0a 20 20 65 6e 74 65 72 4d 75 74 65 78   */.  enterMutex
b90d0 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73  ();..  /* Make s
b90e0 75 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ure the current 
b90f0 74 68 72 65 61 64 20 6f 77 6e 73 20 74 68 65 20  thread owns the 
b9100 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63  pFile..  */.  rc
b9110 20 3d 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72   = transferOwner
b9120 73 68 69 70 28 70 46 69 6c 65 29 3b 0a 20 20 69  ship(pFile);.  i
b9130 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
b9140 20 29 7b 0a 20 20 20 20 6c 65 61 76 65 4d 75 74   ){.    leaveMut
b9150 65 78 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ex();.    return
b9160 20 72 63 3b 0a 20 20 7d 0a 20 20 20 20 0a 20 20   rc;.  }.    .  
b9170 2f 2a 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63  /* A PENDING loc
b9180 6b 20 69 73 20 6e 65 65 64 65 64 20 62 65 66 6f  k is needed befo
b9190 72 65 20 61 63 71 75 69 72 69 6e 67 20 61 20 53  re acquiring a S
b91a0 48 41 52 45 44 20 6c 6f 63 6b 20 61 6e 64 20 62  HARED lock and b
b91b0 65 66 6f 72 65 0a 20 20 2a 2a 20 61 63 71 75 69  efore.  ** acqui
b91c0 72 69 6e 67 20 61 6e 20 45 58 43 4c 55 53 49 56  ring an EXCLUSIV
b91d0 45 20 6c 6f 63 6b 2e 20 20 46 6f 72 20 74 68 65  E lock.  For the
b91e0 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68   SHARED lock, th
b91f0 65 20 50 45 4e 44 49 4e 47 20 77 69 6c 6c 0a 20  e PENDING will. 
b9200 20 2a 2a 20 62 65 20 72 65 6c 65 61 73 65 64 2e   ** be released.
b9210 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b  .  */.  if( lock
b9220 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
b9230 4b 20 0a 20 20 20 20 20 20 7c 7c 20 28 6c 6f 63  K .      || (loc
b9240 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45  ktype==EXCLUSIVE
b9250 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e  _LOCK && pFile->
b9260 6c 6f 63 6b 74 79 70 65 3c 50 45 4e 44 49 4e 47  locktype<PENDING
b9270 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20 20 20  _LOCK).  ){.    
b9280 69 6e 74 20 66 61 69 6c 65 64 3b 0a 20 20 20 20  int failed;.    
b9290 66 61 69 6c 65 64 20 3d 20 5f 41 46 50 46 53 53  failed = _AFPFSS
b92a0 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e  etLock(context->
b92b0 66 69 6c 65 50 61 74 68 2c 20 70 46 69 6c 65 2d  filePath, pFile-
b92c0 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  >h, PENDING_BYTE
b92d0 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 69 66 20  , 1, 1);.    if 
b92e0 28 66 61 69 6c 65 64 29 20 7b 0a 20 20 20 20 20  (failed) {.     
b92f0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
b9300 59 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 66  Y;.      goto af
b9310 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20  p_end_lock;.    
b9320 7d 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 49 66  }.  }.  .  /* If
b9330 20 63 6f 6e 74 72 6f 6c 20 67 65 74 73 20 74 6f   control gets to
b9340 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65   this point, the
b9350 6e 20 61 63 74 75 61 6c 6c 79 20 67 6f 20 61 68  n actually go ah
b9360 65 61 64 20 61 6e 64 20 6d 61 6b 65 0a 20 20 2a  ead and make.  *
b9370 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  * operating syst
b9380 65 6d 20 63 61 6c 6c 73 20 66 6f 72 20 74 68 65  em calls for the
b9390 20 73 70 65 63 69 66 69 65 64 20 6c 6f 63 6b 2e   specified lock.
b93a0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b  .  */.  if( lock
b93b0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
b93c0 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 6c 6b 2c  K ){.    int lk,
b93d0 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 69 6e 74   failed;.    int
b93e0 20 74 72 69 65 73 20 3d 20 30 3b 0a 20 20 20 20   tries = 0;.    
b93f0 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20  .    /* Now get 
b9400 74 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 2a 2f  the read-lock */
b9410 0a 20 20 20 20 2f 2a 20 6e 6f 74 65 20 74 68 61  .    /* note tha
b9420 74 20 74 68 65 20 71 75 61 6c 69 74 79 20 6f 66  t the quality of
b9430 20 74 68 65 20 72 61 6e 64 6f 6d 6e 65 73 73 20   the randomness 
b9440 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 74  doesn't matter t
b9450 68 61 74 20 6d 75 63 68 20 2a 2f 0a 20 20 20 20  hat much */.    
b9460 6c 6b 20 3d 20 72 61 6e 64 6f 6d 28 29 3b 20 0a  lk = random(); .
b9470 20 20 20 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61      context->sha
b9480 72 65 64 4c 6f 63 6b 42 79 74 65 20 3d 20 28 6c  redLockByte = (l
b9490 6b 20 26 20 30 78 37 66 66 66 66 66 66 66 29 25  k & 0x7fffffff)%
b94a0 28 53 48 41 52 45 44 5f 53 49 5a 45 20 2d 20 31  (SHARED_SIZE - 1
b94b0 29 3b 0a 20 20 20 20 66 61 69 6c 65 64 20 3d 20  );.    failed = 
b94c0 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f  _AFPFSSetLock(co
b94d0 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c  ntext->filePath,
b94e0 20 70 46 69 6c 65 2d 3e 68 2c 20 0a 20 20 20 20   pFile->h, .    
b94f0 20 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b 63    SHARED_FIRST+c
b9500 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 4c 6f  ontext->sharedLo
b9510 63 6b 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20  ckByte, 1, 1);. 
b9520 20 20 20 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20     .    /* Drop 
b9530 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 50 45  the temporary PE
b9540 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20  NDING lock */.  
b9550 20 20 69 66 20 28 5f 41 46 50 46 53 53 65 74 4c    if (_AFPFSSetL
b9560 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c  ock(context->fil
b9570 65 50 61 74 68 2c 20 70 46 69 6c 65 2d 3e 68 2c  ePath, pFile->h,
b9580 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31   PENDING_BYTE, 1
b9590 2c 20 30 29 29 20 7b 0a 20 20 20 20 20 20 72 63  , 0)) {.      rc
b95a0 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
b95b0 55 4e 4c 4f 43 4b 3b 20 20 2f 2a 20 54 68 69 73  UNLOCK;  /* This
b95c0 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 61   should never ha
b95d0 70 70 65 6e 20 2a 2f 0a 20 20 20 20 20 20 67 6f  ppen */.      go
b95e0 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3b  to afp_end_lock;
b95f0 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 20 20  .    }.    .    
b9600 69 66 28 20 66 61 69 6c 65 64 20 29 7b 0a 20 20  if( failed ){.  
b9610 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
b9620 42 55 53 59 3b 0a 20 20 20 20 7d 20 65 6c 73 65  BUSY;.    } else
b9630 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e   {.      pFile->
b9640 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45  locktype = SHARE
b9650 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20  D_LOCK;.    }.  
b9660 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68  }else{.    /* Th
b9670 65 20 72 65 71 75 65 73 74 20 77 61 73 20 66 6f  e request was fo
b9680 72 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20  r a RESERVED or 
b9690 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20  EXCLUSIVE lock. 
b96a0 20 49 74 20 69 73 0a 20 20 20 20 2a 2a 20 61 73   It is.    ** as
b96b0 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 72 65  sumed that there
b96c0 20 69 73 20 61 20 53 48 41 52 45 44 20 6f 72 20   is a SHARED or 
b96d0 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20  greater lock on 
b96e0 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20  the file.    ** 
b96f0 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a  already..    */.
b9700 20 20 20 20 69 6e 74 20 66 61 69 6c 65 64 20 3d      int failed =
b9710 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   0;.    assert( 
b9720 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  0!=pFile->lockty
b9730 70 65 20 29 3b 0a 20 20 20 20 69 66 20 28 6c 6f  pe );.    if (lo
b9740 63 6b 74 79 70 65 20 3e 3d 20 52 45 53 45 52 56  cktype >= RESERV
b9750 45 44 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65  ED_LOCK && pFile
b9760 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c 20 52 45 53  ->locktype < RES
b9770 45 52 56 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20  ERVED_LOCK) {.  
b9780 20 20 20 20 20 20 2f 2a 20 41 63 71 75 69 72 65        /* Acquire
b9790 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b   a RESERVED lock
b97a0 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 61 69 6c   */.        fail
b97b0 65 64 20 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f  ed = _AFPFSSetLo
b97c0 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65  ck(context->file
b97d0 50 61 74 68 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  Path, pFile->h, 
b97e0 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31  RESERVED_BYTE, 1
b97f0 2c 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ,1);.    }.    i
b9800 66 20 28 21 66 61 69 6c 65 64 20 26 26 20 6c 6f  f (!failed && lo
b9810 63 6b 74 79 70 65 20 3d 3d 20 45 58 43 4c 55 53  cktype == EXCLUS
b9820 49 56 45 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20  IVE_LOCK) {.    
b9830 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 6e 20    /* Acquire an 
b9840 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 2a  EXCLUSIVE lock *
b9850 2f 0a 20 20 20 20 20 20 20 20 0a 20 20 20 20 20  /.        .     
b9860 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 73   /* Remove the s
b9870 68 61 72 65 64 20 6c 6f 63 6b 20 62 65 66 6f 72  hared lock befor
b9880 65 20 74 72 79 69 6e 67 20 74 68 65 20 72 61 6e  e trying the ran
b9890 67 65 2e 20 20 77 65 27 6c 6c 20 6e 65 65 64 20  ge.  we'll need 
b98a0 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 72 65 65  to .      ** ree
b98b0 73 74 61 62 6c 69 73 68 20 74 68 65 20 73 68 61  stablish the sha
b98c0 72 65 64 20 6c 6f 63 6b 20 69 66 20 77 65 20 63  red lock if we c
b98d0 61 6e 27 74 20 67 65 74 20 74 68 65 20 20 61 66  an't get the  af
b98e0 70 55 6e 69 78 55 6e 6c 6f 63 6b 0a 20 20 20 20  pUnixUnlock.    
b98f0 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 20 28 21    */.      if (!
b9900 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f  _AFPFSSetLock(co
b9910 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c  ntext->filePath,
b9920 20 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45   pFile->h, SHARE
b9930 44 5f 46 49 52 53 54 20 2b 0a 20 20 20 20 20 20  D_FIRST +.      
b9940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b9950 20 20 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72     context->shar
b9960 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31 2c 20 30  edLockByte, 1, 0
b9970 29 29 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  )) {.        /* 
b9980 6e 6f 77 20 61 74 74 65 6d 6d 70 74 20 74 6f 20  now attemmpt to 
b9990 67 65 74 20 74 68 65 20 65 78 63 6c 75 73 69 76  get the exclusiv
b99a0 65 20 6c 6f 63 6b 20 72 61 6e 67 65 20 2a 2f 0a  e lock range */.
b99b0 20 20 20 20 20 20 20 20 66 61 69 6c 65 64 20 3d          failed =
b99c0 20 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28 63   _AFPFSSetLock(c
b99d0 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 74 68  ontext->filePath
b99e0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52  , pFile->h, SHAR
b99f0 45 44 5f 46 49 52 53 54 2c 20 0a 20 20 20 20 20  ED_FIRST, .     
b9a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b9a10 20 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44            SHARED
b9a20 5f 53 49 5a 45 2c 20 31 29 3b 0a 20 20 20 20 20  _SIZE, 1);.     
b9a30 20 20 20 69 66 20 28 66 61 69 6c 65 64 20 26 26     if (failed &&
b9a40 20 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28 63   _AFPFSSetLock(c
b9a50 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 74 68  ontext->filePath
b9a60 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52  , pFile->h, SHAR
b9a70 45 44 5f 46 49 52 53 54 20 2b 0a 20 20 20 20 20  ED_FIRST +.     
b9a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b9a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
b9aa0 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 4c 6f  ontext->sharedLo
b9ab0 63 6b 42 79 74 65 2c 20 31 2c 20 31 29 29 20 7b  ckByte, 1, 1)) {
b9ac0 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
b9ad0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c  SQLITE_IOERR_RDL
b9ae0 4f 43 4b 3b 20 2f 2a 20 74 68 69 73 20 73 68 6f  OCK; /* this sho
b9af0 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e  uld never happen
b9b00 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   */.        }.  
b9b10 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20      } else {.   
b9b20 20 20 20 20 20 2f 2a 20 2a 2f 0a 20 20 20 20 20       /* */.     
b9b30 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49     rc = SQLITE_I
b9b40 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 20 2f 2a 20  OERR_UNLOCK; /* 
b9b50 74 68 69 73 20 73 68 6f 75 6c 64 20 6e 65 76 65  this should neve
b9b60 72 20 68 61 70 70 65 6e 20 2a 2f 0a 20 20 20 20  r happen */.    
b9b70 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
b9b80 28 20 66 61 69 6c 65 64 20 26 26 20 72 63 20 3d  ( failed && rc =
b9b90 3d 20 53 51 4c 49 54 45 5f 4f 4b 29 7b 0a 20 20  = SQLITE_OK){.  
b9ba0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
b9bb0 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  BUSY;.    }.  }.
b9bc0 20 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c    .  if( rc==SQL
b9bd0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46  ITE_OK ){.    pF
b9be0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
b9bf0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 65 6c 73  locktype;.  }els
b9c00 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d  e if( locktype==
b9c10 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29  EXCLUSIVE_LOCK )
b9c20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  {.    pFile->loc
b9c30 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f  ktype = PENDING_
b9c40 4c 4f 43 4b 3b 0a 20 20 7d 0a 20 20 0a 61 66 70  LOCK;.  }.  .afp
b9c50 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 6c 65 61  _end_lock:.  lea
b9c60 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53 54  veMutex();.  OST
b9c70 52 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20 25  RACE4("LOCK    %
b9c80 64 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69 6c  d %s %s\n", pFil
b9c90 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e 61  e->h, locktypeNa
b9ca0 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a 20  me(locktype), . 
b9cb0 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49          rc==SQLI
b9cc0 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22  TE_OK ? "ok" : "
b9cd0 66 61 69 6c 65 64 22 29 3b 0a 20 20 72 65 74 75  failed");.  retu
b9ce0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
b9cf0 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e  Lower the lockin
b9d00 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20  g level on file 
b9d10 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65  descriptor pFile
b9d20 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c   to locktype.  l
b9d30 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20  ocktype.** must 
b9d40 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43  be either NO_LOC
b9d50 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b  K or SHARED_LOCK
b9d60 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c  ..**.** If the l
b9d70 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20  ocking level of 
b9d80 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
b9d90 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61  tor is already a
b9da0 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68  t or below.** th
b9db0 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b  e requested lock
b9dc0 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20  ing level, this 
b9dd0 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
b9de0 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  op..*/.static in
b9df0 74 20 61 66 70 55 6e 69 78 55 6e 6c 6f 63 6b 28  t afpUnixUnlock(
b9e00 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
b9e10 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20  , int locktype) 
b9e20 7b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b  {.  struct flock
b9e30 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74 20 72 63 20   lock;.  int rc 
b9e40 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75  = SQLITE_OK;.  u
b9e50 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
b9e60 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
b9e70 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74    afpLockingCont
b9e80 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 28  ext *context = (
b9e90 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  afpLockingContex
b9ea0 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  t *) pFile->lock
b9eb0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 61  ingContext;..  a
b9ec0 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a  ssert( pFile );.
b9ed0 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f    OSTRACE5("UNLO
b9ee0 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 64  CK  %d %d was %d
b9ef0 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c   pid=%d\n", pFil
b9f00 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a  e->h, locktype,.
b9f10 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e           pFile->
b9f20 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69 64  locktype, getpid
b9f30 28 29 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74  ());.  .  assert
b9f40 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52  ( locktype<=SHAR
b9f50 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28  ED_LOCK );.  if(
b9f60 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
b9f70 3c 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20  <=locktype ){.  
b9f80 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
b9f90 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 43 48  OK;.  }.  if( CH
b9fa0 45 43 4b 5f 54 48 52 45 41 44 49 44 28 70 46 69  ECK_THREADID(pFi
b9fb0 6c 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  le) ){.    retur
b9fc0 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  n SQLITE_MISUSE;
b9fd0 0a 20 20 7d 0a 20 20 65 6e 74 65 72 4d 75 74 65  .  }.  enterMute
b9fe0 78 28 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65  x();.  if( pFile
b9ff0 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45  ->locktype>SHARE
ba000 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 69 66  D_LOCK ){.    if
ba010 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  ( locktype==SHAR
ba020 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20  ED_LOCK ){.     
ba030 20 69 6e 74 20 66 61 69 6c 65 64 20 3d 20 30 3b   int failed = 0;
ba040 0a 0a 20 20 20 20 20 20 2f 2a 20 75 6e 6c 6f 63  ..      /* unloc
ba050 6b 20 74 68 65 20 65 78 63 6c 75 73 69 76 65 20  k the exclusive 
ba060 72 61 6e 67 65 20 2d 20 74 68 65 6e 20 72 65 2d  range - then re-
ba070 65 73 74 61 62 6c 69 73 68 20 74 68 65 20 73 68  establish the sh
ba080 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20  ared lock */.   
ba090 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f     if (pFile->lo
ba0a0 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56  cktype==EXCLUSIV
ba0b0 45 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 20 20  E_LOCK) {.      
ba0c0 20 20 66 61 69 6c 65 64 20 3d 20 5f 41 46 50 46    failed = _AFPF
ba0d0 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74  SSetLock(context
ba0e0 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70 46 69 6c  ->filePath, pFil
ba0f0 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52  e->h, SHARED_FIR
ba100 53 54 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  ST, .           
ba110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ba120 20 20 20 20 20 20 53 48 41 52 45 44 5f 53 49 5a        SHARED_SIZ
ba130 45 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69  E, 0);.        i
ba140 66 20 28 21 66 61 69 6c 65 64 29 20 7b 0a 20 20  f (!failed) {.  
ba150 20 20 20 20 20 20 20 20 2f 2a 20 73 75 63 63 65          /* succe
ba160 73 73 66 75 6c 6c 79 20 72 65 6d 6f 76 65 64 20  ssfully removed 
ba170 74 68 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f  the exclusive lo
ba180 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  ck */.          
ba190 69 66 20 28 5f 41 46 50 46 53 53 65 74 4c 6f 63  if (_AFPFSSetLoc
ba1a0 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50  k(context->fileP
ba1b0 61 74 68 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 53  ath, pFile->h, S
ba1c0 48 41 52 45 44 5f 46 49 52 53 54 2b 0a 20 20 20  HARED_FIRST+.   
ba1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ba1e0 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65 78 74           context
ba1f0 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65  ->sharedLockByte
ba200 2c 20 31 2c 20 31 29 29 20 7b 0a 20 20 20 20 20  , 1, 1)) {.     
ba210 20 20 20 20 20 20 20 2f 2a 20 66 61 69 6c 65 64         /* failed
ba220 20 74 6f 20 72 65 2d 65 73 74 61 62 6c 69 73 68   to re-establish
ba230 20 6f 75 72 20 73 68 61 72 65 64 20 6c 6f 63 6b   our shared lock
ba240 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
ba250 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  rc = SQLITE_IOER
ba260 52 5f 52 44 4c 4f 43 4b 3b 20 2f 2a 20 54 68 69  R_RDLOCK; /* Thi
ba270 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68  s should never h
ba280 61 70 70 65 6e 20 2a 2f 0a 20 20 20 20 20 20 20  appen */.       
ba290 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 20 65     }.        } e
ba2a0 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 20 20  lse {.          
ba2b0 2f 2a 20 54 68 69 73 20 73 68 6f 75 6c 64 20 6e  /* This should n
ba2c0 65 76 65 72 20 68 61 70 70 65 6e 20 2d 20 66 61  ever happen - fa
ba2d0 69 6c 65 64 20 74 6f 20 75 6e 6c 6f 63 6b 20 74  iled to unlock t
ba2e0 68 65 20 65 78 63 6c 75 73 69 76 65 20 72 61 6e  he exclusive ran
ba2f0 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  ge */.          
ba300 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  rc = SQLITE_IOER
ba310 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 20 20  R_UNLOCK;.      
ba320 20 20 7d 20 0a 20 20 20 20 20 20 7d 0a 20 20 20    } .      }.   
ba330 20 7d 0a 20 20 20 20 69 66 20 28 72 63 20 3d 3d   }.    if (rc ==
ba340 20 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 46   SQLITE_OK && pF
ba350 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 50  ile->locktype>=P
ba360 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 20 7b 0a 20  ENDING_LOCK) {. 
ba370 20 20 20 20 20 69 66 20 28 5f 41 46 50 46 53 53       if (_AFPFSS
ba380 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e  etLock(context->
ba390 66 69 6c 65 50 61 74 68 2c 20 70 46 69 6c 65 2d  filePath, pFile-
ba3a0 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  >h, PENDING_BYTE
ba3b0 2c 20 31 2c 20 30 29 29 7b 0a 20 20 20 20 20 20  , 1, 0)){.      
ba3c0 20 20 2f 2a 20 66 61 69 6c 65 64 20 74 6f 20 72    /* failed to r
ba3d0 65 6c 65 61 73 65 20 74 68 65 20 70 65 6e 64 69  elease the pendi
ba3e0 6e 67 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20  ng lock */.     
ba3f0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49     rc = SQLITE_I
ba400 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 20 2f 2a 20  OERR_UNLOCK; /* 
ba410 54 68 69 73 20 73 68 6f 75 6c 64 20 6e 65 76 65  This should neve
ba420 72 20 68 61 70 70 65 6e 20 2a 2f 0a 20 20 20 20  r happen */.    
ba430 20 20 7d 0a 20 20 20 20 7d 20 0a 20 20 20 20 69    }.    } .    i
ba440 66 20 28 72 63 20 3d 3d 20 53 51 4c 49 54 45 5f  f (rc == SQLITE_
ba450 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63  OK && pFile->loc
ba460 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f  ktype>=RESERVED_
ba470 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 20 20 69 66  LOCK) {.      if
ba480 20 28 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28   (_AFPFSSetLock(
ba490 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 74  context->filePat
ba4a0 68 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53  h, pFile->h, RES
ba4b0 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 20 30  ERVED_BYTE, 1, 0
ba4c0 29 29 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  )) {.        /* 
ba4d0 66 61 69 6c 65 64 20 74 6f 20 72 65 6c 65 61 73  failed to releas
ba4e0 65 20 74 68 65 20 72 65 73 65 72 76 65 64 20 6c  e the reserved l
ba4f0 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 72  ock */.        r
ba500 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  c = SQLITE_IOERR
ba510 5f 55 4e 4c 4f 43 4b 3b 20 20 2f 2a 20 54 68 69  _UNLOCK;  /* Thi
ba520 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68  s should never h
ba530 61 70 70 65 6e 20 2a 2f 0a 20 20 20 20 20 20 7d  appen */.      }
ba540 0a 20 20 20 20 7d 20 0a 20 20 7d 0a 20 20 69 66  .    } .  }.  if
ba550 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c  ( locktype==NO_L
ba560 4f 43 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 66  OCK ){.    int f
ba570 61 69 6c 65 64 20 3d 20 5f 41 46 50 46 53 53 65  ailed = _AFPFSSe
ba580 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66  tLock(context->f
ba590 69 6c 65 50 61 74 68 2c 20 70 46 69 6c 65 2d 3e  ilePath, pFile->
ba5a0 68 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  h, .            
ba5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ba5c0 20 20 20 53 48 41 52 45 44 5f 46 49 52 53 54 20     SHARED_FIRST 
ba5d0 2b 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65  + context->share
ba5e0 64 4c 6f 63 6b 42 79 74 65 2c 20 31 2c 20 30 29  dLockByte, 1, 0)
ba5f0 3b 0a 20 20 20 20 69 66 20 28 66 61 69 6c 65 64  ;.    if (failed
ba600 29 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  ) {.      rc = S
ba610 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f  QLITE_IOERR_UNLO
ba620 43 4b 3b 20 20 2f 2a 20 54 68 69 73 20 73 68 6f  CK;  /* This sho
ba630 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e  uld never happen
ba640 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   */.    }.  }.  
ba650 69 66 20 28 72 63 20 3d 3d 20 53 51 4c 49 54 45  if (rc == SQLITE
ba660 5f 4f 4b 29 0a 20 20 20 20 70 46 69 6c 65 2d 3e  _OK).    pFile->
ba670 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74  locktype = lockt
ba680 79 70 65 3b 0a 20 20 6c 65 61 76 65 4d 75 74 65  ype;.  leaveMute
ba690 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  x();.  return rc
ba6a0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  ;.}../*.** Close
ba6b0 20 61 20 66 69 6c 65 20 26 20 63 6c 65 61 6e 75   a file & cleanu
ba6c0 70 20 41 46 50 20 73 70 65 63 69 66 69 63 20 6c  p AFP specific l
ba6d0 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 0a  ocking context .
ba6e0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66  */.static int af
ba6f0 70 55 6e 69 78 43 6c 6f 73 65 28 73 71 6c 69 74  pUnixClose(sqlit
ba700 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20  e3_file *id) {. 
ba710 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
ba720 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
ba730 3b 0a 0a 20 20 69 66 28 20 21 70 46 69 6c 65 20  ;..  if( !pFile 
ba740 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
ba750 4f 4b 3b 0a 20 20 61 66 70 55 6e 69 78 55 6e 6c  OK;.  afpUnixUnl
ba760 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29  ock(id, NO_LOCK)
ba770 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
ba780 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43  (pFile->lockingC
ba790 6f 6e 74 65 78 74 29 3b 0a 20 20 69 66 28 20 70  ontext);.  if( p
ba7a0 46 69 6c 65 2d 3e 64 69 72 66 64 3e 3d 30 20 29  File->dirfd>=0 )
ba7b0 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 64 69   close(pFile->di
ba7c0 72 66 64 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 64  rfd);.  pFile->d
ba7d0 69 72 66 64 20 3d 20 2d 31 3b 0a 20 20 65 6e 74  irfd = -1;.  ent
ba7e0 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 63 6c 6f  erMutex();.  clo
ba7f0 73 65 28 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20  se(pFile->h);.  
ba800 6c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  leaveMutex();.  
ba810 4f 53 54 52 41 43 45 32 28 22 43 4c 4f 53 45 20  OSTRACE2("CLOSE 
ba820 20 20 25 2d 33 64 5c 6e 22 2c 20 70 46 69 6c 65    %-3d\n", pFile
ba830 2d 3e 68 29 3b 0a 20 20 4f 70 65 6e 43 6f 75 6e  ->h);.  OpenCoun
ba840 74 65 72 28 2d 31 29 3b 0a 20 20 6d 65 6d 73 65  ter(-1);.  memse
ba850 74 28 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65  t(pFile, 0, size
ba860 6f 66 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 20  of(unixFile));. 
ba870 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
ba880 4b 3b 0a 7d 0a 0a 0a 23 70 72 61 67 6d 61 20 6d  K;.}...#pragma m
ba890 61 72 6b 20 66 6c 6f 63 6b 28 29 20 73 74 79 6c  ark flock() styl
ba8a0 65 20 6c 6f 63 6b 69 6e 67 0a 0a 2f 2a 0a 2a 2a  e locking../*.**
ba8b0 20 54 68 65 20 66 6c 6f 63 6b 4c 6f 63 6b 69 6e   The flockLockin
ba8c0 67 43 6f 6e 74 65 78 74 20 69 73 20 6e 6f 74 20  gContext is not 
ba8d0 75 73 65 64 0a 2a 2f 0a 74 79 70 65 64 65 66 20  used.*/.typedef 
ba8e0 76 6f 69 64 20 66 6c 6f 63 6b 4c 6f 63 6b 69 6e  void flockLockin
ba8f0 67 43 6f 6e 74 65 78 74 3b 0a 0a 73 74 61 74 69  gContext;..stati
ba900 63 20 69 6e 74 20 66 6c 6f 63 6b 55 6e 69 78 43  c int flockUnixC
ba910 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
ba920 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
ba930 64 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  d){.  unixFile *
ba940 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
ba950 65 2a 29 69 64 3b 0a 20 20 0a 20 20 69 66 20 28  e*)id;.  .  if (
ba960 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
ba970 3d 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  == RESERVED_LOCK
ba980 29 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  ) {.    return 1
ba990 3b 20 2f 2a 20 61 6c 72 65 61 64 79 20 68 61 76  ; /* already hav
ba9a0 65 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63  e a reserved loc
ba9b0 6b 20 2a 2f 0a 20 20 7d 20 65 6c 73 65 20 7b 0a  k */.  } else {.
ba9c0 20 20 20 20 2f 2a 20 61 74 74 65 6d 70 74 20 74      /* attempt t
ba9d0 6f 20 67 65 74 20 74 68 65 20 6c 6f 63 6b 20 2a  o get the lock *
ba9e0 2f 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 66  /.    int rc = f
ba9f0 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c  lock(pFile->h, L
baa00 4f 43 4b 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42  OCK_EX | LOCK_NB
baa10 29 3b 0a 20 20 20 20 69 66 20 28 21 72 63 29 20  );.    if (!rc) 
baa20 7b 0a 20 20 20 20 20 20 2f 2a 20 67 6f 74 20 74  {.      /* got t
baa30 68 65 20 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20  he lock, unlock 
baa40 69 74 20 2a 2f 0a 20 20 20 20 20 20 66 6c 6f 63  it */.      floc
baa50 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b  k(pFile->h, LOCK
baa60 5f 55 4e 29 3b 0a 20 20 20 20 20 20 72 65 74 75  _UN);.      retu
baa70 72 6e 20 30 3b 20 20 2f 2a 20 6e 6f 20 6f 6e 65  rn 0;  /* no one
baa80 20 68 61 73 20 69 74 20 72 65 73 65 72 76 65 64   has it reserved
baa90 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 72 65   */.    }.    re
baaa0 74 75 72 6e 20 31 3b 20 2f 2a 20 73 6f 6d 65 6f  turn 1; /* someo
baab0 6e 65 20 65 6c 73 65 20 6d 69 67 68 74 20 68 61  ne else might ha
baac0 76 65 20 69 74 20 72 65 73 65 72 76 65 64 20 2a  ve it reserved *
baad0 2f 0a 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20  /.  }.}..static 
baae0 69 6e 74 20 66 6c 6f 63 6b 55 6e 69 78 4c 6f 63  int flockUnixLoc
baaf0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
bab00 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
bab10 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  ) {.  unixFile *
bab20 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
bab30 65 2a 29 69 64 3b 0a 20 20 0a 20 20 2f 2a 20 69  e*)id;.  .  /* i
bab40 66 20 77 65 20 61 6c 72 65 61 64 79 20 68 61 76  f we already hav
bab50 65 20 61 20 6c 6f 63 6b 2c 20 69 74 20 69 73 20  e a lock, it is 
bab60 65 78 63 6c 75 73 69 76 65 2e 20 20 0a 20 20 2a  exclusive.  .  *
bab70 2a 20 4a 75 73 74 20 61 64 6a 75 73 74 20 6c 65  * Just adjust le
bab80 76 65 6c 20 61 6e 64 20 70 75 6e 74 20 6f 6e 20  vel and punt on 
bab90 6f 75 74 74 61 20 68 65 72 65 2e 20 2a 2f 0a 20  outta here. */. 
baba0 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if (pFile->lock
babb0 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 29 20  type > NO_LOCK) 
babc0 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  {.    pFile->loc
babd0 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65  ktype = locktype
babe0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
babf0 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20  ITE_OK;.  }.  . 
bac00 20 2f 2a 20 67 72 61 62 20 61 6e 20 65 78 63 6c   /* grab an excl
bac10 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20  usive lock */.  
bac20 69 6e 74 20 72 63 20 3d 20 66 6c 6f 63 6b 28 70  int rc = flock(p
bac30 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 58  File->h, LOCK_EX
bac40 20 7c 20 4c 4f 43 4b 5f 4e 42 29 3b 0a 20 20 69   | LOCK_NB);.  i
bac50 66 20 28 72 63 29 20 7b 0a 20 20 20 20 2f 2a 20  f (rc) {.    /* 
bac60 64 69 64 6e 27 74 20 67 65 74 2c 20 6d 75 73 74  didn't get, must
bac70 20 62 65 20 62 75 73 79 20 2a 2f 0a 20 20 20 20   be busy */.    
bac80 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
bac90 53 59 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20  SY;.  } else {. 
baca0 20 20 20 2f 2a 20 67 6f 74 20 69 74 2c 20 73 65     /* got it, se
bacb0 74 20 74 68 65 20 74 79 70 65 20 61 6e 64 20 72  t the type and r
bacc0 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 20 20  eturn ok */.    
bacd0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
bace0 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20  = locktype;.    
bacf0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
bad00 3b 0a 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20  ;.  }.}..static 
bad10 69 6e 74 20 66 6c 6f 63 6b 55 6e 69 78 55 6e 6c  int flockUnixUnl
bad20 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
bad30 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79   *id, int lockty
bad40 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65  pe) {.  unixFile
bad50 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
bad60 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 61 73  ile*)id;.  .  as
bad70 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d  sert( locktype<=
bad80 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20  SHARED_LOCK );. 
bad90 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20   .  /* no-op if 
bada0 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 20 69 66  possible */.  if
badb0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
badc0 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20  e==locktype ){. 
badd0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
bade0 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a  _OK;.  }.  .  /*
badf0 20 73 68 61 72 65 64 20 63 61 6e 20 6a 75 73 74   shared can just
bae00 20 62 65 20 73 65 74 20 62 65 63 61 75 73 65 20   be set because 
bae10 77 65 20 61 6c 77 61 79 73 20 68 61 76 65 20 61  we always have a
bae20 6e 20 65 78 63 6c 75 73 69 76 65 20 2a 2f 0a 20  n exclusive */. 
bae30 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53   if (locktype==S
bae40 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20  HARED_LOCK) {.  
bae50 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
bae60 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  e = locktype;.  
bae70 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
bae80 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20  OK;.  }.  .  /* 
bae90 6e 6f 2c 20 72 65 61 6c 6c 79 2c 20 75 6e 6c 6f  no, really, unlo
baea0 63 6b 2e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20  ck. */.  int rc 
baeb0 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68  = flock(pFile->h
baec0 2c 20 4c 4f 43 4b 5f 55 4e 29 3b 0a 20 20 69 66  , LOCK_UN);.  if
baed0 20 28 72 63 29 0a 20 20 20 20 72 65 74 75 72 6e   (rc).    return
baee0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e   SQLITE_IOERR_UN
baef0 4c 4f 43 4b 3b 0a 20 20 65 6c 73 65 20 7b 0a 20  LOCK;.  else {. 
baf00 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
baf10 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20  pe = NO_LOCK;.  
baf20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
baf30 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  OK;.  }.}../*.**
baf40 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a   Close a file..*
baf50 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 6f  /.static int flo
baf60 63 6b 55 6e 69 78 43 6c 6f 73 65 28 73 71 6c 69  ckUnixClose(sqli
baf70 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a  te3_file *id) {.
baf80 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
baf90 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
bafa0 64 3b 0a 20 20 0a 20 20 69 66 28 20 21 70 46 69  d;.  .  if( !pFi
bafb0 6c 65 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  le ) return SQLI
bafc0 54 45 5f 4f 4b 3b 0a 20 20 66 6c 6f 63 6b 55 6e  TE_OK;.  flockUn
bafd0 69 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f  ixUnlock(id, NO_
bafe0 4c 4f 43 4b 29 3b 0a 20 20 0a 20 20 69 66 28 20  LOCK);.  .  if( 
baff0 70 46 69 6c 65 2d 3e 64 69 72 66 64 3e 3d 30 20  pFile->dirfd>=0 
bb000 29 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 64  ) close(pFile->d
bb010 69 72 66 64 29 3b 0a 20 20 70 46 69 6c 65 2d 3e  irfd);.  pFile->
bb020 64 69 72 66 64 20 3d 20 2d 31 3b 0a 0a 20 20 65  dirfd = -1;..  e
bb030 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 63  nterMutex();.  c
bb040 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 3b 20  lose(pFile->h); 
bb050 20 0a 20 20 6c 65 61 76 65 4d 75 74 65 78 28 29   .  leaveMutex()
bb060 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22 43 4c  ;.  OSTRACE2("CL
bb070 4f 53 45 20 20 20 25 2d 33 64 5c 6e 22 2c 20 70  OSE   %-3d\n", p
bb080 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 4f 70 65 6e  File->h);.  Open
bb090 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20 6d  Counter(-1);.  m
bb0a0 65 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 2c 20  emset(pFile, 0, 
bb0b0 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29  sizeof(unixFile)
bb0c0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
bb0d0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 70 72 61 67 6d  TE_OK;.}..#pragm
bb0e0 61 20 6d 61 72 6b 20 4f 6c 64 2d 53 63 68 6f 6f  a mark Old-Schoo
bb0f0 6c 20 2e 6c 6f 63 6b 20 66 69 6c 65 20 62 61 73  l .lock file bas
bb100 65 64 20 6c 6f 63 6b 69 6e 67 0a 0a 2f 2a 0a 2a  ed locking../*.*
bb110 2a 20 54 68 65 20 64 6f 74 6c 6f 63 6b 4c 6f 63  * The dotlockLoc
bb120 6b 69 6e 67 43 6f 6e 74 65 78 74 20 73 74 72 75  kingContext stru
bb130 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61  cture contains a
bb140 6c 6c 20 64 6f 74 6c 6f 63 6b 20 28 2e 6c 6f 63  ll dotlock (.loc
bb150 6b 29 20 6c 6f 63 6b 0a 2a 2a 20 73 70 65 63 69  k) lock.** speci
bb160 66 69 63 20 73 74 61 74 65 0a 2a 2f 0a 74 79 70  fic state.*/.typ
bb170 65 64 65 66 20 73 74 72 75 63 74 20 64 6f 74 6c  edef struct dotl
bb180 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  ockLockingContex
bb190 74 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67  t dotlockLocking
bb1a0 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63 74 20  Context;.struct 
bb1b0 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f  dotlockLockingCo
bb1c0 6e 74 65 78 74 20 7b 0a 20 20 63 68 61 72 20 2a  ntext {.  char *
bb1d0 6c 6f 63 6b 50 61 74 68 3b 0a 7d 3b 0a 0a 0a 73  lockPath;.};...s
bb1e0 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63  tatic int dotloc
bb1f0 6b 55 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76  kUnixCheckReserv
bb200 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  edLock(sqlite3_f
bb210 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 75 6e 69  ile *id) {.  uni
bb220 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
bb230 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20  unixFile*)id;.  
bb240 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f  dotlockLockingCo
bb250 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 3b 0a  ntext *context;.
bb260 0a 20 20 63 6f 6e 74 65 78 74 20 3d 20 28 64 6f  .  context = (do
bb270 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74  tlockLockingCont
bb280 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ext*)pFile->lock
bb290 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 69 66  ingContext;.  if
bb2a0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70   (pFile->locktyp
bb2b0 65 20 3d 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f  e == RESERVED_LO
bb2c0 43 4b 29 20 7b 0a 20 20 20 20 72 65 74 75 72 6e  CK) {.    return
bb2d0 20 31 3b 20 2f 2a 20 61 6c 72 65 61 64 79 20 68   1; /* already h
bb2e0 61 76 65 20 61 20 72 65 73 65 72 76 65 64 20 6c  ave a reserved l
bb2f0 6f 63 6b 20 2a 2f 0a 20 20 7d 20 65 6c 73 65 20  ock */.  } else 
bb300 7b 0a 20 20 20 20 73 74 72 75 63 74 20 73 74 61  {.    struct sta
bb310 74 20 73 74 61 74 42 75 66 3b 0a 20 20 20 20 69  t statBuf;.    i
bb320 66 20 28 6c 73 74 61 74 28 63 6f 6e 74 65 78 74  f (lstat(context
bb330 2d 3e 6c 6f 63 6b 50 61 74 68 2c 26 73 74 61 74  ->lockPath,&stat
bb340 42 75 66 29 20 3d 3d 20 30 29 7b 0a 20 20 20 20  Buf) == 0){.    
bb350 20 20 2f 2a 20 66 69 6c 65 20 65 78 69 73 74 73    /* file exists
bb360 2c 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 68  , someone else h
bb370 61 73 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20  as the lock */. 
bb380 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
bb390 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
bb3a0 2f 2a 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74  /* file does not
bb3b0 20 65 78 69 73 74 2c 20 77 65 20 63 6f 75 6c 64   exist, we could
bb3c0 20 68 61 76 65 20 69 74 20 69 66 20 77 65 20 77   have it if we w
bb3d0 61 6e 74 20 69 74 20 2a 2f 0a 20 20 20 20 20 20  ant it */.      
bb3e0 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a  return 0;.    }.
bb3f0 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e    }.}..static in
bb400 74 20 64 6f 74 6c 6f 63 6b 55 6e 69 78 4c 6f 63  t dotlockUnixLoc
bb410 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
bb420 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
bb430 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  ) {.  unixFile *
bb440 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
bb450 65 2a 29 69 64 3b 0a 20 20 64 6f 74 6c 6f 63 6b  e*)id;.  dotlock
bb460 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a  LockingContext *
bb470 63 6f 6e 74 65 78 74 3b 0a 20 20 69 6e 74 20 66  context;.  int f
bb480 64 3b 0a 0a 20 20 63 6f 6e 74 65 78 74 20 3d 20  d;..  context = 
bb490 28 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43  (dotlockLockingC
bb4a0 6f 6e 74 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c  ontext*)pFile->l
bb4b0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20  ockingContext;. 
bb4c0 20 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 6c 72   .  /* if we alr
bb4d0 65 61 64 79 20 68 61 76 65 20 61 20 6c 6f 63 6b  eady have a lock
bb4e0 2c 20 69 74 20 69 73 20 65 78 63 6c 75 73 69 76  , it is exclusiv
bb4f0 65 2e 20 20 0a 20 20 2a 2a 20 4a 75 73 74 20 61  e.  .  ** Just a
bb500 64 6a 75 73 74 20 6c 65 76 65 6c 20 61 6e 64 20  djust level and 
bb510 70 75 6e 74 20 6f 6e 20 6f 75 74 74 61 20 68 65  punt on outta he
bb520 72 65 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 69  re. */.  if (pFi
bb530 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e  le->locktype > N
bb540 4f 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46  O_LOCK) {.    pF
bb550 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
bb560 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 0a 20  locktype;.    . 
bb570 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 75 70 64     /* Always upd
bb580 61 74 65 20 74 68 65 20 74 69 6d 65 73 74 61 6d  ate the timestam
bb590 70 20 6f 6e 20 74 68 65 20 6f 6c 64 20 66 69 6c  p on the old fil
bb5a0 65 20 2a 2f 0a 20 20 20 20 75 74 69 6d 65 73 28  e */.    utimes(
bb5b0 63 6f 6e 74 65 78 74 2d 3e 6c 6f 63 6b 50 61 74  context->lockPat
bb5c0 68 2c 4e 55 4c 4c 29 3b 0a 20 20 20 20 72 65 74  h,NULL);.    ret
bb5d0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
bb5e0 20 7d 0a 20 20 0a 20 20 2f 2a 20 63 68 65 63 6b   }.  .  /* check
bb5f0 20 74 6f 20 73 65 65 20 69 66 20 6c 6f 63 6b 20   to see if lock 
bb600 66 69 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69  file already exi
bb610 73 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  sts */.  struct 
bb620 73 74 61 74 20 73 74 61 74 42 75 66 3b 0a 20 20  stat statBuf;.  
bb630 69 66 20 28 6c 73 74 61 74 28 63 6f 6e 74 65 78  if (lstat(contex
bb640 74 2d 3e 6c 6f 63 6b 50 61 74 68 2c 26 73 74 61  t->lockPath,&sta
bb650 74 42 75 66 29 20 3d 3d 20 30 29 7b 0a 20 20 20  tBuf) == 0){.   
bb660 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
bb670 55 53 59 3b 20 2f 2a 20 69 74 20 64 6f 65 73 2c  USY; /* it does,
bb680 20 62 75 73 79 20 2a 2f 0a 20 20 7d 0a 20 20 0a   busy */.  }.  .
bb690 20 20 2f 2a 20 67 72 61 62 20 61 6e 20 65 78 63    /* grab an exc
bb6a0 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20  lusive lock */. 
bb6b0 20 66 64 20 3d 20 6f 70 65 6e 28 63 6f 6e 74 65   fd = open(conte
bb6c0 78 74 2d 3e 6c 6f 63 6b 50 61 74 68 2c 4f 5f 52  xt->lockPath,O_R
bb6d0 44 4f 4e 4c 59 7c 4f 5f 43 52 45 41 54 7c 4f 5f  DONLY|O_CREAT|O_
bb6e0 45 58 43 4c 2c 30 36 30 30 29 3b 0a 20 20 69 66  EXCL,0600);.  if
bb6f0 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 2f 2a  ( fd<0 ){.    /*
bb700 20 66 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e 2f   failed to open/
bb710 63 72 65 61 74 65 20 74 68 65 20 66 69 6c 65 2c  create the file,
bb720 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d 61   someone else ma
bb730 79 20 68 61 76 65 20 73 74 6f 6c 65 6e 20 74 68  y have stolen th
bb740 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 72 65  e lock */.    re
bb750 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
bb760 3b 20 0a 20 20 7d 0a 20 20 63 6c 6f 73 65 28 66  ; .  }.  close(f
bb770 64 29 3b 0a 20 20 0a 20 20 2f 2a 20 67 6f 74 20  d);.  .  /* got 
bb780 69 74 2c 20 73 65 74 20 74 68 65 20 74 79 70 65  it, set the type
bb790 20 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a   and return ok *
bb7a0 2f 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  /.  pFile->lockt
bb7b0 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a  ype = locktype;.
bb7c0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
bb7d0 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  OK;.}..static in
bb7e0 74 20 64 6f 74 6c 6f 63 6b 55 6e 69 78 55 6e 6c  t dotlockUnixUnl
bb7f0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
bb800 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79   *id, int lockty
bb810 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65  pe) {.  unixFile
bb820 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
bb830 69 6c 65 2a 29 69 64 3b 0a 20 20 64 6f 74 6c 6f  ile*)id;.  dotlo
bb840 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ckLockingContext
bb850 20 2a 63 6f 6e 74 65 78 74 3b 0a 0a 20 20 63 6f   *context;..  co
bb860 6e 74 65 78 74 20 3d 20 28 64 6f 74 6c 6f 63 6b  ntext = (dotlock
bb870 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29  LockingContext*)
bb880 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
bb890 6e 74 65 78 74 3b 0a 20 20 0a 20 20 61 73 73 65  ntext;.  .  asse
bb8a0 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48  rt( locktype<=SH
bb8b0 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a  ARED_LOCK );.  .
bb8c0 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f    /* no-op if po
bb8d0 73 73 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20  ssible */.  if( 
bb8e0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
bb8f0 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20  =locktype ){.   
bb900 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
bb910 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73  K;.  }.  .  /* s
bb920 68 61 72 65 64 20 63 61 6e 20 6a 75 73 74 20 62  hared can just b
bb930 65 20 73 65 74 20 62 65 63 61 75 73 65 20 77 65  e set because we
bb940 20 61 6c 77 61 79 73 20 68 61 76 65 20 61 6e 20   always have an 
bb950 65 78 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69  exclusive */.  i
bb960 66 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  f (locktype==SHA
bb970 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20  RED_LOCK) {.    
bb980 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
bb990 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20  = locktype;.    
bb9a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
bb9b0 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6e 6f  ;.  }.  .  /* no
bb9c0 2c 20 72 65 61 6c 6c 79 2c 20 75 6e 6c 6f 63 6b  , really, unlock
bb9d0 2e 20 2a 2f 0a 20 20 75 6e 6c 69 6e 6b 28 63 6f  . */.  unlink(co
bb9e0 6e 74 65 78 74 2d 3e 6c 6f 63 6b 50 61 74 68 29  ntext->lockPath)
bb9f0 3b 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  ;.  pFile->lockt
bba00 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20  ype = NO_LOCK;. 
bba10 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
bba20 4b 3b 0a 7d 0a 0a 2f 2a 0a 20 2a 2a 20 43 6c 6f  K;.}../*. ** Clo
bba30 73 65 20 61 20 66 69 6c 65 2e 0a 20 2a 2f 0a 73  se a file.. */.s
bba40 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63  tatic int dotloc
bba50 6b 55 6e 69 78 43 6c 6f 73 65 28 73 71 6c 69 74  kUnixClose(sqlit
bba60 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20  e3_file *id) {. 
bba70 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
bba80 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
bba90 3b 0a 20 20 0a 20 20 69 66 28 20 21 70 46 69 6c  ;.  .  if( !pFil
bbaa0 65 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  e ) return SQLIT
bbab0 45 5f 4f 4b 3b 0a 20 20 64 6f 74 6c 6f 63 6b 55  E_OK;.  dotlockU
bbac0 6e 69 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f  nixUnlock(id, NO
bbad0 5f 4c 4f 43 4b 29 3b 0a 20 20 73 71 6c 69 74 65  _LOCK);.  sqlite
bbae0 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e 6c 6f  3_free(pFile->lo
bbaf0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b 0a 20  ckingContext);. 
bbb00 20 69 66 28 20 70 46 69 6c 65 2d 3e 64 69 72 66   if( pFile->dirf
bbb10 64 3e 3d 30 20 29 20 63 6c 6f 73 65 28 70 46 69  d>=0 ) close(pFi
bbb20 6c 65 2d 3e 64 69 72 66 64 29 3b 0a 20 20 70 46  le->dirfd);.  pF
bbb30 69 6c 65 2d 3e 64 69 72 66 64 20 3d 20 2d 31 3b  ile->dirfd = -1;
bbb40 0a 20 20 65 6e 74 65 72 4d 75 74 65 78 28 29 3b  .  enterMutex();
bbb50 20 20 0a 20 20 63 6c 6f 73 65 28 70 46 69 6c 65    .  close(pFile
bbb60 2d 3e 68 29 3b 0a 20 20 6c 65 61 76 65 4d 75 74  ->h);.  leaveMut
bbb70 65 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45 32  ex();.  OSTRACE2
bbb80 28 22 43 4c 4f 53 45 20 20 20 25 2d 33 64 5c 6e  ("CLOSE   %-3d\n
bbb90 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20  ", pFile->h);.  
bbba0 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b  OpenCounter(-1);
bbbb0 0a 20 20 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c  .  memset(pFile,
bbbc0 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78 46   0, sizeof(unixF
bbbd0 69 6c 65 29 29 3b 0a 20 20 72 65 74 75 72 6e 20  ile));.  return 
bbbe0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23  SQLITE_OK;.}...#
bbbf0 70 72 61 67 6d 61 20 6d 61 72 6b 20 4e 6f 20 6c  pragma mark No l
bbc00 6f 63 6b 69 6e 67 0a 0a 2f 2a 0a 2a 2a 20 54 68  ocking../*.** Th
bbc10 65 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43  e nolockLockingC
bbc20 6f 6e 74 65 78 74 20 69 73 20 76 6f 69 64 0a 2a  ontext is void.*
bbc30 2f 0a 74 79 70 65 64 65 66 20 76 6f 69 64 20 6e  /.typedef void n
bbc40 6f 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74  olockLockingCont
bbc50 65 78 74 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74  ext;..static int
bbc60 20 6e 6f 6c 6f 63 6b 55 6e 69 78 43 68 65 63 6b   nolockUnixCheck
bbc70 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c  ReservedLock(sql
bbc80 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b  ite3_file *id) {
bbc90 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
bbca0 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63  static int noloc
bbcb0 6b 55 6e 69 78 4c 6f 63 6b 28 73 71 6c 69 74 65  kUnixLock(sqlite
bbcc0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
bbcd0 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 72 65  locktype) {.  re
bbce0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
bbcf0 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f  }..static int no
bbd00 6c 6f 63 6b 55 6e 69 78 55 6e 6c 6f 63 6b 28 73  lockUnixUnlock(s
bbd10 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
bbd20 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b   int locktype) {
bbd30 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
bbd40 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  _OK;.}../*.** Cl
bbd50 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73  ose a file..*/.s
bbd60 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b  tatic int nolock
bbd70 55 6e 69 78 43 6c 6f 73 65 28 73 71 6c 69 74 65  UnixClose(sqlite
bbd80 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20  3_file *id) {.  
bbd90 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
bbda0 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
bbdb0 0a 20 20 0a 20 20 69 66 28 20 21 70 46 69 6c 65  .  .  if( !pFile
bbdc0 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
bbdd0 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 46 69 6c 65  _OK;.  if( pFile
bbde0 2d 3e 64 69 72 66 64 3e 3d 30 20 29 20 63 6c 6f  ->dirfd>=0 ) clo
bbdf0 73 65 28 70 46 69 6c 65 2d 3e 64 69 72 66 64 29  se(pFile->dirfd)
bbe00 3b 0a 20 20 70 46 69 6c 65 2d 3e 64 69 72 66 64  ;.  pFile->dirfd
bbe10 20 3d 20 2d 31 3b 0a 20 20 65 6e 74 65 72 4d 75   = -1;.  enterMu
bbe20 74 65 78 28 29 3b 0a 20 20 63 6c 6f 73 65 28 70  tex();.  close(p
bbe30 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 6c 65 61 76  File->h);.  leav
bbe40 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53 54 52  eMutex();.  OSTR
bbe50 41 43 45 32 28 22 43 4c 4f 53 45 20 20 20 25 2d  ACE2("CLOSE   %-
bbe60 33 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29  3d\n", pFile->h)
bbe70 3b 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28  ;.  OpenCounter(
bbe80 2d 31 29 3b 0a 20 20 6d 65 6d 73 65 74 28 70 46  -1);.  memset(pF
bbe90 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75  ile, 0, sizeof(u
bbea0 6e 69 78 46 69 6c 65 29 29 3b 0a 20 20 72 65 74  nixFile));.  ret
bbeb0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
bbec0 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
bbed0 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
bbee0 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 0a 2f 2a 0a  G_STYLE */.../*.
bbef0 2a 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  ** Information a
bbf00 6e 64 20 63 6f 6e 74 72 6f 6c 20 6f 66 20 61 6e  nd control of an
bbf10 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c   open file handl
bbf20 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
bbf30 20 75 6e 69 78 46 69 6c 65 43 6f 6e 74 72 6f 6c   unixFileControl
bbf40 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
bbf50 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20  d, int op, void 
bbf60 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63 68  *pArg){.  switch
bbf70 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65  ( op ){.    case
bbf80 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f   SQLITE_FCNTL_LO
bbf90 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 20  CKSTATE: {.     
bbfa0 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28   *(int*)pArg = (
bbfb0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e  (unixFile*)id)->
bbfc0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20  locktype;.      
bbfd0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
bbfe0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
bbff0 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
bc000 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  R;.}../*.** Retu
bc010 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 69  rn the sector si
bc020 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74  ze in bytes of t
bc030 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c  he underlying bl
bc040 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a  ock device for.*
bc050 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  * the specified 
bc060 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c  file. This is al
bc070 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 20  most always 512 
bc080 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62  bytes, but may b
bc090 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20  e.** larger for 
bc0a0 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a  some devices..**
bc0b0 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20  .** SQLite code 
bc0c0 61 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e  assumes this fun
bc0d0 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69  ction cannot fai
bc0e0 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d  l. It also assum
bc0f0 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77  es that.** if tw
bc100 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 61  o files are crea
bc110 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20  ted in the same 
bc120 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65  file-system dire
bc130 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61  ctory (i.e..** a
bc140 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 74   database and it
bc150 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20  s journal file) 
bc160 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 20  that the sector 
bc170 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65  size will be the
bc180 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74  .** same for bot
bc190 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  h..*/.static int
bc1a0 20 75 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 28   unixSectorSize(
bc1b0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
bc1c0 29 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ){.  return SQLI
bc1d0 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f  TE_DEFAULT_SECTO
bc1e0 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  R_SIZE;.}../*.**
bc1f0 20 52 65 74 75 72 6e 20 74 68 65 20 64 65 76 69   Return the devi
bc200 63 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69  ce characteristi
bc210 63 73 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e  cs for the file.
bc220 20 54 68 69 73 20 69 73 20 61 6c 77 61 79 73 20   This is always 
bc230 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  0..*/.static int
bc240 20 75 6e 69 78 44 65 76 69 63 65 43 68 61 72 61   unixDeviceChara
bc250 63 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 74  cteristics(sqlit
bc260 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20  e3_file *id){.  
bc270 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
bc280 2a 2a 20 54 68 69 73 20 76 65 63 74 6f 72 20 64  ** This vector d
bc290 65 66 69 6e 65 73 20 61 6c 6c 20 74 68 65 20 6d  efines all the m
bc2a0 65 74 68 6f 64 73 20 74 68 61 74 20 63 61 6e 20  ethods that can 
bc2b0 6f 70 65 72 61 74 65 20 6f 6e 20 61 6e 20 73 71  operate on an sq
bc2c0 6c 69 74 65 33 5f 66 69 6c 65 0a 2a 2a 20 66 6f  lite3_file.** fo
bc2d0 72 20 75 6e 69 78 2e 0a 2a 2f 0a 73 74 61 74 69  r unix..*/.stati
bc2e0 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
bc2f0 69 6f 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74  io_methods sqlit
bc300 65 33 55 6e 69 78 49 6f 4d 65 74 68 6f 64 20 3d  e3UnixIoMethod =
bc310 20 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20   {.  1,         
bc320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
bc330 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20  * iVersion */.  
bc340 75 6e 69 78 43 6c 6f 73 65 2c 0a 20 20 75 6e 69  unixClose,.  uni
bc350 78 52 65 61 64 2c 0a 20 20 75 6e 69 78 57 72 69  xRead,.  unixWri
bc360 74 65 2c 0a 20 20 75 6e 69 78 54 72 75 6e 63 61  te,.  unixTrunca
bc370 74 65 2c 0a 20 20 75 6e 69 78 53 79 6e 63 2c 0a  te,.  unixSync,.
bc380 20 20 75 6e 69 78 46 69 6c 65 53 69 7a 65 2c 0a    unixFileSize,.
bc390 20 20 75 6e 69 78 4c 6f 63 6b 2c 0a 20 20 75 6e    unixLock,.  un
bc3a0 69 78 55 6e 6c 6f 63 6b 2c 0a 20 20 75 6e 69 78  ixUnlock,.  unix
bc3b0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
bc3c0 6b 2c 0a 20 20 75 6e 69 78 46 69 6c 65 43 6f 6e  k,.  unixFileCon
bc3d0 74 72 6f 6c 2c 0a 20 20 75 6e 69 78 53 65 63 74  trol,.  unixSect
bc3e0 6f 72 53 69 7a 65 2c 0a 20 20 75 6e 69 78 44 65  orSize,.  unixDe
bc3f0 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
bc400 69 63 73 0a 7d 3b 0a 0a 23 69 66 64 65 66 20 53  ics.};..#ifdef S
bc410 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
bc420 4b 49 4e 47 5f 53 54 59 4c 45 0a 2f 2a 0a 2a 2a  KING_STYLE./*.**
bc430 20 54 68 69 73 20 76 65 63 74 6f 72 20 64 65 66   This vector def
bc440 69 6e 65 73 20 61 6c 6c 20 74 68 65 20 6d 65 74  ines all the met
bc450 68 6f 64 73 20 74 68 61 74 20 63 61 6e 20 6f 70  hods that can op
bc460 65 72 61 74 65 20 6f 6e 20 61 6e 20 73 71 6c 69  erate on an sqli
bc470 74 65 33 5f 66 69 6c 65 0a 2a 2a 20 66 6f 72 20  te3_file.** for 
bc480 75 6e 69 78 20 77 69 74 68 20 41 46 50 20 73 74  unix with AFP st
bc490 79 6c 65 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67  yle file locking
bc4a0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
bc4b0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
bc4c0 68 6f 64 73 20 73 71 6c 69 74 65 33 41 46 50 4c  hods sqlite3AFPL
bc4d0 6f 63 6b 69 6e 67 55 6e 69 78 49 6f 4d 65 74 68  ockingUnixIoMeth
bc4e0 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20  od = {.  1,     
bc4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bc500 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a     /* iVersion *
bc510 2f 0a 20 20 61 66 70 55 6e 69 78 43 6c 6f 73 65  /.  afpUnixClose
bc520 2c 0a 20 20 75 6e 69 78 52 65 61 64 2c 0a 20 20  ,.  unixRead,.  
bc530 75 6e 69 78 57 72 69 74 65 2c 0a 20 20 75 6e 69  unixWrite,.  uni
bc540 78 54 72 75 6e 63 61 74 65 2c 0a 20 20 75 6e 69  xTruncate,.  uni
bc550 78 53 79 6e 63 2c 0a 20 20 75 6e 69 78 46 69 6c  xSync,.  unixFil
bc560 65 53 69 7a 65 2c 0a 20 20 61 66 70 55 6e 69 78  eSize,.  afpUnix
bc570 4c 6f 63 6b 2c 0a 20 20 61 66 70 55 6e 69 78 55  Lock,.  afpUnixU
bc580 6e 6c 6f 63 6b 2c 0a 20 20 61 66 70 55 6e 69 78  nlock,.  afpUnix
bc590 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
bc5a0 6b 2c 0a 20 20 75 6e 69 78 46 69 6c 65 43 6f 6e  k,.  unixFileCon
bc5b0 74 72 6f 6c 2c 0a 20 20 75 6e 69 78 53 65 63 74  trol,.  unixSect
bc5c0 6f 72 53 69 7a 65 2c 0a 20 20 75 6e 69 78 44 65  orSize,.  unixDe
bc5d0 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
bc5e0 69 63 73 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68  ics.};../*.** Th
bc5f0 69 73 20 76 65 63 74 6f 72 20 64 65 66 69 6e 65  is vector define
bc600 73 20 61 6c 6c 20 74 68 65 20 6d 65 74 68 6f 64  s all the method
bc610 73 20 74 68 61 74 20 63 61 6e 20 6f 70 65 72 61  s that can opera
bc620 74 65 20 6f 6e 20 61 6e 20 73 71 6c 69 74 65 33  te on an sqlite3
bc630 5f 66 69 6c 65 0a 2a 2a 20 66 6f 72 20 75 6e 69  _file.** for uni
bc640 78 20 77 69 74 68 20 66 6c 6f 63 6b 28 29 20 73  x with flock() s
bc650 74 79 6c 65 20 66 69 6c 65 20 6c 6f 63 6b 69 6e  tyle file lockin
bc660 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  g..*/.static con
bc670 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  st sqlite3_io_me
bc680 74 68 6f 64 73 20 73 71 6c 69 74 65 33 46 6c 6f  thods sqlite3Flo
bc690 63 6b 4c 6f 63 6b 69 6e 67 55 6e 69 78 49 6f 4d  ckLockingUnixIoM
bc6a0 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20  ethod = {.  1,  
bc6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bc6c0 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f        /* iVersio
bc6d0 6e 20 2a 2f 0a 20 20 66 6c 6f 63 6b 55 6e 69 78  n */.  flockUnix
bc6e0 43 6c 6f 73 65 2c 0a 20 20 75 6e 69 78 52 65 61  Close,.  unixRea
bc6f0 64 2c 0a 20 20 75 6e 69 78 57 72 69 74 65 2c 0a  d,.  unixWrite,.
bc700 20 20 75 6e 69 78 54 72 75 6e 63 61 74 65 2c 0a    unixTruncate,.
bc710 20 20 75 6e 69 78 53 79 6e 63 2c 0a 20 20 75 6e    unixSync,.  un
bc720 69 78 46 69 6c 65 53 69 7a 65 2c 0a 20 20 66 6c  ixFileSize,.  fl
bc730 6f 63 6b 55 6e 69 78 4c 6f 63 6b 2c 0a 20 20 66  ockUnixLock,.  f
bc740 6c 6f 63 6b 55 6e 69 78 55 6e 6c 6f 63 6b 2c 0a  lockUnixUnlock,.
bc750 20 20 66 6c 6f 63 6b 55 6e 69 78 43 68 65 63 6b    flockUnixCheck
bc760 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c 0a 20 20  ReservedLock,.  
bc770 75 6e 69 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c  unixFileControl,
bc780 0a 20 20 75 6e 69 78 53 65 63 74 6f 72 53 69 7a  .  unixSectorSiz
bc790 65 2c 0a 20 20 75 6e 69 78 44 65 76 69 63 65 43  e,.  unixDeviceC
bc7a0 68 61 72 61 63 74 65 72 69 73 74 69 63 73 0a 7d  haracteristics.}
bc7b0 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65  ;../*.** This ve
bc7c0 63 74 6f 72 20 64 65 66 69 6e 65 73 20 61 6c 6c  ctor defines all
bc7d0 20 74 68 65 20 6d 65 74 68 6f 64 73 20 74 68 61   the methods tha
bc7e0 74 20 63 61 6e 20 6f 70 65 72 61 74 65 20 6f 6e  t can operate on
bc7f0 20 61 6e 20 73 71 6c 69 74 65 33 5f 66 69 6c 65   an sqlite3_file
bc800 0a 2a 2a 20 66 6f 72 20 75 6e 69 78 20 77 69 74  .** for unix wit
bc810 68 20 64 6f 74 6c 6f 63 6b 20 73 74 79 6c 65 20  h dotlock style 
bc820 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f  file locking..*/
bc830 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71  .static const sq
bc840 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
bc850 20 73 71 6c 69 74 65 33 44 6f 74 6c 6f 63 6b 4c   sqlite3DotlockL
bc860 6f 63 6b 69 6e 67 55 6e 69 78 49 6f 4d 65 74 68  ockingUnixIoMeth
bc870 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20  od = {.  1,     
bc880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bc890 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a     /* iVersion *
bc8a0 2f 0a 20 20 64 6f 74 6c 6f 63 6b 55 6e 69 78 43  /.  dotlockUnixC
bc8b0 6c 6f 73 65 2c 0a 20 20 75 6e 69 78 52 65 61 64  lose,.  unixRead
bc8c0 2c 0a 20 20 75 6e 69 78 57 72 69 74 65 2c 0a 20  ,.  unixWrite,. 
bc8d0 20 75 6e 69 78 54 72 75 6e 63 61 74 65 2c 0a 20   unixTruncate,. 
bc8e0 20 75 6e 69 78 53 79 6e 63 2c 0a 20 20 75 6e 69   unixSync,.  uni
bc8f0 78 46 69 6c 65 53 69 7a 65 2c 0a 20 20 64 6f 74  xFileSize,.  dot
bc900 6c 6f 63 6b 55 6e 69 78 4c 6f 63 6b 2c 0a 20 20  lockUnixLock,.  
bc910 64 6f 74 6c 6f 63 6b 55 6e 69 78 55 6e 6c 6f 63  dotlockUnixUnloc
bc920 6b 2c 0a 20 20 64 6f 74 6c 6f 63 6b 55 6e 69 78  k,.  dotlockUnix
bc930 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
bc940 6b 2c 0a 20 20 75 6e 69 78 46 69 6c 65 43 6f 6e  k,.  unixFileCon
bc950 74 72 6f 6c 2c 0a 20 20 75 6e 69 78 53 65 63 74  trol,.  unixSect
bc960 6f 72 53 69 7a 65 2c 0a 20 20 75 6e 69 78 44 65  orSize,.  unixDe
bc970 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
bc980 69 63 73 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68  ics.};../*.** Th
bc990 69 73 20 76 65 63 74 6f 72 20 64 65 66 69 6e 65  is vector define
bc9a0 73 20 61 6c 6c 20 74 68 65 20 6d 65 74 68 6f 64  s all the method
bc9b0 73 20 74 68 61 74 20 63 61 6e 20 6f 70 65 72 61  s that can opera
bc9c0 74 65 20 6f 6e 20 61 6e 20 73 71 6c 69 74 65 33  te on an sqlite3
bc9d0 5f 66 69 6c 65 0a 2a 2a 20 66 6f 72 20 75 6e 69  _file.** for uni
bc9e0 78 20 77 69 74 68 20 6e 6f 6c 6f 63 6b 20 73 74  x with nolock st
bc9f0 79 6c 65 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67  yle file locking
bca00 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
bca10 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
bca20 68 6f 64 73 20 73 71 6c 69 74 65 33 4e 6f 6c 6f  hods sqlite3Nolo
bca30 63 6b 4c 6f 63 6b 69 6e 67 55 6e 69 78 49 6f 4d  ckLockingUnixIoM
bca40 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20  ethod = {.  1,  
bca50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bca60 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f        /* iVersio
bca70 6e 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 55 6e 69  n */.  nolockUni
bca80 78 43 6c 6f 73 65 2c 0a 20 20 75 6e 69 78 52 65  xClose,.  unixRe
bca90 61 64 2c 0a 20 20 75 6e 69 78 57 72 69 74 65 2c  ad,.  unixWrite,
bcaa0 0a 20 20 75 6e 69 78 54 72 75 6e 63 61 74 65 2c  .  unixTruncate,
bcab0 0a 20 20 75 6e 69 78 53 79 6e 63 2c 0a 20 20 75  .  unixSync,.  u
bcac0 6e 69 78 46 69 6c 65 53 69 7a 65 2c 0a 20 20 6e  nixFileSize,.  n
bcad0 6f 6c 6f 63 6b 55 6e 69 78 4c 6f 63 6b 2c 0a 20  olockUnixLock,. 
bcae0 20 6e 6f 6c 6f 63 6b 55 6e 69 78 55 6e 6c 6f 63   nolockUnixUnloc
bcaf0 6b 2c 0a 20 20 6e 6f 6c 6f 63 6b 55 6e 69 78 43  k,.  nolockUnixC
bcb00 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
bcb10 2c 0a 20 20 75 6e 69 78 46 69 6c 65 43 6f 6e 74  ,.  unixFileCont
bcb20 72 6f 6c 2c 0a 20 20 75 6e 69 78 53 65 63 74 6f  rol,.  unixSecto
bcb30 72 53 69 7a 65 2c 0a 20 20 75 6e 69 78 44 65 76  rSize,.  unixDev
bcb40 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
bcb50 63 73 0a 7d 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a  cs.};..#endif /*
bcb60 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
bcb70 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a  OCKING_STYLE */.
bcb80 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  ./*.** Allocate 
bcb90 6d 65 6d 6f 72 79 20 66 6f 72 20 61 20 6e 65 77  memory for a new
bcba0 20 75 6e 69 78 46 69 6c 65 20 61 6e 64 20 69 6e   unixFile and in
bcbb0 69 74 69 61 6c 69 7a 65 20 74 68 61 74 20 75 6e  itialize that un
bcbc0 69 78 46 69 6c 65 2e 0a 2a 2a 20 57 72 69 74 65  ixFile..** Write
bcbd0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
bcbe0 65 20 6e 65 77 20 75 6e 69 78 46 69 6c 65 20 69  e new unixFile i
bcbf0 6e 74 6f 20 2a 70 49 64 2e 0a 2a 2a 20 49 66 20  nto *pId..** If 
bcc00 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65  we run out of me
bcc10 6d 6f 72 79 2c 20 63 6c 6f 73 65 20 74 68 65 20  mory, close the 
bcc20 66 69 6c 65 20 61 6e 64 20 72 65 74 75 72 6e 20  file and return 
bcc30 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23 69 66  an error..*/.#if
bcc40 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
bcc50 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a  E_LOCKING_STYLE.
bcc60 2f 2a 20 0a 2a 2a 20 57 68 65 6e 20 6c 6f 63 6b  /* .** When lock
bcc70 69 6e 67 20 65 78 74 65 6e 73 69 6f 6e 73 20 61  ing extensions a
bcc80 72 65 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 20  re enabled, the 
bcc90 66 69 6c 65 70 61 74 68 20 61 6e 64 20 6c 6f 63  filepath and loc
bcca0 6b 69 6e 67 20 73 74 79 6c 65 20 0a 2a 2a 20 61  king style .** a
bccb0 72 65 20 6e 65 65 64 65 64 20 74 6f 20 64 65 74  re needed to det
bccc0 65 72 6d 69 6e 65 20 74 68 65 20 75 6e 69 78 46  ermine the unixF
bccd0 69 6c 65 20 70 4d 65 74 68 6f 64 20 74 6f 20 75  ile pMethod to u
bcce0 73 65 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 6f  se for locking o
bccf0 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68  perations..** Th
bcd00 65 20 6c 6f 63 6b 69 6e 67 2d 73 74 79 6c 65 20  e locking-style 
bcd10 73 70 65 63 69 66 69 63 20 6c 6f 63 6b 69 6e 67  specific locking
bcd20 43 6f 6e 74 65 78 74 20 64 61 74 61 20 73 74 72  Context data str
bcd30 75 63 74 75 72 65 20 69 73 20 63 72 65 61 74 65  ucture is create
bcd40 64 20 0a 2a 2a 20 61 6e 64 20 61 73 73 69 67 6e  d .** and assign
bcd50 65 64 20 68 65 72 65 20 61 6c 73 6f 2e 0a 2a 2f  ed here also..*/
bcd60 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c 6c  .static int fill
bcd70 49 6e 55 6e 69 78 46 69 6c 65 28 0a 20 20 69 6e  InUnixFile(.  in
bcd80 74 20 68 2c 20 20 20 20 20 20 20 20 20 20 20 20  t h,            
bcd90 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 66 69        /* Open fi
bcda0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f 66  le descriptor of
bcdb0 20 66 69 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e   file being open
bcdc0 65 64 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 66  ed */.  int dirf
bcdd0 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d,              
bcde0 2f 2a 20 44 69 72 65 63 74 6f 72 79 20 66 69 6c  /* Directory fil
bcdf0 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a  e descriptor */.
bce00 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
bce10 70 49 64 2c 20 20 20 20 20 20 2f 2a 20 57 72 69  pId,      /* Wri
bce20 74 65 20 74 6f 20 74 68 65 20 75 6e 69 78 46 69  te to the unixFi
bce30 6c 65 20 73 74 72 75 63 74 75 72 65 20 68 65 72  le structure her
bce40 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
bce50 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 20 20 20 2f  r *zFilename   /
bce60 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69  * Name of the fi
bce70 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20  le being opened 
bce80 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 4c  */.){.  sqlite3L
bce90 6f 63 6b 69 6e 67 53 74 79 6c 65 20 6c 6f 63 6b  ockingStyle lock
bcea0 69 6e 67 53 74 79 6c 65 3b 0a 20 20 75 6e 69 78  ingStyle;.  unix
bceb0 46 69 6c 65 20 2a 70 4e 65 77 20 3d 20 28 75 6e  File *pNew = (un
bcec0 69 78 46 69 6c 65 20 2a 29 70 49 64 3b 0a 20 20  ixFile *)pId;.  
bced0 69 6e 74 20 72 63 3b 0a 0a 23 69 66 64 65 66 20  int rc;..#ifdef 
bcee0 46 44 5f 43 4c 4f 45 58 45 43 0a 20 20 66 63 6e  FD_CLOEXEC.  fcn
bcef0 74 6c 28 68 2c 20 46 5f 53 45 54 46 44 2c 20 66  tl(h, F_SETFD, f
bcf00 63 6e 74 6c 28 68 2c 20 46 5f 47 45 54 46 44 2c  cntl(h, F_GETFD,
bcf10 20 30 29 20 7c 20 46 44 5f 43 4c 4f 45 58 45 43   0) | FD_CLOEXEC
bcf20 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 6c 6f 63  );.#endif..  loc
bcf30 6b 69 6e 67 53 74 79 6c 65 20 3d 20 73 71 6c 69  kingStyle = sqli
bcf40 74 65 33 44 65 74 65 63 74 4c 6f 63 6b 69 6e 67  te3DetectLocking
bcf50 53 74 79 6c 65 28 7a 46 69 6c 65 6e 61 6d 65 2c  Style(zFilename,
bcf60 20 68 29 3b 0a 20 20 69 66 20 28 20 6c 6f 63 6b   h);.  if ( lock
bcf70 69 6e 67 53 74 79 6c 65 3d 3d 70 6f 73 69 78 4c  ingStyle==posixL
bcf80 6f 63 6b 69 6e 67 53 74 79 6c 65 20 29 7b 0a 20  ockingStyle ){. 
bcf90 20 20 20 65 6e 74 65 72 4d 75 74 65 78 28 29 3b     enterMutex();
bcfa0 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c 6f  .    rc = findLo
bcfb0 63 6b 49 6e 66 6f 28 68 2c 20 26 70 4e 65 77 2d  ckInfo(h, &pNew-
bcfc0 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65 77 2d 3e 70  >pLock, &pNew->p
bcfd0 4f 70 65 6e 29 3b 0a 20 20 20 20 6c 65 61 76 65  Open);.    leave
bcfe0 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 69 66 28  Mutex();.    if(
bcff0 20 72 63 20 29 7b 0a 20 20 20 20 20 20 69 66 28   rc ){.      if(
bd000 20 64 69 72 66 64 3e 3d 30 20 29 20 63 6c 6f 73   dirfd>=0 ) clos
bd010 65 28 64 69 72 66 64 29 3b 0a 20 20 20 20 20 20  e(dirfd);.      
bd020 63 6c 6f 73 65 28 68 29 3b 0a 20 20 20 20 20 20  close(h);.      
bd030 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
bd040 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20  .  } else {.    
bd050 2f 2a 20 20 70 4c 6f 63 6b 20 61 6e 64 20 70 4f  /*  pLock and pO
bd060 70 65 6e 20 61 72 65 20 6f 6e 6c 79 20 75 73 65  pen are only use
bd070 64 20 66 6f 72 20 70 6f 73 69 78 20 61 64 76 69  d for posix advi
bd080 73 6f 72 79 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a  sory locking */.
bd090 20 20 20 20 70 4e 65 77 2d 3e 70 4c 6f 63 6b 20      pNew->pLock 
bd0a0 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 70 4e 65 77  = NULL;.    pNew
bd0b0 2d 3e 70 4f 70 65 6e 20 3d 20 4e 55 4c 4c 3b 0a  ->pOpen = NULL;.
bd0c0 20 20 7d 0a 0a 20 20 4f 53 54 52 41 43 45 33 28    }..  OSTRACE3(
bd0d0 22 4f 50 45 4e 20 20 20 20 25 2d 33 64 20 25 73  "OPEN    %-3d %s
bd0e0 5c 6e 22 2c 20 68 2c 20 7a 46 69 6c 65 6e 61 6d  \n", h, zFilenam
bd0f0 65 29 3b 20 20 20 20 0a 20 20 70 4e 65 77 2d 3e  e);    .  pNew->
bd100 64 69 72 66 64 20 3d 20 2d 31 3b 0a 20 20 70 4e  dirfd = -1;.  pN
bd110 65 77 2d 3e 68 20 3d 20 68 3b 0a 20 20 70 4e 65  ew->h = h;.  pNe
bd120 77 2d 3e 64 69 72 66 64 20 3d 20 64 69 72 66 64  w->dirfd = dirfd
bd130 3b 0a 20 20 53 45 54 5f 54 48 52 45 41 44 49 44  ;.  SET_THREADID
bd140 28 70 4e 65 77 29 3b 0a 20 20 20 20 0a 20 20 73  (pNew);.    .  s
bd150 77 69 74 63 68 28 6c 6f 63 6b 69 6e 67 53 74 79  witch(lockingSty
bd160 6c 65 29 20 7b 0a 20 20 20 20 63 61 73 65 20 61  le) {.    case a
bd170 66 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3a 20  fpLockingStyle: 
bd180 7b 0a 20 20 20 20 20 20 2f 2a 20 61 66 70 20 6c  {.      /* afp l
bd190 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20  ocking uses the 
bd1a0 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20  file path so it 
bd1b0 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e 63 6c  needs to be incl
bd1c0 75 64 65 64 20 69 6e 0a 20 20 20 20 20 20 2a 2a  uded in.      **
bd1d0 20 74 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43   the afpLockingC
bd1e0 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 20 20 20 20  ontext */.      
bd1f0 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  afpLockingContex
bd200 74 20 2a 63 6f 6e 74 65 78 74 3b 0a 20 20 20 20  t *context;.    
bd210 20 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 20    pNew->pMethod 
bd220 3d 20 26 73 71 6c 69 74 65 33 41 46 50 4c 6f 63  = &sqlite3AFPLoc
bd230 6b 69 6e 67 55 6e 69 78 49 6f 4d 65 74 68 6f 64  kingUnixIoMethod
bd240 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 6c 6f  ;.      pNew->lo
bd250 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 63  ckingContext = c
bd260 6f 6e 74 65 78 74 20 3d 20 73 71 6c 69 74 65 33  ontext = sqlite3
bd270 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28  _malloc( sizeof(
bd280 2a 63 6f 6e 74 65 78 74 29 20 29 3b 0a 20 20 20  *context) );.   
bd290 20 20 20 69 66 28 20 63 6f 6e 74 65 78 74 3d 3d     if( context==
bd2a0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6c 6f  0 ){.        clo
bd2b0 73 65 28 68 29 3b 0a 20 20 20 20 20 20 20 20 69  se(h);.        i
bd2c0 66 28 20 64 69 72 66 64 3e 3d 30 20 29 20 63 6c  f( dirfd>=0 ) cl
bd2d0 6f 73 65 28 64 69 72 66 64 29 3b 0a 20 20 20 20  ose(dirfd);.    
bd2e0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
bd2f0 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d  E_NOMEM;.      }
bd300 0a 0a 20 20 20 20 20 20 2f 2a 20 4e 42 3a 20 7a  ..      /* NB: z
bd310 46 69 6c 65 6e 61 6d 65 20 65 78 69 73 74 73 20  Filename exists 
bd320 61 6e 64 20 72 65 6d 61 69 6e 73 20 76 61 6c 69  and remains vali
bd330 64 20 75 6e 74 69 6c 20 74 68 65 20 66 69 6c 65  d until the file
bd340 20 69 73 20 63 6c 6f 73 65 64 0a 20 20 20 20 20   is closed.     
bd350 20 2a 2a 20 61 63 63 6f 72 64 69 6e 67 20 74 6f   ** according to
bd360 20 72 65 71 75 69 72 65 6d 65 6e 74 20 46 31 31   requirement F11
bd370 31 34 31 2e 20 20 53 6f 20 77 65 20 64 6f 20 6e  141.  So we do n
bd380 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20  ot need to make 
bd390 61 0a 20 20 20 20 20 20 2a 2a 20 63 6f 70 79 20  a.      ** copy 
bd3a0 6f 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 2e  of the filename.
bd3b0 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 74 65 78   */.      contex
bd3c0 74 2d 3e 66 69 6c 65 50 61 74 68 20 3d 20 7a 46  t->filePath = zF
bd3d0 69 6c 65 6e 61 6d 65 3b 0a 20 20 20 20 20 20 73  ilename;.      s
bd3e0 72 61 6e 64 6f 6d 64 65 76 28 29 3b 0a 20 20 20  randomdev();.   
bd3f0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
bd400 20 20 20 20 63 61 73 65 20 66 6c 6f 63 6b 4c 6f      case flockLo
bd410 63 6b 69 6e 67 53 74 79 6c 65 3a 0a 20 20 20 20  ckingStyle:.    
bd420 20 20 2f 2a 20 66 6c 6f 63 6b 20 6c 6f 63 6b 69    /* flock locki
bd430 6e 67 20 64 6f 65 73 6e 27 74 20 6e 65 65 64 20  ng doesn't need 
bd440 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 69  additional locki
bd450 6e 67 43 6f 6e 74 65 78 74 20 69 6e 66 6f 72 6d  ngContext inform
bd460 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 70  ation */.      p
bd470 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26  New->pMethod = &
bd480 73 71 6c 69 74 65 33 46 6c 6f 63 6b 4c 6f 63 6b  sqlite3FlockLock
bd490 69 6e 67 55 6e 69 78 49 6f 4d 65 74 68 6f 64 3b  ingUnixIoMethod;
bd4a0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
bd4b0 20 20 63 61 73 65 20 64 6f 74 6c 6f 63 6b 4c 6f    case dotlockLo
bd4c0 63 6b 69 6e 67 53 74 79 6c 65 3a 20 7b 0a 20 20  ckingStyle: {.  
bd4d0 20 20 20 20 2f 2a 20 64 6f 74 6c 6f 63 6b 20 6c      /* dotlock l
bd4e0 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20  ocking uses the 
bd4f0 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20  file path so it 
bd500 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e 63 6c  needs to be incl
bd510 75 64 65 64 20 69 6e 0a 20 20 20 20 20 20 2a 2a  uded in.      **
bd520 20 74 68 65 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b   the dotlockLock
bd530 69 6e 67 43 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ingContext */.  
bd540 20 20 20 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69      dotlockLocki
bd550 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  ngContext *conte
bd560 78 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 46  xt;.      int nF
bd570 69 6c 65 6e 61 6d 65 3b 0a 20 20 20 20 20 20 6e  ilename;.      n
bd580 46 69 6c 65 6e 61 6d 65 20 3d 20 73 74 72 6c 65  Filename = strle
bd590 6e 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20  n(zFilename);.  
bd5a0 20 20 20 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f      pNew->pMetho
bd5b0 64 20 3d 20 26 73 71 6c 69 74 65 33 44 6f 74 6c  d = &sqlite3Dotl
bd5c0 6f 63 6b 4c 6f 63 6b 69 6e 67 55 6e 69 78 49 6f  ockLockingUnixIo
bd5d0 4d 65 74 68 6f 64 3b 0a 20 20 20 20 20 20 70 4e  Method;.      pN
bd5e0 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  ew->lockingConte
bd5f0 78 74 20 3d 20 63 6f 6e 74 65 78 74 20 3d 20 0a  xt = context = .
bd600 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
bd610 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28  _malloc( sizeof(
bd620 2a 63 6f 6e 74 65 78 74 29 20 2b 20 6e 46 69 6c  *context) + nFil
bd630 65 6e 61 6d 65 20 2b 20 36 20 29 3b 0a 20 20 20  ename + 6 );.   
bd640 20 20 20 69 66 28 20 63 6f 6e 74 65 78 74 3d 3d     if( context==
bd650 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6c 6f  0 ){.        clo
bd660 73 65 28 68 29 3b 0a 20 20 20 20 20 20 20 20 69  se(h);.        i
bd670 66 28 20 64 69 72 66 64 3e 3d 30 20 29 20 63 6c  f( dirfd>=0 ) cl
bd680 6f 73 65 28 64 69 72 66 64 29 3b 0a 20 20 20 20  ose(dirfd);.    
bd690 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
bd6a0 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d  E_NOMEM;.      }
bd6b0 0a 20 20 20 20 20 20 63 6f 6e 74 65 78 74 2d 3e  .      context->
bd6c0 6c 6f 63 6b 50 61 74 68 20 3d 20 28 63 68 61 72  lockPath = (char
bd6d0 2a 29 26 63 6f 6e 74 65 78 74 5b 31 5d 3b 0a 20  *)&context[1];. 
bd6e0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
bd6f0 72 69 6e 74 66 28 6e 46 69 6c 65 6e 61 6d 65 2c  rintf(nFilename,
bd700 20 63 6f 6e 74 65 78 74 2d 3e 6c 6f 63 6b 50 61   context->lockPa
bd710 74 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  th,.            
bd720 20 20 20 20 20 20 20 20 20 20 20 22 25 73 2e 6c             "%s.l
bd730 6f 63 6b 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29  ock", zFilename)
bd740 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
bd750 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 70 6f     }.    case po
bd760 73 69 78 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3a  sixLockingStyle:
bd770 0a 20 20 20 20 20 20 2f 2a 20 70 6f 73 69 78 20  .      /* posix 
bd780 6c 6f 63 6b 69 6e 67 20 64 6f 65 73 6e 27 74 20  locking doesn't 
bd790 6e 65 65 64 20 61 64 64 69 74 69 6f 6e 61 6c 20  need additional 
bd7a0 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 69  lockingContext i
bd7b0 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20  nformation */.  
bd7c0 20 20 20 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f      pNew->pMetho
bd7d0 64 20 3d 20 26 73 71 6c 69 74 65 33 55 6e 69 78  d = &sqlite3Unix
bd7e0 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 20 20 20 20  IoMethod;.      
bd7f0 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
bd800 6e 6f 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3a 0a  noLockingStyle:.
bd810 20 20 20 20 63 61 73 65 20 75 6e 73 75 70 70 6f      case unsuppo
bd820 72 74 65 64 4c 6f 63 6b 69 6e 67 53 74 79 6c 65  rtedLockingStyle
bd830 3a 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 0a  :.    default: .
bd840 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4d 65 74        pNew->pMet
bd850 68 6f 64 20 3d 20 26 73 71 6c 69 74 65 33 4e 6f  hod = &sqlite3No
bd860 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 55 6e 69 78 49  lockLockingUnixI
bd870 6f 4d 65 74 68 6f 64 3b 0a 20 20 7d 0a 20 20 4f  oMethod;.  }.  O
bd880 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a  penCounter(+1);.
bd890 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
bd8a0 4f 4b 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 53  OK;.}.#else /* S
bd8b0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
bd8c0 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 73 74  KING_STYLE */.st
bd8d0 61 74 69 63 20 69 6e 74 20 66 69 6c 6c 49 6e 55  atic int fillInU
bd8e0 6e 69 78 46 69 6c 65 28 0a 20 20 69 6e 74 20 68  nixFile(.  int h
bd8f0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
bd900 20 20 2f 2a 20 4f 70 65 6e 20 66 69 6c 65 20 64    /* Open file d
bd910 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 66 69 6c  escriptor on fil
bd920 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 2a  e being opened *
bd930 2f 0a 20 20 69 6e 74 20 64 69 72 66 64 2c 0a 20  /.  int dirfd,. 
bd940 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
bd950 49 64 2c 20 20 20 20 20 2f 2a 20 57 72 69 74 65  Id,     /* Write
bd960 20 74 6f 20 74 68 65 20 75 6e 69 78 46 69 6c 65   to the unixFile
bd970 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65 20   structure here 
bd980 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
bd990 2a 7a 46 69 6c 65 6e 61 6d 65 20 20 2f 2a 20 4e  *zFilename  /* N
bd9a0 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20  ame of the file 
bd9b0 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f 0a  being opened */.
bd9c0 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  ){.  unixFile *p
bd9d0 4e 65 77 20 3d 20 28 75 6e 69 78 46 69 6c 65 20  New = (unixFile 
bd9e0 2a 29 70 49 64 3b 0a 20 20 69 6e 74 20 72 63 3b  *)pId;.  int rc;
bd9f0 0a 0a 23 69 66 64 65 66 20 46 44 5f 43 4c 4f 45  ..#ifdef FD_CLOE
bda00 58 45 43 0a 20 20 66 63 6e 74 6c 28 68 2c 20 46  XEC.  fcntl(h, F
bda10 5f 53 45 54 46 44 2c 20 66 63 6e 74 6c 28 68 2c  _SETFD, fcntl(h,
bda20 20 46 5f 47 45 54 46 44 2c 20 30 29 20 7c 20 46   F_GETFD, 0) | F
bda30 44 5f 43 4c 4f 45 58 45 43 29 3b 0a 23 65 6e 64  D_CLOEXEC);.#end
bda40 69 66 0a 0a 20 20 65 6e 74 65 72 4d 75 74 65 78  if..  enterMutex
bda50 28 29 3b 0a 20 20 72 63 20 3d 20 66 69 6e 64 4c  ();.  rc = findL
bda60 6f 63 6b 49 6e 66 6f 28 68 2c 20 26 70 4e 65 77  ockInfo(h, &pNew
bda70 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65 77 2d 3e  ->pLock, &pNew->
bda80 70 4f 70 65 6e 29 3b 0a 20 20 6c 65 61 76 65 4d  pOpen);.  leaveM
bda90 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 72 63  utex();.  if( rc
bdaa0 20 29 7b 0a 20 20 20 20 69 66 28 20 64 69 72 66   ){.    if( dirf
bdab0 64 3e 3d 30 20 29 20 63 6c 6f 73 65 28 64 69 72  d>=0 ) close(dir
bdac0 66 64 29 3b 0a 20 20 20 20 63 6c 6f 73 65 28 68  fd);.    close(h
bdad0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  );.    return rc
bdae0 3b 0a 20 20 7d 0a 0a 20 20 4f 53 54 52 41 43 45  ;.  }..  OSTRACE
bdaf0 33 28 22 4f 50 45 4e 20 20 20 20 25 2d 33 64 20  3("OPEN    %-3d 
bdb00 25 73 5c 6e 22 2c 20 68 2c 20 7a 46 69 6c 65 6e  %s\n", h, zFilen
bdb10 61 6d 65 29 3b 0a 20 20 70 4e 65 77 2d 3e 64 69  ame);.  pNew->di
bdb20 72 66 64 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77  rfd = -1;.  pNew
bdb30 2d 3e 68 20 3d 20 68 3b 0a 20 20 70 4e 65 77 2d  ->h = h;.  pNew-
bdb40 3e 64 69 72 66 64 20 3d 20 64 69 72 66 64 3b 0a  >dirfd = dirfd;.
bdb50 20 20 53 45 54 5f 54 48 52 45 41 44 49 44 28 70    SET_THREADID(p
bdb60 4e 65 77 29 3b 0a 0a 20 20 70 4e 65 77 2d 3e 70  New);..  pNew->p
bdb70 4d 65 74 68 6f 64 20 3d 20 26 73 71 6c 69 74 65  Method = &sqlite
bdb80 33 55 6e 69 78 49 6f 4d 65 74 68 6f 64 3b 0a 20  3UnixIoMethod;. 
bdb90 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29   OpenCounter(+1)
bdba0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
bdbb0 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  E_OK;.}.#endif /
bdbc0 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  * SQLITE_ENABLE_
bdbd0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f  LOCKING_STYLE */
bdbe0 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 66  ../*.** Open a f
bdbf0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74  ile descriptor t
bdc00 6f 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  o the directory 
bdc10 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69 6c 65 20  containing file 
bdc20 7a 46 69 6c 65 6e 61 6d 65 2e 0a 2a 2a 20 49 66  zFilename..** If
bdc30 20 73 75 63 63 65 73 73 66 75 6c 2c 20 2a 70 46   successful, *pF
bdc40 64 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  d is set to the 
bdc50 6f 70 65 6e 65 64 20 66 69 6c 65 20 64 65 73 63  opened file desc
bdc60 72 69 70 74 6f 72 20 61 6e 64 0a 2a 2a 20 53 51  riptor and.** SQ
bdc70 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
bdc80 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72  ned. If an error
bdc90 20 6f 63 63 75 72 73 2c 20 65 69 74 68 65 72 20   occurs, either 
bdca0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20  SQLITE_NOMEM.** 
bdcb0 6f 72 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50  or SQLITE_CANTOP
bdcc0 45 4e 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  EN is returned a
bdcd0 6e 64 20 2a 70 46 64 20 69 73 20 73 65 74 20 74  nd *pFd is set t
bdce0 6f 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a  o an undefined.*
bdcf0 2a 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 49  * value..**.** I
bdd00 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  f SQLITE_OK is r
bdd10 65 74 75 72 6e 65 64 2c 20 74 68 65 20 63 61 6c  eturned, the cal
bdd20 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62  ler is responsib
bdd30 6c 65 20 66 6f 72 20 63 6c 6f 73 69 6e 67 0a 2a  le for closing.*
bdd40 2a 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72  * the file descr
bdd50 69 70 74 6f 72 20 2a 70 46 64 20 75 73 69 6e 67  iptor *pFd using
bdd60 20 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a 73 74 61   close()..*/.sta
bdd70 74 69 63 20 69 6e 74 20 6f 70 65 6e 44 69 72 65  tic int openDire
bdd80 63 74 6f 72 79 28 63 6f 6e 73 74 20 63 68 61 72  ctory(const char
bdd90 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 69 6e 74   *zFilename, int
bdda0 20 2a 70 46 64 29 7b 0a 20 20 69 6e 74 20 69 69   *pFd){.  int ii
bddb0 3b 0a 20 20 69 6e 74 20 66 64 20 3d 20 2d 31 3b  ;.  int fd = -1;
bddc0 0a 20 20 63 68 61 72 20 7a 44 69 72 6e 61 6d 65  .  char zDirname
bddd0 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d  [MAX_PATHNAME+1]
bdde0 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  ;..  sqlite3_snp
bddf0 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e 41  rintf(MAX_PATHNA
bde00 4d 45 2c 20 7a 44 69 72 6e 61 6d 65 2c 20 22 25  ME, zDirname, "%
bde10 73 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  s", zFilename);.
bde20 20 20 66 6f 72 28 69 69 3d 73 74 72 6c 65 6e 28    for(ii=strlen(
bde30 7a 44 69 72 6e 61 6d 65 29 3b 20 69 69 3e 3d 30  zDirname); ii>=0
bde40 20 26 26 20 7a 44 69 72 6e 61 6d 65 5b 69 69 5d   && zDirname[ii]
bde50 21 3d 27 2f 27 3b 20 69 69 2d 2d 29 3b 0a 20 20  !='/'; ii--);.  
bde60 69 66 28 20 69 69 3e 30 20 29 7b 0a 20 20 20 20  if( ii>0 ){.    
bde70 7a 44 69 72 6e 61 6d 65 5b 69 69 5d 20 3d 20 27  zDirname[ii] = '
bde80 5c 30 27 3b 0a 20 20 20 20 66 64 20 3d 20 6f 70  \0';.    fd = op
bde90 65 6e 28 7a 44 69 72 6e 61 6d 65 2c 20 4f 5f 52  en(zDirname, O_R
bdea0 44 4f 4e 4c 59 7c 4f 5f 42 49 4e 41 52 59 2c 20  DONLY|O_BINARY, 
bdeb0 30 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3e 3d  0);.    if( fd>=
bdec0 30 20 29 7b 0a 23 69 66 64 65 66 20 46 44 5f 43  0 ){.#ifdef FD_C
bded0 4c 4f 45 58 45 43 0a 20 20 20 20 20 20 66 63 6e  LOEXEC.      fcn
bdee0 74 6c 28 66 64 2c 20 46 5f 53 45 54 46 44 2c 20  tl(fd, F_SETFD, 
bdef0 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47 45 54 46  fcntl(fd, F_GETF
bdf00 44 2c 20 30 29 20 7c 20 46 44 5f 43 4c 4f 45 58  D, 0) | FD_CLOEX
bdf10 45 43 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  EC);.#endif.    
bdf20 20 20 4f 53 54 52 41 43 45 33 28 22 4f 50 45 4e    OSTRACE3("OPEN
bdf30 44 49 52 20 25 2d 33 64 20 25 73 5c 6e 22 2c 20  DIR %-3d %s\n", 
bdf40 66 64 2c 20 7a 44 69 72 6e 61 6d 65 29 3b 0a 20  fd, zDirname);. 
bdf50 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 46 64 20     }.  }.  *pFd 
bdf60 3d 20 66 64 3b 0a 20 20 72 65 74 75 72 6e 20 28  = fd;.  return (
bdf70 66 64 3e 3d 30 3f 53 51 4c 49 54 45 5f 4f 4b 3a  fd>=0?SQLITE_OK:
bdf80 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 29  SQLITE_CANTOPEN)
bdf90 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20  ;.}../*.** Open 
bdfa0 74 68 65 20 66 69 6c 65 20 7a 50 61 74 68 2e 0a  the file zPath..
bdfb0 2a 2a 20 0a 2a 2a 20 50 72 65 76 69 6f 75 73 6c  ** .** Previousl
bdfc0 79 2c 20 74 68 65 20 53 51 4c 69 74 65 20 4f 53  y, the SQLite OS
bdfd0 20 6c 61 79 65 72 20 75 73 65 64 20 74 68 72 65   layer used thre
bdfe0 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 70  e functions in p
bdff0 6c 61 63 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20  lace of this.** 
be000 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73  one:.**.**     s
be010 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 64  qlite3OsOpenRead
be020 57 72 69 74 65 28 29 3b 0a 2a 2a 20 20 20 20 20  Write();.**     
be030 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61  sqlite3OsOpenRea
be040 64 4f 6e 6c 79 28 29 3b 0a 2a 2a 20 20 20 20 20  dOnly();.**     
be050 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 45 78 63  sqlite3OsOpenExc
be060 6c 75 73 69 76 65 28 29 3b 0a 2a 2a 0a 2a 2a 20  lusive();.**.** 
be070 54 68 65 73 65 20 63 61 6c 6c 73 20 63 6f 72 72  These calls corr
be080 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 20 66 6f  espond to the fo
be090 6c 6c 6f 77 69 6e 67 20 63 6f 6d 62 69 6e 61 74  llowing combinat
be0a0 69 6f 6e 73 20 6f 66 20 66 6c 61 67 73 3a 0a 2a  ions of flags:.*
be0b0 2a 0a 2a 2a 20 20 20 20 20 52 65 61 64 57 72 69  *.**     ReadWri
be0c0 74 65 28 29 20 2d 3e 20 20 20 20 20 28 52 45 41  te() ->     (REA
be0d0 44 57 52 49 54 45 20 7c 20 43 52 45 41 54 45 29  DWRITE | CREATE)
be0e0 0a 2a 2a 20 20 20 20 20 52 65 61 64 4f 6e 6c 79  .**     ReadOnly
be0f0 28 29 20 20 2d 3e 20 20 20 20 20 28 52 45 41 44  ()  ->     (READ
be100 4f 4e 4c 59 29 20 0a 2a 2a 20 20 20 20 20 4f 70  ONLY) .**     Op
be110 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20 2d 3e  enExclusive() ->
be120 20 28 52 45 41 44 57 52 49 54 45 20 7c 20 43 52   (READWRITE | CR
be130 45 41 54 45 20 7c 20 45 58 43 4c 55 53 49 56 45  EATE | EXCLUSIVE
be140 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6c 64 20  ).**.** The old 
be150 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20  OpenExclusive() 
be160 61 63 63 65 70 74 65 64 20 61 20 62 6f 6f 6c 65  accepted a boole
be170 61 6e 20 61 72 67 75 6d 65 6e 74 20 2d 20 22 64  an argument - "d
be180 65 6c 46 6c 61 67 22 2e 20 49 66 0a 2a 2a 20 74  elFlag". If.** t
be190 72 75 65 2c 20 74 68 65 20 66 69 6c 65 20 77 61  rue, the file wa
be1a0 73 20 63 6f 6e 66 69 67 75 72 65 64 20 74 6f 20  s configured to 
be1b0 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  be automatically
be1c0 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68   deleted when th
be1d0 65 0a 2a 2a 20 66 69 6c 65 20 68 61 6e 64 6c 65  e.** file handle
be1e0 20 63 6c 6f 73 65 64 2e 20 54 6f 20 61 63 68 69   closed. To achi
be1f0 65 76 65 20 74 68 65 20 73 61 6d 65 20 65 66 66  eve the same eff
be200 65 63 74 20 75 73 69 6e 67 20 74 68 69 73 20 6e  ect using this n
be210 65 77 20 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  ew .** interface
be220 2c 20 61 64 64 20 74 68 65 20 44 45 4c 45 54 45  , add the DELETE
be230 4f 4e 43 4c 4f 53 45 20 66 6c 61 67 20 74 6f 20  ONCLOSE flag to 
be240 74 68 6f 73 65 20 73 70 65 63 69 66 69 65 64 20  those specified 
be250 61 62 6f 76 65 20 66 6f 72 20 0a 2a 2a 20 4f 70  above for .** Op
be260 65 6e 45 78 63 6c 75 73 69 76 65 28 29 2e 0a 2a  enExclusive()..*
be270 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
be280 78 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33  xOpen(.  sqlite3
be290 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63  _vfs *pVfs, .  c
be2a0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68  onst char *zPath
be2b0 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  , .  sqlite3_fil
be2c0 65 20 2a 70 46 69 6c 65 2c 0a 20 20 69 6e 74 20  e *pFile,.  int 
be2d0 66 6c 61 67 73 2c 0a 20 20 69 6e 74 20 2a 70 4f  flags,.  int *pO
be2e0 75 74 46 6c 61 67 73 0a 29 7b 0a 20 20 69 6e 74  utFlags.){.  int
be2f0 20 66 64 20 3d 20 30 3b 20 20 20 20 20 20 20 20   fd = 0;        
be300 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
be310 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 72  ile descriptor r
be320 65 74 75 72 6e 65 64 20 62 79 20 6f 70 65 6e 28  eturned by open(
be330 29 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 66 64  ) */.  int dirfd
be340 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 20   = -1;          
be350 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63 74 6f        /* Directo
be360 72 79 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ry file descript
be370 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 6c 61  or */.  int ofla
be380 67 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  gs = 0;         
be390 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
be3a0 74 6f 20 70 61 73 73 20 74 6f 20 6f 70 65 6e 28  to pass to open(
be3b0 29 20 2a 2f 0a 20 20 69 6e 74 20 65 54 79 70 65  ) */.  int eType
be3c0 20 3d 20 66 6c 61 67 73 26 30 78 46 46 46 46 46   = flags&0xFFFFF
be3d0 46 30 30 3b 20 20 2f 2a 20 54 79 70 65 20 6f 66  F00;  /* Type of
be3e0 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f   file to open */
be3f0 0a 0a 20 20 69 6e 74 20 69 73 45 78 63 6c 75 73  ..  int isExclus
be400 69 76 65 20 20 3d 20 28 66 6c 61 67 73 20 26 20  ive  = (flags & 
be410 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c  SQLITE_OPEN_EXCL
be420 55 53 49 56 45 29 3b 0a 20 20 69 6e 74 20 69 73  USIVE);.  int is
be430 44 65 6c 65 74 65 20 20 20 20 20 3d 20 28 66 6c  Delete     = (fl
be440 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
be450 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 29  N_DELETEONCLOSE)
be460 3b 0a 20 20 69 6e 74 20 69 73 43 72 65 61 74 65  ;.  int isCreate
be470 20 20 20 20 20 3d 20 28 66 6c 61 67 73 20 26 20       = (flags & 
be480 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
be490 54 45 29 3b 0a 20 20 69 6e 74 20 69 73 52 65 61  TE);.  int isRea
be4a0 64 6f 6e 6c 79 20 20 20 3d 20 28 66 6c 61 67 73  donly   = (flags
be4b0 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52   & SQLITE_OPEN_R
be4c0 45 41 44 4f 4e 4c 59 29 3b 0a 20 20 69 6e 74 20  EADONLY);.  int 
be4d0 69 73 52 65 61 64 57 72 69 74 65 20 20 3d 20 28  isReadWrite  = (
be4e0 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
be4f0 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29 3b 0a  PEN_READWRITE);.
be500 0a 20 20 2f 2a 20 49 66 20 63 72 65 61 74 69 6e  .  /* If creatin
be510 67 20 61 20 6d 61 73 74 65 72 20 6f 72 20 6d 61  g a master or ma
be520 69 6e 2d 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 2c  in-file journal,
be530 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77   this function w
be540 69 6c 6c 20 6f 70 65 6e 0a 20 20 2a 2a 20 61 20  ill open.  ** a 
be550 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f 72 20  file-descriptor 
be560 6f 6e 20 74 68 65 20 64 69 72 65 63 74 6f 72 79  on the directory
be570 20 74 6f 6f 2e 20 54 68 65 20 66 69 72 73 74 20   too. The first 
be580 74 69 6d 65 20 75 6e 69 78 53 79 6e 63 28 29 0a  time unixSync().
be590 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 74    ** is called t
be5a0 68 65 20 64 69 72 65 63 74 6f 72 79 20 66 69 6c  he directory fil
be5b0 65 20 64 65 73 63 72 69 70 74 6f 72 20 77 69 6c  e descriptor wil
be5c0 6c 20 62 65 20 66 73 79 6e 63 28 29 65 64 20 61  l be fsync()ed a
be5d0 6e 64 20 63 6c 6f 73 65 28 29 64 2e 0a 20 20 2a  nd close()d..  *
be5e0 2f 0a 20 20 69 6e 74 20 69 73 4f 70 65 6e 44 69  /.  int isOpenDi
be5f0 72 65 63 74 6f 72 79 20 3d 20 28 69 73 43 72 65  rectory = (isCre
be600 61 74 65 20 26 26 20 0a 20 20 20 20 20 20 28 65  ate && .      (e
be610 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45  Type==SQLITE_OPE
be620 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c  N_MASTER_JOURNAL
be630 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54   || eType==SQLIT
be640 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52  E_OPEN_MAIN_JOUR
be650 4e 41 4c 29 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20  NAL).  );..  /* 
be660 43 68 65 63 6b 20 74 68 65 20 66 6f 6c 6c 6f 77  Check the follow
be670 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 20 61  ing statements a
be680 72 65 20 74 72 75 65 3a 20 0a 20 20 2a 2a 0a 20  re true: .  **. 
be690 20 2a 2a 20 20 20 28 61 29 20 45 78 61 63 74 6c   **   (a) Exactl
be6a0 79 20 6f 6e 65 20 6f 66 20 74 68 65 20 52 45 41  y one of the REA
be6b0 44 57 52 49 54 45 20 61 6e 64 20 52 45 41 44 4f  DWRITE and READO
be6c0 4e 4c 59 20 66 6c 61 67 73 20 6d 75 73 74 20 62  NLY flags must b
be6d0 65 20 73 65 74 2c 20 61 6e 64 20 0a 20 20 2a 2a  e set, and .  **
be6e0 20 20 20 28 62 29 20 69 66 20 43 52 45 41 54 45     (b) if CREATE
be6f0 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 52 45   is set, then RE
be700 41 44 57 52 49 54 45 20 6d 75 73 74 20 61 6c 73  ADWRITE must als
be710 6f 20 62 65 20 73 65 74 2c 20 61 6e 64 0a 20 20  o be set, and.  
be720 2a 2a 20 20 20 28 63 29 20 69 66 20 45 58 43 4c  **   (c) if EXCL
be730 55 53 49 56 45 20 69 73 20 73 65 74 2c 20 74 68  USIVE is set, th
be740 65 6e 20 43 52 45 41 54 45 20 6d 75 73 74 20 61  en CREATE must a
be750 6c 73 6f 20 62 65 20 73 65 74 2e 0a 20 20 2a 2a  lso be set..  **
be760 20 20 20 28 64 29 20 69 66 20 44 45 4c 45 54 45     (d) if DELETE
be770 4f 4e 43 4c 4f 53 45 20 69 73 20 73 65 74 2c 20  ONCLOSE is set, 
be780 74 68 65 6e 20 43 52 45 41 54 45 20 6d 75 73 74  then CREATE must
be790 20 61 6c 73 6f 20 62 65 20 73 65 74 2e 0a 20 20   also be set..  
be7a0 2a 2f 0a 20 20 61 73 73 65 72 74 28 28 69 73 52  */.  assert((isR
be7b0 65 61 64 6f 6e 6c 79 3d 3d 30 20 7c 7c 20 69 73  eadonly==0 || is
be7c0 52 65 61 64 57 72 69 74 65 3d 3d 30 29 20 26 26  ReadWrite==0) &&
be7d0 20 28 69 73 52 65 61 64 57 72 69 74 65 20 7c 7c   (isReadWrite ||
be7e0 20 69 73 52 65 61 64 6f 6e 6c 79 29 29 3b 0a 20   isReadonly));. 
be7f0 20 61 73 73 65 72 74 28 69 73 43 72 65 61 74 65   assert(isCreate
be800 3d 3d 30 20 7c 7c 20 69 73 52 65 61 64 57 72 69  ==0 || isReadWri
be810 74 65 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73  te);.  assert(is
be820 45 78 63 6c 75 73 69 76 65 3d 3d 30 20 7c 7c 20  Exclusive==0 || 
be830 69 73 43 72 65 61 74 65 29 3b 0a 20 20 61 73 73  isCreate);.  ass
be840 65 72 74 28 69 73 44 65 6c 65 74 65 3d 3d 30 20  ert(isDelete==0 
be850 7c 7c 20 69 73 43 72 65 61 74 65 29 3b 0a 0a 0a  || isCreate);...
be860 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 44 42    /* The main DB
be870 2c 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 2c 20  , main journal, 
be880 61 6e 64 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  and master journ
be890 61 6c 20 61 72 65 20 6e 65 76 65 72 20 61 75 74  al are never aut
be8a0 6f 6d 61 74 69 63 61 6c 6c 79 0a 20 20 2a 2a 20  omatically.  ** 
be8b0 64 65 6c 65 74 65 64 0a 20 20 2a 2f 0a 20 20 61  deleted.  */.  a
be8c0 73 73 65 72 74 28 20 65 54 79 70 65 21 3d 53 51  ssert( eType!=SQ
be8d0 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44  LITE_OPEN_MAIN_D
be8e0 42 20 7c 7c 20 21 69 73 44 65 6c 65 74 65 20 29  B || !isDelete )
be8f0 3b 0a 20 20 61 73 73 65 72 74 28 20 65 54 79 70  ;.  assert( eTyp
be900 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  e!=SQLITE_OPEN_M
be910 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 21  AIN_JOURNAL || !
be920 69 73 44 65 6c 65 74 65 20 29 3b 0a 20 20 61 73  isDelete );.  as
be930 73 65 72 74 28 20 65 54 79 70 65 21 3d 53 51 4c  sert( eType!=SQL
be940 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f  ITE_OPEN_MASTER_
be950 4a 4f 55 52 4e 41 4c 20 7c 7c 20 21 69 73 44 65  JOURNAL || !isDe
be960 6c 65 74 65 20 29 3b 0a 0a 20 20 2f 2a 20 41 73  lete );..  /* As
be970 73 65 72 74 20 74 68 61 74 20 74 68 65 20 75 70  sert that the up
be980 70 65 72 20 6c 61 79 65 72 20 68 61 73 20 73 65  per layer has se
be990 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 22 66 69  t one of the "fi
be9a0 6c 65 2d 74 79 70 65 22 20 66 6c 61 67 73 2e 20  le-type" flags. 
be9b0 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 65 54 79  */.  assert( eTy
be9c0 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  pe==SQLITE_OPEN_
be9d0 4d 41 49 4e 5f 44 42 20 20 20 20 20 20 7c 7c 20  MAIN_DB      || 
be9e0 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50  eType==SQLITE_OP
be9f0 45 4e 5f 54 45 4d 50 5f 44 42 20 0a 20 20 20 20  EN_TEMP_DB .    
bea00 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c     || eType==SQL
bea10 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f  ITE_OPEN_MAIN_JO
bea20 55 52 4e 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d  URNAL || eType==
bea30 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50  SQLITE_OPEN_TEMP
bea40 5f 4a 4f 55 52 4e 41 4c 20 0a 20 20 20 20 20 20  _JOURNAL .      
bea50 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54   || eType==SQLIT
bea60 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41  E_OPEN_SUBJOURNA
bea70 4c 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51  L   || eType==SQ
bea80 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52  LITE_OPEN_MASTER
bea90 5f 4a 4f 55 52 4e 41 4c 20 0a 20 20 20 20 20 20  _JOURNAL .      
beaa0 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54   || eType==SQLIT
beab0 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54  E_OPEN_TRANSIENT
beac0 5f 44 42 0a 20 20 29 3b 0a 0a 20 20 69 66 28 20  _DB.  );..  if( 
bead0 69 73 52 65 61 64 6f 6e 6c 79 20 29 20 20 6f 66  isReadonly )  of
beae0 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 4f 4e 4c 59  lags |= O_RDONLY
beaf0 3b 0a 20 20 69 66 28 20 69 73 52 65 61 64 57 72  ;.  if( isReadWr
beb00 69 74 65 20 29 20 6f 66 6c 61 67 73 20 7c 3d 20  ite ) oflags |= 
beb10 4f 5f 52 44 57 52 3b 0a 20 20 69 66 28 20 69 73  O_RDWR;.  if( is
beb20 43 72 65 61 74 65 20 29 20 20 20 20 6f 66 6c 61  Create )    ofla
beb30 67 73 20 7c 3d 20 4f 5f 43 52 45 41 54 3b 0a 20  gs |= O_CREAT;. 
beb40 20 69 66 28 20 69 73 45 78 63 6c 75 73 69 76 65   if( isExclusive
beb50 20 29 20 6f 66 6c 61 67 73 20 7c 3d 20 28 4f 5f   ) oflags |= (O_
beb60 45 58 43 4c 7c 4f 5f 4e 4f 46 4f 4c 4c 4f 57 29  EXCL|O_NOFOLLOW)
beb70 3b 0a 20 20 6f 66 6c 61 67 73 20 7c 3d 20 28 4f  ;.  oflags |= (O
beb80 5f 4c 41 52 47 45 46 49 4c 45 7c 4f 5f 42 49 4e  _LARGEFILE|O_BIN
beb90 41 52 59 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28  ARY);..  memset(
beba0 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f 66  pFile, 0, sizeof
bebb0 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 20 20 66  (unixFile));.  f
bebc0 64 20 3d 20 6f 70 65 6e 28 7a 50 61 74 68 2c 20  d = open(zPath, 
bebd0 6f 66 6c 61 67 73 2c 20 69 73 44 65 6c 65 74 65  oflags, isDelete
bebe0 3f 30 36 30 30 3a 53 51 4c 49 54 45 5f 44 45 46  ?0600:SQLITE_DEF
bebf0 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53  AULT_FILE_PERMIS
bec00 53 49 4f 4e 53 29 3b 0a 20 20 69 66 28 20 66 64  SIONS);.  if( fd
bec10 3c 30 20 26 26 20 65 72 72 6e 6f 21 3d 45 49 53  <0 && errno!=EIS
bec20 44 49 52 20 26 26 20 69 73 52 65 61 64 57 72 69  DIR && isReadWri
bec30 74 65 20 26 26 20 21 69 73 45 78 63 6c 75 73 69  te && !isExclusi
bec40 76 65 20 29 7b 0a 20 20 20 20 2f 2a 20 46 61 69  ve ){.    /* Fai
bec50 6c 65 64 20 74 6f 20 6f 70 65 6e 20 74 68 65 20  led to open the 
bec60 66 69 6c 65 20 66 6f 72 20 72 65 61 64 2f 77 72  file for read/wr
bec70 69 74 65 20 61 63 63 65 73 73 2e 20 54 72 79 20  ite access. Try 
bec80 72 65 61 64 2d 6f 6e 6c 79 2e 20 2a 2f 0a 20 20  read-only. */.  
bec90 20 20 66 6c 61 67 73 20 26 3d 20 7e 28 53 51 4c    flags &= ~(SQL
beca0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
becb0 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43  TE|SQLITE_OPEN_C
becc0 52 45 41 54 45 29 3b 0a 20 20 20 20 66 6c 61 67  REATE);.    flag
becd0 73 20 7c 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e  s |= SQLITE_OPEN
bece0 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 72  _READONLY;.    r
becf0 65 74 75 72 6e 20 75 6e 69 78 4f 70 65 6e 28 70  eturn unixOpen(p
bed00 56 66 73 2c 20 7a 50 61 74 68 2c 20 70 46 69 6c  Vfs, zPath, pFil
bed10 65 2c 20 66 6c 61 67 73 2c 20 70 4f 75 74 46 6c  e, flags, pOutFl
bed20 61 67 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ags);.  }.  if( 
bed30 66 64 3c 30 20 29 7b 0a 20 20 20 20 72 65 74 75  fd<0 ){.    retu
bed40 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50  rn SQLITE_CANTOP
bed50 45 4e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73  EN;.  }.  if( is
bed60 44 65 6c 65 74 65 20 29 7b 0a 20 20 20 20 75 6e  Delete ){.    un
bed70 6c 69 6e 6b 28 7a 50 61 74 68 29 3b 0a 20 20 7d  link(zPath);.  }
bed80 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61 67 73  .  if( pOutFlags
bed90 20 29 7b 0a 20 20 20 20 2a 70 4f 75 74 46 6c 61   ){.    *pOutFla
beda0 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 7d 0a  gs = flags;.  }.
bedb0 0a 20 20 61 73 73 65 72 74 28 66 64 21 3d 30 29  .  assert(fd!=0)
bedc0 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 44 69  ;.  if( isOpenDi
bedd0 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 69  rectory ){.    i
bede0 6e 74 20 72 63 20 3d 20 6f 70 65 6e 44 69 72 65  nt rc = openDire
bedf0 63 74 6f 72 79 28 7a 50 61 74 68 2c 20 26 64 69  ctory(zPath, &di
bee00 72 66 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63  rfd);.    if( rc
bee10 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
bee20 20 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 0a       close(fd);.
bee30 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
bee40 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
bee50 75 72 6e 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69  urn fillInUnixFi
bee60 6c 65 28 66 64 2c 20 64 69 72 66 64 2c 20 70 46  le(fd, dirfd, pF
bee70 69 6c 65 2c 20 7a 50 61 74 68 29 3b 0a 7d 0a 0a  ile, zPath);.}..
bee80 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65  /*.** Delete the
bee90 20 66 69 6c 65 20 61 74 20 7a 50 61 74 68 2e 20   file at zPath. 
beea0 49 66 20 74 68 65 20 64 69 72 53 79 6e 63 20 61  If the dirSync a
beeb0 72 67 75 6d 65 6e 74 20 69 73 20 74 72 75 65 2c  rgument is true,
beec0 20 66 73 79 6e 63 28 29 0a 2a 2a 20 74 68 65 20   fsync().** the 
beed0 64 69 72 65 63 74 6f 72 79 20 61 66 74 65 72 20  directory after 
beee0 64 65 6c 65 74 69 6e 67 20 74 68 65 20 66 69 6c  deleting the fil
beef0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
bef00 20 75 6e 69 78 44 65 6c 65 74 65 28 73 71 6c 69   unixDelete(sqli
bef10 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63  te3_vfs *pVfs, c
bef20 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68  onst char *zPath
bef30 2c 20 69 6e 74 20 64 69 72 53 79 6e 63 29 7b 0a  , int dirSync){.
bef40 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
bef50 45 5f 4f 4b 3b 0a 20 20 53 69 6d 75 6c 61 74 65  E_OK;.  Simulate
bef60 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53  IOError(return S
bef70 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45  QLITE_IOERR_DELE
bef80 54 45 29 3b 0a 20 20 75 6e 6c 69 6e 6b 28 7a 50  TE);.  unlink(zP
bef90 61 74 68 29 3b 0a 20 20 69 66 28 20 64 69 72 53  ath);.  if( dirS
befa0 79 6e 63 20 29 7b 0a 20 20 20 20 69 6e 74 20 66  ync ){.    int f
befb0 64 3b 0a 20 20 20 20 72 63 20 3d 20 6f 70 65 6e  d;.    rc = open
befc0 44 69 72 65 63 74 6f 72 79 28 7a 50 61 74 68 2c  Directory(zPath,
befd0 20 26 66 64 29 3b 0a 20 20 20 20 69 66 28 20 72   &fd);.    if( r
befe0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
beff0 20 20 20 20 20 20 69 66 28 20 66 73 79 6e 63 28        if( fsync(
bf000 66 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72  fd) ){.        r
bf010 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  c = SQLITE_IOERR
bf020 5f 44 49 52 5f 46 53 59 4e 43 3b 0a 20 20 20 20  _DIR_FSYNC;.    
bf030 20 20 7d 0a 20 20 20 20 20 20 63 6c 6f 73 65 28    }.      close(
bf040 66 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  fd);.    }.  }. 
bf050 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
bf060 2a 0a 2a 2a 20 54 65 73 74 20 74 68 65 20 65 78  *.** Test the ex
bf070 69 73 74 61 6e 63 65 20 6f 66 20 6f 72 20 61 63  istance of or ac
bf080 63 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 73  cess permissions
bf090 20 6f 66 20 66 69 6c 65 20 7a 50 61 74 68 2e 20   of file zPath. 
bf0a0 54 68 65 0a 2a 2a 20 74 65 73 74 20 70 65 72 66  The.** test perf
bf0b0 6f 72 6d 65 64 20 64 65 70 65 6e 64 73 20 6f 6e  ormed depends on
bf0c0 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 66 6c   the value of fl
bf0d0 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53  ags:.**.**     S
bf0e0 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49  QLITE_ACCESS_EXI
bf0f0 53 54 53 3a 20 52 65 74 75 72 6e 20 31 20 69 66  STS: Return 1 if
bf100 20 74 68 65 20 66 69 6c 65 20 65 78 69 73 74 73   the file exists
bf110 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 41  .**     SQLITE_A
bf120 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a  CCESS_READWRITE:
bf130 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65   Return 1 if the
bf140 20 66 69 6c 65 20 69 73 20 72 65 61 64 20 61 6e   file is read an
bf150 64 20 77 72 69 74 61 62 6c 65 2e 0a 2a 2a 20 20  d writable..**  
bf160 20 20 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53     SQLITE_ACCESS
bf170 5f 52 45 41 44 4f 4e 4c 59 3a 20 52 65 74 75 72  _READONLY: Retur
bf180 6e 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 20  n 1 if the file 
bf190 69 73 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a  is readable..**.
bf1a0 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 72 65 74  ** Otherwise ret
bf1b0 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63  urn 0..*/.static
bf1c0 20 69 6e 74 20 75 6e 69 78 41 63 63 65 73 73 28   int unixAccess(
bf1d0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
bf1e0 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  s, const char *z
bf1f0 50 61 74 68 2c 20 69 6e 74 20 66 6c 61 67 73 29  Path, int flags)
bf200 7b 0a 20 20 69 6e 74 20 61 6d 6f 64 65 20 3d 20  {.  int amode = 
bf210 30 3b 0a 20 20 73 77 69 74 63 68 28 20 66 6c 61  0;.  switch( fla
bf220 67 73 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53  gs ){.    case S
bf230 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49  QLITE_ACCESS_EXI
bf240 53 54 53 3a 0a 20 20 20 20 20 20 61 6d 6f 64 65  STS:.      amode
bf250 20 3d 20 46 5f 4f 4b 3b 0a 20 20 20 20 20 20 62   = F_OK;.      b
bf260 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
bf270 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41  QLITE_ACCESS_REA
bf280 44 57 52 49 54 45 3a 0a 20 20 20 20 20 20 61 6d  DWRITE:.      am
bf290 6f 64 65 20 3d 20 57 5f 4f 4b 7c 52 5f 4f 4b 3b  ode = W_OK|R_OK;
bf2a0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
bf2b0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43    case SQLITE_AC
bf2c0 43 45 53 53 5f 52 45 41 44 3a 0a 20 20 20 20 20  CESS_READ:.     
bf2d0 20 61 6d 6f 64 65 20 3d 20 52 5f 4f 4b 3b 0a 20   amode = R_OK;. 
bf2e0 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20       break;..   
bf2f0 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20   default:.      
bf300 61 73 73 65 72 74 28 21 22 49 6e 76 61 6c 69 64  assert(!"Invalid
bf310 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 22   flags argument"
bf320 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
bf330 28 61 63 63 65 73 73 28 7a 50 61 74 68 2c 20 61  (access(zPath, a
bf340 6d 6f 64 65 29 3d 3d 30 29 3b 0a 7d 0a 0a 2f 2a  mode)==0);.}../*
bf350 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 74 65 6d  .** Create a tem
bf360 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65  porary file name
bf370 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75 66 20   in zBuf.  zBuf 
bf380 6d 75 73 74 20 62 65 20 61 6c 6c 6f 63 61 74 65  must be allocate
bf390 64 0a 2a 2a 20 62 79 20 74 68 65 20 63 61 6c 6c  d.** by the call
bf3a0 69 6e 67 20 70 72 6f 63 65 73 73 20 61 6e 64 20  ing process and 
bf3b0 6d 75 73 74 20 62 65 20 62 69 67 20 65 6e 6f 75  must be big enou
bf3c0 67 68 20 74 6f 20 68 6f 6c 64 20 61 74 20 6c 65  gh to hold at le
bf3d0 61 73 74 0a 2a 2a 20 70 56 66 73 2d 3e 6d 78 50  ast.** pVfs->mxP
bf3e0 61 74 68 6e 61 6d 65 20 62 79 74 65 73 2e 0a 2a  athname bytes..*
bf3f0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
bf400 78 47 65 74 54 65 6d 70 6e 61 6d 65 28 73 71 6c  xGetTempname(sql
bf410 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
bf420 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a  int nBuf, char *
bf430 7a 42 75 66 29 7b 0a 20 20 73 74 61 74 69 63 20  zBuf){.  static 
bf440 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 7a 44 69  const char *azDi
bf450 72 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 30 2c  rs[] = {.     0,
bf460 0a 20 20 20 20 20 22 2f 76 61 72 2f 74 6d 70 22  .     "/var/tmp"
bf470 2c 0a 20 20 20 20 20 22 2f 75 73 72 2f 74 6d 70  ,.     "/usr/tmp
bf480 22 2c 0a 20 20 20 20 20 22 2f 74 6d 70 22 2c 0a  ",.     "/tmp",.
bf490 20 20 20 20 20 22 2e 22 2c 0a 20 20 7d 3b 0a 20       ".",.  };. 
bf4a0 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
bf4b0 73 69 67 6e 65 64 20 63 68 61 72 20 7a 43 68 61  signed char zCha
bf4c0 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63 64  rs[] =.    "abcd
bf4d0 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74  efghijklmnopqrst
bf4e0 75 76 77 78 79 7a 22 0a 20 20 20 20 22 41 42 43  uvwxyz".    "ABC
bf4f0 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53  DEFGHIJKLMNOPQRS
bf500 54 55 56 57 58 59 5a 22 0a 20 20 20 20 22 30 31  TUVWXYZ".    "01
bf510 32 33 34 35 36 37 38 39 22 3b 0a 20 20 69 6e 74  23456789";.  int
bf520 20 69 2c 20 6a 3b 0a 20 20 73 74 72 75 63 74 20   i, j;.  struct 
bf530 73 74 61 74 20 62 75 66 3b 0a 20 20 63 6f 6e 73  stat buf;.  cons
bf540 74 20 63 68 61 72 20 2a 7a 44 69 72 20 3d 20 22  t char *zDir = "
bf550 2e 22 3b 0a 0a 20 20 2f 2a 20 49 74 27 73 20 6f  .";..  /* It's o
bf560 64 64 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61  dd to simulate a
bf570 6e 20 69 6f 2d 65 72 72 6f 72 20 68 65 72 65 2c  n io-error here,
bf580 20 62 75 74 20 72 65 61 6c 6c 79 20 74 68 69 73   but really this
bf590 20 69 73 20 6a 75 73 74 0a 20 20 2a 2a 20 75 73   is just.  ** us
bf5a0 69 6e 67 20 74 68 65 20 69 6f 2d 65 72 72 6f 72  ing the io-error
bf5b0 20 69 6e 66 72 61 73 74 72 75 63 74 75 72 65 20   infrastructure 
bf5c0 74 6f 20 74 65 73 74 20 74 68 61 74 20 53 51 4c  to test that SQL
bf5d0 69 74 65 20 68 61 6e 64 6c 65 73 20 74 68 69 73  ite handles this
bf5e0 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 66  .  ** function f
bf5f0 61 69 6c 69 6e 67 2e 20 0a 20 20 2a 2f 0a 20 20  ailing. .  */.  
bf600 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
bf610 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
bf620 52 52 4f 52 20 29 3b 0a 0a 20 20 61 7a 44 69 72  RROR );..  azDir
bf630 73 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 5f 74  s[0] = sqlite3_t
bf640 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a 20  emp_directory;. 
bf650 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65   for(i=0; i<size
bf660 6f 66 28 61 7a 44 69 72 73 29 2f 73 69 7a 65 6f  of(azDirs)/sizeo
bf670 66 28 61 7a 44 69 72 73 5b 30 5d 29 3b 20 69 2b  f(azDirs[0]); i+
bf680 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 7a 44 69  +){.    if( azDi
bf690 72 73 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69  rs[i]==0 ) conti
bf6a0 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 73 74 61  nue;.    if( sta
bf6b0 74 28 61 7a 44 69 72 73 5b 69 5d 2c 20 26 62 75  t(azDirs[i], &bu
bf6c0 66 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  f) ) continue;. 
bf6d0 20 20 20 69 66 28 20 21 53 5f 49 53 44 49 52 28     if( !S_ISDIR(
bf6e0 62 75 66 2e 73 74 5f 6d 6f 64 65 29 20 29 20 63  buf.st_mode) ) c
bf6f0 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28  ontinue;.    if(
bf700 20 61 63 63 65 73 73 28 61 7a 44 69 72 73 5b 69   access(azDirs[i
bf710 5d 2c 20 30 37 29 20 29 20 63 6f 6e 74 69 6e 75  ], 07) ) continu
bf720 65 3b 0a 20 20 20 20 7a 44 69 72 20 3d 20 61 7a  e;.    zDir = az
bf730 44 69 72 73 5b 69 5d 3b 0a 20 20 20 20 62 72 65  Dirs[i];.    bre
bf740 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68  ak;.  }..  /* Ch
bf750 65 63 6b 20 74 68 61 74 20 74 68 65 20 6f 75 74  eck that the out
bf760 70 75 74 20 62 75 66 66 65 72 20 69 73 20 6c 61  put buffer is la
bf770 72 67 65 20 65 6e 6f 75 67 68 20 66 6f 72 20 74  rge enough for t
bf780 68 65 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c  he temporary fil
bf790 65 20 0a 20 20 2a 2a 20 6e 61 6d 65 2e 20 49 66  e .  ** name. If
bf7a0 20 69 74 20 69 73 20 6e 6f 74 2c 20 72 65 74 75   it is not, retu
bf7b0 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e  rn SQLITE_ERROR.
bf7c0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 73 74 72  .  */.  if( (str
bf7d0 6c 65 6e 28 7a 44 69 72 29 20 2b 20 73 74 72 6c  len(zDir) + strl
bf7e0 65 6e 28 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46  en(SQLITE_TEMP_F
bf7f0 49 4c 45 5f 50 52 45 46 49 58 29 20 2b 20 31 37  ILE_PREFIX) + 17
bf800 29 20 3e 3d 20 6e 42 75 66 20 29 7b 0a 20 20 20  ) >= nBuf ){.   
bf810 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
bf820 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 64 6f 7b  RROR;.  }..  do{
bf830 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 56 66  .    assert( pVf
bf840 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 3d 3d 4d  s->mxPathname==M
bf850 41 58 5f 50 41 54 48 4e 41 4d 45 20 29 3b 0a 20  AX_PATHNAME );. 
bf860 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
bf870 6e 74 66 28 6e 42 75 66 2d 31 37 2c 20 7a 42 75  ntf(nBuf-17, zBu
bf880 66 2c 20 22 25 73 2f 22 53 51 4c 49 54 45 5f 54  f, "%s/"SQLITE_T
bf890 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c  EMP_FILE_PREFIX,
bf8a0 20 7a 44 69 72 29 3b 0a 20 20 20 20 6a 20 3d 20   zDir);.    j = 
bf8b0 73 74 72 6c 65 6e 28 7a 42 75 66 29 3b 0a 20 20  strlen(zBuf);.  
bf8c0 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d    sqlite3_random
bf8d0 6e 65 73 73 28 31 35 2c 20 26 7a 42 75 66 5b 6a  ness(15, &zBuf[j
bf8e0 5d 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  ]);.    for(i=0;
bf8f0 20 69 3c 31 35 3b 20 69 2b 2b 2c 20 6a 2b 2b 29   i<15; i++, j++)
bf900 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 6a 5d 20  {.      zBuf[j] 
bf910 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73 5b 20  = (char)zChars[ 
bf920 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29  ((unsigned char)
bf930 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66  zBuf[j])%(sizeof
bf940 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20  (zChars)-1) ];. 
bf950 20 20 20 7d 0a 20 20 20 20 7a 42 75 66 5b 6a 5d     }.    zBuf[j]
bf960 20 3d 20 30 3b 0a 20 20 7d 77 68 69 6c 65 28 20   = 0;.  }while( 
bf970 61 63 63 65 73 73 28 7a 42 75 66 2c 30 29 3d 3d  access(zBuf,0)==
bf980 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  0 );.  return SQ
bf990 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a  LITE_OK;.}.../*.
bf9a0 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c 61 74 69  ** Turn a relati
bf9b0 76 65 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f  ve pathname into
bf9c0 20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65   a full pathname
bf9d0 2e 20 54 68 65 20 72 65 6c 61 74 69 76 65 20 70  . The relative p
bf9e0 61 74 68 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64  ath.** is stored
bf9f0 20 61 73 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e   as a nul-termin
bfa00 61 74 65 64 20 73 74 72 69 6e 67 20 69 6e 20 74  ated string in t
bfa10 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65  he buffer pointe
bfa20 64 20 74 6f 20 62 79 0a 2a 2a 20 7a 50 61 74 68  d to by.** zPath
bfa30 2e 20 0a 2a 2a 0a 2a 2a 20 7a 4f 75 74 20 70 6f  . .**.** zOut po
bfa40 69 6e 74 73 20 74 6f 20 61 20 62 75 66 66 65 72  ints to a buffer
bfa50 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73 71 6c   of at least sql
bfa60 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e  ite3_vfs.mxPathn
bfa70 61 6d 65 20 62 79 74 65 73 20 0a 2a 2a 20 28 69  ame bytes .** (i
bfa80 6e 20 74 68 69 73 20 63 61 73 65 2c 20 4d 41 58  n this case, MAX
bfa90 5f 50 41 54 48 4e 41 4d 45 20 62 79 74 65 73 29  _PATHNAME bytes)
bfaa0 2e 20 54 68 65 20 66 75 6c 6c 2d 70 61 74 68 20  . The full-path 
bfab0 69 73 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a  is written to.**
bfac0 20 74 68 69 73 20 62 75 66 66 65 72 20 62 65 66   this buffer bef
bfad0 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
bfae0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
bfaf0 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20  xFullPathname(. 
bfb00 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
bfb10 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  fs,            /
bfb20 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73  * Pointer to vfs
bfb30 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e   object */.  con
bfb40 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20  st char *zPath, 
bfb50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
bfb60 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20  ssibly relative 
bfb70 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20  input path */.  
bfb80 69 6e 74 20 6e 4f 75 74 2c 20 20 20 20 20 20 20  int nOut,       
bfb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
bfba0 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 75 74 20   Size of output 
bfbb0 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 20  buffer in bytes 
bfbc0 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4f 75 74 20  */.  char *zOut 
bfbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bfbe0 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66     /* Output buf
bfbf0 66 65 72 20 2a 2f 0a 29 7b 0a 0a 20 20 2f 2a 20  fer */.){..  /* 
bfc00 49 74 27 73 20 6f 64 64 20 74 6f 20 73 69 6d 75  It's odd to simu
bfc10 6c 61 74 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72  late an io-error
bfc20 20 68 65 72 65 2c 20 62 75 74 20 72 65 61 6c 6c   here, but reall
bfc30 79 20 74 68 69 73 20 69 73 20 6a 75 73 74 0a 20  y this is just. 
bfc40 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 69 6f   ** using the io
bfc50 2d 65 72 72 6f 72 20 69 6e 66 72 61 73 74 72 75  -error infrastru
bfc60 63 74 75 72 65 20 74 6f 20 74 65 73 74 20 74 68  cture to test th
bfc70 61 74 20 53 51 4c 69 74 65 20 68 61 6e 64 6c 65  at SQLite handle
bfc80 73 20 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63  s this.  ** func
bfc90 74 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20 54 68  tion failing. Th
bfca0 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 75 6c  is function coul
bfcb0 64 20 66 61 69 6c 20 69 66 2c 20 66 6f 72 20 65  d fail if, for e
bfcc0 78 61 6d 70 6c 65 2c 20 74 68 65 0a 20 20 2a 2a  xample, the.  **
bfcd0 20 63 75 72 72 65 6e 74 20 77 6f 72 6b 69 6e 67   current working
bfce0 20 64 69 72 65 63 74 6c 79 20 68 61 73 20 62 65   directly has be
bfcf0 65 6e 20 75 6e 6c 69 6e 6b 65 64 2e 0a 20 20 2a  en unlinked..  *
bfd00 2f 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  /.  SimulateIOEr
bfd10 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49  ror( return SQLI
bfd20 54 45 5f 45 52 52 4f 52 20 29 3b 0a 0a 20 20 61  TE_ERROR );..  a
bfd30 73 73 65 72 74 28 20 70 56 66 73 2d 3e 6d 78 50  ssert( pVfs->mxP
bfd40 61 74 68 6e 61 6d 65 3d 3d 4d 41 58 5f 50 41 54  athname==MAX_PAT
bfd50 48 4e 41 4d 45 20 29 3b 0a 20 20 7a 4f 75 74 5b  HNAME );.  zOut[
bfd60 6e 4f 75 74 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a  nOut-1] = '\0';.
bfd70 20 20 69 66 28 20 7a 50 61 74 68 5b 30 5d 3d 3d    if( zPath[0]==
bfd80 27 2f 27 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  '/' ){.    sqlit
bfd90 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 4f 75 74  e3_snprintf(nOut
bfda0 2c 20 7a 4f 75 74 2c 20 22 25 73 22 2c 20 7a 50  , zOut, "%s", zP
bfdb0 61 74 68 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ath);.  }else{. 
bfdc0 20 20 20 69 6e 74 20 6e 43 77 64 3b 0a 20 20 20     int nCwd;.   
bfdd0 20 69 66 28 20 67 65 74 63 77 64 28 7a 4f 75 74   if( getcwd(zOut
bfde0 2c 20 6e 4f 75 74 2d 31 29 3d 3d 30 20 29 7b 0a  , nOut-1)==0 ){.
bfdf0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
bfe00 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20  ITE_CANTOPEN;.  
bfe10 20 20 7d 0a 20 20 20 20 6e 43 77 64 20 3d 20 73    }.    nCwd = s
bfe20 74 72 6c 65 6e 28 7a 4f 75 74 29 3b 0a 20 20 20  trlen(zOut);.   
bfe30 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
bfe40 66 28 6e 4f 75 74 2d 6e 43 77 64 2c 20 26 7a 4f  f(nOut-nCwd, &zO
bfe50 75 74 5b 6e 43 77 64 5d 2c 20 22 2f 25 73 22 2c  ut[nCwd], "/%s",
bfe60 20 7a 50 61 74 68 29 3b 0a 20 20 7d 0a 20 20 72   zPath);.  }.  r
bfe70 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
bfe80 0a 0a 23 69 66 20 30 0a 20 20 2f 2a 0a 20 20 2a  ..#if 0.  /*.  *
bfe90 2a 20 52 65 6d 6f 76 65 20 22 2f 2e 2f 22 20 70  * Remove "/./" p
bfea0 61 74 68 20 65 6c 65 6d 65 6e 74 73 20 61 6e 64  ath elements and
bfeb0 20 63 6f 6e 76 65 72 74 20 22 2f 41 2f 2e 2f 22   convert "/A/./"
bfec0 20 70 61 74 68 20 65 6c 65 6d 65 6e 74 73 0a 20   path elements. 
bfed0 20 2a 2a 20 74 6f 20 6a 75 73 74 20 22 2f 22 2e   ** to just "/".
bfee0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 46 75 6c  .  */.  if( zFul
bfef0 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20  l ){.    int i, 
bff00 6a 3b 0a 20 20 20 20 66 6f 72 28 69 3d 6a 3d 30  j;.    for(i=j=0
bff10 3b 20 7a 46 75 6c 6c 5b 69 5d 3b 20 69 2b 2b 29  ; zFull[i]; i++)
bff20 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 46 75 6c  {.      if( zFul
bff30 6c 5b 69 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20  l[i]=='/' ){.   
bff40 20 20 20 20 20 69 66 28 20 7a 46 75 6c 6c 5b 69       if( zFull[i
bff50 2b 31 5d 3d 3d 27 2f 27 20 29 20 63 6f 6e 74 69  +1]=='/' ) conti
bff60 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28  nue;.        if(
bff70 20 7a 46 75 6c 6c 5b 69 2b 31 5d 3d 3d 27 2e 27   zFull[i+1]=='.'
bff80 20 26 26 20 7a 46 75 6c 6c 5b 69 2b 32 5d 3d 3d   && zFull[i+2]==
bff90 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20  '/' ){.         
bffa0 20 69 20 2b 3d 20 31 3b 0a 20 20 20 20 20 20 20   i += 1;.       
bffb0 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
bffc0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
bffd0 66 28 20 7a 46 75 6c 6c 5b 69 2b 31 5d 3d 3d 27  f( zFull[i+1]=='
bffe0 2e 27 20 26 26 20 7a 46 75 6c 6c 5b 69 2b 32 5d  .' && zFull[i+2]
bfff0 3d 3d 27 2e 27 20 26 26 20 7a 46 75 6c 6c 5b 69  =='.' && zFull[i
c0000 2b 33 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20  +3]=='/' ){.    
c0010 20 20 20 20 20 20 77 68 69 6c 65 28 20 6a 3e 30        while( j>0
c0020 20 26 26 20 7a 46 75 6c 6c 5b 6a 2d 31 5d 21 3d   && zFull[j-1]!=
c0030 27 2f 27 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20  '/' ){ j--; }.  
c0040 20 20 20 20 20 20 20 20 69 20 2b 3d 20 33 3b 0a          i += 3;.
c0050 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e            contin
c0060 75 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ue;.        }.  
c0070 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 46 75 6c      }.      zFul
c0080 6c 5b 6a 2b 2b 5d 20 3d 20 7a 46 75 6c 6c 5b 69  l[j++] = zFull[i
c0090 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 46 75  ];.    }.    zFu
c00a0 6c 6c 5b 6a 5d 20 3d 20 30 3b 0a 20 20 7d 0a 23  ll[j] = 0;.  }.#
c00b0 65 6e 64 69 66 0a 7d 0a 0a 0a 23 69 66 6e 64 65  endif.}...#ifnde
c00c0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
c00d0 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a  AD_EXTENSION./*.
c00e0 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f  ** Interfaces fo
c00f0 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72  r opening a shar
c0100 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64  ed library, find
c0110 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73  ing entry points
c0120 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73  .** within the s
c0130 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61  hared library, a
c0140 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73  nd closing the s
c0150 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a  hared library..*
c0160 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 64 6c 66 63  /.#include <dlfc
c0170 6e 2e 68 3e 0a 73 74 61 74 69 63 20 76 6f 69 64  n.h>.static void
c0180 20 2a 75 6e 69 78 44 6c 4f 70 65 6e 28 73 71 6c   *unixDlOpen(sql
c0190 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
c01a0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
c01b0 65 6e 61 6d 65 29 7b 0a 20 20 72 65 74 75 72 6e  ename){.  return
c01c0 20 64 6c 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d   dlopen(zFilenam
c01d0 65 2c 20 52 54 4c 44 5f 4e 4f 57 20 7c 20 52 54  e, RTLD_NOW | RT
c01e0 4c 44 5f 47 4c 4f 42 41 4c 29 3b 0a 7d 0a 0a 2f  LD_GLOBAL);.}../
c01f0 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 61 6c 6c  *.** SQLite call
c0200 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  s this function 
c0210 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65  immediately afte
c0220 72 20 61 20 63 61 6c 6c 20 74 6f 20 75 6e 69 78  r a call to unix
c0230 44 6c 53 79 6d 28 29 20 6f 72 0a 2a 2a 20 75 6e  DlSym() or.** un
c0240 69 78 44 6c 4f 70 65 6e 28 29 20 66 61 69 6c 73  ixDlOpen() fails
c0250 20 28 72 65 74 75 72 6e 73 20 61 20 6e 75 6c 6c   (returns a null
c0260 20 70 6f 69 6e 74 65 72 29 2e 20 49 66 20 61 20   pointer). If a 
c0270 6d 6f 72 65 20 64 65 74 61 69 6c 65 64 20 65 72  more detailed er
c0280 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 69  ror.** message i
c0290 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 69 74 20  s available, it 
c02a0 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 7a 42  is written to zB
c02b0 75 66 4f 75 74 2e 20 49 66 20 6e 6f 20 65 72 72  ufOut. If no err
c02c0 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73  or message.** is
c02d0 20 61 76 61 69 6c 61 62 6c 65 2c 20 7a 42 75 66   available, zBuf
c02e0 4f 75 74 20 69 73 20 6c 65 66 74 20 75 6e 6d 6f  Out is left unmo
c02f0 64 69 66 69 65 64 20 61 6e 64 20 53 51 4c 69 74  dified and SQLit
c0300 65 20 75 73 65 73 20 61 20 64 65 66 61 75 6c 74  e uses a default
c0310 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67  .** error messag
c0320 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
c0330 64 20 75 6e 69 78 44 6c 45 72 72 6f 72 28 73 71  d unixDlError(sq
c0340 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
c0350 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20   int nBuf, char 
c0360 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 63 68 61  *zBufOut){.  cha
c0370 72 20 2a 7a 45 72 72 3b 0a 20 20 65 6e 74 65 72  r *zErr;.  enter
c0380 4d 75 74 65 78 28 29 3b 0a 20 20 7a 45 72 72 20  Mutex();.  zErr 
c0390 3d 20 64 6c 65 72 72 6f 72 28 29 3b 0a 20 20 69  = dlerror();.  i
c03a0 66 28 20 7a 45 72 72 20 29 7b 0a 20 20 20 20 73  f( zErr ){.    s
c03b0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
c03c0 6e 42 75 66 2c 20 7a 42 75 66 4f 75 74 2c 20 22  nBuf, zBufOut, "
c03d0 25 73 22 2c 20 7a 45 72 72 29 3b 0a 20 20 7d 0a  %s", zErr);.  }.
c03e0 20 20 6c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a    leaveMutex();.
c03f0 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 75  }.static void *u
c0400 6e 69 78 44 6c 53 79 6d 28 73 71 6c 69 74 65 33  nixDlSym(sqlite3
c0410 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64  _vfs *pVfs, void
c0420 20 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74   *pHandle, const
c0430 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 7b   char *zSymbol){
c0440 0a 20 20 72 65 74 75 72 6e 20 64 6c 73 79 6d 28  .  return dlsym(
c0450 70 48 61 6e 64 6c 65 2c 20 7a 53 79 6d 62 6f 6c  pHandle, zSymbol
c0460 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  );.}.static void
c0470 20 75 6e 69 78 44 6c 43 6c 6f 73 65 28 73 71 6c   unixDlClose(sql
c0480 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
c0490 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a  void *pHandle){.
c04a0 20 20 64 6c 63 6c 6f 73 65 28 70 48 61 6e 64 6c    dlclose(pHandl
c04b0 65 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69  e);.}.#else /* i
c04c0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
c04d0 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 69 73 20  AD_EXTENSION is 
c04e0 64 65 66 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64  defined: */.  #d
c04f0 65 66 69 6e 65 20 75 6e 69 78 44 6c 4f 70 65 6e  efine unixDlOpen
c0500 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e    0.  #define un
c0510 69 78 44 6c 45 72 72 6f 72 20 30 0a 20 20 23 64  ixDlError 0.  #d
c0520 65 66 69 6e 65 20 75 6e 69 78 44 6c 53 79 6d 20  efine unixDlSym 
c0530 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e    0.  #define un
c0540 69 78 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64  ixDlClose 0.#end
c0550 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  if../*.** Write 
c0560 6e 42 75 66 20 62 79 74 65 73 20 6f 66 20 72 61  nBuf bytes of ra
c0570 6e 64 6f 6d 20 64 61 74 61 20 74 6f 20 74 68 65  ndom data to the
c0580 20 73 75 70 70 6c 69 65 64 20 62 75 66 66 65 72   supplied buffer
c0590 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 63   zBuf..*/.static
c05a0 20 69 6e 74 20 75 6e 69 78 52 61 6e 64 6f 6d 6e   int unixRandomn
c05b0 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ess(sqlite3_vfs 
c05c0 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c  *pVfs, int nBuf,
c05d0 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 0a 20   char *zBuf){.. 
c05e0 20 61 73 73 65 72 74 28 6e 42 75 66 3e 3d 28 73   assert(nBuf>=(s
c05f0 69 7a 65 6f 66 28 74 69 6d 65 5f 74 29 2b 73 69  izeof(time_t)+si
c0600 7a 65 6f 66 28 69 6e 74 29 29 29 3b 0a 0a 20 20  zeof(int)));..  
c0610 2f 2a 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e  /* We have to in
c0620 69 74 69 61 6c 69 7a 65 20 7a 42 75 66 20 74 6f  itialize zBuf to
c0630 20 70 72 65 76 65 6e 74 20 76 61 6c 67 72 69 6e   prevent valgrin
c0640 64 20 66 72 6f 6d 20 72 65 70 6f 72 74 69 6e 67  d from reporting
c0650 0a 20 20 2a 2a 20 65 72 72 6f 72 73 2e 20 20 54  .  ** errors.  T
c0660 68 65 20 72 65 70 6f 72 74 73 20 69 73 73 75 65  he reports issue
c0670 64 20 62 79 20 76 61 6c 67 72 69 6e 64 20 61 72  d by valgrind ar
c0680 65 20 69 6e 63 6f 72 72 65 63 74 20 2d 20 77 65  e incorrect - we
c0690 20 77 6f 75 6c 64 0a 20 20 2a 2a 20 70 72 65 66   would.  ** pref
c06a0 65 72 20 74 68 61 74 20 74 68 65 20 72 61 6e 64  er that the rand
c06b0 6f 6d 6e 65 73 73 20 62 65 20 69 6e 63 72 65 61  omness be increa
c06c0 73 65 64 20 62 79 20 6d 61 6b 69 6e 67 20 75 73  sed by making us
c06d0 65 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 75 6e  e of the.  ** un
c06e0 69 6e 69 74 69 61 6c 69 7a 65 64 20 73 70 61 63  initialized spac
c06f0 65 20 69 6e 20 7a 42 75 66 20 2d 20 62 75 74 20  e in zBuf - but 
c0700 76 61 6c 67 72 69 6e 64 20 65 72 72 6f 72 73 20  valgrind errors 
c0710 74 65 6e 64 20 74 6f 20 77 6f 72 72 79 0a 20 20  tend to worry.  
c0720 2a 2a 20 73 6f 6d 65 20 75 73 65 72 73 2e 20 20  ** some users.  
c0730 52 61 74 68 65 72 20 74 68 61 6e 20 61 72 67 75  Rather than argu
c0740 65 2c 20 69 74 20 73 65 65 6d 73 20 65 61 73 69  e, it seems easi
c0750 65 72 20 6a 75 73 74 20 74 6f 20 69 6e 69 74 69  er just to initi
c0760 61 6c 69 7a 65 0a 20 20 2a 2a 20 74 68 65 20 77  alize.  ** the w
c0770 68 6f 6c 65 20 61 72 72 61 79 20 61 6e 64 20 73  hole array and s
c0780 69 6c 65 6e 63 65 20 76 61 6c 67 72 69 6e 64 2c  ilence valgrind,
c0790 20 65 76 65 6e 20 69 66 20 74 68 61 74 20 6d 65   even if that me
c07a0 61 6e 73 20 6c 65 73 73 20 72 61 6e 64 6f 6d 6e  ans less randomn
c07b0 65 73 73 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20  ess.  ** in the 
c07c0 72 61 6e 64 6f 6d 20 73 65 65 64 2e 0a 20 20 2a  random seed..  *
c07d0 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 74 65 73 74  *.  ** When test
c07e0 69 6e 67 2c 20 69 6e 69 74 69 61 6c 69 7a 69 6e  ing, initializin
c07f0 67 20 7a 42 75 66 5b 5d 20 74 6f 20 7a 65 72 6f  g zBuf[] to zero
c0800 20 69 73 20 61 6c 6c 20 77 65 20 64 6f 2e 20 20   is all we do.  
c0810 54 68 61 74 20 6d 65 61 6e 73 0a 20 20 2a 2a 20  That means.  ** 
c0820 74 68 61 74 20 77 65 20 61 6c 77 61 79 73 20 75  that we always u
c0830 73 65 20 74 68 65 20 73 61 6d 65 20 72 61 6e 64  se the same rand
c0840 6f 6d 20 6e 75 6d 62 65 72 20 73 65 71 75 65 6e  om number sequen
c0850 63 65 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20  ce.  This makes 
c0860 74 68 65 0a 20 20 2a 2a 20 74 65 73 74 73 20 72  the.  ** tests r
c0870 65 70 65 61 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a  epeatable..  */.
c0880 20 20 6d 65 6d 73 65 74 28 7a 42 75 66 2c 20 30    memset(zBuf, 0
c0890 2c 20 6e 42 75 66 29 3b 0a 23 69 66 20 21 64 65  , nBuf);.#if !de
c08a0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53  fined(SQLITE_TES
c08b0 54 29 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 70  T).  {.    int p
c08c0 69 64 2c 20 66 64 3b 0a 20 20 20 20 66 64 20 3d  id, fd;.    fd =
c08d0 20 6f 70 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e   open("/dev/uran
c08e0 64 6f 6d 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b  dom", O_RDONLY);
c08f0 0a 20 20 20 20 69 66 28 20 66 64 3c 30 20 29 7b  .    if( fd<0 ){
c0900 0a 20 20 20 20 20 20 74 69 6d 65 5f 74 20 74 3b  .      time_t t;
c0910 0a 20 20 20 20 20 20 74 69 6d 65 28 26 74 29 3b  .      time(&t);
c0920 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 42  .      memcpy(zB
c0930 75 66 2c 20 26 74 2c 20 73 69 7a 65 6f 66 28 74  uf, &t, sizeof(t
c0940 29 29 3b 0a 20 20 20 20 20 20 70 69 64 20 3d 20  ));.      pid = 
c0950 67 65 74 70 69 64 28 29 3b 0a 20 20 20 20 20 20  getpid();.      
c0960 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 73 69 7a  memcpy(&zBuf[siz
c0970 65 6f 66 28 74 29 5d 2c 20 26 70 69 64 2c 20 73  eof(t)], &pid, s
c0980 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20 20 20  izeof(pid));.   
c0990 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65   }else{.      re
c09a0 61 64 28 66 64 2c 20 7a 42 75 66 2c 20 6e 42 75  ad(fd, zBuf, nBu
c09b0 66 29 3b 0a 20 20 20 20 20 20 63 6c 6f 73 65 28  f);.      close(
c09c0 66 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  fd);.    }.  }.#
c09d0 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 53  endif.  return S
c09e0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a  QLITE_OK;.}.../*
c09f0 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 61 20  .** Sleep for a 
c0a00 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 20 52  little while.  R
c0a10 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74  eturn the amount
c0a20 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a   of time slept..
c0a30 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20  ** The argument 
c0a40 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
c0a50 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 77 65   microseconds we
c0a60 20 77 61 6e 74 20 74 6f 20 73 6c 65 65 70 2e 0a   want to sleep..
c0a70 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  ** The return va
c0a80 6c 75 65 20 69 73 20 74 68 65 20 6e 75 6d 62 65  lue is the numbe
c0a90 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64  r of microsecond
c0aa0 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74 75 61  s of sleep actua
c0ab0 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 65 64  lly.** requested
c0ac0 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 72 6c   from the underl
c0ad0 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73  ying operating s
c0ae0 79 73 74 65 6d 2c 20 61 20 6e 75 6d 62 65 72 20  ystem, a number 
c0af0 77 68 69 63 68 0a 2a 2a 20 6d 69 67 68 74 20 62  which.** might b
c0b00 65 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f  e greater than o
c0b10 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 61  r equal to the a
c0b20 72 67 75 6d 65 6e 74 2c 20 62 75 74 20 6e 6f 74  rgument, but not
c0b30 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68   less.** than th
c0b40 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73  e argument..*/.s
c0b50 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 53 6c  tatic int unixSl
c0b60 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66 73 20  eep(sqlite3_vfs 
c0b70 2a 70 56 66 73 2c 20 69 6e 74 20 6d 69 63 72 6f  *pVfs, int micro
c0b80 73 65 63 6f 6e 64 73 29 7b 0a 23 69 66 20 64 65  seconds){.#if de
c0b90 66 69 6e 65 64 28 48 41 56 45 5f 55 53 4c 45 45  fined(HAVE_USLEE
c0ba0 50 29 20 26 26 20 48 41 56 45 5f 55 53 4c 45 45  P) && HAVE_USLEE
c0bb0 50 0a 20 20 75 73 6c 65 65 70 28 6d 69 63 72 6f  P.  usleep(micro
c0bc0 73 65 63 6f 6e 64 73 29 3b 0a 20 20 72 65 74 75  seconds);.  retu
c0bd0 72 6e 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 3b  rn microseconds;
c0be0 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 73 65 63  .#else.  int sec
c0bf0 6f 6e 64 73 20 3d 20 28 6d 69 63 72 6f 73 65 63  onds = (microsec
c0c00 6f 6e 64 73 2b 39 39 39 39 39 39 29 2f 31 30 30  onds+999999)/100
c0c10 30 30 30 30 3b 0a 20 20 73 6c 65 65 70 28 73 65  0000;.  sleep(se
c0c20 63 6f 6e 64 73 29 3b 0a 20 20 72 65 74 75 72 6e  conds);.  return
c0c30 20 73 65 63 6f 6e 64 73 2a 31 30 30 30 30 30 30   seconds*1000000
c0c40 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  ;.#endif.}../*.*
c0c50 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
c0c60 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73 65 74  variable, if set
c0c70 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76   to a non-zero v
c0c80 61 6c 75 65 2c 20 62 65 63 6f 6d 65 73 20 74 68  alue, becomes th
c0c90 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 65 74 75  e result.** retu
c0ca0 72 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  rned from sqlite
c0cb0 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 29  3OsCurrentTime()
c0cc0 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20  .  This is used 
c0cd0 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a  for testing..*/.
c0ce0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
c0cf0 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
c0d00 74 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e  t sqlite3_curren
c0d10 74 5f 74 69 6d 65 20 3d 20 30 3b 0a 23 65 6e 64  t_time = 0;.#end
c0d20 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74  if../*.** Find t
c0d30 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20  he current time 
c0d40 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 43 6f  (in Universal Co
c0d50 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65 29 2e  ordinated Time).
c0d60 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 63    Write the.** c
c0d70 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e 64 20  urrent time and 
c0d80 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69 61 6e  date as a Julian
c0d90 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e 74 6f   Day number into
c0da0 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72   *prNow and.** r
c0db0 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 6e  eturn 0.  Return
c0dc0 20 31 20 69 66 20 74 68 65 20 74 69 6d 65 20 61   1 if the time a
c0dd0 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 20 62  nd date cannot b
c0de0 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74  e found..*/.stat
c0df0 69 63 20 69 6e 74 20 75 6e 69 78 43 75 72 72 65  ic int unixCurre
c0e00 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76  ntTime(sqlite3_v
c0e10 66 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65  fs *pVfs, double
c0e20 20 2a 70 72 4e 6f 77 29 7b 0a 23 69 66 64 65 66   *prNow){.#ifdef
c0e30 20 4e 4f 5f 47 45 54 54 4f 44 0a 20 20 74 69 6d   NO_GETTOD.  tim
c0e40 65 5f 74 20 74 3b 0a 20 20 74 69 6d 65 28 26 74  e_t t;.  time(&t
c0e50 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 74 2f  );.  *prNow = t/
c0e60 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35 38  86400.0 + 244058
c0e70 37 2e 35 3b 0a 23 65 6c 73 65 0a 20 20 73 74 72  7.5;.#else.  str
c0e80 75 63 74 20 74 69 6d 65 76 61 6c 20 73 4e 6f 77  uct timeval sNow
c0e90 3b 0a 20 20 67 65 74 74 69 6d 65 6f 66 64 61 79  ;.  gettimeofday
c0ea0 28 26 73 4e 6f 77 2c 20 30 29 3b 0a 20 20 2a 70  (&sNow, 0);.  *p
c0eb0 72 4e 6f 77 20 3d 20 32 34 34 30 35 38 37 2e 35  rNow = 2440587.5
c0ec0 20 2b 20 73 4e 6f 77 2e 74 76 5f 73 65 63 2f 38   + sNow.tv_sec/8
c0ed0 36 34 30 30 2e 30 20 2b 20 73 4e 6f 77 2e 74 76  6400.0 + sNow.tv
c0ee0 5f 75 73 65 63 2f 38 36 34 30 30 30 30 30 30 30  _usec/8640000000
c0ef0 30 2e 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64  0.0;.#endif.#ifd
c0f00 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20  ef SQLITE_TEST. 
c0f10 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75 72   if( sqlite3_cur
c0f20 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20 20  rent_time ){.   
c0f30 20 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69 74 65   *prNow = sqlite
c0f40 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 2f 38  3_current_time/8
c0f50 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35 38 37  6400.0 + 2440587
c0f60 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  .5;.  }.#endif. 
c0f70 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
c0f80 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69  .** Return a poi
c0f90 6e 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69  nter to the sqli
c0fa0 74 65 33 44 65 66 61 75 6c 74 56 66 73 20 73 74  te3DefaultVfs st
c0fb0 72 75 63 74 75 72 65 2e 20 20 20 57 65 20 75 73  ructure.   We us
c0fc0 65 0a 2a 2a 20 61 20 66 75 6e 63 74 69 6f 6e 20  e.** a function 
c0fd0 72 61 74 68 65 72 20 74 68 61 6e 20 67 69 76 65  rather than give
c0fe0 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 67   the structure g
c0ff0 6c 6f 62 61 6c 20 73 63 6f 70 65 20 62 65 63 61  lobal scope beca
c1000 75 73 65 0a 2a 2a 20 73 6f 6d 65 20 63 6f 6d 70  use.** some comp
c1010 69 6c 65 72 73 20 28 4d 53 56 43 29 20 64 6f 20  ilers (MSVC) do 
c1020 6e 6f 74 20 61 6c 6c 6f 77 20 66 6f 72 77 61 72  not allow forwar
c1030 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f  d declarations o
c1040 66 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64  f.** initialized
c1050 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a   structures..*/.
c1060 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73  SQLITE_PRIVATE s
c1070 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69  qlite3_vfs *sqli
c1080 74 65 33 4f 73 44 65 66 61 75 6c 74 56 66 73 28  te3OsDefaultVfs(
c1090 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20  void){.  static 
c10a0 73 71 6c 69 74 65 33 5f 76 66 73 20 75 6e 69 78  sqlite3_vfs unix
c10b0 56 66 73 20 3d 20 7b 0a 20 20 20 20 31 2c 20 20  Vfs = {.    1,  
c10c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c10d0 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20  /* iVersion */. 
c10e0 20 20 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69     sizeof(unixFi
c10f0 6c 65 29 2c 20 20 20 2f 2a 20 73 7a 4f 73 46 69  le),   /* szOsFi
c1100 6c 65 20 2a 2f 0a 20 20 20 20 4d 41 58 5f 50 41  le */.    MAX_PA
c1110 54 48 4e 41 4d 45 2c 20 20 20 20 20 20 20 2f 2a  THNAME,       /*
c1120 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20   mxPathname */. 
c1130 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
c1140 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 20         /* pNext 
c1150 2a 2f 0a 20 20 20 20 22 75 6e 69 78 22 2c 20 20  */.    "unix",  
c1160 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e             /* zN
c1170 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20  ame */.    0,   
c1180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
c1190 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 20 20  * pAppData */.  
c11a0 0a 20 20 20 20 75 6e 69 78 4f 70 65 6e 2c 20 20  .    unixOpen,  
c11b0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65           /* xOpe
c11c0 6e 20 2a 2f 0a 20 20 20 20 75 6e 69 78 44 65 6c  n */.    unixDel
c11d0 65 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ete,         /* 
c11e0 78 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20 75  xDelete */.    u
c11f0 6e 69 78 41 63 63 65 73 73 2c 20 20 20 20 20 20  nixAccess,      
c1200 20 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f     /* xAccess */
c1210 0a 20 20 20 20 75 6e 69 78 47 65 74 54 65 6d 70  .    unixGetTemp
c1220 6e 61 6d 65 2c 20 20 20 20 2f 2a 20 78 47 65 74  name,    /* xGet
c1230 54 65 6d 70 4e 61 6d 65 20 2a 2f 0a 20 20 20 20  TempName */.    
c1240 75 6e 69 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65  unixFullPathname
c1250 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68  ,   /* xFullPath
c1260 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 75 6e 69 78  name */.    unix
c1270 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20  DlOpen,         
c1280 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 20  /* xDlOpen */.  
c1290 20 20 75 6e 69 78 44 6c 45 72 72 6f 72 2c 20 20    unixDlError,  
c12a0 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f        /* xDlErro
c12b0 72 20 2a 2f 0a 20 20 20 20 75 6e 69 78 44 6c 53  r */.    unixDlS
c12c0 79 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ym,          /* 
c12d0 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 75 6e  xDlSym */.    un
c12e0 69 78 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20  ixDlClose,      
c12f0 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f    /* xDlClose */
c1300 0a 20 20 20 20 75 6e 69 78 52 61 6e 64 6f 6d 6e  .    unixRandomn
c1310 65 73 73 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e  ess,     /* xRan
c1320 64 6f 6d 6e 65 73 73 20 2a 2f 0a 20 20 20 20 75  domness */.    u
c1330 6e 69 78 53 6c 65 65 70 2c 20 20 20 20 20 20 20  nixSleep,       
c1340 20 20 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 0a     /* xSleep */.
c1350 20 20 20 20 75 6e 69 78 43 75 72 72 65 6e 74 54      unixCurrentT
c1360 69 6d 65 20 20 20 20 20 2f 2a 20 78 43 75 72 72  ime     /* xCurr
c1370 65 6e 74 54 69 6d 65 20 2a 2f 0a 20 20 7d 3b 0a  entTime */.  };.
c1380 20 20 0a 20 20 72 65 74 75 72 6e 20 26 75 6e 69    .  return &uni
c1390 78 56 66 73 3b 0a 7d 0a 20 0a 23 65 6e 64 69 66  xVfs;.}. .#endif
c13a0 20 2f 2a 20 4f 53 5f 55 4e 49 58 20 2a 2f 0a 0a   /* OS_UNIX */..
c13b0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
c13c0 45 6e 64 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63  End of os_unix.c
c13d0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
c13e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c13f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
c1400 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
c1410 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 77 69  Begin file os_wi
c1420 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
c1430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
c1450 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32  /*.** 2004 May 2
c1460 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  2.**.** The auth
c1470 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
c1480 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
c1490 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
c14a0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
c14b0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
c14c0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
c14d0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
c14e0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
c14f0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
c1500 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
c1510 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
c1520 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
c1530 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
c1540 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
c1550 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
c1560 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
c1570 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
c1580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c15a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c15b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c15c0 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
c15d0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
c15e0 6f 64 65 20 74 68 61 74 20 69 73 20 73 70 65 63  ode that is spec
c15f0 69 66 69 63 20 74 6f 20 77 69 6e 64 6f 77 73 2e  ific to windows.
c1600 0a 2a 2f 0a 23 69 66 20 4f 53 5f 57 49 4e 20 20  .*/.#if OS_WIN  
c1610 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c1620 54 68 69 73 20 66 69 6c 65 20 69 73 20 75 73 65  This file is use
c1630 64 20 66 6f 72 20 77 69 6e 64 6f 77 73 20 6f 6e  d for windows on
c1640 6c 79 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20  ly */.../*.** A 
c1650 4e 6f 74 65 20 41 62 6f 75 74 20 4d 65 6d 6f 72  Note About Memor
c1660 79 20 41 6c 6c 6f 63 61 74 69 6f 6e 3a 0a 2a 2a  y Allocation:.**
c1670 0a 2a 2a 20 54 68 69 73 20 64 72 69 76 65 72 20  .** This driver 
c1680 75 73 65 73 20 6d 61 6c 6c 6f 63 28 29 2f 66 72  uses malloc()/fr
c1690 65 65 28 29 20 64 69 72 65 63 74 6c 79 20 72 61  ee() directly ra
c16a0 74 68 65 72 20 74 68 61 6e 20 67 6f 69 6e 67 20  ther than going 
c16b0 74 68 72 6f 75 67 68 0a 2a 2a 20 74 68 65 20 53  through.** the S
c16c0 51 4c 69 74 65 2d 77 72 61 70 70 65 72 73 20 73  QLite-wrappers s
c16d0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2f  qlite3_malloc()/
c16e0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 2e 20  sqlite3_free(). 
c16f0 20 54 68 6f 73 65 20 77 72 61 70 70 65 72 73 0a   Those wrappers.
c1700 2a 2a 20 61 72 65 20 64 65 73 69 67 6e 65 64 20  ** are designed 
c1710 66 6f 72 20 75 73 65 20 6f 6e 20 65 6d 62 65 64  for use on embed
c1720 64 65 64 20 73 79 73 74 65 6d 73 20 77 68 65 72  ded systems wher
c1730 65 20 6d 65 6d 6f 72 79 20 69 73 20 73 63 61 72  e memory is scar
c1740 63 65 20 61 6e 64 0a 2a 2a 20 6d 61 6c 6c 6f 63  ce and.** malloc
c1750 20 66 61 69 6c 75 72 65 73 20 68 61 70 70 65 6e   failures happen
c1760 20 66 72 65 71 75 65 6e 74 6c 79 2e 20 20 57 69   frequently.  Wi
c1770 6e 33 32 20 64 6f 65 73 20 6e 6f 74 20 74 79 70  n32 does not typ
c1780 69 63 61 6c 6c 79 20 72 75 6e 20 6f 6e 0a 2a 2a  ically run on.**
c1790 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d   embedded system
c17a0 73 2c 20 61 6e 64 20 77 68 65 6e 20 69 74 20 64  s, and when it d
c17b0 6f 65 73 20 74 68 65 20 64 65 76 65 6c 6f 70 65  oes the develope
c17c0 72 73 20 6e 6f 72 6d 61 6c 6c 79 20 68 61 76 65  rs normally have
c17d0 20 62 69 67 67 65 72 0a 2a 2a 20 70 72 6f 62 6c   bigger.** probl
c17e0 65 6d 73 20 74 6f 20 77 6f 72 72 79 20 61 62 6f  ems to worry abo
c17f0 75 74 20 74 68 61 6e 20 72 75 6e 6e 69 6e 67 20  ut than running 
c1800 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 20  out of memory.  
c1810 53 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 74 0a  So there is not.
c1820 2a 2a 20 61 20 63 6f 6d 70 65 6c 6c 69 6e 67 20  ** a compelling 
c1830 6e 65 65 64 20 74 6f 20 75 73 65 20 74 68 65 20  need to use the 
c1840 77 72 61 70 70 65 72 73 2e 0a 2a 2a 0a 2a 2a 20  wrappers..**.** 
c1850 42 75 74 20 74 68 65 72 65 20 69 73 20 61 20 67  But there is a g
c1860 6f 6f 64 20 72 65 61 73 6f 6e 20 74 6f 20 6e 6f  ood reason to no
c1870 74 20 75 73 65 20 74 68 65 20 77 72 61 70 70 65  t use the wrappe
c1880 72 73 2e 20 20 49 66 20 77 65 20 75 73 65 20 74  rs.  If we use t
c1890 68 65 0a 2a 2a 20 77 72 61 70 70 65 72 73 20 74  he.** wrappers t
c18a0 68 65 6e 20 77 65 20 77 69 6c 6c 20 67 65 74 20  hen we will get 
c18b0 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63  simulated malloc
c18c0 28 29 20 66 61 69 6c 75 72 65 73 20 77 69 74 68  () failures with
c18d0 69 6e 20 74 68 69 73 0a 2a 2a 20 64 72 69 76 65  in this.** drive
c18e0 72 2e 20 20 41 6e 64 20 74 68 61 74 20 63 61 75  r.  And that cau
c18f0 73 65 73 20 61 6c 6c 20 6b 69 6e 64 73 20 6f 66  ses all kinds of
c1900 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 6f 75   problems for ou
c1910 72 20 74 65 73 74 73 2e 20 20 57 65 0a 2a 2a 20  r tests.  We.** 
c1920 63 6f 75 6c 64 20 65 6e 68 61 6e 63 65 20 53 51  could enhance SQ
c1930 4c 69 74 65 20 74 6f 20 64 65 61 6c 20 77 69 74  Lite to deal wit
c1940 68 20 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c  h simulated mall
c1950 6f 63 20 66 61 69 6c 75 72 65 73 20 77 69 74 68  oc failures with
c1960 69 6e 0a 2a 2a 20 74 68 65 20 4f 53 20 64 72 69  in.** the OS dri
c1970 76 65 72 2c 20 62 75 74 20 74 68 65 20 63 6f 64  ver, but the cod
c1980 65 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 74  e to deal with t
c1990 68 6f 73 65 20 66 61 69 6c 75 72 65 20 77 6f 75  hose failure wou
c19a0 6c 64 20 6e 6f 74 0a 2a 2a 20 62 65 20 65 78 65  ld not.** be exe
c19b0 72 63 69 73 65 64 20 6f 6e 20 4c 69 6e 75 78 20  rcised on Linux 
c19c0 28 77 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20  (which does not 
c19d0 6e 65 65 64 20 74 6f 20 6d 61 6c 6c 6f 63 28 29  need to malloc()
c19e0 20 69 6e 20 74 68 65 20 64 72 69 76 65 72 29 0a   in the driver).
c19f0 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20 77 6f 75  ** and so we wou
c1a00 6c 64 20 68 61 76 65 20 64 69 66 66 69 63 75 6c  ld have difficul
c1a10 74 79 20 77 72 69 74 69 6e 67 20 63 6f 76 65 72  ty writing cover
c1a20 61 67 65 20 74 65 73 74 73 20 66 6f 72 20 74 68  age tests for th
c1a30 61 74 0a 2a 2a 20 63 6f 64 65 2e 20 20 42 65 74  at.** code.  Bet
c1a40 74 65 72 20 74 6f 20 6c 65 61 76 65 20 74 68 65  ter to leave the
c1a50 20 63 6f 64 65 20 6f 75 74 2c 20 77 65 20 74 68   code out, we th
c1a60 69 6e 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  ink..**.** The p
c1a70 6f 69 6e 74 20 6f 66 20 74 68 69 73 20 64 69 73  oint of this dis
c1a80 63 75 73 73 69 6f 6e 20 69 73 20 61 73 20 66 6f  cussion is as fo
c1a90 6c 6c 6f 77 73 3a 20 20 57 68 65 6e 20 63 72 65  llows:  When cre
c1aa0 61 74 69 6e 67 20 61 20 6e 65 77 0a 2a 2a 20 4f  ating a new.** O
c1ab0 53 20 6c 61 79 65 72 20 66 6f 72 20 61 6e 20 65  S layer for an e
c1ac0 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d 2c 20  mbedded system, 
c1ad0 69 66 20 79 6f 75 20 75 73 65 20 74 68 69 73 20  if you use this 
c1ae0 66 69 6c 65 20 61 73 20 61 6e 20 65 78 61 6d 70  file as an examp
c1af0 6c 65 2c 0a 2a 2a 20 61 76 6f 69 64 20 74 68 65  le,.** avoid the
c1b00 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29   use of malloc()
c1b10 2f 66 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20  /free().  Those 
c1b20 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6b  routines work ok
c1b30 20 6f 6e 20 77 69 6e 64 6f 77 73 0a 2a 2a 20 64   on windows.** d
c1b40 65 73 6b 74 6f 70 73 20 62 75 74 20 6e 6f 74 20  esktops but not 
c1b50 73 6f 20 77 65 6c 6c 20 69 6e 20 65 6d 62 65 64  so well in embed
c1b60 64 65 64 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a  ded systems..*/.
c1b70 0a 23 69 6e 63 6c 75 64 65 20 3c 77 69 6e 62 61  .#include <winba
c1b80 73 65 2e 68 3e 0a 0a 23 69 66 64 65 66 20 5f 5f  se.h>..#ifdef __
c1b90 43 59 47 57 49 4e 5f 5f 0a 23 20 69 6e 63 6c 75  CYGWIN__.# inclu
c1ba0 64 65 20 3c 73 79 73 2f 63 79 67 77 69 6e 2e 68  de <sys/cygwin.h
c1bb0 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  >.#endif../*.** 
c1bc0 4d 61 63 72 6f 73 20 75 73 65 64 20 74 6f 20 64  Macros used to d
c1bd0 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72  etermine whether
c1be0 20 6f 72 20 6e 6f 74 20 74 6f 20 75 73 65 20 74   or not to use t
c1bf0 68 72 65 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64  hreads..*/.#if d
c1c00 65 66 69 6e 65 64 28 54 48 52 45 41 44 53 41 46  efined(THREADSAF
c1c10 45 29 20 26 26 20 54 48 52 45 41 44 53 41 46 45  E) && THREADSAFE
c1c20 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
c1c30 5f 57 33 32 5f 54 48 52 45 41 44 53 20 31 0a 23  _W32_THREADS 1.#
c1c40 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63  endif../*.** Inc
c1c50 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 20 69  lude code that i
c1c60 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20  s common to all 
c1c70 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a  os_*.c files.*/.
c1c80 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
c1c90 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f  Include os_commo
c1ca0 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  n.h in the middl
c1cb0 65 20 6f 66 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a  e of os_win.c **
c1cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
c1cd0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
c1ce0 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f  Begin file os_co
c1cf0 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mmon.h *********
c1d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
c1d20 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32  /*.** 2004 May 2
c1d30 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  2.**.** The auth
c1d40 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
c1d50 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
c1d60 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
c1d70 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
c1d80 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
c1d90 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
c1da0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
c1db0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
c1dc0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
c1dd0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
c1de0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
c1df0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
c1e00 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
c1e10 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
c1e20 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
c1e30 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
c1e40 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
c1e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1e90 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
c1ea0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d   file contains m
c1eb0 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 74 74  acros and a litt
c1ec0 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 20 74  le bit of code t
c1ed0 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f  hat is common to
c1ee0 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 70  .** all of the p
c1ef0 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 69 63  latform-specific
c1f00 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20   files (os_*.c) 
c1f10 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 65 64  and is #included
c1f20 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66   into those.** f
c1f30 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  iles..**.** This
c1f40 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20   file should be 
c1f50 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 68 65  #included by the
c1f60 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e   os_*.c files on
c1f70 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 61  ly.  It is not a
c1f80 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 70  .** general purp
c1f90 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e  ose header file.
c1fa0 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65  .*/../*.** At le
c1fb0 61 73 74 20 74 77 6f 20 62 75 67 73 20 68 61 76  ast two bugs hav
c1fc0 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63  e slipped in bec
c1fd0 61 75 73 65 20 77 65 20 63 68 61 6e 67 65 64 20  ause we changed 
c1fe0 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47  the MEMORY_DEBUG
c1ff0 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c  .** macro to SQL
c2000 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20 73 6f  ITE_DEBUG and so
c2010 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c  me older makefil
c2020 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20  es have not yet 
c2030 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 74  made the.** swit
c2040 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ch.  The followi
c2050 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 63  ng code should c
c2060 61 74 63 68 20 74 68 69 73 20 70 72 6f 62 6c 65  atch this proble
c2070 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  m at compile-tim
c2080 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d  e..*/.#ifdef MEM
c2090 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72 72 6f  ORY_DEBUG.# erro
c20a0 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45  r "The MEMORY_DE
c20b0 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 73  BUG macro is obs
c20c0 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51 4c 49  olete.  Use SQLI
c20d0 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65 61 64  TE_DEBUG instead
c20e0 2e 22 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 20  .".#endif.../*. 
c20f0 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20  * When testing, 
c2100 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 69  this global vari
c2110 61 62 6c 65 20 73 74 6f 72 65 73 20 74 68 65 20  able stores the 
c2120 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a  location of the.
c2130 20 2a 20 70 65 6e 64 69 6e 67 2d 62 79 74 65 20   * pending-byte 
c2140 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
c2150 66 69 6c 65 2e 0a 20 2a 2f 0a 23 69 66 64 65 66  file.. */.#ifdef
c2160 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
c2170 49 54 45 5f 41 50 49 20 75 6e 73 69 67 6e 65 64  ITE_API unsigned
c2180 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 65 6e   int sqlite3_pen
c2190 64 69 6e 67 5f 62 79 74 65 20 3d 20 30 78 34 30  ding_byte = 0x40
c21a0 30 30 30 30 30 30 3b 0a 23 65 6e 64 69 66 0a 0a  000000;.#endif..
c21b0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
c21c0 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  BUG.SQLITE_PRIVA
c21d0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 53  TE int sqlite3OS
c21e0 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 65 66 69  Trace = 0;.#defi
c21f0 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 20 20  ne OSTRACE1(X)  
c2200 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
c2210 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69  e3OSTrace ) sqli
c2220 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
c2230 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
c2240 45 32 28 58 2c 59 29 20 20 20 20 20 20 20 69 66  E2(X,Y)       if
c2250 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  ( sqlite3OSTrace
c2260 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50   ) sqlite3DebugP
c2270 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 66 69  rintf(X,Y).#defi
c2280 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c  ne OSTRACE3(X,Y,
c2290 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  Z)     if( sqlit
c22a0 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69  e3OSTrace ) sqli
c22b0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
c22c0 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,Y,Z).#define OS
c22d0 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20  TRACE4(X,Y,Z,A) 
c22e0 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54    if( sqlite3OST
c22f0 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65  race ) sqlite3De
c2300 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c  bugPrintf(X,Y,Z,
c2310 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  A).#define OSTRA
c2320 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69  CE5(X,Y,Z,A,B) i
c2330 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63  f( sqlite3OSTrac
c2340 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  e ) sqlite3Debug
c2350 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42  Printf(X,Y,Z,A,B
c2360 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
c2370 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20  E6(X,Y,Z,A,B,C) 
c2380 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33  \.    if(sqlite3
c2390 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 33  OSTrace) sqlite3
c23a0 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
c23b0 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65  Z,A,B,C).#define
c23c0 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c   OSTRACE7(X,Y,Z,
c23d0 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69  A,B,C,D) \.    i
c23e0 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  f(sqlite3OSTrace
c23f0 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
c2400 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  intf(X,Y,Z,A,B,C
c2410 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e  ,D).#else.#defin
c2420 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a 23 64  e OSTRACE1(X).#d
c2430 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58  efine OSTRACE2(X
c2440 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,Y).#define OSTR
c2450 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66  ACE3(X,Y,Z).#def
c2460 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59  ine OSTRACE4(X,Y
c2470 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,Z,A).#define OS
c2480 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42  TRACE5(X,Y,Z,A,B
c2490 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
c24a0 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a  E6(X,Y,Z,A,B,C).
c24b0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37  #define OSTRACE7
c24c0 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a  (X,Y,Z,A,B,C,D).
c24d0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61  #endif../*.** Ma
c24e0 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f 72 6d  cros for perform
c24f0 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20 20 4e  ance tracing.  N
c2500 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f  ormally turned o
c2510 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a  ff.  Only works.
c2520 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72 64 77  ** on i486 hardw
c2530 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  are..*/.#ifdef S
c2540 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43  QLITE_PERFORMANC
c2550 45 5f 54 52 41 43 45 0a 5f 5f 69 6e 6c 69 6e 65  E_TRACE.__inline
c2560 5f 5f 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67  __ unsigned long
c2570 20 6c 6f 6e 67 20 69 6e 74 20 68 77 74 69 6d 65   long int hwtime
c2580 28 76 6f 69 64 29 7b 0a 20 20 75 6e 73 69 67 6e  (void){.  unsign
c2590 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74  ed long long int
c25a0 20 78 3b 0a 20 20 5f 5f 61 73 6d 5f 5f 28 22 72   x;.  __asm__("r
c25b0 64 74 73 63 5c 6e 5c 74 22 0a 20 20 20 20 20 20  dtsc\n\t".      
c25c0 20 20 20 20 22 6d 6f 76 20 25 25 65 64 78 2c 20      "mov %%edx, 
c25d0 25 25 65 63 78 5c 6e 5c 74 22 0a 20 20 20 20 20  %%ecx\n\t".     
c25e0 20 20 20 20 20 3a 22 3d 41 22 20 28 78 29 29 3b       :"=A" (x));
c25f0 0a 20 20 72 65 74 75 72 6e 20 78 3b 0a 7d 0a 73  .  return x;.}.s
c2600 74 61 74 69 63 20 75 6e 73 69 67 6e 65 64 20 6c  tatic unsigned l
c2610 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 20 67 5f 73  ong long int g_s
c2620 74 61 72 74 3b 0a 73 74 61 74 69 63 20 75 6e 73  tart;.static uns
c2630 69 67 6e 65 64 20 69 6e 74 20 65 6c 61 70 73 65  igned int elapse
c2640 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f  ;.#define TIMER_
c2650 53 54 41 52 54 20 20 20 20 20 20 20 67 5f 73 74  START       g_st
c2660 61 72 74 3d 68 77 74 69 6d 65 28 29 0a 23 64 65  art=hwtime().#de
c2670 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 20  fine TIMER_END  
c2680 20 20 20 20 20 20 20 65 6c 61 70 73 65 3d 68 77         elapse=hw
c2690 74 69 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a 23  time()-g_start.#
c26a0 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41  define TIMER_ELA
c26b0 50 53 45 44 20 20 20 20 20 65 6c 61 70 73 65 0a  PSED     elapse.
c26c0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 54 49  #else.#define TI
c26d0 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66 69 6e  MER_START.#defin
c26e0 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66  e TIMER_END.#def
c26f0 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45  ine TIMER_ELAPSE
c2700 44 20 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a  D     0.#endif..
c2710 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70  /*.** If we comp
c2720 69 6c 65 20 77 69 74 68 20 74 68 65 20 53 51 4c  ile with the SQL
c2730 49 54 45 5f 54 45 53 54 20 6d 61 63 72 6f 20 73  ITE_TEST macro s
c2740 65 74 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 6c  et, then the fol
c2750 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20  lowing block.** 
c2760 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 67 69 76  of code will giv
c2770 65 20 75 73 20 74 68 65 20 61 62 69 6c 69 74 79  e us the ability
c2780 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 64   to simulate a d
c2790 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20 20  isk I/O error.  
c27a0 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 20  This.** is used 
c27b0 66 6f 72 20 74 65 73 74 69 6e 67 20 74 68 65 20  for testing the 
c27c0 49 2f 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f 67  I/O recovery log
c27d0 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ic..*/.#ifdef SQ
c27e0 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
c27f0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
c2800 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20  _io_error_hit = 
c2810 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  0;            /*
c2820 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   Total number of
c2830 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0a 53   I/O Errors */.S
c2840 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
c2850 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68  lite3_io_error_h
c2860 61 72 64 68 69 74 20 3d 20 30 3b 20 20 20 20 20  ardhit = 0;     
c2870 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
c2880 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f 72  non-benign error
c2890 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  s */.SQLITE_API 
c28a0 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  int sqlite3_io_e
c28b0 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 30  rror_pending = 0
c28c0 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e  ;        /* Coun
c28d0 74 20 64 6f 77 6e 20 74 6f 20 66 69 72 73 74 20  t down to first 
c28e0 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 51 4c  I/O error */.SQL
c28f0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
c2900 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72  te3_io_error_per
c2910 73 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20  sist = 0;       
c2920 20 2f 2a 20 54 72 75 65 20 69 66 20 49 2f 4f 20   /* True if I/O 
c2930 65 72 72 6f 72 73 20 70 65 72 73 69 73 74 20 2a  errors persist *
c2940 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
c2950 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
c2960 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20 20  r_benign = 0;   
c2970 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
c2980 20 65 72 72 6f 72 73 20 61 72 65 20 62 65 6e 69   errors are beni
c2990 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  gn */.SQLITE_API
c29a0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73   int sqlite3_dis
c29b0 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20  kfull_pending = 
c29c0 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  0;.SQLITE_API in
c29d0 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75  t sqlite3_diskfu
c29e0 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20  ll = 0;.#define 
c29f0 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42  SimulateIOErrorB
c2a00 65 6e 69 67 6e 28 58 29 20 73 71 6c 69 74 65 33  enign(X) sqlite3
c2a10 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e  _io_error_benign
c2a20 3d 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d  =(X).#define Sim
c2a30 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 43 4f 44  ulateIOError(COD
c2a40 45 29 20 20 5c 0a 20 20 69 66 28 20 28 73 71 6c  E)  \.  if( (sql
c2a50 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65  ite3_io_error_pe
c2a60 72 73 69 73 74 20 26 26 20 73 71 6c 69 74 65 33  rsist && sqlite3
c2a70 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20 5c  _io_error_hit) \
c2a80 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74  .       || sqlit
c2a90 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64  e3_io_error_pend
c2aa0 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 20 5c 0a  ing-- == 1 )  \.
c2ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20                { 
c2ac0 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 43  local_ioerr(); C
c2ad0 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f  ODE; }.static vo
c2ae0 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29  id local_ioerr()
c2af0 7b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 49 4f  {.  IOTRACE(("IO
c2b00 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73 71 6c 69  ERR\n"));.  sqli
c2b10 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74  te3_io_error_hit
c2b20 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74  ++;.  if( !sqlit
c2b30 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69  e3_io_error_beni
c2b40 67 6e 20 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f  gn ) sqlite3_io_
c2b50 65 72 72 6f 72 5f 68 61 72 64 68 69 74 2b 2b 3b  error_hardhit++;
c2b60 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c  .}.#define Simul
c2b70 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72  ateDiskfullError
c2b80 28 43 4f 44 45 29 20 5c 0a 20 20 20 69 66 28 20  (CODE) \.   if( 
c2b90 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c  sqlite3_diskfull
c2ba0 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 20  _pending ){ \.  
c2bb0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64     if( sqlite3_d
c2bc0 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20  iskfull_pending 
c2bd0 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20 20 20 20  == 1 ){ \.      
c2be0 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20   local_ioerr(); 
c2bf0 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  \.       sqlite3
c2c00 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c  _diskfull = 1; \
c2c10 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  .       sqlite3_
c2c20 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 31  io_error_hit = 1
c2c30 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f 44 45 3b  ; \.       CODE;
c2c40 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20 5c   \.     }else{ \
c2c50 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  .       sqlite3_
c2c60 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67  diskfull_pending
c2c70 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a 20  --; \.     } \. 
c2c80 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e    }.#else.#defin
c2c90 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f  e SimulateIOErro
c2ca0 72 42 65 6e 69 67 6e 28 58 29 0a 23 64 65 66 69  rBenign(X).#defi
c2cb0 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  ne SimulateIOErr
c2cc0 6f 72 28 41 29 0a 23 64 65 66 69 6e 65 20 53 69  or(A).#define Si
c2cd0 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72  mulateDiskfullEr
c2ce0 72 6f 72 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f  ror(A).#endif../
c2cf0 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e  *.** When testin
c2d00 67 2c 20 6b 65 65 70 20 61 20 63 6f 75 6e 74 20  g, keep a count 
c2d10 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  of the number of
c2d20 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a   open files..*/.
c2d30 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
c2d40 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
c2d50 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66  t sqlite3_open_f
c2d60 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23  ile_count = 0;.#
c2d70 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74  define OpenCount
c2d80 65 72 28 58 29 20 20 73 71 6c 69 74 65 33 5f 6f  er(X)  sqlite3_o
c2d90 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d  pen_file_count+=
c2da0 28 58 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e  (X).#else.#defin
c2db0 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29  e OpenCounter(X)
c2dc0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
c2dd0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
c2de0 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a  os_common.h ****
c2df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2e10 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
c2e20 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
c2e30 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
c2e40 74 20 6f 66 66 20 69 6e 20 6f 73 5f 77 69 6e 2e  t off in os_win.
c2e50 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
c2e60 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
c2e70 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65 20  Determine if we 
c2e80 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68  are dealing with
c2e90 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 69   WindowsCE - whi
c2ea0 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a 2a 2a  ch has a much.**
c2eb0 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a 2a 2f   reduced API..*/
c2ec0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 57 49  .#if defined(_WI
c2ed0 4e 33 32 5f 57 43 45 29 0a 23 20 64 65 66 69 6e  N32_WCE).# defin
c2ee0 65 20 4f 53 5f 57 49 4e 43 45 20 31 0a 23 20 64  e OS_WINCE 1.# d
c2ef0 65 66 69 6e 65 20 41 72 65 46 69 6c 65 41 70 69  efine AreFileApi
c2f00 73 41 4e 53 49 28 29 20 31 0a 23 65 6c 73 65 0a  sANSI() 1.#else.
c2f10 23 20 64 65 66 69 6e 65 20 4f 53 5f 57 49 4e 43  # define OS_WINC
c2f20 45 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  E 0.#endif../*.*
c2f30 2a 20 57 69 6e 43 45 20 6c 61 63 6b 73 20 6e 61  * WinCE lacks na
c2f40 74 69 76 65 20 73 75 70 70 6f 72 74 20 66 6f 72  tive support for
c2f50 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 6f   file locking so
c2f60 20 77 65 20 68 61 76 65 20 74 6f 20 66 61 6b 65   we have to fake
c2f70 20 69 74 0a 2a 2a 20 77 69 74 68 20 73 6f 6d 65   it.** with some
c2f80 20 63 6f 64 65 20 6f 66 20 6f 75 72 20 6f 77 6e   code of our own
c2f90 2e 0a 2a 2f 0a 23 69 66 20 4f 53 5f 57 49 4e 43  ..*/.#if OS_WINC
c2fa0 45 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  E.typedef struct
c2fb0 20 77 69 6e 63 65 4c 6f 63 6b 20 7b 0a 20 20 69   winceLock {.  i
c2fc0 6e 74 20 6e 52 65 61 64 65 72 73 3b 20 20 20 20  nt nReaders;    
c2fd0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
c2fe0 72 65 61 64 65 72 20 6c 6f 63 6b 73 20 6f 62 74  reader locks obt
c2ff0 61 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20  ained */.  BOOL 
c3000 62 50 65 6e 64 69 6e 67 3b 20 20 20 20 20 20 2f  bPending;      /
c3010 2a 20 49 6e 64 69 63 61 74 65 73 20 61 20 70 65  * Indicates a pe
c3020 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62  nding lock has b
c3030 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a  een obtained */.
c3040 20 20 42 4f 4f 4c 20 62 52 65 73 65 72 76 65 64    BOOL bReserved
c3050 3b 20 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 74  ;     /* Indicat
c3060 65 73 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f  es a reserved lo
c3070 63 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61  ck has been obta
c3080 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62  ined */.  BOOL b
c3090 45 78 63 6c 75 73 69 76 65 3b 20 20 20 20 2f 2a  Exclusive;    /*
c30a0 20 49 6e 64 69 63 61 74 65 73 20 61 6e 20 65 78   Indicates an ex
c30b0 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 68 61 73  clusive lock has
c30c0 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a   been obtained *
c30d0 2f 0a 7d 20 77 69 6e 63 65 4c 6f 63 6b 3b 0a 23  /.} winceLock;.#
c30e0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
c30f0 20 77 69 6e 46 69 6c 65 20 73 74 72 75 63 74 75   winFile structu
c3100 72 65 20 69 73 20 61 20 73 75 62 63 6c 61 73 73  re is a subclass
c3110 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65   of sqlite3_file
c3120 2a 20 73 70 65 63 69 66 69 63 20 74 6f 20 74 68  * specific to th
c3130 65 20 77 69 6e 33 32 0a 2a 2a 20 70 6f 72 74 61  e win32.** porta
c3140 62 69 6c 69 74 79 20 6c 61 79 65 72 2e 0a 2a 2f  bility layer..*/
c3150 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
c3160 77 69 6e 46 69 6c 65 20 77 69 6e 46 69 6c 65 3b  winFile winFile;
c3170 0a 73 74 72 75 63 74 20 77 69 6e 46 69 6c 65 20  .struct winFile 
c3180 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  {.  const sqlite
c3190 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d  3_io_methods *pM
c31a0 65 74 68 6f 64 3b 2f 2a 20 4d 75 73 74 20 62 65  ethod;/* Must be
c31b0 20 66 69 72 73 74 20 2a 2f 0a 20 20 48 41 4e 44   first */.  HAND
c31c0 4c 45 20 68 3b 20 20 20 20 20 20 20 20 20 20 20  LE h;           
c31d0 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f      /* Handle fo
c31e0 72 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20  r accessing the 
c31f0 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  file */.  unsign
c3200 65 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65  ed char locktype
c3210 3b 20 2f 2a 20 54 79 70 65 20 6f 66 20 6c 6f 63  ; /* Type of loc
c3220 6b 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64  k currently held
c3230 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f   on this file */
c3240 0a 20 20 73 68 6f 72 74 20 73 68 61 72 65 64 4c  .  short sharedL
c3250 6f 63 6b 42 79 74 65 3b 20 20 20 2f 2a 20 52 61  ockByte;   /* Ra
c3260 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 79  ndomly chosen by
c3270 74 65 20 75 73 65 64 20 61 73 20 61 20 73 68 61  te used as a sha
c3280 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 23 69 66 20  red lock */.#if 
c3290 4f 53 5f 57 49 4e 43 45 0a 20 20 57 43 48 41 52  OS_WINCE.  WCHAR
c32a0 20 2a 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65   *zDeleteOnClose
c32b0 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69  ;  /* Name of fi
c32c0 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 77 68 65  le to delete whe
c32d0 6e 20 63 6c 6f 73 69 6e 67 20 2a 2f 0a 20 20 48  n closing */.  H
c32e0 41 4e 44 4c 45 20 68 4d 75 74 65 78 3b 20 20 20  ANDLE hMutex;   
c32f0 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20         /* Mutex 
c3300 75 73 65 64 20 74 6f 20 63 6f 6e 74 72 6f 6c 20  used to control 
c3310 61 63 63 65 73 73 20 74 6f 20 73 68 61 72 65 64  access to shared
c3320 20 6c 6f 63 6b 20 2a 2f 20 20 0a 20 20 48 41 4e   lock */  .  HAN
c3330 44 4c 45 20 68 53 68 61 72 65 64 3b 20 20 20 20  DLE hShared;    
c3340 20 20 20 20 20 2f 2a 20 53 68 61 72 65 64 20 6d       /* Shared m
c3350 65 6d 6f 72 79 20 73 65 67 6d 65 6e 74 20 75 73  emory segment us
c3360 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 2a  ed for locking *
c3370 2f 0a 20 20 77 69 6e 63 65 4c 6f 63 6b 20 6c 6f  /.  winceLock lo
c3380 63 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c  cal;        /* L
c3390 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 62 79  ocks obtained by
c33a0 20 74 68 69 73 20 69 6e 73 74 61 6e 63 65 20 6f   this instance o
c33b0 66 20 77 69 6e 46 69 6c 65 20 2a 2f 0a 20 20 77  f winFile */.  w
c33c0 69 6e 63 65 4c 6f 63 6b 20 2a 73 68 61 72 65 64  inceLock *shared
c33d0 3b 20 20 20 20 20 20 2f 2a 20 47 6c 6f 62 61 6c  ;      /* Global
c33e0 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6d 65 6d   shared lock mem
c33f0 6f 72 79 20 66 6f 72 20 74 68 65 20 66 69 6c 65  ory for the file
c3400 20 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a    */.#endif.};..
c3410 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
c3420 77 69 6e 67 20 76 61 72 69 61 62 6c 65 20 69 73  wing variable is
c3430 20 28 6e 6f 72 6d 61 6c 6c 79 29 20 73 65 74 20   (normally) set 
c3440 6f 6e 63 65 20 61 6e 64 20 6e 65 76 65 72 20 63  once and never c
c3450 68 61 6e 67 65 73 0a 2a 2a 20 74 68 65 72 65 61  hanges.** therea
c3460 66 74 65 72 2e 20 20 49 74 20 72 65 63 6f 72 64  fter.  It record
c3470 73 20 77 68 65 74 68 65 72 20 74 68 65 20 6f 70  s whether the op
c3480 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69  erating system i
c3490 73 20 57 69 6e 39 35 0a 2a 2a 20 6f 72 20 57 69  s Win95.** or Wi
c34a0 6e 4e 54 2e 0a 2a 2a 0a 2a 2a 20 30 3a 20 20 20  nNT..**.** 0:   
c34b0 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  Operating system
c34c0 20 75 6e 6b 6e 6f 77 6e 2e 0a 2a 2a 20 31 3a 20   unknown..** 1: 
c34d0 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74    Operating syst
c34e0 65 6d 20 69 73 20 57 69 6e 39 35 2e 0a 2a 2a 20  em is Win95..** 
c34f0 32 3a 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73  2:   Operating s
c3500 79 73 74 65 6d 20 69 73 20 57 69 6e 4e 54 2e 0a  ystem is WinNT..
c3510 2a 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 74  **.** In order t
c3520 6f 20 66 61 63 69 6c 69 74 61 74 65 20 74 65 73  o facilitate tes
c3530 74 69 6e 67 20 6f 6e 20 61 20 57 69 6e 4e 54 20  ting on a WinNT 
c3540 73 79 73 74 65 6d 2c 20 74 68 65 20 74 65 73 74  system, the test
c3550 20 66 69 78 74 75 72 65 0a 2a 2a 20 63 61 6e 20   fixture.** can 
c3560 6d 61 6e 75 61 6c 6c 79 20 73 65 74 20 74 68 69  manually set thi
c3570 73 20 76 61 6c 75 65 20 74 6f 20 31 20 74 6f 20  s value to 1 to 
c3580 65 6d 75 6c 61 74 65 20 57 69 6e 39 38 20 62 65  emulate Win98 be
c3590 68 61 76 69 6f 72 2e 0a 2a 2f 0a 23 69 66 64 65  havior..*/.#ifde
c35a0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
c35b0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
c35c0 69 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 30  ite3_os_type = 0
c35d0 3b 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 69  ;.#else.static i
c35e0 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79  nt sqlite3_os_ty
c35f0 70 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  pe = 0;.#endif..
c3600 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  /*.** Return tru
c3610 65 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20  e (non-zero) if 
c3620 77 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75  we are running u
c3630 6e 64 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32  nder WinNT, Win2
c3640 4b 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20  K, WinXP,.** or 
c3650 57 69 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66  WinCE.  Return f
c3660 61 6c 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20  alse (zero) for 
c3670 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72  Win95, Win98, or
c3680 20 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65   WinME..**.** He
c3690 72 65 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73  re is an interes
c36a0 74 69 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e  ting observation
c36b0 3a 20 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c  :  Win95, Win98,
c36c0 20 61 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a   and WinME lack.
c36d0 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45  ** the LockFileE
c36e0 78 28 29 20 41 50 49 2e 20 20 42 75 74 20 77 65  x() API.  But we
c36f0 20 63 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69   can still stati
c3700 63 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e  cally link again
c3710 73 74 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61  st that.** API a
c3720 73 20 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e  s long as we don
c3730 27 74 20 63 61 6c 6c 20 69 74 20 77 69 6e 20 72  't call it win r
c3740 75 6e 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f  unning Win95/98/
c3750 4d 45 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a  ME.  A call to.*
c3760 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * this routine i
c3770 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  s used to determ
c3780 69 6e 65 20 69 66 20 74 68 65 20 68 6f 73 74 20  ine if the host 
c3790 69 73 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f  is Win95/98/ME o
c37a0 72 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50  r.** WinNT/2K/XP
c37b0 20 73 6f 20 74 68 61 74 20 77 65 20 77 69 6c 6c   so that we will
c37c0 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72   know whether or
c37d0 20 6e 6f 74 20 77 65 20 63 61 6e 20 73 61 66 65   not we can safe
c37e0 6c 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c  ly call.** the L
c37f0 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e  ockFileEx() API.
c3800 0a 2a 2f 0a 23 69 66 20 4f 53 5f 57 49 4e 43 45  .*/.#if OS_WINCE
c3810 0a 23 20 64 65 66 69 6e 65 20 69 73 4e 54 28 29  .# define isNT()
c3820 20 20 28 31 29 0a 23 65 6c 73 65 0a 20 20 73 74    (1).#else.  st
c3830 61 74 69 63 20 69 6e 74 20 69 73 4e 54 28 76 6f  atic int isNT(vo
c3840 69 64 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  id){.    if( sql
c3850 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d 3d 30 20  ite3_os_type==0 
c3860 29 7b 0a 20 20 20 20 20 20 4f 53 56 45 52 53 49  ){.      OSVERSI
c3870 4f 4e 49 4e 46 4f 20 73 49 6e 66 6f 3b 0a 20 20  ONINFO sInfo;.  
c3880 20 20 20 20 73 49 6e 66 6f 2e 64 77 4f 53 56 65      sInfo.dwOSVe
c3890 72 73 69 6f 6e 49 6e 66 6f 53 69 7a 65 20 3d 20  rsionInfoSize = 
c38a0 73 69 7a 65 6f 66 28 73 49 6e 66 6f 29 3b 0a 20  sizeof(sInfo);. 
c38b0 20 20 20 20 20 47 65 74 56 65 72 73 69 6f 6e 45       GetVersionE
c38c0 78 28 26 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20  x(&sInfo);.     
c38d0 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65   sqlite3_os_type
c38e0 20 3d 20 73 49 6e 66 6f 2e 64 77 50 6c 61 74 66   = sInfo.dwPlatf
c38f0 6f 72 6d 49 64 3d 3d 56 45 52 5f 50 4c 41 54 46  ormId==VER_PLATF
c3900 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54 20 3f 20 32  ORM_WIN32_NT ? 2
c3910 20 3a 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   : 1;.    }.    
c3920 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6f  return sqlite3_o
c3930 73 5f 74 79 70 65 3d 3d 32 3b 0a 20 20 7d 0a 23  s_type==2;.  }.#
c3940 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 57 49 4e 43  endif /* OS_WINC
c3950 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  E */../*.** Conv
c3960 65 72 74 20 61 20 55 54 46 2d 38 20 73 74 72 69  ert a UTF-8 stri
c3970 6e 67 20 74 6f 20 6d 69 63 72 6f 73 6f 66 74 20  ng to microsoft 
c3980 75 6e 69 63 6f 64 65 20 28 55 54 46 2d 31 36 3f  unicode (UTF-16?
c3990 29 2e 20 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20  ). .**.** Space 
c39a0 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75  to hold the retu
c39b0 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f  rned string is o
c39c0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c  btained from mal
c39d0 6c 6f 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57  loc..*/.static W
c39e0 43 48 41 52 20 2a 75 74 66 38 54 6f 55 6e 69 63  CHAR *utf8ToUnic
c39f0 6f 64 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ode(const char *
c3a00 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e  zFilename){.  in
c3a10 74 20 6e 43 68 61 72 3b 0a 20 20 57 43 48 41 52  t nChar;.  WCHAR
c3a20 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b   *zWideFilename;
c3a30 0a 0a 20 20 6e 43 68 61 72 20 3d 20 4d 75 6c 74  ..  nChar = Mult
c3a40 69 42 79 74 65 54 6f 57 69 64 65 43 68 61 72 28  iByteToWideChar(
c3a50 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 46 69 6c  CP_UTF8, 0, zFil
c3a60 65 6e 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c  ename, -1, NULL,
c3a70 20 30 29 3b 0a 20 20 7a 57 69 64 65 46 69 6c 65   0);.  zWideFile
c3a80 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e  name = malloc( n
c3a90 43 68 61 72 2a 73 69 7a 65 6f 66 28 7a 57 69 64  Char*sizeof(zWid
c3aa0 65 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b  eFilename[0]) );
c3ab0 0a 20 20 69 66 28 20 7a 57 69 64 65 46 69 6c 65  .  if( zWideFile
c3ac0 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72  name==0 ){.    r
c3ad0 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e  eturn 0;.  }.  n
c3ae0 43 68 61 72 20 3d 20 4d 75 6c 74 69 42 79 74 65  Char = MultiByte
c3af0 54 6f 57 69 64 65 43 68 61 72 28 43 50 5f 55 54  ToWideChar(CP_UT
c3b00 46 38 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65  F8, 0, zFilename
c3b10 2c 20 2d 31 2c 20 7a 57 69 64 65 46 69 6c 65 6e  , -1, zWideFilen
c3b20 61 6d 65 2c 20 6e 43 68 61 72 29 3b 0a 20 20 69  ame, nChar);.  i
c3b30 66 28 20 6e 43 68 61 72 3d 3d 30 20 29 7b 0a 20  f( nChar==0 ){. 
c3b40 20 20 20 66 72 65 65 28 7a 57 69 64 65 46 69 6c     free(zWideFil
c3b50 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 57 69 64  ename);.    zWid
c3b60 65 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20  eFilename = 0;. 
c3b70 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 57 69 64   }.  return zWid
c3b80 65 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a  eFilename;.}../*
c3b90 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 69 63 72  .** Convert micr
c3ba0 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 20 74 6f  osoft unicode to
c3bb0 20 55 54 46 2d 38 2e 20 20 53 70 61 63 65 20 74   UTF-8.  Space t
c3bc0 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72  o hold the retur
c3bd0 6e 65 64 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a  ned string is.**
c3be0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d   obtained from m
c3bf0 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74  alloc()..*/.stat
c3c00 69 63 20 63 68 61 72 20 2a 75 6e 69 63 6f 64 65  ic char *unicode
c3c10 54 6f 55 74 66 38 28 63 6f 6e 73 74 20 57 43 48  ToUtf8(const WCH
c3c20 41 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d  AR *zWideFilenam
c3c30 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b  e){.  int nByte;
c3c40 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  .  char *zFilena
c3c50 6d 65 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 57  me;..  nByte = W
c3c60 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79  ideCharToMultiBy
c3c70 74 65 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a  te(CP_UTF8, 0, z
c3c80 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31  WideFilename, -1
c3c90 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  , 0, 0, 0, 0);. 
c3ca0 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c   zFilename = mal
c3cb0 6c 6f 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20  loc( nByte );.  
c3cc0 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30  if( zFilename==0
c3cd0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
c3ce0 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20  ;.  }.  nByte = 
c3cf0 57 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42  WideCharToMultiB
c3d00 79 74 65 28 43 50 5f 55 54 46 38 2c 20 30 2c 20  yte(CP_UTF8, 0, 
c3d10 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d  zWideFilename, -
c3d20 31 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42  1, zFilename, nB
c3d30 79 74 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  yte,.           
c3d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3d50 20 20 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20     0, 0);.  if( 
c3d60 6e 42 79 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20  nByte == 0 ){.  
c3d70 20 20 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65    free(zFilename
c3d80 29 3b 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65  );.    zFilename
c3d90 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 0;.  }.  retu
c3da0 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a  rn zFilename;.}.
c3db0 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61  ./*.** Convert a
c3dc0 6e 20 61 6e 73 69 20 73 74 72 69 6e 67 20 74 6f  n ansi string to
c3dd0 20 6d 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f   microsoft unico
c3de0 64 65 2c 20 62 61 73 65 64 20 6f 6e 20 74 68 65  de, based on the
c3df0 0a 2a 2a 20 63 75 72 72 65 6e 74 20 63 6f 64 65  .** current code
c3e00 70 61 67 65 20 73 65 74 74 69 6e 67 73 20 66 6f  page settings fo
c3e10 72 20 66 69 6c 65 20 61 70 69 73 2e 0a 2a 2a 20  r file apis..** 
c3e20 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c  .** Space to hol
c3e30 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73  d the returned s
c3e40 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65  tring is obtaine
c3e50 64 0a 2a 2a 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63  d.** from malloc
c3e60 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43 48 41  ..*/.static WCHA
c3e70 52 20 2a 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65  R *mbcsToUnicode
c3e80 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69  (const char *zFi
c3e90 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e  lename){.  int n
c3ea0 42 79 74 65 3b 0a 20 20 57 43 48 41 52 20 2a 7a  Byte;.  WCHAR *z
c3eb0 4d 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 20 20  MbcsFilename;.  
c3ec0 69 6e 74 20 63 6f 64 65 70 61 67 65 20 3d 20 41  int codepage = A
c3ed0 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29  reFileApisANSI()
c3ee0 20 3f 20 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f   ? CP_ACP : CP_O
c3ef0 45 4d 43 50 3b 0a 0a 20 20 6e 42 79 74 65 20 3d  EMCP;..  nByte =
c3f00 20 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65   MultiByteToWide
c3f10 43 68 61 72 28 63 6f 64 65 70 61 67 65 2c 20 30  Char(codepage, 0
c3f20 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c  , zFilename, -1,
c3f30 20 4e 55 4c 4c 2c 30 29 2a 73 69 7a 65 6f 66 28   NULL,0)*sizeof(
c3f40 57 43 48 41 52 29 3b 0a 20 20 7a 4d 62 63 73 46  WCHAR);.  zMbcsF
c3f50 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63  ilename = malloc
c3f60 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a  ( nByte*sizeof(z
c3f70 4d 62 63 73 46 69 6c 65 6e 61 6d 65 5b 30 5d 29  MbcsFilename[0])
c3f80 20 29 3b 0a 20 20 69 66 28 20 7a 4d 62 63 73 46   );.  if( zMbcsF
c3f90 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20  ilename==0 ){.  
c3fa0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
c3fb0 20 20 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69 42    nByte = MultiB
c3fc0 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 63 6f  yteToWideChar(co
c3fd0 64 65 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65  depage, 0, zFile
c3fe0 6e 61 6d 65 2c 20 2d 31 2c 20 7a 4d 62 63 73 46  name, -1, zMbcsF
c3ff0 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 29 3b  ilename, nByte);
c4000 0a 20 20 69 66 28 20 6e 42 79 74 65 3d 3d 30 20  .  if( nByte==0 
c4010 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 4d 62 63  ){.    free(zMbc
c4020 73 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20  sFilename);.    
c4030 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 20 3d 20  zMbcsFilename = 
c4040 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
c4050 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 7d  zMbcsFilename;.}
c4060 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  ../*.** Convert 
c4070 6d 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64  microsoft unicod
c4080 65 20 74 6f 20 6d 75 6c 74 69 62 79 74 65 20 63  e to multibyte c
c4090 68 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 2c  haracter string,
c40a0 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a   based on the.**
c40b0 20 75 73 65 72 27 73 20 41 6e 73 69 20 63 6f 64   user's Ansi cod
c40c0 65 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 61  epage..**.** Spa
c40d0 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72  ce to hold the r
c40e0 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69  eturned string i
c40f0 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a  s obtained from.
c4100 2a 2a 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a  ** malloc()..*/.
c4110 73 74 61 74 69 63 20 63 68 61 72 20 2a 75 6e 69  static char *uni
c4120 63 6f 64 65 54 6f 4d 62 63 73 28 63 6f 6e 73 74  codeToMbcs(const
c4130 20 57 43 48 41 52 20 2a 7a 57 69 64 65 46 69 6c   WCHAR *zWideFil
c4140 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42  ename){.  int nB
c4150 79 74 65 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69  yte;.  char *zFi
c4160 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f  lename;.  int co
c4170 64 65 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65  depage = AreFile
c4180 41 70 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f  ApisANSI() ? CP_
c4190 41 43 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a  ACP : CP_OEMCP;.
c41a0 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43  .  nByte = WideC
c41b0 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63  harToMultiByte(c
c41c0 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64  odepage, 0, zWid
c41d0 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30  eFilename, -1, 0
c41e0 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46  , 0, 0, 0);.  zF
c41f0 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63  ilename = malloc
c4200 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28  ( nByte );.  if(
c4210 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b   zFilename==0 ){
c4220 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
c4230 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64   }.  nByte = Wid
c4240 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65  eCharToMultiByte
c4250 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 57  (codepage, 0, zW
c4260 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c  ideFilename, -1,
c4270 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74   zFilename, nByt
c4280 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e,.             
c4290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c42a0 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42   0, 0);.  if( nB
c42b0 79 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20  yte == 0 ){.    
c42c0 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b  free(zFilename);
c42d0 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d  .    zFilename =
c42e0 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   0;.  }.  return
c42f0 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f   zFilename;.}../
c4300 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 75 6c  *.** Convert mul
c4310 74 69 62 79 74 65 20 63 68 61 72 61 63 74 65 72  tibyte character
c4320 20 73 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38   string to UTF-8
c4330 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64  .  Space to hold
c4340 20 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64   the.** returned
c4350 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69   string is obtai
c4360 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28  ned from malloc(
c4370 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61  )..*/.static cha
c4380 72 20 2a 6d 62 63 73 54 6f 55 74 66 38 28 63 6f  r *mbcsToUtf8(co
c4390 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
c43a0 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 46  ame){.  char *zF
c43b0 69 6c 65 6e 61 6d 65 55 74 66 38 3b 0a 20 20 57  ilenameUtf8;.  W
c43c0 43 48 41 52 20 2a 7a 54 6d 70 57 69 64 65 3b 0a  CHAR *zTmpWide;.
c43d0 0a 20 20 7a 54 6d 70 57 69 64 65 20 3d 20 6d 62  .  zTmpWide = mb
c43e0 63 73 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c  csToUnicode(zFil
c43f0 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 54  ename);.  if( zT
c4400 6d 70 57 69 64 65 3d 3d 30 20 29 7b 0a 20 20 20  mpWide==0 ){.   
c4410 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
c4420 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 20 3d   zFilenameUtf8 =
c4430 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 7a   unicodeToUtf8(z
c4440 54 6d 70 57 69 64 65 29 3b 0a 20 20 66 72 65 65  TmpWide);.  free
c4450 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 72 65  (zTmpWide);.  re
c4460 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 55 74  turn zFilenameUt
c4470 66 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  f8;.}../*.** Con
c4480 76 65 72 74 20 55 54 46 2d 38 20 74 6f 20 6d 75  vert UTF-8 to mu
c4490 6c 74 69 62 79 74 65 20 63 68 61 72 61 63 74 65  ltibyte characte
c44a0 72 20 73 74 72 69 6e 67 2e 20 20 53 70 61 63 65  r string.  Space
c44b0 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 0a 2a 2a   to hold the .**
c44c0 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67   returned string
c44d0 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f   is obtained fro
c44e0 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73  m malloc()..*/.s
c44f0 74 61 74 69 63 20 63 68 61 72 20 2a 75 74 66 38  tatic char *utf8
c4500 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 63 68 61  ToMbcs(const cha
c4510 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20  r *zFilename){. 
c4520 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
c4530 4d 62 63 73 3b 0a 20 20 57 43 48 41 52 20 2a 7a  Mbcs;.  WCHAR *z
c4540 54 6d 70 57 69 64 65 3b 0a 0a 20 20 7a 54 6d 70  TmpWide;..  zTmp
c4550 57 69 64 65 20 3d 20 75 74 66 38 54 6f 55 6e 69  Wide = utf8ToUni
c4560 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b  code(zFilename);
c4570 0a 20 20 69 66 28 20 7a 54 6d 70 57 69 64 65 3d  .  if( zTmpWide=
c4580 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
c4590 20 30 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c 65 6e   0;.  }.  zFilen
c45a0 61 6d 65 4d 62 63 73 20 3d 20 75 6e 69 63 6f 64  ameMbcs = unicod
c45b0 65 54 6f 4d 62 63 73 28 7a 54 6d 70 57 69 64 65  eToMbcs(zTmpWide
c45c0 29 3b 0a 20 20 66 72 65 65 28 7a 54 6d 70 57 69  );.  free(zTmpWi
c45d0 64 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 46  de);.  return zF
c45e0 69 6c 65 6e 61 6d 65 4d 62 63 73 3b 0a 7d 0a 0a  ilenameMbcs;.}..
c45f0 23 69 66 20 4f 53 5f 57 49 4e 43 45 0a 2f 2a 2a  #if OS_WINCE./**
c4600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c4610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c4620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c4630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c4640 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
c4650 73 65 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73  section contains
c4660 20 63 6f 64 65 20 66 6f 72 20 57 69 6e 43 45 20   code for WinCE 
c4670 6f 6e 6c 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 57  only..*/./*.** W
c4680 69 6e 64 6f 77 73 43 45 20 64 6f 65 73 20 6e 6f  indowsCE does no
c4690 74 20 68 61 76 65 20 61 20 6c 6f 63 61 6c 74 69  t have a localti
c46a0 6d 65 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20  me() function.  
c46b0 53 6f 20 63 72 65 61 74 65 20 61 0a 2a 2a 20 73  So create a.** s
c46c0 75 62 73 74 69 74 75 74 65 2e 0a 2a 2f 0a 73 74  ubstitute..*/.st
c46d0 72 75 63 74 20 74 6d 20 2a 5f 5f 63 64 65 63 6c  ruct tm *__cdecl
c46e0 20 6c 6f 63 61 6c 74 69 6d 65 28 63 6f 6e 73 74   localtime(const
c46f0 20 74 69 6d 65 5f 74 20 2a 74 29 0a 7b 0a 20 20   time_t *t).{.  
c4700 73 74 61 74 69 63 20 73 74 72 75 63 74 20 74 6d  static struct tm
c4710 20 79 3b 0a 20 20 46 49 4c 45 54 49 4d 45 20 75   y;.  FILETIME u
c4720 54 6d 2c 20 6c 54 6d 3b 0a 20 20 53 59 53 54 45  Tm, lTm;.  SYSTE
c4730 4d 54 49 4d 45 20 70 54 6d 3b 0a 20 20 73 71 6c  MTIME pTm;.  sql
c4740 69 74 65 33 5f 69 6e 74 36 34 20 74 36 34 3b 0a  ite3_int64 t64;.
c4750 20 20 74 36 34 20 3d 20 2a 74 3b 0a 20 20 74 36    t64 = *t;.  t6
c4760 34 20 3d 20 28 74 36 34 20 2b 20 31 31 36 34 34  4 = (t64 + 11644
c4770 34 37 33 36 30 30 29 2a 31 30 30 30 30 30 30 30  473600)*10000000
c4780 3b 0a 20 20 75 54 6d 2e 64 77 4c 6f 77 44 61 74  ;.  uTm.dwLowDat
c4790 65 54 69 6d 65 20 3d 20 74 36 34 20 26 20 30 78  eTime = t64 & 0x
c47a0 46 46 46 46 46 46 46 46 3b 0a 20 20 75 54 6d 2e  FFFFFFFF;.  uTm.
c47b0 64 77 48 69 67 68 44 61 74 65 54 69 6d 65 3d 20  dwHighDateTime= 
c47c0 74 36 34 20 3e 3e 20 33 32 3b 0a 20 20 46 69 6c  t64 >> 32;.  Fil
c47d0 65 54 69 6d 65 54 6f 4c 6f 63 61 6c 46 69 6c 65  eTimeToLocalFile
c47e0 54 69 6d 65 28 26 75 54 6d 2c 26 6c 54 6d 29 3b  Time(&uTm,&lTm);
c47f0 0a 20 20 46 69 6c 65 54 69 6d 65 54 6f 53 79 73  .  FileTimeToSys
c4800 74 65 6d 54 69 6d 65 28 26 6c 54 6d 2c 26 70 54  temTime(&lTm,&pT
c4810 6d 29 3b 0a 20 20 79 2e 74 6d 5f 79 65 61 72 20  m);.  y.tm_year 
c4820 3d 20 70 54 6d 2e 77 59 65 61 72 20 2d 20 31 39  = pTm.wYear - 19
c4830 30 30 3b 0a 20 20 79 2e 74 6d 5f 6d 6f 6e 20 3d  00;.  y.tm_mon =
c4840 20 70 54 6d 2e 77 4d 6f 6e 74 68 20 2d 20 31 3b   pTm.wMonth - 1;
c4850 0a 20 20 79 2e 74 6d 5f 77 64 61 79 20 3d 20 70  .  y.tm_wday = p
c4860 54 6d 2e 77 44 61 79 4f 66 57 65 65 6b 3b 0a 20  Tm.wDayOfWeek;. 
c4870 20 79 2e 74 6d 5f 6d 64 61 79 20 3d 20 70 54 6d   y.tm_mday = pTm
c4880 2e 77 44 61 79 3b 0a 20 20 79 2e 74 6d 5f 68 6f  .wDay;.  y.tm_ho
c4890 75 72 20 3d 20 70 54 6d 2e 77 48 6f 75 72 3b 0a  ur = pTm.wHour;.
c48a0 20 20 79 2e 74 6d 5f 6d 69 6e 20 3d 20 70 54 6d    y.tm_min = pTm
c48b0 2e 77 4d 69 6e 75 74 65 3b 0a 20 20 79 2e 74 6d  .wMinute;.  y.tm
c48c0 5f 73 65 63 20 3d 20 70 54 6d 2e 77 53 65 63 6f  _sec = pTm.wSeco
c48d0 6e 64 3b 0a 20 20 72 65 74 75 72 6e 20 26 79 3b  nd;.  return &y;
c48e0 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 77 69 6c 6c  .}../* This will
c48f0 20 6e 65 76 65 72 20 62 65 20 63 61 6c 6c 65 64   never be called
c4900 2c 20 62 75 74 20 64 65 66 69 6e 65 64 20 74 6f  , but defined to
c4910 20 6d 61 6b 65 20 74 68 65 20 63 6f 64 65 20 63   make the code c
c4920 6f 6d 70 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e  ompile */.#defin
c4930 65 20 47 65 74 54 65 6d 70 50 61 74 68 41 28 61  e GetTempPathA(a
c4940 2c 62 29 0a 0a 23 64 65 66 69 6e 65 20 4c 6f 63  ,b)..#define Loc
c4950 6b 46 69 6c 65 28 61 2c 62 2c 63 2c 64 2c 65 29  kFile(a,b,c,d,e)
c4960 20 20 20 20 20 20 20 77 69 6e 63 65 4c 6f 63 6b         winceLock
c4970 46 69 6c 65 28 26 61 2c 20 62 2c 20 63 2c 20 64  File(&a, b, c, d
c4980 2c 20 65 29 0a 23 64 65 66 69 6e 65 20 55 6e 6c  , e).#define Unl
c4990 6f 63 6b 46 69 6c 65 28 61 2c 62 2c 63 2c 64 2c  ockFile(a,b,c,d,
c49a0 65 29 20 20 20 20 20 77 69 6e 63 65 55 6e 6c 6f  e)     winceUnlo
c49b0 63 6b 46 69 6c 65 28 26 61 2c 20 62 2c 20 63 2c  ckFile(&a, b, c,
c49c0 20 64 2c 20 65 29 0a 23 64 65 66 69 6e 65 20 4c   d, e).#define L
c49d0 6f 63 6b 46 69 6c 65 45 78 28 61 2c 62 2c 63 2c  ockFileEx(a,b,c,
c49e0 64 2c 65 2c 66 29 20 20 20 77 69 6e 63 65 4c 6f  d,e,f)   winceLo
c49f0 63 6b 46 69 6c 65 45 78 28 26 61 2c 20 62 2c 20  ckFileEx(&a, b, 
c4a00 63 2c 20 64 2c 20 65 2c 20 66 29 0a 0a 23 64 65  c, d, e, f)..#de
c4a10 66 69 6e 65 20 48 41 4e 44 4c 45 5f 54 4f 5f 57  fine HANDLE_TO_W
c4a20 49 4e 46 49 4c 45 28 61 29 20 28 77 69 6e 46 69  INFILE(a) (winFi
c4a30 6c 65 2a 29 26 28 28 63 68 61 72 2a 29 61 29 5b  le*)&((char*)a)[
c4a40 2d 6f 66 66 73 65 74 6f 66 28 77 69 6e 46 69 6c  -offsetof(winFil
c4a50 65 2c 68 29 5d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71  e,h)]../*.** Acq
c4a60 75 69 72 65 20 61 20 6c 6f 63 6b 20 6f 6e 20 74  uire a lock on t
c4a70 68 65 20 68 61 6e 64 6c 65 20 68 0a 2a 2f 0a 73  he handle h.*/.s
c4a80 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 63 65  tatic void wince
c4a90 4d 75 74 65 78 41 63 71 75 69 72 65 28 48 41 4e  MutexAcquire(HAN
c4aa0 44 4c 45 20 68 29 7b 0a 20 20 20 44 57 4f 52 44  DLE h){.   DWORD
c4ab0 20 64 77 45 72 72 3b 0a 20 20 20 64 6f 20 7b 0a   dwErr;.   do {.
c4ac0 20 20 20 20 20 64 77 45 72 72 20 3d 20 57 61 69       dwErr = Wai
c4ad0 74 46 6f 72 53 69 6e 67 6c 65 4f 62 6a 65 63 74  tForSingleObject
c4ae0 28 68 2c 20 49 4e 46 49 4e 49 54 45 29 3b 0a 20  (h, INFINITE);. 
c4af0 20 20 7d 20 77 68 69 6c 65 20 28 64 77 45 72 72    } while (dwErr
c4b00 20 21 3d 20 57 41 49 54 5f 4f 42 4a 45 43 54 5f   != WAIT_OBJECT_
c4b10 30 20 26 26 20 64 77 45 72 72 20 21 3d 20 57 41  0 && dwErr != WA
c4b20 49 54 5f 41 42 41 4e 44 4f 4e 45 44 29 3b 0a 7d  IT_ABANDONED);.}
c4b30 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61  ./*.** Release a
c4b40 20 6c 6f 63 6b 20 61 63 71 75 69 72 65 64 20 62   lock acquired b
c4b50 79 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75  y winceMutexAcqu
c4b60 69 72 65 28 29 0a 2a 2f 0a 23 64 65 66 69 6e 65  ire().*/.#define
c4b70 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61   winceMutexRelea
c4b80 73 65 28 68 29 20 52 65 6c 65 61 73 65 4d 75 74  se(h) ReleaseMut
c4b90 65 78 28 68 29 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  ex(h)../*.** Cre
c4ba0 61 74 65 20 74 68 65 20 6d 75 74 65 78 20 61 6e  ate the mutex an
c4bb0 64 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20  d shared memory 
c4bc0 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67  used for locking
c4bd0 20 69 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20   in the file.** 
c4be0 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65  descriptor pFile
c4bf0 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20  .*/.static BOOL 
c4c00 77 69 6e 63 65 43 72 65 61 74 65 4c 6f 63 6b 28  winceCreateLock(
c4c10 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
c4c20 65 6e 61 6d 65 2c 20 77 69 6e 46 69 6c 65 20 2a  ename, winFile *
c4c30 70 46 69 6c 65 29 7b 0a 20 20 57 43 48 41 52 20  pFile){.  WCHAR 
c4c40 2a 7a 54 6f 6b 3b 0a 20 20 57 43 48 41 52 20 2a  *zTok;.  WCHAR *
c4c50 7a 4e 61 6d 65 20 3d 20 75 74 66 38 54 6f 55 6e  zName = utf8ToUn
c4c60 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29  icode(zFilename)
c4c70 3b 0a 20 20 42 4f 4f 4c 20 62 49 6e 69 74 20 3d  ;.  BOOL bInit =
c4c80 20 54 52 55 45 3b 0a 0a 20 20 2f 2a 20 49 6e 69   TRUE;..  /* Ini
c4c90 74 69 61 6c 69 7a 65 20 74 68 65 20 6c 6f 63 61  tialize the loca
c4ca0 6c 20 6c 6f 63 6b 64 61 74 61 20 2a 2f 0a 20 20  l lockdata */.  
c4cb0 5a 65 72 6f 4d 65 6d 6f 72 79 28 26 70 46 69 6c  ZeroMemory(&pFil
c4cc0 65 2d 3e 6c 6f 63 61 6c 2c 20 73 69 7a 65 6f 66  e->local, sizeof
c4cd0 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 29 29 3b  (pFile->local));
c4ce0 0a 0a 20 20 2f 2a 20 52 65 70 6c 61 63 65 20 74  ..  /* Replace t
c4cf0 68 65 20 62 61 63 6b 73 6c 61 73 68 65 73 20 66  he backslashes f
c4d00 72 6f 6d 20 74 68 65 20 66 69 6c 65 6e 61 6d 65  rom the filename
c4d10 20 61 6e 64 20 6c 6f 77 65 72 63 61 73 65 20 69   and lowercase i
c4d20 74 0a 20 20 2a 2a 20 74 6f 20 64 65 72 69 76 65  t.  ** to derive
c4d30 20 61 20 6d 75 74 65 78 20 6e 61 6d 65 2e 20 2a   a mutex name. *
c4d40 2f 0a 20 20 7a 54 6f 6b 20 3d 20 43 68 61 72 4c  /.  zTok = CharL
c4d50 6f 77 65 72 57 28 7a 4e 61 6d 65 29 3b 0a 20 20  owerW(zName);.  
c4d60 66 6f 72 20 28 3b 2a 7a 54 6f 6b 3b 7a 54 6f 6b  for (;*zTok;zTok
c4d70 2b 2b 29 7b 0a 20 20 20 20 69 66 20 28 2a 7a 54  ++){.    if (*zT
c4d80 6f 6b 20 3d 3d 20 27 5c 5c 27 29 20 2a 7a 54 6f  ok == '\\') *zTo
c4d90 6b 20 3d 20 27 5f 27 3b 0a 20 20 7d 0a 0a 20 20  k = '_';.  }..  
c4da0 2f 2a 20 43 72 65 61 74 65 2f 6f 70 65 6e 20 74  /* Create/open t
c4db0 68 65 20 6e 61 6d 65 64 20 6d 75 74 65 78 20 2a  he named mutex *
c4dc0 2f 0a 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65  /.  pFile->hMute
c4dd0 78 20 3d 20 43 72 65 61 74 65 4d 75 74 65 78 57  x = CreateMutexW
c4de0 28 4e 55 4c 4c 2c 20 46 41 4c 53 45 2c 20 7a 4e  (NULL, FALSE, zN
c4df0 61 6d 65 29 3b 0a 20 20 69 66 20 28 21 70 46 69  ame);.  if (!pFi
c4e00 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a 20 20 20  le->hMutex){.   
c4e10 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a 20 20   free(zName);.  
c4e20 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45 3b 0a    return FALSE;.
c4e30 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72    }..  /* Acquir
c4e40 65 20 74 68 65 20 6d 75 74 65 78 20 62 65 66 6f  e the mutex befo
c4e50 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67 20 2a 2f  re continuing */
c4e60 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71  .  winceMutexAcq
c4e70 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74  uire(pFile->hMut
c4e80 65 78 29 3b 0a 20 20 0a 20 20 2f 2a 20 53 69 6e  ex);.  .  /* Sin
c4e90 63 65 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20  ce the names of 
c4ea0 6e 61 6d 65 64 20 6d 75 74 65 78 65 73 2c 20 73  named mutexes, s
c4eb0 65 6d 61 70 68 6f 72 65 73 2c 20 66 69 6c 65 20  emaphores, file 
c4ec0 6d 61 70 70 69 6e 67 73 20 65 74 63 20 61 72 65  mappings etc are
c4ed0 20 0a 20 20 2a 2a 20 63 61 73 65 2d 73 65 6e 73   .  ** case-sens
c4ee0 69 74 69 76 65 2c 20 74 61 6b 65 20 61 64 76 61  itive, take adva
c4ef0 6e 74 61 67 65 20 6f 66 20 74 68 61 74 20 62 79  ntage of that by
c4f00 20 75 70 70 65 72 63 61 73 69 6e 67 20 74 68 65   uppercasing the
c4f10 20 6d 75 74 65 78 20 6e 61 6d 65 0a 20 20 2a 2a   mutex name.  **
c4f20 20 61 6e 64 20 75 73 69 6e 67 20 74 68 61 74 20   and using that 
c4f30 61 73 20 74 68 65 20 73 68 61 72 65 64 20 66 69  as the shared fi
c4f40 6c 65 6d 61 70 70 69 6e 67 20 6e 61 6d 65 2e 0a  lemapping name..
c4f50 20 20 2a 2f 0a 20 20 43 68 61 72 55 70 70 65 72    */.  CharUpper
c4f60 57 28 7a 4e 61 6d 65 29 3b 0a 20 20 70 46 69 6c  W(zName);.  pFil
c4f70 65 2d 3e 68 53 68 61 72 65 64 20 3d 20 43 72 65  e->hShared = Cre
c4f80 61 74 65 46 69 6c 65 4d 61 70 70 69 6e 67 57 28  ateFileMappingW(
c4f90 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56  INVALID_HANDLE_V
c4fa0 41 4c 55 45 2c 20 4e 55 4c 4c 2c 0a 20 20 20 20  ALUE, NULL,.    
c4fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c4fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c4fd0 20 20 20 50 41 47 45 5f 52 45 41 44 57 52 49 54     PAGE_READWRIT
c4fe0 45 2c 20 30 2c 20 73 69 7a 65 6f 66 28 77 69 6e  E, 0, sizeof(win
c4ff0 63 65 4c 6f 63 6b 29 2c 0a 20 20 20 20 20 20 20  ceLock),.       
c5000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c5010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c5020 7a 4e 61 6d 65 29 3b 20 20 0a 0a 20 20 2f 2a 20  zName);  ..  /* 
c5030 53 65 74 20 61 20 66 6c 61 67 20 74 68 61 74 20  Set a flag that 
c5040 69 6e 64 69 63 61 74 65 73 20 77 65 27 72 65 20  indicates we're 
c5050 74 68 65 20 66 69 72 73 74 20 74 6f 20 63 72 65  the first to cre
c5060 61 74 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 73  ate the memory s
c5070 6f 20 69 74 20 0a 20 20 2a 2a 20 6d 75 73 74 20  o it .  ** must 
c5080 62 65 20 7a 65 72 6f 2d 69 6e 69 74 69 61 6c 69  be zero-initiali
c5090 7a 65 64 20 2a 2f 0a 20 20 69 66 20 28 47 65 74  zed */.  if (Get
c50a0 4c 61 73 74 45 72 72 6f 72 28 29 20 3d 3d 20 45  LastError() == E
c50b0 52 52 4f 52 5f 41 4c 52 45 41 44 59 5f 45 58 49  RROR_ALREADY_EXI
c50c0 53 54 53 29 7b 0a 20 20 20 20 62 49 6e 69 74 20  STS){.    bInit 
c50d0 3d 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 0a 20 20  = FALSE;.  }..  
c50e0 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a 0a 20 20  free(zName);..  
c50f0 2f 2a 20 49 66 20 77 65 20 73 75 63 63 65 65 64  /* If we succeed
c5100 65 64 20 69 6e 20 6d 61 6b 69 6e 67 20 74 68 65  ed in making the
c5110 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 68   shared memory h
c5120 61 6e 64 6c 65 2c 20 6d 61 70 20 69 74 2e 20 2a  andle, map it. *
c5130 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 68  /.  if (pFile->h
c5140 53 68 61 72 65 64 29 7b 0a 20 20 20 20 70 46 69  Shared){.    pFi
c5150 6c 65 2d 3e 73 68 61 72 65 64 20 3d 20 28 77 69  le->shared = (wi
c5160 6e 63 65 4c 6f 63 6b 2a 29 4d 61 70 56 69 65 77  nceLock*)MapView
c5170 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 53  OfFile(pFile->hS
c5180 68 61 72 65 64 2c 20 0a 20 20 20 20 20 20 20 20  hared, .        
c5190 20 20 20 20 20 46 49 4c 45 5f 4d 41 50 5f 52 45       FILE_MAP_RE
c51a0 41 44 7c 46 49 4c 45 5f 4d 41 50 5f 57 52 49 54  AD|FILE_MAP_WRIT
c51b0 45 2c 20 30 2c 20 30 2c 20 73 69 7a 65 6f 66 28  E, 0, 0, sizeof(
c51c0 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a 20 20 20  winceLock));.   
c51d0 20 2f 2a 20 49 66 20 6d 61 70 70 69 6e 67 20 66   /* If mapping f
c51e0 61 69 6c 65 64 2c 20 63 6c 6f 73 65 20 74 68 65  ailed, close the
c51f0 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 68   shared memory h
c5200 61 6e 64 6c 65 20 61 6e 64 20 65 72 61 73 65 20  andle and erase 
c5210 69 74 20 2a 2f 0a 20 20 20 20 69 66 20 28 21 70  it */.    if (!p
c5220 46 69 6c 65 2d 3e 73 68 61 72 65 64 29 7b 0a 20  File->shared){. 
c5230 20 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65       CloseHandle
c5240 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 29  (pFile->hShared)
c5250 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68  ;.      pFile->h
c5260 53 68 61 72 65 64 20 3d 20 4e 55 4c 4c 3b 0a 20  Shared = NULL;. 
c5270 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
c5280 66 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20  f shared memory 
c5290 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 72 65  could not be cre
c52a0 61 74 65 64 2c 20 74 68 65 6e 20 63 6c 6f 73 65  ated, then close
c52b0 20 74 68 65 20 6d 75 74 65 78 20 61 6e 64 20 66   the mutex and f
c52c0 61 69 6c 20 2a 2f 0a 20 20 69 66 20 28 70 46 69  ail */.  if (pFi
c52d0 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 3d 20 4e  le->hShared == N
c52e0 55 4c 4c 29 7b 0a 20 20 20 20 77 69 6e 63 65 4d  ULL){.    winceM
c52f0 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c  utexRelease(pFil
c5300 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 20 20  e->hMutex);.    
c5310 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c  CloseHandle(pFil
c5320 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 20 20  e->hMutex);.    
c5330 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20  pFile->hMutex = 
c5340 4e 55 4c 4c 3b 0a 20 20 20 20 72 65 74 75 72 6e  NULL;.    return
c5350 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 20 20 0a 20   FALSE;.  }.  . 
c5360 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74   /* Initialize t
c5370 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79  he shared memory
c5380 20 69 66 20 77 65 27 72 65 20 73 75 70 70 6f 73   if we're suppos
c5390 65 64 20 74 6f 20 2a 2f 0a 20 20 69 66 20 28 62  ed to */.  if (b
c53a0 49 6e 69 74 29 20 7b 0a 20 20 20 20 5a 65 72 6f  Init) {.    Zero
c53b0 4d 65 6d 6f 72 79 28 70 46 69 6c 65 2d 3e 73 68  Memory(pFile->sh
c53c0 61 72 65 64 2c 20 73 69 7a 65 6f 66 28 77 69 6e  ared, sizeof(win
c53d0 63 65 4c 6f 63 6b 29 29 3b 0a 20 20 7d 0a 0a 20  ceLock));.  }.. 
c53e0 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61   winceMutexRelea
c53f0 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78  se(pFile->hMutex
c5400 29 3b 0a 20 20 72 65 74 75 72 6e 20 54 52 55 45  );.  return TRUE
c5410 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72  ;.}../*.** Destr
c5420 6f 79 20 74 68 65 20 70 61 72 74 20 6f 66 20 77  oy the part of w
c5430 69 6e 46 69 6c 65 20 74 68 61 74 20 64 65 61 6c  inFile that deal
c5440 73 20 77 69 74 68 20 77 69 6e 63 65 20 6c 6f 63  s with wince loc
c5450 6b 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ks.*/.static voi
c5460 64 20 77 69 6e 63 65 44 65 73 74 72 6f 79 4c 6f  d winceDestroyLo
c5470 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c  ck(winFile *pFil
c5480 65 29 7b 0a 20 20 69 66 20 28 70 46 69 6c 65 2d  e){.  if (pFile-
c5490 3e 68 4d 75 74 65 78 29 7b 0a 20 20 20 20 2f 2a  >hMutex){.    /*
c54a0 20 41 63 71 75 69 72 65 20 74 68 65 20 6d 75 74   Acquire the mut
c54b0 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e 63 65 4d  ex */.    winceM
c54c0 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c  utexAcquire(pFil
c54d0 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 20  e->hMutex);..   
c54e0 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   /* The followin
c54f0 67 20 62 6c 6f 63 6b 73 20 73 68 6f 75 6c 64 20  g blocks should 
c5500 70 72 6f 62 61 62 6c 79 20 61 73 73 65 72 74 20  probably assert 
c5510 69 6e 20 64 65 62 75 67 20 6d 6f 64 65 2c 20 62  in debug mode, b
c5520 75 74 20 74 68 65 79 0a 20 20 20 20 20 20 20 61  ut they.       a
c5530 72 65 20 74 6f 20 63 6c 65 61 6e 75 70 20 69 6e  re to cleanup in
c5540 20 63 61 73 65 20 61 6e 79 20 6c 6f 63 6b 73 20   case any locks 
c5550 72 65 6d 61 69 6e 65 64 20 6f 70 65 6e 20 2a 2f  remained open */
c5560 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  .    if (pFile->
c5570 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 29 7b  local.nReaders){
c5580 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68  .      pFile->sh
c5590 61 72 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 2d  ared->nReaders -
c55a0 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20  -;.    }.    if 
c55b0 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52  (pFile->local.bR
c55c0 65 73 65 72 76 65 64 29 7b 0a 20 20 20 20 20 20  eserved){.      
c55d0 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62  pFile->shared->b
c55e0 52 65 73 65 72 76 65 64 20 3d 20 46 41 4c 53 45  Reserved = FALSE
c55f0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28  ;.    }.    if (
c5600 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65  pFile->local.bPe
c5610 6e 64 69 6e 67 29 7b 0a 20 20 20 20 20 20 70 46  nding){.      pF
c5620 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65  ile->shared->bPe
c5630 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20  nding = FALSE;. 
c5640 20 20 20 7d 0a 20 20 20 20 69 66 20 28 70 46 69     }.    if (pFi
c5650 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c 75  le->local.bExclu
c5660 73 69 76 65 29 7b 0a 20 20 20 20 20 20 70 46 69  sive){.      pFi
c5670 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63  le->shared->bExc
c5680 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 45 3b 0a  lusive = FALSE;.
c5690 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65      }..    /* De
c56a0 2d 72 65 66 65 72 65 6e 63 65 20 61 6e 64 20 63  -reference and c
c56b0 6c 6f 73 65 20 6f 75 72 20 63 6f 70 79 20 6f 66  lose our copy of
c56c0 20 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f   the shared memo
c56d0 72 79 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 20  ry handle */.   
c56e0 20 55 6e 6d 61 70 56 69 65 77 4f 66 46 69 6c 65   UnmapViewOfFile
c56f0 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 29 3b  (pFile->shared);
c5700 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65  .    CloseHandle
c5710 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 29  (pFile->hShared)
c5720 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 6e 65 20 77  ;..    /* Done w
c5730 69 74 68 20 74 68 65 20 6d 75 74 65 78 20 2a 2f  ith the mutex */
c5740 0a 20 20 20 20 77 69 6e 63 65 4d 75 74 65 78 52  .    winceMutexR
c5750 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d  elease(pFile->hM
c5760 75 74 65 78 29 3b 20 20 20 20 0a 20 20 20 20 43  utex);    .    C
c5770 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65  loseHandle(pFile
c5780 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 20 20 70  ->hMutex);.    p
c5790 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20 4e  File->hMutex = N
c57a0 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a  ULL;.  }.}../* .
c57b0 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61  ** An implementa
c57c0 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c 6f 63 6b  tion of the Lock
c57d0 46 69 6c 65 28 29 20 41 50 49 20 6f 66 20 77 69  File() API of wi
c57e0 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 0a  ndows for wince.
c57f0 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 77  */.static BOOL w
c5800 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 0a 20 20  inceLockFile(.  
c5810 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65 2c 0a  HANDLE *phFile,.
c5820 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66    DWORD dwFileOf
c5830 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57 4f 52 44  fsetLow,.  DWORD
c5840 20 64 77 46 69 6c 65 4f 66 66 73 65 74 48 69 67   dwFileOffsetHig
c5850 68 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62  h,.  DWORD nNumb
c5860 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c  erOfBytesToLockL
c5870 6f 77 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d  ow,.  DWORD nNum
c5880 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b  berOfBytesToLock
c5890 48 69 67 68 0a 29 7b 0a 20 20 77 69 6e 46 69 6c  High.){.  winFil
c58a0 65 20 2a 70 46 69 6c 65 20 3d 20 48 41 4e 44 4c  e *pFile = HANDL
c58b0 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 70 68 46  E_TO_WINFILE(phF
c58c0 69 6c 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 52 65  ile);.  BOOL bRe
c58d0 74 75 72 6e 20 3d 20 46 41 4c 53 45 3b 0a 0a 20  turn = FALSE;.. 
c58e0 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 68 4d 75   if (!pFile->hMu
c58f0 74 65 78 29 20 72 65 74 75 72 6e 20 54 52 55 45  tex) return TRUE
c5900 3b 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 41 63  ;.  winceMutexAc
c5910 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75  quire(pFile->hMu
c5920 74 65 78 29 3b 0a 0a 20 20 2f 2a 20 57 61 6e 74  tex);..  /* Want
c5930 69 6e 67 20 61 6e 20 65 78 63 6c 75 73 69 76 65  ing an exclusive
c5940 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 69 66 20 28   lock? */.  if (
c5950 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20  dwFileOffsetLow 
c5960 3d 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 0a  == SHARED_FIRST.
c5970 20 20 20 20 20 20 20 26 26 20 6e 4e 75 6d 62 65         && nNumbe
c5980 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f  rOfBytesToLockLo
c5990 77 20 3d 3d 20 53 48 41 52 45 44 5f 53 49 5a 45  w == SHARED_SIZE
c59a0 29 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65  ){.    if (pFile
c59b0 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65  ->shared->nReade
c59c0 72 73 20 3d 3d 20 30 20 26 26 20 70 46 69 6c 65  rs == 0 && pFile
c59d0 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75  ->shared->bExclu
c59e0 73 69 76 65 20 3d 3d 20 30 29 7b 0a 20 20 20 20  sive == 0){.    
c59f0 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64     pFile->shared
c5a00 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 54  ->bExclusive = T
c5a10 52 55 45 3b 0a 20 20 20 20 20 20 20 70 46 69 6c  RUE;.       pFil
c5a20 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 73  e->local.bExclus
c5a30 69 76 65 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  ive = TRUE;.    
c5a40 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55     bReturn = TRU
c5a50 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  E;.    }.  }..  
c5a60 2f 2a 20 57 61 6e 74 20 61 20 72 65 61 64 2d 6f  /* Want a read-o
c5a70 6e 6c 79 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 65  nly lock? */.  e
c5a80 6c 73 65 20 69 66 20 28 28 64 77 46 69 6c 65 4f  lse if ((dwFileO
c5a90 66 66 73 65 74 4c 6f 77 20 3e 3d 20 53 48 41 52  ffsetLow >= SHAR
c5aa0 45 44 5f 46 49 52 53 54 20 26 26 0a 20 20 20 20  ED_FIRST &&.    
c5ab0 20 20 20 20 20 20 20 20 64 77 46 69 6c 65 4f 66          dwFileOf
c5ac0 66 73 65 74 4c 6f 77 20 3c 20 53 48 41 52 45 44  fsetLow < SHARED
c5ad0 5f 46 49 52 53 54 20 2b 20 53 48 41 52 45 44 5f  _FIRST + SHARED_
c5ae0 53 49 5a 45 29 20 26 26 0a 20 20 20 20 20 20 20  SIZE) &&.       
c5af0 20 20 20 20 20 6e 4e 75 6d 62 65 72 4f 66 42 79       nNumberOfBy
c5b00 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20  tesToLockLow == 
c5b10 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c  1){.    if (pFil
c5b20 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c  e->shared->bExcl
c5b30 75 73 69 76 65 20 3d 3d 20 30 29 7b 0a 20 20 20  usive == 0){.   
c5b40 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e     pFile->local.
c5b50 6e 52 65 61 64 65 72 73 20 2b 2b 3b 0a 20 20 20  nReaders ++;.   
c5b60 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f     if (pFile->lo
c5b70 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 3d 3d 20  cal.nReaders == 
c5b80 31 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c  1){.        pFil
c5b90 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64  e->shared->nRead
c5ba0 65 72 73 20 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a  ers ++;.      }.
c5bb0 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20        bReturn = 
c5bc0 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  TRUE;.    }.  }.
c5bd0 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 70 65 6e  .  /* Want a pen
c5be0 64 69 6e 67 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20  ding lock? */.  
c5bf0 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c 65 4f  else if (dwFileO
c5c00 66 66 73 65 74 4c 6f 77 20 3d 3d 20 50 45 4e 44  ffsetLow == PEND
c5c10 49 4e 47 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d  ING_BYTE && nNum
c5c20 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b  berOfBytesToLock
c5c30 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 2f  Low == 1){.    /
c5c40 2a 20 49 66 20 6e 6f 20 70 65 6e 64 69 6e 67 20  * If no pending 
c5c50 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 61 63  lock has been ac
c5c60 71 75 69 72 65 64 2c 20 74 68 65 6e 20 61 63 71  quired, then acq
c5c70 75 69 72 65 20 69 74 20 2a 2f 0a 20 20 20 20 69  uire it */.    i
c5c80 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64  f (pFile->shared
c5c90 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 3d 20 30 29  ->bPending == 0)
c5ca0 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e   {.      pFile->
c5cb0 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67  shared->bPending
c5cc0 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20 70   = TRUE;.      p
c5cd0 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e  File->local.bPen
c5ce0 64 69 6e 67 20 3d 20 54 52 55 45 3b 0a 20 20 20  ding = TRUE;.   
c5cf0 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55     bReturn = TRU
c5d00 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f  E;.    }.  }.  /
c5d10 2a 20 57 61 6e 74 20 61 20 72 65 73 65 72 76 65  * Want a reserve
c5d20 64 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 65 6c 73  d lock? */.  els
c5d30 65 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73  e if (dwFileOffs
c5d40 65 74 4c 6f 77 20 3d 3d 20 52 45 53 45 52 56 45  etLow == RESERVE
c5d50 44 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65  D_BYTE && nNumbe
c5d60 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f  rOfBytesToLockLo
c5d70 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 20  w == 1){.    if 
c5d80 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e  (pFile->shared->
c5d90 62 52 65 73 65 72 76 65 64 20 3d 3d 20 30 29 20  bReserved == 0) 
c5da0 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73  {.      pFile->s
c5db0 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64  hared->bReserved
c5dc0 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20 70   = TRUE;.      p
c5dd0 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73  File->local.bRes
c5de0 65 72 76 65 64 20 3d 20 54 52 55 45 3b 0a 20 20  erved = TRUE;.  
c5df0 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52      bReturn = TR
c5e00 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  UE;.    }.  }.. 
c5e10 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61   winceMutexRelea
c5e20 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78  se(pFile->hMutex
c5e30 29 3b 0a 20 20 72 65 74 75 72 6e 20 62 52 65 74  );.  return bRet
c5e40 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e  urn;.}../*.** An
c5e50 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
c5e60 6f 66 20 74 68 65 20 55 6e 6c 6f 63 6b 46 69 6c  of the UnlockFil
c5e70 65 20 41 50 49 20 6f 66 20 77 69 6e 64 6f 77 73  e API of windows
c5e80 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74   for wince.*/.st
c5e90 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 55  atic BOOL winceU
c5ea0 6e 6c 6f 63 6b 46 69 6c 65 28 0a 20 20 48 41 4e  nlockFile(.  HAN
c5eb0 44 4c 45 20 2a 70 68 46 69 6c 65 2c 0a 20 20 44  DLE *phFile,.  D
c5ec0 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65  WORD dwFileOffse
c5ed0 74 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 64 77  tLow,.  DWORD dw
c5ee0 46 69 6c 65 4f 66 66 73 65 74 48 69 67 68 2c 0a  FileOffsetHigh,.
c5ef0 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f    DWORD nNumberO
c5f00 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f  fBytesToUnlockLo
c5f10 77 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62  w,.  DWORD nNumb
c5f20 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63  erOfBytesToUnloc
c5f30 6b 48 69 67 68 0a 29 7b 0a 20 20 77 69 6e 46 69  kHigh.){.  winFi
c5f40 6c 65 20 2a 70 46 69 6c 65 20 3d 20 48 41 4e 44  le *pFile = HAND
c5f50 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 70 68  LE_TO_WINFILE(ph
c5f60 46 69 6c 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 52  File);.  BOOL bR
c5f70 65 74 75 72 6e 20 3d 20 46 41 4c 53 45 3b 0a 0a  eturn = FALSE;..
c5f80 20 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 68 4d    if (!pFile->hM
c5f90 75 74 65 78 29 20 72 65 74 75 72 6e 20 54 52 55  utex) return TRU
c5fa0 45 3b 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 41  E;.  winceMutexA
c5fb0 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d  cquire(pFile->hM
c5fc0 75 74 65 78 29 3b 0a 0a 20 20 2f 2a 20 52 65 6c  utex);..  /* Rel
c5fd0 65 61 73 69 6e 67 20 61 20 72 65 61 64 65 72 20  easing a reader 
c5fe0 6c 6f 63 6b 20 6f 72 20 61 6e 20 65 78 63 6c 75  lock or an exclu
c5ff0 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69  sive lock */.  i
c6000 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c  f (dwFileOffsetL
c6010 6f 77 20 3e 3d 20 53 48 41 52 45 44 5f 46 49 52  ow >= SHARED_FIR
c6020 53 54 20 26 26 0a 20 20 20 20 20 20 20 64 77 46  ST &&.       dwF
c6030 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3c 20 53  ileOffsetLow < S
c6040 48 41 52 45 44 5f 46 49 52 53 54 20 2b 20 53 48  HARED_FIRST + SH
c6050 41 52 45 44 5f 53 49 5a 45 29 7b 0a 20 20 20 20  ARED_SIZE){.    
c6060 2f 2a 20 44 69 64 20 77 65 20 68 61 76 65 20 61  /* Did we have a
c6070 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  n exclusive lock
c6080 3f 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 46 69  ? */.    if (pFi
c6090 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c 75  le->local.bExclu
c60a0 73 69 76 65 29 7b 0a 20 20 20 20 20 20 70 46 69  sive){.      pFi
c60b0 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c 75  le->local.bExclu
c60c0 73 69 76 65 20 3d 20 46 41 4c 53 45 3b 0a 20 20  sive = FALSE;.  
c60d0 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65      pFile->share
c60e0 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 20  d->bExclusive = 
c60f0 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65  FALSE;.      bRe
c6100 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20  turn = TRUE;.   
c6110 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 69 64 20 77   }..    /* Did w
c6120 65 20 6a 75 73 74 20 68 61 76 65 20 61 20 72 65  e just have a re
c6130 61 64 65 72 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20  ader lock? */.  
c6140 20 20 65 6c 73 65 20 69 66 20 28 70 46 69 6c 65    else if (pFile
c6150 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73  ->local.nReaders
c6160 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  ){.      pFile->
c6170 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 2d  local.nReaders -
c6180 2d 3b 0a 20 20 20 20 20 20 69 66 20 28 70 46 69  -;.      if (pFi
c6190 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65  le->local.nReade
c61a0 72 73 20 3d 3d 20 30 29 0a 20 20 20 20 20 20 7b  rs == 0).      {
c61b0 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  .        pFile->
c61c0 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 72 73  shared->nReaders
c61d0 20 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   --;.      }.   
c61e0 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55     bReturn = TRU
c61f0 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  E;.    }.  }..  
c6200 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20 70  /* Releasing a p
c6210 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 2a 2f 0a 20  ending lock */. 
c6220 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c 65   else if (dwFile
c6230 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 50 45 4e  OffsetLow == PEN
c6240 44 49 4e 47 5f 42 59 54 45 20 26 26 20 6e 4e 75  DING_BYTE && nNu
c6250 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c  mberOfBytesToUnl
c6260 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20  ockLow == 1){.  
c6270 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63    if (pFile->loc
c6280 61 6c 2e 62 50 65 6e 64 69 6e 67 29 7b 0a 20 20  al.bPending){.  
c6290 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c      pFile->local
c62a0 2e 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53  .bPending = FALS
c62b0 45 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  E;.      pFile->
c62c0 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67  shared->bPending
c62d0 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20   = FALSE;.      
c62e0 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a  bReturn = TRUE;.
c62f0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 52      }.  }.  /* R
c6300 65 6c 65 61 73 69 6e 67 20 61 20 72 65 73 65 72  eleasing a reser
c6310 76 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 65 6c  ved lock */.  el
c6320 73 65 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66  se if (dwFileOff
c6330 73 65 74 4c 6f 77 20 3d 3d 20 52 45 53 45 52 56  setLow == RESERV
c6340 45 44 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62  ED_BYTE && nNumb
c6350 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63  erOfBytesToUnloc
c6360 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20  kLow == 1){.    
c6370 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c  if (pFile->local
c6380 2e 62 52 65 73 65 72 76 65 64 29 20 7b 0a 20 20  .bReserved) {.  
c6390 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c      pFile->local
c63a0 2e 62 52 65 73 65 72 76 65 64 20 3d 20 46 41 4c  .bReserved = FAL
c63b0 53 45 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  SE;.      pFile-
c63c0 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76  >shared->bReserv
c63d0 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20  ed = FALSE;.    
c63e0 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45    bReturn = TRUE
c63f0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 77  ;.    }.  }..  w
c6400 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65  inceMutexRelease
c6410 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b  (pFile->hMutex);
c6420 0a 20 20 72 65 74 75 72 6e 20 62 52 65 74 75 72  .  return bRetur
c6430 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  n;.}../*.** An i
c6440 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
c6450 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28   the LockFileEx(
c6460 29 20 41 50 49 20 6f 66 20 77 69 6e 64 6f 77 73  ) API of windows
c6470 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74   for wince.*/.st
c6480 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 4c  atic BOOL winceL
c6490 6f 63 6b 46 69 6c 65 45 78 28 0a 20 20 48 41 4e  ockFileEx(.  HAN
c64a0 44 4c 45 20 2a 70 68 46 69 6c 65 2c 0a 20 20 44  DLE *phFile,.  D
c64b0 57 4f 52 44 20 64 77 46 6c 61 67 73 2c 0a 20 20  WORD dwFlags,.  
c64c0 44 57 4f 52 44 20 64 77 52 65 73 65 72 76 65 64  DWORD dwReserved
c64d0 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65  ,.  DWORD nNumbe
c64e0 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f  rOfBytesToLockLo
c64f0 77 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62  w,.  DWORD nNumb
c6500 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 48  erOfBytesToLockH
c6510 69 67 68 2c 0a 20 20 4c 50 4f 56 45 52 4c 41 50  igh,.  LPOVERLAP
c6520 50 45 44 20 6c 70 4f 76 65 72 6c 61 70 70 65 64  PED lpOverlapped
c6530 0a 29 7b 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  .){.  /* If the 
c6540 63 61 6c 6c 65 72 20 77 61 6e 74 73 20 61 20 73  caller wants a s
c6550 68 61 72 65 64 20 72 65 61 64 20 6c 6f 63 6b 2c  hared read lock,
c6560 20 66 6f 72 77 61 72 64 20 74 68 69 73 20 63 61   forward this ca
c6570 6c 6c 0a 20 20 2a 2a 20 74 6f 20 77 69 6e 63 65  ll.  ** to wince
c6580 4c 6f 63 6b 46 69 6c 65 20 2a 2f 0a 20 20 69 66  LockFile */.  if
c6590 20 28 6c 70 4f 76 65 72 6c 61 70 70 65 64 2d 3e   (lpOverlapped->
c65a0 4f 66 66 73 65 74 20 3d 3d 20 53 48 41 52 45 44  Offset == SHARED
c65b0 5f 46 49 52 53 54 20 26 26 0a 20 20 20 20 20 20  _FIRST &&.      
c65c0 64 77 46 6c 61 67 73 20 3d 3d 20 31 20 26 26 0a  dwFlags == 1 &&.
c65d0 20 20 20 20 20 20 6e 4e 75 6d 62 65 72 4f 66 42        nNumberOfB
c65e0 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d  ytesToLockLow ==
c65f0 20 53 48 41 52 45 44 5f 53 49 5a 45 29 7b 0a 20   SHARED_SIZE){. 
c6600 20 20 20 72 65 74 75 72 6e 20 77 69 6e 63 65 4c     return winceL
c6610 6f 63 6b 46 69 6c 65 28 70 68 46 69 6c 65 2c 20  ockFile(phFile, 
c6620 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c  SHARED_FIRST, 0,
c6630 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65   1, 0);.  }.  re
c6640 74 75 72 6e 20 46 41 4c 53 45 3b 0a 7d 0a 2f 2a  turn FALSE;.}./*
c6650 0a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 73  .** End of the s
c6660 70 65 63 69 61 6c 20 63 6f 64 65 20 66 6f 72 20  pecial code for 
c6670 77 69 6e 63 65 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  wince.**********
c6680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c66a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c66b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c66c0 2a 2a 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 4f  ***/.#endif /* O
c66d0 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a  S_WINCE */../***
c66e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c66f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
c6730 65 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20  e next group of 
c6740 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65  routines impleme
c6750 6e 74 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f  nt the I/O metho
c6760 64 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20  ds specified.** 
c6770 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  by the sqlite3_i
c6780 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  o_methods object
c6790 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**************
c67a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c67b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c67c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c67d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c67e0 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61  /../*.** Close a
c67f0 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20   file..**.** It 
c6800 69 73 20 72 65 70 6f 72 74 65 64 20 74 68 61 74  is reported that
c6810 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 63   an attempt to c
c6820 6c 6f 73 65 20 61 20 68 61 6e 64 6c 65 20 6d 69  lose a handle mi
c6830 67 68 74 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a  ght sometimes.**
c6840 20 66 61 69 6c 2e 20 20 54 68 69 73 20 69 73 20   fail.  This is 
c6850 61 20 76 65 72 79 20 75 6e 72 65 61 73 6f 6e 61  a very unreasona
c6860 62 6c 65 20 72 65 73 75 6c 74 2c 20 62 75 74 20  ble result, but 
c6870 77 69 6e 64 6f 77 73 20 69 73 20 6e 6f 74 6f 72  windows is notor
c6880 69 6f 75 73 0a 2a 2a 20 66 6f 72 20 62 65 69 6e  ious.** for bein
c6890 67 20 75 6e 72 65 61 73 6f 6e 61 62 6c 65 20 73  g unreasonable s
c68a0 6f 20 49 20 64 6f 20 6e 6f 74 20 64 6f 75 62 74  o I do not doubt
c68b0 20 74 68 61 74 20 69 74 20 6d 69 67 68 74 20 68   that it might h
c68c0 61 70 70 65 6e 2e 20 20 49 66 0a 2a 2a 20 74 68  appen.  If.** th
c68d0 65 20 63 6c 6f 73 65 20 66 61 69 6c 73 2c 20 77  e close fails, w
c68e0 65 20 70 61 75 73 65 20 66 6f 72 20 31 30 30 20  e pause for 100 
c68f0 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64  milliseconds and
c6900 20 74 72 79 20 61 67 61 69 6e 2e 20 20 41 73 0a   try again.  As.
c6910 2a 2a 20 6d 61 6e 79 20 61 73 20 4d 58 5f 43 4c  ** many as MX_CL
c6920 4f 53 45 5f 41 54 54 45 4d 50 54 20 61 74 74 65  OSE_ATTEMPT atte
c6930 6d 70 74 73 20 74 6f 20 63 6c 6f 73 65 20 74 68  mpts to close th
c6940 65 20 68 61 6e 64 6c 65 20 61 72 65 20 6d 61 64  e handle are mad
c6950 65 20 62 65 66 6f 72 65 0a 2a 2a 20 67 69 76 69  e before.** givi
c6960 6e 67 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e  ng up and return
c6970 69 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f  ing an error..*/
c6980 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43 4c 4f 53  .#define MX_CLOS
c6990 45 5f 41 54 54 45 4d 50 54 20 33 0a 73 74 61 74  E_ATTEMPT 3.stat
c69a0 69 63 20 69 6e 74 20 77 69 6e 43 6c 6f 73 65 28  ic int winClose(
c69b0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
c69c0 29 7b 0a 20 20 69 6e 74 20 72 63 2c 20 63 6e 74  ){.  int rc, cnt
c69d0 20 3d 20 30 3b 0a 20 20 77 69 6e 46 69 6c 65 20   = 0;.  winFile 
c69e0 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c  *pFile = (winFil
c69f0 65 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45  e*)id;.  OSTRACE
c6a00 32 28 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20  2("CLOSE %d\n", 
c6a10 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 64 6f 7b  pFile->h);.  do{
c6a20 0a 20 20 20 20 72 63 20 3d 20 43 6c 6f 73 65 48  .    rc = CloseH
c6a30 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 29 3b  andle(pFile->h);
c6a40 0a 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 30  .  }while( rc==0
c6a50 20 26 26 20 63 6e 74 2b 2b 20 3c 20 4d 58 5f 43   && cnt++ < MX_C
c6a60 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20 26 26 20  LOSE_ATTEMPT && 
c6a70 28 53 6c 65 65 70 28 31 30 30 29 2c 20 31 29 20  (Sleep(100), 1) 
c6a80 29 3b 0a 23 69 66 20 4f 53 5f 57 49 4e 43 45 0a  );.#if OS_WINCE.
c6a90 23 64 65 66 69 6e 65 20 57 49 4e 43 45 5f 44 45  #define WINCE_DE
c6aa0 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 20  LETION_ATTEMPTS 
c6ab0 33 0a 20 20 77 69 6e 63 65 44 65 73 74 72 6f 79  3.  winceDestroy
c6ac0 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 69  Lock(pFile);.  i
c6ad0 66 28 20 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74  f( pFile->zDelet
c6ae0 65 4f 6e 43 6c 6f 73 65 20 29 7b 0a 20 20 20 20  eOnClose ){.    
c6af0 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20  int cnt = 0;.   
c6b00 20 77 68 69 6c 65 28 0a 20 20 20 20 20 20 20 20   while(.        
c6b10 20 20 20 44 65 6c 65 74 65 46 69 6c 65 57 28 70     DeleteFileW(p
c6b20 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43  File->zDeleteOnC
c6b30 6c 6f 73 65 29 3d 3d 30 0a 20 20 20 20 20 20 20  lose)==0.       
c6b40 20 26 26 20 47 65 74 46 69 6c 65 41 74 74 72 69   && GetFileAttri
c6b50 62 75 74 65 73 57 28 70 46 69 6c 65 2d 3e 7a 44  butesW(pFile->zD
c6b60 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 21 3d 30  eleteOnClose)!=0
c6b70 78 66 66 66 66 66 66 66 66 20 0a 20 20 20 20 20  xffffffff .     
c6b80 20 20 20 26 26 20 63 6e 74 2b 2b 20 3c 20 57 49     && cnt++ < WI
c6b90 4e 43 45 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54  NCE_DELETION_ATT
c6ba0 45 4d 50 54 53 0a 20 20 20 20 29 7b 0a 20 20 20  EMPTS.    ){.   
c6bb0 20 20 20 20 53 6c 65 65 70 28 31 30 30 29 3b 20      Sleep(100); 
c6bc0 20 2f 2a 20 57 61 69 74 20 61 20 6c 69 74 74 6c   /* Wait a littl
c6bd0 65 20 62 65 66 6f 72 65 20 74 72 79 69 6e 67 20  e before trying 
c6be0 61 67 61 69 6e 20 2a 2f 0a 20 20 20 20 7d 0a 20  again */.    }. 
c6bf0 20 20 20 66 72 65 65 28 70 46 69 6c 65 2d 3e 7a     free(pFile->z
c6c00 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 3b 0a  DeleteOnClose);.
c6c10 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 4f 70 65    }.#endif.  Ope
c6c20 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20  nCounter(-1);.  
c6c30 72 65 74 75 72 6e 20 72 63 20 3f 20 53 51 4c 49  return rc ? SQLI
c6c40 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49  TE_OK : SQLITE_I
c6c50 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  OERR;.}../*.** S
c6c60 6f 6d 65 20 6d 69 63 72 6f 73 6f 66 74 20 63 6f  ome microsoft co
c6c70 6d 70 69 6c 65 72 73 20 6c 61 63 6b 20 74 68 69  mpilers lack thi
c6c80 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f  s definition..*/
c6c90 0a 23 69 66 6e 64 65 66 20 49 4e 56 41 4c 49 44  .#ifndef INVALID
c6ca0 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45  _SET_FILE_POINTE
c6cb0 52 0a 23 20 64 65 66 69 6e 65 20 49 4e 56 41 4c  R.# define INVAL
c6cc0 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e  ID_SET_FILE_POIN
c6cd0 54 45 52 20 28 28 44 57 4f 52 44 29 2d 31 29 0a  TER ((DWORD)-1).
c6ce0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65  #endif../*.** Re
c6cf0 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61 20 66  ad data from a f
c6d00 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 66 66 65  ile into a buffe
c6d10 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54  r.  Return SQLIT
c6d20 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62  E_OK if all.** b
c6d30 79 74 65 73 20 77 65 72 65 20 72 65 61 64 20 73  ytes were read s
c6d40 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6e 64 20  uccessfully and 
c6d50 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 69 66 20  SQLITE_IOERR if 
c6d60 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a  anything goes.**
c6d70 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69   wrong..*/.stati
c6d80 63 20 69 6e 74 20 77 69 6e 52 65 61 64 28 0a 20  c int winRead(. 
c6d90 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69   sqlite3_file *i
c6da0 64 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  d,          /* F
c6db0 69 6c 65 20 74 6f 20 72 65 61 64 20 66 72 6f 6d  ile to read from
c6dc0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 66   */.  void *pBuf
c6dd0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
c6de0 20 2f 2a 20 57 72 69 74 65 20 63 6f 6e 74 65 6e   /* Write conten
c6df0 74 20 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66  t into this buff
c6e00 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c  er */.  int amt,
c6e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6e20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
c6e30 62 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f  bytes to read */
c6e40 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
c6e50 20 6f 66 66 73 65 74 20 20 20 20 20 20 20 2f 2a   offset       /*
c6e60 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61   Begin reading a
c6e70 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f  t this offset */
c6e80 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 72  .){.  LONG upper
c6e90 42 69 74 73 20 3d 20 28 6f 66 66 73 65 74 3e 3e  Bits = (offset>>
c6ea0 33 32 29 20 26 20 30 78 37 66 66 66 66 66 66 66  32) & 0x7fffffff
c6eb0 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69  ;.  LONG lowerBi
c6ec0 74 73 20 3d 20 6f 66 66 73 65 74 20 26 20 30 78  ts = offset & 0x
c6ed0 66 66 66 66 66 66 66 66 3b 0a 20 20 44 57 4f 52  ffffffff;.  DWOR
c6ee0 44 20 72 63 3b 0a 20 20 44 57 4f 52 44 20 67 6f  D rc;.  DWORD go
c6ef0 74 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46  t;.  winFile *pF
c6f00 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29  ile = (winFile*)
c6f10 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64  id;.  assert( id
c6f20 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74  !=0 );.  Simulat
c6f30 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20  eIOError(return 
c6f40 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41  SQLITE_IOERR_REA
c6f50 44 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22  D);.  OSTRACE3("
c6f60 52 45 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c  READ %d lock=%d\
c6f70 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46  n", pFile->h, pF
c6f80 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a  ile->locktype);.
c6f90 20 20 72 63 20 3d 20 53 65 74 46 69 6c 65 50 6f    rc = SetFilePo
c6fa0 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c 20  inter(pFile->h, 
c6fb0 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 70 70 65  lowerBits, &uppe
c6fc0 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 47 49  rBits, FILE_BEGI
c6fd0 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49 4e  N);.  if( rc==IN
c6fe0 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50  VALID_SET_FILE_P
c6ff0 4f 49 4e 54 45 52 20 26 26 20 47 65 74 4c 61 73  OINTER && GetLas
c7000 74 45 72 72 6f 72 28 29 21 3d 4e 4f 5f 45 52 52  tError()!=NO_ERR
c7010 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OR ){.    return
c7020 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20   SQLITE_FULL;.  
c7030 7d 0a 20 20 69 66 28 20 21 52 65 61 64 46 69 6c  }.  if( !ReadFil
c7040 65 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66  e(pFile->h, pBuf
c7050 2c 20 61 6d 74 2c 20 26 67 6f 74 2c 20 30 29 20  , amt, &got, 0) 
c7060 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
c7070 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b  LITE_IOERR_READ;
c7080 0a 20 20 7d 0a 20 20 69 66 28 20 67 6f 74 3d 3d  .  }.  if( got==
c7090 28 44 57 4f 52 44 29 61 6d 74 20 29 7b 0a 20 20  (DWORD)amt ){.  
c70a0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
c70b0 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  OK;.  }else{.   
c70c0 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a   memset(&((char*
c70d0 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20  )pBuf)[got], 0, 
c70e0 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65  amt-got);.    re
c70f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
c7100 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20  R_SHORT_READ;.  
c7110 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  }.}../*.** Write
c7120 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66   data from a buf
c7130 66 65 72 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e  fer into a file.
c7140 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    Return SQLITE_
c7150 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a  OK on success.**
c7160 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65   or some other e
c7170 72 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61 69  rror code on fai
c7180 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lure..*/.static 
c7190 69 6e 74 20 77 69 6e 57 72 69 74 65 28 0a 20 20  int winWrite(.  
c71a0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
c71b0 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c  ,         /* Fil
c71c0 65 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20  e to write into 
c71d0 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
c71e0 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 2f  *pBuf,         /
c71f0 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f 20 62  * The bytes to b
c7200 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69  e written */.  i
c7210 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20  nt amt,         
c7220 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
c7230 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77  er of bytes to w
c7240 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  rite */.  sqlite
c7250 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20 20  3_int64 offset  
c7260 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e      /* Offset in
c7270 74 6f 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62  to the file to b
c7280 65 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20  egin writing at 
c7290 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70  */.){.  LONG upp
c72a0 65 72 42 69 74 73 20 3d 20 28 6f 66 66 73 65 74  erBits = (offset
c72b0 3e 3e 33 32 29 20 26 20 30 78 37 66 66 66 66 66  >>32) & 0x7fffff
c72c0 66 66 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72  ff;.  LONG lower
c72d0 42 69 74 73 20 3d 20 6f 66 66 73 65 74 20 26 20  Bits = offset & 
c72e0 30 78 66 66 66 66 66 66 66 66 3b 0a 20 20 44 57  0xffffffff;.  DW
c72f0 4f 52 44 20 72 63 3b 0a 20 20 44 57 4f 52 44 20  ORD rc;.  DWORD 
c7300 77 72 6f 74 65 3b 0a 20 20 77 69 6e 46 69 6c 65  wrote;.  winFile
c7310 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69   *pFile = (winFi
c7320 6c 65 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74  le*)id;.  assert
c7330 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d  ( id!=0 );.  Sim
c7340 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74  ulateIOError(ret
c7350 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
c7360 5f 57 52 49 54 45 29 3b 0a 20 20 53 69 6d 75 6c  _WRITE);.  Simul
c7370 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72  ateDiskfullError
c7380 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46  (return SQLITE_F
c7390 55 4c 4c 29 3b 0a 20 20 4f 53 54 52 41 43 45 33  ULL);.  OSTRACE3
c73a0 28 22 57 52 49 54 45 20 25 64 20 6c 6f 63 6b 3d  ("WRITE %d lock=
c73b0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
c73c0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
c73d0 29 3b 0a 20 20 72 63 20 3d 20 53 65 74 46 69 6c  );.  rc = SetFil
c73e0 65 50 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e  ePointer(pFile->
c73f0 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75  h, lowerBits, &u
c7400 70 70 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42  pperBits, FILE_B
c7410 45 47 49 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d  EGIN);.  if( rc=
c7420 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c  =INVALID_SET_FIL
c7430 45 5f 50 4f 49 4e 54 45 52 20 26 26 20 47 65 74  E_POINTER && Get
c7440 4c 61 73 74 45 72 72 6f 72 28 29 21 3d 4e 4f 5f  LastError()!=NO_
c7450 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 74  ERROR ){.    ret
c7460 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b  urn SQLITE_FULL;
c7470 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61  .  }.  assert( a
c7480 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28  mt>0 );.  while(
c7490 0a 20 20 20 20 20 61 6d 74 3e 30 0a 20 20 20 20  .     amt>0.    
c74a0 20 26 26 20 28 72 63 20 3d 20 57 72 69 74 65 46   && (rc = WriteF
c74b0 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42  ile(pFile->h, pB
c74c0 75 66 2c 20 61 6d 74 2c 20 26 77 72 6f 74 65 2c  uf, amt, &wrote,
c74d0 20 30 29 29 21 3d 30 0a 20 20 20 20 20 26 26 20   0))!=0.     && 
c74e0 77 72 6f 74 65 3e 30 0a 20 20 29 7b 0a 20 20 20  wrote>0.  ){.   
c74f0 20 61 6d 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20   amt -= wrote;. 
c7500 20 20 20 70 42 75 66 20 3d 20 26 28 28 63 68 61     pBuf = &((cha
c7510 72 2a 29 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b  r*)pBuf)[wrote];
c7520 0a 20 20 7d 0a 20 20 69 66 28 20 21 72 63 20 7c  .  }.  if( !rc |
c7530 7c 20 61 6d 74 3e 28 69 6e 74 29 77 72 6f 74 65  | amt>(int)wrote
c7540 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
c7550 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a  QLITE_FULL;.  }.
c7560 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
c7570 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75  OK;.}../*.** Tru
c7580 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69  ncate an open fi
c7590 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 69 65  le to a specifie
c75a0 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63  d size.*/.static
c75b0 20 69 6e 74 20 77 69 6e 54 72 75 6e 63 61 74 65   int winTruncate
c75c0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
c75d0 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  d, sqlite3_int64
c75e0 20 6e 42 79 74 65 29 7b 0a 20 20 4c 4f 4e 47 20   nByte){.  LONG 
c75f0 75 70 70 65 72 42 69 74 73 20 3d 20 28 6e 42 79  upperBits = (nBy
c7600 74 65 3e 3e 33 32 29 20 26 20 30 78 37 66 66 66  te>>32) & 0x7fff
c7610 66 66 66 66 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77  ffff;.  LONG low
c7620 65 72 42 69 74 73 20 3d 20 6e 42 79 74 65 20 26  erBits = nByte &
c7630 20 30 78 66 66 66 66 66 66 66 66 3b 0a 20 20 77   0xffffffff;.  w
c7640 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  inFile *pFile = 
c7650 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20  (winFile*)id;.  
c7660 4f 53 54 52 41 43 45 33 28 22 54 52 55 4e 43 41  OSTRACE3("TRUNCA
c7670 54 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70  TE %d %lld\n", p
c7680 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 29 3b  File->h, nByte);
c7690 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
c76a0 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45  or(return SQLITE
c76b0 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 29  _IOERR_TRUNCATE)
c76c0 3b 0a 20 20 53 65 74 46 69 6c 65 50 6f 69 6e 74  ;.  SetFilePoint
c76d0 65 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77  er(pFile->h, low
c76e0 65 72 42 69 74 73 2c 20 26 75 70 70 65 72 42 69  erBits, &upperBi
c76f0 74 73 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29 3b  ts, FILE_BEGIN);
c7700 0a 20 20 53 65 74 45 6e 64 4f 66 46 69 6c 65 28  .  SetEndOfFile(
c7710 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 72 65 74  pFile->h);.  ret
c7720 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
c7730 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
c7740 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74  TEST./*.** Count
c7750 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66   the number of f
c7760 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72  ullsyncs and nor
c7770 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73  mal syncs.  This
c7780 20 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74   is used to test
c7790 0a 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61  .** that syncs a
c77a0 6e 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65  nd fullsyncs are
c77b0 20 6f 63 63 75 72 69 6e 67 20 61 74 20 74 68 65   occuring at the
c77c0 20 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f   right times..*/
c77d0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
c77e0 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75  sqlite3_sync_cou
c77f0 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41  nt = 0;.SQLITE_A
c7800 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66  PI int sqlite3_f
c7810 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20  ullsync_count = 
c7820 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  0;.#endif../*.**
c7830 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 77   Make sure all w
c7840 72 69 74 65 73 20 74 6f 20 61 20 70 61 72 74 69  rites to a parti
c7850 63 75 6c 61 72 20 66 69 6c 65 20 61 72 65 20 63  cular file are c
c7860 6f 6d 6d 69 74 74 65 64 20 74 6f 20 64 69 73 6b  ommitted to disk
c7870 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
c7880 77 69 6e 53 79 6e 63 28 73 71 6c 69 74 65 33 5f  winSync(sqlite3_
c7890 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c  file *id, int fl
c78a0 61 67 73 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20  ags){.  winFile 
c78b0 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c  *pFile = (winFil
c78c0 65 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45  e*)id;.  OSTRACE
c78d0 33 28 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b 3d  3("SYNC %d lock=
c78e0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
c78f0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
c7900 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  );.#ifdef SQLITE
c7910 5f 54 45 53 54 0a 20 20 69 66 28 20 66 6c 61 67  _TEST.  if( flag
c7920 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f  s & SQLITE_SYNC_
c7930 46 55 4c 4c 20 29 7b 0a 20 20 20 20 73 71 6c 69  FULL ){.    sqli
c7940 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75  te3_fullsync_cou
c7950 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  nt++;.  }.  sqli
c7960 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b  te3_sync_count++
c7970 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 46  ;.#endif.  if( F
c7980 6c 75 73 68 46 69 6c 65 42 75 66 66 65 72 73 28  lushFileBuffers(
c7990 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 20  pFile->h) ){.   
c79a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
c79b0 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  K;.  }else{.    
c79c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
c79d0 45 52 52 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ERR;.  }.}../*.*
c79e0 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20  * Determine the 
c79f0 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20  current size of 
c7a00 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a  a file in bytes.
c7a10 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
c7a20 6e 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65  nFileSize(sqlite
c7a30 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69  3_file *id, sqli
c7a40 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65  te3_int64 *pSize
c7a50 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46  ){.  winFile *pF
c7a60 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29  ile = (winFile*)
c7a70 69 64 3b 0a 20 20 44 57 4f 52 44 20 75 70 70 65  id;.  DWORD uppe
c7a80 72 42 69 74 73 2c 20 6c 6f 77 65 72 42 69 74 73  rBits, lowerBits
c7a90 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
c7aa0 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54  ror(return SQLIT
c7ab0 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 29 3b 0a  E_IOERR_FSTAT);.
c7ac0 20 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 47 65    lowerBits = Ge
c7ad0 74 46 69 6c 65 53 69 7a 65 28 70 46 69 6c 65 2d  tFileSize(pFile-
c7ae0 3e 68 2c 20 26 75 70 70 65 72 42 69 74 73 29 3b  >h, &upperBits);
c7af0 0a 20 20 2a 70 53 69 7a 65 20 3d 20 28 28 28 73  .  *pSize = (((s
c7b00 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 75 70 70  qlite3_int64)upp
c7b10 65 72 42 69 74 73 29 3c 3c 33 32 29 20 2b 20 6c  erBits)<<32) + l
c7b20 6f 77 65 72 42 69 74 73 3b 0a 20 20 72 65 74 75  owerBits;.  retu
c7b30 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
c7b40 0a 2f 2a 0a 2a 2a 20 4c 4f 43 4b 46 49 4c 45 5f  ./*.** LOCKFILE_
c7b50 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59  FAIL_IMMEDIATELY
c7b60 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 6f 6e   is undefined on
c7b70 20 73 6f 6d 65 20 57 69 6e 64 6f 77 73 20 73 79   some Windows sy
c7b80 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65  stems..*/.#ifnde
c7b90 66 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f  f LOCKFILE_FAIL_
c7ba0 49 4d 4d 45 44 49 41 54 45 4c 59 0a 23 20 64 65  IMMEDIATELY.# de
c7bb0 66 69 6e 65 20 4c 4f 43 4b 46 49 4c 45 5f 46 41  fine LOCKFILE_FA
c7bc0 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 20 31  IL_IMMEDIATELY 1
c7bd0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41  .#endif../*.** A
c7be0 63 71 75 69 72 65 20 61 20 72 65 61 64 65 72 20  cquire a reader 
c7bf0 6c 6f 63 6b 2e 0a 2a 2a 20 44 69 66 66 65 72 65  lock..** Differe
c7c00 6e 74 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20  nt API routines 
c7c10 61 72 65 20 63 61 6c 6c 65 64 20 64 65 70 65 6e  are called depen
c7c20 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 20  ding on whether 
c7c30 6f 72 20 6e 6f 74 20 74 68 69 73 0a 2a 2a 20 69  or not this.** i
c7c40 73 20 57 69 6e 39 35 20 6f 72 20 57 69 6e 4e 54  s Win95 or WinNT
c7c50 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
c7c60 67 65 74 52 65 61 64 4c 6f 63 6b 28 77 69 6e 46  getReadLock(winF
c7c70 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69  ile *pFile){.  i
c7c80 6e 74 20 72 65 73 3b 0a 20 20 69 66 28 20 69 73  nt res;.  if( is
c7c90 4e 54 28 29 20 29 7b 0a 20 20 20 20 4f 56 45 52  NT() ){.    OVER
c7ca0 4c 41 50 50 45 44 20 6f 76 6c 70 3b 0a 20 20 20  LAPPED ovlp;.   
c7cb0 20 6f 76 6c 70 2e 4f 66 66 73 65 74 20 3d 20 53   ovlp.Offset = S
c7cc0 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20  HARED_FIRST;.   
c7cd0 20 6f 76 6c 70 2e 4f 66 66 73 65 74 48 69 67 68   ovlp.OffsetHigh
c7ce0 20 3d 20 30 3b 0a 20 20 20 20 6f 76 6c 70 2e 68   = 0;.    ovlp.h
c7cf0 45 76 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 72  Event = 0;.    r
c7d00 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 45 78 28  es = LockFileEx(
c7d10 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 46 49  pFile->h, LOCKFI
c7d20 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54  LE_FAIL_IMMEDIAT
c7d30 45 4c 59 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ELY,.           
c7d40 20 20 20 20 20 20 20 20 20 20 30 2c 20 53 48 41            0, SHA
c7d50 52 45 44 5f 53 49 5a 45 2c 20 30 2c 20 26 6f 76  RED_SIZE, 0, &ov
c7d60 6c 70 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  lp);.  }else{.  
c7d70 20 20 69 6e 74 20 6c 6b 3b 0a 20 20 20 20 73 71    int lk;.    sq
c7d80 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73  lite3_randomness
c7d90 28 73 69 7a 65 6f 66 28 6c 6b 29 2c 20 26 6c 6b  (sizeof(lk), &lk
c7da0 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 73 68  );.    pFile->sh
c7db0 61 72 65 64 4c 6f 63 6b 42 79 74 65 20 3d 20 28  aredLockByte = (
c7dc0 6c 6b 20 26 20 30 78 37 66 66 66 66 66 66 66 29  lk & 0x7fffffff)
c7dd0 25 28 53 48 41 52 45 44 5f 53 49 5a 45 20 2d 20  %(SHARED_SIZE - 
c7de0 31 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f  1);.    res = Lo
c7df0 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c  ckFile(pFile->h,
c7e00 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b 70 46   SHARED_FIRST+pF
c7e10 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42  ile->sharedLockB
c7e20 79 74 65 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20  yte, 0, 1, 0);. 
c7e30 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b   }.  return res;
c7e40 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61  .}../*.** Undo a
c7e50 20 72 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61   readlock.*/.sta
c7e60 74 69 63 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65  tic int unlockRe
c7e70 61 64 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a  adLock(winFile *
c7e80 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 65  pFile){.  int re
c7e90 73 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20  s;.  if( isNT() 
c7ea0 29 7b 0a 20 20 20 20 72 65 73 20 3d 20 55 6e 6c  ){.    res = Unl
c7eb0 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68  ockFile(pFile->h
c7ec0 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20  , SHARED_FIRST, 
c7ed0 30 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20  0, SHARED_SIZE, 
c7ee0 30 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  0);.  }else{.   
c7ef0 20 72 65 73 20 3d 20 55 6e 6c 6f 63 6b 46 69 6c   res = UnlockFil
c7f00 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52  e(pFile->h, SHAR
c7f10 45 44 5f 46 49 52 53 54 20 2b 20 70 46 69 6c 65  ED_FIRST + pFile
c7f20 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65  ->sharedLockByte
c7f30 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a  , 0, 1, 0);.  }.
c7f40 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a    return res;.}.
c7f50 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20  ./*.** Lock the 
c7f60 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f  file with the lo
c7f70 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 20  ck specified by 
c7f80 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79  parameter lockty
c7f90 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74  pe - one.** of t
c7fa0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a  he following:.**
c7fb0 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 52  .**     (1) SHAR
c7fc0 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28  ED_LOCK.**     (
c7fd0 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  2) RESERVED_LOCK
c7fe0 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44  .**     (3) PEND
c7ff0 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20  ING_LOCK.**     
c8000 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f  (4) EXCLUSIVE_LO
c8010 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d  CK.**.** Sometim
c8020 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 69  es when requesti
c8030 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74  ng one lock stat
c8040 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f  e, additional lo
c8050 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65  ck states.** are
c8060 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74   inserted in bet
c8070 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69  ween.  The locki
c8080 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e  ng might fail on
c8090 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65   one of the late
c80a0 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73  r.** transitions
c80b0 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63   leaving the loc
c80c0 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 65 6e  k state differen
c80d0 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73  t from what it s
c80e0 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74  tarted but.** st
c80f0 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73  ill short of its
c8100 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c   goal.  The foll
c8110 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77  owing chart show
c8120 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a  s the allowed.**
c8130 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64   transitions and
c8140 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e   the inserted in
c8150 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 65  termediate state
c8160 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f  s:.**.**    UNLO
c8170 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a  CKED -> SHARED.*
c8180 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52  *    SHARED -> R
c8190 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48  ESERVED.**    SH
c81a0 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47  ARED -> (PENDING
c81b0 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a  ) -> EXCLUSIVE.*
c81c0 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e  *    RESERVED ->
c81d0 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58   (PENDING) -> EX
c81e0 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45  CLUSIVE.**    PE
c81f0 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49  NDING -> EXCLUSI
c8200 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  VE.**.** This ro
c8210 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20  utine will only 
c8220 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e  increase a lock.
c8230 20 20 54 68 65 20 77 69 6e 55 6e 6c 6f 63 6b 28    The winUnlock(
c8240 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 72 61  ) routine.** era
c8250 73 65 73 20 61 6c 6c 20 6c 6f 63 6b 73 20 61 74  ses all locks at
c8260 20 6f 6e 63 65 20 61 6e 64 20 72 65 74 75 72 6e   once and return
c8270 73 20 75 73 20 69 6d 6d 65 64 69 61 74 65 6c 79  s us immediately
c8280 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65   to locking leve
c8290 6c 20 30 2e 0a 2a 2a 20 49 74 20 69 73 20 6e 6f  l 0..** It is no
c82a0 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6c 6f  t possible to lo
c82b0 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  wer the locking 
c82c0 6c 65 76 65 6c 20 6f 6e 65 20 73 74 65 70 20 61  level one step a
c82d0 74 20 61 20 74 69 6d 65 2e 20 20 59 6f 75 0a 2a  t a time.  You.*
c82e0 2a 20 6d 75 73 74 20 67 6f 20 73 74 72 61 69 67  * must go straig
c82f0 68 74 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65  ht to locking le
c8300 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63  vel 0..*/.static
c8310 20 69 6e 74 20 77 69 6e 4c 6f 63 6b 28 73 71 6c   int winLock(sql
c8320 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
c8330 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20  nt locktype){.  
c8340 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
c8350 4f 4b 3b 20 20 20 20 2f 2a 20 52 65 74 75 72 6e  OK;    /* Return
c8360 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 62 72 6f   code from subro
c8370 75 74 69 6e 65 73 20 2a 2f 0a 20 20 69 6e 74 20  utines */.  int 
c8380 72 65 73 20 3d 20 31 3b 20 20 20 20 20 20 20 20  res = 1;        
c8390 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20     /* Result of 
c83a0 61 20 77 69 6e 64 6f 77 73 20 6c 6f 63 6b 20 63  a windows lock c
c83b0 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77  all */.  int new
c83c0 4c 6f 63 6b 74 79 70 65 3b 20 20 20 20 20 20 20  Locktype;       
c83d0 2f 2a 20 53 65 74 20 70 46 69 6c 65 2d 3e 6c 6f  /* Set pFile->lo
c83e0 63 6b 74 79 70 65 20 74 6f 20 74 68 69 73 20 76  cktype to this v
c83f0 61 6c 75 65 20 62 65 66 6f 72 65 20 65 78 69 74  alue before exit
c8400 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 67 6f 74  ing */.  int got
c8410 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b  PendingLock = 0;
c8420 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 61 63  /* True if we ac
c8430 71 75 69 72 65 64 20 61 20 50 45 4e 44 49 4e 47  quired a PENDING
c8440 20 6c 6f 63 6b 20 74 68 69 73 20 74 69 6d 65 20   lock this time 
c8450 2a 2f 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46  */.  winFile *pF
c8460 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29  ile = (winFile*)
c8470 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  id;..  assert( p
c8480 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 4f 53 54  File!=0 );.  OST
c8490 52 41 43 45 35 28 22 4c 4f 43 4b 20 25 64 20 25  RACE5("LOCK %d %
c84a0 64 20 77 61 73 20 25 64 28 25 64 29 5c 6e 22 2c  d was %d(%d)\n",
c84b0 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65  .          pFile
c84c0 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70  ->h, locktype, p
c84d0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20  File->locktype, 
c84e0 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63  pFile->sharedLoc
c84f0 6b 42 79 74 65 29 3b 0a 0a 20 20 2f 2a 20 49 66  kByte);..  /* If
c8500 20 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64   there is alread
c8510 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73  y a lock of this
c8520 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65   type or more re
c8530 73 74 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65  strictive on the
c8540 0a 20 20 2a 2a 20 4f 73 46 69 6c 65 2c 20 64 6f  .  ** OsFile, do
c8550 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20   nothing. Don't 
c8560 75 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b  use the end_lock
c8570 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a  : exit path, as.
c8580 20 20 2a 2a 20 73 71 6c 69 74 65 33 4f 73 45 6e    ** sqlite3OsEn
c8590 74 65 72 4d 75 74 65 78 28 29 20 68 61 73 6e 27  terMutex() hasn'
c85a0 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65  t been called ye
c85b0 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46  t..  */.  if( pF
c85c0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c  ile->locktype>=l
c85d0 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72  ocktype ){.    r
c85e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
c85f0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20  .  }..  /* Make 
c8600 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67  sure the locking
c8610 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72   sequence is cor
c8620 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65  rect.  */.  asse
c8630 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  rt( pFile->lockt
c8640 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20  ype!=NO_LOCK || 
c8650 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
c8660 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72  _LOCK );.  asser
c8670 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e  t( locktype!=PEN
c8680 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61  DING_LOCK );.  a
c8690 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21  ssert( locktype!
c86a0 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c  =RESERVED_LOCK |
c86b0 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  | pFile->locktyp
c86c0 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e==SHARED_LOCK )
c86d0 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65  ;..  /* Lock the
c86e0 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79   PENDING_LOCK by
c86f0 74 65 20 69 66 20 77 65 20 6e 65 65 64 20 74 6f  te if we need to
c8700 20 61 63 71 75 69 72 65 20 61 20 50 45 4e 44 49   acquire a PENDI
c8710 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 20 2a 2a 20  NG lock or.  ** 
c8720 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 20 20  a SHARED lock.  
c8730 49 66 20 77 65 20 61 72 65 20 61 63 71 75 69 72  If we are acquir
c8740 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63  ing a SHARED loc
c8750 6b 2c 20 74 68 65 20 61 63 71 75 69 73 69 74 69  k, the acquisiti
c8760 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 50  on of.  ** the P
c8770 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65  ENDING_LOCK byte
c8780 20 69 73 20 74 65 6d 70 6f 72 61 72 79 2e 0a 20   is temporary.. 
c8790 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 6b 74 79 70   */.  newLocktyp
c87a0 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  e = pFile->lockt
c87b0 79 70 65 3b 0a 20 20 69 66 28 20 70 46 69 6c 65  ype;.  if( pFile
c87c0 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c  ->locktype==NO_L
c87d0 4f 43 4b 0a 20 20 20 7c 7c 20 28 6c 6f 63 6b 74  OCK.   || (lockt
c87e0 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c  ype==EXCLUSIVE_L
c87f0 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f  OCK && pFile->lo
c8800 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44  cktype==RESERVED
c8810 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20 20 20  _LOCK).  ){.    
c8820 69 6e 74 20 63 6e 74 20 3d 20 33 3b 0a 20 20 20  int cnt = 3;.   
c8830 20 77 68 69 6c 65 28 20 63 6e 74 2d 2d 3e 30 20   while( cnt-->0 
c8840 26 26 20 28 72 65 73 20 3d 20 4c 6f 63 6b 46 69  && (res = LockFi
c8850 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45 4e  le(pFile->h, PEN
c8860 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31 2c  DING_BYTE, 0, 1,
c8870 20 30 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20   0))==0 ){.     
c8880 20 2f 2a 20 54 72 79 20 33 20 74 69 6d 65 73 20   /* Try 3 times 
c8890 74 6f 20 67 65 74 20 74 68 65 20 70 65 6e 64 69  to get the pendi
c88a0 6e 67 20 6c 6f 63 6b 2e 20 20 54 68 65 20 70 65  ng lock.  The pe
c88b0 6e 64 69 6e 67 20 6c 6f 63 6b 20 6d 69 67 68 74  nding lock might
c88c0 20 62 65 0a 20 20 20 20 20 20 2a 2a 20 68 65 6c   be.      ** hel
c88d0 64 20 62 79 20 61 6e 6f 74 68 65 72 20 72 65 61  d by another rea
c88e0 64 65 72 20 70 72 6f 63 65 73 73 20 77 68 6f 20  der process who 
c88f0 77 69 6c 6c 20 72 65 6c 65 61 73 65 20 69 74 20  will release it 
c8900 6d 6f 6d 65 6e 74 61 72 69 6c 79 2e 0a 20 20 20  momentarily..   
c8910 20 20 20 2a 2f 0a 20 20 20 20 20 20 4f 53 54 52     */.      OSTR
c8920 41 43 45 32 28 22 63 6f 75 6c 64 20 6e 6f 74 20  ACE2("could not 
c8930 67 65 74 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f  get a PENDING lo
c8940 63 6b 2e 20 63 6e 74 3d 25 64 5c 6e 22 2c 20 63  ck. cnt=%d\n", c
c8950 6e 74 29 3b 0a 20 20 20 20 20 20 53 6c 65 65 70  nt);.      Sleep
c8960 28 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67  (1);.    }.    g
c8970 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20  otPendingLock = 
c8980 72 65 73 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41  res;.  }..  /* A
c8990 63 71 75 69 72 65 20 61 20 73 68 61 72 65 64 20  cquire a shared 
c89a0 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20  lock.  */.  if( 
c89b0 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
c89c0 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a  _LOCK && res ){.
c89d0 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c      assert( pFil
c89e0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f  e->locktype==NO_
c89f0 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20  LOCK );.    res 
c8a00 3d 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46  = getReadLock(pF
c8a10 69 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 72 65  ile);.    if( re
c8a20 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f  s ){.      newLo
c8a30 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f  cktype = SHARED_
c8a40 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  LOCK;.    }.  }.
c8a50 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20  .  /* Acquire a 
c8a60 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 0a 20 20  RESERVED lock.  
c8a70 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70  */.  if( locktyp
c8a80 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e==RESERVED_LOCK
c8a90 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 61   && res ){.    a
c8aa0 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f  ssert( pFile->lo
c8ab0 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
c8ac0 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d  OCK );.    res =
c8ad0 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d   LockFile(pFile-
c8ae0 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54  >h, RESERVED_BYT
c8af0 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20  E, 0, 1, 0);.   
c8b00 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20   if( res ){.    
c8b10 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20    newLocktype = 
c8b20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3b 0a 20  RESERVED_LOCK;. 
c8b30 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41     }.  }..  /* A
c8b40 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47  cquire a PENDING
c8b50 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28   lock.  */.  if(
c8b60 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55   locktype==EXCLU
c8b70 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73  SIVE_LOCK && res
c8b80 20 29 7b 0a 20 20 20 20 6e 65 77 4c 6f 63 6b 74   ){.    newLockt
c8b90 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f  ype = PENDING_LO
c8ba0 43 4b 3b 0a 20 20 20 20 67 6f 74 50 65 6e 64 69  CK;.    gotPendi
c8bb0 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 7d 0a  ngLock = 0;.  }.
c8bc0 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 6e  .  /* Acquire an
c8bd0 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 0a   EXCLUSIVE lock.
c8be0 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74    */.  if( lockt
c8bf0 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c  ype==EXCLUSIVE_L
c8c00 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20  OCK && res ){.  
c8c10 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d    assert( pFile-
c8c20 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52 45  >locktype>=SHARE
c8c30 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65  D_LOCK );.    re
c8c40 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f  s = unlockReadLo
c8c50 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f  ck(pFile);.    O
c8c60 53 54 52 41 43 45 32 28 22 75 6e 72 65 61 64 6c  STRACE2("unreadl
c8c70 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73  ock = %d\n", res
c8c80 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63  );.    res = Loc
c8c90 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20  kFile(pFile->h, 
c8ca0 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c  SHARED_FIRST, 0,
c8cb0 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29   SHARED_SIZE, 0)
c8cc0 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b  ;.    if( res ){
c8cd0 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79  .      newLockty
c8ce0 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c  pe = EXCLUSIVE_L
c8cf0 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  OCK;.    }else{.
c8d00 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 22        OSTRACE2("
c8d10 65 72 72 6f 72 2d 63 6f 64 65 20 3d 20 25 64 5c  error-code = %d\
c8d20 6e 22 2c 20 47 65 74 4c 61 73 74 45 72 72 6f 72  n", GetLastError
c8d30 28 29 29 3b 0a 20 20 20 20 20 20 67 65 74 52 65  ());.      getRe
c8d40 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20  adLock(pFile);. 
c8d50 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
c8d60 66 20 77 65 20 61 72 65 20 68 6f 6c 64 69 6e 67  f we are holding
c8d70 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20   a PENDING lock 
c8d80 74 68 61 74 20 6f 75 67 68 74 20 74 6f 20 62 65  that ought to be
c8d90 20 72 65 6c 65 61 73 65 64 2c 20 74 68 65 6e 0a   released, then.
c8da0 20 20 2a 2a 20 72 65 6c 65 61 73 65 20 69 74 20    ** release it 
c8db0 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  now..  */.  if( 
c8dc0 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 26  gotPendingLock &
c8dd0 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  & locktype==SHAR
c8de0 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55  ED_LOCK ){.    U
c8df0 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d  nlockFile(pFile-
c8e00 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  >h, PENDING_BYTE
c8e10 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a  , 0, 1, 0);.  }.
c8e20 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65  .  /* Update the
c8e30 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 6c 6f   state of the lo
c8e40 63 6b 20 68 61 73 20 68 65 6c 64 20 69 6e 20 74  ck has held in t
c8e50 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
c8e60 6f 72 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 74  or then.  ** ret
c8e70 75 72 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69  urn the appropri
c8e80 61 74 65 20 72 65 73 75 6c 74 20 63 6f 64 65 2e  ate result code.
c8e90 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 65 73 20  .  */.  if( res 
c8ea0 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
c8eb0 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a  TE_OK;.  }else{.
c8ec0 20 20 20 20 4f 53 54 52 41 43 45 34 28 22 4c 4f      OSTRACE4("LO
c8ed0 43 4b 20 46 41 49 4c 45 44 20 25 64 20 74 72 79  CK FAILED %d try
c8ee0 69 6e 67 20 66 6f 72 20 25 64 20 62 75 74 20 67  ing for %d but g
c8ef0 6f 74 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  ot %d\n", pFile-
c8f00 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c  >h,.           l
c8f10 6f 63 6b 74 79 70 65 2c 20 6e 65 77 4c 6f 63 6b  ocktype, newLock
c8f20 74 79 70 65 29 3b 0a 20 20 20 20 72 63 20 3d 20  type);.    rc = 
c8f30 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d  SQLITE_BUSY;.  }
c8f40 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  .  pFile->lockty
c8f50 70 65 20 3d 20 6e 65 77 4c 6f 63 6b 74 79 70 65  pe = newLocktype
c8f60 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
c8f70 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
c8f80 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74  tine checks if t
c8f90 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56  here is a RESERV
c8fa0 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20  ED lock held on 
c8fb0 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  the specified.**
c8fc0 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72   file by this or
c8fd0 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65   any other proce
c8fe0 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f  ss. If such a lo
c8ff0 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65 74 75  ck is held, retu
c9000 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  rn.** non-zero, 
c9010 6f 74 68 65 72 77 69 73 65 20 7a 65 72 6f 2e 0a  otherwise zero..
c9020 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
c9030 6e 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  nCheckReservedLo
c9040 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
c9050 2a 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  *id){.  int rc;.
c9060 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65    winFile *pFile
c9070 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b   = (winFile*)id;
c9080 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
c9090 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 46 69  !=0 );.  if( pFi
c90a0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52 45  le->locktype>=RE
c90b0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SERVED_LOCK ){. 
c90c0 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 4f     rc = 1;.    O
c90d0 53 54 52 41 43 45 33 28 22 54 45 53 54 20 57 52  STRACE3("TEST WR
c90e0 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 6c 6f 63  -LOCK %d %d (loc
c90f0 61 6c 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  al)\n", pFile->h
c9100 2c 20 72 63 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  , rc);.  }else{.
c9110 20 20 20 20 72 63 20 3d 20 4c 6f 63 6b 46 69 6c      rc = LockFil
c9120 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45  e(pFile->h, RESE
c9130 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c  RVED_BYTE, 0, 1,
c9140 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20   0);.    if( rc 
c9150 29 7b 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b 46  ){.      UnlockF
c9160 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45  ile(pFile->h, RE
c9170 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20  SERVED_BYTE, 0, 
c9180 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20  1, 0);.    }.   
c9190 20 72 63 20 3d 20 21 72 63 3b 0a 20 20 20 20 4f   rc = !rc;.    O
c91a0 53 54 52 41 43 45 33 28 22 54 45 53 54 20 57 52  STRACE3("TEST WR
c91b0 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 72 65 6d  -LOCK %d %d (rem
c91c0 6f 74 65 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  ote)\n", pFile->
c91d0 68 2c 20 72 63 29 3b 0a 20 20 7d 0a 20 20 72 65  h, rc);.  }.  re
c91e0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
c91f0 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b  * Lower the lock
c9200 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c  ing level on fil
c9210 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 64 20  e descriptor id 
c9220 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f  to locktype.  lo
c9230 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62  cktype.** must b
c9240 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b  e either NO_LOCK
c9250 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e   or SHARED_LOCK.
c9260 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f  .**.** If the lo
c9270 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74  cking level of t
c9280 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
c9290 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74  or is already at
c92a0 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65   or below.** the
c92b0 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69   requested locki
c92c0 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72  ng level, this r
c92d0 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
c92e0 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 6e  p..**.** It is n
c92f0 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20  ot possible for 
c9300 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20  this routine to 
c9310 66 61 69 6c 20 69 66 20 74 68 65 20 73 65 63 6f  fail if the seco
c9320 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69  nd argument.** i
c9330 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 20 49 66 20 74  s NO_LOCK.  If t
c9340 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
c9350 6e 74 20 69 73 20 53 48 41 52 45 44 5f 4c 4f 43  nt is SHARED_LOC
c9360 4b 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74  K then this rout
c9370 69 6e 65 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74  ine.** might ret
c9380 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
c9390 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.*/.static int 
c93a0 77 69 6e 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65  winUnlock(sqlite
c93b0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
c93c0 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74  locktype){.  int
c93d0 20 74 79 70 65 3b 0a 20 20 77 69 6e 46 69 6c 65   type;.  winFile
c93e0 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69   *pFile = (winFi
c93f0 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 72 63  le*)id;.  int rc
c9400 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
c9410 61 73 73 65 72 74 28 20 70 46 69 6c 65 21 3d 30  assert( pFile!=0
c9420 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f   );.  assert( lo
c9430 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c  cktype<=SHARED_L
c9440 4f 43 4b 20 29 3b 0a 20 20 4f 53 54 52 41 43 45  OCK );.  OSTRACE
c9450 35 28 22 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20  5("UNLOCK %d to 
c9460 25 64 20 77 61 73 20 25 64 28 25 64 29 5c 6e 22  %d was %d(%d)\n"
c9470 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b  , pFile->h, lock
c9480 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 20 20  type,.          
c9490 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c  pFile->locktype,
c94a0 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f   pFile->sharedLo
c94b0 63 6b 42 79 74 65 29 3b 0a 20 20 74 79 70 65 20  ckByte);.  type 
c94c0 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  = pFile->locktyp
c94d0 65 3b 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 45  e;.  if( type>=E
c94e0 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b  XCLUSIVE_LOCK ){
c94f0 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28  .    UnlockFile(
c9500 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44  pFile->h, SHARED
c9510 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 52 45  _FIRST, 0, SHARE
c9520 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 20 20  D_SIZE, 0);.    
c9530 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  if( locktype==SH
c9540 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 21 67 65  ARED_LOCK && !ge
c9550 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29  tReadLock(pFile)
c9560 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69   ){.      /* Thi
c9570 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68  s should never h
c9580 61 70 70 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c  appen.  We shoul
c9590 64 20 61 6c 77 61 79 73 20 62 65 20 61 62 6c 65  d always be able
c95a0 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61   to.      ** rea
c95b0 63 71 75 69 72 65 20 74 68 65 20 72 65 61 64 20  cquire the read 
c95c0 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 72 63  lock */.      rc
c95d0 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
c95e0 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20  UNLOCK;.    }.  
c95f0 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 52 45  }.  if( type>=RE
c9600 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SERVED_LOCK ){. 
c9610 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46     UnlockFile(pF
c9620 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44  ile->h, RESERVED
c9630 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b  _BYTE, 0, 1, 0);
c9640 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63 6b 74  .  }.  if( lockt
c9650 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 26 20  ype==NO_LOCK && 
c9660 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43  type>=SHARED_LOC
c9670 4b 20 29 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b 52  K ){.    unlockR
c9680 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a  eadLock(pFile);.
c9690 20 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d    }.  if( type>=
c96a0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a  PENDING_LOCK ){.
c96b0 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70      UnlockFile(p
c96c0 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47  File->h, PENDING
c96d0 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b  _BYTE, 0, 1, 0);
c96e0 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f  .  }.  pFile->lo
c96f0 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70  cktype = locktyp
c9700 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  e;.  return rc;.
c9710 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f 6c  }../*.** Control
c9720 20 61 6e 64 20 71 75 65 72 79 20 6f 66 20 74 68   and query of th
c9730 65 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64  e open file hand
c9740 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
c9750 74 20 77 69 6e 46 69 6c 65 43 6f 6e 74 72 6f 6c  t winFileControl
c9760 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
c9770 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20  d, int op, void 
c9780 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63 68  *pArg){.  switch
c9790 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65  ( op ){.    case
c97a0 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f   SQLITE_FCNTL_LO
c97b0 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 20  CKSTATE: {.     
c97c0 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28   *(int*)pArg = (
c97d0 28 77 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 6c  (winFile*)id)->l
c97e0 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 72  ocktype;.      r
c97f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
c9800 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
c9810 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
c9820 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
c9830 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a  n the sector siz
c9840 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68  e in bytes of th
c9850 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f  e underlying blo
c9860 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a  ck device for.**
c9870 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 66   the specified f
c9880 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d  ile. This is alm
c9890 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 20 62  ost always 512 b
c98a0 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65  ytes, but may be
c98b0 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73  .** larger for s
c98c0 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a  ome devices..**.
c98d0 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61  ** SQLite code a
c98e0 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63  ssumes this func
c98f0 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c  tion cannot fail
c9900 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65  . It also assume
c9910 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f  s that.** if two
c9920 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 74   files are creat
c9930 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66  ed in the same f
c9940 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63  ile-system direc
c9950 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20  tory (i.e..** a 
c9960 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 73  database and its
c9970 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74   journal file) t
c9980 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 20 73  hat the sector s
c9990 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a  ize will be the.
c99a0 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68  ** same for both
c99b0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
c99c0 77 69 6e 53 65 63 74 6f 72 53 69 7a 65 28 73 71  winSectorSize(sq
c99d0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b  lite3_file *id){
c99e0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
c99f0 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f  _DEFAULT_SECTOR_
c9a00 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  SIZE;.}../*.** R
c9a10 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72 20 6f  eturn a vector o
c9a20 66 20 64 65 76 69 63 65 20 63 68 61 72 61 63 74  f device charact
c9a30 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 74 61  eristics..*/.sta
c9a40 74 69 63 20 69 6e 74 20 77 69 6e 44 65 76 69 63  tic int winDevic
c9a50 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73  eCharacteristics
c9a60 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
c9a70 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  d){.  return 0;.
c9a80 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65  }../*.** This ve
c9a90 63 74 6f 72 20 64 65 66 69 6e 65 73 20 61 6c 6c  ctor defines all
c9aa0 20 74 68 65 20 6d 65 74 68 6f 64 73 20 74 68 61   the methods tha
c9ab0 74 20 63 61 6e 20 6f 70 65 72 61 74 65 20 6f 6e  t can operate on
c9ac0 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66   an.** sqlite3_f
c9ad0 69 6c 65 20 66 6f 72 20 77 69 6e 33 32 2e 0a 2a  ile for win32..*
c9ae0 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73  /.static const s
c9af0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
c9b00 73 20 77 69 6e 49 6f 4d 65 74 68 6f 64 20 3d 20  s winIoMethod = 
c9b10 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20  {.  1,          
c9b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c9b30 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 77   iVersion */.  w
c9b40 69 6e 43 6c 6f 73 65 2c 0a 20 20 77 69 6e 52 65  inClose,.  winRe
c9b50 61 64 2c 0a 20 20 77 69 6e 57 72 69 74 65 2c 0a  ad,.  winWrite,.
c9b60 20 20 77 69 6e 54 72 75 6e 63 61 74 65 2c 0a 20    winTruncate,. 
c9b70 20 77 69 6e 53 79 6e 63 2c 0a 20 20 77 69 6e 46   winSync,.  winF
c9b80 69 6c 65 53 69 7a 65 2c 0a 20 20 77 69 6e 4c 6f  ileSize,.  winLo
c9b90 63 6b 2c 0a 20 20 77 69 6e 55 6e 6c 6f 63 6b 2c  ck,.  winUnlock,
c9ba0 0a 20 20 77 69 6e 43 68 65 63 6b 52 65 73 65 72  .  winCheckReser
c9bb0 76 65 64 4c 6f 63 6b 2c 0a 20 20 77 69 6e 46 69  vedLock,.  winFi
c9bc0 6c 65 43 6f 6e 74 72 6f 6c 2c 0a 20 20 77 69 6e  leControl,.  win
c9bd0 53 65 63 74 6f 72 53 69 7a 65 2c 0a 20 20 77 69  SectorSize,.  wi
c9be0 6e 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  nDeviceCharacter
c9bf0 69 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a  istics.};../****
c9c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9c40 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 72 65 20  *******.** Here 
c9c50 65 6e 64 73 20 74 68 65 20 49 2f 4f 20 6d 65 74  ends the I/O met
c9c60 68 6f 64 73 20 74 68 61 74 20 66 6f 72 6d 20 74  hods that form t
c9c70 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  he sqlite3_io_me
c9c80 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a  thods object..**
c9c90 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 62 6c 6f  .** The next blo
c9ca0 63 6b 20 6f 66 20 63 6f 64 65 20 69 6d 70 6c 65  ck of code imple
c9cb0 6d 65 6e 74 73 20 74 68 65 20 56 46 53 20 6d 65  ments the VFS me
c9cc0 74 68 6f 64 73 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  thods..*********
c9cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9d10 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  ***/../*.** Conv
c9d20 65 72 74 20 61 20 55 54 46 2d 38 20 66 69 6c 65  ert a UTF-8 file
c9d30 6e 61 6d 65 20 69 6e 74 6f 20 77 68 61 74 65 76  name into whatev
c9d40 65 72 20 66 6f 72 6d 20 74 68 65 20 75 6e 64 65  er form the unde
c9d50 72 6c 79 69 6e 67 0a 2a 2a 20 6f 70 65 72 61 74  rlying.** operat
c9d60 69 6e 67 20 73 79 73 74 65 6d 20 77 61 6e 74 73  ing system wants
c9d70 20 66 69 6c 65 6e 61 6d 65 73 20 69 6e 2e 20 20   filenames in.  
c9d80 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68  Space to hold th
c9d90 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73 20 6f  e result.** is o
c9da0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c  btained from mal
c9db0 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20 62 65 20  loc and must be 
c9dc0 66 72 65 65 64 20 62 79 20 74 68 65 20 63 61 6c  freed by the cal
c9dd0 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  ling.** function
c9de0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
c9df0 20 2a 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c   *convertUtf8Fil
c9e00 65 6e 61 6d 65 28 63 6f 6e 73 74 20 63 68 61 72  ename(const char
c9e10 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20   *zFilename){.  
c9e20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64  void *zConverted
c9e30 20 3d 20 30 3b 0a 20 20 69 66 28 20 69 73 4e 54   = 0;.  if( isNT
c9e40 28 29 20 29 7b 0a 20 20 20 20 7a 43 6f 6e 76 65  () ){.    zConve
c9e50 72 74 65 64 20 3d 20 75 74 66 38 54 6f 55 6e 69  rted = utf8ToUni
c9e60 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b  code(zFilename);
c9e70 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 43  .  }else{.    zC
c9e80 6f 6e 76 65 72 74 65 64 20 3d 20 75 74 66 38 54  onverted = utf8T
c9e90 6f 4d 62 63 73 28 7a 46 69 6c 65 6e 61 6d 65 29  oMbcs(zFilename)
c9ea0 3b 0a 20 20 7d 0a 20 20 2f 2a 20 63 61 6c 6c 65  ;.  }.  /* calle
c9eb0 72 20 77 69 6c 6c 20 68 61 6e 64 6c 65 20 6f 75  r will handle ou
c9ec0 74 20 6f 66 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20  t of memory */. 
c9ed0 20 72 65 74 75 72 6e 20 7a 43 6f 6e 76 65 72 74   return zConvert
c9ee0 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65  ed;.}../*.** Ope
c9ef0 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  n a file..*/.sta
c9f00 74 69 63 20 69 6e 74 20 77 69 6e 4f 70 65 6e 28  tic int winOpen(
c9f10 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
c9f20 70 56 66 73 2c 20 20 20 20 20 20 20 20 2f 2a 20  pVfs,        /* 
c9f30 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f  Not used */.  co
c9f40 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
c9f50 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
c9f60 6f 66 20 74 68 65 20 66 69 6c 65 20 28 55 54 46  of the file (UTF
c9f70 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  -8) */.  sqlite3
c9f80 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20  _file *id,      
c9f90 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
c9fa0 53 51 4c 69 74 65 20 66 69 6c 65 20 68 61 6e 64  SQLite file hand
c9fb0 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  le here */.  int
c9fc0 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
c9fd0 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 6d         /* Open m
c9fe0 6f 64 65 20 66 6c 61 67 73 20 2a 2f 0a 20 20 69  ode flags */.  i
c9ff0 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20  nt *pOutFlags   
ca000 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74           /* Stat
ca010 75 73 20 72 65 74 75 72 6e 20 66 6c 61 67 73 20  us return flags 
ca020 2a 2f 0a 29 7b 0a 20 20 48 41 4e 44 4c 45 20 68  */.){.  HANDLE h
ca030 3b 0a 20 20 44 57 4f 52 44 20 64 77 44 65 73 69  ;.  DWORD dwDesi
ca040 72 65 64 41 63 63 65 73 73 3b 0a 20 20 44 57 4f  redAccess;.  DWO
ca050 52 44 20 64 77 53 68 61 72 65 4d 6f 64 65 3b 0a  RD dwShareMode;.
ca060 20 20 44 57 4f 52 44 20 64 77 43 72 65 61 74 69    DWORD dwCreati
ca070 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 3b 0a 20  onDisposition;. 
ca080 20 44 57 4f 52 44 20 64 77 46 6c 61 67 73 41 6e   DWORD dwFlagsAn
ca090 64 41 74 74 72 69 62 75 74 65 73 20 3d 20 30 3b  dAttributes = 0;
ca0a0 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 3b 0a 20  .  int isTemp;. 
ca0b0 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20   winFile *pFile 
ca0c0 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a  = (winFile*)id;.
ca0d0 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74    void *zConvert
ca0e0 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38  ed = convertUtf8
ca0f0 46 69 6c 65 6e 61 6d 65 28 7a 4e 61 6d 65 29 3b  Filename(zName);
ca100 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65  .  if( zConverte
ca110 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  d==0 ){.    retu
ca120 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
ca130 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67  .  }..  if( flag
ca140 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s & SQLITE_OPEN_
ca150 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20  READWRITE ){.   
ca160 20 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73   dwDesiredAccess
ca170 20 3d 20 47 45 4e 45 52 49 43 5f 52 45 41 44 20   = GENERIC_READ 
ca180 7c 20 47 45 4e 45 52 49 43 5f 57 52 49 54 45 3b  | GENERIC_WRITE;
ca190 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77  .  }else{.    dw
ca1a0 44 65 73 69 72 65 64 41 63 63 65 73 73 20 3d 20  DesiredAccess = 
ca1b0 47 45 4e 45 52 49 43 5f 52 45 41 44 3b 0a 20 20  GENERIC_READ;.  
ca1c0 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20  }.  if( flags & 
ca1d0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
ca1e0 54 45 20 29 7b 0a 20 20 20 20 64 77 43 72 65 61  TE ){.    dwCrea
ca1f0 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 20  tionDisposition 
ca200 3d 20 4f 50 45 4e 5f 41 4c 57 41 59 53 3b 0a 20  = OPEN_ALWAYS;. 
ca210 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77 43 72   }else{.    dwCr
ca220 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f  eationDispositio
ca230 6e 20 3d 20 4f 50 45 4e 5f 45 58 49 53 54 49 4e  n = OPEN_EXISTIN
ca240 47 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 61  G;.  }.  if( fla
ca250 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
ca260 5f 4d 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20  _MAIN_DB ){.    
ca270 64 77 53 68 61 72 65 4d 6f 64 65 20 3d 20 46 49  dwShareMode = FI
ca280 4c 45 5f 53 48 41 52 45 5f 52 45 41 44 20 7c 20  LE_SHARE_READ | 
ca290 46 49 4c 45 5f 53 48 41 52 45 5f 57 52 49 54 45  FILE_SHARE_WRITE
ca2a0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64  ;.  }else{.    d
ca2b0 77 53 68 61 72 65 4d 6f 64 65 20 3d 20 30 3b 0a  wShareMode = 0;.
ca2c0 20 20 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 20    }.  if( flags 
ca2d0 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45  & SQLITE_OPEN_DE
ca2e0 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 29 7b 0a 23  LETEONCLOSE ){.#
ca2f0 69 66 20 4f 53 5f 57 49 4e 43 45 0a 20 20 20 20  if OS_WINCE.    
ca300 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62  dwFlagsAndAttrib
ca310 75 74 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 52  utes = FILE_ATTR
ca320 49 42 55 54 45 5f 48 49 44 44 45 4e 3b 0a 23 65  IBUTE_HIDDEN;.#e
ca330 6c 73 65 0a 20 20 20 20 64 77 46 6c 61 67 73 41  lse.    dwFlagsA
ca340 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d 20 46  ndAttributes = F
ca350 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 54 45  ILE_ATTRIBUTE_TE
ca360 4d 50 4f 52 41 52 59 0a 20 20 20 20 20 20 20 20  MPORARY.        
ca370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca380 20 20 20 20 20 20 20 7c 20 46 49 4c 45 5f 41 54         | FILE_AT
ca390 54 52 49 42 55 54 45 5f 48 49 44 44 45 4e 0a 20  TRIBUTE_HIDDEN. 
ca3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20                | 
ca3c0 46 49 4c 45 5f 46 4c 41 47 5f 44 45 4c 45 54 45  FILE_FLAG_DELETE
ca3d0 5f 4f 4e 5f 43 4c 4f 53 45 3b 0a 23 65 6e 64 69  _ON_CLOSE;.#endi
ca3e0 66 0a 20 20 20 20 69 73 54 65 6d 70 20 3d 20 31  f.    isTemp = 1
ca3f0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64  ;.  }else{.    d
ca400 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75  wFlagsAndAttribu
ca410 74 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 49  tes = FILE_ATTRI
ca420 42 55 54 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 20  BUTE_NORMAL;.   
ca430 20 69 73 54 65 6d 70 20 3d 20 30 3b 0a 20 20 7d   isTemp = 0;.  }
ca440 0a 20 20 2f 2a 20 52 65 70 6f 72 74 73 20 66 72  .  /* Reports fr
ca450 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 65 74 20  om the internet 
ca460 61 72 65 20 74 68 61 74 20 70 65 72 66 6f 72 6d  are that perform
ca470 61 6e 63 65 20 69 73 20 61 6c 77 61 79 73 0a 20  ance is always. 
ca480 20 2a 2a 20 62 65 74 74 65 72 20 69 66 20 46 49   ** better if FI
ca490 4c 45 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41  LE_FLAG_RANDOM_A
ca4a0 43 43 45 53 53 20 69 73 20 75 73 65 64 2e 20 20  CCESS is used.  
ca4b0 54 69 63 6b 65 74 20 23 32 36 39 39 2e 20 2a 2f  Ticket #2699. */
ca4c0 0a 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74  .  dwFlagsAndAtt
ca4d0 72 69 62 75 74 65 73 20 7c 3d 20 46 49 4c 45 5f  ributes |= FILE_
ca4e0 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45  FLAG_RANDOM_ACCE
ca4f0 53 53 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29  SS;.  if( isNT()
ca500 20 29 7b 0a 20 20 20 20 68 20 3d 20 43 72 65 61   ){.    h = Crea
ca510 74 65 46 69 6c 65 57 28 28 57 43 48 41 52 2a 29  teFileW((WCHAR*)
ca520 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 20  zConverted,.    
ca530 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65     dwDesiredAcce
ca540 73 73 2c 0a 20 20 20 20 20 20 20 64 77 53 68 61  ss,.       dwSha
ca550 72 65 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20 4e  reMode,.       N
ca560 55 4c 4c 2c 0a 20 20 20 20 20 20 20 64 77 43 72  ULL,.       dwCr
ca570 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f  eationDispositio
ca580 6e 2c 0a 20 20 20 20 20 20 20 64 77 46 6c 61 67  n,.       dwFlag
ca590 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 2c 0a  sAndAttributes,.
ca5a0 20 20 20 20 20 20 20 4e 55 4c 4c 0a 20 20 20 20         NULL.    
ca5b0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 23 69 66 20  );.  }else{.#if 
ca5c0 4f 53 5f 57 49 4e 43 45 0a 20 20 20 20 72 65 74  OS_WINCE.    ret
ca5d0 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
ca5e0 3b 0a 23 65 6c 73 65 0a 20 20 20 20 68 20 3d 20  ;.#else.    h = 
ca5f0 43 72 65 61 74 65 46 69 6c 65 41 28 28 63 68 61  CreateFileA((cha
ca600 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20  r*)zConverted,. 
ca610 20 20 20 20 20 20 64 77 44 65 73 69 72 65 64 41        dwDesiredA
ca620 63 63 65 73 73 2c 0a 20 20 20 20 20 20 20 64 77  ccess,.       dw
ca630 53 68 61 72 65 4d 6f 64 65 2c 0a 20 20 20 20 20  ShareMode,.     
ca640 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 64    NULL,.       d
ca650 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69  wCreationDisposi
ca660 74 69 6f 6e 2c 0a 20 20 20 20 20 20 20 64 77 46  tion,.       dwF
ca670 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65  lagsAndAttribute
ca680 73 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 0a 20  s,.       NULL. 
ca690 20 20 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d     );.#endif.  }
ca6a0 0a 20 20 69 66 28 20 68 3d 3d 49 4e 56 41 4c 49  .  if( h==INVALI
ca6b0 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45 20 29  D_HANDLE_VALUE )
ca6c0 7b 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76  {.    free(zConv
ca6d0 65 72 74 65 64 29 3b 0a 20 20 20 20 69 66 28 20  erted);.    if( 
ca6e0 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
ca6f0 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b  PEN_READWRITE ){
ca700 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 77 69  .      return wi
ca710 6e 4f 70 65 6e 28 30 2c 20 7a 4e 61 6d 65 2c 20  nOpen(0, zName, 
ca720 69 64 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  id, .           
ca730 20 20 28 28 66 6c 61 67 73 7c 53 51 4c 49 54 45    ((flags|SQLITE
ca740 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 26  _OPEN_READONLY)&
ca750 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41  ~SQLITE_OPEN_REA
ca760 44 57 52 49 54 45 29 2c 20 70 4f 75 74 46 6c 61  DWRITE), pOutFla
ca770 67 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  gs);.    }else{.
ca780 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
ca790 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20  ITE_CANTOPEN;.  
ca7a0 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f    }.  }.  if( pO
ca7b0 75 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 69  utFlags ){.    i
ca7c0 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f( flags & SQLIT
ca7d0 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
ca7e0 20 29 7b 0a 20 20 20 20 20 20 2a 70 4f 75 74 46   ){.      *pOutF
ca7f0 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50  lags = SQLITE_OP
ca800 45 4e 5f 52 45 41 44 57 52 49 54 45 3b 0a 20 20  EN_READWRITE;.  
ca810 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a    }else{.      *
ca820 70 4f 75 74 46 6c 61 67 73 20 3d 20 53 51 4c 49  pOutFlags = SQLI
ca830 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
ca840 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65  ;.    }.  }.  me
ca850 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 2c 20 73  mset(pFile, 0, s
ca860 69 7a 65 6f 66 28 2a 70 46 69 6c 65 29 29 3b 0a  izeof(*pFile));.
ca870 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64    pFile->pMethod
ca880 20 3d 20 26 77 69 6e 49 6f 4d 65 74 68 6f 64 3b   = &winIoMethod;
ca890 0a 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 68 3b  .  pFile->h = h;
ca8a0 0a 23 69 66 20 4f 53 5f 57 49 4e 43 45 0a 20 20  .#if OS_WINCE.  
ca8b0 69 66 28 20 28 66 6c 61 67 73 20 26 20 28 53 51  if( (flags & (SQ
ca8c0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
ca8d0 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  ITE|SQLITE_OPEN_
ca8e0 4d 41 49 4e 5f 44 42 29 29 20 3d 3d 0a 20 20 20  MAIN_DB)) ==.   
ca8f0 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c              (SQL
ca900 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
ca910 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  TE|SQLITE_OPEN_M
ca920 41 49 4e 5f 44 42 29 0a 20 20 20 20 20 20 20 26  AIN_DB).       &
ca930 26 20 21 77 69 6e 63 65 43 72 65 61 74 65 4c 6f  & !winceCreateLo
ca940 63 6b 28 7a 4e 61 6d 65 2c 20 70 46 69 6c 65 29  ck(zName, pFile)
ca950 0a 20 20 29 7b 0a 20 20 20 20 43 6c 6f 73 65 48  .  ){.    CloseH
ca960 61 6e 64 6c 65 28 68 29 3b 0a 20 20 20 20 66 72  andle(h);.    fr
ca970 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  ee(zConverted);.
ca980 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
ca990 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a  E_CANTOPEN;.  }.
ca9a0 20 20 69 66 28 20 69 73 54 65 6d 70 20 29 7b 0a    if( isTemp ){.
ca9b0 20 20 20 20 70 46 69 6c 65 2d 3e 7a 44 65 6c 65      pFile->zDele
ca9c0 74 65 4f 6e 43 6c 6f 73 65 20 3d 20 7a 43 6f 6e  teOnClose = zCon
ca9d0 76 65 72 74 65 64 3b 0a 20 20 7d 65 6c 73 65 0a  verted;.  }else.
ca9e0 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20 66  #endif.  {.    f
ca9f0 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b  ree(zConverted);
caa00 0a 20 20 7d 0a 20 20 4f 70 65 6e 43 6f 75 6e 74  .  }.  OpenCount
caa10 65 72 28 2b 31 29 3b 0a 20 20 72 65 74 75 72 6e  er(+1);.  return
caa20 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
caa30 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20  *.** Delete the 
caa40 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  named file..**.*
caa50 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 69 6e 64  * Note that wind
caa60 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20 61 6c 6c  ows does not all
caa70 6f 77 20 61 20 66 69 6c 65 20 74 6f 20 62 65 20  ow a file to be 
caa80 64 65 6c 65 74 65 64 20 69 66 20 73 6f 6d 65 20  deleted if some 
caa90 6f 74 68 65 72 0a 2a 2a 20 70 72 6f 63 65 73 73  other.** process
caaa0 20 68 61 73 20 69 74 20 6f 70 65 6e 2e 20 20 53   has it open.  S
caab0 6f 6d 65 74 69 6d 65 73 20 61 20 76 69 72 75 73  ometimes a virus
caac0 20 73 63 61 6e 6e 65 72 20 6f 72 20 69 6e 64 65   scanner or inde
caad0 78 69 6e 67 20 70 72 6f 67 72 61 6d 0a 2a 2a 20  xing program.** 
caae0 77 69 6c 6c 20 6f 70 65 6e 20 61 20 6a 6f 75 72  will open a jour
caaf0 6e 61 6c 20 66 69 6c 65 20 73 68 6f 72 74 6c 79  nal file shortly
cab00 20 61 66 74 65 72 20 69 74 20 69 73 20 63 72 65   after it is cre
cab10 61 74 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f  ated in order to
cab20 20 64 6f 0a 2a 2a 20 77 68 61 74 65 76 65 72 20   do.** whatever 
cab30 64 6f 65 73 2e 20 20 57 68 69 6c 65 20 74 68 69  does.  While thi
cab40 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20  s other process 
cab50 69 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a 2a  is holding the.*
cab60 2a 20 66 69 6c 65 20 6f 70 65 6e 2c 20 77 65 20  * file open, we 
cab70 77 69 6c 6c 20 62 65 20 75 6e 61 62 6c 65 20 74  will be unable t
cab80 6f 20 64 65 6c 65 74 65 20 69 74 2e 20 20 54 6f  o delete it.  To
cab90 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 69   work around thi
caba0 73 0a 2a 2a 20 70 72 6f 62 6c 65 6d 2c 20 77 65  s.** problem, we
cabb0 20 64 65 6c 61 79 20 31 30 30 20 6d 69 6c 6c 69   delay 100 milli
cabc0 73 65 63 6f 6e 64 73 20 61 6e 64 20 74 72 79 20  seconds and try 
cabd0 74 6f 20 64 65 6c 65 74 65 20 61 67 61 69 6e 2e  to delete again.
cabe0 20 20 55 70 0a 2a 2a 20 74 6f 20 4d 58 5f 44 45    Up.** to MX_DE
cabf0 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 73 20  LETION_ATTEMPTs 
cac00 64 65 6c 65 74 69 6f 6e 20 61 74 74 65 6d 70 74  deletion attempt
cac10 73 20 61 72 65 20 72 75 6e 20 62 65 66 6f 72 65  s are run before
cac20 20 67 69 76 69 6e 67 0a 2a 2a 20 75 70 20 61 6e   giving.** up an
cac30 64 20 72 65 74 75 72 6e 69 6e 67 20 61 6e 20 65  d returning an e
cac40 72 72 6f 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  rror..*/.#define
cac50 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54   MX_DELETION_ATT
cac60 45 4d 50 54 53 20 35 0a 73 74 61 74 69 63 20 69  EMPTS 5.static i
cac70 6e 74 20 77 69 6e 44 65 6c 65 74 65 28 0a 20 20  nt winDelete(.  
cac80 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
cac90 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  s,          /* N
caca0 6f 74 20 75 73 65 64 20 6f 6e 20 77 69 6e 33 32  ot used on win32
cacb0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
cacc0 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20   *zFilename,    
cacd0 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c    /* Name of fil
cace0 65 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20  e to delete */. 
cacf0 20 69 6e 74 20 73 79 6e 63 44 69 72 20 20 20 20   int syncDir    
cad00 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
cad10 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77 69 6e 33  Not used on win3
cad20 32 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 6e  2 */.){.  int cn
cad30 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b  t = 0;.  int rc;
cad40 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72  .  void *zConver
cad50 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66  ted = convertUtf
cad60 38 46 69 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e  8Filename(zFilen
cad70 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e  ame);.  if( zCon
cad80 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20  verted==0 ){.   
cad90 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
cada0 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75  OMEM;.  }.  Simu
cadb0 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75  lateIOError(retu
cadc0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
cadd0 44 45 4c 45 54 45 29 3b 0a 20 20 69 66 28 20 69  DELETE);.  if( i
cade0 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 64 6f 7b  sNT() ){.    do{
cadf0 0a 20 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c  .      DeleteFil
cae00 65 57 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  eW(zConverted);.
cae10 20 20 20 20 7d 77 68 69 6c 65 28 20 28 72 63 20      }while( (rc 
cae20 3d 20 47 65 74 46 69 6c 65 41 74 74 72 69 62 75  = GetFileAttribu
cae30 74 65 73 57 28 7a 43 6f 6e 76 65 72 74 65 64 29  tesW(zConverted)
cae40 29 21 3d 30 78 66 66 66 66 66 66 66 66 20 0a 20  )!=0xffffffff . 
cae50 20 20 20 20 20 20 20 20 20 20 20 26 26 20 63 6e             && cn
cae60 74 2b 2b 20 3c 20 4d 58 5f 44 45 4c 45 54 49 4f  t++ < MX_DELETIO
cae70 4e 5f 41 54 54 45 4d 50 54 53 20 26 26 20 28 53  N_ATTEMPTS && (S
cae80 6c 65 65 70 28 31 30 30 29 2c 20 31 29 20 29 3b  leep(100), 1) );
cae90 0a 20 20 7d 65 6c 73 65 7b 0a 23 69 66 20 4f 53  .  }else{.#if OS
caea0 5f 57 49 4e 43 45 0a 20 20 20 20 72 65 74 75 72  _WINCE.    retur
caeb0 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
caec0 23 65 6c 73 65 0a 20 20 20 20 64 6f 7b 0a 20 20  #else.    do{.  
caed0 20 20 20 20 44 65 6c 65 74 65 46 69 6c 65 41 28      DeleteFileA(
caee0 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20  zConverted);.   
caef0 20 7d 77 68 69 6c 65 28 20 28 72 63 20 3d 20 47   }while( (rc = G
caf00 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73  etFileAttributes
caf10 41 28 7a 43 6f 6e 76 65 72 74 65 64 29 29 21 3d  A(zConverted))!=
caf20 30 78 66 66 66 66 66 66 66 66 0a 20 20 20 20 20  0xffffffff.     
caf30 20 20 20 20 20 20 20 26 26 20 63 6e 74 2b 2b 20         && cnt++ 
caf40 3c 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54  < MX_DELETION_AT
caf50 54 45 4d 50 54 53 20 26 26 20 28 53 6c 65 65 70  TEMPTS && (Sleep
caf60 28 31 30 30 29 2c 20 31 29 20 29 3b 0a 23 65 6e  (100), 1) );.#en
caf70 64 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 28 7a  dif.  }.  free(z
caf80 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 4f 53  Converted);.  OS
caf90 54 52 41 43 45 32 28 22 44 45 4c 45 54 45 20 5c  TRACE2("DELETE \
cafa0 22 25 73 5c 22 5c 6e 22 2c 20 7a 46 69 6c 65 6e  "%s\"\n", zFilen
cafb0 61 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ame);.  return r
cafc0 63 3d 3d 30 78 66 66 66 66 66 66 66 66 20 3f 20  c==0xffffffff ? 
cafd0 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49  SQLITE_OK : SQLI
cafe0 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 3b  TE_IOERR_DELETE;
caff0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  .}../*.** Check 
cb000 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 61 6e  the existance an
cb010 64 20 73 74 61 74 75 73 20 6f 66 20 61 20 66 69  d status of a fi
cb020 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
cb030 74 20 77 69 6e 41 63 63 65 73 73 28 0a 20 20 73  t winAccess(.  s
cb040 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
cb050 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74  ,         /* Not
cb060 20 75 73 65 64 20 6f 6e 20 77 69 6e 33 32 20 2a   used on win32 *
cb070 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
cb080 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20 2f  zFilename,     /
cb090 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74  * Name of file t
cb0a0 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74  o check */.  int
cb0b0 20 66 6c 61 67 73 20 20 20 20 20 20 20 20 20 20   flags          
cb0c0 20 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20          /* Type 
cb0d0 6f 66 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20  of test to make 
cb0e0 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a  on this file */.
cb0f0 29 7b 0a 20 20 44 57 4f 52 44 20 61 74 74 72 3b  ){.  DWORD attr;
cb100 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 76 6f 69  .  int rc;.  voi
cb110 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20  d *zConverted = 
cb120 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e  convertUtf8Filen
cb130 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  ame(zFilename);.
cb140 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64    if( zConverted
cb150 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
cb160 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
cb170 20 20 7d 0a 20 20 69 66 28 20 69 73 4e 54 28 29    }.  if( isNT()
cb180 20 29 7b 0a 20 20 20 20 61 74 74 72 20 3d 20 47   ){.    attr = G
cb190 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73  etFileAttributes
cb1a0 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65  W((WCHAR*)zConve
cb1b0 72 74 65 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  rted);.  }else{.
cb1c0 23 69 66 20 4f 53 5f 57 49 4e 43 45 0a 20 20 20  #if OS_WINCE.   
cb1d0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
cb1e0 4f 4d 45 4d 3b 0a 23 65 6c 73 65 0a 20 20 20 20  OMEM;.#else.    
cb1f0 61 74 74 72 20 3d 20 47 65 74 46 69 6c 65 41 74  attr = GetFileAt
cb200 74 72 69 62 75 74 65 73 41 28 28 63 68 61 72 2a  tributesA((char*
cb210 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23 65  )zConverted);.#e
cb220 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 28  ndif.  }.  free(
cb230 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 73  zConverted);.  s
cb240 77 69 74 63 68 28 20 66 6c 61 67 73 20 29 7b 0a  witch( flags ){.
cb250 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
cb260 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 20 20  ACCESS_READ:.   
cb270 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43   case SQLITE_ACC
cb280 45 53 53 5f 45 58 49 53 54 53 3a 0a 20 20 20 20  ESS_EXISTS:.    
cb290 20 20 72 63 20 3d 20 61 74 74 72 21 3d 30 78 66    rc = attr!=0xf
cb2a0 66 66 66 66 66 66 66 3b 0a 20 20 20 20 20 20 62  fffffff;.      b
cb2b0 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
cb2c0 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41  QLITE_ACCESS_REA
cb2d0 44 57 52 49 54 45 3a 0a 20 20 20 20 20 20 72 63  DWRITE:.      rc
cb2e0 20 3d 20 28 61 74 74 72 20 26 20 46 49 4c 45 5f   = (attr & FILE_
cb2f0 41 54 54 52 49 42 55 54 45 5f 52 45 41 44 4f 4e  ATTRIBUTE_READON
cb300 4c 59 29 3d 3d 30 3b 0a 20 20 20 20 20 20 62 72  LY)==0;.      br
cb310 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74  eak;.    default
cb320 3a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 21  :.      assert(!
cb330 22 49 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61  "Invalid flags a
cb340 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 0a 20  rgument");.  }. 
cb350 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a   return rc;.}...
cb360 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 74  /*.** Create a t
cb370 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61  emporary file na
cb380 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75  me in zBuf.  zBu
cb390 66 20 6d 75 73 74 20 62 65 20 62 69 67 20 65 6e  f must be big en
cb3a0 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64 20  ough to.** hold 
cb3b0 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e  at pVfs->mxPathn
cb3c0 61 6d 65 20 63 68 61 72 61 63 74 65 72 73 2e 0a  ame characters..
cb3d0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
cb3e0 6e 47 65 74 54 65 6d 70 6e 61 6d 65 28 73 71 6c  nGetTempname(sql
cb3f0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
cb400 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a  int nBuf, char *
cb410 7a 42 75 66 29 7b 0a 20 20 73 74 61 74 69 63 20  zBuf){.  static 
cb420 63 68 61 72 20 7a 43 68 61 72 73 5b 5d 20 3d 0a  char zChars[] =.
cb430 20 20 20 20 22 61 62 63 64 65 66 67 68 69 6a 6b      "abcdefghijk
cb440 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 22  lmnopqrstuvwxyz"
cb450 0a 20 20 20 20 22 41 42 43 44 45 46 47 48 49 4a  .    "ABCDEFGHIJ
cb460 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a  KLMNOPQRSTUVWXYZ
cb470 22 0a 20 20 20 20 22 30 31 32 33 34 35 36 37 38  ".    "012345678
cb480 39 22 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a  9";.  int i, j;.
cb490 20 20 63 68 61 72 20 7a 54 65 6d 70 50 61 74 68    char zTempPath
cb4a0 5b 4d 41 58 5f 50 41 54 48 2b 31 5d 3b 0a 20 20  [MAX_PATH+1];.  
cb4b0 69 66 28 20 73 71 6c 69 74 65 33 5f 74 65 6d 70  if( sqlite3_temp
cb4c0 5f 64 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20  _directory ){.  
cb4d0 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
cb4e0 74 66 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20  tf(MAX_PATH-30, 
cb4f0 7a 54 65 6d 70 50 61 74 68 2c 20 22 25 73 22 2c  zTempPath, "%s",
cb500 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69   sqlite3_temp_di
cb510 72 65 63 74 6f 72 79 29 3b 0a 20 20 7d 65 6c 73  rectory);.  }els
cb520 65 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a  e if( isNT() ){.
cb530 20 20 20 20 63 68 61 72 20 2a 7a 4d 75 6c 74 69      char *zMulti
cb540 3b 0a 20 20 20 20 57 43 48 41 52 20 7a 57 69 64  ;.    WCHAR zWid
cb550 65 50 61 74 68 5b 4d 41 58 5f 50 41 54 48 5d 3b  ePath[MAX_PATH];
cb560 0a 20 20 20 20 47 65 74 54 65 6d 70 50 61 74 68  .    GetTempPath
cb570 57 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a  W(MAX_PATH-30, z
cb580 57 69 64 65 50 61 74 68 29 3b 0a 20 20 20 20 7a  WidePath);.    z
cb590 4d 75 6c 74 69 20 3d 20 75 6e 69 63 6f 64 65 54  Multi = unicodeT
cb5a0 6f 55 74 66 38 28 7a 57 69 64 65 50 61 74 68 29  oUtf8(zWidePath)
cb5b0 3b 0a 20 20 20 20 69 66 28 20 7a 4d 75 6c 74 69  ;.    if( zMulti
cb5c0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
cb5d0 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50  3_snprintf(MAX_P
cb5e0 41 54 48 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74  ATH-30, zTempPat
cb5f0 68 2c 20 22 25 73 22 2c 20 7a 4d 75 6c 74 69 29  h, "%s", zMulti)
cb600 3b 0a 20 20 20 20 20 20 66 72 65 65 28 7a 4d 75  ;.      free(zMu
cb610 6c 74 69 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  lti);.    }else{
cb620 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
cb630 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
cb640 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63  }.  }else{.    c
cb650 68 61 72 20 2a 7a 55 74 66 38 3b 0a 20 20 20 20  har *zUtf8;.    
cb660 63 68 61 72 20 7a 4d 62 63 73 50 61 74 68 5b 4d  char zMbcsPath[M
cb670 41 58 5f 50 41 54 48 5d 3b 0a 20 20 20 20 47 65  AX_PATH];.    Ge
cb680 74 54 65 6d 70 50 61 74 68 41 28 4d 41 58 5f 50  tTempPathA(MAX_P
cb690 41 54 48 2d 33 30 2c 20 7a 4d 62 63 73 50 61 74  ATH-30, zMbcsPat
cb6a0 68 29 3b 0a 20 20 20 20 7a 55 74 66 38 20 3d 20  h);.    zUtf8 = 
cb6b0 6d 62 63 73 54 6f 55 74 66 38 28 7a 4d 62 63 73  mbcsToUtf8(zMbcs
cb6c0 50 61 74 68 29 3b 0a 20 20 20 20 69 66 28 20 7a  Path);.    if( z
cb6d0 55 74 66 38 20 29 7b 0a 20 20 20 20 20 20 73 71  Utf8 ){.      sq
cb6e0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d  lite3_snprintf(M
cb6f0 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d  AX_PATH-30, zTem
cb700 70 50 61 74 68 2c 20 22 25 73 22 2c 20 7a 55 74  pPath, "%s", zUt
cb710 66 38 29 3b 0a 20 20 20 20 20 20 66 72 65 65 28  f8);.      free(
cb720 7a 55 74 66 38 29 3b 0a 20 20 20 20 7d 65 6c 73  zUtf8);.    }els
cb730 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  e{.      return 
cb740 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
cb750 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d    }.  }.  for(i=
cb760 73 74 72 6c 65 6e 28 7a 54 65 6d 70 50 61 74 68  strlen(zTempPath
cb770 29 3b 20 69 3e 30 20 26 26 20 7a 54 65 6d 70 50  ); i>0 && zTempP
cb780 61 74 68 5b 69 2d 31 5d 3d 3d 27 5c 5c 27 3b 20  ath[i-1]=='\\'; 
cb790 69 2d 2d 29 7b 7d 0a 20 20 7a 54 65 6d 70 50 61  i--){}.  zTempPa
cb7a0 74 68 5b 69 5d 20 3d 20 30 3b 0a 20 20 73 71 6c  th[i] = 0;.  sql
cb7b0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42  ite3_snprintf(nB
cb7c0 75 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20  uf-30, zBuf,.   
cb7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cb7e0 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d  "%s\\"SQLITE_TEM
cb7f0 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a  P_FILE_PREFIX, z
cb800 54 65 6d 70 50 61 74 68 29 3b 0a 20 20 6a 20 3d  TempPath);.  j =
cb810 20 73 74 72 6c 65 6e 28 7a 42 75 66 29 3b 0a 20   strlen(zBuf);. 
cb820 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
cb830 65 73 73 28 32 30 2c 20 26 7a 42 75 66 5b 6a 5d  ess(20, &zBuf[j]
cb840 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
cb850 32 30 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20  20; i++, j++){. 
cb860 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68     zBuf[j] = (ch
cb870 61 72 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73  ar)zChars[ ((uns
cb880 69 67 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b  igned char)zBuf[
cb890 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61  j])%(sizeof(zCha
cb8a0 72 73 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a 20 20  rs)-1) ];.  }.  
cb8b0 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 4f  zBuf[j] = 0;.  O
cb8c0 53 54 52 41 43 45 32 28 22 54 45 4d 50 20 46 49  STRACE2("TEMP FI
cb8d0 4c 45 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c 20 7a  LENAME: %s\n", z
cb8e0 42 75 66 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  Buf);.  return S
cb8f0 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a  QLITE_OK; .}../*
cb900 0a 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c 61 74  .** Turn a relat
cb910 69 76 65 20 70 61 74 68 6e 61 6d 65 20 69 6e 74  ive pathname int
cb920 6f 20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d  o a full pathnam
cb930 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 66 75  e.  Write the fu
cb940 6c 6c 0a 2a 2a 20 70 61 74 68 6e 61 6d 65 20 69  ll.** pathname i
cb950 6e 74 6f 20 7a 4f 75 74 5b 5d 2e 20 20 7a 4f 75  nto zOut[].  zOu
cb960 74 5b 5d 20 77 69 6c 6c 20 62 65 20 61 74 20 6c  t[] will be at l
cb970 65 61 73 74 20 70 56 66 73 2d 3e 6d 78 50 61 74  east pVfs->mxPat
cb980 68 6e 61 6d 65 0a 2a 2a 20 62 79 74 65 73 20 69  hname.** bytes i
cb990 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69  n size..*/.stati
cb9a0 63 20 69 6e 74 20 77 69 6e 46 75 6c 6c 50 61 74  c int winFullPat
cb9b0 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33  hname(.  sqlite3
cb9c0 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
cb9d0 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
cb9e0 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 20  r to vfs object 
cb9f0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
cba00 2a 7a 52 65 6c 61 74 69 76 65 2c 20 20 20 20 20  *zRelative,     
cba10 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20 72     /* Possibly r
cba20 65 6c 61 74 69 76 65 20 69 6e 70 75 74 20 70 61  elative input pa
cba30 74 68 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6c  th */.  int nFul
cba40 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l,              
cba50 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
cba60 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 69   output buffer i
cba70 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 61  n bytes */.  cha
cba80 72 20 2a 7a 46 75 6c 6c 20 20 20 20 20 20 20 20  r *zFull        
cba90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75             /* Ou
cbaa0 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 29  tput buffer */.)
cbab0 7b 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f  {..#if defined(_
cbac0 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20 63 79 67  _CYGWIN__).  cyg
cbad0 77 69 6e 5f 63 6f 6e 76 5f 74 6f 5f 66 75 6c 6c  win_conv_to_full
cbae0 5f 77 69 6e 33 32 5f 70 61 74 68 28 7a 52 65 6c  _win32_path(zRel
cbaf0 61 74 69 76 65 2c 20 7a 46 75 6c 6c 29 3b 0a 20  ative, zFull);. 
cbb00 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
cbb10 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 4f  K;.#endif..#if O
cbb20 53 5f 57 49 4e 43 45 0a 20 20 2f 2a 20 57 69 6e  S_WINCE.  /* Win
cbb30 43 45 20 68 61 73 20 6e 6f 20 63 6f 6e 63 65 70  CE has no concep
cbb40 74 20 6f 66 20 61 20 72 65 6c 61 74 69 76 65 20  t of a relative 
cbb50 70 61 74 68 6e 61 6d 65 2c 20 6f 72 20 73 6f 20  pathname, or so 
cbb60 49 20 61 6d 20 74 6f 6c 64 2e 20 2a 2f 0a 20 20  I am told. */.  
cbb70 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
cbb80 28 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d  (pVfs->mxPathnam
cbb90 65 2c 20 7a 46 75 6c 6c 2c 20 22 25 73 22 2c 20  e, zFull, "%s", 
cbba0 7a 52 65 6c 61 74 69 76 65 29 3b 0a 20 20 72 65  zRelative);.  re
cbbb0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
cbbc0 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 4f 53 5f  #endif..#if !OS_
cbbd0 57 49 4e 43 45 20 26 26 20 21 64 65 66 69 6e 65  WINCE && !define
cbbe0 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20  d(__CYGWIN__).  
cbbf0 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 76 6f 69  int nByte;.  voi
cbc00 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20  d *zConverted;. 
cbc10 20 63 68 61 72 20 2a 7a 4f 75 74 3b 0a 20 20 7a   char *zOut;.  z
cbc20 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76  Converted = conv
cbc30 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28  ertUtf8Filename(
cbc40 7a 52 65 6c 61 74 69 76 65 29 3b 0a 20 20 69 66  zRelative);.  if
cbc50 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20  ( isNT() ){.    
cbc60 57 43 48 41 52 20 2a 7a 54 65 6d 70 3b 0a 20 20  WCHAR *zTemp;.  
cbc70 20 20 6e 42 79 74 65 20 3d 20 47 65 74 46 75 6c    nByte = GetFul
cbc80 6c 50 61 74 68 4e 61 6d 65 57 28 28 57 43 48 41  lPathNameW((WCHA
cbc90 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 30  R*)zConverted, 0
cbca0 2c 20 30 2c 20 30 29 20 2b 20 33 3b 0a 20 20 20  , 0, 0) + 3;.   
cbcb0 20 7a 54 65 6d 70 20 3d 20 6d 61 6c 6c 6f 63 28   zTemp = malloc(
cbcc0 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 54   nByte*sizeof(zT
cbcd0 65 6d 70 5b 30 5d 29 20 29 3b 0a 20 20 20 20 69  emp[0]) );.    i
cbce0 66 28 20 7a 54 65 6d 70 3d 3d 30 20 29 7b 0a 20  f( zTemp==0 ){. 
cbcf0 20 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65       free(zConve
cbd00 72 74 65 64 29 3b 0a 20 20 20 20 20 20 72 65 74  rted);.      ret
cbd10 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
cbd20 3b 0a 20 20 20 20 7d 0a 20 20 20 20 47 65 74 46  ;.    }.    GetF
cbd30 75 6c 6c 50 61 74 68 4e 61 6d 65 57 28 28 57 43  ullPathNameW((WC
cbd40 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c  HAR*)zConverted,
cbd50 20 6e 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20 30   nByte, zTemp, 0
cbd60 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e  );.    free(zCon
cbd70 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7a 4f 75  verted);.    zOu
cbd80 74 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66  t = unicodeToUtf
cbd90 38 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 66 72  8(zTemp);.    fr
cbda0 65 65 28 7a 54 65 6d 70 29 3b 0a 20 20 7d 65 6c  ee(zTemp);.  }el
cbdb0 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54  se{.    char *zT
cbdc0 65 6d 70 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d  emp;.    nByte =
cbdd0 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65   GetFullPathName
cbde0 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72  A((char*)zConver
cbdf0 74 65 64 2c 20 30 2c 20 30 2c 20 30 29 20 2b 20  ted, 0, 0, 0) + 
cbe00 33 3b 0a 20 20 20 20 7a 54 65 6d 70 20 3d 20 6d  3;.    zTemp = m
cbe10 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a  alloc( nByte*siz
cbe20 65 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20 29 3b  eof(zTemp[0]) );
cbe30 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70 3d 3d  .    if( zTemp==
cbe40 30 20 29 7b 0a 20 20 20 20 20 20 66 72 65 65 28  0 ){.      free(
cbe50 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20  zConverted);.   
cbe60 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
cbe70 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
cbe80 20 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d    GetFullPathNam
cbe90 65 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65  eA((char*)zConve
cbea0 72 74 65 64 2c 20 6e 42 79 74 65 2c 20 7a 54 65  rted, nByte, zTe
cbeb0 6d 70 2c 20 30 29 3b 0a 20 20 20 20 66 72 65 65  mp, 0);.    free
cbec0 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20  (zConverted);.  
cbed0 20 20 7a 4f 75 74 20 3d 20 6d 62 63 73 54 6f 55    zOut = mbcsToU
cbee0 74 66 38 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20  tf8(zTemp);.    
cbef0 66 72 65 65 28 7a 54 65 6d 70 29 3b 0a 20 20 7d  free(zTemp);.  }
cbf00 0a 20 20 69 66 28 20 7a 4f 75 74 20 29 7b 0a 20  .  if( zOut ){. 
cbf10 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
cbf20 6e 74 66 28 70 56 66 73 2d 3e 6d 78 50 61 74 68  ntf(pVfs->mxPath
cbf30 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22 25 73  name, zFull, "%s
cbf40 22 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 20 66 72  ", zOut);.    fr
cbf50 65 65 28 7a 4f 75 74 29 3b 0a 20 20 20 20 72 65  ee(zOut);.    re
cbf60 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
cbf70 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
cbf80 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
cbf90 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a  ;.  }.#endif.}..
cbfa0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
cbfb0 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
cbfc0 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61  ON./*.** Interfa
cbfd0 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20  ces for opening 
cbfe0 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79  a shared library
cbff0 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20  , finding entry 
cc000 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e  points.** within
cc010 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
cc020 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67  ary, and closing
cc030 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
cc040 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e  ary..*/./*.** In
cc050 74 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65  terfaces for ope
cc060 6e 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69  ning a shared li
cc070 62 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65  brary, finding e
cc080 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77  ntry points.** w
cc090 69 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64  ithin the shared
cc0a0 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c   library, and cl
cc0b0 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64  osing the shared
cc0c0 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 73 74 61   library..*/.sta
cc0d0 74 69 63 20 76 6f 69 64 20 2a 77 69 6e 44 6c 4f  tic void *winDlO
cc0e0 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20  pen(sqlite3_vfs 
cc0f0 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61  *pVfs, const cha
cc100 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20  r *zFilename){. 
cc110 20 48 41 4e 44 4c 45 20 68 3b 0a 20 20 76 6f 69   HANDLE h;.  voi
cc120 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20  d *zConverted = 
cc130 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e  convertUtf8Filen
cc140 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  ame(zFilename);.
cc150 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64    if( zConverted
cc160 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
cc170 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69  n 0;.  }.  if( i
cc180 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 68 20 3d  sNT() ){.    h =
cc190 20 4c 6f 61 64 4c 69 62 72 61 72 79 57 28 28 57   LoadLibraryW((W
cc1a0 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64  CHAR*)zConverted
cc1b0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 23 69 66 20  );.  }else{.#if 
cc1c0 4f 53 5f 57 49 4e 43 45 0a 20 20 20 20 72 65 74  OS_WINCE.    ret
cc1d0 75 72 6e 20 30 3b 0a 23 65 6c 73 65 0a 20 20 20  urn 0;.#else.   
cc1e0 20 68 20 3d 20 4c 6f 61 64 4c 69 62 72 61 72 79   h = LoadLibrary
cc1f0 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72  A((char*)zConver
cc200 74 65 64 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  ted);.#endif.  }
cc210 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74  .  free(zConvert
cc220 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 76  ed);.  return (v
cc230 6f 69 64 2a 29 68 3b 0a 7d 0a 73 74 61 74 69 63  oid*)h;.}.static
cc240 20 76 6f 69 64 20 77 69 6e 44 6c 45 72 72 6f 72   void winDlError
cc250 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
cc260 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68  fs, int nBuf, ch
cc270 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 23 69  ar *zBufOut){.#i
cc280 66 20 4f 53 5f 57 49 4e 43 45 0a 20 20 69 6e 74  f OS_WINCE.  int
cc290 20 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74   error = GetLast
cc2a0 45 72 72 6f 72 28 29 3b 0a 20 20 69 66 28 20 65  Error();.  if( e
cc2b0 72 72 6f 72 3e 30 78 37 46 46 46 46 46 46 20 29  rror>0x7FFFFFF )
cc2c0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  {.    sqlite3_sn
cc2d0 70 72 69 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75  printf(nBuf, zBu
cc2e0 66 4f 75 74 2c 20 22 4f 73 45 72 72 6f 72 20 30  fOut, "OsError 0
cc2f0 78 25 78 22 2c 20 65 72 72 6f 72 29 3b 0a 20 20  x%x", error);.  
cc300 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
cc310 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66  e3_snprintf(nBuf
cc320 2c 20 7a 42 75 66 4f 75 74 2c 20 22 4f 73 45 72  , zBufOut, "OsEr
cc330 72 6f 72 20 25 64 22 2c 20 65 72 72 6f 72 29 3b  ror %d", error);
cc340 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 46 6f 72  .  }.#else.  For
cc350 6d 61 74 4d 65 73 73 61 67 65 41 28 0a 20 20 20  matMessageA(.   
cc360 20 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f   FORMAT_MESSAGE_
cc370 46 52 4f 4d 5f 53 59 53 54 45 4d 2c 0a 20 20 20  FROM_SYSTEM,.   
cc380 20 4e 55 4c 4c 2c 0a 20 20 20 20 47 65 74 4c 61   NULL,.    GetLa
cc390 73 74 45 72 72 6f 72 28 29 2c 0a 20 20 20 20 30  stError(),.    0
cc3a0 2c 0a 20 20 20 20 7a 42 75 66 4f 75 74 2c 0a 20  ,.    zBufOut,. 
cc3b0 20 20 20 6e 42 75 66 2d 31 2c 0a 20 20 20 20 30     nBuf-1,.    0
cc3c0 0a 20 20 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 76  .  );.#endif.}.v
cc3d0 6f 69 64 20 2a 77 69 6e 44 6c 53 79 6d 28 73 71  oid *winDlSym(sq
cc3e0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
cc3f0 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 2c 20   void *pHandle, 
cc400 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d  const char *zSym
cc410 62 6f 6c 29 7b 0a 23 69 66 20 4f 53 5f 57 49 4e  bol){.#if OS_WIN
cc420 43 45 0a 20 20 2f 2a 20 54 68 65 20 47 65 74 50  CE.  /* The GetP
cc430 72 6f 63 41 64 64 72 65 73 73 41 28 29 20 72 6f  rocAddressA() ro
cc440 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 61 76  utine is only av
cc450 61 69 6c 61 62 6c 65 20 6f 6e 20 77 69 6e 63 65  ailable on wince
cc460 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 47 65  . */.  return Ge
cc470 74 50 72 6f 63 41 64 64 72 65 73 73 41 28 28 48  tProcAddressA((H
cc480 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20 7a  ANDLE)pHandle, z
cc490 53 79 6d 62 6f 6c 29 3b 0a 23 65 6c 73 65 0a 20  Symbol);.#else. 
cc4a0 20 2f 2a 20 41 6c 6c 20 6f 74 68 65 72 20 77 69   /* All other wi
cc4b0 6e 64 6f 77 73 20 70 6c 61 74 66 6f 72 6d 73 20  ndows platforms 
cc4c0 65 78 70 65 63 74 20 47 65 74 50 72 6f 63 41 64  expect GetProcAd
cc4d0 64 72 65 73 73 28 29 20 74 6f 20 74 61 6b 65 0a  dress() to take.
cc4e0 20 20 2a 2a 20 61 6e 20 41 6e 73 69 20 73 74 72    ** an Ansi str
cc4f0 69 6e 67 20 72 65 67 61 72 64 6c 65 73 73 20 6f  ing regardless o
cc500 66 20 74 68 65 20 5f 55 4e 49 43 4f 44 45 20 73  f the _UNICODE s
cc510 65 74 74 69 6e 67 20 2a 2f 0a 20 20 72 65 74 75  etting */.  retu
cc520 72 6e 20 47 65 74 50 72 6f 63 41 64 64 72 65 73  rn GetProcAddres
cc530 73 28 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c  s((HANDLE)pHandl
cc540 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65 6e  e, zSymbol);.#en
cc550 64 69 66 0a 7d 0a 76 6f 69 64 20 77 69 6e 44 6c  dif.}.void winDl
cc560 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66  Close(sqlite3_vf
cc570 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70  s *pVfs, void *p
cc580 48 61 6e 64 6c 65 29 7b 0a 20 20 46 72 65 65 4c  Handle){.  FreeL
cc590 69 62 72 61 72 79 28 28 48 41 4e 44 4c 45 29 70  ibrary((HANDLE)p
cc5a0 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65  Handle);.}.#else
cc5b0 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d   /* if SQLITE_OM
cc5c0 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f  IT_LOAD_EXTENSIO
cc5d0 4e 20 69 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f  N is defined: */
cc5e0 0a 20 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c  .  #define winDl
cc5f0 4f 70 65 6e 20 20 30 0a 20 20 23 64 65 66 69 6e  Open  0.  #defin
cc600 65 20 77 69 6e 44 6c 45 72 72 6f 72 20 30 0a 20  e winDlError 0. 
cc610 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 53 79   #define winDlSy
cc620 6d 20 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20  m   0.  #define 
cc630 77 69 6e 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e  winDlClose 0.#en
cc640 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74  dif.../*.** Writ
cc650 65 20 75 70 20 74 6f 20 6e 42 75 66 20 62 79 74  e up to nBuf byt
cc660 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73  es of randomness
cc670 20 69 6e 74 6f 20 7a 42 75 66 2e 0a 2a 2f 0a 73   into zBuf..*/.s
cc680 74 61 74 69 63 20 69 6e 74 20 77 69 6e 52 61 6e  tatic int winRan
cc690 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f  domness(sqlite3_
cc6a0 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e  vfs *pVfs, int n
cc6b0 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29  Buf, char *zBuf)
cc6c0 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20  {.  int n = 0;. 
cc6d0 20 69 66 28 20 73 69 7a 65 6f 66 28 53 59 53 54   if( sizeof(SYST
cc6e0 45 4d 54 49 4d 45 29 3c 3d 6e 42 75 66 2d 6e 20  EMTIME)<=nBuf-n 
cc6f0 29 7b 0a 20 20 20 20 53 59 53 54 45 4d 54 49 4d  ){.    SYSTEMTIM
cc700 45 20 78 3b 0a 20 20 20 20 47 65 74 53 79 73 74  E x;.    GetSyst
cc710 65 6d 54 69 6d 65 28 26 78 29 3b 0a 20 20 20 20  emTime(&x);.    
cc720 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c  memcpy(&zBuf[n],
cc730 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 29 29 3b   &x, sizeof(x));
cc740 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66  .    n += sizeof
cc750 28 78 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  (x);.  }.  if( s
cc760 69 7a 65 6f 66 28 44 57 4f 52 44 29 3c 3d 6e 42  izeof(DWORD)<=nB
cc770 75 66 2d 6e 20 29 7b 0a 20 20 20 20 44 57 4f 52  uf-n ){.    DWOR
cc780 44 20 70 69 64 20 3d 20 47 65 74 43 75 72 72 65  D pid = GetCurre
cc790 6e 74 50 72 6f 63 65 73 73 49 64 28 29 3b 0a 20  ntProcessId();. 
cc7a0 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b     memcpy(&zBuf[
cc7b0 6e 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65 6f 66  n], &pid, sizeof
cc7c0 28 70 69 64 29 29 3b 0a 20 20 20 20 6e 20 2b 3d  (pid));.    n +=
cc7d0 20 73 69 7a 65 6f 66 28 70 69 64 29 3b 0a 20 20   sizeof(pid);.  
cc7e0 7d 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 28 44  }.  if( sizeof(D
cc7f0 57 4f 52 44 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b  WORD)<=nBuf-n ){
cc800 0a 20 20 20 20 44 57 4f 52 44 20 63 6e 74 20 3d  .    DWORD cnt =
cc810 20 47 65 74 54 69 63 6b 43 6f 75 6e 74 28 29 3b   GetTickCount();
cc820 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75  .    memcpy(&zBu
cc830 66 5b 6e 5d 2c 20 26 63 6e 74 2c 20 73 69 7a 65  f[n], &cnt, size
cc840 6f 66 28 63 6e 74 29 29 3b 0a 20 20 20 20 6e 20  of(cnt));.    n 
cc850 2b 3d 20 73 69 7a 65 6f 66 28 63 6e 74 29 3b 0a  += sizeof(cnt);.
cc860 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65 6f 66    }.  if( sizeof
cc870 28 4c 41 52 47 45 5f 49 4e 54 45 47 45 52 29 3c  (LARGE_INTEGER)<
cc880 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 4c  =nBuf-n ){.    L
cc890 41 52 47 45 5f 49 4e 54 45 47 45 52 20 69 3b 0a  ARGE_INTEGER i;.
cc8a0 20 20 20 20 51 75 65 72 79 50 65 72 66 6f 72 6d      QueryPerform
cc8b0 61 6e 63 65 43 6f 75 6e 74 65 72 28 26 69 29 3b  anceCounter(&i);
cc8c0 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75  .    memcpy(&zBu
cc8d0 66 5b 6e 5d 2c 20 26 69 2c 20 73 69 7a 65 6f 66  f[n], &i, sizeof
cc8e0 28 69 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73  (i));.    n += s
cc8f0 69 7a 65 6f 66 28 69 29 3b 0a 20 20 7d 0a 20 20  izeof(i);.  }.  
cc900 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 0a 2f 2a  return n;.}.../*
cc910 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 61 20  .** Sleep for a 
cc920 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 20 52  little while.  R
cc930 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74  eturn the amount
cc940 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a   of time slept..
cc950 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
cc960 6e 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76  nSleep(sqlite3_v
cc970 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d 69  fs *pVfs, int mi
cc980 63 72 6f 73 65 63 29 7b 0a 20 20 53 6c 65 65 70  crosec){.  Sleep
cc990 28 28 6d 69 63 72 6f 73 65 63 2b 39 39 39 29 2f  ((microsec+999)/
cc9a0 31 30 30 30 29 3b 0a 20 20 72 65 74 75 72 6e 20  1000);.  return 
cc9b0 28 28 6d 69 63 72 6f 73 65 63 2b 39 39 39 29 2f  ((microsec+999)/
cc9c0 31 30 30 30 29 2a 31 30 30 30 3b 0a 7d 0a 0a 2f  1000)*1000;.}../
cc9d0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
cc9e0 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20  ng variable, if 
cc9f0 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72  set to a non-zer
cca00 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f 6d 65 73  o value, becomes
cca10 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72   the result.** r
cca20 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 73 71 6c  eturned from sql
cca30 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d  ite3OsCurrentTim
cca40 65 28 29 2e 20 20 54 68 69 73 20 69 73 20 75 73  e().  This is us
cca50 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a  ed for testing..
cca60 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
cca70 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49  _TEST.SQLITE_API
cca80 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 75 72   int sqlite3_cur
cca90 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 0a 23  rent_time = 0;.#
ccaa0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e  endif../*.** Fin
ccab0 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 69  d the current ti
ccac0 6d 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61 6c  me (in Universal
ccad0 20 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 6d   Coordinated Tim
ccae0 65 29 2e 20 20 57 72 69 74 65 20 74 68 65 0a 2a  e).  Write the.*
ccaf0 2a 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 61  * current time a
ccb00 6e 64 20 64 61 74 65 20 61 73 20 61 20 4a 75 6c  nd date as a Jul
ccb10 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 69  ian Day number i
ccb20 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a  nto *prNow and.*
ccb30 2a 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74  * return 0.  Ret
ccb40 75 72 6e 20 31 20 69 66 20 74 68 65 20 74 69 6d  urn 1 if the tim
ccb50 65 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f  e and date canno
ccb60 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 69  t be found..*/.i
ccb70 6e 74 20 77 69 6e 43 75 72 72 65 6e 74 54 69 6d  nt winCurrentTim
ccb80 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  e(sqlite3_vfs *p
ccb90 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70 72 4e  Vfs, double *prN
ccba0 6f 77 29 7b 0a 20 20 46 49 4c 45 54 49 4d 45 20  ow){.  FILETIME 
ccbb0 66 74 3b 0a 20 20 2f 2a 20 46 49 4c 45 54 49 4d  ft;.  /* FILETIM
ccbc0 45 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61  E structure is a
ccbd0 20 36 34 2d 62 69 74 20 76 61 6c 75 65 20 72 65   64-bit value re
ccbe0 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 6e  presenting the n
ccbf0 75 6d 62 65 72 20 6f 66 20 0a 20 20 20 20 20 31  umber of .     1
ccc00 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 20 69 6e  00-nanosecond in
ccc10 74 65 72 76 61 6c 73 20 73 69 6e 63 65 20 4a 61  tervals since Ja
ccc20 6e 75 61 72 79 20 31 2c 20 31 36 30 31 20 28 3d  nuary 1, 1601 (=
ccc30 20 4a 44 20 32 33 30 35 38 31 33 2e 35 29 2e 20   JD 2305813.5). 
ccc40 0a 20 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 6e  .  */.  double n
ccc50 6f 77 3b 0a 23 69 66 20 4f 53 5f 57 49 4e 43 45  ow;.#if OS_WINCE
ccc60 0a 20 20 53 59 53 54 45 4d 54 49 4d 45 20 74 69  .  SYSTEMTIME ti
ccc70 6d 65 3b 0a 20 20 47 65 74 53 79 73 74 65 6d 54  me;.  GetSystemT
ccc80 69 6d 65 28 26 74 69 6d 65 29 3b 0a 20 20 53 79  ime(&time);.  Sy
ccc90 73 74 65 6d 54 69 6d 65 54 6f 46 69 6c 65 54 69  stemTimeToFileTi
ccca0 6d 65 28 26 74 69 6d 65 2c 26 66 74 29 3b 0a 23  me(&time,&ft);.#
cccb0 65 6c 73 65 0a 20 20 47 65 74 53 79 73 74 65 6d  else.  GetSystem
cccc0 54 69 6d 65 41 73 46 69 6c 65 54 69 6d 65 28 20  TimeAsFileTime( 
cccd0 26 66 74 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20  &ft );.#endif.  
ccce0 6e 6f 77 20 3d 20 28 28 64 6f 75 62 6c 65 29 66  now = ((double)f
cccf0 74 2e 64 77 48 69 67 68 44 61 74 65 54 69 6d 65  t.dwHighDateTime
ccd00 29 20 2a 20 34 32 39 34 39 36 37 32 39 36 2e 30  ) * 4294967296.0
ccd10 3b 20 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 28 6e  ; .  *prNow = (n
ccd20 6f 77 20 2b 20 66 74 2e 64 77 4c 6f 77 44 61 74  ow + ft.dwLowDat
ccd30 65 54 69 6d 65 29 2f 38 36 34 30 30 30 30 30 30  eTime)/864000000
ccd40 30 30 30 2e 30 20 2b 20 32 33 30 35 38 31 33 2e  000.0 + 2305813.
ccd50 35 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  5;.#ifdef SQLITE
ccd60 5f 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69  _TEST.  if( sqli
ccd70 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65  te3_current_time
ccd80 20 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d   ){.    *prNow =
ccd90 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74   sqlite3_current
ccda0 5f 74 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20  _time/86400.0 + 
ccdb0 32 34 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23  2440587.5;.  }.#
ccdc0 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 30  endif.  return 0
ccdd0 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ;.}.../*.** Retu
ccde0 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
ccdf0 74 68 65 20 73 71 6c 69 74 65 33 44 65 66 61 75  the sqlite3Defau
cce00 6c 74 56 66 73 20 73 74 72 75 63 74 75 72 65 2e  ltVfs structure.
cce10 20 20 20 57 65 20 75 73 65 0a 2a 2a 20 61 20 66     We use.** a f
cce20 75 6e 63 74 69 6f 6e 20 72 61 74 68 65 72 20 74  unction rather t
cce30 68 61 6e 20 67 69 76 65 20 74 68 65 20 73 74 72  han give the str
cce40 75 63 74 75 72 65 20 67 6c 6f 62 61 6c 20 73 63  ucture global sc
cce50 6f 70 65 20 62 65 63 61 75 73 65 0a 2a 2a 20 73  ope because.** s
cce60 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 20 28 4d  ome compilers (M
cce70 53 56 43 29 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f  SVC) do not allo
cce80 77 20 66 6f 72 77 61 72 64 20 64 65 63 6c 61 72  w forward declar
cce90 61 74 69 6f 6e 73 20 6f 66 0a 2a 2a 20 69 6e 69  ations of.** ini
ccea0 74 69 61 6c 69 7a 65 64 20 73 74 72 75 63 74 75  tialized structu
cceb0 72 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  res..*/.SQLITE_P
ccec0 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76  RIVATE sqlite3_v
cced0 66 73 20 2a 73 71 6c 69 74 65 33 4f 73 44 65 66  fs *sqlite3OsDef
ccee0 61 75 6c 74 56 66 73 28 76 6f 69 64 29 7b 0a 20  aultVfs(void){. 
ccef0 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f   static sqlite3_
ccf00 76 66 73 20 77 69 6e 56 66 73 20 3d 20 7b 0a 20  vfs winVfs = {. 
ccf10 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20     1,           
ccf20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f        /* iVersio
ccf30 6e 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28  n */.    sizeof(
ccf40 77 69 6e 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73  winFile),   /* s
ccf50 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 4d  zOsFile */.    M
ccf60 41 58 5f 50 41 54 48 2c 20 20 20 20 20 20 20 20  AX_PATH,        
ccf70 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20    /* mxPathname 
ccf80 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20  */.    0,       
ccf90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65            /* pNe
ccfa0 78 74 20 2a 2f 0a 20 20 20 20 22 77 69 6e 33 32  xt */.    "win32
ccfb0 22 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ",           /* 
ccfc0 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20  zName */.    0, 
ccfd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ccfe0 2f 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 20  /* pAppData */. 
ccff0 20 0a 20 20 20 20 77 69 6e 4f 70 65 6e 2c 20 20   .    winOpen,  
cd000 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65           /* xOpe
cd010 6e 20 2a 2f 0a 20 20 20 20 77 69 6e 44 65 6c 65  n */.    winDele
cd020 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78  te,         /* x
cd030 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20 77 69  Delete */.    wi
cd040 6e 41 63 63 65 73 73 2c 20 20 20 20 20 20 20 20  nAccess,        
cd050 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a 20   /* xAccess */. 
cd060 20 20 20 77 69 6e 47 65 74 54 65 6d 70 6e 61 6d     winGetTempnam
cd070 65 2c 20 20 20 20 2f 2a 20 78 47 65 74 54 65 6d  e,    /* xGetTem
cd080 70 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e  pName */.    win
cd090 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 20 20  FullPathname,   
cd0a0 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65  /* xFullPathname
cd0b0 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 4f 70 65   */.    winDlOpe
cd0c0 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44  n,         /* xD
cd0d0 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20 20 77 69 6e  lOpen */.    win
cd0e0 44 6c 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20  DlError,        
cd0f0 2f 2a 20 78 44 6c 45 72 72 6f 72 20 2a 2f 0a 20  /* xDlError */. 
cd100 20 20 20 77 69 6e 44 6c 53 79 6d 2c 20 20 20 20     winDlSym,    
cd110 20 20 20 20 20 20 2f 2a 20 78 44 6c 53 79 6d 20        /* xDlSym 
cd120 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 43 6c 6f 73  */.    winDlClos
cd130 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c  e,        /* xDl
cd140 43 6c 6f 73 65 20 2a 2f 0a 20 20 20 20 77 69 6e  Close */.    win
cd150 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20  Randomness,     
cd160 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20 2a  /* xRandomness *
cd170 2f 0a 20 20 20 20 77 69 6e 53 6c 65 65 70 2c 20  /.    winSleep, 
cd180 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 6c 65           /* xSle
cd190 65 70 20 2a 2f 0a 20 20 20 20 77 69 6e 43 75 72  ep */.    winCur
cd1a0 72 65 6e 74 54 69 6d 65 20 20 20 20 20 2f 2a 20  rentTime     /* 
cd1b0 78 43 75 72 72 65 6e 74 54 69 6d 65 20 2a 2f 0a  xCurrentTime */.
cd1c0 20 20 7d 3b 0a 20 20 0a 20 20 72 65 74 75 72 6e    };.  .  return
cd1d0 20 26 77 69 6e 56 66 73 3b 0a 7d 0a 0a 23 65 6e   &winVfs;.}..#en
cd1e0 64 69 66 20 2f 2a 20 4f 53 5f 57 49 4e 20 2a 2f  dif /* OS_WIN */
cd1f0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
cd200 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 77 69 6e 2e  * End of os_win.
cd210 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
cd220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd240 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
cd250 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62 69 74  * Begin file bit
cd260 76 65 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  vec.c **********
cd270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd290 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 46 65 62  /./*.** 2008 Feb
cd2a0 72 75 61 72 79 20 31 36 0a 2a 2a 0a 2a 2a 20 54  ruary 16.**.** T
cd2b0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
cd2c0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
cd2d0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
cd2e0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
cd2f0 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
cd300 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
cd310 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
cd320 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
cd330 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
cd340 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
cd350 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
cd360 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
cd370 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
cd380 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
cd390 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
cd3a0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
cd3b0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
cd3c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd3d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd3e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd3f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd400 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
cd410 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  file implements 
cd420 61 6e 20 6f 62 6a 65 63 74 20 74 68 61 74 20 72  an object that r
cd430 65 70 72 65 73 65 6e 74 73 20 61 20 66 69 78 65  epresents a fixe
cd440 64 2d 6c 65 6e 67 74 68 0a 2a 2a 20 62 69 74 6d  d-length.** bitm
cd450 61 70 2e 20 20 42 69 74 73 20 61 72 65 20 6e 75  ap.  Bits are nu
cd460 6d 62 65 72 65 64 20 73 74 61 72 74 69 6e 67 20  mbered starting 
cd470 77 69 74 68 20 31 2e 0a 2a 2a 0a 2a 2a 20 41 20  with 1..**.** A 
cd480 62 69 74 6d 61 70 20 69 73 20 75 73 65 64 20 74  bitmap is used t
cd490 6f 20 72 65 63 6f 72 64 20 77 68 61 74 20 70 61  o record what pa
cd4a0 67 65 73 20 61 20 64 61 74 61 62 61 73 65 20 66  ges a database f
cd4b0 69 6c 65 20 68 61 76 65 20 62 65 65 6e 0a 2a 2a  ile have been.**
cd4c0 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 64 75 72 69   journalled duri
cd4d0 6e 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  ng a transaction
cd4e0 2e 20 20 55 73 75 61 6c 6c 79 20 6f 6e 6c 79 20  .  Usually only 
cd4f0 61 20 66 65 77 20 70 61 67 65 73 20 61 72 65 0a  a few pages are.
cd500 2a 2a 20 6a 6f 75 72 6e 61 6c 6c 65 64 2e 20 20  ** journalled.  
cd510 53 6f 20 74 68 65 20 62 69 74 6d 61 70 20 69 73  So the bitmap is
cd520 20 75 73 75 61 6c 6c 79 20 73 70 61 72 73 65 20   usually sparse 
cd530 61 6e 64 20 68 61 73 20 6c 6f 77 20 63 61 72 64  and has low card
cd540 69 6e 61 6c 69 74 79 2e 0a 2a 2a 20 42 75 74 20  inality..** But 
cd550 73 6f 6d 65 74 69 6d 65 73 20 28 66 6f 72 20 65  sometimes (for e
cd560 78 61 6d 70 6c 65 20 77 68 65 6e 20 64 75 72 69  xample when duri
cd570 6e 67 20 61 20 44 52 4f 50 20 6f 66 20 61 20 6c  ng a DROP of a l
cd580 61 72 67 65 20 74 61 62 6c 65 29 20 6d 6f 73 74  arge table) most
cd590 0a 2a 2a 20 6f 72 20 61 6c 6c 20 6f 66 20 74 68  .** or all of th
cd5a0 65 20 70 61 67 65 73 20 67 65 74 20 6a 6f 75 72  e pages get jour
cd5b0 6e 61 6c 6c 65 64 2e 20 20 49 6e 20 74 68 6f 73  nalled.  In thos
cd5c0 65 20 63 61 73 65 73 2c 20 74 68 65 20 62 69 74  e cases, the bit
cd5d0 6d 61 70 20 62 65 63 6f 6d 65 73 0a 2a 2a 20 64  map becomes.** d
cd5e0 65 6e 73 65 2e 20 20 54 68 65 20 61 6c 67 6f 72  ense.  The algor
cd5f0 69 74 68 6d 20 6e 65 65 64 73 20 74 6f 20 68 61  ithm needs to ha
cd600 6e 64 6c 65 20 62 6f 74 68 20 63 61 73 65 73 20  ndle both cases 
cd610 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  well..**.** The 
cd620 73 69 7a 65 20 6f 66 20 74 68 65 20 62 69 74 6d  size of the bitm
cd630 61 70 20 69 73 20 66 69 78 65 64 20 77 68 65 6e  ap is fixed when
cd640 20 74 68 65 20 6f 62 6a 65 63 74 20 69 73 20 63   the object is c
cd650 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c  reated..**.** Al
cd660 6c 20 62 69 74 73 20 61 72 65 20 63 6c 65 61 72  l bits are clear
cd670 20 77 68 65 6e 20 74 68 65 20 62 69 74 6d 61 70   when the bitmap
cd680 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 49 6e   is created.  In
cd690 64 69 76 69 64 75 61 6c 20 62 69 74 73 0a 2a 2a  dividual bits.**
cd6a0 20 6d 61 79 20 62 65 20 73 65 74 20 6f 72 20 63   may be set or c
cd6b0 6c 65 61 72 65 64 20 6f 6e 65 20 61 74 20 61 20  leared one at a 
cd6c0 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 65 73 74  time..**.** Test
cd6d0 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20   operations are 
cd6e0 61 62 6f 75 74 20 31 30 30 20 74 69 6d 65 73 20  about 100 times 
cd6f0 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 20 74 68 61 74  more common that
cd700 20 73 65 74 20 6f 70 65 72 61 74 69 6f 6e 73 2e   set operations.
cd710 0a 2a 2a 20 43 6c 65 61 72 20 6f 70 65 72 61 74  .** Clear operat
cd720 69 6f 6e 73 20 61 72 65 20 65 78 63 65 65 64 69  ions are exceedi
cd730 6e 67 6c 79 20 72 61 72 65 2e 20 20 54 68 65 72  ngly rare.  Ther
cd740 65 20 61 72 65 20 75 73 75 61 6c 6c 79 20 62 65  e are usually be
cd750 74 77 65 65 6e 0a 2a 2a 20 35 20 61 6e 64 20 35  tween.** 5 and 5
cd760 30 30 20 73 65 74 20 6f 70 65 72 61 74 69 6f 6e  00 set operation
cd770 73 20 70 65 72 20 42 69 74 76 65 63 20 6f 62 6a  s per Bitvec obj
cd780 65 63 74 2c 20 74 68 6f 75 67 68 20 74 68 65 20  ect, though the 
cd790 6e 75 6d 62 65 72 20 6f 66 20 73 65 74 73 20 63  number of sets c
cd7a0 61 6e 0a 2a 2a 20 73 6f 6d 65 74 69 6d 65 73 20  an.** sometimes 
cd7b0 67 72 6f 77 20 69 6e 74 6f 20 74 65 6e 73 20 6f  grow into tens o
cd7c0 66 20 74 68 6f 75 73 61 6e 64 73 20 6f 72 20 6c  f thousands or l
cd7d0 61 72 67 65 72 2e 20 20 54 68 65 20 73 69 7a 65  arger.  The size
cd7e0 20 6f 66 20 74 68 65 0a 2a 2a 20 42 69 74 76 65   of the.** Bitve
cd7f0 63 20 6f 62 6a 65 63 74 20 69 73 20 74 68 65 20  c object is the 
cd800 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
cd810 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
cd820 66 69 6c 65 20 61 74 20 74 68 65 0a 2a 2a 20 73  file at the.** s
cd830 74 61 72 74 20 6f 66 20 61 20 74 72 61 6e 73 61  tart of a transa
cd840 63 74 69 6f 6e 2c 20 61 6e 64 20 69 73 20 74 68  ction, and is th
cd850 75 73 20 75 73 75 61 6c 6c 79 20 6c 65 73 73 20  us usually less 
cd860 74 68 61 6e 20 61 20 66 65 77 20 74 68 6f 75 73  than a few thous
cd870 61 6e 64 2c 0a 2a 2a 20 62 75 74 20 63 61 6e 20  and,.** but can 
cd880 62 65 20 61 73 20 6c 61 72 67 65 20 61 73 20 32  be as large as 2
cd890 20 62 69 6c 6c 69 6f 6e 20 66 6f 72 20 61 20 72   billion for a r
cd8a0 65 61 6c 6c 79 20 62 69 67 20 64 61 74 61 62 61  eally big databa
cd8b0 73 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24  se..**.** @(#) $
cd8c0 49 64 3a 20 62 69 74 76 65 63 2e 63 2c 76 20 31  Id: bitvec.c,v 1
cd8d0 2e 35 20 32 30 30 38 2f 30 35 2f 31 33 20 31 33  .5 2008/05/13 13
cd8e0 3a 32 37 3a 33 34 20 64 72 68 20 45 78 70 20 24  :27:34 drh Exp $
cd8f0 0a 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 42 49 54  .*/..#define BIT
cd900 56 45 43 5f 53 5a 20 20 20 20 20 20 20 20 35 31  VEC_SZ        51
cd910 32 0a 2f 2a 20 52 6f 75 6e 64 20 74 68 65 20 75  2./* Round the u
cd920 6e 69 6f 6e 20 73 69 7a 65 20 64 6f 77 6e 20 74  nion size down t
cd930 6f 20 74 68 65 20 6e 65 61 72 65 73 74 20 70 6f  o the nearest po
cd940 69 6e 74 65 72 20 62 6f 75 6e 64 61 72 79 2c 20  inter boundary, 
cd950 73 69 6e 63 65 20 74 68 61 74 27 73 20 68 6f 77  since that's how
cd960 20 0a 2a 2a 20 69 74 20 77 69 6c 6c 20 62 65 20   .** it will be 
cd970 61 6c 69 67 6e 65 64 20 77 69 74 68 69 6e 20 74  aligned within t
cd980 68 65 20 42 69 74 76 65 63 20 73 74 72 75 63 74  he Bitvec struct
cd990 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54  . */.#define BIT
cd9a0 56 45 43 5f 55 53 49 5a 45 20 20 20 20 20 28 28  VEC_USIZE     ((
cd9b0 28 42 49 54 56 45 43 5f 53 5a 2d 31 32 29 2f 73  (BITVEC_SZ-12)/s
cd9c0 69 7a 65 6f 66 28 42 69 74 76 65 63 2a 29 29 2a  izeof(Bitvec*))*
cd9d0 73 69 7a 65 6f 66 28 42 69 74 76 65 63 2a 29 29  sizeof(Bitvec*))
cd9e0 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f  .#define BITVEC_
cd9f0 4e 43 48 41 52 20 20 20 20 20 42 49 54 56 45 43  NCHAR     BITVEC
cda00 5f 55 53 49 5a 45 0a 23 64 65 66 69 6e 65 20 42  _USIZE.#define B
cda10 49 54 56 45 43 5f 4e 42 49 54 20 20 20 20 20 20  ITVEC_NBIT      
cda20 28 42 49 54 56 45 43 5f 4e 43 48 41 52 2a 38 29  (BITVEC_NCHAR*8)
cda30 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f  .#define BITVEC_
cda40 4e 49 4e 54 20 20 20 20 20 20 28 42 49 54 56 45  NINT      (BITVE
cda50 43 5f 55 53 49 5a 45 2f 34 29 0a 23 64 65 66 69  C_USIZE/4).#defi
cda60 6e 65 20 42 49 54 56 45 43 5f 4d 58 48 41 53 48  ne BITVEC_MXHASH
cda70 20 20 20 20 28 42 49 54 56 45 43 5f 4e 49 4e 54      (BITVEC_NINT
cda80 2f 32 29 0a 23 64 65 66 69 6e 65 20 42 49 54 56  /2).#define BITV
cda90 45 43 5f 4e 50 54 52 20 20 20 20 20 20 28 42 49  EC_NPTR      (BI
cdaa0 54 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 6f  TVEC_USIZE/sizeo
cdab0 66 28 42 69 74 76 65 63 20 2a 29 29 0a 0a 23 64  f(Bitvec *))..#d
cdac0 65 66 69 6e 65 20 42 49 54 56 45 43 5f 48 41 53  efine BITVEC_HAS
cdad0 48 28 58 29 20 20 20 28 28 28 58 29 2a 33 37 29  H(X)   (((X)*37)
cdae0 25 42 49 54 56 45 43 5f 4e 49 4e 54 29 0a 0a 2f  %BITVEC_NINT)../
cdaf0 2a 0a 2a 2a 20 41 20 62 69 74 6d 61 70 20 69 73  *.** A bitmap is
cdb00 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
cdb10 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
cdb20 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54  ructure..**.** T
cdb30 68 69 73 20 62 69 74 6d 61 70 20 72 65 63 6f 72  his bitmap recor
cdb40 64 73 20 74 68 65 20 65 78 69 73 74 61 6e 63 65  ds the existance
cdb50 20 6f 66 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65   of zero or more
cdb60 20 62 69 74 73 0a 2a 2a 20 77 69 74 68 20 76 61   bits.** with va
cdb70 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 20 61  lues between 1 a
cdb80 6e 64 20 69 53 69 7a 65 2c 20 69 6e 63 6c 75 73  nd iSize, inclus
cdb90 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  ive..**.** There
cdba0 20 61 72 65 20 74 68 72 65 65 20 70 6f 73 73 69   are three possi
cdbb0 62 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74 69  ble representati
cdbc0 6f 6e 73 20 6f 66 20 74 68 65 20 62 69 74 6d 61  ons of the bitma
cdbd0 70 2e 0a 2a 2a 20 49 66 20 69 53 69 7a 65 3c 3d  p..** If iSize<=
cdbe0 42 49 54 56 45 43 5f 4e 42 49 54 2c 20 74 68 65  BITVEC_NBIT, the
cdbf0 6e 20 42 69 74 76 65 63 2e 75 2e 61 42 69 74 6d  n Bitvec.u.aBitm
cdc00 61 70 5b 5d 20 69 73 20 61 20 73 74 72 61 69 67  ap[] is a straig
cdc10 68 74 0a 2a 2a 20 62 69 74 6d 61 70 2e 20 20 54  ht.** bitmap.  T
cdc20 68 65 20 6c 65 61 73 74 20 73 69 67 6e 69 66 69  he least signifi
cdc30 63 61 6e 74 20 62 69 74 20 69 73 20 62 69 74 20  cant bit is bit 
cdc40 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 53 69 7a  1..**.** If iSiz
cdc50 65 3e 42 49 54 56 45 43 5f 4e 42 49 54 20 61 6e  e>BITVEC_NBIT an
cdc60 64 20 69 44 69 76 69 73 6f 72 3d 3d 30 20 74 68  d iDivisor==0 th
cdc70 65 6e 20 42 69 74 76 65 63 2e 75 2e 61 48 61 73  en Bitvec.u.aHas
cdc80 68 5b 5d 20 69 73 0a 2a 2a 20 61 20 68 61 73 68  h[] is.** a hash
cdc90 20 74 61 62 6c 65 20 74 68 61 74 20 77 69 6c 6c   table that will
cdca0 20 68 6f 6c 64 20 75 70 20 74 6f 20 42 49 54 56   hold up to BITV
cdcb0 45 43 5f 4d 58 48 41 53 48 20 64 69 73 74 69 6e  EC_MXHASH distin
cdcc0 63 74 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a  ct values..**.**
cdcd0 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20   Otherwise, the 
cdce0 76 61 6c 75 65 20 69 20 69 73 20 72 65 64 69 72  value i is redir
cdcf0 65 63 74 65 64 20 69 6e 74 6f 20 6f 6e 65 20 6f  ected into one o
cdd00 66 20 42 49 54 56 45 43 5f 4e 50 54 52 0a 2a 2a  f BITVEC_NPTR.**
cdd10 20 73 75 62 2d 62 69 74 6d 61 70 73 20 70 6f 69   sub-bitmaps poi
cdd20 6e 74 65 64 20 74 6f 20 62 79 20 42 69 74 76 65  nted to by Bitve
cdd30 63 2e 75 2e 61 70 53 75 62 5b 5d 2e 20 20 45 61  c.u.apSub[].  Ea
cdd40 63 68 20 73 75 62 62 69 74 6d 61 70 0a 2a 2a 20  ch subbitmap.** 
cdd50 68 61 6e 64 6c 65 73 20 75 70 20 74 6f 20 69 44  handles up to iD
cdd60 69 76 69 73 6f 72 20 73 65 70 61 72 61 74 65 20  ivisor separate 
cdd70 76 61 6c 75 65 73 20 6f 66 20 69 2e 20 20 61 70  values of i.  ap
cdd80 53 75 62 5b 30 5d 20 68 6f 6c 64 73 0a 2a 2a 20  Sub[0] holds.** 
cdd90 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31  values between 1
cdda0 20 61 6e 64 20 69 44 69 76 69 73 6f 72 2e 20 20   and iDivisor.  
cddb0 61 70 53 75 62 5b 31 5d 20 68 6f 6c 64 73 20 76  apSub[1] holds v
cddc0 61 6c 75 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a  alues between.**
cddd0 20 69 44 69 76 69 73 6f 72 2b 31 20 61 6e 64 20   iDivisor+1 and 
cdde0 32 2a 69 44 69 76 69 73 6f 72 2e 20 20 61 70 53  2*iDivisor.  apS
cddf0 75 62 5b 4e 5d 20 68 6f 6c 64 73 20 76 61 6c 75  ub[N] holds valu
cde00 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 4e 2a  es between.** N*
cde10 69 44 69 76 69 73 6f 72 2b 31 20 61 6e 64 20 28  iDivisor+1 and (
cde20 4e 2b 31 29 2a 69 44 69 76 69 73 6f 72 2e 20 20  N+1)*iDivisor.  
cde30 45 61 63 68 20 73 75 62 62 69 74 6d 61 70 20 69  Each subbitmap i
cde40 73 20 6e 6f 72 6d 61 6c 69 7a 65 64 0a 2a 2a 20  s normalized.** 
cde50 74 6f 20 68 6f 6c 64 20 64 65 61 6c 20 77 69 74  to hold deal wit
cde60 68 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e  h values between
cde70 20 31 20 61 6e 64 20 69 44 69 76 69 73 6f 72 2e   1 and iDivisor.
cde80 0a 2a 2f 0a 73 74 72 75 63 74 20 42 69 74 76 65  .*/.struct Bitve
cde90 63 20 7b 0a 20 20 75 33 32 20 69 53 69 7a 65 3b  c {.  u32 iSize;
cdea0 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d        /* Maximum
cdeb0 20 62 69 74 20 69 6e 64 65 78 20 2a 2f 0a 20 20   bit index */.  
cdec0 75 33 32 20 6e 53 65 74 3b 20 20 20 20 20 20 20  u32 nSet;       
cded0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 69 74  /* Number of bit
cdee0 73 20 74 68 61 74 20 61 72 65 20 73 65 74 20 2a  s that are set *
cdef0 2f 0a 20 20 75 33 32 20 69 44 69 76 69 73 6f 72  /.  u32 iDivisor
cdf00 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66  ;   /* Number of
cdf10 20 62 69 74 73 20 68 61 6e 64 6c 65 64 20 62 79   bits handled by
cdf20 20 65 61 63 68 20 61 70 53 75 62 5b 5d 20 65 6e   each apSub[] en
cdf30 74 72 79 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b  try */.  union {
cdf40 0a 20 20 20 20 75 38 20 61 42 69 74 6d 61 70 5b  .    u8 aBitmap[
cdf50 42 49 54 56 45 43 5f 4e 43 48 41 52 5d 3b 20 20  BITVEC_NCHAR];  
cdf60 20 20 2f 2a 20 42 69 74 6d 61 70 20 72 65 70 72    /* Bitmap repr
cdf70 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20  esentation */.  
cdf80 20 20 75 33 32 20 61 48 61 73 68 5b 42 49 54 56    u32 aHash[BITV
cdf90 45 43 5f 4e 49 4e 54 5d 3b 20 20 20 20 20 20 2f  EC_NINT];      /
cdfa0 2a 20 48 61 73 68 20 74 61 62 6c 65 20 72 65 70  * Hash table rep
cdfb0 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20  resentation */. 
cdfc0 20 20 20 42 69 74 76 65 63 20 2a 61 70 53 75 62     Bitvec *apSub
cdfd0 5b 42 49 54 56 45 43 5f 4e 50 54 52 5d 3b 20 20  [BITVEC_NPTR];  
cdfe0 2f 2a 20 52 65 63 75 72 73 69 76 65 20 72 65 70  /* Recursive rep
cdff0 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20  resentation */. 
ce000 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20   } u;.};../*.** 
ce010 43 72 65 61 74 65 20 61 20 6e 65 77 20 62 69 74  Create a new bit
ce020 6d 61 70 20 6f 62 6a 65 63 74 20 61 62 6c 65 20  map object able 
ce030 74 6f 20 68 61 6e 64 6c 65 20 62 69 74 73 20 62  to handle bits b
ce040 65 74 77 65 65 6e 20 30 20 61 6e 64 20 69 53 69  etween 0 and iSi
ce050 7a 65 2c 0a 2a 2a 20 69 6e 63 6c 75 73 69 76 65  ze,.** inclusive
ce060 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  .  Return a poin
ce070 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 6f  ter to the new o
ce080 62 6a 65 63 74 2e 20 20 52 65 74 75 72 6e 20 4e  bject.  Return N
ce090 55 4c 4c 20 69 66 20 0a 2a 2a 20 6d 61 6c 6c 6f  ULL if .** mallo
ce0a0 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49  c fails..*/.SQLI
ce0b0 54 45 5f 50 52 49 56 41 54 45 20 42 69 74 76 65  TE_PRIVATE Bitve
ce0c0 63 20 2a 73 71 6c 69 74 65 33 42 69 74 76 65 63  c *sqlite3Bitvec
ce0d0 43 72 65 61 74 65 28 75 33 32 20 69 53 69 7a 65  Create(u32 iSize
ce0e0 29 7b 0a 20 20 42 69 74 76 65 63 20 2a 70 3b 0a  ){.  Bitvec *p;.
ce0f0 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
ce100 28 2a 70 29 3d 3d 42 49 54 56 45 43 5f 53 5a 20  (*p)==BITVEC_SZ 
ce110 29 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33  );.  p = sqlite3
ce120 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65  MallocZero( size
ce130 6f 66 28 2a 70 29 20 29 3b 0a 20 20 69 66 28 20  of(*p) );.  if( 
ce140 70 20 29 7b 0a 20 20 20 20 70 2d 3e 69 53 69 7a  p ){.    p->iSiz
ce150 65 20 3d 20 69 53 69 7a 65 3b 0a 20 20 7d 0a 20  e = iSize;.  }. 
ce160 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
ce170 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65  .** Check to see
ce180 20 69 66 20 74 68 65 20 69 2d 74 68 20 62 69 74   if the i-th bit
ce190 20 69 73 20 73 65 74 2e 20 20 52 65 74 75 72 6e   is set.  Return
ce1a0 20 74 72 75 65 20 6f 72 20 66 61 6c 73 65 2e 0a   true or false..
ce1b0 2a 2a 20 49 66 20 70 20 69 73 20 4e 55 4c 4c 20  ** If p is NULL 
ce1c0 28 69 66 20 74 68 65 20 62 69 74 6d 61 70 20 68  (if the bitmap h
ce1d0 61 73 20 6e 6f 74 20 62 65 65 6e 20 63 72 65 61  as not been crea
ce1e0 74 65 64 29 20 6f 72 20 69 66 0a 2a 2a 20 69 20  ted) or if.** i 
ce1f0 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c  is out of range,
ce200 20 74 68 65 6e 20 72 65 74 75 72 6e 20 66 61 6c   then return fal
ce210 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  se..*/.SQLITE_PR
ce220 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
ce230 33 42 69 74 76 65 63 54 65 73 74 28 42 69 74 76  3BitvecTest(Bitv
ce240 65 63 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20  ec *p, u32 i){. 
ce250 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
ce260 72 6e 20 30 3b 0a 20 20 69 66 28 20 69 3e 70 2d  rn 0;.  if( i>p-
ce270 3e 69 53 69 7a 65 20 7c 7c 20 69 3d 3d 30 20 29  >iSize || i==0 )
ce280 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28   return 0;.  if(
ce290 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45   p->iSize<=BITVE
ce2a0 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20 20 69 2d  C_NBIT ){.    i-
ce2b0 2d 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 70  -;.    return (p
ce2c0 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f 38 5d  ->u.aBitmap[i/8]
ce2d0 20 26 20 28 31 3c 3c 28 69 26 37 29 29 29 21 3d   & (1<<(i&7)))!=
ce2e0 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  0;.  }.  if( p->
ce2f0 69 44 69 76 69 73 6f 72 3e 30 20 29 7b 0a 20 20  iDivisor>0 ){.  
ce300 20 20 75 33 32 20 62 69 6e 20 3d 20 28 69 2d 31    u32 bin = (i-1
ce310 29 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20  )/p->iDivisor;. 
ce320 20 20 20 69 20 3d 20 28 69 2d 31 29 25 70 2d 3e     i = (i-1)%p->
ce330 69 44 69 76 69 73 6f 72 20 2b 20 31 3b 0a 20 20  iDivisor + 1;.  
ce340 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
ce350 42 69 74 76 65 63 54 65 73 74 28 70 2d 3e 75 2e  BitvecTest(p->u.
ce360 61 70 53 75 62 5b 62 69 6e 5d 2c 20 69 29 3b 0a  apSub[bin], i);.
ce370 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 33 32    }else{.    u32
ce380 20 68 20 3d 20 42 49 54 56 45 43 5f 48 41 53 48   h = BITVEC_HASH
ce390 28 69 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  (i);.    while( 
ce3a0 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 7b  p->u.aHash[h] ){
ce3b0 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 75 2e  .      if( p->u.
ce3c0 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29 20 72 65  aHash[h]==i ) re
ce3d0 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 68 2b  turn 1;.      h+
ce3e0 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 68 3e 3d  +;.      if( h>=
ce3f0 42 49 54 56 45 43 5f 4e 49 4e 54 20 29 20 68 20  BITVEC_NINT ) h 
ce400 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  = 0;.    }.    r
ce410 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a  eturn 0;.  }.}..
ce420 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 69 2d  /*.** Set the i-
ce430 74 68 20 62 69 74 2e 20 20 52 65 74 75 72 6e 20  th bit.  Return 
ce440 30 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64  0 on success and
ce450 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69   an error code i
ce460 66 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 67 6f  f.** anything go
ce470 65 73 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 53 51 4c  es wrong..*/.SQL
ce480 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
ce490 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74  sqlite3BitvecSet
ce4a0 28 42 69 74 76 65 63 20 2a 70 2c 20 75 33 32 20  (Bitvec *p, u32 
ce4b0 69 29 7b 0a 20 20 75 33 32 20 68 3b 0a 20 20 61  i){.  u32 h;.  a
ce4c0 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20  ssert( p!=0 );. 
ce4d0 20 61 73 73 65 72 74 28 20 69 3e 30 20 29 3b 0a   assert( i>0 );.
ce4e0 20 20 61 73 73 65 72 74 28 20 69 3c 3d 70 2d 3e    assert( i<=p->
ce4f0 69 53 69 7a 65 20 29 3b 0a 20 20 69 66 28 20 70  iSize );.  if( p
ce500 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f  ->iSize<=BITVEC_
ce510 4e 42 49 54 20 29 7b 0a 20 20 20 20 69 2d 2d 3b  NBIT ){.    i--;
ce520 0a 20 20 20 20 70 2d 3e 75 2e 61 42 69 74 6d 61  .    p->u.aBitma
ce530 70 5b 69 2f 38 5d 20 7c 3d 20 31 20 3c 3c 20 28  p[i/8] |= 1 << (
ce540 69 26 37 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  i&7);.    return
ce550 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
ce560 20 20 69 66 28 20 70 2d 3e 69 44 69 76 69 73 6f    if( p->iDiviso
ce570 72 20 29 7b 0a 20 20 20 20 75 33 32 20 62 69 6e  r ){.    u32 bin
ce580 20 3d 20 28 69 2d 31 29 2f 70 2d 3e 69 44 69 76   = (i-1)/p->iDiv
ce590 69 73 6f 72 3b 0a 20 20 20 20 69 20 3d 20 28 69  isor;.    i = (i
ce5a0 2d 31 29 25 70 2d 3e 69 44 69 76 69 73 6f 72 20  -1)%p->iDivisor 
ce5b0 2b 20 31 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  + 1;.    if( p->
ce5c0 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 20  u.apSub[bin]==0 
ce5d0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
ce5e0 46 61 75 6c 74 42 65 67 69 6e 42 65 6e 69 67 6e  FaultBeginBenign
ce5f0 28 53 51 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a  (SQLITE_FAULTINJ
ce600 45 43 54 4f 52 5f 4d 41 4c 4c 4f 43 29 3b 0a 20  ECTOR_MALLOC);. 
ce610 20 20 20 20 20 70 2d 3e 75 2e 61 70 53 75 62 5b       p->u.apSub[
ce620 62 69 6e 5d 20 3d 20 73 71 6c 69 74 65 33 42 69  bin] = sqlite3Bi
ce630 74 76 65 63 43 72 65 61 74 65 28 20 70 2d 3e 69  tvecCreate( p->i
ce640 44 69 76 69 73 6f 72 20 29 3b 0a 20 20 20 20 20  Divisor );.     
ce650 20 73 71 6c 69 74 65 33 46 61 75 6c 74 45 6e 64   sqlite3FaultEnd
ce660 42 65 6e 69 67 6e 28 53 51 4c 49 54 45 5f 46 41  Benign(SQLITE_FA
ce670 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 4d 41 4c 4c  ULTINJECTOR_MALL
ce680 4f 43 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  OC);.      if( p
ce690 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3d 3d  ->u.apSub[bin]==
ce6a0 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  0 ) return SQLIT
ce6b0 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20  E_NOMEM;.    }. 
ce6c0 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65     return sqlite
ce6d0 33 42 69 74 76 65 63 53 65 74 28 70 2d 3e 75 2e  3BitvecSet(p->u.
ce6e0 61 70 53 75 62 5b 62 69 6e 5d 2c 20 69 29 3b 0a  apSub[bin], i);.
ce6f0 20 20 7d 0a 20 20 68 20 3d 20 42 49 54 56 45 43    }.  h = BITVEC
ce700 5f 48 41 53 48 28 69 29 3b 0a 20 20 77 68 69 6c  _HASH(i);.  whil
ce710 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d  e( p->u.aHash[h]
ce720 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75   ){.    if( p->u
ce730 2e 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29 20 72  .aHash[h]==i ) r
ce740 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
ce750 0a 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20 69 66  .    h++;.    if
ce760 28 20 68 3d 3d 42 49 54 56 45 43 5f 4e 49 4e 54  ( h==BITVEC_NINT
ce770 20 29 20 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 20   ) h = 0;.  }.  
ce780 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20 69 66 28  p->nSet++;.  if(
ce790 20 70 2d 3e 6e 53 65 74 3e 3d 42 49 54 56 45 43   p->nSet>=BITVEC
ce7a0 5f 4d 58 48 41 53 48 20 29 7b 0a 20 20 20 20 69  _MXHASH ){.    i
ce7b0 6e 74 20 6a 2c 20 72 63 3b 0a 20 20 20 20 75 33  nt j, rc;.    u3
ce7c0 32 20 61 69 56 61 6c 75 65 73 5b 42 49 54 56 45  2 aiValues[BITVE
ce7d0 43 5f 4e 49 4e 54 5d 3b 0a 20 20 20 20 6d 65 6d  C_NINT];.    mem
ce7e0 63 70 79 28 61 69 56 61 6c 75 65 73 2c 20 70 2d  cpy(aiValues, p-
ce7f0 3e 75 2e 61 48 61 73 68 2c 20 73 69 7a 65 6f 66  >u.aHash, sizeof
ce800 28 61 69 56 61 6c 75 65 73 29 29 3b 0a 20 20 20  (aiValues));.   
ce810 20 6d 65 6d 73 65 74 28 70 2d 3e 75 2e 61 70 53   memset(p->u.apS
ce820 75 62 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 2d  ub, 0, sizeof(p-
ce830 3e 75 2e 61 70 53 75 62 5b 30 5d 29 2a 42 49 54  >u.apSub[0])*BIT
ce840 56 45 43 5f 4e 50 54 52 29 3b 0a 20 20 20 20 70  VEC_NPTR);.    p
ce850 2d 3e 69 44 69 76 69 73 6f 72 20 3d 20 28 70 2d  ->iDivisor = (p-
ce860 3e 69 53 69 7a 65 20 2b 20 42 49 54 56 45 43 5f  >iSize + BITVEC_
ce870 4e 50 54 52 20 2d 20 31 29 2f 42 49 54 56 45 43  NPTR - 1)/BITVEC
ce880 5f 4e 50 54 52 3b 0a 20 20 20 20 72 63 20 3d 20  _NPTR;.    rc = 
ce890 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74  sqlite3BitvecSet
ce8a0 28 70 2c 20 69 29 3b 0a 20 20 20 20 66 6f 72 28  (p, i);.    for(
ce8b0 6a 3d 30 3b 20 6a 3c 42 49 54 56 45 43 5f 4e 49  j=0; j<BITVEC_NI
ce8c0 4e 54 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  NT; j++){.      
ce8d0 69 66 28 20 61 69 56 61 6c 75 65 73 5b 6a 5d 20  if( aiValues[j] 
ce8e0 29 20 72 63 20 7c 3d 20 73 71 6c 69 74 65 33 42  ) rc |= sqlite3B
ce8f0 69 74 76 65 63 53 65 74 28 70 2c 20 61 69 56 61  itvecSet(p, aiVa
ce900 6c 75 65 73 5b 6a 5d 29 3b 0a 20 20 20 20 7d 0a  lues[j]);.    }.
ce910 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
ce920 20 7d 0a 20 20 70 2d 3e 75 2e 61 48 61 73 68 5b   }.  p->u.aHash[
ce930 68 5d 20 3d 20 69 3b 0a 20 20 72 65 74 75 72 6e  h] = i;.  return
ce940 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
ce950 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 69  *.** Clear the i
ce960 2d 74 68 20 62 69 74 2e 20 20 52 65 74 75 72 6e  -th bit.  Return
ce970 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e   0 on success an
ce980 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  d an error code 
ce990 69 66 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 67  if.** anything g
ce9a0 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 53 51  oes wrong..*/.SQ
ce9b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
ce9c0 64 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43  d sqlite3BitvecC
ce9d0 6c 65 61 72 28 42 69 74 76 65 63 20 2a 70 2c 20  lear(Bitvec *p, 
ce9e0 75 33 32 20 69 29 7b 0a 20 20 61 73 73 65 72 74  u32 i){.  assert
ce9f0 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  ( p!=0 );.  asse
cea00 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 69 66 28  rt( i>0 );.  if(
cea10 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45   p->iSize<=BITVE
cea20 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20 20 69 2d  C_NBIT ){.    i-
cea30 2d 3b 0a 20 20 20 20 70 2d 3e 75 2e 61 42 69 74  -;.    p->u.aBit
cea40 6d 61 70 5b 69 2f 38 5d 20 26 3d 20 7e 28 31 20  map[i/8] &= ~(1 
cea50 3c 3c 20 28 69 26 37 29 29 3b 0a 20 20 7d 65 6c  << (i&7));.  }el
cea60 73 65 20 69 66 28 20 70 2d 3e 69 44 69 76 69 73  se if( p->iDivis
cea70 6f 72 20 29 7b 0a 20 20 20 20 75 33 32 20 62 69  or ){.    u32 bi
cea80 6e 20 3d 20 28 69 2d 31 29 2f 70 2d 3e 69 44 69  n = (i-1)/p->iDi
cea90 76 69 73 6f 72 3b 0a 20 20 20 20 69 20 3d 20 28  visor;.    i = (
ceaa0 69 2d 31 29 25 70 2d 3e 69 44 69 76 69 73 6f 72  i-1)%p->iDivisor
ceab0 20 2b 20 31 3b 0a 20 20 20 20 69 66 28 20 70 2d   + 1;.    if( p-
ceac0 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 20 29 7b  >u.apSub[bin] ){
cead0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 69  .      sqlite3Bi
ceae0 74 76 65 63 43 6c 65 61 72 28 70 2d 3e 75 2e 61  tvecClear(p->u.a
ceaf0 70 53 75 62 5b 62 69 6e 5d 2c 20 69 29 3b 0a 20  pSub[bin], i);. 
ceb00 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
ceb10 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 75 33 32    int j;.    u32
ceb20 20 61 69 56 61 6c 75 65 73 5b 42 49 54 56 45 43   aiValues[BITVEC
ceb30 5f 4e 49 4e 54 5d 3b 0a 20 20 20 20 6d 65 6d 63  _NINT];.    memc
ceb40 70 79 28 61 69 56 61 6c 75 65 73 2c 20 70 2d 3e  py(aiValues, p->
ceb50 75 2e 61 48 61 73 68 2c 20 73 69 7a 65 6f 66 28  u.aHash, sizeof(
ceb60 61 69 56 61 6c 75 65 73 29 29 3b 0a 20 20 20 20  aiValues));.    
ceb70 6d 65 6d 73 65 74 28 70 2d 3e 75 2e 61 48 61 73  memset(p->u.aHas
ceb80 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e  h, 0, sizeof(p->
ceb90 75 2e 61 48 61 73 68 5b 30 5d 29 2a 42 49 54 56  u.aHash[0])*BITV
ceba0 45 43 5f 4e 49 4e 54 29 3b 0a 20 20 20 20 70 2d  EC_NINT);.    p-
cebb0 3e 6e 53 65 74 20 3d 20 30 3b 0a 20 20 20 20 66  >nSet = 0;.    f
cebc0 6f 72 28 6a 3d 30 3b 20 6a 3c 42 49 54 56 45 43  or(j=0; j<BITVEC
cebd0 5f 4e 49 4e 54 3b 20 6a 2b 2b 29 7b 0a 20 20 20  _NINT; j++){.   
cebe0 20 20 20 69 66 28 20 61 69 56 61 6c 75 65 73 5b     if( aiValues[
cebf0 6a 5d 20 26 26 20 61 69 56 61 6c 75 65 73 5b 6a  j] && aiValues[j
cec00 5d 21 3d 69 20 29 7b 0a 20 20 20 20 20 20 20 20  ]!=i ){.        
cec10 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74  sqlite3BitvecSet
cec20 28 70 2c 20 61 69 56 61 6c 75 65 73 5b 6a 5d 29  (p, aiValues[j])
cec30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
cec40 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73    }.}../*.** Des
cec50 74 72 6f 79 20 61 20 62 69 74 6d 61 70 20 6f 62  troy a bitmap ob
cec60 6a 65 63 74 2e 20 20 52 65 63 6c 61 69 6d 20 61  ject.  Reclaim a
cec70 6c 6c 20 6d 65 6d 6f 72 79 20 75 73 65 64 2e 0a  ll memory used..
cec80 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
cec90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69  E void sqlite3Bi
ceca0 74 76 65 63 44 65 73 74 72 6f 79 28 42 69 74 76  tvecDestroy(Bitv
cecb0 65 63 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d  ec *p){.  if( p=
cecc0 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  =0 ) return;.  i
cecd0 66 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29  f( p->iDivisor )
cece0 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  {.    int i;.   
cecf0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 42 49 54 56   for(i=0; i<BITV
ced00 45 43 5f 4e 50 54 52 3b 20 69 2b 2b 29 7b 0a 20  EC_NPTR; i++){. 
ced10 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76       sqlite3Bitv
ced20 65 63 44 65 73 74 72 6f 79 28 70 2d 3e 75 2e 61  ecDestroy(p->u.a
ced30 70 53 75 62 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a  pSub[i]);.    }.
ced40 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72    }.  sqlite3_fr
ced50 65 65 28 70 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65  ee(p);.}..#ifnde
ced60 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55  f SQLITE_OMIT_BU
ced70 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a  ILTIN_TEST./*.**
ced80 20 4c 65 74 20 56 5b 5d 20 62 65 20 61 6e 20 61   Let V[] be an a
ced90 72 72 61 79 20 6f 66 20 75 6e 73 69 67 6e 65 64  rray of unsigned
ceda0 20 63 68 61 72 61 63 74 65 72 73 20 73 75 66 66   characters suff
cedb0 69 63 69 65 6e 74 20 74 6f 20 68 6f 6c 64 0a 2a  icient to hold.*
cedc0 2a 20 75 70 20 74 6f 20 4e 20 62 69 74 73 2e 20  * up to N bits. 
cedd0 20 4c 65 74 20 49 20 62 65 20 61 6e 20 69 6e 74   Let I be an int
cede0 65 67 65 72 20 62 65 74 77 65 65 6e 20 30 20 61  eger between 0 a
cedf0 6e 64 20 4e 2e 20 20 30 3c 3d 49 3c 4e 2e 0a 2a  nd N.  0<=I<N..*
cee00 2a 20 54 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f  * Then the follo
cee10 77 69 6e 67 20 6d 61 63 72 6f 73 20 63 61 6e 20  wing macros can 
cee20 62 65 20 75 73 65 64 20 74 6f 20 73 65 74 2c 20  be used to set, 
cee30 63 6c 65 61 72 2c 20 6f 72 20 74 65 73 74 0a 2a  clear, or test.*
cee40 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 62 69 74  * individual bit
cee50 73 20 77 69 74 68 69 6e 20 56 2e 0a 2a 2f 0a 23  s within V..*/.#
cee60 64 65 66 69 6e 65 20 53 45 54 42 49 54 28 56 2c  define SETBIT(V,
cee70 49 29 20 20 20 20 20 20 56 5b 49 3e 3e 33 5d 20  I)      V[I>>3] 
cee80 7c 3d 20 28 31 3c 3c 28 49 26 37 29 29 0a 23 64  |= (1<<(I&7)).#d
cee90 65 66 69 6e 65 20 43 4c 45 41 52 42 49 54 28 56  efine CLEARBIT(V
ceea0 2c 49 29 20 20 20 20 56 5b 49 3e 3e 33 5d 20 26  ,I)    V[I>>3] &
ceeb0 3d 20 7e 28 31 3c 3c 28 49 26 37 29 29 0a 23 64  = ~(1<<(I&7)).#d
ceec0 65 66 69 6e 65 20 54 45 53 54 42 49 54 28 56 2c  efine TESTBIT(V,
ceed0 49 29 20 20 20 20 20 28 56 5b 49 3e 3e 33 5d 26  I)     (V[I>>3]&
ceee0 28 31 3c 3c 28 49 26 37 29 29 29 21 3d 30 0a 0a  (1<<(I&7)))!=0..
ceef0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
cef00 6e 65 20 72 75 6e 73 20 61 6e 20 65 78 74 65 6e  ne runs an exten
cef10 73 69 76 65 20 74 65 73 74 20 6f 66 20 74 68 65  sive test of the
cef20 20 42 69 74 76 65 63 20 63 6f 64 65 2e 0a 2a 2a   Bitvec code..**
cef30 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 69 73  .** The input is
cef40 20 61 6e 20 61 72 72 61 79 20 6f 66 20 69 6e 74   an array of int
cef50 65 67 65 72 73 20 74 68 61 74 20 61 63 74 73 20  egers that acts 
cef60 61 73 20 61 20 70 72 6f 67 72 61 6d 0a 2a 2a 20  as a program.** 
cef70 74 6f 20 74 65 73 74 20 74 68 65 20 42 69 74 76  to test the Bitv
cef80 65 63 2e 20 20 54 68 65 20 69 6e 74 65 67 65 72  ec.  The integer
cef90 73 20 61 72 65 20 6f 70 63 6f 64 65 73 20 66 6f  s are opcodes fo
cefa0 6c 6c 6f 77 65 64 0a 2a 2a 20 62 79 20 30 2c 20  llowed.** by 0, 
cefb0 31 2c 20 6f 72 20 33 20 6f 70 65 72 61 6e 64 73  1, or 3 operands
cefc0 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74  , depending on t
cefd0 68 65 20 6f 70 63 6f 64 65 2e 20 20 41 6e 6f 74  he opcode.  Anot
cefe0 68 65 72 0a 2a 2a 20 6f 70 63 6f 64 65 20 66 6f  her.** opcode fo
ceff0 6c 6c 6f 77 73 20 69 6d 6d 65 64 69 61 74 65 6c  llows immediatel
cf000 79 20 61 66 74 65 72 20 74 68 65 20 6c 61 73 74  y after the last
cf010 20 6f 70 65 72 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20   operand..**.** 
cf020 54 68 65 72 65 20 61 72 65 20 36 20 6f 70 63 6f  There are 6 opco
cf030 64 65 73 20 6e 75 6d 62 65 72 65 64 20 66 72 6f  des numbered fro
cf040 6d 20 30 20 74 68 72 6f 75 67 68 20 35 2e 20 20  m 0 through 5.  
cf050 30 20 69 73 20 74 68 65 0a 2a 2a 20 22 68 61 6c  0 is the.** "hal
cf060 74 22 20 6f 70 63 6f 64 65 20 61 6e 64 20 63 61  t" opcode and ca
cf070 75 73 65 73 20 74 68 65 20 74 65 73 74 20 74 6f  uses the test to
cf080 20 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30   end..**.**    0
cf090 20 20 20 20 20 20 20 20 20 20 48 61 6c 74 20 61            Halt a
cf0a0 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75  nd return the nu
cf0b0 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 0a 2a  mber of errors.*
cf0c0 2a 20 20 20 20 31 20 4e 20 53 20 58 20 20 20 20  *    1 N S X    
cf0d0 53 65 74 20 4e 20 62 69 74 73 20 62 65 67 69 6e  Set N bits begin
cf0e0 6e 69 6e 67 20 77 69 74 68 20 53 20 61 6e 64 20  ning with S and 
cf0f0 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 62 79 20  incrementing by 
cf100 58 0a 2a 2a 20 20 20 20 32 20 4e 20 53 20 58 20  X.**    2 N S X 
cf110 20 20 20 43 6c 65 61 72 20 4e 20 62 69 74 73 20     Clear N bits 
cf120 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53  beginning with S
cf130 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 69 6e   and incrementin
cf140 67 20 62 79 20 58 0a 2a 2a 20 20 20 20 33 20 4e  g by X.**    3 N
cf150 20 20 20 20 20 20 20 20 53 65 74 20 4e 20 72 61          Set N ra
cf160 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 69  ndomly chosen bi
cf170 74 73 0a 2a 2a 20 20 20 20 34 20 4e 20 20 20 20  ts.**    4 N    
cf180 20 20 20 20 43 6c 65 61 72 20 4e 20 72 61 6e 64      Clear N rand
cf190 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 69 74 73  omly chosen bits
cf1a0 0a 2a 2a 20 20 20 20 35 20 4e 20 53 20 58 20 20  .**    5 N S X  
cf1b0 20 20 53 65 74 20 4e 20 62 69 74 73 20 66 72 6f    Set N bits fro
cf1c0 6d 20 53 20 69 6e 63 72 65 6d 65 6e 74 20 58 20  m S increment X 
cf1d0 69 6e 20 61 72 72 61 79 20 6f 6e 6c 79 2c 20 6e  in array only, n
cf1e0 6f 74 20 69 6e 20 62 69 74 76 65 63 0a 2a 2a 0a  ot in bitvec.**.
cf1f0 2a 2a 20 54 68 65 20 6f 70 63 6f 64 65 73 20 31  ** The opcodes 1
cf200 20 74 68 72 6f 75 67 68 20 34 20 70 65 72 66 6f   through 4 perfo
cf210 72 6d 20 73 65 74 20 61 6e 64 20 63 6c 65 61 72  rm set and clear
cf220 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20   operations are 
cf230 70 65 72 66 6f 72 6d 65 64 0a 2a 2a 20 6f 6e 20  performed.** on 
cf240 62 6f 74 68 20 61 20 42 69 74 76 65 63 20 6f 62  both a Bitvec ob
cf250 6a 65 63 74 20 61 6e 64 20 6f 6e 20 61 20 6c 69  ject and on a li
cf260 6e 65 61 72 20 61 72 72 61 79 20 6f 66 20 62 69  near array of bi
cf270 74 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ts obtained from
cf280 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 20 4f 70 63 6f   malloc..** Opco
cf290 64 65 20 35 20 77 6f 72 6b 73 20 6f 6e 20 74 68  de 5 works on th
cf2a0 65 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 6f  e linear array o
cf2b0 6e 6c 79 2c 20 6e 6f 74 20 6f 6e 20 74 68 65 20  nly, not on the 
cf2c0 42 69 74 76 65 63 2e 0a 2a 2a 20 4f 70 63 6f 64  Bitvec..** Opcod
cf2d0 65 20 35 20 69 73 20 75 73 65 64 20 74 6f 20 64  e 5 is used to d
cf2e0 65 6c 69 62 65 72 61 74 65 6c 79 20 69 6e 64 75  eliberately indu
cf2f0 63 65 20 61 20 66 61 75 6c 74 20 69 6e 20 6f 72  ce a fault in or
cf300 64 65 72 20 74 6f 0a 2a 2a 20 63 6f 6e 66 69 72  der to.** confir
cf310 6d 20 74 68 61 74 20 65 72 72 6f 72 20 64 65 74  m that error det
cf320 65 63 74 69 6f 6e 20 77 6f 72 6b 73 2e 0a 2a 2a  ection works..**
cf330 0a 2a 2a 20 41 74 20 74 68 65 20 63 6f 6e 63 6c  .** At the concl
cf340 75 73 69 6f 6e 20 6f 66 20 74 68 65 20 74 65 73  usion of the tes
cf350 74 20 74 68 65 20 6c 69 6e 65 61 72 20 61 72 72  t the linear arr
cf360 61 79 20 69 73 20 63 6f 6d 70 61 72 65 64 0a 2a  ay is compared.*
cf370 2a 20 61 67 61 69 6e 73 74 20 74 68 65 20 42 69  * against the Bi
cf380 74 76 65 63 20 6f 62 6a 65 63 74 2e 20 20 49 66  tvec object.  If
cf390 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 64   there are any d
cf3a0 69 66 66 65 72 65 6e 63 65 73 2c 0a 2a 2a 20 61  ifferences,.** a
cf3b0 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72  n error is retur
cf3c0 6e 65 64 2e 20 20 49 66 20 74 68 65 79 20 61 72  ned.  If they ar
cf3d0 65 20 74 68 65 20 73 61 6d 65 2c 20 7a 65 72 6f  e the same, zero
cf3e0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
cf3f0 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20  .** If a memory 
cf400 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72  allocation error
cf410 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20   occurs, return 
cf420 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  -1..*/.SQLITE_PR
cf430 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
cf440 33 42 69 74 76 65 63 42 75 69 6c 74 69 6e 54 65  3BitvecBuiltinTe
cf450 73 74 28 69 6e 74 20 73 7a 2c 20 69 6e 74 20 2a  st(int sz, int *
cf460 61 4f 70 29 7b 0a 20 20 42 69 74 76 65 63 20 2a  aOp){.  Bitvec *
cf470 70 42 69 74 76 65 63 20 3d 20 30 3b 0a 20 20 75  pBitvec = 0;.  u
cf480 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 56  nsigned char *pV
cf490 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d   = 0;.  int rc =
cf4a0 20 2d 31 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 78   -1;.  int i, nx
cf4b0 2c 20 70 63 2c 20 6f 70 3b 0a 0a 20 20 2f 2a 20  , pc, op;..  /* 
cf4c0 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 42 69 74  Allocate the Bit
cf4d0 76 65 63 20 74 6f 20 62 65 20 74 65 73 74 65 64  vec to be tested
cf4e0 20 61 6e 64 20 61 20 6c 69 6e 65 61 72 20 61 72   and a linear ar
cf4f0 72 61 79 20 6f 66 0a 20 20 2a 2a 20 62 69 74 73  ray of.  ** bits
cf500 20 74 6f 20 61 63 74 20 61 73 20 74 68 65 20 72   to act as the r
cf510 65 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 70 42  eference */.  pB
cf520 69 74 76 65 63 20 3d 20 73 71 6c 69 74 65 33 42  itvec = sqlite3B
cf530 69 74 76 65 63 43 72 65 61 74 65 28 20 73 7a 20  itvecCreate( sz 
cf540 29 3b 0a 20 20 70 56 20 3d 20 73 71 6c 69 74 65  );.  pV = sqlite
cf550 33 5f 6d 61 6c 6c 6f 63 28 20 28 73 7a 2b 37 29  3_malloc( (sz+7)
cf560 2f 38 20 2b 20 31 20 29 3b 0a 20 20 69 66 28 20  /8 + 1 );.  if( 
cf570 70 42 69 74 76 65 63 3d 3d 30 20 7c 7c 20 70 56  pBitvec==0 || pV
cf580 3d 3d 30 20 29 20 67 6f 74 6f 20 62 69 74 76 65  ==0 ) goto bitve
cf590 63 5f 65 6e 64 3b 0a 20 20 6d 65 6d 73 65 74 28  c_end;.  memset(
cf5a0 70 56 2c 20 30 2c 20 28 73 7a 2b 37 29 2f 38 20  pV, 0, (sz+7)/8 
cf5b0 2b 20 31 29 3b 0a 0a 20 20 2f 2a 20 52 75 6e 20  + 1);..  /* Run 
cf5c0 74 68 65 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20  the program */. 
cf5d0 20 70 63 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65   pc = 0;.  while
cf5e0 28 20 28 6f 70 20 3d 20 61 4f 70 5b 70 63 5d 29  ( (op = aOp[pc])
cf5f0 21 3d 30 20 29 7b 0a 20 20 20 20 73 77 69 74 63  !=0 ){.    switc
cf600 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 20 20 63  h( op ){.      c
cf610 61 73 65 20 31 3a 0a 20 20 20 20 20 20 63 61 73  ase 1:.      cas
cf620 65 20 32 3a 0a 20 20 20 20 20 20 63 61 73 65 20  e 2:.      case 
cf630 35 3a 20 7b 0a 20 20 20 20 20 20 20 20 6e 78 20  5: {.        nx 
cf640 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 69 20 3d  = 4;.        i =
cf650 20 61 4f 70 5b 70 63 2b 32 5d 20 2d 20 31 3b 0a   aOp[pc+2] - 1;.
cf660 20 20 20 20 20 20 20 20 61 4f 70 5b 70 63 2b 32          aOp[pc+2
cf670 5d 20 2b 3d 20 61 4f 70 5b 70 63 2b 33 5d 3b 0a  ] += aOp[pc+3];.
cf680 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
cf690 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
cf6a0 65 20 33 3a 0a 20 20 20 20 20 20 63 61 73 65 20  e 3:.      case 
cf6b0 34 3a 20 0a 20 20 20 20 20 20 64 65 66 61 75 6c  4: .      defaul
cf6c0 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 6e 78 20  t: {.        nx 
cf6d0 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  = 2;.        sql
cf6e0 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28  ite3_randomness(
cf6f0 73 69 7a 65 6f 66 28 69 29 2c 20 26 69 29 3b 0a  sizeof(i), &i);.
cf700 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
cf710 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
cf720 20 69 66 28 20 28 2d 2d 61 4f 70 5b 70 63 2b 31   if( (--aOp[pc+1
cf730 5d 29 20 3e 20 30 20 29 20 6e 78 20 3d 20 30 3b  ]) > 0 ) nx = 0;
cf740 0a 20 20 20 20 70 63 20 2b 3d 20 6e 78 3b 0a 20  .    pc += nx;. 
cf750 20 20 20 69 20 3d 20 28 69 20 26 20 30 78 37 66     i = (i & 0x7f
cf760 66 66 66 66 66 66 29 25 73 7a 3b 0a 20 20 20 20  ffffff)%sz;.    
cf770 69 66 28 20 28 6f 70 20 26 20 31 29 21 3d 30 20  if( (op & 1)!=0 
cf780 29 7b 0a 20 20 20 20 20 20 53 45 54 42 49 54 28  ){.      SETBIT(
cf790 70 56 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20 20  pV, (i+1));.    
cf7a0 20 20 69 66 28 20 6f 70 21 3d 35 20 29 7b 0a 20    if( op!=5 ){. 
cf7b0 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
cf7c0 65 33 42 69 74 76 65 63 53 65 74 28 70 42 69 74  e3BitvecSet(pBit
cf7d0 76 65 63 2c 20 69 2b 31 29 20 29 20 67 6f 74 6f  vec, i+1) ) goto
cf7e0 20 62 69 74 76 65 63 5f 65 6e 64 3b 0a 20 20 20   bitvec_end;.   
cf7f0 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
cf800 20 20 20 20 20 20 43 4c 45 41 52 42 49 54 28 70        CLEARBIT(p
cf810 56 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 20  V, (i+1));.     
cf820 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 6c   sqlite3BitvecCl
cf830 65 61 72 28 70 42 69 74 76 65 63 2c 20 69 2b 31  ear(pBitvec, i+1
cf840 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
cf850 2f 2a 20 54 65 73 74 20 74 6f 20 6d 61 6b 65 20  /* Test to make 
cf860 73 75 72 65 20 74 68 65 20 6c 69 6e 65 61 72 20  sure the linear 
cf870 61 72 72 61 79 20 65 78 61 63 74 6c 79 20 6d 61  array exactly ma
cf880 74 63 68 65 73 20 74 68 65 0a 20 20 2a 2a 20 42  tches the.  ** B
cf890 69 74 76 65 63 20 6f 62 6a 65 63 74 2e 20 20 53  itvec object.  S
cf8a0 74 61 72 74 20 77 69 74 68 20 74 68 65 20 61 73  tart with the as
cf8b0 73 75 6d 70 74 69 6f 6e 20 74 68 61 74 20 74 68  sumption that th
cf8c0 65 79 20 64 6f 0a 20 20 2a 2a 20 6d 61 74 63 68  ey do.  ** match
cf8d0 20 28 72 63 3d 3d 30 29 2e 20 20 43 68 61 6e 67   (rc==0).  Chang
cf8e0 65 20 72 63 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f  e rc to non-zero
cf8f0 20 69 66 20 61 20 64 69 73 63 72 65 70 61 6e 63   if a discrepanc
cf900 79 0a 20 20 2a 2a 20 69 73 20 66 6f 75 6e 64 2e  y.  ** is found.
cf910 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  .  */.  rc = sql
cf920 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 30  ite3BitvecTest(0
cf930 2c 30 29 20 2b 20 73 71 6c 69 74 65 33 42 69 74  ,0) + sqlite3Bit
cf940 76 65 63 54 65 73 74 28 70 42 69 74 76 65 63 2c  vecTest(pBitvec,
cf950 20 73 7a 2b 31 29 0a 20 20 20 20 20 20 20 20 20   sz+1).         
cf960 20 2b 20 73 71 6c 69 74 65 33 42 69 74 76 65 63   + sqlite3Bitvec
cf970 54 65 73 74 28 70 42 69 74 76 65 63 2c 20 30 29  Test(pBitvec, 0)
cf980 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d  ;.  for(i=1; i<=
cf990 73 7a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  sz; i++){.    if
cf9a0 28 20 20 28 54 45 53 54 42 49 54 28 70 56 2c 69  (  (TESTBIT(pV,i
cf9b0 29 29 21 3d 73 71 6c 69 74 65 33 42 69 74 76 65  ))!=sqlite3Bitve
cf9c0 63 54 65 73 74 28 70 42 69 74 76 65 63 2c 69 29  cTest(pBitvec,i)
cf9d0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 69   ){.      rc = i
cf9e0 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
cf9f0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46     }.  }..  /* F
cfa00 72 65 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 74  ree allocated st
cfa10 72 75 63 74 75 72 65 20 2a 2f 0a 62 69 74 76 65  ructure */.bitve
cfa20 63 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33  c_end:.  sqlite3
cfa30 5f 66 72 65 65 28 70 56 29 3b 0a 20 20 73 71 6c  _free(pV);.  sql
cfa40 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f  ite3BitvecDestro
cfa50 79 28 70 42 69 74 76 65 63 29 3b 0a 20 20 72 65  y(pBitvec);.  re
cfa60 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69  turn rc;.}.#endi
cfa70 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
cfa80 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a 2f  _BUILTIN_TEST */
cfa90 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
cfaa0 2a 20 45 6e 64 20 6f 66 20 62 69 74 76 65 63 2e  * End of bitvec.
cfab0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
cfac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cfad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cfae0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
cfaf0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 61 67  * Begin file pag
cfb00 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.c ***********
cfb10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cfb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cfb30 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
cfb40 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
cfb50 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
cfb60 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
cfb70 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
cfb80 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
cfb90 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
cfba0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
cfbb0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
cfbc0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
cfbd0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
cfbe0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
cfbf0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
cfc00 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
cfc10 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
cfc20 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
cfc30 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
cfc40 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
cfc50 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
cfc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cfc70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cfc80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cfc90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cfca0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
cfcb0 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e   is the implemen
cfcc0 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 61  tation of the pa
cfcd0 67 65 20 63 61 63 68 65 20 73 75 62 73 79 73 74  ge cache subsyst
cfce0 65 6d 20 6f 72 20 22 70 61 67 65 72 22 2e 0a 2a  em or "pager"..*
cfcf0 2a 20 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20  * .** The pager 
cfd00 69 73 20 75 73 65 64 20 74 6f 20 61 63 63 65 73  is used to acces
cfd10 73 20 61 20 64 61 74 61 62 61 73 65 20 64 69 73  s a database dis
cfd20 6b 20 66 69 6c 65 2e 20 20 49 74 20 69 6d 70 6c  k file.  It impl
cfd30 65 6d 65 6e 74 73 0a 2a 2a 20 61 74 6f 6d 69 63  ements.** atomic
cfd40 20 63 6f 6d 6d 69 74 20 61 6e 64 20 72 6f 6c 6c   commit and roll
cfd50 62 61 63 6b 20 74 68 72 6f 75 67 68 20 74 68 65  back through the
cfd60 20 75 73 65 20 6f 66 20 61 20 6a 6f 75 72 6e 61   use of a journa
cfd70 6c 20 66 69 6c 65 20 74 68 61 74 0a 2a 2a 20 69  l file that.** i
cfd80 73 20 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20  s separate from 
cfd90 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
cfda0 65 2e 20 20 54 68 65 20 70 61 67 65 72 20 61 6c  e.  The pager al
cfdb0 73 6f 20 69 6d 70 6c 65 6d 65 6e 74 73 20 66 69  so implements fi
cfdc0 6c 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 74 6f  le.** locking to
cfdd0 20 70 72 65 76 65 6e 74 20 74 77 6f 20 70 72 6f   prevent two pro
cfde0 63 65 73 73 65 73 20 66 72 6f 6d 20 77 72 69 74  cesses from writ
cfdf0 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74  ing the same dat
cfe00 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 73 69  abase.** file si
cfe10 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2c 20 6f 72  multaneously, or
cfe20 20 6f 6e 65 20 70 72 6f 63 65 73 73 20 66 72 6f   one process fro
cfe30 6d 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61  m reading the da
cfe40 74 61 62 61 73 65 20 77 68 69 6c 65 0a 2a 2a 20  tabase while.** 
cfe50 61 6e 6f 74 68 65 72 20 69 73 20 77 72 69 74 69  another is writi
cfe60 6e 67 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24  ng..**.** @(#) $
cfe70 49 64 3a 20 70 61 67 65 72 2e 63 2c 76 20 31 2e  Id: pager.c,v 1.
cfe80 34 34 36 20 32 30 30 38 2f 30 35 2f 31 33 20 31  446 2008/05/13 1
cfe90 33 3a 32 37 3a 33 34 20 64 72 68 20 45 78 70 20  3:27:34 drh Exp 
cfea0 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  $.*/.#ifndef SQL
cfeb0 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a  ITE_OMIT_DISKIO.
cfec0 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f  ./*.** Macros fo
cfed0 72 20 74 72 6f 75 62 6c 65 73 68 6f 6f 74 69 6e  r troubleshootin
cfee0 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72  g.  Normally tur
cfef0 6e 65 64 20 6f 66 66 0a 2a 2f 0a 23 69 66 20 30  ned off.*/.#if 0
cff00 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
cff10 44 65 62 75 67 50 72 69 6e 74 66 20 70 72 69 6e  DebugPrintf prin
cff20 74 66 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  tf.#define PAGER
cff30 54 52 41 43 45 31 28 58 29 20 20 20 20 20 20 20  TRACE1(X)       
cff40 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
cff50 74 66 28 58 29 0a 23 64 65 66 69 6e 65 20 50 41  tf(X).#define PA
cff60 47 45 52 54 52 41 43 45 32 28 58 2c 59 29 20 20  GERTRACE2(X,Y)  
cff70 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
cff80 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 66 69  rintf(X,Y).#defi
cff90 6e 65 20 50 41 47 45 52 54 52 41 43 45 33 28 58  ne PAGERTRACE3(X
cffa0 2c 59 2c 5a 29 20 20 20 73 71 6c 69 74 65 33 44  ,Y,Z)   sqlite3D
cffb0 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a  ebugPrintf(X,Y,Z
cffc0 29 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54  ).#define PAGERT
cffd0 52 41 43 45 34 28 58 2c 59 2c 5a 2c 57 29 20 73  RACE4(X,Y,Z,W) s
cffe0 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
cfff0 66 28 58 2c 59 2c 5a 2c 57 29 0a 23 64 65 66 69  f(X,Y,Z,W).#defi
d0000 6e 65 20 50 41 47 45 52 54 52 41 43 45 35 28 58  ne PAGERTRACE5(X
d0010 2c 59 2c 5a 2c 57 2c 56 29 20 73 71 6c 69 74 65  ,Y,Z,W,V) sqlite
d0020 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59  3DebugPrintf(X,Y
d0030 2c 5a 2c 57 2c 56 29 0a 23 65 6c 73 65 0a 23 64  ,Z,W,V).#else.#d
d0040 65 66 69 6e 65 20 50 41 47 45 52 54 52 41 43 45  efine PAGERTRACE
d0050 31 28 58 29 0a 23 64 65 66 69 6e 65 20 50 41 47  1(X).#define PAG
d0060 45 52 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64  ERTRACE2(X,Y).#d
d0070 65 66 69 6e 65 20 50 41 47 45 52 54 52 41 43 45  efine PAGERTRACE
d0080 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65  3(X,Y,Z).#define
d0090 20 50 41 47 45 52 54 52 41 43 45 34 28 58 2c 59   PAGERTRACE4(X,Y
d00a0 2c 5a 2c 57 29 0a 23 64 65 66 69 6e 65 20 50 41  ,Z,W).#define PA
d00b0 47 45 52 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c  GERTRACE5(X,Y,Z,
d00c0 57 2c 56 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  W,V).#endif../*.
d00d0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
d00e0 20 74 77 6f 20 6d 61 63 72 6f 73 20 61 72 65 20   two macros are 
d00f0 75 73 65 64 20 77 69 74 68 69 6e 20 74 68 65 20  used within the 
d0100 50 41 47 45 52 54 52 41 43 45 58 28 29 20 6d 61  PAGERTRACEX() ma
d0110 63 72 6f 73 20 61 62 6f 76 65 0a 2a 2a 20 74 6f  cros above.** to
d0120 20 70 72 69 6e 74 20 6f 75 74 20 66 69 6c 65 2d   print out file-
d0130 64 65 73 63 72 69 70 74 6f 72 73 2e 20 0a 2a 2a  descriptors. .**
d0140 0a 2a 2a 20 50 41 47 45 52 49 44 28 29 20 74 61  .** PAGERID() ta
d0150 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  kes a pointer to
d0160 20 61 20 50 61 67 65 72 20 73 74 72 75 63 74 20   a Pager struct 
d0170 61 73 20 69 74 73 20 61 72 67 75 6d 65 6e 74 2e  as its argument.
d0180 20 54 68 65 0a 2a 2a 20 61 73 73 6f 63 69 61 74   The.** associat
d0190 65 64 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74  ed file-descript
d01a0 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  or is returned. 
d01b0 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 29 20 74  FILEHANDLEID() t
d01c0 61 6b 65 73 20 61 6e 20 73 71 6c 69 74 65 33 5f  akes an sqlite3_
d01d0 66 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74 20 61  file.** struct a
d01e0 73 20 69 74 73 20 61 72 67 75 6d 65 6e 74 2e 0a  s its argument..
d01f0 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  */.#define PAGER
d0200 49 44 28 70 29 20 28 28 69 6e 74 29 28 70 2d 3e  ID(p) ((int)(p->
d0210 66 64 29 29 0a 23 64 65 66 69 6e 65 20 46 49 4c  fd)).#define FIL
d0220 45 48 41 4e 44 4c 45 49 44 28 66 64 29 20 28 28  EHANDLEID(fd) ((
d0230 69 6e 74 29 66 64 29 0a 0a 2f 2a 0a 2a 2a 20 54  int)fd)../*.** T
d0240 68 65 20 70 61 67 65 20 63 61 63 68 65 20 61 73  he page cache as
d0250 20 61 20 77 68 6f 6c 65 20 69 73 20 61 6c 77 61   a whole is alwa
d0260 79 73 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65  ys in one of the
d0270 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74   following.** st
d0280 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 41  ates:.**.**   PA
d0290 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 20  GER_UNLOCK      
d02a0 20 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65    The page cache
d02b0 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
d02c0 79 20 72 65 61 64 69 6e 67 20 6f 72 20 0a 2a 2a  y reading or .**
d02d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d02e0 20 20 20 20 20 20 20 77 72 69 74 69 6e 67 20 74         writing t
d02f0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
d0300 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f 0a 2a  .  There is no.*
d0310 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
d0320 20 20 20 20 20 20 20 20 64 61 74 61 20 68 65 6c          data hel
d0330 64 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 54 68  d in memory.  Th
d0340 69 73 20 69 73 20 74 68 65 20 69 6e 69 74 69 61  is is the initia
d0350 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  l.**            
d0360 20 20 20 20 20 20 20 20 20 20 20 73 74 61 74 65             state
d0370 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f  ..**.**   PAGER_
d0380 53 48 41 52 45 44 20 20 20 20 20 20 20 20 54 68  SHARED        Th
d0390 65 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20  e page cache is 
d03a0 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 61  reading the data
d03b0 62 61 73 65 2e 0a 2a 2a 20 20 20 20 20 20 20 20  base..**        
d03c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 57                 W
d03d0 72 69 74 69 6e 67 20 69 73 20 6e 6f 74 20 70 65  riting is not pe
d03e0 72 6d 69 74 74 65 64 2e 20 20 54 68 65 72 65 20  rmitted.  There 
d03f0 63 61 6e 20 62 65 0a 2a 2a 20 20 20 20 20 20 20  can be.**       
d0400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d0410 6d 75 6c 74 69 70 6c 65 20 72 65 61 64 65 72 73  multiple readers
d0420 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 73   accessing the s
d0430 61 6d 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  ame database.** 
d0440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d0450 20 20 20 20 20 20 66 69 6c 65 20 61 74 20 74 68        file at th
d0460 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a 0a  e same time..**.
d0470 2a 2a 20 20 20 50 41 47 45 52 5f 52 45 53 45 52  **   PAGER_RESER
d0480 56 45 44 20 20 20 20 20 20 54 68 69 73 20 70 72  VED      This pr
d0490 6f 63 65 73 73 20 68 61 73 20 72 65 73 65 72 76  ocess has reserv
d04a0 65 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ed the database 
d04b0 66 6f 72 20 77 72 69 74 69 6e 67 0a 2a 2a 20 20  for writing.**  
d04c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d04d0 20 20 20 20 20 62 75 74 20 68 61 73 20 6e 6f 74       but has not
d04e0 20 79 65 74 20 6d 61 64 65 20 61 6e 79 20 63 68   yet made any ch
d04f0 61 6e 67 65 73 2e 20 20 4f 6e 6c 79 20 6f 6e 65  anges.  Only one
d0500 20 70 72 6f 63 65 73 73 0a 2a 2a 20 20 20 20 20   process.**     
d0510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d0520 20 20 61 74 20 61 20 74 69 6d 65 20 63 61 6e 20    at a time can 
d0530 72 65 73 65 72 76 65 20 74 68 65 20 64 61 74 61  reserve the data
d0540 62 61 73 65 2e 20 20 54 68 65 20 6f 72 69 67 69  base.  The origi
d0550 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  nal.**          
d0560 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74               dat
d0570 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 6e  abase file has n
d0580 6f 74 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64  ot been modified
d0590 20 73 6f 20 6f 74 68 65 72 0a 2a 2a 20 20 20 20   so other.**    
d05a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d05b0 20 20 20 70 72 6f 63 65 73 73 65 73 20 6d 61 79     processes may
d05c0 20 73 74 69 6c 6c 20 62 65 20 72 65 61 64 69 6e   still be readin
d05d0 67 20 74 68 65 20 6f 6e 2d 64 69 73 6b 0a 2a 2a  g the on-disk.**
d05e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d05f0 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20         database 
d0600 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41  file..**.**   PA
d0610 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 20 20  GER_EXCLUSIVE   
d0620 20 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65    The page cache
d0630 20 69 73 20 77 72 69 74 69 6e 67 20 74 68 65 20   is writing the 
d0640 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 20 20 20  database..**    
d0650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d0660 20 20 20 41 63 63 65 73 73 20 69 73 20 65 78 63     Access is exc
d0670 6c 75 73 69 76 65 2e 20 20 4e 6f 20 6f 74 68 65  lusive.  No othe
d0680 72 20 70 72 6f 63 65 73 73 65 73 20 6f 72 0a 2a  r processes or.*
d0690 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
d06a0 20 20 20 20 20 20 20 20 74 68 72 65 61 64 73 20          threads 
d06b0 63 61 6e 20 62 65 20 72 65 61 64 69 6e 67 20 6f  can be reading o
d06c0 72 20 77 72 69 74 69 6e 67 20 77 68 69 6c 65 20  r writing while 
d06d0 6f 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  one.**          
d06e0 20 20 20 20 20 20 20 20 20 20 20 20 20 70 72 6f               pro
d06f0 63 65 73 73 20 69 73 20 77 72 69 74 69 6e 67 2e  cess is writing.
d0700 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 53  .**.**   PAGER_S
d0710 59 4e 43 45 44 20 20 20 20 20 20 20 20 54 68 65  YNCED        The
d0720 20 70 61 67 65 72 20 6d 6f 76 65 73 20 74 6f 20   pager moves to 
d0730 74 68 69 73 20 73 74 61 74 65 20 66 72 6f 6d 20  this state from 
d0740 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 0a  PAGER_EXCLUSIVE.
d0750 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
d0760 20 20 20 20 20 20 20 20 20 61 66 74 65 72 20 61           after a
d0770 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 68  ll dirty pages h
d0780 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e  ave been written
d0790 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   to the.**      
d07a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d07b0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
d07c0 6e 64 20 74 68 65 20 66 69 6c 65 20 68 61 73 20  nd the file has 
d07d0 62 65 65 6e 20 73 79 6e 63 65 64 20 74 6f 0a 2a  been synced to.*
d07e0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
d07f0 20 20 20 20 20 20 20 20 64 69 73 6b 2e 20 41 6c          disk. Al
d0800 6c 20 74 68 61 74 20 72 65 6d 61 69 6e 73 20 74  l that remains t
d0810 6f 20 64 6f 20 69 73 20 74 6f 20 72 65 6d 6f 76  o do is to remov
d0820 65 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  e or.**         
d0830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 72                tr
d0840 75 6e 63 61 74 65 20 74 68 65 20 6a 6f 75 72 6e  uncate the journ
d0850 61 6c 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20  al file and the 
d0860 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20  transaction .** 
d0870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d0880 20 20 20 20 20 20 77 69 6c 6c 20 62 65 20 63 6f        will be co
d0890 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  mmitted..**.** T
d08a0 68 65 20 70 61 67 65 20 63 61 63 68 65 20 63 6f  he page cache co
d08b0 6d 65 73 20 75 70 20 69 6e 20 50 41 47 45 52 5f  mes up in PAGER_
d08c0 55 4e 4c 4f 43 4b 2e 20 20 54 68 65 20 66 69 72  UNLOCK.  The fir
d08d0 73 74 20 74 69 6d 65 20 61 0a 2a 2a 20 73 71 6c  st time a.** sql
d08e0 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 6f  ite3PagerGet() o
d08f0 63 63 75 72 73 2c 20 74 68 65 20 73 74 61 74 65  ccurs, the state
d0900 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 20   transitions to 
d0910 50 41 47 45 52 5f 53 48 41 52 45 44 2e 0a 2a 2a  PAGER_SHARED..**
d0920 20 41 66 74 65 72 20 61 6c 6c 20 70 61 67 65 73   After all pages
d0930 20 68 61 76 65 20 62 65 65 6e 20 72 65 6c 65 61   have been relea
d0940 73 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65  sed using sqlite
d0950 5f 70 61 67 65 5f 75 6e 72 65 66 28 29 2c 0a 2a  _page_unref(),.*
d0960 2a 20 74 68 65 20 73 74 61 74 65 20 74 72 61 6e  * the state tran
d0970 73 69 74 69 6f 6e 73 20 62 61 63 6b 20 74 6f 20  sitions back to 
d0980 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20 20 54  PAGER_UNLOCK.  T
d0990 68 65 20 66 69 72 73 74 20 74 69 6d 65 0a 2a 2a  he first time.**
d09a0 20 74 68 61 74 20 73 71 6c 69 74 65 33 50 61 67   that sqlite3Pag
d09b0 65 72 57 72 69 74 65 28 29 20 69 73 20 63 61 6c  erWrite() is cal
d09c0 6c 65 64 2c 20 74 68 65 20 73 74 61 74 65 20 74  led, the state t
d09d0 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 0a 2a 2a  ransitions to.**
d09e0 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e   PAGER_RESERVED.
d09f0 20 20 28 4e 6f 74 65 20 74 68 61 74 20 73 71 6c    (Note that sql
d0a00 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29  ite3PagerWrite()
d0a10 20 63 61 6e 20 6f 6e 6c 79 20 62 65 0a 2a 2a 20   can only be.** 
d0a20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 6f 75 74  called on an out
d0a30 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 77 68  standing page wh
d0a40 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 20 74  ich means that t
d0a50 68 65 20 70 61 67 65 72 20 6d 75 73 74 0a 2a 2a  he pager must.**
d0a60 20 62 65 20 69 6e 20 50 41 47 45 52 5f 53 48 41   be in PAGER_SHA
d0a70 52 45 44 20 62 65 66 6f 72 65 20 69 74 20 74 72  RED before it tr
d0a80 61 6e 73 69 74 69 6f 6e 73 20 74 6f 20 50 41 47  ansitions to PAG
d0a90 45 52 5f 52 45 53 45 52 56 45 44 2e 29 0a 2a 2a  ER_RESERVED.).**
d0aa0 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20   PAGER_RESERVED 
d0ab0 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 72 65  means that there
d0ac0 20 69 73 20 61 6e 20 6f 70 65 6e 20 72 6f 6c 6c   is an open roll
d0ad0 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a  back journal..**
d0ae0 20 54 68 65 20 74 72 61 6e 73 69 74 69 6f 6e 20   The transition 
d0af0 74 6f 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49  to PAGER_EXCLUSI
d0b00 56 45 20 6f 63 63 75 72 73 20 62 65 66 6f 72 65  VE occurs before
d0b10 20 61 6e 79 20 63 68 61 6e 67 65 73 0a 2a 2a 20   any changes.** 
d0b20 61 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20  are made to the 
d0b30 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74  database file, t
d0b40 68 6f 75 67 68 20 77 72 69 74 65 73 20 74 6f 20  hough writes to 
d0b50 74 68 65 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20  the rollback.** 
d0b60 6a 6f 75 72 6e 61 6c 20 6f 63 63 75 72 73 20 77  journal occurs w
d0b70 69 74 68 20 6a 75 73 74 20 50 41 47 45 52 5f 52  ith just PAGER_R
d0b80 45 53 45 52 56 45 44 2e 20 20 41 66 74 65 72 20  ESERVED.  After 
d0b90 61 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 52  an sqlite3PagerR
d0ba0 6f 6c 6c 62 61 63 6b 28 29 0a 2a 2a 20 6f 72 20  ollback().** or 
d0bb0 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d  sqlite3PagerComm
d0bc0 69 74 50 68 61 73 65 54 77 6f 28 29 2c 20 74 68  itPhaseTwo(), th
d0bd0 65 20 73 74 61 74 65 20 63 61 6e 20 67 6f 20 62  e state can go b
d0be0 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 53 48 41  ack to PAGER_SHA
d0bf0 52 45 44 2c 0a 2a 2a 20 6f 72 20 69 74 20 63 61  RED,.** or it ca
d0c00 6e 20 73 74 61 79 20 61 74 20 50 41 47 45 52 5f  n stay at PAGER_
d0c10 45 58 43 4c 55 53 49 56 45 20 69 66 20 77 65 20  EXCLUSIVE if we 
d0c20 61 72 65 20 69 6e 20 65 78 63 6c 75 73 69 76 65  are in exclusive
d0c30 20 61 63 63 65 73 73 20 6d 6f 64 65 2e 0a 2a 2f   access mode..*/
d0c40 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 55  .#define PAGER_U
d0c50 4e 4c 4f 43 4b 20 20 20 20 20 20 30 0a 23 64 65  NLOCK      0.#de
d0c60 66 69 6e 65 20 50 41 47 45 52 5f 53 48 41 52 45  fine PAGER_SHARE
d0c70 44 20 20 20 20 20 20 31 20 20 20 2f 2a 20 73 61  D      1   /* sa
d0c80 6d 65 20 61 73 20 53 48 41 52 45 44 5f 4c 4f 43  me as SHARED_LOC
d0c90 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47  K */.#define PAG
d0ca0 45 52 5f 52 45 53 45 52 56 45 44 20 20 20 20 32  ER_RESERVED    2
d0cb0 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 52 45     /* same as RE
d0cc0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 23  SERVED_LOCK */.#
d0cd0 64 65 66 69 6e 65 20 50 41 47 45 52 5f 45 58 43  define PAGER_EXC
d0ce0 4c 55 53 49 56 45 20 20 20 34 20 20 20 2f 2a 20  LUSIVE   4   /* 
d0cf0 73 61 6d 65 20 61 73 20 45 58 43 4c 55 53 49 56  same as EXCLUSIV
d0d00 45 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e  E_LOCK */.#defin
d0d10 65 20 50 41 47 45 52 5f 53 59 4e 43 45 44 20 20  e PAGER_SYNCED  
d0d20 20 20 20 20 35 0a 0a 2f 2a 0a 2a 2a 20 49 66 20      5../*.** If 
d0d30 74 68 65 20 53 51 4c 49 54 45 5f 42 55 53 59 5f  the SQLITE_BUSY_
d0d40 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 6d 61  RESERVED_LOCK ma
d0d50 63 72 6f 20 69 73 20 73 65 74 20 74 6f 20 74 72  cro is set to tr
d0d60 75 65 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69  ue at compile-ti
d0d70 6d 65 2c 0a 2a 2a 20 74 68 65 6e 20 66 61 69 6c  me,.** then fail
d0d80 65 64 20 61 74 74 65 6d 70 74 73 20 74 6f 20 67  ed attempts to g
d0d90 65 74 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f  et a reserved lo
d0da0 63 6b 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74  ck will invoke t
d0db0 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b  he busy callback
d0dc0 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 6f 66 66  ..** This is off
d0dd0 20 62 79 20 64 65 66 61 75 6c 74 2e 20 20 54 6f   by default.  To
d0de0 20 73 65 65 20 77 68 79 2c 20 63 6f 6e 73 69 64   see why, consid
d0df0 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  er the following
d0e00 20 73 63 65 6e 61 72 69 6f 3a 0a 2a 2a 20 0a 2a   scenario:.** .*
d0e10 2a 20 53 75 70 70 6f 73 65 20 74 68 72 65 61 64  * Suppose thread
d0e20 20 41 20 61 6c 72 65 61 64 79 20 68 61 73 20 61   A already has a
d0e30 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6e 64   shared lock and
d0e40 20 77 61 6e 74 73 20 61 20 72 65 73 65 72 76 65   wants a reserve
d0e50 64 20 6c 6f 63 6b 2e 0a 2a 2a 20 54 68 72 65 61  d lock..** Threa
d0e60 64 20 42 20 61 6c 72 65 61 64 79 20 68 61 73 20  d B already has 
d0e70 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20  a reserved lock 
d0e80 61 6e 64 20 77 61 6e 74 73 20 61 6e 20 65 78 63  and wants an exc
d0e90 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20 49 66  lusive lock.  If
d0ea0 0a 2a 2a 20 62 6f 74 68 20 74 68 72 65 61 64 73  .** both threads
d0eb0 20 61 72 65 20 75 73 69 6e 67 20 74 68 65 69 72   are using their
d0ec0 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 73 2c   busy callbacks,
d0ed0 20 69 74 20 6d 69 67 68 74 20 62 65 20 61 20 6c   it might be a l
d0ee0 6f 6e 67 20 74 69 6d 65 0a 2a 2a 20 62 65 20 66  ong time.** be f
d0ef0 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 68  or one of the th
d0f00 72 65 61 64 73 20 67 69 76 65 20 75 70 20 61 6e  reads give up an
d0f10 64 20 61 6c 6c 6f 77 73 20 74 68 65 20 6f 74 68  d allows the oth
d0f20 65 72 20 74 6f 20 70 72 6f 63 65 65 64 2e 0a 2a  er to proceed..*
d0f30 2a 20 42 75 74 20 69 66 20 74 68 65 20 74 68 72  * But if the thr
d0f40 65 61 64 20 74 72 79 69 6e 67 20 74 6f 20 67 65  ead trying to ge
d0f50 74 20 74 68 65 20 72 65 73 65 72 76 65 64 20 6c  t the reserved l
d0f60 6f 63 6b 20 67 69 76 65 73 20 75 70 20 71 75 69  ock gives up qui
d0f70 63 6b 6c 79 0a 2a 2a 20 28 69 66 20 69 74 20 6e  ckly.** (if it n
d0f80 65 76 65 72 20 69 6e 76 6f 6b 65 73 20 69 74 73  ever invokes its
d0f90 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 29 20   busy callback) 
d0fa0 74 68 65 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74  then the content
d0fb0 69 6f 6e 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 72  ion will be.** r
d0fc0 65 73 6f 6c 76 65 64 20 71 75 69 63 6b 6c 79 2e  esolved quickly.
d0fd0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
d0fe0 54 45 5f 42 55 53 59 5f 52 45 53 45 52 56 45 44  TE_BUSY_RESERVED
d0ff0 5f 4c 4f 43 4b 0a 23 20 64 65 66 69 6e 65 20 53  _LOCK.# define S
d1000 51 4c 49 54 45 5f 42 55 53 59 5f 52 45 53 45 52  QLITE_BUSY_RESER
d1010 56 45 44 5f 4c 4f 43 4b 20 30 0a 23 65 6e 64 69  VED_LOCK 0.#endi
d1020 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6d 61  f../*.** This ma
d1030 63 72 6f 20 72 6f 75 6e 64 73 20 76 61 6c 75 65  cro rounds value
d1040 73 20 75 70 20 73 6f 20 74 68 61 74 20 69 66 20  s up so that if 
d1050 74 68 65 20 76 61 6c 75 65 20 69 73 20 61 6e 20  the value is an 
d1060 61 64 64 72 65 73 73 20 69 74 0a 2a 2a 20 69 73  address it.** is
d1070 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62   guaranteed to b
d1080 65 20 61 6e 20 61 64 64 72 65 73 73 20 74 68 61  e an address tha
d1090 74 20 69 73 20 61 6c 69 67 6e 65 64 20 74 6f 20  t is aligned to 
d10a0 61 6e 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61  an 8-byte bounda
d10b0 72 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46  ry..*/.#define F
d10c0 4f 52 43 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 58  ORCE_ALIGNMENT(X
d10d0 29 20 20 20 28 28 28 58 29 2b 37 29 26 7e 37 29  )   (((X)+7)&~7)
d10e0 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
d10f0 20 50 67 48 64 72 20 50 67 48 64 72 3b 0a 0a 2f   PgHdr PgHdr;../
d1100 2a 0a 2a 2a 20 45 61 63 68 20 70 61 67 65 72 20  *.** Each pager 
d1110 73 74 6f 72 65 73 20 61 6c 6c 20 63 75 72 72 65  stores all curre
d1120 6e 74 6c 79 20 75 6e 72 65 66 65 72 65 6e 63 65  ntly unreference
d1130 64 20 70 61 67 65 73 20 69 6e 20 61 20 6c 69 73  d pages in a lis
d1140 74 20 73 6f 72 74 65 64 0a 2a 2a 20 69 6e 20 6c  t sorted.** in l
d1150 65 61 73 74 2d 72 65 63 65 6e 74 6c 79 2d 75 73  east-recently-us
d1160 65 64 20 28 4c 52 55 29 20 6f 72 64 65 72 20 28  ed (LRU) order (
d1170 69 2e 65 2e 20 74 68 65 20 66 69 72 73 74 20 69  i.e. the first i
d1180 74 65 6d 20 6f 6e 20 74 68 65 20 6c 69 73 74 20  tem on the list 
d1190 68 61 73 20 0a 2a 2a 20 6e 6f 74 20 62 65 65 6e  has .** not been
d11a0 20 72 65 66 65 72 65 6e 63 65 64 20 69 6e 20 61   referenced in a
d11b0 20 6c 6f 6e 67 20 74 69 6d 65 2c 20 74 68 65 20   long time, the 
d11c0 6c 61 73 74 20 69 74 65 6d 20 68 61 73 20 62 65  last item has be
d11d0 65 6e 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 75  en recently.** u
d11e0 73 65 64 29 2e 20 41 6e 20 69 6e 73 74 61 6e 63  sed). An instanc
d11f0 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  e of this struct
d1200 75 72 65 20 69 73 20 69 6e 63 6c 75 64 65 64 20  ure is included 
d1210 61 73 20 70 61 72 74 20 6f 66 20 65 61 63 68 0a  as part of each.
d1220 2a 2a 20 70 61 67 65 72 20 73 74 72 75 63 74 75  ** pager structu
d1230 72 65 20 66 6f 72 20 74 68 69 73 20 70 75 72 70  re for this purp
d1240 6f 73 65 20 28 76 61 72 69 61 62 6c 65 20 50 61  ose (variable Pa
d1250 67 65 72 2e 6c 72 75 29 2e 0a 2a 2a 0a 2a 2a 20  ger.lru)..**.** 
d1260 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 69 66  Additionally, if
d1270 20 6d 65 6d 6f 72 79 2d 6d 61 6e 61 67 65 6d 65   memory-manageme
d1280 6e 74 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 61  nt is enabled, a
d1290 6c 6c 20 75 6e 72 65 66 65 72 65 6e 63 65 64 20  ll unreferenced 
d12a0 70 61 67 65 73 20 0a 2a 2a 20 61 72 65 20 73 74  pages .** are st
d12b0 6f 72 65 64 20 69 6e 20 61 20 67 6c 6f 62 61 6c  ored in a global
d12c0 20 4c 52 55 20 6c 69 73 74 20 28 67 6c 6f 62 61   LRU list (globa
d12d0 6c 20 76 61 72 69 61 62 6c 65 20 73 71 6c 69 74  l variable sqlit
d12e0 65 33 4c 72 75 50 61 67 65 4c 69 73 74 29 2e 0a  e3LruPageList)..
d12f0 2a 2a 0a 2a 2a 20 49 6e 20 62 6f 74 68 20 63 61  **.** In both ca
d1300 73 65 73 2c 20 74 68 65 20 50 61 67 65 72 4c 72  ses, the PagerLr
d1310 75 4c 69 73 74 2e 70 46 69 72 73 74 53 79 6e 63  uList.pFirstSync
d1320 65 64 20 76 61 72 69 61 62 6c 65 20 70 6f 69 6e  ed variable poin
d1330 74 73 20 74 6f 0a 2a 2a 20 74 68 65 20 66 69 72  ts to.** the fir
d1340 73 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 63  st page in the c
d1350 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6c 69 73  orresponding lis
d1360 74 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20  t that does not 
d1370 72 65 71 75 69 72 65 20 61 6e 0a 2a 2a 20 66 73  require an.** fs
d1380 79 6e 63 28 29 20 6f 70 65 72 61 74 69 6f 6e 20  ync() operation 
d1390 62 65 66 6f 72 65 20 69 74 73 20 6d 65 6d 6f 72  before its memor
d13a0 79 20 63 61 6e 20 62 65 20 72 65 63 6c 61 69 6d  y can be reclaim
d13b0 65 64 2e 20 49 66 20 6e 6f 20 73 75 63 68 0a 2a  ed. If no such.*
d13c0 2a 20 70 61 67 65 20 65 78 69 73 74 73 2c 20 50  * page exists, P
d13d0 61 67 65 72 4c 72 75 4c 69 73 74 2e 70 46 69 72  agerLruList.pFir
d13e0 73 74 53 79 6e 63 65 64 20 69 73 20 73 65 74 20  stSynced is set 
d13f0 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 74 79 70 65  to NULL..*/.type
d1400 64 65 66 20 73 74 72 75 63 74 20 50 61 67 65 72  def struct Pager
d1410 4c 72 75 4c 69 73 74 20 50 61 67 65 72 4c 72 75  LruList PagerLru
d1420 4c 69 73 74 3b 0a 73 74 72 75 63 74 20 50 61 67  List;.struct Pag
d1430 65 72 4c 72 75 4c 69 73 74 20 7b 0a 20 20 50 67  erLruList {.  Pg
d1440 48 64 72 20 2a 70 46 69 72 73 74 3b 20 20 20 20  Hdr *pFirst;    
d1450 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61       /* First pa
d1460 67 65 20 69 6e 20 4c 52 55 20 6c 69 73 74 20 2a  ge in LRU list *
d1470 2f 0a 20 20 50 67 48 64 72 20 2a 70 4c 61 73 74  /.  PgHdr *pLast
d1480 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61  ;          /* La
d1490 73 74 20 70 61 67 65 20 69 6e 20 4c 52 55 20 6c  st page in LRU l
d14a0 69 73 74 20 28 74 68 65 20 6d 6f 73 74 20 72 65  ist (the most re
d14b0 63 65 6e 74 6c 79 20 75 73 65 64 29 20 2a 2f 0a  cently used) */.
d14c0 20 20 50 67 48 64 72 20 2a 70 46 69 72 73 74 53    PgHdr *pFirstS
d14d0 79 6e 63 65 64 3b 20 20 20 2f 2a 20 46 69 72 73  ynced;   /* Firs
d14e0 74 20 70 61 67 65 20 69 6e 20 6c 69 73 74 20 77  t page in list w
d14f0 69 74 68 20 50 67 48 64 72 2e 6e 65 65 64 53 79  ith PgHdr.needSy
d1500 6e 63 3d 3d 30 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  nc==0 */.};../*.
d1510 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
d1520 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61   structure conta
d1530 69 6e 73 20 74 68 65 20 6e 65 78 74 20 61 6e 64  ins the next and
d1540 20 70 72 65 76 69 6f 75 73 20 70 6f 69 6e 74 65   previous pointe
d1550 72 73 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 69  rs used.** to li
d1560 6e 6b 20 61 20 50 67 48 64 72 20 73 74 72 75 63  nk a PgHdr struc
d1570 74 75 72 65 20 69 6e 74 6f 20 61 20 50 61 67 65  ture into a Page
d1580 72 4c 72 75 4c 69 73 74 20 6c 69 6e 6b 65 64 20  rLruList linked 
d1590 6c 69 73 74 2e 20 0a 2a 2f 0a 74 79 70 65 64 65  list. .*/.typede
d15a0 66 20 73 74 72 75 63 74 20 50 61 67 65 72 4c 72  f struct PagerLr
d15b0 75 4c 69 6e 6b 20 50 61 67 65 72 4c 72 75 4c 69  uLink PagerLruLi
d15c0 6e 6b 3b 0a 73 74 72 75 63 74 20 50 61 67 65 72  nk;.struct Pager
d15d0 4c 72 75 4c 69 6e 6b 20 7b 0a 20 20 50 67 48 64  LruLink {.  PgHd
d15e0 72 20 2a 70 4e 65 78 74 3b 0a 20 20 50 67 48 64  r *pNext;.  PgHd
d15f0 72 20 2a 70 50 72 65 76 3b 0a 7d 3b 0a 0a 2f 2a  r *pPrev;.};../*
d1600 0a 2a 2a 20 45 61 63 68 20 69 6e 2d 6d 65 6d 6f  .** Each in-memo
d1610 72 79 20 69 6d 61 67 65 20 6f 66 20 61 20 70 61  ry image of a pa
d1620 67 65 20 62 65 67 69 6e 73 20 77 69 74 68 20 74  ge begins with t
d1630 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 68 65 61  he following hea
d1640 64 65 72 2e 0a 2a 2a 20 54 68 69 73 20 68 65 61  der..** This hea
d1650 64 65 72 20 69 73 20 6f 6e 6c 79 20 76 69 73 69  der is only visi
d1660 62 6c 65 20 74 6f 20 74 68 69 73 20 70 61 67 65  ble to this page
d1670 72 20 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20 63  r module.  The c
d1680 6c 69 65 6e 74 0a 2a 2a 20 63 6f 64 65 20 74 68  lient.** code th
d1690 61 74 20 63 61 6c 6c 73 20 70 61 67 65 72 20 73  at calls pager s
d16a0 65 65 73 20 6f 6e 6c 79 20 74 68 65 20 64 61 74  ees only the dat
d16b0 61 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 74  a that follows t
d16c0 68 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a  he header..**.**
d16d0 20 43 6c 69 65 6e 74 20 63 6f 64 65 20 73 68 6f   Client code sho
d16e0 75 6c 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33  uld call sqlite3
d16f0 50 61 67 65 72 57 72 69 74 65 28 29 20 6f 6e 20  PagerWrite() on 
d1700 61 20 70 61 67 65 20 70 72 69 6f 72 20 74 6f 20  a page prior to 
d1710 6d 61 6b 69 6e 67 0a 2a 2a 20 61 6e 79 20 6d 6f  making.** any mo
d1720 64 69 66 69 63 61 74 69 6f 6e 73 20 74 6f 20 74  difications to t
d1730 68 61 74 20 70 61 67 65 2e 20 20 54 68 65 20 66  hat page.  The f
d1740 69 72 73 74 20 74 69 6d 65 20 73 71 6c 69 74 65  irst time sqlite
d1750 33 50 61 67 65 72 57 72 69 74 65 28 29 0a 2a 2a  3PagerWrite().**
d1760 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20   is called, the 
d1770 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 20 63 6f  original page co
d1780 6e 74 65 6e 74 73 20 61 72 65 20 77 72 69 74 74  ntents are writt
d1790 65 6e 20 69 6e 74 6f 20 74 68 65 20 72 6f 6c 6c  en into the roll
d17a0 62 61 63 6b 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20  back.** journal 
d17b0 61 6e 64 20 50 67 48 64 72 2e 69 6e 4a 6f 75 72  and PgHdr.inJour
d17c0 6e 61 6c 20 61 6e 64 20 50 67 48 64 72 2e 6e 65  nal and PgHdr.ne
d17d0 65 64 53 79 6e 63 20 61 72 65 20 73 65 74 2e 20  edSync are set. 
d17e0 20 4c 61 74 65 72 2c 20 6f 6e 63 65 0a 2a 2a 20   Later, once.** 
d17f0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 70 61 67 65  the journal page
d1800 20 68 61 73 20 6d 61 64 65 20 69 74 20 6f 6e 74   has made it ont
d1810 6f 20 74 68 65 20 64 69 73 6b 20 73 75 72 66 61  o the disk surfa
d1820 63 65 2c 20 50 67 48 64 72 2e 6e 65 65 64 53 79  ce, PgHdr.needSy
d1830 6e 63 0a 2a 2a 20 69 73 20 63 6c 65 61 72 65 64  nc.** is cleared
d1840 2e 20 20 54 68 65 20 6d 6f 64 69 66 69 65 64 20  .  The modified 
d1850 70 61 67 65 20 63 61 6e 6e 6f 74 20 62 65 20 77  page cannot be w
d1860 72 69 74 74 65 6e 20 62 61 63 6b 20 69 6e 74 6f  ritten back into
d1870 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a   the original.**
d1880 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75   database file u
d1890 6e 74 69 6c 20 74 68 65 20 6a 6f 75 72 6e 61 6c  ntil the journal
d18a0 20 70 61 67 65 73 20 68 61 73 20 62 65 65 6e 20   pages has been 
d18b0 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 20 61  synced to disk a
d18c0 6e 64 20 74 68 65 0a 2a 2a 20 50 67 48 64 72 2e  nd the.** PgHdr.
d18d0 6e 65 65 64 53 79 6e 63 20 68 61 73 20 62 65 65  needSync has bee
d18e0 6e 20 63 6c 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a  n cleared..**.**
d18f0 20 54 68 65 20 50 67 48 64 72 2e 64 69 72 74 79   The PgHdr.dirty
d1900 20 66 6c 61 67 20 69 73 20 73 65 74 20 77 68 65   flag is set whe
d1910 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  n sqlite3PagerWr
d1920 69 74 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20  ite() is called 
d1930 61 6e 64 0a 2a 2a 20 69 73 20 63 6c 65 61 72 65  and.** is cleare
d1940 64 20 61 67 61 69 6e 20 77 68 65 6e 20 74 68 65  d again when the
d1950 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 69 73   page content is
d1960 20 77 72 69 74 74 65 6e 20 62 61 63 6b 20 74 6f   written back to
d1970 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a   the original.**
d1980 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
d1990 2a 2a 0a 2a 2a 20 44 65 74 61 69 6c 73 20 6f 66  **.** Details of
d19a0 20 69 6d 70 6f 72 74 61 6e 74 20 73 74 72 75 63   important struc
d19b0 74 75 72 65 20 65 6c 65 6d 65 6e 74 73 3a 0a 2a  ture elements:.*
d19c0 2a 0a 2a 2a 20 6e 65 65 64 53 79 6e 63 0a 2a 2a  *.** needSync.**
d19d0 0a 2a 2a 20 20 20 20 20 49 66 20 74 68 69 73 20  .**     If this 
d19e0 69 73 20 74 72 75 65 2c 20 74 68 69 73 20 6d 65  is true, this me
d19f0 61 6e 73 20 74 68 61 74 20 69 74 20 69 73 20 6e  ans that it is n
d1a00 6f 74 20 73 61 66 65 20 74 6f 20 77 72 69 74 65  ot safe to write
d1a10 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 20 20 20   the page.**    
d1a20 20 63 6f 6e 74 65 6e 74 20 74 6f 20 74 68 65 20   content to the 
d1a30 64 61 74 61 62 61 73 65 20 62 65 63 61 75 73 65  database because
d1a40 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 63 6f   the original co
d1a50 6e 74 65 6e 74 20 6e 65 65 64 65 64 0a 2a 2a 20  ntent needed.** 
d1a60 20 20 20 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b      for rollback
d1a70 20 68 61 73 20 6e 6f 74 20 62 79 20 73 79 6e 63   has not by sync
d1a80 65 64 20 74 6f 20 74 68 65 20 6d 61 69 6e 20 72  ed to the main r
d1a90 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e  ollback journal.
d1aa0 0a 2a 2a 20 20 20 20 20 54 68 65 20 6f 72 69 67  .**     The orig
d1ab0 69 6e 61 6c 20 63 6f 6e 74 65 6e 74 20 6d 61 79  inal content may
d1ac0 20 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 74   have been writt
d1ad0 65 6e 20 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61  en to the rollba
d1ae0 63 6b 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20  ck journal.**   
d1af0 20 20 62 75 74 20 69 74 20 68 61 73 20 6e 6f 74    but it has not
d1b00 20 79 65 74 20 62 65 65 6e 20 73 79 6e 63 65 64   yet been synced
d1b10 2e 20 20 53 6f 20 77 65 20 63 61 6e 6e 6f 74 20  .  So we cannot 
d1b20 77 72 69 74 65 20 74 6f 20 74 68 65 20 64 61 74  write to the dat
d1b30 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 66 69 6c  abase.**     fil
d1b40 65 20 62 65 63 61 75 73 65 20 70 6f 77 65 72 20  e because power 
d1b50 66 61 69 6c 75 72 65 20 6d 69 67 68 74 20 63 61  failure might ca
d1b60 75 73 65 20 74 68 65 20 70 61 67 65 20 69 6e 20  use the page in 
d1b70 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
d1b80 0a 2a 2a 20 20 20 20 20 74 6f 20 6e 65 76 65 72  .**     to never
d1b90 20 72 65 61 63 68 20 74 68 65 20 64 69 73 6b 2e   reach the disk.
d1ba0 20 20 49 74 20 69 73 20 61 73 20 69 66 20 74 68    It is as if th
d1bb0 65 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 6a  e write to the j
d1bc0 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 20  ournal file.**  
d1bd0 20 20 20 64 6f 65 73 20 6e 6f 74 20 6f 63 63 75     does not occu
d1be0 72 20 75 6e 74 69 6c 20 74 68 65 20 6a 6f 75 72  r until the jour
d1bf0 6e 61 6c 20 66 69 6c 65 20 69 73 20 73 79 6e 63  nal file is sync
d1c00 65 64 2e 0a 2a 2a 20 20 20 20 20 0a 2a 2a 20 20  ed..**     .**  
d1c10 20 20 20 54 68 69 73 20 66 6c 61 67 20 69 73 20     This flag is 
d1c20 66 61 6c 73 65 20 69 66 20 74 68 65 20 70 61 67  false if the pag
d1c30 65 20 63 6f 6e 74 65 6e 74 20 65 78 61 63 74 6c  e content exactl
d1c40 79 20 6d 61 74 63 68 65 73 20 77 68 61 74 0a 2a  y matches what.*
d1c50 2a 20 20 20 20 20 63 75 72 72 65 6e 74 6c 79 20  *     currently 
d1c60 65 78 69 73 74 73 20 69 6e 20 74 68 65 20 64 61  exists in the da
d1c70 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68  tabase file.  Th
d1c80 65 20 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 20  e needSync flag 
d1c90 69 73 20 61 6c 73 6f 0a 2a 2a 20 20 20 20 20 66  is also.**     f
d1ca0 61 6c 73 65 20 69 66 20 74 68 65 20 6f 72 69 67  alse if the orig
d1cb0 69 6e 61 6c 20 63 6f 6e 74 65 6e 74 20 68 61 73  inal content has
d1cc0 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f   been written to
d1cd0 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61   the main rollba
d1ce0 63 6b 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61  ck.**     journa
d1cf0 6c 20 61 6e 64 20 73 79 6e 63 65 64 2e 20 20 49  l and synced.  I
d1d00 66 20 74 68 65 20 70 61 67 65 20 72 65 70 72 65  f the page repre
d1d10 73 65 6e 74 73 20 61 20 6e 65 77 20 70 61 67 65  sents a new page
d1d20 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 20 20 20   that has.**    
d1d30 20 62 65 65 6e 20 61 64 64 65 64 20 6f 6e 74 6f   been added onto
d1d40 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
d1d50 64 61 74 61 62 61 73 65 20 64 75 72 69 6e 67 20  database during 
d1d60 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 20  the current.**  
d1d70 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20     transaction, 
d1d80 74 68 65 20 6e 65 65 64 53 79 6e 63 20 66 6c 61  the needSync fla
d1d90 67 20 69 73 20 74 72 75 65 20 75 6e 74 69 6c 20  g is true until 
d1da0 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74  the original dat
d1db0 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 73 69 7a  abase.**     siz
d1dc0 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e in the journal
d1dd0 20 68 65 61 64 65 72 20 68 61 73 20 62 65 65 6e   header has been
d1de0 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e   synced to disk.
d1df0 0a 2a 2a 0a 2a 2a 20 69 6e 4a 6f 75 72 6e 61 6c  .**.** inJournal
d1e00 0a 2a 2a 0a 2a 2a 20 20 20 20 20 54 68 69 73 20  .**.**     This 
d1e10 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20 6f  is true if the o
d1e20 72 69 67 69 6e 61 6c 20 70 61 67 65 20 68 61 73  riginal page has
d1e30 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 69 6e   been written in
d1e40 74 6f 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 20  to the main.**  
d1e50 20 20 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72     rollback jour
d1e60 6e 61 6c 2e 20 20 54 68 69 73 20 69 73 20 61 6c  nal.  This is al
d1e70 77 61 79 73 20 66 61 6c 73 65 20 66 6f 72 20 6e  ways false for n
d1e80 65 77 20 70 61 67 65 73 20 61 64 64 65 64 20 74  ew pages added t
d1e90 6f 0a 2a 2a 20 20 20 20 20 74 68 65 20 65 6e 64  o.**     the end
d1ea0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
d1eb0 20 66 69 6c 65 20 64 75 72 69 6e 67 20 74 68 65   file during the
d1ec0 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63   current transac
d1ed0 74 69 6f 6e 2e 0a 2a 2a 20 20 20 20 20 41 6e 64  tion..**     And
d1ee0 20 74 68 69 73 20 66 6c 61 67 20 73 61 79 73 20   this flag says 
d1ef0 6e 6f 74 68 69 6e 67 20 61 62 6f 75 74 20 77 68  nothing about wh
d1f00 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
d1f10 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20   journal.**     
d1f20 68 61 73 20 62 65 65 6e 20 73 79 6e 63 65 64 20  has been synced 
d1f30 74 6f 20 64 69 73 6b 2e 20 20 46 6f 72 20 70 61  to disk.  For pa
d1f40 67 65 73 20 74 68 61 74 20 61 72 65 20 69 6e 20  ges that are in 
d1f50 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20  the original.** 
d1f60 20 20 20 20 64 61 74 61 62 61 73 65 20 66 69 6c      database fil
d1f70 65 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  e, the following
d1f80 20 65 78 70 72 65 73 73 69 6f 6e 20 73 68 6f 75   expression shou
d1f90 6c 64 20 61 6c 77 61 79 73 20 62 65 20 74 72 75  ld always be tru
d1fa0 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 69  e:.**.**       i
d1fb0 6e 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c 69 74  nJournal = sqlit
d1fc0 65 33 42 69 74 76 65 63 54 65 73 74 28 70 50 61  e3BitvecTest(pPa
d1fd0 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c  ger->pInJournal,
d1fe0 20 70 67 6e 6f 29 0a 2a 2a 0a 2a 2a 20 20 20 20   pgno).**.**    
d1ff0 20 54 68 65 20 70 50 61 67 65 72 2d 3e 70 49 6e   The pPager->pIn
d2000 4a 6f 75 72 6e 61 6c 20 6f 62 6a 65 63 74 20 69  Journal object i
d2010 73 20 6f 6e 6c 79 20 76 61 6c 69 64 20 66 6f 72  s only valid for
d2020 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a   the original.**
d2030 20 20 20 20 20 70 61 67 65 73 20 6f 66 20 74 68       pages of th
d2040 65 20 64 61 74 61 62 61 73 65 2c 20 6e 6f 74 20  e database, not 
d2050 6e 65 77 20 70 61 67 65 73 20 74 68 61 74 20 61  new pages that a
d2060 72 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  re added to the 
d2070 65 6e 64 0a 2a 2a 20 20 20 20 20 6f 66 20 74 68  end.**     of th
d2080 65 20 64 61 74 61 62 61 73 65 2c 20 73 6f 20 6f  e database, so o
d2090 62 76 69 6f 75 73 6c 79 20 74 68 65 20 61 62 6f  bviously the abo
d20a0 76 65 20 65 78 70 72 65 73 73 69 6f 6e 20 63 61  ve expression ca
d20b0 6e 6e 6f 74 20 62 65 0a 2a 2a 20 20 20 20 20 76  nnot be.**     v
d20c0 61 6c 69 64 20 66 6f 72 20 6e 65 77 20 70 61 67  alid for new pag
d20d0 65 73 2e 20 20 46 6f 72 20 6e 65 77 20 70 61 67  es.  For new pag
d20e0 65 73 20 69 6e 4a 6f 75 72 6e 61 6c 20 69 73 20  es inJournal is 
d20f0 61 6c 77 61 79 73 20 30 2e 0a 2a 2a 0a 2a 2a 20  always 0..**.** 
d2100 64 69 72 74 79 0a 2a 2a 0a 2a 2a 20 20 20 20 20  dirty.**.**     
d2110 57 68 65 6e 20 74 72 75 65 2c 20 74 68 69 73 20  When true, this 
d2120 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 63  means that the c
d2130 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61  ontent of the pa
d2140 67 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 20  ge has been.**  
d2150 20 20 20 6d 6f 64 69 66 69 65 64 20 61 6e 64 20     modified and 
d2160 6e 65 65 64 73 20 74 6f 20 62 65 20 77 72 69 74  needs to be writ
d2170 74 65 6e 20 62 61 63 6b 20 74 6f 20 74 68 65 20  ten back to the 
d2180 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
d2190 2a 20 20 20 20 20 49 66 20 66 61 6c 73 65 2c 20  *     If false, 
d21a0 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 65 69  it means that ei
d21b0 74 68 65 72 20 74 68 65 20 63 6f 6e 74 65 6e 74  ther the content
d21c0 20 6f 66 20 74 68 65 20 70 61 67 65 20 69 73 0a   of the page is.
d21d0 2a 2a 20 20 20 20 20 75 6e 63 68 61 6e 67 65 64  **     unchanged
d21e0 20 6f 72 20 65 6c 73 65 20 74 68 65 20 63 6f 6e   or else the con
d21f0 74 65 6e 74 20 69 73 20 75 6e 69 6d 70 6f 72 74  tent is unimport
d2200 61 6e 74 20 61 6e 64 20 77 65 20 64 6f 20 6e 6f  ant and we do no
d2210 74 0a 2a 2a 20 20 20 20 20 63 61 72 65 20 77 68  t.**     care wh
d2220 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 69 74 20  ether or not it 
d2230 69 73 20 70 72 65 73 65 72 76 65 64 2e 0a 2a 2a  is preserved..**
d2240 0a 2a 2a 20 61 6c 77 61 79 73 52 6f 6c 6c 62 61  .** alwaysRollba
d2250 63 6b 0a 2a 2a 0a 2a 2a 20 20 20 20 20 54 68 69  ck.**.**     Thi
d2260 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  s means that the
d2270 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e   sqlite3PagerDon
d2280 74 52 6f 6c 6c 62 61 63 6b 28 29 20 41 50 49 20  tRollback() API 
d2290 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 20 20 20  should be.**    
d22a0 20 69 67 6e 6f 72 65 64 20 66 6f 72 20 74 68 69   ignored for thi
d22b0 73 20 70 61 67 65 2e 20 20 54 68 65 20 44 6f 6e  s page.  The Don
d22c0 74 52 6f 6c 6c 62 61 63 6b 28 29 20 41 50 49 20  tRollback() API 
d22d0 61 74 74 65 6d 70 74 73 20 74 6f 20 73 61 79 0a  attempts to say.
d22e0 2a 2a 20 20 20 20 20 74 68 61 74 20 74 68 65 20  **     that the 
d22f0 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70  content of the p
d2300 61 67 65 20 6f 6e 20 64 69 73 6b 20 69 73 20 75  age on disk is u
d2310 6e 69 6d 70 6f 72 74 61 6e 74 20 28 69 74 20 69  nimportant (it i
d2320 73 20 61 6e 0a 2a 2a 20 20 20 20 20 75 6e 75 73  s an.**     unus
d2330 65 64 20 70 61 67 65 20 6f 6e 20 74 68 65 20 66  ed page on the f
d2340 72 65 65 6c 69 73 74 29 20 73 6f 20 74 68 61 74  reelist) so that
d2350 20 69 74 20 69 73 20 75 6e 6e 65 63 65 73 73 61   it is unnecessa
d2360 72 79 20 74 6f 20 0a 2a 2a 20 20 20 20 20 72 6f  ry to .**     ro
d2370 6c 6c 62 61 63 6b 20 63 68 61 6e 67 65 73 20 74  llback changes t
d2380 6f 20 74 68 69 73 20 70 61 67 65 20 62 65 63 61  o this page beca
d2390 75 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  use the content 
d23a0 6f 66 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 20  of the page.**  
d23b0 20 20 20 63 61 6e 20 63 68 61 6e 67 65 20 77 69     can change wi
d23c0 74 68 6f 75 74 20 63 68 61 6e 67 69 6e 67 20 74  thout changing t
d23d0 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 74 68  he meaning of th
d23e0 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69  e database.  Thi
d23f0 73 0a 2a 2a 20 20 20 20 20 66 6c 61 67 20 6f 76  s.**     flag ov
d2400 65 72 72 69 64 65 73 20 61 6e 79 20 44 6f 6e 74  errides any Dont
d2410 52 6f 6c 6c 62 61 63 6b 28 29 20 61 74 74 65 6d  Rollback() attem
d2420 70 74 2e 20 20 54 68 69 73 20 66 6c 61 67 20 69  pt.  This flag i
d2430 73 20 73 65 74 0a 2a 2a 20 20 20 20 20 77 68 65  s set.**     whe
d2440 6e 20 61 20 70 61 67 65 20 74 68 61 74 20 6f 72  n a page that or
d2450 69 67 69 6e 61 6c 6c 79 20 63 6f 6e 74 61 69 6e  iginally contain
d2460 65 64 20 76 61 6c 69 64 20 64 61 74 61 20 69 73  ed valid data is
d2470 20 61 64 64 65 64 20 74 6f 0a 2a 2a 20 20 20 20   added to.**    
d2480 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20   the freelist.  
d2490 4c 61 74 65 72 20 69 6e 20 74 68 65 20 73 61 6d  Later in the sam
d24a0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74  e transaction, t
d24b0 68 69 73 20 70 61 67 65 20 6d 69 67 68 74 0a 2a  his page might.*
d24c0 2a 20 20 20 20 20 62 65 20 70 75 6c 6c 65 64 20  *     be pulled 
d24d0 66 72 6f 6d 20 74 68 65 20 66 72 65 65 6c 69 73  from the freelis
d24e0 74 20 61 6e 64 20 72 65 75 73 65 64 20 66 6f 72  t and reused for
d24f0 20 73 6f 6d 65 74 68 69 6e 67 20 64 69 66 66 65   something diffe
d2500 72 65 6e 74 0a 2a 2a 20 20 20 20 20 61 6e 64 20  rent.**     and 
d2510 61 74 20 74 68 61 74 20 70 6f 69 6e 74 20 74 68  at that point th
d2520 65 20 44 6f 6e 74 52 6f 6c 6c 62 61 63 6b 28 29  e DontRollback()
d2530 20 41 50 49 20 77 69 6c 6c 20 62 65 20 63 61 6c   API will be cal
d2540 6c 65 64 20 62 65 63 61 75 73 65 0a 2a 2a 20 20  led because.**  
d2550 20 20 20 70 61 67 65 73 20 74 61 6b 65 6e 20 66     pages taken f
d2560 72 6f 6d 20 74 68 65 20 66 72 65 65 6c 69 73 74  rom the freelist
d2570 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20   do not need to 
d2580 62 65 20 70 72 6f 74 65 63 74 65 64 20 62 79 0a  be protected by.
d2590 2a 2a 20 20 20 20 20 74 68 65 20 72 6f 6c 6c 62  **     the rollb
d25a0 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20 42 75  ack journal.  Bu
d25b0 74 20 74 68 69 73 20 66 6c 61 67 20 73 61 79 73  t this flag says
d25c0 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 77   that the page w
d25d0 61 73 0a 2a 2a 20 20 20 20 20 6e 6f 74 20 6f 72  as.**     not or
d25e0 69 67 69 6e 61 6c 6c 79 20 70 61 72 74 20 6f 66  iginally part of
d25f0 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 73 6f   the freelist so
d2600 20 74 68 61 74 20 69 74 20 73 74 69 6c 6c 20 6e   that it still n
d2610 65 65 64 73 20 74 6f 0a 2a 2a 20 20 20 20 20 62  eeds to.**     b
d2620 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69 6e  e rolled back in
d2630 20 73 70 69 74 65 20 6f 66 20 61 6e 79 20 73 75   spite of any su
d2640 62 73 65 71 75 65 6e 74 20 44 6f 6e 74 52 6f 6c  bsequent DontRol
d2650 6c 62 61 63 6b 28 29 20 63 61 6c 6c 73 2e 0a 2a  lback() calls..*
d2660 2a 0a 2a 2a 20 6e 65 65 64 52 65 61 64 20 0a 2a  *.** needRead .*
d2670 2a 0a 2a 2a 20 20 20 20 20 54 68 69 73 20 66 6c  *.**     This fl
d2680 61 67 20 6d 65 61 6e 73 20 28 77 68 65 6e 20 74  ag means (when t
d2690 72 75 65 29 20 74 68 61 74 20 74 68 65 20 63 6f  rue) that the co
d26a0 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67  ntent of the pag
d26b0 65 20 68 61 73 0a 2a 2a 20 20 20 20 20 6e 6f 74  e has.**     not
d26c0 20 79 65 74 20 62 65 65 6e 20 6c 6f 61 64 65 64   yet been loaded
d26d0 20 66 72 6f 6d 20 64 69 73 6b 2e 20 20 54 68 65   from disk.  The
d26e0 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 6f 6e 74 65   in-memory conte
d26f0 6e 74 20 69 73 20 6a 75 73 74 0a 2a 2a 20 20 20  nt is just.**   
d2700 20 20 67 61 72 62 61 67 65 2e 20 20 28 41 63 74    garbage.  (Act
d2710 75 61 6c 6c 79 2c 20 77 65 20 7a 65 72 6f 20 74  ually, we zero t
d2720 68 65 20 63 6f 6e 74 65 6e 74 2c 20 62 75 74 20  he content, but 
d2730 79 6f 75 20 73 68 6f 75 6c 64 20 6e 6f 74 0a 2a  you should not.*
d2740 2a 20 20 20 20 20 6d 61 6b 65 20 61 6e 79 20 61  *     make any a
d2750 73 73 75 6d 70 74 69 6f 6e 73 20 61 62 6f 75 74  ssumptions about
d2760 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6e 65 76   the content nev
d2770 65 72 74 68 65 6c 65 73 73 2e 29 20 20 49 66 20  ertheless.)  If 
d2780 74 68 65 0a 2a 2a 20 20 20 20 20 63 6f 6e 74 65  the.**     conte
d2790 6e 74 20 69 73 20 6e 65 65 64 65 64 20 69 6e 20  nt is needed in 
d27a0 74 68 65 20 66 75 74 75 72 65 2c 20 69 74 20 73  the future, it s
d27b0 68 6f 75 6c 64 20 62 65 20 72 65 61 64 20 66 72  hould be read fr
d27c0 6f 6d 20 74 68 65 0a 2a 2a 20 20 20 20 20 6f 72  om the.**     or
d27d0 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20  iginal database 
d27e0 66 69 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  file..*/.struct 
d27f0 50 67 48 64 72 20 7b 0a 20 20 50 61 67 65 72 20  PgHdr {.  Pager 
d2800 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20 20 20  *pPager;        
d2810 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
d2820 70 61 67 65 72 20 74 6f 20 77 68 69 63 68 20 74  pager to which t
d2830 68 69 73 20 70 61 67 65 20 62 65 6c 6f 6e 67 73  his page belongs
d2840 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b   */.  Pgno pgno;
d2850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d2860 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65       /* The page
d2870 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 69 73   number for this
d2880 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 48 64 72   page */.  PgHdr
d2890 20 2a 70 4e 65 78 74 48 61 73 68 2c 20 2a 70 50   *pNextHash, *pP
d28a0 72 65 76 48 61 73 68 3b 20 20 2f 2a 20 48 61 73  revHash;  /* Has
d28b0 68 20 63 6f 6c 6c 69 73 69 6f 6e 20 63 68 61 69  h collision chai
d28c0 6e 20 66 6f 72 20 50 67 48 64 72 2e 70 67 6e 6f  n for PgHdr.pgno
d28d0 20 2a 2f 0a 20 20 50 61 67 65 72 4c 72 75 4c 69   */.  PagerLruLi
d28e0 6e 6b 20 66 72 65 65 3b 20 20 20 20 20 20 20 20  nk free;        
d28f0 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 61 6e 64       /* Next and
d2900 20 70 72 65 76 69 6f 75 73 20 66 72 65 65 20 70   previous free p
d2910 61 67 65 73 20 2a 2f 0a 20 20 50 67 48 64 72 20  ages */.  PgHdr 
d2920 2a 70 4e 65 78 74 41 6c 6c 3b 20 20 20 20 20 20  *pNextAll;      
d2930 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 6c 69           /* A li
d2940 73 74 20 6f 66 20 61 6c 6c 20 70 61 67 65 73 20  st of all pages 
d2950 2a 2f 0a 20 20 75 38 20 69 6e 4a 6f 75 72 6e 61  */.  u8 inJourna
d2960 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
d2970 20 20 20 20 2f 2a 20 54 52 55 45 20 69 66 20 68      /* TRUE if h
d2980 61 73 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20  as been written 
d2990 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20  to journal */.  
d29a0 75 38 20 64 69 72 74 79 3b 20 20 20 20 20 20 20  u8 dirty;       
d29b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
d29c0 2a 20 54 52 55 45 20 69 66 20 77 65 20 6e 65 65  * TRUE if we nee
d29d0 64 20 74 6f 20 77 72 69 74 65 20 62 61 63 6b 20  d to write back 
d29e0 63 68 61 6e 67 65 73 20 2a 2f 0a 20 20 75 38 20  changes */.  u8 
d29f0 6e 65 65 64 53 79 6e 63 3b 20 20 20 20 20 20 20  needSync;       
d2a00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
d2a10 79 6e 63 20 6a 6f 75 72 6e 61 6c 20 62 65 66 6f  ync journal befo
d2a20 72 65 20 77 72 69 74 69 6e 67 20 74 68 69 73 20  re writing this 
d2a30 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 61 6c 77  page */.  u8 alw
d2a40 61 79 73 52 6f 6c 6c 62 61 63 6b 3b 20 20 20 20  aysRollback;    
d2a50 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 73 61           /* Disa
d2a60 62 6c 65 20 44 6f 6e 74 52 6f 6c 6c 62 61 63 6b  ble DontRollback
d2a70 28 29 20 66 6f 72 20 74 68 69 73 20 70 61 67 65  () for this page
d2a80 20 2a 2f 0a 20 20 75 38 20 6e 65 65 64 52 65 61   */.  u8 needRea
d2a90 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
d2aa0 20 20 20 20 20 2f 2a 20 52 65 61 64 20 63 6f 6e       /* Read con
d2ab0 74 65 6e 74 20 69 66 20 50 61 67 65 72 57 72 69  tent if PagerWri
d2ac0 74 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 2a  te() is called *
d2ad0 2f 0a 20 20 73 68 6f 72 74 20 69 6e 74 20 6e 52  /.  short int nR
d2ae0 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ef;             
d2af0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
d2b00 75 73 65 72 73 20 6f 66 20 74 68 69 73 20 70 61  users of this pa
d2b10 67 65 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70  ge */.  PgHdr *p
d2b20 44 69 72 74 79 2c 20 2a 70 50 72 65 76 44 69 72  Dirty, *pPrevDir
d2b30 74 79 3b 20 20 20 20 2f 2a 20 44 69 72 74 79 20  ty;    /* Dirty 
d2b40 70 61 67 65 73 20 2a 2f 0a 23 69 66 64 65 66 20  pages */.#ifdef 
d2b50 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
d2b60 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a  MORY_MANAGEMENT.
d2b70 20 20 50 61 67 65 72 4c 72 75 4c 69 6e 6b 20 67    PagerLruLink g
d2b80 66 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20  free;           
d2b90 20 2f 2a 20 47 6c 6f 62 61 6c 20 6c 69 73 74 20   /* Global list 
d2ba0 6f 66 20 6e 52 65 66 3d 3d 30 20 70 61 67 65 73  of nRef==0 pages
d2bb0 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65   */.#endif.#ifde
d2bc0 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50  f SQLITE_CHECK_P
d2bd0 41 47 45 53 0a 20 20 75 33 32 20 70 61 67 65 48  AGES.  u32 pageH
d2be0 61 73 68 3b 0a 23 65 6e 64 69 66 0a 20 20 76 6f  ash;.#endif.  vo
d2bf0 69 64 20 2a 70 44 61 74 61 3b 20 20 20 20 20 20  id *pData;      
d2c00 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
d2c10 50 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 2f  Page data */.  /
d2c20 2a 20 50 61 67 65 72 2e 6e 45 78 74 72 61 20 62  * Pager.nExtra b
d2c30 79 74 65 73 20 6f 66 20 6c 6f 63 61 6c 20 64 61  ytes of local da
d2c40 74 61 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74  ta appended to t
d2c50 68 69 73 20 68 65 61 64 65 72 20 2a 2f 0a 7d 3b  his header */.};
d2c60 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 61 6e 20 69  ../*.** For an i
d2c70 6e 2d 6d 65 6d 6f 72 79 20 6f 6e 6c 79 20 64 61  n-memory only da
d2c80 74 61 62 61 73 65 2c 20 73 6f 6d 65 20 65 78 74  tabase, some ext
d2c90 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  ra information i
d2ca0 73 20 72 65 63 6f 72 64 65 64 20 61 62 6f 75 74  s recorded about
d2cb0 0a 2a 2a 20 65 61 63 68 20 70 61 67 65 20 73 6f  .** each page so
d2cc0 20 74 68 61 74 20 63 68 61 6e 67 65 73 20 63 61   that changes ca
d2cd0 6e 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b  n be rolled back
d2ce0 2e 20 20 28 4a 6f 75 72 6e 61 6c 20 66 69 6c 65  .  (Journal file
d2cf0 73 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 75 73 65  s are not.** use
d2d00 64 20 66 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20  d for in-memory 
d2d10 64 61 74 61 62 61 73 65 73 2e 29 20 20 54 68 65  databases.)  The
d2d20 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 66 6f 72   following infor
d2d30 6d 61 74 69 6f 6e 20 69 73 20 61 64 64 65 64 20  mation is added 
d2d40 74 6f 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66  to.** the end of
d2d50 20 65 76 65 72 79 20 45 58 54 52 41 20 62 6c 6f   every EXTRA blo
d2d60 63 6b 20 66 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79  ck for in-memory
d2d70 20 64 61 74 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a   databases..**.*
d2d80 2a 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69  * This informati
d2d90 6f 6e 20 63 6f 75 6c 64 20 68 61 76 65 20 62 65  on could have be
d2da0 65 6e 20 61 64 64 65 64 20 64 69 72 65 63 74 6c  en added directl
d2db0 79 20 74 6f 20 74 68 65 20 50 67 48 64 72 20 73  y to the PgHdr s
d2dc0 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 42 75 74  tructure..** But
d2dd0 20 74 68 65 6e 20 69 74 20 77 6f 75 6c 64 20 74   then it would t
d2de0 61 6b 65 20 75 70 20 61 6e 20 65 78 74 72 61 20  ake up an extra 
d2df0 38 20 62 79 74 65 73 20 6f 66 20 73 74 6f 72 61  8 bytes of stora
d2e00 67 65 20 6f 6e 20 65 76 65 72 79 20 50 67 48 64  ge on every PgHd
d2e10 72 0a 2a 2a 20 65 76 65 6e 20 66 6f 72 20 64 69  r.** even for di
d2e20 73 6b 2d 62 61 73 65 64 20 64 61 74 61 62 61 73  sk-based databas
d2e30 65 73 2e 20 20 53 70 6c 69 74 74 69 6e 67 20 69  es.  Splitting i
d2e40 74 20 6f 75 74 20 73 61 76 65 73 20 38 20 62 79  t out saves 8 by
d2e50 74 65 73 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73  tes.  This.** is
d2e60 20 6f 6e 6c 79 20 61 20 73 61 76 69 6e 67 73 20   only a savings 
d2e70 6f 66 20 30 2e 38 25 20 62 75 74 20 74 68 6f 73  of 0.8% but thos
d2e80 65 20 70 65 72 63 65 6e 74 61 67 65 73 20 61 64  e percentages ad
d2e90 64 20 75 70 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  d up..*/.typedef
d2ea0 20 73 74 72 75 63 74 20 50 67 48 69 73 74 6f 72   struct PgHistor
d2eb0 79 20 50 67 48 69 73 74 6f 72 79 3b 0a 73 74 72  y PgHistory;.str
d2ec0 75 63 74 20 50 67 48 69 73 74 6f 72 79 20 7b 0a  uct PgHistory {.
d2ed0 20 20 75 38 20 2a 70 4f 72 69 67 3b 20 20 20 20    u8 *pOrig;    
d2ee0 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 70 61 67   /* Original pag
d2ef0 65 20 74 65 78 74 2e 20 20 52 65 73 74 6f 72 65  e text.  Restore
d2f00 20 74 6f 20 74 68 69 73 20 6f 6e 20 61 20 66 75   to this on a fu
d2f10 6c 6c 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 20  ll rollback */. 
d2f20 20 75 38 20 2a 70 53 74 6d 74 3b 20 20 20 20 20   u8 *pStmt;     
d2f30 2f 2a 20 54 65 78 74 20 61 73 20 69 74 20 77 61  /* Text as it wa
d2f40 73 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69  s at the beginni
d2f50 6e 67 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  ng of the curren
d2f60 74 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  t statement */. 
d2f70 20 50 67 48 64 72 20 2a 70 4e 65 78 74 53 74 6d   PgHdr *pNextStm
d2f80 74 2c 20 2a 70 50 72 65 76 53 74 6d 74 3b 20 20  t, *pPrevStmt;  
d2f90 2f 2a 20 4c 69 73 74 20 6f 66 20 70 61 67 65 73  /* List of pages
d2fa0 20 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e   in the statemen
d2fb0 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75  t journal */.  u
d2fc0 38 20 69 6e 53 74 6d 74 3b 20 20 20 20 20 20 20  8 inStmt;       
d2fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
d2fe0 20 54 52 55 45 20 69 66 20 69 6e 20 74 68 65 20   TRUE if in the 
d2ff0 73 74 61 74 65 6d 65 6e 74 20 73 75 62 6a 6f 75  statement subjou
d3000 72 6e 61 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  rnal */.};../*.*
d3010 2a 20 41 20 6d 61 63 72 6f 20 75 73 65 64 20 66  * A macro used f
d3020 6f 72 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20  or invoking the 
d3030 63 6f 64 65 63 20 69 66 20 74 68 65 72 65 20 69  codec if there i
d3040 73 20 6f 6e 65 0a 2a 2f 0a 23 69 66 64 65 66 20  s one.*/.#ifdef 
d3050 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43  SQLITE_HAS_CODEC
d3060 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 31  .# define CODEC1
d3070 28 50 2c 44 2c 4e 2c 58 29 20 69 66 28 20 50 2d  (P,D,N,X) if( P-
d3080 3e 78 43 6f 64 65 63 21 3d 30 20 29 7b 20 50 2d  >xCodec!=0 ){ P-
d3090 3e 78 43 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65  >xCodec(P->pCode
d30a0 63 41 72 67 2c 44 2c 4e 2c 58 29 3b 20 7d 0a 23  cArg,D,N,X); }.#
d30b0 20 64 65 66 69 6e 65 20 43 4f 44 45 43 32 28 50   define CODEC2(P
d30c0 2c 44 2c 4e 2c 58 29 20 28 28 63 68 61 72 2a 29  ,D,N,X) ((char*)
d30d0 28 50 2d 3e 78 43 6f 64 65 63 21 3d 30 3f 50 2d  (P->xCodec!=0?P-
d30e0 3e 78 43 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65  >xCodec(P->pCode
d30f0 63 41 72 67 2c 44 2c 4e 2c 58 29 3a 44 29 29 0a  cArg,D,N,X):D)).
d3100 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 43  #else.# define C
d3110 4f 44 45 43 31 28 50 2c 44 2c 4e 2c 58 29 20 2f  ODEC1(P,D,N,X) /
d3120 2a 20 4e 4f 2d 4f 50 20 2a 2f 0a 23 20 64 65 66  * NO-OP */.# def
d3130 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 2c 4e  ine CODEC2(P,D,N
d3140 2c 58 29 20 28 28 63 68 61 72 2a 29 44 29 0a 23  ,X) ((char*)D).#
d3150 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  endif../*.** Con
d3160 76 65 72 74 20 61 20 70 6f 69 6e 74 65 72 20 74  vert a pointer t
d3170 6f 20 61 20 50 67 48 64 72 20 69 6e 74 6f 20 61  o a PgHdr into a
d3180 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73 20   pointer to its 
d3190 64 61 74 61 0a 2a 2a 20 61 6e 64 20 62 61 63 6b  data.** and back
d31a0 20 61 67 61 69 6e 2e 0a 2a 2f 0a 23 64 65 66 69   again..*/.#defi
d31b0 6e 65 20 50 47 48 44 52 5f 54 4f 5f 44 41 54 41  ne PGHDR_TO_DATA
d31c0 28 50 29 20 20 20 20 28 28 50 29 2d 3e 70 44 61  (P)    ((P)->pDa
d31d0 74 61 29 0a 23 64 65 66 69 6e 65 20 50 47 48 44  ta).#define PGHD
d31e0 52 5f 54 4f 5f 45 58 54 52 41 28 47 2c 50 29 20  R_TO_EXTRA(G,P) 
d31f0 28 28 76 6f 69 64 2a 29 26 28 28 47 29 5b 31 5d  ((void*)&((G)[1]
d3200 29 29 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52  )).#define PGHDR
d3210 5f 54 4f 5f 48 49 53 54 28 50 2c 50 47 52 29 20  _TO_HIST(P,PGR) 
d3220 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 28   \.            (
d3230 28 50 67 48 69 73 74 6f 72 79 2a 29 26 28 28 63  (PgHistory*)&((c
d3240 68 61 72 2a 29 28 26 28 50 29 5b 31 5d 29 29 5b  har*)(&(P)[1]))[
d3250 28 50 47 52 29 2d 3e 6e 45 78 74 72 61 5d 29 0a  (PGR)->nExtra]).
d3260 0a 2f 2a 0a 2a 2a 20 41 20 6f 70 65 6e 20 70 61  ./*.** A open pa
d3270 67 65 20 63 61 63 68 65 20 69 73 20 61 6e 20 69  ge cache is an i
d3280 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
d3290 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
d32a0 72 65 2e 0a 2a 2a 0a 2a 2a 20 50 61 67 65 72 2e  re..**.** Pager.
d32b0 65 72 72 43 6f 64 65 20 6d 61 79 20 62 65 20 73  errCode may be s
d32c0 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 49 4f 45  et to SQLITE_IOE
d32d0 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  RR, SQLITE_CORRU
d32e0 50 54 2c 20 6f 72 0a 2a 2a 20 6f 72 20 53 51 4c  PT, or.** or SQL
d32f0 49 54 45 5f 46 55 4c 4c 2e 20 4f 6e 63 65 20 6f  ITE_FULL. Once o
d3300 6e 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 20  ne of the first 
d3310 74 68 72 65 65 20 65 72 72 6f 72 73 20 6f 63 63  three errors occ
d3320 75 72 73 2c 20 69 74 20 70 65 72 73 69 73 74 73  urs, it persists
d3330 0a 2a 2a 20 61 6e 64 20 69 73 20 72 65 74 75 72  .** and is retur
d3340 6e 65 64 20 61 73 20 74 68 65 20 72 65 73 75 6c  ned as the resul
d3350 74 20 6f 66 20 65 76 65 72 79 20 6d 61 6a 6f 72  t of every major
d3360 20 70 61 67 65 72 20 41 50 49 20 63 61 6c 6c 2e   pager API call.
d3370 20 20 54 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f    The.** SQLITE_
d3380 46 55 4c 4c 20 72 65 74 75 72 6e 20 63 6f 64 65  FULL return code
d3390 20 69 73 20 73 6c 69 67 68 74 6c 79 20 64 69 66   is slightly dif
d33a0 66 65 72 65 6e 74 2e 20 49 74 20 70 65 72 73 69  ferent. It persi
d33b0 73 74 73 20 6f 6e 6c 79 20 75 6e 74 69 6c 20 74  sts only until t
d33c0 68 65 0a 2a 2a 20 6e 65 78 74 20 73 75 63 63 65  he.** next succe
d33d0 73 73 66 75 6c 20 72 6f 6c 6c 62 61 63 6b 20 69  ssful rollback i
d33e0 73 20 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 74  s performed on t
d33f0 68 65 20 70 61 67 65 72 20 63 61 63 68 65 2e 20  he pager cache. 
d3400 41 6c 73 6f 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f  Also,.** SQLITE_
d3410 46 55 4c 4c 20 64 6f 65 73 20 6e 6f 74 20 61 66  FULL does not af
d3420 66 65 63 74 20 74 68 65 20 73 71 6c 69 74 65 33  fect the sqlite3
d3430 50 61 67 65 72 47 65 74 28 29 20 61 6e 64 20 73  PagerGet() and s
d3440 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75  qlite3PagerLooku
d3450 70 28 29 0a 2a 2a 20 41 50 49 73 2c 20 74 68 65  p().** APIs, the
d3460 79 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 75  y may still be u
d3470 73 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79  sed successfully
d3480 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 61 67 65  ..*/.struct Page
d3490 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  r {.  sqlite3_vf
d34a0 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20  s *pVfs;        
d34b0 20 20 2f 2a 20 4f 53 20 66 75 6e 63 74 69 6f 6e    /* OS function
d34c0 73 20 74 6f 20 75 73 65 20 66 6f 72 20 49 4f 20  s to use for IO 
d34d0 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61 6c 4f  */.  u8 journalO
d34e0 70 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  pen;            
d34f0 20 2f 2a 20 54 72 75 65 20 69 66 20 6a 6f 75 72   /* True if jour
d3500 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70  nal file descrip
d3510 74 6f 72 73 20 69 73 20 76 61 6c 69 64 20 2a 2f  tors is valid */
d3520 0a 20 20 75 38 20 6a 6f 75 72 6e 61 6c 53 74 61  .  u8 journalSta
d3530 72 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 2f  rted;          /
d3540 2a 20 54 72 75 65 20 69 66 20 68 65 61 64 65 72  * True if header
d3550 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73   of journal is s
d3560 79 6e 63 65 64 20 2a 2f 0a 20 20 75 38 20 75 73  ynced */.  u8 us
d3570 65 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20  eJournal;       
d3580 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 61 20         /* Use a 
d3590 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
d35a0 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f   on this file */
d35b0 0a 20 20 75 38 20 6e 6f 52 65 61 64 6c 6f 63 6b  .  u8 noReadlock
d35c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
d35d0 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20  * Do not bother 
d35e0 74 6f 20 6f 62 74 61 69 6e 20 72 65 61 64 6c 6f  to obtain readlo
d35f0 63 6b 73 20 2a 2f 0a 20 20 75 38 20 73 74 6d 74  cks */.  u8 stmt
d3600 4f 70 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20  Open;           
d3610 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
d3620 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 73 75  the statement su
d3630 62 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65 6e  bjournal is open
d3640 20 2a 2f 0a 20 20 75 38 20 73 74 6d 74 49 6e 55   */.  u8 stmtInU
d3650 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  se;             
d3660 20 20 2f 2a 20 54 72 75 65 20 77 65 20 61 72 65    /* True we are
d3670 20 69 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20   in a statement 
d3680 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a  subtransaction *
d3690 2f 0a 20 20 75 38 20 73 74 6d 74 41 75 74 6f 6f  /.  u8 stmtAutoo
d36a0 70 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  pen;            
d36b0 2f 2a 20 4f 70 65 6e 20 73 74 6d 74 20 6a 6f 75  /* Open stmt jou
d36c0 72 6e 61 6c 20 77 68 65 6e 20 6d 61 69 6e 20 6a  rnal when main j
d36d0 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65 6e 65 64  ournal is opened
d36e0 2a 2f 0a 20 20 75 38 20 6e 6f 53 79 6e 63 3b 20  */.  u8 noSync; 
d36f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d3700 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 79 6e 63 20   /* Do not sync 
d3710 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 66 20 74  the journal if t
d3720 72 75 65 20 2a 2f 0a 20 20 75 38 20 66 75 6c 6c  rue */.  u8 full
d3730 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20  Sync;           
d3740 20 20 20 20 20 2f 2a 20 44 6f 20 65 78 74 72 61       /* Do extra
d3750 20 73 79 6e 63 73 20 6f 66 20 74 68 65 20 6a 6f   syncs of the jo
d3760 75 72 6e 61 6c 20 66 6f 72 20 72 6f 62 75 73 74  urnal for robust
d3770 6e 65 73 73 20 2a 2f 0a 20 20 75 38 20 73 79 6e  ness */.  u8 syn
d3780 63 5f 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20  c_flags;        
d3790 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
d37a0 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f 72 20 53  SYNC_NORMAL or S
d37b0 59 4e 43 5f 46 55 4c 4c 20 2a 2f 0a 20 20 75 38  YNC_FULL */.  u8
d37c0 20 73 74 61 74 65 3b 20 20 20 20 20 20 20 20 20   state;         
d37d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 41 47            /* PAG
d37e0 45 52 5f 55 4e 4c 4f 43 4b 2c 20 5f 53 48 41 52  ER_UNLOCK, _SHAR
d37f0 45 44 2c 20 5f 52 45 53 45 52 56 45 44 2c 20 65  ED, _RESERVED, e
d3800 74 63 2e 20 2a 2f 0a 20 20 75 38 20 74 65 6d 70  tc. */.  u8 temp
d3810 46 69 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20  File;           
d3820 20 20 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d       /* zFilenam
d3830 65 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79  e is a temporary
d3840 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 72 65   file */.  u8 re
d3850 61 64 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20  adOnly;         
d3860 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66         /* True f
d3870 6f 72 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 64  or a read-only d
d3880 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20  atabase */.  u8 
d3890 6e 65 65 64 53 79 6e 63 3b 20 20 20 20 20 20 20  needSync;       
d38a0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
d38b0 20 69 66 20 61 6e 20 66 73 79 6e 63 28 29 20 69   if an fsync() i
d38c0 73 20 6e 65 65 64 65 64 20 6f 6e 20 74 68 65 20  s needed on the 
d38d0 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20  journal */.  u8 
d38e0 64 69 72 74 79 43 61 63 68 65 3b 20 20 20 20 20  dirtyCache;     
d38f0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
d3900 20 69 66 20 63 61 63 68 65 64 20 70 61 67 65 73   if cached pages
d3910 20 68 61 76 65 20 63 68 61 6e 67 65 64 20 2a 2f   have changed */
d3920 0a 20 20 75 38 20 61 6c 77 61 79 73 52 6f 6c 6c  .  u8 alwaysRoll
d3930 62 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20 2f  back;          /
d3940 2a 20 44 69 73 61 62 6c 65 20 44 6f 6e 74 52 6f  * Disable DontRo
d3950 6c 6c 62 61 63 6b 28 29 20 66 6f 72 20 61 6c 6c  llback() for all
d3960 20 70 61 67 65 73 20 2a 2f 0a 20 20 75 38 20 6d   pages */.  u8 m
d3970 65 6d 44 62 3b 20 20 20 20 20 20 20 20 20 20 20  emDb;           
d3980 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
d3990 74 6f 20 69 6e 68 69 62 69 74 20 61 6c 6c 20 66  to inhibit all f
d39a0 69 6c 65 20 49 2f 4f 20 2a 2f 0a 20 20 75 38 20  ile I/O */.  u8 
d39b0 73 65 74 4d 61 73 74 65 72 3b 20 20 20 20 20 20  setMaster;      
d39c0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
d39d0 20 69 66 20 61 20 6d 2d 6a 20 6e 61 6d 65 20 68   if a m-j name h
d39e0 61 73 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20  as been written 
d39f0 74 6f 20 6a 72 6e 6c 20 2a 2f 0a 20 20 75 38 20  to jrnl */.  u8 
d3a00 64 6f 4e 6f 74 53 79 6e 63 3b 20 20 20 20 20 20  doNotSync;      
d3a10 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c           /* Bool
d3a20 65 61 6e 2e 20 57 68 69 6c 65 20 74 72 75 65 2c  ean. While true,
d3a30 20 64 6f 20 6e 6f 74 20 73 70 69 6c 6c 20 74 68   do not spill th
d3a40 65 20 63 61 63 68 65 20 2a 2f 0a 20 20 75 38 20  e cache */.  u8 
d3a50 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 3b 20 20  exclusiveMode;  
d3a60 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c           /* Bool
d3a70 65 61 6e 2e 20 54 72 75 65 20 69 66 20 6c 6f 63  ean. True if loc
d3a80 6b 69 6e 67 5f 6d 6f 64 65 3d 3d 45 58 43 4c 55  king_mode==EXCLU
d3a90 53 49 56 45 20 2a 2f 0a 20 20 75 38 20 6a 6f 75  SIVE */.  u8 jou
d3aa0 72 6e 61 6c 4d 6f 64 65 3b 20 20 20 20 20 20 20  rnalMode;       
d3ab0 20 20 20 20 20 20 2f 2a 20 4f 6e 20 6f 66 20 74        /* On of t
d3ac0 68 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  he PAGER_JOURNAL
d3ad0 4d 4f 44 45 5f 2a 20 76 61 6c 75 65 73 20 2a 2f  MODE_* values */
d3ae0 0a 20 20 75 38 20 64 62 4d 6f 64 69 66 69 65 64  .  u8 dbModified
d3af0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
d3b00 2a 20 54 72 75 65 20 69 66 20 74 68 65 72 65 20  * True if there 
d3b10 61 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20  are any changes 
d3b20 74 6f 20 74 68 65 20 44 62 20 2a 2f 0a 20 20 75  to the Db */.  u
d3b30 38 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e  8 changeCountDon
d3b40 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65  e;         /* Se
d3b50 74 20 61 66 74 65 72 20 69 6e 63 72 65 6d 65 6e  t after incremen
d3b60 74 69 6e 67 20 74 68 65 20 63 68 61 6e 67 65 2d  ting the change-
d3b70 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 33 32  counter */.  u32
d3b80 20 76 66 73 46 6c 61 67 73 3b 20 20 20 20 20 20   vfsFlags;      
d3b90 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67           /* Flag
d3ba0 73 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 76 66  s for sqlite3_vf
d3bb0 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 69  s.xOpen() */.  i
d3bc0 6e 74 20 65 72 72 43 6f 64 65 3b 20 20 20 20 20  nt errCode;     
d3bd0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
d3be0 65 20 6f 66 20 73 65 76 65 72 61 6c 20 6b 69 6e  e of several kin
d3bf0 64 73 20 6f 66 20 65 72 72 6f 72 73 20 2a 2f 0a  ds of errors */.
d3c00 20 20 69 6e 74 20 64 62 53 69 7a 65 3b 20 20 20    int dbSize;   
d3c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
d3c20 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   Number of pages
d3c30 20 69 6e 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a   in the file */.
d3c40 20 20 69 6e 74 20 6f 72 69 67 44 62 53 69 7a 65    int origDbSize
d3c50 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
d3c60 20 64 62 53 69 7a 65 20 62 65 66 6f 72 65 20 74   dbSize before t
d3c70 68 65 20 63 75 72 72 65 6e 74 20 63 68 61 6e 67  he current chang
d3c80 65 20 2a 2f 0a 20 20 69 6e 74 20 73 74 6d 74 53  e */.  int stmtS
d3c90 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ize;            
d3ca0 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 64 61     /* Size of da
d3cb0 74 61 62 61 73 65 20 28 69 6e 20 70 61 67 65 73  tabase (in pages
d3cc0 29 20 61 74 20 73 74 6d 74 5f 62 65 67 69 6e 28  ) at stmt_begin(
d3cd0 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 63 3b  ) */.  int nRec;
d3ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d3cf0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
d3d00 70 61 67 65 73 20 77 72 69 74 74 65 6e 20 74 6f  pages written to
d3d10 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a   the journal */.
d3d20 20 20 75 33 32 20 63 6b 73 75 6d 49 6e 69 74 3b    u32 cksumInit;
d3d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
d3d40 20 51 75 61 73 69 2d 72 61 6e 64 6f 6d 20 76 61   Quasi-random va
d3d50 6c 75 65 20 61 64 64 65 64 20 74 6f 20 65 76 65  lue added to eve
d3d60 72 79 20 63 68 65 63 6b 73 75 6d 20 2a 2f 0a 20  ry checksum */. 
d3d70 20 69 6e 74 20 73 74 6d 74 4e 52 65 63 3b 20 20   int stmtNRec;  
d3d80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
d3d90 4e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 64  Number of record
d3da0 73 20 69 6e 20 73 74 6d 74 20 73 75 62 6a 6f 75  s in stmt subjou
d3db0 72 6e 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 45  rnal */.  int nE
d3dc0 78 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20  xtra;           
d3dd0 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 69        /* Add thi
d3de0 73 20 6d 61 6e 79 20 62 79 74 65 73 20 74 6f 20  s many bytes to 
d3df0 65 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 20 70  each in-memory p
d3e00 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 70 61 67  age */.  int pag
d3e10 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20  eSize;          
d3e20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
d3e30 66 20 62 79 74 65 73 20 69 6e 20 61 20 70 61 67  f bytes in a pag
d3e40 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 67 65  e */.  int nPage
d3e50 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
d3e60 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62     /* Total numb
d3e70 65 72 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20  er of in-memory 
d3e80 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  pages */.  int n
d3e90 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  Ref;            
d3ea0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
d3eb0 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61   of in-memory pa
d3ec0 67 65 73 20 77 69 74 68 20 50 67 48 64 72 2e 6e  ges with PgHdr.n
d3ed0 52 65 66 3e 30 20 2a 2f 0a 20 20 69 6e 74 20 6d  Ref>0 */.  int m
d3ee0 78 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20  xPage;          
d3ef0 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
d3f00 6d 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  m number of page
d3f10 73 20 74 6f 20 68 6f 6c 64 20 69 6e 20 63 61 63  s to hold in cac
d3f20 68 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78 50  he */.  Pgno mxP
d3f30 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  gno;            
d3f40 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61      /* Maximum a
d3f50 6c 6c 6f 77 65 64 20 73 69 7a 65 20 6f 66 20 74  llowed size of t
d3f60 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  he database */. 
d3f70 20 42 69 74 76 65 63 20 2a 70 49 6e 4a 6f 75 72   Bitvec *pInJour
d3f80 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  nal;         /* 
d3f90 4f 6e 65 20 62 69 74 20 66 6f 72 20 65 61 63 68  One bit for each
d3fa0 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74   page in the dat
d3fb0 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20  abase file */.  
d3fc0 42 69 74 76 65 63 20 2a 70 49 6e 53 74 6d 74 3b  Bitvec *pInStmt;
d3fd0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
d3fe0 6e 65 20 62 69 74 20 66 6f 72 20 65 61 63 68 20  ne bit for each 
d3ff0 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61  page in the data
d4000 62 61 73 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  base */.  char *
d4010 7a 46 69 6c 65 6e 61 6d 65 3b 20 20 20 20 20 20  zFilename;      
d4020 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
d4030 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
d4040 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4a  le */.  char *zJ
d4050 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20  ournal;         
d4060 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
d4070 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
d4080 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44 69 72 65  */.  char *zDire
d4090 63 74 6f 72 79 3b 20 20 20 20 20 20 20 20 20 20  ctory;          
d40a0 20 2f 2a 20 44 69 72 65 63 74 6f 72 79 20 68 6f   /* Directory ho
d40b0 6c 64 20 64 61 74 61 62 61 73 65 20 61 6e 64 20  ld database and 
d40c0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f  journal files */
d40d0 0a 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 4a 72  .  char *zStmtJr
d40e0 6e 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  nl;            /
d40f0 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 73 74  * Name of the st
d4100 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20  atement journal 
d4110 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  file */.  sqlite
d4120 33 5f 66 69 6c 65 20 2a 66 64 2c 20 2a 6a 66 64  3_file *fd, *jfd
d4130 3b 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65  ;     /* File de
d4140 73 63 72 69 70 74 6f 72 73 20 66 6f 72 20 64 61  scriptors for da
d4150 74 61 62 61 73 65 20 61 6e 64 20 6a 6f 75 72 6e  tabase and journ
d4160 61 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  al */.  sqlite3_
d4170 66 69 6c 65 20 2a 73 74 66 64 3b 20 20 20 20 20  file *stfd;     
d4180 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63      /* File desc
d4190 72 69 70 74 6f 72 20 66 6f 72 20 74 68 65 20 73  riptor for the s
d41a0 74 61 74 65 6d 65 6e 74 20 73 75 62 6a 6f 75 72  tatement subjour
d41b0 6e 61 6c 2a 2f 0a 20 20 42 75 73 79 48 61 6e 64  nal*/.  BusyHand
d41c0 6c 65 72 20 2a 70 42 75 73 79 48 61 6e 64 6c 65  ler *pBusyHandle
d41d0 72 3b 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74  r;  /* Pointer t
d41e0 6f 20 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e  o sqlite.busyHan
d41f0 64 6c 65 72 20 2a 2f 0a 20 20 50 61 67 65 72 4c  dler */.  PagerL
d4200 72 75 4c 69 73 74 20 6c 72 75 3b 20 20 20 20 20  ruList lru;     
d4210 20 20 20 20 20 20 2f 2a 20 4c 52 55 20 6c 69 73        /* LRU lis
d4220 74 20 6f 66 20 66 72 65 65 20 70 61 67 65 73 20  t of free pages 
d4230 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 41 6c 6c  */.  PgHdr *pAll
d4240 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
d4250 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c 20   /* List of all 
d4260 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 48 64 72  pages */.  PgHdr
d4270 20 2a 70 53 74 6d 74 3b 20 20 20 20 20 20 20 20   *pStmt;        
d4280 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f         /* List o
d4290 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 73  f pages in the s
d42a0 74 61 74 65 6d 65 6e 74 20 73 75 62 6a 6f 75 72  tatement subjour
d42b0 6e 61 6c 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a  nal */.  PgHdr *
d42c0 70 44 69 72 74 79 3b 20 20 20 20 20 20 20 20 20  pDirty;         
d42d0 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
d42e0 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20  all dirty pages 
d42f0 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c  */.  i64 journal
d4300 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  Off;            
d4310 20 2f 2a 20 43 75 72 72 65 6e 74 20 62 79 74 65   /* Current byte
d4320 20 6f 66 66 73 65 74 20 69 6e 20 74 68 65 20 6a   offset in the j
d4330 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20  ournal file */. 
d4340 20 69 36 34 20 6a 6f 75 72 6e 61 6c 48 64 72 3b   i64 journalHdr;
d4350 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
d4360 42 79 74 65 20 6f 66 66 73 65 74 20 74 6f 20 70  Byte offset to p
d4370 72 65 76 69 6f 75 73 20 6a 6f 75 72 6e 61 6c 20  revious journal 
d4380 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 20  header */.  i64 
d4390 73 74 6d 74 48 64 72 4f 66 66 3b 20 20 20 20 20  stmtHdrOff;     
d43a0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
d43b0 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20   journal header 
d43c0 77 72 69 74 74 65 6e 20 74 68 69 73 20 73 74 61  written this sta
d43d0 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 36 34 20  tement */.  i64 
d43e0 73 74 6d 74 43 6b 73 75 6d 3b 20 20 20 20 20 20  stmtCksum;      
d43f0 20 20 20 20 20 20 20 20 2f 2a 20 63 6b 73 75 6d          /* cksum
d4400 49 6e 69 74 20 77 68 65 6e 20 73 74 61 74 65 6d  Init when statem
d4410 65 6e 74 20 77 61 73 20 73 74 61 72 74 65 64 20  ent was started 
d4420 2a 2f 0a 20 20 69 36 34 20 73 74 6d 74 4a 53 69  */.  i64 stmtJSi
d4430 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
d4440 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6a 6f 75 72   /* Size of jour
d4450 6e 61 6c 20 61 74 20 73 74 6d 74 5f 62 65 67 69  nal at stmt_begi
d4460 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 73 65 63  n() */.  int sec
d4470 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20 20 20  torSize;        
d4480 20 20 20 20 20 2f 2a 20 41 73 73 75 6d 65 64 20       /* Assumed 
d4490 73 65 63 74 6f 72 20 73 69 7a 65 20 64 75 72 69  sector size duri
d44a0 6e 67 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 23  ng rollback */.#
d44b0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
d44c0 54 0a 20 20 69 6e 74 20 6e 48 69 74 2c 20 6e 4d  T.  int nHit, nM
d44d0 69 73 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  iss;            
d44e0 2f 2a 20 43 61 63 68 65 20 68 69 74 73 20 61 6e  /* Cache hits an
d44f0 64 20 6d 69 73 73 69 6e 67 20 2a 2f 0a 20 20 69  d missing */.  i
d4500 6e 74 20 6e 52 65 61 64 2c 20 6e 57 72 69 74 65  nt nRead, nWrite
d4510 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61  ;          /* Da
d4520 74 61 62 61 73 65 20 70 61 67 65 73 20 72 65 61  tabase pages rea
d4530 64 2f 77 72 69 74 74 65 6e 20 2a 2f 0a 23 65 6e  d/written */.#en
d4540 64 69 66 0a 20 20 76 6f 69 64 20 28 2a 78 44 65  dif.  void (*xDe
d4550 73 74 72 75 63 74 6f 72 29 28 44 62 50 61 67 65  structor)(DbPage
d4560 2a 2c 69 6e 74 29 3b 20 2f 2a 20 43 61 6c 6c 20  *,int); /* Call 
d4570 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65  this routine whe
d4580 6e 20 66 72 65 65 69 6e 67 20 70 61 67 65 73 20  n freeing pages 
d4590 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 52 65 69  */.  void (*xRei
d45a0 6e 69 74 65 72 29 28 44 62 50 61 67 65 2a 2c 69  niter)(DbPage*,i
d45b0 6e 74 29 3b 20 20 20 2f 2a 20 43 61 6c 6c 20 74  nt);   /* Call t
d45c0 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e  his routine when
d45d0 20 72 65 6c 6f 61 64 69 6e 67 20 70 61 67 65 73   reloading pages
d45e0 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
d45f0 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 76 6f  E_HAS_CODEC.  vo
d4600 69 64 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 6f  id *(*xCodec)(vo
d4610 69 64 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69  id*,void*,Pgno,i
d4620 6e 74 29 3b 20 2f 2a 20 52 6f 75 74 69 6e 65 20  nt); /* Routine 
d4630 66 6f 72 20 65 6e 2f 64 65 63 6f 64 69 6e 67 20  for en/decoding 
d4640 64 61 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  data */.  void *
d4650 70 43 6f 64 65 63 41 72 67 3b 20 20 20 20 20 20  pCodecArg;      
d4660 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61        /* First a
d4670 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 6f 64 65  rgument to xCode
d4680 63 28 29 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20  c() */.#endif.  
d4690 69 6e 74 20 6e 48 61 73 68 3b 20 20 20 20 20 20  int nHash;      
d46a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
d46b0 69 7a 65 20 6f 66 20 74 68 65 20 70 61 67 65 72  ize of the pager
d46c0 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20   hash table */. 
d46d0 20 50 67 48 64 72 20 2a 2a 61 48 61 73 68 3b 20   PgHdr **aHash; 
d46e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
d46f0 48 61 73 68 20 74 61 62 6c 65 20 74 6f 20 6d 61  Hash table to ma
d4700 70 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74 6f  p page number to
d4710 20 50 67 48 64 72 20 2a 2f 0a 23 69 66 64 65 66   PgHdr */.#ifdef
d4720 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
d4730 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54  EMORY_MANAGEMENT
d4740 0a 20 20 50 61 67 65 72 20 2a 70 4e 65 78 74 3b  .  Pager *pNext;
d4750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
d4760 2a 20 44 6f 75 62 6c 79 20 6c 69 6e 6b 65 64 20  * Doubly linked 
d4770 6c 69 73 74 20 6f 66 20 70 61 67 65 72 73 20 6f  list of pagers o
d4780 6e 20 77 68 69 63 68 20 2a 2f 0a 20 20 50 61 67  n which */.  Pag
d4790 65 72 20 2a 70 50 72 65 76 3b 20 20 20 20 20 20  er *pPrev;      
d47a0 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69           /* sqli
d47b0 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f  te3_release_memo
d47c0 72 79 28 29 20 77 69 6c 6c 20 77 6f 72 6b 20 2a  ry() will work *
d47d0 2f 0a 20 20 69 6e 74 20 69 49 6e 55 73 65 4d 4d  /.  int iInUseMM
d47e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
d47f0 2f 2a 20 4e 6f 6e 2d 7a 65 72 6f 20 69 66 20 75  /* Non-zero if u
d4800 6e 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 4d 4d  navailable to MM
d4810 20 2a 2f 0a 20 20 69 6e 74 20 69 49 6e 55 73 65   */.  int iInUse
d4820 44 42 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  DB;             
d4830 20 20 2f 2a 20 4e 6f 6e 2d 7a 65 72 6f 20 69 66    /* Non-zero if
d4840 20 69 6e 20 73 71 6c 69 74 65 33 5f 72 65 6c 65   in sqlite3_rele
d4850 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 2a 2f 0a  ase_memory() */.
d4860 23 65 6e 64 69 66 0a 20 20 63 68 61 72 20 2a 70  #endif.  char *p
d4870 54 6d 70 53 70 61 63 65 3b 20 20 20 20 20 20 20  TmpSpace;       
d4880 20 20 20 20 20 2f 2a 20 50 61 67 65 72 2e 70 61       /* Pager.pa
d4890 67 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20  geSize bytes of 
d48a0 73 70 61 63 65 20 66 6f 72 20 74 6d 70 20 75 73  space for tmp us
d48b0 65 20 2a 2f 0a 20 20 63 68 61 72 20 64 62 46 69  e */.  char dbFi
d48c0 6c 65 56 65 72 73 5b 31 36 5d 3b 20 20 20 20 20  leVers[16];     
d48d0 20 20 20 2f 2a 20 43 68 61 6e 67 65 73 20 77 68     /* Changes wh
d48e0 65 6e 65 76 65 72 20 64 61 74 61 62 61 73 65 20  enever database 
d48f0 66 69 6c 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a  file changes */.
d4900 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  };../*.** The fo
d4910 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76  llowing global v
d4920 61 72 69 61 62 6c 65 73 20 68 6f 6c 64 20 63 6f  ariables hold co
d4930 75 6e 74 65 72 73 20 75 73 65 64 20 66 6f 72 0a  unters used for.
d4940 2a 2a 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f  ** testing purpo
d4950 73 65 73 20 6f 6e 6c 79 2e 20 20 54 68 65 73 65  ses only.  These
d4960 20 76 61 72 69 61 62 6c 65 73 20 64 6f 20 6e 6f   variables do no
d4970 74 20 65 78 69 73 74 20 69 6e 0a 2a 2a 20 61 20  t exist in.** a 
d4980 6e 6f 6e 2d 74 65 73 74 69 6e 67 20 62 75 69 6c  non-testing buil
d4990 64 2e 20 20 54 68 65 73 65 20 76 61 72 69 61 62  d.  These variab
d49a0 6c 65 73 20 61 72 65 20 6e 6f 74 20 74 68 72 65  les are not thre
d49b0 61 64 2d 73 61 66 65 2e 0a 2a 2f 0a 23 69 66 64  ad-safe..*/.#ifd
d49c0 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
d49d0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
d49e0 6c 69 74 65 33 5f 70 61 67 65 72 5f 72 65 61 64  lite3_pager_read
d49f0 64 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20  db_count = 0;   
d4a00 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 75   /* Number of fu
d4a10 6c 6c 20 70 61 67 65 73 20 72 65 61 64 20 66 72  ll pages read fr
d4a20 6f 6d 20 44 42 20 2a 2f 0a 53 51 4c 49 54 45 5f  om DB */.SQLITE_
d4a30 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
d4a40 70 61 67 65 72 5f 77 72 69 74 65 64 62 5f 63 6f  pager_writedb_co
d4a50 75 6e 74 20 3d 20 30 3b 20 20 20 2f 2a 20 4e 75  unt = 0;   /* Nu
d4a60 6d 62 65 72 20 6f 66 20 66 75 6c 6c 20 70 61 67  mber of full pag
d4a70 65 73 20 77 72 69 74 74 65 6e 20 74 6f 20 44 42  es written to DB
d4a80 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   */.SQLITE_API i
d4a90 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72  nt sqlite3_pager
d4aa0 5f 77 72 69 74 65 6a 5f 63 6f 75 6e 74 20 3d 20  _writej_count = 
d4ab0 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  0;    /* Number 
d4ac0 6f 66 20 70 61 67 65 73 20 77 72 69 74 74 65 6e  of pages written
d4ad0 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 53   to journal */.S
d4ae0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
d4af0 6c 69 74 65 33 5f 70 61 67 65 72 5f 70 67 66 72  lite3_pager_pgfr
d4b00 65 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20  ee_count = 0;   
d4b10 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 61   /* Number of ca
d4b20 63 68 65 20 70 61 67 65 73 20 66 72 65 65 64 20  che pages freed 
d4b30 2a 2f 0a 23 20 64 65 66 69 6e 65 20 50 41 47 45  */.# define PAGE
d4b40 52 5f 49 4e 43 52 28 76 29 20 20 76 2b 2b 0a 23  R_INCR(v)  v++.#
d4b50 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 50 41  else.# define PA
d4b60 47 45 52 5f 49 4e 43 52 28 76 29 0a 23 65 6e 64  GER_INCR(v).#end
d4b70 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  if../*.** The fo
d4b80 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65  llowing variable
d4b90 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 68   points to the h
d4ba0 65 61 64 20 6f 66 20 61 20 64 6f 75 62 6c 65 2d  ead of a double-
d4bb0 6c 69 6e 6b 65 64 20 6c 69 73 74 0a 2a 2a 20 6f  linked list.** o
d4bc0 66 20 61 6c 6c 20 70 61 67 65 72 73 20 74 68 61  f all pagers tha
d4bd0 74 20 61 72 65 20 65 6c 69 67 69 62 6c 65 20 66  t are eligible f
d4be0 6f 72 20 70 61 67 65 20 73 74 65 61 6c 69 6e 67  or page stealing
d4bf0 20 62 79 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74   by the.** sqlit
d4c00 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72  e3_release_memor
d4c10 79 28 29 20 69 6e 74 65 72 66 61 63 65 2e 20 20  y() interface.  
d4c20 41 63 63 65 73 73 20 74 6f 20 74 68 69 73 20 6c  Access to this l
d4c30 69 73 74 20 69 73 0a 2a 2a 20 70 72 6f 74 65 63  ist is.** protec
d4c40 74 65 64 20 62 79 20 74 68 65 20 53 51 4c 49 54  ted by the SQLIT
d4c50 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
d4c60 45 4d 32 20 6d 75 74 65 78 2e 0a 2a 2f 0a 23 69  EM2 mutex..*/.#i
d4c70 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
d4c80 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45  LE_MEMORY_MANAGE
d4c90 4d 45 4e 54 0a 73 74 61 74 69 63 20 50 61 67 65  MENT.static Page
d4ca0 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4c  r *sqlite3PagerL
d4cb0 69 73 74 20 3d 20 30 3b 0a 73 74 61 74 69 63 20  ist = 0;.static 
d4cc0 50 61 67 65 72 4c 72 75 4c 69 73 74 20 73 71 6c  PagerLruList sql
d4cd0 69 74 65 33 4c 72 75 50 61 67 65 4c 69 73 74 20  ite3LruPageList 
d4ce0 3d 20 7b 30 2c 20 30 2c 20 30 7d 3b 0a 23 65 6e  = {0, 0, 0};.#en
d4cf0 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 4a 6f 75 72  dif.../*.** Jour
d4d00 6e 61 6c 20 66 69 6c 65 73 20 62 65 67 69 6e 20  nal files begin 
d4d10 77 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69  with the followi
d4d20 6e 67 20 6d 61 67 69 63 20 73 74 72 69 6e 67 2e  ng magic string.
d4d30 20 20 54 68 65 20 64 61 74 61 0a 2a 2a 20 77 61    The data.** wa
d4d40 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  s obtained from 
d4d50 2f 64 65 76 2f 72 61 6e 64 6f 6d 2e 20 20 49 74  /dev/random.  It
d4d60 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 61 73   is used only as
d4d70 20 61 20 73 61 6e 69 74 79 20 63 68 65 63 6b 2e   a sanity check.
d4d80 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 76 65 72  .**.** Since ver
d4d90 73 69 6f 6e 20 32 2e 38 2e 30 2c 20 74 68 65 20  sion 2.8.0, the 
d4da0 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 63  journal format c
d4db0 6f 6e 74 61 69 6e 73 20 61 64 64 69 74 69 6f 6e  ontains addition
d4dc0 61 6c 20 73 61 6e 69 74 79 0a 2a 2a 20 63 68 65  al sanity.** che
d4dd0 63 6b 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f  cking informatio
d4de0 6e 2e 20 20 49 66 20 74 68 65 20 70 6f 77 65 72  n.  If the power
d4df0 20 66 61 69 6c 73 20 77 68 69 6c 65 20 74 68 65   fails while the
d4e00 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65 67 69   journal is begi
d4e10 6e 0a 2a 2a 20 77 72 69 74 74 65 6e 2c 20 73 65  n.** written, se
d4e20 6d 69 2d 72 61 6e 64 6f 6d 20 67 61 72 62 61 67  mi-random garbag
d4e30 65 20 64 61 74 61 20 6d 69 67 68 74 20 61 70 70  e data might app
d4e40 65 61 72 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  ear in the journ
d4e50 61 6c 0a 2a 2a 20 66 69 6c 65 20 61 66 74 65 72  al.** file after
d4e60 20 70 6f 77 65 72 20 69 73 20 72 65 73 74 6f 72   power is restor
d4e70 65 64 2e 20 20 49 66 20 61 6e 20 61 74 74 65 6d  ed.  If an attem
d4e80 70 74 20 69 73 20 74 68 65 6e 20 6d 61 64 65 0a  pt is then made.
d4e90 2a 2a 20 74 6f 20 72 6f 6c 6c 20 74 68 65 20 6a  ** to roll the j
d4ea0 6f 75 72 6e 61 6c 20 62 61 63 6b 2c 20 74 68 65  ournal back, the
d4eb0 20 64 61 74 61 62 61 73 65 20 63 6f 75 6c 64 20   database could 
d4ec0 62 65 20 63 6f 72 72 75 70 74 65 64 2e 20 20 54  be corrupted.  T
d4ed0 68 65 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a  he additional.**
d4ee0 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67   sanity checking
d4ef0 20 64 61 74 61 20 69 73 20 61 6e 20 61 74 74 65   data is an atte
d4f00 6d 70 74 20 74 6f 20 64 69 73 63 6f 76 65 72 20  mpt to discover 
d4f10 74 68 65 20 67 61 72 62 61 67 65 20 69 6e 20 74  the garbage in t
d4f20 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 61 6e  he.** journal an
d4f30 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a 2a 2a 0a  d ignore it..**.
d4f40 2a 2a 20 54 68 65 20 73 61 6e 69 74 79 20 63 68  ** The sanity ch
d4f50 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69  ecking informati
d4f60 6f 6e 20 66 6f 72 20 74 68 65 20 6e 65 77 20 6a  on for the new j
d4f70 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 63 6f  ournal format co
d4f80 6e 73 69 73 74 73 0a 2a 2a 20 6f 66 20 61 20 33  nsists.** of a 3
d4f90 32 2d 62 69 74 20 63 68 65 63 6b 73 75 6d 20 6f  2-bit checksum o
d4fa0 6e 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 64  n each page of d
d4fb0 61 74 61 2e 20 20 54 68 65 20 63 68 65 63 6b 73  ata.  The checks
d4fc0 75 6d 20 63 6f 76 65 72 73 20 62 6f 74 68 0a 2a  um covers both.*
d4fd0 2a 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65  * the page numbe
d4fe0 72 20 61 6e 64 20 74 68 65 20 70 50 61 67 65 72  r and the pPager
d4ff0 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73  ->pageSize bytes
d5000 20 6f 66 20 64 61 74 61 20 66 6f 72 20 74 68 65   of data for the
d5010 20 70 61 67 65 2e 0a 2a 2a 20 54 68 69 73 20 63   page..** This c
d5020 6b 73 75 6d 20 69 73 20 69 6e 69 74 69 61 6c 69  ksum is initiali
d5030 7a 65 64 20 74 6f 20 61 20 33 32 2d 62 69 74 20  zed to a 32-bit 
d5040 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 74 68 61  random value tha
d5050 74 20 61 70 70 65 61 72 73 20 69 6e 20 74 68 65  t appears in the
d5060 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  .** journal file
d5070 20 72 69 67 68 74 20 61 66 74 65 72 20 74 68 65   right after the
d5080 20 68 65 61 64 65 72 2e 20 20 54 68 65 20 72 61   header.  The ra
d5090 6e 64 6f 6d 20 69 6e 69 74 69 61 6c 69 7a 65 72  ndom initializer
d50a0 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2c 0a 2a   is important,.*
d50b0 2a 20 62 65 63 61 75 73 65 20 67 61 72 62 61 67  * because garbag
d50c0 65 20 64 61 74 61 20 74 68 61 74 20 61 70 70 65  e data that appe
d50d0 61 72 73 20 61 74 20 74 68 65 20 65 6e 64 20 6f  ars at the end o
d50e0 66 20 61 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6c  f a journal is l
d50f0 69 6b 65 6c 79 0a 2a 2a 20 64 61 74 61 20 74 68  ikely.** data th
d5100 61 74 20 77 61 73 20 6f 6e 63 65 20 69 6e 20 6f  at was once in o
d5110 74 68 65 72 20 66 69 6c 65 73 20 74 68 61 74 20  ther files that 
d5120 68 61 76 65 20 6e 6f 77 20 62 65 65 6e 20 64 65  have now been de
d5130 6c 65 74 65 64 2e 20 20 49 66 20 74 68 65 0a 2a  leted.  If the.*
d5140 2a 20 67 61 72 62 61 67 65 20 64 61 74 61 20 63  * garbage data c
d5150 61 6d 65 20 66 72 6f 6d 20 61 6e 20 6f 62 73 6f  ame from an obso
d5160 6c 65 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  lete journal fil
d5170 65 2c 20 74 68 65 20 63 68 65 63 6b 73 75 6d 73  e, the checksums
d5180 20 6d 69 67 68 74 0a 2a 2a 20 62 65 20 63 6f 72   might.** be cor
d5190 72 65 63 74 2e 20 20 42 75 74 20 62 79 20 69 6e  rect.  But by in
d51a0 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 63  itializing the c
d51b0 68 65 63 6b 73 75 6d 20 74 6f 20 72 61 6e 64 6f  hecksum to rando
d51c0 6d 20 76 61 6c 75 65 20 77 68 69 63 68 0a 2a 2a  m value which.**
d51d0 20 69 73 20 64 69 66 66 65 72 65 6e 74 20 66 6f   is different fo
d51e0 72 20 65 76 65 72 79 20 6a 6f 75 72 6e 61 6c 2c  r every journal,
d51f0 20 77 65 20 6d 69 6e 69 6d 69 7a 65 20 74 68 61   we minimize tha
d5200 74 20 72 69 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69  t risk..*/.stati
d5210 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  c const unsigned
d5220 20 63 68 61 72 20 61 4a 6f 75 72 6e 61 6c 4d 61   char aJournalMa
d5230 67 69 63 5b 5d 20 3d 20 7b 0a 20 20 30 78 64 39  gic[] = {.  0xd9
d5240 2c 20 30 78 64 35 2c 20 30 78 30 35 2c 20 30 78  , 0xd5, 0x05, 0x
d5250 66 39 2c 20 30 78 32 30 2c 20 30 78 61 31 2c 20  f9, 0x20, 0xa1, 
d5260 30 78 36 33 2c 20 30 78 64 37 2c 0a 7d 3b 0a 0a  0x63, 0xd7,.};..
d5270 2f 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f  /*.** The size o
d5280 66 20 74 68 65 20 68 65 61 64 65 72 20 61 6e 64  f the header and
d5290 20 6f 66 20 65 61 63 68 20 70 61 67 65 20 69 6e   of each page in
d52a0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20   the journal is 
d52b0 64 65 74 65 72 6d 69 6e 65 64 0a 2a 2a 20 62 79  determined.** by
d52c0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   the following m
d52d0 61 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  acros..*/.#defin
d52e0 65 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28  e JOURNAL_PG_SZ(
d52f0 70 50 61 67 65 72 29 20 20 28 28 70 50 61 67 65  pPager)  ((pPage
d5300 72 2d 3e 70 61 67 65 53 69 7a 65 29 20 2b 20 38  r->pageSize) + 8
d5310 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75  )../*.** The jou
d5320 72 6e 61 6c 20 68 65 61 64 65 72 20 73 69 7a 65  rnal header size
d5330 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e   for this pager.
d5340 20 49 6e 20 74 68 65 20 66 75 74 75 72 65 2c 20   In the future, 
d5350 74 68 69 73 20 63 6f 75 6c 64 20 62 65 0a 2a 2a  this could be.**
d5360 20 73 65 74 20 74 6f 20 73 6f 6d 65 20 76 61 6c   set to some val
d5370 75 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65  ue read from the
d5380 20 64 69 73 6b 20 63 6f 6e 74 72 6f 6c 6c 65 72   disk controller
d5390 2e 20 54 68 65 20 69 6d 70 6f 72 74 61 6e 74 0a  . The important.
d53a0 2a 2a 20 63 68 61 72 61 63 74 65 72 69 73 74 69  ** characteristi
d53b0 63 20 69 73 20 74 68 61 74 20 69 74 20 69 73 20  c is that it is 
d53c0 74 68 65 20 73 61 6d 65 20 73 69 7a 65 20 61 73  the same size as
d53d0 20 61 20 64 69 73 6b 20 73 65 63 74 6f 72 2e 0a   a disk sector..
d53e0 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e  */.#define JOURN
d53f0 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
d5400 29 20 28 70 50 61 67 65 72 2d 3e 73 65 63 74 6f  ) (pPager->secto
d5410 72 53 69 7a 65 29 0a 0a 2f 2a 0a 2a 2a 20 54 68  rSize)../*.** Th
d5420 65 20 6d 61 63 72 6f 20 4d 45 4d 44 42 20 69 73  e macro MEMDB is
d5430 20 74 72 75 65 20 69 66 20 77 65 20 61 72 65 20   true if we are 
d5440 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 6e 20  dealing with an 
d5450 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
d5460 73 65 2e 0a 2a 2a 20 57 65 20 64 6f 20 74 68 69  se..** We do thi
d5470 73 20 61 73 20 61 20 6d 61 63 72 6f 20 73 6f 20  s as a macro so 
d5480 74 68 61 74 20 69 66 20 74 68 65 20 53 51 4c 49  that if the SQLI
d5490 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42  TE_OMIT_MEMORYDB
d54a0 20 6d 61 63 72 6f 20 69 73 20 73 65 74 2c 0a 2a   macro is set,.*
d54b0 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 4d  * the value of M
d54c0 45 4d 44 42 20 77 69 6c 6c 20 62 65 20 61 20 63  EMDB will be a c
d54d0 6f 6e 73 74 61 6e 74 20 61 6e 64 20 74 68 65 20  onstant and the 
d54e0 63 6f 6d 70 69 6c 65 72 20 77 69 6c 6c 20 6f 70  compiler will op
d54f0 74 69 6d 69 7a 65 0a 2a 2a 20 6f 75 74 20 63 6f  timize.** out co
d5500 64 65 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 65  de that would ne
d5510 76 65 72 20 65 78 65 63 75 74 65 2e 0a 2a 2f 0a  ver execute..*/.
d5520 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
d5530 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 23 20 64 65  IT_MEMORYDB.# de
d5540 66 69 6e 65 20 4d 45 4d 44 42 20 30 0a 23 65 6c  fine MEMDB 0.#el
d5550 73 65 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d 44  se.# define MEMD
d5560 42 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 0a  B pPager->memDb.
d5570 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 61  #endif../*.** Pa
d5580 67 65 20 6e 75 6d 62 65 72 20 50 41 47 45 52 5f  ge number PAGER_
d5590 4d 4a 5f 50 47 4e 4f 20 69 73 20 6e 65 76 65 72  MJ_PGNO is never
d55a0 20 75 73 65 64 20 69 6e 20 61 6e 20 53 51 4c 69   used in an SQLi
d55b0 74 65 20 64 61 74 61 62 61 73 65 20 28 69 74 20  te database (it 
d55c0 69 73 0a 2a 2a 20 72 65 73 65 72 76 65 64 20 66  is.** reserved f
d55d0 6f 72 20 77 6f 72 6b 69 6e 67 20 61 72 6f 75 6e  or working aroun
d55e0 64 20 61 20 77 69 6e 64 6f 77 73 2f 70 6f 73 69  d a windows/posi
d55f0 78 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c 69 74  x incompatibilit
d5600 79 29 2e 20 49 74 20 69 73 0a 2a 2a 20 75 73 65  y). It is.** use
d5610 64 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  d in the journal
d5620 20 74 6f 20 73 69 67 6e 69 66 79 20 74 68 61 74   to signify that
d5630 20 74 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f   the remainder o
d5640 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
d5650 6c 65 20 0a 2a 2a 20 69 73 20 64 65 76 6f 74 65  le .** is devote
d5660 64 20 74 6f 20 73 74 6f 72 69 6e 67 20 61 20 6d  d to storing a m
d5670 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61  aster journal na
d5680 6d 65 20 2d 20 74 68 65 72 65 20 61 72 65 20 6e  me - there are n
d5690 6f 20 6d 6f 72 65 20 70 61 67 65 73 20 74 6f 0a  o more pages to.
d56a0 2a 2a 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 53 65  ** roll back. Se
d56b0 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 66  e comments for f
d56c0 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 4d 61 73  unction writeMas
d56d0 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 66 6f 72  terJournal() for
d56e0 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 2f 2a 20   details..*/./* 
d56f0 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4d 4a  #define PAGER_MJ
d5700 5f 50 47 4e 4f 28 78 29 20 28 50 45 4e 44 49 4e  _PGNO(x) (PENDIN
d5710 47 5f 42 59 54 45 2f 28 28 78 29 2d 3e 70 61 67  G_BYTE/((x)->pag
d5720 65 53 69 7a 65 29 29 20 2a 2f 0a 23 64 65 66 69  eSize)) */.#defi
d5730 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  ne PAGER_MJ_PGNO
d5740 28 78 29 20 28 28 50 45 4e 44 49 4e 47 5f 42 59  (x) ((PENDING_BY
d5750 54 45 2f 28 28 78 29 2d 3e 70 61 67 65 53 69 7a  TE/((x)->pageSiz
d5760 65 29 29 2b 31 29 0a 0a 2f 2a 0a 2a 2a 20 54 68  e))+1)../*.** Th
d5770 65 20 6d 61 78 69 6d 75 6d 20 6c 65 67 61 6c 20  e maximum legal 
d5780 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 28  page number is (
d5790 32 5e 33 31 20 2d 20 31 29 2e 0a 2a 2f 0a 23 64  2^31 - 1)..*/.#d
d57a0 65 66 69 6e 65 20 50 41 47 45 52 5f 4d 41 58 5f  efine PAGER_MAX_
d57b0 50 47 4e 4f 20 32 31 34 37 34 38 33 36 34 37 0a  PGNO 2147483647.
d57c0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72  ./*.** The pager
d57d0 45 6e 74 65 72 28 29 20 61 6e 64 20 70 61 67 65  Enter() and page
d57e0 72 4c 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65  rLeave() routine
d57f0 73 20 61 63 71 75 69 72 65 20 61 6e 64 20 72 65  s acquire and re
d5800 6c 65 61 73 65 0a 2a 2a 20 61 20 6d 75 74 65 78  lease.** a mutex
d5810 20 6f 6e 20 65 61 63 68 20 70 61 67 65 72 2e 20   on each pager. 
d5820 20 54 68 65 20 6d 75 74 65 78 20 69 73 20 72 65   The mutex is re
d5830 63 75 72 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54  cursive..**.** T
d5840 68 69 73 20 69 73 20 61 20 73 70 65 63 69 61 6c  his is a special
d5850 2d 70 75 72 70 6f 73 65 20 6d 75 74 65 78 2e 20  -purpose mutex. 
d5860 20 49 74 20 6f 6e 6c 79 20 70 72 6f 76 69 64 65   It only provide
d5870 73 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 69  s mutual exclusi
d5880 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 74 68  on.** between th
d5890 65 20 42 74 72 65 65 20 61 6e 64 20 74 68 65 20  e Btree and the 
d58a0 4d 65 6d 6f 72 79 20 4d 61 6e 61 67 65 6d 65 6e  Memory Managemen
d58b0 74 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73  t sqlite3_releas
d58c0 65 5f 6d 65 6d 6f 72 79 28 29 0a 2a 2a 20 66 75  e_memory().** fu
d58d0 6e 63 74 69 6f 6e 2e 20 20 49 74 20 64 6f 65 73  nction.  It does
d58e0 20 6e 6f 74 20 70 72 65 76 65 6e 74 2c 20 66 6f   not prevent, fo
d58f0 72 20 65 78 61 6d 70 6c 65 2c 20 74 77 6f 20 42  r example, two B
d5900 74 72 65 65 73 20 66 72 6f 6d 20 61 63 63 65 73  trees from acces
d5910 73 69 6e 67 0a 2a 2a 20 74 68 65 20 73 61 6d 65  sing.** the same
d5920 20 70 61 67 65 72 20 61 74 20 74 68 65 20 73 61   pager at the sa
d5930 6d 65 20 74 69 6d 65 2e 20 20 4f 74 68 65 72 20  me time.  Other 
d5940 67 65 6e 65 72 61 6c 2d 70 75 72 70 6f 73 65 20  general-purpose 
d5950 6d 75 74 65 78 65 73 20 69 6e 0a 2a 2a 20 74 68  mutexes in.** th
d5960 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 68 61  e btree layer ha
d5970 6e 64 6c 65 20 74 68 61 74 20 63 68 6f 72 65 2e  ndle that chore.
d5980 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
d5990 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f  E_ENABLE_MEMORY_
d59a0 4d 41 4e 41 47 45 4d 45 4e 54 0a 20 20 73 74 61  MANAGEMENT.  sta
d59b0 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 45 6e  tic void pagerEn
d59c0 74 65 72 28 50 61 67 65 72 20 2a 70 29 7b 0a 20  ter(Pager *p){. 
d59d0 20 20 20 70 2d 3e 69 49 6e 55 73 65 44 42 2b 2b     p->iInUseDB++
d59e0 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 49 6e  ;.    if( p->iIn
d59f0 55 73 65 4d 4d 20 26 26 20 70 2d 3e 69 49 6e 55  UseMM && p->iInU
d5a00 73 65 44 42 3d 3d 31 20 29 7b 0a 23 69 66 6e 64  seDB==1 ){.#ifnd
d5a10 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ef SQLITE_MUTEX_
d5a20 4e 4f 4f 50 0a 20 20 20 20 20 20 73 71 6c 69 74  NOOP.      sqlit
d5a30 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b  e3_mutex *mutex;
d5a40 0a 20 20 20 20 20 20 6d 75 74 65 78 20 3d 20 73  .      mutex = s
d5a50 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
d5a60 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
d5a70 53 54 41 54 49 43 5f 4d 45 4d 32 29 3b 0a 23 65  STATIC_MEM2);.#e
d5a80 6e 64 69 66 0a 20 20 20 20 20 20 70 2d 3e 69 49  ndif.      p->iI
d5a90 6e 55 73 65 44 42 20 3d 20 30 3b 0a 20 20 20 20  nUseDB = 0;.    
d5aa0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
d5ab0 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20  enter(mutex);.  
d5ac0 20 20 20 20 70 2d 3e 69 49 6e 55 73 65 44 42 20      p->iInUseDB 
d5ad0 3d 20 31 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  = 1;.      sqlit
d5ae0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
d5af0 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20  utex);.    }.   
d5b00 20 61 73 73 65 72 74 28 20 70 2d 3e 69 49 6e 55   assert( p->iInU
d5b10 73 65 4d 4d 3d 3d 30 20 29 3b 0a 20 20 7d 0a 20  seMM==0 );.  }. 
d5b20 20 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67   static void pag
d5b30 65 72 4c 65 61 76 65 28 50 61 67 65 72 20 2a 70  erLeave(Pager *p
d5b40 29 7b 0a 20 20 20 20 70 2d 3e 69 49 6e 55 73 65  ){.    p->iInUse
d5b50 44 42 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74  DB--;.    assert
d5b60 28 20 70 2d 3e 69 49 6e 55 73 65 44 42 3e 3d 30  ( p->iInUseDB>=0
d5b70 20 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 23 20   );.  }.#else.# 
d5b80 64 65 66 69 6e 65 20 70 61 67 65 72 45 6e 74 65  define pagerEnte
d5b90 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 70 61  r(X).# define pa
d5ba0 67 65 72 4c 65 61 76 65 28 58 29 0a 23 65 6e 64  gerLeave(X).#end
d5bb0 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 70 61  if../*.** Add pa
d5bc0 67 65 20 70 50 67 20 74 6f 20 74 68 65 20 65 6e  ge pPg to the en
d5bd0 64 20 6f 66 20 74 68 65 20 6c 69 6e 6b 65 64 20  d of the linked 
d5be0 6c 69 73 74 20 6d 61 6e 61 67 65 64 20 62 79 20  list managed by 
d5bf0 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 70 4c 69  structure.** pLi
d5c00 73 74 20 28 70 50 67 20 62 65 63 6f 6d 65 73 20  st (pPg becomes 
d5c10 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69  the last entry i
d5c20 6e 20 74 68 65 20 6c 69 73 74 20 2d 20 74 68 65  n the list - the
d5c30 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 0a   most recently .
d5c40 2a 2a 20 75 73 65 64 29 2e 20 41 72 67 75 6d 65  ** used). Argume
d5c50 6e 74 20 70 4c 69 6e 6b 20 73 68 6f 75 6c 64 20  nt pLink should 
d5c60 70 6f 69 6e 74 20 74 6f 20 65 69 74 68 65 72 20  point to either 
d5c70 70 50 67 2d 3e 66 72 65 65 20 6f 72 20 70 50 67  pPg->free or pPg
d5c80 2d 3e 67 66 72 65 65 2c 0a 2a 2a 20 64 65 70 65  ->gfree,.** depe
d5c90 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72  nding on whether
d5ca0 20 70 50 67 20 69 73 20 62 65 69 6e 67 20 61 64   pPg is being ad
d5cb0 64 65 64 20 74 6f 20 74 68 65 20 70 61 67 65 72  ded to the pager
d5cc0 2d 73 70 65 63 69 66 69 63 20 6f 72 0a 2a 2a 20  -specific or.** 
d5cd0 67 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74 2e  global LRU list.
d5ce0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
d5cf0 6c 69 73 74 41 64 64 28 50 61 67 65 72 4c 72 75  listAdd(PagerLru
d5d00 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 50 61 67  List *pList, Pag
d5d10 65 72 4c 72 75 4c 69 6e 6b 20 2a 70 4c 69 6e 6b  erLruLink *pLink
d5d20 2c 20 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20  , PgHdr *pPg){. 
d5d30 20 70 4c 69 6e 6b 2d 3e 70 4e 65 78 74 20 3d 20   pLink->pNext = 
d5d40 30 3b 0a 20 20 70 4c 69 6e 6b 2d 3e 70 50 72 65  0;.  pLink->pPre
d5d50 76 20 3d 20 70 4c 69 73 74 2d 3e 70 4c 61 73 74  v = pList->pLast
d5d60 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
d5d70 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d  _ENABLE_MEMORY_M
d5d80 41 4e 41 47 45 4d 45 4e 54 0a 20 20 61 73 73 65  ANAGEMENT.  asse
d5d90 72 74 28 70 4c 69 6e 6b 3d 3d 26 70 50 67 2d 3e  rt(pLink==&pPg->
d5da0 66 72 65 65 20 7c 7c 20 70 4c 69 6e 6b 3d 3d 26  free || pLink==&
d5db0 70 50 67 2d 3e 67 66 72 65 65 29 3b 0a 20 20 61  pPg->gfree);.  a
d5dc0 73 73 65 72 74 28 70 4c 69 6e 6b 3d 3d 26 70 50  ssert(pLink==&pP
d5dd0 67 2d 3e 67 66 72 65 65 20 7c 7c 20 70 4c 69 73  g->gfree || pLis
d5de0 74 21 3d 26 73 71 6c 69 74 65 33 4c 72 75 50 61  t!=&sqlite3LruPa
d5df0 67 65 4c 69 73 74 29 3b 0a 23 65 6e 64 69 66 0a  geList);.#endif.
d5e00 0a 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 70 4c  .  if( pList->pL
d5e10 61 73 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  ast ){.    int i
d5e20 4f 66 66 20 3d 20 28 63 68 61 72 20 2a 29 70 4c  Off = (char *)pL
d5e30 69 6e 6b 20 2d 20 28 63 68 61 72 20 2a 29 70 50  ink - (char *)pP
d5e40 67 3b 0a 20 20 20 20 50 61 67 65 72 4c 72 75 4c  g;.    PagerLruL
d5e50 69 6e 6b 20 2a 70 4c 61 73 74 4c 69 6e 6b 20 3d  ink *pLastLink =
d5e60 20 28 50 61 67 65 72 4c 72 75 4c 69 6e 6b 20 2a   (PagerLruLink *
d5e70 29 28 26 28 28 75 38 20 2a 29 70 4c 69 73 74 2d  )(&((u8 *)pList-
d5e80 3e 70 4c 61 73 74 29 5b 69 4f 66 66 5d 29 3b 0a  >pLast)[iOff]);.
d5e90 20 20 20 20 70 4c 61 73 74 4c 69 6e 6b 2d 3e 70      pLastLink->p
d5ea0 4e 65 78 74 20 3d 20 70 50 67 3b 0a 20 20 7d 65  Next = pPg;.  }e
d5eb0 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
d5ec0 21 70 4c 69 73 74 2d 3e 70 46 69 72 73 74 29 3b  !pList->pFirst);
d5ed0 0a 20 20 20 20 70 4c 69 73 74 2d 3e 70 46 69 72  .    pList->pFir
d5ee0 73 74 20 3d 20 70 50 67 3b 0a 20 20 7d 0a 0a 20  st = pPg;.  }.. 
d5ef0 20 70 4c 69 73 74 2d 3e 70 4c 61 73 74 20 3d 20   pList->pLast = 
d5f00 70 50 67 3b 0a 20 20 69 66 28 20 21 70 4c 69 73  pPg;.  if( !pLis
d5f10 74 2d 3e 70 46 69 72 73 74 53 79 6e 63 65 64 20  t->pFirstSynced 
d5f20 26 26 20 70 50 67 2d 3e 6e 65 65 64 53 79 6e 63  && pPg->needSync
d5f30 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69 73 74  ==0 ){.    pList
d5f40 2d 3e 70 46 69 72 73 74 53 79 6e 63 65 64 20 3d  ->pFirstSynced =
d5f50 20 70 50 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   pPg;.  }.}../*.
d5f60 2a 2a 20 52 65 6d 6f 76 65 20 70 50 67 20 66 72  ** Remove pPg fr
d5f70 6f 6d 20 74 68 65 20 6c 69 73 74 20 6d 61 6e 61  om the list mana
d5f80 67 65 64 20 62 79 20 74 68 65 20 73 74 72 75 63  ged by the struc
d5f90 74 75 72 65 20 70 6f 69 6e 74 65 64 20 74 6f 20  ture pointed to 
d5fa0 62 79 20 70 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  by pList..**.** 
d5fb0 41 72 67 75 6d 65 6e 74 20 70 4c 69 6e 6b 20 73  Argument pLink s
d5fc0 68 6f 75 6c 64 20 70 6f 69 6e 74 20 74 6f 20 65  hould point to e
d5fd0 69 74 68 65 72 20 70 50 67 2d 3e 66 72 65 65 20  ither pPg->free 
d5fe0 6f 72 20 70 50 67 2d 3e 67 66 72 65 65 2c 20 64  or pPg->gfree, d
d5ff0 65 70 65 6e 64 69 6e 67 20 0a 2a 2a 20 6f 6e 20  epending .** on 
d6000 77 68 65 74 68 65 72 20 70 50 67 20 69 73 20 62  whether pPg is b
d6010 65 69 6e 67 20 61 64 64 65 64 20 74 6f 20 74 68  eing added to th
d6020 65 20 70 61 67 65 72 2d 73 70 65 63 69 66 69 63  e pager-specific
d6030 20 6f 72 20 67 6c 6f 62 61 6c 20 4c 52 55 20 6c   or global LRU l
d6040 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ist..*/.static v
d6050 6f 69 64 20 6c 69 73 74 52 65 6d 6f 76 65 28 50  oid listRemove(P
d6060 61 67 65 72 4c 72 75 4c 69 73 74 20 2a 70 4c 69  agerLruList *pLi
d6070 73 74 2c 20 50 61 67 65 72 4c 72 75 4c 69 6e 6b  st, PagerLruLink
d6080 20 2a 70 4c 69 6e 6b 2c 20 50 67 48 64 72 20 2a   *pLink, PgHdr *
d6090 70 50 67 29 7b 0a 20 20 69 6e 74 20 69 4f 66 66  pPg){.  int iOff
d60a0 20 3d 20 28 63 68 61 72 20 2a 29 70 4c 69 6e 6b   = (char *)pLink
d60b0 20 2d 20 28 63 68 61 72 20 2a 29 70 50 67 3b 0a   - (char *)pPg;.
d60c0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
d60d0 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e  NABLE_MEMORY_MAN
d60e0 41 47 45 4d 45 4e 54 0a 20 20 61 73 73 65 72 74  AGEMENT.  assert
d60f0 28 70 4c 69 6e 6b 3d 3d 26 70 50 67 2d 3e 66 72  (pLink==&pPg->fr
d6100 65 65 20 7c 7c 20 70 4c 69 6e 6b 3d 3d 26 70 50  ee || pLink==&pP
d6110 67 2d 3e 67 66 72 65 65 29 3b 0a 20 20 61 73 73  g->gfree);.  ass
d6120 65 72 74 28 70 4c 69 6e 6b 3d 3d 26 70 50 67 2d  ert(pLink==&pPg-
d6130 3e 67 66 72 65 65 20 7c 7c 20 70 4c 69 73 74 21  >gfree || pList!
d6140 3d 26 73 71 6c 69 74 65 33 4c 72 75 50 61 67 65  =&sqlite3LruPage
d6150 4c 69 73 74 29 3b 0a 23 65 6e 64 69 66 0a 0a 20  List);.#endif.. 
d6160 20 69 66 28 20 70 50 67 3d 3d 70 4c 69 73 74 2d   if( pPg==pList-
d6170 3e 70 46 69 72 73 74 20 29 7b 0a 20 20 20 20 70  >pFirst ){.    p
d6180 4c 69 73 74 2d 3e 70 46 69 72 73 74 20 3d 20 70  List->pFirst = p
d6190 4c 69 6e 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d  Link->pNext;.  }
d61a0 0a 20 20 69 66 28 20 70 50 67 3d 3d 70 4c 69 73  .  if( pPg==pLis
d61b0 74 2d 3e 70 4c 61 73 74 20 29 7b 0a 20 20 20 20  t->pLast ){.    
d61c0 70 4c 69 73 74 2d 3e 70 4c 61 73 74 20 3d 20 70  pList->pLast = p
d61d0 4c 69 6e 6b 2d 3e 70 50 72 65 76 3b 0a 20 20 7d  Link->pPrev;.  }
d61e0 0a 20 20 69 66 28 20 70 4c 69 6e 6b 2d 3e 70 50  .  if( pLink->pP
d61f0 72 65 76 20 29 7b 0a 20 20 20 20 50 61 67 65 72  rev ){.    Pager
d6200 4c 72 75 4c 69 6e 6b 20 2a 70 50 72 65 76 4c 69  LruLink *pPrevLi
d6210 6e 6b 20 3d 20 28 50 61 67 65 72 4c 72 75 4c 69  nk = (PagerLruLi
d6220 6e 6b 20 2a 29 28 26 28 28 75 38 20 2a 29 70 4c  nk *)(&((u8 *)pL
d6230 69 6e 6b 2d 3e 70 50 72 65 76 29 5b 69 4f 66 66  ink->pPrev)[iOff
d6240 5d 29 3b 0a 20 20 20 20 70 50 72 65 76 4c 69 6e  ]);.    pPrevLin
d6250 6b 2d 3e 70 4e 65 78 74 20 3d 20 70 4c 69 6e 6b  k->pNext = pLink
d6260 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69  ->pNext;.  }.  i
d6270 66 28 20 70 4c 69 6e 6b 2d 3e 70 4e 65 78 74 20  f( pLink->pNext 
d6280 29 7b 0a 20 20 20 20 50 61 67 65 72 4c 72 75 4c  ){.    PagerLruL
d6290 69 6e 6b 20 2a 70 4e 65 78 74 4c 69 6e 6b 20 3d  ink *pNextLink =
d62a0 20 28 50 61 67 65 72 4c 72 75 4c 69 6e 6b 20 2a   (PagerLruLink *
d62b0 29 28 26 28 28 75 38 20 2a 29 70 4c 69 6e 6b 2d  )(&((u8 *)pLink-
d62c0 3e 70 4e 65 78 74 29 5b 69 4f 66 66 5d 29 3b 0a  >pNext)[iOff]);.
d62d0 20 20 20 20 70 4e 65 78 74 4c 69 6e 6b 2d 3e 70      pNextLink->p
d62e0 50 72 65 76 20 3d 20 70 4c 69 6e 6b 2d 3e 70 50  Prev = pLink->pP
d62f0 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  rev;.  }.  if( p
d6300 50 67 3d 3d 70 4c 69 73 74 2d 3e 70 46 69 72 73  Pg==pList->pFirs
d6310 74 53 79 6e 63 65 64 20 29 7b 0a 20 20 20 20 50  tSynced ){.    P
d6320 67 48 64 72 20 2a 70 20 3d 20 70 4c 69 6e 6b 2d  gHdr *p = pLink-
d6330 3e 70 4e 65 78 74 3b 0a 20 20 20 20 77 68 69 6c  >pNext;.    whil
d6340 65 28 20 70 20 26 26 20 70 2d 3e 6e 65 65 64 53  e( p && p->needS
d6350 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 50 61 67  ync ){.      Pag
d6360 65 72 4c 72 75 4c 69 6e 6b 20 2a 70 4c 20 3d 20  erLruLink *pL = 
d6370 28 50 61 67 65 72 4c 72 75 4c 69 6e 6b 20 2a 29  (PagerLruLink *)
d6380 28 26 28 28 75 38 20 2a 29 70 29 5b 69 4f 66 66  (&((u8 *)p)[iOff
d6390 5d 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 70 4c  ]);.      p = pL
d63a0 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20  ->pNext;.    }. 
d63b0 20 20 20 70 4c 69 73 74 2d 3e 70 46 69 72 73 74     pList->pFirst
d63c0 53 79 6e 63 65 64 20 3d 20 70 3b 0a 20 20 7d 0a  Synced = p;.  }.
d63d0 0a 20 20 70 4c 69 6e 6b 2d 3e 70 4e 65 78 74 20  .  pLink->pNext 
d63e0 3d 20 70 4c 69 6e 6b 2d 3e 70 50 72 65 76 20 3d  = pLink->pPrev =
d63f0 20 30 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 41 64   0;.}../* .** Ad
d6400 64 20 70 61 67 65 20 70 50 67 20 74 6f 20 74 68  d page pPg to th
d6410 65 20 6c 69 73 74 20 6f 66 20 66 72 65 65 20 70  e list of free p
d6420 61 67 65 73 20 66 6f 72 20 74 68 65 20 70 61 67  ages for the pag
d6430 65 72 2e 20 49 66 20 0a 2a 2a 20 6d 65 6d 6f 72  er. If .** memor
d6440 79 2d 6d 61 6e 61 67 65 6d 65 6e 74 20 69 73 20  y-management is 
d6450 65 6e 61 62 6c 65 64 2c 20 61 6c 73 6f 20 61 64  enabled, also ad
d6460 64 20 74 68 65 20 70 61 67 65 20 74 6f 20 74 68  d the page to th
d6470 65 20 67 6c 6f 62 61 6c 20 0a 2a 2a 20 6c 69 73  e global .** lis
d6480 74 20 6f 66 20 66 72 65 65 20 70 61 67 65 73 2e  t of free pages.
d6490 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
d64a0 6c 72 75 4c 69 73 74 41 64 64 28 50 67 48 64 72  lruListAdd(PgHdr
d64b0 20 2a 70 50 67 29 7b 0a 20 20 6c 69 73 74 41 64   *pPg){.  listAd
d64c0 64 28 26 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e  d(&pPg->pPager->
d64d0 6c 72 75 2c 20 26 70 50 67 2d 3e 66 72 65 65 2c  lru, &pPg->free,
d64e0 20 70 50 67 29 3b 0a 23 69 66 64 65 66 20 53 51   pPg);.#ifdef SQ
d64f0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f  LITE_ENABLE_MEMO
d6500 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 20 20  RY_MANAGEMENT.  
d6510 69 66 28 20 21 70 50 67 2d 3e 70 50 61 67 65 72  if( !pPg->pPager
d6520 2d 3e 6d 65 6d 44 62 20 29 7b 0a 20 20 20 20 73  ->memDb ){.    s
d6530 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
d6540 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  er(sqlite3_mutex
d6550 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  _alloc(SQLITE_MU
d6560 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 29 29  TEX_STATIC_LRU))
d6570 3b 0a 20 20 20 20 6c 69 73 74 41 64 64 28 26 73  ;.    listAdd(&s
d6580 71 6c 69 74 65 33 4c 72 75 50 61 67 65 4c 69 73  qlite3LruPageLis
d6590 74 2c 20 26 70 50 67 2d 3e 67 66 72 65 65 2c 20  t, &pPg->gfree, 
d65a0 70 50 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  pPg);.    sqlite
d65b0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71  3_mutex_leave(sq
d65c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
d65d0 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
d65e0 54 41 54 49 43 5f 4c 52 55 29 29 3b 0a 20 20 7d  TATIC_LRU));.  }
d65f0 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 20 0a 2a  .#endif.}../* .*
d6600 2a 20 52 65 6d 6f 76 65 20 70 61 67 65 20 70 50  * Remove page pP
d6610 67 20 66 72 6f 6d 20 74 68 65 20 6c 69 73 74 20  g from the list 
d6620 6f 66 20 66 72 65 65 20 70 61 67 65 73 20 66 6f  of free pages fo
d6630 72 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64  r the associated
d6640 20 70 61 67 65 72 2e 0a 2a 2a 20 49 66 20 6d 65   pager..** If me
d6650 6d 6f 72 79 2d 6d 61 6e 61 67 65 6d 65 6e 74 20  mory-management 
d6660 69 73 20 65 6e 61 62 6c 65 64 2c 20 61 6c 73 6f  is enabled, also
d6670 20 72 65 6d 6f 76 65 20 70 50 67 20 66 72 6f 6d   remove pPg from
d6680 20 74 68 65 20 67 6c 6f 62 61 6c 20 6c 69 73 74   the global list
d6690 0a 2a 2a 20 6f 66 20 66 72 65 65 20 70 61 67 65  .** of free page
d66a0 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
d66b0 64 20 6c 72 75 4c 69 73 74 52 65 6d 6f 76 65 28  d lruListRemove(
d66c0 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 6c  PgHdr *pPg){.  l
d66d0 69 73 74 52 65 6d 6f 76 65 28 26 70 50 67 2d 3e  istRemove(&pPg->
d66e0 70 50 61 67 65 72 2d 3e 6c 72 75 2c 20 26 70 50  pPager->lru, &pP
d66f0 67 2d 3e 66 72 65 65 2c 20 70 50 67 29 3b 0a 23  g->free, pPg);.#
d6700 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
d6710 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47  BLE_MEMORY_MANAG
d6720 45 4d 45 4e 54 0a 20 20 69 66 28 20 21 70 50 67  EMENT.  if( !pPg
d6730 2d 3e 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20  ->pPager->memDb 
d6740 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  ){.    sqlite3_m
d6750 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74  utex_enter(sqlit
d6760 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53  e3_mutex_alloc(S
d6770 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
d6780 49 43 5f 4c 52 55 29 29 3b 0a 20 20 20 20 6c 69  IC_LRU));.    li
d6790 73 74 52 65 6d 6f 76 65 28 26 73 71 6c 69 74 65  stRemove(&sqlite
d67a0 33 4c 72 75 50 61 67 65 4c 69 73 74 2c 20 26 70  3LruPageList, &p
d67b0 50 67 2d 3e 67 66 72 65 65 2c 20 70 50 67 29 3b  Pg->gfree, pPg);
d67c0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
d67d0 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33  ex_leave(sqlite3
d67e0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c  _mutex_alloc(SQL
d67f0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
d6800 5f 4c 52 55 29 29 3b 0a 20 20 7d 0a 23 65 6e 64  _LRU));.  }.#end
d6810 69 66 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69  if.}../* .** Thi
d6820 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
d6830 6c 6c 65 64 20 6a 75 73 74 20 61 66 74 65 72 20  lled just after 
d6840 74 68 65 20 6e 65 65 64 53 79 6e 63 20 66 6c 61  the needSync fla
d6850 67 20 68 61 73 20 62 65 65 6e 20 63 6c 65 61 72  g has been clear
d6860 65 64 0a 2a 2a 20 66 72 6f 6d 20 61 6c 6c 20 70  ed.** from all p
d6870 61 67 65 73 20 6d 61 6e 61 67 65 64 20 62 79 20  ages managed by 
d6880 70 50 61 67 65 72 20 28 75 73 75 61 6c 6c 79 20  pPager (usually 
d6890 62 65 63 61 75 73 65 20 74 68 65 20 6a 6f 75 72  because the jour
d68a0 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 68 61 73 20  nal file.** has 
d68b0 6a 75 73 74 20 62 65 65 6e 20 73 79 6e 63 65 64  just been synced
d68c0 29 2e 20 49 74 20 75 70 64 61 74 65 73 20 74 68  ). It updates th
d68d0 65 20 70 50 61 67 65 72 2d 3e 6c 72 75 2e 70 46  e pPager->lru.pF
d68e0 69 72 73 74 53 79 6e 63 65 64 20 76 61 72 69 61  irstSynced varia
d68f0 62 6c 65 0a 2a 2a 20 61 6e 64 2c 20 69 66 20 6d  ble.** and, if m
d6900 65 6d 6f 72 79 2d 6d 61 6e 61 67 65 6d 65 6e 74  emory-management
d6910 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65   is enabled, the
d6920 20 73 71 6c 69 74 65 33 4c 72 75 50 61 67 65 4c   sqlite3LruPageL
d6930 69 73 74 2e 70 46 69 72 73 74 53 79 6e 63 65 64  ist.pFirstSynced
d6940 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 61 6c 73  .** variable als
d6950 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  o..*/.static voi
d6960 64 20 6c 72 75 4c 69 73 74 53 65 74 46 69 72 73  d lruListSetFirs
d6970 74 53 79 6e 63 65 64 28 50 61 67 65 72 20 2a 70  tSynced(Pager *p
d6980 50 61 67 65 72 29 7b 0a 20 20 70 50 61 67 65 72  Pager){.  pPager
d6990 2d 3e 6c 72 75 2e 70 46 69 72 73 74 53 79 6e 63  ->lru.pFirstSync
d69a0 65 64 20 3d 20 70 50 61 67 65 72 2d 3e 6c 72 75  ed = pPager->lru
d69b0 2e 70 46 69 72 73 74 3b 0a 23 69 66 64 65 66 20  .pFirst;.#ifdef 
d69c0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
d69d0 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a  MORY_MANAGEMENT.
d69e0 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6d    if( !pPager->m
d69f0 65 6d 44 62 20 29 7b 0a 20 20 20 20 50 67 48 64  emDb ){.    PgHd
d6a00 72 20 2a 70 3b 0a 20 20 20 20 73 71 6c 69 74 65  r *p;.    sqlite
d6a10 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71  3_mutex_enter(sq
d6a20 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
d6a30 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
d6a40 54 41 54 49 43 5f 4c 52 55 29 29 3b 0a 20 20 20  TATIC_LRU));.   
d6a50 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 33 4c 72   for(p=sqlite3Lr
d6a60 75 50 61 67 65 4c 69 73 74 2e 70 46 69 72 73 74  uPageList.pFirst
d6a70 3b 20 70 20 26 26 20 70 2d 3e 6e 65 65 64 53 79  ; p && p->needSy
d6a80 6e 63 3b 20 70 3d 70 2d 3e 67 66 72 65 65 2e 70  nc; p=p->gfree.p
d6a90 4e 65 78 74 29 3b 0a 20 20 20 20 61 73 73 65 72  Next);.    asser
d6aa0 74 28 70 3d 3d 70 50 61 67 65 72 2d 3e 6c 72 75  t(p==pPager->lru
d6ab0 2e 70 46 69 72 73 74 53 79 6e 63 65 64 20 7c 7c  .pFirstSynced ||
d6ac0 20 70 3d 3d 73 71 6c 69 74 65 33 4c 72 75 50 61   p==sqlite3LruPa
d6ad0 67 65 4c 69 73 74 2e 70 46 69 72 73 74 53 79 6e  geList.pFirstSyn
d6ae0 63 65 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ced);.    sqlite
d6af0 33 4c 72 75 50 61 67 65 4c 69 73 74 2e 70 46 69  3LruPageList.pFi
d6b00 72 73 74 53 79 6e 63 65 64 20 3d 20 70 3b 0a 20  rstSynced = p;. 
d6b10 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
d6b20 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d  _leave(sqlite3_m
d6b30 75 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54  utex_alloc(SQLIT
d6b40 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c  E_MUTEX_STATIC_L
d6b50 52 55 29 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  RU));.  }.#endif
d6b60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
d6b70 20 74 72 75 65 20 69 66 20 70 61 67 65 20 2a 70   true if page *p
d6b80 50 67 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  Pg has already b
d6b90 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74  een written to t
d6ba0 68 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20  he statement.** 
d6bb0 6a 6f 75 72 6e 61 6c 20 28 6f 72 20 73 74 61 74  journal (or stat
d6bc0 65 6d 65 6e 74 20 73 6e 61 70 73 68 6f 74 20 68  ement snapshot h
d6bd0 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c  as been created,
d6be0 20 69 66 20 2a 70 50 67 20 69 73 20 70 61 72 74   if *pPg is part
d6bf0 0a 2a 2a 20 6f 66 20 61 6e 20 69 6e 2d 6d 65 6d  .** of an in-mem
d6c00 6f 72 79 20 64 61 74 61 62 61 73 65 29 2e 0a 2a  ory database)..*
d6c10 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
d6c20 65 49 6e 53 74 61 74 65 6d 65 6e 74 28 50 67 48  eInStatement(PgH
d6c30 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65  dr *pPg){.  Page
d6c40 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d  r *pPager = pPg-
d6c50 3e 70 50 61 67 65 72 3b 0a 20 20 69 66 28 20 4d  >pPager;.  if( M
d6c60 45 4d 44 42 20 29 7b 0a 20 20 20 20 72 65 74 75  EMDB ){.    retu
d6c70 72 6e 20 50 47 48 44 52 5f 54 4f 5f 48 49 53 54  rn PGHDR_TO_HIST
d6c80 28 70 50 67 2c 20 70 50 61 67 65 72 29 2d 3e 69  (pPg, pPager)->i
d6c90 6e 53 74 6d 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a  nStmt;.  }else{.
d6ca0 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
d6cb0 65 33 42 69 74 76 65 63 54 65 73 74 28 70 50 61  e3BitvecTest(pPa
d6cc0 67 65 72 2d 3e 70 49 6e 53 74 6d 74 2c 20 70 50  ger->pInStmt, pP
d6cd0 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a 7d 0a  g->pgno);.  }.}.
d6ce0 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
d6cf0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70 61  e size of the pa
d6d00 67 65 72 20 68 61 73 68 20 74 61 62 6c 65 20 74  ger hash table t
d6d10 6f 20 4e 2e 20 20 4e 20 6d 75 73 74 20 62 65 20  o N.  N must be 
d6d20 61 20 70 6f 77 65 72 0a 2a 2a 20 6f 66 20 74 77  a power.** of tw
d6d30 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  o..*/.static voi
d6d40 64 20 70 61 67 65 72 5f 72 65 73 69 7a 65 5f 68  d pager_resize_h
d6d50 61 73 68 5f 74 61 62 6c 65 28 50 61 67 65 72 20  ash_table(Pager 
d6d60 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 4e 29 7b  *pPager, int N){
d6d70 0a 20 20 50 67 48 64 72 20 2a 2a 61 48 61 73 68  .  PgHdr **aHash
d6d80 2c 20 2a 70 50 67 3b 0a 20 20 61 73 73 65 72 74  , *pPg;.  assert
d6d90 28 20 4e 3e 30 20 26 26 20 28 4e 26 28 4e 2d 31  ( N>0 && (N&(N-1
d6da0 29 29 3d 3d 30 20 29 3b 0a 23 69 66 64 65 66 20  ))==0 );.#ifdef 
d6db0 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f  SQLITE_MALLOC_SO
d6dc0 46 54 5f 4c 49 4d 49 54 0a 20 20 69 66 28 20 4e  FT_LIMIT.  if( N
d6dd0 2a 73 69 7a 65 6f 66 28 61 48 61 73 68 5b 30 5d  *sizeof(aHash[0]
d6de0 29 3e 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f  )>SQLITE_MALLOC_
d6df0 53 4f 46 54 5f 4c 49 4d 49 54 20 29 7b 0a 20 20  SOFT_LIMIT ){.  
d6e00 20 20 4e 20 3d 20 53 51 4c 49 54 45 5f 4d 41 4c    N = SQLITE_MAL
d6e10 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 2f 73  LOC_SOFT_LIMIT/s
d6e20 69 7a 65 6f 66 28 61 48 61 73 68 5b 30 5d 29 3b  izeof(aHash[0]);
d6e30 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3d 3d 70 50  .  }.  if( N==pP
d6e40 61 67 65 72 2d 3e 6e 48 61 73 68 20 29 20 72 65  ager->nHash ) re
d6e50 74 75 72 6e 3b 0a 23 65 6e 64 69 66 0a 20 20 70  turn;.#endif.  p
d6e60 61 67 65 72 4c 65 61 76 65 28 70 50 61 67 65 72  agerLeave(pPager
d6e70 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  );.  if( pPager-
d6e80 3e 61 48 61 73 68 21 3d 30 20 29 20 73 71 6c 69  >aHash!=0 ) sqli
d6e90 74 65 33 46 61 75 6c 74 42 65 67 69 6e 42 65 6e  te3FaultBeginBen
d6ea0 69 67 6e 28 53 51 4c 49 54 45 5f 46 41 55 4c 54  ign(SQLITE_FAULT
d6eb0 49 4e 4a 45 43 54 4f 52 5f 4d 41 4c 4c 4f 43 29  INJECTOR_MALLOC)
d6ec0 3b 0a 20 20 61 48 61 73 68 20 3d 20 73 71 6c 69  ;.  aHash = sqli
d6ed0 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73  te3MallocZero( s
d6ee0 69 7a 65 6f 66 28 61 48 61 73 68 5b 30 5d 29 2a  izeof(aHash[0])*
d6ef0 4e 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65  N );.  if( pPage
d6f00 72 2d 3e 61 48 61 73 68 21 3d 30 20 29 20 73 71  r->aHash!=0 ) sq
d6f10 6c 69 74 65 33 46 61 75 6c 74 45 6e 64 42 65 6e  lite3FaultEndBen
d6f20 69 67 6e 28 53 51 4c 49 54 45 5f 46 41 55 4c 54  ign(SQLITE_FAULT
d6f30 49 4e 4a 45 43 54 4f 52 5f 4d 41 4c 4c 4f 43 29  INJECTOR_MALLOC)
d6f40 3b 0a 20 20 70 61 67 65 72 45 6e 74 65 72 28 70  ;.  pagerEnter(p
d6f50 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 61 48  Pager);.  if( aH
d6f60 61 73 68 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  ash==0 ){.    /*
d6f70 20 46 61 69 6c 75 72 65 20 74 6f 20 72 65 68 61   Failure to reha
d6f80 73 68 20 69 73 20 6e 6f 74 20 61 6e 20 65 72 72  sh is not an err
d6f90 6f 72 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 20  or.  It is only 
d6fa0 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 68 69  a performance hi
d6fb0 74 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  t. */.    return
d6fc0 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
d6fd0 66 72 65 65 28 70 50 61 67 65 72 2d 3e 61 48 61  free(pPager->aHa
d6fe0 73 68 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e  sh);.  pPager->n
d6ff0 48 61 73 68 20 3d 20 4e 3b 0a 20 20 70 50 61 67  Hash = N;.  pPag
d7000 65 72 2d 3e 61 48 61 73 68 20 3d 20 61 48 61 73  er->aHash = aHas
d7010 68 3b 0a 20 20 66 6f 72 28 70 50 67 3d 70 50 61  h;.  for(pPg=pPa
d7020 67 65 72 2d 3e 70 41 6c 6c 3b 20 70 50 67 3b 20  ger->pAll; pPg; 
d7030 70 50 67 3d 70 50 67 2d 3e 70 4e 65 78 74 41 6c  pPg=pPg->pNextAl
d7040 6c 29 7b 0a 20 20 20 20 69 6e 74 20 68 3b 0a 20  l){.    int h;. 
d7050 20 20 20 69 66 28 20 70 50 67 2d 3e 70 67 6e 6f     if( pPg->pgno
d7060 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ==0 ){.      ass
d7070 65 72 74 28 20 70 50 67 2d 3e 70 4e 65 78 74 48  ert( pPg->pNextH
d7080 61 73 68 3d 3d 30 20 26 26 20 70 50 67 2d 3e 70  ash==0 && pPg->p
d7090 50 72 65 76 48 61 73 68 3d 3d 30 20 29 3b 0a 20  PrevHash==0 );. 
d70a0 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
d70b0 20 20 20 7d 0a 20 20 20 20 68 20 3d 20 70 50 67     }.    h = pPg
d70c0 2d 3e 70 67 6e 6f 20 26 20 28 4e 2d 31 29 3b 0a  ->pgno & (N-1);.
d70d0 20 20 20 20 70 50 67 2d 3e 70 4e 65 78 74 48 61      pPg->pNextHa
d70e0 73 68 20 3d 20 61 48 61 73 68 5b 68 5d 3b 0a 20  sh = aHash[h];. 
d70f0 20 20 20 69 66 28 20 61 48 61 73 68 5b 68 5d 20     if( aHash[h] 
d7100 29 7b 0a 20 20 20 20 20 20 61 48 61 73 68 5b 68  ){.      aHash[h
d7110 5d 2d 3e 70 50 72 65 76 48 61 73 68 20 3d 20 70  ]->pPrevHash = p
d7120 50 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 48  Pg;.    }.    aH
d7130 61 73 68 5b 68 5d 20 3d 20 70 50 67 3b 0a 20 20  ash[h] = pPg;.  
d7140 20 20 70 50 67 2d 3e 70 50 72 65 76 48 61 73 68    pPg->pPrevHash
d7150 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   = 0;.  }.}../*.
d7160 2a 2a 20 52 65 61 64 20 61 20 33 32 2d 62 69 74  ** Read a 32-bit
d7170 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 74 68   integer from th
d7180 65 20 67 69 76 65 6e 20 66 69 6c 65 20 64 65 73  e given file des
d7190 63 72 69 70 74 6f 72 2e 20 20 53 74 6f 72 65 20  criptor.  Store 
d71a0 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 74  the integer.** t
d71b0 68 61 74 20 69 73 20 72 65 61 64 20 69 6e 20 2a  hat is read in *
d71c0 70 52 65 73 2e 20 20 52 65 74 75 72 6e 20 53 51  pRes.  Return SQ
d71d0 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79  LITE_OK if every
d71e0 74 68 69 6e 67 20 77 6f 72 6b 65 64 2c 20 6f 72  thing worked, or
d71f0 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64   an.** error cod
d7200 65 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 67  e is something g
d7210 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a  oes wrong..**.**
d7220 20 41 6c 6c 20 76 61 6c 75 65 73 20 61 72 65 20   All values are 
d7230 73 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b 20 61  stored on disk a
d7240 73 20 62 69 67 2d 65 6e 64 69 61 6e 2e 0a 2a 2f  s big-endian..*/
d7250 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64  .static int read
d7260 33 32 62 69 74 73 28 73 71 6c 69 74 65 33 5f 66  32bits(sqlite3_f
d7270 69 6c 65 20 2a 66 64 2c 20 69 36 34 20 6f 66 66  ile *fd, i64 off
d7280 73 65 74 2c 20 75 33 32 20 2a 70 52 65 73 29 7b  set, u32 *pRes){
d7290 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
d72a0 20 61 63 5b 34 5d 3b 0a 20 20 69 6e 74 20 72 63   ac[4];.  int rc
d72b0 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64   = sqlite3OsRead
d72c0 28 66 64 2c 20 61 63 2c 20 73 69 7a 65 6f 66 28  (fd, ac, sizeof(
d72d0 61 63 29 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20  ac), offset);.  
d72e0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
d72f0 4b 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d  K ){.    *pRes =
d7300 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65   sqlite3Get4byte
d7310 28 61 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  (ac);.  }.  retu
d7320 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
d7330 57 72 69 74 65 20 61 20 33 32 2d 62 69 74 20 69  Write a 32-bit i
d7340 6e 74 65 67 65 72 20 69 6e 74 6f 20 61 20 73 74  nteger into a st
d7350 72 69 6e 67 20 62 75 66 66 65 72 20 69 6e 20 62  ring buffer in b
d7360 69 67 2d 65 6e 64 69 61 6e 20 62 79 74 65 20 6f  ig-endian byte o
d7370 72 64 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  rder..*/.#define
d7380 20 70 75 74 33 32 62 69 74 73 28 41 2c 42 29 20   put32bits(A,B) 
d7390 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 65   sqlite3Put4byte
d73a0 28 28 75 38 2a 29 41 2c 42 29 0a 0a 2f 2a 0a 2a  ((u8*)A,B)../*.*
d73b0 2a 20 57 72 69 74 65 20 61 20 33 32 2d 62 69 74  * Write a 32-bit
d73c0 20 69 6e 74 65 67 65 72 20 69 6e 74 6f 20 74 68   integer into th
d73d0 65 20 67 69 76 65 6e 20 66 69 6c 65 20 64 65 73  e given file des
d73e0 63 72 69 70 74 6f 72 2e 20 20 52 65 74 75 72 6e  criptor.  Return
d73f0 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e   SQLITE_OK.** on
d7400 20 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65   success or an e
d7410 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f 6d  rror code is som
d7420 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e  ething goes wron
d7430 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  g..*/.static int
d7440 20 77 72 69 74 65 33 32 62 69 74 73 28 73 71 6c   write32bits(sql
d7450 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 69  ite3_file *fd, i
d7460 36 34 20 6f 66 66 73 65 74 2c 20 75 33 32 20 76  64 offset, u32 v
d7470 61 6c 29 7b 0a 20 20 63 68 61 72 20 61 63 5b 34  al){.  char ac[4
d7480 5d 3b 0a 20 20 70 75 74 33 32 62 69 74 73 28 61  ];.  put32bits(a
d7490 63 2c 20 76 61 6c 29 3b 0a 20 20 72 65 74 75 72  c, val);.  retur
d74a0 6e 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65  n sqlite3OsWrite
d74b0 28 66 64 2c 20 61 63 2c 20 34 2c 20 6f 66 66 73  (fd, ac, 4, offs
d74c0 65 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  et);.}../*.** If
d74d0 20 66 69 6c 65 20 70 46 64 20 69 73 20 6f 70 65   file pFd is ope
d74e0 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 4f  n, call sqlite3O
d74f0 73 55 6e 6c 6f 63 6b 28 29 20 6f 6e 20 69 74 2e  sUnlock() on it.
d7500 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
d7510 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  sUnlock(sqlite3_
d7520 66 69 6c 65 20 2a 70 46 64 2c 20 69 6e 74 20 65  file *pFd, int e
d7530 4c 6f 63 6b 29 7b 0a 20 20 69 66 28 20 21 70 46  Lock){.  if( !pF
d7540 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20  d->pMethods ){. 
d7550 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
d7560 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  _OK;.  }.  retur
d7570 6e 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63  n sqlite3OsUnloc
d7580 6b 28 70 46 64 2c 20 65 4c 6f 63 6b 29 3b 0a 7d  k(pFd, eLock);.}
d7590 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
d75a0 63 74 69 6f 6e 20 64 65 74 65 72 6d 69 6e 65 73  ction determines
d75b0 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
d75c0 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65  the atomic-write
d75d0 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a 2a 2a   optimization.**
d75e0 20 63 61 6e 20 62 65 20 75 73 65 64 20 77 69 74   can be used wit
d75f0 68 20 74 68 69 73 20 70 61 67 65 72 2e 20 54 68  h this pager. Th
d7600 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 63  e optimization c
d7610 61 6e 20 62 65 20 75 73 65 64 20 69 66 3a 0a 2a  an be used if:.*
d7620 2a 0a 2a 2a 20 20 28 61 29 20 74 68 65 20 76 61  *.**  (a) the va
d7630 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20  lue returned by 
d7640 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65  OsDeviceCharacte
d7650 72 69 73 74 69 63 73 28 29 20 69 6e 64 69 63 61  ristics() indica
d7660 74 65 73 20 74 68 61 74 0a 2a 2a 20 20 20 20 20  tes that.**     
d7670 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65   a database page
d7680 20 6d 61 79 20 62 65 20 77 72 69 74 74 65 6e 20   may be written 
d7690 61 74 6f 6d 69 63 61 6c 6c 79 2c 20 61 6e 64 0a  atomically, and.
d76a0 2a 2a 20 20 28 62 29 20 74 68 65 20 76 61 6c 75  **  (b) the valu
d76b0 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 4f 73  e returned by Os
d76c0 53 65 63 74 6f 72 53 69 7a 65 28 29 20 69 73 20  SectorSize() is 
d76d0 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75  less than or equ
d76e0 61 6c 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 68  al.**      to th
d76f0 65 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 0a  e page size..**.
d7700 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69  ** If the optimi
d7710 7a 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65  zation cannot be
d7720 20 75 73 65 64 2c 20 30 20 69 73 20 72 65 74 75   used, 0 is retu
d7730 72 6e 65 64 2e 20 49 66 20 69 74 20 63 61 6e 20  rned. If it can 
d7740 62 65 20 75 73 65 64 2c 0a 2a 2a 20 74 68 65 6e  be used,.** then
d7750 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   the value retur
d7760 6e 65 64 20 69 73 20 74 68 65 20 73 69 7a 65 20  ned is the size 
d7770 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  of the journal f
d7780 69 6c 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20 63  ile when it.** c
d7790 6f 6e 74 61 69 6e 73 20 72 6f 6c 6c 62 61 63 6b  ontains rollback
d77a0 20 64 61 74 61 20 66 6f 72 20 65 78 61 63 74 6c   data for exactl
d77b0 79 20 6f 6e 65 20 70 61 67 65 2e 0a 2a 2f 0a 23  y one page..*/.#
d77c0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
d77d0 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45  BLE_ATOMIC_WRITE
d77e0 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c  .static int jrnl
d77f0 42 75 66 66 65 72 53 69 7a 65 28 50 61 67 65 72  BufferSize(Pager
d7800 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74   *pPager){.  int
d7810 20 64 63 3b 20 20 20 20 20 20 20 20 20 20 20 2f   dc;           /
d7820 2a 20 44 65 76 69 63 65 20 63 68 61 72 61 63 74  * Device charact
d7830 65 72 69 73 74 69 63 73 20 2a 2f 0a 20 20 69 6e  eristics */.  in
d7840 74 20 6e 53 65 63 74 6f 72 3b 20 20 20 20 20 20  t nSector;      
d7850 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 2a  /* Sector size *
d7860 2f 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 20 20  /.  int nPage;  
d7870 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 73 69        /* Page si
d7880 7a 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ze */.  sqlite3_
d7890 66 69 6c 65 20 2a 66 64 20 3d 20 70 50 61 67 65  file *fd = pPage
d78a0 72 2d 3e 66 64 3b 0a 0a 20 20 69 66 28 20 66 64  r->fd;..  if( fd
d78b0 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20  ->pMethods ){.  
d78c0 20 20 64 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    dc = sqlite3Os
d78d0 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
d78e0 73 74 69 63 73 28 66 64 29 3b 0a 20 20 20 20 6e  stics(fd);.    n
d78f0 53 65 63 74 6f 72 20 3d 20 73 71 6c 69 74 65 33  Sector = sqlite3
d7900 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 66 64 29  OsSectorSize(fd)
d7910 3b 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 70 50  ;.    nPage = pP
d7920 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a  ager->pageSize;.
d7930 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 53 51    }..  assert(SQ
d7940 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
d7950 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29 3b  C512==(512>>8));
d7960 0a 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45  .  assert(SQLITE
d7970 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 4b  _IOCAP_ATOMIC64K
d7980 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29 3b 0a 0a  ==(65536>>8));..
d7990 20 20 69 66 28 20 21 66 64 2d 3e 70 4d 65 74 68    if( !fd->pMeth
d79a0 6f 64 73 20 7c 7c 20 28 64 63 26 28 53 51 4c 49  ods || (dc&(SQLI
d79b0 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 7c  TE_IOCAP_ATOMIC|
d79c0 28 6e 50 61 67 65 3e 3e 38 29 29 26 26 6e 53 65  (nPage>>8))&&nSe
d79d0 63 74 6f 72 3c 3d 6e 50 61 67 65 29 20 29 7b 0a  ctor<=nPage) ){.
d79e0 20 20 20 20 72 65 74 75 72 6e 20 4a 4f 55 52 4e      return JOURN
d79f0 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
d7a00 29 20 2b 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53  ) + JOURNAL_PG_S
d7a10 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20  Z(pPager);.  }. 
d7a20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e   return 0;.}.#en
d7a30 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  dif../*.** This 
d7a40 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20  function should 
d7a50 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61  be called when a
d7a60 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77  n error occurs w
d7a70 69 74 68 69 6e 20 74 68 65 20 70 61 67 65 72 0a  ithin the pager.
d7a80 2a 2a 20 63 6f 64 65 2e 20 54 68 65 20 66 69 72  ** code. The fir
d7a90 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61  st argument is a
d7aa0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
d7ab0 70 61 67 65 72 20 73 74 72 75 63 74 75 72 65 2c  pager structure,
d7ac0 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 74   the.** second t
d7ad0 68 65 20 65 72 72 6f 72 2d 63 6f 64 65 20 61 62  he error-code ab
d7ae0 6f 75 74 20 74 6f 20 62 65 20 72 65 74 75 72 6e  out to be return
d7af0 65 64 20 62 79 20 61 20 70 61 67 65 72 20 41 50  ed by a pager AP
d7b00 49 20 66 75 6e 63 74 69 6f 6e 2e 20 0a 2a 2a 20  I function. .** 
d7b10 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  The value return
d7b20 65 64 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20  ed is a copy of 
d7b30 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
d7b40 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63  ent to this func
d7b50 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20  tion. .**.** If 
d7b60 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
d7b70 65 6e 74 20 69 73 20 53 51 4c 49 54 45 5f 49 4f  ent is SQLITE_IO
d7b80 45 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f 52 52  ERR, SQLITE_CORR
d7b90 55 50 54 2c 20 6f 72 20 53 51 4c 49 54 45 5f 46  UPT, or SQLITE_F
d7ba0 55 4c 4c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72  ULL.** the error
d7bb0 20 62 65 63 6f 6d 65 73 20 70 65 72 73 69 73 74   becomes persist
d7bc0 65 6e 74 2e 20 55 6e 74 69 6c 20 74 68 65 20 70  ent. Until the p
d7bd0 65 72 73 69 73 74 65 6e 20 65 72 72 6f 72 20 69  ersisten error i
d7be0 73 20 63 6c 65 61 72 65 64 2c 0a 2a 2a 20 73 75  s cleared,.** su
d7bf0 62 73 65 71 75 65 6e 74 20 41 50 49 20 63 61 6c  bsequent API cal
d7c00 6c 73 20 6f 6e 20 74 68 69 73 20 50 61 67 65 72  ls on this Pager
d7c10 20 77 69 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c   will immediatel
d7c20 79 20 72 65 74 75 72 6e 20 74 68 65 20 73 61 6d  y return the sam
d7c30 65 20 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65  e .** error code
d7c40 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 65 72 73 69 73  ..**.** A persis
d7c50 74 65 6e 74 20 65 72 72 6f 72 20 69 6e 64 69 63  tent error indic
d7c60 61 74 65 73 20 74 68 61 74 20 74 68 65 20 63 6f  ates that the co
d7c70 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61  ntents of the pa
d7c80 67 65 72 2d 63 61 63 68 65 20 0a 2a 2a 20 63 61  ger-cache .** ca
d7c90 6e 6e 6f 74 20 62 65 20 74 72 75 73 74 65 64 2e  nnot be trusted.
d7ca0 20 54 68 69 73 20 73 74 61 74 65 20 63 61 6e 20   This state can 
d7cb0 62 65 20 63 6c 65 61 72 65 64 20 62 79 20 63 6f  be cleared by co
d7cc0 6d 70 6c 65 74 65 6c 79 20 64 69 73 63 61 72 64  mpletely discard
d7cd0 69 6e 67 20 0a 2a 2a 20 74 68 65 20 63 6f 6e 74  ing .** the cont
d7ce0 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65  ents of the page
d7cf0 72 2d 63 61 63 68 65 2e 20 49 66 20 61 20 74 72  r-cache. If a tr
d7d00 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 61 63  ansaction was ac
d7d10 74 69 76 65 20 77 68 65 6e 0a 2a 2a 20 74 68 65  tive when.** the
d7d20 20 70 65 72 73 69 73 74 65 6e 74 20 65 72 72 6f   persistent erro
d7d30 72 20 6f 63 63 75 72 65 64 2c 20 74 68 65 6e 20  r occured, then 
d7d40 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  the rollback jou
d7d50 72 6e 61 6c 20 6d 61 79 20 6e 65 65 64 0a 2a 2a  rnal may need.**
d7d60 20 74 6f 20 62 65 20 72 65 70 6c 61 79 65 64 2e   to be replayed.
d7d70 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
d7d80 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 50 61 67  pager_unlock(Pag
d7d90 65 72 20 2a 70 50 61 67 65 72 29 3b 0a 73 74 61  er *pPager);.sta
d7da0 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 65 72  tic int pager_er
d7db0 72 6f 72 28 50 61 67 65 72 20 2a 70 50 61 67 65  ror(Pager *pPage
d7dc0 72 2c 20 69 6e 74 20 72 63 29 7b 0a 20 20 69 6e  r, int rc){.  in
d7dd0 74 20 72 63 32 20 3d 20 72 63 20 26 20 30 78 66  t rc2 = rc & 0xf
d7de0 66 3b 0a 20 20 61 73 73 65 72 74 28 0a 20 20 20  f;.  assert(.   
d7df0 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43      pPager->errC
d7e00 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c  ode==SQLITE_FULL
d7e10 20 7c 7c 0a 20 20 20 20 20 20 20 70 50 61 67 65   ||.       pPage
d7e20 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49  r->errCode==SQLI
d7e30 54 45 5f 4f 4b 20 7c 7c 0a 20 20 20 20 20 20 20  TE_OK ||.       
d7e40 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65  (pPager->errCode
d7e50 20 26 20 30 78 66 66 29 3d 3d 53 51 4c 49 54 45   & 0xff)==SQLITE
d7e60 5f 49 4f 45 52 52 0a 20 20 29 3b 0a 20 20 69 66  _IOERR.  );.  if
d7e70 28 0a 20 20 20 20 72 63 32 3d 3d 53 51 4c 49 54  (.    rc2==SQLIT
d7e80 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20 20 20 72 63  E_FULL ||.    rc
d7e90 32 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20  2==SQLITE_IOERR 
d7ea0 7c 7c 0a 20 20 20 20 72 63 32 3d 3d 53 51 4c 49  ||.    rc2==SQLI
d7eb0 54 45 5f 43 4f 52 52 55 50 54 0a 20 20 29 7b 0a  TE_CORRUPT.  ){.
d7ec0 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43      pPager->errC
d7ed0 6f 64 65 20 3d 20 72 63 3b 0a 20 20 20 20 69 66  ode = rc;.    if
d7ee0 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d  ( pPager->state=
d7ef0 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 26 26  =PAGER_UNLOCK &&
d7f00 20 70 50 61 67 65 72 2d 3e 6e 52 65 66 3d 3d 30   pPager->nRef==0
d7f10 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20   ){.      /* If 
d7f20 74 68 65 20 70 61 67 65 72 20 69 73 20 61 6c 72  the pager is alr
d7f30 65 61 64 79 20 75 6e 6c 6f 63 6b 65 64 2c 20 63  eady unlocked, c
d7f40 61 6c 6c 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b  all pager_unlock
d7f50 28 29 20 6e 6f 77 20 74 6f 0a 20 20 20 20 20 20  () now to.      
d7f60 2a 2a 20 63 6c 65 61 72 20 74 68 65 20 65 72 72  ** clear the err
d7f70 6f 72 20 73 74 61 74 65 20 61 6e 64 20 65 6e 73  or state and ens
d7f80 75 72 65 20 74 68 61 74 20 74 68 65 20 70 61 67  ure that the pag
d7f90 65 72 2d 63 61 63 68 65 20 69 73 20 0a 20 20 20  er-cache is .   
d7fa0 20 20 20 2a 2a 20 63 6f 6d 70 6c 65 74 65 6c 79     ** completely
d7fb0 20 65 6d 70 74 79 2e 0a 20 20 20 20 20 20 2a 2f   empty..      */
d7fc0 0a 20 20 20 20 20 20 70 61 67 65 72 5f 75 6e 6c  .      pager_unl
d7fd0 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20  ock(pPager);.   
d7fe0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
d7ff0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  rc;.}../*.** If 
d8000 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47  SQLITE_CHECK_PAG
d8010 45 53 20 69 73 20 64 65 66 69 6e 65 64 20 74 68  ES is defined th
d8020 65 6e 20 77 65 20 64 6f 20 73 6f 6d 65 20 73 61  en we do some sa
d8030 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 0a 2a 2a  nity checking.**
d8040 20 6f 6e 20 74 68 65 20 63 61 63 68 65 20 75 73   on the cache us
d8050 69 6e 67 20 61 20 68 61 73 68 20 66 75 6e 63 74  ing a hash funct
d8060 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 75 73  ion.  This is us
d8070 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a  ed for testing.*
d8080 2a 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20  * and debugging 
d8090 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  only..*/.#ifdef 
d80a0 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47  SQLITE_CHECK_PAG
d80b0 45 53 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  ES./*.** Return 
d80c0 61 20 33 32 2d 62 69 74 20 68 61 73 68 20 6f 66  a 32-bit hash of
d80d0 20 74 68 65 20 70 61 67 65 20 64 61 74 61 20 66   the page data f
d80e0 6f 72 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 74 61  or pPage..*/.sta
d80f0 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 64 61  tic u32 pager_da
d8100 74 61 68 61 73 68 28 69 6e 74 20 6e 42 79 74 65  tahash(int nByte
d8110 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  , unsigned char 
d8120 2a 70 44 61 74 61 29 7b 0a 20 20 75 33 32 20 68  *pData){.  u32 h
d8130 61 73 68 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69  ash = 0;.  int i
d8140 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  ;.  for(i=0; i<n
d8150 42 79 74 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Byte; i++){.    
d8160 68 61 73 68 20 3d 20 28 68 61 73 68 2a 31 30 33  hash = (hash*103
d8170 39 29 20 2b 20 70 44 61 74 61 5b 69 5d 3b 0a 20  9) + pData[i];. 
d8180 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 61 73 68   }.  return hash
d8190 3b 0a 7d 0a 73 74 61 74 69 63 20 75 33 32 20 70  ;.}.static u32 p
d81a0 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 50 67  ager_pagehash(Pg
d81b0 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 72  Hdr *pPage){.  r
d81c0 65 74 75 72 6e 20 70 61 67 65 72 5f 64 61 74 61  eturn pager_data
d81d0 68 61 73 68 28 70 50 61 67 65 2d 3e 70 50 61 67  hash(pPage->pPag
d81e0 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 0a 20  er->pageSize, . 
d81f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d8200 20 20 20 20 20 20 20 28 75 6e 73 69 67 6e 65 64         (unsigned
d8210 20 63 68 61 72 20 2a 29 50 47 48 44 52 5f 54 4f   char *)PGHDR_TO
d8220 5f 44 41 54 41 28 70 50 61 67 65 29 29 3b 0a 7d  _DATA(pPage));.}
d8230 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 48 45 43  ../*.** The CHEC
d8240 4b 5f 50 41 47 45 20 6d 61 63 72 6f 20 74 61 6b  K_PAGE macro tak
d8250 65 73 20 61 20 50 67 48 64 72 2a 20 61 73 20 61  es a PgHdr* as a
d8260 6e 20 61 72 67 75 6d 65 6e 74 2e 20 49 66 20 53  n argument. If S
d8270 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45  QLITE_CHECK_PAGE
d8280 53 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 2c  S.** is defined,
d8290 20 61 6e 64 20 4e 44 45 42 55 47 20 69 73 20 6e   and NDEBUG is n
d82a0 6f 74 20 64 65 66 69 6e 65 64 2c 20 61 6e 20 61  ot defined, an a
d82b0 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e  ssert() statemen
d82c0 74 20 63 68 65 63 6b 73 0a 2a 2a 20 74 68 61 74  t checks.** that
d82d0 20 74 68 65 20 70 61 67 65 20 69 73 20 65 69 74   the page is eit
d82e0 68 65 72 20 64 69 72 74 79 20 6f 72 20 73 74 69  her dirty or sti
d82f0 6c 6c 20 6d 61 74 63 68 65 73 20 74 68 65 20 63  ll matches the c
d8300 61 6c 63 75 6c 61 74 65 64 20 70 61 67 65 2d 68  alculated page-h
d8310 61 73 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ash..*/.#define 
d8320 43 48 45 43 4b 5f 50 41 47 45 28 78 29 20 63 68  CHECK_PAGE(x) ch
d8330 65 63 6b 50 61 67 65 28 78 29 0a 73 74 61 74 69  eckPage(x).stati
d8340 63 20 76 6f 69 64 20 63 68 65 63 6b 50 61 67 65  c void checkPage
d8350 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20  (PgHdr *pPg){.  
d8360 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20  Pager *pPager = 
d8370 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 61  pPg->pPager;.  a
d8380 73 73 65 72 74 28 20 21 70 50 67 2d 3e 70 61 67  ssert( !pPg->pag
d8390 65 48 61 73 68 20 7c 7c 20 70 50 61 67 65 72 2d  eHash || pPager-
d83a0 3e 65 72 72 43 6f 64 65 20 7c 7c 20 4d 45 4d 44  >errCode || MEMD
d83b0 42 20 7c 7c 20 70 50 67 2d 3e 64 69 72 74 79 20  B || pPg->dirty 
d83c0 7c 7c 20 0a 20 20 20 20 20 20 70 50 67 2d 3e 70  || .      pPg->p
d83d0 61 67 65 48 61 73 68 3d 3d 70 61 67 65 72 5f 70  ageHash==pager_p
d83e0 61 67 65 68 61 73 68 28 70 50 67 29 20 29 3b 0a  agehash(pPg) );.
d83f0 7d 0a 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65  }..#else.#define
d8400 20 70 61 67 65 72 5f 64 61 74 61 68 61 73 68 28   pager_datahash(
d8410 58 2c 59 29 20 20 30 0a 23 64 65 66 69 6e 65 20  X,Y)  0.#define 
d8420 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 58  pager_pagehash(X
d8430 29 20 20 30 0a 23 64 65 66 69 6e 65 20 43 48 45  )  0.#define CHE
d8440 43 4b 5f 50 41 47 45 28 78 29 0a 23 65 6e 64 69  CK_PAGE(x).#endi
d8450 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68  f../*.** When th
d8460 69 73 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65  is is called the
d8470 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f   journal file fo
d8480 72 20 70 61 67 65 72 20 70 50 61 67 65 72 20 6d  r pager pPager m
d8490 75 73 74 20 62 65 20 6f 70 65 6e 2e 0a 2a 2a 20  ust be open..** 
d84a0 54 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  The master journ
d84b0 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20  al file name is 
d84c0 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 65 6e  read from the en
d84d0 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 61 6e  d of the file an
d84e0 64 20 0a 2a 2a 20 77 72 69 74 74 65 6e 20 69 6e  d .** written in
d84f0 74 6f 20 6d 65 6d 6f 72 79 20 73 75 70 70 6c 69  to memory suppli
d8500 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72  ed by the caller
d8510 2e 20 0a 2a 2a 0a 2a 2a 20 7a 4d 61 73 74 65 72  . .**.** zMaster
d8520 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 61   must point to a
d8530 20 62 75 66 66 65 72 20 6f 66 20 61 74 20 6c 65   buffer of at le
d8540 61 73 74 20 6e 4d 61 73 74 65 72 20 62 79 74 65  ast nMaster byte
d8550 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a  s allocated by.*
d8560 2a 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 54 68  * the caller. Th
d8570 69 73 20 73 68 6f 75 6c 64 20 62 65 20 73 71 6c  is should be sql
d8580 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e  ite3_vfs.mxPathn
d8590 61 6d 65 2b 31 20 28 74 6f 20 65 6e 73 75 72 65  ame+1 (to ensure
d85a0 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 65 6e 6f   there is.** eno
d85b0 75 67 68 20 73 70 61 63 65 20 74 6f 20 77 72 69  ugh space to wri
d85c0 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  te the master jo
d85d0 75 72 6e 61 6c 20 6e 61 6d 65 29 2e 20 49 66 20  urnal name). If 
d85e0 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
d85f0 61 6c 0a 2a 2a 20 6e 61 6d 65 20 69 6e 20 74 68  al.** name in th
d8600 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6c 6f 6e  e journal is lon
d8610 67 65 72 20 74 68 61 6e 20 6e 4d 61 73 74 65 72  ger than nMaster
d8620 20 62 79 74 65 73 20 28 69 6e 63 6c 75 64 69 6e   bytes (includin
d8630 67 20 61 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69  g a.** nul-termi
d8640 6e 61 74 6f 72 29 2c 20 74 68 65 6e 20 74 68 69  nator), then thi
d8650 73 20 69 73 20 68 61 6e 64 6c 65 64 20 61 73 20  s is handled as 
d8660 69 66 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75  if no master jou
d8670 72 6e 61 6c 20 6e 61 6d 65 0a 2a 2a 20 77 65 72  rnal name.** wer
d8680 65 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65  e present in the
d8690 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20   journal..**.** 
d86a0 49 66 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75  If no master jou
d86b0 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69  rnal file name i
d86c0 73 20 70 72 65 73 65 6e 74 20 7a 4d 61 73 74 65  s present zMaste
d86d0 72 5b 30 5d 20 69 73 20 73 65 74 20 74 6f 20 30  r[0] is set to 0
d86e0 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f   and.** SQLITE_O
d86f0 4b 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73  K returned..*/.s
d8700 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 4d 61  tatic int readMa
d8710 73 74 65 72 4a 6f 75 72 6e 61 6c 28 73 71 6c 69  sterJournal(sqli
d8720 74 65 33 5f 66 69 6c 65 20 2a 70 4a 72 6e 6c 2c  te3_file *pJrnl,
d8730 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c 20   char *zMaster, 
d8740 69 6e 74 20 6e 4d 61 73 74 65 72 29 7b 0a 20 20  int nMaster){.  
d8750 69 6e 74 20 72 63 3b 0a 20 20 75 33 32 20 6c 65  int rc;.  u32 le
d8760 6e 3b 0a 20 20 69 36 34 20 73 7a 4a 3b 0a 20 20  n;.  i64 szJ;.  
d8770 75 33 32 20 63 6b 73 75 6d 3b 0a 20 20 69 6e 74  u32 cksum;.  int
d8780 20 69 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   i;.  unsigned c
d8790 68 61 72 20 61 4d 61 67 69 63 5b 38 5d 3b 20 2f  har aMagic[8]; /
d87a0 2a 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 6f  * A buffer to ho
d87b0 6c 64 20 74 68 65 20 6d 61 67 69 63 20 68 65 61  ld the magic hea
d87c0 64 65 72 20 2a 2f 0a 0a 20 20 7a 4d 61 73 74 65  der */..  zMaste
d87d0 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20  r[0] = '\0';..  
d87e0 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69  rc = sqlite3OsFi
d87f0 6c 65 53 69 7a 65 28 70 4a 72 6e 6c 2c 20 26 73  leSize(pJrnl, &s
d8800 7a 4a 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  zJ);.  if( rc!=S
d8810 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 7a 4a 3c  QLITE_OK || szJ<
d8820 31 36 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  16 ) return rc;.
d8830 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 69  .  rc = read32bi
d8840 74 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2d 31 36  ts(pJrnl, szJ-16
d8850 2c 20 26 6c 65 6e 29 3b 0a 20 20 69 66 28 20 72  , &len);.  if( r
d8860 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
d8870 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 69 66 28  eturn rc;..  if(
d8880 20 6c 65 6e 3e 3d 6e 4d 61 73 74 65 72 20 29 7b   len>=nMaster ){
d8890 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
d88a0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 72 63  TE_OK;.  }..  rc
d88b0 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 4a   = read32bits(pJ
d88c0 72 6e 6c 2c 20 73 7a 4a 2d 31 32 2c 20 26 63 6b  rnl, szJ-12, &ck
d88d0 73 75 6d 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  sum);.  if( rc!=
d88e0 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
d88f0 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73  rn rc;..  rc = s
d8900 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 4a 72  qlite3OsRead(pJr
d8910 6e 6c 2c 20 61 4d 61 67 69 63 2c 20 38 2c 20 73  nl, aMagic, 8, s
d8920 7a 4a 2d 38 29 3b 0a 20 20 69 66 28 20 72 63 21  zJ-8);.  if( rc!
d8930 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6d 65  =SQLITE_OK || me
d8940 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f  mcmp(aMagic, aJo
d8950 75 72 6e 61 6c 4d 61 67 69 63 2c 20 38 29 20 29  urnalMagic, 8) )
d8960 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
d8970 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61  c = sqlite3OsRea
d8980 64 28 70 4a 72 6e 6c 2c 20 7a 4d 61 73 74 65 72  d(pJrnl, zMaster
d8990 2c 20 6c 65 6e 2c 20 73 7a 4a 2d 31 36 2d 6c 65  , len, szJ-16-le
d89a0 6e 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  n);.  if( rc!=SQ
d89b0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
d89c0 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
d89d0 7a 4d 61 73 74 65 72 5b 6c 65 6e 5d 20 3d 20 27  zMaster[len] = '
d89e0 5c 30 27 3b 0a 0a 20 20 2f 2a 20 53 65 65 20 69  \0';..  /* See i
d89f0 66 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 6d  f the checksum m
d8a00 61 74 63 68 65 73 20 74 68 65 20 6d 61 73 74 65  atches the maste
d8a10 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a  r journal name *
d8a20 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6c  /.  for(i=0; i<l
d8a30 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 6b  en; i++){.    ck
d8a40 73 75 6d 20 2d 3d 20 7a 4d 61 73 74 65 72 5b 69  sum -= zMaster[i
d8a50 5d 3b 0a 20 20 20 7d 0a 20 20 69 66 28 20 63 6b  ];.   }.  if( ck
d8a60 73 75 6d 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  sum ){.    /* If
d8a70 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 64 6f   the checksum do
d8a80 65 73 6e 27 74 20 61 64 64 20 75 70 2c 20 74 68  esn't add up, th
d8a90 65 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f  en one or more o
d8aa0 66 20 74 68 65 20 64 69 73 6b 20 73 65 63 74 6f  f the disk secto
d8ab0 72 73 0a 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69  rs.    ** contai
d8ac0 6e 69 6e 67 20 74 68 65 20 6d 61 73 74 65 72 20  ning the master 
d8ad0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65  journal filename
d8ae0 20 69 73 20 63 6f 72 72 75 70 74 65 64 2e 20 54   is corrupted. T
d8af0 68 69 73 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a  his means.    **
d8b00 20 64 65 66 69 6e 69 74 65 6c 79 20 72 6f 6c 6c   definitely roll
d8b10 20 62 61 63 6b 2c 20 73 6f 20 6a 75 73 74 20 72   back, so just r
d8b20 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
d8b30 61 6e 64 20 72 65 70 6f 72 74 20 61 20 28 6e 75  and report a (nu
d8b40 6c 29 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65 72  l).    ** master
d8b50 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d  -journal filenam
d8b60 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 4d  e..    */.    zM
d8b70 61 73 74 65 72 5b 30 5d 20 3d 20 27 5c 30 27 3b  aster[0] = '\0';
d8b80 0a 20 20 7d 0a 20 20 20 0a 20 20 72 65 74 75 72  .  }.   .  retur
d8b90 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
d8ba0 2f 2a 0a 2a 2a 20 53 65 65 6b 20 74 68 65 20 6a  /*.** Seek the j
d8bb0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63  ournal file desc
d8bc0 72 69 70 74 6f 72 20 74 6f 20 74 68 65 20 6e 65  riptor to the ne
d8bd0 78 74 20 73 65 63 74 6f 72 20 62 6f 75 6e 64 61  xt sector bounda
d8be0 72 79 20 77 68 65 72 65 20 61 0a 2a 2a 20 6a 6f  ry where a.** jo
d8bf0 75 72 6e 61 6c 20 68 65 61 64 65 72 20 6d 61 79  urnal header may
d8c00 20 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 74   be read or writ
d8c10 74 65 6e 2e 20 50 61 67 65 72 2e 6a 6f 75 72 6e  ten. Pager.journ
d8c20 61 6c 4f 66 66 20 69 73 20 75 70 64 61 74 65 64  alOff is updated
d8c30 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 6e 65 77   with.** the new
d8c40 20 73 65 65 6b 20 6f 66 66 73 65 74 2e 0a 2a 2a   seek offset..**
d8c50 0a 2a 2a 20 69 2e 65 20 66 6f 72 20 61 20 73 65  .** i.e for a se
d8c60 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 35 31 32  ctor size of 512
d8c70 3a 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 20 4f 66  :.**.** Input Of
d8c80 66 73 65 74 20 20 20 20 20 20 20 20 20 20 20 20  fset            
d8c90 20 20 4f 75 74 70 75 74 20 4f 66 66 73 65 74 0a    Output Offset.
d8ca0 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ** -------------
d8cb0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
d8cc0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 30 20  ----------.** 0 
d8cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d8ce0 20 20 20 20 20 20 20 20 30 0a 2a 2a 20 35 31 32          0.** 512
d8cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d8d00 20 20 20 20 20 20 20 35 31 32 0a 2a 2a 20 31 30         512.** 10
d8d10 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
d8d20 20 20 20 20 20 20 20 20 35 31 32 0a 2a 2a 20 32          512.** 2
d8d30 30 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20  000             
d8d40 20 20 20 20 20 20 20 20 20 32 30 34 38 0a 2a 2a           2048.**
d8d50 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64   .*/.static void
d8d60 20 73 65 65 6b 4a 6f 75 72 6e 61 6c 48 64 72 28   seekJournalHdr(
d8d70 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
d8d80 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 30    i64 offset = 0
d8d90 3b 0a 20 20 69 36 34 20 63 20 3d 20 70 50 61 67  ;.  i64 c = pPag
d8da0 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a  er->journalOff;.
d8db0 20 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 6f    if( c ){.    o
d8dc0 66 66 73 65 74 20 3d 20 28 28 63 2d 31 29 2f 4a  ffset = ((c-1)/J
d8dd0 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
d8de0 61 67 65 72 29 20 2b 20 31 29 20 2a 20 4a 4f 55  ager) + 1) * JOU
d8df0 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
d8e00 65 72 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  er);.  }.  asser
d8e10 74 28 20 6f 66 66 73 65 74 25 4a 4f 55 52 4e 41  t( offset%JOURNA
d8e20 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29  L_HDR_SZ(pPager)
d8e30 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
d8e40 20 6f 66 66 73 65 74 3e 3d 63 20 29 3b 0a 20 20   offset>=c );.  
d8e50 61 73 73 65 72 74 28 20 28 6f 66 66 73 65 74 2d  assert( (offset-
d8e60 63 29 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53  c)<JOURNAL_HDR_S
d8e70 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 70  Z(pPager) );.  p
d8e80 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
d8e90 66 20 3d 20 6f 66 66 73 65 74 3b 0a 7d 0a 0a 2f  f = offset;.}../
d8ea0 2a 0a 2a 2a 20 57 72 69 74 65 20 7a 65 72 6f 73  *.** Write zeros
d8eb0 20 6f 76 65 72 20 74 68 65 20 68 65 61 64 65 72   over the header
d8ec0 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
d8ed0 66 69 6c 65 2e 20 20 54 68 69 73 20 68 61 73 20  file.  This has 
d8ee0 74 68 65 0a 2a 2a 20 65 66 66 65 63 74 20 6f 66  the.** effect of
d8ef0 20 69 6e 76 61 6c 69 64 61 74 69 6e 67 20 74 68   invalidating th
d8f00 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61  e journal file a
d8f10 6e 64 20 63 6f 6d 6d 69 74 74 69 6e 67 20 74 68  nd committing th
d8f20 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e  e.** transaction
d8f30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
d8f40 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 28 50  zeroJournalHdr(P
d8f50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e  ager *pPager, in
d8f60 74 20 64 6f 54 72 75 6e 63 61 74 65 29 7b 0a 20  t doTruncate){. 
d8f70 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
d8f80 5f 4f 4b 3b 0a 20 20 73 74 61 74 69 63 20 63 6f  _OK;.  static co
d8f90 6e 73 74 20 63 68 61 72 20 7a 65 72 6f 48 64 72  nst char zeroHdr
d8fa0 5b 32 38 5d 3b 0a 0a 20 20 69 66 28 20 70 50 61  [28];..  if( pPa
d8fb0 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
d8fc0 29 7b 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28  ){.    IOTRACE((
d8fd0 22 4a 5a 45 52 4f 48 44 52 20 25 70 5c 6e 22 2c  "JZEROHDR %p\n",
d8fe0 20 70 50 61 67 65 72 29 29 0a 20 20 20 20 69 66   pPager)).    if
d8ff0 28 20 64 6f 54 72 75 6e 63 61 74 65 20 29 7b 0a  ( doTruncate ){.
d9000 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
d9010 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61  e3OsTruncate(pPa
d9020 67 65 72 2d 3e 6a 66 64 2c 20 30 29 3b 0a 20 20  ger->jfd, 0);.  
d9030 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
d9040 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69  c = sqlite3OsWri
d9050 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  te(pPager->jfd, 
d9060 7a 65 72 6f 48 64 72 2c 20 73 69 7a 65 6f 66 28  zeroHdr, sizeof(
d9070 7a 65 72 6f 48 64 72 29 2c 20 30 29 3b 0a 20 20  zeroHdr), 0);.  
d9080 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d    }.    if( rc==
d9090 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
d90a0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
d90b0 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66  sSync(pPager->jf
d90c0 64 2c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44  d, SQLITE_SYNC_D
d90d0 41 54 41 4f 4e 4c 59 7c 70 50 61 67 65 72 2d 3e  ATAONLY|pPager->
d90e0 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 20 20  sync_flags);.   
d90f0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
d9100 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  rc;.}../*.** The
d9110 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75   journal file mu
d9120 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65 6e 20  st be open when 
d9130 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
d9140 63 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72 6e 61  called. A journa
d9150 6c 0a 2a 2a 20 68 65 61 64 65 72 20 28 4a 4f 55  l.** header (JOU
d9160 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65  RNAL_HDR_SZ byte
d9170 73 29 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e  s) is written in
d9180 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  to the journal f
d9190 69 6c 65 20 61 74 20 74 68 65 0a 2a 2a 20 63 75  ile at the.** cu
d91a0 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e 0a  rrent location..
d91b0 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74  **.** The format
d91c0 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c   for the journal
d91d0 20 68 65 61 64 65 72 20 69 73 20 61 73 20 66 6f   header is as fo
d91e0 6c 6c 6f 77 73 3a 0a 2a 2a 20 2d 20 38 20 62 79  llows:.** - 8 by
d91f0 74 65 73 3a 20 4d 61 67 69 63 20 69 64 65 6e 74  tes: Magic ident
d9200 69 66 79 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 66  ifying journal f
d9210 6f 72 6d 61 74 2e 0a 2a 2a 20 2d 20 34 20 62 79  ormat..** - 4 by
d9220 74 65 73 3a 20 4e 75 6d 62 65 72 20 6f 66 20 72  tes: Number of r
d9230 65 63 6f 72 64 73 20 69 6e 20 6a 6f 75 72 6e 61  ecords in journa
d9240 6c 2c 20 6f 72 20 2d 31 20 6e 6f 2d 73 79 6e 63  l, or -1 no-sync
d9250 20 6d 6f 64 65 20 69 73 20 6f 6e 2e 0a 2a 2a 20   mode is on..** 
d9260 2d 20 34 20 62 79 74 65 73 3a 20 52 61 6e 64 6f  - 4 bytes: Rando
d9270 6d 20 6e 75 6d 62 65 72 20 75 73 65 64 20 66 6f  m number used fo
d9280 72 20 70 61 67 65 20 68 61 73 68 2e 0a 2a 2a 20  r page hash..** 
d9290 2d 20 34 20 62 79 74 65 73 3a 20 49 6e 69 74 69  - 4 bytes: Initi
d92a0 61 6c 20 64 61 74 61 62 61 73 65 20 70 61 67 65  al database page
d92b0 20 63 6f 75 6e 74 2e 0a 2a 2a 20 2d 20 34 20 62   count..** - 4 b
d92c0 79 74 65 73 3a 20 53 65 63 74 6f 72 20 73 69 7a  ytes: Sector siz
d92d0 65 20 75 73 65 64 20 62 79 20 74 68 65 20 70 72  e used by the pr
d92e0 6f 63 65 73 73 20 74 68 61 74 20 77 72 6f 74 65  ocess that wrote
d92f0 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e 0a 2a   this journal..*
d9300 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 44 61 74  * - 4 bytes: Dat
d9310 61 62 61 73 65 20 70 61 67 65 20 73 69 7a 65 2e  abase page size.
d9320 0a 2a 2a 20 0a 2a 2a 20 46 6f 6c 6c 6f 77 65 64  .** .** Followed
d9330 20 62 79 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52   by (JOURNAL_HDR
d9340 5f 53 5a 20 2d 20 32 38 29 20 62 79 74 65 73 20  _SZ - 28) bytes 
d9350 6f 66 20 75 6e 75 73 65 64 20 73 70 61 63 65 2e  of unused space.
d9360 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
d9370 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 50  riteJournalHdr(P
d9380 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
d9390 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
d93a0 5f 4f 4b 3b 0a 20 20 63 68 61 72 20 2a 7a 48 65  _OK;.  char *zHe
d93b0 61 64 65 72 20 3d 20 70 50 61 67 65 72 2d 3e 70  ader = pPager->p
d93c0 54 6d 70 53 70 61 63 65 3b 0a 20 20 69 6e 74 20  TmpSpace;.  int 
d93d0 6e 48 65 61 64 65 72 20 3d 20 70 50 61 67 65 72  nHeader = pPager
d93e0 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 69 6e  ->pageSize;.  in
d93f0 74 20 6e 57 72 69 74 65 3b 0a 0a 20 20 69 66 28  t nWrite;..  if(
d9400 20 6e 48 65 61 64 65 72 3e 4a 4f 55 52 4e 41 4c   nHeader>JOURNAL
d9410 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20  _HDR_SZ(pPager) 
d9420 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20 3d  ){.    nHeader =
d9430 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28   JOURNAL_HDR_SZ(
d9440 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20  pPager);.  }..  
d9450 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 6d 74  if( pPager->stmt
d9460 48 64 72 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20  HdrOff==0 ){.   
d9470 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 48 64 72   pPager->stmtHdr
d9480 4f 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f  Off = pPager->jo
d9490 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 7d 0a 0a 20  urnalOff;.  }.. 
d94a0 20 73 65 65 6b 4a 6f 75 72 6e 61 6c 48 64 72 28   seekJournalHdr(
d94b0 70 50 61 67 65 72 29 3b 0a 20 20 70 50 61 67 65  pPager);.  pPage
d94c0 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20  r->journalHdr = 
d94d0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
d94e0 66 66 3b 0a 0a 20 20 6d 65 6d 63 70 79 28 7a 48  ff;..  memcpy(zH
d94f0 65 61 64 65 72 2c 20 61 4a 6f 75 72 6e 61 6c 4d  eader, aJournalM
d9500 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f  agic, sizeof(aJo
d9510 75 72 6e 61 6c 4d 61 67 69 63 29 29 3b 0a 0a 20  urnalMagic));.. 
d9520 20 2f 2a 20 0a 20 20 2a 2a 20 57 72 69 74 65 20   /* .  ** Write 
d9530 74 68 65 20 6e 52 65 63 20 46 69 65 6c 64 20 2d  the nRec Field -
d9540 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
d9550 61 67 65 20 72 65 63 6f 72 64 73 20 74 68 61 74  age records that
d9560 20 66 6f 6c 6c 6f 77 20 74 68 69 73 0a 20 20 2a   follow this.  *
d9570 2a 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  * journal header
d9580 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 7a 65 72 6f  . Normally, zero
d9590 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74   is written to t
d95a0 68 69 73 20 76 61 6c 75 65 20 61 74 20 74 68 69  his value at thi
d95b0 73 20 74 69 6d 65 2e 0a 20 20 2a 2a 20 41 66 74  s time..  ** Aft
d95c0 65 72 20 74 68 65 20 72 65 63 6f 72 64 73 20 61  er the records a
d95d0 72 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  re added to the 
d95e0 6a 6f 75 72 6e 61 6c 20 28 61 6e 64 20 74 68 65  journal (and the
d95f0 20 6a 6f 75 72 6e 61 6c 20 73 79 6e 63 65 64 2c   journal synced,
d9600 20 0a 20 20 2a 2a 20 69 66 20 69 6e 20 66 75 6c   .  ** if in ful
d9610 6c 2d 73 79 6e 63 20 6d 6f 64 65 29 2c 20 74 68  l-sync mode), th
d9620 65 20 7a 65 72 6f 20 69 73 20 6f 76 65 72 77 72  e zero is overwr
d9630 69 74 74 65 6e 20 77 69 74 68 20 74 68 65 20 74  itten with the t
d9640 72 75 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20  rue number.  ** 
d9650 6f 66 20 72 65 63 6f 72 64 73 20 28 73 65 65 20  of records (see 
d9660 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 29 29 2e 0a  syncJournal())..
d9670 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 66 61 73 74    **.  ** A fast
d9680 65 72 20 61 6c 74 65 72 6e 61 74 69 76 65 20 69  er alternative i
d9690 73 20 74 6f 20 77 72 69 74 65 20 30 78 46 46 46  s to write 0xFFF
d96a0 46 46 46 46 46 20 74 6f 20 74 68 65 20 6e 52 65  FFFFF to the nRe
d96b0 63 20 66 69 65 6c 64 2e 20 57 68 65 6e 0a 20 20  c field. When.  
d96c0 2a 2a 20 72 65 61 64 69 6e 67 20 74 68 65 20 6a  ** reading the j
d96d0 6f 75 72 6e 61 6c 20 74 68 69 73 20 76 61 6c 75  ournal this valu
d96e0 65 20 74 65 6c 6c 73 20 53 51 4c 69 74 65 20 74  e tells SQLite t
d96f0 6f 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68  o assume that th
d9700 65 0a 20 20 2a 2a 20 72 65 73 74 20 6f 66 20 74  e.  ** rest of t
d9710 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
d9720 63 6f 6e 74 61 69 6e 73 20 76 61 6c 69 64 20 70  contains valid p
d9730 61 67 65 20 72 65 63 6f 72 64 73 2e 20 54 68 69  age records. Thi
d9740 73 20 61 73 73 75 6d 70 74 69 6f 6e 0a 20 20 2a  s assumption.  *
d9750 2a 20 69 73 20 64 61 6e 67 65 72 6f 75 73 2c 20  * is dangerous, 
d9760 61 73 20 69 66 20 61 20 66 61 69 6c 75 72 65 20  as if a failure 
d9770 6f 63 63 75 72 65 64 20 77 68 69 6c 73 74 20 77  occured whilst w
d9780 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f  riting to the jo
d9790 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20  urnal.  ** file 
d97a0 69 74 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 73  it may contain s
d97b0 6f 6d 65 20 67 61 72 62 61 67 65 20 64 61 74 61  ome garbage data
d97c0 2e 20 54 68 65 72 65 20 61 72 65 20 74 77 6f 20  . There are two 
d97d0 73 63 65 6e 61 72 69 6f 73 0a 20 20 2a 2a 20 77  scenarios.  ** w
d97e0 68 65 72 65 20 74 68 69 73 20 72 69 73 6b 20 63  here this risk c
d97f0 61 6e 20 62 65 20 69 67 6e 6f 72 65 64 3a 0a 20  an be ignored:. 
d9800 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65   **.  **   * Whe
d9810 6e 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69  n the pager is i
d9820 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e 20  n no-sync mode. 
d9830 43 6f 72 72 75 70 74 69 6f 6e 20 63 61 6e 20 66  Corruption can f
d9840 6f 6c 6c 6f 77 20 61 0a 20 20 2a 2a 20 20 20 20  ollow a.  **    
d9850 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 69   power failure i
d9860 6e 20 74 68 69 73 20 63 61 73 65 20 61 6e 79 77  n this case anyw
d9870 61 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  ay..  **.  **   
d9880 2a 20 57 68 65 6e 20 74 68 65 20 53 51 4c 49 54  * When the SQLIT
d9890 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50  E_IOCAP_SAFE_APP
d98a0 45 4e 44 20 66 6c 61 67 20 69 73 20 73 65 74 2e  END flag is set.
d98b0 20 54 68 69 73 20 67 75 61 72 61 6e 74 65 65 73   This guarantees
d98c0 0a 20 20 2a 2a 20 20 20 20 20 74 68 61 74 20 67  .  **     that g
d98d0 61 72 62 61 67 65 20 64 61 74 61 20 69 73 20 6e  arbage data is n
d98e0 65 76 65 72 20 61 70 70 65 6e 64 65 64 20 74 6f  ever appended to
d98f0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
d9900 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  e..  */.  assert
d9910 28 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65  (pPager->fd->pMe
d9920 74 68 6f 64 73 7c 7c 70 50 61 67 65 72 2d 3e 6e  thods||pPager->n
d9930 6f 53 79 6e 63 29 3b 0a 20 20 69 66 28 20 28 70  oSync);.  if( (p
d9940 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 29 20 0a  Pager->noSync) .
d9950 20 20 20 7c 7c 20 28 73 71 6c 69 74 65 33 4f 73     || (sqlite3Os
d9960 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
d9970 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64  stics(pPager->fd
d9980 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53  )&SQLITE_IOCAP_S
d9990 41 46 45 5f 41 50 50 45 4e 44 29 20 0a 20 20 29  AFE_APPEND) .  )
d99a0 7b 0a 20 20 20 20 70 75 74 33 32 62 69 74 73 28  {.    put32bits(
d99b0 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28  &zHeader[sizeof(
d99c0 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c  aJournalMagic)],
d99d0 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20   0xffffffff);.  
d99e0 7d 65 6c 73 65 7b 0a 20 20 20 20 70 75 74 33 32  }else{.    put32
d99f0 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69  bits(&zHeader[si
d9a00 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67  zeof(aJournalMag
d9a10 69 63 29 5d 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20  ic)], 0);.  }.. 
d9a20 20 2f 2a 20 54 68 65 20 72 61 6e 64 6f 6d 20 63   /* The random c
d9a30 68 65 63 6b 2d 68 61 73 68 20 69 6e 69 74 69 61  heck-hash initia
d9a40 6c 69 73 65 72 20 2a 2f 20 0a 20 20 73 71 6c 69  liser */ .  sqli
d9a50 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73  te3_randomness(s
d9a60 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 63 6b  izeof(pPager->ck
d9a70 73 75 6d 49 6e 69 74 29 2c 20 26 70 50 61 67 65  sumInit), &pPage
d9a80 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a 20  r->cksumInit);. 
d9a90 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61   put32bits(&zHea
d9aa0 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72  der[sizeof(aJour
d9ab0 6e 61 6c 4d 61 67 69 63 29 2b 34 5d 2c 20 70 50  nalMagic)+4], pP
d9ac0 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29  ager->cksumInit)
d9ad0 3b 0a 20 20 2f 2a 20 54 68 65 20 69 6e 69 74 69  ;.  /* The initi
d9ae0 61 6c 20 64 61 74 61 62 61 73 65 20 73 69 7a 65  al database size
d9af0 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28   */.  put32bits(
d9b00 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28  &zHeader[sizeof(
d9b10 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 38  aJournalMagic)+8
d9b20 5d 2c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  ], pPager->dbSiz
d9b30 65 29 3b 0a 20 20 2f 2a 20 54 68 65 20 61 73 73  e);.  /* The ass
d9b40 75 6d 65 64 20 73 65 63 74 6f 72 20 73 69 7a 65  umed sector size
d9b50 20 66 6f 72 20 74 68 69 73 20 70 72 6f 63 65 73   for this proces
d9b60 73 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73  s */.  put32bits
d9b70 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66  (&zHeader[sizeof
d9b80 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b  (aJournalMagic)+
d9b90 31 32 5d 2c 20 70 50 61 67 65 72 2d 3e 73 65 63  12], pPager->sec
d9ba0 74 6f 72 53 69 7a 65 29 3b 0a 20 20 69 66 28 20  torSize);.  if( 
d9bb0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48  pPager->journalH
d9bc0 64 72 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  dr==0 ){.    /* 
d9bd0 54 68 65 20 70 61 67 65 20 73 69 7a 65 20 2a 2f  The page size */
d9be0 0a 20 20 20 20 70 75 74 33 32 62 69 74 73 28 26  .    put32bits(&
d9bf0 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61  zHeader[sizeof(a
d9c00 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31 36  JournalMagic)+16
d9c10 5d 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  ], pPager->pageS
d9c20 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72  ize);.  }..  for
d9c30 28 6e 57 72 69 74 65 3d 30 3b 20 72 63 3d 3d 53  (nWrite=0; rc==S
d9c40 51 4c 49 54 45 5f 4f 4b 26 26 6e 57 72 69 74 65  QLITE_OK&&nWrite
d9c50 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28  <JOURNAL_HDR_SZ(
d9c60 70 50 61 67 65 72 29 3b 20 6e 57 72 69 74 65 2b  pPager); nWrite+
d9c70 3d 6e 48 65 61 64 65 72 29 7b 0a 20 20 20 20 49  =nHeader){.    I
d9c80 4f 54 52 41 43 45 28 28 22 4a 48 44 52 20 25 70  OTRACE(("JHDR %p
d9c90 20 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61   %lld %d\n", pPa
d9ca0 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75  ger, pPager->jou
d9cb0 72 6e 61 6c 48 64 72 2c 20 6e 48 65 61 64 65 72  rnalHdr, nHeader
d9cc0 29 29 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  )).    rc = sqli
d9cd0 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65  te3OsWrite(pPage
d9ce0 72 2d 3e 6a 66 64 2c 20 7a 48 65 61 64 65 72 2c  r->jfd, zHeader,
d9cf0 20 6e 48 65 61 64 65 72 2c 20 70 50 61 67 65 72   nHeader, pPager
d9d00 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a 20  ->journalOff);. 
d9d10 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
d9d20 61 6c 4f 66 66 20 2b 3d 20 6e 48 65 61 64 65 72  alOff += nHeader
d9d30 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
d9d40 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  rc;.}../*.** The
d9d50 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75   journal file mu
d9d60 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65 6e 20  st be open when 
d9d70 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2e 20  this is called. 
d9d80 41 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  A journal header
d9d90 20 66 69 6c 65 0a 2a 2a 20 28 4a 4f 55 52 4e 41   file.** (JOURNA
d9da0 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65 73 29 20  L_HDR_SZ bytes) 
d9db0 69 73 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65  is read from the
d9dc0 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f   current locatio
d9dd0 6e 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  n in the journal
d9de0 0a 2a 2a 20 66 69 6c 65 2e 20 53 65 65 20 63 6f  .** file. See co
d9df0 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 66 75 6e  mments above fun
d9e00 63 74 69 6f 6e 20 77 72 69 74 65 4a 6f 75 72 6e  ction writeJourn
d9e10 61 6c 48 64 72 28 29 20 66 6f 72 20 61 20 64 65  alHdr() for a de
d9e20 73 63 72 69 70 74 69 6f 6e 20 6f 66 0a 2a 2a 20  scription of.** 
d9e30 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  the journal head
d9e40 65 72 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a  er format..**.**
d9e50 20 49 66 20 74 68 65 20 68 65 61 64 65 72 20 69   If the header i
d9e60 73 20 72 65 61 64 20 73 75 63 63 65 73 73 66 75  s read successfu
d9e70 6c 6c 79 2c 20 2a 6e 52 65 63 20 69 73 20 73 65  lly, *nRec is se
d9e80 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20  t to the number 
d9e90 6f 66 0a 2a 2a 20 70 61 67 65 20 72 65 63 6f 72  of.** page recor
d9ea0 64 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69  ds following thi
d9eb0 73 20 68 65 61 64 65 72 20 61 6e 64 20 2a 64 62  s header and *db
d9ec0 53 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20 74  Size is set to t
d9ed0 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 0a 2a  he size of the.*
d9ee0 2a 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72  * database befor
d9ef0 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  e the transactio
d9f00 6e 20 62 65 67 61 6e 2c 20 69 6e 20 70 61 67 65  n began, in page
d9f10 73 2e 20 41 6c 73 6f 2c 20 70 50 61 67 65 72 2d  s. Also, pPager-
d9f20 3e 63 6b 73 75 6d 49 6e 69 74 0a 2a 2a 20 69 73  >cksumInit.** is
d9f30 20 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75   set to the valu
d9f40 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20  e read from the 
d9f50 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 20  journal header. 
d9f60 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
d9f70 75 72 6e 65 64 0a 2a 2a 20 69 6e 20 74 68 69 73  urned.** in this
d9f80 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   case..**.** If 
d9f90 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  the journal head
d9fa0 65 72 20 66 69 6c 65 20 61 70 70 65 61 72 73 20  er file appears 
d9fb0 74 6f 20 62 65 20 63 6f 72 72 75 70 74 65 64 2c  to be corrupted,
d9fc0 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 0a   SQLITE_DONE is.
d9fd0 2a 2a 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  ** returned and 
d9fe0 2a 6e 52 65 63 20 61 6e 64 20 2a 64 62 53 69 7a  *nRec and *dbSiz
d9ff0 65 20 61 72 65 20 6e 6f 74 20 73 65 74 2e 20 20  e are not set.  
da000 49 66 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53  If JOURNAL_HDR_S
da010 5a 20 62 79 74 65 73 0a 2a 2a 20 63 61 6e 6e 6f  Z bytes.** canno
da020 74 20 62 65 20 72 65 61 64 20 66 72 6f 6d 20 74  t be read from t
da030 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
da040 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  an error code is
da050 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74   returned..*/.st
da060 61 74 69 63 20 69 6e 74 20 72 65 61 64 4a 6f 75  atic int readJou
da070 72 6e 61 6c 48 64 72 28 0a 20 20 50 61 67 65 72  rnalHdr(.  Pager
da080 20 2a 70 50 61 67 65 72 2c 20 0a 20 20 69 36 34   *pPager, .  i64
da090 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 2c 0a 20 20   journalSize,.  
da0a0 75 33 32 20 2a 70 4e 52 65 63 2c 20 0a 20 20 75  u32 *pNRec, .  u
da0b0 33 32 20 2a 70 44 62 53 69 7a 65 0a 29 7b 0a 20  32 *pDbSize.){. 
da0c0 20 69 6e 74 20 72 63 3b 0a 20 20 75 6e 73 69 67   int rc;.  unsig
da0d0 6e 65 64 20 63 68 61 72 20 61 4d 61 67 69 63 5b  ned char aMagic[
da0e0 38 5d 3b 20 2f 2a 20 41 20 62 75 66 66 65 72 20  8]; /* A buffer 
da0f0 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 61 67 69  to hold the magi
da100 63 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 36  c header */.  i6
da110 34 20 6a 72 6e 6c 4f 66 66 3b 0a 20 20 69 6e 74  4 jrnlOff;.  int
da120 20 69 50 61 67 65 53 69 7a 65 3b 0a 0a 20 20 73   iPageSize;..  s
da130 65 65 6b 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50  eekJournalHdr(pP
da140 61 67 65 72 29 3b 0a 20 20 69 66 28 20 70 50 61  ager);.  if( pPa
da150 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2b  ger->journalOff+
da160 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70  JOURNAL_HDR_SZ(p
da170 50 61 67 65 72 29 20 3e 20 6a 6f 75 72 6e 61 6c  Pager) > journal
da180 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 65 74 75  Size ){.    retu
da190 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a  rn SQLITE_DONE;.
da1a0 20 20 7d 0a 20 20 6a 72 6e 6c 4f 66 66 20 3d 20    }.  jrnlOff = 
da1b0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
da1c0 66 66 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  ff;..  rc = sqli
da1d0 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72  te3OsRead(pPager
da1e0 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 63 2c 20 73  ->jfd, aMagic, s
da1f0 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 2c 20 6a  izeof(aMagic), j
da200 72 6e 6c 4f 66 66 29 3b 0a 20 20 69 66 28 20 72  rnlOff);.  if( r
da210 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
da220 20 6a 72 6e 6c 4f 66 66 20 2b 3d 20 73 69 7a 65   jrnlOff += size
da230 6f 66 28 61 4d 61 67 69 63 29 3b 0a 0a 20 20 69  of(aMagic);..  i
da240 66 28 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63  f( memcmp(aMagic
da250 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c  , aJournalMagic,
da260 20 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 29   sizeof(aMagic))
da270 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  !=0 ){.    retur
da280 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  n SQLITE_DONE;. 
da290 20 7d 0a 0a 20 20 72 63 20 3d 20 72 65 61 64 33   }..  rc = read3
da2a0 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66  2bits(pPager->jf
da2b0 64 2c 20 6a 72 6e 6c 4f 66 66 2c 20 70 4e 52 65  d, jrnlOff, pNRe
da2c0 63 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  c);.  if( rc ) r
da2d0 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
da2e0 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 50 61  = read32bits(pPa
da2f0 67 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f 66  ger->jfd, jrnlOf
da300 66 2b 34 2c 20 26 70 50 61 67 65 72 2d 3e 63 6b  f+4, &pPager->ck
da310 73 75 6d 49 6e 69 74 29 3b 0a 20 20 69 66 28 20  sumInit);.  if( 
da320 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
da330 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 69  .  rc = read32bi
da340 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  ts(pPager->jfd, 
da350 6a 72 6e 6c 4f 66 66 2b 38 2c 20 70 44 62 53 69  jrnlOff+8, pDbSi
da360 7a 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20  ze);.  if( rc ) 
da370 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
da380 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 50   = read32bits(pP
da390 61 67 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f  ager->jfd, jrnlO
da3a0 66 66 2b 31 36 2c 20 28 75 33 32 20 2a 29 26 69  ff+16, (u32 *)&i
da3b0 50 61 67 65 53 69 7a 65 29 3b 0a 20 20 69 66 28  PageSize);.  if(
da3c0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 0a   rc==SQLITE_OK .
da3d0 20 20 20 26 26 20 69 50 61 67 65 53 69 7a 65 3e     && iPageSize>
da3e0 3d 35 31 32 20 0a 20 20 20 26 26 20 69 50 61 67  =512 .   && iPag
da3f0 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45 5f 4d 41  eSize<=SQLITE_MA
da400 58 5f 50 41 47 45 5f 53 49 5a 45 20 0a 20 20 20  X_PAGE_SIZE .   
da410 26 26 20 28 28 69 50 61 67 65 53 69 7a 65 2d 31  && ((iPageSize-1
da420 29 26 69 50 61 67 65 53 69 7a 65 29 3d 3d 30 20  )&iPageSize)==0 
da430 0a 20 20 29 7b 0a 20 20 20 20 75 31 36 20 70 61  .  ){.    u16 pa
da440 67 65 73 69 7a 65 20 3d 20 69 50 61 67 65 53 69  gesize = iPageSi
da450 7a 65 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  ze;.    rc = sql
da460 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67 65  ite3PagerSetPage
da470 73 69 7a 65 28 70 50 61 67 65 72 2c 20 26 70 61  size(pPager, &pa
da480 67 65 73 69 7a 65 29 3b 0a 20 20 7d 0a 20 20 69  gesize);.  }.  i
da490 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
da4a0 63 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20  c;..  /* Update 
da4b0 74 68 65 20 61 73 73 75 6d 65 64 20 73 65 63 74  the assumed sect
da4c0 6f 72 2d 73 69 7a 65 20 74 6f 20 6d 61 74 63 68  or-size to match
da4d0 20 74 68 65 20 76 61 6c 75 65 20 75 73 65 64 20   the value used 
da4e0 62 79 20 0a 20 20 2a 2a 20 74 68 65 20 70 72 6f  by .  ** the pro
da4f0 63 65 73 73 20 74 68 61 74 20 63 72 65 61 74 65  cess that create
da500 64 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e 20  d this journal. 
da510 49 66 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20  If this journal 
da520 77 61 73 0a 20 20 2a 2a 20 63 72 65 61 74 65 64  was.  ** created
da530 20 62 79 20 61 20 70 72 6f 63 65 73 73 20 6f 74   by a process ot
da540 68 65 72 20 74 68 61 6e 20 74 68 69 73 20 6f 6e  her than this on
da550 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  e, then this rou
da560 74 69 6e 65 0a 20 20 2a 2a 20 69 73 20 62 65 69  tine.  ** is bei
da570 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77  ng called from w
da580 69 74 68 69 6e 20 70 61 67 65 72 5f 70 6c 61 79  ithin pager_play
da590 62 61 63 6b 28 29 2e 20 54 68 65 20 6c 6f 63 61  back(). The loca
da5a0 6c 20 76 61 6c 75 65 0a 20 20 2a 2a 20 6f 66 20  l value.  ** of 
da5b0 50 61 67 65 72 2e 73 65 63 74 6f 72 53 69 7a 65  Pager.sectorSize
da5c0 20 69 73 20 72 65 73 74 6f 72 65 64 20 61 74 20   is restored at 
da5d0 74 68 65 20 65 6e 64 20 6f 66 20 74 68 61 74 20  the end of that 
da5e0 72 6f 75 74 69 6e 65 2e 0a 20 20 2a 2f 0a 20 20  routine..  */.  
da5f0 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28  rc = read32bits(
da600 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e  pPager->jfd, jrn
da610 6c 4f 66 66 2b 31 32 2c 20 28 75 33 32 20 2a 29  lOff+12, (u32 *)
da620 26 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53  &pPager->sectorS
da630 69 7a 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29  ize);.  if( rc )
da640 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 70   return rc;..  p
da650 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
da660 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52  f += JOURNAL_HDR
da670 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 72  _SZ(pPager);.  r
da680 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
da690 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  .}.../*.** Write
da6a0 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 6d 61   the supplied ma
da6b0 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d  ster journal nam
da6c0 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e  e into the journ
da6d0 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 67 65  al file for page
da6e0 72 0a 2a 2a 20 70 50 61 67 65 72 20 61 74 20 74  r.** pPager at t
da6f0 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74  he current locat
da700 69 6f 6e 2e 20 54 68 65 20 6d 61 73 74 65 72 20  ion. The master 
da710 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 6d 75 73  journal name mus
da720 74 20 62 65 20 74 68 65 20 6c 61 73 74 0a 2a 2a  t be the last.**
da730 20 74 68 69 6e 67 20 77 72 69 74 74 65 6e 20 74   thing written t
da740 6f 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  o a journal file
da750 2e 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69  . If the pager i
da760 73 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 6d  s in full-sync m
da770 6f 64 65 2c 20 74 68 65 0a 2a 2a 20 6a 6f 75 72  ode, the.** jour
da780 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70  nal file descrip
da790 74 6f 72 20 69 73 20 61 64 76 61 6e 63 65 64 20  tor is advanced 
da7a0 74 6f 20 74 68 65 20 6e 65 78 74 20 73 65 63 74  to the next sect
da7b0 6f 72 20 62 6f 75 6e 64 61 72 79 20 62 65 66 6f  or boundary befo
da7c0 72 65 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 69  re.** anything i
da7d0 73 20 77 72 69 74 74 65 6e 2e 20 54 68 65 20 66  s written. The f
da7e0 6f 72 6d 61 74 20 69 73 3a 0a 2a 2a 0a 2a 2a 20  ormat is:.**.** 
da7f0 2b 20 34 20 62 79 74 65 73 3a 20 50 41 47 45 52  + 4 bytes: PAGER
da800 5f 4d 4a 5f 50 47 4e 4f 2e 0a 2a 2a 20 2b 20 4e  _MJ_PGNO..** + N
da810 20 62 79 74 65 73 3a 20 6c 65 6e 67 74 68 20 6f   bytes: length o
da820 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  f master journal
da830 20 6e 61 6d 65 2e 0a 2a 2a 20 2b 20 34 20 62 79   name..** + 4 by
da840 74 65 73 3a 20 4e 0a 2a 2a 20 2b 20 34 20 62 79  tes: N.** + 4 by
da850 74 65 73 3a 20 4d 61 73 74 65 72 20 6a 6f 75 72  tes: Master jour
da860 6e 61 6c 20 6e 61 6d 65 20 63 68 65 63 6b 73 75  nal name checksu
da870 6d 2e 0a 2a 2a 20 2b 20 38 20 62 79 74 65 73 3a  m..** + 8 bytes:
da880 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b 5d   aJournalMagic[]
da890 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 73 74  ..**.** The mast
da8a0 65 72 20 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20  er journal page 
da8b0 63 68 65 63 6b 73 75 6d 20 69 73 20 74 68 65 20  checksum is the 
da8c0 73 75 6d 20 6f 66 20 74 68 65 20 62 79 74 65 73  sum of the bytes
da8d0 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72 0a 2a   in the master.*
da8e0 2a 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e 0a  * journal name..
da8f0 2a 2a 0a 2a 2a 20 49 66 20 7a 4d 61 73 74 65 72  **.** If zMaster
da900 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74   is a NULL point
da910 65 72 20 28 6f 63 63 75 72 73 20 66 6f 72 20 61  er (occurs for a
da920 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65   single database
da930 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 0a   transaction), .
da940 2a 2a 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20  ** this call is 
da950 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74  a no-op..*/.stat
da960 69 63 20 69 6e 74 20 77 72 69 74 65 4d 61 73 74  ic int writeMast
da970 65 72 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20  erJournal(Pager 
da980 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 63  *pPager, const c
da990 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20  har *zMaster){. 
da9a0 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 6c   int rc;.  int l
da9b0 65 6e 3b 20 0a 20 20 69 6e 74 20 69 3b 20 0a 20  en; .  int i; . 
da9c0 20 69 36 34 20 6a 72 6e 6c 4f 66 66 3b 0a 20 20   i64 jrnlOff;.  
da9d0 69 36 34 20 6a 72 6e 6c 53 69 7a 65 3b 0a 20 20  i64 jrnlSize;.  
da9e0 75 33 32 20 63 6b 73 75 6d 20 3d 20 30 3b 0a 20  u32 cksum = 0;. 
da9f0 20 63 68 61 72 20 7a 42 75 66 5b 73 69 7a 65 6f   char zBuf[sizeo
daa00 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29  f(aJournalMagic)
daa10 2b 32 2a 34 5d 3b 0a 0a 20 20 69 66 28 20 21 7a  +2*4];..  if( !z
daa20 4d 61 73 74 65 72 20 7c 7c 20 70 50 61 67 65 72  Master || pPager
daa30 2d 3e 73 65 74 4d 61 73 74 65 72 29 20 72 65 74  ->setMaster) ret
daa40 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
daa50 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74   pPager->setMast
daa60 65 72 20 3d 20 31 3b 0a 0a 20 20 6c 65 6e 20 3d  er = 1;..  len =
daa70 20 73 74 72 6c 65 6e 28 7a 4d 61 73 74 65 72 29   strlen(zMaster)
daa80 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6c  ;.  for(i=0; i<l
daa90 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 6b  en; i++){.    ck
daaa0 73 75 6d 20 2b 3d 20 7a 4d 61 73 74 65 72 5b 69  sum += zMaster[i
daab0 5d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  ];.  }..  /* If 
daac0 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64  in full-sync mod
daad0 65 2c 20 61 64 76 61 6e 63 65 20 74 6f 20 74 68  e, advance to th
daae0 65 20 6e 65 78 74 20 64 69 73 6b 20 73 65 63 74  e next disk sect
daaf0 6f 72 20 62 65 66 6f 72 65 20 77 72 69 74 69 6e  or before writin
dab00 67 0a 20 20 2a 2a 20 74 68 65 20 6d 61 73 74 65  g.  ** the maste
dab10 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e 20  r journal name. 
dab20 54 68 69 73 20 69 73 20 69 6e 20 63 61 73 65 20  This is in case 
dab30 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 61 67  the previous pag
dab40 65 20 77 72 69 74 74 65 6e 20 74 6f 0a 20 20 2a  e written to.  *
dab50 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 61  * the journal ha
dab60 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 73  s already been s
dab70 79 6e 63 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  ynced..  */.  if
dab80 28 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79  ( pPager->fullSy
dab90 6e 63 20 29 7b 0a 20 20 20 20 73 65 65 6b 4a 6f  nc ){.    seekJo
daba0 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29  urnalHdr(pPager)
dabb0 3b 0a 20 20 7d 0a 20 20 6a 72 6e 6c 4f 66 66 20  ;.  }.  jrnlOff 
dabc0 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  = pPager->journa
dabd0 6c 4f 66 66 3b 0a 20 20 70 50 61 67 65 72 2d 3e  lOff;.  pPager->
dabe0 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 28 6c  journalOff += (l
dabf0 65 6e 2b 32 30 29 3b 0a 0a 20 20 72 63 20 3d 20  en+20);..  rc = 
dac00 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67  write32bits(pPag
dac10 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f 66 66  er->jfd, jrnlOff
dac20 2c 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28  , PAGER_MJ_PGNO(
dac30 70 50 61 67 65 72 29 29 3b 0a 20 20 69 66 28 20  pPager));.  if( 
dac40 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
dac50 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 6a 72 6e  return rc;.  jrn
dac60 6c 4f 66 66 20 2b 3d 20 34 3b 0a 0a 20 20 72 63  lOff += 4;..  rc
dac70 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
dac80 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a  e(pPager->jfd, z
dac90 4d 61 73 74 65 72 2c 20 6c 65 6e 2c 20 6a 72 6e  Master, len, jrn
daca0 6c 4f 66 66 29 3b 0a 20 20 69 66 28 20 72 63 21  lOff);.  if( rc!
dacb0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
dacc0 75 72 6e 20 72 63 3b 0a 20 20 6a 72 6e 6c 4f 66  urn rc;.  jrnlOf
dacd0 66 20 2b 3d 20 6c 65 6e 3b 0a 0a 20 20 70 75 74  f += len;..  put
dace0 33 32 62 69 74 73 28 7a 42 75 66 2c 20 6c 65 6e  32bits(zBuf, len
dacf0 29 3b 0a 20 20 70 75 74 33 32 62 69 74 73 28 26  );.  put32bits(&
dad00 7a 42 75 66 5b 34 5d 2c 20 63 6b 73 75 6d 29 3b  zBuf[4], cksum);
dad10 0a 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b  .  memcpy(&zBuf[
dad20 38 5d 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  8], aJournalMagi
dad30 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e  c, sizeof(aJourn
dad40 61 6c 4d 61 67 69 63 29 29 3b 0a 20 20 72 63 20  alMagic));.  rc 
dad50 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65  = sqlite3OsWrite
dad60 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 42  (pPager->jfd, zB
dad70 75 66 2c 20 38 2b 73 69 7a 65 6f 66 28 61 4a 6f  uf, 8+sizeof(aJo
dad80 75 72 6e 61 6c 4d 61 67 69 63 29 2c 20 6a 72 6e  urnalMagic), jrn
dad90 6c 4f 66 66 29 3b 0a 20 20 6a 72 6e 6c 4f 66 66  lOff);.  jrnlOff
dada0 20 2b 3d 20 38 2b 73 69 7a 65 6f 66 28 61 4a 6f   += 8+sizeof(aJo
dadb0 75 72 6e 61 6c 4d 61 67 69 63 29 3b 0a 20 20 70  urnalMagic);.  p
dadc0 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20  Pager->needSync 
dadd0 3d 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  = !pPager->noSyn
dade0 63 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  c;..  /* If the 
dadf0 70 61 67 65 72 20 69 73 20 69 6e 20 70 65 72 69  pager is in peri
dae00 73 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c 20 6d 6f  stent-journal mo
dae10 64 65 2c 20 74 68 65 6e 20 74 68 65 20 70 68 79  de, then the phy
dae20 73 69 63 61 6c 20 0a 20 20 2a 2a 20 6a 6f 75 72  sical .  ** jour
dae30 6e 61 6c 2d 66 69 6c 65 20 6d 61 79 20 65 78 74  nal-file may ext
dae40 65 6e 64 20 70 61 73 74 20 74 68 65 20 65 6e 64  end past the end
dae50 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 2d 6a   of the master-j
dae60 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a 20 20 2a 2a  ournal name.  **
dae70 20 61 6e 64 20 38 20 62 79 74 65 73 20 6f 66 20   and 8 bytes of 
dae80 6d 61 67 69 63 20 64 61 74 61 20 6a 75 73 74 20  magic data just 
dae90 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 66  written to the f
daea0 69 6c 65 2e 20 54 68 69 73 20 69 73 20 0a 20 20  ile. This is .  
daeb0 2a 2a 20 64 61 6e 67 65 72 6f 75 73 20 62 65 63  ** dangerous bec
daec0 61 75 73 65 20 74 68 65 20 63 6f 64 65 20 74 6f  ause the code to
daed0 20 72 6f 6c 6c 62 61 63 6b 20 61 20 68 6f 74 2d   rollback a hot-
daee0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a  journal file.  *
daef0 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62  * will not be ab
daf00 6c 65 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6d  le to find the m
daf10 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e 61  aster-journal na
daf20 6d 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  me to determine 
daf30 0a 20 20 2a 2a 20 77 68 65 74 68 65 72 20 6f 72  .  ** whether or
daf40 20 6e 6f 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c   not the journal
daf50 20 69 73 20 68 6f 74 2e 20 0a 20 20 2a 2a 0a 20   is hot. .  **. 
daf60 20 2a 2a 20 45 61 73 69 65 73 74 20 74 68 69 6e   ** Easiest thin
daf70 67 20 74 6f 20 64 6f 20 69 6e 20 74 68 69 73 20  g to do in this 
daf80 73 63 65 6e 61 72 69 6f 20 69 73 20 74 6f 20 74  scenario is to t
daf90 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f 75 72  runcate the jour
dafa0 6e 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20 74  nal .  ** file t
dafb0 6f 20 74 68 65 20 72 65 71 75 69 72 65 64 20 73  o the required s
dafc0 69 7a 65 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 28  ize..  */ .  if(
dafd0 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29   (rc==SQLITE_OK)
dafe0 0a 20 20 20 26 26 20 28 72 63 20 3d 20 73 71 6c  .   && (rc = sql
daff0 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70  ite3OsFileSize(p
db000 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 6a 72 6e  Pager->jfd, &jrn
db010 6c 53 69 7a 65 29 29 3d 3d 53 51 4c 49 54 45 5f  lSize))==SQLITE_
db020 4f 4b 0a 20 20 20 26 26 20 6a 72 6e 6c 53 69 7a  OK.   && jrnlSiz
db030 65 3e 6a 72 6e 6c 4f 66 66 0a 20 20 29 7b 0a 20  e>jrnlOff.  ){. 
db040 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
db050 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72  sTruncate(pPager
db060 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f 66 66 29 3b  ->jfd, jrnlOff);
db070 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
db080 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 6f  ;.}../*.** Add o
db090 72 20 72 65 6d 6f 76 65 20 61 20 70 61 67 65 20  r remove a page 
db0a0 66 72 6f 6d 20 74 68 65 20 6c 69 73 74 20 6f 66  from the list of
db0b0 20 61 6c 6c 20 70 61 67 65 73 20 74 68 61 74 20   all pages that 
db0c0 61 72 65 20 69 6e 20 74 68 65 0a 2a 2a 20 73 74  are in the.** st
db0d0 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e  atement journal.
db0e0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 61 67 65 72  .**.** The Pager
db0f0 20 6b 65 65 70 73 20 61 20 73 65 70 61 72 61 74   keeps a separat
db100 65 20 6c 69 73 74 20 6f 66 20 70 61 67 65 73 20  e list of pages 
db110 74 68 61 74 20 61 72 65 20 63 75 72 72 65 6e 74  that are current
db120 6c 79 20 69 6e 0a 2a 2a 20 74 68 65 20 73 74 61  ly in.** the sta
db130 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20  tement journal. 
db140 20 54 68 69 73 20 68 65 6c 70 73 20 74 68 65 20   This helps the 
db150 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d 74  sqlite3PagerStmt
db160 43 6f 6d 6d 69 74 28 29 0a 2a 2a 20 72 6f 75 74  Commit().** rout
db170 69 6e 65 20 72 75 6e 20 4d 55 43 48 20 66 61 73  ine run MUCH fas
db180 74 65 72 20 66 6f 72 20 74 68 65 20 63 6f 6d 6d  ter for the comm
db190 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20 74 68  on case where th
db1a0 65 72 65 20 61 72 65 20 6d 61 6e 79 0a 2a 2a 20  ere are many.** 
db1b0 70 61 67 65 73 20 69 6e 20 6d 65 6d 6f 72 79 20  pages in memory 
db1c0 62 75 74 20 6f 6e 6c 79 20 61 20 66 65 77 20 61  but only a few a
db1d0 72 65 20 69 6e 20 74 68 65 20 73 74 61 74 65 6d  re in the statem
db1e0 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f 0a  ent journal..*/.
db1f0 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65  static void page
db200 5f 61 64 64 5f 74 6f 5f 73 74 6d 74 5f 6c 69 73  _add_to_stmt_lis
db210 74 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20  t(PgHdr *pPg){. 
db220 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d   Pager *pPager =
db230 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20   pPg->pPager;.  
db240 50 67 48 69 73 74 6f 72 79 20 2a 70 48 69 73 74  PgHistory *pHist
db250 20 3d 20 50 47 48 44 52 5f 54 4f 5f 48 49 53 54   = PGHDR_TO_HIST
db260 28 70 50 67 2c 20 70 50 61 67 65 72 29 3b 0a 20  (pPg, pPager);. 
db270 20 61 73 73 65 72 74 28 20 4d 45 4d 44 42 20 29   assert( MEMDB )
db280 3b 0a 20 20 69 66 28 20 21 70 48 69 73 74 2d 3e  ;.  if( !pHist->
db290 69 6e 53 74 6d 74 20 29 7b 0a 20 20 20 20 61 73  inStmt ){.    as
db2a0 73 65 72 74 28 20 70 48 69 73 74 2d 3e 70 50 72  sert( pHist->pPr
db2b0 65 76 53 74 6d 74 3d 3d 30 20 26 26 20 70 48 69  evStmt==0 && pHi
db2c0 73 74 2d 3e 70 4e 65 78 74 53 74 6d 74 3d 3d 30  st->pNextStmt==0
db2d0 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67   );.    if( pPag
db2e0 65 72 2d 3e 70 53 74 6d 74 20 29 7b 0a 20 20 20  er->pStmt ){.   
db2f0 20 20 20 50 47 48 44 52 5f 54 4f 5f 48 49 53 54     PGHDR_TO_HIST
db300 28 70 50 61 67 65 72 2d 3e 70 53 74 6d 74 2c 20  (pPager->pStmt, 
db310 70 50 61 67 65 72 29 2d 3e 70 50 72 65 76 53 74  pPager)->pPrevSt
db320 6d 74 20 3d 20 70 50 67 3b 0a 20 20 20 20 7d 0a  mt = pPg;.    }.
db330 20 20 20 20 70 48 69 73 74 2d 3e 70 4e 65 78 74      pHist->pNext
db340 53 74 6d 74 20 3d 20 70 50 61 67 65 72 2d 3e 70  Stmt = pPager->p
db350 53 74 6d 74 3b 0a 20 20 20 20 70 50 61 67 65 72  Stmt;.    pPager
db360 2d 3e 70 53 74 6d 74 20 3d 20 70 50 67 3b 0a 20  ->pStmt = pPg;. 
db370 20 20 20 70 48 69 73 74 2d 3e 69 6e 53 74 6d 74     pHist->inStmt
db380 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   = 1;.  }.}../*.
db390 2a 2a 20 46 69 6e 64 20 61 20 70 61 67 65 20 69  ** Find a page i
db3a0 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  n the hash table
db3b0 20 67 69 76 65 6e 20 69 74 73 20 70 61 67 65 20   given its page 
db3c0 6e 75 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 0a  number.  Return.
db3d0 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
db3e0 74 68 65 20 70 61 67 65 20 6f 72 20 4e 55 4c 4c  the page or NULL
db3f0 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a   if not found..*
db400 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72 20 2a  /.static PgHdr *
db410 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 50 61 67  pager_lookup(Pag
db420 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f  er *pPager, Pgno
db430 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48 64 72 20   pgno){.  PgHdr 
db440 2a 70 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72  *p;.  if( pPager
db450 2d 3e 61 48 61 73 68 3d 3d 30 20 29 20 72 65 74  ->aHash==0 ) ret
db460 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20 70 50 61  urn 0;.  p = pPa
db470 67 65 72 2d 3e 61 48 61 73 68 5b 70 67 6e 6f 20  ger->aHash[pgno 
db480 26 20 28 70 50 61 67 65 72 2d 3e 6e 48 61 73 68  & (pPager->nHash
db490 2d 31 29 5d 3b 0a 20 20 77 68 69 6c 65 28 20 70  -1)];.  while( p
db4a0 20 26 26 20 70 2d 3e 70 67 6e 6f 21 3d 70 67 6e   && p->pgno!=pgn
db4b0 6f 20 29 7b 0a 20 20 20 20 70 20 3d 20 70 2d 3e  o ){.    p = p->
db4c0 70 4e 65 78 74 48 61 73 68 3b 0a 20 20 7d 0a 20  pNextHash;.  }. 
db4d0 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
db4e0 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 69 6e  .** Clear the in
db4f0 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e 20 20  -memory cache.  
db500 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  This routine.** 
db510 73 65 74 73 20 74 68 65 20 73 74 61 74 65 20 6f  sets the state o
db520 66 20 74 68 65 20 70 61 67 65 72 20 62 61 63 6b  f the pager back
db530 20 74 6f 20 77 68 61 74 20 69 74 20 77 61 73 20   to what it was 
db540 77 68 65 6e 20 69 74 20 77 61 73 20 66 69 72 73  when it was firs
db550 74 0a 2a 2a 20 6f 70 65 6e 65 64 2e 20 20 41 6e  t.** opened.  An
db560 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61  y outstanding pa
db570 67 65 73 20 61 72 65 20 69 6e 76 61 6c 69 64 61  ges are invalida
db580 74 65 64 20 61 6e 64 20 73 75 62 73 65 71 75 65  ted and subseque
db590 6e 74 20 61 74 74 65 6d 70 74 73 0a 2a 2a 20 74  nt attempts.** t
db5a0 6f 20 61 63 63 65 73 73 20 74 68 6f 73 65 20 70  o access those p
db5b0 61 67 65 73 20 77 69 6c 6c 20 6c 69 6b 65 6c 79  ages will likely
db5c0 20 72 65 73 75 6c 74 20 69 6e 20 61 20 63 6f 72   result in a cor
db5d0 65 64 75 6d 70 2e 0a 2a 2f 0a 73 74 61 74 69 63  edump..*/.static
db5e0 20 76 6f 69 64 20 70 61 67 65 72 5f 72 65 73 65   void pager_rese
db5f0 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  t(Pager *pPager)
db600 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 67 2c 20  {.  PgHdr *pPg, 
db610 2a 70 4e 65 78 74 3b 0a 20 20 69 66 28 20 70 50  *pNext;.  if( pP
db620 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 20  ager->errCode ) 
db630 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 70 50  return;.  for(pP
db640 67 3d 70 50 61 67 65 72 2d 3e 70 41 6c 6c 3b 20  g=pPager->pAll; 
db650 70 50 67 3b 20 70 50 67 3d 70 4e 65 78 74 29 7b  pPg; pPg=pNext){
db660 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 50  .    IOTRACE(("P
db670 47 46 52 45 45 20 25 70 20 25 64 5c 6e 22 2c 20  GFREE %p %d\n", 
db680 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e  pPager, pPg->pgn
db690 6f 29 29 3b 0a 20 20 20 20 50 41 47 45 52 5f 49  o));.    PAGER_I
db6a0 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65  NCR(sqlite3_page
db6b0 72 5f 70 67 66 72 65 65 5f 63 6f 75 6e 74 29 3b  r_pgfree_count);
db6c0 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70 50 67  .    pNext = pPg
db6d0 2d 3e 70 4e 65 78 74 41 6c 6c 3b 0a 20 20 20 20  ->pNextAll;.    
db6e0 6c 72 75 4c 69 73 74 52 65 6d 6f 76 65 28 70 50  lruListRemove(pP
db6f0 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  g);.    sqlite3_
db700 66 72 65 65 28 70 50 67 2d 3e 70 44 61 74 61 29  free(pPg->pData)
db710 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
db720 65 65 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 61  ee(pPg);.  }.  a
db730 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e 6c 72  ssert(pPager->lr
db740 75 2e 70 46 69 72 73 74 3d 3d 30 29 3b 0a 20 20  u.pFirst==0);.  
db750 61 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e 6c  assert(pPager->l
db760 72 75 2e 70 46 69 72 73 74 53 79 6e 63 65 64 3d  ru.pFirstSynced=
db770 3d 30 29 3b 0a 20 20 61 73 73 65 72 74 28 70 50  =0);.  assert(pP
db780 61 67 65 72 2d 3e 6c 72 75 2e 70 4c 61 73 74 3d  ager->lru.pLast=
db790 3d 30 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70  =0);.  pPager->p
db7a0 53 74 6d 74 20 3d 20 30 3b 0a 20 20 70 50 61 67  Stmt = 0;.  pPag
db7b0 65 72 2d 3e 70 41 6c 6c 20 3d 20 30 3b 0a 20 20  er->pAll = 0;.  
db7c0 70 50 61 67 65 72 2d 3e 70 44 69 72 74 79 20 3d  pPager->pDirty =
db7d0 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 48   0;.  pPager->nH
db7e0 61 73 68 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  ash = 0;.  sqlit
db7f0 65 33 5f 66 72 65 65 28 70 50 61 67 65 72 2d 3e  e3_free(pPager->
db800 61 48 61 73 68 29 3b 0a 20 20 70 50 61 67 65 72  aHash);.  pPager
db810 2d 3e 6e 50 61 67 65 20 3d 20 30 3b 0a 20 20 70  ->nPage = 0;.  p
db820 50 61 67 65 72 2d 3e 61 48 61 73 68 20 3d 20 30  Pager->aHash = 0
db830 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 66  ;.  pPager->nRef
db840 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55   = 0;.}../*.** U
db850 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61  nlock the databa
db860 73 65 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20  se file. .**.** 
db870 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20  If the pager is 
db880 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 65 72 72  currently in err
db890 6f 72 20 73 74 61 74 65 2c 20 64 69 73 63 61 72  or state, discar
db8a0 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  d the contents o
db8b0 66 20 0a 2a 2a 20 74 68 65 20 63 61 63 68 65 20  f .** the cache 
db8c0 61 6e 64 20 72 65 73 65 74 20 74 68 65 20 50 61  and reset the Pa
db8d0 67 65 72 20 73 74 72 75 63 74 75 72 65 20 69 6e  ger structure in
db8e0 74 65 72 6e 61 6c 20 73 74 61 74 65 2e 20 49 66  ternal state. If
db8f0 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 61 6e 20   there is.** an 
db900 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c  open journal-fil
db910 65 2c 20 74 68 65 6e 20 74 68 65 20 6e 65 78 74  e, then the next
db920 20 74 69 6d 65 20 61 20 73 68 61 72 65 64 2d 6c   time a shared-l
db930 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 0a  ock is obtained.
db940 2a 2a 20 6f 6e 20 74 68 65 20 70 61 67 65 72 20  ** on the pager 
db950 66 69 6c 65 20 28 62 79 20 74 68 69 73 20 6f 72  file (by this or
db960 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65   any other proce
db970 73 73 29 2c 20 69 74 20 77 69 6c 6c 20 62 65 0a  ss), it will be.
db980 2a 2a 20 74 72 65 61 74 65 64 20 61 73 20 61 20  ** treated as a 
db990 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 61 6e 64 20  hot-journal and 
db9a0 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2f 0a  rolled back..*/.
db9b0 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65  static void page
db9c0 72 5f 75 6e 6c 6f 63 6b 28 50 61 67 65 72 20 2a  r_unlock(Pager *
db9d0 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 21  pPager){.  if( !
db9e0 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
db9f0 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 69 66 28  eMode ){.    if(
dba00 20 21 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 20   !MEMDB ){.     
dba10 20 69 6e 74 20 72 63 20 3d 20 6f 73 55 6e 6c 6f   int rc = osUnlo
dba20 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 4e  ck(pPager->fd, N
dba30 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69  O_LOCK);.      i
dba40 66 28 20 72 63 20 29 20 70 50 61 67 65 72 2d 3e  f( rc ) pPager->
dba50 65 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20  errCode = rc;.  
dba60 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69      pPager->dbSi
dba70 7a 65 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 49  ze = -1;.      I
dba80 4f 54 52 41 43 45 28 28 22 55 4e 4c 4f 43 4b 20  OTRACE(("UNLOCK 
dba90 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a  %p\n", pPager)).
dbaa0 0a 20 20 20 20 20 20 2f 2a 20 41 6c 77 61 79 73  .      /* Always
dbab0 20 63 6c 6f 73 65 20 74 68 65 20 6a 6f 75 72 6e   close the journ
dbac0 61 6c 20 66 69 6c 65 20 77 68 65 6e 20 64 72 6f  al file when dro
dbad0 70 70 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  pping the databa
dbae0 73 65 20 6c 6f 63 6b 2e 0a 20 20 20 20 20 20 2a  se lock..      *
dbaf0 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 6f  * Otherwise, ano
dbb00 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  ther connection 
dbb10 77 69 74 68 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64  with journal_mod
dbb20 65 3d 64 65 6c 65 74 65 20 6d 69 67 68 74 0a 20  e=delete might. 
dbb30 20 20 20 20 20 2a 2a 20 64 65 6c 65 74 65 20 74       ** delete t
dbb40 68 65 20 66 69 6c 65 20 6f 75 74 20 66 72 6f 6d  he file out from
dbb50 20 75 6e 64 65 72 20 75 73 2e 0a 20 20 20 20 20   under us..     
dbb60 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 50   */.      if( pP
dbb70 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65  ager->journalOpe
dbb80 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  n ){.        sql
dbb90 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67  ite3OsClose(pPag
dbba0 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20  er->jfd);.      
dbbb0 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
dbbc0 6c 4f 70 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20  lOpen = 0;.     
dbbd0 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63     sqlite3Bitvec
dbbe0 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e  Destroy(pPager->
dbbf0 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20  pInJournal);.   
dbc00 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49 6e       pPager->pIn
dbc10 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20  Journal = 0;.   
dbc20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49     }..      /* I
dbc30 66 20 50 61 67 65 72 2e 65 72 72 43 6f 64 65 20  f Pager.errCode 
dbc40 69 73 20 73 65 74 2c 20 74 68 65 20 63 6f 6e 74  is set, the cont
dbc50 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65  ents of the page
dbc60 72 20 63 61 63 68 65 20 63 61 6e 6e 6f 74 20 62  r cache cannot b
dbc70 65 0a 20 20 20 20 20 20 2a 2a 20 74 72 75 73 74  e.      ** trust
dbc80 65 64 2e 20 4e 6f 77 20 74 68 61 74 20 74 68 65  ed. Now that the
dbc90 20 70 61 67 65 72 20 66 69 6c 65 20 69 73 20 75   pager file is u
dbca0 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 20 63 6f 6e  nlocked, the con
dbcb0 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 20 20 20  tents of the.   
dbcc0 20 20 20 2a 2a 20 63 61 63 68 65 20 63 61 6e 20     ** cache can 
dbcd0 62 65 20 64 69 73 63 61 72 64 65 64 20 61 6e 64  be discarded and
dbce0 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20   the error code 
dbcf0 73 61 66 65 6c 79 20 63 6c 65 61 72 65 64 2e 0a  safely cleared..
dbd00 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
dbd10 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  f( pPager->errCo
dbd20 64 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  de ){.        if
dbd30 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
dbd40 29 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  ) pPager->errCod
dbd50 65 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  e = SQLITE_OK;. 
dbd60 20 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 73         pager_res
dbd70 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  et(pPager);.    
dbd80 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
dbd90 73 74 6d 74 4f 70 65 6e 20 29 7b 0a 20 20 20 20  stmtOpen ){.    
dbda0 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43        sqlite3OsC
dbdb0 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 73 74 66  lose(pPager->stf
dbdc0 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  d);.          sq
dbdd0 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72  lite3BitvecDestr
dbde0 6f 79 28 70 50 61 67 65 72 2d 3e 70 49 6e 53 74  oy(pPager->pInSt
dbdf0 6d 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70  mt);.          p
dbe00 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74 20 3d  Pager->pInStmt =
dbe10 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   0;.        }.  
dbe20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74        pPager->st
dbe30 6d 74 4f 70 65 6e 20 3d 20 30 3b 0a 20 20 20 20  mtOpen = 0;.    
dbe40 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74      pPager->stmt
dbe50 49 6e 55 73 65 20 3d 20 30 3b 0a 20 20 20 20 20  InUse = 0;.     
dbe60 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
dbe70 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 20  alOff = 0;.     
dbe80 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
dbe90 61 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20  alStarted = 0;. 
dbea0 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73         pPager->s
dbeb0 74 6d 74 41 75 74 6f 6f 70 65 6e 20 3d 20 30 3b  tmtAutoopen = 0;
dbec0 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d  .        pPager-
dbed0 3e 6f 72 69 67 44 62 53 69 7a 65 20 3d 20 30 3b  >origDbSize = 0;
dbee0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
dbef0 20 20 20 20 69 66 28 20 21 4d 45 4d 44 42 20 7c      if( !MEMDB |
dbf00 7c 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  | pPager->errCod
dbf10 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  e==SQLITE_OK ){.
dbf20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74        pPager->st
dbf30 61 74 65 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f  ate = PAGER_UNLO
dbf40 43 4b 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72  CK;.      pPager
dbf50 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e  ->changeCountDon
dbf60 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  e = 0;.    }.  }
dbf70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74  .}../*.** Execut
dbf80 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69 66 20  e a rollback if 
dbf90 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
dbfa0 20 61 63 74 69 76 65 20 61 6e 64 20 75 6e 6c 6f   active and unlo
dbfb0 63 6b 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62  ck the .** datab
dbfc0 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65  ase file. If the
dbfd0 20 70 61 67 65 72 20 68 61 73 20 61 6c 72 65 61   pager has alrea
dbfe0 64 79 20 65 6e 74 65 72 65 64 20 74 68 65 20 65  dy entered the e
dbff0 72 72 6f 72 20 73 74 61 74 65 2c 20 0a 2a 2a 20  rror state, .** 
dc000 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74  do not attempt t
dc010 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a  he rollback..*/.
dc020 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65  static void page
dc030 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61  rUnlockAndRollba
dc040 63 6b 28 50 61 67 65 72 20 2a 70 29 7b 0a 20 20  ck(Pager *p){.  
dc050 2f 2a 20 61 73 73 65 72 74 28 20 70 2d 3e 73 74  /* assert( p->st
dc060 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52  ate>=PAGER_RESER
dc070 56 45 44 20 7c 7c 20 70 2d 3e 6a 6f 75 72 6e 61  VED || p->journa
dc080 6c 4f 70 65 6e 3d 3d 30 20 29 3b 20 2a 2f 0a 20  lOpen==0 ); */. 
dc090 20 69 66 28 20 70 2d 3e 65 72 72 43 6f 64 65 3d   if( p->errCode=
dc0a0 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 2d  =SQLITE_OK && p-
dc0b0 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45  >state>=PAGER_RE
dc0c0 53 45 52 56 45 44 20 29 7b 0a 20 20 20 20 73 71  SERVED ){.    sq
dc0d0 6c 69 74 65 33 46 61 75 6c 74 42 65 67 69 6e 42  lite3FaultBeginB
dc0e0 65 6e 69 67 6e 28 2d 31 29 3b 0a 20 20 20 20 73  enign(-1);.    s
dc0f0 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62  qlite3PagerRollb
dc100 61 63 6b 28 70 29 3b 0a 20 20 20 20 73 71 6c 69  ack(p);.    sqli
dc110 74 65 33 46 61 75 6c 74 45 6e 64 42 65 6e 69 67  te3FaultEndBenig
dc120 6e 28 2d 31 29 3b 0a 20 20 7d 0a 20 20 70 61 67  n(-1);.  }.  pag
dc130 65 72 5f 75 6e 6c 6f 63 6b 28 70 29 3b 0a 23 69  er_unlock(p);.#i
dc140 66 20 30 0a 20 20 61 73 73 65 72 74 28 20 70 2d  f 0.  assert( p-
dc150 3e 65 72 72 43 6f 64 65 20 7c 7c 20 21 70 2d 3e  >errCode || !p->
dc160 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 7c 7c 20 28  journalOpen || (
dc170 70 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65  p->exclusiveMode
dc180 26 26 21 70 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  &&!p->journalOff
dc190 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
dc1a0 2d 3e 65 72 72 43 6f 64 65 20 7c 7c 20 21 70 2d  ->errCode || !p-
dc1b0 3e 73 74 6d 74 4f 70 65 6e 20 7c 7c 20 70 2d 3e  >stmtOpen || p->
dc1c0 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 3b  exclusiveMode );
dc1d0 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
dc1e0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e   This routine en
dc1f0 64 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  ds a transaction
dc200 2e 20 20 41 20 74 72 61 6e 73 61 63 74 69 6f 6e  .  A transaction
dc210 20 69 73 20 65 6e 64 65 64 20 62 79 20 65 69 74   is ended by eit
dc220 68 65 72 0a 2a 2a 20 61 20 43 4f 4d 4d 49 54 20  her.** a COMMIT 
dc230 6f 72 20 61 20 52 4f 4c 4c 42 41 43 4b 2e 0a 2a  or a ROLLBACK..*
dc240 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 72  *.** When this r
dc250 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
dc260 2c 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20  , the pager has 
dc270 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
dc280 20 6f 70 65 6e 20 61 6e 64 0a 2a 2a 20 61 20 52   open and.** a R
dc290 45 53 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55  ESERVED or EXCLU
dc2a0 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  SIVE lock on the
dc2b0 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73   database.  This
dc2c0 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65   routine will re
dc2d0 6c 65 61 73 65 0a 2a 2a 20 74 68 65 20 64 61 74  lease.** the dat
dc2e0 61 62 61 73 65 20 6c 6f 63 6b 20 61 6e 64 20 61  abase lock and a
dc2f0 63 71 75 69 72 65 73 20 61 20 53 48 41 52 45 44  cquires a SHARED
dc300 20 6c 6f 63 6b 20 69 6e 20 69 74 73 20 70 6c 61   lock in its pla
dc310 63 65 20 69 66 20 74 68 61 74 20 69 73 0a 2a 2a  ce if that is.**
dc320 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
dc330 20 74 68 69 6e 67 20 74 6f 20 64 6f 2e 20 20 52   thing to do.  R
dc340 65 6c 65 61 73 65 20 6c 6f 63 6b 73 20 75 73 75  elease locks usu
dc350 61 6c 6c 79 20 69 73 20 61 70 70 72 6f 70 72 69  ally is appropri
dc360 61 74 65 2c 0a 2a 2a 20 75 6e 6c 65 73 73 20 77  ate,.** unless w
dc370 65 20 61 72 65 20 69 6e 20 65 78 63 6c 75 73 69  e are in exclusi
dc380 76 65 20 61 63 63 65 73 73 20 6d 6f 64 65 20 6f  ve access mode o
dc390 72 20 75 6e 6c 65 73 73 20 74 68 69 73 20 69 73  r unless this is
dc3a0 20 61 20 0a 2a 2a 20 43 4f 4d 4d 49 54 20 41 4e   a .** COMMIT AN
dc3b0 44 20 42 45 47 49 4e 20 6f 72 20 52 4f 4c 4c 42  D BEGIN or ROLLB
dc3c0 41 43 4b 20 41 4e 44 20 42 45 47 49 4e 20 6f 70  ACK AND BEGIN op
dc3d0 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  eration..**.** T
dc3e0 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
dc3f0 69 73 20 65 69 74 68 65 72 20 64 65 6c 65 74 65  is either delete
dc400 64 20 6f 72 20 74 72 75 6e 63 61 74 65 64 2e 0a  d or truncated..
dc410 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 43 6f 6e 73  **.** TODO: Cons
dc420 69 64 65 72 20 6b 65 65 70 69 6e 67 20 74 68 65  ider keeping the
dc430 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 70   journal file op
dc440 65 6e 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79  en for temporary
dc450 20 64 61 74 61 62 61 73 65 73 2e 0a 2a 2a 20 54   databases..** T
dc460 68 69 73 20 6d 69 67 68 74 20 67 69 76 65 20 61  his might give a
dc470 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d 70   performance imp
dc480 72 6f 76 65 6d 65 6e 74 20 6f 6e 20 77 69 6e 64  rovement on wind
dc490 6f 77 73 20 77 68 65 72 65 20 6f 70 65 6e 69 6e  ows where openin
dc4a0 67 0a 2a 2a 20 61 20 66 69 6c 65 20 69 73 20 61  g.** a file is a
dc4b0 6e 20 65 78 70 65 6e 73 69 76 65 20 6f 70 65 72  n expensive oper
dc4c0 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ation..*/.static
dc4d0 20 69 6e 74 20 70 61 67 65 72 5f 65 6e 64 5f 74   int pager_end_t
dc4e0 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 67 65 72  ransaction(Pager
dc4f0 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 68 61   *pPager, int ha
dc500 73 4d 61 73 74 65 72 29 7b 0a 20 20 50 67 48 64  sMaster){.  PgHd
dc510 72 20 2a 70 50 67 3b 0a 20 20 69 6e 74 20 72 63  r *pPg;.  int rc
dc520 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
dc530 69 6e 74 20 72 63 32 20 3d 20 53 51 4c 49 54 45  int rc2 = SQLITE
dc540 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 21  _OK;.  assert( !
dc550 4d 45 4d 44 42 20 29 3b 0a 20 20 69 66 28 20 70  MEMDB );.  if( p
dc560 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 50 41 47  Pager->state<PAG
dc570 45 52 5f 52 45 53 45 52 56 45 44 20 29 7b 0a 20  ER_RESERVED ){. 
dc580 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
dc590 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  _OK;.  }.  sqlit
dc5a0 65 33 50 61 67 65 72 53 74 6d 74 43 6f 6d 6d 69  e3PagerStmtCommi
dc5b0 74 28 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28  t(pPager);.  if(
dc5c0 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 4f 70 65   pPager->stmtOpe
dc5d0 6e 20 26 26 20 21 70 50 61 67 65 72 2d 3e 65 78  n && !pPager->ex
dc5e0 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 7b 0a 20  clusiveMode ){. 
dc5f0 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73     sqlite3OsClos
dc600 65 28 70 50 61 67 65 72 2d 3e 73 74 66 64 29 3b  e(pPager->stfd);
dc610 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 6d  .    pPager->stm
dc620 74 4f 70 65 6e 20 3d 20 30 3b 0a 20 20 7d 0a 20  tOpen = 0;.  }. 
dc630 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75   if( pPager->jou
dc640 72 6e 61 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20  rnalOpen ){.    
dc650 69 66 28 20 70 50 61 67 65 72 2d 3e 65 78 63 6c  if( pPager->excl
dc660 75 73 69 76 65 4d 6f 64 65 20 0a 20 20 20 20 20  usiveMode .     
dc670 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  || pPager->journ
dc680 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f  alMode==PAGER_JO
dc690 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53  URNALMODE_PERSIS
dc6a0 54 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72  T.    ){.      r
dc6b0 63 20 3d 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48  c = zeroJournalH
dc6c0 64 72 28 70 50 61 67 65 72 2c 20 68 61 73 4d 61  dr(pPager, hasMa
dc6d0 73 74 65 72 29 3b 0a 20 20 20 20 20 20 70 61 67  ster);.      pag
dc6e0 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c  er_error(pPager,
dc6f0 20 72 63 29 3b 0a 20 20 20 20 20 20 70 50 61 67   rc);.      pPag
dc700 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d  er->journalOff =
dc710 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72   0;.      pPager
dc720 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64  ->journalStarted
dc730 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 0;.    }else{
dc740 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  .      sqlite3Os
dc750 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66  Close(pPager->jf
dc760 64 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72  d);.      pPager
dc770 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 3d 20  ->journalOpen = 
dc780 30 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  0;.      if( rc=
dc790 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
dc7a0 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
dc7b0 65 33 4f 73 44 65 6c 65 74 65 28 70 50 61 67 65  e3OsDelete(pPage
dc7c0 72 2d 3e 70 56 66 73 2c 20 70 50 61 67 65 72 2d  r->pVfs, pPager-
dc7d0 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20  >zJournal, 0);. 
dc7e0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
dc7f0 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65   sqlite3BitvecDe
dc800 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 49  stroy(pPager->pI
dc810 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 70  nJournal);.    p
dc820 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
dc830 6c 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 70  l = 0;.    for(p
dc840 50 67 3d 70 50 61 67 65 72 2d 3e 70 41 6c 6c 3b  Pg=pPager->pAll;
dc850 20 70 50 67 3b 20 70 50 67 3d 70 50 67 2d 3e 70   pPg; pPg=pPg->p
dc860 4e 65 78 74 41 6c 6c 29 7b 0a 20 20 20 20 20 20  NextAll){.      
dc870 70 50 67 2d 3e 69 6e 4a 6f 75 72 6e 61 6c 20 3d  pPg->inJournal =
dc880 20 30 3b 0a 20 20 20 20 20 20 70 50 67 2d 3e 64   0;.      pPg->d
dc890 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 20 20  irty = 0;.      
dc8a0 70 50 67 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20  pPg->needSync = 
dc8b0 30 3b 0a 20 20 20 20 20 20 70 50 67 2d 3e 61 6c  0;.      pPg->al
dc8c0 77 61 79 73 52 6f 6c 6c 62 61 63 6b 20 3d 20 30  waysRollback = 0
dc8d0 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
dc8e0 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20  CHECK_PAGES.    
dc8f0 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20    pPg->pageHash 
dc900 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68  = pager_pagehash
dc910 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20  (pPg);.#endif.  
dc920 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e    }.    pPager->
dc930 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20  pDirty = 0;.    
dc940 70 50 61 67 65 72 2d 3e 64 69 72 74 79 43 61 63  pPager->dirtyCac
dc950 68 65 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67  he = 0;.    pPag
dc960 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20  er->nRec = 0;.  
dc970 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
dc980 74 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f  t( pPager->pInJo
dc990 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 7d 0a  urnal==0 );.  }.
dc9a0 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e  .  if( !pPager->
dc9b0 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 7b  exclusiveMode ){
dc9c0 0a 20 20 20 20 72 63 32 20 3d 20 6f 73 55 6e 6c  .    rc2 = osUnl
dc9d0 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20  ock(pPager->fd, 
dc9e0 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20  SHARED_LOCK);.  
dc9f0 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20    pPager->state 
dca00 3d 20 50 41 47 45 52 5f 53 48 41 52 45 44 3b 0a  = PAGER_SHARED;.
dca10 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67    }else if( pPag
dca20 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52  er->state==PAGER
dca30 5f 53 59 4e 43 45 44 20 29 7b 0a 20 20 20 20 70  _SYNCED ){.    p
dca40 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50  Pager->state = P
dca50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 3b 0a  AGER_EXCLUSIVE;.
dca60 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6f 72    }.  pPager->or
dca70 69 67 44 62 53 69 7a 65 20 3d 20 30 3b 0a 20 20  igDbSize = 0;.  
dca80 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65  pPager->setMaste
dca90 72 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d  r = 0;.  pPager-
dcaa0 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20  >needSync = 0;. 
dcab0 20 6c 72 75 4c 69 73 74 53 65 74 46 69 72 73 74   lruListSetFirst
dcac0 53 79 6e 63 65 64 28 70 50 61 67 65 72 29 3b 0a  Synced(pPager);.
dcad0 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65    pPager->dbSize
dcae0 20 3d 20 2d 31 3b 0a 20 20 70 50 61 67 65 72 2d   = -1;.  pPager-
dcaf0 3e 64 62 4d 6f 64 69 66 69 65 64 20 3d 20 30 3b  >dbModified = 0;
dcb00 0a 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d  ..  return (rc==
dcb10 53 51 4c 49 54 45 5f 4f 4b 3f 72 63 32 3a 72 63  SQLITE_OK?rc2:rc
dcb20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  );.}../*.** Comp
dcb30 75 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61  ute and return a
dcb40 20 63 68 65 63 6b 73 75 6d 20 66 6f 72 20 74 68   checksum for th
dcb50 65 20 70 61 67 65 20 6f 66 20 64 61 74 61 2e 0a  e page of data..
dcb60 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f  **.** This is no
dcb70 74 20 61 20 72 65 61 6c 20 63 68 65 63 6b 73 75  t a real checksu
dcb80 6d 2e 20 20 49 74 20 69 73 20 72 65 61 6c 6c 79  m.  It is really
dcb90 20 6a 75 73 74 20 74 68 65 20 73 75 6d 20 6f 66   just the sum of
dcba0 20 74 68 65 20 0a 2a 2a 20 72 61 6e 64 6f 6d 20   the .** random 
dcbb0 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 61 6e  initial value an
dcbc0 64 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65  d the page numbe
dcbd0 72 2e 20 20 57 65 20 65 78 70 65 72 69 6d 65 6e  r.  We experimen
dcbe0 74 65 64 20 77 69 74 68 0a 2a 2a 20 61 20 63 68  ted with.** a ch
dcbf0 65 63 6b 73 75 6d 20 6f 66 20 74 68 65 20 65 6e  ecksum of the en
dcc00 74 69 72 65 20 64 61 74 61 2c 20 62 75 74 20 74  tire data, but t
dcc10 68 61 74 20 77 61 73 20 66 6f 75 6e 64 20 74 6f  hat was found to
dcc20 20 62 65 20 74 6f 6f 20 73 6c 6f 77 2e 0a 2a 2a   be too slow..**
dcc30 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68  .** Note that th
dcc40 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73  e page number is
dcc50 20 73 74 6f 72 65 64 20 61 74 20 74 68 65 20 62   stored at the b
dcc60 65 67 69 6e 6e 69 6e 67 20 6f 66 20 64 61 74 61  eginning of data
dcc70 20 61 6e 64 0a 2a 2a 20 74 68 65 20 63 68 65 63   and.** the chec
dcc80 6b 73 75 6d 20 69 73 20 73 74 6f 72 65 64 20 61  ksum is stored a
dcc90 74 20 74 68 65 20 65 6e 64 2e 20 20 54 68 69 73  t the end.  This
dcca0 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20   is important.  
dccb0 49 66 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 63 6f  If journal.** co
dccc0 72 72 75 70 74 69 6f 6e 20 6f 63 63 75 72 73 20  rruption occurs 
dccd0 64 75 65 20 74 6f 20 61 20 70 6f 77 65 72 20 66  due to a power f
dcce0 61 69 6c 75 72 65 2c 20 74 68 65 20 6d 6f 73 74  ailure, the most
dccf0 20 6c 69 6b 65 6c 79 20 73 63 65 6e 61 72 69 6f   likely scenario
dcd00 0a 2a 2a 20 69 73 20 74 68 61 74 20 6f 6e 65 20  .** is that one 
dcd10 65 6e 64 20 6f 72 20 74 68 65 20 6f 74 68 65 72  end or the other
dcd20 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 77   of the record w
dcd30 69 6c 6c 20 62 65 20 63 68 61 6e 67 65 64 2e 20  ill be changed. 
dcd40 20 49 74 20 69 73 0a 2a 2a 20 6d 75 63 68 20 6c   It is.** much l
dcd50 65 73 73 20 6c 69 6b 65 6c 79 20 74 68 61 74 20  ess likely that 
dcd60 74 68 65 20 74 77 6f 20 65 6e 64 73 20 6f 66 20  the two ends of 
dcd70 74 68 65 20 6a 6f 75 72 6e 61 6c 20 72 65 63 6f  the journal reco
dcd80 72 64 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 63 6f  rd will be.** co
dcd90 72 72 65 63 74 20 61 6e 64 20 74 68 65 20 6d 69  rrect and the mi
dcda0 64 64 6c 65 20 62 65 20 63 6f 72 72 75 70 74 2e  ddle be corrupt.
dcdb0 20 20 54 68 75 73 2c 20 74 68 69 73 20 22 63 68    Thus, this "ch
dcdc0 65 63 6b 73 75 6d 22 20 73 63 68 65 6d 65 2c 0a  ecksum" scheme,.
dcdd0 2a 2a 20 74 68 6f 75 67 68 20 66 61 73 74 20 61  ** though fast a
dcde0 6e 64 20 73 69 6d 70 6c 65 2c 20 63 61 74 63 68  nd simple, catch
dcdf0 65 73 20 74 68 65 20 6d 6f 73 74 6c 79 20 6c 69  es the mostly li
dce00 6b 65 6c 79 20 6b 69 6e 64 20 6f 66 20 63 6f 72  kely kind of cor
dce10 72 75 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46  ruption..**.** F
dce20 49 58 20 4d 45 3a 20 20 43 6f 6e 73 69 64 65 72  IX ME:  Consider
dce30 20 61 64 64 69 6e 67 20 65 76 65 72 79 20 32 30   adding every 20
dce40 30 74 68 20 28 6f 72 20 73 6f 29 20 62 79 74 65  0th (or so) byte
dce50 20 6f 66 20 74 68 65 20 64 61 74 61 20 74 6f 20   of the data to 
dce60 74 68 65 0a 2a 2a 20 63 68 65 63 6b 73 75 6d 2e  the.** checksum.
dce70 20 20 54 68 61 74 20 77 61 79 20 69 66 20 61 20    That way if a 
dce80 73 69 6e 67 6c 65 20 70 61 67 65 20 73 70 61 6e  single page span
dce90 73 20 33 20 6f 72 20 6d 6f 72 65 20 64 69 73 6b  s 3 or more disk
dcea0 20 73 65 63 74 6f 72 73 20 61 6e 64 0a 2a 2a 20   sectors and.** 
dceb0 6f 6e 6c 79 20 74 68 65 20 6d 69 64 64 6c 65 20  only the middle 
dcec0 73 65 63 74 6f 72 20 69 73 20 63 6f 72 72 75 70  sector is corrup
dced0 74 2c 20 77 65 20 77 69 6c 6c 20 73 74 69 6c 6c  t, we will still
dcee0 20 68 61 76 65 20 61 20 72 65 61 73 6f 6e 61 62   have a reasonab
dcef0 6c 65 0a 2a 2a 20 63 68 61 6e 63 65 20 6f 66 20  le.** chance of 
dcf00 66 61 69 6c 69 6e 67 20 74 68 65 20 63 68 65 63  failing the chec
dcf10 6b 73 75 6d 20 61 6e 64 20 74 68 75 73 20 64 65  ksum and thus de
dcf20 74 65 63 74 69 6e 67 20 74 68 65 20 70 72 6f 62  tecting the prob
dcf30 6c 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75  lem..*/.static u
dcf40 33 32 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 50  32 pager_cksum(P
dcf50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 63 6f  ager *pPager, co
dcf60 6e 73 74 20 75 38 20 2a 61 44 61 74 61 29 7b 0a  nst u8 *aData){.
dcf70 20 20 75 33 32 20 63 6b 73 75 6d 20 3d 20 70 50    u32 cksum = pP
dcf80 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 3b  ager->cksumInit;
dcf90 0a 20 20 69 6e 74 20 69 20 3d 20 70 50 61 67 65  .  int i = pPage
dcfa0 72 2d 3e 70 61 67 65 53 69 7a 65 2d 32 30 30 3b  r->pageSize-200;
dcfb0 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 20 29 7b  .  while( i>0 ){
dcfc0 0a 20 20 20 20 63 6b 73 75 6d 20 2b 3d 20 61 44  .    cksum += aD
dcfd0 61 74 61 5b 69 5d 3b 0a 20 20 20 20 69 20 2d 3d  ata[i];.    i -=
dcfe0 20 32 30 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   200;.  }.  retu
dcff0 72 6e 20 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f 2a 20  rn cksum;.}../* 
dd000 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74  Forward declarat
dd010 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ion */.static vo
dd020 69 64 20 6d 61 6b 65 43 6c 65 61 6e 28 50 67 48  id makeClean(PgH
dd030 64 72 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 61  dr*);../*.** Rea
dd040 64 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20  d a single page 
dd050 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c  from the journal
dd060 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 6f 6e 20   file opened on 
dd070 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a  file descriptor.
dd080 2a 2a 20 6a 66 64 2e 20 20 50 6c 61 79 62 61 63  ** jfd.  Playbac
dd090 6b 20 74 68 69 73 20 6f 6e 65 20 70 61 67 65 2e  k this one page.
dd0a0 0a 2a 2a 0a 2a 2a 20 49 66 20 75 73 65 43 6b 73  .**.** If useCks
dd0b0 75 6d 3d 3d 30 20 69 74 20 6d 65 61 6e 73 20 74  um==0 it means t
dd0c0 68 69 73 20 6a 6f 75 72 6e 61 6c 20 64 6f 65 73  his journal does
dd0d0 20 6e 6f 74 20 75 73 65 20 63 68 65 63 6b 73 75   not use checksu
dd0e0 6d 73 2e 20 20 43 68 65 63 6b 73 75 6d 73 0a 2a  ms.  Checksums.*
dd0f0 2a 20 61 72 65 20 6e 6f 74 20 75 73 65 64 20 69  * are not used i
dd100 6e 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72  n statement jour
dd110 6e 61 6c 73 20 62 65 63 61 75 73 65 20 73 74 61  nals because sta
dd120 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 73 20  tement journals 
dd130 64 6f 20 6e 6f 74 0a 2a 2a 20 6e 65 65 64 20 74  do not.** need t
dd140 6f 20 73 75 72 76 69 76 65 20 70 6f 77 65 72 20  o survive power 
dd150 66 61 69 6c 75 72 65 73 2e 0a 2a 2f 0a 73 74 61  failures..*/.sta
dd160 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 70 6c  tic int pager_pl
dd170 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28  ayback_one_page(
dd180 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
dd190 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  , .  sqlite3_fil
dd1a0 65 20 2a 6a 66 64 2c 0a 20 20 69 36 34 20 6f 66  e *jfd,.  i64 of
dd1b0 66 73 65 74 2c 0a 20 20 69 6e 74 20 75 73 65 43  fset,.  int useC
dd1c0 6b 73 75 6d 0a 29 7b 0a 20 20 69 6e 74 20 72 63  ksum.){.  int rc
dd1d0 3b 0a 20 20 50 67 48 64 72 20 2a 70 50 67 3b 20  ;.  PgHdr *pPg; 
dd1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dd1f0 20 20 2f 2a 20 41 6e 20 65 78 69 73 74 69 6e 67    /* An existing
dd200 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 61 63   page in the cac
dd210 68 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e  he */.  Pgno pgn
dd220 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o;              
dd230 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67        /* The pag
dd240 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 20 70 61  e number of a pa
dd250 67 65 20 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f  ge in journal */
dd260 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b 20 20 20  .  u32 cksum;   
dd270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dd280 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 75 73 65   /* Checksum use
dd290 64 20 66 6f 72 20 73 61 6e 69 74 79 20 63 68 65  d for sanity che
dd2a0 63 6b 69 6e 67 20 2a 2f 0a 20 20 75 38 20 2a 61  cking */.  u8 *a
dd2b0 44 61 74 61 20 3d 20 28 75 38 20 2a 29 70 50 61  Data = (u8 *)pPa
dd2c0 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 20  ger->pTmpSpace; 
dd2d0 20 20 2f 2a 20 54 65 6d 70 20 73 74 6f 72 61 67    /* Temp storag
dd2e0 65 20 66 6f 72 20 61 20 70 61 67 65 20 2a 2f 0a  e for a page */.
dd2f0 0a 20 20 2f 2a 20 75 73 65 43 6b 73 75 6d 20 73  .  /* useCksum s
dd300 68 6f 75 6c 64 20 62 65 20 74 72 75 65 20 66 6f  hould be true fo
dd310 72 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e  r the main journ
dd320 61 6c 20 61 6e 64 20 66 61 6c 73 65 20 66 6f 72  al and false for
dd330 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20  .  ** statement 
dd340 6a 6f 75 72 6e 61 6c 73 2e 20 20 56 65 72 69 66  journals.  Verif
dd350 79 20 74 68 61 74 20 74 68 69 73 20 69 73 20 61  y that this is a
dd360 6c 77 61 79 73 20 74 68 65 20 63 61 73 65 0a 20  lways the case. 
dd370 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 6a 66   */.  assert( jf
dd380 64 20 3d 3d 20 28 75 73 65 43 6b 73 75 6d 20 3f  d == (useCksum ?
dd390 20 70 50 61 67 65 72 2d 3e 6a 66 64 20 3a 20 70   pPager->jfd : p
dd3a0 50 61 67 65 72 2d 3e 73 74 66 64 29 20 29 3b 0a  Pager->stfd) );.
dd3b0 20 20 61 73 73 65 72 74 28 20 61 44 61 74 61 20    assert( aData 
dd3c0 29 3b 0a 0a 20 20 72 63 20 3d 20 72 65 61 64 33  );..  rc = read3
dd3d0 32 62 69 74 73 28 6a 66 64 2c 20 6f 66 66 73 65  2bits(jfd, offse
dd3e0 74 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 69 66 28  t, &pgno);.  if(
dd3f0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
dd400 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63   return rc;.  rc
dd410 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64   = sqlite3OsRead
dd420 28 6a 66 64 2c 20 61 44 61 74 61 2c 20 70 50 61  (jfd, aData, pPa
dd430 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f  ger->pageSize, o
dd440 66 66 73 65 74 2b 34 29 3b 0a 20 20 69 66 28 20  ffset+4);.  if( 
dd450 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
dd460 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 70 50 61  return rc;.  pPa
dd470 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
dd480 2b 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  += pPager->pageS
dd490 69 7a 65 20 2b 20 34 3b 0a 0a 20 20 2f 2a 20 53  ize + 4;..  /* S
dd4a0 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 6f  anity checking o
dd4b0 6e 20 74 68 65 20 70 61 67 65 2e 20 20 54 68 69  n the page.  Thi
dd4c0 73 20 69 73 20 6d 6f 72 65 20 69 6d 70 6f 72 74  s is more import
dd4d0 61 6e 74 20 74 68 61 74 20 49 20 6f 72 69 67 69  ant that I origi
dd4e0 6e 61 6c 6c 79 0a 20 20 2a 2a 20 74 68 6f 75 67  nally.  ** thoug
dd4f0 68 74 2e 20 20 49 66 20 61 20 70 6f 77 65 72 20  ht.  If a power 
dd500 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20 77  failure occurs w
dd510 68 69 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  hile the journal
dd520 20 69 73 20 62 65 69 6e 67 20 77 72 69 74 74 65   is being writte
dd530 6e 2c 0a 20 20 2a 2a 20 69 74 20 63 6f 75 6c 64  n,.  ** it could
dd540 20 63 61 75 73 65 20 69 6e 76 61 6c 69 64 20 64   cause invalid d
dd550 61 74 61 20 74 6f 20 62 65 20 77 72 69 74 74 65  ata to be writte
dd560 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e  n into the journ
dd570 61 6c 2e 20 20 57 65 20 6e 65 65 64 20 74 6f 0a  al.  We need to.
dd580 20 20 2a 2a 20 64 65 74 65 63 74 20 74 68 69 73    ** detect this
dd590 20 69 6e 76 61 6c 69 64 20 64 61 74 61 20 28 77   invalid data (w
dd5a0 69 74 68 20 68 69 67 68 20 70 72 6f 62 61 62 69  ith high probabi
dd5b0 6c 69 74 79 29 20 61 6e 64 20 69 67 6e 6f 72 65  lity) and ignore
dd5c0 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20   it..  */.  if( 
dd5d0 70 67 6e 6f 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d  pgno==0 || pgno=
dd5e0 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70  =PAGER_MJ_PGNO(p
dd5f0 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 72 65  Pager) ){.    re
dd600 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45  turn SQLITE_DONE
dd610 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 67 6e 6f  ;.  }.  if( pgno
dd620 3e 28 75 6e 73 69 67 6e 65 64 29 70 50 61 67 65  >(unsigned)pPage
dd630 72 2d 3e 64 62 53 69 7a 65 20 29 7b 0a 20 20 20  r->dbSize ){.   
dd640 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
dd650 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 75 73 65  K;.  }.  if( use
dd660 43 6b 73 75 6d 20 29 7b 0a 20 20 20 20 72 63 20  Cksum ){.    rc 
dd670 3d 20 72 65 61 64 33 32 62 69 74 73 28 6a 66 64  = read32bits(jfd
dd680 2c 20 6f 66 66 73 65 74 2b 70 50 61 67 65 72 2d  , offset+pPager-
dd690 3e 70 61 67 65 53 69 7a 65 2b 34 2c 20 26 63 6b  >pageSize+4, &ck
dd6a0 73 75 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 63  sum);.    if( rc
dd6b0 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
dd6c0 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
dd6d0 6c 4f 66 66 20 2b 3d 20 34 3b 0a 20 20 20 20 69  lOff += 4;.    i
dd6e0 66 28 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 70  f( pager_cksum(p
dd6f0 50 61 67 65 72 2c 20 61 44 61 74 61 29 21 3d 63  Pager, aData)!=c
dd700 6b 73 75 6d 20 29 7b 0a 20 20 20 20 20 20 72 65  ksum ){.      re
dd710 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45  turn SQLITE_DONE
dd720 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 61  ;.    }.  }..  a
dd730 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
dd740 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 53 45  tate==PAGER_RESE
dd750 52 56 45 44 20 7c 7c 20 70 50 61 67 65 72 2d 3e  RVED || pPager->
dd760 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43  state>=PAGER_EXC
dd770 4c 55 53 49 56 45 20 29 3b 0a 0a 20 20 2f 2a 20  LUSIVE );..  /* 
dd780 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20  If the pager is 
dd790 69 6e 20 52 45 53 45 52 56 45 44 20 73 74 61 74  in RESERVED stat
dd7a0 65 2c 20 74 68 65 6e 20 74 68 65 72 65 20 6d 75  e, then there mu
dd7b0 73 74 20 62 65 20 61 20 63 6f 70 79 20 6f 66 20  st be a copy of 
dd7c0 74 68 69 73 0a 20 20 2a 2a 20 70 61 67 65 20 69  this.  ** page i
dd7d0 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68  n the pager cach
dd7e0 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  e. In this case 
dd7f0 6a 75 73 74 20 75 70 64 61 74 65 20 74 68 65 20  just update the 
dd800 70 61 67 65 72 20 63 61 63 68 65 2c 0a 20 20 2a  pager cache,.  *
dd810 2a 20 6e 6f 74 20 74 68 65 20 64 61 74 61 62 61  * not the databa
dd820 73 65 20 66 69 6c 65 2e 20 54 68 65 20 70 61 67  se file. The pag
dd830 65 20 69 73 20 6c 65 66 74 20 6d 61 72 6b 65 64  e is left marked
dd840 20 64 69 72 74 79 20 69 6e 20 74 68 69 73 20 63   dirty in this c
dd850 61 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41  ase..  **.  ** A
dd860 6e 20 65 78 63 65 70 74 69 6f 6e 20 74 6f 20 74  n exception to t
dd870 68 65 20 61 62 6f 76 65 20 72 75 6c 65 3a 20 49  he above rule: I
dd880 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  f the database i
dd890 73 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64  s in no-sync mod
dd8a0 65 0a 20 20 2a 2a 20 61 6e 64 20 61 20 70 61 67  e.  ** and a pag
dd8b0 65 20 69 73 20 6d 6f 76 65 64 20 64 75 72 69 6e  e is moved durin
dd8c0 67 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c  g an incremental
dd8d0 20 76 61 63 75 75 6d 20 74 68 65 6e 20 74 68 65   vacuum then the
dd8e0 20 70 61 67 65 20 6d 61 79 0a 20 20 2a 2a 20 6e   page may.  ** n
dd8f0 6f 74 20 62 65 20 69 6e 20 74 68 65 20 70 61 67  ot be in the pag
dd900 65 72 20 63 61 63 68 65 2e 20 4c 61 74 65 72 3a  er cache. Later:
dd910 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f   if a malloc() o
dd920 72 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72  r IO error occur
dd930 73 0a 20 20 2a 2a 20 64 75 72 69 6e 67 20 61 20  s.  ** during a 
dd940 4d 6f 76 65 70 61 67 65 28 29 20 63 61 6c 6c 2c  Movepage() call,
dd950 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 6d   then the page m
dd960 61 79 20 6e 6f 74 20 62 65 20 69 6e 20 74 68 65  ay not be in the
dd970 20 63 61 63 68 65 0a 20 20 2a 2a 20 65 69 74 68   cache.  ** eith
dd980 65 72 2e 20 53 6f 20 74 68 65 20 63 6f 6e 64 69  er. So the condi
dd990 74 69 6f 6e 20 64 65 73 63 72 69 62 65 64 20 69  tion described i
dd9a0 6e 20 74 68 65 20 61 62 6f 76 65 20 70 61 72 61  n the above para
dd9b0 67 72 61 70 68 20 69 73 20 6e 6f 74 0a 20 20 2a  graph is not.  *
dd9c0 2a 20 61 73 73 65 72 74 28 29 61 62 6c 65 2e 0a  * assert()able..
dd9d0 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 69 6e 20    **.  ** If in 
dd9e0 45 58 43 4c 55 53 49 56 45 20 73 74 61 74 65 2c  EXCLUSIVE state,
dd9f0 20 74 68 65 6e 20 77 65 20 75 70 64 61 74 65 20   then we update 
dda00 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 20  the pager cache 
dda10 69 66 20 69 74 20 65 78 69 73 74 73 0a 20 20 2a  if it exists.  *
dda20 2a 20 61 6e 64 20 74 68 65 20 6d 61 69 6e 20 66  * and the main f
dda30 69 6c 65 2e 20 54 68 65 20 70 61 67 65 20 69 73  ile. The page is
dda40 20 74 68 65 6e 20 6d 61 72 6b 65 64 20 6e 6f 74   then marked not
dda50 20 64 69 72 74 79 2e 0a 20 20 2a 2a 0a 20 20 2a   dirty..  **.  *
dda60 2a 20 54 69 63 6b 65 74 20 23 31 31 37 31 3a 20  * Ticket #1171: 
dda70 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a   The statement j
dda80 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20 63 6f 6e  ournal might con
dda90 74 61 69 6e 20 70 61 67 65 20 63 6f 6e 74 65 6e  tain page conten
ddaa0 74 20 74 68 61 74 20 69 73 0a 20 20 2a 2a 20 64  t that is.  ** d
ddab0 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68  ifferent from th
ddac0 65 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 61  e page content a
ddad0 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  t the start of t
ddae0 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a  he transaction..
ddaf0 20 20 2a 2a 20 54 68 69 73 20 6f 63 63 75 72 73    ** This occurs
ddb00 20 77 68 65 6e 20 61 20 70 61 67 65 20 69 73 20   when a page is 
ddb10 63 68 61 6e 67 65 64 20 70 72 69 6f 72 20 74 6f  changed prior to
ddb20 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 61 20   the start of a 
ddb30 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 74  statement.  ** t
ddb40 68 65 6e 20 63 68 61 6e 67 65 64 20 61 67 61 69  hen changed agai
ddb50 6e 20 77 69 74 68 69 6e 20 74 68 65 20 73 74 61  n within the sta
ddb60 74 65 6d 65 6e 74 2e 20 20 57 68 65 6e 20 72 6f  tement.  When ro
ddb70 6c 6c 69 6e 67 20 62 61 63 6b 20 73 75 63 68 20  lling back such 
ddb80 61 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74  a.  ** statement
ddb90 20 77 65 20 6d 75 73 74 20 6e 6f 74 20 77 72 69   we must not wri
ddba0 74 65 20 74 6f 20 74 68 65 20 6f 72 69 67 69 6e  te to the origin
ddbb0 61 6c 20 64 61 74 61 62 61 73 65 20 75 6e 6c 65  al database unle
ddbc0 73 73 20 77 65 20 6b 6e 6f 77 0a 20 20 2a 2a 20  ss we know.  ** 
ddbd0 66 6f 72 20 63 65 72 74 61 69 6e 20 74 68 61 74  for certain that
ddbe0 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 20 63   original page c
ddbf0 6f 6e 74 65 6e 74 73 20 61 72 65 20 73 79 6e 63  ontents are sync
ddc00 65 64 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e  ed into the main
ddc10 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a   rollback.  ** j
ddc20 6f 75 72 6e 61 6c 2e 20 20 4f 74 68 65 72 77 69  ournal.  Otherwi
ddc30 73 65 2c 20 61 20 70 6f 77 65 72 20 6c 6f 73 73  se, a power loss
ddc40 20 6d 69 67 68 74 20 6c 65 61 76 65 20 6d 6f 64   might leave mod
ddc50 69 66 69 65 64 20 64 61 74 61 20 69 6e 20 74 68  ified data in th
ddc60 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20  e.  ** database 
ddc70 66 69 6c 65 20 77 69 74 68 6f 75 74 20 61 6e 20  file without an 
ddc80 65 6e 74 72 79 20 69 6e 20 74 68 65 20 72 6f 6c  entry in the rol
ddc90 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 74 68  lback journal th
ddca0 61 74 20 63 61 6e 0a 20 20 2a 2a 20 72 65 73 74  at can.  ** rest
ddcb0 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ore the database
ddcc0 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c   to its original
ddcd0 20 66 6f 72 6d 2e 20 20 54 77 6f 20 63 6f 6e 64   form.  Two cond
ddce0 69 74 69 6f 6e 73 20 6d 75 73 74 20 62 65 0a 20  itions must be. 
ddcf0 20 2a 2a 20 6d 65 74 20 62 65 66 6f 72 65 20 77   ** met before w
ddd00 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61  riting to the da
ddd10 74 61 62 61 73 65 20 66 69 6c 65 73 2e 20 28 31  tabase files. (1
ddd20 29 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d  ) the database m
ddd30 75 73 74 20 62 65 0a 20 20 2a 2a 20 6c 6f 63 6b  ust be.  ** lock
ddd40 65 64 2e 20 20 28 32 29 20 77 65 20 6b 6e 6f 77  ed.  (2) we know
ddd50 20 74 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e   that the origin
ddd60 61 6c 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20  al page content 
ddd70 69 73 20 66 75 6c 6c 79 20 73 79 6e 63 65 64 0a  is fully synced.
ddd80 20 20 2a 2a 20 69 6e 20 74 68 65 20 6d 61 69 6e    ** in the main
ddd90 20 6a 6f 75 72 6e 61 6c 20 65 69 74 68 65 72 20   journal either 
ddda0 62 65 63 61 75 73 65 20 74 68 65 20 70 61 67 65  because the page
dddb0 20 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65   is not in cache
dddc0 20 6f 72 20 65 6c 73 65 0a 20 20 2a 2a 20 74 68   or else.  ** th
dddd0 65 20 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64  e page is marked
ddde0 20 61 73 20 6e 65 65 64 53 79 6e 63 3d 3d 30 2e   as needSync==0.
dddf0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 32 30 30 38 2d  .  **.  ** 2008-
dde00 30 34 2d 31 34 3a 20 20 57 68 65 6e 20 61 74 74  04-14:  When att
dde10 65 6d 70 74 69 6e 67 20 74 6f 20 76 61 63 75 75  empting to vacuu
dde20 6d 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61  m a corrupt data
dde30 62 61 73 65 20 66 69 6c 65 2c 20 69 74 0a 20 20  base file, it.  
dde40 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74  ** is possible t
dde50 6f 20 66 61 69 6c 20 61 20 73 74 61 74 65 6d 65  o fail a stateme
dde60 6e 74 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65  nt on a database
dde70 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 79   that does not y
dde80 65 74 20 65 78 69 73 74 2e 0a 20 20 2a 2a 20 44  et exist..  ** D
dde90 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f  o not attempt to
ddea0 20 77 72 69 74 65 20 69 66 20 64 61 74 61 62 61   write if databa
ddeb0 73 65 20 66 69 6c 65 20 68 61 73 20 6e 65 76 65  se file has neve
ddec0 72 20 62 65 65 6e 20 6f 70 65 6e 65 64 2e 0a 20  r been opened.. 
dded0 20 2a 2f 0a 20 20 70 50 67 20 3d 20 70 61 67 65   */.  pPg = page
ddee0 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c  r_lookup(pPager,
ddef0 20 70 67 6e 6f 29 3b 0a 20 20 50 41 47 45 52 54   pgno);.  PAGERT
ddf00 52 41 43 45 34 28 22 50 4c 41 59 42 41 43 4b 20  RACE4("PLAYBACK 
ddf10 25 64 20 70 61 67 65 20 25 64 20 68 61 73 68 28  %d page %d hash(
ddf20 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20  %08x)\n",.      
ddf30 20 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44           PAGERID
ddf40 28 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20  (pPager), pgno, 
ddf50 70 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 70  pager_datahash(p
ddf60 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c  Pager->pageSize,
ddf70 20 61 44 61 74 61 29 29 3b 0a 20 20 69 66 28 20   aData));.  if( 
ddf80 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50  pPager->state>=P
ddf90 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 26  AGER_EXCLUSIVE &
ddfa0 26 20 28 70 50 67 3d 3d 30 20 7c 7c 20 70 50 67  & (pPg==0 || pPg
ddfb0 2d 3e 6e 65 65 64 53 79 6e 63 3d 3d 30 29 0a 20  ->needSync==0). 
ddfc0 20 20 20 20 20 20 20 26 26 20 70 50 61 67 65 72         && pPager
ddfd0 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29  ->fd->pMethods )
ddfe0 7b 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65 74  {.    i64 offset
ddff0 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34   = (pgno-1)*(i64
de000 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a  )pPager->pageSiz
de010 65 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  e;.    rc = sqli
de020 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65  te3OsWrite(pPage
de030 72 2d 3e 66 64 2c 20 61 44 61 74 61 2c 20 70 50  r->fd, aData, pP
de040 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20  ager->pageSize, 
de050 6f 66 66 73 65 74 29 3b 0a 20 20 20 20 69 66 28  offset);.    if(
de060 20 70 50 67 20 29 7b 0a 20 20 20 20 20 20 6d 61   pPg ){.      ma
de070 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20  keClean(pPg);.  
de080 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 50    }.  }.  if( pP
de090 67 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 20 70  g ){.    /* No p
de0a0 61 67 65 20 73 68 6f 75 6c 64 20 65 76 65 72 20  age should ever 
de0b0 62 65 20 65 78 70 6c 69 63 69 74 6c 79 20 72 6f  be explicitly ro
de0c0 6c 6c 65 64 20 62 61 63 6b 20 74 68 61 74 20 69  lled back that i
de0d0 73 20 69 6e 20 75 73 65 2c 20 65 78 63 65 70 74  s in use, except
de0e0 0a 20 20 20 20 2a 2a 20 66 6f 72 20 70 61 67 65  .    ** for page
de0f0 20 31 20 77 68 69 63 68 20 69 73 20 68 65 6c 64   1 which is held
de100 20 69 6e 20 75 73 65 20 69 6e 20 6f 72 64 65 72   in use in order
de110 20 74 6f 20 6b 65 65 70 20 74 68 65 20 6c 6f 63   to keep the loc
de120 6b 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20  k on the.    ** 
de130 64 61 74 61 62 61 73 65 20 61 63 74 69 76 65 2e  database active.
de140 20 48 6f 77 65 76 65 72 20 73 75 63 68 20 61 20   However such a 
de150 70 61 67 65 20 6d 61 79 20 62 65 20 72 6f 6c 6c  page may be roll
de160 65 64 20 62 61 63 6b 20 61 73 20 61 20 72 65 73  ed back as a res
de170 75 6c 74 0a 20 20 20 20 2a 2a 20 6f 66 20 61 6e  ult.    ** of an
de180 20 69 6e 74 65 72 6e 61 6c 20 65 72 72 6f 72 20   internal error 
de190 72 65 73 75 6c 74 69 6e 67 20 69 6e 20 61 6e 20  resulting in an 
de1a0 61 75 74 6f 6d 61 74 69 63 20 63 61 6c 6c 20 74  automatic call t
de1b0 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33  o.    ** sqlite3
de1c0 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29 2e  PagerRollback().
de1d0 0a 20 20 20 20 2a 2f 0a 20 20 20 20 76 6f 69 64  .    */.    void
de1e0 20 2a 70 44 61 74 61 3b 0a 20 20 20 20 2f 2a 20   *pData;.    /* 
de1f0 61 73 73 65 72 74 28 20 70 50 67 2d 3e 6e 52 65  assert( pPg->nRe
de200 66 3d 3d 30 20 7c 7c 20 70 50 67 2d 3e 70 67 6e  f==0 || pPg->pgn
de210 6f 3d 3d 31 20 29 3b 20 2a 2f 0a 20 20 20 20 70  o==1 ); */.    p
de220 44 61 74 61 20 3d 20 50 47 48 44 52 5f 54 4f 5f  Data = PGHDR_TO_
de230 44 41 54 41 28 70 50 67 29 3b 0a 20 20 20 20 6d  DATA(pPg);.    m
de240 65 6d 63 70 79 28 70 44 61 74 61 2c 20 61 44 61  emcpy(pData, aDa
de250 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65  ta, pPager->page
de260 53 69 7a 65 29 3b 0a 20 20 20 20 69 66 28 20 70  Size);.    if( p
de270 50 61 67 65 72 2d 3e 78 52 65 69 6e 69 74 65 72  Pager->xReiniter
de280 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72   ){.      pPager
de290 2d 3e 78 52 65 69 6e 69 74 65 72 28 70 50 67 2c  ->xReiniter(pPg,
de2a0 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
de2b0 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66  e);.    }.#ifdef
de2c0 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41   SQLITE_CHECK_PA
de2d0 47 45 53 0a 20 20 20 20 70 50 67 2d 3e 70 61 67  GES.    pPg->pag
de2e0 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70 61  eHash = pager_pa
de2f0 67 65 68 61 73 68 28 70 50 67 29 3b 0a 23 65 6e  gehash(pPg);.#en
de300 64 69 66 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  dif.    /* If th
de310 69 73 20 77 61 73 20 70 61 67 65 20 31 2c 20 74  is was page 1, t
de320 68 65 6e 20 72 65 73 74 6f 72 65 20 74 68 65 20  hen restore the 
de330 76 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e 64  value of Pager.d
de340 62 46 69 6c 65 56 65 72 73 2e 0a 20 20 20 20 2a  bFileVers..    *
de350 2a 20 44 6f 20 74 68 69 73 20 62 65 66 6f 72 65  * Do this before
de360 20 61 6e 79 20 64 65 63 6f 64 69 6e 67 2e 20 2a   any decoding. *
de370 2f 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d  /.    if( pgno==
de380 31 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  1 ){.      memcp
de390 79 28 26 70 50 61 67 65 72 2d 3e 64 62 46 69 6c  y(&pPager->dbFil
de3a0 65 56 65 72 73 2c 20 26 28 28 75 38 2a 29 70 44  eVers, &((u8*)pD
de3b0 61 74 61 29 5b 32 34 5d 2c 73 69 7a 65 6f 66 28  ata)[24],sizeof(
de3c0 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65  pPager->dbFileVe
de3d0 72 73 29 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  rs));.    }..   
de3e0 20 2f 2a 20 44 65 63 6f 64 65 20 74 68 65 20 70   /* Decode the p
de3f0 61 67 65 20 6a 75 73 74 20 72 65 61 64 20 66 72  age just read fr
de400 6f 6d 20 64 69 73 6b 20 2a 2f 0a 20 20 20 20 43  om disk */.    C
de410 4f 44 45 43 31 28 70 50 61 67 65 72 2c 20 70 44  ODEC1(pPager, pD
de420 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20  ata, pPg->pgno, 
de430 33 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  3);.  }.  return
de440 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61   rc;.}../*.** Pa
de450 72 61 6d 65 74 65 72 20 7a 4d 61 73 74 65 72 20  rameter zMaster 
de460 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61  is the name of a
de470 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
de480 66 69 6c 65 2e 20 41 20 73 69 6e 67 6c 65 20 6a  file. A single j
de490 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 74  ournal.** file t
de4a0 68 61 74 20 72 65 66 65 72 72 65 64 20 74 6f 20  hat referred to 
de4b0 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
de4c0 61 6c 20 66 69 6c 65 20 68 61 73 20 6a 75 73 74  al file has just
de4d0 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61 63   been rolled bac
de4e0 6b 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  k..** This routi
de4f0 6e 65 20 63 68 65 63 6b 73 20 69 66 20 69 74 20  ne checks if it 
de500 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 64  is possible to d
de510 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72  elete the master
de520 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 0a 2a   journal file,.*
de530 2a 20 61 6e 64 20 64 6f 65 73 20 73 6f 20 69 66  * and does so if
de540 20 69 74 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 41 72   it is..**.** Ar
de550 67 75 6d 65 6e 74 20 7a 4d 61 73 74 65 72 20 6d  gument zMaster m
de560 61 79 20 70 6f 69 6e 74 20 74 6f 20 50 61 67 65  ay point to Page
de570 72 2e 70 54 6d 70 53 70 61 63 65 2e 20 53 6f 20  r.pTmpSpace. So 
de580 74 68 61 74 20 62 75 66 66 65 72 20 69 73 20 6e  that buffer is n
de590 6f 74 20 0a 2a 2a 20 61 76 61 69 6c 61 62 6c 65  ot .** available
de5a0 20 66 6f 72 20 75 73 65 20 77 69 74 68 69 6e 20   for use within 
de5b0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  this function..*
de5c0 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 73 74  *.**.** The mast
de5d0 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
de5e0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 61 6d  contains the nam
de5f0 65 73 20 6f 66 20 61 6c 6c 20 63 68 69 6c 64 20  es of all child 
de600 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2a 20 54 6f 20  journals..** To 
de610 74 65 6c 6c 20 69 66 20 61 20 6d 61 73 74 65 72  tell if a master
de620 20 6a 6f 75 72 6e 61 6c 20 63 61 6e 20 62 65 20   journal can be 
de630 64 65 6c 65 74 65 64 2c 20 63 68 65 63 6b 20 74  deleted, check t
de640 6f 20 65 61 63 68 20 6f 66 20 74 68 65 0a 2a 2a  o each of the.**
de650 20 63 68 69 6c 64 72 65 6e 2e 20 20 49 66 20 61   children.  If a
de660 6c 6c 20 63 68 69 6c 64 72 65 6e 20 61 72 65 20  ll children are 
de670 65 69 74 68 65 72 20 6d 69 73 73 69 6e 67 20 6f  either missing o
de680 72 20 64 6f 20 6e 6f 74 20 72 65 66 65 72 20 74  r do not refer t
de690 6f 0a 2a 2a 20 61 20 64 69 66 66 65 72 65 6e 74  o.** a different
de6a0 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2c   master journal,
de6b0 20 74 68 65 6e 20 74 68 69 73 20 6d 61 73 74 65   then this maste
de6c0 72 20 6a 6f 75 72 6e 61 6c 20 63 61 6e 20 62 65  r journal can be
de6d0 20 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a 73 74 61   deleted..*/.sta
de6e0 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 64 65  tic int pager_de
de6f0 6c 6d 61 73 74 65 72 28 50 61 67 65 72 20 2a 70  lmaster(Pager *p
de700 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 63 68 61  Pager, const cha
de710 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 73  r *zMaster){.  s
de720 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
de730 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b   = pPager->pVfs;
de740 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74  .  int rc;.  int
de750 20 6d 61 73 74 65 72 5f 6f 70 65 6e 20 3d 20 30   master_open = 0
de760 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ;.  sqlite3_file
de770 20 2a 70 4d 61 73 74 65 72 3b 0a 20 20 73 71 6c   *pMaster;.  sql
de780 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 6f 75 72  ite3_file *pJour
de790 6e 61 6c 3b 0a 20 20 63 68 61 72 20 2a 7a 4d 61  nal;.  char *zMa
de7a0 73 74 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b  sterJournal = 0;
de7b0 20 2f 2a 20 43 6f 6e 74 65 6e 74 73 20 6f 66 20   /* Contents of 
de7c0 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
de7d0 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6e 4d 61  ile */.  i64 nMa
de7e0 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 20 20 20 20  sterJournal;    
de7f0 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6d 61     /* Size of ma
de800 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
de810 65 20 2a 2f 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20  e */..  /* Open 
de820 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
de830 61 6c 20 66 69 6c 65 20 65 78 63 6c 75 73 69 76  al file exclusiv
de840 65 6c 79 20 69 6e 20 63 61 73 65 20 73 6f 6d 65  ely in case some
de850 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 0a 20   other process. 
de860 20 2a 2a 20 69 73 20 72 75 6e 6e 69 6e 67 20 74   ** is running t
de870 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f  his routine also
de880 2e 20 4e 6f 74 20 74 68 61 74 20 69 74 20 6d 61  . Not that it ma
de890 6b 65 73 20 74 6f 6f 20 6d 75 63 68 20 64 69 66  kes too much dif
de8a0 66 65 72 65 6e 63 65 2e 0a 20 20 2a 2f 0a 20 20  ference..  */.  
de8b0 70 4d 61 73 74 65 72 20 3d 20 28 73 71 6c 69 74  pMaster = (sqlit
de8c0 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74 65  e3_file *)sqlite
de8d0 33 5f 6d 61 6c 6c 6f 63 28 70 56 66 73 2d 3e 73  3_malloc(pVfs->s
de8e0 7a 4f 73 46 69 6c 65 20 2a 20 32 29 3b 0a 20 20  zOsFile * 2);.  
de8f0 70 4a 6f 75 72 6e 61 6c 20 3d 20 28 73 71 6c 69  pJournal = (sqli
de900 74 65 33 5f 66 69 6c 65 20 2a 29 28 28 28 75 38  te3_file *)(((u8
de910 20 2a 29 70 4d 61 73 74 65 72 29 20 2b 20 70 56   *)pMaster) + pV
de920 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a 20  fs->szOsFile);. 
de930 20 69 66 28 20 21 70 4d 61 73 74 65 72 20 29 7b   if( !pMaster ){
de940 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
de950 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b  _NOMEM;.  }else{
de960 0a 20 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d  .    int flags =
de970 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45   (SQLITE_OPEN_RE
de980 41 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50  ADONLY|SQLITE_OP
de990 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41  EN_MASTER_JOURNA
de9a0 4c 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  L);.    rc = sql
de9b0 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c  ite3OsOpen(pVfs,
de9c0 20 7a 4d 61 73 74 65 72 2c 20 70 4d 61 73 74 65   zMaster, pMaste
de9d0 72 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20 20  r, flags, 0);.  
de9e0 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  }.  if( rc!=SQLI
de9f0 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c  TE_OK ) goto del
dea00 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 6d 61  master_out;.  ma
dea10 73 74 65 72 5f 6f 70 65 6e 20 3d 20 31 3b 0a 0a  ster_open = 1;..
dea20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
dea30 46 69 6c 65 53 69 7a 65 28 70 4d 61 73 74 65 72  FileSize(pMaster
dea40 2c 20 26 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61  , &nMasterJourna
dea50 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  l);.  if( rc!=SQ
dea60 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 64  LITE_OK ) goto d
dea70 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 0a 20  elmaster_out;.. 
dea80 20 69 66 28 20 6e 4d 61 73 74 65 72 4a 6f 75 72   if( nMasterJour
dea90 6e 61 6c 3e 30 20 29 7b 0a 20 20 20 20 63 68 61  nal>0 ){.    cha
deaa0 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20  r *zJournal;.   
deab0 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 50 74   char *zMasterPt
deac0 72 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 6e  r = 0;.    int n
dead0 4d 61 73 74 65 72 50 74 72 20 3d 20 70 50 61 67  MasterPtr = pPag
deae0 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68  er->pVfs->mxPath
deaf0 6e 61 6d 65 2b 31 3b 0a 0a 20 20 20 20 2f 2a 20  name+1;..    /* 
deb00 4c 6f 61 64 20 74 68 65 20 65 6e 74 69 72 65 20  Load the entire 
deb10 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
deb20 69 6c 65 20 69 6e 74 6f 20 73 70 61 63 65 20 6f  ile into space o
deb30 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 20 20 20  btained from.   
deb40 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c   ** sqlite3_mall
deb50 6f 63 28 29 20 61 6e 64 20 70 6f 69 6e 74 65 64  oc() and pointed
deb60 20 74 6f 20 62 79 20 7a 4d 61 73 74 65 72 4a 6f   to by zMasterJo
deb70 75 72 6e 61 6c 2e 20 0a 20 20 20 20 2a 2f 0a 20  urnal. .    */. 
deb80 20 20 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61     zMasterJourna
deb90 6c 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69  l = (char *)sqli
deba0 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 4d 61 73 74  te3_malloc(nMast
debb0 65 72 4a 6f 75 72 6e 61 6c 20 2b 20 6e 4d 61 73  erJournal + nMas
debc0 74 65 72 50 74 72 29 3b 0a 20 20 20 20 69 66 28  terPtr);.    if(
debd0 20 21 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c   !zMasterJournal
debe0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
debf0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
dec00 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65     goto delmaste
dec10 72 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20  r_out;.    }.   
dec20 20 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 26 7a   zMasterPtr = &z
dec30 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 5b 6e 4d  MasterJournal[nM
dec40 61 73 74 65 72 4a 6f 75 72 6e 61 6c 5d 3b 0a 20  asterJournal];. 
dec50 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
dec60 73 52 65 61 64 28 70 4d 61 73 74 65 72 2c 20 7a  sRead(pMaster, z
dec70 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2c 20 6e  MasterJournal, n
dec80 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2c 20 30  MasterJournal, 0
dec90 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
deca0 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
decb0 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 0a  delmaster_out;..
decc0 20 20 20 20 7a 4a 6f 75 72 6e 61 6c 20 3d 20 7a      zJournal = z
decd0 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 0a 20  MasterJournal;. 
dece0 20 20 20 77 68 69 6c 65 28 20 28 7a 4a 6f 75 72     while( (zJour
decf0 6e 61 6c 2d 7a 4d 61 73 74 65 72 4a 6f 75 72 6e  nal-zMasterJourn
ded00 61 6c 29 3c 6e 4d 61 73 74 65 72 4a 6f 75 72 6e  al)<nMasterJourn
ded10 61 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  al ){.      rc =
ded20 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73   sqlite3OsAccess
ded30 28 70 56 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c  (pVfs, zJournal,
ded40 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45   SQLITE_ACCESS_E
ded50 58 49 53 54 53 29 3b 0a 20 20 20 20 20 20 69 66  XISTS);.      if
ded60 28 20 72 63 21 3d 30 20 26 26 20 72 63 21 3d 31  ( rc!=0 && rc!=1
ded70 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
ded80 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f   SQLITE_IOERR_NO
ded90 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 67 6f 74  MEM;.        got
deda0 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b  o delmaster_out;
dedb0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
dedc0 66 28 20 72 63 3d 3d 31 20 29 7b 0a 20 20 20 20  f( rc==1 ){.    
dedd0 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68      /* One of th
dede0 65 20 6a 6f 75 72 6e 61 6c 73 20 70 6f 69 6e 74  e journals point
dedf0 65 64 20 74 6f 20 62 79 20 74 68 65 20 6d 61 73  ed to by the mas
dee00 74 65 72 20 6a 6f 75 72 6e 61 6c 20 65 78 69 73  ter journal exis
dee10 74 73 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f  ts..        ** O
dee20 70 65 6e 20 69 74 20 61 6e 64 20 63 68 65 63 6b  pen it and check
dee30 20 69 66 20 69 74 20 70 6f 69 6e 74 73 20 61 74   if it points at
dee40 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
dee50 6e 61 6c 2e 20 49 66 0a 20 20 20 20 20 20 20 20  nal. If.        
dee60 2a 2a 20 73 6f 2c 20 72 65 74 75 72 6e 20 77 69  ** so, return wi
dee70 74 68 6f 75 74 20 64 65 6c 65 74 69 6e 67 20 74  thout deleting t
dee80 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
dee90 6c 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 20 20  l file..        
deea0 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 63  */.        int c
deeb0 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 66 6c  ;.        int fl
deec0 61 67 73 20 3d 20 28 53 51 4c 49 54 45 5f 4f 50  ags = (SQLITE_OP
deed0 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c 49  EN_READONLY|SQLI
deee0 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55  TE_OPEN_MAIN_JOU
deef0 52 4e 41 4c 29 3b 0a 20 20 20 20 20 20 20 20 72  RNAL);.        r
def00 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  c = sqlite3OsOpe
def10 6e 28 70 56 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c  n(pVfs, zJournal
def20 2c 20 70 4a 6f 75 72 6e 61 6c 2c 20 66 6c 61 67  , pJournal, flag
def30 73 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69  s, 0);.        i
def40 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
def50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f   ){.          go
def60 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74  to delmaster_out
def70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20  ;.        }..   
def80 20 20 20 20 20 72 63 20 3d 20 72 65 61 64 4d 61       rc = readMa
def90 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 4a 6f 75  sterJournal(pJou
defa0 72 6e 61 6c 2c 20 7a 4d 61 73 74 65 72 50 74 72  rnal, zMasterPtr
defb0 2c 20 6e 4d 61 73 74 65 72 50 74 72 29 3b 0a 20  , nMasterPtr);. 
defc0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73         sqlite3Os
defd0 43 6c 6f 73 65 28 70 4a 6f 75 72 6e 61 6c 29 3b  Close(pJournal);
defe0 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
deff0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
df000 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c          goto del
df010 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20  master_out;.    
df020 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 63      }..        c
df030 20 3d 20 7a 4d 61 73 74 65 72 50 74 72 5b 30 5d   = zMasterPtr[0]
df040 21 3d 30 20 26 26 20 73 74 72 63 6d 70 28 7a 4d  !=0 && strcmp(zM
df050 61 73 74 65 72 50 74 72 2c 20 7a 4d 61 73 74 65  asterPtr, zMaste
df060 72 29 3d 3d 30 3b 0a 20 20 20 20 20 20 20 20 69  r)==0;.        i
df070 66 28 20 63 20 29 7b 0a 20 20 20 20 20 20 20 20  f( c ){.        
df080 20 20 2f 2a 20 57 65 20 68 61 76 65 20 61 20 6d    /* We have a m
df090 61 74 63 68 2e 20 44 6f 20 6e 6f 74 20 64 65 6c  atch. Do not del
df0a0 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a  ete the master j
df0b0 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 2a 2f 0a  ournal file. */.
df0c0 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64            goto d
df0d0 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20  elmaster_out;.  
df0e0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
df0f0 20 20 20 20 20 20 7a 4a 6f 75 72 6e 61 6c 20 2b        zJournal +
df100 3d 20 28 73 74 72 6c 65 6e 28 7a 4a 6f 75 72 6e  = (strlen(zJourn
df110 61 6c 29 2b 31 29 3b 0a 20 20 20 20 7d 0a 20 20  al)+1);.    }.  
df120 7d 0a 20 20 0a 20 20 72 63 20 3d 20 73 71 6c 69  }.  .  rc = sqli
df130 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73  te3OsDelete(pVfs
df140 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 0a  , zMaster, 0);..
df150 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3a 0a 20  delmaster_out:. 
df160 20 69 66 28 20 7a 4d 61 73 74 65 72 4a 6f 75 72   if( zMasterJour
df170 6e 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  nal ){.    sqlit
df180 65 33 5f 66 72 65 65 28 7a 4d 61 73 74 65 72 4a  e3_free(zMasterJ
df190 6f 75 72 6e 61 6c 29 3b 0a 20 20 7d 20 20 0a 20  ournal);.  }  . 
df1a0 20 69 66 28 20 6d 61 73 74 65 72 5f 6f 70 65 6e   if( master_open
df1b0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f   ){.    sqlite3O
df1c0 73 43 6c 6f 73 65 28 70 4d 61 73 74 65 72 29 3b  sClose(pMaster);
df1d0 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66  .  }.  sqlite3_f
df1e0 72 65 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20  ree(pMaster);.  
df1f0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 73  return rc;.}...s
df200 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72  tatic void pager
df210 5f 74 72 75 6e 63 61 74 65 5f 63 61 63 68 65 28  _truncate_cache(
df220 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 3b 0a  Pager *pPager);.
df230 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20  ./*.** Truncate 
df240 74 68 65 20 6d 61 69 6e 20 66 69 6c 65 20 6f 66  the main file of
df250 20 74 68 65 20 67 69 76 65 6e 20 70 61 67 65 72   the given pager
df260 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   to the number o
df270 66 20 70 61 67 65 73 0a 2a 2a 20 69 6e 64 69 63  f pages.** indic
df280 61 74 65 64 2e 20 41 6c 73 6f 20 74 72 75 6e 63  ated. Also trunc
df290 61 74 65 20 74 68 65 20 63 61 63 68 65 64 20 72  ate the cached r
df2a0 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
df2b0 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a   the file..**.**
df2c0 20 4d 69 67 68 74 20 6d 69 67 68 74 20 62 65 20   Might might be 
df2d0 74 68 65 20 63 61 73 65 20 74 68 61 74 20 74 68  the case that th
df2e0 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 20 69  e file on disk i
df2f0 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 6e  s smaller than n
df300 50 61 67 65 2e 0a 2a 2a 20 54 68 69 73 20 63 61  Page..** This ca
df310 6e 20 68 61 70 70 65 6e 2c 20 66 6f 72 20 65 78  n happen, for ex
df320 61 6d 70 6c 65 2c 20 69 66 20 77 65 20 61 72 65  ample, if we are
df330 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
df340 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a  f a transaction.
df350 2a 2a 20 77 68 69 63 68 20 68 61 73 20 65 78 74  ** which has ext
df360 65 6e 64 65 64 20 74 68 65 20 66 69 6c 65 20 73  ended the file s
df370 69 7a 65 20 61 6e 64 20 74 68 65 20 6e 65 77 20  ize and the new 
df380 70 61 67 65 73 20 61 72 65 20 73 74 69 6c 6c 20  pages are still 
df390 61 6c 6c 20 68 65 6c 64 0a 2a 2a 20 69 6e 20 63  all held.** in c
df3a0 61 63 68 65 2c 20 74 68 65 6e 20 61 6e 20 49 4e  ache, then an IN
df3b0 53 45 52 54 20 6f 72 20 55 50 44 41 54 45 20 64  SERT or UPDATE d
df3c0 6f 65 73 20 61 20 73 74 61 74 65 6d 65 6e 74 20  oes a statement 
df3d0 72 6f 6c 6c 62 61 63 6b 2e 20 20 53 6f 6d 65 0a  rollback.  Some.
df3e0 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  ** operating sys
df3f0 74 65 6d 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  tem implementati
df400 6f 6e 73 20 63 61 6e 20 67 65 74 20 63 6f 6e 66  ons can get conf
df410 75 73 65 64 20 69 66 20 79 6f 75 20 74 72 79 20  used if you try 
df420 74 6f 0a 2a 2a 20 74 72 75 6e 63 61 74 65 20 61  to.** truncate a
df430 20 66 69 6c 65 20 74 6f 20 73 6f 6d 65 20 73 69   file to some si
df440 7a 65 20 74 68 61 74 20 69 73 20 6c 61 72 67 65  ze that is large
df450 72 20 74 68 61 6e 20 69 74 20 63 75 72 72 65 6e  r than it curren
df460 74 6c 79 20 69 73 2c 0a 2a 2a 20 73 6f 20 64 65  tly is,.** so de
df470 74 65 63 74 20 74 68 69 73 20 63 61 73 65 20 61  tect this case a
df480 6e 64 20 77 72 69 74 65 20 61 20 73 69 6e 67 6c  nd write a singl
df490 65 20 7a 65 72 6f 20 62 79 74 65 20 74 6f 20 74  e zero byte to t
df4a0 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6e 65  he end of the ne
df4b0 77 0a 2a 2a 20 66 69 6c 65 20 69 6e 73 74 65 61  w.** file instea
df4c0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
df4d0 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28   pager_truncate(
df4e0 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
df4f0 6e 74 20 6e 50 61 67 65 29 7b 0a 20 20 69 6e 74  nt nPage){.  int
df500 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
df510 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  .  if( pPager->s
df520 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c  tate>=PAGER_EXCL
df530 55 53 49 56 45 20 26 26 20 70 50 61 67 65 72 2d  USIVE && pPager-
df540 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b  >fd->pMethods ){
df550 0a 20 20 20 20 69 36 34 20 63 75 72 72 65 6e 74  .    i64 current
df560 53 69 7a 65 2c 20 6e 65 77 53 69 7a 65 3b 0a 20  Size, newSize;. 
df570 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
df580 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72  sFileSize(pPager
df590 2d 3e 66 64 2c 20 26 63 75 72 72 65 6e 74 53 69  ->fd, &currentSi
df5a0 7a 65 29 3b 0a 20 20 20 20 6e 65 77 53 69 7a 65  ze);.    newSize
df5b0 20 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53   = pPager->pageS
df5c0 69 7a 65 2a 28 69 36 34 29 6e 50 61 67 65 3b 0a  ize*(i64)nPage;.
df5d0 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
df5e0 54 45 5f 4f 4b 20 26 26 20 63 75 72 72 65 6e 74  TE_OK && current
df5f0 53 69 7a 65 21 3d 6e 65 77 53 69 7a 65 20 29 7b  Size!=newSize ){
df600 0a 20 20 20 20 20 20 69 66 28 20 63 75 72 72 65  .      if( curre
df610 6e 74 53 69 7a 65 3e 6e 65 77 53 69 7a 65 20 29  ntSize>newSize )
df620 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
df630 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65  qlite3OsTruncate
df640 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 6e 65 77  (pPager->fd, new
df650 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c  Size);.      }el
df660 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  se{.        rc =
df670 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
df680 70 50 61 67 65 72 2d 3e 66 64 2c 20 22 22 2c 20  pPager->fd, "", 
df690 31 2c 20 6e 65 77 53 69 7a 65 2d 31 29 3b 0a 20  1, newSize-1);. 
df6a0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
df6b0 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
df6c0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 67  E_OK ){.    pPag
df6d0 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6e 50 61  er->dbSize = nPa
df6e0 67 65 3b 0a 20 20 20 20 70 61 67 65 72 5f 74 72  ge;.    pager_tr
df6f0 75 6e 63 61 74 65 5f 63 61 63 68 65 28 70 50 61  uncate_cache(pPa
df700 67 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ger);.  }.  retu
df710 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
df720 53 65 74 20 74 68 65 20 73 65 63 74 6f 72 53 69  Set the sectorSi
df730 7a 65 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e  ze for the given
df740 20 70 61 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68   pager..**.** Th
df750 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 73  e sector size is
df760 20 61 74 20 6c 65 61 73 74 20 61 73 20 62 69 67   at least as big
df770 20 61 73 20 74 68 65 20 73 65 63 74 6f 72 20 73   as the sector s
df780 69 7a 65 20 72 65 70 6f 72 74 65 64 0a 2a 2a 20  ize reported.** 
df790 62 79 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74  by sqlite3OsSect
df7a0 6f 72 53 69 7a 65 28 29 2e 20 20 54 68 65 20 6d  orSize().  The m
df7b0 69 6e 69 6d 75 6d 20 73 65 63 74 6f 72 20 73 69  inimum sector si
df7c0 7a 65 20 69 73 20 35 31 32 2e 0a 2a 2f 0a 73 74  ze is 512..*/.st
df7d0 61 74 69 63 20 76 6f 69 64 20 73 65 74 53 65 63  atic void setSec
df7e0 74 6f 72 53 69 7a 65 28 50 61 67 65 72 20 2a 70  torSize(Pager *p
df7f0 50 61 67 65 72 29 7b 0a 20 20 61 73 73 65 72 74  Pager){.  assert
df800 28 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65  (pPager->fd->pMe
df810 74 68 6f 64 73 7c 7c 70 50 61 67 65 72 2d 3e 74  thods||pPager->t
df820 65 6d 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20  empFile);.  if( 
df830 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c  !pPager->tempFil
df840 65 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65 63 74  e ){.    /* Sect
df850 6f 72 20 73 69 7a 65 20 64 6f 65 73 6e 27 74 20  or size doesn't 
df860 6d 61 74 74 65 72 20 66 6f 72 20 74 65 6d 70 6f  matter for tempo
df870 72 61 72 79 20 66 69 6c 65 73 2e 20 41 6c 73 6f  rary files. Also
df880 2c 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a  , the file.    *
df890 2a 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 62  * may not have b
df8a0 65 65 6e 20 6f 70 65 6e 65 64 20 79 65 74 2c 20  een opened yet, 
df8b0 69 6e 20 77 68 63 69 68 20 63 61 73 65 20 74 68  in whcih case th
df8c0 65 20 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29  e OsSectorSize()
df8d0 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 77 69 6c  .    ** call wil
df8e0 6c 20 73 65 67 66 61 75 6c 74 2e 0a 20 20 20 20  l segfault..    
df8f0 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73  */.    pPager->s
df900 65 63 74 6f 72 53 69 7a 65 20 3d 20 73 71 6c 69  ectorSize = sqli
df910 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28  te3OsSectorSize(
df920 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 7d  pPager->fd);.  }
df930 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  .  if( pPager->s
df940 65 63 74 6f 72 53 69 7a 65 3c 35 31 32 20 29 7b  ectorSize<512 ){
df950 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63  .    pPager->sec
df960 74 6f 72 53 69 7a 65 20 3d 20 35 31 32 3b 0a 20  torSize = 512;. 
df970 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79   }.}../*.** Play
df980 62 61 63 6b 20 74 68 65 20 6a 6f 75 72 6e 61 6c  back the journal
df990 20 61 6e 64 20 74 68 75 73 20 72 65 73 74 6f 72   and thus restor
df9a0 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  e the database f
df9b0 69 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 20 73 74  ile to.** the st
df9c0 61 74 65 20 69 74 20 77 61 73 20 69 6e 20 62 65  ate it was in be
df9d0 66 6f 72 65 20 77 65 20 73 74 61 72 74 65 64 20  fore we started 
df9e0 6d 61 6b 69 6e 67 20 63 68 61 6e 67 65 73 2e 20  making changes. 
df9f0 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72   .**.** The jour
dfa00 6e 61 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20  nal file format 
dfa10 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 0a  is as follows: .
dfa20 2a 2a 0a 2a 2a 20 20 28 31 29 20 20 38 20 62 79  **.**  (1)  8 by
dfa30 74 65 20 70 72 65 66 69 78 2e 20 20 41 20 63 6f  te prefix.  A co
dfa40 70 79 20 6f 66 20 61 4a 6f 75 72 6e 61 6c 4d 61  py of aJournalMa
dfa50 67 69 63 5b 5d 2e 0a 2a 2a 20 20 28 32 29 20 20  gic[]..**  (2)  
dfa60 34 20 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61  4 byte big-endia
dfa70 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20  n integer which 
dfa80 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
dfa90 20 76 61 6c 69 64 20 70 61 67 65 20 72 65 63 6f   valid page reco
dfaa0 72 64 73 0a 2a 2a 20 20 20 20 20 20 20 69 6e 20  rds.**       in 
dfab0 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 66  the journal.  If
dfac0 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 20 30   this value is 0
dfad0 78 66 66 66 66 66 66 66 66 2c 20 74 68 65 6e 20  xffffffff, then 
dfae0 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a 20 20  compute the.**  
dfaf0 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 70       number of p
dfb00 61 67 65 20 72 65 63 6f 72 64 73 20 66 72 6f 6d  age records from
dfb10 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 73 69 7a   the journal siz
dfb20 65 2e 0a 2a 2a 20 20 28 33 29 20 20 34 20 62 79  e..**  (3)  4 by
dfb30 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e  te big-endian in
dfb40 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74  teger which is t
dfb50 68 65 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65  he initial value
dfb60 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 20 20 20   for the .**    
dfb70 20 20 20 73 61 6e 69 74 79 20 63 68 65 63 6b 73     sanity checks
dfb80 75 6d 2e 0a 2a 2a 20 20 28 34 29 20 20 34 20 62  um..**  (4)  4 b
dfb90 79 74 65 20 69 6e 74 65 67 65 72 20 77 68 69 63  yte integer whic
dfba0 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  h is the number 
dfbb0 6f 66 20 70 61 67 65 73 20 74 6f 20 74 72 75 6e  of pages to trun
dfbc0 63 61 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 20  cate the.**     
dfbd0 20 20 64 61 74 61 62 61 73 65 20 74 6f 20 64 75    database to du
dfbe0 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2e  ring a rollback.
dfbf0 0a 2a 2a 20 20 28 35 29 20 20 34 20 62 79 74 65  .**  (5)  4 byte
dfc00 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65   big-endian inte
dfc10 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65  ger which is the
dfc20 20 73 65 63 74 6f 72 20 73 69 7a 65 2e 20 20 54   sector size.  T
dfc30 68 65 20 68 65 61 64 65 72 0a 2a 2a 20 20 20 20  he header.**    
dfc40 20 20 20 69 73 20 74 68 69 73 20 6d 61 6e 79 20     is this many 
dfc50 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a  bytes in size..*
dfc60 2a 20 20 28 36 29 20 20 34 20 62 79 74 65 20 62  *  (6)  4 byte b
dfc70 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65  ig-endian intege
dfc80 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 70  r which is the p
dfc90 61 67 65 20 63 61 73 65 2e 0a 2a 2a 20 20 28 37  age case..**  (7
dfca0 29 20 20 34 20 62 79 74 65 20 69 6e 74 65 67 65  )  4 byte intege
dfcb0 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 6e  r which is the n
dfcc0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
dfcd0 6e 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  n the master jou
dfce0 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 6e 61  rnal.**       na
dfcf0 6d 65 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6d  me.  The value m
dfd00 61 79 20 62 65 20 7a 65 72 6f 20 28 69 6e 64 69  ay be zero (indi
dfd10 63 61 74 65 20 74 68 61 74 20 74 68 65 72 65 20  cate that there 
dfd20 69 73 20 6e 6f 20 6d 61 73 74 65 72 0a 2a 2a 20  is no master.** 
dfd30 20 20 20 20 20 20 6a 6f 75 72 6e 61 6c 2e 29 0a        journal.).
dfd40 2a 2a 20 20 28 38 29 20 20 4e 20 62 79 74 65 73  **  (8)  N bytes
dfd50 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a   of the master j
dfd60 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e 20 20 54 68  ournal name.  Th
dfd70 65 20 6e 61 6d 65 20 77 69 6c 6c 20 62 65 20 6e  e name will be n
dfd80 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a  ul-terminated.**
dfd90 20 20 20 20 20 20 20 61 6e 64 20 6d 69 67 68 74         and might
dfda0 20 62 65 20 73 68 6f 72 74 65 72 20 74 68 61 6e   be shorter than
dfdb0 20 74 68 65 20 76 61 6c 75 65 20 72 65 61 64 20   the value read 
dfdc0 66 72 6f 6d 20 28 35 29 2e 20 20 49 66 20 74 68  from (5).  If th
dfdd0 65 20 66 69 72 73 74 20 62 79 74 65 0a 2a 2a 20  e first byte.** 
dfde0 20 20 20 20 20 20 6f 66 20 74 68 65 20 6e 61 6d        of the nam
dfdf0 65 20 69 73 20 5c 30 30 30 20 74 68 65 6e 20 74  e is \000 then t
dfe00 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 73 74 65  here is no maste
dfe10 72 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20  r journal.  The 
dfe20 6d 61 73 74 65 72 0a 2a 2a 20 20 20 20 20 20 20  master.**       
dfe30 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 73 20  journal name is 
dfe40 73 74 6f 72 65 64 20 69 6e 20 55 54 46 2d 38 2e  stored in UTF-8.
dfe50 0a 2a 2a 20 20 28 39 29 20 20 5a 65 72 6f 20 6f  .**  (9)  Zero o
dfe60 72 20 6d 6f 72 65 20 70 61 67 65 73 20 69 6e 73  r more pages ins
dfe70 74 61 6e 63 65 73 2c 20 65 61 63 68 20 61 73 20  tances, each as 
dfe80 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 20 20 20 20  follows:.**     
dfe90 20 20 20 2b 20 20 34 20 62 79 74 65 20 70 61 67     +  4 byte pag
dfea0 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 20 20 20  e number..**    
dfeb0 20 20 20 20 2b 20 20 70 50 61 67 65 72 2d 3e 70      +  pPager->p
dfec0 61 67 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66  ageSize bytes of
dfed0 20 64 61 74 61 2e 0a 2a 2a 20 20 20 20 20 20 20   data..**       
dfee0 20 2b 20 20 34 20 62 79 74 65 20 63 68 65 63 6b   +  4 byte check
dfef0 73 75 6d 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77  sum.**.** When w
dff00 65 20 73 70 65 61 6b 20 6f 66 20 74 68 65 20 6a  e speak of the j
dff10 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2c 20 77  ournal header, w
dff20 65 20 6d 65 61 6e 20 74 68 65 20 66 69 72 73 74  e mean the first
dff30 20 38 20 69 74 65 6d 73 20 61 62 6f 76 65 2e 0a   8 items above..
dff40 2a 2a 20 45 61 63 68 20 65 6e 74 72 79 20 69 6e  ** Each entry in
dff50 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20   the journal is 
dff60 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
dff70 68 65 20 39 74 68 20 69 74 65 6d 2e 0a 2a 2a 0a  he 9th item..**.
dff80 2a 2a 20 43 61 6c 6c 20 74 68 65 20 76 61 6c 75  ** Call the valu
dff90 65 20 66 72 6f 6d 20 74 68 65 20 73 65 63 6f 6e  e from the secon
dffa0 64 20 62 75 6c 6c 65 74 20 22 6e 52 65 63 22 2e  d bullet "nRec".
dffb0 20 20 6e 52 65 63 20 69 73 20 74 68 65 20 6e 75    nRec is the nu
dffc0 6d 62 65 72 20 6f 66 0a 2a 2a 20 76 61 6c 69 64  mber of.** valid
dffd0 20 70 61 67 65 20 65 6e 74 72 69 65 73 20 69 6e   page entries in
dffe0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49   the journal.  I
dfff0 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c 20 79 6f  n most cases, yo
e0000 75 20 63 61 6e 20 63 6f 6d 70 75 74 65 20 74 68  u can compute th
e0010 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6e 52  e.** value of nR
e0020 65 63 20 66 72 6f 6d 20 74 68 65 20 73 69 7a 65  ec from the size
e0030 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
e0040 66 69 6c 65 2e 20 20 42 75 74 20 69 66 20 61 20  file.  But if a 
e0050 70 6f 77 65 72 0a 2a 2a 20 66 61 69 6c 75 72 65  power.** failure
e0060 20 6f 63 63 75 72 72 65 64 20 77 68 69 6c 65 20   occurred while 
e0070 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20  the journal was 
e0080 62 65 69 6e 67 20 77 72 69 74 74 65 6e 2c 20 69  being written, i
e0090 74 20 63 6f 75 6c 64 20 62 65 20 74 68 65 0a 2a  t could be the.*
e00a0 2a 20 63 61 73 65 20 74 68 61 74 20 74 68 65 20  * case that the 
e00b0 73 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72  size of the jour
e00c0 6e 61 6c 20 66 69 6c 65 20 68 61 64 20 61 6c 72  nal file had alr
e00d0 65 61 64 79 20 62 65 65 6e 20 69 6e 63 72 65 61  eady been increa
e00e0 73 65 64 20 62 75 74 0a 2a 2a 20 74 68 65 20 65  sed but.** the e
e00f0 78 74 72 61 20 65 6e 74 72 69 65 73 20 68 61 64  xtra entries had
e0100 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 69 74   not yet made it
e0110 20 73 61 66 65 6c 79 20 74 6f 20 64 69 73 6b 2e   safely to disk.
e0120 20 20 49 6e 20 73 75 63 68 20 61 20 63 61 73 65    In such a case
e0130 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f  ,.** the value o
e0140 66 20 6e 52 65 63 20 63 6f 6d 70 75 74 65 64 20  f nRec computed 
e0150 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 20 73 69  from the file si
e0160 7a 65 20 77 6f 75 6c 64 20 62 65 20 74 6f 6f 20  ze would be too 
e0170 6c 61 72 67 65 2e 20 20 46 6f 72 0a 2a 2a 20 74  large.  For.** t
e0180 68 61 74 20 72 65 61 73 6f 6e 2c 20 77 65 20 61  hat reason, we a
e0190 6c 77 61 79 73 20 75 73 65 20 74 68 65 20 6e 52  lways use the nR
e01a0 65 63 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20  ec value in the 
e01b0 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66  header..**.** If
e01c0 20 74 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20   the nRec value 
e01d0 69 73 20 30 78 66 66 66 66 66 66 66 66 20 69 74  is 0xffffffff it
e01e0 20 6d 65 61 6e 73 20 74 68 61 74 20 6e 52 65 63   means that nRec
e01f0 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75   should be compu
e0200 74 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20  ted.** from the 
e0210 66 69 6c 65 20 73 69 7a 65 2e 20 20 54 68 69 73  file size.  This
e0220 20 76 61 6c 75 65 20 69 73 20 75 73 65 64 20 77   value is used w
e0230 68 65 6e 20 74 68 65 20 75 73 65 72 20 73 65 6c  hen the user sel
e0240 65 63 74 73 20 74 68 65 0a 2a 2a 20 6e 6f 2d 73  ects the.** no-s
e0250 79 6e 63 20 6f 70 74 69 6f 6e 20 66 6f 72 20 74  ync option for t
e0260 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 20 70  he journal.  A p
e0270 6f 77 65 72 20 66 61 69 6c 75 72 65 20 63 6f 75  ower failure cou
e0280 6c 64 20 6c 65 61 64 20 74 6f 20 63 6f 72 72 75  ld lead to corru
e0290 70 74 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 69 73  ption.** in this
e02a0 20 63 61 73 65 2e 20 20 42 75 74 20 66 6f 72 20   case.  But for 
e02b0 74 68 69 6e 67 73 20 6c 69 6b 65 20 74 65 6d 70  things like temp
e02c0 6f 72 61 72 79 20 74 61 62 6c 65 20 28 77 68 69  orary table (whi
e02d0 63 68 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 64 65  ch will be.** de
e02e0 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 20 70  leted when the p
e02f0 6f 77 65 72 20 69 73 20 72 65 73 74 6f 72 65 64  ower is restored
e0300 29 20 77 65 20 64 6f 6e 27 74 20 63 61 72 65 2e  ) we don't care.
e0310 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20    .**.** If the 
e0320 66 69 6c 65 20 6f 70 65 6e 65 64 20 61 73 20 74  file opened as t
e0330 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
e0340 69 73 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f  is not a well-fo
e0350 72 6d 65 64 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20  rmed.** journal 
e0360 66 69 6c 65 20 74 68 65 6e 20 61 6c 6c 20 70 61  file then all pa
e0370 67 65 73 20 75 70 20 74 6f 20 74 68 65 20 66 69  ges up to the fi
e0380 72 73 74 20 63 6f 72 72 75 70 74 65 64 20 70 61  rst corrupted pa
e0390 67 65 20 61 72 65 20 72 6f 6c 6c 65 64 0a 2a 2a  ge are rolled.**
e03a0 20 62 61 63 6b 20 28 6f 72 20 6e 6f 20 70 61 67   back (or no pag
e03b0 65 73 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61  es if the journa
e03c0 6c 20 68 65 61 64 65 72 20 69 73 20 63 6f 72 72  l header is corr
e03d0 75 70 74 65 64 29 2e 20 54 68 65 20 6a 6f 75 72  upted). The jour
e03e0 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 20 74  nal file.** is t
e03f0 68 65 6e 20 64 65 6c 65 74 65 64 20 61 6e 64 20  hen deleted and 
e0400 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e  SQLITE_OK return
e0410 65 64 2c 20 6a 75 73 74 20 61 73 20 69 66 20 6e  ed, just as if n
e0420 6f 20 63 6f 72 72 75 70 74 69 6f 6e 20 68 61 64  o corruption had
e0430 0a 2a 2a 20 62 65 65 6e 20 65 6e 63 6f 75 6e 74  .** been encount
e0440 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  ered..**.** If a
e0450 6e 20 49 2f 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28  n I/O or malloc(
e0460 29 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  ) error occurs, 
e0470 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65  the journal-file
e0480 20 69 73 20 6e 6f 74 20 64 65 6c 65 74 65 64 0a   is not deleted.
e0490 2a 2a 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20  ** and an error 
e04a0 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
e04b0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
e04c0 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 50  pager_playback(P
e04d0 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e  ager *pPager, in
e04e0 74 20 69 73 48 6f 74 29 7b 0a 20 20 73 71 6c 69  t isHot){.  sqli
e04f0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20  te3_vfs *pVfs = 
e0500 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20  pPager->pVfs;.  
e0510 69 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20  i64 szJ;        
e0520 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
e0530 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
e0540 66 69 6c 65 20 69 6e 20 62 79 74 65 73 20 2a 2f  file in bytes */
e0550 0a 20 20 75 33 32 20 6e 52 65 63 3b 20 20 20 20  .  u32 nRec;    
e0560 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
e0570 75 6d 62 65 72 20 6f 66 20 52 65 63 6f 72 64 73  umber of Records
e0580 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   in the journal 
e0590 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  */.  int i;     
e05a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e05b0 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
e05c0 0a 20 20 50 67 6e 6f 20 6d 78 50 67 20 3d 20 30  .  Pgno mxPg = 0
e05d0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  ;           /* S
e05e0 69 7a 65 20 6f 66 20 74 68 65 20 6f 72 69 67 69  ize of the origi
e05f0 6e 61 6c 20 66 69 6c 65 20 69 6e 20 70 61 67 65  nal file in page
e0600 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  s */.  int rc;  
e0610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0620 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 6f  /* Result code o
e0630 66 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 2a  f a subroutine *
e0640 2f 0a 20 20 69 6e 74 20 72 65 73 20 3d 20 30 3b  /.  int res = 0;
e0650 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e0660 56 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62  Value returned b
e0670 79 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73  y sqlite3OsAcces
e0680 73 28 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  s() */.  char *z
e0690 4d 61 73 74 65 72 20 3d 20 30 3b 20 20 20 20 20  Master = 0;     
e06a0 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 6d 61 73    /* Name of mas
e06b0 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
e06c0 20 69 66 20 61 6e 79 20 2a 2f 0a 0a 20 20 2f 2a   if any */..  /*
e06d0 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20   Figure out how 
e06e0 6d 61 6e 79 20 72 65 63 6f 72 64 73 20 61 72 65  many records are
e06f0 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e   in the journal.
e0700 20 20 41 62 6f 72 74 20 65 61 72 6c 79 20 69 66    Abort early if
e0710 0a 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61  .  ** the journa
e0720 6c 20 69 73 20 65 6d 70 74 79 2e 0a 20 20 2a 2f  l is empty..  */
e0730 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
e0740 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29  r->journalOpen )
e0750 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
e0760 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65  OsFileSize(pPage
e0770 72 2d 3e 6a 66 64 2c 20 26 73 7a 4a 29 3b 0a 20  r->jfd, &szJ);. 
e0780 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
e0790 4f 4b 20 7c 7c 20 73 7a 4a 3d 3d 30 20 29 7b 0a  OK || szJ==0 ){.
e07a0 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61      goto end_pla
e07b0 79 62 61 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  yback;.  }..  /*
e07c0 20 52 65 61 64 20 74 68 65 20 6d 61 73 74 65 72   Read the master
e07d0 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 66 72   journal name fr
e07e0 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20  om the journal, 
e07f0 69 66 20 69 74 20 69 73 20 70 72 65 73 65 6e 74  if it is present
e0800 2e 0a 20 20 2a 2a 20 49 66 20 61 20 6d 61 73 74  ..  ** If a mast
e0810 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
e0820 6e 61 6d 65 20 69 73 20 73 70 65 63 69 66 69 65  name is specifie
e0830 64 2c 20 62 75 74 20 74 68 65 20 66 69 6c 65 20  d, but the file 
e0840 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 70 72 65 73  is not.  ** pres
e0850 65 6e 74 20 6f 6e 20 64 69 73 6b 2c 20 74 68 65  ent on disk, the
e0860 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73  n the journal is
e0870 20 6e 6f 74 20 68 6f 74 20 61 6e 64 20 64 6f 65   not hot and doe
e0880 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65  s not need to be
e0890 0a 20 20 2a 2a 20 70 6c 61 79 65 64 20 62 61 63  .  ** played bac
e08a0 6b 2e 0a 20 20 2a 2f 0a 20 20 7a 4d 61 73 74 65  k..  */.  zMaste
e08b0 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70  r = pPager->pTmp
e08c0 53 70 61 63 65 3b 0a 20 20 72 63 20 3d 20 72 65  Space;.  rc = re
e08d0 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28  adMasterJournal(
e08e0 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61  pPager->jfd, zMa
e08f0 73 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 56  ster, pPager->pV
e0900 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31  fs->mxPathname+1
e0910 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
e0920 49 54 45 5f 4f 4b 20 7c 7c 20 28 7a 4d 61 73 74  ITE_OK || (zMast
e0930 65 72 5b 30 5d 20 0a 20 20 20 26 26 20 28 72 65  er[0] .   && (re
e0940 73 3d 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73  s=sqlite3OsAcces
e0950 73 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c  s(pVfs, zMaster,
e0960 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45   SQLITE_ACCESS_E
e0970 58 49 53 54 53 29 29 3d 3d 30 20 29 20 0a 20 20  XISTS))==0 ) .  
e0980 29 7b 0a 20 20 20 20 7a 4d 61 73 74 65 72 20 3d  ){.    zMaster =
e0990 20 30 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64   0;.    goto end
e09a0 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 7d 0a 20  _playback;.  }. 
e09b0 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20   zMaster = 0;.  
e09c0 69 66 28 20 72 65 73 3c 30 20 29 7b 0a 20 20 20  if( res<0 ){.   
e09d0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45   rc = SQLITE_IOE
e09e0 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f  RR_NOMEM;.    go
e09f0 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b  to end_playback;
e0a00 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6a  .  }.  pPager->j
e0a10 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 0a  ournalOff = 0;..
e0a20 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 74    /* This loop t
e0a30 65 72 6d 69 6e 61 74 65 73 20 65 69 74 68 65 72  erminates either
e0a40 20 77 68 65 6e 20 74 68 65 20 72 65 61 64 4a 6f   when the readJo
e0a50 75 72 6e 61 6c 48 64 72 28 29 20 63 61 6c 6c 20  urnalHdr() call 
e0a60 72 65 74 75 72 6e 73 0a 20 20 2a 2a 20 53 51 4c  returns.  ** SQL
e0a70 49 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 49  ITE_DONE or an I
e0a80 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 20  O error occurs. 
e0a90 2a 2f 0a 20 20 77 68 69 6c 65 28 20 31 20 29 7b  */.  while( 1 ){
e0aa0 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68  ..    /* Read th
e0ab0 65 20 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68  e next journal h
e0ac0 65 61 64 65 72 20 66 72 6f 6d 20 74 68 65 20 6a  eader from the j
e0ad0 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 49 66  ournal file.  If
e0ae0 20 74 68 65 72 65 20 61 72 65 0a 20 20 20 20 2a   there are.    *
e0af0 2a 20 6e 6f 74 20 65 6e 6f 75 67 68 20 62 79 74  * not enough byt
e0b00 65 73 20 6c 65 66 74 20 69 6e 20 74 68 65 20 6a  es left in the j
e0b10 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20  ournal file for 
e0b20 61 20 63 6f 6d 70 6c 65 74 65 20 68 65 61 64 65  a complete heade
e0b30 72 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 69 74 20  r, or.    ** it 
e0b40 69 73 20 63 6f 72 72 75 70 74 65 64 2c 20 74 68  is corrupted, th
e0b50 65 6e 20 61 20 70 72 6f 63 65 73 73 20 6d 75 73  en a process mus
e0b60 74 20 6f 66 20 66 61 69 6c 65 64 20 77 68 69 6c  t of failed whil
e0b70 65 20 77 72 69 74 69 6e 67 20 69 74 2e 0a 20 20  e writing it..  
e0b80 20 20 2a 2a 20 54 68 69 73 20 69 6e 64 69 63 61    ** This indica
e0b90 74 65 73 20 6e 6f 74 68 69 6e 67 20 6d 6f 72 65  tes nothing more
e0ba0 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 6f 6c   needs to be rol
e0bb0 6c 65 64 20 62 61 63 6b 2e 0a 20 20 20 20 2a 2f  led back..    */
e0bc0 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64 4a 6f  .    rc = readJo
e0bd0 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 2c  urnalHdr(pPager,
e0be0 20 73 7a 4a 2c 20 26 6e 52 65 63 2c 20 26 6d 78   szJ, &nRec, &mx
e0bf0 50 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  Pg);.    if( rc!
e0c00 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 0a 20  =SQLITE_OK ){ . 
e0c10 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
e0c20 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20  ITE_DONE ){.    
e0c30 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
e0c40 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  OK;.      }.    
e0c50 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62    goto end_playb
e0c60 61 63 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  ack;.    }..    
e0c70 2f 2a 20 49 66 20 6e 52 65 63 20 69 73 20 30 78  /* If nRec is 0x
e0c80 66 66 66 66 66 66 66 66 2c 20 74 68 65 6e 20 74  ffffffff, then t
e0c90 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20  his journal was 
e0ca0 63 72 65 61 74 65 64 20 62 79 20 61 20 70 72 6f  created by a pro
e0cb0 63 65 73 73 0a 20 20 20 20 2a 2a 20 77 6f 72 6b  cess.    ** work
e0cc0 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d  ing in no-sync m
e0cd0 6f 64 65 2e 20 54 68 69 73 20 6d 65 61 6e 73 20  ode. This means 
e0ce0 74 68 61 74 20 74 68 65 20 72 65 73 74 20 6f 66  that the rest of
e0cf0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20   the journal.   
e0d00 20 2a 2a 20 66 69 6c 65 20 63 6f 6e 73 69 73 74   ** file consist
e0d10 73 20 6f 66 20 70 61 67 65 73 2c 20 74 68 65 72  s of pages, ther
e0d20 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 6a 6f  e are no more jo
e0d30 75 72 6e 61 6c 20 68 65 61 64 65 72 73 2e 20 43  urnal headers. C
e0d40 6f 6d 70 75 74 65 0a 20 20 20 20 2a 2a 20 74 68  ompute.    ** th
e0d50 65 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 20  e value of nRec 
e0d60 62 61 73 65 64 20 6f 6e 20 74 68 69 73 20 61 73  based on this as
e0d70 73 75 6d 70 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f  sumption..    */
e0d80 0a 20 20 20 20 69 66 28 20 6e 52 65 63 3d 3d 30  .    if( nRec==0
e0d90 78 66 66 66 66 66 66 66 66 20 29 7b 0a 20 20 20  xffffffff ){.   
e0da0 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
e0db0 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a  r->journalOff==J
e0dc0 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
e0dd0 61 67 65 72 29 20 29 3b 0a 20 20 20 20 20 20 6e  ager) );.      n
e0de0 52 65 63 20 3d 20 28 73 7a 4a 20 2d 20 4a 4f 55  Rec = (szJ - JOU
e0df0 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
e0e00 65 72 29 29 2f 4a 4f 55 52 4e 41 4c 5f 50 47 5f  er))/JOURNAL_PG_
e0e10 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  SZ(pPager);.    
e0e20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 6e 52 65  }..    /* If nRe
e0e30 63 20 69 73 20 30 20 61 6e 64 20 74 68 69 73 20  c is 0 and this 
e0e40 72 6f 6c 6c 62 61 63 6b 20 69 73 20 6f 66 20 61  rollback is of a
e0e50 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 72 65   transaction cre
e0e60 61 74 65 64 20 62 79 20 74 68 69 73 0a 20 20 20  ated by this.   
e0e70 20 2a 2a 20 70 72 6f 63 65 73 73 20 61 6e 64 20   ** process and 
e0e80 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66  if this is the f
e0e90 69 6e 61 6c 20 68 65 61 64 65 72 20 69 6e 20 74  inal header in t
e0ea0 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 6e  he journal, then
e0eb0 20 69 74 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a   it means.    **
e0ec0 20 74 68 61 74 20 74 68 69 73 20 70 61 72 74 20   that this part 
e0ed0 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77  of the journal w
e0ee0 61 73 20 62 65 69 6e 67 20 66 69 6c 6c 65 64 20  as being filled 
e0ef0 62 75 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20  but has not yet 
e0f00 62 65 65 6e 0a 20 20 20 20 2a 2a 20 73 79 6e 63  been.    ** sync
e0f10 65 64 20 74 6f 20 64 69 73 6b 2e 20 20 43 6f 6d  ed to disk.  Com
e0f20 70 75 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20  pute the number 
e0f30 6f 66 20 70 61 67 65 73 20 62 61 73 65 64 20 6f  of pages based o
e0f40 6e 20 74 68 65 20 72 65 6d 61 69 6e 69 6e 67 0a  n the remaining.
e0f50 20 20 20 20 2a 2a 20 73 69 7a 65 20 6f 66 20 74      ** size of t
e0f60 68 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a  he file..    **.
e0f70 20 20 20 20 2a 2a 20 54 68 65 20 74 68 69 72 64      ** The third
e0f80 20 74 65 72 6d 20 6f 66 20 74 68 65 20 74 65 73   term of the tes
e0f90 74 20 77 61 73 20 61 64 64 65 64 20 74 6f 20 66  t was added to f
e0fa0 69 78 20 74 69 63 6b 65 74 20 23 32 35 36 35 2e  ix ticket #2565.
e0fb0 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
e0fc0 6e 52 65 63 3d 3d 30 20 26 26 20 21 69 73 48 6f  nRec==0 && !isHo
e0fd0 74 20 26 26 0a 20 20 20 20 20 20 20 20 70 50 61  t &&.        pPa
e0fe0 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b  ger->journalHdr+
e0ff0 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70  JOURNAL_HDR_SZ(p
e1000 50 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e  Pager)==pPager->
e1010 6a 6f 75 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 20  journalOff ){.  
e1020 20 20 20 20 6e 52 65 63 20 3d 20 28 73 7a 4a 20      nRec = (szJ 
e1030 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  - pPager->journa
e1040 6c 4f 66 66 29 20 2f 20 4a 4f 55 52 4e 41 4c 5f  lOff) / JOURNAL_
e1050 50 47 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20  PG_SZ(pPager);. 
e1060 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
e1070 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73  this is the firs
e1080 74 20 68 65 61 64 65 72 20 72 65 61 64 20 66 72  t header read fr
e1090 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20  om the journal, 
e10a0 74 72 75 6e 63 61 74 65 20 74 68 65 0a 20 20 20  truncate the.   
e10b0 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c   ** database fil
e10c0 65 20 62 61 63 6b 20 74 6f 20 69 74 73 20 6f 72  e back to its or
e10d0 69 67 69 6e 61 6c 20 73 69 7a 65 2e 0a 20 20 20  iginal size..   
e10e0 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67   */.    if( pPag
e10f0 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d  er->journalOff==
e1100 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70  JOURNAL_HDR_SZ(p
e1110 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20  Pager) ){.      
e1120 72 63 20 3d 20 70 61 67 65 72 5f 74 72 75 6e 63  rc = pager_trunc
e1130 61 74 65 28 70 50 61 67 65 72 2c 20 6d 78 50 67  ate(pPager, mxPg
e1140 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
e1150 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
e1160 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70        goto end_p
e1170 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 20 20 7d  layback;.      }
e1180 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43  .    }..    /* C
e1190 6f 70 79 20 6f 72 69 67 69 6e 61 6c 20 70 61 67  opy original pag
e11a0 65 73 20 6f 75 74 20 6f 66 20 74 68 65 20 6a 6f  es out of the jo
e11b0 75 72 6e 61 6c 20 61 6e 64 20 62 61 63 6b 20 69  urnal and back i
e11c0 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65  nto the database
e11d0 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20   file..    */.  
e11e0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65    for(i=0; i<nRe
e11f0 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72  c; i++){.      r
e1200 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61  c = pager_playba
e1210 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67  ck_one_page(pPag
e1220 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c  er, pPager->jfd,
e1230 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
e1240 4f 66 66 2c 20 31 29 3b 0a 20 20 20 20 20 20 69  Off, 1);.      i
e1250 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
e1260 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
e1270 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
e1280 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
e1290 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
e12a0 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a         pPager->j
e12b0 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b  ournalOff = szJ;
e12c0 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
e12d0 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
e12e0 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
e12f0 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20  end_playback;.  
e1300 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
e1310 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 4e 4f      }.  }.  /*NO
e1320 54 52 45 41 43 48 45 44 2a 2f 0a 20 20 61 73 73  TREACHED*/.  ass
e1330 65 72 74 28 20 30 20 29 3b 0a 0a 65 6e 64 5f 70  ert( 0 );..end_p
e1340 6c 61 79 62 61 63 6b 3a 0a 20 20 69 66 28 20 72  layback:.  if( r
e1350 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
e1360 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 70 50      zMaster = pP
e1370 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b  ager->pTmpSpace;
e1380 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64 4d 61  .    rc = readMa
e1390 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67  sterJournal(pPag
e13a0 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72  er->jfd, zMaster
e13b0 2c 20 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e  , pPager->pVfs->
e13c0 6d 78 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20  mxPathname+1);. 
e13d0 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c   }.  if( rc==SQL
e13e0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63  ITE_OK ){.    rc
e13f0 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61   = pager_end_tra
e1400 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c  nsaction(pPager,
e1410 20 7a 4d 61 73 74 65 72 5b 30 5d 21 3d 27 5c 30   zMaster[0]!='\0
e1420 27 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63  ');.  }.  if( rc
e1430 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 7a  ==SQLITE_OK && z
e1440 4d 61 73 74 65 72 5b 30 5d 20 29 7b 0a 20 20 20  Master[0] ){.   
e1450 20 2f 2a 20 49 66 20 74 68 65 72 65 20 77 61 73   /* If there was
e1460 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61   a master journa
e1470 6c 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69  l and this routi
e1480 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 73  ne will return s
e1490 75 63 63 65 73 73 2c 0a 20 20 20 20 2a 2a 20 73  uccess,.    ** s
e14a0 65 65 20 69 66 20 69 74 20 69 73 20 70 6f 73 73  ee if it is poss
e14b0 69 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 74  ible to delete t
e14c0 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
e14d0 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63  l..    */.    rc
e14e0 20 3d 20 70 61 67 65 72 5f 64 65 6c 6d 61 73 74   = pager_delmast
e14f0 65 72 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74  er(pPager, zMast
e1500 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54  er);.  }..  /* T
e1510 68 65 20 50 61 67 65 72 2e 73 65 63 74 6f 72 53  he Pager.sectorS
e1520 69 7a 65 20 76 61 72 69 61 62 6c 65 20 6d 61 79  ize variable may
e1530 20 68 61 76 65 20 62 65 65 6e 20 75 70 64 61 74   have been updat
e1540 65 64 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e 67  ed while rolling
e1550 0a 20 20 2a 2a 20 62 61 63 6b 20 61 20 6a 6f 75  .  ** back a jou
e1560 72 6e 61 6c 20 63 72 65 61 74 65 64 20 62 79 20  rnal created by 
e1570 61 20 70 72 6f 63 65 73 73 20 77 69 74 68 20 61  a process with a
e1580 20 64 69 66 66 65 72 65 6e 74 20 73 65 63 74 6f   different secto
e1590 72 20 73 69 7a 65 0a 20 20 2a 2a 20 76 61 6c 75  r size.  ** valu
e15a0 65 2e 20 52 65 73 65 74 20 69 74 20 74 6f 20 74  e. Reset it to t
e15b0 68 65 20 63 6f 72 72 65 63 74 20 76 61 6c 75 65  he correct value
e15c0 20 66 6f 72 20 74 68 69 73 20 70 72 6f 63 65 73   for this proces
e15d0 73 2e 0a 20 20 2a 2f 0a 20 20 73 65 74 53 65 63  s..  */.  setSec
e15e0 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29 3b  torSize(pPager);
e15f0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
e1600 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b 20  ./*.** Playback 
e1610 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f  the statement jo
e1620 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  urnal..**.** Thi
e1630 73 20 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20  s is similar to 
e1640 70 6c 61 79 69 6e 67 20 62 61 63 6b 20 74 68 65  playing back the
e1650 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75   transaction jou
e1660 72 6e 61 6c 20 62 75 74 20 77 69 74 68 0a 2a 2a  rnal but with.**
e1670 20 61 20 66 65 77 20 65 78 74 72 61 20 74 77 69   a few extra twi
e1680 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31  sts..**.**    (1
e1690 29 20 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66  )  The number of
e16a0 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61   pages in the da
e16b0 74 61 62 61 73 65 20 66 69 6c 65 20 61 74 20 74  tabase file at t
e16c0 68 65 20 73 74 61 72 74 20 6f 66 0a 2a 2a 20 20  he start of.**  
e16d0 20 20 20 20 20 20 20 74 68 65 20 73 74 61 74 65         the state
e16e0 6d 65 6e 74 20 69 73 20 73 74 6f 72 65 64 20 69  ment is stored i
e16f0 6e 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 53 69  n pPager->stmtSi
e1700 7a 65 2c 20 6e 6f 74 20 69 6e 20 74 68 65 0a 2a  ze, not in the.*
e1710 2a 20 20 20 20 20 20 20 20 20 6a 6f 75 72 6e 61  *         journa
e1720 6c 20 66 69 6c 65 20 69 74 73 65 6c 66 2e 0a 2a  l file itself..*
e1730 2a 0a 2a 2a 20 20 20 20 28 32 29 20 20 49 6e 20  *.**    (2)  In 
e1740 61 64 64 69 74 69 6f 6e 20 74 6f 20 70 6c 61 79  addition to play
e1750 69 6e 67 20 62 61 63 6b 20 74 68 65 20 73 74 61  ing back the sta
e1760 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2c 20  tement journal, 
e1770 61 6c 73 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20  also.**         
e1780 70 6c 61 79 62 61 63 6b 20 61 6c 6c 20 70 61 67  playback all pag
e1790 65 73 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61  es of the transa
e17a0 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 62 65  ction journal be
e17b0 67 69 6e 6e 69 6e 67 0a 2a 2a 20 20 20 20 20 20  ginning.**      
e17c0 20 20 20 61 74 20 6f 66 66 73 65 74 20 70 50 61     at offset pPa
e17d0 67 65 72 2d 3e 73 74 6d 74 4a 53 69 7a 65 2e 0a  ger->stmtJSize..
e17e0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
e17f0 67 65 72 5f 73 74 6d 74 5f 70 6c 61 79 62 61 63  ger_stmt_playbac
e1800 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  k(Pager *pPager)
e1810 7b 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20 20 20  {.  i64 szJ;    
e1820 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e1830 53 69 7a 65 20 6f 66 20 74 68 65 20 66 75 6c 6c  Size of the full
e1840 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36   journal */.  i6
e1850 34 20 68 64 72 4f 66 66 3b 0a 20 20 69 6e 74 20  4 hdrOff;.  int 
e1860 6e 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20  nRec;           
e1870 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
e1880 66 20 52 65 63 6f 72 64 73 20 2a 2f 0a 20 20 69  f Records */.  i
e1890 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
e18a0 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
e18b0 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74  counter */.  int
e18c0 20 72 63 3b 0a 0a 20 20 73 7a 4a 20 3d 20 70 50   rc;..  szJ = pP
e18d0 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
e18e0 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 68 64 72 4f  ;..  /* Set hdrO
e18f0 66 66 20 74 6f 20 62 65 20 74 68 65 20 6f 66 66  ff to be the off
e1900 73 65 74 20 6a 75 73 74 20 61 66 74 65 72 20 74  set just after t
e1910 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 61  he end of the la
e1920 73 74 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20  st journal.  ** 
e1930 70 61 67 65 20 77 72 69 74 74 65 6e 20 62 65 66  page written bef
e1940 6f 72 65 20 74 68 65 20 66 69 72 73 74 20 6a 6f  ore the first jo
e1950 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 66 6f 72  urnal-header for
e1960 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 0a   this statement.
e1970 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e    ** transaction
e1980 20 77 61 73 20 77 72 69 74 74 65 6e 2c 20 6f 72   was written, or
e1990 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
e19a0 66 69 6c 65 20 69 66 20 6e 6f 20 6a 6f 75 72 6e  file if no journ
e19b0 61 6c 0a 20 20 2a 2a 20 68 65 61 64 65 72 20 77  al.  ** header w
e19c0 61 73 20 77 72 69 74 74 65 6e 2e 0a 20 20 2a 2f  as written..  */
e19d0 0a 20 20 68 64 72 4f 66 66 20 3d 20 70 50 61 67  .  hdrOff = pPag
e19e0 65 72 2d 3e 73 74 6d 74 48 64 72 4f 66 66 3b 0a  er->stmtHdrOff;.
e19f0 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
e1a00 2d 3e 66 75 6c 6c 53 79 6e 63 20 7c 7c 20 21 68  ->fullSync || !h
e1a10 64 72 4f 66 66 20 29 3b 0a 20 20 69 66 28 20 21  drOff );.  if( !
e1a20 68 64 72 4f 66 66 20 29 7b 0a 20 20 20 20 68 64  hdrOff ){.    hd
e1a30 72 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 7d 0a  rOff = szJ;.  }.
e1a40 20 20 0a 20 20 2f 2a 20 54 72 75 6e 63 61 74 65    .  /* Truncate
e1a50 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 61   the database ba
e1a60 63 6b 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e  ck to its origin
e1a70 61 6c 20 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20  al size..  */.  
e1a80 72 63 20 3d 20 70 61 67 65 72 5f 74 72 75 6e 63  rc = pager_trunc
e1a90 61 74 65 28 70 50 61 67 65 72 2c 20 70 50 61 67  ate(pPager, pPag
e1aa0 65 72 2d 3e 73 74 6d 74 53 69 7a 65 29 3b 0a 20  er->stmtSize);. 
e1ab0 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
e1ac0 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48  >state>=PAGER_SH
e1ad0 41 52 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 46 69  ARED );..  /* Fi
e1ae0 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e  gure out how man
e1af0 79 20 72 65 63 6f 72 64 73 20 61 72 65 20 69 6e  y records are in
e1b00 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a   the statement j
e1b10 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 61  ournal..  */.  a
e1b20 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
e1b30 74 6d 74 49 6e 55 73 65 20 26 26 20 70 50 61 67  tmtInUse && pPag
e1b40 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20  er->journalOpen 
e1b50 29 3b 0a 20 20 6e 52 65 63 20 3d 20 70 50 61 67  );.  nRec = pPag
e1b60 65 72 2d 3e 73 74 6d 74 4e 52 65 63 3b 0a 20 20  er->stmtNRec;.  
e1b70 0a 20 20 2f 2a 20 43 6f 70 79 20 6f 72 69 67 69  .  /* Copy origi
e1b80 6e 61 6c 20 70 61 67 65 73 20 6f 75 74 20 6f 66  nal pages out of
e1b90 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a   the statement j
e1ba0 6f 75 72 6e 61 6c 20 61 6e 64 20 62 61 63 6b 20  ournal and back 
e1bb0 69 6e 74 6f 20 74 68 65 0a 20 20 2a 2a 20 64 61  into the.  ** da
e1bc0 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4e 6f  tabase file.  No
e1bd0 74 65 20 74 68 61 74 20 74 68 65 20 73 74 61 74  te that the stat
e1be0 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 6f 6d  ement journal om
e1bf0 69 74 73 20 63 68 65 63 6b 73 75 6d 73 20 66 72  its checksums fr
e1c00 6f 6d 0a 20 20 2a 2a 20 65 61 63 68 20 72 65 63  om.  ** each rec
e1c10 6f 72 64 20 73 69 6e 63 65 20 70 6f 77 65 72 2d  ord since power-
e1c20 66 61 69 6c 75 72 65 20 72 65 63 6f 76 65 72 79  failure recovery
e1c30 20 69 73 20 6e 6f 74 20 69 6d 70 6f 72 74 61 6e   is not importan
e1c40 74 20 74 6f 20 73 74 61 74 65 6d 65 6e 74 0a 20  t to statement. 
e1c50 20 2a 2a 20 6a 6f 75 72 6e 61 6c 73 2e 0a 20 20   ** journals..  
e1c60 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  */.  for(i=0; i<
e1c70 6e 52 65 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nRec; i++){.    
e1c80 69 36 34 20 6f 66 66 73 65 74 20 3d 20 69 2a 28  i64 offset = i*(
e1c90 34 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  4+pPager->pageSi
e1ca0 7a 65 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 61  ze);.    rc = pa
e1cb0 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65  ger_playback_one
e1cc0 5f 70 61 67 65 28 70 50 61 67 65 72 2c 20 70 50  _page(pPager, pP
e1cd0 61 67 65 72 2d 3e 73 74 66 64 2c 20 6f 66 66 73  ager->stfd, offs
e1ce0 65 74 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65  et, 0);.    asse
e1cf0 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44  rt( rc!=SQLITE_D
e1d00 4f 4e 45 20 29 3b 0a 20 20 20 20 69 66 28 20 72  ONE );.    if( r
e1d10 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
e1d20 6f 74 6f 20 65 6e 64 5f 73 74 6d 74 5f 70 6c 61  oto end_stmt_pla
e1d30 79 62 61 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  yback;.  }..  /*
e1d40 20 4e 6f 77 20 72 6f 6c 6c 20 73 6f 6d 65 20 70   Now roll some p
e1d50 61 67 65 73 20 62 61 63 6b 20 66 72 6f 6d 20 74  ages back from t
e1d60 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a  he transaction j
e1d70 6f 75 72 6e 61 6c 2e 20 50 61 67 65 72 2e 73 74  ournal. Pager.st
e1d80 6d 74 4a 53 69 7a 65 0a 20 20 2a 2a 20 77 61 73  mtJSize.  ** was
e1d90 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
e1da0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 68   journal file wh
e1db0 65 6e 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e  en this statemen
e1dc0 74 20 77 61 73 20 73 74 61 72 74 65 64 2c 20 73  t was started, s
e1dd0 6f 0a 20 20 2a 2a 20 65 76 65 72 79 74 68 69 6e  o.  ** everythin
e1de0 67 20 61 66 74 65 72 20 74 68 61 74 20 6e 65 65  g after that nee
e1df0 64 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20  ds to be rolled 
e1e00 62 61 63 6b 2c 20 65 69 74 68 65 72 20 69 6e 74  back, either int
e1e10 6f 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62  o the.  ** datab
e1e20 61 73 65 2c 20 74 68 65 20 6d 65 6d 6f 72 79 20  ase, the memory 
e1e30 63 61 63 68 65 2c 20 6f 72 20 62 6f 74 68 2e 0a  cache, or both..
e1e40 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 69 74 20    **.  ** If it 
e1e50 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65  is not zero, the
e1e60 6e 20 50 61 67 65 72 2e 73 74 6d 74 48 64 72 4f  n Pager.stmtHdrO
e1e70 66 66 20 69 73 20 74 68 65 20 6f 66 66 73 65 74  ff is the offset
e1e80 20 74 6f 20 74 68 65 20 73 74 61 72 74 0a 20 20   to the start.  
e1e90 2a 2a 20 6f 66 20 74 68 65 20 66 69 72 73 74 20  ** of the first 
e1ea0 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 77  journal header w
e1eb0 72 69 74 74 65 6e 20 64 75 72 69 6e 67 20 74 68  ritten during th
e1ec0 69 73 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61  is statement tra
e1ed0 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20  nsaction..  */. 
e1ee0 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
e1ef0 4f 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 73 74  Off = pPager->st
e1f00 6d 74 4a 53 69 7a 65 3b 0a 20 20 70 50 61 67 65  mtJSize;.  pPage
e1f10 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 20 3d 20 70  r->cksumInit = p
e1f20 50 61 67 65 72 2d 3e 73 74 6d 74 43 6b 73 75 6d  Pager->stmtCksum
e1f30 3b 0a 20 20 77 68 69 6c 65 28 20 70 50 61 67 65  ;.  while( pPage
e1f40 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3c 20  r->journalOff < 
e1f50 68 64 72 4f 66 66 20 29 7b 0a 20 20 20 20 72 63  hdrOff ){.    rc
e1f60 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63   = pager_playbac
e1f70 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65  k_one_page(pPage
e1f80 72 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  r, pPager->jfd, 
e1f90 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
e1fa0 66 66 2c 20 31 29 3b 0a 20 20 20 20 61 73 73 65  ff, 1);.    asse
e1fb0 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44  rt( rc!=SQLITE_D
e1fc0 4f 4e 45 20 29 3b 0a 20 20 20 20 69 66 28 20 72  ONE );.    if( r
e1fd0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
e1fe0 6f 74 6f 20 65 6e 64 5f 73 74 6d 74 5f 70 6c 61  oto end_stmt_pla
e1ff0 79 62 61 63 6b 3b 0a 20 20 7d 0a 0a 20 20 77 68  yback;.  }..  wh
e2000 69 6c 65 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75  ile( pPager->jou
e2010 72 6e 61 6c 4f 66 66 20 3c 20 73 7a 4a 20 29 7b  rnalOff < szJ ){
e2020 0a 20 20 20 20 75 33 32 20 6e 4a 52 65 63 3b 20  .    u32 nJRec; 
e2030 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
e2040 72 20 6f 66 20 4a 6f 75 72 6e 61 6c 20 52 65 63  r of Journal Rec
e2050 6f 72 64 73 20 2a 2f 0a 20 20 20 20 75 33 32 20  ords */.    u32 
e2060 64 75 6d 6d 79 3b 0a 20 20 20 20 72 63 20 3d 20  dummy;.    rc = 
e2070 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 70  readJournalHdr(p
e2080 50 61 67 65 72 2c 20 73 7a 4a 2c 20 26 6e 4a 52  Pager, szJ, &nJR
e2090 65 63 2c 20 26 64 75 6d 6d 79 29 3b 0a 20 20 20  ec, &dummy);.   
e20a0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
e20b0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  OK ){.      asse
e20c0 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44  rt( rc!=SQLITE_D
e20d0 4f 4e 45 20 29 3b 0a 20 20 20 20 20 20 67 6f 74  ONE );.      got
e20e0 6f 20 65 6e 64 5f 73 74 6d 74 5f 70 6c 61 79 62  o end_stmt_playb
e20f0 61 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ack;.    }.    i
e2100 66 28 20 6e 4a 52 65 63 3d 3d 30 20 29 7b 0a 20  f( nJRec==0 ){. 
e2110 20 20 20 20 20 6e 4a 52 65 63 20 3d 20 28 73 7a       nJRec = (sz
e2120 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  J - pPager->jour
e2130 6e 61 6c 4f 66 66 29 20 2f 20 28 70 50 61 67 65  nalOff) / (pPage
e2140 72 2d 3e 70 61 67 65 53 69 7a 65 2b 38 29 3b 0a  r->pageSize+8);.
e2150 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d      }.    for(i=
e2160 6e 4a 52 65 63 2d 31 3b 20 69 3e 3d 30 20 26 26  nJRec-1; i>=0 &&
e2170 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
e2180 4f 66 66 20 3c 20 73 7a 4a 3b 20 69 2d 2d 29 7b  Off < szJ; i--){
e2190 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
e21a0 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70  r_playback_one_p
e21b0 61 67 65 28 70 50 61 67 65 72 2c 20 70 50 61 67  age(pPager, pPag
e21c0 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d  er->jfd, pPager-
e21d0 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 29 3b  >journalOff, 1);
e21e0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72  .      assert( r
e21f0 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29  c!=SQLITE_DONE )
e2200 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
e2210 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
e2220 20 65 6e 64 5f 73 74 6d 74 5f 70 6c 61 79 62 61   end_stmt_playba
e2230 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ck;.    }.  }.. 
e2240 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
e2250 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 0a 65 6e  Off = szJ;.  .en
e2260 64 5f 73 74 6d 74 5f 70 6c 61 79 62 61 63 6b 3a  d_stmt_playback:
e2270 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
e2280 45 5f 4f 4b 29 20 7b 0a 20 20 20 20 70 50 61 67  E_OK) {.    pPag
e2290 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d  er->journalOff =
e22a0 20 73 7a 4a 3b 0a 20 20 20 20 2f 2a 20 70 61 67   szJ;.    /* pag
e22b0 65 72 5f 72 65 6c 6f 61 64 5f 63 61 63 68 65 28  er_reload_cache(
e22c0 70 50 61 67 65 72 29 3b 20 2a 2f 0a 20 20 7d 0a  pPager); */.  }.
e22d0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
e22e0 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
e22f0 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
e2300 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67  of in-memory pag
e2310 65 73 20 74 68 61 74 20 61 72 65 20 61 6c 6c 6f  es that are allo
e2320 77 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  wed..*/.SQLITE_P
e2330 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
e2340 74 65 33 50 61 67 65 72 53 65 74 43 61 63 68 65  te3PagerSetCache
e2350 73 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67  size(Pager *pPag
e2360 65 72 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b  er, int mxPage){
e2370 0a 20 20 69 66 28 20 6d 78 50 61 67 65 3e 31 30  .  if( mxPage>10
e2380 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   ){.    pPager->
e2390 6d 78 50 61 67 65 20 3d 20 6d 78 50 61 67 65 3b  mxPage = mxPage;
e23a0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50  .  }else{.    pP
e23b0 61 67 65 72 2d 3e 6d 78 50 61 67 65 20 3d 20 31  ager->mxPage = 1
e23c0 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  0;.  }.}../*.** 
e23d0 41 64 6a 75 73 74 20 74 68 65 20 72 6f 62 75 73  Adjust the robus
e23e0 74 6e 65 73 73 20 6f 66 20 74 68 65 20 64 61 74  tness of the dat
e23f0 61 62 61 73 65 20 74 6f 20 64 61 6d 61 67 65 20  abase to damage 
e2400 64 75 65 20 74 6f 20 4f 53 20 63 72 61 73 68 65  due to OS crashe
e2410 73 0a 2a 2a 20 6f 72 20 70 6f 77 65 72 20 66 61  s.** or power fa
e2420 69 6c 75 72 65 73 20 62 79 20 63 68 61 6e 67 69  ilures by changi
e2430 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ng the number of
e2440 20 73 79 6e 63 73 28 29 73 20 77 68 65 6e 20 77   syncs()s when w
e2450 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 72 6f  riting.** the ro
e2460 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20  llback journal. 
e2470 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65 65   There are three
e2480 20 6c 65 76 65 6c 73 3a 0a 2a 2a 0a 2a 2a 20 20   levels:.**.**  
e2490 20 20 4f 46 46 20 20 20 20 20 20 20 73 71 6c 69    OFF       sqli
e24a0 74 65 33 4f 73 53 79 6e 63 28 29 20 69 73 20 6e  te3OsSync() is n
e24b0 65 76 65 72 20 63 61 6c 6c 65 64 2e 20 20 54 68  ever called.  Th
e24c0 69 73 20 69 73 20 74 68 65 20 64 65 66 61 75 6c  is is the defaul
e24d0 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
e24e0 20 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20    for temporary 
e24f0 61 6e 64 20 74 72 61 6e 73 69 65 6e 74 20 66 69  and transient fi
e2500 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 4e 4f  les..**.**    NO
e2510 52 4d 41 4c 20 20 20 20 54 68 65 20 6a 6f 75 72  RMAL    The jour
e2520 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 6f 6e  nal is synced on
e2530 63 65 20 62 65 66 6f 72 65 20 77 72 69 74 65 73  ce before writes
e2540 20 62 65 67 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a   begin on the.**
e2550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61                da
e2560 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 69 73  tabase.  This is
e2570 20 6e 6f 72 6d 61 6c 6c 79 20 61 64 65 71 75 61   normally adequa
e2580 74 65 20 70 72 6f 74 65 63 74 69 6f 6e 2c 20 62  te protection, b
e2590 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ut.**           
e25a0 20 20 20 69 74 20 69 73 20 74 68 65 6f 72 65 74     it is theoret
e25b0 69 63 61 6c 6c 79 20 70 6f 73 73 69 62 6c 65 2c  ically possible,
e25c0 20 74 68 6f 75 67 68 20 76 65 72 79 20 75 6e 6c   though very unl
e25d0 69 6b 65 6c 79 2c 0a 2a 2a 20 20 20 20 20 20 20  ikely,.**       
e25e0 20 20 20 20 20 20 20 74 68 61 74 20 61 6e 20 69         that an i
e25f0 6e 6f 70 65 72 74 75 6e 65 20 70 6f 77 65 72 20  nopertune power 
e2600 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c 65  failure could le
e2610 61 76 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a  ave the journal.
e2620 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
e2630 69 6e 20 61 20 73 74 61 74 65 20 77 68 69 63 68  in a state which
e2640 20 77 6f 75 6c 64 20 63 61 75 73 65 20 64 61 6d   would cause dam
e2650 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 62  age to the datab
e2660 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ase.**          
e2670 20 20 20 20 77 68 65 6e 20 69 74 20 69 73 20 72      when it is r
e2680 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a  olled back..**.*
e2690 2a 20 20 20 20 46 55 4c 4c 20 20 20 20 20 20 54  *    FULL      T
e26a0 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79  he journal is sy
e26b0 6e 63 65 64 20 74 77 69 63 65 20 62 65 66 6f 72  nced twice befor
e26c0 65 20 77 72 69 74 65 73 20 62 65 67 69 6e 20 6f  e writes begin o
e26d0 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  n the.**        
e26e0 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20 28        database (
e26f0 77 69 74 68 20 73 6f 6d 65 20 61 64 64 69 74 69  with some additi
e2700 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
e2710 20 2d 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c   - the nRec fiel
e2720 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  d.**            
e2730 20 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c    of the journal
e2740 20 68 65 61 64 65 72 20 2d 20 62 65 69 6e 67 20   header - being 
e2750 77 72 69 74 74 65 6e 20 69 6e 20 62 65 74 77 65  written in betwe
e2760 65 6e 20 74 68 65 20 74 77 6f 0a 2a 2a 20 20 20  en the two.**   
e2770 20 20 20 20 20 20 20 20 20 20 20 73 79 6e 63 73             syncs
e2780 29 2e 20 20 49 66 20 77 65 20 61 73 73 75 6d 65  ).  If we assume
e2790 20 74 68 61 74 20 77 72 69 74 69 6e 67 20 61 0a   that writing a.
e27a0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
e27b0 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 74  single disk sect
e27c0 6f 72 20 69 73 20 61 74 6f 6d 69 63 2c 20 74 68  or is atomic, th
e27d0 65 6e 20 74 68 69 73 20 6d 6f 64 65 20 70 72 6f  en this mode pro
e27e0 76 69 64 65 73 0a 2a 2a 20 20 20 20 20 20 20 20  vides.**        
e27f0 20 20 20 20 20 20 61 73 73 75 72 61 6e 63 65 20        assurance 
e2800 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c  that the journal
e2810 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 63 6f 72   will not be cor
e2820 72 75 70 74 65 64 20 74 6f 20 74 68 65 0a 2a 2a  rupted to the.**
e2830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6f                po
e2840 69 6e 74 20 6f 66 20 63 61 75 73 69 6e 67 20 64  int of causing d
e2850 61 6d 61 67 65 20 74 6f 20 74 68 65 20 64 61 74  amage to the dat
e2860 61 62 61 73 65 20 64 75 72 69 6e 67 20 72 6f 6c  abase during rol
e2870 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 4e 75 6d  lback..**.** Num
e2880 65 72 69 63 20 76 61 6c 75 65 73 20 61 73 73 6f  eric values asso
e2890 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 73  ciated with thes
e28a0 65 20 73 74 61 74 65 73 20 61 72 65 20 4f 46 46  e states are OFF
e28b0 3d 3d 31 2c 20 4e 4f 52 4d 41 4c 3d 32 2c 0a 2a  ==1, NORMAL=2,.*
e28c0 2a 20 61 6e 64 20 46 55 4c 4c 3d 33 2e 0a 2a 2f  * and FULL=3..*/
e28d0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
e28e0 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d  OMIT_PAGER_PRAGM
e28f0 41 53 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  AS.SQLITE_PRIVAT
e2900 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
e2910 67 65 72 53 65 74 53 61 66 65 74 79 4c 65 76 65  gerSetSafetyLeve
e2920 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  l(Pager *pPager,
e2930 20 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e 74 20   int level, int 
e2940 66 75 6c 6c 5f 66 73 79 6e 63 29 7b 0a 20 20 70  full_fsync){.  p
e2950 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d 20  Pager->noSync = 
e2960 20 6c 65 76 65 6c 3d 3d 31 20 7c 7c 20 70 50 61   level==1 || pPa
e2970 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20  ger->tempFile;. 
e2980 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e   pPager->fullSyn
e2990 63 20 3d 20 6c 65 76 65 6c 3d 3d 33 20 26 26 20  c = level==3 && 
e29a0 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c  !pPager->tempFil
e29b0 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 79 6e  e;.  pPager->syn
e29c0 63 5f 66 6c 61 67 73 20 3d 20 28 66 75 6c 6c 5f  c_flags = (full_
e29d0 66 73 79 6e 63 3f 53 51 4c 49 54 45 5f 53 59 4e  fsync?SQLITE_SYN
e29e0 43 5f 46 55 4c 4c 3a 53 51 4c 49 54 45 5f 53 59  C_FULL:SQLITE_SY
e29f0 4e 43 5f 4e 4f 52 4d 41 4c 29 3b 0a 20 20 69 66  NC_NORMAL);.  if
e2a00 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63  ( pPager->noSync
e2a10 20 29 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53   ) pPager->needS
e2a20 79 6e 63 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69  ync = 0;.}.#endi
e2a30 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  f../*.** The fol
e2a40 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61  lowing global va
e2a50 72 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d  riable is increm
e2a60 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72 20 74  ented whenever t
e2a70 68 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 61 74  he library.** at
e2a80 74 65 6d 70 74 73 20 74 6f 20 6f 70 65 6e 20 61  tempts to open a
e2a90 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e   temporary file.
e2aa0 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69    This informati
e2ab0 6f 6e 20 69 73 20 75 73 65 64 20 66 6f 72 0a 2a  on is used for.*
e2ac0 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 6e  * testing and an
e2ad0 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 20 20 0a 2a  alysis only.  .*
e2ae0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
e2af0 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20  TEST.SQLITE_API 
e2b00 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  int sqlite3_open
e2b10 74 65 6d 70 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a  temp_count = 0;.
e2b20 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 70  #endif../*.** Op
e2b30 65 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66  en a temporary f
e2b40 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 57 72 69 74  ile. .**.** Writ
e2b50 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72  e the file descr
e2b60 69 70 74 6f 72 20 69 6e 74 6f 20 2a 66 64 2e 20  iptor into *fd. 
e2b70 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
e2b80 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20  K on success or 
e2b90 73 6f 6d 65 0a 2a 2a 20 6f 74 68 65 72 20 65 72  some.** other er
e2ba0 72 6f 72 20 63 6f 64 65 20 69 66 20 77 65 20 66  ror code if we f
e2bb0 61 69 6c 2e 20 54 68 65 20 4f 53 20 77 69 6c 6c  ail. The OS will
e2bc0 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64   automatically d
e2bd0 65 6c 65 74 65 20 74 68 65 20 74 65 6d 70 6f 72  elete the tempor
e2be0 61 72 79 0a 2a 2a 20 66 69 6c 65 20 77 68 65 6e  ary.** file when
e2bf0 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a   it is closed..*
e2c00 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  /.static int sql
e2c10 69 74 65 33 50 61 67 65 72 4f 70 65 6e 74 65 6d  ite3PagerOpentem
e2c20 70 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  p(.  sqlite3_vfs
e2c30 20 2a 70 56 66 73 2c 20 20 20 20 2f 2a 20 54 68   *pVfs,    /* Th
e2c40 65 20 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73  e virtual file s
e2c50 79 73 74 65 6d 20 6c 61 79 65 72 20 2a 2f 0a 20  ystem layer */. 
e2c60 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
e2c70 46 69 6c 65 2c 20 20 2f 2a 20 57 72 69 74 65 20  File,  /* Write 
e2c80 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
e2c90 74 6f 72 20 68 65 72 65 20 2a 2f 0a 20 20 63 68  tor here */.  ch
e2ca0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20  ar *zFilename,  
e2cb0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
e2cc0 68 65 20 66 69 6c 65 2e 20 20 4d 69 67 68 74 20  he file.  Might 
e2cd0 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74  be NULL */.  int
e2ce0 20 76 66 73 46 6c 61 67 73 20 20 20 20 20 20 20   vfsFlags       
e2cf0 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73     /* Flags pass
e2d00 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68  ed through to th
e2d10 65 20 56 46 53 20 2a 2f 0a 29 7b 0a 20 20 69 6e  e VFS */.){.  in
e2d20 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  t rc;.  assert( 
e2d30 7a 46 69 6c 65 6e 61 6d 65 21 3d 30 20 29 3b 0a  zFilename!=0 );.
e2d40 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
e2d50 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 6f 70  EST.  sqlite3_op
e2d60 65 6e 74 65 6d 70 5f 63 6f 75 6e 74 2b 2b 3b 20  entemp_count++; 
e2d70 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 74 65 73   /* Used for tes
e2d80 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69  ting and analysi
e2d90 73 20 6f 6e 6c 79 20 2a 2f 0a 23 65 6e 64 69 66  s only */.#endif
e2da0 0a 0a 20 20 76 66 73 46 6c 61 67 73 20 7c 3d 20  ..  vfsFlags |= 
e2db0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
e2dc0 44 57 52 49 54 45 20 7c 20 53 51 4c 49 54 45 5f  DWRITE | SQLITE_
e2dd0 4f 50 45 4e 5f 43 52 45 41 54 45 20 7c 0a 20 20  OPEN_CREATE |.  
e2de0 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
e2df0 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20  _OPEN_EXCLUSIVE 
e2e00 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45  | SQLITE_OPEN_DE
e2e10 4c 45 54 45 4f 4e 43 4c 4f 53 45 3b 0a 20 20 72  LETEONCLOSE;.  r
e2e20 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  c = sqlite3OsOpe
e2e30 6e 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d  n(pVfs, zFilenam
e2e40 65 2c 20 70 46 69 6c 65 2c 20 76 66 73 46 6c 61  e, pFile, vfsFla
e2e50 67 73 2c 20 30 29 3b 0a 20 20 61 73 73 65 72 74  gs, 0);.  assert
e2e60 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
e2e70 7c 7c 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f  || pFile->pMetho
e2e80 64 73 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ds );.  return r
e2e90 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61  c;.}../*.** Crea
e2ea0 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 63 61  te a new page ca
e2eb0 63 68 65 20 61 6e 64 20 70 75 74 20 61 20 70 6f  che and put a po
e2ec0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67  inter to the pag
e2ed0 65 20 63 61 63 68 65 20 69 6e 20 2a 70 70 50 61  e cache in *ppPa
e2ee0 67 65 72 2e 0a 2a 2a 20 54 68 65 20 66 69 6c 65  ger..** The file
e2ef0 20 74 6f 20 62 65 20 63 61 63 68 65 64 20 6e 65   to be cached ne
e2f00 65 64 20 6e 6f 74 20 65 78 69 73 74 2e 20 20 54  ed not exist.  T
e2f10 68 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 6c  he file is not l
e2f20 6f 63 6b 65 64 20 75 6e 74 69 6c 0a 2a 2a 20 74  ocked until.** t
e2f30 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f  he first call to
e2f40 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
e2f50 28 29 20 61 6e 64 20 69 73 20 6f 6e 6c 79 20 68  () and is only h
e2f60 65 6c 64 20 6f 70 65 6e 20 75 6e 74 69 6c 20 74  eld open until t
e2f70 68 65 0a 2a 2a 20 6c 61 73 74 20 70 61 67 65 20  he.** last page 
e2f80 69 73 20 72 65 6c 65 61 73 65 64 20 75 73 69 6e  is released usin
e2f90 67 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e  g sqlite3PagerUn
e2fa0 72 65 66 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ref()..**.** If 
e2fb0 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c  zFilename is NUL
e2fc0 4c 20 74 68 65 6e 20 61 20 72 61 6e 64 6f 6d 6c  L then a randoml
e2fd0 79 2d 6e 61 6d 65 64 20 74 65 6d 70 6f 72 61 72  y-named temporar
e2fe0 79 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 65  y file is create
e2ff0 64 0a 2a 2a 20 61 6e 64 20 75 73 65 64 20 61 73  d.** and used as
e3000 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 20   the file to be 
e3010 63 61 63 68 65 64 2e 20 20 54 68 65 20 66 69 6c  cached.  The fil
e3020 65 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 74 65  e will be delete
e3030 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c  d.** automatical
e3040 6c 79 20 77 68 65 6e 20 69 74 20 69 73 20 63 6c  ly when it is cl
e3050 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a  osed..**.** If z
e3060 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65  Filename is ":me
e3070 6d 6f 72 79 3a 22 20 74 68 65 6e 20 61 6c 6c 20  mory:" then all 
e3080 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 68  information is h
e3090 65 6c 64 20 69 6e 20 63 61 63 68 65 2e 0a 2a 2a  eld in cache..**
e30a0 20 49 74 20 69 73 20 6e 65 76 65 72 20 77 72 69   It is never wri
e30b0 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e 20 20 54  tten to disk.  T
e30c0 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20  his can be used 
e30d0 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 0a  to implement an.
e30e0 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  ** in-memory dat
e30f0 61 62 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  abase..*/.SQLITE
e3100 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
e3110 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 0a 20  ite3PagerOpen(. 
e3120 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
e3130 66 73 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65  fs,       /* The
e3140 20 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79   virtual file sy
e3150 73 74 65 6d 20 74 6f 20 75 73 65 20 2a 2f 0a 20  stem to use */. 
e3160 20 50 61 67 65 72 20 2a 2a 70 70 50 61 67 65 72   Pager **ppPager
e3170 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74  ,         /* Ret
e3180 75 72 6e 20 74 68 65 20 50 61 67 65 72 20 73 74  urn the Pager st
e3190 72 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a  ructure here */.
e31a0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
e31b0 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61  ilename,   /* Na
e31c0 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
e31d0 73 65 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20  se file to open 
e31e0 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 2c  */.  int nExtra,
e31f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e3200 20 45 78 74 72 61 20 62 79 74 65 73 20 61 70 70   Extra bytes app
e3210 65 6e 64 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d  end to each in-m
e3220 65 6d 6f 72 79 20 70 61 67 65 20 2a 2f 0a 20 20  emory page */.  
e3230 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20  int flags,      
e3240 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67           /* flag
e3250 73 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68  s controlling th
e3260 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74  is file */.  int
e3270 20 76 66 73 46 6c 61 67 73 20 20 20 20 20 20 20   vfsFlags       
e3280 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 70        /* flags p
e3290 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f  assed through to
e32a0 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70   sqlite3_vfs.xOp
e32b0 65 6e 28 29 20 2a 2f 0a 29 7b 0a 20 20 75 38 20  en() */.){.  u8 
e32c0 2a 70 50 74 72 3b 0a 20 20 50 61 67 65 72 20 2a  *pPtr;.  Pager *
e32d0 70 50 61 67 65 72 20 3d 20 30 3b 0a 20 20 69 6e  pPager = 0;.  in
e32e0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
e32f0 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  ;.  int i;.  int
e3300 20 74 65 6d 70 46 69 6c 65 20 3d 20 30 3b 0a 20   tempFile = 0;. 
e3310 20 69 6e 74 20 6d 65 6d 44 62 20 3d 20 30 3b 0a   int memDb = 0;.
e3320 20 20 69 6e 74 20 72 65 61 64 4f 6e 6c 79 20 3d    int readOnly =
e3330 20 30 3b 0a 20 20 69 6e 74 20 75 73 65 4a 6f 75   0;.  int useJou
e3340 72 6e 61 6c 20 3d 20 28 66 6c 61 67 73 20 26 20  rnal = (flags & 
e3350 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e  PAGER_OMIT_JOURN
e3360 41 4c 29 3d 3d 30 3b 0a 20 20 69 6e 74 20 6e 6f  AL)==0;.  int no
e3370 52 65 61 64 6c 6f 63 6b 20 3d 20 28 66 6c 61 67  Readlock = (flag
e3380 73 20 26 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41  s & PAGER_NO_REA
e3390 44 4c 4f 43 4b 29 21 3d 30 3b 0a 20 20 69 6e 74  DLOCK)!=0;.  int
e33a0 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65   journalFileSize
e33b0 20 3d 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61   = sqlite3Journa
e33c0 6c 53 69 7a 65 28 70 56 66 73 29 3b 0a 20 20 69  lSize(pVfs);.  i
e33d0 6e 74 20 6e 44 65 66 61 75 6c 74 50 61 67 65 20  nt nDefaultPage 
e33e0 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  = SQLITE_DEFAULT
e33f0 5f 50 41 47 45 5f 53 49 5a 45 3b 0a 20 20 63 68  _PAGE_SIZE;.  ch
e3400 61 72 20 2a 7a 50 61 74 68 6e 61 6d 65 3b 0a 20  ar *zPathname;. 
e3410 20 69 6e 74 20 6e 50 61 74 68 6e 61 6d 65 3b 0a   int nPathname;.
e3420 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 4a 72 6e    char *zStmtJrn
e3430 6c 3b 0a 20 20 69 6e 74 20 6e 53 74 6d 74 4a 72  l;.  int nStmtJr
e3440 6e 6c 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 64 65  nl;..  /* The de
e3450 66 61 75 6c 74 20 72 65 74 75 72 6e 20 69 73 20  fault return is 
e3460 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 2a  a NULL pointer *
e3470 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 3d 20 30  /.  *ppPager = 0
e3480 3b 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20  ;..  /* Compute 
e3490 74 68 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d  the full pathnam
e34a0 65 20 2a 2f 0a 20 20 6e 50 61 74 68 6e 61 6d 65  e */.  nPathname
e34b0 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e   = pVfs->mxPathn
e34c0 61 6d 65 2b 31 3b 0a 20 20 7a 50 61 74 68 6e 61  ame+1;.  zPathna
e34d0 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  me = sqlite3_mal
e34e0 6c 6f 63 28 6e 50 61 74 68 6e 61 6d 65 2a 32 29  loc(nPathname*2)
e34f0 3b 0a 20 20 69 66 28 20 7a 50 61 74 68 6e 61 6d  ;.  if( zPathnam
e3500 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  e==0 ){.    retu
e3510 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
e3520 0a 20 20 7d 0a 20 20 69 66 28 20 7a 46 69 6c 65  .  }.  if( zFile
e3530 6e 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d  name && zFilenam
e3540 65 5b 30 5d 20 29 7b 0a 23 69 66 6e 64 65 66 20  e[0] ){.#ifndef 
e3550 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f  SQLITE_OMIT_MEMO
e3560 52 59 44 42 0a 20 20 20 20 69 66 28 20 73 74 72  RYDB.    if( str
e3570 63 6d 70 28 7a 46 69 6c 65 6e 61 6d 65 2c 22 3a  cmp(zFilename,":
e3580 6d 65 6d 6f 72 79 3a 22 29 3d 3d 30 20 29 7b 0a  memory:")==0 ){.
e3590 20 20 20 20 20 20 6d 65 6d 44 62 20 3d 20 31 3b        memDb = 1;
e35a0 0a 20 20 20 20 20 20 7a 50 61 74 68 6e 61 6d 65  .      zPathname
e35b0 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  [0] = 0;.    }el
e35c0 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a  se.#endif.    {.
e35d0 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
e35e0 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65  e3OsFullPathname
e35f0 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d 65  (pVfs, zFilename
e3600 2c 20 6e 50 61 74 68 6e 61 6d 65 2c 20 7a 50 61  , nPathname, zPa
e3610 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20  thname);.    }. 
e3620 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d   }else{.    rc =
e3630 20 73 71 6c 69 74 65 33 4f 73 47 65 74 54 65 6d   sqlite3OsGetTem
e3640 70 6e 61 6d 65 28 70 56 66 73 2c 20 6e 50 61 74  pname(pVfs, nPat
e3650 68 6e 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65  hname, zPathname
e3660 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21  );.  }.  if( rc!
e3670 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
e3680 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
e3690 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 72  Pathname);.    r
e36a0 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
e36b0 6e 50 61 74 68 6e 61 6d 65 20 3d 20 73 74 72 6c  nPathname = strl
e36c0 65 6e 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 0a  en(zPathname);..
e36d0 20 20 2f 2a 20 50 75 74 20 74 68 65 20 73 74 61    /* Put the sta
e36e0 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 69  tement journal i
e36f0 6e 20 74 65 6d 70 6f 72 61 72 79 20 64 69 73 6b  n temporary disk
e3700 20 73 70 61 63 65 20 73 69 6e 63 65 20 74 68 69   space since thi
e3710 73 20 69 73 0a 20 20 2a 2a 20 73 6f 6d 65 74 69  s is.  ** someti
e3720 6d 65 73 20 52 41 4d 20 64 69 73 6b 20 6f 72 20  mes RAM disk or 
e3730 6f 74 68 65 72 20 6f 70 74 69 6d 69 7a 65 64 20  other optimized 
e3740 73 74 6f 72 61 67 65 2e 20 20 55 6e 6c 69 6b 65  storage.  Unlike
e3750 6c 79 20 74 68 65 20 6d 61 69 6e 0a 20 20 2a 2a  ly the main.  **
e3760 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69   main journal fi
e3770 6c 65 2c 20 74 68 65 20 73 74 61 74 65 6d 65 6e  le, the statemen
e3780 74 20 6a 6f 75 72 6e 61 6c 20 64 6f 65 73 20 6e  t journal does n
e3790 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 0a 20  ot need to be . 
e37a0 20 2a 2a 20 63 6f 6c 6f 63 61 74 65 64 20 77 69   ** colocated wi
e37b0 74 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20  th the database 
e37c0 6e 6f 72 20 64 6f 65 73 20 69 74 20 6e 65 65 64  nor does it need
e37d0 20 74 6f 20 62 65 20 70 65 72 73 69 73 74 65 6e   to be persisten
e37e0 74 2e 0a 20 20 2a 2f 0a 20 20 7a 53 74 6d 74 4a  t..  */.  zStmtJ
e37f0 72 6e 6c 20 3d 20 26 7a 50 61 74 68 6e 61 6d 65  rnl = &zPathname
e3800 5b 6e 50 61 74 68 6e 61 6d 65 2b 31 5d 3b 0a 20  [nPathname+1];. 
e3810 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 47   rc = sqlite3OsG
e3820 65 74 54 65 6d 70 6e 61 6d 65 28 70 56 66 73 2c  etTempname(pVfs,
e3830 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d   pVfs->mxPathnam
e3840 65 2b 31 2c 20 7a 53 74 6d 74 4a 72 6e 6c 29 3b  e+1, zStmtJrnl);
e3850 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
e3860 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69  E_OK ){.    sqli
e3870 74 65 33 5f 66 72 65 65 28 7a 50 61 74 68 6e 61  te3_free(zPathna
e3880 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  me);.    return 
e3890 72 63 3b 0a 20 20 7d 0a 20 20 6e 53 74 6d 74 4a  rc;.  }.  nStmtJ
e38a0 72 6e 6c 20 3d 20 73 74 72 6c 65 6e 28 7a 53 74  rnl = strlen(zSt
e38b0 6d 74 4a 72 6e 6c 29 3b 0a 0a 20 20 2f 2a 20 41  mtJrnl);..  /* A
e38c0 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 66  llocate memory f
e38d0 6f 72 20 74 68 65 20 70 61 67 65 72 20 73 74 72  or the pager str
e38e0 75 63 74 75 72 65 20 2a 2f 0a 20 20 70 50 61 67  ucture */.  pPag
e38f0 65 72 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  er = sqlite3Mall
e3900 6f 63 5a 65 72 6f 28 0a 20 20 20 20 73 69 7a 65  ocZero(.    size
e3910 6f 66 28 2a 70 50 61 67 65 72 29 20 2b 20 20 20  of(*pPager) +   
e3920 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72          /* Pager
e3930 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20   structure */.  
e3940 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a    journalFileSiz
e3950 65 20 2b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e +           /*
e3960 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   The journal fil
e3970 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 20 0a  e structure */ .
e3980 20 20 20 20 70 56 66 73 2d 3e 73 7a 4f 73 46 69      pVfs->szOsFi
e3990 6c 65 20 2a 20 33 20 2b 20 20 20 20 20 20 20 20  le * 3 +        
e39a0 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 62 20 61  /* The main db a
e39b0 6e 64 20 74 77 6f 20 6a 6f 75 72 6e 61 6c 20 66  nd two journal f
e39c0 69 6c 65 73 20 2a 2f 20 0a 20 20 20 20 33 2a 6e  iles */ .    3*n
e39d0 50 61 74 68 6e 61 6d 65 20 2b 20 34 30 20 2b 20  Pathname + 40 + 
e39e0 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 69 6c           /* zFil
e39f0 65 6e 61 6d 65 2c 20 7a 44 69 72 65 63 74 6f 72  ename, zDirector
e3a00 79 2c 20 7a 4a 6f 75 72 6e 61 6c 20 2a 2f 0a 20  y, zJournal */. 
e3a10 20 20 20 6e 53 74 6d 74 4a 72 6e 6c 20 20 20 20     nStmtJrnl    
e3a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
e3a30 2a 20 7a 53 74 6d 74 4a 72 6e 6c 20 2a 2f 0a 20  * zStmtJrnl */. 
e3a40 20 29 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65   );.  if( !pPage
e3a50 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  r ){.    sqlite3
e3a60 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d 65 29  _free(zPathname)
e3a70 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
e3a80 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
e3a90 20 70 50 74 72 20 3d 20 28 75 38 20 2a 29 26 70   pPtr = (u8 *)&p
e3aa0 50 61 67 65 72 5b 31 5d 3b 0a 20 20 70 50 61 67  Pager[1];.  pPag
e3ab0 65 72 2d 3e 76 66 73 46 6c 61 67 73 20 3d 20 76  er->vfsFlags = v
e3ac0 66 73 46 6c 61 67 73 3b 0a 20 20 70 50 61 67 65  fsFlags;.  pPage
e3ad0 72 2d 3e 66 64 20 3d 20 28 73 71 6c 69 74 65 33  r->fd = (sqlite3
e3ae0 5f 66 69 6c 65 2a 29 26 70 50 74 72 5b 70 56 66  _file*)&pPtr[pVf
e3af0 73 2d 3e 73 7a 4f 73 46 69 6c 65 2a 30 5d 3b 0a  s->szOsFile*0];.
e3b00 20 20 70 50 61 67 65 72 2d 3e 73 74 66 64 20 3d    pPager->stfd =
e3b10 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29   (sqlite3_file*)
e3b20 26 70 50 74 72 5b 70 56 66 73 2d 3e 73 7a 4f 73  &pPtr[pVfs->szOs
e3b30 46 69 6c 65 2a 31 5d 3b 0a 20 20 70 50 61 67 65  File*1];.  pPage
e3b40 72 2d 3e 6a 66 64 20 3d 20 28 73 71 6c 69 74 65  r->jfd = (sqlite
e3b50 33 5f 66 69 6c 65 2a 29 26 70 50 74 72 5b 70 56  3_file*)&pPtr[pV
e3b60 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 2a 32 5d 3b  fs->szOsFile*2];
e3b70 0a 20 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65  .  pPager->zFile
e3b80 6e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29 26 70  name = (char*)&p
e3b90 50 74 72 5b 70 56 66 73 2d 3e 73 7a 4f 73 46 69  Ptr[pVfs->szOsFi
e3ba0 6c 65 2a 32 2b 6a 6f 75 72 6e 61 6c 46 69 6c 65  le*2+journalFile
e3bb0 53 69 7a 65 5d 3b 0a 20 20 70 50 61 67 65 72 2d  Size];.  pPager-
e3bc0 3e 7a 44 69 72 65 63 74 6f 72 79 20 3d 20 26 70  >zDirectory = &p
e3bd0 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65  Pager->zFilename
e3be0 5b 6e 50 61 74 68 6e 61 6d 65 2b 31 5d 3b 0a 20  [nPathname+1];. 
e3bf0 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61   pPager->zJourna
e3c00 6c 20 3d 20 26 70 50 61 67 65 72 2d 3e 7a 44 69  l = &pPager->zDi
e3c10 72 65 63 74 6f 72 79 5b 6e 50 61 74 68 6e 61 6d  rectory[nPathnam
e3c20 65 2b 31 5d 3b 0a 20 20 70 50 61 67 65 72 2d 3e  e+1];.  pPager->
e3c30 7a 53 74 6d 74 4a 72 6e 6c 20 3d 20 26 70 50 61  zStmtJrnl = &pPa
e3c40 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 5b 6e 50  ger->zJournal[nP
e3c50 61 74 68 6e 61 6d 65 2b 31 30 5d 3b 0a 20 20 70  athname+10];.  p
e3c60 50 61 67 65 72 2d 3e 70 56 66 73 20 3d 20 70 56  Pager->pVfs = pV
e3c70 66 73 3b 0a 20 20 6d 65 6d 63 70 79 28 70 50 61  fs;.  memcpy(pPa
e3c80 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20  ger->zFilename, 
e3c90 7a 50 61 74 68 6e 61 6d 65 2c 20 6e 50 61 74 68  zPathname, nPath
e3ca0 6e 61 6d 65 2b 31 29 3b 0a 20 20 6d 65 6d 63 70  name+1);.  memcp
e3cb0 79 28 70 50 61 67 65 72 2d 3e 7a 53 74 6d 74 4a  y(pPager->zStmtJ
e3cc0 72 6e 6c 2c 20 7a 53 74 6d 74 4a 72 6e 6c 2c 20  rnl, zStmtJrnl, 
e3cd0 6e 53 74 6d 74 4a 72 6e 6c 2b 31 29 3b 0a 20 20  nStmtJrnl+1);.  
e3ce0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 61  sqlite3_free(zPa
e3cf0 74 68 6e 61 6d 65 29 3b 0a 0a 20 20 2f 2a 20 4f  thname);..  /* O
e3d00 70 65 6e 20 74 68 65 20 70 61 67 65 72 20 66 69  pen the pager fi
e3d10 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a  le..  */.  if( z
e3d20 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c  Filename && zFil
e3d30 65 6e 61 6d 65 5b 30 5d 20 26 26 20 21 6d 65 6d  ename[0] && !mem
e3d40 44 62 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 50  Db ){.    if( nP
e3d50 61 74 68 6e 61 6d 65 3e 28 70 56 66 73 2d 3e 6d  athname>(pVfs->m
e3d60 78 50 61 74 68 6e 61 6d 65 20 2d 20 73 69 7a 65  xPathname - size
e3d70 6f 66 28 22 2d 6a 6f 75 72 6e 61 6c 22 29 29 20  of("-journal")) 
e3d80 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
e3d90 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20  LITE_CANTOPEN;. 
e3da0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
e3db0 69 6e 74 20 66 6f 75 74 20 3d 20 30 3b 0a 20 20  int fout = 0;.  
e3dc0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
e3dd0 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 61  OsOpen(pVfs, pPa
e3de0 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20  ger->zFilename, 
e3df0 70 50 61 67 65 72 2d 3e 66 64 2c 0a 20 20 20 20  pPager->fd,.    
e3e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3e10 20 20 20 20 20 70 50 61 67 65 72 2d 3e 76 66 73       pPager->vfs
e3e20 46 6c 61 67 73 2c 20 26 66 6f 75 74 29 3b 0a 20  Flags, &fout);. 
e3e30 20 20 20 20 20 72 65 61 64 4f 6e 6c 79 20 3d 20       readOnly = 
e3e40 28 66 6f 75 74 26 53 51 4c 49 54 45 5f 4f 50 45  (fout&SQLITE_OPE
e3e50 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 0a 20 20  N_READONLY);..  
e3e60 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69      /* If the fi
e3e70 6c 65 20 77 61 73 20 73 75 63 63 65 73 73 66 75  le was successfu
e3e80 6c 6c 79 20 6f 70 65 6e 65 64 20 66 6f 72 20 72  lly opened for r
e3e90 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73  ead/write access
e3ea0 2c 0a 20 20 20 20 20 20 2a 2a 20 63 68 6f 6f 73  ,.      ** choos
e3eb0 65 20 61 20 64 65 66 61 75 6c 74 20 70 61 67 65  e a default page
e3ec0 20 73 69 7a 65 20 69 6e 20 63 61 73 65 20 77 65   size in case we
e3ed0 20 68 61 76 65 20 74 6f 20 63 72 65 61 74 65 20   have to create 
e3ee0 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 74  the.      ** dat
e3ef0 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20  abase file. The 
e3f00 64 65 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a  default page siz
e3f10 65 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d  e is the maximum
e3f20 20 6f 66 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20   of:.      **.  
e3f30 20 20 20 20 2a 2a 20 20 20 20 2b 20 53 51 4c 49      **    + SQLI
e3f40 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  TE_DEFAULT_PAGE_
e3f50 53 49 5a 45 2c 0a 20 20 20 20 20 20 2a 2a 20 20  SIZE,.      **  
e3f60 20 20 2b 20 54 68 65 20 76 61 6c 75 65 20 72 65    + The value re
e3f70 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65  turned by sqlite
e3f80 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 0a  3OsSectorSize().
e3f90 20 20 20 20 20 20 2a 2a 20 20 20 20 2b 20 54 68        **    + Th
e3fa0 65 20 6c 61 72 67 65 73 74 20 70 61 67 65 20 73  e largest page s
e3fb0 69 7a 65 20 74 68 61 74 20 63 61 6e 20 62 65 20  ize that can be 
e3fc0 77 72 69 74 74 65 6e 20 61 74 6f 6d 69 63 61 6c  written atomical
e3fd0 6c 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ly..      */.   
e3fe0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
e3ff0 45 5f 4f 4b 20 26 26 20 21 72 65 61 64 4f 6e 6c  E_OK && !readOnl
e4000 79 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  y ){.        int
e4010 20 69 53 65 63 74 6f 72 53 69 7a 65 20 3d 20 73   iSectorSize = s
e4020 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69  qlite3OsSectorSi
e4030 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a  ze(pPager->fd);.
e4040 20 20 20 20 20 20 20 20 69 66 28 20 6e 44 65 66          if( nDef
e4050 61 75 6c 74 50 61 67 65 3c 69 53 65 63 74 6f 72  aultPage<iSector
e4060 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Size ){.        
e4070 20 20 6e 44 65 66 61 75 6c 74 50 61 67 65 20 3d    nDefaultPage =
e4080 20 69 53 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20   iSectorSize;.  
e4090 20 20 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53        }.#ifdef S
e40a0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f  QLITE_ENABLE_ATO
e40b0 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 20 20 20  MIC_WRITE.      
e40c0 20 20 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e    {.          in
e40d0 74 20 69 44 63 20 3d 20 73 71 6c 69 74 65 33 4f  t iDc = sqlite3O
e40e0 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  sDeviceCharacter
e40f0 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66  istics(pPager->f
e4100 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e  d);.          in
e4110 74 20 69 69 3b 0a 20 20 20 20 20 20 20 20 20 20  t ii;.          
e4120 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49 4f  assert(SQLITE_IO
e4130 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 3d 3d 28  CAP_ATOMIC512==(
e4140 35 31 32 3e 3e 38 29 29 3b 0a 20 20 20 20 20 20  512>>8));.      
e4150 20 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 54      assert(SQLIT
e4160 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34  E_IOCAP_ATOMIC64
e4170 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29 3b 0a  K==(65536>>8));.
e4180 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
e4190 28 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41  (SQLITE_MAX_DEFA
e41a0 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3c 3d 36  ULT_PAGE_SIZE<=6
e41b0 35 35 33 36 29 3b 0a 20 20 20 20 20 20 20 20 20  5536);.         
e41c0 20 66 6f 72 28 69 69 3d 6e 44 65 66 61 75 6c 74   for(ii=nDefault
e41d0 50 61 67 65 3b 20 69 69 3c 3d 53 51 4c 49 54 45  Page; ii<=SQLITE
e41e0 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47  _MAX_DEFAULT_PAG
e41f0 45 5f 53 49 5a 45 3b 20 69 69 3d 69 69 2a 32 29  E_SIZE; ii=ii*2)
e4200 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  {.            if
e4210 28 20 69 44 63 26 28 53 51 4c 49 54 45 5f 49 4f  ( iDc&(SQLITE_IO
e4220 43 41 50 5f 41 54 4f 4d 49 43 7c 28 69 69 3e 3e  CAP_ATOMIC|(ii>>
e4230 38 29 29 20 29 20 6e 44 65 66 61 75 6c 74 50 61  8)) ) nDefaultPa
e4240 67 65 20 3d 20 69 69 3b 0a 20 20 20 20 20 20 20  ge = ii;.       
e4250 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 23     }.        }.#
e4260 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 69 66  endif.        if
e4270 28 20 6e 44 65 66 61 75 6c 74 50 61 67 65 3e 53  ( nDefaultPage>S
e4280 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c  QLITE_MAX_DEFAUL
e4290 54 5f 50 41 47 45 5f 53 49 5a 45 20 29 7b 0a 20  T_PAGE_SIZE ){. 
e42a0 20 20 20 20 20 20 20 20 20 6e 44 65 66 61 75 6c           nDefaul
e42b0 74 50 61 67 65 20 3d 20 53 51 4c 49 54 45 5f 4d  tPage = SQLITE_M
e42c0 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  AX_DEFAULT_PAGE_
e42d0 53 49 5a 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a  SIZE;.        }.
e42e0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
e42f0 7d 65 6c 73 65 20 69 66 28 20 21 6d 65 6d 44 62  }else if( !memDb
e4300 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 61 20   ){.    /* If a 
e4310 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 69  temporary file i
e4320 73 20 72 65 71 75 65 73 74 65 64 2c 20 69 74 20  s requested, it 
e4330 69 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 69 6d  is not opened im
e4340 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a  mediately..    *
e4350 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 77  * In this case w
e4360 65 20 61 63 63 65 70 74 20 74 68 65 20 64 65 66  e accept the def
e4370 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20 61  ault page size a
e4380 6e 64 20 64 65 6c 61 79 20 61 63 74 75 61 6c 6c  nd delay actuall
e4390 79 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 69 6e 67  y.    ** opening
e43a0 20 74 68 65 20 66 69 6c 65 20 75 6e 74 69 6c 20   the file until 
e43b0 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 74  the first call t
e43c0 6f 20 4f 73 57 72 69 74 65 28 29 2e 0a 20 20 20  o OsWrite()..   
e43d0 20 2a 2f 20 0a 20 20 20 20 74 65 6d 70 46 69 6c   */ .    tempFil
e43e0 65 20 3d 20 31 3b 0a 20 20 20 20 70 50 61 67 65  e = 1;.    pPage
e43f0 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52  r->state = PAGER
e4400 5f 45 58 43 4c 55 53 49 56 45 3b 0a 20 20 7d 0a  _EXCLUSIVE;.  }.
e4410 0a 20 20 69 66 28 20 70 50 61 67 65 72 20 26 26  .  if( pPager &&
e4420 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
e4430 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 54  {.    pPager->pT
e4440 6d 70 53 70 61 63 65 20 3d 20 73 71 6c 69 74 65  mpSpace = sqlite
e4450 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e 44 65 66  3MallocZero(nDef
e4460 61 75 6c 74 50 61 67 65 29 3b 0a 20 20 7d 0a 0a  aultPage);.  }..
e4470 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72    /* If an error
e4480 20 6f 63 63 75 72 65 64 20 69 6e 20 65 69 74 68   occured in eith
e4490 65 72 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b 73  er of the blocks
e44a0 20 61 62 6f 76 65 2e 0a 20 20 2a 2a 20 46 72 65   above..  ** Fre
e44b0 65 20 74 68 65 20 50 61 67 65 72 20 73 74 72 75  e the Pager stru
e44c0 63 74 75 72 65 20 61 6e 64 20 63 6c 6f 73 65 20  cture and close 
e44d0 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a 2a 20 53  the file..  ** S
e44e0 69 6e 63 65 20 74 68 65 20 70 61 67 65 72 20 69  ince the pager i
e44f0 73 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 64 20  s not allocated 
e4500 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64  there is no need
e4510 20 74 6f 20 73 65 74 20 0a 20 20 2a 2a 20 61 6e   to set .  ** an
e4520 79 20 50 61 67 65 72 2e 65 72 72 4d 61 73 6b 20  y Pager.errMask 
e4530 76 61 72 69 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a  variables..  */.
e4540 20 20 69 66 28 20 21 70 50 61 67 65 72 20 7c 7c    if( !pPager ||
e4550 20 21 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70   !pPager->pTmpSp
e4560 61 63 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ace ){.    sqlit
e4570 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72  e3OsClose(pPager
e4580 2d 3e 66 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  ->fd);.    sqlit
e4590 65 33 5f 66 72 65 65 28 70 50 61 67 65 72 29 3b  e3_free(pPager);
e45a0 0a 20 20 20 20 72 65 74 75 72 6e 20 28 28 72 63  .    return ((rc
e45b0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 3f 53 51 4c  ==SQLITE_OK)?SQL
e45c0 49 54 45 5f 4e 4f 4d 45 4d 3a 72 63 29 3b 0a 20  ITE_NOMEM:rc);. 
e45d0 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45   }..  PAGERTRACE
e45e0 33 28 22 4f 50 45 4e 20 25 64 20 25 73 5c 6e 22  3("OPEN %d %s\n"
e45f0 2c 20 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 70  , FILEHANDLEID(p
e4600 50 61 67 65 72 2d 3e 66 64 29 2c 20 70 50 61 67  Pager->fd), pPag
e4610 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  er->zFilename);.
e4620 20 20 49 4f 54 52 41 43 45 28 28 22 4f 50 45 4e    IOTRACE(("OPEN
e4630 20 25 70 20 25 73 5c 6e 22 2c 20 70 50 61 67 65   %p %s\n", pPage
e4640 72 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65  r, pPager->zFile
e4650 6e 61 6d 65 29 29 0a 0a 20 20 2f 2a 20 46 69 6c  name))..  /* Fil
e4660 6c 20 69 6e 20 50 61 67 65 72 2e 7a 44 69 72 65  l in Pager.zDire
e4670 63 74 6f 72 79 5b 5d 20 2a 2f 0a 20 20 6d 65 6d  ctory[] */.  mem
e4680 63 70 79 28 70 50 61 67 65 72 2d 3e 7a 44 69 72  cpy(pPager->zDir
e4690 65 63 74 6f 72 79 2c 20 70 50 61 67 65 72 2d 3e  ectory, pPager->
e46a0 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 50 61 74 68  zFilename, nPath
e46b0 6e 61 6d 65 2b 31 29 3b 0a 20 20 66 6f 72 28 69  name+1);.  for(i
e46c0 3d 73 74 72 6c 65 6e 28 70 50 61 67 65 72 2d 3e  =strlen(pPager->
e46d0 7a 44 69 72 65 63 74 6f 72 79 29 3b 20 69 3e 30  zDirectory); i>0
e46e0 20 26 26 20 70 50 61 67 65 72 2d 3e 7a 44 69 72   && pPager->zDir
e46f0 65 63 74 6f 72 79 5b 69 2d 31 5d 21 3d 27 2f 27  ectory[i-1]!='/'
e4700 3b 20 69 2d 2d 29 7b 7d 0a 20 20 69 66 28 20 69  ; i--){}.  if( i
e4710 3e 30 20 29 20 70 50 61 67 65 72 2d 3e 7a 44 69  >0 ) pPager->zDi
e4720 72 65 63 74 6f 72 79 5b 69 2d 31 5d 20 3d 20 30  rectory[i-1] = 0
e4730 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20  ;..  /* Fill in 
e4740 50 61 67 65 72 2e 7a 4a 6f 75 72 6e 61 6c 5b 5d  Pager.zJournal[]
e4750 20 2a 2f 0a 20 20 6d 65 6d 63 70 79 28 70 50 61   */.  memcpy(pPa
e4760 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70  ger->zJournal, p
e4770 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65  Pager->zFilename
e4780 2c 20 6e 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20  , nPathname);.  
e4790 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e  memcpy(&pPager->
e47a0 7a 4a 6f 75 72 6e 61 6c 5b 6e 50 61 74 68 6e 61  zJournal[nPathna
e47b0 6d 65 5d 2c 20 22 2d 6a 6f 75 72 6e 61 6c 22 2c  me], "-journal",
e47c0 20 39 29 3b 0a 0a 20 20 2f 2a 20 70 50 61 67 65   9);..  /* pPage
e47d0 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 3d  r->journalOpen =
e47e0 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d   0; */.  pPager-
e47f0 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 3d 20 75 73  >useJournal = us
e4800 65 4a 6f 75 72 6e 61 6c 20 26 26 20 21 6d 65 6d  eJournal && !mem
e4810 44 62 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f  Db;.  pPager->no
e4820 52 65 61 64 6c 6f 63 6b 20 3d 20 6e 6f 52 65 61  Readlock = noRea
e4830 64 6c 6f 63 6b 20 26 26 20 72 65 61 64 4f 6e 6c  dlock && readOnl
e4840 79 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e  y;.  /* pPager->
e4850 73 74 6d 74 4f 70 65 6e 20 3d 20 30 3b 20 2a 2f  stmtOpen = 0; */
e4860 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74  .  /* pPager->st
e4870 6d 74 49 6e 55 73 65 20 3d 20 30 3b 20 2a 2f 0a  mtInUse = 0; */.
e4880 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 52 65    /* pPager->nRe
e4890 66 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67  f = 0; */.  pPag
e48a0 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6d 65 6d  er->dbSize = mem
e48b0 44 62 2d 31 3b 0a 20 20 70 50 61 67 65 72 2d 3e  Db-1;.  pPager->
e48c0 70 61 67 65 53 69 7a 65 20 3d 20 6e 44 65 66 61  pageSize = nDefa
e48d0 75 6c 74 50 61 67 65 3b 0a 20 20 2f 2a 20 70 50  ultPage;.  /* pP
e48e0 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65 20 3d  ager->stmtSize =
e48f0 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67   0; */.  /* pPag
e4900 65 72 2d 3e 73 74 6d 74 4a 53 69 7a 65 20 3d 20  er->stmtJSize = 
e4910 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65  0; */.  /* pPage
e4920 72 2d 3e 6e 50 61 67 65 20 3d 20 30 3b 20 2a 2f  r->nPage = 0; */
e4930 0a 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 61 67  .  pPager->mxPag
e4940 65 20 3d 20 31 30 30 3b 0a 20 20 70 50 61 67 65  e = 100;.  pPage
e4950 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 53 51 4c 49  r->mxPgno = SQLI
e4960 54 45 5f 4d 41 58 5f 50 41 47 45 5f 43 4f 55 4e  TE_MAX_PAGE_COUN
e4970 54 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e  T;.  /* pPager->
e4980 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 55 4e  state = PAGER_UN
e4990 4c 4f 43 4b 3b 20 2a 2f 0a 20 20 61 73 73 65 72  LOCK; */.  asser
e49a0 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  t( pPager->state
e49b0 20 3d 3d 20 28 74 65 6d 70 46 69 6c 65 20 3f 20   == (tempFile ? 
e49c0 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20  PAGER_EXCLUSIVE 
e49d0 3a 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 29 20  : PAGER_UNLOCK) 
e49e0 29 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e  );.  /* pPager->
e49f0 65 72 72 4d 61 73 6b 20 3d 20 30 3b 20 2a 2f 0a  errMask = 0; */.
e4a00 20 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69    pPager->tempFi
e4a10 6c 65 20 3d 20 74 65 6d 70 46 69 6c 65 3b 0a 20  le = tempFile;. 
e4a20 20 61 73 73 65 72 74 28 20 74 65 6d 70 46 69 6c   assert( tempFil
e4a30 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  e==PAGER_LOCKING
e4a40 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 0a 20 20 20  MODE_NORMAL .   
e4a50 20 20 20 20 20 20 20 7c 7c 20 74 65 6d 70 46 69         || tempFi
e4a60 6c 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  le==PAGER_LOCKIN
e4a70 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20  GMODE_EXCLUSIVE 
e4a80 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47  );.  assert( PAG
e4a90 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45  ER_LOCKINGMODE_E
e4aa0 58 43 4c 55 53 49 56 45 3d 3d 31 20 29 3b 0a 20  XCLUSIVE==1 );. 
e4ab0 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69   pPager->exclusi
e4ac0 76 65 4d 6f 64 65 20 3d 20 74 65 6d 70 46 69 6c  veMode = tempFil
e4ad0 65 3b 20 0a 20 20 70 50 61 67 65 72 2d 3e 6d 65  e; .  pPager->me
e4ae0 6d 44 62 20 3d 20 6d 65 6d 44 62 3b 0a 20 20 70  mDb = memDb;.  p
e4af0 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 20  Pager->readOnly 
e4b00 3d 20 72 65 61 64 4f 6e 6c 79 3b 0a 20 20 2f 2a  = readOnly;.  /*
e4b10 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
e4b20 63 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67  c = 0; */.  pPag
e4b30 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d 20 70 50 61  er->noSync = pPa
e4b40 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 7c 7c  ger->tempFile ||
e4b50 20 21 75 73 65 4a 6f 75 72 6e 61 6c 3b 0a 20 20   !useJournal;.  
e4b60 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63  pPager->fullSync
e4b70 20 3d 20 28 70 50 61 67 65 72 2d 3e 6e 6f 53 79   = (pPager->noSy
e4b80 6e 63 3f 30 3a 31 29 3b 0a 20 20 70 50 61 67 65  nc?0:1);.  pPage
e4b90 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 20 3d 20  r->sync_flags = 
e4ba0 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d  SQLITE_SYNC_NORM
e4bb0 41 4c 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d  AL;.  /* pPager-
e4bc0 3e 70 46 69 72 73 74 20 3d 20 30 3b 20 2a 2f 0a  >pFirst = 0; */.
e4bd0 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 46 69    /* pPager->pFi
e4be0 72 73 74 53 79 6e 63 65 64 20 3d 20 30 3b 20 2a  rstSynced = 0; *
e4bf0 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70  /.  /* pPager->p
e4c00 4c 61 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 20 70  Last = 0; */.  p
e4c10 50 61 67 65 72 2d 3e 6e 45 78 74 72 61 20 3d 20  Pager->nExtra = 
e4c20 46 4f 52 43 45 5f 41 4c 49 47 4e 4d 45 4e 54 28  FORCE_ALIGNMENT(
e4c30 6e 45 78 74 72 61 29 3b 0a 20 20 61 73 73 65 72  nExtra);.  asser
e4c40 74 28 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d  t(pPager->fd->pM
e4c50 65 74 68 6f 64 73 7c 7c 6d 65 6d 44 62 7c 7c 74  ethods||memDb||t
e4c60 65 6d 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20  empFile);.  if( 
e4c70 21 6d 65 6d 44 62 20 29 7b 0a 20 20 20 20 73 65  !memDb ){.    se
e4c80 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67  tSectorSize(pPag
e4c90 65 72 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 70 50  er);.  }.  /* pP
e4ca0 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c  ager->pBusyHandl
e4cb0 65 72 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20  er = 0; */.  /* 
e4cc0 6d 65 6d 73 65 74 28 70 50 61 67 65 72 2d 3e 61  memset(pPager->a
e4cd0 48 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Hash, 0, sizeof(
e4ce0 70 50 61 67 65 72 2d 3e 61 48 61 73 68 29 29 3b  pPager->aHash));
e4cf0 20 2a 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 3d   */.  *ppPager =
e4d00 20 70 50 61 67 65 72 3b 0a 23 69 66 64 65 66 20   pPager;.#ifdef 
e4d10 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
e4d20 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a  MORY_MANAGEMENT.
e4d30 20 20 70 50 61 67 65 72 2d 3e 69 49 6e 55 73 65    pPager->iInUse
e4d40 4d 4d 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72  MM = 0;.  pPager
e4d50 2d 3e 69 49 6e 55 73 65 44 42 20 3d 20 30 3b 0a  ->iInUseDB = 0;.
e4d60 20 20 69 66 28 20 21 6d 65 6d 44 62 20 29 7b 0a    if( !memDb ){.
e4d70 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  #ifndef SQLITE_M
e4d80 55 54 45 58 5f 4e 4f 4f 50 0a 20 20 20 20 73 71  UTEX_NOOP.    sq
e4d90 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
e4da0 65 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 75 74  ex = sqlite3_mut
e4db0 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  ex_alloc(SQLITE_
e4dc0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d  MUTEX_STATIC_MEM
e4dd0 32 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73  2);.#endif.    s
e4de0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
e4df0 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 70  er(mutex);.    p
e4e00 50 61 67 65 72 2d 3e 70 4e 65 78 74 20 3d 20 73  Pager->pNext = s
e4e10 71 6c 69 74 65 33 50 61 67 65 72 4c 69 73 74 3b  qlite3PagerList;
e4e20 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
e4e30 50 61 67 65 72 4c 69 73 74 20 29 7b 0a 20 20 20  PagerList ){.   
e4e40 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
e4e50 65 33 50 61 67 65 72 4c 69 73 74 2d 3e 70 50 72  e3PagerList->pPr
e4e60 65 76 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73  ev==0 );.      s
e4e70 71 6c 69 74 65 33 50 61 67 65 72 4c 69 73 74 2d  qlite3PagerList-
e4e80 3e 70 50 72 65 76 20 3d 20 70 50 61 67 65 72 3b  >pPrev = pPager;
e4e90 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65  .    }.    pPage
e4ea0 72 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 20  r->pPrev = 0;.  
e4eb0 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 69    sqlite3PagerLi
e4ec0 73 74 20 3d 20 70 50 61 67 65 72 3b 0a 20 20 20  st = pPager;.   
e4ed0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
e4ee0 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 7d  eave(mutex);.  }
e4ef0 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e  .#endif.  return
e4f00 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
e4f10 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 75 73  *.** Set the bus
e4f20 79 20 68 61 6e 64 6c 65 72 20 66 75 6e 63 74 69  y handler functi
e4f30 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
e4f40 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
e4f50 65 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61  e3PagerSetBusyha
e4f60 6e 64 6c 65 72 28 50 61 67 65 72 20 2a 70 50 61  ndler(Pager *pPa
e4f70 67 65 72 2c 20 42 75 73 79 48 61 6e 64 6c 65 72  ger, BusyHandler
e4f80 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 29 7b   *pBusyHandler){
e4f90 0a 20 20 70 50 61 67 65 72 2d 3e 70 42 75 73 79  .  pPager->pBusy
e4fa0 48 61 6e 64 6c 65 72 20 3d 20 70 42 75 73 79 48  Handler = pBusyH
e4fb0 61 6e 64 6c 65 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  andler;.}../*.**
e4fc0 20 53 65 74 20 74 68 65 20 64 65 73 74 72 75 63   Set the destruc
e4fd0 74 6f 72 20 66 6f 72 20 74 68 69 73 20 70 61 67  tor for this pag
e4fe0 65 72 2e 20 20 49 66 20 6e 6f 74 20 4e 55 4c 4c  er.  If not NULL
e4ff0 2c 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72  , the destructor
e5000 20 69 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 77 68   is called.** wh
e5010 65 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  en the reference
e5020 20 63 6f 75 6e 74 20 6f 6e 20 65 61 63 68 20 70   count on each p
e5030 61 67 65 20 72 65 61 63 68 65 73 20 7a 65 72 6f  age reaches zero
e5040 2e 20 20 54 68 65 20 64 65 73 74 72 75 63 74 6f  .  The destructo
e5050 72 20 63 61 6e 0a 2a 2a 20 62 65 20 75 73 65 64  r can.** be used
e5060 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 69 6e 66   to clean up inf
e5070 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65 20  ormation in the 
e5080 65 78 74 72 61 20 73 65 67 6d 65 6e 74 20 61 70  extra segment ap
e5090 70 65 6e 64 65 64 20 74 6f 20 65 61 63 68 20 70  pended to each p
e50a0 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  age..**.** The d
e50b0 65 73 74 72 75 63 74 6f 72 20 69 73 20 6e 6f 74  estructor is not
e50c0 20 63 61 6c 6c 65 64 20 61 73 20 61 20 72 65 73   called as a res
e50d0 75 6c 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  ult sqlite3Pager
e50e0 43 6c 6f 73 65 28 29 2e 20 20 0a 2a 2a 20 44 65  Close().  .** De
e50f0 73 74 72 75 63 74 6f 72 73 20 61 72 65 20 6f 6e  structors are on
e5100 6c 79 20 63 61 6c 6c 65 64 20 62 79 20 73 71 6c  ly called by sql
e5110 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 29  ite3PagerUnref()
e5120 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
e5130 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
e5140 50 61 67 65 72 53 65 74 44 65 73 74 72 75 63 74  PagerSetDestruct
e5150 6f 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  or(Pager *pPager
e5160 2c 20 76 6f 69 64 20 28 2a 78 44 65 73 63 29 28  , void (*xDesc)(
e5170 44 62 50 61 67 65 2a 2c 69 6e 74 29 29 7b 0a 20  DbPage*,int)){. 
e5180 20 70 50 61 67 65 72 2d 3e 78 44 65 73 74 72 75   pPager->xDestru
e5190 63 74 6f 72 20 3d 20 78 44 65 73 63 3b 0a 7d 0a  ctor = xDesc;.}.
e51a0 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 72  ./*.** Set the r
e51b0 65 69 6e 69 74 69 61 6c 69 7a 65 72 20 66 6f 72  einitializer for
e51c0 20 74 68 69 73 20 70 61 67 65 72 2e 20 20 49 66   this pager.  If
e51d0 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 20 72   not NULL, the r
e51e0 65 69 6e 69 74 69 61 6c 69 7a 65 72 0a 2a 2a 20  einitializer.** 
e51f0 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74  is called when t
e5200 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20  he content of a 
e5210 70 61 67 65 20 69 6e 20 63 61 63 68 65 20 69 73  page in cache is
e5220 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69 74 73   restored to its
e5230 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 76 61 6c   original.** val
e5240 75 65 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f  ue as a result o
e5250 66 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 20 20 54  f a rollback.  T
e5260 68 65 20 63 61 6c 6c 62 61 63 6b 20 67 69 76 65  he callback give
e5270 73 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 63  s higher-level c
e5280 6f 64 65 0a 2a 2a 20 61 6e 20 6f 70 70 6f 72 74  ode.** an opport
e5290 75 6e 69 74 79 20 74 6f 20 72 65 73 74 6f 72 65  unity to restore
e52a0 20 74 68 65 20 45 58 54 52 41 20 73 65 63 74 69   the EXTRA secti
e52b0 6f 6e 20 74 6f 20 61 67 72 65 65 20 77 69 74 68  on to agree with
e52c0 20 74 68 65 20 72 65 73 74 6f 72 65 64 0a 2a 2a   the restored.**
e52d0 20 70 61 67 65 20 64 61 74 61 2e 0a 2a 2f 0a 53   page data..*/.S
e52e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
e52f0 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  id sqlite3PagerS
e5300 65 74 52 65 69 6e 69 74 65 72 28 50 61 67 65 72  etReiniter(Pager
e5310 20 2a 70 50 61 67 65 72 2c 20 76 6f 69 64 20 28   *pPager, void (
e5320 2a 78 52 65 69 6e 69 74 29 28 44 62 50 61 67 65  *xReinit)(DbPage
e5330 2a 2c 69 6e 74 29 29 7b 0a 20 20 70 50 61 67 65  *,int)){.  pPage
e5340 72 2d 3e 78 52 65 69 6e 69 74 65 72 20 3d 20 78  r->xReiniter = x
e5350 52 65 69 6e 69 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Reinit;.}../*.**
e5360 20 53 65 74 20 74 68 65 20 70 61 67 65 20 73 69   Set the page si
e5370 7a 65 20 74 6f 20 2a 70 50 61 67 65 53 69 7a 65  ze to *pPageSize
e5380 2e 20 49 66 20 74 68 65 20 73 75 67 67 65 73 74  . If the suggest
e5390 20 6e 65 77 20 70 61 67 65 20 73 69 7a 65 20 69   new page size i
e53a0 73 0a 2a 2a 20 69 6e 61 70 70 72 6f 70 72 69 61  s.** inappropria
e53b0 74 65 2c 20 74 68 65 6e 20 61 6e 20 61 6c 74 65  te, then an alte
e53c0 72 6e 61 74 69 76 65 20 70 61 67 65 20 73 69 7a  rnative page siz
e53d0 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 61 74  e is set to that
e53e0 0a 2a 2a 20 76 61 6c 75 65 20 62 65 66 6f 72 65  .** value before
e53f0 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53   returning..*/.S
e5400 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
e5410 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65  t sqlite3PagerSe
e5420 74 50 61 67 65 73 69 7a 65 28 50 61 67 65 72 20  tPagesize(Pager 
e5430 2a 70 50 61 67 65 72 2c 20 75 31 36 20 2a 70 50  *pPager, u16 *pP
e5440 61 67 65 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20  ageSize){.  int 
e5450 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
e5460 20 20 75 31 36 20 70 61 67 65 53 69 7a 65 20 3d    u16 pageSize =
e5470 20 2a 70 50 61 67 65 53 69 7a 65 3b 0a 20 20 61   *pPageSize;.  a
e5480 73 73 65 72 74 28 20 70 61 67 65 53 69 7a 65 3d  ssert( pageSize=
e5490 3d 30 20 7c 7c 20 28 70 61 67 65 53 69 7a 65 3e  =0 || (pageSize>
e54a0 3d 35 31 32 20 26 26 20 70 61 67 65 53 69 7a 65  =512 && pageSize
e54b0 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47  <=SQLITE_MAX_PAG
e54c0 45 5f 53 49 5a 45 29 20 29 3b 0a 20 20 69 66 28  E_SIZE) );.  if(
e54d0 20 70 61 67 65 53 69 7a 65 20 26 26 20 70 61 67   pageSize && pag
e54e0 65 53 69 7a 65 21 3d 70 50 61 67 65 72 2d 3e 70  eSize!=pPager->p
e54f0 61 67 65 53 69 7a 65 20 0a 20 20 20 26 26 20 21  ageSize .   && !
e5500 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 26 26  pPager->memDb &&
e5510 20 70 50 61 67 65 72 2d 3e 6e 52 65 66 3d 3d 30   pPager->nRef==0
e5520 20 0a 20 20 29 7b 0a 20 20 20 20 63 68 61 72 20   .  ){.    char 
e5530 2a 70 4e 65 77 20 3d 20 28 63 68 61 72 20 2a 29  *pNew = (char *)
e5540 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 70  sqlite3_malloc(p
e5550 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 69 66  ageSize);.    if
e5560 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 20  ( !pNew ){.     
e5570 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
e5580 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  EM;.    }else{. 
e5590 20 20 20 20 20 70 61 67 65 72 45 6e 74 65 72 28       pagerEnter(
e55a0 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 70  pPager);.      p
e55b0 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65  ager_reset(pPage
e55c0 72 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72  r);.      pPager
e55d0 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 70 61 67  ->pageSize = pag
e55e0 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 73 65 74  eSize;.      set
e55f0 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65  SectorSize(pPage
e5600 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  r);.      sqlite
e5610 33 5f 66 72 65 65 28 70 50 61 67 65 72 2d 3e 70  3_free(pPager->p
e5620 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 20 20 20  TmpSpace);.     
e5630 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61   pPager->pTmpSpa
e5640 63 65 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20  ce = pNew;.     
e5650 20 70 61 67 65 72 4c 65 61 76 65 28 70 50 61 67   pagerLeave(pPag
e5660 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  er);.    }.  }. 
e5670 20 2a 70 50 61 67 65 53 69 7a 65 20 3d 20 70 50   *pPageSize = pP
e5680 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a  ager->pageSize;.
e5690 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
e56a0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70  /*.** Return a p
e56b0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 22 74  ointer to the "t
e56c0 65 6d 70 6f 72 61 72 79 20 70 61 67 65 22 20 62  emporary page" b
e56d0 75 66 66 65 72 20 68 65 6c 64 20 69 6e 74 65 72  uffer held inter
e56e0 6e 61 6c 6c 79 0a 2a 2a 20 62 79 20 74 68 65 20  nally.** by the 
e56f0 70 61 67 65 72 2e 20 20 54 68 69 73 20 69 73 20  pager.  This is 
e5700 61 20 62 75 66 66 65 72 20 74 68 61 74 20 69 73  a buffer that is
e5710 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 68   big enough to h
e5720 6f 6c 64 20 74 68 65 0a 2a 2a 20 65 6e 74 69 72  old the.** entir
e5730 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 64  e content of a d
e5740 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 20 54  atabase page.  T
e5750 68 69 73 20 62 75 66 66 65 72 20 69 73 20 75 73  his buffer is us
e5760 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a  ed internally.**
e5770 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62 61 63 6b   during rollback
e5780 20 61 6e 64 20 77 69 6c 6c 20 62 65 20 6f 76 65   and will be ove
e5790 72 77 72 69 74 74 65 6e 20 77 68 65 6e 65 76 65  rwritten wheneve
e57a0 72 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20  r a rollback.** 
e57b0 6f 63 63 75 72 73 2e 20 20 42 75 74 20 6f 74 68  occurs.  But oth
e57c0 65 72 20 6d 6f 64 75 6c 65 73 20 61 72 65 20 66  er modules are f
e57d0 72 65 65 20 74 6f 20 75 73 65 20 69 74 20 74 6f  ree to use it to
e57e0 6f 2c 20 61 73 20 6c 6f 6e 67 20 61 73 0a 2a 2a  o, as long as.**
e57f0 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 61 72   no rollbacks ar
e5800 65 20 68 61 70 70 65 6e 69 6e 67 2e 0a 2a 2f 0a  e happening..*/.
e5810 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
e5820 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65  oid *sqlite3Page
e5830 72 54 65 6d 70 53 70 61 63 65 28 50 61 67 65 72  rTempSpace(Pager
e5840 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74   *pPager){.  ret
e5850 75 72 6e 20 70 50 61 67 65 72 2d 3e 70 54 6d 70  urn pPager->pTmp
e5860 53 70 61 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Space;.}../*.** 
e5870 41 74 74 65 6d 70 74 20 74 6f 20 73 65 74 20 74  Attempt to set t
e5880 68 65 20 6d 61 78 69 6d 75 6d 20 64 61 74 61 62  he maximum datab
e5890 61 73 65 20 70 61 67 65 20 63 6f 75 6e 74 20 69  ase page count i
e58a0 66 20 6d 78 50 61 67 65 20 69 73 20 70 6f 73 69  f mxPage is posi
e58b0 74 69 76 65 2e 20 0a 2a 2a 20 4d 61 6b 65 20 6e  tive. .** Make n
e58c0 6f 20 63 68 61 6e 67 65 73 20 69 66 20 6d 78 50  o changes if mxP
e58d0 61 67 65 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e  age is zero or n
e58e0 65 67 61 74 69 76 65 2e 20 20 41 6e 64 20 6e 65  egative.  And ne
e58f0 76 65 72 20 72 65 64 75 63 65 20 74 68 65 0a 2a  ver reduce the.*
e5900 2a 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63  * maximum page c
e5910 6f 75 6e 74 20 62 65 6c 6f 77 20 74 68 65 20 63  ount below the c
e5920 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 74  urrent size of t
e5930 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a  he database..**.
e5940 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 20 6f 66  ** Regardless of
e5950 20 6d 78 50 61 67 65 2c 20 72 65 74 75 72 6e 20   mxPage, return 
e5960 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61 78 69  the current maxi
e5970 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a  mum page count..
e5980 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
e5990 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
e59a0 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 50  erMaxPageCount(P
e59b0 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e  ager *pPager, in
e59c0 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 69 66 28  t mxPage){.  if(
e59d0 20 6d 78 50 61 67 65 3e 30 20 29 7b 0a 20 20 20   mxPage>0 ){.   
e59e0 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20   pPager->mxPgno 
e59f0 3d 20 6d 78 50 61 67 65 3b 0a 20 20 7d 0a 20 20  = mxPage;.  }.  
e5a00 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
e5a10 63 6f 75 6e 74 28 70 50 61 67 65 72 29 3b 0a 20  count(pPager);. 
e5a20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e   return pPager->
e5a30 6d 78 50 67 6e 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  mxPgno;.}../*.**
e5a40 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   The following s
e5a50 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 61  et of routines a
e5a60 72 65 20 75 73 65 64 20 74 6f 20 64 69 73 61 62  re used to disab
e5a70 6c 65 20 74 68 65 20 73 69 6d 75 6c 61 74 65 64  le the simulated
e5a80 0a 2a 2a 20 49 2f 4f 20 65 72 72 6f 72 20 6d 65  .** I/O error me
e5a90 63 68 61 6e 69 73 6d 2e 20 20 54 68 65 73 65 20  chanism.  These 
e5aa0 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65  routines are use
e5ab0 64 20 74 6f 20 61 76 6f 69 64 20 73 69 6d 75 6c  d to avoid simul
e5ac0 61 74 65 64 0a 2a 2a 20 65 72 72 6f 72 73 20 69  ated.** errors i
e5ad0 6e 20 70 6c 61 63 65 73 20 77 68 65 72 65 20 77  n places where w
e5ae0 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62  e do not care ab
e5af0 6f 75 74 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a  out errors..**.*
e5b00 2a 20 55 6e 6c 65 73 73 20 2d 44 53 51 4c 49 54  * Unless -DSQLIT
e5b10 45 5f 54 45 53 54 3d 31 20 69 73 20 75 73 65 64  E_TEST=1 is used
e5b20 2c 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  , these routines
e5b30 20 61 72 65 20 61 6c 6c 20 6e 6f 2d 6f 70 73 0a   are all no-ops.
e5b40 2a 2a 20 61 6e 64 20 67 65 6e 65 72 61 74 65 20  ** and generate 
e5b50 6e 6f 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 66 64  no code..*/.#ifd
e5b60 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
e5b70 51 4c 49 54 45 5f 41 50 49 20 65 78 74 65 72 6e  QLITE_API extern
e5b80 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f   int sqlite3_io_
e5b90 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a 53  error_pending;.S
e5ba0 51 4c 49 54 45 5f 41 50 49 20 65 78 74 65 72 6e  QLITE_API extern
e5bb0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f   int sqlite3_io_
e5bc0 65 72 72 6f 72 5f 68 69 74 3b 0a 73 74 61 74 69  error_hit;.stati
e5bd0 63 20 69 6e 74 20 73 61 76 65 64 5f 63 6e 74 3b  c int saved_cnt;
e5be0 0a 76 6f 69 64 20 64 69 73 61 62 6c 65 5f 73 69  .void disable_si
e5bf0 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72  mulated_io_error
e5c00 73 28 76 6f 69 64 29 7b 0a 20 20 73 61 76 65 64  s(void){.  saved
e5c10 5f 63 6e 74 20 3d 20 73 71 6c 69 74 65 33 5f 69  _cnt = sqlite3_i
e5c20 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b  o_error_pending;
e5c30 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  .  sqlite3_io_er
e5c40 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 2d 31  ror_pending = -1
e5c50 3b 0a 7d 0a 76 6f 69 64 20 65 6e 61 62 6c 65 5f  ;.}.void enable_
e5c60 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72  simulated_io_err
e5c70 6f 72 73 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c  ors(void){.  sql
e5c80 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65  ite3_io_error_pe
e5c90 6e 64 69 6e 67 20 3d 20 73 61 76 65 64 5f 63 6e  nding = saved_cn
e5ca0 74 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66  t;.}.#else.# def
e5cb0 69 6e 65 20 64 69 73 61 62 6c 65 5f 73 69 6d 75  ine disable_simu
e5cc0 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28  lated_io_errors(
e5cd0 29 0a 23 20 64 65 66 69 6e 65 20 65 6e 61 62 6c  ).# define enabl
e5ce0 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65  e_simulated_io_e
e5cf0 72 72 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a  rrors().#endif..
e5d00 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 66  /*.** Read the f
e5d10 69 72 73 74 20 4e 20 62 79 74 65 73 20 66 72 6f  irst N bytes fro
e5d20 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  m the beginning 
e5d30 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e 74 6f  of the file into
e5d40 20 6d 65 6d 6f 72 79 0a 2a 2a 20 74 68 61 74 20   memory.** that 
e5d50 70 44 65 73 74 20 70 6f 69 6e 74 73 20 74 6f 2e  pDest points to.
e5d60 20 0a 2a 2a 0a 2a 2a 20 4e 6f 20 65 72 72 6f 72   .**.** No error
e5d70 20 63 68 65 63 6b 69 6e 67 20 69 73 20 64 6f 6e   checking is don
e5d80 65 2e 20 54 68 65 20 72 61 74 69 6f 6e 61 6c 20  e. The rational 
e5d90 66 6f 72 20 74 68 69 73 20 69 73 20 74 68 61 74  for this is that
e5da0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 0a   this function .
e5db0 2a 2a 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64  ** may be called
e5dc0 20 65 76 65 6e 20 69 66 20 74 68 65 20 66 69 6c   even if the fil
e5dd0 65 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74  e does not exist
e5de0 20 6f 72 20 63 6f 6e 74 61 69 6e 20 61 20 68 65   or contain a he
e5df0 61 64 65 72 2e 20 49 6e 20 0a 2a 2a 20 74 68 65  ader. In .** the
e5e00 73 65 20 63 61 73 65 73 20 73 71 6c 69 74 65 33  se cases sqlite3
e5e10 4f 73 52 65 61 64 28 29 20 77 69 6c 6c 20 72 65  OsRead() will re
e5e20 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2c 20 74  turn an error, t
e5e30 6f 20 77 68 69 63 68 20 74 68 65 20 63 6f 72 72  o which the corr
e5e40 65 63 74 20 0a 2a 2a 20 72 65 73 70 6f 6e 73 65  ect .** response
e5e50 20 69 73 20 74 6f 20 7a 65 72 6f 20 74 68 65 20   is to zero the 
e5e60 6d 65 6d 6f 72 79 20 61 74 20 70 44 65 73 74 20  memory at pDest 
e5e70 61 6e 64 20 63 6f 6e 74 69 6e 75 65 2e 20 20 41  and continue.  A
e5e80 20 72 65 61 6c 20 49 4f 20 65 72 72 6f 72 20 0a   real IO error .
e5e90 2a 2a 20 77 69 6c 6c 20 70 72 65 73 75 6d 61 62  ** will presumab
e5ea0 6c 79 20 72 65 63 75 72 20 61 6e 64 20 62 65 20  ly recur and be 
e5eb0 70 69 63 6b 65 64 20 75 70 20 6c 61 74 65 72 20  picked up later 
e5ec0 28 54 6f 64 6f 3a 20 54 68 69 6e 6b 20 61 62 6f  (Todo: Think abo
e5ed0 75 74 20 74 68 69 73 29 2e 0a 2a 2f 0a 53 51 4c  ut this)..*/.SQL
e5ee0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
e5ef0 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 61 64  sqlite3PagerRead
e5f00 46 69 6c 65 68 65 61 64 65 72 28 50 61 67 65 72  Fileheader(Pager
e5f10 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 4e 2c   *pPager, int N,
e5f20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
e5f30 70 44 65 73 74 29 7b 0a 20 20 69 6e 74 20 72 63  pDest){.  int rc
e5f40 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
e5f50 6d 65 6d 73 65 74 28 70 44 65 73 74 2c 20 30 2c  memset(pDest, 0,
e5f60 20 4e 29 3b 0a 20 20 61 73 73 65 72 74 28 4d 45   N);.  assert(ME
e5f70 4d 44 42 7c 7c 70 50 61 67 65 72 2d 3e 66 64 2d  MDB||pPager->fd-
e5f80 3e 70 4d 65 74 68 6f 64 73 7c 7c 70 50 61 67 65  >pMethods||pPage
e5f90 72 2d 3e 74 65 6d 70 46 69 6c 65 29 3b 0a 20 20  r->tempFile);.  
e5fa0 69 66 28 20 70 50 61 67 65 72 2d 3e 66 64 2d 3e  if( pPager->fd->
e5fb0 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20  pMethods ){.    
e5fc0 49 4f 54 52 41 43 45 28 28 22 44 42 48 44 52 20  IOTRACE(("DBHDR 
e5fd0 25 70 20 30 20 25 64 5c 6e 22 2c 20 70 50 61 67  %p 0 %d\n", pPag
e5fe0 65 72 2c 20 4e 29 29 0a 20 20 20 20 72 63 20 3d  er, N)).    rc =
e5ff0 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70   sqlite3OsRead(p
e6000 50 61 67 65 72 2d 3e 66 64 2c 20 70 44 65 73 74  Pager->fd, pDest
e6010 2c 20 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  , N, 0);.    if(
e6020 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52   rc==SQLITE_IOER
e6030 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a  R_SHORT_READ ){.
e6040 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
e6050 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  E_OK;.    }.  }.
e6060 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
e6070 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
e6080 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
e6090 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 69   pages in the di
e60a0 73 6b 20 66 69 6c 65 20 61 73 73 6f 63 69 61 74  sk file associat
e60b0 65 64 20 77 69 74 68 0a 2a 2a 20 70 50 61 67 65  ed with.** pPage
e60c0 72 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  r. .**.** If the
e60d0 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 6c 69   PENDING_BYTE li
e60e0 65 73 20 6f 6e 20 74 68 65 20 70 61 67 65 20 64  es on the page d
e60f0 69 72 65 63 74 6c 79 20 61 66 74 65 72 20 74 68  irectly after th
e6100 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20  e end of the.** 
e6110 66 69 6c 65 2c 20 74 68 65 6e 20 63 6f 6e 73 69  file, then consi
e6120 64 65 72 20 74 68 69 73 20 70 61 67 65 20 70 61  der this page pa
e6130 72 74 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74  rt of the file t
e6140 6f 6f 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  oo. For example,
e6150 20 69 66 0a 2a 2a 20 50 45 4e 44 49 4e 47 5f 42   if.** PENDING_B
e6160 59 54 45 20 69 73 20 62 79 74 65 20 34 30 39 36  YTE is byte 4096
e6170 20 28 74 68 65 20 66 69 72 73 74 20 62 79 74 65   (the first byte
e6180 20 6f 66 20 70 61 67 65 20 35 29 20 61 6e 64 20   of page 5) and 
e6190 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 0a  the size of the.
e61a0 2a 2a 20 66 69 6c 65 20 69 73 20 34 30 39 36 20  ** file is 4096 
e61b0 62 79 74 65 73 2c 20 35 20 69 73 20 72 65 74 75  bytes, 5 is retu
e61c0 72 6e 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20  rned instead of 
e61d0 34 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  4..*/.SQLITE_PRI
e61e0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
e61f0 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 50  PagerPagecount(P
e6200 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
e6210 20 69 36 34 20 6e 20 3d 20 30 3b 0a 20 20 69 6e   i64 n = 0;.  in
e6220 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  t rc;.  assert( 
e6230 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 69  pPager!=0 );.  i
e6240 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  f( pPager->errCo
e6250 64 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  de ){.    return
e6260 20 2d 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70   -1;.  }.  if( p
e6270 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e 3d 30  Pager->dbSize>=0
e6280 20 29 7b 0a 20 20 20 20 6e 20 3d 20 70 50 61 67   ){.    n = pPag
e6290 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 7d 20  er->dbSize;.  } 
e62a0 65 6c 73 65 20 7b 0a 20 20 20 20 61 73 73 65 72  else {.    asser
e62b0 74 28 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d  t(pPager->fd->pM
e62c0 65 74 68 6f 64 73 7c 7c 70 50 61 67 65 72 2d 3e  ethods||pPager->
e62d0 74 65 6d 70 46 69 6c 65 29 3b 0a 20 20 20 20 69  tempFile);.    i
e62e0 66 28 20 28 70 50 61 67 65 72 2d 3e 66 64 2d 3e  f( (pPager->fd->
e62f0 70 4d 65 74 68 6f 64 73 29 0a 20 20 20 20 20 26  pMethods).     &
e6300 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f  & (rc = sqlite3O
e6310 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72  sFileSize(pPager
e6320 2d 3e 66 64 2c 20 26 6e 29 29 21 3d 53 51 4c 49  ->fd, &n))!=SQLI
e6330 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70  TE_OK ){.      p
e6340 50 61 67 65 72 2d 3e 6e 52 65 66 2b 2b 3b 0a 20  Pager->nRef++;. 
e6350 20 20 20 20 20 70 61 67 65 72 5f 65 72 72 6f 72       pager_error
e6360 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20  (pPager, rc);.  
e6370 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 66      pPager->nRef
e6380 2d 2d 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  --;.      return
e6390 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69   -1;.    }.    i
e63a0 66 28 20 6e 3e 30 20 26 26 20 6e 3c 70 50 61 67  f( n>0 && n<pPag
e63b0 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a  er->pageSize ){.
e63c0 20 20 20 20 20 20 6e 20 3d 20 31 3b 0a 20 20 20        n = 1;.   
e63d0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 20   }else{.      n 
e63e0 2f 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  /= pPager->pageS
e63f0 69 7a 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ize;.    }.    i
e6400 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  f( pPager->state
e6410 21 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29  !=PAGER_UNLOCK )
e6420 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  {.      pPager->
e6430 64 62 53 69 7a 65 20 3d 20 6e 3b 0a 20 20 20 20  dbSize = n;.    
e6440 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6e 3d 3d 28  }.  }.  if( n==(
e6450 50 45 4e 44 49 4e 47 5f 42 59 54 45 2f 70 50 61  PENDING_BYTE/pPa
e6460 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 20 29  ger->pageSize) )
e6470 7b 0a 20 20 20 20 6e 2b 2b 3b 0a 20 20 7d 0a 20  {.    n++;.  }. 
e6480 20 69 66 28 20 6e 3e 70 50 61 67 65 72 2d 3e 6d   if( n>pPager->m
e6490 78 50 67 6e 6f 20 29 7b 0a 20 20 20 20 70 50 61  xPgno ){.    pPa
e64a0 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 6e 3b  ger->mxPgno = n;
e64b0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3b  .  }.  return n;
e64c0 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  .}...#ifndef SQL
e64d0 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44  ITE_OMIT_MEMORYD
e64e0 42 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 20  B./*.** Clear a 
e64f0 50 67 48 69 73 74 6f 72 79 20 62 6c 6f 63 6b 0a  PgHistory block.
e6500 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
e6510 6c 65 61 72 48 69 73 74 6f 72 79 28 50 67 48 69  learHistory(PgHi
e6520 73 74 6f 72 79 20 2a 70 48 69 73 74 29 7b 0a 20  story *pHist){. 
e6530 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48   sqlite3_free(pH
e6540 69 73 74 2d 3e 70 4f 72 69 67 29 3b 0a 20 20 73  ist->pOrig);.  s
e6550 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48 69 73  qlite3_free(pHis
e6560 74 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 70 48 69  t->pStmt);.  pHi
e6570 73 74 2d 3e 70 4f 72 69 67 20 3d 20 30 3b 0a 20  st->pOrig = 0;. 
e6580 20 70 48 69 73 74 2d 3e 70 53 74 6d 74 20 3d 20   pHist->pStmt = 
e6590 30 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69  0;.}.#else.#defi
e65a0 6e 65 20 63 6c 65 61 72 48 69 73 74 6f 72 79 28  ne clearHistory(
e65b0 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  x).#endif../*.**
e65c0 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61   Forward declara
e65d0 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  tion.*/.static i
e65e0 6e 74 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 50  nt syncJournal(P
e65f0 61 67 65 72 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 55  ager*);../*.** U
e6600 6e 6c 69 6e 6b 20 70 50 67 20 66 72 6f 6d 20 69  nlink pPg from i
e6610 74 73 20 68 61 73 68 20 63 68 61 69 6e 2e 20 41  ts hash chain. A
e6620 6c 73 6f 20 73 65 74 20 74 68 65 20 70 61 67 65  lso set the page
e6630 20 6e 75 6d 62 65 72 20 74 6f 20 30 20 74 6f 20   number to 0 to 
e6640 69 6e 64 69 63 61 74 65 0a 2a 2a 20 74 68 61 74  indicate.** that
e6650 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
e6660 20 70 61 72 74 20 6f 66 20 61 6e 79 20 68 61 73   part of any has
e6670 68 20 63 68 61 69 6e 2e 20 54 68 69 73 20 69 73  h chain. This is
e6680 20 72 65 71 75 69 72 65 64 20 62 65 63 61 75 73   required becaus
e6690 65 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33  e the.** sqlite3
e66a0 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 29 20  PagerMovepage() 
e66b0 72 6f 75 74 69 6e 65 20 63 61 6e 20 6c 65 61 76  routine can leav
e66c0 65 20 61 20 70 61 67 65 20 69 6e 20 74 68 65 20  e a page in the 
e66d0 0a 2a 2a 20 70 4e 65 78 74 46 72 65 65 2f 70 50  .** pNextFree/pP
e66e0 72 65 76 46 72 65 65 20 6c 69 73 74 20 74 68 61  revFree list tha
e66f0 74 20 69 73 20 6e 6f 74 20 61 20 70 61 72 74 20  t is not a part 
e6700 6f 66 20 61 6e 79 20 68 61 73 68 2d 63 68 61 69  of any hash-chai
e6710 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
e6720 64 20 75 6e 6c 69 6e 6b 48 61 73 68 43 68 61 69  d unlinkHashChai
e6730 6e 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  n(Pager *pPager,
e6740 20 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20   PgHdr *pPg){.  
e6750 69 66 28 20 70 50 67 2d 3e 70 67 6e 6f 3d 3d 30  if( pPg->pgno==0
e6760 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
e6770 70 50 67 2d 3e 70 4e 65 78 74 48 61 73 68 3d 3d  pPg->pNextHash==
e6780 30 20 26 26 20 70 50 67 2d 3e 70 50 72 65 76 48  0 && pPg->pPrevH
e6790 61 73 68 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65  ash==0 );.    re
e67a0 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20  turn;.  }.  if( 
e67b0 70 50 67 2d 3e 70 4e 65 78 74 48 61 73 68 20 29  pPg->pNextHash )
e67c0 7b 0a 20 20 20 20 70 50 67 2d 3e 70 4e 65 78 74  {.    pPg->pNext
e67d0 48 61 73 68 2d 3e 70 50 72 65 76 48 61 73 68 20  Hash->pPrevHash 
e67e0 3d 20 70 50 67 2d 3e 70 50 72 65 76 48 61 73 68  = pPg->pPrevHash
e67f0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 67 2d  ;.  }.  if( pPg-
e6800 3e 70 50 72 65 76 48 61 73 68 20 29 7b 0a 20 20  >pPrevHash ){.  
e6810 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
e6820 2d 3e 61 48 61 73 68 5b 70 50 67 2d 3e 70 67 6e  ->aHash[pPg->pgn
e6830 6f 20 26 20 28 70 50 61 67 65 72 2d 3e 6e 48 61  o & (pPager->nHa
e6840 73 68 2d 31 29 5d 21 3d 70 50 67 20 29 3b 0a 20  sh-1)]!=pPg );. 
e6850 20 20 20 70 50 67 2d 3e 70 50 72 65 76 48 61 73     pPg->pPrevHas
e6860 68 2d 3e 70 4e 65 78 74 48 61 73 68 20 3d 20 70  h->pNextHash = p
e6870 50 67 2d 3e 70 4e 65 78 74 48 61 73 68 3b 0a 20  Pg->pNextHash;. 
e6880 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20   }else{.    int 
e6890 68 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 20 26 20  h = pPg->pgno & 
e68a0 28 70 50 61 67 65 72 2d 3e 6e 48 61 73 68 2d 31  (pPager->nHash-1
e68b0 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 61  );.    pPager->a
e68c0 48 61 73 68 5b 68 5d 20 3d 20 70 50 67 2d 3e 70  Hash[h] = pPg->p
e68d0 4e 65 78 74 48 61 73 68 3b 0a 20 20 7d 0a 20 20  NextHash;.  }.  
e68e0 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20  if( MEMDB ){.   
e68f0 20 63 6c 65 61 72 48 69 73 74 6f 72 79 28 50 47   clearHistory(PG
e6900 48 44 52 5f 54 4f 5f 48 49 53 54 28 70 50 67 2c  HDR_TO_HIST(pPg,
e6910 20 70 50 61 67 65 72 29 29 3b 0a 20 20 7d 0a 20   pPager));.  }. 
e6920 20 70 50 67 2d 3e 70 67 6e 6f 20 3d 20 30 3b 0a   pPg->pgno = 0;.
e6930 20 20 70 50 67 2d 3e 70 4e 65 78 74 48 61 73 68    pPg->pNextHash
e6940 20 3d 20 70 50 67 2d 3e 70 50 72 65 76 48 61 73   = pPg->pPrevHas
e6950 68 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  h = 0;.}../*.** 
e6960 55 6e 6c 69 6e 6b 20 61 20 70 61 67 65 20 66 72  Unlink a page fr
e6970 6f 6d 20 74 68 65 20 66 72 65 65 20 6c 69 73 74  om the free list
e6980 20 28 74 68 65 20 6c 69 73 74 20 6f 66 20 61 6c   (the list of al
e6990 6c 20 70 61 67 65 73 20 77 68 65 72 65 20 6e 52  l pages where nR
e69a0 65 66 3d 3d 30 29 0a 2a 2a 20 61 6e 64 20 66 72  ef==0).** and fr
e69b0 6f 6d 20 69 74 73 20 68 61 73 68 20 63 6f 6c 6c  om its hash coll
e69c0 69 73 69 6f 6e 20 63 68 61 69 6e 2e 0a 2a 2f 0a  ision chain..*/.
e69d0 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 6c 69  static void unli
e69e0 6e 6b 50 61 67 65 28 50 67 48 64 72 20 2a 70 50  nkPage(PgHdr *pP
e69f0 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61  g){.  Pager *pPa
e6a00 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65  ger = pPg->pPage
e6a10 72 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 69 6e 6b 20  r;..  /* Unlink 
e6a20 66 72 6f 6d 20 66 72 65 65 20 70 61 67 65 20 6c  from free page l
e6a30 69 73 74 20 2a 2f 0a 20 20 6c 72 75 4c 69 73 74  ist */.  lruList
e6a40 52 65 6d 6f 76 65 28 70 50 67 29 3b 0a 0a 20 20  Remove(pPg);..  
e6a50 2f 2a 20 55 6e 6c 69 6e 6b 20 66 72 6f 6d 20 74  /* Unlink from t
e6a60 68 65 20 70 67 6e 6f 20 68 61 73 68 20 74 61 62  he pgno hash tab
e6a70 6c 65 20 2a 2f 0a 20 20 75 6e 6c 69 6e 6b 48 61  le */.  unlinkHa
e6a80 73 68 43 68 61 69 6e 28 70 50 61 67 65 72 2c 20  shChain(pPager, 
e6a90 70 50 67 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  pPg);.}../*.** T
e6aa0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
e6ab0 73 65 64 20 74 6f 20 74 72 75 6e 63 61 74 65 20  sed to truncate 
e6ac0 74 68 65 20 63 61 63 68 65 20 77 68 65 6e 20 61  the cache when a
e6ad0 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 73 20   database.** is 
e6ae0 74 72 75 6e 63 61 74 65 64 2e 20 20 44 72 6f 70  truncated.  Drop
e6af0 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20   from the cache 
e6b00 61 6c 6c 20 70 61 67 65 73 20 77 68 6f 73 65 20  all pages whose 
e6b10 70 67 6e 6f 20 69 73 0a 2a 2a 20 6c 61 72 67 65  pgno is.** large
e6b20 72 20 74 68 61 6e 20 70 50 61 67 65 72 2d 3e 64  r than pPager->d
e6b30 62 53 69 7a 65 20 61 6e 64 20 69 73 20 75 6e 72  bSize and is unr
e6b40 65 66 65 72 65 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a  eferenced..**.**
e6b50 20 52 65 66 65 72 65 6e 63 65 64 20 70 61 67 65   Referenced page
e6b60 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 70 50  s larger than pP
e6b70 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 61 72 65  ager->dbSize are
e6b80 20 7a 65 72 6f 65 64 2e 0a 2a 2a 0a 2a 2a 20 41   zeroed..**.** A
e6b90 63 74 75 61 6c 6c 79 2c 20 61 74 20 74 68 65 20  ctually, at the 
e6ba0 70 6f 69 6e 74 20 74 68 69 73 20 72 6f 75 74 69  point this routi
e6bb0 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 69 74  ne is called, it
e6bc0 20 77 6f 75 6c 64 20 62 65 0a 2a 2a 20 61 6e 20   would be.** an 
e6bd0 65 72 72 6f 72 20 74 6f 20 68 61 76 65 20 61 20  error to have a 
e6be0 72 65 66 65 72 65 6e 63 65 64 20 70 61 67 65 2e  referenced page.
e6bf0 20 20 42 75 74 20 72 61 74 68 65 72 20 74 68 61    But rather tha
e6c00 6e 20 64 65 6c 65 74 65 0a 2a 2a 20 74 68 61 74  n delete.** that
e6c10 20 70 61 67 65 20 61 6e 64 20 67 75 61 72 61 6e   page and guaran
e6c20 74 65 65 20 61 20 73 75 62 73 65 71 75 65 6e 74  tee a subsequent
e6c30 20 73 65 67 66 61 75 6c 74 2c 20 69 74 20 73 65   segfault, it se
e6c40 65 6d 73 20 62 65 74 74 65 72 0a 2a 2a 20 74 6f  ems better.** to
e6c50 20 7a 65 72 6f 20 69 74 20 61 6e 64 20 68 6f 70   zero it and hop
e6c60 65 20 74 68 61 74 20 77 65 20 65 72 72 6f 72 20  e that we error 
e6c70 6f 75 74 20 73 61 6e 65 6c 79 2e 0a 2a 2f 0a 73  out sanely..*/.s
e6c80 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72  tatic void pager
e6c90 5f 74 72 75 6e 63 61 74 65 5f 63 61 63 68 65 28  _truncate_cache(
e6ca0 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
e6cb0 20 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 20 20    PgHdr *pPg;.  
e6cc0 50 67 48 64 72 20 2a 2a 70 70 50 67 3b 0a 20 20  PgHdr **ppPg;.  
e6cd0 69 6e 74 20 64 62 53 69 7a 65 20 3d 20 70 50 61  int dbSize = pPa
e6ce0 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 0a 20 20  ger->dbSize;..  
e6cf0 70 70 50 67 20 3d 20 26 70 50 61 67 65 72 2d 3e  ppPg = &pPager->
e6d00 70 41 6c 6c 3b 0a 20 20 77 68 69 6c 65 28 20 28  pAll;.  while( (
e6d10 70 50 67 20 3d 20 2a 70 70 50 67 29 21 3d 30 20  pPg = *ppPg)!=0 
e6d20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 67 2d 3e  ){.    if( pPg->
e6d30 70 67 6e 6f 3c 3d 64 62 53 69 7a 65 20 29 7b 0a  pgno<=dbSize ){.
e6d40 20 20 20 20 20 20 70 70 50 67 20 3d 20 26 70 50        ppPg = &pP
e6d50 67 2d 3e 70 4e 65 78 74 41 6c 6c 3b 0a 20 20 20  g->pNextAll;.   
e6d60 20 7d 65 6c 73 65 20 69 66 28 20 70 50 67 2d 3e   }else if( pPg->
e6d70 6e 52 65 66 3e 30 20 29 7b 0a 20 20 20 20 20 20  nRef>0 ){.      
e6d80 6d 65 6d 73 65 74 28 50 47 48 44 52 5f 54 4f 5f  memset(PGHDR_TO_
e6d90 44 41 54 41 28 70 50 67 29 2c 20 30 2c 20 70 50  DATA(pPg), 0, pP
e6da0 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b  ager->pageSize);
e6db0 0a 20 20 20 20 20 20 70 70 50 67 20 3d 20 26 70  .      ppPg = &p
e6dc0 50 67 2d 3e 70 4e 65 78 74 41 6c 6c 3b 0a 20 20  Pg->pNextAll;.  
e6dd0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a    }else{.      *
e6de0 70 70 50 67 20 3d 20 70 50 67 2d 3e 70 4e 65 78  ppPg = pPg->pNex
e6df0 74 41 6c 6c 3b 0a 20 20 20 20 20 20 49 4f 54 52  tAll;.      IOTR
e6e00 41 43 45 28 28 22 50 47 46 52 45 45 20 25 70 20  ACE(("PGFREE %p 
e6e10 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70  %d\n", pPager, p
e6e20 50 67 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20  Pg->pgno));.    
e6e30 20 20 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c    PAGER_INCR(sql
e6e40 69 74 65 33 5f 70 61 67 65 72 5f 70 67 66 72 65  ite3_pager_pgfre
e6e50 65 5f 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20  e_count);.      
e6e60 75 6e 6c 69 6e 6b 50 61 67 65 28 70 50 67 29 3b  unlinkPage(pPg);
e6e70 0a 20 20 20 20 20 20 6d 61 6b 65 43 6c 65 61 6e  .      makeClean
e6e80 28 70 50 67 29 3b 0a 20 20 20 20 20 20 73 71 6c  (pPg);.      sql
e6e90 69 74 65 33 5f 66 72 65 65 28 70 50 67 2d 3e 70  ite3_free(pPg->p
e6ea0 44 61 74 61 29 3b 0a 20 20 20 20 20 20 73 71 6c  Data);.      sql
e6eb0 69 74 65 33 5f 66 72 65 65 28 70 50 67 29 3b 0a  ite3_free(pPg);.
e6ec0 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 50        pPager->nP
e6ed0 61 67 65 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 7d  age--;.    }.  }
e6ee0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f  .}../*.** Try to
e6ef0 20 6f 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f   obtain a lock o
e6f00 6e 20 61 20 66 69 6c 65 2e 20 20 49 6e 76 6f 6b  n a file.  Invok
e6f10 65 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62  e the busy callb
e6f20 61 63 6b 20 69 66 20 74 68 65 20 6c 6f 63 6b 0a  ack if the lock.
e6f30 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  ** is currently 
e6f40 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 20  not available.  
e6f50 52 65 70 65 61 74 20 75 6e 74 69 6c 20 74 68 65  Repeat until the
e6f60 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 72   busy callback r
e6f70 65 74 75 72 6e 73 0a 2a 2a 20 66 61 6c 73 65 20  eturns.** false 
e6f80 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 6c 6f 63  or until the loc
e6f90 6b 20 73 75 63 63 65 65 64 73 2e 0a 2a 2a 0a 2a  k succeeds..**.*
e6fa0 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  * Return SQLITE_
e6fb0 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e  OK on success an
e6fc0 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  d an error code 
e6fd0 69 66 20 77 65 20 63 61 6e 6e 6f 74 20 6f 62 74  if we cannot obt
e6fe0 61 69 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e  ain.** the lock.
e6ff0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
e7000 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63  ager_wait_on_loc
e7010 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  k(Pager *pPager,
e7020 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a   int locktype){.
e7030 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20    int rc;..  /* 
e7040 54 68 65 20 4f 53 20 6c 6f 63 6b 20 76 61 6c 75  The OS lock valu
e7050 65 73 20 6d 75 73 74 20 62 65 20 74 68 65 20 73  es must be the s
e7060 61 6d 65 20 61 73 20 74 68 65 20 50 61 67 65 72  ame as the Pager
e7070 20 6c 6f 63 6b 20 76 61 6c 75 65 73 20 2a 2f 0a   lock values */.
e7080 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f    assert( PAGER_
e7090 53 48 41 52 45 44 3d 3d 53 48 41 52 45 44 5f 4c  SHARED==SHARED_L
e70a0 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  OCK );.  assert(
e70b0 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 3d   PAGER_RESERVED=
e70c0 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29  =RESERVED_LOCK )
e70d0 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45  ;.  assert( PAGE
e70e0 52 5f 45 58 43 4c 55 53 49 56 45 3d 3d 45 58 43  R_EXCLUSIVE==EXC
e70f0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 3b 0a 0a  LUSIVE_LOCK );..
e7100 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65    /* If the file
e7110 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 75 6e   is currently un
e7120 6c 6f 63 6b 65 64 20 74 68 65 6e 20 74 68 65 20  locked then the 
e7130 73 69 7a 65 20 6d 75 73 74 20 62 65 20 75 6e 6b  size must be unk
e7140 6e 6f 77 6e 20 2a 2f 0a 20 20 61 73 73 65 72 74  nown */.  assert
e7150 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
e7160 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 7c 7c  =PAGER_SHARED ||
e7170 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3c   pPager->dbSize<
e7180 30 20 7c 7c 20 4d 45 4d 44 42 20 29 3b 0a 0a 20  0 || MEMDB );.. 
e7190 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61   if( pPager->sta
e71a0 74 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a  te>=locktype ){.
e71b0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
e71c0 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  OK;.  }else{.   
e71d0 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70 42 75   if( pPager->pBu
e71e0 73 79 48 61 6e 64 6c 65 72 20 29 20 70 50 61 67  syHandler ) pPag
e71f0 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72  er->pBusyHandler
e7200 2d 3e 6e 42 75 73 79 20 3d 20 30 3b 0a 20 20 20  ->nBusy = 0;.   
e7210 20 64 6f 20 7b 0a 20 20 20 20 20 20 72 63 20 3d   do {.      rc =
e7220 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70   sqlite3OsLock(p
e7230 50 61 67 65 72 2d 3e 66 64 2c 20 6c 6f 63 6b 74  Pager->fd, lockt
e7240 79 70 65 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65  ype);.    }while
e7250 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53  ( rc==SQLITE_BUS
e7260 59 20 26 26 20 73 71 6c 69 74 65 33 49 6e 76 6f  Y && sqlite3Invo
e7270 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 70 50  keBusyHandler(pP
e7280 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c  ager->pBusyHandl
e7290 65 72 29 20 29 3b 0a 20 20 20 20 69 66 28 20 72  er) );.    if( r
e72a0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
e72b0 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74        pPager->st
e72c0 61 74 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a  ate = locktype;.
e72d0 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22        IOTRACE(("
e72e0 4c 4f 43 4b 20 25 70 20 25 64 5c 6e 22 2c 20 70  LOCK %p %d\n", p
e72f0 50 61 67 65 72 2c 20 6c 6f 63 6b 74 79 70 65 29  Pager, locktype)
e7300 29 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ).    }.  }.  re
e7310 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
e7320 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 66  * Truncate the f
e7330 69 6c 65 20 74 6f 20 74 68 65 20 6e 75 6d 62 65  ile to the numbe
e7340 72 20 6f 66 20 70 61 67 65 73 20 73 70 65 63 69  r of pages speci
e7350 66 69 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  fied..*/.SQLITE_
e7360 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
e7370 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 65  te3PagerTruncate
e7380 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
e7390 50 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20 69  Pgno nPage){.  i
e73a0 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28  nt rc;.  assert(
e73b0 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d   pPager->state>=
e73c0 50 41 47 45 52 5f 53 48 41 52 45 44 20 7c 7c 20  PAGER_SHARED || 
e73d0 4d 45 4d 44 42 20 29 3b 0a 20 20 73 71 6c 69 74  MEMDB );.  sqlit
e73e0 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
e73f0 28 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20  (pPager);.  if( 
e7400 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20  pPager->errCode 
e7410 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 50 61 67  ){.    rc = pPag
e7420 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 20  er->errCode;.   
e7430 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
e7440 20 20 69 66 28 20 6e 50 61 67 65 3e 3d 28 75 6e    if( nPage>=(un
e7450 73 69 67 6e 65 64 29 70 50 61 67 65 72 2d 3e 64  signed)pPager->d
e7460 62 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 65 74  bSize ){.    ret
e7470 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
e7480 20 7d 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 29   }.  if( MEMDB )
e7490 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62  {.    pPager->db
e74a0 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20  Size = nPage;.  
e74b0 20 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65    pager_truncate
e74c0 5f 63 61 63 68 65 28 70 50 61 67 65 72 29 3b 0a  _cache(pPager);.
e74d0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
e74e0 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 61 67 65  E_OK;.  }.  page
e74f0 72 45 6e 74 65 72 28 70 50 61 67 65 72 29 3b 0a  rEnter(pPager);.
e7500 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f 75 72 6e    rc = syncJourn
e7510 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 70 61  al(pPager);.  pa
e7520 67 65 72 4c 65 61 76 65 28 70 50 61 67 65 72 29  gerLeave(pPager)
e7530 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
e7540 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74  TE_OK ){.    ret
e7550 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f  urn rc;.  }..  /
e7560 2a 20 47 65 74 20 61 6e 20 65 78 63 6c 75 73 69  * Get an exclusi
e7570 76 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  ve lock on the d
e7580 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 20 74  atabase before t
e7590 72 75 6e 63 61 74 69 6e 67 2e 20 2a 2f 0a 20 20  runcating. */.  
e75a0 70 61 67 65 72 45 6e 74 65 72 28 70 50 61 67 65  pagerEnter(pPage
e75b0 72 29 3b 0a 20 20 72 63 20 3d 20 70 61 67 65 72  r);.  rc = pager
e75c0 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50  _wait_on_lock(pP
e75d0 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f  ager, EXCLUSIVE_
e75e0 4c 4f 43 4b 29 3b 0a 20 20 70 61 67 65 72 4c 65  LOCK);.  pagerLe
e75f0 61 76 65 28 70 50 61 67 65 72 29 3b 0a 20 20 69  ave(pPager);.  i
e7600 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
e7610 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
e7620 63 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 70  c;.  }..  rc = p
e7630 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 70 50  ager_truncate(pP
e7640 61 67 65 72 2c 20 6e 50 61 67 65 29 3b 0a 20 20  ager, nPage);.  
e7650 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
e7660 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20 74 68 65  .** Shutdown the
e7670 20 70 61 67 65 20 63 61 63 68 65 2e 20 20 46 72   page cache.  Fr
e7680 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6e  ee all memory an
e7690 64 20 63 6c 6f 73 65 20 61 6c 6c 20 66 69 6c 65  d close all file
e76a0 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 72  s..**.** If a tr
e76b0 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 69 6e  ansaction was in
e76c0 20 70 72 6f 67 72 65 73 73 20 77 68 65 6e 20 74   progress when t
e76d0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
e76e0 61 6c 6c 65 64 2c 20 74 68 61 74 0a 2a 2a 20 74  alled, that.** t
e76f0 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f  ransaction is ro
e7700 6c 6c 65 64 20 62 61 63 6b 2e 20 20 41 6c 6c 20  lled back.  All 
e7710 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65  outstanding page
e7720 73 20 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65  s are invalidate
e7730 64 0a 2a 2a 20 61 6e 64 20 74 68 65 69 72 20 6d  d.** and their m
e7740 65 6d 6f 72 79 20 69 73 20 66 72 65 65 64 2e 20  emory is freed. 
e7750 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20   Any attempt to 
e7760 75 73 65 20 61 20 70 61 67 65 20 61 73 73 6f 63  use a page assoc
e7770 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68  iated.** with th
e7780 69 73 20 70 61 67 65 20 63 61 63 68 65 20 61 66  is page cache af
e7790 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ter this functio
e77a0 6e 20 72 65 74 75 72 6e 73 20 77 69 6c 6c 20 6c  n returns will l
e77b0 69 6b 65 6c 79 0a 2a 2a 20 72 65 73 75 6c 74 20  ikely.** result 
e77c0 69 6e 20 61 20 63 6f 72 65 64 75 6d 70 2e 0a 2a  in a coredump..*
e77d0 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
e77e0 6f 6e 20 61 6c 77 61 79 73 20 73 75 63 63 65 65  on always succee
e77f0 64 73 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63  ds. If a transac
e7800 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 61  tion is active a
e7810 6e 20 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73 20  n attempt.** is 
e7820 6d 61 64 65 20 74 6f 20 72 6f 6c 6c 20 69 74 20  made to roll it 
e7830 62 61 63 6b 2e 20 49 66 20 61 6e 20 65 72 72 6f  back. If an erro
e7840 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20  r occurs during 
e7850 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 0a 2a 2a  the rollback .**
e7860 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 6d   a hot journal m
e7870 61 79 20 62 65 20 6c 65 66 74 20 69 6e 20 74 68  ay be left in th
e7880 65 20 66 69 6c 65 73 79 73 74 65 6d 20 62 75 74  e filesystem but
e7890 20 6e 6f 20 65 72 72 6f 72 20 69 73 20 72 65 74   no error is ret
e78a0 75 72 6e 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20  urned.** to the 
e78b0 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  caller..*/.SQLIT
e78c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
e78d0 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28  lite3PagerClose(
e78e0 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
e78f0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
e7900 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41  ABLE_MEMORY_MANA
e7910 47 45 4d 45 4e 54 0a 20 20 69 66 28 20 21 4d 45  GEMENT.  if( !ME
e7920 4d 44 42 20 29 7b 0a 23 69 66 6e 64 65 66 20 53  MDB ){.#ifndef S
e7930 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50  QLITE_MUTEX_NOOP
e7940 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
e7950 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69  ex *mutex = sqli
e7960 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
e7970 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
e7980 54 49 43 5f 4d 45 4d 32 29 3b 0a 23 65 6e 64 69  TIC_MEM2);.#endi
e7990 66 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  f.    sqlite3_mu
e79a0 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29  tex_enter(mutex)
e79b0 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72  ;.    if( pPager
e79c0 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 20  ->pPrev ){.     
e79d0 20 70 50 61 67 65 72 2d 3e 70 50 72 65 76 2d 3e   pPager->pPrev->
e79e0 70 4e 65 78 74 20 3d 20 70 50 61 67 65 72 2d 3e  pNext = pPager->
e79f0 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65  pNext;.    }else
e7a00 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50  {.      sqlite3P
e7a10 61 67 65 72 4c 69 73 74 20 3d 20 70 50 61 67 65  agerList = pPage
e7a20 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a  r->pNext;.    }.
e7a30 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
e7a40 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 70  pNext ){.      p
e7a50 50 61 67 65 72 2d 3e 70 4e 65 78 74 2d 3e 70 50  Pager->pNext->pP
e7a60 72 65 76 20 3d 20 70 50 61 67 65 72 2d 3e 70 50  rev = pPager->pP
e7a70 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  rev;.    }.    s
e7a80 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
e7a90 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 23  ve(mutex);.  }.#
e7aa0 65 6e 64 69 66 0a 0a 20 20 64 69 73 61 62 6c 65  endif..  disable
e7ab0 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72  _simulated_io_er
e7ac0 72 6f 72 73 28 29 3b 0a 20 20 73 71 6c 69 74 65  rors();.  sqlite
e7ad0 33 46 61 75 6c 74 42 65 67 69 6e 42 65 6e 69 67  3FaultBeginBenig
e7ae0 6e 28 2d 31 29 3b 0a 20 20 70 50 61 67 65 72 2d  n(-1);.  pPager-
e7af0 3e 65 72 72 43 6f 64 65 20 3d 20 30 3b 0a 20 20  >errCode = 0;.  
e7b00 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
e7b10 65 4d 6f 64 65 20 3d 20 30 3b 0a 20 20 70 61 67  eMode = 0;.  pag
e7b20 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29  er_reset(pPager)
e7b30 3b 0a 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41  ;.  pagerUnlockA
e7b40 6e 64 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65  ndRollback(pPage
e7b50 72 29 3b 0a 20 20 65 6e 61 62 6c 65 5f 73 69 6d  r);.  enable_sim
e7b60 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73  ulated_io_errors
e7b70 28 29 3b 0a 20 20 73 71 6c 69 74 65 33 46 61 75  ();.  sqlite3Fau
e7b80 6c 74 45 6e 64 42 65 6e 69 67 6e 28 2d 31 29 3b  ltEndBenign(-1);
e7b90 0a 20 20 50 41 47 45 52 54 52 41 43 45 32 28 22  .  PAGERTRACE2("
e7ba0 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 50 41 47  CLOSE %d\n", PAG
e7bb0 45 52 49 44 28 70 50 61 67 65 72 29 29 3b 0a 20  ERID(pPager));. 
e7bc0 20 49 4f 54 52 41 43 45 28 28 22 43 4c 4f 53 45   IOTRACE(("CLOSE
e7bd0 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29   %p\n", pPager))
e7be0 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a  .  if( pPager->j
e7bf0 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 7b 0a 20 20  ournalOpen ){.  
e7c00 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65    sqlite3OsClose
e7c10 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20  (pPager->jfd);. 
e7c20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76   }.  sqlite3Bitv
e7c30 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72  ecDestroy(pPager
e7c40 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20  ->pInJournal);. 
e7c50 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 6d   if( pPager->stm
e7c60 74 4f 70 65 6e 20 29 7b 0a 20 20 20 20 73 71 6c  tOpen ){.    sql
e7c70 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67  ite3OsClose(pPag
e7c80 65 72 2d 3e 73 74 66 64 29 3b 0a 20 20 7d 0a 20  er->stfd);.  }. 
e7c90 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28   sqlite3OsClose(
e7ca0 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 2f  pPager->fd);.  /
e7cb0 2a 20 54 65 6d 70 20 66 69 6c 65 73 20 61 72 65  * Temp files are
e7cc0 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64   automatically d
e7cd0 65 6c 65 74 65 64 20 62 79 20 74 68 65 20 4f 53  eleted by the OS
e7ce0 0a 20 20 2a 2a 20 69 66 28 20 70 50 61 67 65 72  .  ** if( pPager
e7cf0 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20  ->tempFile ){.  
e7d00 2a 2a 20 20 20 73 71 6c 69 74 65 33 4f 73 44 65  **   sqlite3OsDe
e7d10 6c 65 74 65 28 70 50 61 67 65 72 2d 3e 7a 46 69  lete(pPager->zFi
e7d20 6c 65 6e 61 6d 65 29 3b 0a 20 20 2a 2a 20 7d 0a  lename);.  ** }.
e7d30 20 20 2a 2f 0a 0a 20 20 73 71 6c 69 74 65 33 5f    */..  sqlite3_
e7d40 66 72 65 65 28 70 50 61 67 65 72 2d 3e 61 48 61  free(pPager->aHa
e7d50 73 68 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  sh);.  sqlite3_f
e7d60 72 65 65 28 70 50 61 67 65 72 2d 3e 70 54 6d 70  ree(pPager->pTmp
e7d70 53 70 61 63 65 29 3b 0a 20 20 73 71 6c 69 74 65  Space);.  sqlite
e7d80 33 5f 66 72 65 65 28 70 50 61 67 65 72 29 3b 0a  3_free(pPager);.
e7d90 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e7da0 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69  OK;.}..#if !defi
e7db0 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64  ned(NDEBUG) || d
e7dc0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45  efined(SQLITE_TE
e7dd0 53 54 29 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  ST)./*.** Return
e7de0 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
e7df0 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 70   for the given p
e7e00 61 67 65 20 64 61 74 61 2e 0a 2a 2f 0a 53 51 4c  age data..*/.SQL
e7e10 49 54 45 5f 50 52 49 56 41 54 45 20 50 67 6e 6f  ITE_PRIVATE Pgno
e7e20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
e7e30 65 6e 75 6d 62 65 72 28 44 62 50 61 67 65 20 2a  enumber(DbPage *
e7e40 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e  p){.  return p->
e7e50 70 67 6e 6f 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  pgno;.}.#endif..
e7e60 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 5f 72  /*.** The page_r
e7e70 65 66 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 6e  ef() function in
e7e80 63 72 65 6d 65 6e 74 73 20 74 68 65 20 72 65 66  crements the ref
e7e90 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72  erence count for
e7ea0 20 61 20 70 61 67 65 2e 0a 2a 2a 20 49 66 20 74   a page..** If t
e7eb0 68 65 20 70 61 67 65 20 69 73 20 63 75 72 72 65  he page is curre
e7ec0 6e 74 6c 79 20 6f 6e 20 74 68 65 20 66 72 65 65  ntly on the free
e7ed0 6c 69 73 74 20 28 74 68 65 20 72 65 66 65 72 65  list (the refere
e7ee0 6e 63 65 20 63 6f 75 6e 74 20 69 73 20 7a 65 72  nce count is zer
e7ef0 6f 29 20 74 68 65 6e 0a 2a 2a 20 72 65 6d 6f 76  o) then.** remov
e7f00 65 20 69 74 20 66 72 6f 6d 20 74 68 65 20 66 72  e it from the fr
e7f10 65 65 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f  eelist..**.** Fo
e7f20 72 20 6e 6f 6e 2d 74 65 73 74 20 73 79 73 74 65  r non-test syste
e7f30 6d 73 2c 20 70 61 67 65 5f 72 65 66 28 29 20 69  ms, page_ref() i
e7f40 73 20 61 20 6d 61 63 72 6f 20 74 68 61 74 20 63  s a macro that c
e7f50 61 6c 6c 73 20 5f 70 61 67 65 5f 72 65 66 28 29  alls _page_ref()
e7f60 0a 2a 2a 20 6f 6e 6c 69 6e 65 20 6f 66 20 74 68  .** online of th
e7f70 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
e7f80 74 20 69 73 20 7a 65 72 6f 2e 20 20 46 6f 72 20  t is zero.  For 
e7f90 74 65 73 74 20 73 79 73 74 65 6d 73 2c 20 70 61  test systems, pa
e7fa0 67 65 5f 72 65 66 28 29 0a 2a 2a 20 69 73 20 61  ge_ref().** is a
e7fb0 20 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e 20 73   real function s
e7fc0 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20 73 65  o that we can se
e7fd0 74 20 62 72 65 61 6b 70 6f 69 6e 74 73 20 61 6e  t breakpoints an
e7fe0 64 20 74 72 61 63 65 20 69 74 2e 0a 2a 2f 0a 73  d trace it..*/.s
e7ff0 74 61 74 69 63 20 76 6f 69 64 20 5f 70 61 67 65  tatic void _page
e8000 5f 72 65 66 28 50 67 48 64 72 20 2a 70 50 67 29  _ref(PgHdr *pPg)
e8010 7b 0a 20 20 69 66 28 20 70 50 67 2d 3e 6e 52 65  {.  if( pPg->nRe
e8020 66 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54  f==0 ){.    /* T
e8030 68 65 20 70 61 67 65 20 69 73 20 63 75 72 72 65  he page is curre
e8040 6e 74 6c 79 20 6f 6e 20 74 68 65 20 66 72 65 65  ntly on the free
e8050 6c 69 73 74 2e 20 20 52 65 6d 6f 76 65 20 69 74  list.  Remove it
e8060 2e 20 2a 2f 0a 20 20 20 20 6c 72 75 4c 69 73 74  . */.    lruList
e8070 52 65 6d 6f 76 65 28 70 50 67 29 3b 0a 20 20 20  Remove(pPg);.   
e8080 20 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e 6e 52   pPg->pPager->nR
e8090 65 66 2b 2b 3b 0a 20 20 7d 0a 20 20 70 50 67 2d  ef++;.  }.  pPg-
e80a0 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 23 69 66 64 65  >nRef++;.}.#ifde
e80b0 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
e80c0 20 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67   static void pag
e80d0 65 5f 72 65 66 28 50 67 48 64 72 20 2a 70 50 67  e_ref(PgHdr *pPg
e80e0 29 7b 0a 20 20 20 20 69 66 28 20 70 50 67 2d 3e  ){.    if( pPg->
e80f0 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20  nRef==0 ){.     
e8100 20 5f 70 61 67 65 5f 72 65 66 28 70 50 67 29 3b   _page_ref(pPg);
e8110 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
e8120 20 20 70 50 67 2d 3e 6e 52 65 66 2b 2b 3b 0a 20    pPg->nRef++;. 
e8130 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 23     }.  }.#else.#
e8140 20 64 65 66 69 6e 65 20 70 61 67 65 5f 72 65 66   define page_ref
e8150 28 50 29 20 20 20 28 28 50 29 2d 3e 6e 52 65 66  (P)   ((P)->nRef
e8160 3d 3d 30 3f 5f 70 61 67 65 5f 72 65 66 28 50 29  ==0?_page_ref(P)
e8170 3a 28 76 6f 69 64 29 28 50 29 2d 3e 6e 52 65 66  :(void)(P)->nRef
e8180 2b 2b 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ++).#endif../*.*
e8190 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20  * Increment the 
e81a0 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20  reference count 
e81b0 66 6f 72 20 61 20 70 61 67 65 2e 20 20 54 68 65  for a page.  The
e81c0 20 69 6e 70 75 74 20 70 6f 69 6e 74 65 72 20 69   input pointer i
e81d0 73 0a 2a 2a 20 61 20 72 65 66 65 72 65 6e 63 65  s.** a reference
e81e0 20 74 6f 20 74 68 65 20 70 61 67 65 20 64 61 74   to the page dat
e81f0 61 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  a..*/.SQLITE_PRI
e8200 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
e8210 50 61 67 65 72 52 65 66 28 44 62 50 61 67 65 20  PagerRef(DbPage 
e8220 2a 70 50 67 29 7b 0a 20 20 70 61 67 65 72 45 6e  *pPg){.  pagerEn
e8230 74 65 72 28 70 50 67 2d 3e 70 50 61 67 65 72 29  ter(pPg->pPager)
e8240 3b 0a 20 20 70 61 67 65 5f 72 65 66 28 70 50 67  ;.  page_ref(pPg
e8250 29 3b 0a 20 20 70 61 67 65 72 4c 65 61 76 65 28  );.  pagerLeave(
e8260 70 50 67 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20  pPg->pPager);.  
e8270 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
e8280 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20  ;.}../*.** Sync 
e8290 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 6e  the journal.  In
e82a0 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 6d 61   other words, ma
e82b0 6b 65 20 73 75 72 65 20 61 6c 6c 20 74 68 65 20  ke sure all the 
e82c0 70 61 67 65 73 20 74 68 61 74 20 68 61 76 65 0a  pages that have.
e82d0 2a 2a 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20  ** been written 
e82e0 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68  to the journal h
e82f0 61 76 65 20 61 63 74 75 61 6c 6c 79 20 72 65 61  ave actually rea
e8300 63 68 65 64 20 74 68 65 20 73 75 72 66 61 63 65  ched the surface
e8310 20 6f 66 20 74 68 65 0a 2a 2a 20 64 69 73 6b 2e   of the.** disk.
e8320 20 20 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65    It is not safe
e8330 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 6f   to modify the o
e8340 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65  riginal database
e8350 20 66 69 6c 65 20 75 6e 74 69 6c 20 61 66 74 65   file until afte
e8360 72 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c  r.** the journal
e8370 20 68 61 73 20 62 65 65 6e 20 73 79 6e 63 65 64   has been synced
e8380 2e 20 20 49 66 20 74 68 65 20 6f 72 69 67 69 6e  .  If the origin
e8390 61 6c 20 64 61 74 61 62 61 73 65 20 69 73 20 6d  al database is m
e83a0 6f 64 69 66 69 65 64 20 62 65 66 6f 72 65 0a 2a  odified before.*
e83b0 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73  * the journal is
e83c0 20 73 79 6e 63 65 64 20 61 6e 64 20 61 20 70 6f   synced and a po
e83d0 77 65 72 20 66 61 69 6c 75 72 65 20 6f 63 63 75  wer failure occu
e83e0 72 73 2c 20 74 68 65 20 75 6e 73 79 6e 63 65 64  rs, the unsynced
e83f0 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 64 61 74 61   journal.** data
e8400 20 77 6f 75 6c 64 20 62 65 20 6c 6f 73 74 20 61   would be lost a
e8410 6e 64 20 77 65 20 77 6f 75 6c 64 20 62 65 20 75  nd we would be u
e8420 6e 61 62 6c 65 20 74 6f 20 63 6f 6d 70 6c 65 74  nable to complet
e8430 65 6c 79 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65  ely rollback the
e8440 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 68 61  .** database cha
e8450 6e 67 65 73 2e 20 20 44 61 74 61 62 61 73 65 20  nges.  Database 
e8460 63 6f 72 72 75 70 74 69 6f 6e 20 77 6f 75 6c 64  corruption would
e8470 20 6f 63 63 75 72 2e 0a 2a 2a 20 0a 2a 2a 20 54   occur..** .** T
e8480 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f  his routine also
e8490 20 75 70 64 61 74 65 73 20 74 68 65 20 6e 52 65   updates the nRe
e84a0 63 20 66 69 65 6c 64 20 69 6e 20 74 68 65 20 68  c field in the h
e84b0 65 61 64 65 72 20 6f 66 20 74 68 65 20 6a 6f 75  eader of the jou
e84c0 72 6e 61 6c 2e 0a 2a 2a 20 28 53 65 65 20 63 6f  rnal..** (See co
e84d0 6d 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20 70 61  mments on the pa
e84e0 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 20 72  ger_playback() r
e84f0 6f 75 74 69 6e 65 20 66 6f 72 20 61 64 64 69 74  outine for addit
e8500 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
e8510 6e 2e 29 0a 2a 2a 20 49 66 20 74 68 65 20 73 79  n.).** If the sy
e8520 6e 63 20 6d 6f 64 65 20 69 73 20 46 55 4c 4c 2c  nc mode is FULL,
e8530 20 74 77 6f 20 73 79 6e 63 73 20 77 69 6c 6c 20   two syncs will 
e8540 6f 63 63 75 72 2e 20 20 46 69 72 73 74 20 74 68  occur.  First th
e8550 65 20 77 68 6f 6c 65 20 6a 6f 75 72 6e 61 6c 0a  e whole journal.
e8560 2a 2a 20 69 73 20 73 79 6e 63 65 64 2c 20 74 68  ** is synced, th
e8570 65 6e 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c  en the nRec fiel
e8580 64 20 69 73 20 75 70 64 61 74 65 64 2c 20 74 68  d is updated, th
e8590 65 6e 20 61 20 73 65 63 6f 6e 64 20 73 79 6e 63  en a second sync
e85a0 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 46   occurs..**.** F
e85b0 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74  or temporary dat
e85c0 61 62 61 73 65 73 2c 20 77 65 20 64 6f 20 6e 6f  abases, we do no
e85d0 74 20 63 61 72 65 20 69 66 20 77 65 20 61 72 65  t care if we are
e85e0 20 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63   able to rollbac
e85f0 6b 0a 2a 2a 20 61 66 74 65 72 20 61 20 70 6f 77  k.** after a pow
e8600 65 72 20 66 61 69 6c 75 72 65 2c 20 73 6f 20 6e  er failure, so n
e8610 6f 20 73 79 6e 63 20 6f 63 63 75 72 73 2e 0a 2a  o sync occurs..*
e8620 2a 0a 2a 2a 20 49 66 20 74 68 65 20 49 4f 43 41  *.** If the IOCA
e8630 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 66 6c 61  P_SEQUENTIAL fla
e8640 67 20 69 73 20 73 65 74 20 66 6f 72 20 74 68 65  g is set for the
e8650 20 70 65 72 73 69 73 74 65 6e 74 20 6d 65 64 69   persistent medi
e8660 61 20 6f 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68  a on which.** th
e8670 65 20 64 61 74 61 62 61 73 65 20 69 73 20 73 74  e database is st
e8680 6f 72 65 64 2c 20 74 68 65 6e 20 4f 73 53 79 6e  ored, then OsSyn
e8690 63 28 29 20 69 73 20 6e 65 76 65 72 20 63 61 6c  c() is never cal
e86a0 6c 65 64 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e  led on the journ
e86b0 61 6c 0a 2a 2a 20 66 69 6c 65 2e 20 49 6e 20 74  al.** file. In t
e86c0 68 69 73 20 63 61 73 65 20 61 6c 6c 20 74 68 61  his case all tha
e86d0 74 20 69 73 20 72 65 71 75 69 72 65 64 20 69 73  t is required is
e86e0 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 6e   to update the n
e86f0 52 65 63 20 66 69 65 6c 64 20 69 6e 0a 2a 2a 20  Rec field in.** 
e8700 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  the journal head
e8710 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  er..**.** This r
e8720 6f 75 74 69 6e 65 20 63 6c 65 61 72 73 20 74 68  outine clears th
e8730 65 20 6e 65 65 64 53 79 6e 63 20 66 69 65 6c 64  e needSync field
e8740 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 20 63   of every page c
e8750 75 72 72 65 6e 74 20 68 65 6c 64 20 69 6e 0a 2a  urrent held in.*
e8760 2a 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61  * memory..*/.sta
e8770 74 69 63 20 69 6e 74 20 73 79 6e 63 4a 6f 75 72  tic int syncJour
e8780 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65  nal(Pager *pPage
e8790 72 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 67  r){.  PgHdr *pPg
e87a0 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
e87b0 49 54 45 5f 4f 4b 3b 0a 0a 0a 20 20 2f 2a 20 53  ITE_OK;...  /* S
e87c0 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ync the journal 
e87d0 62 65 66 6f 72 65 20 6d 6f 64 69 66 79 69 6e 67  before modifying
e87e0 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
e87f0 73 65 0a 20 20 2a 2a 20 28 61 73 73 75 6d 69 6e  se.  ** (assumin
e8800 67 20 74 68 65 72 65 20 69 73 20 61 20 6a 6f 75  g there is a jou
e8810 72 6e 61 6c 20 61 6e 64 20 69 74 20 6e 65 65 64  rnal and it need
e8820 73 20 74 6f 20 62 65 20 73 79 6e 63 65 64 2e 29  s to be synced.)
e8830 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67  .  */.  if( pPag
e8840 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 29 7b 0a  er->needSync ){.
e8850 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d      if( !pPager-
e8860 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20  >tempFile ){.   
e8870 20 20 20 69 6e 74 20 69 44 63 20 3d 20 73 71 6c     int iDc = sql
e8880 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72  ite3OsDeviceChar
e8890 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61 67  acteristics(pPag
e88a0 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 20 20 61  er->fd);.      a
e88b0 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a  ssert( pPager->j
e88c0 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 3b 0a 0a 20  ournalOpen );.. 
e88d0 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69 44 63       if( 0==(iDc
e88e0 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41  &SQLITE_IOCAP_SA
e88f0 46 45 5f 41 50 50 45 4e 44 29 20 29 7b 0a 20 20  FE_APPEND) ){.  
e8900 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
e8910 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20 69 6e  he nRec value in
e8920 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  to the journal f
e8930 69 6c 65 20 68 65 61 64 65 72 2e 20 49 66 20 69  ile header. If i
e8940 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6c  n.        ** ful
e8950 6c 2d 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f  l-synchronous mo
e8960 64 65 2c 20 73 79 6e 63 20 74 68 65 20 6a 6f 75  de, sync the jou
e8970 72 6e 61 6c 20 66 69 72 73 74 2e 20 54 68 69 73  rnal first. This
e8980 20 65 6e 73 75 72 65 73 20 74 68 61 74 0a 20 20   ensures that.  
e8990 20 20 20 20 20 20 2a 2a 20 61 6c 6c 20 64 61 74        ** all dat
e89a0 61 20 68 61 73 20 72 65 61 6c 6c 79 20 68 69 74  a has really hit
e89b0 20 74 68 65 20 64 69 73 6b 20 62 65 66 6f 72 65   the disk before
e89c0 20 6e 52 65 63 20 69 73 20 75 70 64 61 74 65 64   nRec is updated
e89d0 20 74 6f 20 6d 61 72 6b 0a 20 20 20 20 20 20 20   to mark.       
e89e0 20 2a 2a 20 69 74 20 61 73 20 61 20 63 61 6e 64   ** it as a cand
e89f0 69 64 61 74 65 20 66 6f 72 20 72 6f 6c 6c 62 61  idate for rollba
e8a00 63 6b 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  ck..        **. 
e8a10 20 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69         ** This i
e8a20 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 69  s not required i
e8a30 66 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 74  f the persistent
e8a40 20 6d 65 64 69 61 20 73 75 70 70 6f 72 74 73 20   media supports 
e8a50 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 53  the.        ** S
e8a60 41 46 45 5f 41 50 50 45 4e 44 20 70 72 6f 70 65  AFE_APPEND prope
e8a70 72 74 79 2e 20 42 65 63 61 75 73 65 20 69 6e 20  rty. Because in 
e8a80 74 68 69 73 20 63 61 73 65 20 69 74 20 69 73 20  this case it is 
e8a90 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 0a 20 20  not possible .  
e8aa0 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 67 61 72        ** for gar
e8ab0 62 61 67 65 20 64 61 74 61 20 74 6f 20 62 65 20  bage data to be 
e8ac0 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20  appended to the 
e8ad0 66 69 6c 65 2c 20 74 68 65 20 6e 52 65 63 20 66  file, the nRec f
e8ae0 69 65 6c 64 0a 20 20 20 20 20 20 20 20 2a 2a 20  ield.        ** 
e8af0 69 73 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74  is populated wit
e8b00 68 20 30 78 46 46 46 46 46 46 46 46 20 77 68 65  h 0xFFFFFFFF whe
e8b10 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65  n the journal he
e8b20 61 64 65 72 20 69 73 20 77 72 69 74 74 65 6e 0a  ader is written.
e8b30 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6e          ** and n
e8b40 65 76 65 72 20 6e 65 65 64 73 20 74 6f 20 62 65  ever needs to be
e8b50 20 75 70 64 61 74 65 64 2e 0a 20 20 20 20 20 20   updated..      
e8b60 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 36 34    */.        i64
e8b70 20 6a 72 6e 6c 4f 66 66 3b 0a 20 20 20 20 20 20   jrnlOff;.      
e8b80 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 66 75    if( pPager->fu
e8b90 6c 6c 53 79 6e 63 20 26 26 20 30 3d 3d 28 69 44  llSync && 0==(iD
e8ba0 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53  c&SQLITE_IOCAP_S
e8bb0 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20  EQUENTIAL) ){.  
e8bc0 20 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41          PAGERTRA
e8bd0 43 45 32 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61  CE2("SYNC journa
e8be0 6c 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45  l of %d\n", PAGE
e8bf0 52 49 44 28 70 50 61 67 65 72 29 29 3b 0a 20 20  RID(pPager));.  
e8c00 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28          IOTRACE(
e8c10 28 22 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70  ("JSYNC %p\n", p
e8c20 50 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 20  Pager)).        
e8c30 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
e8c40 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66 64  Sync(pPager->jfd
e8c50 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66  , pPager->sync_f
e8c60 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 20 20 20  lags);.         
e8c70 20 69 66 28 20 72 63 21 3d 30 20 29 20 72 65 74   if( rc!=0 ) ret
e8c80 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20  urn rc;.        
e8c90 7d 0a 0a 20 20 20 20 20 20 20 20 6a 72 6e 6c 4f  }..        jrnlO
e8ca0 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75  ff = pPager->jou
e8cb0 72 6e 61 6c 48 64 72 20 2b 20 73 69 7a 65 6f 66  rnalHdr + sizeof
e8cc0 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 3b  (aJournalMagic);
e8cd0 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45  .        IOTRACE
e8ce0 28 28 22 4a 48 44 52 20 25 70 20 25 6c 6c 64 20  (("JHDR %p %lld 
e8cf0 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 6a  %d\n", pPager, j
e8d00 72 6e 6c 4f 66 66 2c 20 34 29 29 3b 0a 20 20 20  rnlOff, 4));.   
e8d10 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 33       rc = write3
e8d20 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66  2bits(pPager->jf
e8d30 64 2c 20 6a 72 6e 6c 4f 66 66 2c 20 70 50 61 67  d, jrnlOff, pPag
e8d40 65 72 2d 3e 6e 52 65 63 29 3b 0a 20 20 20 20 20  er->nRec);.     
e8d50 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75     if( rc ) retu
e8d60 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20  rn rc;.      }. 
e8d70 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69 44 63       if( 0==(iDc
e8d80 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45  &SQLITE_IOCAP_SE
e8d90 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20 20  QUENTIAL) ){.   
e8da0 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45 32       PAGERTRACE2
e8db0 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20 6f  ("SYNC journal o
e8dc0 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44  f %d\n", PAGERID
e8dd0 28 70 50 61 67 65 72 29 29 3b 0a 20 20 20 20 20  (pPager));.     
e8de0 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 53 59     IOTRACE(("JSY
e8df0 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72  NC %p\n", pPager
e8e00 29 29 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  )).        rc = 
e8e10 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50  sqlite3OsSync(pP
e8e20 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65  ager->jfd, pPage
e8e30 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 7c 20 0a  r->sync_flags| .
e8e40 20 20 20 20 20 20 20 20 20 20 28 70 50 61 67 65            (pPage
e8e50 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 3d 3d 53  r->sync_flags==S
e8e60 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 3f  QLITE_SYNC_FULL?
e8e70 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41  SQLITE_SYNC_DATA
e8e80 4f 4e 4c 59 3a 30 29 0a 20 20 20 20 20 20 20 20  ONLY:0).        
e8e90 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
e8ea0 63 21 3d 30 20 29 20 72 65 74 75 72 6e 20 72 63  c!=0 ) return rc
e8eb0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
e8ec0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53  pPager->journalS
e8ed0 74 61 72 74 65 64 20 3d 20 31 3b 0a 20 20 20 20  tarted = 1;.    
e8ee0 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65  }.    pPager->ne
e8ef0 65 64 53 79 6e 63 20 3d 20 30 3b 0a 0a 20 20 20  edSync = 0;..   
e8f00 20 2f 2a 20 45 72 61 73 65 20 74 68 65 20 6e 65   /* Erase the ne
e8f10 65 64 53 79 6e 63 20 66 6c 61 67 20 66 72 6f 6d  edSync flag from
e8f20 20 65 76 65 72 79 20 70 61 67 65 2e 0a 20 20 20   every page..   
e8f30 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70 50 67 3d   */.    for(pPg=
e8f40 70 50 61 67 65 72 2d 3e 70 41 6c 6c 3b 20 70 50  pPager->pAll; pP
e8f50 67 3b 20 70 50 67 3d 70 50 67 2d 3e 70 4e 65 78  g; pPg=pPg->pNex
e8f60 74 41 6c 6c 29 7b 0a 20 20 20 20 20 20 70 50 67  tAll){.      pPg
e8f70 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a  ->needSync = 0;.
e8f80 20 20 20 20 7d 0a 20 20 20 20 6c 72 75 4c 69 73      }.    lruLis
e8f90 74 53 65 74 46 69 72 73 74 53 79 6e 63 65 64 28  tSetFirstSynced(
e8fa0 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 23 69  pPager);.  }..#i
e8fb0 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f  fndef NDEBUG.  /
e8fc0 2a 20 49 66 20 74 68 65 20 50 61 67 65 72 2e 6e  * If the Pager.n
e8fd0 65 65 64 53 79 6e 63 20 66 6c 61 67 20 69 73 20  eedSync flag is 
e8fe0 63 6c 65 61 72 20 74 68 65 6e 20 74 68 65 20 50  clear then the P
e8ff0 67 48 64 72 2e 6e 65 65 64 53 79 6e 63 0a 20 20  gHdr.needSync.  
e9000 2a 2a 20 66 6c 61 67 20 6d 75 73 74 20 61 6c 73  ** flag must als
e9010 6f 20 62 65 20 63 6c 65 61 72 20 66 6f 72 20 61  o be clear for a
e9020 6c 6c 20 70 61 67 65 73 2e 20 20 56 65 72 69 66  ll pages.  Verif
e9030 79 20 74 68 61 74 20 74 68 69 73 0a 20 20 2a 2a  y that this.  **
e9040 20 69 6e 76 61 72 69 61 6e 74 20 69 73 20 74 72   invariant is tr
e9050 75 65 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 7b  ue..  */.  else{
e9060 0a 20 20 20 20 66 6f 72 28 70 50 67 3d 70 50 61  .    for(pPg=pPa
e9070 67 65 72 2d 3e 70 41 6c 6c 3b 20 70 50 67 3b 20  ger->pAll; pPg; 
e9080 70 50 67 3d 70 50 67 2d 3e 70 4e 65 78 74 41 6c  pPg=pPg->pNextAl
e9090 6c 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  l){.      assert
e90a0 28 20 70 50 67 2d 3e 6e 65 65 64 53 79 6e 63 3d  ( pPg->needSync=
e90b0 3d 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  =0 );.    }.    
e90c0 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
e90d0 6c 72 75 2e 70 46 69 72 73 74 53 79 6e 63 65 64  lru.pFirstSynced
e90e0 3d 3d 70 50 61 67 65 72 2d 3e 6c 72 75 2e 70 46  ==pPager->lru.pF
e90f0 69 72 73 74 20 29 3b 0a 20 20 7d 0a 23 65 6e 64  irst );.  }.#end
e9100 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  if..  return rc;
e9110 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 20  .}../*.** Merge 
e9120 74 77 6f 20 6c 69 73 74 73 20 6f 66 20 70 61 67  two lists of pag
e9130 65 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20  es connected by 
e9140 70 44 69 72 74 79 20 61 6e 64 20 69 6e 20 70 67  pDirty and in pg
e9150 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a 20 44 6f 20  no order..** Do 
e9160 6e 6f 74 20 62 6f 74 68 20 66 69 78 69 6e 67 20  not both fixing 
e9170 74 68 65 20 70 50 72 65 76 44 69 72 74 79 20 70  the pPrevDirty p
e9180 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74  ointers..*/.stat
e9190 69 63 20 50 67 48 64 72 20 2a 6d 65 72 67 65 5f  ic PgHdr *merge_
e91a0 70 61 67 65 6c 69 73 74 28 50 67 48 64 72 20 2a  pagelist(PgHdr *
e91b0 70 41 2c 20 50 67 48 64 72 20 2a 70 42 29 7b 0a  pA, PgHdr *pB){.
e91c0 20 20 50 67 48 64 72 20 72 65 73 75 6c 74 2c 20    PgHdr result, 
e91d0 2a 70 54 61 69 6c 3b 0a 20 20 70 54 61 69 6c 20  *pTail;.  pTail 
e91e0 3d 20 26 72 65 73 75 6c 74 3b 0a 20 20 77 68 69  = &result;.  whi
e91f0 6c 65 28 20 70 41 20 26 26 20 70 42 20 29 7b 0a  le( pA && pB ){.
e9200 20 20 20 20 69 66 28 20 70 41 2d 3e 70 67 6e 6f      if( pA->pgno
e9210 3c 70 42 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 20  <pB->pgno ){.   
e9220 20 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 79     pTail->pDirty
e9230 20 3d 20 70 41 3b 0a 20 20 20 20 20 20 70 54 61   = pA;.      pTa
e9240 69 6c 20 3d 20 70 41 3b 0a 20 20 20 20 20 20 70  il = pA;.      p
e9250 41 20 3d 20 70 41 2d 3e 70 44 69 72 74 79 3b 0a  A = pA->pDirty;.
e9260 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
e9270 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d   pTail->pDirty =
e9280 20 70 42 3b 0a 20 20 20 20 20 20 70 54 61 69 6c   pB;.      pTail
e9290 20 3d 20 70 42 3b 0a 20 20 20 20 20 20 70 42 20   = pB;.      pB 
e92a0 3d 20 70 42 2d 3e 70 44 69 72 74 79 3b 0a 20 20  = pB->pDirty;.  
e92b0 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 41    }.  }.  if( pA
e92c0 20 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e 70   ){.    pTail->p
e92d0 44 69 72 74 79 20 3d 20 70 41 3b 0a 20 20 7d 65  Dirty = pA;.  }e
e92e0 6c 73 65 20 69 66 28 20 70 42 20 29 7b 0a 20 20  lse if( pB ){.  
e92f0 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 79 20    pTail->pDirty 
e9300 3d 20 70 42 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  = pB;.  }else{. 
e9310 20 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 79     pTail->pDirty
e9320 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 0;.  }.  retu
e9330 72 6e 20 72 65 73 75 6c 74 2e 70 44 69 72 74 79  rn result.pDirty
e9340 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 72 74 20  ;.}../*.** Sort 
e9350 74 68 65 20 6c 69 73 74 20 6f 66 20 70 61 67 65  the list of page
e9360 73 20 69 6e 20 61 63 63 65 6e 64 69 6e 67 20 6f  s in accending o
e9370 72 64 65 72 20 62 79 20 70 67 6e 6f 2e 20 20 50  rder by pgno.  P
e9380 61 67 65 73 20 61 72 65 0a 2a 2a 20 63 6f 6e 6e  ages are.** conn
e9390 65 63 74 65 64 20 62 79 20 70 44 69 72 74 79 20  ected by pDirty 
e93a0 70 6f 69 6e 74 65 72 73 2e 20 20 54 68 65 20 70  pointers.  The p
e93b0 50 72 65 76 44 69 72 74 79 20 70 6f 69 6e 74 65  PrevDirty pointe
e93c0 72 73 20 61 72 65 0a 2a 2a 20 63 6f 72 72 75 70  rs are.** corrup
e93d0 74 65 64 20 62 79 20 74 68 69 73 20 73 6f 72 74  ted by this sort
e93e0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 53  ..*/.#define N_S
e93f0 4f 52 54 5f 42 55 43 4b 45 54 5f 41 4c 4c 4f 43  ORT_BUCKET_ALLOC
e9400 20 32 35 0a 23 64 65 66 69 6e 65 20 4e 5f 53 4f   25.#define N_SO
e9410 52 54 5f 42 55 43 4b 45 54 20 20 20 20 20 20 20  RT_BUCKET       
e9420 32 35 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  25.#ifdef SQLITE
e9430 5f 54 45 53 54 0a 20 20 69 6e 74 20 73 71 6c 69  _TEST.  int sqli
e9440 74 65 33 5f 70 61 67 65 72 5f 6e 5f 73 6f 72 74  te3_pager_n_sort
e9450 5f 62 75 63 6b 65 74 20 3d 20 30 3b 0a 20 20 23  _bucket = 0;.  #
e9460 75 6e 64 65 66 20 4e 5f 53 4f 52 54 5f 42 55 43  undef N_SORT_BUC
e9470 4b 45 54 0a 20 20 23 64 65 66 69 6e 65 20 4e 5f  KET.  #define N_
e9480 53 4f 52 54 5f 42 55 43 4b 45 54 20 5c 0a 20 20  SORT_BUCKET \.  
e9490 20 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f   (sqlite3_pager_
e94a0 6e 5f 73 6f 72 74 5f 62 75 63 6b 65 74 3f 73 71  n_sort_bucket?sq
e94b0 6c 69 74 65 33 5f 70 61 67 65 72 5f 6e 5f 73 6f  lite3_pager_n_so
e94c0 72 74 5f 62 75 63 6b 65 74 3a 4e 5f 53 4f 52 54  rt_bucket:N_SORT
e94d0 5f 42 55 43 4b 45 54 5f 41 4c 4c 4f 43 29 0a 23  _BUCKET_ALLOC).#
e94e0 65 6e 64 69 66 0a 73 74 61 74 69 63 20 50 67 48  endif.static PgH
e94f0 64 72 20 2a 73 6f 72 74 5f 70 61 67 65 6c 69 73  dr *sort_pagelis
e9500 74 28 50 67 48 64 72 20 2a 70 49 6e 29 7b 0a 20  t(PgHdr *pIn){. 
e9510 20 50 67 48 64 72 20 2a 61 5b 4e 5f 53 4f 52 54   PgHdr *a[N_SORT
e9520 5f 42 55 43 4b 45 54 5f 41 4c 4c 4f 43 5d 2c 20  _BUCKET_ALLOC], 
e9530 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 6d  *p;.  int i;.  m
e9540 65 6d 73 65 74 28 61 2c 20 30 2c 20 73 69 7a 65  emset(a, 0, size
e9550 6f 66 28 61 29 29 3b 0a 20 20 77 68 69 6c 65 28  of(a));.  while(
e9560 20 70 49 6e 20 29 7b 0a 20 20 20 20 70 20 3d 20   pIn ){.    p = 
e9570 70 49 6e 3b 0a 20 20 20 20 70 49 6e 20 3d 20 70  pIn;.    pIn = p
e9580 2d 3e 70 44 69 72 74 79 3b 0a 20 20 20 20 70 2d  ->pDirty;.    p-
e9590 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 20  >pDirty = 0;.   
e95a0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 53 4f   for(i=0; i<N_SO
e95b0 52 54 5f 42 55 43 4b 45 54 2d 31 3b 20 69 2b 2b  RT_BUCKET-1; i++
e95c0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 5b 69  ){.      if( a[i
e95d0 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  ]==0 ){.        
e95e0 61 5b 69 5d 20 3d 20 70 3b 0a 20 20 20 20 20 20  a[i] = p;.      
e95f0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
e9600 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 20  else{.        p 
e9610 3d 20 6d 65 72 67 65 5f 70 61 67 65 6c 69 73 74  = merge_pagelist
e9620 28 61 5b 69 5d 2c 20 70 29 3b 0a 20 20 20 20 20  (a[i], p);.     
e9630 20 20 20 61 5b 69 5d 20 3d 20 30 3b 0a 20 20 20     a[i] = 0;.   
e9640 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
e9650 66 28 20 69 3d 3d 4e 5f 53 4f 52 54 5f 42 55 43  f( i==N_SORT_BUC
e9660 4b 45 54 2d 31 20 29 7b 0a 20 20 20 20 20 20 2f  KET-1 ){.      /
e9670 2a 20 43 6f 76 65 72 61 67 65 3a 20 54 6f 20 67  * Coverage: To g
e9680 65 74 20 68 65 72 65 2c 20 74 68 65 72 65 20 6e  et here, there n
e9690 65 65 64 20 74 6f 20 62 65 20 32 5e 28 4e 5f 53  eed to be 2^(N_S
e96a0 4f 52 54 5f 42 55 43 4b 45 54 29 20 0a 20 20 20  ORT_BUCKET) .   
e96b0 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 73 20 69     ** elements i
e96c0 6e 20 74 68 65 20 69 6e 70 75 74 20 6c 69 73 74  n the input list
e96d0 2e 20 54 68 69 73 20 69 73 20 70 6f 73 73 69 62  . This is possib
e96e0 6c 65 2c 20 62 75 74 20 69 6d 70 72 61 63 74 69  le, but impracti
e96f0 63 61 6c 2e 0a 20 20 20 20 20 20 2a 2a 20 54 65  cal..      ** Te
e9700 73 74 69 6e 67 20 74 68 69 73 20 6c 69 6e 65 20  sting this line 
e9710 69 73 20 74 68 65 20 70 6f 69 6e 74 20 6f 66 20  is the point of 
e9720 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 0a  global variable.
e9730 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33        ** sqlite3
e9740 5f 70 61 67 65 72 5f 6e 5f 73 6f 72 74 5f 62 75  _pager_n_sort_bu
e9750 63 6b 65 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  cket..      */. 
e9760 20 20 20 20 20 61 5b 69 5d 20 3d 20 6d 65 72 67       a[i] = merg
e9770 65 5f 70 61 67 65 6c 69 73 74 28 61 5b 69 5d 2c  e_pagelist(a[i],
e9780 20 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20   p);.    }.  }. 
e9790 20 70 20 3d 20 61 5b 30 5d 3b 0a 20 20 66 6f 72   p = a[0];.  for
e97a0 28 69 3d 31 3b 20 69 3c 4e 5f 53 4f 52 54 5f 42  (i=1; i<N_SORT_B
e97b0 55 43 4b 45 54 3b 20 69 2b 2b 29 7b 0a 20 20 20  UCKET; i++){.   
e97c0 20 70 20 3d 20 6d 65 72 67 65 5f 70 61 67 65 6c   p = merge_pagel
e97d0 69 73 74 28 70 2c 20 61 5b 69 5d 29 3b 0a 20 20  ist(p, a[i]);.  
e97e0 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  }.  return p;.}.
e97f0 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 6c  ./*.** Given a l
e9800 69 73 74 20 6f 66 20 70 61 67 65 73 20 28 63 6f  ist of pages (co
e9810 6e 6e 65 63 74 65 64 20 62 79 20 74 68 65 20 50  nnected by the P
e9820 67 48 64 72 2e 70 44 69 72 74 79 20 70 6f 69 6e  gHdr.pDirty poin
e9830 74 65 72 29 20 77 72 69 74 65 0a 2a 2a 20 65 76  ter) write.** ev
e9840 65 72 79 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65  ery one of those
e9850 20 70 61 67 65 73 20 6f 75 74 20 74 6f 20 74 68   pages out to th
e9860 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
e9870 61 6e 64 20 6d 61 72 6b 20 74 68 65 6d 20 61 6c  and mark them al
e9880 6c 0a 2a 2a 20 61 73 20 63 6c 65 61 6e 2e 0a 2a  l.** as clean..*
e9890 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
e98a0 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73  er_write_pagelis
e98b0 74 28 50 67 48 64 72 20 2a 70 4c 69 73 74 29 7b  t(PgHdr *pList){
e98c0 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
e98d0 3b 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20  ;.  PgHdr *p;.  
e98e0 69 6e 74 20 72 63 3b 0a 0a 20 20 69 66 28 20 70  int rc;..  if( p
e98f0 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e  List==0 ) return
e9900 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 50   SQLITE_OK;.  pP
e9910 61 67 65 72 20 3d 20 70 4c 69 73 74 2d 3e 70 50  ager = pList->pP
e9920 61 67 65 72 3b 0a 0a 20 20 2f 2a 20 41 74 20 74  ager;..  /* At t
e9930 68 69 73 20 70 6f 69 6e 74 20 74 68 65 72 65 20  his point there 
e9940 6d 61 79 20 62 65 20 65 69 74 68 65 72 20 61 20  may be either a 
e9950 52 45 53 45 52 56 45 44 20 6f 72 20 45 58 43 4c  RESERVED or EXCL
e9960 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68  USIVE lock on th
e9970 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20  e.  ** database 
e9980 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69  file. If there i
e9990 73 20 61 6c 72 65 61 64 79 20 61 6e 20 45 58 43  s already an EXC
e99a0 4c 55 53 49 56 45 20 6c 6f 63 6b 2c 20 74 68 65  LUSIVE lock, the
e99b0 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20   following.  ** 
e99c0 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33  calls to sqlite3
e99d0 4f 73 4c 6f 63 6b 28 29 20 61 72 65 20 6e 6f 2d  OsLock() are no-
e99e0 6f 70 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d  ops..  **.  ** M
e99f0 6f 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 66  oving the lock f
e9a00 72 6f 6d 20 52 45 53 45 52 56 45 44 20 74 6f 20  rom RESERVED to 
e9a10 45 58 43 4c 55 53 49 56 45 20 61 63 74 75 61 6c  EXCLUSIVE actual
e9a20 6c 79 20 69 6e 76 6f 6c 76 65 73 20 67 6f 69 6e  ly involves goin
e9a30 67 0a 20 20 2a 2a 20 74 68 72 6f 75 67 68 20 61  g.  ** through a
e9a40 6e 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73  n intermediate s
e9a50 74 61 74 65 20 50 45 4e 44 49 4e 47 2e 20 20 20  tate PENDING.   
e9a60 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 70  A PENDING lock p
e9a70 72 65 76 65 6e 74 73 20 6e 65 77 0a 20 20 2a 2a  revents new.  **
e9a80 20 72 65 61 64 65 72 73 20 66 72 6f 6d 20 61 74   readers from at
e9a90 74 61 63 68 69 6e 67 20 74 6f 20 74 68 65 20 64  taching to the d
e9aa0 61 74 61 62 61 73 65 20 62 75 74 20 69 73 20 75  atabase but is u
e9ab0 6e 73 75 66 66 69 63 69 65 6e 74 20 66 6f 72 20  nsufficient for 
e9ac0 75 73 20 74 6f 0a 20 20 2a 2a 20 77 72 69 74 65  us to.  ** write
e9ad0 2e 20 20 54 68 65 20 69 64 65 61 20 6f 66 20 61  .  The idea of a
e9ae0 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73   PENDING lock is
e9af0 20 74 6f 20 70 72 65 76 65 6e 74 20 6e 65 77 20   to prevent new 
e9b00 72 65 61 64 65 72 73 20 66 72 6f 6d 0a 20 20 2a  readers from.  *
e9b10 2a 20 63 6f 6d 69 6e 67 20 69 6e 20 77 68 69 6c  * coming in whil
e9b20 65 20 77 65 20 77 61 69 74 20 66 6f 72 20 65 78  e we wait for ex
e9b30 69 73 74 69 6e 67 20 72 65 61 64 65 72 73 20 74  isting readers t
e9b40 6f 20 63 6c 65 61 72 2e 0a 20 20 2a 2a 0a 20 20  o clear..  **.  
e9b50 2a 2a 20 57 68 69 6c 65 20 74 68 65 20 70 61 67  ** While the pag
e9b60 65 72 20 69 73 20 69 6e 20 74 68 65 20 52 45 53  er is in the RES
e9b70 45 52 56 45 44 20 73 74 61 74 65 2c 20 74 68 65  ERVED state, the
e9b80 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61   original databa
e9b90 73 65 20 66 69 6c 65 0a 20 20 2a 2a 20 69 73 20  se file.  ** is 
e9ba0 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 77 65  unchanged and we
e9bb0 20 63 61 6e 20 72 6f 6c 6c 62 61 63 6b 20 77 69   can rollback wi
e9bc0 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20  thout having to 
e9bd0 70 6c 61 79 62 61 63 6b 20 74 68 65 0a 20 20 2a  playback the.  *
e9be0 2a 20 6a 6f 75 72 6e 61 6c 20 69 6e 74 6f 20 74  * journal into t
e9bf0 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61  he original data
e9c00 62 61 73 65 20 66 69 6c 65 2e 20 20 4f 6e 63 65  base file.  Once
e9c10 20 77 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 74   we transition t
e9c20 6f 0a 20 20 2a 2a 20 45 58 43 4c 55 53 49 56 45  o.  ** EXCLUSIVE
e9c30 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 65 20 64  , it means the d
e9c40 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 73  atabase file has
e9c50 20 62 65 65 6e 20 63 68 61 6e 67 65 64 20 61 6e   been changed an
e9c60 64 20 61 6e 79 20 72 6f 6c 6c 62 61 63 6b 0a 20  d any rollback. 
e9c70 20 2a 2a 20 77 69 6c 6c 20 72 65 71 75 69 72 65   ** will require
e9c80 20 61 20 6a 6f 75 72 6e 61 6c 20 70 6c 61 79 62   a journal playb
e9c90 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d  ack..  */.  rc =
e9ca0 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c   pager_wait_on_l
e9cb0 6f 63 6b 28 70 50 61 67 65 72 2c 20 45 58 43 4c  ock(pPager, EXCL
e9cc0 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 69  USIVE_LOCK);.  i
e9cd0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
e9ce0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
e9cf0 63 3b 0a 20 20 7d 0a 0a 20 20 70 4c 69 73 74 20  c;.  }..  pList 
e9d00 3d 20 73 6f 72 74 5f 70 61 67 65 6c 69 73 74 28  = sort_pagelist(
e9d10 70 4c 69 73 74 29 3b 0a 20 20 66 6f 72 28 70 3d  pList);.  for(p=
e9d20 70 4c 69 73 74 3b 20 70 3b 20 70 3d 70 2d 3e 70  pList; p; p=p->p
e9d30 44 69 72 74 79 29 7b 0a 20 20 20 20 61 73 73 65  Dirty){.    asse
e9d40 72 74 28 20 70 2d 3e 64 69 72 74 79 20 29 3b 0a  rt( p->dirty );.
e9d50 20 20 20 20 70 2d 3e 64 69 72 74 79 20 3d 20 30      p->dirty = 0
e9d60 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 70  ;.  }.  while( p
e9d70 4c 69 73 74 20 29 7b 0a 0a 20 20 20 20 2f 2a 20  List ){..    /* 
e9d80 49 66 20 74 68 65 20 66 69 6c 65 20 68 61 73 20  If the file has 
e9d90 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70 65  not yet been ope
e9da0 6e 65 64 2c 20 6f 70 65 6e 20 69 74 20 6e 6f 77  ned, open it now
e9db0 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 50  . */.    if( !pP
e9dc0 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f  ager->fd->pMetho
e9dd0 64 73 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  ds ){.      asse
e9de0 72 74 28 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  rt(pPager->tempF
e9df0 69 6c 65 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  ile);.      rc =
e9e00 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65   sqlite3PagerOpe
e9e10 6e 74 65 6d 70 28 70 50 61 67 65 72 2d 3e 70 56  ntemp(pPager->pV
e9e20 66 73 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c 20  fs, pPager->fd, 
e9e30 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d  pPager->zFilenam
e9e40 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e,.             
e9e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e9e60 20 20 20 70 50 61 67 65 72 2d 3e 76 66 73 46 6c     pPager->vfsFl
e9e70 61 67 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ags);.      if( 
e9e80 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
e9e90 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66      }..    /* If
e9ea0 20 74 68 65 72 65 20 61 72 65 20 64 69 72 74 79   there are dirty
e9eb0 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 70 61   pages in the pa
e9ec0 67 65 20 63 61 63 68 65 20 77 69 74 68 20 70 61  ge cache with pa
e9ed0 67 65 20 6e 75 6d 62 65 72 73 20 67 72 65 61 74  ge numbers great
e9ee0 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 6e 20 50  er.    ** than P
e9ef0 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 74 68 69  ager.dbSize, thi
e9f00 73 20 6d 65 61 6e 73 20 73 71 6c 69 74 65 33 50  s means sqlite3P
e9f10 61 67 65 72 54 72 75 6e 63 61 74 65 28 29 20 77  agerTruncate() w
e9f20 61 73 20 63 61 6c 6c 65 64 20 74 6f 0a 20 20 20  as called to.   
e9f30 20 2a 2a 20 6d 61 6b 65 20 74 68 65 20 66 69 6c   ** make the fil
e9f40 65 20 73 6d 61 6c 6c 65 72 20 28 70 72 65 73 75  e smaller (presu
e9f50 6d 61 62 6c 79 20 62 79 20 61 75 74 6f 2d 76 61  mably by auto-va
e9f60 63 75 75 6d 20 63 6f 64 65 29 2e 20 44 6f 20 6e  cuum code). Do n
e9f70 6f 74 20 77 72 69 74 65 0a 20 20 20 20 2a 2a 20  ot write.    ** 
e9f80 61 6e 79 20 73 75 63 68 20 70 61 67 65 73 20 74  any such pages t
e9f90 6f 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 20 20  o the file..    
e9fa0 2a 2f 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74  */.    if( pList
e9fb0 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e  ->pgno<=pPager->
e9fc0 64 62 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  dbSize ){.      
e9fd0 69 36 34 20 6f 66 66 73 65 74 20 3d 20 28 70 4c  i64 offset = (pL
e9fe0 69 73 74 2d 3e 70 67 6e 6f 2d 31 29 2a 28 69 36  ist->pgno-1)*(i6
e9ff0 34 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  4)pPager->pageSi
ea000 7a 65 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a  ze;.      char *
ea010 70 44 61 74 61 20 3d 20 43 4f 44 45 43 32 28 70  pData = CODEC2(p
ea020 50 61 67 65 72 2c 20 50 47 48 44 52 5f 54 4f 5f  Pager, PGHDR_TO_
ea030 44 41 54 41 28 70 4c 69 73 74 29 2c 20 70 4c 69  DATA(pList), pLi
ea040 73 74 2d 3e 70 67 6e 6f 2c 20 36 29 3b 0a 20 20  st->pgno, 6);.  
ea050 20 20 20 20 50 41 47 45 52 54 52 41 43 45 34 28      PAGERTRACE4(
ea060 22 53 54 4f 52 45 20 25 64 20 70 61 67 65 20 25  "STORE %d page %
ea070 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22 2c  d hash(%08x)\n",
ea080 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
ea090 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67      PAGERID(pPag
ea0a0 65 72 29 2c 20 70 4c 69 73 74 2d 3e 70 67 6e 6f  er), pList->pgno
ea0b0 2c 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68  , pager_pagehash
ea0c0 28 70 4c 69 73 74 29 29 3b 0a 20 20 20 20 20 20  (pList));.      
ea0d0 49 4f 54 52 41 43 45 28 28 22 50 47 4f 55 54 20  IOTRACE(("PGOUT 
ea0e0 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72  %p %d\n", pPager
ea0f0 2c 20 70 4c 69 73 74 2d 3e 70 67 6e 6f 29 29 3b  , pList->pgno));
ea100 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
ea110 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65  te3OsWrite(pPage
ea120 72 2d 3e 66 64 2c 20 70 44 61 74 61 2c 20 70 50  r->fd, pData, pP
ea130 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20  ager->pageSize, 
ea140 6f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20 50  offset);.      P
ea150 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65  AGER_INCR(sqlite
ea160 33 5f 70 61 67 65 72 5f 77 72 69 74 65 64 62 5f  3_pager_writedb_
ea170 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 50 41  count);.      PA
ea180 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d  GER_INCR(pPager-
ea190 3e 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 20 20  >nWrite);.      
ea1a0 69 66 28 20 70 4c 69 73 74 2d 3e 70 67 6e 6f 3d  if( pList->pgno=
ea1b0 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65  =1 ){.        me
ea1c0 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 64 62  mcpy(&pPager->db
ea1d0 46 69 6c 65 56 65 72 73 2c 20 26 70 44 61 74 61  FileVers, &pData
ea1e0 5b 32 34 5d 2c 20 73 69 7a 65 6f 66 28 70 50 61  [24], sizeof(pPa
ea1f0 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29  ger->dbFileVers)
ea200 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
ea210 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
ea220 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20      else{.      
ea230 50 41 47 45 52 54 52 41 43 45 33 28 22 4e 4f 53  PAGERTRACE3("NOS
ea240 54 4f 52 45 20 25 64 20 70 61 67 65 20 25 64 5c  TORE %d page %d\
ea250 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67  n", PAGERID(pPag
ea260 65 72 29 2c 20 70 4c 69 73 74 2d 3e 70 67 6e 6f  er), pList->pgno
ea270 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
ea280 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
ea290 75 72 6e 20 72 63 3b 0a 23 69 66 64 65 66 20 53  urn rc;.#ifdef S
ea2a0 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45  QLITE_CHECK_PAGE
ea2b0 53 0a 20 20 20 20 70 4c 69 73 74 2d 3e 70 61 67  S.    pList->pag
ea2c0 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70 61  eHash = pager_pa
ea2d0 67 65 68 61 73 68 28 70 4c 69 73 74 29 3b 0a 23  gehash(pList);.#
ea2e0 65 6e 64 69 66 0a 20 20 20 20 70 4c 69 73 74 20  endif.    pList 
ea2f0 3d 20 70 4c 69 73 74 2d 3e 70 44 69 72 74 79 3b  = pList->pDirty;
ea300 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
ea310 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
ea320 2a 20 43 6f 6c 6c 65 63 74 20 65 76 65 72 79 20  * Collect every 
ea330 64 69 72 74 79 20 70 61 67 65 20 69 6e 74 6f 20  dirty page into 
ea340 61 20 64 69 72 74 79 20 6c 69 73 74 20 61 6e 64  a dirty list and
ea350 0a 2a 2a 20 72 65 74 75 72 6e 20 61 20 70 6f 69  .** return a poi
ea360 6e 74 65 72 20 74 6f 20 74 68 65 20 68 65 61 64  nter to the head
ea370 20 6f 66 20 74 68 61 74 20 6c 69 73 74 2e 20 20   of that list.  
ea380 41 6c 6c 20 70 61 67 65 73 20 61 72 65 0a 2a 2a  All pages are.**
ea390 20 63 6f 6c 6c 65 63 74 65 64 20 65 76 65 6e 20   collected even 
ea3a0 69 66 20 74 68 65 79 20 61 72 65 20 73 74 69 6c  if they are stil
ea3b0 6c 20 69 6e 20 75 73 65 2e 0a 2a 2f 0a 73 74 61  l in use..*/.sta
ea3c0 74 69 63 20 50 67 48 64 72 20 2a 70 61 67 65 72  tic PgHdr *pager
ea3d0 5f 67 65 74 5f 61 6c 6c 5f 64 69 72 74 79 5f 70  _get_all_dirty_p
ea3e0 61 67 65 73 28 50 61 67 65 72 20 2a 70 50 61 67  ages(Pager *pPag
ea3f0 65 72 29 7b 0a 0a 23 69 66 6e 64 65 66 20 4e 44  er){..#ifndef ND
ea400 45 42 55 47 0a 20 20 2f 2a 20 56 65 72 69 66 79  EBUG.  /* Verify
ea410 20 74 68 65 20 73 61 6e 69 74 79 20 6f 66 20 74   the sanity of t
ea420 68 65 20 64 69 72 74 79 20 6c 69 73 74 20 77 68  he dirty list wh
ea430 65 6e 20 77 65 20 61 72 65 20 72 75 6e 6e 69 6e  en we are runnin
ea440 67 0a 20 20 2a 2a 20 69 6e 20 64 65 62 75 67 67  g.  ** in debugg
ea450 69 6e 67 20 6d 6f 64 65 2e 20 20 54 68 69 73 20  ing mode.  This 
ea460 69 73 20 65 78 70 65 6e 73 69 76 65 2c 20 73 6f  is expensive, so
ea470 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 64 6f 20   do not.  ** do 
ea480 74 68 69 73 20 6f 6e 20 61 20 6e 6f 72 6d 61 6c  this on a normal
ea490 20 62 75 69 6c 64 2e 20 2a 2f 0a 20 20 69 6e 74   build. */.  int
ea4a0 20 6e 31 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e   n1 = 0;.  int n
ea4b0 32 20 3d 20 30 3b 0a 20 20 50 67 48 64 72 20 2a  2 = 0;.  PgHdr *
ea4c0 70 3b 0a 20 20 66 6f 72 28 70 3d 70 50 61 67 65  p;.  for(p=pPage
ea4d0 72 2d 3e 70 41 6c 6c 3b 20 70 3b 20 70 3d 70 2d  r->pAll; p; p=p-
ea4e0 3e 70 4e 65 78 74 41 6c 6c 29 7b 20 69 66 28 20  >pNextAll){ if( 
ea4f0 70 2d 3e 64 69 72 74 79 20 29 20 6e 31 2b 2b 3b  p->dirty ) n1++;
ea500 20 7d 0a 20 20 66 6f 72 28 70 3d 70 50 61 67 65   }.  for(p=pPage
ea510 72 2d 3e 70 44 69 72 74 79 3b 20 70 3b 20 70 3d  r->pDirty; p; p=
ea520 70 2d 3e 70 44 69 72 74 79 29 7b 20 6e 32 2b 2b  p->pDirty){ n2++
ea530 3b 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 31  ; }.  assert( n1
ea540 3d 3d 6e 32 20 29 3b 0a 23 65 6e 64 69 66 0a 0a  ==n2 );.#endif..
ea550 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
ea560 3e 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a  >pDirty;.}../*.*
ea570 2a 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68  * Return 1 if th
ea580 65 72 65 20 69 73 20 61 20 68 6f 74 20 6a 6f 75  ere is a hot jou
ea590 72 6e 61 6c 20 6f 6e 20 74 68 65 20 67 69 76 65  rnal on the give
ea5a0 6e 20 70 61 67 65 72 2e 0a 2a 2a 20 41 20 68 6f  n pager..** A ho
ea5b0 74 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 6e 65  t journal is one
ea5c0 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20 62   that needs to b
ea5d0 65 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a 2a  e played back..*
ea5e0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 75 72 72  *.** If the curr
ea5f0 65 6e 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20  ent size of the 
ea600 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73  database file is
ea610 20 30 20 62 75 74 20 61 20 6a 6f 75 72 6e 61 6c   0 but a journal
ea620 20 66 69 6c 65 0a 2a 2a 20 65 78 69 73 74 73 2c   file.** exists,
ea630 20 74 68 61 74 20 69 73 20 70 72 6f 62 61 62 6c   that is probabl
ea640 79 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c  y an old journal
ea650 20 6c 65 66 74 20 6f 76 65 72 20 66 72 6f 6d 20   left over from 
ea660 61 20 70 72 69 6f 72 0a 2a 2a 20 64 61 74 61 62  a prior.** datab
ea670 61 73 65 20 77 69 74 68 20 74 68 65 20 73 61 6d  ase with the sam
ea680 65 20 6e 61 6d 65 2e 20 20 4a 75 73 74 20 64 65  e name.  Just de
ea690 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  lete the journal
ea6a0 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e  ..**.** Return n
ea6b0 65 67 61 74 69 76 65 20 69 66 20 75 6e 61 62 6c  egative if unabl
ea6c0 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74  e to determine t
ea6d0 68 65 20 73 74 61 74 75 73 20 6f 66 20 74 68 65  he status of the
ea6e0 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20   journal..**.** 
ea6f0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65  This routine doe
ea700 73 20 6e 6f 74 20 6f 70 65 6e 20 74 68 65 20 6a  s not open the j
ea710 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 65  ournal file to e
ea720 78 61 6d 69 6e 65 20 69 74 73 0a 2a 2a 20 63 6f  xamine its.** co
ea730 6e 74 65 6e 74 2e 20 20 48 65 6e 63 65 2c 20 74  ntent.  Hence, t
ea740 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74  he journal might
ea750 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e 61 6d   contain the nam
ea760 65 20 6f 66 20 61 20 6d 61 73 74 65 72 0a 2a 2a  e of a master.**
ea770 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68   journal file th
ea780 61 74 20 68 61 73 20 62 65 65 6e 20 64 65 6c 65  at has been dele
ea790 74 65 64 2c 20 61 6e 64 20 68 65 6e 63 65 20 6e  ted, and hence n
ea7a0 6f 74 20 62 65 20 68 6f 74 2e 20 20 4f 72 0a 2a  ot be hot.  Or.*
ea7b0 2a 20 74 68 65 20 68 65 61 64 65 72 20 6f 66 20  * the header of 
ea7c0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68  the journal migh
ea7d0 74 20 62 65 20 7a 65 72 6f 65 64 20 6f 75 74 2e  t be zeroed out.
ea7e0 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a    This routine.*
ea7f0 2a 20 64 6f 65 73 20 6e 6f 74 20 64 69 73 63 6f  * does not disco
ea800 76 65 72 20 74 68 65 73 65 20 63 61 73 65 73 20  ver these cases 
ea810 6f 66 20 61 20 6e 6f 6e 2d 68 6f 74 20 6a 6f 75  of a non-hot jou
ea820 72 6e 61 6c 20 2d 20 69 66 20 74 68 65 0a 2a 2a  rnal - if the.**
ea830 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78   journal file ex
ea840 69 73 74 73 20 61 6e 64 20 69 73 20 6e 6f 74 20  ists and is not 
ea850 65 6d 70 74 79 20 74 68 69 73 20 72 6f 75 74 69  empty this routi
ea860 6e 65 20 61 73 73 75 6d 65 73 20 69 74 0a 2a 2a  ne assumes it.**
ea870 20 69 73 20 68 6f 74 2e 20 20 54 68 65 20 70 61   is hot.  The pa
ea880 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 20 72  ger_playback() r
ea890 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64 69 73 63  outine will disc
ea8a0 6f 76 65 72 20 74 68 61 74 20 74 68 65 0a 2a 2a  over that the.**
ea8b0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
ea8c0 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74 20   not really hot 
ea8d0 61 6e 64 20 77 69 6c 6c 20 6e 6f 2d 6f 70 2e 0a  and will no-op..
ea8e0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68 61  */.static int ha
ea8f0 73 48 6f 74 4a 6f 75 72 6e 61 6c 28 50 61 67 65  sHotJournal(Page
ea900 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 73 71  r *pPager){.  sq
ea910 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20  lite3_vfs *pVfs 
ea920 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a  = pPager->pVfs;.
ea930 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20    int rc;.  if( 
ea940 21 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72  !pPager->useJour
ea950 6e 61 6c 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  nal ) return 0;.
ea960 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 66    if( !pPager->f
ea970 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 20 72 65  d->pMethods ) re
ea980 74 75 72 6e 20 30 3b 0a 20 20 72 63 20 3d 20 73  turn 0;.  rc = s
ea990 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 70  qlite3OsAccess(p
ea9a0 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f  Vfs, pPager->zJo
ea9b0 75 72 6e 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43  urnal, SQLITE_AC
ea9c0 43 45 53 53 5f 45 58 49 53 54 53 29 3b 0a 20 20  CESS_EXISTS);.  
ea9d0 69 66 28 20 72 63 3c 3d 30 20 29 7b 0a 20 20 20  if( rc<=0 ){.   
ea9e0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
ea9f0 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 73 43    if( sqlite3OsC
eaa00 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
eaa10 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a  (pPager->fd) ){.
eaa20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
eaa30 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 50  }.  if( sqlite3P
eaa40 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50  agerPagecount(pP
eaa50 61 67 65 72 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ager)==0 ){.    
eaa60 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28  sqlite3OsDelete(
eaa70 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a  pVfs, pPager->zJ
eaa80 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20  ournal, 0);.    
eaa90 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73  return 0;.  }els
eaaa0 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  e{.    return 1;
eaab0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72  .  }.}../*.** Tr
eaac0 79 20 74 6f 20 66 69 6e 64 20 61 20 70 61 67 65  y to find a page
eaad0 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 74 68   in the cache th
eaae0 61 74 20 63 61 6e 20 62 65 20 72 65 63 79 63 6c  at can be recycl
eaaf0 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ed. .**.** This 
eab00 72 6f 75 74 69 6e 65 20 6d 61 79 20 72 65 74 75  routine may retu
eab10 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c  rn SQLITE_IOERR,
eab20 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 6f 72 20   SQLITE_FULL or 
eab30 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 74 20 0a 2a  SQLITE_OK. It .*
eab40 2a 20 64 6f 65 73 20 6e 6f 74 20 73 65 74 20 74  * does not set t
eab50 68 65 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  he pPager->errCo
eab60 64 65 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2f 0a  de variable..*/.
eab70 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72  static int pager
eab80 5f 72 65 63 79 63 6c 65 28 50 61 67 65 72 20 2a  _recycle(Pager *
eab90 70 50 61 67 65 72 2c 20 50 67 48 64 72 20 2a 2a  pPager, PgHdr **
eaba0 70 70 50 67 29 7b 0a 20 20 50 67 48 64 72 20 2a  ppPg){.  PgHdr *
eabb0 70 50 67 3b 0a 20 20 2a 70 70 50 67 20 3d 20 30  pPg;.  *ppPg = 0
eabc0 3b 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20 69 6c  ;..  /* It is il
eabd0 6c 65 67 61 6c 20 74 6f 20 63 61 6c 6c 20 74 68  legal to call th
eabe0 69 73 20 66 75 6e 63 74 69 6f 6e 20 75 6e 6c 65  is function unle
eabf0 73 73 20 74 68 65 20 70 61 67 65 72 20 6f 62 6a  ss the pager obj
eac00 65 63 74 0a 20 20 2a 2a 20 70 6f 69 6e 74 65 64  ect.  ** pointed
eac10 20 74 6f 20 62 79 20 70 50 61 67 65 72 20 68 61   to by pPager ha
eac20 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 66  s at least one f
eac30 72 65 65 20 70 61 67 65 20 28 70 61 67 65 20 77  ree page (page w
eac40 69 74 68 20 6e 52 65 66 3d 3d 30 29 2e 0a 20 20  ith nRef==0)..  
eac50 2a 2f 20 0a 20 20 61 73 73 65 72 74 28 21 4d 45  */ .  assert(!ME
eac60 4d 44 42 29 3b 0a 20 20 61 73 73 65 72 74 28 70  MDB);.  assert(p
eac70 50 61 67 65 72 2d 3e 6c 72 75 2e 70 46 69 72 73  Pager->lru.pFirs
eac80 74 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 61  t);..  /* Find a
eac90 20 70 61 67 65 20 74 6f 20 72 65 63 79 63 6c 65   page to recycle
eaca0 2e 20 20 54 72 79 20 74 6f 20 6c 6f 63 61 74 65  .  Try to locate
eacb0 20 61 20 70 61 67 65 20 74 68 61 74 20 64 6f 65   a page that doe
eacc0 73 20 6e 6f 74 0a 20 20 2a 2a 20 72 65 71 75 69  s not.  ** requi
eacd0 72 65 20 75 73 20 74 6f 20 64 6f 20 61 6e 20 66  re us to do an f
eace0 73 79 6e 63 28 29 20 6f 6e 20 74 68 65 20 6a 6f  sync() on the jo
eacf0 75 72 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 70 50  urnal..  */.  pP
ead00 67 20 3d 20 70 50 61 67 65 72 2d 3e 6c 72 75 2e  g = pPager->lru.
ead10 70 46 69 72 73 74 53 79 6e 63 65 64 3b 0a 0a 20  pFirstSynced;.. 
ead20 20 2f 2a 20 49 66 20 77 65 20 63 6f 75 6c 64 20   /* If we could 
ead30 6e 6f 74 20 66 69 6e 64 20 61 20 70 61 67 65 20  not find a page 
ead40 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 72 65  that does not re
ead50 71 75 69 72 65 20 61 6e 20 66 73 79 6e 63 28 29  quire an fsync()
ead60 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6a 6f 75  .  ** on the jou
ead70 72 6e 61 6c 20 66 69 6c 65 20 74 68 65 6e 20 66  rnal file then f
ead80 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c  sync the journal
ead90 20 66 69 6c 65 2e 20 20 54 68 69 73 20 69 73 20   file.  This is 
eada0 61 0a 20 20 2a 2a 20 76 65 72 79 20 73 6c 6f 77  a.  ** very slow
eadb0 20 6f 70 65 72 61 74 69 6f 6e 2c 20 73 6f 20 77   operation, so w
eadc0 65 20 77 6f 72 6b 20 68 61 72 64 20 74 6f 20 61  e work hard to a
eadd0 76 6f 69 64 20 69 74 2e 20 20 42 75 74 20 73 6f  void it.  But so
eade0 6d 65 74 69 6d 65 73 0a 20 20 2a 2a 20 69 74 20  metimes.  ** it 
eadf0 63 61 6e 27 74 20 62 65 20 68 65 6c 70 65 64 2e  can't be helped.
eae00 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 67 3d  .  */.  if( pPg=
eae10 3d 30 20 26 26 20 70 50 61 67 65 72 2d 3e 6c 72  =0 && pPager->lr
eae20 75 2e 70 46 69 72 73 74 29 7b 0a 20 20 20 20 69  u.pFirst){.    i
eae30 6e 74 20 69 44 63 20 3d 20 73 71 6c 69 74 65 33  nt iDc = sqlite3
eae40 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65  OsDeviceCharacte
eae50 72 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e  ristics(pPager->
eae60 66 64 29 3b 0a 20 20 20 20 69 6e 74 20 72 63 20  fd);.    int rc 
eae70 3d 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 50  = syncJournal(pP
eae80 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72  ager);.    if( r
eae90 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65  c!=0 ){.      re
eaea0 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
eaeb0 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 66     if( pPager->f
eaec0 75 6c 6c 53 79 6e 63 20 26 26 20 30 3d 3d 28 69  ullSync && 0==(i
eaed0 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  Dc&SQLITE_IOCAP_
eaee0 53 41 46 45 5f 41 50 50 45 4e 44 29 20 29 7b 0a  SAFE_APPEND) ){.
eaef0 20 20 20 20 20 20 2f 2a 20 49 66 20 69 6e 20 66        /* If in f
eaf00 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 77  ull-sync mode, w
eaf10 72 69 74 65 20 61 20 6e 65 77 20 6a 6f 75 72 6e  rite a new journ
eaf20 61 6c 20 68 65 61 64 65 72 20 69 6e 74 6f 20 74  al header into t
eaf30 68 65 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72  he.      ** jour
eaf40 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 69  nal file. This i
eaf50 73 20 64 6f 6e 65 20 74 6f 20 61 76 6f 69 64 20  s done to avoid 
eaf60 65 76 65 72 20 6d 6f 64 69 66 79 69 6e 67 20 61  ever modifying a
eaf70 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20 2a   journal.      *
eaf80 2a 20 68 65 61 64 65 72 20 74 68 61 74 20 69 73  * header that is
eaf90 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 74 68 65   involved in the
eafa0 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 70 61 67   rollback of pag
eafb0 65 73 20 74 68 61 74 20 68 61 76 65 0a 20 20 20  es that have.   
eafc0 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 62 65     ** already be
eafd0 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  en written to th
eafe0 65 20 64 61 74 61 62 61 73 65 20 28 69 6e 20 63  e database (in c
eaff0 61 73 65 20 74 68 65 20 68 65 61 64 65 72 20 69  ase the header i
eb000 73 0a 20 20 20 20 20 20 2a 2a 20 74 72 61 73 68  s.      ** trash
eb010 65 64 20 77 68 65 6e 20 74 68 65 20 6e 52 65 63  ed when the nRec
eb020 20 66 69 65 6c 64 20 69 73 20 75 70 64 61 74 65   field is update
eb030 64 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  d)..      */.   
eb040 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20     pPager->nRec 
eb050 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72  = 0;.      asser
eb060 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  t( pPager->journ
eb070 61 6c 4f 66 66 20 3e 20 30 20 29 3b 0a 20 20 20  alOff > 0 );.   
eb080 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
eb090 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 3d 3d 30 20  r->doNotSync==0 
eb0a0 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 72  );.      rc = wr
eb0b0 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50  iteJournalHdr(pP
eb0c0 61 67 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28  ager);.      if(
eb0d0 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20   rc!=0 ){.      
eb0e0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
eb0f0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70     }.    }.    p
eb100 50 67 20 3d 20 70 50 61 67 65 72 2d 3e 6c 72 75  Pg = pPager->lru
eb110 2e 70 46 69 72 73 74 3b 0a 20 20 7d 0a 0a 20 20  .pFirst;.  }..  
eb120 61 73 73 65 72 74 28 20 70 50 67 2d 3e 6e 52 65  assert( pPg->nRe
eb130 66 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 57 72  f==0 );..  /* Wr
eb140 69 74 65 20 74 68 65 20 70 61 67 65 20 74 6f 20  ite the page to 
eb150 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
eb160 65 20 69 66 20 69 74 20 69 73 20 64 69 72 74 79  e if it is dirty
eb170 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 67  ..  */.  if( pPg
eb180 2d 3e 64 69 72 74 79 20 29 7b 0a 20 20 20 20 69  ->dirty ){.    i
eb190 6e 74 20 72 63 3b 0a 20 20 20 20 61 73 73 65 72  nt rc;.    asser
eb1a0 74 28 20 70 50 67 2d 3e 6e 65 65 64 53 79 6e 63  t( pPg->needSync
eb1b0 3d 3d 30 20 29 3b 0a 20 20 20 20 6d 61 6b 65 43  ==0 );.    makeC
eb1c0 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 20 20 70  lean(pPg);.    p
eb1d0 50 67 2d 3e 64 69 72 74 79 20 3d 20 31 3b 0a 20  Pg->dirty = 1;. 
eb1e0 20 20 20 70 50 67 2d 3e 70 44 69 72 74 79 20 3d     pPg->pDirty =
eb1f0 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67   0;.    rc = pag
eb200 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73  er_write_pagelis
eb210 74 28 20 70 50 67 20 29 3b 0a 20 20 20 20 70 50  t( pPg );.    pP
eb220 67 2d 3e 64 69 72 74 79 20 3d 20 30 3b 0a 20 20  g->dirty = 0;.  
eb230 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
eb240 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74  _OK ){.      ret
eb250 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
eb260 7d 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d  }.  assert( pPg-
eb270 3e 64 69 72 74 79 3d 3d 30 20 29 3b 0a 0a 20 20  >dirty==0 );..  
eb280 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 77  /* If the page w
eb290 65 20 61 72 65 20 72 65 63 79 63 6c 69 6e 67 20  e are recycling 
eb2a0 69 73 20 6d 61 72 6b 65 64 20 61 73 20 61 6c 77  is marked as alw
eb2b0 61 79 73 52 6f 6c 6c 62 61 63 6b 2c 20 74 68 65  aysRollback, the
eb2c0 6e 0a 20 20 2a 2a 20 73 65 74 20 74 68 65 20 67  n.  ** set the g
eb2d0 6c 6f 62 61 6c 20 61 6c 77 61 79 73 52 6f 6c 6c  lobal alwaysRoll
eb2e0 62 61 63 6b 20 66 6c 61 67 2c 20 74 68 75 73 20  back flag, thus 
eb2f0 64 69 73 61 62 6c 69 6e 67 20 74 68 65 0a 20 20  disabling the.  
eb300 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 44  ** sqlite3PagerD
eb310 6f 6e 74 52 6f 6c 6c 62 61 63 6b 28 29 20 6f 70  ontRollback() op
eb320 74 69 6d 69 7a 61 74 69 6f 6e 20 66 6f 72 20 74  timization for t
eb330 68 65 20 72 65 73 74 20 6f 66 20 74 68 69 73 20  he rest of this 
eb340 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a  transaction..  *
eb350 2a 20 49 74 20 69 73 20 6e 65 63 65 73 73 61 72  * It is necessar
eb360 79 20 74 6f 20 64 6f 20 74 68 69 73 20 62 65 63  y to do this bec
eb370 61 75 73 65 20 74 68 65 20 70 61 67 65 20 6d 61  ause the page ma
eb380 72 6b 65 64 20 61 6c 77 61 79 73 52 6f 6c 6c 62  rked alwaysRollb
eb390 61 63 6b 0a 20 20 2a 2a 20 6d 69 67 68 74 20 62  ack.  ** might b
eb3a0 65 20 72 65 6c 6f 61 64 65 64 20 61 74 20 61 20  e reloaded at a 
eb3b0 6c 61 74 65 72 20 74 69 6d 65 20 62 75 74 20 61  later time but a
eb3c0 74 20 74 68 61 74 20 70 6f 69 6e 74 20 77 65 20  t that point we 
eb3d0 77 6f 6e 27 74 20 72 65 6d 65 6d 62 65 72 0a 20  won't remember. 
eb3e0 20 2a 2a 20 74 68 61 74 20 69 73 20 77 61 73 20   ** that is was 
eb3f0 6d 61 72 6b 65 64 20 61 6c 77 61 79 73 52 6f 6c  marked alwaysRol
eb400 6c 62 61 63 6b 2e 20 20 54 68 69 73 20 6d 65 61  lback.  This mea
eb410 6e 73 20 74 68 61 74 20 61 6c 6c 20 70 61 67 65  ns that all page
eb420 73 20 6d 75 73 74 0a 20 20 2a 2a 20 62 65 20 6d  s must.  ** be m
eb430 61 72 6b 65 64 20 61 73 20 61 6c 77 61 79 73 52  arked as alwaysR
eb440 6f 6c 6c 62 61 63 6b 20 66 72 6f 6d 20 68 65 72  ollback from her
eb450 65 20 6f 6e 20 6f 75 74 2e 0a 20 20 2a 2f 0a 20  e on out..  */. 
eb460 20 69 66 28 20 70 50 67 2d 3e 61 6c 77 61 79 73   if( pPg->always
eb470 52 6f 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20  Rollback ){.    
eb480 49 4f 54 52 41 43 45 28 28 22 41 4c 57 41 59 53  IOTRACE(("ALWAYS
eb490 5f 52 4f 4c 4c 42 41 43 4b 20 25 70 5c 6e 22 2c  _ROLLBACK %p\n",
eb4a0 20 70 50 61 67 65 72 29 29 0a 20 20 20 20 70 50   pPager)).    pP
eb4b0 61 67 65 72 2d 3e 61 6c 77 61 79 73 52 6f 6c 6c  ager->alwaysRoll
eb4c0 62 61 63 6b 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20  back = 1;.  }.. 
eb4d0 20 2f 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 6f   /* Unlink the o
eb4e0 6c 64 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65  ld page from the
eb4f0 20 66 72 65 65 20 6c 69 73 74 20 61 6e 64 20 74   free list and t
eb500 68 65 20 68 61 73 68 20 74 61 62 6c 65 0a 20 20  he hash table.  
eb510 2a 2f 0a 20 20 75 6e 6c 69 6e 6b 50 61 67 65 28  */.  unlinkPage(
eb520 70 50 67 29 3b 0a 20 20 61 73 73 65 72 74 28 20  pPg);.  assert( 
eb530 70 50 67 2d 3e 70 67 6e 6f 3d 3d 30 20 29 3b 0a  pPg->pgno==0 );.
eb540 0a 20 20 2a 70 70 50 67 20 3d 20 70 50 67 3b 0a  .  *ppPg = pPg;.
eb550 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
eb560 4f 4b 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51  OK;.}..#ifdef SQ
eb570 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f  LITE_ENABLE_MEMO
eb580 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a  RY_MANAGEMENT./*
eb590 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
eb5a0 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 66  n is called to f
eb5b0 72 65 65 20 73 75 70 65 72 66 6c 75 6f 75 73 20  ree superfluous 
eb5c0 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f  dynamically allo
eb5d0 63 61 74 65 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20  cated memory.** 
eb5e0 68 65 6c 64 20 62 79 20 74 68 65 20 70 61 67 65  held by the page
eb5f0 72 20 73 79 73 74 65 6d 2e 20 4d 65 6d 6f 72 79  r system. Memory
eb600 20 69 6e 20 75 73 65 20 62 79 20 61 6e 79 20 53   in use by any S
eb610 51 4c 69 74 65 20 70 61 67 65 72 20 61 6c 6c 6f  QLite pager allo
eb620 63 61 74 65 64 0a 2a 2a 20 62 79 20 74 68 65 20  cated.** by the 
eb630 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 6d  current thread m
eb640 61 79 20 62 65 20 73 71 6c 69 74 65 33 5f 66 72  ay be sqlite3_fr
eb650 65 65 28 29 65 64 2e 0a 2a 2a 0a 2a 2a 20 6e 52  ee()ed..**.** nR
eb660 65 71 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  eq is the number
eb670 20 6f 66 20 62 79 74 65 73 20 6f 66 20 6d 65 6d   of bytes of mem
eb680 6f 72 79 20 72 65 71 75 69 72 65 64 2e 20 4f 6e  ory required. On
eb690 63 65 20 74 68 69 73 20 6d 75 63 68 20 68 61 73  ce this much has
eb6a0 0a 2a 2a 20 62 65 65 6e 20 72 65 6c 65 61 73 65  .** been release
eb6b0 64 2c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  d, the function 
eb6c0 72 65 74 75 72 6e 73 2e 20 54 68 65 20 72 65 74  returns. The ret
eb6d0 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65  urn value is the
eb6e0 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 0a 2a   total number .*
eb6f0 2a 20 6f 66 20 62 79 74 65 73 20 6f 66 20 6d 65  * of bytes of me
eb700 6d 6f 72 79 20 72 65 6c 65 61 73 65 64 2e 0a 2a  mory released..*
eb710 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
eb720 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
eb730 72 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69  rReleaseMemory(i
eb740 6e 74 20 6e 52 65 71 29 7b 0a 20 20 69 6e 74 20  nt nReq){.  int 
eb750 6e 52 65 6c 65 61 73 65 64 20 3d 20 30 3b 20 20  nReleased = 0;  
eb760 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73          /* Bytes
eb770 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 6c 65 61   of memory relea
eb780 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20  sed so far */.  
eb790 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20  Pager *pPager;  
eb7a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
eb7b0 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
eb7c0 70 61 67 65 72 73 20 2a 2f 0a 20 20 42 75 73 79  pagers */.  Busy
eb7d0 48 61 6e 64 6c 65 72 20 2a 73 61 76 65 64 42 75  Handler *savedBu
eb7e0 73 79 3b 20 20 20 20 20 2f 2a 20 53 61 76 65 64  sy;     /* Saved
eb7f0 20 63 6f 70 79 20 6f 66 20 74 68 65 20 62 75 73   copy of the bus
eb800 79 20 68 61 6e 64 6c 65 72 20 2a 2f 0a 20 20 69  y handler */.  i
eb810 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
eb820 4b 3b 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65  K;..  /* Acquire
eb830 20 74 68 65 20 6d 65 6d 6f 72 79 2d 6d 61 6e 61   the memory-mana
eb840 67 65 6d 65 6e 74 20 6d 75 74 65 78 0a 20 20 2a  gement mutex.  *
eb850 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
eb860 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a 20 20 73 71  _MUTEX_NOOP.  sq
eb870 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
eb880 65 78 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ex;       /* The
eb890 20 4d 45 4d 32 20 6d 75 74 65 78 20 2a 2f 0a 20   MEM2 mutex */. 
eb8a0 20 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33   mutex = sqlite3
eb8b0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c  _mutex_alloc(SQL
eb8c0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
eb8d0 5f 4d 45 4d 32 29 3b 0a 23 65 6e 64 69 66 0a 20  _MEM2);.#endif. 
eb8e0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
eb8f0 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 0a 20 20  nter(mutex);..  
eb900 2f 2a 20 53 69 67 6e 61 6c 20 61 6c 6c 20 64 61  /* Signal all da
eb910 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
eb920 6e 73 20 74 68 61 74 20 6d 65 6d 6f 72 79 20 6d  ns that memory m
eb930 61 6e 61 67 65 6d 65 6e 74 20 77 61 6e 74 73 0a  anagement wants.
eb940 20 20 2a 2a 20 74 6f 20 68 61 76 65 20 61 63 63    ** to have acc
eb950 65 73 73 20 74 6f 20 74 68 65 20 70 61 67 65 72  ess to the pager
eb960 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70 50  s..  */.  for(pP
eb970 61 67 65 72 3d 73 71 6c 69 74 65 33 50 61 67 65  ager=sqlite3Page
eb980 72 4c 69 73 74 3b 20 70 50 61 67 65 72 3b 20 70  rList; pPager; p
eb990 50 61 67 65 72 3d 70 50 61 67 65 72 2d 3e 70 4e  Pager=pPager->pN
eb9a0 65 78 74 29 7b 0a 20 20 20 20 20 70 50 61 67 65  ext){.     pPage
eb9b0 72 2d 3e 69 49 6e 55 73 65 4d 4d 20 3d 20 31 3b  r->iInUseMM = 1;
eb9c0 0a 20 20 7d 0a 0a 20 20 77 68 69 6c 65 28 20 72  .  }..  while( r
eb9d0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
eb9e0 28 6e 52 65 71 3c 30 20 7c 7c 20 6e 52 65 6c 65  (nReq<0 || nRele
eb9f0 61 73 65 64 3c 6e 52 65 71 29 20 29 7b 0a 20 20  ased<nReq) ){.  
eba00 20 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 20 20    PgHdr *pPg;.  
eba10 20 20 50 67 48 64 72 20 2a 70 52 65 63 79 63 6c    PgHdr *pRecycl
eba20 65 64 3b 0a 20 0a 20 20 20 20 2f 2a 20 54 72 79  ed;. .    /* Try
eba30 20 74 6f 20 66 69 6e 64 20 61 20 70 61 67 65 20   to find a page 
eba40 74 6f 20 72 65 63 79 63 6c 65 20 74 68 61 74 20  to recycle that 
eba50 64 6f 65 73 20 6e 6f 74 20 72 65 71 75 69 72 65  does not require
eba60 20 61 20 73 79 6e 63 28 29 2e 20 49 66 0a 20 20   a sync(). If.  
eba70 20 20 2a 2a 20 74 68 69 73 20 69 73 20 6e 6f 74    ** this is not
eba80 20 70 6f 73 73 69 62 6c 65 2c 20 66 69 6e 64 20   possible, find 
eba90 6f 6e 65 20 74 68 61 74 20 64 6f 65 73 20 72 65  one that does re
ebaa0 71 75 69 72 65 20 61 20 73 79 6e 63 28 29 2e 0a  quire a sync()..
ebab0 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
ebac0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73  e3_mutex_enter(s
ebad0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
ebae0 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
ebaf0 53 54 41 54 49 43 5f 4c 52 55 29 29 3b 0a 20 20  STATIC_LRU));.  
ebb00 20 20 70 50 67 20 3d 20 73 71 6c 69 74 65 33 4c    pPg = sqlite3L
ebb10 72 75 50 61 67 65 4c 69 73 74 2e 70 46 69 72 73  ruPageList.pFirs
ebb20 74 53 79 6e 63 65 64 3b 0a 20 20 20 20 77 68 69  tSynced;.    whi
ebb30 6c 65 28 20 70 50 67 20 26 26 20 28 70 50 67 2d  le( pPg && (pPg-
ebb40 3e 6e 65 65 64 53 79 6e 63 20 7c 7c 20 70 50 67  >needSync || pPg
ebb50 2d 3e 70 50 61 67 65 72 2d 3e 69 49 6e 55 73 65  ->pPager->iInUse
ebb60 44 42 29 20 29 7b 0a 20 20 20 20 20 20 70 50 67  DB) ){.      pPg
ebb70 20 3d 20 70 50 67 2d 3e 67 66 72 65 65 2e 70 4e   = pPg->gfree.pN
ebb80 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ext;.    }.    i
ebb90 66 28 20 21 70 50 67 20 29 7b 0a 20 20 20 20 20  f( !pPg ){.     
ebba0 20 70 50 67 20 3d 20 73 71 6c 69 74 65 33 4c 72   pPg = sqlite3Lr
ebbb0 75 50 61 67 65 4c 69 73 74 2e 70 46 69 72 73 74  uPageList.pFirst
ebbc0 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 70  ;.      while( p
ebbd0 50 67 20 26 26 20 70 50 67 2d 3e 70 50 61 67 65  Pg && pPg->pPage
ebbe0 72 2d 3e 69 49 6e 55 73 65 44 42 20 29 7b 0a 20  r->iInUseDB ){. 
ebbf0 20 20 20 20 20 20 20 70 50 67 20 3d 20 70 50 67         pPg = pPg
ebc00 2d 3e 67 66 72 65 65 2e 70 4e 65 78 74 3b 0a 20  ->gfree.pNext;. 
ebc10 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
ebc20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
ebc30 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  eave(sqlite3_mut
ebc40 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  ex_alloc(SQLITE_
ebc50 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55  MUTEX_STATIC_LRU
ebc60 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70  ));..    /* If p
ebc70 50 67 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 20  Pg==0, then the 
ebc80 62 6c 6f 63 6b 20 61 62 6f 76 65 20 68 61 73 20  block above has 
ebc90 66 61 69 6c 65 64 20 74 6f 20 66 69 6e 64 20 61  failed to find a
ebca0 20 70 61 67 65 20 74 6f 0a 20 20 20 20 2a 2a 20   page to.    ** 
ebcb0 72 65 63 79 63 6c 65 2e 20 49 6e 20 74 68 69 73  recycle. In this
ebcc0 20 63 61 73 65 20 72 65 74 75 72 6e 20 65 61 72   case return ear
ebcd0 6c 79 20 2d 20 6e 6f 20 66 75 72 74 68 65 72 20  ly - no further 
ebce0 6d 65 6d 6f 72 79 20 77 69 6c 6c 0a 20 20 20 20  memory will.    
ebcf0 2a 2a 20 62 65 20 72 65 6c 65 61 73 65 64 2e 0a  ** be released..
ebd00 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21      */.    if( !
ebd10 70 50 67 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20  pPg ) break;..  
ebd20 20 20 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e    pPager = pPg->
ebd30 70 50 61 67 65 72 3b 0a 20 20 20 20 61 73 73 65  pPager;.    asse
ebd40 72 74 28 21 70 50 67 2d 3e 6e 65 65 64 53 79 6e  rt(!pPg->needSyn
ebd50 63 20 7c 7c 20 70 50 67 3d 3d 70 50 61 67 65 72  c || pPg==pPager
ebd60 2d 3e 6c 72 75 2e 70 46 69 72 73 74 29 3b 0a 20  ->lru.pFirst);. 
ebd70 20 20 20 61 73 73 65 72 74 28 70 50 67 2d 3e 6e     assert(pPg->n
ebd80 65 65 64 53 79 6e 63 20 7c 7c 20 70 50 67 3d 3d  eedSync || pPg==
ebd90 70 50 61 67 65 72 2d 3e 6c 72 75 2e 70 46 69 72  pPager->lru.pFir
ebda0 73 74 53 79 6e 63 65 64 29 3b 0a 20 20 0a 20 20  stSynced);.  .  
ebdb0 20 20 73 61 76 65 64 42 75 73 79 20 3d 20 70 50    savedBusy = pP
ebdc0 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c  ager->pBusyHandl
ebdd0 65 72 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  er;.    pPager->
ebde0 70 42 75 73 79 48 61 6e 64 6c 65 72 20 3d 20 30  pBusyHandler = 0
ebdf0 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72  ;.    rc = pager
ebe00 5f 72 65 63 79 63 6c 65 28 70 50 61 67 65 72 2c  _recycle(pPager,
ebe10 20 26 70 52 65 63 79 63 6c 65 64 29 3b 0a 20 20   &pRecycled);.  
ebe20 20 20 70 50 61 67 65 72 2d 3e 70 42 75 73 79 48    pPager->pBusyH
ebe30 61 6e 64 6c 65 72 20 3d 20 73 61 76 65 64 42 75  andler = savedBu
ebe40 73 79 3b 0a 20 20 20 20 61 73 73 65 72 74 28 70  sy;.    assert(p
ebe50 52 65 63 79 63 6c 65 64 3d 3d 70 50 67 20 7c 7c  Recycled==pPg ||
ebe60 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 3b   rc!=SQLITE_OK);
ebe70 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
ebe80 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
ebe90 2f 2a 20 57 65 27 76 65 20 66 6f 75 6e 64 20 61  /* We've found a
ebea0 20 70 61 67 65 20 74 6f 20 66 72 65 65 2e 20 41   page to free. A
ebeb0 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65  t this point the
ebec0 20 70 61 67 65 20 68 61 73 20 62 65 65 6e 20 0a   page has been .
ebed0 20 20 20 20 20 20 2a 2a 20 72 65 6d 6f 76 65 64        ** removed
ebee0 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 20 68   from the page h
ebef0 61 73 68 2d 74 61 62 6c 65 2c 20 66 72 65 65 2d  ash-table, free-
ebf00 6c 69 73 74 20 61 6e 64 20 73 79 6e 63 65 64 2d  list and synced-
ebf10 6c 69 73 74 20 0a 20 20 20 20 20 20 2a 2a 20 28  list .      ** (
ebf20 70 46 69 72 73 74 53 79 6e 63 65 64 29 2e 20 49  pFirstSynced). I
ebf30 74 20 69 73 20 73 74 69 6c 6c 20 69 6e 20 74 68  t is still in th
ebf40 65 20 61 6c 6c 20 70 61 67 65 73 20 28 70 41 6c  e all pages (pAl
ebf50 6c 29 20 6c 69 73 74 2e 20 0a 20 20 20 20 20 20  l) list. .      
ebf60 2a 2a 20 52 65 6d 6f 76 65 20 69 74 20 66 72 6f  ** Remove it fro
ebf70 6d 20 74 68 69 73 20 6c 69 73 74 20 62 65 66 6f  m this list befo
ebf80 72 65 20 66 72 65 65 69 6e 67 2e 0a 20 20 20 20  re freeing..    
ebf90 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 6f    **.      ** To
ebfa0 64 6f 3a 20 43 68 65 63 6b 20 74 68 65 20 50 61  do: Check the Pa
ebfb0 67 65 72 2e 70 53 74 6d 74 20 6c 69 73 74 20 74  ger.pStmt list t
ebfc0 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 69 73  o make sure this
ebfd0 20 69 73 20 4f 6b 2e 20 49 74 20 0a 20 20 20 20   is Ok. It .    
ebfe0 20 20 2a 2a 20 70 72 6f 62 61 62 6c 79 20 69 73    ** probably is
ebff0 20 74 68 6f 75 67 68 2e 0a 20 20 20 20 20 20 2a   though..      *
ec000 2f 0a 20 20 20 20 20 20 50 67 48 64 72 20 2a 70  /.      PgHdr *p
ec010 54 6d 70 3b 0a 20 20 20 20 20 20 61 73 73 65 72  Tmp;.      asser
ec020 74 28 20 70 50 67 20 29 3b 0a 20 20 20 20 20 20  t( pPg );.      
ec030 69 66 28 20 70 50 67 3d 3d 70 50 61 67 65 72 2d  if( pPg==pPager-
ec040 3e 70 41 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20  >pAll ){.       
ec050 20 20 70 50 61 67 65 72 2d 3e 70 41 6c 6c 20 3d    pPager->pAll =
ec060 20 70 50 67 2d 3e 70 4e 65 78 74 41 6c 6c 3b 0a   pPg->pNextAll;.
ec070 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
ec080 20 20 20 20 20 66 6f 72 28 20 70 54 6d 70 3d 70       for( pTmp=p
ec090 50 61 67 65 72 2d 3e 70 41 6c 6c 3b 20 70 54 6d  Pager->pAll; pTm
ec0a0 70 2d 3e 70 4e 65 78 74 41 6c 6c 21 3d 70 50 67  p->pNextAll!=pPg
ec0b0 3b 20 70 54 6d 70 3d 70 54 6d 70 2d 3e 70 4e 65  ; pTmp=pTmp->pNe
ec0c0 78 74 41 6c 6c 20 29 7b 7d 0a 20 20 20 20 20 20  xtAll ){}.      
ec0d0 20 20 70 54 6d 70 2d 3e 70 4e 65 78 74 41 6c 6c    pTmp->pNextAll
ec0e0 20 3d 20 70 50 67 2d 3e 70 4e 65 78 74 41 6c 6c   = pPg->pNextAll
ec0f0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
ec100 6e 52 65 6c 65 61 73 65 64 20 2b 3d 20 28 0a 20  nReleased += (. 
ec110 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28           sizeof(
ec120 2a 70 50 67 29 20 2b 20 70 50 61 67 65 72 2d 3e  *pPg) + pPager->
ec130 70 61 67 65 53 69 7a 65 0a 20 20 20 20 20 20 20  pageSize.       
ec140 20 20 20 2b 20 73 69 7a 65 6f 66 28 75 33 32 29     + sizeof(u32)
ec150 20 2b 20 70 50 61 67 65 72 2d 3e 6e 45 78 74 72   + pPager->nExtr
ec160 61 0a 20 20 20 20 20 20 20 20 20 20 2b 20 4d 45  a.          + ME
ec170 4d 44 42 2a 73 69 7a 65 6f 66 28 50 67 48 69 73  MDB*sizeof(PgHis
ec180 74 6f 72 79 29 20 0a 20 20 20 20 20 20 29 3b 0a  tory) .      );.
ec190 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22        IOTRACE(("
ec1a0 50 47 46 52 45 45 20 25 70 20 25 64 20 2a 5c 6e  PGFREE %p %d *\n
ec1b0 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 2d 3e  ", pPager, pPg->
ec1c0 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 50 41  pgno));.      PA
ec1d0 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65 33  GER_INCR(sqlite3
ec1e0 5f 70 61 67 65 72 5f 70 67 66 72 65 65 5f 63 6f  _pager_pgfree_co
ec1f0 75 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  unt);.      sqli
ec200 74 65 33 5f 66 72 65 65 28 70 50 67 2d 3e 70 44  te3_free(pPg->pD
ec210 61 74 61 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ata);.      sqli
ec220 74 65 33 5f 66 72 65 65 28 70 50 67 29 3b 0a 20  te3_free(pPg);. 
ec230 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 50 61       pPager->nPa
ec240 67 65 2d 2d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ge--;.    }else{
ec250 0a 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 72 72  .      /* An err
ec260 6f 72 20 6f 63 63 75 72 65 64 20 77 68 69 6c 73  or occured whils
ec270 74 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65  t writing to the
ec280 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f   database file o
ec290 72 20 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72  r .      ** jour
ec2a0 6e 61 6c 20 69 6e 20 70 61 67 65 72 5f 72 65 63  nal in pager_rec
ec2b0 79 63 6c 65 28 29 2e 20 54 68 65 20 65 72 72 6f  ycle(). The erro
ec2c0 72 20 69 73 20 6e 6f 74 20 72 65 74 75 72 6e 65  r is not returne
ec2d0 64 20 74 6f 20 74 68 65 20 0a 20 20 20 20 20 20  d to the .      
ec2e0 2a 2a 20 63 61 6c 6c 65 72 20 6f 66 20 74 68 69  ** caller of thi
ec2f0 73 20 66 75 6e 63 74 69 6f 6e 2e 20 49 6e 73 74  s function. Inst
ec300 65 61 64 2c 20 73 65 74 20 74 68 65 20 50 61 67  ead, set the Pag
ec310 65 72 2e 65 72 72 43 6f 64 65 20 76 61 72 69 61  er.errCode varia
ec320 62 6c 65 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68  ble..      ** Th
ec330 65 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20  e error will be 
ec340 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20  returned to the 
ec350 75 73 65 72 20 28 6f 72 20 75 73 65 72 73 2c 20  user (or users, 
ec360 69 6e 20 74 68 65 20 63 61 73 65 20 0a 20 20 20  in the case .   
ec370 20 20 20 2a 2a 20 6f 66 20 61 20 73 68 61 72 65     ** of a share
ec380 64 20 70 61 67 65 72 20 63 61 63 68 65 29 20 6f  d pager cache) o
ec390 66 20 74 68 65 20 70 61 67 65 72 20 66 6f 72 20  f the pager for 
ec3a0 77 68 69 63 68 20 74 68 65 20 65 72 72 6f 72 20  which the error 
ec3b0 6f 63 63 75 72 65 64 2e 0a 20 20 20 20 20 20 2a  occured..      *
ec3c0 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 0a  /.      assert(.
ec3d0 20 20 20 20 20 20 20 20 20 20 28 72 63 26 30 78            (rc&0x
ec3e0 66 66 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52  ff)==SQLITE_IOER
ec3f0 52 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 72  R ||.          r
ec400 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 7c  c==SQLITE_FULL |
ec410 7c 0a 20 20 20 20 20 20 20 20 20 20 72 63 3d 3d  |.          rc==
ec420 53 51 4c 49 54 45 5f 42 55 53 59 0a 20 20 20 20  SQLITE_BUSY.    
ec430 20 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72    );.      asser
ec440 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  t( pPager->state
ec450 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44  >=PAGER_RESERVED
ec460 20 29 3b 0a 20 20 20 20 20 20 70 61 67 65 72 5f   );.      pager_
ec470 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63  error(pPager, rc
ec480 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
ec490 2f 2a 20 43 6c 65 61 72 20 74 68 65 20 6d 65 6d  /* Clear the mem
ec4a0 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 20 66  ory management f
ec4b0 6c 61 67 73 20 61 6e 64 20 72 65 6c 65 61 73 65  lags and release
ec4c0 20 74 68 65 20 6d 75 74 65 78 0a 20 20 2a 2f 0a   the mutex.  */.
ec4d0 20 20 66 6f 72 28 70 50 61 67 65 72 3d 73 71 6c    for(pPager=sql
ec4e0 69 74 65 33 50 61 67 65 72 4c 69 73 74 3b 20 70  ite3PagerList; p
ec4f0 50 61 67 65 72 3b 20 70 50 61 67 65 72 3d 70 50  Pager; pPager=pP
ec500 61 67 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  ager->pNext){.  
ec510 20 20 20 70 50 61 67 65 72 2d 3e 69 49 6e 55 73     pPager->iInUs
ec520 65 4d 4d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73  eMM = 0;.  }.  s
ec530 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
ec540 76 65 28 6d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a  ve(mutex);..  /*
ec550 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
ec560 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 6c 65  er of bytes rele
ec570 61 73 65 64 0a 20 20 2a 2f 0a 20 20 72 65 74 75  ased.  */.  retu
ec580 72 6e 20 6e 52 65 6c 65 61 73 65 64 3b 0a 7d 0a  rn nReleased;.}.
ec590 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
ec5a0 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d  _ENABLE_MEMORY_M
ec5b0 41 4e 41 47 45 4d 45 4e 54 20 2a 2f 0a 0a 2f 2a  ANAGEMENT */../*
ec5c0 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 63 6f 6e  .** Read the con
ec5d0 74 65 6e 74 20 6f 66 20 70 61 67 65 20 70 50 67  tent of page pPg
ec5e0 20 6f 75 74 20 6f 66 20 74 68 65 20 64 61 74 61   out of the data
ec5f0 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74  base file..*/.st
ec600 61 74 69 63 20 69 6e 74 20 72 65 61 64 44 62 50  atic int readDbP
ec610 61 67 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  age(Pager *pPage
ec620 72 2c 20 50 67 48 64 72 20 2a 70 50 67 2c 20 50  r, PgHdr *pPg, P
ec630 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74  gno pgno){.  int
ec640 20 72 63 3b 0a 20 20 69 36 34 20 6f 66 66 73 65   rc;.  i64 offse
ec650 74 3b 0a 20 20 61 73 73 65 72 74 28 20 4d 45 4d  t;.  assert( MEM
ec660 44 42 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  DB==0 );.  asser
ec670 74 28 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d  t(pPager->fd->pM
ec680 65 74 68 6f 64 73 7c 7c 70 50 61 67 65 72 2d 3e  ethods||pPager->
ec690 74 65 6d 70 46 69 6c 65 29 3b 0a 20 20 69 66 28  tempFile);.  if(
ec6a0 20 21 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d   !pPager->fd->pM
ec6b0 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 72 65  ethods ){.    re
ec6c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
ec6d0 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20  R_SHORT_READ;.  
ec6e0 7d 0a 20 20 6f 66 66 73 65 74 20 3d 20 28 70 67  }.  offset = (pg
ec6f0 6e 6f 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65  no-1)*(i64)pPage
ec700 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 72  r->pageSize;.  r
ec710 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61  c = sqlite3OsRea
ec720 64 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 50 47  d(pPager->fd, PG
ec730 48 44 52 5f 54 4f 5f 44 41 54 41 28 70 50 67 29  HDR_TO_DATA(pPg)
ec740 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  , pPager->pageSi
ec750 7a 65 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 50  ze, offset);.  P
ec760 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65  AGER_INCR(sqlite
ec770 33 5f 70 61 67 65 72 5f 72 65 61 64 64 62 5f 63  3_pager_readdb_c
ec780 6f 75 6e 74 29 3b 0a 20 20 50 41 47 45 52 5f 49  ount);.  PAGER_I
ec790 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 52 65 61  NCR(pPager->nRea
ec7a0 64 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22  d);.  IOTRACE(("
ec7b0 50 47 49 4e 20 25 70 20 25 64 5c 6e 22 2c 20 70  PGIN %p %d\n", p
ec7c0 50 61 67 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20  Pager, pgno));. 
ec7d0 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a   if( pgno==1 ){.
ec7e0 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67      memcpy(&pPag
ec7f0 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20  er->dbFileVers, 
ec800 26 28 28 75 38 2a 29 50 47 48 44 52 5f 54 4f 5f  &((u8*)PGHDR_TO_
ec810 44 41 54 41 28 70 50 67 29 29 5b 32 34 5d 2c 0a  DATA(pPg))[24],.
ec820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ec830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ec840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 69                si
ec850 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 46  zeof(pPager->dbF
ec860 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 7d 0a 20  ileVers));.  }. 
ec870 20 43 4f 44 45 43 31 28 70 50 61 67 65 72 2c 20   CODEC1(pPager, 
ec880 50 47 48 44 52 5f 54 4f 5f 44 41 54 41 28 70 50  PGHDR_TO_DATA(pP
ec890 67 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 33  g), pPg->pgno, 3
ec8a0 29 3b 0a 20 20 50 41 47 45 52 54 52 41 43 45 34  );.  PAGERTRACE4
ec8b0 28 22 46 45 54 43 48 20 25 64 20 70 61 67 65 20  ("FETCH %d page 
ec8c0 25 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22  %d hash(%08x)\n"
ec8d0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
ec8e0 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29   PAGERID(pPager)
ec8f0 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 70 61 67  , pPg->pgno, pag
ec900 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29  er_pagehash(pPg)
ec910 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
ec920 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  }.../*.** This f
ec930 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
ec940 64 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20  d to obtain the 
ec950 73 68 61 72 65 64 20 6c 6f 63 6b 20 72 65 71 75  shared lock requ
ec960 69 72 65 64 20 62 65 66 6f 72 65 0a 2a 2a 20 64  ired before.** d
ec970 61 74 61 20 6d 61 79 20 62 65 20 72 65 61 64 20  ata may be read 
ec980 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20 63  from the pager c
ec990 61 63 68 65 2e 20 49 66 20 74 68 65 20 73 68 61  ache. If the sha
ec9a0 72 65 64 20 6c 6f 63 6b 20 68 61 73 20 61 6c 72  red lock has alr
ec9b0 65 61 64 79 0a 2a 2a 20 62 65 65 6e 20 6f 62 74  eady.** been obt
ec9c0 61 69 6e 65 64 2c 20 74 68 69 73 20 66 75 6e 63  ained, this func
ec9d0 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tion is a no-op.
ec9e0 0a 2a 2a 0a 2a 2a 20 49 6d 6d 65 64 69 61 74 65  .**.** Immediate
ec9f0 6c 79 20 61 66 74 65 72 20 6f 62 74 61 69 6e 69  ly after obtaini
eca00 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f  ng the shared lo
eca10 63 6b 20 28 69 66 20 72 65 71 75 69 72 65 64 29  ck (if required)
eca20 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a  , this function.
eca30 2a 2a 20 63 68 65 63 6b 73 20 66 6f 72 20 61 20  ** checks for a 
eca40 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  hot-journal file
eca50 2e 20 49 66 20 6f 6e 65 20 69 73 20 66 6f 75 6e  . If one is foun
eca60 64 2c 20 61 6e 20 65 6d 65 72 67 65 6e 63 79 20  d, an emergency 
eca70 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 70  rollback.** is p
eca80 65 72 66 6f 72 6d 65 64 20 69 6d 6d 65 64 69 61  erformed immedia
eca90 74 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tely..*/.static 
ecaa0 69 6e 74 20 70 61 67 65 72 53 68 61 72 65 64 4c  int pagerSharedL
ecab0 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65  ock(Pager *pPage
ecac0 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  r){.  int rc = S
ecad0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20  QLITE_OK;.  int 
ecae0 69 73 48 6f 74 20 3d 20 30 3b 0a 0a 20 20 2f 2a  isHot = 0;..  /*
ecaf0 20 49 66 20 74 68 69 73 20 64 61 74 61 62 61 73   If this databas
ecb00 65 20 69 73 20 6f 70 65 6e 65 64 20 66 6f 72 20  e is opened for 
ecb10 65 78 63 6c 75 73 69 76 65 20 61 63 63 65 73 73  exclusive access
ecb20 2c 20 68 61 73 20 6e 6f 20 6f 75 74 73 74 61 6e  , has no outstan
ecb30 64 69 6e 67 20 0a 20 20 2a 2a 20 70 61 67 65 20  ding .  ** page 
ecb40 72 65 66 65 72 65 6e 63 65 73 20 61 6e 64 20 69  references and i
ecb50 73 20 69 6e 20 61 6e 20 65 72 72 6f 72 2d 73 74  s in an error-st
ecb60 61 74 65 2c 20 6e 6f 77 20 69 73 20 74 68 65 20  ate, now is the 
ecb70 63 68 61 6e 63 65 20 74 6f 20 63 6c 65 61 72 0a  chance to clear.
ecb80 20 20 2a 2a 20 74 68 65 20 65 72 72 6f 72 2e 20    ** the error. 
ecb90 44 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74  Discard the cont
ecba0 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65  ents of the page
ecbb0 72 2d 63 61 63 68 65 20 61 6e 64 20 74 72 65 61  r-cache and trea
ecbc0 74 20 61 6e 79 0a 20 20 2a 2a 20 6f 70 65 6e 20  t any.  ** open 
ecbd0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 73 20  journal file as 
ecbe0 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 2e 0a 20  a hot-journal.. 
ecbf0 20 2a 2f 0a 20 20 69 66 28 20 21 4d 45 4d 44 42   */.  if( !MEMDB
ecc00 20 26 26 20 70 50 61 67 65 72 2d 3e 65 78 63 6c   && pPager->excl
ecc10 75 73 69 76 65 4d 6f 64 65 20 26 26 20 70 50 61  usiveMode && pPa
ecc20 67 65 72 2d 3e 6e 52 65 66 3d 3d 30 20 26 26 20  ger->nRef==0 && 
ecc30 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20  pPager->errCode 
ecc40 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  ){.    if( pPage
ecc50 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29  r->journalOpen )
ecc60 7b 0a 20 20 20 20 20 20 69 73 48 6f 74 20 3d 20  {.      isHot = 
ecc70 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61  1;.    }.    pPa
ecc80 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53  ger->errCode = S
ecc90 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 70 61  QLITE_OK;.    pa
ecca0 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72  ger_reset(pPager
eccb0 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  );.  }..  /* If 
eccc0 74 68 65 20 70 61 67 65 72 20 69 73 20 73 74 69  the pager is sti
eccd0 6c 6c 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73  ll in an error s
ecce0 74 61 74 65 2c 20 64 6f 20 6e 6f 74 20 70 72 6f  tate, do not pro
eccf0 63 65 65 64 2e 20 54 68 65 20 65 72 72 6f 72 20  ceed. The error 
ecd00 0a 20 20 2a 2a 20 73 74 61 74 65 20 77 69 6c 6c  .  ** state will
ecd10 20 62 65 20 63 6c 65 61 72 65 64 20 61 74 20 73   be cleared at s
ecd20 6f 6d 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65  ome point in the
ecd30 20 66 75 74 75 72 65 20 77 68 65 6e 20 61 6c 6c   future when all
ecd40 20 70 61 67 65 20 0a 20 20 2a 2a 20 72 65 66 65   page .  ** refe
ecd50 72 65 6e 63 65 73 20 61 72 65 20 64 72 6f 70 70  rences are dropp
ecd60 65 64 20 61 6e 64 20 74 68 65 20 63 61 63 68 65  ed and the cache
ecd70 20 63 61 6e 20 62 65 20 64 69 73 63 61 72 64 65   can be discarde
ecd80 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50  d..  */.  if( pP
ecd90 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 26 26  ager->errCode &&
ecda0 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
ecdb0 21 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 7b  !=SQLITE_FULL ){
ecdc0 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 61 67  .    return pPag
ecdd0 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d  er->errCode;.  }
ecde0 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ..  if( pPager->
ecdf0 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c  state==PAGER_UNL
ece00 4f 43 4b 20 7c 7c 20 69 73 48 6f 74 20 29 7b 0a  OCK || isHot ){.
ece10 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20      sqlite3_vfs 
ece20 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e  *pVfs = pPager->
ece30 70 56 66 73 3b 0a 20 20 20 20 69 66 28 20 21 4d  pVfs;.    if( !M
ece40 45 4d 44 42 20 29 7b 0a 20 20 20 20 20 20 61 73  EMDB ){.      as
ece50 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 52  sert( pPager->nR
ece60 65 66 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69  ef==0 );.      i
ece70 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 52 65  f( !pPager->noRe
ece80 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20  adlock ){.      
ece90 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69    rc = pager_wai
ecea0 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72  t_on_lock(pPager
eceb0 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a  , SHARED_LOCK);.
ecec0 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
eced0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
ecee0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
ecef0 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41  Pager->state==PA
ecf00 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20  GER_UNLOCK );.  
ecf10 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70          return p
ecf20 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65  ager_error(pPage
ecf30 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 20 20  r, rc);.        
ecf40 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  }.        assert
ecf50 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
ecf60 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
ecf70 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 20        }.  .     
ecf80 20 2f 2a 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c   /* If a journal
ecf90 20 66 69 6c 65 20 65 78 69 73 74 73 2c 20 61 6e   file exists, an
ecfa0 64 20 74 68 65 72 65 20 69 73 20 6e 6f 20 52 45  d there is no RE
ecfb0 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74  SERVED lock on t
ecfc0 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61  he.      ** data
ecfd0 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20  base file, then 
ecfe0 69 74 20 65 69 74 68 65 72 20 6e 65 65 64 73 20  it either needs 
ecff0 74 6f 20 62 65 20 70 6c 61 79 65 64 20 62 61 63  to be played bac
ed000 6b 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a 20 20  k or deleted..  
ed010 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20      */.      rc 
ed020 3d 20 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c 28  = hasHotJournal(
ed030 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 69  pPager);.      i
ed040 66 28 20 72 63 3c 30 20 29 7b 0a 20 20 20 20 20  f( rc<0 ){.     
ed050 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49     rc = SQLITE_I
ed060 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  OERR_NOMEM;.    
ed070 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b      goto failed;
ed080 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
ed090 66 28 20 72 63 3d 3d 31 20 7c 7c 20 69 73 48 6f  f( rc==1 || isHo
ed0a0 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  t ){.        /* 
ed0b0 47 65 74 20 61 6e 20 45 58 43 4c 55 53 49 56 45  Get an EXCLUSIVE
ed0c0 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74   lock on the dat
ed0d0 61 62 61 73 65 20 66 69 6c 65 2e 20 41 74 20 74  abase file. At t
ed0e0 68 69 73 20 70 6f 69 6e 74 20 69 74 20 69 73 0a  his point it is.
ed0f0 20 20 20 20 20 20 20 20 2a 2a 20 69 6d 70 6f 72          ** impor
ed100 74 61 6e 74 20 74 68 61 74 20 61 20 52 45 53 45  tant that a RESE
ed110 52 56 45 44 20 6c 6f 63 6b 20 69 73 20 6e 6f 74  RVED lock is not
ed120 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65   obtained on the
ed130 20 77 61 79 20 74 6f 20 74 68 65 0a 20 20 20 20   way to the.    
ed140 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49 56 45      ** EXCLUSIVE
ed150 20 6c 6f 63 6b 2e 20 49 66 20 69 74 20 77 65 72   lock. If it wer
ed160 65 2c 20 61 6e 6f 74 68 65 72 20 70 72 6f 63 65  e, another proce
ed170 73 73 20 6d 69 67 68 74 20 6f 70 65 6e 20 74 68  ss might open th
ed180 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 61 74  e.        ** dat
ed190 61 62 61 73 65 20 66 69 6c 65 2c 20 64 65 74 65  abase file, dete
ed1a0 63 74 20 74 68 65 20 52 45 53 45 52 56 45 44 20  ct the RESERVED 
ed1b0 6c 6f 63 6b 2c 20 61 6e 64 20 63 6f 6e 63 6c 75  lock, and conclu
ed1c0 64 65 20 74 68 61 74 20 74 68 65 0a 20 20 20 20  de that the.    
ed1d0 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20      ** database 
ed1e0 69 73 20 73 61 66 65 20 74 6f 20 72 65 61 64 20  is safe to read 
ed1f0 77 68 69 6c 65 20 74 68 69 73 20 70 72 6f 63 65  while this proce
ed200 73 73 20 69 73 20 73 74 69 6c 6c 20 72 6f 6c 6c  ss is still roll
ed210 69 6e 67 20 69 74 20 0a 20 20 20 20 20 20 20 20  ing it .        
ed220 2a 2a 20 62 61 63 6b 2e 0a 20 20 20 20 20 20 20  ** back..       
ed230 20 2a 2a 20 0a 20 20 20 20 20 20 20 20 2a 2a 20   ** .        ** 
ed240 42 65 63 61 75 73 65 20 74 68 65 20 69 6e 74 65  Because the inte
ed250 72 6d 65 64 69 61 74 65 20 52 45 53 45 52 56 45  rmediate RESERVE
ed260 44 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 72 65  D lock is not re
ed270 71 75 65 73 74 65 64 2c 20 74 68 65 0a 20 20 20  quested, the.   
ed280 20 20 20 20 20 2a 2a 20 73 65 63 6f 6e 64 20 70       ** second p
ed290 72 6f 63 65 73 73 20 77 69 6c 6c 20 67 65 74 20  rocess will get 
ed2a0 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 20 69 6e  to this point in
ed2b0 20 74 68 65 20 63 6f 64 65 20 61 6e 64 20 66 61   the code and fa
ed2c0 69 6c 20 74 6f 0a 20 20 20 20 20 20 20 20 2a 2a  il to.        **
ed2d0 20 6f 62 74 61 69 6e 20 69 74 73 20 6f 77 6e 20   obtain its own 
ed2e0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f  EXCLUSIVE lock o
ed2f0 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
ed300 69 6c 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ile..        */.
ed310 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67          if( pPag
ed320 65 72 2d 3e 73 74 61 74 65 3c 45 58 43 4c 55 53  er->state<EXCLUS
ed330 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  IVE_LOCK ){.    
ed340 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
ed350 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d  e3OsLock(pPager-
ed360 3e 66 64 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c  >fd, EXCLUSIVE_L
ed370 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 20 20  OCK);.          
ed380 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
ed390 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  K ){.           
ed3a0 20 72 63 20 3d 20 70 61 67 65 72 5f 65 72 72 6f   rc = pager_erro
ed3b0 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20  r(pPager, rc);. 
ed3c0 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20             goto 
ed3d0 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 20  failed;.        
ed3e0 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 50    }.          pP
ed3f0 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41  ager->state = PA
ed400 47 45 52 5f 45 58 43 4c 55 53 49 56 45 3b 0a 20  GER_EXCLUSIVE;. 
ed410 20 20 20 20 20 20 20 7d 0a 20 0a 20 20 20 20 20         }. .     
ed420 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a     /* Open the j
ed430 6f 75 72 6e 61 6c 20 66 6f 72 20 72 65 61 64 2f  ournal for read/
ed440 77 72 69 74 65 20 61 63 63 65 73 73 2e 20 54 68  write access. Th
ed450 69 73 20 69 73 20 62 65 63 61 75 73 65 20 69 6e  is is because in
ed460 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 78 63   .        ** exc
ed470 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20 6d 6f  lusive-access mo
ed480 64 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  de the file desc
ed490 72 69 70 74 6f 72 20 77 69 6c 6c 20 62 65 20 6b  riptor will be k
ed4a0 65 70 74 20 6f 70 65 6e 20 61 6e 64 0a 20 20 20  ept open and.   
ed4b0 20 20 20 20 20 2a 2a 20 70 6f 73 73 69 62 6c 79       ** possibly
ed4c0 20 75 73 65 64 20 66 6f 72 20 61 20 74 72 61 6e   used for a tran
ed4d0 73 61 63 74 69 6f 6e 20 6c 61 74 65 72 20 6f 6e  saction later on
ed4e0 2e 20 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d  . On some system
ed4f0 73 2c 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  s, the.        *
ed500 2a 20 4f 73 54 72 75 6e 63 61 74 65 28 29 20 63  * OsTruncate() c
ed510 61 6c 6c 20 75 73 65 64 20 69 6e 20 65 78 63 6c  all used in excl
ed520 75 73 69 76 65 2d 61 63 63 65 73 73 20 6d 6f 64  usive-access mod
ed530 65 20 61 6c 73 6f 20 72 65 71 75 69 72 65 73 0a  e also requires.
ed540 20 20 20 20 20 20 20 20 2a 2a 20 61 20 72 65 61          ** a rea
ed550 64 2f 77 72 69 74 65 20 66 69 6c 65 20 68 61 6e  d/write file han
ed560 64 6c 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  dle..        */.
ed570 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73 48          if( !isH
ed580 6f 74 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f  ot && pPager->jo
ed590 75 72 6e 61 6c 4f 70 65 6e 3d 3d 30 20 29 7b 0a  urnalOpen==0 ){.
ed5a0 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 72 65            int re
ed5b0 73 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63  s = sqlite3OsAcc
ed5c0 65 73 73 28 70 56 66 73 2c 70 50 61 67 65 72 2d  ess(pVfs,pPager-
ed5d0 3e 7a 4a 6f 75 72 6e 61 6c 2c 53 51 4c 49 54 45  >zJournal,SQLITE
ed5e0 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 29 3b  _ACCESS_EXISTS);
ed5f0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72  .          if( r
ed600 65 73 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20  es==1 ){.       
ed610 20 20 20 20 20 69 6e 74 20 66 6f 75 74 20 3d 20       int fout = 
ed620 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  0;.            i
ed630 6e 74 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f 50  nt f = SQLITE_OP
ed640 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c  EN_READWRITE|SQL
ed650 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f  ITE_OPEN_MAIN_JO
ed660 55 52 4e 41 4c 3b 0a 20 20 20 20 20 20 20 20 20  URNAL;.         
ed670 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61 67     assert( !pPag
ed680 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a  er->tempFile );.
ed690 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
ed6a0 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70   sqlite3OsOpen(p
ed6b0 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f  Vfs, pPager->zJo
ed6c0 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a  urnal, pPager->j
ed6d0 66 64 2c 20 66 2c 20 26 66 6f 75 74 29 3b 0a 20  fd, f, &fout);. 
ed6e0 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72             asser
ed6f0 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc!=SQLITE_OK
ed700 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 66 64 2d   || pPager->jfd-
ed710 3e 70 4d 65 74 68 6f 64 73 20 29 3b 0a 20 20 20  >pMethods );.   
ed720 20 20 20 20 20 20 20 20 20 69 66 28 20 66 6f 75           if( fou
ed730 74 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  t&SQLITE_OPEN_RE
ed740 41 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20  ADONLY ){.      
ed750 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
ed760 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20  ITE_BUSY;.      
ed770 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f          sqlite3O
ed780 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a  sClose(pPager->j
ed790 66 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  fd);.           
ed7a0 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c   }.          }el
ed7b0 73 65 20 69 66 28 20 72 65 73 3d 3d 30 20 29 7b  se if( res==0 ){
ed7c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  .            /* 
ed7d0 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 64  If the journal d
ed7e0 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 74  oes not exist, t
ed7f0 68 61 74 20 6d 65 61 6e 73 20 73 6f 6d 65 20 6f  hat means some o
ed800 74 68 65 72 20 70 72 6f 63 65 73 73 0a 20 20 20  ther process.   
ed810 20 20 20 20 20 20 20 20 20 2a 2a 20 68 61 73 20           ** has 
ed820 61 6c 72 65 61 64 79 20 72 6f 6c 6c 65 64 20 69  already rolled i
ed830 74 20 62 61 63 6b 20 2a 2f 0a 20 20 20 20 20 20  t back */.      
ed840 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
ed850 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20  E_BUSY;.        
ed860 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
ed870 20 20 20 20 20 2f 2a 20 49 66 20 73 71 6c 69 74       /* If sqlit
ed880 65 33 4f 73 41 63 63 65 73 73 28 29 20 72 65 74  e3OsAccess() ret
ed890 75 72 6e 73 20 61 20 6e 65 67 61 74 69 76 65 20  urns a negative 
ed8a0 76 61 6c 75 65 2c 20 74 68 61 74 20 6d 65 61 6e  value, that mean
ed8b0 73 20 69 74 0a 20 20 20 20 20 20 20 20 20 20 20  s it.           
ed8c0 20 2a 2a 20 66 61 69 6c 65 64 20 61 20 6d 65 6d   ** failed a mem
ed8d0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a  ory allocation *
ed8e0 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  /.            rc
ed8f0 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
ed900 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20  NOMEM;.         
ed910 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
ed920 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
ed930 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
ed940 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
ed950 54 45 5f 4e 4f 4d 45 4d 20 26 26 20 72 63 21 3d  TE_NOMEM && rc!=
ed960 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c  SQLITE_IOERR_UNL
ed970 4f 43 4b 20 0a 20 20 20 20 20 20 20 20 20 20 20  OCK .           
ed980 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f  && rc!=SQLITE_IO
ed990 45 52 52 5f 4e 4f 4d 45 4d 20 0a 20 20 20 20 20  ERR_NOMEM .     
ed9a0 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
ed9b0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
ed9c0 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 20 20  BUSY;.          
ed9d0 7d 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  }.          goto
ed9e0 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20   failed;.       
ed9f0 20 7d 0a 20 20 20 20 20 20 20 20 70 50 61 67 65   }.        pPage
eda00 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 3d  r->journalOpen =
eda10 20 31 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67   1;.        pPag
eda20 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74  er->journalStart
eda30 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ed = 0;.        
eda40 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
eda50 66 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ff = 0;.        
eda60 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65  pPager->setMaste
eda70 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70  r = 0;.        p
eda80 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64  Pager->journalHd
eda90 72 20 3d 20 30 3b 0a 20 0a 20 20 20 20 20 20 20  r = 0;. .       
edaa0 20 2f 2a 20 50 6c 61 79 62 61 63 6b 20 61 6e 64   /* Playback and
edab0 20 64 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72   delete the jour
edac0 6e 61 6c 2e 20 20 44 72 6f 70 20 74 68 65 20 64  nal.  Drop the d
edad0 61 74 61 62 61 73 65 20 77 72 69 74 65 0a 20 20  atabase write.  
edae0 20 20 20 20 20 20 2a 2a 20 6c 6f 63 6b 20 61 6e        ** lock an
edaf0 64 20 72 65 61 63 71 75 69 72 65 20 74 68 65 20  d reacquire the 
edb00 72 65 61 64 20 6c 6f 63 6b 2e 0a 20 20 20 20 20  read lock..     
edb10 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63     */.        rc
edb20 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63   = pager_playbac
edb30 6b 28 70 50 61 67 65 72 2c 20 31 29 3b 0a 20 20  k(pPager, 1);.  
edb40 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
edb50 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
edb60 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f       rc = pager_
edb70 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63  error(pPager, rc
edb80 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  );.          got
edb90 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20  o failed;.      
edba0 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65    }.        asse
edbb0 72 74 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65  rt(pPager->state
edbc0 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 7c  ==PAGER_SHARED |
edbd0 7c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 28  | .            (
edbe0 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
edbf0 65 4d 6f 64 65 20 26 26 20 70 50 61 67 65 72 2d  eMode && pPager-
edc00 3e 73 74 61 74 65 3e 50 41 47 45 52 5f 53 48 41  >state>PAGER_SHA
edc10 52 45 44 29 0a 20 20 20 20 20 20 20 20 29 3b 0a  RED).        );.
edc20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69        }..      i
edc30 66 28 20 70 50 61 67 65 72 2d 3e 70 41 6c 6c 20  f( pPager->pAll 
edc40 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
edc50 65 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 68 61  e shared-lock ha
edc60 73 20 6a 75 73 74 20 62 65 65 6e 20 61 63 71 75  s just been acqu
edc70 69 72 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61  ired on the data
edc80 62 61 73 65 20 66 69 6c 65 0a 20 20 20 20 20 20  base file.      
edc90 20 20 2a 2a 20 61 6e 64 20 74 68 65 72 65 20 61    ** and there a
edca0 72 65 20 61 6c 72 65 61 64 79 20 70 61 67 65 73  re already pages
edcb0 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 28 66   in the cache (f
edcc0 72 6f 6d 20 61 20 70 72 65 76 69 6f 75 73 0a 20  rom a previous. 
edcd0 20 20 20 20 20 20 20 2a 2a 20 72 65 61 64 20 6f         ** read o
edce0 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74  r write transact
edcf0 69 6f 6e 29 2e 20 20 43 68 65 63 6b 20 74 6f 20  ion).  Check to 
edd00 73 65 65 20 69 66 20 74 68 65 20 64 61 74 61 62  see if the datab
edd10 61 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 68  ase.        ** h
edd20 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64  as been modified
edd30 2e 20 20 49 66 20 74 68 65 20 64 61 74 61 62 61  .  If the databa
edd40 73 65 20 68 61 73 20 63 68 61 6e 67 65 64 2c 20  se has changed, 
edd50 66 6c 75 73 68 20 74 68 65 0a 20 20 20 20 20 20  flush the.      
edd60 20 20 2a 2a 20 63 61 63 68 65 2e 0a 20 20 20 20    ** cache..    
edd70 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
edd80 2a 20 44 61 74 61 62 61 73 65 20 63 68 61 6e 67  * Database chang
edd90 65 73 20 69 73 20 64 65 74 65 63 74 65 64 20 62  es is detected b
edda0 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 31 35 20  y looking at 15 
eddb0 62 79 74 65 73 20 62 65 67 69 6e 6e 69 6e 67 0a  bytes beginning.
eddc0 20 20 20 20 20 20 20 20 2a 2a 20 61 74 20 6f 66          ** at of
eddd0 66 73 65 74 20 32 34 20 69 6e 74 6f 20 74 68 65  fset 24 into the
edde0 20 66 69 6c 65 2e 20 20 54 68 65 20 66 69 72 73   file.  The firs
eddf0 74 20 34 20 6f 66 20 74 68 65 73 65 20 31 36 20  t 4 of these 16 
ede00 62 79 74 65 73 20 61 72 65 0a 20 20 20 20 20 20  bytes are.      
ede10 20 20 2a 2a 20 61 20 33 32 2d 62 69 74 20 63 6f    ** a 32-bit co
ede20 75 6e 74 65 72 20 74 68 61 74 20 69 73 20 69 6e  unter that is in
ede30 63 72 65 6d 65 6e 74 65 64 20 77 69 74 68 20 65  cremented with e
ede40 61 63 68 20 63 68 61 6e 67 65 2e 20 20 54 68 65  ach change.  The
ede50 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 74 68 65  .        ** othe
ede60 72 20 62 79 74 65 73 20 63 68 61 6e 67 65 20 72  r bytes change r
ede70 61 6e 64 6f 6d 6c 79 20 77 69 74 68 20 65 61 63  andomly with eac
ede80 68 20 66 69 6c 65 20 63 68 61 6e 67 65 20 77 68  h file change wh
ede90 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20  en.        ** a 
edea0 63 6f 64 65 63 20 69 73 20 69 6e 20 75 73 65 2e  codec is in use.
edeb0 0a 20 20 20 20 20 20 20 20 2a 2a 20 0a 20 20 20  .        ** .   
edec0 20 20 20 20 20 2a 2a 20 54 68 65 72 65 20 69 73       ** There is
eded0 20 61 20 76 61 6e 69 73 68 69 6e 67 6c 79 20 73   a vanishingly s
edee0 6d 61 6c 6c 20 63 68 61 6e 63 65 20 74 68 61 74  mall chance that
edef0 20 61 20 63 68 61 6e 67 65 20 77 69 6c 6c 20 6e   a change will n
edf00 6f 74 20 62 65 20 0a 20 20 20 20 20 20 20 20 2a  ot be .        *
edf10 2a 20 64 65 74 65 63 74 65 64 2e 20 20 54 68 65  * detected.  The
edf20 20 63 68 61 6e 63 65 20 6f 66 20 61 6e 20 75 6e   chance of an un
edf30 64 65 74 65 63 74 65 64 20 63 68 61 6e 67 65 20  detected change 
edf40 69 73 20 73 6f 20 73 6d 61 6c 6c 20 74 68 61 74  is so small that
edf50 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20 63  .        ** it c
edf60 61 6e 20 62 65 20 6e 65 67 6c 65 63 74 65 64 2e  an be neglected.
edf70 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
edf80 20 20 20 20 63 68 61 72 20 64 62 46 69 6c 65 56      char dbFileV
edf90 65 72 73 5b 73 69 7a 65 6f 66 28 70 50 61 67 65  ers[sizeof(pPage
edfa0 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 5d 3b  r->dbFileVers)];
edfb0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
edfc0 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  PagerPagecount(p
edfd0 50 61 67 65 72 29 3b 0a 0a 20 20 20 20 20 20 20  Pager);..       
edfe0 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72   if( pPager->err
edff0 43 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Code ){.        
ee000 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65    rc = pPager->e
ee010 72 72 43 6f 64 65 3b 0a 20 20 20 20 20 20 20 20  rrCode;.        
ee020 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20    goto failed;. 
ee030 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
ee040 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62    if( pPager->db
ee050 53 69 7a 65 3e 30 20 29 7b 0a 20 20 20 20 20 20  Size>0 ){.      
ee060 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 43 4b      IOTRACE(("CK
ee070 56 45 52 53 20 25 70 20 25 64 5c 6e 22 2c 20 70  VERS %p %d\n", p
ee080 50 61 67 65 72 2c 20 73 69 7a 65 6f 66 28 64 62  Pager, sizeof(db
ee090 46 69 6c 65 56 65 72 73 29 29 29 3b 0a 20 20 20  FileVers)));.   
ee0a0 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
ee0b0 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72  te3OsRead(pPager
ee0c0 2d 3e 66 64 2c 20 26 64 62 46 69 6c 65 56 65 72  ->fd, &dbFileVer
ee0d0 73 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c 65  s, sizeof(dbFile
ee0e0 56 65 72 73 29 2c 20 32 34 29 3b 0a 20 20 20 20  Vers), 24);.    
ee0f0 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
ee100 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
ee110 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c         goto fail
ee120 65 64 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ed;.          }.
ee130 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
ee140 20 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28           memset(
ee150 64 62 46 69 6c 65 56 65 72 73 2c 20 30 2c 20 73  dbFileVers, 0, s
ee160 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73  izeof(dbFileVers
ee170 29 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20  ));.        }.. 
ee180 20 20 20 20 20 20 20 69 66 28 20 6d 65 6d 63 6d         if( memcm
ee190 70 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65  p(pPager->dbFile
ee1a0 56 65 72 73 2c 20 64 62 46 69 6c 65 56 65 72 73  Vers, dbFileVers
ee1b0 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56  , sizeof(dbFileV
ee1c0 65 72 73 29 29 21 3d 30 20 29 7b 0a 20 20 20 20  ers))!=0 ){.    
ee1d0 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 73 65        pager_rese
ee1e0 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20  t(pPager);.     
ee1f0 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
ee200 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
ee210 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65  Pager->exclusive
ee220 4d 6f 64 65 20 7c 7c 20 70 50 61 67 65 72 2d 3e  Mode || pPager->
ee230 73 74 61 74 65 3c 3d 50 41 47 45 52 5f 53 48 41  state<=PAGER_SHA
ee240 52 45 44 20 29 3b 0a 20 20 20 20 69 66 28 20 70  RED );.    if( p
ee250 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41  Pager->state==PA
ee260 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 7b 0a 20 20  GER_UNLOCK ){.  
ee270 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74      pPager->stat
ee280 65 20 3d 20 50 41 47 45 52 5f 53 48 41 52 45 44  e = PAGER_SHARED
ee290 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 66 61  ;.    }.  }.. fa
ee2a0 69 6c 65 64 3a 0a 20 20 69 66 28 20 72 63 21 3d  iled:.  if( rc!=
ee2b0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
ee2c0 20 2f 2a 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b   /* pager_unlock
ee2d0 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f  () is a no-op fo
ee2e0 72 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65  r exclusive mode
ee2f0 20 61 6e 64 20 69 6e 2d 6d 65 6d 6f 72 79 20 64   and in-memory d
ee300 61 74 61 62 61 73 65 73 2e 20 2a 2f 0a 20 20 20  atabases. */.   
ee310 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50   pager_unlock(pP
ee320 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ager);.  }.  ret
ee330 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
ee340 20 41 6c 6c 6f 63 61 74 65 20 61 20 50 67 48 64   Allocate a PgHd
ee350 72 20 6f 62 6a 65 63 74 2e 20 20 20 45 69 74 68  r object.   Eith
ee360 65 72 20 63 72 65 61 74 65 20 61 20 6e 65 77 20  er create a new 
ee370 6f 6e 65 20 6f 72 20 72 65 75 73 65 0a 2a 2a 20  one or reuse.** 
ee380 61 6e 20 65 78 69 73 74 69 6e 67 20 6f 6e 65 20  an existing one 
ee390 74 68 61 74 20 69 73 20 6e 6f 74 20 6f 74 68 65  that is not othe
ee3a0 72 77 69 73 65 20 69 6e 20 75 73 65 2e 0a 2a 2a  rwise in use..**
ee3b0 0a 2a 2a 20 41 20 6e 65 77 20 50 67 48 64 72 20  .** A new PgHdr 
ee3c0 73 74 72 75 63 74 75 72 65 20 69 73 20 63 72 65  structure is cre
ee3d0 61 74 65 64 20 69 66 20 61 6e 79 20 6f 66 20 74  ated if any of t
ee3e0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65  he following are
ee3f0 0a 2a 2a 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20  .** true:.**.** 
ee400 20 20 20 20 28 31 29 20 20 57 65 20 68 61 76 65      (1)  We have
ee410 20 6e 6f 74 20 65 78 63 65 65 64 65 64 20 6f 75   not exceeded ou
ee420 72 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 63 61  r maximum alloca
ee430 74 65 64 20 63 61 63 68 65 20 73 69 7a 65 0a 2a  ted cache size.*
ee440 2a 20 20 20 20 20 20 20 20 20 20 61 73 20 73 65  *          as se
ee450 74 20 62 79 20 74 68 65 20 22 50 52 41 47 4d 41  t by the "PRAGMA
ee460 20 63 61 63 68 65 5f 73 69 7a 65 22 20 63 6f 6d   cache_size" com
ee470 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  mand..**.**     
ee480 28 32 29 20 20 54 68 65 72 65 20 61 72 65 20 6e  (2)  There are n
ee490 6f 20 75 6e 75 73 65 64 20 50 67 48 64 72 20 6f  o unused PgHdr o
ee4a0 62 6a 65 63 74 73 20 61 76 61 69 6c 61 62 6c 65  bjects available
ee4b0 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 0a 2a   at this time..*
ee4c0 2a 0a 2a 2a 20 20 20 20 20 28 33 29 20 20 54 68  *.**     (3)  Th
ee4d0 69 73 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f  is is an in-memo
ee4e0 72 79 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a  ry database..**.
ee4f0 2a 2a 20 20 20 20 20 28 34 29 20 20 54 68 65 72  **     (4)  Ther
ee500 65 20 61 72 65 20 6e 6f 20 50 67 48 64 72 20 6f  e are no PgHdr o
ee510 62 6a 65 63 74 73 20 74 68 61 74 20 64 6f 20 6e  bjects that do n
ee520 6f 74 20 72 65 71 75 69 72 65 20 61 20 6a 6f 75  ot require a jou
ee530 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20  rnal.**         
ee540 20 66 69 6c 65 20 73 79 6e 63 20 61 6e 64 20 61   file sync and a
ee550 20 73 79 6e 63 20 6f 66 20 74 68 65 20 6a 6f 75   sync of the jou
ee560 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 63 75 72  rnal file is cur
ee570 72 65 6e 74 6c 79 0a 2a 2a 20 20 20 20 20 20 20  rently.**       
ee580 20 20 20 70 72 6f 68 69 62 69 74 65 64 2e 0a 2a     prohibited..*
ee590 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20  *.** Otherwise, 
ee5a0 72 65 75 73 65 20 61 6e 20 65 78 69 73 74 69 6e  reuse an existin
ee5b0 67 20 50 67 48 64 72 2e 20 20 49 6e 20 6f 74 68  g PgHdr.  In oth
ee5c0 65 72 20 77 6f 72 64 73 2c 20 72 65 75 73 65 20  er words, reuse 
ee5d0 61 6e 0a 2a 2a 20 65 78 69 73 74 69 6e 67 20 50  an.** existing P
ee5e0 67 48 64 72 20 69 66 20 61 6c 6c 20 6f 66 20 74  gHdr if all of t
ee5f0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65  he following are
ee600 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   true:.**.**    
ee610 20 28 31 29 20 20 57 65 20 68 61 76 65 20 72 65   (1)  We have re
ee620 61 63 68 65 64 20 6f 72 20 65 78 63 65 65 64 65  ached or exceede
ee630 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 63 61  d the maximum ca
ee640 63 68 65 20 73 69 7a 65 0a 2a 2a 20 20 20 20 20  che size.**     
ee650 20 20 20 20 20 61 6c 6c 6f 77 65 64 20 62 79 20       allowed by 
ee660 22 50 52 41 47 4d 41 20 63 61 63 68 65 5f 73 69  "PRAGMA cache_si
ee670 7a 65 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28  ze"..**.**     (
ee680 32 29 20 20 54 68 65 72 65 20 69 73 20 61 20 50  2)  There is a P
ee690 67 48 64 72 20 61 76 61 69 6c 61 62 6c 65 20 77  gHdr available w
ee6a0 69 74 68 20 50 67 48 64 72 2d 3e 6e 52 65 66 3d  ith PgHdr->nRef=
ee6b0 3d 30 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 33 29  =0.**.**     (3)
ee6c0 20 20 57 65 20 61 72 65 20 6e 6f 74 20 69 6e 20    We are not in 
ee6d0 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  an in-memory dat
ee6e0 61 62 61 73 65 0a 2a 2a 0a 2a 2a 20 20 20 20 20  abase.**.**     
ee6f0 28 34 29 20 20 45 69 74 68 65 72 20 74 68 65 72  (4)  Either ther
ee700 65 20 69 73 20 61 6e 20 61 76 61 69 6c 61 62 6c  e is an availabl
ee710 65 20 50 67 48 64 72 20 74 68 61 74 20 64 6f 65  e PgHdr that doe
ee720 73 20 6e 6f 74 20 6e 65 65 64 0a 2a 2a 20 20 20  s not need.**   
ee730 20 20 20 20 20 20 20 74 6f 20 62 65 20 73 79 6e         to be syn
ee740 63 65 64 20 74 6f 20 64 69 73 6b 20 6f 72 20 65  ced to disk or e
ee750 6c 73 65 20 64 69 73 6b 20 73 79 6e 63 69 6e 67  lse disk syncing
ee760 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a   is currently.**
ee770 20 20 20 20 20 20 20 20 20 20 61 6c 6c 6f 77 65            allowe
ee780 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
ee790 20 70 61 67 65 72 41 6c 6c 6f 63 61 74 65 50 61   pagerAllocatePa
ee7a0 67 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ge(Pager *pPager
ee7b0 2c 20 50 67 48 64 72 20 2a 2a 70 70 50 67 29 7b  , PgHdr **ppPg){
ee7c0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
ee7d0 54 45 5f 4f 4b 3b 0a 20 20 50 67 48 64 72 20 2a  TE_OK;.  PgHdr *
ee7e0 70 50 67 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65  pPg;.  int nByte
ee7f0 48 64 72 3b 0a 0a 20 20 2f 2a 20 43 72 65 61 74  Hdr;..  /* Creat
ee800 65 20 61 20 6e 65 77 20 50 67 48 64 72 20 69 66  e a new PgHdr if
ee810 20 61 6e 79 20 6f 66 20 74 68 65 20 66 6f 75 72   any of the four
ee820 20 63 6f 6e 64 69 74 69 6f 6e 73 20 64 65 66 69   conditions defi
ee830 6e 65 64 20 0a 20 20 2a 2a 20 61 62 6f 76 65 20  ned .  ** above 
ee840 61 72 65 20 6d 65 74 3a 20 2a 2f 0a 20 20 69 66  are met: */.  if
ee850 28 20 70 50 61 67 65 72 2d 3e 6e 50 61 67 65 3c  ( pPager->nPage<
ee860 70 50 61 67 65 72 2d 3e 6d 78 50 61 67 65 0a 20  pPager->mxPage. 
ee870 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6c 72 75    || pPager->lru
ee880 2e 70 46 69 72 73 74 3d 3d 30 20 0a 20 20 20 7c  .pFirst==0 .   |
ee890 7c 20 4d 45 4d 44 42 0a 20 20 20 7c 7c 20 28 70  | MEMDB.   || (p
ee8a0 50 61 67 65 72 2d 3e 6c 72 75 2e 70 46 69 72 73  Pager->lru.pFirs
ee8b0 74 53 79 6e 63 65 64 3d 3d 30 20 26 26 20 70 50  tSynced==0 && pP
ee8c0 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 29  ager->doNotSync)
ee8d0 0a 20 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a  .  ){.    void *
ee8e0 70 44 61 74 61 3b 0a 20 20 20 20 69 66 28 20 70  pData;.    if( p
ee8f0 50 61 67 65 72 2d 3e 6e 50 61 67 65 3e 3d 70 50  Pager->nPage>=pP
ee900 61 67 65 72 2d 3e 6e 48 61 73 68 20 29 7b 0a 20  ager->nHash ){. 
ee910 20 20 20 20 20 70 61 67 65 72 5f 72 65 73 69 7a       pager_resiz
ee920 65 5f 68 61 73 68 5f 74 61 62 6c 65 28 70 50 61  e_hash_table(pPa
ee930 67 65 72 2c 0a 20 20 20 20 20 20 20 20 20 70 50  ger,.         pP
ee940 61 67 65 72 2d 3e 6e 48 61 73 68 3c 32 35 36 20  ager->nHash<256 
ee950 3f 20 32 35 36 20 3a 20 70 50 61 67 65 72 2d 3e  ? 256 : pPager->
ee960 6e 48 61 73 68 2a 32 29 3b 0a 20 20 20 20 20 20  nHash*2);.      
ee970 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 48 61 73  if( pPager->nHas
ee980 68 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  h==0 ){.        
ee990 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
ee9a0 4d 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  M;.        goto 
ee9b0 70 61 67 65 72 5f 61 6c 6c 6f 63 61 74 65 5f 6f  pager_allocate_o
ee9c0 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ut;.      }.    
ee9d0 7d 0a 20 20 20 20 70 61 67 65 72 4c 65 61 76 65  }.    pagerLeave
ee9e0 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 6e 42  (pPager);.    nB
ee9f0 79 74 65 48 64 72 20 3d 20 73 69 7a 65 6f 66 28  yteHdr = sizeof(
eea00 2a 70 50 67 29 20 2b 20 73 69 7a 65 6f 66 28 75  *pPg) + sizeof(u
eea10 33 32 29 20 2b 20 70 50 61 67 65 72 2d 3e 6e 45  32) + pPager->nE
eea20 78 74 72 61 0a 20 20 20 20 20 20 20 20 20 20 20  xtra.           
eea30 20 20 20 2b 20 4d 45 4d 44 42 2a 73 69 7a 65 6f     + MEMDB*sizeo
eea40 66 28 50 67 48 69 73 74 6f 72 79 29 3b 0a 20 20  f(PgHistory);.  
eea50 20 20 70 50 67 20 3d 20 73 71 6c 69 74 65 33 5f    pPg = sqlite3_
eea60 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 48 64 72  malloc( nByteHdr
eea70 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50 67 20   );.    if( pPg 
eea80 29 7b 0a 20 20 20 20 20 20 70 44 61 74 61 20 3d  ){.      pData =
eea90 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
eeaa0 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
eeab0 65 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  e );.      if( p
eeac0 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Data==0 ){.     
eead0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
eeae0 70 50 67 29 3b 0a 20 20 20 20 20 20 20 20 70 50  pPg);.        pP
eeaf0 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  g = 0;.      }. 
eeb00 20 20 20 7d 0a 20 20 20 20 70 61 67 65 72 45 6e     }.    pagerEn
eeb10 74 65 72 28 70 50 61 67 65 72 29 3b 0a 20 20 20  ter(pPager);.   
eeb20 20 69 66 28 20 70 50 67 3d 3d 30 20 29 7b 0a 20   if( pPg==0 ){. 
eeb30 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
eeb40 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f  _NOMEM;.      go
eeb50 74 6f 20 70 61 67 65 72 5f 61 6c 6c 6f 63 61 74  to pager_allocat
eeb60 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20  e_out;.    }.   
eeb70 20 6d 65 6d 73 65 74 28 70 50 67 2c 20 30 2c 20   memset(pPg, 0, 
eeb80 6e 42 79 74 65 48 64 72 29 3b 0a 20 20 20 20 70  nByteHdr);.    p
eeb90 50 67 2d 3e 70 44 61 74 61 20 3d 20 70 44 61 74  Pg->pData = pDat
eeba0 61 3b 0a 20 20 20 20 70 50 67 2d 3e 70 50 61 67  a;.    pPg->pPag
eebb0 65 72 20 3d 20 70 50 61 67 65 72 3b 0a 20 20 20  er = pPager;.   
eebc0 20 70 50 67 2d 3e 70 4e 65 78 74 41 6c 6c 20 3d   pPg->pNextAll =
eebd0 20 70 50 61 67 65 72 2d 3e 70 41 6c 6c 3b 0a 20   pPager->pAll;. 
eebe0 20 20 20 70 50 61 67 65 72 2d 3e 70 41 6c 6c 20     pPager->pAll 
eebf0 3d 20 70 50 67 3b 0a 20 20 20 20 70 50 61 67 65  = pPg;.    pPage
eec00 72 2d 3e 6e 50 61 67 65 2b 2b 3b 0a 20 20 7d 65  r->nPage++;.  }e
eec10 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 52 65 63 79  lse{.    /* Recy
eec20 63 6c 65 20 61 6e 20 65 78 69 73 74 69 6e 67 20  cle an existing 
eec30 70 61 67 65 20 77 69 74 68 20 61 20 7a 65 72 6f  page with a zero
eec40 20 72 65 66 2d 63 6f 75 6e 74 2e 20 2a 2f 0a 20   ref-count. */. 
eec50 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 72 65     rc = pager_re
eec60 63 79 63 6c 65 28 70 50 61 67 65 72 2c 20 26 70  cycle(pPager, &p
eec70 50 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  Pg);.    if( rc=
eec80 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a  =SQLITE_BUSY ){.
eec90 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
eeca0 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 3b  E_IOERR_BLOCKED;
eecb0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
eecc0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
eecd0 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 72        goto pager
eece0 5f 61 6c 6c 6f 63 61 74 65 5f 6f 75 74 3b 0a 20  _allocate_out;. 
eecf0 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
eed00 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d   pPager->state>=
eed10 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20  SHARED_LOCK );. 
eed20 20 20 20 61 73 73 65 72 74 28 70 50 67 29 3b 0a     assert(pPg);.
eed30 20 20 7d 0a 20 20 2a 70 70 50 67 20 3d 20 70 50    }.  *ppPg = pP
eed40 67 3b 0a 0a 70 61 67 65 72 5f 61 6c 6c 6f 63 61  g;..pager_alloca
eed50 74 65 5f 6f 75 74 3a 0a 20 20 72 65 74 75 72 6e  te_out:.  return
eed60 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61   rc;.}../*.** Ma
eed70 6b 65 20 73 75 72 65 20 77 65 20 68 61 76 65 20  ke sure we have 
eed80 74 68 65 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20  the content for 
eed90 61 20 70 61 67 65 2e 20 20 49 66 20 74 68 65 20  a page.  If the 
eeda0 70 61 67 65 20 77 61 73 0a 2a 2a 20 70 72 65 76  page was.** prev
eedb0 69 6f 75 73 6c 79 20 61 63 71 75 69 72 65 64 20  iously acquired 
eedc0 77 69 74 68 20 6e 6f 43 6f 6e 74 65 6e 74 3d 3d  with noContent==
eedd0 31 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 74  1, then the cont
eede0 65 6e 74 20 77 61 73 0a 2a 2a 20 6a 75 73 74 20  ent was.** just 
eedf0 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a  initialized to z
eee00 65 72 6f 73 20 69 6e 73 74 65 61 64 20 6f 66 20  eros instead of 
eee10 62 65 69 6e 67 20 72 65 61 64 20 66 72 6f 6d 20  being read from 
eee20 64 69 73 6b 2e 0a 2a 2a 20 42 75 74 20 6e 6f 77  disk..** But now
eee30 20 77 65 20 6e 65 65 64 20 74 68 65 20 72 65 61   we need the rea
eee40 6c 20 64 61 74 61 20 6f 66 66 20 6f 66 20 64 69  l data off of di
eee50 73 6b 2e 20 20 53 6f 20 6d 61 6b 65 20 73 75 72  sk.  So make sur
eee60 65 20 77 65 0a 2a 2a 20 68 61 76 65 20 69 74 2e  e we.** have it.
eee70 20 20 52 65 61 64 20 69 74 20 69 6e 20 69 66 20    Read it in if 
eee80 77 65 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 69  we do not have i
eee90 74 20 61 6c 72 65 61 64 79 2e 0a 2a 2f 0a 73 74  t already..*/.st
eeea0 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 67  atic int pager_g
eeeb0 65 74 5f 63 6f 6e 74 65 6e 74 28 50 67 48 64 72  et_content(PgHdr
eeec0 20 2a 70 50 67 29 7b 0a 20 20 69 66 28 20 70 50   *pPg){.  if( pP
eeed0 67 2d 3e 6e 65 65 64 52 65 61 64 20 29 7b 0a 20  g->needRead ){. 
eeee0 20 20 20 69 6e 74 20 72 63 20 3d 20 72 65 61 64     int rc = read
eeef0 44 62 50 61 67 65 28 70 50 67 2d 3e 70 50 61 67  DbPage(pPg->pPag
eef00 65 72 2c 20 70 50 67 2c 20 70 50 67 2d 3e 70 67  er, pPg, pPg->pg
eef10 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  no);.    if( rc=
eef20 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
eef30 20 20 20 20 70 50 67 2d 3e 6e 65 65 64 52 65 61      pPg->needRea
eef40 64 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65  d = 0;.    }else
eef50 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  {.      return r
eef60 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  c;.    }.  }.  r
eef70 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
eef80 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72  .}../*.** Acquir
eef90 65 20 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20  e a page..**.** 
eefa0 41 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74  A read lock on t
eefb0 68 65 20 64 69 73 6b 20 66 69 6c 65 20 69 73 20  he disk file is 
eefc0 6f 62 74 61 69 6e 65 64 20 77 68 65 6e 20 74 68  obtained when th
eefd0 65 20 66 69 72 73 74 20 70 61 67 65 20 69 73 20  e first page is 
eefe0 61 63 71 75 69 72 65 64 2e 20 0a 2a 2a 20 54 68  acquired. .** Th
eeff0 69 73 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 20  is read lock is 
ef000 64 72 6f 70 70 65 64 20 77 68 65 6e 20 74 68 65  dropped when the
ef010 20 6c 61 73 74 20 70 61 67 65 20 69 73 20 72 65   last page is re
ef020 6c 65 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  leased..**.** Th
ef030 69 73 20 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73  is routine works
ef040 20 66 6f 72 20 61 6e 79 20 70 61 67 65 20 6e 75   for any page nu
ef050 6d 62 65 72 20 67 72 65 61 74 65 72 20 74 68 61  mber greater tha
ef060 6e 20 30 2e 20 20 49 66 20 74 68 65 20 64 61 74  n 0.  If the dat
ef070 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69 73  abase.** file is
ef080 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68   smaller than th
ef090 65 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65  e requested page
ef0a0 2c 20 74 68 65 6e 20 6e 6f 20 61 63 74 75 61 6c  , then no actual
ef0b0 20 64 69 73 6b 0a 2a 2a 20 72 65 61 64 20 6f 63   disk.** read oc
ef0c0 63 75 72 73 20 61 6e 64 20 74 68 65 20 6d 65 6d  curs and the mem
ef0d0 6f 72 79 20 69 6d 61 67 65 20 6f 66 20 74 68 65  ory image of the
ef0e0 20 70 61 67 65 20 69 73 20 69 6e 69 74 69 61 6c   page is initial
ef0f0 69 7a 65 64 20 74 6f 0a 2a 2a 20 61 6c 6c 20 7a  ized to.** all z
ef100 65 72 6f 73 2e 20 20 54 68 65 20 65 78 74 72 61  eros.  The extra
ef110 20 64 61 74 61 20 61 70 70 65 6e 64 65 64 20 74   data appended t
ef120 6f 20 61 20 70 61 67 65 20 69 73 20 61 6c 77 61  o a page is alwa
ef130 79 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 0a 2a  ys initialized.*
ef140 2a 20 74 6f 20 7a 65 72 6f 73 20 74 68 65 20 66  * to zeros the f
ef150 69 72 73 74 20 74 69 6d 65 20 61 20 70 61 67 65  irst time a page
ef160 20 69 73 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20   is loaded into 
ef170 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  memory..**.** Th
ef180 65 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6d 69  e acquisition mi
ef190 67 68 74 20 66 61 69 6c 20 66 6f 72 20 73 65 76  ght fail for sev
ef1a0 65 72 61 6c 20 72 65 61 73 6f 6e 73 2e 20 20 49  eral reasons.  I
ef1b0 6e 20 61 6c 6c 20 63 61 73 65 73 2c 0a 2a 2a 20  n all cases,.** 
ef1c0 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 65  an appropriate e
ef1d0 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
ef1e0 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 50 61 67  urned and *ppPag
ef1f0 65 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c  e is set to NULL
ef200 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
ef210 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f   sqlite3PagerLoo
ef220 6b 75 70 28 29 2e 20 20 42 6f 74 68 20 74 68 69  kup().  Both thi
ef230 73 20 72 6f 75 74 69 6e 65 20 61 6e 64 20 4c 6f  s routine and Lo
ef240 6f 6b 75 70 28 29 20 61 74 74 65 6d 70 74 0a 2a  okup() attempt.*
ef250 2a 20 74 6f 20 66 69 6e 64 20 61 20 70 61 67 65  * to find a page
ef260 20 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72   in the in-memor
ef270 79 20 63 61 63 68 65 20 66 69 72 73 74 2e 20 20  y cache first.  
ef280 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e  If the page is n
ef290 6f 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e  ot already.** in
ef2a0 20 6d 65 6d 6f 72 79 2c 20 74 68 69 73 20 72 6f   memory, this ro
ef2b0 75 74 69 6e 65 20 67 6f 65 73 20 74 6f 20 64 69  utine goes to di
ef2c0 73 6b 20 74 6f 20 72 65 61 64 20 69 74 20 69 6e  sk to read it in
ef2d0 20 77 68 65 72 65 61 73 20 4c 6f 6f 6b 75 70 28   whereas Lookup(
ef2e0 29 0a 2a 2a 20 6a 75 73 74 20 72 65 74 75 72 6e  ).** just return
ef2f0 73 20 30 2e 20 20 54 68 69 73 20 72 6f 75 74 69  s 0.  This routi
ef300 6e 65 20 61 63 71 75 69 72 65 73 20 61 20 72 65  ne acquires a re
ef310 61 64 2d 6c 6f 63 6b 20 74 68 65 20 66 69 72 73  ad-lock the firs
ef320 74 20 74 69 6d 65 20 69 74 0a 2a 2a 20 68 61 73  t time it.** has
ef330 20 74 6f 20 67 6f 20 74 6f 20 64 69 73 6b 2c 20   to go to disk, 
ef340 61 6e 64 20 63 6f 75 6c 64 20 61 6c 73 6f 20 70  and could also p
ef350 6c 61 79 62 61 63 6b 20 61 6e 20 6f 6c 64 20 6a  layback an old j
ef360 6f 75 72 6e 61 6c 20 69 66 20 6e 65 63 65 73 73  ournal if necess
ef370 61 72 79 2e 0a 2a 2a 20 53 69 6e 63 65 20 4c 6f  ary..** Since Lo
ef380 6f 6b 75 70 28 29 20 6e 65 76 65 72 20 67 6f 65  okup() never goe
ef390 73 20 74 6f 20 64 69 73 6b 2c 20 69 74 20 6e 65  s to disk, it ne
ef3a0 76 65 72 20 68 61 73 20 74 6f 20 64 65 61 6c 20  ver has to deal 
ef3b0 77 69 74 68 20 6c 6f 63 6b 73 0a 2a 2a 20 6f 72  with locks.** or
ef3c0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a   journal files..
ef3d0 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65  **.** If noConte
ef3e0 6e 74 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65  nt is false, the
ef3f0 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 73 20 61   page contents a
ef400 72 65 20 61 63 74 75 61 6c 6c 79 20 72 65 61 64  re actually read
ef410 20 66 72 6f 6d 20 64 69 73 6b 2e 0a 2a 2a 20 49   from disk..** I
ef420 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 20 74  f noContent is t
ef430 72 75 65 2c 20 69 74 20 6d 65 61 6e 73 20 74 68  rue, it means th
ef440 61 74 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72  at we do not car
ef450 65 20 61 62 6f 75 74 20 74 68 65 20 63 6f 6e 74  e about the cont
ef460 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70  ents.** of the p
ef470 61 67 65 20 61 74 20 74 68 69 73 20 74 69 6d 65  age at this time
ef480 2c 20 73 6f 20 64 6f 20 6e 6f 74 20 64 6f 20 61  , so do not do a
ef490 20 64 69 73 6b 20 72 65 61 64 2e 20 20 4a 75 73   disk read.  Jus
ef4a0 74 20 66 69 6c 6c 20 69 6e 20 74 68 65 0a 2a 2a  t fill in the.**
ef4b0 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 77 69   page content wi
ef4c0 74 68 20 7a 65 72 6f 73 2e 20 20 42 75 74 20 6d  th zeros.  But m
ef4d0 61 72 6b 20 74 68 65 20 66 61 63 74 20 74 68 61  ark the fact tha
ef4e0 74 20 77 65 20 68 61 76 65 20 6e 6f 74 20 72 65  t we have not re
ef4f0 61 64 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e  ad the.** conten
ef500 74 20 62 79 20 73 65 74 74 69 6e 67 20 74 68 65  t by setting the
ef510 20 50 67 48 64 72 2e 6e 65 65 64 52 65 61 64 20   PgHdr.needRead 
ef520 66 6c 61 67 2e 20 20 4c 61 74 65 72 20 6f 6e 2c  flag.  Later on,
ef530 20 69 66 20 0a 2a 2a 20 73 71 6c 69 74 65 33 50   if .** sqlite3P
ef540 61 67 65 72 57 72 69 74 65 28 29 20 69 73 20 63  agerWrite() is c
ef550 61 6c 6c 65 64 20 6f 6e 20 74 68 69 73 20 70 61  alled on this pa
ef560 67 65 20 6f 72 20 69 66 20 74 68 69 73 20 72 6f  ge or if this ro
ef570 75 74 69 6e 65 20 69 73 0a 2a 2a 20 63 61 6c 6c  utine is.** call
ef580 65 64 20 61 67 61 69 6e 20 77 69 74 68 20 6e 6f  ed again with no
ef590 43 6f 6e 74 65 6e 74 3d 3d 30 2c 20 74 68 61 74  Content==0, that
ef5a0 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
ef5b0 63 6f 6e 74 65 6e 74 20 69 73 20 6e 65 65 64 65  content is neede
ef5c0 64 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 69 73  d.** and the dis
ef5d0 6b 20 72 65 61 64 20 73 68 6f 75 6c 64 20 6f 63  k read should oc
ef5e0 63 75 72 20 61 74 20 74 68 61 74 20 70 6f 69 6e  cur at that poin
ef5f0 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
ef600 20 70 61 67 65 72 41 63 71 75 69 72 65 28 0a 20   pagerAcquire(. 
ef610 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20   Pager *pPager, 
ef620 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65       /* The page
ef630 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 64 61  r open on the da
ef640 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20  tabase file */. 
ef650 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20   Pgno pgno,     
ef660 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d       /* Page num
ef670 62 65 72 20 74 6f 20 66 65 74 63 68 20 2a 2f 0a  ber to fetch */.
ef680 20 20 44 62 50 61 67 65 20 2a 2a 70 70 50 61 67    DbPage **ppPag
ef690 65 2c 20 20 20 20 2f 2a 20 57 72 69 74 65 20 61  e,    /* Write a
ef6a0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
ef6b0 70 61 67 65 20 68 65 72 65 20 2a 2f 0a 20 20 69  page here */.  i
ef6c0 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 20 20 20 20  nt noContent    
ef6d0 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 62 6f 74     /* Do not bot
ef6e0 68 65 72 20 72 65 61 64 69 6e 67 20 63 6f 6e 74  her reading cont
ef6f0 65 6e 74 20 66 72 6f 6d 20 64 69 73 6b 20 69 66  ent from disk if
ef700 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 50 67   true */.){.  Pg
ef710 48 64 72 20 2a 70 50 67 3b 0a 20 20 69 6e 74 20  Hdr *pPg;.  int 
ef720 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  rc;..  assert( p
ef730 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41  Pager->state==PA
ef740 47 45 52 5f 55 4e 4c 4f 43 4b 20 7c 7c 20 70 50  GER_UNLOCK || pP
ef750 61 67 65 72 2d 3e 6e 52 65 66 3e 30 20 7c 7c 20  ager->nRef>0 || 
ef760 70 67 6e 6f 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a  pgno==1 );..  /*
ef770 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67   The maximum pag
ef780 65 20 6e 75 6d 62 65 72 20 69 73 20 32 5e 33 31  e number is 2^31
ef790 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  . Return SQLITE_
ef7a0 43 4f 52 52 55 50 54 20 69 66 20 61 20 70 61 67  CORRUPT if a pag
ef7b0 65 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 67 72  e.  ** number gr
ef7c0 65 61 74 65 72 20 74 68 61 6e 20 74 68 69 73 2c  eater than this,
ef7d0 20 6f 72 20 7a 65 72 6f 2c 20 69 73 20 72 65 71   or zero, is req
ef7e0 75 65 73 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  uested..  */.  i
ef7f0 66 28 20 70 67 6e 6f 3e 50 41 47 45 52 5f 4d 41  f( pgno>PAGER_MA
ef800 58 5f 50 47 4e 4f 20 7c 7c 20 70 67 6e 6f 3d 3d  X_PGNO || pgno==
ef810 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52  0 || pgno==PAGER
ef820 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29  _MJ_PGNO(pPager)
ef830 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
ef840 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
ef850 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61  PT;.  }..  /* Ma
ef860 6b 65 20 73 75 72 65 20 77 65 20 68 61 76 65 20  ke sure we have 
ef870 6e 6f 74 20 68 69 74 20 61 6e 79 20 63 72 69 74  not hit any crit
ef880 69 63 61 6c 20 65 72 72 6f 72 73 2e 0a 20 20 2a  ical errors..  *
ef890 2f 20 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  / .  assert( pPa
ef8a0 67 65 72 21 3d 30 20 29 3b 0a 20 20 2a 70 70 50  ger!=0 );.  *ppP
ef8b0 61 67 65 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 49  age = 0;..  /* I
ef8c0 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69  f this is the fi
ef8d0 72 73 74 20 70 61 67 65 20 61 63 63 65 73 73 65  rst page accesse
ef8e0 64 2c 20 74 68 65 6e 20 67 65 74 20 61 20 53 48  d, then get a SH
ef8f0 41 52 45 44 20 6c 6f 63 6b 0a 20 20 2a 2a 20 6f  ARED lock.  ** o
ef900 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
ef910 69 6c 65 2e 20 70 61 67 65 72 53 68 61 72 65 64  ile. pagerShared
ef920 4c 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f 2d 6f  Lock() is a no-o
ef930 70 20 69 66 20 0a 20 20 2a 2a 20 61 20 64 61 74  p if .  ** a dat
ef940 61 62 61 73 65 20 6c 6f 63 6b 20 69 73 20 61 6c  abase lock is al
ef950 72 65 61 64 79 20 68 65 6c 64 2e 0a 20 20 2a 2f  ready held..  */
ef960 0a 20 20 72 63 20 3d 20 70 61 67 65 72 53 68 61  .  rc = pagerSha
ef970 72 65 64 4c 6f 63 6b 28 70 50 61 67 65 72 29 3b  redLock(pPager);
ef980 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
ef990 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
ef9a0 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73  rn rc;.  }.  ass
ef9b0 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61  ert( pPager->sta
ef9c0 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b  te!=PAGER_UNLOCK
ef9d0 20 29 3b 0a 0a 20 20 70 50 67 20 3d 20 70 61 67   );..  pPg = pag
ef9e0 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72  er_lookup(pPager
ef9f0 2c 20 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70  , pgno);.  if( p
efa00 50 67 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  Pg==0 ){.    /* 
efa10 54 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61  The requested pa
efa20 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 74 68 65  ge is not in the
efa30 20 70 61 67 65 20 63 61 63 68 65 2e 20 2a 2f 0a   page cache. */.
efa40 20 20 20 20 69 6e 74 20 6e 4d 61 78 3b 0a 20 20      int nMax;.  
efa50 20 20 69 6e 74 20 68 3b 0a 20 20 20 20 50 41 47    int h;.    PAG
efa60 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e  ER_INCR(pPager->
efa70 6e 4d 69 73 73 29 3b 0a 20 20 20 20 72 63 20 3d  nMiss);.    rc =
efa80 20 70 61 67 65 72 41 6c 6c 6f 63 61 74 65 50 61   pagerAllocatePa
efa90 67 65 28 70 50 61 67 65 72 2c 20 26 70 50 67 29  ge(pPager, &pPg)
efaa0 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
efab0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
efac0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
efad0 7d 0a 0a 20 20 20 20 70 50 67 2d 3e 70 67 6e 6f  }..    pPg->pgno
efae0 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 61 73 73   = pgno;.    ass
efaf0 65 72 74 28 20 21 4d 45 4d 44 42 20 7c 7c 20 70  ert( !MEMDB || p
efb00 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 73 74 6d 74  gno>pPager->stmt
efb10 53 69 7a 65 20 29 3b 0a 20 20 20 20 70 50 67 2d  Size );.    pPg-
efb20 3e 69 6e 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c  >inJournal = sql
efb30 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70  ite3BitvecTest(p
efb40 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
efb50 6c 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 70 50  l, pgno);.    pP
efb60 67 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b  g->needSync = 0;
efb70 0a 0a 20 20 20 20 6d 61 6b 65 43 6c 65 61 6e 28  ..    makeClean(
efb80 70 50 67 29 3b 0a 20 20 20 20 70 50 67 2d 3e 6e  pPg);.    pPg->n
efb90 52 65 66 20 3d 20 31 3b 0a 0a 20 20 20 20 70 50  Ref = 1;..    pP
efba0 61 67 65 72 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20  ager->nRef++;.  
efbb0 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 45    if( pPager->nE
efbc0 78 74 72 61 3e 30 20 29 7b 0a 20 20 20 20 20 20  xtra>0 ){.      
efbd0 6d 65 6d 73 65 74 28 50 47 48 44 52 5f 54 4f 5f  memset(PGHDR_TO_
efbe0 45 58 54 52 41 28 70 50 67 2c 20 70 50 61 67 65  EXTRA(pPg, pPage
efbf0 72 29 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e 6e  r), 0, pPager->n
efc00 45 78 74 72 61 29 3b 0a 20 20 20 20 7d 0a 20 20  Extra);.    }.  
efc10 20 20 6e 4d 61 78 20 3d 20 73 71 6c 69 74 65 33    nMax = sqlite3
efc20 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  PagerPagecount(p
efc30 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20  Pager);.    if( 
efc40 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20  pPager->errCode 
efc50 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 50  ){.      rc = pP
efc60 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20  ager->errCode;. 
efc70 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
efc80 72 55 6e 72 65 66 28 70 50 67 29 3b 0a 20 20 20  rUnref(pPg);.   
efc90 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
efca0 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75    }..    /* Popu
efcb0 6c 61 74 65 20 74 68 65 20 70 61 67 65 20 77 69  late the page wi
efcc0 74 68 20 64 61 74 61 2c 20 65 69 74 68 65 72 20  th data, either 
efcd0 62 79 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20  by reading from 
efce0 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20  the database.   
efcf0 20 2a 2a 20 66 69 6c 65 2c 20 6f 72 20 62 79 20   ** file, or by 
efd00 73 65 74 74 69 6e 67 20 74 68 65 20 65 6e 74 69  setting the enti
efd10 72 65 20 70 61 67 65 20 74 6f 20 7a 65 72 6f 2e  re page to zero.
efd20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
efd30 6e 4d 61 78 3c 28 69 6e 74 29 70 67 6e 6f 20 7c  nMax<(int)pgno |
efd40 7c 20 4d 45 4d 44 42 20 7c 7c 20 28 6e 6f 43 6f  | MEMDB || (noCo
efd50 6e 74 65 6e 74 20 26 26 20 21 70 50 61 67 65 72  ntent && !pPager
efd60 2d 3e 61 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b  ->alwaysRollback
efd70 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  ) ){.      if( p
efd80 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 6d 78 50 67  gno>pPager->mxPg
efd90 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  no ){.        sq
efda0 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
efdb0 70 50 67 29 3b 0a 20 20 20 20 20 20 20 20 72 65  pPg);.        re
efdc0 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c  turn SQLITE_FULL
efdd0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
efde0 6d 65 6d 73 65 74 28 50 47 48 44 52 5f 54 4f 5f  memset(PGHDR_TO_
efdf0 44 41 54 41 28 70 50 67 29 2c 20 30 2c 20 70 50  DATA(pPg), 0, pP
efe00 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b  ager->pageSize);
efe10 0a 20 20 20 20 20 20 70 50 67 2d 3e 6e 65 65 64  .      pPg->need
efe20 52 65 61 64 20 3d 20 6e 6f 43 6f 6e 74 65 6e 74  Read = noContent
efe30 20 26 26 20 21 70 50 61 67 65 72 2d 3e 61 6c 77   && !pPager->alw
efe40 61 79 73 52 6f 6c 6c 62 61 63 6b 3b 0a 20 20 20  aysRollback;.   
efe50 20 20 20 49 4f 54 52 41 43 45 28 28 22 5a 45 52     IOTRACE(("ZER
efe60 4f 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67  O %p %d\n", pPag
efe70 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 20 20  er, pgno));.    
efe80 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
efe90 3d 20 72 65 61 64 44 62 50 61 67 65 28 70 50 61  = readDbPage(pPa
efea0 67 65 72 2c 20 70 50 67 2c 20 70 67 6e 6f 29 3b  ger, pPg, pgno);
efeb0 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
efec0 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d  QLITE_OK && rc!=
efed0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f  SQLITE_IOERR_SHO
efee0 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20  RT_READ ){.     
efef0 20 20 20 70 50 67 2d 3e 70 67 6e 6f 20 3d 20 30     pPg->pgno = 0
eff00 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
eff10 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 67 29  3PagerUnref(pPg)
eff20 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
eff30 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   rc;.      }.   
eff40 20 20 20 70 50 67 2d 3e 6e 65 65 64 52 65 61 64     pPg->needRead
eff50 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20   = 0;.    }..   
eff60 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 70 61 67   /* Link the pag
eff70 65 20 69 6e 74 6f 20 74 68 65 20 70 61 67 65 20  e into the page 
eff80 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20  hash table */.  
eff90 20 20 68 20 3d 20 70 67 6e 6f 20 26 20 28 70 50    h = pgno & (pP
effa0 61 67 65 72 2d 3e 6e 48 61 73 68 2d 31 29 3b 0a  ager->nHash-1);.
effb0 20 20 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f      assert( pgno
effc0 21 3d 30 20 29 3b 0a 20 20 20 20 70 50 67 2d 3e  !=0 );.    pPg->
effd0 70 4e 65 78 74 48 61 73 68 20 3d 20 70 50 61 67  pNextHash = pPag
effe0 65 72 2d 3e 61 48 61 73 68 5b 68 5d 3b 0a 20 20  er->aHash[h];.  
efff0 20 20 70 50 61 67 65 72 2d 3e 61 48 61 73 68 5b    pPager->aHash[
f0000 68 5d 20 3d 20 70 50 67 3b 0a 20 20 20 20 69 66  h] = pPg;.    if
f0010 28 20 70 50 67 2d 3e 70 4e 65 78 74 48 61 73 68  ( pPg->pNextHash
f0020 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
f0030 28 20 70 50 67 2d 3e 70 4e 65 78 74 48 61 73 68  ( pPg->pNextHash
f0040 2d 3e 70 50 72 65 76 48 61 73 68 3d 3d 30 20 29  ->pPrevHash==0 )
f0050 3b 0a 20 20 20 20 20 20 70 50 67 2d 3e 70 4e 65  ;.      pPg->pNe
f0060 78 74 48 61 73 68 2d 3e 70 50 72 65 76 48 61 73  xtHash->pPrevHas
f0070 68 20 3d 20 70 50 67 3b 0a 20 20 20 20 7d 0a 0a  h = pPg;.    }..
f0080 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48  #ifdef SQLITE_CH
f0090 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 50  ECK_PAGES.    pP
f00a0 67 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61  g->pageHash = pa
f00b0 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67  ger_pagehash(pPg
f00c0 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73  );.#endif.  }els
f00d0 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65  e{.    /* The re
f00e0 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73 20  quested page is 
f00f0 69 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68  in the page cach
f0100 65 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  e. */.    assert
f0110 28 70 50 61 67 65 72 2d 3e 6e 52 65 66 3e 30 20  (pPager->nRef>0 
f0120 7c 7c 20 70 67 6e 6f 3d 3d 31 29 3b 0a 20 20 20  || pgno==1);.   
f0130 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67   PAGER_INCR(pPag
f0140 65 72 2d 3e 6e 48 69 74 29 3b 0a 20 20 20 20 69  er->nHit);.    i
f0150 66 28 20 21 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b  f( !noContent ){
f0160 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
f0170 72 5f 67 65 74 5f 63 6f 6e 74 65 6e 74 28 70 50  r_get_content(pP
f0180 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  g);.      if( rc
f0190 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
f01a0 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20  rn rc;.      }. 
f01b0 20 20 20 7d 0a 20 20 20 20 70 61 67 65 5f 72 65     }.    page_re
f01c0 66 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 2a 70  f(pPg);.  }.  *p
f01d0 70 50 61 67 65 20 3d 20 70 50 67 3b 0a 20 20 72  pPage = pPg;.  r
f01e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
f01f0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
f0200 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
f0210 65 72 41 63 71 75 69 72 65 28 0a 20 20 50 61 67  erAcquire(.  Pag
f0220 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20  er *pPager,     
f0230 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 6f 70   /* The pager op
f0240 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  en on the databa
f0250 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e  se file */.  Pgn
f0260 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20  o pgno,         
f0270 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20   /* Page number 
f0280 74 6f 20 66 65 74 63 68 20 2a 2f 0a 20 20 44 62  to fetch */.  Db
f0290 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 20  Page **ppPage,  
f02a0 20 20 2f 2a 20 57 72 69 74 65 20 61 20 70 6f 69    /* Write a poi
f02b0 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 65  nter to the page
f02c0 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e   here */.  int n
f02d0 6f 43 6f 6e 74 65 6e 74 20 20 20 20 20 20 20 2f  oContent       /
f02e0 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20  * Do not bother 
f02f0 72 65 61 64 69 6e 67 20 63 6f 6e 74 65 6e 74 20  reading content 
f0300 66 72 6f 6d 20 64 69 73 6b 20 69 66 20 74 72 75  from disk if tru
f0310 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
f0320 3b 0a 20 20 70 61 67 65 72 45 6e 74 65 72 28 70  ;.  pagerEnter(p
f0330 50 61 67 65 72 29 3b 0a 20 20 72 63 20 3d 20 70  Pager);.  rc = p
f0340 61 67 65 72 41 63 71 75 69 72 65 28 70 50 61 67  agerAcquire(pPag
f0350 65 72 2c 20 70 67 6e 6f 2c 20 70 70 50 61 67 65  er, pgno, ppPage
f0360 2c 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 20  , noContent);.  
f0370 70 61 67 65 72 4c 65 61 76 65 28 70 50 61 67 65  pagerLeave(pPage
f0380 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  r);.  return rc;
f0390 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69  .}.../*.** Acqui
f03a0 72 65 20 61 20 70 61 67 65 20 69 66 20 69 74 20  re a page if it 
f03b0 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68  is already in th
f03c0 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68  e in-memory cach
f03d0 65 2e 20 20 44 6f 0a 2a 2a 20 6e 6f 74 20 72 65  e.  Do.** not re
f03e0 61 64 20 74 68 65 20 70 61 67 65 20 66 72 6f 6d  ad the page from
f03f0 20 64 69 73 6b 2e 20 20 52 65 74 75 72 6e 20 61   disk.  Return a
f0400 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
f0410 70 61 67 65 2c 0a 2a 2a 20 6f 72 20 30 20 69 66  page,.** or 0 if
f0420 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
f0430 20 69 6e 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a   in cache..**.**
f0440 20 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 65   See also sqlite
f0450 33 50 61 67 65 72 47 65 74 28 29 2e 20 20 54 68  3PagerGet().  Th
f0460 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74  e difference bet
f0470 77 65 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  ween this routin
f0480 65 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33  e.** and sqlite3
f0490 50 61 67 65 72 47 65 74 28 29 20 69 73 20 74 68  PagerGet() is th
f04a0 61 74 20 5f 67 65 74 28 29 20 77 69 6c 6c 20 67  at _get() will g
f04b0 6f 20 74 6f 20 74 68 65 20 64 69 73 6b 20 61 6e  o to the disk an
f04c0 64 20 72 65 61 64 0a 2a 2a 20 69 6e 20 74 68 65  d read.** in the
f04d0 20 70 61 67 65 20 69 66 20 74 68 65 20 70 61 67   page if the pag
f04e0 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  e is not already
f04f0 20 69 6e 20 63 61 63 68 65 2e 20 20 54 68 69 73   in cache.  This
f0500 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75   routine.** retu
f0510 72 6e 73 20 4e 55 4c 4c 20 69 66 20 74 68 65 20  rns NULL if the 
f0520 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 63  page is not in c
f0530 61 63 68 65 20 6f 72 20 69 66 20 61 20 64 69 73  ache or if a dis
f0540 6b 20 49 2f 4f 20 65 72 72 6f 72 20 0a 2a 2a 20  k I/O error .** 
f0550 68 61 73 20 65 76 65 72 20 68 61 70 70 65 6e 65  has ever happene
f0560 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
f0570 56 41 54 45 20 44 62 50 61 67 65 20 2a 73 71 6c  VATE DbPage *sql
f0580 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28  ite3PagerLookup(
f0590 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50  Pager *pPager, P
f05a0 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48  gno pgno){.  PgH
f05b0 64 72 20 2a 70 50 67 20 3d 20 30 3b 0a 0a 20 20  dr *pPg = 0;..  
f05c0 61 73 73 65 72 74 28 20 70 50 61 67 65 72 21 3d  assert( pPager!=
f05d0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
f05e0 67 6e 6f 21 3d 30 20 29 3b 0a 0a 20 20 70 61 67  gno!=0 );..  pag
f05f0 65 72 45 6e 74 65 72 28 70 50 61 67 65 72 29 3b  erEnter(pPager);
f0600 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  .  if( pPager->s
f0610 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f  tate==PAGER_UNLO
f0620 43 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  CK ){.    assert
f0630 28 20 21 70 50 61 67 65 72 2d 3e 70 41 6c 6c 20  ( !pPager->pAll 
f0640 7c 7c 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75  || pPager->exclu
f0650 73 69 76 65 4d 6f 64 65 20 29 3b 0a 20 20 7d 65  siveMode );.  }e
f0660 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e  lse if( pPager->
f0670 65 72 72 43 6f 64 65 20 26 26 20 70 50 61 67 65  errCode && pPage
f0680 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c 49  r->errCode!=SQLI
f0690 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 2f  TE_FULL ){.    /
f06a0 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  * Do nothing */.
f06b0 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 50 67    }else if( (pPg
f06c0 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28   = pager_lookup(
f06d0 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 29 21 3d  pPager, pgno))!=
f06e0 30 20 29 7b 0a 20 20 20 20 70 61 67 65 5f 72 65  0 ){.    page_re
f06f0 66 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 70 61  f(pPg);.  }.  pa
f0700 67 65 72 4c 65 61 76 65 28 70 50 61 67 65 72 29  gerLeave(pPager)
f0710 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 3b 0a  ;.  return pPg;.
f0720 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65  }../*.** Release
f0730 20 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49   a page..**.** I
f0740 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  f the number of 
f0750 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68  references to th
f0760 65 20 70 61 67 65 20 64 72 6f 70 20 74 6f 20 7a  e page drop to z
f0770 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a  ero, then the.**
f0780 20 70 61 67 65 20 69 73 20 61 64 64 65 64 20 74   page is added t
f0790 6f 20 74 68 65 20 4c 52 55 20 6c 69 73 74 2e 20  o the LRU list. 
f07a0 20 57 68 65 6e 20 61 6c 6c 20 72 65 66 65 72 65   When all refere
f07b0 6e 63 65 73 20 74 6f 20 61 6c 6c 20 70 61 67 65  nces to all page
f07c0 73 0a 2a 2a 20 61 72 65 20 72 65 6c 65 61 73 65  s.** are release
f07d0 64 2c 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 63  d, a rollback oc
f07e0 63 75 72 73 20 61 6e 64 20 74 68 65 20 6c 6f 63  curs and the loc
f07f0 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
f0800 65 20 69 73 0a 2a 2a 20 72 65 6d 6f 76 65 64 2e  e is.** removed.
f0810 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
f0820 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
f0830 67 65 72 55 6e 72 65 66 28 44 62 50 61 67 65 20  gerUnref(DbPage 
f0840 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a  *pPg){.  Pager *
f0850 70 50 61 67 65 72 3b 0a 0a 20 20 69 66 28 20 70  pPager;..  if( p
f0860 50 67 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53  Pg==0 ) return S
f0870 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 50 61 67  QLITE_OK;.  pPag
f0880 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72  er = pPg->pPager
f0890 3b 0a 0a 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e  ;..  /* Decremen
f08a0 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20  t the reference 
f08b0 63 6f 75 6e 74 20 66 6f 72 20 74 68 69 73 20 70  count for this p
f08c0 61 67 65 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  age.  */.  asser
f08d0 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30 20 29  t( pPg->nRef>0 )
f08e0 3b 0a 20 20 70 61 67 65 72 45 6e 74 65 72 28 70  ;.  pagerEnter(p
f08f0 50 67 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 70  Pg->pPager);.  p
f0900 50 67 2d 3e 6e 52 65 66 2d 2d 3b 0a 0a 20 20 43  Pg->nRef--;..  C
f0910 48 45 43 4b 5f 50 41 47 45 28 70 50 67 29 3b 0a  HECK_PAGE(pPg);.
f0920 0a 20 20 2f 2a 20 57 68 65 6e 20 74 68 65 20 6e  .  /* When the n
f0930 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e  umber of referen
f0940 63 65 73 20 74 6f 20 61 20 70 61 67 65 20 72 65  ces to a page re
f0950 61 63 68 20 30 2c 20 63 61 6c 6c 20 74 68 65 0a  ach 0, call the.
f0960 20 20 2a 2a 20 64 65 73 74 72 75 63 74 6f 72 20    ** destructor 
f0970 61 6e 64 20 61 64 64 20 74 68 65 20 70 61 67 65  and add the page
f0980 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74   to the freelist
f0990 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 67  ..  */.  if( pPg
f09a0 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 0a 20 20  ->nRef==0 ){..  
f09b0 20 20 6c 72 75 4c 69 73 74 41 64 64 28 70 50 67    lruListAdd(pPg
f09c0 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  );.    if( pPage
f09d0 72 2d 3e 78 44 65 73 74 72 75 63 74 6f 72 20 29  r->xDestructor )
f09e0 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  {.      pPager->
f09f0 78 44 65 73 74 72 75 63 74 6f 72 28 70 50 67 2c  xDestructor(pPg,
f0a00 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
f0a10 65 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20  e);.    }.  .   
f0a20 20 2f 2a 20 57 68 65 6e 20 61 6c 6c 20 70 61 67   /* When all pag
f0a30 65 73 20 72 65 61 63 68 20 74 68 65 20 66 72 65  es reach the fre
f0a40 65 6c 69 73 74 2c 20 64 72 6f 70 20 74 68 65 20  elist, drop the 
f0a50 72 65 61 64 20 6c 6f 63 6b 20 66 72 6f 6d 0a 20  read lock from. 
f0a60 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61     ** the databa
f0a70 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a  se file..    */.
f0a80 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 66      pPager->nRef
f0a90 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  --;.    assert( 
f0aa0 70 50 61 67 65 72 2d 3e 6e 52 65 66 3e 3d 30 20  pPager->nRef>=0 
f0ab0 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  );.    if( pPage
f0ac0 72 2d 3e 6e 52 65 66 3d 3d 30 20 26 26 20 28 21  r->nRef==0 && (!
f0ad0 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
f0ae0 65 4d 6f 64 65 20 7c 7c 20 70 50 61 67 65 72 2d  eMode || pPager-
f0af0 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 29 20 29  >journalOff>0) )
f0b00 7b 0a 20 20 20 20 20 20 70 61 67 65 72 55 6e 6c  {.      pagerUnl
f0b10 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 70  ockAndRollback(p
f0b20 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20  Pager);.    }.  
f0b30 7d 0a 20 20 70 61 67 65 72 4c 65 61 76 65 28 70  }.  pagerLeave(p
f0b40 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72 6e  Pager);.  return
f0b50 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
f0b60 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6a 6f  *.** Create a jo
f0b70 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 70  urnal file for p
f0b80 50 61 67 65 72 2e 20 20 54 68 65 72 65 20 73 68  Pager.  There sh
f0b90 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 62 65 20  ould already be 
f0ba0 61 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 6f 72  a RESERVED.** or
f0bb0 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20   EXCLUSIVE lock 
f0bc0 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
f0bd0 66 69 6c 65 20 77 68 65 6e 20 74 68 69 73 20 72  file when this r
f0be0 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
f0bf0 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ..**.** Return S
f0c00 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72  QLITE_OK if ever
f0c10 79 74 68 69 6e 67 2e 20 20 52 65 74 75 72 6e 20  ything.  Return 
f0c20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e  an error code an
f0c30 64 20 72 65 6c 65 61 73 65 20 74 68 65 0a 2a 2a  d release the.**
f0c40 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 61   write lock if a
f0c50 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f  nything goes wro
f0c60 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ng..*/.static in
f0c70 74 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75  t pager_open_jou
f0c80 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67  rnal(Pager *pPag
f0c90 65 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76  er){.  sqlite3_v
f0ca0 66 73 20 2a 70 56 66 73 20 3d 20 70 50 61 67 65  fs *pVfs = pPage
f0cb0 72 2d 3e 70 56 66 73 3b 0a 20 20 69 6e 74 20 66  r->pVfs;.  int f
f0cc0 6c 61 67 73 20 3d 20 28 53 51 4c 49 54 45 5f 4f  lags = (SQLITE_O
f0cd0 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51  PEN_READWRITE|SQ
f0ce0 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53  LITE_OPEN_EXCLUS
f0cf0 49 56 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  IVE|SQLITE_OPEN_
f0d00 43 52 45 41 54 45 29 3b 0a 0a 20 20 69 6e 74 20  CREATE);..  int 
f0d10 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 21 4d  rc;.  assert( !M
f0d20 45 4d 44 42 20 29 3b 0a 20 20 61 73 73 65 72 74  EMDB );.  assert
f0d30 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
f0d40 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20  =PAGER_RESERVED 
f0d50 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
f0d60 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20  ger->useJournal 
f0d70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
f0d80 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d  ger->pInJournal=
f0d90 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 50  =0 );.  sqlite3P
f0da0 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50  agerPagecount(pP
f0db0 61 67 65 72 29 3b 0a 20 20 70 61 67 65 72 4c 65  ager);.  pagerLe
f0dc0 61 76 65 28 70 50 61 67 65 72 29 3b 0a 20 20 70  ave(pPager);.  p
f0dd0 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
f0de0 6c 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65  l = sqlite3Bitve
f0df0 63 43 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e  cCreate(pPager->
f0e00 64 62 53 69 7a 65 29 3b 0a 20 20 70 61 67 65 72  dbSize);.  pager
f0e10 45 6e 74 65 72 28 70 50 61 67 65 72 29 3b 0a 20  Enter(pPager);. 
f0e20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70 49 6e   if( pPager->pIn
f0e30 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 7b 0a 20 20  Journal==0 ){.  
f0e40 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
f0e50 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 66 61  MEM;.    goto fa
f0e60 69 6c 65 64 5f 74 6f 5f 6f 70 65 6e 5f 6a 6f 75  iled_to_open_jou
f0e70 72 6e 61 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  rnal;.  }..  if(
f0e80 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
f0e90 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 69  Open==0 ){.    i
f0ea0 66 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  f( pPager->tempF
f0eb0 69 6c 65 20 29 7b 0a 20 20 20 20 20 20 66 6c 61  ile ){.      fla
f0ec0 67 73 20 7c 3d 20 28 53 51 4c 49 54 45 5f 4f 50  gs |= (SQLITE_OP
f0ed0 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45  EN_DELETEONCLOSE
f0ee0 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d  |SQLITE_OPEN_TEM
f0ef0 50 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20  P_JOURNAL);.    
f0f00 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 6c 61  }else{.      fla
f0f10 67 73 20 7c 3d 20 28 53 51 4c 49 54 45 5f 4f 50  gs |= (SQLITE_OP
f0f20 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29  EN_MAIN_JOURNAL)
f0f30 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53  ;.    }.#ifdef S
f0f40 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f  QLITE_ENABLE_ATO
f0f50 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 20 72 63  MIC_WRITE.    rc
f0f60 20 3d 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61   = sqlite3Journa
f0f70 6c 4f 70 65 6e 28 0a 20 20 20 20 20 20 20 20 70  lOpen(.        p
f0f80 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f  Vfs, pPager->zJo
f0f90 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a  urnal, pPager->j
f0fa0 66 64 2c 20 66 6c 61 67 73 2c 20 6a 72 6e 6c 42  fd, flags, jrnlB
f0fb0 75 66 66 65 72 53 69 7a 65 28 70 50 61 67 65 72  ufferSize(pPager
f0fc0 29 0a 20 20 20 20 29 3b 0a 23 65 6c 73 65 0a 20  ).    );.#else. 
f0fd0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
f0fe0 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67  sOpen(pVfs, pPag
f0ff0 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50  er->zJournal, pP
f1000 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 67 73  ager->jfd, flags
f1010 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20  , 0);.#endif.   
f1020 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
f1030 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 72  ITE_OK || pPager
f1040 2d 3e 6a 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20  ->jfd->pMethods 
f1050 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a  );.    pPager->j
f1060 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20  ournalOff = 0;. 
f1070 20 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61     pPager->setMa
f1080 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 70 50  ster = 0;.    pP
f1090 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72  ager->journalHdr
f10a0 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 72 63   = 0;.    if( rc
f10b0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
f10c0 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
f10d0 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20  ITE_NOMEM ){.   
f10e0 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 65       sqlite3OsDe
f10f0 6c 65 74 65 28 70 56 66 73 2c 20 70 50 61 67 65  lete(pVfs, pPage
f1100 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b  r->zJournal, 0);
f1110 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67  .      }.      g
f1120 6f 74 6f 20 66 61 69 6c 65 64 5f 74 6f 5f 6f 70  oto failed_to_op
f1130 65 6e 5f 6a 6f 75 72 6e 61 6c 3b 0a 20 20 20 20  en_journal;.    
f1140 7d 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e  }.  }.  pPager->
f1150 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 3d 20 31 3b  journalOpen = 1;
f1160 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  .  pPager->journ
f1170 61 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20  alStarted = 0;. 
f1180 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
f1190 63 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d  c = 0;.  pPager-
f11a0 3e 61 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 20  >alwaysRollback 
f11b0 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e  = 0;.  pPager->n
f11c0 52 65 63 20 3d 20 30 3b 0a 20 20 69 66 28 20 70  Rec = 0;.  if( p
f11d0 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29  Pager->errCode )
f11e0 7b 0a 20 20 20 20 72 63 20 3d 20 70 50 61 67 65  {.    rc = pPage
f11f0 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 20 20  r->errCode;.    
f1200 67 6f 74 6f 20 66 61 69 6c 65 64 5f 74 6f 5f 6f  goto failed_to_o
f1210 70 65 6e 5f 6a 6f 75 72 6e 61 6c 3b 0a 20 20 7d  pen_journal;.  }
f1220 0a 20 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 44  .  pPager->origD
f1230 62 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e  bSize = pPager->
f1240 64 62 53 69 7a 65 3b 0a 0a 20 20 72 63 20 3d 20  dbSize;..  rc = 
f1250 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28  writeJournalHdr(
f1260 70 50 61 67 65 72 29 3b 0a 0a 20 20 69 66 28 20  pPager);..  if( 
f1270 70 50 61 67 65 72 2d 3e 73 74 6d 74 41 75 74 6f  pPager->stmtAuto
f1280 6f 70 65 6e 20 26 26 20 72 63 3d 3d 53 51 4c 49  open && rc==SQLI
f1290 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20  TE_OK ){.    rc 
f12a0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 74  = sqlite3PagerSt
f12b0 6d 74 42 65 67 69 6e 28 70 50 61 67 65 72 29 3b  mtBegin(pPager);
f12c0 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 3d 53  .  }.  if( rc!=S
f12d0 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d  QLITE_OK && rc!=
f12e0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 26 26 20  SQLITE_NOMEM && 
f12f0 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc!=SQLITE_IOERR
f1300 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 72 63  _NOMEM ){.    rc
f1310 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61   = pager_end_tra
f1320 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c  nsaction(pPager,
f1330 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d   0);.    if( rc=
f1340 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
f1350 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
f1360 46 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  FULL;.    }.  }.
f1370 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 66 61    return rc;..fa
f1380 69 6c 65 64 5f 74 6f 5f 6f 70 65 6e 5f 6a 6f 75  iled_to_open_jou
f1390 72 6e 61 6c 3a 0a 20 20 73 71 6c 69 74 65 33 42  rnal:.  sqlite3B
f13a0 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61  itvecDestroy(pPa
f13b0 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29  ger->pInJournal)
f13c0 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a  ;.  pPager->pInJ
f13d0 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 72 65  ournal = 0;.  re
f13e0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
f13f0 2a 20 41 63 71 75 69 72 65 20 61 20 77 72 69 74  * Acquire a writ
f1400 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61  e-lock on the da
f1410 74 61 62 61 73 65 2e 20 20 54 68 65 20 6c 6f 63  tabase.  The loc
f1420 6b 20 69 73 20 72 65 6d 6f 76 65 64 20 77 68 65  k is removed whe
f1430 6e 0a 2a 2a 20 74 68 65 20 61 6e 79 20 6f 66 20  n.** the any of 
f1440 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 68 61  the following ha
f1450 70 70 65 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  ppen:.**.**   * 
f1460 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d   sqlite3PagerCom
f1470 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 69 73  mitPhaseTwo() is
f1480 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 20 20 2a 20   called..**   * 
f1490 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c   sqlite3PagerRol
f14a0 6c 62 61 63 6b 28 29 20 69 73 20 63 61 6c 6c 65  lback() is calle
f14b0 64 2e 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74  d..**   *  sqlit
f14c0 65 33 50 61 67 65 72 43 6c 6f 73 65 28 29 20 69  e3PagerClose() i
f14d0 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 20 20 2a  s called..**   *
f14e0 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
f14f0 72 65 66 28 29 20 69 73 20 63 61 6c 6c 65 64 20  ref() is called 
f1500 74 6f 20 6f 6e 20 65 76 65 72 79 20 6f 75 74 73  to on every outs
f1510 74 61 6e 64 69 6e 67 20 70 61 67 65 2e 0a 2a 2a  tanding page..**
f1520 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61  .** The first pa
f1530 72 61 6d 65 74 65 72 20 74 6f 20 74 68 69 73 20  rameter to this 
f1540 72 6f 75 74 69 6e 65 20 69 73 20 61 20 70 6f 69  routine is a poi
f1550 6e 74 65 72 20 74 6f 20 61 6e 79 20 6f 70 65 6e  nter to any open
f1560 20 70 61 67 65 20 6f 66 20 74 68 65 0a 2a 2a 20   page of the.** 
f1570 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
f1580 4e 6f 74 68 69 6e 67 20 63 68 61 6e 67 65 73 20  Nothing changes 
f1590 61 62 6f 75 74 20 74 68 65 20 70 61 67 65 20 2d  about the page -
f15a0 20 69 74 20 69 73 20 75 73 65 64 20 6d 65 72 65   it is used mere
f15b0 6c 79 20 74 6f 0a 2a 2a 20 61 63 71 75 69 72 65  ly to.** acquire
f15c0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
f15d0 65 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72  e Pager structur
f15e0 65 20 61 6e 64 20 61 73 20 70 72 6f 6f 66 20 74  e and as proof t
f15f0 68 61 74 20 74 68 65 72 65 20 69 73 0a 2a 2a 20  hat there is.** 
f1600 61 6c 72 65 61 64 79 20 61 20 72 65 61 64 2d 6c  already a read-l
f1610 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
f1620 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ase..**.** The s
f1630 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
f1640 69 6e 64 69 63 61 74 65 73 20 68 6f 77 20 6d 75  indicates how mu
f1650 63 68 20 73 70 61 63 65 20 69 6e 20 62 79 74 65  ch space in byte
f1660 73 20 74 6f 20 72 65 73 65 72 76 65 20 66 6f 72  s to reserve for
f1670 20 61 0a 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75   a.** master jou
f1680 72 6e 61 6c 20 66 69 6c 65 2d 6e 61 6d 65 20 61  rnal file-name a
f1690 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  t the start of t
f16a0 68 65 20 6a 6f 75 72 6e 61 6c 20 77 68 65 6e 20  he journal when 
f16b0 69 74 20 69 73 20 63 72 65 61 74 65 64 2e 0a 2a  it is created..*
f16c0 2a 0a 2a 2a 20 41 20 6a 6f 75 72 6e 61 6c 20 66  *.** A journal f
f16d0 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 69 66  ile is opened if
f16e0 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 20 74   this is not a t
f16f0 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 20  emporary file.  
f1700 46 6f 72 20 74 65 6d 70 6f 72 61 72 79 0a 2a 2a  For temporary.**
f1710 20 66 69 6c 65 73 2c 20 74 68 65 20 6f 70 65 6e   files, the open
f1720 69 6e 67 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  ing of the journ
f1730 61 6c 20 66 69 6c 65 20 69 73 20 64 65 66 65 72  al file is defer
f1740 72 65 64 20 75 6e 74 69 6c 20 74 68 65 72 65 20  red until there 
f1750 69 73 20 61 6e 0a 2a 2a 20 61 63 74 75 61 6c 20  is an.** actual 
f1760 6e 65 65 64 20 74 6f 20 77 72 69 74 65 20 74 6f  need to write to
f1770 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a   the journal..**
f1780 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62  .** If the datab
f1790 61 73 65 20 69 73 20 61 6c 72 65 61 64 79 20 72  ase is already r
f17a0 65 73 65 72 76 65 64 20 66 6f 72 20 77 72 69 74  eserved for writ
f17b0 69 6e 67 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  ing, this routin
f17c0 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a  e is a no-op..**
f17d0 0a 2a 2a 20 49 66 20 65 78 46 6c 61 67 20 69 73  .** If exFlag is
f17e0 20 74 72 75 65 2c 20 67 6f 20 61 68 65 61 64 20   true, go ahead 
f17f0 61 6e 64 20 67 65 74 20 61 6e 20 45 58 43 4c 55  and get an EXCLU
f1800 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  SIVE lock on the
f1810 20 66 69 6c 65 0a 2a 2a 20 69 6d 6d 65 64 69 61   file.** immedia
f1820 74 65 6c 79 20 69 6e 73 74 65 61 64 20 6f 66 20  tely instead of 
f1830 77 61 69 74 69 6e 67 20 75 6e 74 69 6c 20 77 65  waiting until we
f1840 20 74 72 79 20 74 6f 20 66 6c 75 73 68 20 74 68   try to flush th
f1850 65 20 63 61 63 68 65 2e 20 20 54 68 65 0a 2a 2a  e cache.  The.**
f1860 20 65 78 46 6c 61 67 20 69 73 20 69 67 6e 6f 72   exFlag is ignor
f1870 65 64 20 69 66 20 61 20 74 72 61 6e 73 61 63 74  ed if a transact
f1880 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 79 20 61  ion is already a
f1890 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ctive..*/.SQLITE
f18a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
f18b0 69 74 65 33 50 61 67 65 72 42 65 67 69 6e 28 44  ite3PagerBegin(D
f18c0 62 50 61 67 65 20 2a 70 50 67 2c 20 69 6e 74 20  bPage *pPg, int 
f18d0 65 78 46 6c 61 67 29 7b 0a 20 20 50 61 67 65 72  exFlag){.  Pager
f18e0 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e   *pPager = pPg->
f18f0 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63  pPager;.  int rc
f1900 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
f1910 70 61 67 65 72 45 6e 74 65 72 28 70 50 61 67 65  pagerEnter(pPage
f1920 72 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50  r);.  assert( pP
f1930 67 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 61  g->nRef>0 );.  a
f1940 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
f1950 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f  tate!=PAGER_UNLO
f1960 43 4b 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67  CK );.  if( pPag
f1970 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52  er->state==PAGER
f1980 5f 53 48 41 52 45 44 20 29 7b 0a 20 20 20 20 61  _SHARED ){.    a
f1990 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70  ssert( pPager->p
f19a0 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a  InJournal==0 );.
f19b0 20 20 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b      if( MEMDB ){
f19c0 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73  .      pPager->s
f19d0 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58 43  tate = PAGER_EXC
f19e0 4c 55 53 49 56 45 3b 0a 20 20 20 20 20 20 70 50  LUSIVE;.      pP
f19f0 61 67 65 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65  ager->origDbSize
f1a00 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a   = pPager->dbSiz
f1a10 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  e;.    }else{.  
f1a20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
f1a30 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66  OsLock(pPager->f
f1a40 64 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  d, RESERVED_LOCK
f1a50 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
f1a60 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
f1a70 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74        pPager->st
f1a80 61 74 65 20 3d 20 50 41 47 45 52 5f 52 45 53 45  ate = PAGER_RESE
f1a90 52 56 45 44 3b 0a 20 20 20 20 20 20 20 20 69 66  RVED;.        if
f1aa0 28 20 65 78 46 6c 61 67 20 29 7b 0a 20 20 20 20  ( exFlag ){.    
f1ab0 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
f1ac0 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50  _wait_on_lock(pP
f1ad0 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f  ager, EXCLUSIVE_
f1ae0 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 7d  LOCK);.        }
f1af0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
f1b00 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
f1b10 20 29 7b 0a 20 20 20 20 20 20 20 20 70 61 67 65   ){.        page
f1b20 72 4c 65 61 76 65 28 70 50 61 67 65 72 29 3b 0a  rLeave(pPager);.
f1b30 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
f1b40 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  c;.      }.     
f1b50 20 70 50 61 67 65 72 2d 3e 64 69 72 74 79 43 61   pPager->dirtyCa
f1b60 63 68 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 50  che = 0;.      P
f1b70 41 47 45 52 54 52 41 43 45 32 28 22 54 52 41 4e  AGERTRACE2("TRAN
f1b80 53 41 43 54 49 4f 4e 20 25 64 5c 6e 22 2c 20 50  SACTION %d\n", P
f1b90 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 3b  AGERID(pPager));
f1ba0 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65  .      if( pPage
f1bb0 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 26 26  r->useJournal &&
f1bc0 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69   !pPager->tempFi
f1bd0 6c 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  le.             
f1be0 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  && pPager->journ
f1bf0 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f  alMode!=PAGER_JO
f1c00 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b  URNALMODE_OFF ){
f1c10 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61  .        rc = pa
f1c20 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c  ger_open_journal
f1c30 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20  (pPager);.      
f1c40 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  }.    }.  }else 
f1c50 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  if( pPager->jour
f1c60 6e 61 6c 4f 70 65 6e 20 26 26 20 70 50 61 67 65  nalOpen && pPage
f1c70 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30  r->journalOff==0
f1c80 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20   ){.    /* This 
f1c90 68 61 70 70 65 6e 73 20 77 68 65 6e 20 74 68 65  happens when the
f1ca0 20 70 61 67 65 72 20 77 61 73 20 69 6e 20 65 78   pager was in ex
f1cb0 63 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20 6d  clusive-access m
f1cc0 6f 64 65 20 74 68 65 20 6c 61 73 74 0a 20 20 20  ode the last.   
f1cd0 20 2a 2a 20 74 69 6d 65 20 61 20 28 72 65 61 64   ** time a (read
f1ce0 20 6f 72 20 77 72 69 74 65 29 20 74 72 61 6e 73   or write) trans
f1cf0 61 63 74 69 6f 6e 20 77 61 73 20 73 75 63 63 65  action was succe
f1d00 73 73 66 75 6c 6c 79 20 63 6f 6e 63 6c 75 64 65  ssfully conclude
f1d10 64 0a 20 20 20 20 2a 2a 20 62 79 20 74 68 69 73  d.    ** by this
f1d20 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 6e 73   connection. Ins
f1d30 74 65 61 64 20 6f 66 20 64 65 6c 65 74 69 6e 67  tead of deleting
f1d40 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
f1d50 65 20 69 74 20 77 61 73 20 0a 20 20 20 20 2a 2a  e it was .    **
f1d60 20 6b 65 70 74 20 6f 70 65 6e 20 61 6e 64 20 65   kept open and e
f1d70 69 74 68 65 72 20 77 61 73 20 74 72 75 6e 63 61  ither was trunca
f1d80 74 65 64 20 74 6f 20 30 20 62 79 74 65 73 20 6f  ted to 0 bytes o
f1d90 72 20 69 74 73 20 68 65 61 64 65 72 20 77 61 73  r its header was
f1da0 0a 20 20 20 20 2a 2a 20 6f 76 65 72 77 72 69 74  .    ** overwrit
f1db0 74 65 6e 20 77 69 74 68 20 7a 65 72 6f 73 2e 0a  ten with zeros..
f1dc0 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
f1dd0 74 28 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 3d  t( pPager->nRec=
f1de0 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
f1df0 28 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62  ( pPager->origDb
f1e00 53 69 7a 65 3d 3d 30 20 29 3b 0a 20 20 20 20 61  Size==0 );.    a
f1e10 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70  ssert( pPager->p
f1e20 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a  InJournal==0 );.
f1e30 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
f1e40 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72  Pagecount(pPager
f1e50 29 3b 0a 20 20 20 20 70 61 67 65 72 4c 65 61 76  );.    pagerLeav
f1e60 65 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 70  e(pPager);.    p
f1e70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
f1e80 6c 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65  l = sqlite3Bitve
f1e90 63 43 72 65 61 74 65 28 20 70 50 61 67 65 72 2d  cCreate( pPager-
f1ea0 3e 64 62 53 69 7a 65 20 29 3b 0a 20 20 20 20 70  >dbSize );.    p
f1eb0 61 67 65 72 45 6e 74 65 72 28 70 50 61 67 65 72  agerEnter(pPager
f1ec0 29 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67  );.    if( !pPag
f1ed0 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 29  er->pInJournal )
f1ee0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
f1ef0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
f1f00 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 50 61 67  else{.      pPag
f1f10 65 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65 20 3d  er->origDbSize =
f1f20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b   pPager->dbSize;
f1f30 0a 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74  .      rc = writ
f1f40 65 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67  eJournalHdr(pPag
f1f50 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  er);.    }.  }. 
f1f60 20 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72   assert( !pPager
f1f70 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 7c 7c  ->journalOpen ||
f1f80 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
f1f90 4f 66 66 3e 30 20 7c 7c 20 72 63 21 3d 53 51 4c  Off>0 || rc!=SQL
f1fa0 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 70 61 67 65  ITE_OK );.  page
f1fb0 72 4c 65 61 76 65 28 70 50 61 67 65 72 29 3b 0a  rLeave(pPager);.
f1fc0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
f1fd0 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 70 61 67  /*.** Make a pag
f1fe0 65 20 64 69 72 74 79 2e 20 20 53 65 74 20 69 74  e dirty.  Set it
f1ff0 73 20 64 69 72 74 79 20 66 6c 61 67 20 61 6e 64  s dirty flag and
f2000 20 61 64 64 20 69 74 20 74 6f 20 74 68 65 20 64   add it to the d
f2010 69 72 74 79 0a 2a 2a 20 70 61 67 65 20 6c 69 73  irty.** page lis
f2020 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
f2030 64 20 6d 61 6b 65 44 69 72 74 79 28 50 67 48 64  d makeDirty(PgHd
f2040 72 20 2a 70 50 67 29 7b 0a 20 20 69 66 28 20 70  r *pPg){.  if( p
f2050 50 67 2d 3e 64 69 72 74 79 3d 3d 30 20 29 7b 0a  Pg->dirty==0 ){.
f2060 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65      Pager *pPage
f2070 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b  r = pPg->pPager;
f2080 0a 20 20 20 20 70 50 67 2d 3e 64 69 72 74 79 20  .    pPg->dirty 
f2090 3d 20 31 3b 0a 20 20 20 20 70 50 67 2d 3e 70 44  = 1;.    pPg->pD
f20a0 69 72 74 79 20 3d 20 70 50 61 67 65 72 2d 3e 70  irty = pPager->p
f20b0 44 69 72 74 79 3b 0a 20 20 20 20 69 66 28 20 70  Dirty;.    if( p
f20c0 50 61 67 65 72 2d 3e 70 44 69 72 74 79 20 29 7b  Pager->pDirty ){
f20d0 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70  .      pPager->p
f20e0 44 69 72 74 79 2d 3e 70 50 72 65 76 44 69 72 74  Dirty->pPrevDirt
f20f0 79 20 3d 20 70 50 67 3b 0a 20 20 20 20 7d 0a 20  y = pPg;.    }. 
f2100 20 20 20 70 50 67 2d 3e 70 50 72 65 76 44 69 72     pPg->pPrevDir
f2110 74 79 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67  ty = 0;.    pPag
f2120 65 72 2d 3e 70 44 69 72 74 79 20 3d 20 70 50 67  er->pDirty = pPg
f2130 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  ;.  }.}../*.** M
f2140 61 6b 65 20 61 20 70 61 67 65 20 63 6c 65 61 6e  ake a page clean
f2150 2e 20 20 43 6c 65 61 72 20 69 74 73 20 64 69 72  .  Clear its dir
f2160 74 79 20 62 69 74 20 61 6e 64 20 72 65 6d 6f 76  ty bit and remov
f2170 65 20 69 74 20 66 72 6f 6d 20 74 68 65 0a 2a 2a  e it from the.**
f2180 20 64 69 72 74 79 20 70 61 67 65 20 6c 69 73 74   dirty page list
f2190 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
f21a0 20 6d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64 72   makeClean(PgHdr
f21b0 20 2a 70 50 67 29 7b 0a 20 20 69 66 28 20 70 50   *pPg){.  if( pP
f21c0 67 2d 3e 64 69 72 74 79 20 29 7b 0a 20 20 20 20  g->dirty ){.    
f21d0 70 50 67 2d 3e 64 69 72 74 79 20 3d 20 30 3b 0a  pPg->dirty = 0;.
f21e0 20 20 20 20 69 66 28 20 70 50 67 2d 3e 70 44 69      if( pPg->pDi
f21f0 72 74 79 20 29 7b 0a 20 20 20 20 20 20 61 73 73  rty ){.      ass
f2200 65 72 74 28 20 70 50 67 2d 3e 70 44 69 72 74 79  ert( pPg->pDirty
f2210 2d 3e 70 50 72 65 76 44 69 72 74 79 3d 3d 70 50  ->pPrevDirty==pP
f2220 67 20 29 3b 0a 20 20 20 20 20 20 70 50 67 2d 3e  g );.      pPg->
f2230 70 44 69 72 74 79 2d 3e 70 50 72 65 76 44 69 72  pDirty->pPrevDir
f2240 74 79 20 3d 20 70 50 67 2d 3e 70 50 72 65 76 44  ty = pPg->pPrevD
f2250 69 72 74 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20  irty;.    }.    
f2260 69 66 28 20 70 50 67 2d 3e 70 50 72 65 76 44 69  if( pPg->pPrevDi
f2270 72 74 79 20 29 7b 0a 20 20 20 20 20 20 61 73 73  rty ){.      ass
f2280 65 72 74 28 20 70 50 67 2d 3e 70 50 72 65 76 44  ert( pPg->pPrevD
f2290 69 72 74 79 2d 3e 70 44 69 72 74 79 3d 3d 70 50  irty->pDirty==pP
f22a0 67 20 29 3b 0a 20 20 20 20 20 20 70 50 67 2d 3e  g );.      pPg->
f22b0 70 50 72 65 76 44 69 72 74 79 2d 3e 70 44 69 72  pPrevDirty->pDir
f22c0 74 79 20 3d 20 70 50 67 2d 3e 70 44 69 72 74 79  ty = pPg->pDirty
f22d0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
f22e0 20 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e     assert( pPg->
f22f0 70 50 61 67 65 72 2d 3e 70 44 69 72 74 79 3d 3d  pPager->pDirty==
f2300 70 50 67 20 29 3b 0a 20 20 20 20 20 20 70 50 67  pPg );.      pPg
f2310 2d 3e 70 50 61 67 65 72 2d 3e 70 44 69 72 74 79  ->pPager->pDirty
f2320 20 3d 20 70 50 67 2d 3e 70 44 69 72 74 79 3b 0a   = pPg->pDirty;.
f2330 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a      }.  }.}.../*
f2340 0a 2a 2a 20 4d 61 72 6b 20 61 20 64 61 74 61 20  .** Mark a data 
f2350 70 61 67 65 20 61 73 20 77 72 69 74 65 61 62 6c  page as writeabl
f2360 65 2e 20 20 54 68 65 20 70 61 67 65 20 69 73 20  e.  The page is 
f2370 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65  written into the
f2380 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 69 66 20   journal .** if 
f2390 69 74 20 69 73 20 6e 6f 74 20 74 68 65 72 65 20  it is not there 
f23a0 61 6c 72 65 61 64 79 2e 20 20 54 68 69 73 20 72  already.  This r
f23b0 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63  outine must be c
f23c0 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 6d 61 6b  alled before mak
f23d0 69 6e 67 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74  ing.** changes t
f23e0 6f 20 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20  o a page..**.** 
f23f0 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74  The first time t
f2400 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
f2410 61 6c 6c 65 64 2c 20 74 68 65 20 70 61 67 65 72  alled, the pager
f2420 20 63 72 65 61 74 65 73 20 61 20 6e 65 77 0a 2a   creates a new.*
f2430 2a 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 61 63  * journal and ac
f2440 71 75 69 72 65 73 20 61 20 52 45 53 45 52 56 45  quires a RESERVE
f2450 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61  D lock on the da
f2460 74 61 62 61 73 65 2e 20 20 49 66 20 74 68 65 20  tabase.  If the 
f2470 52 45 53 45 52 56 45 44 0a 2a 2a 20 6c 6f 63 6b  RESERVED.** lock
f2480 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 63   could not be ac
f2490 71 75 69 72 65 64 2c 20 74 68 69 73 20 72 6f 75  quired, this rou
f24a0 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c  tine returns SQL
f24b0 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 0a 2a  ITE_BUSY.  The.*
f24c0 2a 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e  * calling routin
f24d0 65 20 6d 75 73 74 20 63 68 65 63 6b 20 66 6f 72  e must check for
f24e0 20 74 68 61 74 20 72 65 74 75 72 6e 20 76 61 6c   that return val
f24f0 75 65 20 61 6e 64 20 62 65 20 63 61 72 65 66 75  ue and be carefu
f2500 6c 20 6e 6f 74 20 74 6f 0a 2a 2a 20 63 68 61 6e  l not to.** chan
f2510 67 65 20 61 6e 79 20 70 61 67 65 20 64 61 74 61  ge any page data
f2520 20 75 6e 74 69 6c 20 74 68 69 73 20 72 6f 75 74   until this rout
f2530 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49  ine returns SQLI
f2540 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  TE_OK..**.** If 
f2550 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
f2560 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 77 72   could not be wr
f2570 69 74 74 65 6e 20 62 65 63 61 75 73 65 20 74 68  itten because th
f2580 65 20 64 69 73 6b 20 69 73 20 66 75 6c 6c 2c 0a  e disk is full,.
f2590 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  ** then this rou
f25a0 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c  tine returns SQL
f25b0 49 54 45 5f 46 55 4c 4c 20 61 6e 64 20 64 6f 65  ITE_FULL and doe
f25c0 73 20 61 6e 20 69 6d 6d 65 64 69 61 74 65 20 72  s an immediate r
f25d0 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 41 6c 6c 20  ollback..** All 
f25e0 73 75 62 73 65 71 75 65 6e 74 20 77 72 69 74 65  subsequent write
f25f0 20 61 74 74 65 6d 70 74 73 20 61 6c 73 6f 20 72   attempts also r
f2600 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c  eturn SQLITE_FUL
f2610 4c 20 75 6e 74 69 6c 20 74 68 65 72 65 0a 2a 2a  L until there.**
f2620 20 69 73 20 61 20 63 61 6c 6c 20 74 6f 20 73 71   is a call to sq
f2630 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74  lite3PagerCommit
f2640 28 29 20 6f 72 20 73 71 6c 69 74 65 33 50 61 67  () or sqlite3Pag
f2650 65 72 52 6f 6c 6c 62 61 63 6b 28 29 20 74 6f 0a  erRollback() to.
f2660 2a 2a 20 72 65 73 65 74 2e 0a 2a 2f 0a 73 74 61  ** reset..*/.sta
f2670 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 77 72  tic int pager_wr
f2680 69 74 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b  ite(PgHdr *pPg){
f2690 0a 20 20 76 6f 69 64 20 2a 70 44 61 74 61 20 3d  .  void *pData =
f26a0 20 50 47 48 44 52 5f 54 4f 5f 44 41 54 41 28 70   PGHDR_TO_DATA(p
f26b0 50 67 29 3b 0a 20 20 50 61 67 65 72 20 2a 70 50  Pg);.  Pager *pP
f26c0 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67  ager = pPg->pPag
f26d0 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  er;.  int rc = S
f26e0 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20  QLITE_OK;..  /* 
f26f0 43 68 65 63 6b 20 66 6f 72 20 65 72 72 6f 72 73  Check for errors
f2700 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67  .  */.  if( pPag
f2710 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 20 0a  er->errCode ){ .
f2720 20 20 20 20 72 65 74 75 72 6e 20 70 50 61 67 65      return pPage
f2730 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d 0a  r->errCode;.  }.
f2740 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 72 65    if( pPager->re
f2750 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 72 65  adOnly ){.    re
f2760 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d  turn SQLITE_PERM
f2770 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  ;.  }..  assert(
f2780 20 21 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73   !pPager->setMas
f2790 74 65 72 20 29 3b 0a 0a 20 20 43 48 45 43 4b 5f  ter );..  CHECK_
f27a0 50 41 47 45 28 70 50 67 29 3b 0a 0a 20 20 2f 2a  PAGE(pPg);..  /*
f27b0 20 49 66 20 74 68 69 73 20 70 61 67 65 20 77 61   If this page wa
f27c0 73 20 70 72 65 76 69 6f 75 73 6c 79 20 61 63 71  s previously acq
f27d0 75 69 72 65 64 20 77 69 74 68 20 6e 6f 43 6f 6e  uired with noCon
f27e0 74 65 6e 74 3d 3d 31 2c 20 74 68 61 74 20 6d 65  tent==1, that me
f27f0 61 6e 73 0a 20 20 2a 2a 20 77 65 20 64 69 64 6e  ans.  ** we didn
f2800 27 74 20 72 65 61 6c 6c 79 20 72 65 61 64 20 69  't really read i
f2810 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  n the content of
f2820 20 74 68 65 20 70 61 67 65 2e 20 20 54 68 69 73   the page.  This
f2830 20 63 61 6e 20 68 61 70 70 65 6e 0a 20 20 2a 2a   can happen.  **
f2840 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 29 20 77   (for example) w
f2850 68 65 6e 20 74 68 65 20 70 61 67 65 20 69 73 20  hen the page is 
f2860 62 65 69 6e 67 20 6d 6f 76 65 64 20 74 6f 20 74  being moved to t
f2870 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 42 75  he freelist.  Bu
f2880 74 0a 20 20 2a 2a 20 6e 6f 77 20 77 65 20 61 72  t.  ** now we ar
f2890 65 20 28 70 65 72 68 61 70 73 29 20 6d 6f 76 69  e (perhaps) movi
f28a0 6e 67 20 74 68 65 20 70 61 67 65 20 6f 66 66 20  ng the page off 
f28b0 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73 74 20  of the freelist 
f28c0 66 6f 72 0a 20 20 2a 2a 20 72 65 75 73 65 20 61  for.  ** reuse a
f28d0 6e 64 20 77 65 20 6e 65 65 64 20 74 6f 20 6b 6e  nd we need to kn
f28e0 6f 77 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20  ow its original 
f28f0 63 6f 6e 74 65 6e 74 20 73 6f 20 74 68 61 74 20  content so that 
f2900 63 6f 6e 74 65 6e 74 0a 20 20 2a 2a 20 63 61 6e  content.  ** can
f2910 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68   be stored in th
f2920 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  e rollback journ
f2930 61 6c 2e 20 20 53 6f 20 64 6f 20 74 68 65 20 72  al.  So do the r
f2940 65 61 64 20 61 74 20 74 68 69 73 0a 20 20 2a 2a  ead at this.  **
f2950 20 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20 20 72 63   time..  */.  rc
f2960 20 3d 20 70 61 67 65 72 5f 67 65 74 5f 63 6f 6e   = pager_get_con
f2970 74 65 6e 74 28 70 50 67 29 3b 0a 20 20 69 66 28  tent(pPg);.  if(
f2980 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72   rc ){.    retur
f2990 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  n rc;.  }..  /* 
f29a0 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73  Mark the page as
f29b0 20 64 69 72 74 79 2e 20 20 49 66 20 74 68 65 20   dirty.  If the 
f29c0 70 61 67 65 20 68 61 73 20 61 6c 72 65 61 64 79  page has already
f29d0 20 62 65 65 6e 20 77 72 69 74 74 65 6e 0a 20 20   been written.  
f29e0 2a 2a 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61  ** to the journa
f29f0 6c 20 74 68 65 6e 20 77 65 20 63 61 6e 20 72 65  l then we can re
f2a00 74 75 72 6e 20 72 69 67 68 74 20 61 77 61 79 2e  turn right away.
f2a10 0a 20 20 2a 2f 0a 20 20 6d 61 6b 65 44 69 72 74  .  */.  makeDirt
f2a20 79 28 70 50 67 29 3b 0a 20 20 69 66 28 20 70 50  y(pPg);.  if( pP
f2a30 67 2d 3e 69 6e 4a 6f 75 72 6e 61 6c 20 26 26 20  g->inJournal && 
f2a40 28 70 61 67 65 49 6e 53 74 61 74 65 6d 65 6e 74  (pageInStatement
f2a50 28 70 50 67 29 20 7c 7c 20 70 50 61 67 65 72 2d  (pPg) || pPager-
f2a60 3e 73 74 6d 74 49 6e 55 73 65 3d 3d 30 29 20 29  >stmtInUse==0) )
f2a70 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 69  {.    pPager->di
f2a80 72 74 79 43 61 63 68 65 20 3d 20 31 3b 0a 20 20  rtyCache = 1;.  
f2a90 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69    pPager->dbModi
f2aa0 66 69 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73  fied = 1;.  }els
f2ab0 65 7b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65  e{..    /* If we
f2ac0 20 67 65 74 20 74 68 69 73 20 66 61 72 2c 20 69   get this far, i
f2ad0 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  t means that the
f2ae0 20 70 61 67 65 20 6e 65 65 64 73 20 74 6f 20 62   page needs to b
f2af0 65 0a 20 20 20 20 2a 2a 20 77 72 69 74 74 65 6e  e.    ** written
f2b00 20 74 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74   to the transact
f2b10 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 74  ion journal or t
f2b20 68 65 20 63 6b 65 63 6b 70 6f 69 6e 74 20 6a 6f  he ckeckpoint jo
f2b30 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 6f 72 20  urnal.    ** or 
f2b40 62 6f 74 68 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  both..    **.   
f2b50 20 2a 2a 20 46 69 72 73 74 20 63 68 65 63 6b 20   ** First check 
f2b60 74 6f 20 73 65 65 20 74 68 61 74 20 74 68 65 20  to see that the 
f2b70 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72  transaction jour
f2b80 6e 61 6c 20 65 78 69 73 74 73 20 61 6e 64 0a 20  nal exists and. 
f2b90 20 20 20 2a 2a 20 63 72 65 61 74 65 20 69 74 20     ** create it 
f2ba0 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 2e 0a  if it does not..
f2bb0 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
f2bc0 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  t( pPager->state
f2bd0 21 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29  !=PAGER_UNLOCK )
f2be0 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
f2bf0 65 33 50 61 67 65 72 42 65 67 69 6e 28 70 50 67  e3PagerBegin(pPg
f2c00 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63  , 0);.    if( rc
f2c10 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
f2c20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
f2c30 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
f2c40 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
f2c50 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20  =PAGER_RESERVED 
f2c60 29 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67  );.    if( !pPag
f2c70 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20  er->journalOpen 
f2c80 26 26 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f  && pPager->useJo
f2c90 75 72 6e 61 6c 0a 20 20 20 20 20 20 20 20 20 20  urnal.          
f2ca0 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  && pPager->journ
f2cb0 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f  alMode!=PAGER_JO
f2cc0 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b  URNALMODE_OFF ){
f2cd0 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
f2ce0 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70  r_open_journal(p
f2cf0 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 69 66  Pager);.      if
f2d00 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
f2d10 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
f2d20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64   }.    pPager->d
f2d30 69 72 74 79 43 61 63 68 65 20 3d 20 31 3b 0a 20  irtyCache = 1;. 
f2d40 20 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64     pPager->dbMod
f2d50 69 66 69 65 64 20 3d 20 31 3b 0a 20 20 0a 20 20  ified = 1;.  .  
f2d60 20 20 2f 2a 20 54 68 65 20 74 72 61 6e 73 61 63    /* The transac
f2d70 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6e 6f 77  tion journal now
f2d80 20 65 78 69 73 74 73 20 61 6e 64 20 77 65 20 68   exists and we h
f2d90 61 76 65 20 61 20 52 45 53 45 52 56 45 44 20 6f  ave a RESERVED o
f2da0 72 20 61 6e 0a 20 20 20 20 2a 2a 20 45 58 43 4c  r an.    ** EXCL
f2db0 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68  USIVE lock on th
f2dc0 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
f2dd0 66 69 6c 65 2e 20 20 57 72 69 74 65 20 74 68 65  file.  Write the
f2de0 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 74 6f   current page to
f2df0 0a 20 20 20 20 2a 2a 20 74 68 65 20 74 72 61 6e  .    ** the tran
f2e00 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20  saction journal 
f2e10 69 66 20 69 74 20 69 73 20 6e 6f 74 20 74 68 65  if it is not the
f2e20 72 65 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20  re already..    
f2e30 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 50 67 2d  */.    if( !pPg-
f2e40 3e 69 6e 4a 6f 75 72 6e 61 6c 20 26 26 20 28 70  >inJournal && (p
f2e50 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70  Pager->journalOp
f2e60 65 6e 20 7c 7c 20 4d 45 4d 44 42 29 20 29 7b 0a  en || MEMDB) ){.
f2e70 20 20 20 20 20 20 69 66 28 20 28 69 6e 74 29 70        if( (int)p
f2e80 50 67 2d 3e 70 67 6e 6f 20 3c 3d 20 70 50 61 67  Pg->pgno <= pPag
f2e90 65 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65 20 29  er->origDbSize )
f2ea0 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 4d 45  {.        if( ME
f2eb0 4d 44 42 20 29 7b 0a 20 20 20 20 20 20 20 20 20  MDB ){.         
f2ec0 20 50 67 48 69 73 74 6f 72 79 20 2a 70 48 69 73   PgHistory *pHis
f2ed0 74 20 3d 20 50 47 48 44 52 5f 54 4f 5f 48 49 53  t = PGHDR_TO_HIS
f2ee0 54 28 70 50 67 2c 20 70 50 61 67 65 72 29 3b 0a  T(pPg, pPager);.
f2ef0 20 20 20 20 20 20 20 20 20 20 50 41 47 45 52 54            PAGERT
f2f00 52 41 43 45 33 28 22 4a 4f 55 52 4e 41 4c 20 25  RACE3("JOURNAL %
f2f10 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41  d page %d\n", PA
f2f20 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70  GERID(pPager), p
f2f30 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20  Pg->pgno);.     
f2f40 20 20 20 20 20 61 73 73 65 72 74 28 20 70 48 69       assert( pHi
f2f50 73 74 2d 3e 70 4f 72 69 67 3d 3d 30 20 29 3b 0a  st->pOrig==0 );.
f2f60 20 20 20 20 20 20 20 20 20 20 70 48 69 73 74 2d            pHist-
f2f70 3e 70 4f 72 69 67 20 3d 20 73 71 6c 69 74 65 33  >pOrig = sqlite3
f2f80 5f 6d 61 6c 6c 6f 63 28 20 70 50 61 67 65 72 2d  _malloc( pPager-
f2f90 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20  >pageSize );.   
f2fa0 20 20 20 20 20 20 20 69 66 28 20 21 70 48 69 73         if( !pHis
f2fb0 74 2d 3e 70 4f 72 69 67 20 29 7b 0a 20 20 20 20  t->pOrig ){.    
f2fc0 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
f2fd0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
f2fe0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
f2ff0 20 20 20 6d 65 6d 63 70 79 28 70 48 69 73 74 2d     memcpy(pHist-
f3000 3e 70 4f 72 69 67 2c 20 50 47 48 44 52 5f 54 4f  >pOrig, PGHDR_TO
f3010 5f 44 41 54 41 28 70 50 67 29 2c 20 70 50 61 67  _DATA(pPg), pPag
f3020 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20  er->pageSize);. 
f3030 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
f3040 20 20 20 20 20 20 20 20 75 33 32 20 63 6b 73 75          u32 cksu
f3050 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61  m;.          cha
f3060 72 20 2a 70 44 61 74 61 32 3b 0a 0a 20 20 20 20  r *pData2;..    
f3070 20 20 20 20 20 20 2f 2a 20 57 65 20 73 68 6f 75        /* We shou
f3080 6c 64 20 6e 65 76 65 72 20 77 72 69 74 65 20 74  ld never write t
f3090 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  o the journal fi
f30a0 6c 65 20 74 68 65 20 70 61 67 65 20 74 68 61 74  le the page that
f30b0 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f  .          ** co
f30c0 6e 74 61 69 6e 73 20 74 68 65 20 64 61 74 61 62  ntains the datab
f30d0 61 73 65 20 6c 6f 63 6b 73 2e 20 20 54 68 65 20  ase locks.  The 
f30e0 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74  following assert
f30f0 20 76 65 72 69 66 69 65 73 0a 20 20 20 20 20 20   verifies.      
f3100 20 20 20 20 2a 2a 20 74 68 61 74 20 77 65 20 64      ** that we d
f3110 6f 20 6e 6f 74 2e 20 2a 2f 0a 20 20 20 20 20 20  o not. */.      
f3120 20 20 20 20 61 73 73 65 72 74 28 20 70 50 67 2d      assert( pPg-
f3130 3e 70 67 6e 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f  >pgno!=PAGER_MJ_
f3140 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 3b 0a  PGNO(pPager) );.
f3150 20 20 20 20 20 20 20 20 20 20 70 44 61 74 61 32            pData2
f3160 20 3d 20 43 4f 44 45 43 32 28 70 50 61 67 65 72   = CODEC2(pPager
f3170 2c 20 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67  , pData, pPg->pg
f3180 6e 6f 2c 20 37 29 3b 0a 20 20 20 20 20 20 20 20  no, 7);.        
f3190 20 20 63 6b 73 75 6d 20 3d 20 70 61 67 65 72 5f    cksum = pager_
f31a0 63 6b 73 75 6d 28 70 50 61 67 65 72 2c 20 28 75  cksum(pPager, (u
f31b0 38 2a 29 70 44 61 74 61 32 29 3b 0a 20 20 20 20  8*)pData2);.    
f31c0 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65        rc = write
f31d0 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a  32bits(pPager->j
f31e0 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  fd, pPager->jour
f31f0 6e 61 6c 4f 66 66 2c 20 70 50 67 2d 3e 70 67 6e  nalOff, pPg->pgn
f3200 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  o);.          if
f3210 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
f3220 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  ){.            r
f3230 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69  c = sqlite3OsWri
f3240 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  te(pPager->jfd, 
f3250 70 44 61 74 61 32 2c 20 70 50 61 67 65 72 2d 3e  pData2, pPager->
f3260 70 61 67 65 53 69 7a 65 2c 0a 20 20 20 20 20 20  pageSize,.      
f3270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3280 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72            pPager
f3290 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 20 34  ->journalOff + 4
f32a0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  );.            p
f32b0 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
f32c0 66 20 2b 3d 20 70 50 61 67 65 72 2d 3e 70 61 67  f += pPager->pag
f32d0 65 53 69 7a 65 2b 34 3b 0a 20 20 20 20 20 20 20  eSize+4;.       
f32e0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69     }.          i
f32f0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
f3300 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
f3310 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 73  rc = write32bits
f3320 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50  (pPager->jfd, pP
f3330 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
f3340 2c 20 63 6b 73 75 6d 29 3b 0a 20 20 20 20 20 20  , cksum);.      
f3350 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f        pPager->jo
f3360 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 34 3b 0a 20  urnalOff += 4;. 
f3370 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
f3380 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a       IOTRACE(("J
f3390 4f 55 54 20 25 70 20 25 64 20 25 6c 6c 64 20 25  OUT %p %d %lld %
f33a0 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50  d\n", pPager, pP
f33b0 67 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 20 20  g->pgno, .      
f33c0 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61               pPa
f33d0 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c  ger->journalOff,
f33e0 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
f33f0 65 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 50  e));.          P
f3400 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65  AGER_INCR(sqlite
f3410 33 5f 70 61 67 65 72 5f 77 72 69 74 65 6a 5f 63  3_pager_writej_c
f3420 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20  ount);.         
f3430 20 50 41 47 45 52 54 52 41 43 45 35 28 22 4a 4f   PAGERTRACE5("JO
f3440 55 52 4e 41 4c 20 25 64 20 70 61 67 65 20 25 64  URNAL %d page %d
f3450 20 6e 65 65 64 53 79 6e 63 3d 25 64 20 68 61 73   needSync=%d has
f3460 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20  h(%08x)\n",.    
f3470 20 20 20 20 20 20 20 20 20 20 20 50 41 47 45 52             PAGER
f3480 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d  ID(pPager), pPg-
f3490 3e 70 67 6e 6f 2c 20 70 50 67 2d 3e 6e 65 65 64  >pgno, pPg->need
f34a0 53 79 6e 63 2c 20 70 61 67 65 72 5f 70 61 67 65  Sync, pager_page
f34b0 68 61 73 68 28 70 50 67 29 29 3b 0a 0a 20 20 20  hash(pPg));..   
f34c0 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 72 72         /* An err
f34d0 6f 72 20 68 61 73 20 6f 63 63 75 72 65 64 20 77  or has occured w
f34e0 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f  riting to the jo
f34f0 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 20  urnal file. The 
f3500 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 72  .          ** tr
f3510 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62  ansaction will b
f3520 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 62 79  e rolled back by
f3530 20 74 68 65 20 6c 61 79 65 72 20 61 62 6f 76 65   the layer above
f3540 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
f3550 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21           if( rc!
f3560 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
f3570 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
f3580 20 72 63 3b 0a 20 20 20 20 20 20 20 20 20 20 7d   rc;.          }
f3590 0a 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67  ..          pPag
f35a0 65 72 2d 3e 6e 52 65 63 2b 2b 3b 0a 20 20 20 20  er->nRec++;.    
f35b0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
f35c0 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c  ager->pInJournal
f35d0 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20  !=0 );.         
f35e0 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65   sqlite3BitvecSe
f35f0 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75  t(pPager->pInJou
f3600 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29  rnal, pPg->pgno)
f3610 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 67 2d  ;.          pPg-
f3620 3e 6e 65 65 64 53 79 6e 63 20 3d 20 21 70 50 61  >needSync = !pPa
f3630 67 65 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 20 20 20  ger->noSync;.   
f3640 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65         if( pPage
f3650 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 29 7b 0a  r->stmtInUse ){.
f3660 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
f3670 74 65 33 42 69 74 76 65 63 53 65 74 28 70 50 61  te3BitvecSet(pPa
f3680 67 65 72 2d 3e 70 49 6e 53 74 6d 74 2c 20 70 50  ger->pInStmt, pP
f3690 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20  g->pgno);.      
f36a0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
f36b0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
f36c0 20 20 20 20 20 70 50 67 2d 3e 6e 65 65 64 53 79       pPg->needSy
f36d0 6e 63 20 3d 20 21 70 50 61 67 65 72 2d 3e 6a 6f  nc = !pPager->jo
f36e0 75 72 6e 61 6c 53 74 61 72 74 65 64 20 26 26 20  urnalStarted && 
f36f0 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3b  !pPager->noSync;
f3700 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 54 52  .        PAGERTR
f3710 41 43 45 34 28 22 41 50 50 45 4e 44 20 25 64 20  ACE4("APPEND %d 
f3720 70 61 67 65 20 25 64 20 6e 65 65 64 53 79 6e 63  page %d needSync
f3730 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20  =%d\n",.        
f3740 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28          PAGERID(
f3750 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67  pPager), pPg->pg
f3760 6e 6f 2c 20 70 50 67 2d 3e 6e 65 65 64 53 79 6e  no, pPg->needSyn
f3770 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  c);.      }.    
f3780 20 20 69 66 28 20 70 50 67 2d 3e 6e 65 65 64 53    if( pPg->needS
f3790 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ync ){.        p
f37a0 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20  Pager->needSync 
f37b0 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
f37c0 20 20 20 70 50 67 2d 3e 69 6e 4a 6f 75 72 6e 61     pPg->inJourna
f37d0 6c 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 0a  l = 1;.    }.  .
f37e0 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 74      /* If the st
f37f0 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20  atement journal 
f3800 69 73 20 6f 70 65 6e 20 61 6e 64 20 74 68 65 20  is open and the 
f3810 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 69  page is not in i
f3820 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 77  t,.    ** then w
f3830 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74  rite the current
f3840 20 70 61 67 65 20 74 6f 20 74 68 65 20 73 74 61   page to the sta
f3850 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20  tement journal. 
f3860 20 4e 6f 74 65 20 74 68 61 74 0a 20 20 20 20 2a   Note that.    *
f3870 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  * the statement 
f3880 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 64  journal format d
f3890 69 66 66 65 72 73 20 66 72 6f 6d 20 74 68 65 20  iffers from the 
f38a0 73 74 61 6e 64 61 72 64 20 6a 6f 75 72 6e 61 6c  standard journal
f38b0 20 66 6f 72 6d 61 74 0a 20 20 20 20 2a 2a 20 69   format.    ** i
f38c0 6e 20 74 68 61 74 20 69 74 20 6f 6d 69 74 73 20  n that it omits 
f38d0 74 68 65 20 63 68 65 63 6b 73 75 6d 73 20 61 6e  the checksums an
f38e0 64 20 74 68 65 20 68 65 61 64 65 72 2e 0a 20 20  d the header..  
f38f0 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61    */.    if( pPa
f3900 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 0a  ger->stmtInUse .
f3910 20 20 20 20 20 26 26 20 21 70 61 67 65 49 6e 53       && !pageInS
f3920 74 61 74 65 6d 65 6e 74 28 70 50 67 29 20 0a 20  tatement(pPg) . 
f3930 20 20 20 20 26 26 20 28 69 6e 74 29 70 50 67 2d      && (int)pPg-
f3940 3e 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 73  >pgno<=pPager->s
f3950 74 6d 74 53 69 7a 65 20 0a 20 20 20 20 29 7b 0a  tmtSize .    ){.
f3960 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
f3970 67 2d 3e 69 6e 4a 6f 75 72 6e 61 6c 20 7c 7c 20  g->inJournal || 
f3980 28 69 6e 74 29 70 50 67 2d 3e 70 67 6e 6f 3e 70  (int)pPg->pgno>p
f3990 50 61 67 65 72 2d 3e 6f 72 69 67 44 62 53 69 7a  Pager->origDbSiz
f39a0 65 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 4d  e );.      if( M
f39b0 45 4d 44 42 20 29 7b 0a 20 20 20 20 20 20 20 20  EMDB ){.        
f39c0 50 67 48 69 73 74 6f 72 79 20 2a 70 48 69 73 74  PgHistory *pHist
f39d0 20 3d 20 50 47 48 44 52 5f 54 4f 5f 48 49 53 54   = PGHDR_TO_HIST
f39e0 28 70 50 67 2c 20 70 50 61 67 65 72 29 3b 0a 20  (pPg, pPager);. 
f39f0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
f3a00 48 69 73 74 2d 3e 70 53 74 6d 74 3d 3d 30 20 29  Hist->pStmt==0 )
f3a10 3b 0a 20 20 20 20 20 20 20 20 70 48 69 73 74 2d  ;.        pHist-
f3a20 3e 70 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33  >pStmt = sqlite3
f3a30 5f 6d 61 6c 6c 6f 63 28 20 70 50 61 67 65 72 2d  _malloc( pPager-
f3a40 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20  >pageSize );.   
f3a50 20 20 20 20 20 69 66 28 20 70 48 69 73 74 2d 3e       if( pHist->
f3a60 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 20 20 20  pStmt ){.       
f3a70 20 20 20 6d 65 6d 63 70 79 28 70 48 69 73 74 2d     memcpy(pHist-
f3a80 3e 70 53 74 6d 74 2c 20 50 47 48 44 52 5f 54 4f  >pStmt, PGHDR_TO
f3a90 5f 44 41 54 41 28 70 50 67 29 2c 20 70 50 61 67  _DATA(pPg), pPag
f3aa0 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20  er->pageSize);. 
f3ab0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
f3ac0 20 50 41 47 45 52 54 52 41 43 45 33 28 22 53 54   PAGERTRACE3("ST
f3ad0 4d 54 2d 4a 4f 55 52 4e 41 4c 20 25 64 20 70 61  MT-JOURNAL %d pa
f3ae0 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49  ge %d\n", PAGERI
f3af0 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e  D(pPager), pPg->
f3b00 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 70  pgno);.        p
f3b10 61 67 65 5f 61 64 64 5f 74 6f 5f 73 74 6d 74 5f  age_add_to_stmt_
f3b20 6c 69 73 74 28 70 50 67 29 3b 0a 20 20 20 20 20  list(pPg);.     
f3b30 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
f3b40 69 36 34 20 6f 66 66 73 65 74 20 3d 20 70 50 61  i64 offset = pPa
f3b50 67 65 72 2d 3e 73 74 6d 74 4e 52 65 63 2a 28 34  ger->stmtNRec*(4
f3b60 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a  +pPager->pageSiz
f3b70 65 29 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72  e);.        char
f3b80 20 2a 70 44 61 74 61 32 20 3d 20 43 4f 44 45 43   *pData2 = CODEC
f3b90 32 28 70 50 61 67 65 72 2c 20 70 44 61 74 61 2c  2(pPager, pData,
f3ba0 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 37 29 3b 0a   pPg->pgno, 7);.
f3bb0 20 20 20 20 20 20 20 20 72 63 20 3d 20 77 72 69          rc = wri
f3bc0 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d  te32bits(pPager-
f3bd0 3e 73 74 66 64 2c 20 6f 66 66 73 65 74 2c 20 70  >stfd, offset, p
f3be0 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20  Pg->pgno);.     
f3bf0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
f3c00 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
f3c10 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
f3c20 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 73 74  Write(pPager->st
f3c30 66 64 2c 20 70 44 61 74 61 32 2c 20 70 50 61 67  fd, pData2, pPag
f3c40 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66  er->pageSize, of
f3c50 66 73 65 74 2b 34 29 3b 0a 20 20 20 20 20 20 20  fset+4);.       
f3c60 20 7d 0a 20 20 20 20 20 20 20 20 50 41 47 45 52   }.        PAGER
f3c70 54 52 41 43 45 33 28 22 53 54 4d 54 2d 4a 4f 55  TRACE3("STMT-JOU
f3c80 52 4e 41 4c 20 25 64 20 70 61 67 65 20 25 64 5c  RNAL %d page %d\
f3c90 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67  n", PAGERID(pPag
f3ca0 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b  er), pPg->pgno);
f3cb0 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
f3cc0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
f3cd0 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
f3ce0 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  c;.        }.   
f3cf0 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 6d       pPager->stm
f3d00 74 4e 52 65 63 2b 2b 3b 0a 20 20 20 20 20 20 20  tNRec++;.       
f3d10 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
f3d20 3e 70 49 6e 53 74 6d 74 21 3d 30 20 29 3b 0a 20  >pInStmt!=0 );. 
f3d30 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 69         sqlite3Bi
f3d40 74 76 65 63 53 65 74 28 70 50 61 67 65 72 2d 3e  tvecSet(pPager->
f3d50 70 49 6e 53 74 6d 74 2c 20 70 50 67 2d 3e 70 67  pInStmt, pPg->pg
f3d60 6e 6f 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  no);.      }.   
f3d70 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64   }.  }..  /* Upd
f3d80 61 74 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ate the database
f3d90 20 73 69 7a 65 20 61 6e 64 20 72 65 74 75 72 6e   size and return
f3da0 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
f3db0 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d   pPager->state>=
f3dc0 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a  PAGER_SHARED );.
f3dd0 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62    if( pPager->db
f3de0 53 69 7a 65 3c 28 69 6e 74 29 70 50 67 2d 3e 70  Size<(int)pPg->p
f3df0 67 6e 6f 20 29 7b 0a 20 20 20 20 70 50 61 67 65  gno ){.    pPage
f3e00 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70 50 67 2d  r->dbSize = pPg-
f3e10 3e 70 67 6e 6f 3b 0a 20 20 20 20 69 66 28 20 21  >pgno;.    if( !
f3e20 4d 45 4d 44 42 20 26 26 20 70 50 61 67 65 72 2d  MEMDB && pPager-
f3e30 3e 64 62 53 69 7a 65 3d 3d 50 45 4e 44 49 4e 47  >dbSize==PENDING
f3e40 5f 42 59 54 45 2f 70 50 61 67 65 72 2d 3e 70 61  _BYTE/pPager->pa
f3e50 67 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  geSize ){.      
f3e60 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 2b 2b  pPager->dbSize++
f3e70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
f3e80 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
f3e90 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
f3ea0 69 73 20 75 73 65 64 20 74 6f 20 6d 61 72 6b 20  is used to mark 
f3eb0 61 20 64 61 74 61 2d 70 61 67 65 20 61 73 20 77  a data-page as w
f3ec0 72 69 74 61 62 6c 65 2e 20 49 74 20 75 73 65 73  ritable. It uses
f3ed0 20 0a 2a 2a 20 70 61 67 65 72 5f 77 72 69 74 65   .** pager_write
f3ee0 28 29 20 74 6f 20 6f 70 65 6e 20 61 20 6a 6f 75  () to open a jou
f3ef0 72 6e 61 6c 20 66 69 6c 65 20 28 69 66 20 69 74  rnal file (if it
f3f00 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20   is not already 
f3f10 6f 70 65 6e 29 0a 2a 2a 20 61 6e 64 20 77 72 69  open).** and wri
f3f20 74 65 20 74 68 65 20 70 61 67 65 20 2a 70 44 61  te the page *pDa
f3f30 74 61 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61  ta to the journa
f3f40 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 66  l..**.** The dif
f3f50 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20  ference between 
f3f60 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6e  this function an
f3f70 64 20 70 61 67 65 72 5f 77 72 69 74 65 28 29 20  d pager_write() 
f3f80 69 73 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20  is that this.** 
f3f90 66 75 6e 63 74 69 6f 6e 20 61 6c 73 6f 20 64 65  function also de
f3fa0 61 6c 73 20 77 69 74 68 20 74 68 65 20 73 70 65  als with the spe
f3fb0 63 69 61 6c 20 63 61 73 65 20 77 68 65 72 65 20  cial case where 
f3fc0 32 20 6f 72 20 6d 6f 72 65 20 70 61 67 65 73 0a  2 or more pages.
f3fd0 2a 2a 20 66 69 74 20 6f 6e 20 61 20 73 69 6e 67  ** fit on a sing
f3fe0 6c 65 20 64 69 73 6b 20 73 65 63 74 6f 72 2e 20  le disk sector. 
f3ff0 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 6c 6c  In this case all
f4000 20 63 6f 2d 72 65 73 69 64 65 6e 74 20 70 61 67   co-resident pag
f4010 65 73 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65 20  es.** must have 
f4020 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20  been written to 
f4030 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
f4040 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
f4050 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  g..*/.SQLITE_PRI
f4060 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
f4070 50 61 67 65 72 57 72 69 74 65 28 44 62 50 61 67  PagerWrite(DbPag
f4080 65 20 2a 70 44 62 50 61 67 65 29 7b 0a 20 20 69  e *pDbPage){.  i
f4090 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
f40a0 4b 3b 0a 0a 20 20 50 67 48 64 72 20 2a 70 50 67  K;..  PgHdr *pPg
f40b0 20 3d 20 70 44 62 50 61 67 65 3b 0a 20 20 50 61   = pDbPage;.  Pa
f40c0 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50  ger *pPager = pP
f40d0 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 50 67 6e  g->pPager;.  Pgn
f40e0 6f 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72  o nPagePerSector
f40f0 20 3d 20 28 70 50 61 67 65 72 2d 3e 73 65 63 74   = (pPager->sect
f4100 6f 72 53 69 7a 65 2f 70 50 61 67 65 72 2d 3e 70  orSize/pPager->p
f4110 61 67 65 53 69 7a 65 29 3b 0a 0a 20 20 70 61 67  ageSize);..  pag
f4120 65 72 45 6e 74 65 72 28 70 50 61 67 65 72 29 3b  erEnter(pPager);
f4130 0a 20 20 69 66 28 20 21 4d 45 4d 44 42 20 26 26  .  if( !MEMDB &&
f4140 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 3e   nPagePerSector>
f4150 31 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 50  1 ){.    Pgno nP
f4160 61 67 65 43 6f 75 6e 74 3b 20 20 20 20 20 20 20  ageCount;       
f4170 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62     /* Total numb
f4180 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 64  er of pages in d
f4190 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a  atabase file */.
f41a0 20 20 20 20 50 67 6e 6f 20 70 67 31 3b 20 20 20      Pgno pg1;   
f41b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
f41c0 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20 74   First page of t
f41d0 68 65 20 73 65 63 74 6f 72 20 70 50 67 20 69 73  he sector pPg is
f41e0 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 20 2a 2f 0a   located on. */.
f41f0 20 20 20 20 69 6e 74 20 6e 50 61 67 65 3b 20 20      int nPage;  
f4200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
f4210 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   Number of pages
f4220 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 67 31   starting at pg1
f4230 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20   to journal */. 
f4240 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 69     int ii;.    i
f4250 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b  nt needSync = 0;
f4260 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65  ..    /* Set the
f4270 20 64 6f 4e 6f 74 53 79 6e 63 20 66 6c 61 67 20   doNotSync flag 
f4280 74 6f 20 31 2e 20 54 68 69 73 20 69 73 20 62 65  to 1. This is be
f4290 63 61 75 73 65 20 77 65 20 63 61 6e 6e 6f 74 20  cause we cannot 
f42a0 61 6c 6c 6f 77 20 61 20 6a 6f 75 72 6e 61 6c 0a  allow a journal.
f42b0 20 20 20 20 2a 2a 20 68 65 61 64 65 72 20 74 6f      ** header to
f42c0 20 62 65 20 77 72 69 74 74 65 6e 20 62 65 74 77   be written betw
f42d0 65 65 6e 20 74 68 65 20 70 61 67 65 73 20 6a 6f  een the pages jo
f42e0 75 72 6e 61 6c 65 64 20 62 79 20 74 68 69 73 20  urnaled by this 
f42f0 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f  function..    */
f4300 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
f4310 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 3d 3d  ger->doNotSync==
f4320 30 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  0 );.    pPager-
f4330 3e 64 6f 4e 6f 74 53 79 6e 63 20 3d 20 31 3b 0a  >doNotSync = 1;.
f4340 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 74 72 69  .    /* This tri
f4350 63 6b 20 61 73 73 75 6d 65 73 20 74 68 61 74 20  ck assumes that 
f4360 62 6f 74 68 20 74 68 65 20 70 61 67 65 2d 73 69  both the page-si
f4370 7a 65 20 61 6e 64 20 73 65 63 74 6f 72 2d 73 69  ze and sector-si
f4380 7a 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 61 6e  ze are.    ** an
f4390 20 69 6e 74 65 67 65 72 20 70 6f 77 65 72 20 6f   integer power o
f43a0 66 20 32 2e 20 49 74 20 73 65 74 73 20 76 61 72  f 2. It sets var
f43b0 69 61 62 6c 65 20 70 67 31 20 74 6f 20 74 68 65  iable pg1 to the
f43c0 20 69 64 65 6e 74 69 66 69 65 72 0a 20 20 20 20   identifier.    
f43d0 2a 2a 20 6f 66 20 74 68 65 20 66 69 72 73 74 20  ** of the first 
f43e0 70 61 67 65 20 6f 66 20 74 68 65 20 73 65 63 74  page of the sect
f43f0 6f 72 20 70 50 67 20 69 73 20 6c 6f 63 61 74 65  or pPg is locate
f4400 64 20 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  d on..    */.   
f4410 20 70 67 31 20 3d 20 28 28 70 50 67 2d 3e 70 67   pg1 = ((pPg->pg
f4420 6e 6f 2d 31 29 20 26 20 7e 28 6e 50 61 67 65 50  no-1) & ~(nPageP
f4430 65 72 53 65 63 74 6f 72 2d 31 29 29 20 2b 20 31  erSector-1)) + 1
f4440 3b 0a 0a 20 20 20 20 6e 50 61 67 65 43 6f 75 6e  ;..    nPageCoun
f4450 74 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  t = sqlite3Pager
f4460 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72  Pagecount(pPager
f4470 29 3b 0a 20 20 20 20 69 66 28 20 70 50 67 2d 3e  );.    if( pPg->
f4480 70 67 6e 6f 3e 6e 50 61 67 65 43 6f 75 6e 74 20  pgno>nPageCount 
f4490 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d  ){.      nPage =
f44a0 20 28 70 50 67 2d 3e 70 67 6e 6f 20 2d 20 70 67   (pPg->pgno - pg
f44b0 31 29 2b 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20  1)+1;.    }else 
f44c0 69 66 28 20 28 70 67 31 2b 6e 50 61 67 65 50 65  if( (pg1+nPagePe
f44d0 72 53 65 63 74 6f 72 2d 31 29 3e 6e 50 61 67 65  rSector-1)>nPage
f44e0 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20 20 6e  Count ){.      n
f44f0 50 61 67 65 20 3d 20 6e 50 61 67 65 43 6f 75 6e  Page = nPageCoun
f4500 74 2b 31 2d 70 67 31 3b 0a 20 20 20 20 7d 65 6c  t+1-pg1;.    }el
f4510 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20  se{.      nPage 
f4520 3d 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72  = nPagePerSector
f4530 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
f4540 72 74 28 6e 50 61 67 65 3e 30 29 3b 0a 20 20 20  rt(nPage>0);.   
f4550 20 61 73 73 65 72 74 28 70 67 31 3c 3d 70 50 67   assert(pg1<=pPg
f4560 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 61 73 73  ->pgno);.    ass
f4570 65 72 74 28 28 70 67 31 2b 6e 50 61 67 65 29 3e  ert((pg1+nPage)>
f4580 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 20  pPg->pgno);..   
f4590 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 50   for(ii=0; ii<nP
f45a0 61 67 65 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  age && rc==SQLIT
f45b0 45 5f 4f 4b 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  E_OK; ii++){.   
f45c0 20 20 20 50 67 6e 6f 20 70 67 20 3d 20 70 67 31     Pgno pg = pg1
f45d0 2b 69 69 3b 0a 20 20 20 20 20 20 50 67 48 64 72  +ii;.      PgHdr
f45e0 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 20 20 69   *pPage;.      i
f45f0 66 28 20 70 67 3d 3d 70 50 67 2d 3e 70 67 6e 6f  f( pg==pPg->pgno
f4600 20 7c 7c 20 21 73 71 6c 69 74 65 33 42 69 74 76   || !sqlite3Bitv
f4610 65 63 54 65 73 74 28 70 50 61 67 65 72 2d 3e 70  ecTest(pPager->p
f4620 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 29 20 29  InJournal, pg) )
f4630 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 67  {.        if( pg
f4640 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28  !=PAGER_MJ_PGNO(
f4650 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20  pPager) ){.     
f4660 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
f4670 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72  3PagerGet(pPager
f4680 2c 20 70 67 2c 20 26 70 50 61 67 65 29 3b 0a 20  , pg, &pPage);. 
f4690 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d           if( rc=
f46a0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
f46b0 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 70            rc = p
f46c0 61 67 65 72 5f 77 72 69 74 65 28 70 50 61 67 65  ager_write(pPage
f46d0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  );.            i
f46e0 66 28 20 70 50 61 67 65 2d 3e 6e 65 65 64 53 79  f( pPage->needSy
f46f0 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  nc ){.          
f4700 20 20 20 20 6e 65 65 64 53 79 6e 63 20 3d 20 31      needSync = 1
f4710 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
f4720 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
f4730 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50  te3PagerUnref(pP
f4740 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  age);.          
f4750 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
f4760 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 50 61    }else if( (pPa
f4770 67 65 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75  ge = pager_looku
f4780 70 28 70 50 61 67 65 72 2c 20 70 67 29 29 21 3d  p(pPager, pg))!=
f4790 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  0 ){.        if(
f47a0 20 70 50 61 67 65 2d 3e 6e 65 65 64 53 79 6e 63   pPage->needSync
f47b0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 65   ){.          ne
f47c0 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20  edSync = 1;.    
f47d0 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
f47e0 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74    }..    /* If t
f47f0 68 65 20 50 67 48 64 72 2e 6e 65 65 64 53 79 6e  he PgHdr.needSyn
f4800 63 20 66 6c 61 67 20 69 73 20 73 65 74 20 66 6f  c flag is set fo
f4810 72 20 61 6e 79 20 6f 66 20 74 68 65 20 6e 50 61  r any of the nPa
f4820 67 65 20 70 61 67 65 73 20 0a 20 20 20 20 2a 2a  ge pages .    **
f4830 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 67 31   starting at pg1
f4840 2c 20 74 68 65 6e 20 69 74 20 6e 65 65 64 73 20  , then it needs 
f4850 74 6f 20 62 65 20 73 65 74 20 66 6f 72 20 61 6c  to be set for al
f4860 6c 20 6f 66 20 74 68 65 6d 2e 20 42 65 63 61 75  l of them. Becau
f4870 73 65 0a 20 20 20 20 2a 2a 20 77 72 69 74 69 6e  se.    ** writin
f4880 67 20 74 6f 20 61 6e 79 20 6f 66 20 74 68 65 73  g to any of thes
f4890 65 20 6e 50 61 67 65 20 70 61 67 65 73 20 6d 61  e nPage pages ma
f48a0 79 20 64 61 6d 61 67 65 20 74 68 65 20 6f 74 68  y damage the oth
f48b0 65 72 73 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20  ers, the.    ** 
f48c0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73  journal file mus
f48d0 74 20 63 6f 6e 74 61 69 6e 20 73 79 6e 63 28 29  t contain sync()
f48e0 65 64 20 63 6f 70 69 65 73 20 6f 66 20 61 6c 6c  ed copies of all
f48f0 20 6f 66 20 74 68 65 6d 0a 20 20 20 20 2a 2a 20   of them.    ** 
f4900 62 65 66 6f 72 65 20 61 6e 79 20 6f 66 20 74 68  before any of th
f4910 65 6d 20 63 61 6e 20 62 65 20 77 72 69 74 74 65  em can be writte
f4920 6e 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74  n out to the dat
f4930 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20  abase file..    
f4940 2a 2f 0a 20 20 20 20 69 66 28 20 6e 65 65 64 53  */.    if( needS
f4950 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 66 6f 72  ync ){.      for
f4960 28 69 69 3d 30 3b 20 69 69 3c 6e 50 61 67 65 20  (ii=0; ii<nPage 
f4970 26 26 20 6e 65 65 64 53 79 6e 63 3b 20 69 69 2b  && needSync; ii+
f4980 2b 29 7b 0a 20 20 20 20 20 20 20 20 50 67 48 64  +){.        PgHd
f4990 72 20 2a 70 50 61 67 65 20 3d 20 70 61 67 65 72  r *pPage = pager
f49a0 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20  _lookup(pPager, 
f49b0 70 67 31 2b 69 69 29 3b 0a 20 20 20 20 20 20 20  pg1+ii);.       
f49c0 20 69 66 28 20 70 50 61 67 65 20 29 20 70 50 61   if( pPage ) pPa
f49d0 67 65 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 31  ge->needSync = 1
f49e0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
f49f0 61 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e 6e  assert(pPager->n
f4a00 65 65 64 53 79 6e 63 29 3b 0a 20 20 20 20 7d 0a  eedSync);.    }.
f4a10 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
f4a20 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 3d 3d  ger->doNotSync==
f4a30 31 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  1 );.    pPager-
f4a40 3e 64 6f 4e 6f 74 53 79 6e 63 20 3d 20 30 3b 0a  >doNotSync = 0;.
f4a50 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
f4a60 3d 20 70 61 67 65 72 5f 77 72 69 74 65 28 70 44  = pager_write(pD
f4a70 62 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 70 61  bPage);.  }.  pa
f4a80 67 65 72 4c 65 61 76 65 28 70 50 61 67 65 72 29  gerLeave(pPager)
f4a90 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
f4aa0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ../*.** Return T
f4ab0 52 55 45 20 69 66 20 74 68 65 20 70 61 67 65 20  RUE if the page 
f4ac0 67 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67  given in the arg
f4ad0 75 6d 65 6e 74 20 77 61 73 20 70 72 65 76 69 6f  ument was previo
f4ae0 75 73 6c 79 20 70 61 73 73 65 64 0a 2a 2a 20 74  usly passed.** t
f4af0 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  o sqlite3PagerWr
f4b00 69 74 65 28 29 2e 20 20 49 6e 20 6f 74 68 65 72  ite().  In other
f4b10 20 77 6f 72 64 73 2c 20 72 65 74 75 72 6e 20 54   words, return T
f4b20 52 55 45 20 69 66 20 69 74 20 69 73 20 6f 6b 0a  RUE if it is ok.
f4b30 2a 2a 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65  ** to change the
f4b40 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20   content of the 
f4b50 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  page..*/.#ifndef
f4b60 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50   NDEBUG.SQLITE_P
f4b70 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
f4b80 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
f4b90 6c 65 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b  le(DbPage *pPg){
f4ba0 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 64  .  return pPg->d
f4bb0 69 72 74 79 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  irty;.}.#endif..
f4bc0 2f 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20  /*.** A call to 
f4bd0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 65 6c  this routine tel
f4be0 6c 73 20 74 68 65 20 70 61 67 65 72 20 74 68 61  ls the pager tha
f4bf0 74 20 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 65  t it is not nece
f4c00 73 73 61 72 79 20 74 6f 0a 2a 2a 20 77 72 69 74  ssary to.** writ
f4c10 65 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f  e the informatio
f4c20 6e 20 6f 6e 20 70 61 67 65 20 70 50 67 20 62 61  n on page pPg ba
f4c30 63 6b 20 74 6f 20 74 68 65 20 64 69 73 6b 2c 20  ck to the disk, 
f4c40 65 76 65 6e 20 74 68 6f 75 67 68 0a 2a 2a 20 74  even though.** t
f4c50 68 61 74 20 70 61 67 65 20 6d 69 67 68 74 20 62  hat page might b
f4c60 65 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74  e marked as dirt
f4c70 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 76 65  y..**.** The ove
f4c80 72 6c 79 69 6e 67 20 73 6f 66 74 77 61 72 65 20  rlying software 
f4c90 6c 61 79 65 72 20 63 61 6c 6c 73 20 74 68 69 73  layer calls this
f4ca0 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 61 6c   routine when al
f4cb0 6c 20 6f 66 20 74 68 65 20 64 61 74 61 0a 2a 2a  l of the data.**
f4cc0 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 70 61   on the given pa
f4cd0 67 65 20 69 73 20 75 6e 75 73 65 64 2e 20 20 54  ge is unused.  T
f4ce0 68 65 20 70 61 67 65 72 20 6d 61 72 6b 73 20 74  he pager marks t
f4cf0 68 65 20 70 61 67 65 20 61 73 20 63 6c 65 61 6e  he page as clean
f4d00 20 73 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 64   so.** that it d
f4d10 6f 65 73 20 6e 6f 74 20 67 65 74 20 77 72 69 74  oes not get writ
f4d20 74 65 6e 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a  ten to disk..**.
f4d30 2a 2a 20 54 65 73 74 73 20 73 68 6f 77 20 74 68  ** Tests show th
f4d40 61 74 20 74 68 69 73 20 6f 70 74 69 6d 69 7a 61  at this optimiza
f4d50 74 69 6f 6e 2c 20 74 6f 67 65 74 68 65 72 20 77  tion, together w
f4d60 69 74 68 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74  ith the.** sqlit
f4d70 65 33 50 61 67 65 72 44 6f 6e 74 52 6f 6c 6c 62  e3PagerDontRollb
f4d80 61 63 6b 28 29 20 62 65 6c 6f 77 2c 20 6d 6f 72  ack() below, mor
f4d90 65 20 74 68 61 6e 20 64 6f 75 62 6c 65 20 74 68  e than double th
f4da0 65 20 73 70 65 65 64 0a 2a 2a 20 6f 66 20 6c 61  e speed.** of la
f4db0 72 67 65 20 49 4e 53 45 52 54 20 6f 70 65 72 61  rge INSERT opera
f4dc0 74 69 6f 6e 73 20 61 6e 64 20 71 75 61 64 72 75  tions and quadru
f4dd0 70 6c 65 20 74 68 65 20 73 70 65 65 64 20 6f 66  ple the speed of
f4de0 20 6c 61 72 67 65 20 44 45 4c 45 54 45 73 2e 0a   large DELETEs..
f4df0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20  **.** When this 
f4e00 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
f4e10 64 2c 20 73 65 74 20 74 68 65 20 61 6c 77 61 79  d, set the alway
f4e20 73 52 6f 6c 6c 62 61 63 6b 20 66 6c 61 67 20 74  sRollback flag t
f4e30 6f 20 74 72 75 65 2e 0a 2a 2a 20 53 75 62 73 65  o true..** Subse
f4e40 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73  quent calls to s
f4e50 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 52  qlite3PagerDontR
f4e60 6f 6c 6c 62 61 63 6b 28 29 20 66 6f 72 20 74 68  ollback() for th
f4e70 65 20 73 61 6d 65 20 70 61 67 65 0a 2a 2a 20 77  e same page.** w
f4e80 69 6c 6c 20 74 68 65 72 65 61 66 74 65 72 20 62  ill thereafter b
f4e90 65 20 69 67 6e 6f 72 65 64 2e 20 20 54 68 69 73  e ignored.  This
f4ea0 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 74 6f   is necessary to
f4eb0 20 61 76 6f 69 64 20 61 20 70 72 6f 62 6c 65 6d   avoid a problem
f4ec0 0a 2a 2a 20 77 68 65 72 65 20 61 20 70 61 67 65  .** where a page
f4ed0 20 77 69 74 68 20 64 61 74 61 20 69 73 20 61 64   with data is ad
f4ee0 64 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 6c  ded to the freel
f4ef0 69 73 74 20 64 75 72 69 6e 67 20 6f 6e 65 20 70  ist during one p
f4f00 61 72 74 20 6f 66 0a 2a 2a 20 61 20 74 72 61 6e  art of.** a tran
f4f10 73 61 63 74 69 6f 6e 20 74 68 65 6e 20 72 65 6d  saction then rem
f4f20 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 66 72  oved from the fr
f4f30 65 65 6c 69 73 74 20 64 75 72 69 6e 67 20 61 20  eelist during a 
f4f40 6c 61 74 65 72 20 70 61 72 74 0a 2a 2a 20 6f 66  later part.** of
f4f50 20 74 68 65 20 73 61 6d 65 20 74 72 61 6e 73 61   the same transa
f4f60 63 74 69 6f 6e 20 61 6e 64 20 72 65 75 73 65 64  ction and reused
f4f70 20 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20   for some other 
f4f80 70 75 72 70 6f 73 65 2e 20 20 57 68 65 6e 20 69  purpose.  When i
f4f90 74 0a 2a 2a 20 69 73 20 66 69 72 73 74 20 61 64  t.** is first ad
f4fa0 64 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 6c  ded to the freel
f4fb0 69 73 74 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  ist, this routin
f4fc0 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 57 68  e is called.  Wh
f4fd0 65 6e 20 72 65 75 73 65 64 2c 0a 2a 2a 20 74 68  en reused,.** th
f4fe0 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f  e sqlite3PagerDo
f4ff0 6e 74 52 6f 6c 6c 62 61 63 6b 28 29 20 72 6f 75  ntRollback() rou
f5000 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20  tine is called. 
f5010 20 42 75 74 20 62 65 63 61 75 73 65 20 74 68 65   But because the
f5020 0a 2a 2a 20 70 61 67 65 20 63 6f 6e 74 61 69 6e  .** page contain
f5030 73 20 63 72 69 74 69 63 61 6c 20 64 61 74 61 2c  s critical data,
f5040 20 77 65 20 73 74 69 6c 6c 20 6e 65 65 64 20 74   we still need t
f5050 6f 20 62 65 20 73 75 72 65 20 69 74 20 67 65 74  o be sure it get
f5060 73 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b  s.** rolled back
f5070 20 69 6e 20 73 70 69 74 65 20 6f 66 20 74 68 65   in spite of the
f5080 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e   sqlite3PagerDon
f5090 74 52 6f 6c 6c 62 61 63 6b 28 29 20 63 61 6c 6c  tRollback() call
f50a0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
f50b0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
f50c0 50 61 67 65 72 44 6f 6e 74 57 72 69 74 65 28 44  PagerDontWrite(D
f50d0 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 29 7b  bPage *pDbPage){
f50e0 0a 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20  .  PgHdr *pPg = 
f50f0 70 44 62 50 61 67 65 3b 0a 20 20 50 61 67 65 72  pDbPage;.  Pager
f5100 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e   *pPager = pPg->
f5110 70 50 61 67 65 72 3b 0a 0a 20 20 69 66 28 20 4d  pPager;..  if( M
f5120 45 4d 44 42 20 29 20 72 65 74 75 72 6e 3b 0a 20  EMDB ) return;. 
f5130 20 70 61 67 65 72 45 6e 74 65 72 28 70 50 61 67   pagerEnter(pPag
f5140 65 72 29 3b 0a 20 20 70 50 67 2d 3e 61 6c 77 61  er);.  pPg->alwa
f5150 79 73 52 6f 6c 6c 62 61 63 6b 20 3d 20 31 3b 0a  ysRollback = 1;.
f5160 20 20 69 66 28 20 70 50 67 2d 3e 64 69 72 74 79    if( pPg->dirty
f5170 20 26 26 20 21 70 50 61 67 65 72 2d 3e 73 74 6d   && !pPager->stm
f5180 74 49 6e 55 73 65 20 29 7b 0a 20 20 20 20 61 73  tInUse ){.    as
f5190 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74  sert( pPager->st
f51a0 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45  ate>=PAGER_SHARE
f51b0 44 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61  D );.    if( pPa
f51c0 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 28 69 6e  ger->dbSize==(in
f51d0 74 29 70 50 67 2d 3e 70 67 6e 6f 20 26 26 20 70  t)pPg->pgno && p
f51e0 50 61 67 65 72 2d 3e 6f 72 69 67 44 62 53 69 7a  Pager->origDbSiz
f51f0 65 3c 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  e<pPager->dbSize
f5200 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20   ){.      /* If 
f5210 74 68 69 73 20 70 61 67 65 73 20 69 73 20 74 68  this pages is th
f5220 65 20 6c 61 73 74 20 70 61 67 65 20 69 6e 20 74  e last page in t
f5230 68 65 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20  he file and the 
f5240 66 69 6c 65 20 68 61 73 20 67 72 6f 77 6e 0a 20  file has grown. 
f5250 20 20 20 20 20 2a 2a 20 64 75 72 69 6e 67 20 74       ** during t
f5260 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73  he current trans
f5270 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 64 6f 20  action, then do 
f5280 4e 4f 54 20 6d 61 72 6b 20 74 68 65 20 70 61 67  NOT mark the pag
f5290 65 20 61 73 20 63 6c 65 61 6e 2e 0a 20 20 20 20  e as clean..    
f52a0 20 20 2a 2a 20 57 68 65 6e 20 74 68 65 20 64 61    ** When the da
f52b0 74 61 62 61 73 65 20 66 69 6c 65 20 67 72 6f 77  tabase file grow
f52c0 73 2c 20 77 65 20 6d 75 73 74 20 6d 61 6b 65 20  s, we must make 
f52d0 73 75 72 65 20 74 68 61 74 20 74 68 65 20 6c 61  sure that the la
f52e0 73 74 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a  st page.      **
f52f0 20 67 65 74 73 20 77 72 69 74 74 65 6e 20 61 74   gets written at
f5300 20 6c 65 61 73 74 20 6f 6e 63 65 20 73 6f 20 74   least once so t
f5310 68 61 74 20 74 68 65 20 64 69 73 6b 20 66 69 6c  hat the disk fil
f5320 65 20 77 69 6c 6c 20 62 65 20 74 68 65 20 63 6f  e will be the co
f5330 72 72 65 63 74 0a 20 20 20 20 20 20 2a 2a 20 73  rrect.      ** s
f5340 69 7a 65 2e 20 49 66 20 79 6f 75 20 64 6f 20 6e  ize. If you do n
f5350 6f 74 20 77 72 69 74 65 20 74 68 69 73 20 70 61  ot write this pa
f5360 67 65 20 61 6e 64 20 74 68 65 20 73 69 7a 65 20  ge and the size 
f5370 6f 66 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20  of the file.    
f5380 20 20 2a 2a 20 6f 6e 20 74 68 65 20 64 69 73 6b    ** on the disk
f5390 20 65 6e 64 73 20 75 70 20 62 65 69 6e 67 20 74   ends up being t
f53a0 6f 6f 20 73 6d 61 6c 6c 2c 20 74 68 61 74 20 63  oo small, that c
f53b0 61 6e 20 6c 65 61 64 20 74 6f 20 64 61 74 61 62  an lead to datab
f53c0 61 73 65 0a 20 20 20 20 20 20 2a 2a 20 63 6f 72  ase.      ** cor
f53d0 72 75 70 74 69 6f 6e 20 64 75 72 69 6e 67 20 74  ruption during t
f53e0 68 65 20 6e 65 78 74 20 74 72 61 6e 73 61 63 74  he next transact
f53f0 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ion..      */.  
f5400 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 50    }else{.      P
f5410 41 47 45 52 54 52 41 43 45 33 28 22 44 4f 4e 54  AGERTRACE3("DONT
f5420 5f 57 52 49 54 45 20 70 61 67 65 20 25 64 20 6f  _WRITE page %d o
f5430 66 20 25 64 5c 6e 22 2c 20 70 50 67 2d 3e 70 67  f %d\n", pPg->pg
f5440 6e 6f 2c 20 50 41 47 45 52 49 44 28 70 50 61 67  no, PAGERID(pPag
f5450 65 72 29 29 3b 0a 20 20 20 20 20 20 49 4f 54 52  er));.      IOTR
f5460 41 43 45 28 28 22 43 4c 45 41 4e 20 25 70 20 25  ACE(("CLEAN %p %
f5470 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50  d\n", pPager, pP
f5480 67 2d 3e 70 67 6e 6f 29 29 0a 20 20 20 20 20 20  g->pgno)).      
f5490 6d 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a  makeClean(pPg);.
f54a0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48  #ifdef SQLITE_CH
f54b0 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 20 20  ECK_PAGES.      
f54c0 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 3d 20  pPg->pageHash = 
f54d0 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70  pager_pagehash(p
f54e0 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  Pg);.#endif.    
f54f0 7d 0a 20 20 7d 0a 20 20 70 61 67 65 72 4c 65 61  }.  }.  pagerLea
f5500 76 65 28 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f  ve(pPager);.}../
f5510 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 74  *.** A call to t
f5520 68 69 73 20 72 6f 75 74 69 6e 65 20 74 65 6c 6c  his routine tell
f5530 73 20 74 68 65 20 70 61 67 65 72 20 74 68 61 74  s the pager that
f5540 20 69 66 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f   if a rollback o
f5550 63 63 75 72 73 2c 0a 2a 2a 20 69 74 20 69 73 20  ccurs,.** it is 
f5560 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20 74 6f  not necessary to
f5570 20 72 65 73 74 6f 72 65 20 74 68 65 20 64 61 74   restore the dat
f5580 61 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 70  a on the given p
f5590 61 67 65 2e 20 20 54 68 69 73 0a 2a 2a 20 6d 65  age.  This.** me
f55a0 61 6e 73 20 74 68 61 74 20 74 68 65 20 70 61 67  ans that the pag
f55b0 65 72 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65  er does not have
f55c0 20 74 6f 20 72 65 63 6f 72 64 20 74 68 65 20 67   to record the g
f55d0 69 76 65 6e 20 70 61 67 65 20 69 6e 20 74 68 65  iven page in the
f55e0 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  .** rollback jou
f55f0 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77  rnal..**.** If w
f5600 65 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 61  e have not yet a
f5610 63 74 75 61 6c 6c 79 20 72 65 61 64 20 74 68 65  ctually read the
f5620 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 73   content of this
f5630 20 70 61 67 65 20 28 69 66 0a 2a 2a 20 74 68 65   page (if.** the
f5640 20 50 67 48 64 72 2e 6e 65 65 64 52 65 61 64 20   PgHdr.needRead 
f5650 66 6c 61 67 20 69 73 20 73 65 74 29 20 74 68 65  flag is set) the
f5660 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61  n this routine a
f5670 63 74 73 20 61 73 20 61 20 70 72 6f 6d 69 73 65  cts as a promise
f5680 0a 2a 2a 20 74 68 61 74 20 77 65 20 77 69 6c 6c  .** that we will
f5690 20 6e 65 76 65 72 20 6e 65 65 64 20 74 6f 20 72   never need to r
f56a0 65 61 64 20 74 68 65 20 70 61 67 65 20 63 6f 6e  ead the page con
f56b0 74 65 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75  tent in the futu
f56c0 72 65 2e 0a 2a 2a 20 73 6f 20 74 68 65 20 6e 65  re..** so the ne
f56d0 65 64 52 65 61 64 20 66 6c 61 67 20 63 61 6e 20  edRead flag can 
f56e0 62 65 20 63 6c 65 61 72 65 64 20 61 74 20 74 68  be cleared at th
f56f0 69 73 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 53 51 4c  is point..*/.SQL
f5700 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
f5710 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e   sqlite3PagerDon
f5720 74 52 6f 6c 6c 62 61 63 6b 28 44 62 50 61 67 65  tRollback(DbPage
f5730 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20   *pPg){.  Pager 
f5740 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70  *pPager = pPg->p
f5750 50 61 67 65 72 3b 0a 0a 20 20 70 61 67 65 72 45  Pager;..  pagerE
f5760 6e 74 65 72 28 70 50 61 67 65 72 29 3b 0a 20 20  nter(pPager);.  
f5770 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
f5780 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53  state>=PAGER_RES
f5790 45 52 56 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 49  ERVED );..  /* I
f57a0 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
f57b0 6c 65 20 69 73 20 6e 6f 74 20 6f 70 65 6e 2c 20  le is not open, 
f57c0 6f 72 20 44 6f 6e 74 57 72 69 74 65 28 29 20 68  or DontWrite() h
f57d0 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 6f  as been called o
f57e0 6e 0a 20 20 2a 2a 20 74 68 69 73 20 70 61 67 65  n.  ** this page
f57f0 20 28 44 6f 6e 74 57 72 69 74 65 28 29 20 73 65   (DontWrite() se
f5800 74 73 20 74 68 65 20 61 6c 77 61 79 73 52 6f 6c  ts the alwaysRol
f5810 6c 62 61 63 6b 20 66 6c 61 67 29 2c 20 74 68 65  lback flag), the
f5820 6e 20 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63  n this.  ** func
f5830 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tion is a no-op.
f5840 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67  .  */.  if( pPag
f5850 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 3d  er->journalOpen=
f5860 3d 30 20 7c 7c 20 70 50 67 2d 3e 61 6c 77 61 79  =0 || pPg->alway
f5870 73 52 6f 6c 6c 62 61 63 6b 20 7c 7c 20 70 50 61  sRollback || pPa
f5880 67 65 72 2d 3e 61 6c 77 61 79 73 52 6f 6c 6c 62  ger->alwaysRollb
f5890 61 63 6b 20 29 7b 0a 20 20 20 20 70 61 67 65 72  ack ){.    pager
f58a0 4c 65 61 76 65 28 70 50 61 67 65 72 29 3b 0a 20  Leave(pPager);. 
f58b0 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
f58c0 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20   assert( !MEMDB 
f58d0 29 3b 20 20 20 20 2f 2a 20 46 6f 72 20 61 20 6d  );    /* For a m
f58e0 65 6d 64 62 2c 20 70 50 61 67 65 72 2d 3e 6a 6f  emdb, pPager->jo
f58f0 75 72 6e 61 6c 4f 70 65 6e 20 69 73 20 61 6c 77  urnalOpen is alw
f5900 61 79 73 20 30 20 2a 2f 0a 0a 23 69 66 64 65 66  ays 0 */..#ifdef
f5910 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44   SQLITE_SECURE_D
f5920 45 4c 45 54 45 0a 20 20 69 66 28 20 70 50 67 2d  ELETE.  if( pPg-
f5930 3e 69 6e 4a 6f 75 72 6e 61 6c 20 7c 7c 20 28 69  >inJournal || (i
f5940 6e 74 29 70 50 67 2d 3e 70 67 6e 6f 20 3e 20 70  nt)pPg->pgno > p
f5950 50 61 67 65 72 2d 3e 6f 72 69 67 44 62 53 69 7a  Pager->origDbSiz
f5960 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  e ){.    return;
f5970 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
f5980 2a 20 49 66 20 53 45 43 55 52 45 5f 44 45 4c 45  * If SECURE_DELE
f5990 54 45 20 69 73 20 64 69 73 61 62 6c 65 64 2c 20  TE is disabled, 
f59a0 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e 6f  then there is no
f59b0 20 77 61 79 20 74 68 61 74 20 74 68 69 73 0a 20   way that this. 
f59c0 20 2a 2a 20 72 6f 75 74 69 6e 65 20 63 61 6e 20   ** routine can 
f59d0 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 70  be called on a p
f59e0 61 67 65 20 66 6f 72 20 77 68 69 63 68 20 73 71  age for which sq
f59f0 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72  lite3PagerDontWr
f5a00 69 74 65 28 29 0a 20 20 2a 2a 20 68 61 73 20 6e  ite().  ** has n
f5a10 6f 74 20 62 65 65 6e 20 70 72 65 76 69 6f 75 73  ot been previous
f5a20 6c 79 20 63 61 6c 6c 65 64 20 64 75 72 69 6e 67  ly called during
f5a30 20 74 68 65 20 73 61 6d 65 20 74 72 61 6e 73 61   the same transa
f5a40 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 41 6e 64 20  ction..  ** And 
f5a50 69 66 20 44 6f 6e 74 57 72 69 74 65 28 29 20 68  if DontWrite() h
f5a60 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 62 65  as previously be
f5a70 65 6e 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 66  en called, the f
f5a80 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 63 6f  ollowing.  ** co
f5a90 6e 64 69 74 69 6f 6e 73 20 6d 75 73 74 20 62 65  nditions must be
f5aa0 20 6d 65 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 73   met..  */.  ass
f5ab0 65 72 74 28 20 21 70 50 67 2d 3e 69 6e 4a 6f 75  ert( !pPg->inJou
f5ac0 72 6e 61 6c 20 26 26 20 28 69 6e 74 29 70 50 67  rnal && (int)pPg
f5ad0 2d 3e 70 67 6e 6f 20 3c 3d 20 70 50 61 67 65 72  ->pgno <= pPager
f5ae0 2d 3e 6f 72 69 67 44 62 53 69 7a 65 20 29 3b 0a  ->origDbSize );.
f5af0 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
f5b00 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 21 3d 30  r->pInJournal!=0
f5b10 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 69 74   );.  sqlite3Bit
f5b20 76 65 63 53 65 74 28 70 50 61 67 65 72 2d 3e 70  vecSet(pPager->p
f5b30 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 2d 3e  InJournal, pPg->
f5b40 70 67 6e 6f 29 3b 0a 20 20 70 50 67 2d 3e 69 6e  pgno);.  pPg->in
f5b50 4a 6f 75 72 6e 61 6c 20 3d 20 31 3b 0a 20 20 70  Journal = 1;.  p
f5b60 50 67 2d 3e 6e 65 65 64 52 65 61 64 20 3d 20 30  Pg->needRead = 0
f5b70 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ;.  if( pPager->
f5b80 73 74 6d 74 49 6e 55 73 65 20 29 7b 0a 20 20 20  stmtInUse ){.   
f5b90 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
f5ba0 3e 73 74 6d 74 53 69 7a 65 20 3e 3d 20 70 50 61  >stmtSize >= pPa
f5bb0 67 65 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65 20  ger->origDbSize 
f5bc0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 69  );.    sqlite3Bi
f5bd0 74 76 65 63 53 65 74 28 70 50 61 67 65 72 2d 3e  tvecSet(pPager->
f5be0 70 49 6e 53 74 6d 74 2c 20 70 50 67 2d 3e 70 67  pInStmt, pPg->pg
f5bf0 6e 6f 29 3b 0a 20 20 7d 0a 20 20 50 41 47 45 52  no);.  }.  PAGER
f5c00 54 52 41 43 45 33 28 22 44 4f 4e 54 5f 52 4f 4c  TRACE3("DONT_ROL
f5c10 4c 42 41 43 4b 20 70 61 67 65 20 25 64 20 6f 66  LBACK page %d of
f5c20 20 25 64 5c 6e 22 2c 20 70 50 67 2d 3e 70 67 6e   %d\n", pPg->pgn
f5c30 6f 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65  o, PAGERID(pPage
f5c40 72 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28  r));.  IOTRACE((
f5c50 22 47 41 52 42 41 47 45 20 25 70 20 25 64 5c 6e  "GARBAGE %p %d\n
f5c60 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 2d 3e  ", pPager, pPg->
f5c70 70 67 6e 6f 29 29 0a 20 20 70 61 67 65 72 4c 65  pgno)).  pagerLe
f5c80 61 76 65 28 70 50 61 67 65 72 29 3b 0a 7d 0a 0a  ave(pPager);.}..
f5c90 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
f5ca0 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f  ine is called to
f5cb0 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 64   increment the d
f5cc0 61 74 61 62 61 73 65 20 66 69 6c 65 20 63 68 61  atabase file cha
f5cd0 6e 67 65 2d 63 6f 75 6e 74 65 72 2c 0a 2a 2a 20  nge-counter,.** 
f5ce0 73 74 6f 72 65 64 20 61 74 20 62 79 74 65 20 32  stored at byte 2
f5cf0 34 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 66  4 of the pager f
f5d00 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ile..*/.static i
f5d10 6e 74 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68  nt pager_incr_ch
f5d20 61 6e 67 65 63 6f 75 6e 74 65 72 28 50 61 67 65  angecounter(Page
f5d30 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 69  r *pPager, int i
f5d40 73 44 69 72 65 63 74 29 7b 0a 20 20 50 67 48 64  sDirect){.  PgHd
f5d50 72 20 2a 70 50 67 48 64 72 3b 0a 20 20 75 33 32  r *pPgHdr;.  u32
f5d60 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 3b   change_counter;
f5d70 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
f5d80 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 21 70  TE_OK;..  if( !p
f5d90 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75  Pager->changeCou
f5da0 6e 74 44 6f 6e 65 20 29 7b 0a 20 20 20 20 2f 2a  ntDone ){.    /*
f5db0 20 4f 70 65 6e 20 70 61 67 65 20 31 20 6f 66 20   Open page 1 of 
f5dc0 74 68 65 20 66 69 6c 65 20 66 6f 72 20 77 72 69  the file for wri
f5dd0 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 63 20  ting. */.    rc 
f5de0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
f5df0 74 28 70 50 61 67 65 72 2c 20 31 2c 20 26 70 50  t(pPager, 1, &pP
f5e00 67 48 64 72 29 3b 0a 20 20 20 20 69 66 28 20 72  gHdr);.    if( r
f5e10 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
f5e20 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 69  eturn rc;..    i
f5e30 66 28 20 21 69 73 44 69 72 65 63 74 20 29 7b 0a  f( !isDirect ){.
f5e40 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
f5e50 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 67  e3PagerWrite(pPg
f5e60 48 64 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20  Hdr);.      if( 
f5e70 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
f5e80 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
f5e90 50 61 67 65 72 55 6e 72 65 66 28 70 50 67 48 64  PagerUnref(pPgHd
f5ea0 72 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  r);.        retu
f5eb0 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20  rn rc;.      }. 
f5ec0 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 6e 63     }..    /* Inc
f5ed0 72 65 6d 65 6e 74 20 74 68 65 20 76 61 6c 75 65  rement the value
f5ee0 20 6a 75 73 74 20 72 65 61 64 20 61 6e 64 20 77   just read and w
f5ef0 72 69 74 65 20 69 74 20 62 61 63 6b 20 74 6f 20  rite it back to 
f5f00 62 79 74 65 20 32 34 2e 20 2a 2f 0a 20 20 20 20  byte 24. */.    
f5f10 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 20 3d  change_counter =
f5f20 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65   sqlite3Get4byte
f5f30 28 28 75 38 2a 29 70 50 61 67 65 72 2d 3e 64 62  ((u8*)pPager->db
f5f40 46 69 6c 65 56 65 72 73 29 3b 0a 20 20 20 20 63  FileVers);.    c
f5f50 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 2b 2b 3b  hange_counter++;
f5f60 0a 20 20 20 20 70 75 74 33 32 62 69 74 73 28 28  .    put32bits((
f5f70 28 63 68 61 72 2a 29 50 47 48 44 52 5f 54 4f 5f  (char*)PGHDR_TO_
f5f80 44 41 54 41 28 70 50 67 48 64 72 29 29 2b 32 34  DATA(pPgHdr))+24
f5f90 2c 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72  , change_counter
f5fa0 29 3b 0a 0a 20 20 20 20 69 66 28 20 69 73 44 69  );..    if( isDi
f5fb0 72 65 63 74 20 26 26 20 70 50 61 67 65 72 2d 3e  rect && pPager->
f5fc0 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a  fd->pMethods ){.
f5fd0 20 20 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64        const void
f5fe0 20 2a 7a 42 75 66 20 3d 20 50 47 48 44 52 5f 54   *zBuf = PGHDR_T
f5ff0 4f 5f 44 41 54 41 28 70 50 67 48 64 72 29 3b 0a  O_DATA(pPgHdr);.
f6000 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
f6010 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72  e3OsWrite(pPager
f6020 2d 3e 66 64 2c 20 7a 42 75 66 2c 20 70 50 61 67  ->fd, zBuf, pPag
f6030 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 30 29  er->pageSize, 0)
f6040 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
f6050 52 65 6c 65 61 73 65 20 74 68 65 20 70 61 67 65  Release the page
f6060 20 72 65 66 65 72 65 6e 63 65 2e 20 2a 2f 0a 20   reference. */. 
f6070 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55     sqlite3PagerU
f6080 6e 72 65 66 28 70 50 67 48 64 72 29 3b 0a 20 20  nref(pPgHdr);.  
f6090 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65    pPager->change
f60a0 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a 20  CountDone = 1;. 
f60b0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
f60c0 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68  }../*.** Sync th
f60d0 65 20 70 61 67 65 72 20 66 69 6c 65 20 74 6f 20  e pager file to 
f60e0 64 69 73 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  disk..*/.SQLITE_
f60f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
f6100 74 65 33 50 61 67 65 72 53 79 6e 63 28 50 61 67  te3PagerSync(Pag
f6110 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69  er *pPager){.  i
f6120 6e 74 20 72 63 3b 0a 20 20 70 61 67 65 72 45 6e  nt rc;.  pagerEn
f6130 74 65 72 28 70 50 61 67 65 72 29 3b 0a 20 20 72  ter(pPager);.  r
f6140 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e  c = sqlite3OsSyn
f6150 63 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50  c(pPager->fd, pP
f6160 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73  ager->sync_flags
f6170 29 3b 0a 20 20 70 61 67 65 72 4c 65 61 76 65 28  );.  pagerLeave(
f6180 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72  pPager);.  retur
f6190 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  n rc;.}../*.** S
f61a0 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65  ync the database
f61b0 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 70 61   file for the pa
f61c0 67 65 72 20 70 50 61 67 65 72 2e 20 7a 4d 61 73  ger pPager. zMas
f61d0 74 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74 68  ter points to th
f61e0 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 61 20 6d  e name.** of a m
f61f0 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
f6200 6c 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62  le that should b
f6210 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  e written into t
f6220 68 65 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a  he individual.**
f6230 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 7a   journal file. z
f6240 4d 61 73 74 65 72 20 6d 61 79 20 62 65 20 4e 55  Master may be NU
f6250 4c 4c 2c 20 77 68 69 63 68 20 69 73 20 69 6e 74  LL, which is int
f6260 65 72 70 72 65 74 65 64 20 61 73 20 6e 6f 20 6d  erpreted as no m
f6270 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c  aster.** journal
f6280 20 28 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62   (a single datab
f6290 61 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 29  ase transaction)
f62a0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
f62b0 74 69 6e 65 20 65 6e 73 75 72 65 73 20 74 68 61  tine ensures tha
f62c0 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73  t the journal is
f62d0 20 73 79 6e 63 65 64 2c 20 61 6c 6c 20 64 69 72   synced, all dir
f62e0 74 79 20 70 61 67 65 73 20 77 72 69 74 74 65 6e  ty pages written
f62f0 0a 2a 2a 20 74 6f 20 74 68 65 20 64 61 74 61 62  .** to the datab
f6300 61 73 65 20 66 69 6c 65 20 61 6e 64 20 74 68 65  ase file and the
f6310 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 73   database file s
f6320 79 6e 63 65 64 2e 20 54 68 65 20 6f 6e 6c 79 20  ynced. The only 
f6330 74 68 69 6e 67 20 74 68 61 74 0a 2a 2a 20 72 65  thing that.** re
f6340 6d 61 69 6e 73 20 74 6f 20 63 6f 6d 6d 69 74 20  mains to commit 
f6350 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
f6360 69 73 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65  is to delete the
f6370 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f   journal file (o
f6380 72 0a 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72  r.** master jour
f6390 6e 61 6c 20 66 69 6c 65 20 69 66 20 73 70 65 63  nal file if spec
f63a0 69 66 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  ified)..**.** No
f63b0 74 65 20 74 68 61 74 20 69 66 20 7a 4d 61 73 74  te that if zMast
f63c0 65 72 3d 3d 4e 55 4c 4c 2c 20 74 68 69 73 20 64  er==NULL, this d
f63d0 6f 65 73 20 6e 6f 74 20 6f 76 65 72 77 72 69 74  oes not overwrit
f63e0 65 20 61 20 70 72 65 76 69 6f 75 73 20 76 61 6c  e a previous val
f63f0 75 65 0a 2a 2a 20 70 61 73 73 65 64 20 74 6f 20  ue.** passed to 
f6400 61 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 43  an sqlite3PagerC
f6410 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20  ommitPhaseOne() 
f6420 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70  call..**.** If p
f6430 61 72 61 6d 65 74 65 72 20 6e 54 72 75 6e 63 20  arameter nTrunc 
f6440 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
f6450 6e 20 74 68 65 20 70 61 67 65 72 20 66 69 6c 65  n the pager file
f6460 20 69 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f   is truncated to
f6470 0a 2a 2a 20 6e 54 72 75 6e 63 20 70 61 67 65 73  .** nTrunc pages
f6480 20 28 74 68 69 73 20 69 73 20 75 73 65 64 20 62   (this is used b
f6490 79 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61  y auto-vacuum da
f64a0 74 61 62 61 73 65 73 29 2e 0a 2a 2a 0a 2a 2a 20  tabases)..**.** 
f64b0 49 66 20 74 68 65 20 66 69 6e 61 6c 20 70 61 72  If the final par
f64c0 61 6d 65 74 65 72 20 2d 20 6e 6f 53 79 6e 63 20  ameter - noSync 
f64d0 2d 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20  - is true, then 
f64e0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
f64f0 65 20 69 74 73 65 6c 66 0a 2a 2a 20 69 73 20 6e  e itself.** is n
f6500 6f 74 20 73 79 6e 63 65 64 2e 20 54 68 65 20 63  ot synced. The c
f6510 61 6c 6c 65 72 20 6d 75 73 74 20 63 61 6c 6c 20  aller must call 
f6520 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63  sqlite3PagerSync
f6530 28 29 20 64 69 72 65 63 74 6c 79 20 74 6f 0a 2a  () directly to.*
f6540 2a 20 73 79 6e 63 20 74 68 65 20 64 61 74 61 62  * sync the datab
f6550 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65 20  ase file before 
f6560 63 61 6c 6c 69 6e 67 20 43 6f 6d 6d 69 74 50 68  calling CommitPh
f6570 61 73 65 54 77 6f 28 29 20 74 6f 20 64 65 6c 65  aseTwo() to dele
f6580 74 65 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61  te the.** journa
f6590 6c 20 66 69 6c 65 20 69 6e 20 74 68 69 73 20 63  l file in this c
f65a0 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ase..*/.SQLITE_P
f65b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
f65c0 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61  e3PagerCommitPha
f65d0 73 65 4f 6e 65 28 0a 20 20 50 61 67 65 72 20 2a  seOne(.  Pager *
f65e0 70 50 61 67 65 72 2c 20 0a 20 20 63 6f 6e 73 74  pPager, .  const
f65f0 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c 20   char *zMaster, 
f6600 0a 20 20 50 67 6e 6f 20 6e 54 72 75 6e 63 2c 0a  .  Pgno nTrunc,.
f6610 20 20 69 6e 74 20 6e 6f 53 79 6e 63 0a 29 7b 0a    int noSync.){.
f6620 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
f6630 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 49 66 20 6e  E_OK;..  /* If n
f6640 6f 20 63 68 61 6e 67 65 73 20 68 61 76 65 20 62  o changes have b
f6650 65 65 6e 20 6d 61 64 65 2c 20 77 65 20 63 61 6e  een made, we can
f6660 20 6c 65 61 76 65 20 74 68 65 20 74 72 61 6e 73   leave the trans
f6670 61 63 74 69 6f 6e 20 65 61 72 6c 79 2e 0a 20 20  action early..  
f6680 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  */.  if( pPager-
f6690 3e 64 62 4d 6f 64 69 66 69 65 64 3d 3d 30 20 26  >dbModified==0 &
f66a0 26 0a 20 20 20 20 20 20 20 20 28 70 50 61 67 65  &.        (pPage
f66b0 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d  r->journalMode!=
f66c0 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
f66d0 45 5f 44 45 4c 45 54 45 20 7c 7c 0a 20 20 20 20  E_DELETE ||.    
f66e0 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 78        pPager->ex
f66f0 63 6c 75 73 69 76 65 4d 6f 64 65 21 3d 30 29 20  clusiveMode!=0) 
f6700 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
f6710 50 61 67 65 72 2d 3e 64 69 72 74 79 43 61 63 68  Pager->dirtyCach
f6720 65 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e  e==0 || pPager->
f6730 6a 6f 75 72 6e 61 6c 4f 70 65 6e 3d 3d 30 20 29  journalOpen==0 )
f6740 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
f6750 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 50  ITE_OK;.  }..  P
f6760 41 47 45 52 54 52 41 43 45 34 28 22 44 41 54 41  AGERTRACE4("DATA
f6770 42 41 53 45 20 53 59 4e 43 3a 20 46 69 6c 65 3d  BASE SYNC: File=
f6780 25 73 20 7a 4d 61 73 74 65 72 3d 25 73 20 6e 54  %s zMaster=%s nT
f6790 72 75 6e 63 3d 25 64 5c 6e 22 2c 20 0a 20 20 20  runc=%d\n", .   
f67a0 20 20 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65     pPager->zFile
f67b0 6e 61 6d 65 2c 20 7a 4d 61 73 74 65 72 2c 20 6e  name, zMaster, n
f67c0 54 72 75 6e 63 29 3b 0a 20 20 70 61 67 65 72 45  Trunc);.  pagerE
f67d0 6e 74 65 72 28 70 50 61 67 65 72 29 3b 0a 0a 20  nter(pPager);.. 
f67e0 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
f67f0 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 62 2c 20  n in-memory db, 
f6800 6f 72 20 6e 6f 20 70 61 67 65 73 20 68 61 76 65  or no pages have
f6810 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f   been written to
f6820 2c 20 6f 72 20 74 68 69 73 0a 20 20 2a 2a 20 66  , or this.  ** f
f6830 75 6e 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65  unction has alre
f6840 61 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c  ady been called,
f6850 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a   it is a no-op..
f6860 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65    */.  if( pPage
f6870 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f  r->state!=PAGER_
f6880 53 59 4e 43 45 44 20 26 26 20 21 4d 45 4d 44 42  SYNCED && !MEMDB
f6890 20 26 26 20 70 50 61 67 65 72 2d 3e 64 69 72 74   && pPager->dirt
f68a0 79 43 61 63 68 65 20 29 7b 0a 20 20 20 20 50 67  yCache ){.    Pg
f68b0 48 64 72 20 2a 70 50 67 3b 0a 0a 23 69 66 64 65  Hdr *pPg;..#ifde
f68c0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
f68d0 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20  ATOMIC_WRITE.   
f68e0 20 2f 2a 20 54 68 65 20 61 74 6f 6d 69 63 2d 77   /* The atomic-w
f68f0 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f  rite optimizatio
f6900 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 66  n can be used if
f6910 20 61 6c 6c 20 6f 66 20 74 68 65 0a 20 20 20 20   all of the.    
f6920 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65  ** following are
f6930 20 74 72 75 65 3a 0a 20 20 20 20 2a 2a 0a 20 20   true:.    **.  
f6940 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 66 69    **    + The fi
f6950 6c 65 2d 73 79 73 74 65 6d 20 73 75 70 70 6f 72  le-system suppor
f6960 74 73 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72  ts the atomic-wr
f6970 69 74 65 20 70 72 6f 70 65 72 74 79 20 66 6f 72  ite property for
f6980 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 62 6c 6f  .    **      blo
f6990 63 6b 73 20 6f 66 20 73 69 7a 65 20 70 61 67 65  cks of size page
f69a0 2d 73 69 7a 65 2c 20 61 6e 64 0a 20 20 20 20 2a  -size, and.    *
f69b0 2a 20 20 20 20 2b 20 54 68 69 73 20 63 6f 6d 6d  *    + This comm
f69c0 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f  it is not part o
f69d0 66 20 61 20 6d 75 6c 74 69 2d 66 69 6c 65 20 74  f a multi-file t
f69e0 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64 0a  ransaction, and.
f69f0 20 20 20 20 2a 2a 20 20 20 20 2b 20 45 78 61 63      **    + Exac
f6a00 74 6c 79 20 6f 6e 65 20 70 61 67 65 20 68 61 73  tly one page has
f6a10 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 61   been modified a
f6a20 6e 64 20 73 74 6f 72 65 20 69 6e 20 74 68 65 20  nd store in the 
f6a30 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20  journal file..  
f6a40 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74    **.    ** If t
f6a50 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  he optimization 
f6a60 63 61 6e 20 62 65 20 75 73 65 64 2c 20 74 68 65  can be used, the
f6a70 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  n the journal fi
f6a80 6c 65 20 77 69 6c 6c 20 6e 65 76 65 72 0a 20 20  le will never.  
f6a90 20 20 2a 2a 20 62 65 20 63 72 65 61 74 65 64 20    ** be created 
f6aa0 66 6f 72 20 74 68 69 73 20 74 72 61 6e 73 61 63  for this transac
f6ab0 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tion..    */.   
f6ac0 20 69 6e 74 20 75 73 65 41 74 6f 6d 69 63 57 72   int useAtomicWr
f6ad0 69 74 65 20 3d 20 28 0a 20 20 20 20 20 20 20 20  ite = (.        
f6ae0 21 7a 4d 61 73 74 65 72 20 26 26 20 0a 20 20 20  !zMaster && .   
f6af0 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75       pPager->jou
f6b00 72 6e 61 6c 4f 70 65 6e 20 26 26 0a 20 20 20 20  rnalOpen &&.    
f6b10 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
f6b20 6e 61 6c 4f 66 66 3d 3d 6a 72 6e 6c 42 75 66 66  nalOff==jrnlBuff
f6b30 65 72 53 69 7a 65 28 70 50 61 67 65 72 29 20 26  erSize(pPager) &
f6b40 26 20 0a 20 20 20 20 20 20 20 20 6e 54 72 75 6e  & .        nTrun
f6b50 63 3d 3d 30 20 26 26 20 0a 20 20 20 20 20 20 20  c==0 && .       
f6b60 20 28 30 3d 3d 70 50 61 67 65 72 2d 3e 70 44 69   (0==pPager->pDi
f6b70 72 74 79 20 7c 7c 20 30 3d 3d 70 50 61 67 65 72  rty || 0==pPager
f6b80 2d 3e 70 44 69 72 74 79 2d 3e 70 44 69 72 74 79  ->pDirty->pDirty
f6b90 29 0a 20 20 20 20 29 3b 0a 20 20 20 20 61 73 73  ).    );.    ass
f6ba0 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75  ert( pPager->jou
f6bb0 72 6e 61 6c 4f 70 65 6e 20 7c 7c 20 70 50 61 67  rnalOpen || pPag
f6bc0 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d  er->journalMode=
f6bd0 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
f6be0 44 45 5f 4f 46 46 20 29 3b 0a 20 20 20 20 69 66  DE_OFF );.    if
f6bf0 28 20 75 73 65 41 74 6f 6d 69 63 57 72 69 74 65  ( useAtomicWrite
f6c00 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55 70 64   ){.      /* Upd
f6c10 61 74 65 20 74 68 65 20 6e 52 65 63 20 66 69 65  ate the nRec fie
f6c20 6c 64 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61  ld in the journa
f6c30 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20  l file. */.     
f6c40 20 69 6e 74 20 6f 66 66 73 65 74 20 3d 20 70 50   int offset = pP
f6c50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72  ager->journalHdr
f6c60 20 2b 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e   + sizeof(aJourn
f6c70 61 6c 4d 61 67 69 63 29 3b 0a 20 20 20 20 20 20  alMagic);.      
f6c80 61 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e 6e  assert(pPager->n
f6c90 52 65 63 3d 3d 31 29 3b 0a 20 20 20 20 20 20 72  Rec==1);.      r
f6ca0 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28  c = write32bits(
f6cb0 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 6f 66 66  pPager->jfd, off
f6cc0 73 65 74 2c 20 70 50 61 67 65 72 2d 3e 6e 52 65  set, pPager->nRe
f6cd0 63 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 55 70  c);..      /* Up
f6ce0 64 61 74 65 20 74 68 65 20 64 62 20 66 69 6c 65  date the db file
f6cf0 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 2e   change counter.
f6d00 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
f6d10 61 6c 6c 20 77 69 6c 6c 20 6d 6f 64 69 66 79 0a  all will modify.
f6d20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 69 6e 2d        ** the in-
f6d30 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74  memory represent
f6d40 61 74 69 6f 6e 20 6f 66 20 70 61 67 65 20 31 20  ation of page 1 
f6d50 74 6f 20 69 6e 63 6c 75 64 65 20 74 68 65 20 75  to include the u
f6d60 70 64 61 74 65 64 0a 20 20 20 20 20 20 2a 2a 20  pdated.      ** 
f6d70 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 61  change counter a
f6d80 6e 64 20 74 68 65 6e 20 77 72 69 74 65 20 70 61  nd then write pa
f6d90 67 65 20 31 20 64 69 72 65 63 74 6c 79 20 74 6f  ge 1 directly to
f6da0 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20   the database.  
f6db0 20 20 20 20 2a 2a 20 66 69 6c 65 2e 20 42 65 63      ** file. Bec
f6dc0 61 75 73 65 20 6f 66 20 74 68 65 20 61 74 6f 6d  ause of the atom
f6dd0 69 63 2d 77 72 69 74 65 20 70 72 6f 70 65 72 74  ic-write propert
f6de0 79 20 6f 66 20 74 68 65 20 68 6f 73 74 20 66 69  y of the host fi
f6df0 6c 65 2d 73 79 73 74 65 6d 2c 20 0a 20 20 20 20  le-system, .    
f6e00 20 20 2a 2a 20 74 68 69 73 20 69 73 20 73 61 66    ** this is saf
f6e10 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  e..      */.    
f6e20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
f6e30 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
f6e40 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63  c = pager_incr_c
f6e50 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 61  hangecounter(pPa
f6e60 67 65 72 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d  ger, 1);.      }
f6e70 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
f6e80 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4a 6f    rc = sqlite3Jo
f6e90 75 72 6e 61 6c 43 72 65 61 74 65 28 70 50 61 67  urnalCreate(pPag
f6ea0 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 7d 0a  er->jfd);.    }.
f6eb0 0a 20 20 20 20 69 66 28 20 21 75 73 65 41 74 6f  .    if( !useAto
f6ec0 6d 69 63 57 72 69 74 65 20 26 26 20 72 63 3d 3d  micWrite && rc==
f6ed0 53 51 4c 49 54 45 5f 4f 4b 20 29 0a 23 65 6e 64  SQLITE_OK ).#end
f6ee0 69 66 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61 20  if..    /* If a 
f6ef0 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
f6f00 69 6c 65 20 6e 61 6d 65 20 68 61 73 20 61 6c 72  ile name has alr
f6f10 65 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65  eady been writte
f6f20 6e 20 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20  n to the.    ** 
f6f30 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68  journal file, th
f6f40 65 6e 20 6e 6f 20 73 79 6e 63 20 69 73 20 72 65  en no sync is re
f6f50 71 75 69 72 65 64 2e 20 54 68 69 73 20 68 61 70  quired. This hap
f6f60 70 65 6e 73 20 77 68 65 6e 20 69 74 20 69 73 0a  pens when it is.
f6f70 20 20 20 20 2a 2a 20 77 72 69 74 74 65 6e 2c 20      ** written, 
f6f80 74 68 65 6e 20 74 68 65 20 70 72 6f 63 65 73 73  then the process
f6f90 20 66 61 69 6c 73 20 74 6f 20 75 70 67 72 61 64   fails to upgrad
f6fa0 65 20 66 72 6f 6d 20 61 20 52 45 53 45 52 56 45  e from a RESERVE
f6fb0 44 20 74 6f 20 61 6e 0a 20 20 20 20 2a 2a 20 45  D to an.    ** E
f6fc0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 54  XCLUSIVE lock. T
f6fd0 68 65 20 6e 65 78 74 20 74 69 6d 65 20 74 68 65  he next time the
f6fe0 20 70 72 6f 63 65 73 73 20 74 72 69 65 73 20 74   process tries t
f6ff0 6f 20 63 6f 6d 6d 69 74 20 74 68 65 0a 20 20 20  o commit the.   
f7000 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20   ** transaction 
f7010 74 68 65 20 6d 2d 6a 20 6e 61 6d 65 20 77 69 6c  the m-j name wil
f7020 6c 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62  l have already b
f7030 65 65 6e 20 77 72 69 74 74 65 6e 2e 0a 20 20 20  een written..   
f7040 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 50 61   */.    if( !pPa
f7050 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 29  ger->setMaster )
f7060 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67  {.      rc = pag
f7070 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f  er_incr_changeco
f7080 75 6e 74 65 72 28 70 50 61 67 65 72 2c 20 30 29  unter(pPager, 0)
f7090 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
f70a0 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
f70b0 20 73 79 6e 63 5f 65 78 69 74 3b 0a 23 69 66 6e   sync_exit;.#ifn
f70c0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
f70d0 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20  AUTOVACUUM.     
f70e0 20 69 66 28 20 6e 54 72 75 6e 63 21 3d 30 20 29   if( nTrunc!=0 )
f70f0 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20  {.        /* If 
f7100 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e  this transaction
f7110 20 68 61 73 20 6d 61 64 65 20 74 68 65 20 64 61   has made the da
f7120 74 61 62 61 73 65 20 73 6d 61 6c 6c 65 72 2c 20  tabase smaller, 
f7130 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 0a 20  then all pages. 
f7140 20 20 20 20 20 20 20 2a 2a 20 62 65 69 6e 67 20         ** being 
f7150 64 69 73 63 61 72 64 65 64 20 62 79 20 74 68 65  discarded by the
f7160 20 74 72 75 6e 63 61 74 69 6f 6e 20 6d 75 73 74   truncation must
f7170 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20 74   be written to t
f7180 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20  he journal.     
f7190 20 20 20 2a 2a 20 66 69 6c 65 2e 0a 20 20 20 20     ** file..    
f71a0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 50      */.        P
f71b0 67 6e 6f 20 69 3b 0a 20 20 20 20 20 20 20 20 69  gno i;.        i
f71c0 6e 74 20 69 53 6b 69 70 20 3d 20 50 41 47 45 52  nt iSkip = PAGER
f71d0 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29  _MJ_PGNO(pPager)
f71e0 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 20 69  ;.        for( i
f71f0 3d 6e 54 72 75 6e 63 2b 31 3b 20 69 3c 3d 70 50  =nTrunc+1; i<=pP
f7200 61 67 65 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65  ager->origDbSize
f7210 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 20 20 20  ; i++ ){.       
f7220 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 42     if( !sqlite3B
f7230 69 74 76 65 63 54 65 73 74 28 70 50 61 67 65 72  itvecTest(pPager
f7240 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 69 29  ->pInJournal, i)
f7250 20 26 26 20 69 21 3d 69 53 6b 69 70 20 29 7b 0a   && i!=iSkip ){.
f7260 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
f7270 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
f7280 28 70 50 61 67 65 72 2c 20 69 2c 20 26 70 50 67  (pPager, i, &pPg
f7290 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  );.            i
f72a0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
f72b0 20 29 20 67 6f 74 6f 20 73 79 6e 63 5f 65 78 69   ) goto sync_exi
f72c0 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  t;.            r
f72d0 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
f72e0 57 72 69 74 65 28 70 50 67 29 3b 0a 20 20 20 20  Write(pPg);.    
f72f0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
f7300 61 67 65 72 55 6e 72 65 66 28 70 50 67 29 3b 0a  agerUnref(pPg);.
f7310 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
f7320 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
f7330 67 6f 74 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a  goto sync_exit;.
f7340 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
f7350 20 20 20 20 7d 20 0a 20 20 20 20 20 20 7d 0a 23      } .      }.#
f7360 65 6e 64 69 66 0a 20 20 20 20 20 20 72 63 20 3d  endif.      rc =
f7370 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72   writeMasterJour
f7380 6e 61 6c 28 70 50 61 67 65 72 2c 20 7a 4d 61 73  nal(pPager, zMas
f7390 74 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ter);.      if( 
f73a0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
f73b0 67 6f 74 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a  goto sync_exit;.
f73c0 20 20 20 20 20 20 72 63 20 3d 20 73 79 6e 63 4a        rc = syncJ
f73d0 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a  ournal(pPager);.
f73e0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
f73f0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
f7400 74 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a 0a 23  to sync_exit;..#
f7410 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
f7420 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
f7430 20 20 69 66 28 20 6e 54 72 75 6e 63 21 3d 30 20    if( nTrunc!=0 
f7440 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
f7450 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61  lite3PagerTrunca
f7460 74 65 28 70 50 61 67 65 72 2c 20 6e 54 72 75 6e  te(pPager, nTrun
f7470 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  c);.      if( rc
f7480 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
f7490 74 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a 20 20  to sync_exit;.  
f74a0 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
f74b0 2f 2a 20 57 72 69 74 65 20 61 6c 6c 20 64 69 72  /* Write all dir
f74c0 74 79 20 70 61 67 65 73 20 74 6f 20 74 68 65 20  ty pages to the 
f74d0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
f74e0 0a 20 20 20 20 70 50 67 20 3d 20 70 61 67 65 72  .    pPg = pager
f74f0 5f 67 65 74 5f 61 6c 6c 5f 64 69 72 74 79 5f 70  _get_all_dirty_p
f7500 61 67 65 73 28 70 50 61 67 65 72 29 3b 0a 20 20  ages(pPager);.  
f7510 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69    rc = pager_wri
f7520 74 65 5f 70 61 67 65 6c 69 73 74 28 70 50 67 29  te_pagelist(pPg)
f7530 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
f7540 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
f7550 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
f7560 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45  ITE_IOERR_BLOCKE
f7570 44 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68  D );.      /* Th
f7580 65 20 65 72 72 6f 72 20 6d 69 67 68 74 20 68 61  e error might ha
f7590 76 65 20 6c 65 66 74 20 74 68 65 20 64 69 72 74  ve left the dirt
f75a0 79 20 6c 69 73 74 20 61 6c 6c 20 66 6f 75 6c 65  y list all foule
f75b0 64 20 75 70 20 68 65 72 65 2c 0a 20 20 20 20 20  d up here,.     
f75c0 20 2a 2a 20 62 75 74 20 74 68 61 74 20 64 6f 65   ** but that doe
f75d0 73 20 6e 6f 74 20 6d 61 74 74 65 72 20 62 65 63  s not matter bec
f75e0 61 75 73 65 20 69 66 20 74 68 65 20 69 66 20 74  ause if the if t
f75f0 68 65 20 64 69 72 74 79 20 6c 69 73 74 20 64 69  he dirty list di
f7600 64 0a 20 20 20 20 20 20 2a 2a 20 67 65 74 20 63  d.      ** get c
f7610 6f 72 72 75 70 74 65 64 2c 20 74 68 65 6e 20 74  orrupted, then t
f7620 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  he transaction w
f7630 69 6c 6c 20 72 6f 6c 6c 20 62 61 63 6b 20 61 6e  ill roll back an
f7640 64 0a 20 20 20 20 20 20 2a 2a 20 64 69 73 63 61  d.      ** disca
f7650 72 64 20 74 68 65 20 64 69 72 74 79 20 6c 69 73  rd the dirty lis
f7660 74 2e 20 20 54 68 65 72 65 20 69 73 20 61 6e 20  t.  There is an 
f7670 61 73 73 65 72 74 20 69 6e 0a 20 20 20 20 20 20  assert in.      
f7680 2a 2a 20 70 61 67 65 72 5f 67 65 74 5f 61 6c 6c  ** pager_get_all
f7690 5f 64 69 72 74 79 5f 70 61 67 65 73 28 29 20 74  _dirty_pages() t
f76a0 68 61 74 20 76 65 72 69 66 69 65 73 20 74 68 61  hat verifies tha
f76b0 74 20 6e 6f 20 61 74 74 65 6d 70 74 0a 20 20 20  t no attempt.   
f76c0 20 20 20 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f     ** is made to
f76d0 20 75 73 65 20 61 6e 20 69 6e 76 61 6c 69 64 20   use an invalid 
f76e0 64 69 72 74 79 20 6c 69 73 74 2e 0a 20 20 20 20  dirty list..    
f76f0 20 20 2a 2f 0a 20 20 20 20 20 20 67 6f 74 6f 20    */.      goto 
f7700 73 79 6e 63 5f 65 78 69 74 3b 0a 20 20 20 20 7d  sync_exit;.    }
f7710 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 44 69  .    pPager->pDi
f7720 72 74 79 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a  rty = 0;..    /*
f7730 20 53 79 6e 63 20 74 68 65 20 64 61 74 61 62 61   Sync the databa
f7740 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20  se file. */.    
f7750 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53  if( !pPager->noS
f7760 79 6e 63 20 26 26 20 21 6e 6f 53 79 6e 63 20 29  ync && !noSync )
f7770 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
f7780 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65  ite3OsSync(pPage
f7790 72 2d 3e 66 64 2c 20 70 50 61 67 65 72 2d 3e 73  r->fd, pPager->s
f77a0 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 20 20 20  ync_flags);.    
f77b0 7d 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22  }.    IOTRACE(("
f77c0 44 42 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50  DBSYNC %p\n", pP
f77d0 61 67 65 72 29 29 0a 0a 20 20 20 20 70 50 61 67  ager))..    pPag
f77e0 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45  er->state = PAGE
f77f0 52 5f 53 59 4e 43 45 44 3b 0a 20 20 7d 65 6c 73  R_SYNCED;.  }els
f7800 65 20 69 66 28 20 4d 45 4d 44 42 20 26 26 20 6e  e if( MEMDB && n
f7810 54 72 75 6e 63 21 3d 30 20 29 7b 0a 20 20 20 20  Trunc!=0 ){.    
f7820 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
f7830 72 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72  rTruncate(pPager
f7840 2c 20 6e 54 72 75 6e 63 29 3b 0a 20 20 7d 0a 0a  , nTrunc);.  }..
f7850 73 79 6e 63 5f 65 78 69 74 3a 0a 20 20 69 66 28  sync_exit:.  if(
f7860 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52   rc==SQLITE_IOER
f7870 52 5f 42 4c 4f 43 4b 45 44 20 29 7b 0a 20 20 20  R_BLOCKED ){.   
f7880 20 2f 2a 20 70 61 67 65 72 5f 69 6e 63 72 5f 63   /* pager_incr_c
f7890 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 6d  hangecounter() m
f78a0 61 79 20 61 74 74 65 6d 70 74 20 74 6f 20 6f 62  ay attempt to ob
f78b0 74 61 69 6e 20 61 6e 20 65 78 63 6c 75 73 69 76  tain an exclusiv
f78c0 65 0a 20 20 20 20 20 2a 20 6c 6f 63 6b 20 74 6f  e.     * lock to
f78d0 20 73 70 69 6c 6c 20 74 68 65 20 63 61 63 68 65   spill the cache
f78e0 20 61 6e 64 20 72 65 74 75 72 6e 20 49 4f 45 52   and return IOER
f78f0 52 5f 42 4c 4f 43 4b 45 44 2e 20 42 75 74 20 73  R_BLOCKED. But s
f7900 69 6e 63 65 20 0a 20 20 20 20 20 2a 20 74 68 65  ince .     * the
f7910 72 65 20 69 73 20 6e 6f 20 63 68 61 6e 63 65 20  re is no chance 
f7920 74 68 65 20 63 61 63 68 65 20 69 73 20 69 6e 63  the cache is inc
f7930 6f 6e 73 69 73 74 65 6e 74 2c 20 69 74 20 69 73  onsistent, it is
f7940 0a 20 20 20 20 20 2a 20 62 65 74 74 65 72 20 74  .     * better t
f7950 6f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  o return SQLITE_
f7960 42 55 53 59 2e 0a 20 20 20 20 20 2a 2f 0a 20 20  BUSY..     */.  
f7970 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55    rc = SQLITE_BU
f7980 53 59 3b 0a 20 20 7d 0a 20 20 70 61 67 65 72 4c  SY;.  }.  pagerL
f7990 65 61 76 65 28 70 50 61 67 65 72 29 3b 0a 20 20  eave(pPager);.  
f79a0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
f79b0 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 61 6c 6c 20  *.** Commit all 
f79c0 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 64  changes to the d
f79d0 61 74 61 62 61 73 65 20 61 6e 64 20 72 65 6c 65  atabase and rele
f79e0 61 73 65 20 74 68 65 20 77 72 69 74 65 20 6c 6f  ase the write lo
f79f0 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ck..**.** If the
f7a00 20 63 6f 6d 6d 69 74 20 66 61 69 6c 73 20 66 6f   commit fails fo
f7a10 72 20 61 6e 79 20 72 65 61 73 6f 6e 2c 20 61 20  r any reason, a 
f7a20 72 6f 6c 6c 62 61 63 6b 20 61 74 74 65 6d 70 74  rollback attempt
f7a30 20 69 73 20 6d 61 64 65 0a 2a 2a 20 61 6e 64 20   is made.** and 
f7a40 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  an error code is
f7a50 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74   returned.  If t
f7a60 68 65 20 63 6f 6d 6d 69 74 20 77 6f 72 6b 65 64  he commit worked
f7a70 2c 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69  , SQLITE_OK.** i
f7a80 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53  s returned..*/.S
f7a90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
f7aa0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f  t sqlite3PagerCo
f7ab0 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 50 61 67  mmitPhaseTwo(Pag
f7ac0 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69  er *pPager){.  i
f7ad0 6e 74 20 72 63 3b 0a 20 20 50 67 48 64 72 20 2a  nt rc;.  PgHdr *
f7ae0 70 50 67 3b 0a 0a 20 20 69 66 28 20 70 50 61 67  pPg;..  if( pPag
f7af0 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20  er->errCode ){. 
f7b00 20 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72     return pPager
f7b10 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d 0a 20  ->errCode;.  }. 
f7b20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61   if( pPager->sta
f7b30 74 65 3c 50 41 47 45 52 5f 52 45 53 45 52 56 45  te<PAGER_RESERVE
f7b40 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  D ){.    return 
f7b50 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
f7b60 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  }.  if( pPager->
f7b70 64 62 4d 6f 64 69 66 69 65 64 3d 3d 30 20 26 26  dbModified==0 &&
f7b80 0a 20 20 20 20 20 20 20 20 28 70 50 61 67 65 72  .        (pPager
f7b90 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50  ->journalMode!=P
f7ba0 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
f7bb0 5f 44 45 4c 45 54 45 20 7c 7c 0a 20 20 20 20 20  _DELETE ||.     
f7bc0 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 78 63       pPager->exc
f7bd0 6c 75 73 69 76 65 4d 6f 64 65 21 3d 30 29 20 29  lusiveMode!=0) )
f7be0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  {.    assert( pP
f7bf0 61 67 65 72 2d 3e 64 69 72 74 79 43 61 63 68 65  ager->dirtyCache
f7c00 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a  ==0 || pPager->j
f7c10 6f 75 72 6e 61 6c 4f 70 65 6e 3d 3d 30 20 29 3b  ournalOpen==0 );
f7c20 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
f7c30 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 61 67  TE_OK;.  }.  pag
f7c40 65 72 45 6e 74 65 72 28 70 50 61 67 65 72 29 3b  erEnter(pPager);
f7c50 0a 20 20 50 41 47 45 52 54 52 41 43 45 32 28 22  .  PAGERTRACE2("
f7c60 43 4f 4d 4d 49 54 20 25 64 5c 6e 22 2c 20 50 41  COMMIT %d\n", PA
f7c70 47 45 52 49 44 28 70 50 61 67 65 72 29 29 3b 0a  GERID(pPager));.
f7c80 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20    if( MEMDB ){. 
f7c90 20 20 20 70 50 67 20 3d 20 70 61 67 65 72 5f 67     pPg = pager_g
f7ca0 65 74 5f 61 6c 6c 5f 64 69 72 74 79 5f 70 61 67  et_all_dirty_pag
f7cb0 65 73 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  es(pPager);.    
f7cc0 77 68 69 6c 65 28 20 70 50 67 20 29 7b 0a 20 20  while( pPg ){.  
f7cd0 20 20 20 20 50 67 48 69 73 74 6f 72 79 20 2a 70      PgHistory *p
f7ce0 48 69 73 74 20 3d 20 50 47 48 44 52 5f 54 4f 5f  Hist = PGHDR_TO_
f7cf0 48 49 53 54 28 70 50 67 2c 20 70 50 61 67 65 72  HIST(pPg, pPager
f7d00 29 3b 0a 20 20 20 20 20 20 63 6c 65 61 72 48 69  );.      clearHi
f7d10 73 74 6f 72 79 28 70 48 69 73 74 29 3b 0a 20 20  story(pHist);.  
f7d20 20 20 20 20 70 50 67 2d 3e 64 69 72 74 79 20 3d      pPg->dirty =
f7d30 20 30 3b 0a 20 20 20 20 20 20 70 50 67 2d 3e 69   0;.      pPg->i
f7d40 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20  nJournal = 0;.  
f7d50 20 20 20 20 70 48 69 73 74 2d 3e 69 6e 53 74 6d      pHist->inStm
f7d60 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 67  t = 0;.      pPg
f7d70 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a  ->needSync = 0;.
f7d80 20 20 20 20 20 20 70 48 69 73 74 2d 3e 70 50 72        pHist->pPr
f7d90 65 76 53 74 6d 74 20 3d 20 70 48 69 73 74 2d 3e  evStmt = pHist->
f7da0 70 4e 65 78 74 53 74 6d 74 20 3d 20 30 3b 0a 20  pNextStmt = 0;. 
f7db0 20 20 20 20 20 70 50 67 20 3d 20 70 50 67 2d 3e       pPg = pPg->
f7dc0 70 44 69 72 74 79 3b 0a 20 20 20 20 7d 0a 20 20  pDirty;.    }.  
f7dd0 20 20 70 50 61 67 65 72 2d 3e 70 44 69 72 74 79    pPager->pDirty
f7de0 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 4e 44   = 0;.#ifndef ND
f7df0 45 42 55 47 0a 20 20 20 20 66 6f 72 28 70 50 67  EBUG.    for(pPg
f7e00 3d 70 50 61 67 65 72 2d 3e 70 41 6c 6c 3b 20 70  =pPager->pAll; p
f7e10 50 67 3b 20 70 50 67 3d 70 50 67 2d 3e 70 4e 65  Pg; pPg=pPg->pNe
f7e20 78 74 41 6c 6c 29 7b 0a 20 20 20 20 20 20 50 67  xtAll){.      Pg
f7e30 48 69 73 74 6f 72 79 20 2a 70 48 69 73 74 20 3d  History *pHist =
f7e40 20 50 47 48 44 52 5f 54 4f 5f 48 49 53 54 28 70   PGHDR_TO_HIST(p
f7e50 50 67 2c 20 70 50 61 67 65 72 29 3b 0a 20 20 20  Pg, pPager);.   
f7e60 20 20 20 61 73 73 65 72 74 28 20 21 70 50 67 2d     assert( !pPg-
f7e70 3e 61 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 20  >alwaysRollback 
f7e80 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
f7e90 20 21 70 48 69 73 74 2d 3e 70 4f 72 69 67 20 29   !pHist->pOrig )
f7ea0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
f7eb0 21 70 48 69 73 74 2d 3e 70 53 74 6d 74 20 29 3b  !pHist->pStmt );
f7ec0 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
f7ed0 20 20 70 50 61 67 65 72 2d 3e 70 53 74 6d 74 20    pPager->pStmt 
f7ee0 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  = 0;.    pPager-
f7ef0 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 53  >state = PAGER_S
f7f00 48 41 52 45 44 3b 0a 20 20 20 20 70 61 67 65 72  HARED;.    pager
f7f10 4c 65 61 76 65 28 70 50 61 67 65 72 29 3b 0a 20  Leave(pPager);. 
f7f20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
f7f30 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  _OK;.  }.  asser
f7f40 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  t( pPager->state
f7f50 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 20 7c  ==PAGER_SYNCED |
f7f60 7c 20 21 70 50 61 67 65 72 2d 3e 64 69 72 74 79  | !pPager->dirty
f7f70 43 61 63 68 65 20 29 3b 0a 20 20 72 63 20 3d 20  Cache );.  rc = 
f7f80 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61  pager_end_transa
f7f90 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 70 50  ction(pPager, pP
f7fa0 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 29  ager->setMaster)
f7fb0 3b 0a 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65  ;.  rc = pager_e
f7fc0 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29  rror(pPager, rc)
f7fd0 3b 0a 20 20 70 61 67 65 72 4c 65 61 76 65 28 70  ;.  pagerLeave(p
f7fe0 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72 6e  Pager);.  return
f7ff0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f   rc;.}../*.** Ro
f8000 6c 6c 62 61 63 6b 20 61 6c 6c 20 63 68 61 6e 67  llback all chang
f8010 65 73 2e 20 20 54 68 65 20 64 61 74 61 62 61 73  es.  The databas
f8020 65 20 66 61 6c 6c 73 20 62 61 63 6b 20 74 6f 20  e falls back to 
f8030 50 41 47 45 52 5f 53 48 41 52 45 44 20 6d 6f 64  PAGER_SHARED mod
f8040 65 2e 0a 2a 2a 20 41 6c 6c 20 69 6e 2d 6d 65 6d  e..** All in-mem
f8050 6f 72 79 20 63 61 63 68 65 20 70 61 67 65 73 20  ory cache pages 
f8060 72 65 76 65 72 74 20 74 6f 20 74 68 65 69 72 20  revert to their 
f8070 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 20 63 6f  original data co
f8080 6e 74 65 6e 74 73 2e 0a 2a 2a 20 54 68 65 20 6a  ntents..** The j
f8090 6f 75 72 6e 61 6c 20 69 73 20 64 65 6c 65 74 65  ournal is delete
f80a0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  d..**.** This ro
f80b0 75 74 69 6e 65 20 63 61 6e 6e 6f 74 20 66 61 69  utine cannot fai
f80c0 6c 20 75 6e 6c 65 73 73 20 73 6f 6d 65 20 6f 74  l unless some ot
f80d0 68 65 72 20 70 72 6f 63 65 73 73 20 69 73 20 6e  her process is n
f80e0 6f 74 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  ot following.** 
f80f0 74 68 65 20 63 6f 72 72 65 63 74 20 6c 6f 63 6b  the correct lock
f8100 69 6e 67 20 70 72 6f 74 6f 63 6f 6c 20 6f 72 20  ing protocol or 
f8110 75 6e 6c 65 73 73 20 73 6f 6d 65 20 6f 74 68 65  unless some othe
f8120 72 0a 2a 2a 20 70 72 6f 63 65 73 73 20 69 73 20  r.** process is 
f8130 77 72 69 74 69 6e 67 20 74 72 61 73 68 20 69 6e  writing trash in
f8140 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  to the journal f
f8150 69 6c 65 20 28 53 51 4c 49 54 45 5f 43 4f 52 52  ile (SQLITE_CORR
f8160 55 50 54 29 20 6f 72 0a 2a 2a 20 75 6e 6c 65 73  UPT) or.** unles
f8170 73 20 61 20 70 72 69 6f 72 20 6d 61 6c 6c 6f 63  s a prior malloc
f8180 28 29 20 66 61 69 6c 65 64 20 28 53 51 4c 49 54  () failed (SQLIT
f8190 45 5f 4e 4f 4d 45 4d 29 2e 20 20 41 70 70 72 6f  E_NOMEM).  Appro
f81a0 70 72 69 61 74 65 20 65 72 72 6f 72 0a 2a 2a 20  priate error.** 
f81b0 63 6f 64 65 73 20 61 72 65 20 72 65 74 75 72 6e  codes are return
f81c0 65 64 20 66 6f 72 20 61 6c 6c 20 74 68 65 73 65  ed for all these
f81d0 20 6f 63 63 61 73 69 6f 6e 73 2e 20 20 4f 74 68   occasions.  Oth
f81e0 65 72 77 69 73 65 2c 0a 2a 2a 20 53 51 4c 49 54  erwise,.** SQLIT
f81f0 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
f8200 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
f8210 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
f8220 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 50 61 67  agerRollback(Pag
f8230 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69  er *pPager){.  i
f8240 6e 74 20 72 63 3b 0a 20 20 50 41 47 45 52 54 52  nt rc;.  PAGERTR
f8250 41 43 45 32 28 22 52 4f 4c 4c 42 41 43 4b 20 25  ACE2("ROLLBACK %
f8260 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50  d\n", PAGERID(pP
f8270 61 67 65 72 29 29 3b 0a 20 20 69 66 28 20 4d 45  ager));.  if( ME
f8280 4d 44 42 20 29 7b 0a 20 20 20 20 50 67 48 64 72  MDB ){.    PgHdr
f8290 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28 70 3d 70   *p;.    for(p=p
f82a0 50 61 67 65 72 2d 3e 70 41 6c 6c 3b 20 70 3b 20  Pager->pAll; p; 
f82b0 70 3d 70 2d 3e 70 4e 65 78 74 41 6c 6c 29 7b 0a  p=p->pNextAll){.
f82c0 20 20 20 20 20 20 50 67 48 69 73 74 6f 72 79 20        PgHistory 
f82d0 2a 70 48 69 73 74 3b 0a 20 20 20 20 20 20 61 73  *pHist;.      as
f82e0 73 65 72 74 28 20 21 70 2d 3e 61 6c 77 61 79 73  sert( !p->always
f82f0 52 6f 6c 6c 62 61 63 6b 20 29 3b 0a 20 20 20 20  Rollback );.    
f8300 20 20 69 66 28 20 21 70 2d 3e 64 69 72 74 79 20    if( !p->dirty 
f8310 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ){.        asser
f8320 74 28 20 21 28 28 50 67 48 69 73 74 6f 72 79 20  t( !((PgHistory 
f8330 2a 29 50 47 48 44 52 5f 54 4f 5f 48 49 53 54 28  *)PGHDR_TO_HIST(
f8340 70 2c 20 70 50 61 67 65 72 29 29 2d 3e 70 4f 72  p, pPager))->pOr
f8350 69 67 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73  ig );.        as
f8360 73 65 72 74 28 20 21 28 28 50 67 48 69 73 74 6f  sert( !((PgHisto
f8370 72 79 20 2a 29 50 47 48 44 52 5f 54 4f 5f 48 49  ry *)PGHDR_TO_HI
f8380 53 54 28 70 2c 20 70 50 61 67 65 72 29 29 2d 3e  ST(p, pPager))->
f8390 70 53 74 6d 74 20 29 3b 0a 20 20 20 20 20 20 20  pStmt );.       
f83a0 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
f83b0 20 7d 0a 0a 20 20 20 20 20 20 70 48 69 73 74 20   }..      pHist 
f83c0 3d 20 50 47 48 44 52 5f 54 4f 5f 48 49 53 54 28  = PGHDR_TO_HIST(
f83d0 70 2c 20 70 50 61 67 65 72 29 3b 0a 20 20 20 20  p, pPager);.    
f83e0 20 20 69 66 28 20 70 48 69 73 74 2d 3e 70 4f 72    if( pHist->pOr
f83f0 69 67 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65  ig ){.        me
f8400 6d 63 70 79 28 50 47 48 44 52 5f 54 4f 5f 44 41  mcpy(PGHDR_TO_DA
f8410 54 41 28 70 29 2c 20 70 48 69 73 74 2d 3e 70 4f  TA(p), pHist->pO
f8420 72 69 67 2c 20 70 50 61 67 65 72 2d 3e 70 61 67  rig, pPager->pag
f8430 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20  eSize);.        
f8440 50 41 47 45 52 54 52 41 43 45 33 28 22 52 4f 4c  PAGERTRACE3("ROL
f8450 4c 42 41 43 4b 2d 50 41 47 45 20 25 64 20 6f 66  LBACK-PAGE %d of
f8460 20 25 64 5c 6e 22 2c 20 70 2d 3e 70 67 6e 6f 2c   %d\n", p->pgno,
f8470 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29   PAGERID(pPager)
f8480 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
f8490 20 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41          PAGERTRA
f84a0 43 45 33 28 22 50 41 47 45 20 25 64 20 69 73 20  CE3("PAGE %d is 
f84b0 63 6c 65 61 6e 20 6f 6e 20 25 64 5c 6e 22 2c 20  clean on %d\n", 
f84c0 70 2d 3e 70 67 6e 6f 2c 20 50 41 47 45 52 49 44  p->pgno, PAGERID
f84d0 28 70 50 61 67 65 72 29 29 3b 0a 20 20 20 20 20  (pPager));.     
f84e0 20 7d 0a 20 20 20 20 20 20 63 6c 65 61 72 48 69   }.      clearHi
f84f0 73 74 6f 72 79 28 70 48 69 73 74 29 3b 0a 20 20  story(pHist);.  
f8500 20 20 20 20 70 2d 3e 64 69 72 74 79 20 3d 20 30      p->dirty = 0
f8510 3b 0a 20 20 20 20 20 20 70 2d 3e 69 6e 4a 6f 75  ;.      p->inJou
f8520 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20  rnal = 0;.      
f8530 70 48 69 73 74 2d 3e 69 6e 53 74 6d 74 20 3d 20  pHist->inStmt = 
f8540 30 3b 0a 20 20 20 20 20 20 70 48 69 73 74 2d 3e  0;.      pHist->
f8550 70 50 72 65 76 53 74 6d 74 20 3d 20 70 48 69 73  pPrevStmt = pHis
f8560 74 2d 3e 70 4e 65 78 74 53 74 6d 74 20 3d 20 30  t->pNextStmt = 0
f8570 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67  ;.      if( pPag
f8580 65 72 2d 3e 78 52 65 69 6e 69 74 65 72 20 29 7b  er->xReiniter ){
f8590 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d  .        pPager-
f85a0 3e 78 52 65 69 6e 69 74 65 72 28 70 2c 20 70 50  >xReiniter(p, pP
f85b0 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b  ager->pageSize);
f85c0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
f85d0 20 20 20 70 50 61 67 65 72 2d 3e 70 44 69 72 74     pPager->pDirt
f85e0 79 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65  y = 0;.    pPage
f85f0 72 2d 3e 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20  r->pStmt = 0;.  
f8600 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65    pPager->dbSize
f8610 20 3d 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 44   = pPager->origD
f8620 62 53 69 7a 65 3b 0a 20 20 20 20 70 61 67 65 72  bSize;.    pager
f8630 5f 74 72 75 6e 63 61 74 65 5f 63 61 63 68 65 28  _truncate_cache(
f8640 70 50 61 67 65 72 29 3b 0a 20 20 20 20 70 50 61  pPager);.    pPa
f8650 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 3d  ger->stmtInUse =
f8660 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   0;.    pPager->
f8670 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 53 48  state = PAGER_SH
f8680 41 52 45 44 3b 0a 20 20 20 20 72 65 74 75 72 6e  ARED;.    return
f8690 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
f86a0 0a 20 20 70 61 67 65 72 45 6e 74 65 72 28 70 50  .  pagerEnter(pP
f86b0 61 67 65 72 29 3b 0a 20 20 69 66 28 20 21 70 50  ager);.  if( !pP
f86c0 61 67 65 72 2d 3e 64 69 72 74 79 43 61 63 68 65  ager->dirtyCache
f86d0 20 7c 7c 20 21 70 50 61 67 65 72 2d 3e 6a 6f 75   || !pPager->jou
f86e0 72 6e 61 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20  rnalOpen ){.    
f86f0 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74  rc = pager_end_t
f8700 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65  ransaction(pPage
f8710 72 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61  r, pPager->setMa
f8720 73 74 65 72 29 3b 0a 20 20 20 20 70 61 67 65 72  ster);.    pager
f8730 4c 65 61 76 65 28 70 50 61 67 65 72 29 3b 0a 20  Leave(pPager);. 
f8740 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
f8750 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  }..  if( pPager-
f8760 3e 65 72 72 43 6f 64 65 20 26 26 20 70 50 61 67  >errCode && pPag
f8770 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c  er->errCode!=SQL
f8780 49 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20  ITE_FULL ){.    
f8790 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  if( pPager->stat
f87a0 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49  e>=PAGER_EXCLUSI
f87b0 56 45 20 29 7b 0a 20 20 20 20 20 20 70 61 67 65  VE ){.      page
f87c0 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65  r_playback(pPage
f87d0 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20  r, 0);.    }.   
f87e0 20 70 61 67 65 72 4c 65 61 76 65 28 70 50 61 67   pagerLeave(pPag
f87f0 65 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  er);.    return 
f8800 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b  pPager->errCode;
f8810 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65  .  }.  if( pPage
f8820 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f  r->state==PAGER_
f8830 52 45 53 45 52 56 45 44 20 29 7b 0a 20 20 20 20  RESERVED ){.    
f8840 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 72 63 20  int rc2;.    rc 
f8850 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b  = pager_playback
f8860 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20  (pPager, 0);.   
f8870 20 72 63 32 20 3d 20 70 61 67 65 72 5f 65 6e 64   rc2 = pager_end
f8880 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61  _transaction(pPa
f8890 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 65 74  ger, pPager->set
f88a0 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 69 66 28  Master);.    if(
f88b0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
f88c0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 63 32  {.      rc = rc2
f88d0 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
f88e0 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f  .    rc = pager_
f88f0 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72 2c  playback(pPager,
f8900 20 30 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 70 61   0);.  }.  /* pa
f8910 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72  ger_reset(pPager
f8920 29 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e  ); */.  pPager->
f8930 64 62 53 69 7a 65 20 3d 20 2d 31 3b 0a 0a 20 20  dbSize = -1;..  
f8940 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  /* If an error o
f8950 63 63 75 72 73 20 64 75 72 69 6e 67 20 61 20 52  ccurs during a R
f8960 4f 4c 4c 42 41 43 4b 2c 20 77 65 20 63 61 6e 20  OLLBACK, we can 
f8970 6e 6f 20 6c 6f 6e 67 65 72 20 74 72 75 73 74 20  no longer trust 
f8980 74 68 65 20 70 61 67 65 72 0a 20 20 2a 2a 20 63  the pager.  ** c
f8990 61 63 68 65 2e 20 53 6f 20 63 61 6c 6c 20 70 61  ache. So call pa
f89a0 67 65 72 5f 65 72 72 6f 72 28 29 20 6f 6e 20 74  ger_error() on t
f89b0 68 65 20 77 61 79 20 6f 75 74 20 74 6f 20 6d 61  he way out to ma
f89c0 6b 65 20 61 6e 79 20 65 72 72 6f 72 20 0a 20 20  ke any error .  
f89d0 2a 2a 20 70 65 72 73 69 73 74 65 6e 74 2e 0a 20  ** persistent.. 
f89e0 20 2a 2f 0a 20 20 72 63 20 3d 20 70 61 67 65 72   */.  rc = pager
f89f0 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72  _error(pPager, r
f8a00 63 29 3b 0a 20 20 70 61 67 65 72 4c 65 61 76 65  c);.  pagerLeave
f8a10 28 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75  (pPager);.  retu
f8a20 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
f8a30 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74  Return TRUE if t
f8a40 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
f8a50 20 69 73 20 6f 70 65 6e 65 64 20 72 65 61 64 2d   is opened read-
f8a60 6f 6e 6c 79 2e 20 20 52 65 74 75 72 6e 20 46 41  only.  Return FA
f8a70 4c 53 45 0a 2a 2a 20 69 66 20 74 68 65 20 64 61  LSE.** if the da
f8a80 74 61 62 61 73 65 20 69 73 20 28 69 6e 20 74 68  tabase is (in th
f8a90 65 6f 72 79 29 20 77 72 69 74 61 62 6c 65 2e 0a  eory) writable..
f8aa0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
f8ab0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
f8ac0 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 50 61 67  erIsreadonly(Pag
f8ad0 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72  er *pPager){.  r
f8ae0 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 72 65  eturn pPager->re
f8af0 61 64 4f 6e 6c 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  adOnly;.}../*.**
f8b00 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
f8b10 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73  er of references
f8b20 20 74 6f 20 74 68 65 20 70 61 67 65 72 2e 0a 2a   to the pager..*
f8b30 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
f8b40 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
f8b50 72 52 65 66 63 6f 75 6e 74 28 50 61 67 65 72 20  rRefcount(Pager 
f8b60 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75  *pPager){.  retu
f8b70 72 6e 20 70 50 61 67 65 72 2d 3e 6e 52 65 66 3b  rn pPager->nRef;
f8b80 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .}..#ifdef SQLIT
f8b90 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69  E_TEST./*.** Thi
f8ba0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  s routine is use
f8bb0 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e  d for testing an
f8bc0 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e  d analysis only.
f8bd0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
f8be0 54 45 20 69 6e 74 20 2a 73 71 6c 69 74 65 33 50  TE int *sqlite3P
f8bf0 61 67 65 72 53 74 61 74 73 28 50 61 67 65 72 20  agerStats(Pager 
f8c00 2a 70 50 61 67 65 72 29 7b 0a 20 20 73 74 61 74  *pPager){.  stat
f8c10 69 63 20 69 6e 74 20 61 5b 31 31 5d 3b 0a 20 20  ic int a[11];.  
f8c20 61 5b 30 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e  a[0] = pPager->n
f8c30 52 65 66 3b 0a 20 20 61 5b 31 5d 20 3d 20 70 50  Ref;.  a[1] = pP
f8c40 61 67 65 72 2d 3e 6e 50 61 67 65 3b 0a 20 20 61  ager->nPage;.  a
f8c50 5b 32 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6d 78  [2] = pPager->mx
f8c60 50 61 67 65 3b 0a 20 20 61 5b 33 5d 20 3d 20 70  Page;.  a[3] = p
f8c70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20  Pager->dbSize;. 
f8c80 20 61 5b 34 5d 20 3d 20 70 50 61 67 65 72 2d 3e   a[4] = pPager->
f8c90 73 74 61 74 65 3b 0a 20 20 61 5b 35 5d 20 3d 20  state;.  a[5] = 
f8ca0 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b  pPager->errCode;
f8cb0 0a 20 20 61 5b 36 5d 20 3d 20 70 50 61 67 65 72  .  a[6] = pPager
f8cc0 2d 3e 6e 48 69 74 3b 0a 20 20 61 5b 37 5d 20 3d  ->nHit;.  a[7] =
f8cd0 20 70 50 61 67 65 72 2d 3e 6e 4d 69 73 73 3b 0a   pPager->nMiss;.
f8ce0 20 20 61 5b 38 5d 20 3d 20 30 3b 20 20 2f 2a 20    a[8] = 0;  /* 
f8cf0 55 73 65 64 20 74 6f 20 62 65 20 70 50 61 67 65  Used to be pPage
f8d00 72 2d 3e 6e 4f 76 66 6c 20 2a 2f 0a 20 20 61 5b  r->nOvfl */.  a[
f8d10 39 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 52 65  9] = pPager->nRe
f8d20 61 64 3b 0a 20 20 61 5b 31 30 5d 20 3d 20 70 50  ad;.  a[10] = pP
f8d30 61 67 65 72 2d 3e 6e 57 72 69 74 65 3b 0a 20 20  ager->nWrite;.  
f8d40 72 65 74 75 72 6e 20 61 3b 0a 7d 0a 23 65 6e 64  return a;.}.#end
f8d50 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  if../*.** Set th
f8d60 65 20 73 74 61 74 65 6d 65 6e 74 20 72 6f 6c 6c  e statement roll
f8d70 62 61 63 6b 20 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a  back point..**.*
f8d80 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  * This routine s
f8d90 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20  hould be called 
f8da0 77 69 74 68 20 74 68 65 20 74 72 61 6e 73 61 63  with the transac
f8db0 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 61 6c 72  tion journal alr
f8dc0 65 61 64 79 0a 2a 2a 20 6f 70 65 6e 2e 20 20 41  eady.** open.  A
f8dd0 20 6e 65 77 20 73 74 61 74 65 6d 65 6e 74 20 6a   new statement j
f8de0 6f 75 72 6e 61 6c 20 69 73 20 63 72 65 61 74 65  ournal is create
f8df0 64 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73  d that can be us
f8e00 65 64 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 0a 2a  ed to rollback.*
f8e10 2a 20 63 68 61 6e 67 65 73 20 6f 66 20 61 20 73  * changes of a s
f8e20 69 6e 67 6c 65 20 53 51 4c 20 63 6f 6d 6d 61 6e  ingle SQL comman
f8e30 64 20 77 69 74 68 69 6e 20 61 20 6c 61 72 67 65  d within a large
f8e40 72 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a  r transaction..*
f8e50 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
f8e60 65 72 53 74 6d 74 42 65 67 69 6e 28 50 61 67 65  erStmtBegin(Page
f8e70 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e  r *pPager){.  in
f8e80 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  t rc;.  assert( 
f8e90 21 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55  !pPager->stmtInU
f8ea0 73 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  se );.  assert( 
f8eb0 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50  pPager->state>=P
f8ec0 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 20  AGER_SHARED );. 
f8ed0 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
f8ee0 3e 64 62 53 69 7a 65 3e 3d 30 20 29 3b 0a 20 20  >dbSize>=0 );.  
f8ef0 50 41 47 45 52 54 52 41 43 45 32 28 22 53 54 4d  PAGERTRACE2("STM
f8f00 54 2d 42 45 47 49 4e 20 25 64 5c 6e 22 2c 20 50  T-BEGIN %d\n", P
f8f10 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 3b  AGERID(pPager));
f8f20 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a  .  if( MEMDB ){.
f8f30 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74      pPager->stmt
f8f40 49 6e 55 73 65 20 3d 20 31 3b 0a 20 20 20 20 70  InUse = 1;.    p
f8f50 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65 20  Pager->stmtSize 
f8f60 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  = pPager->dbSize
f8f70 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
f8f80 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66  ITE_OK;.  }.  if
f8f90 28 20 21 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  ( !pPager->journ
f8fa0 61 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20 70 50  alOpen ){.    pP
f8fb0 61 67 65 72 2d 3e 73 74 6d 74 41 75 74 6f 6f 70  ager->stmtAutoop
f8fc0 65 6e 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75  en = 1;.    retu
f8fd0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
f8fe0 7d 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  }.  assert( pPag
f8ff0 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20  er->journalOpen 
f9000 29 3b 0a 20 20 70 61 67 65 72 4c 65 61 76 65 28  );.  pagerLeave(
f9010 70 50 61 67 65 72 29 3b 0a 20 20 61 73 73 65 72  pPager);.  asser
f9020 74 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 53 74  t( pPager->pInSt
f9030 6d 74 3d 3d 30 20 29 3b 0a 20 20 70 50 61 67 65  mt==0 );.  pPage
f9040 72 2d 3e 70 49 6e 53 74 6d 74 20 3d 20 73 71 6c  r->pInStmt = sql
f9050 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65  ite3BitvecCreate
f9060 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29  (pPager->dbSize)
f9070 3b 0a 20 20 70 61 67 65 72 45 6e 74 65 72 28 70  ;.  pagerEnter(p
f9080 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 70 50  Pager);.  if( pP
f9090 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74 3d 3d 30  ager->pInStmt==0
f90a0 20 29 7b 0a 20 20 20 20 2f 2a 20 73 71 6c 69 74   ){.    /* sqlit
f90b0 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d  e3OsLock(pPager-
f90c0 3e 66 64 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b  >fd, SHARED_LOCK
f90d0 29 3b 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  ); */.    return
f90e0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
f90f0 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 73 74 6d   }.  pPager->stm
f9100 74 4a 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d  tJSize = pPager-
f9110 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 70  >journalOff;.  p
f9120 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65 20  Pager->stmtSize 
f9130 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  = pPager->dbSize
f9140 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74  ;.  pPager->stmt
f9150 48 64 72 4f 66 66 20 3d 20 30 3b 0a 20 20 70 50  HdrOff = 0;.  pP
f9160 61 67 65 72 2d 3e 73 74 6d 74 43 6b 73 75 6d 20  ager->stmtCksum 
f9170 3d 20 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49  = pPager->cksumI
f9180 6e 69 74 3b 0a 20 20 69 66 28 20 21 70 50 61 67  nit;.  if( !pPag
f9190 65 72 2d 3e 73 74 6d 74 4f 70 65 6e 20 29 7b 0a  er->stmtOpen ){.
f91a0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
f91b0 50 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 70 50  PagerOpentemp(pP
f91c0 61 67 65 72 2d 3e 70 56 66 73 2c 20 70 50 61 67  ager->pVfs, pPag
f91d0 65 72 2d 3e 73 74 66 64 2c 20 70 50 61 67 65 72  er->stfd, pPager
f91e0 2d 3e 7a 53 74 6d 74 4a 72 6e 6c 2c 0a 20 20 20  ->zStmtJrnl,.   
f91f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f9200 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
f9210 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41  E_OPEN_SUBJOURNA
f9220 4c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  L);.    if( rc )
f9230 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 74 6d  {.      goto stm
f9240 74 5f 62 65 67 69 6e 5f 66 61 69 6c 65 64 3b 0a  t_begin_failed;.
f9250 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72      }.    pPager
f9260 2d 3e 73 74 6d 74 4f 70 65 6e 20 3d 20 31 3b 0a  ->stmtOpen = 1;.
f9270 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74      pPager->stmt
f9280 4e 52 65 63 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  NRec = 0;.  }.  
f9290 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73  pPager->stmtInUs
f92a0 65 20 3d 20 31 3b 0a 20 20 72 65 74 75 72 6e 20  e = 1;.  return 
f92b0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 0a 73 74 6d  SQLITE_OK;. .stm
f92c0 74 5f 62 65 67 69 6e 5f 66 61 69 6c 65 64 3a 0a  t_begin_failed:.
f92d0 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70 49    if( pPager->pI
f92e0 6e 53 74 6d 74 20 29 7b 0a 20 20 20 20 73 71 6c  nStmt ){.    sql
f92f0 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f  ite3BitvecDestro
f9300 79 28 70 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d  y(pPager->pInStm
f9310 74 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  t);.    pPager->
f9320 70 49 6e 53 74 6d 74 20 3d 20 30 3b 0a 20 20 7d  pInStmt = 0;.  }
f9330 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
f9340 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
f9350 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  nt sqlite3PagerS
f9360 74 6d 74 42 65 67 69 6e 28 50 61 67 65 72 20 2a  tmtBegin(Pager *
f9370 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72  pPager){.  int r
f9380 63 3b 0a 20 20 70 61 67 65 72 45 6e 74 65 72 28  c;.  pagerEnter(
f9390 70 50 61 67 65 72 29 3b 0a 20 20 72 63 20 3d 20  pPager);.  rc = 
f93a0 70 61 67 65 72 53 74 6d 74 42 65 67 69 6e 28 70  pagerStmtBegin(p
f93b0 50 61 67 65 72 29 3b 0a 20 20 70 61 67 65 72 4c  Pager);.  pagerL
f93c0 65 61 76 65 28 70 50 61 67 65 72 29 3b 0a 20 20  eave(pPager);.  
f93d0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
f93e0 0a 2a 2a 20 43 6f 6d 6d 69 74 20 61 20 73 74 61  .** Commit a sta
f93f0 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  tement..*/.SQLIT
f9400 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
f9410 6c 69 74 65 33 50 61 67 65 72 53 74 6d 74 43 6f  lite3PagerStmtCo
f9420 6d 6d 69 74 28 50 61 67 65 72 20 2a 70 50 61 67  mmit(Pager *pPag
f9430 65 72 29 7b 0a 20 20 70 61 67 65 72 45 6e 74 65  er){.  pagerEnte
f9440 72 28 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28  r(pPager);.  if(
f9450 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55   pPager->stmtInU
f9460 73 65 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20  se ){.    PgHdr 
f9470 2a 70 50 67 2c 20 2a 70 4e 65 78 74 3b 0a 20 20  *pPg, *pNext;.  
f9480 20 20 50 41 47 45 52 54 52 41 43 45 32 28 22 53    PAGERTRACE2("S
f9490 54 4d 54 2d 43 4f 4d 4d 49 54 20 25 64 5c 6e 22  TMT-COMMIT %d\n"
f94a0 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72  , PAGERID(pPager
f94b0 29 29 3b 0a 20 20 20 20 69 66 28 20 21 4d 45 4d  ));.    if( !MEM
f94c0 44 42 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 73  DB ){.      /* s
f94d0 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65  qlite3OsTruncate
f94e0 28 70 50 61 67 65 72 2d 3e 73 74 66 64 2c 20 30  (pPager->stfd, 0
f94f0 29 3b 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  ); */.      sqli
f9500 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79  te3BitvecDestroy
f9510 28 70 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74  (pPager->pInStmt
f9520 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  );.      pPager-
f9530 3e 70 49 6e 53 74 6d 74 20 3d 20 30 3b 0a 20 20  >pInStmt = 0;.  
f9540 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66    }else{.      f
f9550 6f 72 28 70 50 67 3d 70 50 61 67 65 72 2d 3e 70  or(pPg=pPager->p
f9560 53 74 6d 74 3b 20 70 50 67 3b 20 70 50 67 3d 70  Stmt; pPg; pPg=p
f9570 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 50  Next){.        P
f9580 67 48 69 73 74 6f 72 79 20 2a 70 48 69 73 74 20  gHistory *pHist 
f9590 3d 20 50 47 48 44 52 5f 54 4f 5f 48 49 53 54 28  = PGHDR_TO_HIST(
f95a0 70 50 67 2c 20 70 50 61 67 65 72 29 3b 0a 20 20  pPg, pPager);.  
f95b0 20 20 20 20 20 20 70 4e 65 78 74 20 3d 20 70 48        pNext = pH
f95c0 69 73 74 2d 3e 70 4e 65 78 74 53 74 6d 74 3b 0a  ist->pNextStmt;.
f95d0 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
f95e0 70 48 69 73 74 2d 3e 69 6e 53 74 6d 74 20 29 3b  pHist->inStmt );
f95f0 0a 20 20 20 20 20 20 20 20 70 48 69 73 74 2d 3e  .        pHist->
f9600 69 6e 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20  inStmt = 0;.    
f9610 20 20 20 20 70 48 69 73 74 2d 3e 70 50 72 65 76      pHist->pPrev
f9620 53 74 6d 74 20 3d 20 70 48 69 73 74 2d 3e 70 4e  Stmt = pHist->pN
f9630 65 78 74 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20  extStmt = 0;.   
f9640 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
f9650 65 28 70 48 69 73 74 2d 3e 70 53 74 6d 74 29 3b  e(pHist->pStmt);
f9660 0a 20 20 20 20 20 20 20 20 70 48 69 73 74 2d 3e  .        pHist->
f9670 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 20  pStmt = 0;.     
f9680 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61   }.    }.    pPa
f9690 67 65 72 2d 3e 73 74 6d 74 4e 52 65 63 20 3d 20  ger->stmtNRec = 
f96a0 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73  0;.    pPager->s
f96b0 74 6d 74 49 6e 55 73 65 20 3d 20 30 3b 0a 20 20  tmtInUse = 0;.  
f96c0 20 20 70 50 61 67 65 72 2d 3e 70 53 74 6d 74 20    pPager->pStmt 
f96d0 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65  = 0;.  }.  pPage
f96e0 72 2d 3e 73 74 6d 74 41 75 74 6f 6f 70 65 6e 20  r->stmtAutoopen 
f96f0 3d 20 30 3b 0a 20 20 70 61 67 65 72 4c 65 61 76  = 0;.  pagerLeav
f9700 65 28 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74  e(pPager);.  ret
f9710 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
f9720 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b  ../*.** Rollback
f9730 20 61 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f   a statement..*/
f9740 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
f9750 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
f9760 53 74 6d 74 52 6f 6c 6c 62 61 63 6b 28 50 61 67  StmtRollback(Pag
f9770 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69  er *pPager){.  i
f9780 6e 74 20 72 63 3b 0a 20 20 70 61 67 65 72 45 6e  nt rc;.  pagerEn
f9790 74 65 72 28 70 50 61 67 65 72 29 3b 0a 20 20 69  ter(pPager);.  i
f97a0 66 28 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 49  f( pPager->stmtI
f97b0 6e 55 73 65 20 29 7b 0a 20 20 20 20 50 41 47 45  nUse ){.    PAGE
f97c0 52 54 52 41 43 45 32 28 22 53 54 4d 54 2d 52 4f  RTRACE2("STMT-RO
f97d0 4c 4c 42 41 43 4b 20 25 64 5c 6e 22 2c 20 50 41  LLBACK %d\n", PA
f97e0 47 45 52 49 44 28 70 50 61 67 65 72 29 29 3b 0a  GERID(pPager));.
f97f0 20 20 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b      if( MEMDB ){
f9800 0a 20 20 20 20 20 20 50 67 48 64 72 20 2a 70 50  .      PgHdr *pP
f9810 67 3b 0a 20 20 20 20 20 20 50 67 48 69 73 74 6f  g;.      PgHisto
f9820 72 79 20 2a 70 48 69 73 74 3b 0a 20 20 20 20 20  ry *pHist;.     
f9830 20 66 6f 72 28 70 50 67 3d 70 50 61 67 65 72 2d   for(pPg=pPager-
f9840 3e 70 53 74 6d 74 3b 20 70 50 67 3b 20 70 50 67  >pStmt; pPg; pPg
f9850 3d 70 48 69 73 74 2d 3e 70 4e 65 78 74 53 74 6d  =pHist->pNextStm
f9860 74 29 7b 0a 20 20 20 20 20 20 20 20 70 48 69 73  t){.        pHis
f9870 74 20 3d 20 50 47 48 44 52 5f 54 4f 5f 48 49 53  t = PGHDR_TO_HIS
f9880 54 28 70 50 67 2c 20 70 50 61 67 65 72 29 3b 0a  T(pPg, pPager);.
f9890 20 20 20 20 20 20 20 20 69 66 28 20 70 48 69 73          if( pHis
f98a0 74 2d 3e 70 53 74 6d 74 20 29 7b 0a 20 20 20 20  t->pStmt ){.    
f98b0 20 20 20 20 20 20 6d 65 6d 63 70 79 28 50 47 48        memcpy(PGH
f98c0 44 52 5f 54 4f 5f 44 41 54 41 28 70 50 67 29 2c  DR_TO_DATA(pPg),
f98d0 20 70 48 69 73 74 2d 3e 70 53 74 6d 74 2c 20 70   pHist->pStmt, p
f98e0 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29  Pager->pageSize)
f98f0 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
f9900 74 65 33 5f 66 72 65 65 28 70 48 69 73 74 2d 3e  te3_free(pHist->
f9910 70 53 74 6d 74 29 3b 0a 20 20 20 20 20 20 20 20  pStmt);.        
f9920 20 20 70 48 69 73 74 2d 3e 70 53 74 6d 74 20 3d    pHist->pStmt =
f9930 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   0;.        }.  
f9940 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67      }.      pPag
f9950 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70 50 61  er->dbSize = pPa
f9960 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65 3b 0a 20  ger->stmtSize;. 
f9970 20 20 20 20 20 70 61 67 65 72 5f 74 72 75 6e 63       pager_trunc
f9980 61 74 65 5f 63 61 63 68 65 28 70 50 61 67 65 72  ate_cache(pPager
f9990 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  );.      rc = SQ
f99a0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c  LITE_OK;.    }el
f99b0 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70  se{.      rc = p
f99c0 61 67 65 72 5f 73 74 6d 74 5f 70 6c 61 79 62 61  ager_stmt_playba
f99d0 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  ck(pPager);.    
f99e0 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67  }.    sqlite3Pag
f99f0 65 72 53 74 6d 74 43 6f 6d 6d 69 74 28 70 50 61  erStmtCommit(pPa
f9a00 67 65 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ger);.  }else{. 
f9a10 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
f9a20 4b 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d  K;.  }.  pPager-
f9a30 3e 73 74 6d 74 41 75 74 6f 6f 70 65 6e 20 3d 20  >stmtAutoopen = 
f9a40 30 3b 0a 20 20 70 61 67 65 72 4c 65 61 76 65 28  0;.  pagerLeave(
f9a50 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72  pPager);.  retur
f9a60 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
f9a70 65 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 70  eturn the full p
f9a80 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 64  athname of the d
f9a90 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f  atabase file..*/
f9aa0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
f9ab0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
f9ac0 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d 65  te3PagerFilename
f9ad0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
f9ae0 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72  .  return pPager
f9af0 2d 3e 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a  ->zFilename;.}..
f9b00 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
f9b10 20 56 46 53 20 73 74 72 75 63 74 75 72 65 20 66   VFS structure f
f9b20 6f 72 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2f  or the pager..*/
f9b30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
f9b40 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 66  const sqlite3_vf
f9b50 73 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 56  s *sqlite3PagerV
f9b60 66 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  fs(Pager *pPager
f9b70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67  ){.  return pPag
f9b80 65 72 2d 3e 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a  er->pVfs;.}../*.
f9b90 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 69  ** Return the fi
f9ba0 6c 65 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68  le handle for th
f9bb0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
f9bc0 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69  associated.** wi
f9bd0 74 68 20 74 68 65 20 70 61 67 65 72 2e 20 20 54  th the pager.  T
f9be0 68 69 73 20 6d 69 67 68 74 20 72 65 74 75 72 6e  his might return
f9bf0 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 66 69 6c   NULL if the fil
f9c00 65 20 68 61 73 0a 2a 2a 20 6e 6f 74 20 79 65 74  e has.** not yet
f9c10 20 62 65 65 6e 20 6f 70 65 6e 65 64 2e 0a 2a 2f   been opened..*/
f9c20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
f9c30 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 71  sqlite3_file *sq
f9c40 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 50  lite3PagerFile(P
f9c50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
f9c60 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e   return pPager->
f9c70 66 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  fd;.}../*.** Ret
f9c80 75 72 6e 20 74 68 65 20 64 69 72 65 63 74 6f 72  urn the director
f9c90 79 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  y of the databas
f9ca0 65 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54  e file..*/.SQLIT
f9cb0 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
f9cc0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67  char *sqlite3Pag
f9cd0 65 72 44 69 72 6e 61 6d 65 28 50 61 67 65 72 20  erDirname(Pager 
f9ce0 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75  *pPager){.  retu
f9cf0 72 6e 20 70 50 61 67 65 72 2d 3e 7a 44 69 72 65  rn pPager->zDire
f9d00 63 74 6f 72 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ctory;.}../*.** 
f9d10 52 65 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20  Return the full 
f9d20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20  pathname of the 
f9d30 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f  journal file..*/
f9d40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
f9d50 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
f9d60 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 6e  te3PagerJournaln
f9d70 61 6d 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  ame(Pager *pPage
f9d80 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61  r){.  return pPa
f9d90 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 3b 0a 7d  ger->zJournal;.}
f9da0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
f9db0 72 75 65 20 69 66 20 66 73 79 6e 63 28 29 20 63  rue if fsync() c
f9dc0 61 6c 6c 73 20 61 72 65 20 64 69 73 61 62 6c 65  alls are disable
f9dd0 64 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 72  d for this pager
f9de0 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45 0a  .  Return FALSE.
f9df0 2a 2a 20 69 66 20 66 73 79 6e 63 28 29 73 20 61  ** if fsync()s a
f9e00 72 65 20 65 78 65 63 75 74 65 64 20 6e 6f 72 6d  re executed norm
f9e10 61 6c 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ally..*/.SQLITE_
f9e20 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
f9e30 74 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 28 50  te3PagerNosync(P
f9e40 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
f9e50 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e   return pPager->
f9e60 6e 6f 53 79 6e 63 3b 0a 7d 0a 0a 23 69 66 64 65  noSync;.}..#ifde
f9e70 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44  f SQLITE_HAS_COD
f9e80 45 43 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  EC./*.** Set the
f9e90 20 63 6f 64 65 63 20 66 6f 72 20 74 68 69 73 20   codec for this 
f9ea0 70 61 67 65 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f  pager.*/.SQLITE_
f9eb0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
f9ec0 69 74 65 33 50 61 67 65 72 53 65 74 43 6f 64 65  ite3PagerSetCode
f9ed0 63 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  c(.  Pager *pPag
f9ee0 65 72 2c 0a 20 20 76 6f 69 64 20 2a 28 2a 78 43  er,.  void *(*xC
f9ef0 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76 6f 69 64  odec)(void*,void
f9f00 2a 2c 50 67 6e 6f 2c 69 6e 74 29 2c 0a 20 20 76  *,Pgno,int),.  v
f9f10 6f 69 64 20 2a 70 43 6f 64 65 63 41 72 67 0a 29  oid *pCodecArg.)
f9f20 7b 0a 20 20 70 50 61 67 65 72 2d 3e 78 43 6f 64  {.  pPager->xCod
f9f30 65 63 20 3d 20 78 43 6f 64 65 63 3b 0a 20 20 70  ec = xCodec;.  p
f9f40 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 41 72 67  Pager->pCodecArg
f9f50 20 3d 20 70 43 6f 64 65 63 41 72 67 3b 0a 7d 0a   = pCodecArg;.}.
f9f60 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20  #endif..#ifndef 
f9f70 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
f9f80 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 4d 6f 76  VACUUM./*.** Mov
f9f90 65 20 74 68 65 20 70 61 67 65 20 70 50 67 20 74  e the page pPg t
f9fa0 6f 20 6c 6f 63 61 74 69 6f 6e 20 70 67 6e 6f 20  o location pgno 
f9fb0 69 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a  in the file..**.
f9fc0 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20 62 65  ** There must be
f9fd0 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 73 20 74   no references t
f9fe0 6f 20 74 68 65 20 70 61 67 65 20 70 72 65 76 69  o the page previ
f9ff0 6f 75 73 6c 79 20 6c 6f 63 61 74 65 64 20 61 74  ously located at
fa000 0a 2a 2a 20 70 67 6e 6f 20 28 77 68 69 63 68 20  .** pgno (which 
fa010 77 65 20 63 61 6c 6c 20 70 50 67 4f 6c 64 29 20  we call pPgOld) 
fa020 74 68 6f 75 67 68 20 74 68 61 74 20 70 61 67 65  though that page
fa030 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62   is allowed to b
fa040 65 0a 2a 2a 20 69 6e 20 63 61 63 68 65 2e 20 20  e.** in cache.  
fa050 49 66 20 74 68 65 20 70 61 67 65 20 70 72 65 76  If the page prev
fa060 69 6f 75 73 20 6c 6f 63 61 74 65 64 20 61 74 20  ious located at 
fa070 70 67 6e 6f 20 69 73 20 6e 6f 74 20 61 6c 72 65  pgno is not alre
fa080 61 64 79 0a 2a 2a 20 69 6e 20 74 68 65 20 72 6f  ady.** in the ro
fa090 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20  llback journal, 
fa0a0 69 74 20 69 73 20 6e 6f 74 20 70 75 74 20 74 68  it is not put th
fa0b0 65 72 65 20 62 79 20 62 79 20 74 68 69 73 20 72  ere by by this r
fa0c0 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 52 65  outine..**.** Re
fa0d0 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20  ferences to the 
fa0e0 70 61 67 65 20 70 50 67 20 72 65 6d 61 69 6e 20  page pPg remain 
fa0f0 76 61 6c 69 64 2e 20 55 70 64 61 74 69 6e 67 20  valid. Updating 
fa100 61 6e 79 0a 2a 2a 20 6d 65 74 61 2d 64 61 74 61  any.** meta-data
fa110 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
fa120 20 70 50 67 20 28 69 2e 65 2e 20 64 61 74 61 20   pPg (i.e. data 
fa130 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 6e 45  stored in the nE
fa140 78 74 72 61 20 62 79 74 65 73 0a 2a 2a 20 61 6c  xtra bytes.** al
fa150 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69  located along wi
fa160 74 68 20 74 68 65 20 70 61 67 65 29 20 69 73 20  th the page) is 
fa170 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69  the responsibili
fa180 74 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72  ty of the caller
fa190 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61  ..**.** A transa
fa1a0 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 61 63  ction must be ac
fa1b0 74 69 76 65 20 77 68 65 6e 20 74 68 69 73 20 72  tive when this r
fa1c0 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
fa1d0 2e 20 49 74 20 75 73 65 64 20 74 6f 20 62 65 0a  . It used to be.
fa1e0 2a 2a 20 72 65 71 75 69 72 65 64 20 74 68 61 74  ** required that
fa1f0 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61   a statement tra
fa200 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 6e 6f 74  nsaction was not
fa210 20 61 63 74 69 76 65 2c 20 62 75 74 20 74 68 69   active, but thi
fa220 73 20 72 65 73 74 72 69 63 74 69 6f 6e 0a 2a 2a  s restriction.**
fa230 20 68 61 73 20 62 65 65 6e 20 72 65 6d 6f 76 65   has been remove
fa240 64 20 28 43 52 45 41 54 45 20 49 4e 44 45 58 20  d (CREATE INDEX 
fa250 6e 65 65 64 73 20 74 6f 20 6d 6f 76 65 20 61 20  needs to move a 
fa260 70 61 67 65 20 77 68 65 6e 20 61 20 73 74 61 74  page when a stat
fa270 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63  ement.** transac
fa280 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 29 2e  tion is active).
fa290 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
fa2a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
fa2b0 67 65 72 4d 6f 76 65 70 61 67 65 28 50 61 67 65  gerMovepage(Page
fa2c0 72 20 2a 70 50 61 67 65 72 2c 20 44 62 50 61 67  r *pPager, DbPag
fa2d0 65 20 2a 70 50 67 2c 20 50 67 6e 6f 20 70 67 6e  e *pPg, Pgno pgn
fa2e0 6f 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 67  o){.  PgHdr *pPg
fa2f0 4f 6c 64 3b 20 20 2f 2a 20 54 68 65 20 70 61 67  Old;  /* The pag
fa300 65 20 62 65 69 6e 67 20 6f 76 65 72 77 72 69 74  e being overwrit
fa310 74 65 6e 2e 20 2a 2f 0a 20 20 69 6e 74 20 68 3b  ten. */.  int h;
fa320 0a 20 20 50 67 6e 6f 20 6e 65 65 64 53 79 6e 63  .  Pgno needSync
fa330 50 67 6e 6f 20 3d 20 30 3b 0a 0a 20 20 70 61 67  Pgno = 0;..  pag
fa340 65 72 45 6e 74 65 72 28 70 50 61 67 65 72 29 3b  erEnter(pPager);
fa350 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e  .  assert( pPg->
fa360 6e 52 65 66 3e 30 20 29 3b 0a 0a 20 20 50 41 47  nRef>0 );..  PAG
fa370 45 52 54 52 41 43 45 35 28 22 4d 4f 56 45 20 25  ERTRACE5("MOVE %
fa380 64 20 70 61 67 65 20 25 64 20 28 6e 65 65 64 53  d page %d (needS
fa390 79 6e 63 3d 25 64 29 20 6d 6f 76 65 73 20 74 6f  ync=%d) moves to
fa3a0 20 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 50   %d\n", .      P
fa3b0 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20  AGERID(pPager), 
fa3c0 70 50 67 2d 3e 70 67 6e 6f 2c 20 70 50 67 2d 3e  pPg->pgno, pPg->
fa3d0 6e 65 65 64 53 79 6e 63 2c 20 70 67 6e 6f 29 3b  needSync, pgno);
fa3e0 0a 20 20 49 4f 54 52 41 43 45 28 28 22 4d 4f 56  .  IOTRACE(("MOV
fa3f0 45 20 25 70 20 25 64 20 25 64 5c 6e 22 2c 20 70  E %p %d %d\n", p
fa400 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f  Pager, pPg->pgno
fa410 2c 20 70 67 6e 6f 29 29 0a 0a 20 20 70 61 67 65  , pgno))..  page
fa420 72 5f 67 65 74 5f 63 6f 6e 74 65 6e 74 28 70 50  r_get_content(pP
fa430 67 29 3b 0a 20 20 69 66 28 20 70 50 67 2d 3e 6e  g);.  if( pPg->n
fa440 65 65 64 53 79 6e 63 20 29 7b 0a 20 20 20 20 6e  eedSync ){.    n
fa450 65 65 64 53 79 6e 63 50 67 6e 6f 20 3d 20 70 50  eedSyncPgno = pP
fa460 67 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 61 73 73  g->pgno;.    ass
fa470 65 72 74 28 20 70 50 67 2d 3e 69 6e 4a 6f 75 72  ert( pPg->inJour
fa480 6e 61 6c 20 7c 7c 20 28 69 6e 74 29 70 67 6e 6f  nal || (int)pgno
fa490 3e 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62 53  >pPager->origDbS
fa4a0 69 7a 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ize );.    asser
fa4b0 74 28 20 70 50 67 2d 3e 64 69 72 74 79 20 29 3b  t( pPg->dirty );
fa4c0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
fa4d0 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 29 3b  ger->needSync );
fa4e0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 6e 6c 69 6e  .  }..  /* Unlin
fa4f0 6b 20 70 50 67 20 66 72 6f 6d 20 69 74 73 20 68  k pPg from its h
fa500 61 73 68 2d 63 68 61 69 6e 20 2a 2f 0a 20 20 75  ash-chain */.  u
fa510 6e 6c 69 6e 6b 48 61 73 68 43 68 61 69 6e 28 70  nlinkHashChain(p
fa520 50 61 67 65 72 2c 20 70 50 67 29 3b 0a 0a 20 20  Pager, pPg);..  
fa530 2f 2a 20 49 66 20 74 68 65 20 63 61 63 68 65 20  /* If the cache 
fa540 63 6f 6e 74 61 69 6e 73 20 61 20 70 61 67 65 20  contains a page 
fa550 77 69 74 68 20 70 61 67 65 2d 6e 75 6d 62 65 72  with page-number
fa560 20 70 67 6e 6f 2c 20 72 65 6d 6f 76 65 20 69 74   pgno, remove it
fa570 0a 20 20 2a 2a 20 66 72 6f 6d 20 69 74 73 20 68  .  ** from its h
fa580 61 73 68 20 63 68 61 69 6e 2e 20 41 6c 73 6f 2c  ash chain. Also,
fa590 20 69 66 20 74 68 65 20 50 67 48 64 72 2e 6e 65   if the PgHdr.ne
fa5a0 65 64 53 79 6e 63 20 77 61 73 20 73 65 74 20 66  edSync was set f
fa5b0 6f 72 20 0a 20 20 2a 2a 20 70 61 67 65 20 70 67  or .  ** page pg
fa5c0 6e 6f 20 62 65 66 6f 72 65 20 74 68 65 20 27 6d  no before the 'm
fa5d0 6f 76 65 27 20 6f 70 65 72 61 74 69 6f 6e 2c 20  ove' operation, 
fa5e0 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72  it needs to be r
fa5f0 65 74 61 69 6e 65 64 20 0a 20 20 2a 2a 20 66 6f  etained .  ** fo
fa600 72 20 74 68 65 20 70 61 67 65 20 6d 6f 76 65 64  r the page moved
fa610 20 74 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 70   there..  */.  p
fa620 50 67 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30  Pg->needSync = 0
fa630 3b 0a 20 20 70 50 67 4f 6c 64 20 3d 20 70 61 67  ;.  pPgOld = pag
fa640 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72  er_lookup(pPager
fa650 2c 20 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70  , pgno);.  if( p
fa660 50 67 4f 6c 64 20 29 7b 0a 20 20 20 20 61 73 73  PgOld ){.    ass
fa670 65 72 74 28 20 70 50 67 4f 6c 64 2d 3e 6e 52 65  ert( pPgOld->nRe
fa680 66 3d 3d 30 20 29 3b 0a 20 20 20 20 75 6e 6c 69  f==0 );.    unli
fa690 6e 6b 48 61 73 68 43 68 61 69 6e 28 70 50 61 67  nkHashChain(pPag
fa6a0 65 72 2c 20 70 50 67 4f 6c 64 29 3b 0a 20 20 20  er, pPgOld);.   
fa6b0 20 6d 61 6b 65 43 6c 65 61 6e 28 70 50 67 4f 6c   makeClean(pPgOl
fa6c0 64 29 3b 0a 20 20 20 20 70 50 67 2d 3e 6e 65 65  d);.    pPg->nee
fa6d0 64 53 79 6e 63 20 3d 20 70 50 67 4f 6c 64 2d 3e  dSync = pPgOld->
fa6e0 6e 65 65 64 53 79 6e 63 3b 0a 20 20 7d 65 6c 73  needSync;.  }els
fa6f0 65 7b 0a 20 20 20 20 70 50 67 2d 3e 6e 65 65 64  e{.    pPg->need
fa700 53 79 6e 63 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  Sync = 0;.  }.  
fa710 70 50 67 2d 3e 69 6e 4a 6f 75 72 6e 61 6c 20 3d  pPg->inJournal =
fa720 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65   sqlite3BitvecTe
fa730 73 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f  st(pPager->pInJo
fa740 75 72 6e 61 6c 2c 20 70 67 6e 6f 29 3b 0a 0a 20  urnal, pgno);.. 
fa750 20 2f 2a 20 43 68 61 6e 67 65 20 74 68 65 20 70   /* Change the p
fa760 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 70  age number for p
fa770 50 67 20 61 6e 64 20 69 6e 73 65 72 74 20 69 74  Pg and insert it
fa780 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 20 68 61   into the new ha
fa790 73 68 2d 63 68 61 69 6e 2e 20 2a 2f 0a 20 20 61  sh-chain. */.  a
fa7a0 73 73 65 72 74 28 20 70 67 6e 6f 21 3d 30 20 29  ssert( pgno!=0 )
fa7b0 3b 0a 20 20 70 50 67 2d 3e 70 67 6e 6f 20 3d 20  ;.  pPg->pgno = 
fa7c0 70 67 6e 6f 3b 0a 20 20 68 20 3d 20 70 67 6e 6f  pgno;.  h = pgno
fa7d0 20 26 20 28 70 50 61 67 65 72 2d 3e 6e 48 61 73   & (pPager->nHas
fa7e0 68 2d 31 29 3b 0a 20 20 69 66 28 20 70 50 61 67  h-1);.  if( pPag
fa7f0 65 72 2d 3e 61 48 61 73 68 5b 68 5d 20 29 7b 0a  er->aHash[h] ){.
fa800 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
fa810 65 72 2d 3e 61 48 61 73 68 5b 68 5d 2d 3e 70 50  er->aHash[h]->pP
fa820 72 65 76 48 61 73 68 3d 3d 30 20 29 3b 0a 20 20  revHash==0 );.  
fa830 20 20 70 50 61 67 65 72 2d 3e 61 48 61 73 68 5b    pPager->aHash[
fa840 68 5d 2d 3e 70 50 72 65 76 48 61 73 68 20 3d 20  h]->pPrevHash = 
fa850 70 50 67 3b 0a 20 20 7d 0a 20 20 70 50 67 2d 3e  pPg;.  }.  pPg->
fa860 70 4e 65 78 74 48 61 73 68 20 3d 20 70 50 61 67  pNextHash = pPag
fa870 65 72 2d 3e 61 48 61 73 68 5b 68 5d 3b 0a 20 20  er->aHash[h];.  
fa880 70 50 61 67 65 72 2d 3e 61 48 61 73 68 5b 68 5d  pPager->aHash[h]
fa890 20 3d 20 70 50 67 3b 0a 20 20 70 50 67 2d 3e 70   = pPg;.  pPg->p
fa8a0 50 72 65 76 48 61 73 68 20 3d 20 30 3b 0a 0a 20  PrevHash = 0;.. 
fa8b0 20 6d 61 6b 65 44 69 72 74 79 28 70 50 67 29 3b   makeDirty(pPg);
fa8c0 0a 20 20 70 50 61 67 65 72 2d 3e 64 69 72 74 79  .  pPager->dirty
fa8d0 43 61 63 68 65 20 3d 20 31 3b 0a 20 20 70 50 61  Cache = 1;.  pPa
fa8e0 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20  ger->dbModified 
fa8f0 3d 20 31 3b 0a 0a 20 20 69 66 28 20 6e 65 65 64  = 1;..  if( need
fa900 53 79 6e 63 50 67 6e 6f 20 29 7b 0a 20 20 20 20  SyncPgno ){.    
fa910 2f 2a 20 49 66 20 6e 65 65 64 53 79 6e 63 50 67  /* If needSyncPg
fa920 6e 6f 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  no is non-zero, 
fa930 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  then the journal
fa940 20 66 69 6c 65 20 6e 65 65 64 73 20 74 6f 20 62   file needs to b
fa950 65 20 0a 20 20 20 20 2a 2a 20 73 79 6e 63 28 29  e .    ** sync()
fa960 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 64 61  ed before any da
fa970 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f  ta is written to
fa980 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 70   database file p
fa990 61 67 65 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f  age needSyncPgno
fa9a0 2e 0a 20 20 20 20 2a 2a 20 43 75 72 72 65 6e 74  ..    ** Current
fa9b0 6c 79 2c 20 6e 6f 20 73 75 63 68 20 70 61 67 65  ly, no such page
fa9c0 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20 70   exists in the p
fa9d0 61 67 65 2d 63 61 63 68 65 20 61 6e 64 20 74 68  age-cache and th
fa9e0 65 20 0a 20 20 20 20 2a 2a 20 50 61 67 65 72 2e  e .    ** Pager.
fa9f0 70 49 6e 4a 6f 75 72 6e 61 6c 20 62 69 74 20 68  pInJournal bit h
faa00 61 73 20 62 65 65 6e 20 73 65 74 2e 20 54 68 69  as been set. Thi
faa10 73 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65  s needs to be re
faa20 6d 65 64 69 65 64 20 62 79 20 6c 6f 61 64 69 6e  medied by loadin
faa30 67 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 61 67  g.    ** the pag
faa40 65 20 69 6e 74 6f 20 74 68 65 20 70 61 67 65 72  e into the pager
faa50 2d 63 61 63 68 65 20 61 6e 64 20 73 65 74 74 69  -cache and setti
faa60 6e 67 20 74 68 65 20 50 67 48 64 72 2e 6e 65 65  ng the PgHdr.nee
faa70 64 53 79 6e 63 20 66 6c 61 67 2e 0a 20 20 20 20  dSync flag..    
faa80 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65  **.    ** If the
faa90 20 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f 61 64   attempt to load
faaa0 20 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74   the page into t
faab0 68 65 20 70 61 67 65 2d 63 61 63 68 65 20 66 61  he page-cache fa
faac0 69 6c 73 2c 20 28 64 75 65 0a 20 20 20 20 2a 2a  ils, (due.    **
faad0 20 74 6f 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f   to a malloc() o
faae0 72 20 49 4f 20 66 61 69 6c 75 72 65 29 2c 20 63  r IO failure), c
faaf0 6c 65 61 72 20 74 68 65 20 62 69 74 20 69 6e 20  lear the bit in 
fab00 74 68 65 20 70 49 6e 4a 6f 75 72 6e 61 6c 5b 5d  the pInJournal[]
fab10 0a 20 20 20 20 2a 2a 20 61 72 72 61 79 2e 20 4f  .    ** array. O
fab20 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65  therwise, if the
fab30 20 70 61 67 65 20 69 73 20 6c 6f 61 64 65 64 20   page is loaded 
fab40 61 6e 64 20 77 72 69 74 74 65 6e 20 61 67 61 69  and written agai
fab50 6e 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 69 73  n in.    ** this
fab60 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74   transaction, it
fab70 20 6d 61 79 20 62 65 20 77 72 69 74 74 65 6e 20   may be written 
fab80 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
fab90 66 69 6c 65 20 62 65 66 6f 72 65 0a 20 20 20 20  file before.    
faba0 2a 2a 20 69 74 20 69 73 20 73 79 6e 63 65 64 20  ** it is synced 
fabb0 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  into the journal
fabc0 20 66 69 6c 65 2e 20 54 68 69 73 20 77 61 79 2c   file. This way,
fabd0 20 69 74 20 6d 61 79 20 65 6e 64 20 75 70 20 69   it may end up i
fabe0 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75  n.    ** the jou
fabf0 72 6e 61 6c 20 66 69 6c 65 20 74 77 69 63 65 2c  rnal file twice,
fac00 20 62 75 74 20 74 68 61 74 20 69 73 20 6e 6f 74   but that is not
fac10 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 20 20   a problem..    
fac20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 71  **.    ** The sq
fac30 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20  lite3PagerGet() 
fac40 63 61 6c 6c 20 6d 61 79 20 63 61 75 73 65 20 74  call may cause t
fac50 68 65 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 73 79  he journal to sy
fac60 6e 63 2e 20 53 6f 20 6d 61 6b 65 0a 20 20 20 20  nc. So make.    
fac70 2a 2a 20 73 75 72 65 20 74 68 65 20 50 61 67 65  ** sure the Page
fac80 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 20  r.needSync flag 
fac90 69 73 20 73 65 74 20 74 6f 6f 2e 0a 20 20 20 20  is set too..    
faca0 2a 2f 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20  */.    int rc;. 
facb0 20 20 20 50 67 48 64 72 20 2a 70 50 67 48 64 72     PgHdr *pPgHdr
facc0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  ;.    assert( pP
facd0 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 29  ager->needSync )
face0 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
facf0 65 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65  e3PagerGet(pPage
fad00 72 2c 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2c  r, needSyncPgno,
fad10 20 26 70 50 67 48 64 72 29 3b 0a 20 20 20 20 69   &pPgHdr);.    i
fad20 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
fad30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50   ){.      if( pP
fad40 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c  ager->pInJournal
fad50 20 26 26 20 28 69 6e 74 29 6e 65 65 64 53 79 6e   && (int)needSyn
fad60 63 50 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 6f  cPgno<=pPager->o
fad70 72 69 67 44 62 53 69 7a 65 20 29 7b 0a 20 20 20  rigDbSize ){.   
fad80 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76       sqlite3Bitv
fad90 65 63 43 6c 65 61 72 28 70 50 61 67 65 72 2d 3e  ecClear(pPager->
fada0 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 6e 65 65 64  pInJournal, need
fadb0 53 79 6e 63 50 67 6e 6f 29 3b 0a 20 20 20 20 20  SyncPgno);.     
fadc0 20 7d 0a 20 20 20 20 20 20 70 61 67 65 72 4c 65   }.      pagerLe
fadd0 61 76 65 28 70 50 61 67 65 72 29 3b 0a 20 20 20  ave(pPager);.   
fade0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
fadf0 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e    }.    pPager->
fae00 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20  needSync = 1;.  
fae10 20 20 70 50 67 48 64 72 2d 3e 6e 65 65 64 53 79    pPgHdr->needSy
fae20 6e 63 20 3d 20 31 3b 0a 20 20 20 20 70 50 67 48  nc = 1;.    pPgH
fae30 64 72 2d 3e 69 6e 4a 6f 75 72 6e 61 6c 20 3d 20  dr->inJournal = 
fae40 31 3b 0a 20 20 20 20 6d 61 6b 65 44 69 72 74 79  1;.    makeDirty
fae50 28 70 50 67 48 64 72 29 3b 0a 20 20 20 20 73 71  (pPgHdr);.    sq
fae60 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
fae70 70 50 67 48 64 72 29 3b 0a 20 20 7d 0a 0a 20 20  pPgHdr);.  }..  
fae80 70 61 67 65 72 4c 65 61 76 65 28 70 50 61 67 65  pagerLeave(pPage
fae90 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  r);.  return SQL
faea0 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66  ITE_OK;.}.#endif
faeb0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
faec0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
faed0 64 61 74 61 20 66 6f 72 20 74 68 65 20 73 70 65  data for the spe
faee0 63 69 66 69 65 64 20 70 61 67 65 2e 0a 2a 2f 0a  cified page..*/.
faef0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
faf00 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65  oid *sqlite3Page
faf10 72 47 65 74 44 61 74 61 28 44 62 50 61 67 65 20  rGetData(DbPage 
faf20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 72 6e 20  *pPg){.  return 
faf30 50 47 48 44 52 5f 54 4f 5f 44 41 54 41 28 70 50  PGHDR_TO_DATA(pP
faf40 67 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  g);.}../*.** Ret
faf50 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
faf60 20 74 68 65 20 50 61 67 65 72 2e 6e 45 78 74 72   the Pager.nExtr
faf70 61 20 62 79 74 65 73 20 6f 66 20 22 65 78 74 72  a bytes of "extr
faf80 61 22 20 73 70 61 63 65 20 0a 2a 2a 20 61 6c 6c  a" space .** all
faf90 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74  ocated along wit
fafa0 68 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  h the specified 
fafb0 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  page..*/.SQLITE_
fafc0 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
fafd0 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74  lite3PagerGetExt
fafe0 72 61 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b  ra(DbPage *pPg){
faff0 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
fb000 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a   = pPg->pPager;.
fb010 20 20 72 65 74 75 72 6e 20 28 70 50 61 67 65 72    return (pPager
fb020 3f 50 47 48 44 52 5f 54 4f 5f 45 58 54 52 41 28  ?PGHDR_TO_EXTRA(
fb030 70 50 67 2c 20 70 50 61 67 65 72 29 3a 30 29 3b  pPg, pPager):0);
fb040 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65  .}../*.** Get/se
fb050 74 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f  t the locking-mo
fb060 64 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65  de for this page
fb070 72 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d 6f  r. Parameter eMo
fb080 64 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 0a 2a  de must be one.*
fb090 2a 20 6f 66 20 50 41 47 45 52 5f 4c 4f 43 4b 49  * of PAGER_LOCKI
fb0a0 4e 47 4d 4f 44 45 5f 51 55 45 52 59 2c 20 50 41  NGMODE_QUERY, PA
fb0b0 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
fb0c0 4e 4f 52 4d 41 4c 20 6f 72 20 0a 2a 2a 20 50 41  NORMAL or .** PA
fb0d0 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
fb0e0 45 58 43 4c 55 53 49 56 45 2e 20 49 66 20 74 68  EXCLUSIVE. If th
fb0f0 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e  e parameter is n
fb100 6f 74 20 5f 51 55 45 52 59 2c 20 74 68 65 6e 0a  ot _QUERY, then.
fb110 2a 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d  ** the locking-m
fb120 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 74 68  ode is set to th
fb130 65 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 65  e value specifie
fb140 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74  d..**.** The ret
fb150 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 65  urned value is e
fb160 69 74 68 65 72 20 50 41 47 45 52 5f 4c 4f 43 4b  ither PAGER_LOCK
fb170 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 6f  INGMODE_NORMAL o
fb180 72 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b 49  r.** PAGER_LOCKI
fb190 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45  NGMODE_EXCLUSIVE
fb1a0 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65  , indicating the
fb1b0 20 63 75 72 72 65 6e 74 20 28 70 6f 73 73 69 62   current (possib
fb1c0 6c 79 20 75 70 64 61 74 65 64 29 0a 2a 2a 20 6c  ly updated).** l
fb1d0 6f 63 6b 69 6e 67 2d 6d 6f 64 65 2e 0a 2a 2f 0a  ocking-mode..*/.
fb1e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
fb1f0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c  nt sqlite3PagerL
fb200 6f 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67 65 72  ockingMode(Pager
fb210 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 4d   *pPager, int eM
fb220 6f 64 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20  ode){.  assert( 
fb230 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43  eMode==PAGER_LOC
fb240 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 0a 20  KINGMODE_QUERY. 
fb250 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d             || eM
fb260 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49  ode==PAGER_LOCKI
fb270 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 0a 20 20  NGMODE_NORMAL.  
fb280 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f            || eMo
fb290 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  de==PAGER_LOCKIN
fb2a0 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20  GMODE_EXCLUSIVE 
fb2b0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47  );.  assert( PAG
fb2c0 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51  ER_LOCKINGMODE_Q
fb2d0 55 45 52 59 3c 30 20 29 3b 0a 20 20 61 73 73 65  UERY<0 );.  asse
fb2e0 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  rt( PAGER_LOCKIN
fb2f0 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3e 3d 30 20  GMODE_NORMAL>=0 
fb300 26 26 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  && PAGER_LOCKING
fb310 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3e 3d  MODE_EXCLUSIVE>=
fb320 30 20 29 3b 0a 20 20 69 66 28 20 65 4d 6f 64 65  0 );.  if( eMode
fb330 3e 3d 30 20 26 26 20 21 70 50 61 67 65 72 2d 3e  >=0 && !pPager->
fb340 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20  tempFile ){.    
fb350 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
fb360 65 4d 6f 64 65 20 3d 20 65 4d 6f 64 65 3b 0a 20  eMode = eMode;. 
fb370 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74   }.  return (int
fb380 29 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69  )pPager->exclusi
fb390 76 65 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  veMode;.}../*.**
fb3a0 20 47 65 74 2f 73 65 74 20 74 68 65 20 6a 6f 75   Get/set the jou
fb3b0 72 6e 61 6c 2d 6d 6f 64 65 20 66 6f 72 20 74 68  rnal-mode for th
fb3c0 69 73 20 70 61 67 65 72 2e 20 50 61 72 61 6d 65  is pager. Parame
fb3d0 74 65 72 20 65 4d 6f 64 65 20 6d 75 73 74 20 62  ter eMode must b
fb3e0 65 20 6f 6e 65 0a 2a 2a 20 6f 66 20 50 41 47 45  e one.** of PAGE
fb3f0 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55  R_JOURNALMODE_QU
fb400 45 52 59 2c 20 50 41 47 45 52 5f 4a 4f 55 52 4e  ERY, PAGER_JOURN
fb410 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 20 6f 72  ALMODE_DELETE or
fb420 20 0a 2a 2a 20 50 41 47 45 52 5f 4a 4f 55 52 4e   .** PAGER_JOURN
fb430 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 2e 20  ALMODE_PERSIST. 
fb440 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  If the parameter
fb450 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c 20   is not _QUERY, 
fb460 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6a 6f 75 72  then.** the jour
fb470 6e 61 6c 2d 6d 6f 64 65 20 69 73 20 73 65 74 20  nal-mode is set 
fb480 74 6f 20 74 68 65 20 76 61 6c 75 65 20 73 70 65  to the value spe
fb490 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  cified..**.** Th
fb4a0 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65  e returned value
fb4b0 20 69 73 20 65 69 74 68 65 72 20 50 41 47 45 52   is either PAGER
fb4c0 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c  _JOURNALMODE_DEL
fb4d0 45 54 45 20 6f 72 0a 2a 2a 20 50 41 47 45 52 5f  ETE or.** PAGER_
fb4e0 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53  JOURNALMODE_PERS
fb4f0 49 53 54 2c 20 69 6e 64 69 63 61 74 69 6e 67 20  IST, indicating 
fb500 74 68 65 20 63 75 72 72 65 6e 74 20 28 70 6f 73  the current (pos
fb510 73 69 62 6c 79 20 75 70 64 61 74 65 64 29 0a 2a  sibly updated).*
fb520 2a 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 2e 0a  * journal-mode..
fb530 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
fb540 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
fb550 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 61  erJournalMode(Pa
fb560 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
fb570 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65 72   eMode){.  asser
fb580 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  t( eMode==PAGER_
fb590 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52  JOURNALMODE_QUER
fb5a0 59 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c  Y.            ||
fb5b0 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f   eMode==PAGER_JO
fb5c0 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45  URNALMODE_DELETE
fb5d0 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  .            || 
fb5e0 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  eMode==PAGER_JOU
fb5f0 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54  RNALMODE_PERSIST
fb600 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  .            || 
fb610 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  eMode==PAGER_JOU
fb620 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a  RNALMODE_OFF );.
fb630 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f    assert( PAGER_
fb640 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52  JOURNALMODE_QUER
fb650 59 3c 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  Y<0 );.  assert(
fb660 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f   PAGER_JOURNALMO
fb670 44 45 5f 44 45 4c 45 54 45 3e 3d 30 20 26 26 20  DE_DELETE>=0 && 
fb680 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
fb690 45 5f 50 45 52 53 49 53 54 3e 3d 30 20 29 3b 0a  E_PERSIST>=0 );.
fb6a0 20 20 69 66 28 20 65 4d 6f 64 65 3e 3d 30 20 29    if( eMode>=0 )
fb6b0 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f  {.    pPager->jo
fb6c0 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 65 4d 6f 64  urnalMode = eMod
fb6d0 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  e;.  }.  return 
fb6e0 28 69 6e 74 29 70 50 61 67 65 72 2d 3e 6a 6f 75  (int)pPager->jou
fb6f0 72 6e 61 6c 4d 6f 64 65 3b 0a 7d 0a 0a 23 69 66  rnalMode;.}..#if
fb700 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
fb710 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 61 20 6c 69  /*.** Print a li
fb720 73 74 69 6e 67 20 6f 66 20 61 6c 6c 20 72 65 66  sting of all ref
fb730 65 72 65 6e 63 65 64 20 70 61 67 65 73 20 61 6e  erenced pages an
fb740 64 20 74 68 65 69 72 20 72 65 66 20 63 6f 75 6e  d their ref coun
fb750 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
fb760 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
fb770 33 50 61 67 65 72 52 65 66 64 75 6d 70 28 50 61  3PagerRefdump(Pa
fb780 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
fb790 50 67 48 64 72 20 2a 70 50 67 3b 0a 20 20 66 6f  PgHdr *pPg;.  fo
fb7a0 72 28 70 50 67 3d 70 50 61 67 65 72 2d 3e 70 41  r(pPg=pPager->pA
fb7b0 6c 6c 3b 20 70 50 67 3b 20 70 50 67 3d 70 50 67  ll; pPg; pPg=pPg
fb7c0 2d 3e 70 4e 65 78 74 41 6c 6c 29 7b 0a 20 20 20  ->pNextAll){.   
fb7d0 20 69 66 28 20 70 50 67 2d 3e 6e 52 65 66 3c 3d   if( pPg->nRef<=
fb7e0 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
fb7f0 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
fb800 69 6e 74 66 28 22 50 41 47 45 20 25 33 64 20 61  intf("PAGE %3d a
fb810 64 64 72 3d 25 70 20 6e 52 65 66 3d 25 64 5c 6e  ddr=%p nRef=%d\n
fb820 22 2c 20 0a 20 20 20 20 20 20 20 70 50 67 2d 3e  ", .       pPg->
fb830 70 67 6e 6f 2c 20 50 47 48 44 52 5f 54 4f 5f 44  pgno, PGHDR_TO_D
fb840 41 54 41 28 70 50 67 29 2c 20 70 50 67 2d 3e 6e  ATA(pPg), pPg->n
fb850 52 65 66 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64  Ref);.  }.}.#end
fb860 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  if..#endif /* SQ
fb870 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f  LITE_OMIT_DISKIO
fb880 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
fb890 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67 65  **** End of page
fb8a0 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  r.c ************
fb8b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fb8c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fb8d0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
fb8e0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
fb8f0 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a  btmutex.c ******
fb900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fb910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fb920 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20  ***/./*.** 2007 
fb930 41 75 67 75 73 74 20 32 37 0a 2a 2a 0a 2a 2a 20  August 27.**.** 
fb940 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
fb950 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
fb960 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
fb970 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
fb980 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
fb990 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
fb9a0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
fb9b0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
fb9c0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
fb9d0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
fb9e0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
fb9f0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
fba00 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
fba10 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
fba20 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
fba30 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
fba40 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
fba50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fba60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fba70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fba80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fba90 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 24  ********.**.** $
fbaa0 49 64 3a 20 62 74 6d 75 74 65 78 2e 63 2c 76 20  Id: btmutex.c,v 
fbab0 31 2e 39 20 32 30 30 38 2f 30 31 2f 32 33 20 31  1.9 2008/01/23 1
fbac0 32 3a 35 32 3a 34 31 20 64 72 68 20 45 78 70 20  2:52:41 drh Exp 
fbad0 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  $.**.** This fil
fbae0 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
fbaf0 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
fbb00 74 20 6d 75 74 65 78 65 73 20 6f 6e 20 42 74 72  t mutexes on Btr
fbb10 65 65 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 54  ee objects..** T
fbb20 68 69 73 20 63 6f 64 65 20 72 65 61 6c 6c 79 20  his code really 
fbb30 62 65 6c 6f 6e 67 73 20 69 6e 20 62 74 72 65 65  belongs in btree
fbb40 2e 63 2e 20 20 42 75 74 20 62 74 72 65 65 2e 63  .c.  But btree.c
fbb50 20 69 73 20 67 65 74 74 69 6e 67 20 74 6f 6f 0a   is getting too.
fbb60 2a 2a 20 62 69 67 20 61 6e 64 20 77 65 20 77 61  ** big and we wa
fbb70 6e 74 20 74 6f 20 62 72 65 61 6b 20 69 74 20 64  nt to break it d
fbb80 6f 77 6e 20 73 6f 6d 65 2e 20 20 54 68 69 73 20  own some.  This 
fbb90 70 61 63 6b 61 67 65 64 20 73 65 65 6d 65 64 20  packaged seemed 
fbba0 6c 69 6b 65 0a 2a 2a 20 61 20 67 6f 6f 64 20 62  like.** a good b
fbbb0 72 65 61 6b 6f 75 74 2e 0a 2a 2f 0a 2f 2a 2a 2a  reakout..*/./***
fbbc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
fbbd0 75 64 65 20 62 74 72 65 65 49 6e 74 2e 68 20 69  ude btreeInt.h i
fbbe0 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
fbbf0 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a  btmutex.c ******
fbc00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
fbc10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
fbc20 6e 20 66 69 6c 65 20 62 74 72 65 65 49 6e 74 2e  n file btreeInt.
fbc30 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
fbc40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fbc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
fbc60 2a 20 32 30 30 34 20 41 70 72 69 6c 20 36 0a 2a  * 2004 April 6.*
fbc70 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
fbc80 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
fbc90 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
fbca0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
fbcb0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
fbcc0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
fbcd0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
fbce0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
fbcf0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
fbd00 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
fbd10 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
fbd20 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
fbd30 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
fbd40 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
fbd50 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
fbd60 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
fbd70 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
fbd80 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
fbd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fbda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fbdb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fbdc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
fbdd0 20 24 49 64 3a 20 62 74 72 65 65 49 6e 74 2e 68   $Id: btreeInt.h
fbde0 2c 76 20 31 2e 32 31 20 32 30 30 38 2f 30 34 2f  ,v 1.21 2008/04/
fbdf0 32 34 20 31 39 3a 31 35 3a 31 30 20 73 68 61 6e  24 19:15:10 shan
fbe00 65 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68  e Exp $.**.** Th
fbe10 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e  is file implemen
fbe20 74 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28 64  ts a external (d
fbe30 69 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61 62  isk-based) datab
fbe40 61 73 65 20 75 73 69 6e 67 20 42 54 72 65 65 73  ase using BTrees
fbe50 2e 0a 2a 2a 20 46 6f 72 20 61 20 64 65 74 61 69  ..** For a detai
fbe60 6c 65 64 20 64 69 73 63 75 73 73 69 6f 6e 20 6f  led discussion o
fbe70 66 20 42 54 72 65 65 73 2c 20 72 65 66 65 72 20  f BTrees, refer 
fbe80 74 6f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 6f 6e  to.**.**     Don
fbe90 61 6c 64 20 45 2e 20 4b 6e 75 74 68 2c 20 54 48  ald E. Knuth, TH
fbea0 45 20 41 52 54 20 4f 46 20 43 4f 4d 50 55 54 45  E ART OF COMPUTE
fbeb0 52 20 50 52 4f 47 52 41 4d 4d 49 4e 47 2c 20 56  R PROGRAMMING, V
fbec0 6f 6c 75 6d 65 20 33 3a 0a 2a 2a 20 20 20 20 20  olume 3:.**     
fbed0 22 53 6f 72 74 69 6e 67 20 41 6e 64 20 53 65 61  "Sorting And Sea
fbee0 72 63 68 69 6e 67 22 2c 20 70 61 67 65 73 20 34  rching", pages 4
fbef0 37 33 2d 34 38 30 2e 20 41 64 64 69 73 6f 6e 2d  73-480. Addison-
fbf00 57 65 73 6c 65 79 0a 2a 2a 20 20 20 20 20 50 75  Wesley.**     Pu
fbf10 62 6c 69 73 68 69 6e 67 20 43 6f 6d 70 61 6e 79  blishing Company
fbf20 2c 20 52 65 61 64 69 6e 67 2c 20 4d 61 73 73 61  , Reading, Massa
fbf30 63 68 75 73 65 74 74 73 2e 0a 2a 2a 0a 2a 2a 20  chusetts..**.** 
fbf40 54 68 65 20 62 61 73 69 63 20 69 64 65 61 20 69  The basic idea i
fbf50 73 20 74 68 61 74 20 65 61 63 68 20 70 61 67 65  s that each page
fbf60 20 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f 6e   of the file con
fbf70 74 61 69 6e 73 20 4e 20 64 61 74 61 62 61 73 65  tains N database
fbf80 0a 2a 2a 20 65 6e 74 72 69 65 73 20 61 6e 64 20  .** entries and 
fbf90 4e 2b 31 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  N+1 pointers to 
fbfa0 73 75 62 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20  subpages..**.** 
fbfb0 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
fbfc0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
fbfd0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
fbfe0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
fbff0 2d 2d 0a 2a 2a 20 20 20 7c 20 20 50 74 72 28 30  --.**   |  Ptr(0
fc000 29 20 7c 20 4b 65 79 28 30 29 20 7c 20 50 74 72  ) | Key(0) | Ptr
fc010 28 31 29 20 7c 20 4b 65 79 28 31 29 20 7c 20 2e  (1) | Key(1) | .
fc020 2e 2e 20 7c 20 4b 65 79 28 4e 2d 31 29 20 7c 20  .. | Key(N-1) | 
fc030 50 74 72 28 4e 29 20 7c 0a 2a 2a 20 20 20 2d 2d  Ptr(N) |.**   --
fc040 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
fc050 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
fc060 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
fc070 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a  --------------.*
fc080 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20  *.** All of the 
fc090 6b 65 79 73 20 6f 6e 20 74 68 65 20 70 61 67 65  keys on the page
fc0a0 20 74 68 61 74 20 50 74 72 28 30 29 20 70 6f 69   that Ptr(0) poi
fc0b0 6e 74 73 20 74 6f 20 68 61 76 65 20 76 61 6c 75  nts to have valu
fc0c0 65 73 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20  es less.** than 
fc0d0 4b 65 79 28 30 29 2e 20 20 41 6c 6c 20 6f 66 20  Key(0).  All of 
fc0e0 74 68 65 20 6b 65 79 73 20 6f 6e 20 70 61 67 65  the keys on page
fc0f0 20 50 74 72 28 31 29 20 61 6e 64 20 69 74 73 20   Ptr(1) and its 
fc100 73 75 62 70 61 67 65 73 20 68 61 76 65 0a 2a 2a  subpages have.**
fc110 20 76 61 6c 75 65 73 20 67 72 65 61 74 65 72 20   values greater 
fc120 74 68 61 6e 20 4b 65 79 28 30 29 20 61 6e 64 20  than Key(0) and 
fc130 6c 65 73 73 20 74 68 61 6e 20 4b 65 79 28 31 29  less than Key(1)
fc140 2e 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65  .  All of the ke
fc150 79 73 0a 2a 2a 20 6f 6e 20 50 74 72 28 4e 29 20  ys.** on Ptr(N) 
fc160 61 6e 64 20 69 74 73 20 73 75 62 70 61 67 65 73  and its subpages
fc170 20 68 61 76 65 20 76 61 6c 75 65 73 20 67 72 65   have values gre
fc180 61 74 65 72 20 74 68 61 6e 20 4b 65 79 28 4e 2d  ater than Key(N-
fc190 31 29 2e 20 20 41 6e 64 0a 2a 2a 20 73 6f 20 66  1).  And.** so f
fc1a0 6f 72 74 68 2e 0a 2a 2a 0a 2a 2a 20 46 69 6e 64  orth..**.** Find
fc1b0 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61 72  ing a particular
fc1c0 20 6b 65 79 20 72 65 71 75 69 72 65 73 20 72 65   key requires re
fc1d0 61 64 69 6e 67 20 4f 28 6c 6f 67 28 4d 29 29 20  ading O(log(M)) 
fc1e0 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 0a  pages from the .
fc1f0 2a 2a 20 64 69 73 6b 20 77 68 65 72 65 20 4d 20  ** disk where M 
fc200 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
fc210 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20   entries in the 
fc220 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74  tree..**.** In t
fc230 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  his implementati
fc240 6f 6e 2c 20 61 20 73 69 6e 67 6c 65 20 66 69 6c  on, a single fil
fc250 65 20 63 61 6e 20 68 6f 6c 64 20 6f 6e 65 20 6f  e can hold one o
fc260 72 20 6d 6f 72 65 20 73 65 70 61 72 61 74 65 20  r more separate 
fc270 0a 2a 2a 20 42 54 72 65 65 73 2e 20 20 45 61 63  .** BTrees.  Eac
fc280 68 20 42 54 72 65 65 20 69 73 20 69 64 65 6e 74  h BTree is ident
fc290 69 66 69 65 64 20 62 79 20 74 68 65 20 69 6e 64  ified by the ind
fc2a0 65 78 20 6f 66 20 69 74 73 20 72 6f 6f 74 20 70  ex of its root p
fc2b0 61 67 65 2e 20 20 54 68 65 0a 2a 2a 20 6b 65 79  age.  The.** key
fc2c0 20 61 6e 64 20 64 61 74 61 20 66 6f 72 20 61 6e   and data for an
fc2d0 79 20 65 6e 74 72 79 20 61 72 65 20 63 6f 6d 62  y entry are comb
fc2e0 69 6e 65 64 20 74 6f 20 66 6f 72 6d 20 74 68 65  ined to form the
fc2f0 20 22 70 61 79 6c 6f 61 64 22 2e 20 20 41 0a 2a   "payload".  A.*
fc300 2a 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20 6f  * fixed amount o
fc310 66 20 70 61 79 6c 6f 61 64 20 63 61 6e 20 62 65  f payload can be
fc320 20 63 61 72 72 69 65 64 20 64 69 72 65 63 74 6c   carried directl
fc330 79 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  y on the databas
fc340 65 0a 2a 2a 20 70 61 67 65 2e 20 20 49 66 20 74  e.** page.  If t
fc350 68 65 20 70 61 79 6c 6f 61 64 20 69 73 20 6c 61  he payload is la
fc360 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 70 72  rger than the pr
fc370 65 73 65 74 20 61 6d 6f 75 6e 74 20 74 68 65 6e  eset amount then
fc380 20 73 75 72 70 6c 75 73 0a 2a 2a 20 62 79 74 65   surplus.** byte
fc390 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20  s are stored on 
fc3a0 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20  overflow pages. 
fc3b0 20 54 68 65 20 70 61 79 6c 6f 61 64 20 66 6f 72   The payload for
fc3c0 20 61 6e 20 65 6e 74 72 79 0a 2a 2a 20 61 6e 64   an entry.** and
fc3d0 20 74 68 65 20 70 72 65 63 65 64 69 6e 67 20 70   the preceding p
fc3e0 6f 69 6e 74 65 72 20 61 72 65 20 63 6f 6d 62 69  ointer are combi
fc3f0 6e 65 64 20 74 6f 20 66 6f 72 6d 20 61 20 22 43  ned to form a "C
fc400 65 6c 6c 22 2e 20 20 45 61 63 68 20 0a 2a 2a 20  ell".  Each .** 
fc410 70 61 67 65 20 68 61 73 20 61 20 73 6d 61 6c 6c  page has a small
fc420 20 68 65 61 64 65 72 20 77 68 69 63 68 20 63 6f   header which co
fc430 6e 74 61 69 6e 73 20 74 68 65 20 50 74 72 28 4e  ntains the Ptr(N
fc440 29 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 6f 74  ) pointer and ot
fc450 68 65 72 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  her.** informati
fc460 6f 6e 20 73 75 63 68 20 61 73 20 74 68 65 20 73  on such as the s
fc470 69 7a 65 20 6f 66 20 6b 65 79 20 61 6e 64 20 64  ize of key and d
fc480 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 46 4f 52 4d 41  ata..**.** FORMA
fc490 54 20 44 45 54 41 49 4c 53 0a 2a 2a 0a 2a 2a 20  T DETAILS.**.** 
fc4a0 54 68 65 20 66 69 6c 65 20 69 73 20 64 69 76 69  The file is divi
fc4b0 64 65 64 20 69 6e 74 6f 20 70 61 67 65 73 2e 20  ded into pages. 
fc4c0 20 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20   The first page 
fc4d0 69 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20 31  is called page 1
fc4e0 2c 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20  ,.** the second 
fc4f0 69 73 20 70 61 67 65 20 32 2c 20 61 6e 64 20 73  is page 2, and s
fc500 6f 20 66 6f 72 74 68 2e 20 20 41 20 70 61 67 65  o forth.  A page
fc510 20 6e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 20   number of zero 
fc520 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 22 6e 6f  indicates.** "no
fc530 20 73 75 63 68 20 70 61 67 65 22 2e 20 20 54 68   such page".  Th
fc540 65 20 70 61 67 65 20 73 69 7a 65 20 63 61 6e 20  e page size can 
fc550 62 65 20 61 6e 79 74 68 69 6e 67 20 62 65 74 77  be anything betw
fc560 65 65 6e 20 35 31 32 20 61 6e 64 20 36 35 35 33  een 512 and 6553
fc570 36 2e 0a 2a 2a 20 45 61 63 68 20 70 61 67 65 20  6..** Each page 
fc580 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 20  can be either a 
fc590 62 74 72 65 65 20 70 61 67 65 2c 20 61 20 66 72  btree page, a fr
fc5a0 65 65 6c 69 73 74 20 70 61 67 65 20 6f 72 20 61  eelist page or a
fc5b0 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70 61  n overflow.** pa
fc5c0 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  ge..**.** The fi
fc5d0 72 73 74 20 70 61 67 65 20 69 73 20 61 6c 77 61  rst page is alwa
fc5e0 79 73 20 61 20 62 74 72 65 65 20 70 61 67 65 2e  ys a btree page.
fc5f0 20 20 54 68 65 20 66 69 72 73 74 20 31 30 30 20    The first 100 
fc600 62 79 74 65 73 20 6f 66 20 74 68 65 20 66 69 72  bytes of the fir
fc610 73 74 0a 2a 2a 20 70 61 67 65 20 63 6f 6e 74 61  st.** page conta
fc620 69 6e 20 61 20 73 70 65 63 69 61 6c 20 68 65 61  in a special hea
fc630 64 65 72 20 28 74 68 65 20 22 66 69 6c 65 20 68  der (the "file h
fc640 65 61 64 65 72 22 29 20 74 68 61 74 20 64 65 73  eader") that des
fc650 63 72 69 62 65 73 20 74 68 65 20 66 69 6c 65 2e  cribes the file.
fc660 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 6f  .** The format o
fc670 66 20 74 68 65 20 66 69 6c 65 20 68 65 61 64 65  f the file heade
fc680 72 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  r is as follows:
fc690 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 45 54 20  .**.**   OFFSET 
fc6a0 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 49    SIZE    DESCRI
fc6b0 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 20  PTION.**      0 
fc6c0 20 20 20 20 20 31 36 20 20 20 20 20 48 65 61 64       16     Head
fc6d0 65 72 20 73 74 72 69 6e 67 3a 20 22 53 51 4c 69  er string: "SQLi
fc6e0 74 65 20 66 6f 72 6d 61 74 20 33 5c 30 30 30 22  te format 3\000"
fc6f0 0a 2a 2a 20 20 20 20 20 31 36 20 20 20 20 20 20  .**     16      
fc700 20 32 20 20 20 20 20 50 61 67 65 20 73 69 7a 65   2     Page size
fc710 20 69 6e 20 62 79 74 65 73 2e 20 20 0a 2a 2a 20   in bytes.  .** 
fc720 20 20 20 20 31 38 20 20 20 20 20 20 20 31 20 20      18       1  
fc730 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 77     File format w
fc740 72 69 74 65 20 76 65 72 73 69 6f 6e 0a 2a 2a 20  rite version.** 
fc750 20 20 20 20 31 39 20 20 20 20 20 20 20 31 20 20      19       1  
fc760 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 72     File format r
fc770 65 61 64 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 20  ead version.**  
fc780 20 20 20 32 30 20 20 20 20 20 20 20 31 20 20 20     20       1   
fc790 20 20 42 79 74 65 73 20 6f 66 20 75 6e 75 73 65    Bytes of unuse
fc7a0 64 20 73 70 61 63 65 20 61 74 20 74 68 65 20 65  d space at the e
fc7b0 6e 64 20 6f 66 20 65 61 63 68 20 70 61 67 65 0a  nd of each page.
fc7c0 2a 2a 20 20 20 20 20 32 31 20 20 20 20 20 20 20  **     21       
fc7d0 31 20 20 20 20 20 4d 61 78 20 65 6d 62 65 64 64  1     Max embedd
fc7e0 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74  ed payload fract
fc7f0 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 32 20 20 20  ion.**     22   
fc800 20 20 20 20 31 20 20 20 20 20 4d 69 6e 20 65 6d      1     Min em
fc810 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66  bedded payload f
fc820 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32  raction.**     2
fc830 33 20 20 20 20 20 20 20 31 20 20 20 20 20 4d 69  3       1     Mi
fc840 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20 66  n leaf payload f
fc850 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32  raction.**     2
fc860 34 20 20 20 20 20 20 20 34 20 20 20 20 20 46 69  4       4     Fi
fc870 6c 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65  le change counte
fc880 72 0a 2a 2a 20 20 20 20 20 32 38 20 20 20 20 20  r.**     28     
fc890 20 20 34 20 20 20 20 20 52 65 73 65 72 76 65 64    4     Reserved
fc8a0 20 66 6f 72 20 66 75 74 75 72 65 20 75 73 65 0a   for future use.
fc8b0 2a 2a 20 20 20 20 20 33 32 20 20 20 20 20 20 20  **     32       
fc8c0 34 20 20 20 20 20 46 69 72 73 74 20 66 72 65 65  4     First free
fc8d0 6c 69 73 74 20 70 61 67 65 0a 2a 2a 20 20 20 20  list page.**    
fc8e0 20 33 36 20 20 20 20 20 20 20 34 20 20 20 20 20   36       4     
fc8f0 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c 69  Number of freeli
fc900 73 74 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  st pages in the 
fc910 66 69 6c 65 0a 2a 2a 20 20 20 20 20 34 30 20 20  file.**     40  
fc920 20 20 20 20 36 30 20 20 20 20 20 31 35 20 34 2d      60     15 4-
fc930 62 79 74 65 20 6d 65 74 61 20 76 61 6c 75 65 73  byte meta values
fc940 20 70 61 73 73 65 64 20 74 6f 20 68 69 67 68 65   passed to highe
fc950 72 20 6c 61 79 65 72 73 0a 2a 2a 0a 2a 2a 20 41  r layers.**.** A
fc960 6c 6c 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65  ll of the intege
fc970 72 20 76 61 6c 75 65 73 20 61 72 65 20 62 69 67  r values are big
fc980 2d 65 6e 64 69 61 6e 20 28 6d 6f 73 74 20 73 69  -endian (most si
fc990 67 6e 69 66 69 63 61 6e 74 20 62 79 74 65 20 66  gnificant byte f
fc9a0 69 72 73 74 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  irst)..**.** The
fc9b0 20 66 69 6c 65 20 63 68 61 6e 67 65 20 63 6f 75   file change cou
fc9c0 6e 74 65 72 20 69 73 20 69 6e 63 72 65 6d 65 6e  nter is incremen
fc9d0 74 65 64 20 77 68 65 6e 20 74 68 65 20 64 61 74  ted when the dat
fc9e0 61 62 61 73 65 20 69 73 20 63 68 61 6e 67 65 64  abase is changed
fc9f0 0a 2a 2a 20 54 68 69 73 20 63 6f 75 6e 74 65 72  .** This counter
fca00 20 61 6c 6c 6f 77 73 20 6f 74 68 65 72 20 70 72   allows other pr
fca10 6f 63 65 73 73 65 73 20 74 6f 20 6b 6e 6f 77 20  ocesses to know 
fca20 77 68 65 6e 20 74 68 65 20 66 69 6c 65 20 68 61  when the file ha
fca30 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 61 6e 64  s changed.** and
fca40 20 74 68 75 73 20 77 68 65 6e 20 74 68 65 79 20   thus when they 
fca50 6e 65 65 64 20 74 6f 20 66 6c 75 73 68 20 74 68  need to flush th
fca60 65 69 72 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a  eir cache..**.**
fca70 20 54 68 65 20 6d 61 78 20 65 6d 62 65 64 64 65   The max embedde
fca80 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69  d payload fracti
fca90 6f 6e 20 69 73 20 74 68 65 20 61 6d 6f 75 6e 74  on is the amount
fcaa0 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 20 75 73   of the total us
fcab0 61 62 6c 65 0a 2a 2a 20 73 70 61 63 65 20 69 6e  able.** space in
fcac0 20 61 20 70 61 67 65 20 74 68 61 74 20 63 61 6e   a page that can
fcad0 20 62 65 20 63 6f 6e 73 75 6d 65 64 20 62 79 20   be consumed by 
fcae0 61 20 73 69 6e 67 6c 65 20 63 65 6c 6c 20 66 6f  a single cell fo
fcaf0 72 20 73 74 61 6e 64 61 72 64 0a 2a 2a 20 42 2d  r standard.** B-
fcb00 74 72 65 65 20 28 6e 6f 6e 2d 4c 45 41 46 44 41  tree (non-LEAFDA
fcb10 54 41 29 20 74 61 62 6c 65 73 2e 20 20 41 20 76  TA) tables.  A v
fcb20 61 6c 75 65 20 6f 66 20 32 35 35 20 6d 65 61 6e  alue of 255 mean
fcb30 73 20 31 30 30 25 2e 20 20 54 68 65 20 64 65 66  s 100%.  The def
fcb40 61 75 6c 74 0a 2a 2a 20 69 73 20 74 6f 20 6c 69  ault.** is to li
fcb50 6d 69 74 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  mit the maximum 
fcb60 63 65 6c 6c 20 73 69 7a 65 20 73 6f 20 74 68 61  cell size so tha
fcb70 74 20 61 74 20 6c 65 61 73 74 20 34 20 63 65 6c  t at least 4 cel
fcb80 6c 73 20 77 69 6c 6c 20 66 69 74 0a 2a 2a 20 6f  ls will fit.** o
fcb90 6e 20 6f 6e 65 20 70 61 67 65 2e 20 20 54 68 75  n one page.  Thu
fcba0 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 6d 61  s the default ma
fcbb0 78 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f  x embedded paylo
fcbc0 61 64 20 66 72 61 63 74 69 6f 6e 20 69 73 20 36  ad fraction is 6
fcbd0 34 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  4..**.** If the 
fcbe0 70 61 79 6c 6f 61 64 20 66 6f 72 20 61 20 63 65  payload for a ce
fcbf0 6c 6c 20 69 73 20 6c 61 72 67 65 72 20 74 68 61  ll is larger tha
fcc00 6e 20 74 68 65 20 6d 61 78 20 70 61 79 6c 6f 61  n the max payloa
fcc10 64 2c 20 74 68 65 6e 20 65 78 74 72 61 0a 2a 2a  d, then extra.**
fcc20 20 70 61 79 6c 6f 61 64 20 69 73 20 73 70 69 6c   payload is spil
fcc30 6c 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20  led to overflow 
fcc40 70 61 67 65 73 2e 20 20 4f 6e 63 65 20 61 6e 20  pages.  Once an 
fcc50 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 73  overflow page is
fcc60 20 61 6c 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20 61   allocated,.** a
fcc70 73 20 6d 61 6e 79 20 62 79 74 65 73 20 61 73 20  s many bytes as 
fcc80 70 6f 73 73 69 62 6c 65 20 61 72 65 20 6d 6f 76  possible are mov
fcc90 65 64 20 69 6e 74 6f 20 74 68 65 20 6f 76 65 72  ed into the over
fcca0 66 6c 6f 77 20 70 61 67 65 73 20 77 69 74 68 6f  flow pages witho
fccb0 75 74 20 6c 65 74 74 69 6e 67 0a 2a 2a 20 74 68  ut letting.** th
fccc0 65 20 63 65 6c 6c 20 73 69 7a 65 20 64 72 6f 70  e cell size drop
fccd0 20 62 65 6c 6f 77 20 74 68 65 20 6d 69 6e 20 65   below the min e
fcce0 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20  mbedded payload 
fccf0 66 72 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  fraction..**.** 
fcd00 54 68 65 20 6d 69 6e 20 6c 65 61 66 20 70 61 79  The min leaf pay
fcd10 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69 73  load fraction is
fcd20 20 6c 69 6b 65 20 74 68 65 20 6d 69 6e 20 65 6d   like the min em
fcd30 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66  bedded payload f
fcd40 72 61 63 74 69 6f 6e 0a 2a 2a 20 65 78 63 65 70  raction.** excep
fcd50 74 20 74 68 61 74 20 69 74 20 61 70 70 6c 69 65  t that it applie
fcd60 73 20 74 6f 20 6c 65 61 66 20 6e 6f 64 65 73 20  s to leaf nodes 
fcd70 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74 72  in a LEAFDATA tr
fcd80 65 65 2e 20 20 54 68 65 20 6d 61 78 69 6d 75 6d  ee.  The maximum
fcd90 0a 2a 2a 20 70 61 79 6c 6f 61 64 20 66 72 61 63  .** payload frac
fcda0 74 69 6f 6e 20 66 6f 72 20 61 20 4c 45 41 46 44  tion for a LEAFD
fcdb0 41 54 41 20 74 72 65 65 20 69 73 20 61 6c 77 61  ATA tree is alwa
fcdc0 79 73 20 31 30 30 25 20 28 6f 72 20 32 35 35 29  ys 100% (or 255)
fcdd0 20 61 6e 64 20 69 74 0a 2a 2a 20 6e 6f 74 20 73   and it.** not s
fcde0 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20  pecified in the 
fcdf0 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 45 61  header..**.** Ea
fce00 63 68 20 62 74 72 65 65 20 70 61 67 65 73 20 69  ch btree pages i
fce10 73 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20 74  s divided into t
fce20 68 72 65 65 20 73 65 63 74 69 6f 6e 73 3a 20 20  hree sections:  
fce30 54 68 65 20 68 65 61 64 65 72 2c 20 74 68 65 0a  The header, the.
fce40 2a 2a 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20  ** cell pointer 
fce50 61 72 72 61 79 2c 20 61 6e 64 20 74 68 65 20 63  array, and the c
fce60 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61  ell content area
fce70 2e 20 20 50 61 67 65 20 31 20 61 6c 73 6f 20 68  .  Page 1 also h
fce80 61 73 20 61 20 31 30 30 2d 62 79 74 65 0a 2a 2a  as a 100-byte.**
fce90 20 66 69 6c 65 20 68 65 61 64 65 72 20 74 68 61   file header tha
fcea0 74 20 6f 63 63 75 72 73 20 62 65 66 6f 72 65 20  t occurs before 
fceb0 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 2e  the page header.
fcec0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d  .**.**      |---
fced0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a  -------------|.*
fcee0 2a 20 20 20 20 20 20 7c 20 66 69 6c 65 20 68 65  *      | file he
fcef0 61 64 65 72 20 20 20 20 7c 20 20 20 31 30 30 20  ader    |   100 
fcf00 62 79 74 65 73 2e 20 20 50 61 67 65 20 31 20 6f  bytes.  Page 1 o
fcf10 6e 6c 79 2e 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d  nly..**      |--
fcf20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a  --------------|.
fcf30 2a 2a 20 20 20 20 20 20 7c 20 70 61 67 65 20 68  **      | page h
fcf40 65 61 64 65 72 20 20 20 20 7c 20 20 20 38 20 62  eader    |   8 b
fcf50 79 74 65 73 20 66 6f 72 20 6c 65 61 76 65 73 2e  ytes for leaves.
fcf60 20 20 31 32 20 62 79 74 65 73 20 66 6f 72 20 69    12 bytes for i
fcf70 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 0a 2a 2a  nterior nodes.**
fcf80 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d        |---------
fcf90 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20  -------|.**     
fcfa0 20 7c 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20   | cell pointer 
fcfb0 20 20 7c 20 20 20 7c 20 20 32 20 62 79 74 65 73    |   |  2 bytes
fcfc0 20 70 65 72 20 63 65 6c 6c 2e 20 20 53 6f 72 74   per cell.  Sort
fcfd0 65 64 20 6f 72 64 65 72 2e 0a 2a 2a 20 20 20 20  ed order..**    
fcfe0 20 20 7c 20 61 72 72 61 79 20 20 20 20 20 20 20    | array       
fcff0 20 20 20 7c 20 20 20 7c 20 20 47 72 6f 77 73 20     |   |  Grows 
fd000 64 6f 77 6e 77 61 72 64 0a 2a 2a 20 20 20 20 20  downward.**     
fd010 20 7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20   |              
fd020 20 20 7c 20 20 20 76 0a 2a 2a 20 20 20 20 20 20    |   v.**      
fd030 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  |---------------
fd040 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 75 6e 61  -|.**      | una
fd050 6c 6c 6f 63 61 74 65 64 20 20 20 20 7c 0a 2a 2a  llocated    |.**
fd060 20 20 20 20 20 20 7c 20 73 70 61 63 65 20 20 20        | space   
fd070 20 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20         |.**     
fd080 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   |--------------
fd090 2d 2d 7c 20 20 20 5e 20 20 47 72 6f 77 73 20 75  --|   ^  Grows u
fd0a0 70 77 61 72 64 73 0a 2a 2a 20 20 20 20 20 20 7c  pwards.**      |
fd0b0 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 20 20   cell content   
fd0c0 7c 20 20 20 7c 20 20 41 72 62 69 74 72 61 72 79  |   |  Arbitrary
fd0d0 20 6f 72 64 65 72 20 69 6e 74 65 72 73 70 65 72   order intersper
fd0e0 73 65 64 20 77 69 74 68 20 66 72 65 65 62 6c 6f  sed with freeblo
fd0f0 63 6b 73 2e 0a 2a 2a 20 20 20 20 20 20 7c 20 61  cks..**      | a
fd100 72 65 61 20 20 20 20 20 20 20 20 20 20 20 7c 20  rea           | 
fd110 20 20 7c 20 20 61 6e 64 20 66 72 65 65 20 73 70    |  and free sp
fd120 61 63 65 20 66 72 61 67 6d 65 6e 74 73 2e 0a 2a  ace fragments..*
fd130 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d  *      |--------
fd140 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 0a 2a 2a 20  --------|.**.** 
fd150 54 68 65 20 70 61 67 65 20 68 65 61 64 65 72 73  The page headers
fd160 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73   looks like this
fd170 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 45 54  :.**.**   OFFSET
fd180 20 20 20 53 49 5a 45 20 20 20 20 20 44 45 53 43     SIZE     DESC
fd190 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20  RIPTION.**      
fd1a0 30 20 20 20 20 20 20 20 31 20 20 20 20 20 20 46  0       1      F
fd1b0 6c 61 67 73 2e 20 31 3a 20 69 6e 74 6b 65 79 2c  lags. 1: intkey,
fd1c0 20 32 3a 20 7a 65 72 6f 64 61 74 61 2c 20 34 3a   2: zerodata, 4:
fd1d0 20 6c 65 61 66 64 61 74 61 2c 20 38 3a 20 6c 65   leafdata, 8: le
fd1e0 61 66 0a 2a 2a 20 20 20 20 20 20 31 20 20 20 20  af.**      1    
fd1f0 20 20 20 32 20 20 20 20 20 20 62 79 74 65 20 6f     2      byte o
fd200 66 66 73 65 74 20 74 6f 20 74 68 65 20 66 69 72  ffset to the fir
fd210 73 74 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20  st freeblock.** 
fd220 20 20 20 20 20 33 20 20 20 20 20 20 20 32 20 20       3       2  
fd230 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 63 65      number of ce
fd240 6c 6c 73 20 6f 6e 20 74 68 69 73 20 70 61 67 65  lls on this page
fd250 0a 2a 2a 20 20 20 20 20 20 35 20 20 20 20 20 20  .**      5      
fd260 20 32 20 20 20 20 20 20 66 69 72 73 74 20 62 79   2      first by
fd270 74 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63  te of the cell c
fd280 6f 6e 74 65 6e 74 20 61 72 65 61 0a 2a 2a 20 20  ontent area.**  
fd290 20 20 20 20 37 20 20 20 20 20 20 20 31 20 20 20      7       1   
fd2a0 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 61     number of fra
fd2b0 67 6d 65 6e 74 65 64 20 66 72 65 65 20 62 79 74  gmented free byt
fd2c0 65 73 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 20  es.**      8    
fd2d0 20 20 20 34 20 20 20 20 20 20 52 69 67 68 74 20     4      Right 
fd2e0 63 68 69 6c 64 20 28 74 68 65 20 50 74 72 28 4e  child (the Ptr(N
fd2f0 29 20 76 61 6c 75 65 29 2e 20 20 4f 6d 69 74 74  ) value).  Omitt
fd300 65 64 20 6f 6e 20 6c 65 61 76 65 73 2e 0a 2a 2a  ed on leaves..**
fd310 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 64 65  .** The flags de
fd320 66 69 6e 65 20 74 68 65 20 66 6f 72 6d 61 74 20  fine the format 
fd330 6f 66 20 74 68 69 73 20 62 74 72 65 65 20 70 61  of this btree pa
fd340 67 65 2e 20 20 54 68 65 20 6c 65 61 66 20 66 6c  ge.  The leaf fl
fd350 61 67 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a  ag means that.**
fd360 20 74 68 69 73 20 70 61 67 65 20 68 61 73 20 6e   this page has n
fd370 6f 20 63 68 69 6c 64 72 65 6e 2e 20 20 54 68 65  o children.  The
fd380 20 7a 65 72 6f 64 61 74 61 20 66 6c 61 67 20 6d   zerodata flag m
fd390 65 61 6e 73 20 74 68 61 74 20 74 68 69 73 20 70  eans that this p
fd3a0 61 67 65 20 63 61 72 72 69 65 73 0a 2a 2a 20 6f  age carries.** o
fd3b0 6e 6c 79 20 6b 65 79 73 20 61 6e 64 20 6e 6f 20  nly keys and no 
fd3c0 64 61 74 61 2e 20 20 54 68 65 20 69 6e 74 6b 65  data.  The intke
fd3d0 79 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68 61  y flag means tha
fd3e0 74 20 74 68 65 20 6b 65 79 20 69 73 20 61 20 69  t the key is a i
fd3f0 6e 74 65 67 65 72 0a 2a 2a 20 77 68 69 63 68 20  nteger.** which 
fd400 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  is stored in the
fd410 20 6b 65 79 20 73 69 7a 65 20 65 6e 74 72 79 20   key size entry 
fd420 6f 66 20 74 68 65 20 63 65 6c 6c 20 68 65 61 64  of the cell head
fd430 65 72 20 72 61 74 68 65 72 20 74 68 61 6e 20 69  er rather than i
fd440 6e 0a 2a 2a 20 74 68 65 20 70 61 79 6c 6f 61 64  n.** the payload
fd450 20 61 72 65 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   area..**.** The
fd460 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72   cell pointer ar
fd470 72 61 79 20 62 65 67 69 6e 73 20 6f 6e 20 74 68  ray begins on th
fd480 65 20 66 69 72 73 74 20 62 79 74 65 20 61 66 74  e first byte aft
fd490 65 72 20 74 68 65 20 70 61 67 65 20 68 65 61 64  er the page head
fd4a0 65 72 2e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20  er..** The cell 
fd4b0 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 63 6f  pointer array co
fd4c0 6e 74 61 69 6e 73 20 7a 65 72 6f 20 6f 72 20 6d  ntains zero or m
fd4d0 6f 72 65 20 32 2d 62 79 74 65 20 6e 75 6d 62 65  ore 2-byte numbe
fd4e0 72 73 20 77 68 69 63 68 20 61 72 65 0a 2a 2a 20  rs which are.** 
fd4f0 6f 66 66 73 65 74 73 20 66 72 6f 6d 20 74 68 65  offsets from the
fd500 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
fd510 65 20 70 61 67 65 20 74 6f 20 74 68 65 20 63 65  e page to the ce
fd520 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 74 68  ll content in th
fd530 65 20 63 65 6c 6c 0a 2a 2a 20 63 6f 6e 74 65 6e  e cell.** conten
fd540 74 20 61 72 65 61 2e 20 20 54 68 65 20 63 65 6c  t area.  The cel
fd550 6c 20 70 6f 69 6e 74 65 72 73 20 6f 63 63 75 72  l pointers occur
fd560 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72   in sorted order
fd570 2e 20 20 54 68 65 20 73 79 73 74 65 6d 20 73 74  .  The system st
fd580 72 69 76 65 73 0a 2a 2a 20 74 6f 20 6b 65 65 70  rives.** to keep
fd590 20 66 72 65 65 20 73 70 61 63 65 20 61 66 74 65   free space afte
fd5a0 72 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20  r the last cell 
fd5b0 70 6f 69 6e 74 65 72 20 73 6f 20 74 68 61 74 20  pointer so that 
fd5c0 6e 65 77 20 63 65 6c 6c 73 20 63 61 6e 0a 2a 2a  new cells can.**
fd5d0 20 62 65 20 65 61 73 69 6c 79 20 61 64 64 65 64   be easily added
fd5e0 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20   without having 
fd5f0 74 6f 20 64 65 66 72 61 67 6d 65 6e 74 20 74 68  to defragment th
fd600 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 43 65  e page..**.** Ce
fd610 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 73 74  ll content is st
fd620 6f 72 65 64 20 61 74 20 74 68 65 20 76 65 72 79  ored at the very
fd630 20 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65   end of the page
fd640 20 61 6e 64 20 67 72 6f 77 73 20 74 6f 77 61 72   and grows towar
fd650 64 20 74 68 65 0a 2a 2a 20 62 65 67 69 6e 6e 69  d the.** beginni
fd660 6e 67 20 6f 66 20 74 68 65 20 70 61 67 65 2e 0a  ng of the page..
fd670 2a 2a 0a 2a 2a 20 55 6e 75 73 65 64 20 73 70 61  **.** Unused spa
fd680 63 65 20 77 69 74 68 69 6e 20 74 68 65 20 63 65  ce within the ce
fd690 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20  ll content area 
fd6a0 69 73 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74  is collected int
fd6b0 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  o a linked list 
fd6c0 6f 66 0a 2a 2a 20 66 72 65 65 62 6c 6f 63 6b 73  of.** freeblocks
fd6d0 2e 20 20 45 61 63 68 20 66 72 65 65 62 6c 6f 63  .  Each freebloc
fd6e0 6b 20 69 73 20 61 74 20 6c 65 61 73 74 20 34 20  k is at least 4 
fd6f0 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 20  bytes in size.  
fd700 54 68 65 20 62 79 74 65 20 6f 66 66 73 65 74 0a  The byte offset.
fd710 2a 2a 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  ** to the first 
fd720 66 72 65 65 62 6c 6f 63 6b 20 69 73 20 67 69 76  freeblock is giv
fd730 65 6e 20 69 6e 20 74 68 65 20 68 65 61 64 65 72  en in the header
fd740 2e 20 20 46 72 65 65 62 6c 6f 63 6b 73 20 6f 63  .  Freeblocks oc
fd750 63 75 72 20 69 6e 0a 2a 2a 20 69 6e 63 72 65 61  cur in.** increa
fd760 73 69 6e 67 20 6f 72 64 65 72 2e 20 20 42 65 63  sing order.  Bec
fd770 61 75 73 65 20 61 20 66 72 65 65 62 6c 6f 63 6b  ause a freeblock
fd780 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 73   must be at leas
fd790 74 20 34 20 62 79 74 65 73 20 69 6e 20 73 69 7a  t 4 bytes in siz
fd7a0 65 2c 0a 2a 2a 20 61 6e 79 20 67 72 6f 75 70 20  e,.** any group 
fd7b0 6f 66 20 33 20 6f 72 20 66 65 77 65 72 20 75 6e  of 3 or fewer un
fd7c0 75 73 65 64 20 62 79 74 65 73 20 69 6e 20 74 68  used bytes in th
fd7d0 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61  e cell content a
fd7e0 72 65 61 20 63 61 6e 6e 6f 74 0a 2a 2a 20 65 78  rea cannot.** ex
fd7f0 69 73 74 20 6f 6e 20 74 68 65 20 66 72 65 65 62  ist on the freeb
fd800 6c 6f 63 6b 20 63 68 61 69 6e 2e 20 20 41 20 67  lock chain.  A g
fd810 72 6f 75 70 20 6f 66 20 33 20 6f 72 20 66 65 77  roup of 3 or few
fd820 65 72 20 66 72 65 65 20 62 79 74 65 73 20 69 73  er free bytes is
fd830 20 63 61 6c 6c 65 64 0a 2a 2a 20 61 20 66 72 61   called.** a fra
fd840 67 6d 65 6e 74 2e 20 20 54 68 65 20 74 6f 74 61  gment.  The tota
fd850 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  l number of byte
fd860 73 20 69 6e 20 61 6c 6c 20 66 72 61 67 6d 65 6e  s in all fragmen
fd870 74 73 20 69 73 20 72 65 63 6f 72 64 65 64 2e 0a  ts is recorded..
fd880 2a 2a 20 69 6e 20 74 68 65 20 70 61 67 65 20 68  ** in the page h
fd890 65 61 64 65 72 20 61 74 20 6f 66 66 73 65 74 20  eader at offset 
fd8a0 37 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45  7..**.**    SIZE
fd8b0 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a      DESCRIPTION.
fd8c0 2a 2a 20 20 20 20 20 20 32 20 20 20 20 20 42 79  **      2     By
fd8d0 74 65 20 6f 66 66 73 65 74 20 6f 66 20 74 68 65  te offset of the
fd8e0 20 6e 65 78 74 20 66 72 65 65 62 6c 6f 63 6b 0a   next freeblock.
fd8f0 2a 2a 20 20 20 20 20 20 32 20 20 20 20 20 42 79  **      2     By
fd900 74 65 73 20 69 6e 20 74 68 69 73 20 66 72 65 65  tes in this free
fd910 62 6c 6f 63 6b 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c  block.**.** Cell
fd920 73 20 61 72 65 20 6f 66 20 76 61 72 69 61 62 6c  s are of variabl
fd930 65 20 6c 65 6e 67 74 68 2e 20 20 43 65 6c 6c 73  e length.  Cells
fd940 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74   are stored in t
fd950 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  he cell content 
fd960 61 72 65 61 20 61 74 0a 2a 2a 20 74 68 65 20 65  area at.** the e
fd970 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65 2e 20  nd of the page. 
fd980 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65   Pointers to the
fd990 20 63 65 6c 6c 73 20 61 72 65 20 69 6e 20 74 68   cells are in th
fd9a0 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61  e cell pointer a
fd9b0 72 72 61 79 0a 2a 2a 20 74 68 61 74 20 69 6d 6d  rray.** that imm
fd9c0 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 73  ediately follows
fd9d0 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72   the page header
fd9e0 2e 20 20 43 65 6c 6c 73 20 69 73 20 6e 6f 74 20  .  Cells is not 
fd9f0 6e 65 63 65 73 73 61 72 69 6c 79 0a 2a 2a 20 63  necessarily.** c
fda00 6f 6e 74 69 67 75 6f 75 73 20 6f 72 20 69 6e 20  ontiguous or in 
fda10 6f 72 64 65 72 2c 20 62 75 74 20 63 65 6c 6c 20  order, but cell 
fda20 70 6f 69 6e 74 65 72 73 20 61 72 65 20 63 6f 6e  pointers are con
fda30 74 69 67 75 6f 75 73 20 61 6e 64 20 69 6e 20 6f  tiguous and in o
fda40 72 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c  rder..**.** Cell
fda50 20 63 6f 6e 74 65 6e 74 20 6d 61 6b 65 73 20 75   content makes u
fda60 73 65 20 6f 66 20 76 61 72 69 61 62 6c 65 20 6c  se of variable l
fda70 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 2e 20  ength integers. 
fda80 20 41 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6c   A variable.** l
fda90 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 69 73  ength integer is
fdaa0 20 31 20 74 6f 20 39 20 62 79 74 65 73 20 77 68   1 to 9 bytes wh
fdab0 65 72 65 20 74 68 65 20 6c 6f 77 65 72 20 37 20  ere the lower 7 
fdac0 62 69 74 73 20 6f 66 20 65 61 63 68 20 0a 2a 2a  bits of each .**
fdad0 20 62 79 74 65 20 61 72 65 20 75 73 65 64 2e 20   byte are used. 
fdae0 20 54 68 65 20 69 6e 74 65 67 65 72 20 63 6f 6e   The integer con
fdaf0 73 69 73 74 73 20 6f 66 20 61 6c 6c 20 62 79 74  sists of all byt
fdb00 65 73 20 74 68 61 74 20 68 61 76 65 20 62 69 74  es that have bit
fdb10 20 38 20 73 65 74 20 61 6e 64 0a 2a 2a 20 74 68   8 set and.** th
fdb20 65 20 66 69 72 73 74 20 62 79 74 65 20 77 69 74  e first byte wit
fdb30 68 20 62 69 74 20 38 20 63 6c 65 61 72 2e 20 20  h bit 8 clear.  
fdb40 54 68 65 20 6d 6f 73 74 20 73 69 67 6e 69 66 69  The most signifi
fdb50 63 61 6e 74 20 62 79 74 65 20 6f 66 20 74 68 65  cant byte of the
fdb60 20 69 6e 74 65 67 65 72 0a 2a 2a 20 61 70 70 65   integer.** appe
fdb70 61 72 73 20 66 69 72 73 74 2e 20 20 41 20 76 61  ars first.  A va
fdb80 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e  riable-length in
fdb90 74 65 67 65 72 20 6d 61 79 20 6e 6f 74 20 62 65  teger may not be
fdba0 20 6d 6f 72 65 20 74 68 61 6e 20 39 20 62 79 74   more than 9 byt
fdbb0 65 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 41 73 20 61  es long..** As a
fdbc0 20 73 70 65 63 69 61 6c 20 63 61 73 65 2c 20 61   special case, a
fdbd0 6c 6c 20 38 20 62 79 74 65 73 20 6f 66 20 74 68  ll 8 bytes of th
fdbe0 65 20 39 74 68 20 62 79 74 65 20 61 72 65 20 75  e 9th byte are u
fdbf0 73 65 64 20 61 73 20 64 61 74 61 2e 20 20 54 68  sed as data.  Th
fdc00 69 73 0a 2a 2a 20 61 6c 6c 6f 77 73 20 61 20 36  is.** allows a 6
fdc10 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 74 6f  4-bit integer to
fdc20 20 62 65 20 65 6e 63 6f 64 65 64 20 69 6e 20 39   be encoded in 9
fdc30 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20   bytes..**.**   
fdc40 20 30 78 30 30 20 20 20 20 20 20 20 20 20 20 20   0x00           
fdc50 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d             becom
fdc60 65 73 20 20 30 78 30 30 30 30 30 30 30 30 0a 2a  es  0x00000000.*
fdc70 2a 20 20 20 20 30 78 37 66 20 20 20 20 20 20 20  *    0x7f       
fdc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
fdc90 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 30  ecomes  0x000000
fdca0 37 66 0a 2a 2a 20 20 20 20 30 78 38 31 20 30 78  7f.**    0x81 0x
fdcb0 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20  00              
fdcc0 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30     becomes  0x00
fdcd0 30 30 30 30 38 30 0a 2a 2a 20 20 20 20 30 78 38  000080.**    0x8
fdce0 32 20 30 78 30 30 20 20 20 20 20 20 20 20 20 20  2 0x00          
fdcf0 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20         becomes  
fdd00 30 78 30 30 30 30 30 31 30 30 0a 2a 2a 20 20 20  0x00000100.**   
fdd10 20 30 78 38 30 20 30 78 37 66 20 20 20 20 20 20   0x80 0x7f      
fdd20 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d             becom
fdd30 65 73 20 20 30 78 30 30 30 30 30 30 37 66 0a 2a  es  0x0000007f.*
fdd40 2a 20 20 20 20 30 78 38 61 20 30 78 39 31 20 30  *    0x8a 0x91 0
fdd50 78 64 31 20 30 78 61 63 20 30 78 37 38 20 20 62  xd1 0xac 0x78  b
fdd60 65 63 6f 6d 65 73 20 20 30 78 31 32 33 34 35 36  ecomes  0x123456
fdd70 37 38 0a 2a 2a 20 20 20 20 30 78 38 31 20 30 78  78.**    0x81 0x
fdd80 38 31 20 30 78 38 31 20 30 78 38 31 20 30 78 30  81 0x81 0x81 0x0
fdd90 31 20 20 62 65 63 6f 6d 65 73 20 20 30 78 31 30  1  becomes  0x10
fdda0 32 30 34 30 38 31 0a 2a 2a 0a 2a 2a 20 56 61 72  204081.**.** Var
fddb0 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74  iable length int
fddc0 65 67 65 72 73 20 61 72 65 20 75 73 65 64 20 66  egers are used f
fddd0 6f 72 20 72 6f 77 69 64 73 20 61 6e 64 20 74 6f  or rowids and to
fdde0 20 68 6f 6c 64 20 74 68 65 20 6e 75 6d 62 65 72   hold the number
fddf0 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 6f 66 20   of.** bytes of 
fde00 6b 65 79 20 61 6e 64 20 64 61 74 61 20 69 6e 20  key and data in 
fde10 61 20 62 74 72 65 65 20 63 65 6c 6c 2e 0a 2a 2a  a btree cell..**
fde20 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20  .** The content 
fde30 6f 66 20 61 20 63 65 6c 6c 20 6c 6f 6f 6b 73 20  of a cell looks 
fde40 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a  like this:.**.**
fde50 20 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43      SIZE    DESC
fde60 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20  RIPTION.**      
fde70 34 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65  4     Page numbe
fde80 72 20 6f 66 20 74 68 65 20 6c 65 66 74 20 63 68  r of the left ch
fde90 69 6c 64 2e 20 4f 6d 69 74 74 65 64 20 69 66 20  ild. Omitted if 
fdea0 6c 65 61 66 20 66 6c 61 67 20 69 73 20 73 65 74  leaf flag is set
fdeb0 2e 0a 2a 2a 20 20 20 20 20 76 61 72 20 20 20 20  ..**     var    
fdec0 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
fded0 6f 66 20 64 61 74 61 2e 20 4f 6d 69 74 74 65 64  of data. Omitted
fdee0 20 69 66 20 74 68 65 20 7a 65 72 6f 64 61 74 61   if the zerodata
fdef0 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a   flag is set..**
fdf00 20 20 20 20 20 76 61 72 20 20 20 20 4e 75 6d 62       var    Numb
fdf10 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 6b  er of bytes of k
fdf20 65 79 2e 20 4f 72 20 74 68 65 20 6b 65 79 20 69  ey. Or the key i
fdf30 74 73 65 6c 66 20 69 66 20 69 6e 74 6b 65 79 20  tself if intkey 
fdf40 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20  flag is set..** 
fdf50 20 20 20 20 20 2a 20 20 20 20 20 50 61 79 6c 6f       *     Paylo
fdf60 61 64 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20  ad.**      4    
fdf70 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20 74   First page of t
fdf80 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69  he overflow chai
fdf90 6e 2e 20 20 4f 6d 69 74 74 65 64 20 69 66 20 6e  n.  Omitted if n
fdfa0 6f 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 0a 2a 2a  o overflow.**.**
fdfb0 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20   Overflow pages 
fdfc0 66 6f 72 6d 20 61 20 6c 69 6e 6b 65 64 20 6c 69  form a linked li
fdfd0 73 74 2e 20 20 45 61 63 68 20 70 61 67 65 20 65  st.  Each page e
fdfe0 78 63 65 70 74 20 74 68 65 20 6c 61 73 74 20 69  xcept the last i
fdff0 73 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20  s completely.** 
fe000 66 69 6c 6c 65 64 20 77 69 74 68 20 64 61 74 61  filled with data
fe010 20 28 70 61 67 65 73 69 7a 65 20 2d 20 34 20 62   (pagesize - 4 b
fe020 79 74 65 73 29 2e 20 20 54 68 65 20 6c 61 73 74  ytes).  The last
fe030 20 70 61 67 65 20 63 61 6e 20 68 61 76 65 20 61   page can have a
fe040 73 20 6c 69 74 74 6c 65 0a 2a 2a 20 61 73 20 31  s little.** as 1
fe050 20 62 79 74 65 20 6f 66 20 64 61 74 61 2e 0a 2a   byte of data..*
fe060 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 20  *.**    SIZE    
fe070 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20  DESCRIPTION.**  
fe080 20 20 20 20 34 20 20 20 20 20 50 61 67 65 20 6e      4     Page n
fe090 75 6d 62 65 72 20 6f 66 20 6e 65 78 74 20 6f 76  umber of next ov
fe0a0 65 72 66 6c 6f 77 20 70 61 67 65 0a 2a 2a 20 20  erflow page.**  
fe0b0 20 20 20 20 2a 20 20 20 20 20 44 61 74 61 0a 2a      *     Data.*
fe0c0 2a 0a 2a 2a 20 46 72 65 65 6c 69 73 74 20 70 61  *.** Freelist pa
fe0d0 67 65 73 20 63 6f 6d 65 20 69 6e 20 74 77 6f 20  ges come in two 
fe0e0 73 75 62 74 79 70 65 73 3a 20 74 72 75 6e 6b 20  subtypes: trunk 
fe0f0 70 61 67 65 73 20 61 6e 64 20 6c 65 61 66 20 70  pages and leaf p
fe100 61 67 65 73 2e 20 20 54 68 65 0a 2a 2a 20 66 69  ages.  The.** fi
fe110 6c 65 20 68 65 61 64 65 72 20 70 6f 69 6e 74 73  le header points
fe120 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 69 6e   to the first in
fe130 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f   a linked list o
fe140 66 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 20 45  f trunk page.  E
fe150 61 63 68 20 74 72 75 6e 6b 0a 2a 2a 20 70 61 67  ach trunk.** pag
fe160 65 20 70 6f 69 6e 74 73 20 74 6f 20 6d 75 6c 74  e points to mult
fe170 69 70 6c 65 20 6c 65 61 66 20 70 61 67 65 73 2e  iple leaf pages.
fe180 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66    The content of
fe190 20 61 20 6c 65 61 66 20 70 61 67 65 20 69 73 0a   a leaf page is.
fe1a0 2a 2a 20 75 6e 73 70 65 63 69 66 69 65 64 2e 20  ** unspecified. 
fe1b0 20 41 20 74 72 75 6e 6b 20 70 61 67 65 20 6c 6f   A trunk page lo
fe1c0 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a  oks like this:.*
fe1d0 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 20  *.**    SIZE    
fe1e0 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20  DESCRIPTION.**  
fe1f0 20 20 20 20 34 20 20 20 20 20 50 61 67 65 20 6e      4     Page n
fe200 75 6d 62 65 72 20 6f 66 20 6e 65 78 74 20 74 72  umber of next tr
fe210 75 6e 6b 20 70 61 67 65 0a 2a 2a 20 20 20 20 20  unk page.**     
fe220 20 34 20 20 20 20 20 4e 75 6d 62 65 72 20 6f 66   4     Number of
fe230 20 6c 65 61 66 20 70 6f 69 6e 74 65 72 73 20 6f   leaf pointers o
fe240 6e 20 74 68 69 73 20 70 61 67 65 0a 2a 2a 20 20  n this page.**  
fe250 20 20 20 20 2a 20 20 20 20 20 7a 65 72 6f 20 6f      *     zero o
fe260 72 20 6d 6f 72 65 20 70 61 67 65 73 20 6e 75 6d  r more pages num
fe270 62 65 72 73 20 6f 66 20 6c 65 61 76 65 73 0a 2a  bers of leaves.*
fe280 2f 0a 0a 2f 2a 20 52 6f 75 6e 64 20 75 70 20 61  /../* Round up a
fe290 20 6e 75 6d 62 65 72 20 74 6f 20 74 68 65 20 6e   number to the n
fe2a0 65 78 74 20 6c 61 72 67 65 72 20 6d 75 6c 74 69  ext larger multi
fe2b0 70 6c 65 20 6f 66 20 38 2e 20 20 54 68 69 73 20  ple of 8.  This 
fe2c0 69 73 20 75 73 65 64 0a 2a 2a 20 74 6f 20 66 6f  is used.** to fo
fe2d0 72 63 65 20 38 2d 62 79 74 65 20 61 6c 69 67 6e  rce 8-byte align
fe2e0 6d 65 6e 74 20 6f 6e 20 36 34 2d 62 69 74 20 61  ment on 64-bit a
fe2f0 72 63 68 69 74 65 63 74 75 72 65 73 2e 0a 2a 2f  rchitectures..*/
fe300 0a 23 64 65 66 69 6e 65 20 52 4f 55 4e 44 38 28  .#define ROUND8(
fe310 78 29 20 20 20 28 28 78 2b 37 29 26 7e 37 29 0a  x)   ((x+7)&~7).
fe320 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ../* The followi
fe330 6e 67 20 76 61 6c 75 65 20 69 73 20 74 68 65 20  ng value is the 
fe340 6d 61 78 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a  maximum cell siz
fe350 65 20 61 73 73 75 6d 69 6e 67 20 61 20 6d 61 78  e assuming a max
fe360 69 6d 75 6d 20 70 61 67 65 0a 2a 2a 20 73 69 7a  imum page.** siz
fe370 65 20 67 69 76 65 20 61 62 6f 76 65 2e 0a 2a 2f  e give above..*/
fe380 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43 45 4c 4c  .#define MX_CELL
fe390 5f 53 49 5a 45 28 70 42 74 29 20 20 28 70 42 74  _SIZE(pBt)  (pBt
fe3a0 2d 3e 70 61 67 65 53 69 7a 65 2d 38 29 0a 0a 2f  ->pageSize-8)../
fe3b0 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75  * The maximum nu
fe3c0 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e  mber of cells on
fe3d0 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 6f   a single page o
fe3e0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  f the database. 
fe3f0 20 54 68 69 73 0a 2a 2a 20 61 73 73 75 6d 65 73   This.** assumes
fe400 20 61 20 6d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20   a minimum cell 
fe410 73 69 7a 65 20 6f 66 20 36 20 62 79 74 65 73 20  size of 6 bytes 
fe420 20 28 34 20 62 79 74 65 73 20 66 6f 72 20 74 68   (4 bytes for th
fe430 65 20 63 65 6c 6c 20 69 74 73 65 6c 66 0a 2a 2a  e cell itself.**
fe440 20 70 6c 75 73 20 32 20 62 79 74 65 73 20 66 6f   plus 2 bytes fo
fe450 72 20 74 68 65 20 69 6e 64 65 78 20 74 6f 20 74  r the index to t
fe460 68 65 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 70  he cell in the p
fe470 61 67 65 20 68 65 61 64 65 72 29 2e 20 20 53 75  age header).  Su
fe480 63 68 0a 2a 2a 20 73 6d 61 6c 6c 20 63 65 6c 6c  ch.** small cell
fe490 73 20 77 69 6c 6c 20 62 65 20 72 61 72 65 2c 20  s will be rare, 
fe4a0 62 75 74 20 74 68 65 79 20 61 72 65 20 70 6f 73  but they are pos
fe4b0 73 69 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e  sible..*/.#defin
fe4c0 65 20 4d 58 5f 43 45 4c 4c 28 70 42 74 29 20 28  e MX_CELL(pBt) (
fe4d0 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2d 38  (pBt->pageSize-8
fe4e0 29 2f 36 29 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  )/6)../* Forward
fe4f0 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 2a 2f   declarations */
fe500 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
fe510 4d 65 6d 50 61 67 65 20 4d 65 6d 50 61 67 65 3b  MemPage MemPage;
fe520 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
fe530 42 74 4c 6f 63 6b 20 42 74 4c 6f 63 6b 3b 0a 0a  BtLock BtLock;..
fe540 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20  /*.** This is a 
fe550 6d 61 67 69 63 20 73 74 72 69 6e 67 20 74 68 61  magic string tha
fe560 74 20 61 70 70 65 61 72 73 20 61 74 20 74 68 65  t appears at the
fe570 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76   beginning of ev
fe580 65 72 79 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61  ery.** SQLite da
fe590 74 61 62 61 73 65 20 69 6e 20 6f 72 64 65 72 20  tabase in order 
fe5a0 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68 65 20  to identify the 
fe5b0 66 69 6c 65 20 61 73 20 61 20 72 65 61 6c 20 64  file as a real d
fe5c0 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 59  atabase..**.** Y
fe5d0 6f 75 20 63 61 6e 20 63 68 61 6e 67 65 20 74 68  ou can change th
fe5e0 69 73 20 76 61 6c 75 65 20 61 74 20 63 6f 6d 70  is value at comp
fe5f0 69 6c 65 2d 74 69 6d 65 20 62 79 20 73 70 65 63  ile-time by spec
fe600 69 66 79 69 6e 67 20 61 0a 2a 2a 20 2d 44 53 51  ifying a.** -DSQ
fe610 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52  LITE_FILE_HEADER
fe620 3d 22 2e 2e 2e 22 20 6f 6e 20 74 68 65 20 63 6f  ="..." on the co
fe630 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 2d 6c  mpiler command-l
fe640 69 6e 65 2e 20 20 54 68 65 0a 2a 2a 20 68 65 61  ine.  The.** hea
fe650 64 65 72 20 6d 75 73 74 20 62 65 20 65 78 61 63  der must be exac
fe660 74 6c 79 20 31 36 20 62 79 74 65 73 20 69 6e 63  tly 16 bytes inc
fe670 6c 75 64 69 6e 67 20 74 68 65 20 7a 65 72 6f 2d  luding the zero-
fe680 74 65 72 6d 69 6e 61 74 6f 72 20 73 6f 0a 2a 2a  terminator so.**
fe690 20 74 68 65 20 73 74 72 69 6e 67 20 69 74 73 65   the string itse
fe6a0 6c 66 20 73 68 6f 75 6c 64 20 62 65 20 31 35 20  lf should be 15 
fe6b0 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e  characters long.
fe6c0 20 20 49 66 20 79 6f 75 20 63 68 61 6e 67 65 0a    If you change.
fe6d0 2a 2a 20 74 68 65 20 68 65 61 64 65 72 2c 20 74  ** the header, t
fe6e0 68 65 6e 20 79 6f 75 72 20 63 75 73 74 6f 6d 20  hen your custom 
fe6f0 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 6e 6f 74  library will not
fe700 20 62 65 20 61 62 6c 65 20 74 6f 20 72 65 61 64   be able to read
fe710 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 73 20 67   .** databases g
fe720 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 20  enerated by the 
fe730 73 74 61 6e 64 61 72 64 20 74 6f 6f 6c 73 20 61  standard tools a
fe740 6e 64 20 74 68 65 20 73 74 61 6e 64 61 72 64 20  nd the standard 
fe750 74 6f 6f 6c 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f  tools.** will no
fe760 74 20 62 65 20 61 62 6c 65 20 74 6f 20 72 65 61  t be able to rea
fe770 64 20 64 61 74 61 62 61 73 65 73 20 63 72 65 61  d databases crea
fe780 74 65 64 20 62 79 20 79 6f 75 72 20 63 75 73 74  ted by your cust
fe790 6f 6d 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23  om library..*/.#
fe7a0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 46 49  ifndef SQLITE_FI
fe7b0 4c 45 5f 48 45 41 44 45 52 20 2f 2a 20 31 32 33  LE_HEADER /* 123
fe7c0 34 35 36 37 38 39 20 31 32 33 34 35 36 20 2a 2f  456789 123456 */
fe7d0 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54  .#  define SQLIT
fe7e0 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 20 22 53  E_FILE_HEADER "S
fe7f0 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 22 0a  QLite format 3".
fe800 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 61  #endif../*.** Pa
fe810 67 65 20 74 79 70 65 20 66 6c 61 67 73 2e 20 20  ge type flags.  
fe820 41 6e 20 4f 52 65 64 20 63 6f 6d 62 69 6e 61 74  An ORed combinat
fe830 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 66 6c 61  ion of these fla
fe840 67 73 20 61 70 70 65 61 72 20 61 73 20 74 68 65  gs appear as the
fe850 0a 2a 2a 20 66 69 72 73 74 20 62 79 74 65 20 6f  .** first byte o
fe860 66 20 6f 6e 2d 64 69 73 6b 20 69 6d 61 67 65 20  f on-disk image 
fe870 6f 66 20 65 76 65 72 79 20 42 54 72 65 65 20 70  of every BTree p
fe880 61 67 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  age..*/.#define 
fe890 50 54 46 5f 49 4e 54 4b 45 59 20 20 20 20 30 78  PTF_INTKEY    0x
fe8a0 30 31 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 5a  01.#define PTF_Z
fe8b0 45 52 4f 44 41 54 41 20 20 30 78 30 32 0a 23 64  ERODATA  0x02.#d
fe8c0 65 66 69 6e 65 20 50 54 46 5f 4c 45 41 46 44 41  efine PTF_LEAFDA
fe8d0 54 41 20 20 30 78 30 34 0a 23 64 65 66 69 6e 65  TA  0x04.#define
fe8e0 20 50 54 46 5f 4c 45 41 46 20 20 20 20 20 20 30   PTF_LEAF      0
fe8f0 78 30 38 0a 0a 2f 2a 0a 2a 2a 20 41 73 20 65 61  x08../*.** As ea
fe900 63 68 20 70 61 67 65 20 6f 66 20 74 68 65 20 66  ch page of the f
fe910 69 6c 65 20 69 73 20 6c 6f 61 64 65 64 20 69 6e  ile is loaded in
fe920 74 6f 20 6d 65 6d 6f 72 79 2c 20 61 6e 20 69 6e  to memory, an in
fe930 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
fe940 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63  llowing.** struc
fe950 74 75 72 65 20 69 73 20 61 70 70 65 6e 64 65 64  ture is appended
fe960 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 64   and initialized
fe970 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 69 73 20   to zero.  This 
fe980 73 74 72 75 63 74 75 72 65 20 73 74 6f 72 65 73  structure stores
fe990 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  .** information 
fe9a0 61 62 6f 75 74 20 74 68 65 20 70 61 67 65 20 74  about the page t
fe9b0 68 61 74 20 69 73 20 64 65 63 6f 64 65 64 20 66  hat is decoded f
fe9c0 72 6f 6d 20 74 68 65 20 72 61 77 20 66 69 6c 65  rom the raw file
fe9d0 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   page..**.** The
fe9e0 20 70 50 61 72 65 6e 74 20 66 69 65 6c 64 20 70   pParent field p
fe9f0 6f 69 6e 74 73 20 62 61 63 6b 20 74 6f 20 74 68  oints back to th
fea00 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20 20  e parent page.  
fea10 54 68 69 73 20 61 6c 6c 6f 77 73 20 75 73 20 74  This allows us t
fea20 6f 0a 2a 2a 20 77 61 6c 6b 20 75 70 20 74 68 65  o.** walk up the
fea30 20 42 54 72 65 65 20 66 72 6f 6d 20 61 6e 79 20   BTree from any 
fea40 6c 65 61 66 20 74 6f 20 74 68 65 20 72 6f 6f 74  leaf to the root
fea50 2e 20 20 43 61 72 65 20 6d 75 73 74 20 62 65 20  .  Care must be 
fea60 74 61 6b 65 6e 20 74 6f 0a 2a 2a 20 75 6e 72 65  taken to.** unre
fea70 66 28 29 20 74 68 65 20 70 61 72 65 6e 74 20 70  f() the parent p
fea80 61 67 65 20 70 6f 69 6e 74 65 72 20 77 68 65 6e  age pointer when
fea90 20 74 68 69 73 20 70 61 67 65 20 69 73 20 6e 6f   this page is no
feaa0 20 6c 6f 6e 67 65 72 20 72 65 66 65 72 65 6e 63   longer referenc
feab0 65 64 2e 0a 2a 2a 20 54 68 65 20 70 61 67 65 44  ed..** The pageD
feac0 65 73 74 72 75 63 74 6f 72 28 29 20 72 6f 75 74  estructor() rout
fead0 69 6e 65 20 68 61 6e 64 6c 65 73 20 74 68 61 74  ine handles that
feae0 20 63 68 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 63   chore..**.** Ac
feaf0 63 65 73 73 20 74 6f 20 61 6c 6c 20 66 69 65 6c  cess to all fiel
feb00 64 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ds of this struc
feb10 74 75 72 65 20 69 73 20 63 6f 6e 74 72 6f 6c 6c  ture is controll
feb20 65 64 20 62 79 20 74 68 65 20 6d 75 74 65 78 0a  ed by the mutex.
feb30 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 4d 65 6d  ** stored in Mem
feb40 50 61 67 65 2e 70 42 74 2d 3e 6d 75 74 65 78 2e  Page.pBt->mutex.
feb50 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 50 61  .*/.struct MemPa
feb60 67 65 20 7b 0a 20 20 75 38 20 69 73 49 6e 69 74  ge {.  u8 isInit
feb70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
feb80 72 75 65 20 69 66 20 70 72 65 76 69 6f 75 73 6c  rue if previousl
feb90 79 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 4d  y initialized. M
feba0 55 53 54 20 42 45 20 46 49 52 53 54 21 20 2a 2f  UST BE FIRST! */
febb0 0a 20 20 75 38 20 69 64 78 53 68 69 66 74 3b 20  .  u8 idxShift; 
febc0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
febd0 69 66 20 43 65 6c 6c 20 69 6e 64 69 63 65 73 20  if Cell indices 
febe0 68 61 76 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a  have changed */.
febf0 20 20 75 38 20 6e 4f 76 65 72 66 6c 6f 77 3b 20    u8 nOverflow; 
fec00 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
fec10 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c   of overflow cel
fec20 6c 20 62 6f 64 69 65 73 20 69 6e 20 61 43 65 6c  l bodies in aCel
fec30 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 69 6e 74 4b  l[] */.  u8 intK
fec40 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ey;           /*
fec50 20 54 72 75 65 20 69 66 20 69 6e 74 6b 65 79 20   True if intkey 
fec60 66 6c 61 67 20 69 73 20 73 65 74 20 2a 2f 0a 20  flag is set */. 
fec70 20 75 38 20 6c 65 61 66 3b 20 20 20 20 20 20 20   u8 leaf;       
fec80 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
fec90 20 6c 65 61 66 20 66 6c 61 67 20 69 73 20 73 65   leaf flag is se
feca0 74 20 2a 2f 0a 20 20 75 38 20 7a 65 72 6f 44 61  t */.  u8 zeroDa
fecb0 74 61 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ta;         /* T
fecc0 72 75 65 20 69 66 20 74 61 62 6c 65 20 73 74 6f  rue if table sto
fecd0 72 65 73 20 6b 65 79 73 20 6f 6e 6c 79 20 2a 2f  res keys only */
fece0 0a 20 20 75 38 20 6c 65 61 66 44 61 74 61 3b 20  .  u8 leafData; 
fecf0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
fed00 69 66 20 74 61 62 6c 65 73 20 73 74 6f 72 65 73  if tables stores
fed10 20 64 61 74 61 20 6f 6e 20 6c 65 61 76 65 73 20   data on leaves 
fed20 6f 6e 6c 79 20 2a 2f 0a 20 20 75 38 20 68 61 73  only */.  u8 has
fed30 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 2f  Data;          /
fed40 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 70  * True if this p
fed50 61 67 65 20 73 74 6f 72 65 73 20 64 61 74 61 20  age stores data 
fed60 2a 2f 0a 20 20 75 38 20 68 64 72 4f 66 66 73 65  */.  u8 hdrOffse
fed70 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 31 30 30  t;        /* 100
fed80 20 66 6f 72 20 70 61 67 65 20 31 2e 20 20 30 20   for page 1.  0 
fed90 6f 74 68 65 72 77 69 73 65 20 2a 2f 0a 20 20 75  otherwise */.  u
feda0 38 20 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 20  8 childPtrSize; 
fedb0 20 20 20 20 2f 2a 20 30 20 69 66 20 6c 65 61 66      /* 0 if leaf
fedc0 3d 3d 31 2e 20 20 34 20 69 66 20 6c 65 61 66 3d  ==1.  4 if leaf=
fedd0 3d 30 20 2a 2f 0a 20 20 75 31 36 20 6d 61 78 4c  =0 */.  u16 maxL
fede0 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20  ocal;        /* 
fedf0 43 6f 70 79 20 6f 66 20 42 74 53 68 61 72 65 64  Copy of BtShared
fee00 2e 6d 61 78 4c 6f 63 61 6c 20 6f 72 20 42 74 53  .maxLocal or BtS
fee10 68 61 72 65 64 2e 6d 61 78 4c 65 61 66 20 2a 2f  hared.maxLeaf */
fee20 0a 20 20 75 31 36 20 6d 69 6e 4c 6f 63 61 6c 3b  .  u16 minLocal;
fee30 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20          /* Copy 
fee40 6f 66 20 42 74 53 68 61 72 65 64 2e 6d 69 6e 4c  of BtShared.minL
fee50 6f 63 61 6c 20 6f 72 20 42 74 53 68 61 72 65 64  ocal or BtShared
fee60 2e 6d 69 6e 4c 65 61 66 20 2a 2f 0a 20 20 75 31  .minLeaf */.  u1
fee70 36 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20  6 cellOffset;   
fee80 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 61     /* Index in a
fee90 44 61 74 61 20 6f 66 20 66 69 72 73 74 20 63 65  Data of first ce
feea0 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20  ll pointer */.  
feeb0 75 31 36 20 69 64 78 50 61 72 65 6e 74 3b 20 20  u16 idxParent;  
feec0 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e       /* Index in
feed0 20 70 61 72 65 6e 74 20 6f 66 20 74 68 69 73 20   parent of this 
feee0 6e 6f 64 65 20 2a 2f 0a 20 20 75 31 36 20 6e 46  node */.  u16 nF
feef0 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ree;           /
fef00 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65  * Number of free
fef10 20 62 79 74 65 73 20 6f 6e 20 74 68 65 20 70 61   bytes on the pa
fef20 67 65 20 2a 2f 0a 20 20 75 31 36 20 6e 43 65 6c  ge */.  u16 nCel
fef30 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  l;           /* 
fef40 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20  Number of cells 
fef50 6f 6e 20 74 68 69 73 20 70 61 67 65 2c 20 6c 6f  on this page, lo
fef60 63 61 6c 20 61 6e 64 20 6f 76 66 6c 20 2a 2f 0a  cal and ovfl */.
fef70 20 20 73 74 72 75 63 74 20 5f 4f 76 66 6c 43 65    struct _OvflCe
fef80 6c 6c 20 7b 20 20 20 2f 2a 20 43 65 6c 6c 73 20  ll {   /* Cells 
fef90 74 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 66 69  that will not fi
fefa0 74 20 6f 6e 20 61 44 61 74 61 5b 5d 20 2a 2f 0a  t on aData[] */.
fefb0 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20      u8 *pCell;  
fefc0 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
fefd0 65 72 73 20 74 6f 20 74 68 65 20 62 6f 64 79 20  ers to the body 
fefe0 6f 66 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  of the overflow 
feff0 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 75 31 36 20  cell */.    u16 
ff000 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  idx;            
ff010 2f 2a 20 49 6e 73 65 72 74 20 74 68 69 73 20 63  /* Insert this c
ff020 65 6c 6c 20 62 65 66 6f 72 65 20 69 64 78 2d 74  ell before idx-t
ff030 68 20 6e 6f 6e 2d 6f 76 65 72 66 6c 6f 77 20 63  h non-overflow c
ff040 65 6c 6c 20 2a 2f 0a 20 20 7d 20 61 4f 76 66 6c  ell */.  } aOvfl
ff050 5b 35 5d 3b 0a 20 20 42 74 53 68 61 72 65 64 20  [5];.  BtShared 
ff060 2a 70 42 74 3b 20 20 20 20 20 20 20 2f 2a 20 50  *pBt;       /* P
ff070 6f 69 6e 74 65 72 20 74 6f 20 42 74 53 68 61 72  ointer to BtShar
ff080 65 64 20 74 68 61 74 20 74 68 69 73 20 70 61 67  ed that this pag
ff090 65 20 69 73 20 70 61 72 74 20 6f 66 20 2a 2f 0a  e is part of */.
ff0a0 20 20 75 38 20 2a 61 44 61 74 61 3b 20 20 20 20    u8 *aData;    
ff0b0 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
ff0c0 72 20 74 6f 20 64 69 73 6b 20 69 6d 61 67 65 20  r to disk image 
ff0d0 6f 66 20 74 68 65 20 70 61 67 65 20 64 61 74 61  of the page data
ff0e0 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 70 44   */.  DbPage *pD
ff0f0 62 50 61 67 65 3b 20 20 20 20 20 2f 2a 20 50 61  bPage;     /* Pa
ff100 67 65 72 20 70 61 67 65 20 68 61 6e 64 6c 65 20  ger page handle 
ff110 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 20  */.  Pgno pgno; 
ff120 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
ff130 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 69  e number for thi
ff140 73 20 70 61 67 65 20 2a 2f 0a 20 20 4d 65 6d 50  s page */.  MemP
ff150 61 67 65 20 2a 70 50 61 72 65 6e 74 3b 20 20 20  age *pParent;   
ff160 20 2f 2a 20 54 68 65 20 70 61 72 65 6e 74 20 6f   /* The parent o
ff170 66 20 74 68 69 73 20 70 61 67 65 2e 20 20 4e 55  f this page.  NU
ff180 4c 4c 20 66 6f 72 20 72 6f 6f 74 20 2a 2f 0a 7d  LL for root */.}
ff190 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 2d  ;../*.** The in-
ff1a0 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20 6f 66 20  memory image of 
ff1b0 61 20 64 69 73 6b 20 70 61 67 65 20 68 61 73 20  a disk page has 
ff1c0 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 69 6e  the auxiliary in
ff1d0 66 6f 72 6d 61 74 69 6f 6e 20 61 70 70 65 6e 64  formation append
ff1e0 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6e 64  ed.** to the end
ff1f0 2e 20 20 45 58 54 52 41 5f 53 49 5a 45 20 69 73  .  EXTRA_SIZE is
ff200 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
ff210 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 6e 65  ytes of space ne
ff220 65 64 65 64 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20  eded to hold.** 
ff230 74 68 61 74 20 65 78 74 72 61 20 69 6e 66 6f 72  that extra infor
ff240 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69  mation..*/.#defi
ff250 6e 65 20 45 58 54 52 41 5f 53 49 5a 45 20 73 69  ne EXTRA_SIZE si
ff260 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 0a 0a 2f  zeof(MemPage)../
ff270 2a 20 41 20 42 74 72 65 65 20 68 61 6e 64 6c 65  * A Btree handle
ff280 0a 2a 2a 0a 2a 2a 20 41 20 64 61 74 61 62 61 73  .**.** A databas
ff290 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 6f 6e  e connection con
ff2a0 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  tains a pointer 
ff2b0 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  to an instance o
ff2c0 66 0a 2a 2a 20 74 68 69 73 20 6f 62 6a 65 63 74  f.** this object
ff2d0 20 66 6f 72 20 65 76 65 72 79 20 64 61 74 61 62   for every datab
ff2e0 61 73 65 20 66 69 6c 65 20 74 68 61 74 20 69 74  ase file that it
ff2f0 20 68 61 73 20 6f 70 65 6e 2e 20 20 54 68 69 73   has open.  This
ff300 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73   structure.** is
ff310 20 6f 70 61 71 75 65 20 74 6f 20 74 68 65 20 64   opaque to the d
ff320 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
ff330 6f 6e 2e 20 20 54 68 65 20 64 61 74 61 62 61 73  on.  The databas
ff340 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 61 6e  e connection can
ff350 6e 6f 74 0a 2a 2a 20 73 65 65 20 74 68 65 20 69  not.** see the i
ff360 6e 74 65 72 6e 61 6c 73 20 6f 66 20 74 68 69 73  nternals of this
ff370 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 6f   structure and o
ff380 6e 6c 79 20 64 65 61 6c 73 20 77 69 74 68 20 70  nly deals with p
ff390 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 74 68  ointers to.** th
ff3a0 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  is structure..**
ff3b0 0a 2a 2a 20 46 6f 72 20 73 6f 6d 65 20 64 61 74  .** For some dat
ff3c0 61 62 61 73 65 20 66 69 6c 65 73 2c 20 74 68 65  abase files, the
ff3d0 20 73 61 6d 65 20 75 6e 64 65 72 6c 79 69 6e 67   same underlying
ff3e0 20 64 61 74 61 62 61 73 65 20 63 61 63 68 65 20   database cache 
ff3f0 6d 69 67 68 74 20 62 65 20 0a 2a 2a 20 73 68 61  might be .** sha
ff400 72 65 64 20 62 65 74 77 65 65 6e 20 6d 75 6c 74  red between mult
ff410 69 70 6c 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  iple connections
ff420 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c  .  In that case,
ff430 20 65 61 63 68 20 63 6f 6e 74 65 63 74 69 6f 6e   each contection
ff440 0a 2a 2a 20 68 61 73 20 69 74 20 6f 77 6e 20 70  .** has it own p
ff450 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 6f  ointer to this o
ff460 62 6a 65 63 74 2e 20 20 42 75 74 20 65 61 63 68  bject.  But each
ff470 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
ff480 73 20 6f 62 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e  s object.** poin
ff490 74 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 42  ts to the same B
ff4a0 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 20  tShared object. 
ff4b0 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 61   The database ca
ff4c0 63 68 65 20 61 6e 64 20 74 68 65 0a 2a 2a 20 73  che and the.** s
ff4d0 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74 65 64  chema associated
ff4e0 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61   with the databa
ff4f0 73 65 20 66 69 6c 65 20 61 72 65 20 61 6c 6c 20  se file are all 
ff500 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e  contained within
ff510 0a 2a 2a 20 74 68 65 20 42 74 53 68 61 72 65 64  .** the BtShared
ff520 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41   object..**.** A
ff530 6c 6c 20 66 69 65 6c 64 73 20 69 6e 20 74 68 69  ll fields in thi
ff540 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20  s structure are 
ff550 61 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 73  accessed under s
ff560 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e 0a 2a 2a  qlite3.mutex..**
ff570 20 54 68 65 20 70 42 74 20 70 6f 69 6e 74 65 72   The pBt pointer
ff580 20 69 74 73 65 6c 66 20 6d 61 79 20 6e 6f 74 20   itself may not 
ff590 62 65 20 63 68 61 6e 67 65 64 20 77 68 69 6c 65  be changed while
ff5a0 20 74 68 65 72 65 20 65 78 69 73 74 73 20 63 75   there exists cu
ff5b0 72 73 6f 72 73 20 0a 2a 2a 20 69 6e 20 74 68 65  rsors .** in the
ff5c0 20 72 65 66 65 72 65 6e 63 65 64 20 42 74 53 68   referenced BtSh
ff5d0 61 72 65 64 20 74 68 61 74 20 70 6f 69 6e 74 20  ared that point 
ff5e0 62 61 63 6b 20 74 6f 20 74 68 69 73 20 42 74 72  back to this Btr
ff5f0 65 65 20 73 69 6e 63 65 20 74 68 6f 73 65 0a 2a  ee since those.*
ff600 2a 20 63 75 72 73 6f 72 73 20 68 61 76 65 20 74  * cursors have t
ff610 6f 20 64 6f 20 67 6f 20 74 68 72 6f 75 67 68 20  o do go through 
ff620 74 68 69 73 20 42 74 72 65 65 20 74 6f 20 66 69  this Btree to fi
ff630 6e 64 20 74 68 65 69 72 20 42 74 53 68 61 72 65  nd their BtShare
ff640 64 20 61 6e 64 0a 2a 2a 20 74 68 65 79 20 6f 66  d and.** they of
ff650 74 65 6e 20 64 6f 20 73 6f 20 77 69 74 68 6f 75  ten do so withou
ff660 74 20 68 6f 6c 64 69 6e 67 20 73 71 6c 69 74 65  t holding sqlite
ff670 33 2e 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 72 75  3.mutex..*/.stru
ff680 63 74 20 42 74 72 65 65 20 7b 0a 20 20 73 71 6c  ct Btree {.  sql
ff690 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
ff6a0 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
ff6b0 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 6f 6c 64 69  connection holdi
ff6c0 6e 67 20 74 68 69 73 20 62 74 72 65 65 20 2a 2f  ng this btree */
ff6d0 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
ff6e0 3b 20 20 20 20 20 2f 2a 20 53 68 61 72 61 62 6c  ;     /* Sharabl
ff6f0 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69  e content of thi
ff700 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 75 38 20  s btree */.  u8 
ff710 69 6e 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20  inTrans;        
ff720 2f 2a 20 54 52 41 4e 53 5f 4e 4f 4e 45 2c 20 54  /* TRANS_NONE, T
ff730 52 41 4e 53 5f 52 45 41 44 20 6f 72 20 54 52 41  RANS_READ or TRA
ff740 4e 53 5f 57 52 49 54 45 20 2a 2f 0a 20 20 75 38  NS_WRITE */.  u8
ff750 20 73 68 61 72 61 62 6c 65 3b 20 20 20 20 20 20   sharable;      
ff760 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 63   /* True if we c
ff770 61 6e 20 73 68 61 72 65 20 70 42 74 20 77 69 74  an share pBt wit
ff780 68 20 61 6e 6f 74 68 65 72 20 64 62 20 2a 2f 0a  h another db */.
ff790 20 20 75 38 20 6c 6f 63 6b 65 64 3b 20 20 20 20    u8 locked;    
ff7a0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
ff7b0 64 62 20 63 75 72 72 65 6e 74 6c 79 20 68 61 73  db currently has
ff7c0 20 70 42 74 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20   pBt locked */. 
ff7d0 20 69 6e 74 20 77 61 6e 74 54 6f 4c 6f 63 6b 3b   int wantToLock;
ff7e0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
ff7f0 20 6e 65 73 74 65 64 20 63 61 6c 6c 73 20 74 6f   nested calls to
ff800 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
ff810 65 72 28 29 20 2a 2f 0a 20 20 42 74 72 65 65 20  er() */.  Btree 
ff820 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20  *pNext;      /* 
ff830 4c 69 73 74 20 6f 66 20 6f 74 68 65 72 20 73 68  List of other sh
ff840 61 72 61 62 6c 65 20 42 74 72 65 65 73 20 66 72  arable Btrees fr
ff850 6f 6d 20 74 68 65 20 73 61 6d 65 20 64 62 20 2a  om the same db *
ff860 2f 0a 20 20 42 74 72 65 65 20 2a 70 50 72 65 76  /.  Btree *pPrev
ff870 3b 20 20 20 20 20 20 2f 2a 20 42 61 63 6b 20 70  ;      /* Back p
ff880 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 73 61  ointer of the sa
ff890 6d 65 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f  me list */.};../
ff8a0 2a 0a 2a 2a 20 42 74 72 65 65 2e 69 6e 54 72 61  *.** Btree.inTra
ff8b0 6e 73 20 6d 61 79 20 74 61 6b 65 20 6f 6e 65 20  ns may take one 
ff8c0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
ff8d0 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 49   values..**.** I
ff8e0 66 20 74 68 65 20 73 68 61 72 65 64 2d 64 61 74  f the shared-dat
ff8f0 61 20 65 78 74 65 6e 73 69 6f 6e 20 69 73 20 65  a extension is e
ff900 6e 61 62 6c 65 64 2c 20 74 68 65 72 65 20 6d 61  nabled, there ma
ff910 79 20 62 65 20 6d 75 6c 74 69 70 6c 65 20 75 73  y be multiple us
ff920 65 72 73 0a 2a 2a 20 6f 66 20 74 68 65 20 42 74  ers.** of the Bt
ff930 72 65 65 20 73 74 72 75 63 74 75 72 65 2e 20 41  ree structure. A
ff940 74 20 6d 6f 73 74 20 6f 6e 65 20 6f 66 20 74 68  t most one of th
ff950 65 73 65 20 6d 61 79 20 6f 70 65 6e 20 61 20 77  ese may open a w
ff960 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  rite transaction
ff970 2c 0a 2a 2a 20 62 75 74 20 61 6e 79 20 6e 75 6d  ,.** but any num
ff980 62 65 72 20 6d 61 79 20 68 61 76 65 20 61 63 74  ber may have act
ff990 69 76 65 20 72 65 61 64 20 74 72 61 6e 73 61 63  ive read transac
ff9a0 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  tions..*/.#defin
ff9b0 65 20 54 52 41 4e 53 5f 4e 4f 4e 45 20 20 30 0a  e TRANS_NONE  0.
ff9c0 23 64 65 66 69 6e 65 20 54 52 41 4e 53 5f 52 45  #define TRANS_RE
ff9d0 41 44 20 20 31 0a 23 64 65 66 69 6e 65 20 54 52  AD  1.#define TR
ff9e0 41 4e 53 5f 57 52 49 54 45 20 32 0a 0a 2f 2a 0a  ANS_WRITE 2../*.
ff9f0 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
ffa00 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65  f this object re
ffa10 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c  presents a singl
ffa20 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
ffa30 0a 2a 2a 20 0a 2a 2a 20 41 20 73 69 6e 67 6c 65  .** .** A single
ffa40 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 63   database file c
ffa50 61 6e 20 62 65 20 69 6e 20 75 73 65 20 61 73 20  an be in use as 
ffa60 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 62 79  the same time by
ffa70 20 74 77 6f 0a 2a 2a 20 6f 72 20 6d 6f 72 65 20   two.** or more 
ffa80 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
ffa90 69 6f 6e 73 2e 20 20 57 68 65 6e 20 74 77 6f 20  ions.  When two 
ffaa0 6f 72 20 6d 6f 72 65 20 63 6f 6e 6e 65 63 74 69  or more connecti
ffab0 6f 6e 73 20 61 72 65 0a 2a 2a 20 73 68 61 72 69  ons are.** shari
ffac0 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  ng the same data
ffad0 62 61 73 65 20 66 69 6c 65 2c 20 65 61 63 68 20  base file, each 
ffae0 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20 69  connection has i
ffaf0 74 20 6f 77 6e 0a 2a 2a 20 70 72 69 76 61 74 65  t own.** private
ffb00 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 66 6f   Btree object fo
ffb10 72 20 74 68 65 20 66 69 6c 65 20 61 6e 64 20 65  r the file and e
ffb20 61 63 68 20 6f 66 20 74 68 6f 73 65 20 42 74 72  ach of those Btr
ffb30 65 65 73 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f  ees points.** to
ffb40 20 74 68 69 73 20 6f 6e 65 20 42 74 53 68 61 72   this one BtShar
ffb50 65 64 20 6f 62 6a 65 63 74 2e 20 20 42 74 53 68  ed object.  BtSh
ffb60 61 72 65 64 2e 6e 52 65 66 20 69 73 20 74 68 65  ared.nRef is the
ffb70 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 6f   number of.** co
ffb80 6e 6e 65 63 74 69 6f 6e 73 20 63 75 72 72 65 6e  nnections curren
ffb90 74 6c 79 20 73 68 61 72 69 6e 67 20 74 68 69 73  tly sharing this
ffba0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
ffbb0 2a 2a 0a 2a 2a 20 46 69 65 6c 64 73 20 69 6e 20  **.** Fields in 
ffbc0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61  this structure a
ffbd0 72 65 20 61 63 63 65 73 73 65 64 20 75 6e 64 65  re accessed unde
ffbe0 72 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 6d  r the BtShared.m
ffbf0 75 74 65 78 0a 2a 2a 20 6d 75 74 65 78 2c 20 65  utex.** mutex, e
ffc00 78 63 65 70 74 20 66 6f 72 20 6e 52 65 66 20 61  xcept for nRef a
ffc10 6e 64 20 70 4e 65 78 74 20 77 68 69 63 68 20 61  nd pNext which a
ffc20 72 65 20 61 63 63 65 73 73 65 64 20 75 6e 64 65  re accessed unde
ffc30 72 20 74 68 65 0a 2a 2a 20 67 6c 6f 62 61 6c 20  r the.** global 
ffc40 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
ffc50 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74 65 78  TIC_MASTER mutex
ffc60 2e 20 20 54 68 65 20 70 50 61 67 65 72 20 66 69  .  The pPager fi
ffc70 65 6c 64 0a 2a 2a 20 6d 61 79 20 6e 6f 74 20 62  eld.** may not b
ffc80 65 20 6d 6f 64 69 66 69 65 64 20 6f 6e 63 65 20  e modified once 
ffc90 69 74 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20  it is initially 
ffca0 73 65 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 6e  set as long as n
ffcb0 52 65 66 3e 30 2e 0a 2a 2a 20 54 68 65 20 70 53  Ref>0..** The pS
ffcc0 63 68 65 6d 61 20 66 69 65 6c 64 20 6d 61 79 20  chema field may 
ffcd0 62 65 20 73 65 74 20 6f 6e 63 65 20 75 6e 64 65  be set once unde
ffce0 72 20 42 74 53 68 61 72 65 64 2e 6d 75 74 65 78  r BtShared.mutex
ffcf0 20 61 6e 64 0a 2a 2a 20 74 68 65 72 65 61 66 74   and.** thereaft
ffd00 65 72 20 69 73 20 75 6e 63 68 61 6e 67 65 64 20  er is unchanged 
ffd10 61 73 20 6c 6f 6e 67 20 61 73 20 6e 52 65 66 3e  as long as nRef>
ffd20 30 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 53  0..*/.struct BtS
ffd30 68 61 72 65 64 20 7b 0a 20 20 50 61 67 65 72 20  hared {.  Pager 
ffd40 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20 20 20  *pPager;        
ffd50 2f 2a 20 54 68 65 20 70 61 67 65 20 63 61 63 68  /* The page cach
ffd60 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  e */.  sqlite3 *
ffd70 64 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  db;          /* 
ffd80 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  Database connect
ffd90 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 73  ion currently us
ffda0 69 6e 67 20 74 68 69 73 20 42 74 72 65 65 20 2a  ing this Btree *
ffdb0 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  /.  BtCursor *pC
ffdc0 75 72 73 6f 72 3b 20 20 20 20 2f 2a 20 41 20 6c  ursor;    /* A l
ffdd0 69 73 74 20 6f 66 20 61 6c 6c 20 6f 70 65 6e 20  ist of all open 
ffde0 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 4d 65 6d  cursors */.  Mem
ffdf0 50 61 67 65 20 2a 70 50 61 67 65 31 3b 20 20 20  Page *pPage1;   
ffe00 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 67 65     /* First page
ffe10 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
ffe20 20 2a 2f 0a 20 20 75 38 20 69 6e 53 74 6d 74 3b   */.  u8 inStmt;
ffe30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
ffe40 72 75 65 20 69 66 20 77 65 20 61 72 65 20 69 6e  rue if we are in
ffe50 20 61 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62   a statement sub
ffe60 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20  transaction */. 
ffe70 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20   u8 readOnly;   
ffe80 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
ffe90 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  f the underlying
ffea0 20 66 69 6c 65 20 69 73 20 72 65 61 64 6f 6e 6c   file is readonl
ffeb0 79 20 2a 2f 0a 20 20 75 38 20 6d 61 78 45 6d 62  y */.  u8 maxEmb
ffec0 65 64 46 72 61 63 3b 20 20 20 20 20 20 2f 2a 20  edFrac;      /* 
ffed0 4d 61 78 69 6d 75 6d 20 70 61 79 6c 6f 61 64 20  Maximum payload 
ffee0 61 73 20 25 20 6f 66 20 74 6f 74 61 6c 20 70 61  as % of total pa
ffef0 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 75 38 20  ge size */.  u8 
fff00 6d 69 6e 45 6d 62 65 64 46 72 61 63 3b 20 20 20  minEmbedFrac;   
fff10 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 70 61     /* Minimum pa
fff20 79 6c 6f 61 64 20 61 73 20 25 20 6f 66 20 74 6f  yload as % of to
fff30 74 61 6c 20 70 61 67 65 20 73 69 7a 65 20 2a 2f  tal page size */
fff40 0a 20 20 75 38 20 6d 69 6e 4c 65 61 66 46 72 61  .  u8 minLeafFra
fff50 63 3b 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69  c;       /* Mini
fff60 6d 75 6d 20 6c 65 61 66 20 70 61 79 6c 6f 61 64  mum leaf payload
fff70 20 61 73 20 25 20 6f 66 20 74 6f 74 61 6c 20 70   as % of total p
fff80 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 75 38  age size */.  u8
fff90 20 70 61 67 65 53 69 7a 65 46 69 78 65 64 3b 20   pageSizeFixed; 
fffa0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
fffb0 68 65 20 70 61 67 65 20 73 69 7a 65 20 63 61 6e  he page size can
fffc0 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 20 63 68   no longer be ch
fffd0 61 6e 67 65 64 20 2a 2f 0a 23 69 66 6e 64 65 66  anged */.#ifndef
fffe0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
ffff0 4f 56 41 43 55 55 4d 0a 20 20 75 38 20 61 75 74  OVACUUM.  u8 aut
10000 6f 56 61 63 75 75 6d 3b 20 20 20 20 20 20 20 20  oVacuum;        
10001 2f 2a 20 54 72 75 65 20 69 66 20 61 75 74 6f 2d  /* True if auto-
10002 76 61 63 75 75 6d 20 69 73 20 65 6e 61 62 6c 65  vacuum is enable
10003 64 20 2a 2f 0a 20 20 75 38 20 69 6e 63 72 56 61  d */.  u8 incrVa
10004 63 75 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20  cuum;        /* 
10005 54 72 75 65 20 69 66 20 69 6e 63 72 2d 76 61 63  True if incr-vac
10006 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 2a  uum is enabled *
10007 2f 0a 20 20 50 67 6e 6f 20 6e 54 72 75 6e 63 3b  /.  Pgno nTrunc;
10008 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 6e            /* Non
10009 2d 7a 65 72 6f 20 69 66 20 74 68 65 20 64 62 20  -zero if the db 
1000a 77 69 6c 6c 20 62 65 20 74 72 75 6e 63 61 74 65  will be truncate
1000b 64 20 28 69 6e 63 72 20 76 61 63 75 75 6d 29 20  d (incr vacuum) 
1000c 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75 31 36 20  */.#endif.  u16 
1000d 70 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20 20  pageSize;       
1000e 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65    /* Total numbe
1000f 72 20 6f 66 20 62 79 74 65 73 20 6f 6e 20 61 20  r of bytes on a 
10010 70 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 75 73  page */.  u16 us
10011 61 62 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 20  ableSize;       
10012 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 61  /* Number of usa
10013 62 6c 65 20 62 79 74 65 73 20 6f 6e 20 65 61 63  ble bytes on eac
10014 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  h page */.  int 
10015 6d 61 78 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20  maxLocal;       
10016 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 6f 63    /* Maximum loc
10017 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 6e 6f  al payload in no
10018 6e 2d 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65  n-LEAFDATA table
10019 73 20 2a 2f 0a 20 20 69 6e 74 20 6d 69 6e 4c 6f  s */.  int minLo
1001a 63 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  cal;         /* 
1001b 4d 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61  Minimum local pa
1001c 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41  yload in non-LEA
1001d 46 44 41 54 41 20 74 61 62 6c 65 73 20 2a 2f 0a  FDATA tables */.
1001e 20 20 69 6e 74 20 6d 61 78 4c 65 61 66 3b 20 20    int maxLeaf;  
1001f 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d          /* Maxim
10020 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64  um local payload
10021 20 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74   in a LEAFDATA t
10022 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 69  able */.  int mi
10023 6e 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20  nLeaf;          
10024 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c  /* Minimum local
10025 20 70 61 79 6c 6f 61 64 20 69 6e 20 61 20 4c 45   payload in a LE
10026 41 46 44 41 54 41 20 74 61 62 6c 65 20 2a 2f 0a  AFDATA table */.
10027 20 20 75 38 20 69 6e 54 72 61 6e 73 61 63 74 69    u8 inTransacti
10028 6f 6e 3b 20 20 20 20 20 2f 2a 20 54 72 61 6e 73  on;     /* Trans
10029 61 63 74 69 6f 6e 20 73 74 61 74 65 20 2a 2f 0a  action state */.
1002a 20 20 69 6e 74 20 6e 54 72 61 6e 73 61 63 74 69    int nTransacti
1002b 6f 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  on;     /* Numbe
1002c 72 20 6f 66 20 6f 70 65 6e 20 74 72 61 6e 73 61  r of open transa
1002d 63 74 69 6f 6e 73 20 28 72 65 61 64 20 2b 20 77  ctions (read + w
1002e 72 69 74 65 29 20 2a 2f 0a 20 20 76 6f 69 64 20  rite) */.  void 
1002f 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 20 20 20  *pSchema;       
10030 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 73   /* Pointer to s
10031 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 62  pace allocated b
10032 79 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63  y sqlite3BtreeSc
10033 68 65 6d 61 28 29 20 2a 2f 0a 20 20 76 6f 69 64  hema() */.  void
10034 20 28 2a 78 46 72 65 65 53 63 68 65 6d 61 29 28   (*xFreeSchema)(
10035 76 6f 69 64 2a 29 3b 20 20 2f 2a 20 44 65 73 74  void*);  /* Dest
10036 72 75 63 74 6f 72 20 66 6f 72 20 42 74 53 68 61  ructor for BtSha
10037 72 65 64 2e 70 53 63 68 65 6d 61 20 2a 2f 0a 20  red.pSchema */. 
10038 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
10039 6d 75 74 65 78 3b 20 2f 2a 20 4e 6f 6e 2d 72 65  mutex; /* Non-re
1003a 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 72 65  cursive mutex re
1003b 71 75 69 72 65 64 20 74 6f 20 61 63 63 65 73 73  quired to access
1003c 20 74 68 69 73 20 73 74 72 75 63 74 20 2a 2f 0a   this struct */.
1003d 20 20 42 75 73 79 48 61 6e 64 6c 65 72 20 62 75    BusyHandler bu
1003e 73 79 48 64 72 3b 20 20 2f 2a 20 54 68 65 20 62  syHdr;  /* The b
1003f 75 73 79 20 68 61 6e 64 6c 65 72 20 66 6f 72 20  usy handler for 
10040 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 23 69  this btree */.#i
10041 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
10042 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20  T_SHARED_CACHE. 
10043 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20   int nRef;      
10044 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
10045 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74   of references t
10046 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  o this structure
10047 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a   */.  BtShared *
10048 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e  pNext;      /* N
10049 65 78 74 20 6f 6e 20 61 20 6c 69 73 74 20 6f 66  ext on a list of
1004a 20 73 68 61 72 61 62 6c 65 20 42 74 53 68 61 72   sharable BtShar
1004b 65 64 20 73 74 72 75 63 74 73 20 2a 2f 0a 20 20  ed structs */.  
1004c 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 20 20  BtLock *pLock;  
1004d 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66        /* List of
1004e 20 6c 6f 63 6b 73 20 68 65 6c 64 20 6f 6e 20 74   locks held on t
1004f 68 69 73 20 73 68 61 72 65 64 2d 62 74 72 65 65  his shared-btree
10050 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 42 74 72   struct */.  Btr
10051 65 65 20 2a 70 45 78 63 6c 75 73 69 76 65 3b 20  ee *pExclusive; 
10052 20 20 20 2f 2a 20 42 74 72 65 65 20 77 69 74 68     /* Btree with
10053 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   an EXCLUSIVE lo
10054 63 6b 20 6f 6e 20 74 68 65 20 77 68 6f 6c 65 20  ck on the whole 
10055 64 62 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75  db */.#endif.  u
10056 38 20 2a 70 54 6d 70 53 70 61 63 65 3b 20 20 20  8 *pTmpSpace;   
10057 20 20 20 20 20 2f 2a 20 42 74 53 68 61 72 65 64       /* BtShared
10058 2e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 20  .pageSize bytes 
10059 6f 66 20 73 70 61 63 65 20 66 6f 72 20 74 6d 70  of space for tmp
1005a 20 75 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a   use */.};../*.*
1005b 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
1005c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
1005d 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64  tructure is used
1005e 20 74 6f 20 68 6f 6c 64 20 69 6e 66 6f 72 6d 61   to hold informa
1005f 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 61 20  tion.** about a 
10060 63 65 6c 6c 2e 20 20 54 68 65 20 70 61 72 73 65  cell.  The parse
10061 43 65 6c 6c 50 74 72 28 29 20 66 75 6e 63 74 69  CellPtr() functi
10062 6f 6e 20 66 69 6c 6c 73 20 69 6e 20 74 68 69 73  on fills in this
10063 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 62 61   structure.** ba
10064 73 65 64 20 6f 6e 20 69 6e 66 6f 72 6d 61 74 69  sed on informati
10065 6f 6e 20 65 78 74 72 61 63 74 20 66 72 6f 6d 20  on extract from 
10066 74 68 65 20 72 61 77 20 64 69 73 6b 20 70 61 67  the raw disk pag
10067 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  e..*/.typedef st
10068 72 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20 43 65  ruct CellInfo Ce
10069 6c 6c 49 6e 66 6f 3b 0a 73 74 72 75 63 74 20 43  llInfo;.struct C
1006a 65 6c 6c 49 6e 66 6f 20 7b 0a 20 20 75 38 20 2a  ellInfo {.  u8 *
1006b 70 43 65 6c 6c 3b 20 20 20 20 20 2f 2a 20 50 6f  pCell;     /* Po
1006c 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73 74 61  inter to the sta
1006d 72 74 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 65  rt of cell conte
1006e 6e 74 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 79  nt */.  i64 nKey
1006f 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6b 65  ;      /* The ke
10070 79 20 66 6f 72 20 49 4e 54 4b 45 59 20 74 61 62  y for INTKEY tab
10071 6c 65 73 2c 20 6f 72 20 6e 75 6d 62 65 72 20 6f  les, or number o
10072 66 20 62 79 74 65 73 20 69 6e 20 6b 65 79 20 2a  f bytes in key *
10073 2f 0a 20 20 75 33 32 20 6e 44 61 74 61 3b 20 20  /.  u32 nData;  
10074 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
10075 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 2a 2f  bytes of data */
10076 0a 20 20 75 33 32 20 6e 50 61 79 6c 6f 61 64 3b  .  u32 nPayload;
10077 20 20 2f 2a 20 54 6f 74 61 6c 20 61 6d 6f 75 6e    /* Total amoun
10078 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 2a 2f 0a  t of payload */.
10079 20 20 75 31 36 20 6e 48 65 61 64 65 72 3b 20 20    u16 nHeader;  
1007a 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20   /* Size of the 
1007b 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65 61  cell content hea
1007c 64 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  der in bytes */.
1007d 20 20 75 31 36 20 6e 4c 6f 63 61 6c 3b 20 20 20    u16 nLocal;   
1007e 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 70 61   /* Amount of pa
1007f 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c  yload held local
10080 6c 79 20 2a 2f 0a 20 20 75 31 36 20 69 4f 76 65  ly */.  u16 iOve
10081 72 66 6c 6f 77 3b 20 2f 2a 20 4f 66 66 73 65 74  rflow; /* Offset
10082 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   to overflow pag
10083 65 20 6e 75 6d 62 65 72 2e 20 20 5a 65 72 6f 20  e number.  Zero 
10084 69 66 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 20 2a  if no overflow *
10085 2f 0a 20 20 75 31 36 20 6e 53 69 7a 65 3b 20 20  /.  u16 nSize;  
10086 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68     /* Size of th
10087 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 6f  e cell content o
10088 6e 20 74 68 65 20 6d 61 69 6e 20 62 2d 74 72 65  n the main b-tre
10089 65 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  e page */.};../*
1008a 0a 2a 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20  .** A cursor is 
1008b 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70  a pointer to a p
1008c 61 72 74 69 63 75 6c 61 72 20 65 6e 74 72 79 20  articular entry 
1008d 77 69 74 68 69 6e 20 61 20 70 61 72 74 69 63 75  within a particu
1008e 6c 61 72 0a 2a 2a 20 62 2d 74 72 65 65 20 77 69  lar.** b-tree wi
1008f 74 68 69 6e 20 61 20 64 61 74 61 62 61 73 65 20  thin a database 
10090 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  file..**.** The 
10091 65 6e 74 72 79 20 69 73 20 69 64 65 6e 74 69 66  entry is identif
10092 69 65 64 20 62 79 20 69 74 73 20 4d 65 6d 50 61  ied by its MemPa
10093 67 65 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78  ge and the index
10094 20 69 6e 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61   in.** MemPage.a
10095 43 65 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 65 6e  Cell[] of the en
10096 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  try..**.** When 
10097 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73  a single databas
10098 65 20 66 69 6c 65 20 63 61 6e 20 73 68 61 72 65  e file can share
10099 64 20 62 79 20 74 77 6f 20 6d 6f 72 65 20 64 61  d by two more da
1009a 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1009b 6e 73 2c 0a 2a 2a 20 62 75 74 20 63 75 72 73 6f  ns,.** but curso
1009c 72 73 20 63 61 6e 6e 6f 74 20 62 65 20 73 68 61  rs cannot be sha
1009d 72 65 64 2e 20 20 45 61 63 68 20 63 75 72 73 6f  red.  Each curso
1009e 72 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20  r is associated 
1009f 77 69 74 68 20 61 0a 2a 2a 20 70 61 72 74 69 63  with a.** partic
100a0 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f  ular database co
100a1 6e 6e 65 63 74 69 6f 6e 20 69 64 65 6e 74 69 66  nnection identif
100a2 69 65 64 20 42 74 43 75 72 73 6f 72 2e 70 42 74  ied BtCursor.pBt
100a3 72 65 65 2e 64 62 2e 0a 2a 2a 0a 2a 2a 20 46 69  ree.db..**.** Fi
100a4 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74 72  elds in this str
100a5 75 63 74 75 72 65 20 61 72 65 20 61 63 63 65 73  ucture are acces
100a6 73 65 64 20 75 6e 64 65 72 20 74 68 65 20 42 74  sed under the Bt
100a7 53 68 61 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20  Shared.mutex.** 
100a8 66 6f 75 6e 64 20 61 74 20 73 65 6c 66 2d 3e 70  found at self->p
100a9 42 74 2d 3e 6d 75 74 65 78 2e 20 0a 2a 2f 0a 73  Bt->mutex. .*/.s
100aa 74 72 75 63 74 20 42 74 43 75 72 73 6f 72 20 7b  truct BtCursor {
100ab 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65  .  Btree *pBtree
100ac 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
100ad 54 68 65 20 42 74 72 65 65 20 74 6f 20 77 68 69  The Btree to whi
100ae 63 68 20 74 68 69 73 20 63 75 72 73 6f 72 20 62  ch this cursor b
100af 65 6c 6f 6e 67 73 20 2a 2f 0a 20 20 42 74 53 68  elongs */.  BtSh
100b0 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20  ared *pBt;      
100b1 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 74 53        /* The BtS
100b2 68 61 72 65 64 20 74 68 69 73 20 63 75 72 73 6f  hared this curso
100b3 72 20 70 6f 69 6e 74 73 20 74 6f 20 2a 2f 0a 20  r points to */. 
100b4 20 42 74 43 75 72 73 6f 72 20 2a 70 4e 65 78 74   BtCursor *pNext
100b5 2c 20 2a 70 50 72 65 76 3b 20 20 2f 2a 20 46 6f  , *pPrev;  /* Fo
100b6 72 6d 73 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73  rms a linked lis
100b7 74 20 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72 73  t of all cursors
100b8 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79   */.  struct Key
100b9 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20  Info *pKeyInfo; 
100ba 2f 2a 20 41 72 67 75 6d 65 6e 74 20 70 61 73 73  /* Argument pass
100bb 65 64 20 74 6f 20 63 6f 6d 70 61 72 69 73 6f 6e  ed to comparison
100bc 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 50   function */.  P
100bd 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 20 20 20  gno pgnoRoot;   
100be 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
100bf 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 69  root page of thi
100c0 73 20 74 72 65 65 20 2a 2f 0a 20 20 4d 65 6d 50  s tree */.  MemP
100c1 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20 20  age *pPage;     
100c2 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 74 68        /* Page th
100c3 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
100c4 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69  entry */.  int i
100c5 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dx;             
100c6 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
100c7 20 74 68 65 20 65 6e 74 72 79 20 69 6e 20 70 50   the entry in pP
100c8 61 67 65 2d 3e 61 43 65 6c 6c 5b 5d 20 2a 2f 0a  age->aCell[] */.
100c9 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b    CellInfo info;
100ca 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
100cb 20 70 61 72 73 65 20 6f 66 20 74 68 65 20 63 65   parse of the ce
100cc 6c 6c 20 77 65 20 61 72 65 20 70 6f 69 6e 74 69  ll we are pointi
100cd 6e 67 20 61 74 20 2a 2f 0a 20 20 75 38 20 77 72  ng at */.  u8 wr
100ce 46 6c 61 67 3b 20 20 20 20 20 20 20 20 20 20 20  Flag;           
100cf 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
100d0 77 72 69 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38  writable */.  u8
100d1 20 61 74 4c 61 73 74 3b 20 20 20 20 20 20 20 20   atLast;        
100d2 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f          /* Curso
100d3 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68  r pointing to th
100d4 65 20 6c 61 73 74 20 65 6e 74 72 79 20 2a 2f 0a  e last entry */.
100d5 20 20 75 38 20 76 61 6c 69 64 4e 4b 65 79 3b 20    u8 validNKey; 
100d6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
100d7 72 75 65 20 69 66 20 69 6e 66 6f 2e 6e 4b 65 79  rue if info.nKey
100d8 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 75   is valid */.  u
100d9 38 20 65 53 74 61 74 65 3b 20 20 20 20 20 20 20  8 eState;       
100da 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
100db 6f 66 20 74 68 65 20 43 55 52 53 4f 52 5f 58 58  of the CURSOR_XX
100dc 58 20 63 6f 6e 73 74 61 6e 74 73 20 28 73 65 65  X constants (see
100dd 20 62 65 6c 6f 77 29 20 2a 2f 0a 20 20 76 6f 69   below) */.  voi
100de 64 20 2a 70 4b 65 79 3b 20 20 20 20 20 20 2f 2a  d *pKey;      /*
100df 20 53 61 76 65 64 20 6b 65 79 20 74 68 61 74 20   Saved key that 
100e0 77 61 73 20 63 75 72 73 6f 72 27 73 20 6c 61 73  was cursor's las
100e1 74 20 6b 6e 6f 77 6e 20 70 6f 73 69 74 69 6f 6e  t known position
100e2 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 79 3b 20   */.  i64 nKey; 
100e3 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
100e4 66 20 70 4b 65 79 2c 20 6f 72 20 6c 61 73 74 20  f pKey, or last 
100e5 69 6e 74 65 67 65 72 20 6b 65 79 20 2a 2f 0a 20  integer key */. 
100e6 20 69 6e 74 20 73 6b 69 70 3b 20 20 20 20 20 20   int skip;      
100e7 20 20 2f 2a 20 28 73 6b 69 70 3c 30 29 20 2d 3e    /* (skip<0) ->
100e8 20 50 72 65 76 28 29 20 69 73 20 61 20 6e 6f 2d   Prev() is a no-
100e9 6f 70 2e 20 28 73 6b 69 70 3e 30 29 20 2d 3e 20  op. (skip>0) -> 
100ea 4e 65 78 74 28 29 20 69 73 20 2a 2f 0a 23 69 66  Next() is */.#if
100eb 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
100ec 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 75 38 20 69  _INCRBLOB.  u8 i
100ed 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 3b  sIncrblobHandle;
100ee 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
100ef 20 74 68 69 73 20 63 75 72 73 6f 72 20 69 73 20   this cursor is 
100f0 61 6e 20 69 6e 63 72 2e 20 69 6f 20 68 61 6e 64  an incr. io hand
100f1 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 2a 61 4f  le */.  Pgno *aO
100f2 76 65 72 66 6c 6f 77 3b 20 20 20 20 20 20 20 20  verflow;        
100f3 20 20 2f 2a 20 43 61 63 68 65 20 6f 66 20 6f 76    /* Cache of ov
100f4 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 6f 63 61  erflow page loca
100f5 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e 64 69 66 0a  tions */.#endif.
100f6 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 6f 74 65 6e 74  };../*.** Potent
100f7 69 61 6c 20 76 61 6c 75 65 73 20 66 6f 72 20 42  ial values for B
100f8 74 43 75 72 73 6f 72 2e 65 53 74 61 74 65 2e 0a  tCursor.eState..
100f9 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 56 41 4c  **.** CURSOR_VAL
100fa 49 44 3a 0a 2a 2a 20 20 20 43 75 72 73 6f 72 20  ID:.**   Cursor 
100fb 70 6f 69 6e 74 73 20 74 6f 20 61 20 76 61 6c 69  points to a vali
100fc 64 20 65 6e 74 72 79 2e 20 67 65 74 50 61 79 6c  d entry. getPayl
100fd 6f 61 64 28 29 20 65 74 63 2e 20 6d 61 79 20 62  oad() etc. may b
100fe 65 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20  e called..**.** 
100ff 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3a 0a  CURSOR_INVALID:.
10100 2a 2a 20 20 20 43 75 72 73 6f 72 20 64 6f 65 73  **   Cursor does
10101 20 6e 6f 74 20 70 6f 69 6e 74 20 74 6f 20 61 20   not point to a 
10102 76 61 6c 69 64 20 65 6e 74 72 79 2e 20 54 68 69  valid entry. Thi
10103 73 20 63 61 6e 20 68 61 70 70 65 6e 20 28 66 6f  s can happen (fo
10104 72 20 65 78 61 6d 70 6c 65 29 20 0a 2a 2a 20 20  r example) .**  
10105 20 62 65 63 61 75 73 65 20 74 68 65 20 74 61 62   because the tab
10106 6c 65 20 69 73 20 65 6d 70 74 79 20 6f 72 20 62  le is empty or b
10107 65 63 61 75 73 65 20 42 74 72 65 65 43 75 72 73  ecause BtreeCurs
10108 6f 72 46 69 72 73 74 28 29 20 68 61 73 20 6e 6f  orFirst() has no
10109 74 20 62 65 65 6e 0a 2a 2a 20 20 20 63 61 6c 6c  t been.**   call
1010a 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52  ed..**.** CURSOR
1010b 5f 52 45 51 55 49 52 45 53 45 45 4b 3a 0a 2a 2a  _REQUIRESEEK:.**
1010c 20 20 20 54 68 65 20 74 61 62 6c 65 20 74 68 61     The table tha
1010d 74 20 74 68 69 73 20 63 75 72 73 6f 72 20 77 61  t this cursor wa
1010e 73 20 6f 70 65 6e 65 64 20 6f 6e 20 73 74 69 6c  s opened on stil
1010f 6c 20 65 78 69 73 74 73 2c 20 62 75 74 20 68 61  l exists, but ha
10110 73 20 62 65 65 6e 20 0a 2a 2a 20 20 20 6d 6f 64  s been .**   mod
10111 69 66 69 65 64 20 73 69 6e 63 65 20 74 68 65 20  ified since the 
10112 63 75 72 73 6f 72 20 77 61 73 20 6c 61 73 74 20  cursor was last 
10113 75 73 65 64 2e 20 54 68 65 20 63 75 72 73 6f 72  used. The cursor
10114 20 70 6f 73 69 74 69 6f 6e 20 69 73 20 73 61 76   position is sav
10115 65 64 0a 2a 2a 20 20 20 69 6e 20 76 61 72 69 61  ed.**   in varia
10116 62 6c 65 73 20 42 74 43 75 72 73 6f 72 2e 70 4b  bles BtCursor.pK
10117 65 79 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e  ey and BtCursor.
10118 6e 4b 65 79 2e 20 57 68 65 6e 20 61 20 63 75 72  nKey. When a cur
10119 73 6f 72 20 69 73 20 69 6e 20 0a 2a 2a 20 20 20  sor is in .**   
1011a 74 68 69 73 20 73 74 61 74 65 2c 20 72 65 73 74  this state, rest
1011b 6f 72 65 4f 72 43 6c 65 61 72 43 75 72 73 6f 72  oreOrClearCursor
1011c 50 6f 73 69 74 69 6f 6e 28 29 20 63 61 6e 20 62  Position() can b
1011d 65 20 63 61 6c 6c 65 64 20 74 6f 20 61 74 74 65  e called to atte
1011e 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 73 65 65 6b  mpt to.**   seek
1011f 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74   the cursor to t
10120 68 65 20 73 61 76 65 64 20 70 6f 73 69 74 69 6f  he saved positio
10121 6e 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f  n..**.** CURSOR_
10122 46 41 55 4c 54 3a 0a 2a 2a 20 20 20 41 20 75 6e  FAULT:.**   A un
10123 72 65 63 6f 76 65 72 61 62 6c 65 20 65 72 72 6f  recoverable erro
10124 72 20 28 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20  r (an I/O error 
10125 6f 72 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  or a malloc fail
10126 75 72 65 29 20 68 61 73 20 6f 63 63 75 72 72 65  ure) has occurre
10127 64 0a 2a 2a 20 20 20 6f 6e 20 61 20 64 69 66 66  d.**   on a diff
10128 65 72 65 6e 74 20 63 6f 6e 6e 65 63 74 69 6f 6e  erent connection
10129 20 74 68 61 74 20 73 68 61 72 65 73 20 74 68 65   that shares the
1012a 20 42 74 53 68 61 72 65 64 20 63 61 63 68 65 20   BtShared cache 
1012b 77 69 74 68 20 74 68 69 73 0a 2a 2a 20 20 20 63  with this.**   c
1012c 75 72 73 6f 72 2e 20 20 54 68 65 20 65 72 72 6f  ursor.  The erro
1012d 72 20 68 61 73 20 6c 65 66 74 20 74 68 65 20 63  r has left the c
1012e 61 63 68 65 20 69 6e 20 61 6e 20 69 6e 63 6f 6e  ache in an incon
1012f 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0a 2a  sistent state..*
10130 2a 20 20 20 44 6f 20 6e 6f 74 68 69 6e 67 20 65  *   Do nothing e
10131 6c 73 65 20 77 69 74 68 20 74 68 69 73 20 63 75  lse with this cu
10132 72 73 6f 72 2e 20 20 41 6e 79 20 61 74 74 65 6d  rsor.  Any attem
10133 70 74 20 74 6f 20 75 73 65 20 74 68 65 20 63 75  pt to use the cu
10134 72 73 6f 72 0a 2a 2a 20 20 20 73 68 6f 75 6c 64  rsor.**   should
10135 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f   return the erro
10136 72 20 63 6f 64 65 20 73 74 6f 72 65 64 20 69 6e  r code stored in
10137 20 42 74 43 75 72 73 6f 72 2e 73 6b 69 70 0a 2a   BtCursor.skip.*
10138 2f 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52  /.#define CURSOR
10139 5f 49 4e 56 41 4c 49 44 20 20 20 20 20 20 20 20  _INVALID        
1013a 20 20 20 30 0a 23 64 65 66 69 6e 65 20 43 55 52     0.#define CUR
1013b 53 4f 52 5f 56 41 4c 49 44 20 20 20 20 20 20 20  SOR_VALID       
1013c 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20        1.#define 
1013d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45  CURSOR_REQUIRESE
1013e 45 4b 20 20 20 20 20 20 20 32 0a 23 64 65 66 69  EK       2.#defi
1013f 6e 65 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20  ne CURSOR_FAULT 
10140 20 20 20 20 20 20 20 20 20 20 20 20 33 0a 0a 2f              3../
10141 2a 0a 2a 2a 20 54 68 65 20 54 52 41 43 45 20 6d  *.** The TRACE m
10142 61 63 72 6f 20 77 69 6c 6c 20 70 72 69 6e 74 20  acro will print 
10143 68 69 67 68 2d 6c 65 76 65 6c 20 73 74 61 74 75  high-level statu
10144 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  s information ab
10145 6f 75 74 20 74 68 65 0a 2a 2a 20 62 74 72 65 65  out the.** btree
10146 20 6f 70 65 72 61 74 69 6f 6e 20 77 68 65 6e 20   operation when 
10147 74 68 65 20 67 6c 6f 62 61 6c 20 76 61 72 69 61  the global varia
10148 62 6c 65 20 73 71 6c 69 74 65 33 42 74 72 65 65  ble sqlite3Btree
10149 54 72 61 63 65 20 69 73 0a 2a 2a 20 65 6e 61 62  Trace is.** enab
1014a 6c 65 64 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49  led..*/.#if SQLI
1014b 54 45 5f 54 45 53 54 0a 23 20 64 65 66 69 6e 65  TE_TEST.# define
1014c 20 54 52 41 43 45 28 58 29 20 20 20 69 66 28 20   TRACE(X)   if( 
1014d 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 61 63  sqlite3BtreeTrac
1014e 65 20 29 7b 20 70 72 69 6e 74 66 20 58 3b 20 66  e ){ printf X; f
1014f 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b 20 7d  flush(stdout); }
10150 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
10151 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69 66 0a  TRACE(X).#endif.
10152 0a 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65  ./* The database
10153 20 70 61 67 65 20 74 68 65 20 50 45 4e 44 49 4e   page the PENDIN
10154 47 5f 42 59 54 45 20 6f 63 63 75 70 69 65 73 2e  G_BYTE occupies.
10155 20 54 68 69 73 20 70 61 67 65 20 69 73 20 6e 65   This page is ne
10156 76 65 72 20 75 73 65 64 2e 0a 2a 2a 20 54 4f 44  ver used..** TOD
10157 4f 3a 20 54 68 69 73 20 6d 61 63 72 6f 20 69 73  O: This macro is
10158 20 76 65 72 79 20 73 69 6d 69 6c 61 72 79 20 74   very similary t
10159 6f 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28  o PAGER_MJ_PGNO(
1015a 29 20 69 6e 20 70 61 67 65 72 2e 63 2e 20 54 68  ) in pager.c. Th
1015b 65 79 0a 2a 2a 20 73 68 6f 75 6c 64 20 70 6f 73  ey.** should pos
1015c 73 69 62 6c 79 20 62 65 20 63 6f 6e 73 6f 6c 69  sibly be consoli
1015d 64 61 74 65 64 20 28 70 72 65 73 75 6d 61 62 6c  dated (presumabl
1015e 79 20 69 6e 20 70 61 67 65 72 2e 68 29 2e 0a 2a  y in pager.h)..*
1015f 2a 0a 2a 2a 20 49 66 20 64 69 73 6b 20 49 2f 4f  *.** If disk I/O
10160 20 69 73 20 6f 6d 69 74 74 65 64 20 28 6d 65 61   is omitted (mea
10161 6e 69 6e 67 20 74 68 61 74 20 74 68 65 20 64 61  ning that the da
10162 74 61 62 61 73 65 20 69 73 20 73 74 6f 72 65 64  tabase is stored
10163 20 70 75 72 65 6c 79 0a 2a 2a 20 69 6e 20 6d 65   purely.** in me
10164 6d 6f 72 79 29 20 74 68 65 6e 20 74 68 65 72 65  mory) then there
10165 20 69 73 20 6e 6f 20 70 65 6e 64 69 6e 67 20 62   is no pending b
10166 79 74 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  yte..*/.#ifdef S
10167 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49  QLITE_OMIT_DISKI
10168 4f 0a 23 20 64 65 66 69 6e 65 20 50 45 4e 44 49  O.# define PENDI
10169 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74  NG_BYTE_PAGE(pBt
1016a 29 20 20 30 78 37 66 66 66 66 66 66 66 0a 23 65  )  0x7fffffff.#e
1016b 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 50 45 4e  lse.# define PEN
1016c 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
1016d 42 74 29 20 28 28 50 45 4e 44 49 4e 47 5f 42 59  Bt) ((PENDING_BY
1016e 54 45 2f 28 70 42 74 29 2d 3e 70 61 67 65 53 69  TE/(pBt)->pageSi
1016f 7a 65 29 2b 31 29 0a 23 65 6e 64 69 66 0a 0a 2f  ze)+1).#endif../
10170 2a 0a 2a 2a 20 41 20 6c 69 6e 6b 65 64 20 6c 69  *.** A linked li
10171 73 74 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  st of the follow
10172 69 6e 67 20 73 74 72 75 63 74 75 72 65 73 20 69  ing structures i
10173 73 20 73 74 6f 72 65 64 20 61 74 20 42 74 53 68  s stored at BtSh
10174 61 72 65 64 2e 70 4c 6f 63 6b 2e 0a 2a 2a 20 4c  ared.pLock..** L
10175 6f 63 6b 73 20 61 72 65 20 61 64 64 65 64 20 28  ocks are added (
10176 6f 72 20 75 70 67 72 61 64 65 64 20 66 72 6f 6d  or upgraded from
10177 20 52 45 41 44 5f 4c 4f 43 4b 20 74 6f 20 57 52   READ_LOCK to WR
10178 49 54 45 5f 4c 4f 43 4b 29 20 77 68 65 6e 20 61  ITE_LOCK) when a
10179 20 63 75 72 73 6f 72 20 0a 2a 2a 20 69 73 20 6f   cursor .** is o
1017a 70 65 6e 65 64 20 6f 6e 20 74 68 65 20 74 61 62  pened on the tab
1017b 6c 65 20 77 69 74 68 20 72 6f 6f 74 20 70 61 67  le with root pag
1017c 65 20 42 74 53 68 61 72 65 64 2e 69 54 61 62 6c  e BtShared.iTabl
1017d 65 2e 20 4c 6f 63 6b 73 20 61 72 65 20 72 65 6d  e. Locks are rem
1017e 6f 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 69  oved.** from thi
1017f 73 20 6c 69 73 74 20 77 68 65 6e 20 61 20 74 72  s list when a tr
10180 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d  ansaction is com
10181 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64  mitted or rolled
10182 20 62 61 63 6b 2c 20 6f 72 20 77 68 65 6e 0a 2a   back, or when.*
10183 2a 20 61 20 62 74 72 65 65 20 68 61 6e 64 6c 65  * a btree handle
10184 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 73   is closed..*/.s
10185 74 72 75 63 74 20 42 74 4c 6f 63 6b 20 7b 0a 20  truct BtLock {. 
10186 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 3b 20   Btree *pBtree; 
10187 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20         /* Btree 
10188 68 61 6e 64 6c 65 20 68 6f 6c 64 69 6e 67 20 74  handle holding t
10189 68 69 73 20 6c 6f 63 6b 20 2a 2f 0a 20 20 50 67  his lock */.  Pg
1018a 6e 6f 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20  no iTable;      
1018b 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65      /* Root page
1018c 20 6f 66 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75   of table */.  u
1018d 38 20 65 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20  8 eLock;        
1018e 20 20 20 20 20 2f 2a 20 52 45 41 44 5f 4c 4f 43       /* READ_LOC
1018f 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 20  K or WRITE_LOCK 
10190 2a 2f 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4e 65  */.  BtLock *pNe
10191 78 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65  xt;        /* Ne
10192 78 74 20 69 6e 20 42 74 53 68 61 72 65 64 2e 70  xt in BtShared.p
10193 4c 6f 63 6b 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a  Lock list */.};.
10194 0a 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 76 61  ./* Candidate va
10195 6c 75 65 73 20 66 6f 72 20 42 74 4c 6f 63 6b 2e  lues for BtLock.
10196 65 4c 6f 63 6b 20 2a 2f 0a 23 64 65 66 69 6e 65  eLock */.#define
10197 20 52 45 41 44 5f 4c 4f 43 4b 20 20 20 20 20 31   READ_LOCK     1
10198 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 4c  .#define WRITE_L
10199 4f 43 4b 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20  OCK    2../*.** 
1019a 54 68 65 73 65 20 6d 61 63 72 6f 73 20 64 65 66  These macros def
1019b 69 6e 65 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e  ine the location
1019c 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d   of the pointer-
1019d 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 20 61 20  map entry for a 
1019e 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61 67  .** database pag
1019f 65 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 67  e. The first arg
101a0 75 6d 65 6e 74 20 74 6f 20 65 61 63 68 20 69 73  ument to each is
101a1 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 75   the number of u
101a2 73 61 62 6c 65 0a 2a 2a 20 62 79 74 65 73 20 6f  sable.** bytes o
101a3 6e 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 74  n each page of t
101a4 68 65 20 64 61 74 61 62 61 73 65 20 28 6f 66 74  he database (oft
101a5 65 6e 20 31 30 32 34 29 2e 20 54 68 65 20 73 65  en 1024). The se
101a6 63 6f 6e 64 20 69 73 20 74 68 65 0a 2a 2a 20 70  cond is the.** p
101a7 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 6c 6f  age number to lo
101a8 6f 6b 20 75 70 20 69 6e 20 74 68 65 20 70 6f 69  ok up in the poi
101a9 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20  nter map..**.** 
101aa 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 20 72 65  PTRMAP_PAGENO re
101ab 74 75 72 6e 73 20 74 68 65 20 64 61 74 61 62 61  turns the databa
101ac 73 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  se page number o
101ad 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  f the pointer-ma
101ae 70 0a 2a 2a 20 70 61 67 65 20 74 68 61 74 20 73  p.** page that s
101af 74 6f 72 65 73 20 74 68 65 20 72 65 71 75 69 72  tores the requir
101b0 65 64 20 70 6f 69 6e 74 65 72 2e 20 50 54 52 4d  ed pointer. PTRM
101b1 41 50 5f 50 54 52 4f 46 46 53 45 54 20 72 65 74  AP_PTROFFSET ret
101b2 75 72 6e 73 0a 2a 2a 20 74 68 65 20 6f 66 66 73  urns.** the offs
101b3 65 74 20 6f 66 20 74 68 65 20 72 65 71 75 65 73  et of the reques
101b4 74 65 64 20 6d 61 70 20 65 6e 74 72 79 2e 0a 2a  ted map entry..*
101b5 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 67 6e 6f  *.** If the pgno
101b6 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64   argument passed
101b7 20 74 6f 20 50 54 52 4d 41 50 5f 50 41 47 45 4e   to PTRMAP_PAGEN
101b8 4f 20 69 73 20 61 20 70 6f 69 6e 74 65 72 2d 6d  O is a pointer-m
101b9 61 70 20 70 61 67 65 2c 0a 2a 2a 20 74 68 65 6e  ap page,.** then
101ba 20 70 67 6e 6f 20 69 73 20 72 65 74 75 72 6e 65   pgno is returne
101bb 64 2e 20 53 6f 20 28 70 67 6e 6f 3d 3d 50 54 52  d. So (pgno==PTR
101bc 4d 41 50 5f 50 41 47 45 4e 4f 28 70 67 73 7a 2c  MAP_PAGENO(pgsz,
101bd 20 70 67 6e 6f 29 29 20 63 61 6e 20 62 65 0a 2a   pgno)) can be.*
101be 2a 20 75 73 65 64 20 74 6f 20 74 65 73 74 20 69  * used to test i
101bf 66 20 70 67 6e 6f 20 69 73 20 61 20 70 6f 69 6e  f pgno is a poin
101c0 74 65 72 2d 6d 61 70 20 70 61 67 65 2e 20 50 54  ter-map page. PT
101c1 52 4d 41 50 5f 49 53 50 41 47 45 20 69 6d 70 6c  RMAP_ISPAGE impl
101c2 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 69 73 20 74  ements.** this t
101c3 65 73 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  est..*/.#define 
101c4 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42  PTRMAP_PAGENO(pB
101c5 74 2c 20 70 67 6e 6f 29 20 70 74 72 6d 61 70 50  t, pgno) ptrmapP
101c6 61 67 65 6e 6f 28 70 42 74 2c 20 70 67 6e 6f 29  ageno(pBt, pgno)
101c7 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f  .#define PTRMAP_
101c8 50 54 52 4f 46 46 53 45 54 28 70 42 74 2c 20 70  PTROFFSET(pBt, p
101c9 67 6e 6f 29 20 28 35 2a 28 70 67 6e 6f 2d 70 74  gno) (5*(pgno-pt
101ca 72 6d 61 70 50 61 67 65 6e 6f 28 70 42 74 2c 20  rmapPageno(pBt, 
101cb 70 67 6e 6f 29 2d 31 29 29 0a 23 64 65 66 69 6e  pgno)-1)).#defin
101cc 65 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28  e PTRMAP_ISPAGE(
101cd 70 42 74 2c 20 70 67 6e 6f 29 20 28 50 54 52 4d  pBt, pgno) (PTRM
101ce 41 50 5f 50 41 47 45 4e 4f 28 28 70 42 74 29 2c  AP_PAGENO((pBt),
101cf 28 70 67 6e 6f 29 29 3d 3d 28 70 67 6e 6f 29 29  (pgno))==(pgno))
101d0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e  ../*.** The poin
101d1 74 65 72 20 6d 61 70 20 69 73 20 61 20 6c 6f 6f  ter map is a loo
101d2 6b 75 70 20 74 61 62 6c 65 20 74 68 61 74 20 69  kup table that i
101d3 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 61  dentifies the pa
101d4 72 65 6e 74 20 70 61 67 65 20 66 6f 72 0a 2a 2a  rent page for.**
101d5 20 65 61 63 68 20 63 68 69 6c 64 20 70 61 67 65   each child page
101d6 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
101d7 20 66 69 6c 65 2e 20 20 54 68 65 20 70 61 72 65   file.  The pare
101d8 6e 74 20 70 61 67 65 20 69 73 20 74 68 65 20 70  nt page is the p
101d9 61 67 65 20 74 68 61 74 0a 2a 2a 20 63 6f 6e 74  age that.** cont
101da 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  ains a pointer t
101db 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 20 45 76  o the child.  Ev
101dc 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20  ery page in the 
101dd 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
101de 73 0a 2a 2a 20 30 20 6f 72 20 31 20 70 61 72 65  s.** 0 or 1 pare
101df 6e 74 20 70 61 67 65 73 2e 20 20 28 49 6e 20 74  nt pages.  (In t
101e0 68 69 73 20 63 6f 6e 74 65 78 74 20 27 64 61 74  his context 'dat
101e1 61 62 61 73 65 20 70 61 67 65 27 20 72 65 66 65  abase page' refe
101e2 72 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 70 61 67  rs.** to any pag
101e3 65 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70 61  e that is not pa
101e4 72 74 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65  rt of the pointe
101e5 72 20 6d 61 70 20 69 74 73 65 6c 66 2e 29 20 20  r map itself.)  
101e6 45 61 63 68 20 70 6f 69 6e 74 65 72 20 6d 61 70  Each pointer map
101e7 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 6e 73 69 73  .** entry consis
101e8 74 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 62  ts of a single b
101e9 79 74 65 20 27 74 79 70 65 27 20 61 6e 64 20 61  yte 'type' and a
101ea 20 34 20 62 79 74 65 20 70 61 72 65 6e 74 20 70   4 byte parent p
101eb 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 54  age number..** T
101ec 68 65 20 50 54 52 4d 41 50 5f 58 58 58 20 69 64  he PTRMAP_XXX id
101ed 65 6e 74 69 66 69 65 72 73 20 62 65 6c 6f 77 20  entifiers below 
101ee 61 72 65 20 74 68 65 20 76 61 6c 69 64 20 74 79  are the valid ty
101ef 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  pes..**.** The p
101f0 75 72 70 6f 73 65 20 6f 66 20 74 68 65 20 70 6f  urpose of the po
101f1 69 6e 74 65 72 20 6d 61 70 20 69 73 20 74 6f 20  inter map is to 
101f2 66 61 63 69 6c 69 74 79 20 6d 6f 76 69 6e 67 20  facility moving 
101f3 70 61 67 65 73 20 66 72 6f 6d 20 6f 6e 65 0a 2a  pages from one.*
101f4 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68  * position in th
101f5 65 20 66 69 6c 65 20 74 6f 20 61 6e 6f 74 68 65  e file to anothe
101f6 72 20 61 73 20 70 61 72 74 20 6f 66 20 61 75 74  r as part of aut
101f7 6f 76 61 63 75 75 6d 2e 20 20 57 68 65 6e 20 61  ovacuum.  When a
101f8 20 70 61 67 65 0a 2a 2a 20 69 73 20 6d 6f 76 65   page.** is move
101f9 64 2c 20 74 68 65 20 70 6f 69 6e 74 65 72 20 69  d, the pointer i
101fa 6e 20 69 74 73 20 70 61 72 65 6e 74 20 6d 75 73  n its parent mus
101fb 74 20 62 65 20 75 70 64 61 74 65 64 20 74 6f 20  t be updated to 
101fc 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20  point to the.** 
101fd 6e 65 77 20 6c 6f 63 61 74 69 6f 6e 2e 20 20 54  new location.  T
101fe 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69  he pointer map i
101ff 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65  s used to locate
10200 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   the parent page
10201 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 0a 2a 2a 20   quickly..**.** 
10202 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 3a  PTRMAP_ROOTPAGE:
10203 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61   The database pa
10204 67 65 20 69 73 20 61 20 72 6f 6f 74 2d 70 61 67  ge is a root-pag
10205 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62  e. The page-numb
10206 65 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 20 20 20  er is not.**    
10207 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75 73                us
10208 65 64 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e  ed in this case.
10209 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 46 52  .**.** PTRMAP_FR
1020a 45 45 50 41 47 45 3a 20 54 68 65 20 64 61 74 61  EEPAGE: The data
1020b 62 61 73 65 20 70 61 67 65 20 69 73 20 61 6e 20  base page is an 
1020c 75 6e 75 73 65 64 20 28 66 72 65 65 29 20 70 61  unused (free) pa
1020d 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d  ge. The page-num
1020e 62 65 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 20  ber .**         
1020f 20 20 20 20 20 20 20 20 20 69 73 20 6e 6f 74 20           is not 
10210 75 73 65 64 20 69 6e 20 74 68 69 73 20 63 61 73  used in this cas
10211 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f  e..**.** PTRMAP_
10212 4f 56 45 52 46 4c 4f 57 31 3a 20 54 68 65 20 64  OVERFLOW1: The d
10213 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20  atabase page is 
10214 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 69  the first page i
10215 6e 20 61 20 6c 69 73 74 20 6f 66 20 0a 2a 2a 20  n a list of .** 
10216 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10217 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73    overflow pages
10218 2e 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65  . The page numbe
10219 72 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65  r identifies the
1021a 20 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 20 20   page that.**   
1021b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1021c 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 65 6c  contains the cel
1021d 6c 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72  l with a pointer
1021e 20 74 6f 20 74 68 69 73 20 6f 76 65 72 66 6c 6f   to this overflo
1021f 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54  w page..**.** PT
10220 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a 20  RMAP_OVERFLOW2: 
10221 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67  The database pag
10222 65 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20  e is the second 
10223 6f 72 20 6c 61 74 65 72 20 70 61 67 65 20 69 6e  or later page in
10224 20 61 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 20 20   a list of.**   
10225 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10226 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20  overflow pages. 
10227 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20  The page-number 
10228 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70  identifies the p
10229 72 65 76 69 6f 75 73 0a 2a 2a 20 20 20 20 20 20  revious.**      
1022a 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67               pag
1022b 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f  e in the overflo
1022c 77 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a 2a 0a  w page list..**.
1022d 2a 2a 20 50 54 52 4d 41 50 5f 42 54 52 45 45 3a  ** PTRMAP_BTREE:
1022e 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61   The database pa
1022f 67 65 20 69 73 20 61 20 6e 6f 6e 2d 72 6f 6f 74  ge is a non-root
10230 20 62 74 72 65 65 20 70 61 67 65 2e 20 54 68 65   btree page. The
10231 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a 2a 20   page number.** 
10232 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 64                id
10233 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 61 72  entifies the par
10234 65 6e 74 20 70 61 67 65 20 69 6e 20 74 68 65 20  ent page in the 
10235 62 74 72 65 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e  btree..*/.#defin
10236 65 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47  e PTRMAP_ROOTPAG
10237 45 20 31 0a 23 64 65 66 69 6e 65 20 50 54 52 4d  E 1.#define PTRM
10238 41 50 5f 46 52 45 45 50 41 47 45 20 32 0a 23 64  AP_FREEPAGE 2.#d
10239 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f 56 45  efine PTRMAP_OVE
1023a 52 46 4c 4f 57 31 20 33 0a 23 64 65 66 69 6e 65  RFLOW1 3.#define
1023b 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
1023c 32 20 34 0a 23 64 65 66 69 6e 65 20 50 54 52 4d  2 4.#define PTRM
1023d 41 50 5f 42 54 52 45 45 20 35 0a 0a 2f 2a 20 41  AP_BTREE 5../* A
1023e 20 62 75 6e 63 68 20 6f 66 20 61 73 73 65 72 74   bunch of assert
1023f 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f  () statements to
10240 20 63 68 65 63 6b 20 74 68 65 20 74 72 61 6e 73   check the trans
10241 61 63 74 69 6f 6e 20 73 74 61 74 65 20 76 61 72  action state var
10242 69 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 68 61 6e  iables.** of han
10243 64 6c 65 20 70 20 28 74 79 70 65 20 42 74 72 65  dle p (type Btre
10244 65 2a 29 20 61 72 65 20 69 6e 74 65 72 6e 61 6c  e*) are internal
10245 6c 79 20 63 6f 6e 73 69 73 74 65 6e 74 2e 0a 2a  ly consistent..*
10246 2f 0a 23 64 65 66 69 6e 65 20 62 74 72 65 65 49  /.#define btreeI
10247 6e 74 65 67 72 69 74 79 28 70 29 20 5c 0a 20 20  ntegrity(p) \.  
10248 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e  assert( p->pBt->
10249 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d 54  inTransaction!=T
1024a 52 41 4e 53 5f 4e 4f 4e 45 20 7c 7c 20 70 2d 3e  RANS_NONE || p->
1024b 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f  pBt->nTransactio
1024c 6e 3d 3d 30 20 29 3b 20 5c 0a 20 20 61 73 73 65  n==0 ); \.  asse
1024d 72 74 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54 72  rt( p->pBt->inTr
1024e 61 6e 73 61 63 74 69 6f 6e 3e 3d 70 2d 3e 69 6e  ansaction>=p->in
1024f 54 72 61 6e 73 20 29 3b 20 0a 0a 0a 2f 2a 0a 2a  Trans ); .../*.*
10250 2a 20 54 68 65 20 49 53 41 55 54 4f 56 41 43 55  * The ISAUTOVACU
10251 55 4d 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64  UM macro is used
10252 20 77 69 74 68 69 6e 20 62 61 6c 61 6e 63 65 5f   within balance_
10253 6e 6f 6e 72 6f 6f 74 28 29 20 74 6f 20 64 65 74  nonroot() to det
10254 65 72 6d 69 6e 65 0a 2a 2a 20 69 66 20 74 68 65  ermine.** if the
10255 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72   database suppor
10256 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6f  ts auto-vacuum o
10257 72 20 6e 6f 74 2e 20 42 65 63 61 75 73 65 20 69  r not. Because i
10258 74 20 69 73 20 75 73 65 64 0a 2a 2a 20 77 69 74  t is used.** wit
10259 68 69 6e 20 61 6e 20 65 78 70 72 65 73 73 69 6f  hin an expressio
1025a 6e 20 74 68 61 74 20 69 73 20 61 6e 20 61 72 67  n that is an arg
1025b 75 6d 65 6e 74 20 74 6f 20 61 6e 6f 74 68 65 72  ument to another
1025c 20 6d 61 63 72 6f 20 0a 2a 2a 20 28 73 71 6c 69   macro .** (sqli
1025d 74 65 4d 61 6c 6c 6f 63 52 61 77 29 2c 20 69 74  teMallocRaw), it
1025e 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
1025f 20 74 6f 20 75 73 65 20 63 6f 6e 64 69 74 69 6f   to use conditio
10260 6e 61 6c 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 2e  nal compilation.
10261 0a 2a 2a 20 53 6f 2c 20 74 68 69 73 20 6d 61 63  .** So, this mac
10262 72 6f 20 69 73 20 64 65 66 69 6e 65 64 20 69 6e  ro is defined in
10263 73 74 65 61 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65  stead..*/.#ifnde
10264 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
10265 54 4f 56 41 43 55 55 4d 0a 23 64 65 66 69 6e 65  TOVACUUM.#define
10266 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 28 70   ISAUTOVACUUM (p
10267 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 0a  Bt->autoVacuum).
10268 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 49 53  #else.#define IS
10269 41 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 65 6e  AUTOVACUUM 0.#en
1026a 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  dif.../*.** This
1026b 20 73 74 72 75 63 74 75 72 65 20 69 73 20 70 61   structure is pa
1026c 73 73 65 64 20 61 72 6f 75 6e 64 20 74 68 72 6f  ssed around thro
1026d 75 67 68 20 61 6c 6c 20 74 68 65 20 73 61 6e 69  ugh all the sani
1026e 74 79 20 63 68 65 63 6b 69 6e 67 20 72 6f 75 74  ty checking rout
1026f 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 72 64 65 72  ines.** in order
10270 20 74 6f 20 6b 65 65 70 20 74 72 61 63 6b 20 6f   to keep track o
10271 66 20 73 6f 6d 65 20 67 6c 6f 62 61 6c 20 73 74  f some global st
10272 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  ate information.
10273 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
10274 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 49  ct IntegrityCk I
10275 6e 74 65 67 72 69 74 79 43 6b 3b 0a 73 74 72 75  ntegrityCk;.stru
10276 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 7b  ct IntegrityCk {
10277 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
10278 3b 20 20 20 20 2f 2a 20 54 68 65 20 74 72 65 65  ;    /* The tree
10279 20 62 65 69 6e 67 20 63 68 65 63 6b 65 64 20 6f   being checked o
1027a 75 74 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a 70  ut */.  Pager *p
1027b 50 61 67 65 72 3b 20 20 20 20 2f 2a 20 54 68 65  Pager;    /* The
1027c 20 61 73 73 6f 63 69 61 74 65 64 20 70 61 67 65   associated page
1027d 72 2e 20 20 41 6c 73 6f 20 61 63 63 65 73 73 69  r.  Also accessi
1027e 62 6c 65 20 62 79 20 70 42 74 2d 3e 70 50 61 67  ble by pBt->pPag
1027f 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 67  er */.  int nPag
10280 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  e;        /* Num
10281 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
10282 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  the database */.
10283 20 20 69 6e 74 20 2a 61 6e 52 65 66 3b 20 20 20    int *anRef;   
10284 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
10285 20 74 69 6d 65 73 20 65 61 63 68 20 70 61 67 65   times each page
10286 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 20 2a   is referenced *
10287 2f 0a 20 20 69 6e 74 20 6d 78 45 72 72 3b 20 20  /.  int mxErr;  
10288 20 20 20 20 20 20 2f 2a 20 53 74 6f 70 20 61 63        /* Stop ac
10289 63 75 6d 75 6c 61 74 69 6e 67 20 65 72 72 6f 72  cumulating error
1028a 73 20 77 68 65 6e 20 74 68 69 73 20 72 65 61 63  s when this reac
1028b 68 65 73 20 7a 65 72 6f 20 2a 2f 0a 20 20 63 68  hes zero */.  ch
1028c 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20  ar *zErrMsg;    
1028d 2f 2a 20 41 6e 20 65 72 72 6f 72 20 6d 65 73 73  /* An error mess
1028e 61 67 65 2e 20 20 4e 55 4c 4c 20 69 66 20 6e 6f  age.  NULL if no
1028f 20 65 72 72 6f 72 73 20 73 65 65 6e 2e 20 2a 2f   errors seen. */
10290 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20  .  int nErr;    
10291 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
10292 66 20 6d 65 73 73 61 67 65 73 20 77 72 69 74 74  f messages writt
10293 65 6e 20 74 6f 20 7a 45 72 72 4d 73 67 20 73 6f  en to zErrMsg so
10294 20 66 61 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a   far */.};../*.*
10295 2a 20 52 65 61 64 20 6f 72 20 77 72 69 74 65 20  * Read or write 
10296 61 20 74 77 6f 2d 20 61 6e 64 20 66 6f 75 72 2d  a two- and four-
10297 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20  byte big-endian 
10298 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 2e 0a  integer values..
10299 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 32 62  */.#define get2b
1029a 79 74 65 28 78 29 20 20 20 28 28 78 29 5b 30 5d  yte(x)   ((x)[0]
1029b 3c 3c 38 20 7c 20 28 78 29 5b 31 5d 29 0a 23 64  <<8 | (x)[1]).#d
1029c 65 66 69 6e 65 20 70 75 74 32 62 79 74 65 28 70  efine put2byte(p
1029d 2c 76 29 20 28 28 70 29 5b 30 5d 20 3d 20 28 76  ,v) ((p)[0] = (v
1029e 29 3e 3e 38 2c 20 28 70 29 5b 31 5d 20 3d 20 28  )>>8, (p)[1] = (
1029f 76 29 29 0a 23 64 65 66 69 6e 65 20 67 65 74 34  v)).#define get4
102a0 62 79 74 65 20 73 71 6c 69 74 65 33 47 65 74 34  byte sqlite3Get4
102a1 62 79 74 65 0a 23 64 65 66 69 6e 65 20 70 75 74  byte.#define put
102a2 34 62 79 74 65 20 73 71 6c 69 74 65 33 50 75 74  4byte sqlite3Put
102a3 34 62 79 74 65 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74  4byte../*.** Int
102a4 65 72 6e 61 6c 20 72 6f 75 74 69 6e 65 73 20 74  ernal routines t
102a5 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 61 63  hat should be ac
102a6 63 65 73 73 65 64 20 62 79 20 74 68 65 20 62 74  cessed by the bt
102a7 72 65 65 20 6c 61 79 65 72 20 6f 6e 6c 79 2e 0a  ree layer only..
102a8 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
102a9 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
102aa 65 65 47 65 74 50 61 67 65 28 42 74 53 68 61 72  eeGetPage(BtShar
102ab 65 64 2a 2c 20 50 67 6e 6f 2c 20 4d 65 6d 50 61  ed*, Pgno, MemPa
102ac 67 65 2a 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ge**, int);.SQLI
102ad 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
102ae 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50  qlite3BtreeInitP
102af 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  age(MemPage *pPa
102b0 67 65 2c 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ge, MemPage *pPa
102b1 72 65 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  rent);.SQLITE_PR
102b2 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
102b3 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c  e3BtreeParseCell
102b4 50 74 72 28 4d 65 6d 50 61 67 65 2a 2c 20 75 38  Ptr(MemPage*, u8
102b5 2a 2c 20 43 65 6c 6c 49 6e 66 6f 2a 29 3b 0a 53  *, CellInfo*);.S
102b6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
102b7 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  id sqlite3BtreeP
102b8 61 72 73 65 43 65 6c 6c 28 4d 65 6d 50 61 67 65  arseCell(MemPage
102b9 2a 2c 20 69 6e 74 2c 20 43 65 6c 6c 49 6e 66 6f  *, int, CellInfo
102ba 2a 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  *);.#ifdef SQLIT
102bb 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52  E_TEST.SQLITE_PR
102bc 49 56 41 54 45 20 75 38 20 2a 73 71 6c 69 74 65  IVATE u8 *sqlite
102bd 33 42 74 72 65 65 46 69 6e 64 43 65 6c 6c 28 4d  3BtreeFindCell(M
102be 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69  emPage *pPage, i
102bf 6e 74 20 69 43 65 6c 6c 29 3b 0a 23 65 6e 64 69  nt iCell);.#endi
102c0 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  f.SQLITE_PRIVATE
102c1 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
102c2 65 52 65 73 74 6f 72 65 4f 72 43 6c 65 61 72 43  eRestoreOrClearC
102c3 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74  ursorPosition(Bt
102c4 43 75 72 73 6f 72 20 2a 70 43 75 72 29 3b 0a 53  Cursor *pCur);.S
102c5 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
102c6 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  id sqlite3BtreeG
102c7 65 74 54 65 6d 70 43 75 72 73 6f 72 28 42 74 43  etTempCursor(BtC
102c8 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 42 74 43  ursor *pCur, BtC
102c9 75 72 73 6f 72 20 2a 70 54 65 6d 70 43 75 72 29  ursor *pTempCur)
102ca 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
102cb 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
102cc 65 65 52 65 6c 65 61 73 65 54 65 6d 70 43 75 72  eeReleaseTempCur
102cd 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43  sor(BtCursor *pC
102ce 75 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ur);.SQLITE_PRIV
102cf 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
102d0 74 72 65 65 49 73 52 6f 6f 74 50 61 67 65 28 4d  treeIsRootPage(M
102d1 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 3b 0a  emPage *pPage);.
102d2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
102d3 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
102d4 4d 6f 76 65 54 6f 50 61 72 65 6e 74 28 42 74 43  MoveToParent(BtC
102d5 75 72 73 6f 72 20 2a 70 43 75 72 29 3b 0a 0a 2f  ursor *pCur);../
102d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
102d7 6e 64 20 6f 66 20 62 74 72 65 65 49 6e 74 2e 68  nd of btreeInt.h
102d8 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
102d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
102da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
102db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
102dc 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
102dd 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 62  we left off in b
102de 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a  tmutex.c *******
102df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23  *************/.#
102e0 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
102e1 53 41 46 45 20 26 26 20 21 64 65 66 69 6e 65 64  SAFE && !defined
102e2 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41  (SQLITE_OMIT_SHA
102e3 52 45 44 5f 43 41 43 48 45 29 0a 0a 0a 2f 2a 0a  RED_CACHE).../*.
102e4 2a 2a 20 45 6e 74 65 72 20 61 20 6d 75 74 65 78  ** Enter a mutex
102e5 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 42 54   on the given BT
102e6 72 65 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a  ree object..**.*
102e7 2a 20 49 66 20 74 68 65 20 6f 62 6a 65 63 74 20  * If the object 
102e8 69 73 20 6e 6f 74 20 73 68 61 72 61 62 6c 65 2c  is not sharable,
102e9 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 20 69   then no mutex i
102ea 73 20 65 76 65 72 20 72 65 71 75 69 72 65 64 0a  s ever required.
102eb 2a 2a 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74  ** and this rout
102ec 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20  ine is a no-op. 
102ed 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   The underlying 
102ee 6d 75 74 65 78 20 69 73 20 6e 6f 6e 2d 72 65 63  mutex is non-rec
102ef 75 72 73 69 76 65 2e 0a 2a 2a 20 42 75 74 20 77  ursive..** But w
102f0 65 20 6b 65 65 70 20 61 20 72 65 66 65 72 65 6e  e keep a referen
102f1 63 65 20 63 6f 75 6e 74 20 69 6e 20 42 74 72 65  ce count in Btre
102f2 65 2e 77 61 6e 74 54 6f 4c 6f 63 6b 20 73 6f 20  e.wantToLock so 
102f3 74 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20  the behavior.** 
102f4 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66 61 63  of this interfac
102f5 65 20 69 73 20 72 65 63 75 72 73 69 76 65 2e 0a  e is recursive..
102f6 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 64  **.** To avoid d
102f7 65 61 64 6c 6f 63 6b 73 2c 20 6d 75 6c 74 69 70  eadlocks, multip
102f8 6c 65 20 42 74 72 65 65 73 20 61 72 65 20 6c 6f  le Btrees are lo
102f9 63 6b 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65  cked in the same
102fa 20 6f 72 64 65 72 0a 2a 2a 20 62 79 20 61 6c 6c   order.** by all
102fb 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
102fc 74 69 6f 6e 73 2e 20 20 54 68 65 20 70 2d 3e 70  tions.  The p->p
102fd 4e 65 78 74 20 69 73 20 61 20 6c 69 73 74 20 6f  Next is a list o
102fe 66 20 6f 74 68 65 72 0a 2a 2a 20 42 74 72 65 65  f other.** Btree
102ff 73 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 74  s belonging to t
10300 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
10301 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 74   connection as t
10302 68 65 20 70 20 42 74 72 65 65 0a 2a 2a 20 77 68  he p Btree.** wh
10303 69 63 68 20 6e 65 65 64 20 74 6f 20 62 65 20 6c  ich need to be l
10304 6f 63 6b 65 64 20 61 66 74 65 72 20 70 2e 20 20  ocked after p.  
10305 49 66 20 77 65 20 63 61 6e 6e 6f 74 20 67 65 74  If we cannot get
10306 20 61 20 6c 6f 63 6b 20 6f 6e 0a 2a 2a 20 70 2c   a lock on.** p,
10307 20 74 68 65 6e 20 66 69 72 73 74 20 75 6e 6c 6f   then first unlo
10308 63 6b 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f 74  ck all of the ot
10309 68 65 72 73 20 6f 6e 20 70 2d 3e 70 4e 65 78 74  hers on p->pNext
1030a 2c 20 74 68 65 6e 20 77 61 69 74 0a 2a 2a 20 66  , then wait.** f
1030b 6f 72 20 74 68 65 20 6c 6f 63 6b 20 74 6f 20 62  or the lock to b
1030c 65 63 6f 6d 65 20 61 76 61 69 6c 61 62 6c 65 20  ecome available 
1030d 6f 6e 20 70 2c 20 74 68 65 6e 20 72 65 6c 6f 63  on p, then reloc
1030e 6b 20 61 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a 20  k all of the.** 
1030f 73 75 62 73 65 71 75 65 6e 74 20 42 74 72 65 65  subsequent Btree
10310 73 20 74 68 61 74 20 64 65 73 69 72 65 20 61 20  s that desire a 
10311 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lock..*/.SQLITE_
10312 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
10313 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 42  ite3BtreeEnter(B
10314 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 72 65  tree *p){.  Btre
10315 65 20 2a 70 4c 61 74 65 72 3b 0a 0a 20 20 2f 2a  e *pLater;..  /*
10316 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e 69   Some basic sani
10317 74 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74  ty checking on t
10318 68 65 20 42 74 72 65 65 2e 20 20 54 68 65 20 6c  he Btree.  The l
10319 69 73 74 20 6f 66 20 42 74 72 65 65 73 0a 20 20  ist of Btrees.  
1031a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20  ** connected by 
1031b 70 4e 65 78 74 20 61 6e 64 20 70 50 72 65 76 20  pNext and pPrev 
1031c 73 68 6f 75 6c 64 20 62 65 20 69 6e 20 73 6f 72  should be in sor
1031d 74 65 64 20 6f 72 64 65 72 20 62 79 0a 20 20 2a  ted order by.  *
1031e 2a 20 42 74 72 65 65 2e 70 42 74 20 76 61 6c 75  * Btree.pBt valu
1031f 65 2e 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20  e. All elements 
10320 6f 66 20 74 68 65 20 6c 69 73 74 20 73 68 6f 75  of the list shou
10321 6c 64 20 62 65 6c 6f 6e 67 20 74 6f 0a 20 20 2a  ld belong to.  *
10322 2a 20 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65  * the same conne
10323 63 74 69 6f 6e 2e 20 4f 6e 6c 79 20 73 68 61 72  ction. Only shar
10324 65 64 20 42 74 72 65 65 73 20 61 72 65 20 6f 6e  ed Btrees are on
10325 20 74 68 65 20 6c 69 73 74 2e 20 2a 2f 0a 20 20   the list. */.  
10326 61 73 73 65 72 74 28 20 70 2d 3e 70 4e 65 78 74  assert( p->pNext
10327 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d  ==0 || p->pNext-
10328 3e 70 42 74 3e 70 2d 3e 70 42 74 20 29 3b 0a 20  >pBt>p->pBt );. 
10329 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72 65   assert( p->pPre
1032a 76 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65 76  v==0 || p->pPrev
1032b 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29 3b 0a  ->pBt<p->pBt );.
1032c 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4e 65    assert( p->pNe
1032d 78 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78  xt==0 || p->pNex
1032e 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a  t->db==p->db );.
1032f 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72    assert( p->pPr
10330 65 76 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65  ev==0 || p->pPre
10331 76 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a  v->db==p->db );.
10332 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61    assert( p->sha
10333 72 61 62 6c 65 20 7c 7c 20 28 70 2d 3e 70 4e 65  rable || (p->pNe
10334 78 74 3d 3d 30 20 26 26 20 70 2d 3e 70 50 72 65  xt==0 && p->pPre
10335 76 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 43  v==0) );..  /* C
10336 68 65 63 6b 20 66 6f 72 20 6c 6f 63 6b 69 6e 67  heck for locking
10337 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 2a 2f 0a   consistency */.
10338 20 20 61 73 73 65 72 74 28 20 21 70 2d 3e 6c 6f    assert( !p->lo
10339 63 6b 65 64 20 7c 7c 20 70 2d 3e 77 61 6e 74 54  cked || p->wantT
1033a 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 61 73 73  oLock>0 );.  ass
1033b 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65  ert( p->sharable
1033c 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63   || p->wantToLoc
1033d 6b 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 57 65  k==0 );..  /* We
1033e 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20   should already 
1033f 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74  hold a lock on t
10340 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
10341 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 65  ection */.  asse
10342 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
10343 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
10344 74 65 78 29 20 29 3b 0a 0a 20 20 69 66 28 20 21  tex) );..  if( !
10345 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 20 72 65  p->sharable ) re
10346 74 75 72 6e 3b 0a 20 20 70 2d 3e 77 61 6e 74 54  turn;.  p->wantT
10347 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 69 66 28 20 70  oLock++;.  if( p
10348 2d 3e 6c 6f 63 6b 65 64 20 29 20 72 65 74 75 72  ->locked ) retur
10349 6e 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  n;..#ifndef SQLI
1034a 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a 20 20  TE_MUTEX_NOOP.  
1034b 2f 2a 20 49 6e 20 6d 6f 73 74 20 63 61 73 65 73  /* In most cases
1034c 2c 20 77 65 20 73 68 6f 75 6c 64 20 62 65 20 61  , we should be a
1034d 62 6c 65 20 74 6f 20 61 63 71 75 69 72 65 20 74  ble to acquire t
1034e 68 65 20 6c 6f 63 6b 20 77 65 0a 20 20 2a 2a 20  he lock we.  ** 
1034f 77 61 6e 74 20 77 69 74 68 6f 75 74 20 68 61 76  want without hav
10350 69 6e 67 20 74 6f 20 67 6f 20 74 68 72 6f 75 67  ing to go throug
10351 68 74 20 74 68 65 20 61 73 63 65 6e 64 69 6e 67  ht the ascending
10352 20 6c 6f 63 6b 0a 20 20 2a 2a 20 70 72 6f 63 65   lock.  ** proce
10353 64 75 72 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77  dure that follow
10354 73 2e 20 20 4a 75 73 74 20 62 65 20 73 75 72 65  s.  Just be sure
10355 20 6e 6f 74 20 74 6f 20 62 6c 6f 63 6b 2e 0a 20   not to block.. 
10356 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
10357 33 5f 6d 75 74 65 78 5f 74 72 79 28 70 2d 3e 70  3_mutex_try(p->p
10358 42 74 2d 3e 6d 75 74 65 78 29 3d 3d 53 51 4c 49  Bt->mutex)==SQLI
10359 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 2d 3e  TE_OK ){.    p->
1035a 6c 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 20 20 20  locked = 1;.    
1035b 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f  return;.  }..  /
1035c 2a 20 54 6f 20 61 76 6f 69 64 20 64 65 61 64 6c  * To avoid deadl
1035d 6f 63 6b 2c 20 66 69 72 73 74 20 72 65 6c 65 61  ock, first relea
1035e 73 65 20 61 6c 6c 20 6c 6f 63 6b 73 20 77 69 74  se all locks wit
1035f 68 20 61 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20  h a larger.  ** 
10360 42 74 53 68 61 72 65 64 20 61 64 64 72 65 73 73  BtShared address
10361 2e 20 20 54 68 65 6e 20 61 63 71 75 69 72 65 20  .  Then acquire 
10362 6f 75 72 20 6c 6f 63 6b 2e 20 20 54 68 65 6e 20  our lock.  Then 
10363 72 65 61 63 71 75 69 72 65 0a 20 20 2a 2a 20 74  reacquire.  ** t
10364 68 65 20 6f 74 68 65 72 20 42 74 53 68 61 72 65  he other BtShare
10365 64 20 6c 6f 63 6b 73 20 74 68 61 74 20 77 65 20  d locks that we 
10366 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 69 6e 20  used to hold in 
10367 61 73 63 65 6e 64 69 6e 67 0a 20 20 2a 2a 20 6f  ascending.  ** o
10368 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  rder..  */.  for
10369 28 70 4c 61 74 65 72 3d 70 2d 3e 70 4e 65 78 74  (pLater=p->pNext
1036a 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 72  ; pLater; pLater
1036b 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b  =pLater->pNext){
1036c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 61  .    assert( pLa
1036d 74 65 72 2d 3e 73 68 61 72 61 62 6c 65 20 29 3b  ter->sharable );
1036e 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 61  .    assert( pLa
1036f 74 65 72 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c  ter->pNext==0 ||
10370 20 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 2d 3e   pLater->pNext->
10371 70 42 74 3e 70 4c 61 74 65 72 2d 3e 70 42 74 20  pBt>pLater->pBt 
10372 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  );.    assert( !
10373 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 7c  pLater->locked |
10374 7c 20 70 4c 61 74 65 72 2d 3e 77 61 6e 74 54 6f  | pLater->wantTo
10375 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 69 66  Lock>0 );.    if
10376 28 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64  ( pLater->locked
10377 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
10378 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 4c  3_mutex_leave(pL
10379 61 74 65 72 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  ater->pBt->mutex
1037a 29 3b 0a 20 20 20 20 20 20 70 4c 61 74 65 72 2d  );.      pLater-
1037b 3e 6c 6f 63 6b 65 64 20 3d 20 30 3b 0a 20 20 20  >locked = 0;.   
1037c 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
1037d 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e  _mutex_enter(p->
1037e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70  pBt->mutex);.  p
1037f 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 20  ->locked = 1;.  
10380 66 6f 72 28 70 4c 61 74 65 72 3d 70 2d 3e 70 4e  for(pLater=p->pN
10381 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61  ext; pLater; pLa
10382 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78  ter=pLater->pNex
10383 74 29 7b 0a 20 20 20 20 69 66 28 20 70 4c 61 74  t){.    if( pLat
10384 65 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 20 29  er->wantToLock )
10385 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
10386 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 4c 61 74  mutex_enter(pLat
10387 65 72 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b  er->pBt->mutex);
10388 0a 20 20 20 20 20 20 70 4c 61 74 65 72 2d 3e 6c  .      pLater->l
10389 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d  ocked = 1;.    }
1038a 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
1038b 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50  QLITE_MUTEX_NOOP
1038c 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 69   */.}../*.** Exi
1038d 74 20 74 68 65 20 72 65 63 75 72 73 69 76 65 20  t the recursive 
1038e 6d 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65 65  mutex on a Btree
1038f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
10390 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
10391 42 74 72 65 65 4c 65 61 76 65 28 42 74 72 65 65  BtreeLeave(Btree
10392 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 73   *p){.  if( p->s
10393 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 61  harable ){.    a
10394 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f  ssert( p->wantTo
10395 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 70 2d  Lock>0 );.    p-
10396 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20  >wantToLock--;. 
10397 20 20 20 69 66 28 20 70 2d 3e 77 61 6e 74 54 6f     if( p->wantTo
10398 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Lock==0 ){.     
10399 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b   assert( p->lock
1039a 65 64 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ed );.      sqli
1039b 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
1039c 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a  p->pBt->mutex);.
1039d 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20        p->locked 
1039e 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  = 0;.    }.  }.}
1039f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
103a0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  ./*.** Return tr
103a1 75 65 20 69 66 20 74 68 65 20 42 74 53 68 61 72  ue if the BtShar
103a2 65 64 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64  ed mutex is held
103a3 20 6f 6e 20 74 68 65 20 62 74 72 65 65 2e 20 20   on the btree.  
103a4 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
103a5 69 6e 65 20 6d 61 6b 65 73 20 6e 6f 20 64 65 74  ine makes no det
103a6 65 72 6d 69 6e 61 74 69 6f 6e 20 6f 6e 65 20 77  ermination one w
103a7 68 79 20 6f 72 20 61 6e 6f 74 68 65 72 20 69 66  hy or another if
103a8 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
103a9 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 74 65   connection mute
103aa 78 20 69 73 20 68 65 6c 64 2e 0a 2a 2a 0a 2a 2a  x is held..**.**
103ab 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
103ac 20 75 73 65 64 20 6f 6e 6c 79 20 66 72 6f 6d 20   used only from 
103ad 77 69 74 68 69 6e 20 61 73 73 65 72 74 28 29 20  within assert() 
103ae 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53  statements..*/.S
103af 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
103b0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  t sqlite3BtreeHo
103b1 6c 64 73 4d 75 74 65 78 28 42 74 72 65 65 20 2a  ldsMutex(Btree *
103b2 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 2d  p){.  return (p-
103b3 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 7c 7c 0a  >sharable==0 ||.
103b4 20 20 20 20 20 20 20 20 20 20 20 20 20 28 70 2d               (p-
103b5 3e 6c 6f 63 6b 65 64 20 26 26 20 70 2d 3e 77 61  >locked && p->wa
103b6 6e 74 54 6f 4c 6f 63 6b 20 26 26 20 73 71 6c 69  ntToLock && sqli
103b7 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
103b8 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 29 29 3b  ->pBt->mutex)));
103b9 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e  .}.#endif...#ifn
103ba 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
103bb 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 45  INCRBLOB./*.** E
103bc 6e 74 65 72 20 61 6e 64 20 6c 65 61 76 65 20 61  nter and leave a
103bd 20 6d 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65   mutex on a Btre
103be 65 20 67 69 76 65 6e 20 61 20 63 75 72 73 6f 72  e given a cursor
103bf 20 6f 77 6e 65 64 20 62 79 20 74 68 61 74 0a 2a   owned by that.*
103c0 2a 20 42 74 72 65 65 2e 20 20 54 68 65 73 65 20  * Btree.  These 
103c1 65 6e 74 72 79 20 70 6f 69 6e 74 73 20 61 72 65  entry points are
103c2 20 75 73 65 64 20 62 79 20 69 6e 63 72 65 6d 65   used by increme
103c3 6e 74 61 6c 20 49 2f 4f 20 61 6e 64 20 63 61 6e  ntal I/O and can
103c4 20 62 65 0a 2a 2a 20 6f 6d 69 74 74 65 64 20 69   be.** omitted i
103c5 66 20 74 68 61 74 20 6d 6f 64 75 6c 65 20 69 73  f that module is
103c6 20 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 53 51   not used..*/.SQ
103c7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
103c8 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e  d sqlite3BtreeEn
103c9 74 65 72 43 75 72 73 6f 72 28 42 74 43 75 72 73  terCursor(BtCurs
103ca 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73 71 6c  or *pCur){.  sql
103cb 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
103cc 43 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a 7d 0a  Cur->pBtree);.}.
103cd 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
103ce 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
103cf 4c 65 61 76 65 43 75 72 73 6f 72 28 42 74 43 75  LeaveCursor(BtCu
103d0 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73  rsor *pCur){.  s
103d1 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
103d2 28 70 43 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a  (pCur->pBtree);.
103d3 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
103d4 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42  TE_OMIT_INCRBLOB
103d5 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65   */.../*.** Ente
103d6 72 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 65  r the mutex on e
103d7 76 65 72 79 20 42 74 72 65 65 20 61 73 73 6f 63  very Btree assoc
103d8 69 61 74 65 64 20 77 69 74 68 20 61 20 64 61 74  iated with a dat
103d9 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74  abase.** connect
103da 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 6e 65  ion.  This is ne
103db 65 64 65 64 20 28 66 6f 72 20 65 78 61 6d 70 6c  eded (for exampl
103dc 65 29 20 70 72 69 6f 72 20 74 6f 20 70 61 72 73  e) prior to pars
103dd 69 6e 67 0a 2a 2a 20 61 20 73 74 61 74 65 6d 65  ing.** a stateme
103de 6e 74 20 73 69 6e 63 65 20 77 65 20 77 69 6c 6c  nt since we will
103df 20 62 65 20 63 6f 6d 70 61 72 69 6e 67 20 74 61   be comparing ta
103e0 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e  ble and column n
103e1 61 6d 65 73 0a 2a 2a 20 61 67 61 69 6e 73 74 20  ames.** against 
103e2 61 6c 6c 20 73 63 68 65 6d 61 73 20 61 6e 64 20  all schemas and 
103e3 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74  we do not want t
103e4 68 6f 73 65 20 73 63 68 65 6d 61 73 20 62 65 69  hose schemas bei
103e5 6e 67 0a 2a 2a 20 72 65 73 65 74 20 6f 75 74 20  ng.** reset out 
103e6 66 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a 2a  from under us..*
103e7 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20  *.** There is a 
103e8 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6c 65  corresponding le
103e9 61 76 65 2d 61 6c 6c 20 70 72 6f 63 65 64 75 72  ave-all procedur
103ea 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 6e 74 65 72 20  es..**.** Enter 
103eb 74 68 65 20 6d 75 74 65 78 65 73 20 69 6e 20 61  the mutexes in a
103ec 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62  ccending order b
103ed 79 20 42 74 53 68 61 72 65 64 20 70 6f 69 6e 74  y BtShared point
103ee 65 72 20 61 64 64 72 65 73 73 0a 2a 2a 20 74 6f  er address.** to
103ef 20 61 76 6f 69 64 20 74 68 65 20 70 6f 73 73 69   avoid the possi
103f0 62 69 6c 69 74 79 20 6f 66 20 64 65 61 64 6c 6f  bility of deadlo
103f1 63 6b 20 77 68 65 6e 20 74 77 6f 20 74 68 72 65  ck when two thre
103f2 61 64 73 20 77 69 74 68 0a 2a 2a 20 74 77 6f 20  ads with.** two 
103f3 6f 72 20 6d 6f 72 65 20 62 74 72 65 65 73 20 69  or more btrees i
103f4 6e 20 63 6f 6d 6d 6f 6e 20 62 6f 74 68 20 74 72  n common both tr
103f5 79 20 74 6f 20 6c 6f 63 6b 20 61 6c 6c 20 74 68  y to lock all th
103f6 65 69 72 20 62 74 72 65 65 73 0a 2a 2a 20 61 74  eir btrees.** at
103f7 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 61 6e   the same instan
103f8 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
103f9 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
103fa 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 73  3BtreeEnterAll(s
103fb 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69  qlite3 *db){.  i
103fc 6e 74 20 69 3b 0a 20 20 42 74 72 65 65 20 2a 70  nt i;.  Btree *p
103fd 2c 20 2a 70 4c 61 74 65 72 3b 0a 20 20 61 73 73  , *pLater;.  ass
103fe 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
103ff 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
10400 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  x) );.  for(i=0;
10401 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
10402 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 44  {.    p = db->aD
10403 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66  b[i].pBt;.    if
10404 28 20 70 20 26 26 20 70 2d 3e 73 68 61 72 61 62  ( p && p->sharab
10405 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 77  le ){.      p->w
10406 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20  antToLock++;.   
10407 20 20 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65     if( !p->locke
10408 64 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  d ){.        ass
10409 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f  ert( p->wantToLo
1040a 63 6b 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20  ck==1 );.       
1040b 20 77 68 69 6c 65 28 20 70 2d 3e 70 50 72 65 76   while( p->pPrev
1040c 20 29 20 70 20 3d 20 70 2d 3e 70 50 72 65 76 3b   ) p = p->pPrev;
1040d 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
1040e 70 2d 3e 6c 6f 63 6b 65 64 20 26 26 20 70 2d 3e  p->locked && p->
1040f 70 4e 65 78 74 20 29 20 70 20 3d 20 70 2d 3e 70  pNext ) p = p->p
10410 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 66 6f  Next;.        fo
10411 72 28 70 4c 61 74 65 72 20 3d 20 70 2d 3e 70 4e  r(pLater = p->pN
10412 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61  ext; pLater; pLa
10413 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78  ter=pLater->pNex
10414 74 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  t){.          if
10415 28 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64  ( pLater->locked
10416 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
10417 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
10418 61 76 65 28 70 4c 61 74 65 72 2d 3e 70 42 74 2d  ave(pLater->pBt-
10419 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20  >mutex);.       
1041a 20 20 20 20 20 70 4c 61 74 65 72 2d 3e 6c 6f 63       pLater->loc
1041b 6b 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ked = 0;.       
1041c 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
1041d 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 20         while( p 
1041e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
1041f 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
10420 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b  (p->pBt->mutex);
10421 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 6c 6f  .          p->lo
10422 63 6b 65 64 2b 2b 3b 0a 20 20 20 20 20 20 20 20  cked++;.        
10423 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a    p = p->pNext;.
10424 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
10425 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 53 51  }.    }.  }.}.SQ
10426 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
10427 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65  d sqlite3BtreeLe
10428 61 76 65 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a  aveAll(sqlite3 *
10429 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  db){.  int i;.  
1042a 42 74 72 65 65 20 2a 70 3b 0a 20 20 61 73 73 65  Btree *p;.  asse
1042b 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1042c 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
1042d 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  ) );.  for(i=0; 
1042e 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
1042f 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 44 62  .    p = db->aDb
10430 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28  [i].pBt;.    if(
10431 20 70 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c   p && p->sharabl
10432 65 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  e ){.      asser
10433 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b  t( p->wantToLock
10434 3e 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 77  >0 );.      p->w
10435 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 20  antToLock--;.   
10436 20 20 20 69 66 28 20 70 2d 3e 77 61 6e 74 54 6f     if( p->wantTo
10437 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Lock==0 ){.     
10438 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f     assert( p->lo
10439 63 6b 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20  cked );.        
1043a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1043b 61 76 65 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65  ave(p->pBt->mute
1043c 78 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6c  x);.        p->l
1043d 6f 63 6b 65 64 20 3d 20 30 3b 0a 20 20 20 20 20  ocked = 0;.     
1043e 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a   }.    }.  }.}..
1043f 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f  #ifndef NDEBUG./
10440 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
10441 20 69 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   if the current 
10442 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68 65  thread holds the
10443 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
10444 74 69 6f 6e 0a 2a 2a 20 6d 75 74 65 78 20 61 6e  tion.** mutex an
10445 64 20 61 6c 6c 20 72 65 71 75 69 72 65 64 20 42  d all required B
10446 74 53 68 61 72 65 64 20 6d 75 74 65 78 65 73 2e  tShared mutexes.
10447 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
10448 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73 69  ine is used insi
10449 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74  de assert() stat
1044a 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a  ements only..*/.
1044b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1044c 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  nt sqlite3BtreeH
1044d 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 73  oldsAllMutexes(s
1044e 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69  qlite3 *db){.  i
1044f 6e 74 20 69 3b 0a 20 20 69 66 28 20 21 73 71 6c  nt i;.  if( !sql
10450 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
10451 64 62 2d 3e 6d 75 74 65 78 29 20 29 7b 0a 20 20  db->mutex) ){.  
10452 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
10453 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
10454 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
10455 42 74 72 65 65 20 2a 70 3b 0a 20 20 20 20 70 20  Btree *p;.    p 
10456 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74  = db->aDb[i].pBt
10457 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 70  ;.    if( p && p
10458 2d 3e 73 68 61 72 61 62 6c 65 20 26 26 0a 20 20  ->sharable &&.  
10459 20 20 20 20 20 20 20 28 70 2d 3e 77 61 6e 74 54         (p->wantT
1045a 6f 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 21 73 71 6c  oLock==0 || !sql
1045b 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1045c 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 29 20  p->pBt->mutex)) 
1045d 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
1045e 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  0;.    }.  }.  r
1045f 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69  eturn 1;.}.#endi
10460 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a  f /* NDEBUG */..
10461 2f 2a 0a 2a 2a 20 50 6f 74 65 6e 74 69 61 6c 6c  /*.** Potentiall
10462 79 20 64 64 20 61 20 6e 65 77 20 42 74 72 65 65  y dd a new Btree
10463 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 42 74   pointer to a Bt
10464 72 65 65 4d 75 74 65 78 41 72 72 61 79 2e 0a 2a  reeMutexArray..*
10465 2a 20 52 65 61 6c 6c 79 20 6f 6e 6c 79 20 61 64  * Really only ad
10466 64 20 74 68 65 20 42 74 72 65 65 20 69 66 20 69  d the Btree if i
10467 74 20 63 61 6e 20 70 6f 73 73 69 62 6c 79 20 62  t can possibly b
10468 65 20 73 68 61 72 65 64 20 77 69 74 68 0a 2a 2a  e shared with.**
10469 20 61 6e 6f 74 68 65 72 20 64 61 74 61 62 61 73   another databas
1046a 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a  e connection..**
1046b 0a 2a 2a 20 54 68 65 20 42 74 72 65 65 73 20 61  .** The Btrees a
1046c 72 65 20 6b 65 70 74 20 69 6e 20 73 6f 72 74 65  re kept in sorte
1046d 64 20 6f 72 64 65 72 20 62 79 20 70 42 74 72 65  d order by pBtre
1046e 65 2d 3e 70 42 74 2e 20 20 54 68 61 74 0a 2a 2a  e->pBt.  That.**
1046f 20 77 61 79 20 77 68 65 6e 20 77 65 20 67 6f 20   way when we go 
10470 74 6f 20 65 6e 74 65 72 20 61 6c 6c 20 74 68 65  to enter all the
10471 20 6d 75 74 65 78 65 73 2c 20 77 65 20 63 61 6e   mutexes, we can
10472 20 65 6e 74 65 72 20 74 68 65 6d 0a 2a 2a 20 69   enter them.** i
10473 6e 20 6f 72 64 65 72 20 77 69 74 68 6f 75 74 20  n order without 
10474 65 76 65 72 79 20 68 61 76 69 6e 67 20 74 6f 20  every having to 
10475 62 61 63 6b 75 70 20 61 6e 64 20 72 65 74 72 79  backup and retry
10476 20 61 6e 64 20 77 69 74 68 6f 75 74 0a 2a 2a 20   and without.** 
10477 77 6f 72 72 79 69 6e 67 20 61 62 6f 75 74 20 64  worrying about d
10478 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54  eadlock..**.** T
10479 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 68 61  he number of sha
1047a 72 65 64 20 62 74 72 65 65 73 20 77 69 6c 6c 20  red btrees will 
1047b 61 6c 77 61 79 73 20 62 65 20 73 6d 61 6c 6c 20  always be small 
1047c 28 75 73 75 61 6c 6c 79 20 30 20 6f 72 20 31 29  (usually 0 or 1)
1047d 0a 2a 2a 20 73 6f 20 61 6e 20 69 6e 73 65 72 74  .** so an insert
1047e 69 6f 6e 20 73 6f 72 74 20 69 73 20 61 6e 20 61  ion sort is an a
1047f 64 65 71 75 61 74 65 20 61 6c 67 6f 72 69 74 68  dequate algorith
10480 6d 20 68 65 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54  m here..*/.SQLIT
10481 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
10482 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78  qlite3BtreeMutex
10483 41 72 72 61 79 49 6e 73 65 72 74 28 42 74 72 65  ArrayInsert(Btre
10484 65 4d 75 74 65 78 41 72 72 61 79 20 2a 70 41 72  eMutexArray *pAr
10485 72 61 79 2c 20 42 74 72 65 65 20 2a 70 42 74 72  ray, Btree *pBtr
10486 65 65 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  ee){.  int i, j;
10487 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
10488 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65 3d 3d  ;.  if( pBtree==
10489 30 20 7c 7c 20 70 42 74 72 65 65 2d 3e 73 68 61  0 || pBtree->sha
1048a 72 61 62 6c 65 3d 3d 30 20 29 20 72 65 74 75 72  rable==0 ) retur
1048b 6e 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  n;.#ifndef NDEBU
1048c 47 0a 20 20 7b 0a 20 20 20 20 66 6f 72 28 69 3d  G.  {.    for(i=
1048d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75  0; i<pArray->nMu
1048e 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  tex; i++){.     
1048f 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d   assert( pArray-
10490 3e 61 42 74 72 65 65 5b 69 5d 21 3d 70 42 74 72  >aBtree[i]!=pBtr
10491 65 65 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ee );.    }.  }.
10492 23 65 6e 64 69 66 0a 20 20 61 73 73 65 72 74 28  #endif.  assert(
10493 20 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3e   pArray->nMutex>
10494 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
10495 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3c 73  pArray->nMutex<s
10496 69 7a 65 6f 66 28 70 41 72 72 61 79 2d 3e 61 42  izeof(pArray->aB
10497 74 72 65 65 29 2f 73 69 7a 65 6f 66 28 70 41 72  tree)/sizeof(pAr
10498 72 61 79 2d 3e 61 42 74 72 65 65 5b 30 5d 29 2d  ray->aBtree[0])-
10499 31 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 42 74  1 );.  pBt = pBt
1049a 72 65 65 2d 3e 70 42 74 3b 0a 20 20 66 6f 72 28  ree->pBt;.  for(
1049b 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e  i=0; i<pArray->n
1049c 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20  Mutex; i++){.   
1049d 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d   assert( pArray-
1049e 3e 61 42 74 72 65 65 5b 69 5d 21 3d 70 42 74 72  >aBtree[i]!=pBtr
1049f 65 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 41  ee );.    if( pA
104a0 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 2d  rray->aBtree[i]-
104a1 3e 70 42 74 3e 70 42 74 20 29 7b 0a 20 20 20 20  >pBt>pBt ){.    
104a2 20 20 66 6f 72 28 6a 3d 70 41 72 72 61 79 2d 3e    for(j=pArray->
104a3 6e 4d 75 74 65 78 3b 20 6a 3e 69 3b 20 6a 2d 2d  nMutex; j>i; j--
104a4 29 7b 0a 20 20 20 20 20 20 20 20 70 41 72 72 61  ){.        pArra
104a5 79 2d 3e 61 42 74 72 65 65 5b 6a 5d 20 3d 20 70  y->aBtree[j] = p
104a6 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 6a 2d  Array->aBtree[j-
104a7 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  1];.      }.    
104a8 20 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65    pArray->aBtree
104a9 5b 69 5d 20 3d 20 70 42 74 72 65 65 3b 0a 20 20  [i] = pBtree;.  
104aa 20 20 20 20 70 41 72 72 61 79 2d 3e 6e 4d 75 74      pArray->nMut
104ab 65 78 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75  ex++;.      retu
104ac 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  rn;.    }.  }.  
104ad 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 70  pArray->aBtree[p
104ae 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 2b 2b 5d  Array->nMutex++]
104af 20 3d 20 70 42 74 72 65 65 3b 0a 7d 0a 0a 2f 2a   = pBtree;.}../*
104b0 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75  .** Enter the mu
104b1 74 65 78 20 6f 66 20 65 76 65 72 79 20 62 74 72  tex of every btr
104b2 65 65 20 69 6e 20 74 68 65 20 61 72 72 61 79 2e  ee in the array.
104b3 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69    This routine i
104b4 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 74 20 74  s.** called at t
104b5 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
104b6 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28  sqlite3VdbeExec(
104b7 29 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73 20  ).  The mutexes 
104b8 61 72 65 0a 2a 2a 20 65 78 69 74 65 64 20 61 74  are.** exited at
104b9 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
104ba 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  same function..*
104bb 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
104bc 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
104bd 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65  eeMutexArrayEnte
104be 72 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61  r(BtreeMutexArra
104bf 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69 6e  y *pArray){.  in
104c0 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  t i;.  for(i=0; 
104c1 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78  i<pArray->nMutex
104c2 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65  ; i++){.    Btre
104c3 65 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e 61  e *p = pArray->a
104c4 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f 2a  Btree[i];.    /*
104c5 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e 69   Some basic sani
104c6 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20  ty checking */. 
104c7 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30 20     assert( i==0 
104c8 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65  || pArray->aBtre
104c9 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e 70  e[i-1]->pBt<p->p
104ca 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  Bt );.    assert
104cb 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20  ( !p->locked || 
104cc 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20  p->wantToLock>0 
104cd 29 3b 0a 0a 20 20 20 20 2f 2a 20 57 65 20 73 68  );..    /* We sh
104ce 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 6f 6c  ould already hol
104cf 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  d a lock on the 
104d0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
104d1 69 6f 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  ion */.    asser
104d2 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
104d3 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74  _held(p->db->mut
104d4 65 78 29 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 77  ex) );..    p->w
104d5 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20  antToLock++;.   
104d6 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20   if( !p->locked 
104d7 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29  && p->sharable )
104d8 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
104d9 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70  mutex_enter(p->p
104da 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20  Bt->mutex);.    
104db 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b    p->locked = 1;
104dc 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
104dd 0a 2a 2a 20 4c 65 61 76 65 20 74 68 65 20 6d 75  .** Leave the mu
104de 74 65 78 20 6f 66 20 65 76 65 72 79 20 62 74 72  tex of every btr
104df 65 65 20 69 6e 20 74 68 65 20 67 72 6f 75 70 2e  ee in the group.
104e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
104e1 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
104e2 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65  treeMutexArrayLe
104e3 61 76 65 28 42 74 72 65 65 4d 75 74 65 78 41 72  ave(BtreeMutexAr
104e4 72 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 20  ray *pArray){.  
104e5 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30  int i;.  for(i=0
104e6 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74  ; i<pArray->nMut
104e7 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74  ex; i++){.    Bt
104e8 72 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79 2d  ree *p = pArray-
104e9 3e 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20  >aBtree[i];.    
104ea 2f 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61  /* Some basic sa
104eb 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f  nity checking */
104ec 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d  .    assert( i==
104ed 30 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 74  0 || pArray->aBt
104ee 72 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d  ree[i-1]->pBt<p-
104ef 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65  >pBt );.    asse
104f0 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c  rt( p->locked ||
104f1 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 3b   !p->sharable );
104f2 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
104f3 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a  wantToLock>0 );.
104f4 0a 20 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c  .    /* We shoul
104f5 64 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61  d already hold a
104f6 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74   lock on the dat
104f7 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
104f8 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
104f9 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
104fa 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ld(p->db->mutex)
104fb 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 77 61 6e 74   );..    p->want
104fc 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 69 66  ToLock--;.    if
104fd 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d  ( p->wantToLock=
104fe 3d 30 20 26 26 20 70 2d 3e 6c 6f 63 6b 65 64 20  =0 && p->locked 
104ff 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
10500 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e  _mutex_leave(p->
10501 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  pBt->mutex);.   
10502 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30     p->locked = 0
10503 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a  ;.    }.  }.}...
10504 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54  #endif  /* SQLIT
10505 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20  E_THREADSAFE && 
10506 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41  !SQLITE_OMIT_SHA
10507 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 2f 2a  RED_CACHE */../*
10508 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
10509 64 20 6f 66 20 62 74 6d 75 74 65 78 2e 63 20 2a  d of btmutex.c *
1050a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1050b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1050c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1050d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
1050e 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65 2e 63  gin file btree.c
1050f 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
10510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10511 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
10512 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20 36  .** 2004 April 6
10513 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
10514 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
10515 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
10516 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
10517 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
10518 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
10519 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
1051a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
1051b 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
1051c 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
1051d 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
1051e 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
1051f 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
10520 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
10521 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
10522 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
10523 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
10524 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
10525 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10526 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10527 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10528 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
10529 2a 2a 20 24 49 64 3a 20 62 74 72 65 65 2e 63 2c  ** $Id: btree.c,
1052a 76 20 31 2e 34 35 38 20 32 30 30 38 2f 30 35 2f  v 1.458 2008/05/
1052b 30 39 20 31 36 3a 35 37 3a 35 31 20 64 61 6e 69  09 16:57:51 dani
1052c 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2a  elk1977 Exp $.**
1052d 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d  .** This file im
1052e 70 6c 65 6d 65 6e 74 73 20 61 20 65 78 74 65 72  plements a exter
1052f 6e 61 6c 20 28 64 69 73 6b 2d 62 61 73 65 64 29  nal (disk-based)
10530 20 64 61 74 61 62 61 73 65 20 75 73 69 6e 67 20   database using 
10531 42 54 72 65 65 73 2e 0a 2a 2a 20 53 65 65 20 74  BTrees..** See t
10532 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e  he header commen
10533 74 20 6f 6e 20 22 62 74 72 65 65 49 6e 74 2e 68  t on "btreeInt.h
10534 22 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  " for additional
10535 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a   information..**
10536 20 49 6e 63 6c 75 64 69 6e 67 20 61 20 64 65 73   Including a des
10537 63 72 69 70 74 69 6f 6e 20 6f 66 20 66 69 6c 65  cription of file
10538 20 66 6f 72 6d 61 74 20 61 6e 64 20 61 6e 20 6f   format and an o
10539 76 65 72 76 69 65 77 20 6f 66 20 6f 70 65 72 61  verview of opera
1053a 74 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  tion..*/../*.** 
1053b 54 68 65 20 68 65 61 64 65 72 20 73 74 72 69 6e  The header strin
1053c 67 20 74 68 61 74 20 61 70 70 65 61 72 73 20 61  g that appears a
1053d 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  t the beginning 
1053e 6f 66 20 65 76 65 72 79 0a 2a 2a 20 53 51 4c 69  of every.** SQLi
1053f 74 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a  te database..*/.
10540 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
10541 72 20 7a 4d 61 67 69 63 48 65 61 64 65 72 5b 5d  r zMagicHeader[]
10542 20 3d 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48   = SQLITE_FILE_H
10543 45 41 44 45 52 3b 0a 0a 2f 2a 0a 2a 2a 20 53 65  EADER;../*.** Se
10544 74 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61  t this global va
10545 72 69 61 62 6c 65 20 74 6f 20 31 20 74 6f 20 65  riable to 1 to e
10546 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20 75 73  nable tracing us
10547 69 6e 67 20 74 68 65 20 54 52 41 43 45 0a 2a 2a  ing the TRACE.**
10548 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 20 53   macro..*/.#if S
10549 51 4c 49 54 45 5f 54 45 53 54 0a 69 6e 74 20 73  QLITE_TEST.int s
1054a 71 6c 69 74 65 33 42 74 72 65 65 54 72 61 63 65  qlite3BtreeTrace
1054b 3d 30 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f 20  =0;  /* True to 
1054c 65 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20 2a  enable tracing *
1054d 2f 0a 23 65 6e 64 69 66 0a 0a 0a 0a 23 69 66 6e  /.#endif....#ifn
1054e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1054f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a  SHARED_CACHE./*.
10550 2a 2a 20 41 20 66 6c 61 67 20 74 6f 20 69 6e 64  ** A flag to ind
10551 69 63 61 74 65 20 77 68 65 74 68 65 72 20 6f 72  icate whether or
10552 20 6e 6f 74 20 73 68 61 72 65 64 20 63 61 63 68   not shared cach
10553 65 20 69 73 20 65 6e 61 62 6c 65 64 2e 20 20 41  e is enabled.  A
10554 6c 73 6f 2c 0a 2a 2a 20 61 20 6c 69 73 74 20 6f  lso,.** a list o
10555 66 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63  f BtShared objec
10556 74 73 20 74 68 61 74 20 61 72 65 20 65 6c 69 67  ts that are elig
10557 69 62 6c 65 20 66 6f 72 20 70 61 72 74 69 63 69  ible for partici
10558 70 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20 73 68 61  pation.** in sha
10559 72 65 64 20 63 61 63 68 65 2e 20 20 54 68 65 20  red cache.  The 
1055a 76 61 72 69 61 62 6c 65 73 20 68 61 76 65 20 66  variables have f
1055b 69 6c 65 20 73 63 6f 70 65 20 64 75 72 69 6e 67  ile scope during
1055c 20 6e 6f 72 6d 61 6c 20 62 75 69 6c 64 73 2c 0a   normal builds,.
1055d 2a 2a 20 62 75 74 20 74 68 65 20 74 65 73 74 20  ** but the test 
1055e 68 61 72 6e 65 73 73 20 6e 65 65 64 73 20 74 6f  harness needs to
1055f 20 61 63 63 65 73 73 20 74 68 65 73 65 20 76 61   access these va
10560 72 69 61 62 6c 65 73 20 73 6f 20 77 65 20 6d 61  riables so we ma
10561 6b 65 20 74 68 65 6d 0a 2a 2a 20 67 6c 6f 62 61  ke them.** globa
10562 6c 20 66 6f 72 20 74 65 73 74 20 62 75 69 6c 64  l for test build
10563 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  s..*/.#ifdef SQL
10564 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
10565 50 52 49 56 41 54 45 20 42 74 53 68 61 72 65 64  PRIVATE BtShared
10566 20 2a 73 71 6c 69 74 65 33 53 68 61 72 65 64 43   *sqlite3SharedC
10567 61 63 68 65 4c 69 73 74 20 3d 20 30 3b 0a 53 51  acheList = 0;.SQ
10568 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
10569 20 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61   sqlite3SharedCa
1056a 63 68 65 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a  cheEnabled = 0;.
1056b 23 65 6c 73 65 0a 73 74 61 74 69 63 20 42 74 53  #else.static BtS
1056c 68 61 72 65 64 20 2a 73 71 6c 69 74 65 33 53 68  hared *sqlite3Sh
1056d 61 72 65 64 43 61 63 68 65 4c 69 73 74 20 3d 20  aredCacheList = 
1056e 30 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  0;.static int sq
1056f 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65  lite3SharedCache
10570 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 23 65 6e  Enabled = 0;.#en
10571 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  dif.#endif /* SQ
10572 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
10573 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64  _CACHE */..#ifnd
10574 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
10575 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a  HARED_CACHE./*.*
10576 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64 69 73 61  * Enable or disa
10577 62 6c 65 20 74 68 65 20 73 68 61 72 65 64 20 70  ble the shared p
10578 61 67 65 72 20 61 6e 64 20 73 63 68 65 6d 61 20  ager and schema 
10579 66 65 61 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20  features..**.** 
1057a 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73  This routine has
1057b 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e 20 65 78   no effect on ex
1057c 69 73 74 69 6e 67 20 64 61 74 61 62 61 73 65 20  isting database 
1057d 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0a 2a 2a 20  connections..** 
1057e 54 68 65 20 73 68 61 72 65 64 20 63 61 63 68 65  The shared cache
1057f 20 73 65 74 74 69 6e 67 20 65 66 66 65 63 74 73   setting effects
10580 20 6f 6e 6c 79 20 66 75 74 75 72 65 20 63 61 6c   only future cal
10581 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33  ls to.** sqlite3
10582 5f 6f 70 65 6e 28 29 2c 20 73 71 6c 69 74 65 33  _open(), sqlite3
10583 5f 6f 70 65 6e 31 36 28 29 2c 20 6f 72 20 73 71  _open16(), or sq
10584 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 2e  lite3_open_v2().
10585 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
10586 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c  nt sqlite3_enabl
10587 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 28 69  e_shared_cache(i
10588 6e 74 20 65 6e 61 62 6c 65 29 7b 0a 20 20 73 71  nt enable){.  sq
10589 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65  lite3SharedCache
1058a 45 6e 61 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65  Enabled = enable
1058b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
1058c 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  E_OK;.}.#endif..
1058d 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 64  ./*.** Forward d
1058e 65 63 6c 61 72 61 74 69 6f 6e 0a 2a 2f 0a 73 74  eclaration.*/.st
1058f 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 52 65  atic int checkRe
10590 61 64 4c 6f 63 6b 73 28 42 74 72 65 65 2a 2c 50  adLocks(Btree*,P
10591 67 6e 6f 2c 42 74 43 75 72 73 6f 72 2a 29 3b 0a  gno,BtCursor*);.
10592 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
10593 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
10594 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20  E.  /*.  ** The 
10595 66 75 6e 63 74 69 6f 6e 73 20 71 75 65 72 79 54  functions queryT
10596 61 62 6c 65 4c 6f 63 6b 28 29 2c 20 6c 6f 63 6b  ableLock(), lock
10597 54 61 62 6c 65 28 29 20 61 6e 64 20 75 6e 6c 6f  Table() and unlo
10598 63 6b 41 6c 6c 54 61 62 6c 65 73 28 29 0a 20 20  ckAllTables().  
10599 2a 2a 20 6d 61 6e 69 70 75 6c 61 74 65 20 65 6e  ** manipulate en
1059a 74 72 69 65 73 20 69 6e 20 74 68 65 20 42 74 53  tries in the BtS
1059b 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c 69 6e 6b  hared.pLock link
1059c 65 64 20 6c 69 73 74 20 75 73 65 64 20 74 6f 20  ed list used to 
1059d 73 74 6f 72 65 0a 20 20 2a 2a 20 73 68 61 72 65  store.  ** share
1059e 64 2d 63 61 63 68 65 20 74 61 62 6c 65 20 6c 65  d-cache table le
1059f 76 65 6c 20 6c 6f 63 6b 73 2e 20 49 66 20 74 68  vel locks. If th
105a0 65 20 6c 69 62 72 61 72 79 20 69 73 20 63 6f 6d  e library is com
105a1 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 20  piled with the. 
105a2 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65   ** shared-cache
105a3 20 66 65 61 74 75 72 65 20 64 69 73 61 62 6c 65   feature disable
105a4 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 73  d, then there is
105a5 20 6f 6e 6c 79 20 65 76 65 72 20 6f 6e 65 20 75   only ever one u
105a6 73 65 72 0a 20 20 2a 2a 20 6f 66 20 65 61 63 68  ser.  ** of each
105a7 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74   BtShared struct
105a8 75 72 65 20 61 6e 64 20 73 6f 20 74 68 69 73 20  ure and so this 
105a9 6c 6f 63 6b 69 6e 67 20 69 73 20 6e 6f 74 20 6e  locking is not n
105aa 65 63 65 73 73 61 72 79 2e 20 0a 20 20 2a 2a 20  ecessary. .  ** 
105ab 53 6f 20 64 65 66 69 6e 65 20 74 68 65 20 6c 6f  So define the lo
105ac 63 6b 20 72 65 6c 61 74 65 64 20 66 75 6e 63 74  ck related funct
105ad 69 6f 6e 73 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a  ions as no-ops..
105ae 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 71    */.  #define q
105af 75 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 61 2c  ueryTableLock(a,
105b0 62 2c 63 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20  b,c) SQLITE_OK. 
105b1 20 23 64 65 66 69 6e 65 20 6c 6f 63 6b 54 61 62   #define lockTab
105b2 6c 65 28 61 2c 62 2c 63 29 20 53 51 4c 49 54 45  le(a,b,c) SQLITE
105b3 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 75 6e  _OK.  #define un
105b4 6c 6f 63 6b 41 6c 6c 54 61 62 6c 65 73 28 61 29  lockAllTables(a)
105b5 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
105b6 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
105b7 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20  RED_CACHE./*.** 
105b8 51 75 65 72 79 20 74 6f 20 73 65 65 20 69 66 20  Query to see if 
105b9 62 74 72 65 65 20 68 61 6e 64 6c 65 20 70 20 6d  btree handle p m
105ba 61 79 20 6f 62 74 61 69 6e 20 61 20 6c 6f 63 6b  ay obtain a lock
105bb 20 6f 66 20 74 79 70 65 20 65 4c 6f 63 6b 20 0a   of type eLock .
105bc 2a 2a 20 28 52 45 41 44 5f 4c 4f 43 4b 20 6f 72  ** (READ_LOCK or
105bd 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 6f 6e 20   WRITE_LOCK) on 
105be 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 72  the table with r
105bf 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 2e 20 52  oot-page iTab. R
105c0 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f  eturn.** SQLITE_
105c1 4f 4b 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 6d  OK if the lock m
105c2 61 79 20 62 65 20 6f 62 74 61 69 6e 65 64 20 28  ay be obtained (
105c3 62 79 20 63 61 6c 6c 69 6e 67 20 6c 6f 63 6b 54  by calling lockT
105c4 61 62 6c 65 28 29 29 2c 20 6f 72 0a 2a 2a 20 53  able()), or.** S
105c5 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20  QLITE_LOCKED if 
105c6 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  not..*/.static i
105c7 6e 74 20 71 75 65 72 79 54 61 62 6c 65 4c 6f 63  nt queryTableLoc
105c8 6b 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f  k(Btree *p, Pgno
105c9 20 69 54 61 62 2c 20 75 38 20 65 4c 6f 63 6b 29   iTab, u8 eLock)
105ca 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  {.  BtShared *pB
105cb 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74  t = p->pBt;.  Bt
105cc 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 0a 20 20  Lock *pIter;..  
105cd 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
105ce 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70  treeHoldsMutex(p
105cf 29 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 54 68 69  ) );.  .  /* Thi
105d0 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20  s is a no-op if 
105d1 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 65  the shared-cache
105d2 20 69 73 20 6e 6f 74 20 65 6e 61 62 6c 65 64 20   is not enabled 
105d3 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 61  */.  if( !p->sha
105d4 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 72 65 74  rable ){.    ret
105d5 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
105d6 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d 65   }..  /* If some
105d7 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f   other connectio
105d8 6e 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 6e 20  n is holding an 
105d9 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2c 20  exclusive lock, 
105da 74 68 65 0a 20 20 2a 2a 20 72 65 71 75 65 73 74  the.  ** request
105db 65 64 20 6c 6f 63 6b 20 6d 61 79 20 6e 6f 74 20  ed lock may not 
105dc 62 65 20 6f 62 74 61 69 6e 65 64 2e 0a 20 20 2a  be obtained..  *
105dd 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 45 78  /.  if( pBt->pEx
105de 63 6c 75 73 69 76 65 20 26 26 20 70 42 74 2d 3e  clusive && pBt->
105df 70 45 78 63 6c 75 73 69 76 65 21 3d 70 20 29 7b  pExclusive!=p ){
105e0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
105e1 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 7d 0a 0a  TE_LOCKED;.  }..
105e2 20 20 2f 2a 20 54 68 69 73 20 28 61 6c 6f 6e 67    /* This (along
105e3 20 77 69 74 68 20 6c 6f 63 6b 54 61 62 6c 65 28   with lockTable(
105e4 29 29 20 69 73 20 77 68 65 72 65 20 74 68 65 20  )) is where the 
105e5 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20  ReadUncommitted 
105e6 66 6c 61 67 20 69 73 0a 20 20 2a 2a 20 64 65 61  flag is.  ** dea
105e7 6c 74 20 77 69 74 68 2e 20 49 66 20 74 68 65 20  lt with. If the 
105e8 63 61 6c 6c 65 72 20 69 73 20 71 75 65 72 79 69  caller is queryi
105e9 6e 67 20 66 6f 72 20 61 20 72 65 61 64 2d 6c 6f  ng for a read-lo
105ea 63 6b 20 61 6e 64 20 74 68 65 20 66 6c 61 67 20  ck and the flag 
105eb 69 73 0a 20 20 2a 2a 20 73 65 74 2c 20 69 74 20  is.  ** set, it 
105ec 69 73 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c  is unconditional
105ed 6c 79 20 67 72 61 6e 74 65 64 20 2d 20 65 76 65  ly granted - eve
105ee 6e 20 69 66 20 74 68 65 72 65 20 61 72 65 20 77  n if there are w
105ef 72 69 74 65 2d 6c 6f 63 6b 73 0a 20 20 2a 2a 20  rite-locks.  ** 
105f0 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 49 66  on the table. If
105f1 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 69 73   a write-lock is
105f2 20 72 65 71 75 65 73 74 65 64 2c 20 74 68 65 20   requested, the 
105f3 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20  ReadUncommitted 
105f4 66 6c 61 67 0a 20 20 2a 2a 20 69 73 20 6e 6f 74  flag.  ** is not
105f5 20 63 6f 6e 73 69 64 65 72 65 64 2e 0a 20 20 2a   considered..  *
105f6 2a 0a 20 20 2a 2a 20 49 6e 20 66 75 6e 63 74 69  *.  ** In functi
105f7 6f 6e 20 6c 6f 63 6b 54 61 62 6c 65 28 29 2c 20  on lockTable(), 
105f8 69 66 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69  if a read-lock i
105f9 73 20 64 65 6d 61 6e 64 65 64 20 61 6e 64 20 74  s demanded and t
105fa 68 65 20 0a 20 20 2a 2a 20 52 65 61 64 55 6e 63  he .  ** ReadUnc
105fb 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 69 73  ommitted flag is
105fc 20 73 65 74 2c 20 6e 6f 20 65 6e 74 72 79 20 69   set, no entry i
105fd 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 6c  s added to the l
105fe 6f 63 6b 73 20 6c 69 73 74 20 0a 20 20 2a 2a 20  ocks list .  ** 
105ff 28 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 29  (BtShared.pLock)
10600 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 6f 20 73  ..  **.  ** To s
10601 75 6d 6d 61 72 69 7a 65 3a 20 49 66 20 74 68 65  ummarize: If the
10602 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64   ReadUncommitted
10603 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68   flag is set, th
10604 65 6e 20 72 65 61 64 20 63 75 72 73 6f 72 73 20  en read cursors 
10605 64 6f 0a 20 20 2a 2a 20 6e 6f 74 20 63 72 65 61  do.  ** not crea
10606 74 65 20 6f 72 20 72 65 73 70 65 63 74 20 74 61  te or respect ta
10607 62 6c 65 20 6c 6f 63 6b 73 2e 20 54 68 65 20 6c  ble locks. The l
10608 6f 63 6b 69 6e 67 20 70 72 6f 63 65 64 75 72 65  ocking procedure
10609 20 66 6f 72 20 61 20 0a 20 20 2a 2a 20 77 72 69   for a .  ** wri
1060a 74 65 2d 63 75 72 73 6f 72 20 64 6f 65 73 20 6e  te-cursor does n
1060b 6f 74 20 63 68 61 6e 67 65 2e 0a 20 20 2a 2f 0a  ot change..  */.
1060c 20 20 69 66 28 20 0a 20 20 20 20 21 70 2d 3e 64    if( .    !p->d
1060d 62 20 7c 7c 20 0a 20 20 20 20 30 3d 3d 28 70 2d  b || .    0==(p-
1060e 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54  >db->flags&SQLIT
1060f 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65  E_ReadUncommitte
10610 64 29 20 7c 7c 20 0a 20 20 20 20 65 4c 6f 63 6b  d) || .    eLock
10611 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 7c 7c 0a  ==WRITE_LOCK ||.
10612 20 20 20 20 69 54 61 62 3d 3d 4d 41 53 54 45 52      iTab==MASTER
10613 5f 52 4f 4f 54 0a 20 20 29 7b 0a 20 20 20 20 66  _ROOT.  ){.    f
10614 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c  or(pIter=pBt->pL
10615 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65  ock; pIter; pIte
10616 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b  r=pIter->pNext){
10617 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65 72  .      if( pIter
10618 2d 3e 70 42 74 72 65 65 21 3d 70 20 26 26 20 70  ->pBtree!=p && p
10619 49 74 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54  Iter->iTable==iT
1061a 61 62 20 26 26 20 0a 20 20 20 20 20 20 20 20 20  ab && .         
1061b 20 28 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d   (pIter->eLock!=
1061c 65 4c 6f 63 6b 20 7c 7c 20 65 4c 6f 63 6b 21 3d  eLock || eLock!=
1061d 52 45 41 44 5f 4c 4f 43 4b 29 20 29 7b 0a 20 20  READ_LOCK) ){.  
1061e 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
1061f 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20 20  ITE_LOCKED;.    
10620 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
10621 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
10622 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53  ;.}.#endif /* !S
10623 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
10624 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e  D_CACHE */..#ifn
10625 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
10626 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a  SHARED_CACHE./*.
10627 2a 2a 20 41 64 64 20 61 20 6c 6f 63 6b 20 6f 6e  ** Add a lock on
10628 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 20   the table with 
10629 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 6c 65  root-page iTable
1062a 20 74 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62   to the shared-b
1062b 74 72 65 65 20 75 73 65 64 0a 2a 2a 20 62 79 20  tree used.** by 
1062c 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 20  Btree handle p. 
1062d 50 61 72 61 6d 65 74 65 72 20 65 4c 6f 63 6b 20  Parameter eLock 
1062e 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 52  must be either R
1062f 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 0a 2a 2a 20  EAD_LOCK or .** 
10630 57 52 49 54 45 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a  WRITE_LOCK..**.*
10631 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  * SQLITE_OK is r
10632 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 6c  eturned if the l
10633 6f 63 6b 20 69 73 20 61 64 64 65 64 20 73 75 63  ock is added suc
10634 63 65 73 73 66 75 6c 6c 79 2e 20 53 51 4c 49 54  cessfully. SQLIT
10635 45 5f 42 55 53 59 20 61 6e 64 0a 2a 2a 20 53 51  E_BUSY and.** SQ
10636 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6d 61 79 20 61  LITE_NOMEM may a
10637 6c 73 6f 20 62 65 20 72 65 74 75 72 6e 65 64 2e  lso be returned.
10638 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
10639 6f 63 6b 54 61 62 6c 65 28 42 74 72 65 65 20 2a  ockTable(Btree *
1063a 70 2c 20 50 67 6e 6f 20 69 54 61 62 6c 65 2c 20  p, Pgno iTable, 
1063b 75 38 20 65 4c 6f 63 6b 29 7b 0a 20 20 42 74 53  u8 eLock){.  BtS
1063c 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
1063d 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70  pBt;.  BtLock *p
1063e 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 42 74 4c 6f  Lock = 0;.  BtLo
1063f 63 6b 20 2a 70 49 74 65 72 3b 0a 0a 20 20 61 73  ck *pIter;..  as
10640 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
10641 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20  eeHoldsMutex(p) 
10642 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 69 73  );..  /* This is
10643 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20   a no-op if the 
10644 73 68 61 72 65 64 2d 63 61 63 68 65 20 69 73 20  shared-cache is 
10645 6e 6f 74 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20  not enabled */. 
10646 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c   if( !p->sharabl
10647 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
10648 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a  SQLITE_OK;.  }..
10649 20 20 61 73 73 65 72 74 28 20 53 51 4c 49 54 45    assert( SQLITE
1064a 5f 4f 4b 3d 3d 71 75 65 72 79 54 61 62 6c 65 4c  _OK==queryTableL
1064b 6f 63 6b 28 70 2c 20 69 54 61 62 6c 65 2c 20 65  ock(p, iTable, e
1064c 4c 6f 63 6b 29 20 29 3b 0a 0a 20 20 2f 2a 20 49  Lock) );..  /* I
1064d 66 20 74 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d  f the read-uncom
1064e 6d 69 74 74 65 64 20 66 6c 61 67 20 69 73 20 73  mitted flag is s
1064f 65 74 20 61 6e 64 20 61 20 72 65 61 64 2d 6c 6f  et and a read-lo
10650 63 6b 20 69 73 20 72 65 71 75 65 73 74 65 64 2c  ck is requested,
10651 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 65 61 72  .  ** return ear
10652 6c 79 20 77 69 74 68 6f 75 74 20 61 64 64 69 6e  ly without addin
10653 67 20 61 6e 20 65 6e 74 72 79 20 74 6f 20 74 68  g an entry to th
10654 65 20 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b  e BtShared.pLock
10655 20 6c 69 73 74 2e 20 53 65 65 0a 20 20 2a 2a 20   list. See.  ** 
10656 63 6f 6d 6d 65 6e 74 20 69 6e 20 66 75 6e 63 74  comment in funct
10657 69 6f 6e 20 71 75 65 72 79 54 61 62 6c 65 4c 6f  ion queryTableLo
10658 63 6b 28 29 20 66 6f 72 20 6d 6f 72 65 20 69 6e  ck() for more in
10659 66 6f 20 6f 6e 20 68 61 6e 64 6c 69 6e 67 20 0a  fo on handling .
1065a 20 20 2a 2a 20 74 68 65 20 52 65 61 64 55 6e 63    ** the ReadUnc
1065b 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 2e 0a 20  ommitted flag.. 
1065c 20 2a 2f 0a 20 20 69 66 28 20 0a 20 20 20 20 28   */.  if( .    (
1065d 70 2d 3e 64 62 29 20 26 26 20 0a 20 20 20 20 28  p->db) && .    (
1065e 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c  p->db->flags&SQL
1065f 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74  ITE_ReadUncommit
10660 74 65 64 29 20 26 26 20 0a 20 20 20 20 28 65 4c  ted) && .    (eL
10661 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 29 20  ock==READ_LOCK) 
10662 26 26 0a 20 20 20 20 69 54 61 62 6c 65 21 3d 4d  &&.    iTable!=M
10663 41 53 54 45 52 5f 52 4f 4f 54 0a 20 20 29 7b 0a  ASTER_ROOT.  ){.
10664 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
10665 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  E_OK;.  }..  /* 
10666 46 69 72 73 74 20 73 65 61 72 63 68 20 74 68 65  First search the
10667 20 6c 69 73 74 20 66 6f 72 20 61 6e 20 65 78 69   list for an exi
10668 73 74 69 6e 67 20 6c 6f 63 6b 20 6f 6e 20 74 68  sting lock on th
10669 69 73 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 66  is table. */.  f
1066a 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c  or(pIter=pBt->pL
1066b 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65  ock; pIter; pIte
1066c 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b  r=pIter->pNext){
1066d 0a 20 20 20 20 69 66 28 20 70 49 74 65 72 2d 3e  .    if( pIter->
1066e 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c 65 20 26  iTable==iTable &
1066f 26 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 3d  & pIter->pBtree=
10670 3d 70 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63  =p ){.      pLoc
10671 6b 20 3d 20 70 49 74 65 72 3b 0a 20 20 20 20 20  k = pIter;.     
10672 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
10673 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 61  }..  /* If the a
10674 62 6f 76 65 20 73 65 61 72 63 68 20 64 69 64 20  bove search did 
10675 6e 6f 74 20 66 69 6e 64 20 61 20 42 74 4c 6f 63  not find a BtLoc
10676 6b 20 73 74 72 75 63 74 20 61 73 73 6f 63 69 61  k struct associa
10677 74 69 6e 67 20 42 74 72 65 65 20 70 0a 20 20 2a  ting Btree p.  *
10678 2a 20 77 69 74 68 20 74 61 62 6c 65 20 69 54 61  * with table iTa
10679 62 6c 65 2c 20 61 6c 6c 6f 63 61 74 65 20 6f 6e  ble, allocate on
1067a 65 20 61 6e 64 20 6c 69 6e 6b 20 69 74 20 69 6e  e and link it in
1067b 74 6f 20 74 68 65 20 6c 69 73 74 2e 0a 20 20 2a  to the list..  *
1067c 2f 0a 20 20 69 66 28 20 21 70 4c 6f 63 6b 20 29  /.  if( !pLock )
1067d 7b 0a 20 20 20 20 70 4c 6f 63 6b 20 3d 20 28 42  {.    pLock = (B
1067e 74 4c 6f 63 6b 20 2a 29 73 71 6c 69 74 65 33 4d  tLock *)sqlite3M
1067f 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66  allocZero(sizeof
10680 28 42 74 4c 6f 63 6b 29 29 3b 0a 20 20 20 20 69  (BtLock));.    i
10681 66 28 20 21 70 4c 6f 63 6b 20 29 7b 0a 20 20 20  f( !pLock ){.   
10682 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
10683 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
10684 20 20 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 20    pLock->iTable 
10685 3d 20 69 54 61 62 6c 65 3b 0a 20 20 20 20 70 4c  = iTable;.    pL
10686 6f 63 6b 2d 3e 70 42 74 72 65 65 20 3d 20 70 3b  ock->pBtree = p;
10687 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78  .    pLock->pNex
10688 74 20 3d 20 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a  t = pBt->pLock;.
10689 20 20 20 20 70 42 74 2d 3e 70 4c 6f 63 6b 20 3d      pBt->pLock =
1068a 20 70 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f   pLock;.  }..  /
1068b 2a 20 53 65 74 20 74 68 65 20 42 74 4c 6f 63 6b  * Set the BtLock
1068c 2e 65 4c 6f 63 6b 20 76 61 72 69 61 62 6c 65 20  .eLock variable 
1068d 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f  to the maximum o
1068e 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f  f the current lo
1068f 63 6b 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20  ck.  ** and the 
10690 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 2e 20  requested lock. 
10691 54 68 69 73 20 6d 65 61 6e 73 20 69 66 20 61 20  This means if a 
10692 77 72 69 74 65 2d 6c 6f 63 6b 20 77 61 73 20 61  write-lock was a
10693 6c 72 65 61 64 79 20 68 65 6c 64 0a 20 20 2a 2a  lready held.  **
10694 20 61 6e 64 20 61 20 72 65 61 64 2d 6c 6f 63 6b   and a read-lock
10695 20 72 65 71 75 65 73 74 65 64 2c 20 77 65 20 64   requested, we d
10696 6f 6e 27 74 20 69 6e 63 6f 72 72 65 63 74 6c 79  on't incorrectly
10697 20 64 6f 77 6e 67 72 61 64 65 20 74 68 65 20 6c   downgrade the l
10698 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ock..  */.  asse
10699 72 74 28 20 57 52 49 54 45 5f 4c 4f 43 4b 3e 52  rt( WRITE_LOCK>R
1069a 45 41 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66  EAD_LOCK );.  if
1069b 28 20 65 4c 6f 63 6b 3e 70 4c 6f 63 6b 2d 3e 65  ( eLock>pLock->e
1069c 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63  Lock ){.    pLoc
1069d 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 65 4c 6f 63 6b  k->eLock = eLock
1069e 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
1069f 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e  SQLITE_OK;.}.#en
106a0 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f  dif /* !SQLITE_O
106a1 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
106a2 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
106a3 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
106a4 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 52 65 6c 65  CACHE./*.** Rele
106a5 61 73 65 20 61 6c 6c 20 74 68 65 20 74 61 62 6c  ase all the tabl
106a6 65 20 6c 6f 63 6b 73 20 28 6c 6f 63 6b 73 20 6f  e locks (locks o
106a7 62 74 61 69 6e 65 64 20 76 69 61 20 63 61 6c 6c  btained via call
106a8 73 20 74 6f 20 74 68 65 20 6c 6f 63 6b 54 61 62  s to the lockTab
106a9 6c 65 28 29 0a 2a 2a 20 70 72 6f 63 65 64 75 72  le().** procedur
106aa 65 29 20 68 65 6c 64 20 62 79 20 42 74 72 65 65  e) held by Btree
106ab 20 68 61 6e 64 6c 65 20 70 2e 0a 2a 2f 0a 73 74   handle p..*/.st
106ac 61 74 69 63 20 76 6f 69 64 20 75 6e 6c 6f 63 6b  atic void unlock
106ad 41 6c 6c 54 61 62 6c 65 73 28 42 74 72 65 65 20  AllTables(Btree 
106ae 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  *p){.  BtShared 
106af 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20  *pBt = p->pBt;. 
106b0 20 42 74 4c 6f 63 6b 20 2a 2a 70 70 49 74 65 72   BtLock **ppIter
106b1 20 3d 20 26 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a   = &pBt->pLock;.
106b2 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
106b3 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65  e3BtreeHoldsMute
106b4 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74  x(p) );.  assert
106b5 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c  ( p->sharable ||
106b6 20 30 3d 3d 2a 70 70 49 74 65 72 20 29 3b 0a 0a   0==*ppIter );..
106b7 20 20 77 68 69 6c 65 28 20 2a 70 70 49 74 65 72    while( *ppIter
106b8 20 29 7b 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a   ){.    BtLock *
106b9 70 4c 6f 63 6b 20 3d 20 2a 70 70 49 74 65 72 3b  pLock = *ppIter;
106ba 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74  .    assert( pBt
106bb 2d 3e 70 45 78 63 6c 75 73 69 76 65 3d 3d 30 20  ->pExclusive==0 
106bc 7c 7c 20 70 42 74 2d 3e 70 45 78 63 6c 75 73 69  || pBt->pExclusi
106bd 76 65 3d 3d 70 4c 6f 63 6b 2d 3e 70 42 74 72 65  ve==pLock->pBtre
106be 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f  e );.    if( pLo
106bf 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b  ck->pBtree==p ){
106c0 0a 20 20 20 20 20 20 2a 70 70 49 74 65 72 20 3d  .      *ppIter =
106c1 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20   pLock->pNext;. 
106c2 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
106c3 65 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 7d 65  e(pLock);.    }e
106c4 6c 73 65 7b 0a 20 20 20 20 20 20 70 70 49 74 65  lse{.      ppIte
106c5 72 20 3d 20 26 70 4c 6f 63 6b 2d 3e 70 4e 65 78  r = &pLock->pNex
106c6 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  t;.    }.  }..  
106c7 69 66 28 20 70 42 74 2d 3e 70 45 78 63 6c 75 73  if( pBt->pExclus
106c8 69 76 65 3d 3d 70 20 29 7b 0a 20 20 20 20 70 42  ive==p ){.    pB
106c9 74 2d 3e 70 45 78 63 6c 75 73 69 76 65 20 3d 20  t->pExclusive = 
106ca 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20  0;.  }.}.#endif 
106cb 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  /* SQLITE_OMIT_S
106cc 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a  HARED_CACHE */..
106cd 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65  static void rele
106ce 61 73 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20  asePage(MemPage 
106cf 2a 70 50 61 67 65 29 3b 20 20 2f 2a 20 46 6f 72  *pPage);  /* For
106d0 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 20 2a  ward reference *
106d1 2f 0a 0a 2f 2a 0a 2a 2a 20 56 65 72 69 66 79 20  /../*.** Verify 
106d2 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20  that the cursor 
106d3 68 6f 6c 64 73 20 61 20 6d 75 74 65 78 20 6f 6e  holds a mutex on
106d4 20 74 68 65 20 42 74 53 68 61 72 65 64 0a 2a 2f   the BtShared.*/
106d5 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
106d6 73 74 61 74 69 63 20 69 6e 74 20 63 75 72 73 6f  static int curso
106d7 72 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 43 75  rHoldsMutex(BtCu
106d8 72 73 6f 72 20 2a 70 29 7b 0a 20 20 72 65 74 75  rsor *p){.  retu
106d9 72 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  rn sqlite3_mutex
106da 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75  _held(p->pBt->mu
106db 74 65 78 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  tex);.}.#endif..
106dc 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
106dd 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a  OMIT_INCRBLOB./*
106de 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 74  .** Invalidate t
106df 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  he overflow page
106e0 2d 6c 69 73 74 20 63 61 63 68 65 20 66 6f 72 20  -list cache for 
106e1 63 75 72 73 6f 72 20 70 43 75 72 2c 20 69 66 20  cursor pCur, if 
106e2 61 6e 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  any..*/.static v
106e3 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 4f 76  oid invalidateOv
106e4 65 72 66 6c 6f 77 43 61 63 68 65 28 42 74 43 75  erflowCache(BtCu
106e5 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61  rsor *pCur){.  a
106e6 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
106e7 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
106e8 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
106e9 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 29  pCur->aOverflow)
106ea 3b 0a 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 66  ;.  pCur->aOverf
106eb 6c 6f 77 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  low = 0;.}../*.*
106ec 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 74 68 65  * Invalidate the
106ed 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c   overflow page-l
106ee 69 73 74 20 63 61 63 68 65 20 66 6f 72 20 61 6c  ist cache for al
106ef 6c 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64  l cursors opened
106f0 0a 2a 2a 20 6f 6e 20 74 68 65 20 73 68 61 72 65  .** on the share
106f1 64 20 62 74 72 65 65 20 73 74 72 75 63 74 75 72  d btree structur
106f2 65 20 70 42 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  e pBt..*/.static
106f3 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65   void invalidate
106f4 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65  AllOverflowCache
106f5 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b  (BtShared *pBt){
106f6 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a  .  BtCursor *p;.
106f7 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
106f8 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
106f9 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f  ->mutex) );.  fo
106fa 72 28 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72  r(p=pBt->pCursor
106fb 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29  ; p; p=p->pNext)
106fc 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65  {.    invalidate
106fd 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 29  OverflowCache(p)
106fe 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 20 20  ;.  }.}.#else.  
106ff 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69 64 61  #define invalida
10700 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28  teOverflowCache(
10701 78 29 0a 20 20 23 64 65 66 69 6e 65 20 69 6e 76  x).  #define inv
10702 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c  alidateAllOverfl
10703 6f 77 43 61 63 68 65 28 78 29 0a 23 65 6e 64 69  owCache(x).#endi
10704 66 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20 74 68  f../*.** Save th
10705 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72  e current cursor
10706 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 65   position in the
10707 20 76 61 72 69 61 62 6c 65 73 20 42 74 43 75 72   variables BtCur
10708 73 6f 72 2e 6e 4b 65 79 20 0a 2a 2a 20 61 6e 64  sor.nKey .** and
10709 20 42 74 43 75 72 73 6f 72 2e 70 4b 65 79 2e 20   BtCursor.pKey. 
1070a 54 68 65 20 63 75 72 73 6f 72 27 73 20 73 74 61  The cursor's sta
1070b 74 65 20 69 73 20 73 65 74 20 74 6f 20 43 55 52  te is set to CUR
1070c 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 2e  SOR_REQUIRESEEK.
1070d 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
1070e 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f  aveCursorPositio
1070f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  n(BtCursor *pCur
10710 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  ){.  int rc;..  
10711 61 73 73 65 72 74 28 20 43 55 52 53 4f 52 5f 56  assert( CURSOR_V
10712 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61  ALID==pCur->eSta
10713 74 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  te );.  assert( 
10714 30 3d 3d 70 43 75 72 2d 3e 70 4b 65 79 20 29 3b  0==pCur->pKey );
10715 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
10716 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
10717 29 20 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c  ) );..  rc = sql
10718 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65  ite3BtreeKeySize
10719 28 70 43 75 72 2c 20 26 70 43 75 72 2d 3e 6e 4b  (pCur, &pCur->nK
1071a 65 79 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  ey);..  /* If th
1071b 69 73 20 69 73 20 61 6e 20 69 6e 74 4b 65 79 20  is is an intKey 
1071c 74 61 62 6c 65 2c 20 74 68 65 6e 20 74 68 65 20  table, then the 
1071d 61 62 6f 76 65 20 63 61 6c 6c 20 74 6f 20 42 74  above call to Bt
1071e 72 65 65 4b 65 79 53 69 7a 65 28 29 0a 20 20 2a  reeKeySize().  *
1071f 2a 20 73 74 6f 72 65 73 20 74 68 65 20 69 6e 74  * stores the int
10720 65 67 65 72 20 6b 65 79 20 69 6e 20 70 43 75 72  eger key in pCur
10721 2d 3e 6e 4b 65 79 2e 20 49 6e 20 74 68 69 73 20  ->nKey. In this 
10722 63 61 73 65 20 74 68 69 73 20 76 61 6c 75 65 20  case this value 
10723 69 73 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74  is.  ** all that
10724 20 69 73 20 72 65 71 75 69 72 65 64 2e 20 4f 74   is required. Ot
10725 68 65 72 77 69 73 65 2c 20 69 66 20 70 43 75 72  herwise, if pCur
10726 20 69 73 20 6e 6f 74 20 6f 70 65 6e 20 6f 6e 20   is not open on 
10727 61 6e 20 69 6e 74 4b 65 79 0a 20 20 2a 2a 20 74  an intKey.  ** t
10728 61 62 6c 65 2c 20 74 68 65 6e 20 6d 61 6c 6c 6f  able, then mallo
10729 63 20 73 70 61 63 65 20 66 6f 72 20 61 6e 64 20  c space for and 
1072a 73 74 6f 72 65 20 74 68 65 20 70 43 75 72 2d 3e  store the pCur->
1072b 6e 4b 65 79 20 62 79 74 65 73 20 6f 66 20 6b 65  nKey bytes of ke
1072c 79 20 0a 20 20 2a 2a 20 64 61 74 61 2e 0a 20 20  y .  ** data..  
1072d 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  */.  if( rc==SQL
1072e 49 54 45 5f 4f 4b 20 26 26 20 30 3d 3d 70 43 75  ITE_OK && 0==pCu
1072f 72 2d 3e 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79  r->pPage->intKey
10730 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 4b 65  ){.    void *pKe
10731 79 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  y = sqlite3_mall
10732 6f 63 28 70 43 75 72 2d 3e 6e 4b 65 79 29 3b 0a  oc(pCur->nKey);.
10733 20 20 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a      if( pKey ){.
10734 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
10735 65 33 42 74 72 65 65 4b 65 79 28 70 43 75 72 2c  e3BtreeKey(pCur,
10736 20 30 2c 20 70 43 75 72 2d 3e 6e 4b 65 79 2c 20   0, pCur->nKey, 
10737 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 69 66 28  pKey);.      if(
10738 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
10739 7b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e  {.        pCur->
1073a 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20  pKey = pKey;.   
1073b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1073c 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
1073d 4b 65 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Key);.      }.  
1073e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
1073f 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
10740 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73  ;.    }.  }.  as
10741 73 65 72 74 28 20 21 70 43 75 72 2d 3e 70 50 61  sert( !pCur->pPa
10742 67 65 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 21 70  ge->intKey || !p
10743 43 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 0a 20 20  Cur->pKey );..  
10744 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
10745 4b 20 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65  K ){.    release
10746 50 61 67 65 28 70 43 75 72 2d 3e 70 50 61 67 65  Page(pCur->pPage
10747 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 70 50 61  );.    pCur->pPa
10748 67 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72  ge = 0;.    pCur
10749 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f  ->eState = CURSO
1074a 52 5f 52 45 51 55 49 52 45 53 45 45 4b 3b 0a 20  R_REQUIRESEEK;. 
1074b 20 7d 0a 0a 20 20 69 6e 76 61 6c 69 64 61 74 65   }..  invalidate
1074c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 43  OverflowCache(pC
1074d 75 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ur);.  return rc
1074e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20  ;.}../*.** Save 
1074f 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66  the positions of
10750 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 65 78 63   all cursors exc
10751 65 70 74 20 70 45 78 63 65 70 74 20 6f 70 65 6e  ept pExcept open
10752 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 0a 2a   on the table .*
10753 2a 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65  * with root-page
10754 20 69 52 6f 6f 74 2e 20 55 73 75 61 6c 6c 79 2c   iRoot. Usually,
10755 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20   this is called 
10756 6a 75 73 74 20 62 65 66 6f 72 65 20 63 75 72 73  just before curs
10757 6f 72 0a 2a 2a 20 70 45 78 63 65 70 74 20 69 73  or.** pExcept is
10758 20 75 73 65 64 20 74 6f 20 6d 6f 64 69 66 79 20   used to modify 
10759 74 68 65 20 74 61 62 6c 65 20 28 42 74 72 65 65  the table (Btree
1075a 44 65 6c 65 74 65 28 29 20 6f 72 20 42 74 72 65  Delete() or Btre
1075b 65 49 6e 73 65 72 74 28 29 29 2e 0a 2a 2f 0a 73  eInsert())..*/.s
1075c 74 61 74 69 63 20 69 6e 74 20 73 61 76 65 41 6c  tatic int saveAl
1075d 6c 43 75 72 73 6f 72 73 28 42 74 53 68 61 72 65  lCursors(BtShare
1075e 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 69 52 6f  d *pBt, Pgno iRo
1075f 6f 74 2c 20 42 74 43 75 72 73 6f 72 20 2a 70 45  ot, BtCursor *pE
10760 78 63 65 70 74 29 7b 0a 20 20 42 74 43 75 72 73  xcept){.  BtCurs
10761 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28  or *p;.  assert(
10762 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
10763 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
10764 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78  );.  assert( pEx
10765 63 65 70 74 3d 3d 30 20 7c 7c 20 70 45 78 63 65  cept==0 || pExce
10766 70 74 2d 3e 70 42 74 3d 3d 70 42 74 20 29 3b 0a  pt->pBt==pBt );.
10767 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75    for(p=pBt->pCu
10768 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e  rsor; p; p=p->pN
10769 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 21  ext){.    if( p!
1076a 3d 70 45 78 63 65 70 74 20 26 26 20 28 30 3d 3d  =pExcept && (0==
1076b 69 52 6f 6f 74 20 7c 7c 20 70 2d 3e 70 67 6e 6f  iRoot || p->pgno
1076c 52 6f 6f 74 3d 3d 69 52 6f 6f 74 29 20 26 26 20  Root==iRoot) && 
1076d 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 53 74 61  .        p->eSta
1076e 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
1076f 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63   ){.      int rc
10770 20 3d 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73   = saveCursorPos
10771 69 74 69 6f 6e 28 70 29 3b 0a 20 20 20 20 20 20  ition(p);.      
10772 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72  if( SQLITE_OK!=r
10773 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  c ){.        ret
10774 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
10775 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
10776 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
10777 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65  ./*.** Clear the
10778 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72 20   current cursor 
10779 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  position..*/.sta
1077a 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 43 75  tic void clearCu
1077b 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74 43  rsorPosition(BtC
1077c 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20  ursor *pCur){.  
1077d 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f  assert( cursorHo
1077e 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29  ldsMutex(pCur) )
1077f 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
10780 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 20  (pCur->pKey);.  
10781 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b 0a  pCur->pKey = 0;.
10782 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d    pCur->eState =
10783 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b   CURSOR_INVALID;
10784 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 74 6f 72  .}../*.** Restor
10785 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20  e the cursor to 
10786 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 69 74 20  the position it 
10787 77 61 73 20 69 6e 20 28 6f 72 20 61 73 20 63 6c  was in (or as cl
10788 6f 73 65 20 74 6f 20 61 73 20 70 6f 73 73 69 62  ose to as possib
10789 6c 65 29 0a 2a 2a 20 77 68 65 6e 20 73 61 76 65  le).** when save
1078a 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29  CursorPosition()
1078b 20 77 61 73 20 63 61 6c 6c 65 64 2e 20 4e 6f 74   was called. Not
1078c 65 20 74 68 61 74 20 74 68 69 73 20 63 61 6c 6c  e that this call
1078d 20 64 65 6c 65 74 65 73 20 74 68 65 20 0a 2a 2a   deletes the .**
1078e 20 73 61 76 65 64 20 70 6f 73 69 74 69 6f 6e 20   saved position 
1078f 69 6e 66 6f 20 73 74 6f 72 65 64 20 62 79 20 73  info stored by s
10790 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f  aveCursorPositio
10791 6e 28 29 2c 20 73 6f 20 74 68 65 72 65 20 63 61  n(), so there ca
10792 6e 20 62 65 0a 2a 2a 20 61 74 20 6d 6f 73 74 20  n be.** at most 
10793 6f 6e 65 20 65 66 66 65 63 74 69 76 65 20 72 65  one effective re
10794 73 74 6f 72 65 4f 72 43 6c 65 61 72 43 75 72 73  storeOrClearCurs
10795 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 63 61 6c  orPosition() cal
10796 6c 20 61 66 74 65 72 20 65 61 63 68 20 0a 2a 2a  l after each .**
10797 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74   saveCursorPosit
10798 69 6f 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ion()..**.** If 
10799 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
1079a 65 6e 74 20 61 72 67 75 6d 65 6e 74 20 2d 20 64  ent argument - d
1079b 6f 53 65 65 6b 20 2d 20 69 73 20 66 61 6c 73 65  oSeek - is false
1079c 2c 20 74 68 65 6e 20 69 6e 73 74 65 61 64 20 6f  , then instead o
1079d 66 20 0a 2a 2a 20 72 65 74 75 72 6e 69 6e 67 20  f .** returning 
1079e 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 69 74  the cursor to it
1079f 73 20 73 61 76 65 64 20 70 6f 73 69 74 69 6f 6e  s saved position
107a0 2c 20 61 6e 79 20 73 61 76 65 64 20 70 6f 73 69  , any saved posi
107a1 74 69 6f 6e 20 69 73 20 64 65 6c 65 74 65 64 0a  tion is deleted.
107a2 2a 2a 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f  ** and the curso
107a3 72 20 73 74 61 74 65 20 73 65 74 20 74 6f 20 43  r state set to C
107a4 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 2e 0a 2a  URSOR_INVALID..*
107a5 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
107a6 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
107a7 65 52 65 73 74 6f 72 65 4f 72 43 6c 65 61 72 43  eRestoreOrClearC
107a8 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74  ursorPosition(Bt
107a9 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
107aa 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72   int rc;.  asser
107ab 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
107ac 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61  tex(pCur) );.  a
107ad 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74  ssert( pCur->eSt
107ae 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55  ate>=CURSOR_REQU
107af 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 69 66 28  IRESEEK );.  if(
107b0 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
107b1 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20  URSOR_FAULT ){. 
107b2 20 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d 3e     return pCur->
107b3 73 6b 69 70 3b 0a 20 20 7d 0a 23 69 66 6e 64 65  skip;.  }.#ifnde
107b4 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
107b5 43 52 42 4c 4f 42 0a 20 20 69 66 28 20 70 43 75  CRBLOB.  if( pCu
107b6 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e  r->isIncrblobHan
107b7 64 6c 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  dle ){.    retur
107b8 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a  n SQLITE_ABORT;.
107b9 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 43 75    }.#endif.  pCu
107ba 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53  r->eState = CURS
107bb 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 72 63  OR_INVALID;.  rc
107bc 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d   = sqlite3BtreeM
107bd 6f 76 65 74 6f 28 70 43 75 72 2c 20 70 43 75 72  oveto(pCur, pCur
107be 2d 3e 70 4b 65 79 2c 20 30 2c 20 70 43 75 72 2d  ->pKey, 0, pCur-
107bf 3e 6e 4b 65 79 2c 20 30 2c 20 26 70 43 75 72 2d  >nKey, 0, &pCur-
107c0 3e 73 6b 69 70 29 3b 0a 20 20 69 66 28 20 72 63  >skip);.  if( rc
107c1 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
107c2 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
107c3 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 20  pCur->pKey);.   
107c4 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b   pCur->pKey = 0;
107c5 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75  .    assert( pCu
107c6 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
107c7 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d  R_VALID || pCur-
107c8 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
107c9 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20 7d 0a 20  INVALID );.  }. 
107ca 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23   return rc;.}..#
107cb 64 65 66 69 6e 65 20 72 65 73 74 6f 72 65 4f 72  define restoreOr
107cc 43 6c 65 61 72 43 75 72 73 6f 72 50 6f 73 69 74  ClearCursorPosit
107cd 69 6f 6e 28 70 29 20 5c 0a 20 20 28 70 2d 3e 65  ion(p) \.  (p->e
107ce 53 74 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45  State>=CURSOR_RE
107cf 51 55 49 52 45 53 45 45 4b 20 3f 20 5c 0a 20 20  QUIRESEEK ? \.  
107d0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74         sqlite3Bt
107d1 72 65 65 52 65 73 74 6f 72 65 4f 72 43 6c 65 61  reeRestoreOrClea
107d2 72 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  rCursorPosition(
107d3 70 29 20 3a 20 5c 0a 20 20 20 20 20 20 20 20 20  p) : \.         
107d4 53 51 4c 49 54 45 5f 4f 4b 29 0a 0a 23 69 66 6e  SQLITE_OK)..#ifn
107d5 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
107d6 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a  AUTOVACUUM./*.**
107d7 20 47 69 76 65 6e 20 61 20 70 61 67 65 20 6e 75   Given a page nu
107d8 6d 62 65 72 20 6f 66 20 61 20 72 65 67 75 6c 61  mber of a regula
107d9 72 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2c  r database page,
107da 20 72 65 74 75 72 6e 20 74 68 65 20 70 61 67 65   return the page
107db 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  .** number for t
107dc 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70  he pointer-map p
107dd 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  age that contain
107de 73 20 74 68 65 20 65 6e 74 72 79 20 66 6f 72 20  s the entry for 
107df 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 70 61 67  the.** input pag
107e0 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61  e number..*/.sta
107e1 74 69 63 20 50 67 6e 6f 20 70 74 72 6d 61 70 50  tic Pgno ptrmapP
107e2 61 67 65 6e 6f 28 42 74 53 68 61 72 65 64 20 2a  ageno(BtShared *
107e3 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b  pBt, Pgno pgno){
107e4 0a 20 20 69 6e 74 20 6e 50 61 67 65 73 50 65 72  .  int nPagesPer
107e5 4d 61 70 50 61 67 65 2c 20 69 50 74 72 4d 61 70  MapPage, iPtrMap
107e6 2c 20 72 65 74 3b 0a 20 20 61 73 73 65 72 74 28  , ret;.  assert(
107e7 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
107e8 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
107e9 29 3b 0a 20 20 6e 50 61 67 65 73 50 65 72 4d 61  );.  nPagesPerMa
107ea 70 50 61 67 65 20 3d 20 28 70 42 74 2d 3e 75 73  pPage = (pBt->us
107eb 61 62 6c 65 53 69 7a 65 2f 35 29 2b 31 3b 0a 20  ableSize/5)+1;. 
107ec 20 69 50 74 72 4d 61 70 20 3d 20 28 70 67 6e 6f   iPtrMap = (pgno
107ed 2d 32 29 2f 6e 50 61 67 65 73 50 65 72 4d 61 70  -2)/nPagesPerMap
107ee 50 61 67 65 3b 0a 20 20 72 65 74 20 3d 20 28 69  Page;.  ret = (i
107ef 50 74 72 4d 61 70 2a 6e 50 61 67 65 73 50 65 72  PtrMap*nPagesPer
107f0 4d 61 70 50 61 67 65 29 20 2b 20 32 3b 20 0a 20  MapPage) + 2; . 
107f1 20 69 66 28 20 72 65 74 3d 3d 50 45 4e 44 49 4e   if( ret==PENDIN
107f2 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29  G_BYTE_PAGE(pBt)
107f3 20 29 7b 0a 20 20 20 20 72 65 74 2b 2b 3b 0a 20   ){.    ret++;. 
107f4 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 74 3b   }.  return ret;
107f5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  .}../*.** Write 
107f6 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68  an entry into th
107f7 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a  e pointer map..*
107f8 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
107f9 65 20 75 70 64 61 74 65 73 20 74 68 65 20 70 6f  e updates the po
107fa 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20  inter map entry 
107fb 66 6f 72 20 70 61 67 65 20 6e 75 6d 62 65 72 20  for page number 
107fc 27 6b 65 79 27 0a 2a 2a 20 73 6f 20 74 68 61 74  'key'.** so that
107fd 20 69 74 20 6d 61 70 73 20 74 6f 20 74 79 70 65   it maps to type
107fe 20 27 65 54 79 70 65 27 20 61 6e 64 20 70 61 72   'eType' and par
107ff 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20  ent page number 
10800 27 70 67 6e 6f 27 2e 0a 2a 2a 20 41 6e 20 65 72  'pgno'..** An er
10801 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
10802 72 6e 65 64 20 69 66 20 73 6f 6d 65 74 68 69 6e  rned if somethin
10803 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 6f 74  g goes wrong, ot
10804 68 65 72 77 69 73 65 20 53 51 4c 49 54 45 5f 4f  herwise SQLITE_O
10805 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  K..*/.static int
10806 20 70 74 72 6d 61 70 50 75 74 28 42 74 53 68 61   ptrmapPut(BtSha
10807 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6b  red *pBt, Pgno k
10808 65 79 2c 20 75 38 20 65 54 79 70 65 2c 20 50 67  ey, u8 eType, Pg
10809 6e 6f 20 70 61 72 65 6e 74 29 7b 0a 20 20 44 62  no parent){.  Db
1080a 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20 20  Page *pDbPage;  
1080b 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d  /* The pointer m
1080c 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20  ap page */.  u8 
1080d 2a 70 50 74 72 6d 61 70 3b 20 20 20 20 20 20 2f  *pPtrmap;      /
1080e 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61  * The pointer ma
1080f 70 20 64 61 74 61 20 2a 2f 0a 20 20 50 67 6e 6f  p data */.  Pgno
10810 20 69 50 74 72 6d 61 70 3b 20 20 20 20 20 2f 2a   iPtrmap;     /*
10811 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   The pointer map
10812 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a   page number */.
10813 20 20 69 6e 74 20 6f 66 66 73 65 74 3b 20 20 20    int offset;   
10814 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e      /* Offset in
10815 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67   pointer map pag
10816 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  e */.  int rc;..
10817 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
10818 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
10819 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 2f 2a  ->mutex) );.  /*
1081a 20 54 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72   The master-jour
1081b 6e 61 6c 20 70 61 67 65 20 6e 75 6d 62 65 72 20  nal page number 
1081c 6d 75 73 74 20 6e 65 76 65 72 20 62 65 20 75 73  must never be us
1081d 65 64 20 61 73 20 61 20 70 6f 69 6e 74 65 72 20  ed as a pointer 
1081e 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 61 73  map page */.  as
1081f 73 65 72 74 28 20 30 3d 3d 50 54 52 4d 41 50 5f  sert( 0==PTRMAP_
10820 49 53 50 41 47 45 28 70 42 74 2c 20 50 45 4e 44  ISPAGE(pBt, PEND
10821 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
10822 74 29 29 20 29 3b 0a 0a 20 20 61 73 73 65 72 74  t)) );..  assert
10823 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ( pBt->autoVacuu
10824 6d 20 29 3b 0a 20 20 69 66 28 20 6b 65 79 3d 3d  m );.  if( key==
10825 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
10826 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
10827 4b 50 54 3b 0a 20 20 7d 0a 20 20 69 50 74 72 6d  KPT;.  }.  iPtrm
10828 61 70 20 3d 20 50 54 52 4d 41 50 5f 50 41 47 45  ap = PTRMAP_PAGE
10829 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20  NO(pBt, key);.  
1082a 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1082b 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65 72  rGet(pBt->pPager
1082c 2c 20 69 50 74 72 6d 61 70 2c 20 26 70 44 62 50  , iPtrmap, &pDbP
1082d 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  age);.  if( rc!=
1082e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1082f 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
10830 20 20 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41    offset = PTRMA
10831 50 5f 50 54 52 4f 46 46 53 45 54 28 70 42 74 2c  P_PTROFFSET(pBt,
10832 20 6b 65 79 29 3b 0a 20 20 70 50 74 72 6d 61 70   key);.  pPtrmap
10833 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33   = (u8 *)sqlite3
10834 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62  PagerGetData(pDb
10835 50 61 67 65 29 3b 0a 0a 20 20 69 66 28 20 65 54  Page);..  if( eT
10836 79 70 65 21 3d 70 50 74 72 6d 61 70 5b 6f 66 66  ype!=pPtrmap[off
10837 73 65 74 5d 20 7c 7c 20 67 65 74 34 62 79 74 65  set] || get4byte
10838 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74  (&pPtrmap[offset
10839 2b 31 5d 29 21 3d 70 61 72 65 6e 74 20 29 7b 0a  +1])!=parent ){.
1083a 20 20 20 20 54 52 41 43 45 28 28 22 50 54 52 4d      TRACE(("PTRM
1083b 41 50 5f 55 50 44 41 54 45 3a 20 25 64 2d 3e 28  AP_UPDATE: %d->(
1083c 25 64 2c 25 64 29 5c 6e 22 2c 20 6b 65 79 2c 20  %d,%d)\n", key, 
1083d 65 54 79 70 65 2c 20 70 61 72 65 6e 74 29 29 3b  eType, parent));
1083e 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1083f 33 50 61 67 65 72 57 72 69 74 65 28 70 44 62 50  3PagerWrite(pDbP
10840 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63  age);.    if( rc
10841 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
10842 20 20 20 20 20 70 50 74 72 6d 61 70 5b 6f 66 66       pPtrmap[off
10843 73 65 74 5d 20 3d 20 65 54 79 70 65 3b 0a 20 20  set] = eType;.  
10844 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50      put4byte(&pP
10845 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d 2c  trmap[offset+1],
10846 20 70 61 72 65 6e 74 29 3b 0a 20 20 20 20 7d 0a   parent);.    }.
10847 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 50 61    }..  sqlite3Pa
10848 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 65  gerUnref(pDbPage
10849 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
1084a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 6e  }../*.** Read an
1084b 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20   entry from the 
1084c 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a  pointer map..**.
1084d 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1084e 72 65 74 72 69 65 76 65 73 20 74 68 65 20 70 6f  retrieves the po
1084f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20  inter map entry 
10850 66 6f 72 20 70 61 67 65 20 27 6b 65 79 27 2c 20  for page 'key', 
10851 77 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 74  writing.** the t
10852 79 70 65 20 61 6e 64 20 70 61 72 65 6e 74 20 70  ype and parent p
10853 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 2a 70  age number to *p
10854 45 54 79 70 65 20 61 6e 64 20 2a 70 50 67 6e 6f  EType and *pPgno
10855 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a   respectively..*
10856 2a 20 41 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  * An error code 
10857 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 73  is returned if s
10858 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72  omething goes wr
10859 6f 6e 67 2c 20 6f 74 68 65 72 77 69 73 65 20 53  ong, otherwise S
1085a 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61  QLITE_OK..*/.sta
1085b 74 69 63 20 69 6e 74 20 70 74 72 6d 61 70 47 65  tic int ptrmapGe
1085c 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c  t(BtShared *pBt,
1085d 20 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20 2a 70   Pgno key, u8 *p
1085e 45 54 79 70 65 2c 20 50 67 6e 6f 20 2a 70 50 67  EType, Pgno *pPg
1085f 6e 6f 29 7b 0a 20 20 44 62 50 61 67 65 20 2a 70  no){.  DbPage *p
10860 44 62 50 61 67 65 3b 20 20 20 2f 2a 20 54 68 65  DbPage;   /* The
10861 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67   pointer map pag
10862 65 20 2a 2f 0a 20 20 69 6e 74 20 69 50 74 72 6d  e */.  int iPtrm
10863 61 70 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69  ap;       /* Poi
10864 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 69 6e  nter map page in
10865 64 65 78 20 2a 2f 0a 20 20 75 38 20 2a 70 50 74  dex */.  u8 *pPt
10866 72 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a 20 50  rmap;       /* P
10867 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20  ointer map page 
10868 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6f 66  data */.  int of
10869 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20  fset;        /* 
1086a 4f 66 66 73 65 74 20 6f 66 20 65 6e 74 72 79 20  Offset of entry 
1086b 69 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 2a  in pointer map *
1086c 2f 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61  /.  int rc;..  a
1086d 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1086e 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d  utex_held(pBt->m
1086f 75 74 65 78 29 20 29 3b 0a 0a 20 20 69 50 74 72  utex) );..  iPtr
10870 6d 61 70 20 3d 20 50 54 52 4d 41 50 5f 50 41 47  map = PTRMAP_PAG
10871 45 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b 0a 20  ENO(pBt, key);. 
10872 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
10873 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65  erGet(pBt->pPage
10874 72 2c 20 69 50 74 72 6d 61 70 2c 20 26 70 44 62  r, iPtrmap, &pDb
10875 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21  Page);.  if( rc!
10876 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
10877 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50 74 72 6d   rc;.  }.  pPtrm
10878 61 70 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74  ap = (u8 *)sqlit
10879 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70  e3PagerGetData(p
1087a 44 62 50 61 67 65 29 3b 0a 0a 20 20 6f 66 66 73  DbPage);..  offs
1087b 65 74 20 3d 20 50 54 52 4d 41 50 5f 50 54 52 4f  et = PTRMAP_PTRO
1087c 46 46 53 45 54 28 70 42 74 2c 20 6b 65 79 29 3b  FFSET(pBt, key);
1087d 0a 20 20 61 73 73 65 72 74 28 20 70 45 54 79 70  .  assert( pETyp
1087e 65 21 3d 30 20 29 3b 0a 20 20 2a 70 45 54 79 70  e!=0 );.  *pETyp
1087f 65 20 3d 20 70 50 74 72 6d 61 70 5b 6f 66 66 73  e = pPtrmap[offs
10880 65 74 5d 3b 0a 20 20 69 66 28 20 70 50 67 6e 6f  et];.  if( pPgno
10881 20 29 20 2a 70 50 67 6e 6f 20 3d 20 67 65 74 34   ) *pPgno = get4
10882 62 79 74 65 28 26 70 50 74 72 6d 61 70 5b 6f 66  byte(&pPtrmap[of
10883 66 73 65 74 2b 31 5d 29 3b 0a 0a 20 20 73 71 6c  fset+1]);..  sql
10884 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
10885 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 2a  DbPage);.  if( *
10886 70 45 54 79 70 65 3c 31 20 7c 7c 20 2a 70 45 54  pEType<1 || *pET
10887 79 70 65 3e 35 20 29 20 72 65 74 75 72 6e 20 53  ype>5 ) return S
10888 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
10889 50 54 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  PT;.  return SQL
1088a 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69  ITE_OK;.}..#endi
1088b 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1088c 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 0a  _AUTOVACUUM */..
1088d 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 62 74  /*.** Given a bt
1088e 72 65 65 20 70 61 67 65 20 61 6e 64 20 61 20 63  ree page and a c
1088f 65 6c 6c 20 69 6e 64 65 78 20 28 30 20 6d 65 61  ell index (0 mea
10890 6e 73 20 74 68 65 20 66 69 72 73 74 20 63 65 6c  ns the first cel
10891 6c 20 6f 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65  l on.** the page
10892 2c 20 31 20 6d 65 61 6e 73 20 74 68 65 20 73 65  , 1 means the se
10893 63 6f 6e 64 20 63 65 6c 6c 2c 20 61 6e 64 20 73  cond cell, and s
10894 6f 20 66 6f 72 74 68 29 20 72 65 74 75 72 6e 20  o forth) return 
10895 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20  a pointer.** to 
10896 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
10897 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
10898 74 69 6e 65 20 77 6f 72 6b 73 20 6f 6e 6c 79 20  tine works only 
10899 66 6f 72 20 70 61 67 65 73 20 74 68 61 74 20 64  for pages that d
1089a 6f 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 6f 76  o not contain ov
1089b 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a 2a 2f  erflow cells..*/
1089c 0a 23 64 65 66 69 6e 65 20 66 69 6e 64 43 65 6c  .#define findCel
1089d 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 29 20  l(pPage, iCell) 
1089e 5c 0a 20 20 28 28 70 50 61 67 65 29 2d 3e 61 44  \.  ((pPage)->aD
1089f 61 74 61 20 2b 20 67 65 74 32 62 79 74 65 28 26  ata + get2byte(&
108a0 28 70 50 61 67 65 29 2d 3e 61 44 61 74 61 5b 28  (pPage)->aData[(
108a1 70 50 61 67 65 29 2d 3e 63 65 6c 6c 4f 66 66 73  pPage)->cellOffs
108a2 65 74 2b 32 2a 28 69 43 65 6c 6c 29 5d 29 29 0a  et+2*(iCell)])).
108a3 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
108a4 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ST.SQLITE_PRIVAT
108a5 45 20 75 38 20 2a 73 71 6c 69 74 65 33 42 74 72  E u8 *sqlite3Btr
108a6 65 65 46 69 6e 64 43 65 6c 6c 28 4d 65 6d 50 61  eeFindCell(MemPa
108a7 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69  ge *pPage, int i
108a8 43 65 6c 6c 29 7b 0a 20 20 61 73 73 65 72 74 28  Cell){.  assert(
108a9 20 69 43 65 6c 6c 3e 3d 30 20 29 3b 0a 20 20 61   iCell>=0 );.  a
108aa 73 73 65 72 74 28 20 69 43 65 6c 6c 3c 67 65 74  ssert( iCell<get
108ab 32 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44  2byte(&pPage->aD
108ac 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66  ata[pPage->hdrOf
108ad 66 73 65 74 2b 33 5d 29 20 29 3b 0a 20 20 72 65  fset+3]) );.  re
108ae 74 75 72 6e 20 66 69 6e 64 43 65 6c 6c 28 70 50  turn findCell(pP
108af 61 67 65 2c 20 69 43 65 6c 6c 29 3b 0a 7d 0a 23  age, iCell);.}.#
108b0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  endif../*.** Thi
108b1 73 20 61 20 6d 6f 72 65 20 63 6f 6d 70 6c 65 78  s a more complex
108b2 20 76 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69   version of sqli
108b3 74 65 33 42 74 72 65 65 46 69 6e 64 43 65 6c 6c  te3BtreeFindCell
108b4 28 29 20 74 68 61 74 20 77 6f 72 6b 73 20 66 6f  () that works fo
108b5 72 0a 2a 2a 20 70 61 67 65 73 20 74 68 61 74 20  r.** pages that 
108b6 64 6f 20 63 6f 6e 74 61 69 6e 20 6f 76 65 72 66  do contain overf
108b7 6c 6f 77 20 63 65 6c 6c 73 2e 20 20 53 65 65 20  low cells.  See 
108b8 69 6e 73 65 72 74 0a 2a 2f 0a 73 74 61 74 69 63  insert.*/.static
108b9 20 75 38 20 2a 66 69 6e 64 4f 76 65 72 66 6c 6f   u8 *findOverflo
108ba 77 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70  wCell(MemPage *p
108bb 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29  Page, int iCell)
108bc 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73  {.  int i;.  ass
108bd 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
108be 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
108bf 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
108c0 66 6f 72 28 69 3d 70 50 61 67 65 2d 3e 6e 4f 76  for(i=pPage->nOv
108c1 65 72 66 6c 6f 77 2d 31 3b 20 69 3e 3d 30 3b 20  erflow-1; i>=0; 
108c2 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 6b 3b  i--){.    int k;
108c3 0a 20 20 20 20 73 74 72 75 63 74 20 5f 4f 76 66  .    struct _Ovf
108c4 6c 43 65 6c 6c 20 2a 70 4f 76 66 6c 3b 0a 20 20  lCell *pOvfl;.  
108c5 20 20 70 4f 76 66 6c 20 3d 20 26 70 50 61 67 65    pOvfl = &pPage
108c6 2d 3e 61 4f 76 66 6c 5b 69 5d 3b 0a 20 20 20 20  ->aOvfl[i];.    
108c7 6b 20 3d 20 70 4f 76 66 6c 2d 3e 69 64 78 3b 0a  k = pOvfl->idx;.
108c8 20 20 20 20 69 66 28 20 6b 3c 3d 69 43 65 6c 6c      if( k<=iCell
108c9 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6b 3d   ){.      if( k=
108ca 3d 69 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20  =iCell ){.      
108cb 20 20 72 65 74 75 72 6e 20 70 4f 76 66 6c 2d 3e    return pOvfl->
108cc 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20  pCell;.      }. 
108cd 20 20 20 20 20 69 43 65 6c 6c 2d 2d 3b 0a 20 20       iCell--;.  
108ce 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
108cf 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c   findCell(pPage,
108d0 20 69 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a   iCell);.}../*.*
108d1 2a 20 50 61 72 73 65 20 61 20 63 65 6c 6c 20 63  * Parse a cell c
108d2 6f 6e 74 65 6e 74 20 62 6c 6f 63 6b 20 61 6e 64  ontent block and
108d3 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 43 65 6c   fill in the Cel
108d4 6c 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e  lInfo structure.
108d5 20 20 54 68 65 72 65 0a 2a 2a 20 61 72 65 20 74    There.** are t
108d6 77 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74  wo versions of t
108d7 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 73  his function.  s
108d8 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65  qlite3BtreeParse
108d9 43 65 6c 6c 28 29 20 74 61 6b 65 73 20 61 20 0a  Cell() takes a .
108da 2a 2a 20 63 65 6c 6c 20 69 6e 64 65 78 20 61 73  ** cell index as
108db 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
108dc 6d 65 6e 74 20 61 6e 64 20 73 71 6c 69 74 65 33  ment and sqlite3
108dd 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74  BtreeParseCellPt
108de 72 28 29 20 0a 2a 2a 20 74 61 6b 65 73 20 61 20  r() .** takes a 
108df 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 62  pointer to the b
108e0 6f 64 79 20 6f 66 20 74 68 65 20 63 65 6c 6c 20  ody of the cell 
108e1 61 73 20 69 74 73 20 73 65 63 6f 6e 64 20 61 72  as its second ar
108e2 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 69  gument..**.** Wi
108e3 74 68 69 6e 20 74 68 69 73 20 66 69 6c 65 2c 20  thin this file, 
108e4 74 68 65 20 70 61 72 73 65 43 65 6c 6c 28 29 20  the parseCell() 
108e5 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 63 61 6c  macro can be cal
108e6 6c 65 64 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a  led instead of.*
108e7 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  * sqlite3BtreePa
108e8 72 73 65 43 65 6c 6c 50 74 72 28 29 2e 20 55 73  rseCellPtr(). Us
108e9 69 6e 67 20 73 6f 6d 65 20 63 6f 6d 70 69 6c 65  ing some compile
108ea 72 73 2c 20 74 68 69 73 20 77 69 6c 6c 20 62 65  rs, this will be
108eb 20 66 61 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49   faster..*/.SQLI
108ec 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
108ed 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73  sqlite3BtreePars
108ee 65 43 65 6c 6c 50 74 72 28 0a 20 20 4d 65 6d 50  eCellPtr(.  MemP
108ef 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20  age *pPage,     
108f0 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74      /* Page cont
108f1 61 69 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c 20  aining the cell 
108f2 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20  */.  u8 *pCell, 
108f3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
108f4 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63  Pointer to the c
108f5 65 6c 6c 20 74 65 78 74 2e 20 2a 2f 0a 20 20 43  ell text. */.  C
108f6 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20  ellInfo *pInfo  
108f7 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69         /* Fill i
108f8 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  n this structure
108f9 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 20   */.){.  int n; 
108fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
108fb 20 2f 2a 20 4e 75 6d 62 65 72 20 62 79 74 65 73   /* Number bytes
108fc 20 69 6e 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74   in cell content
108fd 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32   header */.  u32
108fe 20 6e 50 61 79 6c 6f 61 64 3b 20 20 20 20 20 20   nPayload;      
108ff 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
10900 66 20 62 79 74 65 73 20 6f 66 20 63 65 6c 6c 20  f bytes of cell 
10901 70 61 79 6c 6f 61 64 20 2a 2f 0a 0a 20 20 61 73  payload */..  as
10902 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
10903 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
10904 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a  pBt->mutex) );..
10905 20 20 70 49 6e 66 6f 2d 3e 70 43 65 6c 6c 20 3d    pInfo->pCell =
10906 20 70 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74   pCell;.  assert
10907 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 30  ( pPage->leaf==0
10908 20 7c 7c 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d   || pPage->leaf=
10909 3d 31 20 29 3b 0a 20 20 6e 20 3d 20 70 50 61 67  =1 );.  n = pPag
1090a 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 3b  e->childPtrSize;
1090b 0a 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 34 2d  .  assert( n==4-
1090c 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b  4*pPage->leaf );
1090d 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 68 61  .  if( pPage->ha
1090e 73 44 61 74 61 20 29 7b 0a 20 20 20 20 6e 20 2b  sData ){.    n +
1090f 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 26 70  = getVarint32(&p
10910 43 65 6c 6c 5b 6e 5d 2c 20 6e 50 61 79 6c 6f 61  Cell[n], nPayloa
10911 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  d);.  }else{.   
10912 20 6e 50 61 79 6c 6f 61 64 20 3d 20 30 3b 0a 20   nPayload = 0;. 
10913 20 7d 0a 20 20 70 49 6e 66 6f 2d 3e 6e 44 61 74   }.  pInfo->nDat
10914 61 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20  a = nPayload;.  
10915 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65  if( pPage->intKe
10916 79 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 67 65  y ){.    n += ge
10917 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e  tVarint(&pCell[n
10918 5d 2c 20 28 75 36 34 20 2a 29 26 70 49 6e 66 6f  ], (u64 *)&pInfo
10919 2d 3e 6e 4b 65 79 29 3b 0a 20 20 7d 65 6c 73 65  ->nKey);.  }else
1091a 7b 0a 20 20 20 20 75 33 32 20 78 3b 0a 20 20 20  {.    u32 x;.   
1091b 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33   n += getVarint3
1091c 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 78 29 3b  2(&pCell[n], x);
1091d 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4b 65 79  .    pInfo->nKey
1091e 20 3d 20 78 3b 0a 20 20 20 20 6e 50 61 79 6c 6f   = x;.    nPaylo
1091f 61 64 20 2b 3d 20 78 3b 0a 20 20 7d 0a 20 20 70  ad += x;.  }.  p
10920 49 6e 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 20 3d  Info->nPayload =
10921 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 70 49 6e   nPayload;.  pIn
10922 66 6f 2d 3e 6e 48 65 61 64 65 72 20 3d 20 6e 3b  fo->nHeader = n;
10923 0a 20 20 69 66 28 20 6e 50 61 79 6c 6f 61 64 3c  .  if( nPayload<
10924 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c  =pPage->maxLocal
10925 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20   ){.    /* This 
10926 69 73 20 74 68 65 20 28 65 61 73 79 29 20 63 6f  is the (easy) co
10927 6d 6d 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20  mmon case where 
10928 74 68 65 20 65 6e 74 69 72 65 20 70 61 79 6c 6f  the entire paylo
10929 61 64 20 66 69 74 73 0a 20 20 20 20 2a 2a 20 6f  ad fits.    ** o
1092a 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65  n the local page
1092b 2e 20 20 4e 6f 20 6f 76 65 72 66 6c 6f 77 20 69  .  No overflow i
1092c 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20  s required..    
1092d 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 53 69 7a 65  */.    int nSize
1092e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f  ;          /* To
1092f 74 61 6c 20 73 69 7a 65 20 6f 66 20 63 65 6c 6c  tal size of cell
10930 20 63 6f 6e 74 65 6e 74 20 69 6e 20 62 79 74 65   content in byte
10931 73 20 2a 2f 0a 20 20 20 20 70 49 6e 66 6f 2d 3e  s */.    pInfo->
10932 6e 4c 6f 63 61 6c 20 3d 20 6e 50 61 79 6c 6f 61  nLocal = nPayloa
10933 64 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f  d;.    pInfo->iO
10934 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20  verflow = 0;.   
10935 20 6e 53 69 7a 65 20 3d 20 6e 50 61 79 6c 6f 61   nSize = nPayloa
10936 64 20 2b 20 6e 3b 0a 20 20 20 20 69 66 28 20 6e  d + n;.    if( n
10937 53 69 7a 65 3c 34 20 29 7b 0a 20 20 20 20 20 20  Size<4 ){.      
10938 6e 53 69 7a 65 20 3d 20 34 3b 20 20 20 20 20 20  nSize = 4;      
10939 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c    /* Minimum cel
1093a 6c 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a 20  l size is 4 */. 
1093b 20 20 20 7d 0a 20 20 20 20 70 49 6e 66 6f 2d 3e     }.    pInfo->
1093c 6e 53 69 7a 65 20 3d 20 6e 53 69 7a 65 3b 0a 20  nSize = nSize;. 
1093d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49   }else{.    /* I
1093e 66 20 74 68 65 20 70 61 79 6c 6f 61 64 20 77 69  f the payload wi
1093f 6c 6c 20 6e 6f 74 20 66 69 74 20 63 6f 6d 70 6c  ll not fit compl
10940 65 74 65 6c 79 20 6f 6e 20 74 68 65 20 6c 6f 63  etely on the loc
10941 61 6c 20 70 61 67 65 2c 20 77 65 20 68 61 76 65  al page, we have
10942 0a 20 20 20 20 2a 2a 20 74 6f 20 64 65 63 69 64  .    ** to decid
10943 65 20 68 6f 77 20 6d 75 63 68 20 74 6f 20 73 74  e how much to st
10944 6f 72 65 20 6c 6f 63 61 6c 6c 79 20 61 6e 64 20  ore locally and 
10945 68 6f 77 20 6d 75 63 68 20 74 6f 20 73 70 69 6c  how much to spil
10946 6c 20 6f 6e 74 6f 0a 20 20 20 20 2a 2a 20 6f 76  l onto.    ** ov
10947 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 20 54  erflow pages.  T
10948 68 65 20 73 74 72 61 74 65 67 79 20 69 73 20 74  he strategy is t
10949 6f 20 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 61  o minimize the a
1094a 6d 6f 75 6e 74 20 6f 66 20 75 6e 75 73 65 64 0a  mount of unused.
1094b 20 20 20 20 2a 2a 20 73 70 61 63 65 20 6f 6e 20      ** space on 
1094c 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 77  overflow pages w
1094d 68 69 6c 65 20 6b 65 65 70 69 6e 67 20 74 68 65  hile keeping the
1094e 20 61 6d 6f 75 6e 74 20 6f 66 20 6c 6f 63 61 6c   amount of local
1094f 20 73 74 6f 72 61 67 65 0a 20 20 20 20 2a 2a 20   storage.    ** 
10950 69 6e 20 62 65 74 77 65 65 6e 20 6d 69 6e 4c 6f  in between minLo
10951 63 61 6c 20 61 6e 64 20 6d 61 78 4c 6f 63 61 6c  cal and maxLocal
10952 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
10953 57 61 72 6e 69 6e 67 3a 20 20 63 68 61 6e 67 69  Warning:  changi
10954 6e 67 20 74 68 65 20 77 61 79 20 6f 76 65 72 66  ng the way overf
10955 6c 6f 77 20 70 61 79 6c 6f 61 64 20 69 73 20 64  low payload is d
10956 69 73 74 72 69 62 75 74 65 64 20 69 6e 20 61 6e  istributed in an
10957 79 0a 20 20 20 20 2a 2a 20 77 61 79 20 77 69 6c  y.    ** way wil
10958 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 6e 20 69  l result in an i
10959 6e 63 6f 6d 70 61 74 69 62 6c 65 20 66 69 6c 65  ncompatible file
1095a 20 66 6f 72 6d 61 74 2e 0a 20 20 20 20 2a 2f 0a   format..    */.
1095b 20 20 20 20 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c      int minLocal
1095c 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 61 6d  ;  /* Minimum am
1095d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20  ount of payload 
1095e 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a  held locally */.
1095f 20 20 20 20 69 6e 74 20 6d 61 78 4c 6f 63 61 6c      int maxLocal
10960 3b 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6d  ;  /* Maximum am
10961 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20  ount of payload 
10962 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a  held locally */.
10963 20 20 20 20 69 6e 74 20 73 75 72 70 6c 75 73 3b      int surplus;
10964 20 20 20 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70     /* Overflow p
10965 61 79 6c 6f 61 64 20 61 76 61 69 6c 61 62 6c 65  ayload available
10966 20 66 6f 72 20 6c 6f 63 61 6c 20 73 74 6f 72 61   for local stora
10967 67 65 20 2a 2f 0a 0a 20 20 20 20 6d 69 6e 4c 6f  ge */..    minLo
10968 63 61 6c 20 3d 20 70 50 61 67 65 2d 3e 6d 69 6e  cal = pPage->min
10969 4c 6f 63 61 6c 3b 0a 20 20 20 20 6d 61 78 4c 6f  Local;.    maxLo
1096a 63 61 6c 20 3d 20 70 50 61 67 65 2d 3e 6d 61 78  cal = pPage->max
1096b 4c 6f 63 61 6c 3b 0a 20 20 20 20 73 75 72 70 6c  Local;.    surpl
1096c 75 73 20 3d 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20  us = minLocal + 
1096d 28 6e 50 61 79 6c 6f 61 64 20 2d 20 6d 69 6e 4c  (nPayload - minL
1096e 6f 63 61 6c 29 25 28 70 50 61 67 65 2d 3e 70 42  ocal)%(pPage->pB
1096f 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20  t->usableSize - 
10970 34 29 3b 0a 20 20 20 20 69 66 28 20 73 75 72 70  4);.    if( surp
10971 6c 75 73 20 3c 3d 20 6d 61 78 4c 6f 63 61 6c 20  lus <= maxLocal 
10972 29 7b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e  ){.      pInfo->
10973 6e 4c 6f 63 61 6c 20 3d 20 73 75 72 70 6c 75 73  nLocal = surplus
10974 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
10975 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c     pInfo->nLocal
10976 20 3d 20 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20   = minLocal;.   
10977 20 7d 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f   }.    pInfo->iO
10978 76 65 72 66 6c 6f 77 20 3d 20 70 49 6e 66 6f 2d  verflow = pInfo-
10979 3e 6e 4c 6f 63 61 6c 20 2b 20 6e 3b 0a 20 20 20  >nLocal + n;.   
1097a 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20   pInfo->nSize = 
1097b 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77  pInfo->iOverflow
1097c 20 2b 20 34 3b 0a 20 20 7d 0a 7d 0a 23 64 65 66   + 4;.  }.}.#def
1097d 69 6e 65 20 70 61 72 73 65 43 65 6c 6c 28 70 50  ine parseCell(pP
1097e 61 67 65 2c 20 69 43 65 6c 6c 2c 20 70 49 6e 66  age, iCell, pInf
1097f 6f 29 20 5c 0a 20 20 73 71 6c 69 74 65 33 42 74  o) \.  sqlite3Bt
10980 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28  reeParseCellPtr(
10981 28 70 50 61 67 65 29 2c 20 66 69 6e 64 43 65 6c  (pPage), findCel
10982 6c 28 28 70 50 61 67 65 29 2c 20 28 69 43 65 6c  l((pPage), (iCel
10983 6c 29 29 2c 20 28 70 49 6e 66 6f 29 29 0a 53 51  l)), (pInfo)).SQ
10984 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
10985 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  d sqlite3BtreePa
10986 72 73 65 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61  rseCell(.  MemPa
10987 67 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20  ge *pPage,      
10988 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61     /* Page conta
10989 69 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a  ining the cell *
1098a 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 2c 20 20  /.  int iCell,  
1098b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1098c 68 65 20 63 65 6c 6c 20 69 6e 64 65 78 2e 20 20  he cell index.  
1098d 46 69 72 73 74 20 63 65 6c 6c 20 69 73 20 30 20  First cell is 0 
1098e 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70  */.  CellInfo *p
1098f 49 6e 66 6f 20 20 20 20 20 20 20 20 20 2f 2a 20  Info         /* 
10990 46 69 6c 6c 20 69 6e 20 74 68 69 73 20 73 74 72  Fill in this str
10991 75 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 70  ucture */.){.  p
10992 61 72 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20  arseCell(pPage, 
10993 69 43 65 6c 6c 2c 20 70 49 6e 66 6f 29 3b 0a 7d  iCell, pInfo);.}
10994 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20  ../*.** Compute 
10995 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  the total number
10996 20 6f 66 20 62 79 74 65 73 20 74 68 61 74 20 61   of bytes that a
10997 20 43 65 6c 6c 20 6e 65 65 64 73 20 69 6e 20 74   Cell needs in t
10998 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 20  he cell.** data 
10999 61 72 65 61 20 6f 66 20 74 68 65 20 62 74 72 65  area of the btre
1099a 65 2d 70 61 67 65 2e 20 20 54 68 65 20 72 65 74  e-page.  The ret
1099b 75 72 6e 20 6e 75 6d 62 65 72 20 69 6e 63 6c 75  urn number inclu
1099c 64 65 73 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20  des the cell.** 
1099d 64 61 74 61 20 68 65 61 64 65 72 20 61 6e 64 20  data header and 
1099e 74 68 65 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61  the local payloa
1099f 64 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f  d, but not any o
109a0 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6f 72 0a  verflow page or.
109a1 2a 2a 20 74 68 65 20 73 70 61 63 65 20 75 73 65  ** the space use
109a2 64 20 62 79 20 74 68 65 20 63 65 6c 6c 20 70 6f  d by the cell po
109a3 69 6e 74 65 72 2e 0a 2a 2f 0a 23 69 66 6e 64 65  inter..*/.#ifnde
109a4 66 20 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20  f NDEBUG.static 
109a5 75 31 36 20 63 65 6c 6c 53 69 7a 65 28 4d 65 6d  u16 cellSize(Mem
109a6 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74  Page *pPage, int
109a7 20 69 43 65 6c 6c 29 7b 0a 20 20 43 65 6c 6c 49   iCell){.  CellI
109a8 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 73 71 6c 69  nfo info;.  sqli
109a9 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c  te3BtreeParseCel
109aa 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c 20  l(pPage, iCell, 
109ab 26 69 6e 66 6f 29 3b 0a 20 20 72 65 74 75 72 6e  &info);.  return
109ac 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b 0a 7d 0a 23   info.nSize;.}.#
109ad 65 6e 64 69 66 0a 73 74 61 74 69 63 20 75 31 36  endif.static u16
109ae 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 4d 65 6d   cellSizePtr(Mem
109af 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20  Page *pPage, u8 
109b0 2a 70 43 65 6c 6c 29 7b 0a 20 20 43 65 6c 6c 49  *pCell){.  CellI
109b1 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 73 71 6c 69  nfo info;.  sqli
109b2 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c  te3BtreeParseCel
109b3 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c  lPtr(pPage, pCel
109b4 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 72 65 74  l, &info);.  ret
109b5 75 72 6e 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b 0a  urn info.nSize;.
109b6 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
109b7 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
109b8 4d 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63  M./*.** If the c
109b9 65 6c 6c 20 70 43 65 6c 6c 2c 20 70 61 72 74 20  ell pCell, part 
109ba 6f 66 20 70 61 67 65 20 70 50 61 67 65 20 63 6f  of page pPage co
109bb 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72  ntains a pointer
109bc 0a 2a 2a 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c  .** to an overfl
109bd 6f 77 20 70 61 67 65 2c 20 69 6e 73 65 72 74 20  ow page, insert 
109be 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68  an entry into th
109bf 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a  e pointer-map.**
109c0 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f   for the overflo
109c1 77 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69  w page..*/.stati
109c2 63 20 69 6e 74 20 70 74 72 6d 61 70 50 75 74 4f  c int ptrmapPutO
109c3 76 66 6c 50 74 72 28 4d 65 6d 50 61 67 65 20 2a  vflPtr(MemPage *
109c4 70 50 61 67 65 2c 20 75 38 20 2a 70 43 65 6c 6c  pPage, u8 *pCell
109c5 29 7b 0a 20 20 69 66 28 20 70 43 65 6c 6c 20 29  ){.  if( pCell )
109c6 7b 0a 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69  {.    CellInfo i
109c7 6e 66 6f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  nfo;.    sqlite3
109c8 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74  BtreeParseCellPt
109c9 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20  r(pPage, pCell, 
109ca 26 69 6e 66 6f 29 3b 0a 20 20 20 20 61 73 73 65  &info);.    asse
109cb 72 74 28 20 28 69 6e 66 6f 2e 6e 44 61 74 61 2b  rt( (info.nData+
109cc 28 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3f 30  (pPage->intKey?0
109cd 3a 69 6e 66 6f 2e 6e 4b 65 79 29 29 3d 3d 69 6e  :info.nKey))==in
109ce 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20  fo.nPayload );. 
109cf 20 20 20 69 66 28 20 28 69 6e 66 6f 2e 6e 44 61     if( (info.nDa
109d0 74 61 2b 28 70 50 61 67 65 2d 3e 69 6e 74 4b 65  ta+(pPage->intKe
109d1 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 79 29 29 3e  y?0:info.nKey))>
109d2 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20  info.nLocal ){. 
109d3 20 20 20 20 20 50 67 6e 6f 20 6f 76 66 6c 20 3d       Pgno ovfl =
109d4 20 67 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c   get4byte(&pCell
109d5 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d  [info.iOverflow]
109d6 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
109d7 70 74 72 6d 61 70 50 75 74 28 70 50 61 67 65 2d  ptrmapPut(pPage-
109d8 3e 70 42 74 2c 20 6f 76 66 6c 2c 20 50 54 52 4d  >pBt, ovfl, PTRM
109d9 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 70 50  AP_OVERFLOW1, pP
109da 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20  age->pgno);.    
109db 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  }.  }.  return S
109dc 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 2f 2a 0a 2a  QLITE_OK;.}./*.*
109dd 2a 20 49 66 20 74 68 65 20 63 65 6c 6c 20 77 69  * If the cell wi
109de 74 68 20 69 6e 64 65 78 20 69 43 65 6c 6c 20 6f  th index iCell o
109df 6e 20 70 61 67 65 20 70 50 61 67 65 20 63 6f 6e  n page pPage con
109e0 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 0a  tains a pointer.
109e1 2a 2a 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f  ** to an overflo
109e2 77 20 70 61 67 65 2c 20 69 6e 73 65 72 74 20 61  w page, insert a
109e3 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65  n entry into the
109e4 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20   pointer-map.** 
109e5 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77  for the overflow
109e6 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   page..*/.static
109e7 20 69 6e 74 20 70 74 72 6d 61 70 50 75 74 4f 76   int ptrmapPutOv
109e8 66 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  fl(MemPage *pPag
109e9 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20  e, int iCell){. 
109ea 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 61 73   u8 *pCell;.  as
109eb 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
109ec 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
109ed 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
109ee 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 4f 76 65   pCell = findOve
109ef 72 66 6c 6f 77 43 65 6c 6c 28 70 50 61 67 65 2c  rflowCell(pPage,
109f0 20 69 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72   iCell);.  retur
109f1 6e 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50  n ptrmapPutOvflP
109f2 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 29  tr(pPage, pCell)
109f3 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  ;.}.#endif.../*.
109f4 2a 2a 20 44 65 66 72 61 67 6d 65 6e 74 20 74 68  ** Defragment th
109f5 65 20 70 61 67 65 20 67 69 76 65 6e 2e 20 20 41  e page given.  A
109f6 6c 6c 20 43 65 6c 6c 73 20 61 72 65 20 6d 6f 76  ll Cells are mov
109f7 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 65 6e 64  ed to the.** end
109f8 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 6e 64   of the page and
109f9 20 61 6c 6c 20 66 72 65 65 20 73 70 61 63 65 20   all free space 
109fa 69 73 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74  is collected int
109fb 6f 20 6f 6e 65 0a 2a 2a 20 62 69 67 20 46 72 65  o one.** big Fre
109fc 65 42 6c 6b 20 74 68 61 74 20 6f 63 63 75 72 73  eBlk that occurs
109fd 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20   in between the 
109fe 68 65 61 64 65 72 20 61 6e 64 20 63 65 6c 6c 0a  header and cell.
109ff 2a 2a 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79  ** pointer array
10a00 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f   and the cell co
10a01 6e 74 65 6e 74 20 61 72 65 61 2e 0a 2a 2f 0a 73  ntent area..*/.s
10a02 74 61 74 69 63 20 69 6e 74 20 64 65 66 72 61 67  tatic int defrag
10a03 6d 65 6e 74 50 61 67 65 28 4d 65 6d 50 61 67 65  mentPage(MemPage
10a04 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 6e 74 20   *pPage){.  int 
10a05 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
10a06 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
10a07 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  ounter */.  int 
10a08 70 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  pc;             
10a09 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
10a0a 73 20 6f 66 20 61 20 69 2d 74 68 20 63 65 6c 6c  s of a i-th cell
10a0b 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 3b 20   */.  int addr; 
10a0c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10a0d 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 66 69   /* Offset of fi
10a0e 72 73 74 20 62 79 74 65 20 61 66 74 65 72 20 63  rst byte after c
10a0f 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61  ell pointer arra
10a10 79 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20  y */.  int hdr; 
10a11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10a12 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74    /* Offset to t
10a13 68 65 20 70 61 67 65 20 68 65 61 64 65 72 20 2a  he page header *
10a14 2f 0a 20 20 69 6e 74 20 73 69 7a 65 3b 20 20 20  /.  int size;   
10a15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10a16 2a 20 53 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c  * Size of a cell
10a17 20 2a 2f 0a 20 20 69 6e 74 20 75 73 61 62 6c 65   */.  int usable
10a18 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20  Size;           
10a19 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73   /* Number of us
10a1a 61 62 6c 65 20 62 79 74 65 73 20 6f 6e 20 61 20  able bytes on a 
10a1b 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 63 65  page */.  int ce
10a1c 6c 6c 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20  llOffset;       
10a1d 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74       /* Offset t
10a1e 6f 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74  o the cell point
10a1f 65 72 20 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e  er array */.  in
10a20 74 20 62 72 6b 3b 20 20 20 20 20 20 20 20 20 20  t brk;          
10a21 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
10a22 65 74 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63  et to the cell c
10a23 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20  ontent area */. 
10a24 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20   int nCell;     
10a25 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
10a26 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f  umber of cells o
10a27 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20  n the page */.  
10a28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64  unsigned char *d
10a29 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 54 68  ata;       /* Th
10a2a 65 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20  e page data */. 
10a2b 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
10a2c 74 65 6d 70 3b 20 20 20 20 20 20 20 2f 2a 20 54  temp;       /* T
10a2d 65 6d 70 20 61 72 65 61 20 66 6f 72 20 63 65 6c  emp area for cel
10a2e 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 0a 20 20  l content */..  
10a2f 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
10a30 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
10a31 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20  pPage->pDbPage) 
10a32 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
10a33 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20  ge->pBt!=0 );.  
10a34 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70  assert( pPage->p
10a35 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3c  Bt->usableSize <
10a36 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47  = SQLITE_MAX_PAG
10a37 45 5f 53 49 5a 45 20 29 3b 0a 20 20 61 73 73 65  E_SIZE );.  asse
10a38 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72  rt( pPage->nOver
10a39 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73  flow==0 );.  ass
10a3a 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
10a3b 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
10a3c 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
10a3d 74 65 6d 70 20 3d 20 73 71 6c 69 74 65 33 50 61  temp = sqlite3Pa
10a3e 67 65 72 54 65 6d 70 53 70 61 63 65 28 70 50 61  gerTempSpace(pPa
10a3f 67 65 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 29  ge->pBt->pPager)
10a40 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65  ;.  data = pPage
10a41 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20 3d  ->aData;.  hdr =
10a42 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65   pPage->hdrOffse
10a43 74 3b 0a 20 20 63 65 6c 6c 4f 66 66 73 65 74 20  t;.  cellOffset 
10a44 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66  = pPage->cellOff
10a45 73 65 74 3b 0a 20 20 6e 43 65 6c 6c 20 3d 20 70  set;.  nCell = p
10a46 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 61  Page->nCell;.  a
10a47 73 73 65 72 74 28 20 6e 43 65 6c 6c 3d 3d 67 65  ssert( nCell==ge
10a48 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72  t2byte(&data[hdr
10a49 2b 33 5d 29 20 29 3b 0a 20 20 75 73 61 62 6c 65  +3]) );.  usable
10a4a 53 69 7a 65 20 3d 20 70 50 61 67 65 2d 3e 70 42  Size = pPage->pB
10a4b 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20  t->usableSize;. 
10a4c 20 62 72 6b 20 3d 20 67 65 74 32 62 79 74 65 28   brk = get2byte(
10a4d 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20  &data[hdr+5]);. 
10a4e 20 6d 65 6d 63 70 79 28 26 74 65 6d 70 5b 62 72   memcpy(&temp[br
10a4f 6b 5d 2c 20 26 64 61 74 61 5b 62 72 6b 5d 2c 20  k], &data[brk], 
10a50 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 62 72 6b  usableSize - brk
10a51 29 3b 0a 20 20 62 72 6b 20 3d 20 75 73 61 62 6c  );.  brk = usabl
10a52 65 53 69 7a 65 3b 0a 20 20 66 6f 72 28 69 3d 30  eSize;.  for(i=0
10a53 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b  ; i<nCell; i++){
10a54 0a 20 20 20 20 75 38 20 2a 70 41 64 64 72 3b 20  .    u8 *pAddr; 
10a55 20 20 20 20 2f 2a 20 54 68 65 20 69 2d 74 68 20      /* The i-th 
10a56 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a  cell pointer */.
10a57 20 20 20 20 70 41 64 64 72 20 3d 20 26 64 61 74      pAddr = &dat
10a58 61 5b 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 69  a[cellOffset + i
10a59 2a 32 5d 3b 0a 20 20 20 20 70 63 20 3d 20 67 65  *2];.    pc = ge
10a5a 74 32 62 79 74 65 28 70 41 64 64 72 29 3b 0a 20  t2byte(pAddr);. 
10a5b 20 20 20 61 73 73 65 72 74 28 20 70 63 3c 70 50     assert( pc<pP
10a5c 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  age->pBt->usable
10a5d 53 69 7a 65 20 29 3b 0a 20 20 20 20 73 69 7a 65  Size );.    size
10a5e 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70   = cellSizePtr(p
10a5f 50 61 67 65 2c 20 26 74 65 6d 70 5b 70 63 5d 29  Page, &temp[pc])
10a60 3b 0a 20 20 20 20 62 72 6b 20 2d 3d 20 73 69 7a  ;.    brk -= siz
10a61 65 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64  e;.    memcpy(&d
10a62 61 74 61 5b 62 72 6b 5d 2c 20 26 74 65 6d 70 5b  ata[brk], &temp[
10a63 70 63 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20  pc], size);.    
10a64 70 75 74 32 62 79 74 65 28 70 41 64 64 72 2c 20  put2byte(pAddr, 
10a65 62 72 6b 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65  brk);.  }.  asse
10a66 72 74 28 20 62 72 6b 3e 3d 63 65 6c 6c 4f 66 66  rt( brk>=cellOff
10a67 73 65 74 2b 32 2a 6e 43 65 6c 6c 20 29 3b 0a 20  set+2*nCell );. 
10a68 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b   put2byte(&data[
10a69 68 64 72 2b 35 5d 2c 20 62 72 6b 29 3b 0a 20 20  hdr+5], brk);.  
10a6a 64 61 74 61 5b 68 64 72 2b 31 5d 20 3d 20 30 3b  data[hdr+1] = 0;
10a6b 0a 20 20 64 61 74 61 5b 68 64 72 2b 32 5d 20 3d  .  data[hdr+2] =
10a6c 20 30 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 37   0;.  data[hdr+7
10a6d 5d 20 3d 20 30 3b 0a 20 20 61 64 64 72 20 3d 20  ] = 0;.  addr = 
10a6e 63 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 6e 43 65  cellOffset+2*nCe
10a6f 6c 6c 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61  ll;.  memset(&da
10a70 74 61 5b 61 64 64 72 5d 2c 20 30 2c 20 62 72 6b  ta[addr], 0, brk
10a71 2d 61 64 64 72 29 3b 0a 20 20 72 65 74 75 72 6e  -addr);.  return
10a72 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
10a73 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42  *.** Allocate nB
10a74 79 74 65 20 62 79 74 65 73 20 6f 66 20 73 70 61  yte bytes of spa
10a75 63 65 20 6f 6e 20 61 20 70 61 67 65 2e 0a 2a 2a  ce on a page..**
10a76 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 69  .** Return the i
10a77 6e 64 65 78 20 69 6e 74 6f 20 70 50 61 67 65 2d  ndex into pPage-
10a78 3e 61 44 61 74 61 5b 5d 20 6f 66 20 74 68 65 20  >aData[] of the 
10a79 66 69 72 73 74 20 62 79 74 65 20 6f 66 0a 2a 2a  first byte of.**
10a7a 20 74 68 65 20 6e 65 77 20 61 6c 6c 6f 63 61 74   the new allocat
10a7b 69 6f 6e 2e 20 4f 72 20 72 65 74 75 72 6e 20 30  ion. Or return 0
10a7c 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74   if there is not
10a7d 20 65 6e 6f 75 67 68 20 66 72 65 65 0a 2a 2a 20   enough free.** 
10a7e 73 70 61 63 65 20 6f 6e 20 74 68 65 20 70 61 67  space on the pag
10a7f 65 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65  e to satisfy the
10a80 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75   allocation requ
10a81 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  est..**.** If th
10a82 65 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 73 20  e page contains 
10a83 6e 42 79 74 65 73 20 6f 66 20 66 72 65 65 20 73  nBytes of free s
10a84 70 61 63 65 20 62 75 74 20 64 6f 65 73 20 6e 6f  pace but does no
10a85 74 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 6e 42 79  t contain.** nBy
10a86 74 65 73 20 6f 66 20 63 6f 6e 74 69 67 75 6f 75  tes of contiguou
10a87 73 20 66 72 65 65 20 73 70 61 63 65 2c 20 74 68  s free space, th
10a88 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
10a89 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a  automatically.**
10a8a 20 63 61 6c 6c 73 20 64 65 66 72 61 67 65 6d 65   calls defrageme
10a8b 6e 74 50 61 67 65 28 29 20 74 6f 20 63 6f 6e 73  ntPage() to cons
10a8c 6f 6c 69 64 61 74 65 20 61 6c 6c 20 66 72 65 65  olidate all free
10a8d 20 73 70 61 63 65 20 62 65 66 6f 72 65 20 0a 2a   space before .*
10a8e 2a 20 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68 65  * allocating the
10a8f 20 6e 65 77 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 73   new chunk..*/.s
10a90 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61  tatic int alloca
10a91 74 65 53 70 61 63 65 28 4d 65 6d 50 61 67 65 20  teSpace(MemPage 
10a92 2a 70 50 61 67 65 2c 20 69 6e 74 20 6e 42 79 74  *pPage, int nByt
10a93 65 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 2c 20  e){.  int addr, 
10a94 70 63 2c 20 68 64 72 3b 0a 20 20 69 6e 74 20 73  pc, hdr;.  int s
10a95 69 7a 65 3b 0a 20 20 69 6e 74 20 6e 46 72 61 67  ize;.  int nFrag
10a96 3b 0a 20 20 69 6e 74 20 74 6f 70 3b 0a 20 20 69  ;.  int top;.  i
10a97 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 69 6e 74 20  nt nCell;.  int 
10a98 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 75 6e  cellOffset;.  un
10a99 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74  signed char *dat
10a9a 61 3b 0a 20 20 0a 20 20 64 61 74 61 20 3d 20 70  a;.  .  data = p
10a9b 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 61  Page->aData;.  a
10a9c 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
10a9d 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
10a9e 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
10a9f 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
10aa0 65 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65  e->pBt );.  asse
10aa1 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
10aa2 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42  x_held(pPage->pB
10aa3 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69  t->mutex) );.  i
10aa4 66 28 20 6e 42 79 74 65 3c 34 20 29 20 6e 42 79  f( nByte<4 ) nBy
10aa5 74 65 20 3d 20 34 3b 0a 20 20 69 66 28 20 70 50  te = 4;.  if( pP
10aa6 61 67 65 2d 3e 6e 46 72 65 65 3c 6e 42 79 74 65  age->nFree<nByte
10aa7 20 7c 7c 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72   || pPage->nOver
10aa8 66 6c 6f 77 3e 30 20 29 20 72 65 74 75 72 6e 20  flow>0 ) return 
10aa9 30 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65  0;.  pPage->nFre
10aaa 65 20 2d 3d 20 6e 42 79 74 65 3b 0a 20 20 68 64  e -= nByte;.  hd
10aab 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66  r = pPage->hdrOf
10aac 66 73 65 74 3b 0a 0a 20 20 6e 46 72 61 67 20 3d  fset;..  nFrag =
10aad 20 64 61 74 61 5b 68 64 72 2b 37 5d 3b 0a 20 20   data[hdr+7];.  
10aae 69 66 28 20 6e 46 72 61 67 3c 36 30 20 29 7b 0a  if( nFrag<60 ){.
10aaf 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20 74 68      /* Search th
10ab0 65 20 66 72 65 65 6c 69 73 74 20 6c 6f 6f 6b 69  e freelist looki
10ab1 6e 67 20 66 6f 72 20 61 20 73 6c 6f 74 20 62 69  ng for a slot bi
10ab2 67 20 65 6e 6f 75 67 68 20 74 6f 20 73 61 74 69  g enough to sati
10ab3 73 66 79 20 74 68 65 0a 20 20 20 20 2a 2a 20 73  sfy the.    ** s
10ab4 70 61 63 65 20 72 65 71 75 65 73 74 2e 20 2a 2f  pace request. */
10ab5 0a 20 20 20 20 61 64 64 72 20 3d 20 68 64 72 2b  .    addr = hdr+
10ab6 31 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 70  1;.    while( (p
10ab7 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  c = get2byte(&da
10ab8 74 61 5b 61 64 64 72 5d 29 29 3e 30 20 29 7b 0a  ta[addr]))>0 ){.
10ab9 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67 65 74        size = get
10aba 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 2b 32  2byte(&data[pc+2
10abb 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73 69  ]);.      if( si
10abc 7a 65 3e 3d 6e 42 79 74 65 20 29 7b 0a 20 20 20  ze>=nByte ){.   
10abd 20 20 20 20 20 69 66 28 20 73 69 7a 65 3c 6e 42       if( size<nB
10abe 79 74 65 2b 34 20 29 7b 0a 20 20 20 20 20 20 20  yte+4 ){.       
10abf 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b     memcpy(&data[
10ac0 61 64 64 72 5d 2c 20 26 64 61 74 61 5b 70 63 5d  addr], &data[pc]
10ac1 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 2);.          
10ac2 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d 20 6e 46  data[hdr+7] = nF
10ac3 72 61 67 20 2b 20 73 69 7a 65 20 2d 20 6e 42 79  rag + size - nBy
10ac4 74 65 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65  te;.          re
10ac5 74 75 72 6e 20 70 63 3b 0a 20 20 20 20 20 20 20  turn pc;.       
10ac6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
10ac7 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61    put2byte(&data
10ac8 5b 70 63 2b 32 5d 2c 20 73 69 7a 65 2d 6e 42 79  [pc+2], size-nBy
10ac9 74 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72  te);.          r
10aca 65 74 75 72 6e 20 70 63 20 2b 20 73 69 7a 65 20  eturn pc + size 
10acb 2d 20 6e 42 79 74 65 3b 0a 20 20 20 20 20 20 20  - nByte;.       
10acc 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
10acd 20 61 64 64 72 20 3d 20 70 63 3b 0a 20 20 20 20   addr = pc;.    
10ace 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  }.  }..  /* Allo
10acf 63 61 74 65 20 6d 65 6d 6f 72 79 20 66 72 6f 6d  cate memory from
10ad0 20 74 68 65 20 67 61 70 20 69 6e 20 62 65 74 77   the gap in betw
10ad1 65 65 6e 20 74 68 65 20 63 65 6c 6c 20 70 6f 69  een the cell poi
10ad2 6e 74 65 72 20 61 72 72 61 79 0a 20 20 2a 2a 20  nter array.  ** 
10ad3 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e  and the cell con
10ad4 74 65 6e 74 20 61 72 65 61 2e 0a 20 20 2a 2f 0a  tent area..  */.
10ad5 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 65    top = get2byte
10ad6 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a  (&data[hdr+5]);.
10ad7 20 20 6e 43 65 6c 6c 20 3d 20 67 65 74 32 62 79    nCell = get2by
10ad8 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 29  te(&data[hdr+3])
10ad9 3b 0a 20 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d  ;.  cellOffset =
10ada 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73   pPage->cellOffs
10adb 65 74 3b 0a 20 20 69 66 28 20 6e 46 72 61 67 3e  et;.  if( nFrag>
10adc 3d 36 30 20 7c 7c 20 63 65 6c 6c 4f 66 66 73 65  =60 || cellOffse
10add 74 20 2b 20 32 2a 6e 43 65 6c 6c 20 3e 20 74 6f  t + 2*nCell > to
10ade 70 20 2d 20 6e 42 79 74 65 20 29 7b 0a 20 20 20  p - nByte ){.   
10adf 20 69 66 28 20 64 65 66 72 61 67 6d 65 6e 74 50   if( defragmentP
10ae0 61 67 65 28 70 50 61 67 65 29 20 29 20 72 65 74  age(pPage) ) ret
10ae1 75 72 6e 20 30 3b 0a 20 20 20 20 74 6f 70 20 3d  urn 0;.    top =
10ae2 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
10ae3 68 64 72 2b 35 5d 29 3b 0a 20 20 7d 0a 20 20 74  hdr+5]);.  }.  t
10ae4 6f 70 20 2d 3d 20 6e 42 79 74 65 3b 0a 20 20 61  op -= nByte;.  a
10ae5 73 73 65 72 74 28 20 63 65 6c 6c 4f 66 66 73 65  ssert( cellOffse
10ae6 74 20 2b 20 32 2a 6e 43 65 6c 6c 20 3c 3d 20 74  t + 2*nCell <= t
10ae7 6f 70 20 29 3b 0a 20 20 70 75 74 32 62 79 74 65  op );.  put2byte
10ae8 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 74  (&data[hdr+5], t
10ae9 6f 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 74 6f  op);.  return to
10aea 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  p;.}../*.** Retu
10aeb 72 6e 20 61 20 73 65 63 74 69 6f 6e 20 6f 66 20  rn a section of 
10aec 74 68 65 20 70 50 61 67 65 2d 3e 61 44 61 74 61  the pPage->aData
10aed 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74   to the freelist
10aee 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 62  ..** The first b
10aef 79 74 65 20 6f 66 20 74 68 65 20 6e 65 77 20 66  yte of the new f
10af0 72 65 65 20 62 6c 6f 63 6b 20 69 73 20 70 50 61  ree block is pPa
10af1 67 65 2d 3e 61 44 69 73 6b 5b 73 74 61 72 74 5d  ge->aDisk[start]
10af2 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 69 7a 65  .** and the size
10af3 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b 20 69 73   of the block is
10af4 20 22 73 69 7a 65 22 20 62 79 74 65 73 2e 0a 2a   "size" bytes..*
10af5 2a 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65  *.** Most of the
10af6 20 65 66 66 6f 72 74 20 68 65 72 65 20 69 73 20   effort here is 
10af7 69 6e 76 6f 6c 76 65 64 20 69 6e 20 63 6f 61 6c  involved in coal
10af8 65 73 69 6e 67 20 61 64 6a 61 63 65 6e 74 0a 2a  esing adjacent.*
10af9 2a 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 69 6e  * free blocks in
10afa 74 6f 20 61 20 73 69 6e 67 6c 65 20 62 69 67 20  to a single big 
10afb 66 72 65 65 20 62 6c 6f 63 6b 2e 0a 2a 2f 0a 73  free block..*/.s
10afc 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 53  tatic void freeS
10afd 70 61 63 65 28 4d 65 6d 50 61 67 65 20 2a 70 50  pace(MemPage *pP
10afe 61 67 65 2c 20 69 6e 74 20 73 74 61 72 74 2c 20  age, int start, 
10aff 69 6e 74 20 73 69 7a 65 29 7b 0a 20 20 69 6e 74  int size){.  int
10b00 20 61 64 64 72 2c 20 70 62 65 67 69 6e 2c 20 68   addr, pbegin, h
10b01 64 72 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  dr;.  unsigned c
10b02 68 61 72 20 2a 64 61 74 61 20 3d 20 70 50 61 67  har *data = pPag
10b03 65 2d 3e 61 44 61 74 61 3b 0a 0a 20 20 61 73 73  e->aData;..  ass
10b04 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 21  ert( pPage->pBt!
10b05 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
10b06 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
10b07 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70  iteable(pPage->p
10b08 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73  DbPage) );.  ass
10b09 65 72 74 28 20 73 74 61 72 74 3e 3d 70 50 61 67  ert( start>=pPag
10b0a 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 36 2b 28  e->hdrOffset+6+(
10b0b 70 50 61 67 65 2d 3e 6c 65 61 66 3f 30 3a 34 29  pPage->leaf?0:4)
10b0c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 73   );.  assert( (s
10b0d 74 61 72 74 20 2b 20 73 69 7a 65 29 3c 3d 70 50  tart + size)<=pP
10b0e 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  age->pBt->usable
10b0f 53 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74  Size );.  assert
10b10 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
10b11 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d  held(pPage->pBt-
10b12 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28  >mutex) );.  if(
10b13 20 73 69 7a 65 3c 34 20 29 20 73 69 7a 65 20 3d   size<4 ) size =
10b14 20 34 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49   4;..#ifdef SQLI
10b15 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45  TE_SECURE_DELETE
10b16 0a 20 20 2f 2a 20 4f 76 65 72 77 72 69 74 65 20  .  /* Overwrite 
10b17 64 65 6c 65 74 65 64 20 69 6e 66 6f 72 6d 61 74  deleted informat
10b18 69 6f 6e 20 77 69 74 68 20 7a 65 72 6f 73 20 77  ion with zeros w
10b19 68 65 6e 20 74 68 65 20 53 45 43 55 52 45 5f 44  hen the SECURE_D
10b1a 45 4c 45 54 45 20 0a 20 20 2a 2a 20 6f 70 74 69  ELETE .  ** opti
10b1b 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 20 61 74  on is enabled at
10b1c 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 2a 2f   compile-time */
10b1d 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b  .  memset(&data[
10b1e 73 74 61 72 74 5d 2c 20 30 2c 20 73 69 7a 65 29  start], 0, size)
10b1f 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41  ;.#endif..  /* A
10b20 64 64 20 74 68 65 20 73 70 61 63 65 20 62 61 63  dd the space bac
10b21 6b 20 69 6e 74 6f 20 74 68 65 20 6c 69 6e 6b 65  k into the linke
10b22 64 20 6c 69 73 74 20 6f 66 20 66 72 65 65 62 6c  d list of freebl
10b23 6f 63 6b 73 20 2a 2f 0a 20 20 68 64 72 20 3d 20  ocks */.  hdr = 
10b24 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
10b25 3b 0a 20 20 61 64 64 72 20 3d 20 68 64 72 20 2b  ;.  addr = hdr +
10b26 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 28 70 62   1;.  while( (pb
10b27 65 67 69 6e 20 3d 20 67 65 74 32 62 79 74 65 28  egin = get2byte(
10b28 26 64 61 74 61 5b 61 64 64 72 5d 29 29 3c 73 74  &data[addr]))<st
10b29 61 72 74 20 26 26 20 70 62 65 67 69 6e 3e 30 20  art && pbegin>0 
10b2a 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
10b2b 62 65 67 69 6e 3c 3d 70 50 61 67 65 2d 3e 70 42  begin<=pPage->pB
10b2c 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 34 20  t->usableSize-4 
10b2d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
10b2e 62 65 67 69 6e 3e 61 64 64 72 20 29 3b 0a 20 20  begin>addr );.  
10b2f 20 20 61 64 64 72 20 3d 20 70 62 65 67 69 6e 3b    addr = pbegin;
10b30 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
10b31 62 65 67 69 6e 3c 3d 70 50 61 67 65 2d 3e 70 42  begin<=pPage->pB
10b32 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 34 20  t->usableSize-4 
10b33 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 62 65  );.  assert( pbe
10b34 67 69 6e 3e 61 64 64 72 20 7c 7c 20 70 62 65 67  gin>addr || pbeg
10b35 69 6e 3d 3d 30 20 29 3b 0a 20 20 70 75 74 32 62  in==0 );.  put2b
10b36 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d 2c  yte(&data[addr],
10b37 20 73 74 61 72 74 29 3b 0a 20 20 70 75 74 32 62   start);.  put2b
10b38 79 74 65 28 26 64 61 74 61 5b 73 74 61 72 74 5d  yte(&data[start]
10b39 2c 20 70 62 65 67 69 6e 29 3b 0a 20 20 70 75 74  , pbegin);.  put
10b3a 32 62 79 74 65 28 26 64 61 74 61 5b 73 74 61 72  2byte(&data[star
10b3b 74 2b 32 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 70  t+2], size);.  p
10b3c 50 61 67 65 2d 3e 6e 46 72 65 65 20 2b 3d 20 73  Page->nFree += s
10b3d 69 7a 65 3b 0a 0a 20 20 2f 2a 20 43 6f 61 6c 65  ize;..  /* Coale
10b3e 73 63 65 20 61 64 6a 61 63 65 6e 74 20 66 72 65  sce adjacent fre
10b3f 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a 20 20 61 64  e blocks */.  ad
10b40 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f  dr = pPage->hdrO
10b41 66 66 73 65 74 20 2b 20 31 3b 0a 20 20 77 68 69  ffset + 1;.  whi
10b42 6c 65 28 20 28 70 62 65 67 69 6e 20 3d 20 67 65  le( (pbegin = ge
10b43 74 32 62 79 74 65 28 26 64 61 74 61 5b 61 64 64  t2byte(&data[add
10b44 72 5d 29 29 3e 30 20 29 7b 0a 20 20 20 20 69 6e  r]))>0 ){.    in
10b45 74 20 70 6e 65 78 74 2c 20 70 73 69 7a 65 3b 0a  t pnext, psize;.
10b46 20 20 20 20 61 73 73 65 72 74 28 20 70 62 65 67      assert( pbeg
10b47 69 6e 3e 61 64 64 72 20 29 3b 0a 20 20 20 20 61  in>addr );.    a
10b48 73 73 65 72 74 28 20 70 62 65 67 69 6e 3c 3d 70  ssert( pbegin<=p
10b49 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c  Page->pBt->usabl
10b4a 65 53 69 7a 65 2d 34 20 29 3b 0a 20 20 20 20 70  eSize-4 );.    p
10b4b 6e 65 78 74 20 3d 20 67 65 74 32 62 79 74 65 28  next = get2byte(
10b4c 26 64 61 74 61 5b 70 62 65 67 69 6e 5d 29 3b 0a  &data[pbegin]);.
10b4d 20 20 20 20 70 73 69 7a 65 20 3d 20 67 65 74 32      psize = get2
10b4e 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69  byte(&data[pbegi
10b4f 6e 2b 32 5d 29 3b 0a 20 20 20 20 69 66 28 20 70  n+2]);.    if( p
10b50 62 65 67 69 6e 20 2b 20 70 73 69 7a 65 20 2b 20  begin + psize + 
10b51 33 20 3e 3d 20 70 6e 65 78 74 20 26 26 20 70 6e  3 >= pnext && pn
10b52 65 78 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 69  ext>0 ){.      i
10b53 6e 74 20 66 72 61 67 20 3d 20 70 6e 65 78 74 20  nt frag = pnext 
10b54 2d 20 28 70 62 65 67 69 6e 2b 70 73 69 7a 65 29  - (pbegin+psize)
10b55 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
10b56 66 72 61 67 3c 3d 64 61 74 61 5b 70 50 61 67 65  frag<=data[pPage
10b57 2d 3e 68 64 72 4f 66 66 73 65 74 2b 37 5d 20 29  ->hdrOffset+7] )
10b58 3b 0a 20 20 20 20 20 20 64 61 74 61 5b 70 50 61  ;.      data[pPa
10b59 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 37 5d  ge->hdrOffset+7]
10b5a 20 2d 3d 20 66 72 61 67 3b 0a 20 20 20 20 20 20   -= frag;.      
10b5b 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  put2byte(&data[p
10b5c 62 65 67 69 6e 5d 2c 20 67 65 74 32 62 79 74 65  begin], get2byte
10b5d 28 26 64 61 74 61 5b 70 6e 65 78 74 5d 29 29 3b  (&data[pnext]));
10b5e 0a 20 20 20 20 20 20 70 75 74 32 62 79 74 65 28  .      put2byte(
10b5f 26 64 61 74 61 5b 70 62 65 67 69 6e 2b 32 5d 2c  &data[pbegin+2],
10b60 20 70 6e 65 78 74 2b 67 65 74 32 62 79 74 65 28   pnext+get2byte(
10b61 26 64 61 74 61 5b 70 6e 65 78 74 2b 32 5d 29 2d  &data[pnext+2])-
10b62 70 62 65 67 69 6e 29 3b 0a 20 20 20 20 7d 65 6c  pbegin);.    }el
10b63 73 65 7b 0a 20 20 20 20 20 20 61 64 64 72 20 3d  se{.      addr =
10b64 20 70 62 65 67 69 6e 3b 0a 20 20 20 20 7d 0a 20   pbegin;.    }. 
10b65 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20   }..  /* If the 
10b66 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65  cell content are
10b67 61 20 62 65 67 69 6e 73 20 77 69 74 68 20 61 20  a begins with a 
10b68 66 72 65 65 62 6c 6f 63 6b 2c 20 72 65 6d 6f 76  freeblock, remov
10b69 65 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 64  e it. */.  if( d
10b6a 61 74 61 5b 68 64 72 2b 31 5d 3d 3d 64 61 74 61  ata[hdr+1]==data
10b6b 5b 68 64 72 2b 35 5d 20 26 26 20 64 61 74 61 5b  [hdr+5] && data[
10b6c 68 64 72 2b 32 5d 3d 3d 64 61 74 61 5b 68 64 72  hdr+2]==data[hdr
10b6d 2b 36 5d 20 29 7b 0a 20 20 20 20 69 6e 74 20 74  +6] ){.    int t
10b6e 6f 70 3b 0a 20 20 20 20 70 62 65 67 69 6e 20 3d  op;.    pbegin =
10b6f 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
10b70 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6d 65 6d  hdr+1]);.    mem
10b71 63 70 79 28 26 64 61 74 61 5b 68 64 72 2b 31 5d  cpy(&data[hdr+1]
10b72 2c 20 26 64 61 74 61 5b 70 62 65 67 69 6e 5d 2c  , &data[pbegin],
10b73 20 32 29 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67   2);.    top = g
10b74 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  et2byte(&data[hd
10b75 72 2b 35 5d 29 3b 0a 20 20 20 20 70 75 74 32 62  r+5]);.    put2b
10b76 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d  yte(&data[hdr+5]
10b77 2c 20 74 6f 70 20 2b 20 67 65 74 32 62 79 74 65  , top + get2byte
10b78 28 26 64 61 74 61 5b 70 62 65 67 69 6e 2b 32 5d  (&data[pbegin+2]
10b79 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ));.  }.}../*.**
10b7a 20 44 65 63 6f 64 65 20 74 68 65 20 66 6c 61 67   Decode the flag
10b7b 73 20 62 79 74 65 20 28 74 68 65 20 66 69 72 73  s byte (the firs
10b7c 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 68 65  t byte of the he
10b7d 61 64 65 72 29 20 66 6f 72 20 61 20 70 61 67 65  ader) for a page
10b7e 0a 2a 2a 20 61 6e 64 20 69 6e 69 74 69 61 6c 69  .** and initiali
10b7f 7a 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65  ze fields of the
10b80 20 4d 65 6d 50 61 67 65 20 73 74 72 75 63 74 75   MemPage structu
10b81 72 65 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a  re accordingly..
10b82 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  */.static void d
10b83 65 63 6f 64 65 46 6c 61 67 73 28 4d 65 6d 50 61  ecodeFlags(MemPa
10b84 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 66  ge *pPage, int f
10b85 6c 61 67 42 79 74 65 29 7b 0a 20 20 42 74 53 68  lagByte){.  BtSh
10b86 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 2f  ared *pBt;     /
10b87 2a 20 41 20 63 6f 70 79 20 6f 66 20 70 50 61 67  * A copy of pPag
10b88 65 2d 3e 70 42 74 20 2a 2f 0a 0a 20 20 61 73 73  e->pBt */..  ass
10b89 65 72 74 28 20 70 50 61 67 65 2d 3e 68 64 72 4f  ert( pPage->hdrO
10b8a 66 66 73 65 74 3d 3d 28 70 50 61 67 65 2d 3e 70  ffset==(pPage->p
10b8b 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30  gno==1 ? 100 : 0
10b8c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
10b8d 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
10b8e 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75  d(pPage->pBt->mu
10b8f 74 65 78 29 20 29 3b 0a 20 20 70 50 61 67 65 2d  tex) );.  pPage-
10b90 3e 69 6e 74 4b 65 79 20 3d 20 28 66 6c 61 67 42  >intKey = (flagB
10b91 79 74 65 20 26 20 28 50 54 46 5f 49 4e 54 4b 45  yte & (PTF_INTKE
10b92 59 7c 50 54 46 5f 4c 45 41 46 44 41 54 41 29 29  Y|PTF_LEAFDATA))
10b93 21 3d 30 3b 0a 20 20 70 50 61 67 65 2d 3e 7a 65  !=0;.  pPage->ze
10b94 72 6f 44 61 74 61 20 3d 20 28 66 6c 61 67 42 79  roData = (flagBy
10b95 74 65 20 26 20 50 54 46 5f 5a 45 52 4f 44 41 54  te & PTF_ZERODAT
10b96 41 29 21 3d 30 3b 0a 20 20 70 50 61 67 65 2d 3e  A)!=0;.  pPage->
10b97 6c 65 61 66 20 3d 20 28 66 6c 61 67 42 79 74 65  leaf = (flagByte
10b98 20 26 20 50 54 46 5f 4c 45 41 46 29 21 3d 30 3b   & PTF_LEAF)!=0;
10b99 0a 20 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50  .  pPage->childP
10b9a 74 72 53 69 7a 65 20 3d 20 34 2a 28 70 50 61 67  trSize = 4*(pPag
10b9b 65 2d 3e 6c 65 61 66 3d 3d 30 29 3b 0a 20 20 70  e->leaf==0);.  p
10b9c 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b  Bt = pPage->pBt;
10b9d 0a 20 20 69 66 28 20 66 6c 61 67 42 79 74 65 20  .  if( flagByte 
10b9e 26 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 29  & PTF_LEAFDATA )
10b9f 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6c 65 61  {.    pPage->lea
10ba0 66 44 61 74 61 20 3d 20 31 3b 0a 20 20 20 20 70  fData = 1;.    p
10ba1 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d  Page->maxLocal =
10ba2 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 3b 0a 20   pBt->maxLeaf;. 
10ba3 20 20 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63     pPage->minLoc
10ba4 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c 65 61  al = pBt->minLea
10ba5 66 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  f;.  }else{.    
10ba6 70 50 61 67 65 2d 3e 6c 65 61 66 44 61 74 61 20  pPage->leafData 
10ba7 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  = 0;.    pPage->
10ba8 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e  maxLocal = pBt->
10ba9 6d 61 78 4c 6f 63 61 6c 3b 0a 20 20 20 20 70 50  maxLocal;.    pP
10baa 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20  age->minLocal = 
10bab 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20  pBt->minLocal;. 
10bac 20 7d 0a 20 20 70 50 61 67 65 2d 3e 68 61 73 44   }.  pPage->hasD
10bad 61 74 61 20 3d 20 21 28 70 50 61 67 65 2d 3e 7a  ata = !(pPage->z
10bae 65 72 6f 44 61 74 61 20 7c 7c 20 28 21 70 50 61  eroData || (!pPa
10baf 67 65 2d 3e 6c 65 61 66 20 26 26 20 70 50 61 67  ge->leaf && pPag
10bb0 65 2d 3e 6c 65 61 66 44 61 74 61 29 29 3b 0a 7d  e->leafData));.}
10bb1 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  ../*.** Initiali
10bb2 7a 65 20 74 68 65 20 61 75 78 69 6c 69 61 72 79  ze the auxiliary
10bb3 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72   information for
10bb4 20 61 20 64 69 73 6b 20 62 6c 6f 63 6b 2e 0a 2a   a disk block..*
10bb5 2a 0a 2a 2a 20 54 68 65 20 70 50 61 72 65 6e 74  *.** The pParent
10bb6 20 70 61 72 61 6d 65 74 65 72 20 6d 75 73 74 20   parameter must 
10bb7 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  be a pointer to 
10bb8 74 68 65 20 4d 65 6d 50 61 67 65 20 77 68 69 63  the MemPage whic
10bb9 68 0a 2a 2a 20 69 73 20 74 68 65 20 70 61 72 65  h.** is the pare
10bba 6e 74 20 6f 66 20 74 68 65 20 70 61 67 65 20 62  nt of the page b
10bbb 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65 64  eing initialized
10bbc 2e 20 20 54 68 65 20 72 6f 6f 74 20 6f 66 20 61  .  The root of a
10bbd 0a 2a 2a 20 42 54 72 65 65 20 68 61 73 20 6e 6f  .** BTree has no
10bbe 20 70 61 72 65 6e 74 20 61 6e 64 20 73 6f 20 66   parent and so f
10bbf 6f 72 20 74 68 61 74 20 70 61 67 65 2c 20 70 50  or that page, pP
10bc0 61 72 65 6e 74 3d 3d 4e 55 4c 4c 2e 0a 2a 2a 0a  arent==NULL..**.
10bc1 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  ** Return SQLITE
10bc2 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e 20  _OK on success. 
10bc3 20 49 66 20 77 65 20 73 65 65 20 74 68 61 74 20   If we see that 
10bc4 74 68 65 20 70 61 67 65 20 64 6f 65 73 0a 2a 2a  the page does.**
10bc5 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 77   not contain a w
10bc6 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61 62  ell-formed datab
10bc7 61 73 65 20 70 61 67 65 2c 20 74 68 65 6e 20 72  ase page, then r
10bc8 65 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45  eturn .** SQLITE
10bc9 5f 43 4f 52 52 55 50 54 2e 20 20 4e 6f 74 65 20  _CORRUPT.  Note 
10bca 74 68 61 74 20 61 20 72 65 74 75 72 6e 20 6f 66  that a return of
10bcb 20 53 51 4c 49 54 45 5f 4f 4b 20 64 6f 65 73 20   SQLITE_OK does 
10bcc 6e 6f 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65  not.** guarantee
10bcd 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 69   that the page i
10bce 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 2e 20 20  s well-formed.  
10bcf 49 74 20 6f 6e 6c 79 20 73 68 6f 77 73 20 74 68  It only shows th
10bd0 61 74 0a 2a 2a 20 77 65 20 66 61 69 6c 65 64 20  at.** we failed 
10bd1 74 6f 20 64 65 74 65 63 74 20 61 6e 79 20 63 6f  to detect any co
10bd2 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  rruption..*/.SQL
10bd3 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
10bd4 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74  sqlite3BtreeInit
10bd5 50 61 67 65 28 0a 20 20 4d 65 6d 50 61 67 65 20  Page(.  MemPage 
10bd6 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 2f  *pPage,        /
10bd7 2a 20 54 68 65 20 70 61 67 65 20 74 6f 20 62 65  * The page to be
10bd8 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a   initialized */.
10bd9 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 72 65    MemPage *pPare
10bda 6e 74 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  nt       /* The 
10bdb 70 61 72 65 6e 74 2e 20 20 4d 69 67 68 74 20 62  parent.  Might b
10bdc 65 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 69  e NULL */.){.  i
10bdd 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20 20  nt pc;          
10bde 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
10bdf 61 20 66 72 65 65 62 6c 6f 63 6b 20 77 69 74 68  a freeblock with
10be0 69 6e 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b  in pPage->aData[
10be1 5d 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20  ] */.  int hdr; 
10be2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66            /* Off
10be3 73 65 74 20 74 6f 20 62 65 67 69 6e 6e 69 6e 67  set to beginning
10be4 20 6f 66 20 70 61 67 65 20 68 65 61 64 65 72 20   of page header 
10be5 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20  */.  u8 *data;  
10be6 20 20 20 20 20 20 20 20 2f 2a 20 45 71 75 61 6c          /* Equal
10be7 20 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 61   to pPage->aData
10be8 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a   */.  BtShared *
10be9 70 42 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  pBt;        /* T
10bea 68 65 20 6d 61 69 6e 20 62 74 72 65 65 20 73 74  he main btree st
10beb 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74  ructure */.  int
10bec 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 20   usableSize;    
10bed 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 75 73 61  /* Amount of usa
10bee 62 6c 65 20 73 70 61 63 65 20 6f 6e 20 65 61 63  ble space on eac
10bef 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  h page */.  int 
10bf0 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 20 2f  cellOffset;    /
10bf1 2a 20 4f 66 66 73 65 74 20 66 72 6f 6d 20 73 74  * Offset from st
10bf2 61 72 74 20 6f 66 20 70 61 67 65 20 74 6f 20 66  art of page to f
10bf3 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65  irst cell pointe
10bf4 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 72 65 65  r */.  int nFree
10bf5 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ;         /* Num
10bf6 62 65 72 20 6f 66 20 75 6e 75 73 65 64 20 62 79  ber of unused by
10bf7 74 65 73 20 6f 6e 20 74 68 65 20 70 61 67 65 20  tes on the page 
10bf8 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 3b 20 20 20  */.  int top;   
10bf9 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
10bfa 20 62 79 74 65 20 6f 66 20 74 68 65 20 63 65 6c   byte of the cel
10bfb 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a  l content area *
10bfc 2f 0a 0a 20 20 70 42 74 20 3d 20 70 50 61 67 65  /..  pBt = pPage
10bfd 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28  ->pBt;.  assert(
10bfe 20 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73   pBt!=0 );.  ass
10bff 65 72 74 28 20 70 50 61 72 65 6e 74 3d 3d 30 20  ert( pParent==0 
10c00 7c 7c 20 70 50 61 72 65 6e 74 2d 3e 70 42 74 3d  || pParent->pBt=
10c01 3d 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72 74  =pBt );.  assert
10c02 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
10c03 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
10c04 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
10c05 61 67 65 2d 3e 70 67 6e 6f 3d 3d 73 71 6c 69 74  age->pgno==sqlit
10c06 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65  e3PagerPagenumbe
10c07 72 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  r(pPage->pDbPage
10c08 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
10c09 50 61 67 65 20 3d 3d 20 73 71 6c 69 74 65 33 50  Page == sqlite3P
10c0a 61 67 65 72 47 65 74 45 78 74 72 61 28 70 50 61  agerGetExtra(pPa
10c0b 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a  ge->pDbPage) );.
10c0c 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
10c0d 3e 61 44 61 74 61 20 3d 3d 20 73 71 6c 69 74 65  >aData == sqlite
10c0e 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 50  3PagerGetData(pP
10c0f 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  age->pDbPage) );
10c10 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 50  .  if( pPage->pP
10c11 61 72 65 6e 74 21 3d 70 50 61 72 65 6e 74 20 26  arent!=pParent &
10c12 26 20 28 70 50 61 67 65 2d 3e 70 50 61 72 65 6e  & (pPage->pParen
10c13 74 21 3d 30 20 7c 7c 20 70 50 61 67 65 2d 3e 69  t!=0 || pPage->i
10c14 73 49 6e 69 74 29 20 29 7b 0a 20 20 20 20 2f 2a  sInit) ){.    /*
10c15 20 54 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   The parent page
10c16 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 63 68   should never ch
10c17 61 6e 67 65 20 75 6e 6c 65 73 73 20 74 68 65 20  ange unless the 
10c18 66 69 6c 65 20 69 73 20 63 6f 72 72 75 70 74 20  file is corrupt 
10c19 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  */.    return SQ
10c1a 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
10c1b 54 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  T;.  }.  if( pPa
10c1c 67 65 2d 3e 69 73 49 6e 69 74 20 29 20 72 65 74  ge->isInit ) ret
10c1d 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
10c1e 20 69 66 28 20 70 50 61 67 65 2d 3e 70 50 61 72   if( pPage->pPar
10c1f 65 6e 74 3d 3d 30 20 26 26 20 70 50 61 72 65 6e  ent==0 && pParen
10c20 74 21 3d 30 20 29 7b 0a 20 20 20 20 70 50 61 67  t!=0 ){.    pPag
10c21 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 50 61  e->pParent = pPa
10c22 72 65 6e 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  rent;.    sqlite
10c23 33 50 61 67 65 72 52 65 66 28 70 50 61 72 65 6e  3PagerRef(pParen
10c24 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 7d  t->pDbPage);.  }
10c25 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e  .  hdr = pPage->
10c26 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 64 61 74  hdrOffset;.  dat
10c27 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61  a = pPage->aData
10c28 3b 0a 20 20 64 65 63 6f 64 65 46 6c 61 67 73 28  ;.  decodeFlags(
10c29 70 50 61 67 65 2c 20 64 61 74 61 5b 68 64 72 5d  pPage, data[hdr]
10c2a 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65  );.  pPage->nOve
10c2b 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 70 50 61  rflow = 0;.  pPa
10c2c 67 65 2d 3e 69 64 78 53 68 69 66 74 20 3d 20 30  ge->idxShift = 0
10c2d 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d  ;.  usableSize =
10c2e 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
10c2f 3b 0a 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f  ;.  pPage->cellO
10c30 66 66 73 65 74 20 3d 20 63 65 6c 6c 4f 66 66 73  ffset = cellOffs
10c31 65 74 20 3d 20 68 64 72 20 2b 20 31 32 20 2d 20  et = hdr + 12 - 
10c32 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20  4*pPage->leaf;. 
10c33 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 65 28   top = get2byte(
10c34 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20  &data[hdr+5]);. 
10c35 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20   pPage->nCell = 
10c36 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
10c37 64 72 2b 33 5d 29 3b 0a 20 20 69 66 28 20 70 50  dr+3]);.  if( pP
10c38 61 67 65 2d 3e 6e 43 65 6c 6c 3e 4d 58 5f 43 45  age->nCell>MX_CE
10c39 4c 4c 28 70 42 74 29 20 29 7b 0a 20 20 20 20 2f  LL(pBt) ){.    /
10c3a 2a 20 54 6f 20 6d 61 6e 79 20 63 65 6c 6c 73 20  * To many cells 
10c3b 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 70 61 67  for a single pag
10c3c 65 2e 20 20 54 68 65 20 70 61 67 65 20 6d 75 73  e.  The page mus
10c3d 74 20 62 65 20 63 6f 72 72 75 70 74 20 2a 2f 0a  t be corrupt */.
10c3e 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
10c3f 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
10c40 20 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 2d    }.  if( pPage-
10c41 3e 6e 43 65 6c 6c 3d 3d 30 20 26 26 20 70 50 61  >nCell==0 && pPa
10c42 72 65 6e 74 21 3d 30 20 26 26 20 70 50 61 72 65  rent!=0 && pPare
10c43 6e 74 2d 3e 70 67 6e 6f 21 3d 31 20 29 7b 0a 20  nt->pgno!=1 ){. 
10c44 20 20 20 2f 2a 20 41 6c 6c 20 70 61 67 65 73 20     /* All pages 
10c45 6d 75 73 74 20 68 61 76 65 20 61 74 20 6c 65 61  must have at lea
10c46 73 74 20 6f 6e 65 20 63 65 6c 6c 2c 20 65 78 63  st one cell, exc
10c47 65 70 74 20 66 6f 72 20 72 6f 6f 74 20 70 61 67  ept for root pag
10c48 65 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  es */.    return
10c49 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
10c4a 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  BKPT;.  }..  /* 
10c4b 43 6f 6d 70 75 74 65 20 74 68 65 20 74 6f 74 61  Compute the tota
10c4c 6c 20 66 72 65 65 20 73 70 61 63 65 20 6f 6e 20  l free space on 
10c4d 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 70 63  the page */.  pc
10c4e 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
10c4f 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 6e 46 72  a[hdr+1]);.  nFr
10c50 65 65 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d  ee = data[hdr+7]
10c51 20 2b 20 74 6f 70 20 2d 20 28 63 65 6c 6c 4f 66   + top - (cellOf
10c52 66 73 65 74 20 2b 20 32 2a 70 50 61 67 65 2d 3e  fset + 2*pPage->
10c53 6e 43 65 6c 6c 29 3b 0a 20 20 77 68 69 6c 65 28  nCell);.  while(
10c54 20 70 63 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74   pc>0 ){.    int
10c55 20 6e 65 78 74 2c 20 73 69 7a 65 3b 0a 20 20 20   next, size;.   
10c56 20 69 66 28 20 70 63 3e 75 73 61 62 6c 65 53 69   if( pc>usableSi
10c57 7a 65 2d 34 20 29 7b 0a 20 20 20 20 20 20 2f 2a  ze-4 ){.      /*
10c58 20 46 72 65 65 20 62 6c 6f 63 6b 20 69 73 20 6f   Free block is o
10c59 66 66 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20  ff the page */. 
10c5a 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
10c5b 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
10c5c 20 0a 20 20 20 20 7d 0a 20 20 20 20 6e 65 78 74   .    }.    next
10c5d 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
10c5e 61 5b 70 63 5d 29 3b 0a 20 20 20 20 73 69 7a 65  a[pc]);.    size
10c5f 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
10c60 61 5b 70 63 2b 32 5d 29 3b 0a 20 20 20 20 69 66  a[pc+2]);.    if
10c61 28 20 6e 65 78 74 3e 30 20 26 26 20 6e 65 78 74  ( next>0 && next
10c62 3c 3d 70 63 2b 73 69 7a 65 2b 33 20 29 7b 0a 20  <=pc+size+3 ){. 
10c63 20 20 20 20 20 2f 2a 20 46 72 65 65 20 62 6c 6f       /* Free blo
10c64 63 6b 73 20 6d 75 73 74 20 62 65 20 69 6e 20 61  cks must be in a
10c65 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a  ccending order *
10c66 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  /.      return S
10c67 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
10c68 50 54 3b 20 0a 20 20 20 20 7d 0a 20 20 20 20 6e  PT; .    }.    n
10c69 46 72 65 65 20 2b 3d 20 73 69 7a 65 3b 0a 20 20  Free += size;.  
10c6a 20 20 70 63 20 3d 20 6e 65 78 74 3b 0a 20 20 7d    pc = next;.  }
10c6b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20  .  pPage->nFree 
10c6c 3d 20 6e 46 72 65 65 3b 0a 20 20 69 66 28 20 6e  = nFree;.  if( n
10c6d 46 72 65 65 3e 3d 75 73 61 62 6c 65 53 69 7a 65  Free>=usableSize
10c6e 20 29 7b 0a 20 20 20 20 2f 2a 20 46 72 65 65 20   ){.    /* Free 
10c6f 73 70 61 63 65 20 63 61 6e 6e 6f 74 20 65 78 63  space cannot exc
10c70 65 65 64 20 74 6f 74 61 6c 20 70 61 67 65 20 73  eed total page s
10c71 69 7a 65 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  ize */.    retur
10c72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
10c73 5f 42 4b 50 54 3b 20 0a 20 20 7d 0a 0a 20 20 70  _BKPT; .  }..  p
10c74 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 31  Page->isInit = 1
10c75 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
10c76 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  E_OK;.}../*.** S
10c77 65 74 20 75 70 20 61 20 72 61 77 20 70 61 67 65  et up a raw page
10c78 20 73 6f 20 74 68 61 74 20 69 74 20 6c 6f 6f 6b   so that it look
10c79 73 20 6c 69 6b 65 20 61 20 64 61 74 61 62 61 73  s like a databas
10c7a 65 20 70 61 67 65 20 68 6f 6c 64 69 6e 67 0a 2a  e page holding.*
10c7b 2a 20 6e 6f 20 65 6e 74 72 69 65 73 2e 0a 2a 2f  * no entries..*/
10c7c 0a 73 74 61 74 69 63 20 76 6f 69 64 20 7a 65 72  .static void zer
10c7d 6f 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70  oPage(MemPage *p
10c7e 50 61 67 65 2c 20 69 6e 74 20 66 6c 61 67 73 29  Page, int flags)
10c7f 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  {.  unsigned cha
10c80 72 20 2a 64 61 74 61 20 3d 20 70 50 61 67 65 2d  r *data = pPage-
10c81 3e 61 44 61 74 61 3b 0a 20 20 42 74 53 68 61 72  >aData;.  BtShar
10c82 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d  ed *pBt = pPage-
10c83 3e 70 42 74 3b 0a 20 20 69 6e 74 20 68 64 72 20  >pBt;.  int hdr 
10c84 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  = pPage->hdrOffs
10c85 65 74 3b 0a 20 20 69 6e 74 20 66 69 72 73 74 3b  et;.  int first;
10c86 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
10c87 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62  te3PagerPagenumb
10c88 65 72 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  er(pPage->pDbPag
10c89 65 29 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20  e)==pPage->pgno 
10c8a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
10c8b 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72  ite3PagerGetExtr
10c8c 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  a(pPage->pDbPage
10c8d 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70 50 61 67  ) == (void*)pPag
10c8e 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  e );.  assert( s
10c8f 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61  qlite3PagerGetDa
10c90 74 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  ta(pPage->pDbPag
10c91 65 29 20 3d 3d 20 64 61 74 61 20 29 3b 0a 20 20  e) == data );.  
10c92 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
10c93 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
10c94 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20  pPage->pDbPage) 
10c95 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
10c96 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
10c97 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
10c98 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 68 64   memset(&data[hd
10c99 72 5d 2c 20 30 2c 20 70 42 74 2d 3e 75 73 61 62  r], 0, pBt->usab
10c9a 6c 65 53 69 7a 65 20 2d 20 68 64 72 29 3b 0a 20  leSize - hdr);. 
10c9b 20 64 61 74 61 5b 68 64 72 5d 20 3d 20 66 6c 61   data[hdr] = fla
10c9c 67 73 3b 0a 20 20 66 69 72 73 74 20 3d 20 68 64  gs;.  first = hd
10c9d 72 20 2b 20 38 20 2b 20 34 2a 28 28 66 6c 61 67  r + 8 + 4*((flag
10c9e 73 26 50 54 46 5f 4c 45 41 46 29 3d 3d 30 29 3b  s&PTF_LEAF)==0);
10c9f 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b  .  memset(&data[
10ca0 68 64 72 2b 31 5d 2c 20 30 2c 20 34 29 3b 0a 20  hdr+1], 0, 4);. 
10ca1 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d 20 30   data[hdr+7] = 0
10ca2 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61  ;.  put2byte(&da
10ca3 74 61 5b 68 64 72 2b 35 5d 2c 20 70 42 74 2d 3e  ta[hdr+5], pBt->
10ca4 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 20 70  usableSize);.  p
10ca5 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 70 42  Page->nFree = pB
10ca6 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20  t->usableSize - 
10ca7 66 69 72 73 74 3b 0a 20 20 64 65 63 6f 64 65 46  first;.  decodeF
10ca8 6c 61 67 73 28 70 50 61 67 65 2c 20 66 6c 61 67  lags(pPage, flag
10ca9 73 29 3b 0a 20 20 70 50 61 67 65 2d 3e 68 64 72  s);.  pPage->hdr
10caa 4f 66 66 73 65 74 20 3d 20 68 64 72 3b 0a 20 20  Offset = hdr;.  
10cab 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65  pPage->cellOffse
10cac 74 20 3d 20 66 69 72 73 74 3b 0a 20 20 70 50 61  t = first;.  pPa
10cad 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20  ge->nOverflow = 
10cae 30 3b 0a 20 20 70 50 61 67 65 2d 3e 69 64 78 53  0;.  pPage->idxS
10caf 68 69 66 74 20 3d 20 30 3b 0a 20 20 70 50 61 67  hift = 0;.  pPag
10cb0 65 2d 3e 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20 20  e->nCell = 0;.  
10cb1 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20  pPage->isInit = 
10cb2 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20  1;.}../*.** Get 
10cb3 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20  a page from the 
10cb4 70 61 67 65 72 2e 20 20 49 6e 69 74 69 61 6c 69  pager.  Initiali
10cb5 7a 65 20 74 68 65 20 4d 65 6d 50 61 67 65 2e 70  ze the MemPage.p
10cb6 42 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67  Bt and.** MemPag
10cb7 65 2e 61 44 61 74 61 20 65 6c 65 6d 65 6e 74 73  e.aData elements
10cb8 20 69 66 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a   if needed..**.*
10cb9 2a 20 49 66 20 74 68 65 20 6e 6f 43 6f 6e 74 65  * If the noConte
10cba 6e 74 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20  nt flag is set, 
10cbb 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65  it means that we
10cbc 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f   do not care abo
10cbd 75 74 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e  ut.** the conten
10cbe 74 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 74  t of the page at
10cbf 20 74 68 69 73 20 74 69 6d 65 2e 20 20 53 6f 20   this time.  So 
10cc0 64 6f 20 6e 6f 74 20 67 6f 20 74 6f 20 74 68 65  do not go to the
10cc1 20 64 69 73 6b 0a 2a 2a 20 74 6f 20 66 65 74 63   disk.** to fetc
10cc2 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e 20 20  h the content.  
10cc3 4a 75 73 74 20 66 69 6c 6c 20 69 6e 20 74 68 65  Just fill in the
10cc4 20 63 6f 6e 74 65 6e 74 20 77 69 74 68 20 7a 65   content with ze
10cc5 72 6f 73 20 66 6f 72 20 6e 6f 77 2e 0a 2a 2a 20  ros for now..** 
10cc6 49 66 20 69 6e 20 74 68 65 20 66 75 74 75 72 65  If in the future
10cc7 20 77 65 20 63 61 6c 6c 20 73 71 6c 69 74 65 33   we call sqlite3
10cc8 50 61 67 65 72 57 72 69 74 65 28 29 20 6f 6e 20  PagerWrite() on 
10cc9 74 68 69 73 20 70 61 67 65 2c 20 74 68 61 74 0a  this page, that.
10cca 2a 2a 20 6d 65 61 6e 73 20 77 65 20 68 61 76 65  ** means we have
10ccb 20 73 74 61 72 74 65 64 20 74 6f 20 62 65 20 63   started to be c
10ccc 6f 6e 63 65 72 6e 65 64 20 61 62 6f 75 74 20 63  oncerned about c
10ccd 6f 6e 74 65 6e 74 20 61 6e 64 20 74 68 65 20 64  ontent and the d
10cce 69 73 6b 0a 2a 2a 20 72 65 61 64 20 73 68 6f 75  isk.** read shou
10ccf 6c 64 20 6f 63 63 75 72 20 61 74 20 74 68 61 74  ld occur at that
10cd0 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54   point..*/.SQLIT
10cd1 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
10cd2 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
10cd3 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  e(.  BtShared *p
10cd4 42 74 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65  Bt,       /* The
10cd5 20 62 74 72 65 65 20 2a 2f 0a 20 20 50 67 6e 6f   btree */.  Pgno
10cd6 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20   pgno,          
10cd7 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 68   /* Number of th
10cd8 65 20 70 61 67 65 20 74 6f 20 66 65 74 63 68 20  e page to fetch 
10cd9 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70  */.  MemPage **p
10cda 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 52 65 74  pPage,    /* Ret
10cdb 75 72 6e 20 74 68 65 20 70 61 67 65 20 69 6e 20  urn the page in 
10cdc 74 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 2a  this parameter *
10cdd 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e  /.  int noConten
10cde 74 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e  t        /* Do n
10cdf 6f 74 20 6c 6f 61 64 20 70 61 67 65 20 63 6f 6e  ot load page con
10ce0 74 65 6e 74 20 69 66 20 74 72 75 65 20 2a 2f 0a  tent if true */.
10ce1 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d  ){.  int rc;.  M
10ce2 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20  emPage *pPage;. 
10ce3 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65   DbPage *pDbPage
10ce4 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
10ce5 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
10ce6 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
10ce7 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
10ce8 65 72 41 63 71 75 69 72 65 28 70 42 74 2d 3e 70  erAcquire(pBt->p
10ce9 50 61 67 65 72 2c 20 70 67 6e 6f 2c 20 28 44 62  Pager, pgno, (Db
10cea 50 61 67 65 2a 2a 29 26 70 44 62 50 61 67 65 2c  Page**)&pDbPage,
10ceb 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 69   noContent);.  i
10cec 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
10ced 63 3b 0a 20 20 70 50 61 67 65 20 3d 20 28 4d 65  c;.  pPage = (Me
10cee 6d 50 61 67 65 20 2a 29 73 71 6c 69 74 65 33 50  mPage *)sqlite3P
10cef 61 67 65 72 47 65 74 45 78 74 72 61 28 70 44 62  agerGetExtra(pDb
10cf0 50 61 67 65 29 3b 0a 20 20 70 50 61 67 65 2d 3e  Page);.  pPage->
10cf1 61 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 50  aData = sqlite3P
10cf2 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50  agerGetData(pDbP
10cf3 61 67 65 29 3b 0a 20 20 70 50 61 67 65 2d 3e 70  age);.  pPage->p
10cf4 44 62 50 61 67 65 20 3d 20 70 44 62 50 61 67 65  DbPage = pDbPage
10cf5 3b 0a 20 20 70 50 61 67 65 2d 3e 70 42 74 20 3d  ;.  pPage->pBt =
10cf6 20 70 42 74 3b 0a 20 20 70 50 61 67 65 2d 3e 70   pBt;.  pPage->p
10cf7 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20 20 70 50  gno = pgno;.  pP
10cf8 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 20 3d  age->hdrOffset =
10cf9 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20   pPage->pgno==1 
10cfa 3f 20 31 30 30 20 3a 20 30 3b 0a 20 20 2a 70 70  ? 100 : 0;.  *pp
10cfb 50 61 67 65 20 3d 20 70 50 61 67 65 3b 0a 20 20  Page = pPage;.  
10cfc 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
10cfd 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61  ;.}../*.** Get a
10cfe 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70   page from the p
10cff 61 67 65 72 20 61 6e 64 20 69 6e 69 74 69 61 6c  ager and initial
10d00 69 7a 65 20 69 74 2e 20 20 54 68 69 73 20 72 6f  ize it.  This ro
10d01 75 74 69 6e 65 0a 2a 2a 20 69 73 20 6a 75 73 74  utine.** is just
10d02 20 61 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77   a convenience w
10d03 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73 65  rapper around se
10d04 70 61 72 61 74 65 20 63 61 6c 6c 73 20 74 6f 0a  parate calls to.
10d05 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  ** sqlite3BtreeG
10d06 65 74 50 61 67 65 28 29 20 61 6e 64 20 73 71 6c  etPage() and sql
10d07 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67  ite3BtreeInitPag
10d08 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  e()..*/.static i
10d09 6e 74 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67  nt getAndInitPag
10d0a 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  e(.  BtShared *p
10d0b 42 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  Bt,          /* 
10d0c 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  The database fil
10d0d 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  e */.  Pgno pgno
10d0e 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ,           /* N
10d0f 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67  umber of the pag
10d10 65 20 74 6f 20 67 65 74 20 2a 2f 0a 20 20 4d 65  e to get */.  Me
10d11 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20  mPage **ppPage, 
10d12 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
10d13 70 61 67 65 20 70 6f 69 6e 74 65 72 20 68 65 72  page pointer her
10d14 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a  e */.  MemPage *
10d15 70 50 61 72 65 6e 74 20 20 20 20 20 2f 2a 20 50  pParent     /* P
10d16 61 72 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67  arent of the pag
10d17 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
10d18 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
10d19 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
10d1a 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
10d1b 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29 7b 0a 20  if( pgno==0 ){. 
10d1c 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
10d1d 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a  _CORRUPT_BKPT; .
10d1e 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74    }.  rc = sqlit
10d1f 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70  e3BtreeGetPage(p
10d20 42 74 2c 20 70 67 6e 6f 2c 20 70 70 50 61 67 65  Bt, pgno, ppPage
10d21 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  , 0);.  if( rc==
10d22 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 2a 70  SQLITE_OK && (*p
10d23 70 50 61 67 65 29 2d 3e 69 73 49 6e 69 74 3d 3d  pPage)->isInit==
10d24 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  0 ){.    rc = sq
10d25 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61  lite3BtreeInitPa
10d26 67 65 28 2a 70 70 50 61 67 65 2c 20 70 50 61 72  ge(*ppPage, pPar
10d27 65 6e 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ent);.    if( rc
10d28 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
10d29 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
10d2a 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20  (*ppPage);.     
10d2b 20 2a 70 70 50 61 67 65 20 3d 20 30 3b 0a 20 20   *ppPage = 0;.  
10d2c 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
10d2d 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
10d2e 6c 65 61 73 65 20 61 20 4d 65 6d 50 61 67 65 2e  lease a MemPage.
10d2f 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65    This should be
10d30 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72   called once for
10d31 20 65 61 63 68 20 70 72 69 6f 72 0a 2a 2a 20 63   each prior.** c
10d32 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74  all to sqlite3Bt
10d33 72 65 65 47 65 74 50 61 67 65 2e 0a 2a 2f 0a 73  reeGetPage..*/.s
10d34 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61  tatic void relea
10d35 73 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a  sePage(MemPage *
10d36 70 50 61 67 65 29 7b 0a 20 20 69 66 28 20 70 50  pPage){.  if( pP
10d37 61 67 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72  age ){.    asser
10d38 74 28 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20  t( pPage->aData 
10d39 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
10d3a 50 61 67 65 2d 3e 70 42 74 20 29 3b 0a 20 20 20  Page->pBt );.   
10d3b 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
10d3c 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70 50  PagerGetExtra(pP
10d3d 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d  age->pDbPage) ==
10d3e 20 28 76 6f 69 64 2a 29 70 50 61 67 65 20 29 3b   (void*)pPage );
10d3f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
10d40 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61  ite3PagerGetData
10d41 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
10d42 3d 3d 70 50 61 67 65 2d 3e 61 44 61 74 61 20 29  ==pPage->aData )
10d43 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71  ;.    assert( sq
10d44 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
10d45 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74  (pPage->pBt->mut
10d46 65 78 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ex) );.    sqlit
10d47 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61  e3PagerUnref(pPa
10d48 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  ge->pDbPage);.  
10d49 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
10d4a 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
10d4b 64 20 77 68 65 6e 20 74 68 65 20 72 65 66 65 72  d when the refer
10d4c 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72 20 61  ence count for a
10d4d 20 70 61 67 65 0a 2a 2a 20 72 65 61 63 68 65 73   page.** reaches
10d4e 20 7a 65 72 6f 2e 20 20 57 65 20 6e 65 65 64 20   zero.  We need 
10d4f 74 6f 20 75 6e 72 65 66 20 74 68 65 20 70 50 61  to unref the pPa
10d50 72 65 6e 74 20 70 6f 69 6e 74 65 72 20 77 68 65  rent pointer whe
10d51 6e 20 74 68 61 74 0a 2a 2a 20 68 61 70 70 65 6e  n that.** happen
10d52 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
10d53 64 20 70 61 67 65 44 65 73 74 72 75 63 74 6f 72  d pageDestructor
10d54 28 44 62 50 61 67 65 20 2a 70 44 61 74 61 2c 20  (DbPage *pData, 
10d55 69 6e 74 20 70 61 67 65 53 69 7a 65 29 7b 0a 20  int pageSize){. 
10d56 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
10d57 0a 20 20 61 73 73 65 72 74 28 20 28 70 61 67 65  .  assert( (page
10d58 53 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b 0a  Size & 7)==0 );.
10d59 20 20 70 50 61 67 65 20 3d 20 28 4d 65 6d 50 61    pPage = (MemPa
10d5a 67 65 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65  ge *)sqlite3Page
10d5b 72 47 65 74 45 78 74 72 61 28 70 44 61 74 61 29  rGetExtra(pData)
10d5c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
10d5d 65 2d 3e 69 73 49 6e 69 74 3d 3d 30 20 7c 7c 20  e->isInit==0 || 
10d5e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
10d5f 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d  ld(pPage->pBt->m
10d60 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70  utex) );.  if( p
10d61 50 61 67 65 2d 3e 70 50 61 72 65 6e 74 20 29 7b  Page->pParent ){
10d62 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  .    MemPage *pP
10d63 61 72 65 6e 74 20 3d 20 70 50 61 67 65 2d 3e 70  arent = pPage->p
10d64 50 61 72 65 6e 74 3b 0a 20 20 20 20 61 73 73 65  Parent;.    asse
10d65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 70 42 74  rt( pParent->pBt
10d66 3d 3d 70 50 61 67 65 2d 3e 70 42 74 20 29 3b 0a  ==pPage->pBt );.
10d67 20 20 20 20 70 50 61 67 65 2d 3e 70 50 61 72 65      pPage->pPare
10d68 6e 74 20 3d 20 30 3b 0a 20 20 20 20 72 65 6c 65  nt = 0;.    rele
10d69 61 73 65 50 61 67 65 28 70 50 61 72 65 6e 74 29  asePage(pParent)
10d6a 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 69  ;.  }.  pPage->i
10d6b 73 49 6e 69 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  sInit = 0;.}../*
10d6c 0a 2a 2a 20 44 75 72 69 6e 67 20 61 20 72 6f 6c  .** During a rol
10d6d 6c 62 61 63 6b 2c 20 77 68 65 6e 20 74 68 65 20  lback, when the 
10d6e 70 61 67 65 72 20 72 65 6c 6f 61 64 73 20 69 6e  pager reloads in
10d6f 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 74  formation into t
10d70 68 65 20 63 61 63 68 65 0a 2a 2a 20 73 6f 20 74  he cache.** so t
10d71 68 61 74 20 74 68 65 20 63 61 63 68 65 20 69 73  hat the cache is
10d72 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69 74 73   restored to its
10d73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 20   original state 
10d74 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 0a  at the start of.
10d75 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ** the transacti
10d76 6f 6e 2c 20 66 6f 72 20 65 61 63 68 20 70 61 67  on, for each pag
10d77 65 20 72 65 73 74 6f 72 65 64 20 74 68 69 73 20  e restored this 
10d78 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
10d79 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  d..**.** This ro
10d7a 75 74 69 6e 65 20 6e 65 65 64 73 20 74 6f 20 72  utine needs to r
10d7b 65 73 65 74 20 74 68 65 20 65 78 74 72 61 20 64  eset the extra d
10d7c 61 74 61 20 73 65 63 74 69 6f 6e 20 61 74 20 74  ata section at t
10d7d 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a  he end of the.**
10d7e 20 70 61 67 65 20 74 6f 20 61 67 72 65 65 20 77   page to agree w
10d7f 69 74 68 20 74 68 65 20 72 65 73 74 6f 72 65 64  ith the restored
10d80 20 64 61 74 61 2e 0a 2a 2f 0a 73 74 61 74 69 63   data..*/.static
10d81 20 76 6f 69 64 20 70 61 67 65 52 65 69 6e 69 74   void pageReinit
10d82 28 44 62 50 61 67 65 20 2a 70 44 61 74 61 2c 20  (DbPage *pData, 
10d83 69 6e 74 20 70 61 67 65 53 69 7a 65 29 7b 0a 20  int pageSize){. 
10d84 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
10d85 0a 20 20 61 73 73 65 72 74 28 20 28 70 61 67 65  .  assert( (page
10d86 53 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b 0a  Size & 7)==0 );.
10d87 20 20 70 50 61 67 65 20 3d 20 28 4d 65 6d 50 61    pPage = (MemPa
10d88 67 65 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65  ge *)sqlite3Page
10d89 72 47 65 74 45 78 74 72 61 28 70 44 61 74 61 29  rGetExtra(pData)
10d8a 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69  ;.  if( pPage->i
10d8b 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 61 73 73  sInit ){.    ass
10d8c 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
10d8d 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
10d8e 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
10d8f 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20    pPage->isInit 
10d90 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 0;.    sqlite3
10d91 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 50  BtreeInitPage(pP
10d92 61 67 65 2c 20 70 50 61 67 65 2d 3e 70 50 61 72  age, pPage->pPar
10d93 65 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ent);.  }.}../*.
10d94 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 62 75  ** Invoke the bu
10d95 73 79 20 68 61 6e 64 6c 65 72 20 66 6f 72 20 61  sy handler for a
10d96 20 62 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69   btree..*/.stati
10d97 63 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  c int sqlite3Btr
10d98 65 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64  eeInvokeBusyHand
10d99 6c 65 72 28 76 6f 69 64 20 2a 70 41 72 67 2c 20  ler(void *pArg, 
10d9a 69 6e 74 20 6e 29 7b 0a 20 20 42 74 53 68 61 72  int n){.  BtShar
10d9b 65 64 20 2a 70 42 74 20 3d 20 28 42 74 53 68 61  ed *pBt = (BtSha
10d9c 72 65 64 2a 29 70 41 72 67 3b 0a 20 20 61 73 73  red*)pArg;.  ass
10d9d 65 72 74 28 20 70 42 74 2d 3e 64 62 20 29 3b 0a  ert( pBt->db );.
10d9e 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
10d9f 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
10da0 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
10da1 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
10da2 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65  InvokeBusyHandle
10da3 72 28 26 70 42 74 2d 3e 64 62 2d 3e 62 75 73 79  r(&pBt->db->busy
10da4 48 61 6e 64 6c 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a  Handler);.}../*.
10da5 2a 2a 20 4f 70 65 6e 20 61 20 64 61 74 61 62 61  ** Open a databa
10da6 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20  se file..** .** 
10da7 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 74 68 65  zFilename is the
10da8 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   name of the dat
10da9 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20  abase file.  If 
10daa 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c  zFilename is NUL
10dab 4c 0a 2a 2a 20 61 20 6e 65 77 20 64 61 74 61 62  L.** a new datab
10dac 61 73 65 20 77 69 74 68 20 61 20 72 61 6e 64 6f  ase with a rando
10dad 6d 20 6e 61 6d 65 20 69 73 20 63 72 65 61 74 65  m name is create
10dae 64 2e 20 20 54 68 69 73 20 72 61 6e 64 6f 6d 6c  d.  This randoml
10daf 79 20 6e 61 6d 65 64 0a 2a 2a 20 64 61 74 61 62  y named.** datab
10db0 61 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 62 65  ase file will be
10db1 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 73 71   deleted when sq
10db2 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28  lite3BtreeClose(
10db3 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20  ) is called..** 
10db4 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20  If zFilename is 
10db5 22 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e 20  ":memory:" then 
10db6 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  an in-memory dat
10db7 61 62 61 73 65 20 69 73 20 63 72 65 61 74 65 64  abase is created
10db8 0a 2a 2a 20 74 68 61 74 20 69 73 20 61 75 74 6f  .** that is auto
10db9 6d 61 74 69 63 61 6c 6c 79 20 64 65 73 74 72 6f  matically destro
10dba 79 65 64 20 77 68 65 6e 20 69 74 20 69 73 20 63  yed when it is c
10dbb 6c 6f 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  losed..*/.SQLITE
10dbc 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
10dbd 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28 0a 20  ite3BtreeOpen(. 
10dbe 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
10dbf 6c 65 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65  lename,  /* Name
10dc0 20 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f 6e   of the file con
10dc1 74 61 69 6e 69 6e 67 20 74 68 65 20 42 54 72 65  taining the BTre
10dc2 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  e database */.  
10dc3 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
10dc4 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 6f 63          /* Assoc
10dc5 69 61 74 65 64 20 64 61 74 61 62 61 73 65 20 68  iated database h
10dc6 61 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72 65 65  andle */.  Btree
10dc7 20 2a 2a 70 70 42 74 72 65 65 2c 20 20 20 20 20   **ppBtree,     
10dc8 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
10dc9 20 6e 65 77 20 42 74 72 65 65 20 6f 62 6a 65 63   new Btree objec
10dca 74 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a  t written here *
10dcb 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  /.  int flags,  
10dcc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
10dcd 70 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20  ptions */.  int 
10dce 76 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 20  vfsFlags        
10dcf 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73      /* Flags pas
10dd0 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73  sed through to s
10dd1 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e  qlite3_vfs.xOpen
10dd2 28 29 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  () */.){.  sqlit
10dd3 65 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 20  e3_vfs *pVfs;   
10dd4 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 74 6f     /* The VFS to
10dd5 20 75 73 65 20 66 6f 72 20 74 68 69 73 20 62 74   use for this bt
10dd6 72 65 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65  ree */.  BtShare
10dd7 64 20 2a 70 42 74 20 3d 20 30 3b 20 20 20 20 20  d *pBt = 0;     
10dd8 20 2f 2a 20 53 68 61 72 65 64 20 70 61 72 74 20   /* Shared part 
10dd9 6f 66 20 62 74 72 65 65 20 73 74 72 75 63 74 75  of btree structu
10dda 72 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70  re */.  Btree *p
10ddb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
10ddc 2f 2a 20 48 61 6e 64 6c 65 20 74 6f 20 72 65 74  /* Handle to ret
10ddd 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20  urn */.  int rc 
10dde 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
10ddf 6e 74 20 6e 52 65 73 65 72 76 65 3b 0a 20 20 75  nt nReserve;.  u
10de0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a 44 62  nsigned char zDb
10de1 48 65 61 64 65 72 5b 31 30 30 5d 3b 0a 0a 20 20  Header[100];..  
10de2 2f 2a 20 53 65 74 20 74 68 65 20 76 61 72 69 61  /* Set the varia
10de3 62 6c 65 20 69 73 4d 65 6d 64 62 20 74 6f 20 74  ble isMemdb to t
10de4 72 75 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65  rue for an in-me
10de5 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20 6f  mory database, o
10de6 72 20 0a 20 20 2a 2a 20 66 61 6c 73 65 20 66 6f  r .  ** false fo
10de7 72 20 61 20 66 69 6c 65 2d 62 61 73 65 64 20 64  r a file-based d
10de8 61 74 61 62 61 73 65 2e 20 54 68 69 73 20 73 79  atabase. This sy
10de9 6d 62 6f 6c 20 69 73 20 6f 6e 6c 79 20 72 65 71  mbol is only req
10dea 75 69 72 65 64 20 69 66 0a 20 20 2a 2a 20 65 69  uired if.  ** ei
10deb 74 68 65 72 20 6f 66 20 74 68 65 20 73 68 61 72  ther of the shar
10dec 65 64 2d 64 61 74 61 20 6f 72 20 61 75 74 6f 76  ed-data or autov
10ded 61 63 75 75 6d 20 66 65 61 74 75 72 65 73 20 61  acuum features a
10dee 72 65 20 63 6f 6d 70 69 6c 65 64 20 0a 20 20 2a  re compiled .  *
10def 2a 20 69 6e 74 6f 20 74 68 65 20 6c 69 62 72 61  * into the libra
10df0 72 79 2e 0a 20 20 2a 2f 0a 23 69 66 20 21 64 65  ry..  */.#if !de
10df1 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
10df2 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29 20  T_SHARED_CACHE) 
10df3 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  || !defined(SQLI
10df4 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
10df5 55 4d 29 0a 20 20 23 69 66 64 65 66 20 53 51 4c  UM).  #ifdef SQL
10df6 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44  ITE_OMIT_MEMORYD
10df7 42 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20  B.    const int 
10df8 69 73 4d 65 6d 64 62 20 3d 20 30 3b 0a 20 20 23  isMemdb = 0;.  #
10df9 65 6c 73 65 0a 20 20 20 20 63 6f 6e 73 74 20 69  else.    const i
10dfa 6e 74 20 69 73 4d 65 6d 64 62 20 3d 20 7a 46 69  nt isMemdb = zFi
10dfb 6c 65 6e 61 6d 65 20 26 26 20 21 73 74 72 63 6d  lename && !strcm
10dfc 70 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 3a 6d  p(zFilename, ":m
10dfd 65 6d 6f 72 79 3a 22 29 3b 0a 20 20 23 65 6e 64  emory:");.  #end
10dfe 69 66 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73  if.#endif..  ass
10dff 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20  ert( db!=0 );.  
10e00 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
10e01 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d  mutex_held(db->m
10e02 75 74 65 78 29 20 29 3b 0a 0a 20 20 70 56 66 73  utex) );..  pVfs
10e03 20 3d 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20 70   = db->pVfs;.  p
10e04 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
10e05 5a 65 72 6f 28 73 69 7a 65 6f 66 28 42 74 72 65  Zero(sizeof(Btre
10e06 65 29 29 3b 0a 20 20 69 66 28 20 21 70 20 29 7b  e));.  if( !p ){
10e07 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
10e08 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
10e09 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 41  p->inTrans = TRA
10e0a 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 70 2d 3e 64 62  NS_NONE;.  p->db
10e0b 20 3d 20 64 62 3b 0a 0a 23 69 66 20 21 64 65 66   = db;..#if !def
10e0c 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
10e0d 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29 20 26  _SHARED_CACHE) &
10e0e 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
10e0f 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20  E_OMIT_DISKIO). 
10e10 20 2f 2a 0a 20 20 2a 2a 20 49 66 20 74 68 69 73   /*.  ** If this
10e11 20 42 74 72 65 65 20 69 73 20 61 20 63 61 6e 64   Btree is a cand
10e12 69 64 61 74 65 20 66 6f 72 20 73 68 61 72 65 64  idate for shared
10e13 20 63 61 63 68 65 2c 20 74 72 79 20 74 6f 20 66   cache, try to f
10e14 69 6e 64 20 61 6e 0a 20 20 2a 2a 20 65 78 69 73  ind an.  ** exis
10e15 74 69 6e 67 20 42 74 53 68 61 72 65 64 20 6f 62  ting BtShared ob
10e16 6a 65 63 74 20 74 68 61 74 20 77 65 20 63 61 6e  ject that we can
10e17 20 73 68 61 72 65 20 77 69 74 68 0a 20 20 2a 2f   share with.  */
10e18 0a 20 20 69 66 28 20 28 66 6c 61 67 73 20 26 20  .  if( (flags & 
10e19 42 54 52 45 45 5f 50 52 49 56 41 54 45 29 3d 3d  BTREE_PRIVATE)==
10e1a 30 0a 20 20 20 26 26 20 69 73 4d 65 6d 64 62 3d  0.   && isMemdb=
10e1b 3d 30 0a 20 20 20 26 26 20 28 64 62 2d 3e 66 6c  =0.   && (db->fl
10e1c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 56 74 61  ags & SQLITE_Vta
10e1d 62 29 3d 3d 30 0a 20 20 20 26 26 20 7a 46 69 6c  b)==0.   && zFil
10e1e 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61  ename && zFilena
10e1f 6d 65 5b 30 5d 0a 20 20 29 7b 0a 20 20 20 20 69  me[0].  ){.    i
10e20 66 28 20 73 71 6c 69 74 65 33 53 68 61 72 65 64  f( sqlite3Shared
10e21 43 61 63 68 65 45 6e 61 62 6c 65 64 20 29 7b 0a  CacheEnabled ){.
10e22 20 20 20 20 20 20 69 6e 74 20 6e 46 75 6c 6c 50        int nFullP
10e23 61 74 68 6e 61 6d 65 20 3d 20 70 56 66 73 2d 3e  athname = pVfs->
10e24 6d 78 50 61 74 68 6e 61 6d 65 2b 31 3b 0a 20 20  mxPathname+1;.  
10e25 20 20 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 50      char *zFullP
10e26 61 74 68 6e 61 6d 65 20 3d 20 28 63 68 61 72 20  athname = (char 
10e27 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
10e28 28 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b  (nFullPathname);
10e29 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
10e2a 75 74 65 78 20 2a 6d 75 74 65 78 53 68 61 72 65  utex *mutexShare
10e2b 64 3b 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 72  d;.      p->shar
10e2c 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 20 20  able = 1;.      
10e2d 69 66 28 20 64 62 20 29 7b 0a 20 20 20 20 20 20  if( db ){.      
10e2e 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53    db->flags |= S
10e2f 51 4c 49 54 45 5f 53 68 61 72 65 64 43 61 63 68  QLITE_SharedCach
10e30 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  e;.      }.     
10e31 20 69 66 28 20 21 7a 46 75 6c 6c 50 61 74 68 6e   if( !zFullPathn
10e32 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ame ){.        s
10e33 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a  qlite3_free(p);.
10e34 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
10e35 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
10e36 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
10e37 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65  e3OsFullPathname
10e38 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d 65  (pVfs, zFilename
10e39 2c 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c  , nFullPathname,
10e3a 20 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b   zFullPathname);
10e3b 0a 20 20 20 20 20 20 6d 75 74 65 78 53 68 61 72  .      mutexShar
10e3c 65 64 20 3d 20 73 71 6c 69 74 65 33 5f 6d 75 74  ed = sqlite3_mut
10e3d 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  ex_alloc(SQLITE_
10e3e 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
10e3f 54 45 52 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  TER);.      sqli
10e40 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
10e41 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20  mutexShared);.  
10e42 20 20 20 20 66 6f 72 28 70 42 74 3d 73 71 6c 69      for(pBt=sqli
10e43 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69  te3SharedCacheLi
10e44 73 74 3b 20 70 42 74 3b 20 70 42 74 3d 70 42 74  st; pBt; pBt=pBt
10e45 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20  ->pNext){.      
10e46 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6e    assert( pBt->n
10e47 52 65 66 3e 30 20 29 3b 0a 20 20 20 20 20 20 20  Ref>0 );.       
10e48 20 69 66 28 20 30 3d 3d 73 74 72 63 6d 70 28 7a   if( 0==strcmp(z
10e49 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 73 71  FullPathname, sq
10e4a 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61  lite3PagerFilena
10e4b 6d 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 29  me(pBt->pPager))
10e4c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
10e4d 20 20 26 26 20 73 71 6c 69 74 65 33 50 61 67 65    && sqlite3Page
10e4e 72 56 66 73 28 70 42 74 2d 3e 70 50 61 67 65 72  rVfs(pBt->pPager
10e4f 29 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 20  )==pVfs ){.     
10e50 20 20 20 20 20 70 2d 3e 70 42 74 20 3d 20 70 42       p->pBt = pB
10e51 74 3b 0a 20 20 20 20 20 20 20 20 20 20 70 42 74  t;.          pBt
10e52 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20  ->nRef++;.      
10e53 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
10e54 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
10e55 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
10e56 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61 72  _leave(mutexShar
10e57 65 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ed);.      sqlit
10e58 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61 74  e3_free(zFullPat
10e59 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23 69  hname);.    }.#i
10e5a 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
10e5b 47 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20  G.    else{.    
10e5c 20 20 2f 2a 20 49 6e 20 64 65 62 75 67 20 6d 6f    /* In debug mo
10e5d 64 65 2c 20 77 65 20 6d 61 72 6b 20 61 6c 6c 20  de, we mark all 
10e5e 70 65 72 73 69 73 74 65 6e 74 20 64 61 74 61 62  persistent datab
10e5f 61 73 65 73 20 61 73 20 73 68 61 72 61 62 6c 65  ases as sharable
10e60 0a 20 20 20 20 20 20 2a 2a 20 65 76 65 6e 20 77  .      ** even w
10e61 68 65 6e 20 74 68 65 79 20 61 72 65 20 6e 6f 74  hen they are not
10e62 2e 20 20 54 68 69 73 20 65 78 65 72 63 69 73 65  .  This exercise
10e63 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f  s the locking co
10e64 64 65 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a 20  de and.      ** 
10e65 67 69 76 65 73 20 6d 6f 72 65 20 6f 70 70 6f 72  gives more oppor
10e66 74 75 6e 69 74 79 20 66 6f 72 20 61 73 73 65 72  tunity for asser
10e67 74 73 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ts(sqlite3_mutex
10e68 5f 68 65 6c 64 28 29 29 0a 20 20 20 20 20 20 2a  _held()).      *
10e69 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20  * statements to 
10e6a 66 69 6e 64 20 6c 6f 63 6b 69 6e 67 20 70 72 6f  find locking pro
10e6b 62 6c 65 6d 73 2e 0a 20 20 20 20 20 20 2a 2f 0a  blems..      */.
10e6c 20 20 20 20 20 20 70 2d 3e 73 68 61 72 61 62 6c        p->sharabl
10e6d 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e  e = 1;.    }.#en
10e6e 64 69 66 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  dif.  }.#endif. 
10e6f 20 69 66 28 20 70 42 74 3d 3d 30 20 29 7b 0a 20   if( pBt==0 ){. 
10e70 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65     /*.    ** The
10e71 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72   following asser
10e72 74 73 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  ts make sure tha
10e73 74 20 73 74 72 75 63 74 75 72 65 73 20 75 73 65  t structures use
10e74 64 20 62 79 20 74 68 65 20 62 74 72 65 65 20 61  d by the btree a
10e75 72 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 69  re.    ** the ri
10e76 67 68 74 20 73 69 7a 65 2e 20 20 54 68 69 73 20  ght size.  This 
10e77 69 73 20 74 6f 20 67 75 61 72 64 20 61 67 61 69  is to guard agai
10e78 6e 73 74 20 73 69 7a 65 20 63 68 61 6e 67 65 73  nst size changes
10e79 20 74 68 61 74 20 72 65 73 75 6c 74 0a 20 20 20   that result.   
10e7a 20 2a 2a 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69   ** when compili
10e7b 6e 67 20 6f 6e 20 61 20 64 69 66 66 65 72 65 6e  ng on a differen
10e7c 74 20 61 72 63 68 69 74 65 63 74 75 72 65 2e 0a  t architecture..
10e7d 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
10e7e 74 28 20 73 69 7a 65 6f 66 28 69 36 34 29 3d 3d  t( sizeof(i64)==
10e7f 38 20 7c 7c 20 73 69 7a 65 6f 66 28 69 36 34 29  8 || sizeof(i64)
10e80 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==4 );.    asser
10e81 74 28 20 73 69 7a 65 6f 66 28 75 36 34 29 3d 3d  t( sizeof(u64)==
10e82 38 20 7c 7c 20 73 69 7a 65 6f 66 28 75 36 34 29  8 || sizeof(u64)
10e83 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==4 );.    asser
10e84 74 28 20 73 69 7a 65 6f 66 28 75 33 32 29 3d 3d  t( sizeof(u32)==
10e85 34 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  4 );.    assert(
10e86 20 73 69 7a 65 6f 66 28 75 31 36 29 3d 3d 32 20   sizeof(u16)==2 
10e87 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  );.    assert( s
10e88 69 7a 65 6f 66 28 50 67 6e 6f 29 3d 3d 34 20 29  izeof(Pgno)==4 )
10e89 3b 0a 20 20 0a 20 20 20 20 70 42 74 20 3d 20 73  ;.  .    pBt = s
10e8a 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
10e8b 28 20 73 69 7a 65 6f 66 28 2a 70 42 74 29 20 29  ( sizeof(*pBt) )
10e8c 3b 0a 20 20 20 20 69 66 28 20 70 42 74 3d 3d 30  ;.    if( pBt==0
10e8d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
10e8e 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
10e8f 20 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70     goto btree_op
10e90 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20  en_out;.    }.  
10e91 20 20 70 42 74 2d 3e 62 75 73 79 48 64 72 2e 78    pBt->busyHdr.x
10e92 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 42 74  Func = sqlite3Bt
10e93 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e  reeInvokeBusyHan
10e94 64 6c 65 72 3b 0a 20 20 20 20 70 42 74 2d 3e 62  dler;.    pBt->b
10e95 75 73 79 48 64 72 2e 70 41 72 67 20 3d 20 70 42  usyHdr.pArg = pB
10e96 74 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  t;.    rc = sqli
10e97 74 65 33 50 61 67 65 72 4f 70 65 6e 28 70 56 66  te3PagerOpen(pVf
10e98 73 2c 20 26 70 42 74 2d 3e 70 50 61 67 65 72 2c  s, &pBt->pPager,
10e99 20 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 20   zFilename,.    
10e9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10e9b 20 20 20 20 20 20 45 58 54 52 41 5f 53 49 5a 45        EXTRA_SIZE
10e9c 2c 20 66 6c 61 67 73 2c 20 76 66 73 46 6c 61 67  , flags, vfsFlag
10e9d 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  s);.    if( rc==
10e9e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
10e9f 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
10ea0 61 67 65 72 52 65 61 64 46 69 6c 65 68 65 61 64  agerReadFilehead
10ea1 65 72 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 73  er(pBt->pPager,s
10ea2 69 7a 65 6f 66 28 7a 44 62 48 65 61 64 65 72 29  izeof(zDbHeader)
10ea3 2c 7a 44 62 48 65 61 64 65 72 29 3b 0a 20 20 20  ,zDbHeader);.   
10ea4 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53   }.    if( rc!=S
10ea5 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
10ea6 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65    goto btree_ope
10ea7 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20  n_out;.    }.   
10ea8 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
10ea9 42 75 73 79 68 61 6e 64 6c 65 72 28 70 42 74 2d  Busyhandler(pBt-
10eaa 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 62  >pPager, &pBt->b
10eab 75 73 79 48 64 72 29 3b 0a 20 20 20 20 70 2d 3e  usyHdr);.    p->
10eac 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 0a 20 20  pBt = pBt;.  .  
10ead 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65    sqlite3PagerSe
10eae 74 44 65 73 74 72 75 63 74 6f 72 28 70 42 74 2d  tDestructor(pBt-
10eaf 3e 70 50 61 67 65 72 2c 20 70 61 67 65 44 65 73  >pPager, pageDes
10eb0 74 72 75 63 74 6f 72 29 3b 0a 20 20 20 20 73 71  tructor);.    sq
10eb1 6c 69 74 65 33 50 61 67 65 72 53 65 74 52 65 69  lite3PagerSetRei
10eb2 6e 69 74 65 72 28 70 42 74 2d 3e 70 50 61 67 65  niter(pBt->pPage
10eb3 72 2c 20 70 61 67 65 52 65 69 6e 69 74 29 3b 0a  r, pageReinit);.
10eb4 20 20 20 20 70 42 74 2d 3e 70 43 75 72 73 6f 72      pBt->pCursor
10eb5 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 70   = 0;.    pBt->p
10eb6 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 20 20 70  Page1 = 0;.    p
10eb7 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 73  Bt->readOnly = s
10eb8 71 6c 69 74 65 33 50 61 67 65 72 49 73 72 65 61  qlite3PagerIsrea
10eb9 64 6f 6e 6c 79 28 70 42 74 2d 3e 70 50 61 67 65  donly(pBt->pPage
10eba 72 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 61 67  r);.    pBt->pag
10ebb 65 53 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65  eSize = get2byte
10ebc 28 26 7a 44 62 48 65 61 64 65 72 5b 31 36 5d 29  (&zDbHeader[16])
10ebd 3b 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 70  ;.    if( pBt->p
10ebe 61 67 65 53 69 7a 65 3c 35 31 32 20 7c 7c 20 70  ageSize<512 || p
10ebf 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 53 51 4c  Bt->pageSize>SQL
10ec0 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a  ITE_MAX_PAGE_SIZ
10ec1 45 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 28 28  E.         || ((
10ec2 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2d 31 29  pBt->pageSize-1)
10ec3 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 21  &pBt->pageSize)!
10ec4 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d  =0 ){.      pBt-
10ec5 3e 70 61 67 65 53 69 7a 65 20 3d 20 30 3b 0a 20  >pageSize = 0;. 
10ec6 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
10ec7 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 42 74  rSetPagesize(pBt
10ec8 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e  ->pPager, &pBt->
10ec9 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20  pageSize);.     
10eca 20 70 42 74 2d 3e 6d 61 78 45 6d 62 65 64 46 72   pBt->maxEmbedFr
10ecb 61 63 20 3d 20 36 34 3b 20 20 20 2f 2a 20 32 35  ac = 64;   /* 25
10ecc 25 20 2a 2f 0a 20 20 20 20 20 20 70 42 74 2d 3e  % */.      pBt->
10ecd 6d 69 6e 45 6d 62 65 64 46 72 61 63 20 3d 20 33  minEmbedFrac = 3
10ece 32 3b 20 20 20 2f 2a 20 31 32 2e 35 25 20 2a 2f  2;   /* 12.5% */
10ecf 0a 20 20 20 20 20 20 70 42 74 2d 3e 6d 69 6e 4c  .      pBt->minL
10ed0 65 61 66 46 72 61 63 20 3d 20 33 32 3b 20 20 20  eafFrac = 32;   
10ed1 20 2f 2a 20 31 32 2e 35 25 20 2a 2f 0a 23 69 66   /* 12.5% */.#if
10ed2 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
10ed3 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
10ed4 20 20 2f 2a 20 49 66 20 74 68 65 20 6d 61 67 69    /* If the magi
10ed5 63 20 6e 61 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a  c name ":memory:
10ed6 22 20 77 69 6c 6c 20 63 72 65 61 74 65 20 61 6e  " will create an
10ed7 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62   in-memory datab
10ed8 61 73 65 2c 20 74 68 65 6e 0a 20 20 20 20 20 20  ase, then.      
10ed9 2a 2a 20 6c 65 61 76 65 20 74 68 65 20 61 75 74  ** leave the aut
10eda 6f 56 61 63 75 75 6d 20 6d 6f 64 65 20 61 74 20  oVacuum mode at 
10edb 30 20 28 64 6f 20 6e 6f 74 20 61 75 74 6f 2d 76  0 (do not auto-v
10edc 61 63 75 75 6d 29 2c 20 65 76 65 6e 20 69 66 0a  acuum), even if.
10edd 20 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f        ** SQLITE_
10ede 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55  DEFAULT_AUTOVACU
10edf 55 4d 20 69 73 20 74 72 75 65 2e 20 4f 6e 20 74  UM is true. On t
10ee0 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 69  he other hand, i
10ee1 66 0a 20 20 20 20 20 20 2a 2a 20 53 51 4c 49 54  f.      ** SQLIT
10ee2 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20  E_OMIT_MEMORYDB 
10ee3 68 61 73 20 62 65 65 6e 20 64 65 66 69 6e 65 64  has been defined
10ee4 2c 20 74 68 65 6e 20 22 3a 6d 65 6d 6f 72 79 3a  , then ":memory:
10ee5 22 20 69 73 20 6a 75 73 74 20 61 0a 20 20 20 20  " is just a.    
10ee6 20 20 2a 2a 20 72 65 67 75 6c 61 72 20 66 69 6c    ** regular fil
10ee7 65 2d 6e 61 6d 65 2e 20 49 6e 20 74 68 69 73 20  e-name. In this 
10ee8 63 61 73 65 20 74 68 65 20 61 75 74 6f 2d 76 61  case the auto-va
10ee9 63 75 75 6d 20 61 70 70 6c 69 65 73 20 61 73 20  cuum applies as 
10eea 70 65 72 20 6e 6f 72 6d 61 6c 2e 0a 20 20 20 20  per normal..    
10eeb 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 7a    */.      if( z
10eec 46 69 6c 65 6e 61 6d 65 20 26 26 20 21 69 73 4d  Filename && !isM
10eed 65 6d 64 62 20 29 7b 0a 20 20 20 20 20 20 20 20  emdb ){.        
10eee 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
10eef 3d 20 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  = (SQLITE_DEFAUL
10ef0 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 3f 20 31  T_AUTOVACUUM ? 1
10ef1 20 3a 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70   : 0);.        p
10ef2 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d  Bt->incrVacuum =
10ef3 20 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54   (SQLITE_DEFAULT
10ef4 5f 41 55 54 4f 56 41 43 55 55 4d 3d 3d 32 20 3f  _AUTOVACUUM==2 ?
10ef5 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 20 20 7d   1 : 0);.      }
10ef6 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 6e 52  .#endif.      nR
10ef7 65 73 65 72 76 65 20 3d 20 30 3b 0a 20 20 20 20  eserve = 0;.    
10ef8 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 52 65  }else{.      nRe
10ef9 73 65 72 76 65 20 3d 20 7a 44 62 48 65 61 64 65  serve = zDbHeade
10efa 72 5b 32 30 5d 3b 0a 20 20 20 20 20 20 70 42 74  r[20];.      pBt
10efb 2d 3e 6d 61 78 45 6d 62 65 64 46 72 61 63 20 3d  ->maxEmbedFrac =
10efc 20 7a 44 62 48 65 61 64 65 72 5b 32 31 5d 3b 0a   zDbHeader[21];.
10efd 20 20 20 20 20 20 70 42 74 2d 3e 6d 69 6e 45 6d        pBt->minEm
10efe 62 65 64 46 72 61 63 20 3d 20 7a 44 62 48 65 61  bedFrac = zDbHea
10eff 64 65 72 5b 32 32 5d 3b 0a 20 20 20 20 20 20 70  der[22];.      p
10f00 42 74 2d 3e 6d 69 6e 4c 65 61 66 46 72 61 63 20  Bt->minLeafFrac 
10f01 3d 20 7a 44 62 48 65 61 64 65 72 5b 32 33 5d 3b  = zDbHeader[23];
10f02 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61 67 65  .      pBt->page
10f03 53 69 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a 23  SizeFixed = 1;.#
10f04 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
10f05 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
10f06 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63      pBt->autoVac
10f07 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28  uum = (get4byte(
10f08 26 7a 44 62 48 65 61 64 65 72 5b 33 36 20 2b 20  &zDbHeader[36 + 
10f09 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 20  4*4])?1:0);.    
10f0a 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75    pBt->incrVacuu
10f0b 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 7a  m = (get4byte(&z
10f0c 44 62 48 65 61 64 65 72 5b 33 36 20 2b 20 37 2a  DbHeader[36 + 7*
10f0d 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 6e 64 69 66  4])?1:0);.#endif
10f0e 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e  .    }.    pBt->
10f0f 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74  usableSize = pBt
10f10 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 6e 52 65  ->pageSize - nRe
10f11 73 65 72 76 65 3b 0a 20 20 20 20 61 73 73 65 72  serve;.    asser
10f12 74 28 20 28 70 42 74 2d 3e 70 61 67 65 53 69 7a  t( (pBt->pageSiz
10f13 65 20 26 20 37 29 3d 3d 30 20 29 3b 20 20 2f 2a  e & 7)==0 );  /*
10f14 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e   8-byte alignmen
10f15 74 20 6f 66 20 70 61 67 65 53 69 7a 65 20 2a 2f  t of pageSize */
10f16 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65  .    sqlite3Page
10f17 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 42 74  rSetPagesize(pBt
10f18 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e  ->pPager, &pBt->
10f19 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 0a 23  pageSize);.   .#
10f1a 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
10f1b 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
10f1c 41 43 48 45 29 20 26 26 20 21 64 65 66 69 6e 65  ACHE) && !define
10f1d 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49  d(SQLITE_OMIT_DI
10f1e 53 4b 49 4f 29 0a 20 20 20 20 2f 2a 20 41 64 64  SKIO).    /* Add
10f1f 20 74 68 65 20 6e 65 77 20 42 74 53 68 61 72 65   the new BtShare
10f20 64 20 6f 62 6a 65 63 74 20 74 6f 20 74 68 65 20  d object to the 
10f21 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 68 61 72  linked list shar
10f22 61 62 6c 65 20 42 74 53 68 61 72 65 64 73 2e 0a  able BtShareds..
10f23 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
10f24 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20  ->sharable ){.  
10f25 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
10f26 78 20 2a 6d 75 74 65 78 53 68 61 72 65 64 3b 0a  x *mutexShared;.
10f27 20 20 20 20 20 20 70 42 74 2d 3e 6e 52 65 66 20        pBt->nRef 
10f28 3d 20 31 3b 0a 20 20 20 20 20 20 6d 75 74 65 78  = 1;.      mutex
10f29 53 68 61 72 65 64 20 3d 20 73 71 6c 69 74 65 33  Shared = sqlite3
10f2a 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c  _mutex_alloc(SQL
10f2b 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
10f2c 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20 20 20 20  _MASTER);.      
10f2d 69 66 28 20 53 51 4c 49 54 45 5f 54 48 52 45 41  if( SQLITE_THREA
10f2e 44 53 41 46 45 20 29 7b 0a 20 20 20 20 20 20 20  DSAFE ){.       
10f2f 20 70 42 74 2d 3e 6d 75 74 65 78 20 3d 20 73 71   pBt->mutex = sq
10f30 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
10f31 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  c(SQLITE_MUTEX_F
10f32 41 53 54 29 3b 0a 20 20 20 20 20 20 20 20 69 66  AST);.        if
10f33 28 20 70 42 74 2d 3e 6d 75 74 65 78 3d 3d 30 20  ( pBt->mutex==0 
10f34 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
10f35 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
10f36 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61            db->ma
10f37 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a  llocFailed = 0;.
10f38 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62            goto b
10f39 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20  tree_open_out;. 
10f3a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
10f3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
10f3c 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78  utex_enter(mutex
10f3d 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 70  Shared);.      p
10f3e 42 74 2d 3e 70 4e 65 78 74 20 3d 20 73 71 6c 69  Bt->pNext = sqli
10f3f 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69  te3SharedCacheLi
10f40 73 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  st;.      sqlite
10f41 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74  3SharedCacheList
10f42 20 3d 20 70 42 74 3b 0a 20 20 20 20 20 20 73 71   = pBt;.      sq
10f43 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
10f44 65 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a  e(mutexShared);.
10f45 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
10f46 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
10f47 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
10f48 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66  D_CACHE) && !def
10f49 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
10f4a 5f 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 20 49 66  _DISKIO).  /* If
10f4b 20 74 68 65 20 6e 65 77 20 42 74 72 65 65 20 75   the new Btree u
10f4c 73 65 73 20 61 20 73 68 61 72 61 62 6c 65 20 70  ses a sharable p
10f4d 42 74 53 68 61 72 65 64 2c 20 74 68 65 6e 20 6c  BtShared, then l
10f4e 69 6e 6b 20 74 68 65 20 6e 65 77 0a 20 20 2a 2a  ink the new.  **
10f4f 20 42 74 72 65 65 20 69 6e 74 6f 20 74 68 65 20   Btree into the 
10f50 6c 69 73 74 20 6f 66 20 61 6c 6c 20 73 68 61 72  list of all shar
10f51 61 62 6c 65 20 42 74 72 65 65 73 20 66 6f 72 20  able Btrees for 
10f52 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63 74  the same connect
10f53 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 65 20 6c 69  ion..  ** The li
10f54 73 74 20 69 73 20 6b 65 70 74 20 69 6e 20 61 73  st is kept in as
10f55 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79  cending order by
10f56 20 70 42 74 20 61 64 64 72 65 73 73 2e 0a 20 20   pBt address..  
10f57 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 72  */.  if( p->shar
10f58 61 62 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74 20  able ){.    int 
10f59 69 3b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 53  i;.    Btree *pS
10f5a 69 62 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  ib;.    for(i=0;
10f5b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
10f5c 7b 0a 20 20 20 20 20 20 69 66 28 20 28 70 53 69  {.      if( (pSi
10f5d 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70  b = db->aDb[i].p
10f5e 42 74 29 21 3d 30 20 26 26 20 70 53 69 62 2d 3e  Bt)!=0 && pSib->
10f5f 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20  sharable ){.    
10f60 20 20 20 20 77 68 69 6c 65 28 20 70 53 69 62 2d      while( pSib-
10f61 3e 70 50 72 65 76 20 29 7b 20 70 53 69 62 20 3d  >pPrev ){ pSib =
10f62 20 70 53 69 62 2d 3e 70 50 72 65 76 3b 20 7d 0a   pSib->pPrev; }.
10f63 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70          if( p->p
10f64 42 74 3c 70 53 69 62 2d 3e 70 42 74 20 29 7b 0a  Bt<pSib->pBt ){.
10f65 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65            p->pNe
10f66 78 74 20 3d 20 70 53 69 62 3b 0a 20 20 20 20 20  xt = pSib;.     
10f67 20 20 20 20 20 70 2d 3e 70 50 72 65 76 20 3d 20       p->pPrev = 
10f68 30 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53 69  0;.          pSi
10f69 62 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20  b->pPrev = p;.  
10f6a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
10f6b 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 53         while( pS
10f6c 69 62 2d 3e 70 4e 65 78 74 20 26 26 20 70 53 69  ib->pNext && pSi
10f6d 62 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3c 70 2d  b->pNext->pBt<p-
10f6e 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20  >pBt ){.        
10f6f 20 20 20 20 70 53 69 62 20 3d 20 70 53 69 62 2d      pSib = pSib-
10f70 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20  >pNext;.        
10f71 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 2d    }.          p-
10f72 3e 70 4e 65 78 74 20 3d 20 70 53 69 62 2d 3e 70  >pNext = pSib->p
10f73 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 20 20  Next;.          
10f74 70 2d 3e 70 50 72 65 76 20 3d 20 70 53 69 62 3b  p->pPrev = pSib;
10f75 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
10f76 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20  ->pNext ){.     
10f77 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 2d         p->pNext-
10f78 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 20 20  >pPrev = p;.    
10f79 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
10f7a 20 20 70 53 69 62 2d 3e 70 4e 65 78 74 20 3d 20    pSib->pNext = 
10f7b 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  p;.        }.   
10f7c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
10f7d 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65    }.    }.  }.#e
10f7e 6e 64 69 66 0a 20 20 2a 70 70 42 74 72 65 65 20  ndif.  *ppBtree 
10f7f 3d 20 70 3b 0a 0a 62 74 72 65 65 5f 6f 70 65 6e  = p;..btree_open
10f80 5f 6f 75 74 3a 0a 20 20 69 66 28 20 72 63 21 3d  _out:.  if( rc!=
10f81 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
10f82 20 69 66 28 20 70 42 74 20 26 26 20 70 42 74 2d   if( pBt && pBt-
10f83 3e 70 50 61 67 65 72 20 29 7b 0a 20 20 20 20 20  >pPager ){.     
10f84 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f   sqlite3PagerClo
10f85 73 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b  se(pBt->pPager);
10f86 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
10f87 65 33 5f 66 72 65 65 28 70 42 74 29 3b 0a 20 20  e3_free(pBt);.  
10f88 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
10f89 29 3b 0a 20 20 20 20 2a 70 70 42 74 72 65 65 20  );.    *ppBtree 
10f8a 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 0;.  }.  retur
10f8b 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  n rc;.}../*.** D
10f8c 65 63 72 65 6d 65 6e 74 20 74 68 65 20 42 74 53  ecrement the BtS
10f8d 68 61 72 65 64 2e 6e 52 65 66 20 63 6f 75 6e 74  hared.nRef count
10f8e 65 72 2e 20 20 57 68 65 6e 20 69 74 20 72 65 61  er.  When it rea
10f8f 63 68 65 73 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65  ches zero,.** re
10f90 6d 6f 76 65 20 74 68 65 20 42 74 53 68 61 72 65  move the BtShare
10f91 64 20 73 74 72 75 63 74 75 72 65 20 66 72 6f 6d  d structure from
10f92 20 74 68 65 20 73 68 61 72 69 6e 67 20 6c 69 73   the sharing lis
10f93 74 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 72  t.  Return.** tr
10f94 75 65 20 69 66 20 74 68 65 20 42 74 53 68 61 72  ue if the BtShar
10f95 65 64 2e 6e 52 65 66 20 63 6f 75 6e 74 65 72 20  ed.nRef counter 
10f96 72 65 61 63 68 65 73 20 7a 65 72 6f 20 61 6e 64  reaches zero and
10f97 20 72 65 74 75 72 6e 0a 2a 2a 20 66 61 6c 73 65   return.** false
10f98 20 69 66 20 69 74 20 69 73 20 73 74 69 6c 6c 20   if it is still 
10f99 70 6f 73 69 74 69 76 65 2e 0a 2a 2f 0a 73 74 61  positive..*/.sta
10f9a 74 69 63 20 69 6e 74 20 72 65 6d 6f 76 65 46 72  tic int removeFr
10f9b 6f 6d 53 68 61 72 69 6e 67 4c 69 73 74 28 42 74  omSharingList(Bt
10f9c 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 23 69  Shared *pBt){.#i
10f9d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
10f9e 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20  T_SHARED_CACHE. 
10f9f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
10fa0 70 4d 61 73 74 65 72 3b 0a 20 20 42 74 53 68 61  pMaster;.  BtSha
10fa1 72 65 64 20 2a 70 4c 69 73 74 3b 0a 20 20 69 6e  red *pList;.  in
10fa2 74 20 72 65 6d 6f 76 65 64 20 3d 20 30 3b 0a 0a  t removed = 0;..
10fa3 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
10fa4 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28  3_mutex_notheld(
10fa5 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
10fa6 20 70 4d 61 73 74 65 72 20 3d 20 73 71 6c 69 74   pMaster = sqlit
10fa7 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53  e3_mutex_alloc(S
10fa8 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
10fa9 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 73 71  IC_MASTER);.  sq
10faa 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
10fab 72 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 70 42  r(pMaster);.  pB
10fac 74 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28  t->nRef--;.  if(
10fad 20 70 42 74 2d 3e 6e 52 65 66 3c 3d 30 20 29 7b   pBt->nRef<=0 ){
10fae 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
10faf 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 3d  SharedCacheList=
10fb0 3d 70 42 74 20 29 7b 0a 20 20 20 20 20 20 73 71  =pBt ){.      sq
10fb1 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65  lite3SharedCache
10fb2 4c 69 73 74 20 3d 20 70 42 74 2d 3e 70 4e 65 78  List = pBt->pNex
10fb3 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  t;.    }else{.  
10fb4 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69      pList = sqli
10fb5 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69  te3SharedCacheLi
10fb6 73 74 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28  st;.      while(
10fb7 20 70 4c 69 73 74 20 26 26 20 70 4c 69 73 74 2d   pList && pList-
10fb8 3e 70 4e 65 78 74 21 3d 70 42 74 20 29 7b 0a 20  >pNext!=pBt ){. 
10fb9 20 20 20 20 20 20 20 70 4c 69 73 74 3d 70 4c 69         pList=pLi
10fba 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20  st->pNext;.     
10fbb 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4c 69   }.      if( pLi
10fbc 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c  st ){.        pL
10fbd 69 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74  ist->pNext = pBt
10fbe 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d  ->pNext;.      }
10fbf 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 53  .    }.    if( S
10fc0 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
10fc1 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
10fc2 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 70 42 74  3_mutex_free(pBt
10fc3 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a  ->mutex);.    }.
10fc4 20 20 20 20 72 65 6d 6f 76 65 64 20 3d 20 31 3b      removed = 1;
10fc5 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
10fc6 75 74 65 78 5f 6c 65 61 76 65 28 70 4d 61 73 74  utex_leave(pMast
10fc7 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65  er);.  return re
10fc8 6d 6f 76 65 64 3b 0a 23 65 6c 73 65 0a 20 20 72  moved;.#else.  r
10fc9 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a  eturn 1;.#endif.
10fca 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61  }../*.** Close a
10fcb 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20  n open database 
10fcc 61 6e 64 20 69 6e 76 61 6c 69 64 61 74 65 20 61  and invalidate a
10fcd 6c 6c 20 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a 53  ll cursors..*/.S
10fce 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
10fcf 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c  t sqlite3BtreeCl
10fd0 6f 73 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  ose(Btree *p){. 
10fd1 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
10fd2 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 43 75 72   p->pBt;.  BtCur
10fd3 73 6f 72 20 2a 70 43 75 72 3b 0a 0a 20 20 2f 2a  sor *pCur;..  /*
10fd4 20 43 6c 6f 73 65 20 61 6c 6c 20 63 75 72 73 6f   Close all curso
10fd5 72 73 20 6f 70 65 6e 65 64 20 76 69 61 20 74 68  rs opened via th
10fd6 69 73 20 68 61 6e 64 6c 65 2e 20 20 2a 2f 0a 20  is handle.  */. 
10fd7 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
10fd8 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64  _mutex_held(p->d
10fd9 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73  b->mutex) );.  s
10fda 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
10fdb 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 62 20 3d  (p);.  pBt->db =
10fdc 20 70 2d 3e 64 62 3b 0a 20 20 70 43 75 72 20 3d   p->db;.  pCur =
10fdd 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 0a 20   pBt->pCursor;. 
10fde 20 77 68 69 6c 65 28 20 70 43 75 72 20 29 7b 0a   while( pCur ){.
10fdf 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 54      BtCursor *pT
10fe0 6d 70 20 3d 20 70 43 75 72 3b 0a 20 20 20 20 70  mp = pCur;.    p
10fe1 43 75 72 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78  Cur = pCur->pNex
10fe2 74 3b 0a 20 20 20 20 69 66 28 20 70 54 6d 70 2d  t;.    if( pTmp-
10fe3 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20  >pBtree==p ){.  
10fe4 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
10fe5 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 54 6d 70  CloseCursor(pTmp
10fe6 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
10fe7 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6e 79 20  /* Rollback any 
10fe8 61 63 74 69 76 65 20 74 72 61 6e 73 61 63 74 69  active transacti
10fe9 6f 6e 20 61 6e 64 20 66 72 65 65 20 74 68 65 20  on and free the 
10fea 68 61 6e 64 6c 65 20 73 74 72 75 63 74 75 72 65  handle structure
10feb 2e 0a 20 20 2a 2a 20 54 68 65 20 63 61 6c 6c 20  ..  ** The call 
10fec 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 52  to sqlite3BtreeR
10fed 6f 6c 6c 62 61 63 6b 28 29 20 64 72 6f 70 73 20  ollback() drops 
10fee 61 6e 79 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 20  any table-locks 
10fef 68 65 6c 64 20 62 79 0a 20 20 2a 2a 20 74 68 69  held by.  ** thi
10ff0 73 20 68 61 6e 64 6c 65 2e 0a 20 20 2a 2f 0a 20  s handle..  */. 
10ff1 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c   sqlite3BtreeRol
10ff2 6c 62 61 63 6b 28 70 29 3b 0a 20 20 73 71 6c 69  lback(p);.  sqli
10ff3 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
10ff4 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  ;..  /* If there
10ff5 20 61 72 65 20 73 74 69 6c 6c 20 6f 74 68 65 72   are still other
10ff6 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66   outstanding ref
10ff7 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 73  erences to the s
10ff8 68 61 72 65 64 2d 62 74 72 65 65 0a 20 20 2a 2a  hared-btree.  **
10ff9 20 73 74 72 75 63 74 75 72 65 2c 20 72 65 74 75   structure, retu
10ffa 72 6e 20 6e 6f 77 2e 20 54 68 65 20 72 65 6d 61  rn now. The rema
10ffb 69 6e 64 65 72 20 6f 66 20 74 68 69 73 20 70 72  inder of this pr
10ffc 6f 63 65 64 75 72 65 20 63 6c 65 61 6e 73 20 0a  ocedure cleans .
10ffd 20 20 2a 2a 20 75 70 20 74 68 65 20 73 68 61 72    ** up the shar
10ffe 65 64 2d 62 74 72 65 65 2e 0a 20 20 2a 2f 0a 20  ed-btree..  */. 
10fff 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74   assert( p->want
11000 54 6f 4c 6f 63 6b 3d 3d 30 20 26 26 20 70 2d 3e  ToLock==0 && p->
11001 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a 20 20 69  locked==0 );.  i
11002 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20  f( !p->sharable 
11003 7c 7c 20 72 65 6d 6f 76 65 46 72 6f 6d 53 68 61  || removeFromSha
11004 72 69 6e 67 4c 69 73 74 28 70 42 74 29 20 29 7b  ringList(pBt) ){
11005 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 42 74 20  .    /* The pBt 
11006 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6f 6e 20  is no longer on 
11007 74 68 65 20 73 68 61 72 69 6e 67 20 6c 69 73 74  the sharing list
11008 2c 20 73 6f 20 77 65 20 63 61 6e 20 61 63 63 65  , so we can acce
11009 73 73 0a 20 20 20 20 2a 2a 20 69 74 20 77 69 74  ss.    ** it wit
1100a 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 68  hout having to h
1100b 6f 6c 64 20 74 68 65 20 6d 75 74 65 78 2e 0a 20  old the mutex.. 
1100c 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 6c 65     **.    ** Cle
1100d 61 6e 20 6f 75 74 20 61 6e 64 20 64 65 6c 65 74  an out and delet
1100e 65 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6f  e the BtShared o
1100f 62 6a 65 63 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  bject..    */.  
11010 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e    assert( !pBt->
11011 70 43 75 72 73 6f 72 20 29 3b 0a 20 20 20 20 73  pCursor );.    s
11012 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65  qlite3PagerClose
11013 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20  (pBt->pPager);. 
11014 20 20 20 69 66 28 20 70 42 74 2d 3e 78 46 72 65     if( pBt->xFre
11015 65 53 63 68 65 6d 61 20 26 26 20 70 42 74 2d 3e  eSchema && pBt->
11016 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20  pSchema ){.     
11017 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68 65 6d   pBt->xFreeSchem
11018 61 28 70 42 74 2d 3e 70 53 63 68 65 6d 61 29 3b  a(pBt->pSchema);
11019 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
1101a 65 33 5f 66 72 65 65 28 70 42 74 2d 3e 70 53 63  e3_free(pBt->pSc
1101b 68 65 6d 61 29 3b 0a 20 20 20 20 73 71 6c 69 74  hema);.    sqlit
1101c 65 33 5f 66 72 65 65 28 70 42 74 2d 3e 70 54 6d  e3_free(pBt->pTm
1101d 70 53 70 61 63 65 29 3b 0a 20 20 20 20 73 71 6c  pSpace);.    sql
1101e 69 74 65 33 5f 66 72 65 65 28 70 42 74 29 3b 0a  ite3_free(pBt);.
1101f 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
11020 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
11021 43 41 43 48 45 0a 20 20 61 73 73 65 72 74 28 20  CACHE.  assert( 
11022 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30  p->wantToLock==0
11023 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
11024 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a 20 20  >locked==0 );.  
11025 69 66 28 20 70 2d 3e 70 50 72 65 76 20 29 20 70  if( p->pPrev ) p
11026 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d  ->pPrev->pNext =
11027 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 69 66 28   p->pNext;.  if(
11028 20 70 2d 3e 70 4e 65 78 74 20 29 20 70 2d 3e 70   p->pNext ) p->p
11029 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 2d  Next->pPrev = p-
1102a 3e 70 50 72 65 76 3b 0a 23 65 6e 64 69 66 0a 0a  >pPrev;.#endif..
1102b 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
1102c 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
1102d 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
1102e 43 68 61 6e 67 65 20 74 68 65 20 6c 69 6d 69 74  Change the limit
1102f 20 6f 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   on the number o
11030 66 20 70 61 67 65 73 20 61 6c 6c 6f 77 65 64 20  f pages allowed 
11031 69 6e 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2a  in the cache..**
11032 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20  .** The maximum 
11033 6e 75 6d 62 65 72 20 6f 66 20 63 61 63 68 65 20  number of cache 
11034 70 61 67 65 73 20 69 73 20 73 65 74 20 74 6f 20  pages is set to 
11035 74 68 65 20 61 62 73 6f 6c 75 74 65 0a 2a 2a 20  the absolute.** 
11036 76 61 6c 75 65 20 6f 66 20 6d 78 50 61 67 65 2e  value of mxPage.
11037 20 20 49 66 20 6d 78 50 61 67 65 20 69 73 20 6e    If mxPage is n
11038 65 67 61 74 69 76 65 2c 20 74 68 65 20 70 61 67  egative, the pag
11039 65 72 20 77 69 6c 6c 0a 2a 2a 20 6f 70 65 72 61  er will.** opera
1103a 74 65 20 61 73 79 6e 63 68 72 6f 6e 6f 75 73 6c  te asynchronousl
1103b 79 20 2d 20 69 74 20 77 69 6c 6c 20 6e 6f 74 20  y - it will not 
1103c 73 74 6f 70 20 74 6f 20 64 6f 20 66 73 79 6e 63  stop to do fsync
1103d 28 29 73 0a 2a 2a 20 74 6f 20 69 6e 73 75 72 65  ()s.** to insure
1103e 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e   data is written
1103f 20 74 6f 20 74 68 65 20 64 69 73 6b 20 73 75 72   to the disk sur
11040 66 61 63 65 20 62 65 66 6f 72 65 0a 2a 2a 20 63  face before.** c
11041 6f 6e 74 69 6e 75 69 6e 67 2e 20 20 54 72 61 6e  ontinuing.  Tran
11042 73 61 63 74 69 6f 6e 73 20 73 74 69 6c 6c 20 77  sactions still w
11043 6f 72 6b 20 69 66 20 73 79 6e 63 68 72 6f 6e 6f  ork if synchrono
11044 75 73 20 69 73 20 6f 66 66 2c 0a 2a 2a 20 61 6e  us is off,.** an
11045 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  d the database c
11046 61 6e 6e 6f 74 20 62 65 20 63 6f 72 72 75 70 74  annot be corrupt
11047 65 64 20 69 66 20 74 68 69 73 20 70 72 6f 67 72  ed if this progr
11048 61 6d 0a 2a 2a 20 63 72 61 73 68 65 73 2e 20 20  am.** crashes.  
11049 42 75 74 20 69 66 20 74 68 65 20 6f 70 65 72 61  But if the opera
1104a 74 69 6e 67 20 73 79 73 74 65 6d 20 63 72 61 73  ting system cras
1104b 68 65 73 20 6f 72 20 74 68 65 72 65 20 69 73 0a  hes or there is.
1104c 2a 2a 20 61 6e 20 61 62 72 75 70 74 20 70 6f 77  ** an abrupt pow
1104d 65 72 20 66 61 69 6c 75 72 65 20 77 68 65 6e 20  er failure when 
1104e 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f  synchronous is o
1104f 66 66 2c 20 74 68 65 20 64 61 74 61 62 61 73 65  ff, the database
11050 0a 2a 2a 20 63 6f 75 6c 64 20 62 65 20 6c 65 66  .** could be lef
11051 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73  t in an inconsis
11052 74 65 6e 74 20 61 6e 64 20 75 6e 72 65 63 6f 76  tent and unrecov
11053 65 72 61 62 6c 65 20 73 74 61 74 65 2e 0a 2a 2a  erable state..**
11054 20 53 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20   Synchronous is 
11055 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 20 73 6f  on by default so
11056 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70   database corrup
11057 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20 6e  tion is not.** n
11058 6f 72 6d 61 6c 6c 79 20 61 20 77 6f 72 72 79 2e  ormally a worry.
11059 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1105a 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
1105b 72 65 65 53 65 74 43 61 63 68 65 53 69 7a 65 28  reeSetCacheSize(
1105c 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6d 78  Btree *p, int mx
1105d 50 61 67 65 29 7b 0a 20 20 42 74 53 68 61 72 65  Page){.  BtShare
1105e 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
1105f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
11060 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d  e3_mutex_held(p-
11061 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
11062 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
11063 65 72 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  er(p);.  sqlite3
11064 50 61 67 65 72 53 65 74 43 61 63 68 65 73 69 7a  PagerSetCachesiz
11065 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6d  e(pBt->pPager, m
11066 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 74 65  xPage);.  sqlite
11067 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
11068 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
11069 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61  OK;.}../*.** Cha
1106a 6e 67 65 20 74 68 65 20 77 61 79 20 64 61 74 61  nge the way data
1106b 20 69 73 20 73 79 6e 63 65 64 20 74 6f 20 64 69   is synced to di
1106c 73 6b 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 69  sk in order to i
1106d 6e 63 72 65 61 73 65 20 6f 72 20 64 65 63 72 65  ncrease or decre
1106e 61 73 65 0a 2a 2a 20 68 6f 77 20 77 65 6c 6c 20  ase.** how well 
1106f 74 68 65 20 64 61 74 61 62 61 73 65 20 72 65 73  the database res
11070 69 73 74 73 20 64 61 6d 61 67 65 20 64 75 65 20  ists damage due 
11071 74 6f 20 4f 53 20 63 72 61 73 68 65 73 20 61 6e  to OS crashes an
11072 64 20 70 6f 77 65 72 0a 2a 2a 20 66 61 69 6c 75  d power.** failu
11073 72 65 73 2e 20 20 4c 65 76 65 6c 20 31 20 69 73  res.  Level 1 is
11074 20 74 68 65 20 73 61 6d 65 20 61 73 20 61 73 79   the same as asy
11075 6e 63 68 72 6f 6e 6f 75 73 20 28 6e 6f 20 73 79  nchronous (no sy
11076 6e 63 73 28 29 20 6f 63 63 75 72 20 61 6e 64 0a  ncs() occur and.
11077 2a 2a 20 74 68 65 72 65 20 69 73 20 61 20 68 69  ** there is a hi
11078 67 68 20 70 72 6f 62 61 62 69 6c 69 74 79 20 6f  gh probability o
11079 66 20 64 61 6d 61 67 65 29 20 20 4c 65 76 65 6c  f damage)  Level
1107a 20 32 20 69 73 20 74 68 65 20 64 65 66 61 75 6c   2 is the defaul
1107b 74 2e 20 20 54 68 65 72 65 0a 2a 2a 20 69 73 20  t.  There.** is 
1107c 61 20 76 65 72 79 20 6c 6f 77 20 62 75 74 20 6e  a very low but n
1107d 6f 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62 69 6c  on-zero probabil
1107e 69 74 79 20 6f 66 20 64 61 6d 61 67 65 2e 20 20  ity of damage.  
1107f 4c 65 76 65 6c 20 33 20 72 65 64 75 63 65 73 20  Level 3 reduces 
11080 74 68 65 0a 2a 2a 20 70 72 6f 62 61 62 69 6c 69  the.** probabili
11081 74 79 20 6f 66 20 64 61 6d 61 67 65 20 74 6f 20  ty of damage to 
11082 6e 65 61 72 20 7a 65 72 6f 20 62 75 74 20 77 69  near zero but wi
11083 74 68 20 61 20 77 72 69 74 65 20 70 65 72 66 6f  th a write perfo
11084 72 6d 61 6e 63 65 20 72 65 64 75 63 74 69 6f 6e  rmance reduction
11085 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
11086 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50  ITE_OMIT_PAGER_P
11087 52 41 47 4d 41 53 0a 53 51 4c 49 54 45 5f 50 52  RAGMAS.SQLITE_PR
11088 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
11089 33 42 74 72 65 65 53 65 74 53 61 66 65 74 79 4c  3BtreeSetSafetyL
1108a 65 76 65 6c 28 42 74 72 65 65 20 2a 70 2c 20 69  evel(Btree *p, i
1108b 6e 74 20 6c 65 76 65 6c 2c 20 69 6e 74 20 66 75  nt level, int fu
1108c 6c 6c 53 79 6e 63 29 7b 0a 20 20 42 74 53 68 61  llSync){.  BtSha
1108d 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
1108e 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  t;.  assert( sql
1108f 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
11090 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
11091 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
11092 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c 69 74  nter(p);.  sqlit
11093 65 33 50 61 67 65 72 53 65 74 53 61 66 65 74 79  e3PagerSetSafety
11094 4c 65 76 65 6c 28 70 42 74 2d 3e 70 50 61 67 65  Level(pBt->pPage
11095 72 2c 20 6c 65 76 65 6c 2c 20 66 75 6c 6c 53 79  r, level, fullSy
11096 6e 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  nc);.  sqlite3Bt
11097 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
11098 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
11099 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
1109a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
1109b 74 68 65 20 67 69 76 65 6e 20 62 74 72 65 65 20  the given btree 
1109c 69 73 20 73 65 74 20 74 6f 20 73 61 66 65 74 79  is set to safety
1109d 20 6c 65 76 65 6c 20 31 2e 20 20 49 6e 20 6f 74   level 1.  In ot
1109e 68 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 72 65  her.** words, re
1109f 74 75 72 6e 20 54 52 55 45 20 69 66 20 6e 6f 20  turn TRUE if no 
110a0 73 79 6e 63 28 29 20 6f 63 63 75 72 73 20 6f 6e  sync() occurs on
110a1 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65 73 2e   the disk files.
110a2 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
110a3 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
110a4 72 65 65 53 79 6e 63 44 69 73 61 62 6c 65 64 28  reeSyncDisabled(
110a5 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53  Btree *p){.  BtS
110a6 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
110a7 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  pBt;.  int rc;. 
110a8 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
110a9 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64  _mutex_held(p->d
110aa 62 2d 3e 6d 75 74 65 78 29 20 29 3b 20 20 0a 20  b->mutex) );  . 
110ab 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
110ac 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28  er(p);.  assert(
110ad 20 70 42 74 20 26 26 20 70 42 74 2d 3e 70 50 61   pBt && pBt->pPa
110ae 67 65 72 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  ger );.  rc = sq
110af 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79 6e 63  lite3PagerNosync
110b0 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20  (pBt->pPager);. 
110b1 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
110b2 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ve(p);.  return 
110b3 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69  rc;.}..#if !defi
110b4 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
110b5 50 41 47 45 52 5f 50 52 41 47 4d 41 53 29 20 7c  PAGER_PRAGMAS) |
110b6 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  | !defined(SQLIT
110b7 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 0a 2f  E_OMIT_VACUUM)./
110b8 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
110b9 64 65 66 61 75 6c 74 20 70 61 67 65 73 20 73 69  default pages si
110ba 7a 65 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65  ze and the numbe
110bb 72 20 6f 66 20 72 65 73 65 72 76 65 64 20 62 79  r of reserved by
110bc 74 65 73 20 70 65 72 20 70 61 67 65 2e 0a 2a 2a  tes per page..**
110bd 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 73 69 7a  .** The page siz
110be 65 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65  e must be a powe
110bf 72 20 6f 66 20 32 20 62 65 74 77 65 65 6e 20 35  r of 2 between 5
110c0 31 32 20 61 6e 64 20 36 35 35 33 36 2e 20 20 49  12 and 65536.  I
110c1 66 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 73 69  f the page.** si
110c2 7a 65 20 73 75 70 70 6c 69 65 64 20 64 6f 65 73  ze supplied does
110c3 20 6e 6f 74 20 6d 65 65 74 20 74 68 69 73 20 63   not meet this c
110c4 6f 6e 73 74 72 61 69 6e 74 20 74 68 65 6e 20 74  onstraint then t
110c5 68 65 20 70 61 67 65 20 73 69 7a 65 20 69 73 20  he page size is 
110c6 6e 6f 74 0a 2a 2a 20 63 68 61 6e 67 65 64 2e 0a  not.** changed..
110c7 2a 2a 0a 2a 2a 20 50 61 67 65 20 73 69 7a 65 73  **.** Page sizes
110c8 20 61 72 65 20 63 6f 6e 73 74 72 61 69 6e 65 64   are constrained
110c9 20 74 6f 20 62 65 20 61 20 70 6f 77 65 72 20 6f   to be a power o
110ca 66 20 74 77 6f 20 73 6f 20 74 68 61 74 20 74 68  f two so that th
110cb 65 20 72 65 67 69 6f 6e 0a 2a 2a 20 6f 66 20 74  e region.** of t
110cc 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
110cd 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e   used for lockin
110ce 67 20 28 62 65 67 69 6e 6e 69 6e 67 20 61 74 20  g (beginning at 
110cf 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 0a 2a 2a  PENDING_BYTE,.**
110d0 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20   the first byte 
110d1 70 61 73 74 20 74 68 65 20 31 47 42 20 62 6f 75  past the 1GB bou
110d2 6e 64 61 72 79 2c 20 30 78 34 30 30 30 30 30 30  ndary, 0x4000000
110d3 30 29 20 6e 65 65 64 73 20 74 6f 20 6f 63 63 75  0) needs to occu
110d4 72 0a 2a 2a 20 61 74 20 74 68 65 20 62 65 67 69  r.** at the begi
110d5 6e 6e 69 6e 67 20 6f 66 20 61 20 70 61 67 65 2e  nning of a page.
110d6 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 72 61 6d 65  .**.** If parame
110d7 74 65 72 20 6e 52 65 73 65 72 76 65 20 69 73 20  ter nReserve is 
110d8 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20  less than zero, 
110d9 74 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  then the number 
110da 6f 66 20 72 65 73 65 72 76 65 64 0a 2a 2a 20 62  of reserved.** b
110db 79 74 65 73 20 70 65 72 20 70 61 67 65 20 69 73  ytes per page is
110dc 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e   left unchanged.
110dd 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
110de 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
110df 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 42  reeSetPageSize(B
110e0 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 70 61 67  tree *p, int pag
110e1 65 53 69 7a 65 2c 20 69 6e 74 20 6e 52 65 73 65  eSize, int nRese
110e2 72 76 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  rve){.  int rc =
110e3 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74   SQLITE_OK;.  Bt
110e4 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
110e5 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 42  >pBt;.  sqlite3B
110e6 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
110e7 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a  if( pBt->pageSiz
110e8 65 46 69 78 65 64 20 29 7b 0a 20 20 20 20 73 71  eFixed ){.    sq
110e9 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
110ea 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  p);.    return S
110eb 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a  QLITE_READONLY;.
110ec 20 20 7d 0a 20 20 69 66 28 20 6e 52 65 73 65 72    }.  if( nReser
110ed 76 65 3c 30 20 29 7b 0a 20 20 20 20 6e 52 65 73  ve<0 ){.    nRes
110ee 65 72 76 65 20 3d 20 70 42 74 2d 3e 70 61 67 65  erve = pBt->page
110ef 53 69 7a 65 20 2d 20 70 42 74 2d 3e 75 73 61 62  Size - pBt->usab
110f0 6c 65 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 69 66  leSize;.  }.  if
110f1 28 20 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 20  ( pageSize>=512 
110f2 26 26 20 70 61 67 65 53 69 7a 65 3c 3d 53 51 4c  && pageSize<=SQL
110f3 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a  ITE_MAX_PAGE_SIZ
110f4 45 20 26 26 0a 20 20 20 20 20 20 20 20 28 28 70  E &&.        ((p
110f5 61 67 65 53 69 7a 65 2d 31 29 26 70 61 67 65 53  ageSize-1)&pageS
110f6 69 7a 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 61  ize)==0 ){.    a
110f7 73 73 65 72 74 28 20 28 70 61 67 65 53 69 7a 65  ssert( (pageSize
110f8 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20   & 7)==0 );.    
110f9 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70 50  assert( !pBt->pP
110fa 61 67 65 31 20 26 26 20 21 70 42 74 2d 3e 70 43  age1 && !pBt->pC
110fb 75 72 73 6f 72 20 29 3b 0a 20 20 20 20 70 42 74  ursor );.    pBt
110fc 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 70 61 67  ->pageSize = pag
110fd 65 53 69 7a 65 3b 0a 20 20 20 20 73 71 6c 69 74  eSize;.    sqlit
110fe 65 33 5f 66 72 65 65 28 70 42 74 2d 3e 70 54 6d  e3_free(pBt->pTm
110ff 70 53 70 61 63 65 29 3b 0a 20 20 20 20 70 42 74  pSpace);.    pBt
11100 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 30 3b  ->pTmpSpace = 0;
11101 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
11102 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a  3PagerSetPagesiz
11103 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26  e(pBt->pPager, &
11104 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a  pBt->pageSize);.
11105 20 20 7d 0a 20 20 70 42 74 2d 3e 75 73 61 62 6c    }.  pBt->usabl
11106 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61 67  eSize = pBt->pag
11107 65 53 69 7a 65 20 2d 20 6e 52 65 73 65 72 76 65  eSize - nReserve
11108 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
11109 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
1110a 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1110b 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65  Return the curre
1110c 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 70 61 67  ntly defined pag
1110d 65 20 73 69 7a 65 0a 2a 2f 0a 53 51 4c 49 54 45  e size.*/.SQLITE
1110e 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1110f 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
11110 53 69 7a 65 28 42 74 72 65 65 20 2a 70 29 7b 0a  Size(Btree *p){.
11111 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d    return p->pBt-
11112 3e 70 61 67 65 53 69 7a 65 3b 0a 7d 0a 53 51 4c  >pageSize;.}.SQL
11113 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
11114 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 52  sqlite3BtreeGetR
11115 65 73 65 72 76 65 28 42 74 72 65 65 20 2a 70 29  eserve(Btree *p)
11116 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c  {.  int n;.  sql
11117 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
11118 29 3b 0a 20 20 6e 20 3d 20 70 2d 3e 70 42 74 2d  );.  n = p->pBt-
11119 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 2d 3e 70  >pageSize - p->p
1111a 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a  Bt->usableSize;.
1111b 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
1111c 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
1111d 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74   n;.}../*.** Set
1111e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67   the maximum pag
1111f 65 20 63 6f 75 6e 74 20 66 6f 72 20 61 20 64 61  e count for a da
11120 74 61 62 61 73 65 20 69 66 20 6d 78 50 61 67 65  tabase if mxPage
11121 20 69 73 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2a   is positive..**
11122 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 72 65 20   No changes are 
11123 6d 61 64 65 20 69 66 20 6d 78 50 61 67 65 20 69  made if mxPage i
11124 73 20 30 20 6f 72 20 6e 65 67 61 74 69 76 65 2e  s 0 or negative.
11125 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 20 6f  .** Regardless o
11126 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d  f the value of m
11127 78 50 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68  xPage, return th
11128 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63  e maximum page c
11129 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ount..*/.SQLITE_
1112a 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1112b 74 65 33 42 74 72 65 65 4d 61 78 50 61 67 65 43  te3BtreeMaxPageC
1112c 6f 75 6e 74 28 42 74 72 65 65 20 2a 70 2c 20 69  ount(Btree *p, i
1112d 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 69 6e  nt mxPage){.  in
1112e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  t n;.  sqlite3Bt
1112f 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 6e  reeEnter(p);.  n
11130 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d   = sqlite3PagerM
11131 61 78 50 61 67 65 43 6f 75 6e 74 28 70 2d 3e 70  axPageCount(p->p
11132 42 74 2d 3e 70 50 61 67 65 72 2c 20 6d 78 50 61  Bt->pPager, mxPa
11133 67 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  ge);.  sqlite3Bt
11134 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
11135 65 74 75 72 6e 20 6e 3b 0a 7d 0a 23 65 6e 64 69  eturn n;.}.#endi
11136 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51  f /* !defined(SQ
11137 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f  LITE_OMIT_PAGER_
11138 50 52 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 66  PRAGMAS) || !def
11139 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
1113a 5f 56 41 43 55 55 4d 29 20 2a 2f 0a 0a 2f 2a 0a  _VACUUM) */../*.
1113b 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 27 61  ** Change the 'a
1113c 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 72 6f 70  uto-vacuum' prop
1113d 65 72 74 79 20 6f 66 20 74 68 65 20 64 61 74 61  erty of the data
1113e 62 61 73 65 2e 20 49 66 20 74 68 65 20 27 61 75  base. If the 'au
1113f 74 6f 56 61 63 75 75 6d 27 0a 2a 2a 20 70 61 72  toVacuum'.** par
11140 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65  ameter is non-ze
11141 72 6f 2c 20 74 68 65 6e 20 61 75 74 6f 2d 76 61  ro, then auto-va
11142 63 75 75 6d 20 6d 6f 64 65 20 69 73 20 65 6e 61  cuum mode is ena
11143 62 6c 65 64 2e 20 49 66 20 7a 65 72 6f 2c 20 69  bled. If zero, i
11144 74 0a 2a 2a 20 69 73 20 64 69 73 61 62 6c 65 64  t.** is disabled
11145 2e 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61  . The default va
11146 6c 75 65 20 66 6f 72 20 74 68 65 20 61 75 74 6f  lue for the auto
11147 2d 76 61 63 75 75 6d 20 70 72 6f 70 65 72 74 79  -vacuum property
11148 20 69 73 20 0a 2a 2a 20 64 65 74 65 72 6d 69 6e   is .** determin
11149 65 64 20 62 79 20 74 68 65 20 53 51 4c 49 54 45  ed by the SQLITE
1114a 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43  _DEFAULT_AUTOVAC
1114b 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 53 51  UUM macro..*/.SQ
1114c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1114d 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
1114e 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65  AutoVacuum(Btree
1114f 20 2a 70 2c 20 69 6e 74 20 61 75 74 6f 56 61 63   *p, int autoVac
11150 75 75 6d 29 7b 0a 23 69 66 64 65 66 20 53 51 4c  uum){.#ifdef SQL
11151 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
11152 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  UUM.  return SQL
11153 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 23 65  ITE_READONLY;.#e
11154 6c 73 65 0a 20 20 42 74 53 68 61 72 65 64 20 2a  lse.  BtShared *
11155 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
11156 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
11157 4f 4b 3b 0a 20 20 69 6e 74 20 61 76 20 3d 20 28  OK;.  int av = (
11158 61 75 74 6f 56 61 63 75 75 6d 3f 31 3a 30 29 3b  autoVacuum?1:0);
11159 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ..  sqlite3Btree
1115a 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20  Enter(p);.  if( 
1115b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78  pBt->pageSizeFix
1115c 65 64 20 26 26 20 61 76 21 3d 70 42 74 2d 3e 61  ed && av!=pBt->a
1115d 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20  utoVacuum ){.   
1115e 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41   rc = SQLITE_REA
1115f 44 4f 4e 4c 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a  DONLY;.  }else{.
11160 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63      pBt->autoVac
11161 75 75 6d 20 3d 20 61 76 3b 0a 20 20 7d 0a 20 20  uum = av;.  }.  
11162 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
11163 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  e(p);.  return r
11164 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a  c;.#endif.}../*.
11165 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 61  ** Return the va
11166 6c 75 65 20 6f 66 20 74 68 65 20 27 61 75 74 6f  lue of the 'auto
11167 2d 76 61 63 75 75 6d 27 20 70 72 6f 70 65 72 74  -vacuum' propert
11168 79 2e 20 49 66 20 61 75 74 6f 2d 76 61 63 75 75  y. If auto-vacuu
11169 6d 20 69 73 20 0a 2a 2a 20 65 6e 61 62 6c 65 64  m is .** enabled
1116a 20 31 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20   1 is returned. 
1116b 4f 74 68 65 72 77 69 73 65 20 30 2e 0a 2a 2f 0a  Otherwise 0..*/.
1116c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1116d 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  nt sqlite3BtreeG
1116e 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72  etAutoVacuum(Btr
1116f 65 65 20 2a 70 29 7b 0a 23 69 66 64 65 66 20 53  ee *p){.#ifdef S
11170 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
11171 41 43 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 42  ACUUM.  return B
11172 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f  TREE_AUTOVACUUM_
11173 4e 4f 4e 45 3b 0a 23 65 6c 73 65 0a 20 20 69 6e  NONE;.#else.  in
11174 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42  t rc;.  sqlite3B
11175 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
11176 72 63 20 3d 20 28 0a 20 20 20 20 28 21 70 2d 3e  rc = (.    (!p->
11177 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29  pBt->autoVacuum)
11178 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55  ?BTREE_AUTOVACUU
11179 4d 5f 4e 4f 4e 45 3a 0a 20 20 20 20 28 21 70 2d  M_NONE:.    (!p-
1117a 3e 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d  >pBt->incrVacuum
1117b 29 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55  )?BTREE_AUTOVACU
1117c 55 4d 5f 46 55 4c 4c 3a 0a 20 20 20 20 42 54 52  UM_FULL:.    BTR
1117d 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e  EE_AUTOVACUUM_IN
1117e 43 52 0a 20 20 29 3b 0a 20 20 73 71 6c 69 74 65  CR.  );.  sqlite
1117f 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
11180 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e    return rc;.#en
11181 64 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65  dif.}.../*.** Ge
11182 74 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f  t a reference to
11183 20 70 50 61 67 65 31 20 6f 66 20 74 68 65 20 64   pPage1 of the d
11184 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54  atabase file.  T
11185 68 69 73 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f  his will.** also
11186 20 61 63 71 75 69 72 65 20 61 20 72 65 61 64 6c   acquire a readl
11187 6f 63 6b 20 6f 6e 20 74 68 61 74 20 66 69 6c 65  ock on that file
11188 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f  ..**.** SQLITE_O
11189 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e  K is returned on
1118a 20 73 75 63 63 65 73 73 2e 20 20 49 66 20 74 68   success.  If th
1118b 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 61 0a  e file is not a.
1118c 2a 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64  ** well-formed d
1118d 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68  atabase file, th
1118e 65 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  en SQLITE_CORRUP
1118f 54 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  T is returned..*
11190 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73  * SQLITE_BUSY is
11191 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65   returned if the
11192 20 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63   database is loc
11193 6b 65 64 2e 20 20 53 51 4c 49 54 45 5f 4e 4f 4d  ked.  SQLITE_NOM
11194 45 4d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65  EM.** is returne
11195 64 20 69 66 20 77 65 20 72 75 6e 20 6f 75 74 20  d if we run out 
11196 6f 66 20 6d 65 6d 6f 72 79 2e 20 0a 2a 2f 0a 73  of memory. .*/.s
11197 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 42 74  tatic int lockBt
11198 72 65 65 28 42 74 53 68 61 72 65 64 20 2a 70 42  ree(BtShared *pB
11199 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  t){.  int rc;.  
1119a 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b  MemPage *pPage1;
1119b 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20  .  int nPage;.. 
1119c 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1119d 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
1119e 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28  >mutex) );.  if(
1119f 20 70 42 74 2d 3e 70 50 61 67 65 31 20 29 20 72   pBt->pPage1 ) r
111a0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
111a1 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
111a2 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c  treeGetPage(pBt,
111a3 20 31 2c 20 26 70 50 61 67 65 31 2c 20 30 29 3b   1, &pPage1, 0);
111a4 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
111a5 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
111a6 3b 0a 0a 20 20 2f 2a 20 44 6f 20 73 6f 6d 65 20  ;..  /* Do some 
111a7 63 68 65 63 6b 69 6e 67 20 74 6f 20 68 65 6c 70  checking to help
111a8 20 69 6e 73 75 72 65 20 74 68 65 20 66 69 6c 65   insure the file
111a9 20 77 65 20 6f 70 65 6e 65 64 20 72 65 61 6c 6c   we opened reall
111aa 79 20 69 73 0a 20 20 2a 2a 20 61 20 76 61 6c 69  y is.  ** a vali
111ab 64 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  d database file.
111ac 20 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 53 51   .  */.  rc = SQ
111ad 4c 49 54 45 5f 4e 4f 54 41 44 42 3b 0a 20 20 6e  LITE_NOTADB;.  n
111ae 50 61 67 65 20 3d 20 73 71 6c 69 74 65 33 50 61  Page = sqlite3Pa
111af 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74  gerPagecount(pBt
111b0 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28  ->pPager);.  if(
111b1 20 6e 50 61 67 65 3c 30 20 29 7b 0a 20 20 20 20   nPage<0 ){.    
111b2 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  rc = SQLITE_IOER
111b3 52 3b 0a 20 20 20 20 67 6f 74 6f 20 70 61 67 65  R;.    goto page
111b4 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20  1_init_failed;. 
111b5 20 7d 65 6c 73 65 20 69 66 28 20 6e 50 61 67 65   }else if( nPage
111b6 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 70 61  >0 ){.    int pa
111b7 67 65 53 69 7a 65 3b 0a 20 20 20 20 69 6e 74 20  geSize;.    int 
111b8 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20  usableSize;.    
111b9 75 38 20 2a 70 61 67 65 31 20 3d 20 70 50 61 67  u8 *page1 = pPag
111ba 65 31 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 69  e1->aData;.    i
111bb 66 28 20 6d 65 6d 63 6d 70 28 70 61 67 65 31 2c  f( memcmp(page1,
111bc 20 7a 4d 61 67 69 63 48 65 61 64 65 72 2c 20 31   zMagicHeader, 1
111bd 36 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 67  6)!=0 ){.      g
111be 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66  oto page1_init_f
111bf 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20  ailed;.    }.   
111c0 20 69 66 28 20 70 61 67 65 31 5b 31 38 5d 3e 31   if( page1[18]>1
111c1 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 72   ){.      pBt->r
111c2 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20 20  eadOnly = 1;.   
111c3 20 7d 0a 20 20 20 20 69 66 28 20 70 61 67 65 31   }.    if( page1
111c4 5b 31 39 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20  [19]>1 ){.      
111c5 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f  goto page1_init_
111c6 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20  failed;.    }.  
111c7 20 20 70 61 67 65 53 69 7a 65 20 3d 20 67 65 74    pageSize = get
111c8 32 62 79 74 65 28 26 70 61 67 65 31 5b 31 36 5d  2byte(&page1[16]
111c9 29 3b 0a 20 20 20 20 69 66 28 20 28 28 70 61 67  );.    if( ((pag
111ca 65 53 69 7a 65 2d 31 29 26 70 61 67 65 53 69 7a  eSize-1)&pageSiz
111cb 65 29 21 3d 30 20 7c 7c 20 70 61 67 65 53 69 7a  e)!=0 || pageSiz
111cc 65 3c 35 31 32 20 7c 7c 0a 20 20 20 20 20 20 20  e<512 ||.       
111cd 20 28 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47   (SQLITE_MAX_PAG
111ce 45 5f 53 49 5a 45 3c 33 32 37 36 38 20 26 26 20  E_SIZE<32768 && 
111cf 70 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f  pageSize>SQLITE_
111d0 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 29 0a 20  MAX_PAGE_SIZE). 
111d1 20 20 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f     ){.      goto
111d2 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c   page1_init_fail
111d3 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  ed;.    }.    as
111d4 73 65 72 74 28 20 28 70 61 67 65 53 69 7a 65 20  sert( (pageSize 
111d5 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20 75  & 7)==0 );.    u
111d6 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 61 67 65  sableSize = page
111d7 53 69 7a 65 20 2d 20 70 61 67 65 31 5b 32 30 5d  Size - page1[20]
111d8 3b 0a 20 20 20 20 69 66 28 20 70 61 67 65 53 69  ;.    if( pageSi
111d9 7a 65 21 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a  ze!=pBt->pageSiz
111da 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 66  e ){.      /* Af
111db 74 65 72 20 72 65 61 64 69 6e 67 20 74 68 65 20  ter reading the 
111dc 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68  first page of th
111dd 65 20 64 61 74 61 62 61 73 65 20 61 73 73 75 6d  e database assum
111de 69 6e 67 20 61 20 70 61 67 65 20 73 69 7a 65 0a  ing a page size.
111df 20 20 20 20 20 20 2a 2a 20 6f 66 20 42 74 53 68        ** of BtSh
111e0 61 72 65 64 2e 70 61 67 65 53 69 7a 65 2c 20 77  ared.pageSize, w
111e1 65 20 68 61 76 65 20 64 69 73 63 6f 76 65 72 65  e have discovere
111e2 64 20 74 68 61 74 20 74 68 65 20 70 61 67 65 2d  d that the page-
111e3 73 69 7a 65 20 69 73 0a 20 20 20 20 20 20 2a 2a  size is.      **
111e4 20 61 63 74 75 61 6c 6c 79 20 70 61 67 65 53 69   actually pageSi
111e5 7a 65 2e 20 55 6e 6c 6f 63 6b 20 74 68 65 20 64  ze. Unlock the d
111e6 61 74 61 62 61 73 65 2c 20 6c 65 61 76 65 20 70  atabase, leave p
111e7 42 74 2d 3e 70 50 61 67 65 31 20 61 74 0a 20 20  Bt->pPage1 at.  
111e8 20 20 20 20 2a 2a 20 7a 65 72 6f 20 61 6e 64 20      ** zero and 
111e9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
111ea 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77 69 6c  . The caller wil
111eb 6c 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63  l call this func
111ec 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 61 67  tion.      ** ag
111ed 61 69 6e 20 77 69 74 68 20 74 68 65 20 63 6f 72  ain with the cor
111ee 72 65 63 74 20 70 61 67 65 2d 73 69 7a 65 2e 0a  rect page-size..
111ef 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72        */.      r
111f0 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65  eleasePage(pPage
111f1 31 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 75  1);.      pBt->u
111f2 73 61 62 6c 65 53 69 7a 65 20 3d 20 75 73 61 62  sableSize = usab
111f3 6c 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 70 42  leSize;.      pB
111f4 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 70 61  t->pageSize = pa
111f5 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 73 71  geSize;.      sq
111f6 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74 2d 3e  lite3_free(pBt->
111f7 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 20 20  pTmpSpace);.    
111f8 20 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65    pBt->pTmpSpace
111f9 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69   = 0;.      sqli
111fa 74 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73  te3PagerSetPages
111fb 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  ize(pBt->pPager,
111fc 20 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29   &pBt->pageSize)
111fd 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
111fe 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
111ff 20 20 20 20 69 66 28 20 75 73 61 62 6c 65 53 69      if( usableSi
11200 7a 65 3c 35 30 30 20 29 7b 0a 20 20 20 20 20 20  ze<500 ){.      
11201 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f  goto page1_init_
11202 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20  failed;.    }.  
11203 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20    pBt->pageSize 
11204 3d 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20  = pageSize;.    
11205 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
11206 3d 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20  = usableSize;.  
11207 20 20 70 42 74 2d 3e 6d 61 78 45 6d 62 65 64 46    pBt->maxEmbedF
11208 72 61 63 20 3d 20 70 61 67 65 31 5b 32 31 5d 3b  rac = page1[21];
11209 0a 20 20 20 20 70 42 74 2d 3e 6d 69 6e 45 6d 62  .    pBt->minEmb
1120a 65 64 46 72 61 63 20 3d 20 70 61 67 65 31 5b 32  edFrac = page1[2
1120b 32 5d 3b 0a 20 20 20 20 70 42 74 2d 3e 6d 69 6e  2];.    pBt->min
1120c 4c 65 61 66 46 72 61 63 20 3d 20 70 61 67 65 31  LeafFrac = page1
1120d 5b 32 33 5d 3b 0a 23 69 66 6e 64 65 66 20 53 51  [23];.#ifndef SQ
1120e 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
1120f 43 55 55 4d 0a 20 20 20 20 70 42 74 2d 3e 61 75  CUUM.    pBt->au
11210 74 6f 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34  toVacuum = (get4
11211 62 79 74 65 28 26 70 61 67 65 31 5b 33 36 20 2b  byte(&page1[36 +
11212 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20   4*4])?1:0);.   
11213 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d   pBt->incrVacuum
11214 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 70 61   = (get4byte(&pa
11215 67 65 31 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31  ge1[36 + 7*4])?1
11216 3a 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a  :0);.#endif.  }.
11217 0a 20 20 2f 2a 20 6d 61 78 4c 6f 63 61 6c 20 69  .  /* maxLocal i
11218 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d  s the maximum am
11219 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20  ount of payload 
1121a 74 6f 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c 79  to store locally
1121b 20 66 6f 72 0a 20 20 2a 2a 20 61 20 63 65 6c 6c   for.  ** a cell
1121c 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 69 74 20  .  Make sure it 
1121d 69 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 20  is small enough 
1121e 73 6f 20 74 68 61 74 20 61 74 20 6c 65 61 73 74  so that at least
1121f 20 6d 69 6e 46 61 6e 6f 75 74 0a 20 20 2a 2a 20   minFanout.  ** 
11220 63 65 6c 6c 73 20 63 61 6e 20 77 69 6c 6c 20 66  cells can will f
11221 69 74 20 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20  it on one page. 
11222 20 57 65 20 61 73 73 75 6d 65 20 61 20 31 30 2d   We assume a 10-
11223 62 79 74 65 20 70 61 67 65 20 68 65 61 64 65 72  byte page header
11224 2e 0a 20 20 2a 2a 20 42 65 73 69 64 65 73 20 74  ..  ** Besides t
11225 68 65 20 70 61 79 6c 6f 61 64 2c 20 74 68 65 20  he payload, the 
11226 63 65 6c 6c 20 6d 75 73 74 20 73 74 6f 72 65 3a  cell must store:
11227 0a 20 20 2a 2a 20 20 20 20 20 32 2d 62 79 74 65  .  **     2-byte
11228 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
11229 63 65 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 34 2d  cell.  **     4-
1122a 62 79 74 65 20 63 68 69 6c 64 20 70 6f 69 6e 74  byte child point
1122b 65 72 0a 20 20 2a 2a 20 20 20 20 20 39 2d 62 79  er.  **     9-by
1122c 74 65 20 6e 4b 65 79 20 76 61 6c 75 65 0a 20 20  te nKey value.  
1122d 2a 2a 20 20 20 20 20 34 2d 62 79 74 65 20 6e 44  **     4-byte nD
1122e 61 74 61 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20  ata value.  **  
1122f 20 20 20 34 2d 62 79 74 65 20 6f 76 65 72 66 6c     4-byte overfl
11230 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72 0a  ow page pointer.
11231 20 20 2a 2a 20 53 6f 20 61 20 63 65 6c 6c 20 63    ** So a cell c
11232 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 32 2d 62  onsists of a 2-b
11233 79 74 65 20 70 6f 69 6e 65 72 2c 20 61 20 68 65  yte poiner, a he
11234 61 64 65 72 20 77 68 69 63 68 20 69 73 20 61 73  ader which is as
11235 20 6d 75 63 68 20 61 73 0a 20 20 2a 2a 20 31 37   much as.  ** 17
11236 20 62 79 74 65 73 20 6c 6f 6e 67 2c 20 30 20 74   bytes long, 0 t
11237 6f 20 4e 20 62 79 74 65 73 20 6f 66 20 70 61 79  o N bytes of pay
11238 6c 6f 61 64 2c 20 61 6e 64 20 61 6e 20 6f 70 74  load, and an opt
11239 69 6f 6e 61 6c 20 34 20 62 79 74 65 20 6f 76 65  ional 4 byte ove
1123a 72 66 6c 6f 77 0a 20 20 2a 2a 20 70 61 67 65 20  rflow.  ** page 
1123b 70 6f 69 6e 74 65 72 2e 0a 20 20 2a 2f 0a 20 20  pointer..  */.  
1123c 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20  pBt->maxLocal = 
1123d 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  (pBt->usableSize
1123e 2d 31 32 29 2a 70 42 74 2d 3e 6d 61 78 45 6d 62  -12)*pBt->maxEmb
1123f 65 64 46 72 61 63 2f 32 35 35 20 2d 20 32 33 3b  edFrac/255 - 23;
11240 0a 20 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 6c  .  pBt->minLocal
11241 20 3d 20 28 70 42 74 2d 3e 75 73 61 62 6c 65 53   = (pBt->usableS
11242 69 7a 65 2d 31 32 29 2a 70 42 74 2d 3e 6d 69 6e  ize-12)*pBt->min
11243 45 6d 62 65 64 46 72 61 63 2f 32 35 35 20 2d 20  EmbedFrac/255 - 
11244 32 33 3b 0a 20 20 70 42 74 2d 3e 6d 61 78 4c 65  23;.  pBt->maxLe
11245 61 66 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65  af = pBt->usable
11246 53 69 7a 65 20 2d 20 33 35 3b 0a 20 20 70 42 74  Size - 35;.  pBt
11247 2d 3e 6d 69 6e 4c 65 61 66 20 3d 20 28 70 42 74  ->minLeaf = (pBt
11248 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29  ->usableSize-12)
11249 2a 70 42 74 2d 3e 6d 69 6e 4c 65 61 66 46 72 61  *pBt->minLeafFra
1124a 63 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 69 66  c/255 - 23;.  if
1124b 28 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 6c 3e  ( pBt->minLocal>
1124c 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 20 7c 7c  pBt->maxLocal ||
1124d 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 3c 30   pBt->maxLocal<0
1124e 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 70 61 67   ){.    goto pag
1124f 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a  e1_init_failed;.
11250 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 42    }.  assert( pB
11251 74 2d 3e 6d 61 78 4c 65 61 66 20 2b 20 32 33 20  t->maxLeaf + 23 
11252 3c 3d 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28  <= MX_CELL_SIZE(
11253 70 42 74 29 20 29 3b 0a 20 20 70 42 74 2d 3e 70  pBt) );.  pBt->p
11254 50 61 67 65 31 20 3d 20 70 50 61 67 65 31 3b 0a  Page1 = pPage1;.
11255 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
11256 4f 4b 3b 0a 0a 70 61 67 65 31 5f 69 6e 69 74 5f  OK;..page1_init_
11257 66 61 69 6c 65 64 3a 0a 20 20 72 65 6c 65 61 73  failed:.  releas
11258 65 50 61 67 65 28 70 50 61 67 65 31 29 3b 0a 20  ePage(pPage1);. 
11259 20 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30   pBt->pPage1 = 0
1125a 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
1125b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
1125c 74 69 6e 65 20 77 6f 72 6b 73 20 6c 69 6b 65 20  tine works like 
1125d 6c 6f 63 6b 42 74 72 65 65 28 29 20 65 78 63 65  lockBtree() exce
1125e 70 74 20 74 68 61 74 20 69 74 20 61 6c 73 6f 20  pt that it also 
1125f 69 6e 76 6f 6b 65 73 20 74 68 65 0a 2a 2a 20 62  invokes the.** b
11260 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 69 66 20  usy callback if 
11261 74 68 65 72 65 20 69 73 20 6c 6f 63 6b 20 63 6f  there is lock co
11262 6e 74 65 6e 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  ntention..*/.sta
11263 74 69 63 20 69 6e 74 20 6c 6f 63 6b 42 74 72 65  tic int lockBtre
11264 65 57 69 74 68 52 65 74 72 79 28 42 74 72 65 65  eWithRetry(Btree
11265 20 2a 70 52 65 66 29 7b 0a 20 20 69 6e 74 20 72   *pRef){.  int r
11266 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  c = SQLITE_OK;..
11267 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
11268 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78  3BtreeHoldsMutex
11269 28 70 52 65 66 29 20 29 3b 0a 20 20 69 66 28 20  (pRef) );.  if( 
1126a 70 52 65 66 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54  pRef->inTrans==T
1126b 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20  RANS_NONE ){.   
1126c 20 75 38 20 69 6e 54 72 61 6e 73 61 63 74 69 6f   u8 inTransactio
1126d 6e 20 3d 20 70 52 65 66 2d 3e 70 42 74 2d 3e 69  n = pRef->pBt->i
1126e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b 0a 20 20  nTransaction;.  
1126f 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79    btreeIntegrity
11270 28 70 52 65 66 29 3b 0a 20 20 20 20 72 63 20 3d  (pRef);.    rc =
11271 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67   sqlite3BtreeBeg
11272 69 6e 54 72 61 6e 73 28 70 52 65 66 2c 20 30 29  inTrans(pRef, 0)
11273 3b 0a 20 20 20 20 70 52 65 66 2d 3e 70 42 74 2d  ;.    pRef->pBt-
11274 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d  >inTransaction =
11275 20 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b 0a   inTransaction;.
11276 20 20 20 20 70 52 65 66 2d 3e 69 6e 54 72 61 6e      pRef->inTran
11277 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a  s = TRANS_NONE;.
11278 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
11279 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70  TE_OK ){.      p
1127a 52 65 66 2d 3e 70 42 74 2d 3e 6e 54 72 61 6e 73  Ref->pBt->nTrans
1127b 61 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20 20 7d 0a  action--;.    }.
1127c 20 20 20 20 62 74 72 65 65 49 6e 74 65 67 72 69      btreeIntegri
1127d 74 79 28 70 52 65 66 29 3b 0a 20 20 7d 0a 20 20  ty(pRef);.  }.  
1127e 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 20 20 20  return rc;.}.   
1127f 20 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74      ../*.** If t
11280 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73  here are no outs
11281 74 61 6e 64 69 6e 67 20 63 75 72 73 6f 72 73 20  tanding cursors 
11282 61 6e 64 20 77 65 20 61 72 65 20 6e 6f 74 20 69  and we are not i
11283 6e 20 74 68 65 20 6d 69 64 64 6c 65 0a 2a 2a 20  n the middle.** 
11284 6f 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  of a transaction
11285 20 62 75 74 20 74 68 65 72 65 20 69 73 20 61 20   but there is a 
11286 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  read lock on the
11287 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 0a   database, then.
11288 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ** this routine 
11289 75 6e 72 65 66 73 20 74 68 65 20 66 69 72 73 74  unrefs the first
1128a 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74   page of the dat
1128b 61 62 61 73 65 20 66 69 6c 65 20 77 68 69 63 68  abase file which
1128c 20 0a 2a 2a 20 68 61 73 20 74 68 65 20 65 66 66   .** has the eff
1128d 65 63 74 20 6f 66 20 72 65 6c 65 61 73 69 6e 67  ect of releasing
1128e 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a   the read lock..
1128f 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61  **.** If there a
11290 72 65 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69  re any outstandi
11291 6e 67 20 63 75 72 73 6f 72 73 2c 20 74 68 69 73  ng cursors, this
11292 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
11293 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  -op..**.** If th
11294 65 72 65 20 69 73 20 61 20 74 72 61 6e 73 61 63  ere is a transac
11295 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73  tion in progress
11296 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  , this routine i
11297 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74  s a no-op..*/.st
11298 61 74 69 63 20 76 6f 69 64 20 75 6e 6c 6f 63 6b  atic void unlock
11299 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 42 74  BtreeIfUnused(Bt
1129a 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20  Shared *pBt){.  
1129b 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1129c 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
1129d 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
1129e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69  pBt->inTransacti
1129f 6f 6e 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 26  on==TRANS_NONE &
112a0 26 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3d 3d  & pBt->pCursor==
112a1 30 20 26 26 20 70 42 74 2d 3e 70 50 61 67 65 31  0 && pBt->pPage1
112a2 21 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73  !=0 ){.    if( s
112a3 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f  qlite3PagerRefco
112a4 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29  unt(pBt->pPager)
112a5 3e 3d 31 20 29 7b 0a 20 20 20 20 20 20 61 73 73  >=1 ){.      ass
112a6 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31  ert( pBt->pPage1
112a7 2d 3e 61 44 61 74 61 20 29 3b 0a 23 69 66 20 30  ->aData );.#if 0
112a8 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e  .      if( pBt->
112a9 70 50 61 67 65 31 2d 3e 61 44 61 74 61 3d 3d 30  pPage1->aData==0
112aa 20 29 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d 50   ){.        MemP
112ab 61 67 65 20 2a 70 50 61 67 65 20 3d 20 70 42 74  age *pPage = pBt
112ac 2d 3e 70 50 61 67 65 31 3b 0a 20 20 20 20 20 20  ->pPage1;.      
112ad 20 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 3d    pPage->aData =
112ae 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
112af 44 61 74 61 28 70 50 61 67 65 2d 3e 70 44 62 50  Data(pPage->pDbP
112b0 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 70 50  age);.        pP
112b1 61 67 65 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a  age->pBt = pBt;.
112b2 20 20 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70          pPage->p
112b3 67 6e 6f 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  gno = 1;.      }
112b4 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 72 65  .#endif.      re
112b5 6c 65 61 73 65 50 61 67 65 28 70 42 74 2d 3e 70  leasePage(pBt->p
112b6 50 61 67 65 31 29 3b 0a 20 20 20 20 7d 0a 20 20  Page1);.    }.  
112b7 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20    pBt->pPage1 = 
112b8 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 53 74  0;.    pBt->inSt
112b9 6d 74 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f  mt = 0;.  }.}../
112ba 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  *.** Create a ne
112bb 77 20 64 61 74 61 62 61 73 65 20 62 79 20 69 6e  w database by in
112bc 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 66  itializing the f
112bd 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65  irst page of the
112be 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  .** file..*/.sta
112bf 74 69 63 20 69 6e 74 20 6e 65 77 44 61 74 61 62  tic int newDatab
112c0 61 73 65 28 42 74 53 68 61 72 65 64 20 2a 70 42  ase(BtShared *pB
112c1 74 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  t){.  MemPage *p
112c2 50 31 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  P1;.  unsigned c
112c3 68 61 72 20 2a 64 61 74 61 3b 0a 20 20 69 6e 74  har *data;.  int
112c4 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   rc;..  assert( 
112c5 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
112c6 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  ld(pBt->mutex) )
112c7 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 50  ;.  if( sqlite3P
112c8 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42  agerPagecount(pB
112c9 74 2d 3e 70 50 61 67 65 72 29 3e 30 20 29 20 72  t->pPager)>0 ) r
112ca 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
112cb 0a 20 20 70 50 31 20 3d 20 70 42 74 2d 3e 70 50  .  pP1 = pBt->pP
112cc 61 67 65 31 3b 0a 20 20 61 73 73 65 72 74 28 20  age1;.  assert( 
112cd 70 50 31 21 3d 30 20 29 3b 0a 20 20 64 61 74 61  pP1!=0 );.  data
112ce 20 3d 20 70 50 31 2d 3e 61 44 61 74 61 3b 0a 20   = pP1->aData;. 
112cf 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
112d0 65 72 57 72 69 74 65 28 70 50 31 2d 3e 70 44 62  erWrite(pP1->pDb
112d1 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20  Page);.  if( rc 
112d2 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 6d  ) return rc;.  m
112d3 65 6d 63 70 79 28 64 61 74 61 2c 20 7a 4d 61 67  emcpy(data, zMag
112d4 69 63 48 65 61 64 65 72 2c 20 73 69 7a 65 6f 66  icHeader, sizeof
112d5 28 7a 4d 61 67 69 63 48 65 61 64 65 72 29 29 3b  (zMagicHeader));
112d6 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f  .  assert( sizeo
112d7 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72 29 3d  f(zMagicHeader)=
112d8 3d 31 36 20 29 3b 0a 20 20 70 75 74 32 62 79 74  =16 );.  put2byt
112d9 65 28 26 64 61 74 61 5b 31 36 5d 2c 20 70 42 74  e(&data[16], pBt
112da 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 64  ->pageSize);.  d
112db 61 74 61 5b 31 38 5d 20 3d 20 31 3b 0a 20 20 64  ata[18] = 1;.  d
112dc 61 74 61 5b 31 39 5d 20 3d 20 31 3b 0a 20 20 64  ata[19] = 1;.  d
112dd 61 74 61 5b 32 30 5d 20 3d 20 70 42 74 2d 3e 70  ata[20] = pBt->p
112de 61 67 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e 75  ageSize - pBt->u
112df 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 64 61 74  sableSize;.  dat
112e0 61 5b 32 31 5d 20 3d 20 70 42 74 2d 3e 6d 61 78  a[21] = pBt->max
112e1 45 6d 62 65 64 46 72 61 63 3b 0a 20 20 64 61 74  EmbedFrac;.  dat
112e2 61 5b 32 32 5d 20 3d 20 70 42 74 2d 3e 6d 69 6e  a[22] = pBt->min
112e3 45 6d 62 65 64 46 72 61 63 3b 0a 20 20 64 61 74  EmbedFrac;.  dat
112e4 61 5b 32 33 5d 20 3d 20 70 42 74 2d 3e 6d 69 6e  a[23] = pBt->min
112e5 4c 65 61 66 46 72 61 63 3b 0a 20 20 6d 65 6d 73  LeafFrac;.  mems
112e6 65 74 28 26 64 61 74 61 5b 32 34 5d 2c 20 30 2c  et(&data[24], 0,
112e7 20 31 30 30 2d 32 34 29 3b 0a 20 20 7a 65 72 6f   100-24);.  zero
112e8 50 61 67 65 28 70 50 31 2c 20 50 54 46 5f 49 4e  Page(pP1, PTF_IN
112e9 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 7c 50 54  TKEY|PTF_LEAF|PT
112ea 46 5f 4c 45 41 46 44 41 54 41 20 29 3b 0a 20 20  F_LEAFDATA );.  
112eb 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78  pBt->pageSizeFix
112ec 65 64 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20  ed = 1;.#ifndef 
112ed 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
112ee 56 41 43 55 55 4d 0a 20 20 61 73 73 65 72 74 28  VACUUM.  assert(
112ef 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
112f0 3d 3d 31 20 7c 7c 20 70 42 74 2d 3e 61 75 74 6f  ==1 || pBt->auto
112f1 56 61 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 20 61  Vacuum==0 );.  a
112f2 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 63 72  ssert( pBt->incr
112f3 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70 42 74  Vacuum==1 || pBt
112f4 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d 30 20  ->incrVacuum==0 
112f5 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 64  );.  put4byte(&d
112f6 61 74 61 5b 33 36 20 2b 20 34 2a 34 5d 2c 20 70  ata[36 + 4*4], p
112f7 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3b  Bt->autoVacuum);
112f8 0a 20 20 70 75 74 34 62 79 74 65 28 26 64 61 74  .  put4byte(&dat
112f9 61 5b 33 36 20 2b 20 37 2a 34 5d 2c 20 70 42 74  a[36 + 7*4], pBt
112fa 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29 3b 0a 23  ->incrVacuum);.#
112fb 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 53  endif.  return S
112fc 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
112fd 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 73 74  ** Attempt to st
112fe 61 72 74 20 61 20 6e 65 77 20 74 72 61 6e 73 61  art a new transa
112ff 63 74 69 6f 6e 2e 20 41 20 77 72 69 74 65 2d 74  ction. A write-t
11300 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73  ransaction.** is
11301 20 73 74 61 72 74 65 64 20 69 66 20 74 68 65 20   started if the 
11302 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
11303 69 73 20 6e 6f 6e 7a 65 72 6f 2c 20 6f 74 68 65  is nonzero, othe
11304 72 77 69 73 65 20 61 20 72 65 61 64 2d 0a 2a 2a  rwise a read-.**
11305 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49   transaction.  I
11306 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  f the second arg
11307 75 6d 65 6e 74 20 69 73 20 32 20 6f 72 20 6d 6f  ument is 2 or mo
11308 72 65 20 61 6e 64 20 65 78 63 6c 75 73 69 76 65  re and exclusive
11309 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  .** transaction 
1130a 69 73 20 73 74 61 72 74 65 64 2c 20 6d 65 61 6e  is started, mean
1130b 69 6e 67 20 74 68 61 74 20 6e 6f 20 6f 74 68 65  ing that no othe
1130c 72 20 70 72 6f 63 65 73 73 20 69 73 20 61 6c 6c  r process is all
1130d 6f 77 65 64 0a 2a 2a 20 74 6f 20 61 63 63 65 73  owed.** to acces
1130e 73 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  s the database. 
1130f 20 41 20 70 72 65 65 78 69 73 74 69 6e 67 20 74   A preexisting t
11310 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 6e  ransaction may n
11311 6f 74 20 62 65 0a 2a 2a 20 75 70 67 72 61 64 65  ot be.** upgrade
11312 64 20 74 6f 20 65 78 63 6c 75 73 69 76 65 20 62  d to exclusive b
11313 79 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72  y calling this r
11314 6f 75 74 69 6e 65 20 61 20 73 65 63 6f 6e 64 20  outine a second 
11315 74 69 6d 65 20 2d 20 74 68 65 0a 2a 2a 20 65 78  time - the.** ex
11316 63 6c 75 73 69 76 69 74 79 20 66 6c 61 67 20 6f  clusivity flag o
11317 6e 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 61 20  nly works for a 
11318 6e 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  new transaction.
11319 0a 2a 2a 0a 2a 2a 20 41 20 77 72 69 74 65 2d 74  .**.** A write-t
1131a 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20  ransaction must 
1131b 62 65 20 73 74 61 72 74 65 64 20 62 65 66 6f 72  be started befor
1131c 65 20 61 74 74 65 6d 70 74 69 6e 67 20 61 6e 79  e attempting any
1131d 20 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f 20   .** changes to 
1131e 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 4e  the database.  N
1131f 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  one of the follo
11320 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 0a 2a  wing routines .*
11321 2a 20 77 69 6c 6c 20 77 6f 72 6b 20 75 6e 6c 65  * will work unle
11322 73 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  ss a transaction
11323 20 69 73 20 73 74 61 72 74 65 64 20 66 69 72 73   is started firs
11324 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 73 71  t:.**.**      sq
11325 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65  lite3BtreeCreate
11326 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20  Table().**      
11327 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61  sqlite3BtreeCrea
11328 74 65 49 6e 64 65 78 28 29 0a 2a 2a 20 20 20 20  teIndex().**    
11329 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c    sqlite3BtreeCl
1132a 65 61 72 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20  earTable().**   
1132b 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 44     sqlite3BtreeD
1132c 72 6f 70 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20  ropTable().**   
1132d 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 49     sqlite3BtreeI
1132e 6e 73 65 72 74 28 29 0a 2a 2a 20 20 20 20 20 20  nsert().**      
1132f 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65  sqlite3BtreeDele
11330 74 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c  te().**      sql
11331 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d  ite3BtreeUpdateM
11332 65 74 61 28 29 0a 2a 2a 0a 2a 2a 20 49 66 20 61  eta().**.** If a
11333 6e 20 69 6e 69 74 69 61 6c 20 61 74 74 65 6d 70  n initial attemp
11334 74 20 74 6f 20 61 63 71 75 69 72 65 20 74 68 65  t to acquire the
11335 20 6c 6f 63 6b 20 66 61 69 6c 73 20 62 65 63 61   lock fails beca
11336 75 73 65 20 6f 66 20 6c 6f 63 6b 20 63 6f 6e 74  use of lock cont
11337 65 6e 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 74 68  ention.** and th
11338 65 20 64 61 74 61 62 61 73 65 20 77 61 73 20 70  e database was p
11339 72 65 76 69 6f 75 73 6c 79 20 75 6e 6c 6f 63 6b  reviously unlock
1133a 65 64 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65 20  ed, then invoke 
1133b 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72  the busy handler
1133c 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 69 73 20  .** if there is 
1133d 6f 6e 65 2e 20 20 42 75 74 20 69 66 20 74 68 65  one.  But if the
1133e 72 65 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c  re was previousl
1133f 79 20 61 20 72 65 61 64 2d 6c 6f 63 6b 2c 20 64  y a read-lock, d
11340 6f 20 6e 6f 74 0a 2a 2a 20 69 6e 76 6f 6b 65 20  o not.** invoke 
11341 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72  the busy handler
11342 20 2d 20 6a 75 73 74 20 72 65 74 75 72 6e 20 53   - just return S
11343 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 53 51 4c  QLITE_BUSY.  SQL
11344 49 54 45 5f 42 55 53 59 20 69 73 20 0a 2a 2a 20  ITE_BUSY is .** 
11345 72 65 74 75 72 6e 65 64 20 77 68 65 6e 20 74 68  returned when th
11346 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61  ere is already a
11347 20 72 65 61 64 2d 6c 6f 63 6b 20 69 6e 20 6f 72   read-lock in or
11348 64 65 72 20 74 6f 20 61 76 6f 69 64 20 61 20 64  der to avoid a d
11349 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53  eadlock..**.** S
1134a 75 70 70 6f 73 65 20 74 68 65 72 65 20 61 72 65  uppose there are
1134b 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20 41   two processes A
1134c 20 61 6e 64 20 42 2e 20 20 41 20 68 61 73 20 61   and B.  A has a
1134d 20 72 65 61 64 20 6c 6f 63 6b 20 61 6e 64 20 42   read lock and B
1134e 20 68 61 73 0a 2a 2a 20 61 20 72 65 73 65 72 76   has.** a reserv
1134f 65 64 20 6c 6f 63 6b 2e 20 20 42 20 74 72 69 65  ed lock.  B trie
11350 73 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20  s to promote to 
11351 65 78 63 6c 75 73 69 76 65 20 62 75 74 20 69 73  exclusive but is
11352 20 62 6c 6f 63 6b 65 64 20 62 65 63 61 75 73 65   blocked because
11353 0a 2a 2a 20 6f 66 20 41 27 73 20 72 65 61 64 20  .** of A's read 
11354 6c 6f 63 6b 2e 20 20 41 20 74 72 69 65 73 20 74  lock.  A tries t
11355 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 72 65 73  o promote to res
11356 65 72 76 65 64 20 62 75 74 20 69 73 20 62 6c 6f  erved but is blo
11357 63 6b 65 64 20 62 79 20 42 2e 0a 2a 2a 20 4f 6e  cked by B..** On
11358 65 20 6f 72 20 74 68 65 20 6f 74 68 65 72 20 6f  e or the other o
11359 66 20 74 68 65 20 74 77 6f 20 70 72 6f 63 65 73  f the two proces
1135a 73 65 73 20 6d 75 73 74 20 67 69 76 65 20 77 61  ses must give wa
1135b 79 20 6f 72 20 74 68 65 72 65 20 63 61 6e 20 62  y or there can b
1135c 65 0a 2a 2a 20 6e 6f 20 70 72 6f 67 72 65 73 73  e.** no progress
1135d 2e 20 20 42 79 20 72 65 74 75 72 6e 69 6e 67 20  .  By returning 
1135e 53 51 4c 49 54 45 5f 42 55 53 59 20 61 6e 64 20  SQLITE_BUSY and 
1135f 6e 6f 74 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65  not invoking the
11360 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 0a 2a   busy callback.*
11361 2a 20 77 68 65 6e 20 41 20 61 6c 72 65 61 64 79  * when A already
11362 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b   has a read lock
11363 2c 20 77 65 20 65 6e 63 6f 75 72 61 67 65 20 41  , we encourage A
11364 20 74 6f 20 67 69 76 65 20 75 70 20 61 6e 64 20   to give up and 
11365 6c 65 74 20 42 0a 2a 2a 20 70 72 6f 63 65 65 64  let B.** proceed
11366 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
11367 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
11368 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 42  treeBeginTrans(B
11369 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 77 72 66  tree *p, int wrf
1136a 6c 61 67 29 7b 0a 20 20 42 74 53 68 61 72 65 64  lag){.  BtShared
1136b 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
1136c 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1136d 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33  E_OK;..  sqlite3
1136e 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
1136f 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62   pBt->db = p->db
11370 3b 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69  ;.  btreeIntegri
11371 74 79 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  ty(p);..  /* If 
11372 74 68 65 20 62 74 72 65 65 20 69 73 20 61 6c 72  the btree is alr
11373 65 61 64 79 20 69 6e 20 61 20 77 72 69 74 65 2d  eady in a write-
11374 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20  transaction, or 
11375 69 74 0a 20 20 2a 2a 20 69 73 20 61 6c 72 65 61  it.  ** is alrea
11376 64 79 20 69 6e 20 61 20 72 65 61 64 2d 74 72 61  dy in a read-tra
11377 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 61 20 72  nsaction and a r
11378 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 0a  ead-transaction.
11379 20 20 2a 2a 20 69 73 20 72 65 71 75 65 73 74 65    ** is requeste
1137a 64 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d  d, this is a no-
1137b 6f 70 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  op..  */.  if( p
1137c 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53  ->inTrans==TRANS
1137d 5f 57 52 49 54 45 20 7c 7c 20 28 70 2d 3e 69 6e  _WRITE || (p->in
1137e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 52 45 41  Trans==TRANS_REA
1137f 44 20 26 26 20 21 77 72 66 6c 61 67 29 20 29 7b  D && !wrflag) ){
11380 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f  .    goto trans_
11381 62 65 67 75 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  begun;.  }..  /*
11382 20 57 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   Write transacti
11383 6f 6e 73 20 61 72 65 20 6e 6f 74 20 70 6f 73 73  ons are not poss
11384 69 62 6c 65 20 6f 6e 20 61 20 72 65 61 64 2d 6f  ible on a read-o
11385 6e 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  nly database */.
11386 20 20 69 66 28 20 70 42 74 2d 3e 72 65 61 64 4f    if( pBt->readO
11387 6e 6c 79 20 26 26 20 77 72 66 6c 61 67 20 29 7b  nly && wrflag ){
11388 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
11389 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 67  _READONLY;.    g
1138a 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b  oto trans_begun;
1138b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e  .  }..  /* If an
1138c 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 68  other database h
1138d 61 6e 64 6c 65 20 68 61 73 20 61 6c 72 65 61 64  andle has alread
1138e 79 20 6f 70 65 6e 65 64 20 61 20 77 72 69 74 65  y opened a write
1138f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 20 20   transaction .  
11390 2a 2a 20 6f 6e 20 74 68 69 73 20 73 68 61 72 65  ** on this share
11391 64 2d 62 74 72 65 65 20 73 74 72 75 63 74 75 72  d-btree structur
11392 65 20 61 6e 64 20 61 20 73 65 63 6f 6e 64 20 77  e and a second w
11393 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  rite transaction
11394 20 69 73 0a 20 20 2a 2a 20 72 65 71 75 65 73 74   is.  ** request
11395 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ed, return SQLIT
11396 45 5f 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 20 69  E_BUSY..  */.  i
11397 66 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61  f( pBt->inTransa
11398 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49  ction==TRANS_WRI
11399 54 45 20 26 26 20 77 72 66 6c 61 67 20 29 7b 0a  TE && wrflag ){.
1139a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1139b 42 55 53 59 3b 0a 20 20 20 20 67 6f 74 6f 20 74  BUSY;.    goto t
1139c 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a  rans_begun;.  }.
1139d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1139e 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
1139f 45 0a 20 20 69 66 28 20 77 72 66 6c 61 67 3e 31  E.  if( wrflag>1
113a0 20 29 7b 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a   ){.    BtLock *
113a1 70 49 74 65 72 3b 0a 20 20 20 20 66 6f 72 28 70  pIter;.    for(p
113a2 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b  Iter=pBt->pLock;
113a3 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d 70 49   pIter; pIter=pI
113a4 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  ter->pNext){.   
113a5 20 20 20 69 66 28 20 70 49 74 65 72 2d 3e 70 42     if( pIter->pB
113a6 74 72 65 65 21 3d 70 20 29 7b 0a 20 20 20 20 20  tree!=p ){.     
113a7 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
113a8 55 53 59 3b 0a 20 20 20 20 20 20 20 20 67 6f 74  USY;.        got
113a9 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20  o trans_begun;. 
113aa 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
113ab 0a 23 65 6e 64 69 66 0a 0a 20 20 64 6f 20 7b 0a  .#endif..  do {.
113ac 20 20 20 20 69 66 28 20 70 42 74 2d 3e 70 50 61      if( pBt->pPa
113ad 67 65 31 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ge1==0 ){.      
113ae 64 6f 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  do{.        rc =
113af 20 6c 6f 63 6b 42 74 72 65 65 28 70 42 74 29 3b   lockBtree(pBt);
113b0 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 70  .      }while( p
113b1 42 74 2d 3e 70 50 61 67 65 31 3d 3d 30 20 26 26  Bt->pPage1==0 &&
113b2 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
113b3 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
113b4 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
113b5 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20  & wrflag ){.    
113b6 20 20 69 66 28 20 70 42 74 2d 3e 72 65 61 64 4f    if( pBt->readO
113b7 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 20 20 72  nly ){.        r
113b8 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 4f  c = SQLITE_READO
113b9 4e 4c 59 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  NLY;.      }else
113ba 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
113bb 71 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 6e  qlite3PagerBegin
113bc 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44  (pBt->pPage1->pD
113bd 62 50 61 67 65 2c 20 77 72 66 6c 61 67 3e 31 29  bPage, wrflag>1)
113be 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
113bf 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
113c0 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 6e 65           rc = ne
113c1 77 44 61 74 61 62 61 73 65 28 70 42 74 29 3b 0a  wDatabase(pBt);.
113c2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
113c3 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 69  }.    }.  .    i
113c4 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
113c5 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 77 72   ){.      if( wr
113c6 66 6c 61 67 20 29 20 70 42 74 2d 3e 69 6e 53 74  flag ) pBt->inSt
113c7 6d 74 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  mt = 0;.    }els
113c8 65 7b 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42  e{.      unlockB
113c9 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74  treeIfUnused(pBt
113ca 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 77 68 69 6c  );.    }.  }whil
113cb 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55  e( rc==SQLITE_BU
113cc 53 59 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61  SY && pBt->inTra
113cd 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f  nsaction==TRANS_
113ce 4e 4f 4e 45 20 26 26 0a 20 20 20 20 20 20 20 20  NONE &&.        
113cf 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e    sqlite3BtreeIn
113d0 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28  vokeBusyHandler(
113d1 70 42 74 2c 20 30 29 20 29 3b 0a 0a 20 20 69 66  pBt, 0) );..  if
113d2 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
113d3 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e  ){.    if( p->in
113d4 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e  Trans==TRANS_NON
113d5 45 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e  E ){.      pBt->
113d6 6e 54 72 61 6e 73 61 63 74 69 6f 6e 2b 2b 3b 0a  nTransaction++;.
113d7 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 69 6e 54      }.    p->inT
113d8 72 61 6e 73 20 3d 20 28 77 72 66 6c 61 67 3f 54  rans = (wrflag?T
113d9 52 41 4e 53 5f 57 52 49 54 45 3a 54 52 41 4e 53  RANS_WRITE:TRANS
113da 5f 52 45 41 44 29 3b 0a 20 20 20 20 69 66 28 20  _READ);.    if( 
113db 70 2d 3e 69 6e 54 72 61 6e 73 3e 70 42 74 2d 3e  p->inTrans>pBt->
113dc 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b  inTransaction ){
113dd 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72  .      pBt->inTr
113de 61 6e 73 61 63 74 69 6f 6e 20 3d 20 70 2d 3e 69  ansaction = p->i
113df 6e 54 72 61 6e 73 3b 0a 20 20 20 20 7d 0a 23 69  nTrans;.    }.#i
113e0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
113e1 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20  T_SHARED_CACHE. 
113e2 20 20 20 69 66 28 20 77 72 66 6c 61 67 3e 31 20     if( wrflag>1 
113e3 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
113e4 20 21 70 42 74 2d 3e 70 45 78 63 6c 75 73 69 76   !pBt->pExclusiv
113e5 65 20 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e  e );.      pBt->
113e6 70 45 78 63 6c 75 73 69 76 65 20 3d 20 70 3b 0a  pExclusive = p;.
113e7 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
113e8 0a 0a 0a 74 72 61 6e 73 5f 62 65 67 75 6e 3a 0a  ...trans_begun:.
113e9 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79    btreeIntegrity
113ea 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  (p);.  sqlite3Bt
113eb 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
113ec 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66  eturn rc;.}..#if
113ed 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
113ee 5f 41 55 54 4f 56 41 43 55 55 4d 0a 0a 2f 2a 0a  _AUTOVACUUM../*.
113ef 2a 2a 20 53 65 74 20 74 68 65 20 70 6f 69 6e 74  ** Set the point
113f0 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 66  er-map entries f
113f1 6f 72 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20  or all children 
113f2 6f 66 20 70 61 67 65 20 70 50 61 67 65 2e 20 41  of page pPage. A
113f3 6c 73 6f 2c 20 69 66 0a 2a 2a 20 70 50 61 67 65  lso, if.** pPage
113f4 20 63 6f 6e 74 61 69 6e 73 20 63 65 6c 6c 73 20   contains cells 
113f5 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 76  that point to ov
113f6 65 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 73 65  erflow pages, se
113f7 74 20 74 68 65 20 70 6f 69 6e 74 65 72 0a 2a 2a  t the pointer.**
113f8 20 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72   map entries for
113f9 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61   the overflow pa
113fa 67 65 73 20 61 73 20 77 65 6c 6c 2e 0a 2a 2f 0a  ges as well..*/.
113fb 73 74 61 74 69 63 20 69 6e 74 20 73 65 74 43 68  static int setCh
113fc 69 6c 64 50 74 72 6d 61 70 73 28 4d 65 6d 50 61  ildPtrmaps(MemPa
113fd 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 6e  ge *pPage){.  in
113fe 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
113ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11400 20 2f 2a 20 43 6f 75 6e 74 65 72 20 76 61 72 69   /* Counter vari
11401 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  able */.  int nC
11402 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  ell;            
11403 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11404 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20  Number of cells 
11405 69 6e 20 70 61 67 65 20 70 50 61 67 65 20 2a 2f  in page pPage */
11406 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
11407 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11408 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
11409 63 6f 64 65 20 2a 2f 0a 20 20 42 74 53 68 61 72  code */.  BtShar
1140a 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d  ed *pBt = pPage-
1140b 3e 70 42 74 3b 0a 20 20 69 6e 74 20 69 73 49 6e  >pBt;.  int isIn
1140c 69 74 4f 72 69 67 20 3d 20 70 50 61 67 65 2d 3e  itOrig = pPage->
1140d 69 73 49 6e 69 74 3b 0a 20 20 50 67 6e 6f 20 70  isInit;.  Pgno p
1140e 67 6e 6f 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e  gno = pPage->pgn
1140f 6f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  o;..  assert( sq
11410 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
11411 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74  (pPage->pBt->mut
11412 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  ex) );.  rc = sq
11413 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61  lite3BtreeInitPa
11414 67 65 28 70 50 61 67 65 2c 20 70 50 61 67 65 2d  ge(pPage, pPage-
11415 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20 69 66 28  >pParent);.  if(
11416 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
11417 7b 0a 20 20 20 20 67 6f 74 6f 20 73 65 74 5f 63  {.    goto set_c
11418 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74  hild_ptrmaps_out
11419 3b 0a 20 20 7d 0a 20 20 6e 43 65 6c 6c 20 3d 20  ;.  }.  nCell = 
1141a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20  pPage->nCell;.. 
1141b 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c   for(i=0; i<nCel
1141c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20  l; i++){.    u8 
1141d 2a 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c  *pCell = findCel
1141e 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a 0a 20 20  l(pPage, i);..  
1141f 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74    rc = ptrmapPut
11420 4f 76 66 6c 50 74 72 28 70 50 61 67 65 2c 20 70  OvflPtr(pPage, p
11421 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 72  Cell);.    if( r
11422 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
11423 20 20 20 20 20 20 67 6f 74 6f 20 73 65 74 5f 63        goto set_c
11424 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74  hild_ptrmaps_out
11425 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
11426 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
11427 0a 20 20 20 20 20 20 50 67 6e 6f 20 63 68 69 6c  .      Pgno chil
11428 64 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65  dPgno = get4byte
11429 28 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 72  (pCell);.      r
1142a 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42  c = ptrmapPut(pB
1142b 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50 54  t, childPgno, PT
1142c 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 67 6e 6f  RMAP_BTREE, pgno
1142d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
1142e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
1142f 6f 20 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d  o set_child_ptrm
11430 61 70 73 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  aps_out;.    }. 
11431 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65   }..  if( !pPage
11432 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 50 67  ->leaf ){.    Pg
11433 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 67  no childPgno = g
11434 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e  et4byte(&pPage->
11435 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72  aData[pPage->hdr
11436 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20  Offset+8]);.    
11437 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70  rc = ptrmapPut(p
11438 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50  Bt, childPgno, P
11439 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 67 6e  TRMAP_BTREE, pgn
1143a 6f 29 3b 0a 20 20 7d 0a 0a 73 65 74 5f 63 68 69  o);.  }..set_chi
1143b 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3a 0a  ld_ptrmaps_out:.
1143c 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20    pPage->isInit 
1143d 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b 0a 20 20  = isInitOrig;.  
1143e 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1143f 0a 2a 2a 20 53 6f 6d 65 77 68 65 72 65 20 6f 6e  .** Somewhere on
11440 20 70 50 61 67 65 2c 20 77 68 69 63 68 20 69 73   pPage, which is
11441 20 67 75 61 72 65 6e 74 65 65 64 20 74 6f 20 62   guarenteed to b
11442 65 20 61 20 62 74 72 65 65 20 70 61 67 65 2c 20  e a btree page, 
11443 6e 6f 74 20 61 6e 20 6f 76 65 72 66 6c 6f 77 0a  not an overflow.
11444 2a 2a 20 70 61 67 65 2c 20 69 73 20 61 20 70 6f  ** page, is a po
11445 69 6e 74 65 72 20 74 6f 20 70 61 67 65 20 69 46  inter to page iF
11446 72 6f 6d 2e 20 4d 6f 64 69 66 79 20 74 68 69 73  rom. Modify this
11447 20 70 6f 69 6e 74 65 72 20 73 6f 20 74 68 61 74   pointer so that
11448 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a   it points to.**
11449 20 69 54 6f 2e 20 50 61 72 61 6d 65 74 65 72 20   iTo. Parameter 
1144a 65 54 79 70 65 20 64 65 73 63 72 69 62 65 73 20  eType describes 
1144b 74 68 65 20 74 79 70 65 20 6f 66 20 70 6f 69 6e  the type of poin
1144c 74 65 72 20 74 6f 20 62 65 20 6d 6f 64 69 66 69  ter to be modifi
1144d 65 64 2c 20 61 73 20 0a 2a 2a 20 66 6f 6c 6c 6f  ed, as .** follo
1144e 77 73 3a 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50  ws:.**.** PTRMAP
1144f 5f 42 54 52 45 45 3a 20 20 20 20 20 70 50 61 67  _BTREE:     pPag
11450 65 20 69 73 20 61 20 62 74 72 65 65 2d 70 61 67  e is a btree-pag
11451 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 70  e. The pointer p
11452 6f 69 6e 74 73 20 61 74 20 61 20 63 68 69 6c 64  oints at a child
11453 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20   .**            
11454 20 20 20 20 20 20 20 70 61 67 65 20 6f 66 20 70         page of p
11455 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d  Page..**.** PTRM
11456 41 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 70 50  AP_OVERFLOW1: pP
11457 61 67 65 20 69 73 20 61 20 62 74 72 65 65 2d 70  age is a btree-p
11458 61 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72  age. The pointer
11459 20 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 6f 76   points at an ov
1145a 65 72 66 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 20  erflow.**       
1145b 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65              page
1145c 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 6f   pointed to by o
1145d 6e 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20  ne of the cells 
1145e 6f 6e 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20  on pPage..**.** 
1145f 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32  PTRMAP_OVERFLOW2
11460 3a 20 70 50 61 67 65 20 69 73 20 61 6e 20 6f 76  : pPage is an ov
11461 65 72 66 6c 6f 77 2d 70 61 67 65 2e 20 54 68 65  erflow-page. The
11462 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20   pointer points 
11463 61 74 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 20  at the next.**  
11464 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11465 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69   overflow page i
11466 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73  n the list..*/.s
11467 74 61 74 69 63 20 69 6e 74 20 6d 6f 64 69 66 79  tatic int modify
11468 50 61 67 65 50 6f 69 6e 74 65 72 28 4d 65 6d 50  PagePointer(MemP
11469 61 67 65 20 2a 70 50 61 67 65 2c 20 50 67 6e 6f  age *pPage, Pgno
1146a 20 69 46 72 6f 6d 2c 20 50 67 6e 6f 20 69 54 6f   iFrom, Pgno iTo
1146b 2c 20 75 38 20 65 54 79 70 65 29 7b 0a 20 20 61  , u8 eType){.  a
1146c 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1146d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d  utex_held(pPage-
1146e 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  >pBt->mutex) );.
1146f 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52    if( eType==PTR
11470 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 29 7b  MAP_OVERFLOW2 ){
11471 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e  .    /* The poin
11472 74 65 72 20 69 73 20 61 6c 77 61 79 73 20 74 68  ter is always th
11473 65 20 66 69 72 73 74 20 34 20 62 79 74 65 73 20  e first 4 bytes 
11474 6f 66 20 74 68 65 20 70 61 67 65 20 69 6e 20 74  of the page in t
11475 68 69 73 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20  his case.  */.  
11476 20 20 69 66 28 20 67 65 74 34 62 79 74 65 28 70    if( get4byte(p
11477 50 61 67 65 2d 3e 61 44 61 74 61 29 21 3d 69 46  Page->aData)!=iF
11478 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 72 65 74  rom ){.      ret
11479 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
1147a 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20  PT_BKPT;.    }. 
1147b 20 20 20 70 75 74 34 62 79 74 65 28 70 50 61 67     put4byte(pPag
1147c 65 2d 3e 61 44 61 74 61 2c 20 69 54 6f 29 3b 0a  e->aData, iTo);.
1147d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74    }else{.    int
1147e 20 69 73 49 6e 69 74 4f 72 69 67 20 3d 20 70 50   isInitOrig = pP
1147f 61 67 65 2d 3e 69 73 49 6e 69 74 3b 0a 20 20 20  age->isInit;.   
11480 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 6e 74 20   int i;.    int 
11481 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 73 71 6c 69  nCell;..    sqli
11482 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65  te3BtreeInitPage
11483 28 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20  (pPage, 0);.    
11484 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e  nCell = pPage->n
11485 43 65 6c 6c 3b 0a 0a 20 20 20 20 66 6f 72 28 69  Cell;..    for(i
11486 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b  =0; i<nCell; i++
11487 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43 65  ){.      u8 *pCe
11488 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50  ll = findCell(pP
11489 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 20 20 69  age, i);.      i
1148a 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50  f( eType==PTRMAP
1148b 5f 4f 56 45 52 46 4c 4f 57 31 20 29 7b 0a 20 20  _OVERFLOW1 ){.  
1148c 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69        CellInfo i
1148d 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  nfo;.        sql
1148e 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65  ite3BtreeParseCe
1148f 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65  llPtr(pPage, pCe
11490 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20  ll, &info);.    
11491 20 20 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76      if( info.iOv
11492 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20  erflow ){.      
11493 20 20 20 20 69 66 28 20 69 46 72 6f 6d 3d 3d 67      if( iFrom==g
11494 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69  et4byte(&pCell[i
11495 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 20  nfo.iOverflow]) 
11496 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  ){.            p
11497 75 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69  ut4byte(&pCell[i
11498 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 2c 20  nfo.iOverflow], 
11499 69 54 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20  iTo);.          
1149a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
1149b 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
1149c 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1149d 20 20 20 20 69 66 28 20 67 65 74 34 62 79 74 65      if( get4byte
1149e 28 70 43 65 6c 6c 29 3d 3d 69 46 72 6f 6d 20 29  (pCell)==iFrom )
1149f 7b 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 34  {.          put4
114a0 62 79 74 65 28 70 43 65 6c 6c 2c 20 69 54 6f 29  byte(pCell, iTo)
114a1 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
114a2 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
114a3 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20     }.    }.  .  
114a4 20 20 69 66 28 20 69 3d 3d 6e 43 65 6c 6c 20 29    if( i==nCell )
114a5 7b 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 70  {.      if( eTyp
114a6 65 21 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20  e!=PTRMAP_BTREE 
114a7 7c 7c 20 0a 20 20 20 20 20 20 20 20 20 20 67 65  || .          ge
114a8 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61  t4byte(&pPage->a
114a9 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f  Data[pPage->hdrO
114aa 66 66 73 65 74 2b 38 5d 29 21 3d 69 46 72 6f 6d  ffset+8])!=iFrom
114ab 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
114ac 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
114ad 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a  T_BKPT;.      }.
114ae 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26        put4byte(&
114af 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61  pPage->aData[pPa
114b0 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  ge->hdrOffset+8]
114b1 2c 20 69 54 6f 29 3b 0a 20 20 20 20 7d 0a 0a 20  , iTo);.    }.. 
114b2 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74     pPage->isInit
114b3 20 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b 0a 20   = isInitOrig;. 
114b4 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
114b5 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  TE_OK;.}.../*.**
114b6 20 4d 6f 76 65 20 74 68 65 20 6f 70 65 6e 20 64   Move the open d
114b7 61 74 61 62 61 73 65 20 70 61 67 65 20 70 44 62  atabase page pDb
114b8 50 61 67 65 20 74 6f 20 6c 6f 63 61 74 69 6f 6e  Page to location
114b9 20 69 46 72 65 65 50 61 67 65 20 69 6e 20 74 68   iFreePage in th
114ba 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20  e .** database. 
114bb 54 68 65 20 70 44 62 50 61 67 65 20 72 65 66 65  The pDbPage refe
114bc 72 65 6e 63 65 20 72 65 6d 61 69 6e 73 20 76 61  rence remains va
114bd 6c 69 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  lid..*/.static i
114be 6e 74 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28  nt relocatePage(
114bf 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
114c0 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42  ,           /* B
114c1 74 72 65 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67  tree */.  MemPag
114c2 65 20 2a 70 44 62 50 61 67 65 2c 20 20 20 20 20  e *pDbPage,     
114c3 20 20 20 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20     /* Open page 
114c4 74 6f 20 6d 6f 76 65 20 2a 2f 0a 20 20 75 38 20  to move */.  u8 
114c5 65 54 79 70 65 2c 20 20 20 20 20 20 20 20 20 20  eType,          
114c6 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
114c7 20 6d 61 70 20 27 74 79 70 65 27 20 65 6e 74 72   map 'type' entr
114c8 79 20 66 6f 72 20 70 44 62 50 61 67 65 20 2a 2f  y for pDbPage */
114c9 0a 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65  .  Pgno iPtrPage
114ca 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  ,           /* P
114cb 6f 69 6e 74 65 72 20 6d 61 70 20 27 70 61 67 65  ointer map 'page
114cc 2d 6e 6f 27 20 65 6e 74 72 79 20 66 6f 72 20 70  -no' entry for p
114cd 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f  DbPage */.  Pgno
114ce 20 69 46 72 65 65 50 61 67 65 20 20 20 20 20 20   iFreePage      
114cf 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 61       /* The loca
114d0 74 69 6f 6e 20 74 6f 20 6d 6f 76 65 20 70 44 62  tion to move pDb
114d1 50 61 67 65 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20  Page to */.){.  
114d2 4d 65 6d 50 61 67 65 20 2a 70 50 74 72 50 61 67  MemPage *pPtrPag
114d3 65 3b 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65  e;   /* The page
114d4 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61   that contains a
114d5 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 44 62 50   pointer to pDbP
114d6 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 44  age */.  Pgno iD
114d7 62 50 61 67 65 20 3d 20 70 44 62 50 61 67 65 2d  bPage = pDbPage-
114d8 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65 72 20 2a  >pgno;.  Pager *
114d9 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50  pPager = pBt->pP
114da 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  ager;.  int rc;.
114db 0a 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65  .  assert( eType
114dc 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f  ==PTRMAP_OVERFLO
114dd 57 32 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52  W2 || eType==PTR
114de 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 7c 7c  MAP_OVERFLOW1 ||
114df 20 0a 20 20 20 20 20 20 65 54 79 70 65 3d 3d 50   .      eType==P
114e0 54 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 65  TRMAP_BTREE || e
114e1 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f  Type==PTRMAP_ROO
114e2 54 50 41 47 45 20 29 3b 0a 20 20 61 73 73 65 72  TPAGE );.  asser
114e3 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
114e4 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78  _held(pBt->mutex
114e5 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
114e6 44 62 50 61 67 65 2d 3e 70 42 74 3d 3d 70 42 74  DbPage->pBt==pBt
114e7 20 29 3b 0a 0a 20 20 2f 2a 20 4d 6f 76 65 20 70   );..  /* Move p
114e8 61 67 65 20 69 44 62 50 61 67 65 20 66 72 6f 6d  age iDbPage from
114e9 20 69 74 73 20 63 75 72 72 65 6e 74 20 6c 6f 63   its current loc
114ea 61 74 69 6f 6e 20 74 6f 20 70 61 67 65 20 6e 75  ation to page nu
114eb 6d 62 65 72 20 69 46 72 65 65 50 61 67 65 20 2a  mber iFreePage *
114ec 2f 0a 20 20 54 52 41 43 45 28 28 22 41 55 54 4f  /.  TRACE(("AUTO
114ed 56 41 43 55 55 4d 3a 20 4d 6f 76 69 6e 67 20 25  VACUUM: Moving %
114ee 64 20 74 6f 20 66 72 65 65 20 70 61 67 65 20 25  d to free page %
114ef 64 20 28 70 74 72 20 70 61 67 65 20 25 64 20 74  d (ptr page %d t
114f0 79 70 65 20 25 64 29 5c 6e 22 2c 20 0a 20 20 20  ype %d)\n", .   
114f1 20 20 20 69 44 62 50 61 67 65 2c 20 69 46 72 65     iDbPage, iFre
114f2 65 50 61 67 65 2c 20 69 50 74 72 50 61 67 65 2c  ePage, iPtrPage,
114f3 20 65 54 79 70 65 29 29 3b 0a 20 20 72 63 20 3d   eType));.  rc =
114f4 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76   sqlite3PagerMov
114f5 65 70 61 67 65 28 70 50 61 67 65 72 2c 20 70 44  epage(pPager, pD
114f6 62 50 61 67 65 2d 3e 70 44 62 50 61 67 65 2c 20  bPage->pDbPage, 
114f7 69 46 72 65 65 50 61 67 65 29 3b 0a 20 20 69 66  iFreePage);.  if
114f8 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
114f9 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
114fa 3b 0a 20 20 7d 0a 20 20 70 44 62 50 61 67 65 2d  ;.  }.  pDbPage-
114fb 3e 70 67 6e 6f 20 3d 20 69 46 72 65 65 50 61 67  >pgno = iFreePag
114fc 65 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 44 62 50  e;..  /* If pDbP
114fd 61 67 65 20 77 61 73 20 61 20 62 74 72 65 65 2d  age was a btree-
114fe 70 61 67 65 2c 20 74 68 65 6e 20 69 74 20 6d 61  page, then it ma
114ff 79 20 68 61 76 65 20 63 68 69 6c 64 20 70 61 67  y have child pag
11500 65 73 20 61 6e 64 2f 6f 72 20 63 65 6c 6c 73 0a  es and/or cells.
11501 20 20 2a 2a 20 74 68 61 74 20 70 6f 69 6e 74 20    ** that point 
11502 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  to overflow page
11503 73 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d  s. The pointer m
11504 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20 61  ap entries for a
11505 6c 6c 20 74 68 65 73 65 0a 20 20 2a 2a 20 70 61  ll these.  ** pa
11506 67 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20 63  ges need to be c
11507 68 61 6e 67 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a  hanged..  **.  *
11508 2a 20 49 66 20 70 44 62 50 61 67 65 20 69 73 20  * If pDbPage is 
11509 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  an overflow page
1150a 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72 73 74  , then the first
1150b 20 34 20 62 79 74 65 73 20 6d 61 79 20 73 74 6f   4 bytes may sto
1150c 72 65 20 61 0a 20 20 2a 2a 20 70 6f 69 6e 74 65  re a.  ** pointe
1150d 72 20 74 6f 20 61 20 73 75 62 73 65 71 75 65 6e  r to a subsequen
1150e 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e  t overflow page.
1150f 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
11510 63 61 73 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20  case, then.  ** 
11511 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20  the pointer map 
11512 6e 65 65 64 73 20 74 6f 20 62 65 20 75 70 64 61  needs to be upda
11513 74 65 64 20 66 6f 72 20 74 68 65 20 73 75 62 73  ted for the subs
11514 65 71 75 65 6e 74 20 6f 76 65 72 66 6c 6f 77 20  equent overflow 
11515 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  page..  */.  if(
11516 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 42   eType==PTRMAP_B
11517 54 52 45 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50  TREE || eType==P
11518 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29  TRMAP_ROOTPAGE )
11519 7b 0a 20 20 20 20 72 63 20 3d 20 73 65 74 43 68  {.    rc = setCh
1151a 69 6c 64 50 74 72 6d 61 70 73 28 70 44 62 50 61  ildPtrmaps(pDbPa
1151b 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  ge);.    if( rc!
1151c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1151d 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1151e 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
1151f 20 20 50 67 6e 6f 20 6e 65 78 74 4f 76 66 6c 20    Pgno nextOvfl 
11520 3d 20 67 65 74 34 62 79 74 65 28 70 44 62 50 61  = get4byte(pDbPa
11521 67 65 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20 20  ge->aData);.    
11522 69 66 28 20 6e 65 78 74 4f 76 66 6c 21 3d 30 20  if( nextOvfl!=0 
11523 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74  ){.      rc = pt
11524 72 6d 61 70 50 75 74 28 70 42 74 2c 20 6e 65 78  rmapPut(pBt, nex
11525 74 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56  tOvfl, PTRMAP_OV
11526 45 52 46 4c 4f 57 32 2c 20 69 46 72 65 65 50 61  ERFLOW2, iFreePa
11527 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ge);.      if( r
11528 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
11529 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
1152a 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  c;.      }.    }
1152b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 78 20 74  .  }..  /* Fix t
1152c 68 65 20 64 61 74 61 62 61 73 65 20 70 6f 69 6e  he database poin
1152d 74 65 72 20 6f 6e 20 70 61 67 65 20 69 50 74 72  ter on page iPtr
1152e 50 61 67 65 20 74 68 61 74 20 70 6f 69 6e 74 65  Page that pointe
1152f 64 20 61 74 20 69 44 62 50 61 67 65 20 73 6f 0a  d at iDbPage so.
11530 20 20 2a 2a 20 74 68 61 74 20 69 74 20 70 6f 69    ** that it poi
11531 6e 74 73 20 61 74 20 69 46 72 65 65 50 61 67 65  nts at iFreePage
11532 2e 20 41 6c 73 6f 20 66 69 78 20 74 68 65 20 70  . Also fix the p
11533 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79  ointer map entry
11534 20 66 6f 72 0a 20 20 2a 2a 20 69 50 74 72 50 61   for.  ** iPtrPa
11535 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65  ge..  */.  if( e
11536 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f 4f  Type!=PTRMAP_ROO
11537 54 50 41 47 45 20 29 7b 0a 20 20 20 20 72 63 20  TPAGE ){.    rc 
11538 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
11539 74 50 61 67 65 28 70 42 74 2c 20 69 50 74 72 50  tPage(pBt, iPtrP
1153a 61 67 65 2c 20 26 70 50 74 72 50 61 67 65 2c 20  age, &pPtrPage, 
1153b 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  0);.    if( rc!=
1153c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1153d 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
1153e 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c    }.    rc = sql
1153f 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
11540 50 74 72 50 61 67 65 2d 3e 70 44 62 50 61 67 65  PtrPage->pDbPage
11541 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
11542 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
11543 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
11544 74 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20 72  trPage);.      r
11545 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
11546 20 20 20 20 72 63 20 3d 20 6d 6f 64 69 66 79 50      rc = modifyP
11547 61 67 65 50 6f 69 6e 74 65 72 28 70 50 74 72 50  agePointer(pPtrP
11548 61 67 65 2c 20 69 44 62 50 61 67 65 2c 20 69 46  age, iDbPage, iF
11549 72 65 65 50 61 67 65 2c 20 65 54 79 70 65 29 3b  reePage, eType);
1154a 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65  .    releasePage
1154b 28 70 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20  (pPtrPage);.    
1154c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1154d 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  K ){.      rc = 
1154e 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 69  ptrmapPut(pBt, i
1154f 46 72 65 65 50 61 67 65 2c 20 65 54 79 70 65 2c  FreePage, eType,
11550 20 69 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20   iPtrPage);.    
11551 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
11552 63 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  c;.}../* Forward
11553 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 72 65 71   declaration req
11554 75 69 72 65 64 20 62 79 20 69 6e 63 72 56 61 63  uired by incrVac
11555 75 75 6d 53 74 65 70 28 29 2e 20 2a 2f 0a 73 74  uumStep(). */.st
11556 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74  atic int allocat
11557 65 42 74 72 65 65 50 61 67 65 28 42 74 53 68 61  eBtreePage(BtSha
11558 72 65 64 20 2a 2c 20 4d 65 6d 50 61 67 65 20 2a  red *, MemPage *
11559 2a 2c 20 50 67 6e 6f 20 2a 2c 20 50 67 6e 6f 2c  *, Pgno *, Pgno,
1155a 20 75 38 29 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72   u8);../*.** Per
1155b 66 6f 72 6d 20 61 20 73 69 6e 67 6c 65 20 73 74  form a single st
1155c 65 70 20 6f 66 20 61 6e 20 69 6e 63 72 65 6d 65  ep of an increme
1155d 6e 74 61 6c 2d 76 61 63 75 75 6d 2e 20 49 66 20  ntal-vacuum. If 
1155e 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 72  successful,.** r
1155f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e  eturn SQLITE_OK.
11560 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
11561 77 6f 72 6b 20 74 6f 20 64 6f 20 28 61 6e 64 20  work to do (and 
11562 74 68 65 72 65 66 6f 72 65 20 6e 6f 0a 2a 2a 20  therefore no.** 
11563 70 6f 69 6e 74 20 69 6e 20 63 61 6c 6c 69 6e 67  point in calling
11564 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   this function a
11565 67 61 69 6e 29 2c 20 72 65 74 75 72 6e 20 53 51  gain), return SQ
11566 4c 49 54 45 5f 44 4f 4e 45 2e 0a 2a 2a 0a 2a 2a  LITE_DONE..**.**
11567 20 4d 6f 72 65 20 73 70 65 63 69 66 69 63 6c 79   More specificly
11568 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
11569 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 2d 6f  attempts to re-o
1156a 72 67 61 6e 69 7a 65 20 74 68 65 20 0a 2a 2a 20  rganize the .** 
1156b 64 61 74 61 62 61 73 65 20 73 6f 20 74 68 61 74  database so that
1156c 20 74 68 65 20 6c 61 73 74 20 70 61 67 65 20 6f   the last page o
1156d 66 20 74 68 65 20 66 69 6c 65 20 63 75 72 72 65  f the file curre
1156e 6e 74 6c 79 20 69 6e 20 75 73 65 0a 2a 2a 20 69  ntly in use.** i
1156f 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 69 6e 20 75  s no longer in u
11570 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  se..**.** If the
11571 20 6e 46 69 6e 20 70 61 72 61 6d 65 74 65 72 20   nFin parameter 
11572 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
11573 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
11574 61 73 73 75 6d 65 73 0a 2a 2a 20 74 68 61 74 20  assumes.** that 
11575 74 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20  the caller will 
11576 6b 65 65 70 20 63 61 6c 6c 69 6e 67 20 69 6e 63  keep calling inc
11577 72 56 61 63 75 75 6d 53 74 65 70 28 29 20 75 6e  rVacuumStep() un
11578 74 69 6c 0a 2a 2a 20 69 74 20 72 65 74 75 72 6e  til.** it return
11579 73 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 6f 72  s SQLITE_DONE or
1157a 20 61 6e 20 65 72 72 6f 72 2c 20 61 6e 64 20 74   an error, and t
1157b 68 61 74 20 6e 46 69 6e 20 69 73 20 74 68 65 0a  hat nFin is the.
1157c 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  ** number of pag
1157d 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  es the database 
1157e 66 69 6c 65 20 77 69 6c 6c 20 63 6f 6e 74 61 69  file will contai
1157f 6e 20 61 66 74 65 72 20 74 68 69 73 20 0a 2a 2a  n after this .**
11580 20 70 72 6f 63 65 73 73 20 69 73 20 63 6f 6d 70   process is comp
11581 6c 65 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lete..*/.static 
11582 69 6e 74 20 69 6e 63 72 56 61 63 75 75 6d 53 74  int incrVacuumSt
11583 65 70 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  ep(BtShared *pBt
11584 2c 20 50 67 6e 6f 20 6e 46 69 6e 29 7b 0a 20 20  , Pgno nFin){.  
11585 50 67 6e 6f 20 69 4c 61 73 74 50 67 3b 20 20 20  Pgno iLastPg;   
11586 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73            /* Las
11587 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61  t page in the da
11588 74 61 62 61 73 65 20 2a 2f 0a 20 20 50 67 6e 6f  tabase */.  Pgno
11589 20 6e 46 72 65 65 4c 69 73 74 3b 20 20 20 20 20   nFreeList;     
1158a 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1158b 6f 66 20 70 61 67 65 73 20 73 74 69 6c 6c 20 6f  of pages still o
1158c 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20  n the free-list 
1158d 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  */..  assert( sq
1158e 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1158f 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
11590 20 20 69 4c 61 73 74 50 67 20 3d 20 70 42 74 2d    iLastPg = pBt-
11591 3e 6e 54 72 75 6e 63 3b 0a 20 20 69 66 28 20 69  >nTrunc;.  if( i
11592 4c 61 73 74 50 67 3d 3d 30 20 29 7b 0a 20 20 20  LastPg==0 ){.   
11593 20 69 4c 61 73 74 50 67 20 3d 20 73 71 6c 69 74   iLastPg = sqlit
11594 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
11595 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20  (pBt->pPager);. 
11596 20 7d 0a 0a 20 20 69 66 28 20 21 50 54 52 4d 41   }..  if( !PTRMA
11597 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 69 4c  P_ISPAGE(pBt, iL
11598 61 73 74 50 67 29 20 26 26 20 69 4c 61 73 74 50  astPg) && iLastP
11599 67 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  g!=PENDING_BYTE_
1159a 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20  PAGE(pBt) ){.   
1159b 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 75 38 20   int rc;.    u8 
1159c 65 54 79 70 65 3b 0a 20 20 20 20 50 67 6e 6f 20  eType;.    Pgno 
1159d 69 50 74 72 50 61 67 65 3b 0a 0a 20 20 20 20 6e  iPtrPage;..    n
1159e 46 72 65 65 4c 69 73 74 20 3d 20 67 65 74 34 62  FreeList = get4b
1159f 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31  yte(&pBt->pPage1
115a0 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20  ->aData[36]);.  
115a1 20 20 69 66 28 20 6e 46 72 65 65 4c 69 73 74 3d    if( nFreeList=
115a2 3d 30 20 7c 7c 20 6e 46 69 6e 3d 3d 69 4c 61 73  =0 || nFin==iLas
115a3 74 50 67 20 29 7b 0a 20 20 20 20 20 20 72 65 74  tPg ){.      ret
115a4 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  urn SQLITE_DONE;
115a5 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d  .    }..    rc =
115a6 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20   ptrmapGet(pBt, 
115a7 69 4c 61 73 74 50 67 2c 20 26 65 54 79 70 65 2c  iLastPg, &eType,
115a8 20 26 69 50 74 72 50 61 67 65 29 3b 0a 20 20 20   &iPtrPage);.   
115a9 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
115aa 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  OK ){.      retu
115ab 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
115ac 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d   if( eType==PTRM
115ad 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20  AP_ROOTPAGE ){. 
115ae 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
115af 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
115b0 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
115b1 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52  eType==PTRMAP_FR
115b2 45 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20  EEPAGE ){.      
115b3 69 66 28 20 6e 46 69 6e 3d 3d 30 20 29 7b 0a 20  if( nFin==0 ){. 
115b4 20 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65         /* Remove
115b5 20 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20 74   the page from t
115b6 68 65 20 66 69 6c 65 73 20 66 72 65 65 2d 6c 69  he files free-li
115b7 73 74 2e 20 54 68 69 73 20 69 73 20 6e 6f 74 20  st. This is not 
115b8 72 65 71 75 69 72 65 64 0a 20 20 20 20 20 20 20  required.       
115b9 20 2a 2a 20 69 66 20 6e 46 69 6e 20 69 73 20 6e   ** if nFin is n
115ba 6f 6e 2d 7a 65 72 6f 2e 20 49 6e 20 74 68 61 74  on-zero. In that
115bb 20 63 61 73 65 2c 20 74 68 65 20 66 72 65 65 2d   case, the free-
115bc 6c 69 73 74 20 77 69 6c 6c 20 62 65 0a 20 20 20  list will be.   
115bd 20 20 20 20 20 2a 2a 20 74 72 75 6e 63 61 74 65       ** truncate
115be 64 20 74 6f 20 7a 65 72 6f 20 61 66 74 65 72 20  d to zero after 
115bf 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  this function re
115c0 74 75 72 6e 73 2c 20 73 6f 20 69 74 20 64 6f 65  turns, so it doe
115c1 73 6e 27 74 20 0a 20 20 20 20 20 20 20 20 2a 2a  sn't .        **
115c2 20 6d 61 74 74 65 72 20 69 66 20 69 74 20 73 74   matter if it st
115c3 69 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d  ill contains som
115c4 65 20 67 61 72 62 61 67 65 20 65 6e 74 72 69 65  e garbage entrie
115c5 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  s..        */.  
115c6 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65        Pgno iFree
115c7 50 67 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 50  Pg;.        MemP
115c8 61 67 65 20 2a 70 46 72 65 65 50 67 3b 0a 20 20  age *pFreePg;.  
115c9 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63        rc = alloc
115ca 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74  ateBtreePage(pBt
115cb 2c 20 26 70 46 72 65 65 50 67 2c 20 26 69 46 72  , &pFreePg, &iFr
115cc 65 65 50 67 2c 20 69 4c 61 73 74 50 67 2c 20 31  eePg, iLastPg, 1
115cd 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
115ce 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
115cf 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
115d0 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   rc;.        }. 
115d1 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69         assert( i
115d2 46 72 65 65 50 67 3d 3d 69 4c 61 73 74 50 67 20  FreePg==iLastPg 
115d3 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61  );.        relea
115d4 73 65 50 61 67 65 28 70 46 72 65 65 50 67 29 3b  sePage(pFreePg);
115d5 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 65  .      }.    } e
115d6 6c 73 65 20 7b 0a 20 20 20 20 20 20 50 67 6e 6f  lse {.      Pgno
115d7 20 69 46 72 65 65 50 67 3b 20 20 20 20 20 20 20   iFreePg;       
115d8 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
115d9 66 20 66 72 65 65 20 70 61 67 65 20 74 6f 20 6d  f free page to m
115da 6f 76 65 20 70 4c 61 73 74 50 67 20 74 6f 20 2a  ove pLastPg to *
115db 2f 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20  /.      MemPage 
115dc 2a 70 4c 61 73 74 50 67 3b 0a 0a 20 20 20 20 20  *pLastPg;..     
115dd 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
115de 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69  eeGetPage(pBt, i
115df 4c 61 73 74 50 67 2c 20 26 70 4c 61 73 74 50 67  LastPg, &pLastPg
115e0 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 0);.      if( 
115e1 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
115e2 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
115e3 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  rc;.      }..   
115e4 20 20 20 2f 2a 20 49 66 20 6e 46 69 6e 20 69 73     /* If nFin is
115e5 20 7a 65 72 6f 2c 20 74 68 69 73 20 6c 6f 6f 70   zero, this loop
115e6 20 72 75 6e 73 20 65 78 61 63 74 6c 79 20 6f 6e   runs exactly on
115e7 63 65 20 61 6e 64 20 70 61 67 65 20 70 4c 61 73  ce and page pLas
115e8 74 50 67 0a 20 20 20 20 20 20 2a 2a 20 69 73 20  tPg.      ** is 
115e9 73 77 61 70 70 65 64 20 77 69 74 68 20 74 68 65  swapped with the
115ea 20 66 69 72 73 74 20 66 72 65 65 20 70 61 67 65   first free page
115eb 20 70 75 6c 6c 65 64 20 6f 66 66 20 74 68 65 20   pulled off the 
115ec 66 72 65 65 20 6c 69 73 74 2e 0a 20 20 20 20 20  free list..     
115ed 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4f 6e 20   **.      ** On 
115ee 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20  the other hand, 
115ef 69 66 20 6e 46 69 6e 20 69 73 20 67 72 65 61 74  if nFin is great
115f0 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68  er than zero, th
115f1 65 6e 20 6b 65 65 70 0a 20 20 20 20 20 20 2a 2a  en keep.      **
115f2 20 6c 6f 6f 70 69 6e 67 20 75 6e 74 69 6c 20 61   looping until a
115f3 20 66 72 65 65 2d 70 61 67 65 20 6c 6f 63 61 74   free-page locat
115f4 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 66 69  ed within the fi
115f5 72 73 74 20 6e 46 69 6e 20 70 61 67 65 73 0a 20  rst nFin pages. 
115f6 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 66       ** of the f
115f7 69 6c 65 20 69 73 20 66 6f 75 6e 64 2e 0a 20 20  ile is found..  
115f8 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f 20      */.      do 
115f9 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67  {.        MemPag
115fa 65 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 20 20  e *pFreePg;.    
115fb 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74      rc = allocat
115fc 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20  eBtreePage(pBt, 
115fd 26 70 46 72 65 65 50 67 2c 20 26 69 46 72 65 65  &pFreePg, &iFree
115fe 50 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20  Pg, 0, 0);.     
115ff 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
11600 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
11601 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4c    releasePage(pL
11602 61 73 74 50 67 29 3b 0a 20 20 20 20 20 20 20 20  astPg);.        
11603 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
11604 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72       }.        r
11605 65 6c 65 61 73 65 50 61 67 65 28 70 46 72 65 65  eleasePage(pFree
11606 50 67 29 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c  Pg);.      }whil
11607 65 28 20 6e 46 69 6e 21 3d 30 20 26 26 20 69 46  e( nFin!=0 && iF
11608 72 65 65 50 67 3e 6e 46 69 6e 20 29 3b 0a 20 20  reePg>nFin );.  
11609 20 20 20 20 61 73 73 65 72 74 28 20 69 46 72 65      assert( iFre
1160a 65 50 67 3c 69 4c 61 73 74 50 67 20 29 3b 0a 20  ePg<iLastPg );. 
1160b 20 20 20 20 20 0a 20 20 20 20 20 20 72 63 20 3d       .      rc =
1160c 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
1160d 74 65 28 70 4c 61 73 74 50 67 2d 3e 70 44 62 50  te(pLastPg->pDbP
1160e 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  age);.      if( 
1160f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
11610 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 65  .        rc = re
11611 6c 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c 20  locatePage(pBt, 
11612 70 4c 61 73 74 50 67 2c 20 65 54 79 70 65 2c 20  pLastPg, eType, 
11613 69 50 74 72 50 61 67 65 2c 20 69 46 72 65 65 50  iPtrPage, iFreeP
11614 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  g);.      }.    
11615 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4c    releasePage(pL
11616 61 73 74 50 67 29 3b 0a 20 20 20 20 20 20 69 66  astPg);.      if
11617 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
11618 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
11619 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20  n rc;.      }.  
1161a 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 42 74 2d 3e    }.  }..  pBt->
1161b 6e 54 72 75 6e 63 20 3d 20 69 4c 61 73 74 50 67  nTrunc = iLastPg
1161c 20 2d 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 70   - 1;.  while( p
1161d 42 74 2d 3e 6e 54 72 75 6e 63 3d 3d 50 45 4e 44  Bt->nTrunc==PEND
1161e 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
1161f 74 29 7c 7c 50 54 52 4d 41 50 5f 49 53 50 41 47  t)||PTRMAP_ISPAG
11620 45 28 70 42 74 2c 20 70 42 74 2d 3e 6e 54 72 75  E(pBt, pBt->nTru
11621 6e 63 29 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e  nc) ){.    pBt->
11622 6e 54 72 75 6e 63 2d 2d 3b 0a 20 20 7d 0a 20 20  nTrunc--;.  }.  
11623 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
11624 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 77 72 69  ;.}../*.** A wri
11625 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d  te-transaction m
11626 75 73 74 20 62 65 20 6f 70 65 6e 65 64 20 62 65  ust be opened be
11627 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 74 68 69  fore calling thi
11628 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 49  s function..** I
11629 74 20 70 65 72 66 6f 72 6d 73 20 61 20 73 69 6e  t performs a sin
1162a 67 6c 65 20 75 6e 69 74 20 6f 66 20 77 6f 72 6b  gle unit of work
1162b 20 74 6f 77 61 72 64 73 20 61 6e 20 69 6e 63 72   towards an incr
1162c 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 2e 0a  emental vacuum..
1162d 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 63  **.** If the inc
1162e 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20  remental vacuum 
1162f 69 73 20 66 69 6e 69 73 68 65 64 20 61 66 74 65  is finished afte
11630 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  r this function 
11631 68 61 73 20 72 75 6e 2c 0a 2a 2a 20 53 51 4c 49  has run,.** SQLI
11632 54 45 5f 44 4f 4e 45 20 69 73 20 72 65 74 75 72  TE_DONE is retur
11633 6e 65 64 2e 20 49 66 20 69 74 20 69 73 20 6e 6f  ned. If it is no
11634 74 20 66 69 6e 69 73 68 65 64 2c 20 62 75 74 20  t finished, but 
11635 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64  no error occured
11636 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69  ,.** SQLITE_OK i
11637 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65  s returned. Othe
11638 72 77 69 73 65 20 61 6e 20 53 51 4c 69 74 65 20  rwise an SQLite 
11639 65 72 72 6f 72 20 63 6f 64 65 2e 20 0a 2a 2f 0a  error code. .*/.
1163a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1163b 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  nt sqlite3BtreeI
1163c 6e 63 72 56 61 63 75 75 6d 28 42 74 72 65 65 20  ncrVacuum(Btree 
1163d 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  *p){.  int rc;. 
1163e 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
1163f 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69   p->pBt;..  sqli
11640 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
11641 3b 0a 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d  ;.  pBt->db = p-
11642 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70  >db;.  assert( p
11643 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f  Bt->inTransactio
11644 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 26  n==TRANS_WRITE &
11645 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52  & p->inTrans==TR
11646 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 69  ANS_WRITE );.  i
11647 66 28 20 21 70 42 74 2d 3e 61 75 74 6f 56 61 63  f( !pBt->autoVac
11648 75 75 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  uum ){.    rc = 
11649 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d  SQLITE_DONE;.  }
1164a 65 6c 73 65 7b 0a 20 20 20 20 69 6e 76 61 6c 69  else{.    invali
1164b 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43  dateAllOverflowC
1164c 61 63 68 65 28 70 42 74 29 3b 0a 20 20 20 20 72  ache(pBt);.    r
1164d 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74  c = incrVacuumSt
1164e 65 70 28 70 42 74 2c 20 30 29 3b 0a 20 20 7d 0a  ep(pBt, 0);.  }.
1164f 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
11650 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
11651 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
11652 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
11653 6c 6c 65 64 20 70 72 69 6f 72 20 74 6f 20 73 71  lled prior to sq
11654 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74  lite3PagerCommit
11655 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74   when a transact
11656 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f 6d 6d 69 74  ion.** is commit
11657 65 64 20 66 6f 72 20 61 6e 20 61 75 74 6f 2d 76  ed for an auto-v
11658 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 2e 0a  acuum database..
11659 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f  **.** If SQLITE_
1165a 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20  OK is returned, 
1165b 74 68 65 6e 20 2a 70 6e 54 72 75 6e 63 20 69 73  then *pnTrunc is
1165c 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62   set to the numb
1165d 65 72 20 6f 66 20 70 61 67 65 73 0a 2a 2a 20 74  er of pages.** t
1165e 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1165f 20 73 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63   should be trunc
11660 61 74 65 64 20 74 6f 20 64 75 72 69 6e 67 20 74  ated to during t
11661 68 65 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 73  he commit proces
11662 73 2e 20 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 20  s. .** i.e. the 
11663 64 61 74 61 62 61 73 65 20 68 61 73 20 62 65 65  database has bee
11664 6e 20 72 65 6f 72 67 61 6e 69 7a 65 64 20 73 6f  n reorganized so
11665 20 74 68 61 74 20 6f 6e 6c 79 20 74 68 65 20 66   that only the f
11666 69 72 73 74 20 2a 70 6e 54 72 75 6e 63 0a 2a 2a  irst *pnTrunc.**
11667 20 70 61 67 65 73 20 61 72 65 20 69 6e 20 75 73   pages are in us
11668 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
11669 20 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69   autoVacuumCommi
1166a 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c  t(BtShared *pBt,
1166b 20 50 67 6e 6f 20 2a 70 6e 54 72 75 6e 63 29 7b   Pgno *pnTrunc){
1166c 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
1166d 54 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20 2a  TE_OK;.  Pager *
1166e 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50  pPager = pBt->pP
1166f 61 67 65 72 3b 0a 23 69 66 6e 64 65 66 20 4e 44  ager;.#ifndef ND
11670 45 42 55 47 0a 20 20 69 6e 74 20 6e 52 65 66 20  EBUG.  int nRef 
11671 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65  = sqlite3PagerRe
11672 66 63 6f 75 6e 74 28 70 50 61 67 65 72 29 3b 0a  fcount(pPager);.
11673 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74  #endif..  assert
11674 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
11675 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
11676 20 29 3b 0a 20 20 69 6e 76 61 6c 69 64 61 74 65   );.  invalidate
11677 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65  AllOverflowCache
11678 28 70 42 74 29 3b 0a 20 20 61 73 73 65 72 74 28  (pBt);.  assert(
11679 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29  pBt->autoVacuum)
1167a 3b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 69 6e  ;.  if( !pBt->in
1167b 63 72 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20  crVacuum ){.    
1167c 50 67 6e 6f 20 6e 46 69 6e 20 3d 20 30 3b 0a 0a  Pgno nFin = 0;..
1167d 20 20 20 20 69 66 28 20 70 42 74 2d 3e 6e 54 72      if( pBt->nTr
1167e 75 6e 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  unc==0 ){.      
1167f 50 67 6e 6f 20 6e 46 72 65 65 3b 0a 20 20 20 20  Pgno nFree;.    
11680 20 20 50 67 6e 6f 20 6e 50 74 72 6d 61 70 3b 0a    Pgno nPtrmap;.
11681 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20        const int 
11682 70 67 73 7a 20 3d 20 70 42 74 2d 3e 70 61 67 65  pgsz = pBt->page
11683 53 69 7a 65 3b 0a 20 20 20 20 20 20 50 67 6e 6f  Size;.      Pgno
11684 20 6e 4f 72 69 67 20 3d 20 73 71 6c 69 74 65 33   nOrig = sqlite3
11685 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  PagerPagecount(p
11686 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 0a 20 20  Bt->pPager);..  
11687 20 20 20 20 69 66 28 20 50 54 52 4d 41 50 5f 49      if( PTRMAP_I
11688 53 50 41 47 45 28 70 42 74 2c 20 6e 4f 72 69 67  SPAGE(pBt, nOrig
11689 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  ) ){.        ret
1168a 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
1168b 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d  PT_BKPT;.      }
1168c 0a 20 20 20 20 20 20 69 66 28 20 6e 4f 72 69 67  .      if( nOrig
1168d 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  ==PENDING_BYTE_P
1168e 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20  AGE(pBt) ){.    
1168f 20 20 20 20 6e 4f 72 69 67 2d 2d 3b 0a 20 20 20      nOrig--;.   
11690 20 20 20 7d 0a 20 20 20 20 20 20 6e 46 72 65 65     }.      nFree
11691 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 42 74   = get4byte(&pBt
11692 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  ->pPage1->aData[
11693 33 36 5d 29 3b 0a 20 20 20 20 20 20 6e 50 74 72  36]);.      nPtr
11694 6d 61 70 20 3d 20 28 6e 46 72 65 65 2d 6e 4f 72  map = (nFree-nOr
11695 69 67 2b 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f  ig+PTRMAP_PAGENO
11696 28 70 42 74 2c 20 6e 4f 72 69 67 29 2b 70 67 73  (pBt, nOrig)+pgs
11697 7a 2f 35 29 2f 28 70 67 73 7a 2f 35 29 3b 0a 20  z/5)/(pgsz/5);. 
11698 20 20 20 20 20 6e 46 69 6e 20 3d 20 6e 4f 72 69       nFin = nOri
11699 67 20 2d 20 6e 46 72 65 65 20 2d 20 6e 50 74 72  g - nFree - nPtr
1169a 6d 61 70 3b 0a 20 20 20 20 20 20 69 66 28 20 6e  map;.      if( n
1169b 4f 72 69 67 3e 50 45 4e 44 49 4e 47 5f 42 59 54  Orig>PENDING_BYT
1169c 45 5f 50 41 47 45 28 70 42 74 29 20 26 26 20 6e  E_PAGE(pBt) && n
1169d 46 69 6e 3c 3d 50 45 4e 44 49 4e 47 5f 42 59 54  Fin<=PENDING_BYT
1169e 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20  E_PAGE(pBt) ){. 
1169f 20 20 20 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20         nFin--;. 
116a0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 77 68 69       }.      whi
116a1 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47  le( PTRMAP_ISPAG
116a2 45 28 70 42 74 2c 20 6e 46 69 6e 29 20 7c 7c 20  E(pBt, nFin) || 
116a3 6e 46 69 6e 3d 3d 50 45 4e 44 49 4e 47 5f 42 59  nFin==PENDING_BY
116a4 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a  TE_PAGE(pBt) ){.
116a5 20 20 20 20 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a          nFin--;.
116a6 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
116a7 20 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51     while( rc==SQ
116a8 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
116a9 20 72 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d   rc = incrVacuum
116aa 53 74 65 70 28 70 42 74 2c 20 6e 46 69 6e 29 3b  Step(pBt, nFin);
116ab 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
116ac 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29  c==SQLITE_DONE )
116ad 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 6e  {.      assert(n
116ae 46 69 6e 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 6e  Fin==0 || pBt->n
116af 54 72 75 6e 63 3d 3d 30 20 7c 7c 20 6e 46 69 6e  Trunc==0 || nFin
116b0 3c 3d 70 42 74 2d 3e 6e 54 72 75 6e 63 29 3b 0a  <=pBt->nTrunc);.
116b1 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
116b2 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 69 66 28 20  E_OK;.      if( 
116b3 70 42 74 2d 3e 6e 54 72 75 6e 63 20 26 26 20 6e  pBt->nTrunc && n
116b4 46 69 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 72  Fin ){.        r
116b5 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
116b6 57 72 69 74 65 28 70 42 74 2d 3e 70 50 61 67 65  Write(pBt->pPage
116b7 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  1->pDbPage);.   
116b8 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70       put4byte(&p
116b9 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74  Bt->pPage1->aDat
116ba 61 5b 33 32 5d 2c 20 30 29 3b 0a 20 20 20 20 20  a[32], 0);.     
116bb 20 20 20 70 75 74 34 62 79 74 65 28 26 70 42 74     put4byte(&pBt
116bc 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  ->pPage1->aData[
116bd 33 36 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  36], 0);.       
116be 20 70 42 74 2d 3e 6e 54 72 75 6e 63 20 3d 20 6e   pBt->nTrunc = n
116bf 46 69 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  Fin;.      }.   
116c0 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53   }.    if( rc!=S
116c1 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
116c2 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f    sqlite3PagerRo
116c3 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a  llback(pPager);.
116c4 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28      }.  }..  if(
116c5 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
116c6 7b 0a 20 20 20 20 2a 70 6e 54 72 75 6e 63 20 3d  {.    *pnTrunc =
116c7 20 70 42 74 2d 3e 6e 54 72 75 6e 63 3b 0a 20 20   pBt->nTrunc;.  
116c8 20 20 70 42 74 2d 3e 6e 54 72 75 6e 63 20 3d 20    pBt->nTrunc = 
116c9 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  0;.  }.  assert(
116ca 20 6e 52 65 66 3d 3d 73 71 6c 69 74 65 33 50 61   nRef==sqlite3Pa
116cb 67 65 72 52 65 66 63 6f 75 6e 74 28 70 50 61 67  gerRefcount(pPag
116cc 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  er) );.  return 
116cd 72 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66 0a 0a 2f  rc;.}..#endif../
116ce 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
116cf 65 20 64 6f 65 73 20 74 68 65 20 66 69 72 73 74  e does the first
116d0 20 70 68 61 73 65 20 6f 66 20 61 20 74 77 6f 2d   phase of a two-
116d1 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54  phase commit.  T
116d2 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 63  his routine.** c
116d3 61 75 73 65 73 20 61 20 72 6f 6c 6c 62 61 63 6b  auses a rollback
116d4 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 62 65 20 63   journal to be c
116d5 72 65 61 74 65 64 20 28 69 66 20 69 74 20 64 6f  reated (if it do
116d6 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65  es not already e
116d7 78 69 73 74 29 0a 2a 2a 20 61 6e 64 20 70 6f 70  xist).** and pop
116d8 75 6c 61 74 65 64 20 77 69 74 68 20 65 6e 6f 75  ulated with enou
116d9 67 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73  gh information s
116da 6f 20 74 68 61 74 20 69 66 20 61 20 70 6f 77 65  o that if a powe
116db 72 20 6c 6f 73 73 20 6f 63 63 75 72 73 0a 2a 2a  r loss occurs.**
116dc 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 61   the database ca
116dd 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 74 6f  n be restored to
116de 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 74   its original st
116df 61 74 65 20 62 79 20 70 6c 61 79 69 6e 67 20 62  ate by playing b
116e0 61 63 6b 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e  ack.** the journ
116e1 61 6c 2e 20 20 54 68 65 6e 20 74 68 65 20 63 6f  al.  Then the co
116e2 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 6a 6f  ntents of the jo
116e3 75 72 6e 61 6c 20 61 72 65 20 66 6c 75 73 68 65  urnal are flushe
116e4 64 20 6f 75 74 20 74 6f 0a 2a 2a 20 74 68 65 20  d out to.** the 
116e5 64 69 73 6b 2e 20 20 41 66 74 65 72 20 74 68 65  disk.  After the
116e6 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 61 66 65   journal is safe
116e7 6c 79 20 6f 6e 20 6f 78 69 64 65 2c 20 74 68 65  ly on oxide, the
116e8 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0a   changes to the.
116e9 2a 2a 20 64 61 74 61 62 61 73 65 20 61 72 65 20  ** database are 
116ea 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65  written into the
116eb 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
116ec 6e 64 20 66 6c 75 73 68 65 64 20 74 6f 20 6f 78  nd flushed to ox
116ed 69 64 65 2e 0a 2a 2a 20 41 74 20 74 68 65 20 65  ide..** At the e
116ee 6e 64 20 6f 66 20 74 68 69 73 20 63 61 6c 6c 2c  nd of this call,
116ef 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
116f0 75 72 6e 61 6c 20 73 74 69 6c 6c 20 65 78 69 73  urnal still exis
116f1 74 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 69 73  ts on the.** dis
116f2 6b 20 61 6e 64 20 77 65 20 61 72 65 20 73 74 69  k and we are sti
116f3 6c 6c 20 68 6f 6c 64 69 6e 67 20 61 6c 6c 20 6c  ll holding all l
116f4 6f 63 6b 73 2c 20 73 6f 20 74 68 65 20 74 72 61  ocks, so the tra
116f5 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 6e 6f 74  nsaction has not
116f6 0a 2a 2a 20 63 6f 6d 6d 69 74 74 65 64 2e 20 20  .** committed.  
116f7 53 65 65 20 73 71 6c 69 74 65 33 42 74 72 65 65  See sqlite3Btree
116f8 43 6f 6d 6d 69 74 28 29 20 66 6f 72 20 74 68 65  Commit() for the
116f9 20 73 65 63 6f 6e 64 20 70 68 61 73 65 20 6f 66   second phase of
116fa 20 74 68 65 0a 2a 2a 20 63 6f 6d 6d 69 74 20 70   the.** commit p
116fb 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  rocess..**.** Th
116fc 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d  is call is a no-
116fd 6f 70 20 69 66 20 6e 6f 20 77 72 69 74 65 2d 74  op if no write-t
116fe 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 75  ransaction is cu
116ff 72 72 65 6e 74 6c 79 20 61 63 74 69 76 65 20 6f  rrently active o
11700 6e 20 70 42 74 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68  n pBt..**.** Oth
11701 65 72 77 69 73 65 2c 20 73 79 6e 63 20 74 68 65  erwise, sync the
11702 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66   database file f
11703 6f 72 20 74 68 65 20 62 74 72 65 65 20 70 42 74  or the btree pBt
11704 2e 20 7a 4d 61 73 74 65 72 20 70 6f 69 6e 74 73  . zMaster points
11705 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20   to.** the name 
11706 6f 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72  of a master jour
11707 6e 61 6c 20 66 69 6c 65 20 74 68 61 74 20 73 68  nal file that sh
11708 6f 75 6c 64 20 62 65 20 77 72 69 74 74 65 6e 20  ould be written 
11709 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 69 6e 64 69  into the.** indi
1170a 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66  vidual journal f
1170b 69 6c 65 2c 20 6f 72 20 69 73 20 4e 55 4c 4c 2c  ile, or is NULL,
1170c 20 69 6e 64 69 63 61 74 69 6e 67 20 6e 6f 20 6d   indicating no m
1170d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
1170e 6c 65 20 0a 2a 2a 20 28 73 69 6e 67 6c 65 20 64  le .** (single d
1170f 61 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 74  atabase transact
11710 69 6f 6e 29 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  ion)..**.** When
11711 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c   this is called,
11712 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
11713 6e 61 6c 20 73 68 6f 75 6c 64 20 61 6c 72 65 61  nal should alrea
11714 64 79 20 68 61 76 65 20 62 65 65 6e 0a 2a 2a 20  dy have been.** 
11715 63 72 65 61 74 65 64 2c 20 70 6f 70 75 6c 61 74  created, populat
11716 65 64 20 77 69 74 68 20 74 68 69 73 20 6a 6f 75  ed with this jou
11717 72 6e 61 6c 20 70 6f 69 6e 74 65 72 20 61 6e 64  rnal pointer and
11718 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e   synced to disk.
11719 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 74 68 69 73  .**.** Once this
1171a 20 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20   is routine has 
1171b 72 65 74 75 72 6e 65 64 2c 20 74 68 65 20 6f 6e  returned, the on
1171c 6c 79 20 74 68 69 6e 67 20 72 65 71 75 69 72 65  ly thing require
1171d 64 20 74 6f 20 63 6f 6d 6d 69 74 0a 2a 2a 20 74  d to commit.** t
1171e 68 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63  he write-transac
1171f 74 69 6f 6e 20 66 6f 72 20 74 68 69 73 20 64 61  tion for this da
11720 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 74  tabase file is t
11721 6f 20 64 65 6c 65 74 65 20 74 68 65 20 6a 6f 75  o delete the jou
11722 72 6e 61 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rnal..*/.SQLITE_
11723 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
11724 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68  te3BtreeCommitPh
11725 61 73 65 4f 6e 65 28 42 74 72 65 65 20 2a 70 2c  aseOne(Btree *p,
11726 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61   const char *zMa
11727 73 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20  ster){.  int rc 
11728 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
11729 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54  f( p->inTrans==T
1172a 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20  RANS_WRITE ){.  
1172b 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
1172c 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 20 20 50 67  = p->pBt;.    Pg
1172d 6e 6f 20 6e 54 72 75 6e 63 20 3d 20 30 3b 0a 20  no nTrunc = 0;. 
1172e 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45     sqlite3BtreeE
1172f 6e 74 65 72 28 70 29 3b 0a 20 20 20 20 70 42 74  nter(p);.    pBt
11730 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 23 69  ->db = p->db;.#i
11731 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
11732 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
11733 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61   if( pBt->autoVa
11734 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 72 63  cuum ){.      rc
11735 20 3d 20 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d   = autoVacuumCom
11736 6d 69 74 28 70 42 74 2c 20 26 6e 54 72 75 6e 63  mit(pBt, &nTrunc
11737 29 3b 20 0a 20 20 20 20 20 20 69 66 28 20 72 63  ); .      if( rc
11738 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
11739 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74         sqlite3Bt
1173a 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20  reeLeave(p);.   
1173b 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1173c 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65        }.    }.#e
1173d 6e 64 69 66 0a 20 20 20 20 72 63 20 3d 20 73 71  ndif.    rc = sq
1173e 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74  lite3PagerCommit
1173f 50 68 61 73 65 4f 6e 65 28 70 42 74 2d 3e 70 50  PhaseOne(pBt->pP
11740 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 2c 20 6e  ager, zMaster, n
11741 54 72 75 6e 63 2c 20 30 29 3b 0a 20 20 20 20 73  Trunc, 0);.    s
11742 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
11743 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  (p);.  }.  retur
11744 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  n rc;.}../*.** C
11745 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73 61  ommit the transa
11746 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20  ction currently 
11747 69 6e 20 70 72 6f 67 72 65 73 73 2e 0a 2a 2a 0a  in progress..**.
11748 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
11749 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 73  implements the s
1174a 65 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20 61  econd phase of a
1174b 20 32 2d 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e   2-phase commit.
1174c 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33    The.** sqlite3
1174d 42 74 72 65 65 53 79 6e 63 28 29 20 72 6f 75 74  BtreeSync() rout
1174e 69 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72  ine does the fir
1174f 73 74 20 70 68 61 73 65 20 61 6e 64 20 73 68 6f  st phase and sho
11750 75 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a  uld be invoked.*
11751 2a 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69  * prior to calli
11752 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  ng this routine.
11753 20 20 54 68 65 20 73 71 6c 69 74 65 33 42 74 72    The sqlite3Btr
11754 65 65 53 79 6e 63 28 29 20 72 6f 75 74 69 6e 65  eeSync() routine
11755 20 64 69 64 0a 2a 2a 20 61 6c 6c 20 74 68 65 20   did.** all the 
11756 77 6f 72 6b 20 6f 66 20 77 72 69 74 69 6e 67 20  work of writing 
11757 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20  information out 
11758 74 6f 20 64 69 73 6b 20 61 6e 64 20 66 6c 75 73  to disk and flus
11759 68 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74  hing the.** cont
1175a 65 6e 74 73 20 73 6f 20 74 68 61 74 20 74 68 65  ents so that the
1175b 79 20 61 72 65 20 77 72 69 74 74 65 6e 20 6f 6e  y are written on
1175c 74 6f 20 74 68 65 20 64 69 73 6b 20 70 6c 61 74  to the disk plat
1175d 74 65 72 2e 20 20 41 6c 6c 20 74 68 69 73 0a 2a  ter.  All this.*
1175e 2a 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 6f  * routine has to
1175f 20 64 6f 20 69 73 20 64 65 6c 65 74 65 20 6f 72   do is delete or
11760 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 72 6f   truncate the ro
11761 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 0a 2a  llback journal.*
11762 2a 20 28 77 68 69 63 68 20 63 61 75 73 65 73 20  * (which causes 
11763 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
11764 74 6f 20 63 6f 6d 6d 69 74 29 20 61 6e 64 20 64  to commit) and d
11765 72 6f 70 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a  rop locks..**.**
11766 20 54 68 69 73 20 77 69 6c 6c 20 72 65 6c 65 61   This will relea
11767 73 65 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63  se the write loc
11768 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
11769 65 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72  e file.  If ther
1176a 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69  e.** are no acti
1176b 76 65 20 63 75 72 73 6f 72 73 2c 20 69 74 20 61  ve cursors, it a
1176c 6c 73 6f 20 72 65 6c 65 61 73 65 73 20 74 68 65  lso releases the
1176d 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53   read lock..*/.S
1176e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1176f 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  t sqlite3BtreeCo
11770 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 42 74 72  mmitPhaseTwo(Btr
11771 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72  ee *p){.  BtShar
11772 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
11773 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  ;..  sqlite3Btre
11774 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74  eEnter(p);.  pBt
11775 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20  ->db = p->db;.  
11776 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70  btreeIntegrity(p
11777 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
11778 68 61 6e 64 6c 65 20 68 61 73 20 61 20 77 72 69  handle has a wri
11779 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  te-transaction o
1177a 70 65 6e 2c 20 63 6f 6d 6d 69 74 20 74 68 65 20  pen, commit the 
1177b 73 68 61 72 65 64 2d 62 74 72 65 65 73 20 0a 20  shared-btrees . 
1177c 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20   ** transaction 
1177d 61 6e 64 20 73 65 74 20 74 68 65 20 73 68 61 72  and set the shar
1177e 65 64 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e  ed state to TRAN
1177f 53 5f 52 45 41 44 2e 0a 20 20 2a 2f 0a 20 20 69  S_READ..  */.  i
11780 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54  f( p->inTrans==T
11781 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20  RANS_WRITE ){.  
11782 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 61 73    int rc;.    as
11783 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61  sert( pBt->inTra
11784 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f  nsaction==TRANS_
11785 57 52 49 54 45 20 29 3b 0a 20 20 20 20 61 73 73  WRITE );.    ass
11786 65 72 74 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73  ert( pBt->nTrans
11787 61 63 74 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20  action>0 );.    
11788 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
11789 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28  rCommitPhaseTwo(
1178a 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20  pBt->pPager);.  
1178b 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1178c 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
1178d 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
1178e 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
1178f 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42  rc;.    }.    pB
11790 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
11791 20 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20   = TRANS_READ;. 
11792 20 20 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d     pBt->inStmt =
11793 20 30 3b 0a 20 20 7d 0a 20 20 75 6e 6c 6f 63 6b   0;.  }.  unlock
11794 41 6c 6c 54 61 62 6c 65 73 28 70 29 3b 0a 0a 20  AllTables(p);.. 
11795 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e 64 6c   /* If the handl
11796 65 20 68 61 73 20 61 6e 79 20 6b 69 6e 64 20 6f  e has any kind o
11797 66 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70  f transaction op
11798 65 6e 2c 20 64 65 63 72 65 6d 65 6e 74 20 74 68  en, decrement th
11799 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20  e transaction.  
1179a 2a 2a 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20  ** count of the 
1179b 73 68 61 72 65 64 20 62 74 72 65 65 2e 20 49 66  shared btree. If
1179c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
1179d 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 30   count reaches 0
1179e 2c 20 73 65 74 0a 20 20 2a 2a 20 74 68 65 20 73  , set.  ** the s
1179f 68 61 72 65 64 20 73 74 61 74 65 20 74 6f 20 54  hared state to T
117a0 52 41 4e 53 5f 4e 4f 4e 45 2e 20 54 68 65 20 75  RANS_NONE. The u
117a1 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73  nlockBtreeIfUnus
117a2 65 64 28 29 20 63 61 6c 6c 20 62 65 6c 6f 77 0a  ed() call below.
117a3 20 20 2a 2a 20 77 69 6c 6c 20 75 6e 6c 6f 63 6b    ** will unlock
117a4 20 74 68 65 20 70 61 67 65 72 2e 0a 20 20 2a 2f   the pager..  */
117a5 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e  .  if( p->inTran
117a6 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b  s!=TRANS_NONE ){
117a7 0a 20 20 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73  .    pBt->nTrans
117a8 61 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20 20 69 66  action--;.    if
117a9 28 20 30 3d 3d 70 42 74 2d 3e 6e 54 72 61 6e 73  ( 0==pBt->nTrans
117aa 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20  action ){.      
117ab 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69  pBt->inTransacti
117ac 6f 6e 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b  on = TRANS_NONE;
117ad 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
117ae 20 53 65 74 20 74 68 65 20 68 61 6e 64 6c 65 73   Set the handles
117af 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63   current transac
117b0 74 69 6f 6e 20 73 74 61 74 65 20 74 6f 20 54 52  tion state to TR
117b1 41 4e 53 5f 4e 4f 4e 45 20 61 6e 64 20 75 6e 6c  ANS_NONE and unl
117b2 6f 63 6b 0a 20 20 2a 2a 20 74 68 65 20 70 61 67  ock.  ** the pag
117b3 65 72 20 69 66 20 74 68 69 73 20 63 61 6c 6c 20  er if this call 
117b4 63 6c 6f 73 65 64 20 74 68 65 20 6f 6e 6c 79 20  closed the only 
117b5 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 72  read or write tr
117b6 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a  ansaction..  */.
117b7 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54    p->inTrans = T
117b8 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 75 6e 6c  RANS_NONE;.  unl
117b9 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64  ockBtreeIfUnused
117ba 28 70 42 74 29 3b 0a 0a 20 20 62 74 72 65 65 49  (pBt);..  btreeI
117bb 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 20 20 73  ntegrity(p);.  s
117bc 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
117bd 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  (p);.  return SQ
117be 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
117bf 2a 20 44 6f 20 62 6f 74 68 20 70 68 61 73 65 73  * Do both phases
117c0 20 6f 66 20 61 20 63 6f 6d 6d 69 74 2e 0a 2a 2f   of a commit..*/
117c1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
117c2 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
117c3 43 6f 6d 6d 69 74 28 42 74 72 65 65 20 2a 70 29  Commit(Btree *p)
117c4 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71  {.  int rc;.  sq
117c5 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
117c6 70 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  p);.  rc = sqlit
117c7 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
117c8 73 65 4f 6e 65 28 70 2c 20 30 29 3b 0a 20 20 69  seOne(p, 0);.  i
117c9 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
117ca 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
117cb 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50  ite3BtreeCommitP
117cc 68 61 73 65 54 77 6f 28 70 29 3b 0a 20 20 7d 0a  haseTwo(p);.  }.
117cd 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
117ce 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
117cf 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20   rc;.}..#ifndef 
117d0 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74  NDEBUG./*.** Ret
117d1 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
117d2 66 20 77 72 69 74 65 2d 63 75 72 73 6f 72 73 20  f write-cursors 
117d3 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20 68 61 6e  open on this han
117d4 64 6c 65 2e 20 54 68 69 73 20 69 73 20 66 6f 72  dle. This is for
117d5 20 75 73 65 0a 2a 2a 20 69 6e 20 61 73 73 65 72   use.** in asser
117d6 74 28 29 20 65 78 70 72 65 73 73 69 6f 6e 73 2c  t() expressions,
117d7 20 73 6f 20 69 74 20 69 73 20 6f 6e 6c 79 20 63   so it is only c
117d8 6f 6d 70 69 6c 65 64 20 69 66 20 4e 44 45 42 55  ompiled if NDEBU
117d9 47 20 69 73 20 6e 6f 74 0a 2a 2a 20 64 65 66 69  G is not.** defi
117da 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74  ned..**.** For t
117db 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74  he purposes of t
117dc 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 20 77  his routine, a w
117dd 72 69 74 65 2d 63 75 72 73 6f 72 20 69 73 20 61  rite-cursor is a
117de 6e 79 20 63 75 72 73 6f 72 20 74 68 61 74 0a 2a  ny cursor that.*
117df 2a 20 69 73 20 63 61 70 61 62 6c 65 20 6f 66 20  * is capable of 
117e0 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64  writing to the d
117e1 61 74 61 62 73 65 2e 20 20 54 68 61 74 20 6d 65  atabse.  That me
117e2 61 6e 73 20 74 68 65 20 63 75 72 73 6f 72 20 77  ans the cursor w
117e3 61 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c 79  as.** originally
117e4 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74   opened for writ
117e5 69 6e 67 20 61 6e 64 20 74 68 65 20 63 75 72 73  ing and the curs
117e6 6f 72 20 68 61 73 20 6e 6f 74 20 62 65 20 64 69  or has not be di
117e7 73 61 62 6c 65 64 0a 2a 2a 20 62 79 20 68 61 76  sabled.** by hav
117e8 69 6e 67 20 69 74 73 20 73 74 61 74 65 20 63 68  ing its state ch
117e9 61 6e 67 65 64 20 74 6f 20 43 55 52 53 4f 52 5f  anged to CURSOR_
117ea 46 41 55 4c 54 2e 0a 2a 2f 0a 73 74 61 74 69 63  FAULT..*/.static
117eb 20 69 6e 74 20 63 6f 75 6e 74 57 72 69 74 65 43   int countWriteC
117ec 75 72 73 6f 72 73 28 42 74 53 68 61 72 65 64 20  ursors(BtShared 
117ed 2a 70 42 74 29 7b 0a 20 20 42 74 43 75 72 73 6f  *pBt){.  BtCurso
117ee 72 20 2a 70 43 75 72 3b 0a 20 20 69 6e 74 20 72  r *pCur;.  int r
117ef 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 43 75 72   = 0;.  for(pCur
117f0 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70  =pBt->pCursor; p
117f1 43 75 72 3b 20 70 43 75 72 3d 70 43 75 72 2d 3e  Cur; pCur=pCur->
117f2 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20  pNext){.    if( 
117f3 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 26 26 20  pCur->wrFlag && 
117f4 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55  pCur->eState!=CU
117f5 52 53 4f 52 5f 46 41 55 4c 54 20 29 20 72 2b 2b  RSOR_FAULT ) r++
117f6 3b 20 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  ; .  }.  return 
117f7 72 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  r;.}.#endif../*.
117f8 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
117f9 73 65 74 73 20 74 68 65 20 73 74 61 74 65 20 74  sets the state t
117fa 6f 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 61  o CURSOR_FAULT a
117fb 6e 64 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20  nd the error.** 
117fc 63 6f 64 65 20 74 6f 20 65 72 72 43 6f 64 65 20  code to errCode 
117fd 66 6f 72 20 65 76 65 72 79 20 63 75 72 73 6f 72  for every cursor
117fe 20 6f 6e 20 42 74 53 68 61 72 65 64 20 74 68 61   on BtShared tha
117ff 74 20 70 42 74 72 65 65 0a 2a 2a 20 72 65 66 65  t pBtree.** refe
11800 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 76  rences..**.** Ev
11801 65 72 79 20 63 75 72 73 6f 72 20 69 73 20 74 72  ery cursor is tr
11802 69 70 70 65 64 2c 20 69 6e 63 6c 75 64 69 6e 67  ipped, including
11803 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 62 65   cursors that be
11804 6c 6f 6e 67 0a 2a 2a 20 74 6f 20 6f 74 68 65 72  long.** to other
11805 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
11806 74 69 6f 6e 73 20 74 68 61 74 20 68 61 70 70 65  tions that happe
11807 6e 20 74 6f 20 62 65 20 73 68 61 72 69 6e 67 0a  n to be sharing.
11808 2a 2a 20 74 68 65 20 63 61 63 68 65 20 77 69 74  ** the cache wit
11809 68 20 70 42 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20  h pBtree..**.** 
1180a 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65 74  This routine get
1180b 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20  s called when a 
1180c 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73 2e  rollback occurs.
1180d 0a 2a 2a 20 41 6c 6c 20 63 75 72 73 6f 72 73 20  .** All cursors 
1180e 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 63  using the same c
1180f 61 63 68 65 20 6d 75 73 74 20 62 65 20 74 72 69  ache must be tri
11810 70 70 65 64 0a 2a 2a 20 74 6f 20 70 72 65 76 65  pped.** to preve
11811 6e 74 20 74 68 65 6d 20 66 72 6f 6d 20 74 72 79  nt them from try
11812 69 6e 67 20 74 6f 20 75 73 65 20 74 68 65 20 62  ing to use the b
11813 74 72 65 65 20 61 66 74 65 72 0a 2a 2a 20 74 68  tree after.** th
11814 65 20 72 6f 6c 6c 62 61 63 6b 2e 20 20 54 68 65  e rollback.  The
11815 20 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 20 68 61   rollback may ha
11816 76 65 20 64 65 6c 65 74 65 64 20 74 61 62 6c 65  ve deleted table
11817 73 0a 2a 2a 20 6f 72 20 6d 6f 76 65 64 20 72 6f  s.** or moved ro
11818 6f 74 20 70 61 67 65 73 2c 20 73 6f 20 69 74 20  ot pages, so it 
11819 69 73 20 6e 6f 74 20 73 75 66 66 69 63 69 65 6e  is not sufficien
1181a 74 20 74 6f 0a 2a 2a 20 73 61 76 65 20 74 68 65  t to.** save the
1181b 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 63 75   state of the cu
1181c 72 73 6f 72 2e 20 20 54 68 65 20 63 75 72 73 6f  rsor.  The curso
1181d 72 20 6d 75 73 74 20 62 65 0a 2a 2a 20 69 6e 76  r must be.** inv
1181e 61 6c 69 64 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c  alidated..*/.SQL
1181f 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
11820 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 69   sqlite3BtreeTri
11821 70 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 72 65  pAllCursors(Btre
11822 65 20 2a 70 42 74 72 65 65 2c 20 69 6e 74 20 65  e *pBtree, int e
11823 72 72 43 6f 64 65 29 7b 0a 20 20 42 74 43 75 72  rrCode){.  BtCur
11824 73 6f 72 20 2a 70 3b 0a 20 20 73 71 6c 69 74 65  sor *p;.  sqlite
11825 33 42 74 72 65 65 45 6e 74 65 72 28 70 42 74 72  3BtreeEnter(pBtr
11826 65 65 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74  ee);.  for(p=pBt
11827 72 65 65 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f  ree->pBt->pCurso
11828 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74  r; p; p=p->pNext
11829 29 7b 0a 20 20 20 20 63 6c 65 61 72 43 75 72 73  ){.    clearCurs
1182a 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 3b 0a 20  orPosition(p);. 
1182b 20 20 20 70 2d 3e 65 53 74 61 74 65 20 3d 20 43     p->eState = C
1182c 55 52 53 4f 52 5f 46 41 55 4c 54 3b 0a 20 20 20  URSOR_FAULT;.   
1182d 20 70 2d 3e 73 6b 69 70 20 3d 20 65 72 72 43 6f   p->skip = errCo
1182e 64 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  de;.  }.  sqlite
1182f 33 42 74 72 65 65 4c 65 61 76 65 28 70 42 74 72  3BtreeLeave(pBtr
11830 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f  ee);.}../*.** Ro
11831 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73  llback the trans
11832 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65  action in progre
11833 73 73 2e 20 20 41 6c 6c 20 63 75 72 73 6f 72 73  ss.  All cursors
11834 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 69 6e 76 61   will be.** inva
11835 6c 69 64 65 64 20 62 79 20 74 68 69 73 20 6f 70  lided by this op
11836 65 72 61 74 69 6f 6e 2e 20 20 41 6e 79 20 61 74  eration.  Any at
11837 74 65 6d 70 74 20 74 6f 20 75 73 65 20 61 20 63  tempt to use a c
11838 75 72 73 6f 72 0a 2a 2a 20 74 68 61 74 20 77 61  ursor.** that wa
11839 73 20 6f 70 65 6e 20 61 74 20 74 68 65 20 62 65  s open at the be
1183a 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 69 73 20  ginning of this 
1183b 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c 6c 20 72  operation will r
1183c 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 61 6e 20 65  esult.** in an e
1183d 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  rror..**.** This
1183e 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20 74 68   will release th
1183f 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e 20  e write lock on 
11840 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
11841 65 2e 20 20 49 66 20 74 68 65 72 65 0a 2a 2a 20  e.  If there.** 
11842 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 63 75  are no active cu
11843 72 73 6f 72 73 2c 20 69 74 20 61 6c 73 6f 20 72  rsors, it also r
11844 65 6c 65 61 73 65 73 20 74 68 65 20 72 65 61 64  eleases the read
11845 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45   lock..*/.SQLITE
11846 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
11847 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63  ite3BtreeRollbac
11848 6b 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69  k(Btree *p){.  i
11849 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65  nt rc;.  BtShare
1184a 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
1184b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
1184c 65 31 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74  e1;..  sqlite3Bt
1184d 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70  reeEnter(p);.  p
1184e 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  Bt->db = p->db;.
1184f 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75    rc = saveAllCu
11850 72 73 6f 72 73 28 70 42 74 2c 20 30 2c 20 30 29  rsors(pBt, 0, 0)
11851 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
11852 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
11853 48 45 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  HE.  if( rc!=SQL
11854 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a  ITE_OK ){.    /*
11855 20 54 68 69 73 20 69 73 20 61 20 68 6f 72 72 69   This is a horri
11856 62 6c 65 20 73 69 74 75 61 74 69 6f 6e 2e 20 41  ble situation. A
11857 6e 20 49 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29  n IO or malloc()
11858 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20 77   error occured w
11859 68 69 6c 73 74 0a 20 20 20 20 2a 2a 20 74 72 79  hilst.    ** try
1185a 69 6e 67 20 74 6f 20 73 61 76 65 20 63 75 72 73  ing to save curs
1185b 6f 72 20 70 6f 73 69 74 69 6f 6e 73 2e 20 49 66  or positions. If
1185c 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f   this is an auto
1185d 6d 61 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20 28  matic rollback (
1185e 61 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 65  as.    ** the re
1185f 73 75 6c 74 20 6f 66 20 61 20 63 6f 6e 73 74 72  sult of a constr
11860 61 69 6e 74 2c 20 6d 61 6c 6c 6f 63 28 29 20 66  aint, malloc() f
11861 61 69 6c 75 72 65 20 6f 72 20 49 4f 20 65 72 72  ailure or IO err
11862 6f 72 29 20 74 68 65 6e 20 0a 20 20 20 20 2a 2a  or) then .    **
11863 20 74 68 65 20 63 61 63 68 65 20 6d 61 79 20 62   the cache may b
11864 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 69 6e 63  e internally inc
11865 6f 6e 73 69 73 74 65 6e 74 20 28 6e 6f 74 20 63  onsistent (not c
11866 6f 6e 74 61 69 6e 20 76 61 6c 69 64 20 74 72 65  ontain valid tre
11867 65 73 29 20 73 6f 0a 20 20 20 20 2a 2a 20 77 65  es) so.    ** we
11868 20 63 61 6e 6e 6f 74 20 73 69 6d 70 6c 79 20 72   cannot simply r
11869 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20  eturn the error 
1186a 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 49  to the caller. I
1186b 6e 73 74 65 61 64 2c 20 61 62 6f 72 74 20 0a 20  nstead, abort . 
1186c 20 20 20 2a 2a 20 61 6c 6c 20 71 75 65 72 69 65     ** all querie
1186d 73 20 74 68 61 74 20 6d 61 79 20 62 65 20 75 73  s that may be us
1186e 69 6e 67 20 61 6e 79 20 6f 66 20 74 68 65 20 63  ing any of the c
1186f 75 72 73 6f 72 73 20 74 68 61 74 20 66 61 69 6c  ursors that fail
11870 65 64 20 74 6f 20 73 61 76 65 2e 0a 20 20 20 20  ed to save..    
11871 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  */.    sqlite3Bt
11872 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72  reeTripAllCursor
11873 73 28 70 2c 20 72 63 29 3b 0a 20 20 7d 0a 23 65  s(p, rc);.  }.#e
11874 6e 64 69 66 0a 20 20 62 74 72 65 65 49 6e 74 65  ndif.  btreeInte
11875 67 72 69 74 79 28 70 29 3b 0a 20 20 75 6e 6c 6f  grity(p);.  unlo
11876 63 6b 41 6c 6c 54 61 62 6c 65 73 28 70 29 3b 0a  ckAllTables(p);.
11877 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e  .  if( p->inTran
11878 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29  s==TRANS_WRITE )
11879 7b 0a 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 0a  {.    int rc2;..
1187a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1187b 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
1187c 20 20 20 70 42 74 2d 3e 6e 54 72 75 6e 63 20 3d     pBt->nTrunc =
1187d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20   0;.#endif..    
1187e 61 73 73 65 72 74 28 20 54 52 41 4e 53 5f 57 52  assert( TRANS_WR
1187f 49 54 45 3d 3d 70 42 74 2d 3e 69 6e 54 72 61 6e  ITE==pBt->inTran
11880 73 61 63 74 69 6f 6e 20 29 3b 0a 20 20 20 20 72  saction );.    r
11881 63 32 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  c2 = sqlite3Page
11882 72 52 6f 6c 6c 62 61 63 6b 28 70 42 74 2d 3e 70  rRollback(pBt->p
11883 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20  Pager);.    if( 
11884 72 63 32 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  rc2!=SQLITE_OK )
11885 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 63 32  {.      rc = rc2
11886 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
11887 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d 61 79  The rollback may
11888 20 68 61 76 65 20 64 65 73 74 72 6f 79 65 64 20   have destroyed 
11889 74 68 65 20 70 50 61 67 65 31 2d 3e 61 44 61 74  the pPage1->aDat
1188a 61 20 76 61 6c 75 65 2e 20 20 53 6f 0a 20 20 20  a value.  So.   
1188b 20 2a 2a 20 63 61 6c 6c 20 73 71 6c 69 74 65 33   ** call sqlite3
1188c 42 74 72 65 65 47 65 74 50 61 67 65 28 29 20 6f  BtreeGetPage() o
1188d 6e 20 70 61 67 65 20 31 20 61 67 61 69 6e 20 74  n page 1 again t
1188e 6f 20 6d 61 6b 65 0a 20 20 20 20 2a 2a 20 73 75  o make.    ** su
1188f 72 65 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61  re pPage1->aData
11890 20 69 73 20 73 65 74 20 63 6f 72 72 65 63 74 6c   is set correctl
11891 79 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71  y. */.    if( sq
11892 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
11893 65 28 70 42 74 2c 20 31 2c 20 26 70 50 61 67 65  e(pBt, 1, &pPage
11894 31 2c 20 30 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b  1, 0)==SQLITE_OK
11895 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73   ){.      releas
11896 65 50 61 67 65 28 70 50 61 67 65 31 29 3b 0a 20  ePage(pPage1);. 
11897 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
11898 20 63 6f 75 6e 74 57 72 69 74 65 43 75 72 73 6f   countWriteCurso
11899 72 73 28 70 42 74 29 3d 3d 30 20 29 3b 0a 20 20  rs(pBt)==0 );.  
1189a 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63    pBt->inTransac
1189b 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52 45 41  tion = TRANS_REA
1189c 44 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d  D;.  }..  if( p-
1189d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f  >inTrans!=TRANS_
1189e 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 61 73 73 65  NONE ){.    asse
1189f 72 74 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61  rt( pBt->nTransa
118a0 63 74 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 70  ction>0 );.    p
118a1 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e  Bt->nTransaction
118a2 2d 2d 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d 70  --;.    if( 0==p
118a3 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e  Bt->nTransaction
118a4 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69   ){.      pBt->i
118a5 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54  nTransaction = T
118a6 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 7d  RANS_NONE;.    }
118a7 0a 20 20 7d 0a 0a 20 20 70 2d 3e 69 6e 54 72 61  .  }..  p->inTra
118a8 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b  ns = TRANS_NONE;
118a9 0a 20 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d  .  pBt->inStmt =
118aa 20 30 3b 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65   0;.  unlockBtre
118ab 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a  eIfUnused(pBt);.
118ac 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74  .  btreeIntegrit
118ad 79 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  y(p);.  sqlite3B
118ae 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
118af 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
118b0 0a 2a 2a 20 53 74 61 72 74 20 61 20 73 74 61 74  .** Start a stat
118b1 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61 63  ement subtransac
118b2 74 69 6f 6e 2e 20 20 54 68 65 20 73 75 62 74 72  tion.  The subtr
118b3 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 0a 2a 2a  ansaction can.**
118b4 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64 20 62   can be rolled b
118b5 61 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c  ack independentl
118b6 79 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 74 72  y of the main tr
118b7 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 59 6f  ansaction..** Yo
118b8 75 20 6d 75 73 74 20 73 74 61 72 74 20 61 20 74  u must start a t
118b9 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 66 6f 72  ransaction befor
118ba 65 20 73 74 61 72 74 69 6e 67 20 61 20 73 75 62  e starting a sub
118bb 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20  transaction..** 
118bc 54 68 65 20 73 75 62 74 72 61 6e 73 61 63 74 69  The subtransacti
118bd 6f 6e 20 69 73 20 65 6e 64 65 64 20 61 75 74 6f  on is ended auto
118be 6d 61 74 69 63 61 6c 6c 79 20 69 66 20 74 68 65  matically if the
118bf 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 74 69 6f   main transactio
118c0 6e 0a 2a 2a 20 63 6f 6d 6d 69 74 73 20 6f 72 20  n.** commits or 
118c1 72 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2a 0a 2a  rolls back..**.*
118c2 2a 20 4f 6e 6c 79 20 6f 6e 65 20 73 75 62 74 72  * Only one subtr
118c3 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 62 65  ansaction may be
118c4 20 61 63 74 69 76 65 20 61 74 20 61 20 74 69 6d   active at a tim
118c5 65 2e 20 20 49 74 20 69 73 20 61 6e 20 65 72 72  e.  It is an err
118c6 6f 72 20 74 6f 20 74 72 79 0a 2a 2a 20 74 6f 20  or to try.** to 
118c7 73 74 61 72 74 20 61 20 6e 65 77 20 73 75 62 74  start a new subt
118c8 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 61 6e  ransaction if an
118c9 6f 74 68 65 72 20 73 75 62 74 72 61 6e 73 61 63  other subtransac
118ca 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 79 20  tion is already 
118cb 61 63 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 53 74  active..**.** St
118cc 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73  atement subtrans
118cd 61 63 74 69 6f 6e 73 20 61 72 65 20 75 73 65 64  actions are used
118ce 20 61 72 6f 75 6e 64 20 69 6e 64 69 76 69 64 75   around individu
118cf 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  al SQL statement
118d0 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 63 6f  s.** that are co
118d1 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20 61  ntained within a
118d2 20 42 45 47 49 4e 2e 2e 2e 43 4f 4d 4d 49 54 20   BEGIN...COMMIT 
118d3 62 6c 6f 63 6b 2e 20 20 49 66 20 61 20 63 6f 6e  block.  If a con
118d4 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72 6f 72  straint.** error
118d5 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 74   occurs within t
118d6 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68  he statement, th
118d7 65 20 65 66 66 65 63 74 20 6f 66 20 74 68 61 74  e effect of that
118d8 20 6f 6e 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a   one statement.*
118d9 2a 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64 20  * can be rolled 
118da 62 61 63 6b 20 77 69 74 68 6f 75 74 20 68 61 76  back without hav
118db 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20  ing to rollback 
118dc 74 68 65 20 65 6e 74 69 72 65 20 74 72 61 6e 73  the entire trans
118dd 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  action..*/.SQLIT
118de 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
118df 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 53  lite3BtreeBeginS
118e0 74 6d 74 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  tmt(Btree *p){. 
118e1 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61   int rc;.  BtSha
118e2 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
118e3 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  t;.  sqlite3Btre
118e4 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74  eEnter(p);.  pBt
118e5 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20  ->db = p->db;.  
118e6 69 66 28 20 28 70 2d 3e 69 6e 54 72 61 6e 73 21  if( (p->inTrans!
118e7 3d 54 52 41 4e 53 5f 57 52 49 54 45 29 20 7c 7c  =TRANS_WRITE) ||
118e8 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 29 7b 0a   pBt->inStmt ){.
118e9 20 20 20 20 72 63 20 3d 20 70 42 74 2d 3e 72 65      rc = pBt->re
118ea 61 64 4f 6e 6c 79 20 3f 20 53 51 4c 49 54 45 5f  adOnly ? SQLITE_
118eb 52 45 41 44 4f 4e 4c 59 20 3a 20 53 51 4c 49 54  READONLY : SQLIT
118ec 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65  E_ERROR;.  }else
118ed 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42  {.    assert( pB
118ee 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
118ef 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b  ==TRANS_WRITE );
118f0 0a 20 20 20 20 72 63 20 3d 20 70 42 74 2d 3e 72  .    rc = pBt->r
118f1 65 61 64 4f 6e 6c 79 20 3f 20 53 51 4c 49 54 45  eadOnly ? SQLITE
118f2 5f 4f 4b 20 3a 20 73 71 6c 69 74 65 33 50 61 67  _OK : sqlite3Pag
118f3 65 72 53 74 6d 74 42 65 67 69 6e 28 70 42 74 2d  erStmtBegin(pBt-
118f4 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 70 42  >pPager);.    pB
118f5 74 2d 3e 69 6e 53 74 6d 74 20 3d 20 31 3b 0a 20  t->inStmt = 1;. 
118f6 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65   }.  sqlite3Btre
118f7 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
118f8 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  urn rc;.}.../*.*
118f9 2a 20 43 6f 6d 6d 69 74 20 74 68 65 20 73 74 61  * Commit the sta
118fa 74 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61 63  tment subtransac
118fb 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 69  tion currently i
118fc 6e 20 70 72 6f 67 72 65 73 73 2e 20 20 49 66 20  n progress.  If 
118fd 6e 6f 0a 2a 2a 20 73 75 62 74 72 61 6e 73 61 63  no.** subtransac
118fe 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2c 20  tion is active, 
118ff 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  this is a no-op.
11900 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
11901 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
11902 72 65 65 43 6f 6d 6d 69 74 53 74 6d 74 28 42 74  reeCommitStmt(Bt
11903 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72  ree *p){.  int r
11904 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  c;.  BtShared *p
11905 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73  Bt = p->pBt;.  s
11906 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
11907 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 62 20 3d  (p);.  pBt->db =
11908 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 42   p->db;.  if( pB
11909 74 2d 3e 69 6e 53 74 6d 74 20 26 26 20 21 70 42  t->inStmt && !pB
1190a 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20  t->readOnly ){. 
1190b 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
1190c 61 67 65 72 53 74 6d 74 43 6f 6d 6d 69 74 28 70  agerStmtCommit(p
1190d 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 7d  Bt->pPager);.  }
1190e 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 53  else{.    rc = S
1190f 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
11910 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d 20 30 3b  pBt->inStmt = 0;
11911 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
11912 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72  eave(p);.  retur
11913 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
11914 6f 6c 6c 62 61 63 6b 20 74 68 65 20 61 63 74 69  ollback the acti
11915 76 65 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62  ve statement sub
11916 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49 66  transaction.  If
11917 20 6e 6f 20 73 75 62 74 72 61 6e 73 61 63 74 69   no subtransacti
11918 6f 6e 0a 2a 2a 20 69 73 20 61 63 74 69 76 65 20  on.** is active 
11919 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
1191a 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 41  a no-op..**.** A
1191b 6c 6c 20 63 75 72 73 6f 72 73 20 77 69 6c 6c 20  ll cursors will 
1191c 62 65 20 69 6e 76 61 6c 69 64 61 74 65 64 20 62  be invalidated b
1191d 79 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e  y this operation
1191e 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 0a 2a  .  Any attempt.*
1191f 2a 20 74 6f 20 75 73 65 20 61 20 63 75 72 73 6f  * to use a curso
11920 72 20 74 68 61 74 20 77 61 73 20 6f 70 65 6e 20  r that was open 
11921 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
11922 20 6f 66 20 74 68 69 73 20 6f 70 65 72 61 74 69   of this operati
11923 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 72 65 73 75 6c  on.** will resul
11924 74 20 69 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a  t in an error..*
11925 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
11926 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
11927 65 52 6f 6c 6c 62 61 63 6b 53 74 6d 74 28 42 74  eRollbackStmt(Bt
11928 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72  ree *p){.  int r
11929 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1192a 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
1192b 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74   p->pBt;.  sqlit
1192c 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
1192d 0a 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e  .  pBt->db = p->
1192e 64 62 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 69  db;.  if( pBt->i
1192f 6e 53 74 6d 74 20 26 26 20 21 70 42 74 2d 3e 72  nStmt && !pBt->r
11930 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 72  eadOnly ){.    r
11931 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
11932 53 74 6d 74 52 6f 6c 6c 62 61 63 6b 28 70 42 74  StmtRollback(pBt
11933 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 61  ->pPager);.    a
11934 73 73 65 72 74 28 20 63 6f 75 6e 74 57 72 69 74  ssert( countWrit
11935 65 43 75 72 73 6f 72 73 28 70 42 74 29 3d 3d 30  eCursors(pBt)==0
11936 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 53   );.    pBt->inS
11937 74 6d 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73  tmt = 0;.  }.  s
11938 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
11939 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
1193a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  ;.}../*.** Creat
1193b 65 20 61 20 6e 65 77 20 63 75 72 73 6f 72 20 66  e a new cursor f
1193c 6f 72 20 74 68 65 20 42 54 72 65 65 20 77 68 6f  or the BTree who
1193d 73 65 20 72 6f 6f 74 20 69 73 20 6f 6e 20 74 68  se root is on th
1193e 65 20 70 61 67 65 0a 2a 2a 20 69 54 61 62 6c 65  e page.** iTable
1193f 2e 20 20 54 68 65 20 61 63 74 20 6f 66 20 61 63  .  The act of ac
11940 71 75 69 72 69 6e 67 20 61 20 63 75 72 73 6f 72  quiring a cursor
11941 20 67 65 74 73 20 61 20 72 65 61 64 20 6c 6f 63   gets a read loc
11942 6b 20 6f 6e 20 0a 2a 2a 20 74 68 65 20 64 61 74  k on .** the dat
11943 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  abase file..**.*
11944 2a 20 49 66 20 77 72 46 6c 61 67 3d 3d 30 2c 20  * If wrFlag==0, 
11945 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20  then the cursor 
11946 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65 64  can only be used
11947 20 66 6f 72 20 72 65 61 64 69 6e 67 2e 0a 2a 2a   for reading..**
11948 20 49 66 20 77 72 46 6c 61 67 3d 3d 31 2c 20 74   If wrFlag==1, t
11949 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 63  hen the cursor c
1194a 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 72  an be used for r
1194b 65 61 64 69 6e 67 20 6f 72 20 66 6f 72 0a 2a 2a  eading or for.**
1194c 20 77 72 69 74 69 6e 67 20 69 66 20 6f 74 68 65   writing if othe
1194d 72 20 63 6f 6e 64 69 74 69 6f 6e 73 20 66 6f 72  r conditions for
1194e 20 77 72 69 74 69 6e 67 20 61 72 65 20 61 6c 73   writing are als
1194f 6f 20 6d 65 74 2e 20 20 54 68 65 73 65 0a 2a 2a  o met.  These.**
11950 20 61 72 65 20 74 68 65 20 63 6f 6e 64 69 74 69   are the conditi
11951 6f 6e 73 20 74 68 61 74 20 6d 75 73 74 20 62 65  ons that must be
11952 20 6d 65 74 20 69 6e 20 6f 72 64 65 72 20 66 6f   met in order fo
11953 72 20 77 72 69 74 69 6e 67 20 74 6f 0a 2a 2a 20  r writing to.** 
11954 62 65 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a  be allowed:.**.*
11955 2a 20 31 3a 20 20 54 68 65 20 63 75 72 73 6f 72  * 1:  The cursor
11956 20 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e 20   must have been 
11957 6f 70 65 6e 65 64 20 77 69 74 68 20 77 72 46 6c  opened with wrFl
11958 61 67 3d 3d 31 0a 2a 2a 0a 2a 2a 20 32 3a 20 20  ag==1.**.** 2:  
11959 4f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63  Other database c
1195a 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 68 61 74 20  onnections that 
1195b 73 68 61 72 65 20 74 68 65 20 73 61 6d 65 20 70  share the same p
1195c 61 67 65 72 20 63 61 63 68 65 0a 2a 2a 20 20 20  ager cache.**   
1195d 20 20 62 75 74 20 77 68 69 63 68 20 61 72 65 20    but which are 
1195e 6e 6f 74 20 69 6e 20 74 68 65 20 52 45 41 44 5f  not in the READ_
1195f 55 4e 43 4f 4d 4d 49 54 54 45 44 20 73 74 61 74  UNCOMMITTED stat
11960 65 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 0a 2a  e may not have.*
11961 2a 20 20 20 20 20 63 75 72 73 6f 72 73 20 6f 70  *     cursors op
11962 65 6e 20 77 69 74 68 20 77 72 46 6c 61 67 3d 3d  en with wrFlag==
11963 30 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 74 61  0 on the same ta
11964 62 6c 65 2e 20 20 4f 74 68 65 72 77 69 73 65 0a  ble.  Otherwise.
11965 2a 2a 20 20 20 20 20 74 68 65 20 63 68 61 6e 67  **     the chang
11966 65 73 20 6d 61 64 65 20 62 79 20 74 68 69 73 20  es made by this 
11967 77 72 69 74 65 20 63 75 72 73 6f 72 20 77 6f 75  write cursor wou
11968 6c 64 20 62 65 20 76 69 73 69 62 6c 65 20 74 6f  ld be visible to
11969 0a 2a 2a 20 20 20 20 20 74 68 65 20 72 65 61 64  .**     the read
1196a 20 63 75 72 73 6f 72 73 20 69 6e 20 74 68 65 20   cursors in the 
1196b 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63  other database c
1196c 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  onnection..**.**
1196d 20 33 3a 20 20 54 68 65 20 64 61 74 61 62 61 73   3:  The databas
1196e 65 20 6d 75 73 74 20 62 65 20 77 72 69 74 61 62  e must be writab
1196f 6c 65 20 28 6e 6f 74 20 6f 6e 20 72 65 61 64 2d  le (not on read-
11970 6f 6e 6c 79 20 6d 65 64 69 61 29 0a 2a 2a 0a 2a  only media).**.*
11971 2a 20 34 3a 20 20 54 68 65 72 65 20 6d 75 73 74  * 4:  There must
11972 20 62 65 20 61 6e 20 61 63 74 69 76 65 20 74 72   be an active tr
11973 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  ansaction..**.**
11974 20 4e 6f 20 63 68 65 63 6b 69 6e 67 20 69 73 20   No checking is 
11975 64 6f 6e 65 20 74 6f 20 6d 61 6b 65 20 73 75 72  done to make sur
11976 65 20 74 68 61 74 20 70 61 67 65 20 69 54 61 62  e that page iTab
11977 6c 65 20 72 65 61 6c 6c 79 20 69 73 20 74 68 65  le really is the
11978 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66  .** root page of
11979 20 61 20 62 2d 74 72 65 65 2e 20 20 49 66 20 69   a b-tree.  If i
1197a 74 20 69 73 20 6e 6f 74 2c 20 74 68 65 6e 20 74  t is not, then t
1197b 68 65 20 63 75 72 73 6f 72 20 61 63 71 75 69 72  he cursor acquir
1197c 65 64 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 77  ed.** will not w
1197d 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a  ork correctly..*
1197e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72  /.static int btr
1197f 65 65 43 75 72 73 6f 72 28 0a 20 20 42 74 72 65  eeCursor(.  Btre
11980 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  e *p,           
11981 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11982 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20     /* The btree 
11983 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c  */.  int iTable,
11984 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11985 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
11986 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 61 62 6c  oot page of tabl
11987 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69  e to open */.  i
11988 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20  nt wrFlag,      
11989 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1198a 20 20 20 20 20 20 2f 2a 20 31 20 74 6f 20 77 72        /* 1 to wr
1198b 69 74 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c 79  ite. 0 read-only
1198c 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79   */.  struct Key
1198d 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20  Info *pKeyInfo, 
1198e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1198f 46 69 72 73 74 20 61 72 67 20 74 6f 20 63 6f 6d  First arg to com
11990 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e  parison function
11991 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a   */.  BtCursor *
11992 70 43 75 72 20 20 20 20 20 20 20 20 20 20 20 20  pCur            
11993 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11994 53 70 61 63 65 20 66 6f 72 20 6e 65 77 20 63 75  Space for new cu
11995 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  rsor */.){.  int
11996 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20   rc;.  BtShared 
11997 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a  *pBt = p->pBt;..
11998 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
11999 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78  3BtreeHoldsMutex
1199a 28 70 29 20 29 3b 0a 20 20 69 66 28 20 77 72 46  (p) );.  if( wrF
1199b 6c 61 67 20 29 7b 0a 20 20 20 20 69 66 28 20 70  lag ){.    if( p
1199c 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a  Bt->readOnly ){.
1199d 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
1199e 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20  ITE_READONLY;.  
1199f 20 20 7d 0a 20 20 20 20 69 66 28 20 63 68 65 63    }.    if( chec
119a0 6b 52 65 61 64 4c 6f 63 6b 73 28 70 2c 20 69 54  kReadLocks(p, iT
119a1 61 62 6c 65 2c 20 30 29 20 29 7b 0a 20 20 20 20  able, 0) ){.    
119a2 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
119a3 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 7d 0a 20 20  LOCKED;.    }.  
119a4 7d 0a 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 50  }..  if( pBt->pP
119a5 61 67 65 31 3d 3d 30 20 29 7b 0a 20 20 20 20 72  age1==0 ){.    r
119a6 63 20 3d 20 6c 6f 63 6b 42 74 72 65 65 57 69 74  c = lockBtreeWit
119a7 68 52 65 74 72 79 28 70 29 3b 0a 20 20 20 20 69  hRetry(p);.    i
119a8 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
119a9 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
119aa 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69   rc;.    }.    i
119ab 66 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79  f( pBt->readOnly
119ac 20 26 26 20 77 72 46 6c 61 67 20 29 7b 0a 20 20   && wrFlag ){.  
119ad 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
119ae 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20  E_READONLY;.    
119af 7d 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 70 67  }.  }.  pCur->pg
119b0 6e 6f 52 6f 6f 74 20 3d 20 28 50 67 6e 6f 29 69  noRoot = (Pgno)i
119b1 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 69 54 61  Table;.  if( iTa
119b2 62 6c 65 3d 3d 31 20 26 26 20 73 71 6c 69 74 65  ble==1 && sqlite
119b3 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
119b4 70 42 74 2d 3e 70 50 61 67 65 72 29 3d 3d 30 20  pBt->pPager)==0 
119b5 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
119b6 54 45 5f 45 4d 50 54 59 3b 0a 20 20 20 20 67 6f  TE_EMPTY;.    go
119b7 74 6f 20 63 72 65 61 74 65 5f 63 75 72 73 6f 72  to create_cursor
119b8 5f 65 78 63 65 70 74 69 6f 6e 3b 0a 20 20 7d 0a  _exception;.  }.
119b9 20 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69    rc = getAndIni
119ba 74 50 61 67 65 28 70 42 74 2c 20 70 43 75 72 2d  tPage(pBt, pCur-
119bb 3e 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 43 75 72  >pgnoRoot, &pCur
119bc 2d 3e 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 69  ->pPage, 0);.  i
119bd 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
119be 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 63 72 65   ){.    goto cre
119bf 61 74 65 5f 63 75 72 73 6f 72 5f 65 78 63 65 70  ate_cursor_excep
119c0 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  tion;.  }..  /* 
119c1 4e 6f 77 20 74 68 61 74 20 6e 6f 20 6f 74 68 65  Now that no othe
119c2 72 20 65 72 72 6f 72 73 20 63 61 6e 20 6f 63 63  r errors can occ
119c3 75 72 2c 20 66 69 6e 69 73 68 20 66 69 6c 6c 69  ur, finish filli
119c4 6e 67 20 69 6e 20 74 68 65 20 42 74 43 75 72 73  ng in the BtCurs
119c5 6f 72 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c 65  or.  ** variable
119c6 73 2c 20 6c 69 6e 6b 20 74 68 65 20 63 75 72 73  s, link the curs
119c7 6f 72 20 69 6e 74 6f 20 74 68 65 20 42 74 53 68  or into the BtSh
119c8 61 72 65 64 20 6c 69 73 74 20 61 6e 64 20 73 65  ared list and se
119c9 74 20 2a 70 70 43 75 72 20 28 74 68 65 0a 20 20  t *ppCur (the.  
119ca 2a 2a 20 6f 75 74 70 75 74 20 61 72 67 75 6d 65  ** output argume
119cb 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  nt to this funct
119cc 69 6f 6e 29 2e 0a 20 20 2a 2f 0a 20 20 70 43 75  ion)..  */.  pCu
119cd 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4b  r->pKeyInfo = pK
119ce 65 79 49 6e 66 6f 3b 0a 20 20 70 43 75 72 2d 3e  eyInfo;.  pCur->
119cf 70 42 74 72 65 65 20 3d 20 70 3b 0a 20 20 70 43  pBtree = p;.  pC
119d0 75 72 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20  ur->pBt = pBt;. 
119d1 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 3d 20   pCur->wrFlag = 
119d2 77 72 46 6c 61 67 3b 0a 20 20 70 43 75 72 2d 3e  wrFlag;.  pCur->
119d3 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 43 75  pNext = pBt->pCu
119d4 72 73 6f 72 3b 0a 20 20 69 66 28 20 70 43 75 72  rsor;.  if( pCur
119d5 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 70  ->pNext ){.    p
119d6 43 75 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65  Cur->pNext->pPre
119d7 76 20 3d 20 70 43 75 72 3b 0a 20 20 7d 0a 20 20  v = pCur;.  }.  
119d8 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 70  pBt->pCursor = p
119d9 43 75 72 3b 0a 20 20 70 43 75 72 2d 3e 65 53 74  Cur;.  pCur->eSt
119da 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56  ate = CURSOR_INV
119db 41 4c 49 44 3b 0a 0a 20 20 72 65 74 75 72 6e 20  ALID;..  return 
119dc 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 63 72 65 61  SQLITE_OK;..crea
119dd 74 65 5f 63 75 72 73 6f 72 5f 65 78 63 65 70 74  te_cursor_except
119de 69 6f 6e 3a 0a 20 20 69 66 28 20 70 43 75 72 20  ion:.  if( pCur 
119df 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61  ){.    releasePa
119e0 67 65 28 70 43 75 72 2d 3e 70 50 61 67 65 29 3b  ge(pCur->pPage);
119e1 0a 20 20 7d 0a 20 20 75 6e 6c 6f 63 6b 42 74 72  .  }.  unlockBtr
119e2 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b  eeIfUnused(pBt);
119e3 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
119e4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
119e5 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
119e6 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 20 2a  ursor(.  Btree *
119e7 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
119e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
119e9 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65       /* The btre
119ea 65 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c  e */.  int iTabl
119eb 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
119ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
119ed 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20     /* Root page 
119ee 6f 66 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e  of table to open
119ef 20 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67   */.  int wrFlag
119f0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
119f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
119f2 20 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 65 2e    /* 1 to write.
119f3 20 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a   0 read-only */.
119f4 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f    struct KeyInfo
119f5 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20   *pKeyInfo,     
119f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
119f7 20 46 69 72 73 74 20 61 72 67 20 74 6f 20 78 43   First arg to xC
119f8 6f 6d 70 61 72 65 28 29 20 2a 2f 0a 20 20 42 74  ompare() */.  Bt
119f9 43 75 72 73 6f 72 20 2a 70 43 75 72 20 20 20 20  Cursor *pCur    
119fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
119fb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
119fc 74 65 20 6e 65 77 20 63 75 72 73 6f 72 20 68 65  te new cursor he
119fd 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  re */.){.  int r
119fe 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  c;.  sqlite3Btre
119ff 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 2d 3e  eEnter(p);.  p->
11a00 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b  pBt->db = p->db;
11a01 0a 20 20 72 63 20 3d 20 62 74 72 65 65 43 75 72  .  rc = btreeCur
11a02 73 6f 72 28 70 2c 20 69 54 61 62 6c 65 2c 20 77  sor(p, iTable, w
11a03 72 46 6c 61 67 2c 20 70 4b 65 79 49 6e 66 6f 2c  rFlag, pKeyInfo,
11a04 20 70 43 75 72 29 3b 0a 20 20 73 71 6c 69 74 65   pCur);.  sqlite
11a05 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
11a06 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53    return rc;.}.S
11a07 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
11a08 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  t sqlite3BtreeCu
11a09 72 73 6f 72 53 69 7a 65 28 29 7b 0a 20 20 72 65  rsorSize(){.  re
11a0a 74 75 72 6e 20 73 69 7a 65 6f 66 28 42 74 43 75  turn sizeof(BtCu
11a0b 72 73 6f 72 29 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a  rsor);.}..../*.*
11a0c 2a 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72  * Close a cursor
11a0d 2e 20 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b  .  The read lock
11a0e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
11a0f 20 66 69 6c 65 20 69 73 20 72 65 6c 65 61 73 65   file is release
11a10 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 6c 61  d.** when the la
11a11 73 74 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f  st cursor is clo
11a12 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  sed..*/.SQLITE_P
11a13 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
11a14 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73  e3BtreeCloseCurs
11a15 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  or(BtCursor *pCu
11a16 72 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74  r){.  Btree *pBt
11a17 72 65 65 20 3d 20 70 43 75 72 2d 3e 70 42 74 72  ree = pCur->pBtr
11a18 65 65 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65  ee;.  if( pBtree
11a19 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64   ){.    BtShared
11a1a 20 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42   *pBt = pCur->pB
11a1b 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  t;.    sqlite3Bt
11a1c 72 65 65 45 6e 74 65 72 28 70 42 74 72 65 65 29  reeEnter(pBtree)
11a1d 3b 0a 20 20 20 20 70 42 74 2d 3e 64 62 20 3d 20  ;.    pBt->db = 
11a1e 70 42 74 72 65 65 2d 3e 64 62 3b 0a 20 20 20 20  pBtree->db;.    
11a1f 63 6c 65 61 72 43 75 72 73 6f 72 50 6f 73 69 74  clearCursorPosit
11a20 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 20 20 69  ion(pCur);.    i
11a21 66 28 20 70 43 75 72 2d 3e 70 50 72 65 76 20 29  f( pCur->pPrev )
11a22 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 50  {.      pCur->pP
11a23 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 43 75  rev->pNext = pCu
11a24 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65  r->pNext;.    }e
11a25 6c 73 65 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e  lse{.      pBt->
11a26 70 43 75 72 73 6f 72 20 3d 20 70 43 75 72 2d 3e  pCursor = pCur->
11a27 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20  pNext;.    }.   
11a28 20 69 66 28 20 70 43 75 72 2d 3e 70 4e 65 78 74   if( pCur->pNext
11a29 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e   ){.      pCur->
11a2a 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70  pNext->pPrev = p
11a2b 43 75 72 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20  Cur->pPrev;.    
11a2c 7d 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67  }.    releasePag
11a2d 65 28 70 43 75 72 2d 3e 70 50 61 67 65 29 3b 0a  e(pCur->pPage);.
11a2e 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49      unlockBtreeI
11a2f 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20  fUnused(pBt);.  
11a30 20 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72    invalidateOver
11a31 66 6c 6f 77 43 61 63 68 65 28 70 43 75 72 29 3b  flowCache(pCur);
11a32 0a 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f  .    /* sqlite3_
11a33 66 72 65 65 28 70 43 75 72 29 3b 20 2a 2f 0a 20  free(pCur); */. 
11a34 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
11a35 65 61 76 65 28 70 42 74 72 65 65 29 3b 0a 20 20  eave(pBtree);.  
11a36 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
11a37 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  E_OK;.}../*.** M
11a38 61 6b 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20  ake a temporary 
11a39 63 75 72 73 6f 72 20 62 79 20 66 69 6c 6c 69 6e  cursor by fillin
11a3a 67 20 69 6e 20 74 68 65 20 66 69 65 6c 64 73 20  g in the fields 
11a3b 6f 66 20 70 54 65 6d 70 43 75 72 2e 0a 2a 2a 20  of pTempCur..** 
11a3c 54 68 65 20 74 65 6d 70 6f 72 61 72 79 20 63 75  The temporary cu
11a3d 72 73 6f 72 20 69 73 20 6e 6f 74 20 6f 6e 20 74  rsor is not on t
11a3e 68 65 20 63 75 72 73 6f 72 20 6c 69 73 74 20 66  he cursor list f
11a3f 6f 72 20 74 68 65 20 42 74 72 65 65 2e 0a 2a 2f  or the Btree..*/
11a40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
11a41 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
11a42 65 47 65 74 54 65 6d 70 43 75 72 73 6f 72 28 42  eGetTempCursor(B
11a43 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 42  tCursor *pCur, B
11a44 74 43 75 72 73 6f 72 20 2a 70 54 65 6d 70 43 75  tCursor *pTempCu
11a45 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75  r){.  assert( cu
11a46 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
11a47 43 75 72 29 20 29 3b 0a 20 20 6d 65 6d 63 70 79  Cur) );.  memcpy
11a48 28 70 54 65 6d 70 43 75 72 2c 20 70 43 75 72 2c  (pTempCur, pCur,
11a49 20 73 69 7a 65 6f 66 28 2a 70 43 75 72 29 29 3b   sizeof(*pCur));
11a4a 0a 20 20 70 54 65 6d 70 43 75 72 2d 3e 70 4e 65  .  pTempCur->pNe
11a4b 78 74 20 3d 20 30 3b 0a 20 20 70 54 65 6d 70 43  xt = 0;.  pTempC
11a4c 75 72 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20  ur->pPrev = 0;. 
11a4d 20 69 66 28 20 70 54 65 6d 70 43 75 72 2d 3e 70   if( pTempCur->p
11a4e 50 61 67 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  Page ){.    sqli
11a4f 74 65 33 50 61 67 65 72 52 65 66 28 70 54 65 6d  te3PagerRef(pTem
11a50 70 43 75 72 2d 3e 70 50 61 67 65 2d 3e 70 44 62  pCur->pPage->pDb
11a51 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  Page);.  }.}../*
11a52 0a 2a 2a 20 44 65 6c 65 74 65 20 61 20 74 65 6d  .** Delete a tem
11a53 70 6f 72 61 72 79 20 63 75 72 73 6f 72 20 73 75  porary cursor su
11a54 63 68 20 61 73 20 77 61 73 20 6d 61 64 65 20 62  ch as was made b
11a55 79 20 74 68 65 20 43 72 65 61 74 65 54 65 6d 70  y the CreateTemp
11a56 6f 72 61 72 79 43 75 72 73 6f 72 28 29 0a 2a 2a  oraryCursor().**
11a57 20 66 75 6e 63 74 69 6f 6e 20 61 62 6f 76 65 2e   function above.
11a58 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
11a59 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
11a5a 74 72 65 65 52 65 6c 65 61 73 65 54 65 6d 70 43  treeReleaseTempC
11a5b 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a  ursor(BtCursor *
11a5c 70 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28  pCur){.  assert(
11a5d 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
11a5e 78 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28  x(pCur) );.  if(
11a5f 20 70 43 75 72 2d 3e 70 50 61 67 65 20 29 7b 0a   pCur->pPage ){.
11a60 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
11a61 55 6e 72 65 66 28 70 43 75 72 2d 3e 70 50 61 67  Unref(pCur->pPag
11a62 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 7d  e->pDbPage);.  }
11a63 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73  .}../*.** Make s
11a64 75 72 65 20 74 68 65 20 42 74 43 75 72 73 6f 72  ure the BtCursor
11a65 2a 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 61  * given in the a
11a66 72 67 75 6d 65 6e 74 20 68 61 73 20 61 20 76 61  rgument has a va
11a67 6c 69 64 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e  lid.** BtCursor.
11a68 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20  info structure. 
11a69 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c   If it is not al
11a6a 72 65 61 64 79 20 76 61 6c 69 64 2c 20 63 61 6c  ready valid, cal
11a6b 6c 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65  l.** sqlite3Btre
11a6c 65 50 61 72 73 65 43 65 6c 6c 28 29 20 74 6f 20  eParseCell() to 
11a6d 66 69 6c 6c 20 69 74 20 69 6e 2e 0a 2a 2a 0a 2a  fill it in..**.*
11a6e 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20  * BtCursor.info 
11a6f 69 73 20 61 20 63 61 63 68 65 20 6f 66 20 74 68  is a cache of th
11a70 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e  e information in
11a71 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 65 6c   the current cel
11a72 6c 2e 0a 2a 2a 20 55 73 69 6e 67 20 74 68 69 73  l..** Using this
11a73 20 63 61 63 68 65 20 72 65 64 75 63 65 73 20 74   cache reduces t
11a74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c  he number of cal
11a75 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72  ls to sqlite3Btr
11a76 65 65 50 61 72 73 65 43 65 6c 6c 28 29 2e 0a 2a  eeParseCell()..*
11a77 2a 0a 2a 2a 20 32 30 30 37 2d 30 36 2d 32 35 3a  *.** 2007-06-25:
11a78 20 20 54 68 65 72 65 20 69 73 20 61 20 62 75 67    There is a bug
11a79 20 69 6e 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e   in some version
11a7a 73 20 6f 66 20 4d 53 56 43 20 74 68 61 74 20 63  s of MSVC that c
11a7b 61 75 73 65 20 74 68 65 0a 2a 2a 20 63 6f 6d 70  ause the.** comp
11a7c 69 6c 65 72 20 74 6f 20 63 72 61 73 68 20 77 68  iler to crash wh
11a7d 65 6e 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 29  en getCellInfo()
11a7e 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20   is implemented 
11a7f 61 73 20 61 20 6d 61 63 72 6f 2e 0a 2a 2a 20 42  as a macro..** B
11a80 75 74 20 74 68 65 72 65 20 69 73 20 61 20 6d 65  ut there is a me
11a81 61 73 75 72 65 61 62 6c 65 20 73 70 65 65 64 20  asureable speed 
11a82 61 64 76 61 6e 74 61 67 65 20 74 6f 20 75 73 69  advantage to usi
11a83 6e 67 20 74 68 65 20 6d 61 63 72 6f 20 6f 6e 20  ng the macro on 
11a84 67 63 63 0a 2a 2a 20 28 77 68 65 6e 20 6c 65 73  gcc.** (when les
11a85 73 20 63 6f 6d 70 69 6c 65 72 20 6f 70 74 69 6d  s compiler optim
11a86 69 7a 61 74 69 6f 6e 73 20 6c 69 6b 65 20 2d 4f  izations like -O
11a87 73 20 6f 72 20 2d 4f 30 20 61 72 65 20 75 73 65  s or -O0 are use
11a88 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 63 6f 6d  d and the.** com
11a89 70 69 6c 65 72 20 69 73 20 6e 6f 74 20 64 6f 69  piler is not doi
11a8a 6e 67 20 61 67 72 65 73 73 69 76 65 20 69 6e 6c  ng agressive inl
11a8b 69 6e 69 6e 67 2e 29 20 20 53 6f 20 77 65 20 75  ining.)  So we u
11a8c 73 65 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69  se a real functi
11a8d 6f 6e 0a 2a 2a 20 66 6f 72 20 4d 53 56 43 20 61  on.** for MSVC a
11a8e 6e 64 20 61 20 6d 61 63 72 6f 20 66 6f 72 20 65  nd a macro for e
11a8f 76 65 72 79 74 68 69 6e 67 20 65 6c 73 65 2e 20  verything else. 
11a90 20 54 69 63 6b 65 74 20 23 32 34 35 37 2e 0a 2a   Ticket #2457..*
11a91 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  /.#ifndef NDEBUG
11a92 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64 20 61  .  static void a
11a93 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 42 74  ssertCellInfo(Bt
11a94 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
11a95 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f     CellInfo info
11a96 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 69 6e  ;.    memset(&in
11a97 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 69 6e  fo, 0, sizeof(in
11a98 66 6f 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  fo));.    sqlite
11a99 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28  3BtreeParseCell(
11a9a 70 43 75 72 2d 3e 70 50 61 67 65 2c 20 70 43 75  pCur->pPage, pCu
11a9b 72 2d 3e 69 64 78 2c 20 26 69 6e 66 6f 29 3b 0a  r->idx, &info);.
11a9c 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 63      assert( memc
11a9d 6d 70 28 26 69 6e 66 6f 2c 20 26 70 43 75 72 2d  mp(&info, &pCur-
11a9e 3e 69 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 69 6e  >info, sizeof(in
11a9f 66 6f 29 29 3d 3d 30 20 29 3b 0a 20 20 7d 0a 23  fo))==0 );.  }.#
11aa0 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 61  else.  #define a
11aa1 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 78 29  ssertCellInfo(x)
11aa2 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 5f  .#endif.#ifdef _
11aa3 4d 53 43 5f 56 45 52 0a 20 20 2f 2a 20 55 73 65  MSC_VER.  /* Use
11aa4 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e   a real function
11aa5 20 69 6e 20 4d 53 56 43 20 74 6f 20 77 6f 72 6b   in MSVC to work
11aa6 20 61 72 6f 75 6e 64 20 62 75 67 73 20 69 6e 20   around bugs in 
11aa7 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 2e 20 2a  that compiler. *
11aa8 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64 20  /.  static void 
11aa9 67 65 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75  getCellInfo(BtCu
11aaa 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 20  rsor *pCur){.   
11aab 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e   if( pCur->info.
11aac 6e 53 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20  nSize==0 ){.    
11aad 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61    sqlite3BtreePa
11aae 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e 70 50  rseCell(pCur->pP
11aaf 61 67 65 2c 20 70 43 75 72 2d 3e 69 64 78 2c 20  age, pCur->idx, 
11ab0 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a 20 20  &pCur->info);.  
11ab1 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e      pCur->validN
11ab2 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c  Key = 1;.    }el
11ab3 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
11ab4 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a  CellInfo(pCur);.
11ab5 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 20      }.  }.#else 
11ab6 2f 2a 20 69 66 20 6e 6f 74 20 5f 4d 53 43 5f 56  /* if not _MSC_V
11ab7 45 52 20 2a 2f 0a 20 20 2f 2a 20 55 73 65 20 61  ER */.  /* Use a
11ab8 20 6d 61 63 72 6f 20 69 6e 20 61 6c 6c 20 6f 74   macro in all ot
11ab9 68 65 72 20 63 6f 6d 70 69 6c 65 72 73 20 73 6f  her compilers so
11aba 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69   that the functi
11abb 6f 6e 20 69 73 20 69 6e 6c 69 6e 65 64 20 2a 2f  on is inlined */
11abc 0a 23 64 65 66 69 6e 65 20 67 65 74 43 65 6c 6c  .#define getCell
11abd 49 6e 66 6f 28 70 43 75 72 29 20 20 20 20 20 20  Info(pCur)      
11abe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11abf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11ac0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28           \.  if(
11ac1 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a   pCur->info.nSiz
11ac2 65 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20  e==0 ){         
11ac3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11ac4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11ac5 20 20 20 5c 0a 20 20 20 20 73 71 6c 69 74 65 33     \.    sqlite3
11ac6 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70  BtreeParseCell(p
11ac7 43 75 72 2d 3e 70 50 61 67 65 2c 20 70 43 75 72  Cur->pPage, pCur
11ac8 2d 3e 69 64 78 2c 20 26 70 43 75 72 2d 3e 69 6e  ->idx, &pCur->in
11ac9 66 6f 29 3b 20 20 20 20 20 20 20 20 20 5c 0a 20  fo);         \. 
11aca 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b     pCur->validNK
11acb 65 79 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20  ey = 1;         
11acc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11acd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11ace 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65         \.  }else
11acf 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
11ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11ad1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11ad2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11ad3 20 5c 0a 20 20 20 20 61 73 73 65 72 74 43 65 6c   \.    assertCel
11ad4 6c 49 6e 66 6f 28 70 43 75 72 29 3b 20 20 20 20  lInfo(pCur);    
11ad5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11ad6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11ad7 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d             \.  }
11ad8 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 4d 53 43 5f  .#endif /* _MSC_
11ad9 56 45 52 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65  VER */../*.** Se
11ada 74 20 2a 70 53 69 7a 65 20 74 6f 20 74 68 65 20  t *pSize to the 
11adb 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75 66 66  size of the buff
11adc 65 72 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c  er needed to hol
11add 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 0a 2a  d the value of.*
11ade 2a 20 74 68 65 20 6b 65 79 20 66 6f 72 20 74 68  * the key for th
11adf 65 20 63 75 72 72 65 6e 74 20 65 6e 74 72 79 2e  e current entry.
11ae0 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20    If the cursor 
11ae1 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 0a  is not pointing.
11ae2 2a 2a 20 74 6f 20 61 20 76 61 6c 69 64 20 65 6e  ** to a valid en
11ae3 74 72 79 2c 20 2a 70 53 69 7a 65 20 69 73 20 73  try, *pSize is s
11ae4 65 74 20 74 6f 20 30 2e 20 0a 2a 2a 0a 2a 2a 20  et to 0. .**.** 
11ae5 46 6f 72 20 61 20 74 61 62 6c 65 20 77 69 74 68  For a table with
11ae6 20 74 68 65 20 49 4e 54 4b 45 59 20 66 6c 61 67   the INTKEY flag
11ae7 20 73 65 74 2c 20 74 68 69 73 20 72 6f 75 74 69   set, this routi
11ae8 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6b  ne returns the k
11ae9 65 79 0a 2a 2a 20 69 74 73 65 6c 66 2c 20 6e 6f  ey.** itself, no
11aea 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
11aeb 62 79 74 65 73 20 69 6e 20 74 68 65 20 6b 65 79  bytes in the key
11aec 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
11aed 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
11aee 74 72 65 65 4b 65 79 53 69 7a 65 28 42 74 43 75  treeKeySize(BtCu
11aef 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 36 34 20  rsor *pCur, i64 
11af0 2a 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72  *pSize){.  int r
11af1 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75  c;..  assert( cu
11af2 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
11af3 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72  Cur) );.  rc = r
11af4 65 73 74 6f 72 65 4f 72 43 6c 65 61 72 43 75 72  estoreOrClearCur
11af5 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72  sorPosition(pCur
11af6 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
11af7 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73  ITE_OK ){.    as
11af8 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61  sert( pCur->eSta
11af9 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c  te==CURSOR_INVAL
11afa 49 44 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61  ID || pCur->eSta
11afb 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
11afc 20 29 3b 0a 20 20 20 20 69 66 28 20 70 43 75 72   );.    if( pCur
11afd 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
11afe 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20  _INVALID ){.    
11aff 20 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 20 20    *pSize = 0;.  
11b00 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 67    }else{.      g
11b01 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29  etCellInfo(pCur)
11b02 3b 0a 20 20 20 20 20 20 2a 70 53 69 7a 65 20 3d  ;.      *pSize =
11b03 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79   pCur->info.nKey
11b04 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
11b05 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
11b06 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74 6f 20  * Set *pSize to 
11b07 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
11b08 74 65 73 20 6f 66 20 64 61 74 61 20 69 6e 20 74  tes of data in t
11b09 68 65 20 65 6e 74 72 79 20 74 68 65 0a 2a 2a 20  he entry the.** 
11b0a 63 75 72 73 6f 72 20 63 75 72 72 65 6e 74 6c 79  cursor currently
11b0b 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20 41 6c 77   points to.  Alw
11b0c 61 79 73 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ays return SQLIT
11b0d 45 5f 4f 4b 2e 0a 2a 2a 20 46 61 69 6c 75 72 65  E_OK..** Failure
11b0e 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
11b0f 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72  .  If the cursor
11b10 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
11b11 79 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f  y.** pointing to
11b12 20 61 6e 20 65 6e 74 72 79 20 28 77 68 69 63 68   an entry (which
11b13 20 63 61 6e 20 68 61 70 70 65 6e 2c 20 66 6f 72   can happen, for
11b14 20 65 78 61 6d 70 6c 65 2c 20 69 66 0a 2a 2a 20   example, if.** 
11b15 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
11b16 65 6d 70 74 79 29 20 74 68 65 6e 20 2a 70 53 69  empty) then *pSi
11b17 7a 65 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a  ze is set to 0..
11b18 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
11b19 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
11b1a 65 65 44 61 74 61 53 69 7a 65 28 42 74 43 75 72  eeDataSize(BtCur
11b1b 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 2a  sor *pCur, u32 *
11b1c 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63  pSize){.  int rc
11b1d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  ;..  assert( cur
11b1e 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
11b1f 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65  ur) );.  rc = re
11b20 73 74 6f 72 65 4f 72 43 6c 65 61 72 43 75 72 73  storeOrClearCurs
11b21 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29  orPosition(pCur)
11b22 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
11b23 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73  TE_OK ){.    ass
11b24 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74  ert( pCur->eStat
11b25 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49  e==CURSOR_INVALI
11b26 44 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74  D || pCur->eStat
11b27 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  e==CURSOR_VALID 
11b28 29 3b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d  );.    if( pCur-
11b29 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
11b2a 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20  INVALID ){.     
11b2b 20 2f 2a 20 4e 6f 74 20 70 6f 69 6e 74 69 6e 67   /* Not pointing
11b2c 20 61 74 20 61 20 76 61 6c 69 64 20 65 6e 74 72   at a valid entr
11b2d 79 20 2d 20 73 65 74 20 2a 70 53 69 7a 65 20 74  y - set *pSize t
11b2e 6f 20 30 2e 20 2a 2f 0a 20 20 20 20 20 20 2a 70  o 0. */.      *p
11b2f 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65  Size = 0;.    }e
11b30 6c 73 65 7b 0a 20 20 20 20 20 20 67 65 74 43 65  lse{.      getCe
11b31 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20  llInfo(pCur);.  
11b32 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 70 43 75      *pSize = pCu
11b33 72 2d 3e 69 6e 66 6f 2e 6e 44 61 74 61 3b 0a 20  r->info.nData;. 
11b34 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
11b35 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  n rc;.}../*.** G
11b36 69 76 65 6e 20 74 68 65 20 70 61 67 65 20 6e 75  iven the page nu
11b37 6d 62 65 72 20 6f 66 20 61 6e 20 6f 76 65 72 66  mber of an overf
11b38 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65 20  low page in the 
11b39 64 61 74 61 62 61 73 65 20 28 70 61 72 61 6d 65  database (parame
11b3a 74 65 72 0a 2a 2a 20 6f 76 66 6c 29 2c 20 74 68  ter.** ovfl), th
11b3b 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 69 6e 64  is function find
11b3c 73 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65  s the page numbe
11b3d 72 20 6f 66 20 74 68 65 20 6e 65 78 74 20 70 61  r of the next pa
11b3e 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 6c 69  ge in the .** li
11b3f 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 6f 76 65  nked list of ove
11b40 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 49 66 20  rflow pages. If 
11b41 70 6f 73 73 69 62 6c 65 2c 20 69 74 20 75 73 65  possible, it use
11b42 73 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75  s the auto-vacuu
11b43 6d 0a 2a 2a 20 70 6f 69 6e 74 65 72 2d 6d 61 70  m.** pointer-map
11b44 20 64 61 74 61 20 69 6e 73 74 65 61 64 20 6f 66   data instead of
11b45 20 72 65 61 64 69 6e 67 20 74 68 65 20 63 6f 6e   reading the con
11b46 74 65 6e 74 20 6f 66 20 70 61 67 65 20 6f 76 66  tent of page ovf
11b47 6c 20 74 6f 20 64 6f 20 73 6f 2e 20 0a 2a 2a 0a  l to do so. .**.
11b48 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  ** If an error o
11b49 63 63 75 72 73 20 61 6e 20 53 51 4c 69 74 65 20  ccurs an SQLite 
11b4a 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
11b4b 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73  turned. Otherwis
11b4c 65 3a 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20  e:.**.** Unless 
11b4d 70 50 67 6e 6f 4e 65 78 74 20 69 73 20 4e 55 4c  pPgnoNext is NUL
11b4e 4c 2c 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62  L, the page numb
11b4f 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74 20 6f  er of the next o
11b50 76 65 72 66 6c 6f 77 20 0a 2a 2a 20 70 61 67 65  verflow .** page
11b51 20 69 6e 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c   in the linked l
11b52 69 73 74 20 69 73 20 77 72 69 74 74 65 6e 20 74  ist is written t
11b53 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 2e 20 49 66  o *pPgnoNext. If
11b54 20 70 61 67 65 20 6f 76 66 6c 0a 2a 2a 20 69 73   page ovfl.** is
11b55 20 74 68 65 20 6c 61 73 74 20 70 61 67 65 20 69   the last page i
11b56 6e 20 69 74 73 20 6c 69 6e 6b 65 64 20 6c 69 73  n its linked lis
11b57 74 2c 20 2a 70 50 67 6e 6f 4e 65 78 74 20 69 73  t, *pPgnoNext is
11b58 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 0a 2a   set to zero. .*
11b59 2a 0a 2a 2a 20 49 66 20 70 70 50 61 67 65 20 69  *.** If ppPage i
11b5a 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 2a 70 70 50  s not NULL, *ppP
11b5b 61 67 65 20 69 73 20 73 65 74 20 74 6f 20 74 68  age is set to th
11b5c 65 20 4d 65 6d 50 61 67 65 2a 20 68 61 6e 64 6c  e MemPage* handl
11b5d 65 0a 2a 2a 20 66 6f 72 20 70 61 67 65 20 6f 76  e.** for page ov
11b5e 66 6c 2e 20 54 68 65 20 75 6e 64 65 72 6c 79 69  fl. The underlyi
11b5f 6e 67 20 70 61 67 65 72 20 70 61 67 65 20 6d 61  ng pager page ma
11b60 79 20 68 61 76 65 20 62 65 65 6e 20 72 65 71 75  y have been requ
11b61 65 73 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68  ested.** with th
11b62 65 20 6e 6f 43 6f 6e 74 65 6e 74 20 66 6c 61 67  e noContent flag
11b63 20 73 65 74 2c 20 73 6f 20 74 68 65 20 70 61 67   set, so the pag
11b64 65 20 64 61 74 61 20 61 63 63 65 73 73 61 62 6c  e data accessabl
11b65 65 20 76 69 61 0a 2a 2a 20 74 68 69 73 20 68 61  e via.** this ha
11b66 6e 64 6c 65 20 6d 61 79 20 6e 6f 74 20 62 65 20  ndle may not be 
11b67 74 72 75 73 74 65 64 2e 0a 2a 2f 0a 73 74 61 74  trusted..*/.stat
11b68 69 63 20 69 6e 74 20 67 65 74 4f 76 65 72 66 6c  ic int getOverfl
11b69 6f 77 50 61 67 65 28 0a 20 20 42 74 53 68 61 72  owPage(.  BtShar
11b6a 65 64 20 2a 70 42 74 2c 20 0a 20 20 50 67 6e 6f  ed *pBt, .  Pgno
11b6b 20 6f 76 66 6c 2c 20 20 20 20 20 20 20 20 20 20   ovfl,          
11b6c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 76 65 72           /* Over
11b6d 66 6c 6f 77 20 70 61 67 65 20 2a 2f 0a 20 20 4d  flow page */.  M
11b6e 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c  emPage **ppPage,
11b6f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
11b70 55 54 3a 20 4d 65 6d 50 61 67 65 20 68 61 6e 64  UT: MemPage hand
11b71 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 2a 70 50  le */.  Pgno *pP
11b72 67 6e 6f 4e 65 78 74 20 20 20 20 20 20 20 20 20  gnoNext         
11b73 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 65 78       /* OUT: Nex
11b74 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  t overflow page 
11b75 6e 75 6d 62 65 72 20 2a 2f 0a 29 7b 0a 20 20 50  number */.){.  P
11b76 67 6e 6f 20 6e 65 78 74 20 3d 20 30 3b 0a 20 20  gno next = 0;.  
11b77 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72  int rc;..  asser
11b78 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
11b79 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78  _held(pBt->mutex
11b7a 29 20 29 3b 0a 20 20 2f 2a 20 4f 6e 65 20 6f 66  ) );.  /* One of
11b7b 20 74 68 65 73 65 20 6d 75 73 74 20 6e 6f 74 20   these must not 
11b7c 62 65 20 4e 55 4c 4c 2e 20 4f 74 68 65 72 77 69  be NULL. Otherwi
11b7d 73 65 2c 20 77 68 79 20 63 61 6c 6c 20 74 68 69  se, why call thi
11b7e 73 20 66 75 6e 63 74 69 6f 6e 3f 20 2a 2f 0a 20  s function? */. 
11b7f 20 61 73 73 65 72 74 28 70 70 50 61 67 65 20 7c   assert(ppPage |
11b80 7c 20 70 50 67 6e 6f 4e 65 78 74 29 3b 0a 0a 20  | pPgnoNext);.. 
11b81 20 2f 2a 20 49 66 20 70 50 67 6e 6f 4e 65 78 74   /* If pPgnoNext
11b82 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74   is NULL, then t
11b83 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
11b84 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 74 6f 20  being called to 
11b85 6f 62 74 61 69 6e 0a 20 20 2a 2a 20 61 20 4d 65  obtain.  ** a Me
11b86 6d 50 61 67 65 2a 20 72 65 66 65 72 65 6e 63 65  mPage* reference
11b87 20 6f 6e 6c 79 2e 20 4e 6f 20 70 61 67 65 2d 64   only. No page-d
11b88 61 74 61 20 69 73 20 72 65 71 75 69 72 65 64 20  ata is required 
11b89 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 20 20  in this case..  
11b8a 2a 2f 0a 20 20 69 66 28 20 21 70 50 67 6e 6f 4e  */.  if( !pPgnoN
11b8b 65 78 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ext ){.    retur
11b8c 6e 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  n sqlite3BtreeGe
11b8d 74 50 61 67 65 28 70 42 74 2c 20 6f 76 66 6c 2c  tPage(pBt, ovfl,
11b8e 20 70 70 50 61 67 65 2c 20 31 29 3b 0a 20 20 7d   ppPage, 1);.  }
11b8f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
11b90 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
11b91 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e  .  /* Try to fin
11b92 64 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20  d the next page 
11b93 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  in the overflow 
11b94 6c 69 73 74 20 75 73 69 6e 67 20 74 68 65 0a 20  list using the. 
11b95 20 2a 2a 20 61 75 74 6f 76 61 63 75 75 6d 20 70   ** autovacuum p
11b96 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 73  ointer-map pages
11b97 2e 20 47 75 65 73 73 20 74 68 61 74 20 74 68 65  . Guess that the
11b98 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 0a 20   next page in . 
11b99 20 2a 2a 20 74 68 65 20 6f 76 65 72 66 6c 6f 77   ** the overflow
11b9a 20 6c 69 73 74 20 69 73 20 70 61 67 65 20 6e 75   list is page nu
11b9b 6d 62 65 72 20 28 6f 76 66 6c 2b 31 29 2e 20 49  mber (ovfl+1). I
11b9c 66 20 74 68 61 74 20 67 75 65 73 73 20 74 75 72  f that guess tur
11b9d 6e 73 20 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20  ns .  ** out to 
11b9e 62 65 20 77 72 6f 6e 67 2c 20 66 61 6c 6c 20 62  be wrong, fall b
11b9f 61 63 6b 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74  ack to loading t
11ba0 68 65 20 64 61 74 61 20 6f 66 20 70 61 67 65 20  he data of page 
11ba1 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 76 66  .  ** number ovf
11ba2 6c 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74  l to determine t
11ba3 68 65 20 6e 65 78 74 20 70 61 67 65 20 6e 75 6d  he next page num
11ba4 62 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ber..  */.  if( 
11ba5 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
11ba6 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f  ){.    Pgno pgno
11ba7 3b 0a 20 20 20 20 50 67 6e 6f 20 69 47 75 65 73  ;.    Pgno iGues
11ba8 73 20 3d 20 6f 76 66 6c 2b 31 3b 0a 20 20 20 20  s = ovfl+1;.    
11ba9 75 38 20 65 54 79 70 65 3b 0a 0a 20 20 20 20 77  u8 eType;..    w
11baa 68 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50  hile( PTRMAP_ISP
11bab 41 47 45 28 70 42 74 2c 20 69 47 75 65 73 73 29  AGE(pBt, iGuess)
11bac 20 7c 7c 20 69 47 75 65 73 73 3d 3d 50 45 4e 44   || iGuess==PEND
11bad 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
11bae 74 29 20 29 7b 0a 20 20 20 20 20 20 69 47 75 65  t) ){.      iGue
11baf 73 73 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ss++;.    }..   
11bb0 20 69 66 28 20 69 47 75 65 73 73 3c 3d 73 71 6c   if( iGuess<=sql
11bb1 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75  ite3PagerPagecou
11bb2 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 20  nt(pBt->pPager) 
11bb3 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74  ){.      rc = pt
11bb4 72 6d 61 70 47 65 74 28 70 42 74 2c 20 69 47 75  rmapGet(pBt, iGu
11bb5 65 73 73 2c 20 26 65 54 79 70 65 2c 20 26 70 67  ess, &eType, &pg
11bb6 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  no);.      if( r
11bb7 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
11bb8 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
11bb9 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  c;.      }.     
11bba 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d   if( eType==PTRM
11bbb 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 26 26 20  AP_OVERFLOW2 && 
11bbc 70 67 6e 6f 3d 3d 6f 76 66 6c 20 29 7b 0a 20 20  pgno==ovfl ){.  
11bbd 20 20 20 20 20 20 6e 65 78 74 20 3d 20 69 47 75        next = iGu
11bbe 65 73 73 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ess;.      }.   
11bbf 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20   }.  }.#endif.. 
11bc0 20 69 66 28 20 6e 65 78 74 3d 3d 30 20 7c 7c 20   if( next==0 || 
11bc1 70 70 50 61 67 65 20 29 7b 0a 20 20 20 20 4d 65  ppPage ){.    Me
11bc2 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30  mPage *pPage = 0
11bc3 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ;..    rc = sqli
11bc4 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28  te3BtreeGetPage(
11bc5 70 42 74 2c 20 6f 76 66 6c 2c 20 26 70 50 61 67  pBt, ovfl, &pPag
11bc6 65 2c 20 6e 65 78 74 21 3d 30 29 3b 0a 20 20 20  e, next!=0);.   
11bc7 20 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49   assert(rc==SQLI
11bc8 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 3d 3d  TE_OK || pPage==
11bc9 30 29 3b 0a 20 20 20 20 69 66 28 20 6e 65 78 74  0);.    if( next
11bca 3d 3d 30 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  ==0 && rc==SQLIT
11bcb 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 6e 65  E_OK ){.      ne
11bcc 78 74 20 3d 20 67 65 74 34 62 79 74 65 28 70 50  xt = get4byte(pP
11bcd 61 67 65 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20  age->aData);.   
11bce 20 7d 0a 0a 20 20 20 20 69 66 28 20 70 70 50 61   }..    if( ppPa
11bcf 67 65 20 29 7b 0a 20 20 20 20 20 20 2a 70 70 50  ge ){.      *ppP
11bd0 61 67 65 20 3d 20 70 50 61 67 65 3b 0a 20 20 20  age = pPage;.   
11bd1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65   }else{.      re
11bd2 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29  leasePage(pPage)
11bd3 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70  ;.    }.  }.  *p
11bd4 50 67 6e 6f 4e 65 78 74 20 3d 20 6e 65 78 74 3b  PgnoNext = next;
11bd5 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
11bd6 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 64 61 74  ../*.** Copy dat
11bd7 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20  a from a buffer 
11bd8 74 6f 20 61 20 70 61 67 65 2c 20 6f 72 20 66 72  to a page, or fr
11bd9 6f 6d 20 61 20 70 61 67 65 20 74 6f 20 61 20 62  om a page to a b
11bda 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 70 50 61  uffer..**.** pPa
11bdb 79 6c 6f 61 64 20 69 73 20 61 20 70 6f 69 6e 74  yload is a point
11bdc 65 72 20 74 6f 20 64 61 74 61 20 73 74 6f 72 65  er to data store
11bdd 64 20 6f 6e 20 64 61 74 61 62 61 73 65 20 70 61  d on database pa
11bde 67 65 20 70 44 62 50 61 67 65 2e 0a 2a 2a 20 49  ge pDbPage..** I
11bdf 66 20 61 72 67 75 6d 65 6e 74 20 65 4f 70 20 69  f argument eOp i
11be0 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 6e 42  s false, then nB
11be1 79 74 65 20 62 79 74 65 73 20 6f 66 20 64 61 74  yte bytes of dat
11be2 61 20 61 72 65 20 63 6f 70 69 65 64 0a 2a 2a 20  a are copied.** 
11be3 66 72 6f 6d 20 70 50 61 79 6c 6f 61 64 20 74 6f  from pPayload to
11be4 20 74 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e   the buffer poin
11be5 74 65 64 20 61 74 20 62 79 20 70 42 75 66 2e 20  ted at by pBuf. 
11be6 49 66 20 65 4f 70 20 69 73 20 74 72 75 65 2c 0a  If eOp is true,.
11be7 2a 2a 20 74 68 65 6e 20 73 71 6c 69 74 65 33 50  ** then sqlite3P
11be8 61 67 65 72 57 72 69 74 65 28 29 20 69 73 20 63  agerWrite() is c
11be9 61 6c 6c 65 64 20 6f 6e 20 70 44 62 50 61 67 65  alled on pDbPage
11bea 20 61 6e 64 20 6e 42 79 74 65 20 62 79 74 65 73   and nByte bytes
11beb 0a 2a 2a 20 6f 66 20 64 61 74 61 20 61 72 65 20  .** of data are 
11bec 63 6f 70 69 65 64 20 66 72 6f 6d 20 74 68 65 20  copied from the 
11bed 62 75 66 66 65 72 20 70 42 75 66 20 74 6f 20 70  buffer pBuf to p
11bee 50 61 79 6c 6f 61 64 2e 0a 2a 2a 0a 2a 2a 20 53  Payload..**.** S
11bef 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
11bf0 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2c  rned on success,
11bf1 20 6f 74 68 65 72 77 69 73 65 20 61 6e 20 65 72   otherwise an er
11bf2 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61  ror code..*/.sta
11bf3 74 69 63 20 69 6e 74 20 63 6f 70 79 50 61 79 6c  tic int copyPayl
11bf4 6f 61 64 28 0a 20 20 76 6f 69 64 20 2a 70 50 61  oad(.  void *pPa
11bf5 79 6c 6f 61 64 2c 20 20 20 20 20 20 20 20 20 20  yload,          
11bf6 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70   /* Pointer to p
11bf7 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 76 6f  age data */.  vo
11bf8 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20  id *pBuf,       
11bf9 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
11bfa 65 72 20 74 6f 20 62 75 66 66 65 72 20 2a 2f 0a  er to buffer */.
11bfb 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20    int nByte,    
11bfc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
11bfd 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
11bfe 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 69 6e 74 20  o copy */.  int 
11bff 65 4f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20  eOp,            
11c00 20 20 20 20 20 20 2f 2a 20 30 20 2d 3e 20 63 6f        /* 0 -> co
11c01 70 79 20 66 72 6f 6d 20 70 61 67 65 2c 20 31 20  py from page, 1 
11c02 2d 3e 20 63 6f 70 79 20 74 6f 20 70 61 67 65 20  -> copy to page 
11c03 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62  */.  DbPage *pDb
11c04 50 61 67 65 20 20 20 20 20 20 20 20 20 20 20 2f  Page           /
11c05 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e  * Page containin
11c06 67 20 70 50 61 79 6c 6f 61 64 20 2a 2f 0a 29 7b  g pPayload */.){
11c07 0a 20 20 69 66 28 20 65 4f 70 20 29 7b 0a 20 20  .  if( eOp ){.  
11c08 20 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 20 66    /* Copy data f
11c09 72 6f 6d 20 62 75 66 66 65 72 20 74 6f 20 70 61  rom buffer to pa
11c0a 67 65 20 28 61 20 77 72 69 74 65 20 6f 70 65 72  ge (a write oper
11c0b 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 69 6e  ation) */.    in
11c0c 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61  t rc = sqlite3Pa
11c0d 67 65 72 57 72 69 74 65 28 70 44 62 50 61 67 65  gerWrite(pDbPage
11c0e 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
11c0f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
11c10 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
11c11 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50   }.    memcpy(pP
11c12 61 79 6c 6f 61 64 2c 20 70 42 75 66 2c 20 6e 42  ayload, pBuf, nB
11c13 79 74 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  yte);.  }else{. 
11c14 20 20 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 20     /* Copy data 
11c15 66 72 6f 6d 20 70 61 67 65 20 74 6f 20 62 75 66  from page to buf
11c16 66 65 72 20 28 61 20 72 65 61 64 20 6f 70 65 72  fer (a read oper
11c17 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 6d 65  ation) */.    me
11c18 6d 63 70 79 28 70 42 75 66 2c 20 70 50 61 79 6c  mcpy(pBuf, pPayl
11c19 6f 61 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d  oad, nByte);.  }
11c1a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
11c1b 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  _OK;.}../*.** Th
11c1c 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
11c1d 73 65 64 20 74 6f 20 72 65 61 64 20 6f 72 20 6f  sed to read or o
11c1e 76 65 72 77 72 69 74 65 20 70 61 79 6c 6f 61 64  verwrite payload
11c1f 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20   information.** 
11c20 66 6f 72 20 74 68 65 20 65 6e 74 72 79 20 74 68  for the entry th
11c21 61 74 20 74 68 65 20 70 43 75 72 20 63 75 72 73  at the pCur curs
11c22 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74  or is pointing t
11c23 6f 2e 20 49 66 20 74 68 65 20 65 4f 70 0a 2a 2a  o. If the eOp.**
11c24 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 2c   parameter is 0,
11c25 20 74 68 69 73 20 69 73 20 61 20 72 65 61 64 20   this is a read 
11c26 6f 70 65 72 61 74 69 6f 6e 20 28 64 61 74 61 20  operation (data 
11c27 63 6f 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 62  copied into.** b
11c28 75 66 66 65 72 20 70 42 75 66 29 2e 20 49 66 20  uffer pBuf). If 
11c29 69 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  it is non-zero, 
11c2a 61 20 77 72 69 74 65 20 28 64 61 74 61 20 63 6f  a write (data co
11c2b 70 69 65 64 20 66 72 6f 6d 0a 2a 2a 20 62 75 66  pied from.** buf
11c2c 66 65 72 20 70 42 75 66 29 2e 0a 2a 2a 0a 2a 2a  fer pBuf)..**.**
11c2d 20 41 20 74 6f 74 61 6c 20 6f 66 20 22 61 6d 74   A total of "amt
11c2e 22 20 62 79 74 65 73 20 61 72 65 20 72 65 61 64  " bytes are read
11c2f 20 6f 72 20 77 72 69 74 74 65 6e 20 62 65 67 69   or written begi
11c30 6e 6e 69 6e 67 20 61 74 20 22 6f 66 66 73 65 74  nning at "offset
11c31 22 2e 0a 2a 2a 20 44 61 74 61 20 69 73 20 72 65  "..** Data is re
11c32 61 64 20 74 6f 20 6f 72 20 66 72 6f 6d 20 74 68  ad to or from th
11c33 65 20 62 75 66 66 65 72 20 70 42 75 66 2e 0a 2a  e buffer pBuf..*
11c34 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
11c35 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 6b 65 20  e does not make 
11c36 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 20 62 65  a distinction be
11c37 74 77 65 65 6e 20 6b 65 79 20 61 6e 64 20 64 61  tween key and da
11c38 74 61 2e 0a 2a 2a 20 49 74 20 6a 75 73 74 20 72  ta..** It just r
11c39 65 61 64 73 20 6f 72 20 77 72 69 74 65 73 20 62  eads or writes b
11c3a 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 70 61  ytes from the pa
11c3b 79 6c 6f 61 64 20 61 72 65 61 2e 20 20 44 61 74  yload area.  Dat
11c3c 61 20 6d 69 67 68 74 20 0a 2a 2a 20 61 70 70 65  a might .** appe
11c3d 61 72 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 70  ar on the main p
11c3e 61 67 65 20 6f 72 20 62 65 20 73 63 61 74 74 65  age or be scatte
11c3f 72 65 64 20 6f 75 74 20 6f 6e 20 6d 75 6c 74 69  red out on multi
11c40 70 6c 65 20 6f 76 65 72 66 6c 6f 77 20 0a 2a 2a  ple overflow .**
11c41 20 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66   pages..**.** If
11c42 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e 69 73   the BtCursor.is
11c43 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 66  IncrblobHandle f
11c44 6c 61 67 20 69 73 20 73 65 74 2c 20 61 6e 64 20  lag is set, and 
11c45 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 63  the current.** c
11c46 75 72 73 6f 72 20 65 6e 74 72 79 20 75 73 65 73  ursor entry uses
11c47 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 76 65   one or more ove
11c48 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 74 68 69  rflow pages, thi
11c49 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 6c  s function.** al
11c4a 6c 6f 63 61 74 65 73 20 73 70 61 63 65 20 66 6f  locates space fo
11c4b 72 20 61 6e 64 20 6c 61 7a 69 6c 79 20 70 6f 70  r and lazily pop
11c4c 6c 75 61 74 65 73 20 74 68 65 20 6f 76 65 72 66  luates the overf
11c4d 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 0a 2a  low page-list .*
11c4e 2a 20 63 61 63 68 65 20 61 72 72 61 79 20 28 42  * cache array (B
11c4f 74 43 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f  tCursor.aOverflo
11c50 77 29 2e 20 53 75 62 73 65 71 75 65 6e 74 20 63  w). Subsequent c
11c51 61 6c 6c 73 20 75 73 65 20 74 68 69 73 0a 2a 2a  alls use this.**
11c52 20 63 61 63 68 65 20 74 6f 20 6d 61 6b 65 20 73   cache to make s
11c53 65 65 6b 69 6e 67 20 74 6f 20 74 68 65 20 73 75  eeking to the su
11c54 70 70 6c 69 65 64 20 6f 66 66 73 65 74 20 6d 6f  pplied offset mo
11c55 72 65 20 65 66 66 69 63 69 65 6e 74 2e 0a 2a 2a  re efficient..**
11c56 0a 2a 2a 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72  .** Once an over
11c57 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63  flow page-list c
11c58 61 63 68 65 20 68 61 73 20 62 65 65 6e 20 61 6c  ache has been al
11c59 6c 6f 63 61 74 65 64 2c 20 69 74 20 6d 61 79 20  located, it may 
11c5a 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65  be.** invalidate
11c5b 64 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20  d if some other 
11c5c 63 75 72 73 6f 72 20 77 72 69 74 65 73 20 74 6f  cursor writes to
11c5d 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2c   the same table,
11c5e 20 6f 72 20 69 66 0a 2a 2a 20 74 68 65 20 63 75   or if.** the cu
11c5f 72 73 6f 72 20 69 73 20 6d 6f 76 65 64 20 74 6f  rsor is moved to
11c60 20 61 20 64 69 66 66 65 72 65 6e 74 20 72 6f 77   a different row
11c61 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20  . Additionally, 
11c62 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a 2a  in auto-vacuum.*
11c63 2a 20 6d 6f 64 65 2c 20 74 68 65 20 66 6f 6c 6c  * mode, the foll
11c64 6f 77 69 6e 67 20 65 76 65 6e 74 73 20 6d 61 79  owing events may
11c65 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e 20 6f   invalidate an o
11c66 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73  verflow page-lis
11c67 74 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 20  t cache..**.**  
11c68 20 2a 20 41 6e 20 69 6e 63 72 65 6d 65 6e 74 61   * An incrementa
11c69 6c 20 76 61 63 75 75 6d 2c 0a 2a 2a 20 20 20 2a  l vacuum,.**   *
11c6a 20 41 20 63 6f 6d 6d 69 74 20 69 6e 20 61 75 74   A commit in aut
11c6b 6f 5f 76 61 63 75 75 6d 3d 22 66 75 6c 6c 22 20  o_vacuum="full" 
11c6c 6d 6f 64 65 2c 0a 2a 2a 20 20 20 2a 20 43 72 65  mode,.**   * Cre
11c6d 61 74 69 6e 67 20 61 20 74 61 62 6c 65 20 28 6d  ating a table (m
11c6e 61 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69 6e  ay require movin
11c6f 67 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61  g an overflow pa
11c70 67 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ge)..*/.static i
11c71 6e 74 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64  nt accessPayload
11c72 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  (.  BtCursor *pC
11c73 75 72 2c 20 20 20 20 20 20 2f 2a 20 43 75 72 73  ur,      /* Curs
11c74 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 65  or pointing to e
11c75 6e 74 72 79 20 74 6f 20 72 65 61 64 20 66 72 6f  ntry to read fro
11c76 6d 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65  m */.  int offse
11c77 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42  t,          /* B
11c78 65 67 69 6e 20 72 65 61 64 69 6e 67 20 74 68 69  egin reading thi
11c79 73 20 66 61 72 20 69 6e 74 6f 20 70 61 79 6c 6f  s far into paylo
11c7a 61 64 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c  ad */.  int amt,
11c7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11c7c 52 65 61 64 20 74 68 69 73 20 6d 61 6e 79 20 62  Read this many b
11c7d 79 74 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  ytes */.  unsign
11c7e 65 64 20 63 68 61 72 20 2a 70 42 75 66 2c 20 2f  ed char *pBuf, /
11c7f 2a 20 57 72 69 74 65 20 74 68 65 20 62 79 74 65  * Write the byte
11c80 73 20 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66  s into this buff
11c81 65 72 20 2a 2f 20 0a 20 20 69 6e 74 20 73 6b 69  er */ .  int ski
11c82 70 4b 65 79 2c 20 20 20 20 20 20 20 20 20 2f 2a  pKey,         /*
11c83 20 6f 66 66 73 65 74 20 62 65 67 69 6e 73 20 61   offset begins a
11c84 74 20 64 61 74 61 20 69 66 20 74 68 69 73 20 69  t data if this i
11c85 73 20 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20  s true */.  int 
11c86 65 4f 70 20 20 20 20 20 20 20 20 20 20 20 20 20  eOp             
11c87 20 2f 2a 20 7a 65 72 6f 20 74 6f 20 72 65 61 64   /* zero to read
11c88 2e 20 6e 6f 6e 2d 7a 65 72 6f 20 74 6f 20 77 72  . non-zero to wr
11c89 69 74 65 2e 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73  ite. */.){.  uns
11c8a 69 67 6e 65 64 20 63 68 61 72 20 2a 61 50 61 79  igned char *aPay
11c8b 6c 6f 61 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d  load;.  int rc =
11c8c 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 33   SQLITE_OK;.  u3
11c8d 32 20 6e 4b 65 79 3b 0a 20 20 69 6e 74 20 69 49  2 nKey;.  int iI
11c8e 64 78 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67  dx = 0;.  MemPag
11c8f 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d  e *pPage = pCur-
11c90 3e 70 50 61 67 65 3b 20 20 20 20 20 2f 2a 20 42  >pPage;     /* B
11c91 74 72 65 65 20 70 61 67 65 20 6f 66 20 63 75 72  tree page of cur
11c92 72 65 6e 74 20 63 75 72 73 6f 72 20 65 6e 74 72  rent cursor entr
11c93 79 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20  y */.  BtShared 
11c94 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20  *pBt;           
11c95 20 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65          /* Btree
11c96 20 74 68 69 73 20 63 75 72 73 6f 72 20 62 65 6c   this cursor bel
11c97 6f 6e 67 73 20 74 6f 20 2a 2f 0a 0a 20 20 61 73  ongs to */..  as
11c98 73 65 72 74 28 20 70 50 61 67 65 20 29 3b 0a 20  sert( pPage );. 
11c99 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65   assert( pCur->e
11c9a 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41  State==CURSOR_VA
11c9b 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28  LID );.  assert(
11c9c 20 70 43 75 72 2d 3e 69 64 78 3e 3d 30 20 26 26   pCur->idx>=0 &&
11c9d 20 70 43 75 72 2d 3e 69 64 78 3c 70 50 61 67 65   pCur->idx<pPage
11c9e 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 73  ->nCell );.  ass
11c9f 65 72 74 28 20 6f 66 66 73 65 74 3e 3d 30 20 29  ert( offset>=0 )
11ca0 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73  ;.  assert( curs
11ca1 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75  orHoldsMutex(pCu
11ca2 72 29 20 29 3b 0a 0a 20 20 67 65 74 43 65 6c 6c  r) );..  getCell
11ca3 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 61 50  Info(pCur);.  aP
11ca4 61 79 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69  ayload = pCur->i
11ca5 6e 66 6f 2e 70 43 65 6c 6c 20 2b 20 70 43 75 72  nfo.pCell + pCur
11ca6 2d 3e 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3b 0a  ->info.nHeader;.
11ca7 20 20 6e 4b 65 79 20 3d 20 28 70 50 61 67 65 2d    nKey = (pPage-
11ca8 3e 69 6e 74 4b 65 79 20 3f 20 30 20 3a 20 70 43  >intKey ? 0 : pC
11ca9 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 29 3b 0a  ur->info.nKey);.
11caa 0a 20 20 69 66 28 20 73 6b 69 70 4b 65 79 20 29  .  if( skipKey )
11cab 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20  {.    offset += 
11cac 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20  nKey;.  }.  if( 
11cad 6f 66 66 73 65 74 2b 61 6d 74 20 3e 20 6e 4b 65  offset+amt > nKe
11cae 79 2b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61  y+pCur->info.nDa
11caf 74 61 20 29 7b 0a 20 20 20 20 2f 2a 20 54 72 79  ta ){.    /* Try
11cb0 69 6e 67 20 74 6f 20 72 65 61 64 20 6f 72 20 77  ing to read or w
11cb1 72 69 74 65 20 70 61 73 74 20 74 68 65 20 65 6e  rite past the en
11cb2 64 20 6f 66 20 74 68 65 20 64 61 74 61 20 69 73  d of the data is
11cb3 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20   an error */.   
11cb4 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
11cb5 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  RROR;.  }..  /* 
11cb6 43 68 65 63 6b 20 69 66 20 64 61 74 61 20 6d 75  Check if data mu
11cb7 73 74 20 62 65 20 72 65 61 64 2f 77 72 69 74 74  st be read/writt
11cb8 65 6e 20 74 6f 2f 66 72 6f 6d 20 74 68 65 20 62  en to/from the b
11cb9 74 72 65 65 20 70 61 67 65 20 69 74 73 65 6c 66  tree page itself
11cba 2e 20 2a 2f 0a 20 20 69 66 28 20 6f 66 66 73 65  . */.  if( offse
11cbb 74 3c 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f  t<pCur->info.nLo
11cbc 63 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 61  cal ){.    int a
11cbd 20 3d 20 61 6d 74 3b 0a 20 20 20 20 69 66 28 20   = amt;.    if( 
11cbe 61 2b 6f 66 66 73 65 74 3e 70 43 75 72 2d 3e 69  a+offset>pCur->i
11cbf 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20  nfo.nLocal ){.  
11cc0 20 20 20 20 61 20 3d 20 70 43 75 72 2d 3e 69 6e      a = pCur->in
11cc1 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 6f 66 66 73  fo.nLocal - offs
11cc2 65 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  et;.    }.    rc
11cc3 20 3d 20 63 6f 70 79 50 61 79 6c 6f 61 64 28 26   = copyPayload(&
11cc4 61 50 61 79 6c 6f 61 64 5b 6f 66 66 73 65 74 5d  aPayload[offset]
11cc5 2c 20 70 42 75 66 2c 20 61 2c 20 65 4f 70 2c 20  , pBuf, a, eOp, 
11cc6 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b  pPage->pDbPage);
11cc7 0a 20 20 20 20 6f 66 66 73 65 74 20 3d 20 30 3b  .    offset = 0;
11cc8 0a 20 20 20 20 70 42 75 66 20 2b 3d 20 61 3b 0a  .    pBuf += a;.
11cc9 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b 0a 20 20      amt -= a;.  
11cca 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 66 66 73 65  }else{.    offse
11ccb 74 20 2d 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e  t -= pCur->info.
11ccc 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 0a 0a 20 20 70  nLocal;.  }..  p
11ccd 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a  Bt = pCur->pBt;.
11cce 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
11ccf 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b 0a  _OK && amt>0 ){.
11cd0 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 6f 76      const int ov
11cd1 66 6c 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73  flSize = pBt->us
11cd2 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 20 20 2f  ableSize - 4;  /
11cd3 2a 20 42 79 74 65 73 20 63 6f 6e 74 65 6e 74 20  * Bytes content 
11cd4 70 65 72 20 6f 76 66 6c 20 70 61 67 65 20 2a 2f  per ovfl page */
11cd5 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78 74 50 61  .    Pgno nextPa
11cd6 67 65 3b 0a 0a 20 20 20 20 6e 65 78 74 50 61 67  ge;..    nextPag
11cd7 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 50  e = get4byte(&aP
11cd8 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e 66  ayload[pCur->inf
11cd9 6f 2e 6e 4c 6f 63 61 6c 5d 29 3b 0a 0a 23 69 66  o.nLocal]);..#if
11cda 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
11cdb 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 2f 2a  _INCRBLOB.    /*
11cdc 20 49 66 20 74 68 65 20 69 73 49 6e 63 72 62 6c   If the isIncrbl
11cdd 6f 62 48 61 6e 64 6c 65 20 66 6c 61 67 20 69 73  obHandle flag is
11cde 20 73 65 74 20 61 6e 64 20 74 68 65 20 42 74 43   set and the BtC
11cdf 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 5b  ursor.aOverflow[
11ce0 5d 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e 6f 74  ].    ** has not
11ce1 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c   been allocated,
11ce2 20 61 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77   allocate it now
11ce3 2e 20 54 68 65 20 61 72 72 61 79 20 69 73 20 73  . The array is s
11ce4 69 7a 65 64 20 61 74 0a 20 20 20 20 2a 2a 20 6f  ized at.    ** o
11ce5 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63  ne entry for eac
11ce6 68 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  h overflow page 
11ce7 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  in the overflow 
11ce8 63 68 61 69 6e 2e 20 54 68 65 0a 20 20 20 20 2a  chain. The.    *
11ce9 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66  * page number of
11cea 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 66   the first overf
11ceb 6c 6f 77 20 70 61 67 65 20 69 73 20 73 74 6f 72  low page is stor
11cec 65 64 20 69 6e 20 61 4f 76 65 72 66 6c 6f 77 5b  ed in aOverflow[
11ced 30 5d 2c 0a 20 20 20 20 2a 2a 20 65 74 63 2e 20  0],.    ** etc. 
11cee 41 20 76 61 6c 75 65 20 6f 66 20 30 20 69 6e 20  A value of 0 in 
11cef 74 68 65 20 61 4f 76 65 72 66 6c 6f 77 5b 5d 20  the aOverflow[] 
11cf0 61 72 72 61 79 20 6d 65 61 6e 73 20 22 6e 6f 74  array means "not
11cf1 20 79 65 74 20 6b 6e 6f 77 6e 22 0a 20 20 20 20   yet known".    
11cf2 2a 2a 20 28 74 68 65 20 63 61 63 68 65 20 69 73  ** (the cache is
11cf3 20 6c 61 7a 69 6c 79 20 70 6f 70 75 6c 61 74 65   lazily populate
11cf4 64 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  d)..    */.    i
11cf5 66 28 20 70 43 75 72 2d 3e 69 73 49 6e 63 72 62  f( pCur->isIncrb
11cf6 6c 6f 62 48 61 6e 64 6c 65 20 26 26 20 21 70 43  lobHandle && !pC
11cf7 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b  ur->aOverflow ){
11cf8 0a 20 20 20 20 20 20 69 6e 74 20 6e 4f 76 66 6c  .      int nOvfl
11cf9 20 3d 20 28 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e   = (pCur->info.n
11cfa 50 61 79 6c 6f 61 64 2d 70 43 75 72 2d 3e 69 6e  Payload-pCur->in
11cfb 66 6f 2e 6e 4c 6f 63 61 6c 2b 6f 76 66 6c 53 69  fo.nLocal+ovflSi
11cfc 7a 65 2d 31 29 2f 6f 76 66 6c 53 69 7a 65 3b 0a  ze-1)/ovflSize;.
11cfd 20 20 20 20 20 20 70 43 75 72 2d 3e 61 4f 76 65        pCur->aOve
11cfe 72 66 6c 6f 77 20 3d 20 28 50 67 6e 6f 20 2a 29  rflow = (Pgno *)
11cff 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72  sqlite3MallocZer
11d00 6f 28 73 69 7a 65 6f 66 28 50 67 6e 6f 29 2a 6e  o(sizeof(Pgno)*n
11d01 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 69 66 28  Ovfl);.      if(
11d02 20 6e 4f 76 66 6c 20 26 26 20 21 70 43 75 72 2d   nOvfl && !pCur-
11d03 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20  >aOverflow ){.  
11d04 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
11d05 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d  E_NOMEM;.      }
11d06 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49  .    }..    /* I
11d07 66 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70  f the overflow p
11d08 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20 68  age-list cache h
11d09 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65  as been allocate
11d0a 64 20 61 6e 64 20 74 68 65 0a 20 20 20 20 2a 2a  d and the.    **
11d0b 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 66   entry for the f
11d0c 69 72 73 74 20 72 65 71 75 69 72 65 64 20 6f 76  irst required ov
11d0d 65 72 66 6c 6f 77 20 70 61 67 65 20 69 73 20 76  erflow page is v
11d0e 61 6c 69 64 2c 20 73 6b 69 70 0a 20 20 20 20 2a  alid, skip.    *
11d0f 2a 20 64 69 72 65 63 74 6c 79 20 74 6f 20 69 74  * directly to it
11d10 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
11d11 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77   pCur->aOverflow
11d12 20 26 26 20 70 43 75 72 2d 3e 61 4f 76 65 72 66   && pCur->aOverf
11d13 6c 6f 77 5b 6f 66 66 73 65 74 2f 6f 76 66 6c 53  low[offset/ovflS
11d14 69 7a 65 5d 20 29 7b 0a 20 20 20 20 20 20 69 49  ize] ){.      iI
11d15 64 78 20 3d 20 28 6f 66 66 73 65 74 2f 6f 76 66  dx = (offset/ovf
11d16 6c 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 6e 65  lSize);.      ne
11d17 78 74 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61  xtPage = pCur->a
11d18 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 3b 0a  Overflow[iIdx];.
11d19 20 20 20 20 20 20 6f 66 66 73 65 74 20 3d 20 28        offset = (
11d1a 6f 66 66 73 65 74 25 6f 76 66 6c 53 69 7a 65 29  offset%ovflSize)
11d1b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  ;.    }.#endif..
11d1c 20 20 20 20 66 6f 72 28 20 3b 20 72 63 3d 3d 53      for( ; rc==S
11d1d 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e  QLITE_OK && amt>
11d1e 30 20 26 26 20 6e 65 78 74 50 61 67 65 3b 20 69  0 && nextPage; i
11d1f 49 64 78 2b 2b 29 7b 0a 0a 23 69 66 6e 64 65 66  Idx++){..#ifndef
11d20 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43   SQLITE_OMIT_INC
11d21 52 42 4c 4f 42 0a 20 20 20 20 20 20 2f 2a 20 49  RBLOB.      /* I
11d22 66 20 72 65 71 75 69 72 65 64 2c 20 70 6f 70 75  f required, popu
11d23 6c 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f  late the overflo
11d24 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68  w page-list cach
11d25 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  e. */.      if( 
11d26 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20  pCur->aOverflow 
11d27 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ){.        asser
11d28 74 28 21 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c  t(!pCur->aOverfl
11d29 6f 77 5b 69 49 64 78 5d 20 7c 7c 20 70 43 75 72  ow[iIdx] || pCur
11d2a 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78  ->aOverflow[iIdx
11d2b 5d 3d 3d 6e 65 78 74 50 61 67 65 29 3b 0a 20 20  ]==nextPage);.  
11d2c 20 20 20 20 20 20 70 43 75 72 2d 3e 61 4f 76 65        pCur->aOve
11d2d 72 66 6c 6f 77 5b 69 49 64 78 5d 20 3d 20 6e 65  rflow[iIdx] = ne
11d2e 78 74 50 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a  xtPage;.      }.
11d2f 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 69 66  #endif..      if
11d30 28 20 6f 66 66 73 65 74 3e 3d 6f 76 66 6c 53 69  ( offset>=ovflSi
11d31 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  ze ){.        /*
11d32 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 73 6f 6e   The only reason
11d33 20 74 6f 20 72 65 61 64 20 74 68 69 73 20 70 61   to read this pa
11d34 67 65 20 69 73 20 74 6f 20 6f 62 74 61 69 6e 20  ge is to obtain 
11d35 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 20  the page.       
11d36 20 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74   ** number for t
11d37 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20  he next page in 
11d38 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 61  the overflow cha
11d39 69 6e 2e 20 54 68 65 20 70 61 67 65 0a 20 20 20  in. The page.   
11d3a 20 20 20 20 20 2a 2a 20 64 61 74 61 20 69 73 20       ** data is 
11d3b 6e 6f 74 20 72 65 71 75 69 72 65 64 2e 20 53 6f  not required. So
11d3c 20 66 69 72 73 74 20 74 72 79 20 74 6f 20 6c 6f   first try to lo
11d3d 6f 6b 75 70 20 74 68 65 20 6f 76 65 72 66 6c 6f  okup the overflo
11d3e 77 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67  w.        ** pag
11d3f 65 2d 6c 69 73 74 20 63 61 63 68 65 2c 20 69 66  e-list cache, if
11d40 20 61 6e 79 2c 20 74 68 65 6e 20 66 61 6c 6c 20   any, then fall 
11d41 62 61 63 6b 20 74 6f 20 74 68 65 20 67 65 74 4f  back to the getO
11d42 76 65 72 66 6c 6f 77 50 61 67 65 28 29 0a 20 20  verflowPage().  
11d43 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f        ** functio
11d44 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 23 69  n..        */.#i
11d45 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
11d46 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20  T_INCRBLOB.     
11d47 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76     if( pCur->aOv
11d48 65 72 66 6c 6f 77 20 26 26 20 70 43 75 72 2d 3e  erflow && pCur->
11d49 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31  aOverflow[iIdx+1
11d4a 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  ] ){.          n
11d4b 65 78 74 50 61 67 65 20 3d 20 70 43 75 72 2d 3e  extPage = pCur->
11d4c 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31  aOverflow[iIdx+1
11d4d 5d 3b 0a 20 20 20 20 20 20 20 20 7d 20 65 6c 73  ];.        } els
11d4e 65 20 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  e .#endif.      
11d4f 20 20 20 20 72 63 20 3d 20 67 65 74 4f 76 65 72      rc = getOver
11d50 66 6c 6f 77 50 61 67 65 28 70 42 74 2c 20 6e 65  flowPage(pBt, ne
11d51 78 74 50 61 67 65 2c 20 30 2c 20 26 6e 65 78 74  xtPage, 0, &next
11d52 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 6f  Page);.        o
11d53 66 66 73 65 74 20 2d 3d 20 6f 76 66 6c 53 69 7a  ffset -= ovflSiz
11d54 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  e;.      }else{.
11d55 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 65 64 20          /* Need 
11d56 74 6f 20 72 65 61 64 20 74 68 69 73 20 70 61 67  to read this pag
11d57 65 20 70 72 6f 70 65 72 6c 79 2e 20 49 74 20 63  e properly. It c
11d58 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 20 6f 66 20  ontains some of 
11d59 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 72  the.        ** r
11d5a 61 6e 67 65 20 6f 66 20 64 61 74 61 20 74 68 61  ange of data tha
11d5b 74 20 69 73 20 62 65 69 6e 67 20 72 65 61 64 20  t is being read 
11d5c 28 65 4f 70 3d 3d 30 29 20 6f 72 20 77 72 69 74  (eOp==0) or writ
11d5d 74 65 6e 20 28 65 4f 70 21 3d 30 29 2e 0a 20 20  ten (eOp!=0)..  
11d5e 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
11d5f 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65   DbPage *pDbPage
11d60 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 61 20  ;.        int a 
11d61 3d 20 61 6d 74 3b 0a 20 20 20 20 20 20 20 20 72  = amt;.        r
11d62 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
11d63 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  Get(pBt->pPager,
11d64 20 6e 65 78 74 50 61 67 65 2c 20 26 70 44 62 50   nextPage, &pDbP
11d65 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66  age);.        if
11d66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
11d67 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 50 61  ){.          aPa
11d68 79 6c 6f 61 64 20 3d 20 73 71 6c 69 74 65 33 50  yload = sqlite3P
11d69 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50  agerGetData(pDbP
11d6a 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  age);.          
11d6b 6e 65 78 74 50 61 67 65 20 3d 20 67 65 74 34 62  nextPage = get4b
11d6c 79 74 65 28 61 50 61 79 6c 6f 61 64 29 3b 0a 20  yte(aPayload);. 
11d6d 20 20 20 20 20 20 20 20 20 69 66 28 20 61 20 2b           if( a +
11d6e 20 6f 66 66 73 65 74 20 3e 20 6f 76 66 6c 53 69   offset > ovflSi
11d6f 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ze ){.          
11d70 20 20 61 20 3d 20 6f 76 66 6c 53 69 7a 65 20 2d    a = ovflSize -
11d71 20 6f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 20   offset;.       
11d72 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 72     }.          r
11d73 63 20 3d 20 63 6f 70 79 50 61 79 6c 6f 61 64 28  c = copyPayload(
11d74 26 61 50 61 79 6c 6f 61 64 5b 6f 66 66 73 65 74  &aPayload[offset
11d75 2b 34 5d 2c 20 70 42 75 66 2c 20 61 2c 20 65 4f  +4], pBuf, a, eO
11d76 70 2c 20 70 44 62 50 61 67 65 29 3b 0a 20 20 20  p, pDbPage);.   
11d77 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
11d78 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 65  gerUnref(pDbPage
11d79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6f 66 66  );.          off
11d7a 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  set = 0;.       
11d7b 20 20 20 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 20     amt -= a;.   
11d7c 20 20 20 20 20 20 20 70 42 75 66 20 2b 3d 20 61         pBuf += a
11d7d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
11d7e 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
11d7f 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
11d80 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b 0a 20  OK && amt>0 ){. 
11d81 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
11d82 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
11d83 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
11d84 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 70 61  }../*.** Read pa
11d85 72 74 20 6f 66 20 74 68 65 20 6b 65 79 20 61 73  rt of the key as
11d86 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 63 75  sociated with cu
11d87 72 73 6f 72 20 70 43 75 72 2e 20 20 45 78 61 63  rsor pCur.  Exac
11d88 74 6c 79 0a 2a 2a 20 22 61 6d 74 22 20 62 79 74  tly.** "amt" byt
11d89 65 73 20 77 69 6c 6c 20 62 65 20 74 72 61 6e 73  es will be trans
11d8a 66 65 72 65 64 20 69 6e 74 6f 20 70 42 75 66 5b  fered into pBuf[
11d8b 5d 2e 20 20 54 68 65 20 74 72 61 6e 73 66 65 72  ].  The transfer
11d8c 0a 2a 2a 20 62 65 67 69 6e 73 20 61 74 20 22 6f  .** begins at "o
11d8d 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a 2a 20 52 65  ffset"..**.** Re
11d8e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f  turn SQLITE_OK o
11d8f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20  n success or an 
11d90 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e  error code if an
11d91 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77  ything goes.** w
11d92 72 6f 6e 67 2e 20 20 41 6e 20 65 72 72 6f 72 20  rong.  An error 
11d93 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 22  is returned if "
11d94 6f 66 66 73 65 74 2b 61 6d 74 22 20 69 73 20 6c  offset+amt" is l
11d95 61 72 67 65 72 20 74 68 61 6e 0a 2a 2a 20 74 68  arger than.** th
11d96 65 20 61 76 61 69 6c 61 62 6c 65 20 70 61 79 6c  e available payl
11d97 6f 61 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  oad..*/.SQLITE_P
11d98 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
11d99 65 33 42 74 72 65 65 4b 65 79 28 42 74 43 75 72  e3BtreeKey(BtCur
11d9a 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 6f  sor *pCur, u32 o
11d9b 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20  ffset, u32 amt, 
11d9c 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a 20 20 69  void *pBuf){.  i
11d9d 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74  nt rc;..  assert
11d9e 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
11d9f 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63  ex(pCur) );.  rc
11da0 20 3d 20 72 65 73 74 6f 72 65 4f 72 43 6c 65 61   = restoreOrClea
11da1 72 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  rCursorPosition(
11da2 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 3d  pCur);.  if( rc=
11da3 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
11da4 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
11da5 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56  eState==CURSOR_V
11da6 41 4c 49 44 20 29 3b 0a 20 20 20 20 61 73 73 65  ALID );.    asse
11da7 72 74 28 20 70 43 75 72 2d 3e 70 50 61 67 65 21  rt( pCur->pPage!
11da8 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 43  =0 );.    if( pC
11da9 75 72 2d 3e 70 50 61 67 65 2d 3e 69 6e 74 4b 65  ur->pPage->intKe
11daa 79 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  y ){.      retur
11dab 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
11dac 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20  _BKPT;.    }.   
11dad 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70   assert( pCur->p
11dae 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3d 3d 30 20  Page->intKey==0 
11daf 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
11db0 43 75 72 2d 3e 69 64 78 3e 3d 30 20 26 26 20 70  Cur->idx>=0 && p
11db1 43 75 72 2d 3e 69 64 78 3c 70 43 75 72 2d 3e 70  Cur->idx<pCur->p
11db2 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20  Page->nCell );. 
11db3 20 20 20 72 63 20 3d 20 61 63 63 65 73 73 50 61     rc = accessPa
11db4 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66 66 73  yload(pCur, offs
11db5 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 6e  et, amt, (unsign
11db6 65 64 20 63 68 61 72 2a 29 70 42 75 66 2c 20 30  ed char*)pBuf, 0
11db7 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , 0);.  }.  retu
11db8 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
11db9 52 65 61 64 20 70 61 72 74 20 6f 66 20 74 68 65  Read part of the
11dba 20 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 64   data associated
11dbb 20 77 69 74 68 20 63 75 72 73 6f 72 20 70 43 75   with cursor pCu
11dbc 72 2e 20 20 45 78 61 63 74 6c 79 0a 2a 2a 20 22  r.  Exactly.** "
11dbd 61 6d 74 22 20 62 79 74 65 73 20 77 69 6c 6c 20  amt" bytes will 
11dbe 62 65 20 74 72 61 6e 73 66 65 72 65 64 20 69 6e  be transfered in
11dbf 74 6f 20 70 42 75 66 5b 5d 2e 20 20 54 68 65 20  to pBuf[].  The 
11dc0 74 72 61 6e 73 66 65 72 0a 2a 2a 20 62 65 67 69  transfer.** begi
11dc1 6e 73 20 61 74 20 22 6f 66 66 73 65 74 22 2e 0a  ns at "offset"..
11dc2 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c  **.** Return SQL
11dc3 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73  ITE_OK on succes
11dc4 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f  s or an error co
11dc5 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67  de if anything g
11dc6 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41  oes.** wrong.  A
11dc7 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72  n error is retur
11dc8 6e 65 64 20 69 66 20 22 6f 66 66 73 65 74 2b 61  ned if "offset+a
11dc9 6d 74 22 20 69 73 20 6c 61 72 67 65 72 20 74 68  mt" is larger th
11dca 61 6e 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c 61  an.** the availa
11dcb 62 6c 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a  ble payload..*/.
11dcc 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
11dcd 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44  nt sqlite3BtreeD
11dce 61 74 61 28 42 74 43 75 72 73 6f 72 20 2a 70 43  ata(BtCursor *pC
11dcf 75 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20  ur, u32 offset, 
11dd0 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70  u32 amt, void *p
11dd1 42 75 66 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Buf){.  int rc;.
11dd2 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
11dd3 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
11dd4 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74  ) );.  rc = rest
11dd5 6f 72 65 4f 72 43 6c 65 61 72 43 75 72 73 6f 72  oreOrClearCursor
11dd6 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a  Position(pCur);.
11dd7 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
11dd8 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72  _OK ){.    asser
11dd9 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  t( pCur->eState=
11dda 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b  =CURSOR_VALID );
11ddb 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75  .    assert( pCu
11ddc 72 2d 3e 70 50 61 67 65 21 3d 30 20 29 3b 0a 20  r->pPage!=0 );. 
11ddd 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
11dde 3e 69 64 78 3e 3d 30 20 26 26 20 70 43 75 72 2d  >idx>=0 && pCur-
11ddf 3e 69 64 78 3c 70 43 75 72 2d 3e 70 50 61 67 65  >idx<pCur->pPage
11de0 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 72  ->nCell );.    r
11de1 63 20 3d 20 61 63 63 65 73 73 50 61 79 6c 6f 61  c = accessPayloa
11de2 64 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20  d(pCur, offset, 
11de3 61 6d 74 2c 20 70 42 75 66 2c 20 31 2c 20 30 29  amt, pBuf, 1, 0)
11de4 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
11de5 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  c;.}../*.** Retu
11de6 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
11de7 70 61 79 6c 6f 61 64 20 69 6e 66 6f 72 6d 61 74  payload informat
11de8 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 65 6e 74  ion from the ent
11de9 72 79 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20  ry that the .** 
11dea 70 43 75 72 20 63 75 72 73 6f 72 20 69 73 20 70  pCur cursor is p
11deb 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 65  ointing to.  The
11dec 20 70 6f 69 6e 74 65 72 20 69 73 20 74 6f 20 74   pointer is to t
11ded 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 0a  he beginning of.
11dee 2a 2a 20 74 68 65 20 6b 65 79 20 69 66 20 73 6b  ** the key if sk
11def 69 70 4b 65 79 3d 3d 30 20 61 6e 64 20 69 74 20  ipKey==0 and it 
11df0 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 62 65  points to the be
11df1 67 69 6e 6e 69 6e 67 20 6f 66 20 64 61 74 61 20  ginning of data 
11df2 69 66 0a 2a 2a 20 73 6b 69 70 4b 65 79 3d 3d 31  if.** skipKey==1
11df3 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66  .  The number of
11df4 20 62 79 74 65 73 20 6f 66 20 61 76 61 69 6c 61   bytes of availa
11df5 62 6c 65 20 6b 65 79 2f 64 61 74 61 20 69 73 20  ble key/data is 
11df6 77 72 69 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20  written.** into 
11df7 2a 70 41 6d 74 2e 20 20 49 66 20 2a 70 41 6d 74  *pAmt.  If *pAmt
11df8 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 76 61  ==0, then the va
11df9 6c 75 65 20 72 65 74 75 72 6e 65 64 20 77 69 6c  lue returned wil
11dfa 6c 20 6e 6f 74 20 62 65 0a 2a 2a 20 61 20 76 61  l not be.** a va
11dfb 6c 69 64 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a  lid pointer..**.
11dfc 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
11dfd 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69  is an optimizati
11dfe 6f 6e 2e 20 20 49 74 20 69 73 20 63 6f 6d 6d 6f  on.  It is commo
11dff 6e 20 66 6f 72 20 74 68 65 20 65 6e 74 69 72 65  n for the entire
11e00 20 6b 65 79 0a 2a 2a 20 61 6e 64 20 64 61 74 61   key.** and data
11e01 20 74 6f 20 66 69 74 20 6f 6e 20 74 68 65 20 6c   to fit on the l
11e02 6f 63 61 6c 20 70 61 67 65 20 61 6e 64 20 66 6f  ocal page and fo
11e03 72 20 74 68 65 72 65 20 74 6f 20 62 65 20 6e 6f  r there to be no
11e04 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70 61 67   overflow.** pag
11e05 65 73 2e 20 20 57 68 65 6e 20 74 68 61 74 20 69  es.  When that i
11e06 73 20 73 6f 2c 20 74 68 69 73 20 72 6f 75 74 69  s so, this routi
11e07 6e 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 74  ne can be used t
11e08 6f 20 61 63 63 65 73 73 20 74 68 65 0a 2a 2a 20  o access the.** 
11e09 6b 65 79 20 61 6e 64 20 64 61 74 61 20 77 69 74  key and data wit
11e0a 68 6f 75 74 20 6d 61 6b 69 6e 67 20 61 20 63 6f  hout making a co
11e0b 70 79 2e 20 20 49 66 20 74 68 65 20 6b 65 79 20  py.  If the key 
11e0c 61 6e 64 2f 6f 72 20 64 61 74 61 20 73 70 69 6c  and/or data spil
11e0d 6c 73 0a 2a 2a 20 6f 6e 74 6f 20 6f 76 65 72 66  ls.** onto overf
11e0e 6c 6f 77 20 70 61 67 65 73 2c 20 74 68 65 6e 20  low pages, then 
11e0f 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 29 20  accessPayload() 
11e10 6d 75 73 74 20 62 65 20 75 73 65 64 20 74 6f 20  must be used to 
11e11 72 65 61 73 73 65 6d 62 6c 79 0a 2a 2a 20 74 68  reassembly.** th
11e12 65 20 6b 65 79 2f 64 61 74 61 20 61 6e 64 20 63  e key/data and c
11e13 6f 70 79 20 69 74 20 69 6e 74 6f 20 61 20 70 72  opy it into a pr
11e14 65 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65  eallocated buffe
11e15 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69  r..**.** The poi
11e16 6e 74 65 72 20 72 65 74 75 72 6e 65 64 20 62 79  nter returned by
11e17 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6c 6f   this routine lo
11e18 6f 6b 73 20 64 69 72 65 63 74 6c 79 20 69 6e 74  oks directly int
11e19 6f 20 74 68 65 20 63 61 63 68 65 64 0a 2a 2a 20  o the cached.** 
11e1a 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61  page of the data
11e1b 62 61 73 65 2e 20 20 54 68 65 20 64 61 74 61 20  base.  The data 
11e1c 6d 69 67 68 74 20 63 68 61 6e 67 65 20 6f 72 20  might change or 
11e1d 6d 6f 76 65 20 74 68 65 20 6e 65 78 74 20 74 69  move the next ti
11e1e 6d 65 0a 2a 2a 20 61 6e 79 20 62 74 72 65 65 20  me.** any btree 
11e1f 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
11e20 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  d..*/.static con
11e21 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
11e22 20 2a 66 65 74 63 68 50 61 79 6c 6f 61 64 28 0a   *fetchPayload(.
11e23 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72    BtCursor *pCur
11e24 2c 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72  ,      /* Cursor
11e25 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 65 6e 74   pointing to ent
11e26 72 79 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20  ry to read from 
11e27 2a 2f 0a 20 20 69 6e 74 20 2a 70 41 6d 74 2c 20  */.  int *pAmt, 
11e28 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
11e29 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  te the number of
11e2a 20 61 76 61 69 6c 61 62 6c 65 20 62 79 74 65 73   available bytes
11e2b 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 73   here */.  int s
11e2c 6b 69 70 4b 65 79 20 20 20 20 20 20 20 20 20 20  kipKey          
11e2d 2f 2a 20 72 65 61 64 20 62 65 67 69 6e 6e 69 6e  /* read beginnin
11e2e 67 20 61 74 20 64 61 74 61 20 69 66 20 74 68 69  g at data if thi
11e2f 73 20 69 73 20 74 72 75 65 20 2a 2f 0a 29 7b 0a  s is true */.){.
11e30 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
11e31 2a 61 50 61 79 6c 6f 61 64 3b 0a 20 20 4d 65 6d  *aPayload;.  Mem
11e32 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 75  Page *pPage;.  u
11e33 33 32 20 6e 4b 65 79 3b 0a 20 20 69 6e 74 20 6e  32 nKey;.  int n
11e34 4c 6f 63 61 6c 3b 0a 0a 20 20 61 73 73 65 72 74  Local;..  assert
11e35 28 20 70 43 75 72 21 3d 30 20 26 26 20 70 43 75  ( pCur!=0 && pCu
11e36 72 2d 3e 70 50 61 67 65 21 3d 30 20 29 3b 0a 20  r->pPage!=0 );. 
11e37 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65   assert( pCur->e
11e38 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41  State==CURSOR_VA
11e39 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28  LID );.  assert(
11e3a 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
11e3b 78 28 70 43 75 72 29 20 29 3b 0a 20 20 70 50 61  x(pCur) );.  pPa
11e3c 67 65 20 3d 20 70 43 75 72 2d 3e 70 50 61 67 65  ge = pCur->pPage
11e3d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
11e3e 2d 3e 69 64 78 3e 3d 30 20 26 26 20 70 43 75 72  ->idx>=0 && pCur
11e3f 2d 3e 69 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65  ->idx<pPage->nCe
11e40 6c 6c 20 29 3b 0a 20 20 67 65 74 43 65 6c 6c 49  ll );.  getCellI
11e41 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 61 50 61  nfo(pCur);.  aPa
11e42 79 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69 6e  yload = pCur->in
11e43 66 6f 2e 70 43 65 6c 6c 3b 0a 20 20 61 50 61 79  fo.pCell;.  aPay
11e44 6c 6f 61 64 20 2b 3d 20 70 43 75 72 2d 3e 69 6e  load += pCur->in
11e45 66 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20 69 66  fo.nHeader;.  if
11e46 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20  ( pPage->intKey 
11e47 29 7b 0a 20 20 20 20 6e 4b 65 79 20 3d 20 30 3b  ){.    nKey = 0;
11e48 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 4b  .  }else{.    nK
11e49 65 79 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e  ey = pCur->info.
11e4a 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20  nKey;.  }.  if( 
11e4b 73 6b 69 70 4b 65 79 20 29 7b 0a 20 20 20 20 61  skipKey ){.    a
11e4c 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65 79 3b  Payload += nKey;
11e4d 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43  .    nLocal = pC
11e4e 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20  ur->info.nLocal 
11e4f 2d 20 6e 4b 65 79 3b 0a 20 20 7d 65 6c 73 65 7b  - nKey;.  }else{
11e50 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43  .    nLocal = pC
11e51 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b  ur->info.nLocal;
11e52 0a 20 20 20 20 69 66 28 20 6e 4c 6f 63 61 6c 3e  .    if( nLocal>
11e53 6e 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 6e 4c  nKey ){.      nL
11e54 6f 63 61 6c 20 3d 20 6e 4b 65 79 3b 0a 20 20 20  ocal = nKey;.   
11e55 20 7d 0a 20 20 7d 0a 20 20 2a 70 41 6d 74 20 3d   }.  }.  *pAmt =
11e56 20 6e 4c 6f 63 61 6c 3b 0a 20 20 72 65 74 75 72   nLocal;.  retur
11e57 6e 20 61 50 61 79 6c 6f 61 64 3b 0a 7d 0a 0a 0a  n aPayload;.}...
11e58 2f 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 65 6e  /*.** For the en
11e59 74 72 79 20 74 68 61 74 20 63 75 72 73 6f 72 20  try that cursor 
11e5a 70 43 75 72 20 69 73 20 70 6f 69 6e 74 20 74 6f  pCur is point to
11e5b 2c 20 72 65 74 75 72 6e 20 61 73 0a 2a 2a 20 6d  , return as.** m
11e5c 61 6e 79 20 62 79 74 65 73 20 6f 66 20 74 68 65  any bytes of the
11e5d 20 6b 65 79 20 6f 72 20 64 61 74 61 20 61 73 20   key or data as 
11e5e 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e  are available on
11e5f 20 74 68 65 20 6c 6f 63 61 6c 0a 2a 2a 20 62 2d   the local.** b-
11e60 74 72 65 65 20 70 61 67 65 2e 20 20 57 72 69 74  tree page.  Writ
11e61 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
11e62 61 76 61 69 6c 61 62 6c 65 20 62 79 74 65 73 20  available bytes 
11e63 69 6e 74 6f 20 2a 70 41 6d 74 2e 0a 2a 2a 0a 2a  into *pAmt..**.*
11e64 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 72 65  * The pointer re
11e65 74 75 72 6e 65 64 20 69 73 20 65 70 68 65 6d 65  turned is epheme
11e66 72 61 6c 2e 20 20 54 68 65 20 6b 65 79 2f 64 61  ral.  The key/da
11e67 74 61 20 6d 61 79 20 6d 6f 76 65 0a 2a 2a 20 6f  ta may move.** o
11e68 72 20 62 65 20 64 65 73 74 72 6f 79 65 64 20 6f  r be destroyed o
11e69 6e 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20  n the next call 
11e6a 74 6f 20 61 6e 79 20 42 74 72 65 65 20 72 6f 75  to any Btree rou
11e6b 74 69 6e 65 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69  tine,.** includi
11e6c 6e 67 20 63 61 6c 6c 73 20 66 72 6f 6d 20 6f 74  ng calls from ot
11e6d 68 65 72 20 74 68 72 65 61 64 73 20 61 67 61 69  her threads agai
11e6e 6e 73 74 20 74 68 65 20 73 61 6d 65 20 63 61 63  nst the same cac
11e6f 68 65 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 61 20  he..** Hence, a 
11e70 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 42 74 53  mutex on the BtS
11e71 68 61 72 65 64 20 73 68 6f 75 6c 64 20 62 65 20  hared should be 
11e72 68 65 6c 64 20 70 72 69 6f 72 20 74 6f 20 63 61  held prior to ca
11e73 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 72 6f  lling.** this ro
11e74 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  utine..**.** The
11e75 73 65 20 72 6f 75 74 69 6e 65 73 20 69 73 20 75  se routines is u
11e76 73 65 64 20 74 6f 20 67 65 74 20 71 75 69 63 6b  sed to get quick
11e77 20 61 63 63 65 73 73 20 74 6f 20 6b 65 79 20 61   access to key a
11e78 6e 64 20 64 61 74 61 0a 2a 2a 20 69 6e 20 74 68  nd data.** in th
11e79 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 68  e common case wh
11e7a 65 72 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 20  ere no overflow 
11e7b 70 61 67 65 73 20 61 72 65 20 75 73 65 64 2e 0a  pages are used..
11e7c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
11e7d 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  E const void *sq
11e7e 6c 69 74 65 33 42 74 72 65 65 4b 65 79 46 65 74  lite3BtreeKeyFet
11e7f 63 68 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  ch(BtCursor *pCu
11e80 72 2c 20 69 6e 74 20 2a 70 41 6d 74 29 7b 0a 20  r, int *pAmt){. 
11e81 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
11e82 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
11e83 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65  );.  if( pCur->e
11e84 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41  State==CURSOR_VA
11e85 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72  LID ){.    retur
11e86 6e 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 29 66  n (const void*)f
11e87 65 74 63 68 50 61 79 6c 6f 61 64 28 70 43 75 72  etchPayload(pCur
11e88 2c 20 70 41 6d 74 2c 20 30 29 3b 0a 20 20 7d 0a  , pAmt, 0);.  }.
11e89 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53 51    return 0;.}.SQ
11e8a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
11e8b 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
11e8c 42 74 72 65 65 44 61 74 61 46 65 74 63 68 28 42  BtreeDataFetch(B
11e8d 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69  tCursor *pCur, i
11e8e 6e 74 20 2a 70 41 6d 74 29 7b 0a 20 20 61 73 73  nt *pAmt){.  ass
11e8f 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
11e90 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
11e91 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74   if( pCur->eStat
11e92 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  e==CURSOR_VALID 
11e93 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 63  ){.    return (c
11e94 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63 68  onst void*)fetch
11e95 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 41  Payload(pCur, pA
11e96 6d 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65  mt, 1);.  }.  re
11e97 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  turn 0;.}.../*.*
11e98 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f  * Move the curso
11e99 72 20 64 6f 77 6e 20 74 6f 20 61 20 6e 65 77 20  r down to a new 
11e9a 63 68 69 6c 64 20 70 61 67 65 2e 20 20 54 68 65  child page.  The
11e9b 20 6e 65 77 50 67 6e 6f 20 61 72 67 75 6d 65 6e   newPgno argumen
11e9c 74 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 65  t is the.** page
11e9d 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 63   number of the c
11e9e 68 69 6c 64 20 70 61 67 65 20 74 6f 20 6d 6f 76  hild page to mov
11e9f 65 20 74 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  e to..*/.static 
11ea0 69 6e 74 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28  int moveToChild(
11ea1 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
11ea2 75 33 32 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 20  u32 newPgno){.  
11ea3 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67  int rc;.  MemPag
11ea4 65 20 2a 70 4e 65 77 50 61 67 65 3b 0a 20 20 4d  e *pNewPage;.  M
11ea5 65 6d 50 61 67 65 20 2a 70 4f 6c 64 50 61 67 65  emPage *pOldPage
11ea6 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
11ea7 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 0a  t = pCur->pBt;..
11ea8 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
11ea9 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
11eaa 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
11eab 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
11eac 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 72 63  OR_VALID );.  rc
11ead 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67   = getAndInitPag
11eae 65 28 70 42 74 2c 20 6e 65 77 50 67 6e 6f 2c 20  e(pBt, newPgno, 
11eaf 26 70 4e 65 77 50 61 67 65 2c 20 70 43 75 72 2d  &pNewPage, pCur-
11eb0 3e 70 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72  >pPage);.  if( r
11eb1 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
11eb2 20 70 4e 65 77 50 61 67 65 2d 3e 69 64 78 50 61   pNewPage->idxPa
11eb3 72 65 6e 74 20 3d 20 70 43 75 72 2d 3e 69 64 78  rent = pCur->idx
11eb4 3b 0a 20 20 70 4f 6c 64 50 61 67 65 20 3d 20 70  ;.  pOldPage = p
11eb5 43 75 72 2d 3e 70 50 61 67 65 3b 0a 20 20 70 4f  Cur->pPage;.  pO
11eb6 6c 64 50 61 67 65 2d 3e 69 64 78 53 68 69 66 74  ldPage->idxShift
11eb7 20 3d 20 30 3b 0a 20 20 72 65 6c 65 61 73 65 50   = 0;.  releaseP
11eb8 61 67 65 28 70 4f 6c 64 50 61 67 65 29 3b 0a 20  age(pOldPage);. 
11eb9 20 70 43 75 72 2d 3e 70 50 61 67 65 20 3d 20 70   pCur->pPage = p
11eba 4e 65 77 50 61 67 65 3b 0a 20 20 70 43 75 72 2d  NewPage;.  pCur-
11ebb 3e 69 64 78 20 3d 20 30 3b 0a 20 20 70 43 75 72  >idx = 0;.  pCur
11ebc 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30  ->info.nSize = 0
11ebd 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e  ;.  pCur->validN
11ebe 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 70  Key = 0;.  if( p
11ebf 4e 65 77 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 31  NewPage->nCell<1
11ec0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
11ec1 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
11ec2 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  PT;.  }.  return
11ec3 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
11ec4 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
11ec5 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20   if the page is 
11ec6 74 68 65 20 76 69 72 74 75 61 6c 20 72 6f 6f 74  the virtual root
11ec7 20 6f 66 20 69 74 73 20 74 61 62 6c 65 2e 0a 2a   of its table..*
11ec8 2a 0a 2a 2a 20 54 68 65 20 76 69 72 74 75 61 6c  *.** The virtual
11ec9 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20 74 68   root page is th
11eca 65 20 72 6f 6f 74 20 70 61 67 65 20 66 6f 72 20  e root page for 
11ecb 6d 6f 73 74 20 74 61 62 6c 65 73 2e 20 20 42 75  most tables.  Bu
11ecc 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62  t.** for the tab
11ecd 6c 65 20 72 6f 6f 74 65 64 20 6f 6e 20 70 61 67  le rooted on pag
11ece 65 20 31 2c 20 73 6f 6d 65 74 69 6d 65 20 74 68  e 1, sometime th
11ecf 65 20 72 65 61 6c 20 72 6f 6f 74 20 70 61 67 65  e real root page
11ed0 0a 2a 2a 20 69 73 20 65 6d 70 74 79 20 65 78 63  .** is empty exc
11ed1 65 70 74 20 66 6f 72 20 74 68 65 20 72 69 67 68  ept for the righ
11ed2 74 2d 70 6f 69 6e 74 65 72 2e 20 20 49 6e 20 73  t-pointer.  In s
11ed3 75 63 68 20 63 61 73 65 73 20 74 68 65 0a 2a 2a  uch cases the.**
11ed4 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61   virtual root pa
11ed5 67 65 20 69 73 20 74 68 65 20 70 61 67 65 20 74  ge is the page t
11ed6 68 61 74 20 74 68 65 20 72 69 67 68 74 2d 70 6f  hat the right-po
11ed7 69 6e 74 65 72 20 6f 66 20 70 61 67 65 0a 2a 2a  inter of page.**
11ed8 20 31 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74   1 is pointing t
11ed9 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  o..*/.SQLITE_PRI
11eda 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
11edb 42 74 72 65 65 49 73 52 6f 6f 74 50 61 67 65 28  BtreeIsRootPage(
11edc 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b  MemPage *pPage){
11edd 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 72  .  MemPage *pPar
11ede 65 6e 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ent;..  assert( 
11edf 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
11ee0 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d  ld(pPage->pBt->m
11ee1 75 74 65 78 29 20 29 3b 0a 20 20 70 50 61 72 65  utex) );.  pPare
11ee2 6e 74 20 3d 20 70 50 61 67 65 2d 3e 70 50 61 72  nt = pPage->pPar
11ee3 65 6e 74 3b 0a 20 20 69 66 28 20 70 50 61 72 65  ent;.  if( pPare
11ee4 6e 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31  nt==0 ) return 1
11ee5 3b 0a 20 20 69 66 28 20 70 50 61 72 65 6e 74 2d  ;.  if( pParent-
11ee6 3e 70 67 6e 6f 3e 31 20 29 20 72 65 74 75 72 6e  >pgno>1 ) return
11ee7 20 30 3b 0a 20 20 69 66 28 20 67 65 74 32 62 79   0;.  if( get2by
11ee8 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 44 61  te(&pParent->aDa
11ee9 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f  ta[pParent->hdrO
11eea 66 66 73 65 74 2b 33 5d 29 3d 3d 30 20 29 20 72  ffset+3])==0 ) r
11eeb 65 74 75 72 6e 20 31 3b 0a 20 20 72 65 74 75 72  eturn 1;.  retur
11eec 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f  n 0;.}../*.** Mo
11eed 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 75 70  ve the cursor up
11eee 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20 70   to the parent p
11eef 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 72 2d  age..**.** pCur-
11ef0 3e 69 64 78 20 69 73 20 73 65 74 20 74 6f 20 74  >idx is set to t
11ef1 68 65 20 63 65 6c 6c 20 69 6e 64 65 78 20 74 68  he cell index th
11ef2 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
11ef3 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68  pointer.** to th
11ef4 65 20 70 61 67 65 20 77 65 20 61 72 65 20 63 6f  e page we are co
11ef5 6d 69 6e 67 20 66 72 6f 6d 2e 20 20 49 66 20 77  ming from.  If w
11ef6 65 20 61 72 65 20 63 6f 6d 69 6e 67 20 66 72 6f  e are coming fro
11ef7 6d 20 74 68 65 0a 2a 2a 20 72 69 67 68 74 2d 6d  m the.** right-m
11ef8 6f 73 74 20 63 68 69 6c 64 20 70 61 67 65 20 74  ost child page t
11ef9 68 65 6e 20 70 43 75 72 2d 3e 69 64 78 20 69 73  hen pCur->idx is
11efa 20 73 65 74 20 74 6f 20 6f 6e 65 20 6d 6f 72 65   set to one more
11efb 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 6c 61 72   than.** the lar
11efc 67 65 73 74 20 63 65 6c 6c 20 69 6e 64 65 78 2e  gest cell index.
11efd 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
11efe 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
11eff 74 72 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e 74  treeMoveToParent
11f00 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29  (BtCursor *pCur)
11f01 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  {.  MemPage *pPa
11f02 72 65 6e 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20  rent;.  MemPage 
11f03 2a 70 50 61 67 65 3b 0a 20 20 69 6e 74 20 69 64  *pPage;.  int id
11f04 78 50 61 72 65 6e 74 3b 0a 0a 20 20 61 73 73 65  xParent;..  asse
11f05 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  rt( cursorHoldsM
11f06 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20  utex(pCur) );.  
11f07 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53  assert( pCur->eS
11f08 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c  tate==CURSOR_VAL
11f09 49 44 20 29 3b 0a 20 20 70 50 61 67 65 20 3d 20  ID );.  pPage = 
11f0a 70 43 75 72 2d 3e 70 50 61 67 65 3b 0a 20 20 61  pCur->pPage;.  a
11f0b 73 73 65 72 74 28 20 70 50 61 67 65 21 3d 30 20  ssert( pPage!=0 
11f0c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 73 71  );.  assert( !sq
11f0d 6c 69 74 65 33 42 74 72 65 65 49 73 52 6f 6f 74  lite3BtreeIsRoot
11f0e 50 61 67 65 28 70 50 61 67 65 29 20 29 3b 0a 20  Page(pPage) );. 
11f0f 20 70 50 61 72 65 6e 74 20 3d 20 70 50 61 67 65   pParent = pPage
11f10 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 61 73 73  ->pParent;.  ass
11f11 65 72 74 28 20 70 50 61 72 65 6e 74 21 3d 30 20  ert( pParent!=0 
11f12 29 3b 0a 20 20 69 64 78 50 61 72 65 6e 74 20 3d  );.  idxParent =
11f13 20 70 50 61 67 65 2d 3e 69 64 78 50 61 72 65 6e   pPage->idxParen
11f14 74 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65  t;.  sqlite3Page
11f15 72 52 65 66 28 70 50 61 72 65 6e 74 2d 3e 70 44  rRef(pParent->pD
11f16 62 50 61 67 65 29 3b 0a 20 20 72 65 6c 65 61 73  bPage);.  releas
11f17 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
11f18 70 43 75 72 2d 3e 70 50 61 67 65 20 3d 20 70 50  pCur->pPage = pP
11f19 61 72 65 6e 74 3b 0a 20 20 70 43 75 72 2d 3e 69  arent;.  pCur->i
11f1a 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20  nfo.nSize = 0;. 
11f1b 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79   pCur->validNKey
11f1c 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
11f1d 70 50 61 72 65 6e 74 2d 3e 69 64 78 53 68 69 66  pParent->idxShif
11f1e 74 3d 3d 30 20 29 3b 0a 20 20 70 43 75 72 2d 3e  t==0 );.  pCur->
11f1f 69 64 78 20 3d 20 69 64 78 50 61 72 65 6e 74 3b  idx = idxParent;
11f20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74  .}../*.** Move t
11f21 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65  he cursor to the
11f22 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2f 0a 73 74   root page.*/.st
11f23 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52  atic int moveToR
11f24 6f 6f 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43  oot(BtCursor *pC
11f25 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  ur){.  MemPage *
11f26 70 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 20  pRoot;.  int rc 
11f27 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42  = SQLITE_OK;.  B
11f28 74 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e  tree *p = pCur->
11f29 70 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61 72  pBtree;.  BtShar
11f2a 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
11f2b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  ;..  assert( cur
11f2c 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
11f2d 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ur) );.  assert(
11f2e 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20   CURSOR_INVALID 
11f2f 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  < CURSOR_REQUIRE
11f30 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  SEEK );.  assert
11f31 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 20  ( CURSOR_VALID  
11f32 20 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52   < CURSOR_REQUIR
11f33 45 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72  ESEEK );.  asser
11f34 74 28 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20  t( CURSOR_FAULT 
11f35 20 20 3e 20 43 55 52 53 4f 52 5f 52 45 51 55 49    > CURSOR_REQUI
11f36 52 45 53 45 45 4b 20 29 3b 0a 20 20 69 66 28 20  RESEEK );.  if( 
11f37 70 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55  pCur->eState>=CU
11f38 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b  RSOR_REQUIRESEEK
11f39 20 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72   ){.    if( pCur
11f3a 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
11f3b 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 20 20  _FAULT ){.      
11f3c 72 65 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69  return pCur->ski
11f3d 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 6c 65  p;.    }.    cle
11f3e 61 72 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  arCursorPosition
11f3f 28 70 43 75 72 29 3b 0a 20 20 7d 0a 20 20 70 52  (pCur);.  }.  pR
11f40 6f 6f 74 20 3d 20 70 43 75 72 2d 3e 70 50 61 67  oot = pCur->pPag
11f41 65 3b 0a 20 20 69 66 28 20 70 52 6f 6f 74 20 26  e;.  if( pRoot &
11f42 26 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d 70  & pRoot->pgno==p
11f43 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 29 7b  Cur->pgnoRoot ){
11f44 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 52 6f  .    assert( pRo
11f45 6f 74 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20  ot->isInit );.  
11f46 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 0a  }else{.    if( .
11f47 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21        SQLITE_OK!
11f48 3d 28 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69  =(rc = getAndIni
11f49 74 50 61 67 65 28 70 42 74 2c 20 70 43 75 72 2d  tPage(pBt, pCur-
11f4a 3e 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f  >pgnoRoot, &pRoo
11f4b 74 2c 20 30 29 29 0a 20 20 20 20 29 7b 0a 20 20  t, 0)).    ){.  
11f4c 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65      pCur->eState
11f4d 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49   = CURSOR_INVALI
11f4e 44 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  D;.      return 
11f4f 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65  rc;.    }.    re
11f50 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e  leasePage(pCur->
11f51 70 50 61 67 65 29 3b 0a 20 20 20 20 70 43 75 72  pPage);.    pCur
11f52 2d 3e 70 50 61 67 65 20 3d 20 70 52 6f 6f 74 3b  ->pPage = pRoot;
11f53 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 69 64 78  .  }.  pCur->idx
11f54 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69 6e   = 0;.  pCur->in
11f55 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20  fo.nSize = 0;.  
11f56 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 30  pCur->atLast = 0
11f57 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e  ;.  pCur->validN
11f58 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 70  Key = 0;.  if( p
11f59 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26  Root->nCell==0 &
11f5a 26 20 21 70 52 6f 6f 74 2d 3e 6c 65 61 66 20 29  & !pRoot->leaf )
11f5b 7b 0a 20 20 20 20 50 67 6e 6f 20 73 75 62 70 61  {.    Pgno subpa
11f5c 67 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ge;.    assert( 
11f5d 70 52 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d 31 20 29  pRoot->pgno==1 )
11f5e 3b 0a 20 20 20 20 73 75 62 70 61 67 65 20 3d 20  ;.    subpage = 
11f5f 67 65 74 34 62 79 74 65 28 26 70 52 6f 6f 74 2d  get4byte(&pRoot-
11f60 3e 61 44 61 74 61 5b 70 52 6f 6f 74 2d 3e 68 64  >aData[pRoot->hd
11f61 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20  rOffset+8]);.   
11f62 20 61 73 73 65 72 74 28 20 73 75 62 70 61 67 65   assert( subpage
11f63 3e 30 20 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e  >0 );.    pCur->
11f64 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f  eState = CURSOR_
11f65 56 41 4c 49 44 3b 0a 20 20 20 20 72 63 20 3d 20  VALID;.    rc = 
11f66 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72  moveToChild(pCur
11f67 2c 20 73 75 62 70 61 67 65 29 3b 0a 20 20 7d 0a  , subpage);.  }.
11f68 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d    pCur->eState =
11f69 20 28 28 70 43 75 72 2d 3e 70 50 61 67 65 2d 3e   ((pCur->pPage->
11f6a 6e 43 65 6c 6c 3e 30 29 3f 43 55 52 53 4f 52 5f  nCell>0)?CURSOR_
11f6b 56 41 4c 49 44 3a 43 55 52 53 4f 52 5f 49 4e 56  VALID:CURSOR_INV
11f6c 41 4c 49 44 29 3b 0a 20 20 72 65 74 75 72 6e 20  ALID);.  return 
11f6d 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76  rc;.}../*.** Mov
11f6e 65 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77  e the cursor dow
11f6f 6e 20 74 6f 20 74 68 65 20 6c 65 66 74 2d 6d 6f  n to the left-mo
11f70 73 74 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65  st leaf entry be
11f71 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20 65 6e 74  neath the.** ent
11f72 72 79 20 74 6f 20 77 68 69 63 68 20 69 74 20 69  ry to which it i
11f73 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  s currently poin
11f74 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ting..**.** The 
11f75 6c 65 66 74 2d 6d 6f 73 74 20 6c 65 61 66 20 69  left-most leaf i
11f76 73 20 74 68 65 20 6f 6e 65 20 77 69 74 68 20 74  s the one with t
11f77 68 65 20 73 6d 61 6c 6c 65 73 74 20 6b 65 79 20  he smallest key 
11f78 2d 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 69  - the first.** i
11f79 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65  n ascending orde
11f7a 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
11f7b 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28   moveToLeftmost(
11f7c 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
11f7d 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20  .  Pgno pgno;.  
11f7e 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
11f7f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  OK;.  MemPage *p
11f80 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28  Page;..  assert(
11f81 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
11f82 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73  x(pCur) );.  ass
11f83 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74  ert( pCur->eStat
11f84 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  e==CURSOR_VALID 
11f85 29 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d  );.  while( rc==
11f86 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70  SQLITE_OK && !(p
11f87 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 70 50 61  Page = pCur->pPa
11f88 67 65 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  ge)->leaf ){.   
11f89 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69   assert( pCur->i
11f8a 64 78 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 69  dx>=0 && pCur->i
11f8b 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20  dx<pPage->nCell 
11f8c 29 3b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 65  );.    pgno = ge
11f8d 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28  t4byte(findCell(
11f8e 70 50 61 67 65 2c 20 70 43 75 72 2d 3e 69 64 78  pPage, pCur->idx
11f8f 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76  ));.    rc = mov
11f90 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 70  eToChild(pCur, p
11f91 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  gno);.  }.  retu
11f92 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
11f93 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20  Move the cursor 
11f94 64 6f 77 6e 20 74 6f 20 74 68 65 20 72 69 67 68  down to the righ
11f95 74 2d 6d 6f 73 74 20 6c 65 61 66 20 65 6e 74 72  t-most leaf entr
11f96 79 20 62 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a  y beneath the.**
11f97 20 70 61 67 65 20 74 6f 20 77 68 69 63 68 20 69   page to which i
11f98 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70  t is currently p
11f99 6f 69 6e 74 69 6e 67 2e 20 20 4e 6f 74 69 63 65  ointing.  Notice
11f9a 20 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 0a   the difference.
11f9b 2a 2a 20 62 65 74 77 65 65 6e 20 6d 6f 76 65 54  ** between moveT
11f9c 6f 4c 65 66 74 6d 6f 73 74 28 29 20 61 6e 64 20  oLeftmost() and 
11f9d 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28  moveToRightmost(
11f9e 29 2e 20 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f  ).  moveToLeftmo
11f9f 73 74 28 29 0a 2a 2a 20 66 69 6e 64 73 20 74 68  st().** finds th
11fa0 65 20 6c 65 66 74 2d 6d 6f 73 74 20 65 6e 74 72  e left-most entr
11fa1 79 20 62 65 6e 65 61 74 68 20 74 68 65 20 2a 65  y beneath the *e
11fa2 6e 74 72 79 2a 20 77 68 65 72 65 61 73 20 6d 6f  ntry* whereas mo
11fa3 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 29 0a  veToRightmost().
11fa4 2a 2a 20 66 69 6e 64 73 20 74 68 65 20 72 69 67  ** finds the rig
11fa5 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 62 65  ht-most entry be
11fa6 6e 65 61 74 68 20 74 68 65 20 2a 70 61 67 65 2a  neath the *page*
11fa7 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 69 67 68  ..**.** The righ
11fa8 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 69 73 20  t-most entry is 
11fa9 74 68 65 20 6f 6e 65 20 77 69 74 68 20 74 68 65  the one with the
11faa 20 6c 61 72 67 65 73 74 20 6b 65 79 20 2d 20 74   largest key - t
11fab 68 65 20 6c 61 73 74 0a 2a 2a 20 6b 65 79 20 69  he last.** key i
11fac 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65  n ascending orde
11fad 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
11fae 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74   moveToRightmost
11faf 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29  (BtCursor *pCur)
11fb0 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20  {.  Pgno pgno;. 
11fb1 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
11fb2 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  _OK;.  MemPage *
11fb3 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74  pPage;..  assert
11fb4 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
11fb5 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73  ex(pCur) );.  as
11fb6 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61  sert( pCur->eSta
11fb7 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
11fb8 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d   );.  while( rc=
11fb9 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28  =SQLITE_OK && !(
11fba 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 70 50  pPage = pCur->pP
11fbb 61 67 65 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  age)->leaf ){.  
11fbc 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74    pgno = get4byt
11fbd 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b  e(&pPage->aData[
11fbe 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
11fbf 2b 38 5d 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e  +8]);.    pCur->
11fc0 69 64 78 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65  idx = pPage->nCe
11fc1 6c 6c 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76  ll;.    rc = mov
11fc2 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 70  eToChild(pCur, p
11fc3 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  gno);.  }.  if( 
11fc4 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
11fc5 0a 20 20 20 20 70 43 75 72 2d 3e 69 64 78 20 3d  .    pCur->idx =
11fc6 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 2d 20   pPage->nCell - 
11fc7 31 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66  1;.    pCur->inf
11fc8 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20  o.nSize = 0;.   
11fc9 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79   pCur->validNKey
11fca 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 0;.  }.  retu
11fcb 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
11fcc 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72  ./* Move the cur
11fcd 73 6f 72 20 74 6f 20 74 68 65 20 66 69 72 73 74  sor to the first
11fce 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61   entry in the ta
11fcf 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c  ble.  Return SQL
11fd0 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63  ITE_OK.** on suc
11fd1 63 65 73 73 2e 20 20 53 65 74 20 2a 70 52 65 73  cess.  Set *pRes
11fd2 20 74 6f 20 30 20 69 66 20 74 68 65 20 63 75 72   to 0 if the cur
11fd3 73 6f 72 20 61 63 74 75 61 6c 6c 79 20 70 6f 69  sor actually poi
11fd4 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68 69 6e 67  nts to something
11fd5 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70 52 65 73  .** or set *pRes
11fd6 20 74 6f 20 31 20 69 66 20 74 68 65 20 74 61 62   to 1 if the tab
11fd7 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a  le is empty..*/.
11fd8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
11fd9 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46  nt sqlite3BtreeF
11fda 69 72 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70  irst(BtCursor *p
11fdb 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b  Cur, int *pRes){
11fdc 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73  .  int rc;..  as
11fdd 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
11fde 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
11fdf 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
11fe0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75  3_mutex_held(pCu
11fe1 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d  r->pBtree->db->m
11fe2 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20  utex) );.  rc = 
11fe3 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29  moveToRoot(pCur)
11fe4 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
11fe5 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28  TE_OK ){.    if(
11fe6 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
11fe7 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b  URSOR_INVALID ){
11fe8 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
11fe9 43 75 72 2d 3e 70 50 61 67 65 2d 3e 6e 43 65 6c  Cur->pPage->nCel
11fea 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a 70  l==0 );.      *p
11feb 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 72  Res = 1;.      r
11fec 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
11fed 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
11fee 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70 50  assert( pCur->pP
11fef 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a  age->nCell>0 );.
11ff0 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b        *pRes = 0;
11ff1 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65  .      rc = move
11ff2 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29  ToLeftmost(pCur)
11ff3 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
11ff4 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d  turn rc;.}../* M
11ff5 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74  ove the cursor t
11ff6 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79  o the last entry
11ff7 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20   in the table.  
11ff8 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
11ff9 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e 20  .** on success. 
11ffa 20 53 65 74 20 2a 70 52 65 73 20 74 6f 20 30 20   Set *pRes to 0 
11ffb 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 63  if the cursor ac
11ffc 74 75 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f  tually points to
11ffd 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72   something.** or
11ffe 20 73 65 74 20 2a 70 52 65 73 20 74 6f 20 31 20   set *pRes to 1 
11fff 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  if the table is 
12000 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  empty..*/.SQLITE
12001 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
12002 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 42 74  ite3BtreeLast(Bt
12003 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e  Cursor *pCur, in
12004 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20  t *pRes){.  int 
12005 72 63 3b 0a 20 0a 20 20 61 73 73 65 72 74 28 20  rc;. .  assert( 
12006 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
12007 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
12008 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
12009 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74  x_held(pCur->pBt
1200a 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  ree->db->mutex) 
1200b 29 3b 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f  );.  rc = moveTo
1200c 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 66  Root(pCur);.  if
1200d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1200e 29 7b 0a 20 20 20 20 69 66 28 20 43 55 52 53 4f  ){.    if( CURSO
1200f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d  R_INVALID==pCur-
12010 3e 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20 20  >eState ){.     
12011 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70   assert( pCur->p
12012 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29  Page->nCell==0 )
12013 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20  ;.      *pRes = 
12014 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  1;.    }else{.  
12015 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
12016 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
12017 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 20 20  _VALID );.      
12018 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20  *pRes = 0;.     
12019 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68   rc = moveToRigh
1201a 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20  tmost(pCur);.   
1201b 20 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70     getCellInfo(p
1201c 43 75 72 29 3b 0a 20 20 20 20 20 20 70 43 75 72  Cur);.      pCur
1201d 2d 3e 61 74 4c 61 73 74 20 3d 20 72 63 3d 3d 53  ->atLast = rc==S
1201e 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
1201f 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
12020 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20  .}../* Move the 
12021 63 75 72 73 6f 72 20 73 6f 20 74 68 61 74 20 69  cursor so that i
12022 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65  t points to an e
12023 6e 74 72 79 20 6e 65 61 72 20 74 68 65 20 6b 65  ntry near the ke
12024 79 20 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20  y .** specified 
12025 62 79 20 70 4b 65 79 2f 6e 4b 65 79 2f 70 55 6e  by pKey/nKey/pUn
12026 4b 65 79 2e 20 52 65 74 75 72 6e 20 61 20 73 75  Key. Return a su
12027 63 63 65 73 73 20 63 6f 64 65 2e 0a 2a 2a 0a 2a  ccess code..**.*
12028 2a 20 46 6f 72 20 49 4e 54 4b 45 59 20 74 61 62  * For INTKEY tab
12029 6c 65 73 2c 20 6f 6e 6c 79 20 74 68 65 20 6e 4b  les, only the nK
1202a 65 79 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  ey parameter is 
1202b 75 73 65 64 2e 20 20 70 4b 65 79 20 0a 2a 2a 20  used.  pKey .** 
1202c 61 6e 64 20 70 55 6e 4b 65 79 20 6d 75 73 74 20  and pUnKey must 
1202d 62 65 20 4e 55 4c 4c 2e 20 20 46 6f 72 20 69 6e  be NULL.  For in
1202e 64 65 78 20 74 61 62 6c 65 73 2c 20 65 69 74 68  dex tables, eith
1202f 65 72 20 70 55 6e 4b 65 79 0a 2a 2a 20 6d 75 73  er pUnKey.** mus
12030 74 20 70 6f 69 6e 74 20 74 6f 20 61 20 6b 65 79  t point to a key
12031 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61 64   that has alread
12032 79 20 62 65 65 6e 20 75 6e 70 61 63 6b 65 64 2c  y been unpacked,
12033 20 6f 72 20 65 6c 73 65 0a 2a 2a 20 70 4b 65 79   or else.** pKey
12034 2f 6e 4b 65 79 20 64 65 73 63 72 69 62 65 73 20  /nKey describes 
12035 61 20 62 6c 6f 62 20 63 6f 6e 74 61 69 6e 69 6e  a blob containin
12036 67 20 74 68 65 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a  g the key..**.**
12037 20 49 66 20 61 6e 20 65 78 61 63 74 20 6d 61 74   If an exact mat
12038 63 68 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c  ch is not found,
12039 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72   then the cursor
1203a 20 69 73 20 61 6c 77 61 79 73 0a 2a 2a 20 6c 65   is always.** le
1203b 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61  ft pointing at a
1203c 20 6c 65 61 66 20 70 61 67 65 20 77 68 69 63 68   leaf page which
1203d 20 77 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20   would hold the 
1203e 65 6e 74 72 79 20 69 66 20 69 74 0a 2a 2a 20 77  entry if it.** w
1203f 65 72 65 20 70 72 65 73 65 6e 74 2e 20 20 54 68  ere present.  Th
12040 65 20 63 75 72 73 6f 72 20 6d 69 67 68 74 20 70  e cursor might p
12041 6f 69 6e 74 20 74 6f 20 61 6e 20 65 6e 74 72 79  oint to an entry
12042 20 74 68 61 74 20 63 6f 6d 65 73 0a 2a 2a 20 62   that comes.** b
12043 65 66 6f 72 65 20 6f 72 20 61 66 74 65 72 20 74  efore or after t
12044 68 65 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  he key..**.** Th
12045 65 20 72 65 73 75 6c 74 20 6f 66 20 63 6f 6d 70  e result of comp
12046 61 72 69 6e 67 20 74 68 65 20 6b 65 79 20 77 69  aring the key wi
12047 74 68 20 74 68 65 20 65 6e 74 72 79 20 74 6f 20  th the entry to 
12048 77 68 69 63 68 20 74 68 65 0a 2a 2a 20 63 75 72  which the.** cur
12049 73 6f 72 20 69 73 20 77 72 69 74 74 65 6e 20 74  sor is written t
1204a 6f 20 2a 70 52 65 73 20 69 66 20 70 52 65 73 21  o *pRes if pRes!
1204b 3d 4e 55 4c 4c 2e 20 20 54 68 65 20 6d 65 61 6e  =NULL.  The mean
1204c 69 6e 67 20 6f 66 0a 2a 2a 20 74 68 69 73 20 76  ing of.** this v
1204d 61 6c 75 65 20 69 73 20 61 73 20 66 6f 6c 6c 6f  alue is as follo
1204e 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70  ws:.**.**     *p
1204f 52 65 73 3c 30 20 20 20 20 20 20 54 68 65 20 63  Res<0      The c
12050 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f  ursor is left po
12051 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74  inting at an ent
12052 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20  ry that.**      
12053 20 20 20 20 20 20 20 20 20 20 20 20 69 73 20 73              is s
12054 6d 61 6c 6c 65 72 20 74 68 61 6e 20 70 4b 65 79  maller than pKey
12055 20 6f 72 20 69 66 20 74 68 65 20 74 61 62 6c 65   or if the table
12056 20 69 73 20 65 6d 70 74 79 0a 2a 2a 20 20 20 20   is empty.**    
12057 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6e                an
12058 64 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  d the cursor is 
12059 74 68 65 72 65 66 6f 72 65 20 6c 65 66 74 20 70  therefore left p
1205a 6f 69 6e 74 20 74 6f 20 6e 6f 74 68 69 6e 67 2e  oint to nothing.
1205b 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73  .**.**     *pRes
1205c 3d 3d 30 20 20 20 20 20 54 68 65 20 63 75 72 73  ==0     The curs
1205d 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74  or is left point
1205e 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20  ing at an entry 
1205f 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  that.**         
12060 20 20 20 20 20 20 20 20 20 65 78 61 63 74 6c 79           exactly
12061 20 6d 61 74 63 68 65 73 20 70 4b 65 79 2e 0a 2a   matches pKey..*
12062 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3e 30  *.**     *pRes>0
12063 20 20 20 20 20 20 54 68 65 20 63 75 72 73 6f 72        The cursor
12064 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
12065 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68  g at an entry th
12066 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  at.**           
12067 20 20 20 20 20 20 20 69 73 20 6c 61 72 67 65 72         is larger
12068 20 74 68 61 6e 20 70 4b 65 79 2e 0a 2a 2a 0a 2a   than pKey..**.*
12069 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1206a 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
1206b 65 4d 6f 76 65 74 6f 28 0a 20 20 42 74 43 75 72  eMoveto(.  BtCur
1206c 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20  sor *pCur,      
1206d 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20    /* The cursor 
1206e 74 6f 20 62 65 20 6d 6f 76 65 64 20 2a 2f 0a 20  to be moved */. 
1206f 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
12070 79 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6b  y,      /* The k
12071 65 79 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20 69  ey content for i
12072 6e 64 69 63 65 73 2e 20 20 4e 6f 74 20 75 73 65  ndices.  Not use
12073 64 20 62 79 20 74 61 62 6c 65 73 20 2a 2f 0a 20  d by tables */. 
12074 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
12075 2a 70 55 6e 4b 65 79 2c 2f 2a 20 55 6e 70 61 63  *pUnKey,/* Unpac
12076 6b 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 70  ked version of p
12077 4b 65 79 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65  Key */.  i64 nKe
12078 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y,              
12079 2f 2a 20 53 69 7a 65 20 6f 66 20 70 4b 65 79 2e  /* Size of pKey.
1207a 20 20 4f 72 20 74 68 65 20 6b 65 79 20 66 6f 72    Or the key for
1207b 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 69 6e 74   tables */.  int
1207c 20 62 69 61 73 52 69 67 68 74 2c 20 20 20 20 20   biasRight,     
1207d 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20      /* If true, 
1207e 62 69 61 73 20 74 68 65 20 73 65 61 72 63 68 20  bias the search 
1207f 74 6f 20 74 68 65 20 68 69 67 68 20 65 6e 64 20  to the high end 
12080 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 20 20  */.  int *pRes  
12081 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
12082 65 61 72 63 68 20 72 65 73 75 6c 74 20 66 6c 61  earch result fla
12083 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  g */.){.  int rc
12084 3b 0a 20 20 63 68 61 72 20 61 53 70 61 63 65 5b  ;.  char aSpace[
12085 32 30 30 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28  200];..  assert(
12086 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
12087 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73  x(pCur) );.  ass
12088 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
12089 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42  ex_held(pCur->pB
1208a 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  tree->db->mutex)
1208b 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65   );..  /* If the
1208c 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65 61   cursor is alrea
1208d 64 79 20 70 6f 73 69 74 69 6f 6e 65 64 20 61 74  dy positioned at
1208e 20 74 68 65 20 70 6f 69 6e 74 20 77 65 20 61 72   the point we ar
1208f 65 20 74 72 79 69 6e 67 0a 20 20 2a 2a 20 74 6f  e trying.  ** to
12090 20 6d 6f 76 65 20 74 6f 2c 20 74 68 65 6e 20 6a   move to, then j
12091 75 73 74 20 72 65 74 75 72 6e 20 77 69 74 68 6f  ust return witho
12092 75 74 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f 72  ut doing any wor
12093 6b 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 72 2d  k */.  if( pCur-
12094 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
12095 56 41 4c 49 44 20 26 26 20 70 43 75 72 2d 3e 76  VALID && pCur->v
12096 61 6c 69 64 4e 4b 65 79 20 26 26 20 70 43 75 72  alidNKey && pCur
12097 2d 3e 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20  ->pPage->intKey 
12098 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d  ){.    if( pCur-
12099 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b 65 79  >info.nKey==nKey
1209a 20 29 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20   ){.      *pRes 
1209b 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72  = 0;.      retur
1209c 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
1209d 20 7d 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d   }.    if( pCur-
1209e 3e 61 74 4c 61 73 74 20 26 26 20 70 43 75 72 2d  >atLast && pCur-
1209f 3e 69 6e 66 6f 2e 6e 4b 65 79 3c 6e 4b 65 79 20  >info.nKey<nKey 
120a0 29 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d  ){.      *pRes =
120a1 20 2d 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72   -1;.      retur
120a2 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
120a3 20 7d 0a 20 20 7d 0a 0a 0a 20 20 72 63 20 3d 20   }.  }...  rc = 
120a4 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29  moveToRoot(pCur)
120a5 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20  ;.  if( rc ){.  
120a6 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
120a7 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
120a8 3e 70 50 61 67 65 20 29 3b 0a 20 20 61 73 73 65  >pPage );.  asse
120a9 72 74 28 20 70 43 75 72 2d 3e 70 50 61 67 65 2d  rt( pCur->pPage-
120aa 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 69 66 28  >isInit );.  if(
120ab 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
120ac 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b  URSOR_INVALID ){
120ad 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 2d 31 3b  .    *pRes = -1;
120ae 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75  .    assert( pCu
120af 72 2d 3e 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d  r->pPage->nCell=
120b0 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  =0 );.    return
120b1 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
120b2 20 20 69 66 28 20 70 43 75 72 2d 3e 70 50 61 67    if( pCur->pPag
120b3 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20  e->intKey ){.   
120b4 20 2f 2a 20 57 65 20 61 72 65 20 67 69 76 65 6e   /* We are given
120b5 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 74 6f   an SQL table to
120b6 20 73 65 61 72 63 68 2e 20 20 54 68 65 20 6b 65   search.  The ke
120b7 79 20 69 73 20 74 68 65 20 69 6e 74 65 67 65 72  y is the integer
120b8 0a 20 20 20 20 2a 2a 20 72 6f 77 69 64 20 63 6f  .    ** rowid co
120b9 6e 74 61 69 6e 65 64 20 69 6e 20 6e 4b 65 79 2e  ntained in nKey.
120ba 20 20 70 4b 65 79 20 61 6e 64 20 70 55 6e 4b 65    pKey and pUnKe
120bb 79 20 73 68 6f 75 6c 64 20 62 6f 74 68 20 62 65  y should both be
120bc 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 61 73 73   NULL */.    ass
120bd 65 72 74 28 20 70 55 6e 4b 65 79 3d 3d 30 20 29  ert( pUnKey==0 )
120be 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4b  ;.    assert( pK
120bf 65 79 3d 3d 30 20 29 3b 0a 20 20 7d 65 6c 73 65  ey==0 );.  }else
120c0 20 69 66 28 20 70 55 6e 4b 65 79 3d 3d 30 20 29   if( pUnKey==0 )
120c1 7b 0a 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20  {.    /* We are 
120c2 74 6f 20 73 65 61 72 63 68 20 61 6e 20 53 51 4c  to search an SQL
120c3 20 69 6e 64 65 78 20 75 73 69 6e 67 20 61 20 6b   index using a k
120c4 65 79 20 65 6e 63 6f 64 65 64 20 61 73 20 61 20  ey encoded as a 
120c5 62 6c 6f 62 2e 0a 20 20 20 20 2a 2a 20 54 68 65  blob..    ** The
120c6 20 62 6c 6f 62 20 69 73 20 66 6f 75 6e 64 20 61   blob is found a
120c7 74 20 70 4b 65 79 20 61 6e 64 20 69 73 20 6e 4b  t pKey and is nK
120c8 65 79 20 62 79 74 65 73 20 69 6e 20 6c 65 6e 67  ey bytes in leng
120c9 74 68 2e 20 20 55 6e 70 61 63 6b 0a 20 20 20 20  th.  Unpack.    
120ca 2a 2a 20 74 68 69 73 20 6b 65 79 20 73 6f 20 74  ** this key so t
120cb 68 61 74 20 77 65 20 63 61 6e 20 75 73 65 20 69  hat we can use i
120cc 74 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  t. */.    assert
120cd 28 20 70 4b 65 79 21 3d 30 20 29 3b 0a 20 20 20  ( pKey!=0 );.   
120ce 20 70 55 6e 4b 65 79 20 3d 20 73 71 6c 69 74 65   pUnKey = sqlite
120cf 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63  3VdbeRecordUnpac
120d0 6b 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f  k(pCur->pKeyInfo
120d1 2c 20 6e 4b 65 79 2c 20 70 4b 65 79 2c 0a 20 20  , nKey, pKey,.  
120d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
120d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
120d4 20 61 53 70 61 63 65 2c 20 73 69 7a 65 6f 66 28   aSpace, sizeof(
120d5 61 53 70 61 63 65 29 29 3b 0a 20 20 20 20 69 66  aSpace));.    if
120d6 28 20 70 55 6e 4b 65 79 3d 3d 30 20 29 20 72 65  ( pUnKey==0 ) re
120d7 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
120d8 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  M;.  }else{.    
120d9 2f 2a 20 57 65 20 61 72 65 20 74 6f 20 73 65 61  /* We are to sea
120da 72 63 68 20 61 6e 20 53 51 4c 20 69 6e 64 65 78  rch an SQL index
120db 20 75 73 69 6e 67 20 61 20 6b 65 79 20 74 68 61   using a key tha
120dc 74 20 69 73 20 61 6c 72 65 61 64 79 20 75 6e 70  t is already unp
120dd 61 63 6b 65 64 0a 20 20 20 20 2a 2a 20 61 6e 64  acked.    ** and
120de 20 68 61 6e 64 65 64 20 74 6f 20 75 73 20 69 6e   handed to us in
120df 20 70 55 6e 4b 65 79 2e 20 2a 2f 0a 20 20 20 20   pUnKey. */.    
120e0 61 73 73 65 72 74 28 20 70 4b 65 79 3d 3d 30 20  assert( pKey==0 
120e1 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 3b 3b 29  );.  }.  for(;;)
120e2 7b 0a 20 20 20 20 69 6e 74 20 6c 77 72 2c 20 75  {.    int lwr, u
120e3 70 72 3b 0a 20 20 20 20 50 67 6e 6f 20 63 68 6c  pr;.    Pgno chl
120e4 64 50 67 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65  dPg;.    MemPage
120e5 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e   *pPage = pCur->
120e6 70 50 61 67 65 3b 0a 20 20 20 20 69 6e 74 20 63  pPage;.    int c
120e7 20 3d 20 2d 31 3b 20 20 2f 2a 20 70 52 65 73 20   = -1;  /* pRes 
120e8 72 65 74 75 72 6e 20 69 66 20 74 61 62 6c 65 20  return if table 
120e9 69 73 20 65 6d 70 74 79 20 6d 75 73 74 20 62 65  is empty must be
120ea 20 2d 31 20 2a 2f 0a 20 20 20 20 6c 77 72 20 3d   -1 */.    lwr =
120eb 20 30 3b 0a 20 20 20 20 75 70 72 20 3d 20 70 50   0;.    upr = pP
120ec 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20  age->nCell-1;.  
120ed 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69 6e    if( !pPage->in
120ee 74 4b 65 79 20 26 26 20 70 55 6e 4b 65 79 3d 3d  tKey && pUnKey==
120ef 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
120f0 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
120f1 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  KPT;.      goto 
120f2 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20  moveto_finish;. 
120f3 20 20 20 7d 0a 20 20 20 20 69 66 28 20 62 69 61     }.    if( bia
120f4 73 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  sRight ){.      
120f5 70 43 75 72 2d 3e 69 64 78 20 3d 20 75 70 72 3b  pCur->idx = upr;
120f6 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
120f7 20 20 70 43 75 72 2d 3e 69 64 78 20 3d 20 28 75    pCur->idx = (u
120f8 70 72 2b 6c 77 72 29 2f 32 3b 0a 20 20 20 20 7d  pr+lwr)/2;.    }
120f9 0a 20 20 20 20 69 66 28 20 6c 77 72 3c 3d 75 70  .    if( lwr<=up
120fa 72 20 29 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20  r ) for(;;){.   
120fb 20 20 20 76 6f 69 64 20 2a 70 43 65 6c 6c 4b 65     void *pCellKe
120fc 79 3b 0a 20 20 20 20 20 20 69 36 34 20 6e 43 65  y;.      i64 nCe
120fd 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 70 43 75  llKey;.      pCu
120fe 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20  r->info.nSize = 
120ff 30 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 76  0;.      pCur->v
12100 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 0a 20 20  alidNKey = 1;.  
12101 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69      if( pPage->i
12102 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20  ntKey ){.       
12103 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20   u8 *pCell;.    
12104 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64      pCell = find
12105 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 75 72  Cell(pPage, pCur
12106 2d 3e 69 64 78 29 20 2b 20 70 50 61 67 65 2d 3e  ->idx) + pPage->
12107 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20  childPtrSize;.  
12108 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d        if( pPage-
12109 3e 68 61 73 44 61 74 61 20 29 7b 0a 20 20 20 20  >hasData ){.    
1210a 20 20 20 20 20 20 75 33 32 20 64 75 6d 6d 79 3b        u32 dummy;
1210b 0a 20 20 20 20 20 20 20 20 20 20 70 43 65 6c 6c  .          pCell
1210c 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28   += getVarint32(
1210d 70 43 65 6c 6c 2c 20 64 75 6d 6d 79 29 3b 0a 20  pCell, dummy);. 
1210e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1210f 20 67 65 74 56 61 72 69 6e 74 28 70 43 65 6c 6c   getVarint(pCell
12110 2c 20 28 75 36 34 2a 29 26 6e 43 65 6c 6c 4b 65  , (u64*)&nCellKe
12111 79 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  y);.        if( 
12112 6e 43 65 6c 6c 4b 65 79 3d 3d 6e 4b 65 79 20 29  nCellKey==nKey )
12113 7b 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20  {.          c = 
12114 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  0;.        }else
12115 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 6e 4b   if( nCellKey<nK
12116 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ey ){.          
12117 63 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20  c = -1;.        
12118 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
12119 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 4b 65   assert( nCellKe
1211a 79 3e 6e 4b 65 79 20 29 3b 0a 20 20 20 20 20 20  y>nKey );.      
1211b 20 20 20 20 63 20 3d 20 2b 31 3b 0a 20 20 20 20      c = +1;.    
1211c 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
1211d 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 61  e{.        int a
1211e 76 61 69 6c 61 62 6c 65 3b 0a 20 20 20 20 20 20  vailable;.      
1211f 20 20 70 43 65 6c 6c 4b 65 79 20 3d 20 28 76 6f    pCellKey = (vo
12120 69 64 20 2a 29 66 65 74 63 68 50 61 79 6c 6f 61  id *)fetchPayloa
12121 64 28 70 43 75 72 2c 20 26 61 76 61 69 6c 61 62  d(pCur, &availab
12122 6c 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  le, 0);.        
12123 6e 43 65 6c 6c 4b 65 79 20 3d 20 70 43 75 72 2d  nCellKey = pCur-
12124 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20  >info.nKey;.    
12125 20 20 20 20 69 66 28 20 61 76 61 69 6c 61 62 6c      if( availabl
12126 65 3e 3d 6e 43 65 6c 6c 4b 65 79 20 29 7b 0a 20  e>=nCellKey ){. 
12127 20 20 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c           c = sql
12128 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f  ite3VdbeRecordCo
12129 6d 70 61 72 65 28 6e 43 65 6c 6c 4b 65 79 2c 20  mpare(nCellKey, 
1212a 70 43 65 6c 6c 4b 65 79 2c 20 70 55 6e 4b 65 79  pCellKey, pUnKey
1212b 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
1212c 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43 65 6c  {.          pCel
1212d 6c 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 5f 6d  lKey = sqlite3_m
1212e 61 6c 6c 6f 63 28 20 6e 43 65 6c 6c 4b 65 79 20  alloc( nCellKey 
1212f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
12130 20 70 43 65 6c 6c 4b 65 79 3d 3d 30 20 29 7b 0a   pCellKey==0 ){.
12131 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
12132 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
12133 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20             goto 
12134 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20  moveto_finish;. 
12135 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
12136 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
12137 33 42 74 72 65 65 4b 65 79 28 70 43 75 72 2c 20  3BtreeKey(pCur, 
12138 30 2c 20 6e 43 65 6c 6c 4b 65 79 2c 20 28 76 6f  0, nCellKey, (vo
12139 69 64 20 2a 29 70 43 65 6c 6c 4b 65 79 29 3b 0a  id *)pCellKey);.
1213a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 73 71            c = sq
1213b 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43  lite3VdbeRecordC
1213c 6f 6d 70 61 72 65 28 6e 43 65 6c 6c 4b 65 79 2c  ompare(nCellKey,
1213d 20 70 43 65 6c 6c 4b 65 79 2c 20 70 55 6e 4b 65   pCellKey, pUnKe
1213e 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  y);.          sq
1213f 6c 69 74 65 33 5f 66 72 65 65 28 70 43 65 6c 6c  lite3_free(pCell
12140 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Key);.          
12141 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 6d 6f  if( rc ) goto mo
12142 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20  veto_finish;.   
12143 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
12144 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 7b       if( c==0 ){
12145 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 69  .        pCur->i
12146 6e 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c  nfo.nKey = nCell
12147 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 69 66 28  Key;.        if(
12148 20 70 50 61 67 65 2d 3e 6c 65 61 66 44 61 74 61   pPage->leafData
12149 20 26 26 20 21 70 50 61 67 65 2d 3e 6c 65 61 66   && !pPage->leaf
1214a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 77   ){.          lw
1214b 72 20 3d 20 70 43 75 72 2d 3e 69 64 78 3b 0a 20  r = pCur->idx;. 
1214c 20 20 20 20 20 20 20 20 20 75 70 72 20 3d 20 6c           upr = l
1214d 77 72 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20  wr - 1;.        
1214e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
1214f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
12150 20 20 69 66 28 20 70 52 65 73 20 29 20 2a 70 52    if( pRes ) *pR
12151 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  es = 0;.        
12152 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
12153 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
12154 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a   moveto_finish;.
12155 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
12156 7d 0a 20 20 20 20 20 20 69 66 28 20 63 3c 30 20  }.      if( c<0 
12157 29 7b 0a 20 20 20 20 20 20 20 20 6c 77 72 20 3d  ){.        lwr =
12158 20 70 43 75 72 2d 3e 69 64 78 2b 31 3b 0a 20 20   pCur->idx+1;.  
12159 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1215a 20 20 20 75 70 72 20 3d 20 70 43 75 72 2d 3e 69     upr = pCur->i
1215b 64 78 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  dx-1;.      }.  
1215c 20 20 20 20 69 66 28 20 6c 77 72 3e 75 70 72 20      if( lwr>upr 
1215d 29 7b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d  ){.        pCur-
1215e 3e 69 6e 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65  >info.nKey = nCe
1215f 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 62  llKey;.        b
12160 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
12161 20 20 20 20 70 43 75 72 2d 3e 69 64 78 20 3d 20      pCur->idx = 
12162 28 6c 77 72 2b 75 70 72 29 2f 32 3b 0a 20 20 20  (lwr+upr)/2;.   
12163 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c   }.    assert( l
12164 77 72 3d 3d 75 70 72 2b 31 20 29 3b 0a 20 20 20  wr==upr+1 );.   
12165 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
12166 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 69 66  isInit );.    if
12167 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b  ( pPage->leaf ){
12168 0a 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20  .      chldPg = 
12169 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  0;.    }else if(
1216a 20 6c 77 72 3e 3d 70 50 61 67 65 2d 3e 6e 43 65   lwr>=pPage->nCe
1216b 6c 6c 20 29 7b 0a 20 20 20 20 20 20 63 68 6c 64  ll ){.      chld
1216c 50 67 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  Pg = get4byte(&p
1216d 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67  Page->aData[pPag
1216e 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  e->hdrOffset+8])
1216f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
12170 20 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74 34     chldPg = get4
12171 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50  byte(findCell(pP
12172 61 67 65 2c 20 6c 77 72 29 29 3b 0a 20 20 20 20  age, lwr));.    
12173 7d 0a 20 20 20 20 69 66 28 20 63 68 6c 64 50 67  }.    if( chldPg
12174 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ==0 ){.      ass
12175 65 72 74 28 20 70 43 75 72 2d 3e 69 64 78 3e 3d  ert( pCur->idx>=
12176 30 20 26 26 20 70 43 75 72 2d 3e 69 64 78 3c 70  0 && pCur->idx<p
12177 43 75 72 2d 3e 70 50 61 67 65 2d 3e 6e 43 65 6c  Cur->pPage->nCel
12178 6c 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  l );.      if( p
12179 52 65 73 20 29 20 2a 70 52 65 73 20 3d 20 63 3b  Res ) *pRes = c;
1217a 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
1217b 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 67 6f 74  TE_OK;.      got
1217c 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b  o moveto_finish;
1217d 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75 72 2d  .    }.    pCur-
1217e 3e 69 64 78 20 3d 20 6c 77 72 3b 0a 20 20 20 20  >idx = lwr;.    
1217f 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65  pCur->info.nSize
12180 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e   = 0;.    pCur->
12181 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20  validNKey = 0;. 
12182 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68     rc = moveToCh
12183 69 6c 64 28 70 43 75 72 2c 20 63 68 6c 64 50 67  ild(pCur, chldPg
12184 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20  );.    if( rc ) 
12185 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69  goto moveto_fini
12186 73 68 3b 0a 20 20 7d 0a 6d 6f 76 65 74 6f 5f 66  sh;.  }.moveto_f
12187 69 6e 69 73 68 3a 0a 20 20 69 66 28 20 70 4b 65  inish:.  if( pKe
12188 79 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 77  y ){.    /* If w
12189 65 20 63 72 65 61 74 65 64 20 6f 75 72 20 6f 77  e created our ow
1218a 6e 20 75 6e 70 61 63 6b 65 64 20 6b 65 79 20 61  n unpacked key a
1218b 74 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 69  t the top of thi
1218c 73 0a 20 20 20 20 2a 2a 20 70 72 6f 63 65 64 75  s.    ** procedu
1218d 72 65 2c 20 74 68 65 6e 20 64 65 73 74 72 6f 79  re, then destroy
1218e 20 74 68 61 74 20 6b 65 79 20 62 65 66 6f 72 65   that key before
1218f 20 72 65 74 75 72 6e 69 6e 67 2e 20 2a 2f 0a 20   returning. */. 
12190 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65     sqlite3VdbeDe
12191 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f  leteUnpackedReco
12192 72 64 28 70 55 6e 4b 65 79 29 3b 0a 20 20 7d 0a  rd(pUnKey);.  }.
12193 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
12194 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
12195 55 45 20 69 66 20 74 68 65 20 63 75 72 73 6f 72  UE if the cursor
12196 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67   is not pointing
12197 20 61 74 20 61 6e 20 65 6e 74 72 79 20 6f 66 20   at an entry of 
12198 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  the table..**.**
12199 20 54 52 55 45 20 77 69 6c 6c 20 62 65 20 72 65   TRUE will be re
1219a 74 75 72 6e 65 64 20 61 66 74 65 72 20 61 20 63  turned after a c
1219b 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74  all to sqlite3Bt
1219c 72 65 65 4e 65 78 74 28 29 20 6d 6f 76 65 73 0a  reeNext() moves.
1219d 2a 2a 20 70 61 73 74 20 74 68 65 20 6c 61 73 74  ** past the last
1219e 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61   entry in the ta
1219f 62 6c 65 20 6f 72 20 73 71 6c 69 74 65 33 42 74  ble or sqlite3Bt
121a0 72 65 65 50 72 65 76 28 29 20 6d 6f 76 65 73 20  reePrev() moves 
121a1 70 61 73 74 0a 2a 2a 20 74 68 65 20 66 69 72 73  past.** the firs
121a2 74 20 65 6e 74 72 79 2e 20 20 54 52 55 45 20 69  t entry.  TRUE i
121a3 73 20 61 6c 73 6f 20 72 65 74 75 72 6e 65 64 20  s also returned 
121a4 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  if the table is 
121a5 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  empty..*/.SQLITE
121a6 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
121a7 69 74 65 33 42 74 72 65 65 45 6f 66 28 42 74 43  ite3BtreeEof(BtC
121a8 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20  ursor *pCur){.  
121a9 2f 2a 20 54 4f 44 4f 3a 20 57 68 61 74 20 69 66  /* TODO: What if
121aa 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 69   the cursor is i
121ab 6e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  n CURSOR_REQUIRE
121ac 53 45 45 4b 20 62 75 74 20 61 6c 6c 20 74 61 62  SEEK but all tab
121ad 6c 65 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20  le entries.  ** 
121ae 68 61 76 65 20 62 65 65 6e 20 64 65 6c 65 74 65  have been delete
121af 64 3f 20 54 68 69 73 20 41 50 49 20 77 69 6c 6c  d? This API will
121b0 20 6e 65 65 64 20 74 6f 20 63 68 61 6e 67 65 20   need to change 
121b1 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72  to return an err
121b2 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 73 20  or code.  ** as 
121b3 77 65 6c 6c 20 61 73 20 74 68 65 20 62 6f 6f 6c  well as the bool
121b4 65 61 6e 20 72 65 73 75 6c 74 20 76 61 6c 75 65  ean result value
121b5 2e 0a 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  ..  */.  return 
121b6 28 43 55 52 53 4f 52 5f 56 41 4c 49 44 21 3d 70  (CURSOR_VALID!=p
121b7 43 75 72 2d 3e 65 53 74 61 74 65 29 3b 0a 7d 0a  Cur->eState);.}.
121b8 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
121b9 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
121ba 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 66 6f 72  ction handle for
121bb 20 61 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a 53 51   a cursor..*/.SQ
121bc 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
121bd 69 74 65 33 20 2a 73 71 6c 69 74 65 33 42 74 72  ite3 *sqlite3Btr
121be 65 65 43 75 72 73 6f 72 44 62 28 63 6f 6e 73 74  eeCursorDb(const
121bf 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29   BtCursor *pCur)
121c0 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  {.  assert( sqli
121c1 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
121c2 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d  Cur->pBtree->db-
121c3 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74  >mutex) );.  ret
121c4 75 72 6e 20 70 43 75 72 2d 3e 70 42 74 72 65 65  urn pCur->pBtree
121c5 2d 3e 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ->db;.}../*.** A
121c6 64 76 61 6e 63 65 20 74 68 65 20 63 75 72 73 6f  dvance the curso
121c7 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 65 6e  r to the next en
121c8 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62  try in the datab
121c9 61 73 65 2e 20 20 49 66 0a 2a 2a 20 73 75 63 63  ase.  If.** succ
121ca 65 73 73 66 75 6c 20 74 68 65 6e 20 73 65 74 20  essful then set 
121cb 2a 70 52 65 73 3d 30 2e 20 20 49 66 20 74 68 65  *pRes=0.  If the
121cc 20 63 75 72 73 6f 72 0a 2a 2a 20 77 61 73 20 61   cursor.** was a
121cd 6c 72 65 61 64 79 20 70 6f 69 6e 74 69 6e 67 20  lready pointing 
121ce 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72  to the last entr
121cf 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  y in the databas
121d0 65 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 69 73  e before.** this
121d1 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c   routine was cal
121d2 6c 65 64 2c 20 74 68 65 6e 20 73 65 74 20 2a 70  led, then set *p
121d3 52 65 73 3d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45  Res=1..*/.SQLITE
121d4 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
121d5 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 42 74  ite3BtreeNext(Bt
121d6 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e  Cursor *pCur, in
121d7 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20  t *pRes){.  int 
121d8 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  rc;.  MemPage *p
121d9 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28  Page;..  assert(
121da 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
121db 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20  x(pCur) );.  rc 
121dc 3d 20 72 65 73 74 6f 72 65 4f 72 43 6c 65 61 72  = restoreOrClear
121dd 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70  CursorPosition(p
121de 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  Cur);.  if( rc!=
121df 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
121e0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
121e1 20 20 61 73 73 65 72 74 28 20 70 52 65 73 21 3d    assert( pRes!=
121e2 30 20 29 3b 0a 20 20 70 50 61 67 65 20 3d 20 70  0 );.  pPage = p
121e3 43 75 72 2d 3e 70 50 61 67 65 3b 0a 20 20 69 66  Cur->pPage;.  if
121e4 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  ( CURSOR_INVALID
121e5 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29  ==pCur->eState )
121e6 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b  {.    *pRes = 1;
121e7 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
121e8 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28  TE_OK;.  }.  if(
121e9 20 70 43 75 72 2d 3e 73 6b 69 70 3e 30 20 29 7b   pCur->skip>0 ){
121ea 0a 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 70 20  .    pCur->skip 
121eb 3d 20 30 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d  = 0;.    *pRes =
121ec 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   0;.    return S
121ed 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
121ee 70 43 75 72 2d 3e 73 6b 69 70 20 3d 20 30 3b 0a  pCur->skip = 0;.
121ef 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
121f0 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 61 73  ->isInit );.  as
121f1 73 65 72 74 28 20 70 43 75 72 2d 3e 69 64 78 3c  sert( pCur->idx<
121f2 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a  pPage->nCell );.
121f3 0a 20 20 70 43 75 72 2d 3e 69 64 78 2b 2b 3b 0a  .  pCur->idx++;.
121f4 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69    pCur->info.nSi
121f5 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e  ze = 0;.  pCur->
121f6 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20  validNKey = 0;. 
121f7 20 69 66 28 20 70 43 75 72 2d 3e 69 64 78 3e 3d   if( pCur->idx>=
121f8 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a  pPage->nCell ){.
121f9 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e      if( !pPage->
121fa 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63  leaf ){.      rc
121fb 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70   = moveToChild(p
121fc 43 75 72 2c 20 67 65 74 34 62 79 74 65 28 26 70  Cur, get4byte(&p
121fd 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67  Page->aData[pPag
121fe 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  e->hdrOffset+8])
121ff 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20  );.      if( rc 
12200 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
12201 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65     rc = moveToLe
12202 66 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20  ftmost(pCur);.  
12203 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20      *pRes = 0;. 
12204 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
12205 20 20 20 20 7d 0a 20 20 20 20 64 6f 7b 0a 20 20      }.    do{.  
12206 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42      if( sqlite3B
12207 74 72 65 65 49 73 52 6f 6f 74 50 61 67 65 28 70  treeIsRootPage(p
12208 50 61 67 65 29 20 29 7b 0a 20 20 20 20 20 20 20  Page) ){.       
12209 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20   *pRes = 1;.    
1220a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65      pCur->eState
1220b 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49   = CURSOR_INVALI
1220c 44 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  D;.        retur
1220d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
1220e 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
1220f 65 33 42 74 72 65 65 4d 6f 76 65 54 6f 50 61 72  e3BtreeMoveToPar
12210 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20  ent(pCur);.     
12211 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 70   pPage = pCur->p
12212 50 61 67 65 3b 0a 20 20 20 20 7d 77 68 69 6c 65  Page;.    }while
12213 28 20 70 43 75 72 2d 3e 69 64 78 3e 3d 70 50 61  ( pCur->idx>=pPa
12214 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20  ge->nCell );.   
12215 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20   *pRes = 0;.    
12216 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 44  if( pPage->leafD
12217 61 74 61 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ata ){.      rc 
12218 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65  = sqlite3BtreeNe
12219 78 74 28 70 43 75 72 2c 20 70 52 65 73 29 3b 0a  xt(pCur, pRes);.
1221a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1221b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1221c 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
1221d 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 2a 70 52 65  n rc;.  }.  *pRe
1221e 73 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 50 61  s = 0;.  if( pPa
1221f 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20  ge->leaf ){.    
12220 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
12221 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 6d 6f 76  ;.  }.  rc = mov
12222 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72  eToLeftmost(pCur
12223 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
12224 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 70 20 74  }.../*.** Step t
12225 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65  he cursor to the
12226 20 62 61 63 6b 20 74 6f 20 74 68 65 20 70 72 65   back to the pre
12227 76 69 6f 75 73 20 65 6e 74 72 79 20 69 6e 20 74  vious entry in t
12228 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 66  he database.  If
12229 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 74  .** successful t
1222a 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 2e  hen set *pRes=0.
1222b 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 0a    If the cursor.
1222c 2a 2a 20 77 61 73 20 61 6c 72 65 61 64 79 20 70  ** was already p
1222d 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 66  ointing to the f
1222e 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68  irst entry in th
1222f 65 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72  e database befor
12230 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e  e.** this routin
12231 65 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 68  e was called, th
12232 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a  en set *pRes=1..
12233 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
12234 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
12235 65 65 50 72 65 76 69 6f 75 73 28 42 74 43 75 72  eePrevious(BtCur
12236 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a  sor *pCur, int *
12237 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b  pRes){.  int rc;
12238 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20  .  Pgno pgno;.  
12239 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a  MemPage *pPage;.
1223a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
1223b 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
1223c 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74  ) );.  rc = rest
1223d 6f 72 65 4f 72 43 6c 65 61 72 43 75 72 73 6f 72  oreOrClearCursor
1223e 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a  Position(pCur);.
1223f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
12240 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  _OK ){.    retur
12241 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 43 75 72  n rc;.  }.  pCur
12242 2d 3e 61 74 4c 61 73 74 20 3d 20 30 3b 0a 20 20  ->atLast = 0;.  
12243 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c  if( CURSOR_INVAL
12244 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65  ID==pCur->eState
12245 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20   ){.    *pRes = 
12246 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  1;.    return SQ
12247 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69  LITE_OK;.  }.  i
12248 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 3c 30 20  f( pCur->skip<0 
12249 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 73 6b 69  ){.    pCur->ski
1224a 70 20 3d 20 30 3b 0a 20 20 20 20 2a 70 52 65 73  p = 0;.    *pRes
1224b 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 0;.    return
1224c 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
1224d 20 20 70 43 75 72 2d 3e 73 6b 69 70 20 3d 20 30    pCur->skip = 0
1224e 3b 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75  ;..  pPage = pCu
1224f 72 2d 3e 70 50 61 67 65 3b 0a 20 20 61 73 73 65  r->pPage;.  asse
12250 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69  rt( pPage->isIni
12251 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  t );.  assert( p
12252 43 75 72 2d 3e 69 64 78 3e 3d 30 20 29 3b 0a 20  Cur->idx>=0 );. 
12253 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61   if( !pPage->lea
12254 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20  f ){.    pgno = 
12255 67 65 74 34 62 79 74 65 28 20 66 69 6e 64 43 65  get4byte( findCe
12256 6c 6c 28 70 50 61 67 65 2c 20 70 43 75 72 2d 3e  ll(pPage, pCur->
12257 69 64 78 29 20 29 3b 0a 20 20 20 20 72 63 20 3d  idx) );.    rc =
12258 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75   moveToChild(pCu
12259 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 69 66  r, pgno);.    if
1225a 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65  ( rc ){.      re
1225b 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
1225c 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 69     rc = moveToRi
1225d 67 68 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20  ghtmost(pCur);. 
1225e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 68 69 6c   }else{.    whil
1225f 65 28 20 70 43 75 72 2d 3e 69 64 78 3d 3d 30 20  e( pCur->idx==0 
12260 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  ){.      if( sql
12261 69 74 65 33 42 74 72 65 65 49 73 52 6f 6f 74 50  ite3BtreeIsRootP
12262 61 67 65 28 70 50 61 67 65 29 20 29 7b 0a 20 20  age(pPage) ){.  
12263 20 20 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61        pCur->eSta
12264 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41  te = CURSOR_INVA
12265 4c 49 44 3b 0a 20 20 20 20 20 20 20 20 2a 70 52  LID;.        *pR
12266 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  es = 1;.        
12267 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
12268 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
12269 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65  sqlite3BtreeMove
1226a 54 6f 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a  ToParent(pCur);.
1226b 20 20 20 20 20 20 70 50 61 67 65 20 3d 20 70 43        pPage = pC
1226c 75 72 2d 3e 70 50 61 67 65 3b 0a 20 20 20 20 7d  ur->pPage;.    }
1226d 0a 20 20 20 20 70 43 75 72 2d 3e 69 64 78 2d 2d  .    pCur->idx--
1226e 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f  ;.    pCur->info
1226f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20  .nSize = 0;.    
12270 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20  pCur->validNKey 
12271 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 50 61  = 0;.    if( pPa
12272 67 65 2d 3e 6c 65 61 66 44 61 74 61 20 26 26 20  ge->leafData && 
12273 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
12274 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
12275 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28  e3BtreePrevious(
12276 70 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 20 20  pCur, pRes);.   
12277 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
12278 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
12279 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 52 65 73 20    }.  }.  *pRes 
1227a 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  = 0;.  return rc
1227b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  ;.}../*.** Alloc
1227c 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 66  ate a new page f
1227d 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
1227e 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   file..**.** The
1227f 20 6e 65 77 20 70 61 67 65 20 69 73 20 6d 61 72   new page is mar
12280 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 20 28  ked as dirty.  (
12281 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
12282 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
12283 65 28 29 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61  e().** has alrea
12284 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 6f  dy been called o
12285 6e 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e 29  n the new page.)
12286 20 20 54 68 65 20 6e 65 77 20 70 61 67 65 20 68    The new page h
12287 61 73 20 61 6c 73 6f 0a 2a 2a 20 62 65 65 6e 20  as also.** been 
12288 72 65 66 65 72 65 6e 63 65 64 20 61 6e 64 20 74  referenced and t
12289 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69  he calling routi
1228a 6e 65 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  ne is responsibl
1228b 65 20 66 6f 72 20 63 61 6c 6c 69 6e 67 0a 2a 2a  e for calling.**
1228c 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
1228d 65 66 28 29 20 6f 6e 20 74 68 65 20 6e 65 77 20  ef() on the new 
1228e 70 61 67 65 20 77 68 65 6e 20 69 74 20 69 73 20  page when it is 
1228f 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49  done..**.** SQLI
12290 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
12291 64 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 41  d on success.  A
12292 6e 79 20 6f 74 68 65 72 20 72 65 74 75 72 6e 20  ny other return 
12293 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 0a  value indicates.
12294 2a 2a 20 61 6e 20 65 72 72 6f 72 2e 20 20 2a 70  ** an error.  *p
12295 70 50 61 67 65 20 61 6e 64 20 2a 70 50 67 6e 6f  pPage and *pPgno
12296 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20 69   are undefined i
12297 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61  n the event of a
12298 6e 20 65 72 72 6f 72 2e 0a 2a 2a 20 44 6f 20 6e  n error..** Do n
12299 6f 74 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65  ot invoke sqlite
1229a 33 50 61 67 65 72 55 6e 72 65 66 28 29 20 6f 6e  3PagerUnref() on
1229b 20 2a 70 70 50 61 67 65 20 69 66 20 61 6e 20 65   *ppPage if an e
1229c 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64  rror is returned
1229d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22  ..**.** If the "
1229e 6e 65 61 72 62 79 22 20 70 61 72 61 6d 65 74 65  nearby" paramete
1229f 72 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e  r is not 0, then
122a0 20 61 20 28 66 65 65 62 6c 65 29 20 65 66 66 6f   a (feeble) effo
122a1 72 74 20 69 73 20 6d 61 64 65 20 74 6f 20 0a 2a  rt is made to .*
122a2 2a 20 6c 6f 63 61 74 65 20 61 20 70 61 67 65 20  * locate a page 
122a3 63 6c 6f 73 65 20 74 6f 20 74 68 65 20 70 61 67  close to the pag
122a4 65 20 6e 75 6d 62 65 72 20 22 6e 65 61 72 62 79  e number "nearby
122a5 22 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20  ".  This can be 
122a6 75 73 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 61 74  used in an.** at
122a7 74 65 6d 70 74 20 74 6f 20 6b 65 65 70 20 72 65  tempt to keep re
122a8 6c 61 74 65 64 20 70 61 67 65 73 20 63 6c 6f 73  lated pages clos
122a9 65 20 74 6f 20 65 61 63 68 20 6f 74 68 65 72 20  e to each other 
122aa 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
122ab 66 69 6c 65 2c 0a 2a 2a 20 77 68 69 63 68 20 69  file,.** which i
122ac 6e 20 74 75 72 6e 20 63 61 6e 20 6d 61 6b 65 20  n turn can make 
122ad 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20  database access 
122ae 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66  faster..**.** If
122af 20 74 68 65 20 22 65 78 61 63 74 22 20 70 61 72   the "exact" par
122b0 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 30 2c  ameter is not 0,
122b1 20 61 6e 64 20 74 68 65 20 70 61 67 65 2d 6e 75   and the page-nu
122b2 6d 62 65 72 20 6e 65 61 72 62 79 20 65 78 69 73  mber nearby exis
122b3 74 73 20 0a 2a 2a 20 61 6e 79 77 68 65 72 65 20  ts .** anywhere 
122b4 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  on the free-list
122b5 2c 20 74 68 65 6e 20 69 74 20 69 73 20 67 75 61  , then it is gua
122b6 72 65 6e 74 65 65 64 20 74 6f 20 62 65 20 72 65  renteed to be re
122b7 74 75 72 6e 65 64 2e 20 54 68 69 73 0a 2a 2a 20  turned. This.** 
122b8 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 20  is only used by 
122b9 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61  auto-vacuum data
122ba 62 61 73 65 73 20 77 68 65 6e 20 61 6c 6c 6f 63  bases when alloc
122bb 61 74 69 6e 67 20 61 20 6e 65 77 20 74 61 62 6c  ating a new tabl
122bc 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
122bd 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61   allocateBtreePa
122be 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a  ge(.  BtShared *
122bf 70 42 74 2c 20 0a 20 20 4d 65 6d 50 61 67 65 20  pBt, .  MemPage 
122c0 2a 2a 70 70 50 61 67 65 2c 20 0a 20 20 50 67 6e  **ppPage, .  Pgn
122c1 6f 20 2a 70 50 67 6e 6f 2c 20 0a 20 20 50 67 6e  o *pPgno, .  Pgn
122c2 6f 20 6e 65 61 72 62 79 2c 0a 20 20 75 38 20 65  o nearby,.  u8 e
122c3 78 61 63 74 0a 29 7b 0a 20 20 4d 65 6d 50 61 67  xact.){.  MemPag
122c4 65 20 2a 70 50 61 67 65 31 3b 0a 20 20 69 6e 74  e *pPage1;.  int
122c5 20 72 63 3b 0a 20 20 69 6e 74 20 6e 3b 20 20 20   rc;.  int n;   
122c6 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70    /* Number of p
122c7 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65  ages on the free
122c8 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6b 3b  list */.  int k;
122c9 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
122ca 66 20 6c 65 61 76 65 73 20 6f 6e 20 74 68 65 20  f leaves on the 
122cb 74 72 75 6e 6b 20 6f 66 20 74 68 65 20 66 72 65  trunk of the fre
122cc 65 6c 69 73 74 20 2a 2f 0a 20 20 4d 65 6d 50 61  elist */.  MemPa
122cd 67 65 20 2a 70 54 72 75 6e 6b 20 3d 20 30 3b 0a  ge *pTrunk = 0;.
122ce 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 72 65 76    MemPage *pPrev
122cf 54 72 75 6e 6b 20 3d 20 30 3b 0a 0a 20 20 61 73  Trunk = 0;..  as
122d0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
122d1 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
122d2 74 65 78 29 20 29 3b 0a 20 20 70 50 61 67 65 31  tex) );.  pPage1
122d3 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a   = pBt->pPage1;.
122d4 20 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26    n = get4byte(&
122d5 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36  pPage1->aData[36
122d6 5d 29 3b 0a 20 20 69 66 28 20 6e 3e 30 20 29 7b  ]);.  if( n>0 ){
122d7 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 61 72  .    /* There ar
122d8 65 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66  e pages on the f
122d9 72 65 65 6c 69 73 74 2e 20 20 52 65 75 73 65 20  reelist.  Reuse 
122da 6f 6e 65 20 6f 66 20 74 68 6f 73 65 20 70 61 67  one of those pag
122db 65 73 2e 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20  es. */.    Pgno 
122dc 69 54 72 75 6e 6b 3b 0a 20 20 20 20 75 38 20 73  iTrunk;.    u8 s
122dd 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 20 2f  earchList = 0; /
122de 2a 20 49 66 20 74 68 65 20 66 72 65 65 2d 6c 69  * If the free-li
122df 73 74 20 6d 75 73 74 20 62 65 20 73 65 61 72 63  st must be searc
122e0 68 65 64 20 66 6f 72 20 27 6e 65 61 72 62 79 27  hed for 'nearby'
122e1 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20   */.    .    /* 
122e2 49 66 20 74 68 65 20 27 65 78 61 63 74 27 20 70  If the 'exact' p
122e3 61 72 61 6d 65 74 65 72 20 77 61 73 20 74 72 75  arameter was tru
122e4 65 20 61 6e 64 20 61 20 71 75 65 72 79 20 6f 66  e and a query of
122e5 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70   the pointer-map
122e6 0a 20 20 20 20 2a 2a 20 73 68 6f 77 73 20 74 68  .    ** shows th
122e7 61 74 20 74 68 65 20 70 61 67 65 20 27 6e 65 61  at the page 'nea
122e8 72 62 79 27 20 69 73 20 73 6f 6d 65 77 68 65 72  rby' is somewher
122e9 65 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69  e on the free-li
122ea 73 74 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20  st, then.    ** 
122eb 74 68 65 20 65 6e 74 69 72 65 2d 6c 69 73 74 20  the entire-list 
122ec 77 69 6c 6c 20 62 65 20 73 65 61 72 63 68 65 64  will be searched
122ed 20 66 6f 72 20 74 68 61 74 20 70 61 67 65 2e 0a   for that page..
122ee 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53      */.#ifndef S
122ef 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
122f0 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 65 78  ACUUM.    if( ex
122f1 61 63 74 20 26 26 20 6e 65 61 72 62 79 3c 3d 73  act && nearby<=s
122f2 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63  qlite3PagerPagec
122f3 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72  ount(pBt->pPager
122f4 29 20 29 7b 0a 20 20 20 20 20 20 75 38 20 65 54  ) ){.      u8 eT
122f5 79 70 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72  ype;.      asser
122f6 74 28 20 6e 65 61 72 62 79 3e 30 20 29 3b 0a 20  t( nearby>0 );. 
122f7 20 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74       assert( pBt
122f8 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 3b 0a  ->autoVacuum );.
122f9 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61        rc = ptrma
122fa 70 47 65 74 28 70 42 74 2c 20 6e 65 61 72 62 79  pGet(pBt, nearby
122fb 2c 20 26 65 54 79 70 65 2c 20 30 29 3b 0a 20 20  , &eType, 0);.  
122fc 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
122fd 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 69 66  urn rc;.      if
122fe 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f  ( eType==PTRMAP_
122ff 46 52 45 45 50 41 47 45 20 29 7b 0a 20 20 20 20  FREEPAGE ){.    
12300 20 20 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d      searchList =
12301 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
12302 20 20 2a 70 50 67 6e 6f 20 3d 20 6e 65 61 72 62    *pPgno = nearb
12303 79 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  y;.    }.#endif.
12304 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e  .    /* Decremen
12305 74 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20  t the free-list 
12306 63 6f 75 6e 74 20 62 79 20 31 2e 20 53 65 74 20  count by 1. Set 
12307 69 54 72 75 6e 6b 20 74 6f 20 74 68 65 20 69 6e  iTrunk to the in
12308 64 65 78 20 6f 66 20 74 68 65 0a 20 20 20 20 2a  dex of the.    *
12309 2a 20 66 69 72 73 74 20 66 72 65 65 2d 6c 69 73  * first free-lis
1230a 74 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 69 50  t trunk page. iP
1230b 72 65 76 54 72 75 6e 6b 20 69 73 20 69 6e 69 74  revTrunk is init
1230c 69 61 6c 6c 79 20 31 2e 0a 20 20 20 20 2a 2f 0a  ially 1..    */.
1230d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1230e 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65  PagerWrite(pPage
1230f 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  1->pDbPage);.   
12310 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
12311 20 72 63 3b 0a 20 20 20 20 70 75 74 34 62 79 74   rc;.    put4byt
12312 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61  e(&pPage1->aData
12313 5b 33 36 5d 2c 20 6e 2d 31 29 3b 0a 0a 20 20 20  [36], n-1);..   
12314 20 2f 2a 20 54 68 65 20 63 6f 64 65 20 77 69 74   /* The code wit
12315 68 69 6e 20 74 68 69 73 20 6c 6f 6f 70 20 69 73  hin this loop is
12316 20 72 75 6e 20 6f 6e 6c 79 20 6f 6e 63 65 20 69   run only once i
12317 66 20 74 68 65 20 27 73 65 61 72 63 68 4c 69 73  f the 'searchLis
12318 74 27 20 76 61 72 69 61 62 6c 65 0a 20 20 20 20  t' variable.    
12319 2a 2a 20 69 73 20 6e 6f 74 20 74 72 75 65 2e 20  ** is not true. 
1231a 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 72 75  Otherwise, it ru
1231b 6e 73 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ns once for each
1231c 20 74 72 75 6e 6b 2d 70 61 67 65 20 6f 6e 20 74   trunk-page on t
1231d 68 65 0a 20 20 20 20 2a 2a 20 66 72 65 65 2d 6c  he.    ** free-l
1231e 69 73 74 20 75 6e 74 69 6c 20 74 68 65 20 70 61  ist until the pa
1231f 67 65 20 27 6e 65 61 72 62 79 27 20 69 73 20 6c  ge 'nearby' is l
12320 6f 63 61 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20  ocated..    */. 
12321 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 70 50     do {.      pP
12322 72 65 76 54 72 75 6e 6b 20 3d 20 70 54 72 75 6e  revTrunk = pTrun
12323 6b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 72  k;.      if( pPr
12324 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20  evTrunk ){.     
12325 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 34     iTrunk = get4
12326 62 79 74 65 28 26 70 50 72 65 76 54 72 75 6e 6b  byte(&pPrevTrunk
12327 2d 3e 61 44 61 74 61 5b 30 5d 29 3b 0a 20 20 20  ->aData[0]);.   
12328 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
12329 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 62    iTrunk = get4b
1232a 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61  yte(&pPage1->aDa
1232b 74 61 5b 33 32 5d 29 3b 0a 20 20 20 20 20 20 7d  ta[32]);.      }
1232c 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1232d 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28  te3BtreeGetPage(
1232e 70 42 74 2c 20 69 54 72 75 6e 6b 2c 20 26 70 54  pBt, iTrunk, &pT
1232f 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 20 20  runk, 0);.      
12330 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
12331 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20    pTrunk = 0;.  
12332 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61        goto end_a
12333 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20  llocate_page;.  
12334 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 6b 20 3d      }..      k =
12335 20 67 65 74 34 62 79 74 65 28 26 70 54 72 75 6e   get4byte(&pTrun
12336 6b 2d 3e 61 44 61 74 61 5b 34 5d 29 3b 0a 20 20  k->aData[4]);.  
12337 20 20 20 20 69 66 28 20 6b 3d 3d 30 20 26 26 20      if( k==0 && 
12338 21 73 65 61 72 63 68 4c 69 73 74 20 29 7b 0a 20  !searchList ){. 
12339 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72         /* The tr
1233a 75 6e 6b 20 68 61 73 20 6e 6f 20 6c 65 61 76 65  unk has no leave
1233b 73 20 61 6e 64 20 74 68 65 20 6c 69 73 74 20 69  s and the list i
1233c 73 20 6e 6f 74 20 62 65 69 6e 67 20 73 65 61 72  s not being sear
1233d 63 68 65 64 2e 20 0a 20 20 20 20 20 20 20 20 2a  ched. .        *
1233e 2a 20 53 6f 20 65 78 74 72 61 63 74 20 74 68 65  * So extract the
1233f 20 74 72 75 6e 6b 20 70 61 67 65 20 69 74 73 65   trunk page itse
12340 6c 66 20 61 6e 64 20 75 73 65 20 69 74 20 61 73  lf and use it as
12341 20 74 68 65 20 6e 65 77 6c 79 20 0a 20 20 20 20   the newly .    
12342 20 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64      ** allocated
12343 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 20   page */.       
12344 20 61 73 73 65 72 74 28 20 70 50 72 65 76 54 72   assert( pPrevTr
12345 75 6e 6b 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  unk==0 );.      
12346 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
12347 67 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d  gerWrite(pTrunk-
12348 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  >pDbPage);.     
12349 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
1234a 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
1234b 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20  allocate_page;. 
1234c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1234d 20 2a 70 50 67 6e 6f 20 3d 20 69 54 72 75 6e 6b   *pPgno = iTrunk
1234e 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79  ;.        memcpy
1234f 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  (&pPage1->aData[
12350 33 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44  32], &pTrunk->aD
12351 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20  ata[0], 4);.    
12352 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 54      *ppPage = pT
12353 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 70 54  runk;.        pT
12354 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20  runk = 0;.      
12355 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41    TRACE(("ALLOCA
12356 54 45 3a 20 25 64 20 74 72 75 6e 6b 20 2d 20 25  TE: %d trunk - %
12357 64 20 66 72 65 65 20 70 61 67 65 73 20 6c 65 66  d free pages lef
12358 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 6e 2d  t\n", *pPgno, n-
12359 31 29 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  1));.      }else
1235a 20 69 66 28 20 6b 3e 70 42 74 2d 3e 75 73 61 62   if( k>pBt->usab
1235b 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 29 7b 0a  leSize/4 - 8 ){.
1235c 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
1235d 20 6f 66 20 6b 20 69 73 20 6f 75 74 20 6f 66 20   of k is out of 
1235e 72 61 6e 67 65 2e 20 20 44 61 74 61 62 61 73 65  range.  Database
1235f 20 63 6f 72 72 75 70 74 69 6f 6e 20 2a 2f 0a 20   corruption */. 
12360 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
12361 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
12362 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e  .        goto en
12363 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b  d_allocate_page;
12364 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
12365 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
12366 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
12367 73 65 61 72 63 68 4c 69 73 74 20 26 26 20 6e 65  searchList && ne
12368 61 72 62 79 3d 3d 69 54 72 75 6e 6b 20 29 7b 0a  arby==iTrunk ){.
12369 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c          /* The l
1236a 69 73 74 20 69 73 20 62 65 69 6e 67 20 73 65 61  ist is being sea
1236b 72 63 68 65 64 20 61 6e 64 20 74 68 69 73 20 74  rched and this t
1236c 72 75 6e 6b 20 70 61 67 65 20 69 73 20 74 68 65  runk page is the
1236d 20 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a   page.        **
1236e 20 74 6f 20 61 6c 6c 6f 63 61 74 65 2c 20 72 65   to allocate, re
1236f 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74  gardless of whet
12370 68 65 72 20 69 74 20 68 61 73 20 6c 65 61 76 65  her it has leave
12371 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  s..        */.  
12372 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2a 70        assert( *p
12373 50 67 6e 6f 3d 3d 69 54 72 75 6e 6b 20 29 3b 0a  Pgno==iTrunk );.
12374 20 20 20 20 20 20 20 20 2a 70 70 50 61 67 65 20          *ppPage 
12375 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20  = pTrunk;.      
12376 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30    searchList = 0
12377 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
12378 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
12379 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65  (pTrunk->pDbPage
1237a 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
1237b 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67  c ){.          g
1237c 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65  oto end_allocate
1237d 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d  _page;.        }
1237e 0a 20 20 20 20 20 20 20 20 69 66 28 20 6b 3d 3d  .        if( k==
1237f 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  0 ){.          i
12380 66 28 20 21 70 50 72 65 76 54 72 75 6e 6b 20 29  f( !pPrevTrunk )
12381 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d 65  {.            me
12382 6d 63 70 79 28 26 70 50 61 67 65 31 2d 3e 61 44  mcpy(&pPage1->aD
12383 61 74 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b  ata[32], &pTrunk
12384 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a  ->aData[0], 4);.
12385 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
12386 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d  .            mem
12387 63 70 79 28 26 70 50 72 65 76 54 72 75 6e 6b 2d  cpy(&pPrevTrunk-
12388 3e 61 44 61 74 61 5b 30 5d 2c 20 26 70 54 72 75  >aData[0], &pTru
12389 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29  nk->aData[0], 4)
1238a 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
1238b 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1238c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72         /* The tr
1238d 75 6e 6b 20 70 61 67 65 20 69 73 20 72 65 71 75  unk page is requ
1238e 69 72 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c  ired by the call
1238f 65 72 20 62 75 74 20 69 74 20 63 6f 6e 74 61 69  er but it contai
12390 6e 73 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ns .          **
12391 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 66 72 65   pointers to fre
12392 65 2d 6c 69 73 74 20 6c 65 61 76 65 73 2e 20 54  e-list leaves. T
12393 68 65 20 66 69 72 73 74 20 6c 65 61 66 20 62 65  he first leaf be
12394 63 6f 6d 65 73 20 61 20 74 72 75 6e 6b 0a 20 20  comes a trunk.  
12395 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20          ** page 
12396 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 20 20  in this case..  
12397 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
12398 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e       MemPage *pN
12399 65 77 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20  ewTrunk;.       
1239a 20 20 20 50 67 6e 6f 20 69 4e 65 77 54 72 75 6e     Pgno iNewTrun
1239b 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 54  k = get4byte(&pT
1239c 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d 29 3b  runk->aData[8]);
1239d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
1239e 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
1239f 61 67 65 28 70 42 74 2c 20 69 4e 65 77 54 72 75  age(pBt, iNewTru
123a0 6e 6b 2c 20 26 70 4e 65 77 54 72 75 6e 6b 2c 20  nk, &pNewTrunk, 
123a1 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  0);.          if
123a2 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
123a3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67  ){.            g
123a4 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65  oto end_allocate
123a5 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20  _page;.         
123a6 20 7d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20   }.          rc 
123a7 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
123a8 69 74 65 28 70 4e 65 77 54 72 75 6e 6b 2d 3e 70  ite(pNewTrunk->p
123a9 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  DbPage);.       
123aa 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
123ab 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
123ac 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
123ad 70 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20  pNewTrunk);.    
123ae 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64          goto end
123af 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a  _allocate_page;.
123b0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
123b1 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e        memcpy(&pN
123b2 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30  ewTrunk->aData[0
123b3 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74  ], &pTrunk->aDat
123b4 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20  a[0], 4);.      
123b5 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 4e      put4byte(&pN
123b6 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34  ewTrunk->aData[4
123b7 5d 2c 20 6b 2d 31 29 3b 0a 20 20 20 20 20 20 20  ], k-1);.       
123b8 20 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 77 54     memcpy(&pNewT
123b9 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d 2c 20  runk->aData[8], 
123ba 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 31  &pTrunk->aData[1
123bb 32 5d 2c 20 28 6b 2d 31 29 2a 34 29 3b 0a 20 20  2], (k-1)*4);.  
123bc 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50          releaseP
123bd 61 67 65 28 70 4e 65 77 54 72 75 6e 6b 29 3b 0a  age(pNewTrunk);.
123be 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 70            if( !p
123bf 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20  PrevTrunk ){.   
123c0 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74           put4byt
123c1 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61  e(&pPage1->aData
123c2 5b 33 32 5d 2c 20 69 4e 65 77 54 72 75 6e 6b 29  [32], iNewTrunk)
123c3 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
123c4 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  e{.            r
123c5 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
123c6 57 72 69 74 65 28 70 50 72 65 76 54 72 75 6e 6b  Write(pPrevTrunk
123c7 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
123c8 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29          if( rc )
123c9 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
123ca 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74  goto end_allocat
123cb 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20  e_page;.        
123cc 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
123cd 20 20 70 75 74 34 62 79 74 65 28 26 70 50 72 65    put4byte(&pPre
123ce 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d  vTrunk->aData[0]
123cf 2c 20 69 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20  , iNewTrunk);.  
123d0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
123d1 20 20 7d 0a 20 20 20 20 20 20 20 20 70 54 72 75    }.        pTru
123d2 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  nk = 0;.        
123d3 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45  TRACE(("ALLOCATE
123d4 3a 20 25 64 20 74 72 75 6e 6b 20 2d 20 25 64 20  : %d trunk - %d 
123d5 66 72 65 65 20 70 61 67 65 73 20 6c 65 66 74 5c  free pages left\
123d6 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 6e 2d 31 29  n", *pPgno, n-1)
123d7 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  );.#endif.      
123d8 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f  }else{.        /
123d9 2a 20 45 78 74 72 61 63 74 20 61 20 6c 65 61 66  * Extract a leaf
123da 20 66 72 6f 6d 20 74 68 65 20 74 72 75 6e 6b 20   from the trunk 
123db 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 63  */.        int c
123dc 6c 6f 73 65 73 74 3b 0a 20 20 20 20 20 20 20 20  losest;.        
123dd 50 67 6e 6f 20 69 50 61 67 65 3b 0a 20 20 20 20  Pgno iPage;.    
123de 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
123df 72 20 2a 61 44 61 74 61 20 3d 20 70 54 72 75 6e  r *aData = pTrun
123e0 6b 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 20 20  k->aData;.      
123e1 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
123e2 67 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d  gerWrite(pTrunk-
123e3 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  >pDbPage);.     
123e4 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
123e5 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
123e6 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20  allocate_page;. 
123e7 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
123e8 20 69 66 28 20 6e 65 61 72 62 79 3e 30 20 29 7b   if( nearby>0 ){
123e9 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69  .          int i
123ea 2c 20 64 69 73 74 3b 0a 20 20 20 20 20 20 20 20  , dist;.        
123eb 20 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20    closest = 0;. 
123ec 20 20 20 20 20 20 20 20 20 64 69 73 74 20 3d 20           dist = 
123ed 67 65 74 34 62 79 74 65 28 26 61 44 61 74 61 5b  get4byte(&aData[
123ee 38 5d 29 20 2d 20 6e 65 61 72 62 79 3b 0a 20 20  8]) - nearby;.  
123ef 20 20 20 20 20 20 20 20 69 66 28 20 64 69 73 74          if( dist
123f0 3c 30 20 29 20 64 69 73 74 20 3d 20 2d 64 69 73  <0 ) dist = -dis
123f1 74 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72  t;.          for
123f2 28 69 3d 31 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b  (i=1; i<k; i++){
123f3 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74  .            int
123f4 20 64 32 20 3d 20 67 65 74 34 62 79 74 65 28 26   d2 = get4byte(&
123f5 61 44 61 74 61 5b 38 2b 69 2a 34 5d 29 20 2d 20  aData[8+i*4]) - 
123f6 6e 65 61 72 62 79 3b 0a 20 20 20 20 20 20 20 20  nearby;.        
123f7 20 20 20 20 69 66 28 20 64 32 3c 30 20 29 20 64      if( d2<0 ) d
123f8 32 20 3d 20 2d 64 32 3b 0a 20 20 20 20 20 20 20  2 = -d2;.       
123f9 20 20 20 20 20 69 66 28 20 64 32 3c 64 69 73 74       if( d2<dist
123fa 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
123fb 20 20 63 6c 6f 73 65 73 74 20 3d 20 69 3b 0a 20    closest = i;. 
123fc 20 20 20 20 20 20 20 20 20 20 20 20 20 64 69 73               dis
123fd 74 20 3d 20 64 32 3b 0a 20 20 20 20 20 20 20 20  t = d2;.        
123fe 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
123ff 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  }.        }else{
12400 0a 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65  .          close
12401 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  st = 0;.        
12402 7d 0a 0a 20 20 20 20 20 20 20 20 69 50 61 67 65  }..        iPage
12403 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 44 61   = get4byte(&aDa
12404 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 29  ta[8+closest*4])
12405 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 73  ;.        if( !s
12406 65 61 72 63 68 4c 69 73 74 20 7c 7c 20 69 50 61  earchList || iPa
12407 67 65 3d 3d 6e 65 61 72 62 79 20 29 7b 0a 20 20  ge==nearby ){.  
12408 20 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d          *pPgno =
12409 20 69 50 61 67 65 3b 0a 20 20 20 20 20 20 20 20   iPage;.        
1240a 20 20 69 66 28 20 2a 70 50 67 6e 6f 3e 73 71 6c    if( *pPgno>sql
1240b 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75  ite3PagerPagecou
1240c 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 20  nt(pBt->pPager) 
1240d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f  ){.            /
1240e 2a 20 46 72 65 65 20 70 61 67 65 20 6f 66 66 20  * Free page off 
1240f 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66  the end of the f
12410 69 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  ile */.         
12411 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43     rc = SQLITE_C
12412 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
12413 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e           goto en
12414 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b  d_allocate_page;
12415 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
12416 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41         TRACE(("A
12417 4c 4c 4f 43 41 54 45 3a 20 25 64 20 77 61 73 20  LLOCATE: %d was 
12418 6c 65 61 66 20 25 64 20 6f 66 20 25 64 20 6f 6e  leaf %d of %d on
12419 20 74 72 75 6e 6b 20 25 64 22 0a 20 20 20 20 20   trunk %d".     
1241a 20 20 20 20 20 20 20 20 20 20 20 20 22 3a 20 25              ": %
1241b 64 20 6d 6f 72 65 20 66 72 65 65 20 70 61 67 65  d more free page
1241c 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  s\n",.          
1241d 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 2c 20 63         *pPgno, c
1241e 6c 6f 73 65 73 74 2b 31 2c 20 6b 2c 20 70 54 72  losest+1, k, pTr
1241f 75 6e 6b 2d 3e 70 67 6e 6f 2c 20 6e 2d 31 29 29  unk->pgno, n-1))
12420 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
12421 63 6c 6f 73 65 73 74 3c 6b 2d 31 20 29 7b 0a 20  closest<k-1 ){. 
12422 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70             memcp
12423 79 28 26 61 44 61 74 61 5b 38 2b 63 6c 6f 73 65  y(&aData[8+close
12424 73 74 2a 34 5d 2c 20 26 61 44 61 74 61 5b 34 2b  st*4], &aData[4+
12425 6b 2a 34 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20  k*4], 4);.      
12426 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
12427 70 75 74 34 62 79 74 65 28 26 61 44 61 74 61 5b  put4byte(&aData[
12428 34 5d 2c 20 6b 2d 31 29 3b 0a 20 20 20 20 20 20  4], k-1);.      
12429 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1242a 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74  BtreeGetPage(pBt
1242b 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61 67 65  , *pPgno, ppPage
1242c 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 1);.          
1242d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1242e 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  K ){.           
1242f 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e   sqlite3PagerDon
12430 74 52 6f 6c 6c 62 61 63 6b 28 28 2a 70 70 50 61  tRollback((*ppPa
12431 67 65 29 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  ge)->pDbPage);. 
12432 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
12433 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
12434 65 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62  e((*ppPage)->pDb
12435 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Page);.         
12436 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
12437 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
12438 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
12439 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20  e(*ppPage);.    
1243a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1243b 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1243c 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a  searchList = 0;.
1243d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1243e 7d 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50  }.      releaseP
1243f 61 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29 3b  age(pPrevTrunk);
12440 0a 20 20 20 20 20 20 70 50 72 65 76 54 72 75 6e  .      pPrevTrun
12441 6b 20 3d 20 30 3b 0a 20 20 20 20 7d 77 68 69 6c  k = 0;.    }whil
12442 65 28 20 73 65 61 72 63 68 4c 69 73 74 20 29 3b  e( searchList );
12443 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
12444 20 54 68 65 72 65 20 61 72 65 20 6e 6f 20 70 61   There are no pa
12445 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c  ges on the freel
12446 69 73 74 2c 20 73 6f 20 63 72 65 61 74 65 20 61  ist, so create a
12447 20 6e 65 77 20 70 61 67 65 20 61 74 20 74 68 65   new page at the
12448 0a 20 20 20 20 2a 2a 20 65 6e 64 20 6f 66 20 74  .    ** end of t
12449 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 2a  he file */.    *
1244a 70 50 67 6e 6f 20 3d 20 73 71 6c 69 74 65 33 50  pPgno = sqlite3P
1244b 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42  agerPagecount(pB
1244c 74 2d 3e 70 50 61 67 65 72 29 20 2b 20 31 3b 0a  t->pPager) + 1;.
1244d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1244e 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
1244f 20 20 20 20 69 66 28 20 70 42 74 2d 3e 6e 54 72      if( pBt->nTr
12450 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  unc ){.      /* 
12451 41 6e 20 69 6e 63 72 2d 76 61 63 75 75 6d 20 68  An incr-vacuum h
12452 61 73 20 61 6c 72 65 61 64 79 20 72 75 6e 20 77  as already run w
12453 69 74 68 69 6e 20 74 68 69 73 20 74 72 61 6e 73  ithin this trans
12454 61 63 74 69 6f 6e 2e 20 53 6f 20 74 68 65 0a 20  action. So the. 
12455 20 20 20 20 20 2a 2a 20 70 61 67 65 20 74 6f 20       ** page to 
12456 61 6c 6c 6f 63 61 74 65 20 69 73 20 6e 6f 74 20  allocate is not 
12457 66 72 6f 6d 20 74 68 65 20 70 68 79 73 69 63 61  from the physica
12458 6c 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c  l end of the fil
12459 65 2c 20 62 75 74 0a 20 20 20 20 20 20 2a 2a 20  e, but.      ** 
1245a 61 74 20 70 42 74 2d 3e 6e 54 72 75 6e 63 2e 20  at pBt->nTrunc. 
1245b 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
1245c 2a 70 50 67 6e 6f 20 3d 20 70 42 74 2d 3e 6e 54  *pPgno = pBt->nT
1245d 72 75 6e 63 2b 31 3b 0a 20 20 20 20 20 20 69 66  runc+1;.      if
1245e 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e  ( *pPgno==PENDIN
1245f 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29  G_BYTE_PAGE(pBt)
12460 20 29 7b 0a 20 20 20 20 20 20 20 20 28 2a 70 50   ){.        (*pP
12461 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a  gno)++;.      }.
12462 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 42      }.    if( pB
12463 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26  t->autoVacuum &&
12464 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70   PTRMAP_ISPAGE(p
12465 42 74 2c 20 2a 70 50 67 6e 6f 29 20 29 7b 0a 20  Bt, *pPgno) ){. 
12466 20 20 20 20 20 2f 2a 20 49 66 20 2a 70 50 67 6e       /* If *pPgn
12467 6f 20 72 65 66 65 72 73 20 74 6f 20 61 20 70 6f  o refers to a po
12468 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20  inter-map page, 
12469 61 6c 6c 6f 63 61 74 65 20 74 77 6f 20 6e 65 77  allocate two new
1246a 20 70 61 67 65 73 0a 20 20 20 20 20 20 2a 2a 20   pages.      ** 
1246b 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  at the end of th
1246c 65 20 66 69 6c 65 20 69 6e 73 74 65 61 64 20 6f  e file instead o
1246d 66 20 6f 6e 65 2e 20 54 68 65 20 66 69 72 73 74  f one. The first
1246e 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 0a   allocated page.
1246f 20 20 20 20 20 20 2a 2a 20 62 65 63 6f 6d 65 73        ** becomes
12470 20 61 20 6e 65 77 20 70 6f 69 6e 74 65 72 2d 6d   a new pointer-m
12471 61 70 20 70 61 67 65 2c 20 74 68 65 20 73 65 63  ap page, the sec
12472 6f 6e 64 20 69 73 20 75 73 65 64 20 62 79 20 74  ond is used by t
12473 68 65 20 63 61 6c 6c 65 72 2e 0a 20 20 20 20 20  he caller..     
12474 20 2a 2f 0a 20 20 20 20 20 20 54 52 41 43 45 28   */.      TRACE(
12475 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66  ("ALLOCATE: %d f
12476 72 6f 6d 20 65 6e 64 20 6f 66 20 66 69 6c 65 20  rom end of file 
12477 28 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67  (pointer-map pag
12478 65 29 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b  e)\n", *pPgno));
12479 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2a  .      assert( *
1247a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42  pPgno!=PENDING_B
1247b 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b  YTE_PAGE(pBt) );
1247c 0a 20 20 20 20 20 20 28 2a 70 50 67 6e 6f 29 2b  .      (*pPgno)+
1247d 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 2a 70 50  +;.      if( *pP
1247e 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54  gno==PENDING_BYT
1247f 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 20 28  E_PAGE(pBt) ){ (
12480 2a 70 50 67 6e 6f 29 2b 2b 3b 20 7d 0a 20 20 20  *pPgno)++; }.   
12481 20 7d 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e   }.    if( pBt->
12482 6e 54 72 75 6e 63 20 29 7b 0a 20 20 20 20 20 20  nTrunc ){.      
12483 70 42 74 2d 3e 6e 54 72 75 6e 63 20 3d 20 2a 70  pBt->nTrunc = *p
12484 50 67 6e 6f 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  Pgno;.    }.#end
12485 69 66 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20  if..    assert( 
12486 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f  *pPgno!=PENDING_
12487 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29  BYTE_PAGE(pBt) )
12488 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
12489 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70  e3BtreeGetPage(p
1248a 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61  Bt, *pPgno, ppPa
1248b 67 65 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  ge, 0);.    if( 
1248c 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
1248d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1248e 50 61 67 65 72 57 72 69 74 65 28 28 2a 70 70 50  PagerWrite((*ppP
1248f 61 67 65 29 2d 3e 70 44 62 50 61 67 65 29 3b 0a  age)->pDbPage);.
12490 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
12491 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
12492 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 50 61  eleasePage(*ppPa
12493 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 54  ge);.    }.    T
12494 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a  RACE(("ALLOCATE:
12495 20 25 64 20 66 72 6f 6d 20 65 6e 64 20 6f 66 20   %d from end of 
12496 66 69 6c 65 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29  file\n", *pPgno)
12497 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  );.  }..  assert
12498 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e  ( *pPgno!=PENDIN
12499 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29  G_BYTE_PAGE(pBt)
1249a 20 29 3b 0a 0a 65 6e 64 5f 61 6c 6c 6f 63 61 74   );..end_allocat
1249b 65 5f 70 61 67 65 3a 0a 20 20 72 65 6c 65 61 73  e_page:.  releas
1249c 65 50 61 67 65 28 70 54 72 75 6e 6b 29 3b 0a 20  ePage(pTrunk);. 
1249d 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 72   releasePage(pPr
1249e 65 76 54 72 75 6e 6b 29 3b 0a 20 20 72 65 74 75  evTrunk);.  retu
1249f 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
124a0 41 64 64 20 61 20 70 61 67 65 20 6f 66 20 74 68  Add a page of th
124a1 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
124a2 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e  to the freelist.
124a3 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61  .**.** sqlite3Pa
124a4 67 65 72 55 6e 72 65 66 28 29 20 69 73 20 4e 4f  gerUnref() is NO
124a5 54 20 63 61 6c 6c 65 64 20 66 6f 72 20 70 50 61  T called for pPa
124a6 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ge..*/.static in
124a7 74 20 66 72 65 65 50 61 67 65 28 4d 65 6d 50 61  t freePage(MemPa
124a8 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 42 74  ge *pPage){.  Bt
124a9 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50  Shared *pBt = pP
124aa 61 67 65 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50  age->pBt;.  MemP
124ab 61 67 65 20 2a 70 50 61 67 65 31 20 3d 20 70 42  age *pPage1 = pB
124ac 74 2d 3e 70 50 61 67 65 31 3b 0a 20 20 69 6e 74  t->pPage1;.  int
124ad 20 72 63 2c 20 6e 2c 20 6b 3b 0a 0a 20 20 2f 2a   rc, n, k;..  /*
124ae 20 50 72 65 70 61 72 65 20 74 68 65 20 70 61 67   Prepare the pag
124af 65 20 66 6f 72 20 66 72 65 65 69 6e 67 20 2a 2f  e for freeing */
124b0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
124b1 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50  e3_mutex_held(pP
124b2 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  age->pBt->mutex)
124b3 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
124b4 61 67 65 2d 3e 70 67 6e 6f 3e 31 20 29 3b 0a 20  age->pgno>1 );. 
124b5 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d   pPage->isInit =
124b6 20 30 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 67   0;.  releasePag
124b7 65 28 70 50 61 67 65 2d 3e 70 50 61 72 65 6e 74  e(pPage->pParent
124b8 29 3b 0a 20 20 70 50 61 67 65 2d 3e 70 50 61 72  );.  pPage->pPar
124b9 65 6e 74 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 49  ent = 0;..  /* I
124ba 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 66 72 65  ncrement the fre
124bb 65 20 70 61 67 65 20 63 6f 75 6e 74 20 6f 6e 20  e page count on 
124bc 70 50 61 67 65 31 20 2a 2f 0a 20 20 72 63 20 3d  pPage1 */.  rc =
124bd 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
124be 74 65 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61  te(pPage1->pDbPa
124bf 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20  ge);.  if( rc ) 
124c0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 6e 20 3d  return rc;.  n =
124c1 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65   get4byte(&pPage
124c2 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20  1->aData[36]);. 
124c3 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65   put4byte(&pPage
124c4 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 6e 2b  1->aData[36], n+
124c5 31 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  1);..#ifdef SQLI
124c6 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45  TE_SECURE_DELETE
124c7 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 53 51 4c  .  /* If the SQL
124c8 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54  ITE_SECURE_DELET
124c9 45 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f  E compile-time o
124ca 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64  ption is enabled
124cb 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 61 6c 77 61  , then.  ** alwa
124cc 79 73 20 66 75 6c 6c 79 20 6f 76 65 72 77 72 69  ys fully overwri
124cd 74 65 20 64 65 6c 65 74 65 64 20 69 6e 66 6f 72  te deleted infor
124ce 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a 65 72 6f  mation with zero
124cf 73 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73  s..  */.  rc = s
124d0 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
124d1 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
124d2 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74  ;.  if( rc ) ret
124d3 75 72 6e 20 72 63 3b 0a 20 20 6d 65 6d 73 65 74  urn rc;.  memset
124d4 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 30  (pPage->aData, 0
124d5 2c 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 70 61  , pPage->pBt->pa
124d6 67 65 53 69 7a 65 29 3b 0a 23 65 6e 64 69 66 0a  geSize);.#endif.
124d7 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
124d8 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
124d9 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61    /* If the data
124da 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75  base supports au
124db 74 6f 2d 76 61 63 75 75 6d 2c 20 77 72 69 74 65  to-vacuum, write
124dc 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74 68 65   an entry in the
124dd 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20 2a   pointer-map.  *
124de 2a 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68  * to indicate th
124df 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20 66  at the page is f
124e0 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ree..  */.  if( 
124e1 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
124e2 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d  ){.    rc = ptrm
124e3 61 70 50 75 74 28 70 42 74 2c 20 70 50 61 67 65  apPut(pBt, pPage
124e4 2d 3e 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 46  ->pgno, PTRMAP_F
124e5 52 45 45 50 41 47 45 2c 20 30 29 3b 0a 20 20 20  REEPAGE, 0);.   
124e6 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
124e7 20 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a   rc;.  }.#endif.
124e8 0a 20 20 69 66 28 20 6e 3d 3d 30 20 29 7b 0a 20  .  if( n==0 ){. 
124e9 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68     /* This is th
124ea 65 20 66 69 72 73 74 20 66 72 65 65 20 70 61 67  e first free pag
124eb 65 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71  e */.    rc = sq
124ec 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
124ed 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b  pPage->pDbPage);
124ee 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65  .    if( rc ) re
124ef 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 6d 65 6d  turn rc;.    mem
124f0 73 65 74 28 70 50 61 67 65 2d 3e 61 44 61 74 61  set(pPage->aData
124f1 2c 20 30 2c 20 38 29 3b 0a 20 20 20 20 70 75 74  , 0, 8);.    put
124f2 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61  4byte(&pPage1->a
124f3 44 61 74 61 5b 33 32 5d 2c 20 70 50 61 67 65 2d  Data[32], pPage-
124f4 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 54 52 41 43  >pgno);.    TRAC
124f5 45 28 28 22 46 52 45 45 2d 50 41 47 45 3a 20 25  E(("FREE-PAGE: %
124f6 64 20 66 69 72 73 74 5c 6e 22 2c 20 70 50 61 67  d first\n", pPag
124f7 65 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 7d 65 6c  e->pgno));.  }el
124f8 73 65 7b 0a 20 20 20 20 2f 2a 20 4f 74 68 65 72  se{.    /* Other
124f9 20 66 72 65 65 20 70 61 67 65 73 20 61 6c 72 65   free pages alre
124fa 61 64 79 20 65 78 69 73 74 2e 20 20 52 65 74 72  ady exist.  Retr
124fb 69 76 65 20 74 68 65 20 66 69 72 73 74 20 74 72  ive the first tr
124fc 75 6e 6b 20 70 61 67 65 0a 20 20 20 20 2a 2a 20  unk page.    ** 
124fd 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73 74 20  of the freelist 
124fe 61 6e 64 20 66 69 6e 64 20 6f 75 74 20 68 6f 77  and find out how
124ff 20 6d 61 6e 79 20 6c 65 61 76 65 73 20 69 74 20   many leaves it 
12500 68 61 73 2e 20 2a 2f 0a 20 20 20 20 4d 65 6d 50  has. */.    MemP
12501 61 67 65 20 2a 70 54 72 75 6e 6b 3b 0a 20 20 20  age *pTrunk;.   
12502 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
12503 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 67  eeGetPage(pBt, g
12504 65 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d  et4byte(&pPage1-
12505 3e 61 44 61 74 61 5b 33 32 5d 29 2c 20 26 70 54  >aData[32]), &pT
12506 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 69 66  runk, 0);.    if
12507 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
12508 3b 0a 20 20 20 20 6b 20 3d 20 67 65 74 34 62 79  ;.    k = get4by
12509 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74  te(&pTrunk->aDat
1250a 61 5b 34 5d 29 3b 0a 20 20 20 20 69 66 28 20 6b  a[4]);.    if( k
1250b 3e 3d 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  >=pBt->usableSiz
1250c 65 2f 34 20 2d 20 38 20 29 7b 0a 20 20 20 20 20  e/4 - 8 ){.     
1250d 20 2f 2a 20 54 68 65 20 74 72 75 6e 6b 20 69 73   /* The trunk is
1250e 20 66 75 6c 6c 2e 20 20 54 75 72 6e 20 74 68 65   full.  Turn the
1250f 20 70 61 67 65 20 62 65 69 6e 67 20 66 72 65 65   page being free
12510 64 20 69 6e 74 6f 20 61 20 6e 65 77 0a 20 20 20  d into a new.   
12511 20 20 20 2a 2a 20 74 72 75 6e 6b 20 70 61 67 65     ** trunk page
12512 20 77 69 74 68 20 6e 6f 20 6c 65 61 76 65 73 2e   with no leaves.
12513 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73   */.      rc = s
12514 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
12515 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
12516 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
12517 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
12518 20 20 20 20 20 70 75 74 34 62 79 74 65 28 70 50       put4byte(pP
12519 61 67 65 2d 3e 61 44 61 74 61 2c 20 70 54 72 75  age->aData, pTru
1251a 6e 6b 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20  nk->pgno);.     
1251b 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61     put4byte(&pPa
1251c 67 65 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 30 29  ge->aData[4], 0)
1251d 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34 62 79  ;.        put4by
1251e 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74  te(&pPage1->aDat
1251f 61 5b 33 32 5d 2c 20 70 50 61 67 65 2d 3e 70 67  a[32], pPage->pg
12520 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 54 52 41  no);.        TRA
12521 43 45 28 28 22 46 52 45 45 2d 50 41 47 45 3a 20  CE(("FREE-PAGE: 
12522 25 64 20 6e 65 77 20 74 72 75 6e 6b 20 70 61 67  %d new trunk pag
12523 65 20 72 65 70 6c 61 63 69 6e 67 20 25 64 5c 6e  e replacing %d\n
12524 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
12525 20 20 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20     pPage->pgno, 
12526 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29 29 3b 0a  pTrunk->pgno));.
12527 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
12528 65 20 69 66 28 20 6b 3c 30 20 29 7b 0a 20 20 20  e if( k<0 ){.   
12529 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43     rc = SQLITE_C
1252a 4f 52 52 55 50 54 3b 0a 20 20 20 20 7d 65 6c 73  ORRUPT;.    }els
1252b 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 64 64 20  e{.      /* Add 
1252c 74 68 65 20 6e 65 77 6c 79 20 66 72 65 65 64 20  the newly freed 
1252d 70 61 67 65 20 61 73 20 61 20 6c 65 61 66 20 6f  page as a leaf o
1252e 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72  n the current tr
1252f 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 72 63 20  unk */.      rc 
12530 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
12531 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50  ite(pTrunk->pDbP
12532 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  age);.      if( 
12533 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
12534 0a 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74  .        put4byt
12535 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61  e(&pTrunk->aData
12536 5b 34 5d 2c 20 6b 2b 31 29 3b 0a 20 20 20 20 20  [4], k+1);.     
12537 20 20 20 70 75 74 34 62 79 74 65 28 26 70 54 72     put4byte(&pTr
12538 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 2b 6b 2a 34  unk->aData[8+k*4
12539 5d 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b  ], pPage->pgno);
1253a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1253b 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20  SECURE_DELETE.  
1253c 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
1253d 65 72 44 6f 6e 74 57 72 69 74 65 28 70 50 61 67  erDontWrite(pPag
1253e 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 23 65 6e  e->pDbPage);.#en
1253f 64 69 66 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  dif.      }.    
12540 20 20 54 52 41 43 45 28 28 22 46 52 45 45 2d 50    TRACE(("FREE-P
12541 41 47 45 3a 20 25 64 20 6c 65 61 66 20 6f 6e 20  AGE: %d leaf on 
12542 74 72 75 6e 6b 20 70 61 67 65 20 25 64 5c 6e 22  trunk page %d\n"
12543 2c 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 70 54 72  ,pPage->pgno,pTr
12544 75 6e 6b 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20  unk->pgno));.   
12545 20 7d 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61   }.    releasePa
12546 67 65 28 70 54 72 75 6e 6b 29 3b 0a 20 20 7d 0a  ge(pTrunk);.  }.
12547 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
12548 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 79 20 6f  /*.** Free any o
12549 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 73  verflow pages as
1254a 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
1254b 65 20 67 69 76 65 6e 20 43 65 6c 6c 2e 0a 2a 2f  e given Cell..*/
1254c 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6c 65 61  .static int clea
1254d 72 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70  rCell(MemPage *p
1254e 50 61 67 65 2c 20 75 6e 73 69 67 6e 65 64 20 63  Page, unsigned c
1254f 68 61 72 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 42  har *pCell){.  B
12550 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
12551 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 43 65 6c  Page->pBt;.  Cel
12552 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 50 67  lInfo info;.  Pg
12553 6e 6f 20 6f 76 66 6c 50 67 6e 6f 3b 0a 20 20 69  no ovflPgno;.  i
12554 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 6e 4f 76  nt rc;.  int nOv
12555 66 6c 3b 0a 20 20 69 6e 74 20 6f 76 66 6c 50 61  fl;.  int ovflPa
12556 67 65 53 69 7a 65 3b 0a 0a 20 20 61 73 73 65 72  geSize;..  asser
12557 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
12558 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
12559 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71  ->mutex) );.  sq
1255a 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43  lite3BtreeParseC
1255b 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43  ellPtr(pPage, pC
1255c 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 69  ell, &info);.  i
1255d 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f  f( info.iOverflo
1255e 77 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  w==0 ){.    retu
1255f 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 2f  rn SQLITE_OK;  /
12560 2a 20 4e 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61  * No overflow pa
12561 67 65 73 2e 20 52 65 74 75 72 6e 20 77 69 74 68  ges. Return with
12562 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 74 68 69  out doing anythi
12563 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 6f 76 66 6c  ng */.  }.  ovfl
12564 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28  Pgno = get4byte(
12565 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65  &pCell[info.iOve
12566 72 66 6c 6f 77 5d 29 3b 0a 20 20 6f 76 66 6c 50  rflow]);.  ovflP
12567 61 67 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75  ageSize = pBt->u
12568 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20  sableSize - 4;. 
12569 20 6e 4f 76 66 6c 20 3d 20 28 69 6e 66 6f 2e 6e   nOvfl = (info.n
1256a 50 61 79 6c 6f 61 64 20 2d 20 69 6e 66 6f 2e 6e  Payload - info.n
1256b 4c 6f 63 61 6c 20 2b 20 6f 76 66 6c 50 61 67 65  Local + ovflPage
1256c 53 69 7a 65 20 2d 20 31 29 2f 6f 76 66 6c 50 61  Size - 1)/ovflPa
1256d 67 65 53 69 7a 65 3b 0a 20 20 61 73 73 65 72 74  geSize;.  assert
1256e 28 20 6f 76 66 6c 50 67 6e 6f 3d 3d 30 20 7c 7c  ( ovflPgno==0 ||
1256f 20 6e 4f 76 66 6c 3e 30 20 29 3b 0a 20 20 77 68   nOvfl>0 );.  wh
12570 69 6c 65 28 20 6e 4f 76 66 6c 2d 2d 20 29 7b 0a  ile( nOvfl-- ){.
12571 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f 76      MemPage *pOv
12572 66 6c 3b 0a 20 20 20 20 69 66 28 20 6f 76 66 6c  fl;.    if( ovfl
12573 50 67 6e 6f 3d 3d 30 20 7c 7c 20 6f 76 66 6c 50  Pgno==0 || ovflP
12574 67 6e 6f 3e 73 71 6c 69 74 65 33 50 61 67 65 72  gno>sqlite3Pager
12575 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70  Pagecount(pBt->p
12576 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20  Pager) ){.      
12577 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
12578 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
12579 7d 0a 0a 20 20 20 20 72 63 20 3d 20 67 65 74 4f  }..    rc = getO
1257a 76 65 72 66 6c 6f 77 50 61 67 65 28 70 42 74 2c  verflowPage(pBt,
1257b 20 6f 76 66 6c 50 67 6e 6f 2c 20 26 70 4f 76 66   ovflPgno, &pOvf
1257c 6c 2c 20 28 6e 4f 76 66 6c 3d 3d 30 29 3f 30 3a  l, (nOvfl==0)?0:
1257d 26 6f 76 66 6c 50 67 6e 6f 29 3b 0a 20 20 20 20  &ovflPgno);.    
1257e 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
1257f 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 66 72 65  rc;.    rc = fre
12580 65 50 61 67 65 28 70 4f 76 66 6c 29 3b 0a 20 20  ePage(pOvfl);.  
12581 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
12582 72 65 66 28 70 4f 76 66 6c 2d 3e 70 44 62 50 61  ref(pOvfl->pDbPa
12583 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  ge);.    if( rc 
12584 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d  ) return rc;.  }
12585 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
12586 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72  _OK;.}../*.** Cr
12587 65 61 74 65 20 74 68 65 20 62 79 74 65 20 73 65  eate the byte se
12588 71 75 65 6e 63 65 20 75 73 65 64 20 74 6f 20 72  quence used to r
12589 65 70 72 65 73 65 6e 74 20 61 20 63 65 6c 6c 20  epresent a cell 
1258a 6f 6e 20 70 61 67 65 20 70 50 61 67 65 0a 2a 2a  on page pPage.**
1258b 20 61 6e 64 20 77 72 69 74 65 20 74 68 61 74 20   and write that 
1258c 62 79 74 65 20 73 65 71 75 65 6e 63 65 20 69 6e  byte sequence in
1258d 74 6f 20 70 43 65 6c 6c 5b 5d 2e 20 20 4f 76 65  to pCell[].  Ove
1258e 72 66 6c 6f 77 20 70 61 67 65 73 20 61 72 65 0a  rflow pages are.
1258f 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  ** allocated and
12590 20 66 69 6c 6c 65 64 20 69 6e 20 61 73 20 6e 65   filled in as ne
12591 63 65 73 73 61 72 79 2e 20 20 54 68 65 20 63 61  cessary.  The ca
12592 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 0a  lling procedure.
12593 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  ** is responsibl
12594 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73 75 72  e for making sur
12595 65 20 73 75 66 66 69 63 69 65 6e 74 20 73 70 61  e sufficient spa
12596 63 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f  ce has been allo
12597 63 61 74 65 64 0a 2a 2a 20 66 6f 72 20 70 43 65  cated.** for pCe
12598 6c 6c 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65  ll[]..**.** Note
12599 20 74 68 61 74 20 70 43 65 6c 6c 20 64 6f 65 73   that pCell does
1259a 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20 6e   not necessary n
1259b 65 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  eed to point to 
1259c 74 68 65 20 70 50 61 67 65 2d 3e 61 44 61 74 61  the pPage->aData
1259d 0a 2a 2a 20 61 72 65 61 2e 20 20 70 43 65 6c 6c  .** area.  pCell
1259e 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f 20   might point to 
1259f 73 6f 6d 65 20 74 65 6d 70 6f 72 61 72 79 20 73  some temporary s
125a0 74 6f 72 61 67 65 2e 20 20 54 68 65 20 63 65 6c  torage.  The cel
125a1 6c 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 63 6f 6e  l will.** be con
125a2 73 74 72 75 63 74 65 64 20 69 6e 20 74 68 69 73  structed in this
125a3 20 74 65 6d 70 6f 72 61 72 79 20 61 72 65 61 20   temporary area 
125a4 74 68 65 6e 20 63 6f 70 69 65 64 20 69 6e 74 6f  then copied into
125a5 20 70 50 61 67 65 2d 3e 61 44 61 74 61 0a 2a 2a   pPage->aData.**
125a6 20 6c 61 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69   later..*/.stati
125a7 63 20 69 6e 74 20 66 69 6c 6c 49 6e 43 65 6c 6c  c int fillInCell
125a8 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  (.  MemPage *pPa
125a9 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ge,             
125aa 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74     /* The page t
125ab 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  hat contains the
125ac 20 63 65 6c 6c 20 2a 2f 0a 20 20 75 6e 73 69 67   cell */.  unsig
125ad 6e 65 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 2c  ned char *pCell,
125ae 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d            /* Com
125af 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68  plete text of th
125b0 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 63 6f 6e 73  e cell */.  cons
125b1 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36  t void *pKey, i6
125b2 34 20 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 54 68  4 nKey,    /* Th
125b3 65 20 6b 65 79 20 2a 2f 0a 20 20 63 6f 6e 73 74  e key */.  const
125b4 20 76 6f 69 64 20 2a 70 44 61 74 61 2c 69 6e 74   void *pData,int
125b5 20 6e 44 61 74 61 2c 20 20 20 2f 2a 20 54 68 65   nData,   /* The
125b6 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6e   data */.  int n
125b7 5a 65 72 6f 2c 20 20 20 20 20 20 20 20 20 20 20  Zero,           
125b8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74            /* Ext
125b9 72 61 20 7a 65 72 6f 20 62 79 74 65 73 20 74 6f  ra zero bytes to
125ba 20 61 70 70 65 6e 64 20 74 6f 20 70 44 61 74 61   append to pData
125bb 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 53 69 7a   */.  int *pnSiz
125bc 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
125bd 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 65       /* Write ce
125be 6c 6c 20 73 69 7a 65 20 68 65 72 65 20 2a 2f 0a  ll size here */.
125bf 29 7b 0a 20 20 69 6e 74 20 6e 50 61 79 6c 6f 61  ){.  int nPayloa
125c0 64 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 70  d;.  const u8 *p
125c1 53 72 63 3b 0a 20 20 69 6e 74 20 6e 53 72 63 2c  Src;.  int nSrc,
125c2 20 6e 2c 20 72 63 3b 0a 20 20 69 6e 74 20 73 70   n, rc;.  int sp
125c3 61 63 65 4c 65 66 74 3b 0a 20 20 4d 65 6d 50 61  aceLeft;.  MemPa
125c4 67 65 20 2a 70 4f 76 66 6c 20 3d 20 30 3b 0a 20  ge *pOvfl = 0;. 
125c5 20 4d 65 6d 50 61 67 65 20 2a 70 54 6f 52 65 6c   MemPage *pToRel
125c6 65 61 73 65 20 3d 20 30 3b 0a 20 20 75 6e 73 69  ease = 0;.  unsi
125c7 67 6e 65 64 20 63 68 61 72 20 2a 70 50 72 69 6f  gned char *pPrio
125c8 72 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  r;.  unsigned ch
125c9 61 72 20 2a 70 50 61 79 6c 6f 61 64 3b 0a 20 20  ar *pPayload;.  
125ca 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
125cb 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 50 67  pPage->pBt;.  Pg
125cc 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 30 3b  no pgnoOvfl = 0;
125cd 0a 20 20 69 6e 74 20 6e 48 65 61 64 65 72 3b 0a  .  int nHeader;.
125ce 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b    CellInfo info;
125cf 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
125d0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
125d1 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
125d2 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c 20  ) );..  /* Fill 
125d3 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e 20 2a  in the header. *
125d4 2f 0a 20 20 6e 48 65 61 64 65 72 20 3d 20 30 3b  /.  nHeader = 0;
125d5 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c  .  if( !pPage->l
125d6 65 61 66 20 29 7b 0a 20 20 20 20 6e 48 65 61 64  eaf ){.    nHead
125d7 65 72 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 69  er += 4;.  }.  i
125d8 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74  f( pPage->hasDat
125d9 61 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 72  a ){.    nHeader
125da 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 28 26 70   += putVarint(&p
125db 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 6e  Cell[nHeader], n
125dc 44 61 74 61 2b 6e 5a 65 72 6f 29 3b 0a 20 20 7d  Data+nZero);.  }
125dd 65 6c 73 65 7b 0a 20 20 20 20 6e 44 61 74 61 20  else{.    nData 
125de 3d 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 7d  = nZero = 0;.  }
125df 0a 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 70 75  .  nHeader += pu
125e0 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e  tVarint(&pCell[n
125e1 48 65 61 64 65 72 5d 2c 20 2a 28 75 36 34 2a 29  Header], *(u64*)
125e2 26 6e 4b 65 79 29 3b 0a 20 20 73 71 6c 69 74 65  &nKey);.  sqlite
125e3 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50  3BtreeParseCellP
125e4 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c  tr(pPage, pCell,
125e5 20 26 69 6e 66 6f 29 3b 0a 20 20 61 73 73 65 72   &info);.  asser
125e6 74 28 20 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3d  t( info.nHeader=
125e7 3d 6e 48 65 61 64 65 72 20 29 3b 0a 20 20 61 73  =nHeader );.  as
125e8 73 65 72 74 28 20 69 6e 66 6f 2e 6e 4b 65 79 3d  sert( info.nKey=
125e9 3d 6e 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 72  =nKey );.  asser
125ea 74 28 20 69 6e 66 6f 2e 6e 44 61 74 61 3d 3d 6e  t( info.nData==n
125eb 44 61 74 61 2b 6e 5a 65 72 6f 20 29 3b 0a 20 20  Data+nZero );.  
125ec 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68  .  /* Fill in th
125ed 65 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 6e  e payload */.  n
125ee 50 61 79 6c 6f 61 64 20 3d 20 6e 44 61 74 61 20  Payload = nData 
125ef 2b 20 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 20 70  + nZero;.  if( p
125f0 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a  Page->intKey ){.
125f1 20 20 20 20 70 53 72 63 20 3d 20 70 44 61 74 61      pSrc = pData
125f2 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 61  ;.    nSrc = nDa
125f3 74 61 3b 0a 20 20 20 20 6e 44 61 74 61 20 3d 20  ta;.    nData = 
125f4 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  0;.  }else{.    
125f5 6e 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65 79  nPayload += nKey
125f6 3b 0a 20 20 20 20 70 53 72 63 20 3d 20 70 4b 65  ;.    pSrc = pKe
125f7 79 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 6e 4b  y;.    nSrc = nK
125f8 65 79 3b 0a 20 20 7d 0a 20 20 2a 70 6e 53 69 7a  ey;.  }.  *pnSiz
125f9 65 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b 0a  e = info.nSize;.
125fa 20 20 73 70 61 63 65 4c 65 66 74 20 3d 20 69 6e    spaceLeft = in
125fb 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 70 50 61  fo.nLocal;.  pPa
125fc 79 6c 6f 61 64 20 3d 20 26 70 43 65 6c 6c 5b 6e  yload = &pCell[n
125fd 48 65 61 64 65 72 5d 3b 0a 20 20 70 50 72 69 6f  Header];.  pPrio
125fe 72 20 3d 20 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e  r = &pCell[info.
125ff 69 4f 76 65 72 66 6c 6f 77 5d 3b 0a 0a 20 20 77  iOverflow];..  w
12600 68 69 6c 65 28 20 6e 50 61 79 6c 6f 61 64 3e 30  hile( nPayload>0
12601 20 29 7b 0a 20 20 20 20 69 66 28 20 73 70 61 63   ){.    if( spac
12602 65 4c 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20  eLeft==0 ){.    
12603 20 20 69 6e 74 20 69 73 45 78 61 63 74 20 3d 20    int isExact = 
12604 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  0;.#ifndef SQLIT
12605 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
12606 4d 0a 20 20 20 20 20 20 50 67 6e 6f 20 70 67 6e  M.      Pgno pgn
12607 6f 50 74 72 6d 61 70 20 3d 20 70 67 6e 6f 4f 76  oPtrmap = pgnoOv
12608 66 6c 3b 20 2f 2a 20 4f 76 65 72 66 6c 6f 77 20  fl; /* Overflow 
12609 70 61 67 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70  page pointer-map
1260a 20 65 6e 74 72 79 20 70 61 67 65 20 2a 2f 0a 20   entry page */. 
1260b 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75       if( pBt->au
1260c 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20  toVacuum ){.    
1260d 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 20 20      do{.        
1260e 20 20 70 67 6e 6f 4f 76 66 6c 2b 2b 3b 0a 20 20    pgnoOvfl++;.  
1260f 20 20 20 20 20 20 7d 20 77 68 69 6c 65 28 20 0a        } while( .
12610 20 20 20 20 20 20 20 20 20 20 50 54 52 4d 41 50            PTRMAP
12611 5f 49 53 50 41 47 45 28 70 42 74 2c 20 70 67 6e  _ISPAGE(pBt, pgn
12612 6f 4f 76 66 6c 29 20 7c 7c 20 70 67 6e 6f 4f 76  oOvfl) || pgnoOv
12613 66 6c 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45  fl==PENDING_BYTE
12614 5f 50 41 47 45 28 70 42 74 29 20 0a 20 20 20 20  _PAGE(pBt) .    
12615 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69      );.        i
12616 66 28 20 70 67 6e 6f 4f 76 66 6c 3e 31 20 29 7b  f( pgnoOvfl>1 ){
12617 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 73  .          /* is
12618 45 78 61 63 74 20 3d 20 31 3b 20 2a 2f 0a 20 20  Exact = 1; */.  
12619 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1261a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 72 63 20  #endif.      rc 
1261b 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50  = allocateBtreeP
1261c 61 67 65 28 70 42 74 2c 20 26 70 4f 76 66 6c 2c  age(pBt, &pOvfl,
1261d 20 26 70 67 6e 6f 4f 76 66 6c 2c 20 70 67 6e 6f   &pgnoOvfl, pgno
1261e 4f 76 66 6c 2c 20 69 73 45 78 61 63 74 29 3b 0a  Ovfl, isExact);.
1261f 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
12620 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
12621 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 64       /* If the d
12622 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 73  atabase supports
12623 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 61 6e   auto-vacuum, an
12624 64 20 74 68 65 20 73 65 63 6f 6e 64 20 6f 72 20  d the second or 
12625 73 75 62 73 65 71 75 65 6e 74 0a 20 20 20 20 20  subsequent.     
12626 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   ** overflow pag
12627 65 20 69 73 20 62 65 69 6e 67 20 61 6c 6c 6f 63  e is being alloc
12628 61 74 65 64 2c 20 61 64 64 20 61 6e 20 65 6e 74  ated, add an ent
12629 72 79 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 65  ry to the pointe
1262a 72 2d 6d 61 70 0a 20 20 20 20 20 20 2a 2a 20 66  r-map.      ** f
1262b 6f 72 20 74 68 61 74 20 70 61 67 65 20 6e 6f 77  or that page now
1262c 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  . .      **.    
1262d 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20    ** If this is 
1262e 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 66 6c  the first overfl
1262f 6f 77 20 70 61 67 65 2c 20 74 68 65 6e 20 77 72  ow page, then wr
12630 69 74 65 20 61 20 70 61 72 74 69 61 6c 20 65 6e  ite a partial en
12631 74 72 79 20 0a 20 20 20 20 20 20 2a 2a 20 74 6f  try .      ** to
12632 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70   the pointer-map
12633 2e 20 49 66 20 77 65 20 77 72 69 74 65 20 6e 6f  . If we write no
12634 74 68 69 6e 67 20 74 6f 20 74 68 69 73 20 70 6f  thing to this po
12635 69 6e 74 65 72 2d 6d 61 70 20 73 6c 6f 74 2c 0a  inter-map slot,.
12636 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68        ** then th
12637 65 20 6f 70 74 69 6d 69 73 74 69 63 20 6f 76 65  e optimistic ove
12638 72 66 6c 6f 77 20 63 68 61 69 6e 20 70 72 6f 63  rflow chain proc
12639 65 73 73 69 6e 67 20 69 6e 20 63 6c 65 61 72 43  essing in clearC
1263a 65 6c 6c 28 29 0a 20 20 20 20 20 20 2a 2a 20 6d  ell().      ** m
1263b 61 79 20 6d 69 73 69 6e 74 65 72 70 72 65 74 20  ay misinterpret 
1263c 74 68 65 20 75 6e 69 6e 69 74 69 61 6c 69 73 65  the uninitialise
1263d 64 20 76 61 6c 75 65 73 20 61 6e 64 20 64 65 6c  d values and del
1263e 65 74 65 20 74 68 65 0a 20 20 20 20 20 20 2a 2a  ete the.      **
1263f 20 77 72 6f 6e 67 20 70 61 67 65 73 20 66 72 6f   wrong pages fro
12640 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a  m the database..
12641 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
12642 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  f( pBt->autoVacu
12643 75 6d 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45  um && rc==SQLITE
12644 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 75  _OK ){.        u
12645 38 20 65 54 79 70 65 20 3d 20 28 70 67 6e 6f 50  8 eType = (pgnoP
12646 74 72 6d 61 70 3f 50 54 52 4d 41 50 5f 4f 56 45  trmap?PTRMAP_OVE
12647 52 46 4c 4f 57 32 3a 50 54 52 4d 41 50 5f 4f 56  RFLOW2:PTRMAP_OV
12648 45 52 46 4c 4f 57 31 29 3b 0a 20 20 20 20 20 20  ERFLOW1);.      
12649 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74    rc = ptrmapPut
1264a 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20  (pBt, pgnoOvfl, 
1264b 65 54 79 70 65 2c 20 70 67 6e 6f 50 74 72 6d 61  eType, pgnoPtrma
1264c 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  p);.        if( 
1264d 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  rc ){.          
1264e 72 65 6c 65 61 73 65 50 61 67 65 28 70 4f 76 66  releasePage(pOvf
1264f 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  l);.        }.  
12650 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
12651 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
12652 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
12653 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20  (pToRelease);.  
12654 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
12655 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
12656 75 74 34 62 79 74 65 28 70 50 72 69 6f 72 2c 20  ut4byte(pPrior, 
12657 70 67 6e 6f 4f 76 66 6c 29 3b 0a 20 20 20 20 20  pgnoOvfl);.     
12658 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f   releasePage(pTo
12659 52 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 20 20  Release);.      
1265a 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 70 4f 76  pToRelease = pOv
1265b 66 6c 3b 0a 20 20 20 20 20 20 70 50 72 69 6f 72  fl;.      pPrior
1265c 20 3d 20 70 4f 76 66 6c 2d 3e 61 44 61 74 61 3b   = pOvfl->aData;
1265d 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28  .      put4byte(
1265e 70 50 72 69 6f 72 2c 20 30 29 3b 0a 20 20 20 20  pPrior, 0);.    
1265f 20 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70 4f    pPayload = &pO
12660 76 66 6c 2d 3e 61 44 61 74 61 5b 34 5d 3b 0a 20  vfl->aData[4];. 
12661 20 20 20 20 20 73 70 61 63 65 4c 65 66 74 20 3d       spaceLeft =
12662 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
12663 20 2d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20   - 4;.    }.    
12664 6e 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20  n = nPayload;.  
12665 20 20 69 66 28 20 6e 3e 73 70 61 63 65 4c 65 66    if( n>spaceLef
12666 74 20 29 20 6e 20 3d 20 73 70 61 63 65 4c 65 66  t ) n = spaceLef
12667 74 3b 0a 20 20 20 20 69 66 28 20 6e 53 72 63 3e  t;.    if( nSrc>
12668 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e  0 ){.      if( n
12669 3e 6e 53 72 63 20 29 20 6e 20 3d 20 6e 53 72 63  >nSrc ) n = nSrc
1266a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1266b 70 53 72 63 20 29 3b 0a 20 20 20 20 20 20 6d 65  pSrc );.      me
1266c 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 2c 20 70  mcpy(pPayload, p
1266d 53 72 63 2c 20 6e 29 3b 0a 20 20 20 20 7d 65 6c  Src, n);.    }el
1266e 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74  se{.      memset
1266f 28 70 50 61 79 6c 6f 61 64 2c 20 30 2c 20 6e 29  (pPayload, 0, n)
12670 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 50 61 79  ;.    }.    nPay
12671 6c 6f 61 64 20 2d 3d 20 6e 3b 0a 20 20 20 20 70  load -= n;.    p
12672 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 3b 0a 20 20  Payload += n;.  
12673 20 20 70 53 72 63 20 2b 3d 20 6e 3b 0a 20 20 20    pSrc += n;.   
12674 20 6e 53 72 63 20 2d 3d 20 6e 3b 0a 20 20 20 20   nSrc -= n;.    
12675 73 70 61 63 65 4c 65 66 74 20 2d 3d 20 6e 3b 0a  spaceLeft -= n;.
12676 20 20 20 20 69 66 28 20 6e 53 72 63 3d 3d 30 20      if( nSrc==0 
12677 29 7b 0a 20 20 20 20 20 20 6e 53 72 63 20 3d 20  ){.      nSrc = 
12678 6e 44 61 74 61 3b 0a 20 20 20 20 20 20 70 53 72  nData;.      pSr
12679 63 20 3d 20 70 44 61 74 61 3b 0a 20 20 20 20 7d  c = pData;.    }
1267a 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50 61  .  }.  releasePa
1267b 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a  ge(pToRelease);.
1267c 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1267d 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61  OK;.}../*.** Cha
1267e 6e 67 65 20 74 68 65 20 4d 65 6d 50 61 67 65 2e  nge the MemPage.
1267f 70 50 61 72 65 6e 74 20 70 6f 69 6e 74 65 72 20  pParent pointer 
12680 6f 6e 20 74 68 65 20 70 61 67 65 20 77 68 6f 73  on the page whos
12681 65 20 6e 75 6d 62 65 72 20 69 73 0a 2a 2a 20 67  e number is.** g
12682 69 76 65 6e 20 69 6e 20 74 68 65 20 73 65 63 6f  iven in the seco
12683 6e 64 20 61 72 67 75 6d 65 6e 74 20 73 6f 20 74  nd argument so t
12684 68 61 74 20 4d 65 6d 50 61 67 65 2e 70 50 61 72  hat MemPage.pPar
12685 65 6e 74 20 68 6f 6c 64 73 20 74 68 65 0a 2a 2a  ent holds the.**
12686 20 70 6f 69 6e 74 65 72 20 69 6e 20 74 68 65 20   pointer in the 
12687 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 2e 0a  third argument..
12688 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65  */.static int re
12689 70 61 72 65 6e 74 50 61 67 65 28 42 74 53 68 61  parentPage(BtSha
1268a 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70  red *pBt, Pgno p
1268b 67 6e 6f 2c 20 4d 65 6d 50 61 67 65 20 2a 70 4e  gno, MemPage *pN
1268c 65 77 50 61 72 65 6e 74 2c 20 69 6e 74 20 69 64  ewParent, int id
1268d 78 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  x){.  MemPage *p
1268e 54 68 69 73 3b 0a 20 20 44 62 50 61 67 65 20 2a  This;.  DbPage *
1268f 70 44 62 50 61 67 65 3b 0a 0a 20 20 61 73 73 65  pDbPage;..  asse
12690 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
12691 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  x_held(pBt->mute
12692 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
12693 70 4e 65 77 50 61 72 65 6e 74 21 3d 30 20 29 3b  pNewParent!=0 );
12694 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29  .  if( pgno==0 )
12695 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
12696 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  K;.  assert( pBt
12697 2d 3e 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20  ->pPager!=0 );. 
12698 20 70 44 62 50 61 67 65 20 3d 20 73 71 6c 69 74   pDbPage = sqlit
12699 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 70 42  e3PagerLookup(pB
1269a 74 2d 3e 70 50 61 67 65 72 2c 20 70 67 6e 6f 29  t->pPager, pgno)
1269b 3b 0a 20 20 69 66 28 20 70 44 62 50 61 67 65 20  ;.  if( pDbPage 
1269c 29 7b 0a 20 20 20 20 70 54 68 69 73 20 3d 20 28  ){.    pThis = (
1269d 4d 65 6d 50 61 67 65 20 2a 29 73 71 6c 69 74 65  MemPage *)sqlite
1269e 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70  3PagerGetExtra(p
1269f 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28  DbPage);.    if(
126a0 20 70 54 68 69 73 2d 3e 69 73 49 6e 69 74 20 29   pThis->isInit )
126a1 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
126a2 70 54 68 69 73 2d 3e 61 44 61 74 61 3d 3d 73 71  pThis->aData==sq
126a3 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74  lite3PagerGetDat
126a4 61 28 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  a(pDbPage) );.  
126a5 20 20 20 20 69 66 28 20 70 54 68 69 73 2d 3e 70      if( pThis->p
126a6 50 61 72 65 6e 74 21 3d 70 4e 65 77 50 61 72 65  Parent!=pNewPare
126a7 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  nt ){.        if
126a8 28 20 70 54 68 69 73 2d 3e 70 50 61 72 65 6e 74  ( pThis->pParent
126a9 20 29 20 73 71 6c 69 74 65 33 50 61 67 65 72 55   ) sqlite3PagerU
126aa 6e 72 65 66 28 70 54 68 69 73 2d 3e 70 50 61 72  nref(pThis->pPar
126ab 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  ent->pDbPage);. 
126ac 20 20 20 20 20 20 20 70 54 68 69 73 2d 3e 70 50         pThis->pP
126ad 61 72 65 6e 74 20 3d 20 70 4e 65 77 50 61 72 65  arent = pNewPare
126ae 6e 74 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  nt;.        sqli
126af 74 65 33 50 61 67 65 72 52 65 66 28 70 4e 65 77  te3PagerRef(pNew
126b0 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29  Parent->pDbPage)
126b1 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
126b2 70 54 68 69 73 2d 3e 69 64 78 50 61 72 65 6e 74  pThis->idxParent
126b3 20 3d 20 69 64 78 3b 0a 20 20 20 20 7d 0a 20 20   = idx;.    }.  
126b4 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
126b5 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20  ref(pDbPage);.  
126b6 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
126b7 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
126b8 4d 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74  M.  if( pBt->aut
126b9 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 72  oVacuum ){.    r
126ba 65 74 75 72 6e 20 70 74 72 6d 61 70 50 75 74 28  eturn ptrmapPut(
126bb 70 42 74 2c 20 70 67 6e 6f 2c 20 50 54 52 4d 41  pBt, pgno, PTRMA
126bc 50 5f 42 54 52 45 45 2c 20 70 4e 65 77 50 61 72  P_BTREE, pNewPar
126bd 65 6e 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a  ent->pgno);.  }.
126be 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
126bf 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a  SQLITE_OK;.}....
126c0 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
126c1 20 70 50 61 72 65 6e 74 20 70 6f 69 6e 74 65 72   pParent pointer
126c2 20 6f 66 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e   of all children
126c3 20 6f 66 20 70 50 61 67 65 20 74 6f 20 70 6f 69   of pPage to poi
126c4 6e 74 20 62 61 63 6b 0a 2a 2a 20 74 6f 20 70 50  nt back.** to pP
126c5 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74  age..**.** In ot
126c6 68 65 72 20 77 6f 72 64 73 2c 20 66 6f 72 20 65  her words, for e
126c7 76 65 72 79 20 63 68 69 6c 64 20 6f 66 20 70 50  very child of pP
126c8 61 67 65 2c 20 69 6e 76 6f 6b 65 20 72 65 70 61  age, invoke repa
126c9 72 65 6e 74 50 61 67 65 28 29 0a 2a 2a 20 74 6f  rentPage().** to
126ca 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   make sure that 
126cb 65 61 63 68 20 63 68 69 6c 64 20 6b 6e 6f 77 73  each child knows
126cc 20 74 68 61 74 20 70 50 61 67 65 20 69 73 20 69   that pPage is i
126cd 74 73 20 70 61 72 65 6e 74 2e 0a 2a 2a 0a 2a 2a  ts parent..**.**
126ce 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65   This routine ge
126cf 74 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20  ts called after 
126d0 79 6f 75 20 6d 65 6d 63 70 79 28 29 20 6f 6e 65  you memcpy() one
126d1 20 70 61 67 65 20 69 6e 74 6f 0a 2a 2a 20 61 6e   page into.** an
126d2 6f 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  other..*/.static
126d3 20 69 6e 74 20 72 65 70 61 72 65 6e 74 43 68 69   int reparentChi
126d4 6c 64 50 61 67 65 73 28 4d 65 6d 50 61 67 65 20  ldPages(MemPage 
126d5 2a 70 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 69  *pPage){.  int i
126d6 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
126d7 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a  t = pPage->pBt;.
126d8 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
126d9 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28  E_OK;..  assert(
126da 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
126db 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
126dc 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
126dd 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 20 72 65  pPage->leaf ) re
126de 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
126df 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50  .  for(i=0; i<pP
126e0 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29  age->nCell; i++)
126e1 7b 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20  {.    u8 *pCell 
126e2 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65  = findCell(pPage
126e3 2c 20 69 29 3b 0a 20 20 20 20 72 63 20 3d 20 72  , i);.    rc = r
126e4 65 70 61 72 65 6e 74 50 61 67 65 28 70 42 74 2c  eparentPage(pBt,
126e5 20 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29   get4byte(pCell)
126e6 2c 20 70 50 61 67 65 2c 20 69 29 3b 0a 20 20 20  , pPage, i);.   
126e7 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
126e8 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
126e9 20 20 7d 0a 20 20 72 63 20 3d 20 72 65 70 61 72    }.  rc = repar
126ea 65 6e 74 50 61 67 65 28 70 42 74 2c 20 67 65 74  entPage(pBt, get
126eb 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44  4byte(&pPage->aD
126ec 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66  ata[pPage->hdrOf
126ed 66 73 65 74 2b 38 5d 29 2c 20 0a 20 20 20 20 20  fset+8]), .     
126ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
126ef 50 61 67 65 2c 20 69 29 3b 0a 20 20 70 50 61 67  Page, i);.  pPag
126f0 65 2d 3e 69 64 78 53 68 69 66 74 20 3d 20 30 3b  e->idxShift = 0;
126f1 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
126f2 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68  ./*.** Remove th
126f3 65 20 69 2d 74 68 20 63 65 6c 6c 20 66 72 6f 6d  e i-th cell from
126f4 20 70 50 61 67 65 2e 20 20 54 68 69 73 20 72 6f   pPage.  This ro
126f5 75 74 69 6e 65 20 65 66 66 65 63 74 73 20 70 50  utine effects pP
126f6 61 67 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 54 68 65  age only..** The
126f7 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 73   cell content is
126f8 20 6e 6f 74 20 66 72 65 65 64 20 6f 72 20 64 65   not freed or de
126f9 61 6c 6c 6f 63 61 74 65 64 2e 20 20 49 74 20 69  allocated.  It i
126fa 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a 2a  s assumed that.*
126fb 2a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65  * the cell conte
126fc 6e 74 20 68 61 73 20 62 65 65 6e 20 63 6f 70 69  nt has been copi
126fd 65 64 20 73 6f 6d 65 70 6c 61 63 65 20 65 6c 73  ed someplace els
126fe 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  e.  This routine
126ff 20 6a 75 73 74 0a 2a 2a 20 72 65 6d 6f 76 65 73   just.** removes
12700 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 74   the reference t
12701 6f 20 74 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20  o the cell from 
12702 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 22 73 7a  pPage..**.** "sz
12703 22 20 6d 75 73 74 20 62 65 20 74 68 65 20 6e 75  " must be the nu
12704 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
12705 20 74 68 65 20 63 65 6c 6c 2e 0a 2a 2f 0a 73 74   the cell..*/.st
12706 61 74 69 63 20 76 6f 69 64 20 64 72 6f 70 43 65  atic void dropCe
12707 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  ll(MemPage *pPag
12708 65 2c 20 69 6e 74 20 69 64 78 2c 20 69 6e 74 20  e, int idx, int 
12709 73 7a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20  sz){.  int i;   
1270a 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
1270b 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  ounter */.  int 
1270c 70 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  pc;         /* O
1270d 66 66 73 65 74 20 74 6f 20 63 65 6c 6c 20 63 6f  ffset to cell co
1270e 6e 74 65 6e 74 20 6f 66 20 63 65 6c 6c 20 62 65  ntent of cell be
1270f 69 6e 67 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20  ing deleted */. 
12710 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20 20   u8 *data;      
12711 20 2f 2a 20 70 50 61 67 65 2d 3e 61 44 61 74 61   /* pPage->aData
12712 20 2a 2f 0a 20 20 75 38 20 2a 70 74 72 3b 20 20   */.  u8 *ptr;  
12713 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f        /* Used to
12714 20 6d 6f 76 65 20 62 79 74 65 73 20 61 72 6f 75   move bytes arou
12715 6e 64 20 77 69 74 68 69 6e 20 64 61 74 61 5b 5d  nd within data[]
12716 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69   */..  assert( i
12717 64 78 3e 3d 30 20 26 26 20 69 64 78 3c 70 50 61  dx>=0 && idx<pPa
12718 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 61  ge->nCell );.  a
12719 73 73 65 72 74 28 20 73 7a 3d 3d 63 65 6c 6c 53  ssert( sz==cellS
1271a 69 7a 65 28 70 50 61 67 65 2c 20 69 64 78 29 20  ize(pPage, idx) 
1271b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
1271c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65  ite3PagerIswrite
1271d 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50  able(pPage->pDbP
1271e 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74  age) );.  assert
1271f 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
12720 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d  held(pPage->pBt-
12721 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 64 61 74  >mutex) );.  dat
12722 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61  a = pPage->aData
12723 3b 0a 20 20 70 74 72 20 3d 20 26 64 61 74 61 5b  ;.  ptr = &data[
12724 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65  pPage->cellOffse
12725 74 20 2b 20 32 2a 69 64 78 5d 3b 0a 20 20 70 63  t + 2*idx];.  pc
12726 20 3d 20 67 65 74 32 62 79 74 65 28 70 74 72 29   = get2byte(ptr)
12727 3b 0a 20 20 61 73 73 65 72 74 28 20 70 63 3e 31  ;.  assert( pc>1
12728 30 20 26 26 20 70 63 2b 73 7a 3c 3d 70 50 61 67  0 && pc+sz<=pPag
12729 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  e->pBt->usableSi
1272a 7a 65 20 29 3b 0a 20 20 66 72 65 65 53 70 61 63  ze );.  freeSpac
1272b 65 28 70 50 61 67 65 2c 20 70 63 2c 20 73 7a 29  e(pPage, pc, sz)
1272c 3b 0a 20 20 66 6f 72 28 69 3d 69 64 78 2b 31 3b  ;.  for(i=idx+1;
1272d 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b   i<pPage->nCell;
1272e 20 69 2b 2b 2c 20 70 74 72 2b 3d 32 29 7b 0a 20   i++, ptr+=2){. 
1272f 20 20 20 70 74 72 5b 30 5d 20 3d 20 70 74 72 5b     ptr[0] = ptr[
12730 32 5d 3b 0a 20 20 20 20 70 74 72 5b 31 5d 20 3d  2];.    ptr[1] =
12731 20 70 74 72 5b 33 5d 3b 0a 20 20 7d 0a 20 20 70   ptr[3];.  }.  p
12732 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 2d 3b 0a 20  Page->nCell--;. 
12733 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b   put2byte(&data[
12734 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
12735 2b 33 5d 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c  +3], pPage->nCel
12736 6c 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72  l);.  pPage->nFr
12737 65 65 20 2b 3d 20 32 3b 0a 20 20 70 50 61 67 65  ee += 2;.  pPage
12738 2d 3e 69 64 78 53 68 69 66 74 20 3d 20 31 3b 0a  ->idxShift = 1;.
12739 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20  }../*.** Insert 
1273a 61 20 6e 65 77 20 63 65 6c 6c 20 6f 6e 20 70 50  a new cell on pP
1273b 61 67 65 20 61 74 20 63 65 6c 6c 20 69 6e 64 65  age at cell inde
1273c 78 20 22 69 22 2e 20 20 70 43 65 6c 6c 20 70 6f  x "i".  pCell po
1273d 69 6e 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63  ints to the.** c
1273e 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 63 65  ontent of the ce
1273f 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ll..**.** If the
12740 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 77 69   cell content wi
12741 6c 6c 20 66 69 74 20 6f 6e 20 74 68 65 20 70 61  ll fit on the pa
12742 67 65 2c 20 74 68 65 6e 20 70 75 74 20 69 74 20  ge, then put it 
12743 74 68 65 72 65 2e 20 20 49 66 20 69 74 0a 2a 2a  there.  If it.**
12744 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 2c 20 74   will not fit, t
12745 68 65 6e 20 6d 61 6b 65 20 61 20 63 6f 70 79 20  hen make a copy 
12746 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  of the cell cont
12747 65 6e 74 20 69 6e 74 6f 20 70 54 65 6d 70 20 69  ent into pTemp i
12748 66 0a 2a 2a 20 70 54 65 6d 70 20 69 73 20 6e 6f  f.** pTemp is no
12749 74 20 6e 75 6c 6c 2e 20 20 52 65 67 61 72 64 6c  t null.  Regardl
1274a 65 73 73 20 6f 66 20 70 54 65 6d 70 2c 20 61 6c  ess of pTemp, al
1274b 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 65 6e 74  locate a new ent
1274c 72 79 0a 2a 2a 20 69 6e 20 70 50 61 67 65 2d 3e  ry.** in pPage->
1274d 61 4f 76 66 6c 5b 5d 20 61 6e 64 20 6d 61 6b 65  aOvfl[] and make
1274e 20 69 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   it point to the
1274f 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 28 65   cell content (e
12750 69 74 68 65 72 0a 2a 2a 20 69 6e 20 70 54 65 6d  ither.** in pTem
12751 70 20 6f 72 20 74 68 65 20 6f 72 69 67 69 6e 61  p or the origina
12752 6c 20 70 43 65 6c 6c 29 20 61 6e 64 20 61 6c 73  l pCell) and als
12753 6f 20 72 65 63 6f 72 64 20 69 74 73 20 69 6e 64  o record its ind
12754 65 78 2e 20 0a 2a 2a 20 41 6c 6c 6f 63 61 74 69  ex. .** Allocati
12755 6e 67 20 61 20 6e 65 77 20 65 6e 74 72 79 20 69  ng a new entry i
12756 6e 20 70 50 61 67 65 2d 3e 61 43 65 6c 6c 5b 5d  n pPage->aCell[]
12757 20 69 6d 70 6c 69 65 73 20 74 68 61 74 20 0a 2a   implies that .*
12758 2a 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c  * pPage->nOverfl
12759 6f 77 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65  ow is incremente
1275a 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 53 6b 69  d..**.** If nSki
1275b 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  p is non-zero, t
1275c 68 65 6e 20 64 6f 20 6e 6f 74 20 63 6f 70 79 20  hen do not copy 
1275d 74 68 65 20 66 69 72 73 74 20 6e 53 6b 69 70 20  the first nSkip 
1275e 62 79 74 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20  bytes of the.** 
1275f 63 65 6c 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72  cell. The caller
12760 20 77 69 6c 6c 20 6f 76 65 72 77 72 69 74 65 20   will overwrite 
12761 74 68 65 6d 20 61 66 74 65 72 20 74 68 69 73 20  them after this 
12762 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
12763 2e 20 49 66 0a 2a 2a 20 6e 53 6b 69 70 20 69 73  . If.** nSkip is
12764 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20   non-zero, then 
12765 70 43 65 6c 6c 20 6d 61 79 20 6e 6f 74 20 70 6f  pCell may not po
12766 69 6e 74 20 74 6f 20 61 6e 20 69 6e 76 61 6c 69  int to an invali
12767 64 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  d memory locatio
12768 6e 20 0a 2a 2a 20 28 62 75 74 20 70 43 65 6c 6c  n .** (but pCell
12769 2b 6e 53 6b 69 70 20 69 73 20 61 6c 77 61 79 73  +nSkip is always
1276a 20 76 61 6c 69 64 29 2e 0a 2a 2f 0a 73 74 61 74   valid)..*/.stat
1276b 69 63 20 69 6e 74 20 69 6e 73 65 72 74 43 65 6c  ic int insertCel
1276c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  l(.  MemPage *pP
1276d 61 67 65 2c 20 20 20 2f 2a 20 50 61 67 65 20 69  age,   /* Page i
1276e 6e 74 6f 20 77 68 69 63 68 20 77 65 20 61 72 65  nto which we are
1276f 20 63 6f 70 79 69 6e 67 20 2a 2f 0a 20 20 69 6e   copying */.  in
12770 74 20 69 2c 20 20 20 20 20 20 20 20 20 20 20 20  t i,            
12771 2f 2a 20 4e 65 77 20 63 65 6c 6c 20 62 65 63 6f  /* New cell beco
12772 6d 65 73 20 74 68 65 20 69 2d 74 68 20 63 65 6c  mes the i-th cel
12773 6c 20 6f 66 20 74 68 65 20 70 61 67 65 20 2a 2f  l of the page */
12774 0a 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20  .  u8 *pCell,   
12775 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20       /* Content 
12776 6f 66 20 74 68 65 20 6e 65 77 20 63 65 6c 6c 20  of the new cell 
12777 2a 2f 0a 20 20 69 6e 74 20 73 7a 2c 20 20 20 20  */.  int sz,    
12778 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20         /* Bytes 
12779 6f 66 20 63 6f 6e 74 65 6e 74 20 69 6e 20 70 43  of content in pC
1277a 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 54 65  ell */.  u8 *pTe
1277b 6d 70 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 65  mp,        /* Te
1277c 6d 70 20 73 74 6f 72 61 67 65 20 73 70 61 63 65  mp storage space
1277d 20 66 6f 72 20 70 43 65 6c 6c 2c 20 69 66 20 6e   for pCell, if n
1277e 65 65 64 65 64 20 2a 2f 0a 20 20 75 38 20 6e 53  eeded */.  u8 nS
1277f 6b 69 70 20 20 20 20 20 20 20 20 20 20 2f 2a 20  kip          /* 
12780 44 6f 20 6e 6f 74 20 77 72 69 74 65 20 74 68 65  Do not write the
12781 20 66 69 72 73 74 20 6e 53 6b 69 70 20 62 79 74   first nSkip byt
12782 65 73 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 2a  es of the cell *
12783 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 64 78 3b 20  /.){.  int idx; 
12784 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72           /* Wher
12785 65 20 74 6f 20 77 72 69 74 65 20 6e 65 77 20 63  e to write new c
12786 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 64  ell content in d
12787 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6a  ata[] */.  int j
12788 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
12789 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
1278a 20 20 69 6e 74 20 74 6f 70 3b 20 20 20 20 20 20    int top;      
1278b 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74      /* First byt
1278c 65 20 6f 66 20 63 6f 6e 74 65 6e 74 20 66 6f 72  e of content for
1278d 20 61 6e 79 20 63 65 6c 6c 20 69 6e 20 64 61 74   any cell in dat
1278e 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64  a[] */.  int end
1278f 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69  ;          /* Fi
12790 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74 68  rst byte past th
12791 65 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e  e last cell poin
12792 74 65 72 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f  ter in data[] */
12793 0a 20 20 69 6e 74 20 69 6e 73 3b 20 20 20 20 20  .  int ins;     
12794 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e       /* Index in
12795 20 64 61 74 61 5b 5d 20 77 68 65 72 65 20 6e 65   data[] where ne
12796 77 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 69  w cell pointer i
12797 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20  s inserted */.  
12798 69 6e 74 20 68 64 72 3b 20 20 20 20 20 20 20 20  int hdr;        
12799 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f    /* Offset into
1279a 20 64 61 74 61 5b 5d 20 6f 66 20 74 68 65 20 70   data[] of the p
1279b 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20  age header */.  
1279c 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20  int cellOffset; 
1279d 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
1279e 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74  first cell point
1279f 65 72 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a  er in data[] */.
127a0 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20    u8 *data;     
127a1 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 74 65      /* The conte
127a2 6e 74 20 6f 66 20 74 68 65 20 77 68 6f 6c 65 20  nt of the whole 
127a3 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 74  page */.  u8 *pt
127a4 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55  r;          /* U
127a5 73 65 64 20 66 6f 72 20 6d 6f 76 69 6e 67 20 69  sed for moving i
127a6 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 72 6f 75 6e  nformation aroun
127a7 64 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 0a  d in data[] */..
127a8 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
127a9 26 20 69 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c  & i<=pPage->nCel
127aa 6c 2b 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c  l+pPage->nOverfl
127ab 6f 77 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ow );.  assert( 
127ac 73 7a 3d 3d 63 65 6c 6c 53 69 7a 65 50 74 72 28  sz==cellSizePtr(
127ad 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 20 29 3b  pPage, pCell) );
127ae 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
127af 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50  e3_mutex_held(pP
127b0 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  age->pBt->mutex)
127b1 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d   );.  if( pPage-
127b2 3e 6e 4f 76 65 72 66 6c 6f 77 20 7c 7c 20 73 7a  >nOverflow || sz
127b3 2b 32 3e 70 50 61 67 65 2d 3e 6e 46 72 65 65 20  +2>pPage->nFree 
127b4 29 7b 0a 20 20 20 20 69 66 28 20 70 54 65 6d 70  ){.    if( pTemp
127b5 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   ){.      memcpy
127b6 28 70 54 65 6d 70 2b 6e 53 6b 69 70 2c 20 70 43  (pTemp+nSkip, pC
127b7 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53  ell+nSkip, sz-nS
127b8 6b 69 70 29 3b 0a 20 20 20 20 20 20 70 43 65 6c  kip);.      pCel
127b9 6c 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20 20 7d  l = pTemp;.    }
127ba 0a 20 20 20 20 6a 20 3d 20 70 50 61 67 65 2d 3e  .    j = pPage->
127bb 6e 4f 76 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20 20  nOverflow++;.   
127bc 20 61 73 73 65 72 74 28 20 6a 3c 73 69 7a 65 6f   assert( j<sizeo
127bd 66 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 29 2f  f(pPage->aOvfl)/
127be 73 69 7a 65 6f 66 28 70 50 61 67 65 2d 3e 61 4f  sizeof(pPage->aO
127bf 76 66 6c 5b 30 5d 29 20 29 3b 0a 20 20 20 20 70  vfl[0]) );.    p
127c0 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 70  Page->aOvfl[j].p
127c1 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 3b 0a 20 20  Cell = pCell;.  
127c2 20 20 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a    pPage->aOvfl[j
127c3 5d 2e 69 64 78 20 3d 20 69 3b 0a 20 20 20 20 70  ].idx = i;.    p
127c4 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 30 3b  Page->nFree = 0;
127c5 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
127c6 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61  t rc = sqlite3Pa
127c7 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e  gerWrite(pPage->
127c8 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66  pDbPage);.    if
127c9 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
127ca 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
127cb 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  rc;.    }.    as
127cc 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
127cd 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50  erIswriteable(pP
127ce 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  age->pDbPage) );
127cf 0a 20 20 20 20 64 61 74 61 20 3d 20 70 50 61 67  .    data = pPag
127d0 65 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 68 64  e->aData;.    hd
127d1 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66  r = pPage->hdrOf
127d2 66 73 65 74 3b 0a 20 20 20 20 74 6f 70 20 3d 20  fset;.    top = 
127d3 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
127d4 64 72 2b 35 5d 29 3b 0a 20 20 20 20 63 65 6c 6c  dr+5]);.    cell
127d5 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e  Offset = pPage->
127d6 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 20 20  cellOffset;.    
127d7 65 6e 64 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74  end = cellOffset
127d8 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c   + 2*pPage->nCel
127d9 6c 20 2b 20 32 3b 0a 20 20 20 20 69 6e 73 20 3d  l + 2;.    ins =
127da 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a   cellOffset + 2*
127db 69 3b 0a 20 20 20 20 69 66 28 20 65 6e 64 20 3e  i;.    if( end >
127dc 20 74 6f 70 20 2d 20 73 7a 20 29 7b 0a 20 20 20   top - sz ){.   
127dd 20 20 20 72 63 20 3d 20 64 65 66 72 61 67 6d 65     rc = defragme
127de 6e 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  ntPage(pPage);. 
127df 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
127e0 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
127e1 72 63 3b 0a 20 20 20 20 20 20 74 6f 70 20 3d 20  rc;.      top = 
127e2 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
127e3 64 72 2b 35 5d 29 3b 0a 20 20 20 20 20 20 61 73  dr+5]);.      as
127e4 73 65 72 74 28 20 65 6e 64 20 2b 20 73 7a 20 3c  sert( end + sz <
127e5 3d 20 74 6f 70 20 29 3b 0a 20 20 20 20 7d 0a 20  = top );.    }. 
127e6 20 20 20 69 64 78 20 3d 20 61 6c 6c 6f 63 61 74     idx = allocat
127e7 65 53 70 61 63 65 28 70 50 61 67 65 2c 20 73 7a  eSpace(pPage, sz
127e8 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  );.    assert( i
127e9 64 78 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65  dx>0 );.    asse
127ea 72 74 28 20 65 6e 64 20 3c 3d 20 67 65 74 32 62  rt( end <= get2b
127eb 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d  yte(&data[hdr+5]
127ec 29 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  ) );.    pPage->
127ed 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 70 50 61  nCell++;.    pPa
127ee 67 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 32 3b 0a  ge->nFree -= 2;.
127ef 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61      memcpy(&data
127f0 5b 69 64 78 2b 6e 53 6b 69 70 5d 2c 20 70 43 65  [idx+nSkip], pCe
127f1 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b  ll+nSkip, sz-nSk
127f2 69 70 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 65  ip);.    for(j=e
127f3 6e 64 2d 32 2c 20 70 74 72 3d 26 64 61 74 61 5b  nd-2, ptr=&data[
127f4 6a 5d 3b 20 6a 3e 69 6e 73 3b 20 6a 2d 3d 32 2c  j]; j>ins; j-=2,
127f5 20 70 74 72 2d 3d 32 29 7b 0a 20 20 20 20 20 20   ptr-=2){.      
127f6 70 74 72 5b 30 5d 20 3d 20 70 74 72 5b 2d 32 5d  ptr[0] = ptr[-2]
127f7 3b 0a 20 20 20 20 20 20 70 74 72 5b 31 5d 20 3d  ;.      ptr[1] =
127f8 20 70 74 72 5b 2d 31 5d 3b 0a 20 20 20 20 7d 0a   ptr[-1];.    }.
127f9 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61      put2byte(&da
127fa 74 61 5b 69 6e 73 5d 2c 20 69 64 78 29 3b 0a 20  ta[ins], idx);. 
127fb 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74     put2byte(&dat
127fc 61 5b 68 64 72 2b 33 5d 2c 20 70 50 61 67 65 2d  a[hdr+3], pPage-
127fd 3e 6e 43 65 6c 6c 29 3b 0a 20 20 20 20 70 50 61  >nCell);.    pPa
127fe 67 65 2d 3e 69 64 78 53 68 69 66 74 20 3d 20 31  ge->idxShift = 1
127ff 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
12800 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
12801 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e  .    if( pPage->
12802 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
12803 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ){.      /* The 
12804 63 65 6c 6c 20 6d 61 79 20 63 6f 6e 74 61 69 6e  cell may contain
12805 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
12806 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20   overflow page. 
12807 49 66 20 73 6f 2c 20 77 72 69 74 65 0a 20 20 20  If so, write.   
12808 20 20 20 2a 2a 20 74 68 65 20 65 6e 74 72 79 20     ** the entry 
12809 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77  for the overflow
1280a 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 70   page into the p
1280b 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 20 20 20 20  ointer map..    
1280c 20 20 2a 2f 0a 20 20 20 20 20 20 43 65 6c 6c 49    */.      CellI
1280d 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20  nfo info;.      
1280e 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73  sqlite3BtreePars
1280f 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20  eCellPtr(pPage, 
12810 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20  pCell, &info);. 
12811 20 20 20 20 20 61 73 73 65 72 74 28 20 28 69 6e       assert( (in
12812 66 6f 2e 6e 44 61 74 61 2b 28 70 50 61 67 65 2d  fo.nData+(pPage-
12813 3e 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e  >intKey?0:info.n
12814 4b 65 79 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 79  Key))==info.nPay
12815 6c 6f 61 64 20 29 3b 0a 20 20 20 20 20 20 69 66  load );.      if
12816 28 20 28 69 6e 66 6f 2e 6e 44 61 74 61 2b 28 70  ( (info.nData+(p
12817 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3f 30 3a 69  Page->intKey?0:i
12818 6e 66 6f 2e 6e 4b 65 79 29 29 3e 69 6e 66 6f 2e  nfo.nKey))>info.
12819 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20  nLocal ){.      
1281a 20 20 50 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20    Pgno pgnoOvfl 
1281b 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 65 6c  = get4byte(&pCel
1281c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77  l[info.iOverflow
1281d 5d 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  ]);.        rc =
1281e 20 70 74 72 6d 61 70 50 75 74 28 70 50 61 67 65   ptrmapPut(pPage
1281f 2d 3e 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 2c  ->pBt, pgnoOvfl,
12820 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
12821 31 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b  1, pPage->pgno);
12822 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
12823 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
12824 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
12825 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
12826 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
12827 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  E_OK;.}../*.** A
12828 64 64 20 61 20 6c 69 73 74 20 6f 66 20 63 65 6c  dd a list of cel
12829 6c 73 20 74 6f 20 61 20 70 61 67 65 2e 20 20 54  ls to a page.  T
1282a 68 65 20 70 61 67 65 20 73 68 6f 75 6c 64 20 62  he page should b
1282b 65 20 69 6e 69 74 69 61 6c 6c 79 20 65 6d 70 74  e initially empt
1282c 79 2e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 73 20  y..** The cells 
1282d 61 72 65 20 67 75 61 72 61 6e 74 65 65 64 20 74  are guaranteed t
1282e 6f 20 66 69 74 20 6f 6e 20 74 68 65 20 70 61 67  o fit on the pag
1282f 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
12830 64 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 0a  d assemblePage(.
12831 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
12832 2c 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20  ,   /* The page 
12833 74 6f 20 62 65 20 61 73 73 65 6d 62 6c 69 65 64  to be assemblied
12834 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c   */.  int nCell,
12835 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e          /* The n
12836 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 74  umber of cells t
12837 6f 20 61 64 64 20 74 6f 20 74 68 69 73 20 70 61  o add to this pa
12838 67 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 43  ge */.  u8 **apC
12839 65 6c 6c 2c 20 20 20 20 20 20 2f 2a 20 50 6f 69  ell,      /* Poi
1283a 6e 74 65 72 73 20 74 6f 20 63 65 6c 6c 20 62 6f  nters to cell bo
1283b 64 69 65 73 20 2a 2f 0a 20 20 75 31 36 20 2a 61  dies */.  u16 *a
1283c 53 69 7a 65 20 20 20 20 20 20 20 20 2f 2a 20 53  Size        /* S
1283d 69 7a 65 73 20 6f 66 20 74 68 65 20 63 65 6c 6c  izes of the cell
1283e 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  s */.){.  int i;
1283f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
12840 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
12841 20 69 6e 74 20 74 6f 74 61 6c 53 69 7a 65 3b 20   int totalSize; 
12842 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a 65     /* Total size
12843 20 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20 2a 2f   of all cells */
12844 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20  .  int hdr;     
12845 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
12846 20 70 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a   page header */.
12847 20 20 69 6e 74 20 63 65 6c 6c 70 74 72 3b 20 20    int cellptr;  
12848 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
12849 66 20 6e 65 78 74 20 63 65 6c 6c 20 70 6f 69 6e  f next cell poin
1284a 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c  ter */.  int cel
1284b 6c 62 6f 64 79 3b 20 20 20 20 20 2f 2a 20 41 64  lbody;     /* Ad
1284c 64 72 65 73 73 20 6f 66 20 6e 65 78 74 20 63 65  dress of next ce
1284d 6c 6c 20 62 6f 64 79 20 2a 2f 0a 20 20 75 38 20  ll body */.  u8 
1284e 2a 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 2f  *data;         /
1284f 2a 20 44 61 74 61 20 66 6f 72 20 74 68 65 20 70  * Data for the p
12850 61 67 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  age */..  assert
12851 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c  ( pPage->nOverfl
12852 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ow==0 );.  asser
12853 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
12854 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
12855 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 74 6f  ->mutex) );.  to
12856 74 61 6c 53 69 7a 65 20 3d 20 30 3b 0a 20 20 66  talSize = 0;.  f
12857 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b  or(i=0; i<nCell;
12858 20 69 2b 2b 29 7b 0a 20 20 20 20 74 6f 74 61 6c   i++){.    total
12859 53 69 7a 65 20 2b 3d 20 61 53 69 7a 65 5b 69 5d  Size += aSize[i]
1285a 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
1285b 74 6f 74 61 6c 53 69 7a 65 2b 32 2a 6e 43 65 6c  totalSize+2*nCel
1285c 6c 3c 3d 70 50 61 67 65 2d 3e 6e 46 72 65 65 20  l<=pPage->nFree 
1285d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
1285e 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a  ge->nCell==0 );.
1285f 20 20 63 65 6c 6c 70 74 72 20 3d 20 70 50 61 67    cellptr = pPag
12860 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20  e->cellOffset;. 
12861 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61   data = pPage->a
12862 44 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50  Data;.  hdr = pP
12863 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a  age->hdrOffset;.
12864 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61    put2byte(&data
12865 5b 68 64 72 2b 33 5d 2c 20 6e 43 65 6c 6c 29 3b  [hdr+3], nCell);
12866 0a 20 20 69 66 28 20 6e 43 65 6c 6c 20 29 7b 0a  .  if( nCell ){.
12867 20 20 20 20 63 65 6c 6c 62 6f 64 79 20 3d 20 61      cellbody = a
12868 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 70 50 61  llocateSpace(pPa
12869 67 65 2c 20 74 6f 74 61 6c 53 69 7a 65 29 3b 0a  ge, totalSize);.
1286a 20 20 20 20 61 73 73 65 72 74 28 20 63 65 6c 6c      assert( cell
1286b 62 6f 64 79 3e 30 20 29 3b 0a 20 20 20 20 61 73  body>0 );.    as
1286c 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 46 72  sert( pPage->nFr
1286d 65 65 20 3e 3d 20 32 2a 6e 43 65 6c 6c 20 29 3b  ee >= 2*nCell );
1286e 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65  .    pPage->nFre
1286f 65 20 2d 3d 20 32 2a 6e 43 65 6c 6c 3b 0a 20 20  e -= 2*nCell;.  
12870 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65    for(i=0; i<nCe
12871 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ll; i++){.      
12872 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 63  put2byte(&data[c
12873 65 6c 6c 70 74 72 5d 2c 20 63 65 6c 6c 62 6f 64  ellptr], cellbod
12874 79 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79  y);.      memcpy
12875 28 26 64 61 74 61 5b 63 65 6c 6c 62 6f 64 79 5d  (&data[cellbody]
12876 2c 20 61 70 43 65 6c 6c 5b 69 5d 2c 20 61 53 69  , apCell[i], aSi
12877 7a 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 63 65  ze[i]);.      ce
12878 6c 6c 70 74 72 20 2b 3d 20 32 3b 0a 20 20 20 20  llptr += 2;.    
12879 20 20 63 65 6c 6c 62 6f 64 79 20 2b 3d 20 61 53    cellbody += aS
1287a 69 7a 65 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20  ize[i];.    }.  
1287b 20 20 61 73 73 65 72 74 28 20 63 65 6c 6c 62 6f    assert( cellbo
1287c 64 79 3d 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e  dy==pPage->pBt->
1287d 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20  usableSize );.  
1287e 7d 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  }.  pPage->nCell
1287f 20 3d 20 6e 43 65 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a   = nCell;.}../*.
12880 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
12881 20 70 61 72 61 6d 65 74 65 72 73 20 64 65 74 65   parameters dete
12882 72 6d 69 6e 65 20 68 6f 77 20 6d 61 6e 79 20 61  rmine how many a
12883 64 6a 61 63 65 6e 74 20 70 61 67 65 73 20 67 65  djacent pages ge
12884 74 20 69 6e 76 6f 6c 76 65 64 0a 2a 2a 20 69 6e  t involved.** in
12885 20 61 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65   a balancing ope
12886 72 61 74 69 6f 6e 2e 20 20 4e 4e 20 69 73 20 74  ration.  NN is t
12887 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65 69  he number of nei
12888 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72  ghbors on either
12889 20 73 69 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20   side.** of the 
1288a 70 61 67 65 20 74 68 61 74 20 70 61 72 74 69 63  page that partic
1288b 69 70 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c  ipate in the bal
1288c 61 6e 63 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e  ancing operation
1288d 2e 20 20 4e 42 20 69 73 20 74 68 65 0a 2a 2a 20  .  NB is the.** 
1288e 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
1288f 70 61 67 65 73 20 74 68 61 74 20 70 61 72 74 69  pages that parti
12890 63 69 70 61 74 65 2c 20 69 6e 63 6c 75 64 69 6e  cipate, includin
12891 67 20 74 68 65 20 74 61 72 67 65 74 20 70 61 67  g the target pag
12892 65 20 61 6e 64 0a 2a 2a 20 4e 4e 20 6e 65 69 67  e and.** NN neig
12893 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20  hbors on either 
12894 73 69 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  side..**.** The 
12895 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66  minimum value of
12896 20 4e 4e 20 69 73 20 31 20 28 6f 66 20 63 6f 75   NN is 1 (of cou
12897 72 73 65 29 2e 20 20 49 6e 63 72 65 61 73 69 6e  rse).  Increasin
12898 67 20 4e 4e 20 61 62 6f 76 65 20 31 0a 2a 2a 20  g NN above 1.** 
12899 28 74 6f 20 32 20 6f 72 20 33 29 20 67 69 76 65  (to 2 or 3) give
1289a 73 20 61 20 6d 6f 64 65 73 74 20 69 6d 70 72 6f  s a modest impro
1289b 76 65 6d 65 6e 74 20 69 6e 20 53 45 4c 45 43 54  vement in SELECT
1289c 20 61 6e 64 20 44 45 4c 45 54 45 20 70 65 72 66   and DELETE perf
1289d 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69 6e 20 65 78  ormance.** in ex
1289e 63 68 61 6e 67 65 20 66 6f 72 20 61 20 6c 61 72  change for a lar
1289f 67 65 72 20 64 65 67 72 61 64 61 74 69 6f 6e 20  ger degradation 
128a0 69 6e 20 49 4e 53 45 52 54 20 61 6e 64 20 55 50  in INSERT and UP
128a1 44 41 54 45 20 70 65 72 66 6f 72 6d 61 6e 63 65  DATE performance
128a2 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f  ..** The value o
128a3 66 20 4e 4e 20 61 70 70 65 61 72 73 20 74 6f 20  f NN appears to 
128a4 67 69 76 65 20 74 68 65 20 62 65 73 74 20 72 65  give the best re
128a5 73 75 6c 74 73 20 6f 76 65 72 61 6c 6c 2e 0a 2a  sults overall..*
128a6 2f 0a 23 64 65 66 69 6e 65 20 4e 4e 20 31 20 20  /.#define NN 1  
128a7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
128a8 6d 62 65 72 20 6f 66 20 6e 65 69 67 68 62 6f 72  mber of neighbor
128a9 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65  s on either side
128aa 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a 23 64 65   of pPage */.#de
128ab 66 69 6e 65 20 4e 42 20 28 4e 4e 2a 32 2b 31 29  fine NB (NN*2+1)
128ac 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 70        /* Total p
128ad 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e  ages involved in
128ae 20 74 68 65 20 62 61 6c 61 6e 63 65 20 2a 2f 0a   the balance */.
128af 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65  ./* Forward refe
128b0 72 65 6e 63 65 20 2a 2f 0a 73 74 61 74 69 63 20  rence */.static 
128b1 69 6e 74 20 62 61 6c 61 6e 63 65 28 4d 65 6d 50  int balance(MemP
128b2 61 67 65 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69 66  age*, int);..#if
128b3 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
128b4 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 0a 2f 2a  _QUICKBALANCE./*
128b5 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e  .** This version
128b6 20 6f 66 20 62 61 6c 61 6e 63 65 28 29 20 68 61   of balance() ha
128b7 6e 64 6c 65 73 20 74 68 65 20 63 6f 6d 6d 6f 6e  ndles the common
128b8 20 73 70 65 63 69 61 6c 20 63 61 73 65 20 77 68   special case wh
128b9 65 72 65 0a 2a 2a 20 61 20 6e 65 77 20 65 6e 74  ere.** a new ent
128ba 72 79 20 69 73 20 62 65 69 6e 67 20 69 6e 73 65  ry is being inse
128bb 72 74 65 64 20 6f 6e 20 74 68 65 20 65 78 74 72  rted on the extr
128bc 65 6d 65 20 72 69 67 68 74 2d 65 6e 64 20 6f 66  eme right-end of
128bd 20 74 68 65 0a 2a 2a 20 74 72 65 65 2c 20 69 6e   the.** tree, in
128be 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 77 68   other words, wh
128bf 65 6e 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79  en the new entry
128c0 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65   will become the
128c1 20 6c 61 72 67 65 73 74 0a 2a 2a 20 65 6e 74 72   largest.** entr
128c2 79 20 69 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a  y in the tree..*
128c3 2a 0a 2a 2a 20 49 6e 73 74 65 61 64 20 6f 66 20  *.** Instead of 
128c4 74 72 79 69 6e 67 20 62 61 6c 61 6e 63 65 20 74  trying balance t
128c5 68 65 20 33 20 72 69 67 68 74 2d 6d 6f 73 74 20  he 3 right-most 
128c6 6c 65 61 66 20 70 61 67 65 73 2c 20 6a 75 73 74  leaf pages, just
128c7 20 61 64 64 0a 2a 2a 20 61 20 6e 65 77 20 70 61   add.** a new pa
128c8 67 65 20 74 6f 20 74 68 65 20 72 69 67 68 74 2d  ge to the right-
128c9 68 61 6e 64 20 73 69 64 65 20 61 6e 64 20 70 75  hand side and pu
128ca 74 20 74 68 65 20 6f 6e 65 20 6e 65 77 20 65 6e  t the one new en
128cb 74 72 79 20 69 6e 0a 2a 2a 20 74 68 61 74 20 70  try in.** that p
128cc 61 67 65 2e 20 20 54 68 69 73 20 6c 65 61 76 65  age.  This leave
128cd 73 20 74 68 65 20 72 69 67 68 74 20 73 69 64 65  s the right side
128ce 20 6f 66 20 74 68 65 20 74 72 65 65 20 73 6f 6d   of the tree som
128cf 65 77 68 61 74 0a 2a 2a 20 75 6e 62 61 6c 61 6e  ewhat.** unbalan
128d0 63 65 64 2e 20 20 42 75 74 20 6f 64 64 73 20 61  ced.  But odds a
128d1 72 65 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20  re that we will 
128d2 62 65 20 69 6e 73 65 72 74 69 6e 67 20 6e 65 77  be inserting new
128d3 20 65 6e 74 72 69 65 73 0a 2a 2a 20 61 74 20 74   entries.** at t
128d4 68 65 20 65 6e 64 20 73 6f 6f 6e 20 61 66 74 65  he end soon afte
128d5 72 77 61 72 64 73 20 73 6f 20 74 68 65 20 6e 65  rwards so the ne
128d6 61 72 6c 79 20 65 6d 70 74 79 20 70 61 67 65 20  arly empty page 
128d7 77 69 6c 6c 20 71 75 69 63 6b 6c 79 0a 2a 2a 20  will quickly.** 
128d8 66 69 6c 6c 20 75 70 2e 20 20 4f 6e 20 61 76 65  fill up.  On ave
128d9 72 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 50 61 67  rage..**.** pPag
128da 65 20 69 73 20 74 68 65 20 6c 65 61 66 20 70 61  e is the leaf pa
128db 67 65 20 77 68 69 63 68 20 69 73 20 74 68 65 20  ge which is the 
128dc 72 69 67 68 74 2d 6d 6f 73 74 20 70 61 67 65 20  right-most page 
128dd 69 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 20  in the tree..** 
128de 70 50 61 72 65 6e 74 20 69 73 20 69 74 73 20 70  pParent is its p
128df 61 72 65 6e 74 2e 20 20 70 50 61 67 65 20 6d 75  arent.  pPage mu
128e0 73 74 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65  st have a single
128e1 20 6f 76 65 72 66 6c 6f 77 20 65 6e 74 72 79 0a   overflow entry.
128e2 2a 2a 20 77 68 69 63 68 20 69 73 20 61 6c 73 6f  ** which is also
128e3 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20   the right-most 
128e4 65 6e 74 72 79 20 6f 6e 20 74 68 65 20 70 61 67  entry on the pag
128e5 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
128e6 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 4d   balance_quick(M
128e7 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 4d  emPage *pPage, M
128e8 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74 29  emPage *pParent)
128e9 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65  {.  int rc;.  Me
128ea 6d 50 61 67 65 20 2a 70 4e 65 77 3b 0a 20 20 50  mPage *pNew;.  P
128eb 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b 0a 20 20 75  gno pgnoNew;.  u
128ec 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 75 31 36 20  8 *pCell;.  u16 
128ed 73 7a 43 65 6c 6c 3b 0a 20 20 43 65 6c 6c 49 6e  szCell;.  CellIn
128ee 66 6f 20 69 6e 66 6f 3b 0a 20 20 42 74 53 68 61  fo info;.  BtSha
128ef 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65  red *pBt = pPage
128f0 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 70 61 72  ->pBt;.  int par
128f1 65 6e 74 49 64 78 20 3d 20 70 50 61 72 65 6e 74  entIdx = pParent
128f2 2d 3e 6e 43 65 6c 6c 3b 20 20 20 2f 2a 20 70 50  ->nCell;   /* pP
128f3 61 72 65 6e 74 20 6e 65 77 20 64 69 76 69 64 65  arent new divide
128f4 72 20 63 65 6c 6c 20 69 6e 64 65 78 20 2a 2f 0a  r cell index */.
128f5 20 20 69 6e 74 20 70 61 72 65 6e 74 53 69 7a 65    int parentSize
128f6 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
128f7 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6e      /* Size of n
128f8 65 77 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20  ew divider cell 
128f9 2a 2f 0a 20 20 75 38 20 70 61 72 65 6e 74 43 65  */.  u8 parentCe
128fa 6c 6c 5b 36 34 5d 3b 20 20 20 20 20 20 20 20 20  ll[64];         
128fb 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20         /* Space 
128fc 66 6f 72 20 74 68 65 20 6e 65 77 20 64 69 76 69  for the new divi
128fd 64 65 72 20 63 65 6c 6c 20 2a 2f 0a 0a 20 20 61  der cell */..  a
128fe 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
128ff 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d  utex_held(pPage-
12900 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  >pBt->mutex) );.
12901 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61  .  /* Allocate a
12902 20 6e 65 77 20 70 61 67 65 2e 20 49 6e 73 65 72   new page. Inser
12903 74 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63  t the overflow c
12904 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 0a 20  ell from pPage. 
12905 20 2a 2a 20 69 6e 74 6f 20 69 74 2e 20 54 68 65   ** into it. The
12906 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 6f 76 65  n remove the ove
12907 72 66 6c 6f 77 20 63 65 6c 6c 20 66 72 6f 6d 20  rflow cell from 
12908 70 50 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 72 63  pPage..  */.  rc
12909 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65   = allocateBtree
1290a 50 61 67 65 28 70 42 74 2c 20 26 70 4e 65 77 2c  Page(pBt, &pNew,
1290b 20 26 70 67 6e 6f 4e 65 77 2c 20 30 2c 20 30 29   &pgnoNew, 0, 0)
1290c 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
1290d 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74  TE_OK ){.    ret
1290e 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 43  urn rc;.  }.  pC
1290f 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 61 4f 76  ell = pPage->aOv
12910 66 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a 20 20 73  fl[0].pCell;.  s
12911 7a 43 65 6c 6c 20 3d 20 63 65 6c 6c 53 69 7a 65  zCell = cellSize
12912 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c  Ptr(pPage, pCell
12913 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 4e  );.  zeroPage(pN
12914 65 77 2c 20 70 50 61 67 65 2d 3e 61 44 61 74 61  ew, pPage->aData
12915 5b 30 5d 29 3b 0a 20 20 61 73 73 65 6d 62 6c 65  [0]);.  assemble
12916 50 61 67 65 28 70 4e 65 77 2c 20 31 2c 20 26 70  Page(pNew, 1, &p
12917 43 65 6c 6c 2c 20 26 73 7a 43 65 6c 6c 29 3b 0a  Cell, &szCell);.
12918 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c    pPage->nOverfl
12919 6f 77 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 53 65  ow = 0;..  /* Se
1291a 74 20 74 68 65 20 70 61 72 65 6e 74 20 6f 66 20  t the parent of 
1291b 74 68 65 20 6e 65 77 6c 79 20 61 6c 6c 6f 63 61  the newly alloca
1291c 74 65 64 20 70 61 67 65 20 74 6f 20 70 50 61 72  ted page to pPar
1291d 65 6e 74 2e 20 2a 2f 0a 20 20 70 4e 65 77 2d 3e  ent. */.  pNew->
1291e 70 50 61 72 65 6e 74 20 3d 20 70 50 61 72 65 6e  pParent = pParen
1291f 74 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65  t;.  sqlite3Page
12920 72 52 65 66 28 70 50 61 72 65 6e 74 2d 3e 70 44  rRef(pParent->pD
12921 62 50 61 67 65 29 3b 0a 0a 20 20 2f 2a 20 70 50  bPage);..  /* pP
12922 61 67 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79  age is currently
12923 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64   the right-child
12924 20 6f 66 20 70 50 61 72 65 6e 74 2e 20 43 68 61   of pParent. Cha
12925 6e 67 65 20 74 68 69 73 0a 20 20 2a 2a 20 73 6f  nge this.  ** so
12926 20 74 68 61 74 20 74 68 65 20 72 69 67 68 74 2d   that the right-
12927 63 68 69 6c 64 20 69 73 20 74 68 65 20 6e 65 77  child is the new
12928 20 70 61 67 65 20 61 6c 6c 6f 63 61 74 65 64 20   page allocated 
12929 61 62 6f 76 65 20 61 6e 64 0a 20 20 2a 2a 20 70  above and.  ** p
1292a 50 61 67 65 20 69 73 20 74 68 65 20 6e 65 78 74  Page is the next
1292b 2d 74 6f 2d 72 69 67 68 74 20 63 68 69 6c 64 2e  -to-right child.
1292c 20 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28   .  */.  assert(
1292d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20   pPage->nCell>0 
1292e 29 3b 0a 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e  );.  pCell = fin
1292f 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 50 61  dCell(pPage, pPa
12930 67 65 2d 3e 6e 43 65 6c 6c 2d 31 29 3b 0a 20 20  ge->nCell-1);.  
12931 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73  sqlite3BtreePars
12932 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20  eCellPtr(pPage, 
12933 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20  pCell, &info);. 
12934 20 72 63 20 3d 20 66 69 6c 6c 49 6e 43 65 6c 6c   rc = fillInCell
12935 28 70 50 61 72 65 6e 74 2c 20 70 61 72 65 6e 74  (pParent, parent
12936 43 65 6c 6c 2c 20 30 2c 20 69 6e 66 6f 2e 6e 4b  Cell, 0, info.nK
12937 65 79 2c 20 30 2c 20 30 2c 20 30 2c 20 26 70 61  ey, 0, 0, 0, &pa
12938 72 65 6e 74 53 69 7a 65 29 3b 0a 20 20 69 66 28  rentSize);.  if(
12939 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1293a 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
1293b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
1293c 61 72 65 6e 74 53 69 7a 65 3c 36 34 20 29 3b 0a  arentSize<64 );.
1293d 20 20 72 63 20 3d 20 69 6e 73 65 72 74 43 65 6c    rc = insertCel
1293e 6c 28 70 50 61 72 65 6e 74 2c 20 70 61 72 65 6e  l(pParent, paren
1293f 74 49 64 78 2c 20 70 61 72 65 6e 74 43 65 6c 6c  tIdx, parentCell
12940 2c 20 70 61 72 65 6e 74 53 69 7a 65 2c 20 30 2c  , parentSize, 0,
12941 20 34 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53   4);.  if( rc!=S
12942 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
12943 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
12944 20 70 75 74 34 62 79 74 65 28 66 69 6e 64 4f 76   put4byte(findOv
12945 65 72 66 6c 6f 77 43 65 6c 6c 28 70 50 61 72 65  erflowCell(pPare
12946 6e 74 2c 70 61 72 65 6e 74 49 64 78 29 2c 20 70  nt,parentIdx), p
12947 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 70  Page->pgno);.  p
12948 75 74 34 62 79 74 65 28 26 70 50 61 72 65 6e 74  ut4byte(&pParent
12949 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d  ->aData[pParent-
1294a 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 70  >hdrOffset+8], p
1294b 67 6e 6f 4e 65 77 29 3b 0a 0a 23 69 66 6e 64 65  gnoNew);..#ifnde
1294c 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
1294d 54 4f 56 41 43 55 55 4d 0a 20 20 2f 2a 20 49 66  TOVACUUM.  /* If
1294e 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f   this is an auto
1294f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65  -vacuum database
12950 2c 20 75 70 64 61 74 65 20 74 68 65 20 70 6f 69  , update the poi
12951 6e 74 65 72 20 6d 61 70 0a 20 20 2a 2a 20 77 69  nter map.  ** wi
12952 74 68 20 65 6e 74 72 69 65 73 20 66 6f 72 20 74  th entries for t
12953 68 65 20 6e 65 77 20 70 61 67 65 2c 20 61 6e 64  he new page, and
12954 20 61 6e 79 20 70 6f 69 6e 74 65 72 20 66 72 6f   any pointer fro
12955 6d 20 74 68 65 20 0a 20 20 2a 2a 20 63 65 6c 6c  m the .  ** cell
12956 20 6f 6e 20 74 68 65 20 70 61 67 65 20 74 6f 20   on the page to 
12957 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  an overflow page
12958 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 42 74  ..  */.  if( pBt
12959 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a  ->autoVacuum ){.
1295a 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50      rc = ptrmapP
1295b 75 74 28 70 42 74 2c 20 70 67 6e 6f 4e 65 77 2c  ut(pBt, pgnoNew,
1295c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70   PTRMAP_BTREE, p
1295d 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 29 3b 0a 20  Parent->pgno);. 
1295e 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
1295f 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63  E_OK ){.      rc
12960 20 3d 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c   = ptrmapPutOvfl
12961 28 70 4e 65 77 2c 20 30 29 3b 0a 20 20 20 20 7d  (pNew, 0);.    }
12962 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
12963 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
12964 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77  releasePage(pNew
12965 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
12966 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  rc;.    }.  }.#e
12967 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 6c 65 61  ndif..  /* Relea
12968 73 65 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  se the reference
12969 20 74 6f 20 74 68 65 20 6e 65 77 20 70 61 67 65   to the new page
1296a 20 61 6e 64 20 62 61 6c 61 6e 63 65 20 74 68 65   and balance the
1296b 20 70 61 72 65 6e 74 20 70 61 67 65 2c 0a 20 20   parent page,.  
1296c 2a 2a 20 69 6e 20 63 61 73 65 20 74 68 65 20 64  ** in case the d
1296d 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 73 65  ivider cell inse
1296e 72 74 65 64 20 63 61 75 73 65 64 20 69 74 20 74  rted caused it t
1296f 6f 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c  o become overful
12970 6c 2e 0a 20 20 2a 2f 0a 20 20 72 65 6c 65 61 73  l..  */.  releas
12971 65 50 61 67 65 28 70 4e 65 77 29 3b 0a 20 20 72  ePage(pNew);.  r
12972 65 74 75 72 6e 20 62 61 6c 61 6e 63 65 28 70 50  eturn balance(pP
12973 61 72 65 6e 74 2c 20 30 29 3b 0a 7d 0a 23 65 6e  arent, 0);.}.#en
12974 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
12975 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 20  IT_QUICKBALANCE 
12976 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  */../*.** This r
12977 6f 75 74 69 6e 65 20 72 65 64 69 73 74 72 69 62  outine redistrib
12978 75 74 65 73 20 43 65 6c 6c 73 20 6f 6e 20 70 50  utes Cells on pP
12979 61 67 65 20 61 6e 64 20 75 70 20 74 6f 20 4e 4e  age and up to NN
1297a 2a 32 20 73 69 62 6c 69 6e 67 73 0a 2a 2a 20 6f  *2 siblings.** o
1297b 66 20 70 50 61 67 65 20 73 6f 20 74 68 61 74 20  f pPage so that 
1297c 61 6c 6c 20 70 61 67 65 73 20 68 61 76 65 20 61  all pages have a
1297d 62 6f 75 74 20 74 68 65 20 73 61 6d 65 20 61 6d  bout the same am
1297e 6f 75 6e 74 20 6f 66 20 66 72 65 65 20 73 70 61  ount of free spa
1297f 63 65 2e 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 4e  ce..** Usually N
12980 4e 20 73 69 62 6c 69 6e 67 73 20 6f 6e 20 65 69  N siblings on ei
12981 74 68 65 72 20 73 69 64 65 20 6f 66 20 70 50 61  ther side of pPa
12982 67 65 20 69 73 20 75 73 65 64 20 69 6e 20 74 68  ge is used in th
12983 65 20 62 61 6c 61 6e 63 69 6e 67 2c 0a 2a 2a 20  e balancing,.** 
12984 74 68 6f 75 67 68 20 6d 6f 72 65 20 73 69 62 6c  though more sibl
12985 69 6e 67 73 20 6d 69 67 68 74 20 63 6f 6d 65 20  ings might come 
12986 66 72 6f 6d 20 6f 6e 65 20 73 69 64 65 20 69 66  from one side if
12987 20 70 50 61 67 65 20 69 73 20 74 68 65 20 66 69   pPage is the fi
12988 72 73 74 0a 2a 2a 20 6f 72 20 6c 61 73 74 20 63  rst.** or last c
12989 68 69 6c 64 20 6f 66 20 69 74 73 20 70 61 72 65  hild of its pare
1298a 6e 74 2e 20 20 49 66 20 70 50 61 67 65 20 68 61  nt.  If pPage ha
1298b 73 20 66 65 77 65 72 20 74 68 61 6e 20 32 2a 4e  s fewer than 2*N
1298c 4e 20 73 69 62 6c 69 6e 67 73 0a 2a 2a 20 28 73  N siblings.** (s
1298d 6f 6d 65 74 68 69 6e 67 20 77 68 69 63 68 20 63  omething which c
1298e 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69  an only happen i
1298f 66 20 70 50 61 67 65 20 69 73 20 74 68 65 20 72  f pPage is the r
12990 6f 6f 74 20 70 61 67 65 20 6f 72 20 61 20 0a 2a  oot page or a .*
12991 2a 20 63 68 69 6c 64 20 6f 66 20 72 6f 6f 74 29  * child of root)
12992 20 74 68 65 6e 20 61 6c 6c 20 61 76 61 69 6c 61   then all availa
12993 62 6c 65 20 73 69 62 6c 69 6e 67 73 20 70 61 72  ble siblings par
12994 74 69 63 69 70 61 74 65 20 69 6e 20 74 68 65 20  ticipate in the 
12995 62 61 6c 61 6e 63 69 6e 67 2e 0a 2a 2a 0a 2a 2a  balancing..**.**
12996 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73   The number of s
12997 69 62 6c 69 6e 67 73 20 6f 66 20 70 50 61 67 65  iblings of pPage
12998 20 6d 69 67 68 74 20 62 65 20 69 6e 63 72 65 61   might be increa
12999 73 65 64 20 6f 72 20 64 65 63 72 65 61 73 65 64  sed or decreased
1299a 20 62 79 20 6f 6e 65 20 6f 72 0a 2a 2a 20 74 77   by one or.** tw
1299b 6f 20 69 6e 20 61 6e 20 65 66 66 6f 72 74 20 74  o in an effort t
1299c 6f 20 6b 65 65 70 20 70 61 67 65 73 20 6e 65 61  o keep pages nea
1299d 72 6c 79 20 66 75 6c 6c 20 62 75 74 20 6e 6f 74  rly full but not
1299e 20 6f 76 65 72 20 66 75 6c 6c 2e 20 54 68 65 20   over full. The 
1299f 72 6f 6f 74 20 70 61 67 65 0a 2a 2a 20 69 73 20  root page.** is 
129a0 73 70 65 63 69 61 6c 20 61 6e 64 20 69 73 20 61  special and is a
129a1 6c 6c 6f 77 65 64 20 74 6f 20 62 65 20 6e 65 61  llowed to be nea
129a2 72 6c 79 20 65 6d 70 74 79 2e 20 49 66 20 70 50  rly empty. If pP
129a3 61 67 65 20 69 73 20 0a 2a 2a 20 74 68 65 20 72  age is .** the r
129a4 6f 6f 74 20 70 61 67 65 2c 20 74 68 65 6e 20 74  oot page, then t
129a5 68 65 20 64 65 70 74 68 20 6f 66 20 74 68 65 20  he depth of the 
129a6 74 72 65 65 20 6d 69 67 68 74 20 62 65 20 69 6e  tree might be in
129a7 63 72 65 61 73 65 64 0a 2a 2a 20 6f 72 20 64 65  creased.** or de
129a8 63 72 65 61 73 65 64 20 62 79 20 6f 6e 65 2c 20  creased by one, 
129a9 61 73 20 6e 65 63 65 73 73 61 72 79 2c 20 74 6f  as necessary, to
129aa 20 6b 65 65 70 20 74 68 65 20 72 6f 6f 74 20 70   keep the root p
129ab 61 67 65 20 66 72 6f 6d 20 62 65 69 6e 67 0a 2a  age from being.*
129ac 2a 20 6f 76 65 72 66 75 6c 6c 20 6f 72 20 63 6f  * overfull or co
129ad 6d 70 6c 65 74 65 6c 79 20 65 6d 70 74 79 2e 0a  mpletely empty..
129ae 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
129af 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  when this routin
129b0 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 73 6f 6d  e is called, som
129b1 65 20 6f 66 20 74 68 65 20 43 65 6c 6c 73 20 6f  e of the Cells o
129b2 6e 20 70 50 61 67 65 0a 2a 2a 20 6d 69 67 68 74  n pPage.** might
129b3 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 62 65   not actually be
129b4 20 73 74 6f 72 65 64 20 69 6e 20 70 50 61 67 65   stored in pPage
129b5 2d 3e 61 44 61 74 61 5b 5d 2e 20 20 54 68 69 73  ->aData[].  This
129b6 20 63 61 6e 20 68 61 70 70 65 6e 0a 2a 2a 20 69   can happen.** i
129b7 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6f 76  f the page is ov
129b8 65 72 66 75 6c 6c 2e 20 20 50 61 72 74 20 6f 66  erfull.  Part of
129b9 20 74 68 65 20 6a 6f 62 20 6f 66 20 74 68 69 73   the job of this
129ba 20 72 6f 75 74 69 6e 65 20 69 73 20 74 6f 0a 2a   routine is to.*
129bb 2a 20 6d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20  * make sure all 
129bc 43 65 6c 6c 73 20 66 6f 72 20 70 50 61 67 65 20  Cells for pPage 
129bd 6f 6e 63 65 20 61 67 61 69 6e 20 66 69 74 20 69  once again fit i
129be 6e 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d  n pPage->aData[]
129bf 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 63  ..**.** In the c
129c0 6f 75 72 73 65 20 6f 66 20 62 61 6c 61 6e 63 69  ourse of balanci
129c1 6e 67 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20  ng the siblings 
129c2 6f 66 20 70 50 61 67 65 2c 20 74 68 65 20 70 61  of pPage, the pa
129c3 72 65 6e 74 20 6f 66 20 70 50 61 67 65 0a 2a 2a  rent of pPage.**
129c4 20 6d 69 67 68 74 20 62 65 63 6f 6d 65 20 6f 76   might become ov
129c5 65 72 66 75 6c 6c 20 6f 72 20 75 6e 64 65 72 66  erfull or underf
129c6 75 6c 6c 2e 20 20 49 66 20 74 68 61 74 20 68 61  ull.  If that ha
129c7 70 70 65 6e 73 2c 20 74 68 65 6e 20 74 68 69 73  ppens, then this
129c8 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63   routine.** is c
129c9 61 6c 6c 65 64 20 72 65 63 75 72 73 69 76 65 6c  alled recursivel
129ca 79 20 6f 6e 20 74 68 65 20 70 61 72 65 6e 74 2e  y on the parent.
129cb 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 72  .**.** If this r
129cc 6f 75 74 69 6e 65 20 66 61 69 6c 73 20 66 6f 72  outine fails for
129cd 20 61 6e 79 20 72 65 61 73 6f 6e 2c 20 69 74 20   any reason, it 
129ce 6d 69 67 68 74 20 6c 65 61 76 65 20 74 68 65 20  might leave the 
129cf 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 6e 20 61  database.** in a
129d0 20 63 6f 72 72 75 70 74 65 64 20 73 74 61 74 65   corrupted state
129d1 2e 20 20 53 6f 20 69 66 20 74 68 69 73 20 72 6f  .  So if this ro
129d2 75 74 69 6e 65 20 66 61 69 6c 73 2c 20 74 68 65  utine fails, the
129d3 20 64 61 74 61 62 61 73 65 20 73 68 6f 75 6c 64   database should
129d4 0a 2a 2a 20 62 65 20 72 6f 6c 6c 65 64 20 62 61  .** be rolled ba
129d5 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ck..*/.static in
129d6 74 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f  t balance_nonroo
129d7 74 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  t(MemPage *pPage
129d8 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  ){.  MemPage *pP
129d9 61 72 65 6e 74 3b 20 20 20 20 20 20 20 20 20 20  arent;          
129da 20 20 2f 2a 20 54 68 65 20 70 61 72 65 6e 74 20    /* The parent 
129db 6f 66 20 70 50 61 67 65 20 2a 2f 0a 20 20 42 74  of pPage */.  Bt
129dc 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20  Shared *pBt;    
129dd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
129de 65 20 77 68 6f 6c 65 20 64 61 74 61 62 61 73 65  e whole database
129df 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 20   */.  int nCell 
129e0 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
129e1 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
129e2 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b  cells in apCell[
129e3 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 78 43  ] */.  int nMaxC
129e4 65 6c 6c 73 20 3d 20 30 3b 20 20 20 20 20 20 20  ells = 0;       
129e5 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64      /* Allocated
129e6 20 73 69 7a 65 20 6f 66 20 61 70 43 65 6c 6c 2c   size of apCell,
129e7 20 73 7a 43 65 6c 6c 2c 20 61 46 72 6f 6d 2e 20   szCell, aFrom. 
129e8 2a 2f 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b 20 20  */.  int nOld;  
129e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
129ea 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70    /* Number of p
129eb 61 67 65 73 20 69 6e 20 61 70 4f 6c 64 5b 5d 20  ages in apOld[] 
129ec 2a 2f 0a 20 20 69 6e 74 20 6e 4e 65 77 3b 20 20  */.  int nNew;  
129ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
129ee 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70    /* Number of p
129ef 61 67 65 73 20 69 6e 20 61 70 4e 65 77 5b 5d 20  ages in apNew[] 
129f0 2a 2f 0a 20 20 69 6e 74 20 6e 44 69 76 3b 20 20  */.  int nDiv;  
129f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
129f2 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
129f3 65 6c 6c 73 20 69 6e 20 61 70 44 69 76 5b 5d 20  ells in apDiv[] 
129f4 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6b  */.  int i, j, k
129f5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
129f6 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
129f7 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 3b  rs */.  int idx;
129f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
129f9 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
129fa 20 70 50 61 67 65 20 69 6e 20 70 50 61 72 65 6e   pPage in pParen
129fb 74 2d 3e 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20  t->aCell[] */.  
129fc 69 6e 74 20 6e 78 44 69 76 3b 20 20 20 20 20 20  int nxDiv;      
129fd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
129fe 4e 65 78 74 20 64 69 76 69 64 65 72 20 73 6c 6f  Next divider slo
129ff 74 20 69 6e 20 70 50 61 72 65 6e 74 2d 3e 61 43  t in pParent->aC
12a00 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 72  ell[] */.  int r
12a01 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
12a02 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
12a03 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
12a04 69 6e 74 20 6c 65 61 66 43 6f 72 72 65 63 74 69  int leafCorrecti
12a05 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  on;          /* 
12a06 34 20 69 66 20 70 50 61 67 65 20 69 73 20 61 20  4 if pPage is a 
12a07 6c 65 61 66 2e 20 20 30 20 69 66 20 6e 6f 74 20  leaf.  0 if not 
12a08 2a 2f 0a 20 20 69 6e 74 20 6c 65 61 66 44 61 74  */.  int leafDat
12a09 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  a;              
12a0a 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 50 61    /* True if pPa
12a0b 67 65 20 69 73 20 61 20 6c 65 61 66 20 6f 66 20  ge is a leaf of 
12a0c 61 20 4c 45 41 46 44 41 54 41 20 74 72 65 65 20  a LEAFDATA tree 
12a0d 2a 2f 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53  */.  int usableS
12a0e 70 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20  pace;           
12a0f 20 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 70 50    /* Bytes in pP
12a10 61 67 65 20 62 65 79 6f 6e 64 20 74 68 65 20 68  age beyond the h
12a11 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70  eader */.  int p
12a12 61 67 65 46 6c 61 67 73 3b 20 20 20 20 20 20 20  ageFlags;       
12a13 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
12a14 20 6f 66 20 70 50 61 67 65 2d 3e 61 44 61 74 61   of pPage->aData
12a15 5b 30 5d 20 2a 2f 0a 20 20 69 6e 74 20 73 75 62  [0] */.  int sub
12a16 74 6f 74 61 6c 3b 20 20 20 20 20 20 20 20 20 20  total;          
12a17 20 20 20 20 20 20 2f 2a 20 53 75 62 74 6f 74 61        /* Subtota
12a18 6c 20 6f 66 20 62 79 74 65 73 20 69 6e 20 63 65  l of bytes in ce
12a19 6c 6c 73 20 6f 6e 20 6f 6e 65 20 70 61 67 65 20  lls on one page 
12a1a 2a 2f 0a 20 20 69 6e 74 20 69 53 70 61 63 65 20  */.  int iSpace 
12a1b 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
12a1c 20 20 2f 2a 20 46 69 72 73 74 20 75 6e 75 73 65    /* First unuse
12a1d 64 20 62 79 74 65 20 6f 66 20 61 53 70 61 63 65  d byte of aSpace
12a1e 5b 5d 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  [] */.  MemPage 
12a1f 2a 61 70 4f 6c 64 5b 4e 42 5d 3b 20 20 20 20 20  *apOld[NB];     
12a20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 61 6e       /* pPage an
12a21 64 20 75 70 20 74 6f 20 74 77 6f 20 73 69 62 6c  d up to two sibl
12a22 69 6e 67 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 70  ings */.  Pgno p
12a23 67 6e 6f 4f 6c 64 5b 4e 42 5d 3b 20 20 20 20 20  gnoOld[NB];     
12a24 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e         /* Page n
12a25 75 6d 62 65 72 73 20 66 6f 72 20 65 61 63 68 20  umbers for each 
12a26 70 61 67 65 20 69 6e 20 61 70 4f 6c 64 5b 5d 20  page in apOld[] 
12a27 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70  */.  MemPage *ap
12a28 43 6f 70 79 5b 4e 42 5d 3b 20 20 20 20 20 20 20  Copy[NB];       
12a29 20 20 2f 2a 20 50 72 69 76 61 74 65 20 63 6f 70    /* Private cop
12a2a 69 65 73 20 6f 66 20 61 70 4f 6c 64 5b 5d 20 70  ies of apOld[] p
12a2b 61 67 65 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67  ages */.  MemPag
12a2c 65 20 2a 61 70 4e 65 77 5b 4e 42 2b 32 5d 3b 20  e *apNew[NB+2]; 
12a2d 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20         /* pPage 
12a2e 61 6e 64 20 75 70 20 74 6f 20 4e 42 20 73 69 62  and up to NB sib
12a2f 6c 69 6e 67 73 20 61 66 74 65 72 20 62 61 6c 61  lings after bala
12a30 6e 63 69 6e 67 20 2a 2f 0a 20 20 50 67 6e 6f 20  ncing */.  Pgno 
12a31 70 67 6e 6f 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20  pgnoNew[NB+2];  
12a32 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20          /* Page 
12a33 6e 75 6d 62 65 72 73 20 66 6f 72 20 65 61 63 68  numbers for each
12a34 20 70 61 67 65 20 69 6e 20 61 70 4e 65 77 5b 5d   page in apNew[]
12a35 20 2a 2f 0a 20 20 75 38 20 2a 61 70 44 69 76 5b   */.  u8 *apDiv[
12a36 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20  NB];            
12a37 20 20 20 2f 2a 20 44 69 76 69 64 65 72 20 63 65     /* Divider ce
12a38 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 20 2a  lls in pParent *
12a39 2f 0a 20 20 69 6e 74 20 63 6e 74 4e 65 77 5b 4e  /.  int cntNew[N
12a3a 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20  B+2];           
12a3b 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 61 43 65   /* Index in aCe
12a3c 6c 6c 5b 5d 20 6f 66 20 63 65 6c 6c 20 61 66 74  ll[] of cell aft
12a3d 65 72 20 69 2d 74 68 20 70 61 67 65 20 2a 2f 0a  er i-th page */.
12a3e 20 20 69 6e 74 20 73 7a 4e 65 77 5b 4e 42 2b 32    int szNew[NB+2
12a3f 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ];             /
12a40 2a 20 43 6f 6d 62 69 6e 65 64 20 73 69 7a 65 20  * Combined size 
12a41 6f 66 20 63 65 6c 6c 73 20 70 6c 61 63 65 20 6f  of cells place o
12a42 6e 20 69 2d 74 68 20 70 61 67 65 20 2a 2f 0a 20  n i-th page */. 
12a43 20 75 38 20 2a 2a 61 70 43 65 6c 6c 20 3d 20 30   u8 **apCell = 0
12a44 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
12a45 20 41 6c 6c 20 63 65 6c 6c 73 20 62 65 67 69 6e   All cells begin
12a46 20 62 61 6c 61 6e 63 65 64 20 2a 2f 0a 20 20 75   balanced */.  u
12a47 31 36 20 2a 73 7a 43 65 6c 6c 3b 20 20 20 20 20  16 *szCell;     
12a48 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
12a49 6f 63 61 6c 20 73 69 7a 65 20 6f 66 20 61 6c 6c  ocal size of all
12a4a 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c   cells in apCell
12a4b 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 43 6f 70  [] */.  u8 *aCop
12a4c 79 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 20  y[NB];          
12a4d 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f       /* Space fo
12a4e 72 20 68 6f 6c 64 69 6e 67 20 64 61 74 61 20 6f  r holding data o
12a4f 66 20 61 70 43 6f 70 79 5b 5d 20 2a 2f 0a 20 20  f apCopy[] */.  
12a50 75 38 20 2a 61 53 70 61 63 65 3b 20 20 20 20 20  u8 *aSpace;     
12a51 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
12a52 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 63 6f  Space to hold co
12a53 70 69 65 73 20 6f 66 20 64 69 76 69 64 65 72 73  pies of dividers
12a54 20 63 65 6c 6c 73 20 2a 2f 0a 23 69 66 6e 64 65   cells */.#ifnde
12a55 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
12a56 54 4f 56 41 43 55 55 4d 0a 20 20 75 38 20 2a 61  TOVACUUM.  u8 *a
12a57 46 72 6f 6d 20 3d 20 30 3b 0a 23 65 6e 64 69 66  From = 0;.#endif
12a58 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
12a59 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
12a5a 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
12a5b 29 20 29 3b 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a  ) );..  /* .  **
12a5c 20 46 69 6e 64 20 74 68 65 20 70 61 72 65 6e 74   Find the parent
12a5d 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 61 73   page..  */.  as
12a5e 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49  sert( pPage->isI
12a5f 6e 69 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28  nit );.  assert(
12a60 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77   sqlite3PagerIsw
12a61 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e  riteable(pPage->
12a62 70 44 62 50 61 67 65 29 20 7c 7c 20 70 50 61 67  pDbPage) || pPag
12a63 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 20  e->nOverflow==1 
12a64 29 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 67 65  );.  pBt = pPage
12a65 2d 3e 70 42 74 3b 0a 20 20 70 50 61 72 65 6e 74  ->pBt;.  pParent
12a66 20 3d 20 70 50 61 67 65 2d 3e 70 50 61 72 65 6e   = pPage->pParen
12a67 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  t;.  assert( pPa
12a68 72 65 6e 74 20 29 3b 0a 20 20 69 66 28 20 53 51  rent );.  if( SQ
12a69 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73  LITE_OK!=(rc = s
12a6a 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
12a6b 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67  (pParent->pDbPag
12a6c 65 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  e)) ){.    retur
12a6d 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 54 52 41 43  n rc;.  }.  TRAC
12a6e 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 62 65 67  E(("BALANCE: beg
12a6f 69 6e 20 70 61 67 65 20 25 64 20 63 68 69 6c 64  in page %d child
12a70 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 61 67 65   of %d\n", pPage
12a71 2d 3e 70 67 6e 6f 2c 20 70 50 61 72 65 6e 74 2d  ->pgno, pParent-
12a72 3e 70 67 6e 6f 29 29 3b 0a 0a 23 69 66 6e 64 65  >pgno));..#ifnde
12a73 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55  f SQLITE_OMIT_QU
12a74 49 43 4b 42 41 4c 41 4e 43 45 0a 20 20 2f 2a 0a  ICKBALANCE.  /*.
12a75 20 20 2a 2a 20 41 20 73 70 65 63 69 61 6c 20 63    ** A special c
12a76 61 73 65 3a 20 20 49 66 20 61 20 6e 65 77 20 65  ase:  If a new e
12a77 6e 74 72 79 20 68 61 73 20 6a 75 73 74 20 62 65  ntry has just be
12a78 65 6e 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f  en inserted into
12a79 20 61 0a 20 20 2a 2a 20 74 61 62 6c 65 20 28 74   a.  ** table (t
12a7a 68 61 74 20 69 73 2c 20 61 20 62 74 72 65 65 20  hat is, a btree 
12a7b 77 69 74 68 20 69 6e 74 65 67 65 72 20 6b 65 79  with integer key
12a7c 73 20 61 6e 64 20 61 6c 6c 20 64 61 74 61 20 61  s and all data a
12a7d 74 20 74 68 65 20 6c 65 61 76 65 73 29 0a 20 20  t the leaves).  
12a7e 2a 2a 20 61 6e 64 20 74 68 65 20 6e 65 77 20 65  ** and the new e
12a7f 6e 74 72 79 20 69 73 20 74 68 65 20 72 69 67 68  ntry is the righ
12a80 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 69 6e 20  t-most entry in 
12a81 74 68 65 20 74 72 65 65 20 28 69 74 20 68 61 73  the tree (it has
12a82 20 74 68 65 0a 20 20 2a 2a 20 6c 61 72 67 65 73   the.  ** larges
12a83 74 20 6b 65 79 29 20 74 68 65 6e 20 75 73 65 20  t key) then use 
12a84 74 68 65 20 73 70 65 63 69 61 6c 20 62 61 6c 61  the special bala
12a85 6e 63 65 5f 71 75 69 63 6b 28 29 20 72 6f 75 74  nce_quick() rout
12a86 69 6e 65 20 66 6f 72 0a 20 20 2a 2a 20 62 61 6c  ine for.  ** bal
12a87 61 6e 63 69 6e 67 2e 20 20 62 61 6c 61 6e 63 65  ancing.  balance
12a88 5f 71 75 69 63 6b 28 29 20 69 73 20 6d 75 63 68  _quick() is much
12a89 20 66 61 73 74 65 72 20 61 6e 64 20 72 65 73 75   faster and resu
12a8a 6c 74 73 20 69 6e 20 61 20 74 69 67 68 74 65 72  lts in a tighter
12a8b 0a 20 20 2a 2a 20 70 61 63 6b 69 6e 67 20 6f 66  .  ** packing of
12a8c 20 64 61 74 61 20 69 6e 20 74 68 65 20 63 6f 6d   data in the com
12a8d 6d 6f 6e 20 63 61 73 65 2e 0a 20 20 2a 2f 0a 20  mon case..  */. 
12a8e 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66   if( pPage->leaf
12a8f 20 26 26 0a 20 20 20 20 20 20 70 50 61 67 65 2d   &&.      pPage-
12a90 3e 69 6e 74 4b 65 79 20 26 26 0a 20 20 20 20 20  >intKey &&.     
12a91 20 70 50 61 67 65 2d 3e 6c 65 61 66 44 61 74 61   pPage->leafData
12a92 20 26 26 0a 20 20 20 20 20 20 70 50 61 67 65 2d   &&.      pPage-
12a93 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 20 26 26  >nOverflow==1 &&
12a94 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 61 4f  .      pPage->aO
12a95 76 66 6c 5b 30 5d 2e 69 64 78 3d 3d 70 50 61 67  vfl[0].idx==pPag
12a96 65 2d 3e 6e 43 65 6c 6c 20 26 26 0a 20 20 20 20  e->nCell &&.    
12a97 20 20 70 50 61 67 65 2d 3e 70 50 61 72 65 6e 74    pPage->pParent
12a98 2d 3e 70 67 6e 6f 21 3d 31 20 26 26 0a 20 20 20  ->pgno!=1 &&.   
12a99 20 20 20 67 65 74 34 62 79 74 65 28 26 70 50 61     get4byte(&pPa
12a9a 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72  rent->aData[pPar
12a9b 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38  ent->hdrOffset+8
12a9c 5d 29 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 0a  ])==pPage->pgno.
12a9d 20 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 20    ){.    /*.    
12a9e 2a 2a 20 54 4f 44 4f 3a 20 43 68 65 63 6b 20 74  ** TODO: Check t
12a9f 68 65 20 73 69 62 6c 69 6e 67 73 20 74 6f 20 74  he siblings to t
12aa0 68 65 20 6c 65 66 74 20 6f 66 20 70 50 61 67 65  he left of pPage
12aa1 2e 20 49 74 20 6d 61 79 20 62 65 20 74 68 61 74  . It may be that
12aa2 0a 20 20 20 20 2a 2a 20 74 68 65 79 20 61 72 65  .    ** they are
12aa3 20 6e 6f 74 20 66 75 6c 6c 20 61 6e 64 20 6e 6f   not full and no
12aa4 20 6e 65 77 20 70 61 67 65 20 69 73 20 72 65 71   new page is req
12aa5 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  uired..    */.  
12aa6 20 20 72 65 74 75 72 6e 20 62 61 6c 61 6e 63 65    return balance
12aa7 5f 71 75 69 63 6b 28 70 50 61 67 65 2c 20 70 50  _quick(pPage, pP
12aa8 61 72 65 6e 74 29 3b 0a 20 20 7d 0a 23 65 6e 64  arent);.  }.#end
12aa9 69 66 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45  if..  if( SQLITE
12aaa 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74  _OK!=(rc = sqlit
12aab 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61  e3PagerWrite(pPa
12aac 67 65 2d 3e 70 44 62 50 61 67 65 29 29 20 29 7b  ge->pDbPage)) ){
12aad 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
12aae 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46    }..  /*.  ** F
12aaf 69 6e 64 20 74 68 65 20 63 65 6c 6c 20 69 6e 20  ind the cell in 
12ab0 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20  the parent page 
12ab1 77 68 6f 73 65 20 6c 65 66 74 20 63 68 69 6c 64  whose left child
12ab2 20 70 6f 69 6e 74 73 20 62 61 63 6b 0a 20 20 2a   points back.  *
12ab3 2a 20 74 6f 20 70 50 61 67 65 2e 20 20 54 68 65  * to pPage.  The
12ab4 20 22 69 64 78 22 20 76 61 72 69 61 62 6c 65 20   "idx" variable 
12ab5 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  is the index of 
12ab6 74 68 61 74 20 63 65 6c 6c 2e 20 20 49 66 20 70  that cell.  If p
12ab7 50 61 67 65 0a 20 20 2a 2a 20 69 73 20 74 68 65  Page.  ** is the
12ab8 20 72 69 67 68 74 6d 6f 73 74 20 63 68 69 6c 64   rightmost child
12ab9 20 6f 66 20 70 50 61 72 65 6e 74 20 74 68 65 6e   of pParent then
12aba 20 73 65 74 20 69 64 78 20 74 6f 20 70 50 61 72   set idx to pPar
12abb 65 6e 74 2d 3e 6e 43 65 6c 6c 20 0a 20 20 2a 2f  ent->nCell .  */
12abc 0a 20 20 69 66 28 20 70 50 61 72 65 6e 74 2d 3e  .  if( pParent->
12abd 69 64 78 53 68 69 66 74 20 29 7b 0a 20 20 20 20  idxShift ){.    
12abe 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 20 20 70  Pgno pgno;.    p
12abf 67 6e 6f 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e  gno = pPage->pgn
12ac0 6f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  o;.    assert( p
12ac1 67 6e 6f 3d 3d 73 71 6c 69 74 65 33 50 61 67 65  gno==sqlite3Page
12ac2 72 50 61 67 65 6e 75 6d 62 65 72 28 70 50 61 67  rPagenumber(pPag
12ac3 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  e->pDbPage) );. 
12ac4 20 20 20 66 6f 72 28 69 64 78 3d 30 3b 20 69 64     for(idx=0; id
12ac5 78 3c 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c  x<pParent->nCell
12ac6 3b 20 69 64 78 2b 2b 29 7b 0a 20 20 20 20 20 20  ; idx++){.      
12ac7 69 66 28 20 67 65 74 34 62 79 74 65 28 66 69 6e  if( get4byte(fin
12ac8 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69  dCell(pParent, i
12ac9 64 78 29 29 3d 3d 70 67 6e 6f 20 29 7b 0a 20 20  dx))==pgno ){.  
12aca 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
12acb 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61     }.    }.    a
12acc 73 73 65 72 74 28 20 69 64 78 3c 70 50 61 72 65  ssert( idx<pPare
12acd 6e 74 2d 3e 6e 43 65 6c 6c 0a 20 20 20 20 20 20  nt->nCell.      
12ace 20 20 20 20 20 20 20 7c 7c 20 67 65 74 34 62 79         || get4by
12acf 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 44 61  te(&pParent->aDa
12ad0 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f  ta[pParent->hdrO
12ad1 66 66 73 65 74 2b 38 5d 29 3d 3d 70 67 6e 6f 20  ffset+8])==pgno 
12ad2 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
12ad3 69 64 78 20 3d 20 70 50 61 67 65 2d 3e 69 64 78  idx = pPage->idx
12ad4 50 61 72 65 6e 74 3b 0a 20 20 7d 0a 0a 20 20 2f  Parent;.  }..  /
12ad5 2a 0a 20 20 2a 2a 20 49 6e 69 74 69 61 6c 69 7a  *.  ** Initializ
12ad6 65 20 76 61 72 69 61 62 6c 65 73 20 73 6f 20 74  e variables so t
12ad7 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 73  hat it will be s
12ad8 61 66 65 20 74 6f 20 6a 75 6d 70 0a 20 20 2a 2a  afe to jump.  **
12ad9 20 64 69 72 65 63 74 6c 79 20 74 6f 20 62 61 6c   directly to bal
12ada 61 6e 63 65 5f 63 6c 65 61 6e 75 70 20 61 74 20  ance_cleanup at 
12adb 61 6e 79 20 6d 6f 6d 65 6e 74 2e 0a 20 20 2a 2f  any moment..  */
12adc 0a 20 20 6e 4f 6c 64 20 3d 20 6e 4e 65 77 20 3d  .  nOld = nNew =
12add 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67   0;.  sqlite3Pag
12ade 65 72 52 65 66 28 70 50 61 72 65 6e 74 2d 3e 70  erRef(pParent->p
12adf 44 62 50 61 67 65 29 3b 0a 0a 20 20 2f 2a 0a 20  DbPage);..  /*. 
12ae0 20 2a 2a 20 46 69 6e 64 20 73 69 62 6c 69 6e 67   ** Find sibling
12ae1 20 70 61 67 65 73 20 74 6f 20 70 50 61 67 65 20   pages to pPage 
12ae2 61 6e 64 20 74 68 65 20 63 65 6c 6c 73 20 69 6e  and the cells in
12ae3 20 70 50 61 72 65 6e 74 20 74 68 61 74 20 64 69   pParent that di
12ae4 76 69 64 65 0a 20 20 2a 2a 20 74 68 65 20 73 69  vide.  ** the si
12ae5 62 6c 69 6e 67 73 2e 20 20 41 6e 20 61 74 74 65  blings.  An atte
12ae6 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 66  mpt is made to f
12ae7 69 6e 64 20 4e 4e 20 73 69 62 6c 69 6e 67 73 20  ind NN siblings 
12ae8 6f 6e 20 65 69 74 68 65 72 0a 20 20 2a 2a 20 73  on either.  ** s
12ae9 69 64 65 20 6f 66 20 70 50 61 67 65 2e 20 20 4d  ide of pPage.  M
12aea 6f 72 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65  ore siblings are
12aeb 20 74 61 6b 65 6e 20 66 72 6f 6d 20 6f 6e 65 20   taken from one 
12aec 73 69 64 65 2c 20 68 6f 77 65 76 65 72 2c 20 69  side, however, i
12aed 66 0a 20 20 2a 2a 20 70 50 61 67 65 20 74 68 65  f.  ** pPage the
12aee 72 65 20 61 72 65 20 66 65 77 65 72 20 74 68 61  re are fewer tha
12aef 6e 20 4e 4e 20 73 69 62 6c 69 6e 67 73 20 6f 6e  n NN siblings on
12af0 20 74 68 65 20 6f 74 68 65 72 20 73 69 64 65 2e   the other side.
12af1 20 20 49 66 20 70 50 61 72 65 6e 74 0a 20 20 2a    If pParent.  *
12af2 2a 20 68 61 73 20 4e 42 20 6f 72 20 66 65 77 65  * has NB or fewe
12af3 72 20 63 68 69 6c 64 72 65 6e 20 74 68 65 6e 20  r children then 
12af4 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 6f 66 20  all children of 
12af5 70 50 61 72 65 6e 74 20 61 72 65 20 74 61 6b 65  pParent are take
12af6 6e 2e 0a 20 20 2a 2f 0a 20 20 6e 78 44 69 76 20  n..  */.  nxDiv 
12af7 3d 20 69 64 78 20 2d 20 4e 4e 3b 0a 20 20 69 66  = idx - NN;.  if
12af8 28 20 6e 78 44 69 76 20 2b 20 4e 42 20 3e 20 70  ( nxDiv + NB > p
12af9 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b  Parent->nCell ){
12afa 0a 20 20 20 20 6e 78 44 69 76 20 3d 20 70 50 61  .    nxDiv = pPa
12afb 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 2d 20 4e 42  rent->nCell - NB
12afc 20 2b 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20   + 1;.  }.  if( 
12afd 6e 78 44 69 76 3c 30 20 29 7b 0a 20 20 20 20 6e  nxDiv<0 ){.    n
12afe 78 44 69 76 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  xDiv = 0;.  }.  
12aff 6e 44 69 76 20 3d 20 30 3b 0a 20 20 66 6f 72 28  nDiv = 0;.  for(
12b00 69 3d 30 2c 20 6b 3d 6e 78 44 69 76 3b 20 69 3c  i=0, k=nxDiv; i<
12b01 4e 42 3b 20 69 2b 2b 2c 20 6b 2b 2b 29 7b 0a 20  NB; i++, k++){. 
12b02 20 20 20 69 66 28 20 6b 3c 70 50 61 72 65 6e 74     if( k<pParent
12b03 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20  ->nCell ){.     
12b04 20 61 70 44 69 76 5b 69 5d 20 3d 20 66 69 6e 64   apDiv[i] = find
12b05 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6b 29  Cell(pParent, k)
12b06 3b 0a 20 20 20 20 20 20 6e 44 69 76 2b 2b 3b 0a  ;.      nDiv++;.
12b07 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
12b08 50 61 72 65 6e 74 2d 3e 6c 65 61 66 20 29 3b 0a  Parent->leaf );.
12b09 20 20 20 20 20 20 70 67 6e 6f 4f 6c 64 5b 69 5d        pgnoOld[i]
12b0a 20 3d 20 67 65 74 34 62 79 74 65 28 61 70 44 69   = get4byte(apDi
12b0b 76 5b 69 5d 29 3b 0a 20 20 20 20 7d 65 6c 73 65  v[i]);.    }else
12b0c 20 69 66 28 20 6b 3d 3d 70 50 61 72 65 6e 74 2d   if( k==pParent-
12b0d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20  >nCell ){.      
12b0e 70 67 6e 6f 4f 6c 64 5b 69 5d 20 3d 20 67 65 74  pgnoOld[i] = get
12b0f 34 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d 3e  4byte(&pParent->
12b10 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68  aData[pParent->h
12b11 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20  drOffset+8]);.  
12b12 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 62    }else{.      b
12b13 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
12b14 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50  rc = getAndInitP
12b15 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 4f 6c 64  age(pBt, pgnoOld
12b16 5b 69 5d 2c 20 26 61 70 4f 6c 64 5b 69 5d 2c 20  [i], &apOld[i], 
12b17 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 69 66  pParent);.    if
12b18 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61  ( rc ) goto bala
12b19 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  nce_cleanup;.   
12b1a 20 61 70 4f 6c 64 5b 69 5d 2d 3e 69 64 78 50 61   apOld[i]->idxPa
12b1b 72 65 6e 74 20 3d 20 6b 3b 0a 20 20 20 20 61 70  rent = k;.    ap
12b1c 43 6f 70 79 5b 69 5d 20 3d 20 30 3b 0a 20 20 20  Copy[i] = 0;.   
12b1d 20 61 73 73 65 72 74 28 20 69 3d 3d 6e 4f 6c 64   assert( i==nOld
12b1e 20 29 3b 0a 20 20 20 20 6e 4f 6c 64 2b 2b 3b 0a   );.    nOld++;.
12b1f 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73 20 2b 3d      nMaxCells +=
12b20 20 31 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 43 65   1+apOld[i]->nCe
12b21 6c 6c 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 4f 76  ll+apOld[i]->nOv
12b22 65 72 66 6c 6f 77 3b 0a 20 20 7d 0a 0a 20 20 2f  erflow;.  }..  /
12b23 2a 20 4d 61 6b 65 20 6e 4d 61 78 43 65 6c 6c 73  * Make nMaxCells
12b24 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 34   a multiple of 4
12b25 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 72 65   in order to pre
12b26 73 65 72 76 65 20 38 2d 62 79 74 65 0a 20 20 2a  serve 8-byte.  *
12b27 2a 20 61 6c 69 67 6e 6d 65 6e 74 20 2a 2f 0a 20  * alignment */. 
12b28 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 28 6e 4d   nMaxCells = (nM
12b29 61 78 43 65 6c 6c 73 20 2b 20 33 29 26 7e 33 3b  axCells + 3)&~3;
12b2a 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f  ..  /*.  ** Allo
12b2b 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 6d  cate space for m
12b2c 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 73  emory structures
12b2d 0a 20 20 2a 2f 0a 20 20 61 70 43 65 6c 6c 20 3d  .  */.  apCell =
12b2e 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
12b2f 20 0a 20 20 20 20 20 20 20 6e 4d 61 78 43 65 6c   .       nMaxCel
12b30 6c 73 2a 73 69 7a 65 6f 66 28 75 38 2a 29 20 20  ls*sizeof(u8*)  
12b31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12b32 20 20 20 20 20 2f 2a 20 61 70 43 65 6c 6c 20 2a       /* apCell *
12b33 2f 0a 20 20 20 20 20 2b 20 6e 4d 61 78 43 65 6c  /.     + nMaxCel
12b34 6c 73 2a 73 69 7a 65 6f 66 28 75 31 36 29 20 20  ls*sizeof(u16)  
12b35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12b36 20 20 20 20 20 2f 2a 20 73 7a 43 65 6c 6c 20 2a       /* szCell *
12b37 2f 0a 20 20 20 20 20 2b 20 28 52 4f 55 4e 44 38  /.     + (ROUND8
12b38 28 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29  (sizeof(MemPage)
12b39 29 2b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29  )+pBt->pageSize)
12b3a 2a 4e 42 20 20 2f 2a 20 61 43 6f 70 79 20 2a 2f  *NB  /* aCopy */
12b3b 0a 20 20 20 20 20 2b 20 70 42 74 2d 3e 70 61 67  .     + pBt->pag
12b3c 65 53 69 7a 65 2a 35 20 20 20 20 20 20 20 20 20  eSize*5         
12b3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12b3e 20 20 20 20 2f 2a 20 61 53 70 61 63 65 20 2a 2f      /* aSpace */
12b3f 0a 20 20 20 20 20 2b 20 28 49 53 41 55 54 4f 56  .     + (ISAUTOV
12b40 41 43 55 55 4d 20 3f 20 6e 4d 61 78 43 65 6c 6c  ACUUM ? nMaxCell
12b41 73 20 3a 20 30 29 20 20 20 20 20 20 20 20 20 20  s : 0)          
12b42 20 20 20 20 2f 2a 20 61 46 72 6f 6d 20 2a 2f 0a      /* aFrom */.
12b43 20 20 29 3b 0a 20 20 69 66 28 20 61 70 43 65 6c    );.  if( apCel
12b44 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d  l==0 ){.    rc =
12b45 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
12b46 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f     goto balance_
12b47 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 73  cleanup;.  }.  s
12b48 7a 43 65 6c 6c 20 3d 20 28 75 31 36 2a 29 26 61  zCell = (u16*)&a
12b49 70 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d  pCell[nMaxCells]
12b4a 3b 0a 20 20 61 43 6f 70 79 5b 30 5d 20 3d 20 28  ;.  aCopy[0] = (
12b4b 75 38 2a 29 26 73 7a 43 65 6c 6c 5b 6e 4d 61 78  u8*)&szCell[nMax
12b4c 43 65 6c 6c 73 5d 3b 0a 20 20 61 73 73 65 72 74  Cells];.  assert
12b4d 28 20 28 28 61 43 6f 70 79 5b 30 5d 20 2d 20 28  ( ((aCopy[0] - (
12b4e 75 38 2a 29 61 70 43 65 6c 6c 29 20 26 20 37 29  u8*)apCell) & 7)
12b4f 3d 3d 30 20 29 3b 20 2f 2a 20 38 2d 62 79 74 65  ==0 ); /* 8-byte
12b50 20 61 6c 69 67 6e 6d 65 6e 74 20 72 65 71 75 69   alignment requi
12b51 72 65 64 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 31  red */.  for(i=1
12b52 3b 20 69 3c 4e 42 3b 20 69 2b 2b 29 7b 0a 20 20  ; i<NB; i++){.  
12b53 20 20 61 43 6f 70 79 5b 69 5d 20 3d 20 26 61 43    aCopy[i] = &aC
12b54 6f 70 79 5b 69 2d 31 5d 5b 70 42 74 2d 3e 70 61  opy[i-1][pBt->pa
12b55 67 65 53 69 7a 65 2b 52 4f 55 4e 44 38 28 73 69  geSize+ROUND8(si
12b56 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 29 5d 3b  zeof(MemPage))];
12b57 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 28 61  .    assert( ((a
12b58 43 6f 70 79 5b 69 5d 20 2d 20 28 75 38 2a 29 61  Copy[i] - (u8*)a
12b59 70 43 65 6c 6c 29 20 26 20 37 29 3d 3d 30 20 29  pCell) & 7)==0 )
12b5a 3b 20 2f 2a 20 38 2d 62 79 74 65 20 61 6c 69 67  ; /* 8-byte alig
12b5b 6e 6d 65 6e 74 20 72 65 71 75 69 72 65 64 20 2a  nment required *
12b5c 2f 0a 20 20 7d 0a 20 20 61 53 70 61 63 65 20 3d  /.  }.  aSpace =
12b5d 20 26 61 43 6f 70 79 5b 4e 42 2d 31 5d 5b 70 42   &aCopy[NB-1][pB
12b5e 74 2d 3e 70 61 67 65 53 69 7a 65 2b 52 4f 55 4e  t->pageSize+ROUN
12b5f 44 38 28 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67  D8(sizeof(MemPag
12b60 65 29 29 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  e))];.  assert( 
12b61 28 28 61 53 70 61 63 65 20 2d 20 28 75 38 2a 29  ((aSpace - (u8*)
12b62 61 70 43 65 6c 6c 29 20 26 20 37 29 3d 3d 30 20  apCell) & 7)==0 
12b63 29 3b 20 2f 2a 20 38 2d 62 79 74 65 20 61 6c 69  ); /* 8-byte ali
12b64 67 6e 6d 65 6e 74 20 72 65 71 75 69 72 65 64 20  gnment required 
12b65 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
12b66 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
12b67 4d 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74  M.  if( pBt->aut
12b68 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 61  oVacuum ){.    a
12b69 46 72 6f 6d 20 3d 20 26 61 53 70 61 63 65 5b 35  From = &aSpace[5
12b6a 2a 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 3b  *pBt->pageSize];
12b6b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 0a 20  .  }.#endif.  . 
12b6c 20 2f 2a 0a 20 20 2a 2a 20 4d 61 6b 65 20 63 6f   /*.  ** Make co
12b6d 70 69 65 73 20 6f 66 20 74 68 65 20 63 6f 6e 74  pies of the cont
12b6e 65 6e 74 20 6f 66 20 70 50 61 67 65 20 61 6e 64  ent of pPage and
12b6f 20 69 74 73 20 73 69 62 6c 69 6e 67 73 20 69 6e   its siblings in
12b70 74 6f 20 61 4f 6c 64 5b 5d 2e 0a 20 20 2a 2a 20  to aOld[]..  ** 
12b71 54 68 65 20 72 65 73 74 20 6f 66 20 74 68 69 73  The rest of this
12b72 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 75   function will u
12b73 73 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65  se data from the
12b74 20 63 6f 70 69 65 73 20 72 61 74 68 65 72 0a 20   copies rather. 
12b75 20 2a 2a 20 74 68 61 74 20 74 68 65 20 6f 72 69   ** that the ori
12b76 67 69 6e 61 6c 20 70 61 67 65 73 20 73 69 6e 63  ginal pages sinc
12b77 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70  e the original p
12b78 61 67 65 73 20 77 69 6c 6c 20 62 65 20 69 6e 20  ages will be in 
12b79 74 68 65 0a 20 20 2a 2a 20 70 72 6f 63 65 73 73  the.  ** process
12b7a 20 6f 66 20 62 65 69 6e 67 20 6f 76 65 72 77 72   of being overwr
12b7b 69 74 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20 66 6f  itten..  */.  fo
12b7c 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69  r(i=0; i<nOld; i
12b7d 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65  ++){.    MemPage
12b7e 20 2a 70 20 3d 20 61 70 43 6f 70 79 5b 69 5d 20   *p = apCopy[i] 
12b7f 3d 20 28 4d 65 6d 50 61 67 65 2a 29 61 43 6f 70  = (MemPage*)aCop
12b80 79 5b 69 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79  y[i];.    memcpy
12b81 28 70 2c 20 61 70 4f 6c 64 5b 69 5d 2c 20 73 69  (p, apOld[i], si
12b82 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 29 3b 0a  zeof(MemPage));.
12b83 20 20 20 20 70 2d 3e 61 44 61 74 61 20 3d 20 28      p->aData = (
12b84 76 6f 69 64 2a 29 26 70 5b 31 5d 3b 0a 20 20 20  void*)&p[1];.   
12b85 20 6d 65 6d 63 70 79 28 70 2d 3e 61 44 61 74 61   memcpy(p->aData
12b86 2c 20 61 70 4f 6c 64 5b 69 5d 2d 3e 61 44 61 74  , apOld[i]->aDat
12b87 61 2c 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  a, pBt->pageSize
12b88 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a  );.  }..  /*.  *
12b89 2a 20 4c 6f 61 64 20 70 6f 69 6e 74 65 72 73 20  * Load pointers 
12b8a 74 6f 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20  to all cells on 
12b8b 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61 6e  sibling pages an
12b8c 64 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65  d the divider ce
12b8d 6c 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68  lls.  ** into th
12b8e 65 20 6c 6f 63 61 6c 20 61 70 43 65 6c 6c 5b 5d  e local apCell[]
12b8f 20 61 72 72 61 79 2e 20 20 4d 61 6b 65 20 63 6f   array.  Make co
12b90 70 69 65 73 20 6f 66 20 74 68 65 20 64 69 76 69  pies of the divi
12b91 64 65 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 69  der cells.  ** i
12b92 6e 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e  nto space obtain
12b93 65 64 20 66 6f 72 6d 20 61 53 70 61 63 65 5b 5d  ed form aSpace[]
12b94 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 65 20   and remove the 
12b95 74 68 65 20 64 69 76 69 64 65 72 20 43 65 6c 6c  the divider Cell
12b96 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 70 50 61 72  s.  ** from pPar
12b97 65 6e 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  ent..  **.  ** I
12b98 66 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20 61  f the siblings a
12b99 72 65 20 6f 6e 20 6c 65 61 66 20 70 61 67 65 73  re on leaf pages
12b9a 2c 20 74 68 65 6e 20 74 68 65 20 63 68 69 6c 64  , then the child
12b9b 20 70 6f 69 6e 74 65 72 73 20 6f 66 20 74 68 65   pointers of the
12b9c 0a 20 20 2a 2a 20 64 69 76 69 64 65 72 20 63 65  .  ** divider ce
12b9d 6c 6c 73 20 61 72 65 20 73 74 72 69 70 70 65 64  lls are stripped
12b9e 20 66 72 6f 6d 20 74 68 65 20 63 65 6c 6c 73 20   from the cells 
12b9f 62 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 20  before they are 
12ba0 63 6f 70 69 65 64 0a 20 20 2a 2a 20 69 6e 74 6f  copied.  ** into
12ba1 20 61 53 70 61 63 65 5b 5d 2e 20 20 49 6e 20 74   aSpace[].  In t
12ba2 68 69 73 20 77 61 79 2c 20 61 6c 6c 20 63 65 6c  his way, all cel
12ba3 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61  ls in apCell[] a
12ba4 72 65 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a 20  re without.  ** 
12ba5 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 2e 20  child pointers. 
12ba6 20 49 66 20 73 69 62 6c 69 6e 67 73 20 61 72 65   If siblings are
12ba7 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20 74 68 65   not leaves, the
12ba8 6e 20 61 6c 6c 20 63 65 6c 6c 20 69 6e 0a 20 20  n all cell in.  
12ba9 2a 2a 20 61 70 43 65 6c 6c 5b 5d 20 69 6e 63 6c  ** apCell[] incl
12baa 75 64 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 65  ude child pointe
12bab 72 73 2e 20 20 45 69 74 68 65 72 20 77 61 79 2c  rs.  Either way,
12bac 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70   all cells in ap
12bad 43 65 6c 6c 5b 5d 0a 20 20 2a 2a 20 61 72 65 20  Cell[].  ** are 
12bae 61 6c 69 6b 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  alike..  **.  **
12baf 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3a   leafCorrection:
12bb0 20 20 34 20 69 66 20 70 50 61 67 65 20 69 73 20    4 if pPage is 
12bb1 61 20 6c 65 61 66 2e 20 20 30 20 69 66 20 70 50  a leaf.  0 if pP
12bb2 61 67 65 20 69 73 20 6e 6f 74 20 61 20 6c 65 61  age is not a lea
12bb3 66 2e 0a 20 20 2a 2a 20 20 20 20 20 20 20 6c 65  f..  **       le
12bb4 61 66 44 61 74 61 3a 20 20 31 20 69 66 20 70 50  afData:  1 if pP
12bb5 61 67 65 20 68 6f 6c 64 73 20 6b 65 79 2b 64 61  age holds key+da
12bb6 74 61 20 61 6e 64 20 70 50 61 72 65 6e 74 20 68  ta and pParent h
12bb7 6f 6c 64 73 20 6f 6e 6c 79 20 6b 65 79 73 2e 0a  olds only keys..
12bb8 20 20 2a 2f 0a 20 20 6e 43 65 6c 6c 20 3d 20 30    */.  nCell = 0
12bb9 3b 0a 20 20 6c 65 61 66 43 6f 72 72 65 63 74 69  ;.  leafCorrecti
12bba 6f 6e 20 3d 20 70 50 61 67 65 2d 3e 6c 65 61 66  on = pPage->leaf
12bbb 2a 34 3b 0a 20 20 6c 65 61 66 44 61 74 61 20 3d  *4;.  leafData =
12bbc 20 70 50 61 67 65 2d 3e 6c 65 61 66 44 61 74 61   pPage->leafData
12bbd 20 26 26 20 70 50 61 67 65 2d 3e 6c 65 61 66 3b   && pPage->leaf;
12bbe 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f  .  for(i=0; i<nO
12bbf 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4d 65  ld; i++){.    Me
12bc0 6d 50 61 67 65 20 2a 70 4f 6c 64 20 3d 20 61 70  mPage *pOld = ap
12bc1 43 6f 70 79 5b 69 5d 3b 0a 20 20 20 20 69 6e 74  Copy[i];.    int
12bc2 20 6c 69 6d 69 74 20 3d 20 70 4f 6c 64 2d 3e 6e   limit = pOld->n
12bc3 43 65 6c 6c 2b 70 4f 6c 64 2d 3e 6e 4f 76 65 72  Cell+pOld->nOver
12bc4 66 6c 6f 77 3b 0a 20 20 20 20 66 6f 72 28 6a 3d  flow;.    for(j=
12bc5 30 3b 20 6a 3c 6c 69 6d 69 74 3b 20 6a 2b 2b 29  0; j<limit; j++)
12bc6 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
12bc7 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 73 20  nCell<nMaxCells 
12bc8 29 3b 0a 20 20 20 20 20 20 61 70 43 65 6c 6c 5b  );.      apCell[
12bc9 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e 64 4f 76 65  nCell] = findOve
12bca 72 66 6c 6f 77 43 65 6c 6c 28 70 4f 6c 64 2c 20  rflowCell(pOld, 
12bcb 6a 29 3b 0a 20 20 20 20 20 20 73 7a 43 65 6c 6c  j);.      szCell
12bcc 5b 6e 43 65 6c 6c 5d 20 3d 20 63 65 6c 6c 53 69  [nCell] = cellSi
12bcd 7a 65 50 74 72 28 70 4f 6c 64 2c 20 61 70 43 65  zePtr(pOld, apCe
12bce 6c 6c 5b 6e 43 65 6c 6c 5d 29 3b 0a 23 69 66 6e  ll[nCell]);.#ifn
12bcf 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
12bd0 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20  AUTOVACUUM.     
12bd1 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61   if( pBt->autoVa
12bd2 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  cuum ){.        
12bd3 69 6e 74 20 61 3b 0a 20 20 20 20 20 20 20 20 61  int a;.        a
12bd4 46 72 6f 6d 5b 6e 43 65 6c 6c 5d 20 3d 20 69 3b  From[nCell] = i;
12bd5 0a 20 20 20 20 20 20 20 20 66 6f 72 28 61 3d 30  .        for(a=0
12bd6 3b 20 61 3c 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66  ; a<pOld->nOverf
12bd7 6c 6f 77 3b 20 61 2b 2b 29 7b 0a 20 20 20 20 20  low; a++){.     
12bd8 20 20 20 20 20 69 66 28 20 70 4f 6c 64 2d 3e 61       if( pOld->a
12bd9 4f 76 66 6c 5b 61 5d 2e 70 43 65 6c 6c 3d 3d 61  Ovfl[a].pCell==a
12bda 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 29 7b 0a  pCell[nCell] ){.
12bdb 20 20 20 20 20 20 20 20 20 20 20 20 61 46 72 6f              aFro
12bdc 6d 5b 6e 43 65 6c 6c 5d 20 3d 20 30 78 46 46 3b  m[nCell] = 0xFF;
12bdd 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
12bde 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ak;.          }.
12bdf 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
12be0 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 6e  }.#endif.      n
12be1 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  Cell++;.    }.  
12be2 20 20 69 66 28 20 69 3c 6e 4f 6c 64 2d 31 20 29    if( i<nOld-1 )
12be3 7b 0a 20 20 20 20 20 20 75 31 36 20 73 7a 20 3d  {.      u16 sz =
12be4 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61   cellSizePtr(pPa
12be5 72 65 6e 74 2c 20 61 70 44 69 76 5b 69 5d 29 3b  rent, apDiv[i]);
12be6 0a 20 20 20 20 20 20 69 66 28 20 6c 65 61 66 44  .      if( leafD
12be7 61 74 61 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ata ){.        /
12be8 2a 20 57 69 74 68 20 74 68 65 20 4c 45 41 46 44  * With the LEAFD
12be9 41 54 41 20 66 6c 61 67 2c 20 70 50 61 72 65 6e  ATA flag, pParen
12bea 74 20 63 65 6c 6c 73 20 68 6f 6c 64 20 6f 6e 6c  t cells hold onl
12beb 79 20 49 4e 54 4b 45 59 73 20 74 68 61 74 0a 20  y INTKEYs that. 
12bec 20 20 20 20 20 20 20 2a 2a 20 61 72 65 20 64 75         ** are du
12bed 70 6c 69 63 61 74 65 73 20 6f 66 20 6b 65 79 73  plicates of keys
12bee 20 6f 6e 20 74 68 65 20 63 68 69 6c 64 20 70 61   on the child pa
12bef 67 65 73 2e 20 20 57 65 20 6e 65 65 64 20 74 6f  ges.  We need to
12bf0 20 72 65 6d 6f 76 65 0a 20 20 20 20 20 20 20 20   remove.        
12bf1 2a 2a 20 74 68 65 20 64 69 76 69 64 65 72 20 63  ** the divider c
12bf2 65 6c 6c 73 20 66 72 6f 6d 20 70 50 61 72 65 6e  ells from pParen
12bf3 74 2c 20 62 75 74 20 74 68 65 20 64 69 76 69 64  t, but the divid
12bf4 65 72 73 20 63 65 6c 6c 73 20 61 72 65 20 6e 6f  ers cells are no
12bf5 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 64 64  t.        ** add
12bf6 65 64 20 74 6f 20 61 70 43 65 6c 6c 5b 5d 20 62  ed to apCell[] b
12bf7 65 63 61 75 73 65 20 74 68 65 79 20 61 72 65 20  ecause they are 
12bf8 64 75 70 6c 69 63 61 74 65 73 20 6f 66 20 63 68  duplicates of ch
12bf9 69 6c 64 20 63 65 6c 6c 73 2e 0a 20 20 20 20 20  ild cells..     
12bfa 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 64 72     */.        dr
12bfb 6f 70 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20  opCell(pParent, 
12bfc 6e 78 44 69 76 2c 20 73 7a 29 3b 0a 20 20 20 20  nxDiv, sz);.    
12bfd 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
12bfe 20 75 38 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20   u8 *pTemp;.    
12bff 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c      assert( nCel
12c00 6c 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20  l<nMaxCells );. 
12c01 20 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43         szCell[nC
12c02 65 6c 6c 5d 20 3d 20 73 7a 3b 0a 20 20 20 20 20  ell] = sz;.     
12c03 20 20 20 70 54 65 6d 70 20 3d 20 26 61 53 70 61     pTemp = &aSpa
12c04 63 65 5b 69 53 70 61 63 65 5d 3b 0a 20 20 20 20  ce[iSpace];.    
12c05 20 20 20 20 69 53 70 61 63 65 20 2b 3d 20 73 7a      iSpace += sz
12c06 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
12c07 28 20 69 53 70 61 63 65 3c 3d 70 42 74 2d 3e 70  ( iSpace<=pBt->p
12c08 61 67 65 53 69 7a 65 2a 35 20 29 3b 0a 20 20 20  ageSize*5 );.   
12c09 20 20 20 20 20 6d 65 6d 63 70 79 28 70 54 65 6d       memcpy(pTem
12c0a 70 2c 20 61 70 44 69 76 5b 69 5d 2c 20 73 7a 29  p, apDiv[i], sz)
12c0b 3b 0a 20 20 20 20 20 20 20 20 61 70 43 65 6c 6c  ;.        apCell
12c0c 5b 6e 43 65 6c 6c 5d 20 3d 20 70 54 65 6d 70 2b  [nCell] = pTemp+
12c0d 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a  leafCorrection;.
12c0e 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
12c0f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
12c10 20 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e         if( pBt->
12c11 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20  autoVacuum ){.  
12c12 20 20 20 20 20 20 20 20 61 46 72 6f 6d 5b 6e 43          aFrom[nC
12c13 65 6c 6c 5d 20 3d 20 30 78 46 46 3b 0a 20 20 20  ell] = 0xFF;.   
12c14 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
12c15 20 20 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70        dropCell(p
12c16 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20 73  Parent, nxDiv, s
12c17 7a 29 3b 0a 20 20 20 20 20 20 20 20 73 7a 43 65  z);.        szCe
12c18 6c 6c 5b 6e 43 65 6c 6c 5d 20 2d 3d 20 6c 65 61  ll[nCell] -= lea
12c19 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a 20 20 20  fCorrection;.   
12c1a 20 20 20 20 20 61 73 73 65 72 74 28 20 67 65 74       assert( get
12c1b 34 62 79 74 65 28 70 54 65 6d 70 29 3d 3d 70 67  4byte(pTemp)==pg
12c1c 6e 6f 4f 6c 64 5b 69 5d 20 29 3b 0a 20 20 20 20  noOld[i] );.    
12c1d 20 20 20 20 69 66 28 20 21 70 4f 6c 64 2d 3e 6c      if( !pOld->l
12c1e 65 61 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20  eaf ){.         
12c1f 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 6f 72   assert( leafCor
12c20 72 65 63 74 69 6f 6e 3d 3d 30 20 29 3b 0a 20 20  rection==0 );.  
12c21 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
12c22 69 67 68 74 20 70 6f 69 6e 74 65 72 20 6f 66 20  ight pointer of 
12c23 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 70  the child page p
12c24 4f 6c 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20  Old becomes the 
12c25 6c 65 66 74 0a 20 20 20 20 20 20 20 20 20 20 2a  left.          *
12c26 2a 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65  * pointer of the
12c27 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 2a 2f   divider cell */
12c28 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70  .          memcp
12c29 79 28 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c  y(apCell[nCell],
12c2a 20 26 70 4f 6c 64 2d 3e 61 44 61 74 61 5b 70 4f   &pOld->aData[pO
12c2b 6c 64 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  ld->hdrOffset+8]
12c2c 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  , 4);.        }e
12c2d 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61  lse{.          a
12c2e 73 73 65 72 74 28 20 6c 65 61 66 43 6f 72 72 65  ssert( leafCorre
12c2f 63 74 69 6f 6e 3d 3d 34 20 29 3b 0a 20 20 20 20  ction==4 );.    
12c30 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c        if( szCell
12c31 5b 6e 43 65 6c 6c 5d 3c 34 20 29 7b 0a 20 20 20  [nCell]<4 ){.   
12c32 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e           /* Do n
12c33 6f 74 20 61 6c 6c 6f 77 20 61 6e 79 20 63 65 6c  ot allow any cel
12c34 6c 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20  ls smaller than 
12c35 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 20 20  4 bytes. */.    
12c36 20 20 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e          szCell[n
12c37 43 65 6c 6c 5d 20 3d 20 34 3b 0a 20 20 20 20 20  Cell] = 4;.     
12c38 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
12c39 0a 20 20 20 20 20 20 20 20 6e 43 65 6c 6c 2b 2b  .        nCell++
12c3a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
12c3b 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46    }..  /*.  ** F
12c3c 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 6e 75  igure out the nu
12c3d 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6e 65  mber of pages ne
12c3e 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 61 6c 6c  eded to hold all
12c3f 20 6e 43 65 6c 6c 20 63 65 6c 6c 73 2e 0a 20 20   nCell cells..  
12c40 2a 2a 20 53 74 6f 72 65 20 74 68 69 73 20 6e 75  ** Store this nu
12c41 6d 62 65 72 20 69 6e 20 22 6b 22 2e 20 20 41 6c  mber in "k".  Al
12c42 73 6f 20 63 6f 6d 70 75 74 65 20 73 7a 4e 65 77  so compute szNew
12c43 5b 5d 20 77 68 69 63 68 20 69 73 20 74 68 65 20  [] which is the 
12c44 74 6f 74 61 6c 0a 20 20 2a 2a 20 73 69 7a 65 20  total.  ** size 
12c45 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20  of all cells on 
12c46 74 68 65 20 69 2d 74 68 20 70 61 67 65 20 61 6e  the i-th page an
12c47 64 20 63 6e 74 4e 65 77 5b 5d 20 77 68 69 63 68  d cntNew[] which
12c48 20 69 73 20 74 68 65 20 69 6e 64 65 78 0a 20 20   is the index.  
12c49 2a 2a 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 6f  ** in apCell[] o
12c4a 66 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20  f the cell that 
12c4b 64 69 76 69 64 65 73 20 70 61 67 65 20 69 20 66  divides page i f
12c4c 72 6f 6d 20 70 61 67 65 20 69 2b 31 2e 20 20 0a  rom page i+1.  .
12c4d 20 20 2a 2a 20 63 6e 74 4e 65 77 5b 6b 5d 20 73    ** cntNew[k] s
12c4e 68 6f 75 6c 64 20 65 71 75 61 6c 20 6e 43 65 6c  hould equal nCel
12c4f 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 6c  l..  **.  ** Val
12c50 75 65 73 20 63 6f 6d 70 75 74 65 64 20 62 79 20  ues computed by 
12c51 74 68 69 73 20 62 6c 6f 63 6b 3a 0a 20 20 2a 2a  this block:.  **
12c52 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20  .  **           
12c53 6b 3a 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d  k: The total num
12c54 62 65 72 20 6f 66 20 73 69 62 6c 69 6e 67 20 70  ber of sibling p
12c55 61 67 65 73 0a 20 20 2a 2a 20 20 20 20 73 7a 4e  ages.  **    szN
12c56 65 77 5b 69 5d 3a 20 53 70 61 63 65 64 20 75 73  ew[i]: Spaced us
12c57 65 64 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 73  ed on the i-th s
12c58 69 62 6c 69 6e 67 20 70 61 67 65 2e 0a 20 20 2a  ibling page..  *
12c59 2a 20 20 20 63 6e 74 4e 65 77 5b 69 5d 3a 20 49  *   cntNew[i]: I
12c5a 6e 64 65 78 20 69 6e 20 61 70 43 65 6c 6c 5b 5d  ndex in apCell[]
12c5b 20 61 6e 64 20 73 7a 43 65 6c 6c 5b 5d 20 66 6f   and szCell[] fo
12c5c 72 20 74 68 65 20 66 69 72 73 74 20 63 65 6c 6c  r the first cell
12c5d 20 74 6f 0a 20 20 2a 2a 20 20 20 20 20 20 20 20   to.  **        
12c5e 20 20 20 20 20 20 74 68 65 20 72 69 67 68 74 20        the right 
12c5f 6f 66 20 74 68 65 20 69 2d 74 68 20 73 69 62 6c  of the i-th sibl
12c60 69 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a 20 75  ing page..  ** u
12c61 73 61 62 6c 65 53 70 61 63 65 3a 20 4e 75 6d 62  sableSpace: Numb
12c62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73  er of bytes of s
12c63 70 61 63 65 20 61 76 61 69 6c 61 62 6c 65 20 6f  pace available o
12c64 6e 20 65 61 63 68 20 73 69 62 6c 69 6e 67 2e 0a  n each sibling..
12c65 20 20 2a 2a 20 0a 20 20 2a 2f 0a 20 20 75 73 61    ** .  */.  usa
12c66 62 6c 65 53 70 61 63 65 20 3d 20 70 42 74 2d 3e  bleSpace = pBt->
12c67 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 31 32 20  usableSize - 12 
12c68 2b 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e  + leafCorrection
12c69 3b 0a 20 20 66 6f 72 28 73 75 62 74 6f 74 61 6c  ;.  for(subtotal
12c6a 3d 6b 3d 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b  =k=i=0; i<nCell;
12c6b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72   i++){.    asser
12c6c 74 28 20 69 3c 6e 4d 61 78 43 65 6c 6c 73 20 29  t( i<nMaxCells )
12c6d 3b 0a 20 20 20 20 73 75 62 74 6f 74 61 6c 20 2b  ;.    subtotal +
12c6e 3d 20 73 7a 43 65 6c 6c 5b 69 5d 20 2b 20 32 3b  = szCell[i] + 2;
12c6f 0a 20 20 20 20 69 66 28 20 73 75 62 74 6f 74 61  .    if( subtota
12c70 6c 20 3e 20 75 73 61 62 6c 65 53 70 61 63 65 20  l > usableSpace 
12c71 29 7b 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b 6b  ){.      szNew[k
12c72 5d 20 3d 20 73 75 62 74 6f 74 61 6c 20 2d 20 73  ] = subtotal - s
12c73 7a 43 65 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20  zCell[i];.      
12c74 63 6e 74 4e 65 77 5b 6b 5d 20 3d 20 69 3b 0a 20  cntNew[k] = i;. 
12c75 20 20 20 20 20 69 66 28 20 6c 65 61 66 44 61 74       if( leafDat
12c76 61 20 29 7b 20 69 2d 2d 3b 20 7d 0a 20 20 20 20  a ){ i--; }.    
12c77 20 20 73 75 62 74 6f 74 61 6c 20 3d 20 30 3b 0a    subtotal = 0;.
12c78 20 20 20 20 20 20 6b 2b 2b 3b 0a 20 20 20 20 7d        k++;.    }
12c79 0a 20 20 7d 0a 20 20 73 7a 4e 65 77 5b 6b 5d 20  .  }.  szNew[k] 
12c7a 3d 20 73 75 62 74 6f 74 61 6c 3b 0a 20 20 63 6e  = subtotal;.  cn
12c7b 74 4e 65 77 5b 6b 5d 20 3d 20 6e 43 65 6c 6c 3b  tNew[k] = nCell;
12c7c 0a 20 20 6b 2b 2b 3b 0a 0a 20 20 2f 2a 0a 20 20  .  k++;..  /*.  
12c7d 2a 2a 20 54 68 65 20 70 61 63 6b 69 6e 67 20 63  ** The packing c
12c7e 6f 6d 70 75 74 65 64 20 62 79 20 74 68 65 20 70  omputed by the p
12c7f 72 65 76 69 6f 75 73 20 62 6c 6f 63 6b 20 69 73  revious block is
12c80 20 62 69 61 73 65 64 20 74 6f 77 61 72 64 20 74   biased toward t
12c81 68 65 20 73 69 62 6c 69 6e 67 73 0a 20 20 2a 2a  he siblings.  **
12c82 20 6f 6e 20 74 68 65 20 6c 65 66 74 20 73 69 64   on the left sid
12c83 65 2e 20 20 54 68 65 20 6c 65 66 74 20 73 69 62  e.  The left sib
12c84 6c 69 6e 67 73 20 61 72 65 20 61 6c 77 61 79 73  lings are always
12c85 20 6e 65 61 72 6c 79 20 66 75 6c 6c 2c 20 77 68   nearly full, wh
12c86 69 6c 65 20 74 68 65 0a 20 20 2a 2a 20 72 69 67  ile the.  ** rig
12c87 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e 67 20  ht-most sibling 
12c88 6d 69 67 68 74 20 62 65 20 6e 65 61 72 6c 79 20  might be nearly 
12c89 65 6d 70 74 79 2e 20 20 54 68 69 73 20 62 6c 6f  empty.  This blo
12c8a 63 6b 20 6f 66 20 63 6f 64 65 20 61 74 74 65 6d  ck of code attem
12c8b 70 74 73 0a 20 20 2a 2a 20 74 6f 20 61 64 6a 75  pts.  ** to adju
12c8c 73 74 20 74 68 65 20 70 61 63 6b 69 6e 67 20 6f  st the packing o
12c8d 66 20 73 69 62 6c 69 6e 67 73 20 74 6f 20 67 65  f siblings to ge
12c8e 74 20 61 20 62 65 74 74 65 72 20 62 61 6c 61 6e  t a better balan
12c8f 63 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  ce..  **.  ** Th
12c90 69 73 20 61 64 6a 75 73 74 6d 65 6e 74 20 69 73  is adjustment is
12c91 20 6d 6f 72 65 20 74 68 61 6e 20 61 6e 20 6f 70   more than an op
12c92 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65  timization.  The
12c93 20 70 61 63 6b 69 6e 67 20 61 62 6f 76 65 20 6d   packing above m
12c94 69 67 68 74 0a 20 20 2a 2a 20 62 65 20 73 6f 20  ight.  ** be so 
12c95 6f 75 74 20 6f 66 20 62 61 6c 61 6e 63 65 20 61  out of balance a
12c96 73 20 74 6f 20 62 65 20 69 6c 6c 65 67 61 6c 2e  s to be illegal.
12c97 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74    For example, t
12c98 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 0a 20 20  he right-most.  
12c99 2a 2a 20 73 69 62 6c 69 6e 67 20 6d 69 67 68 74  ** sibling might
12c9a 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65   be completely e
12c9b 6d 70 74 79 2e 20 20 54 68 69 73 20 61 64 6a 75  mpty.  This adju
12c9c 73 74 6d 65 6e 74 20 69 73 20 6e 6f 74 20 6f 70  stment is not op
12c9d 74 69 6f 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 66  tional..  */.  f
12c9e 6f 72 28 69 3d 6b 2d 31 3b 20 69 3e 30 3b 20 69  or(i=k-1; i>0; i
12c9f 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 73 7a 52  --){.    int szR
12ca0 69 67 68 74 20 3d 20 73 7a 4e 65 77 5b 69 5d 3b  ight = szNew[i];
12ca1 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69 62    /* Size of sib
12ca2 6c 69 6e 67 20 6f 6e 20 74 68 65 20 72 69 67 68  ling on the righ
12ca3 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 4c  t */.    int szL
12ca4 65 66 74 20 3d 20 73 7a 4e 65 77 5b 69 2d 31 5d  eft = szNew[i-1]
12ca5 3b 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69 62  ; /* Size of sib
12ca6 6c 69 6e 67 20 6f 6e 20 74 68 65 20 6c 65 66 74  ling on the left
12ca7 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 3b 20 20   */.    int r;  
12ca8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
12ca9 6e 64 65 78 20 6f 66 20 72 69 67 68 74 2d 6d 6f  ndex of right-mo
12caa 73 74 20 63 65 6c 6c 20 69 6e 20 6c 65 66 74 20  st cell in left 
12cab 73 69 62 6c 69 6e 67 20 2a 2f 0a 20 20 20 20 69  sibling */.    i
12cac 6e 74 20 64 3b 20 20 20 20 20 20 20 20 20 20 20  nt d;           
12cad 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66     /* Index of f
12cae 69 72 73 74 20 63 65 6c 6c 20 74 6f 20 74 68 65  irst cell to the
12caf 20 6c 65 66 74 20 6f 66 20 72 69 67 68 74 20 73   left of right s
12cb0 69 62 6c 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 72  ibling */..    r
12cb1 20 3d 20 63 6e 74 4e 65 77 5b 69 2d 31 5d 20 2d   = cntNew[i-1] -
12cb2 20 31 3b 0a 20 20 20 20 64 20 3d 20 72 20 2b 20   1;.    d = r + 
12cb3 31 20 2d 20 6c 65 61 66 44 61 74 61 3b 0a 20 20  1 - leafData;.  
12cb4 20 20 61 73 73 65 72 74 28 20 64 3c 6e 4d 61 78    assert( d<nMax
12cb5 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 61 73 73  Cells );.    ass
12cb6 65 72 74 28 20 72 3c 6e 4d 61 78 43 65 6c 6c 73  ert( r<nMaxCells
12cb7 20 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73   );.    while( s
12cb8 7a 52 69 67 68 74 3d 3d 30 20 7c 7c 20 73 7a 52  zRight==0 || szR
12cb9 69 67 68 74 2b 73 7a 43 65 6c 6c 5b 64 5d 2b 32  ight+szCell[d]+2
12cba 3c 3d 73 7a 4c 65 66 74 2d 28 73 7a 43 65 6c 6c  <=szLeft-(szCell
12cbb 5b 72 5d 2b 32 29 20 29 7b 0a 20 20 20 20 20 20  [r]+2) ){.      
12cbc 73 7a 52 69 67 68 74 20 2b 3d 20 73 7a 43 65 6c  szRight += szCel
12cbd 6c 5b 64 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20  l[d] + 2;.      
12cbe 73 7a 4c 65 66 74 20 2d 3d 20 73 7a 43 65 6c 6c  szLeft -= szCell
12cbf 5b 72 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20 63  [r] + 2;.      c
12cc0 6e 74 4e 65 77 5b 69 2d 31 5d 2d 2d 3b 0a 20 20  ntNew[i-1]--;.  
12cc1 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b 69      r = cntNew[i
12cc2 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20 64  -1] - 1;.      d
12cc3 20 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 44   = r + 1 - leafD
12cc4 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ata;.    }.    s
12cc5 7a 4e 65 77 5b 69 5d 20 3d 20 73 7a 52 69 67 68  zNew[i] = szRigh
12cc6 74 3b 0a 20 20 20 20 73 7a 4e 65 77 5b 69 2d 31  t;.    szNew[i-1
12cc7 5d 20 3d 20 73 7a 4c 65 66 74 3b 0a 20 20 7d 0a  ] = szLeft;.  }.
12cc8 0a 20 20 2f 2a 20 45 69 74 68 65 72 20 77 65 20  .  /* Either we 
12cc9 66 6f 75 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f 72  found one or mor
12cca 65 20 63 65 6c 6c 73 20 28 63 6e 74 6e 65 77 5b  e cells (cntnew[
12ccb 30 5d 29 3e 30 29 20 6f 72 20 77 65 20 61 72 65  0])>0) or we are
12ccc 20 74 68 65 0a 20 20 2a 2a 20 61 20 76 69 72 74   the.  ** a virt
12ccd 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 2e 20 20  ual root page.  
12cce 41 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70  A virtual root p
12ccf 61 67 65 20 69 73 20 77 68 65 6e 20 74 68 65 20  age is when the 
12cd0 72 65 61 6c 20 72 6f 6f 74 0a 20 20 2a 2a 20 70  real root.  ** p
12cd1 61 67 65 20 69 73 20 70 61 67 65 20 31 20 61 6e  age is page 1 an
12cd2 64 20 77 65 20 61 72 65 20 74 68 65 20 6f 6e 6c  d we are the onl
12cd3 79 20 63 68 69 6c 64 20 6f 66 20 74 68 61 74 20  y child of that 
12cd4 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  page..  */.  ass
12cd5 65 72 74 28 20 63 6e 74 4e 65 77 5b 30 5d 3e 30  ert( cntNew[0]>0
12cd6 20 7c 7c 20 28 70 50 61 72 65 6e 74 2d 3e 70 67   || (pParent->pg
12cd7 6e 6f 3d 3d 31 20 26 26 20 70 50 61 72 65 6e 74  no==1 && pParent
12cd8 2d 3e 6e 43 65 6c 6c 3d 3d 30 29 20 29 3b 0a 0a  ->nCell==0) );..
12cd9 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61    /*.  ** Alloca
12cda 74 65 20 6b 20 6e 65 77 20 70 61 67 65 73 2e 20  te k new pages. 
12cdb 20 52 65 75 73 65 20 6f 6c 64 20 70 61 67 65 73   Reuse old pages
12cdc 20 77 68 65 72 65 20 70 6f 73 73 69 62 6c 65 2e   where possible.
12cdd 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
12cde 70 50 61 67 65 2d 3e 70 67 6e 6f 3e 31 20 29 3b  pPage->pgno>1 );
12cdf 0a 20 20 70 61 67 65 46 6c 61 67 73 20 3d 20 70  .  pageFlags = p
12ce0 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 3b 0a  Page->aData[0];.
12ce1 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6b 3b 20    for(i=0; i<k; 
12ce2 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67  i++){.    MemPag
12ce3 65 20 2a 70 4e 65 77 3b 0a 20 20 20 20 69 66 28  e *pNew;.    if(
12ce4 20 69 3c 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 20   i<nOld ){.     
12ce5 20 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b 69 5d   pNew = apNew[i]
12ce6 20 3d 20 61 70 4f 6c 64 5b 69 5d 3b 0a 20 20 20   = apOld[i];.   
12ce7 20 20 20 70 67 6e 6f 4e 65 77 5b 69 5d 20 3d 20     pgnoNew[i] = 
12ce8 70 67 6e 6f 4f 6c 64 5b 69 5d 3b 0a 20 20 20 20  pgnoOld[i];.    
12ce9 20 20 61 70 4f 6c 64 5b 69 5d 20 3d 20 30 3b 0a    apOld[i] = 0;.
12cea 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
12ceb 65 33 50 61 67 65 72 57 72 69 74 65 28 70 4e 65  e3PagerWrite(pNe
12cec 77 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  w->pDbPage);.   
12ced 20 20 20 6e 4e 65 77 2b 2b 3b 0a 20 20 20 20 20     nNew++;.     
12cee 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62   if( rc ) goto b
12cef 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a  alance_cleanup;.
12cf0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
12cf1 20 61 73 73 65 72 74 28 20 69 3e 30 20 29 3b 0a   assert( i>0 );.
12cf2 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63        rc = alloc
12cf3 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74  ateBtreePage(pBt
12cf4 2c 20 26 70 4e 65 77 2c 20 26 70 67 6e 6f 4e 65  , &pNew, &pgnoNe
12cf5 77 5b 69 5d 2c 20 70 67 6e 6f 4e 65 77 5b 69 2d  w[i], pgnoNew[i-
12cf6 31 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66  1], 0);.      if
12cf7 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61  ( rc ) goto bala
12cf8 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  nce_cleanup;.   
12cf9 20 20 20 61 70 4e 65 77 5b 69 5d 20 3d 20 70 4e     apNew[i] = pN
12cfa 65 77 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b  ew;.      nNew++
12cfb 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 65 72 6f  ;.    }.    zero
12cfc 50 61 67 65 28 70 4e 65 77 2c 20 70 61 67 65 46  Page(pNew, pageF
12cfd 6c 61 67 73 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  lags);.  }..  /*
12cfe 20 46 72 65 65 20 61 6e 79 20 6f 6c 64 20 70 61   Free any old pa
12cff 67 65 73 20 74 68 61 74 20 77 65 72 65 20 6e 6f  ges that were no
12d00 74 20 72 65 75 73 65 64 20 61 73 20 6e 65 77 20  t reused as new 
12d01 70 61 67 65 73 2e 0a 20 20 2a 2f 0a 20 20 77 68  pages..  */.  wh
12d02 69 6c 65 28 20 69 3c 6e 4f 6c 64 20 29 7b 0a 20  ile( i<nOld ){. 
12d03 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 65     rc = freePage
12d04 28 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 20 20  (apOld[i]);.    
12d05 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61  if( rc ) goto ba
12d06 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  lance_cleanup;. 
12d07 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 61     releasePage(a
12d08 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 20 20 61 70  pOld[i]);.    ap
12d09 4f 6c 64 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20  Old[i] = 0;.    
12d0a 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20  i++;.  }..  /*. 
12d0b 20 2a 2a 20 50 75 74 20 74 68 65 20 6e 65 77 20   ** Put the new 
12d0c 70 61 67 65 73 20 69 6e 20 61 63 63 65 6e 64 69  pages in accendi
12d0d 6e 67 20 6f 72 64 65 72 2e 20 20 54 68 69 73 20  ng order.  This 
12d0e 68 65 6c 70 73 20 74 6f 0a 20 20 2a 2a 20 6b 65  helps to.  ** ke
12d0f 65 70 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  ep entries in th
12d10 65 20 64 69 73 6b 20 66 69 6c 65 20 69 6e 20 6f  e disk file in o
12d11 72 64 65 72 20 73 6f 20 74 68 61 74 20 61 20 73  rder so that a s
12d12 63 61 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  can.  ** of the 
12d13 74 61 62 6c 65 20 69 73 20 61 20 6c 69 6e 65 61  table is a linea
12d14 72 20 73 63 61 6e 20 74 68 72 6f 75 67 68 20 74  r scan through t
12d15 68 65 20 66 69 6c 65 2e 20 20 54 68 61 74 0a 20  he file.  That. 
12d16 20 2a 2a 20 69 6e 20 74 75 72 6e 20 68 65 6c 70   ** in turn help
12d17 73 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20  s the operating 
12d18 73 79 73 74 65 6d 20 74 6f 20 64 65 6c 69 76 65  system to delive
12d19 72 20 70 61 67 65 73 0a 20 20 2a 2a 20 66 72 6f  r pages.  ** fro
12d1a 6d 20 74 68 65 20 64 69 73 6b 20 6d 6f 72 65 20  m the disk more 
12d1b 72 61 70 69 64 6c 79 2e 0a 20 20 2a 2a 0a 20 20  rapidly..  **.  
12d1c 2a 2a 20 41 6e 20 4f 28 6e 5e 32 29 20 69 6e 73  ** An O(n^2) ins
12d1d 65 72 74 69 6f 6e 20 73 6f 72 74 20 61 6c 67 6f  ertion sort algo
12d1e 72 69 74 68 6d 20 69 73 20 75 73 65 64 2c 20 62  rithm is used, b
12d1f 75 74 20 73 69 6e 63 65 0a 20 20 2a 2a 20 6e 20  ut since.  ** n 
12d20 69 73 20 6e 65 76 65 72 20 6d 6f 72 65 20 74 68  is never more th
12d21 61 6e 20 4e 42 20 28 61 20 73 6d 61 6c 6c 20 63  an NB (a small c
12d22 6f 6e 73 74 61 6e 74 29 2c 20 74 68 61 74 20 73  onstant), that s
12d23 68 6f 75 6c 64 0a 20 20 2a 2a 20 6e 6f 74 20 62  hould.  ** not b
12d24 65 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 2a  e a problem..  *
12d25 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 4e 42 3d 3d  *.  ** When NB==
12d26 33 2c 20 74 68 69 73 20 6f 6e 65 20 6f 70 74 69  3, this one opti
12d27 6d 69 7a 61 74 69 6f 6e 20 6d 61 6b 65 73 20 74  mization makes t
12d28 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a  he database.  **
12d29 20 61 62 6f 75 74 20 32 35 25 20 66 61 73 74 65   about 25% faste
12d2a 72 20 66 6f 72 20 6c 61 72 67 65 20 69 6e 73 65  r for large inse
12d2b 72 74 69 6f 6e 73 20 61 6e 64 20 64 65 6c 65 74  rtions and delet
12d2c 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  ions..  */.  for
12d2d 28 69 3d 30 3b 20 69 3c 6b 2d 31 3b 20 69 2b 2b  (i=0; i<k-1; i++
12d2e 29 7b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 56 20  ){.    int minV 
12d2f 3d 20 70 67 6e 6f 4e 65 77 5b 69 5d 3b 0a 20 20  = pgnoNew[i];.  
12d30 20 20 69 6e 74 20 6d 69 6e 49 20 3d 20 69 3b 0a    int minI = i;.
12d31 20 20 20 20 66 6f 72 28 6a 3d 69 2b 31 3b 20 6a      for(j=i+1; j
12d32 3c 6b 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  <k; j++){.      
12d33 69 66 28 20 70 67 6e 6f 4e 65 77 5b 6a 5d 3c 28  if( pgnoNew[j]<(
12d34 75 6e 73 69 67 6e 65 64 29 6d 69 6e 56 20 29 7b  unsigned)minV ){
12d35 0a 20 20 20 20 20 20 20 20 6d 69 6e 49 20 3d 20  .        minI = 
12d36 6a 3b 0a 20 20 20 20 20 20 20 20 6d 69 6e 56 20  j;.        minV 
12d37 3d 20 70 67 6e 6f 4e 65 77 5b 6a 5d 3b 0a 20 20  = pgnoNew[j];.  
12d38 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
12d39 69 66 28 20 6d 69 6e 49 3e 69 20 29 7b 0a 20 20  if( minI>i ){.  
12d3a 20 20 20 20 69 6e 74 20 74 3b 0a 20 20 20 20 20      int t;.     
12d3b 20 4d 65 6d 50 61 67 65 20 2a 70 54 3b 0a 20 20   MemPage *pT;.  
12d3c 20 20 20 20 74 20 3d 20 70 67 6e 6f 4e 65 77 5b      t = pgnoNew[
12d3d 69 5d 3b 0a 20 20 20 20 20 20 70 54 20 3d 20 61  i];.      pT = a
12d3e 70 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20 70  pNew[i];.      p
12d3f 67 6e 6f 4e 65 77 5b 69 5d 20 3d 20 70 67 6e 6f  gnoNew[i] = pgno
12d40 4e 65 77 5b 6d 69 6e 49 5d 3b 0a 20 20 20 20 20  New[minI];.     
12d41 20 61 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4e 65   apNew[i] = apNe
12d42 77 5b 6d 69 6e 49 5d 3b 0a 20 20 20 20 20 20 70  w[minI];.      p
12d43 67 6e 6f 4e 65 77 5b 6d 69 6e 49 5d 20 3d 20 74  gnoNew[minI] = t
12d44 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 6d 69  ;.      apNew[mi
12d45 6e 49 5d 20 3d 20 70 54 3b 0a 20 20 20 20 7d 0a  nI] = pT;.    }.
12d46 20 20 7d 0a 20 20 54 52 41 43 45 28 28 22 42 41    }.  TRACE(("BA
12d47 4c 41 4e 43 45 3a 20 6f 6c 64 3a 20 25 64 20 25  LANCE: old: %d %
12d48 64 20 25 64 20 20 6e 65 77 3a 20 25 64 28 25 64  d %d  new: %d(%d
12d49 29 20 25 64 28 25 64 29 20 25 64 28 25 64 29 20  ) %d(%d) %d(%d) 
12d4a 25 64 28 25 64 29 20 25 64 28 25 64 29 5c 6e 22  %d(%d) %d(%d)\n"
12d4b 2c 0a 20 20 20 20 70 67 6e 6f 4f 6c 64 5b 30 5d  ,.    pgnoOld[0]
12d4c 2c 20 0a 20 20 20 20 6e 4f 6c 64 3e 3d 32 20 3f  , .    nOld>=2 ?
12d4d 20 70 67 6e 6f 4f 6c 64 5b 31 5d 20 3a 20 30 2c   pgnoOld[1] : 0,
12d4e 0a 20 20 20 20 6e 4f 6c 64 3e 3d 33 20 3f 20 70  .    nOld>=3 ? p
12d4f 67 6e 6f 4f 6c 64 5b 32 5d 20 3a 20 30 2c 0a 20  gnoOld[2] : 0,. 
12d50 20 20 20 70 67 6e 6f 4e 65 77 5b 30 5d 2c 20 73     pgnoNew[0], s
12d51 7a 4e 65 77 5b 30 5d 2c 0a 20 20 20 20 6e 4e 65  zNew[0],.    nNe
12d52 77 3e 3d 32 20 3f 20 70 67 6e 6f 4e 65 77 5b 31  w>=2 ? pgnoNew[1
12d53 5d 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 32 20 3f  ] : 0, nNew>=2 ?
12d54 20 73 7a 4e 65 77 5b 31 5d 20 3a 20 30 2c 0a 20   szNew[1] : 0,. 
12d55 20 20 20 6e 4e 65 77 3e 3d 33 20 3f 20 70 67 6e     nNew>=3 ? pgn
12d56 6f 4e 65 77 5b 32 5d 20 3a 20 30 2c 20 6e 4e 65  oNew[2] : 0, nNe
12d57 77 3e 3d 33 20 3f 20 73 7a 4e 65 77 5b 32 5d 20  w>=3 ? szNew[2] 
12d58 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 34  : 0,.    nNew>=4
12d59 20 3f 20 70 67 6e 6f 4e 65 77 5b 33 5d 20 3a 20   ? pgnoNew[3] : 
12d5a 30 2c 20 6e 4e 65 77 3e 3d 34 20 3f 20 73 7a 4e  0, nNew>=4 ? szN
12d5b 65 77 5b 33 5d 20 3a 20 30 2c 0a 20 20 20 20 6e  ew[3] : 0,.    n
12d5c 4e 65 77 3e 3d 35 20 3f 20 70 67 6e 6f 4e 65 77  New>=5 ? pgnoNew
12d5d 5b 34 5d 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 35  [4] : 0, nNew>=5
12d5e 20 3f 20 73 7a 4e 65 77 5b 34 5d 20 3a 20 30 29   ? szNew[4] : 0)
12d5f 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 45 76  );..  /*.  ** Ev
12d60 65 6e 6c 79 20 64 69 73 74 72 69 62 75 74 65 20  enly distribute 
12d61 74 68 65 20 64 61 74 61 20 69 6e 20 61 70 43 65  the data in apCe
12d62 6c 6c 5b 5d 20 61 63 72 6f 73 73 20 74 68 65 20  ll[] across the 
12d63 6e 65 77 20 70 61 67 65 73 2e 0a 20 20 2a 2a 20  new pages..  ** 
12d64 49 6e 73 65 72 74 20 64 69 76 69 64 65 72 20 63  Insert divider c
12d65 65 6c 6c 73 20 69 6e 74 6f 20 70 50 61 72 65 6e  ells into pParen
12d66 74 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a  t as necessary..
12d67 20 20 2a 2f 0a 20 20 6a 20 3d 20 30 3b 0a 20 20    */.  j = 0;.  
12d68 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 3b  for(i=0; i<nNew;
12d69 20 69 2b 2b 29 7b 0a 20 20 20 20 2f 2a 20 41 73   i++){.    /* As
12d6a 73 65 6d 62 6c 65 20 74 68 65 20 6e 65 77 20 73  semble the new s
12d6b 69 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a  ibling page. */.
12d6c 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65      MemPage *pNe
12d6d 77 20 3d 20 61 70 4e 65 77 5b 69 5d 3b 0a 20 20  w = apNew[i];.  
12d6e 20 20 61 73 73 65 72 74 28 20 6a 3c 6e 4d 61 78    assert( j<nMax
12d6f 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 61 73 73  Cells );.    ass
12d70 65 72 74 28 20 70 4e 65 77 2d 3e 70 67 6e 6f 3d  ert( pNew->pgno=
12d71 3d 70 67 6e 6f 4e 65 77 5b 69 5d 20 29 3b 0a 20  =pgnoNew[i] );. 
12d72 20 20 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28     assemblePage(
12d73 70 4e 65 77 2c 20 63 6e 74 4e 65 77 5b 69 5d 2d  pNew, cntNew[i]-
12d74 6a 2c 20 26 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26  j, &apCell[j], &
12d75 73 7a 43 65 6c 6c 5b 6a 5d 29 3b 0a 20 20 20 20  szCell[j]);.    
12d76 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 43  assert( pNew->nC
12d77 65 6c 6c 3e 30 20 7c 7c 20 28 6e 4e 65 77 3d 3d  ell>0 || (nNew==
12d78 31 20 26 26 20 63 6e 74 4e 65 77 5b 30 5d 3d 3d  1 && cntNew[0]==
12d79 30 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  0) );.    assert
12d7a 28 20 70 4e 65 77 2d 3e 6e 4f 76 65 72 66 6c 6f  ( pNew->nOverflo
12d7b 77 3d 3d 30 20 29 3b 0a 0a 23 69 66 6e 64 65 66  w==0 );..#ifndef
12d7c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
12d7d 4f 56 41 43 55 55 4d 0a 20 20 20 20 2f 2a 20 49  OVACUUM.    /* I
12d7e 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74  f this is an aut
12d7f 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73  o-vacuum databas
12d80 65 2c 20 75 70 64 61 74 65 20 74 68 65 20 70 6f  e, update the po
12d81 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 69 65  inter map entrie
12d82 73 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 70 6f  s.    ** that po
12d83 69 6e 74 20 74 6f 20 74 68 65 20 73 69 62 6c 69  int to the sibli
12d84 6e 67 73 20 74 68 61 74 20 77 65 72 65 20 72 65  ngs that were re
12d85 61 72 72 61 6e 67 65 64 2e 20 54 68 65 73 65 20  arranged. These 
12d86 63 61 6e 20 62 65 3a 20 6c 65 66 74 0a 20 20 20  can be: left.   
12d87 20 2a 2a 20 63 68 69 6c 64 72 65 6e 20 6f 66 20   ** children of 
12d88 63 65 6c 6c 73 2c 20 74 68 65 20 72 69 67 68 74  cells, the right
12d89 2d 63 68 69 6c 64 20 6f 66 20 74 68 65 20 70 61  -child of the pa
12d8a 67 65 2c 20 6f 72 20 6f 76 65 72 66 6c 6f 77 20  ge, or overflow 
12d8b 70 61 67 65 73 0a 20 20 20 20 2a 2a 20 70 6f 69  pages.    ** poi
12d8c 6e 74 65 64 20 74 6f 20 62 79 20 63 65 6c 6c 73  nted to by cells
12d8d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
12d8e 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
12d8f 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d   ){.      for(k=
12d90 6a 3b 20 6b 3c 63 6e 74 4e 65 77 5b 69 5d 3b 20  j; k<cntNew[i]; 
12d91 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61 73  k++){.        as
12d92 73 65 72 74 28 20 6b 3c 6e 4d 61 78 43 65 6c 6c  sert( k<nMaxCell
12d93 73 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  s );.        if(
12d94 20 61 46 72 6f 6d 5b 6b 5d 3d 3d 30 78 46 46 20   aFrom[k]==0xFF 
12d95 7c 7c 20 61 70 43 6f 70 79 5b 61 46 72 6f 6d 5b  || apCopy[aFrom[
12d96 6b 5d 5d 2d 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d  k]]->pgno!=pNew-
12d97 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20  >pgno ){.       
12d98 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75     rc = ptrmapPu
12d99 74 4f 76 66 6c 28 70 4e 65 77 2c 20 6b 2d 6a 29  tOvfl(pNew, k-j)
12d9a 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
12d9b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
12d9c 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74  .            got
12d9d 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75  o balance_cleanu
12d9e 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  p;.          }. 
12d9f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
12da0 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  .    }.#endif.. 
12da1 20 20 20 6a 20 3d 20 63 6e 74 4e 65 77 5b 69 5d     j = cntNew[i]
12da2 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  ;..    /* If the
12da3 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20 61 73   sibling page as
12da4 73 65 6d 62 6c 65 64 20 61 62 6f 76 65 20 77 61  sembled above wa
12da5 73 20 6e 6f 74 20 74 68 65 20 72 69 67 68 74 2d  s not the right-
12da6 6d 6f 73 74 20 73 69 62 6c 69 6e 67 2c 0a 20 20  most sibling,.  
12da7 20 20 2a 2a 20 69 6e 73 65 72 74 20 61 20 64 69    ** insert a di
12da8 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 74 6f 20  vider cell into 
12da9 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e  the parent page.
12daa 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
12dab 69 3c 6e 4e 65 77 2d 31 20 26 26 20 6a 3c 6e 43  i<nNew-1 && j<nC
12dac 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 75 38 20  ell ){.      u8 
12dad 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 75 38  *pCell;.      u8
12dae 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 69   *pTemp;.      i
12daf 6e 74 20 73 7a 3b 0a 0a 20 20 20 20 20 20 61 73  nt sz;..      as
12db0 73 65 72 74 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c  sert( j<nMaxCell
12db1 73 20 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c  s );.      pCell
12db2 20 3d 20 61 70 43 65 6c 6c 5b 6a 5d 3b 0a 20 20   = apCell[j];.  
12db3 20 20 20 20 73 7a 20 3d 20 73 7a 43 65 6c 6c 5b      sz = szCell[
12db4 6a 5d 20 2b 20 6c 65 61 66 43 6f 72 72 65 63 74  j] + leafCorrect
12db5 69 6f 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 21  ion;.      if( !
12db6 70 4e 65 77 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  pNew->leaf ){.  
12db7 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e        memcpy(&pN
12db8 65 77 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 70 43  ew->aData[8], pC
12db9 65 6c 6c 2c 20 34 29 3b 0a 20 20 20 20 20 20 20  ell, 4);.       
12dba 20 70 54 65 6d 70 20 3d 20 30 3b 0a 20 20 20 20   pTemp = 0;.    
12dbb 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 65 61 66    }else if( leaf
12dbc 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 20 20  Data ){.        
12dbd 2f 2a 20 49 66 20 74 68 65 20 74 72 65 65 20 69  /* If the tree i
12dbe 73 20 61 20 6c 65 61 66 2d 64 61 74 61 20 74 72  s a leaf-data tr
12dbf 65 65 2c 20 61 6e 64 20 74 68 65 20 73 69 62 6c  ee, and the sibl
12dc0 69 6e 67 73 20 61 72 65 20 6c 65 61 76 65 73 2c  ings are leaves,
12dc1 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65   .        ** the
12dc2 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 64 69  n there is no di
12dc3 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 20 61 70  vider cell in ap
12dc4 43 65 6c 6c 5b 5d 2e 20 49 6e 73 74 65 61 64 2c  Cell[]. Instead,
12dc5 20 74 68 65 20 64 69 76 69 64 65 72 20 0a 20 20   the divider .  
12dc6 20 20 20 20 20 20 2a 2a 20 63 65 6c 6c 20 63 6f        ** cell co
12dc7 6e 73 69 73 74 73 20 6f 66 20 74 68 65 20 69 6e  nsists of the in
12dc8 74 65 67 65 72 20 6b 65 79 20 66 6f 72 20 74 68  teger key for th
12dc9 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 65 6c  e right-most cel
12dca 6c 20 6f 66 20 0a 20 20 20 20 20 20 20 20 2a 2a  l of .        **
12dcb 20 74 68 65 20 73 69 62 6c 69 6e 67 2d 70 61 67   the sibling-pag
12dcc 65 20 61 73 73 65 6d 62 6c 65 64 20 61 62 6f 76  e assembled abov
12dcd 65 20 6f 6e 6c 79 2e 0a 20 20 20 20 20 20 20 20  e only..        
12dce 2a 2f 0a 20 20 20 20 20 20 20 20 43 65 6c 6c 49  */.        CellI
12dcf 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20  nfo info;.      
12dd0 20 20 6a 2d 2d 3b 0a 20 20 20 20 20 20 20 20 73    j--;.        s
12dd1 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65  qlite3BtreeParse
12dd2 43 65 6c 6c 50 74 72 28 70 4e 65 77 2c 20 61 70  CellPtr(pNew, ap
12dd3 43 65 6c 6c 5b 6a 5d 2c 20 26 69 6e 66 6f 29 3b  Cell[j], &info);
12dd4 0a 20 20 20 20 20 20 20 20 70 43 65 6c 6c 20 3d  .        pCell =
12dd5 20 26 61 53 70 61 63 65 5b 69 53 70 61 63 65 5d   &aSpace[iSpace]
12dd6 3b 0a 20 20 20 20 20 20 20 20 66 69 6c 6c 49 6e  ;.        fillIn
12dd7 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 43  Cell(pParent, pC
12dd8 65 6c 6c 2c 20 30 2c 20 69 6e 66 6f 2e 6e 4b 65  ell, 0, info.nKe
12dd9 79 2c 20 30 2c 20 30 2c 20 30 2c 20 26 73 7a 29  y, 0, 0, 0, &sz)
12dda 3b 0a 20 20 20 20 20 20 20 20 69 53 70 61 63 65  ;.        iSpace
12ddb 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20 20 20 20   += sz;.        
12ddc 61 73 73 65 72 74 28 20 69 53 70 61 63 65 3c 3d  assert( iSpace<=
12ddd 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2a 35 20  pBt->pageSize*5 
12dde 29 3b 0a 20 20 20 20 20 20 20 20 70 54 65 6d 70  );.        pTemp
12ddf 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73   = 0;.      }els
12de0 65 7b 0a 20 20 20 20 20 20 20 20 70 43 65 6c 6c  e{.        pCell
12de1 20 2d 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 70   -= 4;.        p
12de2 54 65 6d 70 20 3d 20 26 61 53 70 61 63 65 5b 69  Temp = &aSpace[i
12de3 53 70 61 63 65 5d 3b 0a 20 20 20 20 20 20 20 20  Space];.        
12de4 69 53 70 61 63 65 20 2b 3d 20 73 7a 3b 0a 20 20  iSpace += sz;.  
12de5 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 53        assert( iS
12de6 70 61 63 65 3c 3d 70 42 74 2d 3e 70 61 67 65 53  pace<=pBt->pageS
12de7 69 7a 65 2a 35 20 29 3b 0a 20 20 20 20 20 20 20  ize*5 );.       
12de8 20 2f 2a 20 4f 62 73 63 75 72 65 20 63 61 73 65   /* Obscure case
12de9 20 66 6f 72 20 6e 6f 6e 2d 6c 65 61 66 2d 64 61   for non-leaf-da
12dea 74 61 20 74 72 65 65 73 3a 20 49 66 20 74 68 65  ta trees: If the
12deb 20 63 65 6c 6c 20 61 74 20 70 43 65 6c 6c 20 77   cell at pCell w
12dec 61 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 72  as.        ** pr
12ded 65 76 69 6f 75 73 6c 79 20 73 74 6f 72 65 64 20  eviously stored 
12dee 6f 6e 20 61 20 6c 65 61 66 20 6e 6f 64 65 2c 20  on a leaf node, 
12def 61 6e 64 20 69 74 73 20 72 65 70 6f 72 74 65 64  and its reported
12df0 20 73 69 7a 65 20 77 61 73 20 34 0a 20 20 20 20   size was 4.    
12df1 20 20 20 20 2a 2a 20 62 79 74 65 73 2c 20 74 68      ** bytes, th
12df2 65 6e 20 69 74 20 6d 61 79 20 61 63 74 75 61 6c  en it may actual
12df3 6c 79 20 62 65 20 73 6d 61 6c 6c 65 72 20 74 68  ly be smaller th
12df4 61 6e 20 74 68 69 73 20 0a 20 20 20 20 20 20 20  an this .       
12df5 20 2a 2a 20 28 73 65 65 20 73 71 6c 69 74 65 33   ** (see sqlite3
12df6 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74  BtreeParseCellPt
12df7 72 28 29 2c 20 34 20 62 79 74 65 73 20 69 73 20  r(), 4 bytes is 
12df8 74 68 65 20 6d 69 6e 69 6d 75 6d 20 73 69 7a 65  the minimum size
12df9 20 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 61   of.        ** a
12dfa 6e 79 20 63 65 6c 6c 29 2e 20 42 75 74 20 69 74  ny cell). But it
12dfb 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f   is important to
12dfc 20 70 61 73 73 20 74 68 65 20 63 6f 72 72 65 63   pass the correc
12dfd 74 20 73 69 7a 65 20 74 6f 20 0a 20 20 20 20 20  t size to .     
12dfe 20 20 20 2a 2a 20 69 6e 73 65 72 74 43 65 6c 6c     ** insertCell
12dff 28 29 2c 20 73 6f 20 72 65 70 61 72 73 65 20 74  (), so reparse t
12e00 68 65 20 63 65 6c 6c 20 6e 6f 77 2e 0a 20 20 20  he cell now..   
12e01 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
12e02 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 69  ** Note that thi
12e03 73 20 63 61 6e 20 6e 65 76 65 72 20 68 61 70 70  s can never happ
12e04 65 6e 20 69 6e 20 61 6e 20 53 51 4c 69 74 65 20  en in an SQLite 
12e05 64 61 74 61 20 66 69 6c 65 2c 20 61 73 20 61 6c  data file, as al
12e06 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 65 6c  l.        ** cel
12e07 6c 73 20 61 72 65 20 61 74 20 6c 65 61 73 74 20  ls are at least 
12e08 34 20 62 79 74 65 73 2e 20 49 74 20 6f 6e 6c 79  4 bytes. It only
12e09 20 68 61 70 70 65 6e 73 20 69 6e 20 62 2d 74 72   happens in b-tr
12e0a 65 65 73 20 75 73 65 64 0a 20 20 20 20 20 20 20  ees used.       
12e0b 20 2a 2a 20 74 6f 20 65 76 61 6c 75 61 74 65 20   ** to evaluate 
12e0c 22 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29  "IN (SELECT ...)
12e0d 22 20 61 6e 64 20 73 69 6d 69 6c 61 72 20 63 6c  " and similar cl
12e0e 61 75 73 65 73 2e 0a 20 20 20 20 20 20 20 20 2a  auses..        *
12e0f 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 7a  /.        if( sz
12e10 43 65 6c 6c 5b 6a 5d 3d 3d 34 20 29 7b 0a 20 20  Cell[j]==4 ){.  
12e11 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 6c          assert(l
12e12 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34  eafCorrection==4
12e13 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 7a 20  );.          sz 
12e14 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50  = cellSizePtr(pP
12e15 61 72 65 6e 74 2c 20 70 43 65 6c 6c 29 3b 0a 20  arent, pCell);. 
12e16 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
12e17 0a 20 20 20 20 20 20 72 63 20 3d 20 69 6e 73 65  .      rc = inse
12e18 72 74 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20  rtCell(pParent, 
12e19 6e 78 44 69 76 2c 20 70 43 65 6c 6c 2c 20 73 7a  nxDiv, pCell, sz
12e1a 2c 20 70 54 65 6d 70 2c 20 34 29 3b 0a 20 20 20  , pTemp, 4);.   
12e1b 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
12e1c 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 62 61 6c 61  E_OK ) goto bala
12e1d 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  nce_cleanup;.   
12e1e 20 20 20 70 75 74 34 62 79 74 65 28 66 69 6e 64     put4byte(find
12e1f 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 50 61  OverflowCell(pPa
12e20 72 65 6e 74 2c 6e 78 44 69 76 29 2c 20 70 4e 65  rent,nxDiv), pNe
12e21 77 2d 3e 70 67 6e 6f 29 3b 0a 23 69 66 6e 64 65  w->pgno);.#ifnde
12e22 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
12e23 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 2f  TOVACUUM.      /
12e24 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20  * If this is an 
12e25 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61  auto-vacuum data
12e26 62 61 73 65 2c 20 61 6e 64 20 6e 6f 74 20 61 20  base, and not a 
12e27 6c 65 61 66 2d 64 61 74 61 20 74 72 65 65 2c 0a  leaf-data tree,.
12e28 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 75 70        ** then up
12e29 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65 72  date the pointer
12e2a 20 6d 61 70 20 77 69 74 68 20 61 6e 20 65 6e 74   map with an ent
12e2b 72 79 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66  ry for the overf
12e2c 6c 6f 77 20 70 61 67 65 0a 20 20 20 20 20 20 2a  low page.      *
12e2d 2a 20 74 68 61 74 20 74 68 65 20 63 65 6c 6c 20  * that the cell 
12e2e 6a 75 73 74 20 69 6e 73 65 72 74 65 64 20 70 6f  just inserted po
12e2f 69 6e 74 73 20 74 6f 20 28 69 66 20 61 6e 79 29  ints to (if any)
12e30 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
12e31 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61   if( pBt->autoVa
12e32 63 75 75 6d 20 26 26 20 21 6c 65 61 66 44 61 74  cuum && !leafDat
12e33 61 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  a ){.        rc 
12e34 3d 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28  = ptrmapPutOvfl(
12e35 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 29 3b  pParent, nxDiv);
12e36 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
12e37 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
12e38 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c          goto bal
12e39 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  ance_cleanup;.  
12e3a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
12e3b 23 65 6e 64 69 66 0a 20 20 20 20 20 20 6a 2b 2b  #endif.      j++
12e3c 3b 0a 20 20 20 20 20 20 6e 78 44 69 76 2b 2b 3b  ;.      nxDiv++;
12e3d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73  .    }.  }.  ass
12e3e 65 72 74 28 20 6a 3d 3d 6e 43 65 6c 6c 20 29 3b  ert( j==nCell );
12e3f 0a 20 20 61 73 73 65 72 74 28 20 6e 4f 6c 64 3e  .  assert( nOld>
12e40 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  0 );.  assert( n
12e41 4e 65 77 3e 30 20 29 3b 0a 20 20 69 66 28 20 28  New>0 );.  if( (
12e42 70 61 67 65 46 6c 61 67 73 20 26 20 50 54 46 5f  pageFlags & PTF_
12e43 4c 45 41 46 29 3d 3d 30 20 29 7b 0a 20 20 20 20  LEAF)==0 ){.    
12e44 6d 65 6d 63 70 79 28 26 61 70 4e 65 77 5b 6e 4e  memcpy(&apNew[nN
12e45 65 77 2d 31 5d 2d 3e 61 44 61 74 61 5b 38 5d 2c  ew-1]->aData[8],
12e46 20 26 61 70 43 6f 70 79 5b 6e 4f 6c 64 2d 31 5d   &apCopy[nOld-1]
12e47 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 34 29 3b 0a  ->aData[8], 4);.
12e48 20 20 7d 0a 20 20 69 66 28 20 6e 78 44 69 76 3d    }.  if( nxDiv=
12e49 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 2b  =pParent->nCell+
12e4a 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c  pParent->nOverfl
12e4b 6f 77 20 29 7b 0a 20 20 20 20 2f 2a 20 52 69 67  ow ){.    /* Rig
12e4c 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e 67 20  ht-most sibling 
12e4d 69 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73  is the right-mos
12e4e 74 20 63 68 69 6c 64 20 6f 66 20 70 50 61 72 65  t child of pPare
12e4f 6e 74 20 2a 2f 0a 20 20 20 20 70 75 74 34 62 79  nt */.    put4by
12e50 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 44 61  te(&pParent->aDa
12e51 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f  ta[pParent->hdrO
12e52 66 66 73 65 74 2b 38 5d 2c 20 70 67 6e 6f 4e 65  ffset+8], pgnoNe
12e53 77 5b 6e 4e 65 77 2d 31 5d 29 3b 0a 20 20 7d 65  w[nNew-1]);.  }e
12e54 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 52 69 67 68  lse{.    /* Righ
12e55 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e 67 20 69  t-most sibling i
12e56 73 20 74 68 65 20 6c 65 66 74 20 63 68 69 6c 64  s the left child
12e57 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 65 6e   of the first en
12e58 74 72 79 20 69 6e 20 70 50 61 72 65 6e 74 0a 20  try in pParent. 
12e59 20 20 20 2a 2a 20 70 61 73 74 20 74 68 65 20 72     ** past the r
12e5a 69 67 68 74 2d 6d 6f 73 74 20 64 69 76 69 64 65  ight-most divide
12e5b 72 20 65 6e 74 72 79 20 2a 2f 0a 20 20 20 20 70  r entry */.    p
12e5c 75 74 34 62 79 74 65 28 66 69 6e 64 4f 76 65 72  ut4byte(findOver
12e5d 66 6c 6f 77 43 65 6c 6c 28 70 50 61 72 65 6e 74  flowCell(pParent
12e5e 2c 20 6e 78 44 69 76 29 2c 20 70 67 6e 6f 4e 65  , nxDiv), pgnoNe
12e5f 77 5b 6e 4e 65 77 2d 31 5d 29 3b 0a 20 20 7d 0a  w[nNew-1]);.  }.
12e60 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 70 61 72  .  /*.  ** Repar
12e61 65 6e 74 20 63 68 69 6c 64 72 65 6e 20 6f 66 20  ent children of 
12e62 61 6c 6c 20 63 65 6c 6c 73 2e 0a 20 20 2a 2f 0a  all cells..  */.
12e63 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e 65    for(i=0; i<nNe
12e64 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72 63 20  w; i++){.    rc 
12e65 3d 20 72 65 70 61 72 65 6e 74 43 68 69 6c 64 50  = reparentChildP
12e66 61 67 65 73 28 61 70 4e 65 77 5b 69 5d 29 3b 0a  ages(apNew[i]);.
12e67 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
12e68 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 62 61 6c  TE_OK ) goto bal
12e69 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  ance_cleanup;.  
12e6a 7d 0a 20 20 72 63 20 3d 20 72 65 70 61 72 65 6e  }.  rc = reparen
12e6b 74 43 68 69 6c 64 50 61 67 65 73 28 70 50 61 72  tChildPages(pPar
12e6c 65 6e 74 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  ent);.  if( rc!=
12e6d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
12e6e 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70   balance_cleanup
12e6f 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 42 61 6c  ;..  /*.  ** Bal
12e70 61 6e 63 65 20 74 68 65 20 70 61 72 65 6e 74 20  ance the parent 
12e71 70 61 67 65 2e 20 20 4e 6f 74 65 20 74 68 61 74  page.  Note that
12e72 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 67   the current pag
12e73 65 20 28 70 50 61 67 65 29 20 6d 69 67 68 74 0a  e (pPage) might.
12e74 20 20 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 61    ** have been a
12e75 64 64 65 64 20 74 6f 20 74 68 65 20 66 72 65 65  dded to the free
12e76 6c 69 73 74 20 73 6f 20 69 74 20 6d 69 67 68 74  list so it might
12e77 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 20 69 6e   no longer be in
12e78 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2a 20  itialized..  ** 
12e79 42 75 74 20 74 68 65 20 70 61 72 65 6e 74 20 70  But the parent p
12e7a 61 67 65 20 77 69 6c 6c 20 61 6c 77 61 79 73 20  age will always 
12e7b 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a  be initialized..
12e7c 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
12e7d 50 61 72 65 6e 74 2d 3e 69 73 49 6e 69 74 20 29  Parent->isInit )
12e7e 3b 0a 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65  ;.  rc = balance
12e7f 28 70 50 61 72 65 6e 74 2c 20 30 29 3b 0a 20 20  (pParent, 0);.  
12e80 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 6c 65 61 6e  .  /*.  ** Clean
12e81 75 70 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  up before return
12e82 69 6e 67 2e 0a 20 20 2a 2f 0a 62 61 6c 61 6e 63  ing..  */.balanc
12e83 65 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c  e_cleanup:.  sql
12e84 69 74 65 33 5f 66 72 65 65 28 61 70 43 65 6c 6c  ite3_free(apCell
12e85 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
12e86 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nOld; i++){.    
12e87 72 65 6c 65 61 73 65 50 61 67 65 28 61 70 4f 6c  releasePage(apOl
12e88 64 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 66 6f 72  d[i]);.  }.  for
12e89 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 3b 20 69 2b  (i=0; i<nNew; i+
12e8a 2b 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50  +){.    releaseP
12e8b 61 67 65 28 61 70 4e 65 77 5b 69 5d 29 3b 0a 20  age(apNew[i]);. 
12e8c 20 7d 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65   }.  releasePage
12e8d 28 70 50 61 72 65 6e 74 29 3b 0a 20 20 54 52 41  (pParent);.  TRA
12e8e 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 66 69  CE(("BALANCE: fi
12e8f 6e 69 73 68 65 64 20 77 69 74 68 20 25 64 3a 20  nished with %d: 
12e90 6f 6c 64 3d 25 64 20 6e 65 77 3d 25 64 20 63 65  old=%d new=%d ce
12e91 6c 6c 73 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20  lls=%d\n",.     
12e92 20 20 20 20 20 70 50 61 67 65 2d 3e 70 67 6e 6f       pPage->pgno
12e93 2c 20 6e 4f 6c 64 2c 20 6e 4e 65 77 2c 20 6e 43  , nOld, nNew, nC
12e94 65 6c 6c 29 29 3b 0a 20 20 72 65 74 75 72 6e 20  ell));.  return 
12e95 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rc;.}../*.** Thi
12e96 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
12e97 6c 65 64 20 66 6f 72 20 74 68 65 20 72 6f 6f 74  led for the root
12e98 20 70 61 67 65 20 6f 66 20 61 20 62 74 72 65 65   page of a btree
12e99 20 77 68 65 6e 20 74 68 65 20 72 6f 6f 74 0a 2a   when the root.*
12e9a 2a 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 73 20  * page contains 
12e9b 6e 6f 20 63 65 6c 6c 73 2e 20 20 54 68 69 73 20  no cells.  This 
12e9c 69 73 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74  is an opportunit
12e9d 79 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 74 72  y to make the tr
12e9e 65 65 0a 2a 2a 20 73 68 61 6c 6c 6f 77 65 72 20  ee.** shallower 
12e9f 62 79 20 6f 6e 65 20 6c 65 76 65 6c 2e 0a 2a 2f  by one level..*/
12ea0 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61  .static int bala
12ea1 6e 63 65 5f 73 68 61 6c 6c 6f 77 65 72 28 4d 65  nce_shallower(Me
12ea2 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20  mPage *pPage){. 
12ea3 20 4d 65 6d 50 61 67 65 20 2a 70 43 68 69 6c 64   MemPage *pChild
12ea4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
12ea5 20 54 68 65 20 6f 6e 6c 79 20 63 68 69 6c 64 20   The only child 
12ea6 70 61 67 65 20 6f 66 20 70 50 61 67 65 20 2a 2f  page of pPage */
12ea7 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69 6c  .  Pgno pgnoChil
12ea8 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
12ea9 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66  /* Page number f
12eaa 6f 72 20 70 43 68 69 6c 64 20 2a 2f 0a 20 20 69  or pChild */.  i
12eab 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
12eac 4b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52  K;          /* R
12ead 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20  eturn code from 
12eae 73 75 62 70 72 6f 63 65 64 75 72 65 73 20 2a 2f  subprocedures */
12eaf 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
12eb0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
12eb1 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 42     /* The main B
12eb2 54 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a  Tree structure *
12eb3 2f 0a 20 20 69 6e 74 20 6d 78 43 65 6c 6c 50 65  /.  int mxCellPe
12eb4 72 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20  rPage;          
12eb5 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62   /* Maximum numb
12eb6 65 72 20 6f 66 20 63 65 6c 6c 73 20 70 65 72 20  er of cells per 
12eb7 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61  page */.  u8 **a
12eb8 70 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  pCell;          
12eb9 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 65         /* All ce
12eba 6c 6c 73 20 66 72 6f 6d 20 70 61 67 65 73 20 62  lls from pages b
12ebb 65 69 6e 67 20 62 61 6c 61 6e 63 65 64 20 2a 2f  eing balanced */
12ebc 0a 20 20 75 31 36 20 2a 73 7a 43 65 6c 6c 3b 20  .  u16 *szCell; 
12ebd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12ebe 2f 2a 20 4c 6f 63 61 6c 20 73 69 7a 65 20 6f 66  /* Local size of
12ebf 20 61 6c 6c 20 63 65 6c 6c 73 20 2a 2f 0a 0a 20   all cells */.. 
12ec0 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
12ec1 70 50 61 72 65 6e 74 3d 3d 30 20 29 3b 0a 20 20  pParent==0 );.  
12ec2 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e  assert( pPage->n
12ec3 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 61 73 73  Cell==0 );.  ass
12ec4 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
12ec5 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
12ec6 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
12ec7 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74  pBt = pPage->pBt
12ec8 3b 0a 20 20 6d 78 43 65 6c 6c 50 65 72 50 61 67  ;.  mxCellPerPag
12ec9 65 20 3d 20 4d 58 5f 43 45 4c 4c 28 70 42 74 29  e = MX_CELL(pBt)
12eca 3b 0a 20 20 61 70 43 65 6c 6c 20 3d 20 73 71 6c  ;.  apCell = sql
12ecb 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 6d 78 43  ite3_malloc( mxC
12ecc 65 6c 6c 50 65 72 50 61 67 65 2a 28 73 69 7a 65  ellPerPage*(size
12ecd 6f 66 28 75 38 2a 29 2b 73 69 7a 65 6f 66 28 75  of(u8*)+sizeof(u
12ece 31 36 29 29 20 29 3b 0a 20 20 69 66 28 20 61 70  16)) );.  if( ap
12ecf 43 65 6c 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e  Cell==0 ) return
12ed0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
12ed1 20 73 7a 43 65 6c 6c 20 3d 20 28 75 31 36 2a 29   szCell = (u16*)
12ed2 26 61 70 43 65 6c 6c 5b 6d 78 43 65 6c 6c 50 65  &apCell[mxCellPe
12ed3 72 50 61 67 65 5d 3b 0a 20 20 69 66 28 20 70 50  rPage];.  if( pP
12ed4 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
12ed5 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 69 73   /* The table is
12ed6 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 6d 70 74   completely empt
12ed7 79 20 2a 2f 0a 20 20 20 20 54 52 41 43 45 28 28  y */.    TRACE((
12ed8 22 42 41 4c 41 4e 43 45 3a 20 65 6d 70 74 79 20  "BALANCE: empty 
12ed9 74 61 62 6c 65 20 25 64 5c 6e 22 2c 20 70 50 61  table %d\n", pPa
12eda 67 65 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 7d 65  ge->pgno));.  }e
12edb 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  lse{.    /* The 
12edc 72 6f 6f 74 20 70 61 67 65 20 69 73 20 65 6d 70  root page is emp
12edd 74 79 20 62 75 74 20 68 61 73 20 6f 6e 65 20 63  ty but has one c
12ede 68 69 6c 64 2e 20 20 54 72 61 6e 73 66 65 72 20  hild.  Transfer 
12edf 74 68 65 0a 20 20 20 20 2a 2a 20 69 6e 66 6f 72  the.    ** infor
12ee0 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 61 74  mation from that
12ee1 20 6f 6e 65 20 63 68 69 6c 64 20 69 6e 74 6f 20   one child into 
12ee2 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 69 66  the root page if
12ee3 20 69 74 20 0a 20 20 20 20 2a 2a 20 77 69 6c 6c   it .    ** will
12ee4 20 66 69 74 2e 20 20 54 68 69 73 20 72 65 64 75   fit.  This redu
12ee5 63 65 73 20 74 68 65 20 64 65 70 74 68 20 6f 66  ces the depth of
12ee6 20 74 68 65 20 74 72 65 65 20 62 79 20 6f 6e 65   the tree by one
12ee7 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
12ee8 49 66 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  If the root page
12ee9 20 69 73 20 70 61 67 65 20 31 2c 20 69 74 20 68   is page 1, it h
12eea 61 73 20 6c 65 73 73 20 73 70 61 63 65 20 61 76  as less space av
12eeb 61 69 6c 61 62 6c 65 20 74 68 61 6e 0a 20 20 20  ailable than.   
12eec 20 2a 2a 20 69 74 73 20 63 68 69 6c 64 20 28 64   ** its child (d
12eed 75 65 20 74 6f 20 74 68 65 20 31 30 30 20 62 79  ue to the 100 by
12eee 74 65 20 68 65 61 64 65 72 20 74 68 61 74 20 6f  te header that o
12eef 63 63 75 72 73 20 61 74 20 74 68 65 20 62 65 67  ccurs at the beg
12ef0 69 6e 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 6f 66  inning.    ** of
12ef1 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 6c   the database fl
12ef2 65 29 2c 20 73 6f 20 69 74 20 6d 69 67 68 74 20  e), so it might 
12ef3 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 68  not be able to h
12ef4 6f 6c 64 20 61 6c 6c 20 6f 66 20 74 68 65 20 0a  old all of the .
12ef5 20 20 20 20 2a 2a 20 69 6e 66 6f 72 6d 61 74 69      ** informati
12ef6 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e  on currently con
12ef7 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 63 68  tained in the ch
12ef8 69 6c 64 2e 20 20 49 66 20 74 68 69 73 20 69 73  ild.  If this is
12ef9 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 61 73   the .    ** cas
12efa 65 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 64  e, then do not d
12efb 6f 20 74 68 65 20 74 72 61 6e 73 66 65 72 2e 20  o the transfer. 
12efc 20 4c 65 61 76 65 20 70 61 67 65 20 31 20 65 6d   Leave page 1 em
12efd 70 74 79 20 65 78 63 65 70 74 0a 20 20 20 20 2a  pty except.    *
12efe 2a 20 66 6f 72 20 74 68 65 20 72 69 67 68 74 2d  * for the right-
12eff 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63  pointer to the c
12f00 68 69 6c 64 20 70 61 67 65 2e 20 20 54 68 65 20  hild page.  The 
12f01 63 68 69 6c 64 20 70 61 67 65 20 62 65 63 6f 6d  child page becom
12f02 65 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 76 69  es.    ** the vi
12f03 72 74 75 61 6c 20 72 6f 6f 74 20 6f 66 20 74 68  rtual root of th
12f04 65 20 74 72 65 65 2e 0a 20 20 20 20 2a 2f 0a 20  e tree..    */. 
12f05 20 20 20 70 67 6e 6f 43 68 69 6c 64 20 3d 20 67     pgnoChild = g
12f06 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e  et4byte(&pPage->
12f07 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72  aData[pPage->hdr
12f08 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20  Offset+8]);.    
12f09 61 73 73 65 72 74 28 20 70 67 6e 6f 43 68 69 6c  assert( pgnoChil
12f0a 64 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  d>0 );.    asser
12f0b 74 28 20 70 67 6e 6f 43 68 69 6c 64 3c 3d 73 71  t( pgnoChild<=sq
12f0c 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f  lite3PagerPageco
12f0d 75 6e 74 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  unt(pPage->pBt->
12f0e 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 72  pPager) );.    r
12f0f 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
12f10 47 65 74 50 61 67 65 28 70 50 61 67 65 2d 3e 70  GetPage(pPage->p
12f11 42 74 2c 20 70 67 6e 6f 43 68 69 6c 64 2c 20 26  Bt, pgnoChild, &
12f12 70 43 68 69 6c 64 2c 20 30 29 3b 0a 20 20 20 20  pChild, 0);.    
12f13 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e  if( rc ) goto en
12f14 64 5f 73 68 61 6c 6c 6f 77 5f 62 61 6c 61 6e 63  d_shallow_balanc
12f15 65 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  e;.    if( pPage
12f16 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20  ->pgno==1 ){.   
12f17 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
12f18 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 43 68  treeInitPage(pCh
12f19 69 6c 64 2c 20 70 50 61 67 65 29 3b 0a 20 20 20  ild, pPage);.   
12f1a 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f     if( rc ) goto
12f1b 20 65 6e 64 5f 73 68 61 6c 6c 6f 77 5f 62 61 6c   end_shallow_bal
12f1c 61 6e 63 65 3b 0a 20 20 20 20 20 20 61 73 73 65  ance;.      asse
12f1d 72 74 28 20 70 43 68 69 6c 64 2d 3e 6e 4f 76 65  rt( pChild->nOve
12f1e 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 20 20  rflow==0 );.    
12f1f 20 20 69 66 28 20 70 43 68 69 6c 64 2d 3e 6e 46    if( pChild->nF
12f20 72 65 65 3e 3d 31 30 30 20 29 7b 0a 20 20 20 20  ree>=100 ){.    
12f21 20 20 20 20 2f 2a 20 54 68 65 20 63 68 69 6c 64      /* The child
12f22 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 69 6c   information wil
12f23 6c 20 66 69 74 20 6f 6e 20 74 68 65 20 72 6f 6f  l fit on the roo
12f24 74 20 70 61 67 65 2c 20 73 6f 20 64 6f 20 74 68  t page, so do th
12f25 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 70  e.        ** cop
12f26 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74  y */.        int
12f27 20 69 3b 0a 20 20 20 20 20 20 20 20 7a 65 72 6f   i;.        zero
12f28 50 61 67 65 28 70 50 61 67 65 2c 20 70 43 68 69  Page(pPage, pChi
12f29 6c 64 2d 3e 61 44 61 74 61 5b 30 5d 29 3b 0a 20  ld->aData[0]);. 
12f2a 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20         for(i=0; 
12f2b 69 3c 70 43 68 69 6c 64 2d 3e 6e 43 65 6c 6c 3b  i<pChild->nCell;
12f2c 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
12f2d 20 61 70 43 65 6c 6c 5b 69 5d 20 3d 20 66 69 6e   apCell[i] = fin
12f2e 64 43 65 6c 6c 28 70 43 68 69 6c 64 2c 69 29 3b  dCell(pChild,i);
12f2f 0a 20 20 20 20 20 20 20 20 20 20 73 7a 43 65 6c  .          szCel
12f30 6c 5b 69 5d 20 3d 20 63 65 6c 6c 53 69 7a 65 50  l[i] = cellSizeP
12f31 74 72 28 70 43 68 69 6c 64 2c 20 61 70 43 65 6c  tr(pChild, apCel
12f32 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d  l[i]);.        }
12f33 0a 20 20 20 20 20 20 20 20 61 73 73 65 6d 62 6c  .        assembl
12f34 65 50 61 67 65 28 70 50 61 67 65 2c 20 70 43 68  ePage(pPage, pCh
12f35 69 6c 64 2d 3e 6e 43 65 6c 6c 2c 20 61 70 43 65  ild->nCell, apCe
12f36 6c 6c 2c 20 73 7a 43 65 6c 6c 29 3b 0a 20 20 20  ll, szCell);.   
12f37 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 68 65       /* Copy the
12f38 20 72 69 67 68 74 2d 70 6f 69 6e 74 65 72 20 6f   right-pointer o
12f39 66 20 74 68 65 20 63 68 69 6c 64 20 74 6f 20 74  f the child to t
12f3a 68 65 20 70 61 72 65 6e 74 2e 20 2a 2f 0a 20 20  he parent. */.  
12f3b 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26        put4byte(&
12f3c 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61  pPage->aData[pPa
12f3d 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  ge->hdrOffset+8]
12f3e 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 67  , .            g
12f3f 65 74 34 62 79 74 65 28 26 70 43 68 69 6c 64 2d  et4byte(&pChild-
12f40 3e 61 44 61 74 61 5b 70 43 68 69 6c 64 2d 3e 68  >aData[pChild->h
12f41 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a 20  drOffset+8]));. 
12f42 20 20 20 20 20 20 20 66 72 65 65 50 61 67 65 28         freePage(
12f43 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 20 20 20  pChild);.       
12f44 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45   TRACE(("BALANCE
12f45 3a 20 63 68 69 6c 64 20 25 64 20 74 72 61 6e 73  : child %d trans
12f46 66 65 72 20 74 6f 20 70 61 67 65 20 31 5c 6e 22  fer to page 1\n"
12f47 2c 20 70 43 68 69 6c 64 2d 3e 70 67 6e 6f 29 29  , pChild->pgno))
12f48 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
12f49 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 68         /* The ch
12f4a 69 6c 64 20 68 61 73 20 6d 6f 72 65 20 69 6e 66  ild has more inf
12f4b 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 77 69  ormation that wi
12f4c 6c 6c 20 66 69 74 20 6f 6e 20 74 68 65 20 72 6f  ll fit on the ro
12f4d 6f 74 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 54  ot..        ** T
12f4e 68 65 20 74 72 65 65 20 69 73 20 61 6c 72 65 61  he tree is alrea
12f4f 64 79 20 62 61 6c 61 6e 63 65 64 2e 20 20 44 6f  dy balanced.  Do
12f50 20 6e 6f 74 68 69 6e 67 2e 20 2a 2f 0a 20 20 20   nothing. */.   
12f51 20 20 20 20 20 54 52 41 43 45 28 28 22 42 41 4c       TRACE(("BAL
12f52 41 4e 43 45 3a 20 63 68 69 6c 64 20 25 64 20 77  ANCE: child %d w
12f53 69 6c 6c 20 6e 6f 74 20 66 69 74 20 6f 6e 20 70  ill not fit on p
12f54 61 67 65 20 31 5c 6e 22 2c 20 70 43 68 69 6c 64  age 1\n", pChild
12f55 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20  ->pgno));.      
12f56 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
12f57 20 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 2d     memcpy(pPage-
12f58 3e 61 44 61 74 61 2c 20 70 43 68 69 6c 64 2d 3e  >aData, pChild->
12f59 61 44 61 74 61 2c 20 70 50 61 67 65 2d 3e 70 42  aData, pPage->pB
12f5a 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a  t->usableSize);.
12f5b 20 20 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49        pPage->isI
12f5c 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  nit = 0;.      p
12f5d 50 61 67 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20  Page->pParent = 
12f5e 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  0;.      rc = sq
12f5f 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61  lite3BtreeInitPa
12f60 67 65 28 70 50 61 67 65 2c 20 30 29 3b 0a 20 20  ge(pPage, 0);.  
12f61 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
12f62 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20  SQLITE_OK );.   
12f63 20 20 20 66 72 65 65 50 61 67 65 28 70 43 68 69     freePage(pChi
12f64 6c 64 29 3b 0a 20 20 20 20 20 20 54 52 41 43 45  ld);.      TRACE
12f65 28 28 22 42 41 4c 41 4e 43 45 3a 20 74 72 61 6e  (("BALANCE: tran
12f66 73 66 65 72 20 63 68 69 6c 64 20 25 64 20 69 6e  sfer child %d in
12f67 74 6f 20 72 6f 6f 74 20 25 64 5c 6e 22 2c 0a 20  to root %d\n",. 
12f68 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 68               pCh
12f69 69 6c 64 2d 3e 70 67 6e 6f 2c 20 70 50 61 67 65  ild->pgno, pPage
12f6a 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a  ->pgno));.    }.
12f6b 20 20 20 20 72 63 20 3d 20 72 65 70 61 72 65 6e      rc = reparen
12f6c 74 43 68 69 6c 64 50 61 67 65 73 28 70 50 61 67  tChildPages(pPag
12f6d 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  e);.    assert( 
12f6e 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
12f6f 3d 3d 30 20 29 3b 0a 23 69 66 6e 64 65 66 20 53  ==0 );.#ifndef S
12f70 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
12f71 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42  ACUUM.    if( pB
12f72 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b  t->autoVacuum ){
12f73 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
12f74 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
12f75 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b  Page->nCell; i++
12f76 29 7b 20 0a 20 20 20 20 20 20 20 20 72 63 20 3d  ){ .        rc =
12f77 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 70   ptrmapPutOvfl(p
12f78 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 20 20  Page, i);.      
12f79 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
12f7a 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
12f7b 20 67 6f 74 6f 20 65 6e 64 5f 73 68 61 6c 6c 6f   goto end_shallo
12f7c 77 5f 62 61 6c 61 6e 63 65 3b 0a 20 20 20 20 20  w_balance;.     
12f7d 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
12f7e 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 65   }.#endif.    re
12f7f 6c 65 61 73 65 50 61 67 65 28 70 43 68 69 6c 64  leasePage(pChild
12f80 29 3b 0a 20 20 7d 0a 65 6e 64 5f 73 68 61 6c 6c  );.  }.end_shall
12f81 6f 77 5f 62 61 6c 61 6e 63 65 3a 0a 20 20 73 71  ow_balance:.  sq
12f82 6c 69 74 65 33 5f 66 72 65 65 28 61 70 43 65 6c  lite3_free(apCel
12f83 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  l);.  return rc;
12f84 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72  .}.../*.** The r
12f85 6f 6f 74 20 70 61 67 65 20 69 73 20 6f 76 65 72  oot page is over
12f86 66 75 6c 6c 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  full.**.** When 
12f87 74 68 69 73 20 68 61 70 70 65 6e 73 2c 20 43 72  this happens, Cr
12f88 65 61 74 65 20 61 20 6e 65 77 20 63 68 69 6c 64  eate a new child
12f89 20 70 61 67 65 20 61 6e 64 20 63 6f 70 79 20 74   page and copy t
12f8a 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f  he.** contents o
12f8b 66 20 74 68 65 20 72 6f 6f 74 20 69 6e 74 6f 20  f the root into 
12f8c 74 68 65 20 63 68 69 6c 64 2e 20 20 54 68 65 6e  the child.  Then
12f8d 20 6d 61 6b 65 20 74 68 65 20 72 6f 6f 74 0a 2a   make the root.*
12f8e 2a 20 70 61 67 65 20 61 6e 20 65 6d 70 74 79 20  * page an empty 
12f8f 70 61 67 65 20 77 69 74 68 20 72 69 67 68 74 43  page with rightC
12f90 68 69 6c 64 20 70 6f 69 6e 74 69 6e 67 20 74 6f  hild pointing to
12f91 20 74 68 65 20 6e 65 77 0a 2a 2a 20 63 68 69 6c   the new.** chil
12f92 64 2e 20 20 20 46 69 6e 61 6c 6c 79 2c 20 63 61  d.   Finally, ca
12f93 6c 6c 20 62 61 6c 61 6e 63 65 5f 69 6e 74 65 72  ll balance_inter
12f94 6e 61 6c 28 29 20 6f 6e 20 74 68 65 20 6e 65 77  nal() on the new
12f95 20 63 68 69 6c 64 0a 2a 2a 20 74 6f 20 63 61 75   child.** to cau
12f96 73 65 20 69 74 20 74 6f 20 73 70 6c 69 74 2e 0a  se it to split..
12f97 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61  */.static int ba
12f98 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 4d 65 6d  lance_deeper(Mem
12f99 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20  Page *pPage){.  
12f9a 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
12f9b 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 76 61      /* Return va
12f9c 6c 75 65 20 66 72 6f 6d 20 73 75 62 70 72 6f 63  lue from subproc
12f9d 65 64 75 72 65 73 20 2a 2f 0a 20 20 4d 65 6d 50  edures */.  MemP
12f9e 61 67 65 20 2a 70 43 68 69 6c 64 3b 20 20 20 20  age *pChild;    
12f9f 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20  /* Pointer to a 
12fa0 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 20 2a  new child page *
12fa1 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69  /.  Pgno pgnoChi
12fa2 6c 64 3b 20 20 20 20 20 2f 2a 20 50 61 67 65 20  ld;     /* Page 
12fa3 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65  number of the ne
12fa4 77 20 63 68 69 6c 64 20 70 61 67 65 20 2a 2f 0a  w child page */.
12fa5 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b    BtShared *pBt;
12fa6 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
12fa7 42 54 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 75  BTree */.  int u
12fa8 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 20 20 2f  sableSize;     /
12fa9 2a 20 54 6f 74 61 6c 20 75 73 61 62 6c 65 20 73  * Total usable s
12faa 69 7a 65 20 6f 66 20 61 20 70 61 67 65 20 2a 2f  ize of a page */
12fab 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20  .  u8 *data;    
12fac 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e         /* Conten
12fad 74 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20  t of the parent 
12fae 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 63 64  page */.  u8 *cd
12faf 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ata;          /*
12fb0 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20   Content of the 
12fb1 63 68 69 6c 64 20 70 61 67 65 20 2a 2f 0a 20 20  child page */.  
12fb2 69 6e 74 20 68 64 72 3b 20 20 20 20 20 20 20 20  int hdr;        
12fb3 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f      /* Offset to
12fb4 20 70 61 67 65 20 68 65 61 64 65 72 20 69 6e 20   page header in 
12fb5 70 61 72 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  parent */.  int 
12fb6 62 72 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  brk;            
12fb7 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 63 6f 6e  /* Offset to con
12fb8 74 65 6e 74 20 6f 66 20 66 69 72 73 74 20 63 65  tent of first ce
12fb9 6c 6c 20 69 6e 20 70 61 72 65 6e 74 20 2a 2f 0a  ll in parent */.
12fba 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
12fbb 2d 3e 70 50 61 72 65 6e 74 3d 3d 30 20 29 3b 0a  ->pParent==0 );.
12fbc 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
12fbd 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 29 3b 0a  >nOverflow>0 );.
12fbe 20 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70    pBt = pPage->p
12fbf 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  Bt;.  assert( sq
12fc0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
12fc1 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
12fc2 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42    rc = allocateB
12fc3 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70  treePage(pBt, &p
12fc4 43 68 69 6c 64 2c 20 26 70 67 6e 6f 43 68 69 6c  Child, &pgnoChil
12fc5 64 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20  d, pPage->pgno, 
12fc6 30 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  0);.  if( rc ) r
12fc7 65 74 75 72 6e 20 72 63 3b 0a 20 20 61 73 73 65  eturn rc;.  asse
12fc8 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
12fc9 49 73 77 72 69 74 65 61 62 6c 65 28 70 43 68 69  Iswriteable(pChi
12fca 6c 64 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a  ld->pDbPage) );.
12fcb 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70    usableSize = p
12fcc 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a  Bt->usableSize;.
12fcd 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e    data = pPage->
12fce 61 44 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 70  aData;.  hdr = p
12fcf 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b  Page->hdrOffset;
12fd0 0a 20 20 62 72 6b 20 3d 20 67 65 74 32 62 79 74  .  brk = get2byt
12fd1 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b  e(&data[hdr+5]);
12fd2 0a 20 20 63 64 61 74 61 20 3d 20 70 43 68 69 6c  .  cdata = pChil
12fd3 64 2d 3e 61 44 61 74 61 3b 0a 20 20 6d 65 6d 63  d->aData;.  memc
12fd4 70 79 28 63 64 61 74 61 2c 20 26 64 61 74 61 5b  py(cdata, &data[
12fd5 68 64 72 5d 2c 20 70 50 61 67 65 2d 3e 63 65 6c  hdr], pPage->cel
12fd6 6c 4f 66 66 73 65 74 2b 32 2a 70 50 61 67 65 2d  lOffset+2*pPage-
12fd7 3e 6e 43 65 6c 6c 2d 68 64 72 29 3b 0a 20 20 6d  >nCell-hdr);.  m
12fd8 65 6d 63 70 79 28 26 63 64 61 74 61 5b 62 72 6b  emcpy(&cdata[brk
12fd9 5d 2c 20 26 64 61 74 61 5b 62 72 6b 5d 2c 20 75  ], &data[brk], u
12fda 73 61 62 6c 65 53 69 7a 65 2d 62 72 6b 29 3b 0a  sableSize-brk);.
12fdb 20 20 61 73 73 65 72 74 28 20 70 43 68 69 6c 64    assert( pChild
12fdc 2d 3e 69 73 49 6e 69 74 3d 3d 30 20 29 3b 0a 20  ->isInit==0 );. 
12fdd 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
12fde 65 65 49 6e 69 74 50 61 67 65 28 70 43 68 69 6c  eeInitPage(pChil
12fdf 64 2c 20 70 50 61 67 65 29 3b 0a 20 20 69 66 28  d, pPage);.  if(
12fe0 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e   rc ) goto balan
12fe1 63 65 64 65 65 70 65 72 5f 6f 75 74 3b 0a 20 20  cedeeper_out;.  
12fe2 6d 65 6d 63 70 79 28 70 43 68 69 6c 64 2d 3e 61  memcpy(pChild->a
12fe3 4f 76 66 6c 2c 20 70 50 61 67 65 2d 3e 61 4f 76  Ovfl, pPage->aOv
12fe4 66 6c 2c 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72  fl, pPage->nOver
12fe5 66 6c 6f 77 2a 73 69 7a 65 6f 66 28 70 50 61 67  flow*sizeof(pPag
12fe6 65 2d 3e 61 4f 76 66 6c 5b 30 5d 29 29 3b 0a 20  e->aOvfl[0]));. 
12fe7 20 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c   pChild->nOverfl
12fe8 6f 77 20 3d 20 70 50 61 67 65 2d 3e 6e 4f 76 65  ow = pPage->nOve
12fe9 72 66 6c 6f 77 3b 0a 20 20 69 66 28 20 70 43 68  rflow;.  if( pCh
12fea 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29  ild->nOverflow )
12feb 7b 0a 20 20 20 20 70 43 68 69 6c 64 2d 3e 6e 46  {.    pChild->nF
12fec 72 65 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 61  ree = 0;.  }.  a
12fed 73 73 65 72 74 28 20 70 43 68 69 6c 64 2d 3e 6e  ssert( pChild->n
12fee 43 65 6c 6c 3d 3d 70 50 61 67 65 2d 3e 6e 43 65  Cell==pPage->nCe
12fef 6c 6c 20 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65  ll );.  zeroPage
12ff0 28 70 50 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e  (pPage, pChild->
12ff1 61 44 61 74 61 5b 30 5d 20 26 20 7e 50 54 46 5f  aData[0] & ~PTF_
12ff2 4c 45 41 46 29 3b 0a 20 20 70 75 74 34 62 79 74  LEAF);.  put4byt
12ff3 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b  e(&pPage->aData[
12ff4 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
12ff5 2b 38 5d 2c 20 70 67 6e 6f 43 68 69 6c 64 29 3b  +8], pgnoChild);
12ff6 0a 20 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e  .  TRACE(("BALAN
12ff7 43 45 3a 20 63 6f 70 79 20 72 6f 6f 74 20 25 64  CE: copy root %d
12ff8 20 69 6e 74 6f 20 25 64 5c 6e 22 2c 20 70 50 61   into %d\n", pPa
12ff9 67 65 2d 3e 70 67 6e 6f 2c 20 70 43 68 69 6c 64  ge->pgno, pChild
12ffa 2d 3e 70 67 6e 6f 29 29 3b 0a 23 69 66 6e 64 65  ->pgno));.#ifnde
12ffb 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
12ffc 54 4f 56 41 43 55 55 4d 0a 20 20 69 66 28 20 70  TOVACUUM.  if( p
12ffd 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29  Bt->autoVacuum )
12ffe 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  {.    int i;.   
12fff 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28   rc = ptrmapPut(
13000 70 42 74 2c 20 70 43 68 69 6c 64 2d 3e 70 67 6e  pBt, pChild->pgn
13001 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c  o, PTRMAP_BTREE,
13002 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20   pPage->pgno);. 
13003 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f     if( rc ) goto
13004 20 62 61 6c 61 6e 63 65 64 65 65 70 65 72 5f 6f   balancedeeper_o
13005 75 74 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  ut;.    for(i=0;
13006 20 69 3c 70 43 68 69 6c 64 2d 3e 6e 43 65 6c 6c   i<pChild->nCell
13007 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 63  ; i++){.      rc
13008 20 3d 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c   = ptrmapPutOvfl
13009 28 70 43 68 69 6c 64 2c 20 69 29 3b 0a 20 20 20  (pChild, i);.   
1300a 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1300b 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1300c 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
1300d 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e   }.    }.  }.#en
1300e 64 69 66 0a 20 20 72 63 20 3d 20 62 61 6c 61 6e  dif.  rc = balan
1300f 63 65 5f 6e 6f 6e 72 6f 6f 74 28 70 43 68 69 6c  ce_nonroot(pChil
13010 64 29 3b 0a 0a 62 61 6c 61 6e 63 65 64 65 65 70  d);..balancedeep
13011 65 72 5f 6f 75 74 3a 0a 20 20 72 65 6c 65 61 73  er_out:.  releas
13012 65 50 61 67 65 28 70 43 68 69 6c 64 29 3b 0a 20  ePage(pChild);. 
13013 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
13014 2a 0a 2a 2a 20 44 65 63 69 64 65 20 69 66 20 74  *.** Decide if t
13015 68 65 20 70 61 67 65 20 70 50 61 67 65 20 6e 65  he page pPage ne
13016 65 64 73 20 74 6f 20 62 65 20 62 61 6c 61 6e 63  eds to be balanc
13017 65 64 2e 20 20 49 66 20 62 61 6c 61 6e 63 69 6e  ed.  If balancin
13018 67 20 69 73 0a 2a 2a 20 72 65 71 75 69 72 65 64  g is.** required
13019 2c 20 63 61 6c 6c 20 74 68 65 20 61 70 70 72 6f  , call the appro
1301a 70 72 69 61 74 65 20 62 61 6c 61 6e 63 69 6e 67  priate balancing
1301b 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 73 74 61   routine..*/.sta
1301c 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 28  tic int balance(
1301d 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20  MemPage *pPage, 
1301e 69 6e 74 20 69 6e 73 65 72 74 29 7b 0a 20 20 69  int insert){.  i
1301f 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
13020 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  K;.  assert( sql
13021 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
13022 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65  pPage->pBt->mute
13023 78 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67  x) );.  if( pPag
13024 65 2d 3e 70 50 61 72 65 6e 74 3d 3d 30 20 29 7b  e->pParent==0 ){
13025 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
13026 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67  3PagerWrite(pPag
13027 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  e->pDbPage);.   
13028 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
13029 4f 4b 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f 76  OK && pPage->nOv
1302a 65 72 66 6c 6f 77 3e 30 20 29 7b 0a 20 20 20 20  erflow>0 ){.    
1302b 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 64    rc = balance_d
1302c 65 65 70 65 72 28 70 50 61 67 65 29 3b 0a 20 20  eeper(pPage);.  
1302d 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d    }.    if( rc==
1302e 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61  SQLITE_OK && pPa
1302f 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 7b 0a  ge->nCell==0 ){.
13030 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e        rc = balan
13031 63 65 5f 73 68 61 6c 6c 6f 77 65 72 28 70 50 61  ce_shallower(pPa
13032 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  ge);.    }.  }el
13033 73 65 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67  se{.    if( pPag
13034 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c  e->nOverflow>0 |
13035 7c 20 0a 20 20 20 20 20 20 20 20 28 21 69 6e 73  | .        (!ins
13036 65 72 74 20 26 26 20 70 50 61 67 65 2d 3e 6e 46  ert && pPage->nF
13037 72 65 65 3e 70 50 61 67 65 2d 3e 70 42 74 2d 3e  ree>pPage->pBt->
13038 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 29 20  usableSize*2/3) 
13039 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 62 61  ){.      rc = ba
1303a 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 70 50  lance_nonroot(pP
1303b 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  age);.    }.  }.
1303c 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1303d 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
1303e 6e 65 20 63 68 65 63 6b 73 20 61 6c 6c 20 63 75  ne checks all cu
1303f 72 73 6f 72 73 20 74 68 61 74 20 70 6f 69 6e 74  rsors that point
13040 20 74 6f 20 74 61 62 6c 65 20 70 67 6e 6f 52 6f   to table pgnoRo
13041 6f 74 2e 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66  ot..** If any of
13042 20 74 68 6f 73 65 20 63 75 72 73 6f 72 73 20 77   those cursors w
13043 65 72 65 20 6f 70 65 6e 65 64 20 77 69 74 68 20  ere opened with 
13044 77 72 46 6c 61 67 3d 3d 30 20 69 6e 20 61 20 64  wrFlag==0 in a d
13045 69 66 66 65 72 65 6e 74 0a 2a 2a 20 64 61 74 61  ifferent.** data
13046 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
13047 28 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  (a database conn
13048 65 63 74 69 6f 6e 20 74 68 61 74 20 73 68 61 72  ection that shar
13049 65 73 20 74 68 65 20 70 61 67 65 72 0a 2a 2a 20  es the pager.** 
1304a 63 61 63 68 65 20 77 69 74 68 20 74 68 65 20 63  cache with the c
1304b 75 72 72 65 6e 74 20 63 6f 6e 6e 65 63 74 69 6f  urrent connectio
1304c 6e 29 20 61 6e 64 20 74 68 61 74 20 6f 74 68 65  n) and that othe
1304d 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a  r connection .**
1304e 20 69 73 20 6e 6f 74 20 69 6e 20 74 68 65 20 52   is not in the R
1304f 65 61 64 55 6e 63 6f 6d 6d 6d 69 74 74 65 64 20  eadUncommmitted 
13050 73 74 61 74 65 2c 20 74 68 65 6e 20 74 68 69 73  state, then this
13051 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
13052 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b   .** SQLITE_LOCK
13053 45 44 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 64 64  ED..**.** In add
13054 69 74 69 6f 6e 20 74 6f 20 63 68 65 63 6b 69 6e  ition to checkin
13055 67 20 66 6f 72 20 72 65 61 64 2d 6c 6f 63 6b 73  g for read-locks
13056 20 28 77 68 65 72 65 20 61 20 72 65 61 64 2d 6c   (where a read-l
13057 6f 63 6b 20 0a 2a 2a 20 6d 65 61 6e 73 20 61 20  ock .** means a 
13058 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 77 69  cursor opened wi
13059 74 68 20 77 72 46 6c 61 67 3d 3d 30 29 20 74 68  th wrFlag==0) th
1305a 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20  is routine also 
1305b 6d 6f 76 65 73 0a 2a 2a 20 61 6c 6c 20 77 72 69  moves.** all wri
1305c 74 65 20 63 75 72 73 6f 72 73 20 73 6f 20 74 68  te cursors so th
1305d 61 74 20 74 68 65 79 20 61 72 65 20 70 6f 69 6e  at they are poin
1305e 74 69 6e 67 20 74 6f 20 74 68 65 20 0a 2a 2a 20  ting to the .** 
1305f 66 69 72 73 74 20 43 65 6c 6c 20 6f 6e 20 74 68  first Cell on th
13060 65 20 72 6f 6f 74 20 70 61 67 65 2e 20 20 54 68  e root page.  Th
13061 69 73 20 69 73 20 6e 65 63 65 73 73 61 72 79 20  is is necessary 
13062 62 65 63 61 75 73 65 20 61 6e 20 69 6e 73 65 72  because an inser
13063 74 20 0a 2a 2a 20 6f 72 20 64 65 6c 65 74 65 20  t .** or delete 
13064 6d 69 67 68 74 20 63 68 61 6e 67 65 20 74 68 65  might change the
13065 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73   number of cells
13066 20 6f 6e 20 61 20 70 61 67 65 20 6f 72 20 64 65   on a page or de
13067 6c 65 74 65 0a 2a 2a 20 61 20 70 61 67 65 20 65  lete.** a page e
13068 6e 74 69 72 65 6c 79 20 61 6e 64 20 77 65 20 64  ntirely and we d
13069 6f 20 6e 6f 74 20 77 61 6e 74 20 74 6f 20 6c 65  o not want to le
1306a 61 76 65 20 61 6e 79 20 63 75 72 73 6f 72 73 20  ave any cursors 
1306b 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20  .** pointing to 
1306c 6e 6f 6e 2d 65 78 69 73 74 61 6e 74 20 70 61 67  non-existant pag
1306d 65 73 20 6f 72 20 63 65 6c 6c 73 2e 0a 2a 2f 0a  es or cells..*/.
1306e 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b  static int check
1306f 52 65 61 64 4c 6f 63 6b 73 28 42 74 72 65 65 20  ReadLocks(Btree 
13070 2a 70 42 74 72 65 65 2c 20 50 67 6e 6f 20 70 67  *pBtree, Pgno pg
13071 6e 6f 52 6f 6f 74 2c 20 42 74 43 75 72 73 6f 72  noRoot, BtCursor
13072 20 2a 70 45 78 63 6c 75 64 65 29 7b 0a 20 20 42   *pExclude){.  B
13073 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 42 74  tCursor *p;.  Bt
13074 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 42  Shared *pBt = pB
13075 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20 73 71 6c  tree->pBt;.  sql
13076 69 74 65 33 20 2a 64 62 20 3d 20 70 42 74 72 65  ite3 *db = pBtre
13077 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28  e->db;.  assert(
13078 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
13079 64 73 4d 75 74 65 78 28 70 42 74 72 65 65 29 20  dsMutex(pBtree) 
1307a 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e  );.  for(p=pBt->
1307b 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d  pCursor; p; p=p-
1307c 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28  >pNext){.    if(
1307d 20 70 3d 3d 70 45 78 63 6c 75 64 65 20 29 20 63   p==pExclude ) c
1307e 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28  ontinue;.    if(
1307f 20 70 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53   p->eState!=CURS
13080 4f 52 5f 56 41 4c 49 44 20 29 20 63 6f 6e 74 69  OR_VALID ) conti
13081 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  nue;.    if( p->
13082 70 67 6e 6f 52 6f 6f 74 21 3d 70 67 6e 6f 52 6f  pgnoRoot!=pgnoRo
13083 6f 74 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  ot ) continue;. 
13084 20 20 20 69 66 28 20 70 2d 3e 77 72 46 6c 61 67     if( p->wrFlag
13085 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
13086 69 74 65 33 20 2a 64 62 4f 74 68 65 72 20 3d 20  ite3 *dbOther = 
13087 70 2d 3e 70 42 74 72 65 65 2d 3e 64 62 3b 0a 20  p->pBtree->db;. 
13088 20 20 20 20 20 69 66 28 20 64 62 4f 74 68 65 72       if( dbOther
13089 3d 3d 30 20 7c 7c 0a 20 20 20 20 20 20 20 20 20  ==0 ||.         
1308a 28 64 62 4f 74 68 65 72 21 3d 64 62 20 26 26 20  (dbOther!=db && 
1308b 28 64 62 4f 74 68 65 72 2d 3e 66 6c 61 67 73 20  (dbOther->flags 
1308c 26 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63  & SQLITE_ReadUnc
1308d 6f 6d 6d 69 74 74 65 64 29 3d 3d 30 29 20 29 7b  ommitted)==0) ){
1308e 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
1308f 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20  SQLITE_LOCKED;. 
13090 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
13091 20 69 66 28 20 70 2d 3e 70 50 61 67 65 2d 3e 70   if( p->pPage->p
13092 67 6e 6f 21 3d 70 2d 3e 70 67 6e 6f 52 6f 6f 74  gno!=p->pgnoRoot
13093 20 29 7b 0a 20 20 20 20 20 20 6d 6f 76 65 54 6f   ){.      moveTo
13094 52 6f 6f 74 28 70 29 3b 0a 20 20 20 20 7d 0a 20  Root(p);.    }. 
13095 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
13096 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
13097 4d 61 6b 65 20 73 75 72 65 20 70 42 74 2d 3e 70  Make sure pBt->p
13098 54 6d 70 53 70 61 63 65 20 70 6f 69 6e 74 73 20  TmpSpace points 
13099 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e  to an allocation
1309a 20 6f 66 20 0a 2a 2a 20 4d 58 5f 43 45 4c 4c 5f   of .** MX_CELL_
1309b 53 49 5a 45 28 70 42 74 29 20 62 79 74 65 73 2e  SIZE(pBt) bytes.
1309c 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1309d 61 6c 6c 6f 63 61 74 65 54 65 6d 70 53 70 61 63  allocateTempSpac
1309e 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29  e(BtShared *pBt)
1309f 7b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70 54  {.  if( !pBt->pT
130a0 6d 70 53 70 61 63 65 20 29 7b 0a 20 20 20 20 70  mpSpace ){.    p
130a1 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20  Bt->pTmpSpace = 
130a2 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 4d  sqlite3_malloc(M
130a3 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29  X_CELL_SIZE(pBt)
130a4 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
130a5 49 6e 73 65 72 74 20 61 20 6e 65 77 20 72 65 63  Insert a new rec
130a6 6f 72 64 20 69 6e 74 6f 20 74 68 65 20 42 54 72  ord into the BTr
130a7 65 65 2e 20 20 54 68 65 20 6b 65 79 20 69 73 20  ee.  The key is 
130a8 67 69 76 65 6e 20 62 79 20 28 70 4b 65 79 2c 6e  given by (pKey,n
130a9 4b 65 79 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20  Key).** and the 
130aa 64 61 74 61 20 69 73 20 67 69 76 65 6e 20 62 79  data is given by
130ab 20 28 70 44 61 74 61 2c 6e 44 61 74 61 29 2e 20   (pData,nData). 
130ac 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 75   The cursor is u
130ad 73 65 64 20 6f 6e 6c 79 20 74 6f 0a 2a 2a 20 64  sed only to.** d
130ae 65 66 69 6e 65 20 77 68 61 74 20 74 61 62 6c 65  efine what table
130af 20 74 68 65 20 72 65 63 6f 72 64 20 73 68 6f 75   the record shou
130b0 6c 64 20 62 65 20 69 6e 73 65 72 74 65 64 20 69  ld be inserted i
130b1 6e 74 6f 2e 20 20 54 68 65 20 63 75 72 73 6f 72  nto.  The cursor
130b2 0a 2a 2a 20 69 73 20 6c 65 66 74 20 70 6f 69 6e  .** is left poin
130b3 74 69 6e 67 20 61 74 20 61 20 72 61 6e 64 6f 6d  ting at a random
130b4 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   location..**.**
130b5 20 46 6f 72 20 61 6e 20 49 4e 54 4b 45 59 20 74   For an INTKEY t
130b6 61 62 6c 65 2c 20 6f 6e 6c 79 20 74 68 65 20 6e  able, only the n
130b7 4b 65 79 20 76 61 6c 75 65 20 6f 66 20 74 68 65  Key value of the
130b8 20 6b 65 79 20 69 73 20 75 73 65 64 2e 20 20 70   key is used.  p
130b9 4b 65 79 20 69 73 0a 2a 2a 20 69 67 6e 6f 72 65  Key is.** ignore
130ba 64 2e 20 20 46 6f 72 20 61 20 5a 45 52 4f 44 41  d.  For a ZERODA
130bb 54 41 20 74 61 62 6c 65 2c 20 74 68 65 20 70 44  TA table, the pD
130bc 61 74 61 20 61 6e 64 20 6e 44 61 74 61 20 61 72  ata and nData ar
130bd 65 20 62 6f 74 68 20 69 67 6e 6f 72 65 64 2e 0a  e both ignored..
130be 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
130bf 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
130c0 65 65 49 6e 73 65 72 74 28 0a 20 20 42 74 43 75  eeInsert(.  BtCu
130c1 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20  rsor *pCur,     
130c2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
130c3 73 65 72 74 20 64 61 74 61 20 69 6e 74 6f 20 74  sert data into t
130c4 68 65 20 74 61 62 6c 65 20 6f 66 20 74 68 69 73  he table of this
130c5 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e   cursor */.  con
130c6 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69  st void *pKey, i
130c7 36 34 20 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 54  64 nKey,    /* T
130c8 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 6e 65  he key of the ne
130c9 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 6f  w record */.  co
130ca 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c  nst void *pData,
130cb 20 69 6e 74 20 6e 44 61 74 61 2c 20 20 2f 2a 20   int nData,  /* 
130cc 54 68 65 20 64 61 74 61 20 6f 66 20 74 68 65 20  The data of the 
130cd 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  new record */.  
130ce 69 6e 74 20 6e 5a 65 72 6f 2c 20 20 20 20 20 20  int nZero,      
130cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
130d0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 74 72  * Number of extr
130d1 61 20 30 20 62 79 74 65 73 20 74 6f 20 61 70 70  a 0 bytes to app
130d2 65 6e 64 20 74 6f 20 64 61 74 61 20 2a 2f 0a 20  end to data */. 
130d3 20 69 6e 74 20 61 70 70 65 6e 64 42 69 61 73 20   int appendBias 
130d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
130d5 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20  /* True if this 
130d6 69 73 20 6c 69 6b 65 6c 79 20 61 6e 20 61 70 70  is likely an app
130d7 65 6e 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  end */.){.  int 
130d8 72 63 3b 0a 20 20 69 6e 74 20 6c 6f 63 3b 0a 20  rc;.  int loc;. 
130d9 20 69 6e 74 20 73 7a 4e 65 77 3b 0a 20 20 4d 65   int szNew;.  Me
130da 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20  mPage *pPage;.  
130db 42 74 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d  Btree *p = pCur-
130dc 3e 70 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61  >pBtree;.  BtSha
130dd 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
130de 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  t;.  unsigned ch
130df 61 72 20 2a 6f 6c 64 43 65 6c 6c 3b 0a 20 20 75  ar *oldCell;.  u
130e0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 6e 65  nsigned char *ne
130e1 77 43 65 6c 6c 20 3d 20 30 3b 0a 0a 20 20 61 73  wCell = 0;..  as
130e2 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
130e3 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
130e4 20 20 69 66 28 20 70 42 74 2d 3e 69 6e 54 72 61    if( pBt->inTra
130e5 6e 73 61 63 74 69 6f 6e 21 3d 54 52 41 4e 53 5f  nsaction!=TRANS_
130e6 57 52 49 54 45 20 29 7b 0a 20 20 20 20 2f 2a 20  WRITE ){.    /* 
130e7 4d 75 73 74 20 73 74 61 72 74 20 61 20 74 72 61  Must start a tra
130e8 6e 73 61 63 74 69 6f 6e 20 62 65 66 6f 72 65 20  nsaction before 
130e9 64 6f 69 6e 67 20 61 6e 20 69 6e 73 65 72 74 20  doing an insert 
130ea 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 42 74 2d  */.    rc = pBt-
130eb 3e 72 65 61 64 4f 6e 6c 79 20 3f 20 53 51 4c 49  >readOnly ? SQLI
130ec 54 45 5f 52 45 41 44 4f 4e 4c 59 20 3a 20 53 51  TE_READONLY : SQ
130ed 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
130ee 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
130ef 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 72   assert( !pBt->r
130f0 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 69 66 28  eadOnly );.  if(
130f1 20 21 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 29   !pCur->wrFlag )
130f2 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
130f3 49 54 45 5f 50 45 52 4d 3b 20 20 20 2f 2a 20 43  ITE_PERM;   /* C
130f4 75 72 73 6f 72 20 6e 6f 74 20 6f 70 65 6e 20 66  ursor not open f
130f5 6f 72 20 77 72 69 74 69 6e 67 20 2a 2f 0a 20 20  or writing */.  
130f6 7d 0a 20 20 69 66 28 20 63 68 65 63 6b 52 65 61  }.  if( checkRea
130f7 64 4c 6f 63 6b 73 28 70 43 75 72 2d 3e 70 42 74  dLocks(pCur->pBt
130f8 72 65 65 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52  ree, pCur->pgnoR
130f9 6f 6f 74 2c 20 70 43 75 72 29 20 29 7b 0a 20 20  oot, pCur) ){.  
130fa 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
130fb 4c 4f 43 4b 45 44 3b 20 2f 2a 20 54 68 65 20 74  LOCKED; /* The t
130fc 61 62 6c 65 20 70 43 75 72 20 70 6f 69 6e 74 73  able pCur points
130fd 20 74 6f 20 68 61 73 20 61 20 72 65 61 64 20 6c   to has a read l
130fe 6f 63 6b 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28  ock */.  }.  if(
130ff 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
13100 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20  URSOR_FAULT ){. 
13101 20 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d 3e     return pCur->
13102 73 6b 69 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  skip;.  }..  /* 
13103 53 61 76 65 20 74 68 65 20 70 6f 73 69 74 69 6f  Save the positio
13104 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 20  ns of any other 
13105 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20  cursors open on 
13106 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20  this table */.  
13107 63 6c 65 61 72 43 75 72 73 6f 72 50 6f 73 69 74  clearCursorPosit
13108 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28  ion(pCur);.  if(
13109 20 0a 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21   .    SQLITE_OK!
1310a 3d 28 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75  =(rc = saveAllCu
1310b 72 73 6f 72 73 28 70 42 74 2c 20 70 43 75 72 2d  rsors(pBt, pCur-
1310c 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29  >pgnoRoot, pCur)
1310d 29 20 7c 7c 0a 20 20 20 20 53 51 4c 49 54 45 5f  ) ||.    SQLITE_
1310e 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65  OK!=(rc = sqlite
1310f 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75  3BtreeMoveto(pCu
13110 72 2c 20 70 4b 65 79 2c 20 30 2c 20 6e 4b 65 79  r, pKey, 0, nKey
13111 2c 20 61 70 70 65 6e 64 42 69 61 73 2c 20 26 6c  , appendBias, &l
13112 6f 63 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65  oc)).  ){.    re
13113 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
13114 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 70 50  pPage = pCur->pP
13115 61 67 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70  age;.  assert( p
13116 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20  Page->intKey || 
13117 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 61 73 73  nKey>=0 );.  ass
13118 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66  ert( pPage->leaf
13119 20 7c 7c 20 21 70 50 61 67 65 2d 3e 6c 65 61 66   || !pPage->leaf
1311a 44 61 74 61 20 29 3b 0a 20 20 54 52 41 43 45 28  Data );.  TRACE(
1311b 28 22 49 4e 53 45 52 54 3a 20 74 61 62 6c 65 3d  ("INSERT: table=
1311c 25 64 20 6e 6b 65 79 3d 25 6c 6c 64 20 6e 64 61  %d nkey=%lld nda
1311d 74 61 3d 25 64 20 70 61 67 65 3d 25 64 20 25 73  ta=%d page=%d %s
1311e 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70  \n",.          p
1311f 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 6e  Cur->pgnoRoot, n
13120 4b 65 79 2c 20 6e 44 61 74 61 2c 20 70 50 61 67  Key, nData, pPag
13121 65 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20  e->pgno,.       
13122 20 20 20 6c 6f 63 3d 3d 30 20 3f 20 22 6f 76 65     loc==0 ? "ove
13123 72 77 72 69 74 65 22 20 3a 20 22 6e 65 77 20 65  rwrite" : "new e
13124 6e 74 72 79 22 29 29 3b 0a 20 20 61 73 73 65 72  ntry"));.  asser
13125 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74  t( pPage->isInit
13126 20 29 3b 0a 20 20 61 6c 6c 6f 63 61 74 65 54 65   );.  allocateTe
13127 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20  mpSpace(pBt);.  
13128 6e 65 77 43 65 6c 6c 20 3d 20 70 42 74 2d 3e 70  newCell = pBt->p
13129 54 6d 70 53 70 61 63 65 3b 0a 20 20 69 66 28 20  TmpSpace;.  if( 
1312a 6e 65 77 43 65 6c 6c 3d 3d 30 20 29 20 72 65 74  newCell==0 ) ret
1312b 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
1312c 3b 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e 43  ;.  rc = fillInC
1312d 65 6c 6c 28 70 50 61 67 65 2c 20 6e 65 77 43 65  ell(pPage, newCe
1312e 6c 6c 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20  ll, pKey, nKey, 
1312f 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 6e 5a  pData, nData, nZ
13130 65 72 6f 2c 20 26 73 7a 4e 65 77 29 3b 0a 20 20  ero, &szNew);.  
13131 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e  if( rc ) goto en
13132 64 5f 69 6e 73 65 72 74 3b 0a 20 20 61 73 73 65  d_insert;.  asse
13133 72 74 28 20 73 7a 4e 65 77 3d 3d 63 65 6c 6c 53  rt( szNew==cellS
13134 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 6e 65  izePtr(pPage, ne
13135 77 43 65 6c 6c 29 20 29 3b 0a 20 20 61 73 73 65  wCell) );.  asse
13136 72 74 28 20 73 7a 4e 65 77 3c 3d 4d 58 5f 43 45  rt( szNew<=MX_CE
13137 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 3b 0a  LL_SIZE(pBt) );.
13138 20 20 69 66 28 20 6c 6f 63 3d 3d 30 20 26 26 20    if( loc==0 && 
13139 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43  CURSOR_VALID==pC
1313a 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 20  ur->eState ){.  
1313b 20 20 75 31 36 20 73 7a 4f 6c 64 3b 0a 20 20 20    u16 szOld;.   
1313c 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69   assert( pCur->i
1313d 64 78 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 69  dx>=0 && pCur->i
1313e 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20  dx<pPage->nCell 
1313f 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
13140 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
13141 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  age->pDbPage);. 
13142 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
13143 20 20 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65     goto end_inse
13144 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6f 6c  rt;.    }.    ol
13145 64 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c  dCell = findCell
13146 28 70 50 61 67 65 2c 20 70 43 75 72 2d 3e 69 64  (pPage, pCur->id
13147 78 29 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61  x);.    if( !pPa
13148 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20  ge->leaf ){.    
13149 20 20 6d 65 6d 63 70 79 28 6e 65 77 43 65 6c 6c    memcpy(newCell
1314a 2c 20 6f 6c 64 43 65 6c 6c 2c 20 34 29 3b 0a 20  , oldCell, 4);. 
1314b 20 20 20 7d 0a 20 20 20 20 73 7a 4f 6c 64 20 3d     }.    szOld =
1314c 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61   cellSizePtr(pPa
1314d 67 65 2c 20 6f 6c 64 43 65 6c 6c 29 3b 0a 20 20  ge, oldCell);.  
1314e 20 20 72 63 20 3d 20 63 6c 65 61 72 43 65 6c 6c    rc = clearCell
1314f 28 70 50 61 67 65 2c 20 6f 6c 64 43 65 6c 6c 29  (pPage, oldCell)
13150 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67  ;.    if( rc ) g
13151 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a  oto end_insert;.
13152 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 50 61      dropCell(pPa
13153 67 65 2c 20 70 43 75 72 2d 3e 69 64 78 2c 20 73  ge, pCur->idx, s
13154 7a 4f 6c 64 29 3b 0a 20 20 7d 65 6c 73 65 20 69  zOld);.  }else i
13155 66 28 20 6c 6f 63 3c 30 20 26 26 20 70 50 61 67  f( loc<0 && pPag
13156 65 2d 3e 6e 43 65 6c 6c 3e 30 20 29 7b 0a 20 20  e->nCell>0 ){.  
13157 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
13158 3e 6c 65 61 66 20 29 3b 0a 20 20 20 20 70 43 75  >leaf );.    pCu
13159 72 2d 3e 69 64 78 2b 2b 3b 0a 20 20 20 20 70 43  r->idx++;.    pC
1315a 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d  ur->info.nSize =
1315b 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 61   0;.    pCur->va
1315c 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d  lidNKey = 0;.  }
1315d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
1315e 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b  ( pPage->leaf );
1315f 0a 20 20 7d 0a 20 20 72 63 20 3d 20 69 6e 73 65  .  }.  rc = inse
13160 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43  rtCell(pPage, pC
13161 75 72 2d 3e 69 64 78 2c 20 6e 65 77 43 65 6c 6c  ur->idx, newCell
13162 2c 20 73 7a 4e 65 77 2c 20 30 2c 20 30 29 3b 0a  , szNew, 0, 0);.
13163 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
13164 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 69  _OK ) goto end_i
13165 6e 73 65 72 74 3b 0a 20 20 72 63 20 3d 20 62 61  nsert;.  rc = ba
13166 6c 61 6e 63 65 28 70 50 61 67 65 2c 20 31 29 3b  lance(pPage, 1);
13167 0a 20 20 2f 2a 20 73 71 6c 69 74 65 33 42 74 72  .  /* sqlite3Btr
13168 65 65 50 61 67 65 44 75 6d 70 28 70 43 75 72 2d  eePageDump(pCur-
13169 3e 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 6e 6f  >pBt, pCur->pgno
1316a 52 6f 6f 74 2c 20 31 29 3b 20 2a 2f 0a 20 20 2f  Root, 1); */.  /
1316b 2a 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29  * fflush(stdout)
1316c 3b 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53  ; */.  if( rc==S
1316d 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1316e 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29  moveToRoot(pCur)
1316f 3b 0a 20 20 7d 0a 65 6e 64 5f 69 6e 73 65 72 74  ;.  }.end_insert
13170 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  :.  return rc;.}
13171 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74  ../*.** Delete t
13172 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 68  he entry that th
13173 65 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e  e cursor is poin
13174 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 20 63 75  ting to.  The cu
13175 72 73 6f 72 0a 2a 2a 20 69 73 20 6c 65 66 74 20  rsor.** is left 
13176 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 72 61  pointing at a ra
13177 6e 64 6f 6d 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a  ndom location..*
13178 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
13179 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
1317a 65 44 65 6c 65 74 65 28 42 74 43 75 72 73 6f 72  eDelete(BtCursor
1317b 20 2a 70 43 75 72 29 7b 0a 20 20 4d 65 6d 50 61   *pCur){.  MemPa
1317c 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72  ge *pPage = pCur
1317d 2d 3e 70 50 61 67 65 3b 0a 20 20 75 6e 73 69 67  ->pPage;.  unsig
1317e 6e 65 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 3b  ned char *pCell;
1317f 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 67 6e  .  int rc;.  Pgn
13180 6f 20 70 67 6e 6f 43 68 69 6c 64 20 3d 20 30 3b  o pgnoChild = 0;
13181 0a 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 43  .  Btree *p = pC
13182 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 42 74  ur->pBtree;.  Bt
13183 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
13184 3e 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28  >pBt;..  assert(
13185 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
13186 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73  x(pCur) );.  ass
13187 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e  ert( pPage->isIn
13188 69 74 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d  it );.  if( pBt-
13189 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d  >inTransaction!=
1318a 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20  TRANS_WRITE ){. 
1318b 20 20 20 2f 2a 20 4d 75 73 74 20 73 74 61 72 74     /* Must start
1318c 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62   a transaction b
1318d 65 66 6f 72 65 20 64 6f 69 6e 67 20 61 20 64 65  efore doing a de
1318e 6c 65 74 65 20 2a 2f 0a 20 20 20 20 72 63 20 3d  lete */.    rc =
1318f 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3f   pBt->readOnly ?
13190 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59   SQLITE_READONLY
13191 20 3a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   : SQLITE_ERROR;
13192 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
13193 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 70    }.  assert( !p
13194 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a  Bt->readOnly );.
13195 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61    if( pCur->eSta
13196 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54  te==CURSOR_FAULT
13197 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70   ){.    return p
13198 43 75 72 2d 3e 73 6b 69 70 3b 0a 20 20 7d 0a 20  Cur->skip;.  }. 
13199 20 69 66 28 20 70 43 75 72 2d 3e 69 64 78 20 3e   if( pCur->idx >
1319a 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29  = pPage->nCell )
1319b 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
1319c 49 54 45 5f 45 52 52 4f 52 3b 20 20 2f 2a 20 54  ITE_ERROR;  /* T
1319d 68 65 20 63 75 72 73 6f 72 20 69 73 20 6e 6f 74  he cursor is not
1319e 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 6e 79   pointing to any
1319f 74 68 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 69  thing */.  }.  i
131a0 66 28 20 21 70 43 75 72 2d 3e 77 72 46 6c 61 67  f( !pCur->wrFlag
131a1 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
131a2 51 4c 49 54 45 5f 50 45 52 4d 3b 20 20 20 2f 2a  QLITE_PERM;   /*
131a3 20 44 69 64 20 6e 6f 74 20 6f 70 65 6e 20 74 68   Did not open th
131a4 69 73 20 63 75 72 73 6f 72 20 66 6f 72 20 77 72  is cursor for wr
131a5 69 74 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 69  iting */.  }.  i
131a6 66 28 20 63 68 65 63 6b 52 65 61 64 4c 6f 63 6b  f( checkReadLock
131a7 73 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2c 20  s(pCur->pBtree, 
131a8 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20  pCur->pgnoRoot, 
131a9 70 43 75 72 29 20 29 7b 0a 20 20 20 20 72 65 74  pCur) ){.    ret
131aa 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  urn SQLITE_LOCKE
131ab 44 3b 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20  D; /* The table 
131ac 70 43 75 72 20 70 6f 69 6e 74 73 20 74 6f 20 68  pCur points to h
131ad 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 2a  as a read lock *
131ae 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 73 74  /.  }..  /* Rest
131af 6f 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ore the current 
131b0 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20  cursor position 
131b1 28 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20  (a no-op if the 
131b2 63 75 72 73 6f 72 20 69 73 20 6e 6f 74 20 69 6e  cursor is not in
131b3 20 0a 20 20 2a 2a 20 43 55 52 53 4f 52 5f 52 45   .  ** CURSOR_RE
131b4 51 55 49 52 45 53 45 45 4b 20 73 74 61 74 65 29  QUIRESEEK state)
131b5 20 61 6e 64 20 73 61 76 65 20 74 68 65 20 70 6f   and save the po
131b6 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e 79 20 6f  sitions of any o
131b7 74 68 65 72 20 63 75 72 73 6f 72 73 20 0a 20 20  ther cursors .  
131b8 2a 2a 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73  ** open on the s
131b9 61 6d 65 20 74 61 62 6c 65 2e 20 54 68 65 6e 20  ame table. Then 
131ba 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65  call sqlite3Page
131bb 72 57 72 69 74 65 28 29 20 6f 6e 20 74 68 65 20  rWrite() on the 
131bc 70 61 67 65 0a 20 20 2a 2a 20 74 68 61 74 20 74  page.  ** that t
131bd 68 65 20 65 6e 74 72 79 20 77 69 6c 6c 20 62 65  he entry will be
131be 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 2e 0a 20   deleted from.. 
131bf 20 2a 2f 0a 20 20 69 66 28 20 0a 20 20 20 20 28   */.  if( .    (
131c0 72 63 20 3d 20 72 65 73 74 6f 72 65 4f 72 43 6c  rc = restoreOrCl
131c1 65 61 72 43 75 72 73 6f 72 50 6f 73 69 74 69 6f  earCursorPositio
131c2 6e 28 70 43 75 72 29 29 21 3d 30 20 7c 7c 0a 20  n(pCur))!=0 ||. 
131c3 20 20 20 28 72 63 20 3d 20 73 61 76 65 41 6c 6c     (rc = saveAll
131c4 43 75 72 73 6f 72 73 28 70 42 74 2c 20 70 43 75  Cursors(pBt, pCu
131c5 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75  r->pgnoRoot, pCu
131c6 72 29 29 21 3d 30 20 7c 7c 0a 20 20 20 20 28 72  r))!=0 ||.    (r
131c7 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
131c8 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62  Write(pPage->pDb
131c9 50 61 67 65 29 29 21 3d 30 0a 20 20 29 7b 0a 20  Page))!=0.  ){. 
131ca 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
131cb 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74  }..  /* Locate t
131cc 68 65 20 63 65 6c 6c 20 77 69 74 68 69 6e 20 69  he cell within i
131cd 74 73 20 70 61 67 65 20 61 6e 64 20 6c 65 61 76  ts page and leav
131ce 65 20 70 43 65 6c 6c 20 70 6f 69 6e 74 69 6e 67  e pCell pointing
131cf 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 64 61 74   to the.  ** dat
131d0 61 2e 20 54 68 65 20 63 6c 65 61 72 43 65 6c 6c  a. The clearCell
131d1 28 29 20 63 61 6c 6c 20 66 72 65 65 73 20 61 6e  () call frees an
131d2 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  y overflow pages
131d3 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
131d4 20 74 68 65 0a 20 20 2a 2a 20 63 65 6c 6c 2e 20   the.  ** cell. 
131d5 54 68 65 20 63 65 6c 6c 20 69 74 73 65 6c 66 20  The cell itself 
131d6 69 73 20 73 74 69 6c 6c 20 69 6e 74 61 63 74 2e  is still intact.
131d7 0a 20 20 2a 2f 0a 20 20 70 43 65 6c 6c 20 3d 20  .  */.  pCell = 
131d8 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20  findCell(pPage, 
131d9 70 43 75 72 2d 3e 69 64 78 29 3b 0a 20 20 69 66  pCur->idx);.  if
131da 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  ( !pPage->leaf )
131db 7b 0a 20 20 20 20 70 67 6e 6f 43 68 69 6c 64 20  {.    pgnoChild 
131dc 3d 20 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c  = get4byte(pCell
131dd 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 63 6c  );.  }.  rc = cl
131de 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 70  earCell(pPage, p
131df 43 65 6c 6c 29 3b 0a 20 20 69 66 28 20 72 63 20  Cell);.  if( rc 
131e0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
131e1 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50  ;.  }..  if( !pP
131e2 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
131e3 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 65   /*.    ** The e
131e4 6e 74 72 79 20 77 65 20 61 72 65 20 61 62 6f 75  ntry we are abou
131e5 74 20 74 6f 20 64 65 6c 65 74 65 20 69 73 20 6e  t to delete is n
131e6 6f 74 20 61 20 6c 65 61 66 20 73 6f 20 69 66 20  ot a leaf so if 
131e7 77 65 20 64 6f 20 6e 6f 74 0a 20 20 20 20 2a 2a  we do not.    **
131e8 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77 65   do something we
131e9 20 77 69 6c 6c 20 6c 65 61 76 65 20 61 20 68 6f   will leave a ho
131ea 6c 65 20 6f 6e 20 61 6e 20 69 6e 74 65 72 6e 61  le on an interna
131eb 6c 20 70 61 67 65 2e 0a 20 20 20 20 2a 2a 20 57  l page..    ** W
131ec 65 20 68 61 76 65 20 74 6f 20 66 69 6c 6c 20 74  e have to fill t
131ed 68 65 20 68 6f 6c 65 20 62 79 20 6d 6f 76 69 6e  he hole by movin
131ee 67 20 69 6e 20 61 20 63 65 6c 6c 20 66 72 6f 6d  g in a cell from
131ef 20 61 20 6c 65 61 66 2e 20 20 54 68 65 0a 20 20   a leaf.  The.  
131f0 20 20 2a 2a 20 6e 65 78 74 20 43 65 6c 6c 20 61    ** next Cell a
131f1 66 74 65 72 20 74 68 65 20 6f 6e 65 20 74 6f 20  fter the one to 
131f2 62 65 20 64 65 6c 65 74 65 64 20 69 73 20 67 75  be deleted is gu
131f3 61 72 61 6e 74 65 65 64 20 74 6f 20 65 78 69 73  aranteed to exis
131f4 74 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74 6f 20  t and.    ** to 
131f5 62 65 20 61 20 6c 65 61 66 20 73 6f 20 77 65 20  be a leaf so we 
131f6 63 61 6e 20 75 73 65 20 69 74 2e 0a 20 20 20 20  can use it..    
131f7 2a 2f 0a 20 20 20 20 42 74 43 75 72 73 6f 72 20  */.    BtCursor 
131f8 6c 65 61 66 43 75 72 3b 0a 20 20 20 20 75 6e 73  leafCur;.    uns
131f9 69 67 6e 65 64 20 63 68 61 72 20 2a 70 4e 65 78  igned char *pNex
131fa 74 3b 0a 20 20 20 20 69 6e 74 20 6e 6f 74 55 73  t;.    int notUs
131fb 65 64 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64  ed;.    unsigned
131fc 20 63 68 61 72 20 2a 74 65 6d 70 43 65 6c 6c 20   char *tempCell 
131fd 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28  = 0;.    assert(
131fe 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 44 61 74   !pPage->leafDat
131ff 61 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  a );.    sqlite3
13200 42 74 72 65 65 47 65 74 54 65 6d 70 43 75 72 73  BtreeGetTempCurs
13201 6f 72 28 70 43 75 72 2c 20 26 6c 65 61 66 43 75  or(pCur, &leafCu
13202 72 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  r);.    rc = sql
13203 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 26 6c  ite3BtreeNext(&l
13204 65 61 66 43 75 72 2c 20 26 6e 6f 74 55 73 65 64  eafCur, &notUsed
13205 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
13206 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
13207 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
13208 67 65 72 57 72 69 74 65 28 6c 65 61 66 43 75 72  gerWrite(leafCur
13209 2e 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  .pPage->pDbPage)
1320a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1320b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1320c 0a 20 20 20 20 20 20 75 31 36 20 73 7a 4e 65 78  .      u16 szNex
1320d 74 3b 0a 20 20 20 20 20 20 54 52 41 43 45 28 28  t;.      TRACE((
1320e 22 44 45 4c 45 54 45 3a 20 74 61 62 6c 65 3d 25  "DELETE: table=%
1320f 64 20 64 65 6c 65 74 65 20 69 6e 74 65 72 6e 61  d delete interna
13210 6c 20 66 72 6f 6d 20 25 64 20 72 65 70 6c 61 63  l from %d replac
13211 65 20 66 72 6f 6d 20 6c 65 61 66 20 25 64 5c 6e  e from leaf %d\n
13212 22 2c 0a 20 20 20 20 20 20 20 20 20 70 43 75 72  ",.         pCur
13213 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 50 61 67  ->pgnoRoot, pPag
13214 65 2d 3e 70 67 6e 6f 2c 20 6c 65 61 66 43 75 72  e->pgno, leafCur
13215 2e 70 50 61 67 65 2d 3e 70 67 6e 6f 29 29 3b 0a  .pPage->pgno));.
13216 20 20 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70        dropCell(p
13217 50 61 67 65 2c 20 70 43 75 72 2d 3e 69 64 78 2c  Page, pCur->idx,
13218 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61   cellSizePtr(pPa
13219 67 65 2c 20 70 43 65 6c 6c 29 29 3b 0a 20 20 20  ge, pCell));.   
1321a 20 20 20 70 4e 65 78 74 20 3d 20 66 69 6e 64 43     pNext = findC
1321b 65 6c 6c 28 6c 65 61 66 43 75 72 2e 70 50 61 67  ell(leafCur.pPag
1321c 65 2c 20 6c 65 61 66 43 75 72 2e 69 64 78 29 3b  e, leafCur.idx);
1321d 0a 20 20 20 20 20 20 73 7a 4e 65 78 74 20 3d 20  .      szNext = 
1321e 63 65 6c 6c 53 69 7a 65 50 74 72 28 6c 65 61 66  cellSizePtr(leaf
1321f 43 75 72 2e 70 50 61 67 65 2c 20 70 4e 65 78 74  Cur.pPage, pNext
13220 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
13221 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42   MX_CELL_SIZE(pB
13222 74 29 3e 3d 73 7a 4e 65 78 74 2b 34 20 29 3b 0a  t)>=szNext+4 );.
13223 20 20 20 20 20 20 61 6c 6c 6f 63 61 74 65 54 65        allocateTe
13224 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20  mpSpace(pBt);.  
13225 20 20 20 20 74 65 6d 70 43 65 6c 6c 20 3d 20 70      tempCell = p
13226 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20  Bt->pTmpSpace;. 
13227 20 20 20 20 20 69 66 28 20 74 65 6d 70 43 65 6c       if( tempCel
13228 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  l==0 ){.        
13229 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
1322a 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  M;.      }.     
1322b 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1322c 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
1322d 20 3d 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50   = insertCell(pP
1322e 61 67 65 2c 20 70 43 75 72 2d 3e 69 64 78 2c 20  age, pCur->idx, 
1322f 70 4e 65 78 74 2d 34 2c 20 73 7a 4e 65 78 74 2b  pNext-4, szNext+
13230 34 2c 20 74 65 6d 70 43 65 6c 6c 2c 20 30 29 3b  4, tempCell, 0);
13231 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
13232 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
13233 20 29 7b 0a 20 20 20 20 20 20 20 20 70 75 74 34   ){.        put4
13234 62 79 74 65 28 66 69 6e 64 4f 76 65 72 66 6c 6f  byte(findOverflo
13235 77 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 75  wCell(pPage, pCu
13236 72 2d 3e 69 64 78 29 2c 20 70 67 6e 6f 43 68 69  r->idx), pgnoChi
13237 6c 64 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  ld);.        rc 
13238 3d 20 62 61 6c 61 6e 63 65 28 70 50 61 67 65 2c  = balance(pPage,
13239 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   0);.      }.   
1323a 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
1323b 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1323c 64 72 6f 70 43 65 6c 6c 28 6c 65 61 66 43 75 72  dropCell(leafCur
1323d 2e 70 50 61 67 65 2c 20 6c 65 61 66 43 75 72 2e  .pPage, leafCur.
1323e 69 64 78 2c 20 73 7a 4e 65 78 74 29 3b 0a 20 20  idx, szNext);.  
1323f 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e        rc = balan
13240 63 65 28 6c 65 61 66 43 75 72 2e 70 50 61 67 65  ce(leafCur.pPage
13241 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 0);.      }.  
13242 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42    }.    sqlite3B
13243 74 72 65 65 52 65 6c 65 61 73 65 54 65 6d 70 43  treeReleaseTempC
13244 75 72 73 6f 72 28 26 6c 65 61 66 43 75 72 29 3b  ursor(&leafCur);
13245 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 54 52  .  }else{.    TR
13246 41 43 45 28 28 22 44 45 4c 45 54 45 3a 20 74 61  ACE(("DELETE: ta
13247 62 6c 65 3d 25 64 20 64 65 6c 65 74 65 20 66 72  ble=%d delete fr
13248 6f 6d 20 6c 65 61 66 20 25 64 5c 6e 22 2c 0a 20  om leaf %d\n",. 
13249 20 20 20 20 20 20 70 43 75 72 2d 3e 70 67 6e 6f        pCur->pgno
1324a 52 6f 6f 74 2c 20 70 50 61 67 65 2d 3e 70 67 6e  Root, pPage->pgn
1324b 6f 29 29 3b 0a 20 20 20 20 64 72 6f 70 43 65 6c  o));.    dropCel
1324c 6c 28 70 50 61 67 65 2c 20 70 43 75 72 2d 3e 69  l(pPage, pCur->i
1324d 64 78 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72 28  dx, cellSizePtr(
1324e 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 29 3b 0a  pPage, pCell));.
1324f 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65      rc = balance
13250 28 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 7d 0a  (pPage, 0);.  }.
13251 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
13252 5f 4f 4b 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54  _OK ){.    moveT
13253 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d  oRoot(pCur);.  }
13254 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
13255 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
13256 6e 65 77 20 42 54 72 65 65 20 74 61 62 6c 65 2e  new BTree table.
13257 20 20 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 69    Write into *pi
13258 54 61 62 6c 65 20 74 68 65 20 70 61 67 65 0a 2a  Table the page.*
13259 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  * number for the
1325a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68   root page of th
1325b 65 20 6e 65 77 20 74 61 62 6c 65 2e 0a 2a 2a 0a  e new table..**.
1325c 2a 2a 20 54 68 65 20 74 79 70 65 20 6f 66 20 74  ** The type of t
1325d 79 70 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65  ype is determine
1325e 64 20 62 79 20 74 68 65 20 66 6c 61 67 73 20 70  d by the flags p
1325f 61 72 61 6d 65 74 65 72 2e 20 20 4f 6e 6c 79 20  arameter.  Only 
13260 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67  the.** following
13261 20 76 61 6c 75 65 73 20 6f 66 20 66 6c 61 67 73   values of flags
13262 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 69   are currently i
13263 6e 20 75 73 65 2e 20 20 4f 74 68 65 72 20 76 61  n use.  Other va
13264 6c 75 65 73 20 66 6f 72 0a 2a 2a 20 66 6c 61 67  lues for.** flag
13265 73 20 6d 69 67 68 74 20 6e 6f 74 20 77 6f 72 6b  s might not work
13266 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 42 54 52 45  :.**.**     BTRE
13267 45 5f 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f 4c  E_INTKEY|BTREE_L
13268 45 41 46 44 41 54 41 20 20 20 20 20 55 73 65 64  EAFDATA     Used
13269 20 66 6f 72 20 53 51 4c 20 74 61 62 6c 65 73 20   for SQL tables 
1326a 77 69 74 68 20 72 6f 77 69 64 20 6b 65 79 73 0a  with rowid keys.
1326b 2a 2a 20 20 20 20 20 42 54 52 45 45 5f 5a 45 52  **     BTREE_ZER
1326c 4f 44 41 54 41 20 20 20 20 20 20 20 20 20 20 20  ODATA           
1326d 20 20 20 20 20 20 20 55 73 65 64 20 66 6f 72 20         Used for 
1326e 53 51 4c 20 69 6e 64 69 63 65 73 0a 2a 2f 0a 73  SQL indices.*/.s
1326f 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 43  tatic int btreeC
13270 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65  reateTable(Btree
13271 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c   *p, int *piTabl
13272 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20  e, int flags){. 
13273 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
13274 20 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61   p->pBt;.  MemPa
13275 67 65 20 2a 70 52 6f 6f 74 3b 0a 20 20 50 67 6e  ge *pRoot;.  Pgn
13276 6f 20 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 69 6e  o pgnoRoot;.  in
13277 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  t rc;..  assert(
13278 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
13279 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20  dsMutex(p) );.  
1327a 69 66 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73  if( pBt->inTrans
1327b 61 63 74 69 6f 6e 21 3d 54 52 41 4e 53 5f 57 52  action!=TRANS_WR
1327c 49 54 45 20 29 7b 0a 20 20 20 20 2f 2a 20 4d 75  ITE ){.    /* Mu
1327d 73 74 20 73 74 61 72 74 20 61 20 74 72 61 6e 73  st start a trans
1327e 61 63 74 69 6f 6e 20 66 69 72 73 74 20 2a 2f 0a  action first */.
1327f 20 20 20 20 72 63 20 3d 20 70 42 74 2d 3e 72 65      rc = pBt->re
13280 61 64 4f 6e 6c 79 20 3f 20 53 51 4c 49 54 45 5f  adOnly ? SQLITE_
13281 52 45 41 44 4f 4e 4c 59 20 3a 20 53 51 4c 49 54  READONLY : SQLIT
13282 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 72 65 74  E_ERROR;.    ret
13283 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73  urn rc;.  }.  as
13284 73 65 72 74 28 20 21 70 42 74 2d 3e 72 65 61 64  sert( !pBt->read
13285 4f 6e 6c 79 20 29 3b 0a 0a 23 69 66 64 65 66 20  Only );..#ifdef 
13286 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
13287 56 41 43 55 55 4d 0a 20 20 72 63 20 3d 20 61 6c  VACUUM.  rc = al
13288 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28  locateBtreePage(
13289 70 42 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67  pBt, &pRoot, &pg
1328a 6e 6f 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20  noRoot, 1, 0);. 
1328b 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72   if( rc ){.    r
1328c 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65  eturn rc;.  }.#e
1328d 6c 73 65 0a 20 20 69 66 28 20 70 42 74 2d 3e 61  lse.  if( pBt->a
1328e 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20  utoVacuum ){.   
1328f 20 50 67 6e 6f 20 70 67 6e 6f 4d 6f 76 65 3b 20   Pgno pgnoMove; 
13290 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20 61 20 70       /* Move a p
13291 61 67 65 20 68 65 72 65 20 74 6f 20 6d 61 6b 65  age here to make
13292 20 72 6f 6f 6d 20 66 6f 72 20 74 68 65 20 72 6f   room for the ro
13293 6f 74 2d 70 61 67 65 20 2a 2f 0a 20 20 20 20 4d  ot-page */.    M
13294 65 6d 50 61 67 65 20 2a 70 50 61 67 65 4d 6f 76  emPage *pPageMov
13295 65 3b 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74  e; /* The page t
13296 6f 20 6d 6f 76 65 20 74 6f 2e 20 2a 2f 0a 0a 20  o move to. */.. 
13297 20 20 20 2f 2a 20 43 72 65 61 74 69 6e 67 20 61     /* Creating a
13298 20 6e 65 77 20 74 61 62 6c 65 20 6d 61 79 20 70   new table may p
13299 72 6f 62 61 62 6c 79 20 72 65 71 75 69 72 65 20  robably require 
1329a 6d 6f 76 69 6e 67 20 61 6e 20 65 78 69 73 74 69  moving an existi
1329b 6e 67 20 64 61 74 61 62 61 73 65 0a 20 20 20 20  ng database.    
1329c 2a 2a 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20  ** to make room 
1329d 66 6f 72 20 74 68 65 20 6e 65 77 20 74 61 62 6c  for the new tabl
1329e 65 73 20 72 6f 6f 74 20 70 61 67 65 2e 20 49 6e  es root page. In
1329f 20 63 61 73 65 20 74 68 69 73 20 70 61 67 65 20   case this page 
132a0 74 75 72 6e 73 0a 20 20 20 20 2a 2a 20 6f 75 74  turns.    ** out
132a1 20 74 6f 20 62 65 20 61 6e 20 6f 76 65 72 66 6c   to be an overfl
132a2 6f 77 20 70 61 67 65 2c 20 64 65 6c 65 74 65 20  ow page, delete 
132a3 61 6c 6c 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  all overflow pag
132a4 65 2d 6d 61 70 20 63 61 63 68 65 73 0a 20 20 20  e-map caches.   
132a5 20 2a 2a 20 68 65 6c 64 20 62 79 20 6f 70 65 6e   ** held by open
132a6 20 63 75 72 73 6f 72 73 2e 0a 20 20 20 20 2a 2f   cursors..    */
132a7 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 41  .    invalidateA
132a8 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28  llOverflowCache(
132a9 70 42 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 65  pBt);..    /* Re
132aa 61 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ad the value of 
132ab 6d 65 74 61 5b 33 5d 20 66 72 6f 6d 20 74 68 65  meta[3] from the
132ac 20 64 61 74 61 62 61 73 65 20 74 6f 20 64 65 74   database to det
132ad 65 72 6d 69 6e 65 20 77 68 65 72 65 20 74 68 65  ermine where the
132ae 0a 20 20 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67  .    ** root pag
132af 65 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62  e of the new tab
132b0 6c 65 20 73 68 6f 75 6c 64 20 67 6f 2e 20 6d 65  le should go. me
132b1 74 61 5b 33 5d 20 69 73 20 74 68 65 20 6c 61 72  ta[3] is the lar
132b2 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20  gest root-page. 
132b3 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20 73 6f     ** created so
132b4 20 66 61 72 2c 20 73 6f 20 74 68 65 20 6e 65 77   far, so the new
132b5 20 72 6f 6f 74 2d 70 61 67 65 20 69 73 20 28 6d   root-page is (m
132b6 65 74 61 5b 33 5d 2b 31 29 2e 0a 20 20 20 20 2a  eta[3]+1)..    *
132b7 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
132b8 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 70  e3BtreeGetMeta(p
132b9 2c 20 34 2c 20 26 70 67 6e 6f 52 6f 6f 74 29 3b  , 4, &pgnoRoot);
132ba 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
132bb 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
132bc 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
132bd 0a 20 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b  .    pgnoRoot++;
132be 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77  ..    /* The new
132bf 20 72 6f 6f 74 2d 70 61 67 65 20 6d 61 79 20 6e   root-page may n
132c0 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20  ot be allocated 
132c1 6f 6e 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70  on a pointer-map
132c2 20 70 61 67 65 2c 20 6f 72 20 74 68 65 0a 20 20   page, or the.  
132c3 20 20 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54    ** PENDING_BYT
132c4 45 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20  E page..    */. 
132c5 20 20 20 77 68 69 6c 65 28 20 70 67 6e 6f 52 6f     while( pgnoRo
132c6 6f 74 3d 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e  ot==PTRMAP_PAGEN
132c7 4f 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 29  O(pBt, pgnoRoot)
132c8 20 7c 7c 0a 20 20 20 20 20 20 20 20 70 67 6e 6f   ||.        pgno
132c9 52 6f 6f 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59  Root==PENDING_BY
132ca 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a  TE_PAGE(pBt) ){.
132cb 20 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b        pgnoRoot++
132cc 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
132cd 72 74 28 20 70 67 6e 6f 52 6f 6f 74 3e 3d 33 20  rt( pgnoRoot>=3 
132ce 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63  );..    /* Alloc
132cf 61 74 65 20 61 20 70 61 67 65 2e 20 54 68 65 20  ate a page. The 
132d0 70 61 67 65 20 74 68 61 74 20 63 75 72 72 65 6e  page that curren
132d1 74 6c 79 20 72 65 73 69 64 65 73 20 61 74 20 70  tly resides at p
132d2 67 6e 6f 52 6f 6f 74 20 77 69 6c 6c 0a 20 20 20  gnoRoot will.   
132d3 20 2a 2a 20 62 65 20 6d 6f 76 65 64 20 74 6f 20   ** be moved to 
132d4 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 70 61  the allocated pa
132d5 67 65 20 28 75 6e 6c 65 73 73 20 74 68 65 20 61  ge (unless the a
132d6 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20 68 61  llocated page ha
132d7 70 70 65 6e 73 0a 20 20 20 20 2a 2a 20 74 6f 20  ppens.    ** to 
132d8 72 65 73 69 64 65 20 61 74 20 70 67 6e 6f 52 6f  reside at pgnoRo
132d9 6f 74 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ot)..    */.    
132da 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72  rc = allocateBtr
132db 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 50 61  eePage(pBt, &pPa
132dc 67 65 4d 6f 76 65 2c 20 26 70 67 6e 6f 4d 6f 76  geMove, &pgnoMov
132dd 65 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 31 29 3b  e, pgnoRoot, 1);
132de 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
132df 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
132e0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
132e1 0a 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 4d 6f  ..    if( pgnoMo
132e2 76 65 21 3d 70 67 6e 6f 52 6f 6f 74 20 29 7b 0a  ve!=pgnoRoot ){.
132e3 20 20 20 20 20 20 2f 2a 20 70 67 6e 6f 52 6f 6f        /* pgnoRoo
132e4 74 20 69 73 20 74 68 65 20 70 61 67 65 20 74 68  t is the page th
132e5 61 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20  at will be used 
132e6 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 61 67  for the root-pag
132e7 65 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68  e of.      ** th
132e8 65 20 6e 65 77 20 74 61 62 6c 65 20 28 61 73 73  e new table (ass
132e9 75 6d 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 64  uming an error d
132ea 69 64 20 6e 6f 74 20 6f 63 63 75 72 29 2e 20 42  id not occur). B
132eb 75 74 20 77 65 20 77 65 72 65 0a 20 20 20 20 20  ut we were.     
132ec 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 70 67   ** allocated pg
132ed 6e 6f 4d 6f 76 65 2e 20 49 66 20 72 65 71 75 69  noMove. If requi
132ee 72 65 64 20 28 69 2e 65 2e 20 69 66 20 69 74 20  red (i.e. if it 
132ef 77 61 73 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65  was not allocate
132f0 64 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 65 78  d.      ** by ex
132f1 74 65 6e 64 69 6e 67 20 74 68 65 20 66 69 6c 65  tending the file
132f2 29 2c 20 74 68 65 20 63 75 72 72 65 6e 74 20 70  ), the current p
132f3 61 67 65 20 61 74 20 70 6f 73 69 74 69 6f 6e 20  age at position 
132f4 70 67 6e 6f 4d 6f 76 65 0a 20 20 20 20 20 20 2a  pgnoMove.      *
132f5 2a 20 69 73 20 61 6c 72 65 61 64 79 20 6a 6f 75  * is already jou
132f6 72 6e 61 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2f  rnaled..      */
132f7 0a 20 20 20 20 20 20 75 38 20 65 54 79 70 65 3b  .      u8 eType;
132f8 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 50 74 72  .      Pgno iPtr
132f9 50 61 67 65 3b 0a 0a 20 20 20 20 20 20 72 65 6c  Page;..      rel
132fa 65 61 73 65 50 61 67 65 28 70 50 61 67 65 4d 6f  easePage(pPageMo
132fb 76 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4d  ve);..      /* M
132fc 6f 76 65 20 74 68 65 20 70 61 67 65 20 63 75 72  ove the page cur
132fd 72 65 6e 74 6c 79 20 61 74 20 70 67 6e 6f 52 6f  rently at pgnoRo
132fe 6f 74 20 74 6f 20 70 67 6e 6f 4d 6f 76 65 2e 20  ot to pgnoMove. 
132ff 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  */.      rc = sq
13300 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
13301 65 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c  e(pBt, pgnoRoot,
13302 20 26 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20   &pRoot, 0);.   
13303 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
13304 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
13305 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
13306 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74   }.      rc = pt
13307 72 6d 61 70 47 65 74 28 70 42 74 2c 20 70 67 6e  rmapGet(pBt, pgn
13308 6f 52 6f 6f 74 2c 20 26 65 54 79 70 65 2c 20 26  oRoot, &eType, &
13309 69 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 20  iPtrPage);.     
1330a 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1330b 4f 4b 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52  OK || eType==PTR
1330c 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 7c 7c 20  MAP_ROOTPAGE || 
1330d 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52  eType==PTRMAP_FR
1330e 45 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20  EEPAGE ){.      
1330f 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52    releasePage(pR
13310 6f 6f 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65  oot);.        re
13311 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
13312 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65  .      assert( e
13313 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f 4f  Type!=PTRMAP_ROO
13314 54 50 41 47 45 20 29 3b 0a 20 20 20 20 20 20 61  TPAGE );.      a
13315 73 73 65 72 74 28 20 65 54 79 70 65 21 3d 50 54  ssert( eType!=PT
13316 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 3b  RMAP_FREEPAGE );
13317 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
13318 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 52  te3PagerWrite(pR
13319 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  oot->pDbPage);. 
1331a 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
1331b 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1331c 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52    releasePage(pR
1331d 6f 6f 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65  oot);.        re
1331e 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
1331f 0a 20 20 20 20 20 20 72 63 20 3d 20 72 65 6c 6f  .      rc = relo
13320 63 61 74 65 50 61 67 65 28 70 42 74 2c 20 70 52  catePage(pBt, pR
13321 6f 6f 74 2c 20 65 54 79 70 65 2c 20 69 50 74 72  oot, eType, iPtr
13322 50 61 67 65 2c 20 70 67 6e 6f 4d 6f 76 65 29 3b  Page, pgnoMove);
13323 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61  .      releasePa
13324 67 65 28 70 52 6f 6f 74 29 3b 0a 0a 20 20 20 20  ge(pRoot);..    
13325 20 20 2f 2a 20 4f 62 74 61 69 6e 20 74 68 65 20    /* Obtain the 
13326 70 61 67 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74  page at pgnoRoot
13327 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 72 63   */.      if( rc
13328 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
13329 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
1332a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1332b 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1332c 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 70 67  eGetPage(pBt, pg
1332d 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c 20  noRoot, &pRoot, 
1332e 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  0);.      if( rc
1332f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
13330 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
13331 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
13332 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
13333 72 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44  rWrite(pRoot->pD
13334 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66  bPage);.      if
13335 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
13336 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61  ){.        relea
13337 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20  sePage(pRoot);. 
13338 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
13339 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
1333a 6c 73 65 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74  lse{.      pRoot
1333b 20 3d 20 70 50 61 67 65 4d 6f 76 65 3b 0a 20 20   = pPageMove;.  
1333c 20 20 7d 20 0a 0a 20 20 20 20 2f 2a 20 55 70 64    } ..    /* Upd
1333d 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d  ate the pointer-
1333e 6d 61 70 20 61 6e 64 20 6d 65 74 61 2d 64 61 74  map and meta-dat
1333f 61 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 72  a with the new r
13340 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e  oot-page number.
13341 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 74 72   */.    rc = ptr
13342 6d 61 70 50 75 74 28 70 42 74 2c 20 70 67 6e 6f  mapPut(pBt, pgno
13343 52 6f 6f 74 2c 20 50 54 52 4d 41 50 5f 52 4f 4f  Root, PTRMAP_ROO
13344 54 50 41 47 45 2c 20 30 29 3b 0a 20 20 20 20 69  TPAGE, 0);.    i
13345 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72  f( rc ){.      r
13346 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74  eleasePage(pRoot
13347 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
13348 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  rc;.    }.    rc
13349 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 55   = sqlite3BtreeU
1334a 70 64 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20  pdateMeta(p, 4, 
1334b 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69  pgnoRoot);.    i
1334c 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72  f( rc ){.      r
1334d 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74  eleasePage(pRoot
1334e 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
1334f 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 6c  rc;.    }..  }el
13350 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c  se{.    rc = all
13351 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70  ocateBtreePage(p
13352 42 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e  Bt, &pRoot, &pgn
13353 6f 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20  oRoot, 1, 0);.  
13354 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
13355 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  n rc;.  }.#endif
13356 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
13357 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
13358 6c 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67  le(pRoot->pDbPag
13359 65 29 20 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65  e) );.  zeroPage
1335a 28 70 52 6f 6f 74 2c 20 66 6c 61 67 73 20 7c 20  (pRoot, flags | 
1335b 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 73 71 6c  PTF_LEAF);.  sql
1335c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
1335d 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a  Root->pDbPage);.
1335e 20 20 2a 70 69 54 61 62 6c 65 20 3d 20 28 69 6e    *piTable = (in
1335f 74 29 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 65  t)pgnoRoot;.  re
13360 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
13361 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
13362 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
13363 65 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 72  eCreateTable(Btr
13364 65 65 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61  ee *p, int *piTa
13365 62 6c 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b  ble, int flags){
13366 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c  .  int rc;.  sql
13367 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
13368 29 3b 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20  );.  p->pBt->db 
13369 3d 20 70 2d 3e 64 62 3b 0a 20 20 72 63 20 3d 20  = p->db;.  rc = 
1336a 62 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65  btreeCreateTable
1336b 28 70 2c 20 70 69 54 61 62 6c 65 2c 20 66 6c 61  (p, piTable, fla
1336c 67 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  gs);.  sqlite3Bt
1336d 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
1336e 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
1336f 2a 2a 20 45 72 61 73 65 20 74 68 65 20 67 69 76  ** Erase the giv
13370 65 6e 20 64 61 74 61 62 61 73 65 20 70 61 67 65  en database page
13371 20 61 6e 64 20 61 6c 6c 20 69 74 73 20 63 68 69   and all its chi
13372 6c 64 72 65 6e 2e 20 20 52 65 74 75 72 6e 0a 2a  ldren.  Return.*
13373 2a 20 74 68 65 20 70 61 67 65 20 74 6f 20 74 68  * the page to th
13374 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2f 0a 73  e freelist..*/.s
13375 74 61 74 69 63 20 69 6e 74 20 63 6c 65 61 72 44  tatic int clearD
13376 61 74 61 62 61 73 65 50 61 67 65 28 0a 20 20 42  atabasePage(.  B
13377 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20  tShared *pBt,   
13378 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42          /* The B
13379 54 72 65 65 20 74 68 61 74 20 63 6f 6e 74 61 69  Tree that contai
1337a 6e 73 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  ns the table */.
1337b 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20    Pgno pgno,    
1337c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20          /* Page 
1337d 6e 75 6d 62 65 72 20 74 6f 20 63 6c 65 61 72 20  number to clear 
1337e 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  */.  MemPage *pP
1337f 61 72 65 6e 74 2c 20 20 20 20 20 2f 2a 20 50 61  arent,     /* Pa
13380 72 65 6e 74 20 70 61 67 65 2e 20 20 4e 55 4c 4c  rent page.  NULL
13381 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 20 2a 2f   for the root */
13382 0a 20 20 69 6e 74 20 66 72 65 65 50 61 67 65 46  .  int freePageF
13383 6c 61 67 20 20 20 20 20 20 2f 2a 20 44 65 61 6c  lag      /* Deal
13384 6c 6f 63 61 74 65 20 70 61 67 65 20 69 66 20 74  locate page if t
13385 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50  rue */.){.  MemP
13386 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a  age *pPage = 0;.
13387 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 6e 73 69    int rc;.  unsi
13388 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65 6c 6c  gned char *pCell
13389 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 61 73  ;.  int i;..  as
1338a 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1338b 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
1338c 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 67  tex) );.  if( pg
1338d 6e 6f 3e 73 71 6c 69 74 65 33 50 61 67 65 72 50  no>sqlite3PagerP
1338e 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50  agecount(pBt->pP
1338f 61 67 65 72 29 20 29 7b 0a 20 20 20 20 72 65 74  ager) ){.    ret
13390 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
13391 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20  PT_BKPT;.  }..  
13392 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50  rc = getAndInitP
13393 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 2c 20 26  age(pBt, pgno, &
13394 70 50 61 67 65 2c 20 70 50 61 72 65 6e 74 29 3b  pPage, pParent);
13395 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f  .  if( rc ) goto
13396 20 63 6c 65 61 72 64 61 74 61 62 61 73 65 70 61   cleardatabasepa
13397 67 65 5f 6f 75 74 3b 0a 20 20 66 6f 72 28 69 3d  ge_out;.  for(i=
13398 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c  0; i<pPage->nCel
13399 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 43 65  l; i++){.    pCe
1339a 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50  ll = findCell(pP
1339b 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 69 66 28  age, i);.    if(
1339c 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
1339d 0a 20 20 20 20 20 20 72 63 20 3d 20 63 6c 65 61  .      rc = clea
1339e 72 44 61 74 61 62 61 73 65 50 61 67 65 28 70 42  rDatabasePage(pB
1339f 74 2c 20 67 65 74 34 62 79 74 65 28 70 43 65 6c  t, get4byte(pCel
133a0 6c 29 2c 20 70 50 61 67 65 2d 3e 70 50 61 72 65  l), pPage->pPare
133a1 6e 74 2c 20 31 29 3b 0a 20 20 20 20 20 20 69 66  nt, 1);.      if
133a2 28 20 72 63 20 29 20 67 6f 74 6f 20 63 6c 65 61  ( rc ) goto clea
133a3 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75  rdatabasepage_ou
133a4 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20  t;.    }.    rc 
133a5 3d 20 63 6c 65 61 72 43 65 6c 6c 28 70 50 61 67  = clearCell(pPag
133a6 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 69  e, pCell);.    i
133a7 66 28 20 72 63 20 29 20 67 6f 74 6f 20 63 6c 65  f( rc ) goto cle
133a8 61 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f  ardatabasepage_o
133a9 75 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70  ut;.  }.  if( !p
133aa 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  Page->leaf ){.  
133ab 20 20 72 63 20 3d 20 63 6c 65 61 72 44 61 74 61    rc = clearData
133ac 62 61 73 65 50 61 67 65 28 70 42 74 2c 20 67 65  basePage(pBt, ge
133ad 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61  t4byte(&pPage->a
133ae 44 61 74 61 5b 38 5d 29 2c 20 70 50 61 67 65 2d  Data[8]), pPage-
133af 3e 70 50 61 72 65 6e 74 2c 20 31 29 3b 0a 20 20  >pParent, 1);.  
133b0 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20    if( rc ) goto 
133b1 63 6c 65 61 72 64 61 74 61 62 61 73 65 70 61 67  cleardatabasepag
133b2 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 69 66 28  e_out;.  }.  if(
133b3 20 66 72 65 65 50 61 67 65 46 6c 61 67 20 29 7b   freePageFlag ){
133b4 0a 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61  .    rc = freePa
133b5 67 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 65 6c  ge(pPage);.  }el
133b6 73 65 20 69 66 28 20 28 72 63 20 3d 20 73 71 6c  se if( (rc = sql
133b7 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
133b8 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 29 3d  Page->pDbPage))=
133b9 3d 30 20 29 7b 0a 20 20 20 20 7a 65 72 6f 50 61  =0 ){.    zeroPa
133ba 67 65 28 70 50 61 67 65 2c 20 70 50 61 67 65 2d  ge(pPage, pPage-
133bb 3e 61 44 61 74 61 5b 30 5d 20 7c 20 50 54 46 5f  >aData[0] | PTF_
133bc 4c 45 41 46 29 3b 0a 20 20 7d 0a 0a 63 6c 65 61  LEAF);.  }..clea
133bd 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75  rdatabasepage_ou
133be 74 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65  t:.  releasePage
133bf 28 70 50 61 67 65 29 3b 0a 20 20 72 65 74 75 72  (pPage);.  retur
133c0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  n rc;.}../*.** D
133c1 65 6c 65 74 65 20 61 6c 6c 20 69 6e 66 6f 72 6d  elete all inform
133c2 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 73 69 6e  ation from a sin
133c3 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65  gle table in the
133c4 20 64 61 74 61 62 61 73 65 2e 20 20 69 54 61 62   database.  iTab
133c5 6c 65 20 69 73 0a 2a 2a 20 74 68 65 20 70 61 67  le is.** the pag
133c6 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
133c7 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c  root of the tabl
133c8 65 2e 20 20 41 66 74 65 72 20 74 68 69 73 20 72  e.  After this r
133c9 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2c 0a  outine returns,.
133ca 2a 2a 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  ** the root page
133cb 20 69 73 20 65 6d 70 74 79 2c 20 62 75 74 20 73   is empty, but s
133cc 74 69 6c 6c 20 65 78 69 73 74 73 2e 0a 2a 2a 0a  till exists..**.
133cd 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
133ce 77 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 53  will fail with S
133cf 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20  QLITE_LOCKED if 
133d0 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 6f 70  there are any op
133d1 65 6e 0a 2a 2a 20 72 65 61 64 20 63 75 72 73 6f  en.** read curso
133d2 72 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e  rs on the table.
133d3 20 20 4f 70 65 6e 20 77 72 69 74 65 20 63 75 72    Open write cur
133d4 73 6f 72 73 20 61 72 65 20 6d 6f 76 65 64 20 74  sors are moved t
133d5 6f 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 6f 66  o the.** root of
133d6 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53   the table..*/.S
133d7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
133d8 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c  t sqlite3BtreeCl
133d9 65 61 72 54 61 62 6c 65 28 42 74 72 65 65 20 2a  earTable(Btree *
133da 70 2c 20 69 6e 74 20 69 54 61 62 6c 65 29 7b 0a  p, int iTable){.
133db 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68    int rc;.  BtSh
133dc 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
133dd 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  Bt;.  sqlite3Btr
133de 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42  eeEnter(p);.  pB
133df 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20  t->db = p->db;. 
133e0 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21   if( p->inTrans!
133e1 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a  =TRANS_WRITE ){.
133e2 20 20 20 20 72 63 20 3d 20 70 42 74 2d 3e 72 65      rc = pBt->re
133e3 61 64 4f 6e 6c 79 20 3f 20 53 51 4c 49 54 45 5f  adOnly ? SQLITE_
133e4 52 45 41 44 4f 4e 4c 59 20 3a 20 53 51 4c 49 54  READONLY : SQLIT
133e5 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65  E_ERROR;.  }else
133e6 20 69 66 28 20 28 72 63 20 3d 20 63 68 65 63 6b   if( (rc = check
133e7 52 65 61 64 4c 6f 63 6b 73 28 70 2c 20 69 54 61  ReadLocks(p, iTa
133e8 62 6c 65 2c 20 30 29 29 21 3d 53 51 4c 49 54 45  ble, 0))!=SQLITE
133e9 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 6e 6f  _OK ){.    /* no
133ea 74 68 69 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a 20  thing to do */. 
133eb 20 7d 65 6c 73 65 20 69 66 28 20 53 51 4c 49 54   }else if( SQLIT
133ec 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 61 76 65  E_OK!=(rc = save
133ed 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20  AllCursors(pBt, 
133ee 69 54 61 62 6c 65 2c 20 30 29 29 20 29 7b 0a 20  iTable, 0)) ){. 
133ef 20 20 20 2f 2a 20 6e 6f 74 68 69 6e 67 20 74 6f     /* nothing to
133f0 20 64 6f 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a   do */.  }else{.
133f1 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44 61      rc = clearDa
133f2 74 61 62 61 73 65 50 61 67 65 28 70 42 74 2c 20  tabasePage(pBt, 
133f3 28 50 67 6e 6f 29 69 54 61 62 6c 65 2c 20 30 2c  (Pgno)iTable, 0,
133f4 20 30 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74   0);.  }.  sqlit
133f5 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
133f6 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
133f7 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c  ./*.** Erase all
133f8 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20   information in 
133f9 61 20 74 61 62 6c 65 20 61 6e 64 20 61 64 64 20  a table and add 
133fa 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20  the root of the 
133fb 74 61 62 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 20  table to.** the 
133fc 66 72 65 65 6c 69 73 74 2e 20 20 45 78 63 65 70  freelist.  Excep
133fd 74 2c 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74  t, the root of t
133fe 68 65 20 70 72 69 6e 63 69 70 6c 65 20 74 61 62  he principle tab
133ff 6c 65 20 28 74 68 65 20 6f 6e 65 20 6f 6e 0a 2a  le (the one on.*
13400 2a 20 70 61 67 65 20 31 29 20 69 73 20 6e 65 76  * page 1) is nev
13401 65 72 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  er added to the 
13402 66 72 65 65 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  freelist..**.** 
13403 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  This routine wil
13404 6c 20 66 61 69 6c 20 77 69 74 68 20 53 51 4c 49  l fail with SQLI
13405 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20 74 68 65  TE_LOCKED if the
13406 72 65 20 61 72 65 20 61 6e 79 20 6f 70 65 6e 0a  re are any open.
13407 2a 2a 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68  ** cursors on th
13408 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49  e table..**.** I
13409 66 20 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20  f AUTOVACUUM is 
1340a 65 6e 61 62 6c 65 64 20 61 6e 64 20 74 68 65 20  enabled and the 
1340b 70 61 67 65 20 61 74 20 69 54 61 62 6c 65 20 69  page at iTable i
1340c 73 20 6e 6f 74 20 74 68 65 20 6c 61 73 74 0a 2a  s not the last.*
1340d 2a 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74  * root page in t
1340e 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1340f 2c 20 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20  , then the last 
13410 72 6f 6f 74 20 70 61 67 65 20 0a 2a 2a 20 69 6e  root page .** in
13411 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
13412 6c 65 20 69 73 20 6d 6f 76 65 64 20 69 6e 74 6f  le is moved into
13413 20 74 68 65 20 73 6c 6f 74 20 66 6f 72 6d 65 72   the slot former
13414 6c 79 20 6f 63 63 75 70 69 65 64 20 62 79 0a 2a  ly occupied by.*
13415 2a 20 69 54 61 62 6c 65 20 61 6e 64 20 74 68 61  * iTable and tha
13416 74 20 6c 61 73 74 20 73 6c 6f 74 20 66 6f 72 6d  t last slot form
13417 65 72 6c 79 20 6f 63 63 75 70 69 65 64 20 62 79  erly occupied by
13418 20 74 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70   the last root p
13419 61 67 65 0a 2a 2a 20 69 73 20 61 64 64 65 64 20  age.** is added 
1341a 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74 20  to the freelist 
1341b 69 6e 73 74 65 61 64 20 6f 66 20 69 54 61 62 6c  instead of iTabl
1341c 65 2e 20 20 49 6e 20 74 68 69 73 20 73 61 79 2c  e.  In this say,
1341d 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 61 67   all.** root pag
1341e 65 73 20 61 72 65 20 6b 65 70 74 20 61 74 20 74  es are kept at t
1341f 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
13420 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
13421 65 2c 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 6e  e, which.** is n
13422 65 63 65 73 73 61 72 79 20 66 6f 72 20 41 55 54  ecessary for AUT
13423 4f 56 41 43 55 55 4d 20 74 6f 20 77 6f 72 6b 20  OVACUUM to work 
13424 72 69 67 68 74 2e 20 20 2a 70 69 4d 6f 76 65 64  right.  *piMoved
13425 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 0a   is set to the .
13426 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74  ** page number t
13427 68 61 74 20 75 73 65 64 20 74 6f 20 62 65 20 74  hat used to be t
13428 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67  he last root pag
13429 65 20 69 6e 20 74 68 65 20 66 69 6c 65 20 62 65  e in the file be
1342a 66 6f 72 65 0a 2a 2a 20 74 68 65 20 6d 6f 76 65  fore.** the move
1342b 2e 20 20 49 66 20 6e 6f 20 70 61 67 65 20 67 65  .  If no page ge
1342c 74 73 20 6d 6f 76 65 64 2c 20 2a 70 69 4d 6f 76  ts moved, *piMov
1342d 65 64 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a  ed is set to 0..
1342e 2a 2a 20 54 68 65 20 6c 61 73 74 20 72 6f 6f 74  ** The last root
1342f 20 70 61 67 65 20 69 73 20 72 65 63 6f 72 64 65   page is recorde
13430 64 20 69 6e 20 6d 65 74 61 5b 33 5d 20 61 6e 64  d in meta[3] and
13431 20 74 68 65 20 76 61 6c 75 65 20 6f 66 0a 2a 2a   the value of.**
13432 20 6d 65 74 61 5b 33 5d 20 69 73 20 75 70 64 61   meta[3] is upda
13433 74 65 64 20 62 79 20 74 68 69 73 20 70 72 6f 63  ted by this proc
13434 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  edure..*/.static
13435 20 69 6e 74 20 62 74 72 65 65 44 72 6f 70 54 61   int btreeDropTa
13436 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e  ble(Btree *p, in
13437 74 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70  t iTable, int *p
13438 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72  iMoved){.  int r
13439 63 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  c;.  MemPage *pP
1343a 61 67 65 20 3d 20 30 3b 0a 20 20 42 74 53 68 61  age = 0;.  BtSha
1343b 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
1343c 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  t;..  assert( sq
1343d 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d  lite3BtreeHoldsM
1343e 75 74 65 78 28 70 29 20 29 3b 0a 20 20 69 66 28  utex(p) );.  if(
1343f 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41   p->inTrans!=TRA
13440 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20  NS_WRITE ){.    
13441 72 65 74 75 72 6e 20 70 42 74 2d 3e 72 65 61 64  return pBt->read
13442 4f 6e 6c 79 20 3f 20 53 51 4c 49 54 45 5f 52 45  Only ? SQLITE_RE
13443 41 44 4f 4e 4c 59 20 3a 20 53 51 4c 49 54 45 5f  ADONLY : SQLITE_
13444 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  ERROR;.  }..  /*
13445 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74   It is illegal t
13446 6f 20 64 72 6f 70 20 61 20 74 61 62 6c 65 20 69  o drop a table i
13447 66 20 61 6e 79 20 63 75 72 73 6f 72 73 20 61 72  f any cursors ar
13448 65 20 6f 70 65 6e 20 6f 6e 20 74 68 65 0a 20 20  e open on the.  
13449 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 54 68 69  ** database. Thi
1344a 73 20 69 73 20 62 65 63 61 75 73 65 20 69 6e 20  s is because in 
1344b 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65  auto-vacuum mode
1344c 20 74 68 65 20 62 61 63 6b 65 6e 64 20 6d 61 79   the backend may
1344d 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20 6d 6f  .  ** need to mo
1344e 76 65 20 61 6e 6f 74 68 65 72 20 72 6f 6f 74 2d  ve another root-
1344f 70 61 67 65 20 74 6f 20 66 69 6c 6c 20 61 20 67  page to fill a g
13450 61 70 20 6c 65 66 74 20 62 79 20 74 68 65 20 64  ap left by the d
13451 65 6c 65 74 65 64 0a 20 20 2a 2a 20 72 6f 6f 74  eleted.  ** root
13452 20 70 61 67 65 2e 20 49 66 20 61 6e 20 6f 70 65   page. If an ope
13453 6e 20 63 75 72 73 6f 72 20 77 61 73 20 75 73 69  n cursor was usi
13454 6e 67 20 74 68 69 73 20 70 61 67 65 20 61 20 70  ng this page a p
13455 72 6f 62 6c 65 6d 20 77 6f 75 6c 64 20 0a 20 20  roblem would .  
13456 2a 2a 20 6f 63 63 75 72 2e 0a 20 20 2a 2f 0a 20  ** occur..  */. 
13457 20 69 66 28 20 70 42 74 2d 3e 70 43 75 72 73 6f   if( pBt->pCurso
13458 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  r ){.    return 
13459 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20  SQLITE_LOCKED;. 
1345a 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   }..  rc = sqlit
1345b 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70  e3BtreeGetPage(p
1345c 42 74 2c 20 28 50 67 6e 6f 29 69 54 61 62 6c 65  Bt, (Pgno)iTable
1345d 2c 20 26 70 50 61 67 65 2c 20 30 29 3b 0a 20 20  , &pPage, 0);.  
1345e 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
1345f 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  rc;.  rc = sqlit
13460 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c  e3BtreeClearTabl
13461 65 28 70 2c 20 69 54 61 62 6c 65 29 3b 0a 20 20  e(p, iTable);.  
13462 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65  if( rc ){.    re
13463 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29  leasePage(pPage)
13464 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  ;.    return rc;
13465 0a 20 20 7d 0a 0a 20 20 2a 70 69 4d 6f 76 65 64  .  }..  *piMoved
13466 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 69 54 61   = 0;..  if( iTa
13467 62 6c 65 3e 31 20 29 7b 0a 23 69 66 64 65 66 20  ble>1 ){.#ifdef 
13468 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
13469 56 41 43 55 55 4d 0a 20 20 20 20 72 63 20 3d 20  VACUUM.    rc = 
1346a 66 72 65 65 50 61 67 65 28 70 50 61 67 65 29 3b  freePage(pPage);
1346b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65  .    releasePage
1346c 28 70 50 61 67 65 29 3b 0a 23 65 6c 73 65 0a 20  (pPage);.#else. 
1346d 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f     if( pBt->auto
1346e 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20  Vacuum ){.      
1346f 50 67 6e 6f 20 6d 61 78 52 6f 6f 74 50 67 6e 6f  Pgno maxRootPgno
13470 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
13471 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61  ite3BtreeGetMeta
13472 28 70 2c 20 34 2c 20 26 6d 61 78 52 6f 6f 74 50  (p, 4, &maxRootP
13473 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20  gno);.      if( 
13474 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
13475 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
13476 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
13477 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
13478 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69        }..      i
13479 66 28 20 69 54 61 62 6c 65 3d 3d 6d 61 78 52 6f  f( iTable==maxRo
1347a 6f 74 50 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20  otPgno ){.      
1347b 20 20 2f 2a 20 49 66 20 74 68 65 20 74 61 62 6c    /* If the tabl
1347c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65 64 20  e being dropped 
1347d 69 73 20 74 68 65 20 74 61 62 6c 65 20 77 69 74  is the table wit
1347e 68 20 74 68 65 20 6c 61 72 67 65 73 74 20 72 6f  h the largest ro
1347f 6f 74 2d 70 61 67 65 0a 20 20 20 20 20 20 20 20  ot-page.        
13480 2a 2a 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65  ** number in the
13481 20 64 61 74 61 62 61 73 65 2c 20 70 75 74 20 74   database, put t
13482 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 6e 20  he root page on 
13483 74 68 65 20 66 72 65 65 20 6c 69 73 74 2e 20 0a  the free list. .
13484 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
13485 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 65     rc = freePage
13486 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  (pPage);.       
13487 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61   releasePage(pPa
13488 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ge);.        if(
13489 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1348a 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  {.          retu
1348b 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d  rn rc;.        }
1348c 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1348d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62        /* The tab
1348e 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65 64  le being dropped
1348f 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74   does not have t
13490 68 65 20 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d  he largest root-
13491 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  page.        ** 
13492 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 20 64 61  number in the da
13493 74 61 62 61 73 65 2e 20 53 6f 20 6d 6f 76 65 20  tabase. So move 
13494 74 68 65 20 70 61 67 65 20 74 68 61 74 20 64 6f  the page that do
13495 65 73 20 69 6e 74 6f 20 74 68 65 20 0a 20 20 20  es into the .   
13496 20 20 20 20 20 2a 2a 20 67 61 70 20 6c 65 66 74       ** gap left
13497 20 62 79 20 74 68 65 20 64 65 6c 65 74 65 64 20   by the deleted 
13498 72 6f 6f 74 2d 70 61 67 65 2e 0a 20 20 20 20 20  root-page..     
13499 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 4d 65     */.        Me
1349a 6d 50 61 67 65 20 2a 70 4d 6f 76 65 3b 0a 20 20  mPage *pMove;.  
1349b 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
1349c 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20  e(pPage);.      
1349d 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1349e 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
1349f 6d 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26 70 4d  maxRootPgno, &pM
134a0 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ove, 0);.       
134a1 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
134a2 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
134a3 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
134a4 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20     }.        rc 
134a5 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 70  = relocatePage(p
134a6 42 74 2c 20 70 4d 6f 76 65 2c 20 50 54 52 4d 41  Bt, pMove, PTRMA
134a7 50 5f 52 4f 4f 54 50 41 47 45 2c 20 30 2c 20 69  P_ROOTPAGE, 0, i
134a8 54 61 62 6c 65 29 3b 0a 20 20 20 20 20 20 20 20  Table);.        
134a9 72 65 6c 65 61 73 65 50 61 67 65 28 70 4d 6f 76  releasePage(pMov
134aa 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  e);.        if( 
134ab 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
134ac 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
134ad 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a  n rc;.        }.
134ae 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
134af 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
134b0 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e  (pBt, maxRootPgn
134b1 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 29 3b 0a 20  o, &pMove, 0);. 
134b2 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
134b3 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
134b4 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
134b5 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
134b6 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 65     rc = freePage
134b7 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20  (pMove);.       
134b8 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4d 6f   releasePage(pMo
134b9 76 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ve);.        if(
134ba 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
134bb 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  {.          retu
134bc 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d  rn rc;.        }
134bd 0a 20 20 20 20 20 20 20 20 2a 70 69 4d 6f 76 65  .        *piMove
134be 64 20 3d 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3b  d = maxRootPgno;
134bf 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
134c0 2f 2a 20 53 65 74 20 74 68 65 20 6e 65 77 20 27  /* Set the new '
134c1 6d 61 78 2d 72 6f 6f 74 2d 70 61 67 65 27 20 76  max-root-page' v
134c2 61 6c 75 65 20 69 6e 20 74 68 65 20 64 61 74 61  alue in the data
134c3 62 61 73 65 20 68 65 61 64 65 72 2e 20 54 68 69  base header. Thi
134c4 73 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 74 68  s.      ** is th
134c5 65 20 6f 6c 64 20 76 61 6c 75 65 20 6c 65 73 73  e old value less
134c6 20 6f 6e 65 2c 20 6c 65 73 73 20 6f 6e 65 20 6d   one, less one m
134c7 6f 72 65 20 69 66 20 74 68 61 74 20 68 61 70 70  ore if that happ
134c8 65 6e 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20  ens to.      ** 
134c9 62 65 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 6e  be a root-page n
134ca 75 6d 62 65 72 2c 20 6c 65 73 73 20 6f 6e 65 20  umber, less one 
134cb 61 67 61 69 6e 20 69 66 20 74 68 61 74 20 69 73  again if that is
134cc 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 50 45   the.      ** PE
134cd 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 2e  NDING_BYTE_PAGE.
134ce 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
134cf 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20  maxRootPgno--;. 
134d0 20 20 20 20 20 69 66 28 20 6d 61 78 52 6f 6f 74       if( maxRoot
134d1 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47 5f 42 59  Pgno==PENDING_BY
134d2 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a  TE_PAGE(pBt) ){.
134d3 20 20 20 20 20 20 20 20 6d 61 78 52 6f 6f 74 50          maxRootP
134d4 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 20  gno--;.      }. 
134d5 20 20 20 20 20 69 66 28 20 6d 61 78 52 6f 6f 74       if( maxRoot
134d6 50 67 6e 6f 3d 3d 50 54 52 4d 41 50 5f 50 41 47  Pgno==PTRMAP_PAG
134d7 45 4e 4f 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74  ENO(pBt, maxRoot
134d8 50 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 20  Pgno) ){.       
134d9 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a   maxRootPgno--;.
134da 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73        }.      as
134db 73 65 72 74 28 20 6d 61 78 52 6f 6f 74 50 67 6e  sert( maxRootPgn
134dc 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  o!=PENDING_BYTE_
134dd 50 41 47 45 28 70 42 74 29 20 29 3b 0a 0a 20 20  PAGE(pBt) );..  
134de 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
134df 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 28  BtreeUpdateMeta(
134e0 70 2c 20 34 2c 20 6d 61 78 52 6f 6f 74 50 67 6e  p, 4, maxRootPgn
134e1 6f 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  o);.    }else{. 
134e2 20 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61       rc = freePa
134e3 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20  ge(pPage);.     
134e4 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61   releasePage(pPa
134e5 67 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  ge);.    }.#endi
134e6 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  f.  }else{.    /
134e7 2a 20 49 66 20 73 71 6c 69 74 65 33 42 74 72 65  * If sqlite3Btre
134e8 65 44 72 6f 70 54 61 62 6c 65 20 77 61 73 20 63  eDropTable was c
134e9 61 6c 6c 65 64 20 6f 6e 20 70 61 67 65 20 31 2e  alled on page 1.
134ea 20 2a 2f 0a 20 20 20 20 7a 65 72 6f 50 61 67 65   */.    zeroPage
134eb 28 70 50 61 67 65 2c 20 50 54 46 5f 49 4e 54 4b  (pPage, PTF_INTK
134ec 45 59 7c 50 54 46 5f 4c 45 41 46 20 29 3b 0a 20  EY|PTF_LEAF );. 
134ed 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
134ee 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74  Page);.  }.  ret
134ef 75 72 6e 20 72 63 3b 20 20 0a 7d 0a 53 51 4c 49  urn rc;  .}.SQLI
134f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
134f1 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54  qlite3BtreeDropT
134f2 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69  able(Btree *p, i
134f3 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a  nt iTable, int *
134f4 70 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20  piMoved){.  int 
134f5 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  rc;.  sqlite3Btr
134f6 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 2d  eeEnter(p);.  p-
134f7 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62  >pBt->db = p->db
134f8 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 44 72  ;.  rc = btreeDr
134f9 6f 70 54 61 62 6c 65 28 70 2c 20 69 54 61 62 6c  opTable(p, iTabl
134fa 65 2c 20 70 69 4d 6f 76 65 64 29 3b 0a 20 20 73  e, piMoved);.  s
134fb 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
134fc 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
134fd 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  ;.}.../*.** Read
134fe 20 74 68 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d   the meta-inform
134ff 61 74 69 6f 6e 20 6f 75 74 20 6f 66 20 61 20 64  ation out of a d
13500 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4d  atabase file.  M
13501 65 74 61 5b 30 5d 0a 2a 2a 20 69 73 20 74 68 65  eta[0].** is the
13502 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20   number of free 
13503 70 61 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20  pages currently 
13504 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  in the database.
13505 20 20 4d 65 74 61 5b 31 5d 0a 2a 2a 20 74 68 72    Meta[1].** thr
13506 6f 75 67 68 20 6d 65 74 61 5b 31 35 5d 20 61 72  ough meta[15] ar
13507 65 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20  e available for 
13508 75 73 65 20 62 79 20 68 69 67 68 65 72 20 6c 61  use by higher la
13509 79 65 72 73 2e 20 20 4d 65 74 61 5b 30 5d 0a 2a  yers.  Meta[0].*
1350a 2a 20 69 73 20 72 65 61 64 2d 6f 6e 6c 79 2c 20  * is read-only, 
1350b 74 68 65 20 6f 74 68 65 72 73 20 61 72 65 20 72  the others are r
1350c 65 61 64 2f 77 72 69 74 65 2e 0a 2a 2a 20 0a 2a  ead/write..** .*
1350d 2a 20 54 68 65 20 73 63 68 65 6d 61 20 6c 61 79  * The schema lay
1350e 65 72 20 6e 75 6d 62 65 72 73 20 6d 65 74 61 20  er numbers meta 
1350f 76 61 6c 75 65 73 20 64 69 66 66 65 72 65 6e 74  values different
13510 6c 79 2e 20 20 41 74 20 74 68 65 20 73 63 68 65  ly.  At the sche
13511 6d 61 0a 2a 2a 20 6c 61 79 65 72 20 28 61 6e 64  ma.** layer (and
13512 20 74 68 65 20 53 65 74 43 6f 6f 6b 69 65 20 61   the SetCookie a
13513 6e 64 20 52 65 61 64 43 6f 6f 6b 69 65 20 6f 70  nd ReadCookie op
13514 63 6f 64 65 73 29 20 74 68 65 20 6e 75 6d 62 65  codes) the numbe
13515 72 20 6f 66 0a 2a 2a 20 66 72 65 65 20 70 61 67  r of.** free pag
13516 65 73 20 69 73 20 6e 6f 74 20 76 69 73 69 62 6c  es is not visibl
13517 65 2e 20 20 53 6f 20 43 6f 6f 6b 69 65 5b 30 5d  e.  So Cookie[0]
13518 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
13519 4d 65 74 61 5b 31 5d 2e 0a 2a 2f 0a 53 51 4c 49  Meta[1]..*/.SQLI
1351a 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1351b 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65  qlite3BtreeGetMe
1351c 74 61 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74  ta(Btree *p, int
1351d 20 69 64 78 2c 20 75 33 32 20 2a 70 4d 65 74 61   idx, u32 *pMeta
1351e 29 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62  ){.  DbPage *pDb
1351f 50 61 67 65 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  Page;.  int rc;.
13520 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
13521 2a 70 50 31 3b 0a 20 20 42 74 53 68 61 72 65 64  *pP1;.  BtShared
13522 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
13523 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
13524 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e  nter(p);.  pBt->
13525 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f  db = p->db;..  /
13526 2a 20 52 65 61 64 69 6e 67 20 61 20 6d 65 74 61  * Reading a meta
13527 2d 64 61 74 61 20 76 61 6c 75 65 20 72 65 71 75  -data value requ
13528 69 72 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b  ires a read-lock
13529 20 6f 6e 20 70 61 67 65 20 31 20 28 61 6e 64 20   on page 1 (and 
1352a 68 65 6e 63 65 0a 20 20 2a 2a 20 74 68 65 20 73  hence.  ** the s
1352b 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
1352c 6c 65 2e 20 57 65 20 67 72 61 62 20 74 68 69 73  le. We grab this
1352d 20 6c 6f 63 6b 20 72 65 67 61 72 64 6c 65 73 73   lock regardless
1352e 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 0a 20   of whether or. 
1352f 20 2a 2a 20 6e 6f 74 20 74 68 65 20 53 51 4c 49   ** not the SQLI
13530 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74  TE_ReadUncommitt
13531 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 28  ed flag is set (
13532 74 68 65 20 74 61 62 6c 65 20 72 6f 6f 74 65 64  the table rooted
13533 20 61 74 20 70 61 67 65 0a 20 20 2a 2a 20 31 20   at page.  ** 1 
13534 69 73 20 74 72 65 61 74 65 64 20 61 73 20 61 20  is treated as a 
13535 73 70 65 63 69 61 6c 20 63 61 73 65 20 62 79 20  special case by 
13536 71 75 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 29  queryTableLock()
13537 20 61 6e 64 20 6c 6f 63 6b 54 61 62 6c 65 28 29   and lockTable()
13538 29 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 71  )..  */.  rc = q
13539 75 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 70 2c  ueryTableLock(p,
1353a 20 31 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a   1, READ_LOCK);.
1353b 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1353c 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  _OK ){.    sqlit
1353d 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
1353e 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
1353f 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 69    }..  assert( i
13540 64 78 3e 3d 30 20 26 26 20 69 64 78 3c 3d 31 35  dx>=0 && idx<=15
13541 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   );.  rc = sqlit
13542 65 33 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e  e3PagerGet(pBt->
13543 70 50 61 67 65 72 2c 20 31 2c 20 26 70 44 62 50  pPager, 1, &pDbP
13544 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29  age);.  if( rc )
13545 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  {.    sqlite3Btr
13546 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20  eeLeave(p);.    
13547 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
13548 20 70 50 31 20 3d 20 28 75 6e 73 69 67 6e 65 64   pP1 = (unsigned
13549 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 50   char *)sqlite3P
1354a 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50  agerGetData(pDbP
1354b 61 67 65 29 3b 0a 20 20 2a 70 4d 65 74 61 20 3d  age);.  *pMeta =
1354c 20 67 65 74 34 62 79 74 65 28 26 70 50 31 5b 33   get4byte(&pP1[3
1354d 36 20 2b 20 69 64 78 2a 34 5d 29 3b 0a 20 20 73  6 + idx*4]);.  s
1354e 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
1354f 28 70 44 62 50 61 67 65 29 3b 0a 0a 20 20 2f 2a  (pDbPage);..  /*
13550 20 49 66 20 61 75 74 6f 76 61 63 75 75 6d 65 64   If autovacuumed
13551 20 69 73 20 64 69 73 61 62 6c 65 64 20 69 6e 20   is disabled in 
13552 74 68 69 73 20 62 75 69 6c 64 20 62 75 74 20 77  this build but w
13553 65 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f 20  e are trying to 
13554 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 61 6e 20  .  ** access an 
13555 61 75 74 6f 76 61 63 75 75 6d 65 64 20 64 61 74  autovacuumed dat
13556 61 62 61 73 65 2c 20 74 68 65 6e 20 6d 61 6b 65  abase, then make
13557 20 74 68 65 20 64 61 74 61 62 61 73 65 20 72 65   the database re
13558 61 64 6f 6e 6c 79 2e 20 0a 20 20 2a 2f 0a 23 69  adonly. .  */.#i
13559 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
1355a 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66  _AUTOVACUUM.  if
1355b 28 20 69 64 78 3d 3d 34 20 26 26 20 2a 70 4d 65  ( idx==4 && *pMe
1355c 74 61 3e 30 20 29 20 70 42 74 2d 3e 72 65 61 64  ta>0 ) pBt->read
1355d 4f 6e 6c 79 20 3d 20 31 3b 0a 23 65 6e 64 69 66  Only = 1;.#endif
1355e 0a 0a 20 20 2f 2a 20 47 72 61 62 20 74 68 65 20  ..  /* Grab the 
1355f 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 70 61 67  read-lock on pag
13560 65 20 31 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 6c  e 1. */.  rc = l
13561 6f 63 6b 54 61 62 6c 65 28 70 2c 20 31 2c 20 52  ockTable(p, 1, R
13562 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 73 71 6c  EAD_LOCK);.  sql
13563 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
13564 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
13565 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d  }../*.** Write m
13566 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  eta-information 
13567 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61  back into the da
13568 74 61 62 61 73 65 2e 20 20 4d 65 74 61 5b 30 5d  tabase.  Meta[0]
13569 20 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79   is.** read-only
1356a 20 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62 65 20   and may not be 
1356b 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a 53 51 4c 49  written..*/.SQLI
1356c 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1356d 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74  qlite3BtreeUpdat
1356e 65 4d 65 74 61 28 42 74 72 65 65 20 2a 70 2c 20  eMeta(Btree *p, 
1356f 69 6e 74 20 69 64 78 2c 20 75 33 32 20 69 4d 65  int idx, u32 iMe
13570 74 61 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  ta){.  BtShared 
13571 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20  *pBt = p->pBt;. 
13572 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
13573 70 50 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  pP1;.  int rc;. 
13574 20 61 73 73 65 72 74 28 20 69 64 78 3e 3d 31 20   assert( idx>=1 
13575 26 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 20 20  && idx<=15 );.  
13576 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
13577 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 62 20  r(p);.  pBt->db 
13578 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70  = p->db;.  if( p
13579 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53  ->inTrans!=TRANS
1357a 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 72 63  _WRITE ){.    rc
1357b 20 3d 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79   = pBt->readOnly
1357c 20 3f 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e   ? SQLITE_READON
1357d 4c 59 20 3a 20 53 51 4c 49 54 45 5f 45 52 52 4f  LY : SQLITE_ERRO
1357e 52 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  R;.  }else{.    
1357f 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61  assert( pBt->pPa
13580 67 65 31 21 3d 30 20 29 3b 0a 20 20 20 20 70 50  ge1!=0 );.    pP
13581 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 2d  1 = pBt->pPage1-
13582 3e 61 44 61 74 61 3b 0a 20 20 20 20 72 63 20 3d  >aData;.    rc =
13583 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
13584 74 65 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e  te(pBt->pPage1->
13585 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66  pDbPage);.    if
13586 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
13587 29 7b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74  ){.      put4byt
13588 65 28 26 70 50 31 5b 33 36 20 2b 20 69 64 78 2a  e(&pP1[36 + idx*
13589 34 5d 2c 20 69 4d 65 74 61 29 3b 0a 23 69 66 6e  4], iMeta);.#ifn
1358a 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1358b 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20  AUTOVACUUM.     
1358c 20 69 66 28 20 69 64 78 3d 3d 37 20 29 7b 0a 20   if( idx==7 ){. 
1358d 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1358e 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 7c  Bt->autoVacuum |
1358f 7c 20 69 4d 65 74 61 3d 3d 30 20 29 3b 0a 20 20  | iMeta==0 );.  
13590 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 4d        assert( iM
13591 65 74 61 3d 3d 30 20 7c 7c 20 69 4d 65 74 61 3d  eta==0 || iMeta=
13592 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 70 42  =1 );.        pB
13593 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20  t->incrVacuum = 
13594 69 4d 65 74 61 3b 0a 20 20 20 20 20 20 7d 0a 23  iMeta;.      }.#
13595 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 7d 0a  endif.    }.  }.
13596 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
13597 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
13598 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
13599 74 75 72 6e 20 74 68 65 20 66 6c 61 67 20 62 79  turn the flag by
1359a 74 65 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e  te at the beginn
1359b 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67 65 20  ing of the page 
1359c 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 0a  that the cursor.
1359d 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  ** is currently 
1359e 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a 2a 2f 0a  pointing to..*/.
1359f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
135a0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46  nt sqlite3BtreeF
135a1 6c 61 67 73 28 42 74 43 75 72 73 6f 72 20 2a 70  lags(BtCursor *p
135a2 43 75 72 29 7b 0a 20 20 2f 2a 20 54 4f 44 4f 3a  Cur){.  /* TODO:
135a3 20 57 68 61 74 20 61 62 6f 75 74 20 43 55 52 53   What about CURS
135a4 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 73  OR_REQUIRESEEK s
135a5 74 61 74 65 3f 20 50 72 6f 62 61 62 6c 79 20 6e  tate? Probably n
135a6 65 65 64 20 74 6f 20 63 61 6c 6c 0a 20 20 2a 2a  eed to call.  **
135a7 20 72 65 73 74 6f 72 65 4f 72 43 6c 65 61 72 43   restoreOrClearC
135a8 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20  ursorPosition() 
135a9 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 4d 65 6d  here..  */.  Mem
135aa 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 72  Page *pPage;.  r
135ab 65 73 74 6f 72 65 4f 72 43 6c 65 61 72 43 75 72  estoreOrClearCur
135ac 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72  sorPosition(pCur
135ad 29 3b 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75  );.  pPage = pCu
135ae 72 2d 3e 70 50 61 67 65 3b 0a 20 20 61 73 73 65  r->pPage;.  asse
135af 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  rt( cursorHoldsM
135b0 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20  utex(pCur) );.  
135b1 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70  assert( pPage->p
135b2 42 74 3d 3d 70 43 75 72 2d 3e 70 42 74 20 29 3b  Bt==pCur->pBt );
135b3 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 20  .  return pPage 
135b4 3f 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70  ? pPage->aData[p
135b5 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 5d  Page->hdrOffset]
135b6 20 3a 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20   : 0;.}.../*.** 
135b7 52 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 72  Return the pager
135b8 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
135b9 20 61 20 42 54 72 65 65 2e 20 20 54 68 69 73 20   a BTree.  This 
135ba 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
135bb 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61  for.** testing a
135bc 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c  nd debugging onl
135bd 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
135be 56 41 54 45 20 50 61 67 65 72 20 2a 73 71 6c 69  VATE Pager *sqli
135bf 74 65 33 42 74 72 65 65 50 61 67 65 72 28 42 74  te3BtreePager(Bt
135c0 72 65 65 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  ree *p){.  retur
135c1 6e 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72  n p->pBt->pPager
135c2 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
135c3 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49  ITE_OMIT_INTEGRI
135c4 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 41  TY_CHECK./*.** A
135c5 70 70 65 6e 64 20 61 20 6d 65 73 73 61 67 65 20  ppend a message 
135c6 74 6f 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73  to the error mes
135c7 73 61 67 65 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a  sage string..*/.
135c8 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 65 63  static void chec
135c9 6b 41 70 70 65 6e 64 4d 73 67 28 0a 20 20 49 6e  kAppendMsg(.  In
135ca 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63  tegrityCk *pChec
135cb 6b 2c 0a 20 20 63 68 61 72 20 2a 7a 4d 73 67 31  k,.  char *zMsg1
135cc 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
135cd 7a 46 6f 72 6d 61 74 2c 0a 20 20 2e 2e 2e 0a 29  zFormat,.  ....)
135ce 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a  {.  va_list ap;.
135cf 20 20 63 68 61 72 20 2a 7a 4d 73 67 32 3b 0a 20    char *zMsg2;. 
135d0 20 69 66 28 20 21 70 43 68 65 63 6b 2d 3e 6d 78   if( !pCheck->mx
135d1 45 72 72 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  Err ) return;.  
135d2 70 43 68 65 63 6b 2d 3e 6d 78 45 72 72 2d 2d 3b  pCheck->mxErr--;
135d3 0a 20 20 70 43 68 65 63 6b 2d 3e 6e 45 72 72 2b  .  pCheck->nErr+
135d4 2b 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70  +;.  va_start(ap
135d5 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 4d  , zFormat);.  zM
135d6 73 67 32 20 3d 20 73 71 6c 69 74 65 33 56 4d 50  sg2 = sqlite3VMP
135d7 72 69 6e 74 66 28 30 2c 20 7a 46 6f 72 6d 61 74  rintf(0, zFormat
135d8 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28  , ap);.  va_end(
135d9 61 70 29 3b 0a 20 20 69 66 28 20 7a 4d 73 67 31  ap);.  if( zMsg1
135da 3d 3d 30 20 29 20 7a 4d 73 67 31 20 3d 20 22 22  ==0 ) zMsg1 = ""
135db 3b 0a 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e  ;.  if( pCheck->
135dc 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 63  zErrMsg ){.    c
135dd 68 61 72 20 2a 7a 4f 6c 64 20 3d 20 70 43 68 65  har *zOld = pChe
135de 63 6b 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20  ck->zErrMsg;.   
135df 20 70 43 68 65 63 6b 2d 3e 7a 45 72 72 4d 73 67   pCheck->zErrMsg
135e0 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65   = 0;.    sqlite
135e1 33 53 65 74 53 74 72 69 6e 67 28 26 70 43 68 65  3SetString(&pChe
135e2 63 6b 2d 3e 7a 45 72 72 4d 73 67 2c 20 7a 4f 6c  ck->zErrMsg, zOl
135e3 64 2c 20 22 5c 6e 22 2c 20 7a 4d 73 67 31 2c 20  d, "\n", zMsg1, 
135e4 7a 4d 73 67 32 2c 20 28 63 68 61 72 2a 29 30 29  zMsg2, (char*)0)
135e5 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
135e6 65 65 28 7a 4f 6c 64 29 3b 0a 20 20 7d 65 6c 73  ee(zOld);.  }els
135e7 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  e{.    sqlite3Se
135e8 74 53 74 72 69 6e 67 28 26 70 43 68 65 63 6b 2d  tString(&pCheck-
135e9 3e 7a 45 72 72 4d 73 67 2c 20 7a 4d 73 67 31 2c  >zErrMsg, zMsg1,
135ea 20 7a 4d 73 67 32 2c 20 28 63 68 61 72 2a 29 30   zMsg2, (char*)0
135eb 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
135ec 5f 66 72 65 65 28 7a 4d 73 67 32 29 3b 0a 7d 0a  _free(zMsg2);.}.
135ed 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
135ee 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f  _OMIT_INTEGRITY_
135ef 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65  CHECK */..#ifnde
135f0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
135f1 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a  TEGRITY_CHECK./*
135f2 0a 2a 2a 20 41 64 64 20 31 20 74 6f 20 74 68 65  .** Add 1 to the
135f3 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
135f4 20 66 6f 72 20 70 61 67 65 20 69 50 61 67 65 2e   for page iPage.
135f5 20 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65    If this is the
135f6 20 73 65 63 6f 6e 64 0a 2a 2a 20 72 65 66 65 72   second.** refer
135f7 65 6e 63 65 20 74 6f 20 74 68 65 20 70 61 67 65  ence to the page
135f8 2c 20 61 64 64 20 61 6e 20 65 72 72 6f 72 20 6d  , add an error m
135f9 65 73 73 61 67 65 20 74 6f 20 70 43 68 65 63 6b  essage to pCheck
135fa 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2a 20 52 65  ->zErrMsg..** Re
135fb 74 75 72 6e 20 31 20 69 66 20 74 68 65 72 65 20  turn 1 if there 
135fc 61 72 65 20 32 20 6f 72 65 20 6d 6f 72 65 20 72  are 2 ore more r
135fd 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65  eferences to the
135fe 20 70 61 67 65 20 61 6e 64 20 30 20 69 66 0a 2a   page and 0 if.*
135ff 2a 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65  * if this is the
13600 20 66 69 72 73 74 20 72 65 66 65 72 65 6e 63 65   first reference
13601 20 74 6f 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a   to the page..**
13602 0a 2a 2a 20 41 6c 73 6f 20 63 68 65 63 6b 20 74  .** Also check t
13603 68 61 74 20 74 68 65 20 70 61 67 65 20 6e 75 6d  hat the page num
13604 62 65 72 20 69 73 20 69 6e 20 62 6f 75 6e 64 73  ber is in bounds
13605 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
13606 63 68 65 63 6b 52 65 66 28 49 6e 74 65 67 72 69  checkRef(Integri
13607 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 69 6e  tyCk *pCheck, in
13608 74 20 69 50 61 67 65 2c 20 63 68 61 72 20 2a 7a  t iPage, char *z
13609 43 6f 6e 74 65 78 74 29 7b 0a 20 20 69 66 28 20  Context){.  if( 
1360a 69 50 61 67 65 3d 3d 30 20 29 20 72 65 74 75 72  iPage==0 ) retur
1360b 6e 20 31 3b 0a 20 20 69 66 28 20 69 50 61 67 65  n 1;.  if( iPage
1360c 3e 70 43 68 65 63 6b 2d 3e 6e 50 61 67 65 20 7c  >pCheck->nPage |
1360d 7c 20 69 50 61 67 65 3c 30 20 29 7b 0a 20 20 20  | iPage<0 ){.   
1360e 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28   checkAppendMsg(
1360f 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74  pCheck, zContext
13610 2c 20 22 69 6e 76 61 6c 69 64 20 70 61 67 65 20  , "invalid page 
13611 6e 75 6d 62 65 72 20 25 64 22 2c 20 69 50 61 67  number %d", iPag
13612 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  e);.    return 1
13613 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 68 65  ;.  }.  if( pChe
13614 63 6b 2d 3e 61 6e 52 65 66 5b 69 50 61 67 65 5d  ck->anRef[iPage]
13615 3d 3d 31 20 29 7b 0a 20 20 20 20 63 68 65 63 6b  ==1 ){.    check
13616 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b  AppendMsg(pCheck
13617 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 32 6e 64  , zContext, "2nd
13618 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61   reference to pa
13619 67 65 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a  ge %d", iPage);.
1361a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
1361b 7d 0a 20 20 72 65 74 75 72 6e 20 20 28 70 43 68  }.  return  (pCh
1361c 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 50 61 67 65  eck->anRef[iPage
1361d 5d 2b 2b 29 3e 31 3b 0a 7d 0a 0a 23 69 66 6e 64  ]++)>1;.}..#ifnd
1361e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1361f 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20  UTOVACUUM./*.** 
13620 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 65  Check that the e
13621 6e 74 72 79 20 69 6e 20 74 68 65 20 70 6f 69 6e  ntry in the poin
13622 74 65 72 2d 6d 61 70 20 66 6f 72 20 70 61 67 65  ter-map for page
13623 20 69 43 68 69 6c 64 20 6d 61 70 73 20 74 6f 20   iChild maps to 
13624 0a 2a 2a 20 70 61 67 65 20 69 50 61 72 65 6e 74  .** page iParent
13625 2c 20 70 6f 69 6e 74 65 72 20 74 79 70 65 20 70  , pointer type p
13626 74 72 54 79 70 65 2e 20 49 66 20 6e 6f 74 2c 20  trType. If not, 
13627 61 70 70 65 6e 64 20 61 6e 20 65 72 72 6f 72 20  append an error 
13628 6d 65 73 73 61 67 65 0a 2a 2a 20 74 6f 20 70 43  message.** to pC
13629 68 65 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  heck..*/.static 
1362a 76 6f 69 64 20 63 68 65 63 6b 50 74 72 6d 61 70  void checkPtrmap
1362b 28 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20  (.  IntegrityCk 
1362c 2a 70 43 68 65 63 6b 2c 20 20 20 2f 2a 20 49 6e  *pCheck,   /* In
1362d 74 65 67 72 69 74 79 20 63 68 65 63 6b 20 63 6f  tegrity check co
1362e 6e 74 65 78 74 20 2a 2f 0a 20 20 50 67 6e 6f 20  ntext */.  Pgno 
1362f 69 43 68 69 6c 64 2c 20 20 20 20 20 20 20 20 20  iChild,         
13630 20 20 2f 2a 20 43 68 69 6c 64 20 70 61 67 65 20    /* Child page 
13631 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 75 38 20 65  number */.  u8 e
13632 54 79 70 65 2c 20 20 20 20 20 20 20 20 20 20 20  Type,           
13633 20 20 20 2f 2a 20 45 78 70 65 63 74 65 64 20 70     /* Expected p
13634 6f 69 6e 74 65 72 20 6d 61 70 20 74 79 70 65 20  ointer map type 
13635 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 61 72 65 6e  */.  Pgno iParen
13636 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45  t,          /* E
13637 78 70 65 63 74 65 64 20 70 6f 69 6e 74 65 72 20  xpected pointer 
13638 6d 61 70 20 70 61 72 65 6e 74 20 70 61 67 65 20  map parent page 
13639 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 63 68 61 72  number */.  char
1363a 20 2a 7a 43 6f 6e 74 65 78 74 20 20 20 20 20 20   *zContext      
1363b 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 64 65     /* Context de
1363c 73 63 72 69 70 74 69 6f 6e 20 28 75 73 65 64 20  scription (used 
1363d 66 6f 72 20 65 72 72 6f 72 20 6d 73 67 29 20 2a  for error msg) *
1363e 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  /.){.  int rc;. 
1363f 20 75 38 20 65 50 74 72 6d 61 70 54 79 70 65 3b   u8 ePtrmapType;
13640 0a 20 20 50 67 6e 6f 20 69 50 74 72 6d 61 70 50  .  Pgno iPtrmapP
13641 61 72 65 6e 74 3b 0a 0a 20 20 72 63 20 3d 20 70  arent;..  rc = p
13642 74 72 6d 61 70 47 65 74 28 70 43 68 65 63 6b 2d  trmapGet(pCheck-
13643 3e 70 42 74 2c 20 69 43 68 69 6c 64 2c 20 26 65  >pBt, iChild, &e
13644 50 74 72 6d 61 70 54 79 70 65 2c 20 26 69 50 74  PtrmapType, &iPt
13645 72 6d 61 70 50 61 72 65 6e 74 29 3b 0a 20 20 69  rmapParent);.  i
13646 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
13647 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70   ){.    checkApp
13648 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a  endMsg(pCheck, z
13649 43 6f 6e 74 65 78 74 2c 20 22 46 61 69 6c 65 64  Context, "Failed
1364a 20 74 6f 20 72 65 61 64 20 70 74 72 6d 61 70 20   to read ptrmap 
1364b 6b 65 79 3d 25 64 22 2c 20 69 43 68 69 6c 64 29  key=%d", iChild)
1364c 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
1364d 7d 0a 0a 20 20 69 66 28 20 65 50 74 72 6d 61 70  }..  if( ePtrmap
1364e 54 79 70 65 21 3d 65 54 79 70 65 20 7c 7c 20 69  Type!=eType || i
1364f 50 74 72 6d 61 70 50 61 72 65 6e 74 21 3d 69 50  PtrmapParent!=iP
13650 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 63 68 65  arent ){.    che
13651 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65  ckAppendMsg(pChe
13652 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20  ck, zContext, . 
13653 20 20 20 20 20 22 42 61 64 20 70 74 72 20 6d 61       "Bad ptr ma
13654 70 20 65 6e 74 72 79 20 6b 65 79 3d 25 64 20 65  p entry key=%d e
13655 78 70 65 63 74 65 64 3d 28 25 64 2c 25 64 29 20  xpected=(%d,%d) 
13656 67 6f 74 3d 28 25 64 2c 25 64 29 22 2c 20 0a 20  got=(%d,%d)", . 
13657 20 20 20 20 20 69 43 68 69 6c 64 2c 20 65 54 79       iChild, eTy
13658 70 65 2c 20 69 50 61 72 65 6e 74 2c 20 65 50 74  pe, iParent, ePt
13659 72 6d 61 70 54 79 70 65 2c 20 69 50 74 72 6d 61  rmapType, iPtrma
1365a 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a 7d 0a  pParent);.  }.}.
1365b 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68  #endif../*.** Ch
1365c 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72 69 74  eck the integrit
1365d 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73  y of the freelis
1365e 74 20 6f 72 20 6f 66 20 61 6e 20 6f 76 65 72 66  t or of an overf
1365f 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a  low page list..*
13660 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68  * Verify that th
13661 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
13662 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 69 73  s on the list is
13663 20 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f   N..*/.static vo
13664 69 64 20 63 68 65 63 6b 4c 69 73 74 28 0a 20 20  id checkList(.  
13665 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68  IntegrityCk *pCh
13666 65 63 6b 2c 20 20 2f 2a 20 49 6e 74 65 67 72 69  eck,  /* Integri
13667 74 79 20 63 68 65 63 6b 69 6e 67 20 63 6f 6e 74  ty checking cont
13668 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 46  ext */.  int isF
13669 72 65 65 4c 69 73 74 2c 20 20 20 20 20 20 20 2f  reeList,       /
1366a 2a 20 54 72 75 65 20 66 6f 72 20 61 20 66 72 65  * True for a fre
1366b 65 6c 69 73 74 2e 20 20 46 61 6c 73 65 20 66 6f  elist.  False fo
1366c 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  r overflow page 
1366d 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 50  list */.  int iP
1366e 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  age,            
1366f 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66  /* Page number f
13670 6f 72 20 66 69 72 73 74 20 70 61 67 65 20 69 6e  or first page in
13671 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69   the list */.  i
13672 6e 74 20 4e 2c 20 20 20 20 20 20 20 20 20 20 20  nt N,           
13673 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74 65 64       /* Expected
13674 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
13675 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a   in the list */.
13676 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74    char *zContext
13677 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65          /* Conte
13678 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73  xt for error mes
13679 73 61 67 65 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e  sages */.){.  in
1367a 74 20 69 3b 0a 20 20 69 6e 74 20 65 78 70 65 63  t i;.  int expec
1367b 74 65 64 20 3d 20 4e 3b 0a 20 20 69 6e 74 20 69  ted = N;.  int i
1367c 46 69 72 73 74 20 3d 20 69 50 61 67 65 3b 0a 20  First = iPage;. 
1367d 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e 20 30 20   while( N-- > 0 
1367e 26 26 20 70 43 68 65 63 6b 2d 3e 6d 78 45 72 72  && pCheck->mxErr
1367f 20 29 7b 0a 20 20 20 20 44 62 50 61 67 65 20 2a   ){.    DbPage *
13680 70 4f 76 66 6c 50 61 67 65 3b 0a 20 20 20 20 75  pOvflPage;.    u
13681 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 4f  nsigned char *pO
13682 76 66 6c 44 61 74 61 3b 0a 20 20 20 20 69 66 28  vflData;.    if(
13683 20 69 50 61 67 65 3c 31 20 29 7b 0a 20 20 20 20   iPage<1 ){.    
13684 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67    checkAppendMsg
13685 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78  (pCheck, zContex
13686 74 2c 0a 20 20 20 20 20 20 20 20 20 22 25 64 20  t,.         "%d 
13687 6f 66 20 25 64 20 70 61 67 65 73 20 6d 69 73 73  of %d pages miss
13688 69 6e 67 20 66 72 6f 6d 20 6f 76 65 72 66 6c 6f  ing from overflo
13689 77 20 6c 69 73 74 20 73 74 61 72 74 69 6e 67 20  w list starting 
1368a 61 74 20 25 64 22 2c 0a 20 20 20 20 20 20 20 20  at %d",.        
1368b 20 20 4e 2b 31 2c 20 65 78 70 65 63 74 65 64 2c    N+1, expected,
1368c 20 69 46 69 72 73 74 29 3b 0a 20 20 20 20 20 20   iFirst);.      
1368d 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1368e 20 69 66 28 20 63 68 65 63 6b 52 65 66 28 70 43   if( checkRef(pC
1368f 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 7a 43 6f  heck, iPage, zCo
13690 6e 74 65 78 74 29 20 29 20 62 72 65 61 6b 3b 0a  ntext) ) break;.
13691 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 50      if( sqlite3P
13692 61 67 65 72 47 65 74 28 70 43 68 65 63 6b 2d 3e  agerGet(pCheck->
13693 70 50 61 67 65 72 2c 20 28 50 67 6e 6f 29 69 50  pPager, (Pgno)iP
13694 61 67 65 2c 20 26 70 4f 76 66 6c 50 61 67 65 29  age, &pOvflPage)
13695 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41   ){.      checkA
13696 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c  ppendMsg(pCheck,
13697 20 7a 43 6f 6e 74 65 78 74 2c 20 22 66 61 69 6c   zContext, "fail
13698 65 64 20 74 6f 20 67 65 74 20 70 61 67 65 20 25  ed to get page %
13699 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20  d", iPage);.    
1369a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1369b 20 20 20 70 4f 76 66 6c 44 61 74 61 20 3d 20 28     pOvflData = (
1369c 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
1369d 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44  sqlite3PagerGetD
1369e 61 74 61 28 70 4f 76 66 6c 50 61 67 65 29 3b 0a  ata(pOvflPage);.
1369f 20 20 20 20 69 66 28 20 69 73 46 72 65 65 4c 69      if( isFreeLi
136a0 73 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  st ){.      int 
136a1 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 4f  n = get4byte(&pO
136a2 76 66 6c 44 61 74 61 5b 34 5d 29 3b 0a 23 69 66  vflData[4]);.#if
136a3 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
136a4 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
136a5 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 70 42    if( pCheck->pB
136a6 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b  t->autoVacuum ){
136a7 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 50 74  .        checkPt
136a8 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 50 61  rmap(pCheck, iPa
136a9 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45 45 50  ge, PTRMAP_FREEP
136aa 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 78 74  AGE, 0, zContext
136ab 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  );.      }.#endi
136ac 66 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 70 43  f.      if( n>pC
136ad 68 65 63 6b 2d 3e 70 42 74 2d 3e 75 73 61 62 6c  heck->pBt->usabl
136ae 65 53 69 7a 65 2f 34 2d 38 20 29 7b 0a 20 20 20  eSize/4-8 ){.   
136af 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64       checkAppend
136b0 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e  Msg(pCheck, zCon
136b1 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 20  text,.          
136b2 20 22 66 72 65 65 6c 69 73 74 20 6c 65 61 66 20   "freelist leaf 
136b3 63 6f 75 6e 74 20 74 6f 6f 20 62 69 67 20 6f 6e  count too big on
136b4 20 70 61 67 65 20 25 64 22 2c 20 69 50 61 67 65   page %d", iPage
136b5 29 3b 0a 20 20 20 20 20 20 20 20 4e 2d 2d 3b 0a  );.        N--;.
136b6 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
136b7 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
136b8 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  n; i++){.       
136b9 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50 61 67     Pgno iFreePag
136ba 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 4f  e = get4byte(&pO
136bb 76 66 6c 44 61 74 61 5b 38 2b 69 2a 34 5d 29 3b  vflData[8+i*4]);
136bc 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
136bd 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
136be 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43            if( pC
136bf 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56  heck->pBt->autoV
136c0 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20  acuum ){.       
136c1 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70       checkPtrmap
136c2 28 70 43 68 65 63 6b 2c 20 69 46 72 65 65 50 61  (pCheck, iFreePa
136c3 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45 45 50  ge, PTRMAP_FREEP
136c4 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 78 74  AGE, 0, zContext
136c5 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 23  );.          }.#
136c6 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20 20  endif.          
136c7 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 6b 2c  checkRef(pCheck,
136c8 20 69 46 72 65 65 50 61 67 65 2c 20 7a 43 6f 6e   iFreePage, zCon
136c9 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 7d  text);.        }
136ca 0a 20 20 20 20 20 20 20 20 4e 20 2d 3d 20 6e 3b  .        N -= n;
136cb 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23  .      }.    }.#
136cc 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
136cd 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
136ce 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a    else{.      /*
136cf 20 49 66 20 74 68 69 73 20 64 61 74 61 62 61 73   If this databas
136d0 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d  e supports auto-
136d1 76 61 63 75 75 6d 20 61 6e 64 20 69 50 61 67 65  vacuum and iPage
136d2 20 69 73 20 6e 6f 74 20 74 68 65 20 6c 61 73 74   is not the last
136d3 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69  .      ** page i
136d4 6e 20 74 68 69 73 20 6f 76 65 72 66 6c 6f 77 20  n this overflow 
136d5 6c 69 73 74 2c 20 63 68 65 63 6b 20 74 68 61 74  list, check that
136d6 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70   the pointer-map
136d7 20 65 6e 74 72 79 20 66 6f 72 0a 20 20 20 20 20   entry for.     
136d8 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   ** the followin
136d9 67 20 70 61 67 65 20 6d 61 74 63 68 65 73 20 69  g page matches i
136da 50 61 67 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  Page..      */. 
136db 20 20 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d       if( pCheck-
136dc 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d  >pBt->autoVacuum
136dd 20 26 26 20 4e 3e 30 20 29 7b 0a 20 20 20 20 20   && N>0 ){.     
136de 20 20 20 69 20 3d 20 67 65 74 34 62 79 74 65 28     i = get4byte(
136df 70 4f 76 66 6c 44 61 74 61 29 3b 0a 20 20 20 20  pOvflData);.    
136e0 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28      checkPtrmap(
136e1 70 43 68 65 63 6b 2c 20 69 2c 20 50 54 52 4d 41  pCheck, i, PTRMA
136e2 50 5f 4f 56 45 52 46 4c 4f 57 32 2c 20 69 50 61  P_OVERFLOW2, iPa
136e3 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20  ge, zContext);. 
136e4 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e       }.    }.#en
136e5 64 69 66 0a 20 20 20 20 69 50 61 67 65 20 3d 20  dif.    iPage = 
136e6 67 65 74 34 62 79 74 65 28 70 4f 76 66 6c 44 61  get4byte(pOvflDa
136e7 74 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ta);.    sqlite3
136e8 50 61 67 65 72 55 6e 72 65 66 28 70 4f 76 66 6c  PagerUnref(pOvfl
136e9 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  Page);.  }.}.#en
136ea 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
136eb 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  IT_INTEGRITY_CHE
136ec 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  CK */..#ifndef S
136ed 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47  QLITE_OMIT_INTEG
136ee 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a  RITY_CHECK./*.**
136ef 20 44 6f 20 76 61 72 69 6f 75 73 20 73 61 6e 69   Do various sani
136f0 74 79 20 63 68 65 63 6b 73 20 6f 6e 20 61 20 73  ty checks on a s
136f1 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 61 20  ingle page of a 
136f2 74 72 65 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a  tree.  Return.**
136f3 20 74 68 65 20 74 72 65 65 20 64 65 70 74 68 2e   the tree depth.
136f4 20 20 52 6f 6f 74 20 70 61 67 65 73 20 72 65 74    Root pages ret
136f5 75 72 6e 20 30 2e 20 20 50 61 72 65 6e 74 73 20  urn 0.  Parents 
136f6 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 0a 2a 2a  of root pages.**
136f7 20 72 65 74 75 72 6e 20 31 2c 20 61 6e 64 20 73   return 1, and s
136f8 6f 20 66 6f 72 74 68 2e 0a 2a 2a 20 0a 2a 2a 20  o forth..** .** 
136f9 54 68 65 73 65 20 63 68 65 63 6b 73 20 61 72 65  These checks are
136fa 20 64 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   done:.**.**    
136fb 20 20 31 2e 20 20 4d 61 6b 65 20 73 75 72 65 20    1.  Make sure 
136fc 74 68 61 74 20 63 65 6c 6c 73 20 61 6e 64 20 66  that cells and f
136fd 72 65 65 62 6c 6f 63 6b 73 20 64 6f 20 6e 6f 74  reeblocks do not
136fe 20 6f 76 65 72 6c 61 70 0a 2a 2a 20 20 20 20 20   overlap.**     
136ff 20 20 20 20 20 62 75 74 20 63 6f 6d 62 69 6e 65       but combine
13700 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c 79 20 63   to completely c
13701 6f 76 65 72 20 74 68 65 20 70 61 67 65 2e 0a 2a  over the page..*
13702 2a 20 20 4e 4f 20 20 32 2e 20 20 4d 61 6b 65 20  *  NO  2.  Make 
13703 73 75 72 65 20 63 65 6c 6c 20 6b 65 79 73 20 61  sure cell keys a
13704 72 65 20 69 6e 20 6f 72 64 65 72 2e 0a 2a 2a 20  re in order..** 
13705 20 4e 4f 20 20 33 2e 20 20 4d 61 6b 65 20 73 75   NO  3.  Make su
13706 72 65 20 6e 6f 20 6b 65 79 20 69 73 20 6c 65 73  re no key is les
13707 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  s than or equal 
13708 74 6f 20 7a 4c 6f 77 65 72 42 6f 75 6e 64 2e 0a  to zLowerBound..
13709 2a 2a 20 20 4e 4f 20 20 34 2e 20 20 4d 61 6b 65  **  NO  4.  Make
1370a 20 73 75 72 65 20 6e 6f 20 6b 65 79 20 69 73 20   sure no key is 
1370b 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20  greater than or 
1370c 65 71 75 61 6c 20 74 6f 20 7a 55 70 70 65 72 42  equal to zUpperB
1370d 6f 75 6e 64 2e 0a 2a 2a 20 20 20 20 20 20 35 2e  ound..**      5.
1370e 20 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65    Check the inte
1370f 67 72 69 74 79 20 6f 66 20 6f 76 65 72 66 6c 6f  grity of overflo
13710 77 20 70 61 67 65 73 2e 0a 2a 2a 20 20 20 20 20  w pages..**     
13711 20 36 2e 20 20 52 65 63 75 72 73 69 76 65 6c 79   6.  Recursively
13712 20 63 61 6c 6c 20 63 68 65 63 6b 54 72 65 65 50   call checkTreeP
13713 61 67 65 20 6f 6e 20 61 6c 6c 20 63 68 69 6c 64  age on all child
13714 72 65 6e 2e 0a 2a 2a 20 20 20 20 20 20 37 2e 20  ren..**      7. 
13715 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65   Verify that the
13716 20 64 65 70 74 68 20 6f 66 20 61 6c 6c 20 63 68   depth of all ch
13717 69 6c 64 72 65 6e 20 69 73 20 74 68 65 20 73 61  ildren is the sa
13718 6d 65 2e 0a 2a 2a 20 20 20 20 20 20 38 2e 20 20  me..**      8.  
13719 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73 20 70  Make sure this p
1371a 61 67 65 20 69 73 20 61 74 20 6c 65 61 73 74 20  age is at least 
1371b 33 33 25 20 66 75 6c 6c 20 6f 72 20 65 6c 73 65  33% full or else
1371c 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 20 20 20   it is.**       
1371d 20 20 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74     the root of t
1371e 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74  he tree..*/.stat
1371f 69 63 20 69 6e 74 20 63 68 65 63 6b 54 72 65 65  ic int checkTree
13720 50 61 67 65 28 0a 20 20 49 6e 74 65 67 72 69 74  Page(.  Integrit
13721 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 2f 2a  yCk *pCheck,  /*
13722 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 74 68 65   Context for the
13723 20 73 61 6e 69 74 79 20 63 68 65 63 6b 20 2a 2f   sanity check */
13724 0a 20 20 69 6e 74 20 69 50 61 67 65 2c 20 20 20  .  int iPage,   
13725 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
13726 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70   number of the p
13727 61 67 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a  age to check */.
13728 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 72 65    MemPage *pPare
13729 6e 74 2c 20 20 20 20 20 2f 2a 20 50 61 72 65 6e  nt,     /* Paren
1372a 74 20 70 61 67 65 20 2a 2f 0a 20 20 63 68 61 72  t page */.  char
1372b 20 2a 7a 50 61 72 65 6e 74 43 6f 6e 74 65 78 74   *zParentContext
1372c 20 20 2f 2a 20 50 61 72 65 6e 74 20 63 6f 6e 74    /* Parent cont
1372d 65 78 74 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50  ext */.){.  MemP
1372e 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 69 6e  age *pPage;.  in
1372f 74 20 69 2c 20 72 63 2c 20 64 65 70 74 68 2c 20  t i, rc, depth, 
13730 64 32 2c 20 70 67 6e 6f 2c 20 63 6e 74 3b 0a 20  d2, pgno, cnt;. 
13731 20 69 6e 74 20 68 64 72 2c 20 63 65 6c 6c 53 74   int hdr, cellSt
13732 61 72 74 3b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c  art;.  int nCell
13733 3b 0a 20 20 75 38 20 2a 64 61 74 61 3b 0a 20 20  ;.  u8 *data;.  
13734 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 0a 20  BtShared *pBt;. 
13735 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b   int usableSize;
13736 0a 20 20 63 68 61 72 20 7a 43 6f 6e 74 65 78 74  .  char zContext
13737 5b 31 30 30 5d 3b 0a 20 20 63 68 61 72 20 2a 68  [100];.  char *h
13738 69 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73  it;..  sqlite3_s
13739 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
1373a 43 6f 6e 74 65 78 74 29 2c 20 7a 43 6f 6e 74 65  Context), zConte
1373b 78 74 2c 20 22 50 61 67 65 20 25 64 3a 20 22 2c  xt, "Page %d: ",
1373c 20 69 50 61 67 65 29 3b 0a 0a 20 20 2f 2a 20 43   iPage);..  /* C
1373d 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 61  heck that the pa
1373e 67 65 20 65 78 69 73 74 73 0a 20 20 2a 2f 0a 20  ge exists.  */. 
1373f 20 70 42 74 20 3d 20 70 43 68 65 63 6b 2d 3e 70   pBt = pCheck->p
13740 42 74 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65  Bt;.  usableSize
13741 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69   = pBt->usableSi
13742 7a 65 3b 0a 20 20 69 66 28 20 69 50 61 67 65 3d  ze;.  if( iPage=
13743 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
13744 20 69 66 28 20 63 68 65 63 6b 52 65 66 28 70 43   if( checkRef(pC
13745 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 7a 50 61  heck, iPage, zPa
13746 72 65 6e 74 43 6f 6e 74 65 78 74 29 20 29 20 72  rentContext) ) r
13747 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 28  eturn 0;.  if( (
13748 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
13749 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 28 50  eGetPage(pBt, (P
1374a 67 6e 6f 29 69 50 61 67 65 2c 20 26 70 50 61 67  gno)iPage, &pPag
1374b 65 2c 20 30 29 29 21 3d 30 20 29 7b 0a 20 20 20  e, 0))!=0 ){.   
1374c 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28   checkAppendMsg(
1374d 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74  pCheck, zContext
1374e 2c 0a 20 20 20 20 20 20 20 22 75 6e 61 62 6c 65  ,.       "unable
1374f 20 74 6f 20 67 65 74 20 74 68 65 20 70 61 67 65   to get the page
13750 2e 20 65 72 72 6f 72 20 63 6f 64 65 3d 25 64 22  . error code=%d"
13751 2c 20 72 63 29 3b 0a 20 20 20 20 72 65 74 75 72  , rc);.    retur
13752 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28  n 0;.  }.  if( (
13753 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
13754 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 65 2c  eInitPage(pPage,
13755 20 70 50 61 72 65 6e 74 29 29 21 3d 30 20 29 7b   pParent))!=0 ){
13756 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64  .    checkAppend
13757 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e  Msg(pCheck, zCon
13758 74 65 78 74 2c 20 0a 20 20 20 20 20 20 20 20 20  text, .         
13759 20 20 20 20 20 20 20 20 20 20 22 73 71 6c 69 74            "sqlit
1375a 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28  e3BtreeInitPage(
1375b 29 20 72 65 74 75 72 6e 73 20 65 72 72 6f 72 20  ) returns error 
1375c 63 6f 64 65 20 25 64 22 2c 20 72 63 29 3b 0a 20  code %d", rc);. 
1375d 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
1375e 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72  Page);.    retur
1375f 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  n 0;.  }..  /* C
13760 68 65 63 6b 20 6f 75 74 20 61 6c 6c 20 74 68 65  heck out all the
13761 20 63 65 6c 6c 73 2e 0a 20 20 2a 2f 0a 20 20 64   cells..  */.  d
13762 65 70 74 68 20 3d 20 30 3b 0a 20 20 66 6f 72 28  epth = 0;.  for(
13763 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43  i=0; i<pPage->nC
13764 65 6c 6c 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d  ell && pCheck->m
13765 78 45 72 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  xErr; i++){.    
13766 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 69  u8 *pCell;.    i
13767 6e 74 20 73 7a 3b 0a 20 20 20 20 43 65 6c 6c 49  nt sz;.    CellI
13768 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 20 20 2f  nfo info;..    /
13769 2a 20 43 68 65 63 6b 20 70 61 79 6c 6f 61 64 20  * Check payload 
1376a 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 0a 20  overflow pages. 
1376b 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
1376c 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
1376d 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20 7a 43 6f  f(zContext), zCo
1376e 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20  ntext,.         
1376f 20 20 20 20 22 4f 6e 20 74 72 65 65 20 70 61 67      "On tree pag
13770 65 20 25 64 20 63 65 6c 6c 20 25 64 3a 20 22 2c  e %d cell %d: ",
13771 20 69 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 20   iPage, i);.    
13772 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c  pCell = findCell
13773 28 70 50 61 67 65 2c 69 29 3b 0a 20 20 20 20 73  (pPage,i);.    s
13774 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65  qlite3BtreeParse
13775 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70  CellPtr(pPage, p
13776 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20  Cell, &info);.  
13777 20 20 73 7a 20 3d 20 69 6e 66 6f 2e 6e 44 61 74    sz = info.nDat
13778 61 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67  a;.    if( !pPag
13779 65 2d 3e 69 6e 74 4b 65 79 20 29 20 73 7a 20 2b  e->intKey ) sz +
1377a 3d 20 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20  = info.nKey;.   
1377b 20 61 73 73 65 72 74 28 20 73 7a 3d 3d 69 6e 66   assert( sz==inf
1377c 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20  o.nPayload );.  
1377d 20 20 69 66 28 20 73 7a 3e 69 6e 66 6f 2e 6e 4c    if( sz>info.nL
1377e 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 69 6e  ocal ){.      in
1377f 74 20 6e 50 61 67 65 20 3d 20 28 73 7a 20 2d 20  t nPage = (sz - 
13780 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2b 20 75 73  info.nLocal + us
13781 61 62 6c 65 53 69 7a 65 20 2d 20 35 29 2f 28 75  ableSize - 5)/(u
13782 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a  sableSize - 4);.
13783 20 20 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 4f        Pgno pgnoO
13784 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65 28 26  vfl = get4byte(&
13785 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72  pCell[info.iOver
13786 66 6c 6f 77 5d 29 3b 0a 23 69 66 6e 64 65 66 20  flow]);.#ifndef 
13787 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
13788 56 41 43 55 55 4d 0a 20 20 20 20 20 20 69 66 28  VACUUM.      if(
13789 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
1378a 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63   ){.        chec
1378b 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20  kPtrmap(pCheck, 
1378c 70 67 6e 6f 4f 76 66 6c 2c 20 50 54 52 4d 41 50  pgnoOvfl, PTRMAP
1378d 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 69 50 61 67  _OVERFLOW1, iPag
1378e 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20  e, zContext);.  
1378f 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
13790 20 20 20 63 68 65 63 6b 4c 69 73 74 28 70 43 68     checkList(pCh
13791 65 63 6b 2c 20 30 2c 20 70 67 6e 6f 4f 76 66 6c  eck, 0, pgnoOvfl
13792 2c 20 6e 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78  , nPage, zContex
13793 74 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  t);.    }..    /
13794 2a 20 43 68 65 63 6b 20 73 61 6e 69 74 79 20 6f  * Check sanity o
13795 66 20 6c 65 66 74 20 63 68 69 6c 64 20 70 61 67  f left child pag
13796 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  e..    */.    if
13797 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  ( !pPage->leaf )
13798 7b 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 67  {.      pgno = g
13799 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3b 0a  et4byte(pCell);.
1379a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1379b 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
1379c 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75       if( pBt->au
1379d 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20  toVacuum ){.    
1379e 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28      checkPtrmap(
1379f 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 50 54  pCheck, pgno, PT
137a0 52 4d 41 50 5f 42 54 52 45 45 2c 20 69 50 61 67  RMAP_BTREE, iPag
137a1 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20  e, zContext);.  
137a2 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
137a3 20 20 20 64 32 20 3d 20 63 68 65 63 6b 54 72 65     d2 = checkTre
137a4 65 50 61 67 65 28 70 43 68 65 63 6b 2c 70 67 6e  ePage(pCheck,pgn
137a5 6f 2c 70 50 61 67 65 2c 7a 43 6f 6e 74 65 78 74  o,pPage,zContext
137a6 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3e 30  );.      if( i>0
137a7 20 26 26 20 64 32 21 3d 64 65 70 74 68 20 29 7b   && d2!=depth ){
137a8 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 70  .        checkAp
137a9 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20  pendMsg(pCheck, 
137aa 7a 43 6f 6e 74 65 78 74 2c 20 22 43 68 69 6c 64  zContext, "Child
137ab 20 70 61 67 65 20 64 65 70 74 68 20 64 69 66 66   page depth diff
137ac 65 72 73 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ers");.      }. 
137ad 20 20 20 20 20 64 65 70 74 68 20 3d 20 64 32 3b       depth = d2;
137ae 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
137af 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
137b0 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34  .    pgno = get4
137b1 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61  byte(&pPage->aDa
137b2 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66  ta[pPage->hdrOff
137b3 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 73 71 6c  set+8]);.    sql
137b4 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
137b5 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20  zeof(zContext), 
137b6 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20  zContext, .     
137b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
137b8 22 4f 6e 20 70 61 67 65 20 25 64 20 61 74 20 72  "On page %d at r
137b9 69 67 68 74 20 63 68 69 6c 64 3a 20 22 2c 20 69  ight child: ", i
137ba 50 61 67 65 29 3b 0a 23 69 66 6e 64 65 66 20 53  Page);.#ifndef S
137bb 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
137bc 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42  ACUUM.    if( pB
137bd 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b  t->autoVacuum ){
137be 0a 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d  .      checkPtrm
137bf 61 70 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c  ap(pCheck, pgno,
137c0 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 69   PTRMAP_BTREE, i
137c1 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  Page, 0);.    }.
137c2 23 65 6e 64 69 66 0a 20 20 20 20 63 68 65 63 6b  #endif.    check
137c3 54 72 65 65 50 61 67 65 28 70 43 68 65 63 6b 2c  TreePage(pCheck,
137c4 20 70 67 6e 6f 2c 20 70 50 61 67 65 2c 20 7a 43   pgno, pPage, zC
137c5 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20 0a 20  ontext);.  }. . 
137c6 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 63 6f   /* Check for co
137c7 6d 70 6c 65 74 65 20 63 6f 76 65 72 61 67 65 20  mplete coverage 
137c8 6f 66 20 74 68 65 20 70 61 67 65 0a 20 20 2a 2f  of the page.  */
137c9 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d  .  data = pPage-
137ca 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20 3d 20  >aData;.  hdr = 
137cb 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
137cc 3b 0a 20 20 68 69 74 20 3d 20 73 71 6c 69 74 65  ;.  hit = sqlite
137cd 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 75 73 61  3MallocZero( usa
137ce 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 69 66 28  bleSize );.  if(
137cf 20 68 69 74 20 29 7b 0a 20 20 20 20 6d 65 6d 73   hit ){.    mems
137d0 65 74 28 68 69 74 2c 20 31 2c 20 67 65 74 32 62  et(hit, 1, get2b
137d1 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d  yte(&data[hdr+5]
137d2 29 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d 20  ));.    nCell = 
137d3 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
137d4 64 72 2b 33 5d 29 3b 0a 20 20 20 20 63 65 6c 6c  dr+3]);.    cell
137d5 53 74 61 72 74 20 3d 20 68 64 72 20 2b 20 31 32  Start = hdr + 12
137d6 20 2d 20 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66   - 4*pPage->leaf
137d7 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
137d8 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20  <nCell; i++){.  
137d9 20 20 20 20 69 6e 74 20 70 63 20 3d 20 67 65 74      int pc = get
137da 32 62 79 74 65 28 26 64 61 74 61 5b 63 65 6c 6c  2byte(&data[cell
137db 53 74 61 72 74 2b 69 2a 32 5d 29 3b 0a 20 20 20  Start+i*2]);.   
137dc 20 20 20 75 31 36 20 73 69 7a 65 20 3d 20 63 65     u16 size = ce
137dd 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c  llSizePtr(pPage,
137de 20 26 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20   &data[pc]);.   
137df 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
137e0 69 66 28 20 28 70 63 2b 73 69 7a 65 2d 31 29 3e  if( (pc+size-1)>
137e1 3d 75 73 61 62 6c 65 53 69 7a 65 20 7c 7c 20 70  =usableSize || p
137e2 63 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63  c<0 ){.        c
137e3 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
137e4 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20  heck, 0, .      
137e5 20 20 20 20 20 20 22 43 6f 72 72 75 70 74 69 6f        "Corruptio
137e6 6e 20 64 65 74 65 63 74 65 64 20 69 6e 20 63 65  n detected in ce
137e7 6c 6c 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64  ll %d on page %d
137e8 22 2c 69 2c 69 50 61 67 65 2c 30 29 3b 0a 20 20  ",i,iPage,0);.  
137e9 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
137ea 20 20 20 66 6f 72 28 6a 3d 70 63 2b 73 69 7a 65     for(j=pc+size
137eb 2d 31 3b 20 6a 3e 3d 70 63 3b 20 6a 2d 2d 29 20  -1; j>=pc; j--) 
137ec 68 69 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20  hit[j]++;.      
137ed 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28  }.    }.    for(
137ee 63 6e 74 3d 30 2c 20 69 3d 67 65 74 32 62 79 74  cnt=0, i=get2byt
137ef 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b  e(&data[hdr+1]);
137f0 20 69 3e 30 20 26 26 20 69 3c 75 73 61 62 6c 65   i>0 && i<usable
137f1 53 69 7a 65 20 26 26 20 63 6e 74 3c 31 30 30 30  Size && cnt<1000
137f2 30 3b 20 0a 20 20 20 20 20 20 20 20 20 20 20 63  0; .           c
137f3 6e 74 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74  nt++){.      int
137f4 20 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65   size = get2byte
137f5 28 26 64 61 74 61 5b 69 2b 32 5d 29 3b 0a 20 20  (&data[i+2]);.  
137f6 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20      int j;.     
137f7 20 69 66 28 20 28 69 2b 73 69 7a 65 2d 31 29 3e   if( (i+size-1)>
137f8 3d 75 73 61 62 6c 65 53 69 7a 65 20 7c 7c 20 69  =usableSize || i
137f9 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68  <0 ){.        ch
137fa 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68  eckAppendMsg(pCh
137fb 65 63 6b 2c 20 30 2c 20 20 0a 20 20 20 20 20 20  eck, 0,  .      
137fc 20 20 20 20 20 20 22 43 6f 72 72 75 70 74 69 6f        "Corruptio
137fd 6e 20 64 65 74 65 63 74 65 64 20 69 6e 20 63 65  n detected in ce
137fe 6c 6c 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64  ll %d on page %d
137ff 22 2c 69 2c 69 50 61 67 65 2c 30 29 3b 0a 20 20  ",i,iPage,0);.  
13800 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
13801 20 20 20 66 6f 72 28 6a 3d 69 2b 73 69 7a 65 2d     for(j=i+size-
13802 31 3b 20 6a 3e 3d 69 3b 20 6a 2d 2d 29 20 68 69  1; j>=i; j--) hi
13803 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a  t[j]++;.      }.
13804 20 20 20 20 20 20 69 20 3d 20 67 65 74 32 62 79        i = get2by
13805 74 65 28 26 64 61 74 61 5b 69 5d 29 3b 0a 20 20  te(&data[i]);.  
13806 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 63 6e    }.    for(i=cn
13807 74 3d 30 3b 20 69 3c 75 73 61 62 6c 65 53 69 7a  t=0; i<usableSiz
13808 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  e; i++){.      i
13809 66 28 20 68 69 74 5b 69 5d 3d 3d 30 20 29 7b 0a  f( hit[i]==0 ){.
1380a 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20          cnt++;. 
1380b 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68       }else if( h
1380c 69 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20 20  it[i]>1 ){.     
1380d 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73     checkAppendMs
1380e 67 28 70 43 68 65 63 6b 2c 20 30 2c 0a 20 20 20  g(pCheck, 0,.   
1380f 20 20 20 20 20 20 20 22 4d 75 6c 74 69 70 6c 65         "Multiple
13810 20 75 73 65 73 20 66 6f 72 20 62 79 74 65 20 25   uses for byte %
13811 64 20 6f 66 20 70 61 67 65 20 25 64 22 2c 20 69  d of page %d", i
13812 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20  , iPage);.      
13813 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
13814 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63  .    }.    if( c
13815 6e 74 21 3d 64 61 74 61 5b 68 64 72 2b 37 5d 20  nt!=data[hdr+7] 
13816 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70  ){.      checkAp
13817 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20  pendMsg(pCheck, 
13818 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22 46  0, .          "F
13819 72 61 67 6d 65 6e 74 65 64 20 73 70 61 63 65 20  ragmented space 
1381a 69 73 20 25 64 20 62 79 74 65 20 72 65 70 6f 72  is %d byte repor
1381b 74 65 64 20 61 73 20 25 64 20 6f 6e 20 70 61 67  ted as %d on pag
1381c 65 20 25 64 22 2c 0a 20 20 20 20 20 20 20 20 20  e %d",.         
1381d 20 63 6e 74 2c 20 64 61 74 61 5b 68 64 72 2b 37   cnt, data[hdr+7
1381e 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 7d  ], iPage);.    }
1381f 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66  .  }.  sqlite3_f
13820 72 65 65 28 68 69 74 29 3b 0a 0a 20 20 72 65 6c  ree(hit);..  rel
13821 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b  easePage(pPage);
13822 0a 20 20 72 65 74 75 72 6e 20 64 65 70 74 68 2b  .  return depth+
13823 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  1;.}.#endif /* S
13824 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47  QLITE_OMIT_INTEG
13825 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23  RITY_CHECK */..#
13826 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
13827 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  IT_INTEGRITY_CHE
13828 43 4b 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  CK./*.** This ro
13829 75 74 69 6e 65 20 64 6f 65 73 20 61 20 63 6f 6d  utine does a com
1382a 70 6c 65 74 65 20 63 68 65 63 6b 20 6f 66 20 74  plete check of t
1382b 68 65 20 67 69 76 65 6e 20 42 54 72 65 65 20 66  he given BTree f
1382c 69 6c 65 2e 20 20 61 52 6f 6f 74 5b 5d 20 69 73  ile.  aRoot[] is
1382d 0a 2a 2a 20 61 6e 20 61 72 72 61 79 20 6f 66 20  .** an array of 
1382e 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20 77 65  pages numbers we
1382f 72 65 20 65 61 63 68 20 70 61 67 65 20 6e 75 6d  re each page num
13830 62 65 72 20 69 73 20 74 68 65 20 72 6f 6f 74 20  ber is the root 
13831 70 61 67 65 20 6f 66 0a 2a 2a 20 61 20 74 61 62  page of.** a tab
13832 6c 65 2e 20 20 6e 52 6f 6f 74 20 69 73 20 74 68  le.  nRoot is th
13833 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  e number of entr
13834 69 65 73 20 69 6e 20 61 52 6f 6f 74 2e 0a 2a 2a  ies in aRoot..**
13835 0a 2a 2a 20 49 66 20 65 76 65 72 79 74 68 69 6e  .** If everythin
13836 67 20 63 68 65 63 6b 73 20 6f 75 74 2c 20 74 68  g checks out, th
13837 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
13838 6e 73 20 4e 55 4c 4c 2e 20 20 49 66 20 73 6f 6d  ns NULL.  If som
13839 65 74 68 69 6e 67 20 69 73 0a 2a 2a 20 61 6d 69  ething is.** ami
1383a 73 73 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ss, an error mes
1383b 73 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20  sage is written 
1383c 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61  into memory obta
1383d 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63  ined from malloc
1383e 28 29 0a 2a 2a 20 61 6e 64 20 61 20 70 6f 69 6e  ().** and a poin
1383f 74 65 72 20 74 6f 20 74 68 61 74 20 65 72 72 6f  ter to that erro
13840 72 20 6d 65 73 73 61 67 65 20 69 73 20 72 65 74  r message is ret
13841 75 72 6e 65 64 2e 20 20 54 68 65 20 63 61 6c 6c  urned.  The call
13842 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  ing function.** 
13843 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
13844 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65 20 65  or freeing the e
13845 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 68 65  rror message whe
13846 6e 20 69 74 20 69 73 20 64 6f 6e 65 2e 0a 2a 2f  n it is done..*/
13847 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
13848 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72  char *sqlite3Btr
13849 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 63 6b  eeIntegrityCheck
1384a 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 20 20  (.  Btree *p,   
1384b 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 74    /* The btree t
1384c 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a 2f 0a  o be checked */.
1384d 20 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 20 20    int *aRoot,   
1384e 2f 2a 20 41 6e 20 61 72 72 61 79 20 6f 66 20 72  /* An array of r
1384f 6f 6f 74 20 70 61 67 65 73 20 6e 75 6d 62 65 72  oot pages number
13850 73 20 66 6f 72 20 69 6e 64 69 76 69 64 75 61 6c  s for individual
13851 20 74 72 65 65 73 20 2a 2f 0a 20 20 69 6e 74 20   trees */.  int 
13852 6e 52 6f 6f 74 2c 20 20 20 20 2f 2a 20 4e 75 6d  nRoot,    /* Num
13853 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
13854 6e 20 61 52 6f 6f 74 5b 5d 20 2a 2f 0a 20 20 69  n aRoot[] */.  i
13855 6e 74 20 6d 78 45 72 72 2c 20 20 20 20 2f 2a 20  nt mxErr,    /* 
13856 53 74 6f 70 20 72 65 70 6f 72 74 69 6e 67 20 65  Stop reporting e
13857 72 72 6f 72 73 20 61 66 74 65 72 20 74 68 69 73  rrors after this
13858 20 6d 61 6e 79 20 2a 2f 0a 20 20 69 6e 74 20 2a   many */.  int *
13859 70 6e 45 72 72 20 20 20 20 2f 2a 20 57 72 69 74  pnErr    /* Writ
1385a 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f  e number of erro
1385b 72 73 20 73 65 65 6e 20 74 6f 20 74 68 69 73 20  rs seen to this 
1385c 76 61 72 69 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20  variable */.){. 
1385d 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 52   int i;.  int nR
1385e 65 66 3b 0a 20 20 49 6e 74 65 67 72 69 74 79 43  ef;.  IntegrityC
1385f 6b 20 73 43 68 65 63 6b 3b 0a 20 20 42 74 53 68  k sCheck;.  BtSh
13860 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
13861 42 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74  Bt;..  sqlite3Bt
13862 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70  reeEnter(p);.  p
13863 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  Bt->db = p->db;.
13864 20 20 6e 52 65 66 20 3d 20 73 71 6c 69 74 65 33    nRef = sqlite3
13865 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 42  PagerRefcount(pB
13866 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 69 66  t->pPager);.  if
13867 28 20 6c 6f 63 6b 42 74 72 65 65 57 69 74 68 52  ( lockBtreeWithR
13868 65 74 72 79 28 70 29 21 3d 53 51 4c 49 54 45 5f  etry(p)!=SQLITE_
13869 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OK ){.    sqlite
1386a 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
1386b 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
1386c 65 33 53 74 72 44 75 70 28 22 55 6e 61 62 6c 65  e3StrDup("Unable
1386d 20 74 6f 20 61 63 71 75 69 72 65 20 61 20 72 65   to acquire a re
1386e 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  ad lock on the d
1386f 61 74 61 62 61 73 65 22 29 3b 0a 20 20 7d 0a 20  atabase");.  }. 
13870 20 73 43 68 65 63 6b 2e 70 42 74 20 3d 20 70 42   sCheck.pBt = pB
13871 74 3b 0a 20 20 73 43 68 65 63 6b 2e 70 50 61 67  t;.  sCheck.pPag
13872 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 72  er = pBt->pPager
13873 3b 0a 20 20 73 43 68 65 63 6b 2e 6e 50 61 67 65  ;.  sCheck.nPage
13874 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50   = sqlite3PagerP
13875 61 67 65 63 6f 75 6e 74 28 73 43 68 65 63 6b 2e  agecount(sCheck.
13876 70 50 61 67 65 72 29 3b 0a 20 20 73 43 68 65 63  pPager);.  sChec
13877 6b 2e 6d 78 45 72 72 20 3d 20 6d 78 45 72 72 3b  k.mxErr = mxErr;
13878 0a 20 20 73 43 68 65 63 6b 2e 6e 45 72 72 20 3d  .  sCheck.nErr =
13879 20 30 3b 0a 20 20 2a 70 6e 45 72 72 20 3d 20 30   0;.  *pnErr = 0
1387a 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
1387b 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
1387c 0a 20 20 69 66 28 20 70 42 74 2d 3e 6e 54 72 75  .  if( pBt->nTru
1387d 6e 63 21 3d 30 20 29 7b 0a 20 20 20 20 73 43 68  nc!=0 ){.    sCh
1387e 65 63 6b 2e 6e 50 61 67 65 20 3d 20 70 42 74 2d  eck.nPage = pBt-
1387f 3e 6e 54 72 75 6e 63 3b 0a 20 20 7d 0a 23 65 6e  >nTrunc;.  }.#en
13880 64 69 66 0a 20 20 69 66 28 20 73 43 68 65 63 6b  dif.  if( sCheck
13881 2e 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20  .nPage==0 ){.   
13882 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e   unlockBtreeIfUn
13883 75 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 73  used(pBt);.    s
13884 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
13885 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  (p);.    return 
13886 30 3b 0a 20 20 7d 0a 20 20 73 43 68 65 63 6b 2e  0;.  }.  sCheck.
13887 61 6e 52 65 66 20 3d 20 73 71 6c 69 74 65 33 5f  anRef = sqlite3_
13888 6d 61 6c 6c 6f 63 28 20 28 73 43 68 65 63 6b 2e  malloc( (sCheck.
13889 6e 50 61 67 65 2b 31 29 2a 73 69 7a 65 6f 66 28  nPage+1)*sizeof(
1388a 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 30 5d 29  sCheck.anRef[0])
1388b 20 29 3b 0a 20 20 69 66 28 20 21 73 43 68 65 63   );.  if( !sChec
1388c 6b 2e 61 6e 52 65 66 20 29 7b 0a 20 20 20 20 75  k.anRef ){.    u
1388d 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73  nlockBtreeIfUnus
1388e 65 64 28 70 42 74 29 3b 0a 20 20 20 20 2a 70 6e  ed(pBt);.    *pn
1388f 45 72 72 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c  Err = 1;.    sql
13890 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
13891 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71  );.    return sq
13892 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 70 2d 3e  lite3MPrintf(p->
13893 64 62 2c 20 22 55 6e 61 62 6c 65 20 74 6f 20 6d  db, "Unable to m
13894 61 6c 6c 6f 63 20 25 64 20 62 79 74 65 73 22 2c  alloc %d bytes",
13895 20 0a 20 20 20 20 20 20 20 20 28 73 43 68 65 63   .        (sChec
13896 6b 2e 6e 50 61 67 65 2b 31 29 2a 73 69 7a 65 6f  k.nPage+1)*sizeo
13897 66 28 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 30  f(sCheck.anRef[0
13898 5d 29 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  ]));.  }.  for(i
13899 3d 30 3b 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50  =0; i<=sCheck.nP
1389a 61 67 65 3b 20 69 2b 2b 29 7b 20 73 43 68 65 63  age; i++){ sChec
1389b 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 30 3b 20  k.anRef[i] = 0; 
1389c 7d 0a 20 20 69 20 3d 20 50 45 4e 44 49 4e 47 5f  }.  i = PENDING_
1389d 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 3b 0a  BYTE_PAGE(pBt);.
1389e 20 20 69 66 28 20 69 3c 3d 73 43 68 65 63 6b 2e    if( i<=sCheck.
1389f 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 73 43 68  nPage ){.    sCh
138a0 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 31  eck.anRef[i] = 1
138a1 3b 0a 20 20 7d 0a 20 20 73 43 68 65 63 6b 2e 7a  ;.  }.  sCheck.z
138a2 45 72 72 4d 73 67 20 3d 20 30 3b 0a 0a 20 20 2f  ErrMsg = 0;..  /
138a3 2a 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65  * Check the inte
138a4 67 72 69 74 79 20 6f 66 20 74 68 65 20 66 72 65  grity of the fre
138a5 65 6c 69 73 74 0a 20 20 2a 2f 0a 20 20 63 68 65  elist.  */.  che
138a6 63 6b 4c 69 73 74 28 26 73 43 68 65 63 6b 2c 20  ckList(&sCheck, 
138a7 31 2c 20 67 65 74 34 62 79 74 65 28 26 70 42 74  1, get4byte(&pBt
138a8 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  ->pPage1->aData[
138a9 33 32 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20  32]),.          
138aa 20 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d    get4byte(&pBt-
138ab 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  >pPage1->aData[3
138ac 36 5d 29 2c 20 22 4d 61 69 6e 20 66 72 65 65 6c  6]), "Main freel
138ad 69 73 74 3a 20 22 29 3b 0a 0a 20 20 2f 2a 20 43  ist: ");..  /* C
138ae 68 65 63 6b 20 61 6c 6c 20 74 68 65 20 74 61 62  heck all the tab
138af 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  les..  */.  for(
138b0 69 3d 30 3b 20 69 3c 6e 52 6f 6f 74 20 26 26 20  i=0; i<nRoot && 
138b1 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69 2b  sCheck.mxErr; i+
138b2 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 52 6f 6f  +){.    if( aRoo
138b3 74 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  t[i]==0 ) contin
138b4 75 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ue;.#ifndef SQLI
138b5 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
138b6 55 4d 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e  UM.    if( pBt->
138b7 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 61 52  autoVacuum && aR
138b8 6f 6f 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20  oot[i]>1 ){.    
138b9 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 26 73    checkPtrmap(&s
138ba 43 68 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c  Check, aRoot[i],
138bb 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45   PTRMAP_ROOTPAGE
138bc 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 23  , 0, 0);.    }.#
138bd 65 6e 64 69 66 0a 20 20 20 20 63 68 65 63 6b 54  endif.    checkT
138be 72 65 65 50 61 67 65 28 26 73 43 68 65 63 6b 2c  reePage(&sCheck,
138bf 20 61 52 6f 6f 74 5b 69 5d 2c 20 30 2c 20 22 4c   aRoot[i], 0, "L
138c0 69 73 74 20 6f 66 20 74 72 65 65 20 72 6f 6f 74  ist of tree root
138c1 73 3a 20 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  s: ");.  }..  /*
138c2 20 4d 61 6b 65 20 73 75 72 65 20 65 76 65 72 79   Make sure every
138c3 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c   page in the fil
138c4 65 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 0a  e is referenced.
138c5 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b 20    */.  for(i=1; 
138c6 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65 20  i<=sCheck.nPage 
138c7 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b  && sCheck.mxErr;
138c8 20 69 2b 2b 29 7b 0a 23 69 66 64 65 66 20 53 51   i++){.#ifdef SQ
138c9 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
138ca 43 55 55 4d 0a 20 20 20 20 69 66 28 20 73 43 68  CUUM.    if( sCh
138cb 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20  eck.anRef[i]==0 
138cc 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70  ){.      checkAp
138cd 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c  pendMsg(&sCheck,
138ce 20 30 2c 20 22 50 61 67 65 20 25 64 20 69 73 20   0, "Page %d is 
138cf 6e 65 76 65 72 20 75 73 65 64 22 2c 20 69 29 3b  never used", i);
138d0 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20  .    }.#else.   
138d1 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62   /* If the datab
138d2 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74  ase supports aut
138d3 6f 2d 76 61 63 75 75 6d 2c 20 6d 61 6b 65 20 73  o-vacuum, make s
138d4 75 72 65 20 6e 6f 20 74 61 62 6c 65 73 20 63 6f  ure no tables co
138d5 6e 74 61 69 6e 0a 20 20 20 20 2a 2a 20 72 65 66  ntain.    ** ref
138d6 65 72 65 6e 63 65 73 20 74 6f 20 70 6f 69 6e 74  erences to point
138d7 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 0a 20 20  er-map pages..  
138d8 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 43 68    */.    if( sCh
138d9 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20  eck.anRef[i]==0 
138da 26 26 20 0a 20 20 20 20 20 20 20 28 50 54 52 4d  && .       (PTRM
138db 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 69  AP_PAGENO(pBt, i
138dc 29 21 3d 69 20 7c 7c 20 21 70 42 74 2d 3e 61 75  )!=i || !pBt->au
138dd 74 6f 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 20  toVacuum) ){.   
138de 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73     checkAppendMs
138df 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50  g(&sCheck, 0, "P
138e0 61 67 65 20 25 64 20 69 73 20 6e 65 76 65 72 20  age %d is never 
138e1 75 73 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 7d  used", i);.    }
138e2 0a 20 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e  .    if( sCheck.
138e3 61 6e 52 65 66 5b 69 5d 21 3d 30 20 26 26 20 0a  anRef[i]!=0 && .
138e4 20 20 20 20 20 20 20 28 50 54 52 4d 41 50 5f 50         (PTRMAP_P
138e5 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 3d 3d 69  AGENO(pBt, i)==i
138e6 20 26 26 20 70 42 74 2d 3e 61 75 74 6f 56 61 63   && pBt->autoVac
138e7 75 75 6d 29 20 29 7b 0a 20 20 20 20 20 20 63 68  uum) ){.      ch
138e8 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43  eckAppendMsg(&sC
138e9 68 65 63 6b 2c 20 30 2c 20 22 50 6f 69 6e 74 65  heck, 0, "Pointe
138ea 72 20 6d 61 70 20 70 61 67 65 20 25 64 20 69 73  r map page %d is
138eb 20 72 65 66 65 72 65 6e 63 65 64 22 2c 20 69 29   referenced", i)
138ec 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
138ed 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75   }..  /* Make su
138ee 72 65 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73  re this analysis
138ef 20 64 69 64 20 6e 6f 74 20 6c 65 61 76 65 20 61   did not leave a
138f0 6e 79 20 75 6e 72 65 66 28 29 20 70 61 67 65 73  ny unref() pages
138f1 0a 20 20 2a 2f 0a 20 20 75 6e 6c 6f 63 6b 42 74  .  */.  unlockBt
138f2 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29  reeIfUnused(pBt)
138f3 3b 0a 20 20 69 66 28 20 6e 52 65 66 20 21 3d 20  ;.  if( nRef != 
138f4 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63  sqlite3PagerRefc
138f5 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72  ount(pBt->pPager
138f6 29 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70  ) ){.    checkAp
138f7 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c  pendMsg(&sCheck,
138f8 20 30 2c 20 0a 20 20 20 20 20 20 22 4f 75 74 73   0, .      "Outs
138f9 74 61 6e 64 69 6e 67 20 70 61 67 65 20 63 6f 75  tanding page cou
138fa 6e 74 20 67 6f 65 73 20 66 72 6f 6d 20 25 64 20  nt goes from %d 
138fb 74 6f 20 25 64 20 64 75 72 69 6e 67 20 74 68 69  to %d during thi
138fc 73 20 61 6e 61 6c 79 73 69 73 22 2c 0a 20 20 20  s analysis",.   
138fd 20 20 20 6e 52 65 66 2c 20 73 71 6c 69 74 65 33     nRef, sqlite3
138fe 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 42  PagerRefcount(pB
138ff 74 2d 3e 70 50 61 67 65 72 29 0a 20 20 20 20 29  t->pPager).    )
13900 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61  ;.  }..  /* Clea
13901 6e 20 20 75 70 20 61 6e 64 20 72 65 70 6f 72 74  n  up and report
13902 20 65 72 72 6f 72 73 2e 0a 20 20 2a 2f 0a 20 20   errors..  */.  
13903 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
13904 65 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  e(p);.  sqlite3_
13905 66 72 65 65 28 73 43 68 65 63 6b 2e 61 6e 52 65  free(sCheck.anRe
13906 66 29 3b 0a 20 20 2a 70 6e 45 72 72 20 3d 20 73  f);.  *pnErr = s
13907 43 68 65 63 6b 2e 6e 45 72 72 3b 0a 20 20 72 65  Check.nErr;.  re
13908 74 75 72 6e 20 73 43 68 65 63 6b 2e 7a 45 72 72  turn sCheck.zErr
13909 4d 73 67 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  Msg;.}.#endif /*
1390a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54   SQLITE_OMIT_INT
1390b 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a  EGRITY_CHECK */.
1390c 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
1390d 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20  e full pathname 
1390e 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  of the underlyin
1390f 67 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  g database file.
13910 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72  .**.** The pager
13911 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76   filename is inv
13912 61 72 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61  ariant as long a
13913 73 20 74 68 65 20 70 61 67 65 72 20 69 73 0a 2a  s the pager is.*
13914 2a 20 6f 70 65 6e 20 73 6f 20 69 74 20 69 73 20  * open so it is 
13915 73 61 66 65 20 74 6f 20 61 63 63 65 73 73 20 77  safe to access w
13916 69 74 68 6f 75 74 20 74 68 65 20 42 74 53 68 61  ithout the BtSha
13917 72 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51  red mutex..*/.SQ
13918 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
13919 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
1391a 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65  BtreeGetFilename
1391b 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73  (Btree *p){.  as
1391c 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50  sert( p->pBt->pP
1391d 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 72 65 74  ager!=0 );.  ret
1391e 75 72 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72  urn sqlite3Pager
1391f 46 69 6c 65 6e 61 6d 65 28 70 2d 3e 70 42 74 2d  Filename(p->pBt-
13920 3e 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a  >pPager);.}../*.
13921 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61  ** Return the pa
13922 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 69  thname of the di
13923 72 65 63 74 6f 72 79 20 74 68 61 74 20 63 6f 6e  rectory that con
13924 74 61 69 6e 73 20 74 68 65 20 64 61 74 61 62 61  tains the databa
13925 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  se file..**.** T
13926 68 65 20 70 61 67 65 72 20 64 69 72 65 63 74 6f  he pager directo
13927 72 79 20 6e 61 6d 65 20 69 73 20 69 6e 76 61 72  ry name is invar
13928 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 20  iant as long as 
13929 74 68 65 20 70 61 67 65 72 20 69 73 0a 2a 2a 20  the pager is.** 
1392a 6f 70 65 6e 20 73 6f 20 69 74 20 69 73 20 73 61  open so it is sa
1392b 66 65 20 74 6f 20 61 63 63 65 73 73 20 77 69 74  fe to access wit
1392c 68 6f 75 74 20 74 68 65 20 42 74 53 68 61 72 65  hout the BtShare
1392d 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49  d mutex..*/.SQLI
1392e 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
1392f 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74   char *sqlite3Bt
13930 72 65 65 47 65 74 44 69 72 6e 61 6d 65 28 42 74  reeGetDirname(Bt
13931 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  ree *p){.  asser
13932 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65  t( p->pBt->pPage
13933 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e  r!=0 );.  return
13934 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 69 72   sqlite3PagerDir
13935 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e 70 50 61  name(p->pBt->pPa
13936 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ger);.}../*.** R
13937 65 74 75 72 6e 20 74 68 65 20 70 61 74 68 6e 61  eturn the pathna
13938 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  me of the journa
13939 6c 20 66 69 6c 65 20 66 6f 72 20 74 68 69 73 20  l file for this 
1393a 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 72 65  database. The re
1393b 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 6f 66  turn.** value of
1393c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
1393d 20 74 68 65 20 73 61 6d 65 20 72 65 67 61 72 64   the same regard
1393e 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20  less of whether 
1393f 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
13940 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 72 65  .** has been cre
13941 61 74 65 64 20 6f 72 20 6e 6f 74 2e 0a 2a 2a 0a  ated or not..**.
13942 2a 2a 20 54 68 65 20 70 61 67 65 72 20 6a 6f 75  ** The pager jou
13943 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73  rnal filename is
13944 20 69 6e 76 61 72 69 61 6e 74 20 61 73 20 6c 6f   invariant as lo
13945 6e 67 20 61 73 20 74 68 65 20 70 61 67 65 72 20  ng as the pager 
13946 69 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f 20 69 74  is.** open so it
13947 20 69 73 20 73 61 66 65 20 74 6f 20 61 63 63 65   is safe to acce
13948 73 73 20 77 69 74 68 6f 75 74 20 74 68 65 20 42  ss without the B
13949 74 53 68 61 72 65 64 20 6d 75 74 65 78 2e 0a 2a  tShared mutex..*
1394a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1394b 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
1394c 69 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75 72  ite3BtreeGetJour
1394d 6e 61 6c 6e 61 6d 65 28 42 74 72 65 65 20 2a 70  nalname(Btree *p
1394e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  ){.  assert( p->
1394f 70 42 74 2d 3e 70 50 61 67 65 72 21 3d 30 20 29  pBt->pPager!=0 )
13950 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ;.  return sqlit
13951 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 6e 61  e3PagerJournalna
13952 6d 65 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65  me(p->pBt->pPage
13953 72 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53  r);.}..#ifndef S
13954 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55  QLITE_OMIT_VACUU
13955 4d 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 74 68 65  M./*.** Copy the
13956 20 63 6f 6d 70 6c 65 74 65 20 63 6f 6e 74 65 6e   complete conten
13957 74 20 6f 66 20 70 42 74 46 72 6f 6d 20 69 6e 74  t of pBtFrom int
13958 6f 20 70 42 74 54 6f 2e 20 20 41 20 74 72 61 6e  o pBtTo.  A tran
13959 73 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20  saction.** must 
1395a 62 65 20 61 63 74 69 76 65 20 66 6f 72 20 62 6f  be active for bo
1395b 74 68 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  th files..**.** 
1395c 54 68 65 20 73 69 7a 65 20 6f 66 20 66 69 6c 65  The size of file
1395d 20 70 54 6f 20 6d 61 79 20 62 65 20 72 65 64 75   pTo may be redu
1395e 63 65 64 20 62 79 20 74 68 69 73 20 6f 70 65 72  ced by this oper
1395f 61 74 69 6f 6e 2e 0a 2a 2a 20 49 66 20 61 6e 79  ation..** If any
13960 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67  thing goes wrong
13961 2c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  , the transactio
13962 6e 20 6f 6e 20 70 54 6f 20 69 73 20 72 6f 6c 6c  n on pTo is roll
13963 65 64 20 62 61 63 6b 2e 20 0a 2a 2a 0a 2a 2a 20  ed back. .**.** 
13964 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 43  If successful, C
13965 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20  ommitPhaseOne() 
13966 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e  may be called on
13967 20 70 54 6f 20 62 65 66 6f 72 65 20 72 65 74 75   pTo before retu
13968 72 6e 69 6e 67 2e 20 0a 2a 2a 20 54 68 65 20 63  rning. .** The c
13969 61 6c 6c 65 72 20 73 68 6f 75 6c 64 20 66 69 6e  aller should fin
1396a 69 73 68 20 63 6f 6d 6d 69 74 74 69 6e 67 20 74  ish committing t
1396b 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  he transaction o
1396c 6e 20 70 54 6f 20 62 79 20 63 61 6c 6c 69 6e 67  n pTo by calling
1396d 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65  .** sqlite3Btree
1396e 43 6f 6d 6d 69 74 28 29 2e 0a 2a 2f 0a 73 74 61  Commit()..*/.sta
1396f 74 69 63 20 69 6e 74 20 62 74 72 65 65 43 6f 70  tic int btreeCop
13970 79 46 69 6c 65 28 42 74 72 65 65 20 2a 70 54 6f  yFile(Btree *pTo
13971 2c 20 42 74 72 65 65 20 2a 70 46 72 6f 6d 29 7b  , Btree *pFrom){
13972 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
13973 54 45 5f 4f 4b 3b 0a 20 20 50 67 6e 6f 20 69 3b  TE_OK;.  Pgno i;
13974 0a 0a 20 20 50 67 6e 6f 20 6e 46 72 6f 6d 50 61  ..  Pgno nFromPa
13975 67 65 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ge;     /* Numbe
13976 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 70 46  r of pages in pF
13977 72 6f 6d 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 54  rom */.  Pgno nT
13978 6f 50 61 67 65 3b 20 20 20 20 20 20 20 2f 2a 20  oPage;       /* 
13979 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  Number of pages 
1397a 69 6e 20 70 54 6f 20 2a 2f 0a 20 20 50 67 6e 6f  in pTo */.  Pgno
1397b 20 6e 4e 65 77 50 61 67 65 3b 20 20 20 20 20 20   nNewPage;      
1397c 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67  /* Number of pag
1397d 65 73 20 69 6e 20 70 54 6f 20 61 66 74 65 72 20  es in pTo after 
1397e 74 68 65 20 63 6f 70 79 20 2a 2f 0a 0a 20 20 50  the copy */..  P
1397f 67 6e 6f 20 69 53 6b 69 70 3b 20 20 20 20 20 20  gno iSkip;      
13980 20 20 20 2f 2a 20 50 65 6e 64 69 6e 67 20 62 79     /* Pending by
13981 74 65 20 70 61 67 65 20 69 6e 20 70 54 6f 20 2a  te page in pTo *
13982 2f 0a 20 20 69 6e 74 20 6e 54 6f 50 61 67 65 53  /.  int nToPageS
13983 69 7a 65 3b 20 20 20 20 2f 2a 20 50 61 67 65 20  ize;    /* Page 
13984 73 69 7a 65 20 6f 66 20 70 54 6f 20 69 6e 20 62  size of pTo in b
13985 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 46  ytes */.  int nF
13986 72 6f 6d 50 61 67 65 53 69 7a 65 3b 20 20 2f 2a  romPageSize;  /*
13987 20 50 61 67 65 20 73 69 7a 65 20 6f 66 20 70 46   Page size of pF
13988 72 6f 6d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  rom in bytes */.
13989 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
1398a 54 6f 20 3d 20 70 54 6f 2d 3e 70 42 74 3b 0a 20  To = pTo->pBt;. 
1398b 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 46 72   BtShared *pBtFr
1398c 6f 6d 20 3d 20 70 46 72 6f 6d 2d 3e 70 42 74 3b  om = pFrom->pBt;
1398d 0a 20 20 70 42 74 54 6f 2d 3e 64 62 20 3d 20 70  .  pBtTo->db = p
1398e 54 6f 2d 3e 64 62 3b 0a 20 20 70 42 74 46 72 6f  To->db;.  pBtFro
1398f 6d 2d 3e 64 62 20 3d 20 70 46 72 6f 6d 2d 3e 64  m->db = pFrom->d
13990 62 3b 0a 0a 20 20 6e 54 6f 50 61 67 65 53 69 7a  b;..  nToPageSiz
13991 65 20 3d 20 70 42 74 54 6f 2d 3e 70 61 67 65 53  e = pBtTo->pageS
13992 69 7a 65 3b 0a 20 20 6e 46 72 6f 6d 50 61 67 65  ize;.  nFromPage
13993 53 69 7a 65 20 3d 20 70 42 74 46 72 6f 6d 2d 3e  Size = pBtFrom->
13994 70 61 67 65 53 69 7a 65 3b 0a 0a 20 20 69 66 28  pageSize;..  if(
13995 20 70 54 6f 2d 3e 69 6e 54 72 61 6e 73 21 3d 54   pTo->inTrans!=T
13996 52 41 4e 53 5f 57 52 49 54 45 20 7c 7c 20 70 46  RANS_WRITE || pF
13997 72 6f 6d 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52  rom->inTrans!=TR
13998 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20  ANS_WRITE ){.   
13999 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
1399a 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 69 66 28 20  RROR;.  }.  if( 
1399b 70 42 74 54 6f 2d 3e 70 43 75 72 73 6f 72 20 29  pBtTo->pCursor )
1399c 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
1399d 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 0a 20  ITE_BUSY;.  }.. 
1399e 20 6e 54 6f 50 61 67 65 20 3d 20 73 71 6c 69 74   nToPage = sqlit
1399f 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
139a0 28 70 42 74 54 6f 2d 3e 70 50 61 67 65 72 29 3b  (pBtTo->pPager);
139a1 0a 20 20 6e 46 72 6f 6d 50 61 67 65 20 3d 20 73  .  nFromPage = s
139a2 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63  qlite3PagerPagec
139a3 6f 75 6e 74 28 70 42 74 46 72 6f 6d 2d 3e 70 50  ount(pBtFrom->pP
139a4 61 67 65 72 29 3b 0a 20 20 69 53 6b 69 70 20 3d  ager);.  iSkip =
139a5 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41   PENDING_BYTE_PA
139a6 47 45 28 70 42 74 54 6f 29 3b 0a 0a 20 20 2f 2a  GE(pBtTo);..  /*
139a7 20 56 61 72 69 61 62 6c 65 20 6e 4e 65 77 50 61   Variable nNewPa
139a8 67 65 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ge is the number
139a9 20 6f 66 20 70 61 67 65 73 20 72 65 71 75 69 72   of pages requir
139aa 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 0a  ed to store the.
139ab 20 20 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66    ** contents of
139ac 20 70 46 72 6f 6d 20 75 73 69 6e 67 20 74 68 65   pFrom using the
139ad 20 63 75 72 72 65 6e 74 20 70 61 67 65 2d 73 69   current page-si
139ae 7a 65 20 6f 66 20 70 54 6f 2e 0a 20 20 2a 2f 0a  ze of pTo..  */.
139af 20 20 6e 4e 65 77 50 61 67 65 20 3d 20 28 28 69    nNewPage = ((i
139b0 36 34 29 6e 46 72 6f 6d 50 61 67 65 20 2a 20 28  64)nFromPage * (
139b1 69 36 34 29 6e 46 72 6f 6d 50 61 67 65 53 69 7a  i64)nFromPageSiz
139b2 65 20 2b 20 28 69 36 34 29 6e 54 6f 50 61 67 65  e + (i64)nToPage
139b3 53 69 7a 65 20 2d 20 31 29 20 2f 20 0a 20 20 20  Size - 1) / .   
139b4 20 20 20 28 69 36 34 29 6e 54 6f 50 61 67 65 53     (i64)nToPageS
139b5 69 7a 65 3b 0a 0a 20 20 66 6f 72 28 69 3d 31 3b  ize;..  for(i=1;
139b6 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
139b7 26 20 28 69 3c 3d 6e 54 6f 50 61 67 65 20 7c 7c  & (i<=nToPage ||
139b8 20 69 3c 3d 6e 4e 65 77 50 61 67 65 29 3b 20 69   i<=nNewPage); i
139b9 2b 2b 29 7b 0a 0a 20 20 20 20 2f 2a 20 4a 6f 75  ++){..    /* Jou
139ba 72 6e 61 6c 20 74 68 65 20 6f 72 69 67 69 6e 61  rnal the origina
139bb 6c 20 70 61 67 65 2e 0a 20 20 20 20 2a 2a 0a 20  l page..    **. 
139bc 20 20 20 2a 2a 20 69 53 6b 69 70 20 69 73 20 74     ** iSkip is t
139bd 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  he page number o
139be 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 70 61  f the locking pa
139bf 67 65 20 28 50 45 4e 44 49 4e 47 5f 42 59 54 45  ge (PENDING_BYTE
139c0 5f 50 41 47 45 29 0a 20 20 20 20 2a 2a 20 69 6e  _PAGE).    ** in
139c1 20 64 61 74 61 62 61 73 65 20 2a 70 54 6f 20 28   database *pTo (
139c2 62 65 66 6f 72 65 20 74 68 65 20 63 6f 70 79 29  before the copy)
139c3 2e 20 54 68 69 73 20 70 61 67 65 20 69 73 20 6e  . This page is n
139c4 65 76 65 72 20 77 72 69 74 74 65 6e 20 0a 20 20  ever written .  
139c5 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6a 6f    ** into the jo
139c6 75 72 6e 61 6c 20 66 69 6c 65 2e 20 55 6e 6c 65  urnal file. Unle
139c7 73 73 20 69 3d 3d 69 53 6b 69 70 20 6f 72 20 74  ss i==iSkip or t
139c8 68 65 20 70 61 67 65 20 77 61 73 20 6e 6f 74 0a  he page was not.
139c9 20 20 20 20 2a 2a 20 70 72 65 73 65 6e 74 20 69      ** present i
139ca 6e 20 70 54 6f 20 62 65 66 6f 72 65 20 74 68 65  n pTo before the
139cb 20 63 6f 70 79 20 6f 70 65 72 61 74 69 6f 6e 2c   copy operation,
139cc 20 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 69 20   journal page i 
139cd 66 72 6f 6d 20 70 54 6f 2e 0a 20 20 20 20 2a 2f  from pTo..    */
139ce 0a 20 20 20 20 69 66 28 20 69 21 3d 69 53 6b 69  .    if( i!=iSki
139cf 70 20 26 26 20 69 3c 3d 6e 54 6f 50 61 67 65 20  p && i<=nToPage 
139d0 29 7b 0a 20 20 20 20 20 20 44 62 50 61 67 65 20  ){.      DbPage 
139d1 2a 70 44 62 50 61 67 65 20 3d 20 30 3b 0a 20 20  *pDbPage = 0;.  
139d2 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
139d3 50 61 67 65 72 47 65 74 28 70 42 74 54 6f 2d 3e  PagerGet(pBtTo->
139d4 70 50 61 67 65 72 2c 20 69 2c 20 26 70 44 62 50  pPager, i, &pDbP
139d5 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  age);.      if( 
139d6 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
139d7 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
139d8 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
139d9 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
139da 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
139db 5f 4f 4b 20 26 26 20 69 3e 6e 46 72 6f 6d 50 61  _OK && i>nFromPa
139dc 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ge ){.          
139dd 2f 2a 20 59 65 61 68 2e 20 20 49 74 20 73 65 65  /* Yeah.  It see
139de 6d 73 20 77 69 65 72 64 20 74 6f 20 63 61 6c 6c  ms wierd to call
139df 20 44 6f 6e 74 57 72 69 74 65 28 29 20 72 69 67   DontWrite() rig
139e0 68 74 20 61 66 74 65 72 20 57 72 69 74 65 28 29  ht after Write()
139e1 2e 20 42 75 74 0a 20 20 20 20 20 20 20 20 20 20  . But.          
139e2 2a 2a 20 74 68 61 74 20 69 73 20 62 65 63 61 75  ** that is becau
139e3 73 65 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20  se the names of 
139e4 74 68 6f 73 65 20 70 72 6f 63 65 64 75 72 65 73  those procedures
139e5 20 64 6f 20 6e 6f 74 20 65 78 61 63 74 6c 79 20   do not exactly 
139e6 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 72 65  .          ** re
139e7 70 72 65 73 65 6e 74 20 77 68 61 74 20 74 68 65  present what the
139e8 79 20 64 6f 2e 20 20 57 72 69 74 65 28 29 20 72  y do.  Write() r
139e9 65 61 6c 6c 79 20 6d 65 61 6e 73 20 22 70 75 74  eally means "put
139ea 20 74 68 69 73 20 70 61 67 65 20 69 6e 20 74 68   this page in th
139eb 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 72  e.          ** r
139ec 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
139ed 61 6e 64 20 6d 61 72 6b 20 69 74 20 61 73 20 64  and mark it as d
139ee 69 72 74 79 20 73 6f 20 74 68 61 74 20 69 74 20  irty so that it 
139ef 77 69 6c 6c 20 62 65 20 77 72 69 74 74 65 6e 0a  will be written.
139f0 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20            ** to 
139f1 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
139f2 65 20 6c 61 74 65 72 2e 22 20 20 44 6f 6e 74 57  e later."  DontW
139f3 72 69 74 65 28 29 20 75 6e 64 6f 65 73 20 74 68  rite() undoes th
139f4 65 20 73 65 63 6f 6e 64 20 70 61 72 74 20 6f 66  e second part of
139f5 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68  .          ** th
139f6 61 74 20 61 6e 64 20 70 72 65 76 65 6e 74 73 20  at and prevents 
139f7 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20 62 65  the page from be
139f8 69 6e 67 20 77 72 69 74 74 65 6e 20 74 6f 20 74  ing written to t
139f9 68 65 20 64 61 74 61 62 61 73 65 2e 20 54 68 65  he database. The
139fa 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 61  .          ** pa
139fb 67 65 20 69 73 20 73 74 69 6c 6c 20 6f 6e 20 74  ge is still on t
139fc 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  he rollback jour
139fd 6e 61 6c 2c 20 74 68 6f 75 67 68 2e 20 20 41 6e  nal, though.  An
139fe 64 20 74 68 61 74 20 69 73 20 74 68 65 20 0a 20  d that is the . 
139ff 20 20 20 20 20 20 20 20 20 2a 2a 20 77 68 6f 6c           ** whol
13a00 65 20 70 6f 69 6e 74 20 6f 66 20 74 68 69 73 20  e point of this 
13a01 62 6c 6f 63 6b 3a 20 74 6f 20 70 75 74 20 70 61  block: to put pa
13a02 67 65 73 20 6f 6e 20 74 68 65 20 72 6f 6c 6c 62  ges on the rollb
13a03 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 0a 20 20  ack journal. .  
13a04 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
13a05 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
13a06 72 44 6f 6e 74 57 72 69 74 65 28 70 44 62 50 61  rDontWrite(pDbPa
13a07 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ge);.        }. 
13a08 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
13a09 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 65  gerUnref(pDbPage
13a0a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
13a0b 0a 0a 20 20 20 20 2f 2a 20 4f 76 65 72 77 72 69  ..    /* Overwri
13a0c 74 65 20 74 68 65 20 64 61 74 61 20 69 6e 20 70  te the data in p
13a0d 61 67 65 20 69 20 6f 66 20 74 68 65 20 74 61 72  age i of the tar
13a0e 67 65 74 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  get database */.
13a0f 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
13a10 54 45 5f 4f 4b 20 26 26 20 69 21 3d 69 53 6b 69  TE_OK && i!=iSki
13a11 70 20 26 26 20 69 3c 3d 6e 4e 65 77 50 61 67 65  p && i<=nNewPage
13a12 20 29 7b 0a 0a 20 20 20 20 20 20 44 62 50 61 67   ){..      DbPag
13a13 65 20 2a 70 54 6f 50 61 67 65 20 3d 20 30 3b 0a  e *pToPage = 0;.
13a14 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e        sqlite3_in
13a15 74 36 34 20 69 4f 66 66 3b 0a 0a 20 20 20 20 20  t64 iOff;..     
13a16 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
13a17 65 72 47 65 74 28 70 42 74 54 6f 2d 3e 70 50 61  erGet(pBtTo->pPa
13a18 67 65 72 2c 20 69 2c 20 26 70 54 6f 50 61 67 65  ger, i, &pToPage
13a19 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
13a1a 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
13a1b 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
13a1c 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 6f  e3PagerWrite(pTo
13a1d 50 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a 0a  Page);.      }..
13a1e 20 20 20 20 20 20 66 6f 72 28 0a 20 20 20 20 20        for(.     
13a1f 20 20 20 69 4f 66 66 3d 28 69 2d 31 29 2a 6e 54     iOff=(i-1)*nT
13a20 6f 50 61 67 65 53 69 7a 65 3b 20 0a 20 20 20 20  oPageSize; .    
13a21 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f      rc==SQLITE_O
13a22 4b 20 26 26 20 69 4f 66 66 3c 69 2a 6e 54 6f 50  K && iOff<i*nToP
13a23 61 67 65 53 69 7a 65 3b 20 0a 20 20 20 20 20 20  ageSize; .      
13a24 20 20 69 4f 66 66 20 2b 3d 20 6e 46 72 6f 6d 50    iOff += nFromP
13a25 61 67 65 53 69 7a 65 0a 20 20 20 20 20 20 29 7b  ageSize.      ){
13a26 0a 20 20 20 20 20 20 20 20 44 62 50 61 67 65 20  .        DbPage 
13a27 2a 70 46 72 6f 6d 50 61 67 65 20 3d 20 30 3b 0a  *pFromPage = 0;.
13a28 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72          Pgno iFr
13a29 6f 6d 20 3d 20 28 69 4f 66 66 2f 6e 46 72 6f 6d  om = (iOff/nFrom
13a2a 50 61 67 65 53 69 7a 65 29 2b 31 3b 0a 0a 20 20  PageSize)+1;..  
13a2b 20 20 20 20 20 20 69 66 28 20 69 46 72 6f 6d 3d        if( iFrom=
13a2c 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  =PENDING_BYTE_PA
13a2d 47 45 28 70 42 74 46 72 6f 6d 29 20 29 7b 0a 20  GE(pBtFrom) ){. 
13a2e 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75           continu
13a2f 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  e;.        }..  
13a30 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
13a31 65 33 50 61 67 65 72 47 65 74 28 70 42 74 46 72  e3PagerGet(pBtFr
13a32 6f 6d 2d 3e 70 50 61 67 65 72 2c 20 69 46 72 6f  om->pPager, iFro
13a33 6d 2c 20 26 70 46 72 6f 6d 50 61 67 65 29 3b 0a  m, &pFromPage);.
13a34 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
13a35 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
13a36 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 6f         char *zTo
13a37 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47   = sqlite3PagerG
13a38 65 74 44 61 74 61 28 70 54 6f 50 61 67 65 29 3b  etData(pToPage);
13a39 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20  .          char 
13a3a 2a 7a 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65 33  *zFrom = sqlite3
13a3b 50 61 67 65 72 47 65 74 44 61 74 61 28 70 46 72  PagerGetData(pFr
13a3c 6f 6d 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  omPage);.       
13a3d 20 20 20 69 6e 74 20 6e 43 6f 70 79 3b 0a 0a 20     int nCopy;.. 
13a3e 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 46 72           if( nFr
13a3f 6f 6d 50 61 67 65 53 69 7a 65 3e 3d 6e 54 6f 50  omPageSize>=nToP
13a40 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  ageSize ){.     
13a41 20 20 20 20 20 20 20 7a 46 72 6f 6d 20 2b 3d 20         zFrom += 
13a42 28 28 69 2d 31 29 2a 6e 54 6f 50 61 67 65 53 69  ((i-1)*nToPageSi
13a43 7a 65 20 2d 20 28 28 69 46 72 6f 6d 2d 31 29 2a  ze - ((iFrom-1)*
13a44 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65 29 29 3b  nFromPageSize));
13a45 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 43 6f  .            nCo
13a46 70 79 20 3d 20 6e 54 6f 50 61 67 65 53 69 7a 65  py = nToPageSize
13a47 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
13a48 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a  e{.            z
13a49 54 6f 20 2b 3d 20 28 28 28 69 46 72 6f 6d 2d 31  To += (((iFrom-1
13a4a 29 2a 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65 29  )*nFromPageSize)
13a4b 20 2d 20 28 69 2d 31 29 2a 6e 54 6f 50 61 67 65   - (i-1)*nToPage
13a4c 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Size);.         
13a4d 20 20 20 6e 43 6f 70 79 20 3d 20 6e 46 72 6f 6d     nCopy = nFrom
13a4e 50 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 20  PageSize;.      
13a4f 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20      }..         
13a50 20 6d 65 6d 63 70 79 28 7a 54 6f 2c 20 7a 46 72   memcpy(zTo, zFr
13a51 6f 6d 2c 20 6e 43 6f 70 79 29 3b 0a 09 20 20 73  om, nCopy);..  s
13a52 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
13a53 28 70 46 72 6f 6d 50 61 67 65 29 3b 0a 20 20 20  (pFromPage);.   
13a54 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a       }.      }..
13a55 20 20 20 20 20 20 69 66 28 20 70 54 6f 50 61 67        if( pToPag
13a56 65 20 29 20 73 71 6c 69 74 65 33 50 61 67 65 72  e ) sqlite3Pager
13a57 55 6e 72 65 66 28 70 54 6f 50 61 67 65 29 3b 0a  Unref(pToPage);.
13a58 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
13a59 49 66 20 74 68 69 6e 67 73 20 68 61 76 65 20 77  If things have w
13a5a 6f 72 6b 65 64 20 73 6f 20 66 61 72 2c 20 74 68  orked so far, th
13a5b 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
13a5c 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20 0a  may need to be .
13a5d 20 20 2a 2a 20 74 72 75 6e 63 61 74 65 64 2e 20    ** truncated. 
13a5e 54 68 65 20 63 6f 6d 70 6c 65 78 20 70 61 72 74  The complex part
13a5f 20 69 73 20 74 68 61 74 20 69 74 20 6d 61 79 20   is that it may 
13a60 6e 65 65 64 20 74 6f 20 62 65 20 74 72 75 6e 63  need to be trunc
13a61 61 74 65 64 20 74 6f 0a 20 20 2a 2a 20 61 20 73  ated to.  ** a s
13a62 69 7a 65 20 74 68 61 74 20 69 73 20 6e 6f 74 20  ize that is not 
13a63 61 6e 20 69 6e 74 65 67 65 72 20 6d 75 6c 74 69  an integer multi
13a64 70 6c 65 20 6f 66 20 6e 54 6f 50 61 67 65 53 69  ple of nToPageSi
13a65 7a 65 20 2d 20 74 68 65 20 63 75 72 72 65 6e 74  ze - the current
13a66 0a 20 20 2a 2a 20 70 61 67 65 20 73 69 7a 65 20  .  ** page size 
13a67 75 73 65 64 20 62 79 20 74 68 65 20 70 61 67 65  used by the page
13a68 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  r associated wit
13a69 68 20 42 2d 54 72 65 65 20 70 54 6f 2e 0a 20 20  h B-Tree pTo..  
13a6a 2a 2a 0a 20 20 2a 2a 20 46 6f 72 20 65 78 61 6d  **.  ** For exam
13a6b 70 6c 65 2c 20 73 61 79 20 74 68 65 20 70 61 67  ple, say the pag
13a6c 65 2d 73 69 7a 65 20 6f 66 20 70 54 6f 20 69 73  e-size of pTo is
13a6d 20 32 30 34 38 20 62 79 74 65 73 20 61 6e 64 20   2048 bytes and 
13a6e 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 0a 20 20  the original .  
13a6f 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  ** number of pag
13a70 65 73 20 69 73 20 35 20 28 31 30 20 4b 42 20 66  es is 5 (10 KB f
13a71 69 6c 65 29 2e 20 49 66 20 70 46 72 6f 6d 20 68  ile). If pFrom h
13a72 61 73 20 61 20 70 61 67 65 20 73 69 7a 65 20 6f  as a page size o
13a73 66 20 31 30 32 34 20 0a 20 20 2a 2a 20 62 79 74  f 1024 .  ** byt
13a74 65 73 20 61 6e 64 20 39 20 70 61 67 65 73 2c 20  es and 9 pages, 
13a75 74 68 65 6e 20 74 68 65 20 66 69 6c 65 20 6e 65  then the file ne
13a76 65 64 73 20 74 6f 20 62 65 20 74 72 75 6e 63 61  eds to be trunca
13a77 74 65 64 20 74 6f 20 39 4b 42 2e 0a 20 20 2a 2f  ted to 9KB..  */
13a78 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
13a79 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20  E_OK ){.    if( 
13a7a 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65 21 3d 6e  nFromPageSize!=n
13a7b 54 6f 50 61 67 65 53 69 7a 65 20 29 7b 0a 20 20  ToPageSize ){.  
13a7c 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65      sqlite3_file
13a7d 20 2a 70 46 69 6c 65 20 3d 20 73 71 6c 69 74 65   *pFile = sqlite
13a7e 33 50 61 67 65 72 46 69 6c 65 28 70 42 74 54 6f  3PagerFile(pBtTo
13a7f 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20  ->pPager);.     
13a80 20 69 36 34 20 69 53 69 7a 65 20 3d 20 28 69 36   i64 iSize = (i6
13a81 34 29 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65 20  4)nFromPageSize 
13a82 2a 20 28 69 36 34 29 6e 46 72 6f 6d 50 61 67 65  * (i64)nFromPage
13a83 3b 0a 20 20 20 20 20 20 69 36 34 20 69 4e 6f 77  ;.      i64 iNow
13a84 20 3d 20 28 69 36 34 29 28 28 6e 54 6f 50 61 67   = (i64)((nToPag
13a85 65 3e 6e 4e 65 77 50 61 67 65 29 3f 6e 54 6f 50  e>nNewPage)?nToP
13a86 61 67 65 3a 6e 4e 65 77 50 61 67 65 29 20 2a 20  age:nNewPage) * 
13a87 28 69 36 34 29 6e 54 6f 50 61 67 65 53 69 7a 65  (i64)nToPageSize
13a88 3b 20 0a 20 20 20 20 20 20 69 36 34 20 69 50 65  ; .      i64 iPe
13a89 6e 64 69 6e 67 20 3d 20 28 28 69 36 34 29 50 45  nding = ((i64)PE
13a8a 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
13a8b 70 42 74 54 6f 29 2d 31 29 20 2a 28 69 36 34 29  pBtTo)-1) *(i64)
13a8c 6e 54 6f 50 61 67 65 53 69 7a 65 3b 0a 20 20 0a  nToPageSize;.  .
13a8d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 53        assert( iS
13a8e 69 7a 65 3c 3d 69 4e 6f 77 20 29 3b 0a 20 20 0a  ize<=iNow );.  .
13a8f 20 20 20 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20        /* Commit 
13a90 70 68 61 73 65 20 6f 6e 65 20 73 79 6e 63 73 20  phase one syncs 
13a91 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
13a92 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
13a93 20 70 54 6f 20 0a 20 20 20 20 20 20 2a 2a 20 63   pTo .      ** c
13a94 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6f 72  ontaining the or
13a95 69 67 69 6e 61 6c 20 64 61 74 61 2e 20 49 74 20  iginal data. It 
13a96 64 6f 65 73 20 6e 6f 74 20 73 79 6e 63 20 74 68  does not sync th
13a97 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a  e database file.
13a98 20 20 20 20 20 20 2a 2a 20 69 74 73 65 6c 66 2e        ** itself.
13a99 20 41 66 74 65 72 20 64 6f 69 6e 67 20 74 68 69   After doing thi
13a9a 73 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20  s it is safe to 
13a9b 75 73 65 20 4f 73 54 72 75 6e 63 61 74 65 28 29  use OsTruncate()
13a9c 20 61 6e 64 20 6f 74 68 65 72 0a 20 20 20 20 20   and other.     
13a9d 20 2a 2a 20 66 69 6c 65 20 41 50 49 73 20 6f 6e   ** file APIs on
13a9e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
13a9f 6c 65 20 64 69 72 65 63 74 6c 79 2e 0a 20 20 20  le directly..   
13aa0 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 42 74 54     */.      pBtT
13aa1 6f 2d 3e 64 62 20 3d 20 70 54 6f 2d 3e 64 62 3b  o->db = pTo->db;
13aa2 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
13aa3 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68  te3PagerCommitPh
13aa4 61 73 65 4f 6e 65 28 70 42 74 54 6f 2d 3e 70 50  aseOne(pBtTo->pP
13aa5 61 67 65 72 2c 20 30 2c 20 30 2c 20 31 29 3b 0a  ager, 0, 0, 1);.
13aa6 20 20 20 20 20 20 69 66 28 20 69 53 69 7a 65 3c        if( iSize<
13aa7 69 4e 6f 77 20 26 26 20 72 63 3d 3d 53 51 4c 49  iNow && rc==SQLI
13aa8 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
13aa9 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54   rc = sqlite3OsT
13aaa 72 75 6e 63 61 74 65 28 70 46 69 6c 65 2c 20 69  runcate(pFile, i
13aab 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Size);.      }. 
13aac 20 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c   .      /* The l
13aad 6f 6f 70 20 74 68 61 74 20 63 6f 70 69 65 64 20  oop that copied 
13aae 64 61 74 61 20 66 72 6f 6d 20 64 61 74 61 62 61  data from databa
13aaf 73 65 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f 20  se pFrom to pTo 
13ab0 64 69 64 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a  did not.      **
13ab1 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f   populate the lo
13ab2 63 6b 69 6e 67 20 70 61 67 65 20 6f 66 20 64 61  cking page of da
13ab3 74 61 62 61 73 65 20 70 54 6f 2e 20 49 66 20 74  tabase pTo. If t
13ab4 68 65 20 70 61 67 65 2d 73 69 7a 65 20 6f 66 0a  he page-size of.
13ab5 20 20 20 20 20 20 2a 2a 20 70 46 72 6f 6d 20 69        ** pFrom i
13ab6 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74  s smaller than t
13ab7 68 61 74 20 6f 66 20 70 54 6f 2c 20 74 68 69 73  hat of pTo, this
13ab8 20 6d 65 61 6e 73 20 73 6f 6d 65 20 64 61 74 61   means some data
13ab9 20 77 69 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 6e   will.      ** n
13aba 6f 74 20 68 61 76 65 20 62 65 65 6e 20 63 6f 70  ot have been cop
13abb 69 65 64 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20  ied. .      **. 
13abc 20 20 20 20 20 2a 2a 20 54 68 69 73 20 62 6c 6f       ** This blo
13abd 63 6b 20 63 6f 70 69 65 73 20 74 68 65 20 6d 69  ck copies the mi
13abe 73 73 69 6e 67 20 64 61 74 61 20 66 72 6f 6d 20  ssing data from 
13abf 64 61 74 61 62 61 73 65 20 70 46 72 6f 6d 20 74  database pFrom t
13ac0 6f 20 70 54 6f 20 0a 20 20 20 20 20 20 2a 2a 20  o pTo .      ** 
13ac1 75 73 69 6e 67 20 66 69 6c 65 20 41 50 49 73 2e  using file APIs.
13ac2 20 54 68 69 73 20 69 73 20 73 61 66 65 20 62 65   This is safe be
13ac3 63 61 75 73 65 20 61 74 20 74 68 69 73 20 70 6f  cause at this po
13ac4 69 6e 74 20 77 65 20 6b 6e 6f 77 20 74 68 61 74  int we know that
13ac5 0a 20 20 20 20 20 20 2a 2a 20 61 6c 6c 20 6f 66  .      ** all of
13ac6 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61   the original da
13ac7 74 61 20 66 72 6f 6d 20 70 54 6f 20 68 61 73 20  ta from pTo has 
13ac8 62 65 65 6e 20 73 79 6e 63 65 64 20 69 6e 74 6f  been synced into
13ac9 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 6a   the .      ** j
13aca 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 74 20  ournal file. At 
13acb 74 68 69 73 20 70 6f 69 6e 74 20 69 74 20 77 6f  this point it wo
13acc 75 6c 64 20 62 65 20 73 61 66 65 20 74 6f 20 64  uld be safe to d
13acd 6f 20 61 6e 79 74 68 69 6e 67 20 61 74 0a 20 20  o anything at.  
13ace 20 20 20 20 2a 2a 20 61 6c 6c 20 74 6f 20 74 68      ** all to th
13acf 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
13ad0 65 78 63 65 70 74 20 74 72 75 6e 63 61 74 65 20  except truncate 
13ad1 69 74 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73  it to zero bytes
13ad2 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
13ad3 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
13ad4 4f 4b 20 26 26 20 6e 46 72 6f 6d 50 61 67 65 53  OK && nFromPageS
13ad5 69 7a 65 3c 6e 54 6f 50 61 67 65 53 69 7a 65 20  ize<nToPageSize 
13ad6 26 26 20 69 53 69 7a 65 3e 69 50 65 6e 64 69 6e  && iSize>iPendin
13ad7 67 29 7b 0a 20 20 20 20 20 20 20 20 69 36 34 20  g){.        i64 
13ad8 69 4f 66 66 3b 0a 20 20 20 20 20 20 20 20 66 6f  iOff;.        fo
13ad9 72 28 0a 20 20 20 20 20 20 20 20 20 20 69 4f 66  r(.          iOf
13ada 66 3d 69 50 65 6e 64 69 6e 67 3b 20 0a 20 20 20  f=iPending; .   
13adb 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54         rc==SQLIT
13adc 45 5f 4f 4b 20 26 26 20 69 4f 66 66 3c 28 69 50  E_OK && iOff<(iP
13add 65 6e 64 69 6e 67 2b 6e 54 6f 50 61 67 65 53 69  ending+nToPageSi
13ade 7a 65 29 3b 20 0a 20 20 20 20 20 20 20 20 20 20  ze); .          
13adf 69 4f 66 66 20 2b 3d 20 6e 46 72 6f 6d 50 61 67  iOff += nFromPag
13ae0 65 53 69 7a 65 0a 20 20 20 20 20 20 20 20 29 7b  eSize.        ){
13ae1 0a 20 20 20 20 20 20 20 20 20 20 44 62 50 61 67  .          DbPag
13ae2 65 20 2a 70 46 72 6f 6d 50 61 67 65 20 3d 20 30  e *pFromPage = 0
13ae3 3b 0a 20 20 20 20 20 20 20 20 20 20 50 67 6e 6f  ;.          Pgno
13ae4 20 69 46 72 6f 6d 20 3d 20 28 69 4f 66 66 2f 6e   iFrom = (iOff/n
13ae5 46 72 6f 6d 50 61 67 65 53 69 7a 65 29 2b 31 3b  FromPageSize)+1;
13ae6 0a 20 20 0a 20 20 20 20 20 20 20 20 20 20 69 66  .  .          if
13ae7 28 20 69 46 72 6f 6d 3d 3d 50 45 4e 44 49 4e 47  ( iFrom==PENDING
13ae8 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 46 72  _BYTE_PAGE(pBtFr
13ae9 6f 6d 29 20 7c 7c 20 69 46 72 6f 6d 3e 6e 46 72  om) || iFrom>nFr
13aea 6f 6d 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20  omPage ){.      
13aeb 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
13aec 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 0a 20            }.  . 
13aed 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
13aee 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 42  lite3PagerGet(pB
13aef 74 46 72 6f 6d 2d 3e 70 50 61 67 65 72 2c 20 69  tFrom->pPager, i
13af0 46 72 6f 6d 2c 20 26 70 46 72 6f 6d 50 61 67 65  From, &pFromPage
13af1 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
13af2 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
13af3 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68  {.            ch
13af4 61 72 20 2a 7a 46 72 6f 6d 20 3d 20 73 71 6c 69  ar *zFrom = sqli
13af5 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28  te3PagerGetData(
13af6 70 46 72 6f 6d 50 61 67 65 29 3b 0a 20 20 09 20  pFromPage);.  . 
13af7 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57   rc = sqlite3OsW
13af8 72 69 74 65 28 70 46 69 6c 65 2c 20 7a 46 72 6f  rite(pFile, zFro
13af9 6d 2c 20 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65  m, nFromPageSize
13afa 2c 20 69 4f 66 66 29 3b 0a 20 20 20 20 20 20 20  , iOff);.       
13afb 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
13afc 72 55 6e 72 65 66 28 70 46 72 6f 6d 50 61 67 65  rUnref(pFromPage
13afd 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
13afe 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
13aff 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20 53 79 6e  .  .      /* Syn
13b00 63 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  c the database f
13b01 69 6c 65 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  ile */.      if(
13b02 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
13b03 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
13b04 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28  qlite3PagerSync(
13b05 70 42 74 54 6f 2d 3e 70 50 61 67 65 72 29 3b 0a  pBtTo->pPager);.
13b06 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
13b07 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  e{.      rc = sq
13b08 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61  lite3PagerTrunca
13b09 74 65 28 70 42 74 54 6f 2d 3e 70 50 61 67 65 72  te(pBtTo->pPager
13b0a 2c 20 6e 4e 65 77 50 61 67 65 29 3b 0a 20 20 20  , nNewPage);.   
13b0b 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53   }.    if( rc==S
13b0c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
13b0d 20 20 70 42 74 54 6f 2d 3e 70 61 67 65 53 69 7a    pBtTo->pageSiz
13b0e 65 46 69 78 65 64 20 3d 20 30 3b 0a 20 20 20 20  eFixed = 0;.    
13b0f 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 20  }.  }..  if( rc 
13b10 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  ){.    sqlite3Bt
13b11 72 65 65 52 6f 6c 6c 62 61 63 6b 28 70 54 6f 29  reeRollback(pTo)
13b12 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
13b13 72 63 3b 20 20 0a 7d 0a 53 51 4c 49 54 45 5f 50  rc;  .}.SQLITE_P
13b14 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
13b15 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c 65 28  e3BtreeCopyFile(
13b16 42 74 72 65 65 20 2a 70 54 6f 2c 20 42 74 72 65  Btree *pTo, Btre
13b17 65 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 6e 74  e *pFrom){.  int
13b18 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74   rc;.  sqlite3Bt
13b19 72 65 65 45 6e 74 65 72 28 70 54 6f 29 3b 0a 20  reeEnter(pTo);. 
13b1a 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
13b1b 65 72 28 70 46 72 6f 6d 29 3b 0a 20 20 72 63 20  er(pFrom);.  rc 
13b1c 3d 20 62 74 72 65 65 43 6f 70 79 46 69 6c 65 28  = btreeCopyFile(
13b1d 70 54 6f 2c 20 70 46 72 6f 6d 29 3b 0a 20 20 73  pTo, pFrom);.  s
13b1e 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
13b1f 28 70 46 72 6f 6d 29 3b 0a 20 20 73 71 6c 69 74  (pFrom);.  sqlit
13b20 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 54 6f  e3BtreeLeave(pTo
13b21 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
13b22 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
13b23 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20  ITE_OMIT_VACUUM 
13b24 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  */../*.** Return
13b25 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 74   non-zero if a t
13b26 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63  ransaction is ac
13b27 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tive..*/.SQLITE_
13b28 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
13b29 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e  te3BtreeIsInTran
13b2a 73 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61  s(Btree *p){.  a
13b2b 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 73  ssert( p==0 || s
13b2c 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
13b2d 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  d(p->db->mutex) 
13b2e 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 20 26  );.  return (p &
13b2f 26 20 28 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54  & (p->inTrans==T
13b30 52 41 4e 53 5f 57 52 49 54 45 29 29 3b 0a 7d 0a  RANS_WRITE));.}.
13b31 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e 6f  ./*.** Return no
13b32 6e 2d 7a 65 72 6f 20 69 66 20 61 20 73 74 61 74  n-zero if a stat
13b33 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
13b34 6e 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a  n is active..*/.
13b35 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
13b36 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  nt sqlite3BtreeI
13b37 73 49 6e 53 74 6d 74 28 42 74 72 65 65 20 2a 70  sInStmt(Btree *p
13b38 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
13b39 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75  ite3BtreeHoldsMu
13b3a 74 65 78 28 70 29 20 29 3b 0a 20 20 72 65 74 75  tex(p) );.  retu
13b3b 72 6e 20 28 70 2d 3e 70 42 74 20 26 26 20 70 2d  rn (p->pBt && p-
13b3c 3e 70 42 74 2d 3e 69 6e 53 74 6d 74 29 3b 0a 7d  >pBt->inStmt);.}
13b3d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e  ../*.** Return n
13b3e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 72 65 61  on-zero if a rea
13b3f 64 20 28 6f 72 20 77 72 69 74 65 29 20 74 72 61  d (or write) tra
13b40 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69  nsaction is acti
13b41 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ve..*/.SQLITE_PR
13b42 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
13b43 33 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 72  3BtreeIsInReadTr
13b44 61 6e 73 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  ans(Btree *p){. 
13b45 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
13b46 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64  _mutex_held(p->d
13b47 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72  b->mutex) );.  r
13b48 65 74 75 72 6e 20 28 70 20 26 26 20 28 70 2d 3e  eturn (p && (p->
13b49 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e  inTrans!=TRANS_N
13b4a 4f 4e 45 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ONE));.}../*.** 
13b4b 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
13b4c 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
13b4d 74 6f 20 61 20 62 6c 6f 62 20 6f 66 20 6d 65 6d  to a blob of mem
13b4e 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77  ory associated w
13b4f 69 74 68 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20  ith.** a single 
13b50 73 68 61 72 65 64 2d 62 74 72 65 65 2e 20 54 68  shared-btree. Th
13b51 65 20 6d 65 6d 6f 72 79 20 69 73 20 75 73 65 64  e memory is used
13b52 20 62 79 20 63 6c 69 65 6e 74 20 63 6f 64 65 20   by client code 
13b53 66 6f 72 20 69 74 73 20 6f 77 6e 0a 2a 2a 20 70  for its own.** p
13b54 75 72 70 6f 73 65 73 20 28 66 6f 72 20 65 78 61  urposes (for exa
13b55 6d 70 6c 65 2c 20 74 6f 20 73 74 6f 72 65 20 61  mple, to store a
13b56 20 68 69 67 68 2d 6c 65 76 65 6c 20 73 63 68 65   high-level sche
13b57 6d 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ma associated wi
13b58 74 68 20 0a 2a 2a 20 74 68 65 20 73 68 61 72 65  th .** the share
13b59 64 2d 62 74 72 65 65 29 2e 20 54 68 65 20 62 74  d-btree). The bt
13b5a 72 65 65 20 6c 61 79 65 72 20 6d 61 6e 61 67 65  ree layer manage
13b5b 73 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  s reference coun
13b5c 74 69 6e 67 20 69 73 73 75 65 73 2e 0a 2a 2a 0a  ting issues..**.
13b5d 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 69 6d  ** The first tim
13b5e 65 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64  e this is called
13b5f 20 6f 6e 20 61 20 73 68 61 72 65 64 2d 62 74 72   on a shared-btr
13b60 65 65 2c 20 6e 42 79 74 65 73 20 62 79 74 65 73  ee, nBytes bytes
13b61 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 72   of memory.** ar
13b62 65 20 61 6c 6c 6f 63 61 74 65 64 2c 20 7a 65 72  e allocated, zer
13b63 6f 65 64 2c 20 61 6e 64 20 72 65 74 75 72 6e 65  oed, and returne
13b64 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e  d to the caller.
13b65 20 46 6f 72 20 65 61 63 68 20 73 75 62 73 65 71   For each subseq
13b66 75 65 6e 74 20 0a 2a 2a 20 63 61 6c 6c 20 74 68  uent .** call th
13b67 65 20 6e 42 79 74 65 73 20 70 61 72 61 6d 65 74  e nBytes paramet
13b68 65 72 20 69 73 20 69 67 6e 6f 72 65 64 20 61 6e  er is ignored an
13b69 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  d a pointer to t
13b6a 68 65 20 73 61 6d 65 20 62 6c 6f 62 0a 2a 2a 20  he same blob.** 
13b6b 6f 66 20 6d 65 6d 6f 72 79 20 72 65 74 75 72 6e  of memory return
13b6c 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 4a 75 73 74 20  ed. .**.** Just 
13b6d 62 65 66 6f 72 65 20 74 68 65 20 73 68 61 72 65  before the share
13b6e 64 2d 62 74 72 65 65 20 69 73 20 63 6c 6f 73 65  d-btree is close
13b6f 64 2c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  d, the function 
13b70 70 61 73 73 65 64 20 61 73 20 74 68 65 20 0a 2a  passed as the .*
13b71 2a 20 78 46 72 65 65 20 61 72 67 75 6d 65 6e 74  * xFree argument
13b72 20 77 68 65 6e 20 74 68 65 20 6d 65 6d 6f 72 79   when the memory
13b73 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 20   allocation was 
13b74 6d 61 64 65 20 69 73 20 69 6e 76 6f 6b 65 64 20  made is invoked 
13b75 6f 6e 20 74 68 65 20 0a 2a 2a 20 62 6c 6f 62 20  on the .** blob 
13b76 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d  of allocated mem
13b77 6f 72 79 2e 20 54 68 69 73 20 66 75 6e 63 74 69  ory. This functi
13b78 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 20 63 61  on should not ca
13b79 6c 6c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  ll sqlite3_free(
13b7a 29 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 65 6d 6f  ).** on the memo
13b7b 72 79 2c 20 74 68 65 20 62 74 72 65 65 20 6c 61  ry, the btree la
13b7c 79 65 72 20 64 6f 65 73 20 74 68 61 74 2e 0a 2a  yer does that..*
13b7d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
13b7e 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74   void *sqlite3Bt
13b7f 72 65 65 53 63 68 65 6d 61 28 42 74 72 65 65 20  reeSchema(Btree 
13b80 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 73 2c 20  *p, int nBytes, 
13b81 76 6f 69 64 28 2a 78 46 72 65 65 29 28 76 6f 69  void(*xFree)(voi
13b82 64 20 2a 29 29 7b 0a 20 20 42 74 53 68 61 72 65  d *)){.  BtShare
13b83 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
13b84 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
13b85 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20 21  nter(p);.  if( !
13b86 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a  pBt->pSchema ){.
13b87 20 20 20 20 70 42 74 2d 3e 70 53 63 68 65 6d 61      pBt->pSchema
13b88 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
13b89 5a 65 72 6f 28 6e 42 79 74 65 73 29 3b 0a 20 20  Zero(nBytes);.  
13b8a 20 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68 65    pBt->xFreeSche
13b8b 6d 61 20 3d 20 78 46 72 65 65 3b 0a 20 20 7d 0a  ma = xFree;.  }.
13b8c 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
13b8d 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
13b8e 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 3b 0a 7d   pBt->pSchema;.}
13b8f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
13b90 72 75 65 20 69 66 20 61 6e 6f 74 68 65 72 20 75  rue if another u
13b91 73 65 72 20 6f 66 20 74 68 65 20 73 61 6d 65 20  ser of the same 
13b92 73 68 61 72 65 64 20 62 74 72 65 65 20 61 73 20  shared btree as 
13b93 74 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  the argument.** 
13b94 68 61 6e 64 6c 65 20 68 6f 6c 64 73 20 61 6e 20  handle holds an 
13b95 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f  exclusive lock o
13b96 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  n the sqlite_mas
13b97 74 65 72 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51  ter table..*/.SQ
13b98 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
13b99 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68   sqlite3BtreeSch
13b9a 65 6d 61 4c 6f 63 6b 65 64 28 42 74 72 65 65 20  emaLocked(Btree 
13b9b 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  *p){.  int rc;. 
13b9c 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
13b9d 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64  _mutex_held(p->d
13b9e 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73  b->mutex) );.  s
13b9f 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
13ba0 28 70 29 3b 0a 20 20 72 63 20 3d 20 28 71 75 65  (p);.  rc = (que
13ba1 72 79 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 4d  ryTableLock(p, M
13ba2 41 53 54 45 52 5f 52 4f 4f 54 2c 20 52 45 41 44  ASTER_ROOT, READ
13ba3 5f 4c 4f 43 4b 29 21 3d 53 51 4c 49 54 45 5f 4f  _LOCK)!=SQLITE_O
13ba4 4b 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  K);.  sqlite3Btr
13ba5 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65  eeLeave(p);.  re
13ba6 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 69 66  turn rc;.}...#if
13ba7 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
13ba8 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a  _SHARED_CACHE./*
13ba9 0a 2a 2a 20 4f 62 74 61 69 6e 20 61 20 6c 6f 63  .** Obtain a loc
13baa 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77  k on the table w
13bab 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 20 69  hose root page i
13bac 73 20 69 54 61 62 2e 20 20 54 68 65 0a 2a 2a 20  s iTab.  The.** 
13bad 6c 6f 63 6b 20 69 73 20 61 20 77 72 69 74 65 20  lock is a write 
13bae 6c 6f 63 6b 20 69 66 20 69 73 57 72 69 74 65 6c  lock if isWritel
13baf 6f 63 6b 20 69 73 20 74 72 75 65 20 6f 72 20 61  ock is true or a
13bb0 20 72 65 61 64 20 6c 6f 63 6b 0a 2a 2a 20 69 66   read lock.** if
13bb1 20 69 74 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2f   it is false..*/
13bb2 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
13bb3 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
13bb4 4c 6f 63 6b 54 61 62 6c 65 28 42 74 72 65 65 20  LockTable(Btree 
13bb5 2a 70 2c 20 69 6e 74 20 69 54 61 62 2c 20 75 38  *p, int iTab, u8
13bb6 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 7b 0a 20   isWriteLock){. 
13bb7 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
13bb8 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 73 68  _OK;.  if( p->sh
13bb9 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 75 38  arable ){.    u8
13bba 20 6c 6f 63 6b 54 79 70 65 20 3d 20 52 45 41 44   lockType = READ
13bbb 5f 4c 4f 43 4b 20 2b 20 69 73 57 72 69 74 65 4c  _LOCK + isWriteL
13bbc 6f 63 6b 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ock;.    assert(
13bbd 20 52 45 41 44 5f 4c 4f 43 4b 2b 31 3d 3d 57 52   READ_LOCK+1==WR
13bbe 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20  ITE_LOCK );.    
13bbf 61 73 73 65 72 74 28 20 69 73 57 72 69 74 65 4c  assert( isWriteL
13bc0 6f 63 6b 3d 3d 30 20 7c 7c 20 69 73 57 72 69 74  ock==0 || isWrit
13bc1 65 4c 6f 63 6b 3d 3d 31 20 29 3b 0a 20 20 20 20  eLock==1 );.    
13bc2 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
13bc3 72 28 70 29 3b 0a 20 20 20 20 72 63 20 3d 20 71  r(p);.    rc = q
13bc4 75 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 70 2c  ueryTableLock(p,
13bc5 20 69 54 61 62 2c 20 6c 6f 63 6b 54 79 70 65 29   iTab, lockType)
13bc6 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
13bc7 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
13bc8 20 72 63 20 3d 20 6c 6f 63 6b 54 61 62 6c 65 28   rc = lockTable(
13bc9 70 2c 20 69 54 61 62 2c 20 6c 6f 63 6b 54 79 70  p, iTab, lockTyp
13bca 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  e);.    }.    sq
13bcb 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
13bcc 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  p);.  }.  return
13bcd 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23   rc;.}.#endif..#
13bce 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
13bcf 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a  IT_INCRBLOB./*.*
13bd0 2a 20 41 72 67 75 6d 65 6e 74 20 70 43 73 72 20  * Argument pCsr 
13bd1 6d 75 73 74 20 62 65 20 61 20 63 75 72 73 6f 72  must be a cursor
13bd2 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74   opened for writ
13bd3 69 6e 67 20 6f 6e 20 61 6e 20 0a 2a 2a 20 49 4e  ing on an .** IN
13bd4 54 4b 45 59 20 74 61 62 6c 65 20 63 75 72 72 65  TKEY table curre
13bd5 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 61 74  ntly pointing at
13bd6 20 61 20 76 61 6c 69 64 20 74 61 62 6c 65 20 65   a valid table e
13bd7 6e 74 72 79 2e 20 0a 2a 2a 20 54 68 69 73 20 66  ntry. .** This f
13bd8 75 6e 63 74 69 6f 6e 20 6d 6f 64 69 66 69 65 73  unction modifies
13bd9 20 74 68 65 20 64 61 74 61 20 73 74 6f 72 65 64   the data stored
13bda 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 61 74   as part of that
13bdb 20 65 6e 74 72 79 2e 0a 2a 2a 20 4f 6e 6c 79 20   entry..** Only 
13bdc 74 68 65 20 64 61 74 61 20 63 6f 6e 74 65 6e 74  the data content
13bdd 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 6d 6f 64   may only be mod
13bde 69 66 69 65 64 2c 20 69 74 20 69 73 20 6e 6f 74  ified, it is not
13bdf 20 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20   possible.** to 
13be0 63 68 61 6e 67 65 20 74 68 65 20 6c 65 6e 67 74  change the lengt
13be1 68 20 6f 66 20 74 68 65 20 64 61 74 61 20 73 74  h of the data st
13be2 6f 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ored..*/.SQLITE_
13be3 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
13be4 74 65 33 42 74 72 65 65 50 75 74 44 61 74 61 28  te3BtreePutData(
13be5 42 74 43 75 72 73 6f 72 20 2a 70 43 73 72 2c 20  BtCursor *pCsr, 
13be6 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20  u32 offset, u32 
13be7 61 6d 74 2c 20 76 6f 69 64 20 2a 7a 29 7b 0a 20  amt, void *z){. 
13be8 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
13be9 6f 6c 64 73 4d 75 74 65 78 28 70 43 73 72 29 20  oldsMutex(pCsr) 
13bea 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
13beb 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
13bec 70 43 73 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62  pCsr->pBtree->db
13bed 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
13bee 73 65 72 74 28 70 43 73 72 2d 3e 69 73 49 6e 63  sert(pCsr->isInc
13bef 72 62 6c 6f 62 48 61 6e 64 6c 65 29 3b 0a 20 20  rblobHandle);.  
13bf0 69 66 28 20 70 43 73 72 2d 3e 65 53 74 61 74 65  if( pCsr->eState
13bf1 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  >=CURSOR_REQUIRE
13bf2 53 45 45 4b 20 29 7b 0a 20 20 20 20 69 66 28 20  SEEK ){.    if( 
13bf3 70 43 73 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCsr->eState==CU
13bf4 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20  RSOR_FAULT ){.  
13bf5 20 20 20 20 72 65 74 75 72 6e 20 70 43 73 72 2d      return pCsr-
13bf6 3e 73 6b 69 70 3b 0a 20 20 20 20 7d 65 6c 73 65  >skip;.    }else
13bf7 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
13bf8 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 20  QLITE_ABORT;.   
13bf9 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65   }.  }..  /* Che
13bfa 63 6b 20 73 6f 6d 65 20 70 72 65 63 6f 6e 64 69  ck some precondi
13bfb 74 69 6f 6e 73 3a 20 0a 20 20 2a 2a 20 20 20 28  tions: .  **   (
13bfc 61 29 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  a) the cursor is
13bfd 20 6f 70 65 6e 20 66 6f 72 20 77 72 69 74 69 6e   open for writin
13bfe 67 2c 0a 20 20 2a 2a 20 20 20 28 62 29 20 74 68  g,.  **   (b) th
13bff 65 72 65 20 69 73 20 6e 6f 20 72 65 61 64 2d 6c  ere is no read-l
13c00 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  ock on the table
13c01 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 20   being modified 
13c02 61 6e 64 0a 20 20 2a 2a 20 20 20 28 63 29 20 74  and.  **   (c) t
13c03 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73  he cursor points
13c04 20 61 74 20 61 20 76 61 6c 69 64 20 72 6f 77 20   at a valid row 
13c05 6f 66 20 61 6e 20 69 6e 74 4b 65 79 20 74 61 62  of an intKey tab
13c06 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  le..  */.  if( !
13c07 70 43 73 72 2d 3e 77 72 46 6c 61 67 20 29 7b 0a  pCsr->wrFlag ){.
13c08 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
13c09 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a  E_READONLY;.  }.
13c0a 20 20 61 73 73 65 72 74 28 20 21 70 43 73 72 2d    assert( !pCsr-
13c0b 3e 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 0a  >pBt->readOnly .
13c0c 20 20 20 20 20 20 20 20 20 20 26 26 20 70 43 73            && pCs
13c0d 72 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61  r->pBt->inTransa
13c0e 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49  ction==TRANS_WRI
13c0f 54 45 20 29 3b 0a 20 20 69 66 28 20 63 68 65 63  TE );.  if( chec
13c10 6b 52 65 61 64 4c 6f 63 6b 73 28 70 43 73 72 2d  kReadLocks(pCsr-
13c11 3e 70 42 74 72 65 65 2c 20 70 43 73 72 2d 3e 70  >pBtree, pCsr->p
13c12 67 6e 6f 52 6f 6f 74 2c 20 70 43 73 72 29 20 29  gnoRoot, pCsr) )
13c13 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
13c14 49 54 45 5f 4c 4f 43 4b 45 44 3b 20 2f 2a 20 54  ITE_LOCKED; /* T
13c15 68 65 20 74 61 62 6c 65 20 70 43 75 72 20 70 6f  he table pCur po
13c16 69 6e 74 73 20 74 6f 20 68 61 73 20 61 20 72 65  ints to has a re
13c17 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 7d 0a 20  ad lock */.  }. 
13c18 20 69 66 28 20 70 43 73 72 2d 3e 65 53 74 61 74   if( pCsr->eStat
13c19 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49  e==CURSOR_INVALI
13c1a 44 20 7c 7c 20 21 70 43 73 72 2d 3e 70 50 61 67  D || !pCsr->pPag
13c1b 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20  e->intKey ){.   
13c1c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
13c1d 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  RROR;.  }..  ret
13c1e 75 72 6e 20 61 63 63 65 73 73 50 61 79 6c 6f 61  urn accessPayloa
13c1f 64 28 70 43 73 72 2c 20 6f 66 66 73 65 74 2c 20  d(pCsr, offset, 
13c20 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64 20 63  amt, (unsigned c
13c21 68 61 72 20 2a 29 7a 2c 20 30 2c 20 31 29 3b 0a  har *)z, 0, 1);.
13c22 7d 0a 0a 2f 2a 20 0a 2a 2a 20 53 65 74 20 61 20  }../* .** Set a 
13c23 66 6c 61 67 20 6f 6e 20 74 68 69 73 20 63 75 72  flag on this cur
13c24 73 6f 72 20 74 6f 20 63 61 63 68 65 20 74 68 65  sor to cache the
13c25 20 6c 6f 63 61 74 69 6f 6e 73 20 6f 66 20 70 61   locations of pa
13c26 67 65 73 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a  ges from the .**
13c27 20 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 66   overflow list f
13c28 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  or the current r
13c29 6f 77 2e 20 54 68 69 73 20 69 73 20 75 73 65 64  ow. This is used
13c2a 20 62 79 20 63 75 72 73 6f 72 73 20 6f 70 65 6e   by cursors open
13c2b 65 64 0a 2a 2a 20 66 6f 72 20 69 6e 63 72 65 6d  ed.** for increm
13c2c 65 6e 74 61 6c 20 62 6c 6f 62 20 49 4f 20 6f 6e  ental blob IO on
13c2d 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ly..**.** This f
13c2e 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 61 20 66  unction sets a f
13c2f 6c 61 67 20 6f 6e 6c 79 2e 20 54 68 65 20 61 63  lag only. The ac
13c30 74 75 61 6c 20 70 61 67 65 20 6c 6f 63 61 74 69  tual page locati
13c31 6f 6e 20 63 61 63 68 65 0a 2a 2a 20 28 73 74 6f  on cache.** (sto
13c32 72 65 64 20 69 6e 20 42 74 43 75 72 73 6f 72 2e  red in BtCursor.
13c33 61 4f 76 65 72 66 6c 6f 77 5b 5d 29 20 69 73 20  aOverflow[]) is 
13c34 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 75 73  allocated and us
13c35 65 64 20 62 79 20 66 75 6e 63 74 69 6f 6e 0a 2a  ed by function.*
13c36 2a 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28  * accessPayload(
13c37 29 20 28 74 68 65 20 77 6f 72 6b 65 72 20 66 75  ) (the worker fu
13c38 6e 63 74 69 6f 6e 20 66 6f 72 20 73 71 6c 69 74  nction for sqlit
13c39 65 33 42 74 72 65 65 44 61 74 61 28 29 20 61 6e  e3BtreeData() an
13c3a 64 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65  d.** sqlite3Btre
13c3b 65 50 75 74 44 61 74 61 28 29 29 2e 0a 2a 2f 0a  ePutData())..*/.
13c3c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
13c3d 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
13c3e 43 61 63 68 65 4f 76 65 72 66 6c 6f 77 28 42 74  CacheOverflow(Bt
13c3f 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
13c40 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
13c41 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
13c42 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
13c43 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
13c44 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62  pCur->pBtree->db
13c45 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
13c46 73 65 72 74 28 21 70 43 75 72 2d 3e 69 73 49 6e  sert(!pCur->isIn
13c47 63 72 62 6c 6f 62 48 61 6e 64 6c 65 29 3b 0a 20  crblobHandle);. 
13c48 20 61 73 73 65 72 74 28 21 70 43 75 72 2d 3e 61   assert(!pCur->a
13c49 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43 75  Overflow);.  pCu
13c4a 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e  r->isIncrblobHan
13c4b 64 6c 65 20 3d 20 31 3b 0a 7d 0a 23 65 6e 64 69  dle = 1;.}.#endi
13c4c 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f../************
13c4d 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65 2e  ** End of btree.
13c4e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
13c4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13c51 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
13c52 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64  ** Begin file vd
13c53 62 65 66 69 66 6f 2e 63 20 2a 2a 2a 2a 2a 2a 2a  befifo.c *******
13c54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13c55 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13c56 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4a 75  */./*.** 2005 Ju
13c57 6e 65 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ne 16.**.** The 
13c58 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
13c59 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
13c5a 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
13c5b 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
13c5c 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
13c5d 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
13c5e 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
13c5f 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
13c60 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
13c61 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
13c62 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
13c63 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
13c64 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
13c65 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
13c66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
13c67 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
13c68 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
13c69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13c6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13c6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13c6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13c6d 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
13c6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 46  e implements a F
13c6f 49 46 4f 20 71 75 65 75 65 20 6f 66 20 72 6f 77  IFO queue of row
13c70 69 64 73 20 75 73 65 64 20 66 6f 72 20 70 72 6f  ids used for pro
13c71 63 65 73 73 69 6e 67 0a 2a 2a 20 55 50 44 41 54  cessing.** UPDAT
13c72 45 20 61 6e 64 20 44 45 4c 45 54 45 20 73 74 61  E and DELETE sta
13c73 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 0a 2f 2a 0a  tements..*/../*.
13c74 2a 2a 20 43 6f 6e 73 74 61 6e 74 73 20 46 49 46  ** Constants FIF
13c75 4f 53 49 5a 45 5f 46 49 52 53 54 20 61 6e 64 20  OSIZE_FIRST and 
13c76 46 49 46 4f 53 49 5a 45 5f 4d 41 58 20 61 72 65  FIFOSIZE_MAX are
13c77 20 74 68 65 20 69 6e 69 74 69 61 6c 0a 2a 2a 20   the initial.** 
13c78 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  number of entrie
13c79 73 20 69 6e 20 61 20 66 69 66 6f 20 70 61 67 65  s in a fifo page
13c7a 20 61 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d   and the maximum
13c7b 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6e   number of.** en
13c7c 74 72 69 65 73 20 69 6e 20 61 20 66 69 66 6f 20  tries in a fifo 
13c7d 70 61 67 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  page..*/.#define
13c7e 20 46 49 46 4f 53 49 5a 45 5f 46 49 52 53 54 20   FIFOSIZE_FIRST 
13c7f 28 28 28 31 32 38 2d 73 69 7a 65 6f 66 28 46 69  (((128-sizeof(Fi
13c80 66 6f 50 61 67 65 29 29 2f 38 29 2b 31 29 0a 23  foPage))/8)+1).#
13c81 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 4c  ifdef SQLITE_MAL
13c82 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 0a 23  LOC_SOFT_LIMIT.#
13c83 20 64 65 66 69 6e 65 20 46 49 46 4f 53 49 5a 45   define FIFOSIZE
13c84 5f 4d 41 58 20 20 20 28 28 28 53 51 4c 49 54 45  _MAX   (((SQLITE
13c85 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d  _MALLOC_SOFT_LIM
13c86 49 54 2d 73 69 7a 65 6f 66 28 46 69 66 6f 50 61  IT-sizeof(FifoPa
13c87 67 65 29 29 2f 38 29 2b 31 29 0a 23 65 6c 73 65  ge))/8)+1).#else
13c88 0a 23 20 64 65 66 69 6e 65 20 46 49 46 4f 53 49  .# define FIFOSI
13c89 5a 45 5f 4d 41 58 20 20 20 28 28 28 32 36 32 31  ZE_MAX   (((2621
13c8a 34 34 2d 73 69 7a 65 6f 66 28 46 69 66 6f 50 61  44-sizeof(FifoPa
13c8b 67 65 29 29 2f 38 29 2b 31 29 0a 23 65 6e 64 69  ge))/8)+1).#endi
13c8c 66 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  f../*.** Allocat
13c8d 65 20 61 20 6e 65 77 20 46 69 66 6f 50 61 67 65  e a new FifoPage
13c8e 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f   and return a po
13c8f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 52 65  inter to it.  Re
13c90 74 75 72 6e 20 4e 55 4c 4c 20 69 66 0a 2a 2a 20  turn NULL if.** 
13c91 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65  we run out of me
13c92 6d 6f 72 79 2e 20 20 4c 65 61 76 65 20 73 70 61  mory.  Leave spa
13c93 63 65 20 6f 6e 20 74 68 65 20 70 61 67 65 20 66  ce on the page f
13c94 6f 72 20 6e 45 6e 74 72 79 20 65 6e 74 72 69 65  or nEntry entrie
13c95 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 46 69 66  s..*/.static Fif
13c96 6f 50 61 67 65 20 2a 61 6c 6c 6f 63 61 74 65 46  oPage *allocateF
13c97 69 66 6f 50 61 67 65 28 69 6e 74 20 6e 45 6e 74  ifoPage(int nEnt
13c98 72 79 29 7b 0a 20 20 46 69 66 6f 50 61 67 65 20  ry){.  FifoPage 
13c99 2a 70 50 61 67 65 3b 0a 20 20 69 66 28 20 6e 45  *pPage;.  if( nE
13c9a 6e 74 72 79 3e 46 49 46 4f 53 49 5a 45 5f 4d 41  ntry>FIFOSIZE_MA
13c9b 58 20 29 7b 0a 20 20 20 20 6e 45 6e 74 72 79 20  X ){.    nEntry 
13c9c 3d 20 46 49 46 4f 53 49 5a 45 5f 4d 41 58 3b 0a  = FIFOSIZE_MAX;.
13c9d 20 20 7d 0a 20 20 70 50 61 67 65 20 3d 20 73 71    }.  pPage = sq
13c9e 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69  lite3_malloc( si
13c9f 7a 65 6f 66 28 46 69 66 6f 50 61 67 65 29 20 2b  zeof(FifoPage) +
13ca0 20 73 69 7a 65 6f 66 28 69 36 34 29 2a 28 6e 45   sizeof(i64)*(nE
13ca1 6e 74 72 79 2d 31 29 20 29 3b 0a 20 20 69 66 28  ntry-1) );.  if(
13ca2 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70 50   pPage ){.    pP
13ca3 61 67 65 2d 3e 6e 53 6c 6f 74 20 3d 20 6e 45 6e  age->nSlot = nEn
13ca4 74 72 79 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  try;.    pPage->
13ca5 69 57 72 69 74 65 20 3d 20 30 3b 0a 20 20 20 20  iWrite = 0;.    
13ca6 70 50 61 67 65 2d 3e 69 52 65 61 64 20 3d 20 30  pPage->iRead = 0
13ca7 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4e 65  ;.    pPage->pNe
13ca8 78 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  xt = 0;.  }.  re
13ca9 74 75 72 6e 20 70 50 61 67 65 3b 0a 7d 0a 0a 2f  turn pPage;.}../
13caa 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
13cab 61 20 46 69 66 6f 20 73 74 72 75 63 74 75 72 65  a Fifo structure
13cac 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
13cad 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
13cae 56 64 62 65 46 69 66 6f 49 6e 69 74 28 46 69 66  VdbeFifoInit(Fif
13caf 6f 20 2a 70 46 69 66 6f 29 7b 0a 20 20 6d 65 6d  o *pFifo){.  mem
13cb0 73 65 74 28 70 46 69 66 6f 2c 20 30 2c 20 73 69  set(pFifo, 0, si
13cb1 7a 65 6f 66 28 2a 70 46 69 66 6f 29 29 3b 0a 7d  zeof(*pFifo));.}
13cb2 0a 0a 2f 2a 0a 2a 2a 20 50 75 73 68 20 61 20 73  ../*.** Push a s
13cb3 69 6e 67 6c 65 20 36 34 2d 62 69 74 20 69 6e 74  ingle 64-bit int
13cb4 65 67 65 72 20 76 61 6c 75 65 20 69 6e 74 6f 20  eger value into 
13cb5 74 68 65 20 46 69 66 6f 2e 20 20 52 65 74 75 72  the Fifo.  Retur
13cb6 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6e  n SQLITE_OK.** n
13cb7 6f 72 6d 61 6c 6c 79 2e 20 20 20 53 51 4c 49 54  ormally.   SQLIT
13cb8 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72  E_NOMEM is retur
13cb9 6e 65 64 20 69 66 20 77 65 20 61 72 65 20 75 6e  ned if we are un
13cba 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65  able to allocate
13cbb 0a 2a 2a 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 53  .** memory..*/.S
13cbc 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
13cbd 74 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 66  t sqlite3VdbeFif
13cbe 6f 50 75 73 68 28 46 69 66 6f 20 2a 70 46 69 66  oPush(Fifo *pFif
13cbf 6f 2c 20 69 36 34 20 76 61 6c 29 7b 0a 20 20 46  o, i64 val){.  F
13cc0 69 66 6f 50 61 67 65 20 2a 70 50 61 67 65 3b 0a  ifoPage *pPage;.
13cc1 20 20 70 50 61 67 65 20 3d 20 70 46 69 66 6f 2d    pPage = pFifo-
13cc2 3e 70 4c 61 73 74 3b 0a 20 20 69 66 28 20 70 50  >pLast;.  if( pP
13cc3 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 70 50  age==0 ){.    pP
13cc4 61 67 65 20 3d 20 70 46 69 66 6f 2d 3e 70 4c 61  age = pFifo->pLa
13cc5 73 74 20 3d 20 70 46 69 66 6f 2d 3e 70 46 69 72  st = pFifo->pFir
13cc6 73 74 20 3d 20 61 6c 6c 6f 63 61 74 65 46 69 66  st = allocateFif
13cc7 6f 50 61 67 65 28 46 49 46 4f 53 49 5a 45 5f 46  oPage(FIFOSIZE_F
13cc8 49 52 53 54 29 3b 0a 20 20 20 20 69 66 28 20 70  IRST);.    if( p
13cc9 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Page==0 ){.     
13cca 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
13ccb 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  OMEM;.    }.  }e
13ccc 6c 73 65 20 69 66 28 20 70 50 61 67 65 2d 3e 69  lse if( pPage->i
13ccd 57 72 69 74 65 3e 3d 70 50 61 67 65 2d 3e 6e 53  Write>=pPage->nS
13cce 6c 6f 74 20 29 7b 0a 20 20 20 20 70 50 61 67 65  lot ){.    pPage
13ccf 2d 3e 70 4e 65 78 74 20 3d 20 61 6c 6c 6f 63 61  ->pNext = alloca
13cd0 74 65 46 69 66 6f 50 61 67 65 28 70 46 69 66 6f  teFifoPage(pFifo
13cd1 2d 3e 6e 45 6e 74 72 79 29 3b 0a 20 20 20 20 69  ->nEntry);.    i
13cd2 66 28 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 3d  f( pPage->pNext=
13cd3 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =0 ){.      retu
13cd4 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
13cd5 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65  .    }.    pPage
13cd6 20 3d 20 70 46 69 66 6f 2d 3e 70 4c 61 73 74 20   = pFifo->pLast 
13cd7 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a  = pPage->pNext;.
13cd8 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 61 53 6c    }.  pPage->aSl
13cd9 6f 74 5b 70 50 61 67 65 2d 3e 69 57 72 69 74 65  ot[pPage->iWrite
13cda 2b 2b 5d 20 3d 20 76 61 6c 3b 0a 20 20 70 46 69  ++] = val;.  pFi
13cdb 66 6f 2d 3e 6e 45 6e 74 72 79 2b 2b 3b 0a 20 20  fo->nEntry++;.  
13cdc 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
13cdd 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61  ;.}../*.** Extra
13cde 63 74 20 61 20 73 69 6e 67 6c 65 20 36 34 2d 62  ct a single 64-b
13cdf 69 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  it integer value
13ce0 20 66 72 6f 6d 20 74 68 65 20 46 69 66 6f 2e 20   from the Fifo. 
13ce1 20 54 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20   The integer.** 
13ce2 65 78 74 72 61 63 74 65 64 20 69 73 20 74 68 65  extracted is the
13ce3 20 6f 6e 65 20 6c 65 61 73 74 20 72 65 63 65 6e   one least recen
13ce4 74 6c 79 20 69 6e 73 65 72 74 65 64 2e 20 20 49  tly inserted.  I
13ce5 66 20 74 68 65 20 46 69 66 6f 20 69 73 20 65 6d  f the Fifo is em
13ce6 70 74 79 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51  pty.** return SQ
13ce7 4c 49 54 45 5f 44 4f 4e 45 2e 0a 2a 2f 0a 53 51  LITE_DONE..*/.SQ
13ce8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
13ce9 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 66 6f   sqlite3VdbeFifo
13cea 50 6f 70 28 46 69 66 6f 20 2a 70 46 69 66 6f 2c  Pop(Fifo *pFifo,
13ceb 20 69 36 34 20 2a 70 56 61 6c 29 7b 0a 20 20 46   i64 *pVal){.  F
13cec 69 66 6f 50 61 67 65 20 2a 70 50 61 67 65 3b 0a  ifoPage *pPage;.
13ced 20 20 69 66 28 20 70 46 69 66 6f 2d 3e 6e 45 6e    if( pFifo->nEn
13cee 74 72 79 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  try==0 ){.    re
13cef 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45  turn SQLITE_DONE
13cf0 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
13cf1 70 46 69 66 6f 2d 3e 6e 45 6e 74 72 79 3e 30 20  pFifo->nEntry>0 
13cf2 29 3b 0a 20 20 70 50 61 67 65 20 3d 20 70 46 69  );.  pPage = pFi
13cf3 66 6f 2d 3e 70 46 69 72 73 74 3b 0a 20 20 61 73  fo->pFirst;.  as
13cf4 73 65 72 74 28 20 70 50 61 67 65 21 3d 30 20 29  sert( pPage!=0 )
13cf5 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
13cf6 65 2d 3e 69 57 72 69 74 65 3e 70 50 61 67 65 2d  e->iWrite>pPage-
13cf7 3e 69 52 65 61 64 20 29 3b 0a 20 20 61 73 73 65  >iRead );.  asse
13cf8 72 74 28 20 70 50 61 67 65 2d 3e 69 57 72 69 74  rt( pPage->iWrit
13cf9 65 3c 3d 70 50 61 67 65 2d 3e 6e 53 6c 6f 74 20  e<=pPage->nSlot 
13cfa 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
13cfb 67 65 2d 3e 69 52 65 61 64 3c 70 50 61 67 65 2d  ge->iRead<pPage-
13cfc 3e 6e 53 6c 6f 74 20 29 3b 0a 20 20 61 73 73 65  >nSlot );.  asse
13cfd 72 74 28 20 70 50 61 67 65 2d 3e 69 52 65 61 64  rt( pPage->iRead
13cfe 3e 3d 30 20 29 3b 0a 20 20 2a 70 56 61 6c 20 3d  >=0 );.  *pVal =
13cff 20 70 50 61 67 65 2d 3e 61 53 6c 6f 74 5b 70 50   pPage->aSlot[pP
13d00 61 67 65 2d 3e 69 52 65 61 64 2b 2b 5d 3b 0a 20  age->iRead++];. 
13d01 20 70 46 69 66 6f 2d 3e 6e 45 6e 74 72 79 2d 2d   pFifo->nEntry--
13d02 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69  ;.  if( pPage->i
13d03 52 65 61 64 3e 3d 70 50 61 67 65 2d 3e 69 57 72  Read>=pPage->iWr
13d04 69 74 65 20 29 7b 0a 20 20 20 20 70 46 69 66 6f  ite ){.    pFifo
13d05 2d 3e 70 46 69 72 73 74 20 3d 20 70 50 61 67 65  ->pFirst = pPage
13d06 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c  ->pNext;.    sql
13d07 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 65 29  ite3_free(pPage)
13d08 3b 0a 20 20 20 20 69 66 28 20 70 46 69 66 6f 2d  ;.    if( pFifo-
13d09 3e 6e 45 6e 74 72 79 3d 3d 30 20 29 7b 0a 20 20  >nEntry==0 ){.  
13d0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 66      assert( pFif
13d0b 6f 2d 3e 70 4c 61 73 74 3d 3d 70 50 61 67 65 20  o->pLast==pPage 
13d0c 29 3b 0a 20 20 20 20 20 20 70 46 69 66 6f 2d 3e  );.      pFifo->
13d0d 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20 20 20 7d  pLast = 0;.    }
13d0e 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65  else{.      asse
13d0f 72 74 28 20 70 46 69 66 6f 2d 3e 70 46 69 72 73  rt( pFifo->pFirs
13d10 74 21 3d 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20  t!=0 );.    }.  
13d11 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
13d12 74 28 20 70 46 69 66 6f 2d 3e 6e 45 6e 74 72 79  t( pFifo->nEntry
13d13 3e 30 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  >0 );.  }.  retu
13d14 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
13d15 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c  ./*.** Delete al
13d16 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72  l information fr
13d17 6f 6d 20 61 20 46 69 66 6f 20 6f 62 6a 65 63 74  om a Fifo object
13d18 2e 20 20 20 46 72 65 65 20 61 6c 6c 20 6d 65 6d  .   Free all mem
13d19 6f 72 79 20 68 65 6c 64 0a 2a 2a 20 62 79 20 74  ory held.** by t
13d1a 68 65 20 46 69 66 6f 2e 0a 2a 2f 0a 53 51 4c 49  he Fifo..*/.SQLI
13d1b 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
13d1c 73 71 6c 69 74 65 33 56 64 62 65 46 69 66 6f 43  sqlite3VdbeFifoC
13d1d 6c 65 61 72 28 46 69 66 6f 20 2a 70 46 69 66 6f  lear(Fifo *pFifo
13d1e 29 7b 0a 20 20 46 69 66 6f 50 61 67 65 20 2a 70  ){.  FifoPage *p
13d1f 50 61 67 65 2c 20 2a 70 4e 65 78 74 50 61 67 65  Page, *pNextPage
13d20 3b 0a 20 20 66 6f 72 28 70 50 61 67 65 3d 70 46  ;.  for(pPage=pF
13d21 69 66 6f 2d 3e 70 46 69 72 73 74 3b 20 70 50 61  ifo->pFirst; pPa
13d22 67 65 3b 20 70 50 61 67 65 3d 70 4e 65 78 74 50  ge; pPage=pNextP
13d23 61 67 65 29 7b 0a 20 20 20 20 70 4e 65 78 74 50  age){.    pNextP
13d24 61 67 65 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65  age = pPage->pNe
13d25 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  xt;.    sqlite3_
13d26 66 72 65 65 28 70 50 61 67 65 29 3b 0a 20 20 7d  free(pPage);.  }
13d27 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69  .  sqlite3VdbeFi
13d28 66 6f 49 6e 69 74 28 70 46 69 66 6f 29 3b 0a 7d  foInit(pFifo);.}
13d29 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
13d2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 66 69 66  * End of vdbefif
13d2b 6f 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  o.c ************
13d2c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13d2d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13d2e 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
13d2f 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62  * Begin file vdb
13d30 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  emem.c *********
13d31 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13d32 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13d33 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79  /./*.** 2004 May
13d34 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   26.**.** The au
13d35 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
13d36 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
13d37 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
13d38 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
13d39 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
13d3a 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
13d3b 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
13d3c 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
13d3d 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
13d3e 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
13d3f 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
13d40 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
13d41 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
13d42 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
13d43 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
13d44 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
13d45 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
13d46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13d47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13d48 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13d49 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13d4a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
13d4b 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
13d4c 20 75 73 65 20 74 6f 20 6d 61 6e 69 70 75 6c 61   use to manipula
13d4d 74 65 20 22 4d 65 6d 22 20 73 74 72 75 63 74 75  te "Mem" structu
13d4e 72 65 2e 20 20 41 20 22 4d 65 6d 22 0a 2a 2a 20  re.  A "Mem".** 
13d4f 73 74 6f 72 65 73 20 61 20 73 69 6e 67 6c 65 20  stores a single 
13d50 76 61 6c 75 65 20 69 6e 20 74 68 65 20 56 44 42  value in the VDB
13d51 45 2e 20 20 4d 65 6d 20 69 73 20 61 6e 20 6f 70  E.  Mem is an op
13d52 61 71 75 65 20 73 74 72 75 63 74 75 72 65 20 76  aque structure v
13d53 69 73 69 62 6c 65 0a 2a 2a 20 6f 6e 6c 79 20 77  isible.** only w
13d54 69 74 68 69 6e 20 74 68 65 20 56 44 42 45 2e 20  ithin the VDBE. 
13d55 20 49 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69   Interface routi
13d56 6e 65 73 20 72 65 66 65 72 20 74 6f 20 61 20 4d  nes refer to a M
13d57 65 6d 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  em using the.** 
13d58 6e 61 6d 65 20 73 71 6c 69 74 65 5f 76 61 6c 75  name sqlite_valu
13d59 65 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c  e.*/../*.** Call
13d5a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45   sqlite3VdbeMemE
13d5b 78 70 61 6e 64 42 6c 6f 62 28 29 20 6f 6e 20 74  xpandBlob() on t
13d5c 68 65 20 73 75 70 70 6c 69 65 64 20 76 61 6c 75  he supplied valu
13d5d 65 20 28 74 79 70 65 20 4d 65 6d 2a 29 0a 2a 2a  e (type Mem*).**
13d5e 20 50 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a   P if required..
13d5f 2a 2f 0a 23 64 65 66 69 6e 65 20 65 78 70 61 6e  */.#define expan
13d60 64 42 6c 6f 62 28 50 29 20 28 28 28 50 29 2d 3e  dBlob(P) (((P)->
13d61 66 6c 61 67 73 26 4d 45 4d 5f 5a 65 72 6f 29 3f  flags&MEM_Zero)?
13d62 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78  sqlite3VdbeMemEx
13d63 70 61 6e 64 42 6c 6f 62 28 50 29 3a 30 29 0a 0a  pandBlob(P):0)..
13d64 2f 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73  /*.** If pMem is
13d65 20 61 6e 20 6f 62 6a 65 63 74 20 77 69 74 68 20   an object with 
13d66 61 20 76 61 6c 69 64 20 73 74 72 69 6e 67 20 72  a valid string r
13d67 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2c 20 74  epresentation, t
13d68 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65  his routine.** e
13d69 6e 73 75 72 65 73 20 74 68 65 20 69 6e 74 65 72  nsures the inter
13d6a 6e 61 6c 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72  nal encoding for
13d6b 20 74 68 65 20 73 74 72 69 6e 67 20 72 65 70 72   the string repr
13d6c 65 73 65 6e 74 61 74 69 6f 6e 20 69 73 0a 2a 2a  esentation is.**
13d6d 20 27 64 65 73 69 72 65 64 45 6e 63 27 2c 20 6f   'desiredEnc', o
13d6e 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 55 54 46  ne of SQLITE_UTF
13d6f 38 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  8, SQLITE_UTF16L
13d70 45 20 6f 72 20 53 51 4c 49 54 45 5f 55 54 46 31  E or SQLITE_UTF1
13d71 36 42 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d  6BE..**.** If pM
13d72 65 6d 20 69 73 20 6e 6f 74 20 61 20 73 74 72 69  em is not a stri
13d73 6e 67 20 6f 62 6a 65 63 74 2c 20 6f 72 20 74 68  ng object, or th
13d74 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68  e encoding of th
13d75 65 20 73 74 72 69 6e 67 0a 2a 2a 20 72 65 70 72  e string.** repr
13d76 65 73 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 6c  esentation is al
13d77 72 65 61 64 79 20 73 74 6f 72 65 64 20 75 73 69  ready stored usi
13d78 6e 67 20 74 68 65 20 72 65 71 75 65 73 74 65 64  ng the requested
13d79 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20   encoding, then 
13d7a 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  this.** routine 
13d7b 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  is a no-op..**.*
13d7c 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  * SQLITE_OK is r
13d7d 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 63  eturned if the c
13d7e 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 73 75 63  onversion is suc
13d7f 63 65 73 73 66 75 6c 20 28 6f 72 20 6e 6f 74 20  cessful (or not 
13d80 72 65 71 75 69 72 65 64 29 2e 0a 2a 2a 20 53 51  required)..** SQ
13d81 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6d 61 79 20 62  LITE_NOMEM may b
13d82 65 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 20  e returned if a 
13d83 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20 64  malloc() fails d
13d84 75 72 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e  uring conversion
13d85 0a 2a 2a 20 62 65 74 77 65 65 6e 20 66 6f 72 6d  .** between form
13d86 61 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ats..*/.SQLITE_P
13d87 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
13d88 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f  e3VdbeChangeEnco
13d89 64 69 6e 67 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20  ding(Mem *pMem, 
13d8a 69 6e 74 20 64 65 73 69 72 65 64 45 6e 63 29 7b  int desiredEnc){
13d8b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28  .  int rc;.  if(
13d8c 20 21 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d   !(pMem->flags&M
13d8d 45 4d 5f 53 74 72 29 20 7c 7c 20 70 4d 65 6d 2d  EM_Str) || pMem-
13d8e 3e 65 6e 63 3d 3d 64 65 73 69 72 65 64 45 6e 63  >enc==desiredEnc
13d8f 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
13d90 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
13d91 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62  assert( pMem->db
13d92 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d  ==0 || sqlite3_m
13d93 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e  utex_held(pMem->
13d94 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 23 69  db->mutex) );.#i
13d95 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
13d96 5f 55 54 46 31 36 0a 20 20 72 65 74 75 72 6e 20  _UTF16.  return 
13d97 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 23 65  SQLITE_ERROR;.#e
13d98 6c 73 65 0a 0a 20 20 2f 2a 20 4d 65 6d 54 72 61  lse..  /* MemTra
13d99 6e 73 6c 61 74 65 28 29 20 6d 61 79 20 72 65 74  nslate() may ret
13d9a 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72  urn SQLITE_OK or
13d9b 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49   SQLITE_NOMEM. I
13d9c 66 20 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72  f NOMEM is retur
13d9d 6e 65 64 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74  ned,.  ** then t
13d9e 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74  he encoding of t
13d9f 68 65 20 76 61 6c 75 65 20 6d 61 79 20 6e 6f 74  he value may not
13da0 20 68 61 76 65 20 63 68 61 6e 67 65 64 2e 0a 20   have changed.. 
13da1 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   */.  rc = sqlit
13da2 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61  e3VdbeMemTransla
13da3 74 65 28 70 4d 65 6d 2c 20 64 65 73 69 72 65 64  te(pMem, desired
13da4 45 6e 63 29 3b 0a 20 20 61 73 73 65 72 74 28 72  Enc);.  assert(r
13da5 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20  c==SQLITE_OK    
13da6 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  || rc==SQLITE_NO
13da7 4d 45 4d 29 3b 0a 20 20 61 73 73 65 72 74 28 72  MEM);.  assert(r
13da8 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20  c==SQLITE_OK    
13da9 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65  || pMem->enc!=de
13daa 73 69 72 65 64 45 6e 63 29 3b 0a 20 20 61 73 73  siredEnc);.  ass
13dab 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  ert(rc==SQLITE_N
13dac 4f 4d 45 4d 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e  OMEM || pMem->en
13dad 63 3d 3d 64 65 73 69 72 65 64 45 6e 63 29 3b 0a  c==desiredEnc);.
13dae 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e    return rc;.#en
13daf 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  dif.}../*.** Mak
13db0 65 20 73 75 72 65 20 70 4d 65 6d 2d 3e 7a 20 70  e sure pMem->z p
13db1 6f 69 6e 74 73 20 74 6f 20 61 20 77 72 69 74 61  oints to a writa
13db2 62 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f  ble allocation o
13db3 66 20 61 74 20 6c 65 61 73 74 20 0a 2a 2a 20 6e  f at least .** n
13db4 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66   bytes..**.** If
13db5 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c   the memory cell
13db6 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61   currently conta
13db7 69 6e 73 20 73 74 72 69 6e 67 20 6f 72 20 62 6c  ins string or bl
13db8 6f 62 20 64 61 74 61 0a 2a 2a 20 61 6e 64 20 74  ob data.** and t
13db9 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e  he third argumen
13dba 74 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73  t passed to this
13dbb 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 72 75   function is tru
13dbc 65 2c 20 74 68 65 20 0a 2a 2a 20 63 75 72 72 65  e, the .** curre
13dbd 6e 74 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68  nt content of th
13dbe 65 20 63 65 6c 6c 20 69 73 20 70 72 65 73 65 72  e cell is preser
13dbf 76 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ved. Otherwise, 
13dc0 69 74 20 6d 61 79 0a 2a 2a 20 62 65 20 64 69 73  it may.** be dis
13dc1 63 61 72 64 65 64 2e 20 20 0a 2a 2a 0a 2a 2a 20  carded.  .**.** 
13dc2 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 65  This function se
13dc3 74 73 20 74 68 65 20 4d 45 4d 5f 44 79 6e 20 66  ts the MEM_Dyn f
13dc4 6c 61 67 20 61 6e 64 20 63 6c 65 61 72 73 20 61  lag and clears a
13dc5 6e 79 20 78 44 65 6c 20 63 61 6c 6c 62 61 63 6b  ny xDel callback
13dc6 2e 0a 2a 2a 20 49 74 20 61 6c 73 6f 20 63 6c 65  ..** It also cle
13dc7 61 72 73 20 4d 45 4d 5f 45 70 68 65 6d 20 61 6e  ars MEM_Ephem an
13dc8 64 20 4d 45 4d 5f 53 74 61 74 69 63 2e 20 49 66  d MEM_Static. If
13dc9 20 74 68 65 20 70 72 65 73 65 72 76 65 20 66 6c   the preserve fl
13dca 61 67 20 69 73 20 0a 2a 2a 20 6e 6f 74 20 73 65  ag is .** not se
13dcb 74 2c 20 4d 65 6d 2e 6e 20 69 73 20 7a 65 72 6f  t, Mem.n is zero
13dcc 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
13dcd 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
13dce 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 4d 65 6d  3VdbeMemGrow(Mem
13dcf 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 2c 20 69   *pMem, int n, i
13dd0 6e 74 20 70 72 65 73 65 72 76 65 29 7b 0a 20 20  nt preserve){.  
13dd1 61 73 73 65 72 74 28 20 31 20 3e 3d 0a 20 20 20  assert( 1 >=.   
13dd2 20 28 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63   ((pMem->zMalloc
13dd3 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f   && pMem->zMallo
13dd4 63 3d 3d 70 4d 65 6d 2d 3e 7a 29 20 3f 20 31 20  c==pMem->z) ? 1 
13dd5 3a 20 30 29 20 2b 0a 20 20 20 20 28 28 28 70 4d  : 0) +.    (((pM
13dd6 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79  em->flags&MEM_Dy
13dd7 6e 29 26 26 70 4d 65 6d 2d 3e 78 44 65 6c 29 20  n)&&pMem->xDel) 
13dd8 3f 20 31 20 3a 20 30 29 20 2b 20 0a 20 20 20 20  ? 1 : 0) + .    
13dd9 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45  ((pMem->flags&ME
13dda 4d 5f 45 70 68 65 6d 29 20 3f 20 31 20 3a 20 30  M_Ephem) ? 1 : 0
13ddb 29 20 2b 20 0a 20 20 20 20 28 28 70 4d 65 6d 2d  ) + .    ((pMem-
13ddc 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74 69  >flags&MEM_Stati
13ddd 63 29 20 3f 20 31 20 3a 20 30 29 0a 20 20 29 3b  c) ? 1 : 0).  );
13dde 0a 0a 20 20 69 66 28 20 21 70 4d 65 6d 2d 3e 7a  ..  if( !pMem->z
13ddf 4d 61 6c 6c 6f 63 20 7c 7c 20 73 71 6c 69 74 65  Malloc || sqlite
13de0 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4d 65 6d  3MallocSize(pMem
13de1 2d 3e 7a 4d 61 6c 6c 6f 63 29 3c 6e 20 29 7b 0a  ->zMalloc)<n ){.
13de2 20 20 20 20 6e 20 3d 20 28 6e 3e 33 32 3f 6e 3a      n = (n>32?n:
13de3 33 32 29 3b 0a 20 20 20 20 69 66 28 20 70 72 65  32);.    if( pre
13de4 73 65 72 76 65 20 26 26 20 70 4d 65 6d 2d 3e 7a  serve && pMem->z
13de5 3d 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20  ==pMem->zMalloc 
13de6 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a  ){.      pMem->z
13de7 20 3d 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63   = pMem->zMalloc
13de8 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c   = sqlite3DbReal
13de9 6c 6f 63 4f 72 46 72 65 65 28 70 4d 65 6d 2d 3e  locOrFree(pMem->
13dea 64 62 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 6e 29 3b  db, pMem->z, n);
13deb 0a 20 20 20 20 20 20 69 66 28 20 21 70 4d 65 6d  .      if( !pMem
13dec 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ->z ){.        p
13ded 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
13dee 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20  _Null;.      }. 
13def 20 20 20 20 20 70 72 65 73 65 72 76 65 20 3d 20       preserve = 
13df0 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  0;.    }else{.  
13df1 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
13df2 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b  (pMem->zMalloc);
13df3 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61  .      pMem->zMa
13df4 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62  lloc = sqlite3Db
13df5 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d 2d 3e  MallocRaw(pMem->
13df6 64 62 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20  db, n);.    }.  
13df7 7d 0a 0a 20 20 69 66 28 20 70 72 65 73 65 72 76  }..  if( preserv
13df8 65 20 26 26 20 70 4d 65 6d 2d 3e 7a 20 26 26 20  e && pMem->z && 
13df9 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 26 26  pMem->zMalloc &&
13dfa 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e   pMem->z!=pMem->
13dfb 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 6d  zMalloc ){.    m
13dfc 65 6d 63 70 79 28 70 4d 65 6d 2d 3e 7a 4d 61 6c  emcpy(pMem->zMal
13dfd 6c 6f 63 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 70 4d  loc, pMem->z, pM
13dfe 65 6d 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 20 69 66  em->n);.  }.  if
13dff 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45  ( pMem->flags&ME
13e00 4d 5f 44 79 6e 20 26 26 20 70 4d 65 6d 2d 3e 78  M_Dyn && pMem->x
13e01 44 65 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d  Del ){.    pMem-
13e02 3e 78 44 65 6c 28 28 76 6f 69 64 20 2a 29 28 70  >xDel((void *)(p
13e03 4d 65 6d 2d 3e 7a 29 29 3b 0a 20 20 7d 0a 0a 20  Mem->z));.  }.. 
13e04 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d   pMem->z = pMem-
13e05 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 70 4d 65 6d  >zMalloc;.  pMem
13e06 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d  ->flags &= ~(MEM
13e07 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69  _Ephem|MEM_Stati
13e08 63 29 3b 0a 20 20 70 4d 65 6d 2d 3e 78 44 65 6c  c);.  pMem->xDel
13e09 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 28   = 0;.  return (
13e0a 70 4d 65 6d 2d 3e 7a 20 3f 20 53 51 4c 49 54 45  pMem->z ? SQLITE
13e0b 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d  _OK : SQLITE_NOM
13e0c 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61  EM);.}../*.** Ma
13e0d 6b 65 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d  ke the given Mem
13e0e 20 6f 62 6a 65 63 74 20 4d 45 4d 5f 44 79 6e 2e   object MEM_Dyn.
13e0f 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  .**.** Return SQ
13e10 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
13e11 73 73 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d  ss or SQLITE_NOM
13e12 45 4d 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69  EM if malloc fai
13e13 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ls..*/.SQLITE_PR
13e14 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
13e15 33 56 64 62 65 4d 65 6d 44 79 6e 61 6d 69 63 69  3VdbeMemDynamici
13e16 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20  fy(Mem *pMem){. 
13e17 20 69 6e 74 20 66 3b 0a 20 20 61 73 73 65 72 74   int f;.  assert
13e18 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
13e19 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
13e1a 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
13e1b 74 65 78 29 20 29 3b 0a 20 20 65 78 70 61 6e 64  tex) );.  expand
13e1c 42 6c 6f 62 28 70 4d 65 6d 29 3b 0a 20 20 66 20  Blob(pMem);.  f 
13e1d 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20  = pMem->flags;. 
13e1e 20 69 66 28 20 28 66 26 28 4d 45 4d 5f 53 74 72   if( (f&(MEM_Str
13e1f 7c 4d 45 4d 5f 42 6c 6f 62 29 29 20 26 26 20 70  |MEM_Blob)) && p
13e20 4d 65 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a 4d  Mem->z!=pMem->zM
13e21 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 69 66 28  alloc ){.    if(
13e22 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47   sqlite3VdbeMemG
13e23 72 6f 77 28 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e  row(pMem, pMem->
13e24 6e 20 2b 20 32 2c 20 31 29 20 29 7b 0a 20 20 20  n + 2, 1) ){.   
13e25 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
13e26 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
13e27 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e    pMem->z[pMem->
13e28 6e 5d 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d  n] = 0;.    pMem
13e29 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d  ->z[pMem->n+1] =
13e2a 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c   0;.    pMem->fl
13e2b 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b  ags |= MEM_Term;
13e2c 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53  .  }..  return S
13e2d 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
13e2e 2a 2a 20 49 66 20 74 68 65 20 67 69 76 65 6e 20  ** If the given 
13e2f 4d 65 6d 2a 20 68 61 73 20 61 20 7a 65 72 6f 2d  Mem* has a zero-
13e30 66 69 6c 6c 65 64 20 74 61 69 6c 2c 20 74 75 72  filled tail, tur
13e31 6e 20 69 74 20 69 6e 74 6f 20 61 6e 20 6f 72 64  n it into an ord
13e32 69 6e 61 72 79 0a 2a 2a 20 62 6c 6f 62 20 73 74  inary.** blob st
13e33 6f 72 65 64 20 69 6e 20 64 79 6e 61 6d 69 63 61  ored in dynamica
13e34 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 70  lly allocated sp
13e35 61 63 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ace..*/.#ifndef 
13e36 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52  SQLITE_OMIT_INCR
13e37 42 4c 4f 42 0a 53 51 4c 49 54 45 5f 50 52 49 56  BLOB.SQLITE_PRIV
13e38 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
13e39 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62  dbeMemExpandBlob
13e3a 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69  (Mem *pMem){.  i
13e3b 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  f( pMem->flags &
13e3c 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20   MEM_Zero ){.   
13e3d 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 20 20   int nByte;.    
13e3e 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c  assert( pMem->fl
13e3f 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a  ags&MEM_Blob );.
13e40 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
13e41 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ->db==0 || sqlit
13e42 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d  e3_mutex_held(pM
13e43 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  em->db->mutex) )
13e44 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 6e 42  ;..    /* Set nB
13e45 79 74 65 20 74 6f 20 74 68 65 20 6e 75 6d 62 65  yte to the numbe
13e46 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 69  r of bytes requi
13e47 72 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65  red to store the
13e48 20 65 78 70 61 6e 64 65 64 20 62 6c 6f 62 2e 20   expanded blob. 
13e49 2a 2f 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 70  */.    nByte = p
13e4a 4d 65 6d 2d 3e 6e 20 2b 20 70 4d 65 6d 2d 3e 75  Mem->n + pMem->u
13e4b 2e 69 3b 0a 20 20 20 20 69 66 28 20 6e 42 79 74  .i;.    if( nByt
13e4c 65 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e 42  e<=0 ){.      nB
13e4d 79 74 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  yte = 1;.    }. 
13e4e 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64     if( sqlite3Vd
13e4f 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20  beMemGrow(pMem, 
13e50 6e 42 79 74 65 2c 20 31 29 20 29 7b 0a 20 20 20  nByte, 1) ){.   
13e51 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
13e52 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 0a 20  _NOMEM;.    }.. 
13e53 20 20 20 6d 65 6d 73 65 74 28 26 70 4d 65 6d 2d     memset(&pMem-
13e54 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c 20  >z[pMem->n], 0, 
13e55 70 4d 65 6d 2d 3e 75 2e 69 29 3b 0a 20 20 20 20  pMem->u.i);.    
13e56 70 4d 65 6d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d 2d  pMem->n += pMem-
13e57 3e 75 2e 69 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e  >u.i;.    pMem->
13e58 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 5a  flags &= ~(MEM_Z
13e59 65 72 6f 7c 4d 45 4d 5f 54 65 72 6d 29 3b 0a 20  ero|MEM_Term);. 
13e5a 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
13e5b 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a  TE_OK;.}.#endif.
13e5c 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 74 68 65  ../*.** Make the
13e5d 20 67 69 76 65 6e 20 4d 65 6d 20 6f 62 6a 65 63   given Mem objec
13e5e 74 20 65 69 74 68 65 72 20 4d 45 4d 5f 53 68 6f  t either MEM_Sho
13e5f 72 74 20 6f 72 20 4d 45 4d 5f 44 79 6e 20 73 6f  rt or MEM_Dyn so
13e60 20 74 68 61 74 20 62 79 74 65 73 0a 2a 2a 20 6f   that bytes.** o
13e61 66 20 74 68 65 20 4d 65 6d 2e 7a 5b 5d 20 61 72  f the Mem.z[] ar
13e62 72 61 79 20 63 61 6e 20 62 65 20 6d 6f 64 69 66  ray can be modif
13e63 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ied..**.** Retur
13e64 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  n SQLITE_OK on s
13e65 75 63 63 65 73 73 20 6f 72 20 53 51 4c 49 54 45  uccess or SQLITE
13e66 5f 4e 4f 4d 45 4d 20 69 66 20 6d 61 6c 6c 6f 63  _NOMEM if malloc
13e67 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54   fails..*/.SQLIT
13e68 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
13e69 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65  lite3VdbeMemMake
13e6a 57 72 69 74 65 61 62 6c 65 28 4d 65 6d 20 2a 70  Writeable(Mem *p
13e6b 4d 65 6d 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  Mem){.  return s
13e6c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 44 79 6e  qlite3VdbeMemDyn
13e6d 61 6d 69 63 69 66 79 28 70 4d 65 6d 29 3b 0a 7d  amicify(pMem);.}
13e6e 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72  ../*.** Make sur
13e6f 65 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 20  e the given Mem 
13e70 69 73 20 5c 75 30 30 30 30 20 74 65 72 6d 69 6e  is \u0000 termin
13e71 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ated..*/.SQLITE_
13e72 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
13e73 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72  te3VdbeMemNulTer
13e74 6d 69 6e 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d  minate(Mem *pMem
13e75 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65  ){.  assert( pMe
13e76 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  m->db==0 || sqli
13e77 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
13e78 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  Mem->db->mutex) 
13e79 29 3b 0a 20 20 69 66 28 20 28 70 4d 65 6d 2d 3e  );.  if( (pMem->
13e7a 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d  flags & MEM_Term
13e7b 29 21 3d 30 20 7c 7c 20 28 70 4d 65 6d 2d 3e 66  )!=0 || (pMem->f
13e7c 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 3d  lags & MEM_Str)=
13e7d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
13e7e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f 2a   SQLITE_OK;   /*
13e7f 20 4e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20 2a   Nothing to do *
13e80 2f 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69  /.  }.  if( sqli
13e81 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70  te3VdbeMemGrow(p
13e82 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 2b 32 2c 20  Mem, pMem->n+2, 
13e83 31 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  1) ){.    return
13e84 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
13e85 20 7d 0a 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65   }.  pMem->z[pMe
13e86 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 70 4d 65  m->n] = 0;.  pMe
13e87 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20  m->z[pMem->n+1] 
13e88 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61  = 0;.  pMem->fla
13e89 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a  gs |= MEM_Term;.
13e8a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
13e8b 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  OK;.}../*.** Add
13e8c 20 4d 45 4d 5f 53 74 72 20 74 6f 20 74 68 65 20   MEM_Str to the 
13e8d 73 65 74 20 6f 66 20 72 65 70 72 65 73 65 6e 74  set of represent
13e8e 61 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 67  ations for the g
13e8f 69 76 65 6e 20 4d 65 6d 2e 20 20 4e 75 6d 62 65  iven Mem.  Numbe
13e90 72 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65 72  rs.** are conver
13e91 74 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65  ted using sqlite
13e92 33 5f 73 6e 70 72 69 6e 74 66 28 29 2e 20 20 43  3_snprintf().  C
13e93 6f 6e 76 65 72 74 69 6e 67 20 61 20 42 4c 4f 42  onverting a BLOB
13e94 20 74 6f 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20   to a string.** 
13e95 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  is a no-op..**.*
13e96 2a 20 45 78 69 73 74 69 6e 67 20 72 65 70 72 65  * Existing repre
13e97 73 65 6e 74 61 74 69 6f 6e 73 20 4d 45 4d 5f 49  sentations MEM_I
13e98 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20  nt and MEM_Real 
13e99 61 72 65 20 2a 6e 6f 74 2a 20 69 6e 76 61 6c 69  are *not* invali
13e9a 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 4d  dated..**.** A M
13e9b 45 4d 5f 4e 75 6c 6c 20 76 61 6c 75 65 20 77 69  EM_Null value wi
13e9c 6c 6c 20 6e 65 76 65 72 20 62 65 20 70 61 73 73  ll never be pass
13e9d 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  ed to this funct
13e9e 69 6f 6e 2e 20 54 68 69 73 20 66 75 6e 63 74 69  ion. This functi
13e9f 6f 6e 20 69 73 0a 2a 2a 20 75 73 65 64 20 66 6f  on is.** used fo
13ea0 72 20 63 6f 6e 76 65 72 74 69 6e 67 20 76 61 6c  r converting val
13ea1 75 65 73 20 74 6f 20 74 65 78 74 20 66 6f 72 20  ues to text for 
13ea2 72 65 74 75 72 6e 69 6e 67 20 74 6f 20 74 68 65  returning to the
13ea3 20 75 73 65 72 20 28 69 2e 65 2e 20 76 69 61 0a   user (i.e. via.
13ea4 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ** sqlite3_value
13ea5 5f 74 65 78 74 28 29 29 2c 20 6f 72 20 66 6f 72  _text()), or for
13ea6 20 65 6e 73 75 72 69 6e 67 20 74 68 61 74 20 76   ensuring that v
13ea7 61 6c 75 65 73 20 74 6f 20 62 65 20 75 73 65 64  alues to be used
13ea8 20 61 73 20 62 74 72 65 65 0a 2a 2a 20 6b 65 79   as btree.** key
13ea9 73 20 61 72 65 20 73 74 72 69 6e 67 73 2e 20 49  s are strings. I
13eaa 6e 20 74 68 65 20 66 6f 72 6d 65 72 20 63 61 73  n the former cas
13eab 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  e a NULL pointer
13eac 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 68 65   is returned the
13ead 0a 2a 2a 20 75 73 65 72 20 61 6e 64 20 74 68 65  .** user and the
13eae 20 6c 61 74 65 72 20 69 73 20 61 6e 20 69 6e 74   later is an int
13eaf 65 72 6e 61 6c 20 70 72 6f 67 72 61 6d 6d 69 6e  ernal programmin
13eb0 67 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49  g error..*/.SQLI
13eb1 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
13eb2 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72  qlite3VdbeMemStr
13eb3 69 6e 67 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d  ingify(Mem *pMem
13eb4 2c 20 69 6e 74 20 65 6e 63 29 7b 0a 20 20 69 6e  , int enc){.  in
13eb5 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
13eb6 3b 0a 20 20 69 6e 74 20 66 67 20 3d 20 70 4d 65  ;.  int fg = pMe
13eb7 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 6f 6e 73  m->flags;.  cons
13eb8 74 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 33 32  t int nByte = 32
13eb9 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65  ;..  assert( pMe
13eba 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  m->db==0 || sqli
13ebb 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
13ebc 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  Mem->db->mutex) 
13ebd 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 28 66  );.  assert( !(f
13ebe 67 26 4d 45 4d 5f 5a 65 72 6f 29 20 29 3b 0a 20  g&MEM_Zero) );. 
13ebf 20 61 73 73 65 72 74 28 20 21 28 66 67 26 28 4d   assert( !(fg&(M
13ec0 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29  EM_Str|MEM_Blob)
13ec1 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 66  ) );.  assert( f
13ec2 67 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52  g&(MEM_Int|MEM_R
13ec3 65 61 6c 29 20 29 3b 0a 0a 20 20 69 66 28 20 73  eal) );..  if( s
13ec4 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f  qlite3VdbeMemGro
13ec5 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 30  w(pMem, nByte, 0
13ec6 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
13ec7 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
13ec8 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20 61 20 52 65  }..  /* For a Re
13ec9 61 6c 20 6f 72 20 49 6e 74 65 67 65 72 2c 20 75  al or Integer, u
13eca 73 65 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e  se sqlite3_mprin
13ecb 74 66 28 29 20 74 6f 20 70 72 6f 64 75 63 65 20  tf() to produce 
13ecc 74 68 65 20 55 54 46 2d 38 0a 20 20 2a 2a 20 73  the UTF-8.  ** s
13ecd 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61  tring representa
13ece 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 61 6c 75  tion of the valu
13ecf 65 2e 20 54 68 65 6e 2c 20 69 66 20 74 68 65 20  e. Then, if the 
13ed0 72 65 71 75 69 72 65 64 20 65 6e 63 6f 64 69 6e  required encodin
13ed1 67 0a 20 20 2a 2a 20 69 73 20 55 54 46 2d 31 36  g.  ** is UTF-16
13ed2 6c 65 20 6f 72 20 55 54 46 2d 31 36 62 65 20 64  le or UTF-16be d
13ed3 6f 20 61 20 74 72 61 6e 73 6c 61 74 69 6f 6e 2e  o a translation.
13ed4 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 46 49 58 20  .  ** .  ** FIX 
13ed5 4d 45 3a 20 49 74 20 77 6f 75 6c 64 20 62 65 20  ME: It would be 
13ed6 62 65 74 74 65 72 20 69 66 20 73 71 6c 69 74 65  better if sqlite
13ed7 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 63 6f 75  3_snprintf() cou
13ed8 6c 64 20 64 6f 20 55 54 46 2d 31 36 2e 0a 20 20  ld do UTF-16..  
13ed9 2a 2f 0a 20 20 69 66 28 20 66 67 20 26 20 4d 45  */.  if( fg & ME
13eda 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 73 71 6c  M_Int ){.    sql
13edb 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42  ite3_snprintf(nB
13edc 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25  yte, pMem->z, "%
13edd 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e 69 29  lld", pMem->u.i)
13ede 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
13edf 73 73 65 72 74 28 20 66 67 20 26 20 4d 45 4d 5f  ssert( fg & MEM_
13ee0 52 65 61 6c 20 29 3b 0a 20 20 20 20 73 71 6c 69  Real );.    sqli
13ee1 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 79  te3_snprintf(nBy
13ee2 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 21  te, pMem->z, "%!
13ee3 2e 31 35 67 22 2c 20 70 4d 65 6d 2d 3e 72 29 3b  .15g", pMem->r);
13ee4 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d  .  }.  pMem->n =
13ee5 20 73 74 72 6c 65 6e 28 70 4d 65 6d 2d 3e 7a 29   strlen(pMem->z)
13ee6 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20  ;.  pMem->enc = 
13ee7 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 70  SQLITE_UTF8;.  p
13ee8 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  Mem->flags |= ME
13ee9 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a  M_Str|MEM_Term;.
13eea 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
13eeb 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d  ngeEncoding(pMem
13eec 2c 20 65 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e  , enc);.  return
13eed 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65   rc;.}../*.** Me
13eee 6d 6f 72 79 20 63 65 6c 6c 20 70 4d 65 6d 20 63  mory cell pMem c
13eef 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6e 74  ontains the cont
13ef0 65 78 74 20 6f 66 20 61 6e 20 61 67 67 72 65 67  ext of an aggreg
13ef1 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a  ate function..**
13ef2 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61   This routine ca
13ef3 6c 6c 73 20 74 68 65 20 66 69 6e 61 6c 69 7a 65  lls the finalize
13ef4 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 61 74   method for that
13ef5 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 0a   function.  The.
13ef6 2a 2a 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65  ** result of the
13ef7 20 61 67 67 72 65 67 61 74 65 20 69 73 20 73 74   aggregate is st
13ef8 6f 72 65 64 20 62 61 63 6b 20 69 6e 74 6f 20 70  ored back into p
13ef9 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  Mem..**.** Retur
13efa 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69  n SQLITE_ERROR i
13efb 66 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20  f the finalizer 
13efc 72 65 70 6f 72 74 73 20 61 6e 20 65 72 72 6f 72  reports an error
13efd 2e 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20  .  SQLITE_OK.** 
13efe 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51  otherwise..*/.SQ
13eff 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
13f00 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46   sqlite3VdbeMemF
13f01 69 6e 61 6c 69 7a 65 28 4d 65 6d 20 2a 70 4d 65  inalize(Mem *pMe
13f02 6d 2c 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e  m, FuncDef *pFun
13f03 63 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  c){.  int rc = S
13f04 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
13f05 70 46 75 6e 63 20 26 26 20 70 46 75 6e 63 2d 3e  pFunc && pFunc->
13f06 78 46 69 6e 61 6c 69 7a 65 20 29 7b 0a 20 20 20  xFinalize ){.   
13f07 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
13f08 20 63 74 78 3b 0a 20 20 20 20 61 73 73 65 72 74   ctx;.    assert
13f09 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  ( (pMem->flags &
13f0a 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 7c 7c   MEM_Null)!=0 ||
13f0b 20 70 46 75 6e 63 3d 3d 70 4d 65 6d 2d 3e 75 2e   pFunc==pMem->u.
13f0c 70 44 65 66 20 29 3b 0a 20 20 20 20 61 73 73 65  pDef );.    asse
13f0d 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20  rt( pMem->db==0 
13f0e 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
13f0f 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e  _held(pMem->db->
13f10 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 63 74  mutex) );.    ct
13f11 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  x.s.flags = MEM_
13f12 4e 75 6c 6c 3b 0a 20 20 20 20 63 74 78 2e 73 2e  Null;.    ctx.s.
13f13 64 62 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b 0a 20  db = pMem->db;. 
13f14 20 20 20 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f 63     ctx.s.zMalloc
13f15 20 3d 20 30 3b 0a 20 20 20 20 63 74 78 2e 70 4d   = 0;.    ctx.pM
13f16 65 6d 20 3d 20 70 4d 65 6d 3b 0a 20 20 20 20 63  em = pMem;.    c
13f17 74 78 2e 70 46 75 6e 63 20 3d 20 70 46 75 6e 63  tx.pFunc = pFunc
13f18 3b 0a 20 20 20 20 63 74 78 2e 69 73 45 72 72 6f  ;.    ctx.isErro
13f19 72 20 3d 20 30 3b 0a 20 20 20 20 70 46 75 6e 63  r = 0;.    pFunc
13f1a 2d 3e 78 46 69 6e 61 6c 69 7a 65 28 26 63 74 78  ->xFinalize(&ctx
13f1b 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 30  );.    assert( 0
13f1c 3d 3d 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d  ==(pMem->flags&M
13f1d 45 4d 5f 44 79 6e 29 20 26 26 20 21 70 4d 65 6d  EM_Dyn) && !pMem
13f1e 2d 3e 78 44 65 6c 20 29 3b 0a 20 20 20 20 73 71  ->xDel );.    sq
13f1f 6c 69 74 65 33 5f 66 72 65 65 28 70 4d 65 6d 2d  lite3_free(pMem-
13f20 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 2a  >zMalloc);.    *
13f21 70 4d 65 6d 20 3d 20 63 74 78 2e 73 3b 0a 20 20  pMem = ctx.s;.  
13f22 20 20 72 63 20 3d 20 28 63 74 78 2e 69 73 45 72    rc = (ctx.isEr
13f23 72 6f 72 3f 53 51 4c 49 54 45 5f 45 52 52 4f 52  ror?SQLITE_ERROR
13f24 3a 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 20 20 7d  :SQLITE_OK);.  }
13f25 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
13f26 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d 65  ./*.** If the me
13f27 6d 6f 72 79 20 63 65 6c 6c 20 63 6f 6e 74 61 69  mory cell contai
13f28 6e 73 20 61 20 73 74 72 69 6e 67 20 76 61 6c 75  ns a string valu
13f29 65 20 74 68 61 74 20 6d 75 73 74 20 62 65 20 66  e that must be f
13f2a 72 65 65 64 20 62 79 0a 2a 2a 20 69 6e 76 6f 6b  reed by.** invok
13f2b 69 6e 67 20 61 6e 20 65 78 74 65 72 6e 61 6c 20  ing an external 
13f2c 63 61 6c 6c 62 61 63 6b 2c 20 66 72 65 65 20 69  callback, free i
13f2d 74 20 6e 6f 77 2e 20 43 61 6c 6c 69 6e 67 20 74  t now. Calling t
13f2e 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  his function.** 
13f2f 64 6f 65 73 20 6e 6f 74 20 66 72 65 65 20 61 6e  does not free an
13f30 79 20 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 62 75  y Mem.zMalloc bu
13f31 66 66 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ffer..*/.SQLITE_
13f32 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
13f33 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
13f34 73 65 45 78 74 65 72 6e 61 6c 28 4d 65 6d 20 2a  seExternal(Mem *
13f35 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  p){.  assert( p-
13f36 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  >db==0 || sqlite
13f37 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e  3_mutex_held(p->
13f38 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
13f39 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d  if( p->flags&MEM
13f3a 5f 41 67 67 20 29 7b 0a 20 20 20 20 73 71 6c 69  _Agg ){.    sqli
13f3b 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69  te3VdbeMemFinali
13f3c 7a 65 28 70 2c 20 70 2d 3e 75 2e 70 44 65 66 29  ze(p, p->u.pDef)
13f3d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  ;.    assert( (p
13f3e 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67  ->flags & MEM_Ag
13f3f 67 29 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c  g)==0 );.    sql
13f40 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
13f41 73 65 28 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69  se(p);.  }else i
13f42 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  f( p->flags&MEM_
13f43 44 79 6e 20 26 26 20 70 2d 3e 78 44 65 6c 20 29  Dyn && p->xDel )
13f44 7b 0a 20 20 20 20 70 2d 3e 78 44 65 6c 28 28 76  {.    p->xDel((v
13f45 6f 69 64 20 2a 29 70 2d 3e 7a 29 3b 0a 20 20 20  oid *)p->z);.   
13f46 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20   p->xDel = 0;.  
13f47 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61  }.}../*.** Relea
13f48 73 65 20 61 6e 79 20 6d 65 6d 6f 72 79 20 68 65  se any memory he
13f49 6c 64 20 62 79 20 74 68 65 20 4d 65 6d 2e 20 54  ld by the Mem. T
13f4a 68 69 73 20 6d 61 79 20 6c 65 61 76 65 20 74 68  his may leave th
13f4b 65 20 4d 65 6d 20 69 6e 20 61 6e 0a 2a 2a 20 69  e Mem in an.** i
13f4c 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74  nconsistent stat
13f4d 65 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 20 77  e, for example w
13f4e 69 74 68 20 28 4d 65 6d 2e 7a 3d 3d 30 29 20 61  ith (Mem.z==0) a
13f4f 6e 64 0a 2a 2a 20 28 4d 65 6d 2e 74 79 70 65 3d  nd.** (Mem.type=
13f50 3d 53 51 4c 49 54 45 5f 54 45 58 54 29 2e 0a 2a  =SQLITE_TEXT)..*
13f51 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
13f52 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
13f53 65 4d 65 6d 52 65 6c 65 61 73 65 28 4d 65 6d 20  eMemRelease(Mem 
13f54 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64  *p){.  sqlite3Vd
13f55 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65  beMemReleaseExte
13f56 72 6e 61 6c 28 70 29 3b 0a 20 20 73 71 6c 69 74  rnal(p);.  sqlit
13f57 65 33 5f 66 72 65 65 28 70 2d 3e 7a 4d 61 6c 6c  e3_free(p->zMall
13f58 6f 63 29 3b 0a 20 20 70 2d 3e 7a 20 3d 20 30 3b  oc);.  p->z = 0;
13f59 0a 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20  .  p->zMalloc = 
13f5a 30 3b 0a 20 20 70 2d 3e 78 44 65 6c 20 3d 20 30  0;.  p->xDel = 0
13f5b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  ;.}../*.** Conve
13f5c 72 74 20 61 20 36 34 2d 62 69 74 20 49 45 45 45  rt a 64-bit IEEE
13f5d 20 64 6f 75 62 6c 65 20 69 6e 74 6f 20 61 20 36   double into a 6
13f5e 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74  4-bit signed int
13f5f 65 67 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 20  eger..** If the 
13f60 64 6f 75 62 6c 65 20 69 73 20 74 6f 6f 20 6c 61  double is too la
13f61 72 67 65 2c 20 72 65 74 75 72 6e 20 30 78 38 30  rge, return 0x80
13f62 30 30 30 30 30 30 30 30 30 30 30 30 30 30 2e 0a  00000000000000..
13f63 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 73 79 73 74 65  **.** Most syste
13f64 6d 73 20 61 70 70 65 61 72 20 74 6f 20 64 6f 20  ms appear to do 
13f65 74 68 69 73 20 73 69 6d 70 6c 79 20 62 79 20 61  this simply by a
13f66 73 73 69 67 6e 69 6e 67 0a 2a 2a 20 76 61 72 69  ssigning.** vari
13f67 61 62 6c 65 73 20 61 6e 64 20 77 69 74 68 6f 75  ables and withou
13f68 74 20 74 68 65 20 65 78 74 72 61 20 72 61 6e 67  t the extra rang
13f69 65 20 74 65 73 74 73 2e 20 20 42 75 74 0a 2a 2a  e tests.  But.**
13f6a 20 74 68 65 72 65 20 61 72 65 20 72 65 70 6f 72   there are repor
13f6b 74 73 20 74 68 61 74 20 77 69 6e 64 6f 77 73 20  ts that windows 
13f6c 74 68 72 6f 77 73 20 61 6e 20 65 78 70 65 63 74  throws an expect
13f6d 69 6f 6e 0a 2a 2a 20 69 66 20 74 68 65 20 66 6c  ion.** if the fl
13f6e 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c  oating point val
13f6f 75 65 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e  ue is out of ran
13f70 67 65 2e 20 28 53 65 65 20 74 69 63 6b 65 74 20  ge. (See ticket 
13f71 23 32 38 38 30 2e 29 0a 2a 2a 20 42 65 63 61 75  #2880.).** Becau
13f72 73 65 20 77 65 20 64 6f 20 6e 6f 74 20 63 6f 6d  se we do not com
13f73 70 6c 65 74 65 6c 79 20 75 6e 64 65 72 73 74 61  pletely understa
13f74 6e 64 20 74 68 65 20 70 72 6f 62 6c 65 6d 2c 20  nd the problem, 
13f75 77 65 20 77 69 6c 6c 0a 2a 2a 20 74 61 6b 65 20  we will.** take 
13f76 74 68 65 20 63 6f 6e 73 65 72 76 61 74 69 76 65  the conservative
13f77 20 61 70 70 72 6f 61 63 68 20 61 6e 64 20 61 6c   approach and al
13f78 77 61 79 73 20 64 6f 20 72 61 6e 67 65 20 74 65  ways do range te
13f79 73 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 61 74  sts.** before at
13f7a 74 65 6d 70 74 69 6e 67 20 74 68 65 20 63 6f 6e  tempting the con
13f7b 76 65 72 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  version..*/.stat
13f7c 69 63 20 69 36 34 20 64 6f 75 62 6c 65 54 6f 49  ic i64 doubleToI
13f7d 6e 74 36 34 28 64 6f 75 62 6c 65 20 72 29 7b 0a  nt64(double r){.
13f7e 20 20 2f 2a 0a 20 20 2a 2a 20 4d 61 6e 79 20 63    /*.  ** Many c
13f7f 6f 6d 70 69 6c 65 72 73 20 77 65 20 65 6e 63 6f  ompilers we enco
13f80 75 6e 74 65 72 20 64 6f 20 6e 6f 74 20 64 65 66  unter do not def
13f81 69 6e 65 20 63 6f 6e 73 74 61 6e 74 73 20 66 6f  ine constants fo
13f82 72 20 74 68 65 0a 20 20 2a 2a 20 6d 69 6e 69 6d  r the.  ** minim
13f83 75 6d 20 61 6e 64 20 6d 61 78 69 6d 75 6d 20 36  um and maximum 6
13f84 34 2d 62 69 74 20 69 6e 74 65 67 65 72 73 2c 20  4-bit integers, 
13f85 6f 72 20 74 68 65 79 20 64 65 66 69 6e 65 20 74  or they define t
13f86 68 65 6d 0a 20 20 2a 2a 20 69 6e 63 6f 6e 73 69  hem.  ** inconsi
13f87 73 74 65 6e 74 6c 79 2e 20 20 41 6e 64 20 6d 61  stently.  And ma
13f88 6e 79 20 64 6f 20 6e 6f 74 20 75 6e 64 65 72 73  ny do not unders
13f89 74 61 6e 64 20 74 68 65 20 22 4c 4c 22 20 6e 6f  tand the "LL" no
13f8a 74 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53 6f 20  tation..  ** So 
13f8b 77 65 20 64 65 66 69 6e 65 20 6f 75 72 20 6f 77  we define our ow
13f8c 6e 20 73 74 61 74 69 63 20 63 6f 6e 73 74 61 6e  n static constan
13f8d 74 73 20 68 65 72 65 20 75 73 69 6e 67 20 6e 6f  ts here using no
13f8e 74 68 69 6e 67 0a 20 20 2a 2a 20 6c 61 72 67 65  thing.  ** large
13f8f 72 20 74 68 61 6e 20 61 20 33 32 2d 62 69 74 20  r than a 32-bit 
13f90 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74  integer constant
13f91 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20  ..  */.  static 
13f92 63 6f 6e 73 74 20 69 36 34 20 6d 61 78 49 6e 74  const i64 maxInt
13f93 20 3d 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34   = LARGEST_INT64
13f94 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.  static const
13f95 20 69 36 34 20 6d 69 6e 49 6e 74 20 3d 20 53 4d   i64 minInt = SM
13f96 41 4c 4c 45 53 54 5f 49 4e 54 36 34 3b 0a 0a 20  ALLEST_INT64;.. 
13f97 20 69 66 28 20 72 3c 28 64 6f 75 62 6c 65 29 6d   if( r<(double)m
13f98 69 6e 49 6e 74 20 29 7b 0a 20 20 20 20 72 65 74  inInt ){.    ret
13f99 75 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d 65  urn minInt;.  }e
13f9a 6c 73 65 20 69 66 28 20 72 3e 28 64 6f 75 62 6c  lse if( r>(doubl
13f9b 65 29 6d 61 78 49 6e 74 20 29 7b 0a 20 20 20 20  e)maxInt ){.    
13f9c 72 65 74 75 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20  return minInt;. 
13f9d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
13f9e 72 6e 20 28 69 36 34 29 72 3b 0a 20 20 7d 0a 7d  rn (i64)r;.  }.}
13f9f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 73  ../*.** Return s
13fa0 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 69 6e 74 65  ome kind of inte
13fa1 67 65 72 20 76 61 6c 75 65 20 77 68 69 63 68 20  ger value which 
13fa2 69 73 20 74 68 65 20 62 65 73 74 20 77 65 20 63  is the best we c
13fa3 61 6e 20 64 6f 0a 2a 2a 20 61 74 20 72 65 70 72  an do.** at repr
13fa4 65 73 65 6e 74 69 6e 67 20 74 68 65 20 76 61 6c  esenting the val
13fa5 75 65 20 74 68 61 74 20 2a 70 4d 65 6d 20 64 65  ue that *pMem de
13fa6 73 63 72 69 62 65 73 20 61 73 20 61 6e 20 69 6e  scribes as an in
13fa7 74 65 67 65 72 2e 0a 2a 2a 20 49 66 20 70 4d 65  teger..** If pMe
13fa8 6d 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 2c  m is an integer,
13fa9 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20   then the value 
13faa 69 73 20 65 78 61 63 74 2e 20 20 49 66 20 70 4d  is exact.  If pM
13fab 65 6d 20 69 73 0a 2a 2a 20 61 20 66 6c 6f 61 74  em is.** a float
13fac 69 6e 67 2d 70 6f 69 6e 74 20 74 68 65 6e 20 74  ing-point then t
13fad 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
13fae 64 20 69 73 20 74 68 65 20 69 6e 74 65 67 65 72  d is the integer
13faf 20 70 61 72 74 2e 0a 2a 2a 20 49 66 20 70 4d 65   part..** If pMe
13fb0 6d 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 72  m is a string or
13fb1 20 62 6c 6f 62 2c 20 74 68 65 6e 20 77 65 20 6d   blob, then we m
13fb2 61 6b 65 20 61 6e 20 61 74 74 65 6d 70 74 20 74  ake an attempt t
13fb3 6f 20 63 6f 6e 76 65 72 74 0a 2a 2a 20 69 74 20  o convert.** it 
13fb4 69 6e 74 6f 20 61 20 69 6e 74 65 67 65 72 20 61  into a integer a
13fb5 6e 64 20 72 65 74 75 72 6e 20 74 68 61 74 2e 20  nd return that. 
13fb6 20 49 66 20 70 4d 65 6d 20 69 73 20 4e 55 4c 4c   If pMem is NULL
13fb7 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a  , return 0..**.*
13fb8 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 61 20 73  * If pMem is a s
13fb9 74 72 69 6e 67 2c 20 69 74 73 20 65 6e 63 6f 64  tring, its encod
13fba 69 6e 67 20 6d 69 67 68 74 20 62 65 20 63 68 61  ing might be cha
13fbb 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  nged..*/.SQLITE_
13fbc 50 52 49 56 41 54 45 20 69 36 34 20 73 71 6c 69  PRIVATE i64 sqli
13fbd 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28  te3VdbeIntValue(
13fbe 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e  Mem *pMem){.  in
13fbf 74 20 66 6c 61 67 73 3b 0a 20 20 61 73 73 65 72  t flags;.  asser
13fc0 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c  t( pMem->db==0 |
13fc1 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
13fc2 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d  held(pMem->db->m
13fc3 75 74 65 78 29 20 29 3b 0a 20 20 66 6c 61 67 73  utex) );.  flags
13fc4 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a   = pMem->flags;.
13fc5 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45    if( flags & ME
13fc6 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 72 65 74  M_Int ){.    ret
13fc7 75 72 6e 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20  urn pMem->u.i;. 
13fc8 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73   }else if( flags
13fc9 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20   & MEM_Real ){. 
13fca 20 20 20 72 65 74 75 72 6e 20 64 6f 75 62 6c 65     return double
13fcb 54 6f 49 6e 74 36 34 28 70 4d 65 6d 2d 3e 72 29  ToInt64(pMem->r)
13fcc 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c  ;.  }else if( fl
13fcd 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d  ags & (MEM_Str|M
13fce 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20  EM_Blob) ){.    
13fcf 69 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 20 70  i64 value;.    p
13fd0 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  Mem->flags |= ME
13fd1 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66 28 20 73  M_Str;.    if( s
13fd2 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
13fd3 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 53  Encoding(pMem, S
13fd4 51 4c 49 54 45 5f 55 54 46 38 29 0a 20 20 20 20  QLITE_UTF8).    
13fd5 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 56 64 62     || sqlite3Vdb
13fd6 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65  eMemNulTerminate
13fd7 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 20 20  (pMem) ){.      
13fd8 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a  return 0;.    }.
13fd9 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
13fda 2d 3e 7a 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ->z );.    sqlit
13fdb 65 33 41 74 6f 69 36 34 28 70 4d 65 6d 2d 3e 7a  e3Atoi64(pMem->z
13fdc 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 72  , &value);.    r
13fdd 65 74 75 72 6e 20 76 61 6c 75 65 3b 0a 20 20 7d  eturn value;.  }
13fde 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
13fdf 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   0;.  }.}../*.**
13fe0 20 52 65 74 75 72 6e 20 74 68 65 20 62 65 73 74   Return the best
13fe1 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
13fe2 6f 66 20 70 4d 65 6d 20 74 68 61 74 20 77 65 20  of pMem that we 
13fe3 63 61 6e 20 67 65 74 20 69 6e 74 6f 20 61 0a 2a  can get into a.*
13fe4 2a 20 64 6f 75 62 6c 65 2e 20 20 49 66 20 70 4d  * double.  If pM
13fe5 65 6d 20 69 73 20 61 6c 72 65 61 64 79 20 61 20  em is already a 
13fe6 64 6f 75 62 6c 65 20 6f 72 20 61 6e 20 69 6e 74  double or an int
13fe7 65 67 65 72 2c 20 72 65 74 75 72 6e 20 69 74 73  eger, return its
13fe8 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 49 66 20 69  .** value.  If i
13fe9 74 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 72  t is a string or
13fea 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f   blob, try to co
13feb 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 64 6f  nvert it to a do
13fec 75 62 6c 65 2e 0a 2a 2a 20 49 66 20 69 74 20 69  uble..** If it i
13fed 73 20 61 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e  s a NULL, return
13fee 20 30 2e 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   0.0..*/.SQLITE_
13fef 50 52 49 56 41 54 45 20 64 6f 75 62 6c 65 20 73  PRIVATE double s
13ff0 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61  qlite3VdbeRealVa
13ff1 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a  lue(Mem *pMem){.
13ff2 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
13ff3 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
13ff4 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d  _mutex_held(pMem
13ff5 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
13ff6 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67    if( pMem->flag
13ff7 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a  s & MEM_Real ){.
13ff8 20 20 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d      return pMem-
13ff9 3e 72 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  >r;.  }else if( 
13ffa 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
13ffb 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 72 65 74  M_Int ){.    ret
13ffc 75 72 6e 20 28 64 6f 75 62 6c 65 29 70 4d 65 6d  urn (double)pMem
13ffd 2d 3e 75 2e 69 3b 0a 20 20 7d 65 6c 73 65 20 69  ->u.i;.  }else i
13ffe 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  f( pMem->flags &
13fff 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c   (MEM_Str|MEM_Bl
14000 6f 62 29 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c  ob) ){.    doubl
14001 65 20 76 61 6c 20 3d 20 30 2e 30 3b 0a 20 20 20  e val = 0.0;.   
14002 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20   pMem->flags |= 
14003 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66 28  MEM_Str;.    if(
14004 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
14005 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c  geEncoding(pMem,
14006 20 53 51 4c 49 54 45 5f 55 54 46 38 29 0a 20 20   SQLITE_UTF8).  
14007 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 56       || sqlite3V
14008 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61  dbeMemNulTermina
14009 74 65 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20  te(pMem) ){.    
1400a 20 20 72 65 74 75 72 6e 20 30 2e 30 3b 0a 20 20    return 0.0;.  
1400b 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
1400c 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20 73  pMem->z );.    s
1400d 71 6c 69 74 65 33 41 74 6f 46 28 70 4d 65 6d 2d  qlite3AtoF(pMem-
1400e 3e 7a 2c 20 26 76 61 6c 29 3b 0a 20 20 20 20 72  >z, &val);.    r
1400f 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 65 6c  eturn val;.  }el
14010 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  se{.    return 0
14011 2e 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  .0;.  }.}../*.**
14012 20 54 68 65 20 4d 45 4d 20 73 74 72 75 63 74 75   The MEM structu
14013 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20  re is already a 
14014 4d 45 4d 5f 52 65 61 6c 2e 20 20 54 72 79 20 74  MEM_Real.  Try t
14015 6f 20 61 6c 73 6f 20 6d 61 6b 65 20 69 74 20 61  o also make it a
14016 0a 2a 2a 20 4d 45 4d 5f 49 6e 74 20 69 66 20 77  .** MEM_Int if w
14017 65 20 63 61 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  e can..*/.SQLITE
14018 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
14019 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 72  lite3VdbeInteger
1401a 41 66 66 69 6e 69 74 79 28 4d 65 6d 20 2a 70 4d  Affinity(Mem *pM
1401b 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  em){.  assert( p
1401c 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
1401d 5f 52 65 61 6c 20 29 3b 0a 20 20 61 73 73 65 72  _Real );.  asser
1401e 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c  t( pMem->db==0 |
1401f 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
14020 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d  held(pMem->db->m
14021 75 74 65 78 29 20 29 3b 0a 0a 20 20 70 4d 65 6d  utex) );..  pMem
14022 2d 3e 75 2e 69 20 3d 20 64 6f 75 62 6c 65 54 6f  ->u.i = doubleTo
14023 49 6e 74 36 34 28 70 4d 65 6d 2d 3e 72 29 3b 0a  Int64(pMem->r);.
14024 20 20 69 66 28 20 70 4d 65 6d 2d 3e 72 3d 3d 28    if( pMem->r==(
14025 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69  double)pMem->u.i
14026 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c   ){.    pMem->fl
14027 61 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  ags |= MEM_Int;.
14028 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f    }.}..static vo
14029 69 64 20 73 65 74 54 79 70 65 46 6c 61 67 28 4d  id setTypeFlag(M
1402a 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 66 29  em *pMem, int f)
1402b 7b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c  {.  MemSetTypeFl
1402c 61 67 28 70 4d 65 6d 2c 20 66 29 3b 0a 7d 0a 0a  ag(pMem, f);.}..
1402d 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 70 4d  /*.** Convert pM
1402e 65 6d 20 74 6f 20 74 79 70 65 20 69 6e 74 65 67  em to type integ
1402f 65 72 2e 20 20 49 6e 76 61 6c 69 64 61 74 65 20  er.  Invalidate 
14030 61 6e 79 20 70 72 69 6f 72 20 72 65 70 72 65 73  any prior repres
14031 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51  entations..*/.SQ
14032 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
14033 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49   sqlite3VdbeMemI
14034 6e 74 65 67 65 72 69 66 79 28 4d 65 6d 20 2a 70  ntegerify(Mem *p
14035 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20  Mem){.  assert( 
14036 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73  pMem->db==0 || s
14037 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
14038 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65  d(pMem->db->mute
14039 78 29 20 29 3b 0a 20 20 70 4d 65 6d 2d 3e 75 2e  x) );.  pMem->u.
1403a 69 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49  i = sqlite3VdbeI
1403b 6e 74 56 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20  ntValue(pMem);. 
1403c 20 73 65 74 54 79 70 65 46 6c 61 67 28 70 4d 65   setTypeFlag(pMe
1403d 6d 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 72  m, MEM_Int);.  r
1403e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1403f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  .}../*.** Conver
14040 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 69  t pMem so that i
14041 74 20 69 73 20 6f 66 20 74 79 70 65 20 4d 45 4d  t is of type MEM
14042 5f 52 65 61 6c 2e 0a 2a 2a 20 49 6e 76 61 6c 69  _Real..** Invali
14043 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 72  date any prior r
14044 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a  epresentations..
14045 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14046 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
14047 65 4d 65 6d 52 65 61 6c 69 66 79 28 4d 65 6d 20  eMemRealify(Mem 
14048 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74  *pMem){.  assert
14049 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
1404a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1404b 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
1404c 74 65 78 29 20 29 3b 0a 20 20 70 4d 65 6d 2d 3e  tex) );.  pMem->
1404d 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52  r = sqlite3VdbeR
1404e 65 61 6c 56 61 6c 75 65 28 70 4d 65 6d 29 3b 0a  ealValue(pMem);.
1404f 20 20 73 65 74 54 79 70 65 46 6c 61 67 28 70 4d    setTypeFlag(pM
14050 65 6d 2c 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20  em, MEM_Real);. 
14051 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
14052 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  K;.}../*.** Conv
14053 65 72 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74  ert pMem so that
14054 20 69 74 20 68 61 73 20 74 79 70 65 73 20 4d 45   it has types ME
14055 4d 5f 52 65 61 6c 20 6f 72 20 4d 45 4d 5f 49 6e  M_Real or MEM_In
14056 74 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20 49 6e  t or both..** In
14057 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72 69  validate any pri
14058 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  or representatio
14059 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ns..*/.SQLITE_PR
1405a 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1405b 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79  3VdbeMemNumerify
1405c 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 64  (Mem *pMem){.  d
1405d 6f 75 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20  ouble r1, r2;.  
1405e 69 36 34 20 69 3b 0a 20 20 61 73 73 65 72 74 28  i64 i;.  assert(
1405f 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20   (pMem->flags & 
14060 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61  (MEM_Int|MEM_Rea
14061 6c 7c 4d 45 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 20  l|MEM_Null))==0 
14062 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d  );.  assert( (pM
14063 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d  em->flags & (MEM
14064 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 21  _Blob|MEM_Str))!
14065 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
14066 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73  pMem->db==0 || s
14067 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
14068 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65  d(pMem->db->mute
14069 78 29 20 29 3b 0a 20 20 72 31 20 3d 20 73 71 6c  x) );.  r1 = sql
1406a 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75  ite3VdbeRealValu
1406b 65 28 70 4d 65 6d 29 3b 0a 20 20 69 20 3d 20 64  e(pMem);.  i = d
1406c 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 72 31 29  oubleToInt64(r1)
1406d 3b 0a 20 20 72 32 20 3d 20 28 64 6f 75 62 6c 65  ;.  r2 = (double
1406e 29 69 3b 0a 20 20 69 66 28 20 72 31 3d 3d 72 32  )i;.  if( r1==r2
1406f 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
14070 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79  dbeMemIntegerify
14071 28 70 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b  (pMem);.  }else{
14072 0a 20 20 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 72  .    pMem->r = r
14073 31 3b 0a 20 20 20 20 73 65 74 54 79 70 65 46 6c  1;.    setTypeFl
14074 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52 65 61  ag(pMem, MEM_Rea
14075 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  l);.  }.  return
14076 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
14077 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20  *.** Delete any 
14078 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61  previous value a
14079 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65  nd set the value
1407a 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d   stored in *pMem
1407b 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c   to NULL..*/.SQL
1407c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1407d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1407e 65 74 4e 75 6c 6c 28 4d 65 6d 20 2a 70 4d 65 6d  etNull(Mem *pMem
1407f 29 7b 0a 20 20 73 65 74 54 79 70 65 46 6c 61 67  ){.  setTypeFlag
14080 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75 6c 6c 29  (pMem, MEM_Null)
14081 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d  ;.  pMem->type =
14082 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 7d 0a   SQLITE_NULL;.}.
14083 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e  ./*.** Delete an
14084 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65  y previous value
14085 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c   and set the val
14086 75 65 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 20  ue to be a BLOB 
14087 6f 66 20 6c 65 6e 67 74 68 0a 2a 2a 20 6e 20 63  of length.** n c
14088 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 7a 65  ontaining all ze
14089 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ros..*/.SQLITE_P
1408a 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1408b 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72  te3VdbeMemSetZer
1408c 6f 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d 65 6d 2c  oBlob(Mem *pMem,
1408d 20 69 6e 74 20 6e 29 7b 0a 20 20 73 71 6c 69 74   int n){.  sqlit
1408e 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
1408f 28 70 4d 65 6d 29 3b 0a 20 20 73 65 74 54 79 70  (pMem);.  setTyp
14090 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f  eFlag(pMem, MEM_
14091 42 6c 6f 62 29 3b 0a 20 20 70 4d 65 6d 2d 3e 66  Blob);.  pMem->f
14092 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c  lags = MEM_Blob|
14093 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 70 4d 65 6d  MEM_Zero;.  pMem
14094 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
14095 42 4c 4f 42 3b 0a 20 20 70 4d 65 6d 2d 3e 6e 20  BLOB;.  pMem->n 
14096 3d 20 30 3b 0a 20 20 69 66 28 20 6e 3c 30 20 29  = 0;.  if( n<0 )
14097 20 6e 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e   n = 0;.  pMem->
14098 75 2e 69 20 3d 20 6e 3b 0a 20 20 70 4d 65 6d 2d  u.i = n;.  pMem-
14099 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54  >enc = SQLITE_UT
1409a 46 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c  F8;.}../*.** Del
1409b 65 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73  ete any previous
1409c 20 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74   value and set t
1409d 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20  he value stored 
1409e 69 6e 20 2a 70 4d 65 6d 20 74 6f 20 76 61 6c 2c  in *pMem to val,
1409f 0a 2a 2a 20 6d 61 6e 69 66 65 73 74 20 74 79 70  .** manifest typ
140a0 65 20 49 4e 54 45 47 45 52 2e 0a 2a 2f 0a 53 51  e INTEGER..*/.SQ
140a1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
140a2 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
140a3 53 65 74 49 6e 74 36 34 28 4d 65 6d 20 2a 70 4d  SetInt64(Mem *pM
140a4 65 6d 2c 20 69 36 34 20 76 61 6c 29 7b 0a 20 20  em, i64 val){.  
140a5 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
140a6 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70  lease(pMem);.  p
140a7 4d 65 6d 2d 3e 75 2e 69 20 3d 20 76 61 6c 3b 0a  Mem->u.i = val;.
140a8 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
140a9 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4d 65 6d 2d  MEM_Int;.  pMem-
140aa 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49  >type = SQLITE_I
140ab 4e 54 45 47 45 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  NTEGER;.}../*.**
140ac 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76   Delete any prev
140ad 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73  ious value and s
140ae 65 74 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f  et the value sto
140af 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f 20  red in *pMem to 
140b0 76 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 73 74  val,.** manifest
140b1 20 74 79 70 65 20 52 45 41 4c 2e 0a 2a 2f 0a 53   type REAL..*/.S
140b2 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
140b3 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  id sqlite3VdbeMe
140b4 6d 53 65 74 44 6f 75 62 6c 65 28 4d 65 6d 20 2a  mSetDouble(Mem *
140b5 70 4d 65 6d 2c 20 64 6f 75 62 6c 65 20 76 61 6c  pMem, double val
140b6 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ){.  if( sqlite3
140b7 49 73 4e 61 4e 28 76 61 6c 29 20 29 7b 0a 20 20  IsNaN(val) ){.  
140b8 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
140b9 53 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20  SetNull(pMem);. 
140ba 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
140bb 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
140bc 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65  e(pMem);.    pMe
140bd 6d 2d 3e 72 20 3d 20 76 61 6c 3b 0a 20 20 20 20  m->r = val;.    
140be 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
140bf 4d 5f 52 65 61 6c 3b 0a 20 20 20 20 70 4d 65 6d  M_Real;.    pMem
140c0 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
140c1 46 4c 4f 41 54 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  FLOAT;.  }.}../*
140c2 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
140c3 69 66 20 74 68 65 20 4d 65 6d 20 6f 62 6a 65 63  if the Mem objec
140c4 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 54 45 58  t contains a TEX
140c5 54 20 6f 72 20 42 4c 4f 42 20 74 68 61 74 20 69  T or BLOB that i
140c6 73 0a 2a 2a 20 74 6f 6f 20 6c 61 72 67 65 20 2d  s.** too large -
140c7 20 77 68 6f 73 65 20 73 69 7a 65 20 65 78 63 65   whose size exce
140c8 65 64 73 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c  eds SQLITE_MAX_L
140c9 45 4e 47 54 48 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ENGTH..*/.SQLITE
140ca 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
140cb 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69  ite3VdbeMemTooBi
140cc 67 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 61 73 73  g(Mem *p){.  ass
140cd 65 72 74 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b  ert( p->db!=0 );
140ce 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20  .  if( p->flags 
140cf 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42  & (MEM_Str|MEM_B
140d0 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69 6e 74 20  lob) ){.    int 
140d1 6e 20 3d 20 70 2d 3e 6e 3b 0a 20 20 20 20 69 66  n = p->n;.    if
140d2 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  ( p->flags & MEM
140d3 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 6e  _Zero ){.      n
140d4 20 2b 3d 20 70 2d 3e 75 2e 69 3b 0a 20 20 20 20   += p->u.i;.    
140d5 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 3e 70  }.    return n>p
140d6 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  ->db->aLimit[SQL
140d7 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
140d8 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  ];.  }.  return 
140d9 30 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a  0; .}../*.** Siz
140da 65 20 6f 66 20 73 74 72 75 63 74 20 4d 65 6d 20  e of struct Mem 
140db 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 20 74 68  not including th
140dc 65 20 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 6d 65  e Mem.zMalloc me
140dd 6d 62 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  mber..*/.#define
140de 20 4d 45 4d 43 45 4c 4c 53 49 5a 45 20 28 73 69   MEMCELLSIZE (si
140df 7a 65 5f 74 29 28 26 28 28 28 4d 65 6d 20 2a 29  ze_t)(&(((Mem *)
140e0 30 29 2d 3e 7a 4d 61 6c 6c 6f 63 29 29 0a 0a 2f  0)->zMalloc))../
140e1 2a 0a 2a 2a 20 4d 61 6b 65 20 61 6e 20 73 68 61  *.** Make an sha
140e2 6c 6c 6f 77 20 63 6f 70 79 20 6f 66 20 70 46 72  llow copy of pFr
140e3 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e 20 20 50 72  om into pTo.  Pr
140e4 69 6f 72 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a  ior contents of.
140e5 2a 2a 20 70 54 6f 20 61 72 65 20 66 72 65 65 64  ** pTo are freed
140e6 2e 20 20 54 68 65 20 70 46 72 6f 6d 2d 3e 7a 20  .  The pFrom->z 
140e7 66 69 65 6c 64 20 69 73 20 6e 6f 74 20 64 75 70  field is not dup
140e8 6c 69 63 61 74 65 64 2e 20 20 49 66 0a 2a 2a 20  licated.  If.** 
140e9 70 46 72 6f 6d 2d 3e 7a 20 69 73 20 75 73 65 64  pFrom->z is used
140ea 2c 20 74 68 65 6e 20 70 54 6f 2d 3e 7a 20 70 6f  , then pTo->z po
140eb 69 6e 74 73 20 74 6f 20 74 68 65 20 73 61 6d 65  ints to the same
140ec 20 74 68 69 6e 67 20 61 73 20 70 46 72 6f 6d 2d   thing as pFrom-
140ed 3e 7a 0a 2a 2a 20 61 6e 64 20 66 6c 61 67 73 20  >z.** and flags 
140ee 67 65 74 73 20 73 72 63 54 79 70 65 20 28 65 69  gets srcType (ei
140ef 74 68 65 72 20 4d 45 4d 5f 45 70 68 65 6d 20 6f  ther MEM_Ephem o
140f0 72 20 4d 45 4d 5f 53 74 61 74 69 63 29 2e 0a 2a  r MEM_Static)..*
140f1 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
140f2 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
140f3 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28  eMemShallowCopy(
140f4 4d 65 6d 20 2a 70 54 6f 2c 20 63 6f 6e 73 74 20  Mem *pTo, const 
140f5 4d 65 6d 20 2a 70 46 72 6f 6d 2c 20 69 6e 74 20  Mem *pFrom, int 
140f6 73 72 63 54 79 70 65 29 7b 0a 20 20 73 71 6c 69  srcType){.  sqli
140f7 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
140f8 65 45 78 74 65 72 6e 61 6c 28 70 54 6f 29 3b 0a  eExternal(pTo);.
140f9 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46    memcpy(pTo, pF
140fa 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a 45  rom, MEMCELLSIZE
140fb 29 3b 0a 20 20 70 54 6f 2d 3e 78 44 65 6c 20 3d  );.  pTo->xDel =
140fc 20 30 3b 0a 20 20 69 66 28 20 28 70 46 72 6f 6d   0;.  if( (pFrom
140fd 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29  ->flags&MEM_Dyn)
140fe 21 3d 30 20 7c 7c 20 70 46 72 6f 6d 2d 3e 7a 3d  !=0 || pFrom->z=
140ff 3d 70 46 72 6f 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20  =pFrom->zMalloc 
14100 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 66 6c 61 67  ){.    pTo->flag
14101 73 20 26 3d 20 7e 28 4d 45 4d 5f 44 79 6e 7c 4d  s &= ~(MEM_Dyn|M
14102 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70  EM_Static|MEM_Ep
14103 68 65 6d 29 3b 0a 20 20 20 20 61 73 73 65 72 74  hem);.    assert
14104 28 20 73 72 63 54 79 70 65 3d 3d 4d 45 4d 5f 45  ( srcType==MEM_E
14105 70 68 65 6d 20 7c 7c 20 73 72 63 54 79 70 65 3d  phem || srcType=
14106 3d 4d 45 4d 5f 53 74 61 74 69 63 20 29 3b 0a 20  =MEM_Static );. 
14107 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c 3d     pTo->flags |=
14108 20 73 72 63 54 79 70 65 3b 0a 20 20 7d 0a 7d 0a   srcType;.  }.}.
14109 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 66 75  ./*.** Make a fu
1410a 6c 6c 20 63 6f 70 79 20 6f 66 20 70 46 72 6f 6d  ll copy of pFrom
1410b 20 69 6e 74 6f 20 70 54 6f 2e 20 20 50 72 69 6f   into pTo.  Prio
1410c 72 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 54  r contents of pT
1410d 6f 20 61 72 65 0a 2a 2a 20 66 72 65 65 64 20 62  o are.** freed b
1410e 65 66 6f 72 65 20 74 68 65 20 63 6f 70 79 20 69  efore the copy i
1410f 73 20 6d 61 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54  s made..*/.SQLIT
14110 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
14111 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79  lite3VdbeMemCopy
14112 28 4d 65 6d 20 2a 70 54 6f 2c 20 63 6f 6e 73 74  (Mem *pTo, const
14113 20 4d 65 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20 20   Mem *pFrom){.  
14114 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
14115 4f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33 56 64  OK;..  sqlite3Vd
14116 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65  beMemReleaseExte
14117 72 6e 61 6c 28 70 54 6f 29 3b 0a 20 20 6d 65 6d  rnal(pTo);.  mem
14118 63 70 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20  cpy(pTo, pFrom, 
14119 4d 45 4d 43 45 4c 4c 53 49 5a 45 29 3b 0a 20 20  MEMCELLSIZE);.  
1411a 70 54 6f 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d  pTo->flags &= ~M
1411b 45 4d 5f 44 79 6e 3b 0a 0a 20 20 69 66 28 20 70  EM_Dyn;..  if( p
1411c 54 6f 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53  To->flags&(MEM_S
1411d 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a  tr|MEM_Blob) ){.
1411e 20 20 20 20 69 66 28 20 30 3d 3d 28 70 46 72 6f      if( 0==(pFro
1411f 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61  m->flags&MEM_Sta
14120 74 69 63 29 20 29 7b 0a 20 20 20 20 20 20 70 54  tic) ){.      pT
14121 6f 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f  o->flags |= MEM_
14122 45 70 68 65 6d 3b 0a 20 20 20 20 20 20 72 63 20  Ephem;.      rc 
14123 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  = sqlite3VdbeMem
14124 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 54  MakeWriteable(pT
14125 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  o);.    }.  }.. 
14126 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
14127 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 74 68  *.** Transfer th
14128 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 46  e contents of pF
14129 72 6f 6d 20 74 6f 20 70 54 6f 2e 20 41 6e 79 20  rom to pTo. Any 
1412a 65 78 69 73 74 69 6e 67 20 76 61 6c 75 65 20 69  existing value i
1412b 6e 20 70 54 6f 20 69 73 0a 2a 2a 20 66 72 65 65  n pTo is.** free
1412c 64 2e 20 49 66 20 70 46 72 6f 6d 20 63 6f 6e 74  d. If pFrom cont
1412d 61 69 6e 73 20 65 70 68 65 6d 65 72 61 6c 20 64  ains ephemeral d
1412e 61 74 61 2c 20 61 20 63 6f 70 79 20 69 73 20 6d  ata, a copy is m
1412f 61 64 65 2e 0a 2a 2a 0a 2a 2a 20 70 46 72 6f 6d  ade..**.** pFrom
14130 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 53 51 4c   contains an SQL
14131 20 4e 55 4c 4c 20 77 68 65 6e 20 74 68 69 73 20   NULL when this 
14132 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2e  routine returns.
14133 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
14134 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
14135 64 62 65 4d 65 6d 4d 6f 76 65 28 4d 65 6d 20 2a  dbeMemMove(Mem *
14136 70 54 6f 2c 20 4d 65 6d 20 2a 70 46 72 6f 6d 29  pTo, Mem *pFrom)
14137 7b 0a 20 20 61 73 73 65 72 74 28 20 70 46 72 6f  {.  assert( pFro
14138 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  m->db==0 || sqli
14139 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1413a 46 72 6f 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  From->db->mutex)
1413b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54   );.  assert( pT
1413c 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  o->db==0 || sqli
1413d 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1413e 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  To->db->mutex) )
1413f 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 72 6f  ;.  assert( pFro
14140 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 54 6f 2d  m->db==0 || pTo-
14141 3e 64 62 3d 3d 30 20 7c 7c 20 70 46 72 6f 6d 2d  >db==0 || pFrom-
14142 3e 64 62 3d 3d 70 54 6f 2d 3e 64 62 20 29 3b 0a  >db==pTo->db );.
14143 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
14144 6d 52 65 6c 65 61 73 65 28 70 54 6f 29 3b 0a 20  mRelease(pTo);. 
14145 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46 72   memcpy(pTo, pFr
14146 6f 6d 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29 29  om, sizeof(Mem))
14147 3b 0a 20 20 70 46 72 6f 6d 2d 3e 66 6c 61 67 73  ;.  pFrom->flags
14148 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 70   = MEM_Null;.  p
14149 46 72 6f 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a  From->xDel = 0;.
1414a 20 20 70 46 72 6f 6d 2d 3e 7a 4d 61 6c 6c 6f 63    pFrom->zMalloc
1414b 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43   = 0;.}../*.** C
1414c 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20  hange the value 
1414d 6f 66 20 61 20 4d 65 6d 20 74 6f 20 62 65 20 61  of a Mem to be a
1414e 20 73 74 72 69 6e 67 20 6f 72 20 61 20 42 4c 4f   string or a BLO
1414f 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d  B..**.** The mem
14150 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 20 73  ory management s
14151 74 72 61 74 65 67 79 20 64 65 70 65 6e 64 73 20  trategy depends 
14152 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  on the value of 
14153 74 68 65 20 78 44 65 6c 0a 2a 2a 20 70 61 72 61  the xDel.** para
14154 6d 65 74 65 72 2e 20 49 66 20 74 68 65 20 76 61  meter. If the va
14155 6c 75 65 20 70 61 73 73 65 64 20 69 73 20 53 51  lue passed is SQ
14156 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2c 20  LITE_TRANSIENT, 
14157 74 68 65 6e 20 74 68 65 20 0a 2a 2a 20 73 74 72  then the .** str
14158 69 6e 67 20 69 73 20 63 6f 70 69 65 64 20 69 6e  ing is copied in
14159 74 6f 20 61 20 28 70 6f 73 73 69 62 6c 79 20 65  to a (possibly e
1415a 78 69 73 74 69 6e 67 29 20 62 75 66 66 65 72 20  xisting) buffer 
1415b 6d 61 6e 61 67 65 64 20 62 79 20 74 68 65 20 0a  managed by the .
1415c 2a 2a 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65  ** Mem structure
1415d 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 79  . Otherwise, any
1415e 20 65 78 69 73 74 69 6e 67 20 62 75 66 66 65 72   existing buffer
1415f 20 69 73 20 66 72 65 65 64 20 61 6e 64 20 74 68   is freed and th
14160 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 63 6f 70  e.** pointer cop
14161 69 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ied..*/.SQLITE_P
14162 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
14163 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
14164 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 20 20  .  Mem *pMem,   
14165 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79         /* Memory
14166 20 63 65 6c 6c 20 74 6f 20 73 65 74 20 74 6f 20   cell to set to 
14167 73 74 72 69 6e 67 20 76 61 6c 75 65 20 2a 2f 0a  string value */.
14168 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c    const char *z,
14169 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20        /* String 
1416a 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74  pointer */.  int
1416b 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   n,             
1416c 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 73 74 72   /* Bytes in str
1416d 69 6e 67 2c 20 6f 72 20 6e 65 67 61 74 69 76 65  ing, or negative
1416e 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20   */.  u8 enc,   
1416f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63            /* Enc
14170 6f 64 69 6e 67 20 6f 66 20 7a 2e 20 20 30 20 66  oding of z.  0 f
14171 6f 72 20 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f  or BLOBs */.  vo
14172 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a  id (*xDel)(void*
14173 29 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20  ) /* Destructor 
14174 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20  function */.){. 
14175 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 6e 3b 20   int nByte = n; 
14176 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75       /* New valu
14177 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 6e 20 2a 2f  e for pMem->n */
14178 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 30  .  int flags = 0
14179 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61  ;      /* New va
1417a 6c 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 66 6c  lue for pMem->fl
1417b 61 67 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  ags */..  assert
1417c 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
1417d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1417e 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
1417f 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66  tex) );..  /* If
14180 20 7a 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69   z is a NULL poi
14181 6e 74 65 72 2c 20 73 65 74 20 70 4d 65 6d 20 74  nter, set pMem t
14182 6f 20 63 6f 6e 74 61 69 6e 20 61 6e 20 53 51 4c  o contain an SQL
14183 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 69 66 28 20   NULL. */.  if( 
14184 21 7a 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  !z ){.    sqlite
14185 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28  3VdbeMemSetNull(
14186 70 4d 65 6d 29 3b 0a 20 20 20 20 72 65 74 75 72  pMem);.    retur
14187 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
14188 0a 0a 20 20 66 6c 61 67 73 20 3d 20 28 65 6e 63  ..  flags = (enc
14189 3d 3d 30 3f 4d 45 4d 5f 42 6c 6f 62 3a 4d 45 4d  ==0?MEM_Blob:MEM
1418a 5f 53 74 72 29 3b 0a 20 20 69 66 28 20 6e 42 79  _Str);.  if( nBy
1418b 74 65 3c 30 20 29 7b 0a 20 20 20 20 61 73 73 65  te<0 ){.    asse
1418c 72 74 28 20 65 6e 63 21 3d 30 20 29 3b 0a 20 20  rt( enc!=0 );.  
1418d 20 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54    if( enc==SQLIT
1418e 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 20 20  E_UTF8 ){.      
1418f 66 6f 72 28 6e 42 79 74 65 3d 30 3b 20 7a 5b 6e  for(nByte=0; z[n
14190 42 79 74 65 5d 3b 20 6e 42 79 74 65 2b 2b 29 7b  Byte]; nByte++){
14191 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
14192 20 20 20 66 6f 72 28 6e 42 79 74 65 3d 30 3b 20     for(nByte=0; 
14193 7a 5b 6e 42 79 74 65 5d 20 7c 20 7a 5b 6e 42 79  z[nByte] | z[nBy
14194 74 65 2b 31 5d 3b 20 6e 42 79 74 65 2b 3d 32 29  te+1]; nByte+=2)
14195 7b 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6c 61  {}.    }.    fla
14196 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a  gs |= MEM_Term;.
14197 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f    }..  /* The fo
14198 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 73 65  llowing block se
14199 74 73 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65  ts the new value
1419a 73 20 6f 66 20 4d 65 6d 2e 7a 20 61 6e 64 20 4d  s of Mem.z and M
1419b 65 6d 2e 78 44 65 6c 2e 20 49 74 0a 20 20 2a 2a  em.xDel. It.  **
1419c 20 61 6c 73 6f 20 73 65 74 73 20 61 20 66 6c 61   also sets a fla
1419d 67 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61  g in local varia
1419e 62 6c 65 20 22 66 6c 61 67 73 22 20 74 6f 20 69  ble "flags" to i
1419f 6e 64 69 63 61 74 65 20 74 68 65 20 6d 65 6d 6f  ndicate the memo
141a0 72 79 0a 20 20 2a 2a 20 6d 61 6e 61 67 65 6d 65  ry.  ** manageme
141a1 6e 74 20 28 6f 6e 65 20 6f 66 20 4d 45 4d 5f 44  nt (one of MEM_D
141a2 79 6e 20 6f 72 20 4d 45 4d 5f 53 74 61 74 69 63  yn or MEM_Static
141a3 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 78 44  )..  */.  if( xD
141a4 65 6c 3d 3d 53 51 4c 49 54 45 5f 54 52 41 4e 53  el==SQLITE_TRANS
141a5 49 45 4e 54 20 29 7b 0a 20 20 20 20 69 6e 74 20  IENT ){.    int 
141a6 6e 41 6c 6c 6f 63 20 3d 20 6e 42 79 74 65 3b 0a  nAlloc = nByte;.
141a7 20 20 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45      if( flags&ME
141a8 4d 5f 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20  M_Term ){.      
141a9 6e 41 6c 6c 6f 63 20 2b 3d 20 28 65 6e 63 3d 3d  nAlloc += (enc==
141aa 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29  SQLITE_UTF8?1:2)
141ab 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
141ac 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
141ad 6f 77 28 70 4d 65 6d 2c 20 6e 41 6c 6c 6f 63 2c  ow(pMem, nAlloc,
141ae 20 30 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74   0) ){.      ret
141af 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
141b0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63  ;.    }.    memc
141b1 70 79 28 70 4d 65 6d 2d 3e 7a 2c 20 7a 2c 20 6e  py(pMem->z, z, n
141b2 41 6c 6c 6f 63 29 3b 0a 20 20 7d 65 6c 73 65 7b  Alloc);.  }else{
141b3 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
141b4 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29  MemRelease(pMem)
141b5 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20  ;.    pMem->z = 
141b6 28 63 68 61 72 20 2a 29 7a 3b 0a 20 20 20 20 70  (char *)z;.    p
141b7 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 78 44 65 6c  Mem->xDel = xDel
141b8 3b 0a 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 28  ;.    flags |= (
141b9 28 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 53 54  (xDel==SQLITE_ST
141ba 41 54 49 43 29 3f 4d 45 4d 5f 53 74 61 74 69 63  ATIC)?MEM_Static
141bb 3a 4d 45 4d 5f 44 79 6e 29 3b 0a 20 20 7d 0a 0a  :MEM_Dyn);.  }..
141bc 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74    pMem->n = nByt
141bd 65 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  e;.  pMem->flags
141be 20 3d 20 66 6c 61 67 73 3b 0a 20 20 70 4d 65 6d   = flags;.  pMem
141bf 2d 3e 65 6e 63 20 3d 20 28 65 6e 63 3d 3d 30 20  ->enc = (enc==0 
141c0 3f 20 53 51 4c 49 54 45 5f 55 54 46 38 20 3a 20  ? SQLITE_UTF8 : 
141c1 65 6e 63 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79  enc);.  pMem->ty
141c2 70 65 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53  pe = (enc==0 ? S
141c3 51 4c 49 54 45 5f 42 4c 4f 42 20 3a 20 53 51 4c  QLITE_BLOB : SQL
141c4 49 54 45 5f 54 45 58 54 29 3b 0a 0a 23 69 66 6e  ITE_TEXT);..#ifn
141c5 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
141c6 55 54 46 31 36 0a 20 20 69 66 28 20 70 4d 65 6d  UTF16.  if( pMem
141c7 2d 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54  ->enc!=SQLITE_UT
141c8 46 38 20 26 26 20 73 71 6c 69 74 65 33 56 64 62  F8 && sqlite3Vdb
141c9 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d  eMemHandleBom(pM
141ca 65 6d 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  em) ){.    retur
141cb 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
141cc 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65    }.#endif..  re
141cd 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
141ce 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65  }../*.** Compare
141cf 20 74 68 65 20 76 61 6c 75 65 73 20 63 6f 6e 74   the values cont
141d0 61 69 6e 65 64 20 62 79 20 74 68 65 20 74 77 6f  ained by the two
141d1 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 2c 20 72   memory cells, r
141d2 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 6e 65 67 61  eturning.** nega
141d3 74 69 76 65 2c 20 7a 65 72 6f 20 6f 72 20 70 6f  tive, zero or po
141d4 73 69 74 69 76 65 20 69 66 20 70 4d 65 6d 31 20  sitive if pMem1 
141d5 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71  is less than, eq
141d6 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74  ual to, or great
141d7 65 72 0a 2a 2a 20 74 68 61 6e 20 70 4d 65 6d 32  er.** than pMem2
141d8 2e 20 53 6f 72 74 69 6e 67 20 6f 72 64 65 72 20  . Sorting order 
141d9 69 73 20 4e 55 4c 4c 27 73 20 66 69 72 73 74 2c  is NULL's first,
141da 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6e 75 6d   followed by num
141db 62 65 72 73 20 28 69 6e 74 65 67 65 72 73 0a 2a  bers (integers.*
141dc 2a 20 61 6e 64 20 72 65 61 6c 73 29 20 73 6f 72  * and reals) sor
141dd 74 65 64 20 6e 75 6d 65 72 69 63 61 6c 6c 79 2c  ted numerically,
141de 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 74 65 78   followed by tex
141df 74 20 6f 72 64 65 72 65 64 20 62 79 20 74 68 65  t ordered by the
141e0 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65   collating.** se
141e1 71 75 65 6e 63 65 20 70 43 6f 6c 6c 20 61 6e 64  quence pColl and
141e2 20 66 69 6e 61 6c 6c 79 20 62 6c 6f 62 27 73 20   finally blob's 
141e3 6f 72 64 65 72 65 64 20 62 79 20 6d 65 6d 63 6d  ordered by memcm
141e4 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 77 6f 20 4e  p()..**.** Two N
141e5 55 4c 4c 20 76 61 6c 75 65 73 20 61 72 65 20 63  ULL values are c
141e6 6f 6e 73 69 64 65 72 65 64 20 65 71 75 61 6c 20  onsidered equal 
141e7 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  by this function
141e8 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
141e9 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  ATE int sqlite3M
141ea 65 6d 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20  emCompare(const 
141eb 4d 65 6d 20 2a 70 4d 65 6d 31 2c 20 63 6f 6e 73  Mem *pMem1, cons
141ec 74 20 4d 65 6d 20 2a 70 4d 65 6d 32 2c 20 63 6f  t Mem *pMem2, co
141ed 6e 73 74 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  nst CollSeq *pCo
141ee 6c 6c 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ll){.  int rc;. 
141ef 20 69 6e 74 20 66 31 2c 20 66 32 3b 0a 20 20 69   int f1, f2;.  i
141f0 6e 74 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67  nt combined_flag
141f1 73 3b 0a 0a 20 20 2f 2a 20 49 6e 74 65 72 63 68  s;..  /* Interch
141f2 61 6e 67 65 20 70 4d 65 6d 31 20 61 6e 64 20 70  ange pMem1 and p
141f3 4d 65 6d 32 20 69 66 20 74 68 65 20 63 6f 6c 6c  Mem2 if the coll
141f4 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 73  ating sequence s
141f5 70 65 63 69 66 69 65 73 0a 20 20 2a 2a 20 44 45  pecifies.  ** DE
141f6 53 43 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20  SC order..  */. 
141f7 20 66 31 20 3d 20 70 4d 65 6d 31 2d 3e 66 6c 61   f1 = pMem1->fla
141f8 67 73 3b 0a 20 20 66 32 20 3d 20 70 4d 65 6d 32  gs;.  f2 = pMem2
141f9 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 6f 6d 62 69  ->flags;.  combi
141fa 6e 65 64 5f 66 6c 61 67 73 20 3d 20 66 31 7c 66  ned_flags = f1|f
141fb 32 3b 0a 20 0a 20 20 2f 2a 20 49 66 20 6f 6e 65  2;. .  /* If one
141fc 20 76 61 6c 75 65 20 69 73 20 4e 55 4c 4c 2c 20   value is NULL, 
141fd 69 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20  it is less than 
141fe 74 68 65 20 6f 74 68 65 72 2e 20 49 66 20 62 6f  the other. If bo
141ff 74 68 20 76 61 6c 75 65 73 0a 20 20 2a 2a 20 61  th values.  ** a
14200 72 65 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20  re NULL, return 
14201 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f  0..  */.  if( co
14202 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d  mbined_flags&MEM
14203 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74  _Null ){.    ret
14204 75 72 6e 20 28 66 32 26 4d 45 4d 5f 4e 75 6c 6c  urn (f2&MEM_Null
14205 29 20 2d 20 28 66 31 26 4d 45 4d 5f 4e 75 6c 6c  ) - (f1&MEM_Null
14206 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  );.  }..  /* If 
14207 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 61 20 6e  one value is a n
14208 75 6d 62 65 72 20 61 6e 64 20 74 68 65 20 6f 74  umber and the ot
14209 68 65 72 20 69 73 20 6e 6f 74 2c 20 74 68 65 20  her is not, the 
1420a 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73 2e 0a  number is less..
1420b 20 20 2a 2a 20 49 66 20 62 6f 74 68 20 61 72 65    ** If both are
1420c 20 6e 75 6d 62 65 72 73 2c 20 63 6f 6d 70 61 72   numbers, compar
1420d 65 20 61 73 20 72 65 61 6c 73 20 69 66 20 6f 6e  e as reals if on
1420e 65 20 69 73 20 61 20 72 65 61 6c 2c 20 6f 72 20  e is a real, or 
1420f 61 73 20 69 6e 74 65 67 65 72 73 0a 20 20 2a 2a  as integers.  **
14210 20 69 66 20 62 6f 74 68 20 76 61 6c 75 65 73 20   if both values 
14211 61 72 65 20 69 6e 74 65 67 65 72 73 2e 0a 20 20  are integers..  
14212 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e 65  */.  if( combine
14213 64 5f 66 6c 61 67 73 26 28 4d 45 4d 5f 49 6e 74  d_flags&(MEM_Int
14214 7c 4d 45 4d 5f 52 65 61 6c 29 20 29 7b 0a 20 20  |MEM_Real) ){.  
14215 20 20 69 66 28 20 21 28 66 31 26 28 4d 45 4d 5f    if( !(f1&(MEM_
14216 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29  Int|MEM_Real)) )
14217 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
14218 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
14219 21 28 66 32 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45  !(f2&(MEM_Int|ME
1421a 4d 5f 52 65 61 6c 29 29 20 29 7b 0a 20 20 20 20  M_Real)) ){.    
1421b 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20    return -1;.   
1421c 20 7d 0a 20 20 20 20 69 66 28 20 28 66 31 20 26   }.    if( (f1 &
1421d 20 66 32 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d   f2 & MEM_Int)==
1421e 30 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c  0 ){.      doubl
1421f 65 20 72 31 2c 20 72 32 3b 0a 20 20 20 20 20 20  e r1, r2;.      
14220 69 66 28 20 28 66 31 26 4d 45 4d 5f 52 65 61 6c  if( (f1&MEM_Real
14221 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
14222 72 31 20 3d 20 70 4d 65 6d 31 2d 3e 75 2e 69 3b  r1 = pMem1->u.i;
14223 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
14224 20 20 20 20 20 20 72 31 20 3d 20 70 4d 65 6d 31        r1 = pMem1
14225 2d 3e 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ->r;.      }.   
14226 20 20 20 69 66 28 20 28 66 32 26 4d 45 4d 5f 52     if( (f2&MEM_R
14227 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  eal)==0 ){.     
14228 20 20 20 72 32 20 3d 20 70 4d 65 6d 32 2d 3e 75     r2 = pMem2->u
14229 2e 69 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  .i;.      }else{
1422a 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 70 4d  .        r2 = pM
1422b 65 6d 32 2d 3e 72 3b 0a 20 20 20 20 20 20 7d 0a  em2->r;.      }.
1422c 20 20 20 20 20 20 69 66 28 20 72 31 3c 72 32 20        if( r1<r2 
1422d 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20  ) return -1;.   
1422e 20 20 20 69 66 28 20 72 31 3e 72 32 20 29 20 72     if( r1>r2 ) r
1422f 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 72  eturn 1;.      r
14230 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c  eturn 0;.    }el
14231 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
14232 28 20 66 31 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a  ( f1&MEM_Int );.
14233 20 20 20 20 20 20 61 73 73 65 72 74 28 20 66 32        assert( f2
14234 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20  &MEM_Int );.    
14235 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 75 2e 69    if( pMem1->u.i
14236 20 3c 20 70 4d 65 6d 32 2d 3e 75 2e 69 20 29 20   < pMem2->u.i ) 
14237 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20  return -1;.     
14238 20 69 66 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20   if( pMem1->u.i 
14239 3e 20 70 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72  > pMem2->u.i ) r
1423a 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 72  eturn 1;.      r
1423b 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20  eturn 0;.    }. 
1423c 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20   }..  /* If one 
1423d 76 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e  value is a strin
1423e 67 20 61 6e 64 20 74 68 65 20 6f 74 68 65 72 20  g and the other 
1423f 69 73 20 61 20 62 6c 6f 62 2c 20 74 68 65 20 73  is a blob, the s
14240 74 72 69 6e 67 20 69 73 20 6c 65 73 73 2e 0a 20  tring is less.. 
14241 20 2a 2a 20 49 66 20 62 6f 74 68 20 61 72 65 20   ** If both are 
14242 73 74 72 69 6e 67 73 2c 20 63 6f 6d 70 61 72 65  strings, compare
14243 20 75 73 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61   using the colla
14244 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  ting functions..
14245 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69    */.  if( combi
14246 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d 5f 53 74  ned_flags&MEM_St
14247 72 20 29 7b 0a 20 20 20 20 69 66 28 20 28 66 31  r ){.    if( (f1
14248 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29   & MEM_Str)==0 )
14249 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
1424a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1424b 28 66 32 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d  (f2 & MEM_Str)==
1424c 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
1424d 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  n -1;.    }..   
1424e 20 61 73 73 65 72 74 28 20 70 4d 65 6d 31 2d 3e   assert( pMem1->
1424f 65 6e 63 3d 3d 70 4d 65 6d 32 2d 3e 65 6e 63 20  enc==pMem2->enc 
14250 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
14251 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54  Mem1->enc==SQLIT
14252 45 5f 55 54 46 38 20 7c 7c 20 0a 20 20 20 20 20  E_UTF8 || .     
14253 20 20 20 20 20 20 20 70 4d 65 6d 31 2d 3e 65 6e         pMem1->en
14254 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  c==SQLITE_UTF16L
14255 45 20 7c 7c 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d  E || pMem1->enc=
14256 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20  =SQLITE_UTF16BE 
14257 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63  );..    /* The c
14258 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
14259 65 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65  e must be define
1425a 64 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 2c  d at this point,
1425b 20 65 76 65 6e 20 69 66 0a 20 20 20 20 2a 2a 20   even if.    ** 
1425c 74 68 65 20 75 73 65 72 20 64 65 6c 65 74 65 73  the user deletes
1425d 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   the collation s
1425e 65 71 75 65 6e 63 65 20 61 66 74 65 72 20 74 68  equence after th
1425f 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 69  e vdbe program i
14260 73 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65  s.    ** compile
14261 64 20 28 74 68 69 73 20 77 61 73 20 6e 6f 74 20  d (this was not 
14262 61 6c 77 61 79 73 20 74 68 65 20 63 61 73 65 29  always the case)
14263 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73  ..    */.    ass
14264 65 72 74 28 20 21 70 43 6f 6c 6c 20 7c 7c 20 70  ert( !pColl || p
14265 43 6f 6c 6c 2d 3e 78 43 6d 70 20 29 3b 0a 0a 20  Coll->xCmp );.. 
14266 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a     if( pColl ){.
14267 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d        if( pMem1-
14268 3e 65 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63  >enc==pColl->enc
14269 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54   ){.        /* T
1426a 68 65 20 73 74 72 69 6e 67 73 20 61 72 65 20 61  he strings are a
1426b 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 6f  lready in the co
1426c 72 72 65 63 74 20 65 6e 63 6f 64 69 6e 67 2e 20  rrect encoding. 
1426d 20 43 61 6c 6c 20 74 68 65 0a 20 20 20 20 20 20   Call the.      
1426e 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20    ** comparison 
1426f 66 75 6e 63 74 69 6f 6e 20 64 69 72 65 63 74 6c  function directl
14270 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74  y */.        ret
14271 75 72 6e 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28  urn pColl->xCmp(
14272 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 70 4d 65  pColl->pUser,pMe
14273 6d 31 2d 3e 6e 2c 70 4d 65 6d 31 2d 3e 7a 2c 70  m1->n,pMem1->z,p
14274 4d 65 6d 32 2d 3e 6e 2c 70 4d 65 6d 32 2d 3e 7a  Mem2->n,pMem2->z
14275 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
14276 20 20 20 20 20 20 20 20 75 38 20 6f 72 69 67 45          u8 origE
14277 6e 63 20 3d 20 70 4d 65 6d 31 2d 3e 65 6e 63 3b  nc = pMem1->enc;
14278 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 76  .        const v
14279 6f 69 64 20 2a 76 31 2c 20 2a 76 32 3b 0a 20 20  oid *v1, *v2;.  
1427a 20 20 20 20 20 20 69 6e 74 20 6e 31 2c 20 6e 32        int n1, n2
1427b 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  ;.        /* Con
1427c 76 65 72 74 20 74 68 65 20 73 74 72 69 6e 67 73  vert the strings
1427d 20 69 6e 74 6f 20 74 68 65 20 65 6e 63 6f 64 69   into the encodi
1427e 6e 67 20 74 68 61 74 20 74 68 65 20 63 6f 6d 70  ng that the comp
1427f 61 72 69 73 6f 6e 0a 20 20 20 20 20 20 20 20 2a  arison.        *
14280 2a 20 66 75 6e 63 74 69 6f 6e 20 65 78 70 65 63  * function expec
14281 74 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 76 31  ts */.        v1
14282 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54   = sqlite3ValueT
14283 65 78 74 28 28 73 71 6c 69 74 65 33 5f 76 61 6c  ext((sqlite3_val
14284 75 65 2a 29 70 4d 65 6d 31 2c 20 70 43 6f 6c 6c  ue*)pMem1, pColl
14285 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 20 20  ->enc);.        
14286 6e 31 20 3d 20 76 31 3d 3d 30 20 3f 20 30 20 3a  n1 = v1==0 ? 0 :
14287 20 70 4d 65 6d 31 2d 3e 6e 3b 0a 20 20 20 20 20   pMem1->n;.     
14288 20 20 20 61 73 73 65 72 74 28 20 6e 31 3d 3d 73     assert( n1==s
14289 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65 73  qlite3ValueBytes
1428a 28 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  ((sqlite3_value*
1428b 29 70 4d 65 6d 31 2c 20 70 43 6f 6c 6c 2d 3e 65  )pMem1, pColl->e
1428c 6e 63 29 20 29 3b 0a 20 20 20 20 20 20 20 20 76  nc) );.        v
1428d 32 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65  2 = sqlite3Value
1428e 54 65 78 74 28 28 73 71 6c 69 74 65 33 5f 76 61  Text((sqlite3_va
1428f 6c 75 65 2a 29 70 4d 65 6d 32 2c 20 70 43 6f 6c  lue*)pMem2, pCol
14290 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 20  l->enc);.       
14291 20 6e 32 20 3d 20 76 32 3d 3d 30 20 3f 20 30 20   n2 = v2==0 ? 0 
14292 3a 20 70 4d 65 6d 32 2d 3e 6e 3b 0a 20 20 20 20  : pMem2->n;.    
14293 20 20 20 20 61 73 73 65 72 74 28 20 6e 32 3d 3d      assert( n2==
14294 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65  sqlite3ValueByte
14295 73 28 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  s((sqlite3_value
14296 2a 29 70 4d 65 6d 32 2c 20 70 43 6f 6c 6c 2d 3e  *)pMem2, pColl->
14297 65 6e 63 29 20 29 3b 0a 20 20 20 20 20 20 20 20  enc) );.        
14298 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d 70 61 72  /* Do the compar
14299 69 73 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ison */.        
1429a 72 63 20 3d 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70  rc = pColl->xCmp
1429b 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 20 6e  (pColl->pUser, n
1429c 31 2c 20 76 31 2c 20 6e 32 2c 20 76 32 29 3b 0a  1, v1, n2, v2);.
1429d 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65          /* Conve
1429e 72 74 20 74 68 65 20 73 74 72 69 6e 67 73 20 62  rt the strings b
1429f 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 74  ack into the dat
142a0 61 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 2a  abase encoding *
142a1 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  /.        sqlite
142a2 33 56 61 6c 75 65 54 65 78 74 28 28 73 71 6c 69  3ValueText((sqli
142a3 74 65 33 5f 76 61 6c 75 65 2a 29 70 4d 65 6d 31  te3_value*)pMem1
142a4 2c 20 6f 72 69 67 45 6e 63 29 3b 0a 20 20 20 20  , origEnc);.    
142a5 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65      sqlite3Value
142a6 54 65 78 74 28 28 73 71 6c 69 74 65 33 5f 76 61  Text((sqlite3_va
142a7 6c 75 65 2a 29 70 4d 65 6d 32 2c 20 6f 72 69 67  lue*)pMem2, orig
142a8 45 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 72 65  Enc);.        re
142a9 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
142aa 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 49 66  .    }.    /* If
142ab 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20   a NULL pointer 
142ac 77 61 73 20 70 61 73 73 65 64 20 61 73 20 74 68  was passed as th
142ad 65 20 63 6f 6c 6c 61 74 65 20 66 75 6e 63 74 69  e collate functi
142ae 6f 6e 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68  on, fall through
142af 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 62  .    ** to the b
142b0 6c 6f 62 20 63 61 73 65 20 61 6e 64 20 75 73 65  lob case and use
142b1 20 6d 65 6d 63 6d 70 28 29 2e 20 20 2a 2f 0a 20   memcmp().  */. 
142b2 20 7d 0a 20 0a 20 20 2f 2a 20 42 6f 74 68 20 76   }. .  /* Both v
142b3 61 6c 75 65 73 20 6d 75 73 74 20 62 65 20 62 6c  alues must be bl
142b4 6f 62 73 2e 20 20 43 6f 6d 70 61 72 65 20 75 73  obs.  Compare us
142b5 69 6e 67 20 6d 65 6d 63 6d 70 28 29 2e 20 20 2a  ing memcmp().  *
142b6 2f 0a 20 20 72 63 20 3d 20 6d 65 6d 63 6d 70 28  /.  rc = memcmp(
142b7 70 4d 65 6d 31 2d 3e 7a 2c 20 70 4d 65 6d 32 2d  pMem1->z, pMem2-
142b8 3e 7a 2c 20 28 70 4d 65 6d 31 2d 3e 6e 3e 70 4d  >z, (pMem1->n>pM
142b9 65 6d 32 2d 3e 6e 29 3f 70 4d 65 6d 32 2d 3e 6e  em2->n)?pMem2->n
142ba 3a 70 4d 65 6d 31 2d 3e 6e 29 3b 0a 20 20 69 66  :pMem1->n);.  if
142bb 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 72  ( rc==0 ){.    r
142bc 63 20 3d 20 70 4d 65 6d 31 2d 3e 6e 20 2d 20 70  c = pMem1->n - p
142bd 4d 65 6d 32 2d 3e 6e 3b 0a 20 20 7d 0a 20 20 72  Mem2->n;.  }.  r
142be 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
142bf 2a 2a 20 4d 6f 76 65 20 64 61 74 61 20 6f 75 74  ** Move data out
142c0 20 6f 66 20 61 20 62 74 72 65 65 20 6b 65 79 20   of a btree key 
142c1 6f 72 20 64 61 74 61 20 66 69 65 6c 64 20 61 6e  or data field an
142c2 64 20 69 6e 74 6f 20 61 20 4d 65 6d 20 73 74 72  d into a Mem str
142c3 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20 64  ucture..** The d
142c4 61 74 61 20 6f 72 20 6b 65 79 20 69 73 20 74 61  ata or key is ta
142c5 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 65 6e 74  ken from the ent
142c6 72 79 20 74 68 61 74 20 70 43 75 72 20 69 73 20  ry that pCur is 
142c7 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69  currently pointi
142c8 6e 67 0a 2a 2a 20 74 6f 2e 20 20 6f 66 66 73 65  ng.** to.  offse
142c9 74 20 61 6e 64 20 61 6d 74 20 64 65 74 65 72 6d  t and amt determ
142ca 69 6e 65 20 77 68 61 74 20 70 6f 72 74 69 6f 6e  ine what portion
142cb 20 6f 66 20 74 68 65 20 64 61 74 61 20 6f 72 20   of the data or 
142cc 6b 65 79 20 74 6f 20 72 65 74 72 69 65 76 65 2e  key to retrieve.
142cd 0a 2a 2a 20 6b 65 79 20 69 73 20 74 72 75 65 20  .** key is true 
142ce 74 6f 20 67 65 74 20 74 68 65 20 6b 65 79 20 6f  to get the key o
142cf 72 20 66 61 6c 73 65 20 74 6f 20 67 65 74 20 64  r false to get d
142d0 61 74 61 2e 20 20 54 68 65 20 72 65 73 75 6c 74  ata.  The result
142d1 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69   is written.** i
142d2 6e 74 6f 20 74 68 65 20 70 4d 65 6d 20 65 6c 65  nto the pMem ele
142d3 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ment..**.** The 
142d4 70 4d 65 6d 20 73 74 72 75 63 74 75 72 65 20 69  pMem structure i
142d5 73 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 20  s assumed to be 
142d6 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20  uninitialized.  
142d7 41 6e 79 20 70 72 69 6f 72 20 63 6f 6e 74 65 6e  Any prior conten
142d8 74 0a 2a 2a 20 69 73 20 6f 76 65 72 77 72 69 74  t.** is overwrit
142d9 74 65 6e 20 77 69 74 68 6f 75 74 20 62 65 69 6e  ten without bein
142da 67 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  g freed..**.** I
142db 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66  f this routine f
142dc 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 72 65 61  ails for any rea
142dd 73 6f 6e 20 28 6d 61 6c 6c 6f 63 20 72 65 74 75  son (malloc retu
142de 72 6e 73 20 4e 55 4c 4c 20 6f 72 20 75 6e 61 62  rns NULL or unab
142df 6c 65 0a 2a 2a 20 74 6f 20 72 65 61 64 20 66 72  le.** to read fr
142e0 6f 6d 20 74 68 65 20 64 69 73 6b 29 20 74 68 65  om the disk) the
142e1 6e 20 74 68 65 20 70 4d 65 6d 20 69 73 20 6c 65  n the pMem is le
142e2 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69  ft in an inconsi
142e3 73 74 65 6e 74 20 73 74 61 74 65 2e 0a 2a 2f 0a  stent state..*/.
142e4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
142e5 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
142e6 6d 46 72 6f 6d 42 74 72 65 65 28 0a 20 20 42 74  mFromBtree(.  Bt
142e7 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20  Cursor *pCur,   
142e8 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69  /* Cursor pointi
142e9 6e 67 20 61 74 20 72 65 63 6f 72 64 20 74 6f 20  ng at record to 
142ea 72 65 74 72 69 65 76 65 2e 20 2a 2f 0a 20 20 69  retrieve. */.  i
142eb 6e 74 20 6f 66 66 73 65 74 2c 20 20 20 20 20 20  nt offset,      
142ec 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f 6d 20   /* Offset from 
142ed 74 68 65 20 73 74 61 72 74 20 6f 66 20 64 61 74  the start of dat
142ee 61 20 74 6f 20 72 65 74 75 72 6e 20 62 79 74 65  a to return byte
142ef 73 20 66 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e 74  s from. */.  int
142f0 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 2f   amt,          /
142f1 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
142f2 73 20 74 6f 20 72 65 74 75 72 6e 2e 20 2a 2f 0a  s to return. */.
142f3 20 20 69 6e 74 20 6b 65 79 2c 20 20 20 20 20 20    int key,      
142f4 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20      /* If true, 
142f5 72 65 74 72 69 65 76 65 20 66 72 6f 6d 20 74 68  retrieve from th
142f6 65 20 62 74 72 65 65 20 6b 65 79 2c 20 6e 6f 74  e btree key, not
142f7 20 64 61 74 61 2e 20 2a 2f 0a 20 20 4d 65 6d 20   data. */.  Mem 
142f8 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 20 2f 2a  *pMem         /*
142f9 20 4f 55 54 3a 20 52 65 74 75 72 6e 20 64 61 74   OUT: Return dat
142fa 61 20 69 6e 20 74 68 69 73 20 4d 65 6d 20 73 74  a in this Mem st
142fb 72 75 63 74 75 72 65 2e 20 2a 2f 0a 29 7b 0a 20  ructure. */.){. 
142fc 20 63 68 61 72 20 2a 7a 44 61 74 61 3b 20 20 20   char *zData;   
142fd 20 20 20 20 2f 2a 20 44 61 74 61 20 66 72 6f 6d      /* Data from
142fe 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65 72   the btree layer
142ff 20 2a 2f 0a 20 20 69 6e 74 20 61 76 61 69 6c 61   */.  int availa
14300 62 6c 65 20 3d 20 30 3b 20 2f 2a 20 4e 75 6d 62  ble = 0; /* Numb
14301 65 72 20 6f 66 20 62 79 74 65 73 20 61 76 61 69  er of bytes avai
14302 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63  lable on the loc
14303 61 6c 20 62 74 72 65 65 20 70 61 67 65 20 2a 2f  al btree page */
14304 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
14305 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
14306 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
14307 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
14308 45 5f 4f 4b 3b 0a 0a 20 20 64 62 20 3d 20 73 71  E_OK;..  db = sq
14309 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
1430a 44 62 28 70 43 75 72 29 3b 0a 20 20 61 73 73 65  Db(pCur);.  asse
1430b 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1430c 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
1430d 29 20 29 3b 0a 20 20 69 66 28 20 6b 65 79 20 29  ) );.  if( key )
1430e 7b 0a 20 20 20 20 7a 44 61 74 61 20 3d 20 28 63  {.    zData = (c
1430f 68 61 72 20 2a 29 73 71 6c 69 74 65 33 42 74 72  har *)sqlite3Btr
14310 65 65 4b 65 79 46 65 74 63 68 28 70 43 75 72 2c  eeKeyFetch(pCur,
14311 20 26 61 76 61 69 6c 61 62 6c 65 29 3b 0a 20 20   &available);.  
14312 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 44 61 74 61  }else{.    zData
14313 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74   = (char *)sqlit
14314 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68  e3BtreeDataFetch
14315 28 70 43 75 72 2c 20 26 61 76 61 69 6c 61 62 6c  (pCur, &availabl
14316 65 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  e);.  }.  assert
14317 28 20 7a 44 61 74 61 21 3d 30 20 29 3b 0a 0a 20  ( zData!=0 );.. 
14318 20 69 66 28 20 6f 66 66 73 65 74 2b 61 6d 74 3c   if( offset+amt<
14319 3d 61 76 61 69 6c 61 62 6c 65 20 26 26 20 28 28  =available && ((
1431a 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pMem->flags&MEM_
1431b 44 79 6e 29 3d 3d 30 20 7c 7c 20 70 4d 65 6d 2d  Dyn)==0 || pMem-
1431c 3e 78 44 65 6c 29 20 29 7b 0a 20 20 20 20 73 71  >xDel) ){.    sq
1431d 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
1431e 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70  ase(pMem);.    p
1431f 4d 65 6d 2d 3e 7a 20 3d 20 26 7a 44 61 74 61 5b  Mem->z = &zData[
14320 6f 66 66 73 65 74 5d 3b 0a 20 20 20 20 70 4d 65  offset];.    pMe
14321 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42  m->flags = MEM_B
14322 6c 6f 62 7c 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20  lob|MEM_Ephem;. 
14323 20 7d 65 6c 73 65 20 69 66 28 20 53 51 4c 49 54   }else if( SQLIT
14324 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK==(rc = sqli
14325 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70  te3VdbeMemGrow(p
14326 4d 65 6d 2c 20 61 6d 74 2b 32 2c 20 30 29 29 20  Mem, amt+2, 0)) 
14327 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  ){.    pMem->fla
14328 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45  gs = MEM_Blob|ME
14329 4d 5f 44 79 6e 7c 4d 45 4d 5f 54 65 72 6d 3b 0a  M_Dyn|MEM_Term;.
1432a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20      pMem->enc = 
1432b 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70  0;.    pMem->typ
1432c 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b  e = SQLITE_BLOB;
1432d 0a 20 20 20 20 69 66 28 20 6b 65 79 20 29 7b 0a  .    if( key ){.
1432e 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1432f 65 33 42 74 72 65 65 4b 65 79 28 70 43 75 72 2c  e3BtreeKey(pCur,
14330 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 4d   offset, amt, pM
14331 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 7d 65 6c 73  em->z);.    }els
14332 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  e{.      rc = sq
14333 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28 70  lite3BtreeData(p
14334 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74  Cur, offset, amt
14335 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20  , pMem->z);.    
14336 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 61 6d  }.    pMem->z[am
14337 74 5d 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d  t] = 0;.    pMem
14338 2d 3e 7a 5b 61 6d 74 2b 31 5d 20 3d 20 30 3b 0a  ->z[amt+1] = 0;.
14339 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1433a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73  TE_OK ){.      s
1433b 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1433c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20  ease(pMem);.    
1433d 7d 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 6e 20  }.  }.  pMem->n 
1433e 3d 20 61 6d 74 3b 0a 0a 20 20 72 65 74 75 72 6e  = amt;..  return
1433f 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 30 0a 2f 2a   rc;.}..#if 0./*
14340 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 76 61 72 69  .** Perform vari
14341 6f 75 73 20 63 68 65 63 6b 73 20 6f 6e 20 74 68  ous checks on th
14342 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 70 4d  e memory cell pM
14343 65 6d 2e 20 41 6e 20 61 73 73 65 72 74 28 29 20  em. An assert() 
14344 77 69 6c 6c 0a 2a 2a 20 66 61 69 6c 20 69 66 20  will.** fail if 
14345 70 4d 65 6d 20 69 73 20 69 6e 74 65 72 6e 61 6c  pMem is internal
14346 6c 79 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 2e  ly inconsistent.
14347 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
14348 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
14349 64 62 65 4d 65 6d 53 61 6e 69 74 79 28 4d 65 6d  dbeMemSanity(Mem
1434a 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66   *pMem){.  int f
1434b 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61  lags = pMem->fla
1434c 67 73 3b 0a 20 20 61 73 73 65 72 74 28 20 66 6c  gs;.  assert( fl
1434d 61 67 73 21 3d 30 20 29 3b 20 20 2f 2a 20 4d 75  ags!=0 );  /* Mu
1434e 73 74 20 64 65 66 69 6e 65 20 73 6f 6d 65 20 74  st define some t
1434f 79 70 65 20 2a 2f 0a 20 20 69 66 28 20 66 6c 61  ype */.  if( fla
14350 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45  gs & (MEM_Str|ME
14351 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69  M_Blob) ){.    i
14352 6e 74 20 78 20 3d 20 66 6c 61 67 73 20 26 20 28  nt x = flags & (
14353 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44  MEM_Static|MEM_D
14354 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d  yn|MEM_Ephem|MEM
14355 5f 53 68 6f 72 74 29 3b 0a 20 20 20 20 61 73 73  _Short);.    ass
14356 65 72 74 28 20 78 21 3d 30 20 29 3b 20 20 20 20  ert( x!=0 );    
14357 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e          /* Strin
14358 67 73 20 6d 75 73 74 20 64 65 66 69 6e 65 20 61  gs must define a
14359 20 73 74 72 69 6e 67 20 73 75 62 74 79 70 65 20   string subtype 
1435a 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  */.    assert( (
1435b 78 20 26 20 28 78 2d 31 29 29 3d 3d 30 20 29 3b  x & (x-1))==0 );
1435c 20 20 2f 2a 20 4f 6e 6c 79 20 6f 6e 65 20 73 74    /* Only one st
1435d 72 69 6e 67 20 73 75 62 74 79 70 65 20 63 61 6e  ring subtype can
1435e 20 62 65 20 64 65 66 69 6e 65 64 20 2a 2f 0a 20   be defined */. 
1435f 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d     assert( pMem-
14360 3e 7a 21 3d 30 20 29 3b 20 20 20 20 20 20 2f 2a  >z!=0 );      /*
14361 20 53 74 72 69 6e 67 73 20 6d 75 73 74 20 68 61   Strings must ha
14362 76 65 20 61 20 76 61 6c 75 65 20 2a 2f 0a 20 20  ve a value */.  
14363 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74    /* Mem.z point
14364 73 20 74 6f 20 4d 65 6d 2e 7a 53 68 6f 72 74 20  s to Mem.zShort 
14365 69 66 66 20 74 68 65 20 73 75 62 74 79 70 65 20  iff the subtype 
14366 69 73 20 4d 45 4d 5f 53 68 6f 72 74 20 2a 2f 0a  is MEM_Short */.
14367 20 20 20 20 61 73 73 65 72 74 28 20 28 78 20 26      assert( (x &
14368 20 4d 45 4d 5f 53 68 6f 72 74 29 3d 3d 30 20 7c   MEM_Short)==0 |
14369 7c 20 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 6d 2d  | pMem->z==pMem-
1436a 3e 7a 53 68 6f 72 74 20 29 3b 0a 20 20 20 20 61  >zShort );.    a
1436b 73 73 65 72 74 28 20 28 78 20 26 20 4d 45 4d 5f  ssert( (x & MEM_
1436c 53 68 6f 72 74 29 21 3d 30 20 7c 7c 20 70 4d 65  Short)!=0 || pMe
1436d 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a 53 68 6f  m->z!=pMem->zSho
1436e 72 74 20 29 3b 0a 20 20 20 20 2f 2a 20 4e 6f 20  rt );.    /* No 
1436f 64 65 73 74 72 75 63 74 6f 72 20 75 6e 6c 65 73  destructor unles
14370 73 20 74 68 65 72 65 20 69 73 20 4d 45 4d 5f 44  s there is MEM_D
14371 79 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  yn */.    assert
14372 28 20 70 4d 65 6d 2d 3e 78 44 65 6c 3d 3d 30 20  ( pMem->xDel==0 
14373 7c 7c 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  || (pMem->flags 
14374 26 20 4d 45 4d 5f 44 79 6e 29 21 3d 30 20 29 3b  & MEM_Dyn)!=0 );
14375 0a 0a 20 20 20 20 69 66 28 20 28 66 6c 61 67 73  ..    if( (flags
14376 20 26 20 4d 45 4d 5f 53 74 72 29 20 29 7b 0a 20   & MEM_Str) ){. 
14377 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65       assert( pMe
14378 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  m->enc==SQLITE_U
14379 54 46 38 20 7c 7c 20 0a 20 20 20 20 20 20 20 20  TF8 || .        
1437a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 3d        pMem->enc=
1437b 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20  =SQLITE_UTF16BE 
1437c 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ||.             
1437d 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49   pMem->enc==SQLI
1437e 54 45 5f 55 54 46 31 36 4c 45 20 0a 20 20 20 20  TE_UTF16LE .    
1437f 20 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 49 66    );.      /* If
14380 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 55   the string is U
14381 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 61 6e 64  TF-8 encoded and
14382 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 2c   nul terminated,
14383 20 74 68 65 6e 20 70 4d 65 6d 2d 3e 6e 0a 20 20   then pMem->n.  
14384 20 20 20 20 2a 2a 20 6d 75 73 74 20 62 65 20 74      ** must be t
14385 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65  he length of the
14386 20 73 74 72 69 6e 67 2e 20 20 28 4c 61 74 65 72   string.  (Later
14387 3a 29 20 20 49 66 20 74 68 65 20 64 61 74 61 62  :)  If the datab
14388 61 73 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a  ase file.      *
14389 2a 20 68 61 73 20 62 65 65 6e 20 63 6f 72 72 75  * has been corru
1438a 70 74 65 64 2c 20 27 5c 30 30 30 27 20 63 68 61  pted, '\000' cha
1438b 72 61 63 74 65 72 73 20 6d 69 67 68 74 20 68 61  racters might ha
1438c 76 65 20 62 65 65 6e 20 69 6e 73 65 72 74 65 64  ve been inserted
1438d 0a 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74  .      ** into t
1438e 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 74 68 65  he middle of the
1438f 20 73 74 72 69 6e 67 2e 20 20 49 6e 20 74 68 61   string.  In tha
14390 74 20 63 61 73 65 2c 20 74 68 65 20 73 74 72 6c  t case, the strl
14391 65 6e 28 29 20 6d 69 67 68 74 0a 20 20 20 20 20  en() might.     
14392 20 2a 2a 20 62 65 20 6c 65 73 73 2e 0a 20 20 20   ** be less..   
14393 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
14394 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54  pMem->enc==SQLIT
14395 45 5f 55 54 46 38 20 26 26 20 28 66 6c 61 67 73  E_UTF8 && (flags
14396 20 26 20 4d 45 4d 5f 54 65 72 6d 29 20 29 7b 20   & MEM_Term) ){ 
14397 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
14398 20 73 74 72 6c 65 6e 28 70 4d 65 6d 2d 3e 7a 29   strlen(pMem->z)
14399 3c 3d 70 4d 65 6d 2d 3e 6e 20 29 3b 0a 20 20 20  <=pMem->n );.   
1439a 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65       assert( pMe
1439b 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 3d 3d 30  m->z[pMem->n]==0
1439c 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   );.      }.    
1439d 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  }.  }else{.    /
1439e 2a 20 43 61 6e 6e 6f 74 20 64 65 66 69 6e 65 20  * Cannot define 
1439f 61 20 73 74 72 69 6e 67 20 73 75 62 74 79 70 65  a string subtype
143a0 20 66 6f 72 20 6e 6f 6e 2d 73 74 72 69 6e 67 20   for non-string 
143a1 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 20 20 61  objects */.    a
143a2 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c  ssert( (pMem->fl
143a3 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 61 74 69  ags & (MEM_Stati
143a4 63 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70  c|MEM_Dyn|MEM_Ep
143a5 68 65 6d 7c 4d 45 4d 5f 53 68 6f 72 74 29 29 3d  hem|MEM_Short))=
143a6 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
143a7 28 20 70 4d 65 6d 2d 3e 78 44 65 6c 3d 3d 30 20  ( pMem->xDel==0 
143a8 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 4d 45 4d 5f  );.  }.  /* MEM_
143a9 4e 75 6c 6c 20 65 78 63 6c 75 64 65 73 20 61 6c  Null excludes al
143aa 6c 20 6f 74 68 65 72 20 74 79 70 65 73 20 2a 2f  l other types */
143ab 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d  .  assert( (pMem
143ac 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72  ->flags&(MEM_Str
143ad 7c 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61  |MEM_Int|MEM_Rea
143ae 6c 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30 0a  l|MEM_Blob))==0.
143af 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28 70 4d            || (pM
143b0 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75  em->flags&MEM_Nu
143b1 6c 6c 29 3d 3d 30 20 29 3b 0a 20 20 2f 2a 20 49  ll)==0 );.  /* I
143b2 66 20 74 68 65 20 4d 45 4d 20 69 73 20 62 6f 74  f the MEM is bot
143b3 68 20 72 65 61 6c 20 61 6e 64 20 69 6e 74 65 67  h real and integ
143b4 65 72 2c 20 74 68 65 20 76 61 6c 75 65 73 20 61  er, the values a
143b5 72 65 20 65 71 75 61 6c 20 2a 2f 0a 20 20 61 73  re equal */.  as
143b6 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61  sert( (pMem->fla
143b7 67 73 20 26 20 28 4d 45 4d 5f 49 6e 74 7c 4d 45  gs & (MEM_Int|ME
143b8 4d 5f 52 65 61 6c 29 29 21 3d 28 4d 45 4d 5f 49  M_Real))!=(MEM_I
143b9 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20 0a 20 20  nt|MEM_Real) .  
143ba 20 20 20 20 20 20 20 20 7c 7c 20 70 4d 65 6d 2d          || pMem-
143bb 3e 72 3d 3d 70 4d 65 6d 2d 3e 75 2e 69 20 29 3b  >r==pMem->u.i );
143bc 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 54 68  .}.#endif../* Th
143bd 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f  is function is o
143be 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 6e  nly available in
143bf 74 65 72 6e 61 6c 6c 79 2c 20 69 74 20 69 73 20  ternally, it is 
143c0 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 0a  not part of the.
143c1 2a 2a 20 65 78 74 65 72 6e 61 6c 20 41 50 49 2e  ** external API.
143c2 20 49 74 20 77 6f 72 6b 73 20 69 6e 20 61 20 73   It works in a s
143c3 69 6d 69 6c 61 72 20 77 61 79 20 74 6f 20 73 71  imilar way to sq
143c4 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
143c5 28 29 2c 0a 2a 2a 20 65 78 63 65 70 74 20 74 68  (),.** except th
143c6 65 20 64 61 74 61 20 72 65 74 75 72 6e 65 64 20  e data returned 
143c7 69 73 20 69 6e 20 74 68 65 20 65 6e 63 6f 64 69  is in the encodi
143c8 6e 67 20 73 70 65 63 69 66 69 65 64 20 62 79 20  ng specified by 
143c9 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 70 61  the second.** pa
143ca 72 61 6d 65 74 65 72 2c 20 77 68 69 63 68 20 6d  rameter, which m
143cb 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20 53 51  ust be one of SQ
143cc 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53 51  LITE_UTF16BE, SQ
143cd 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 0a  LITE_UTF16LE or.
143ce 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 38 2e 0a  ** SQLITE_UTF8..
143cf 2a 2a 0a 2a 2a 20 28 32 30 30 36 2d 30 32 2d 31  **.** (2006-02-1
143d0 36 3a 29 20 20 54 68 65 20 65 6e 63 20 76 61 6c  6:)  The enc val
143d1 75 65 20 63 61 6e 20 62 65 20 6f 72 2d 65 64 20  ue can be or-ed 
143d2 77 69 74 68 20 53 51 4c 49 54 45 5f 55 54 46 31  with SQLITE_UTF1
143d3 36 5f 41 4c 49 47 4e 45 44 2e 0a 2a 2a 20 49 66  6_ALIGNED..** If
143d4 20 74 68 61 74 20 69 73 20 74 68 65 20 63 61 73   that is the cas
143d5 65 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  e, then the resu
143d6 6c 74 20 6d 75 73 74 20 62 65 20 61 6c 69 67 6e  lt must be align
143d7 65 64 20 6f 6e 20 61 6e 20 65 76 65 6e 20 62 79  ed on an even by
143d8 74 65 0a 2a 2a 20 62 6f 75 6e 64 61 72 79 2e 0a  te.** boundary..
143d9 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
143da 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  E const void *sq
143db 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 73  lite3ValueText(s
143dc 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 70 56  qlite3_value* pV
143dd 61 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 69  al, u8 enc){.  i
143de 66 28 20 21 70 56 61 6c 20 29 20 72 65 74 75 72  f( !pVal ) retur
143df 6e 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  n 0;..  assert( 
143e0 70 56 61 6c 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73  pVal->db==0 || s
143e1 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
143e2 64 28 70 56 61 6c 2d 3e 64 62 2d 3e 6d 75 74 65  d(pVal->db->mute
143e3 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
143e4 28 65 6e 63 26 33 29 3d 3d 28 65 6e 63 26 7e 53  (enc&3)==(enc&~S
143e5 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47  QLITE_UTF16_ALIG
143e6 4e 45 44 29 20 29 3b 0a 0a 20 20 69 66 28 20 70  NED) );..  if( p
143e7 56 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e  Val->flags&MEM_N
143e8 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ull ){.    retur
143e9 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  n 0;.  }.  asser
143ea 74 28 20 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29  t( (MEM_Blob>>3)
143eb 20 3d 3d 20 4d 45 4d 5f 53 74 72 20 29 3b 0a 20   == MEM_Str );. 
143ec 20 70 56 61 6c 2d 3e 66 6c 61 67 73 20 7c 3d 20   pVal->flags |= 
143ed 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d  (pVal->flags & M
143ee 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a 20 20 65  EM_Blob)>>3;.  e
143ef 78 70 61 6e 64 42 6c 6f 62 28 70 56 61 6c 29 3b  xpandBlob(pVal);
143f0 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 66 6c 61  .  if( pVal->fla
143f1 67 73 26 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20  gs&MEM_Str ){.  
143f2 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
143f3 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 56 61 6c  ngeEncoding(pVal
143f4 2c 20 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f  , enc & ~SQLITE_
143f5 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 3b 0a  UTF16_ALIGNED);.
143f6 20 20 20 20 69 66 28 20 28 65 6e 63 20 26 20 53      if( (enc & S
143f7 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47  QLITE_UTF16_ALIG
143f8 4e 45 44 29 21 3d 30 20 26 26 20 31 3d 3d 28 31  NED)!=0 && 1==(1
143f9 26 28 69 6e 74 29 70 56 61 6c 2d 3e 7a 29 20 29  &(int)pVal->z) )
143fa 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
143fb 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 28  (pVal->flags & (
143fc 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74  MEM_Ephem|MEM_St
143fd 61 74 69 63 29 29 21 3d 30 20 29 3b 0a 20 20 20  atic))!=0 );.   
143fe 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64     if( sqlite3Vd
143ff 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62  beMemMakeWriteab
14400 6c 65 28 70 56 61 6c 29 21 3d 53 51 4c 49 54 45  le(pVal)!=SQLITE
14401 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
14402 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d  eturn 0;.      }
14403 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
14404 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d  e3VdbeMemNulTerm
14405 69 6e 61 74 65 28 70 56 61 6c 29 3b 0a 20 20 7d  inate(pVal);.  }
14406 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
14407 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d  ( (pVal->flags&M
14408 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 3b 0a 20  EM_Blob)==0 );. 
14409 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1440a 6d 53 74 72 69 6e 67 69 66 79 28 70 56 61 6c 2c  mStringify(pVal,
1440b 20 65 6e 63 29 3b 0a 20 20 20 20 61 73 73 65 72   enc);.    asser
1440c 74 28 20 30 3d 3d 28 31 26 28 69 6e 74 29 70 56  t( 0==(1&(int)pV
1440d 61 6c 2d 3e 7a 29 20 29 3b 0a 20 20 7d 0a 20 20  al->z) );.  }.  
1440e 61 73 73 65 72 74 28 70 56 61 6c 2d 3e 65 6e 63  assert(pVal->enc
1440f 3d 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49 54 45  ==(enc & ~SQLITE
14410 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 20  _UTF16_ALIGNED) 
14411 7c 7c 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 0a 20  || pVal->db==0. 
14412 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
14413 70 56 61 6c 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  pVal->db->malloc
14414 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20  Failed );.  if( 
14415 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20  pVal->enc==(enc 
14416 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f  & ~SQLITE_UTF16_
14417 41 4c 49 47 4e 45 44 29 20 29 7b 0a 20 20 20 20  ALIGNED) ){.    
14418 72 65 74 75 72 6e 20 70 56 61 6c 2d 3e 7a 3b 0a  return pVal->z;.
14419 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
1441a 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  urn 0;.  }.}../*
1441b 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
1441c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
1441d 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  bject..*/.SQLITE
1441e 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33  _PRIVATE sqlite3
1441f 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56  _value *sqlite3V
14420 61 6c 75 65 4e 65 77 28 73 71 6c 69 74 65 33 20  alueNew(sqlite3 
14421 2a 64 62 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d  *db){.  Mem *p =
14422 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
14423 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
14424 2a 70 29 29 3b 0a 20 20 69 66 28 20 70 20 29 7b  *p));.  if( p ){
14425 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20  .    p->flags = 
14426 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 2d  MEM_Null;.    p-
14427 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e  >type = SQLITE_N
14428 55 4c 4c 3b 0a 20 20 20 20 70 2d 3e 64 62 20 3d  ULL;.    p->db =
14429 20 64 62 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   db;.  }.  retur
1442a 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72  n p;.}../*.** Cr
1442b 65 61 74 65 20 61 20 6e 65 77 20 73 71 6c 69 74  eate a new sqlit
1442c 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2c  e3_value object,
1442d 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
1442e 76 61 6c 75 65 20 6f 66 20 70 45 78 70 72 2e 0a  value of pExpr..
1442f 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 6e 6c 79 20  **.** This only 
14430 77 6f 72 6b 73 20 66 6f 72 20 76 65 72 79 20 73  works for very s
14431 69 6d 70 6c 65 20 65 78 70 72 65 73 73 69 6f 6e  imple expression
14432 73 20 74 68 61 74 20 63 6f 6e 73 69 73 74 20 6f  s that consist o
14433 66 20 6f 6e 65 20 63 6f 6e 73 74 61 6e 74 0a 2a  f one constant.*
14434 2a 20 74 6f 6b 65 6e 20 28 69 2e 65 2e 20 22 35  * token (i.e. "5
14435 22 2c 20 22 35 2e 31 22 2c 20 22 27 61 20 73 74  ", "5.1", "'a st
14436 72 69 6e 67 27 22 29 2e 20 49 66 20 74 68 65 20  ring'"). If the 
14437 65 78 70 72 65 73 73 69 6f 6e 20 63 61 6e 0a 2a  expression can.*
14438 2a 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 64  * be converted d
14439 69 72 65 63 74 6c 79 20 69 6e 74 6f 20 61 20 76  irectly into a v
1443a 61 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20 76  alue, then the v
1443b 61 6c 75 65 20 69 73 20 61 6c 6c 6f 63 61 74 65  alue is allocate
1443c 64 20 61 6e 64 0a 2a 2a 20 61 20 70 6f 69 6e 74  d and.** a point
1443d 65 72 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70  er written to *p
1443e 70 56 61 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72  pVal. The caller
1443f 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
14440 66 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67  for deallocating
14441 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 62 79  .** the value by
14442 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f 20 73   passing it to s
14443 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
14444 29 20 6c 61 74 65 72 20 6f 6e 2e 20 49 66 20 74  ) later on. If t
14445 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a  he expression.**
14446 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65   cannot be conve
14447 72 74 65 64 20 74 6f 20 61 20 76 61 6c 75 65 2c  rted to a value,
14448 20 74 68 65 6e 20 2a 70 70 56 61 6c 20 69 73 20   then *ppVal is 
14449 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a  set to NULL..*/.
1444a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1444b 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46  nt sqlite3ValueF
1444c 72 6f 6d 45 78 70 72 28 0a 20 20 73 71 6c 69 74  romExpr(.  sqlit
1444d 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
1444e 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
1444f 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
14450 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  */.  Expr *pExpr
14451 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
14452 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e  * The expression
14453 20 74 6f 20 65 76 61 6c 75 61 74 65 20 2a 2f 0a   to evaluate */.
14454 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20    u8 enc,       
14455 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
14456 6e 63 6f 64 69 6e 67 20 74 6f 20 75 73 65 20 2a  ncoding to use *
14457 2f 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 2c  /.  u8 affinity,
14458 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
14459 20 41 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65   Affinity to use
1445a 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61   */.  sqlite3_va
1445b 6c 75 65 20 2a 2a 70 70 56 61 6c 20 20 20 20 20  lue **ppVal     
1445c 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 65 77  /* Write the new
1445d 20 76 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a 29   value here */.)
1445e 7b 0a 20 20 69 6e 74 20 6f 70 3b 0a 20 20 63 68  {.  int op;.  ch
1445f 61 72 20 2a 7a 56 61 6c 20 3d 20 30 3b 0a 20 20  ar *zVal = 0;.  
14460 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
14461 56 61 6c 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20  Val = 0;..  if( 
14462 21 70 45 78 70 72 20 29 7b 0a 20 20 20 20 2a 70  !pExpr ){.    *p
14463 70 56 61 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65  pVal = 0;.    re
14464 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
14465 20 20 7d 0a 20 20 6f 70 20 3d 20 70 45 78 70 72    }.  op = pExpr
14466 2d 3e 6f 70 3b 0a 0a 20 20 69 66 28 20 6f 70 3d  ->op;..  if( op=
14467 3d 54 4b 5f 53 54 52 49 4e 47 20 7c 7c 20 6f 70  =TK_STRING || op
14468 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 6f 70  ==TK_FLOAT || op
14469 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a  ==TK_INTEGER ){.
1446a 20 20 20 20 7a 56 61 6c 20 3d 20 73 71 6c 69 74      zVal = sqlit
1446b 65 33 53 74 72 4e 44 75 70 28 28 63 68 61 72 2a  e3StrNDup((char*
1446c 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c  )pExpr->token.z,
1446d 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 29   pExpr->token.n)
1446e 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20 73 71 6c  ;.    pVal = sql
1446f 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64 62 29  ite3ValueNew(db)
14470 3b 0a 20 20 20 20 69 66 28 20 21 7a 56 61 6c 20  ;.    if( !zVal 
14471 7c 7c 20 21 70 56 61 6c 20 29 20 67 6f 74 6f 20  || !pVal ) goto 
14472 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 73 71 6c 69  no_mem;.    sqli
14473 74 65 33 44 65 71 75 6f 74 65 28 7a 56 61 6c 29  te3Dequote(zVal)
14474 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c  ;.    sqlite3Val
14475 75 65 53 65 74 53 74 72 28 70 56 61 6c 2c 20 2d  ueSetStr(pVal, -
14476 31 2c 20 7a 56 61 6c 2c 20 53 51 4c 49 54 45 5f  1, zVal, SQLITE_
14477 55 54 46 38 2c 20 73 71 6c 69 74 65 33 5f 66 72  UTF8, sqlite3_fr
14478 65 65 29 3b 0a 20 20 20 20 69 66 28 20 28 6f 70  ee);.    if( (op
14479 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 7c 7c 20  ==TK_INTEGER || 
1447a 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 26  op==TK_FLOAT ) &
1447b 26 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49  & affinity==SQLI
1447c 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20  TE_AFF_NONE ){. 
1447d 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75       sqlite3Valu
1447e 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70  eApplyAffinity(p
1447f 56 61 6c 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f  Val, SQLITE_AFF_
14480 4e 55 4d 45 52 49 43 2c 20 65 6e 63 29 3b 0a 20  NUMERIC, enc);. 
14481 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
14482 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70 70 6c  sqlite3ValueAppl
14483 79 41 66 66 69 6e 69 74 79 28 70 56 61 6c 2c 20  yAffinity(pVal, 
14484 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 29 3b 0a  affinity, enc);.
14485 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
14486 28 20 6f 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53 20  ( op==TK_UMINUS 
14487 29 20 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49  ) {.    if( SQLI
14488 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 61  TE_OK==sqlite3Va
14489 6c 75 65 46 72 6f 6d 45 78 70 72 28 64 62 2c 70  lueFromExpr(db,p
1448a 45 78 70 72 2d 3e 70 4c 65 66 74 2c 65 6e 63 2c  Expr->pLeft,enc,
1448b 61 66 66 69 6e 69 74 79 2c 26 70 56 61 6c 29 20  affinity,&pVal) 
1448c 29 7b 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e 75  ){.      pVal->u
1448d 2e 69 20 3d 20 2d 31 20 2a 20 70 56 61 6c 2d 3e  .i = -1 * pVal->
1448e 75 2e 69 3b 0a 20 20 20 20 20 20 70 56 61 6c 2d  u.i;.      pVal-
1448f 3e 72 20 3d 20 2d 31 2e 30 20 2a 20 70 56 61 6c  >r = -1.0 * pVal
14490 2d 3e 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  ->r;.    }.  }.#
14491 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
14492 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a  IT_BLOB_LITERAL.
14493 20 20 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54    else if( op==T
14494 4b 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 69 6e  K_BLOB ){.    in
14495 74 20 6e 56 61 6c 3b 0a 20 20 20 20 61 73 73 65  t nVal;.    asse
14496 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  rt( pExpr->token
14497 2e 6e 3e 3d 33 20 29 3b 0a 20 20 20 20 61 73 73  .n>=3 );.    ass
14498 65 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65  ert( pExpr->toke
14499 6e 2e 7a 5b 30 5d 3d 3d 27 78 27 20 7c 7c 20 70  n.z[0]=='x' || p
1449a 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d  Expr->token.z[0]
1449b 3d 3d 27 58 27 20 29 3b 0a 20 20 20 20 61 73 73  =='X' );.    ass
1449c 65 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65  ert( pExpr->toke
1449d 6e 2e 7a 5b 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a  n.z[1]=='\'' );.
1449e 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70      assert( pExp
1449f 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 70 45 78 70 72  r->token.z[pExpr
144a0 2d 3e 74 6f 6b 65 6e 2e 6e 2d 31 5d 3d 3d 27 5c  ->token.n-1]=='\
144a1 27 27 20 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d  '' );.    pVal =
144a2 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77   sqlite3ValueNew
144a3 28 64 62 29 3b 0a 20 20 20 20 6e 56 61 6c 20 3d  (db);.    nVal =
144a4 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 20   pExpr->token.n 
144a5 2d 20 33 3b 0a 20 20 20 20 7a 56 61 6c 20 3d 20  - 3;.    zVal = 
144a6 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f  (char*)pExpr->to
144a7 6b 65 6e 2e 7a 20 2b 20 32 3b 0a 20 20 20 20 73  ken.z + 2;.    s
144a8 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
144a9 53 74 72 28 70 56 61 6c 2c 20 73 71 6c 69 74 65  Str(pVal, sqlite
144aa 33 48 65 78 54 6f 42 6c 6f 62 28 64 62 2c 20 7a  3HexToBlob(db, z
144ab 56 61 6c 2c 20 6e 56 61 6c 29 2c 20 6e 56 61 6c  Val, nVal), nVal
144ac 2f 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  /2,.            
144ad 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20               0, 
144ae 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20  sqlite3_free);. 
144af 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2a 70 70   }.#endif..  *pp
144b0 56 61 6c 20 3d 20 70 56 61 6c 3b 0a 20 20 72 65  Val = pVal;.  re
144b1 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
144b2 0a 6e 6f 5f 6d 65 6d 3a 0a 20 20 64 62 2d 3e 6d  .no_mem:.  db->m
144b3 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
144b4 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
144b5 7a 56 61 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33  zVal);.  sqlite3
144b6 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29 3b  ValueFree(pVal);
144b7 0a 20 20 2a 70 70 56 61 6c 20 3d 20 30 3b 0a 20  .  *ppVal = 0;. 
144b8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
144b9 4f 4d 45 4d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  OMEM;.}../*.** C
144ba 68 61 6e 67 65 20 74 68 65 20 73 74 72 69 6e 67  hange the string
144bb 20 76 61 6c 75 65 20 6f 66 20 61 6e 20 73 71 6c   value of an sql
144bc 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63  ite3_value objec
144bd 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  t.*/.SQLITE_PRIV
144be 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
144bf 56 61 6c 75 65 53 65 74 53 74 72 28 0a 20 20 73  ValueSetStr(.  s
144c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 76 2c  qlite3_value *v,
144c1 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f       /* Value to
144c2 20 62 65 20 73 65 74 20 2a 2f 0a 20 20 69 6e 74   be set */.  int
144c3 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   n,             
144c4 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
144c5 73 74 72 69 6e 67 20 7a 20 2a 2f 0a 20 20 63 6f  string z */.  co
144c6 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 20 20 20  nst void *z,    
144c7 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74      /* Text of t
144c8 68 65 20 6e 65 77 20 73 74 72 69 6e 67 20 2a 2f  he new string */
144c9 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20  .  u8 enc,      
144ca 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f           /* Enco
144cb 64 69 6e 67 20 74 6f 20 75 73 65 20 2a 2f 0a 20  ding to use */. 
144cc 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
144cd 69 64 2a 29 20 20 20 2f 2a 20 44 65 73 74 72 75  id*)   /* Destru
144ce 63 74 6f 72 20 66 6f 72 20 74 68 65 20 73 74 72  ctor for the str
144cf 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20  ing */.){.  if( 
144d0 76 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 4d  v ) sqlite3VdbeM
144d1 65 6d 53 65 74 53 74 72 28 28 4d 65 6d 20 2a 29  emSetStr((Mem *)
144d2 76 2c 20 7a 2c 20 6e 2c 20 65 6e 63 2c 20 78 44  v, z, n, enc, xD
144d3 65 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72  el);.}../*.** Fr
144d4 65 65 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61  ee an sqlite3_va
144d5 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51  lue object.*/.SQ
144d6 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
144d7 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72  d sqlite3ValueFr
144d8 65 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ee(sqlite3_value
144d9 20 2a 76 29 7b 0a 20 20 69 66 28 20 21 76 20 29   *v){.  if( !v )
144da 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74   return;.  sqlit
144db 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
144dc 28 28 4d 65 6d 20 2a 29 76 29 3b 0a 20 20 73 71  ((Mem *)v);.  sq
144dd 6c 69 74 65 33 5f 66 72 65 65 28 76 29 3b 0a 7d  lite3_free(v);.}
144de 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
144df 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
144e0 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65  es in the sqlite
144e1 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 61  3_value object a
144e2 73 73 75 6d 69 6e 67 0a 2a 2a 20 74 68 61 74 20  ssuming.** that 
144e3 69 74 20 75 73 65 73 20 74 68 65 20 65 6e 63 6f  it uses the enco
144e4 64 69 6e 67 20 22 65 6e 63 22 0a 2a 2f 0a 53 51  ding "enc".*/.SQ
144e5 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
144e6 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74   sqlite3ValueByt
144e7 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  es(sqlite3_value
144e8 20 2a 70 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b   *pVal, u8 enc){
144e9 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 28 4d 65 6d  .  Mem *p = (Mem
144ea 2a 29 70 56 61 6c 3b 0a 20 20 69 66 28 20 28 70  *)pVal;.  if( (p
144eb 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c  ->flags & MEM_Bl
144ec 6f 62 29 21 3d 30 20 7c 7c 20 73 71 6c 69 74 65  ob)!=0 || sqlite
144ed 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c  3ValueText(pVal,
144ee 20 65 6e 63 29 20 29 7b 0a 20 20 20 20 69 66 28   enc) ){.    if(
144ef 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f   p->flags & MEM_
144f0 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 72 65  Zero ){.      re
144f1 74 75 72 6e 20 70 2d 3e 6e 2b 70 2d 3e 75 2e 69  turn p->n+p->u.i
144f2 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
144f3 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 3b 0a     return p->n;.
144f4 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
144f5 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  rn 0;.}../******
144f6 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
144f7 76 64 62 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a  vdbemem.c ******
144f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
144f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
144fa 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
144fb 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
144fc 69 6c 65 20 76 64 62 65 61 75 78 2e 63 20 2a 2a  ile vdbeaux.c **
144fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
144fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
144ff 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
14500 30 30 33 20 53 65 70 74 65 6d 62 65 72 20 36 0a  003 September 6.
14501 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
14502 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
14503 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
14504 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
14505 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
14506 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
14507 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
14508 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
14509 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
1450a 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
1450b 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
1450c 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
1450d 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
1450e 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
1450f 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
14510 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
14511 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
14512 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
14513 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14514 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14515 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14516 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
14517 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
14518 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 66  ains code used f
14519 6f 72 20 63 72 65 61 74 69 6e 67 2c 20 64 65 73  or creating, des
1451a 74 72 6f 79 69 6e 67 2c 20 61 6e 64 20 70 6f 70  troying, and pop
1451b 75 6c 61 74 69 6e 67 0a 2a 2a 20 61 20 56 44 42  ulating.** a VDB
1451c 45 20 28 6f 72 20 61 6e 20 22 73 71 6c 69 74 65  E (or an "sqlite
1451d 33 5f 73 74 6d 74 22 20 61 73 20 69 74 20 69 73  3_stmt" as it is
1451e 20 6b 6e 6f 77 6e 20 74 6f 20 74 68 65 20 6f 75   known to the ou
1451f 74 73 69 64 65 20 77 6f 72 6c 64 2e 29 20 20 50  tside world.)  P
14520 72 69 6f 72 0a 2a 2a 20 74 6f 20 76 65 72 73 69  rior.** to versi
14521 6f 6e 20 32 2e 38 2e 37 2c 20 61 6c 6c 20 74 68  on 2.8.7, all th
14522 69 73 20 63 6f 64 65 20 77 61 73 20 63 6f 6d 62  is code was comb
14523 69 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 76 64  ined into the vd
14524 62 65 2e 63 20 73 6f 75 72 63 65 20 66 69 6c 65  be.c source file
14525 2e 0a 2a 2a 20 42 75 74 20 74 68 61 74 20 66 69  ..** But that fi
14526 6c 65 20 77 61 73 20 67 65 74 74 69 6e 67 20 74  le was getting t
14527 6f 6f 20 62 69 67 20 73 6f 20 74 68 69 73 20 73  oo big so this s
14528 75 62 72 6f 75 74 69 6e 65 73 20 77 65 72 65 20  ubroutines were 
14529 73 70 6c 69 74 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a  split out..**.**
1452a 20 24 49 64 3a 20 76 64 62 65 61 75 78 2e 63 2c   $Id: vdbeaux.c,
1452b 76 20 31 2e 33 38 33 20 32 30 30 38 2f 30 35 2f  v 1.383 2008/05/
1452c 31 33 20 31 33 3a 32 37 3a 33 34 20 64 72 68 20  13 13:27:34 drh 
1452d 45 78 70 20 24 0a 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a  Exp $.*/..../*.*
1452e 2a 20 57 68 65 6e 20 64 65 62 75 67 67 69 6e 67  * When debugging
1452f 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61   the code genera
14530 74 6f 72 20 69 6e 20 61 20 73 79 6d 62 6f 6c 69  tor in a symboli
14531 63 20 64 65 62 75 67 67 65 72 2c 20 6f 6e 65 20  c debugger, one 
14532 63 61 6e 0a 2a 2a 20 73 65 74 20 74 68 65 20 73  can.** set the s
14533 71 6c 69 74 65 33 56 64 62 65 41 64 64 6f 70 54  qlite3VdbeAddopT
14534 72 61 63 65 20 74 6f 20 31 20 61 6e 64 20 61 6c  race to 1 and al
14535 6c 20 6f 70 63 6f 64 65 73 20 77 69 6c 6c 20 62  l opcodes will b
14536 65 20 70 72 69 6e 74 65 64 0a 2a 2a 20 61 73 20  e printed.** as 
14537 74 68 65 79 20 61 72 65 20 61 64 64 65 64 20 74  they are added t
14538 6f 20 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f  o the instructio
14539 6e 20 73 74 72 65 61 6d 2e 0a 2a 2f 0a 23 69 66  n stream..*/.#if
1453a 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
1453b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1453c 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
1453d 64 64 6f 70 54 72 61 63 65 20 3d 20 30 3b 0a 23  ddopTrace = 0;.#
1453e 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72  endif.../*.** Cr
1453f 65 61 74 65 20 61 20 6e 65 77 20 76 69 72 74 75  eate a new virtu
14540 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69  al database engi
14541 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ne..*/.SQLITE_PR
14542 49 56 41 54 45 20 56 64 62 65 20 2a 73 71 6c 69  IVATE Vdbe *sqli
14543 74 65 33 56 64 62 65 43 72 65 61 74 65 28 73 71  te3VdbeCreate(sq
14544 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 64  lite3 *db){.  Vd
14545 62 65 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c  be *p;.  p = sql
14546 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
14547 28 64 62 2c 20 73 69 7a 65 6f 66 28 56 64 62 65  (db, sizeof(Vdbe
14548 29 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  ) );.  if( p==0 
14549 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 2d  ) return 0;.  p-
1454a 3e 64 62 20 3d 20 64 62 3b 0a 20 20 69 66 28 20  >db = db;.  if( 
1454b 64 62 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20 20  db->pVdbe ){.   
1454c 20 64 62 2d 3e 70 56 64 62 65 2d 3e 70 50 72 65   db->pVdbe->pPre
1454d 76 20 3d 20 70 3b 0a 20 20 7d 0a 20 20 70 2d 3e  v = p;.  }.  p->
1454e 70 4e 65 78 74 20 3d 20 64 62 2d 3e 70 56 64 62  pNext = db->pVdb
1454f 65 3b 0a 20 20 70 2d 3e 70 50 72 65 76 20 3d 20  e;.  p->pPrev = 
14550 30 3b 0a 20 20 64 62 2d 3e 70 56 64 62 65 20 3d  0;.  db->pVdbe =
14551 20 70 3b 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d   p;.  p->magic =
14552 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54   VDBE_MAGIC_INIT
14553 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  ;.  return p;.}.
14554 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72 20  ./*.** Remember 
14555 74 68 65 20 53 51 4c 20 73 74 72 69 6e 67 20 66  the SQL string f
14556 6f 72 20 61 20 70 72 65 70 61 72 65 64 20 73 74  or a prepared st
14557 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  atement..*/.SQLI
14558 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
14559 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 53 71  sqlite3VdbeSetSq
1455a 6c 28 56 64 62 65 20 2a 70 2c 20 63 6f 6e 73 74  l(Vdbe *p, const
1455b 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29   char *z, int n)
1455c 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  {.  if( p==0 ) r
1455d 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28  eturn;.  assert(
1455e 20 70 2d 3e 7a 53 71 6c 3d 3d 30 20 29 3b 0a 20   p->zSql==0 );. 
1455f 20 70 2d 3e 7a 53 71 6c 20 3d 20 73 71 6c 69 74   p->zSql = sqlit
14560 65 33 44 62 53 74 72 4e 44 75 70 28 70 2d 3e 64  e3DbStrNDup(p->d
14561 62 2c 20 7a 2c 20 6e 29 3b 0a 7d 0a 0a 2f 2a 0a  b, z, n);.}../*.
14562 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 53 51  ** Return the SQ
14563 4c 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  L associated wit
14564 68 20 61 20 70 72 65 70 61 72 65 64 20 73 74 61  h a prepared sta
14565 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45  tement.*/.SQLITE
14566 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
14567 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28 73 71 6c  *sqlite3_sql(sql
14568 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
14569 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 28 56 64  ){.  return ((Vd
1456a 62 65 20 2a 29 70 53 74 6d 74 29 2d 3e 7a 53 71  be *)pStmt)->zSq
1456b 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 77 61 70  l;.}../*.** Swap
1456c 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 20 62 65 74   all content bet
1456d 77 65 65 6e 20 74 77 6f 20 56 44 42 45 20 73 74  ween two VDBE st
1456e 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 53 51 4c  ructures..*/.SQL
1456f 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
14570 20 73 71 6c 69 74 65 33 56 64 62 65 53 77 61 70   sqlite3VdbeSwap
14571 28 56 64 62 65 20 2a 70 41 2c 20 56 64 62 65 20  (Vdbe *pA, Vdbe 
14572 2a 70 42 29 7b 0a 20 20 56 64 62 65 20 74 6d 70  *pB){.  Vdbe tmp
14573 2c 20 2a 70 54 6d 70 3b 0a 20 20 63 68 61 72 20  , *pTmp;.  char 
14574 2a 7a 54 6d 70 3b 0a 20 20 69 6e 74 20 6e 54 6d  *zTmp;.  int nTm
14575 70 3b 0a 20 20 74 6d 70 20 3d 20 2a 70 41 3b 0a  p;.  tmp = *pA;.
14576 20 20 2a 70 41 20 3d 20 2a 70 42 3b 0a 20 20 2a    *pA = *pB;.  *
14577 70 42 20 3d 20 74 6d 70 3b 0a 20 20 70 54 6d 70  pB = tmp;.  pTmp
14578 20 3d 20 70 41 2d 3e 70 4e 65 78 74 3b 0a 20 20   = pA->pNext;.  
14579 70 41 2d 3e 70 4e 65 78 74 20 3d 20 70 42 2d 3e  pA->pNext = pB->
1457a 70 4e 65 78 74 3b 0a 20 20 70 42 2d 3e 70 4e 65  pNext;.  pB->pNe
1457b 78 74 20 3d 20 70 54 6d 70 3b 0a 20 20 70 54 6d  xt = pTmp;.  pTm
1457c 70 20 3d 20 70 41 2d 3e 70 50 72 65 76 3b 0a 20  p = pA->pPrev;. 
1457d 20 70 41 2d 3e 70 50 72 65 76 20 3d 20 70 42 2d   pA->pPrev = pB-
1457e 3e 70 50 72 65 76 3b 0a 20 20 70 42 2d 3e 70 50  >pPrev;.  pB->pP
1457f 72 65 76 20 3d 20 70 54 6d 70 3b 0a 20 20 7a 54  rev = pTmp;.  zT
14580 6d 70 20 3d 20 70 41 2d 3e 7a 53 71 6c 3b 0a 20  mp = pA->zSql;. 
14581 20 70 41 2d 3e 7a 53 71 6c 20 3d 20 70 42 2d 3e   pA->zSql = pB->
14582 7a 53 71 6c 3b 0a 20 20 70 42 2d 3e 7a 53 71 6c  zSql;.  pB->zSql
14583 20 3d 20 7a 54 6d 70 3b 0a 20 20 6e 54 6d 70 20   = zTmp;.  nTmp 
14584 3d 20 70 41 2d 3e 6e 53 71 6c 3b 0a 20 20 70 41  = pA->nSql;.  pA
14585 2d 3e 6e 53 71 6c 20 3d 20 70 42 2d 3e 6e 53 71  ->nSql = pB->nSq
14586 6c 3b 0a 20 20 70 42 2d 3e 6e 53 71 6c 20 3d 20  l;.  pB->nSql = 
14587 6e 54 6d 70 3b 0a 7d 0a 0a 23 69 66 64 65 66 20  nTmp;.}..#ifdef 
14588 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a  SQLITE_DEBUG./*.
14589 2a 2a 20 54 75 72 6e 20 74 72 61 63 69 6e 67 20  ** Turn tracing 
1458a 6f 6e 20 6f 72 20 6f 66 66 0a 2a 2f 0a 53 51 4c  on or off.*/.SQL
1458b 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1458c 20 73 71 6c 69 74 65 33 56 64 62 65 54 72 61 63   sqlite3VdbeTrac
1458d 65 28 56 64 62 65 20 2a 70 2c 20 46 49 4c 45 20  e(Vdbe *p, FILE 
1458e 2a 74 72 61 63 65 29 7b 0a 20 20 70 2d 3e 74 72  *trace){.  p->tr
1458f 61 63 65 20 3d 20 74 72 61 63 65 3b 0a 7d 0a 23  ace = trace;.}.#
14590 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 73  endif../*.** Res
14591 69 7a 65 20 74 68 65 20 56 64 62 65 2e 61 4f 70  ize the Vdbe.aOp
14592 20 61 72 72 61 79 20 73 6f 20 74 68 61 74 20 69   array so that i
14593 74 20 63 6f 6e 74 61 69 6e 73 20 61 74 20 6c 65  t contains at le
14594 61 73 74 20 4e 0a 2a 2a 20 65 6c 65 6d 65 6e 74  ast N.** element
14595 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 6f  s..**.** If an o
14596 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72  ut-of-memory err
14597 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20  or occurs while 
14598 72 65 73 69 7a 69 6e 67 20 74 68 65 20 61 72 72  resizing the arr
14599 61 79 2c 0a 2a 2a 20 56 64 62 65 2e 61 4f 70 20  ay,.** Vdbe.aOp 
1459a 61 6e 64 20 56 64 62 65 2e 6e 4f 70 41 6c 6c 6f  and Vdbe.nOpAllo
1459b 63 20 72 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67  c remain unchang
1459c 65 64 20 28 74 68 69 73 20 69 73 20 73 6f 20 74  ed (this is so t
1459d 68 61 74 0a 2a 2a 20 61 6e 79 20 6f 70 63 6f 64  hat.** any opcod
1459e 65 73 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63  es already alloc
1459f 61 74 65 64 20 63 61 6e 20 62 65 20 63 6f 72 72  ated can be corr
145a0 65 63 74 6c 79 20 64 65 61 6c 6c 6f 63 61 74 65  ectly deallocate
145a1 64 0a 2a 2a 20 61 6c 6f 6e 67 20 77 69 74 68 20  d.** along with 
145a2 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 20  the rest of the 
145a3 56 64 62 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  Vdbe)..*/.static
145a4 20 76 6f 69 64 20 72 65 73 69 7a 65 4f 70 41 72   void resizeOpAr
145a5 72 61 79 28 56 64 62 65 20 2a 70 2c 20 69 6e 74  ray(Vdbe *p, int
145a6 20 4e 29 7b 0a 20 20 56 64 62 65 4f 70 20 2a 70   N){.  VdbeOp *p
145a7 4e 65 77 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71  New;.  pNew = sq
145a8 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 70  lite3DbRealloc(p
145a9 2d 3e 64 62 2c 20 70 2d 3e 61 4f 70 2c 20 4e 2a  ->db, p->aOp, N*
145aa 73 69 7a 65 6f 66 28 4f 70 29 29 3b 0a 20 20 69  sizeof(Op));.  i
145ab 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 70  f( pNew ){.    p
145ac 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3d 20 4e 3b 0a  ->nOpAlloc = N;.
145ad 20 20 20 20 70 2d 3e 61 4f 70 20 3d 20 70 4e 65      p->aOp = pNe
145ae 77 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  w;.  }.}../*.** 
145af 41 64 64 20 61 20 6e 65 77 20 69 6e 73 74 72 75  Add a new instru
145b0 63 74 69 6f 6e 20 74 6f 20 74 68 65 20 6c 69 73  ction to the lis
145b1 74 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e  t of instruction
145b2 73 20 63 75 72 72 65 6e 74 20 69 6e 20 74 68 65  s current in the
145b3 0a 2a 2a 20 56 44 42 45 2e 20 20 52 65 74 75 72  .** VDBE.  Retur
145b4 6e 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66  n the address of
145b5 20 74 68 65 20 6e 65 77 20 69 6e 73 74 72 75 63   the new instruc
145b6 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61  tion..**.** Para
145b7 6d 65 74 65 72 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  meters:.**.**   
145b8 20 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20   p              
145b9 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   Pointer to the 
145ba 56 44 42 45 0a 2a 2a 0a 2a 2a 20 20 20 20 6f 70  VDBE.**.**    op
145bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68                Th
145bc 65 20 6f 70 63 6f 64 65 20 66 6f 72 20 74 68 69  e opcode for thi
145bd 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a  s instruction.**
145be 0a 2a 2a 20 20 20 20 70 31 2c 20 70 32 2c 20 70  .**    p1, p2, p
145bf 33 20 20 20 20 20 20 4f 70 65 72 61 6e 64 73 0a  3      Operands.
145c0 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 73 71  **.** Use the sq
145c1 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
145c2 4c 61 62 65 6c 28 29 20 66 75 6e 63 74 69 6f 6e  Label() function
145c3 20 74 6f 20 66 69 78 20 61 6e 20 61 64 64 72 65   to fix an addre
145c4 73 73 20 61 6e 64 0a 2a 2a 20 74 68 65 20 73 71  ss and.** the sq
145c5 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
145c6 34 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  4() function to 
145c7 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65  change the value
145c8 20 6f 66 20 74 68 65 20 50 34 0a 2a 2a 20 6f 70   of the P4.** op
145c9 65 72 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  erand..*/.SQLITE
145ca 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
145cb 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 56  ite3VdbeAddOp3(V
145cc 64 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20  dbe *p, int op, 
145cd 69 6e 74 20 70 31 2c 20 69 6e 74 20 70 32 2c 20  int p1, int p2, 
145ce 69 6e 74 20 70 33 29 7b 0a 20 20 69 6e 74 20 69  int p3){.  int i
145cf 3b 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b  ;.  VdbeOp *pOp;
145d0 0a 0a 20 20 69 20 3d 20 70 2d 3e 6e 4f 70 3b 0a  ..  i = p->nOp;.
145d1 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67    assert( p->mag
145d2 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49  ic==VDBE_MAGIC_I
145d3 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e  NIT );.  if( p->
145d4 6e 4f 70 41 6c 6c 6f 63 3c 3d 69 20 29 7b 0a 20  nOpAlloc<=i ){. 
145d5 20 20 20 72 65 73 69 7a 65 4f 70 41 72 72 61 79     resizeOpArray
145d6 28 70 2c 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20  (p, p->nOpAlloc 
145d7 3f 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 2a 32 20  ? p->nOpAlloc*2 
145d8 3a 20 31 30 32 34 2f 73 69 7a 65 6f 66 28 4f 70  : 1024/sizeof(Op
145d9 29 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 64  ));.    if( p->d
145da 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
145db 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
145dc 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  0;.    }.  }.  p
145dd 2d 3e 6e 4f 70 2b 2b 3b 0a 20 20 70 4f 70 20 3d  ->nOp++;.  pOp =
145de 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 70   &p->aOp[i];.  p
145df 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 6f 70 3b  Op->opcode = op;
145e0 0a 20 20 70 4f 70 2d 3e 70 35 20 3d 20 30 3b 0a  .  pOp->p5 = 0;.
145e1 20 20 70 4f 70 2d 3e 70 31 20 3d 20 70 31 3b 0a    pOp->p1 = p1;.
145e2 20 20 70 4f 70 2d 3e 70 32 20 3d 20 70 32 3b 0a    pOp->p2 = p2;.
145e3 20 20 70 4f 70 2d 3e 70 33 20 3d 20 70 33 3b 0a    pOp->p3 = p3;.
145e4 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b    pOp->p4.p = 0;
145e5 0a 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d  .  pOp->p4type =
145e6 20 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 70   P4_NOTUSED;.  p
145e7 2d 3e 65 78 70 69 72 65 64 20 3d 20 30 3b 0a 23  ->expired = 0;.#
145e8 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
145e9 55 47 0a 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65  UG.  pOp->zComme
145ea 6e 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71  nt = 0;.  if( sq
145eb 6c 69 74 65 33 56 64 62 65 41 64 64 6f 70 54 72  lite3VdbeAddopTr
145ec 61 63 65 20 29 20 73 71 6c 69 74 65 33 56 64 62  ace ) sqlite3Vdb
145ed 65 50 72 69 6e 74 4f 70 28 30 2c 20 69 2c 20 26  ePrintOp(0, i, &
145ee 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 23 65 6e 64  p->aOp[i]);.#end
145ef 69 66 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50  if.#ifdef VDBE_P
145f0 52 4f 46 49 4c 45 0a 20 20 70 4f 70 2d 3e 63 79  ROFILE.  pOp->cy
145f1 63 6c 65 73 20 3d 20 30 3b 0a 20 20 70 4f 70 2d  cles = 0;.  pOp-
145f2 3e 63 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66  >cnt = 0;.#endif
145f3 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 53  .  return i;.}.S
145f4 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
145f5 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  t sqlite3VdbeAdd
145f6 4f 70 30 28 56 64 62 65 20 2a 70 2c 20 69 6e 74  Op0(Vdbe *p, int
145f7 20 6f 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73   op){.  return s
145f8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
145f9 28 70 2c 20 6f 70 2c 20 30 2c 20 30 2c 20 30 29  (p, op, 0, 0, 0)
145fa 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
145fb 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
145fc 62 65 41 64 64 4f 70 31 28 56 64 62 65 20 2a 70  beAddOp1(Vdbe *p
145fd 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31  , int op, int p1
145fe 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
145ff 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c  te3VdbeAddOp3(p,
14600 20 6f 70 2c 20 70 31 2c 20 30 2c 20 30 29 3b 0a   op, p1, 0, 0);.
14601 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
14602 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
14603 41 64 64 4f 70 32 28 56 64 62 65 20 2a 70 2c 20  AddOp2(Vdbe *p, 
14604 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20  int op, int p1, 
14605 69 6e 74 20 70 32 29 7b 0a 20 20 72 65 74 75 72  int p2){.  retur
14606 6e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  n sqlite3VdbeAdd
14607 4f 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 70  Op3(p, op, p1, p
14608 32 2c 20 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  2, 0);.}.../*.**
14609 20 41 64 64 20 61 6e 20 6f 70 63 6f 64 65 20 74   Add an opcode t
1460a 68 61 74 20 69 6e 63 6c 75 64 65 73 20 74 68 65  hat includes the
1460b 20 70 34 20 76 61 6c 75 65 20 61 73 20 61 20 70   p4 value as a p
1460c 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  ointer..*/.SQLIT
1460d 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1460e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
1460f 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20  .  Vdbe *p,     
14610 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68         /* Add th
14611 65 20 6f 70 63 6f 64 65 20 74 6f 20 74 68 69 73  e opcode to this
14612 20 56 4d 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c   VM */.  int op,
14613 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
14614 54 68 65 20 6e 65 77 20 6f 70 63 6f 64 65 20 2a  The new opcode *
14615 2f 0a 20 20 69 6e 74 20 70 31 2c 20 20 20 20 20  /.  int p1,     
14616 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50          /* The P
14617 31 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69  1 operand */.  i
14618 6e 74 20 70 32 2c 20 20 20 20 20 20 20 20 20 20  nt p2,          
14619 20 20 20 2f 2a 20 54 68 65 20 50 32 20 6f 70 65     /* The P2 ope
1461a 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 33  rand */.  int p3
1461b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
1461c 20 54 68 65 20 50 33 20 6f 70 65 72 61 6e 64 20   The P3 operand 
1461d 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
1461e 2a 7a 50 34 2c 20 20 20 20 2f 2a 20 54 68 65 20  *zP4,    /* The 
1461f 50 34 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20  P4 operand */.  
14620 69 6e 74 20 70 34 74 79 70 65 20 20 20 20 20 20  int p4type      
14621 20 20 20 20 2f 2a 20 50 34 20 6f 70 65 72 61 6e      /* P4 operan
14622 64 20 74 79 70 65 20 2a 2f 0a 29 7b 0a 20 20 69  d type */.){.  i
14623 6e 74 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65  nt addr = sqlite
14624 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20 6f  3VdbeAddOp3(p, o
14625 70 2c 20 70 31 2c 20 70 32 2c 20 70 33 29 3b 0a  p, p1, p2, p3);.
14626 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
14627 6e 67 65 50 34 28 70 2c 20 61 64 64 72 2c 20 7a  ngeP4(p, addr, z
14628 50 34 2c 20 70 34 74 79 70 65 29 3b 0a 20 20 72  P4, p4type);.  r
14629 65 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f  eturn addr;.}../
1462a 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  *.** Create a ne
1462b 77 20 73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c  w symbolic label
1462c 20 66 6f 72 20 61 6e 20 69 6e 73 74 72 75 63 74   for an instruct
1462d 69 6f 6e 20 74 68 61 74 20 68 61 73 20 79 65 74  ion that has yet
1462e 20 74 6f 20 62 65 0a 2a 2a 20 63 6f 64 65 64 2e   to be.** coded.
1462f 20 20 54 68 65 20 73 79 6d 62 6f 6c 69 63 20 6c    The symbolic l
14630 61 62 65 6c 20 69 73 20 72 65 61 6c 6c 79 20 6a  abel is really j
14631 75 73 74 20 61 20 6e 65 67 61 74 69 76 65 20 6e  ust a negative n
14632 75 6d 62 65 72 2e 20 20 54 68 65 0a 2a 2a 20 6c  umber.  The.** l
14633 61 62 65 6c 20 63 61 6e 20 62 65 20 75 73 65 64  abel can be used
14634 20 61 73 20 74 68 65 20 50 32 20 76 61 6c 75 65   as the P2 value
14635 20 6f 66 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e   of an operation
14636 2e 20 20 4c 61 74 65 72 2c 20 77 68 65 6e 0a 2a  .  Later, when.*
14637 2a 20 74 68 65 20 6c 61 62 65 6c 20 69 73 20 72  * the label is r
14638 65 73 6f 6c 76 65 64 20 74 6f 20 61 20 73 70 65  esolved to a spe
14639 63 69 66 69 63 20 61 64 64 72 65 73 73 2c 20 74  cific address, t
1463a 68 65 20 56 44 42 45 20 77 69 6c 6c 20 73 63 61  he VDBE will sca
1463b 6e 0a 2a 2a 20 74 68 72 6f 75 67 68 20 69 74 73  n.** through its
1463c 20 6f 70 65 72 61 74 69 6f 6e 20 6c 69 73 74 20   operation list 
1463d 61 6e 64 20 63 68 61 6e 67 65 20 61 6c 6c 20 76  and change all v
1463e 61 6c 75 65 73 20 6f 66 20 50 32 20 77 68 69 63  alues of P2 whic
1463f 68 20 6d 61 74 63 68 0a 2a 2a 20 74 68 65 20 6c  h match.** the l
14640 61 62 65 6c 20 69 6e 74 6f 20 74 68 65 20 72 65  abel into the re
14641 73 6f 6c 76 65 64 20 61 64 64 72 65 73 73 2e 0a  solved address..
14642 2a 2a 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 6b  **.** The VDBE k
14643 6e 6f 77 73 20 74 68 61 74 20 61 20 50 32 20 76  nows that a P2 v
14644 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65 6c 20  alue is a label 
14645 62 65 63 61 75 73 65 20 6c 61 62 65 6c 73 20 61  because labels a
14646 72 65 0a 2a 2a 20 61 6c 77 61 79 73 20 6e 65 67  re.** always neg
14647 61 74 69 76 65 20 61 6e 64 20 50 32 20 76 61 6c  ative and P2 val
14648 75 65 73 20 61 72 65 20 73 75 70 70 6f 73 65 20  ues are suppose 
14649 74 6f 20 62 65 20 6e 6f 6e 2d 6e 65 67 61 74 69  to be non-negati
1464a 76 65 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 61 20  ve..** Hence, a 
1464b 6e 65 67 61 74 69 76 65 20 50 32 20 76 61 6c 75  negative P2 valu
1464c 65 20 69 73 20 61 20 6c 61 62 65 6c 20 74 68 61  e is a label tha
1464d 74 20 68 61 73 20 79 65 74 20 74 6f 20 62 65 20  t has yet to be 
1464e 72 65 73 6f 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20  resolved..**.** 
1464f 5a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64  Zero is returned
14650 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66   if a malloc() f
14651 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ails..*/.SQLITE_
14652 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
14653 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
14654 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74  (Vdbe *p){.  int
14655 20 69 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 4c 61   i;.  i = p->nLa
14656 62 65 6c 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28  bel++;.  assert(
14657 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f   p->magic==VDBE_
14658 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20  MAGIC_INIT );.  
14659 69 66 28 20 69 3e 3d 70 2d 3e 6e 4c 61 62 65 6c  if( i>=p->nLabel
1465a 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 70 2d 3e  Alloc ){.    p->
1465b 6e 4c 61 62 65 6c 41 6c 6c 6f 63 20 3d 20 70 2d  nLabelAlloc = p-
1465c 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63 2a 32 20 2b  >nLabelAlloc*2 +
1465d 20 31 30 3b 0a 20 20 20 20 70 2d 3e 61 4c 61 62   10;.    p->aLab
1465e 65 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65  el = sqlite3DbRe
1465f 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 2d 3e 64  allocOrFree(p->d
14660 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 2c 0a 20 20  b, p->aLabel,.  
14661 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14662 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14663 20 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63    p->nLabelAlloc
14664 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 4c 61 62 65  *sizeof(p->aLabe
14665 6c 5b 30 5d 29 29 3b 0a 20 20 7d 0a 20 20 69 66  l[0]));.  }.  if
14666 28 20 70 2d 3e 61 4c 61 62 65 6c 20 29 7b 0a 20  ( p->aLabel ){. 
14667 20 20 20 70 2d 3e 61 4c 61 62 65 6c 5b 69 5d 20     p->aLabel[i] 
14668 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75  = -1;.  }.  retu
14669 72 6e 20 2d 31 2d 69 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn -1-i;.}../*.*
1466a 2a 20 52 65 73 6f 6c 76 65 20 6c 61 62 65 6c 20  * Resolve label 
1466b 22 78 22 20 74 6f 20 62 65 20 74 68 65 20 61 64  "x" to be the ad
1466c 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e 65 78  dress of the nex
1466d 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f  t instruction to
1466e 0a 2a 2a 20 62 65 20 69 6e 73 65 72 74 65 64 2e  .** be inserted.
1466f 20 20 54 68 65 20 70 61 72 61 6d 65 74 65 72 20    The parameter 
14670 22 78 22 20 6d 75 73 74 20 68 61 76 65 20 62 65  "x" must have be
14671 65 6e 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  en obtained from
14672 0a 2a 2a 20 61 20 70 72 69 6f 72 20 63 61 6c 6c  .** a prior call
14673 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 65 4d   to sqlite3VdbeM
14674 61 6b 65 4c 61 62 65 6c 28 29 2e 0a 2a 2f 0a 53  akeLabel()..*/.S
14675 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
14676 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  id sqlite3VdbeRe
14677 73 6f 6c 76 65 4c 61 62 65 6c 28 56 64 62 65 20  solveLabel(Vdbe 
14678 2a 70 2c 20 69 6e 74 20 78 29 7b 0a 20 20 69 6e  *p, int x){.  in
14679 74 20 6a 20 3d 20 2d 31 2d 78 3b 0a 20 20 61 73  t j = -1-x;.  as
1467a 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d  sert( p->magic==
1467b 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20  VDBE_MAGIC_INIT 
1467c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6a 3e 3d  );.  assert( j>=
1467d 30 20 26 26 20 6a 3c 70 2d 3e 6e 4c 61 62 65 6c  0 && j<p->nLabel
1467e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 4c 61   );.  if( p->aLa
1467f 62 65 6c 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4c  bel ){.    p->aL
14680 61 62 65 6c 5b 6a 5d 20 3d 20 70 2d 3e 6e 4f 70  abel[j] = p->nOp
14681 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  ;.  }.}../*.** L
14682 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20  oop through the 
14683 70 72 6f 67 72 61 6d 20 6c 6f 6f 6b 69 6e 67 20  program looking 
14684 66 6f 72 20 50 32 20 76 61 6c 75 65 73 20 74 68  for P2 values th
14685 61 74 20 61 72 65 20 6e 65 67 61 74 69 76 65 0a  at are negative.
14686 2a 2a 20 6f 6e 20 6a 75 6d 70 20 69 6e 73 74 72  ** on jump instr
14687 75 63 74 69 6f 6e 73 2e 20 20 45 61 63 68 20 73  uctions.  Each s
14688 75 63 68 20 76 61 6c 75 65 20 69 73 20 61 20 6c  uch value is a l
14689 61 62 65 6c 2e 20 20 52 65 73 6f 6c 76 65 20 74  abel.  Resolve t
1468a 68 65 0a 2a 2a 20 6c 61 62 65 6c 20 62 79 20 73  he.** label by s
1468b 65 74 74 69 6e 67 20 74 68 65 20 50 32 20 76 61  etting the P2 va
1468c 6c 75 65 20 74 6f 20 69 74 73 20 63 6f 72 72 65  lue to its corre
1468d 63 74 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75  ct non-zero valu
1468e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  e..**.** This ro
1468f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
14690 6f 6e 63 65 20 61 66 74 65 72 20 61 6c 6c 20 6f  once after all o
14691 70 63 6f 64 65 73 20 68 61 76 65 20 62 65 65 6e  pcodes have been
14692 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2a 0a 2a 2a   inserted..**.**
14693 20 56 61 72 69 61 62 6c 65 20 2a 70 4d 61 78 46   Variable *pMaxF
14694 75 6e 63 41 72 67 73 20 69 73 20 73 65 74 20 74  uncArgs is set t
14695 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61  o the maximum va
14696 6c 75 65 20 6f 66 20 61 6e 79 20 50 32 20 61 72  lue of any P2 ar
14697 67 75 6d 65 6e 74 20 0a 2a 2a 20 74 6f 20 61 6e  gument .** to an
14698 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c 20 4f 50   OP_Function, OP
14699 5f 41 67 67 53 74 65 70 20 6f 72 20 4f 50 5f 56  _AggStep or OP_V
1469a 46 69 6c 74 65 72 20 6f 70 63 6f 64 65 2e 20 54  Filter opcode. T
1469b 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 0a  his is used by .
1469c 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61  ** sqlite3VdbeMa
1469d 6b 65 52 65 61 64 79 28 29 20 74 6f 20 73 69 7a  keReady() to siz
1469e 65 20 74 68 65 20 56 64 62 65 2e 61 70 41 72 67  e the Vdbe.apArg
1469f 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20  [] array..**.** 
146a0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 73  This routine als
146a1 6f 20 64 6f 65 73 20 74 68 65 20 66 6f 6c 6c 6f  o does the follo
146a2 77 69 6e 67 20 6f 70 74 69 6d 69 7a 61 74 69 6f  wing optimizatio
146a3 6e 3a 20 20 49 74 20 73 63 61 6e 73 20 66 6f 72  n:  It scans for
146a4 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 73  .** instructions
146a5 20 74 68 61 74 20 6d 69 67 68 74 20 63 61 75 73   that might caus
146a6 65 20 61 20 73 74 61 74 65 6d 65 6e 74 20 72 6f  e a statement ro
146a7 6c 6c 62 61 63 6b 2e 20 20 53 75 63 68 20 69 6e  llback.  Such in
146a8 73 74 72 75 63 74 69 6f 6e 73 0a 2a 2a 20 61 72  structions.** ar
146a9 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 4f 50  e:.**.**   *  OP
146aa 5f 48 61 6c 74 20 77 69 74 68 20 50 31 3d 53 51  _Halt with P1=SQ
146ab 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20  LITE_CONSTRAINT 
146ac 61 6e 64 20 50 32 3d 4f 45 5f 41 62 6f 72 74 2e  and P2=OE_Abort.
146ad 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 44 65 73 74  .**   *  OP_Dest
146ae 72 6f 79 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 56  roy.**   *  OP_V
146af 55 70 64 61 74 65 0a 2a 2a 20 20 20 2a 20 20 4f  Update.**   *  O
146b0 50 5f 56 52 65 6e 61 6d 65 0a 2a 2a 0a 2a 2a 20  P_VRename.**.** 
146b1 49 66 20 6e 6f 20 73 75 63 68 20 69 6e 73 74 72  If no such instr
146b2 75 63 74 69 6f 6e 20 69 73 20 66 6f 75 6e 64 2c  uction is found,
146b3 20 74 68 65 6e 20 65 76 65 72 79 20 53 74 61 74   then every Stat
146b4 65 6d 65 6e 74 20 69 6e 73 74 72 75 63 74 69 6f  ement instructio
146b5 6e 20 0a 2a 2a 20 69 73 20 63 68 61 6e 67 65 64  n .** is changed
146b6 20 74 6f 20 61 20 4e 6f 6f 70 2e 20 20 49 6e 20   to a Noop.  In 
146b7 74 68 69 73 20 77 61 79 2c 20 77 65 20 61 76 6f  this way, we avo
146b8 69 64 20 63 72 65 61 74 69 6e 67 20 74 68 65 20  id creating the 
146b9 73 74 61 74 65 6d 65 6e 74 20 0a 2a 2a 20 6a 6f  statement .** jo
146ba 75 72 6e 61 6c 20 66 69 6c 65 20 75 6e 6e 65 63  urnal file unnec
146bb 65 73 73 61 72 69 6c 79 2e 0a 2a 2f 0a 73 74 61  essarily..*/.sta
146bc 74 69 63 20 76 6f 69 64 20 72 65 73 6f 6c 76 65  tic void resolve
146bd 50 32 56 61 6c 75 65 73 28 56 64 62 65 20 2a 70  P2Values(Vdbe *p
146be 2c 20 69 6e 74 20 2a 70 4d 61 78 46 75 6e 63 41  , int *pMaxFuncA
146bf 72 67 73 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  rgs){.  int i;. 
146c0 20 69 6e 74 20 6e 4d 61 78 41 72 67 73 20 3d 20   int nMaxArgs = 
146c1 30 3b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20  0;.  Op *pOp;.  
146c2 69 6e 74 20 2a 61 4c 61 62 65 6c 20 3d 20 70 2d  int *aLabel = p-
146c3 3e 61 4c 61 62 65 6c 3b 0a 20 20 69 6e 74 20 64  >aLabel;.  int d
146c4 6f 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c  oesStatementRoll
146c5 62 61 63 6b 20 3d 20 30 3b 0a 20 20 69 6e 74 20  back = 0;.  int 
146c6 68 61 73 53 74 61 74 65 6d 65 6e 74 42 65 67 69  hasStatementBegi
146c7 6e 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 4f 70  n = 0;.  for(pOp
146c8 3d 70 2d 3e 61 4f 70 2c 20 69 3d 70 2d 3e 6e 4f  =p->aOp, i=p->nO
146c9 70 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20  p-1; i>=0; i--, 
146ca 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 75 38 20 6f  pOp++){.    u8 o
146cb 70 63 6f 64 65 20 3d 20 70 4f 70 2d 3e 6f 70 63  pcode = pOp->opc
146cc 6f 64 65 3b 0a 0a 20 20 20 20 69 66 28 20 6f 70  ode;..    if( op
146cd 63 6f 64 65 3d 3d 4f 50 5f 46 75 6e 63 74 69 6f  code==OP_Functio
146ce 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  n ){.      if( p
146cf 4f 70 2d 3e 70 35 3e 6e 4d 61 78 41 72 67 73 20  Op->p5>nMaxArgs 
146d0 29 20 6e 4d 61 78 41 72 67 73 20 3d 20 70 4f 70  ) nMaxArgs = pOp
146d1 2d 3e 70 35 3b 0a 20 20 20 20 7d 65 6c 73 65 20  ->p5;.    }else 
146d2 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41  if( opcode==OP_A
146d3 67 67 53 74 65 70 20 0a 23 69 66 6e 64 65 66 20  ggStep .#ifndef 
146d4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
146d5 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 20  UALTABLE.       
146d6 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56   || opcode==OP_V
146d7 55 70 64 61 74 65 0a 23 65 6e 64 69 66 0a 20 20  Update.#endif.  
146d8 20 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70    ){.      if( p
146d9 4f 70 2d 3e 70 32 3e 6e 4d 61 78 41 72 67 73 20  Op->p2>nMaxArgs 
146da 29 20 6e 4d 61 78 41 72 67 73 20 3d 20 70 4f 70  ) nMaxArgs = pOp
146db 2d 3e 70 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ->p2;.    }.    
146dc 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48  if( opcode==OP_H
146dd 61 6c 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28  alt ){.      if(
146de 20 70 4f 70 2d 3e 70 31 3d 3d 53 51 4c 49 54 45   pOp->p1==SQLITE
146df 5f 43 4f 4e 53 54 52 41 49 4e 54 20 26 26 20 70  _CONSTRAINT && p
146e0 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 41 62 6f 72 74  Op->p2==OE_Abort
146e1 20 29 7b 0a 20 20 20 20 20 20 20 20 64 6f 65 73   ){.        does
146e2 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 61 63  StatementRollbac
146e3 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  k = 1;.      }. 
146e4 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63     }else if( opc
146e5 6f 64 65 3d 3d 4f 50 5f 53 74 61 74 65 6d 65 6e  ode==OP_Statemen
146e6 74 20 29 7b 0a 20 20 20 20 20 20 68 61 73 53 74  t ){.      hasSt
146e7 61 74 65 6d 65 6e 74 42 65 67 69 6e 20 3d 20 31  atementBegin = 1
146e8 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
146e9 6f 70 63 6f 64 65 3d 3d 4f 50 5f 44 65 73 74 72  opcode==OP_Destr
146ea 6f 79 20 29 7b 0a 20 20 20 20 20 20 64 6f 65 73  oy ){.      does
146eb 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 61 63  StatementRollbac
146ec 6b 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20 53  k = 1;.#ifndef S
146ed 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
146ee 41 4c 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73  ALTABLE.    }els
146ef 65 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50  e if( opcode==OP
146f0 5f 56 55 70 64 61 74 65 20 7c 7c 20 6f 70 63 6f  _VUpdate || opco
146f1 64 65 3d 3d 4f 50 5f 56 52 65 6e 61 6d 65 20 29  de==OP_VRename )
146f2 7b 0a 20 20 20 20 20 20 64 6f 65 73 53 74 61 74  {.      doesStat
146f3 65 6d 65 6e 74 52 6f 6c 6c 62 61 63 6b 20 3d 20  ementRollback = 
146f4 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  1;.    }else if(
146f5 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 46 69 6c   opcode==OP_VFil
146f6 74 65 72 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  ter ){.      int
146f7 20 6e 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   n;.      assert
146f8 28 20 70 2d 3e 6e 4f 70 20 2d 20 69 20 3e 3d 20  ( p->nOp - i >= 
146f9 33 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  3 );.      asser
146fa 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64  t( pOp[-1].opcod
146fb 65 3d 3d 4f 50 5f 49 6e 74 65 67 65 72 20 29 3b  e==OP_Integer );
146fc 0a 20 20 20 20 20 20 6e 20 3d 20 70 4f 70 5b 2d  .      n = pOp[-
146fd 31 5d 2e 70 31 3b 0a 20 20 20 20 20 20 69 66 28  1].p1;.      if(
146fe 20 6e 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d   n>nMaxArgs ) nM
146ff 61 78 41 72 67 73 20 3d 20 6e 3b 0a 23 65 6e 64  axArgs = n;.#end
14700 69 66 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  if.    }..    if
14701 28 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 63  ( sqlite3VdbeOpc
14702 6f 64 65 48 61 73 50 72 6f 70 65 72 74 79 28 6f  odeHasProperty(o
14703 70 63 6f 64 65 2c 20 4f 50 46 4c 47 5f 4a 55 4d  pcode, OPFLG_JUM
14704 50 29 20 26 26 20 70 4f 70 2d 3e 70 32 3c 30 20  P) && pOp->p2<0 
14705 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
14706 20 2d 31 2d 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e   -1-pOp->p2<p->n
14707 4c 61 62 65 6c 20 29 3b 0a 20 20 20 20 20 20 70  Label );.      p
14708 4f 70 2d 3e 70 32 20 3d 20 61 4c 61 62 65 6c 5b  Op->p2 = aLabel[
14709 2d 31 2d 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20  -1-pOp->p2];.   
1470a 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
1470b 5f 66 72 65 65 28 70 2d 3e 61 4c 61 62 65 6c 29  _free(p->aLabel)
1470c 3b 0a 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20  ;.  p->aLabel = 
1470d 30 3b 0a 0a 20 20 2a 70 4d 61 78 46 75 6e 63 41  0;..  *pMaxFuncA
1470e 72 67 73 20 3d 20 6e 4d 61 78 41 72 67 73 3b 0a  rgs = nMaxArgs;.
1470f 0a 20 20 2f 2a 20 49 66 20 77 65 20 6e 65 76 65  .  /* If we neve
14710 72 20 72 6f 6c 6c 62 61 63 6b 20 61 20 73 74 61  r rollback a sta
14711 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
14712 6f 6e 2c 20 74 68 65 6e 20 73 74 61 74 65 6d 65  on, then stateme
14713 6e 74 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74  nt.  ** transact
14714 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 6e 65 65  ions are not nee
14715 64 65 64 2e 20 20 53 6f 20 63 68 61 6e 67 65 20  ded.  So change 
14716 65 76 65 72 79 20 4f 50 5f 53 74 61 74 65 6d 65  every OP_Stateme
14717 6e 74 0a 20 20 2a 2a 20 6f 70 63 6f 64 65 20 69  nt.  ** opcode i
14718 6e 74 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70 2e 20  nto an OP_Noop. 
14719 20 54 68 69 73 20 61 76 6f 69 64 20 61 20 63 61   This avoid a ca
1471a 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 4f  ll to sqlite3OsO
1471b 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 0a 20  penExclusive(). 
1471c 20 2a 2a 20 77 68 69 63 68 20 63 61 6e 20 62 65   ** which can be
1471d 20 65 78 70 65 6e 73 69 76 65 20 6f 6e 20 73 6f   expensive on so
1471e 6d 65 20 70 6c 61 74 66 6f 72 6d 73 2e 0a 20 20  me platforms..  
1471f 2a 2f 0a 20 20 69 66 28 20 68 61 73 53 74 61 74  */.  if( hasStat
14720 65 6d 65 6e 74 42 65 67 69 6e 20 26 26 20 21 64  ementBegin && !d
14721 6f 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c  oesStatementRoll
14722 62 61 63 6b 20 29 7b 0a 20 20 20 20 66 6f 72 28  back ){.    for(
14723 70 4f 70 3d 70 2d 3e 61 4f 70 2c 20 69 3d 70 2d  pOp=p->aOp, i=p-
14724 3e 6e 4f 70 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d  >nOp-1; i>=0; i-
14725 2d 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 20  -, pOp++){.     
14726 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
14727 3d 3d 4f 50 5f 53 74 61 74 65 6d 65 6e 74 20 29  ==OP_Statement )
14728 7b 0a 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f  {.        pOp->o
14729 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b  pcode = OP_Noop;
1472a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1472b 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75   }.}../*.** Retu
1472c 72 6e 20 74 68 65 20 61 64 64 72 65 73 73 20 6f  rn the address o
1472d 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72  f the next instr
1472e 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e 73  uction to be ins
1472f 65 72 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  erted..*/.SQLITE
14730 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
14731 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
14732 64 64 72 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  ddr(Vdbe *p){.  
14733 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63  assert( p->magic
14734 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49  ==VDBE_MAGIC_INI
14735 54 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d  T );.  return p-
14736 3e 6e 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  >nOp;.}../*.** A
14737 64 64 20 61 20 77 68 6f 6c 65 20 6c 69 73 74 20  dd a whole list 
14738 6f 66 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 6f  of operations to
14739 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 73   the operation s
1473a 74 61 63 6b 2e 20 20 52 65 74 75 72 6e 20 74 68  tack.  Return th
1473b 65 0a 2a 2a 20 61 64 64 72 65 73 73 20 6f 66 20  e.** address of 
1473c 74 68 65 20 66 69 72 73 74 20 6f 70 65 72 61 74  the first operat
1473d 69 6f 6e 20 61 64 64 65 64 2e 0a 2a 2f 0a 53 51  ion added..*/.SQ
1473e 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1473f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
14740 70 4c 69 73 74 28 56 64 62 65 20 2a 70 2c 20 69  pList(Vdbe *p, i
14741 6e 74 20 6e 4f 70 2c 20 56 64 62 65 4f 70 4c 69  nt nOp, VdbeOpLi
14742 73 74 20 63 6f 6e 73 74 20 2a 61 4f 70 29 7b 0a  st const *aOp){.
14743 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 61 73    int addr;.  as
14744 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d  sert( p->magic==
14745 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20  VDBE_MAGIC_INIT 
14746 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 20  );.  if( p->nOp 
14747 2b 20 6e 4f 70 20 3e 20 70 2d 3e 6e 4f 70 41 6c  + nOp > p->nOpAl
14748 6c 6f 63 20 29 7b 0a 20 20 20 20 72 65 73 69 7a  loc ){.    resiz
14749 65 4f 70 41 72 72 61 79 28 70 2c 20 70 2d 3e 6e  eOpArray(p, p->n
1474a 4f 70 41 6c 6c 6f 63 20 3f 20 70 2d 3e 6e 4f 70  OpAlloc ? p->nOp
1474b 41 6c 6c 6f 63 2a 32 20 3a 20 31 30 32 34 2f 73  Alloc*2 : 1024/s
1474c 69 7a 65 6f 66 28 4f 70 29 29 3b 0a 20 20 20 20  izeof(Op));.    
1474d 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 2b 6e  assert( p->nOp+n
1474e 4f 70 3c 3d 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20  Op<=p->nOpAlloc 
1474f 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  || p->db->malloc
14750 46 61 69 6c 65 64 20 29 3b 0a 20 20 7d 0a 20 20  Failed );.  }.  
14751 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  if( p->db->mallo
14752 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72  cFailed ){.    r
14753 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61  eturn 0;.  }.  a
14754 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20  ddr = p->nOp;.  
14755 69 66 28 20 6e 4f 70 3e 30 20 29 7b 0a 20 20 20  if( nOp>0 ){.   
14756 20 69 6e 74 20 69 3b 0a 20 20 20 20 56 64 62 65   int i;.    Vdbe
14757 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 70 49  OpList const *pI
14758 6e 20 3d 20 61 4f 70 3b 0a 20 20 20 20 66 6f 72  n = aOp;.    for
14759 28 69 3d 30 3b 20 69 3c 6e 4f 70 3b 20 69 2b 2b  (i=0; i<nOp; i++
1475a 2c 20 70 49 6e 2b 2b 29 7b 0a 20 20 20 20 20 20  , pIn++){.      
1475b 69 6e 74 20 70 32 20 3d 20 70 49 6e 2d 3e 70 32  int p2 = pIn->p2
1475c 3b 0a 20 20 20 20 20 20 56 64 62 65 4f 70 20 2a  ;.      VdbeOp *
1475d 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4f 70 5b 69  pOut = &p->aOp[i
1475e 2b 61 64 64 72 5d 3b 0a 20 20 20 20 20 20 70 4f  +addr];.      pO
1475f 75 74 2d 3e 6f 70 63 6f 64 65 20 3d 20 70 49 6e  ut->opcode = pIn
14760 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20 20 20  ->opcode;.      
14761 70 4f 75 74 2d 3e 70 31 20 3d 20 70 49 6e 2d 3e  pOut->p1 = pIn->
14762 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20 70 32  p1;.      if( p2
14763 3c 30 20 26 26 20 73 71 6c 69 74 65 33 56 64 62  <0 && sqlite3Vdb
14764 65 4f 70 63 6f 64 65 48 61 73 50 72 6f 70 65 72  eOpcodeHasProper
14765 74 79 28 70 4f 75 74 2d 3e 6f 70 63 6f 64 65 2c  ty(pOut->opcode,
14766 20 4f 50 46 4c 47 5f 4a 55 4d 50 29 20 29 7b 0a   OPFLG_JUMP) ){.
14767 20 20 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 32          pOut->p2
14768 20 3d 20 61 64 64 72 20 2b 20 41 44 44 52 28 70   = addr + ADDR(p
14769 32 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  2);.      }else{
1476a 0a 20 20 20 20 20 20 20 20 70 4f 75 74 2d 3e 70  .        pOut->p
1476b 32 20 3d 20 70 32 3b 0a 20 20 20 20 20 20 7d 0a  2 = p2;.      }.
1476c 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 33 20 3d        pOut->p3 =
1476d 20 70 49 6e 2d 3e 70 33 3b 0a 20 20 20 20 20 20   pIn->p3;.      
1476e 70 4f 75 74 2d 3e 70 34 74 79 70 65 20 3d 20 50  pOut->p4type = P
1476f 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 20 20 20  4_NOTUSED;.     
14770 20 70 4f 75 74 2d 3e 70 34 2e 70 20 3d 20 30 3b   pOut->p4.p = 0;
14771 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 35 20  .      pOut->p5 
14772 3d 20 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  = 0;.#ifdef SQLI
14773 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 70  TE_DEBUG.      p
14774 4f 75 74 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3d 20  Out->zComment = 
14775 30 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  0;.      if( sql
14776 69 74 65 33 56 64 62 65 41 64 64 6f 70 54 72 61  ite3VdbeAddopTra
14777 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ce ){.        sq
14778 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70  lite3VdbePrintOp
14779 28 30 2c 20 69 2b 61 64 64 72 2c 20 26 70 2d 3e  (0, i+addr, &p->
1477a 61 4f 70 5b 69 2b 61 64 64 72 5d 29 3b 0a 20 20  aOp[i+addr]);.  
1477b 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
1477c 20 7d 0a 20 20 20 20 70 2d 3e 6e 4f 70 20 2b 3d   }.    p->nOp +=
1477d 20 6e 4f 70 3b 0a 20 20 7d 0a 20 20 72 65 74 75   nOp;.  }.  retu
1477e 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn addr;.}../*.*
1477f 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c  * Change the val
14780 75 65 20 6f 66 20 74 68 65 20 50 31 20 6f 70 65  ue of the P1 ope
14781 72 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69  rand for a speci
14782 66 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  fic instruction.
14783 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
14784 20 69 73 20 75 73 65 66 75 6c 20 77 68 65 6e 20   is useful when 
14785 61 20 6c 61 72 67 65 20 70 72 6f 67 72 61 6d 20  a large program 
14786 69 73 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20 61  is loaded from a
14787 0a 2a 2a 20 73 74 61 74 69 63 20 61 72 72 61 79  .** static array
14788 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 56 64   using sqlite3Vd
14789 62 65 41 64 64 4f 70 4c 69 73 74 20 62 75 74 20  beAddOpList but 
1478a 77 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20  we want to make 
1478b 61 0a 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20 63  a.** few minor c
1478c 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 70 72  hanges to the pr
1478d 6f 67 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ogram..*/.SQLITE
1478e 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1478f 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
14790 31 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61  1(Vdbe *p, int a
14791 64 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20  ddr, int val){. 
14792 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c   assert( p==0 ||
14793 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f   p->magic==VDBE_
14794 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20  MAGIC_INIT );.  
14795 69 66 28 20 70 20 26 26 20 61 64 64 72 3e 3d 30  if( p && addr>=0
14796 20 26 26 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 20   && p->nOp>addr 
14797 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20  && p->aOp ){.   
14798 20 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 31   p->aOp[addr].p1
14799 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f   = val;.  }.}../
1479a 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
1479b 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 32 20  value of the P2 
1479c 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 20 73 70  operand for a sp
1479d 65 63 69 66 69 63 20 69 6e 73 74 72 75 63 74 69  ecific instructi
1479e 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  on..** This rout
1479f 69 6e 65 20 69 73 20 75 73 65 66 75 6c 20 66 6f  ine is useful fo
147a0 72 20 73 65 74 74 69 6e 67 20 61 20 6a 75 6d 70  r setting a jump
147a1 20 64 65 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f   destination..*/
147a2 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
147a3 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
147a4 43 68 61 6e 67 65 50 32 28 56 64 62 65 20 2a 70  ChangeP2(Vdbe *p
147a5 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20  , int addr, int 
147a6 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20  val){.  assert( 
147a7 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6d 61 67 69 63  p==0 || p->magic
147a8 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49  ==VDBE_MAGIC_INI
147a9 54 20 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20  T );.  if( p && 
147aa 61 64 64 72 3e 3d 30 20 26 26 20 70 2d 3e 6e 4f  addr>=0 && p->nO
147ab 70 3e 61 64 64 72 20 26 26 20 70 2d 3e 61 4f 70  p>addr && p->aOp
147ac 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 61   ){.    p->aOp[a
147ad 64 64 72 5d 2e 70 32 20 3d 20 76 61 6c 3b 0a 20  ddr].p2 = val;. 
147ae 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e   }.}../*.** Chan
147af 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ge the value of 
147b0 74 68 65 20 50 33 20 6f 70 65 72 61 6e 64 20 66  the P3 operand f
147b1 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 69 6e  or a specific in
147b2 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51  struction..*/.SQ
147b3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
147b4 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61  d sqlite3VdbeCha
147b5 6e 67 65 50 33 28 56 64 62 65 20 2a 70 2c 20 69  ngeP3(Vdbe *p, i
147b6 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 76 61 6c  nt addr, int val
147b7 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d  ){.  assert( p==
147b8 30 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56  0 || p->magic==V
147b9 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29  DBE_MAGIC_INIT )
147ba 3b 0a 20 20 69 66 28 20 70 20 26 26 20 61 64 64  ;.  if( p && add
147bb 72 3e 3d 30 20 26 26 20 70 2d 3e 6e 4f 70 3e 61  r>=0 && p->nOp>a
147bc 64 64 72 20 26 26 20 70 2d 3e 61 4f 70 20 29 7b  ddr && p->aOp ){
147bd 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 61 64 64 72  .    p->aOp[addr
147be 5d 2e 70 33 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a  ].p3 = val;.  }.
147bf 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  }../*.** Change 
147c0 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
147c1 20 50 35 20 6f 70 65 72 61 6e 64 20 66 6f 72 20   P5 operand for 
147c2 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  the most recentl
147c3 79 0a 2a 2a 20 61 64 64 65 64 20 6f 70 65 72 61  y.** added opera
147c4 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
147c5 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
147c6 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35  ite3VdbeChangeP5
147c7 28 56 64 62 65 20 2a 70 2c 20 75 38 20 76 61 6c  (Vdbe *p, u8 val
147c8 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d  ){.  assert( p==
147c9 30 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56  0 || p->magic==V
147ca 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29  DBE_MAGIC_INIT )
147cb 3b 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e  ;.  if( p && p->
147cc 61 4f 70 20 29 7b 0a 20 20 20 20 61 73 73 65 72  aOp ){.    asser
147cd 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 20  t( p->nOp>0 );. 
147ce 20 20 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70     p->aOp[p->nOp
147cf 2d 31 5d 2e 70 35 20 3d 20 76 61 6c 3b 0a 20 20  -1].p5 = val;.  
147d0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  }.}../*.** Chang
147d1 65 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e 64  e the P2 operand
147d2 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 20   of instruction 
147d3 61 64 64 72 20 73 6f 20 74 68 61 74 20 69 74 20  addr so that it 
147d4 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 65  points to.** the
147d5 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   address of the 
147d6 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  next instruction
147d7 20 74 6f 20 62 65 20 63 6f 64 65 64 2e 0a 2a 2f   to be coded..*/
147d8 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
147d9 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
147da 4a 75 6d 70 48 65 72 65 28 56 64 62 65 20 2a 70  JumpHere(Vdbe *p
147db 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a 20 20 73  , int addr){.  s
147dc 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
147dd 50 32 28 70 2c 20 61 64 64 72 2c 20 70 2d 3e 6e  P2(p, addr, p->n
147de 4f 70 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49  Op);.}.../*.** I
147df 66 20 74 68 65 20 69 6e 70 75 74 20 46 75 6e 63  f the input Func
147e0 44 65 66 20 73 74 72 75 63 74 75 72 65 20 69 73  Def structure is
147e1 20 65 70 68 65 6d 65 72 61 6c 2c 20 74 68 65 6e   ephemeral, then
147e2 20 66 72 65 65 20 69 74 2e 20 20 49 66 0a 2a 2a   free it.  If.**
147e3 20 74 68 65 20 46 75 6e 63 44 65 66 20 69 73 20   the FuncDef is 
147e4 6e 6f 74 20 65 70 68 65 72 6d 61 6c 2c 20 74 68  not ephermal, th
147e5 65 6e 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 0a 2a  en do nothing..*
147e6 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72  /.static void fr
147e7 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 74  eeEphemeralFunct
147e8 69 6f 6e 28 46 75 6e 63 44 65 66 20 2a 70 44 65  ion(FuncDef *pDe
147e9 66 29 7b 0a 20 20 69 66 28 20 70 44 65 66 20 26  f){.  if( pDef &
147ea 26 20 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26  & (pDef->flags &
147eb 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48   SQLITE_FUNC_EPH
147ec 45 4d 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71  EM)!=0 ){.    sq
147ed 6c 69 74 65 33 5f 66 72 65 65 28 70 44 65 66 29  lite3_free(pDef)
147ee 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  ;.  }.}../*.** D
147ef 65 6c 65 74 65 20 61 20 50 34 20 76 61 6c 75 65  elete a P4 value
147f0 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a   if necessary..*
147f1 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72  /.static void fr
147f2 65 65 50 34 28 69 6e 74 20 70 34 74 79 70 65 2c  eeP4(int p4type,
147f3 20 76 6f 69 64 20 2a 70 33 29 7b 0a 20 20 69 66   void *p3){.  if
147f4 28 20 70 33 20 29 7b 0a 20 20 20 20 73 77 69 74  ( p3 ){.    swit
147f5 63 68 28 20 70 34 74 79 70 65 20 29 7b 0a 20 20  ch( p4type ){.  
147f6 20 20 20 20 63 61 73 65 20 50 34 5f 52 45 41 4c      case P4_REAL
147f7 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f  :.      case P4_
147f8 49 4e 54 36 34 3a 0a 20 20 20 20 20 20 63 61 73  INT64:.      cas
147f9 65 20 50 34 5f 4d 50 52 49 4e 54 46 3a 0a 20 20  e P4_MPRINTF:.  
147fa 20 20 20 20 63 61 73 65 20 50 34 5f 44 59 4e 41      case P4_DYNA
147fb 4d 49 43 3a 0a 20 20 20 20 20 20 63 61 73 65 20  MIC:.      case 
147fc 50 34 5f 4b 45 59 49 4e 46 4f 3a 0a 20 20 20 20  P4_KEYINFO:.    
147fd 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46    case P4_KEYINF
147fe 4f 5f 48 41 4e 44 4f 46 46 3a 20 7b 0a 20 20 20  O_HANDOFF: {.   
147ff 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
14800 65 28 70 33 29 3b 0a 20 20 20 20 20 20 20 20 62  e(p3);.        b
14801 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
14802 20 20 20 20 63 61 73 65 20 50 34 5f 56 44 42 45      case P4_VDBE
14803 46 55 4e 43 3a 20 7b 0a 20 20 20 20 20 20 20 20  FUNC: {.        
14804 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46  VdbeFunc *pVdbeF
14805 75 6e 63 20 3d 20 28 56 64 62 65 46 75 6e 63 20  unc = (VdbeFunc 
14806 2a 29 70 33 3b 0a 20 20 20 20 20 20 20 20 66 72  *)p3;.        fr
14807 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 74  eeEphemeralFunct
14808 69 6f 6e 28 70 56 64 62 65 46 75 6e 63 2d 3e 70  ion(pVdbeFunc->p
14809 46 75 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 73  Func);.        s
1480a 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65  qlite3VdbeDelete
1480b 41 75 78 44 61 74 61 28 70 56 64 62 65 46 75 6e  AuxData(pVdbeFun
1480c 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73  c, 0);.        s
1480d 71 6c 69 74 65 33 5f 66 72 65 65 28 70 56 64 62  qlite3_free(pVdb
1480e 65 46 75 6e 63 29 3b 0a 20 20 20 20 20 20 20 20  eFunc);.        
1480f 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
14810 20 20 20 20 20 63 61 73 65 20 50 34 5f 46 55 4e       case P4_FUN
14811 43 44 45 46 3a 20 7b 0a 20 20 20 20 20 20 20 20  CDEF: {.        
14812 66 72 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e  freeEphemeralFun
14813 63 74 69 6f 6e 28 28 46 75 6e 63 44 65 66 2a 29  ction((FuncDef*)
14814 70 33 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  p3);.        bre
14815 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
14816 20 20 63 61 73 65 20 50 34 5f 4d 45 4d 3a 20 7b    case P4_MEM: {
14817 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
14818 56 61 6c 75 65 46 72 65 65 28 28 73 71 6c 69 74  ValueFree((sqlit
14819 65 33 5f 76 61 6c 75 65 2a 29 70 33 29 3b 0a 20  e3_value*)p3);. 
1481a 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1481b 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1481c 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  }.../*.** Change
1481d 20 4e 20 6f 70 63 6f 64 65 73 20 73 74 61 72 74   N opcodes start
1481e 69 6e 67 20 61 74 20 61 64 64 72 20 74 6f 20 4e  ing at addr to N
1481f 6f 2d 6f 70 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  o-ops..*/.SQLITE
14820 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
14821 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54  lite3VdbeChangeT
14822 6f 4e 6f 6f 70 28 56 64 62 65 20 2a 70 2c 20 69  oNoop(Vdbe *p, i
14823 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 4e 29 7b  nt addr, int N){
14824 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 61  .  if( p && p->a
14825 4f 70 20 29 7b 0a 20 20 20 20 56 64 62 65 4f 70  Op ){.    VdbeOp
14826 20 2a 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b   *pOp = &p->aOp[
14827 61 64 64 72 5d 3b 0a 20 20 20 20 77 68 69 6c 65  addr];.    while
14828 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 20 20 66  ( N-- ){.      f
14829 72 65 65 50 34 28 70 4f 70 2d 3e 70 34 74 79 70  reeP4(pOp->p4typ
1482a 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20  e, pOp->p4.p);. 
1482b 20 20 20 20 20 6d 65 6d 73 65 74 28 70 4f 70 2c       memset(pOp,
1482c 20 30 2c 20 73 69 7a 65 6f 66 28 70 4f 70 5b 30   0, sizeof(pOp[0
1482d 5d 29 29 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e  ]));.      pOp->
1482e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f 70  opcode = OP_Noop
1482f 3b 0a 20 20 20 20 20 20 70 4f 70 2b 2b 3b 0a 20  ;.      pOp++;. 
14830 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
14831 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c  * Change the val
14832 75 65 20 6f 66 20 74 68 65 20 50 34 20 6f 70 65  ue of the P4 ope
14833 72 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69  rand for a speci
14834 66 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  fic instruction.
14835 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
14836 20 69 73 20 75 73 65 66 75 6c 20 77 68 65 6e 20   is useful when 
14837 61 20 6c 61 72 67 65 20 70 72 6f 67 72 61 6d 20  a large program 
14838 69 73 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20 61  is loaded from a
14839 0a 2a 2a 20 73 74 61 74 69 63 20 61 72 72 61 79  .** static array
1483a 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 56 64   using sqlite3Vd
1483b 62 65 41 64 64 4f 70 4c 69 73 74 20 62 75 74 20  beAddOpList but 
1483c 77 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20  we want to make 
1483d 61 0a 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20 63  a.** few minor c
1483e 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 70 72  hanges to the pr
1483f 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ogram..**.** If 
14840 6e 3e 3d 30 20 74 68 65 6e 20 74 68 65 20 50 34  n>=0 then the P4
14841 20 6f 70 65 72 61 6e 64 20 69 73 20 64 79 6e 61   operand is dyna
14842 6d 69 63 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61  mic, meaning tha
14843 74 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74  t a copy of.** t
14844 68 65 20 73 74 72 69 6e 67 20 69 73 20 6d 61 64  he string is mad
14845 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62  e into memory ob
14846 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
14847 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a  te3_malloc()..**
14848 20 41 20 76 61 6c 75 65 20 6f 66 20 6e 3d 3d 30   A value of n==0
14849 20 6d 65 61 6e 73 20 63 6f 70 79 20 62 79 74 65   means copy byte
1484a 73 20 6f 66 20 7a 50 34 20 75 70 20 74 6f 20 61  s of zP4 up to a
1484b 6e 64 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65  nd including the
1484c 0a 2a 2a 20 66 69 72 73 74 20 6e 75 6c 6c 20 62  .** first null b
1484d 79 74 65 2e 20 20 49 66 20 6e 3e 30 20 74 68 65  yte.  If n>0 the
1484e 6e 20 63 6f 70 79 20 6e 2b 31 20 62 79 74 65 73  n copy n+1 bytes
1484f 20 6f 66 20 7a 50 34 2e 0a 2a 2a 0a 2a 2a 20 49   of zP4..**.** I
14850 66 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20  f n==P4_KEYINFO 
14851 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 7a 50  it means that zP
14852 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  4 is a pointer t
14853 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  o a KeyInfo stru
14854 63 74 75 72 65 2e 0a 2a 2a 20 41 20 63 6f 70 79  cture..** A copy
14855 20 69 73 20 6d 61 64 65 20 6f 66 20 74 68 65 20   is made of the 
14856 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
14857 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62  e into memory ob
14858 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73  tained from.** s
14859 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2c 20 74  qlite3_malloc, t
1485a 6f 20 62 65 20 66 72 65 65 64 20 77 68 65 6e 20  o be freed when 
1485b 74 68 65 20 56 64 62 65 20 69 73 20 66 69 6e 61  the Vdbe is fina
1485c 6c 69 7a 65 64 2e 0a 2a 2a 20 6e 3d 3d 50 34 5f  lized..** n==P4_
1485d 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20  KEYINFO_HANDOFF 
1485e 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 7a  indicates that z
1485f 50 34 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 4b  P4 points to a K
14860 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  eyInfo structure
14861 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 6d 65  .** stored in me
14862 6d 6f 72 79 20 74 68 61 74 20 74 68 65 20 63 61  mory that the ca
14863 6c 6c 65 72 20 68 61 73 20 6f 62 74 61 69 6e 65  ller has obtaine
14864 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d  d from sqlite3_m
14865 61 6c 6c 6f 63 2e 20 54 68 65 20 0a 2a 2a 20 63  alloc. The .** c
14866 61 6c 6c 65 72 20 73 68 6f 75 6c 64 20 6e 6f 74  aller should not
14867 20 66 72 65 65 20 74 68 65 20 61 6c 6c 6f 63 61   free the alloca
14868 74 69 6f 6e 2c 20 69 74 20 77 69 6c 6c 20 62 65  tion, it will be
14869 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20   freed when the 
1486a 56 64 62 65 20 69 73 0a 2a 2a 20 66 69 6e 61 6c  Vdbe is.** final
1486b 69 7a 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 4f 74 68  ized..** .** Oth
1486c 65 72 20 76 61 6c 75 65 73 20 6f 66 20 6e 20 28  er values of n (
1486d 50 34 5f 53 54 41 54 49 43 2c 20 50 34 5f 43 4f  P4_STATIC, P4_CO
1486e 4c 4c 53 45 51 20 65 74 63 2e 29 20 69 6e 64 69  LLSEQ etc.) indi
1486f 63 61 74 65 20 74 68 61 74 20 7a 50 34 20 70 6f  cate that zP4 po
14870 69 6e 74 73 0a 2a 2a 20 74 6f 20 61 20 73 74 72  ints.** to a str
14871 69 6e 67 20 6f 72 20 73 74 72 75 63 74 75 72 65  ing or structure
14872 20 74 68 61 74 20 69 73 20 67 75 61 72 61 6e 74   that is guarant
14873 65 65 64 20 74 6f 20 65 78 69 73 74 20 66 6f 72  eed to exist for
14874 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66   the lifetime of
14875 0a 2a 2a 20 74 68 65 20 56 64 62 65 2e 20 49 6e  .** the Vdbe. In
14876 20 74 68 65 73 65 20 63 61 73 65 73 20 77 65 20   these cases we 
14877 63 61 6e 20 6a 75 73 74 20 63 6f 70 79 20 74 68  can just copy th
14878 65 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a  e pointer..**.**
14879 20 49 66 20 61 64 64 72 3c 30 20 74 68 65 6e 20   If addr<0 then 
1487a 63 68 61 6e 67 65 20 50 34 20 6f 6e 20 74 68 65  change P4 on the
1487b 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69   most recently i
1487c 6e 73 65 72 74 65 64 20 69 6e 73 74 72 75 63 74  nserted instruct
1487d 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
1487e 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1487f 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
14880 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64  Vdbe *p, int add
14881 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  r, const char *z
14882 50 34 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 4f 70  P4, int n){.  Op
14883 20 2a 70 4f 70 3b 0a 20 20 61 73 73 65 72 74 28   *pOp;.  assert(
14884 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72   p!=0 );.  asser
14885 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42  t( p->magic==VDB
14886 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a  E_MAGIC_INIT );.
14887 20 20 69 66 28 20 70 2d 3e 61 4f 70 3d 3d 30 20    if( p->aOp==0 
14888 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  || p->db->malloc
14889 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 69 66  Failed ){.    if
1488a 20 28 6e 20 21 3d 20 50 34 5f 4b 45 59 49 4e 46   (n != P4_KEYINF
1488b 4f 29 20 7b 0a 20 20 20 20 20 20 66 72 65 65 50  O) {.      freeP
1488c 34 28 6e 2c 20 28 76 6f 69 64 2a 29 2a 28 63 68  4(n, (void*)*(ch
1488d 61 72 2a 2a 29 26 7a 50 34 29 3b 0a 20 20 20 20  ar**)&zP4);.    
1488e 7d 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  }.    return;.  
1488f 7d 0a 20 20 61 73 73 65 72 74 28 20 61 64 64 72  }.  assert( addr
14890 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 69 66 28  <p->nOp );.  if(
14891 20 61 64 64 72 3c 30 20 29 7b 0a 20 20 20 20 61   addr<0 ){.    a
14892 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 20 2d 20 31  ddr = p->nOp - 1
14893 3b 0a 20 20 20 20 69 66 28 20 61 64 64 72 3c 30  ;.    if( addr<0
14894 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20   ) return;.  }. 
14895 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 61   pOp = &p->aOp[a
14896 64 64 72 5d 3b 0a 20 20 66 72 65 65 50 34 28 70  ddr];.  freeP4(p
14897 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70 2d  Op->p4type, pOp-
14898 3e 70 34 2e 70 29 3b 0a 20 20 70 4f 70 2d 3e 70  >p4.p);.  pOp->p
14899 34 2e 70 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e  4.p = 0;.  if( n
1489a 3d 3d 50 34 5f 49 4e 54 33 32 20 29 7b 0a 20 20  ==P4_INT32 ){.  
1489b 20 20 2f 2a 20 4e 6f 74 65 3a 20 74 68 69 73 20    /* Note: this 
1489c 63 61 73 74 20 69 73 20 73 61 66 65 2c 20 62 65  cast is safe, be
1489d 63 61 75 73 65 20 74 68 65 20 6f 72 69 67 69 6e  cause the origin
1489e 20 64 61 74 61 20 70 6f 69 6e 74 20 77 61 73 20   data point was 
1489f 61 6e 20 69 6e 74 0a 20 20 20 20 2a 2a 20 74 68  an int.    ** th
148a0 61 74 20 77 61 73 20 63 61 73 74 20 74 6f 20 61  at was cast to a
148a1 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 2e   (const char *).
148a2 20 2a 2f 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e   */.    pOp->p4.
148a3 69 20 3d 20 28 69 6e 74 29 7a 50 34 3b 0a 20 20  i = (int)zP4;.  
148a4 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20    pOp->p4type = 
148a5 6e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 7a  n;.  }else if( z
148a6 50 34 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4f 70  P4==0 ){.    pOp
148a7 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20  ->p4.p = 0;.    
148a8 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34  pOp->p4type = P4
148a9 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 7d 65 6c 73  _NOTUSED;.  }els
148aa 65 20 69 66 28 20 6e 3d 3d 50 34 5f 4b 45 59 49  e if( n==P4_KEYI
148ab 4e 46 4f 20 29 7b 0a 20 20 20 20 4b 65 79 49 6e  NFO ){.    KeyIn
148ac 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  fo *pKeyInfo;.  
148ad 20 20 69 6e 74 20 6e 46 69 65 6c 64 2c 20 6e 42    int nField, nB
148ae 79 74 65 3b 0a 0a 20 20 20 20 6e 46 69 65 6c 64  yte;..    nField
148af 20 3d 20 28 28 4b 65 79 49 6e 66 6f 2a 29 7a 50   = ((KeyInfo*)zP
148b0 34 29 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 20 20  4)->nField;.    
148b1 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a  nByte = sizeof(*
148b2 70 4b 65 79 49 6e 66 6f 29 20 2b 20 28 6e 46 69  pKeyInfo) + (nFi
148b3 65 6c 64 2d 31 29 2a 73 69 7a 65 6f 66 28 70 4b  eld-1)*sizeof(pK
148b4 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 30 5d  eyInfo->aColl[0]
148b5 29 20 2b 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20  ) + nField;.    
148b6 70 4b 65 79 49 6e 66 6f 20 3d 20 73 71 6c 69 74  pKeyInfo = sqlit
148b7 65 33 5f 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65  e3_malloc( nByte
148b8 20 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e   );.    pOp->p4.
148b9 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79 49  pKeyInfo = pKeyI
148ba 6e 66 6f 3b 0a 20 20 20 20 69 66 28 20 70 4b 65  nfo;.    if( pKe
148bb 79 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 6d  yInfo ){.      m
148bc 65 6d 63 70 79 28 70 4b 65 79 49 6e 66 6f 2c 20  emcpy(pKeyInfo, 
148bd 7a 50 34 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20  zP4, nByte);.   
148be 20 20 20 2f 2a 20 49 6e 20 74 68 65 20 63 75 72     /* In the cur
148bf 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74  rent implementat
148c0 69 6f 6e 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 20  ion, P4_KEYINFO 
148c1 69 73 20 6f 6e 6c 79 20 65 76 65 72 20 75 73 65  is only ever use
148c2 64 20 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 4b 65  d on.      ** Ke
148c3 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 73  yInfo structures
148c4 20 74 68 61 74 20 68 61 76 65 20 6e 6f 20 61 53   that have no aS
148c5 6f 72 74 4f 72 64 65 72 20 63 6f 6d 70 6f 6e 65  ortOrder compone
148c6 6e 74 2e 20 20 45 6c 65 6d 65 6e 74 73 0a 20 20  nt.  Elements.  
148c7 20 20 20 20 2a 2a 20 77 69 74 68 20 61 6e 20 61      ** with an a
148c8 53 6f 72 74 4f 72 64 65 72 20 61 6c 77 61 79 73  SortOrder always
148c9 20 75 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f   use P4_KEYINFO_
148ca 48 41 4e 44 4f 46 46 2e 20 20 53 6f 20 77 65 20  HANDOFF.  So we 
148cb 64 6f 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20  do not.      ** 
148cc 6e 65 65 64 20 74 6f 20 62 6f 74 68 65 72 20 77  need to bother w
148cd 69 74 68 20 64 75 70 6c 69 63 61 74 69 6e 67 20  ith duplicating 
148ce 74 68 65 20 61 53 6f 72 74 4f 72 64 65 72 2e 20  the aSortOrder. 
148cf 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
148d0 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74   pKeyInfo->aSort
148d1 4f 72 64 65 72 3d 3d 30 20 29 3b 0a 23 69 66 20  Order==0 );.#if 
148d2 30 0a 20 20 20 20 20 20 61 53 6f 72 74 4f 72 64  0.      aSortOrd
148d3 65 72 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61  er = pKeyInfo->a
148d4 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 20  SortOrder;.     
148d5 20 69 66 28 20 61 53 6f 72 74 4f 72 64 65 72 20   if( aSortOrder 
148d6 29 7b 0a 20 20 20 20 20 20 20 20 70 4b 65 79 49  ){.        pKeyI
148d7 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20  nfo->aSortOrder 
148d8 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
148d9 2a 29 26 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f  *)&pKeyInfo->aCo
148da 6c 6c 5b 6e 46 69 65 6c 64 5d 3b 0a 20 20 20 20  ll[nField];.    
148db 20 20 20 20 6d 65 6d 63 70 79 28 70 4b 65 79 49      memcpy(pKeyI
148dc 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 2c  nfo->aSortOrder,
148dd 20 61 53 6f 72 74 4f 72 64 65 72 2c 20 6e 46 69   aSortOrder, nFi
148de 65 6c 64 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65  eld);.      }.#e
148df 6e 64 69 66 0a 20 20 20 20 20 20 70 4f 70 2d 3e  ndif.      pOp->
148e0 70 34 74 79 70 65 20 3d 20 50 34 5f 4b 45 59 49  p4type = P4_KEYI
148e1 4e 46 4f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  NFO;.    }else{.
148e2 20 20 20 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c        p->db->mal
148e3 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
148e4 20 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65       pOp->p4type
148e5 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20   = P4_NOTUSED;. 
148e6 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
148e7 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f 48   n==P4_KEYINFO_H
148e8 41 4e 44 4f 46 46 20 29 7b 0a 20 20 20 20 70 4f  ANDOFF ){.    pO
148e9 70 2d 3e 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a  p->p4.p = (void*
148ea 29 7a 50 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70  )zP4;.    pOp->p
148eb 34 74 79 70 65 20 3d 20 50 34 5f 4b 45 59 49 4e  4type = P4_KEYIN
148ec 46 4f 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  FO;.  }else if( 
148ed 6e 3c 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e  n<0 ){.    pOp->
148ee 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a 50  p4.p = (void*)zP
148ef 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79  4;.    pOp->p4ty
148f0 70 65 20 3d 20 6e 3b 0a 20 20 7d 65 6c 73 65 7b  pe = n;.  }else{
148f1 0a 20 20 20 20 69 66 28 20 6e 3d 3d 30 20 29 20  .    if( n==0 ) 
148f2 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 50 34 29 3b  n = strlen(zP4);
148f3 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d  .    pOp->p4.z =
148f4 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75   sqlite3DbStrNDu
148f5 70 28 70 2d 3e 64 62 2c 20 7a 50 34 2c 20 6e 29  p(p->db, zP4, n)
148f6 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70  ;.    pOp->p4typ
148f7 65 20 3d 20 50 34 5f 44 59 4e 41 4d 49 43 3b 0a  e = P4_DYNAMIC;.
148f8 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e    }.}..#ifndef N
148f9 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  DEBUG./*.** Chan
148fa 67 65 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 6f  ge the comment o
148fb 6e 20 74 68 65 20 74 68 65 20 6d 6f 73 74 20 72  n the the most r
148fc 65 63 65 6e 74 6c 79 20 63 6f 64 65 64 20 69 6e  ecently coded in
148fd 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51  struction..*/.SQ
148fe 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
148ff 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 6d  d sqlite3VdbeCom
14900 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 63 6f  ment(Vdbe *p, co
14901 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61  nst char *zForma
14902 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69  t, ...){.  va_li
14903 73 74 20 61 70 3b 0a 20 20 61 73 73 65 72 74 28  st ap;.  assert(
14904 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c 20 70 2d 3e   p->nOp>0 || p->
14905 61 4f 70 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  aOp==0 );.  asse
14906 72 74 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c  rt( p->aOp==0 ||
14907 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31   p->aOp[p->nOp-1
14908 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d 30 20 7c 7c  ].zComment==0 ||
14909 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   p->db->mallocFa
1490a 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70 2d  iled );.  if( p-
1490b 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 63 68 61 72  >nOp ){.    char
1490c 20 2a 2a 70 7a 20 3d 20 26 70 2d 3e 61 4f 70 5b   **pz = &p->aOp[
1490d 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65  p->nOp-1].zComme
1490e 6e 74 3b 0a 20 20 20 20 76 61 5f 73 74 61 72 74  nt;.    va_start
1490f 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20  (ap, zFormat);. 
14910 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
14911 2a 70 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 3d 20  *pz);.    *pz = 
14912 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28  sqlite3VMPrintf(
14913 70 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20  p->db, zFormat, 
14914 61 70 29 3b 0a 20 20 20 20 76 61 5f 65 6e 64 28  ap);.    va_end(
14915 61 70 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69  ap);.  }.}.#endi
14916 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  f../*.** Return 
14917 74 68 65 20 6f 70 63 6f 64 65 20 66 6f 72 20 61  the opcode for a
14918 20 67 69 76 65 6e 20 61 64 64 72 65 73 73 2e 0a   given address..
14919 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1491a 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65  E VdbeOp *sqlite
1491b 33 56 64 62 65 47 65 74 4f 70 28 56 64 62 65 20  3VdbeGetOp(Vdbe 
1491c 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a 20  *p, int addr){. 
1491d 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69   assert( p->magi
1491e 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e  c==VDBE_MAGIC_IN
1491f 49 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  IT );.  assert( 
14920 28 61 64 64 72 3e 3d 30 20 26 26 20 61 64 64 72  (addr>=0 && addr
14921 3c 70 2d 3e 6e 4f 70 29 20 7c 7c 20 70 2d 3e 64  <p->nOp) || p->d
14922 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
14923 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 61 64  );.  return ((ad
14924 64 72 3e 3d 30 20 26 26 20 61 64 64 72 3c 70 2d  dr>=0 && addr<p-
14925 3e 6e 4f 70 29 3f 28 26 70 2d 3e 61 4f 70 5b 61  >nOp)?(&p->aOp[a
14926 64 64 72 5d 29 3a 30 29 3b 0a 7d 0a 0a 23 69 66  ddr]):0);.}..#if
14927 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
14928 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20 7c  _OMIT_EXPLAIN) |
14929 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55  | !defined(NDEBU
1492a 47 29 20 5c 0a 20 20 20 20 20 7c 7c 20 64 65 66  G) \.     || def
1492b 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c  ined(VDBE_PROFIL
1492c 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
1492d 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a  LITE_DEBUG)./*.*
1492e 2a 20 43 6f 6d 70 75 74 65 20 61 20 73 74 72 69  * Compute a stri
1492f 6e 67 20 74 68 61 74 20 64 65 73 63 72 69 62 65  ng that describe
14930 73 20 74 68 65 20 50 34 20 70 61 72 61 6d 65 74  s the P4 paramet
14931 65 72 20 66 6f 72 20 61 6e 20 6f 70 63 6f 64 65  er for an opcode
14932 2e 0a 2a 2a 20 55 73 65 20 7a 54 65 6d 70 20 66  ..** Use zTemp f
14933 6f 72 20 61 6e 79 20 72 65 71 75 69 72 65 64 20  or any required 
14934 74 65 6d 70 6f 72 61 72 79 20 62 75 66 66 65 72  temporary buffer
14935 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 69   space..*/.stati
14936 63 20 63 68 61 72 20 2a 64 69 73 70 6c 61 79 50  c char *displayP
14937 34 28 4f 70 20 2a 70 4f 70 2c 20 63 68 61 72 20  4(Op *pOp, char 
14938 2a 7a 54 65 6d 70 2c 20 69 6e 74 20 6e 54 65 6d  *zTemp, int nTem
14939 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 50 34 20  p){.  char *zP4 
1493a 3d 20 7a 54 65 6d 70 3b 0a 20 20 61 73 73 65 72  = zTemp;.  asser
1493b 74 28 20 6e 54 65 6d 70 3e 3d 32 30 20 29 3b 0a  t( nTemp>=20 );.
1493c 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 70    switch( pOp->p
1493d 34 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73  4type ){.    cas
1493e 65 20 50 34 5f 4b 45 59 49 4e 46 4f 3a 20 7b 0a  e P4_KEYINFO: {.
1493f 20 20 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a        int i, j;.
14940 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70        KeyInfo *p
14941 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70  KeyInfo = pOp->p
14942 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20  4.pKeyInfo;.    
14943 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
14944 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c  tf(nTemp, zTemp,
14945 20 22 6b 65 79 69 6e 66 6f 28 25 64 22 2c 20 70   "keyinfo(%d", p
14946 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 29  KeyInfo->nField)
14947 3b 0a 20 20 20 20 20 20 69 20 3d 20 73 74 72 6c  ;.      i = strl
14948 65 6e 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 20  en(zTemp);.     
14949 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4b 65 79   for(j=0; j<pKey
1494a 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 3b 20 6a 2b  Info->nField; j+
1494b 2b 29 7b 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c  +){.        Coll
1494c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 4b 65  Seq *pColl = pKe
1494d 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6a 5d 3b  yInfo->aColl[j];
1494e 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 43 6f  .        if( pCo
1494f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ll ){.          
14950 69 6e 74 20 6e 20 3d 20 73 74 72 6c 65 6e 28 70  int n = strlen(p
14951 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  Coll->zName);.  
14952 20 20 20 20 20 20 20 20 69 66 28 20 69 2b 6e 3e          if( i+n>
14953 6e 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20  nTemp-6 ){.     
14954 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a         memcpy(&z
14955 54 65 6d 70 5b 69 5d 2c 22 2c 2e 2e 2e 22 2c 34  Temp[i],",...",4
14956 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62  );.            b
14957 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
14958 7d 0a 20 20 20 20 20 20 20 20 20 20 7a 54 65 6d  }.          zTem
14959 70 5b 69 2b 2b 5d 20 3d 20 27 2c 27 3b 0a 20 20  p[i++] = ',';.  
1495a 20 20 20 20 20 20 20 20 69 66 28 20 70 4b 65 79          if( pKey
1495b 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72  Info->aSortOrder
1495c 20 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53   && pKeyInfo->aS
1495d 6f 72 74 4f 72 64 65 72 5b 6a 5d 20 29 7b 0a 20  ortOrder[j] ){. 
1495e 20 20 20 20 20 20 20 20 20 20 20 7a 54 65 6d 70             zTemp
1495f 5b 69 2b 2b 5d 20 3d 20 27 2d 27 3b 0a 20 20 20  [i++] = '-';.   
14960 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
14961 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70     memcpy(&zTemp
14962 5b 69 5d 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d  [i], pColl->zNam
14963 65 2c 6e 2b 31 29 3b 0a 20 20 20 20 20 20 20 20  e,n+1);.        
14964 20 20 69 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20    i += n;.      
14965 20 20 7d 65 6c 73 65 20 69 66 28 20 69 2b 34 3c    }else if( i+4<
14966 6e 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20  nTemp-6 ){.     
14967 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65       memcpy(&zTe
14968 6d 70 5b 69 5d 2c 22 2c 6e 69 6c 22 2c 34 29 3b  mp[i],",nil",4);
14969 0a 20 20 20 20 20 20 20 20 20 20 69 20 2b 3d 20  .          i += 
1496a 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  4;.        }.   
1496b 20 20 20 7d 0a 20 20 20 20 20 20 7a 54 65 6d 70     }.      zTemp
1496c 5b 69 2b 2b 5d 20 3d 20 27 29 27 3b 0a 20 20 20  [i++] = ')';.   
1496d 20 20 20 7a 54 65 6d 70 5b 69 5d 20 3d 20 30 3b     zTemp[i] = 0;
1496e 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
1496f 3c 6e 54 65 6d 70 20 29 3b 0a 20 20 20 20 20 20  <nTemp );.      
14970 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
14971 20 63 61 73 65 20 50 34 5f 43 4f 4c 4c 53 45 51   case P4_COLLSEQ
14972 3a 20 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65  : {.      CollSe
14973 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 4f 70 2d 3e  q *pColl = pOp->
14974 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20  p4.pColl;.      
14975 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
14976 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22  (nTemp, zTemp, "
14977 63 6f 6c 6c 73 65 71 28 25 2e 32 30 73 29 22 2c  collseq(%.20s)",
14978 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a   pColl->zName);.
14979 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1497a 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 46   }.    case P4_F
1497b 55 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20 20 20  UNCDEF: {.      
1497c 46 75 6e 63 44 65 66 20 2a 70 44 65 66 20 3d 20  FuncDef *pDef = 
1497d 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20  pOp->p4.pFunc;. 
1497e 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
1497f 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65  rintf(nTemp, zTe
14980 6d 70 2c 20 22 25 73 28 25 64 29 22 2c 20 70 44  mp, "%s(%d)", pD
14981 65 66 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66 2d  ef->zName, pDef-
14982 3e 6e 41 72 67 29 3b 0a 20 20 20 20 20 20 62 72  >nArg);.      br
14983 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
14984 61 73 65 20 50 34 5f 49 4e 54 36 34 3a 20 7b 0a  ase P4_INT64: {.
14985 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
14986 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54  printf(nTemp, zT
14987 65 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 2a 70 4f  emp, "%lld", *pO
14988 70 2d 3e 70 34 2e 70 49 36 34 29 3b 0a 20 20 20  p->p4.pI64);.   
14989 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1498a 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 33      case P4_INT3
1498b 32 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  2: {.      sqlit
1498c 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d  e3_snprintf(nTem
1498d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 64 22 2c 20  p, zTemp, "%d", 
1498e 70 4f 70 2d 3e 70 34 2e 69 29 3b 0a 20 20 20 20  pOp->p4.i);.    
1498f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
14990 20 20 20 63 61 73 65 20 50 34 5f 52 45 41 4c 3a     case P4_REAL:
14991 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
14992 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c  _snprintf(nTemp,
14993 20 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c   zTemp, "%.16g",
14994 20 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c 29   *pOp->p4.pReal)
14995 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
14996 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34     }.    case P4
14997 5f 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20 4d 65  _MEM: {.      Me
14998 6d 20 2a 70 4d 65 6d 20 3d 20 70 4f 70 2d 3e 70  m *pMem = pOp->p
14999 34 2e 70 4d 65 6d 3b 0a 20 20 20 20 20 20 61 73  4.pMem;.      as
1499a 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61  sert( (pMem->fla
1499b 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d  gs & MEM_Null)==
1499c 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  0 );.      if( p
1499d 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
1499e 5f 53 74 72 20 29 7b 0a 20 20 20 20 20 20 20 20  _Str ){.        
1499f 7a 50 34 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a 20  zP4 = pMem->z;. 
149a0 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
149a1 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
149a2 5f 49 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20  _Int ){.        
149a3 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
149a4 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22  (nTemp, zTemp, "
149a5 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e 69  %lld", pMem->u.i
149a6 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  );.      }else i
149a7 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  f( pMem->flags &
149a8 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20   MEM_Real ){.   
149a9 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
149aa 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65  rintf(nTemp, zTe
149ab 6d 70 2c 20 22 25 2e 31 36 67 22 2c 20 70 4d 65  mp, "%.16g", pMe
149ac 6d 2d 3e 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20  m->r);.      }. 
149ad 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
149ae 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
149af 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
149b0 4c 45 0a 20 20 20 20 63 61 73 65 20 50 34 5f 56  LE.    case P4_V
149b1 54 41 42 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  TAB: {.      sql
149b2 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
149b3 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62   = pOp->p4.pVtab
149b4 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
149b5 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20  snprintf(nTemp, 
149b6 7a 54 65 6d 70 2c 20 22 76 74 61 62 3a 25 70 3a  zTemp, "vtab:%p:
149b7 25 70 22 2c 20 70 56 74 61 62 2c 20 70 56 74 61  %p", pVtab, pVta
149b8 62 2d 3e 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 20  b->pModule);.   
149b9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
149ba 23 65 6e 64 69 66 0a 20 20 20 20 64 65 66 61 75  #endif.    defau
149bb 6c 74 3a 20 7b 0a 20 20 20 20 20 20 7a 50 34 20  lt: {.      zP4 
149bc 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20  = pOp->p4.z;.   
149bd 20 20 20 69 66 28 20 7a 50 34 3d 3d 30 20 29 7b     if( zP4==0 ){
149be 0a 20 20 20 20 20 20 20 20 7a 50 34 20 3d 20 7a  .        zP4 = z
149bf 54 65 6d 70 3b 0a 20 20 20 20 20 20 20 20 7a 54  Temp;.        zT
149c0 65 6d 70 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20  emp[0] = 0;.    
149c1 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
149c2 61 73 73 65 72 74 28 20 7a 50 34 21 3d 30 20 29  assert( zP4!=0 )
149c3 3b 0a 20 20 72 65 74 75 72 6e 20 7a 50 34 3b 0a  ;.  return zP4;.
149c4 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
149c5 44 65 63 6c 61 72 65 20 74 6f 20 74 68 65 20 56  Declare to the V
149c6 64 62 65 20 74 68 61 74 20 74 68 65 20 42 54 72  dbe that the BTr
149c7 65 65 20 6f 62 6a 65 63 74 20 61 74 20 64 62 2d  ee object at db-
149c8 3e 61 44 62 5b 69 5d 20 69 73 20 75 73 65 64 2e  >aDb[i] is used.
149c9 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  .**.*/.SQLITE_PR
149ca 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
149cb 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28  e3VdbeUsesBtree(
149cc 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 69 29 7b  Vdbe *p, int i){
149cd 0a 20 20 69 6e 74 20 6d 61 73 6b 3b 0a 20 20 61  .  int mask;.  a
149ce 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69  ssert( i>=0 && i
149cf 3c 70 2d 3e 64 62 2d 3e 6e 44 62 20 29 3b 0a 20  <p->db->nDb );. 
149d0 20 61 73 73 65 72 74 28 20 69 3c 73 69 7a 65 6f   assert( i<sizeo
149d1 66 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 29 2a  f(p->btreeMask)*
149d2 38 20 29 3b 0a 20 20 6d 61 73 6b 20 3d 20 31 3c  8 );.  mask = 1<
149d3 3c 69 3b 0a 20 20 69 66 28 20 28 70 2d 3e 62 74  <i;.  if( (p->bt
149d4 72 65 65 4d 61 73 6b 20 26 20 6d 61 73 6b 29 3d  reeMask & mask)=
149d5 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 62 74 72  =0 ){.    p->btr
149d6 65 65 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a  eeMask |= mask;.
149d7 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
149d8 4d 75 74 65 78 41 72 72 61 79 49 6e 73 65 72 74  MutexArrayInsert
149d9 28 26 70 2d 3e 61 4d 75 74 65 78 2c 20 70 2d 3e  (&p->aMutex, p->
149da 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 3b  db->aDb[i].pBt);
149db 0a 20 20 7d 0a 7d 0a 0a 0a 23 69 66 20 64 65 66  .  }.}...#if def
149dc 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c  ined(VDBE_PROFIL
149dd 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
149de 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a  LITE_DEBUG)./*.*
149df 2a 20 50 72 69 6e 74 20 61 20 73 69 6e 67 6c 65  * Print a single
149e0 20 6f 70 63 6f 64 65 2e 20 20 54 68 69 73 20 72   opcode.  This r
149e1 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66  outine is used f
149e2 6f 72 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c  or debugging onl
149e3 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
149e4 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
149e5 33 56 64 62 65 50 72 69 6e 74 4f 70 28 46 49 4c  3VdbePrintOp(FIL
149e6 45 20 2a 70 4f 75 74 2c 20 69 6e 74 20 70 63 2c  E *pOut, int pc,
149e7 20 4f 70 20 2a 70 4f 70 29 7b 0a 20 20 63 68 61   Op *pOp){.  cha
149e8 72 20 2a 7a 50 34 3b 0a 20 20 63 68 61 72 20 7a  r *zP4;.  char z
149e9 50 74 72 5b 35 30 5d 3b 0a 20 20 73 74 61 74 69  Ptr[50];.  stati
149ea 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  c const char *zF
149eb 6f 72 6d 61 74 31 20 3d 20 22 25 34 64 20 25 2d  ormat1 = "%4d %-
149ec 31 33 73 20 25 34 64 20 25 34 64 20 25 34 64 20  13s %4d %4d %4d 
149ed 25 2d 34 73 20 25 2e 32 58 20 25 73 5c 6e 22 3b  %-4s %.2X %s\n";
149ee 0a 20 20 69 66 28 20 70 4f 75 74 3d 3d 30 20 29  .  if( pOut==0 )
149ef 20 70 4f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a   pOut = stdout;.
149f0 20 20 7a 50 34 20 3d 20 64 69 73 70 6c 61 79 50    zP4 = displayP
149f1 34 28 70 4f 70 2c 20 7a 50 74 72 2c 20 73 69 7a  4(pOp, zPtr, siz
149f2 65 6f 66 28 7a 50 74 72 29 29 3b 0a 20 20 66 70  eof(zPtr));.  fp
149f3 72 69 6e 74 66 28 70 4f 75 74 2c 20 7a 46 6f 72  rintf(pOut, zFor
149f4 6d 61 74 31 2c 20 70 63 2c 20 0a 20 20 20 20 20  mat1, pc, .     
149f5 20 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61   sqlite3OpcodeNa
149f6 6d 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 2c  me(pOp->opcode),
149f7 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70   pOp->p1, pOp->p
149f8 32 2c 20 70 4f 70 2d 3e 70 33 2c 20 7a 50 34 2c  2, pOp->p3, zP4,
149f9 20 70 4f 70 2d 3e 70 35 2c 0a 23 69 66 64 65 66   pOp->p5,.#ifdef
149fa 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
149fb 20 20 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e      pOp->zCommen
149fc 74 20 3f 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e  t ? pOp->zCommen
149fd 74 20 3a 20 22 22 0a 23 65 6c 73 65 0a 20 20 20  t : "".#else.   
149fe 20 20 20 22 22 0a 23 65 6e 64 69 66 0a 20 20 29     "".#endif.  )
149ff 3b 0a 20 20 66 66 6c 75 73 68 28 70 4f 75 74 29  ;.  fflush(pOut)
14a00 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
14a01 2a 20 52 65 6c 65 61 73 65 20 61 6e 20 61 72 72  * Release an arr
14a02 61 79 20 6f 66 20 4e 20 4d 65 6d 20 65 6c 65 6d  ay of N Mem elem
14a03 65 6e 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ents.*/.static v
14a04 6f 69 64 20 72 65 6c 65 61 73 65 4d 65 6d 41 72  oid releaseMemAr
14a05 72 61 79 28 4d 65 6d 20 2a 70 2c 20 69 6e 74 20  ray(Mem *p, int 
14a06 4e 2c 20 69 6e 74 20 66 72 65 65 62 75 66 66 65  N, int freebuffe
14a07 72 73 29 7b 0a 20 20 69 66 28 20 70 20 26 26 20  rs){.  if( p && 
14a08 4e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  N ){.    sqlite3
14a09 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20   *db = p->db;.  
14a0a 20 20 69 6e 74 20 6d 61 6c 6c 6f 63 5f 66 61 69    int malloc_fai
14a0b 6c 65 64 20 3d 20 64 62 2d 3e 6d 61 6c 6c 6f 63  led = db->malloc
14a0c 46 61 69 6c 65 64 3b 0a 20 20 20 20 77 68 69 6c  Failed;.    whil
14a0d 65 28 20 4e 2d 2d 3e 30 20 29 7b 0a 20 20 20 20  e( N-->0 ){.    
14a0e 20 20 61 73 73 65 72 74 28 20 4e 3c 32 20 7c 7c    assert( N<2 ||
14a0f 20 70 5b 30 5d 2e 64 62 3d 3d 70 5b 31 5d 2e 64   p[0].db==p[1].d
14a10 62 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 66  b );.      if( f
14a11 72 65 65 62 75 66 66 65 72 73 20 29 7b 0a 20 20  reebuffers ){.  
14a12 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
14a13 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 29 3b 0a  eMemRelease(p);.
14a14 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
14a15 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
14a16 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e  MemReleaseExtern
14a17 61 6c 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20  al(p);.      }. 
14a18 20 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20       p->flags = 
14a19 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20  MEM_Null;.      
14a1a 70 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  p++;.    }.    d
14a1b 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
14a1c 3d 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b  = malloc_failed;
14a1d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64 65 66 20 53  .  }.}..#ifdef S
14a1e 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
14a1f 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 53  ORY_MANAGEMENT.S
14a20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
14a21 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 6c  t sqlite3VdbeRel
14a22 65 61 73 65 42 75 66 66 65 72 73 28 56 64 62 65  easeBuffers(Vdbe
14a23 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a   *p){.  int ii;.
14a24 20 20 69 6e 74 20 6e 46 72 65 65 20 3d 20 30 3b    int nFree = 0;
14a25 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
14a26 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d  e3_mutex_held(p-
14a27 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
14a28 20 66 6f 72 28 69 69 3d 31 3b 20 69 69 3c 3d 70   for(ii=1; ii<=p
14a29 2d 3e 6e 4d 65 6d 3b 20 69 69 2b 2b 29 7b 0a 20  ->nMem; ii++){. 
14a2a 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 26     Mem *pMem = &
14a2b 70 2d 3e 61 4d 65 6d 5b 69 69 5d 3b 0a 20 20 20  p->aMem[ii];.   
14a2c 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 20 26 26 20   if( pMem->z && 
14a2d 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pMem->flags&MEM_
14a2e 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 61 73 73  Dyn ){.      ass
14a2f 65 72 74 28 20 21 70 4d 65 6d 2d 3e 78 44 65 6c  ert( !pMem->xDel
14a30 20 29 3b 0a 20 20 20 20 20 20 6e 46 72 65 65 20   );.      nFree 
14a31 2b 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  += sqlite3Malloc
14a32 53 69 7a 65 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20  Size(pMem->z);. 
14a33 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
14a34 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29  MemRelease(pMem)
14a35 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
14a36 74 75 72 6e 20 6e 46 72 65 65 3b 0a 7d 0a 23 65  turn nFree;.}.#e
14a37 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
14a38 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49  LITE_OMIT_EXPLAI
14a39 4e 0a 2f 2a 0a 2a 2a 20 47 69 76 65 20 61 20 6c  N./*.** Give a l
14a3a 69 73 74 69 6e 67 20 6f 66 20 74 68 65 20 70 72  isting of the pr
14a3b 6f 67 72 61 6d 20 69 6e 20 74 68 65 20 76 69 72  ogram in the vir
14a3c 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a  tual machine..**
14a3d 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63  .** The interfac
14a3e 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  e is the same as
14a3f 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63   sqlite3VdbeExec
14a40 28 29 2e 20 20 42 75 74 20 69 6e 73 74 65 61 64  ().  But instead
14a41 20 6f 66 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 74   of.** running t
14a42 68 65 20 63 6f 64 65 2c 20 69 74 20 69 6e 76 6f  he code, it invo
14a43 6b 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  kes the callback
14a44 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 69   once for each i
14a45 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54  nstruction..** T
14a46 68 69 73 20 66 65 61 74 75 72 65 20 69 73 20 75  his feature is u
14a47 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  sed to implement
14a48 20 22 45 58 50 4c 41 49 4e 22 2e 0a 2a 2a 0a 2a   "EXPLAIN"..**.*
14a49 2a 20 57 68 65 6e 20 70 2d 3e 65 78 70 6c 61 69  * When p->explai
14a4a 6e 3d 3d 31 2c 20 65 61 63 68 20 69 6e 73 74 72  n==1, each instr
14a4b 75 63 74 69 6f 6e 20 69 73 20 6c 69 73 74 65 64  uction is listed
14a4c 2e 20 20 57 68 65 6e 0a 2a 2a 20 70 2d 3e 65 78  .  When.** p->ex
14a4d 70 6c 61 69 6e 3d 3d 32 2c 20 6f 6e 6c 79 20 4f  plain==2, only O
14a4e 50 5f 45 78 70 6c 61 69 6e 20 69 6e 73 74 72 75  P_Explain instru
14a4f 63 74 69 6f 6e 73 20 61 72 65 20 6c 69 73 74 65  ctions are liste
14a50 64 20 61 6e 64 20 74 68 65 73 65 0a 2a 2a 20 61  d and these.** a
14a51 72 65 20 73 68 6f 77 6e 20 69 6e 20 61 20 64 69  re shown in a di
14a52 66 66 65 72 65 6e 74 20 66 6f 72 6d 61 74 2e 20  fferent format. 
14a53 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 69   p->explain==2 i
14a54 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d  s used to implem
14a55 65 6e 74 0a 2a 2a 20 45 58 50 4c 41 49 4e 20 51  ent.** EXPLAIN Q
14a56 55 45 52 59 20 50 4c 41 4e 2e 0a 2a 2f 0a 53 51  UERY PLAN..*/.SQ
14a57 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
14a58 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69 73 74   sqlite3VdbeList
14a59 28 0a 20 20 56 64 62 65 20 2a 70 20 20 20 20 20  (.  Vdbe *p     
14a5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
14a5b 20 54 68 65 20 56 44 42 45 20 2a 2f 0a 29 7b 0a   The VDBE */.){.
14a5c 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
14a5d 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 3b 0a  p->db;.  int i;.
14a5e 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
14a5f 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65  E_OK;.  Mem *pMe
14a60 6d 20 3d 20 70 2d 3e 70 52 65 73 75 6c 74 53 65  m = p->pResultSe
14a61 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 31 5d 3b  t = &p->aMem[1];
14a62 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 65  ..  assert( p->e
14a63 78 70 6c 61 69 6e 20 29 3b 0a 20 20 69 66 28 20  xplain );.  if( 
14a64 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d  p->magic!=VDBE_M
14a65 41 47 49 43 5f 52 55 4e 20 29 20 72 65 74 75 72  AGIC_RUN ) retur
14a66 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  n SQLITE_MISUSE;
14a67 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d  .  assert( db->m
14a68 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47  agic==SQLITE_MAG
14a69 49 43 5f 42 55 53 59 20 29 3b 0a 20 20 61 73 73  IC_BUSY );.  ass
14a6a 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49  ert( p->rc==SQLI
14a6b 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d  TE_OK || p->rc==
14a6c 53 51 4c 49 54 45 5f 42 55 53 59 20 29 3b 0a 0a  SQLITE_BUSY );..
14a6d 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68    /* Even though
14a6e 20 74 68 69 73 20 6f 70 63 6f 64 65 20 64 6f 65   this opcode doe
14a6f 73 20 6e 6f 74 20 75 73 65 20 64 79 6e 61 6d 69  s not use dynami
14a70 63 20 73 74 72 69 6e 67 73 20 66 6f 72 0a 20 20  c strings for.  
14a71 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 2c 20 72  ** the result, r
14a72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 6d 61  esult columns ma
14a73 79 20 62 65 63 6f 6d 65 20 64 79 6e 61 6d 69 63  y become dynamic
14a74 20 69 66 20 74 68 65 20 75 73 65 72 20 63 61 6c   if the user cal
14a75 6c 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f  ls.  ** sqlite3_
14a76 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 2c  column_text16(),
14a77 20 63 61 75 73 69 6e 67 20 61 20 74 72 61 6e 73   causing a trans
14a78 6c 61 74 69 6f 6e 20 74 6f 20 55 54 46 2d 31 36  lation to UTF-16
14a79 20 65 6e 63 6f 64 69 6e 67 2e 0a 20 20 2a 2f 0a   encoding..  */.
14a7a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61    releaseMemArra
14a7b 79 28 70 4d 65 6d 2c 20 70 2d 3e 6e 4d 65 6d 2c  y(pMem, p->nMem,
14a7c 20 31 29 3b 0a 0a 20 20 64 6f 7b 0a 20 20 20 20   1);..  do{.    
14a7d 69 20 3d 20 70 2d 3e 70 63 2b 2b 3b 0a 20 20 7d  i = p->pc++;.  }
14a7e 77 68 69 6c 65 28 20 69 3c 70 2d 3e 6e 4f 70 20  while( i<p->nOp 
14a7f 26 26 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32  && p->explain==2
14a80 20 26 26 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70   && p->aOp[i].op
14a81 63 6f 64 65 21 3d 4f 50 5f 45 78 70 6c 61 69 6e  code!=OP_Explain
14a82 20 29 3b 0a 20 20 69 66 28 20 69 3e 3d 70 2d 3e   );.  if( i>=p->
14a83 6e 4f 70 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63  nOp ){.    p->rc
14a84 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
14a85 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f    rc = SQLITE_DO
14a86 4e 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  NE;.  }else if( 
14a87 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75  db->u1.isInterru
14a88 70 74 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72  pted ){.    p->r
14a89 63 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52  c = SQLITE_INTER
14a8a 52 55 50 54 3b 0a 20 20 20 20 72 63 20 3d 20 53  RUPT;.    rc = S
14a8b 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
14a8c 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
14a8d 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 73  g(&p->zErrMsg, s
14a8e 71 6c 69 74 65 33 45 72 72 53 74 72 28 70 2d 3e  qlite3ErrStr(p->
14a8f 72 63 29 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a  rc), (char*)0);.
14a90 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61    }else{.    cha
14a91 72 20 2a 7a 3b 0a 20 20 20 20 4f 70 20 2a 70 4f  r *z;.    Op *pO
14a92 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a  p = &p->aOp[i];.
14a93 20 20 20 20 69 66 28 20 70 2d 3e 65 78 70 6c 61      if( p->expla
14a94 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 70  in==1 ){.      p
14a95 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
14a96 5f 49 6e 74 3b 0a 20 20 20 20 20 20 70 4d 65 6d  _Int;.      pMem
14a97 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
14a98 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20 70  INTEGER;.      p
14a99 4d 65 6d 2d 3e 75 2e 69 20 3d 20 69 3b 20 20 20  Mem->u.i = i;   
14a9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14a9b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
14a9c 50 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20  Program counter 
14a9d 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b  */.      pMem++;
14a9e 0a 20 20 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  .  .      pMem->
14a9f 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74 61 74  flags = MEM_Stat
14aa0 69 63 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54  ic|MEM_Str|MEM_T
14aa1 65 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  erm;.      pMem-
14aa2 3e 7a 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69  >z = (char*)sqli
14aa3 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 70 4f  te3OpcodeName(pO
14aa4 70 2d 3e 6f 70 63 6f 64 65 29 3b 20 20 2f 2a 20  p->opcode);  /* 
14aa5 4f 70 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20  Opcode */.      
14aa6 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 21  assert( pMem->z!
14aa7 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d  =0 );.      pMem
14aa8 2d 3e 6e 20 3d 20 73 74 72 6c 65 6e 28 70 4d 65  ->n = strlen(pMe
14aa9 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65  m->z);.      pMe
14aaa 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  m->type = SQLITE
14aab 5f 54 45 58 54 3b 0a 20 20 20 20 20 20 70 4d 65  _TEXT;.      pMe
14aac 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f  m->enc = SQLITE_
14aad 55 54 46 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d  UTF8;.      pMem
14aae 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70  ++;.    }..    p
14aaf 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
14ab0 5f 49 6e 74 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e  _Int;.    pMem->
14ab1 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 20  u.i = pOp->p1;  
14ab2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14ab3 20 20 20 20 20 20 20 20 2f 2a 20 50 31 20 2a 2f          /* P1 */
14ab4 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20  .    pMem->type 
14ab5 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  = SQLITE_INTEGER
14ab6 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20  ;.    pMem++;.. 
14ab7 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
14ab8 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4d   MEM_Int;.    pM
14ab9 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70  em->u.i = pOp->p
14aba 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2;              
14abb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
14abc 32 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 74  2 */.    pMem->t
14abd 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ype = SQLITE_INT
14abe 45 47 45 52 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b  EGER;.    pMem++
14abf 3b 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e 65 78  ;..    if( p->ex
14ac0 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20  plain==1 ){.    
14ac1 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
14ac2 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 70  MEM_Int;.      p
14ac3 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e  Mem->u.i = pOp->
14ac4 70 33 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  p3;             
14ac5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
14ac6 50 33 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d  P3 */.      pMem
14ac7 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
14ac8 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20 70  INTEGER;.      p
14ac9 4d 65 6d 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20  Mem++;.    }..  
14aca 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
14acb 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 33  eMemGrow(pMem, 3
14acc 32 2c 20 30 29 20 29 7b 20 20 20 20 20 20 20 20  2, 0) ){        
14acd 20 20 20 20 2f 2a 20 50 34 20 2a 2f 0a 20 20 20      /* P4 */.   
14ace 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63     p->db->malloc
14acf 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
14ad0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
14ad1 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20  NOMEM;.    }.   
14ad2 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
14ad3 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d  EM_Dyn|MEM_Str|M
14ad4 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 7a 20 3d  EM_Term;.    z =
14ad5 20 64 69 73 70 6c 61 79 50 34 28 70 4f 70 2c 20   displayP4(pOp, 
14ad6 70 4d 65 6d 2d 3e 7a 2c 20 33 32 29 3b 0a 20 20  pMem->z, 32);.  
14ad7 20 20 69 66 28 20 7a 21 3d 70 4d 65 6d 2d 3e 7a    if( z!=pMem->z
14ad8 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
14ad9 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70  3VdbeMemSetStr(p
14ada 4d 65 6d 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49  Mem, z, -1, SQLI
14adb 54 45 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20 20  TE_UTF8, 0);.   
14adc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73   }else{.      as
14add 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 21 3d 30  sert( pMem->z!=0
14ade 20 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e   );.      pMem->
14adf 6e 20 3d 20 73 74 72 6c 65 6e 28 70 4d 65 6d 2d  n = strlen(pMem-
14ae0 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  >z);.      pMem-
14ae1 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54  >enc = SQLITE_UT
14ae2 46 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d  F8;.    }.    pM
14ae3 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54  em->type = SQLIT
14ae4 45 5f 54 45 58 54 3b 0a 20 20 20 20 70 4d 65 6d  E_TEXT;.    pMem
14ae5 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e  ++;..    if( p->
14ae6 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20  explain==1 ){.  
14ae7 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56      if( sqlite3V
14ae8 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c  dbeMemGrow(pMem,
14ae9 20 34 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20   4, 0) ){.      
14aea 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46    p->db->mallocF
14aeb 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  ailed = 1;.     
14aec 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
14aed 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a  _NOMEM;.      }.
14aee 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
14aef 73 20 3d 20 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f  s = MEM_Dyn|MEM_
14af0 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20  Str|MEM_Term;.  
14af1 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 32 3b      pMem->n = 2;
14af2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
14af3 6e 70 72 69 6e 74 66 28 33 2c 20 70 4d 65 6d 2d  nprintf(3, pMem-
14af4 3e 7a 2c 20 22 25 2e 32 78 22 2c 20 70 4f 70 2d  >z, "%.2x", pOp-
14af5 3e 70 35 29 3b 20 20 20 2f 2a 20 50 35 20 2a 2f  >p5);   /* P5 */
14af6 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70  .      pMem->typ
14af7 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b  e = SQLITE_TEXT;
14af8 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63  .      pMem->enc
14af9 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a   = SQLITE_UTF8;.
14afa 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20        pMem++;.  
14afb 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
14afc 45 42 55 47 0a 20 20 20 20 20 20 69 66 28 20 70  EBUG.      if( p
14afd 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 29 7b 0a  Op->zComment ){.
14afe 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c          pMem->fl
14aff 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45  ags = MEM_Str|ME
14b00 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 20 20  M_Term;.        
14b01 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 7a  pMem->z = pOp->z
14b02 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 20 20 20  Comment;.       
14b03 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 74 72 6c 65   pMem->n = strle
14b04 6e 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20  n(pMem->z);.    
14b05 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20      pMem->enc = 
14b06 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20  SQLITE_UTF8;.   
14b07 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a     }else.#endif.
14b08 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20        {.        
14b09 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
14b0a 4d 5f 4e 75 6c 6c 3b 20 20 20 20 20 20 20 20 20  M_Null;         
14b0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
14b0c 20 43 6f 6d 6d 65 6e 74 20 2a 2f 0a 20 20 20 20   Comment */.    
14b0d 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d      pMem->type =
14b0e 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20   SQLITE_NULL;.  
14b0f 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
14b10 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 3d   p->nResColumn =
14b11 20 38 20 2d 20 35 2a 28 70 2d 3e 65 78 70 6c 61   8 - 5*(p->expla
14b12 69 6e 2d 31 29 3b 0a 20 20 20 20 70 2d 3e 72 63  in-1);.    p->rc
14b13 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
14b14 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 4f    rc = SQLITE_RO
14b15 57 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  W;.  }.  return 
14b16 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  rc;.}.#endif /* 
14b17 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c  SQLITE_OMIT_EXPL
14b18 41 49 4e 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53  AIN */..#ifdef S
14b19 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a  QLITE_DEBUG./*.*
14b1a 2a 20 50 72 69 6e 74 20 74 68 65 20 53 51 4c 20  * Print the SQL 
14b1b 74 68 61 74 20 77 61 73 20 75 73 65 64 20 74 6f  that was used to
14b1c 20 67 65 6e 65 72 61 74 65 20 61 20 56 44 42 45   generate a VDBE
14b1d 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 53 51 4c   program..*/.SQL
14b1e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
14b1f 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e   sqlite3VdbePrin
14b20 74 53 71 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20  tSql(Vdbe *p){. 
14b21 20 69 6e 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f   int nOp = p->nO
14b22 70 3b 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f 70  p;.  VdbeOp *pOp
14b23 3b 0a 20 20 69 66 28 20 6e 4f 70 3c 31 20 29 20  ;.  if( nOp<1 ) 
14b24 72 65 74 75 72 6e 3b 0a 20 20 70 4f 70 20 3d 20  return;.  pOp = 
14b25 26 70 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 66  &p->aOp[0];.  if
14b26 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  ( pOp->opcode==O
14b27 50 5f 54 72 61 63 65 20 26 26 20 70 4f 70 2d 3e  P_Trace && pOp->
14b28 70 34 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20 63  p4.z!=0 ){.    c
14b29 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70  onst char *z = p
14b2a 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 77 68  Op->p4.z;.    wh
14b2b 69 6c 65 28 20 69 73 73 70 61 63 65 28 2a 28 75  ile( isspace(*(u
14b2c 38 2a 29 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 20  8*)z) ) z++;.   
14b2d 20 70 72 69 6e 74 66 28 22 53 51 4c 3a 20 5b 25   printf("SQL: [%
14b2e 73 5d 5c 6e 22 2c 20 7a 29 3b 0a 20 20 7d 0a 7d  s]\n", z);.  }.}
14b2f 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65  .#endif..#if !de
14b30 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
14b31 54 5f 54 52 41 43 45 29 20 26 26 20 64 65 66 69  T_TRACE) && defi
14b32 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
14b33 45 5f 49 4f 54 52 41 43 45 29 0a 2f 2a 0a 2a 2a  E_IOTRACE)./*.**
14b34 20 50 72 69 6e 74 20 61 6e 20 49 4f 54 52 41 43   Print an IOTRAC
14b35 45 20 6d 65 73 73 61 67 65 20 73 68 6f 77 69 6e  E message showin
14b36 67 20 53 51 4c 20 63 6f 6e 74 65 6e 74 2e 0a 2a  g SQL content..*
14b37 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
14b38 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
14b39 65 49 4f 54 72 61 63 65 53 71 6c 28 56 64 62 65  eIOTraceSql(Vdbe
14b3a 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 20   *p){.  int nOp 
14b3b 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 65  = p->nOp;.  Vdbe
14b3c 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 66 28 20 73  Op *pOp;.  if( s
14b3d 71 6c 69 74 65 33 49 6f 54 72 61 63 65 3d 3d 30  qlite3IoTrace==0
14b3e 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
14b3f 20 6e 4f 70 3c 31 20 29 20 72 65 74 75 72 6e 3b   nOp<1 ) return;
14b40 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70  .  pOp = &p->aOp
14b41 5b 30 5d 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e  [0];.  if( pOp->
14b42 6f 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 63 65  opcode==OP_Trace
14b43 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30   && pOp->p4.z!=0
14b44 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6a   ){.    int i, j
14b45 3b 0a 20 20 20 20 63 68 61 72 20 7a 5b 31 30 30  ;.    char z[100
14b46 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0];.    sqlite3_
14b47 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
14b48 7a 29 2c 20 7a 2c 20 22 25 73 22 2c 20 70 4f 70  z), z, "%s", pOp
14b49 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 66 6f 72  ->p4.z);.    for
14b4a 28 69 3d 30 3b 20 69 73 73 70 61 63 65 28 28 75  (i=0; isspace((u
14b4b 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a 5b 69  nsigned char)z[i
14b4c 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 66  ]); i++){}.    f
14b4d 6f 72 28 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b  or(j=0; z[i]; i+
14b4e 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73  +){.      if( is
14b4f 73 70 61 63 65 28 28 75 6e 73 69 67 6e 65 64 20  space((unsigned 
14b50 63 68 61 72 29 7a 5b 69 5d 29 20 29 7b 0a 20 20  char)z[i]) ){.  
14b51 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2d 31 5d        if( z[i-1]
14b52 21 3d 27 20 27 20 29 7b 0a 20 20 20 20 20 20 20  !=' ' ){.       
14b53 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 20 27 3b     z[j++] = ' ';
14b54 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
14b55 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
14b56 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20  z[j++] = z[i];. 
14b57 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
14b58 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 73   z[j] = 0;.    s
14b59 71 6c 69 74 65 33 49 6f 54 72 61 63 65 28 22 53  qlite3IoTrace("S
14b5a 51 4c 20 25 73 5c 6e 22 2c 20 7a 29 3b 0a 20 20  QL %s\n", z);.  
14b5b 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53  }.}.#endif /* !S
14b5c 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45  QLITE_OMIT_TRACE
14b5d 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   && SQLITE_ENABL
14b5e 45 5f 49 4f 54 52 41 43 45 20 2a 2f 0a 0a 0a 2f  E_IOTRACE */.../
14b5f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 61 20 76  *.** Prepare a v
14b60 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 66  irtual machine f
14b61 6f 72 20 65 78 65 63 75 74 69 6f 6e 2e 20 20 54  or execution.  T
14b62 68 69 73 20 69 6e 76 6f 6c 76 65 73 20 74 68 69  his involves thi
14b63 6e 67 73 20 73 75 63 68 0a 2a 2a 20 61 73 20 61  ngs such.** as a
14b64 6c 6c 6f 63 61 74 69 6e 67 20 73 74 61 63 6b 20  llocating stack 
14b65 73 70 61 63 65 20 61 6e 64 20 69 6e 69 74 69 61  space and initia
14b66 6c 69 7a 69 6e 67 20 74 68 65 20 70 72 6f 67 72  lizing the progr
14b67 61 6d 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 20 41  am counter..** A
14b68 66 74 65 72 20 74 68 65 20 56 44 42 45 20 68 61  fter the VDBE ha
14b69 73 20 62 65 20 70 72 65 70 70 65 64 2c 20 69 74  s be prepped, it
14b6a 20 63 61 6e 20 62 65 20 65 78 65 63 75 74 65 64   can be executed
14b6b 20 62 79 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a   by one or more.
14b6c 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69  ** calls to sqli
14b6d 74 65 33 56 64 62 65 45 78 65 63 28 29 2e 20 20  te3VdbeExec().  
14b6e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74  .**.** This is t
14b6f 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d  he only way to m
14b70 6f 76 65 20 61 20 56 44 42 45 20 66 72 6f 6d 20  ove a VDBE from 
14b71 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20  VDBE_MAGIC_INIT 
14b72 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43  to.** VDBE_MAGIC
14b73 5f 52 55 4e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  _RUN..*/.SQLITE_
14b74 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
14b75 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64  ite3VdbeMakeRead
14b76 79 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20  y(.  Vdbe *p,   
14b77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14b78 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20      /* The VDBE 
14b79 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 2c 20 20  */.  int nVar,  
14b7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14b7b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
14b7c 20 27 3f 27 20 73 65 65 20 69 6e 20 74 68 65 20   '?' see in the 
14b7d 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  SQL statement */
14b7e 0a 20 20 69 6e 74 20 6e 4d 65 6d 2c 20 20 20 20  .  int nMem,    
14b7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14b80 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d    /* Number of m
14b81 65 6d 6f 72 79 20 63 65 6c 6c 73 20 74 6f 20 61  emory cells to a
14b82 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 69 6e 74  llocate */.  int
14b83 20 6e 43 75 72 73 6f 72 2c 20 20 20 20 20 20 20   nCursor,       
14b84 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
14b85 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73  umber of cursors
14b86 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a   to allocate */.
14b87 20 20 69 6e 74 20 69 73 45 78 70 6c 61 69 6e 20    int isExplain 
14b88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14b89 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
14b8a 45 58 50 4c 41 49 4e 20 6b 65 79 77 6f 72 64 73  EXPLAIN keywords
14b8b 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 29   is present */.)
14b8c 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c  {.  int n;.  sql
14b8d 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62  ite3 *db = p->db
14b8e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d  ;..  assert( p!=
14b8f 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
14b90 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41  ->magic==VDBE_MA
14b91 47 49 43 5f 49 4e 49 54 20 29 3b 0a 0a 20 20 2f  GIC_INIT );..  /
14b92 2a 20 54 68 65 72 65 20 73 68 6f 75 6c 64 20 62  * There should b
14b93 65 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f  e at least one o
14b94 70 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 61 73  pcode..  */.  as
14b95 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29  sert( p->nOp>0 )
14b96 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20  ;..  /* Set the 
14b97 6d 61 67 69 63 20 74 6f 20 56 44 42 45 5f 4d 41  magic to VDBE_MA
14b98 47 49 43 5f 52 55 4e 20 73 6f 6f 6e 65 72 20 72  GIC_RUN sooner r
14b99 61 74 68 65 72 20 74 68 61 6e 20 6c 61 74 65 72  ather than later
14b9a 2e 20 54 68 69 73 0a 20 20 20 2a 20 69 73 20 62  . This.   * is b
14b9b 65 63 61 75 73 65 20 74 68 65 20 63 61 6c 6c 20  ecause the call 
14b9c 74 6f 20 72 65 73 69 7a 65 4f 70 41 72 72 61 79  to resizeOpArray
14b9d 28 29 20 62 65 6c 6f 77 20 6d 61 79 20 73 68 72  () below may shr
14b9e 69 6e 6b 20 74 68 65 0a 20 20 20 2a 20 70 2d 3e  ink the.   * p->
14b9f 61 4f 70 5b 5d 20 61 72 72 61 79 20 74 6f 20 73  aOp[] array to s
14ba0 61 76 65 20 6d 65 6d 6f 72 79 20 69 66 20 63 61  ave memory if ca
14ba1 6c 6c 65 64 20 77 68 65 6e 20 69 6e 20 56 44 42  lled when in VDB
14ba2 45 5f 4d 41 47 49 43 5f 52 55 4e 20 0a 20 20 20  E_MAGIC_RUN .   
14ba3 2a 20 73 74 61 74 65 2e 0a 20 20 20 2a 2f 0a 20  * state..   */. 
14ba4 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45   p->magic = VDBE
14ba5 5f 4d 41 47 49 43 5f 52 55 4e 3b 0a 0a 20 20 2f  _MAGIC_RUN;..  /
14ba6 2a 20 46 6f 72 20 65 61 63 68 20 63 75 72 73 6f  * For each curso
14ba7 72 20 72 65 71 75 69 72 65 64 2c 20 61 6c 73 6f  r required, also
14ba8 20 61 6c 6c 6f 63 61 74 65 20 61 20 6d 65 6d 6f   allocate a memo
14ba9 72 79 20 63 65 6c 6c 2e 20 4d 65 6d 6f 72 79 0a  ry cell. Memory.
14baa 20 20 2a 2a 20 63 65 6c 6c 73 20 28 6e 4d 65 6d    ** cells (nMem
14bab 2b 31 2d 6e 43 75 72 73 6f 72 29 2e 2e 6e 4d 65  +1-nCursor)..nMe
14bac 6d 2c 20 69 6e 63 6c 75 73 69 76 65 2c 20 77 69  m, inclusive, wi
14bad 6c 6c 20 6e 65 76 65 72 20 62 65 20 75 73 65 64  ll never be used
14bae 20 62 79 0a 20 20 2a 2a 20 74 68 65 20 76 64 62   by.  ** the vdb
14baf 65 20 70 72 6f 67 72 61 6d 2e 20 49 6e 73 74 65  e program. Inste
14bb0 61 64 20 74 68 65 79 20 61 72 65 20 75 73 65 64  ad they are used
14bb1 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 73 70 61   to allocate spa
14bb2 63 65 20 66 6f 72 0a 20 20 2a 2a 20 43 75 72 73  ce for.  ** Curs
14bb3 6f 72 2f 42 74 43 75 72 73 6f 72 20 73 74 72 75  or/BtCursor stru
14bb4 63 74 75 72 65 73 2e 20 54 68 65 20 62 6c 6f 62  ctures. The blob
14bb5 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63   of memory assoc
14bb6 69 61 74 65 64 20 77 69 74 68 20 0a 20 20 2a 2a  iated with .  **
14bb7 20 63 75 72 73 6f 72 20 30 20 69 73 20 73 74 6f   cursor 0 is sto
14bb8 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65  red in memory ce
14bb9 6c 6c 20 6e 4d 65 6d 2e 20 4d 65 6d 6f 72 79 20  ll nMem. Memory 
14bba 63 65 6c 6c 20 28 6e 4d 65 6d 2d 31 29 0a 20 20  cell (nMem-1).  
14bbb 2a 2a 20 73 74 6f 72 65 73 20 74 68 65 20 62 6c  ** stores the bl
14bbc 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73  ob of memory ass
14bbd 6f 63 69 61 74 65 64 20 77 69 74 68 20 63 75 72  ociated with cur
14bbe 73 6f 72 20 31 2c 20 65 74 63 2e 0a 20 20 2a 2a  sor 1, etc..  **
14bbf 0a 20 20 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  .  ** See also: 
14bc0 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 29  allocateCursor()
14bc1 2e 0a 20 20 2a 2f 0a 20 20 6e 4d 65 6d 20 2b 3d  ..  */.  nMem +=
14bc2 20 6e 43 75 72 73 6f 72 3b 0a 0a 20 20 2f 2a 0a   nCursor;..  /*.
14bc3 20 20 2a 2a 20 41 6c 6c 6f 63 61 74 69 6f 6e 20    ** Allocation 
14bc4 73 70 61 63 65 20 66 6f 72 20 72 65 67 69 73 74  space for regist
14bc5 65 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ers..  */.  if( 
14bc6 70 2d 3e 61 4d 65 6d 3d 3d 30 20 29 7b 0a 20 20  p->aMem==0 ){.  
14bc7 20 20 69 6e 74 20 6e 41 72 67 3b 20 20 20 20 20    int nArg;     
14bc8 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d    /* Maximum num
14bc9 62 65 72 20 6f 66 20 61 72 67 73 20 70 61 73 73  ber of args pass
14bca 65 64 20 74 6f 20 61 20 75 73 65 72 20 66 75 6e  ed to a user fun
14bcb 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 72 65  ction. */.    re
14bcc 73 6f 6c 76 65 50 32 56 61 6c 75 65 73 28 70 2c  solveP2Values(p,
14bcd 20 26 6e 41 72 67 29 3b 0a 20 20 20 20 2f 2a 72   &nArg);.    /*r
14bce 65 73 69 7a 65 4f 70 41 72 72 61 79 28 70 2c 20  esizeOpArray(p, 
14bcf 70 2d 3e 6e 4f 70 29 3b 2a 2f 0a 20 20 20 20 61  p->nOp);*/.    a
14bd0 73 73 65 72 74 28 20 6e 56 61 72 3e 3d 30 20 29  ssert( nVar>=0 )
14bd1 3b 0a 20 20 20 20 69 66 28 20 69 73 45 78 70 6c  ;.    if( isExpl
14bd2 61 69 6e 20 26 26 20 6e 4d 65 6d 3c 31 30 20 29  ain && nMem<10 )
14bd3 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 4d 65 6d 20  {.      p->nMem 
14bd4 3d 20 6e 4d 65 6d 20 3d 20 31 30 3b 0a 20 20 20  = nMem = 10;.   
14bd5 20 7d 0a 20 20 20 20 70 2d 3e 61 4d 65 6d 20 3d   }.    p->aMem =
14bd6 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
14bd7 5a 65 72 6f 28 64 62 2c 0a 20 20 20 20 20 20 20  Zero(db,.       
14bd8 20 6e 4d 65 6d 2a 73 69 7a 65 6f 66 28 4d 65 6d   nMem*sizeof(Mem
14bd9 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  )               
14bda 2f 2a 20 61 4d 65 6d 20 2a 2f 0a 20 20 20 20 20  /* aMem */.     
14bdb 20 2b 20 6e 56 61 72 2a 73 69 7a 65 6f 66 28 4d   + nVar*sizeof(M
14bdc 65 6d 29 20 20 20 20 20 20 20 20 20 20 20 20 20  em)             
14bdd 20 20 2f 2a 20 61 56 61 72 20 2a 2f 0a 20 20 20    /* aVar */.   
14bde 20 20 20 2b 20 6e 41 72 67 2a 73 69 7a 65 6f 66     + nArg*sizeof
14bdf 28 4d 65 6d 2a 29 20 20 20 20 20 20 20 20 20 20  (Mem*)          
14be0 20 20 20 20 2f 2a 20 61 70 41 72 67 20 2a 2f 0a      /* apArg */.
14be1 20 20 20 20 20 20 2b 20 6e 56 61 72 2a 73 69 7a        + nVar*siz
14be2 65 6f 66 28 63 68 61 72 2a 29 20 20 20 20 20 20  eof(char*)      
14be3 20 20 20 20 20 20 20 2f 2a 20 61 7a 56 61 72 20         /* azVar 
14be4 2a 2f 0a 20 20 20 20 20 20 2b 20 6e 43 75 72 73  */.      + nCurs
14be5 6f 72 2a 73 69 7a 65 6f 66 28 43 75 72 73 6f 72  or*sizeof(Cursor
14be6 2a 29 20 2b 20 31 20 20 20 20 2f 2a 20 61 70 43  *) + 1    /* apC
14be7 73 72 20 2a 2f 0a 20 20 20 20 29 3b 0a 20 20 20  sr */.    );.   
14be8 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63   if( !db->malloc
14be9 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
14bea 70 2d 3e 61 4d 65 6d 2d 2d 3b 20 20 20 20 20 20  p->aMem--;      
14beb 20 20 20 20 20 20 20 2f 2a 20 61 4d 65 6d 5b 5d         /* aMem[]
14bec 20 67 6f 65 73 20 66 72 6f 6d 20 31 2e 2e 6e 4d   goes from 1..nM
14bed 65 6d 20 2a 2f 0a 20 20 20 20 20 20 70 2d 3e 6e  em */.      p->n
14bee 4d 65 6d 20 3d 20 6e 4d 65 6d 3b 20 20 20 20 20  Mem = nMem;     
14bef 20 20 20 2f 2a 20 20 20 20 20 20 20 6e 6f 74 20     /*       not 
14bf0 66 72 6f 6d 20 30 2e 2e 6e 4d 65 6d 2d 31 20 2a  from 0..nMem-1 *
14bf1 2f 0a 20 20 20 20 20 20 70 2d 3e 61 56 61 72 20  /.      p->aVar 
14bf2 3d 20 26 70 2d 3e 61 4d 65 6d 5b 6e 4d 65 6d 2b  = &p->aMem[nMem+
14bf3 31 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 56 61  1];.      p->nVa
14bf4 72 20 3d 20 6e 56 61 72 3b 0a 20 20 20 20 20 20  r = nVar;.      
14bf5 70 2d 3e 6f 6b 56 61 72 20 3d 20 30 3b 0a 20 20  p->okVar = 0;.  
14bf6 20 20 20 20 70 2d 3e 61 70 41 72 67 20 3d 20 28      p->apArg = (
14bf7 4d 65 6d 2a 2a 29 26 70 2d 3e 61 56 61 72 5b 6e  Mem**)&p->aVar[n
14bf8 56 61 72 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 61  Var];.      p->a
14bf9 7a 56 61 72 20 3d 20 28 63 68 61 72 2a 2a 29 26  zVar = (char**)&
14bfa 70 2d 3e 61 70 41 72 67 5b 6e 41 72 67 5d 3b 0a  p->apArg[nArg];.
14bfb 20 20 20 20 20 20 70 2d 3e 61 70 43 73 72 20 3d        p->apCsr =
14bfc 20 28 43 75 72 73 6f 72 2a 2a 29 26 70 2d 3e 61   (Cursor**)&p->a
14bfd 7a 56 61 72 5b 6e 56 61 72 5d 3b 0a 20 20 20 20  zVar[nVar];.    
14bfe 20 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 6e    p->nCursor = n
14bff 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 66 6f  Cursor;.      fo
14c00 72 28 6e 3d 30 3b 20 6e 3c 6e 56 61 72 3b 20 6e  r(n=0; n<nVar; n
14c01 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  ++){.        p->
14c02 61 56 61 72 5b 6e 5d 2e 66 6c 61 67 73 20 3d 20  aVar[n].flags = 
14c03 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20  MEM_Null;.      
14c04 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 64 62 20    p->aVar[n].db 
14c05 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a 20 20  = db;.      }.  
14c06 20 20 20 20 66 6f 72 28 6e 3d 31 3b 20 6e 3c 3d      for(n=1; n<=
14c07 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20  nMem; n++){.    
14c08 20 20 20 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 66      p->aMem[n].f
14c09 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b  lags = MEM_Null;
14c0a 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 4d 65 6d  .        p->aMem
14c0b 5b 6e 5d 2e 64 62 20 3d 20 64 62 3b 0a 20 20 20  [n].db = db;.   
14c0c 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23     }.    }.  }.#
14c0d 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
14c0e 55 47 0a 20 20 66 6f 72 28 6e 3d 31 3b 20 6e 3c  UG.  for(n=1; n<
14c0f 70 2d 3e 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b 0a 20  p->nMem; n++){. 
14c10 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4d     assert( p->aM
14c11 65 6d 5b 6e 5d 2e 64 62 3d 3d 64 62 20 29 3b 0a  em[n].db==db );.
14c12 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 70 2d    }.#endif..  p-
14c13 3e 70 63 20 3d 20 2d 31 3b 0a 20 20 70 2d 3e 72  >pc = -1;.  p->r
14c14 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
14c15 20 70 2d 3e 75 6e 69 71 75 65 43 6e 74 20 3d 20   p->uniqueCnt = 
14c16 30 3b 0a 20 20 70 2d 3e 72 65 74 75 72 6e 44 65  0;.  p->returnDe
14c17 70 74 68 20 3d 20 30 3b 0a 20 20 70 2d 3e 65 72  pth = 0;.  p->er
14c18 72 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41  rorAction = OE_A
14c19 62 6f 72 74 3b 0a 20 20 70 2d 3e 65 78 70 6c 61  bort;.  p->expla
14c1a 69 6e 20 7c 3d 20 69 73 45 78 70 6c 61 69 6e 3b  in |= isExplain;
14c1b 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44  .  p->magic = VD
14c1c 42 45 5f 4d 41 47 49 43 5f 52 55 4e 3b 0a 20 20  BE_MAGIC_RUN;.  
14c1d 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a  p->nChange = 0;.
14c1e 20 20 70 2d 3e 63 61 63 68 65 43 74 72 20 3d 20    p->cacheCtr = 
14c1f 31 3b 0a 20 20 70 2d 3e 6d 69 6e 57 72 69 74 65  1;.  p->minWrite
14c20 46 69 6c 65 46 6f 72 6d 61 74 20 3d 20 32 35 35  FileFormat = 255
14c21 3b 0a 20 20 70 2d 3e 6f 70 65 6e 65 64 53 74 61  ;.  p->openedSta
14c22 74 65 6d 65 6e 74 20 3d 20 30 3b 0a 23 69 66 64  tement = 0;.#ifd
14c23 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a  ef VDBE_PROFILE.
14c24 20 20 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20    {.    int i;. 
14c25 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d     for(i=0; i<p-
14c26 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nOp; i++){.    
14c27 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 20    p->aOp[i].cnt 
14c28 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 61 4f  = 0;.      p->aO
14c29 70 5b 69 5d 2e 63 79 63 6c 65 73 20 3d 20 30 3b  p[i].cycles = 0;
14c2a 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
14c2b 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  f.}../*.** Close
14c2c 20 61 20 56 44 42 45 20 63 75 72 73 6f 72 20 61   a VDBE cursor a
14c2d 6e 64 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 74  nd release all t
14c2e 68 65 20 72 65 73 6f 75 72 63 65 73 20 74 68 61  he resources tha
14c2f 74 20 63 75 72 73 6f 72 20 0a 2a 2a 20 68 61 70  t cursor .** hap
14c30 70 65 6e 73 20 74 6f 20 68 6f 6c 64 2e 0a 2a 2f  pens to hold..*/
14c31 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14c32 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
14c33 46 72 65 65 43 75 72 73 6f 72 28 56 64 62 65 20  FreeCursor(Vdbe 
14c34 2a 70 2c 20 43 75 72 73 6f 72 20 2a 70 43 78 29  *p, Cursor *pCx)
14c35 7b 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 20 29  {.  if( pCx==0 )
14c36 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
14c37 7d 0a 20 20 69 66 28 20 70 43 78 2d 3e 70 43 75  }.  if( pCx->pCu
14c38 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69  rsor ){.    sqli
14c39 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72  te3BtreeCloseCur
14c3a 73 6f 72 28 70 43 78 2d 3e 70 43 75 72 73 6f 72  sor(pCx->pCursor
14c3b 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 78  );.  }.  if( pCx
14c3c 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 73 71 6c  ->pBt ){.    sql
14c3d 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 70  ite3BtreeClose(p
14c3e 43 78 2d 3e 70 42 74 29 3b 0a 20 20 7d 0a 23 69  Cx->pBt);.  }.#i
14c3f 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
14c40 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
14c41 20 69 66 28 20 70 43 78 2d 3e 70 56 74 61 62 43   if( pCx->pVtabC
14c42 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c  ursor ){.    sql
14c43 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
14c44 20 2a 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20   *pVtabCursor = 
14c45 70 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f 72  pCx->pVtabCursor
14c46 3b 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69  ;.    const sqli
14c47 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
14c48 75 6c 65 20 3d 20 70 43 78 2d 3e 70 4d 6f 64 75  ule = pCx->pModu
14c49 6c 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61  le;.    p->inVta
14c4a 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 20  bMethod = 1;.   
14c4b 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
14c4c 66 65 74 79 4f 66 66 28 70 2d 3e 64 62 29 3b 0a  fetyOff(p->db);.
14c4d 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c      pModule->xCl
14c4e 6f 73 65 28 70 56 74 61 62 43 75 72 73 6f 72 29  ose(pVtabCursor)
14c4f 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69  ;.    (void)sqli
14c50 74 65 33 53 61 66 65 74 79 4f 6e 28 70 2d 3e 64  te3SafetyOn(p->d
14c51 62 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61  b);.    p->inVta
14c52 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 7d  bMethod = 0;.  }
14c53 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 21 70  .#endif.  if( !p
14c54 43 78 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f 54  Cx->ephemPseudoT
14c55 61 62 6c 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  able ){.    sqli
14c56 74 65 33 5f 66 72 65 65 28 70 43 78 2d 3e 70 44  te3_free(pCx->pD
14c57 61 74 61 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 6d  ata);.  }.  /* m
14c58 65 6d 73 65 74 28 70 43 78 2c 20 30 2c 20 73 69  emset(pCx, 0, si
14c59 7a 65 6f 66 28 43 75 72 73 6f 72 29 29 3b 20 2a  zeof(Cursor)); *
14c5a 2f 0a 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 66  /.  /* sqlite3_f
14c5b 72 65 65 28 70 43 78 2d 3e 61 54 79 70 65 29 3b  ree(pCx->aType);
14c5c 20 2a 2f 0a 20 20 2f 2a 20 73 71 6c 69 74 65 33   */.  /* sqlite3
14c5d 5f 66 72 65 65 28 70 43 78 29 3b 20 2a 2f 0a 7d  _free(pCx); */.}
14c5e 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6c  ../*.** Close al
14c5f 6c 20 63 75 72 73 6f 72 73 20 65 78 63 65 70 74  l cursors except
14c60 20 66 6f 72 20 56 54 61 62 20 63 75 72 73 6f 72   for VTab cursor
14c61 73 20 74 68 61 74 20 61 72 65 20 63 75 72 72 65  s that are curre
14c62 6e 74 6c 79 0a 2a 2a 20 69 6e 20 75 73 65 2e 0a  ntly.** in use..
14c63 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
14c64 6c 6f 73 65 41 6c 6c 43 75 72 73 6f 72 73 45 78  loseAllCursorsEx
14c65 63 65 70 74 41 63 74 69 76 65 56 74 61 62 73 28  ceptActiveVtabs(
14c66 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  Vdbe *p){.  int 
14c67 69 3b 0a 20 20 69 66 28 20 70 2d 3e 61 70 43 73  i;.  if( p->apCs
14c68 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  r==0 ) return;. 
14c69 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e   for(i=0; i<p->n
14c6a 43 75 72 73 6f 72 3b 20 69 2b 2b 29 7b 0a 20 20  Cursor; i++){.  
14c6b 20 20 43 75 72 73 6f 72 20 2a 70 43 20 3d 20 70    Cursor *pC = p
14c6c 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 20 20  ->apCsr[i];.    
14c6d 69 66 28 20 70 43 20 26 26 20 28 21 70 2d 3e 69  if( pC && (!p->i
14c6e 6e 56 74 61 62 4d 65 74 68 6f 64 20 7c 7c 20 21  nVtabMethod || !
14c6f 70 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 29  pC->pVtabCursor)
14c70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
14c71 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28  3VdbeFreeCursor(
14c72 70 2c 20 70 43 29 3b 0a 20 20 20 20 20 20 70 2d  p, pC);.      p-
14c73 3e 61 70 43 73 72 5b 69 5d 20 3d 20 30 3b 0a 20  >apCsr[i] = 0;. 
14c74 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
14c75 2a 20 43 6c 65 61 6e 20 75 70 20 74 68 65 20 56  * Clean up the V
14c76 4d 20 61 66 74 65 72 20 65 78 65 63 75 74 69 6f  M after executio
14c77 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  n..**.** This ro
14c78 75 74 69 6e 65 20 77 69 6c 6c 20 61 75 74 6f 6d  utine will autom
14c79 61 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65 20 61  atically close a
14c7a 6e 79 20 63 75 72 73 6f 72 73 2c 20 6c 69 73 74  ny cursors, list
14c7b 73 2c 20 61 6e 64 2f 6f 72 0a 2a 2a 20 73 6f 72  s, and/or.** sor
14c7c 74 65 72 73 20 74 68 61 74 20 77 65 72 65 20 6c  ters that were l
14c7d 65 66 74 20 6f 70 65 6e 2e 20 20 49 74 20 61 6c  eft open.  It al
14c7e 73 6f 20 64 65 6c 65 74 65 73 20 74 68 65 20 76  so deletes the v
14c7f 61 6c 75 65 73 20 6f 66 0a 2a 2a 20 76 61 72 69  alues of.** vari
14c80 61 62 6c 65 73 20 69 6e 20 74 68 65 20 61 56 61  ables in the aVa
14c81 72 5b 5d 20 61 72 72 61 79 2e 0a 2a 2f 0a 73 74  r[] array..*/.st
14c82 61 74 69 63 20 76 6f 69 64 20 43 6c 65 61 6e 75  atic void Cleanu
14c83 70 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 66  p(Vdbe *p, int f
14c84 72 65 65 62 75 66 66 65 72 73 29 7b 0a 20 20 69  reebuffers){.  i
14c85 6e 74 20 69 3b 0a 20 20 63 6c 6f 73 65 41 6c 6c  nt i;.  closeAll
14c86 43 75 72 73 6f 72 73 45 78 63 65 70 74 41 63 74  CursorsExceptAct
14c87 69 76 65 56 74 61 62 73 28 70 29 3b 0a 20 20 66  iveVtabs(p);.  f
14c88 6f 72 28 69 3d 31 3b 20 69 3c 3d 70 2d 3e 6e 4d  or(i=1; i<=p->nM
14c89 65 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4d 65  em; i++){.    Me
14c8a 6d 53 65 74 54 79 70 65 46 6c 61 67 28 26 70 2d  mSetTypeFlag(&p-
14c8b 3e 61 4d 65 6d 5b 69 5d 2c 20 4d 45 4d 5f 4e 75  >aMem[i], MEM_Nu
14c8c 6c 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61  ll);.  }.  relea
14c8d 73 65 4d 65 6d 41 72 72 61 79 28 26 70 2d 3e 61  seMemArray(&p->a
14c8e 4d 65 6d 5b 31 5d 2c 20 70 2d 3e 6e 4d 65 6d 2c  Mem[1], p->nMem,
14c8f 20 66 72 65 65 62 75 66 66 65 72 73 29 3b 0a 20   freebuffers);. 
14c90 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 66 6f   sqlite3VdbeFifo
14c91 43 6c 65 61 72 28 26 70 2d 3e 73 46 69 66 6f 29  Clear(&p->sFifo)
14c92 3b 0a 20 20 69 66 28 20 70 2d 3e 63 6f 6e 74 65  ;.  if( p->conte
14c93 78 74 53 74 61 63 6b 20 29 7b 0a 20 20 20 20 66  xtStack ){.    f
14c94 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 63 6f 6e  or(i=0; i<p->con
14c95 74 65 78 74 53 74 61 63 6b 54 6f 70 3b 20 69 2b  textStackTop; i+
14c96 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  +){.      sqlite
14c97 33 56 64 62 65 46 69 66 6f 43 6c 65 61 72 28 26  3VdbeFifoClear(&
14c98 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 5b  p->contextStack[
14c99 69 5d 2e 73 46 69 66 6f 29 3b 0a 20 20 20 20 7d  i].sFifo);.    }
14c9a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
14c9b 65 28 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63  e(p->contextStac
14c9c 6b 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 63 6f 6e  k);.  }.  p->con
14c9d 74 65 78 74 53 74 61 63 6b 20 3d 20 30 3b 0a 20  textStack = 0;. 
14c9e 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b   p->contextStack
14c9f 44 65 70 74 68 20 3d 20 30 3b 0a 20 20 70 2d 3e  Depth = 0;.  p->
14ca0 63 6f 6e 74 65 78 74 53 74 61 63 6b 54 6f 70 20  contextStackTop 
14ca1 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  = 0;.  sqlite3_f
14ca2 72 65 65 28 70 2d 3e 7a 45 72 72 4d 73 67 29 3b  ree(p->zErrMsg);
14ca3 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  .  p->zErrMsg = 
14ca4 30 3b 0a 20 20 70 2d 3e 70 52 65 73 75 6c 74 53  0;.  p->pResultS
14ca5 65 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  et = 0;.}../*.**
14ca6 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72 20   Set the number 
14ca7 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  of result column
14ca8 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 72  s that will be r
14ca9 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 20  eturned by this 
14caa 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  SQL.** statement
14cab 2e 20 54 68 69 73 20 69 73 20 6e 6f 77 20 73 65  . This is now se
14cac 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d  t at compile tim
14cad 65 2c 20 72 61 74 68 65 72 20 74 68 61 6e 20 64  e, rather than d
14cae 75 72 69 6e 67 0a 2a 2a 20 65 78 65 63 75 74 69  uring.** executi
14caf 6f 6e 20 6f 66 20 74 68 65 20 76 64 62 65 20 70  on of the vdbe p
14cb0 72 6f 67 72 61 6d 20 73 6f 20 74 68 61 74 20 73  rogram so that s
14cb1 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f  qlite3_column_co
14cb2 75 6e 74 28 29 20 63 61 6e 0a 2a 2a 20 62 65 20  unt() can.** be 
14cb3 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 53 51 4c  called on an SQL
14cb4 20 73 74 61 74 65 6d 65 6e 74 20 62 65 66 6f 72   statement befor
14cb5 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  e sqlite3_step()
14cb6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
14cb7 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
14cb8 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 56  VdbeSetNumCols(V
14cb9 64 62 65 20 2a 70 2c 20 69 6e 74 20 6e 52 65 73  dbe *p, int nRes
14cba 43 6f 6c 75 6d 6e 29 7b 0a 20 20 4d 65 6d 20 2a  Column){.  Mem *
14cbb 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 69 6e 74 20  pColName;.  int 
14cbc 6e 3b 0a 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d  n;..  releaseMem
14cbd 41 72 72 61 79 28 70 2d 3e 61 43 6f 6c 4e 61 6d  Array(p->aColNam
14cbe 65 2c 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e  e, p->nResColumn
14cbf 2a 43 4f 4c 4e 41 4d 45 5f 4e 2c 20 31 29 3b 0a  *COLNAME_N, 1);.
14cc0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
14cc1 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 6e  ->aColName);.  n
14cc2 20 3d 20 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f   = nResColumn*CO
14cc3 4c 4e 41 4d 45 5f 4e 3b 0a 20 20 70 2d 3e 6e 52  LNAME_N;.  p->nR
14cc4 65 73 43 6f 6c 75 6d 6e 20 3d 20 6e 52 65 73 43  esColumn = nResC
14cc5 6f 6c 75 6d 6e 3b 0a 20 20 70 2d 3e 61 43 6f 6c  olumn;.  p->aCol
14cc6 4e 61 6d 65 20 3d 20 70 43 6f 6c 4e 61 6d 65 20  Name = pColName 
14cc7 3d 20 28 4d 65 6d 2a 29 73 71 6c 69 74 65 33 44  = (Mem*)sqlite3D
14cc8 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 2d 3e 64  bMallocZero(p->d
14cc9 62 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 6e  b, sizeof(Mem)*n
14cca 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 43 6f   );.  if( p->aCo
14ccb 6c 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74 75 72  lName==0 ) retur
14ccc 6e 3b 0a 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20  n;.  while( n-- 
14ccd 3e 20 30 20 29 7b 0a 20 20 20 20 70 43 6f 6c 4e  > 0 ){.    pColN
14cce 61 6d 65 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  ame->flags = MEM
14ccf 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 43 6f 6c 4e  _Null;.    pColN
14cd0 61 6d 65 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b  ame->db = p->db;
14cd1 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2b 2b 3b  .    pColName++;
14cd2 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65  .  }.}../*.** Se
14cd3 74 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  t the name of th
14cd4 65 20 69 64 78 27 74 68 20 63 6f 6c 75 6d 6e 20  e idx'th column 
14cd5 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 62  to be returned b
14cd6 79 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  y the SQL statem
14cd7 65 6e 74 2e 0a 2a 2a 20 7a 4e 61 6d 65 20 6d 75  ent..** zName mu
14cd8 73 74 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20  st be a pointer 
14cd9 74 6f 20 61 20 6e 75 6c 20 74 65 72 6d 69 6e 61  to a nul termina
14cda 74 65 64 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a  ted string..**.*
14cdb 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 75 73 74  * This call must
14cdc 20 62 65 20 6d 61 64 65 20 61 66 74 65 72 20 61   be made after a
14cdd 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
14cde 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 29  VdbeSetNumCols()
14cdf 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 4e 3d 3d 50 34  ..**.** If N==P4
14ce0 5f 53 54 41 54 49 43 20 20 69 74 20 6d 65 61 6e  _STATIC  it mean
14ce1 73 20 74 68 61 74 20 7a 4e 61 6d 65 20 69 73 20  s that zName is 
14ce2 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 63  a pointer to a c
14ce3 6f 6e 73 74 61 6e 74 20 73 74 61 74 69 63 0a 2a  onstant static.*
14ce4 2a 20 73 74 72 69 6e 67 20 61 6e 64 20 77 65 20  * string and we 
14ce5 63 61 6e 20 6a 75 73 74 20 63 6f 70 79 20 74 68  can just copy th
14ce6 65 20 70 6f 69 6e 74 65 72 2e 20 49 66 20 69 74  e pointer. If it
14ce7 20 69 73 20 50 34 5f 44 59 4e 41 4d 49 43 2c 20   is P4_DYNAMIC, 
14ce8 74 68 65 6e 20 0a 2a 2a 20 74 68 65 20 73 74 72  then .** the str
14ce9 69 6e 67 20 69 73 20 66 72 65 65 64 20 75 73 69  ing is freed usi
14cea 6e 67 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  ng sqlite3_free(
14ceb 29 20 77 68 65 6e 20 74 68 65 20 76 64 62 65 20  ) when the vdbe 
14cec 69 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68  is finished with
14ced 0a 2a 2a 20 69 74 2e 20 4f 74 68 65 72 77 69 73  .** it. Otherwis
14cee 65 2c 20 4e 20 62 79 74 65 73 20 6f 66 20 7a 4e  e, N bytes of zN
14cef 61 6d 65 20 61 72 65 20 63 6f 70 69 65 64 2e 0a  ame are copied..
14cf0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14cf1 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
14cf2 65 53 65 74 43 6f 6c 4e 61 6d 65 28 56 64 62 65  eSetColName(Vdbe
14cf3 20 2a 70 2c 20 69 6e 74 20 69 64 78 2c 20 69 6e   *p, int idx, in
14cf4 74 20 76 61 72 2c 20 63 6f 6e 73 74 20 63 68 61  t var, const cha
14cf5 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 4e 29  r *zName, int N)
14cf6 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65  {.  int rc;.  Me
14cf7 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 61  m *pColName;.  a
14cf8 73 73 65 72 74 28 20 69 64 78 3c 70 2d 3e 6e 52  ssert( idx<p->nR
14cf9 65 73 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 61 73  esColumn );.  as
14cfa 73 65 72 74 28 20 76 61 72 3c 43 4f 4c 4e 41 4d  sert( var<COLNAM
14cfb 45 5f 4e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e  E_N );.  if( p->
14cfc 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
14cfd 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
14cfe 5f 4e 4f 4d 45 4d 3b 0a 20 20 61 73 73 65 72 74  _NOMEM;.  assert
14cff 28 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 21 3d 30  ( p->aColName!=0
14d00 20 29 3b 0a 20 20 70 43 6f 6c 4e 61 6d 65 20 3d   );.  pColName =
14d01 20 26 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b 69   &(p->aColName[i
14d02 64 78 2b 76 61 72 2a 70 2d 3e 6e 52 65 73 43 6f  dx+var*p->nResCo
14d03 6c 75 6d 6e 5d 29 3b 0a 20 20 69 66 28 20 4e 3d  lumn]);.  if( N=
14d04 3d 50 34 5f 44 59 4e 41 4d 49 43 20 7c 7c 20 4e  =P4_DYNAMIC || N
14d05 3d 3d 50 34 5f 53 54 41 54 49 43 20 29 7b 0a 20  ==P4_STATIC ){. 
14d06 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
14d07 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 43 6f  dbeMemSetStr(pCo
14d08 6c 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 2d 31  lName, zName, -1
14d09 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53  , SQLITE_UTF8, S
14d0a 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
14d0b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d   }else{.    rc =
14d0c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
14d0d 65 74 53 74 72 28 70 43 6f 6c 4e 61 6d 65 2c 20  etStr(pColName, 
14d0e 7a 4e 61 6d 65 2c 20 4e 2c 20 53 51 4c 49 54 45  zName, N, SQLITE
14d0f 5f 55 54 46 38 2c 53 51 4c 49 54 45 5f 54 52 41  _UTF8,SQLITE_TRA
14d10 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 20 20 69  NSIENT);.  }.  i
14d11 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
14d12 20 26 26 20 4e 3d 3d 50 34 5f 44 59 4e 41 4d 49   && N==P4_DYNAMI
14d13 43 20 29 7b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d  C ){.    pColNam
14d14 65 2d 3e 66 6c 61 67 73 20 26 3d 20 28 7e 4d 45  e->flags &= (~ME
14d15 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20 70  M_Static);.    p
14d16 43 6f 6c 4e 61 6d 65 2d 3e 7a 4d 61 6c 6c 6f 63  ColName->zMalloc
14d17 20 3d 20 70 43 6f 6c 4e 61 6d 65 2d 3e 7a 3b 0a   = pColName->z;.
14d18 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
14d19 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 72 65 61 64  .}../*.** A read
14d1a 20 6f 72 20 77 72 69 74 65 20 74 72 61 6e 73 61   or write transa
14d1b 63 74 69 6f 6e 20 6d 61 79 20 6f 72 20 6d 61 79  ction may or may
14d1c 20 6e 6f 74 20 62 65 20 61 63 74 69 76 65 20 6f   not be active o
14d1d 6e 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  n database handl
14d1e 65 0a 2a 2a 20 64 62 2e 20 49 66 20 61 20 74 72  e.** db. If a tr
14d1f 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74  ansaction is act
14d20 69 76 65 2c 20 63 6f 6d 6d 69 74 20 69 74 2e 20  ive, commit it. 
14d21 49 66 20 74 68 65 72 65 20 69 73 20 61 0a 2a 2a  If there is a.**
14d22 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69   write-transacti
14d23 6f 6e 20 73 70 61 6e 6e 69 6e 67 20 6d 6f 72 65  on spanning more
14d24 20 74 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61   than one databa
14d25 73 65 20 66 69 6c 65 2c 20 74 68 69 73 20 72 6f  se file, this ro
14d26 75 74 69 6e 65 0a 2a 2a 20 74 61 6b 65 73 20 63  utine.** takes c
14d27 61 72 65 20 6f 66 20 74 68 65 20 6d 61 73 74 65  are of the maste
14d28 72 20 6a 6f 75 72 6e 61 6c 20 74 72 69 63 6b 65  r journal tricke
14d29 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ry..*/.static in
14d2a 74 20 76 64 62 65 43 6f 6d 6d 69 74 28 73 71 6c  t vdbeCommit(sql
14d2b 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74  ite3 *db){.  int
14d2c 20 69 3b 0a 20 20 69 6e 74 20 6e 54 72 61 6e 73   i;.  int nTrans
14d2d 20 3d 20 30 3b 20 20 2f 2a 20 4e 75 6d 62 65 72   = 0;  /* Number
14d2e 20 6f 66 20 64 61 74 61 62 61 73 65 73 20 77 69   of databases wi
14d2f 74 68 20 61 6e 20 61 63 74 69 76 65 20 77 72 69  th an active wri
14d30 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a  te-transaction *
14d31 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  /.  int rc = SQL
14d32 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 65  ITE_OK;.  int ne
14d33 65 64 58 63 6f 6d 6d 69 74 20 3d 20 30 3b 0a 0a  edXcommit = 0;..
14d34 20 20 2f 2a 20 42 65 66 6f 72 65 20 64 6f 69 6e    /* Before doin
14d35 67 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 2c  g anything else,
14d36 20 63 61 6c 6c 20 74 68 65 20 78 53 79 6e 63 28   call the xSync(
14d37 29 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61  ) callback for a
14d38 6e 79 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20  ny.  ** virtual 
14d39 6d 6f 64 75 6c 65 20 74 61 62 6c 65 73 20 77 72  module tables wr
14d3a 69 74 74 65 6e 20 69 6e 20 74 68 69 73 20 74 72  itten in this tr
14d3b 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 20  ansaction. This 
14d3c 68 61 73 20 74 6f 0a 20 20 2a 2a 20 62 65 20 64  has to.  ** be d
14d3d 6f 6e 65 20 62 65 66 6f 72 65 20 64 65 74 65 72  one before deter
14d3e 6d 69 6e 69 6e 67 20 77 68 65 74 68 65 72 20 61  mining whether a
14d3f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
14d40 66 69 6c 65 20 69 73 20 0a 20 20 2a 2a 20 72 65  file is .  ** re
14d41 71 75 69 72 65 64 2c 20 61 73 20 61 6e 20 78 53  quired, as an xS
14d42 79 6e 63 28 29 20 63 61 6c 6c 62 61 63 6b 20 6d  ync() callback m
14d43 61 79 20 61 64 64 20 61 6e 20 61 74 74 61 63 68  ay add an attach
14d44 65 64 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a  ed database.  **
14d45 20 74 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74   to the transact
14d46 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d  ion..  */.  rc =
14d47 20 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63   sqlite3VtabSync
14d48 28 64 62 2c 20 72 63 29 3b 0a 20 20 69 66 28 20  (db, rc);.  if( 
14d49 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
14d4a 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
14d4b 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c    }..  /* This l
14d4c 6f 6f 70 20 64 65 74 65 72 6d 69 6e 65 73 20 28  oop determines (
14d4d 61 29 20 69 66 20 74 68 65 20 63 6f 6d 6d 69 74  a) if the commit
14d4e 20 68 6f 6f 6b 20 73 68 6f 75 6c 64 20 62 65 20   hook should be 
14d4f 69 6e 76 6f 6b 65 64 20 61 6e 64 0a 20 20 2a 2a  invoked and.  **
14d50 20 28 62 29 20 68 6f 77 20 6d 61 6e 79 20 64 61   (b) how many da
14d51 74 61 62 61 73 65 20 66 69 6c 65 73 20 68 61 76  tabase files hav
14d52 65 20 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61  e open write tra
14d53 6e 73 61 63 74 69 6f 6e 73 2c 20 6e 6f 74 20 0a  nsactions, not .
14d54 20 20 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 74    ** including t
14d55 68 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65  he temp database
14d56 2e 20 28 62 29 20 69 73 20 69 6d 70 6f 72 74 61  . (b) is importa
14d57 6e 74 20 62 65 63 61 75 73 65 20 69 66 20 6d 6f  nt because if mo
14d58 72 65 20 74 68 61 6e 20 0a 20 20 2a 2a 20 6f 6e  re than .  ** on
14d59 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
14d5a 68 61 73 20 61 6e 20 6f 70 65 6e 20 77 72 69 74  has an open writ
14d5b 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61  e transaction, a
14d5c 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a   master journal.
14d5d 20 20 2a 2a 20 66 69 6c 65 20 69 73 20 72 65 71    ** file is req
14d5e 75 69 72 65 64 20 66 6f 72 20 61 6e 20 61 74 6f  uired for an ato
14d5f 6d 69 63 20 63 6f 6d 6d 69 74 2e 0a 20 20 2a 2f  mic commit..  */
14d60 20 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64   .  for(i=0; i<d
14d61 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20  b->nDb; i++){ . 
14d62 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20     Btree *pBt = 
14d63 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a  db->aDb[i].pBt;.
14d64 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42      if( sqlite3B
14d65 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 42  treeIsInTrans(pB
14d66 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 65 65 64  t) ){.      need
14d67 58 63 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20  Xcommit = 1;.   
14d68 20 20 20 69 66 28 20 69 21 3d 31 20 29 20 6e 54     if( i!=1 ) nT
14d69 72 61 6e 73 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  rans++;.    }.  
14d6a 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  }..  /* If there
14d6b 20 61 72 65 20 61 6e 79 20 77 72 69 74 65 2d 74   are any write-t
14d6c 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 74 20 61  ransactions at a
14d6d 6c 6c 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 63  ll, invoke the c
14d6e 6f 6d 6d 69 74 20 68 6f 6f 6b 20 2a 2f 0a 20 20  ommit hook */.  
14d6f 69 66 28 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20  if( needXcommit 
14d70 26 26 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61  && db->xCommitCa
14d71 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 28 76  llback ){.    (v
14d72 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
14d73 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 72 63  yOff(db);.    rc
14d74 20 3d 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61   = db->xCommitCa
14d75 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 43 6f 6d 6d  llback(db->pComm
14d76 69 74 41 72 67 29 3b 0a 20 20 20 20 28 76 6f 69  itArg);.    (voi
14d77 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
14d78 6e 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 72  n(db);.    if( r
14d79 63 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  c ){.      retur
14d7a 6e 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  n SQLITE_CONSTRA
14d7b 49 4e 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  INT;.    }.  }..
14d7c 20 20 2f 2a 20 54 68 65 20 73 69 6d 70 6c 65 20    /* The simple 
14d7d 63 61 73 65 20 2d 20 6e 6f 20 6d 6f 72 65 20 74  case - no more t
14d7e 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65  han one database
14d7f 20 66 69 6c 65 20 28 6e 6f 74 20 63 6f 75 6e 74   file (not count
14d80 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 54 45 4d  ing the.  ** TEM
14d81 50 20 64 61 74 61 62 61 73 65 29 20 68 61 73 20  P database) has 
14d82 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 63  a transaction ac
14d83 74 69 76 65 2e 20 20 20 54 68 65 72 65 20 69 73  tive.   There is
14d84 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 74 68 65   no need for the
14d85 0a 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75  .  ** master-jou
14d86 72 6e 61 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  rnal..  **.  ** 
14d87 49 66 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  If the return va
14d88 6c 75 65 20 6f 66 20 73 71 6c 69 74 65 33 42 74  lue of sqlite3Bt
14d89 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 29  reeGetFilename()
14d8a 20 69 73 20 61 20 7a 65 72 6f 20 6c 65 6e 67 74   is a zero lengt
14d8b 68 0a 20 20 2a 2a 20 73 74 72 69 6e 67 2c 20 69  h.  ** string, i
14d8c 74 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 6e  t means the main
14d8d 20 64 61 74 61 62 61 73 65 20 69 73 20 3a 6d 65   database is :me
14d8e 6d 6f 72 79 3a 2e 20 20 49 6e 20 74 68 61 74 20  mory:.  In that 
14d8f 63 61 73 65 20 77 65 20 64 6f 0a 20 20 2a 2a 20  case we do.  ** 
14d90 6e 6f 74 20 73 75 70 70 6f 72 74 20 61 74 6f 6d  not support atom
14d91 69 63 20 6d 75 6c 74 69 2d 66 69 6c 65 20 63 6f  ic multi-file co
14d92 6d 6d 69 74 73 2c 20 73 6f 20 75 73 65 20 74 68  mmits, so use th
14d93 65 20 73 69 6d 70 6c 65 20 63 61 73 65 20 74 68  e simple case th
14d94 65 6e 0a 20 20 2a 2a 20 74 6f 6f 2e 0a 20 20 2a  en.  ** too..  *
14d95 2f 0a 20 20 69 66 28 20 30 3d 3d 73 74 72 6c 65  /.  if( 0==strle
14d96 6e 28 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  n(sqlite3BtreeGe
14d97 74 46 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44  tFilename(db->aD
14d98 62 5b 30 5d 2e 70 42 74 29 29 20 7c 7c 20 6e 54  b[0].pBt)) || nT
14d99 72 61 6e 73 3c 3d 31 20 29 7b 0a 20 20 20 20 66  rans<=1 ){.    f
14d9a 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49  or(i=0; rc==SQLI
14d9b 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e  TE_OK && i<db->n
14d9c 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20  Db; i++){ .     
14d9d 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62   Btree *pBt = db
14d9e 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20  ->aDb[i].pBt;.  
14d9f 20 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20      if( pBt ){. 
14da0 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
14da1 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68  te3BtreeCommitPh
14da2 61 73 65 4f 6e 65 28 70 42 74 2c 20 30 29 3b 0a  aseOne(pBt, 0);.
14da3 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
14da4 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d     /* Do the com
14da5 6d 69 74 20 6f 6e 6c 79 20 69 66 20 61 6c 6c 20  mit only if all 
14da6 64 61 74 61 62 61 73 65 73 20 73 75 63 63 65 73  databases succes
14da7 73 66 75 6c 6c 79 20 63 6f 6d 70 6c 65 74 65 20  sfully complete 
14da8 70 68 61 73 65 20 31 2e 20 0a 20 20 20 20 2a 2a  phase 1. .    **
14da9 20 49 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 42   If one of the B
14daa 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f  treeCommitPhaseO
14dab 6e 65 28 29 20 63 61 6c 6c 73 20 66 61 69 6c 73  ne() calls fails
14dac 2c 20 74 68 69 73 20 69 6e 64 69 63 61 74 65 73  , this indicates
14dad 20 61 6e 0a 20 20 20 20 2a 2a 20 49 4f 20 65 72   an.    ** IO er
14dae 72 6f 72 20 77 68 69 6c 65 20 64 65 6c 65 74 69  ror while deleti
14daf 6e 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67  ng or truncating
14db0 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e   a journal file.
14db1 20 49 74 20 69 73 20 75 6e 6c 69 6b 65 6c 79 2c   It is unlikely,
14db2 0a 20 20 20 20 2a 2a 20 62 75 74 20 63 6f 75 6c  .    ** but coul
14db3 64 20 68 61 70 70 65 6e 2e 20 49 6e 20 74 68 69  d happen. In thi
14db4 73 20 63 61 73 65 20 61 62 61 6e 64 6f 6e 20 70  s case abandon p
14db5 72 6f 63 65 73 73 69 6e 67 20 61 6e 64 20 72 65  rocessing and re
14db6 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 2e 0a  turn the error..
14db7 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69      */.    for(i
14db8 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
14db9 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  K && i<db->nDb; 
14dba 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74 72 65  i++){.      Btre
14dbb 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62  e *pBt = db->aDb
14dbc 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69  [i].pBt;.      i
14dbd 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20  f( pBt ){.      
14dbe 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
14dbf 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77  reeCommitPhaseTw
14dc0 6f 28 70 42 74 29 3b 0a 20 20 20 20 20 20 7d 0a  o(pBt);.      }.
14dc1 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
14dc2 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
14dc3 20 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62       sqlite3Vtab
14dc4 43 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 20 20 20  Commit(db);.    
14dc5 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20  }.  }..  /* The 
14dc6 63 6f 6d 70 6c 65 78 20 63 61 73 65 20 2d 20 54  complex case - T
14dc7 68 65 72 65 20 69 73 20 61 20 6d 75 6c 74 69 2d  here is a multi-
14dc8 66 69 6c 65 20 77 72 69 74 65 2d 74 72 61 6e 73  file write-trans
14dc9 61 63 74 69 6f 6e 20 61 63 74 69 76 65 2e 0a 20  action active.. 
14dca 20 2a 2a 20 54 68 69 73 20 72 65 71 75 69 72 65   ** This require
14dcb 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  s a master journ
14dcc 61 6c 20 66 69 6c 65 20 74 6f 20 65 6e 73 75 72  al file to ensur
14dcd 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  e the transactio
14dce 6e 20 69 73 0a 20 20 2a 2a 20 63 6f 6d 6d 69 74  n is.  ** commit
14dcf 74 65 64 20 61 74 6f 6d 69 63 6c 79 2e 0a 20 20  ted atomicly..  
14dd0 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
14dd1 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 20 20  E_OMIT_DISKIO.  
14dd2 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
14dd3 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 64 62  3_vfs *pVfs = db
14dd4 2d 3e 70 56 66 73 3b 0a 20 20 20 20 69 6e 74 20  ->pVfs;.    int 
14dd5 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20  needSync = 0;.  
14dd6 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 20    char *zMaster 
14dd7 3d 20 30 3b 20 20 20 2f 2a 20 46 69 6c 65 2d 6e  = 0;   /* File-n
14dd8 61 6d 65 20 66 6f 72 20 74 68 65 20 6d 61 73 74  ame for the mast
14dd9 65 72 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20  er journal */.  
14dda 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d    char const *zM
14ddb 61 69 6e 46 69 6c 65 20 3d 20 73 71 6c 69 74 65  ainFile = sqlite
14ddc 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d  3BtreeGetFilenam
14ddd 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74  e(db->aDb[0].pBt
14dde 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
14ddf 69 6c 65 20 2a 70 4d 61 73 74 65 72 20 3d 20 30  ile *pMaster = 0
14de0 3b 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65 74  ;.    i64 offset
14de1 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 53 65   = 0;..    /* Se
14de2 6c 65 63 74 20 61 20 6d 61 73 74 65 72 20 6a 6f  lect a master jo
14de3 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20  urnal file name 
14de4 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20  */.    do {.    
14de5 20 20 75 33 32 20 72 61 6e 64 6f 6d 3b 0a 20 20    u32 random;.  
14de6 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
14de7 28 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20  (zMaster);.     
14de8 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
14de9 65 73 73 28 73 69 7a 65 6f 66 28 72 61 6e 64 6f  ess(sizeof(rando
14dea 6d 29 2c 20 26 72 61 6e 64 6f 6d 29 3b 0a 20 20  m), &random);.  
14deb 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 73 71      zMaster = sq
14dec 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
14ded 20 22 25 73 2d 6d 6a 25 30 38 58 22 2c 20 7a 4d   "%s-mj%08X", zM
14dee 61 69 6e 46 69 6c 65 2c 20 72 61 6e 64 6f 6d 26  ainFile, random&
14def 30 78 37 66 66 66 66 66 66 66 29 3b 0a 20 20 20  0x7fffffff);.   
14df0 20 20 20 69 66 28 20 21 7a 4d 61 73 74 65 72 20     if( !zMaster 
14df1 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
14df2 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
14df3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63        }.      rc
14df4 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65   = sqlite3OsAcce
14df5 73 73 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72  ss(pVfs, zMaster
14df6 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  , SQLITE_ACCESS_
14df7 45 58 49 53 54 53 29 3b 0a 20 20 20 20 7d 77 68  EXISTS);.    }wh
14df8 69 6c 65 28 20 72 63 3d 3d 31 20 29 3b 0a 20 20  ile( rc==1 );.  
14df9 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20    if( rc!=0 ){. 
14dfa 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
14dfb 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20  _IOERR_NOMEM;.  
14dfc 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f    }else{.      /
14dfd 2a 20 4f 70 65 6e 20 74 68 65 20 6d 61 73 74 65  * Open the maste
14dfe 72 20 6a 6f 75 72 6e 61 6c 2e 20 2a 2f 0a 20 20  r journal. */.  
14dff 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
14e00 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 70 56 66  OsOpenMalloc(pVf
14e01 73 2c 20 7a 4d 61 73 74 65 72 2c 20 26 70 4d 61  s, zMaster, &pMa
14e02 73 74 65 72 2c 20 0a 20 20 20 20 20 20 20 20 20  ster, .         
14e03 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
14e04 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50  DWRITE|SQLITE_OP
14e05 45 4e 5f 43 52 45 41 54 45 7c 0a 20 20 20 20 20  EN_CREATE|.     
14e06 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
14e07 5f 45 58 43 4c 55 53 49 56 45 7c 53 51 4c 49 54  _EXCLUSIVE|SQLIT
14e08 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f  E_OPEN_MASTER_JO
14e09 55 52 4e 41 4c 2c 20 30 0a 20 20 20 20 20 20 29  URNAL, 0.      )
14e0a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
14e0b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
14e0c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
14e0d 72 65 65 28 7a 4d 61 73 74 65 72 29 3b 0a 20 20  ree(zMaster);.  
14e0e 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
14e0f 20 20 20 7d 0a 20 0a 20 20 20 20 2f 2a 20 57 72     }. .    /* Wr
14e10 69 74 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  ite the name of 
14e11 65 61 63 68 20 64 61 74 61 62 61 73 65 20 66 69  each database fi
14e12 6c 65 20 69 6e 20 74 68 65 20 74 72 61 6e 73 61  le in the transa
14e13 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 6e  ction into the n
14e14 65 77 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65 72  ew.    ** master
14e15 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49   journal file. I
14e16 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
14e17 73 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20  s at this point 
14e18 63 6c 6f 73 65 0a 20 20 20 20 2a 2a 20 61 6e 64  close.    ** and
14e19 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74   delete the mast
14e1a 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  er journal file.
14e1b 20 41 6c 6c 20 74 68 65 20 69 6e 64 69 76 69 64   All the individ
14e1c 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ual journal file
14e1d 73 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c 20 68  s.    ** still h
14e1e 61 76 65 20 27 6e 75 6c 6c 27 20 61 73 20 74 68  ave 'null' as th
14e1f 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
14e20 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20 74 68 65   pointer, so the
14e21 79 20 77 69 6c 6c 20 72 6f 6c 6c 0a 20 20 20 20  y will roll.    
14e22 2a 2a 20 62 61 63 6b 20 69 6e 64 65 70 65 6e 64  ** back independ
14e23 65 6e 74 6c 79 20 69 66 20 61 20 66 61 69 6c 75  ently if a failu
14e24 72 65 20 6f 63 63 75 72 73 2e 0a 20 20 20 20 2a  re occurs..    *
14e25 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  /.    for(i=0; i
14e26 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
14e27 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74        Btree *pBt
14e28 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
14e29 74 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d  t;.      if( i==
14e2a 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20 20  1 ) continue;   
14e2b 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 20 54 45  /* Ignore the TE
14e2c 4d 50 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  MP database */. 
14e2d 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
14e2e 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70  BtreeIsInTrans(p
14e2f 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 63  Bt) ){.        c
14e30 68 61 72 20 63 6f 6e 73 74 20 2a 7a 46 69 6c 65  har const *zFile
14e31 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
14e32 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 70 42  etJournalname(pB
14e33 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  t);.        if( 
14e34 7a 46 69 6c 65 5b 30 5d 3d 3d 30 20 29 20 63 6f  zFile[0]==0 ) co
14e35 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 49 67 6e 6f  ntinue;  /* Igno
14e36 72 65 20 3a 6d 65 6d 6f 72 79 3a 20 64 61 74 61  re :memory: data
14e37 62 61 73 65 73 20 2a 2f 0a 20 20 20 20 20 20 20  bases */.       
14e38 20 69 66 28 20 21 6e 65 65 64 53 79 6e 63 20 26   if( !needSync &
14e39 26 20 21 73 71 6c 69 74 65 33 42 74 72 65 65 53  & !sqlite3BtreeS
14e3a 79 6e 63 44 69 73 61 62 6c 65 64 28 70 42 74 29  yncDisabled(pBt)
14e3b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 65   ){.          ne
14e3c 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20  edSync = 1;.    
14e3d 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63      }.        rc
14e3e 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
14e3f 65 28 70 4d 61 73 74 65 72 2c 20 7a 46 69 6c 65  e(pMaster, zFile
14e40 2c 20 73 74 72 6c 65 6e 28 7a 46 69 6c 65 29 2b  , strlen(zFile)+
14e41 31 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 20 20  1, offset);.    
14e42 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20 73 74      offset += st
14e43 72 6c 65 6e 28 7a 46 69 6c 65 29 2b 31 3b 0a 20  rlen(zFile)+1;. 
14e44 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
14e45 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
14e46 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43        sqlite3OsC
14e47 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65 72  loseFree(pMaster
14e48 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
14e49 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66  ite3OsDelete(pVf
14e4a 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a  s, zMaster, 0);.
14e4b 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
14e4c 33 5f 66 72 65 65 28 7a 4d 61 73 74 65 72 29 3b  3_free(zMaster);
14e4d 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
14e4e 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a  n rc;.        }.
14e4f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
14e50 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 6d     /* Sync the m
14e51 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
14e52 6c 65 2e 20 49 66 20 74 68 65 20 49 4f 43 41 50  le. If the IOCAP
14e53 5f 53 45 51 55 45 4e 54 49 41 4c 20 64 65 76 69  _SEQUENTIAL devi
14e54 63 65 0a 20 20 20 20 2a 2a 20 66 6c 61 67 20 69  ce.    ** flag i
14e55 73 20 73 65 74 20 74 68 69 73 20 69 73 20 6e 6f  s set this is no
14e56 74 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20  t required..    
14e57 2a 2f 0a 20 20 20 20 7a 4d 61 69 6e 46 69 6c 65  */.    zMainFile
14e58 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
14e59 65 74 44 69 72 6e 61 6d 65 28 64 62 2d 3e 61 44  etDirname(db->aD
14e5a 62 5b 30 5d 2e 70 42 74 29 3b 0a 20 20 20 20 69  b[0].pBt);.    i
14e5b 66 28 20 28 6e 65 65 64 53 79 6e 63 20 0a 20 20  f( (needSync .  
14e5c 20 20 20 26 26 20 28 30 3d 3d 28 73 71 6c 69 74     && (0==(sqlit
14e5d 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63  e3OsDeviceCharac
14e5e 74 65 72 69 73 74 69 63 73 28 70 4d 61 73 74 65  teristics(pMaste
14e5f 72 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  r)&SQLITE_IOCAP_
14e60 53 45 51 55 45 4e 54 49 41 4c 29 29 0a 20 20 20  SEQUENTIAL)).   
14e61 20 20 26 26 20 28 72 63 3d 73 71 6c 69 74 65 33    && (rc=sqlite3
14e62 4f 73 53 79 6e 63 28 70 4d 61 73 74 65 72 2c 20  OsSync(pMaster, 
14e63 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d  SQLITE_SYNC_NORM
14e64 41 4c 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 29  AL))!=SQLITE_OK)
14e65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
14e66 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d 61  3OsCloseFree(pMa
14e67 73 74 65 72 29 3b 0a 20 20 20 20 20 20 73 71 6c  ster);.      sql
14e68 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66  ite3OsDelete(pVf
14e69 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a  s, zMaster, 0);.
14e6a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
14e6b 65 65 28 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20  ee(zMaster);.   
14e6c 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
14e6d 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 79 6e 63    }..    /* Sync
14e6e 20 61 6c 6c 20 74 68 65 20 64 62 20 66 69 6c 65   all the db file
14e6f 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 74 68  s involved in th
14e70 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54  e transaction. T
14e71 68 65 20 73 61 6d 65 20 63 61 6c 6c 0a 20 20 20  he same call.   
14e72 20 2a 2a 20 73 65 74 73 20 74 68 65 20 6d 61 73   ** sets the mas
14e73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e  ter journal poin
14e74 74 65 72 20 69 6e 20 65 61 63 68 20 69 6e 64 69  ter in each indi
14e75 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 2e 20  vidual journal. 
14e76 49 66 0a 20 20 20 20 2a 2a 20 61 6e 20 65 72 72  If.    ** an err
14e77 6f 72 20 6f 63 63 75 72 73 20 68 65 72 65 2c 20  or occurs here, 
14e78 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68  do not delete th
14e79 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
14e7a 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20   file..    **.  
14e7b 20 20 2a 2a 20 49 66 20 74 68 65 20 65 72 72 6f    ** If the erro
14e7c 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20  r occurs during 
14e7d 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 74  the first call t
14e7e 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33  o.    ** sqlite3
14e7f 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
14e80 4f 6e 65 28 29 2c 20 74 68 65 6e 20 74 68 65 72  One(), then ther
14e81 65 20 69 73 20 61 20 63 68 61 6e 63 65 20 74 68  e is a chance th
14e82 61 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 6d 61  at the.    ** ma
14e83 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
14e84 65 20 77 69 6c 6c 20 62 65 20 6f 72 70 68 61 6e  e will be orphan
14e85 65 64 2e 20 42 75 74 20 77 65 20 63 61 6e 6e 6f  ed. But we canno
14e86 74 20 64 65 6c 65 74 65 20 69 74 2c 0a 20 20 20  t delete it,.   
14e87 20 2a 2a 20 69 6e 20 63 61 73 65 20 74 68 65 20   ** in case the 
14e88 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
14e89 69 6c 65 20 6e 61 6d 65 20 77 61 73 20 77 72 69  ile name was wri
14e8a 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f  tten into the jo
14e8b 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69 6c  urnal.    ** fil
14e8c 65 20 62 65 66 6f 72 65 20 74 68 65 20 66 61 69  e before the fai
14e8d 6c 75 72 65 20 6f 63 63 75 72 65 64 2e 0a 20 20  lure occured..  
14e8e 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30    */.    for(i=0
14e8f 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ; rc==SQLITE_OK 
14e90 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  && i<db->nDb; i+
14e91 2b 29 7b 20 0a 20 20 20 20 20 20 42 74 72 65 65  +){ .      Btree
14e92 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b   *pBt = db->aDb[
14e93 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66  i].pBt;.      if
14e94 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20  ( pBt ){.       
14e95 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
14e96 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  eeCommitPhaseOne
14e97 28 70 42 74 2c 20 7a 4d 61 73 74 65 72 29 3b 0a  (pBt, zMaster);.
14e98 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
14e99 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65    sqlite3OsClose
14e9a 46 72 65 65 28 70 4d 61 73 74 65 72 29 3b 0a 20  Free(pMaster);. 
14e9b 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
14e9c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71  E_OK ){.      sq
14e9d 6c 69 74 65 33 5f 66 72 65 65 28 7a 4d 61 73 74  lite3_free(zMast
14e9e 65 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  er);.      retur
14e9f 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  n rc;.    }..   
14ea0 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 6d   /* Delete the m
14ea1 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
14ea2 6c 65 2e 20 54 68 69 73 20 63 6f 6d 6d 69 74 73  le. This commits
14ea3 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
14ea4 2e 20 41 66 74 65 72 0a 20 20 20 20 2a 2a 20 64  . After.    ** d
14ea5 6f 69 6e 67 20 74 68 69 73 20 74 68 65 20 64 69  oing this the di
14ea6 72 65 63 74 6f 72 79 20 69 73 20 73 79 6e 63 65  rectory is synce
14ea7 64 20 61 67 61 69 6e 20 62 65 66 6f 72 65 20 61  d again before a
14ea8 6e 79 20 69 6e 64 69 76 69 64 75 61 6c 0a 20 20  ny individual.  
14ea9 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e    ** transaction
14eaa 20 66 69 6c 65 73 20 61 72 65 20 64 65 6c 65 74   files are delet
14eab 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72  ed..    */.    r
14eac 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c  c = sqlite3OsDel
14ead 65 74 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 65  ete(pVfs, zMaste
14eae 72 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  r, 1);.    sqlit
14eaf 65 33 5f 66 72 65 65 28 7a 4d 61 73 74 65 72 29  e3_free(zMaster)
14eb0 3b 0a 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20  ;.    zMaster = 
14eb1 30 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b  0;.    if( rc ){
14eb2 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
14eb3 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
14eb4 41 6c 6c 20 66 69 6c 65 73 20 61 6e 64 20 64 69  All files and di
14eb5 72 65 63 74 6f 72 69 65 73 20 68 61 76 65 20 61  rectories have a
14eb6 6c 72 65 61 64 79 20 62 65 65 6e 20 73 79 6e 63  lready been sync
14eb7 65 64 2c 20 73 6f 20 74 68 65 20 66 6f 6c 6c 6f  ed, so the follo
14eb8 77 69 6e 67 0a 20 20 20 20 2a 2a 20 63 61 6c 6c  wing.    ** call
14eb9 73 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65  s to sqlite3Btre
14eba 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28  eCommitPhaseTwo(
14ebb 29 20 61 72 65 20 6f 6e 6c 79 20 63 6c 6f 73 69  ) are only closi
14ebc 6e 67 20 66 69 6c 65 73 20 61 6e 64 0a 20 20 20  ng files and.   
14ebd 20 2a 2a 20 64 65 6c 65 74 69 6e 67 20 6f 72 20   ** deleting or 
14ebe 74 72 75 6e 63 61 74 69 6e 67 20 6a 6f 75 72 6e  truncating journ
14ebf 61 6c 73 2e 20 49 66 20 73 6f 6d 65 74 68 69 6e  als. If somethin
14ec0 67 20 67 6f 65 73 20 77 72 6f 6e 67 20 77 68 69  g goes wrong whi
14ec1 6c 65 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 69  le.    ** this i
14ec2 73 20 68 61 70 70 65 6e 69 6e 67 20 77 65 20 64  s happening we d
14ec3 6f 6e 27 74 20 72 65 61 6c 6c 79 20 63 61 72 65  on't really care
14ec4 2e 20 54 68 65 20 69 6e 74 65 67 72 69 74 79 20  . The integrity 
14ec5 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 72  of the.    ** tr
14ec6 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 6c 72  ansaction is alr
14ec7 65 61 64 79 20 67 75 61 72 61 6e 74 65 65 64 2c  eady guaranteed,
14ec8 20 62 75 74 20 73 6f 6d 65 20 73 74 72 61 79 20   but some stray 
14ec9 27 63 6f 6c 64 27 20 6a 6f 75 72 6e 61 6c 73 0a  'cold' journals.
14eca 20 20 20 20 2a 2a 20 6d 61 79 20 62 65 20 6c 79      ** may be ly
14ecb 69 6e 67 20 61 72 6f 75 6e 64 2e 20 52 65 74 75  ing around. Retu
14ecc 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 63  rning an error c
14ecd 6f 64 65 20 77 6f 6e 27 74 20 68 65 6c 70 20 6d  ode won't help m
14ece 61 74 74 65 72 73 2e 0a 20 20 20 20 2a 2f 0a 20  atters..    */. 
14ecf 20 20 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c     disable_simul
14ed0 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29  ated_io_errors()
14ed1 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 61 75  ;.    sqlite3Fau
14ed2 6c 74 42 65 67 69 6e 42 65 6e 69 67 6e 28 53 51  ltBeginBenign(SQ
14ed3 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43 54  LITE_FAULTINJECT
14ed4 4f 52 5f 4d 41 4c 4c 4f 43 29 3b 0a 20 20 20 20  OR_MALLOC);.    
14ed5 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
14ed6 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20  Db; i++){ .     
14ed7 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62   Btree *pBt = db
14ed8 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20  ->aDb[i].pBt;.  
14ed9 20 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20      if( pBt ){. 
14eda 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74         sqlite3Bt
14edb 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77  reeCommitPhaseTw
14edc 6f 28 70 42 74 29 3b 0a 20 20 20 20 20 20 7d 0a  o(pBt);.      }.
14edd 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
14ede 33 46 61 75 6c 74 45 6e 64 42 65 6e 69 67 6e 28  3FaultEndBenign(
14edf 53 51 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45  SQLITE_FAULTINJE
14ee0 43 54 4f 52 5f 4d 41 4c 4c 4f 43 29 3b 0a 20 20  CTOR_MALLOC);.  
14ee1 20 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74    enable_simulat
14ee2 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a  ed_io_errors();.
14ee3 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62  .    sqlite3Vtab
14ee4 43 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 20 7d 0a  Commit(db);.  }.
14ee5 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e  #endif..  return
14ee6 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54   rc;.}../* .** T
14ee7 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63  his routine chec
14ee8 6b 73 20 74 68 61 74 20 74 68 65 20 73 71 6c 69  ks that the sqli
14ee9 74 65 33 2e 61 63 74 69 76 65 56 64 62 65 43 6e  te3.activeVdbeCn
14eea 74 20 63 6f 75 6e 74 20 76 61 72 69 61 62 6c 65  t count variable
14eeb 0a 2a 2a 20 6d 61 74 63 68 65 73 20 74 68 65 20  .** matches the 
14eec 6e 75 6d 62 65 72 20 6f 66 20 76 64 62 65 27 73  number of vdbe's
14eed 20 69 6e 20 74 68 65 20 6c 69 73 74 20 73 71 6c   in the list sql
14eee 69 74 65 33 2e 70 56 64 62 65 20 74 68 61 74 20  ite3.pVdbe that 
14eef 61 72 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79  are.** currently
14ef0 20 61 63 74 69 76 65 2e 20 41 6e 20 61 73 73 65   active. An asse
14ef1 72 74 69 6f 6e 20 66 61 69 6c 73 20 69 66 20 74  rtion fails if t
14ef2 68 65 20 74 77 6f 20 63 6f 75 6e 74 73 20 64 6f  he two counts do
14ef3 20 6e 6f 74 20 6d 61 74 63 68 2e 0a 2a 2a 20 54   not match..** T
14ef4 68 69 73 20 69 73 20 61 6e 20 69 6e 74 65 72 6e  his is an intern
14ef5 61 6c 20 73 65 6c 66 2d 63 68 65 63 6b 20 6f 6e  al self-check on
14ef6 6c 79 20 2d 20 69 74 20 69 73 20 6e 6f 74 20 61  ly - it is not a
14ef7 6e 20 65 73 73 65 6e 74 69 61 6c 20 70 72 6f 63  n essential proc
14ef8 65 73 73 69 6e 67 0a 2a 2a 20 73 74 65 70 2e 0a  essing.** step..
14ef9 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20  **.** This is a 
14efa 6e 6f 2d 6f 70 20 69 66 20 4e 44 45 42 55 47 20  no-op if NDEBUG 
14efb 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23  is defined..*/.#
14efc 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74  ifndef NDEBUG.st
14efd 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 41  atic void checkA
14efe 63 74 69 76 65 56 64 62 65 43 6e 74 28 73 71 6c  ctiveVdbeCnt(sql
14eff 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 64 62  ite3 *db){.  Vdb
14f00 65 20 2a 70 3b 0a 20 20 69 6e 74 20 63 6e 74 20  e *p;.  int cnt 
14f01 3d 20 30 3b 0a 20 20 70 20 3d 20 64 62 2d 3e 70  = 0;.  p = db->p
14f02 56 64 62 65 3b 0a 20 20 77 68 69 6c 65 28 20 70  Vdbe;.  while( p
14f03 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6d   ){.    if( p->m
14f04 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43  agic==VDBE_MAGIC
14f05 5f 52 55 4e 20 26 26 20 70 2d 3e 70 63 3e 3d 30  _RUN && p->pc>=0
14f06 20 29 7b 0a 20 20 20 20 20 20 63 6e 74 2b 2b 3b   ){.      cnt++;
14f07 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70  .    }.    p = p
14f08 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 61  ->pNext;.  }.  a
14f09 73 73 65 72 74 28 20 63 6e 74 3d 3d 64 62 2d 3e  ssert( cnt==db->
14f0a 61 63 74 69 76 65 56 64 62 65 43 6e 74 20 29 3b  activeVdbeCnt );
14f0b 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65  .}.#else.#define
14f0c 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 65   checkActiveVdbe
14f0d 43 6e 74 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f  Cnt(x).#endif../
14f0e 2a 0a 2a 2a 20 46 6f 72 20 65 76 65 72 79 20 42  *.** For every B
14f0f 74 72 65 65 20 74 68 61 74 20 69 6e 20 64 61 74  tree that in dat
14f10 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
14f11 20 64 62 20 77 68 69 63 68 20 0a 2a 2a 20 68 61   db which .** ha
14f12 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2c  s been modified,
14f13 20 22 74 72 69 70 22 20 6f 72 20 69 6e 76 61 6c   "trip" or inval
14f14 69 64 61 74 65 20 65 61 63 68 20 63 75 72 73 6f  idate each curso
14f15 72 20 69 6e 0a 2a 2a 20 74 68 61 74 20 42 74 72  r in.** that Btr
14f16 65 65 20 6d 69 67 68 74 20 68 61 76 65 20 62 65  ee might have be
14f17 65 6e 20 6d 6f 64 69 66 69 65 64 20 73 6f 20 74  en modified so t
14f18 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 0a 2a  hat the cursor.*
14f19 2a 20 63 61 6e 20 6e 65 76 65 72 20 62 65 20 75  * can never be u
14f1a 73 65 64 20 61 67 61 69 6e 2e 20 20 54 68 69 73  sed again.  This
14f1b 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 61 20   happens when a 
14f1c 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 2a 20 6f 63 63  rollback.*** occ
14f1d 75 72 73 2e 20 20 57 65 20 68 61 76 65 20 74 6f  urs.  We have to
14f1e 20 74 72 69 70 20 61 6c 6c 20 74 68 65 20 6f 74   trip all the ot
14f1f 68 65 72 20 63 75 72 73 6f 72 73 2c 20 65 76 65  her cursors, eve
14f20 6e 0a 2a 2a 20 63 75 72 73 6f 72 20 66 72 6f 6d  n.** cursor from
14f21 20 6f 74 68 65 72 20 56 4d 73 20 69 6e 20 64 69   other VMs in di
14f22 66 66 65 72 65 6e 74 20 64 61 74 61 62 61 73 65  fferent database
14f23 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a   connections,.**
14f24 20 73 6f 20 74 68 61 74 20 6e 6f 6e 65 20 6f 66   so that none of
14f25 20 74 68 65 6d 20 74 72 79 20 74 6f 20 75 73 65   them try to use
14f26 20 74 68 65 20 64 61 74 61 20 61 74 20 77 68 69   the data at whi
14f27 63 68 20 74 68 65 79 0a 2a 2a 20 77 65 72 65 20  ch they.** were 
14f28 70 6f 69 6e 74 69 6e 67 20 61 6e 64 20 77 68 69  pointing and whi
14f29 63 68 20 6e 6f 77 20 6d 61 79 20 68 61 76 65 20  ch now may have 
14f2a 62 65 65 6e 20 63 68 61 6e 67 65 64 20 64 75 65  been changed due
14f2b 0a 2a 2a 20 74 6f 20 74 68 65 20 72 6f 6c 6c 62  .** to the rollb
14f2c 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 6d 65 6d  ack..**.** Remem
14f2d 62 65 72 20 74 68 61 74 20 61 20 72 6f 6c 6c 62  ber that a rollb
14f2e 61 63 6b 20 63 61 6e 20 64 65 6c 65 74 65 20 74  ack can delete t
14f2f 61 62 6c 65 73 20 63 6f 6d 70 6c 65 74 65 20 61  ables complete a
14f30 6e 64 0a 2a 2a 20 72 65 6f 72 64 65 72 20 72 6f  nd.** reorder ro
14f31 6f 74 70 61 67 65 73 2e 20 20 53 6f 20 69 74 20  otpages.  So it 
14f32 69 73 20 6e 6f 74 20 73 75 66 66 69 63 69 65 6e  is not sufficien
14f33 74 20 6a 75 73 74 20 74 6f 20 73 61 76 65 0a 2a  t just to save.*
14f34 2a 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74  * the state of t
14f35 68 65 20 63 75 72 73 6f 72 2e 20 20 57 65 20 68  he cursor.  We h
14f36 61 76 65 20 74 6f 20 69 6e 76 61 6c 69 64 61 74  ave to invalidat
14f37 65 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20  e the cursor.** 
14f38 73 6f 20 74 68 61 74 20 69 74 20 69 73 20 6e 65  so that it is ne
14f39 76 65 72 20 75 73 65 64 20 61 67 61 69 6e 2e 0a  ver used again..
14f3a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  */.static void i
14f3b 6e 76 61 6c 69 64 61 74 65 43 75 72 73 6f 72 73  nvalidateCursors
14f3c 4f 6e 4d 6f 64 69 66 69 65 64 42 74 72 65 65 73  OnModifiedBtrees
14f3d 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
14f3e 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
14f3f 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
14f40 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70  +){.    Btree *p
14f41 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
14f42 74 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20  t;.    if( p && 
14f43 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e  sqlite3BtreeIsIn
14f44 54 72 61 6e 73 28 70 29 20 29 7b 0a 20 20 20 20  Trans(p) ){.    
14f45 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 72    sqlite3BtreeTr
14f46 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 70 2c 20  ipAllCursors(p, 
14f47 53 51 4c 49 54 45 5f 41 42 4f 52 54 29 3b 0a 20  SQLITE_ABORT);. 
14f48 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
14f49 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
14f4a 73 20 63 61 6c 6c 65 64 20 74 68 65 20 77 68 65  s called the whe
14f4b 6e 20 61 20 56 44 42 45 20 74 72 69 65 73 20 74  n a VDBE tries t
14f4c 6f 20 68 61 6c 74 2e 20 20 49 66 20 74 68 65 20  o halt.  If the 
14f4d 56 44 42 45 0a 2a 2a 20 68 61 73 20 6d 61 64 65  VDBE.** has made
14f4e 20 63 68 61 6e 67 65 73 20 61 6e 64 20 69 73 20   changes and is 
14f4f 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f  in autocommit mo
14f50 64 65 2c 20 74 68 65 6e 20 63 6f 6d 6d 69 74 20  de, then commit 
14f51 74 68 6f 73 65 0a 2a 2a 20 63 68 61 6e 67 65 73  those.** changes
14f52 2e 20 20 49 66 20 61 20 72 6f 6c 6c 62 61 63 6b  .  If a rollback
14f53 20 69 73 20 6e 65 65 64 65 64 2c 20 74 68 65 6e   is needed, then
14f54 20 64 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b   do the rollback
14f55 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
14f56 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79  tine is the only
14f57 20 77 61 79 20 74 6f 20 6d 6f 76 65 20 74 68 65   way to move the
14f58 20 73 74 61 74 65 20 6f 66 20 61 20 56 4d 20 66   state of a VM f
14f59 72 6f 6d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 41  rom.** SQLITE_MA
14f5a 47 49 43 5f 52 55 4e 20 74 6f 20 53 51 4c 49 54  GIC_RUN to SQLIT
14f5b 45 5f 4d 41 47 49 43 5f 48 41 4c 54 2e 20 20 49  E_MAGIC_HALT.  I
14f5c 74 20 69 73 20 68 61 72 6d 6c 65 73 73 20 74 6f  t is harmless to
14f5d 0a 2a 2a 20 63 61 6c 6c 20 74 68 69 73 20 6f 6e  .** call this on
14f5e 20 61 20 56 4d 20 74 68 61 74 20 69 73 20 69 6e   a VM that is in
14f5f 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 47 49   the SQLITE_MAGI
14f60 43 5f 48 41 4c 54 20 73 74 61 74 65 2e 0a 2a 2a  C_HALT state..**
14f61 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72  .** Return an er
14f62 72 6f 72 20 63 6f 64 65 2e 20 20 49 66 20 74 68  ror code.  If th
14f63 65 20 63 6f 6d 6d 69 74 20 63 6f 75 6c 64 20 6e  e commit could n
14f64 6f 74 20 63 6f 6d 70 6c 65 74 65 20 62 65 63 61  ot complete beca
14f65 75 73 65 20 6f 66 0a 2a 2a 20 6c 6f 63 6b 20 63  use of.** lock c
14f66 6f 6e 74 65 6e 74 69 6f 6e 2c 20 72 65 74 75 72  ontention, retur
14f67 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20  n SQLITE_BUSY.  
14f68 49 66 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69  If SQLITE_BUSY i
14f69 73 20 72 65 74 75 72 6e 65 64 2c 20 69 74 0a 2a  s returned, it.*
14f6a 2a 20 6d 65 61 6e 73 20 74 68 65 20 63 6c 6f 73  * means the clos
14f6b 65 20 64 69 64 20 6e 6f 74 20 68 61 70 70 65 6e  e did not happen
14f6c 20 61 6e 64 20 6e 65 65 64 73 20 74 6f 20 62 65   and needs to be
14f6d 20 72 65 70 65 61 74 65 64 2e 0a 2a 2f 0a 53 51   repeated..*/.SQ
14f6e 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
14f6f 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74   sqlite3VdbeHalt
14f70 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c  (Vdbe *p){.  sql
14f71 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62  ite3 *db = p->db
14f72 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  ;.  int i;.  int
14f73 20 28 2a 78 46 75 6e 63 29 28 42 74 72 65 65 20   (*xFunc)(Btree 
14f74 2a 70 42 74 29 20 3d 20 30 3b 20 20 2f 2a 20 46  *pBt) = 0;  /* F
14f75 75 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c 6c 20  unction to call 
14f76 6f 6e 20 65 61 63 68 20 62 74 72 65 65 20 62 61  on each btree ba
14f77 63 6b 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 69  ckend */.  int i
14f78 73 53 70 65 63 69 61 6c 45 72 72 6f 72 3b 20 20  sSpecialError;  
14f79 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74            /* Set
14f7a 20 74 6f 20 74 72 75 65 20 69 66 20 53 51 4c 49   to true if SQLI
14f7b 54 45 5f 4e 4f 4d 45 4d 20 6f 72 20 49 4f 45 52  TE_NOMEM or IOER
14f7c 52 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20  R */..  /* This 
14f7d 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e  function contain
14f7e 73 20 74 68 65 20 6c 6f 67 69 63 20 74 68 61 74  s the logic that
14f7f 20 64 65 74 65 72 6d 69 6e 65 73 20 69 66 20 61   determines if a
14f80 20 73 74 61 74 65 6d 65 6e 74 20 6f 72 0a 20 20   statement or.  
14f81 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  ** transaction w
14f82 69 6c 6c 20 62 65 20 63 6f 6d 6d 69 74 74 65 64  ill be committed
14f83 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20   or rolled back 
14f84 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 74  as a result of t
14f85 68 65 0a 20 20 2a 2a 20 65 78 65 63 75 74 69 6f  he.  ** executio
14f86 6e 20 6f 66 20 74 68 69 73 20 76 69 72 74 75 61  n of this virtua
14f87 6c 20 6d 61 63 68 69 6e 65 2e 20 0a 20 20 2a 2a  l machine. .  **
14f88 0a 20 20 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20  .  ** If any of 
14f89 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 72  the following er
14f8a 72 6f 72 73 20 6f 63 63 75 72 3a 0a 20 20 2a 2a  rors occur:.  **
14f8b 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  .  **     SQLITE
14f8c 5f 4e 4f 4d 45 4d 0a 20 20 2a 2a 20 20 20 20 20  _NOMEM.  **     
14f8d 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20 2a  SQLITE_IOERR.  *
14f8e 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 46 55 4c  *     SQLITE_FUL
14f8f 4c 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54  L.  **     SQLIT
14f90 45 5f 49 4e 54 45 52 52 55 50 54 0a 20 20 2a 2a  E_INTERRUPT.  **
14f91 0a 20 20 2a 2a 20 54 68 65 6e 20 74 68 65 20 69  .  ** Then the i
14f92 6e 74 65 72 6e 61 6c 20 63 61 63 68 65 20 6d 69  nternal cache mi
14f93 67 68 74 20 68 61 76 65 20 62 65 65 6e 20 6c 65  ght have been le
14f94 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69  ft in an inconsi
14f95 73 74 65 6e 74 0a 20 20 2a 2a 20 73 74 61 74 65  stent.  ** state
14f96 2e 20 20 57 65 20 6e 65 65 64 20 74 6f 20 72 6f  .  We need to ro
14f97 6c 6c 62 61 63 6b 20 74 68 65 20 73 74 61 74 65  llback the state
14f98 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
14f99 2c 20 69 66 20 74 68 65 72 65 20 69 73 0a 20 20  , if there is.  
14f9a 2a 2a 20 6f 6e 65 2c 20 6f 72 20 74 68 65 20 63  ** one, or the c
14f9b 6f 6d 70 6c 65 74 65 20 74 72 61 6e 73 61 63 74  omplete transact
14f9c 69 6f 6e 20 69 66 20 74 68 65 72 65 20 69 73 20  ion if there is 
14f9d 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61  no statement tra
14f9e 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 0a  nsaction..  */..
14f9f 20 20 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c    if( p->db->mal
14fa0 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
14fa1 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f   p->rc = SQLITE_
14fa2 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 63 6c 6f  NOMEM;.  }.  clo
14fa3 73 65 41 6c 6c 43 75 72 73 6f 72 73 45 78 63 65  seAllCursorsExce
14fa4 70 74 41 63 74 69 76 65 56 74 61 62 73 28 70 29  ptActiveVtabs(p)
14fa5 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63  ;.  if( p->magic
14fa6 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e  !=VDBE_MAGIC_RUN
14fa7 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
14fa8 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
14fa9 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 43  checkActiveVdbeC
14faa 6e 74 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 4e 6f  nt(db);..  /* No
14fab 20 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62   commit or rollb
14fac 61 63 6b 20 6e 65 65 64 65 64 20 69 66 20 74 68  ack needed if th
14fad 65 20 70 72 6f 67 72 61 6d 20 6e 65 76 65 72 20  e program never 
14fae 73 74 61 72 74 65 64 20 2a 2f 0a 20 20 69 66 28  started */.  if(
14faf 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20   p->pc>=0 ){.   
14fb0 20 69 6e 74 20 6d 72 63 3b 20 20 20 2f 2a 20 50   int mrc;   /* P
14fb1 72 69 6d 61 72 79 20 65 72 72 6f 72 20 63 6f 64  rimary error cod
14fb2 65 20 66 72 6f 6d 20 70 2d 3e 72 63 20 2a 2f 0a  e from p->rc */.
14fb3 0a 20 20 20 20 2f 2a 20 4c 6f 63 6b 20 61 6c 6c  .    /* Lock all
14fb4 20 62 74 72 65 65 73 20 75 73 65 64 20 62 79 20   btrees used by 
14fb5 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  the statement */
14fb6 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
14fb7 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72  eMutexArrayEnter
14fb8 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 0a 20  (&p->aMutex);.. 
14fb9 20 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20     /* Check for 
14fba 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63 69  one of the speci
14fbb 61 6c 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20  al errors */.   
14fbc 20 6d 72 63 20 3d 20 70 2d 3e 72 63 20 26 20 30   mrc = p->rc & 0
14fbd 78 66 66 3b 0a 20 20 20 20 69 73 53 70 65 63 69  xff;.    isSpeci
14fbe 61 6c 45 72 72 6f 72 20 3d 20 6d 72 63 3d 3d 53  alError = mrc==S
14fbf 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 6d  QLITE_NOMEM || m
14fc0 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc==SQLITE_IOERR
14fc1 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
14fc2 20 20 20 20 20 20 7c 7c 20 6d 72 63 3d 3d 53 51        || mrc==SQ
14fc3 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 7c  LITE_INTERRUPT |
14fc4 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55  | mrc==SQLITE_FU
14fc5 4c 4c 3b 0a 20 20 20 20 69 66 28 20 69 73 53 70  LL;.    if( isSp
14fc6 65 63 69 61 6c 45 72 72 6f 72 20 29 7b 0a 20 20  ecialError ){.  
14fc7 20 20 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70      /* This loop
14fc8 20 64 6f 65 73 20 73 74 61 74 69 63 20 61 6e 61   does static ana
14fc9 6c 79 73 69 73 20 6f 66 20 74 68 65 20 71 75 65  lysis of the que
14fca 72 79 20 74 6f 20 73 65 65 20 77 68 69 63 68 20  ry to see which 
14fcb 6f 66 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  of the.      ** 
14fcc 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65 20  following three 
14fcd 63 61 74 65 67 6f 72 69 65 73 20 69 74 20 66 61  categories it fa
14fce 6c 6c 73 20 69 6e 74 6f 3a 0a 20 20 20 20 20 20  lls into:.      
14fcf 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20  **.      **     
14fd0 52 65 61 64 2d 6f 6e 6c 79 0a 20 20 20 20 20 20  Read-only.      
14fd1 2a 2a 20 20 20 20 20 51 75 65 72 79 20 77 69 74  **     Query wit
14fd2 68 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72  h statement jour
14fd3 6e 61 6c 0a 20 20 20 20 20 20 2a 2a 20 20 20 20  nal.      **    
14fd4 20 51 75 65 72 79 20 77 69 74 68 6f 75 74 20 73   Query without s
14fd5 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c  tatement journal
14fd6 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
14fd7 2a 2a 20 57 65 20 63 6f 75 6c 64 20 64 6f 20 73  ** We could do s
14fd8 6f 6d 65 74 68 69 6e 67 20 6d 6f 72 65 20 65 6c  omething more el
14fd9 65 67 61 6e 74 20 74 68 61 6e 20 74 68 69 73 20  egant than this 
14fda 73 74 61 74 69 63 20 61 6e 61 6c 79 73 69 73 20  static analysis 
14fdb 28 69 2e 65 2e 0a 20 20 20 20 20 20 2a 2a 20 73  (i.e..      ** s
14fdc 74 6f 72 65 20 74 68 65 20 74 79 70 65 20 6f 66  tore the type of
14fdd 20 71 75 65 72 79 20 61 73 20 70 61 72 74 20 6f   query as part o
14fde 66 20 74 68 65 20 63 6f 6d 70 6c 69 61 74 69 6f  f the compliatio
14fdf 6e 20 70 68 61 73 65 29 2c 20 62 75 74 20 0a 20  n phase), but . 
14fe0 20 20 20 20 20 2a 2a 20 68 61 6e 64 6c 69 6e 67       ** handling
14fe1 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 49 4f 20   malloc() or IO 
14fe2 66 61 69 6c 75 72 65 20 69 73 20 61 20 66 61 69  failure is a fai
14fe3 72 6c 79 20 6f 62 73 63 75 72 65 20 65 64 67 65  rly obscure edge
14fe4 20 63 61 73 65 20 73 6f 20 0a 20 20 20 20 20 20   case so .      
14fe5 2a 2a 20 74 68 69 73 20 69 73 20 70 72 6f 62 61  ** this is proba
14fe6 62 6c 79 20 65 61 73 69 65 72 2e 20 54 6f 64 6f  bly easier. Todo
14fe7 3a 20 4d 69 67 68 74 20 62 65 20 61 6e 20 6f 70  : Might be an op
14fe8 70 6f 72 74 75 6e 69 74 79 20 74 6f 20 72 65 64  portunity to red
14fe9 75 63 65 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f  uce .      ** co
14fea 64 65 20 73 69 7a 65 20 61 20 76 65 72 79 20 73  de size a very s
14feb 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20 74 68 6f 75  mall amount thou
14fec 67 68 2e 2e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  gh....      */. 
14fed 20 20 20 20 20 69 6e 74 20 6e 6f 74 52 65 61 64       int notRead
14fee 4f 6e 6c 79 20 3d 20 30 3b 0a 20 20 20 20 20 20  Only = 0;.      
14fef 69 6e 74 20 69 73 53 74 61 74 65 6d 65 6e 74 20  int isStatement 
14ff0 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72  = 0;.      asser
14ff1 74 28 70 2d 3e 61 4f 70 20 7c 7c 20 70 2d 3e 6e  t(p->aOp || p->n
14ff2 4f 70 3d 3d 30 29 3b 0a 20 20 20 20 20 20 66 6f  Op==0);.      fo
14ff3 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b  r(i=0; i<p->nOp;
14ff4 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 20 20   i++){ .        
14ff5 73 77 69 74 63 68 28 20 70 2d 3e 61 4f 70 5b 69  switch( p->aOp[i
14ff6 5d 2e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20  ].opcode ){.    
14ff7 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 54 72        case OP_Tr
14ff8 61 6e 73 61 63 74 69 6f 6e 3a 0a 20 20 20 20 20  ansaction:.     
14ff9 20 20 20 20 20 20 20 6e 6f 74 52 65 61 64 4f 6e         notReadOn
14ffa 6c 79 20 7c 3d 20 70 2d 3e 61 4f 70 5b 69 5d 2e  ly |= p->aOp[i].
14ffb 70 32 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  p2;.            
14ffc 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
14ffd 20 63 61 73 65 20 4f 50 5f 53 74 61 74 65 6d 65   case OP_Stateme
14ffe 6e 74 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20  nt:.            
14fff 69 73 53 74 61 74 65 6d 65 6e 74 20 3d 20 31 3b  isStatement = 1;
15000 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
15001 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
15002 20 20 20 20 7d 0a 0a 20 20 20 0a 20 20 20 20 20      }..   .     
15003 20 2f 2a 20 49 66 20 74 68 65 20 71 75 65 72 79   /* If the query
15004 20 77 61 73 20 72 65 61 64 2d 6f 6e 6c 79 2c 20   was read-only, 
15005 77 65 20 6e 65 65 64 20 64 6f 20 6e 6f 20 72 6f  we need do no ro
15006 6c 6c 62 61 63 6b 20 61 74 20 61 6c 6c 2e 20 4f  llback at all. O
15007 74 68 65 72 77 69 73 65 2c 0a 20 20 20 20 20 20  therwise,.      
15008 2a 2a 20 70 72 6f 63 65 65 64 20 77 69 74 68 20  ** proceed with 
15009 74 68 65 20 73 70 65 63 69 61 6c 20 68 61 6e 64  the special hand
1500a 6c 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ling..      */. 
1500b 20 20 20 20 20 69 66 28 20 6e 6f 74 52 65 61 64       if( notRead
1500c 4f 6e 6c 79 20 7c 7c 20 6d 72 63 21 3d 53 51 4c  Only || mrc!=SQL
1500d 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 29 7b  ITE_INTERRUPT ){
1500e 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e  .        if( p->
1500f 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc==SQLITE_IOERR
15010 5f 42 4c 4f 43 4b 45 44 20 26 26 20 69 73 53 74  _BLOCKED && isSt
15011 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20  atement ){.     
15012 20 20 20 20 20 78 46 75 6e 63 20 3d 20 73 71 6c       xFunc = sql
15013 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63  ite3BtreeRollbac
15014 6b 53 74 6d 74 3b 0a 20 20 20 20 20 20 20 20 20  kStmt;.         
15015 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f   p->rc = SQLITE_
15016 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 7d 20  BUSY;.        } 
15017 65 6c 73 65 20 69 66 28 20 28 6d 72 63 3d 3d 53  else if( (mrc==S
15018 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 6d  QLITE_NOMEM || m
15019 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 29  rc==SQLITE_FULL)
1501a 20 26 26 20 69 73 53 74 61 74 65 6d 65 6e 74 20   && isStatement 
1501b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 78 46 75  ){.          xFu
1501c 6e 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  nc = sqlite3Btre
1501d 65 52 6f 6c 6c 62 61 63 6b 53 74 6d 74 3b 0a 20  eRollbackStmt;. 
1501e 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
1501f 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 61 72          /* We ar
15020 65 20 66 6f 72 63 65 64 20 74 6f 20 72 6f 6c 6c  e forced to roll
15021 20 62 61 63 6b 20 74 68 65 20 61 63 74 69 76 65   back the active
15022 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 42 65   transaction. Be
15023 66 6f 72 65 20 64 6f 69 6e 67 0a 20 20 20 20 20  fore doing.     
15024 20 20 20 20 20 2a 2a 20 73 6f 2c 20 61 62 6f 72       ** so, abor
15025 74 20 61 6e 79 20 6f 74 68 65 72 20 73 74 61 74  t any other stat
15026 65 6d 65 6e 74 73 20 74 68 69 73 20 68 61 6e 64  ements this hand
15027 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 68 61 73  le currently has
15028 20 61 63 74 69 76 65 2e 0a 20 20 20 20 20 20 20   active..       
15029 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
1502a 69 6e 76 61 6c 69 64 61 74 65 43 75 72 73 6f 72  invalidateCursor
1502b 73 4f 6e 4d 6f 64 69 66 69 65 64 42 74 72 65 65  sOnModifiedBtree
1502c 73 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20  s(db);.         
1502d 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b   sqlite3Rollback
1502e 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20  All(db);.       
1502f 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69     db->autoCommi
15030 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  t = 1;.        }
15031 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
15032 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20   .    /* If the 
15033 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67  auto-commit flag
15034 20 69 73 20 73 65 74 20 61 6e 64 20 74 68 69 73   is set and this
15035 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 61 63 74   is the only act
15036 69 76 65 20 76 64 62 65 2c 20 74 68 65 6e 0a 20  ive vdbe, then. 
15037 20 20 20 2a 2a 20 77 65 20 64 6f 20 65 69 74 68     ** we do eith
15038 65 72 20 61 20 63 6f 6d 6d 69 74 20 6f 72 20 72  er a commit or r
15039 6f 6c 6c 62 61 63 6b 20 6f 66 20 74 68 65 20 63  ollback of the c
1503a 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69  urrent transacti
1503b 6f 6e 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20  on. .    **.    
1503c 2a 2a 20 4e 6f 74 65 3a 20 54 68 69 73 20 62 6c  ** Note: This bl
1503d 6f 63 6b 20 61 6c 73 6f 20 72 75 6e 73 20 69 66  ock also runs if
1503e 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63   one of the spec
1503f 69 61 6c 20 65 72 72 6f 72 73 20 68 61 6e 64 6c  ial errors handl
15040 65 64 20 0a 20 20 20 20 2a 2a 20 61 62 6f 76 65  ed .    ** above
15041 20 68 61 73 20 6f 63 63 75 72 65 64 2e 20 0a 20   has occured. . 
15042 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62     */.    if( db
15043 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 26 26 20  ->autoCommit && 
15044 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e  db->activeVdbeCn
15045 74 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 69 66  t==1 ){.      if
15046 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f  ( p->rc==SQLITE_
15047 4f 4b 20 7c 7c 20 28 70 2d 3e 65 72 72 6f 72 41  OK || (p->errorA
15048 63 74 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 26  ction==OE_Fail &
15049 26 20 21 69 73 53 70 65 63 69 61 6c 45 72 72 6f  & !isSpecialErro
1504a 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  r) ){.        /*
1504b 20 54 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74   The auto-commit
1504c 20 66 6c 61 67 20 69 73 20 74 72 75 65 2c 20 61   flag is true, a
1504d 6e 64 20 74 68 65 20 76 64 62 65 20 70 72 6f 67  nd the vdbe prog
1504e 72 61 6d 20 77 61 73 20 0a 20 20 20 20 20 20 20  ram was .       
1504f 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 6f   ** successful o
15050 72 20 68 69 74 20 61 6e 20 27 4f 52 20 46 41 49  r hit an 'OR FAI
15051 4c 27 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 54  L' constraint. T
15052 68 69 73 20 6d 65 61 6e 73 20 61 20 63 6f 6d 6d  his means a comm
15053 69 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  it .        ** i
15054 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20  s required..    
15055 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69      */.        i
15056 6e 74 20 72 63 20 3d 20 76 64 62 65 43 6f 6d 6d  nt rc = vdbeComm
15057 69 74 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20  it(db);.        
15058 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42  if( rc==SQLITE_B
15059 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20  USY ){.         
1505a 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74   sqlite3BtreeMut
1505b 65 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d  exArrayLeave(&p-
1505c 3e 61 4d 75 74 65 78 29 3b 0a 20 20 20 20 20 20  >aMutex);.      
1505d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1505e 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20  E_BUSY;.        
1505f 7d 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51  }else if( rc!=SQ
15060 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
15061 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b       p->rc = rc;
15062 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
15063 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62  e3RollbackAll(db
15064 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
15065 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
15066 74 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61  te3CommitInterna
15067 6c 43 68 61 6e 67 65 73 28 64 62 29 3b 0a 20 20  lChanges(db);.  
15068 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
15069 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
1506a 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28  ite3RollbackAll(
1506b 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  db);.      }.   
1506c 20 7d 65 6c 73 65 20 69 66 28 20 21 78 46 75 6e   }else if( !xFun
1506d 63 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  c ){.      if( p
1506e 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ->rc==SQLITE_OK 
1506f 7c 7c 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f  || p->errorActio
15070 6e 3d 3d 4f 45 5f 46 61 69 6c 20 29 7b 0a 20 20  n==OE_Fail ){.  
15071 20 20 20 20 20 20 69 66 28 20 70 2d 3e 6f 70 65        if( p->ope
15072 6e 65 64 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a  nedStatement ){.
15073 20 20 20 20 20 20 20 20 20 20 78 46 75 6e 63 20            xFunc 
15074 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  = sqlite3BtreeCo
15075 6d 6d 69 74 53 74 6d 74 3b 0a 20 20 20 20 20 20  mmitStmt;.      
15076 20 20 7d 20 0a 20 20 20 20 20 20 7d 65 6c 73 65    } .      }else
15077 20 69 66 28 20 70 2d 3e 65 72 72 6f 72 41 63 74   if( p->errorAct
15078 69 6f 6e 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b  ion==OE_Abort ){
15079 0a 20 20 20 20 20 20 20 20 78 46 75 6e 63 20 3d  .        xFunc =
1507a 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c   sqlite3BtreeRol
1507b 6c 62 61 63 6b 53 74 6d 74 3b 0a 20 20 20 20 20  lbackStmt;.     
1507c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1507d 69 6e 76 61 6c 69 64 61 74 65 43 75 72 73 6f 72  invalidateCursor
1507e 73 4f 6e 4d 6f 64 69 66 69 65 64 42 74 72 65 65  sOnModifiedBtree
1507f 73 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 73  s(db);.        s
15080 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c  qlite3RollbackAl
15081 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 64  l(db);.        d
15082 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20  b->autoCommit = 
15083 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
15084 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 78 46  .  .    /* If xF
15085 75 6e 63 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c  unc is not NULL,
15086 20 74 68 65 6e 20 69 74 20 69 73 20 6f 6e 65 20   then it is one 
15087 6f 66 20 73 71 6c 69 74 65 33 42 74 72 65 65 52  of sqlite3BtreeR
15088 6f 6c 6c 62 61 63 6b 53 74 6d 74 20 6f 72 0a 20  ollbackStmt or. 
15089 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72     ** sqlite3Btr
1508a 65 65 43 6f 6d 6d 69 74 53 74 6d 74 2e 20 43 61  eeCommitStmt. Ca
1508b 6c 6c 20 69 74 20 6f 6e 63 65 20 6f 6e 20 65 61  ll it once on ea
1508c 63 68 20 62 61 63 6b 65 6e 64 2e 20 49 66 20 61  ch backend. If a
1508d 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 20  n error occurs. 
1508e 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 72 65     ** and the re
1508f 74 75 72 6e 20 63 6f 64 65 20 69 73 20 73 74 69  turn code is sti
15090 6c 6c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 73 65  ll SQLITE_OK, se
15091 74 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64  t the return cod
15092 65 20 74 6f 20 74 68 65 20 6e 65 77 0a 20 20 20  e to the new.   
15093 20 2a 2a 20 65 72 72 6f 72 20 76 61 6c 75 65 2e   ** error value.
15094 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
15095 72 74 28 21 78 46 75 6e 63 20 7c 7c 0a 20 20 20  rt(!xFunc ||.   
15096 20 20 20 78 46 75 6e 63 3d 3d 73 71 6c 69 74 65     xFunc==sqlite
15097 33 42 74 72 65 65 43 6f 6d 6d 69 74 53 74 6d 74  3BtreeCommitStmt
15098 20 7c 7c 0a 20 20 20 20 20 20 78 46 75 6e 63 3d   ||.      xFunc=
15099 3d 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c  =sqlite3BtreeRol
1509a 6c 62 61 63 6b 53 74 6d 74 0a 20 20 20 20 29 3b  lbackStmt.    );
1509b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 78 46  .    for(i=0; xF
1509c 75 6e 63 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62  unc && i<db->nDb
1509d 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 69  ; i++){ .      i
1509e 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 42 74 72  nt rc;.      Btr
1509f 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44  ee *pBt = db->aD
150a0 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20  b[i].pBt;.      
150a1 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20  if( pBt ){.     
150a2 20 20 20 72 63 20 3d 20 78 46 75 6e 63 28 70 42     rc = xFunc(pB
150a3 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  t);.        if( 
150a4 72 63 20 26 26 20 28 70 2d 3e 72 63 3d 3d 53 51  rc && (p->rc==SQ
150a5 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63  LITE_OK || p->rc
150a6 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  ==SQLITE_CONSTRA
150a7 49 4e 54 29 20 29 7b 0a 20 20 20 20 20 20 20 20  INT) ){.        
150a8 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20    p->rc = rc;.  
150a9 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
150aa 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72  etString(&p->zEr
150ab 72 4d 73 67 2c 20 30 29 3b 0a 20 20 20 20 20 20  rMsg, 0);.      
150ac 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
150ad 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74  }.  .    /* If t
150ae 68 69 73 20 77 61 73 20 61 6e 20 49 4e 53 45 52  his was an INSER
150af 54 2c 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c  T, UPDATE or DEL
150b0 45 54 45 20 61 6e 64 20 74 68 65 20 73 74 61 74  ETE and the stat
150b1 65 6d 65 6e 74 20 77 61 73 20 63 6f 6d 6d 69 74  ement was commit
150b2 74 65 64 2c 20 0a 20 20 20 20 2a 2a 20 73 65 74  ted, .    ** set
150b3 20 74 68 65 20 63 68 61 6e 67 65 20 63 6f 75 6e   the change coun
150b4 74 65 72 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20  ter. .    */.   
150b5 20 69 66 28 20 70 2d 3e 63 68 61 6e 67 65 43 6e   if( p->changeCn
150b6 74 4f 6e 20 26 26 20 70 2d 3e 70 63 3e 3d 30 20  tOn && p->pc>=0 
150b7 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 78 46  ){.      if( !xF
150b8 75 6e 63 20 7c 7c 20 78 46 75 6e 63 3d 3d 73 71  unc || xFunc==sq
150b9 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74  lite3BtreeCommit
150ba 53 74 6d 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Stmt ){.        
150bb 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 68  sqlite3VdbeSetCh
150bc 61 6e 67 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68  anges(db, p->nCh
150bd 61 6e 67 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c  ange);.      }el
150be 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
150bf 74 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 65  te3VdbeSetChange
150c0 73 28 64 62 2c 20 30 29 3b 0a 20 20 20 20 20 20  s(db, 0);.      
150c1 7d 0a 20 20 20 20 20 20 70 2d 3e 6e 43 68 61 6e  }.      p->nChan
150c2 67 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  ge = 0;.    }.  
150c3 0a 20 20 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b  .    /* Rollback
150c4 20 6f 72 20 63 6f 6d 6d 69 74 20 61 6e 79 20 73   or commit any s
150c5 63 68 65 6d 61 20 63 68 61 6e 67 65 73 20 74 68  chema changes th
150c6 61 74 20 6f 63 63 75 72 72 65 64 2e 20 2a 2f 0a  at occurred. */.
150c7 20 20 20 20 69 66 28 20 70 2d 3e 72 63 21 3d 53      if( p->rc!=S
150c8 51 4c 49 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e  QLITE_OK && db->
150c9 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74  flags&SQLITE_Int
150ca 65 72 6e 43 68 61 6e 67 65 73 20 29 7b 0a 20 20  ernChanges ){.  
150cb 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 74      sqlite3Reset
150cc 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64  InternalSchema(d
150cd 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 64 62 2d  b, 0);.      db-
150ce 3e 66 6c 61 67 73 20 3d 20 28 64 62 2d 3e 66 6c  >flags = (db->fl
150cf 61 67 73 20 7c 20 53 51 4c 49 54 45 5f 49 6e 74  ags | SQLITE_Int
150d0 65 72 6e 43 68 61 6e 67 65 73 29 3b 0a 20 20 20  ernChanges);.   
150d1 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61   }..    /* Relea
150d2 73 65 20 74 68 65 20 6c 6f 63 6b 73 20 2a 2f 0a  se the locks */.
150d3 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
150d4 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28  MutexArrayLeave(
150d5 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20 7d  &p->aMutex);.  }
150d6 0a 0a 20 20 2f 2a 20 57 65 20 68 61 76 65 20 73  ..  /* We have s
150d7 75 63 63 65 73 73 66 75 6c 6c 79 20 68 61 6c 74  uccessfully halt
150d8 65 64 20 61 6e 64 20 63 6c 6f 73 65 64 20 74 68  ed and closed th
150d9 65 20 56 4d 2e 20 20 52 65 63 6f 72 64 20 74 68  e VM.  Record th
150da 69 73 20 66 61 63 74 2e 20 2a 2f 0a 20 20 69 66  is fact. */.  if
150db 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20  ( p->pc>=0 ){.  
150dc 20 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65    db->activeVdbe
150dd 43 6e 74 2d 2d 3b 0a 20 20 7d 0a 20 20 70 2d 3e  Cnt--;.  }.  p->
150de 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47  magic = VDBE_MAG
150df 49 43 5f 48 41 4c 54 3b 0a 20 20 63 68 65 63 6b  IC_HALT;.  check
150e0 41 63 74 69 76 65 56 64 62 65 43 6e 74 28 64 62  ActiveVdbeCnt(db
150e1 29 3b 0a 20 20 69 66 28 20 70 2d 3e 64 62 2d 3e  );.  if( p->db->
150e2 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
150e3 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49      p->rc = SQLI
150e4 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20  TE_NOMEM;.  }.. 
150e5 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
150e6 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63  K;.}.../*.** Eac
150e7 68 20 56 44 42 45 20 68 6f 6c 64 73 20 74 68 65  h VDBE holds the
150e8 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 6d   result of the m
150e9 6f 73 74 20 72 65 63 65 6e 74 20 73 71 6c 69 74  ost recent sqlit
150ea 65 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 0a 2a  e3_step() call.*
150eb 2a 20 69 6e 20 70 2d 3e 72 63 2e 20 20 54 68 69  * in p->rc.  Thi
150ec 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74  s routine sets t
150ed 68 61 74 20 72 65 73 75 6c 74 20 62 61 63 6b 20  hat result back 
150ee 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f  to SQLITE_OK..*/
150ef 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
150f0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
150f1 52 65 73 65 74 53 74 65 70 52 65 73 75 6c 74 28  ResetStepResult(
150f2 56 64 62 65 20 2a 70 29 7b 0a 20 20 70 2d 3e 72  Vdbe *p){.  p->r
150f3 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  c = SQLITE_OK;.}
150f4 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70  ../*.** Clean up
150f5 20 61 20 56 44 42 45 20 61 66 74 65 72 20 65 78   a VDBE after ex
150f6 65 63 75 74 69 6f 6e 20 62 75 74 20 64 6f 20 6e  ecution but do n
150f7 6f 74 20 64 65 6c 65 74 65 20 74 68 65 20 56 44  ot delete the VD
150f8 42 45 20 6a 75 73 74 20 79 65 74 2e 0a 2a 2a 20  BE just yet..** 
150f9 57 72 69 74 65 20 61 6e 79 20 65 72 72 6f 72 20  Write any error 
150fa 6d 65 73 73 61 67 65 73 20 69 6e 74 6f 20 2a 70  messages into *p
150fb 7a 45 72 72 4d 73 67 2e 20 20 52 65 74 75 72 6e  zErrMsg.  Return
150fc 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 64 65   the result code
150fd 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68  ..**.** After th
150fe 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 75  is routine is ru
150ff 6e 2c 20 74 68 65 20 56 44 42 45 20 73 68 6f 75  n, the VDBE shou
15100 6c 64 20 62 65 20 72 65 61 64 79 20 74 6f 20 62  ld be ready to b
15101 65 20 65 78 65 63 75 74 65 64 0a 2a 2a 20 61 67  e executed.** ag
15102 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 6c 6f  ain..**.** To lo
15103 6f 6b 20 61 74 20 69 74 20 61 6e 6f 74 68 65 72  ok at it another
15104 20 77 61 79 2c 20 74 68 69 73 20 72 6f 75 74 69   way, this routi
15105 6e 65 20 72 65 73 65 74 73 20 74 68 65 20 73 74  ne resets the st
15106 61 74 65 20 6f 66 20 74 68 65 0a 2a 2a 20 76 69  ate of the.** vi
15107 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 66 72  rtual machine fr
15108 6f 6d 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55  om VDBE_MAGIC_RU
15109 4e 20 6f 72 20 56 44 42 45 5f 4d 41 47 49 43 5f  N or VDBE_MAGIC_
1510a 48 41 4c 54 20 62 61 63 6b 20 74 6f 0a 2a 2a 20  HALT back to.** 
1510b 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 2e  VDBE_MAGIC_INIT.
1510c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1510d 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
1510e 62 65 52 65 73 65 74 28 56 64 62 65 20 2a 70 2c  beReset(Vdbe *p,
1510f 20 69 6e 74 20 66 72 65 65 62 75 66 66 65 72 73   int freebuffers
15110 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
15111 3b 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  ;.  db = p->db;.
15112 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 56 4d 20  .  /* If the VM 
15113 64 69 64 20 6e 6f 74 20 72 75 6e 20 74 6f 20 63  did not run to c
15114 6f 6d 70 6c 65 74 69 6f 6e 20 6f 72 20 69 66 20  ompletion or if 
15115 69 74 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 61  it encountered a
15116 6e 0a 20 20 2a 2a 20 65 72 72 6f 72 2c 20 74 68  n.  ** error, th
15117 65 6e 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 20  en it might not 
15118 68 61 76 65 20 62 65 65 6e 20 68 61 6c 74 65 64  have been halted
15119 20 70 72 6f 70 65 72 6c 79 2e 20 20 53 6f 20 68   properly.  So h
1511a 61 6c 74 0a 20 20 2a 2a 20 69 74 20 6e 6f 77 2e  alt.  ** it now.
1511b 0a 20 20 2a 2f 0a 20 20 28 76 6f 69 64 29 73 71  .  */.  (void)sq
1511c 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
1511d 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
1511e 48 61 6c 74 28 70 29 3b 0a 20 20 28 76 6f 69 64  Halt(p);.  (void
1511f 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66  )sqlite3SafetyOf
15120 66 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  f(db);..  /* If 
15121 74 68 65 20 56 44 42 45 20 68 61 73 20 62 65 20  the VDBE has be 
15122 72 75 6e 20 65 76 65 6e 20 70 61 72 74 69 61 6c  run even partial
15123 6c 79 2c 20 74 68 65 6e 20 74 72 61 6e 73 66 65  ly, then transfe
15124 72 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  r the error code
15125 0a 20 20 2a 2a 20 61 6e 64 20 65 72 72 6f 72 20  .  ** and error 
15126 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 74 68 65  message from the
15127 20 56 44 42 45 20 69 6e 74 6f 20 74 68 65 20 6d   VDBE into the m
15128 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 74 72  ain database str
15129 75 63 74 75 72 65 2e 20 20 42 75 74 0a 20 20 2a  ucture.  But.  *
1512a 2a 20 69 66 20 74 68 65 20 56 44 42 45 20 68 61  * if the VDBE ha
1512b 73 20 6a 75 73 74 20 62 65 65 6e 20 73 65 74 20  s just been set 
1512c 74 6f 20 72 75 6e 20 62 75 74 20 68 61 73 20 6e  to run but has n
1512d 6f 74 20 61 63 74 75 61 6c 6c 79 20 65 78 65 63  ot actually exec
1512e 75 74 65 64 20 61 6e 79 0a 20 20 2a 2a 20 69 6e  uted any.  ** in
1512f 73 74 72 75 63 74 69 6f 6e 73 20 79 65 74 2c 20  structions yet, 
15130 6c 65 61 76 65 20 74 68 65 20 6d 61 69 6e 20 64  leave the main d
15131 61 74 61 62 61 73 65 20 65 72 72 6f 72 20 69 6e  atabase error in
15132 66 6f 72 6d 61 74 69 6f 6e 20 75 6e 63 68 61 6e  formation unchan
15133 67 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ged..  */.  if( 
15134 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20  p->pc>=0 ){.    
15135 69 66 28 20 70 2d 3e 7a 45 72 72 4d 73 67 20 29  if( p->zErrMsg )
15136 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
15137 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70  alueSetStr(db->p
15138 45 72 72 2c 2d 31 2c 70 2d 3e 7a 45 72 72 4d 73  Err,-1,p->zErrMs
15139 67 2c 53 51 4c 49 54 45 5f 55 54 46 38 2c 73 71  g,SQLITE_UTF8,sq
1513a 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 20  lite3_free);.   
1513b 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d     db->errCode =
1513c 20 70 2d 3e 72 63 3b 0a 20 20 20 20 20 20 70 2d   p->rc;.      p-
1513d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  >zErrMsg = 0;.  
1513e 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72    }else if( p->r
1513f 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  c ){.      sqlit
15140 65 33 45 72 72 6f 72 28 64 62 2c 20 70 2d 3e 72  e3Error(db, p->r
15141 63 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65  c, 0);.    }else
15142 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
15143 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
15144 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  OK, 0);.    }.  
15145 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 63 20  }else if( p->rc 
15146 26 26 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b  && p->expired ){
15147 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 69  .    /* The expi
15148 72 65 64 20 66 6c 61 67 20 77 61 73 20 73 65 74  red flag was set
15149 20 6f 6e 20 74 68 65 20 56 44 42 45 20 62 65 66   on the VDBE bef
1514a 6f 72 65 20 74 68 65 20 66 69 72 73 74 20 63 61  ore the first ca
1514b 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 73 71 6c  ll.    ** to sql
1514c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 46 6f 72  ite3_step(). For
1514d 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 28 73 69   consistency (si
1514e 6e 63 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70  nce sqlite3_step
1514f 28 29 20 77 61 73 0a 20 20 20 20 2a 2a 20 63 61  () was.    ** ca
15150 6c 6c 65 64 29 2c 20 73 65 74 20 74 68 65 20 64  lled), set the d
15151 61 74 61 62 61 73 65 20 65 72 72 6f 72 20 69 6e  atabase error in
15152 20 74 68 69 73 20 63 61 73 65 20 61 73 20 77 65   this case as we
15153 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  ll..    */.    s
15154 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
15155 70 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 73  p->rc, 0);.    s
15156 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74  qlite3ValueSetSt
15157 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c 20  r(db->pErr, -1, 
15158 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 53 51 4c 49  p->zErrMsg, SQLI
15159 54 45 5f 55 54 46 38 2c 20 73 71 6c 69 74 65 33  TE_UTF8, sqlite3
1515a 5f 66 72 65 65 29 3b 0a 20 20 20 20 70 2d 3e 7a  _free);.    p->z
1515b 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a  ErrMsg = 0;.  }.
1515c 0a 20 20 2f 2a 20 52 65 63 6c 61 69 6d 20 61 6c  .  /* Reclaim al
1515d 6c 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79  l memory used by
1515e 20 74 68 65 20 56 44 42 45 0a 20 20 2a 2f 0a 20   the VDBE.  */. 
1515f 20 43 6c 65 61 6e 75 70 28 70 2c 20 66 72 65 65   Cleanup(p, free
15160 62 75 66 66 65 72 73 29 3b 0a 0a 20 20 2f 2a 20  buffers);..  /* 
15161 53 61 76 65 20 70 72 6f 66 69 6c 69 6e 67 20 69  Save profiling i
15162 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20  nformation from 
15163 74 68 69 73 20 56 44 42 45 20 72 75 6e 2e 0a 20  this VDBE run.. 
15164 20 2a 2f 0a 23 69 66 64 65 66 20 56 44 42 45 5f   */.#ifdef VDBE_
15165 50 52 4f 46 49 4c 45 0a 20 20 7b 0a 20 20 20 20  PROFILE.  {.    
15166 46 49 4c 45 20 2a 6f 75 74 20 3d 20 66 6f 70 65  FILE *out = fope
15167 6e 28 22 76 64 62 65 5f 70 72 6f 66 69 6c 65 2e  n("vdbe_profile.
15168 6f 75 74 22 2c 20 22 61 22 29 3b 0a 20 20 20 20  out", "a");.    
15169 69 66 28 20 6f 75 74 20 29 7b 0a 20 20 20 20 20  if( out ){.     
1516a 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 66 70   int i;.      fp
1516b 72 69 6e 74 66 28 6f 75 74 2c 20 22 2d 2d 2d 2d  rintf(out, "----
1516c 20 22 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69   ");.      for(i
1516d 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b  =0; i<p->nOp; i+
1516e 2b 29 7b 0a 20 20 20 20 20 20 20 20 66 70 72 69  +){.        fpri
1516f 6e 74 66 28 6f 75 74 2c 20 22 25 30 32 78 22 2c  ntf(out, "%02x",
15170 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64   p->aOp[i].opcod
15171 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
15172 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
15173 5c 6e 22 29 3b 0a 20 20 20 20 20 20 66 6f 72 28  \n");.      for(
15174 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69  i=0; i<p->nOp; i
15175 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 66 70 72  ++){.        fpr
15176 69 6e 74 66 28 6f 75 74 2c 20 22 25 36 64 20 25  intf(out, "%6d %
15177 31 30 6c 6c 64 20 25 38 6c 6c 64 20 22 2c 0a 20  10lld %8lld ",. 
15178 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61 4f 70            p->aOp
15179 5b 69 5d 2e 63 6e 74 2c 0a 20 20 20 20 20 20 20  [i].cnt,.       
1517a 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79      p->aOp[i].cy
1517b 63 6c 65 73 2c 0a 20 20 20 20 20 20 20 20 20 20  cles,.          
1517c 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 3e 30   p->aOp[i].cnt>0
1517d 20 3f 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63   ? p->aOp[i].cyc
1517e 6c 65 73 2f 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e  les/p->aOp[i].cn
1517f 74 20 3a 20 30 0a 20 20 20 20 20 20 20 20 29 3b  t : 0.        );
15180 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
15181 56 64 62 65 50 72 69 6e 74 4f 70 28 6f 75 74 2c  VdbePrintOp(out,
15182 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b   i, &p->aOp[i]);
15183 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66  .      }.      f
15184 63 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 20 20 20  close(out);.    
15185 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70  }.  }.#endif.  p
15186 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d  ->magic = VDBE_M
15187 41 47 49 43 5f 49 4e 49 54 3b 0a 20 20 70 2d 3e  AGIC_INIT;.  p->
15188 61 62 6f 72 74 65 64 20 3d 20 30 3b 0a 20 20 72  aborted = 0;.  r
15189 65 74 75 72 6e 20 70 2d 3e 72 63 20 26 20 64 62  eturn p->rc & db
1518a 2d 3e 65 72 72 4d 61 73 6b 3b 0a 7d 0a 20 0a 2f  ->errMask;.}. ./
1518b 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 61 6e  *.** Clean up an
1518c 64 20 64 65 6c 65 74 65 20 61 20 56 44 42 45 20  d delete a VDBE 
1518d 61 66 74 65 72 20 65 78 65 63 75 74 69 6f 6e 2e  after execution.
1518e 20 20 52 65 74 75 72 6e 20 61 6e 20 69 6e 74 65    Return an inte
1518f 67 65 72 20 77 68 69 63 68 20 69 73 0a 2a 2a 20  ger which is.** 
15190 74 68 65 20 72 65 73 75 6c 74 20 63 6f 64 65 2e  the result code.
15191 20 20 57 72 69 74 65 20 61 6e 79 20 65 72 72 6f    Write any erro
15192 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 69  r message text i
15193 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 0a 2a  nto *pzErrMsg..*
15194 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
15195 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
15196 46 69 6e 61 6c 69 7a 65 28 56 64 62 65 20 2a 70  Finalize(Vdbe *p
15197 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
15198 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70  LITE_OK;.  if( p
15199 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41  ->magic==VDBE_MA
1519a 47 49 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e 6d 61  GIC_RUN || p->ma
1519b 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f  gic==VDBE_MAGIC_
1519c 48 41 4c 54 20 29 7b 0a 20 20 20 20 72 63 20 3d  HALT ){.    rc =
1519d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 65   sqlite3VdbeRese
1519e 74 28 70 2c 20 31 29 3b 0a 20 20 20 20 61 73 73  t(p, 1);.    ass
1519f 65 72 74 28 20 28 72 63 20 26 20 70 2d 3e 64 62  ert( (rc & p->db
151a0 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d 72 63 20 29  ->errMask)==rc )
151a1 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d  ;.  }else if( p-
151a2 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47  >magic!=VDBE_MAG
151a3 49 43 5f 49 4e 49 54 20 29 7b 0a 20 20 20 20 72  IC_INIT ){.    r
151a4 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
151a5 55 53 45 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61  USE;.  }.  relea
151a6 73 65 4d 65 6d 41 72 72 61 79 28 26 70 2d 3e 61  seMemArray(&p->a
151a7 4d 65 6d 5b 31 5d 2c 20 70 2d 3e 6e 4d 65 6d 2c  Mem[1], p->nMem,
151a8 20 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64   1);.  sqlite3Vd
151a9 62 65 44 65 6c 65 74 65 28 70 29 3b 0a 20 20 72  beDelete(p);.  r
151aa 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
151ab 2a 2a 20 43 61 6c 6c 20 74 68 65 20 64 65 73 74  ** Call the dest
151ac 72 75 63 74 6f 72 20 66 6f 72 20 65 61 63 68 20  ructor for each 
151ad 61 75 78 64 61 74 61 20 65 6e 74 72 79 20 69 6e  auxdata entry in
151ae 20 70 56 64 62 65 46 75 6e 63 20 66 6f 72 20 77   pVdbeFunc for w
151af 68 69 63 68 0a 2a 2a 20 74 68 65 20 63 6f 72 72  hich.** the corr
151b0 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 20 69 6e  esponding bit in
151b1 20 6d 61 73 6b 20 69 73 20 63 6c 65 61 72 2e 20   mask is clear. 
151b2 20 41 75 78 64 61 74 61 20 65 6e 74 72 69 65 73   Auxdata entries
151b3 20 62 65 79 6f 6e 64 20 33 31 0a 2a 2a 20 61 72   beyond 31.** ar
151b4 65 20 61 6c 77 61 79 73 20 64 65 73 74 72 6f 79  e always destroy
151b5 65 64 2e 20 20 54 6f 20 64 65 73 74 72 6f 79 20  ed.  To destroy 
151b6 61 6c 6c 20 61 75 78 64 61 74 61 20 65 6e 74 72  all auxdata entr
151b7 69 65 73 2c 20 63 61 6c 6c 20 74 68 69 73 0a 2a  ies, call this.*
151b8 2a 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 6d  * routine with m
151b9 61 73 6b 3d 3d 30 2e 0a 2a 2f 0a 53 51 4c 49 54  ask==0..*/.SQLIT
151ba 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
151bb 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65  qlite3VdbeDelete
151bc 41 75 78 44 61 74 61 28 56 64 62 65 46 75 6e 63  AuxData(VdbeFunc
151bd 20 2a 70 56 64 62 65 46 75 6e 63 2c 20 69 6e 74   *pVdbeFunc, int
151be 20 6d 61 73 6b 29 7b 0a 20 20 69 6e 74 20 69 3b   mask){.  int i;
151bf 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 56  .  for(i=0; i<pV
151c0 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78 3b 20 69  dbeFunc->nAux; i
151c1 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  ++){.    struct 
151c2 41 75 78 44 61 74 61 20 2a 70 41 75 78 20 3d 20  AuxData *pAux = 
151c3 26 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75  &pVdbeFunc->apAu
151c4 78 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 28 69  x[i];.    if( (i
151c5 3e 33 31 20 7c 7c 20 21 28 6d 61 73 6b 26 28 31  >31 || !(mask&(1
151c6 3c 3c 69 29 29 29 20 26 26 20 70 41 75 78 2d 3e  <<i))) && pAux->
151c7 70 41 75 78 20 29 7b 0a 20 20 20 20 20 20 69 66  pAux ){.      if
151c8 28 20 70 41 75 78 2d 3e 78 44 65 6c 65 74 65 20  ( pAux->xDelete 
151c9 29 7b 0a 20 20 20 20 20 20 20 20 70 41 75 78 2d  ){.        pAux-
151ca 3e 78 44 65 6c 65 74 65 28 70 41 75 78 2d 3e 70  >xDelete(pAux->p
151cb 41 75 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Aux);.      }.  
151cc 20 20 20 20 70 41 75 78 2d 3e 70 41 75 78 20 3d      pAux->pAux =
151cd 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a   0;.    }.  }.}.
151ce 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e  ./*.** Delete an
151cf 20 65 6e 74 69 72 65 20 56 44 42 45 2e 0a 2a 2f   entire VDBE..*/
151d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
151d1 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
151d2 44 65 6c 65 74 65 28 56 64 62 65 20 2a 70 29 7b  Delete(Vdbe *p){
151d3 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
151d4 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  p==0 ) return;. 
151d5 20 43 6c 65 61 6e 75 70 28 70 2c 20 31 29 3b 0a   Cleanup(p, 1);.
151d6 20 20 69 66 28 20 70 2d 3e 70 50 72 65 76 20 29    if( p->pPrev )
151d7 7b 0a 20 20 20 20 70 2d 3e 70 50 72 65 76 2d 3e  {.    p->pPrev->
151d8 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74  pNext = p->pNext
151d9 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
151da 73 73 65 72 74 28 20 70 2d 3e 64 62 2d 3e 70 56  ssert( p->db->pV
151db 64 62 65 3d 3d 70 20 29 3b 0a 20 20 20 20 70 2d  dbe==p );.    p-
151dc 3e 64 62 2d 3e 70 56 64 62 65 20 3d 20 70 2d 3e  >db->pVdbe = p->
151dd 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28  pNext;.  }.  if(
151de 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20   p->pNext ){.   
151df 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76   p->pNext->pPrev
151e0 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 20 20 7d   = p->pPrev;.  }
151e1 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70 20 29 7b  .  if( p->aOp ){
151e2 0a 20 20 20 20 4f 70 20 2a 70 4f 70 20 3d 20 70  .    Op *pOp = p
151e3 2d 3e 61 4f 70 3b 0a 20 20 20 20 66 6f 72 28 69  ->aOp;.    for(i
151e4 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b  =0; i<p->nOp; i+
151e5 2b 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 20  +, pOp++){.     
151e6 20 66 72 65 65 50 34 28 70 4f 70 2d 3e 70 34 74   freeP4(pOp->p4t
151e7 79 70 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b  ype, pOp->p4.p);
151e8 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
151e9 45 42 55 47 0a 20 20 20 20 20 20 73 71 6c 69 74  EBUG.      sqlit
151ea 65 33 5f 66 72 65 65 28 70 4f 70 2d 3e 7a 43 6f  e3_free(pOp->zCo
151eb 6d 6d 65 6e 74 29 3b 0a 23 65 6e 64 69 66 20 20  mment);.#endif  
151ec 20 20 20 0a 20 20 20 20 7d 0a 20 20 20 20 73 71     .    }.    sq
151ed 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 4f  lite3_free(p->aO
151ee 70 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73  p);.  }.  releas
151ef 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61 56 61  eMemArray(p->aVa
151f0 72 2c 20 70 2d 3e 6e 56 61 72 2c 20 31 29 3b 0a  r, p->nVar, 1);.
151f1 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
151f2 2d 3e 61 4c 61 62 65 6c 29 3b 0a 20 20 69 66 28  ->aLabel);.  if(
151f3 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20 20 20 20   p->aMem ){.    
151f4 73 71 6c 69 74 65 33 5f 66 72 65 65 28 26 70 2d  sqlite3_free(&p-
151f5 3e 61 4d 65 6d 5b 31 5d 29 3b 0a 20 20 7d 0a 20  >aMem[1]);.  }. 
151f6 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79   releaseMemArray
151f7 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20 70 2d  (p->aColName, p-
151f8 3e 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e  >nResColumn*COLN
151f9 41 4d 45 5f 4e 2c 20 31 29 3b 0a 20 20 73 71 6c  AME_N, 1);.  sql
151fa 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 43 6f  ite3_free(p->aCo
151fb 6c 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65  lName);.  sqlite
151fc 33 5f 66 72 65 65 28 70 2d 3e 7a 53 71 6c 29 3b  3_free(p->zSql);
151fd 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44  .  p->magic = VD
151fe 42 45 5f 4d 41 47 49 43 5f 44 45 41 44 3b 0a 20  BE_MAGIC_DEAD;. 
151ff 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29   sqlite3_free(p)
15200 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 61 20  ;.}../*.** If a 
15201 4d 6f 76 65 54 6f 20 6f 70 65 72 61 74 69 6f 6e  MoveTo operation
15202 20 69 73 20 70 65 6e 64 69 6e 67 20 6f 6e 20 74   is pending on t
15203 68 65 20 67 69 76 65 6e 20 63 75 72 73 6f 72 2c  he given cursor,
15204 20 74 68 65 6e 20 64 6f 20 74 68 61 74 0a 2a 2a   then do that.**
15205 20 4d 6f 76 65 54 6f 20 6e 6f 77 2e 20 20 52 65   MoveTo now.  Re
15206 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f  turn an error co
15207 64 65 2e 20 20 49 66 20 6e 6f 20 4d 6f 76 65 54  de.  If no MoveT
15208 6f 20 69 73 20 70 65 6e 64 69 6e 67 2c 20 74 68  o is pending, th
15209 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 64 6f  is.** routine do
1520a 65 73 20 6e 6f 74 68 69 6e 67 20 61 6e 64 20 72  es nothing and r
1520b 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b  eturns SQLITE_OK
1520c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1520d 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
1520e 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28  dbeCursorMoveto(
1520f 43 75 72 73 6f 72 20 2a 70 29 7b 0a 20 20 69 66  Cursor *p){.  if
15210 28 20 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76  ( p->deferredMov
15211 65 74 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 72  eto ){.    int r
15212 65 73 2c 20 72 63 3b 0a 23 69 66 64 65 66 20 53  es, rc;.#ifdef S
15213 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 65  QLITE_TEST.    e
15214 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65  xtern int sqlite
15215 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 3b 0a  3_search_count;.
15216 23 65 6e 64 69 66 0a 20 20 20 20 61 73 73 65 72  #endif.    asser
15217 74 28 20 70 2d 3e 69 73 54 61 62 6c 65 20 29 3b  t( p->isTable );
15218 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
15219 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 70 2d 3e  3BtreeMoveto(p->
1521a 70 43 75 72 73 6f 72 2c 20 30 2c 20 30 2c 20 70  pCursor, 0, 0, p
1521b 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65 74 2c 20  ->movetoTarget, 
1521c 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66  0, &res);.    if
1521d 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
1521e 3b 0a 20 20 20 20 2a 70 2d 3e 70 49 6e 63 72 4b  ;.    *p->pIncrK
1521f 65 79 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 6c  ey = 0;.    p->l
15220 61 73 74 52 6f 77 69 64 20 3d 20 6b 65 79 54 6f  astRowid = keyTo
15221 49 6e 74 28 70 2d 3e 6d 6f 76 65 74 6f 54 61 72  Int(p->movetoTar
15222 67 65 74 29 3b 0a 20 20 20 20 70 2d 3e 72 6f 77  get);.    p->row
15223 69 64 49 73 56 61 6c 69 64 20 3d 20 72 65 73 3d  idIsValid = res=
15224 3d 30 3b 0a 20 20 20 20 69 66 28 20 72 65 73 3c  =0;.    if( res<
15225 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
15226 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74  sqlite3BtreeNext
15227 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 26 72 65  (p->pCursor, &re
15228 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  s);.      if( rc
15229 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
1522a 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54    }.#ifdef SQLIT
1522b 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69 74  E_TEST.    sqlit
1522c 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 2b  e3_search_count+
1522d 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 2d  +;.#endif.    p-
1522e 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20  >deferredMoveto 
1522f 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 63 61 63 68  = 0;.    p->cach
15230 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f  eStatus = CACHE_
15231 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 72 65 74  STALE;.  }.  ret
15232 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
15233 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
15234 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73 3a  owing functions:
15235 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64  .**.** sqlite3Vd
15236 62 65 53 65 72 69 61 6c 54 79 70 65 28 29 0a 2a  beSerialType().*
15237 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  * sqlite3VdbeSer
15238 69 61 6c 54 79 70 65 4c 65 6e 28 29 0a 2a 2a 20  ialTypeLen().** 
15239 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
1523a 6c 52 65 61 64 28 29 0a 2a 2a 20 73 71 6c 69 74  lRead().** sqlit
1523b 65 33 56 64 62 65 53 65 72 69 61 6c 4c 65 6e 28  e3VdbeSerialLen(
1523c 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65  ).** sqlite3Vdbe
1523d 53 65 72 69 61 6c 57 72 69 74 65 28 29 0a 2a 2a  SerialWrite().**
1523e 0a 2a 2a 20 65 6e 63 61 70 73 75 6c 61 74 65 20  .** encapsulate 
1523f 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 73 65  the code that se
15240 72 69 61 6c 69 7a 65 73 20 76 61 6c 75 65 73 20  rializes values 
15241 66 6f 72 20 73 74 6f 72 61 67 65 20 69 6e 20 53  for storage in S
15242 51 4c 69 74 65 0a 2a 2a 20 64 61 74 61 20 61 6e  QLite.** data an
15243 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73 2e  d index records.
15244 20 45 61 63 68 20 73 65 72 69 61 6c 69 7a 65 64   Each serialized
15245 20 76 61 6c 75 65 20 63 6f 6e 73 69 73 74 73 20   value consists 
15246 6f 66 20 61 0a 2a 2a 20 27 73 65 72 69 61 6c 2d  of a.** 'serial-
15247 74 79 70 65 27 20 61 6e 64 20 61 20 62 6c 6f 62  type' and a blob
15248 20 6f 66 20 64 61 74 61 2e 20 54 68 65 20 73 65   of data. The se
15249 72 69 61 6c 20 74 79 70 65 20 69 73 20 61 6e 20  rial type is an 
1524a 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 0a  8-byte unsigned.
1524b 2a 2a 20 69 6e 74 65 67 65 72 2c 20 73 74 6f 72  ** integer, stor
1524c 65 64 20 61 73 20 61 20 76 61 72 69 6e 74 2e 0a  ed as a varint..
1524d 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 20 53 51 4c 69  **.** In an SQLi
1524e 74 65 20 69 6e 64 65 78 20 72 65 63 6f 72 64 2c  te index record,
1524f 20 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 65   the serial type
15250 20 69 73 20 73 74 6f 72 65 64 20 64 69 72 65 63   is stored direc
15251 74 6c 79 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68  tly before.** th
15252 65 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 74  e blob of data t
15253 68 61 74 20 69 74 20 63 6f 72 72 65 73 70 6f 6e  hat it correspon
15254 64 73 20 74 6f 2e 20 49 6e 20 61 20 74 61 62 6c  ds to. In a tabl
15255 65 20 72 65 63 6f 72 64 2c 20 61 6c 6c 20 73 65  e record, all se
15256 72 69 61 6c 0a 2a 2a 20 74 79 70 65 73 20 61 72  rial.** types ar
15257 65 20 73 74 6f 72 65 64 20 61 74 20 74 68 65 20  e stored at the 
15258 73 74 61 72 74 20 6f 66 20 74 68 65 20 72 65 63  start of the rec
15259 6f 72 64 2c 20 61 6e 64 20 74 68 65 20 62 6c 6f  ord, and the blo
1525a 62 73 20 6f 66 20 64 61 74 61 20 61 74 0a 2a 2a  bs of data at.**
1525b 20 74 68 65 20 65 6e 64 2e 20 48 65 6e 63 65 20   the end. Hence 
1525c 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20  these functions 
1525d 61 6c 6c 6f 77 20 74 68 65 20 63 61 6c 6c 65 72  allow the caller
1525e 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 0a 2a   to handle the.*
1525f 2a 20 73 65 72 69 61 6c 2d 74 79 70 65 20 61 6e  * serial-type an
15260 64 20 64 61 74 61 20 62 6c 6f 62 20 73 65 70 65  d data blob sepe
15261 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  rately..**.** Th
15262 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c  e following tabl
15263 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20  e describes the 
15264 76 61 72 69 6f 75 73 20 73 74 6f 72 61 67 65 20  various storage 
15265 63 6c 61 73 73 65 73 20 66 6f 72 20 64 61 74 61  classes for data
15266 3a 0a 2a 2a 0a 2a 2a 20 20 20 73 65 72 69 61 6c  :.**.**   serial
15267 20 74 79 70 65 20 20 20 20 20 20 20 20 62 79 74   type        byt
15268 65 73 20 6f 66 20 64 61 74 61 20 20 20 20 20 20  es of data      
15269 74 79 70 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d  type.**   ------
1526a 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d  --------     ---
1526b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20  ------------    
1526c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
1526d 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 20 20  **      0       
1526e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20                0 
1526f 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 0a             NULL.
15270 2a 2a 20 20 20 20 20 20 31 20 20 20 20 20 20 20  **      1       
15271 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 20                1 
15272 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65             signe
15273 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20  d integer.**    
15274 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20    2             
15275 20 20 20 20 20 20 20 20 32 20 20 20 20 20 20 20          2       
15276 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65       signed inte
15277 67 65 72 0a 2a 2a 20 20 20 20 20 20 33 20 20 20  ger.**      3   
15278 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15279 20 20 33 20 20 20 20 20 20 20 20 20 20 20 20 73    3            s
1527a 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a  igned integer.**
1527b 20 20 20 20 20 20 34 20 20 20 20 20 20 20 20 20        4         
1527c 20 20 20 20 20 20 20 20 20 20 20 20 34 20 20 20              4   
1527d 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 20           signed 
1527e 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20  integer.**      
1527f 35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  5               
15280 20 20 20 20 20 20 36 20 20 20 20 20 20 20 20 20        6         
15281 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65     signed intege
15282 72 0a 2a 2a 20 20 20 20 20 20 36 20 20 20 20 20  r.**      6     
15283 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15284 38 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67  8            sig
15285 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20  ned integer.**  
15286 20 20 20 20 37 20 20 20 20 20 20 20 20 20 20 20      7           
15287 20 20 20 20 20 20 20 20 20 20 38 20 20 20 20 20            8     
15288 20 20 20 20 20 20 20 49 45 45 45 20 66 6c 6f 61         IEEE floa
15289 74 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 20 20  t.**      8     
1528a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1528b 30 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 74  0            Int
1528c 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 30 0a  eger constant 0.
1528d 2a 2a 20 20 20 20 20 20 39 20 20 20 20 20 20 20  **      9       
1528e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20                0 
1528f 20 20 20 20 20 20 20 20 20 20 20 49 6e 74 65 67             Integ
15290 65 72 20 63 6f 6e 73 74 61 6e 74 20 31 0a 2a 2a  er constant 1.**
15291 20 20 20 20 20 31 30 2c 31 31 20 20 20 20 20 20       10,11      
15292 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15293 20 20 20 20 20 20 20 20 20 72 65 73 65 72 76 65           reserve
15294 64 20 66 6f 72 20 65 78 70 61 6e 73 69 6f 6e 0a  d for expansion.
15295 2a 2a 20 20 20 20 4e 3e 3d 31 32 20 61 6e 64 20  **    N>=12 and 
15296 65 76 65 6e 20 20 20 20 20 20 20 28 4e 2d 31 32  even       (N-12
15297 29 2f 32 20 20 20 20 20 20 20 20 42 4c 4f 42 0a  )/2        BLOB.
15298 2a 2a 20 20 20 20 4e 3e 3d 31 33 20 61 6e 64 20  **    N>=13 and 
15299 6f 64 64 20 20 20 20 20 20 20 20 28 4e 2d 31 33  odd        (N-13
1529a 29 2f 32 20 20 20 20 20 20 20 20 74 65 78 74 0a  )/2        text.
1529b 2a 2a 0a 2a 2a 20 54 68 65 20 38 20 61 6e 64 20  **.** The 8 and 
1529c 39 20 74 79 70 65 73 20 77 65 72 65 20 61 64 64  9 types were add
1529d 65 64 20 69 6e 20 33 2e 33 2e 30 2c 20 66 69 6c  ed in 3.3.0, fil
1529e 65 20 66 6f 72 6d 61 74 20 34 2e 20 20 50 72 69  e format 4.  Pri
1529f 6f 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f  or versions.** o
152a0 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f  f SQLite will no
152a1 74 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68 6f  t understand tho
152a2 73 65 20 73 65 72 69 61 6c 20 74 79 70 65 73 2e  se serial types.
152a3 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  .*/../*.** Retur
152a4 6e 20 74 68 65 20 73 65 72 69 61 6c 2d 74 79 70  n the serial-typ
152a5 65 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65 20  e for the value 
152a6 73 74 6f 72 65 64 20 69 6e 20 70 4d 65 6d 2e 0a  stored in pMem..
152a7 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
152a8 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 62  E u32 sqlite3Vdb
152a9 65 53 65 72 69 61 6c 54 79 70 65 28 4d 65 6d 20  eSerialType(Mem 
152aa 2a 70 4d 65 6d 2c 20 69 6e 74 20 66 69 6c 65 5f  *pMem, int file_
152ab 66 6f 72 6d 61 74 29 7b 0a 20 20 69 6e 74 20 66  format){.  int f
152ac 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61  lags = pMem->fla
152ad 67 73 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20  gs;.  int n;..  
152ae 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 4e 75  if( flags&MEM_Nu
152af 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ll ){.    return
152b0 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c   0;.  }.  if( fl
152b1 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20  ags&MEM_Int ){. 
152b2 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74     /* Figure out
152b3 20 77 68 65 74 68 65 72 20 74 6f 20 75 73 65 20   whether to use 
152b4 31 2c 20 32 2c 20 34 2c 20 36 20 6f 72 20 38 20  1, 2, 4, 6 or 8 
152b5 62 79 74 65 73 2e 20 2a 2f 0a 23 20 20 20 64 65  bytes. */.#   de
152b6 66 69 6e 65 20 4d 41 58 5f 36 42 59 54 45 20 28  fine MAX_6BYTE (
152b7 28 28 28 69 36 34 29 30 78 30 30 30 30 38 30 30  (((i64)0x0000800
152b8 30 29 3c 3c 33 32 29 2d 31 29 0a 20 20 20 20 69  0)<<32)-1).    i
152b9 36 34 20 69 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69  64 i = pMem->u.i
152ba 3b 0a 20 20 20 20 75 36 34 20 75 3b 0a 20 20 20  ;.    u64 u;.   
152bb 20 69 66 28 20 66 69 6c 65 5f 66 6f 72 6d 61 74   if( file_format
152bc 3e 3d 34 20 26 26 20 28 69 26 31 29 3d 3d 69 20  >=4 && (i&1)==i 
152bd 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
152be 38 2b 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75  8+i;.    }.    u
152bf 20 3d 20 69 3c 30 20 3f 20 2d 69 20 3a 20 69 3b   = i<0 ? -i : i;
152c0 0a 20 20 20 20 69 66 28 20 75 3c 3d 31 32 37 20  .    if( u<=127 
152c1 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20  ) return 1;.    
152c2 69 66 28 20 75 3c 3d 33 32 37 36 37 20 29 20 72  if( u<=32767 ) r
152c3 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 69 66 28  eturn 2;.    if(
152c4 20 75 3c 3d 38 33 38 38 36 30 37 20 29 20 72 65   u<=8388607 ) re
152c5 74 75 72 6e 20 33 3b 0a 20 20 20 20 69 66 28 20  turn 3;.    if( 
152c6 75 3c 3d 32 31 34 37 34 38 33 36 34 37 20 29 20  u<=2147483647 ) 
152c7 72 65 74 75 72 6e 20 34 3b 0a 20 20 20 20 69 66  return 4;.    if
152c8 28 20 75 3c 3d 4d 41 58 5f 36 42 59 54 45 20 29  ( u<=MAX_6BYTE )
152c9 20 72 65 74 75 72 6e 20 35 3b 0a 20 20 20 20 72   return 5;.    r
152ca 65 74 75 72 6e 20 36 3b 0a 20 20 7d 0a 20 20 69  eturn 6;.  }.  i
152cb 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 52 65 61  f( flags&MEM_Rea
152cc 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  l ){.    return 
152cd 37 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  7;.  }.  assert(
152ce 20 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c   flags&(MEM_Str|
152cf 4d 45 4d 5f 42 6c 6f 62 29 20 29 3b 0a 20 20 6e  MEM_Blob) );.  n
152d0 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 69 66   = pMem->n;.  if
152d1 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65  ( flags & MEM_Ze
152d2 72 6f 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 70  ro ){.    n += p
152d3 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20  Mem->u.i;.  }.  
152d4 61 73 73 65 72 74 28 20 6e 3e 3d 30 20 29 3b 0a  assert( n>=0 );.
152d5 20 20 72 65 74 75 72 6e 20 28 28 6e 2a 32 29 20    return ((n*2) 
152d6 2b 20 31 32 20 2b 20 28 28 66 6c 61 67 73 26 4d  + 12 + ((flags&M
152d7 45 4d 5f 53 74 72 29 21 3d 30 29 29 3b 0a 7d 0a  EM_Str)!=0));.}.
152d8 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
152d9 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  e length of the 
152da 64 61 74 61 20 63 6f 72 72 65 73 70 6f 6e 64 69  data correspondi
152db 6e 67 20 74 6f 20 74 68 65 20 73 75 70 70 6c 69  ng to the suppli
152dc 65 64 20 73 65 72 69 61 6c 2d 74 79 70 65 2e 0a  ed serial-type..
152dd 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
152de 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
152df 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 75  eSerialTypeLen(u
152e0 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 29 7b  32 serial_type){
152e1 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79  .  if( serial_ty
152e2 70 65 3e 3d 31 32 20 29 7b 0a 20 20 20 20 72 65  pe>=12 ){.    re
152e3 74 75 72 6e 20 28 73 65 72 69 61 6c 5f 74 79 70  turn (serial_typ
152e4 65 2d 31 32 29 2f 32 3b 0a 20 20 7d 65 6c 73 65  e-12)/2;.  }else
152e5 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e  {.    static con
152e6 73 74 20 75 38 20 61 53 69 7a 65 5b 5d 20 3d 20  st u8 aSize[] = 
152e7 7b 20 30 2c 20 31 2c 20 32 2c 20 33 2c 20 34 2c  { 0, 1, 2, 3, 4,
152e8 20 36 2c 20 38 2c 20 38 2c 20 30 2c 20 30 2c 20   6, 8, 8, 0, 0, 
152e9 30 2c 20 30 20 7d 3b 0a 20 20 20 20 72 65 74 75  0, 0 };.    retu
152ea 72 6e 20 61 53 69 7a 65 5b 73 65 72 69 61 6c 5f  rn aSize[serial_
152eb 74 79 70 65 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  type];.  }.}../*
152ec 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 20 6f 6e  .** If we are on
152ed 20 61 6e 20 61 72 63 68 69 74 65 63 74 75 72 65   an architecture
152ee 20 77 69 74 68 20 6d 69 78 65 64 2d 65 6e 64 69   with mixed-endi
152ef 61 6e 20 66 6c 6f 61 74 69 6e 67 20 0a 2a 2a 20  an floating .** 
152f0 70 6f 69 6e 74 73 20 28 65 78 3a 20 41 52 4d 37  points (ex: ARM7
152f1 29 20 74 68 65 6e 20 73 77 61 70 20 74 68 65 20  ) then swap the 
152f2 6c 6f 77 65 72 20 34 20 62 79 74 65 73 20 77 69  lower 4 bytes wi
152f3 74 68 20 74 68 65 20 0a 2a 2a 20 75 70 70 65 72  th the .** upper
152f4 20 34 20 62 79 74 65 73 2e 20 20 52 65 74 75 72   4 bytes.  Retur
152f5 6e 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a  n the result..**
152f6 0a 2a 2a 20 46 6f 72 20 6d 6f 73 74 20 61 72 63  .** For most arc
152f7 68 69 74 65 63 74 75 72 65 73 2c 20 74 68 69 73  hitectures, this
152f8 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a   is a no-op..**.
152f9 2a 2a 20 28 6c 61 74 65 72 29 3a 20 20 49 74 20  ** (later):  It 
152fa 69 73 20 72 65 70 6f 72 74 65 64 20 74 6f 20 6d  is reported to m
152fb 65 20 74 68 61 74 20 74 68 65 20 6d 69 78 65 64  e that the mixed
152fc 2d 65 6e 64 69 61 6e 20 70 72 6f 62 6c 65 6d 0a  -endian problem.
152fd 2a 2a 20 6f 6e 20 41 52 4d 37 20 69 73 20 61 6e  ** on ARM7 is an
152fe 20 69 73 73 75 65 20 77 69 74 68 20 47 43 43 2c   issue with GCC,
152ff 20 6e 6f 74 20 77 69 74 68 20 74 68 65 20 41 52   not with the AR
15300 4d 37 20 63 68 69 70 2e 20 20 49 74 20 73 65 65  M7 chip.  It see
15301 6d 73 0a 2a 2a 20 74 68 61 74 20 65 61 72 6c 79  ms.** that early
15302 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 47 43 43   versions of GCC
15303 20 73 74 6f 72 65 64 20 74 68 65 20 74 77 6f 20   stored the two 
15304 77 6f 72 64 73 20 6f 66 20 61 20 36 34 2d 62 69  words of a 64-bi
15305 74 0a 2a 2a 20 66 6c 6f 61 74 20 69 6e 20 74 68  t.** float in th
15306 65 20 77 72 6f 6e 67 20 6f 72 64 65 72 2e 20 20  e wrong order.  
15307 41 6e 64 20 74 68 61 74 20 65 72 72 6f 72 20 68  And that error h
15308 61 73 20 62 65 65 6e 20 70 72 6f 70 61 67 61 74  as been propagat
15309 65 64 0a 2a 2a 20 65 76 65 72 20 73 69 6e 63 65  ed.** ever since
1530a 2e 20 20 54 68 65 20 62 6c 61 6d 65 20 69 73 20  .  The blame is 
1530b 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20  not necessarily 
1530c 77 69 74 68 20 47 43 43 2c 20 74 68 6f 75 67 68  with GCC, though
1530d 2e 0a 2a 2a 20 47 43 43 20 6d 69 67 68 74 20 68  ..** GCC might h
1530e 61 76 65 20 6a 75 73 74 20 63 6f 70 79 69 6e 67  ave just copying
1530f 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 66 72 6f   the problem fro
15310 6d 20 61 20 70 72 69 6f 72 20 63 6f 6d 70 69 6c  m a prior compil
15311 65 72 2e 0a 2a 2a 20 49 20 61 6d 20 61 6c 73 6f  er..** I am also
15312 20 74 6f 6c 64 20 74 68 61 74 20 6e 65 77 65 72   told that newer
15313 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 47 43 43   versions of GCC
15314 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 61 20 64   that follow a d
15315 69 66 66 65 72 65 6e 74 0a 2a 2a 20 41 42 49 20  ifferent.** ABI 
15316 67 65 74 20 74 68 65 20 62 79 74 65 20 6f 72 64  get the byte ord
15317 65 72 20 72 69 67 68 74 2e 0a 2a 2a 0a 2a 2a 20  er right..**.** 
15318 44 65 76 65 6c 6f 70 65 72 73 20 75 73 69 6e 67  Developers using
15319 20 53 51 4c 69 74 65 20 6f 6e 20 61 6e 20 41 52   SQLite on an AR
1531a 4d 37 20 73 68 6f 75 6c 64 20 63 6f 6d 70 69 6c  M7 should compil
1531b 65 20 61 6e 64 20 72 75 6e 20 74 68 65 69 72 0a  e and run their.
1531c 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 75  ** application u
1531d 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 5f 44 45  sing -DSQLITE_DE
1531e 42 55 47 3d 31 20 61 74 20 6c 65 61 73 74 20 6f  BUG=1 at least o
1531f 6e 63 65 2e 20 20 57 69 74 68 20 44 45 42 55 47  nce.  With DEBUG
15320 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c 20 73 6f 6d  .** enabled, som
15321 65 20 61 73 73 65 72 74 73 20 62 65 6c 6f 77 20  e asserts below 
15322 77 69 6c 6c 20 65 6e 73 75 72 65 20 74 68 61 74  will ensure that
15323 20 74 68 65 20 62 79 74 65 20 6f 72 64 65 72 20   the byte order 
15324 6f 66 0a 2a 2a 20 66 6c 6f 61 74 69 6e 67 20 70  of.** floating p
15325 6f 69 6e 74 20 76 61 6c 75 65 73 20 69 73 20 63  oint values is c
15326 6f 72 72 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 28 32  orrect..**.** (2
15327 30 30 37 2d 30 38 2d 33 30 29 20 20 46 72 61 6e  007-08-30)  Fran
15328 6b 20 76 61 6e 20 56 75 67 74 20 68 61 73 20 73  k van Vugt has s
15329 74 75 64 69 65 64 20 74 68 69 73 20 70 72 6f 62  tudied this prob
1532a 6c 65 6d 20 63 6c 6f 73 65 6c 79 0a 2a 2a 20 61  lem closely.** a
1532b 6e 64 20 68 61 73 20 73 65 6e 64 20 68 69 73 20  nd has send his 
1532c 66 69 6e 64 69 6e 67 73 20 74 6f 20 74 68 65 20  findings to the 
1532d 53 51 4c 69 74 65 20 64 65 76 65 6c 6f 70 65 72  SQLite developer
1532e 73 2e 20 20 46 72 61 6e 6b 0a 2a 2a 20 77 72 69  s.  Frank.** wri
1532f 74 65 73 20 74 68 61 74 20 73 6f 6d 65 20 4c 69  tes that some Li
15330 6e 75 78 20 6b 65 72 6e 65 6c 73 20 6f 66 66 65  nux kernels offe
15331 72 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  r floating point
15332 20 68 61 72 64 77 61 72 65 0a 2a 2a 20 65 6d 75   hardware.** emu
15333 6c 61 74 69 6f 6e 20 74 68 61 74 20 75 73 65 73  lation that uses
15334 20 6f 6e 6c 79 20 33 32 2d 62 69 74 20 6d 61 6e   only 32-bit man
15335 74 69 73 73 61 73 20 69 6e 73 74 65 61 64 20 6f  tissas instead o
15336 66 20 61 20 66 75 6c 6c 20 0a 2a 2a 20 34 38 2d  f a full .** 48-
15337 62 69 74 73 20 61 73 20 72 65 71 75 69 72 65 64  bits as required
15338 20 62 79 20 74 68 65 20 49 45 45 45 20 73 74 61   by the IEEE sta
15339 6e 64 61 72 64 2e 20 20 28 54 68 69 73 20 69 73  ndard.  (This is
1533a 20 74 68 65 0a 2a 2a 20 43 4f 4e 46 49 47 5f 46   the.** CONFIG_F
1533b 50 45 5f 46 41 53 54 46 50 45 20 6f 70 74 69 6f  PE_FASTFPE optio
1533c 6e 2e 29 20 20 4f 6e 20 73 75 63 68 20 73 79 73  n.)  On such sys
1533d 74 65 6d 73 2c 20 66 6c 6f 61 74 69 6e 67 20 70  tems, floating p
1533e 6f 69 6e 74 0a 2a 2a 20 62 79 74 65 20 73 77 61  oint.** byte swa
1533f 70 70 69 6e 67 20 62 65 63 6f 6d 65 73 20 76 65  pping becomes ve
15340 72 79 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 20  ry complicated. 
15341 20 54 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c 65   To avoid proble
15342 6d 73 2c 0a 2a 2a 20 74 68 65 20 6e 65 63 65 73  ms,.** the neces
15343 73 61 72 79 20 62 79 74 65 20 73 77 61 70 70 69  sary byte swappi
15344 6e 67 20 69 73 20 63 61 72 72 69 65 64 20 6f 75  ng is carried ou
15345 74 20 75 73 69 6e 67 20 61 20 36 34 2d 62 69 74  t using a 64-bit
15346 20 69 6e 74 65 67 65 72 0a 2a 2a 20 72 61 74 68   integer.** rath
15347 65 72 20 74 68 61 6e 20 61 20 36 34 2d 62 69 74  er than a 64-bit
15348 20 66 6c 6f 61 74 2e 20 20 46 72 61 6e 6b 20 61   float.  Frank a
15349 73 73 75 72 65 73 20 75 73 20 74 68 61 74 20 74  ssures us that t
1534a 68 65 20 63 6f 64 65 20 68 65 72 65 0a 2a 2a 20  he code here.** 
1534b 77 6f 72 6b 73 20 66 6f 72 20 68 69 6d 2e 20 20  works for him.  
1534c 57 65 2c 20 74 68 65 20 64 65 76 65 6c 6f 70 65  We, the develope
1534d 72 73 2c 20 68 61 76 65 20 6e 6f 20 77 61 79 20  rs, have no way 
1534e 74 6f 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79  to independently
1534f 0a 2a 2a 20 76 65 72 69 66 79 20 74 68 69 73 2c  .** verify this,
15350 20 62 75 74 20 46 72 61 6e 6b 20 73 65 65 6d 73   but Frank seems
15351 20 74 6f 20 6b 6e 6f 77 20 77 68 61 74 20 68 65   to know what he
15352 20 69 73 20 74 61 6c 6b 69 6e 67 20 61 62 6f 75   is talking abou
15353 74 0a 2a 2a 20 73 6f 20 77 65 20 74 72 75 73 74  t.** so we trust
15354 20 68 69 6d 2e 0a 2a 2f 0a 23 69 66 64 65 66 20   him..*/.#ifdef 
15355 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f 45 4e 44  SQLITE_MIXED_END
15356 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f 41 54 0a  IAN_64BIT_FLOAT.
15357 73 74 61 74 69 63 20 75 36 34 20 66 6c 6f 61 74  static u64 float
15358 53 77 61 70 28 75 36 34 20 69 6e 29 7b 0a 20 20  Swap(u64 in){.  
15359 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 75 36 34 20  union {.    u64 
1535a 72 3b 0a 20 20 20 20 75 33 32 20 69 5b 32 5d 3b  r;.    u32 i[2];
1535b 0a 20 20 7d 20 75 3b 0a 20 20 75 33 32 20 74 3b  .  } u;.  u32 t;
1535c 0a 0a 20 20 75 2e 72 20 3d 20 69 6e 3b 0a 20 20  ..  u.r = in;.  
1535d 74 20 3d 20 75 2e 69 5b 30 5d 3b 0a 20 20 75 2e  t = u.i[0];.  u.
1535e 69 5b 30 5d 20 3d 20 75 2e 69 5b 31 5d 3b 0a 20  i[0] = u.i[1];. 
1535f 20 75 2e 69 5b 31 5d 20 3d 20 74 3b 0a 20 20 72   u.i[1] = t;.  r
15360 65 74 75 72 6e 20 75 2e 72 3b 0a 7d 0a 23 20 64  eturn u.r;.}.# d
15361 65 66 69 6e 65 20 73 77 61 70 4d 69 78 65 64 45  efine swapMixedE
15362 6e 64 69 61 6e 46 6c 6f 61 74 28 58 29 20 20 58  ndianFloat(X)  X
15363 20 3d 20 66 6c 6f 61 74 53 77 61 70 28 58 29 0a   = floatSwap(X).
15364 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73  #else.# define s
15365 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c  wapMixedEndianFl
15366 6f 61 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f  oat(X).#endif../
15367 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 73  *.** Write the s
15368 65 72 69 61 6c 69 7a 65 64 20 64 61 74 61 20 62  erialized data b
15369 6c 6f 62 20 66 6f 72 20 74 68 65 20 76 61 6c 75  lob for the valu
1536a 65 20 73 74 6f 72 65 64 20 69 6e 20 70 4d 65 6d  e stored in pMem
1536b 20 69 6e 74 6f 20 0a 2a 2a 20 62 75 66 2e 20 49   into .** buf. I
1536c 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61  t is assumed tha
1536d 74 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 73  t the caller has
1536e 20 61 6c 6c 6f 63 61 74 65 64 20 73 75 66 66 69   allocated suffi
1536f 63 69 65 6e 74 20 73 70 61 63 65 2e 0a 2a 2a 20  cient space..** 
15370 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
15371 72 20 6f 66 20 62 79 74 65 73 20 77 72 69 74 74  r of bytes writt
15372 65 6e 2e 0a 2a 2a 0a 2a 2a 20 6e 42 75 66 20 69  en..**.** nBuf i
15373 73 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20  s the amount of 
15374 73 70 61 63 65 20 6c 65 66 74 20 69 6e 20 62 75  space left in bu
15375 66 5b 5d 2e 20 20 6e 42 75 66 20 6d 75 73 74 20  f[].  nBuf must 
15376 61 6c 77 61 79 73 20 62 65 0a 2a 2a 20 6c 61 72  always be.** lar
15377 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c  ge enough to hol
15378 64 20 74 68 65 20 65 6e 74 69 72 65 20 66 69 65  d the entire fie
15379 6c 64 2e 20 20 45 78 63 65 70 74 2c 20 69 66 20  ld.  Except, if 
1537a 74 68 65 20 66 69 65 6c 64 20 69 73 0a 2a 2a 20  the field is.** 
1537b 61 20 62 6c 6f 62 20 77 69 74 68 20 61 20 7a 65  a blob with a ze
1537c 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 2c 20  ro-filled tail, 
1537d 74 68 65 6e 20 62 75 66 5b 5d 20 6d 69 67 68 74  then buf[] might
1537e 20 62 65 20 6a 75 73 74 20 74 68 65 20 72 69 67   be just the rig
1537f 68 74 0a 2a 2a 20 73 69 7a 65 20 74 6f 20 68 6f  ht.** size to ho
15380 6c 64 20 65 76 65 72 79 74 68 69 6e 67 20 65 78  ld everything ex
15381 63 65 70 74 20 66 6f 72 20 74 68 65 20 7a 65 72  cept for the zer
15382 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 2e 20 20  o-filled tail.  
15383 49 66 20 62 75 66 5b 5d 0a 2a 2a 20 69 73 20 6f  If buf[].** is o
15384 6e 6c 79 20 62 69 67 20 65 6e 6f 75 67 68 20 74  nly big enough t
15385 6f 20 68 6f 6c 64 20 74 68 65 20 6e 6f 6e 2d 7a  o hold the non-z
15386 65 72 6f 20 70 72 65 66 69 78 2c 20 74 68 65 6e  ero prefix, then
15387 20 6f 6e 6c 79 20 77 72 69 74 65 20 74 68 61 74   only write that
15388 0a 2a 2a 20 70 72 65 66 69 78 20 69 6e 74 6f 20  .** prefix into 
15389 62 75 66 5b 5d 2e 20 20 42 75 74 20 69 66 20 62  buf[].  But if b
1538a 75 66 5b 5d 20 69 73 20 6c 61 72 67 65 20 65 6e  uf[] is large en
1538b 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 62 6f 74  ough to hold bot
1538c 68 20 74 68 65 0a 2a 2a 20 70 72 65 66 69 78 20  h the.** prefix 
1538d 61 6e 64 20 74 68 65 20 74 61 69 6c 20 74 68 65  and the tail the
1538e 6e 20 77 72 69 74 65 20 74 68 65 20 70 72 65 66  n write the pref
1538f 69 78 20 61 6e 64 20 73 65 74 20 74 68 65 20 74  ix and set the t
15390 61 69 6c 20 74 6f 20 61 6c 6c 0a 2a 2a 20 7a 65  ail to all.** ze
15391 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ros..**.** Retur
15392 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
15393 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20 77  bytes actually w
15394 72 69 74 74 65 6e 20 69 6e 74 6f 20 62 75 66 5b  ritten into buf[
15395 5d 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 0a 2a  ].  The number.*
15396 2a 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  * of bytes in th
15397 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61  e zero-filled ta
15398 69 6c 20 69 73 20 69 6e 63 6c 75 64 65 64 20 69  il is included i
15399 6e 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  n the return val
1539a 75 65 20 6f 6e 6c 79 0a 2a 2a 20 69 66 20 74 68  ue only.** if th
1539b 6f 73 65 20 62 79 74 65 73 20 77 65 72 65 20 7a  ose bytes were z
1539c 65 72 6f 65 64 20 69 6e 20 62 75 66 5b 5d 2e 0a  eroed in buf[]..
1539d 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */ .SQLITE_PRIVA
1539e 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
1539f 62 65 53 65 72 69 61 6c 50 75 74 28 75 38 20 2a  beSerialPut(u8 *
153a0 62 75 66 2c 20 69 6e 74 20 6e 42 75 66 2c 20 4d  buf, int nBuf, M
153a1 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 66 69  em *pMem, int fi
153a2 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a 20 20 75 33  le_format){.  u3
153a3 32 20 73 65 72 69 61 6c 5f 74 79 70 65 20 3d 20  2 serial_type = 
153a4 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
153a5 6c 54 79 70 65 28 70 4d 65 6d 2c 20 66 69 6c 65  lType(pMem, file
153a6 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 69 6e 74 20  _format);.  int 
153a7 6c 65 6e 3b 0a 0a 20 20 2f 2a 20 49 6e 74 65 67  len;..  /* Integ
153a8 65 72 20 61 6e 64 20 52 65 61 6c 20 2a 2f 0a 20  er and Real */. 
153a9 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70 65   if( serial_type
153aa 3c 3d 37 20 26 26 20 73 65 72 69 61 6c 5f 74 79  <=7 && serial_ty
153ab 70 65 3e 30 20 29 7b 0a 20 20 20 20 75 36 34 20  pe>0 ){.    u64 
153ac 76 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  v;.    int i;.  
153ad 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70    if( serial_typ
153ae 65 3d 3d 37 20 29 7b 0a 20 20 20 20 20 20 61 73  e==7 ){.      as
153af 73 65 72 74 28 20 73 69 7a 65 6f 66 28 76 29 3d  sert( sizeof(v)=
153b0 3d 73 69 7a 65 6f 66 28 70 4d 65 6d 2d 3e 72 29  =sizeof(pMem->r)
153b1 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   );.      memcpy
153b2 28 26 76 2c 20 26 70 4d 65 6d 2d 3e 72 2c 20 73  (&v, &pMem->r, s
153b3 69 7a 65 6f 66 28 76 29 29 3b 0a 20 20 20 20 20  izeof(v));.     
153b4 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e   swapMixedEndian
153b5 46 6c 6f 61 74 28 76 29 3b 0a 20 20 20 20 7d 65  Float(v);.    }e
153b6 6c 73 65 7b 0a 20 20 20 20 20 20 76 20 3d 20 70  lse{.      v = p
153b7 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 20 20 7d 0a  Mem->u.i;.    }.
153b8 20 20 20 20 6c 65 6e 20 3d 20 69 20 3d 20 73 71      len = i = sq
153b9 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54  lite3VdbeSerialT
153ba 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79  ypeLen(serial_ty
153bb 70 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  pe);.    assert(
153bc 20 6c 65 6e 3c 3d 6e 42 75 66 20 29 3b 0a 20 20   len<=nBuf );.  
153bd 20 20 77 68 69 6c 65 28 20 69 2d 2d 20 29 7b 0a    while( i-- ){.
153be 20 20 20 20 20 20 62 75 66 5b 69 5d 20 3d 20 28        buf[i] = (
153bf 76 26 30 78 46 46 29 3b 0a 20 20 20 20 20 20 76  v&0xFF);.      v
153c0 20 3e 3e 3d 20 38 3b 0a 20 20 20 20 7d 0a 20 20   >>= 8;.    }.  
153c1 20 20 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20    return len;.  
153c2 7d 0a 0a 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f  }..  /* String o
153c3 72 20 62 6c 6f 62 20 2a 2f 0a 20 20 69 66 28 20  r blob */.  if( 
153c4 73 65 72 69 61 6c 5f 74 79 70 65 3e 3d 31 32 20  serial_type>=12 
153c5 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
153c6 4d 65 6d 2d 3e 6e 20 2b 20 28 28 70 4d 65 6d 2d  Mem->n + ((pMem-
153c7 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72  >flags & MEM_Zer
153c8 6f 29 3f 70 4d 65 6d 2d 3e 75 2e 69 3a 30 29 0a  o)?pMem->u.i:0).
153c9 20 20 20 20 20 20 20 20 20 20 20 20 20 3d 3d 20               == 
153ca 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
153cb 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f  lTypeLen(serial_
153cc 74 79 70 65 29 20 29 3b 0a 20 20 20 20 61 73 73  type) );.    ass
153cd 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 3c 3d 6e 42  ert( pMem->n<=nB
153ce 75 66 20 29 3b 0a 20 20 20 20 6c 65 6e 20 3d 20  uf );.    len = 
153cf 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 20 20 6d 65 6d  pMem->n;.    mem
153d0 63 70 79 28 62 75 66 2c 20 70 4d 65 6d 2d 3e 7a  cpy(buf, pMem->z
153d1 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 69 66 28 20  , len);.    if( 
153d2 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
153d3 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20  M_Zero ){.      
153d4 6c 65 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 69  len += pMem->u.i
153d5 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 65 6e 3e  ;.      if( len>
153d6 6e 42 75 66 20 29 7b 0a 20 20 20 20 20 20 20 20  nBuf ){.        
153d7 6c 65 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 20 20  len = nBuf;.    
153d8 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74    }.      memset
153d9 28 26 62 75 66 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20  (&buf[pMem->n], 
153da 30 2c 20 6c 65 6e 2d 70 4d 65 6d 2d 3e 6e 29 3b  0, len-pMem->n);
153db 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
153dc 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  n len;.  }..  /*
153dd 20 4e 55 4c 4c 20 6f 72 20 63 6f 6e 73 74 61 6e   NULL or constan
153de 74 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 72  ts 0 or 1 */.  r
153df 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
153e0 2a 20 44 65 73 65 72 69 61 6c 69 7a 65 20 74 68  * Deserialize th
153e1 65 20 64 61 74 61 20 62 6c 6f 62 20 70 6f 69 6e  e data blob poin
153e2 74 65 64 20 74 6f 20 62 79 20 62 75 66 20 61 73  ted to by buf as
153e3 20 73 65 72 69 61 6c 20 74 79 70 65 20 73 65 72   serial type ser
153e4 69 61 6c 5f 74 79 70 65 0a 2a 2a 20 61 6e 64 20  ial_type.** and 
153e5 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  store the result
153e6 20 69 6e 20 70 4d 65 6d 2e 20 20 52 65 74 75 72   in pMem.  Retur
153e7 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
153e8 62 79 74 65 73 20 72 65 61 64 2e 0a 2a 2f 20 0a  bytes read..*/ .
153e9 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
153ea 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  nt sqlite3VdbeSe
153eb 72 69 61 6c 47 65 74 28 0a 20 20 63 6f 6e 73 74  rialGet(.  const
153ec 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
153ed 62 75 66 2c 20 20 20 20 20 2f 2a 20 42 75 66 66  buf,     /* Buff
153ee 65 72 20 74 6f 20 64 65 73 65 72 69 61 6c 69 7a  er to deserializ
153ef 65 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20  e from */.  u32 
153f0 73 65 72 69 61 6c 5f 74 79 70 65 2c 20 20 20 20  serial_type,    
153f1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 72            /* Ser
153f2 69 61 6c 20 74 79 70 65 20 74 6f 20 64 65 73 65  ial type to dese
153f3 72 69 61 6c 69 7a 65 20 2a 2f 0a 20 20 4d 65 6d  rialize */.  Mem
153f4 20 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 20 20   *pMem          
153f5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65             /* Me
153f6 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 77 72 69  mory cell to wri
153f7 74 65 20 76 61 6c 75 65 20 69 6e 74 6f 20 2a 2f  te value into */
153f8 0a 29 7b 0a 20 20 73 77 69 74 63 68 28 20 73 65  .){.  switch( se
153f9 72 69 61 6c 5f 74 79 70 65 20 29 7b 0a 20 20 20  rial_type ){.   
153fa 20 63 61 73 65 20 31 30 3a 20 20 20 2f 2a 20 52   case 10:   /* R
153fb 65 73 65 72 76 65 64 20 66 6f 72 20 66 75 74 75  eserved for futu
153fc 72 65 20 75 73 65 20 2a 2f 0a 20 20 20 20 63 61  re use */.    ca
153fd 73 65 20 31 31 3a 20 20 20 2f 2a 20 52 65 73 65  se 11:   /* Rese
153fe 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 65 20  rved for future 
153ff 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  use */.    case 
15400 30 3a 20 7b 20 20 2f 2a 20 4e 55 4c 4c 20 2a 2f  0: {  /* NULL */
15401 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  .      pMem->fla
15402 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
15403 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
15404 7d 0a 20 20 20 20 63 61 73 65 20 31 3a 20 7b 20  }.    case 1: { 
15405 2f 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64  /* 1-byte signed
15406 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20   integer */.    
15407 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 73    pMem->u.i = (s
15408 69 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30  igned char)buf[0
15409 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  ];.      pMem->f
1540a 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
1540b 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
1540c 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 32      }.    case 2
1540d 3a 20 7b 20 2f 2a 20 32 2d 62 79 74 65 20 73 69  : { /* 2-byte si
1540e 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a  gned integer */.
1540f 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20        pMem->u.i 
15410 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68 61 72  = (((signed char
15411 29 62 75 66 5b 30 5d 29 3c 3c 38 29 20 7c 20 62  )buf[0])<<8) | b
15412 75 66 5b 31 5d 3b 0a 20 20 20 20 20 20 70 4d 65  uf[1];.      pMe
15413 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49  m->flags = MEM_I
15414 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  nt;.      return
15415 20 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   2;.    }.    ca
15416 73 65 20 33 3a 20 7b 20 2f 2a 20 33 2d 62 79 74  se 3: { /* 3-byt
15417 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  e signed integer
15418 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e   */.      pMem->
15419 75 2e 69 20 3d 20 28 28 28 73 69 67 6e 65 64 20  u.i = (((signed 
1541a 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 31 36  char)buf[0])<<16
1541b 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 38 29 20  ) | (buf[1]<<8) 
1541c 7c 20 62 75 66 5b 32 5d 3b 0a 20 20 20 20 20 20  | buf[2];.      
1541d 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
1541e 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74  M_Int;.      ret
1541f 75 72 6e 20 33 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 3;.    }.   
15420 20 63 61 73 65 20 34 3a 20 7b 20 2f 2a 20 34 2d   case 4: { /* 4-
15421 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65  byte signed inte
15422 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65  ger */.      pMe
15423 6d 2d 3e 75 2e 69 20 3d 20 28 62 75 66 5b 30 5d  m->u.i = (buf[0]
15424 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 31 5d 3c  <<24) | (buf[1]<
15425 3c 31 36 29 20 7c 20 28 62 75 66 5b 32 5d 3c 3c  <16) | (buf[2]<<
15426 38 29 20 7c 20 62 75 66 5b 33 5d 3b 0a 20 20 20  8) | buf[3];.   
15427 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
15428 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20   MEM_Int;.      
15429 72 65 74 75 72 6e 20 34 3b 0a 20 20 20 20 7d 0a  return 4;.    }.
1542a 20 20 20 20 63 61 73 65 20 35 3a 20 7b 20 2f 2a      case 5: { /*
1542b 20 36 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69   6-byte signed i
1542c 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20  nteger */.      
1542d 75 36 34 20 78 20 3d 20 28 28 28 73 69 67 6e 65  u64 x = (((signe
1542e 64 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c  d char)buf[0])<<
1542f 38 29 20 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20  8) | buf[1];.   
15430 20 20 20 75 33 32 20 79 20 3d 20 28 62 75 66 5b     u32 y = (buf[
15431 32 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 33  2]<<24) | (buf[3
15432 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 34 5d  ]<<16) | (buf[4]
15433 3c 3c 38 29 20 7c 20 62 75 66 5b 35 5d 3b 0a 20  <<8) | buf[5];. 
15434 20 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 29       x = (x<<32)
15435 20 7c 20 79 3b 0a 20 20 20 20 20 20 70 4d 65 6d   | y;.      pMem
15436 2d 3e 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26  ->u.i = *(i64*)&
15437 78 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  x;.      pMem->f
15438 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
15439 20 20 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a        return 6;.
1543a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 36      }.    case 6
1543b 3a 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 73 69  :   /* 8-byte si
1543c 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a  gned integer */.
1543d 20 20 20 20 63 61 73 65 20 37 3a 20 7b 20 2f 2a      case 7: { /*
1543e 20 49 45 45 45 20 66 6c 6f 61 74 69 6e 67 20 70   IEEE floating p
1543f 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 75 36  oint */.      u6
15440 34 20 78 3b 0a 20 20 20 20 20 20 75 33 32 20 79  4 x;.      u32 y
15441 3b 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e  ;.#if !defined(N
15442 44 45 42 55 47 29 20 26 26 20 21 64 65 66 69 6e  DEBUG) && !defin
15443 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ed(SQLITE_OMIT_F
15444 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 29 0a 20  LOATING_POINT). 
15445 20 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74       /* Verify t
15446 68 61 74 20 69 6e 74 65 67 65 72 73 20 61 6e 64  hat integers and
15447 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20   floating point 
15448 76 61 6c 75 65 73 20 75 73 65 20 74 68 65 20 73  values use the s
15449 61 6d 65 0a 20 20 20 20 20 20 2a 2a 20 62 79 74  ame.      ** byt
1544a 65 20 6f 72 64 65 72 2e 20 20 4f 72 2c 20 74 68  e order.  Or, th
1544b 61 74 20 69 66 20 53 51 4c 49 54 45 5f 4d 49 58  at if SQLITE_MIX
1544c 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f  ED_ENDIAN_64BIT_
1544d 46 4c 4f 41 54 20 69 73 0a 20 20 20 20 20 20 2a  FLOAT is.      *
1544e 2a 20 64 65 66 69 6e 65 64 20 74 68 61 74 20 36  * defined that 6
1544f 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70  4-bit floating p
15450 6f 69 6e 74 20 76 61 6c 75 65 73 20 72 65 61 6c  oint values real
15451 6c 79 20 61 72 65 20 6d 69 78 65 64 0a 20 20 20  ly are mixed.   
15452 20 20 20 2a 2a 20 65 6e 64 69 61 6e 2e 0a 20 20     ** endian..  
15453 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 74 61      */.      sta
15454 74 69 63 20 63 6f 6e 73 74 20 75 36 34 20 74 31  tic const u64 t1
15455 20 3d 20 28 28 75 36 34 29 30 78 33 66 66 30 30   = ((u64)0x3ff00
15456 30 30 30 29 3c 3c 33 32 3b 0a 20 20 20 20 20 20  000)<<32;.      
15457 73 74 61 74 69 63 20 63 6f 6e 73 74 20 64 6f 75  static const dou
15458 62 6c 65 20 72 31 20 3d 20 31 2e 30 3b 0a 20 20  ble r1 = 1.0;.  
15459 20 20 20 20 75 36 34 20 74 32 20 3d 20 74 31 3b      u64 t2 = t1;
1545a 0a 20 20 20 20 20 20 73 77 61 70 4d 69 78 65 64  .      swapMixed
1545b 45 6e 64 69 61 6e 46 6c 6f 61 74 28 74 32 29 3b  EndianFloat(t2);
1545c 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73  .      assert( s
1545d 69 7a 65 6f 66 28 72 31 29 3d 3d 73 69 7a 65 6f  izeof(r1)==sizeo
1545e 66 28 74 32 29 20 26 26 20 6d 65 6d 63 6d 70 28  f(t2) && memcmp(
1545f 26 72 31 2c 20 26 74 32 2c 20 73 69 7a 65 6f 66  &r1, &t2, sizeof
15460 28 72 31 29 29 3d 3d 30 20 29 3b 0a 23 65 6e 64  (r1))==0 );.#end
15461 69 66 0a 0a 20 20 20 20 20 20 78 20 3d 20 28 62  if..      x = (b
15462 75 66 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 62 75  uf[0]<<24) | (bu
15463 66 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66  f[1]<<16) | (buf
15464 5b 32 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 33 5d  [2]<<8) | buf[3]
15465 3b 0a 20 20 20 20 20 20 79 20 3d 20 28 62 75 66  ;.      y = (buf
15466 5b 34 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b  [4]<<24) | (buf[
15467 35 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 36  5]<<16) | (buf[6
15468 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 37 5d 3b 0a  ]<<8) | buf[7];.
15469 20 20 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32        x = (x<<32
1546a 29 20 7c 20 79 3b 0a 20 20 20 20 20 20 69 66 28  ) | y;.      if(
1546b 20 73 65 72 69 61 6c 5f 74 79 70 65 3d 3d 36 20   serial_type==6 
1546c 29 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d  ){.        pMem-
1546d 3e 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78  >u.i = *(i64*)&x
1546e 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e  ;.        pMem->
1546f 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
15470 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
15471 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 69        assert( si
15472 7a 65 6f 66 28 78 29 3d 3d 38 20 26 26 20 73 69  zeof(x)==8 && si
15473 7a 65 6f 66 28 70 4d 65 6d 2d 3e 72 29 3d 3d 38  zeof(pMem->r)==8
15474 20 29 3b 0a 20 20 20 20 20 20 20 20 73 77 61 70   );.        swap
15475 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74  MixedEndianFloat
15476 28 78 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d  (x);.        mem
15477 63 70 79 28 26 70 4d 65 6d 2d 3e 72 2c 20 26 78  cpy(&pMem->r, &x
15478 2c 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20  , sizeof(x));.  
15479 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
1547a 73 20 3d 20 73 71 6c 69 74 65 33 49 73 4e 61 4e  s = sqlite3IsNaN
1547b 28 70 4d 65 6d 2d 3e 72 29 20 3f 20 4d 45 4d 5f  (pMem->r) ? MEM_
1547c 4e 75 6c 6c 20 3a 20 4d 45 4d 5f 52 65 61 6c 3b  Null : MEM_Real;
1547d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
1547e 65 74 75 72 6e 20 38 3b 0a 20 20 20 20 7d 0a 20  eturn 8;.    }. 
1547f 20 20 20 63 61 73 65 20 38 3a 20 20 20 20 2f 2a     case 8:    /*
15480 20 49 6e 74 65 67 65 72 20 30 20 2a 2f 0a 20 20   Integer 0 */.  
15481 20 20 63 61 73 65 20 39 3a 20 7b 20 20 2f 2a 20    case 9: {  /* 
15482 49 6e 74 65 67 65 72 20 31 20 2a 2f 0a 20 20 20  Integer 1 */.   
15483 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73     pMem->u.i = s
15484 65 72 69 61 6c 5f 74 79 70 65 2d 38 3b 0a 20 20  erial_type-8;.  
15485 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
15486 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20  = MEM_Int;.     
15487 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
15488 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a  .    default: {.
15489 20 20 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20        int len = 
1548a 28 73 65 72 69 61 6c 5f 74 79 70 65 2d 31 32 29  (serial_type-12)
1548b 2f 32 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  /2;.      pMem->
1548c 7a 20 3d 20 28 63 68 61 72 20 2a 29 62 75 66 3b  z = (char *)buf;
1548d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d  .      pMem->n =
1548e 20 6c 65 6e 3b 0a 20 20 20 20 20 20 70 4d 65 6d   len;.      pMem
1548f 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20  ->xDel = 0;.    
15490 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70    if( serial_typ
15491 65 26 30 78 30 31 20 29 7b 0a 20 20 20 20 20 20  e&0x01 ){.      
15492 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
15493 4d 45 4d 5f 53 74 72 20 7c 20 4d 45 4d 5f 45 70  MEM_Str | MEM_Ep
15494 68 65 6d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  hem;.      }else
15495 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e  {.        pMem->
15496 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62  flags = MEM_Blob
15497 20 7c 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20   | MEM_Ephem;.  
15498 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
15499 72 6e 20 6c 65 6e 3b 0a 20 20 20 20 7d 0a 20 20  rn len;.    }.  
1549a 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
1549b 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68  ../*.** Given th
1549c 65 20 6e 4b 65 79 2d 62 79 74 65 20 65 6e 63 6f  e nKey-byte enco
1549d 64 69 6e 67 20 6f 66 20 61 20 72 65 63 6f 72 64  ding of a record
1549e 20 69 6e 20 70 4b 65 79 5b 5d 2c 20 70 61 72 73   in pKey[], pars
1549f 65 20 74 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20  e the.** record 
154a0 69 6e 74 6f 20 61 20 55 6e 70 61 63 6b 65 64 52  into a UnpackedR
154a1 65 63 6f 72 64 20 73 74 72 75 63 74 75 72 65 2e  ecord structure.
154a2 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74    Return a point
154a3 65 72 20 74 6f 0a 2a 2a 20 74 68 61 74 20 73 74  er to.** that st
154a4 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54  ructure..**.** T
154a5 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
154a6 69 6f 6e 20 6d 69 67 68 74 20 70 72 6f 76 69 64  ion might provid
154a7 65 20 73 7a 53 70 61 63 65 20 62 79 74 65 73 20  e szSpace bytes 
154a8 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 70 61  of memory.** spa
154a9 63 65 20 61 74 20 70 53 70 61 63 65 2e 20 20 54  ce at pSpace.  T
154aa 68 69 73 20 73 70 61 63 65 20 63 61 6e 20 62 65  his space can be
154ab 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68   used to hold th
154ac 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 56 44  e returned.** VD
154ad 62 65 50 61 72 73 65 64 52 65 63 6f 72 64 20 73  beParsedRecord s
154ae 74 72 75 63 74 75 72 65 20 69 66 20 69 74 20 69  tructure if it i
154af 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2e 20  s large enough. 
154b0 20 49 66 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74   If it is.** not
154b1 20 62 69 67 20 65 6e 6f 75 67 68 2c 20 73 70 61   big enough, spa
154b2 63 65 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66  ce is obtained f
154b3 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  rom sqlite3_mall
154b4 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  oc()..**.** The 
154b5 72 65 74 75 72 6e 65 64 20 73 74 72 75 63 74 75  returned structu
154b6 72 65 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 6f  re should be clo
154b7 73 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f  sed by a call to
154b8 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 44  .** sqlite3VdbeD
154b9 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63  eleteUnpackedRec
154ba 6f 72 64 28 29 2e 0a 2a 2f 20 0a 53 51 4c 49 54  ord()..*/ .SQLIT
154bb 45 5f 50 52 49 56 41 54 45 20 55 6e 70 61 63 6b  E_PRIVATE Unpack
154bc 65 64 52 65 63 6f 72 64 20 2a 73 71 6c 69 74 65  edRecord *sqlite
154bd 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63  3VdbeRecordUnpac
154be 6b 28 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  k(.  KeyInfo *pK
154bf 65 79 49 6e 66 6f 2c 20 20 20 20 20 2f 2a 20 49  eyInfo,     /* I
154c0 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
154c1 20 74 68 65 20 72 65 63 6f 72 64 20 66 6f 72 6d   the record form
154c2 61 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79  at */.  int nKey
154c3 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
154c4 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 62 69  * Size of the bi
154c5 6e 61 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20  nary record */. 
154c6 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
154c7 79 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62  y,      /* The b
154c8 69 6e 61 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a  inary record */.
154c9 20 20 76 6f 69 64 20 2a 70 53 70 61 63 65 2c 20    void *pSpace, 
154ca 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63           /* Spac
154cb 65 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 68  e available to h
154cc 6f 6c 64 20 72 65 73 75 6c 74 69 6e 67 20 6f 62  old resulting ob
154cd 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 73 7a  ject */.  int sz
154ce 53 70 61 63 65 20 20 20 20 20 20 20 20 20 20 20  Space           
154cf 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 53 70 61   /* Size of pSpa
154d0 63 65 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f  ce[] in bytes */
154d1 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69  .){.  const unsi
154d2 67 6e 65 64 20 63 68 61 72 20 2a 61 4b 65 79 20  gned char *aKey 
154d3 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  = (const unsigne
154d4 64 20 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a 20  d char *)pKey;. 
154d5 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
154d6 2a 70 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b  *p;.  int nByte;
154d7 0a 20 20 69 6e 74 20 69 2c 20 69 64 78 2c 20 64  .  int i, idx, d
154d8 3b 0a 20 20 75 33 32 20 73 7a 48 64 72 3b 0a 20  ;.  u32 szHdr;. 
154d9 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 0a 20   Mem *pMem;.  . 
154da 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28   assert( sizeof(
154db 4d 65 6d 29 3e 73 69 7a 65 6f 66 28 2a 70 29 20  Mem)>sizeof(*p) 
154dc 29 3b 0a 20 20 6e 42 79 74 65 20 3d 20 73 69 7a  );.  nByte = siz
154dd 65 6f 66 28 4d 65 6d 29 2a 28 70 4b 65 79 49 6e  eof(Mem)*(pKeyIn
154de 66 6f 2d 3e 6e 46 69 65 6c 64 2b 32 29 3b 0a 20  fo->nField+2);. 
154df 20 69 66 28 20 6e 42 79 74 65 3e 73 7a 53 70 61   if( nByte>szSpa
154e0 63 65 20 29 7b 0a 20 20 20 20 70 20 3d 20 73 71  ce ){.    p = sq
154e1 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
154e2 28 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 6e  (pKeyInfo->db, n
154e3 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20 70  Byte);.    if( p
154e4 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
154e5 20 20 20 20 70 2d 3e 6e 65 65 64 46 72 65 65 20      p->needFree 
154e6 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 1;.  }else{.  
154e7 20 20 70 20 3d 20 70 53 70 61 63 65 3b 0a 20 20    p = pSpace;.  
154e8 20 20 70 2d 3e 6e 65 65 64 46 72 65 65 20 3d 20    p->needFree = 
154e9 30 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4b 65 79  0;.  }.  p->pKey
154ea 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b  Info = pKeyInfo;
154eb 0a 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 70  .  p->nField = p
154ec 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20  KeyInfo->nField 
154ed 2b 20 31 3b 0a 20 20 70 2d 3e 6e 65 65 64 44 65  + 1;.  p->needDe
154ee 73 74 72 6f 79 20 3d 20 31 3b 0a 20 20 70 2d 3e  stroy = 1;.  p->
154ef 61 4d 65 6d 20 3d 20 70 4d 65 6d 20 3d 20 26 28  aMem = pMem = &(
154f0 28 4d 65 6d 2a 29 70 29 5b 31 5d 3b 0a 20 20 69  (Mem*)p)[1];.  i
154f1 64 78 20 3d 20 67 65 74 56 61 72 69 6e 74 33 32  dx = getVarint32
154f2 28 61 4b 65 79 2c 20 73 7a 48 64 72 29 3b 0a 20  (aKey, szHdr);. 
154f3 20 64 20 3d 20 73 7a 48 64 72 3b 0a 20 20 69 20   d = szHdr;.  i 
154f4 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 64  = 0;.  while( id
154f5 78 3c 73 7a 48 64 72 20 26 26 20 69 3c 70 2d 3e  x<szHdr && i<p->
154f6 6e 46 69 65 6c 64 20 29 7b 0a 20 20 20 20 75 33  nField ){.    u3
154f7 32 20 73 65 72 69 61 6c 5f 74 79 70 65 3b 0a 0a  2 serial_type;..
154f8 20 20 20 20 69 64 78 20 2b 3d 20 67 65 74 56 61      idx += getVa
154f9 72 69 6e 74 33 32 28 20 61 4b 65 79 2b 69 64 78  rint32( aKey+idx
154fa 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a  , serial_type);.
154fb 20 20 20 20 69 66 28 20 64 3e 3d 6e 4b 65 79 20      if( d>=nKey 
154fc 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  && sqlite3VdbeSe
154fd 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69  rialTypeLen(seri
154fe 61 6c 5f 74 79 70 65 29 3e 30 20 29 20 62 72 65  al_type)>0 ) bre
154ff 61 6b 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e  ak;.    pMem->en
15500 63 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e  c = pKeyInfo->en
15501 63 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 64 62 20  c;.    pMem->db 
15502 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 3b 0a  = pKeyInfo->db;.
15503 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
15504 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a  = 0;.    pMem->z
15505 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20  Malloc = 0;.    
15506 64 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65  d += sqlite3Vdbe
15507 53 65 72 69 61 6c 47 65 74 28 26 61 4b 65 79 5b  SerialGet(&aKey[
15508 64 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 2c  d], serial_type,
15509 20 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d   pMem);.    pMem
1550a 2b 2b 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d  ++;.    i++;.  }
1550b 0a 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 69  .  p->nField = i
1550c 3b 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64  ;.  return (void
1550d 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  *)p;.}../*.** Th
1550e 69 73 20 72 6f 75 74 69 6e 65 20 64 65 73 74 72  is routine destr
1550f 6f 79 73 20 61 20 55 6e 70 61 63 6b 65 64 52 65  oys a UnpackedRe
15510 63 6f 72 64 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53  cord object.*/.S
15511 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
15512 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65  id sqlite3VdbeDe
15513 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f  leteUnpackedReco
15514 72 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  rd(UnpackedRecor
15515 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29  d *p){.  if( p )
15516 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 65 65  {.    if( p->nee
15517 64 44 65 73 74 72 6f 79 20 29 7b 0a 20 20 20 20  dDestroy ){.    
15518 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 4d    int i;.      M
15519 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 20 20 20 20  em *pMem;.      
1551a 66 6f 72 28 69 3d 30 2c 20 70 4d 65 6d 3d 70 2d  for(i=0, pMem=p-
1551b 3e 61 4d 65 6d 3b 20 69 3c 70 2d 3e 6e 46 69 65  >aMem; i<p->nFie
1551c 6c 64 3b 20 69 2b 2b 2c 20 70 4d 65 6d 2b 2b 29  ld; i++, pMem++)
1551d 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4d  {.        if( pM
1551e 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20  em->zMalloc ){. 
1551f 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
15520 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
15521 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Mem);.        }.
15522 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
15523 20 20 69 66 28 20 70 2d 3e 6e 65 65 64 46 72 65    if( p->needFre
15524 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  e ){.      sqlit
15525 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20  e3_free(p);.    
15526 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  }.  }.}../*.** T
15527 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d  his function com
15528 70 61 72 65 73 20 74 68 65 20 74 77 6f 20 74 61  pares the two ta
15529 62 6c 65 20 72 6f 77 73 20 6f 72 20 69 6e 64 65  ble rows or inde
1552a 78 20 72 65 63 6f 72 64 73 0a 2a 2a 20 73 70 65  x records.** spe
1552b 63 69 66 69 65 64 20 62 79 20 7b 6e 4b 65 79 31  cified by {nKey1
1552c 2c 20 70 4b 65 79 31 7d 20 61 6e 64 20 70 50 4b  , pKey1} and pPK
1552d 65 79 32 2e 20 20 49 74 20 72 65 74 75 72 6e 73  ey2.  It returns
1552e 20 61 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72   a negative, zer
1552f 6f 0a 2a 2a 20 6f 72 20 70 6f 73 69 74 69 76 65  o.** or positive
15530 20 69 6e 74 65 67 65 72 20 69 66 20 7b 6e 4b 65   integer if {nKe
15531 79 31 2c 20 70 4b 65 79 31 7d 20 69 73 20 6c 65  y1, pKey1} is le
15532 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c 20 74  ss than, equal t
15533 6f 20 6f 72 20 0a 2a 2a 20 67 72 65 61 74 65 72  o or .** greater
15534 20 74 68 61 6e 20 70 50 4b 65 79 32 2e 20 20 54   than pPKey2.  T
15535 68 65 20 7b 6e 4b 65 79 31 2c 20 70 4b 65 79 31  he {nKey1, pKey1
15536 7d 20 6b 65 79 20 6d 75 73 74 20 62 65 20 61 20  } key must be a 
15537 62 6c 6f 62 0a 2a 2a 20 63 72 65 61 74 65 64 20  blob.** created 
15538 62 79 20 74 68 20 4f 50 5f 4d 61 6b 65 52 65 63  by th OP_MakeRec
15539 6f 72 64 20 6f 70 63 6f 64 65 20 6f 66 20 74 68  ord opcode of th
1553a 65 20 56 44 42 45 2e 20 20 54 68 65 20 70 50 4b  e VDBE.  The pPK
1553b 65 79 32 0a 2a 2a 20 6b 65 79 20 6d 75 73 74 20  ey2.** key must 
1553c 62 65 20 61 20 70 61 72 73 65 64 20 6b 65 79 20  be a parsed key 
1553d 73 75 63 68 20 61 73 20 6f 62 74 61 69 6e 65 64  such as obtained
1553e 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33   from.** sqlite3
1553f 56 64 62 65 50 61 72 73 65 52 65 63 6f 72 64 2e  VdbeParseRecord.
15540 0a 2a 2a 0a 2a 2a 20 4b 65 79 31 20 61 6e 64 20  .**.** Key1 and 
15541 4b 65 79 32 20 64 6f 20 6e 6f 74 20 68 61 76 65  Key2 do not have
15542 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20   to contain the 
15543 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20 66  same number of f
15544 69 65 6c 64 73 2e 0a 2a 2a 20 42 75 74 20 69 66  ields..** But if
15545 20 74 68 65 20 6c 65 6e 67 74 68 73 20 64 69 66   the lengths dif
15546 66 65 72 2c 20 4b 65 79 32 20 6d 75 73 74 20 62  fer, Key2 must b
15547 65 20 74 68 65 20 73 68 6f 72 74 65 72 20 6f 66  e the shorter of
15548 20 74 68 65 20 74 77 6f 2e 0a 2a 2a 0a 2a 2a 20   the two..**.** 
15549 48 69 73 74 6f 72 69 63 61 6c 20 6e 6f 74 65 3a  Historical note:
1554a 20 49 6e 20 65 61 72 6c 69 65 72 20 76 65 72 73   In earlier vers
1554b 69 6f 6e 73 20 6f 66 20 74 68 69 73 20 72 6f 75  ions of this rou
1554c 74 69 6e 65 20 62 6f 74 68 20 4b 65 79 31 0a 2a  tine both Key1.*
1554d 2a 20 61 6e 64 20 4b 65 79 32 20 77 65 72 65 20  * and Key2 were 
1554e 62 6c 6f 62 73 20 6f 62 74 61 69 6e 65 64 20 66  blobs obtained f
1554f 72 6f 6d 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  rom OP_MakeRecor
15550 64 2e 20 20 42 75 74 20 77 65 20 66 6f 75 6e 64  d.  But we found
15551 0a 2a 2a 20 74 68 61 74 20 69 6e 20 74 79 70 69  .** that in typi
15552 63 61 6c 20 75 73 65 20 74 68 65 20 73 61 6d 65  cal use the same
15553 20 4b 65 79 32 20 77 6f 75 6c 64 20 62 65 20 73   Key2 would be s
15554 75 62 6d 69 74 74 65 64 20 6d 75 6c 74 69 70 6c  ubmitted multipl
15555 65 20 74 69 6d 65 73 0a 2a 2a 20 69 6e 20 61 20  e times.** in a 
15556 72 6f 77 2e 20 20 53 6f 20 61 6e 20 6f 70 74 69  row.  So an opti
15557 6d 69 7a 61 74 69 6f 6e 20 77 61 73 20 61 64 64  mization was add
15558 65 64 20 74 6f 20 70 61 72 73 65 20 74 68 65 20  ed to parse the 
15559 4b 65 79 32 20 6b 65 79 0a 2a 2a 20 73 65 70 61  Key2 key.** sepa
1555a 72 61 74 65 6c 79 20 61 6e 64 20 73 75 62 6d 69  rately and submi
1555b 74 20 74 68 65 20 70 61 72 73 65 64 20 76 65 72  t the parsed ver
1555c 73 69 6f 6e 2e 20 20 49 6e 20 74 68 69 73 20 77  sion.  In this w
1555d 61 79 2c 20 77 65 20 61 76 6f 69 64 0a 2a 2a 20  ay, we avoid.** 
1555e 70 61 72 73 69 6e 67 20 74 68 65 20 73 61 6d 65  parsing the same
1555f 20 4b 65 79 32 20 6d 75 6c 74 69 70 6c 65 20 74   Key2 multiple t
15560 69 6d 65 73 20 69 6e 20 61 20 72 6f 77 2e 0a 2a  imes in a row..*
15561 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
15562 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
15563 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 0a 20  RecordCompare(. 
15564 20 69 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e 73   int nKey1, cons
15565 74 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 0a  t void *pKey1, .
15566 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
15567 20 2a 70 50 4b 65 79 32 0a 29 7b 0a 20 20 75 33   *pPKey2.){.  u3
15568 32 20 64 31 3b 20 20 20 20 20 20 20 20 20 20 20  2 d1;           
15569 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20   /* Offset into 
1556a 61 4b 65 79 5b 5d 20 6f 66 20 6e 65 78 74 20 64  aKey[] of next d
1556b 61 74 61 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20  ata element */. 
1556c 20 75 33 32 20 69 64 78 31 3b 20 20 20 20 20 20   u32 idx1;      
1556d 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e      /* Offset in
1556e 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20 6e 65 78  to aKey[] of nex
1556f 74 20 68 65 61 64 65 72 20 65 6c 65 6d 65 6e 74  t header element
15570 20 2a 2f 0a 20 20 75 33 32 20 73 7a 48 64 72 31   */.  u32 szHdr1
15571 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ;        /* Numb
15572 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 68  er of bytes in h
15573 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69  eader */.  int i
15574 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 46 69 65   = 0;.  int nFie
15575 6c 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30  ld;.  int rc = 0
15576 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ;.  const unsign
15577 65 64 20 63 68 61 72 20 2a 61 4b 65 79 31 20 3d  ed char *aKey1 =
15578 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64   (const unsigned
15579 20 63 68 61 72 20 2a 29 70 4b 65 79 31 3b 0a 20   char *)pKey1;. 
1557a 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
1557b 66 6f 3b 0a 20 20 4d 65 6d 20 6d 65 6d 31 3b 0a  fo;.  Mem mem1;.
1557c 0a 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 70 50  .  pKeyInfo = pP
1557d 4b 65 79 32 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a  Key2->pKeyInfo;.
1557e 20 20 6d 65 6d 31 2e 65 6e 63 20 3d 20 70 4b 65    mem1.enc = pKe
1557f 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a 20 20 6d 65  yInfo->enc;.  me
15580 6d 31 2e 64 62 20 3d 20 70 4b 65 79 49 6e 66 6f  m1.db = pKeyInfo
15581 2d 3e 64 62 3b 0a 20 20 6d 65 6d 31 2e 66 6c 61  ->db;.  mem1.fla
15582 67 73 20 3d 20 30 3b 0a 20 20 6d 65 6d 31 2e 7a  gs = 0;.  mem1.z
15583 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 0a 20  Malloc = 0;.  . 
15584 20 69 64 78 31 20 3d 20 67 65 74 56 61 72 69 6e   idx1 = getVarin
15585 74 33 32 28 61 4b 65 79 31 2c 20 73 7a 48 64 72  t32(aKey1, szHdr
15586 31 29 3b 0a 20 20 64 31 20 3d 20 73 7a 48 64 72  1);.  d1 = szHdr
15587 31 3b 0a 20 20 6e 46 69 65 6c 64 20 3d 20 70 4b  1;.  nField = pK
15588 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 3b 0a  eyInfo->nField;.
15589 20 20 77 68 69 6c 65 28 20 69 64 78 31 3c 73 7a    while( idx1<sz
1558a 48 64 72 31 20 26 26 20 69 3c 70 50 4b 65 79 32  Hdr1 && i<pPKey2
1558b 2d 3e 6e 46 69 65 6c 64 20 29 7b 0a 20 20 20 20  ->nField ){.    
1558c 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 31  u32 serial_type1
1558d 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74  ;..    /* Read t
1558e 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 73 20  he serial types 
1558f 66 6f 72 20 74 68 65 20 6e 65 78 74 20 65 6c 65  for the next ele
15590 6d 65 6e 74 20 69 6e 20 65 61 63 68 20 6b 65 79  ment in each key
15591 2e 20 2a 2f 0a 20 20 20 20 69 64 78 31 20 2b 3d  . */.    idx1 +=
15592 20 67 65 74 56 61 72 69 6e 74 33 32 28 20 61 4b   getVarint32( aK
15593 65 79 31 2b 69 64 78 31 2c 20 73 65 72 69 61 6c  ey1+idx1, serial
15594 5f 74 79 70 65 31 20 29 3b 0a 20 20 20 20 69 66  _type1 );.    if
15595 28 20 64 31 3e 3d 6e 4b 65 79 31 20 26 26 20 73  ( d1>=nKey1 && s
15596 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
15597 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74  TypeLen(serial_t
15598 79 70 65 31 29 3e 30 20 29 20 62 72 65 61 6b 3b  ype1)>0 ) break;
15599 0a 0a 20 20 20 20 2f 2a 20 45 78 74 72 61 63 74  ..    /* Extract
1559a 20 74 68 65 20 76 61 6c 75 65 73 20 74 6f 20 62   the values to b
1559b 65 20 63 6f 6d 70 61 72 65 64 2e 0a 20 20 20 20  e compared..    
1559c 2a 2f 0a 20 20 20 20 64 31 20 2b 3d 20 73 71 6c  */.    d1 += sql
1559d 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65  ite3VdbeSerialGe
1559e 74 28 26 61 4b 65 79 31 5b 64 31 5d 2c 20 73 65  t(&aKey1[d1], se
1559f 72 69 61 6c 5f 74 79 70 65 31 2c 20 26 6d 65 6d  rial_type1, &mem
155a0 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74  1);..    /* Do t
155a1 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 0a 20 20  he comparison.  
155a2 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71    */.    rc = sq
155a3 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28  lite3MemCompare(
155a4 26 6d 65 6d 31 2c 20 26 70 50 4b 65 79 32 2d 3e  &mem1, &pPKey2->
155a5 61 4d 65 6d 5b 69 5d 2c 0a 20 20 20 20 20 20 20  aMem[i],.       
155a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
155a7 20 20 20 20 69 3c 6e 46 69 65 6c 64 20 3f 20 70      i<nField ? p
155a8 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69  KeyInfo->aColl[i
155a9 5d 20 3a 20 30 29 3b 0a 20 20 20 20 69 66 28 20  ] : 0);.    if( 
155aa 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 62  rc!=0 ){.      b
155ab 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
155ac 69 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6d  i++;.  }.  if( m
155ad 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 20 29 20 73 71  em1.zMalloc ) sq
155ae 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
155af 61 73 65 28 26 6d 65 6d 31 29 3b 0a 0a 20 20 2f  ase(&mem1);..  /
155b0 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 6b 65 79  * One of the key
155b1 73 20 72 61 6e 20 6f 75 74 20 6f 66 20 66 69 65  s ran out of fie
155b2 6c 64 73 2c 20 62 75 74 20 61 6c 6c 20 74 68 65  lds, but all the
155b3 20 66 69 65 6c 64 73 20 75 70 20 74 6f 20 74 68   fields up to th
155b4 61 74 20 70 6f 69 6e 74 0a 20 20 2a 2a 20 77 65  at point.  ** we
155b5 72 65 20 65 71 75 61 6c 2e 20 49 66 20 74 68 65  re equal. If the
155b6 20 69 6e 63 72 4b 65 79 20 66 6c 61 67 20 69 73   incrKey flag is
155b7 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20   true, then the 
155b8 73 65 63 6f 6e 64 20 6b 65 79 20 69 73 0a 20 20  second key is.  
155b9 2a 2a 20 74 72 65 61 74 65 64 20 61 73 20 6c 61  ** treated as la
155ba 72 67 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  rger..  */.  if(
155bb 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66   rc==0 ){.    if
155bc 28 20 70 4b 65 79 49 6e 66 6f 2d 3e 69 6e 63 72  ( pKeyInfo->incr
155bd 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20  Key ){.      rc 
155be 3d 20 2d 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20  = -1;.    }else 
155bf 69 66 28 20 21 70 4b 65 79 49 6e 66 6f 2d 3e 70  if( !pKeyInfo->p
155c0 72 65 66 69 78 49 73 45 71 75 61 6c 20 29 7b 0a  refixIsEqual ){.
155c1 20 20 20 20 20 20 69 66 28 20 64 31 3c 6e 4b 65        if( d1<nKe
155c2 79 31 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  y1 ){.        rc
155c3 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
155c4 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
155c5 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f  pKeyInfo->aSortO
155c6 72 64 65 72 20 26 26 20 69 3c 70 4b 65 79 49 6e  rder && i<pKeyIn
155c7 66 6f 2d 3e 6e 46 69 65 6c 64 0a 20 20 20 20 20  fo->nField.     
155c8 20 20 20 20 20 20 20 20 20 20 26 26 20 70 4b 65            && pKe
155c9 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65  yInfo->aSortOrde
155ca 72 5b 69 5d 20 29 7b 0a 20 20 20 20 72 63 20 3d  r[i] ){.    rc =
155cb 20 2d 72 63 3b 0a 20 20 7d 0a 0a 20 20 72 65 74   -rc;.  }..  ret
155cc 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
155cd 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73   The argument is
155ce 20 61 6e 20 69 6e 64 65 78 20 65 6e 74 72 79 20   an index entry 
155cf 63 6f 6d 70 6f 73 65 64 20 75 73 69 6e 67 20 74  composed using t
155d0 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  he OP_MakeRecord
155d1 20 6f 70 63 6f 64 65 2e 0a 2a 2a 20 54 68 65 20   opcode..** The 
155d2 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68  last entry in th
155d3 69 73 20 72 65 63 6f 72 64 20 73 68 6f 75 6c 64  is record should
155d4 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 20 28   be an integer (
155d5 73 70 65 63 69 66 69 63 61 6c 6c 79 0a 2a 2a 20  specifically.** 
155d6 61 6e 20 69 6e 74 65 67 65 72 20 72 6f 77 69 64  an integer rowid
155d7 29 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  ).  This routine
155d8 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
155d9 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 0a  ber of bytes in.
155da 2a 2a 20 74 68 61 74 20 69 6e 74 65 67 65 72 2e  ** that integer.
155db 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
155dc 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
155dd 62 65 49 64 78 52 6f 77 69 64 4c 65 6e 28 63 6f  beIdxRowidLen(co
155de 6e 73 74 20 75 38 20 2a 61 4b 65 79 29 7b 0a 20  nst u8 *aKey){. 
155df 20 75 33 32 20 73 7a 48 64 72 3b 20 20 20 20 20   u32 szHdr;     
155e0 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68     /* Size of th
155e1 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 33  e header */.  u3
155e2 32 20 74 79 70 65 52 6f 77 69 64 3b 20 20 20 20  2 typeRowid;    
155e3 2f 2a 20 53 65 72 69 61 6c 20 74 79 70 65 20 6f  /* Serial type o
155e4 66 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 0a  f the rowid */..
155e5 20 20 28 76 6f 69 64 29 67 65 74 56 61 72 69 6e    (void)getVarin
155e6 74 33 32 28 61 4b 65 79 2c 20 73 7a 48 64 72 29  t32(aKey, szHdr)
155e7 3b 0a 20 20 28 76 6f 69 64 29 67 65 74 56 61 72  ;.  (void)getVar
155e8 69 6e 74 33 32 28 26 61 4b 65 79 5b 73 7a 48 64  int32(&aKey[szHd
155e9 72 2d 31 5d 2c 20 74 79 70 65 52 6f 77 69 64 29  r-1], typeRowid)
155ea 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ;.  return sqlit
155eb 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
155ec 4c 65 6e 28 74 79 70 65 52 6f 77 69 64 29 3b 0a  Len(typeRowid);.
155ed 7d 0a 20 20 0a 0a 2f 2a 0a 2a 2a 20 70 43 75 72  }.  ../*.** pCur
155ee 20 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 69 6e   points at an in
155ef 64 65 78 20 65 6e 74 72 79 20 63 72 65 61 74 65  dex entry create
155f0 64 20 75 73 69 6e 67 20 74 68 65 20 4f 50 5f 4d  d using the OP_M
155f1 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65  akeRecord opcode
155f2 2e 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 72 6f  ..** Read the ro
155f3 77 69 64 20 28 74 68 65 20 6c 61 73 74 20 66 69  wid (the last fi
155f4 65 6c 64 20 69 6e 20 74 68 65 20 72 65 63 6f 72  eld in the recor
155f5 64 29 20 61 6e 64 20 73 74 6f 72 65 20 69 74 20  d) and store it 
155f6 69 6e 20 2a 72 6f 77 69 64 2e 0a 2a 2a 20 52 65  in *rowid..** Re
155f7 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69  turn SQLITE_OK i
155f8 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72  f everything wor
155f9 6b 73 2c 20 6f 72 20 61 6e 20 65 72 72 6f 72 20  ks, or an error 
155fa 63 6f 64 65 20 6f 74 68 65 72 77 69 73 65 2e 0a  code otherwise..
155fb 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
155fc 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
155fd 65 49 64 78 52 6f 77 69 64 28 42 74 43 75 72 73  eIdxRowid(BtCurs
155fe 6f 72 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 72  or *pCur, i64 *r
155ff 6f 77 69 64 29 7b 0a 20 20 69 36 34 20 6e 43 65  owid){.  i64 nCe
15600 6c 6c 4b 65 79 20 3d 20 30 3b 0a 20 20 69 6e 74  llKey = 0;.  int
15601 20 72 63 3b 0a 20 20 75 33 32 20 73 7a 48 64 72   rc;.  u32 szHdr
15602 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65  ;        /* Size
15603 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 20 2a   of the header *
15604 2f 0a 20 20 75 33 32 20 74 79 70 65 52 6f 77 69  /.  u32 typeRowi
15605 64 3b 20 20 20 20 2f 2a 20 53 65 72 69 61 6c 20  d;    /* Serial 
15606 74 79 70 65 20 6f 66 20 74 68 65 20 72 6f 77 69  type of the rowi
15607 64 20 2a 2f 0a 20 20 75 33 32 20 6c 65 6e 52 6f  d */.  u32 lenRo
15608 77 69 64 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65  wid;     /* Size
15609 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20 2a 2f   of the rowid */
1560a 0a 20 20 4d 65 6d 20 6d 2c 20 76 3b 0a 0a 20 20  .  Mem m, v;..  
1560b 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53  sqlite3BtreeKeyS
1560c 69 7a 65 28 70 43 75 72 2c 20 26 6e 43 65 6c 6c  ize(pCur, &nCell
1560d 4b 65 79 29 3b 0a 20 20 69 66 28 20 6e 43 65 6c  Key);.  if( nCel
1560e 6c 4b 65 79 3c 3d 30 20 29 7b 0a 20 20 20 20 72  lKey<=0 ){.    r
1560f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
15610 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20  RUPT_BKPT;.  }. 
15611 20 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20   m.flags = 0;.  
15612 6d 2e 64 62 20 3d 20 30 3b 0a 20 20 6d 2e 7a 4d  m.db = 0;.  m.zM
15613 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 72 63 20  alloc = 0;.  rc 
15614 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  = sqlite3VdbeMem
15615 46 72 6f 6d 42 74 72 65 65 28 70 43 75 72 2c 20  FromBtree(pCur, 
15616 30 2c 20 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20  0, nCellKey, 1, 
15617 26 6d 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b  &m);.  if( rc ){
15618 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
15619 20 20 7d 0a 20 20 28 76 6f 69 64 29 67 65 74 56    }.  (void)getV
1561a 61 72 69 6e 74 33 32 28 28 75 38 2a 29 6d 2e 7a  arint32((u8*)m.z
1561b 2c 20 73 7a 48 64 72 29 3b 0a 20 20 28 76 6f 69  , szHdr);.  (voi
1561c 64 29 67 65 74 56 61 72 69 6e 74 33 32 28 28 75  d)getVarint32((u
1561d 38 2a 29 26 6d 2e 7a 5b 73 7a 48 64 72 2d 31 5d  8*)&m.z[szHdr-1]
1561e 2c 20 74 79 70 65 52 6f 77 69 64 29 3b 0a 20 20  , typeRowid);.  
1561f 6c 65 6e 52 6f 77 69 64 20 3d 20 73 71 6c 69 74  lenRowid = sqlit
15620 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
15621 4c 65 6e 28 74 79 70 65 52 6f 77 69 64 29 3b 0a  Len(typeRowid);.
15622 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72    sqlite3VdbeSer
15623 69 61 6c 47 65 74 28 28 75 38 2a 29 26 6d 2e 7a  ialGet((u8*)&m.z
15624 5b 6d 2e 6e 2d 6c 65 6e 52 6f 77 69 64 5d 2c 20  [m.n-lenRowid], 
15625 74 79 70 65 52 6f 77 69 64 2c 20 26 76 29 3b 0a  typeRowid, &v);.
15626 20 20 2a 72 6f 77 69 64 20 3d 20 76 2e 75 2e 69    *rowid = v.u.i
15627 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
15628 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20  emRelease(&m);. 
15629 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1562a 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  K;.}../*.** Comp
1562b 61 72 65 20 74 68 65 20 6b 65 79 20 6f 66 20 74  are the key of t
1562c 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20 74  he index entry t
1562d 68 61 74 20 63 75 72 73 6f 72 20 70 43 20 69 73  hat cursor pC is
1562e 20 70 6f 69 6e 74 20 74 6f 20 61 67 61 69 6e 73   point to agains
1562f 74 0a 2a 2a 20 74 68 65 20 6b 65 79 20 73 74 72  t.** the key str
15630 69 6e 67 20 69 6e 20 70 4b 65 79 20 28 6f 66 20  ing in pKey (of 
15631 6c 65 6e 67 74 68 20 6e 4b 65 79 29 2e 20 20 57  length nKey).  W
15632 72 69 74 65 20 69 6e 74 6f 20 2a 70 52 65 73 20  rite into *pRes 
15633 61 20 6e 75 6d 62 65 72 0a 2a 2a 20 74 68 61 74  a number.** that
15634 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 7a 65   is negative, ze
15635 72 6f 2c 20 6f 72 20 70 6f 73 69 74 69 76 65 20  ro, or positive 
15636 69 66 20 70 43 20 69 73 20 6c 65 73 73 20 74 68  if pC is less th
15637 61 6e 2c 20 65 71 75 61 6c 20 74 6f 2c 0a 2a 2a  an, equal to,.**
15638 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e   or greater than
15639 20 70 4b 65 79 2e 20 20 52 65 74 75 72 6e 20 53   pKey.  Return S
1563a 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63  QLITE_OK on succ
1563b 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 70 4b 65 79 20  ess..**.** pKey 
1563c 69 73 20 65 69 74 68 65 72 20 63 72 65 61 74 65  is either create
1563d 64 20 77 69 74 68 6f 75 74 20 61 20 72 6f 77 69  d without a rowi
1563e 64 20 6f 72 20 69 73 20 74 72 75 6e 63 61 74 65  d or is truncate
1563f 64 20 73 6f 20 74 68 61 74 20 69 74 0a 2a 2a 20  d so that it.** 
15640 6f 6d 69 74 73 20 74 68 65 20 72 6f 77 69 64 20  omits the rowid 
15641 61 74 20 74 68 65 20 65 6e 64 2e 20 20 54 68 65  at the end.  The
15642 20 72 6f 77 69 64 20 61 74 20 74 68 65 20 65 6e   rowid at the en
15643 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 65  d of the index e
15644 6e 74 72 79 0a 2a 2a 20 69 73 20 69 67 6e 6f 72  ntry.** is ignor
15645 65 64 20 61 73 20 77 65 6c 6c 2e 0a 2a 2f 0a 53  ed as well..*/.S
15646 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
15647 74 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78  t sqlite3VdbeIdx
15648 4b 65 79 43 6f 6d 70 61 72 65 28 0a 20 20 43 75  KeyCompare(.  Cu
15649 72 73 6f 72 20 2a 70 43 2c 20 20 20 20 20 20 20  rsor *pC,       
1564a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1564b 20 63 75 72 73 6f 72 20 74 6f 20 63 6f 6d 70 61   cursor to compa
1564c 72 65 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20  re against */.  
1564d 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a  UnpackedRecord *
1564e 70 55 6e 70 61 63 6b 65 64 2c 0a 20 20 69 6e 74  pUnpacked,.  int
1564f 20 6e 4b 65 79 2c 20 63 6f 6e 73 74 20 75 38 20   nKey, const u8 
15650 2a 70 4b 65 79 2c 20 20 20 2f 2a 20 54 68 65 20  *pKey,   /* The 
15651 6b 65 79 20 74 6f 20 63 6f 6d 70 61 72 65 20 2a  key to compare *
15652 2f 0a 20 20 69 6e 74 20 2a 72 65 73 20 20 20 20  /.  int *res    
15653 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15654 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6d  /* Write the com
15655 70 61 72 69 73 6f 6e 20 72 65 73 75 6c 74 20 68  parison result h
15656 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 36 34 20  ere */.){.  i64 
15657 6e 43 65 6c 6c 4b 65 79 20 3d 20 30 3b 0a 20 20  nCellKey = 0;.  
15658 69 6e 74 20 72 63 3b 0a 20 20 42 74 43 75 72 73  int rc;.  BtCurs
15659 6f 72 20 2a 70 43 75 72 20 3d 20 70 43 2d 3e 70  or *pCur = pC->p
1565a 43 75 72 73 6f 72 3b 0a 20 20 69 6e 74 20 6c 65  Cursor;.  int le
1565b 6e 52 6f 77 69 64 3b 0a 20 20 4d 65 6d 20 6d 3b  nRowid;.  Mem m;
1565c 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  .  UnpackedRecor
1565d 64 20 2a 70 52 65 63 3b 0a 20 20 63 68 61 72 20  d *pRec;.  char 
1565e 7a 53 70 61 63 65 5b 32 30 30 5d 3b 0a 0a 20 20  zSpace[200];..  
1565f 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53  sqlite3BtreeKeyS
15660 69 7a 65 28 70 43 75 72 2c 20 26 6e 43 65 6c 6c  ize(pCur, &nCell
15661 4b 65 79 29 3b 0a 20 20 69 66 28 20 6e 43 65 6c  Key);.  if( nCel
15662 6c 4b 65 79 3c 3d 30 20 29 7b 0a 20 20 20 20 2a  lKey<=0 ){.    *
15663 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 74  res = 0;.    ret
15664 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
15665 20 7d 0a 20 20 6d 2e 64 62 20 3d 20 30 3b 0a 20   }.  m.db = 0;. 
15666 20 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20   m.flags = 0;.  
15667 6d 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20  m.zMalloc = 0;. 
15668 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
15669 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43  eMemFromBtree(pC
1566a 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 6e 43  ->pCursor, 0, nC
1566b 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d 29 3b 0a  ellKey, 1, &m);.
1566c 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
1566d 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
1566e 20 6c 65 6e 52 6f 77 69 64 20 3d 20 73 71 6c 69   lenRowid = sqli
1566f 74 65 33 56 64 62 65 49 64 78 52 6f 77 69 64 4c  te3VdbeIdxRowidL
15670 65 6e 28 28 75 38 2a 29 6d 2e 7a 29 3b 0a 20 20  en((u8*)m.z);.  
15671 69 66 28 20 21 70 55 6e 70 61 63 6b 65 64 20 29  if( !pUnpacked )
15672 7b 0a 20 20 20 20 70 52 65 63 20 3d 20 73 71 6c  {.    pRec = sql
15673 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e  ite3VdbeRecordUn
15674 70 61 63 6b 28 70 43 2d 3e 70 4b 65 79 49 6e 66  pack(pC->pKeyInf
15675 6f 2c 20 6e 4b 65 79 2c 20 70 4b 65 79 2c 0a 20  o, nKey, pKey,. 
15676 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15677 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a                 z
15678 53 70 61 63 65 2c 20 73 69 7a 65 6f 66 28 7a 53  Space, sizeof(zS
15679 70 61 63 65 29 29 3b 0a 20 20 7d 65 6c 73 65 7b  pace));.  }else{
1567a 0a 20 20 20 20 70 52 65 63 20 3d 20 70 55 6e 70  .    pRec = pUnp
1567b 61 63 6b 65 64 3b 0a 20 20 7d 0a 20 20 69 66 28  acked;.  }.  if(
1567c 20 70 52 65 63 3d 3d 30 20 29 7b 0a 20 20 20 20   pRec==0 ){.    
1567d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
1567e 4d 45 4d 3b 0a 20 20 7d 0a 20 20 2a 72 65 73 20  MEM;.  }.  *res 
1567f 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63  = sqlite3VdbeRec
15680 6f 72 64 43 6f 6d 70 61 72 65 28 6d 2e 6e 2d 6c  ordCompare(m.n-l
15681 65 6e 52 6f 77 69 64 2c 20 6d 2e 7a 2c 20 70 52  enRowid, m.z, pR
15682 65 63 29 3b 0a 20 20 69 66 28 20 21 70 55 6e 70  ec);.  if( !pUnp
15683 61 63 6b 65 64 20 29 7b 0a 20 20 20 20 73 71 6c  acked ){.    sql
15684 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 6e  ite3VdbeDeleteUn
15685 70 61 63 6b 65 64 52 65 63 6f 72 64 28 70 52 65  packedRecord(pRe
15686 63 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  c);.  }.  sqlite
15687 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
15688 26 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  &m);.  return SQ
15689 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
1568a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  * This routine s
1568b 65 74 73 20 74 68 65 20 76 61 6c 75 65 20 74 6f  ets the value to
1568c 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 20   be returned by 
1568d 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73  subsequent calls
1568e 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63   to.** sqlite3_c
1568f 68 61 6e 67 65 73 28 29 20 6f 6e 20 74 68 65 20  hanges() on the 
15690 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
15691 27 64 62 27 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45  'db'. .*/.SQLITE
15692 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
15693 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e  lite3VdbeSetChan
15694 67 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ges(sqlite3 *db,
15695 20 69 6e 74 20 6e 43 68 61 6e 67 65 29 7b 0a 20   int nChange){. 
15696 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
15697 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
15698 6d 75 74 65 78 29 20 29 3b 0a 20 20 64 62 2d 3e  mutex) );.  db->
15699 6e 43 68 61 6e 67 65 20 3d 20 6e 43 68 61 6e 67  nChange = nChang
1569a 65 3b 0a 20 20 64 62 2d 3e 6e 54 6f 74 61 6c 43  e;.  db->nTotalC
1569b 68 61 6e 67 65 20 2b 3d 20 6e 43 68 61 6e 67 65  hange += nChange
1569c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 61  ;.}../*.** Set a
1569d 20 66 6c 61 67 20 69 6e 20 74 68 65 20 76 64 62   flag in the vdb
1569e 65 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 20  e to update the 
1569f 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 77  change counter w
156a0 68 65 6e 20 69 74 20 69 73 20 66 69 6e 61 6c 69  hen it is finali
156a1 73 65 64 0a 2a 2a 20 6f 72 20 72 65 73 65 74 2e  sed.** or reset.
156a2 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
156a3 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
156a4 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 28  dbeCountChanges(
156a5 56 64 62 65 20 2a 76 29 7b 0a 20 20 76 2d 3e 63  Vdbe *v){.  v->c
156a6 68 61 6e 67 65 43 6e 74 4f 6e 20 3d 20 31 3b 0a  hangeCntOn = 1;.
156a7 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 65 76  }../*.** Mark ev
156a8 65 72 79 20 70 72 65 70 61 72 65 64 20 73 74 61  ery prepared sta
156a9 74 65 6d 65 6e 74 20 61 73 73 6f 63 69 61 74 65  tement associate
156aa 64 20 77 69 74 68 20 61 20 64 61 74 61 62 61 73  d with a databas
156ab 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20  e connection.** 
156ac 61 73 20 65 78 70 69 72 65 64 2e 0a 2a 2a 0a 2a  as expired..**.*
156ad 2a 20 41 6e 20 65 78 70 69 72 65 64 20 73 74 61  * An expired sta
156ae 74 65 6d 65 6e 74 20 6d 65 61 6e 73 20 74 68 61  tement means tha
156af 74 20 72 65 63 6f 6d 70 69 6c 61 74 69 6f 6e 20  t recompilation 
156b0 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  of the statement
156b1 20 69 73 0a 2a 2a 20 72 65 63 6f 6d 6d 65 6e 64   is.** recommend
156b2 2e 20 20 53 74 61 74 65 6d 65 6e 74 73 20 65 78  .  Statements ex
156b3 70 69 72 65 20 77 68 65 6e 20 74 68 69 6e 67 73  pire when things
156b4 20 68 61 70 70 65 6e 20 74 68 61 74 20 6d 61 6b   happen that mak
156b5 65 20 74 68 65 69 72 0a 2a 2a 20 70 72 6f 67 72  e their.** progr
156b6 61 6d 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 52  ams obsolete.  R
156b7 65 6d 6f 76 69 6e 67 20 75 73 65 72 2d 64 65 66  emoving user-def
156b8 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 6f  ined functions o
156b9 72 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73  r collating.** s
156ba 65 71 75 65 6e 63 65 73 2c 20 6f 72 20 63 68 61  equences, or cha
156bb 6e 67 69 6e 67 20 61 6e 20 61 75 74 68 6f 72 69  nging an authori
156bc 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  zation function 
156bd 61 72 65 20 74 68 65 20 74 79 70 65 73 20 6f 66  are the types of
156be 0a 2a 2a 20 74 68 69 6e 67 73 20 74 68 61 74 20  .** things that 
156bf 6d 61 6b 65 20 70 72 65 70 61 72 65 64 20 73 74  make prepared st
156c0 61 74 65 6d 65 6e 74 73 20 6f 62 73 6f 6c 65 74  atements obsolet
156c1 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
156c2 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
156c3 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53  3ExpirePreparedS
156c4 74 61 74 65 6d 65 6e 74 73 28 73 71 6c 69 74 65  tatements(sqlite
156c5 33 20 2a 64 62 29 7b 0a 20 20 56 64 62 65 20 2a  3 *db){.  Vdbe *
156c6 70 3b 0a 20 20 66 6f 72 28 70 20 3d 20 64 62 2d  p;.  for(p = db-
156c7 3e 70 56 64 62 65 3b 20 70 3b 20 70 3d 70 2d 3e  >pVdbe; p; p=p->
156c8 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 65  pNext){.    p->e
156c9 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a  xpired = 1;.  }.
156ca 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
156cb 74 68 65 20 64 61 74 61 62 61 73 65 20 61 73 73  the database ass
156cc 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
156cd 20 56 64 62 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   Vdbe..*/.SQLITE
156ce 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33  _PRIVATE sqlite3
156cf 20 2a 73 71 6c 69 74 65 33 56 64 62 65 44 62 28   *sqlite3VdbeDb(
156d0 56 64 62 65 20 2a 76 29 7b 0a 20 20 72 65 74 75  Vdbe *v){.  retu
156d1 72 6e 20 76 2d 3e 64 62 3b 0a 7d 0a 0a 2f 2a 2a  rn v->db;.}../**
156d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
156d3 20 6f 66 20 76 64 62 65 61 75 78 2e 63 20 2a 2a   of vdbeaux.c **
156d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
156d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
156d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
156d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
156d8 69 6e 20 66 69 6c 65 20 76 64 62 65 61 70 69 2e  in file vdbeapi.
156d9 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
156da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
156db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
156dc 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 36 0a 2a  ** 2004 May 26.*
156dd 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
156de 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
156df 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
156e0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
156e1 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
156e2 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
156e3 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
156e4 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
156e5 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
156e6 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
156e7 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
156e8 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
156e9 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
156ea 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
156eb 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
156ec 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
156ed 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
156ee 0a 2a 2a 0a 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 2a 2a 2a 2a  ****************
156f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
156f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
156f3 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
156f4 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 20  ntains code use 
156f5 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 41 50 49  to implement API
156f6 73 20 74 68 61 74 20 61 72 65 20 70 61 72 74 20  s that are part 
156f7 6f 66 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e 0a  of the.** VDBE..
156f8 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
156f9 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f  E_ENABLE_MEMORY_
156fa 4d 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a  MANAGEMENT./*.**
156fb 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   The following s
156fc 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
156fd 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68  s pointers to th
156fe 65 20 65 6e 64 20 70 6f 69 6e 74 73 20 6f 66 20  e end points of 
156ff 61 0a 2a 2a 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b  a.** doubly-link
15700 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63  ed list of all c
15701 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74  ompiled SQL stat
15702 65 6d 65 6e 74 73 20 74 68 61 74 20 6d 61 79 20  ements that may 
15703 62 65 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 62 75  be holding.** bu
15704 66 66 65 72 73 20 65 6c 69 67 69 62 6c 65 20 66  ffers eligible f
15705 6f 72 20 72 65 6c 65 61 73 65 20 77 68 65 6e 20  or release when 
15706 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65  the sqlite3_rele
15707 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 69 6e 74  ase_memory() int
15708 65 72 66 61 63 65 20 69 73 0a 2a 2a 20 69 6e 76  erface is.** inv
15709 6f 6b 65 64 2e 20 41 63 63 65 73 73 20 74 6f 20  oked. Access to 
1570a 74 68 69 73 20 6c 69 73 74 20 69 73 20 70 72 6f  this list is pro
1570b 74 65 63 74 65 64 20 62 79 20 74 68 65 20 53 51  tected by the SQ
1570c 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
1570d 43 5f 4c 52 55 32 0a 2a 2a 20 6d 75 74 65 78 2e  C_LRU2.** mutex.
1570e 0a 2a 2a 0a 2a 2a 20 53 74 61 74 65 6d 65 6e 74  .**.** Statement
1570f 73 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 74  s are added to t
15710 68 65 20 65 6e 64 20 6f 66 20 74 68 69 73 20 6c  he end of this l
15711 69 73 74 20 77 68 65 6e 20 73 71 6c 69 74 65 33  ist when sqlite3
15712 5f 72 65 73 65 74 28 29 20 69 73 0a 2a 2a 20 63  _reset() is.** c
15713 61 6c 6c 65 64 2e 20 54 68 65 79 20 61 72 65 20  alled. They are 
15714 72 65 6d 6f 76 65 64 20 65 69 74 68 65 72 20 77  removed either w
15715 68 65 6e 20 73 71 6c 69 74 65 33 5f 73 74 65 70  hen sqlite3_step
15716 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 66 69  () or sqlite3_fi
15717 6e 61 6c 69 7a 65 28 29 0a 2a 2a 20 69 73 20 63  nalize().** is c
15718 61 6c 6c 65 64 2e 20 57 68 65 6e 20 73 74 61 74  alled. When stat
15719 65 6d 65 6e 74 73 20 61 72 65 20 61 64 64 65 64  ements are added
1571a 20 74 6f 20 74 68 69 73 20 6c 69 73 74 2c 20 74   to this list, t
1571b 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 0a 2a  he associated .*
1571c 2a 20 72 65 67 69 73 74 65 72 20 61 72 72 61 79  * register array
1571d 20 28 70 2d 3e 61 4d 65 6d 5b 31 2e 2e 70 2d 3e   (p->aMem[1..p->
1571e 6e 4d 65 6d 5d 29 20 6d 61 79 20 63 6f 6e 74 61  nMem]) may conta
1571f 69 6e 20 64 79 6e 61 6d 69 63 20 62 75 66 66 65  in dynamic buffe
15720 72 73 20 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62  rs that.** can b
15721 65 20 66 72 65 65 64 20 75 73 69 6e 67 20 73 71  e freed using sq
15722 6c 69 74 65 33 56 64 62 65 52 65 6c 65 61 73 65  lite3VdbeRelease
15723 4d 65 6d 6f 72 79 28 29 2e 0a 2a 2a 0a 2a 2a 20  Memory()..**.** 
15724 57 68 65 6e 20 73 74 61 74 65 6d 65 6e 74 73 20  When statements 
15725 61 72 65 20 61 64 64 65 64 20 6f 72 20 72 65 6d  are added or rem
15726 6f 76 65 64 20 66 72 6f 6d 20 74 68 69 73 20 6c  oved from this l
15727 69 73 74 2c 20 74 68 65 20 6d 75 74 65 78 0a 2a  ist, the mutex.*
15728 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  * associated wit
15729 68 20 74 68 65 20 56 64 62 65 20 62 65 69 6e 67  h the Vdbe being
1572a 20 61 64 64 65 64 20 6f 72 20 72 65 6d 6f 76 65   added or remove
1572b 64 20 28 56 64 62 65 2e 64 62 2d 3e 6d 75 74 65  d (Vdbe.db->mute
1572c 78 29 20 69 73 0a 2a 2a 20 61 6c 72 65 61 64 79  x) is.** already
1572d 20 68 65 6c 64 2e 20 54 68 65 20 4c 52 55 32 20   held. The LRU2 
1572e 6d 75 74 65 78 20 69 73 20 74 68 65 6e 20 6f 62  mutex is then ob
1572f 74 61 69 6e 65 64 2c 20 62 6c 6f 63 6b 69 6e 67  tained, blocking
15730 20 69 66 20 6e 65 63 65 73 73 61 72 79 2c 0a 2a   if necessary,.*
15731 2a 20 74 68 65 20 6c 69 6e 6b 65 64 2d 6c 69 73  * the linked-lis
15732 74 20 70 6f 69 6e 74 65 72 73 20 6d 61 6e 69 70  t pointers manip
15733 75 6c 61 74 65 64 20 61 6e 64 20 74 68 65 20 4c  ulated and the L
15734 52 55 32 20 6d 75 74 65 78 20 72 65 6c 69 6e 71  RU2 mutex relinq
15735 75 69 73 68 65 64 2e 0a 2a 2f 0a 73 74 72 75 63  uished..*/.struc
15736 74 20 53 74 61 74 65 6d 65 6e 74 4c 72 75 4c 69  t StatementLruLi
15737 73 74 20 7b 0a 20 20 56 64 62 65 20 2a 70 46 69  st {.  Vdbe *pFi
15738 72 73 74 3b 0a 20 20 56 64 62 65 20 2a 70 4c 61  rst;.  Vdbe *pLa
15739 73 74 3b 0a 7d 3b 0a 73 74 61 74 69 63 20 73 74  st;.};.static st
1573a 72 75 63 74 20 53 74 61 74 65 6d 65 6e 74 4c 72  ruct StatementLr
1573b 75 4c 69 73 74 20 73 71 6c 69 74 65 33 4c 72 75  uList sqlite3Lru
1573c 53 74 61 74 65 6d 65 6e 74 73 3b 0a 0a 2f 2a 0a  Statements;../*.
1573d 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68  ** Check that th
1573e 65 20 6c 69 73 74 20 6c 6f 6f 6b 73 20 74 6f 20  e list looks to 
1573f 62 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 63 6f  be internally co
15740 6e 73 69 73 74 65 6e 74 2e 20 54 68 69 73 20 69  nsistent. This i
15741 73 20 75 73 65 64 0a 2a 2a 20 61 73 20 70 61 72  s used.** as par
15742 74 20 6f 66 20 61 6e 20 61 73 73 65 72 74 28 29  t of an assert()
15743 20 73 74 61 74 65 6d 65 6e 74 20 61 73 20 66 6f   statement as fo
15744 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 61  llows:.**.**   a
15745 73 73 65 72 74 28 20 73 74 6d 74 4c 72 75 43 68  ssert( stmtLruCh
15746 65 63 6b 28 29 20 29 3b 0a 2a 2f 0a 23 69 66 6e  eck() );.*/.#ifn
15747 64 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74 69  def NDEBUG.stati
15748 63 20 69 6e 74 20 73 74 6d 74 4c 72 75 43 68 65  c int stmtLruChe
15749 63 6b 28 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b  ck(){.  Vdbe *p;
1574a 0a 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 33  .  for(p=sqlite3
1574b 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46  LruStatements.pF
1574c 69 72 73 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4c  irst; p; p=p->pL
1574d 72 75 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73  ruNext){.    ass
1574e 65 72 74 28 70 2d 3e 70 4c 72 75 4e 65 78 74 20  ert(p->pLruNext 
1574f 7c 7c 20 70 3d 3d 73 71 6c 69 74 65 33 4c 72 75  || p==sqlite3Lru
15750 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 73 74  Statements.pLast
15751 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 21 70  );.    assert(!p
15752 2d 3e 70 4c 72 75 4e 65 78 74 20 7c 7c 20 70 2d  ->pLruNext || p-
15753 3e 70 4c 72 75 4e 65 78 74 2d 3e 70 4c 72 75 50  >pLruNext->pLruP
15754 72 65 76 3d 3d 70 29 3b 0a 20 20 20 20 61 73 73  rev==p);.    ass
15755 65 72 74 28 70 2d 3e 70 4c 72 75 50 72 65 76 20  ert(p->pLruPrev 
15756 7c 7c 20 70 3d 3d 73 71 6c 69 74 65 33 4c 72 75  || p==sqlite3Lru
15757 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 72 73  Statements.pFirs
15758 74 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 21  t);.    assert(!
15759 70 2d 3e 70 4c 72 75 50 72 65 76 20 7c 7c 20 70  p->pLruPrev || p
1575a 2d 3e 70 4c 72 75 50 72 65 76 2d 3e 70 4c 72 75  ->pLruPrev->pLru
1575b 4e 65 78 74 3d 3d 70 29 3b 0a 20 20 7d 0a 20 20  Next==p);.  }.  
1575c 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64  return 1;.}.#end
1575d 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 76 64  if../*.** Add vd
1575e 62 65 20 70 20 74 6f 20 74 68 65 20 65 6e 64 20  be p to the end 
1575f 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  of the statement
15760 20 6c 72 75 20 6c 69 73 74 2e 20 49 74 20 69 73   lru list. It is
15761 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a 2a 2a   assumed that.**
15762 20 70 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64   p is not alread
15763 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 6c 69  y part of the li
15764 73 74 20 77 68 65 6e 20 74 68 69 73 20 69 73 20  st when this is 
15765 63 61 6c 6c 65 64 2e 20 54 68 65 20 6c 72 75 20  called. The lru 
15766 6c 69 73 74 0a 2a 2a 20 69 73 20 70 72 6f 74 65  list.** is prote
15767 63 74 65 64 20 62 79 20 74 68 65 20 53 51 4c 49  cted by the SQLI
15768 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
15769 4c 52 55 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74  LRU mutex..*/.st
1576a 61 74 69 63 20 76 6f 69 64 20 73 74 6d 74 4c 72  atic void stmtLr
1576b 75 41 64 64 28 56 64 62 65 20 2a 70 29 7b 0a 20  uAdd(Vdbe *p){. 
1576c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
1576d 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74  nter(sqlite3_mut
1576e 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  ex_alloc(SQLITE_
1576f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55  MUTEX_STATIC_LRU
15770 32 29 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 70  2));..  if( p->p
15771 4c 72 75 50 72 65 76 20 7c 7c 20 70 2d 3e 70 4c  LruPrev || p->pL
15772 72 75 4e 65 78 74 20 7c 7c 20 73 71 6c 69 74 65  ruNext || sqlite
15773 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70  3LruStatements.p
15774 46 69 72 73 74 3d 3d 70 20 29 7b 0a 20 20 20 20  First==p ){.    
15775 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
15776 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  ave(sqlite3_mute
15777 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  x_alloc(SQLITE_M
15778 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32  UTEX_STATIC_LRU2
15779 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  ));.    return;.
1577a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 73    }..  assert( s
1577b 74 6d 74 4c 72 75 43 68 65 63 6b 28 29 20 29 3b  tmtLruCheck() );
1577c 0a 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33  ..  if( !sqlite3
1577d 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46  LruStatements.pF
1577e 69 72 73 74 20 29 7b 0a 20 20 20 20 61 73 73 65  irst ){.    asse
1577f 72 74 28 20 21 73 71 6c 69 74 65 33 4c 72 75 53  rt( !sqlite3LruS
15780 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 73 74 20  tatements.pLast 
15781 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4c 72  );.    sqlite3Lr
15782 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 72  uStatements.pFir
15783 73 74 20 3d 20 70 3b 0a 20 20 20 20 73 71 6c 69  st = p;.    sqli
15784 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73  te3LruStatements
15785 2e 70 4c 61 73 74 20 3d 20 70 3b 0a 20 20 7d 65  .pLast = p;.  }e
15786 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
15787 20 21 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74   !sqlite3LruStat
15788 65 6d 65 6e 74 73 2e 70 4c 61 73 74 2d 3e 70 4c  ements.pLast->pL
15789 72 75 4e 65 78 74 20 29 3b 0a 20 20 20 20 70 2d  ruNext );.    p-
1578a 3e 70 4c 72 75 50 72 65 76 20 3d 20 73 71 6c 69  >pLruPrev = sqli
1578b 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73  te3LruStatements
1578c 2e 70 4c 61 73 74 3b 0a 20 20 20 20 73 71 6c 69  .pLast;.    sqli
1578d 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73  te3LruStatements
1578e 2e 70 4c 61 73 74 2d 3e 70 4c 72 75 4e 65 78 74  .pLast->pLruNext
1578f 20 3d 20 70 3b 0a 20 20 20 20 73 71 6c 69 74 65   = p;.    sqlite
15790 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70  3LruStatements.p
15791 4c 61 73 74 20 3d 20 70 3b 0a 20 20 7d 0a 0a 20  Last = p;.  }.. 
15792 20 61 73 73 65 72 74 28 20 73 74 6d 74 4c 72 75   assert( stmtLru
15793 43 68 65 63 6b 28 29 20 29 3b 0a 0a 20 20 73 71  Check() );..  sq
15794 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
15795 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e(sqlite3_mutex_
15796 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  alloc(SQLITE_MUT
15797 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 29  EX_STATIC_LRU2))
15798 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d  ;.}../*.** Assum
15799 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45 5f 4d  ing the SQLITE_M
1579a 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32  UTEX_STATIC_LRU2
1579b 20 6d 75 74 65 78 74 20 69 73 20 61 6c 72 65 61   mutext is alrea
1579c 64 79 20 68 65 6c 64 2c 20 72 65 6d 6f 76 65 0a  dy held, remove.
1579d 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 70 20 66  ** statement p f
1579e 72 6f 6d 20 74 68 65 20 6c 65 61 73 74 2d 72 65  rom the least-re
1579f 63 65 6e 74 6c 79 2d 75 73 65 64 20 73 74 61 74  cently-used stat
157a0 65 6d 65 6e 74 20 6c 69 73 74 2e 20 49 66 20 74  ement list. If t
157a1 68 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  he .** statement
157a2 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
157a3 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 6c 69  y part of the li
157a4 73 74 2c 20 74 68 69 73 20 63 61 6c 6c 20 69 73  st, this call is
157a5 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61   a no-op..*/.sta
157a6 74 69 63 20 76 6f 69 64 20 73 74 6d 74 4c 72 75  tic void stmtLru
157a7 52 65 6d 6f 76 65 4e 6f 6d 75 74 65 78 28 56 64  RemoveNomutex(Vd
157a8 62 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d  be *p){.  if( p-
157a9 3e 70 4c 72 75 50 72 65 76 20 7c 7c 20 70 2d 3e  >pLruPrev || p->
157aa 70 4c 72 75 4e 65 78 74 20 7c 7c 20 70 3d 3d 73  pLruNext || p==s
157ab 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65  qlite3LruStateme
157ac 6e 74 73 2e 70 46 69 72 73 74 20 29 7b 0a 20 20  nts.pFirst ){.  
157ad 20 20 61 73 73 65 72 74 28 20 73 74 6d 74 4c 72    assert( stmtLr
157ae 75 43 68 65 63 6b 28 29 20 29 3b 0a 20 20 20 20  uCheck() );.    
157af 69 66 28 20 70 2d 3e 70 4c 72 75 4e 65 78 74 20  if( p->pLruNext 
157b0 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 72 75  ){.      p->pLru
157b1 4e 65 78 74 2d 3e 70 4c 72 75 50 72 65 76 20 3d  Next->pLruPrev =
157b2 20 70 2d 3e 70 4c 72 75 50 72 65 76 3b 0a 20 20   p->pLruPrev;.  
157b3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
157b4 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65  qlite3LruStateme
157b5 6e 74 73 2e 70 4c 61 73 74 20 3d 20 70 2d 3e 70  nts.pLast = p->p
157b6 4c 72 75 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20  LruPrev;.    }. 
157b7 20 20 20 69 66 28 20 70 2d 3e 70 4c 72 75 50 72     if( p->pLruPr
157b8 65 76 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70  ev ){.      p->p
157b9 4c 72 75 50 72 65 76 2d 3e 70 4c 72 75 4e 65 78  LruPrev->pLruNex
157ba 74 20 3d 20 70 2d 3e 70 4c 72 75 4e 65 78 74 3b  t = p->pLruNext;
157bb 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
157bc 20 20 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74    sqlite3LruStat
157bd 65 6d 65 6e 74 73 2e 70 46 69 72 73 74 20 3d 20  ements.pFirst = 
157be 70 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20  p->pLruNext;.   
157bf 20 7d 0a 20 20 20 20 70 2d 3e 70 4c 72 75 4e 65   }.    p->pLruNe
157c0 78 74 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70  xt = 0;.    p->p
157c1 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20 20  LruPrev = 0;.   
157c2 20 61 73 73 65 72 74 28 20 73 74 6d 74 4c 72 75   assert( stmtLru
157c3 43 68 65 63 6b 28 29 20 29 3b 0a 20 20 7d 0a 7d  Check() );.  }.}
157c4 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67  ../*.** Assuming
157c5 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45   the SQLITE_MUTE
157c6 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 20 6d 75  X_STATIC_LRU2 mu
157c7 74 65 78 74 20 69 73 20 6e 6f 74 20 68 65 6c 64  text is not held
157c8 2c 20 72 65 6d 6f 76 65 0a 2a 2a 20 73 74 61 74  , remove.** stat
157c9 65 6d 65 6e 74 20 70 20 66 72 6f 6d 20 74 68 65  ement p from the
157ca 20 6c 65 61 73 74 2d 72 65 63 65 6e 74 6c 79 2d   least-recently-
157cb 75 73 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6c  used statement l
157cc 69 73 74 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20  ist. If the .** 
157cd 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6e 6f 74  statement is not
157ce 20 63 75 72 72 65 6e 74 6c 79 20 70 61 72 74 20   currently part 
157cf 6f 66 20 74 68 65 20 6c 69 73 74 2c 20 74 68 69  of the list, thi
157d0 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f  s call is a no-o
157d1 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  p..*/.static voi
157d2 64 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 28  d stmtLruRemove(
157d3 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69  Vdbe *p){.  sqli
157d4 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
157d5 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
157d6 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
157d7 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a  _STATIC_LRU2));.
157d8 20 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 4e    stmtLruRemoveN
157d9 6f 6d 75 74 65 78 28 70 29 3b 0a 20 20 73 71 6c  omutex(p);.  sql
157da 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
157db 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61  (sqlite3_mutex_a
157dc 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
157dd 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 29 3b  X_STATIC_LRU2));
157de 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f  .}../*.** Try to
157df 20 72 65 6c 65 61 73 65 20 6e 20 62 79 74 65 73   release n bytes
157e0 20 6f 66 20 6d 65 6d 6f 72 79 20 62 79 20 66 72   of memory by fr
157e1 65 65 69 6e 67 20 62 75 66 66 65 72 73 20 61 73  eeing buffers as
157e2 73 6f 63 69 61 74 65 64 20 0a 2a 2a 20 77 69 74  sociated .** wit
157e3 68 20 74 68 65 20 6d 65 6d 6f 72 79 20 72 65 67  h the memory reg
157e4 69 73 74 65 72 73 20 6f 66 20 63 75 72 72 65 6e  isters of curren
157e5 74 6c 79 20 75 6e 75 73 65 64 20 76 64 62 65 73  tly unused vdbes
157e6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
157e7 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
157e8 64 62 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79  dbeReleaseMemory
157e9 28 69 6e 74 20 6e 29 7b 0a 20 20 56 64 62 65 20  (int n){.  Vdbe 
157ea 2a 70 3b 0a 20 20 56 64 62 65 20 2a 70 4e 65 78  *p;.  Vdbe *pNex
157eb 74 3b 0a 20 20 69 6e 74 20 6e 46 72 65 65 20 3d  t;.  int nFree =
157ec 20 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d   0;..  sqlite3_m
157ed 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74  utex_enter(sqlit
157ee 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53  e3_mutex_alloc(S
157ef 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
157f0 49 43 5f 4c 52 55 32 29 29 3b 0a 20 20 66 6f 72  IC_LRU2));.  for
157f1 28 70 3d 73 71 6c 69 74 65 33 4c 72 75 53 74 61  (p=sqlite3LruSta
157f2 74 65 6d 65 6e 74 73 2e 70 46 69 72 73 74 3b 20  tements.pFirst; 
157f3 70 20 26 26 20 6e 46 72 65 65 3c 6e 3b 20 70 3d  p && nFree<n; p=
157f4 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 4e 65 78  pNext){.    pNex
157f5 74 20 3d 20 70 2d 3e 70 4c 72 75 4e 65 78 74 3b  t = p->pLruNext;
157f6 0a 0a 20 20 20 20 2f 2a 20 46 6f 72 20 65 61 63  ..    /* For eac
157f7 68 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64  h statement hand
157f8 6c 65 20 69 6e 20 74 68 65 20 6c 72 75 20 6c 69  le in the lru li
157f9 73 74 2c 20 61 74 74 65 6d 70 74 20 74 6f 20 6f  st, attempt to o
157fa 62 74 61 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a  btain the.    **
157fb 20 61 73 73 6f 63 69 61 74 65 64 20 64 61 74 61   associated data
157fc 62 61 73 65 20 6d 75 74 65 78 2e 20 49 66 20 69  base mutex. If i
157fd 74 20 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61  t cannot be obta
157fe 69 6e 65 64 2c 20 63 6f 6e 74 69 6e 75 65 0a 20  ined, continue. 
157ff 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 6e 65 78     ** to the nex
15800 74 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64  t statement hand
15801 6c 65 2e 20 49 74 20 69 73 20 6e 6f 74 20 70 6f  le. It is not po
15802 73 73 69 62 6c 65 20 74 6f 20 62 6c 6f 63 6b 20  ssible to block 
15803 6f 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61  on.    ** the da
15804 74 61 62 61 73 65 20 6d 75 74 65 78 20 2d 20 74  tabase mutex - t
15805 68 61 74 20 63 6f 75 6c 64 20 63 61 75 73 65 20  hat could cause 
15806 64 65 61 64 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2f  deadlock..    */
15807 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f  .    if( SQLITE_
15808 4f 4b 3d 3d 73 71 6c 69 74 65 33 5f 6d 75 74 65  OK==sqlite3_mute
15809 78 5f 74 72 79 28 70 2d 3e 64 62 2d 3e 6d 75 74  x_try(p->db->mut
1580a 65 78 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 72  ex) ){.      nFr
1580b 65 65 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62  ee += sqlite3Vdb
1580c 65 52 65 6c 65 61 73 65 42 75 66 66 65 72 73 28  eReleaseBuffers(
1580d 70 29 3b 0a 20 20 20 20 20 20 73 74 6d 74 4c 72  p);.      stmtLr
1580e 75 52 65 6d 6f 76 65 4e 6f 6d 75 74 65 78 28 70  uRemoveNomutex(p
1580f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
15810 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e  _mutex_leave(p->
15811 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20  db->mutex);.    
15812 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  }.  }.  sqlite3_
15813 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69  mutex_leave(sqli
15814 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
15815 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
15816 54 49 43 5f 4c 52 55 32 29 29 3b 0a 0a 20 20 72  TIC_LRU2));..  r
15817 65 74 75 72 6e 20 6e 46 72 65 65 3b 0a 7d 0a 0a  eturn nFree;.}..
15818 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74  /*.** Call sqlit
15819 65 33 52 65 70 72 65 70 61 72 65 28 29 20 6f 6e  e3Reprepare() on
1581a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2e 20   the statement. 
1581b 52 65 6d 6f 76 65 20 69 74 20 66 72 6f 6d 20 74  Remove it from t
1581c 68 65 0a 2a 2a 20 6c 72 75 20 6c 69 73 74 20 62  he.** lru list b
1581d 65 66 6f 72 65 20 64 6f 69 6e 67 20 73 6f 2c 20  efore doing so, 
1581e 61 73 20 52 65 70 72 65 70 61 72 65 28 29 20 77  as Reprepare() w
1581f 69 6c 6c 20 66 72 65 65 20 61 6c 6c 20 74 68 65  ill free all the
15820 0a 2a 2a 20 6d 65 6d 6f 72 79 20 72 65 67 69 73  .** memory regis
15821 74 65 72 20 62 75 66 66 65 72 73 20 61 6e 79 77  ter buffers anyw
15822 61 79 2e 0a 2a 2f 0a 69 6e 74 20 76 64 62 65 52  ay..*/.int vdbeR
15823 65 70 72 65 70 61 72 65 28 56 64 62 65 20 2a 70  eprepare(Vdbe *p
15824 29 7b 0a 20 20 73 74 6d 74 4c 72 75 52 65 6d 6f  ){.  stmtLruRemo
15825 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ve(p);.  return 
15826 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65  sqlite3Reprepare
15827 28 70 29 3b 0a 7d 0a 0a 23 65 6c 73 65 20 20 20  (p);.}..#else   
15828 20 20 20 20 2f 2a 20 21 53 51 4c 49 54 45 5f 45      /* !SQLITE_E
15829 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e  NABLE_MEMORY_MAN
1582a 41 47 45 4d 45 4e 54 20 2a 2f 0a 20 20 23 64 65  AGEMENT */.  #de
1582b 66 69 6e 65 20 73 74 6d 74 4c 72 75 52 65 6d 6f  fine stmtLruRemo
1582c 76 65 28 78 29 0a 20 20 23 64 65 66 69 6e 65 20  ve(x).  #define 
1582d 73 74 6d 74 4c 72 75 41 64 64 28 78 29 0a 20 20  stmtLruAdd(x).  
1582e 23 64 65 66 69 6e 65 20 76 64 62 65 52 65 70 72  #define vdbeRepr
1582f 65 70 61 72 65 28 78 29 20 73 71 6c 69 74 65 33  epare(x) sqlite3
15830 52 65 70 72 65 70 61 72 65 28 78 29 0a 23 65 6e  Reprepare(x).#en
15831 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  dif.../*.** Retu
15832 72 6e 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65 72  rn TRUE (non-zer
15833 6f 29 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d  o) of the statem
15834 65 6e 74 20 73 75 70 70 6c 69 65 64 20 61 73 20  ent supplied as 
15835 61 6e 20 61 72 67 75 6d 65 6e 74 20 6e 65 65 64  an argument need
15836 73 0a 2a 2a 20 74 6f 20 62 65 20 72 65 63 6f 6d  s.** to be recom
15837 70 69 6c 65 64 2e 20 20 41 20 73 74 61 74 65 6d  piled.  A statem
15838 65 6e 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20  ent needs to be 
15839 72 65 63 6f 6d 70 69 6c 65 64 20 77 68 65 6e 65  recompiled whene
1583a 76 65 72 20 74 68 65 0a 2a 2a 20 65 78 65 63 75  ver the.** execu
1583b 74 69 6f 6e 20 65 6e 76 69 72 6f 6e 6d 65 6e 74  tion environment
1583c 20 63 68 61 6e 67 65 73 20 69 6e 20 61 20 77 61   changes in a wa
1583d 79 20 74 68 61 74 20 77 6f 75 6c 64 20 61 6c 74  y that would alt
1583e 65 72 20 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a  er the program.*
1583f 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 70  * that sqlite3_p
15840 72 65 70 61 72 65 28 29 20 67 65 6e 65 72 61 74  repare() generat
15841 65 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65  es.  For example
15842 2c 20 69 66 20 6e 65 77 20 66 75 6e 63 74 69 6f  , if new functio
15843 6e 73 20 6f 72 0a 2a 2a 20 63 6f 6c 6c 61 74 69  ns or.** collati
15844 6e 67 20 73 65 71 75 65 6e 63 65 73 20 61 72 65  ng sequences are
15845 20 72 65 67 69 73 74 65 72 65 64 20 6f 72 20 69   registered or i
15846 66 20 61 6e 20 61 75 74 68 6f 72 69 7a 65 72 20  f an authorizer 
15847 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 61  function is.** a
15848 64 64 65 64 20 6f 72 20 63 68 61 6e 67 65 64 2e  dded or changed.
15849 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
1584a 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72  nt sqlite3_expir
1584b 65 64 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  ed(sqlite3_stmt 
1584c 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20  *pStmt){.  Vdbe 
1584d 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d  *p = (Vdbe*)pStm
1584e 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30  t;.  return p==0
1584f 20 7c 7c 20 70 2d 3e 65 78 70 69 72 65 64 3b 0a   || p->expired;.
15850 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  }../*.** The fol
15851 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 64  lowing routine d
15852 65 73 74 72 6f 79 73 20 61 20 76 69 72 74 75 61  estroys a virtua
15853 6c 20 6d 61 63 68 69 6e 65 20 74 68 61 74 20 69  l machine that i
15854 73 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a 20  s created by.** 
15855 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  the sqlite3_comp
15856 69 6c 65 28 29 20 72 6f 75 74 69 6e 65 2e 20 54  ile() routine. T
15857 68 65 20 69 6e 74 65 67 65 72 20 72 65 74 75 72  he integer retur
15858 6e 65 64 20 69 73 20 61 6e 20 53 51 4c 49 54 45  ned is an SQLITE
15859 5f 0a 2a 2a 20 73 75 63 63 65 73 73 2f 66 61 69  _.** success/fai
1585a 6c 75 72 65 20 63 6f 64 65 20 74 68 61 74 20 64  lure code that d
1585b 65 73 63 72 69 62 65 73 20 74 68 65 20 72 65 73  escribes the res
1585c 75 6c 74 20 6f 66 20 65 78 65 63 75 74 69 6e 67  ult of executing
1585d 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20   the virtual.** 
1585e 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54  machine..**.** T
1585f 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73  his routine sets
15860 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20   the error code 
15861 61 6e 64 20 73 74 72 69 6e 67 20 72 65 74 75 72  and string retur
15862 6e 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65  ned by.** sqlite
15863 33 5f 65 72 72 63 6f 64 65 28 29 2c 20 73 71 6c  3_errcode(), sql
15864 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 61 6e  ite3_errmsg() an
15865 64 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  d sqlite3_errmsg
15866 31 36 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  16()..*/.SQLITE_
15867 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
15868 66 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33  finalize(sqlite3
15869 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20  _stmt *pStmt){. 
1586a 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70   int rc;.  if( p
1586b 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72  Stmt==0 ){.    r
1586c 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1586d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64 62 65   }else{.    Vdbe
1586e 20 2a 76 20 3d 20 28 56 64 62 65 2a 29 70 53 74   *v = (Vdbe*)pSt
1586f 6d 74 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  mt;.#ifndef SQLI
15870 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a 20 20  TE_MUTEX_NOOP.  
15871 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
15872 2a 6d 75 74 65 78 20 3d 20 76 2d 3e 64 62 2d 3e  *mutex = v->db->
15873 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66 0a 20 20  mutex;.#endif.  
15874 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
15875 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20  enter(mutex);.  
15876 20 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 28    stmtLruRemove(
15877 76 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  v);.    rc = sql
15878 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65  ite3VdbeFinalize
15879 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  (v);.    sqlite3
1587a 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74  _mutex_leave(mut
1587b 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ex);.  }.  retur
1587c 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
1587d 65 72 6d 69 6e 61 74 65 20 74 68 65 20 63 75 72  erminate the cur
1587e 72 65 6e 74 20 65 78 65 63 75 74 69 6f 6e 20 6f  rent execution o
1587f 66 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65  f an SQL stateme
15880 6e 74 20 61 6e 64 20 72 65 73 65 74 20 69 74 0a  nt and reset it.
15881 2a 2a 20 62 61 63 6b 20 74 6f 20 69 74 73 20 73  ** back to its s
15882 74 61 72 74 69 6e 67 20 73 74 61 74 65 20 73 6f  tarting state so
15883 20 74 68 61 74 20 69 74 20 63 61 6e 20 62 65 20   that it can be 
15884 72 65 75 73 65 64 2e 20 41 20 73 75 63 63 65 73  reused. A succes
15885 73 20 63 6f 64 65 20 66 72 6f 6d 0a 2a 2a 20 74  s code from.** t
15886 68 65 20 70 72 69 6f 72 20 65 78 65 63 75 74 69  he prior executi
15887 6f 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  on is returned..
15888 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
15889 6e 65 20 73 65 74 73 20 74 68 65 20 65 72 72 6f  ne sets the erro
1588a 72 20 63 6f 64 65 20 61 6e 64 20 73 74 72 69 6e  r code and strin
1588b 67 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a  g returned by.**
1588c 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65   sqlite3_errcode
1588d 28 29 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  (), sqlite3_errm
1588e 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  sg() and sqlite3
1588f 5f 65 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a  _errmsg16()..*/.
15890 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
15891 71 6c 69 74 65 33 5f 72 65 73 65 74 28 73 71 6c  qlite3_reset(sql
15892 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
15893 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69  ){.  int rc;.  i
15894 66 28 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a 20  f( pStmt==0 ){. 
15895 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
15896 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  K;.  }else{.    
15897 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a  Vdbe *v = (Vdbe*
15898 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69  )pStmt;.    sqli
15899 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
1589a 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  v->db->mutex);. 
1589b 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
1589c 64 62 65 52 65 73 65 74 28 76 2c 20 31 29 3b 0a  dbeReset(v, 1);.
1589d 20 20 20 20 73 74 6d 74 4c 72 75 41 64 64 28 76      stmtLruAdd(v
1589e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1589f 62 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 2d  beMakeReady(v, -
158a0 31 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20  1, 0, 0, 0);.   
158a1 20 61 73 73 65 72 74 28 20 28 72 63 20 26 20 28   assert( (rc & (
158a2 76 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 6b 29 29  v->db->errMask))
158a3 3d 3d 72 63 20 29 3b 0a 20 20 20 20 73 71 6c 69  ==rc );.    sqli
158a4 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
158a5 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  v->db->mutex);. 
158a6 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
158a7 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 61 6c 6c  }../*.** Set all
158a8 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20   the parameters 
158a9 69 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20  in the compiled 
158aa 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  SQL statement to
158ab 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45   NULL..*/.SQLITE
158ac 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
158ad 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 28  _clear_bindings(
158ae 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
158af 74 6d 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  tmt){.  int i;. 
158b0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
158b1 5f 4f 4b 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d  _OK;.  Vdbe *p =
158b2 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 23   (Vdbe*)pStmt;.#
158b3 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 55  ifndef SQLITE_MU
158b4 54 45 58 5f 4e 4f 4f 50 0a 20 20 73 71 6c 69 74  TEX_NOOP.  sqlit
158b5 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20  e3_mutex *mutex 
158b6 3d 20 28 28 56 64 62 65 2a 29 70 53 74 6d 74 29  = ((Vdbe*)pStmt)
158b7 2d 3e 64 62 2d 3e 6d 75 74 65 78 3b 0a 23 65 6e  ->db->mutex;.#en
158b8 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  dif.  sqlite3_mu
158b9 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29  tex_enter(mutex)
158ba 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
158bb 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20 20  ->nVar; i++){.  
158bc 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
158bd 52 65 6c 65 61 73 65 28 26 70 2d 3e 61 56 61 72  Release(&p->aVar
158be 5b 69 5d 29 3b 0a 20 20 20 20 70 2d 3e 61 56 61  [i]);.    p->aVa
158bf 72 5b 69 5d 2e 66 6c 61 67 73 20 3d 20 4d 45 4d  r[i].flags = MEM
158c0 5f 4e 75 6c 6c 3b 0a 20 20 7d 0a 20 20 73 71 6c  _Null;.  }.  sql
158c1 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
158c2 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  (mutex);.  retur
158c3 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a  n rc;.}.../*****
158c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
158c5 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f  ******* sqlite3_
158c6 76 61 6c 75 65 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a  value_  ********
158c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
158c8 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66  *******.** The f
158c9 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65  ollowing routine
158ca 73 20 65 78 74 72 61 63 74 20 69 6e 66 6f 72 6d  s extract inform
158cb 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 4d 65 6d  ation from a Mem
158cc 20 6f 72 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   or sqlite3_valu
158cd 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a  e.** structure..
158ce 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
158cf 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
158d0 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 73 71 6c  3_value_blob(sql
158d1 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c  ite3_value *pVal
158d2 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 28 4d  ){.  Mem *p = (M
158d3 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 69 66 28 20  em*)pVal;.  if( 
158d4 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  p->flags & (MEM_
158d5 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 20 29 7b  Blob|MEM_Str) ){
158d6 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
158d7 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 70 29  MemExpandBlob(p)
158d8 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 26  ;.    p->flags &
158d9 3d 20 7e 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20  = ~MEM_Str;.    
158da 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f  p->flags |= MEM_
158db 42 6c 6f 62 3b 0a 20 20 20 20 72 65 74 75 72 6e  Blob;.    return
158dc 20 70 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a   p->z;.  }else{.
158dd 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
158de 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 70 56  e3_value_text(pV
158df 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54  al);.  }.}.SQLIT
158e0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
158e1 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 73 71  3_value_bytes(sq
158e2 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
158e3 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  l){.  return sql
158e4 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 70  ite3ValueBytes(p
158e5 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  Val, SQLITE_UTF8
158e6 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  );.}.SQLITE_API 
158e7 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  int sqlite3_valu
158e8 65 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74 65  e_bytes16(sqlite
158e9 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a  3_value *pVal){.
158ea 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
158eb 56 61 6c 75 65 42 79 74 65 73 28 70 56 61 6c 2c  ValueBytes(pVal,
158ec 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
158ed 49 56 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  IVE);.}.SQLITE_A
158ee 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65  PI double sqlite
158ef 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 73  3_value_double(s
158f0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
158f1 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  al){.  return sq
158f2 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c  lite3VdbeRealVal
158f3 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a  ue((Mem*)pVal);.
158f4 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
158f5 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
158f6 6e 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt(sqlite3_value
158f7 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72   *pVal){.  retur
158f8 6e 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74  n sqlite3VdbeInt
158f9 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c  Value((Mem*)pVal
158fa 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  );.}.SQLITE_API 
158fb 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 6c  sqlite_int64 sql
158fc 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34  ite3_value_int64
158fd 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  (sqlite3_value *
158fe 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20  pVal){.  return 
158ff 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61  sqlite3VdbeIntVa
15900 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b  lue((Mem*)pVal);
15901 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  .}.SQLITE_API co
15902 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
15903 72 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  r *sqlite3_value
15904 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61  _text(sqlite3_va
15905 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65  lue *pVal){.  re
15906 74 75 72 6e 20 28 63 6f 6e 73 74 20 75 6e 73 69  turn (const unsi
15907 67 6e 65 64 20 63 68 61 72 20 2a 29 73 71 6c 69  gned char *)sqli
15908 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61  te3ValueText(pVa
15909 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b  l, SQLITE_UTF8);
1590a 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  .}.#ifndef SQLIT
1590b 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c  E_OMIT_UTF16.SQL
1590c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
1590d 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75  id *sqlite3_valu
1590e 65 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 33  e_text16(sqlite3
1590f 5f 76 61 6c 75 65 2a 20 70 56 61 6c 29 7b 0a 20  _value* pVal){. 
15910 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56   return sqlite3V
15911 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53  alueText(pVal, S
15912 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
15913 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  E);.}.SQLITE_API
15914 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
15915 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
15916 36 62 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  6be(sqlite3_valu
15917 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75  e *pVal){.  retu
15918 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54  rn sqlite3ValueT
15919 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45  ext(pVal, SQLITE
1591a 5f 55 54 46 31 36 42 45 29 3b 0a 7d 0a 53 51 4c  _UTF16BE);.}.SQL
1591b 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
1591c 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75  id *sqlite3_valu
1591d 65 5f 74 65 78 74 31 36 6c 65 28 73 71 6c 69 74  e_text16le(sqlit
1591e 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b  e3_value *pVal){
1591f 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
15920 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c  3ValueText(pVal,
15921 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 29   SQLITE_UTF16LE)
15922 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
15923 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
15924 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
15925 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  t sqlite3_value_
15926 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c  type(sqlite3_val
15927 75 65 2a 20 70 56 61 6c 29 7b 0a 20 20 72 65 74  ue* pVal){.  ret
15928 75 72 6e 20 70 56 61 6c 2d 3e 74 79 70 65 3b 0a  urn pVal->type;.
15929 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
1592a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1592b 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1592c 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a    **************
1592d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1592e 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
1592f 6e 67 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  ng routines are 
15930 75 73 65 64 20 62 79 20 75 73 65 72 2d 64 65 66  used by user-def
15931 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 74  ined functions t
15932 6f 20 73 70 65 63 69 66 79 0a 2a 2a 20 74 68 65  o specify.** the
15933 20 66 75 6e 63 74 69 6f 6e 20 72 65 73 75 6c 74   function result
15934 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
15935 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
15936 75 6c 74 5f 62 6c 6f 62 28 0a 20 20 73 71 6c 69  ult_blob(.  sqli
15937 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74  te3_context *pCt
15938 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  x, .  const void
15939 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a   *z, .  int n, .
1593a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76    void (*xDel)(v
1593b 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65  oid *).){.  asse
1593c 72 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 61 73  rt( n>=0 );.  as
1593d 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1593e 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
1593f 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
15940 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
15941 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20  etStr(&pCtx->s, 
15942 7a 2c 20 6e 2c 20 30 2c 20 78 44 65 6c 29 3b 0a  z, n, 0, xDel);.
15943 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  }.SQLITE_API voi
15944 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
15945 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f  _double(sqlite3_
15946 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 64  context *pCtx, d
15947 6f 75 62 6c 65 20 72 56 61 6c 29 7b 0a 20 20 61  ouble rVal){.  a
15948 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
15949 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e  utex_held(pCtx->
1594a 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  s.db->mutex) );.
1594b 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1594c 53 65 74 44 6f 75 62 6c 65 28 26 70 43 74 78 2d  SetDouble(&pCtx-
1594d 3e 73 2c 20 72 56 61 6c 29 3b 0a 7d 0a 53 51 4c  >s, rVal);.}.SQL
1594e 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
1594f 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
15950 72 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  r(sqlite3_contex
15951 74 20 2a 70 43 74 78 2c 20 63 6f 6e 73 74 20 63  t *pCtx, const c
15952 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a  har *z, int n){.
15953 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
15954 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74  3_mutex_held(pCt
15955 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  x->s.db->mutex) 
15956 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72  );.  pCtx->isErr
15957 6f 72 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  or = SQLITE_ERRO
15958 52 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  R;.  sqlite3Vdbe
15959 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d  MemSetStr(&pCtx-
1595a 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45  >s, z, n, SQLITE
1595b 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 54 52  _UTF8, SQLITE_TR
1595c 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 69 66 6e  ANSIENT);.}.#ifn
1595d 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1595e 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49  UTF16.SQLITE_API
1595f 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
15960 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 73 71 6c  sult_error16(sql
15961 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43  ite3_context *pC
15962 74 78 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  tx, const void *
15963 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73  z, int n){.  ass
15964 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
15965 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e  ex_held(pCtx->s.
15966 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
15967 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20  pCtx->isError = 
15968 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
15969 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1596a 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a  tStr(&pCtx->s, z
1596b 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  , n, SQLITE_UTF1
1596c 36 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f  6NATIVE, SQLITE_
1596d 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 65  TRANSIENT);.}.#e
1596e 6e 64 69 66 0a 53 51 4c 49 54 45 5f 41 50 49 20  ndif.SQLITE_API 
1596f 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
15970 75 6c 74 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f  ult_int(sqlite3_
15971 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69  context *pCtx, i
15972 6e 74 20 69 56 61 6c 29 7b 0a 20 20 61 73 73 65  nt iVal){.  asse
15973 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
15974 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64  x_held(pCtx->s.d
15975 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73  b->mutex) );.  s
15976 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
15977 49 6e 74 36 34 28 26 70 43 74 78 2d 3e 73 2c 20  Int64(&pCtx->s, 
15978 28 69 36 34 29 69 56 61 6c 29 3b 0a 7d 0a 53 51  (i64)iVal);.}.SQ
15979 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
1597a 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
1597b 36 34 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  64(sqlite3_conte
1597c 78 74 20 2a 70 43 74 78 2c 20 69 36 34 20 69 56  xt *pCtx, i64 iV
1597d 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73  al){.  assert( s
1597e 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1597f 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75  d(pCtx->s.db->mu
15980 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65  tex) );.  sqlite
15981 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34  3VdbeMemSetInt64
15982 28 26 70 43 74 78 2d 3e 73 2c 20 69 56 61 6c 29  (&pCtx->s, iVal)
15983 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  ;.}.SQLITE_API v
15984 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
15985 6c 74 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f  lt_null(sqlite3_
15986 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 29 7b 0a  context *pCtx){.
15987 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
15988 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74  3_mutex_held(pCt
15989 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  x->s.db->mutex) 
1598a 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
1598b 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 70 43 74 78  MemSetNull(&pCtx
1598c 2d 3e 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  ->s);.}.SQLITE_A
1598d 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
1598e 72 65 73 75 6c 74 5f 74 65 78 74 28 0a 20 20 73  result_text(.  s
1598f 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
15990 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 63  pCtx, .  const c
15991 68 61 72 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e  har *z, .  int n
15992 2c 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29  ,.  void (*xDel)
15993 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73  (void *).){.  as
15994 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
15995 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
15996 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
15997 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
15998 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20  etStr(&pCtx->s, 
15999 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46  z, n, SQLITE_UTF
1599a 38 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23 69 66 6e  8, xDel);.}.#ifn
1599b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1599c 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49  UTF16.SQLITE_API
1599d 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
1599e 73 75 6c 74 5f 74 65 78 74 31 36 28 0a 20 20 73  sult_text16(.  s
1599f 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
159a0 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76  pCtx, .  const v
159a1 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e  oid *z, .  int n
159a2 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c  , .  void (*xDel
159a3 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61  )(void *).){.  a
159a4 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
159a5 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e  utex_held(pCtx->
159a6 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  s.db->mutex) );.
159a7 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
159a8 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c  SetStr(&pCtx->s,
159a9 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54   z, n, SQLITE_UT
159aa 46 31 36 4e 41 54 49 56 45 2c 20 78 44 65 6c 29  F16NATIVE, xDel)
159ab 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  ;.}.SQLITE_API v
159ac 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
159ad 6c 74 5f 74 65 78 74 31 36 62 65 28 0a 20 20 73  lt_text16be(.  s
159ae 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
159af 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76  pCtx, .  const v
159b0 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e  oid *z, .  int n
159b1 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c  , .  void (*xDel
159b2 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61  )(void *).){.  a
159b3 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
159b4 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e  utex_held(pCtx->
159b5 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  s.db->mutex) );.
159b6 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
159b7 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c  SetStr(&pCtx->s,
159b8 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54   z, n, SQLITE_UT
159b9 46 31 36 42 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a  F16BE, xDel);.}.
159ba 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
159bb 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
159bc 65 78 74 31 36 6c 65 28 0a 20 20 73 71 6c 69 74  ext16le(.  sqlit
159bd 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
159be 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  , .  const void 
159bf 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20  *z, .  int n, . 
159c0 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
159c1 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72  id *).){.  asser
159c2 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
159c3 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
159c4 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71  ->mutex) );.  sq
159c5 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53  lite3VdbeMemSetS
159c6 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20  tr(&pCtx->s, z, 
159c7 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  n, SQLITE_UTF16L
159c8 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23 65 6e 64  E, xDel);.}.#end
159c9 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
159ca 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54  T_UTF16 */.SQLIT
159cb 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
159cc 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28  e3_result_value(
159cd 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
159ce 2a 70 43 74 78 2c 20 73 71 6c 69 74 65 33 5f 76  *pCtx, sqlite3_v
159cf 61 6c 75 65 20 2a 70 56 61 6c 75 65 29 7b 0a 20  alue *pValue){. 
159d0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
159d1 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78  _mutex_held(pCtx
159d2 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ->s.db->mutex) )
159d3 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
159d4 65 6d 43 6f 70 79 28 26 70 43 74 78 2d 3e 73 2c  emCopy(&pCtx->s,
159d5 20 70 56 61 6c 75 65 29 3b 0a 7d 0a 53 51 4c 49   pValue);.}.SQLI
159d6 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
159d7 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62  te3_result_zerob
159d8 6c 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  lob(sqlite3_cont
159d9 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 6e  ext *pCtx, int n
159da 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
159db 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
159dc 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65  pCtx->s.db->mute
159dd 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  x) );.  sqlite3V
159de 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f  dbeMemSetZeroBlo
159df 62 28 26 70 43 74 78 2d 3e 73 2c 20 6e 29 3b 0a  b(&pCtx->s, n);.
159e0 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  }.SQLITE_API voi
159e1 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
159e2 5f 65 72 72 6f 72 5f 63 6f 64 65 28 73 71 6c 69  _error_code(sqli
159e3 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74  te3_context *pCt
159e4 78 2c 20 69 6e 74 20 65 72 72 43 6f 64 65 29 7b  x, int errCode){
159e5 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72  .  pCtx->isError
159e6 20 3d 20 65 72 72 43 6f 64 65 3b 0a 7d 0a 0a 2f   = errCode;.}../
159e7 2a 20 46 6f 72 63 65 20 61 6e 20 53 51 4c 49 54  * Force an SQLIT
159e8 45 5f 54 4f 4f 42 49 47 20 65 72 72 6f 72 2e 20  E_TOOBIG error. 
159e9 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
159ea 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
159eb 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 73  t_error_toobig(s
159ec 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
159ed 70 43 74 78 29 7b 0a 20 20 61 73 73 65 72 74 28  pCtx){.  assert(
159ee 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
159ef 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e  eld(pCtx->s.db->
159f0 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 74 78  mutex) );.  pCtx
159f1 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49  ->isError = SQLI
159f2 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 73 71 6c  TE_TOOBIG;.  sql
159f3 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
159f4 72 28 26 70 43 74 78 2d 3e 73 2c 20 22 73 74 72  r(&pCtx->s, "str
159f5 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74 6f 6f 20  ing or blob too 
159f6 62 69 67 22 2c 20 2d 31 2c 20 0a 20 20 20 20 20  big", -1, .     
159f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
159f8 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53    SQLITE_UTF8, S
159f9 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d  QLITE_STATIC);.}
159fa 0a 0a 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f 4e  ../* An SQLITE_N
159fb 4f 4d 45 4d 20 65 72 72 6f 72 2e 20 2a 2f 0a 53  OMEM error. */.S
159fc 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
159fd 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
159fe 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65  ror_nomem(sqlite
159ff 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 29  3_context *pCtx)
15a00 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  {.  assert( sqli
15a01 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
15a02 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78  Ctx->s.db->mutex
15a03 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ) );.  sqlite3Vd
15a04 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 70 43  beMemSetNull(&pC
15a05 74 78 2d 3e 73 29 3b 0a 20 20 70 43 74 78 2d 3e  tx->s);.  pCtx->
15a06 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 45  isError = SQLITE
15a07 5f 4e 4f 4d 45 4d 3b 0a 20 20 70 43 74 78 2d 3e  _NOMEM;.  pCtx->
15a08 73 2e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  s.db->mallocFail
15a09 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ed = 1;.}../*.**
15a0a 20 45 78 65 63 75 74 65 20 74 68 65 20 73 74 61   Execute the sta
15a0b 74 65 6d 65 6e 74 20 70 53 74 6d 74 2c 20 65 69  tement pStmt, ei
15a0c 74 68 65 72 20 75 6e 74 69 6c 20 61 20 72 6f 77  ther until a row
15a0d 20 6f 66 20 64 61 74 61 20 69 73 20 72 65 61 64   of data is read
15a0e 79 2c 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d  y, the.** statem
15a0f 65 6e 74 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c  ent is completel
15a10 79 20 65 78 65 63 75 74 65 64 20 6f 72 20 61 6e  y executed or an
15a11 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a   error occurs..*
15a12 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
15a13 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  e implements the
15a14 20 62 75 6c 6b 20 6f 66 20 74 68 65 20 6c 6f 67   bulk of the log
15a15 69 63 20 62 65 68 69 6e 64 20 74 68 65 20 73 71  ic behind the sq
15a16 6c 69 74 65 5f 73 74 65 70 28 29 0a 2a 2a 20 41  lite_step().** A
15a17 50 49 2e 20 20 54 68 65 20 6f 6e 6c 79 20 74 68  PI.  The only th
15a18 69 6e 67 20 6f 6d 69 74 74 65 64 20 69 73 20 74  ing omitted is t
15a19 68 65 20 61 75 74 6f 6d 61 74 69 63 20 72 65 63  he automatic rec
15a1a 6f 6d 70 69 6c 65 20 69 66 20 61 20 0a 2a 2a 20  ompile if a .** 
15a1b 73 63 68 65 6d 61 20 63 68 61 6e 67 65 20 68 61  schema change ha
15a1c 73 20 6f 63 63 75 72 72 65 64 2e 20 20 54 68 61  s occurred.  Tha
15a1d 74 20 64 65 74 61 69 6c 20 69 73 20 68 61 6e 64  t detail is hand
15a1e 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 6f 75  led by the.** ou
15a1f 74 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70  ter sqlite3_step
15a20 28 29 20 77 72 61 70 70 65 72 20 70 72 6f 63 65  () wrapper proce
15a21 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  dure..*/.static 
15a22 69 6e 74 20 73 71 6c 69 74 65 33 53 74 65 70 28  int sqlite3Step(
15a23 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69  Vdbe *p){.  sqli
15a24 74 65 33 20 2a 64 62 3b 0a 20 20 69 6e 74 20 72  te3 *db;.  int r
15a25 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 70 29 3b  c;..  assert(p);
15a26 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21  .  if( p->magic!
15a27 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20  =VDBE_MAGIC_RUN 
15a28 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
15a29 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d  LITE_MISUSE;.  }
15a2a 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68  ..  /* Assert th
15a2b 61 74 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20  at malloc() has 
15a2c 6e 6f 74 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20  not failed */.  
15a2d 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 61 73  db = p->db;.  as
15a2e 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f  sert( !db->mallo
15a2f 63 46 61 69 6c 65 64 20 29 3b 0a 0a 20 20 69 66  cFailed );..  if
15a30 28 20 70 2d 3e 61 62 6f 72 74 65 64 20 29 7b 0a  ( p->aborted ){.
15a31 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
15a32 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 20 20 69  E_ABORT;.  }.  i
15a33 66 28 20 70 2d 3e 70 63 3c 3d 30 20 26 26 20 70  f( p->pc<=0 && p
15a34 2d 3e 65 78 70 69 72 65 64 20 29 7b 0a 20 20 20  ->expired ){.   
15a35 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49   if( p->rc==SQLI
15a36 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70  TE_OK ){.      p
15a37 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 53 43  ->rc = SQLITE_SC
15a38 48 45 4d 41 3b 0a 20 20 20 20 7d 0a 20 20 20 20  HEMA;.    }.    
15a39 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
15a3a 52 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  R;.    goto end_
15a3b 6f 66 5f 73 74 65 70 3b 0a 20 20 7d 0a 20 20 69  of_step;.  }.  i
15a3c 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
15a3d 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 70 2d  On(db) ){.    p-
15a3e 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53  >rc = SQLITE_MIS
15a3f 55 53 45 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  USE;.    return 
15a40 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
15a41 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 63 3c 30   }.  if( p->pc<0
15a42 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   ){.    /* If th
15a43 65 72 65 20 61 72 65 20 6e 6f 20 6f 74 68 65 72  ere are no other
15a44 20 73 74 61 74 65 6d 65 6e 74 73 20 63 75 72 72   statements curr
15a45 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 2c 20 74  ently running, t
15a46 68 65 6e 0a 20 20 20 20 2a 2a 20 72 65 73 65 74  hen.    ** reset
15a47 20 74 68 65 20 69 6e 74 65 72 72 75 70 74 20 66   the interrupt f
15a48 6c 61 67 2e 20 20 54 68 69 73 20 70 72 65 76 65  lag.  This preve
15a49 6e 74 73 20 61 20 63 61 6c 6c 20 74 6f 20 73 71  nts a call to sq
15a4a 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 0a  lite3_interrupt.
15a4b 20 20 20 20 2a 2a 20 66 72 6f 6d 20 69 6e 74 65      ** from inte
15a4c 72 72 75 70 74 69 6e 67 20 61 20 73 74 61 74 65  rrupting a state
15a4d 6d 65 6e 74 20 74 68 61 74 20 68 61 73 20 6e 6f  ment that has no
15a4e 74 20 79 65 74 20 73 74 61 72 74 65 64 2e 0a 20  t yet started.. 
15a4f 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62     */.    if( db
15a50 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3d  ->activeVdbeCnt=
15a51 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e  =0 ){.      db->
15a52 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64  u1.isInterrupted
15a53 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 23 69 66   = 0;.    }..#if
15a54 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
15a55 5f 54 52 41 43 45 0a 20 20 20 20 69 66 28 20 64  _TRACE.    if( d
15a56 62 2d 3e 78 50 72 6f 66 69 6c 65 20 26 26 20 21  b->xProfile && !
15a57 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b  db->init.busy ){
15a58 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 4e  .      double rN
15a59 6f 77 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ow;.      sqlite
15a5a 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 64  3OsCurrentTime(d
15a5b 62 2d 3e 70 56 66 73 2c 20 26 72 4e 6f 77 29 3b  b->pVfs, &rNow);
15a5c 0a 20 20 20 20 20 20 70 2d 3e 73 74 61 72 74 54  .      p->startT
15a5d 69 6d 65 20 3d 20 28 72 4e 6f 77 20 2d 20 28 69  ime = (rNow - (i
15a5e 6e 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a  nt)rNow)*3600.0*
15a5f 32 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30 2e  24.0*1000000000.
15a60 30 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  0;.    }.#endif.
15a61 0a 20 20 20 20 64 62 2d 3e 61 63 74 69 76 65 56  .    db->activeV
15a62 64 62 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 70 2d  dbeCnt++;.    p-
15a63 3e 70 63 20 3d 20 30 3b 0a 20 20 20 20 73 74 6d  >pc = 0;.    stm
15a64 74 4c 72 75 52 65 6d 6f 76 65 28 70 29 3b 0a 20  tLruRemove(p);. 
15a65 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
15a66 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20  E_OMIT_EXPLAIN. 
15a67 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 20   if( p->explain 
15a68 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
15a69 74 65 33 56 64 62 65 4c 69 73 74 28 70 29 3b 0a  te3VdbeList(p);.
15a6a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f    }else.#endif /
15a6b 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58  * SQLITE_OMIT_EX
15a6c 50 4c 41 49 4e 20 2a 2f 0a 20 20 7b 0a 20 20 20  PLAIN */.  {.   
15a6d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
15a6e 65 45 78 65 63 28 70 29 3b 0a 20 20 7d 0a 0a 20  eExec(p);.  }.. 
15a6f 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
15a70 74 79 4f 66 66 28 64 62 29 20 29 7b 0a 20 20 20  tyOff(db) ){.   
15a71 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53   rc = SQLITE_MIS
15a72 55 53 45 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65  USE;.  }..#ifnde
15a73 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
15a74 41 43 45 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20  ACE.  /* Invoke 
15a75 74 68 65 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c  the profile call
15a76 62 61 63 6b 20 69 66 20 74 68 65 72 65 20 69 73  back if there is
15a77 20 6f 6e 65 0a 20 20 2a 2f 0a 20 20 69 66 28 20   one.  */.  if( 
15a78 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26  rc!=SQLITE_ROW &
15a79 26 20 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 26  & db->xProfile &
15a7a 26 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  & !db->init.busy
15a7b 20 26 26 20 70 2d 3e 6e 4f 70 3e 30 0a 20 20 20   && p->nOp>0.   
15a7c 20 20 20 20 20 20 20 20 26 26 20 70 2d 3e 61 4f          && p->aO
15a7d 70 5b 30 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  p[0].opcode==OP_
15a7e 54 72 61 63 65 20 26 26 20 70 2d 3e 61 4f 70 5b  Trace && p->aOp[
15a7f 30 5d 2e 70 34 2e 7a 21 3d 30 20 29 7b 0a 20 20  0].p4.z!=0 ){.  
15a80 20 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20    double rNow;. 
15a81 20 20 20 75 36 34 20 65 6c 61 70 73 65 54 69 6d     u64 elapseTim
15a82 65 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 4f  e;..    sqlite3O
15a83 73 43 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d  sCurrentTime(db-
15a84 3e 70 56 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20  >pVfs, &rNow);. 
15a85 20 20 20 65 6c 61 70 73 65 54 69 6d 65 20 3d 20     elapseTime = 
15a86 28 72 4e 6f 77 20 2d 20 28 69 6e 74 29 72 4e 6f  (rNow - (int)rNo
15a87 77 29 2a 33 36 30 30 2e 30 2a 32 34 2e 30 2a 31  w)*3600.0*24.0*1
15a88 30 30 30 30 30 30 30 30 30 2e 30 20 2d 20 70 2d  000000000.0 - p-
15a89 3e 73 74 61 72 74 54 69 6d 65 3b 0a 20 20 20 20  >startTime;.    
15a8a 64 62 2d 3e 78 50 72 6f 66 69 6c 65 28 64 62 2d  db->xProfile(db-
15a8b 3e 70 50 72 6f 66 69 6c 65 41 72 67 2c 20 70 2d  >pProfileArg, p-
15a8c 3e 61 4f 70 5b 30 5d 2e 70 34 2e 7a 2c 20 65 6c  >aOp[0].p4.z, el
15a8d 61 70 73 65 54 69 6d 65 29 3b 0a 20 20 7d 0a 23  apseTime);.  }.#
15a8e 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33  endif..  sqlite3
15a8f 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 72 63 2c  Error(p->db, rc,
15a90 20 30 29 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 73   0);.  p->rc = s
15a91 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 70 2d  qlite3ApiExit(p-
15a92 3e 64 62 2c 20 70 2d 3e 72 63 29 3b 0a 65 6e 64  >db, p->rc);.end
15a93 5f 6f 66 5f 73 74 65 70 3a 0a 20 20 61 73 73 65  _of_step:.  asse
15a94 72 74 28 20 28 72 63 26 30 78 66 66 29 3d 3d 72  rt( (rc&0xff)==r
15a95 63 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 7a 53  c );.  if( p->zS
15a96 71 6c 20 26 26 20 28 72 63 26 30 78 66 66 29 3c  ql && (rc&0xff)<
15a97 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
15a98 20 20 2f 2a 20 54 68 69 73 20 62 65 68 61 76 69    /* This behavi
15a99 6f 72 20 6f 63 63 75 72 73 20 69 66 20 73 71 6c  or occurs if sql
15a9a 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
15a9b 29 20 77 61 73 20 75 73 65 64 20 74 6f 20 62 75  ) was used to bu
15a9c 69 6c 64 0a 20 20 20 20 2a 2a 20 74 68 65 20 70  ild.    ** the p
15a9d 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
15a9e 74 2e 20 20 52 65 74 75 72 6e 20 65 72 72 6f 72  t.  Return error
15a9f 20 63 6f 64 65 73 20 64 69 72 65 63 74 6c 79 20   codes directly 
15aa0 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  */.    sqlite3Er
15aa1 72 6f 72 28 70 2d 3e 64 62 2c 20 70 2d 3e 72 63  ror(p->db, p->rc
15aa2 2c 20 30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  , 0);.    return
15aa3 20 70 2d 3e 72 63 3b 0a 20 20 7d 65 6c 73 65 7b   p->rc;.  }else{
15aa4 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20  .    /* This is 
15aa5 66 6f 72 20 6c 65 67 61 63 79 20 73 71 6c 69 74  for legacy sqlit
15aa6 65 33 5f 70 72 65 70 61 72 65 28 29 20 62 75 69  e3_prepare() bui
15aa7 6c 64 73 20 61 6e 64 20 77 68 65 6e 20 74 68 65  lds and when the
15aa8 20 63 6f 64 65 0a 20 20 20 20 2a 2a 20 69 73 20   code.    ** is 
15aa9 53 51 4c 49 54 45 5f 52 4f 57 20 6f 72 20 53 51  SQLITE_ROW or SQ
15aaa 4c 49 54 45 5f 44 4f 4e 45 20 2a 2f 0a 20 20 20  LITE_DONE */.   
15aab 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
15aac 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73  }../*.** This is
15aad 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20 69   the top-level i
15aae 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
15aaf 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e   sqlite3_step().
15ab0 20 20 43 61 6c 6c 0a 2a 2a 20 73 71 6c 69 74 65    Call.** sqlite
15ab1 33 53 74 65 70 28 29 20 74 6f 20 64 6f 20 6d 6f  3Step() to do mo
15ab2 73 74 20 6f 66 20 74 68 65 20 77 6f 72 6b 2e 20  st of the work. 
15ab3 20 49 66 20 61 20 73 63 68 65 6d 61 20 65 72 72   If a schema err
15ab4 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 63 61  or occurs,.** ca
15ab5 6c 6c 20 73 71 6c 69 74 65 33 52 65 70 72 65 70  ll sqlite3Reprep
15ab6 61 72 65 28 29 20 61 6e 64 20 74 72 79 20 61 67  are() and try ag
15ab7 61 69 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ain..*/.#ifdef S
15ab8 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53 45  QLITE_OMIT_PARSE
15ab9 52 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  R.SQLITE_API int
15aba 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 71   sqlite3_step(sq
15abb 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
15abc 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  t){.  int rc = S
15abd 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
15abe 69 66 28 20 70 53 74 6d 74 20 29 7b 0a 20 20 20  if( pStmt ){.   
15abf 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 20 76 20   Vdbe *v;.    v 
15ac0 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a  = (Vdbe*)pStmt;.
15ac1 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
15ac2 78 5f 65 6e 74 65 72 28 76 2d 3e 64 62 2d 3e 6d  x_enter(v->db->m
15ac3 75 74 65 78 29 3b 0a 20 20 20 20 72 63 20 3d 20  utex);.    rc = 
15ac4 73 71 6c 69 74 65 33 53 74 65 70 28 76 29 3b 0a  sqlite3Step(v);.
15ac5 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
15ac6 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62 2d 3e 6d  x_leave(v->db->m
15ac7 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74  utex);.  }.  ret
15ac8 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6c 73 65 0a  urn rc;.}.#else.
15ac9 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
15aca 71 6c 69 74 65 33 5f 73 74 65 70 28 73 71 6c 69  qlite3_step(sqli
15acb 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29  te3_stmt *pStmt)
15acc 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
15acd 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 69 66  ITE_MISUSE;.  if
15ace 28 20 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 69  ( pStmt ){.    i
15acf 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20  nt cnt = 0;.    
15ad0 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a  Vdbe *v = (Vdbe*
15ad1 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69  )pStmt;.    sqli
15ad2 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e 64 62 3b  te3 *db = v->db;
15ad3 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
15ad4 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
15ad5 65 78 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  ex);.    while( 
15ad6 28 72 63 20 3d 20 73 71 6c 69 74 65 33 53 74 65  (rc = sqlite3Ste
15ad7 70 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f 53 43  p(v))==SQLITE_SC
15ad8 48 45 4d 41 0a 20 20 20 20 20 20 20 20 20 20 20  HEMA.           
15ad9 26 26 20 63 6e 74 2b 2b 20 3c 20 35 0a 20 20 20  && cnt++ < 5.   
15ada 20 20 20 20 20 20 20 20 26 26 20 76 64 62 65 52          && vdbeR
15adb 65 70 72 65 70 61 72 65 28 76 29 20 29 7b 0a 20  eprepare(v) ){. 
15adc 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
15add 65 74 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 20  et(pStmt);.     
15ade 20 76 2d 3e 65 78 70 69 72 65 64 20 3d 20 30 3b   v->expired = 0;
15adf 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
15ae0 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  c==SQLITE_SCHEMA
15ae1 20 26 26 20 76 2d 3e 7a 53 71 6c 20 26 26 20 64   && v->zSql && d
15ae2 62 2d 3e 70 45 72 72 20 29 7b 0a 20 20 20 20 20  b->pErr ){.     
15ae3 20 2f 2a 20 54 68 69 73 20 63 61 73 65 20 6f 63   /* This case oc
15ae4 63 75 72 73 20 61 66 74 65 72 20 66 61 69 6c 69  curs after faili
15ae5 6e 67 20 74 6f 20 72 65 63 6f 6d 70 69 6c 65 20  ng to recompile 
15ae6 61 6e 20 73 71 6c 20 73 74 61 74 65 6d 65 6e 74  an sql statement
15ae7 2e 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20  . .      ** The 
15ae8 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72  error message fr
15ae9 6f 6d 20 74 68 65 20 53 51 4c 20 63 6f 6d 70 69  om the SQL compi
15aea 6c 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20  ler has already 
15aeb 62 65 65 6e 20 6c 6f 61 64 65 64 20 0a 20 20 20  been loaded .   
15aec 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 64     ** into the d
15aed 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20  atabase handle. 
15aee 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70 69 65  This block copie
15aef 73 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73  s the error mess
15af0 61 67 65 20 0a 20 20 20 20 20 20 2a 2a 20 66 72  age .      ** fr
15af1 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20  om the database 
15af2 68 61 6e 64 6c 65 20 69 6e 74 6f 20 74 68 65 20  handle into the 
15af3 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 73 65  statement and se
15af4 74 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  ts the statement
15af5 0a 20 20 20 20 20 20 2a 2a 20 70 72 6f 67 72 61  .      ** progra
15af6 6d 20 63 6f 75 6e 74 65 72 20 74 6f 20 30 20 74  m counter to 0 t
15af7 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 77 68  o ensure that wh
15af8 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  en the statement
15af9 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 66 69   is .      ** fi
15afa 6e 61 6c 69 7a 65 64 20 6f 72 20 72 65 73 65 74  nalized or reset
15afb 20 74 68 65 20 70 61 72 73 65 72 20 65 72 72 6f   the parser erro
15afc 72 20 6d 65 73 73 61 67 65 20 69 73 20 61 76 61  r message is ava
15afd 69 6c 61 62 6c 65 20 76 69 61 0a 20 20 20 20 20  ilable via.     
15afe 20 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 6d   ** sqlite3_errm
15aff 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  sg() and sqlite3
15b00 5f 65 72 72 63 6f 64 65 28 29 2e 0a 20 20 20 20  _errcode()..    
15b01 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74    */.      const
15b02 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 28 63   char *zErr = (c
15b03 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69  onst char *)sqli
15b04 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 64  te3_value_text(d
15b05 62 2d 3e 70 45 72 72 29 3b 20 0a 20 20 20 20 20  b->pErr); .     
15b06 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76 2d   sqlite3_free(v-
15b07 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20  >zErrMsg);.     
15b08 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63   if( !db->malloc
15b09 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
15b0a 20 20 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 73    v->zErrMsg = s
15b0b 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
15b0c 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20  b, zErr);.      
15b0d 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20  } else {.       
15b0e 20 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b   v->zErrMsg = 0;
15b0f 0a 20 20 20 20 20 20 20 20 76 2d 3e 72 63 20 3d  .        v->rc =
15b10 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
15b11 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
15b12 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
15b13 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
15b14 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
15b15 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
15b16 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
15b17 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  c;.}.#endif../*.
15b18 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 75  ** Extract the u
15b19 73 65 72 20 64 61 74 61 20 66 72 6f 6d 20 61 20  ser data from a 
15b1a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
15b1b 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 72 65  structure and re
15b1c 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 65  turn a.** pointe
15b1d 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49  r to it..*/.SQLI
15b1e 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
15b1f 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73  ite3_user_data(s
15b20 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
15b21 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20  p){.  assert( p 
15b22 26 26 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20  && p->pFunc );. 
15b23 20 72 65 74 75 72 6e 20 70 2d 3e 70 46 75 6e 63   return p->pFunc
15b24 2d 3e 70 55 73 65 72 44 61 74 61 3b 0a 7d 0a 0a  ->pUserData;.}..
15b25 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68  /*.** Extract th
15b26 65 20 75 73 65 72 20 64 61 74 61 20 66 72 6f 6d  e user data from
15b27 20 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   a sqlite3_conte
15b28 78 74 20 73 74 72 75 63 74 75 72 65 20 61 6e 64  xt structure and
15b29 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69   return a.** poi
15b2a 6e 74 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53  nter to it..*/.S
15b2b 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
15b2c 33 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  3 *sqlite3_conte
15b2d 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c  xt_db_handle(sql
15b2e 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29  ite3_context *p)
15b2f 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26  {.  assert( p &&
15b30 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 72   p->pFunc );.  r
15b31 65 74 75 72 6e 20 70 2d 3e 73 2e 64 62 3b 0a 7d  eturn p->s.db;.}
15b32 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
15b33 6f 77 69 6e 67 20 69 73 20 74 68 65 20 69 6d 70  owing is the imp
15b34 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61  lementation of a
15b35 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74  n SQL function t
15b36 68 61 74 20 61 6c 77 61 79 73 0a 2a 2a 20 66 61  hat always.** fa
15b37 69 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72 6f  ils with an erro
15b38 72 20 6d 65 73 73 61 67 65 20 73 74 61 74 69 6e  r message statin
15b39 67 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74  g that the funct
15b3a 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e 20 74  ion is used in t
15b3b 68 65 0a 2a 2a 20 77 72 6f 6e 67 20 63 6f 6e 74  he.** wrong cont
15b3c 65 78 74 2e 20 20 54 68 65 20 73 71 6c 69 74 65  ext.  The sqlite
15b3d 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74  3_overload_funct
15b3e 69 6f 6e 28 29 20 41 50 49 20 6d 69 67 68 74 20  ion() API might 
15b3f 63 6f 6e 73 74 72 75 63 74 0a 2a 2a 20 53 51 4c  construct.** SQL
15b40 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 75   function that u
15b41 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  se this routine 
15b42 73 6f 20 74 68 61 74 20 74 68 65 20 66 75 6e 63  so that the func
15b43 74 69 6f 6e 73 20 77 69 6c 6c 20 65 78 69 73 74  tions will exist
15b44 0a 2a 2a 20 66 6f 72 20 6e 61 6d 65 20 72 65 73  .** for name res
15b45 6f 6c 75 74 69 6f 6e 20 62 75 74 20 61 72 65 20  olution but are 
15b46 61 63 74 75 61 6c 6c 79 20 6f 76 65 72 6c 6f 61  actually overloa
15b47 64 65 64 20 62 79 20 74 68 65 20 78 46 69 6e 64  ded by the xFind
15b48 46 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 65 74 68  Function.** meth
15b49 6f 64 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61  od of virtual ta
15b4a 62 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  bles..*/.SQLITE_
15b4b 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
15b4c 69 74 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74  ite3InvalidFunct
15b4d 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  ion(.  sqlite3_c
15b4e 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
15b4f 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f    /* The functio
15b50 6e 20 63 61 6c 6c 69 6e 67 20 63 6f 6e 74 65 78  n calling contex
15b51 74 20 2a 2f 0a 20 20 69 6e 74 20 61 72 67 63 2c  t */.  int argc,
15b52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15b53 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
15b54 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20  rguments to the 
15b55 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71  function */.  sq
15b56 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
15b57 67 76 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75  gv       /* Valu
15b58 65 20 6f 66 20 65 61 63 68 20 61 72 67 75 6d 65  e of each argume
15b59 6e 74 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74  nt */.){.  const
15b5a 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 63   char *zName = c
15b5b 6f 6e 74 65 78 74 2d 3e 70 46 75 6e 63 2d 3e 7a  ontext->pFunc->z
15b5c 4e 61 6d 65 3b 0a 20 20 63 68 61 72 20 2a 7a 45  Name;.  char *zE
15b5d 72 72 3b 0a 20 20 7a 45 72 72 20 3d 20 73 71 6c  rr;.  zErr = sql
15b5e 69 74 65 33 4d 50 72 69 6e 74 66 28 30 2c 0a 20  ite3MPrintf(0,. 
15b5f 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20       "unable to 
15b60 75 73 65 20 66 75 6e 63 74 69 6f 6e 20 25 73 20  use function %s 
15b61 69 6e 20 74 68 65 20 72 65 71 75 65 73 74 65 64  in the requested
15b62 20 63 6f 6e 74 65 78 74 22 2c 20 7a 4e 61 6d 65   context", zName
15b63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  );.  sqlite3_res
15b64 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78  ult_error(contex
15b65 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20  t, zErr, -1);.  
15b66 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72  sqlite3_free(zEr
15b67 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  r);.}../*.** All
15b68 6f 63 61 74 65 20 6f 72 20 72 65 74 75 72 6e 20  ocate or return 
15b69 74 68 65 20 61 67 67 72 65 67 61 74 65 20 63 6f  the aggregate co
15b6a 6e 74 65 78 74 20 66 6f 72 20 61 20 75 73 65 72  ntext for a user
15b6b 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 20 6e 65   function.  A ne
15b6c 77 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 20  w.** context is 
15b6d 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 65  allocated on the
15b6e 20 66 69 72 73 74 20 63 61 6c 6c 2e 20 20 53 75   first call.  Su
15b6f 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 72  bsequent calls r
15b70 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 73 61 6d  eturn the.** sam
15b71 65 20 63 6f 6e 74 65 78 74 20 74 68 61 74 20 77  e context that w
15b72 61 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 70  as returned on p
15b73 72 69 6f 72 20 63 61 6c 6c 73 2e 0a 2a 2f 0a 53  rior calls..*/.S
15b74 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
15b75 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
15b76 65 5f 63 6f 6e 74 65 78 74 28 73 71 6c 69 74 65  e_context(sqlite
15b77 33 5f 63 6f 6e 74 65 78 74 20 2a 70 2c 20 69 6e  3_context *p, in
15b78 74 20 6e 42 79 74 65 29 7b 0a 20 20 4d 65 6d 20  t nByte){.  Mem 
15b79 2a 70 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74 28  *pMem;.  assert(
15b7a 20 70 20 26 26 20 70 2d 3e 70 46 75 6e 63 20 26   p && p->pFunc &
15b7b 26 20 70 2d 3e 70 46 75 6e 63 2d 3e 78 53 74 65  & p->pFunc->xSte
15b7c 70 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  p );.  assert( s
15b7d 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
15b7e 64 28 70 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78  d(p->s.db->mutex
15b7f 29 20 29 3b 0a 20 20 70 4d 65 6d 20 3d 20 70 2d  ) );.  pMem = p-
15b80 3e 70 4d 65 6d 3b 0a 20 20 69 66 28 20 28 70 4d  >pMem;.  if( (pM
15b81 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
15b82 41 67 67 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69  Agg)==0 ){.    i
15b83 66 28 20 6e 42 79 74 65 3d 3d 30 20 29 7b 0a 20  f( nByte==0 ){. 
15b84 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
15b85 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e  MemReleaseExtern
15b86 61 6c 28 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20  al(pMem);.      
15b87 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
15b88 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 70 4d  M_Null;.      pM
15b89 65 6d 2d 3e 7a 20 3d 20 30 3b 0a 20 20 20 20 7d  em->z = 0;.    }
15b8a 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
15b8b 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70  te3VdbeMemGrow(p
15b8c 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 30 29 3b 0a  Mem, nByte, 0);.
15b8d 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
15b8e 73 20 3d 20 4d 45 4d 5f 41 67 67 3b 0a 20 20 20  s = MEM_Agg;.   
15b8f 20 20 20 70 4d 65 6d 2d 3e 75 2e 70 44 65 66 20     pMem->u.pDef 
15b90 3d 20 70 2d 3e 70 46 75 6e 63 3b 0a 20 20 20 20  = p->pFunc;.    
15b91 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 20 29 7b    if( pMem->z ){
15b92 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28  .        memset(
15b93 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e 42 79 74  pMem->z, 0, nByt
15b94 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
15b95 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  }.  }.  return (
15b96 76 6f 69 64 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 7d  void*)pMem->z;.}
15b97 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
15b98 68 65 20 61 75 78 69 6c 61 72 79 20 64 61 74 61  he auxilary data
15b99 20 70 6f 69 6e 74 65 72 2c 20 69 66 20 61 6e 79   pointer, if any
15b9a 2c 20 66 6f 72 20 74 68 65 20 69 41 72 67 27 74  , for the iArg't
15b9b 68 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a  h argument to.**
15b9c 20 74 68 65 20 75 73 65 72 2d 66 75 6e 63 74 69   the user-functi
15b9d 6f 6e 20 64 65 66 69 6e 65 64 20 62 79 20 70 43  on defined by pC
15b9e 74 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  tx..*/.SQLITE_AP
15b9f 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  I void *sqlite3_
15ba0 67 65 74 5f 61 75 78 64 61 74 61 28 73 71 6c 69  get_auxdata(sqli
15ba1 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74  te3_context *pCt
15ba2 78 2c 20 69 6e 74 20 69 41 72 67 29 7b 0a 20 20  x, int iArg){.  
15ba3 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46  VdbeFunc *pVdbeF
15ba4 75 6e 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  unc;..  assert( 
15ba5 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
15ba6 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d  ld(pCtx->s.db->m
15ba7 75 74 65 78 29 20 29 3b 0a 20 20 70 56 64 62 65  utex) );.  pVdbe
15ba8 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70 56 64  Func = pCtx->pVd
15ba9 62 65 46 75 6e 63 3b 0a 20 20 69 66 28 20 21 70  beFunc;.  if( !p
15baa 56 64 62 65 46 75 6e 63 20 7c 7c 20 69 41 72 67  VdbeFunc || iArg
15bab 3e 3d 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75  >=pVdbeFunc->nAu
15bac 78 20 7c 7c 20 69 41 72 67 3c 30 20 29 7b 0a 20  x || iArg<0 ){. 
15bad 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
15bae 0a 20 20 72 65 74 75 72 6e 20 70 56 64 62 65 46  .  return pVdbeF
15baf 75 6e 63 2d 3e 61 70 41 75 78 5b 69 41 72 67 5d  unc->apAux[iArg]
15bb0 2e 70 41 75 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  .pAux;.}../*.** 
15bb1 53 65 74 20 74 68 65 20 61 75 78 69 6c 61 72 79  Set the auxilary
15bb2 20 64 61 74 61 20 70 6f 69 6e 74 65 72 20 61 6e   data pointer an
15bb3 64 20 64 65 6c 65 74 65 20 66 75 6e 63 74 69 6f  d delete functio
15bb4 6e 2c 20 66 6f 72 20 74 68 65 20 69 41 72 67 27  n, for the iArg'
15bb5 74 68 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74  th.** argument t
15bb6 6f 20 74 68 65 20 75 73 65 72 2d 66 75 6e 63 74  o the user-funct
15bb7 69 6f 6e 20 64 65 66 69 6e 65 64 20 62 79 20 70  ion defined by p
15bb8 43 74 78 2e 20 41 6e 79 20 70 72 65 76 69 6f 75  Ctx. Any previou
15bb9 73 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 64 65  s value is.** de
15bba 6c 65 74 65 64 20 62 79 20 63 61 6c 6c 69 6e 67  leted by calling
15bbb 20 74 68 65 20 64 65 6c 65 74 65 20 66 75 6e 63   the delete func
15bbc 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20 77  tion specified w
15bbd 68 65 6e 20 69 74 20 77 61 73 20 73 65 74 2e 0a  hen it was set..
15bbe 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
15bbf 69 64 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  id sqlite3_set_a
15bc0 75 78 64 61 74 61 28 0a 20 20 73 71 6c 69 74 65  uxdata(.  sqlite
15bc1 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c  3_context *pCtx,
15bc2 20 0a 20 20 69 6e 74 20 69 41 72 67 2c 20 0a 20   .  int iArg, . 
15bc3 20 76 6f 69 64 20 2a 70 41 75 78 2c 20 0a 20 20   void *pAux, .  
15bc4 76 6f 69 64 20 28 2a 78 44 65 6c 65 74 65 29 28  void (*xDelete)(
15bc5 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 73 74 72 75  void*).){.  stru
15bc6 63 74 20 41 75 78 44 61 74 61 20 2a 70 41 75 78  ct AuxData *pAux
15bc7 44 61 74 61 3b 0a 20 20 56 64 62 65 46 75 6e 63  Data;.  VdbeFunc
15bc8 20 2a 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 69   *pVdbeFunc;.  i
15bc9 66 28 20 69 41 72 67 3c 30 20 29 20 67 6f 74 6f  f( iArg<0 ) goto
15bca 20 66 61 69 6c 65 64 3b 0a 0a 20 20 61 73 73 65   failed;..  asse
15bcb 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
15bcc 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64  x_held(pCtx->s.d
15bcd 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70  b->mutex) );.  p
15bce 56 64 62 65 46 75 6e 63 20 3d 20 70 43 74 78 2d  VdbeFunc = pCtx-
15bcf 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 69 66  >pVdbeFunc;.  if
15bd0 28 20 21 70 56 64 62 65 46 75 6e 63 20 7c 7c 20  ( !pVdbeFunc || 
15bd1 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78 3c  pVdbeFunc->nAux<
15bd2 3d 69 41 72 67 20 29 7b 0a 20 20 20 20 69 6e 74  =iArg ){.    int
15bd3 20 6e 41 75 78 20 3d 20 28 70 56 64 62 65 46 75   nAux = (pVdbeFu
15bd4 6e 63 20 3f 20 70 56 64 62 65 46 75 6e 63 2d 3e  nc ? pVdbeFunc->
15bd5 6e 41 75 78 20 3a 20 30 29 3b 0a 20 20 20 20 69  nAux : 0);.    i
15bd6 6e 74 20 6e 4d 61 6c 6c 6f 63 20 3d 20 73 69 7a  nt nMalloc = siz
15bd7 65 6f 66 28 56 64 62 65 46 75 6e 63 29 20 2b 20  eof(VdbeFunc) + 
15bd8 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 41 75  sizeof(struct Au
15bd9 78 44 61 74 61 29 2a 69 41 72 67 3b 0a 20 20 20  xData)*iArg;.   
15bda 20 70 56 64 62 65 46 75 6e 63 20 3d 20 73 71 6c   pVdbeFunc = sql
15bdb 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 70 43  ite3DbRealloc(pC
15bdc 74 78 2d 3e 73 2e 64 62 2c 20 70 56 64 62 65 46  tx->s.db, pVdbeF
15bdd 75 6e 63 2c 20 6e 4d 61 6c 6c 6f 63 29 3b 0a 20  unc, nMalloc);. 
15bde 20 20 20 69 66 28 20 21 70 56 64 62 65 46 75 6e     if( !pVdbeFun
15bdf 63 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  c ){.      goto 
15be0 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20  failed;.    }.  
15be1 20 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e    pCtx->pVdbeFun
15be2 63 20 3d 20 70 56 64 62 65 46 75 6e 63 3b 0a 20  c = pVdbeFunc;. 
15be3 20 20 20 6d 65 6d 73 65 74 28 26 70 56 64 62 65     memset(&pVdbe
15be4 46 75 6e 63 2d 3e 61 70 41 75 78 5b 6e 41 75 78  Func->apAux[nAux
15be5 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 74 72  ], 0, sizeof(str
15be6 75 63 74 20 41 75 78 44 61 74 61 29 2a 28 69 41  uct AuxData)*(iA
15be7 72 67 2b 31 2d 6e 41 75 78 29 29 3b 0a 20 20 20  rg+1-nAux));.   
15be8 20 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78   pVdbeFunc->nAux
15be9 20 3d 20 69 41 72 67 2b 31 3b 0a 20 20 20 20 70   = iArg+1;.    p
15bea 56 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 20  VdbeFunc->pFunc 
15beb 3d 20 70 43 74 78 2d 3e 70 46 75 6e 63 3b 0a 20  = pCtx->pFunc;. 
15bec 20 7d 0a 0a 20 20 70 41 75 78 44 61 74 61 20 3d   }..  pAuxData =
15bed 20 26 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41   &pVdbeFunc->apA
15bee 75 78 5b 69 41 72 67 5d 3b 0a 20 20 69 66 28 20  ux[iArg];.  if( 
15bef 70 41 75 78 44 61 74 61 2d 3e 70 41 75 78 20 26  pAuxData->pAux &
15bf0 26 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c  & pAuxData->xDel
15bf1 65 74 65 20 29 7b 0a 20 20 20 20 70 41 75 78 44  ete ){.    pAuxD
15bf2 61 74 61 2d 3e 78 44 65 6c 65 74 65 28 70 41 75  ata->xDelete(pAu
15bf3 78 44 61 74 61 2d 3e 70 41 75 78 29 3b 0a 20 20  xData->pAux);.  
15bf4 7d 0a 20 20 70 41 75 78 44 61 74 61 2d 3e 70 41  }.  pAuxData->pA
15bf5 75 78 20 3d 20 70 41 75 78 3b 0a 20 20 70 41 75  ux = pAux;.  pAu
15bf6 78 44 61 74 61 2d 3e 78 44 65 6c 65 74 65 20 3d  xData->xDelete =
15bf7 20 78 44 65 6c 65 74 65 3b 0a 20 20 72 65 74 75   xDelete;.  retu
15bf8 72 6e 3b 0a 0a 66 61 69 6c 65 64 3a 0a 20 20 69  rn;..failed:.  i
15bf9 66 28 20 78 44 65 6c 65 74 65 20 29 7b 0a 20 20  f( xDelete ){.  
15bfa 20 20 78 44 65 6c 65 74 65 28 70 41 75 78 29 3b    xDelete(pAux);
15bfb 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
15bfc 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
15bfd 6f 66 20 74 69 6d 65 73 20 74 68 65 20 53 74 65  of times the Ste
15bfe 70 20 66 75 6e 63 74 69 6f 6e 20 6f 66 20 61 20  p function of a 
15bff 61 67 67 72 65 67 61 74 65 20 68 61 73 20 62 65  aggregate has be
15c00 65 6e 20 0a 2a 2a 20 63 61 6c 6c 65 64 2e 0a 2a  en .** called..*
15c01 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
15c02 6f 6e 20 69 73 20 64 65 70 72 65 63 61 74 65 64  on is deprecated
15c03 2e 20 20 44 6f 20 6e 6f 74 20 75 73 65 20 69 74  .  Do not use it
15c04 20 66 6f 72 20 6e 65 77 20 63 6f 64 65 2e 20 20   for new code.  
15c05 49 74 20 69 73 0a 2a 2a 20 70 72 6f 76 69 64 65  It is.** provide
15c06 20 6f 6e 6c 79 20 74 6f 20 61 76 6f 69 64 20 62   only to avoid b
15c07 72 65 61 6b 69 6e 67 20 6c 65 67 61 63 79 20 63  reaking legacy c
15c08 6f 64 65 2e 20 20 4e 65 77 20 61 67 67 72 65 67  ode.  New aggreg
15c09 61 74 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  ate function.** 
15c0a 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
15c0b 73 68 6f 75 6c 64 20 6b 65 65 70 20 74 68 65 69  should keep thei
15c0c 72 20 6f 77 6e 20 63 6f 75 6e 74 73 20 77 69 74  r own counts wit
15c0d 68 69 6e 20 74 68 65 69 72 20 61 67 67 72 65 67  hin their aggreg
15c0e 61 74 65 0a 2a 2a 20 63 6f 6e 74 65 78 74 2e 0a  ate.** context..
15c0f 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
15c10 74 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67  t sqlite3_aggreg
15c11 61 74 65 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65  ate_count(sqlite
15c12 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b 0a 20  3_context *p){. 
15c13 20 61 73 73 65 72 74 28 20 70 20 26 26 20 70 2d   assert( p && p-
15c14 3e 70 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75  >pFunc && p->pFu
15c15 6e 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 72  nc->xStep );.  r
15c16 65 74 75 72 6e 20 70 2d 3e 70 4d 65 6d 2d 3e 6e  eturn p->pMem->n
15c17 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
15c18 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
15c19 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72  columns in the r
15c1a 65 73 75 6c 74 20 73 65 74 20 66 6f 72 20 74 68  esult set for th
15c1b 65 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d  e statement pStm
15c1c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
15c1d 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c   int sqlite3_col
15c1e 75 6d 6e 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65  umn_count(sqlite
15c1f 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a  3_stmt *pStmt){.
15c20 20 20 56 64 62 65 20 2a 70 56 6d 20 3d 20 28 56    Vdbe *pVm = (V
15c21 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72  dbe *)pStmt;.  r
15c22 65 74 75 72 6e 20 70 56 6d 20 3f 20 70 56 6d 2d  eturn pVm ? pVm-
15c23 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 3a 20 30 3b  >nResColumn : 0;
15c24 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
15c25 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76   the number of v
15c26 61 6c 75 65 73 20 61 76 61 69 6c 61 62 6c 65 20  alues available 
15c27 66 72 6f 6d 20 74 68 65 20 63 75 72 72 65 6e 74  from the current
15c28 20 72 6f 77 20 6f 66 20 74 68 65 0a 2a 2a 20 63   row of the.** c
15c29 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69  urrently executi
15c2a 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74  ng statement pSt
15c2b 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  mt..*/.SQLITE_AP
15c2c 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 61  I int sqlite3_da
15c2d 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33  ta_count(sqlite3
15c2e 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20  _stmt *pStmt){. 
15c2f 20 56 64 62 65 20 2a 70 56 6d 20 3d 20 28 56 64   Vdbe *pVm = (Vd
15c30 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 66  be *)pStmt;.  if
15c31 28 20 70 56 6d 3d 3d 30 20 7c 7c 20 70 56 6d 2d  ( pVm==0 || pVm-
15c32 3e 70 52 65 73 75 6c 74 53 65 74 3d 3d 30 20 29  >pResultSet==0 )
15c33 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74   return 0;.  ret
15c34 75 72 6e 20 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c  urn pVm->nResCol
15c35 75 6d 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43  umn;.}.../*.** C
15c36 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 63  heck to see if c
15c37 6f 6c 75 6d 6e 20 69 43 6f 6c 20 6f 66 20 74 68  olumn iCol of th
15c38 65 20 67 69 76 65 6e 20 73 74 61 74 65 6d 65 6e  e given statemen
15c39 74 20 69 73 20 76 61 6c 69 64 2e 20 20 49 66 0a  t is valid.  If.
15c3a 2a 2a 20 69 74 20 69 73 2c 20 72 65 74 75 72 6e  ** it is, return
15c3b 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
15c3c 65 20 4d 65 6d 20 66 6f 72 20 74 68 65 20 76 61  e Mem for the va
15c3d 6c 75 65 20 6f 66 20 74 68 61 74 20 63 6f 6c 75  lue of that colu
15c3e 6d 6e 2e 0a 2a 2a 20 49 66 20 69 43 6f 6c 20 69  mn..** If iCol i
15c3f 73 20 6e 6f 74 20 76 61 6c 69 64 2c 20 72 65 74  s not valid, ret
15c40 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
15c41 20 61 20 4d 65 6d 20 77 68 69 63 68 20 68 61 73   a Mem which has
15c42 20 61 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 4e   a value.** of N
15c43 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4d  ULL..*/.static M
15c44 65 6d 20 2a 63 6f 6c 75 6d 6e 4d 65 6d 28 73 71  em *columnMem(sq
15c45 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
15c46 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 56 64 62  t, int i){.  Vdb
15c47 65 20 2a 70 56 6d 3b 0a 20 20 69 6e 74 20 76 61  e *pVm;.  int va
15c48 6c 73 3b 0a 20 20 4d 65 6d 20 2a 70 4f 75 74 3b  ls;.  Mem *pOut;
15c49 0a 0a 20 20 70 56 6d 20 3d 20 28 56 64 62 65 20  ..  pVm = (Vdbe 
15c4a 2a 29 70 53 74 6d 74 3b 0a 20 20 69 66 28 20 70  *)pStmt;.  if( p
15c4b 56 6d 20 26 26 20 70 56 6d 2d 3e 70 52 65 73 75  Vm && pVm->pResu
15c4c 6c 74 53 65 74 21 3d 30 20 26 26 20 69 3c 70 56  ltSet!=0 && i<pV
15c4d 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 26 26  m->nResColumn &&
15c4e 20 69 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c   i>=0 ){.    sql
15c4f 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
15c50 28 70 56 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  (pVm->db->mutex)
15c51 3b 0a 20 20 20 20 76 61 6c 73 20 3d 20 73 71 6c  ;.    vals = sql
15c52 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 28  ite3_data_count(
15c53 70 53 74 6d 74 29 3b 0a 20 20 20 20 70 4f 75 74  pStmt);.    pOut
15c54 20 3d 20 26 70 56 6d 2d 3e 70 52 65 73 75 6c 74   = &pVm->pResult
15c55 53 65 74 5b 69 5d 3b 0a 20 20 7d 65 6c 73 65 7b  Set[i];.  }else{
15c56 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73  .    static cons
15c57 74 20 4d 65 6d 20 6e 75 6c 6c 4d 65 6d 20 3d 20  t Mem nullMem = 
15c58 7b 7b 30 7d 2c 20 30 2e 30 2c 20 30 2c 20 22 22  {{0}, 0.0, 0, ""
15c59 2c 20 30 2c 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 53  , 0, MEM_Null, S
15c5a 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20 30 2c 20 30  QLITE_NULL, 0, 0
15c5b 2c 20 30 20 7d 3b 0a 20 20 20 20 69 66 28 20 70  , 0 };.    if( p
15c5c 56 6d 2d 3e 64 62 20 29 7b 0a 20 20 20 20 20 20  Vm->db ){.      
15c5d 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
15c5e 74 65 72 28 70 56 6d 2d 3e 64 62 2d 3e 6d 75 74  ter(pVm->db->mut
15c5f 65 78 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ex);.      sqlit
15c60 65 33 45 72 72 6f 72 28 70 56 6d 2d 3e 64 62 2c  e3Error(pVm->db,
15c61 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 2c 20 30   SQLITE_RANGE, 0
15c62 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f 75  );.    }.    pOu
15c63 74 20 3d 20 28 4d 65 6d 2a 29 26 6e 75 6c 6c 4d  t = (Mem*)&nullM
15c64 65 6d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  em;.  }.  return
15c65 20 70 4f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pOut;.}../*.** 
15c66 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
15c67 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 69 6e   called after in
15c68 76 6f 6b 69 6e 67 20 61 6e 20 73 71 6c 69 74 65  voking an sqlite
15c69 33 5f 76 61 6c 75 65 5f 58 58 58 20 66 75 6e 63  3_value_XXX func
15c6a 74 69 6f 6e 20 6f 6e 20 61 20 0a 2a 2a 20 63 6f  tion on a .** co
15c6b 6c 75 6d 6e 20 76 61 6c 75 65 20 28 69 2e 65 2e  lumn value (i.e.
15c6c 20 61 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65   a value returne
15c6d 64 20 62 79 20 65 76 61 6c 75 61 74 69 6e 67 20  d by evaluating 
15c6e 61 6e 20 53 51 4c 20 65 78 70 72 65 73 73 69 6f  an SQL expressio
15c6f 6e 20 69 6e 20 74 68 65 0a 2a 2a 20 73 65 6c 65  n in the.** sele
15c70 63 74 20 6c 69 73 74 20 6f 66 20 61 20 53 45 4c  ct list of a SEL
15c71 45 43 54 20 73 74 61 74 65 6d 65 6e 74 29 20 74  ECT statement) t
15c72 68 61 74 20 6d 61 79 20 63 61 75 73 65 20 61 20  hat may cause a 
15c73 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65  malloc() failure
15c74 2e 20 49 66 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 28  . If .** malloc(
15c75 29 20 68 61 73 20 66 61 69 6c 65 64 2c 20 74 68  ) has failed, th
15c76 65 20 74 68 72 65 61 64 73 20 6d 61 6c 6c 6f 63  e threads malloc
15c77 46 61 69 6c 65 64 20 66 6c 61 67 20 69 73 20 63  Failed flag is c
15c78 6c 65 61 72 65 64 20 61 6e 64 20 74 68 65 20 72  leared and the r
15c79 65 73 75 6c 74 0a 2a 2a 20 63 6f 64 65 20 6f 66  esult.** code of
15c7a 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74   statement pStmt
15c7b 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4e   set to SQLITE_N
15c7c 4f 4d 45 4d 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 63  OMEM..**.** Spec
15c7d 69 66 69 63 61 6c 6c 79 2c 20 74 68 69 73 20 69  ifically, this i
15c7e 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69  s called from wi
15c7f 74 68 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  thin:.**.**     
15c80 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
15c81 6e 74 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69  nt().**     sqli
15c82 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34  te3_column_int64
15c83 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65  ().**     sqlite
15c84 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 0a  3_column_text().
15c85 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63  **     sqlite3_c
15c86 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 0a 2a  olumn_text16().*
15c87 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f  *     sqlite3_co
15c88 6c 75 6d 6e 5f 72 65 61 6c 28 29 0a 2a 2a 20 20  lumn_real().**  
15c89 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d     sqlite3_colum
15c8a 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20 20 20 20  n_bytes().**    
15c8b 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
15c8c 62 79 74 65 73 31 36 28 29 0a 2a 2a 0a 2a 2a 20  bytes16().**.** 
15c8d 42 75 74 20 6e 6f 74 20 66 6f 72 20 73 71 6c 69  But not for sqli
15c8e 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
15c8f 29 2c 20 77 68 69 63 68 20 6e 65 76 65 72 20 63  ), which never c
15c90 61 6c 6c 73 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a  alls malloc()..*
15c91 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f  /.static void co
15c92 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72  lumnMallocFailur
15c93 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
15c94 70 53 74 6d 74 29 0a 7b 0a 20 20 2f 2a 20 49 66  pStmt).{.  /* If
15c95 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 65 64   malloc() failed
15c96 20 64 75 72 69 6e 67 20 61 6e 20 65 6e 63 6f 64   during an encod
15c97 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 20 77  ing conversion w
15c98 69 74 68 69 6e 20 61 6e 0a 20 20 2a 2a 20 73 71  ithin an.  ** sq
15c99 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 58 58 58  lite3_column_XXX
15c9a 20 41 50 49 2c 20 74 68 65 6e 20 73 65 74 20 74   API, then set t
15c9b 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 6f  he return code o
15c9c 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  f the statement 
15c9d 74 6f 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4e  to.  ** SQLITE_N
15c9e 4f 4d 45 4d 2e 20 54 68 65 20 6e 65 78 74 20 63  OMEM. The next c
15c9f 61 6c 6c 20 74 6f 20 5f 73 74 65 70 28 29 20 28  all to _step() (
15ca0 69 66 20 61 6e 79 29 20 77 69 6c 6c 20 72 65 74  if any) will ret
15ca1 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
15ca2 0a 20 20 2a 2a 20 61 6e 64 20 5f 66 69 6e 61 6c  .  ** and _final
15ca3 69 7a 65 28 29 20 77 69 6c 6c 20 72 65 74 75 72  ize() will retur
15ca4 6e 20 4e 4f 4d 45 4d 2e 0a 20 20 2a 2f 0a 20 20  n NOMEM..  */.  
15ca5 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20  Vdbe *p = (Vdbe 
15ca6 2a 29 70 53 74 6d 74 3b 0a 20 20 69 66 28 20 70  *)pStmt;.  if( p
15ca7 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20   ){.    p->rc = 
15ca8 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 70  sqlite3ApiExit(p
15ca9 2d 3e 64 62 2c 20 70 2d 3e 72 63 29 3b 0a 20 20  ->db, p->rc);.  
15caa 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
15cab 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74  leave(p->db->mut
15cac 65 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a  ex);.  }.}../***
15cad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15cae 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65  ********* sqlite
15caf 33 5f 63 6f 6c 75 6d 6e 5f 20 20 2a 2a 2a 2a 2a  3_column_  *****
15cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15cb1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
15cb2 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74  e following rout
15cb3 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f  ines are used to
15cb4 20 61 63 63 65 73 73 20 65 6c 65 6d 65 6e 74 73   access elements
15cb5 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   of the current 
15cb6 72 6f 77 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65  row.** in the re
15cb7 73 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c  sult set..*/.SQL
15cb8 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
15cb9 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  id *sqlite3_colu
15cba 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f  mn_blob(sqlite3_
15cbb 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
15cbc 20 69 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69   i){.  const voi
15cbd 64 20 2a 76 61 6c 3b 0a 20 20 76 61 6c 20 3d 20  d *val;.  val = 
15cbe 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
15cbf 6f 62 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53  ob( columnMem(pS
15cc0 74 6d 74 2c 69 29 20 29 3b 0a 20 20 2f 2a 20 45  tmt,i) );.  /* E
15cc1 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 72 65  ven though there
15cc2 20 69 73 20 6e 6f 20 65 6e 63 6f 64 69 6e 67 20   is no encoding 
15cc3 63 6f 6e 76 65 72 73 69 6f 6e 2c 20 76 61 6c 75  conversion, valu
15cc4 65 5f 62 6c 6f 62 28 29 20 6d 69 67 68 74 0a 20  e_blob() might. 
15cc5 20 2a 2a 20 6e 65 65 64 20 74 6f 20 63 61 6c 6c   ** need to call
15cc6 20 6d 61 6c 6c 6f 63 28 29 20 74 6f 20 65 78 70   malloc() to exp
15cc7 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 20 6f  and the result o
15cc8 66 20 61 20 7a 65 72 6f 62 6c 6f 62 28 29 20 0a  f a zeroblob() .
15cc9 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e    ** expression.
15cca 20 0a 20 20 2a 2f 0a 20 20 63 6f 6c 75 6d 6e 4d   .  */.  columnM
15ccb 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74  allocFailure(pSt
15ccc 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61  mt);.  return va
15ccd 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  l;.}.SQLITE_API 
15cce 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  int sqlite3_colu
15ccf 6d 6e 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33  mn_bytes(sqlite3
15cd0 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
15cd1 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61 6c 20  t i){.  int val 
15cd2 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
15cd3 62 79 74 65 73 28 20 63 6f 6c 75 6d 6e 4d 65 6d  bytes( columnMem
15cd4 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63  (pStmt,i) );.  c
15cd5 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75  olumnMallocFailu
15cd6 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74  re(pStmt);.  ret
15cd7 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54  urn val;.}.SQLIT
15cd8 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
15cd9 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36  3_column_bytes16
15cda 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
15cdb 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20  Stmt, int i){.  
15cdc 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 65  int val = sqlite
15cdd 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28  3_value_bytes16(
15cde 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74   columnMem(pStmt
15cdf 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d  ,i) );.  columnM
15ce0 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74  allocFailure(pSt
15ce1 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61  mt);.  return va
15ce2 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  l;.}.SQLITE_API 
15ce3 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 5f 63  double sqlite3_c
15ce4 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 73 71 6c  olumn_double(sql
15ce5 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
15ce6 2c 20 69 6e 74 20 69 29 7b 0a 20 20 64 6f 75 62  , int i){.  doub
15ce7 6c 65 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33  le val = sqlite3
15ce8 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 20 63  _value_double( c
15ce9 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69  olumnMem(pStmt,i
15cea 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c  ) );.  columnMal
15ceb 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74  locFailure(pStmt
15cec 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b  );.  return val;
15ced 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  .}.SQLITE_API in
15cee 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  t sqlite3_column
15cef 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d  _int(sqlite3_stm
15cf0 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
15cf1 7b 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 71  {.  int val = sq
15cf2 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28  lite3_value_int(
15cf3 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74   columnMem(pStmt
15cf4 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d  ,i) );.  columnM
15cf5 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74  allocFailure(pSt
15cf6 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61  mt);.  return va
15cf7 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  l;.}.SQLITE_API 
15cf8 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 6c  sqlite_int64 sql
15cf9 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36  ite3_column_int6
15cfa 34 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  4(sqlite3_stmt *
15cfb 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20  pStmt, int i){. 
15cfc 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 76 61   sqlite_int64 va
15cfd 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  l = sqlite3_valu
15cfe 65 5f 69 6e 74 36 34 28 20 63 6f 6c 75 6d 6e 4d  e_int64( columnM
15cff 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20  em(pStmt,i) );. 
15d00 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69   columnMallocFai
15d01 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72  lure(pStmt);.  r
15d02 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c  eturn val;.}.SQL
15d03 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 75 6e  ITE_API const un
15d04 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 71 6c  signed char *sql
15d05 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
15d06 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
15d07 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20  Stmt, int i){.  
15d08 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
15d09 68 61 72 20 2a 76 61 6c 20 3d 20 73 71 6c 69 74  har *val = sqlit
15d0a 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 20 63  e3_value_text( c
15d0b 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69  olumnMem(pStmt,i
15d0c 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c  ) );.  columnMal
15d0d 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74  locFailure(pStmt
15d0e 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b  );.  return val;
15d0f 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  .}.SQLITE_API sq
15d10 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c  lite3_value *sql
15d11 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75  ite3_column_valu
15d12 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
15d13 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20  pStmt, int i){. 
15d14 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
15d15 70 4f 75 74 20 3d 20 63 6f 6c 75 6d 6e 4d 65 6d  pOut = columnMem
15d16 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20 63 6f  (pStmt, i);.  co
15d17 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72  lumnMallocFailur
15d18 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75  e(pStmt);.  retu
15d19 72 6e 20 70 4f 75 74 3b 0a 7d 0a 23 69 66 6e 64  rn pOut;.}.#ifnd
15d1a 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
15d1b 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20  TF16.SQLITE_API 
15d1c 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
15d1d 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31  te3_column_text1
15d1e 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  6(sqlite3_stmt *
15d1f 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20  pStmt, int i){. 
15d20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 76 61 6c   const void *val
15d21 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
15d22 5f 74 65 78 74 31 36 28 20 63 6f 6c 75 6d 6e 4d  _text16( columnM
15d23 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20  em(pStmt,i) );. 
15d24 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69   columnMallocFai
15d25 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72  lure(pStmt);.  r
15d26 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 23 65 6e  eturn val;.}.#en
15d27 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
15d28 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49  IT_UTF16 */.SQLI
15d29 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
15d2a 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73  e3_column_type(s
15d2b 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
15d2c 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e  mt, int i){.  in
15d2d 74 20 69 54 79 70 65 20 3d 20 73 71 6c 69 74 65  t iType = sqlite
15d2e 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 20 63 6f  3_value_type( co
15d2f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29  lumnMem(pStmt,i)
15d30 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c   );.  columnMall
15d31 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29  ocFailure(pStmt)
15d32 3b 0a 20 20 72 65 74 75 72 6e 20 69 54 79 70 65  ;.  return iType
15d33 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c  ;.}../* The foll
15d34 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69  owing function i
15d35 73 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 61  s experimental a
15d36 6e 64 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68  nd subject to ch
15d37 61 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76  ange or.** remov
15d38 61 6c 20 2a 2f 0a 2f 2a 69 6e 74 20 73 71 6c 69  al */./*int sqli
15d39 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 75 6d 65 72  te3_column_numer
15d3a 69 63 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f  ic_type(sqlite3_
15d3b 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
15d3c 20 69 29 7b 0a 2a 2a 20 20 72 65 74 75 72 6e 20   i){.**  return 
15d3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75  sqlite3_value_nu
15d3e 6d 65 72 69 63 5f 74 79 70 65 28 20 63 6f 6c 75  meric_type( colu
15d3f 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29  mnMem(pStmt,i) )
15d40 3b 0a 2a 2a 7d 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  ;.**}.*/../*.** 
15d41 43 6f 6e 76 65 72 74 20 74 68 65 20 4e 2d 74 68  Convert the N-th
15d42 20 65 6c 65 6d 65 6e 74 20 6f 66 20 70 53 74 6d   element of pStm
15d43 74 2d 3e 70 43 6f 6c 4e 61 6d 65 5b 5d 20 69 6e  t->pColName[] in
15d44 74 6f 20 61 20 73 74 72 69 6e 67 20 75 73 69 6e  to a string usin
15d45 67 0a 2a 2a 20 78 46 75 6e 63 28 29 20 74 68 65  g.** xFunc() the
15d46 6e 20 72 65 74 75 72 6e 20 74 68 61 74 20 73 74  n return that st
15d47 72 69 6e 67 2e 20 20 49 66 20 4e 20 69 73 20 6f  ring.  If N is o
15d48 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 72 65 74  ut of range, ret
15d49 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  urn 0..**.** The
15d4a 72 65 20 61 72 65 20 75 70 20 74 6f 20 35 20 6e  re are up to 5 n
15d4b 61 6d 65 73 20 66 6f 72 20 65 61 63 68 20 63 6f  ames for each co
15d4c 6c 75 6d 6e 2e 20 20 75 73 65 54 79 70 65 20 64  lumn.  useType d
15d4d 65 74 65 72 6d 69 6e 65 73 20 77 68 69 63 68 0a  etermines which.
15d4e 2a 2a 20 6e 61 6d 65 20 69 73 20 72 65 74 75 72  ** name is retur
15d4f 6e 65 64 2e 20 20 48 65 72 65 20 61 72 65 20 74  ned.  Here are t
15d50 68 65 20 6e 61 6d 65 73 3a 0a 2a 2a 0a 2a 2a 20  he names:.**.** 
15d51 20 20 20 30 20 20 20 20 20 20 54 68 65 20 63 6f     0      The co
15d52 6c 75 6d 6e 20 6e 61 6d 65 20 61 73 20 69 74 20  lumn name as it 
15d53 73 68 6f 75 6c 64 20 62 65 20 64 69 73 70 6c 61  should be displa
15d54 79 65 64 20 66 6f 72 20 6f 75 74 70 75 74 0a 2a  yed for output.*
15d55 2a 20 20 20 20 31 20 20 20 20 20 20 54 68 65 20  *    1      The 
15d56 64 61 74 61 74 79 70 65 20 6e 61 6d 65 20 66 6f  datatype name fo
15d57 72 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20  r the column.** 
15d58 20 20 20 32 20 20 20 20 20 20 54 68 65 20 6e 61     2      The na
15d59 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
15d5a 73 65 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75  se that the colu
15d5b 6d 6e 20 64 65 72 69 76 65 73 20 66 72 6f 6d 0a  mn derives from.
15d5c 2a 2a 20 20 20 20 33 20 20 20 20 20 20 54 68 65  **    3      The
15d5d 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   name of the tab
15d5e 6c 65 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75  le that the colu
15d5f 6d 6e 20 64 65 72 69 76 65 73 20 66 72 6f 6d 0a  mn derives from.
15d60 2a 2a 20 20 20 20 34 20 20 20 20 20 20 54 68 65  **    4      The
15d61 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   name of the tab
15d62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74  le column that t
15d63 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  he result column
15d64 20 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a   derives from.**
15d65 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c  .** If the resul
15d66 74 20 69 73 20 6e 6f 74 20 61 20 73 69 6d 70 6c  t is not a simpl
15d67 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e  e column referen
15d68 63 65 20 28 69 66 20 69 74 20 69 73 20 61 6e 20  ce (if it is an 
15d69 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 6f 72  expression.** or
15d6a 20 61 20 63 6f 6e 73 74 61 6e 74 29 20 74 68 65   a constant) the
15d6b 6e 20 75 73 65 54 79 70 65 73 20 32 2c 20 33 2c  n useTypes 2, 3,
15d6c 20 61 6e 64 20 34 20 72 65 74 75 72 6e 20 4e 55   and 4 return NU
15d6d 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f  LL..*/.static co
15d6e 6e 73 74 20 76 6f 69 64 20 2a 63 6f 6c 75 6d 6e  nst void *column
15d6f 4e 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  Name(.  sqlite3_
15d70 73 74 6d 74 20 2a 70 53 74 6d 74 2c 0a 20 20 69  stmt *pStmt,.  i
15d71 6e 74 20 4e 2c 0a 20 20 63 6f 6e 73 74 20 76 6f  nt N,.  const vo
15d72 69 64 20 2a 28 2a 78 46 75 6e 63 29 28 4d 65 6d  id *(*xFunc)(Mem
15d73 2a 29 2c 0a 20 20 69 6e 74 20 75 73 65 54 79 70  *),.  int useTyp
15d74 65 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69  e.){.  const voi
15d75 64 20 2a 72 65 74 20 3d 20 30 3b 0a 20 20 56 64  d *ret = 0;.  Vd
15d76 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29  be *p = (Vdbe *)
15d77 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 6e 3b 0a  pStmt;.  int n;.
15d78 20 20 0a 0a 20 20 69 66 28 20 70 21 3d 30 20 29    ..  if( p!=0 )
15d79 7b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65  {.    n = sqlite
15d7a 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 70  3_column_count(p
15d7b 53 74 6d 74 29 3b 0a 20 20 20 20 69 66 28 20 4e  Stmt);.    if( N
15d7c 3c 6e 20 26 26 20 4e 3e 3d 30 20 29 7b 0a 20 20  <n && N>=0 ){.  
15d7d 20 20 20 20 4e 20 2b 3d 20 75 73 65 54 79 70 65      N += useType
15d7e 2a 6e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  *n;.      sqlite
15d7f 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d  3_mutex_enter(p-
15d80 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  >db->mutex);.   
15d81 20 20 20 72 65 74 20 3d 20 78 46 75 6e 63 28 26     ret = xFunc(&
15d82 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b 4e 5d 29 3b  p->aColName[N]);
15d83 0a 0a 20 20 20 20 20 20 2f 2a 20 41 20 6d 61 6c  ..      /* A mal
15d84 6c 6f 63 20 6d 61 79 20 68 61 76 65 20 66 61 69  loc may have fai
15d85 6c 65 64 20 69 6e 73 69 64 65 20 6f 66 20 74 68  led inside of th
15d86 65 20 78 46 75 6e 63 28 29 20 63 61 6c 6c 2e 20  e xFunc() call. 
15d87 49 66 20 74 68 69 73 0a 20 20 20 20 20 20 2a 2a  If this.      **
15d88 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 63 6c   is the case, cl
15d89 65 61 72 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61  ear the mallocFa
15d8a 69 6c 65 64 20 66 6c 61 67 20 61 6e 64 20 72 65  iled flag and re
15d8b 74 75 72 6e 20 4e 55 4c 4c 2e 0a 20 20 20 20 20  turn NULL..     
15d8c 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 2d   */.      if( p-
15d8d 3e 64 62 20 26 26 20 70 2d 3e 64 62 2d 3e 6d 61  >db && p->db->ma
15d8e 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
15d8f 20 20 20 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c        p->db->mal
15d90 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20  locFailed = 0;. 
15d91 20 20 20 20 20 20 20 72 65 74 20 3d 20 30 3b 0a         ret = 0;.
15d92 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
15d93 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
15d94 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  e(p->db->mutex);
15d95 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
15d96 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  urn ret;.}../*.*
15d97 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d  * Return the nam
15d98 65 20 6f 66 20 74 68 65 20 4e 74 68 20 63 6f 6c  e of the Nth col
15d99 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 75 6c  umn of the resul
15d9a 74 20 73 65 74 20 72 65 74 75 72 6e 65 64 20 62  t set returned b
15d9b 79 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65  y SQL.** stateme
15d9c 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c  nt pStmt..*/.SQL
15d9d 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
15d9e 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ar *sqlite3_colu
15d9f 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f  mn_name(sqlite3_
15da0 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
15da1 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f   N){.  return co
15da2 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20  lumnName(.      
15da3 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74  pStmt, N, (const
15da4 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29   void*(*)(Mem*))
15da5 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
15da6 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45  xt, COLNAME_NAME
15da7 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  );.}.#ifndef SQL
15da8 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53  ITE_OMIT_UTF16.S
15da9 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
15daa 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f  void *sqlite3_co
15dab 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69  lumn_name16(sqli
15dac 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
15dad 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72   int N){.  retur
15dae 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20  n columnName(.  
15daf 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63      pStmt, N, (c
15db0 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65  onst void*(*)(Me
15db1 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  m*))sqlite3_valu
15db2 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d  e_text16, COLNAM
15db3 45 5f 4e 41 4d 45 29 3b 0a 7d 0a 23 65 6e 64 69  E_NAME);.}.#endi
15db4 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 61  f../*.** Constra
15db5 69 6e 74 3a 20 20 49 66 20 79 6f 75 20 68 61 76  int:  If you hav
15db6 65 20 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f  e ENABLE_COLUMN_
15db7 4d 45 54 41 44 41 54 41 20 74 68 65 6e 20 79 6f  METADATA then yo
15db8 75 20 6d 75 73 74 0a 2a 2a 20 6e 6f 74 20 64 65  u must.** not de
15db9 66 69 6e 65 20 4f 4d 49 54 5f 44 45 43 4c 54 59  fine OMIT_DECLTY
15dba 50 45 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e  PE..*/.#if defin
15dbb 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ed(SQLITE_OMIT_D
15dbc 45 43 4c 54 59 50 45 29 20 26 26 20 64 65 66 69  ECLTYPE) && defi
15dbd 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
15dbe 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54  E_COLUMN_METADAT
15dbf 41 29 0a 23 20 65 72 72 6f 72 20 22 4d 75 73 74  A).# error "Must
15dc0 20 6e 6f 74 20 64 65 66 69 6e 65 20 62 6f 74 68   not define both
15dc1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43   SQLITE_OMIT_DEC
15dc2 4c 54 59 50 45 20 5c 0a 20 20 20 20 20 20 20 20  LTYPE \.        
15dc3 20 61 6e 64 20 53 51 4c 49 54 45 5f 45 4e 41 42   and SQLITE_ENAB
15dc4 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41  LE_COLUMN_METADA
15dc5 54 41 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  TA".#endif..#ifn
15dc6 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
15dc7 44 45 43 4c 54 59 50 45 0a 2f 2a 0a 2a 2a 20 52  DECLTYPE./*.** R
15dc8 65 74 75 72 6e 20 74 68 65 20 63 6f 6c 75 6d 6e  eturn the column
15dc9 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70   declaration typ
15dca 65 20 28 69 66 20 61 70 70 6c 69 63 61 62 6c 65  e (if applicable
15dcb 29 20 6f 66 20 74 68 65 20 27 69 27 74 68 20 63  ) of the 'i'th c
15dcc 6f 6c 75 6d 6e 0a 2a 2a 20 6f 66 20 74 68 65 20  olumn.** of the 
15dcd 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 53 51  result set of SQ
15dce 4c 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d  L statement pStm
15dcf 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
15dd0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
15dd1 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c  ite3_column_decl
15dd2 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  type(sqlite3_stm
15dd3 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29  t *pStmt, int N)
15dd4 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d  {.  return colum
15dd5 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74  nName(.      pSt
15dd6 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f  mt, N, (const vo
15dd7 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c  id*(*)(Mem*))sql
15dd8 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c  ite3_value_text,
15dd9 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 50   COLNAME_DECLTYP
15dda 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51  E);.}.#ifndef SQ
15ddb 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
15ddc 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
15ddd 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63   void *sqlite3_c
15dde 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36  olumn_decltype16
15ddf 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
15de0 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20  Stmt, int N){.  
15de1 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d  return columnNam
15de2 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20  e(.      pStmt, 
15de3 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28  N, (const void*(
15de4 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33  *)(Mem*))sqlite3
15de5 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43  _value_text16, C
15de6 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 29  OLNAME_DECLTYPE)
15de7 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
15de8 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
15de9 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  */.#endif /* SQL
15dea 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50  ITE_OMIT_DECLTYP
15deb 45 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c  E */..#ifdef SQL
15dec 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d  ITE_ENABLE_COLUM
15ded 4e 5f 4d 45 54 41 44 41 54 41 0a 2f 2a 0a 2a 2a  N_METADATA./*.**
15dee 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65   Return the name
15def 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
15df0 20 66 72 6f 6d 20 77 68 69 63 68 20 61 20 72 65   from which a re
15df1 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69  sult column deri
15df2 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20  ves..** NULL is 
15df3 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20  returned if the 
15df4 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73  result column is
15df5 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f   an expression o
15df6 72 20 63 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a  r constant or.**
15df7 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 77   anything else w
15df8 68 69 63 68 20 69 73 20 6e 6f 74 20 61 6e 20 75  hich is not an u
15df9 6e 61 62 69 67 75 6f 75 73 20 72 65 66 65 72 65  nabiguous refere
15dfa 6e 63 65 20 74 6f 20 61 20 64 61 74 61 62 61 73  nce to a databas
15dfb 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c  e column..*/.SQL
15dfc 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
15dfd 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ar *sqlite3_colu
15dfe 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
15dff 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
15e00 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20  Stmt, int N){.  
15e01 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d  return columnNam
15e02 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20  e(.      pStmt, 
15e03 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28  N, (const void*(
15e04 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33  *)(Mem*))sqlite3
15e05 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c  _value_text, COL
15e06 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 29 3b 0a  NAME_DATABASE);.
15e07 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
15e08 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49  _OMIT_UTF16.SQLI
15e09 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
15e0a 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  d *sqlite3_colum
15e0b 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31  n_database_name1
15e0c 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  6(sqlite3_stmt *
15e0d 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20  pStmt, int N){. 
15e0e 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61   return columnNa
15e0f 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c  me(.      pStmt,
15e10 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a   N, (const void*
15e11 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65  (*)(Mem*))sqlite
15e12 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20  3_value_text16, 
15e13 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45  COLNAME_DATABASE
15e14 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  );.}.#endif /* S
15e15 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
15e16 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72   */../*.** Retur
15e17 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  n the name of th
15e18 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69  e table from whi
15e19 63 68 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75  ch a result colu
15e1a 6d 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e  mn derives..** N
15e1b 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20  ULL is returned 
15e1c 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f  if the result co
15e1d 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65  lumn is an expre
15e1e 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e  ssion or constan
15e1f 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67  t or.** anything
15e20 20 65 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e   else which is n
15e21 6f 74 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73  ot an unabiguous
15e22 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20   reference to a 
15e23 64 61 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e  database column.
15e24 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
15e25 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
15e26 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f  e3_column_table_
15e27 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  name(sqlite3_stm
15e28 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29  t *pStmt, int N)
15e29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d  {.  return colum
15e2a 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74  nName(.      pSt
15e2b 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f  mt, N, (const vo
15e2c 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c  id*(*)(Mem*))sql
15e2d 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c  ite3_value_text,
15e2e 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 29 3b   COLNAME_TABLE);
15e2f 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  .}.#ifndef SQLIT
15e30 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c  E_OMIT_UTF16.SQL
15e31 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
15e32 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  id *sqlite3_colu
15e33 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28  mn_table_name16(
15e34 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
15e35 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72  tmt, int N){.  r
15e36 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65  eturn columnName
15e37 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e  (.      pStmt, N
15e38 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a  , (const void*(*
15e39 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f  )(Mem*))sqlite3_
15e3a 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f  value_text16, CO
15e3b 4c 4e 41 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a  LNAME_TABLE);.}.
15e3c 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
15e3d 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a  _OMIT_UTF16 */..
15e3e 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
15e3f 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   name of the tab
15e40 6c 65 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 77  le column from w
15e41 68 69 63 68 20 61 20 72 65 73 75 6c 74 20 63 6f  hich a result co
15e42 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a  lumn derives..**
15e43 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65   NULL is returne
15e44 64 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 20  d if the result 
15e45 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78 70  column is an exp
15e46 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74  ression or const
15e47 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69  ant or.** anythi
15e48 6e 67 20 65 6c 73 65 20 77 68 69 63 68 20 69 73  ng else which is
15e49 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69 67 75 6f   not an unabiguo
15e4a 75 73 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20  us reference to 
15e4b 61 20 64 61 74 61 62 61 73 65 20 63 6f 6c 75 6d  a database colum
15e4c 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  n..*/.SQLITE_API
15e4d 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
15e4e 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67  ite3_column_orig
15e4f 69 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f  in_name(sqlite3_
15e50 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
15e51 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f   N){.  return co
15e52 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20  lumnName(.      
15e53 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74  pStmt, N, (const
15e54 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29   void*(*)(Mem*))
15e55 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
15e56 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55  xt, COLNAME_COLU
15e57 4d 4e 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53  MN);.}.#ifndef S
15e58 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
15e59 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
15e5a 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
15e5b 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61  column_origin_na
15e5c 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d  me16(sqlite3_stm
15e5d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29  t *pStmt, int N)
15e5e 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d  {.  return colum
15e5f 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74  nName(.      pSt
15e60 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f  mt, N, (const vo
15e61 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c  id*(*)(Mem*))sql
15e62 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
15e63 36 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d  6, COLNAME_COLUM
15e64 4e 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  N);.}.#endif /* 
15e65 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
15e66 36 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53  6 */.#endif /* S
15e67 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c  QLITE_ENABLE_COL
15e68 55 4d 4e 5f 4d 45 54 41 44 41 54 41 20 2a 2f 0a  UMN_METADATA */.
15e69 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
15e6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15e6b 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ** sqlite3_bind_
15e6c 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a    **************
15e6d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
15e6e 20 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 75 73   .** Routines us
15e6f 65 64 20 74 6f 20 61 74 74 61 63 68 20 76 61 6c  ed to attach val
15e70 75 65 73 20 74 6f 20 77 69 6c 64 63 61 72 64 73  ues to wildcards
15e71 20 69 6e 20 61 20 63 6f 6d 70 69 6c 65 64 20 53   in a compiled S
15e72 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f  QL statement..*/
15e73 0a 2f 2a 0a 2a 2a 20 55 6e 62 69 6e 64 20 74 68  ./*.** Unbind th
15e74 65 20 76 61 6c 75 65 20 62 6f 75 6e 64 20 74 6f  e value bound to
15e75 20 76 61 72 69 61 62 6c 65 20 69 20 69 6e 20 76   variable i in v
15e76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 70  irtual machine p
15e77 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 0a 2a  . This is the .*
15e78 2a 20 74 68 65 20 73 61 6d 65 20 61 73 20 62 69  * the same as bi
15e79 6e 64 69 6e 67 20 61 20 4e 55 4c 4c 20 76 61 6c  nding a NULL val
15e7a 75 65 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e  ue to the column
15e7b 2e 20 49 66 20 74 68 65 20 22 69 22 20 70 61 72  . If the "i" par
15e7c 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 6f 75 74  ameter is.** out
15e7d 20 6f 66 20 72 61 6e 67 65 2c 20 74 68 65 6e 20   of range, then 
15e7e 53 51 4c 49 54 45 5f 52 41 4e 47 45 20 69 73 20  SQLITE_RANGE is 
15e7f 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 77 69  returned. Othewi
15e80 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a  se SQLITE_OK..**
15e81 0a 2a 2a 20 54 68 65 20 65 72 72 6f 72 20 63 6f  .** The error co
15e82 64 65 20 73 74 6f 72 65 64 20 69 6e 20 64 61 74  de stored in dat
15e83 61 62 61 73 65 20 70 2d 3e 64 62 20 69 73 20 6f  abase p->db is o
15e84 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 20  verwritten with 
15e85 74 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61  the return.** va
15e86 6c 75 65 20 69 6e 20 61 6e 79 20 63 61 73 65 2e  lue in any case.
15e87 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76  .*/.static int v
15e88 64 62 65 55 6e 62 69 6e 64 28 56 64 62 65 20 2a  dbeUnbind(Vdbe *
15e89 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 4d 65 6d  p, int i){.  Mem
15e8a 20 2a 70 56 61 72 3b 0a 20 20 69 66 28 20 70 3d   *pVar;.  if( p=
15e8b 3d 30 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 21 3d  =0 || p->magic!=
15e8c 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 7c  VDBE_MAGIC_RUN |
15e8d 7c 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20  | p->pc>=0 ){.  
15e8e 20 20 69 66 28 20 70 20 29 20 73 71 6c 69 74 65    if( p ) sqlite
15e8f 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51  3Error(p->db, SQ
15e90 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 30 29 3b  LITE_MISUSE, 0);
15e91 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
15e92 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20  TE_MISUSE;.  }. 
15e93 20 69 66 28 20 69 3c 31 20 7c 7c 20 69 3e 70 2d   if( i<1 || i>p-
15e94 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 73 71 6c  >nVar ){.    sql
15e95 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c  ite3Error(p->db,
15e96 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 2c 20 30   SQLITE_RANGE, 0
15e97 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
15e98 4c 49 54 45 5f 52 41 4e 47 45 3b 0a 20 20 7d 0a  LITE_RANGE;.  }.
15e99 20 20 69 2d 2d 3b 0a 20 20 70 56 61 72 20 3d 20    i--;.  pVar = 
15e9a 26 70 2d 3e 61 56 61 72 5b 69 5d 3b 0a 20 20 73  &p->aVar[i];.  s
15e9b 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
15e9c 65 61 73 65 28 70 56 61 72 29 3b 0a 20 20 70 56  ease(pVar);.  pV
15e9d 61 72 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  ar->flags = MEM_
15e9e 4e 75 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 45  Null;.  sqlite3E
15e9f 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49  rror(p->db, SQLI
15ea0 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 72 65 74  TE_OK, 0);.  ret
15ea1 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
15ea2 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 20 61 20 74  ../*.** Bind a t
15ea3 65 78 74 20 6f 72 20 42 4c 4f 42 20 76 61 6c 75  ext or BLOB valu
15ea4 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
15ea5 20 62 69 6e 64 54 65 78 74 28 0a 20 20 73 71 6c   bindText(.  sql
15ea6 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
15ea7 2c 20 20 20 2f 2a 20 54 68 65 20 73 74 61 74 65  ,   /* The state
15ea8 6d 65 6e 74 20 74 6f 20 62 69 6e 64 20 61 67 61  ment to bind aga
15ea9 69 6e 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 2c  inst */.  int i,
15eaa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15eab 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65   /* Index of the
15eac 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62 69   parameter to bi
15ead 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  nd */.  const vo
15eae 69 64 20 2a 7a 44 61 74 61 2c 20 20 20 20 20 2f  id *zData,     /
15eaf 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  * Pointer to the
15eb0 20 64 61 74 61 20 74 6f 20 62 65 20 62 6f 75 6e   data to be boun
15eb1 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61  d */.  int nData
15eb2 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
15eb3 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
15eb4 20 6f 66 20 64 61 74 61 20 74 6f 20 62 65 20 62   of data to be b
15eb5 6f 75 6e 64 20 2a 2f 0a 20 20 76 6f 69 64 20 28  ound */.  void (
15eb6 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 2c 20 20  *xDel)(void*),  
15eb7 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66   /* Destructor f
15eb8 6f 72 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20  or the data */. 
15eb9 20 69 6e 74 20 65 6e 63 6f 64 69 6e 67 20 20 20   int encoding   
15eba 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64          /* Encod
15ebb 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74 61  ing for the data
15ebc 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 70   */.){.  Vdbe *p
15ebd 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74   = (Vdbe *)pStmt
15ebe 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 20  ;.  Mem *pVar;. 
15ebf 20 69 6e 74 20 72 63 3b 0a 0a 20 20 69 66 28 20   int rc;..  if( 
15ec0 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  p==0 ){.    retu
15ec1 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  rn SQLITE_MISUSE
15ec2 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
15ec3 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64  mutex_enter(p->d
15ec4 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 63 20  b->mutex);.  rc 
15ec5 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20  = vdbeUnbind(p, 
15ec6 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  i);.  if( rc==SQ
15ec7 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 44 61 74 61  LITE_OK && zData
15ec8 21 3d 30 20 29 7b 0a 20 20 20 20 70 56 61 72 20  !=0 ){.    pVar 
15ec9 3d 20 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 3b  = &p->aVar[i-1];
15eca 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
15ecb 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70  3VdbeMemSetStr(p
15ecc 56 61 72 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74  Var, zData, nDat
15ecd 61 2c 20 65 6e 63 6f 64 69 6e 67 2c 20 78 44 65  a, encoding, xDe
15ece 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  l);.    if( rc==
15ecf 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 6e 63  SQLITE_OK && enc
15ed0 6f 64 69 6e 67 21 3d 30 20 29 7b 0a 20 20 20 20  oding!=0 ){.    
15ed1 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
15ed2 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67  beChangeEncoding
15ed3 28 70 56 61 72 2c 20 45 4e 43 28 70 2d 3e 64 62  (pVar, ENC(p->db
15ed4 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  ));.    }.    sq
15ed5 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62  lite3Error(p->db
15ed6 2c 20 72 63 2c 20 30 29 3b 0a 20 20 20 20 72 63  , rc, 0);.    rc
15ed7 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
15ed8 74 28 70 2d 3e 64 62 2c 20 72 63 29 3b 0a 20 20  t(p->db, rc);.  
15ed9 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
15eda 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d  x_leave(p->db->m
15edb 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
15edc 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 42 69  rc;.}.../*.** Bi
15edd 6e 64 20 61 20 62 6c 6f 62 20 76 61 6c 75 65 20  nd a blob value 
15ede 74 6f 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d  to an SQL statem
15edf 65 6e 74 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2f  ent variable..*/
15ee0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
15ee1 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f  sqlite3_bind_blo
15ee2 62 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  b(.  sqlite3_stm
15ee3 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74  t *pStmt, .  int
15ee4 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69   i, .  const voi
15ee5 64 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74  d *zData, .  int
15ee6 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20   nData, .  void 
15ee7 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29  (*xDel)(void*).)
15ee8 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54  {.  return bindT
15ee9 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44  ext(pStmt, i, zD
15eea 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c  ata, nData, xDel
15eeb 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  , 0);.}.SQLITE_A
15eec 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
15eed 69 6e 64 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74  ind_double(sqlit
15eee 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
15eef 69 6e 74 20 69 2c 20 64 6f 75 62 6c 65 20 72 56  int i, double rV
15ef0 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b  alue){.  int rc;
15ef1 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64  .  Vdbe *p = (Vd
15ef2 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 73 71  be *)pStmt;.  sq
15ef3 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
15ef4 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  r(p->db->mutex);
15ef5 0a 20 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69  .  rc = vdbeUnbi
15ef6 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20  nd(p, i);.  if( 
15ef7 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
15ef8 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
15ef9 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28 26 70 2d  MemSetDouble(&p-
15efa 3e 61 56 61 72 5b 69 2d 31 5d 2c 20 72 56 61 6c  >aVar[i-1], rVal
15efb 75 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ue);.  }.  sqlit
15efc 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
15efd 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  ->db->mutex);.  
15efe 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c  return rc;.}.SQL
15eff 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
15f00 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 71 6c  te3_bind_int(sql
15f01 69 74 65 33 5f 73 74 6d 74 20 2a 70 2c 20 69 6e  ite3_stmt *p, in
15f02 74 20 69 2c 20 69 6e 74 20 69 56 61 6c 75 65 29  t i, int iValue)
15f03 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
15f04 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 2c  e3_bind_int64(p,
15f05 20 69 2c 20 28 69 36 34 29 69 56 61 6c 75 65 29   i, (i64)iValue)
15f06 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ;.}.SQLITE_API i
15f07 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
15f08 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74  int64(sqlite3_st
15f09 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
15f0a 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  , sqlite_int64 i
15f0b 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63  Value){.  int rc
15f0c 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56  ;.  Vdbe *p = (V
15f0d 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 73  dbe *)pStmt;.  s
15f0e 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
15f0f 65 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  er(p->db->mutex)
15f10 3b 0a 20 20 72 63 20 3d 20 76 64 62 65 55 6e 62  ;.  rc = vdbeUnb
15f11 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69 66 28  ind(p, i);.  if(
15f12 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
15f13 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
15f14 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 26 70 2d  eMemSetInt64(&p-
15f15 3e 61 56 61 72 5b 69 2d 31 5d 2c 20 69 56 61 6c  >aVar[i-1], iVal
15f16 75 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ue);.  }.  sqlit
15f17 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
15f18 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  ->db->mutex);.  
15f19 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c  return rc;.}.SQL
15f1a 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
15f1b 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 73 71  te3_bind_null(sq
15f1c 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
15f1d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74  t, int i){.  int
15f1e 20 72 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d   rc;.  Vdbe *p =
15f1f 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20   (Vdbe*)pStmt;. 
15f20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
15f21 6e 74 65 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  nter(p->db->mute
15f22 78 29 3b 0a 20 20 72 63 20 3d 20 76 64 62 65 55  x);.  rc = vdbeU
15f23 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20 73  nbind(p, i);.  s
15f24 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
15f25 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ve(p->db->mutex)
15f26 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
15f27 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
15f28 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78  sqlite3_bind_tex
15f29 74 28 20 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  t( .  sqlite3_st
15f2a 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e  mt *pStmt, .  in
15f2b 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68  t i, .  const ch
15f2c 61 72 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e  ar *zData, .  in
15f2d 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 64  t nData, .  void
15f2e 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a   (*xDel)(void*).
15f2f 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e 64  ){.  return bind
15f30 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a  Text(pStmt, i, z
15f31 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 65  Data, nData, xDe
15f32 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b  l, SQLITE_UTF8);
15f33 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  .}.#ifndef SQLIT
15f34 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c  E_OMIT_UTF16.SQL
15f35 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
15f36 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 28  te3_bind_text16(
15f37 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
15f38 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69  *pStmt, .  int i
15f39 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  , .  const void 
15f3a 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e  *zData, .  int n
15f3b 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a  Data, .  void (*
15f3c 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a  xDel)(void*).){.
15f3d 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78    return bindTex
15f3e 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74  t(pStmt, i, zDat
15f3f 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20  a, nData, xDel, 
15f40 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49  SQLITE_UTF16NATI
15f41 56 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  VE);.}.#endif /*
15f42 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
15f43 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  16 */.SQLITE_API
15f44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
15f45 64 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f  d_value(sqlite3_
15f46 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
15f47 20 69 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65   i, const sqlite
15f48 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 29  3_value *pValue)
15f49 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64  {.  int rc;.  Vd
15f4a 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29  be *p = (Vdbe *)
15f4b 70 53 74 6d 74 3b 0a 20 20 73 71 6c 69 74 65 33  pStmt;.  sqlite3
15f4c 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e  _mutex_enter(p->
15f4d 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 63  db->mutex);.  rc
15f4e 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c   = vdbeUnbind(p,
15f4f 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53   i);.  if( rc==S
15f50 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
15f51 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
15f52 4d 65 6d 43 6f 70 79 28 26 70 2d 3e 61 56 61 72  MemCopy(&p->aVar
15f53 5b 69 2d 31 5d 2c 20 70 56 61 6c 75 65 29 3b 0a  [i-1], pValue);.
15f54 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74    }.  rc = sqlit
15f55 65 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c  e3ApiExit(p->db,
15f56 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f   rc);.  sqlite3_
15f57 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64  mutex_leave(p->d
15f58 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
15f59 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45  urn rc;.}.SQLITE
15f5a 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
15f5b 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73  _bind_zeroblob(s
15f5c 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
15f5d 6d 74 2c 20 69 6e 74 20 69 2c 20 69 6e 74 20 6e  mt, int i, int n
15f5e 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56  ){.  int rc;.  V
15f5f 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a  dbe *p = (Vdbe *
15f60 29 70 53 74 6d 74 3b 0a 20 20 73 71 6c 69 74 65  )pStmt;.  sqlite
15f61 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d  3_mutex_enter(p-
15f62 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  >db->mutex);.  r
15f63 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70  c = vdbeUnbind(p
15f64 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  , i);.  if( rc==
15f65 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
15f66 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
15f67 65 74 5a 65 72 6f 42 6c 6f 62 28 26 70 2d 3e 61  etZeroBlob(&p->a
15f68 56 61 72 5b 69 2d 31 5d 2c 20 6e 29 3b 0a 20 20  Var[i-1], n);.  
15f69 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
15f6a 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d  x_leave(p->db->m
15f6b 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
15f6c 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  rc;.}../*.** Ret
15f6d 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
15f6e 66 20 77 69 6c 64 63 61 72 64 73 20 74 68 61 74  f wildcards that
15f6f 20 63 61 6e 20 62 65 20 70 6f 74 65 6e 74 69 61   can be potentia
15f70 6c 6c 79 20 62 6f 75 6e 64 20 74 6f 2e 0a 2a 2a  lly bound to..**
15f71 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
15f72 20 61 64 64 65 64 20 74 6f 20 73 75 70 70 6f 72   added to suppor
15f73 74 20 44 42 44 3a 3a 53 51 4c 69 74 65 2e 20 20  t DBD::SQLite.  
15f74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
15f75 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
15f76 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28  parameter_count(
15f77 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
15f78 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 20  tmt){.  Vdbe *p 
15f79 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a  = (Vdbe*)pStmt;.
15f7a 20 20 72 65 74 75 72 6e 20 70 20 3f 20 70 2d 3e    return p ? p->
15f7b 6e 56 61 72 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a  nVar : 0;.}../*.
15f7c 2a 2a 20 43 72 65 61 74 65 20 61 20 6d 61 70 70  ** Create a mapp
15f7d 69 6e 67 20 66 72 6f 6d 20 76 61 72 69 61 62 6c  ing from variabl
15f7e 65 20 6e 75 6d 62 65 72 73 20 74 6f 20 76 61 72  e numbers to var
15f7f 69 61 62 6c 65 20 6e 61 6d 65 73 0a 2a 2a 20 69  iable names.** i
15f80 6e 20 74 68 65 20 56 64 62 65 2e 61 7a 56 61 72  n the Vdbe.azVar
15f81 5b 5d 20 61 72 72 61 79 2c 20 69 66 20 73 75 63  [] array, if suc
15f82 68 20 61 20 6d 61 70 70 69 6e 67 20 64 6f 65 73  h a mapping does
15f83 20 6e 6f 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20   not already.** 
15f84 65 78 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  exist..*/.static
15f85 20 76 6f 69 64 20 63 72 65 61 74 65 56 61 72 4d   void createVarM
15f86 61 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69  ap(Vdbe *p){.  i
15f87 66 28 20 21 70 2d 3e 6f 6b 56 61 72 20 29 7b 0a  f( !p->okVar ){.
15f88 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
15f89 78 5f 65 6e 74 65 72 28 70 2d 3e 64 62 2d 3e 6d  x_enter(p->db->m
15f8a 75 74 65 78 29 3b 0a 20 20 20 20 69 66 28 20 21  utex);.    if( !
15f8b 70 2d 3e 6f 6b 56 61 72 20 29 7b 0a 20 20 20 20  p->okVar ){.    
15f8c 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 4f    int j;.      O
15f8d 70 20 2a 70 4f 70 3b 0a 20 20 20 20 20 20 66 6f  p *pOp;.      fo
15f8e 72 28 6a 3d 30 2c 20 70 4f 70 3d 70 2d 3e 61 4f  r(j=0, pOp=p->aO
15f8f 70 3b 20 6a 3c 70 2d 3e 6e 4f 70 3b 20 6a 2b 2b  p; j<p->nOp; j++
15f90 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20  , pOp++){.      
15f91 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64    if( pOp->opcod
15f92 65 3d 3d 4f 50 5f 56 61 72 69 61 62 6c 65 20 29  e==OP_Variable )
15f93 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  {.          asse
15f94 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26  rt( pOp->p1>0 &&
15f95 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 56 61   pOp->p1<=p->nVa
15f96 72 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70  r );.          p
15f97 2d 3e 61 7a 56 61 72 5b 70 4f 70 2d 3e 70 31 2d  ->azVar[pOp->p1-
15f98 31 5d 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a  1] = pOp->p4.z;.
15f99 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
15f9a 7d 0a 20 20 20 20 20 20 70 2d 3e 6f 6b 56 61 72  }.      p->okVar
15f9b 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 1;.    }.    
15f9c 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
15f9d 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  ave(p->db->mutex
15f9e 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
15f9f 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20  Return the name 
15fa0 6f 66 20 61 20 77 69 6c 64 63 61 72 64 20 70 61  of a wildcard pa
15fa1 72 61 6d 65 74 65 72 2e 20 20 52 65 74 75 72 6e  rameter.  Return
15fa2 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 69 6e 64   NULL if the ind
15fa3 65 78 0a 2a 2a 20 69 73 20 6f 75 74 20 6f 66 20  ex.** is out of 
15fa4 72 61 6e 67 65 20 6f 72 20 69 66 20 74 68 65 20  range or if the 
15fa5 77 69 6c 64 63 61 72 64 20 69 73 20 75 6e 6e 61  wildcard is unna
15fa6 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  med..**.** The r
15fa7 65 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20  esult is always 
15fa8 55 54 46 2d 38 2e 0a 2a 2f 0a 53 51 4c 49 54 45  UTF-8..*/.SQLITE
15fa9 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
15faa 2a 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61  *sqlite3_bind_pa
15fab 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 73 71 6c  rameter_name(sql
15fac 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
15fad 2c 20 69 6e 74 20 69 29 7b 0a 20 20 56 64 62 65  , int i){.  Vdbe
15fae 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74   *p = (Vdbe*)pSt
15faf 6d 74 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c  mt;.  if( p==0 |
15fb0 7c 20 69 3c 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56  | i<1 || i>p->nV
15fb1 61 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ar ){.    return
15fb2 20 30 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 65   0;.  }.  create
15fb3 56 61 72 4d 61 70 28 70 29 3b 0a 20 20 72 65 74  VarMap(p);.  ret
15fb4 75 72 6e 20 70 2d 3e 61 7a 56 61 72 5b 69 2d 31  urn p->azVar[i-1
15fb5 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65  ];.}../*.** Give
15fb6 6e 20 61 20 77 69 6c 64 63 61 72 64 20 70 61 72  n a wildcard par
15fb7 61 6d 65 74 65 72 20 6e 61 6d 65 2c 20 72 65 74  ameter name, ret
15fb8 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66  urn the index of
15fb9 20 74 68 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a   the variable.**
15fba 20 77 69 74 68 20 74 68 61 74 20 6e 61 6d 65 2e   with that name.
15fbb 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f    If there is no
15fbc 20 76 61 72 69 61 62 6c 65 20 77 69 74 68 20 74   variable with t
15fbd 68 65 20 67 69 76 65 6e 20 6e 61 6d 65 2c 0a 2a  he given name,.*
15fbe 2a 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53  * return 0..*/.S
15fbf 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
15fc0 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
15fc1 65 74 65 72 5f 69 6e 64 65 78 28 73 71 6c 69 74  eter_index(sqlit
15fc2 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
15fc3 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
15fc4 65 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20  e){.  Vdbe *p = 
15fc5 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20  (Vdbe*)pStmt;.  
15fc6 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d  int i;.  if( p==
15fc7 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
15fc8 30 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 65 56  0;.  }.  createV
15fc9 61 72 4d 61 70 28 70 29 3b 20 0a 20 20 69 66 28  arMap(p); .  if(
15fca 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 66 6f   zName ){.    fo
15fcb 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 72  r(i=0; i<p->nVar
15fcc 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 6f  ; i++){.      co
15fcd 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 2d  nst char *z = p-
15fce 3e 61 7a 56 61 72 5b 69 5d 3b 0a 20 20 20 20 20  >azVar[i];.     
15fcf 20 69 66 28 20 7a 20 26 26 20 73 74 72 63 6d 70   if( z && strcmp
15fd0 28 7a 2c 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a  (z,zName)==0 ){.
15fd1 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69          return i
15fd2 2b 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  +1;.      }.    
15fd3 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  }.  }.  return 0
15fd4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73  ;.}../*.** Trans
15fd5 66 65 72 20 61 6c 6c 20 62 69 6e 64 69 6e 67 73  fer all bindings
15fd6 20 66 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20   from the first 
15fd7 73 74 61 74 65 6d 65 6e 74 20 6f 76 65 72 20 74  statement over t
15fd8 6f 20 74 68 65 20 73 65 63 6f 6e 64 2e 0a 2a 2a  o the second..**
15fd9 20 49 66 20 74 68 65 20 74 77 6f 20 73 74 61 74   If the two stat
15fda 65 6d 65 6e 74 73 20 63 6f 6e 74 61 69 6e 20 61  ements contain a
15fdb 20 64 69 66 66 65 72 65 6e 74 20 6e 75 6d 62 65   different numbe
15fdc 72 20 6f 66 20 62 69 6e 64 69 6e 67 73 2c 20 74  r of bindings, t
15fdd 68 65 6e 0a 2a 2a 20 61 6e 20 53 51 4c 49 54 45  hen.** an SQLITE
15fde 5f 45 52 52 4f 52 20 69 73 20 72 65 74 75 72 6e  _ERROR is return
15fdf 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ed..*/.SQLITE_AP
15fe0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 72  I int sqlite3_tr
15fe1 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 28  ansfer_bindings(
15fe2 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 46  sqlite3_stmt *pF
15fe3 72 6f 6d 53 74 6d 74 2c 20 73 71 6c 69 74 65 33  romStmt, sqlite3
15fe4 5f 73 74 6d 74 20 2a 70 54 6f 53 74 6d 74 29 7b  _stmt *pToStmt){
15fe5 0a 20 20 56 64 62 65 20 2a 70 46 72 6f 6d 20 3d  .  Vdbe *pFrom =
15fe6 20 28 56 64 62 65 2a 29 70 46 72 6f 6d 53 74 6d   (Vdbe*)pFromStm
15fe7 74 3b 0a 20 20 56 64 62 65 20 2a 70 54 6f 20 3d  t;.  Vdbe *pTo =
15fe8 20 28 56 64 62 65 2a 29 70 54 6f 53 74 6d 74 3b   (Vdbe*)pToStmt;
15fe9 0a 20 20 69 6e 74 20 69 2c 20 72 63 20 3d 20 53  .  int i, rc = S
15fea 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
15feb 28 70 46 72 6f 6d 2d 3e 6d 61 67 69 63 21 3d 56  (pFrom->magic!=V
15fec 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26 26  DBE_MAGIC_RUN &&
15fed 20 70 46 72 6f 6d 2d 3e 6d 61 67 69 63 21 3d 56   pFrom->magic!=V
15fee 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 29 0a  DBE_MAGIC_HALT).
15fef 20 20 20 20 7c 7c 20 28 70 54 6f 2d 3e 6d 61 67      || (pTo->mag
15ff0 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52  ic!=VDBE_MAGIC_R
15ff1 55 4e 20 26 26 20 70 54 6f 2d 3e 6d 61 67 69 63  UN && pTo->magic
15ff2 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c  !=VDBE_MAGIC_HAL
15ff3 54 29 0a 20 20 20 20 7c 7c 20 70 54 6f 2d 3e 64  T).    || pTo->d
15ff4 62 21 3d 70 46 72 6f 6d 2d 3e 64 62 20 29 7b 0a  b!=pFrom->db ){.
15ff5 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
15ff6 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
15ff7 69 66 28 20 70 46 72 6f 6d 2d 3e 6e 56 61 72 21  if( pFrom->nVar!
15ff8 3d 70 54 6f 2d 3e 6e 56 61 72 20 29 7b 0a 20 20  =pTo->nVar ){.  
15ff9 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
15ffa 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 73 71 6c  ERROR;.  }.  sql
15ffb 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
15ffc 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  (pTo->db->mutex)
15ffd 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d  ;.  for(i=0; rc=
15ffe 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c  =SQLITE_OK && i<
15fff 70 46 72 6f 6d 2d 3e 6e 56 61 72 3b 20 69 2b 2b  pFrom->nVar; i++
16000 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
16001 62 65 4d 65 6d 4d 6f 76 65 28 26 70 54 6f 2d 3e  beMemMove(&pTo->
16002 61 56 61 72 5b 69 5d 2c 20 26 70 46 72 6f 6d 2d  aVar[i], &pFrom-
16003 3e 61 56 61 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20  >aVar[i]);.  }. 
16004 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
16005 65 61 76 65 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75  eave(pTo->db->mu
16006 74 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20  tex);.  assert( 
16007 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
16008 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
16009 4d 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  M );.  return rc
1600a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
1600b 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2a 20 64  n the sqlite3* d
1600c 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74  atabase handle t
1600d 6f 20 77 68 69 63 68 20 74 68 65 20 70 72 65 70  o which the prep
1600e 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 67  ared statement g
1600f 69 76 65 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 61  iven.** in the a
16010 72 67 75 6d 65 6e 74 20 62 65 6c 6f 6e 67 73 2e  rgument belongs.
16011 20 20 54 68 69 73 20 69 73 20 74 68 65 20 73 61    This is the sa
16012 6d 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  me database hand
16013 6c 65 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 74  le that was.** t
16014 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
16015 74 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  t to the sqlite3
16016 5f 70 72 65 70 61 72 65 28 29 20 74 68 61 74 20  _prepare() that 
16017 77 61 73 20 75 73 65 64 20 74 6f 20 63 72 65 61  was used to crea
16018 74 65 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 6d  te.** the statem
16019 65 6e 74 20 69 6e 20 74 68 65 20 66 69 72 73 74  ent in the first
1601a 20 70 6c 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54   place..*/.SQLIT
1601b 45 5f 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73  E_API sqlite3 *s
1601c 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65  qlite3_db_handle
1601d 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1601e 53 74 6d 74 29 7b 0a 20 20 72 65 74 75 72 6e 20  Stmt){.  return 
1601f 70 53 74 6d 74 20 3f 20 28 28 56 64 62 65 2a 29  pStmt ? ((Vdbe*)
16020 70 53 74 6d 74 29 2d 3e 64 62 20 3a 20 30 3b 0a  pStmt)->db : 0;.
16021 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
16022 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 61 70  ** End of vdbeap
16023 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  i.c ************
16024 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16025 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16026 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
16027 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64  ** Begin file vd
16028 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  be.c ***********
16029 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1602a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1602b 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
1602c 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
1602d 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1602e 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1602f 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
16030 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
16031 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
16032 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
16033 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
16034 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
16035 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
16036 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
16037 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
16038 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
16039 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
1603a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1603b 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1603c 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1603d 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1603e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1603f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16041 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16042 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65  *********.** The
16043 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69   code in this fi
16044 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 65 78  le implements ex
16045 65 63 75 74 69 6f 6e 20 6d 65 74 68 6f 64 20 6f  ecution method o
16046 66 20 74 68 65 20 0a 2a 2a 20 56 69 72 74 75 61  f the .** Virtua
16047 6c 20 44 61 74 61 62 61 73 65 20 45 6e 67 69 6e  l Database Engin
16048 65 20 28 56 44 42 45 29 2e 20 20 41 20 73 65 70  e (VDBE).  A sep
16049 61 72 61 74 65 20 66 69 6c 65 20 28 22 76 64 62  arate file ("vdb
1604a 65 61 75 78 2e 63 22 29 0a 2a 2a 20 68 61 6e 64  eaux.c").** hand
1604b 6c 65 73 20 68 6f 75 73 65 6b 65 65 70 69 6e 67  les housekeeping
1604c 20 64 65 74 61 69 6c 73 20 73 75 63 68 20 61 73   details such as
1604d 20 63 72 65 61 74 69 6e 67 20 61 6e 64 20 64 65   creating and de
1604e 6c 65 74 69 6e 67 0a 2a 2a 20 56 44 42 45 20 69  leting.** VDBE i
1604f 6e 73 74 61 6e 63 65 73 2e 20 20 54 68 69 73 20  nstances.  This 
16050 66 69 6c 65 20 69 73 20 73 6f 6c 65 6c 79 20 69  file is solely i
16051 6e 74 65 72 65 73 74 65 64 20 69 6e 20 65 78 65  nterested in exe
16052 63 75 74 69 6e 67 0a 2a 2a 20 74 68 65 20 56 44  cuting.** the VD
16053 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a  BE program..**.*
16054 2a 20 49 6e 20 74 68 65 20 65 78 74 65 72 6e 61  * In the externa
16055 6c 20 69 6e 74 65 72 66 61 63 65 2c 20 61 6e 20  l interface, an 
16056 22 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 22 20  "sqlite3_stmt*" 
16057 69 73 20 61 6e 20 6f 70 61 71 75 65 20 70 6f 69  is an opaque poi
16058 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 20 56 44 42  nter.** to a VDB
16059 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c  E..**.** The SQL
1605a 20 70 61 72 73 65 72 20 67 65 6e 65 72 61 74 65   parser generate
1605b 73 20 61 20 70 72 6f 67 72 61 6d 20 77 68 69 63  s a program whic
1605c 68 20 69 73 20 74 68 65 6e 20 65 78 65 63 75 74  h is then execut
1605d 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 56 44 42  ed by.** the VDB
1605e 45 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b  E to do the work
1605f 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61 74   of the SQL stat
16060 65 6d 65 6e 74 2e 20 20 56 44 42 45 20 70 72 6f  ement.  VDBE pro
16061 67 72 61 6d 73 20 61 72 65 20 0a 2a 2a 20 73 69  grams are .** si
16062 6d 69 6c 61 72 20 69 6e 20 66 6f 72 6d 20 74 6f  milar in form to
16063 20 61 73 73 65 6d 62 6c 79 20 6c 61 6e 67 75 61   assembly langua
16064 67 65 2e 20 20 54 68 65 20 70 72 6f 67 72 61 6d  ge.  The program
16065 20 63 6f 6e 73 69 73 74 73 20 6f 66 0a 2a 2a 20   consists of.** 
16066 61 20 6c 69 6e 65 61 72 20 73 65 71 75 65 6e 63  a linear sequenc
16067 65 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 73 2e  e of operations.
16068 20 20 45 61 63 68 20 6f 70 65 72 61 74 69 6f 6e    Each operation
16069 20 68 61 73 20 61 6e 20 6f 70 63 6f 64 65 20 0a   has an opcode .
1606a 2a 2a 20 61 6e 64 20 35 20 6f 70 65 72 61 6e 64  ** and 5 operand
1606b 73 2e 20 20 4f 70 65 72 61 6e 64 73 20 50 31 2c  s.  Operands P1,
1606c 20 50 32 2c 20 61 6e 64 20 50 33 20 61 72 65 20   P2, and P3 are 
1606d 69 6e 74 65 67 65 72 73 2e 20 20 4f 70 65 72 61  integers.  Opera
1606e 6e 64 20 50 34 20 0a 2a 2a 20 69 73 20 61 20 6e  nd P4 .** is a n
1606f 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 73  ull-terminated s
16070 74 72 69 6e 67 2e 20 20 4f 70 65 72 61 6e 64 20  tring.  Operand 
16071 50 35 20 69 73 20 61 6e 20 75 6e 73 69 67 6e 65  P5 is an unsigne
16072 64 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20  d character..** 
16073 46 65 77 20 6f 70 63 6f 64 65 73 20 75 73 65 20  Few opcodes use 
16074 61 6c 6c 20 35 20 6f 70 65 72 61 6e 64 73 2e 0a  all 5 operands..
16075 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 61 74 69 6f  **.** Computatio
16076 6e 20 72 65 73 75 6c 74 73 20 61 72 65 20 73 74  n results are st
16077 6f 72 65 64 20 6f 6e 20 61 20 73 65 74 20 6f 66  ored on a set of
16078 20 72 65 67 69 73 74 65 72 73 20 6e 75 6d 62 65   registers numbe
16079 72 65 64 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a  red beginning.**
1607a 20 77 69 74 68 20 31 20 61 6e 64 20 67 6f 69 6e   with 1 and goin
1607b 67 20 75 70 20 74 6f 20 56 64 62 65 2e 6e 4d 65  g up to Vdbe.nMe
1607c 6d 2e 20 20 45 61 63 68 20 72 65 67 69 73 74 65  m.  Each registe
1607d 72 20 63 61 6e 20 73 74 6f 72 65 0a 2a 2a 20 65  r can store.** e
1607e 69 74 68 65 72 20 61 6e 20 69 6e 74 65 67 65 72  ither an integer
1607f 2c 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61  , a null-termina
16080 74 65 64 20 73 74 72 69 6e 67 2c 20 61 20 66 6c  ted string, a fl
16081 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20  oating point.** 
16082 6e 75 6d 62 65 72 2c 20 6f 72 20 74 68 65 20 53  number, or the S
16083 51 4c 20 22 4e 55 4c 4c 22 20 76 61 6c 75 65 2e  QL "NULL" value.
16084 20 20 41 6e 20 69 6e 70 6c 69 63 69 74 20 63 6f    An inplicit co
16085 6e 76 65 72 73 69 6f 6e 20 66 72 6f 6d 20 6f 6e  nversion from on
16086 65 0a 2a 2a 20 74 79 70 65 20 74 6f 20 74 68 65  e.** type to the
16087 20 6f 74 68 65 72 20 6f 63 63 75 72 73 20 61 73   other occurs as
16088 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 0a   necessary..** .
16089 2a 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 63  ** Most of the c
1608a 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ode in this file
1608b 20 69 73 20 74 61 6b 65 6e 20 75 70 20 62 79 20   is taken up by 
1608c 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 45  the sqlite3VdbeE
1608d 78 65 63 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f  xec().** functio
1608e 6e 20 77 68 69 63 68 20 64 6f 65 73 20 74 68 65  n which does the
1608f 20 77 6f 72 6b 20 6f 66 20 69 6e 74 65 72 70 72   work of interpr
16090 65 74 69 6e 67 20 61 20 56 44 42 45 20 70 72 6f  eting a VDBE pro
16091 67 72 61 6d 2e 0a 2a 2a 20 42 75 74 20 6f 74 68  gram..** But oth
16092 65 72 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  er routines are 
16093 61 6c 73 6f 20 70 72 6f 76 69 64 65 64 20 74 6f  also provided to
16094 20 68 65 6c 70 20 69 6e 20 62 75 69 6c 64 69 6e   help in buildin
16095 67 20 75 70 0a 2a 2a 20 61 20 70 72 6f 67 72 61  g up.** a progra
16096 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 62 79  m instruction by
16097 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a   instruction..**
16098 0a 2a 2a 20 56 61 72 69 6f 75 73 20 73 63 72 69  .** Various scri
16099 70 74 73 20 73 63 61 6e 20 74 68 69 73 20 73 6f  pts scan this so
1609a 75 72 63 65 20 66 69 6c 65 20 69 6e 20 6f 72 64  urce file in ord
1609b 65 72 20 74 6f 20 67 65 6e 65 72 61 74 65 20 48  er to generate H
1609c 54 4d 4c 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 61  TML.** documenta
1609d 74 69 6f 6e 2c 20 68 65 61 64 65 72 73 20 66 69  tion, headers fi
1609e 6c 65 73 2c 20 6f 72 20 6f 74 68 65 72 20 64 65  les, or other de
1609f 72 69 76 65 64 20 66 69 6c 65 73 2e 20 20 54 68  rived files.  Th
160a0 65 20 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20  e formatting.** 
160a1 6f 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74  of the code in t
160a2 68 69 73 20 66 69 6c 65 20 69 73 2c 20 74 68 65  his file is, the
160a3 72 65 66 6f 72 65 2c 20 69 6d 70 6f 72 74 61 6e  refore, importan
160a4 74 2e 20 20 53 65 65 20 6f 74 68 65 72 20 63 6f  t.  See other co
160a5 6d 6d 65 6e 74 73 0a 2a 2a 20 69 6e 20 74 68 69  mments.** in thi
160a6 73 20 66 69 6c 65 20 66 6f 72 20 64 65 74 61 69  s file for detai
160a7 6c 73 2e 20 20 49 66 20 69 6e 20 64 6f 75 62 74  ls.  If in doubt
160a8 2c 20 64 6f 20 6e 6f 74 20 64 65 76 69 61 74 65  , do not deviate
160a9 20 66 72 6f 6d 20 65 78 69 73 74 69 6e 67 0a 2a   from existing.*
160aa 2a 20 63 6f 6d 6d 65 6e 74 69 6e 67 20 61 6e 64  * commenting and
160ab 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 70 72 61   indentation pra
160ac 63 74 69 63 65 73 20 77 68 65 6e 20 63 68 61 6e  ctices when chan
160ad 67 69 6e 67 20 6f 72 20 61 64 64 69 6e 67 20 63  ging or adding c
160ae 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  ode..**.** $Id: 
160af 76 64 62 65 2e 63 2c 76 20 31 2e 37 34 30 20 32  vdbe.c,v 1.740 2
160b0 30 30 38 2f 30 35 2f 31 33 20 31 33 3a 32 37 3a  008/05/13 13:27:
160b1 33 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  34 drh Exp $.*/.
160b2 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
160b3 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69  wing global vari
160b4 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e  able is incremen
160b5 74 65 64 20 65 76 65 72 79 20 74 69 6d 65 20 61  ted every time a
160b6 20 63 75 72 73 6f 72 0a 2a 2a 20 6d 6f 76 65 73   cursor.** moves
160b7 2c 20 65 69 74 68 65 72 20 62 79 20 74 68 65 20  , either by the 
160b8 4f 50 5f 4d 6f 76 65 58 58 2c 20 4f 50 5f 4e 65  OP_MoveXX, OP_Ne
160b9 78 74 2c 20 6f 72 20 4f 50 5f 50 72 65 76 20 6f  xt, or OP_Prev o
160ba 70 63 6f 64 65 73 2e 20 20 54 68 65 20 74 65 73  pcodes.  The tes
160bb 74 0a 2a 2a 20 70 72 6f 63 65 64 75 72 65 73 20  t.** procedures 
160bc 75 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61  use this informa
160bd 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72  tion to make sur
160be 65 20 74 68 61 74 20 69 6e 64 69 63 65 73 20 61  e that indices a
160bf 72 65 0a 2a 2a 20 77 6f 72 6b 69 6e 67 20 63 6f  re.** working co
160c0 72 72 65 63 74 6c 79 2e 20 20 54 68 69 73 20 76  rrectly.  This v
160c1 61 72 69 61 62 6c 65 20 68 61 73 20 6e 6f 20 66  ariable has no f
160c2 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68  unction other th
160c3 61 6e 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 76 65  an to.** help ve
160c4 72 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 74  rify the correct
160c5 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68   operation of th
160c6 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69  e library..*/.#i
160c7 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
160c8 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
160c9 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63  sqlite3_search_c
160ca 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66  ount = 0;.#endif
160cb 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69  ../*.** When thi
160cc 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  s global variabl
160cd 65 20 69 73 20 70 6f 73 69 74 69 76 65 2c 20 69  e is positive, i
160ce 74 20 67 65 74 73 20 64 65 63 72 65 6d 65 6e 74  t gets decrement
160cf 65 64 20 6f 6e 63 65 20 62 65 66 6f 72 65 0a 2a  ed once before.*
160d0 2a 20 65 61 63 68 20 69 6e 73 74 72 75 63 74 69  * each instructi
160d1 6f 6e 20 69 6e 20 74 68 65 20 56 44 42 45 2e 20  on in the VDBE. 
160d2 20 57 68 65 6e 20 72 65 61 63 68 65 73 20 7a 65   When reaches ze
160d3 72 6f 2c 20 74 68 65 20 75 31 2e 69 73 49 6e 74  ro, the u1.isInt
160d4 65 72 72 75 70 74 65 64 0a 2a 2a 20 66 69 65 6c  errupted.** fiel
160d5 64 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  d of the sqlite3
160d6 20 73 74 72 75 63 74 75 72 65 20 69 73 20 73 65   structure is se
160d7 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 69  t in order to si
160d8 6d 75 6c 61 74 65 20 61 6e 64 20 69 6e 74 65 72  mulate and inter
160d9 72 75 70 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  rupt..**.** This
160da 20 66 61 63 69 6c 69 74 79 20 69 73 20 75 73 65   facility is use
160db 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 70 75  d for testing pu
160dc 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 20 20 49 74  rposes only.  It
160dd 20 64 6f 65 73 20 6e 6f 74 20 66 75 6e 63 74 69   does not functi
160de 6f 6e 0a 2a 2a 20 69 6e 20 61 6e 20 6f 72 64 69  on.** in an ordi
160df 6e 61 72 79 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23  nary build..*/.#
160e0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
160e1 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  T.SQLITE_API int
160e2 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
160e3 70 74 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65  pt_count = 0;.#e
160e4 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
160e5 6e 65 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69  next global vari
160e6 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e  able is incremen
160e7 74 65 64 20 65 61 63 68 20 74 79 70 65 20 74 68  ted each type th
160e8 65 20 4f 50 5f 53 6f 72 74 20 6f 70 63 6f 64 65  e OP_Sort opcode
160e9 0a 2a 2a 20 69 73 20 65 78 65 63 75 74 65 64 2e  .** is executed.
160ea 20 20 54 68 65 20 74 65 73 74 20 70 72 6f 63 65    The test proce
160eb 64 75 72 65 73 20 75 73 65 20 74 68 69 73 20 69  dures use this i
160ec 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61  nformation to ma
160ed 6b 65 20 73 75 72 65 20 74 68 61 74 0a 2a 2a 20  ke sure that.** 
160ee 73 6f 72 74 69 6e 67 20 69 73 20 6f 63 63 75 72  sorting is occur
160ef 72 69 6e 67 20 6f 72 20 6e 6f 74 20 6f 63 63 75  ring or not occu
160f0 72 69 6e 67 20 61 74 20 61 70 70 72 6f 70 72 69  ring at appropri
160f1 61 74 65 20 74 69 6d 65 73 2e 20 20 20 54 68 69  ate times.   Thi
160f2 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 68 61  s variable.** ha
160f3 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74  s no function ot
160f4 68 65 72 20 74 68 61 6e 20 74 6f 20 68 65 6c 70  her than to help
160f5 20 76 65 72 69 66 79 20 74 68 65 20 63 6f 72 72   verify the corr
160f6 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66  ect operation of
160f7 20 74 68 65 0a 2a 2a 20 6c 69 62 72 61 72 79 2e   the.** library.
160f8 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
160f9 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50  E_TEST.SQLITE_AP
160fa 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6f  I int sqlite3_so
160fb 72 74 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65  rt_count = 0;.#e
160fc 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
160fd 6e 65 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69  next global vari
160fe 61 62 6c 65 20 72 65 63 6f 72 64 73 20 74 68 65  able records the
160ff 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72   size of the lar
16100 67 65 73 74 20 4d 45 4d 5f 42 6c 6f 62 0a 2a 2a  gest MEM_Blob.**
16101 20 6f 72 20 4d 45 4d 5f 53 74 72 20 74 68 61 74   or MEM_Str that
16102 20 68 61 73 20 62 65 65 6e 20 75 73 65 64 20 62   has been used b
16103 79 20 61 20 56 44 42 45 20 6f 70 63 6f 64 65 2e  y a VDBE opcode.
16104 20 20 54 68 65 20 74 65 73 74 20 70 72 6f 63 65    The test proce
16105 64 75 72 65 73 0a 2a 2a 20 75 73 65 20 74 68 69  dures.** use thi
16106 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f  s information to
16107 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   make sure that 
16108 74 68 65 20 7a 65 72 6f 2d 62 6c 6f 62 20 66 75  the zero-blob fu
16109 6e 63 74 69 6f 6e 61 6c 69 74 79 0a 2a 2a 20 69  nctionality.** i
1610a 73 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72 65 63  s working correc
1610b 74 6c 79 2e 20 20 20 54 68 69 73 20 76 61 72 69  tly.   This vari
1610c 61 62 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e 63  able has no func
1610d 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20  tion other than 
1610e 74 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69 66  to.** help verif
1610f 79 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70  y the correct op
16110 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c  eration of the l
16111 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65  ibrary..*/.#ifde
16112 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
16113 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
16114 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a  ite3_max_blobsiz
16115 65 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 76 6f  e = 0;.static vo
16116 69 64 20 75 70 64 61 74 65 4d 61 78 42 6c 6f 62  id updateMaxBlob
16117 73 69 7a 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20  size(Mem *p){.  
16118 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20  if( (p->flags & 
16119 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f  (MEM_Str|MEM_Blo
1611a 62 29 29 21 3d 30 20 26 26 20 70 2d 3e 6e 3e 73  b))!=0 && p->n>s
1611b 71 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73  qlite3_max_blobs
1611c 69 7a 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ize ){.    sqlit
1611d 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20  e3_max_blobsize 
1611e 3d 20 70 2d 3e 6e 3b 0a 20 20 7d 0a 7d 0a 23 65  = p->n;.  }.}.#e
1611f 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74  ndif../*.** Test
16120 20 61 20 72 65 67 69 73 74 65 72 20 74 6f 20 73   a register to s
16121 65 65 20 69 66 20 69 74 20 65 78 63 65 65 64 73  ee if it exceeds
16122 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61 78   the current max
16123 69 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a  imum blob size..
16124 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 2c 20 72  ** If it does, r
16125 65 63 6f 72 64 20 74 68 65 20 6e 65 77 20 6d 61  ecord the new ma
16126 78 69 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e  ximum blob size.
16127 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .*/.#if defined(
16128 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20  SQLITE_TEST) && 
16129 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
1612a 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53  OMIT_BUILTIN_TES
1612b 54 29 0a 23 20 64 65 66 69 6e 65 20 55 50 44 41  T).# define UPDA
1612c 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28  TE_MAX_BLOBSIZE(
1612d 50 29 20 20 75 70 64 61 74 65 4d 61 78 42 6c 6f  P)  updateMaxBlo
1612e 62 73 69 7a 65 28 50 29 0a 23 65 6c 73 65 0a 23  bsize(P).#else.#
1612f 20 64 65 66 69 6e 65 20 55 50 44 41 54 45 5f 4d   define UPDATE_M
16130 41 58 5f 42 4c 4f 42 53 49 5a 45 28 50 29 0a 23  AX_BLOBSIZE(P).#
16131 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c  endif../*.** Rel
16132 65 61 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20  ease the memory 
16133 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
16134 61 20 72 65 67 69 73 74 65 72 2e 20 20 54 68 69  a register.  Thi
16135 73 0a 2a 2a 20 6c 65 61 76 65 73 20 74 68 65 20  s.** leaves the 
16136 4d 65 6d 2e 66 6c 61 67 73 20 66 69 65 6c 64 20  Mem.flags field 
16137 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65  in an inconsiste
16138 6e 74 20 73 74 61 74 65 2e 0a 2a 2f 0a 23 64 65  nt state..*/.#de
16139 66 69 6e 65 20 52 65 6c 65 61 73 65 28 50 29 20  fine Release(P) 
1613a 69 66 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45  if((P)->flags&ME
1613b 4d 5f 44 79 6e 29 7b 20 73 71 6c 69 74 65 33 56  M_Dyn){ sqlite3V
1613c 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 50 29  dbeMemRelease(P)
1613d 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  ; }../*.** Conve
1613e 72 74 20 74 68 65 20 67 69 76 65 6e 20 72 65 67  rt the given reg
1613f 69 73 74 65 72 20 69 6e 74 6f 20 61 20 73 74 72  ister into a str
16140 69 6e 67 20 69 66 20 69 74 20 69 73 6e 27 74 20  ing if it isn't 
16141 6f 6e 65 0a 2a 2a 20 61 6c 72 65 61 64 79 2e 20  one.** already. 
16142 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20  Return non-zero 
16143 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61  if a malloc() fa
16144 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ils..*/.#define 
16145 53 74 72 69 6e 67 69 66 79 28 50 2c 20 65 6e 63  Stringify(P, enc
16146 29 20 5c 0a 20 20 20 69 66 28 28 28 50 29 2d 3e  ) \.   if(((P)->
16147 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d  flags&(MEM_Str|M
16148 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20 26 26 20  EM_Blob))==0 && 
16149 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74  sqlite3VdbeMemSt
1614a 72 69 6e 67 69 66 79 28 50 2c 65 6e 63 29 29 20  ringify(P,enc)) 
1614b 5c 0a 20 20 20 20 20 7b 20 67 6f 74 6f 20 6e 6f  \.     { goto no
1614c 5f 6d 65 6d 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 41  _mem; }../*.** A
1614d 6e 20 65 70 68 65 6d 65 72 61 6c 20 73 74 72 69  n ephemeral stri
1614e 6e 67 20 76 61 6c 75 65 20 28 73 69 67 6e 69 66  ng value (signif
1614f 69 65 64 20 62 79 20 74 68 65 20 4d 45 4d 5f 45  ied by the MEM_E
16150 70 68 65 6d 20 66 6c 61 67 29 20 63 6f 6e 74 61  phem flag) conta
16151 69 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72  ins.** a pointer
16152 20 74 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c   to a dynamicall
16153 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 69  y allocated stri
16154 6e 67 20 77 68 65 72 65 20 73 6f 6d 65 20 6f 74  ng where some ot
16155 68 65 72 20 65 6e 74 69 74 79 0a 2a 2a 20 69 73  her entity.** is
16156 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72   responsible for
16157 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68   deallocating th
16158 61 74 20 73 74 72 69 6e 67 2e 20 20 42 65 63 61  at string.  Beca
16159 75 73 65 20 74 68 65 20 72 65 67 69 73 74 65 72  use the register
1615a 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e  .** does not con
1615b 74 72 6f 6c 20 74 68 65 20 73 74 72 69 6e 67 2c  trol the string,
1615c 20 69 74 20 6d 69 67 68 74 20 62 65 20 64 65 6c   it might be del
1615d 65 74 65 64 20 77 69 74 68 6f 75 74 20 74 68 65  eted without the
1615e 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6b 6e 6f   register.** kno
1615f 77 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54  wing it..**.** T
16160 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e 76  his routine conv
16161 65 72 74 73 20 61 6e 20 65 70 68 65 6d 65 72 61  erts an ephemera
16162 6c 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20  l string into a 
16163 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f  dynamically allo
16164 63 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20  cated.** string 
16165 74 68 61 74 20 74 68 65 20 72 65 67 69 73 74 65  that the registe
16166 72 20 69 74 73 65 6c 66 20 63 6f 6e 74 72 6f 6c  r itself control
16167 73 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72  s.  In other wor
16168 64 73 2c 20 69 74 0a 2a 2a 20 63 6f 6e 76 65 72  ds, it.** conver
16169 74 73 20 61 6e 20 4d 45 4d 5f 45 70 68 65 6d 20  ts an MEM_Ephem 
1616a 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 6e 20 4d  string into an M
1616b 45 4d 5f 44 79 6e 20 73 74 72 69 6e 67 2e 0a 2a  EM_Dyn string..*
1616c 2f 0a 23 64 65 66 69 6e 65 20 44 65 65 70 68 65  /.#define Deephe
1616d 6d 65 72 61 6c 69 7a 65 28 50 29 20 5c 0a 20 20  meralize(P) \.  
1616e 20 69 66 28 20 28 28 50 29 2d 3e 66 6c 61 67 73   if( ((P)->flags
1616f 26 4d 45 4d 5f 45 70 68 65 6d 29 21 3d 30 20 5c  &MEM_Ephem)!=0 \
16170 0a 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74  .       && sqlit
16171 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69  e3VdbeMemMakeWri
16172 74 65 61 62 6c 65 28 50 29 20 29 7b 20 67 6f 74  teable(P) ){ got
16173 6f 20 6e 6f 5f 6d 65 6d 3b 7d 0a 0a 2f 2a 0a 2a  o no_mem;}../*.*
16174 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 56 64  * Call sqlite3Vd
16175 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28  beMemExpandBlob(
16176 29 20 6f 6e 20 74 68 65 20 73 75 70 70 6c 69 65  ) on the supplie
16177 64 20 76 61 6c 75 65 20 28 74 79 70 65 20 4d 65  d value (type Me
16178 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 72 65 71 75  m*).** P if requ
16179 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ired..*/.#define
1617a 20 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 20 28   ExpandBlob(P) (
1617b 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  ((P)->flags&MEM_
1617c 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33 56 64 62  Zero)?sqlite3Vdb
1617d 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 50  eMemExpandBlob(P
1617e 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75  ):0)../*.** Argu
1617f 6d 65 6e 74 20 70 4d 65 6d 20 70 6f 69 6e 74 73  ment pMem points
16180 20 61 74 20 61 20 72 65 67 69 73 65 72 20 74 68   at a regiser th
16181 61 74 20 77 69 6c 6c 20 62 65 20 70 61 73 73 65  at will be passe
16182 64 20 74 6f 20 61 0a 2a 2a 20 75 73 65 72 2d 64  d to a.** user-d
16183 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20  efined function 
16184 6f 72 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74  or returned to t
16185 68 65 20 75 73 65 72 20 61 73 20 74 68 65 20 72  he user as the r
16186 65 73 75 6c 74 20 6f 66 20 61 20 71 75 65 72 79  esult of a query
16187 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  ..** The second 
16188 61 72 67 75 6d 65 6e 74 2c 20 27 64 62 5f 65 6e  argument, 'db_en
16189 63 27 20 69 73 20 74 68 65 20 74 65 78 74 20 65  c' is the text e
1618a 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79 20  ncoding used by 
1618b 74 68 65 20 76 64 62 65 20 66 6f 72 0a 2a 2a 20  the vdbe for.** 
1618c 72 65 67 69 73 74 65 72 20 76 61 72 69 61 62 6c  register variabl
1618d 65 73 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  es.  This routin
1618e 65 20 73 65 74 73 20 74 68 65 20 70 4d 65 6d 2d  e sets the pMem-
1618f 3e 65 6e 63 20 61 6e 64 20 70 4d 65 6d 2d 3e 74  >enc and pMem->t
16190 79 70 65 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73  ype.** variables
16191 20 75 73 65 64 20 62 79 20 74 68 65 20 73 71 6c   used by the sql
16192 69 74 65 33 5f 76 61 6c 75 65 5f 2a 28 29 20 72  ite3_value_*() r
16193 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 23 64 65 66  outines..*/.#def
16194 69 6e 65 20 73 74 6f 72 65 54 79 70 65 49 6e 66  ine storeTypeInf
16195 6f 28 41 2c 42 29 20 5f 73 74 6f 72 65 54 79 70  o(A,B) _storeTyp
16196 65 49 6e 66 6f 28 41 29 0a 73 74 61 74 69 63 20  eInfo(A).static 
16197 76 6f 69 64 20 5f 73 74 6f 72 65 54 79 70 65 49  void _storeTypeI
16198 6e 66 6f 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a  nfo(Mem *pMem){.
16199 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 4d    int flags = pM
1619a 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28  em->flags;.  if(
1619b 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c   flags & MEM_Nul
1619c 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74  l ){.    pMem->t
1619d 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c  ype = SQLITE_NUL
1619e 4c 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 20 69 66  L;.  }.  else if
1619f 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ( flags & MEM_In
161a0 74 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74  t ){.    pMem->t
161a1 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ype = SQLITE_INT
161a2 45 47 45 52 3b 0a 20 20 7d 0a 20 20 65 6c 73 65  EGER;.  }.  else
161a3 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d   if( flags & MEM
161a4 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 70 4d 65  _Real ){.    pMe
161a5 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  m->type = SQLITE
161a6 5f 46 4c 4f 41 54 3b 0a 20 20 7d 0a 20 20 65 6c  _FLOAT;.  }.  el
161a7 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d  se if( flags & M
161a8 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 70 4d  EM_Str ){.    pM
161a9 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54  em->type = SQLIT
161aa 45 5f 54 45 58 54 3b 0a 20 20 7d 65 6c 73 65 7b  E_TEXT;.  }else{
161ab 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20  .    pMem->type 
161ac 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20  = SQLITE_BLOB;. 
161ad 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 70   }.}../*.** Prop
161ae 65 72 74 69 65 73 20 6f 66 20 6f 70 63 6f 64 65  erties of opcode
161af 73 2e 20 20 54 68 65 20 4f 50 46 4c 47 5f 49 4e  s.  The OPFLG_IN
161b0 49 54 49 41 4c 49 5a 45 52 20 6d 61 63 72 6f 20  ITIALIZER macro 
161b1 69 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79  is.** created by
161b2 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 64   mkopcodeh.awk d
161b3 75 72 69 6e 67 20 63 6f 6d 70 69 6c 61 74 69 6f  uring compilatio
161b4 6e 2e 20 20 44 61 74 61 20 69 73 20 6f 62 74 61  n.  Data is obta
161b5 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65  ined.** from the
161b6 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f 77   comments follow
161b7 69 6e 67 20 74 68 65 20 22 63 61 73 65 20 4f 50  ing the "case OP
161b8 5f 78 78 78 78 3a 22 20 73 74 61 74 65 6d 65 6e  _xxxx:" statemen
161b9 74 73 20 69 6e 0a 2a 2a 20 74 68 69 73 20 66 69  ts in.** this fi
161ba 6c 65 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20  le.  .*/.static 
161bb 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6f 70  unsigned char op
161bc 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 5d 20 3d  codeProperty[] =
161bd 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a   OPFLG_INITIALIZ
161be 45 52 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ER;../*.** Retur
161bf 6e 20 74 72 75 65 20 69 66 20 61 6e 20 6f 70 63  n true if an opc
161c0 6f 64 65 20 68 61 73 20 61 6e 79 20 6f 66 20 74  ode has any of t
161c1 68 65 20 4f 50 46 4c 47 5f 78 78 78 20 70 72 6f  he OPFLG_xxx pro
161c2 70 65 72 74 69 65 73 0a 2a 2a 20 73 70 65 63 69  perties.** speci
161c3 66 69 65 64 20 62 79 20 6d 61 73 6b 2e 0a 2a 2f  fied by mask..*/
161c4 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
161c5 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4f  int sqlite3VdbeO
161c6 70 63 6f 64 65 48 61 73 50 72 6f 70 65 72 74 79  pcodeHasProperty
161c7 28 69 6e 74 20 6f 70 63 6f 64 65 2c 20 69 6e 74  (int opcode, int
161c8 20 6d 61 73 6b 29 7b 0a 20 20 61 73 73 65 72 74   mask){.  assert
161c9 28 20 6f 70 63 6f 64 65 3e 30 20 26 26 20 6f 70  ( opcode>0 && op
161ca 63 6f 64 65 3c 73 69 7a 65 6f 66 28 6f 70 63 6f  code<sizeof(opco
161cb 64 65 50 72 6f 70 65 72 74 79 29 20 29 3b 0a 20  deProperty) );. 
161cc 20 72 65 74 75 72 6e 20 28 6f 70 63 6f 64 65 50   return (opcodeP
161cd 72 6f 70 65 72 74 79 5b 6f 70 63 6f 64 65 5d 26  roperty[opcode]&
161ce 6d 61 73 6b 29 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a  mask)!=0;.}../*.
161cf 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 63 75 72 73  ** Allocate curs
161d0 6f 72 20 6e 75 6d 62 65 72 20 69 43 75 72 2e 20  or number iCur. 
161d1 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
161d2 72 20 74 6f 20 69 74 2e 20 20 52 65 74 75 72 6e  r to it.  Return
161d3 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 77 65 20 72   NULL.** if we r
161d4 75 6e 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79  un out of memory
161d5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 43 75 72 73  ..*/.static Curs
161d6 6f 72 20 2a 61 6c 6c 6f 63 61 74 65 43 75 72 73  or *allocateCurs
161d7 6f 72 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 0a  or(.  Vdbe *p, .
161d8 20 20 69 6e 74 20 69 43 75 72 2c 20 0a 20 20 4f    int iCur, .  O
161d9 70 20 2a 70 4f 70 2c 0a 20 20 69 6e 74 20 69 44  p *pOp,.  int iD
161da 62 2c 20 0a 20 20 69 6e 74 20 69 73 42 74 72 65  b, .  int isBtre
161db 65 43 75 72 73 6f 72 0a 29 7b 0a 20 20 2f 2a 20  eCursor.){.  /* 
161dc 46 69 6e 64 20 74 68 65 20 6d 65 6d 6f 72 79 20  Find the memory 
161dd 63 65 6c 6c 20 74 68 61 74 20 77 69 6c 6c 20 62  cell that will b
161de 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  e used to store 
161df 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f  the blob of memo
161e0 72 79 0a 20 20 2a 2a 20 72 65 71 75 69 72 65 64  ry.  ** required
161e1 20 66 6f 72 20 74 68 69 73 20 43 75 72 73 6f 72   for this Cursor
161e2 20 73 74 72 75 63 74 75 72 65 2e 20 49 74 20 69   structure. It i
161e3 73 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20  s convenient to 
161e4 75 73 65 20 61 20 0a 20 20 2a 2a 20 76 64 62 65  use a .  ** vdbe
161e5 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20   memory cell to 
161e6 6d 61 6e 61 67 65 20 74 68 65 20 6d 65 6d 6f 72  manage the memor
161e7 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71  y allocation req
161e8 75 69 72 65 64 20 66 6f 72 20 61 0a 20 20 2a 2a  uired for a.  **
161e9 20 43 75 72 73 6f 72 20 73 74 72 75 63 74 75 72   Cursor structur
161ea 65 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77  e for the follow
161eb 69 6e 67 20 72 65 61 73 6f 6e 73 3a 0a 20 20 2a  ing reasons:.  *
161ec 2a 0a 20 20 2a 2a 20 20 20 2a 20 53 6f 6d 65 74  *.  **   * Somet
161ed 69 6d 65 73 20 63 75 72 73 6f 72 20 6e 75 6d 62  imes cursor numb
161ee 65 72 73 20 61 72 65 20 75 73 65 64 20 66 6f 72  ers are used for
161ef 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 64 69 66   a couple of dif
161f0 66 65 72 65 6e 74 0a 20 20 2a 2a 20 20 20 20 20  ferent.  **     
161f1 70 75 72 70 6f 73 65 73 20 69 6e 20 61 20 76 64  purposes in a vd
161f2 62 65 20 70 72 6f 67 72 61 6d 2e 20 54 68 65 20  be program. The 
161f3 64 69 66 66 65 72 65 6e 74 20 75 73 65 73 20 6d  different uses m
161f4 69 67 68 74 20 72 65 71 75 69 72 65 0a 20 20 2a  ight require.  *
161f5 2a 20 20 20 20 20 64 69 66 66 65 72 65 6e 74 20  *     different 
161f6 73 69 7a 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e  sized allocation
161f7 73 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20  s. Memory cells 
161f8 70 72 6f 76 69 64 65 20 67 72 6f 77 61 62 6c 65  provide growable
161f9 0a 20 20 2a 2a 20 20 20 20 20 61 6c 6c 6f 63 61  .  **     alloca
161fa 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  tions..  **.  **
161fb 20 20 20 2a 20 57 68 65 6e 20 75 73 69 6e 67 20     * When using 
161fc 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41  ENABLE_MEMORY_MA
161fd 4e 41 47 45 4d 45 4e 54 2c 20 6d 65 6d 6f 72 79  NAGEMENT, memory
161fe 20 63 65 6c 6c 20 62 75 66 66 65 72 73 20 63 61   cell buffers ca
161ff 6e 0a 20 20 2a 2a 20 20 20 20 20 62 65 20 66 72  n.  **     be fr
16200 65 65 64 20 6c 61 7a 69 6c 79 20 76 69 61 20 74  eed lazily via t
16201 68 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61  he sqlite3_relea
16202 73 65 5f 6d 65 6d 6f 72 79 28 29 20 41 50 49 2e  se_memory() API.
16203 20 54 68 69 73 0a 20 20 2a 2a 20 20 20 20 20 6d   This.  **     m
16204 69 6e 69 6d 69 7a 65 73 20 74 68 65 20 6e 75 6d  inimizes the num
16205 62 65 72 20 6f 66 20 6d 61 6c 6c 6f 63 20 63 61  ber of malloc ca
16206 6c 6c 73 20 6d 61 64 65 20 62 79 20 74 68 65 20  lls made by the 
16207 73 79 73 74 65 6d 2e 0a 20 20 2a 2a 0a 20 20 2a  system..  **.  *
16208 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66  * Memory cells f
16209 6f 72 20 63 75 72 73 6f 72 73 20 61 72 65 20 61  or cursors are a
1620a 6c 6c 6f 63 61 74 65 64 20 61 74 20 74 68 65 20  llocated at the 
1620b 74 6f 70 20 6f 66 20 74 68 65 20 61 64 64 72 65  top of the addre
1620c 73 73 0a 20 20 2a 2a 20 73 70 61 63 65 2e 20 4d  ss.  ** space. M
1620d 65 6d 6f 72 79 20 63 65 6c 6c 20 28 70 2d 3e 6e  emory cell (p->n
1620e 4d 65 6d 29 20 63 6f 72 72 65 73 70 6f 6e 64 73  Mem) corresponds
1620f 20 74 6f 20 63 75 72 73 6f 72 20 30 2e 20 53 70   to cursor 0. Sp
16210 61 63 65 20 66 6f 72 0a 20 20 2a 2a 20 63 75 72  ace for.  ** cur
16211 73 6f 72 20 31 20 69 73 20 6d 61 6e 61 67 65 64  sor 1 is managed
16212 20 62 79 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20   by memory cell 
16213 28 70 2d 3e 6e 4d 65 6d 2d 31 29 2c 20 65 74 63  (p->nMem-1), etc
16214 2e 0a 20 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d  ..  */.  Mem *pM
16215 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 2d  em = &p->aMem[p-
16216 3e 6e 4d 65 6d 2d 69 43 75 72 5d 3b 0a 0a 20 20  >nMem-iCur];..  
16217 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 43 75 72  int nByte;.  Cur
16218 73 6f 72 20 2a 70 43 78 20 3d 20 30 3b 0a 20 20  sor *pCx = 0;.  
16219 2f 2a 20 49 66 20 74 68 65 20 6f 70 63 6f 64 65  /* If the opcode
1621a 20 6f 66 20 70 4f 70 20 69 73 20 4f 50 5f 53 65   of pOp is OP_Se
1621b 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 74 68 65  tNumColumns, the
1621c 6e 20 70 4f 70 2d 3e 70 32 20 63 6f 6e 74 61 69  n pOp->p2 contai
1621d 6e 73 0a 20 20 2a 2a 20 74 68 65 20 6e 75 6d 62  ns.  ** the numb
1621e 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20  er of fields in 
1621f 74 68 65 20 72 65 63 6f 72 64 73 20 63 6f 6e 74  the records cont
16220 61 69 6e 65 64 20 69 6e 20 74 68 65 20 74 61 62  ained in the tab
16221 6c 65 20 6f 72 0a 20 20 2a 2a 20 69 6e 64 65 78  le or.  ** index
16222 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 2e 20 55   being opened. U
16223 73 65 20 74 68 69 73 20 74 6f 20 72 65 73 65 72  se this to reser
16224 76 65 20 73 70 61 63 65 20 66 6f 72 20 74 68 65  ve space for the
16225 20 0a 20 20 2a 2a 20 43 75 72 73 6f 72 2e 61 54   .  ** Cursor.aT
16226 79 70 65 5b 5d 20 61 72 72 61 79 2e 0a 20 20 2a  ype[] array..  *
16227 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 20 3d  /.  int nField =
16228 20 30 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f   0;.  if( pOp->o
16229 70 63 6f 64 65 3d 3d 4f 50 5f 53 65 74 4e 75 6d  pcode==OP_SetNum
1622a 43 6f 6c 75 6d 6e 73 20 7c 7c 20 70 4f 70 2d 3e  Columns || pOp->
1622b 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e 45  opcode==OP_OpenE
1622c 70 68 65 6d 65 72 61 6c 20 29 7b 0a 20 20 20 20  phemeral ){.    
1622d 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 32  nField = pOp->p2
1622e 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20  ;.  }.  nByte = 
1622f 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 43 75  .      sizeof(Cu
16230 72 73 6f 72 29 20 2b 20 0a 20 20 20 20 20 20 28  rsor) + .      (
16231 69 73 42 74 72 65 65 43 75 72 73 6f 72 3f 73 71  isBtreeCursor?sq
16232 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
16233 53 69 7a 65 28 29 3a 30 29 20 2b 20 0a 20 20 20  Size():0) + .   
16234 20 20 20 32 2a 6e 46 69 65 6c 64 2a 73 69 7a 65     2*nField*size
16235 6f 66 28 75 33 32 29 3b 0a 0a 20 20 61 73 73 65  of(u32);..  asse
16236 72 74 28 20 69 43 75 72 3c 70 2d 3e 6e 43 75 72  rt( iCur<p->nCur
16237 73 6f 72 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e  sor );.  if( p->
16238 61 70 43 73 72 5b 69 43 75 72 5d 20 29 7b 0a 20  apCsr[iCur] ){. 
16239 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 72     sqlite3VdbeFr
1623a 65 65 43 75 72 73 6f 72 28 70 2c 20 70 2d 3e 61  eeCursor(p, p->a
1623b 70 43 73 72 5b 69 43 75 72 5d 29 3b 0a 20 20 20  pCsr[iCur]);.   
1623c 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 20   p->apCsr[iCur] 
1623d 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53  = 0;.  }.  if( S
1623e 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65  QLITE_OK==sqlite
1623f 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65  3VdbeMemGrow(pMe
16240 6d 2c 20 6e 42 79 74 65 2c 20 30 29 20 29 7b 0a  m, nByte, 0) ){.
16241 20 20 20 20 70 2d 3e 61 70 43 73 72 5b 69 43 75      p->apCsr[iCu
16242 72 5d 20 3d 20 70 43 78 20 3d 20 28 43 75 72 73  r] = pCx = (Curs
16243 6f 72 20 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20  or *)pMem->z;.  
16244 20 20 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a    memset(pMem->z
16245 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20  , 0, nByte);.   
16246 20 70 43 78 2d 3e 69 44 62 20 3d 20 69 44 62 3b   pCx->iDb = iDb;
16247 0a 20 20 20 20 70 43 78 2d 3e 6e 46 69 65 6c 64  .    pCx->nField
16248 20 3d 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20 69   = nField;.    i
16249 66 28 20 6e 46 69 65 6c 64 20 29 7b 0a 20 20 20  f( nField ){.   
1624a 20 20 20 70 43 78 2d 3e 61 54 79 70 65 20 3d 20     pCx->aType = 
1624b 28 75 33 32 20 2a 29 26 70 4d 65 6d 2d 3e 7a 5b  (u32 *)&pMem->z[
1624c 73 69 7a 65 6f 66 28 43 75 72 73 6f 72 29 5d 3b  sizeof(Cursor)];
1624d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69  .    }.    if( i
1624e 73 42 74 72 65 65 43 75 72 73 6f 72 20 29 7b 0a  sBtreeCursor ){.
1624f 20 20 20 20 20 20 70 43 78 2d 3e 70 43 75 72 73        pCx->pCurs
16250 6f 72 20 3d 20 28 42 74 43 75 72 73 6f 72 20 2a  or = (BtCursor *
16251 29 26 70 4d 65 6d 2d 3e 7a 5b 73 69 7a 65 6f 66  )&pMem->z[sizeof
16252 28 43 75 72 73 6f 72 29 2b 32 2a 6e 46 69 65 6c  (Cursor)+2*nFiel
16253 64 2a 73 69 7a 65 6f 66 28 75 33 32 29 5d 3b 0a  d*sizeof(u32)];.
16254 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
16255 72 6e 20 70 43 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rn pCx;.}../*.**
16256 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20   Try to convert 
16257 61 20 76 61 6c 75 65 20 69 6e 74 6f 20 61 20 6e  a value into a n
16258 75 6d 65 72 69 63 20 72 65 70 72 65 73 65 6e 74  umeric represent
16259 61 74 69 6f 6e 20 69 66 20 77 65 20 63 61 6e 0a  ation if we can.
1625a 2a 2a 20 64 6f 20 73 6f 20 77 69 74 68 6f 75 74  ** do so without
1625b 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61   loss of informa
1625c 74 69 6f 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20  tion.  In other 
1625d 77 6f 72 64 73 2c 20 69 66 20 74 68 65 20 73 74  words, if the st
1625e 72 69 6e 67 0a 2a 2a 20 6c 6f 6f 6b 73 20 6c 69  ring.** looks li
1625f 6b 65 20 61 20 6e 75 6d 62 65 72 2c 20 63 6f 6e  ke a number, con
16260 76 65 72 74 20 69 74 20 69 6e 74 6f 20 61 20 6e  vert it into a n
16261 75 6d 62 65 72 2e 20 20 49 66 20 69 74 20 64 6f  umber.  If it do
16262 65 73 20 6e 6f 74 0a 2a 2a 20 6c 6f 6f 6b 20 6c  es not.** look l
16263 69 6b 65 20 61 20 6e 75 6d 62 65 72 2c 20 6c 65  ike a number, le
16264 61 76 65 20 69 74 20 61 6c 6f 6e 65 2e 0a 2a 2f  ave it alone..*/
16265 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 70 70  .static void app
16266 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74  lyNumericAffinit
16267 79 28 4d 65 6d 20 2a 70 52 65 63 29 7b 0a 20 20  y(Mem *pRec){.  
16268 69 66 28 20 28 70 52 65 63 2d 3e 66 6c 61 67 73  if( (pRec->flags
16269 20 26 20 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d   & (MEM_Real|MEM
1626a 5f 49 6e 74 29 29 3d 3d 30 20 29 7b 0a 20 20 20  _Int))==0 ){.   
1626b 20 69 6e 74 20 72 65 61 6c 6e 75 6d 3b 0a 20 20   int realnum;.  
1626c 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1626d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 70 52 65  NulTerminate(pRe
1626e 63 29 3b 0a 20 20 20 20 69 66 28 20 28 70 52 65  c);.    if( (pRe
1626f 63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72  c->flags&MEM_Str
16270 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 73 71  ).         && sq
16271 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 70 52  lite3IsNumber(pR
16272 65 63 2d 3e 7a 2c 20 26 72 65 61 6c 6e 75 6d 2c  ec->z, &realnum,
16273 20 70 52 65 63 2d 3e 65 6e 63 29 20 29 7b 0a 20   pRec->enc) ){. 
16274 20 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a       i64 value;.
16275 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
16276 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28  eChangeEncoding(
16277 70 52 65 63 2c 20 53 51 4c 49 54 45 5f 55 54 46  pRec, SQLITE_UTF
16278 38 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 72  8);.      if( !r
16279 65 61 6c 6e 75 6d 20 26 26 20 73 71 6c 69 74 65  ealnum && sqlite
1627a 33 41 74 6f 69 36 34 28 70 52 65 63 2d 3e 7a 2c  3Atoi64(pRec->z,
1627b 20 26 76 61 6c 75 65 29 20 29 7b 0a 20 20 20 20   &value) ){.    
1627c 20 20 20 20 70 52 65 63 2d 3e 75 2e 69 20 3d 20      pRec->u.i = 
1627d 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 20 20 4d  value;.        M
1627e 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 52  emSetTypeFlag(pR
1627f 65 63 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20  ec, MEM_Int);.  
16280 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
16281 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
16282 6d 52 65 61 6c 69 66 79 28 70 52 65 63 29 3b 0a  mRealify(pRec);.
16283 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
16284 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65  }.}../*.** Proce
16285 73 73 69 6e 67 20 69 73 20 64 65 74 65 72 6d 69  ssing is determi
16286 6e 65 20 62 79 20 74 68 65 20 61 66 66 69 6e 69  ne by the affini
16287 74 79 20 70 61 72 61 6d 65 74 65 72 3a 0a 2a 2a  ty parameter:.**
16288 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 49  .** SQLITE_AFF_I
16289 4e 54 45 47 45 52 3a 0a 2a 2a 20 53 51 4c 49 54  NTEGER:.** SQLIT
1628a 45 5f 41 46 46 5f 52 45 41 4c 3a 0a 2a 2a 20 53  E_AFF_REAL:.** S
1628b 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49  QLITE_AFF_NUMERI
1628c 43 3a 0a 2a 2a 20 20 20 20 54 72 79 20 74 6f 20  C:.**    Try to 
1628d 63 6f 6e 76 65 72 74 20 70 52 65 63 20 74 6f 20  convert pRec to 
1628e 61 6e 20 69 6e 74 65 67 65 72 20 72 65 70 72 65  an integer repre
1628f 73 65 6e 74 61 74 69 6f 6e 20 6f 72 20 61 20 0a  sentation or a .
16290 2a 2a 20 20 20 20 66 6c 6f 61 74 69 6e 67 2d 70  **    floating-p
16291 6f 69 6e 74 20 72 65 70 72 65 73 65 6e 74 61 74  oint representat
16292 69 6f 6e 20 69 66 20 61 6e 20 69 6e 74 65 67 65  ion if an intege
16293 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  r representation
16294 0a 2a 2a 20 20 20 20 69 73 20 6e 6f 74 20 70 6f  .**    is not po
16295 73 73 69 62 6c 65 2e 20 20 4e 6f 74 65 20 74 68  ssible.  Note th
16296 61 74 20 74 68 65 20 69 6e 74 65 67 65 72 20 72  at the integer r
16297 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 73  epresentation is
16298 0a 2a 2a 20 20 20 20 61 6c 77 61 79 73 20 70 72  .**    always pr
16299 65 66 65 72 72 65 64 2c 20 65 76 65 6e 20 69 66  eferred, even if
1629a 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 69 73   the affinity is
1629b 20 52 45 41 4c 2c 20 62 65 63 61 75 73 65 0a 2a   REAL, because.*
1629c 2a 20 20 20 20 61 6e 20 69 6e 74 65 67 65 72 20  *    an integer 
1629d 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69  representation i
1629e 73 20 6d 6f 72 65 20 73 70 61 63 65 20 65 66 66  s more space eff
1629f 69 63 69 65 6e 74 20 6f 6e 20 64 69 73 6b 2e 0a  icient on disk..
162a0 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46  **.** SQLITE_AFF
162a1 5f 54 45 58 54 3a 0a 2a 2a 20 20 20 20 43 6f 6e  _TEXT:.**    Con
162a2 76 65 72 74 20 70 52 65 63 20 74 6f 20 61 20 74  vert pRec to a t
162a3 65 78 74 20 72 65 70 72 65 73 65 6e 74 61 74 69  ext representati
162a4 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45  on..**.** SQLITE
162a5 5f 41 46 46 5f 4e 4f 4e 45 3a 0a 2a 2a 20 20 20  _AFF_NONE:.**   
162a6 20 4e 6f 2d 6f 70 2e 20 20 70 52 65 63 20 69 73   No-op.  pRec is
162a7 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 73   unchanged..*/.s
162a8 74 61 74 69 63 20 76 6f 69 64 20 61 70 70 6c 79  tatic void apply
162a9 41 66 66 69 6e 69 74 79 28 0a 20 20 4d 65 6d 20  Affinity(.  Mem 
162aa 2a 70 52 65 63 2c 20 20 20 20 20 20 20 20 20 20  *pRec,          
162ab 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 74 6f 20  /* The value to 
162ac 61 70 70 6c 79 20 61 66 66 69 6e 69 74 79 20 74  apply affinity t
162ad 6f 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69  o */.  char affi
162ae 6e 69 74 79 2c 20 20 20 20 20 20 2f 2a 20 54 68  nity,      /* Th
162af 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20 62 65  e affinity to be
162b0 20 61 70 70 6c 69 65 64 20 2a 2f 0a 20 20 75 38   applied */.  u8
162b1 20 65 6e 63 20 20 20 20 20 20 20 20 20 20 20 20   enc            
162b2 20 20 2f 2a 20 55 73 65 20 74 68 69 73 20 74 65    /* Use this te
162b3 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 29  xt encoding */.)
162b4 7b 0a 20 20 69 66 28 20 61 66 66 69 6e 69 74 79  {.  if( affinity
162b5 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  ==SQLITE_AFF_TEX
162b6 54 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79  T ){.    /* Only
162b7 20 61 74 74 65 6d 70 74 20 74 68 65 20 63 6f 6e   attempt the con
162b8 76 65 72 73 69 6f 6e 20 74 6f 20 54 45 58 54 20  version to TEXT 
162b9 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 69  if there is an i
162ba 6e 74 65 67 65 72 20 6f 72 20 72 65 61 6c 0a 20  nteger or real. 
162bb 20 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 61     ** representa
162bc 74 69 6f 6e 20 28 62 6c 6f 62 20 61 6e 64 20 4e  tion (blob and N
162bd 55 4c 4c 20 64 6f 20 6e 6f 74 20 67 65 74 20 63  ULL do not get c
162be 6f 6e 76 65 72 74 65 64 29 20 62 75 74 20 6e 6f  onverted) but no
162bf 20 73 74 72 69 6e 67 0a 20 20 20 20 2a 2a 20 72   string.    ** r
162c0 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 20  epresentation.. 
162c1 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 30 3d     */.    if( 0=
162c2 3d 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45  =(pRec->flags&ME
162c3 4d 5f 53 74 72 29 20 26 26 20 28 70 52 65 63 2d  M_Str) && (pRec-
162c4 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 52 65 61 6c  >flags&(MEM_Real
162c5 7c 4d 45 4d 5f 49 6e 74 29 29 20 29 7b 0a 20 20  |MEM_Int)) ){.  
162c6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
162c7 65 6d 53 74 72 69 6e 67 69 66 79 28 70 52 65 63  emStringify(pRec
162c8 2c 20 65 6e 63 29 3b 0a 20 20 20 20 7d 0a 20 20  , enc);.    }.  
162c9 20 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 3d    pRec->flags &=
162ca 20 7e 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f   ~(MEM_Real|MEM_
162cb 49 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  Int);.  }else if
162cc 28 20 61 66 66 69 6e 69 74 79 21 3d 53 51 4c 49  ( affinity!=SQLI
162cd 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20  TE_AFF_NONE ){. 
162ce 20 20 20 61 73 73 65 72 74 28 20 61 66 66 69 6e     assert( affin
162cf 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  ity==SQLITE_AFF_
162d0 49 4e 54 45 47 45 52 20 7c 7c 20 61 66 66 69 6e  INTEGER || affin
162d1 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  ity==SQLITE_AFF_
162d2 52 45 41 4c 0a 20 20 20 20 20 20 20 20 20 20 20  REAL.           
162d3 20 20 7c 7c 20 61 66 66 69 6e 69 74 79 3d 3d 53    || affinity==S
162d4 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49  QLITE_AFF_NUMERI
162d5 43 20 29 3b 0a 20 20 20 20 61 70 70 6c 79 4e 75  C );.    applyNu
162d6 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 52  mericAffinity(pR
162d7 65 63 29 3b 0a 20 20 20 20 69 66 28 20 70 52 65  ec);.    if( pRe
162d8 63 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  c->flags & MEM_R
162d9 65 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  eal ){.      sql
162da 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 72 41  ite3VdbeIntegerA
162db 66 66 69 6e 69 74 79 28 70 52 65 63 29 3b 0a 20  ffinity(pRec);. 
162dc 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
162dd 2a 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 72 74  * Try to convert
162de 20 74 68 65 20 74 79 70 65 20 6f 66 20 61 20 66   the type of a f
162df 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74  unction argument
162e0 20 6f 72 20 61 20 72 65 73 75 6c 74 20 63 6f 6c   or a result col
162e1 75 6d 6e 0a 2a 2a 20 69 6e 74 6f 20 61 20 6e 75  umn.** into a nu
162e2 6d 65 72 69 63 20 72 65 70 72 65 73 65 6e 74 61  meric representa
162e3 74 69 6f 6e 2e 20 20 55 73 65 20 65 69 74 68 65  tion.  Use eithe
162e4 72 20 49 4e 54 45 47 45 52 20 6f 72 20 52 45 41  r INTEGER or REA
162e5 4c 20 77 68 69 63 68 65 76 65 72 0a 2a 2a 20 69  L whichever.** i
162e6 73 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20 20  s appropriate.  
162e7 42 75 74 20 6f 6e 6c 79 20 64 6f 20 74 68 65 20  But only do the 
162e8 63 6f 6e 76 65 72 73 69 6f 6e 20 69 66 20 69 74  conversion if it
162e9 20 69 73 20 70 6f 73 73 69 62 6c 65 20 77 69 74   is possible wit
162ea 68 6f 75 74 0a 2a 2a 20 6c 6f 73 73 20 6f 66 20  hout.** loss of 
162eb 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 6e 64 20  information and 
162ec 72 65 74 75 72 6e 20 74 68 65 20 72 65 76 69 73  return the revis
162ed 65 64 20 74 79 70 65 20 6f 66 20 74 68 65 20 61  ed type of the a
162ee 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  rgument..**.** T
162ef 68 69 73 20 69 73 20 61 6e 20 45 58 50 45 52 49  his is an EXPERI
162f0 4d 45 4e 54 41 4c 20 61 70 69 20 61 6e 64 20 69  MENTAL api and i
162f1 73 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61  s subject to cha
162f2 6e 67 65 20 6f 72 20 72 65 6d 6f 76 61 6c 2e 0a  nge or removal..
162f3 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
162f4 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  t sqlite3_value_
162f5 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c  numeric_type(sql
162f6 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c  ite3_value *pVal
162f7 29 7b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d  ){.  Mem *pMem =
162f8 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 61   (Mem*)pVal;.  a
162f9 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e  pplyNumericAffin
162fa 69 74 79 28 70 4d 65 6d 29 3b 0a 20 20 73 74 6f  ity(pMem);.  sto
162fb 72 65 54 79 70 65 49 6e 66 6f 28 70 4d 65 6d 2c  reTypeInfo(pMem,
162fc 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4d   0);.  return pM
162fd 65 6d 2d 3e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a  em->type;.}../*.
162fe 2a 2a 20 45 78 70 6f 72 74 65 64 20 76 65 72 73  ** Exported vers
162ff 69 6f 6e 20 6f 66 20 61 70 70 6c 79 41 66 66 69  ion of applyAffi
16300 6e 69 74 79 28 29 2e 20 54 68 69 73 20 6f 6e 65  nity(). This one
16301 20 77 6f 72 6b 73 20 6f 6e 20 73 71 6c 69 74 65   works on sqlite
16302 33 5f 76 61 6c 75 65 2a 2c 20 0a 2a 2a 20 6e 6f  3_value*, .** no
16303 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 4d  t the internal M
16304 65 6d 2a 20 74 79 70 65 2e 0a 2a 2f 0a 53 51 4c  em* type..*/.SQL
16305 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
16306 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70 70   sqlite3ValueApp
16307 6c 79 41 66 66 69 6e 69 74 79 28 0a 20 20 73 71  lyAffinity(.  sq
16308 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
16309 6c 2c 20 0a 20 20 75 38 20 61 66 66 69 6e 69 74  l, .  u8 affinit
1630a 79 2c 20 0a 20 20 75 38 20 65 6e 63 0a 29 7b 0a  y, .  u8 enc.){.
1630b 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28    applyAffinity(
1630c 28 4d 65 6d 20 2a 29 70 56 61 6c 2c 20 61 66 66  (Mem *)pVal, aff
1630d 69 6e 69 74 79 2c 20 65 6e 63 29 3b 0a 7d 0a 0a  inity, enc);.}..
1630e 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
1630f 42 55 47 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  BUG./*.** Write 
16310 61 20 6e 69 63 65 20 73 74 72 69 6e 67 20 72 65  a nice string re
16311 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20  presentation of 
16312 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
16313 63 65 6c 6c 20 70 4d 65 6d 0a 2a 2a 20 69 6e 74  cell pMem.** int
16314 6f 20 62 75 66 66 65 72 20 7a 42 75 66 2c 20 6c  o buffer zBuf, l
16315 65 6e 67 74 68 20 6e 42 75 66 2e 0a 2a 2f 0a 53  ength nBuf..*/.S
16316 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
16317 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  id sqlite3VdbeMe
16318 6d 50 72 65 74 74 79 50 72 69 6e 74 28 4d 65 6d  mPrettyPrint(Mem
16319 20 2a 70 4d 65 6d 2c 20 63 68 61 72 20 2a 7a 42   *pMem, char *zB
1631a 75 66 29 7b 0a 20 20 63 68 61 72 20 2a 7a 43 73  uf){.  char *zCs
1631b 72 20 3d 20 7a 42 75 66 3b 0a 20 20 69 6e 74 20  r = zBuf;.  int 
1631c 66 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b  f = pMem->flags;
1631d 0a 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ..  static const
1631e 20 63 68 61 72 20 2a 63 6f 6e 73 74 20 65 6e 63   char *const enc
1631f 6e 61 6d 65 73 5b 5d 20 3d 20 7b 22 28 58 29 22  names[] = {"(X)"
16320 2c 20 22 28 38 29 22 2c 20 22 28 31 36 4c 45 29  , "(8)", "(16LE)
16321 22 2c 20 22 28 31 36 42 45 29 22 7d 3b 0a 0a 20  ", "(16BE)"};.. 
16322 20 69 66 28 20 66 26 4d 45 4d 5f 42 6c 6f 62 20   if( f&MEM_Blob 
16323 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  ){.    int i;.  
16324 20 20 63 68 61 72 20 63 3b 0a 20 20 20 20 69 66    char c;.    if
16325 28 20 66 20 26 20 4d 45 4d 5f 44 79 6e 20 29 7b  ( f & MEM_Dyn ){
16326 0a 20 20 20 20 20 20 63 20 3d 20 27 7a 27 3b 0a  .      c = 'z';.
16327 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 66        assert( (f
16328 20 26 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d   & (MEM_Static|M
16329 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b  EM_Ephem))==0 );
1632a 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66  .    }else if( f
1632b 20 26 20 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b   & MEM_Static ){
1632c 0a 20 20 20 20 20 20 63 20 3d 20 27 74 27 3b 0a  .      c = 't';.
1632d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 66        assert( (f
1632e 20 26 20 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f   & (MEM_Dyn|MEM_
1632f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20  Ephem))==0 );.  
16330 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20    }else if( f & 
16331 4d 45 4d 5f 45 70 68 65 6d 20 29 7b 0a 20 20 20  MEM_Ephem ){.   
16332 20 20 20 63 20 3d 20 27 65 27 3b 0a 20 20 20 20     c = 'e';.    
16333 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28    assert( (f & (
16334 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44  MEM_Static|MEM_D
16335 79 6e 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d  yn))==0 );.    }
16336 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 20 3d 20  else{.      c = 
16337 27 73 27 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  's';.    }..    
16338 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
16339 28 31 30 30 2c 20 7a 43 73 72 2c 20 22 25 63 22  (100, zCsr, "%c"
1633a 2c 20 63 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b  , c);.    zCsr +
1633b 3d 20 73 74 72 6c 65 6e 28 7a 43 73 72 29 3b 0a  = strlen(zCsr);.
1633c 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1633d 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c 20  intf(100, zCsr, 
1633e 22 25 64 5b 22 2c 20 70 4d 65 6d 2d 3e 6e 29 3b  "%d[", pMem->n);
1633f 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 74 72  .    zCsr += str
16340 6c 65 6e 28 7a 43 73 72 29 3b 0a 20 20 20 20 66  len(zCsr);.    f
16341 6f 72 28 69 3d 30 3b 20 69 3c 31 36 20 26 26 20  or(i=0; i<16 && 
16342 69 3c 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b  i<pMem->n; i++){
16343 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
16344 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73  nprintf(100, zCs
16345 72 2c 20 22 25 30 32 58 22 2c 20 28 28 69 6e 74  r, "%02X", ((int
16346 29 70 4d 65 6d 2d 3e 7a 5b 69 5d 20 26 20 30 78  )pMem->z[i] & 0x
16347 46 46 29 29 3b 0a 20 20 20 20 20 20 7a 43 73 72  FF));.      zCsr
16348 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 43 73 72 29   += strlen(zCsr)
16349 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28  ;.    }.    for(
1634a 69 3d 30 3b 20 69 3c 31 36 20 26 26 20 69 3c 70  i=0; i<16 && i<p
1634b 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Mem->n; i++){.  
1634c 20 20 20 20 63 68 61 72 20 7a 20 3d 20 70 4d 65      char z = pMe
1634d 6d 2d 3e 7a 5b 69 5d 3b 0a 20 20 20 20 20 20 69  m->z[i];.      i
1634e 66 28 20 7a 3c 33 32 20 7c 7c 20 7a 3e 31 32 36  f( z<32 || z>126
1634f 20 29 20 2a 7a 43 73 72 2b 2b 20 3d 20 27 2e 27   ) *zCsr++ = '.'
16350 3b 0a 20 20 20 20 20 20 65 6c 73 65 20 2a 7a 43  ;.      else *zC
16351 73 72 2b 2b 20 3d 20 7a 3b 0a 20 20 20 20 7d 0a  sr++ = z;.    }.
16352 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
16353 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c  rintf(100, zCsr,
16354 20 22 5d 25 73 22 2c 20 65 6e 63 6e 61 6d 65 73   "]%s", encnames
16355 5b 70 4d 65 6d 2d 3e 65 6e 63 5d 29 3b 0a 20 20  [pMem->enc]);.  
16356 20 20 7a 43 73 72 20 2b 3d 20 73 74 72 6c 65 6e    zCsr += strlen
16357 28 7a 43 73 72 29 3b 0a 20 20 20 20 69 66 28 20  (zCsr);.    if( 
16358 66 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a  f & MEM_Zero ){.
16359 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
1635a 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72  printf(100, zCsr
1635b 2c 22 2b 25 6c 6c 64 7a 22 2c 70 4d 65 6d 2d 3e  ,"+%lldz",pMem->
1635c 75 2e 69 29 3b 0a 20 20 20 20 20 20 7a 43 73 72  u.i);.      zCsr
1635d 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 43 73 72 29   += strlen(zCsr)
1635e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 7a 43 73  ;.    }.    *zCs
1635f 72 20 3d 20 27 5c 30 27 3b 0a 20 20 7d 65 6c 73  r = '\0';.  }els
16360 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 53 74  e if( f & MEM_St
16361 72 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 2c 20  r ){.    int j, 
16362 6b 3b 0a 20 20 20 20 7a 42 75 66 5b 30 5d 20 3d  k;.    zBuf[0] =
16363 20 27 20 27 3b 0a 20 20 20 20 69 66 28 20 66 20   ' ';.    if( f 
16364 26 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20  & MEM_Dyn ){.   
16365 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 7a 27     zBuf[1] = 'z'
16366 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
16367 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 69 63  (f & (MEM_Static
16368 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20  |MEM_Ephem))==0 
16369 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  );.    }else if(
1636a 20 66 20 26 20 4d 45 4d 5f 53 74 61 74 69 63 20   f & MEM_Static 
1636b 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d  ){.      zBuf[1]
1636c 20 3d 20 27 74 27 3b 0a 20 20 20 20 20 20 61 73   = 't';.      as
1636d 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f  sert( (f & (MEM_
1636e 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 3d  Dyn|MEM_Ephem))=
1636f 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  =0 );.    }else 
16370 69 66 28 20 66 20 26 20 4d 45 4d 5f 45 70 68 65  if( f & MEM_Ephe
16371 6d 20 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b  m ){.      zBuf[
16372 31 5d 20 3d 20 27 65 27 3b 0a 20 20 20 20 20 20  1] = 'e';.      
16373 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45  assert( (f & (ME
16374 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e  M_Static|MEM_Dyn
16375 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c  ))==0 );.    }el
16376 73 65 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 31  se{.      zBuf[1
16377 5d 20 3d 20 27 73 27 3b 0a 20 20 20 20 7d 0a 20  ] = 's';.    }. 
16378 20 20 20 6b 20 3d 20 32 3b 0a 20 20 20 20 73 71     k = 2;.    sq
16379 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31  lite3_snprintf(1
1637a 30 30 2c 20 26 7a 42 75 66 5b 6b 5d 2c 20 22 25  00, &zBuf[k], "%
1637b 64 22 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20  d", pMem->n);.  
1637c 20 20 6b 20 2b 3d 20 73 74 72 6c 65 6e 28 26 7a    k += strlen(&z
1637d 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a 42 75  Buf[k]);.    zBu
1637e 66 5b 6b 2b 2b 5d 20 3d 20 27 5b 27 3b 0a 20 20  f[k++] = '[';.  
1637f 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 31 35 20    for(j=0; j<15 
16380 26 26 20 6a 3c 70 4d 65 6d 2d 3e 6e 3b 20 6a 2b  && j<pMem->n; j+
16381 2b 29 7b 0a 20 20 20 20 20 20 75 38 20 63 20 3d  +){.      u8 c =
16382 20 70 4d 65 6d 2d 3e 7a 5b 6a 5d 3b 0a 20 20 20   pMem->z[j];.   
16383 20 20 20 69 66 28 20 63 3e 3d 30 78 32 30 20 26     if( c>=0x20 &
16384 26 20 63 3c 30 78 37 66 20 29 7b 0a 20 20 20 20  & c<0x7f ){.    
16385 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20      zBuf[k++] = 
16386 63 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  c;.      }else{.
16387 20 20 20 20 20 20 20 20 7a 42 75 66 5b 6b 2b 2b          zBuf[k++
16388 5d 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 7d  ] = '.';.      }
16389 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 75 66 5b  .    }.    zBuf[
1638a 6b 2b 2b 5d 20 3d 20 27 5d 27 3b 0a 20 20 20 20  k++] = ']';.    
1638b 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1638c 28 31 30 30 2c 26 7a 42 75 66 5b 6b 5d 2c 20 65  (100,&zBuf[k], e
1638d 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65 6e  ncnames[pMem->en
1638e 63 5d 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 73 74  c]);.    k += st
1638f 72 6c 65 6e 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a  rlen(&zBuf[k]);.
16390 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20      zBuf[k++] = 
16391 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a  0;.  }.}.#endif.
16392 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
16393 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74  EBUG./*.** Print
16394 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20   the value of a 
16395 72 65 67 69 73 74 65 72 20 66 6f 72 20 74 72 61  register for tra
16396 63 69 6e 67 20 70 75 72 70 6f 73 65 73 3a 0a 2a  cing purposes:.*
16397 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65  /.static void me
16398 6d 54 72 61 63 65 50 72 69 6e 74 28 46 49 4c 45  mTracePrint(FILE
16399 20 2a 6f 75 74 2c 20 4d 65 6d 20 2a 70 29 7b 0a   *out, Mem *p){.
1639a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26    if( p->flags &
1639b 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20   MEM_Null ){.   
1639c 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20   fprintf(out, " 
1639d 4e 55 4c 4c 22 29 3b 0a 20 20 7d 65 6c 73 65 20  NULL");.  }else 
1639e 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20  if( (p->flags & 
1639f 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72  (MEM_Int|MEM_Str
163a0 29 29 3d 3d 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d  ))==(MEM_Int|MEM
163a1 5f 53 74 72 29 20 29 7b 0a 20 20 20 20 66 70 72  _Str) ){.    fpr
163a2 69 6e 74 66 28 6f 75 74 2c 20 22 20 73 69 3a 25  intf(out, " si:%
163a3 6c 6c 64 22 2c 20 70 2d 3e 75 2e 69 29 3b 0a 20  lld", p->u.i);. 
163a4 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c   }else if( p->fl
163a5 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b  ags & MEM_Int ){
163a6 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74  .    fprintf(out
163a7 2c 20 22 20 69 3a 25 6c 6c 64 22 2c 20 70 2d 3e  , " i:%lld", p->
163a8 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  u.i);.  }else if
163a9 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  ( p->flags & MEM
163aa 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 66 70 72  _Real ){.    fpr
163ab 69 6e 74 66 28 6f 75 74 2c 20 22 20 72 3a 25 67  intf(out, " r:%g
163ac 22 2c 20 70 2d 3e 72 29 3b 0a 20 20 7d 65 6c 73  ", p->r);.  }els
163ad 65 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66  e{.    char zBuf
163ae 5b 32 30 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74  [200];.    sqlit
163af 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50  e3VdbeMemPrettyP
163b0 72 69 6e 74 28 70 2c 20 7a 42 75 66 29 3b 0a 20  rint(p, zBuf);. 
163b1 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
163b2 22 20 22 29 3b 0a 20 20 20 20 66 70 72 69 6e 74  " ");.    fprint
163b3 66 28 6f 75 74 2c 20 22 25 73 22 2c 20 7a 42 75  f(out, "%s", zBu
163b4 66 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63  f);.  }.}.static
163b5 20 76 6f 69 64 20 72 65 67 69 73 74 65 72 54 72   void registerTr
163b6 61 63 65 28 46 49 4c 45 20 2a 6f 75 74 2c 20 69  ace(FILE *out, i
163b7 6e 74 20 69 52 65 67 2c 20 4d 65 6d 20 2a 70 29  nt iReg, Mem *p)
163b8 7b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c  {.  fprintf(out,
163b9 20 22 52 45 47 5b 25 64 5d 20 3d 20 22 2c 20 69   "REG[%d] = ", i
163ba 52 65 67 29 3b 0a 20 20 6d 65 6d 54 72 61 63 65  Reg);.  memTrace
163bb 50 72 69 6e 74 28 6f 75 74 2c 20 70 29 3b 0a 20  Print(out, p);. 
163bc 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c   fprintf(out, "\
163bd 6e 22 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  n");.}.#endif..#
163be 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
163bf 55 47 0a 23 20 20 64 65 66 69 6e 65 20 52 45 47  UG.#  define REG
163c0 49 53 54 45 52 5f 54 52 41 43 45 28 52 2c 4d 29  ISTER_TRACE(R,M)
163c1 20 69 66 28 70 2d 3e 74 72 61 63 65 26 26 52 3e   if(p->trace&&R>
163c2 30 29 72 65 67 69 73 74 65 72 54 72 61 63 65 28  0)registerTrace(
163c3 70 2d 3e 74 72 61 63 65 2c 52 2c 4d 29 0a 23 65  p->trace,R,M).#e
163c4 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 52 45  lse.#  define RE
163c5 47 49 53 54 45 52 5f 54 52 41 43 45 28 52 2c 4d  GISTER_TRACE(R,M
163c6 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 64 65  ).#endif...#ifde
163c7 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 2f  f VDBE_PROFILE./
163c8 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
163c9 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20  ng routine only 
163ca 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d  works on pentium
163cb 2d 63 6c 61 73 73 20 70 72 6f 63 65 73 73 6f 72  -class processor
163cc 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68  s..** It uses th
163cd 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74  e RDTSC opcode t
163ce 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c 65  o read the cycle
163cf 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74   count value out
163d0 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65   of the.** proce
163d1 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73  ssor and returns
163d2 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68   that value.  Th
163d3 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 66  is can be used f
163d4 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70  or high-res.** p
163d5 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 5f 5f 69  rofiling..*/.__i
163d6 6e 6c 69 6e 65 5f 5f 20 75 6e 73 69 67 6e 65 64  nline__ unsigned
163d7 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 20 68   long long int h
163d8 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20  wtime(void){.   
163d9 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c  unsigned int lo,
163da 20 68 69 3b 0a 20 20 20 2f 2a 20 57 65 20 63 61   hi;.   /* We ca
163db 6e 6e 6f 74 20 75 73 65 20 22 3d 41 22 2c 20 73  nnot use "=A", s
163dc 69 6e 63 65 20 74 68 69 73 20 77 6f 75 6c 64 20  ince this would 
163dd 75 73 65 20 25 72 61 78 20 6f 6e 20 78 38 36 5f  use %rax on x86_
163de 36 34 20 2a 2f 0a 20 20 20 5f 5f 61 73 6d 5f 5f  64 */.   __asm__
163df 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22   __volatile__ ("
163e0 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c  rdtsc" : "=a" (l
163e1 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a  o), "=d" (hi));.
163e2 20 20 20 72 65 74 75 72 6e 20 28 75 6e 73 69 67     return (unsig
163e3 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e  ned long long in
163e4 74 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b  t)hi << 32 | lo;
163e5 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
163e6 20 54 68 65 20 43 48 45 43 4b 5f 46 4f 52 5f 49   The CHECK_FOR_I
163e7 4e 54 45 52 52 55 50 54 20 6d 61 63 72 6f 20 64  NTERRUPT macro d
163e8 65 66 69 6e 65 64 20 68 65 72 65 20 6c 6f 6f 6b  efined here look
163e9 73 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 0a  s to see if the.
163ea 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  ** sqlite3_inter
163eb 72 75 70 74 28 29 20 72 6f 75 74 69 6e 65 20 68  rupt() routine h
163ec 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2e 20  as been called. 
163ed 20 49 66 20 69 74 20 68 61 73 20 62 65 65 6e 2c   If it has been,
163ee 20 74 68 65 6e 0a 2a 2a 20 70 72 6f 63 65 73 73   then.** process
163ef 69 6e 67 20 6f 66 20 74 68 65 20 56 44 42 45 20  ing of the VDBE 
163f0 70 72 6f 67 72 61 6d 20 69 73 20 69 6e 74 65 72  program is inter
163f1 72 75 70 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  rupted..**.** Th
163f2 69 73 20 6d 61 63 72 6f 20 61 64 64 65 64 20 74  is macro added t
163f3 6f 20 65 76 65 72 79 20 69 6e 73 74 72 75 63 74  o every instruct
163f4 69 6f 6e 20 74 68 61 74 20 64 6f 65 73 20 61 20  ion that does a 
163f5 6a 75 6d 70 20 69 6e 20 6f 72 64 65 72 20 74 6f  jump in order to
163f6 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20  .** implement a 
163f7 6c 6f 6f 70 2e 20 20 54 68 69 73 20 74 65 73 74  loop.  This test
163f8 20 75 73 65 64 20 74 6f 20 62 65 20 6f 6e 20 65   used to be on e
163f9 76 65 72 79 20 73 69 6e 67 6c 65 20 69 6e 73 74  very single inst
163fa 72 75 63 74 69 6f 6e 2c 0a 2a 2a 20 62 75 74 20  ruction,.** but 
163fb 74 68 61 74 20 6d 65 61 6e 74 20 77 65 20 6d 6f  that meant we mo
163fc 72 65 20 74 65 73 74 69 6e 67 20 74 68 61 74 20  re testing that 
163fd 77 65 20 6e 65 65 64 65 64 2e 20 20 42 79 20 6f  we needed.  By o
163fe 6e 6c 79 20 74 65 73 74 69 6e 67 20 74 68 65 0a  nly testing the.
163ff 2a 2a 20 66 6c 61 67 20 6f 6e 20 6a 75 6d 70 20  ** flag on jump 
16400 69 6e 73 74 72 75 63 74 69 6f 6e 73 2c 20 77 65  instructions, we
16401 20 67 65 74 20 61 20 28 73 6d 61 6c 6c 29 20 73   get a (small) s
16402 70 65 65 64 20 69 6d 70 72 6f 76 65 6d 65 6e 74  peed improvement
16403 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 48 45  ..*/.#define CHE
16404 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54  CK_FOR_INTERRUPT
16405 20 5c 0a 20 20 20 69 66 28 20 64 62 2d 3e 75 31   \.   if( db->u1
16406 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 29  .isInterrupted )
16407 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
16408 74 6f 5f 69 6e 74 65 72 72 75 70 74 3b 0a 0a 0a  to_interrupt;...
16409 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 61 73  /*.** Execute as
1640a 20 6d 75 63 68 20 6f 66 20 61 20 56 44 42 45 20   much of a VDBE 
1640b 70 72 6f 67 72 61 6d 20 61 73 20 77 65 20 63 61  program as we ca
1640c 6e 20 74 68 65 6e 20 72 65 74 75 72 6e 2e 0a 2a  n then return..*
1640d 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65  *.** sqlite3Vdbe
1640e 4d 61 6b 65 52 65 61 64 79 28 29 20 6d 75 73 74  MakeReady() must
1640f 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72   be called befor
16410 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  e this routine i
16411 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 6c  n order to.** cl
16412 6f 73 65 20 74 68 65 20 70 72 6f 67 72 61 6d 20  ose the program 
16413 77 69 74 68 20 61 20 66 69 6e 61 6c 20 4f 50 5f  with a final OP_
16414 48 61 6c 74 20 61 6e 64 20 74 6f 20 73 65 74 20  Halt and to set 
16415 75 70 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 73  up the callbacks
16416 0a 2a 2a 20 61 6e 64 20 74 68 65 20 65 72 72 6f  .** and the erro
16417 72 20 6d 65 73 73 61 67 65 20 70 6f 69 6e 74 65  r message pointe
16418 72 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 65 76 65  r..**.** Wheneve
16419 72 20 61 20 72 6f 77 20 6f 72 20 72 65 73 75 6c  r a row or resul
1641a 74 20 64 61 74 61 20 69 73 20 61 76 61 69 6c 61  t data is availa
1641b 62 6c 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  ble, this routin
1641c 65 20 77 69 6c 6c 20 65 69 74 68 65 72 0a 2a 2a  e will either.**
1641d 20 69 6e 76 6f 6b 65 20 74 68 65 20 72 65 73 75   invoke the resu
1641e 6c 74 20 63 61 6c 6c 62 61 63 6b 20 28 69 66 20  lt callback (if 
1641f 74 68 65 72 65 20 69 73 20 6f 6e 65 29 20 6f 72  there is one) or
16420 20 72 65 74 75 72 6e 20 77 69 74 68 0a 2a 2a 20   return with.** 
16421 53 51 4c 49 54 45 5f 52 4f 57 2e 0a 2a 2a 0a 2a  SQLITE_ROW..**.*
16422 2a 20 49 66 20 61 6e 20 61 74 74 65 6d 70 74 20  * If an attempt 
16423 69 73 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e 20  is made to open 
16424 61 20 6c 6f 63 6b 65 64 20 64 61 74 61 62 61 73  a locked databas
16425 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  e, then this rou
16426 74 69 6e 65 0a 2a 2a 20 77 69 6c 6c 20 65 69 74  tine.** will eit
16427 68 65 72 20 69 6e 76 6f 6b 65 20 74 68 65 20 62  her invoke the b
16428 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 28 69 66  usy callback (if
16429 20 74 68 65 72 65 20 69 73 20 6f 6e 65 29 20 6f   there is one) o
1642a 72 20 69 74 20 77 69 6c 6c 0a 2a 2a 20 72 65 74  r it will.** ret
1642b 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e  urn SQLITE_BUSY.
1642c 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72  .**.** If an err
1642d 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 65 72  or occurs, an er
1642e 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 77  ror message is w
1642f 72 69 74 74 65 6e 20 74 6f 20 6d 65 6d 6f 72 79  ritten to memory
16430 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f   obtained.** fro
16431 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  m sqlite3_malloc
16432 28 29 20 61 6e 64 20 70 2d 3e 7a 45 72 72 4d 73  () and p->zErrMs
16433 67 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69  g is made to poi
16434 6e 74 20 74 6f 20 74 68 61 74 20 6d 65 6d 6f 72  nt to that memor
16435 79 2e 0a 2a 2a 20 54 68 65 20 65 72 72 6f 72 20  y..** The error 
16436 63 6f 64 65 20 69 73 20 73 74 6f 72 65 64 20 69  code is stored i
16437 6e 20 70 2d 3e 72 63 20 61 6e 64 20 74 68 69 73  n p->rc and this
16438 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
16439 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a   SQLITE_ERROR..*
1643a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c  *.** If the call
1643b 62 61 63 6b 20 65 76 65 72 20 72 65 74 75 72 6e  back ever return
1643c 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
1643d 20 74 68 65 20 70 72 6f 67 72 61 6d 20 65 78 69   the program exi
1643e 74 73 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c  ts.** immediatel
1643f 79 2e 20 20 54 68 65 72 65 20 77 69 6c 6c 20 62  y.  There will b
16440 65 20 6e 6f 20 65 72 72 6f 72 20 6d 65 73 73 61  e no error messa
16441 67 65 20 62 75 74 20 74 68 65 20 70 2d 3e 72 63  ge but the p->rc
16442 20 66 69 65 6c 64 20 69 73 0a 2a 2a 20 73 65 74   field is.** set
16443 20 74 6f 20 53 51 4c 49 54 45 5f 41 42 4f 52 54   to SQLITE_ABORT
16444 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e   and this routin
16445 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 51  e will return SQ
16446 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a  LITE_ERROR..**.*
16447 2a 20 41 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  * A memory alloc
16448 61 74 69 6f 6e 20 65 72 72 6f 72 20 63 61 75 73  ation error caus
16449 65 73 20 70 2d 3e 72 63 20 74 6f 20 62 65 20 73  es p->rc to be s
1644a 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d  et to SQLITE_NOM
1644b 45 4d 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20 72  EM and this.** r
1644c 6f 75 74 69 6e 65 20 74 6f 20 72 65 74 75 72 6e  outine to return
1644d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a   SQLITE_ERROR..*
1644e 2a 0a 2a 2a 20 4f 74 68 65 72 20 66 61 74 61 6c  *.** Other fatal
1644f 20 65 72 72 6f 72 73 20 72 65 74 75 72 6e 20 53   errors return S
16450 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a  QLITE_ERROR..**.
16451 2a 2a 20 41 66 74 65 72 20 74 68 69 73 20 72 6f  ** After this ro
16452 75 74 69 6e 65 20 68 61 73 20 66 69 6e 69 73 68  utine has finish
16453 65 64 2c 20 73 71 6c 69 74 65 33 56 64 62 65 46  ed, sqlite3VdbeF
16454 69 6e 61 6c 69 7a 65 28 29 20 73 68 6f 75 6c 64  inalize() should
16455 20 62 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 63   be.** used to c
16456 6c 65 61 6e 20 75 70 20 74 68 65 20 6d 65 73 73  lean up the mess
16457 20 74 68 61 74 20 77 61 73 20 6c 65 66 74 20 62   that was left b
16458 65 68 69 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ehind..*/.SQLITE
16459 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1645a 69 74 65 33 56 64 62 65 45 78 65 63 28 0a 20 20  ite3VdbeExec(.  
1645b 56 64 62 65 20 2a 70 20 20 20 20 20 20 20 20 20  Vdbe *p         
1645c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1645d 65 20 56 44 42 45 20 2a 2f 0a 29 7b 0a 20 20 69  e VDBE */.){.  i
1645e 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20 20  nt pc;          
1645f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
16460 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72   program counter
16461 20 2a 2f 0a 20 20 4f 70 20 2a 70 4f 70 3b 20 20   */.  Op *pOp;  
16462 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16463 20 2f 2a 20 43 75 72 72 65 6e 74 20 6f 70 65 72   /* Current oper
16464 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72  ation */.  int r
16465 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
16466 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74        /* Value t
16467 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71  o return */.  sq
16468 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64  lite3 *db = p->d
16469 62 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  b;       /* The 
1646a 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 38  database */.  u8
1646b 20 65 6e 63 6f 64 69 6e 67 20 3d 20 45 4e 43 28   encoding = ENC(
1646c 64 62 29 3b 20 20 20 20 20 2f 2a 20 54 68 65 20  db);     /* The 
1646d 64 61 74 61 62 61 73 65 20 65 6e 63 6f 64 69 6e  database encodin
1646e 67 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 31  g */.  Mem *pIn1
1646f 2c 20 2a 70 49 6e 32 2c 20 2a 70 49 6e 33 3b 20  , *pIn2, *pIn3; 
16470 20 20 2f 2a 20 49 6e 70 75 74 20 6f 70 65 72 61    /* Input opera
16471 6e 64 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4f  nds */.  Mem *pO
16472 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ut;             
16473 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 6f 70      /* Output op
16474 65 72 61 6e 64 20 2a 2f 0a 20 20 75 38 20 6f 70  erand */.  u8 op
16475 50 72 6f 70 65 72 74 79 3b 0a 23 69 66 64 65 66  Property;.#ifdef
16476 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20   VDBE_PROFILE.  
16477 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
16478 6e 67 20 73 74 61 72 74 3b 20 20 2f 2a 20 43 50  ng start;  /* CP
16479 55 20 63 6c 6f 63 6b 20 63 6f 75 6e 74 20 61 74  U clock count at
1647a 20 73 74 61 72 74 20 6f 66 20 6f 70 63 6f 64 65   start of opcode
1647b 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 69 67 50 63   */.  int origPc
1647c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1647d 20 2f 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75 6e   /* Program coun
1647e 74 65 72 20 61 74 20 73 74 61 72 74 20 6f 66 20  ter at start of 
1647f 6f 70 63 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66  opcode */.#endif
16480 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
16481 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43 41  OMIT_PROGRESS_CA
16482 4c 4c 42 41 43 4b 0a 20 20 69 6e 74 20 6e 50 72  LLBACK.  int nPr
16483 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b 20 20  ogressOps = 0;  
16484 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 73 20 65      /* Opcodes e
16485 78 65 63 75 74 65 64 20 73 69 6e 63 65 20 70 72  xecuted since pr
16486 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 2e  ogress callback.
16487 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73   */.#endif..  as
16488 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d  sert( p->magic==
16489 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29  VDBE_MAGIC_RUN )
1648a 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74  ;  /* sqlite3_st
1648b 65 70 28 29 20 76 65 72 69 66 69 65 73 20 74 68  ep() verifies th
1648c 69 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  is */.  assert( 
1648d 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54  db->magic==SQLIT
1648e 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a  E_MAGIC_BUSY );.
1648f 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75    sqlite3BtreeMu
16490 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 26 70  texArrayEnter(&p
16491 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20 69 66 28  ->aMutex);.  if(
16492 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4e   p->rc==SQLITE_N
16493 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 2f 2a 20 54  OMEM ){.    /* T
16494 68 69 73 20 68 61 70 70 65 6e 73 20 69 66 20 61  his happens if a
16495 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 73 69 64 65   malloc() inside
16496 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74   a call to sqlit
16497 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29  e3_column_text()
16498 20 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74   or.    ** sqlit
16499 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
1649a 28 29 20 66 61 69 6c 65 64 2e 20 20 2a 2f 0a 20  () failed.  */. 
1649b 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a     goto no_mem;.
1649c 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d    }.  assert( p-
1649d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c  >rc==SQLITE_OK |
1649e 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f  | p->rc==SQLITE_
1649f 42 55 53 59 20 29 3b 0a 20 20 70 2d 3e 72 63 20  BUSY );.  p->rc 
164a0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61  = SQLITE_OK;.  a
164a1 73 73 65 72 74 28 20 70 2d 3e 65 78 70 6c 61 69  ssert( p->explai
164a2 6e 3d 3d 30 20 29 3b 0a 20 20 70 2d 3e 70 52 65  n==0 );.  p->pRe
164a3 73 75 6c 74 53 65 74 20 3d 20 30 3b 0a 20 20 64  sultSet = 0;.  d
164a4 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 2e 6e  b->busyHandler.n
164a5 42 75 73 79 20 3d 20 30 3b 0a 20 20 43 48 45 43  Busy = 0;.  CHEC
164a6 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b  K_FOR_INTERRUPT;
164a7 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 49 4f  .  sqlite3VdbeIO
164a8 54 72 61 63 65 53 71 6c 28 70 29 3b 0a 23 69 66  TraceSql(p);.#if
164a9 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
164aa 0a 20 20 73 71 6c 69 74 65 33 46 61 75 6c 74 42  .  sqlite3FaultB
164ab 65 67 69 6e 42 65 6e 69 67 6e 28 2d 31 29 3b 0a  eginBenign(-1);.
164ac 20 20 69 66 28 20 70 2d 3e 70 63 3d 3d 30 20 26    if( p->pc==0 &
164ad 26 20 28 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73  & ((p->db->flags
164ae 20 26 20 53 51 4c 49 54 45 5f 56 64 62 65 4c 69   & SQLITE_VdbeLi
164af 73 74 69 6e 67 29 21 3d 30 0a 20 20 20 20 7c 7c  sting)!=0.    ||
164b0 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73   sqlite3OsAccess
164b1 28 64 62 2d 3e 70 56 66 73 2c 20 22 76 64 62 65  (db->pVfs, "vdbe
164b2 5f 65 78 70 6c 61 69 6e 22 2c 20 53 51 4c 49 54  _explain", SQLIT
164b3 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 29  E_ACCESS_EXISTS)
164b4 3d 3d 31 20 29 0a 20 20 29 7b 0a 20 20 20 20 69  ==1 ).  ){.    i
164b5 6e 74 20 69 3b 0a 20 20 20 20 70 72 69 6e 74 66  nt i;.    printf
164b6 28 22 56 44 42 45 20 50 72 6f 67 72 61 6d 20 4c  ("VDBE Program L
164b7 69 73 74 69 6e 67 3a 5c 6e 22 29 3b 0a 20 20 20  isting:\n");.   
164b8 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e   sqlite3VdbePrin
164b9 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 66 6f 72  tSql(p);.    for
164ba 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20  (i=0; i<p->nOp; 
164bb 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  i++){.      sqli
164bc 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 73  te3VdbePrintOp(s
164bd 74 64 6f 75 74 2c 20 69 2c 20 26 70 2d 3e 61 4f  tdout, i, &p->aO
164be 70 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  p[i]);.    }.  }
164bf 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 73  .  if( sqlite3Os
164c0 41 63 63 65 73 73 28 64 62 2d 3e 70 56 66 73 2c  Access(db->pVfs,
164c1 20 22 76 64 62 65 5f 74 72 61 63 65 22 2c 20 53   "vdbe_trace", S
164c2 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49  QLITE_ACCESS_EXI
164c3 53 54 53 29 3d 3d 31 20 29 7b 0a 20 20 20 20 70  STS)==1 ){.    p
164c4 2d 3e 74 72 61 63 65 20 3d 20 73 74 64 6f 75 74  ->trace = stdout
164c5 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 46  ;.  }.  sqlite3F
164c6 61 75 6c 74 45 6e 64 42 65 6e 69 67 6e 28 2d 31  aultEndBenign(-1
164c7 29 3b 0a 23 65 6e 64 69 66 0a 20 20 66 6f 72 28  );.#endif.  for(
164c8 70 63 3d 70 2d 3e 70 63 3b 20 72 63 3d 3d 53 51  pc=p->pc; rc==SQ
164c9 4c 49 54 45 5f 4f 4b 3b 20 70 63 2b 2b 29 7b 0a  LITE_OK; pc++){.
164ca 20 20 20 20 61 73 73 65 72 74 28 20 70 63 3e 3d      assert( pc>=
164cb 30 20 26 26 20 70 63 3c 70 2d 3e 6e 4f 70 20 29  0 && pc<p->nOp )
164cc 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61  ;.    if( db->ma
164cd 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74  llocFailed ) got
164ce 6f 20 6e 6f 5f 6d 65 6d 3b 0a 23 69 66 64 65 66  o no_mem;.#ifdef
164cf 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20   VDBE_PROFILE.  
164d0 20 20 6f 72 69 67 50 63 20 3d 20 70 63 3b 0a 20    origPc = pc;. 
164d1 20 20 20 73 74 61 72 74 20 3d 20 68 77 74 69 6d     start = hwtim
164d2 65 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  e();.#endif.    
164d3 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 70 63  pOp = &p->aOp[pc
164d4 5d 3b 0a 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20  ];..    /* Only 
164d5 61 6c 6c 6f 77 20 74 72 61 63 69 6e 67 20 69 66  allow tracing if
164d6 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73   SQLITE_DEBUG is
164d7 20 64 65 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f   defined..    */
164d8 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
164d9 45 42 55 47 0a 20 20 20 20 69 66 28 20 70 2d 3e  EBUG.    if( p->
164da 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 69  trace ){.      i
164db 66 28 20 70 63 3d 3d 30 20 29 7b 0a 20 20 20 20  f( pc==0 ){.    
164dc 20 20 20 20 70 72 69 6e 74 66 28 22 56 44 42 45      printf("VDBE
164dd 20 45 78 65 63 75 74 69 6f 6e 20 54 72 61 63 65   Execution Trace
164de 3a 5c 6e 22 29 3b 0a 20 20 20 20 20 20 20 20 73  :\n");.        s
164df 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 53  qlite3VdbePrintS
164e0 71 6c 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ql(p);.      }. 
164e1 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
164e2 50 72 69 6e 74 4f 70 28 70 2d 3e 74 72 61 63 65  PrintOp(p->trace
164e3 2c 20 70 63 2c 20 70 4f 70 29 3b 0a 20 20 20 20  , pc, pOp);.    
164e4 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61  }.    if( p->tra
164e5 63 65 3d 3d 30 20 26 26 20 70 63 3d 3d 30 20 29  ce==0 && pc==0 )
164e6 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 46  {.      sqlite3F
164e7 61 75 6c 74 42 65 67 69 6e 42 65 6e 69 67 6e 28  aultBeginBenign(
164e8 2d 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73  -1);.      if( s
164e9 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 64  qlite3OsAccess(d
164ea 62 2d 3e 70 56 66 73 2c 20 22 76 64 62 65 5f 73  b->pVfs, "vdbe_s
164eb 71 6c 74 72 61 63 65 22 2c 20 53 51 4c 49 54 45  qltrace", SQLITE
164ec 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 29 3d  _ACCESS_EXISTS)=
164ed 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  =1 ){.        sq
164ee 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 53 71  lite3VdbePrintSq
164ef 6c 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  l(p);.      }.  
164f0 20 20 20 20 73 71 6c 69 74 65 33 46 61 75 6c 74      sqlite3Fault
164f1 45 6e 64 42 65 6e 69 67 6e 28 2d 31 29 3b 0a 20  EndBenign(-1);. 
164f2 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
164f3 20 20 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b    ..    /* Check
164f4 20 74 6f 20 73 65 65 20 69 66 20 77 65 20 6e 65   to see if we ne
164f5 65 64 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61  ed to simulate a
164f6 6e 20 69 6e 74 65 72 72 75 70 74 2e 20 20 54 68  n interrupt.  Th
164f7 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 0a  is only happens.
164f8 20 20 20 20 2a 2a 20 69 66 20 77 65 20 68 61 76      ** if we hav
164f9 65 20 61 20 73 70 65 63 69 61 6c 20 74 65 73 74  e a special test
164fa 20 62 75 69 6c 64 2e 0a 20 20 20 20 2a 2f 0a 23   build..    */.#
164fb 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
164fc 54 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  T.    if( sqlite
164fd 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e  3_interrupt_coun
164fe 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  t>0 ){.      sql
164ff 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63  ite3_interrupt_c
16500 6f 75 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 69 66  ount--;.      if
16501 28 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72  ( sqlite3_interr
16502 75 70 74 5f 63 6f 75 6e 74 3d 3d 30 20 29 7b 0a  upt_count==0 ){.
16503 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
16504 69 6e 74 65 72 72 75 70 74 28 64 62 29 3b 0a 20  interrupt(db);. 
16505 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e       }.    }.#en
16506 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
16507 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53  ITE_OMIT_PROGRES
16508 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 20 20 2f  S_CALLBACK.    /
16509 2a 20 43 61 6c 6c 20 74 68 65 20 70 72 6f 67 72  * Call the progr
1650a 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 69 66 20  ess callback if 
1650b 69 74 20 69 73 20 63 6f 6e 66 69 67 75 72 65 64  it is configured
1650c 20 61 6e 64 20 74 68 65 20 72 65 71 75 69 72 65   and the require
1650d 64 20 6e 75 6d 62 65 72 0a 20 20 20 20 2a 2a 20  d number.    ** 
1650e 6f 66 20 56 44 42 45 20 6f 70 73 20 68 61 76 65  of VDBE ops have
1650f 20 62 65 65 6e 20 65 78 65 63 75 74 65 64 20 28   been executed (
16510 65 69 74 68 65 72 20 73 69 6e 63 65 20 74 68 69  either since thi
16511 73 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 0a  s invocation of.
16512 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64      ** sqlite3Vd
16513 62 65 45 78 65 63 28 29 20 6f 72 20 73 69 6e 63  beExec() or sinc
16514 65 20 6c 61 73 74 20 74 69 6d 65 20 74 68 65 20  e last time the 
16515 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63  progress callbac
16516 6b 20 77 61 73 20 63 61 6c 6c 65 64 29 2e 0a 20  k was called).. 
16517 20 20 20 2a 2a 20 49 66 20 74 68 65 20 70 72 6f     ** If the pro
16518 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 72  gress callback r
16519 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  eturns non-zero,
1651a 20 65 78 69 74 20 74 68 65 20 76 69 72 74 75 61   exit the virtua
1651b 6c 20 6d 61 63 68 69 6e 65 20 77 69 74 68 0a 20  l machine with. 
1651c 20 20 20 2a 2a 20 61 20 72 65 74 75 72 6e 20 63     ** a return c
1651d 6f 64 65 20 53 51 4c 49 54 45 5f 41 42 4f 52 54  ode SQLITE_ABORT
1651e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1651f 20 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 20 29   db->xProgress )
16520 7b 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e  {.      if( db->
16521 6e 50 72 6f 67 72 65 73 73 4f 70 73 3d 3d 6e 50  nProgressOps==nP
16522 72 6f 67 72 65 73 73 4f 70 73 20 29 7b 0a 20 20  rogressOps ){.  
16523 20 20 20 20 20 20 69 6e 74 20 70 72 63 3b 0a 20        int prc;. 
16524 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
16525 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20  e3SafetyOff(db) 
16526 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
16527 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20  _to_misuse;.    
16528 20 20 20 20 70 72 63 20 3d 64 62 2d 3e 78 50 72      prc =db->xPr
16529 6f 67 72 65 73 73 28 64 62 2d 3e 70 50 72 6f 67  ogress(db->pProg
1652a 72 65 73 73 41 72 67 29 3b 0a 20 20 20 20 20 20  ressArg);.      
1652b 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
1652c 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f  etyOn(db) ) goto
1652d 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69   abort_due_to_mi
1652e 73 75 73 65 3b 0a 20 20 20 20 20 20 20 20 69 66  suse;.        if
1652f 28 20 70 72 63 21 3d 30 20 29 7b 0a 20 20 20 20  ( prc!=0 ){.    
16530 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
16531 45 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 20  E_INTERRUPT;.   
16532 20 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65         goto vdbe
16533 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 20 20 20  _error_halt;.   
16534 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e       }.        n
16535 50 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b  ProgressOps = 0;
16536 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e  .      }.      n
16537 50 72 6f 67 72 65 73 73 4f 70 73 2b 2b 3b 0a 20  ProgressOps++;. 
16538 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
16539 20 2f 2a 20 44 6f 20 63 6f 6d 6d 6f 6e 20 73 65   /* Do common se
1653a 74 75 70 20 70 72 6f 63 65 73 73 69 6e 67 20 66  tup processing f
1653b 6f 72 20 61 6e 79 20 6f 70 63 6f 64 65 20 74 68  or any opcode th
1653c 61 74 20 69 73 20 6d 61 72 6b 65 64 0a 20 20 20  at is marked.   
1653d 20 2a 2a 20 77 69 74 68 20 74 68 65 20 22 6f 75   ** with the "ou
1653e 74 32 2d 70 72 65 72 65 6c 65 61 73 65 22 20 74  t2-prerelease" t
1653f 61 67 2e 20 20 53 75 63 68 20 6f 70 63 6f 64 65  ag.  Such opcode
16540 73 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 0a  s have a single.
16541 20 20 20 20 2a 2a 20 6f 75 74 70 75 74 20 77 68      ** output wh
16542 69 63 68 20 69 73 20 73 70 65 63 69 66 69 65 64  ich is specified
16543 20 62 79 20 74 68 65 20 50 32 20 70 61 72 61 6d   by the P2 param
16544 65 74 65 72 2e 20 20 54 68 65 20 50 32 20 72 65  eter.  The P2 re
16545 67 69 73 74 65 72 0a 20 20 20 20 2a 2a 20 69 73  gister.    ** is
16546 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20   initialized to 
16547 61 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20  a NULL..    */. 
16548 20 20 20 6f 70 50 72 6f 70 65 72 74 79 20 3d 20     opProperty = 
16549 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 70  opcodeProperty[p
1654a 4f 70 2d 3e 6f 70 63 6f 64 65 5d 3b 0a 20 20 20  Op->opcode];.   
1654b 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74 79   if( (opProperty
1654c 20 26 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50 52   & OPFLG_OUT2_PR
1654d 45 52 45 4c 45 41 53 45 29 21 3d 30 20 29 7b 0a  ERELEASE)!=0 ){.
1654e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
1654f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 20  p->p2>0 );.     
16550 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32   assert( pOp->p2
16551 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20  <=p->nMem );.   
16552 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d     pOut = &p->aM
16553 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20  em[pOp->p2];.   
16554 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
16555 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c  mReleaseExternal
16556 28 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 70 4f  (pOut);.      pO
16557 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  ut->flags = MEM_
16558 4e 75 6c 6c 3b 0a 20 20 20 20 7d 65 6c 73 65 0a  Null;.    }else.
16559 20 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f 6d 6d   .    /* Do comm
1655a 6f 6e 20 73 65 74 75 70 20 66 6f 72 20 6f 70 63  on setup for opc
1655b 6f 64 65 73 20 6d 61 72 6b 65 64 20 77 69 74 68  odes marked with
1655c 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
1655d 6f 77 69 6e 67 0a 20 20 20 20 2a 2a 20 63 6f 6d  owing.    ** com
1655e 62 69 6e 61 74 69 6f 6e 73 20 6f 66 20 70 72 6f  binations of pro
1655f 70 65 72 74 69 65 73 2e 0a 20 20 20 20 2a 2a 0a  perties..    **.
16560 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
16561 20 69 6e 31 0a 20 20 20 20 2a 2a 20 20 20 20 20   in1.    **     
16562 20 20 20 20 20 20 69 6e 31 20 69 6e 32 0a 20 20        in1 in2.  
16563 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69    **           i
16564 6e 31 20 69 6e 32 20 6f 75 74 33 0a 20 20 20 20  n1 in2 out3.    
16565 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69 6e 31  **           in1
16566 20 69 6e 33 0a 20 20 20 20 2a 2a 0a 20 20 20 20   in3.    **.    
16567 2a 2a 20 56 61 72 69 61 62 6c 65 73 20 70 49 6e  ** Variables pIn
16568 31 2c 20 70 49 6e 32 2c 20 61 6e 64 20 70 49 6e  1, pIn2, and pIn
16569 33 20 61 72 65 20 6d 61 64 65 20 74 6f 20 70 6f  3 are made to po
1656a 69 6e 74 20 74 6f 20 61 70 70 72 6f 70 72 69 61  int to appropria
1656b 74 65 0a 20 20 20 20 2a 2a 20 72 65 67 69 73 74  te.    ** regist
1656c 65 72 73 20 66 6f 72 20 69 6e 70 75 74 73 2e 20  ers for inputs. 
1656d 20 56 61 72 69 61 62 6c 65 20 70 4f 75 74 20 70   Variable pOut p
1656e 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6f 75 74  oints to the out
1656f 70 75 74 20 72 65 67 69 73 74 65 72 2e 0a 20 20  put register..  
16570 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 6f 70    */.    if( (op
16571 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47  Property & OPFLG
16572 5f 49 4e 31 29 21 3d 30 20 29 7b 0a 20 20 20 20  _IN1)!=0 ){.    
16573 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
16574 31 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  1>0 );.      ass
16575 65 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d  ert( pOp->p1<=p-
16576 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70  >nMem );.      p
16577 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  In1 = &p->aMem[p
16578 4f 70 2d 3e 70 31 5d 3b 0a 20 20 20 20 20 20 52  Op->p1];.      R
16579 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
1657a 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20  p->p1, pIn1);.  
1657b 20 20 20 20 69 66 28 20 28 6f 70 50 72 6f 70 65      if( (opPrope
1657c 72 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 32 29  rty & OPFLG_IN2)
1657d 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61  !=0 ){.        a
1657e 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30  ssert( pOp->p2>0
1657f 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65   );.        asse
16580 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e  rt( pOp->p2<=p->
16581 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20  nMem );.        
16582 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pIn2 = &p->aMem[
16583 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20  pOp->p2];.      
16584 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
16585 28 70 4f 70 2d 3e 70 32 2c 20 70 49 6e 32 29 3b  (pOp->p2, pIn2);
16586 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 6f 70  .        if( (op
16587 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47  Property & OPFLG
16588 5f 4f 55 54 33 29 21 3d 30 20 29 7b 0a 20 20 20  _OUT3)!=0 ){.   
16589 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1658a 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20  Op->p3>0 );.    
1658b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
1658c 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p3<=p->nMem )
1658d 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 75 74  ;.          pOut
1658e 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
1658f 3e 70 33 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a  >p3];.        }.
16590 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
16591 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50  (opProperty & OP
16592 46 4c 47 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20  FLG_IN3)!=0 ){. 
16593 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
16594 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20  Op->p3>0 );.    
16595 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
16596 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p3<=p->nMem );.
16597 20 20 20 20 20 20 20 20 70 49 6e 33 20 3d 20 26          pIn3 = &
16598 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  p->aMem[pOp->p3]
16599 3b 0a 20 20 20 20 20 20 20 20 52 45 47 49 53 54  ;.        REGIST
1659a 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33  ER_TRACE(pOp->p3
1659b 2c 20 70 49 6e 33 29 3b 0a 20 20 20 20 20 20 7d  , pIn3);.      }
1659c 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28  .    }else if( (
1659d 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46  opProperty & OPF
1659e 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b 0a 20 20  LG_IN2)!=0 ){.  
1659f 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
165a0 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 20 20 61  >p2>0 );.      a
165a1 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d  ssert( pOp->p2<=
165a2 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20  p->nMem );.     
165a3 20 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d   pIn2 = &p->aMem
165a4 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20  [pOp->p2];.     
165a5 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28   REGISTER_TRACE(
165a6 70 4f 70 2d 3e 70 32 2c 20 70 49 6e 32 29 3b 0a  pOp->p2, pIn2);.
165a7 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6f      }else if( (o
165a8 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c  pProperty & OPFL
165a9 47 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20 20 20  G_IN3)!=0 ){.   
165aa 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
165ab 70 33 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73  p3>0 );.      as
165ac 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70  sert( pOp->p3<=p
165ad 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20  ->nMem );.      
165ae 70 49 6e 33 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pIn3 = &p->aMem[
165af 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20  pOp->p3];.      
165b0 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
165b1 4f 70 2d 3e 70 33 2c 20 70 49 6e 33 29 3b 0a 20  Op->p3, pIn3);. 
165b2 20 20 20 7d 0a 0a 20 20 20 20 73 77 69 74 63 68     }..    switch
165b3 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b  ( pOp->opcode ){
165b4 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
165b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
165b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
165b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
165b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
165b9 0a 2a 2a 20 57 68 61 74 20 66 6f 6c 6c 6f 77 73  .** What follows
165ba 20 69 73 20 61 20 6d 61 73 73 69 76 65 20 73 77   is a massive sw
165bb 69 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20 77  itch statement w
165bc 68 65 72 65 20 65 61 63 68 20 63 61 73 65 20 69  here each case i
165bd 6d 70 6c 65 6d 65 6e 74 73 20 61 0a 2a 2a 20 73  mplements a.** s
165be 65 70 61 72 61 74 65 20 69 6e 73 74 72 75 63 74  eparate instruct
165bf 69 6f 6e 20 69 6e 20 74 68 65 20 76 69 72 74 75  ion in the virtu
165c0 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 49 66 20  al machine.  If 
165c1 77 65 20 66 6f 6c 6c 6f 77 20 74 68 65 20 75 73  we follow the us
165c2 75 61 6c 0a 2a 2a 20 69 6e 64 65 6e 74 61 74 69  ual.** indentati
165c3 6f 6e 20 63 6f 6e 76 65 6e 74 69 6f 6e 73 2c 20  on conventions, 
165c4 65 61 63 68 20 63 61 73 65 20 73 68 6f 75 6c 64  each case should
165c5 20 62 65 20 69 6e 64 65 6e 74 65 64 20 62 79 20   be indented by 
165c6 36 20 73 70 61 63 65 73 2e 20 20 42 75 74 0a 2a  6 spaces.  But.*
165c7 2a 20 74 68 61 74 20 69 73 20 61 20 6c 6f 74 20  * that is a lot 
165c8 6f 66 20 77 61 73 74 65 64 20 73 70 61 63 65 20  of wasted space 
165c9 6f 6e 20 74 68 65 20 6c 65 66 74 20 6d 61 72 67  on the left marg
165ca 69 6e 2e 20 20 53 6f 20 74 68 65 20 63 6f 64 65  in.  So the code
165cb 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 73   within.** the s
165cc 77 69 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20  witch statement 
165cd 77 69 6c 6c 20 62 72 65 61 6b 20 77 69 74 68 20  will break with 
165ce 63 6f 6e 76 65 6e 74 69 6f 6e 20 61 6e 64 20 62  convention and b
165cf 65 20 66 6c 75 73 68 2d 6c 65 66 74 2e 20 41 6e  e flush-left. An
165d0 6f 74 68 65 72 0a 2a 2a 20 62 69 67 20 63 6f 6d  other.** big com
165d1 6d 65 6e 74 20 28 73 69 6d 69 6c 61 72 20 74 6f  ment (similar to
165d2 20 74 68 69 73 20 6f 6e 65 29 20 77 69 6c 6c 20   this one) will 
165d3 6d 61 72 6b 20 74 68 65 20 70 6f 69 6e 74 20 69  mark the point i
165d4 6e 20 74 68 65 20 63 6f 64 65 20 77 68 65 72 65  n the code where
165d5 0a 2a 2a 20 77 65 20 74 72 61 6e 73 69 74 69 6f  .** we transitio
165d6 6e 20 62 61 63 6b 20 74 6f 20 6e 6f 72 6d 61 6c  n back to normal
165d7 20 69 6e 64 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a   indentation..**
165d8 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 74 69  .** The formatti
165d9 6e 67 20 6f 66 20 65 61 63 68 20 63 61 73 65 20  ng of each case 
165da 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20 54  is important.  T
165db 68 65 20 6d 61 6b 65 66 69 6c 65 20 66 6f 72 20  he makefile for 
165dc 53 51 4c 69 74 65 0a 2a 2a 20 67 65 6e 65 72 61  SQLite.** genera
165dd 74 65 73 20 74 77 6f 20 43 20 66 69 6c 65 73 20  tes two C files 
165de 22 6f 70 63 6f 64 65 73 2e 68 22 20 61 6e 64 20  "opcodes.h" and 
165df 22 6f 70 63 6f 64 65 73 2e 63 22 20 62 79 20 73  "opcodes.c" by s
165e0 63 61 6e 6e 69 6e 67 20 74 68 69 73 0a 2a 2a 20  canning this.** 
165e1 66 69 6c 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72  file looking for
165e2 20 6c 69 6e 65 73 20 74 68 61 74 20 62 65 67 69   lines that begi
165e3 6e 20 77 69 74 68 20 22 63 61 73 65 20 4f 50 5f  n with "case OP_
165e4 22 2e 20 20 54 68 65 20 6f 70 63 6f 64 65 73 2e  ".  The opcodes.
165e5 68 20 66 69 6c 65 73 0a 2a 2a 20 77 69 6c 6c 20  h files.** will 
165e6 62 65 20 66 69 6c 6c 65 64 20 77 69 74 68 20 23  be filled with #
165e7 64 65 66 69 6e 65 73 20 74 68 61 74 20 67 69 76  defines that giv
165e8 65 20 75 6e 69 71 75 65 20 69 6e 74 65 67 65 72  e unique integer
165e9 20 76 61 6c 75 65 73 20 74 6f 20 65 61 63 68 0a   values to each.
165ea 2a 2a 20 6f 70 63 6f 64 65 20 61 6e 64 20 74 68  ** opcode and th
165eb 65 20 6f 70 63 6f 64 65 73 2e 63 20 66 69 6c 65  e opcodes.c file
165ec 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20   is filled with 
165ed 61 6e 20 61 72 72 61 79 20 6f 66 20 73 74 72 69  an array of stri
165ee 6e 67 73 20 77 68 65 72 65 0a 2a 2a 20 65 61 63  ngs where.** eac
165ef 68 20 73 74 72 69 6e 67 20 69 73 20 74 68 65 20  h string is the 
165f0 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 66 6f  symbolic name fo
165f1 72 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  r the correspond
165f2 69 6e 67 20 6f 70 63 6f 64 65 2e 20 20 49 66 20  ing opcode.  If 
165f3 74 68 65 0a 2a 2a 20 63 61 73 65 20 73 74 61 74  the.** case stat
165f4 65 6d 65 6e 74 20 69 73 20 66 6f 6c 6c 6f 77 65  ement is followe
165f5 64 20 62 79 20 61 20 63 6f 6d 6d 65 6e 74 20 6f  d by a comment o
165f6 66 20 74 68 65 20 66 6f 72 6d 20 22 2f 23 20 73  f the form "/# s
165f7 61 6d 65 20 61 73 20 2e 2e 2e 20 23 2f 22 0a 2a  ame as ... #/".*
165f8 2a 20 74 68 61 74 20 63 6f 6d 6d 65 6e 74 20 69  * that comment i
165f9 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  s used to determ
165fa 69 6e 65 20 74 68 65 20 70 61 72 74 69 63 75 6c  ine the particul
165fb 61 72 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  ar value of the 
165fc 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74  opcode..**.** Ot
165fd 68 65 72 20 6b 65 79 77 6f 72 64 73 20 69 6e 20  her keywords in 
165fe 74 68 65 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74  the comment that
165ff 20 66 6f 6c 6c 6f 77 73 20 65 61 63 68 20 63 61   follows each ca
16600 73 65 20 61 72 65 20 75 73 65 64 20 74 6f 0a 2a  se are used to.*
16601 2a 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20  * construct the 
16602 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45  OPFLG_INITIALIZE
16603 52 20 76 61 6c 75 65 20 74 68 61 74 20 69 6e 69  R value that ini
16604 74 69 61 6c 69 7a 65 73 20 6f 70 63 6f 64 65 50  tializes opcodeP
16605 72 6f 70 65 72 74 79 5b 5d 2e 0a 2a 2a 20 4b 65  roperty[]..** Ke
16606 79 77 6f 72 64 73 20 69 6e 63 6c 75 64 65 3a 20  ywords include: 
16607 69 6e 31 2c 20 69 6e 32 2c 20 69 6e 33 2c 20 6f  in1, in2, in3, o
16608 75 74 32 5f 70 72 65 72 65 6c 65 61 73 65 2c 20  ut2_prerelease, 
16609 6f 75 74 32 2c 20 6f 75 74 33 2e 20 20 53 65 65  out2, out3.  See
1660a 0a 2a 2a 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65  .** the mkopcode
1660b 68 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72  h.awk script for
1660c 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
1660d 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44  rmation..**.** D
1660e 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 61 62 6f  ocumentation abo
1660f 75 74 20 56 44 42 45 20 6f 70 63 6f 64 65 73 20  ut VDBE opcodes 
16610 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20  is generated by 
16611 73 63 61 6e 6e 69 6e 67 20 74 68 69 73 20 66 69  scanning this fi
16612 6c 65 0a 2a 2a 20 66 6f 72 20 6c 69 6e 65 73 20  le.** for lines 
16613 6f 66 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 20  of that contain 
16614 22 4f 70 63 6f 64 65 3a 22 2e 20 20 54 68 61 74  "Opcode:".  That
16615 20 6c 69 6e 65 20 61 6e 64 20 61 6c 6c 20 73 75   line and all su
16616 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 6f 6d 6d  bsequent.** comm
16617 65 6e 74 20 6c 69 6e 65 73 20 61 72 65 20 75 73  ent lines are us
16618 65 64 20 69 6e 20 74 68 65 20 67 65 6e 65 72 61  ed in the genera
16619 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f 70 63 6f  tion of the opco
1661a 64 65 2e 68 74 6d 6c 20 64 6f 63 75 6d 65 6e 74  de.html document
1661b 61 74 69 6f 6e 0a 2a 2a 20 66 69 6c 65 2e 0a 2a  ation.** file..*
1661c 2a 0a 2a 2a 20 53 55 4d 4d 41 52 59 3a 0a 2a 2a  *.** SUMMARY:.**
1661d 0a 2a 2a 20 20 20 20 20 46 6f 72 6d 61 74 74 69  .**     Formatti
1661e 6e 67 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20  ng is important 
1661f 74 6f 20 73 63 72 69 70 74 73 20 74 68 61 74 20  to scripts that 
16620 73 63 61 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a  scan this file..
16621 2a 2a 20 20 20 20 20 44 6f 20 6e 6f 74 20 64 65  **     Do not de
16622 76 69 61 74 65 20 66 72 6f 6d 20 74 68 65 20 66  viate from the f
16623 6f 72 6d 61 74 74 69 6e 67 20 73 74 79 6c 65 20  ormatting style 
16624 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65  currently in use
16625 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
16626 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16627 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16628 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16629 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1662a 2a 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  **/../* Opcode: 
1662b 20 47 6f 74 6f 20 2a 20 50 32 20 2a 20 2a 20 2a   Goto * P2 * * *
1662c 0a 2a 2a 0a 2a 2a 20 41 6e 20 75 6e 63 6f 6e 64  .**.** An uncond
1662d 69 74 69 6f 6e 61 6c 20 6a 75 6d 70 20 74 6f 20  itional jump to 
1662e 61 64 64 72 65 73 73 20 50 32 2e 0a 2a 2a 20 54  address P2..** T
1662f 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74  he next instruct
16630 69 6f 6e 20 65 78 65 63 75 74 65 64 20 77 69 6c  ion executed wil
16631 6c 20 62 65 20 0a 2a 2a 20 74 68 65 20 6f 6e 65  l be .** the one
16632 20 61 74 20 69 6e 64 65 78 20 50 32 20 66 72 6f   at index P2 fro
16633 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  m the beginning 
16634 6f 66 0a 2a 2a 20 74 68 65 20 70 72 6f 67 72 61  of.** the progra
16635 6d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f  m..*/.case OP_Go
16636 74 6f 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  to: {           
16637 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 43    /* jump */.  C
16638 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55  HECK_FOR_INTERRU
16639 50 54 3b 0a 20 20 70 63 20 3d 20 70 4f 70 2d 3e  PT;.  pc = pOp->
1663a 70 32 20 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b  p2 - 1;.  break;
1663b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20  .}../* Opcode:  
1663c 47 6f 73 75 62 20 2a 20 50 32 20 2a 20 2a 20 2a  Gosub * P2 * * *
1663d 0a 2a 2a 0a 2a 2a 20 50 75 73 68 20 74 68 65 20  .**.** Push the 
1663e 63 75 72 72 65 6e 74 20 61 64 64 72 65 73 73 20  current address 
1663f 70 6c 75 73 20 31 20 6f 6e 74 6f 20 74 68 65 20  plus 1 onto the 
16640 72 65 74 75 72 6e 20 61 64 64 72 65 73 73 20 73  return address s
16641 74 61 63 6b 0a 2a 2a 20 61 6e 64 20 74 68 65 6e  tack.** and then
16642 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73   jump to address
16643 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72   P2..**.** The r
16644 65 74 75 72 6e 20 61 64 64 72 65 73 73 20 73 74  eturn address st
16645 61 63 6b 20 69 73 20 6f 66 20 6c 69 6d 69 74 65  ack is of limite
16646 64 20 64 65 70 74 68 2e 20 20 49 66 20 74 6f 6f  d depth.  If too
16647 20 6d 61 6e 79 0a 2a 2a 20 4f 50 5f 47 6f 73 75   many.** OP_Gosu
16648 62 20 6f 70 65 72 61 74 69 6f 6e 73 20 6f 63 63  b operations occ
16649 75 72 20 77 69 74 68 6f 75 74 20 69 6e 74 65 72  ur without inter
1664a 76 65 6e 69 6e 67 20 4f 50 5f 52 65 74 75 72 6e  vening OP_Return
1664b 73 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 72  s, then.** the r
1664c 65 74 75 72 6e 20 61 64 64 72 65 73 73 20 73 74  eturn address st
1664d 61 63 6b 20 77 69 6c 6c 20 66 69 6c 6c 20 75 70  ack will fill up
1664e 20 61 6e 64 20 70 72 6f 63 65 73 73 69 6e 67 20   and processing 
1664f 77 69 6c 6c 20 61 62 6f 72 74 0a 2a 2a 20 77 69  will abort.** wi
16650 74 68 20 61 20 66 61 74 61 6c 20 65 72 72 6f 72  th a fatal error
16651 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 73  ..*/.case OP_Gos
16652 75 62 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  ub: {           
16653 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 61 73   /* jump */.  as
16654 73 65 72 74 28 20 70 2d 3e 72 65 74 75 72 6e 44  sert( p->returnD
16655 65 70 74 68 3c 73 69 7a 65 6f 66 28 70 2d 3e 72  epth<sizeof(p->r
16656 65 74 75 72 6e 53 74 61 63 6b 29 2f 73 69 7a 65  eturnStack)/size
16657 6f 66 28 70 2d 3e 72 65 74 75 72 6e 53 74 61 63  of(p->returnStac
16658 6b 5b 30 5d 29 20 29 3b 0a 20 20 70 2d 3e 72 65  k[0]) );.  p->re
16659 74 75 72 6e 53 74 61 63 6b 5b 70 2d 3e 72 65 74  turnStack[p->ret
1665a 75 72 6e 44 65 70 74 68 2b 2b 5d 20 3d 20 70 63  urnDepth++] = pc
1665b 2b 31 3b 0a 20 20 70 63 20 3d 20 70 4f 70 2d 3e  +1;.  pc = pOp->
1665c 70 32 20 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b  p2 - 1;.  break;
1665d 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20  .}../* Opcode:  
1665e 52 65 74 75 72 6e 20 2a 20 2a 20 2a 20 2a 20 2a  Return * * * * *
1665f 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 69 6d 6d 65  .**.** Jump imme
16660 64 69 61 74 65 6c 79 20 74 6f 20 74 68 65 20 6e  diately to the n
16661 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ext instruction 
16662 61 66 74 65 72 20 74 68 65 20 6c 61 73 74 20 75  after the last u
16663 6e 72 65 74 75 72 6e 65 64 0a 2a 2a 20 4f 50 5f  nreturned.** OP_
16664 47 6f 73 75 62 2e 20 20 49 66 20 61 6e 20 4f 50  Gosub.  If an OP
16665 5f 52 65 74 75 72 6e 20 68 61 73 20 6f 63 63 75  _Return has occu
16666 72 72 65 64 20 66 6f 72 20 61 6c 6c 20 4f 50 5f  rred for all OP_
16667 47 6f 73 75 62 73 2c 20 74 68 65 6e 0a 2a 2a 20  Gosubs, then.** 
16668 70 72 6f 63 65 73 73 69 6e 67 20 61 62 6f 72 74  processing abort
16669 73 20 77 69 74 68 20 61 20 66 61 74 61 6c 20 65  s with a fatal e
1666a 72 72 6f 72 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  rror..*/.case OP
1666b 5f 52 65 74 75 72 6e 3a 20 7b 0a 20 20 61 73 73  _Return: {.  ass
1666c 65 72 74 28 20 70 2d 3e 72 65 74 75 72 6e 44 65  ert( p->returnDe
1666d 70 74 68 3e 30 20 29 3b 0a 20 20 70 2d 3e 72 65  pth>0 );.  p->re
1666e 74 75 72 6e 44 65 70 74 68 2d 2d 3b 0a 20 20 70  turnDepth--;.  p
1666f 63 20 3d 20 70 2d 3e 72 65 74 75 72 6e 53 74 61  c = p->returnSta
16670 63 6b 5b 70 2d 3e 72 65 74 75 72 6e 44 65 70 74  ck[p->returnDept
16671 68 5d 20 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b  h] - 1;.  break;
16672 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20  .}../* Opcode:  
16673 48 61 6c 74 20 50 31 20 50 32 20 2a 20 50 34 20  Halt P1 P2 * P4 
16674 2a 0a 2a 2a 0a 2a 2a 20 45 78 69 74 20 69 6d 6d  *.**.** Exit imm
16675 65 64 69 61 74 65 6c 79 2e 20 20 41 6c 6c 20 6f  ediately.  All o
16676 70 65 6e 20 63 75 72 73 6f 72 73 2c 20 46 69 66  pen cursors, Fif
16677 6f 73 2c 20 65 74 63 20 61 72 65 20 63 6c 6f 73  os, etc are clos
16678 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61  ed.** automatica
16679 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73  lly..**.** P1 is
1667a 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 64 65   the result code
1667b 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c   returned by sql
1667c 69 74 65 33 5f 65 78 65 63 28 29 2c 20 73 71 6c  ite3_exec(), sql
1667d 69 74 65 33 5f 72 65 73 65 74 28 29 2c 0a 2a 2a  ite3_reset(),.**
1667e 20 6f 72 20 73 71 6c 69 74 65 33 5f 66 69 6e 61   or sqlite3_fina
1667f 6c 69 7a 65 28 29 2e 20 20 46 6f 72 20 61 20 6e  lize().  For a n
16680 6f 72 6d 61 6c 20 68 61 6c 74 2c 20 74 68 69 73  ormal halt, this
16681 20 73 68 6f 75 6c 64 20 62 65 20 53 51 4c 49 54   should be SQLIT
16682 45 5f 4f 4b 20 28 30 29 2e 0a 2a 2a 20 46 6f 72  E_OK (0)..** For
16683 20 65 72 72 6f 72 73 2c 20 69 74 20 63 61 6e 20   errors, it can 
16684 62 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 76 61  be some other va
16685 6c 75 65 2e 20 20 49 66 20 50 31 21 3d 30 20 74  lue.  If P1!=0 t
16686 68 65 6e 20 50 32 20 77 69 6c 6c 20 64 65 74 65  hen P2 will dete
16687 72 6d 69 6e 65 0a 2a 2a 20 77 68 65 74 68 65 72  rmine.** whether
16688 20 6f 72 20 6e 6f 74 20 74 6f 20 72 6f 6c 6c 62   or not to rollb
16689 61 63 6b 20 74 68 65 20 63 75 72 72 65 6e 74 20  ack the current 
1668a 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 44 6f  transaction.  Do
1668b 20 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a   not rollback.**
1668c 20 69 66 20 50 32 3d 3d 4f 45 5f 46 61 69 6c 2e   if P2==OE_Fail.
1668d 20 44 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b   Do the rollback
1668e 20 69 66 20 50 32 3d 3d 4f 45 5f 52 6f 6c 6c 62   if P2==OE_Rollb
1668f 61 63 6b 2e 20 20 49 66 20 50 32 3d 3d 4f 45 5f  ack.  If P2==OE_
16690 41 62 6f 72 74 2c 0a 2a 2a 20 74 68 65 6e 20 62  Abort,.** then b
16691 61 63 6b 20 6f 75 74 20 61 6c 6c 20 63 68 61 6e  ack out all chan
16692 67 65 73 20 74 68 61 74 20 68 61 76 65 20 6f 63  ges that have oc
16693 63 75 72 72 65 64 20 64 75 72 69 6e 67 20 74 68  curred during th
16694 69 73 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20  is execution of 
16695 74 68 65 0a 2a 2a 20 56 44 42 45 2c 20 62 75 74  the.** VDBE, but
16696 20 64 6f 20 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b   do not rollback
16697 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
16698 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 20 69  . .**.** If P4 i
16699 73 20 6e 6f 74 20 6e 75 6c 6c 20 74 68 65 6e 20  s not null then 
1669a 69 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 6d  it is an error m
1669b 65 73 73 61 67 65 20 73 74 72 69 6e 67 2e 0a 2a  essage string..*
1669c 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 6e  *.** There is an
1669d 20 69 6d 70 6c 69 65 64 20 22 48 61 6c 74 20 30   implied "Halt 0
1669e 20 30 20 30 22 20 69 6e 73 74 72 75 63 74 69 6f   0 0" instructio
1669f 6e 20 69 6e 73 65 72 74 65 64 20 61 74 20 74 68  n inserted at th
166a0 65 20 76 65 72 79 20 65 6e 64 20 6f 66 0a 2a 2a  e very end of.**
166a1 20 65 76 65 72 79 20 70 72 6f 67 72 61 6d 2e 20   every program. 
166a2 20 53 6f 20 61 20 6a 75 6d 70 20 70 61 73 74 20   So a jump past 
166a3 74 68 65 20 6c 61 73 74 20 69 6e 73 74 72 75 63  the last instruc
166a4 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 72 6f 67  tion of the prog
166a5 72 61 6d 0a 2a 2a 20 69 73 20 74 68 65 20 73 61  ram.** is the sa
166a6 6d 65 20 61 73 20 65 78 65 63 75 74 69 6e 67 20  me as executing 
166a7 48 61 6c 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  Halt..*/.case OP
166a8 5f 48 61 6c 74 3a 20 7b 0a 20 20 70 2d 3e 72 63  _Halt: {.  p->rc
166a9 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 70 2d   = pOp->p1;.  p-
166aa 3e 70 63 20 3d 20 70 63 3b 0a 20 20 70 2d 3e 65  >pc = pc;.  p->e
166ab 72 72 6f 72 41 63 74 69 6f 6e 20 3d 20 70 4f 70  rrorAction = pOp
166ac 2d 3e 70 32 3b 0a 20 20 69 66 28 20 70 4f 70 2d  ->p2;.  if( pOp-
166ad 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 73 71 6c  >p4.z ){.    sql
166ae 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70  ite3SetString(&p
166af 2d 3e 7a 45 72 72 4d 73 67 2c 20 70 4f 70 2d 3e  ->zErrMsg, pOp->
166b0 70 34 2e 7a 2c 20 28 63 68 61 72 2a 29 30 29 3b  p4.z, (char*)0);
166b1 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69  .  }.  rc = sqli
166b2 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a  te3VdbeHalt(p);.
166b3 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
166b4 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20 72 63 3d  LITE_BUSY || rc=
166b5 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
166b6 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42  if( rc==SQLITE_B
166b7 55 53 59 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63  USY ){.    p->rc
166b8 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42   = rc = SQLITE_B
166b9 55 53 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  USY;.  }else{.  
166ba 20 20 72 63 20 3d 20 70 2d 3e 72 63 20 3f 20 53    rc = p->rc ? S
166bb 51 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20 53 51  QLITE_ERROR : SQ
166bc 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20  LITE_DONE;.  }. 
166bd 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72   goto vdbe_retur
166be 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  n;.}../* Opcode:
166bf 20 49 6e 74 65 67 65 72 20 50 31 20 50 32 20 2a   Integer P1 P2 *
166c0 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 33   * *.**.** The 3
166c1 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76 61  2-bit integer va
166c2 6c 75 65 20 50 31 20 69 73 20 77 72 69 74 74 65  lue P1 is writte
166c3 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20  n into register 
166c4 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49  P2..*/.case OP_I
166c5 6e 74 65 67 65 72 3a 20 7b 20 20 20 20 20 20 20  nteger: {       
166c6 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
166c7 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 2d 3e  ease */.  pOut->
166c8 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
166c9 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 70  .  pOut->u.i = p
166ca 4f 70 2d 3e 70 31 3b 0a 20 20 62 72 65 61 6b 3b  Op->p1;.  break;
166cb 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49  .}../* Opcode: I
166cc 6e 74 36 34 20 2a 20 50 32 20 2a 20 50 34 20 2a  nt64 * P2 * P4 *
166cd 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70  .**.** P4 is a p
166ce 6f 69 6e 74 65 72 20 74 6f 20 61 20 36 34 2d 62  ointer to a 64-b
166cf 69 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  it integer value
166d0 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 61 74 20  ..** Write that 
166d1 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 73  value into regis
166d2 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20  ter P2..*/.case 
166d3 4f 50 5f 49 6e 74 36 34 3a 20 7b 20 20 20 20 20  OP_Int64: {     
166d4 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
166d5 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73  erelease */.  as
166d6 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 70 49  sert( pOp->p4.pI
166d7 36 34 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d  64!=0 );.  pOut-
166d8 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74  >flags = MEM_Int
166d9 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20  ;.  pOut->u.i = 
166da 2a 70 4f 70 2d 3e 70 34 2e 70 49 36 34 3b 0a 20  *pOp->p4.pI64;. 
166db 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
166dc 63 6f 64 65 3a 20 52 65 61 6c 20 2a 20 50 32 20  code: Real * P2 
166dd 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20  * P4 *.**.** P4 
166de 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
166df 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e  a 64-bit floatin
166e0 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 2e 0a 2a  g point value..*
166e1 2a 20 57 72 69 74 65 20 74 68 61 74 20 76 61 6c  * Write that val
166e2 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72  ue into register
166e3 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f   P2..*/.case OP_
166e4 52 65 61 6c 3a 20 7b 20 20 20 20 20 20 20 20 20  Real: {         
166e5 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
166e6 5f 46 4c 4f 41 54 2c 20 6f 75 74 32 2d 70 72 65  _FLOAT, out2-pre
166e7 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75  release */.  pOu
166e8 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52  t->flags = MEM_R
166e9 65 61 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 21  eal;.  assert( !
166ea 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 2a 70 4f  sqlite3IsNaN(*pO
166eb 70 2d 3e 70 34 2e 70 52 65 61 6c 29 20 29 3b 0a  p->p4.pReal) );.
166ec 20 20 70 4f 75 74 2d 3e 72 20 3d 20 2a 70 4f 70    pOut->r = *pOp
166ed 2d 3e 70 34 2e 70 52 65 61 6c 3b 0a 20 20 62 72  ->p4.pReal;.  br
166ee 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
166ef 65 3a 20 53 74 72 69 6e 67 38 20 2a 20 50 32 20  e: String8 * P2 
166f0 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20  * P4 *.**.** P4 
166f1 70 6f 69 6e 74 73 20 74 6f 20 61 20 6e 75 6c 20  points to a nul 
166f2 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38  terminated UTF-8
166f3 20 73 74 72 69 6e 67 2e 20 54 68 69 73 20 6f 70   string. This op
166f4 63 6f 64 65 20 69 73 20 74 72 61 6e 73 66 6f 72  code is transfor
166f5 6d 65 64 20 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20  med .** into an 
166f6 4f 50 5f 53 74 72 69 6e 67 20 62 65 66 6f 72 65  OP_String before
166f7 20 69 74 20 69 73 20 65 78 65 63 75 74 65 64 20   it is executed 
166f8 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 69  for the first ti
166f9 6d 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  me..*/.case OP_S
166fa 74 72 69 6e 67 38 3a 20 7b 20 20 20 20 20 20 20  tring8: {       
166fb 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
166fc 53 54 52 49 4e 47 2c 20 6f 75 74 32 2d 70 72 65  STRING, out2-pre
166fd 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73  release */.  ass
166fe 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d  ert( pOp->p4.z!=
166ff 30 20 29 3b 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f  0 );.  pOp->opco
16700 64 65 20 3d 20 4f 50 5f 53 74 72 69 6e 67 3b 0a  de = OP_String;.
16701 20 20 70 4f 70 2d 3e 70 31 20 3d 20 73 74 72 6c    pOp->p1 = strl
16702 65 6e 28 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 0a  en(pOp->p4.z);..
16703 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
16704 4d 49 54 5f 55 54 46 31 36 0a 20 20 69 66 28 20  MIT_UTF16.  if( 
16705 65 6e 63 6f 64 69 6e 67 21 3d 53 51 4c 49 54 45  encoding!=SQLITE
16706 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 73 71 6c  _UTF8 ){.    sql
16707 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
16708 72 28 70 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 2e  r(pOut, pOp->p4.
16709 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54  z, -1, SQLITE_UT
1670a 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  F8, SQLITE_STATI
1670b 43 29 3b 0a 20 20 20 20 69 66 28 20 53 51 4c 49  C);.    if( SQLI
1670c 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 56 64  TE_OK!=sqlite3Vd
1670d 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67  beChangeEncoding
1670e 28 70 4f 75 74 2c 20 65 6e 63 6f 64 69 6e 67 29  (pOut, encoding)
1670f 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
16710 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f      if( SQLITE_O
16711 4b 21 3d 73 71 6c 69 74 65 33 56 64 62 65 4d 65  K!=sqlite3VdbeMe
16712 6d 44 79 6e 61 6d 69 63 69 66 79 28 70 4f 75 74  mDynamicify(pOut
16713 29 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b  ) ) goto no_mem;
16714 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c  .    pOut->zMall
16715 6f 63 20 3d 20 30 3b 0a 20 20 20 20 70 4f 75 74  oc = 0;.    pOut
16716 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53  ->flags |= MEM_S
16717 74 61 74 69 63 3b 0a 20 20 20 20 70 4f 75 74 2d  tatic;.    pOut-
16718 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 44  >flags &= ~MEM_D
16719 79 6e 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d  yn;.    if( pOp-
1671a 3e 70 34 74 79 70 65 3d 3d 50 34 5f 44 59 4e 41  >p4type==P4_DYNA
1671b 4d 49 43 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  MIC ){.      sql
1671c 69 74 65 33 5f 66 72 65 65 28 70 4f 70 2d 3e 70  ite3_free(pOp->p
1671d 34 2e 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  4.z);.    }.    
1671e 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34  pOp->p4type = P4
1671f 5f 44 59 4e 41 4d 49 43 3b 0a 20 20 20 20 70 4f  _DYNAMIC;.    pO
16720 70 2d 3e 70 34 2e 7a 20 3d 20 70 4f 75 74 2d 3e  p->p4.z = pOut->
16721 7a 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 31 20 3d  z;.    pOp->p1 =
16722 20 70 4f 75 74 2d 3e 6e 3b 0a 20 20 20 20 69 66   pOut->n;.    if
16723 28 20 70 4f 70 2d 3e 70 31 3e 64 62 2d 3e 61 4c  ( pOp->p1>db->aL
16724 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
16725 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20  T_LENGTH] ){.   
16726 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b     goto too_big;
16727 0a 20 20 20 20 7d 0a 20 20 20 20 55 50 44 41 54  .    }.    UPDAT
16728 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70  E_MAX_BLOBSIZE(p
16729 4f 75 74 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b  Out);.    break;
1672a 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66  .  }.#endif.  if
1672b 28 20 70 4f 70 2d 3e 70 31 3e 64 62 2d 3e 61 4c  ( pOp->p1>db->aL
1672c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
1672d 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20  T_LENGTH] ){.   
1672e 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20   goto too_big;. 
1672f 20 7d 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72   }.  /* Fall thr
16730 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74  ough to the next
16731 20 63 61 73 65 2c 20 4f 50 5f 53 74 72 69 6e 67   case, OP_String
16732 20 2a 2f 0a 7d 0a 20 20 0a 2f 2a 20 4f 70 63 6f   */.}.  ./* Opco
16733 64 65 3a 20 53 74 72 69 6e 67 20 50 31 20 50 32  de: String P1 P2
16734 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 68   * P4 *.**.** Th
16735 65 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 50  e string value P
16736 34 20 6f 66 20 6c 65 6e 67 74 68 20 50 31 20 28  4 of length P1 (
16737 62 79 74 65 73 29 20 69 73 20 73 74 6f 72 65 64  bytes) is stored
16738 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e   in register P2.
16739 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 74 72 69  .*/.case OP_Stri
1673a 6e 67 3a 20 7b 20 20 20 20 20 20 20 20 20 20 2f  ng: {          /
1673b 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73  * out2-prereleas
1673c 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  e */.  assert( p
1673d 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 3b 0a 20  Op->p4.z!=0 );. 
1673e 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d   pOut->flags = M
1673f 45 4d 5f 53 74 72 7c 4d 45 4d 5f 53 74 61 74 69  EM_Str|MEM_Stati
16740 63 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 70 4f  c|MEM_Term;.  pO
16741 75 74 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e  ut->z = pOp->p4.
16742 7a 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 70  z;.  pOut->n = p
16743 4f 70 2d 3e 70 31 3b 0a 20 20 70 4f 75 74 2d 3e  Op->p1;.  pOut->
16744 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a  enc = encoding;.
16745 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f    UPDATE_MAX_BLO
16746 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62  BSIZE(pOut);.  b
16747 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
16748 64 65 3a 20 4e 75 6c 6c 20 2a 20 50 32 20 2a 20  de: Null * P2 * 
16749 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20  * *.**.** Write 
1674a 61 20 4e 55 4c 4c 20 69 6e 74 6f 20 72 65 67 69  a NULL into regi
1674b 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65  ster P2..*/.case
1674c 20 4f 50 5f 4e 75 6c 6c 3a 20 7b 20 20 20 20 20   OP_Null: {     
1674d 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
1674e 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 62 72  erelease */.  br
1674f 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66  eak;.}...#ifndef
16750 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f   SQLITE_OMIT_BLO
16751 42 5f 4c 49 54 45 52 41 4c 0a 2f 2a 20 4f 70 63  B_LITERAL./* Opc
16752 6f 64 65 3a 20 42 6c 6f 62 20 50 31 20 50 32 20  ode: Blob P1 P2 
16753 2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34 20 70 6f  * P4.**.** P4 po
16754 69 6e 74 73 20 74 6f 20 61 20 62 6c 6f 62 20 6f  ints to a blob o
16755 66 20 64 61 74 61 20 50 31 20 62 79 74 65 73 20  f data P1 bytes 
16756 6c 6f 6e 67 2e 20 20 53 74 6f 72 65 20 74 68 69  long.  Store thi
16757 73 0a 2a 2a 20 62 6c 6f 62 20 69 6e 20 72 65 67  s.** blob in reg
16758 69 73 74 65 72 20 50 32 2e 20 54 68 69 73 20 69  ister P2. This i
16759 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 6e 6f  nstruction is no
1675a 74 20 63 6f 64 65 64 20 64 69 72 65 63 74 6c 79  t coded directly
1675b 0a 2a 2a 20 62 79 20 74 68 65 20 63 6f 6d 70 69  .** by the compi
1675c 6c 65 72 2e 20 49 6e 73 74 65 61 64 2c 20 74 68  ler. Instead, th
1675d 65 20 63 6f 6d 70 69 6c 65 72 20 6c 61 79 65 72  e compiler layer
1675e 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 61 6e   specifies.** an
1675f 20 4f 50 5f 48 65 78 42 6c 6f 62 20 6f 70 63 6f   OP_HexBlob opco
16760 64 65 2c 20 77 69 74 68 20 74 68 65 20 68 65 78  de, with the hex
16761 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e   string represen
16762 74 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65  tation of.** the
16763 20 62 6c 6f 62 20 61 73 20 50 34 2e 20 54 68 69   blob as P4. Thi
16764 73 20 6f 70 63 6f 64 65 20 69 73 20 74 72 61 6e  s opcode is tran
16765 73 66 6f 72 6d 65 64 20 74 6f 20 61 6e 20 4f 50  sformed to an OP
16766 5f 42 6c 6f 62 0a 2a 2a 20 74 68 65 20 66 69 72  _Blob.** the fir
16767 73 74 20 74 69 6d 65 20 69 74 20 69 73 20 65 78  st time it is ex
16768 65 63 75 74 65 64 2e 0a 2a 2f 0a 63 61 73 65 20  ecuted..*/.case 
16769 4f 50 5f 42 6c 6f 62 3a 20 7b 20 20 20 20 20 20  OP_Blob: {      
1676a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74            /* out
1676b 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a  2-prerelease */.
1676c 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1676d 31 20 3c 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f  1 <= SQLITE_MAX_
1676e 4c 45 4e 47 54 48 20 29 3b 0a 20 20 73 71 6c 69  LENGTH );.  sqli
1676f 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72  te3VdbeMemSetStr
16770 28 70 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a  (pOut, pOp->p4.z
16771 2c 20 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 30 29  , pOp->p1, 0, 0)
16772 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20  ;.  pOut->enc = 
16773 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 55 50 44 41  encoding;.  UPDA
16774 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28  TE_MAX_BLOBSIZE(
16775 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  pOut);.  break;.
16776 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
16777 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54  TE_OMIT_BLOB_LIT
16778 45 52 41 4c 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f  ERAL */../* Opco
16779 64 65 3a 20 56 61 72 69 61 62 6c 65 20 50 31 20  de: Variable P1 
1677a 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54  P2 * * *.**.** T
1677b 68 65 20 76 61 6c 75 65 20 6f 66 20 76 61 72 69  he value of vari
1677c 61 62 6c 65 20 50 31 20 69 73 20 77 72 69 74 74  able P1 is writt
1677d 65 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72  en into register
1677e 20 50 32 2e 20 41 20 76 61 72 69 61 62 6c 65 20   P2. A variable 
1677f 69 73 0a 2a 2a 20 61 6e 20 75 6e 6b 6e 6f 77 6e  is.** an unknown
16780 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   in the original
16781 20 53 51 4c 20 73 74 72 69 6e 67 20 61 73 20 68   SQL string as h
16782 61 6e 64 65 64 20 74 6f 20 73 71 6c 69 74 65 33  anded to sqlite3
16783 5f 63 6f 6d 70 69 6c 65 28 29 2e 0a 2a 2a 20 41  _compile()..** A
16784 6e 79 20 6f 63 63 75 72 61 6e 63 65 20 6f 66 20  ny occurance of 
16785 74 68 65 20 27 3f 27 20 63 68 61 72 61 63 74 65  the '?' characte
16786 72 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61  r in the origina
16787 6c 20 53 51 4c 20 69 73 20 63 6f 6e 73 69 64 65  l SQL is conside
16788 72 65 64 0a 2a 2a 20 61 20 76 61 72 69 61 62 6c  red.** a variabl
16789 65 2e 20 20 56 61 72 69 61 62 6c 65 73 20 69 6e  e.  Variables in
1678a 20 74 68 65 20 53 51 4c 20 73 74 72 69 6e 67 20   the SQL string 
1678b 61 72 65 20 6e 75 6d 62 65 72 20 66 72 6f 6d 20  are number from 
1678c 6c 65 66 74 20 74 6f 0a 2a 2a 20 72 69 67 68 74  left to.** right
1678d 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20   beginning with 
1678e 31 2e 20 20 54 68 65 20 76 61 6c 75 65 73 20 6f  1.  The values o
1678f 66 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20  f variables are 
16790 73 65 74 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a  set using the.**
16791 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 20   sqlite3_bind() 
16792 41 50 49 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  API..*/.case OP_
16793 56 61 72 69 61 62 6c 65 3a 20 7b 20 20 20 20 20  Variable: {     
16794 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
16795 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e  erelease */.  in
16796 74 20 6a 20 3d 20 70 4f 70 2d 3e 70 31 20 2d 20  t j = pOp->p1 - 
16797 31 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a  1;.  Mem *pVar;.
16798 20 20 61 73 73 65 72 74 28 20 6a 3e 3d 30 20 26    assert( j>=0 &
16799 26 20 6a 3c 70 2d 3e 6e 56 61 72 20 29 3b 0a 0a  & j<p->nVar );..
1679a 20 20 70 56 61 72 20 3d 20 26 70 2d 3e 61 56 61    pVar = &p->aVa
1679b 72 5b 6a 5d 3b 0a 20 20 69 66 28 20 73 71 6c 69  r[j];.  if( sqli
1679c 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67  te3VdbeMemTooBig
1679d 28 70 56 61 72 29 20 29 7b 0a 20 20 20 20 67 6f  (pVar) ){.    go
1679e 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a  to too_big;.  }.
1679f 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
167a0 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74  ShallowCopy(pOut
167a1 2c 20 26 70 2d 3e 61 56 61 72 5b 6a 5d 2c 20 4d  , &p->aVar[j], M
167a2 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 55 50  EM_Static);.  UP
167a3 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a  DATE_MAX_BLOBSIZ
167a4 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b  E(pOut);.  break
167a5 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
167a6 4d 6f 76 65 20 50 31 20 50 32 20 2a 20 2a 20 2a  Move P1 P2 * * *
167a7 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20  .**.** Move the 
167a8 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
167a9 72 20 50 31 20 6f 76 65 72 20 69 6e 74 6f 20 72  r P1 over into r
167aa 65 67 69 73 74 65 72 20 50 32 2e 20 20 52 65 67  egister P2.  Reg
167ab 69 73 74 65 72 20 50 31 0a 2a 2a 20 69 73 20 6c  ister P1.** is l
167ac 65 66 74 20 68 6f 6c 64 69 6e 67 20 61 20 4e 55  eft holding a NU
167ad 4c 4c 2e 20 20 49 74 20 69 73 20 61 6e 20 65 72  LL.  It is an er
167ae 72 6f 72 20 66 6f 72 20 50 31 20 61 6e 64 20 50  ror for P1 and P
167af 32 20 74 6f 20 62 65 20 74 68 65 0a 2a 2a 20 73  2 to be the.** s
167b0 61 6d 65 20 72 65 67 69 73 74 65 72 2e 0a 2a 2f  ame register..*/
167b1 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65 3a 20 7b  .case OP_Move: {
167b2 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63  .  char *zMalloc
167b3 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
167b4 3e 70 31 3e 30 20 29 3b 0a 20 20 61 73 73 65 72  >p1>0 );.  asser
167b5 74 28 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e  t( pOp->p1<=p->n
167b6 4d 65 6d 20 29 3b 0a 20 20 70 49 6e 31 20 3d 20  Mem );.  pIn1 = 
167b7 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31  &p->aMem[pOp->p1
167b8 5d 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52  ];.  REGISTER_TR
167b9 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70 49 6e  ACE(pOp->p1, pIn
167ba 31 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  1);.  assert( pO
167bb 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 61 73 73  p->p2>0 );.  ass
167bc 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d  ert( pOp->p2<=p-
167bd 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74 20  >nMem );.  pOut 
167be 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  = &p->aMem[pOp->
167bf 70 32 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  p2];.  assert( p
167c0 4f 75 74 21 3d 70 49 6e 31 20 29 3b 0a 20 20 7a  Out!=pIn1 );.  z
167c1 4d 61 6c 6c 6f 63 20 3d 20 70 4f 75 74 2d 3e 7a  Malloc = pOut->z
167c2 4d 61 6c 6c 6f 63 3b 0a 20 20 70 4f 75 74 2d 3e  Malloc;.  pOut->
167c3 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 73  zMalloc = 0;.  s
167c4 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76  qlite3VdbeMemMov
167c5 65 28 70 4f 75 74 2c 20 70 49 6e 31 29 3b 0a 20  e(pOut, pIn1);. 
167c6 20 70 49 6e 31 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d   pIn1->zMalloc =
167c7 20 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 52 45 47 49   zMalloc;.  REGI
167c8 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
167c9 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 62 72 65  p2, pOut);.  bre
167ca 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
167cb 3a 20 43 6f 70 79 20 50 31 20 50 32 20 2a 20 2a  : Copy P1 P2 * *
167cc 20 2a 0a 2a 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20   *.**.** Make a 
167cd 63 6f 70 79 20 6f 66 20 72 65 67 69 73 74 65 72  copy of register
167ce 20 50 31 20 69 6e 74 6f 20 72 65 67 69 73 74 65   P1 into registe
167cf 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  r P2..**.** This
167d0 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6d 61 6b   instruction mak
167d1 65 73 20 61 20 64 65 65 70 20 63 6f 70 79 20 6f  es a deep copy o
167d2 66 20 74 68 65 20 76 61 6c 75 65 2e 20 20 41 20  f the value.  A 
167d3 64 75 70 6c 69 63 61 74 65 0a 2a 2a 20 69 73 20  duplicate.** is 
167d4 6d 61 64 65 20 6f 66 20 61 6e 79 20 73 74 72 69  made of any stri
167d5 6e 67 20 6f 72 20 62 6c 6f 62 20 63 6f 6e 73 74  ng or blob const
167d6 61 6e 74 2e 20 20 53 65 65 20 61 6c 73 6f 20 4f  ant.  See also O
167d7 50 5f 53 43 6f 70 79 2e 0a 2a 2f 0a 63 61 73 65  P_SCopy..*/.case
167d8 20 4f 50 5f 43 6f 70 79 3a 20 7b 0a 20 20 61 73   OP_Copy: {.  as
167d9 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20  sert( pOp->p1>0 
167da 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
167db 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b  ->p1<=p->nMem );
167dc 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d  .  pIn1 = &p->aM
167dd 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 52  em[pOp->p1];.  R
167de 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
167df 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20  p->p1, pIn1);.  
167e0 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e  assert( pOp->p2>
167e1 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
167e2 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p2<=p->nMem 
167e3 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e  );.  pOut = &p->
167e4 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20  aMem[pOp->p2];. 
167e5 20 61 73 73 65 72 74 28 20 70 4f 75 74 21 3d 70   assert( pOut!=p
167e6 49 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  In1 );.  sqlite3
167e7 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f  VdbeMemShallowCo
167e8 70 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c 20 4d  py(pOut, pIn1, M
167e9 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 44 65 65  EM_Ephem);.  Dee
167ea 70 68 65 6d 65 72 61 6c 69 7a 65 28 70 4f 75 74  phemeralize(pOut
167eb 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52  );.  REGISTER_TR
167ec 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75  ACE(pOp->p2, pOu
167ed 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  t);.  break;.}..
167ee 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 43 6f 70 79  /* Opcode: SCopy
167ef 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
167f0 2a 2a 20 4d 61 6b 65 20 61 20 73 68 61 6c 6c 6f  ** Make a shallo
167f1 77 20 63 6f 70 79 20 6f 66 20 72 65 67 69 73 74  w copy of regist
167f2 65 72 20 50 31 20 69 6e 74 6f 20 72 65 67 69 73  er P1 into regis
167f3 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68  ter P2..**.** Th
167f4 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6d  is instruction m
167f5 61 6b 65 73 20 61 20 73 68 61 6c 6c 6f 77 20 63  akes a shallow c
167f6 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65  opy of the value
167f7 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a  .  If the value.
167f8 2a 2a 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f  ** is a string o
167f9 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20 74 68 65  r blob, then the
167fa 20 63 6f 70 79 20 69 73 20 6f 6e 6c 79 20 61 20   copy is only a 
167fb 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a  pointer to the.*
167fc 2a 20 6f 72 69 67 69 6e 61 6c 20 61 6e 64 20 68  * original and h
167fd 65 6e 63 65 20 69 66 20 74 68 65 20 6f 72 69 67  ence if the orig
167fe 69 6e 61 6c 20 63 68 61 6e 67 65 73 20 73 6f 20  inal changes so 
167ff 77 69 6c 6c 20 74 68 65 20 63 6f 70 79 2e 0a 2a  will the copy..*
16800 2a 20 57 6f 72 73 65 2c 20 69 66 20 74 68 65 20  * Worse, if the 
16801 6f 72 69 67 69 6e 61 6c 20 69 73 20 64 65 61 6c  original is deal
16802 6c 6f 63 61 74 65 64 2c 20 74 68 65 20 63 6f 70  located, the cop
16803 79 20 62 65 63 6f 6d 65 73 20 69 6e 76 61 6c 69  y becomes invali
16804 64 2e 0a 2a 2a 20 54 68 75 73 20 74 68 65 20 70  d..** Thus the p
16805 72 6f 67 72 61 6d 20 6d 75 73 74 20 67 75 61 72  rogram must guar
16806 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 6f  antee that the o
16807 72 69 67 69 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74  riginal will not
16808 20 63 68 61 6e 67 65 0a 2a 2a 20 64 75 72 69 6e   change.** durin
16809 67 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f  g the lifetime o
1680a 66 20 74 68 65 20 63 6f 70 79 2e 20 20 55 73 65  f the copy.  Use
1680b 20 4f 50 5f 43 6f 70 79 20 74 6f 20 6d 61 6b 65   OP_Copy to make
1680c 20 61 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 63   a complete.** c
1680d 6f 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  opy..*/.case OP_
1680e 53 43 6f 70 79 3a 20 7b 0a 20 20 61 73 73 65 72  SCopy: {.  asser
1680f 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a  t( pOp->p1>0 );.
16810 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
16811 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  1<=p->nMem );.  
16812 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pIn1 = &p->aMem[
16813 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 52 45 47 49  pOp->p1];.  REGI
16814 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
16815 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 61 73 73  p1, pIn1);.  ass
16816 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29  ert( pOp->p2>0 )
16817 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
16818 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p2<=p->nMem );.
16819 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65    pOut = &p->aMe
1681a 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73  m[pOp->p2];.  as
1681b 73 65 72 74 28 20 70 4f 75 74 21 3d 70 49 6e 31  sert( pOut!=pIn1
1681c 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62   );.  sqlite3Vdb
1681d 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28  eMemShallowCopy(
1681e 70 4f 75 74 2c 20 70 49 6e 31 2c 20 4d 45 4d 5f  pOut, pIn1, MEM_
1681f 45 70 68 65 6d 29 3b 0a 20 20 52 45 47 49 53 54  Ephem);.  REGIST
16820 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32  ER_TRACE(pOp->p2
16821 2c 20 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b  , pOut);.  break
16822 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
16823 52 65 73 75 6c 74 52 6f 77 20 50 31 20 50 32 20  ResultRow P1 P2 
16824 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20  * * *.**.** The 
16825 72 65 67 69 73 74 65 72 73 20 50 31 20 74 68 72  registers P1 thr
16826 6f 75 67 68 74 20 50 31 2b 50 32 2d 31 20 63 6f  ought P1+P2-1 co
16827 6e 74 61 69 6e 20 61 20 73 69 6e 67 6c 65 20 72  ntain a single r
16828 6f 77 20 6f 66 0a 2a 2a 20 72 65 73 75 6c 74 73  ow of.** results
16829 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20 63 61  . This opcode ca
1682a 75 73 65 73 20 74 68 65 20 73 71 6c 69 74 65 33  uses the sqlite3
1682b 5f 73 74 65 70 28 29 20 63 61 6c 6c 20 74 6f 20  _step() call to 
1682c 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 77 69 74  terminate.** wit
1682d 68 20 61 6e 20 53 51 4c 49 54 45 5f 52 4f 57 20  h an SQLITE_ROW 
1682e 72 65 74 75 72 6e 20 63 6f 64 65 20 61 6e 64 20  return code and 
1682f 69 74 20 73 65 74 73 20 75 70 20 74 68 65 20 73  it sets up the s
16830 71 6c 69 74 65 33 5f 73 74 6d 74 0a 2a 2a 20 73  qlite3_stmt.** s
16831 74 72 75 63 74 75 72 65 20 74 6f 20 70 72 6f 76  tructure to prov
16832 69 64 65 20 61 63 63 65 73 73 20 74 6f 20 74 68  ide access to th
16833 65 20 74 6f 70 20 50 31 20 76 61 6c 75 65 73 20  e top P1 values 
16834 61 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a  as the result.**
16835 20 72 6f 77 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50   row..*/.case OP
16836 5f 52 65 73 75 6c 74 52 6f 77 3a 20 7b 0a 20 20  _ResultRow: {.  
16837 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e 74  Mem *pMem;.  int
16838 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   i;.  assert( p-
16839 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3d 3d 70 4f 70  >nResColumn==pOp
1683a 2d 3e 70 32 20 29 3b 0a 20 20 61 73 73 65 72 74  ->p2 );.  assert
1683b 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20  ( pOp->p1>0 );. 
1683c 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31   assert( pOp->p1
1683d 2b 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65  +pOp->p2<=p->nMe
1683e 6d 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76 61 6c  m );..  /* Inval
1683f 69 64 61 74 65 20 61 6c 6c 20 65 70 68 65 6d 65  idate all epheme
16840 72 61 6c 20 63 75 72 73 6f 72 20 72 6f 77 20 63  ral cursor row c
16841 61 63 68 65 73 20 2a 2f 0a 20 20 70 2d 3e 63 61  aches */.  p->ca
16842 63 68 65 43 74 72 20 3d 20 28 70 2d 3e 63 61 63  cheCtr = (p->cac
16843 68 65 43 74 72 20 2b 20 32 29 7c 31 3b 0a 0a 20  heCtr + 2)|1;.. 
16844 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68   /* Make sure th
16845 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65  e results of the
16846 20 63 75 72 72 65 6e 74 20 72 6f 77 20 61 72 65   current row are
16847 20 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64   \000 terminated
16848 0a 20 20 2a 2a 20 61 6e 64 20 68 61 76 65 20 61  .  ** and have a
16849 6e 20 61 73 73 69 67 6e 65 64 20 74 79 70 65 2e  n assigned type.
1684a 20 20 54 68 65 20 72 65 73 75 6c 74 73 20 61 72    The results ar
1684b 65 20 64 65 65 70 68 65 6d 65 72 61 6c 69 7a 65  e deephemeralize
1684c 64 20 61 73 0a 20 20 2a 2a 20 61 73 20 73 69 64  d as.  ** as sid
1684d 65 20 65 66 66 65 63 74 2e 0a 20 20 2a 2f 0a 20  e effect..  */. 
1684e 20 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75   pMem = p->pResu
1684f 6c 74 53 65 74 20 3d 20 26 70 2d 3e 61 4d 65 6d  ltSet = &p->aMem
16850 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72  [pOp->p1];.  for
16851 28 69 3d 30 3b 20 69 3c 70 4f 70 2d 3e 70 32 3b  (i=0; i<pOp->p2;
16852 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74   i++){.    sqlit
16853 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d  e3VdbeMemNulTerm
16854 69 6e 61 74 65 28 26 70 4d 65 6d 5b 69 5d 29 3b  inate(&pMem[i]);
16855 0a 20 20 20 20 73 74 6f 72 65 54 79 70 65 49 6e  .    storeTypeIn
16856 66 6f 28 26 70 4d 65 6d 5b 69 5d 2c 20 65 6e 63  fo(&pMem[i], enc
16857 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a 20 20 69 66  oding);.  }.  if
16858 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
16859 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d  ed ) goto no_mem
1685a 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 53  ;..  /* Return S
1685b 51 4c 49 54 45 5f 52 4f 57 0a 20 20 2a 2f 0a 20  QLITE_ROW.  */. 
1685c 20 70 2d 3e 6e 43 61 6c 6c 62 61 63 6b 2b 2b 3b   p->nCallback++;
1685d 0a 20 20 70 2d 3e 70 63 20 3d 20 70 63 20 2b 20  .  p->pc = pc + 
1685e 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45  1;.  rc = SQLITE
1685f 5f 52 4f 57 3b 0a 20 20 67 6f 74 6f 20 76 64 62  _ROW;.  goto vdb
16860 65 5f 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20  e_return;.}../* 
16861 4f 70 63 6f 64 65 3a 20 43 6f 6e 63 61 74 20 50  Opcode: Concat P
16862 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
16863 2a 20 41 64 64 20 74 68 65 20 74 65 78 74 20 69  * Add the text i
16864 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 6f 6e  n register P1 on
16865 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  to the end of th
16866 65 20 74 65 78 74 20 69 6e 0a 2a 2a 20 72 65 67  e text in.** reg
16867 69 73 74 65 72 20 50 32 20 61 6e 64 20 73 74 6f  ister P2 and sto
16868 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e  re the result in
16869 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a   register P3..**
1686a 20 49 66 20 65 69 74 68 65 72 20 74 68 65 20 50   If either the P
1686b 31 20 6f 72 20 50 32 20 74 65 78 74 20 61 72 65  1 or P2 text are
1686c 20 4e 55 4c 4c 20 74 68 65 6e 20 73 74 6f 72 65   NULL then store
1686d 20 4e 55 4c 4c 20 69 6e 20 50 33 2e 0a 2a 2a 0a   NULL in P3..**.
1686e 2a 2a 20 20 20 50 33 20 3d 20 50 32 20 7c 7c 20  **   P3 = P2 || 
1686f 50 31 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69  P1.**.** It is i
16870 6c 6c 65 67 61 6c 20 66 6f 72 20 50 31 20 61 6e  llegal for P1 an
16871 64 20 50 33 20 74 6f 20 62 65 20 74 68 65 20 73  d P3 to be the s
16872 61 6d 65 20 72 65 67 69 73 74 65 72 2e 20 53 6f  ame register. So
16873 6d 65 74 69 6d 65 73 2c 0a 2a 2a 20 69 66 20 50  metimes,.** if P
16874 33 20 69 73 20 74 68 65 20 73 61 6d 65 20 72 65  3 is the same re
16875 67 69 73 74 65 72 20 61 73 20 50 32 2c 20 74 68  gister as P2, th
16876 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
16877 20 69 73 20 61 62 6c 65 0a 2a 2a 20 74 6f 20 61   is able.** to a
16878 76 6f 69 64 20 61 20 6d 65 6d 63 70 79 28 29 2e  void a memcpy().
16879 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6e 63  .*/.case OP_Conc
1687a 61 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  at: {           
1687b 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 43 4f  /* same as TK_CO
1687c 4e 43 41 54 2c 20 69 6e 31 2c 20 69 6e 32 2c 20  NCAT, in1, in2, 
1687d 6f 75 74 33 20 2a 2f 0a 20 20 69 36 34 20 6e 42  out3 */.  i64 nB
1687e 79 74 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  yte;..  assert( 
1687f 70 49 6e 31 21 3d 70 4f 75 74 20 29 3b 0a 20 20  pIn1!=pOut );.  
16880 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73  if( (pIn1->flags
16881 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 73 29 20   | pIn2->flags) 
16882 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20  & MEM_Null ){.  
16883 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
16884 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20  SetNull(pOut);. 
16885 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20     break;.  }.  
16886 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29  ExpandBlob(pIn1)
16887 3b 0a 20 20 53 74 72 69 6e 67 69 66 79 28 70 49  ;.  Stringify(pI
16888 6e 31 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20  n1, encoding);. 
16889 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 32   ExpandBlob(pIn2
1688a 29 3b 0a 20 20 53 74 72 69 6e 67 69 66 79 28 70  );.  Stringify(p
1688b 49 6e 32 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a  In2, encoding);.
1688c 20 20 6e 42 79 74 65 20 3d 20 70 49 6e 31 2d 3e    nByte = pIn1->
1688d 6e 20 2b 20 70 49 6e 32 2d 3e 6e 3b 0a 20 20 69  n + pIn2->n;.  i
1688e 66 28 20 6e 42 79 74 65 3e 64 62 2d 3e 61 4c 69  f( nByte>db->aLi
1688f 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
16890 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20  _LENGTH] ){.    
16891 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20  goto too_big;.  
16892 7d 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c  }.  MemSetTypeFl
16893 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 53 74 72  ag(pOut, MEM_Str
16894 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
16895 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74  VdbeMemGrow(pOut
16896 2c 20 6e 42 79 74 65 2b 32 2c 20 70 4f 75 74 3d  , nByte+2, pOut=
16897 3d 70 49 6e 32 29 20 29 7b 0a 20 20 20 20 67 6f  =pIn2) ){.    go
16898 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20  to no_mem;.  }. 
16899 20 69 66 28 20 70 4f 75 74 21 3d 70 49 6e 32 20   if( pOut!=pIn2 
1689a 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4f  ){.    memcpy(pO
1689b 75 74 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 7a 2c 20  ut->z, pIn2->z, 
1689c 70 49 6e 32 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 20  pIn2->n);.  }.  
1689d 6d 65 6d 63 70 79 28 26 70 4f 75 74 2d 3e 7a 5b  memcpy(&pOut->z[
1689e 70 49 6e 32 2d 3e 6e 5d 2c 20 70 49 6e 31 2d 3e  pIn2->n], pIn1->
1689f 7a 2c 20 70 49 6e 31 2d 3e 6e 29 3b 0a 20 20 70  z, pIn1->n);.  p
168a0 4f 75 74 2d 3e 7a 5b 6e 42 79 74 65 5d 20 3d 20  Out->z[nByte] = 
168a1 30 3b 0a 20 20 70 4f 75 74 2d 3e 7a 5b 6e 42 79  0;.  pOut->z[nBy
168a2 74 65 2b 31 5d 20 3d 20 30 3b 0a 20 20 70 4f 75  te+1] = 0;.  pOu
168a3 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f  t->flags |= MEM_
168a4 54 65 72 6d 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20  Term;.  pOut->n 
168a5 3d 20 6e 42 79 74 65 3b 0a 20 20 70 4f 75 74 2d  = nByte;.  pOut-
168a6 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b  >enc = encoding;
168a7 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c  .  UPDATE_MAX_BL
168a8 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20  OBSIZE(pOut);.  
168a9 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
168aa 6f 64 65 3a 20 41 64 64 20 50 31 20 50 32 20 50  ode: Add P1 P2 P
168ab 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 64 20  3 * *.**.** Add 
168ac 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
168ad 69 73 74 65 72 20 50 31 20 74 6f 20 74 68 65 20  ister P1 to the 
168ae 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
168af 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72  r P2.** and stor
168b0 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20  e the result in 
168b1 72 65 67 69 73 65 72 20 50 33 2e 0a 2a 2a 20 49  regiser P3..** I
168b2 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69  f either input i
168b3 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75  s NULL, the resu
168b4 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f  lt is NULL..*/./
168b5 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 6c 74 69 70  * Opcode: Multip
168b6 6c 79 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  ly P1 P2 P3 * *.
168b7 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 75 6c 74 69 70 6c  **.**.** Multipl
168b8 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  y the value in r
168b9 65 67 69 73 65 72 20 50 31 20 62 79 20 74 68 65  egiser P1 by the
168ba 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 65   value in regise
168bb 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72  r P2.** and stor
168bc 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20  e the result in 
168bd 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20  register P3..** 
168be 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20  If either input 
168bf 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73  is NULL, the res
168c0 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a  ult is NULL..*/.
168c1 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 75 62 74 72  /* Opcode: Subtr
168c2 61 63 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a  act P1 P2 P3 * *
168c3 0a 2a 2a 0a 2a 2a 20 53 75 62 74 72 61 63 74 20  .**.** Subtract 
168c4 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
168c5 69 73 74 65 72 20 50 31 20 66 72 6f 6d 20 74 68  ister P1 from th
168c6 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
168c7 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74  ter P2.** and st
168c8 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69  ore the result i
168c9 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  n register P3..*
168ca 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75  * If either inpu
168cb 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72  t is NULL, the r
168cc 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a  esult is NULL..*
168cd 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 69 76  /./* Opcode: Div
168ce 69 64 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a  ide P1 P2 P3 * *
168cf 0a 2a 2a 0a 2a 2a 20 44 69 76 69 64 65 20 74 68  .**.** Divide th
168d0 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
168d1 74 65 72 20 50 31 20 62 79 20 74 68 65 20 76 61  ter P1 by the va
168d2 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
168d3 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20  P2.** and store 
168d4 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65  the result in re
168d5 67 69 73 74 65 72 20 50 33 2e 20 20 49 66 20 74  gister P3.  If t
168d6 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
168d7 73 74 65 72 20 50 32 0a 2a 2a 20 69 73 20 7a 65  ster P2.** is ze
168d8 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73  ro, then the res
168d9 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 20  ult is NULL..** 
168da 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20  If either input 
168db 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73  is NULL, the res
168dc 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a  ult is NULL..*/.
168dd 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 6d 61 69  /* Opcode: Remai
168de 6e 64 65 72 20 50 31 20 50 32 20 50 33 20 2a 20  nder P1 P2 P3 * 
168df 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20  *.**.** Compute 
168e0 74 68 65 20 72 65 6d 61 69 6e 64 65 72 20 61 66  the remainder af
168e1 74 65 72 20 69 6e 74 65 67 65 72 20 64 69 76 69  ter integer divi
168e2 73 69 6f 6e 20 6f 66 20 74 68 65 20 76 61 6c 75  sion of the valu
168e3 65 20 69 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72  e in.** register
168e4 20 50 31 20 62 79 20 74 68 65 20 76 61 6c 75 65   P1 by the value
168e5 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20   in register P2 
168e6 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65  and store the re
168e7 73 75 6c 74 20 69 6e 20 50 33 2e 20 0a 2a 2a 20  sult in P3. .** 
168e8 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  If the value in 
168e9 72 65 67 69 73 74 65 72 20 50 32 20 69 73 20 7a  register P2 is z
168ea 65 72 6f 20 74 68 65 20 72 65 73 75 6c 74 20 69  ero the result i
168eb 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 20 65 69  s NULL..** If ei
168ec 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20  ther operand is 
168ed 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74  NULL, the result
168ee 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73   is NULL..*/.cas
168ef 65 20 4f 50 5f 41 64 64 3a 20 20 20 20 20 20 20  e OP_Add:       
168f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
168f1 61 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53 2c 20  ame as TK_PLUS, 
168f2 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a  in1, in2, out3 *
168f3 2f 0a 63 61 73 65 20 4f 50 5f 53 75 62 74 72 61  /.case OP_Subtra
168f4 63 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 20  ct:             
168f5 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4d   /* same as TK_M
168f6 49 4e 55 53 2c 20 69 6e 31 2c 20 69 6e 32 2c 20  INUS, in1, in2, 
168f7 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f  out3 */.case OP_
168f8 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 20 20 20  Multiply:       
168f9 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
168fa 73 20 54 4b 5f 53 54 41 52 2c 20 69 6e 31 2c 20  s TK_STAR, in1, 
168fb 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73  in2, out3 */.cas
168fc 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 20 20 20  e OP_Divide:    
168fd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
168fe 61 6d 65 20 61 73 20 54 4b 5f 53 4c 41 53 48 2c  ame as TK_SLASH,
168ff 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20   in1, in2, out3 
16900 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 6d 61 69  */.case OP_Remai
16901 6e 64 65 72 3a 20 7b 20 20 20 20 20 20 20 20 20  nder: {         
16902 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
16903 52 45 4d 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f  REM, in1, in2, o
16904 75 74 33 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61  ut3 */.  int fla
16905 67 73 3b 0a 20 20 66 6c 61 67 73 20 3d 20 70 49  gs;.  flags = pI
16906 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 6e 32  n1->flags | pIn2
16907 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 20 28  ->flags;.  if( (
16908 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
16909 29 21 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74  )!=0 ) goto arit
1690a 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73  hmetic_result_is
1690b 5f 6e 75 6c 6c 3b 0a 20 20 69 66 28 20 28 70 49  _null;.  if( (pI
1690c 6e 31 2d 3e 66 6c 61 67 73 20 26 20 70 49 6e 32  n1->flags & pIn2
1690d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ->flags & MEM_In
1690e 74 29 3d 3d 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20  t)==MEM_Int ){. 
1690f 20 20 20 69 36 34 20 61 2c 20 62 3b 0a 20 20 20     i64 a, b;.   
16910 20 61 20 3d 20 70 49 6e 31 2d 3e 75 2e 69 3b 0a   a = pIn1->u.i;.
16911 20 20 20 20 62 20 3d 20 70 49 6e 32 2d 3e 75 2e      b = pIn2->u.
16912 69 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 70  i;.    switch( p
16913 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20  Op->opcode ){.  
16914 20 20 20 20 63 61 73 65 20 4f 50 5f 41 64 64 3a      case OP_Add:
16915 20 20 20 20 20 20 20 20 20 62 20 2b 3d 20 61 3b           b += a;
16916 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
16917 20 20 20 20 63 61 73 65 20 4f 50 5f 53 75 62 74      case OP_Subt
16918 72 61 63 74 3a 20 20 20 20 62 20 2d 3d 20 61 3b  ract:    b -= a;
16919 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1691a 20 20 20 20 63 61 73 65 20 4f 50 5f 4d 75 6c 74      case OP_Mult
1691b 69 70 6c 79 3a 20 20 20 20 62 20 2a 3d 20 61 3b  iply:    b *= a;
1691c 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1691d 20 20 20 20 63 61 73 65 20 4f 50 5f 44 69 76 69      case OP_Divi
1691e 64 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 66  de: {.        if
1691f 28 20 61 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72  ( a==0 ) goto ar
16920 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f  ithmetic_result_
16921 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20  is_null;.       
16922 20 2f 2a 20 44 69 76 69 64 69 6e 67 20 74 68 65   /* Dividing the
16923 20 6c 61 72 67 65 73 74 20 70 6f 73 73 69 62 6c   largest possibl
16924 65 20 6e 65 67 61 74 69 76 65 20 36 34 2d 62 69  e negative 64-bi
16925 74 20 69 6e 74 65 67 65 72 20 28 31 3c 3c 36 33  t integer (1<<63
16926 29 20 62 79 20 0a 20 20 20 20 20 20 20 20 2a 2a  ) by .        **
16927 20 2d 31 20 72 65 74 75 72 6e 73 20 61 6e 20 69   -1 returns an i
16928 6e 74 65 67 65 72 20 74 6f 6f 20 6c 61 72 67 65  nteger too large
16929 20 74 6f 20 73 74 6f 72 65 20 69 6e 20 61 20 36   to store in a 6
1692a 34 2d 62 69 74 20 64 61 74 61 2d 74 79 70 65 2e  4-bit data-type.
1692b 20 4f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 73   On.        ** s
1692c 6f 6d 65 20 61 72 63 68 69 74 65 63 74 75 72 65  ome architecture
1692d 73 2c 20 74 68 65 20 76 61 6c 75 65 20 6f 76 65  s, the value ove
1692e 72 66 6c 6f 77 73 20 74 6f 20 28 31 3c 3c 36 33  rflows to (1<<63
1692f 29 2e 20 4f 6e 20 6f 74 68 65 72 73 2c 0a 20 20  ). On others,.  
16930 20 20 20 20 20 20 2a 2a 20 61 20 53 49 47 46 50        ** a SIGFP
16931 45 20 69 73 20 69 73 73 75 65 64 2e 20 54 68 65  E is issued. The
16932 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65   following state
16933 6d 65 6e 74 20 6e 6f 72 6d 61 6c 69 7a 65 73 20  ment normalizes 
16934 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20  this.        ** 
16935 62 65 68 61 76 69 6f 75 72 20 73 6f 20 74 68 61  behaviour so tha
16936 74 20 61 6c 6c 20 61 72 63 68 69 74 65 63 74 75  t all architectu
16937 72 65 73 20 62 65 68 61 76 65 20 61 73 20 69 66  res behave as if
16938 20 69 6e 74 65 67 65 72 20 0a 20 20 20 20 20 20   integer .      
16939 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 6f 63    ** overflow oc
1693a 63 75 72 65 64 2e 0a 20 20 20 20 20 20 20 20 2a  cured..        *
1693b 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 3d  /.        if( a=
1693c 3d 2d 31 20 26 26 20 62 3d 3d 53 4d 41 4c 4c 45  =-1 && b==SMALLE
1693d 53 54 5f 49 4e 54 36 34 20 29 20 61 20 3d 20 31  ST_INT64 ) a = 1
1693e 3b 0a 20 20 20 20 20 20 20 20 62 20 2f 3d 20 61  ;.        b /= a
1693f 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
16940 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64  .      }.      d
16941 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
16942 20 20 69 66 28 20 61 3d 3d 30 20 29 20 67 6f 74    if( a==0 ) got
16943 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73  o arithmetic_res
16944 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20  ult_is_null;.   
16945 20 20 20 20 20 69 66 28 20 61 3d 3d 2d 31 20 29       if( a==-1 )
16946 20 61 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20   a = 1;.        
16947 62 20 25 3d 20 61 3b 0a 20 20 20 20 20 20 20 20  b %= a;.        
16948 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
16949 20 20 20 7d 0a 20 20 20 20 70 4f 75 74 2d 3e 75     }.    pOut->u
1694a 2e 69 20 3d 20 62 3b 0a 20 20 20 20 4d 65 6d 53  .i = b;.    MemS
1694b 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c  etTypeFlag(pOut,
1694c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65 6c   MEM_Int);.  }el
1694d 73 65 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 61  se{.    double a
1694e 2c 20 62 3b 0a 20 20 20 20 61 20 3d 20 73 71 6c  , b;.    a = sql
1694f 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75  ite3VdbeRealValu
16950 65 28 70 49 6e 31 29 3b 0a 20 20 20 20 62 20 3d  e(pIn1);.    b =
16951 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c   sqlite3VdbeReal
16952 56 61 6c 75 65 28 70 49 6e 32 29 3b 0a 20 20 20  Value(pIn2);.   
16953 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70   switch( pOp->op
16954 63 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 63 61  code ){.      ca
16955 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 20 20 20  se OP_Add:      
16956 20 20 20 62 20 2b 3d 20 61 3b 20 20 20 20 20 20     b += a;      
16957 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
16958 73 65 20 4f 50 5f 53 75 62 74 72 61 63 74 3a 20  se OP_Subtract: 
16959 20 20 20 62 20 2d 3d 20 61 3b 20 20 20 20 20 20     b -= a;      
1695a 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
1695b 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20  se OP_Multiply: 
1695c 20 20 20 62 20 2a 3d 20 61 3b 20 20 20 20 20 20     b *= a;      
1695d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
1695e 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 7b 0a  se OP_Divide: {.
1695f 20 20 20 20 20 20 20 20 69 66 28 20 61 3d 3d 30          if( a==0
16960 2e 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d  .0 ) goto arithm
16961 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e  etic_result_is_n
16962 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 62 20 2f  ull;.        b /
16963 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 62 72 65  = a;.        bre
16964 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
16965 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
16966 20 20 20 20 20 69 36 34 20 69 61 20 3d 20 28 69       i64 ia = (i
16967 36 34 29 61 3b 0a 20 20 20 20 20 20 20 20 69 36  64)a;.        i6
16968 34 20 69 62 20 3d 20 28 69 36 34 29 62 3b 0a 20  4 ib = (i64)b;. 
16969 20 20 20 20 20 20 20 69 66 28 20 69 61 3d 3d 30         if( ia==0
1696a 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74   ) goto arithmet
1696b 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c  ic_result_is_nul
1696c 6c 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  l;.        if( i
1696d 61 3d 3d 2d 31 20 29 20 69 61 20 3d 20 31 3b 0a  a==-1 ) ia = 1;.
1696e 20 20 20 20 20 20 20 20 62 20 3d 20 69 62 20 25          b = ib %
1696f 20 69 61 3b 0a 20 20 20 20 20 20 20 20 62 72 65   ia;.        bre
16970 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
16971 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  }.    if( sqlite
16972 33 49 73 4e 61 4e 28 62 29 20 29 7b 0a 20 20 20  3IsNaN(b) ){.   
16973 20 20 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74     goto arithmet
16974 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c  ic_result_is_nul
16975 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f 75  l;.    }.    pOu
16976 74 2d 3e 72 20 3d 20 62 3b 0a 20 20 20 20 4d 65  t->r = b;.    Me
16977 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75  mSetTypeFlag(pOu
16978 74 2c 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20 20  t, MEM_Real);.  
16979 20 20 69 66 28 20 28 66 6c 61 67 73 20 26 20 4d    if( (flags & M
1697a 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20  EM_Real)==0 ){. 
1697b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1697c 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 28  IntegerAffinity(
1697d 70 4f 75 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  pOut);.    }.  }
1697e 0a 20 20 62 72 65 61 6b 3b 0a 0a 61 72 69 74 68  .  break;..arith
1697f 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f  metic_result_is_
16980 6e 75 6c 6c 3a 0a 20 20 73 71 6c 69 74 65 33 56  null:.  sqlite3V
16981 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f  dbeMemSetNull(pO
16982 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  ut);.  break;.}.
16983 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 6c  ./* Opcode: Coll
16984 53 65 71 20 2a 20 2a 20 50 34 0a 2a 2a 0a 2a 2a  Seq * * P4.**.**
16985 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72   P4 is a pointer
16986 20 74 6f 20 61 20 43 6f 6c 6c 53 65 71 20 73 74   to a CollSeq st
16987 72 75 63 74 2e 20 49 66 20 74 68 65 20 6e 65 78  ruct. If the nex
16988 74 20 63 61 6c 6c 20 74 6f 20 61 20 75 73 65 72  t call to a user
16989 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6f 72 20   function.** or 
1698a 61 67 67 72 65 67 61 74 65 20 63 61 6c 6c 73 20  aggregate calls 
1698b 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f  sqlite3GetFuncCo
1698c 6c 6c 53 65 71 28 29 2c 20 74 68 69 73 20 63 6f  llSeq(), this co
1698d 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
1698e 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 72 65 74 75   will.** be retu
1698f 72 6e 65 64 2e 20 54 68 69 73 20 69 73 20 75 73  rned. This is us
16990 65 64 20 62 79 20 74 68 65 20 62 75 69 6c 74 2d  ed by the built-
16991 69 6e 20 6d 69 6e 28 29 2c 20 6d 61 78 28 29 20  in min(), max() 
16992 61 6e 64 20 6e 75 6c 6c 69 66 28 29 0a 2a 2a 20  and nullif().** 
16993 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  functions..**.**
16994 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 75   The interface u
16995 73 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c 65  sed by the imple
16996 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
16997 20 61 66 6f 72 65 6d 65 6e 74 69 6f 6e 65 64 20   aforementioned 
16998 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20  functions.** to 
16999 72 65 74 72 69 65 76 65 20 74 68 65 20 63 6f 6c  retrieve the col
1699a 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
1699b 73 65 74 20 62 79 20 74 68 69 73 20 6f 70 63 6f  set by this opco
1699c 64 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61  de is not availa
1699d 62 6c 65 0a 2a 2a 20 70 75 62 6c 69 63 6c 79 2c  ble.** publicly,
1699e 20 6f 6e 6c 79 20 74 6f 20 75 73 65 72 20 66 75   only to user fu
1699f 6e 63 74 69 6f 6e 73 20 64 65 66 69 6e 65 64 20  nctions defined 
169a0 69 6e 20 66 75 6e 63 2e 63 2e 0a 2a 2f 0a 63 61  in func.c..*/.ca
169a1 73 65 20 4f 50 5f 43 6f 6c 6c 53 65 71 3a 20 7b  se OP_CollSeq: {
169a2 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
169a3 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53  p4type==P4_COLLS
169a4 45 51 20 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  EQ );.  break;.}
169a5 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 75 6e  ../* Opcode: Fun
169a6 63 74 69 6f 6e 20 50 31 20 50 32 20 50 33 20 50  ction P1 P2 P3 P
169a7 34 20 50 35 0a 2a 2a 0a 2a 2a 20 49 6e 76 6f 6b  4 P5.**.** Invok
169a8 65 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f  e a user functio
169a9 6e 20 28 50 34 20 69 73 20 61 20 70 6f 69 6e 74  n (P4 is a point
169aa 65 72 20 74 6f 20 61 20 46 75 6e 63 74 69 6f 6e  er to a Function
169ab 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 0a   structure that.
169ac 2a 2a 20 64 65 66 69 6e 65 73 20 74 68 65 20 66  ** defines the f
169ad 75 6e 63 74 69 6f 6e 29 20 77 69 74 68 20 50 35  unction) with P5
169ae 20 61 72 67 75 6d 65 6e 74 73 20 74 61 6b 65 6e   arguments taken
169af 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50   from register P
169b0 32 20 61 6e 64 0a 2a 2a 20 73 75 63 63 65 73 73  2 and.** success
169b1 6f 72 73 2e 20 20 54 68 65 20 72 65 73 75 6c 74  ors.  The result
169b2 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
169b3 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65   is stored in re
169b4 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 52 65  gister P3..** Re
169b5 67 69 73 74 65 72 20 50 33 20 6d 75 73 74 20 6e  gister P3 must n
169b6 6f 74 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65  ot be one of the
169b7 20 66 75 6e 63 74 69 6f 6e 20 69 6e 70 75 74 73   function inputs
169b8 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20  ..**.** P1 is a 
169b9 33 32 2d 62 69 74 20 62 69 74 6d 61 73 6b 20 69  32-bit bitmask i
169ba 6e 64 69 63 61 74 69 6e 67 20 77 68 65 74 68 65  ndicating whethe
169bb 72 20 6f 72 20 6e 6f 74 20 65 61 63 68 20 61 72  r or not each ar
169bc 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 0a 2a  gument to the .*
169bd 2a 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 64  * function was d
169be 65 74 65 72 6d 69 6e 65 64 20 74 6f 20 62 65 20  etermined to be 
169bf 63 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f 6d 70  constant at comp
169c0 69 6c 65 20 74 69 6d 65 2e 20 49 66 20 74 68 65  ile time. If the
169c1 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65   first.** argume
169c2 6e 74 20 77 61 73 20 63 6f 6e 73 74 61 6e 74 20  nt was constant 
169c3 74 68 65 6e 20 62 69 74 20 30 20 6f 66 20 50 31  then bit 0 of P1
169c4 20 69 73 20 73 65 74 2e 20 54 68 69 73 20 69 73   is set. This is
169c5 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69   used to determi
169c6 6e 65 0a 2a 2a 20 77 68 65 74 68 65 72 20 6d 65  ne.** whether me
169c7 74 61 20 64 61 74 61 20 61 73 73 6f 63 69 61 74  ta data associat
169c8 65 64 20 77 69 74 68 20 61 20 75 73 65 72 20 66  ed with a user f
169c9 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74  unction argument
169ca 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 73 71   using the.** sq
169cb 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74  lite3_set_auxdat
169cc 61 28 29 20 41 50 49 20 6d 61 79 20 62 65 20 73  a() API may be s
169cd 61 66 65 6c 79 20 72 65 74 61 69 6e 65 64 20 75  afely retained u
169ce 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 0a 2a 2a  ntil the next.**
169cf 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74   invocation of t
169d0 68 69 73 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a  his opcode..**.*
169d1 2a 20 53 65 65 20 61 6c 73 6f 3a 20 41 67 67 53  * See also: AggS
169d2 74 65 70 20 61 6e 64 20 41 67 67 46 69 6e 61 6c  tep and AggFinal
169d3 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 75 6e 63  .*/.case OP_Func
169d4 74 69 6f 6e 3a 20 7b 0a 20 20 69 6e 74 20 69 3b  tion: {.  int i;
169d5 0a 20 20 4d 65 6d 20 2a 70 41 72 67 3b 0a 20 20  .  Mem *pArg;.  
169d6 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
169d7 63 74 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76  ctx;.  sqlite3_v
169d8 61 6c 75 65 20 2a 2a 61 70 56 61 6c 3b 0a 20 20  alue **apVal;.  
169d9 69 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e 70 35 3b  int n = pOp->p5;
169da 0a 0a 20 20 61 70 56 61 6c 20 3d 20 70 2d 3e 61  ..  apVal = p->a
169db 70 41 72 67 3b 0a 20 20 61 73 73 65 72 74 28 20  pArg;.  assert( 
169dc 61 70 56 61 6c 20 7c 7c 20 6e 3d 3d 30 20 29 3b  apVal || n==0 );
169dd 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 30  ..  assert( n==0
169de 20 7c 7c 20 28 70 4f 70 2d 3e 70 32 3e 30 20 26   || (pOp->p2>0 &
169df 26 20 70 4f 70 2d 3e 70 32 2b 6e 3c 3d 70 2d 3e  & pOp->p2+n<=p->
169e0 6e 4d 65 6d 29 20 29 3b 0a 20 20 61 73 73 65 72  nMem) );.  asser
169e1 74 28 20 70 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e  t( pOp->p3<pOp->
169e2 70 32 20 7c 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70  p2 || pOp->p3>=p
169e3 4f 70 2d 3e 70 32 2b 6e 20 29 3b 0a 20 20 70 41  Op->p2+n );.  pA
169e4 72 67 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  rg = &p->aMem[pO
169e5 70 2d 3e 70 32 5d 3b 0a 20 20 66 6f 72 28 69 3d  p->p2];.  for(i=
169e6 30 3b 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 41 72  0; i<n; i++, pAr
169e7 67 2b 2b 29 7b 0a 20 20 20 20 61 70 56 61 6c 5b  g++){.    apVal[
169e8 69 5d 20 3d 20 70 41 72 67 3b 0a 20 20 20 20 73  i] = pArg;.    s
169e9 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 70 41 72  toreTypeInfo(pAr
169ea 67 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20  g, encoding);.  
169eb 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
169ec 28 70 4f 70 2d 3e 70 32 2c 20 70 41 72 67 29 3b  (pOp->p2, pArg);
169ed 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
169ee 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
169ef 46 55 4e 43 44 45 46 20 7c 7c 20 70 4f 70 2d 3e  FUNCDEF || pOp->
169f0 70 34 74 79 70 65 3d 3d 50 34 5f 56 44 42 45 46  p4type==P4_VDBEF
169f1 55 4e 43 20 29 3b 0a 20 20 69 66 28 20 70 4f 70  UNC );.  if( pOp
169f2 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 46 55 4e  ->p4type==P4_FUN
169f3 43 44 45 46 20 29 7b 0a 20 20 20 20 63 74 78 2e  CDEF ){.    ctx.
169f4 70 46 75 6e 63 20 3d 20 70 4f 70 2d 3e 70 34 2e  pFunc = pOp->p4.
169f5 70 46 75 6e 63 3b 0a 20 20 20 20 63 74 78 2e 70  pFunc;.    ctx.p
169f6 56 64 62 65 46 75 6e 63 20 3d 20 30 3b 0a 20 20  VdbeFunc = 0;.  
169f7 7d 65 6c 73 65 7b 0a 20 20 20 20 63 74 78 2e 70  }else{.    ctx.p
169f8 56 64 62 65 46 75 6e 63 20 3d 20 28 56 64 62 65  VdbeFunc = (Vdbe
169f9 46 75 6e 63 2a 29 70 4f 70 2d 3e 70 34 2e 70 56  Func*)pOp->p4.pV
169fa 64 62 65 46 75 6e 63 3b 0a 20 20 20 20 63 74 78  dbeFunc;.    ctx
169fb 2e 70 46 75 6e 63 20 3d 20 63 74 78 2e 70 56 64  .pFunc = ctx.pVd
169fc 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 3b 0a 20  beFunc->pFunc;. 
169fd 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f   }..  assert( pO
169fe 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e  p->p3>0 && pOp->
169ff 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20  p3<=p->nMem );. 
16a00 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d   pOut = &p->aMem
16a01 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 63 74 78  [pOp->p3];.  ctx
16a02 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  .s.flags = MEM_N
16a03 75 6c 6c 3b 0a 20 20 63 74 78 2e 73 2e 64 62 20  ull;.  ctx.s.db 
16a04 3d 20 64 62 3b 0a 20 20 63 74 78 2e 73 2e 78 44  = db;.  ctx.s.xD
16a05 65 6c 20 3d 20 30 3b 0a 20 20 63 74 78 2e 73 2e  el = 0;.  ctx.s.
16a06 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 0a 20 20  zMalloc = 0;..  
16a07 2f 2a 20 54 68 65 20 6f 75 74 70 75 74 20 63 65  /* The output ce
16a08 6c 6c 20 6d 61 79 20 61 6c 72 65 61 64 79 20 68  ll may already h
16a09 61 76 65 20 61 20 62 75 66 66 65 72 20 61 6c 6c  ave a buffer all
16a0a 6f 63 61 74 65 64 2e 20 4d 6f 76 65 0a 20 20 2a  ocated. Move.  *
16a0b 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f  * the pointer to
16a0c 20 63 74 78 2e 73 20 73 6f 20 69 6e 20 63 61 73   ctx.s so in cas
16a0d 65 20 74 68 65 20 75 73 65 72 2d 66 75 6e 63 74  e the user-funct
16a0e 69 6f 6e 20 63 61 6e 20 75 73 65 0a 20 20 2a 2a  ion can use.  **
16a0f 20 74 68 65 20 61 6c 72 65 61 64 79 20 61 6c 6c   the already all
16a10 6f 63 61 74 65 64 20 62 75 66 66 65 72 20 69 6e  ocated buffer in
16a11 73 74 65 61 64 20 6f 66 20 61 6c 6c 6f 63 61 74  stead of allocat
16a12 69 6e 67 20 61 20 6e 65 77 20 6f 6e 65 2e 0a 20  ing a new one.. 
16a13 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
16a14 65 4d 65 6d 4d 6f 76 65 28 26 63 74 78 2e 73 2c  eMemMove(&ctx.s,
16a15 20 70 4f 75 74 29 3b 0a 20 20 4d 65 6d 53 65 74   pOut);.  MemSet
16a16 54 79 70 65 46 6c 61 67 28 26 63 74 78 2e 73 2c  TypeFlag(&ctx.s,
16a17 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 63   MEM_Null);..  c
16a18 74 78 2e 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a  tx.isError = 0;.
16a19 20 20 69 66 28 20 63 74 78 2e 70 46 75 6e 63 2d    if( ctx.pFunc-
16a1a 3e 6e 65 65 64 43 6f 6c 6c 53 65 71 20 29 7b 0a  >needCollSeq ){.
16a1b 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 3e      assert( pOp>
16a1c 70 2d 3e 61 4f 70 20 29 3b 0a 20 20 20 20 61 73  p->aOp );.    as
16a1d 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 70 34  sert( pOp[-1].p4
16a1e 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51  type==P4_COLLSEQ
16a1f 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
16a20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d  pOp[-1].opcode==
16a21 4f 50 5f 43 6f 6c 6c 53 65 71 20 29 3b 0a 20 20  OP_CollSeq );.  
16a22 20 20 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 70 4f    ctx.pColl = pO
16a23 70 5b 2d 31 5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a  p[-1].p4.pColl;.
16a24 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65    }.  if( sqlite
16a25 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29  3SafetyOff(db) )
16a26 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
16a27 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 28 2a 63  to_misuse;.  (*c
16a28 74 78 2e 70 46 75 6e 63 2d 3e 78 46 75 6e 63 29  tx.pFunc->xFunc)
16a29 28 26 63 74 78 2c 20 6e 2c 20 61 70 56 61 6c 29  (&ctx, n, apVal)
16a2a 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ;.  if( sqlite3S
16a2b 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20  afetyOn(db) ){. 
16a2c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
16a2d 6d 52 65 6c 65 61 73 65 28 26 63 74 78 2e 73 29  mRelease(&ctx.s)
16a2e 3b 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74  ;.    goto abort
16a2f 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a  _due_to_misuse;.
16a30 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61    }.  if( db->ma
16a31 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
16a32 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68    /* Even though
16a33 20 61 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20   a malloc() has 
16a34 66 61 69 6c 65 64 2c 20 74 68 65 20 69 6d 70 6c  failed, the impl
16a35 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
16a36 65 0a 20 20 20 20 2a 2a 20 75 73 65 72 20 66 75  e.    ** user fu
16a37 6e 63 74 69 6f 6e 20 6d 61 79 20 68 61 76 65 20  nction may have 
16a38 63 61 6c 6c 65 64 20 61 6e 20 73 71 6c 69 74 65  called an sqlite
16a39 33 5f 72 65 73 75 6c 74 5f 58 58 58 28 29 20 66  3_result_XXX() f
16a3a 75 6e 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 74  unction.    ** t
16a3b 6f 20 72 65 74 75 72 6e 20 61 20 76 61 6c 75 65  o return a value
16a3c 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  . The following 
16a3d 63 61 6c 6c 20 72 65 6c 65 61 73 65 73 20 61 6e  call releases an
16a3e 79 20 72 65 73 6f 75 72 63 65 73 0a 20 20 20 20  y resources.    
16a3f 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ** associated wi
16a40 74 68 20 73 75 63 68 20 61 20 76 61 6c 75 65 2e  th such a value.
16a41 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e  .    **.    ** N
16a42 6f 74 65 3a 20 4d 61 79 62 65 20 4d 65 6d 52 65  ote: Maybe MemRe
16a43 6c 65 61 73 65 28 29 20 73 68 6f 75 6c 64 20 62  lease() should b
16a44 65 20 63 61 6c 6c 65 64 20 69 66 20 73 71 6c 69  e called if sqli
16a45 74 65 33 53 61 66 65 74 79 4f 6e 28 29 0a 20 20  te3SafetyOn().  
16a46 20 20 2a 2a 20 66 61 69 6c 73 20 61 6c 73 6f 20    ** fails also 
16a47 28 74 68 65 20 69 66 28 2e 2e 2e 29 20 73 74 61  (the if(...) sta
16a48 74 65 6d 65 6e 74 20 61 62 6f 76 65 29 2e 20 42  tement above). B
16a49 75 74 20 69 66 20 70 65 6f 70 6c 65 20 61 72 65  ut if people are
16a4a 0a 20 20 20 20 2a 2a 20 6d 69 73 75 73 69 6e 67  .    ** misusing
16a4b 20 73 71 6c 69 74 65 2c 20 74 68 65 79 20 68 61   sqlite, they ha
16a4c 76 65 20 62 69 67 67 65 72 20 70 72 6f 62 6c 65  ve bigger proble
16a4d 6d 73 20 74 68 61 6e 20 61 20 6c 65 61 6b 65 64  ms than a leaked
16a4e 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a 20   value..    */. 
16a4f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
16a50 6d 52 65 6c 65 61 73 65 28 26 63 74 78 2e 73 29  mRelease(&ctx.s)
16a51 3b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65  ;.    goto no_me
16a52 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  m;.  }..  /* If 
16a53 61 6e 79 20 61 75 78 69 6c 61 72 79 20 64 61 74  any auxilary dat
16a54 61 20 66 75 6e 63 74 69 6f 6e 73 20 68 61 76 65  a functions have
16a55 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 62 79 20   been called by 
16a56 74 68 69 73 20 75 73 65 72 20 66 75 6e 63 74 69  this user functi
16a57 6f 6e 2c 0a 20 20 2a 2a 20 69 6d 6d 65 64 69 61  on,.  ** immedia
16a58 74 65 6c 79 20 63 61 6c 6c 20 74 68 65 20 64 65  tely call the de
16a59 73 74 72 75 63 74 6f 72 20 66 6f 72 20 61 6e 79  structor for any
16a5a 20 6e 6f 6e 2d 73 74 61 74 69 63 20 76 61 6c 75   non-static valu
16a5b 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63  es..  */.  if( c
16a5c 74 78 2e 70 56 64 62 65 46 75 6e 63 20 29 7b 0a  tx.pVdbeFunc ){.
16a5d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 44      sqlite3VdbeD
16a5e 65 6c 65 74 65 41 75 78 44 61 74 61 28 63 74 78  eleteAuxData(ctx
16a5f 2e 70 56 64 62 65 46 75 6e 63 2c 20 70 4f 70 2d  .pVdbeFunc, pOp-
16a60 3e 70 31 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70  >p1);.    pOp->p
16a61 34 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 63 74  4.pVdbeFunc = ct
16a62 78 2e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 20  x.pVdbeFunc;.   
16a63 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50   pOp->p4type = P
16a64 34 5f 56 44 42 45 46 55 4e 43 3b 0a 20 20 7d 0a  4_VDBEFUNC;.  }.
16a65 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66 75 6e  .  /* If the fun
16a66 63 74 69 6f 6e 20 72 65 74 75 72 6e 65 64 20 61  ction returned a
16a67 6e 20 65 72 72 6f 72 2c 20 74 68 72 6f 77 20 61  n error, throw a
16a68 6e 20 65 78 63 65 70 74 69 6f 6e 20 2a 2f 0a 20  n exception */. 
16a69 20 69 66 28 20 63 74 78 2e 69 73 45 72 72 6f 72   if( ctx.isError
16a6a 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
16a6b 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72  etString(&p->zEr
16a6c 72 4d 73 67 2c 20 73 71 6c 69 74 65 33 5f 76 61  rMsg, sqlite3_va
16a6d 6c 75 65 5f 74 65 78 74 28 26 63 74 78 2e 73 29  lue_text(&ctx.s)
16a6e 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20 20  , (char*)0);.   
16a6f 20 72 63 20 3d 20 63 74 78 2e 69 73 45 72 72 6f   rc = ctx.isErro
16a70 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70  r;.  }..  /* Cop
16a71 79 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  y the result of 
16a72 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e 74  the function int
16a73 6f 20 72 65 67 69 73 74 65 72 20 50 33 20 2a 2f  o register P3 */
16a74 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
16a75 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 63 74  angeEncoding(&ct
16a76 78 2e 73 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a  x.s, encoding);.
16a77 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
16a78 4d 6f 76 65 28 70 4f 75 74 2c 20 26 63 74 78 2e  Move(pOut, &ctx.
16a79 73 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  s);.  if( sqlite
16a7a 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70  3VdbeMemTooBig(p
16a7b 4f 75 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  Out) ){.    goto
16a7c 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20   too_big;.  }.  
16a7d 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
16a7e 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20  Op->p3, pOut);. 
16a7f 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42   UPDATE_MAX_BLOB
16a80 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72  SIZE(pOut);.  br
16a81 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
16a82 65 3a 20 42 69 74 41 6e 64 20 50 31 20 50 32 20  e: BitAnd P1 P2 
16a83 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b  P3 * *.**.** Tak
16a84 65 20 74 68 65 20 62 69 74 2d 77 69 73 65 20 41  e the bit-wise A
16a85 4e 44 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73  ND of the values
16a86 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
16a87 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74  and P2 and.** st
16a88 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69  ore the result i
16a89 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  n register P3..*
16a8a 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75  * If either inpu
16a8b 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72  t is NULL, the r
16a8c 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a  esult is NULL..*
16a8d 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74  /./* Opcode: Bit
16a8e 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  Or P1 P2 P3 * *.
16a8f 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 62  **.** Take the b
16a90 69 74 2d 77 69 73 65 20 4f 52 20 6f 66 20 74 68  it-wise OR of th
16a91 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69  e values in regi
16a92 73 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61  ster P1 and P2 a
16a93 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20  nd.** store the 
16a94 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74  result in regist
16a95 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74  er P3..** If eit
16a96 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c  her input is NUL
16a97 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  L, the result is
16a98 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63   NULL..*/./* Opc
16a99 6f 64 65 3a 20 53 68 69 66 74 4c 65 66 74 20 50  ode: ShiftLeft P
16a9a 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
16a9b 2a 20 53 68 69 66 74 20 74 68 65 20 69 6e 74 65  * Shift the inte
16a9c 67 65 72 20 76 61 6c 75 65 20 69 6e 20 72 65 67  ger value in reg
16a9d 69 73 74 65 72 20 50 32 20 74 6f 20 74 68 65 20  ister P2 to the 
16a9e 6c 65 66 74 20 62 79 20 74 68 65 0a 2a 2a 20 6e  left by the.** n
16a9f 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 73 70  umber of bits sp
16aa0 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 69  ecified by the i
16aa1 6e 74 65 67 65 72 20 69 6e 20 72 65 67 69 73 65  nteger in regise
16aa2 72 20 50 31 2e 0a 2a 2a 20 53 74 6f 72 65 20 74  r P1..** Store t
16aa3 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67  he result in reg
16aa4 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20  ister P3..** If 
16aa5 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20  either input is 
16aa6 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74  NULL, the result
16aa7 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20   is NULL..*/./* 
16aa8 4f 70 63 6f 64 65 3a 20 53 68 69 66 74 52 69 67  Opcode: ShiftRig
16aa9 68 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  ht P1 P2 P3 * *.
16aaa 2a 2a 0a 2a 2a 20 53 68 69 66 74 20 74 68 65 20  **.** Shift the 
16aab 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 6e  integer value in
16aac 20 72 65 67 69 73 74 65 72 20 50 32 20 74 6f 20   register P2 to 
16aad 74 68 65 20 72 69 67 68 74 20 62 79 20 74 68 65  the right by the
16aae 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 69  .** number of bi
16aaf 74 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20  ts specified by 
16ab0 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e 20 72  the integer in r
16ab1 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 20 53  egister P1..** S
16ab2 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20  tore the result 
16ab3 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a  in register P3..
16ab4 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70  ** If either inp
16ab5 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20  ut is NULL, the 
16ab6 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a  result is NULL..
16ab7 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 69 74 41 6e  */.case OP_BitAn
16ab8 64 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d:              
16ab9 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
16aba 5f 42 49 54 41 4e 44 2c 20 69 6e 31 2c 20 69 6e  _BITAND, in1, in
16abb 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20  2, out3 */.case 
16abc 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 20 20  OP_BitOr:       
16abd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
16abe 6d 65 20 61 73 20 54 4b 5f 42 49 54 4f 52 2c 20  me as TK_BITOR, 
16abf 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a  in1, in2, out3 *
16ac0 2f 0a 63 61 73 65 20 4f 50 5f 53 68 69 66 74 4c  /.case OP_ShiftL
16ac1 65 66 74 3a 20 20 20 20 20 20 20 20 20 20 20 20  eft:            
16ac2 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
16ac3 4c 53 48 49 46 54 2c 20 69 6e 31 2c 20 69 6e 32  LSHIFT, in1, in2
16ac4 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f  , out3 */.case O
16ac5 50 5f 53 68 69 66 74 52 69 67 68 74 3a 20 7b 20  P_ShiftRight: { 
16ac6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
16ac7 65 20 61 73 20 54 4b 5f 52 53 48 49 46 54 2c 20  e as TK_RSHIFT, 
16ac8 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a  in1, in2, out3 *
16ac9 2f 0a 20 20 69 36 34 20 61 2c 20 62 3b 0a 0a 20  /.  i64 a, b;.. 
16aca 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67   if( (pIn1->flag
16acb 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 73 29  s | pIn2->flags)
16acc 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20   & MEM_Null ){. 
16acd 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
16ace 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a  mSetNull(pOut);.
16acf 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20      break;.  }. 
16ad0 20 61 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65   a = sqlite3Vdbe
16ad1 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 3b 0a  IntValue(pIn2);.
16ad2 20 20 62 20 3d 20 73 71 6c 69 74 65 33 56 64 62    b = sqlite3Vdb
16ad3 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 3b  eIntValue(pIn1);
16ad4 0a 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e  .  switch( pOp->
16ad5 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 63 61  opcode ){.    ca
16ad6 73 65 20 4f 50 5f 42 69 74 41 6e 64 3a 20 20 20  se OP_BitAnd:   
16ad7 20 20 20 61 20 26 3d 20 62 3b 20 20 20 20 20 62     a &= b;     b
16ad8 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f  reak;.    case O
16ad9 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 20 20 61  P_BitOr:       a
16ada 20 7c 3d 20 62 3b 20 20 20 20 20 62 72 65 61 6b   |= b;     break
16adb 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 53 68  ;.    case OP_Sh
16adc 69 66 74 4c 65 66 74 3a 20 20 20 61 20 3c 3c 3d  iftLeft:   a <<=
16add 20 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20   b;    break;.  
16ade 20 20 64 65 66 61 75 6c 74 3a 20 20 61 73 73 65    default:  asse
16adf 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  rt( pOp->opcode=
16ae0 3d 4f 50 5f 53 68 69 66 74 52 69 67 68 74 20 29  =OP_ShiftRight )
16ae1 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
16ae2 20 20 20 20 20 20 20 20 20 20 20 61 20 3e 3e 3d             a >>=
16ae3 20 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20   b;    break;.  
16ae4 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20  }.  pOut->u.i = 
16ae5 61 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46  a;.  MemSetTypeF
16ae6 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e  lag(pOut, MEM_In
16ae7 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  t);.  break;.}..
16ae8 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 64 64 49 6d  /* Opcode: AddIm
16ae9 6d 20 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  m  P1 P2 * * *.*
16aea 2a 20 0a 2a 2a 20 41 64 64 20 74 68 65 20 63 6f  * .** Add the co
16aeb 6e 73 74 61 6e 74 20 50 32 20 74 68 65 20 76 61  nstant P2 the va
16aec 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
16aed 50 31 2e 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c  P1..** The resul
16aee 74 20 69 73 20 61 6c 77 61 79 73 20 61 6e 20 69  t is always an i
16aef 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 6f  nteger..**.** To
16af0 20 66 6f 72 63 65 20 61 6e 79 20 72 65 67 69 73   force any regis
16af1 74 65 72 20 74 6f 20 62 65 20 61 6e 20 69 6e 74  ter to be an int
16af2 65 67 65 72 2c 20 6a 75 73 74 20 61 64 64 20 30  eger, just add 0
16af3 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 64 64  ..*/.case OP_Add
16af4 49 6d 6d 3a 20 7b 20 20 20 20 20 20 20 20 20 20  Imm: {          
16af5 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 73 71    /* in1 */.  sq
16af6 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65  lite3VdbeMemInte
16af7 67 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20  gerify(pIn1);.  
16af8 70 49 6e 31 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70  pIn1->u.i += pOp
16af9 2d 3e 70 32 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  ->p2;.  break;.}
16afa 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 6f 72  ../* Opcode: For
16afb 63 65 49 6e 74 20 50 31 20 50 32 20 50 33 20 2a  ceInt P1 P2 P3 *
16afc 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74   *.**.** Convert
16afd 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
16afe 65 72 20 50 31 20 69 6e 74 6f 20 61 6e 20 69 6e  er P1 into an in
16aff 74 65 67 65 72 2e 20 20 49 66 20 74 68 65 20 76  teger.  If the v
16b00 61 6c 75 65 20 0a 2a 2a 20 69 6e 20 50 31 20 69  alue .** in P1 i
16b01 73 20 6e 6f 74 20 6e 75 6d 65 72 69 63 20 28 6d  s not numeric (m
16b02 65 61 6e 69 6e 67 20 74 68 61 74 20 69 73 20 69  eaning that is i
16b03 73 20 61 20 4e 55 4c 4c 20 6f 72 20 61 20 73 74  s a NULL or a st
16b04 72 69 6e 67 20 74 68 61 74 0a 2a 2a 20 64 6f 65  ring that.** doe
16b05 73 20 6e 6f 74 20 6c 6f 6f 6b 20 6c 69 6b 65 20  s not look like 
16b06 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 66 6c  an integer or fl
16b07 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d  oating point num
16b08 62 65 72 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d  ber) then.** jum
16b09 70 20 74 6f 20 50 32 2e 20 20 49 66 20 74 68 65  p to P2.  If the
16b0a 20 76 61 6c 75 65 20 69 6e 20 50 31 20 69 73 20   value in P1 is 
16b0b 6e 75 6d 65 72 69 63 20 74 68 65 6e 0a 2a 2a 20  numeric then.** 
16b0c 63 6f 6e 76 65 72 74 20 69 74 20 69 6e 74 6f 20  convert it into 
16b0d 74 68 65 20 6c 65 61 73 74 20 69 6e 74 65 67 65  the least intege
16b0e 72 20 74 68 61 74 20 69 73 20 67 72 65 61 74 65  r that is greate
16b0f 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  r than or equal 
16b10 74 6f 20 69 74 73 0a 2a 2a 20 63 75 72 72 65 6e  to its.** curren
16b11 74 20 76 61 6c 75 65 20 69 66 20 50 33 3d 3d 30  t value if P3==0
16b12 2c 20 6f 72 20 74 6f 20 74 68 65 20 6c 65 61 73  , or to the leas
16b13 74 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 69  t integer that i
16b14 73 20 73 74 72 69 63 74 6c 79 0a 2a 2a 20 67 72  s strictly.** gr
16b15 65 61 74 65 72 20 74 68 61 6e 20 69 74 73 20 63  eater than its c
16b16 75 72 72 65 6e 74 20 76 61 6c 75 65 20 69 66 20  urrent value if 
16b17 50 33 3d 3d 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f  P3==1..*/.case O
16b18 50 5f 46 6f 72 63 65 49 6e 74 3a 20 7b 20 20 20  P_ForceInt: {   
16b19 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70           /* jump
16b1a 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 36 34 20 76  , in1 */.  i64 v
16b1b 3b 0a 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74  ;.  applyAffinit
16b1c 79 28 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41  y(pIn1, SQLITE_A
16b1d 46 46 5f 4e 55 4d 45 52 49 43 2c 20 65 6e 63 6f  FF_NUMERIC, enco
16b1e 64 69 6e 67 29 3b 0a 20 20 69 66 28 20 28 70 49  ding);.  if( (pI
16b1f 6e 31 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d  n1->flags & (MEM
16b20 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 3d  _Int|MEM_Real))=
16b21 3d 30 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70  =0 ){.    pc = p
16b22 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20  Op->p2 - 1;.    
16b23 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28  break;.  }.  if(
16b24 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d   pIn1->flags & M
16b25 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 76 20  EM_Int ){.    v 
16b26 3d 20 70 49 6e 31 2d 3e 75 2e 69 20 2b 20 28 70  = pIn1->u.i + (p
16b27 4f 70 2d 3e 70 33 21 3d 30 29 3b 0a 20 20 7d 65  Op->p3!=0);.  }e
16b28 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
16b29 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d   pIn1->flags & M
16b2a 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 20 20 76  EM_Real );.    v
16b2b 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36   = (sqlite3_int6
16b2c 34 29 70 49 6e 31 2d 3e 72 3b 0a 20 20 20 20 69  4)pIn1->r;.    i
16b2d 66 28 20 70 49 6e 31 2d 3e 72 3e 28 64 6f 75 62  f( pIn1->r>(doub
16b2e 6c 65 29 76 20 29 20 76 2b 2b 3b 0a 20 20 20 20  le)v ) v++;.    
16b2f 69 66 28 20 70 4f 70 2d 3e 70 33 20 26 26 20 70  if( pOp->p3 && p
16b30 49 6e 31 2d 3e 72 3d 3d 28 64 6f 75 62 6c 65 29  In1->r==(double)
16b31 76 20 29 20 76 2b 2b 3b 0a 20 20 7d 0a 20 20 70  v ) v++;.  }.  p
16b32 49 6e 31 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20  In1->u.i = v;.  
16b33 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
16b34 49 6e 31 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20  In1, MEM_Int);. 
16b35 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
16b36 63 6f 64 65 3a 20 4d 75 73 74 42 65 49 6e 74 20  code: MustBeInt 
16b37 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 20 0a  P1 P2 * * *.** .
16b38 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c  ** Force the val
16b39 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
16b3a 31 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67  1 to be an integ
16b3b 65 72 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75  er.  If the valu
16b3c 65 0a 2a 2a 20 69 6e 20 50 31 20 69 73 20 6e 6f  e.** in P1 is no
16b3d 74 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64  t an integer and
16b3e 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65   cannot be conve
16b3f 72 74 65 64 20 69 6e 74 6f 20 61 6e 20 69 6e 74  rted into an int
16b40 65 67 65 72 0a 2a 2a 20 77 69 74 68 6f 75 74 20  eger.** without 
16b41 64 61 74 61 20 6c 6f 73 73 2c 20 74 68 65 6e 20  data loss, then 
16b42 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79  jump immediately
16b43 20 74 6f 20 50 32 2c 20 6f 72 20 69 66 20 50 32   to P2, or if P2
16b44 3d 3d 30 0a 2a 2a 20 72 61 69 73 65 20 61 6e 20  ==0.** raise an 
16b45 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 20  SQLITE_MISMATCH 
16b46 65 78 63 65 70 74 69 6f 6e 2e 0a 2a 2f 0a 63 61  exception..*/.ca
16b47 73 65 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 3a  se OP_MustBeInt:
16b48 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a   {            /*
16b49 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20   jump, in1 */.  
16b4a 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 49  applyAffinity(pI
16b4b 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  n1, SQLITE_AFF_N
16b4c 55 4d 45 52 49 43 2c 20 65 6e 63 6f 64 69 6e 67  UMERIC, encoding
16b4d 29 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e  );.  if( (pIn1->
16b4e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29  flags & MEM_Int)
16b4f 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ==0 ){.    if( p
16b50 4f 70 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20 20 20  Op->p2==0 ){.   
16b51 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d     rc = SQLITE_M
16b52 49 53 4d 41 54 43 48 3b 0a 20 20 20 20 20 20 67  ISMATCH;.      g
16b53 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
16b54 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73  _error;.    }els
16b55 65 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f  e{.      pc = pO
16b56 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d  p->p2 - 1;.    }
16b57 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 65  .  }else{.    Me
16b58 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 49 6e  mSetTypeFlag(pIn
16b59 31 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d  1, MEM_Int);.  }
16b5a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
16b5b 4f 70 63 6f 64 65 3a 20 52 65 61 6c 41 66 66 69  Opcode: RealAffi
16b5c 6e 69 74 79 20 50 31 20 2a 20 2a 20 2a 20 2a 0a  nity P1 * * * *.
16b5d 2a 2a 0a 2a 2a 20 49 66 20 72 65 67 69 73 74 65  **.** If registe
16b5e 72 20 50 31 20 68 6f 6c 64 73 20 61 6e 20 69 6e  r P1 holds an in
16b5f 74 65 67 65 72 20 63 6f 6e 76 65 72 74 20 69 74  teger convert it
16b60 20 74 6f 20 61 20 72 65 61 6c 20 76 61 6c 75 65   to a real value
16b61 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63  ..**.** This opc
16b62 6f 64 65 20 69 73 20 75 73 65 64 20 77 68 65 6e  ode is used when
16b63 20 65 78 74 72 61 63 74 69 6e 67 20 69 6e 66 6f   extracting info
16b64 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 63  rmation from a c
16b65 6f 6c 75 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 61  olumn that.** ha
16b66 73 20 52 45 41 4c 20 61 66 66 69 6e 69 74 79 2e  s REAL affinity.
16b67 20 20 53 75 63 68 20 63 6f 6c 75 6d 6e 20 76 61    Such column va
16b68 6c 75 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62  lues may still b
16b69 65 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 69  e stored as.** i
16b6a 6e 74 65 67 65 72 73 2c 20 66 6f 72 20 73 70 61  ntegers, for spa
16b6b 63 65 20 65 66 66 69 63 69 65 6e 63 79 2c 20 62  ce efficiency, b
16b6c 75 74 20 61 66 74 65 72 20 65 78 74 72 61 63 74  ut after extract
16b6d 69 6f 6e 20 77 65 20 77 61 6e 74 20 74 68 65 6d  ion we want them
16b6e 0a 2a 2a 20 74 6f 20 68 61 76 65 20 6f 6e 6c 79  .** to have only
16b6f 20 61 20 72 65 61 6c 20 76 61 6c 75 65 2e 0a 2a   a real value..*
16b70 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 6c 41 66  /.case OP_RealAf
16b71 66 69 6e 69 74 79 3a 20 7b 20 20 20 20 20 20 20  finity: {       
16b72 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e             /* in
16b73 31 20 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d  1 */.  if( pIn1-
16b74 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74  >flags & MEM_Int
16b75 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
16b76 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 49  dbeMemRealify(pI
16b77 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  n1);.  }.  break
16b78 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
16b79 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 2f 2a  ITE_OMIT_CAST./*
16b7a 20 4f 70 63 6f 64 65 3a 20 54 6f 54 65 78 74 20   Opcode: ToText 
16b7b 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  P1 * * * *.**.**
16b7c 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65   Force the value
16b7d 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
16b7e 74 6f 20 62 65 20 74 65 78 74 2e 0a 2a 2a 20 49  to be text..** I
16b7f 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 6e  f the value is n
16b80 75 6d 65 72 69 63 2c 20 63 6f 6e 76 65 72 74 20  umeric, convert 
16b81 69 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 75  it to a string u
16b82 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75 69  sing the.** equi
16b83 76 61 6c 65 6e 74 20 6f 66 20 70 72 69 6e 74 66  valent of printf
16b84 28 29 2e 20 20 42 6c 6f 62 20 76 61 6c 75 65 73  ().  Blob values
16b85 20 61 72 65 20 75 6e 63 68 61 6e 67 65 64 20 61   are unchanged a
16b86 6e 64 0a 2a 2a 20 61 72 65 20 61 66 74 65 72 77  nd.** are afterw
16b87 61 72 64 73 20 73 69 6d 70 6c 79 20 69 6e 74 65  ards simply inte
16b88 72 70 72 65 74 65 64 20 61 73 20 74 65 78 74 2e  rpreted as text.
16b89 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61  .**.** A NULL va
16b8a 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67  lue is not chang
16b8b 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69  ed by this routi
16b8c 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20  ne.  It remains 
16b8d 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  NULL..*/.case OP
16b8e 5f 54 6f 54 65 78 74 3a 20 7b 20 20 20 20 20 20  _ToText: {      
16b8f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
16b90 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 54 45 58  ame as TK_TO_TEX
16b91 54 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20  T, in1 */.  if( 
16b92 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
16b93 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b 0a  M_Null ) break;.
16b94 20 20 61 73 73 65 72 74 28 20 4d 45 4d 5f 53 74    assert( MEM_St
16b95 72 3d 3d 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29  r==(MEM_Blob>>3)
16b96 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67   );.  pIn1->flag
16b97 73 20 7c 3d 20 28 70 49 6e 31 2d 3e 66 6c 61 67  s |= (pIn1->flag
16b98 73 26 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a  s&MEM_Blob)>>3;.
16b99 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28    applyAffinity(
16b9a 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46  pIn1, SQLITE_AFF
16b9b 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e 67 29  _TEXT, encoding)
16b9c 3b 0a 20 20 72 63 20 3d 20 45 78 70 61 6e 64 42  ;.  rc = ExpandB
16b9d 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 20 61 73 73  lob(pIn1);.  ass
16b9e 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73  ert( pIn1->flags
16b9f 20 26 20 4d 45 4d 5f 53 74 72 20 7c 7c 20 64 62   & MEM_Str || db
16ba0 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
16ba1 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20  ;.  pIn1->flags 
16ba2 26 3d 20 7e 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d  &= ~(MEM_Int|MEM
16ba3 5f 52 65 61 6c 7c 4d 45 4d 5f 42 6c 6f 62 29 3b  _Real|MEM_Blob);
16ba4 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c  .  UPDATE_MAX_BL
16ba5 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20  OBSIZE(pIn1);.  
16ba6 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
16ba7 6f 64 65 3a 20 54 6f 42 6c 6f 62 20 50 31 20 2a  ode: ToBlob P1 *
16ba8 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72   * * *.**.** For
16ba9 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  ce the value in 
16baa 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 62  register P1 to b
16bab 65 20 61 20 42 4c 4f 42 2e 0a 2a 2a 20 49 66 20  e a BLOB..** If 
16bac 74 68 65 20 76 61 6c 75 65 20 69 73 20 6e 75 6d  the value is num
16bad 65 72 69 63 2c 20 63 6f 6e 76 65 72 74 20 69 74  eric, convert it
16bae 20 74 6f 20 61 20 73 74 72 69 6e 67 20 66 69 72   to a string fir
16baf 73 74 2e 0a 2a 2a 20 53 74 72 69 6e 67 73 20 61  st..** Strings a
16bb0 72 65 20 73 69 6d 70 6c 79 20 72 65 69 6e 74 65  re simply reinte
16bb1 72 70 72 65 74 65 64 20 61 73 20 62 6c 6f 62 73  rpreted as blobs
16bb2 20 77 69 74 68 20 6e 6f 20 63 68 61 6e 67 65 0a   with no change.
16bb3 2a 2a 20 74 6f 20 74 68 65 20 75 6e 64 65 72 6c  ** to the underl
16bb4 79 69 6e 67 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a  ying data..**.**
16bb5 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73   A NULL value is
16bb6 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20   not changed by 
16bb7 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49  this routine.  I
16bb8 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a  t remains NULL..
16bb9 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 42 6c 6f  */.case OP_ToBlo
16bba 62 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  b: {            
16bbb 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
16bbc 20 54 4b 5f 54 4f 5f 42 4c 4f 42 2c 20 69 6e 31   TK_TO_BLOB, in1
16bbd 20 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e   */.  if( pIn1->
16bbe 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
16bbf 20 29 20 62 72 65 61 6b 3b 0a 20 20 69 66 28 20   ) break;.  if( 
16bc0 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d  (pIn1->flags & M
16bc1 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 7b 0a 20  EM_Blob)==0 ){. 
16bc2 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79     applyAffinity
16bc3 28 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46  (pIn1, SQLITE_AF
16bc4 46 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e 67  F_TEXT, encoding
16bc5 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
16bc6 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
16bc7 5f 53 74 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  _Str || db->mall
16bc8 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 7d 0a  ocFailed );.  }.
16bc9 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
16bca 28 70 49 6e 31 2c 20 4d 45 4d 5f 42 6c 6f 62 29  (pIn1, MEM_Blob)
16bcb 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42  ;.  UPDATE_MAX_B
16bcc 4c 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20  LOBSIZE(pIn1);. 
16bcd 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
16bce 63 6f 64 65 3a 20 54 6f 4e 75 6d 65 72 69 63 20  code: ToNumeric 
16bcf 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  P1 * * * *.**.**
16bd0 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65   Force the value
16bd1 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
16bd2 74 6f 20 62 65 20 6e 75 6d 65 72 69 63 20 28 65  to be numeric (e
16bd3 69 74 68 65 72 20 61 6e 0a 2a 2a 20 69 6e 74 65  ither an.** inte
16bd4 67 65 72 20 6f 72 20 61 20 66 6c 6f 61 74 69 6e  ger or a floatin
16bd5 67 2d 70 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 29  g-point number.)
16bd6 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65  .** If the value
16bd7 20 69 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62   is text or blob
16bd8 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74  , try to convert
16bd9 20 69 74 20 74 6f 20 61 6e 20 75 73 69 6e 67 20   it to an using 
16bda 74 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e  the.** equivalen
16bdb 74 20 6f 66 20 61 74 6f 69 28 29 20 6f 72 20 61  t of atoi() or a
16bdc 74 6f 66 28 29 20 61 6e 64 20 73 74 6f 72 65 20  tof() and store 
16bdd 30 20 69 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e  0 if no such con
16bde 76 65 72 73 69 6f 6e 20 0a 2a 2a 20 69 73 20 70  version .** is p
16bdf 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41  ossible..**.** A
16be0 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e   NULL value is n
16be1 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68  ot changed by th
16be2 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20  is routine.  It 
16be3 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f  remains NULL..*/
16be4 0a 63 61 73 65 20 4f 50 5f 54 6f 4e 75 6d 65 72  .case OP_ToNumer
16be5 69 63 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  ic: {           
16be6 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
16be7 73 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2c  s TK_TO_NUMERIC,
16be8 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70   in1 */.  if( (p
16be9 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45  In1->flags & (ME
16bea 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 49 6e 74 7c 4d  M_Null|MEM_Int|M
16beb 45 4d 5f 52 65 61 6c 29 29 3d 3d 30 20 29 7b 0a  EM_Real))==0 ){.
16bec 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
16bed 65 6d 4e 75 6d 65 72 69 66 79 28 70 49 6e 31 29  emNumerify(pIn1)
16bee 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
16bef 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
16bf0 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a 0a  E_OMIT_CAST */..
16bf1 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 49 6e 74  /* Opcode: ToInt
16bf2 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a   P1 * * * *.**.*
16bf3 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75  * Force the valu
16bf4 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  e in register P1
16bf5 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 2e 20   be an integer. 
16bf6 20 49 66 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65   If.** The value
16bf7 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 20   is currently a 
16bf8 72 65 61 6c 20 6e 75 6d 62 65 72 2c 20 64 72 6f  real number, dro
16bf9 70 20 69 74 73 20 66 72 61 63 74 69 6f 6e 61 6c  p its fractional
16bfa 20 70 61 72 74 2e 0a 2a 2a 20 49 66 20 74 68 65   part..** If the
16bfb 20 76 61 6c 75 65 20 69 73 20 74 65 78 74 20 6f   value is text o
16bfc 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63  r blob, try to c
16bfd 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 6e 20  onvert it to an 
16bfe 69 6e 74 65 67 65 72 20 75 73 69 6e 67 20 74 68  integer using th
16bff 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20  e.** equivalent 
16c00 6f 66 20 61 74 6f 69 28 29 20 61 6e 64 20 73 74  of atoi() and st
16c01 6f 72 65 20 30 20 69 66 20 6e 6f 20 73 75 63 68  ore 0 if no such
16c02 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70   conversion is p
16c03 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41  ossible..**.** A
16c04 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e   NULL value is n
16c05 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68  ot changed by th
16c06 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20  is routine.  It 
16c07 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f  remains NULL..*/
16c08 0a 63 61 73 65 20 4f 50 5f 54 6f 49 6e 74 3a 20  .case OP_ToInt: 
16c09 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
16c0a 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
16c0b 5f 54 4f 5f 49 4e 54 2c 20 69 6e 31 20 2a 2f 0a  _TO_INT, in1 */.
16c0c 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61    if( (pIn1->fla
16c0d 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d  gs & MEM_Null)==
16c0e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
16c0f 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66  VdbeMemIntegerif
16c10 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62  y(pIn1);.  }.  b
16c11 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  reak;.}..#ifndef
16c12 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53   SQLITE_OMIT_CAS
16c13 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 52  T./* Opcode: ToR
16c14 65 61 6c 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a  eal P1 * * * *.*
16c15 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76  *.** Force the v
16c16 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
16c17 20 50 31 20 74 6f 20 62 65 20 61 20 66 6c 6f 61   P1 to be a floa
16c18 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65  ting point numbe
16c19 72 2e 0a 2a 2a 20 49 66 20 54 68 65 20 76 61 6c  r..** If The val
16c1a 75 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  ue is currently 
16c1b 61 6e 20 69 6e 74 65 67 65 72 2c 20 63 6f 6e 76  an integer, conv
16c1c 65 72 74 20 69 74 2e 0a 2a 2a 20 49 66 20 74 68  ert it..** If th
16c1d 65 20 76 61 6c 75 65 20 69 73 20 74 65 78 74 20  e value is text 
16c1e 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20  or blob, try to 
16c1f 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 6e  convert it to an
16c20 20 69 6e 74 65 67 65 72 20 75 73 69 6e 67 20 74   integer using t
16c21 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74  he.** equivalent
16c22 20 6f 66 20 61 74 6f 69 28 29 20 61 6e 64 20 73   of atoi() and s
16c23 74 6f 72 65 20 30 2e 30 20 69 66 20 6e 6f 20 73  tore 0.0 if no s
16c24 75 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69  uch conversion i
16c25 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a  s possible..**.*
16c26 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69  * A NULL value i
16c27 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79  s not changed by
16c28 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20   this routine.  
16c29 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e  It remains NULL.
16c2a 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 52 65  .*/.case OP_ToRe
16c2b 61 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  al: {           
16c2c 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
16c2d 73 20 54 4b 5f 54 4f 5f 52 45 41 4c 2c 20 69 6e  s TK_TO_REAL, in
16c2e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31  1 */.  if( (pIn1
16c2f 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  ->flags & MEM_Nu
16c30 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  ll)==0 ){.    sq
16c31 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c  lite3VdbeMemReal
16c32 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20  ify(pIn1);.  }. 
16c33 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
16c34 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
16c35 43 41 53 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f  CAST */../* Opco
16c36 64 65 3a 20 4c 74 20 50 31 20 50 32 20 50 33 20  de: Lt P1 P2 P3 
16c37 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70  P4 P5.**.** Comp
16c38 61 72 65 20 74 68 65 20 76 61 6c 75 65 73 20 69  are the values i
16c39 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 61 6e  n register P1 an
16c3a 64 20 50 33 2e 20 20 49 66 20 72 65 67 28 50 33  d P3.  If reg(P3
16c3b 29 3c 72 65 67 28 50 31 29 20 74 68 65 6e 0a 2a  )<reg(P1) then.*
16c3c 2a 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 65 73  * jump to addres
16c3d 73 20 50 32 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66  s P2.  .**.** If
16c3e 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50   the SQLITE_JUMP
16c3f 49 46 4e 55 4c 4c 20 62 69 74 20 6f 66 20 50 35  IFNULL bit of P5
16c40 20 69 73 20 73 65 74 20 61 6e 64 20 65 69 74 68   is set and eith
16c41 65 72 20 72 65 67 28 50 31 29 20 6f 72 0a 2a 2a  er reg(P1) or.**
16c42 20 72 65 67 28 50 33 29 20 69 73 20 4e 55 4c 4c   reg(P3) is NULL
16c43 20 74 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a   then take the j
16c44 75 6d 70 2e 20 20 49 66 20 74 68 65 20 53 51 4c  ump.  If the SQL
16c45 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 0a  ITE_JUMPIFNULL .
16c46 2a 2a 20 62 69 74 20 69 73 20 63 6c 65 61 72 20  ** bit is clear 
16c47 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 75 20 69  then fall thru i
16c48 66 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64  f either operand
16c49 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20   is NULL..**.** 
16c4a 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 55  If the SQLITE_NU
16c4b 4c 4c 45 51 55 41 4c 20 62 69 74 20 6f 66 20 50  LLEQUAL bit of P
16c4c 35 20 69 73 20 73 65 74 20 74 68 65 6e 20 74 72  5 is set then tr
16c4d 65 61 74 20 4e 55 4c 4c 20 6f 70 65 72 61 6e 64  eat NULL operand
16c4e 73 0a 2a 2a 20 61 73 20 62 65 69 6e 67 20 65 71  s.** as being eq
16c4f 75 61 6c 20 74 6f 20 6f 6e 65 20 61 6e 6f 74 68  ual to one anoth
16c50 65 72 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 4e 55  er.  Normally NU
16c51 4c 4c 73 20 61 72 65 20 6e 6f 74 20 65 71 75 61  LLs are not equa
16c52 6c 20 74 6f 20 0a 2a 2a 20 61 6e 79 74 68 69 6e  l to .** anythin
16c53 67 20 69 6e 63 6c 75 64 69 6e 67 20 6f 74 68 65  g including othe
16c54 72 20 4e 55 4c 4c 73 2e 0a 2a 2a 0a 2a 2a 20 54  r NULLs..**.** T
16c55 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41  he SQLITE_AFF_MA
16c56 53 4b 20 70 6f 72 74 69 6f 6e 20 6f 66 20 50 35  SK portion of P5
16c57 20 6d 75 73 74 20 62 65 20 61 6e 20 61 66 66 69   must be an affi
16c58 6e 69 74 79 20 63 68 61 72 61 63 74 65 72 20 2d  nity character -
16c59 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 54  .** SQLITE_AFF_T
16c5a 45 58 54 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f  EXT, SQLITE_AFF_
16c5b 49 4e 54 45 47 45 52 2c 20 61 6e 64 20 73 6f 20  INTEGER, and so 
16c5c 66 6f 72 74 68 2e 20 41 6e 20 61 74 74 65 6d 70  forth. An attemp
16c5d 74 20 69 73 20 6d 61 64 65 20 0a 2a 2a 20 74 6f  t is made .** to
16c5e 20 63 6f 65 72 63 65 20 62 6f 74 68 20 69 6e 70   coerce both inp
16c5f 75 74 73 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  uts according to
16c60 20 74 68 69 73 20 61 66 66 69 6e 69 74 79 20 62   this affinity b
16c61 65 66 6f 72 65 20 74 68 65 0a 2a 2a 20 63 6f 6d  efore the.** com
16c62 70 61 72 69 73 6f 6e 20 69 73 20 6d 61 64 65 2e  parison is made.
16c63 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 41   If the SQLITE_A
16c64 46 46 5f 4d 41 53 4b 20 69 73 20 30 78 30 30 2c  FF_MASK is 0x00,
16c65 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 0a 2a 2a   then numeric.**
16c66 20 61 66 66 69 6e 69 74 79 20 69 73 20 75 73 65   affinity is use
16c67 64 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  d. Note that the
16c68 20 61 66 66 69 6e 69 74 79 20 63 6f 6e 76 65 72   affinity conver
16c69 73 69 6f 6e 73 20 61 72 65 20 73 74 6f 72 65 64  sions are stored
16c6a 0a 2a 2a 20 62 61 63 6b 20 69 6e 74 6f 20 74 68  .** back into th
16c6b 65 20 69 6e 70 75 74 20 72 65 67 69 73 74 65 72  e input register
16c6c 73 20 50 31 20 61 6e 64 20 50 33 2e 20 20 53 6f  s P1 and P3.  So
16c6d 20 74 68 69 73 20 6f 70 63 6f 64 65 20 63 61 6e   this opcode can
16c6e 20 63 61 75 73 65 0a 2a 2a 20 70 65 72 73 69 73   cause.** persis
16c6f 74 65 6e 74 20 63 68 61 6e 67 65 73 20 74 6f 20  tent changes to 
16c70 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e 64  registers P1 and
16c71 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20   P3..**.** Once 
16c72 61 6e 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20  any conversions 
16c73 68 61 76 65 20 74 61 6b 65 6e 20 70 6c 61 63 65  have taken place
16c74 2c 20 61 6e 64 20 6e 65 69 74 68 65 72 20 76 61  , and neither va
16c75 6c 75 65 20 69 73 20 4e 55 4c 4c 2c 20 0a 2a 2a  lue is NULL, .**
16c76 20 74 68 65 20 76 61 6c 75 65 73 20 61 72 65 20   the values are 
16c77 63 6f 6d 70 61 72 65 64 2e 20 49 66 20 62 6f 74  compared. If bot
16c78 68 20 76 61 6c 75 65 73 20 61 72 65 20 62 6c 6f  h values are blo
16c79 62 73 20 74 68 65 6e 20 6d 65 6d 63 6d 70 28 29  bs then memcmp()
16c7a 20 69 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 64   is.** used to d
16c7b 65 74 65 72 6d 69 6e 65 20 74 68 65 20 72 65 73  etermine the res
16c7c 75 6c 74 73 20 6f 66 20 74 68 65 20 63 6f 6d 70  ults of the comp
16c7d 61 72 69 73 6f 6e 2e 20 20 49 66 20 62 6f 74 68  arison.  If both
16c7e 20 76 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 74   values.** are t
16c7f 65 78 74 2c 20 74 68 65 6e 20 74 68 65 20 61 70  ext, then the ap
16c80 70 72 6f 70 72 69 61 74 65 20 63 6f 6c 6c 61 74  propriate collat
16c81 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 70 65  ing function spe
16c82 63 69 66 69 65 64 20 69 6e 0a 2a 2a 20 50 34 20  cified in.** P4 
16c83 69 73 20 20 75 73 65 64 20 74 6f 20 64 6f 20 74  is  used to do t
16c84 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20  he comparison.  
16c85 49 66 20 50 34 20 69 73 20 6e 6f 74 20 73 70 65  If P4 is not spe
16c86 63 69 66 69 65 64 20 74 68 65 6e 0a 2a 2a 20 6d  cified then.** m
16c87 65 6d 63 6d 70 28 29 20 69 73 20 75 73 65 64 20  emcmp() is used 
16c88 74 6f 20 63 6f 6d 70 61 72 65 20 74 65 78 74 20  to compare text 
16c89 73 74 72 69 6e 67 2e 20 20 49 66 20 62 6f 74 68  string.  If both
16c8a 20 76 61 6c 75 65 73 20 61 72 65 0a 2a 2a 20 6e   values are.** n
16c8b 75 6d 65 72 69 63 2c 20 74 68 65 6e 20 61 20 6e  umeric, then a n
16c8c 75 6d 65 72 69 63 20 63 6f 6d 70 61 72 69 73 6f  umeric compariso
16c8d 6e 20 69 73 20 75 73 65 64 2e 20 49 66 20 74 68  n is used. If th
16c8e 65 20 74 77 6f 20 76 61 6c 75 65 73 0a 2a 2a 20  e two values.** 
16c8f 61 72 65 20 6f 66 20 64 69 66 66 65 72 65 6e 74  are of different
16c90 20 74 79 70 65 73 2c 20 74 68 65 6e 20 6e 75 6d   types, then num
16c91 62 65 72 73 20 61 72 65 20 63 6f 6e 73 69 64 65  bers are conside
16c92 72 65 64 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a  red less than.**
16c93 20 73 74 72 69 6e 67 73 20 61 6e 64 20 73 74 72   strings and str
16c94 69 6e 67 73 20 61 72 65 20 63 6f 6e 73 69 64 65  ings are conside
16c95 72 65 64 20 6c 65 73 73 20 74 68 61 6e 20 62 6c  red less than bl
16c96 6f 62 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  obs..**.** If th
16c97 65 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32  e SQLITE_STOREP2
16c98 20 62 69 74 20 6f 66 20 50 35 20 69 73 20 73 65   bit of P5 is se
16c99 74 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 6a  t, then do not j
16c9a 75 6d 70 2e 20 20 49 6e 73 74 65 61 64 2c 0a 2a  ump.  Instead,.*
16c9b 2a 20 73 74 6f 72 65 20 61 20 62 6f 6f 6c 65 61  * store a boolea
16c9c 6e 20 72 65 73 75 6c 74 20 28 65 69 74 68 65 72  n result (either
16c9d 20 30 2c 20 6f 72 20 31 2c 20 6f 72 20 4e 55 4c   0, or 1, or NUL
16c9e 4c 29 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  L) in register P
16c9f 32 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  2..*/./* Opcode:
16ca0 20 4e 65 20 50 31 20 50 32 20 50 33 20 50 34 20   Ne P1 P2 P3 P4 
16ca1 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f  P5.**.** This wo
16ca2 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68  rks just like th
16ca3 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65  e Lt opcode exce
16ca4 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70  pt that the jump
16ca5 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20   is taken if.** 
16ca6 74 68 65 20 6f 70 65 72 61 6e 64 73 20 69 6e 20  the operands in 
16ca7 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e 64  registers P1 and
16ca8 20 50 33 20 61 72 65 20 6e 6f 74 20 65 71 75 61   P3 are not equa
16ca9 6c 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f  l.  See the Lt o
16caa 70 63 6f 64 65 20 66 6f 72 0a 2a 2a 20 61 64 64  pcode for.** add
16cab 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
16cac 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  ion..*/./* Opcod
16cad 65 3a 20 45 71 20 50 31 20 50 32 20 50 33 20 50  e: Eq P1 P2 P3 P
16cae 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  4 P5.**.** This 
16caf 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20  works just like 
16cb0 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78  the Lt opcode ex
16cb1 63 65 70 74 20 74 68 61 74 20 74 68 65 20 6a 75  cept that the ju
16cb2 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a  mp is taken if.*
16cb3 2a 20 74 68 65 20 6f 70 65 72 61 6e 64 73 20 69  * the operands i
16cb4 6e 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61  n registers P1 a
16cb5 6e 64 20 50 33 20 61 72 65 20 65 71 75 61 6c 2e  nd P3 are equal.
16cb6 0a 2a 2a 20 53 65 65 20 74 68 65 20 4c 74 20 6f  .** See the Lt o
16cb7 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69  pcode for additi
16cb8 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
16cb9 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
16cba 4c 65 20 50 31 20 50 32 20 50 33 20 50 34 20 50  Le P1 P2 P3 P4 P
16cbb 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72  5.**.** This wor
16cbc 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65  ks just like the
16cbd 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70   Lt opcode excep
16cbe 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20  t that the jump 
16cbf 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74  is taken if.** t
16cc0 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65  he content of re
16cc1 67 69 73 74 65 72 20 50 33 20 69 73 20 6c 65 73  gister P3 is les
16cc2 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  s than or equal 
16cc3 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  to the content o
16cc4 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31  f.** register P1
16cc5 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70  .  See the Lt op
16cc6 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f  code for additio
16cc7 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
16cc8 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 47  .*/./* Opcode: G
16cc9 74 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35  t P1 P2 P3 P4 P5
16cca 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b  .**.** This work
16ccb 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20  s just like the 
16ccc 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74  Lt opcode except
16ccd 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69   that the jump i
16cce 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68  s taken if.** th
16ccf 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67  e content of reg
16cd0 69 73 74 65 72 20 50 33 20 69 73 20 67 72 65 61  ister P3 is grea
16cd1 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 6f 6e  ter than the con
16cd2 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 69 73  tent of.** regis
16cd3 74 65 72 20 50 31 2e 20 20 53 65 65 20 74 68 65  ter P1.  See the
16cd4 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61   Lt opcode for a
16cd5 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
16cd6 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63  ation..*/./* Opc
16cd7 6f 64 65 3a 20 47 65 20 50 31 20 50 32 20 50 33  ode: Ge P1 P2 P3
16cd8 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69   P4 P5.**.** Thi
16cd9 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b  s works just lik
16cda 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20  e the Lt opcode 
16cdb 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20  except that the 
16cdc 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66  jump is taken if
16cdd 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  .** the content 
16cde 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20 69  of register P3 i
16cdf 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f  s greater than o
16ce0 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 63  r equal to the c
16ce1 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 67  ontent of.** reg
16ce2 69 73 74 65 72 20 50 31 2e 20 20 53 65 65 20 74  ister P1.  See t
16ce3 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72  he Lt opcode for
16ce4 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
16ce5 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65  rmation..*/.case
16ce6 20 4f 50 5f 45 71 3a 20 20 20 20 20 20 20 20 20   OP_Eq:         
16ce7 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
16ce8 20 54 4b 5f 45 51 2c 20 6a 75 6d 70 2c 20 69 6e   TK_EQ, jump, in
16ce9 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f  1, in3 */.case O
16cea 50 5f 4e 65 3a 20 20 20 20 20 20 20 20 20 20 20  P_Ne:           
16ceb 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
16cec 4b 5f 4e 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c  K_NE, jump, in1,
16ced 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f   in3 */.case OP_
16cee 4c 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 20  Lt:             
16cef 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
16cf0 4c 54 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69  LT, jump, in1, i
16cf1 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 65  n3 */.case OP_Le
16cf2 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  :               
16cf3 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 45  /* same as TK_LE
16cf4 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33  , jump, in1, in3
16cf5 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 74 3a 20   */.case OP_Gt: 
16cf6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
16cf7 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 54 2c 20   same as TK_GT, 
16cf8 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a  jump, in1, in3 *
16cf9 2f 0a 63 61 73 65 20 4f 50 5f 47 65 3a 20 7b 20  /.case OP_Ge: { 
16cfa 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
16cfb 61 6d 65 20 61 73 20 54 4b 5f 47 45 2c 20 6a 75  ame as TK_GE, ju
16cfc 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a  mp, in1, in3 */.
16cfd 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 69    int flags;.  i
16cfe 6e 74 20 72 65 73 3b 0a 20 20 63 68 61 72 20 61  nt res;.  char a
16cff 66 66 69 6e 69 74 79 3b 0a 20 20 4d 65 6d 20 78  ffinity;.  Mem x
16d00 31 2c 20 78 33 3b 0a 0a 20 20 66 6c 61 67 73 20  1, x3;..  flags 
16d01 3d 20 70 49 6e 31 2d 3e 66 6c 61 67 73 7c 70 49  = pIn1->flags|pI
16d02 6e 33 2d 3e 66 6c 61 67 73 3b 0a 0a 20 20 69 66  n3->flags;..  if
16d03 28 20 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c  ( flags&MEM_Null
16d04 20 29 7b 0a 20 20 20 20 69 66 28 20 28 70 4f 70   ){.    if( (pOp
16d05 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 4e 55  ->p5 & SQLITE_NU
16d06 4c 4c 45 51 55 41 4c 29 21 3d 30 20 29 7b 0a 20  LLEQUAL)!=0 ){. 
16d07 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a       /*.      **
16d08 20 57 68 65 6e 20 53 51 4c 49 54 45 5f 4e 55 4c   When SQLITE_NUL
16d09 4c 45 51 55 41 4c 20 73 65 74 20 61 6e 64 20 65  LEQUAL set and e
16d0a 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73  ither operand is
16d0b 20 4e 55 4c 4c 0a 20 20 20 20 20 20 2a 2a 20 74   NULL.      ** t
16d0c 68 65 6e 20 62 6f 74 68 20 6f 70 65 72 61 6e 64  hen both operand
16d0d 73 20 61 72 65 20 63 6f 6e 76 65 72 74 65 64 20  s are converted 
16d0e 74 6f 20 69 6e 74 65 67 65 72 73 20 70 72 69 6f  to integers prio
16d0f 72 20 74 6f 20 62 65 69 6e 67 20 0a 20 20 20 20  r to being .    
16d10 20 20 2a 2a 20 70 61 73 73 65 64 20 64 6f 77 6e    ** passed down
16d11 20 69 6e 74 6f 20 74 68 65 20 6e 6f 72 6d 61 6c   into the normal
16d12 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6c 6f 67 69   comparison logi
16d13 63 20 62 65 6c 6f 77 2e 20 20 0a 20 20 20 20 20  c below.  .     
16d14 20 2a 2a 20 4e 55 4c 4c 20 6f 70 65 72 61 6e 64   ** NULL operand
16d15 73 20 61 72 65 20 63 6f 6e 76 65 72 74 65 64 20  s are converted 
16d16 74 6f 20 7a 65 72 6f 20 61 6e 64 20 6e 6f 6e 2d  to zero and non-
16d17 4e 55 4c 4c 20 6f 70 65 72 61 6e 64 73 0a 20 20  NULL operands.  
16d18 20 20 20 20 2a 2a 20 61 72 65 20 63 6f 6e 76 65      ** are conve
16d19 72 74 65 64 20 74 6f 20 31 2e 20 20 54 68 75 73  rted to 1.  Thus
16d1a 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77  , for example, w
16d1b 69 74 68 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45  ith SQLITE_NULLE
16d1c 51 55 41 4c 0a 20 20 20 20 20 20 2a 2a 20 73 65  QUAL.      ** se
16d1d 74 2c 20 20 4e 55 4c 4c 3d 3d 4e 55 4c 4c 20 69  t,  NULL==NULL i
16d1e 73 20 74 72 75 65 20 77 68 65 72 65 61 73 20 69  s true whereas i
16d1f 74 20 77 6f 75 6c 64 20 6e 6f 72 6d 61 6c 6c 79  t would normally
16d20 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 2a 2a 20   NULL..      ** 
16d21 53 69 6d 69 6c 61 72 6c 79 2c 20 20 4e 55 4c 4c  Similarly,  NULL
16d22 21 3d 31 32 33 20 69 73 20 74 72 75 65 2e 0a 20  !=123 is true.. 
16d23 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 78 31       */.      x1
16d24 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74  .flags = MEM_Int
16d25 3b 0a 20 20 20 20 20 20 78 31 2e 75 2e 69 20 3d  ;.      x1.u.i =
16d26 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20   (pIn1->flags & 
16d27 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 3b 0a 20 20  MEM_Null)==0;.  
16d28 20 20 20 20 70 49 6e 31 20 3d 20 26 78 31 3b 0a      pIn1 = &x1;.
16d29 20 20 20 20 20 20 78 33 2e 66 6c 61 67 73 20 3d        x3.flags =
16d2a 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20   MEM_Int;.      
16d2b 78 33 2e 75 2e 69 20 3d 20 28 70 49 6e 33 2d 3e  x3.u.i = (pIn3->
16d2c 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
16d2d 29 3d 3d 30 3b 0a 20 20 20 20 20 20 70 49 6e 33  )==0;.      pIn3
16d2e 20 3d 20 26 78 33 3b 0a 20 20 20 20 7d 65 6c 73   = &x3;.    }els
16d2f 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74  e{.      /* If t
16d30 68 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51  he SQLITE_NULLEQ
16d31 55 41 4c 20 62 69 74 20 69 73 20 63 6c 65 61 72  UAL bit is clear
16d32 20 61 6e 64 20 65 69 74 68 65 72 20 6f 70 65 72   and either oper
16d33 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e  and is NULL then
16d34 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72 65  .      ** the re
16d35 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20 4e  sult is always N
16d36 55 4c 4c 2e 20 20 54 68 65 20 6a 75 6d 70 20 69  ULL.  The jump i
16d37 73 20 74 61 6b 65 6e 20 69 66 20 74 68 65 20 0a  s taken if the .
16d38 20 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f        ** SQLITE_
16d39 4a 55 4d 50 49 46 4e 55 4c 4c 20 62 69 74 20 69  JUMPIFNULL bit i
16d3a 73 20 73 65 74 2e 0a 20 20 20 20 20 20 2a 2f 0a  s set..      */.
16d3b 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70        if( pOp->p
16d3c 35 20 26 20 53 51 4c 49 54 45 5f 53 54 4f 52 45  5 & SQLITE_STORE
16d3d 50 32 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4f  P2 ){.        pO
16d3e 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  ut = &p->aMem[pO
16d3f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 20 20  p->p2];.        
16d40 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
16d41 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a  Out, MEM_Null);.
16d42 20 20 20 20 20 20 20 20 52 45 47 49 53 54 45 52          REGISTER
16d43 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20  _TRACE(pOp->p2, 
16d44 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c  pOut);.      }el
16d45 73 65 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26  se if( pOp->p5 &
16d46 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55   SQLITE_JUMPIFNU
16d47 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 63  LL ){.        pc
16d48 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20   = pOp->p2-1;.  
16d49 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
16d4a 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  k;.    }.  }..  
16d4b 61 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e  affinity = pOp->
16d4c 70 35 20 26 20 53 51 4c 49 54 45 5f 41 46 46 5f  p5 & SQLITE_AFF_
16d4d 4d 41 53 4b 3b 0a 20 20 69 66 28 20 61 66 66 69  MASK;.  if( affi
16d4e 6e 69 74 79 20 29 7b 0a 20 20 20 20 61 70 70 6c  nity ){.    appl
16d4f 79 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20  yAffinity(pIn1, 
16d50 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 69  affinity, encodi
16d51 6e 67 29 3b 0a 20 20 20 20 61 70 70 6c 79 41 66  ng);.    applyAf
16d52 66 69 6e 69 74 79 28 70 49 6e 33 2c 20 61 66 66  finity(pIn3, aff
16d53 69 6e 69 74 79 2c 20 65 6e 63 6f 64 69 6e 67 29  inity, encoding)
16d54 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  ;.  }..  assert(
16d55 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34   pOp->p4type==P4
16d56 5f 43 4f 4c 4c 53 45 51 20 7c 7c 20 70 4f 70 2d  _COLLSEQ || pOp-
16d57 3e 70 34 2e 70 43 6f 6c 6c 3d 3d 30 20 29 3b 0a  >p4.pColl==0 );.
16d58 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e    ExpandBlob(pIn
16d59 31 29 3b 0a 20 20 45 78 70 61 6e 64 42 6c 6f 62  1);.  ExpandBlob
16d5a 28 70 49 6e 33 29 3b 0a 20 20 72 65 73 20 3d 20  (pIn3);.  res = 
16d5b 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72  sqlite3MemCompar
16d5c 65 28 70 49 6e 33 2c 20 70 49 6e 31 2c 20 70 4f  e(pIn3, pIn1, pO
16d5d 70 2d 3e 70 34 2e 70 43 6f 6c 6c 29 3b 0a 20 20  p->p4.pColl);.  
16d5e 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63  switch( pOp->opc
16d5f 6f 64 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20  ode ){.    case 
16d60 4f 50 5f 45 71 3a 20 20 20 20 72 65 73 20 3d 20  OP_Eq:    res = 
16d61 72 65 73 3d 3d 30 3b 20 20 20 20 20 62 72 65 61  res==0;     brea
16d62 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4e  k;.    case OP_N
16d63 65 3a 20 20 20 20 72 65 73 20 3d 20 72 65 73 21  e:    res = res!
16d64 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  =0;     break;. 
16d65 20 20 20 63 61 73 65 20 4f 50 5f 4c 74 3a 20 20     case OP_Lt:  
16d66 20 20 72 65 73 20 3d 20 72 65 73 3c 30 3b 20 20    res = res<0;  
16d67 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
16d68 61 73 65 20 4f 50 5f 4c 65 3a 20 20 20 20 72 65  ase OP_Le:    re
16d69 73 20 3d 20 72 65 73 3c 3d 30 3b 20 20 20 20 20  s = res<=0;     
16d6a 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
16d6b 4f 50 5f 47 74 3a 20 20 20 20 72 65 73 20 3d 20  OP_Gt:    res = 
16d6c 72 65 73 3e 30 3b 20 20 20 20 20 20 62 72 65 61  res>0;      brea
16d6d 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  k;.    default: 
16d6e 20 20 20 20 20 20 72 65 73 20 3d 20 72 65 73 3e        res = res>
16d6f 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  =0;     break;. 
16d70 20 7d 0a 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70   }..  if( pOp->p
16d71 35 20 26 20 53 51 4c 49 54 45 5f 53 54 4f 52 45  5 & SQLITE_STORE
16d72 50 32 20 29 7b 0a 20 20 20 20 70 4f 75 74 20 3d  P2 ){.    pOut =
16d73 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
16d74 32 5d 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79  2];.    MemSetTy
16d75 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d  peFlag(pOut, MEM
16d76 5f 49 6e 74 29 3b 0a 20 20 20 20 70 4f 75 74 2d  _Int);.    pOut-
16d77 3e 75 2e 69 20 3d 20 72 65 73 3b 0a 20 20 20 20  >u.i = res;.    
16d78 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
16d79 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20  Op->p2, pOut);. 
16d7a 20 7d 65 6c 73 65 20 69 66 28 20 72 65 73 20 29   }else if( res )
16d7b 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e  {.    pc = pOp->
16d7c 70 32 2d 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61  p2-1;.  }.  brea
16d7d 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
16d7e 20 41 6e 64 20 50 31 20 50 32 20 50 33 20 2a 20   And P1 P2 P3 * 
16d7f 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65  *.**.** Take the
16d80 20 6c 6f 67 69 63 61 6c 20 41 4e 44 20 6f 66 20   logical AND of 
16d81 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65  the values in re
16d82 67 69 73 74 65 72 73 20 50 31 20 61 6e 64 20 50  gisters P1 and P
16d83 32 20 61 6e 64 0a 2a 2a 20 77 72 69 74 65 20 74  2 and.** write t
16d84 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 72  he result into r
16d85 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 0a 2a  egister P3..**.*
16d86 2a 20 49 66 20 65 69 74 68 65 72 20 50 31 20 6f  * If either P1 o
16d87 72 20 50 32 20 69 73 20 30 20 28 66 61 6c 73 65  r P2 is 0 (false
16d88 29 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  ) then the resul
16d89 74 20 69 73 20 30 20 65 76 65 6e 20 69 66 0a 2a  t is 0 even if.*
16d8a 2a 20 74 68 65 20 6f 74 68 65 72 20 69 6e 70 75  * the other inpu
16d8b 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41 20 4e 55  t is NULL.  A NU
16d8c 4c 4c 20 61 6e 64 20 74 72 75 65 20 6f 72 20 74  LL and true or t
16d8d 77 6f 20 4e 55 4c 4c 73 20 67 69 76 65 0a 2a 2a  wo NULLs give.**
16d8e 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75 74 2e 0a   a NULL output..
16d8f 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 72  */./* Opcode: Or
16d90 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a   P1 P2 P3 * *.**
16d91 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 6c 6f 67  .** Take the log
16d92 69 63 61 6c 20 4f 52 20 6f 66 20 74 68 65 20 76  ical OR of the v
16d93 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65  alues in registe
16d94 72 20 50 31 20 61 6e 64 20 50 32 20 61 6e 64 0a  r P1 and P2 and.
16d95 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 61 6e 73  ** store the ans
16d96 77 65 72 20 69 6e 20 72 65 67 69 73 74 65 72 20  wer in register 
16d97 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 69 74  P3..**.** If eit
16d98 68 65 72 20 50 31 20 6f 72 20 50 32 20 69 73 20  her P1 or P2 is 
16d99 6e 6f 6e 7a 65 72 6f 20 28 74 72 75 65 29 20 74  nonzero (true) t
16d9a 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69  hen the result i
16d9b 73 20 31 20 28 74 72 75 65 29 0a 2a 2a 20 65 76  s 1 (true).** ev
16d9c 65 6e 20 69 66 20 74 68 65 20 6f 74 68 65 72 20  en if the other 
16d9d 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2e 20 20  input is NULL.  
16d9e 41 20 4e 55 4c 4c 20 61 6e 64 20 66 61 6c 73 65  A NULL and false
16d9f 20 6f 72 20 74 77 6f 20 4e 55 4c 4c 73 0a 2a 2a   or two NULLs.**
16da0 20 67 69 76 65 20 61 20 4e 55 4c 4c 20 6f 75 74   give a NULL out
16da1 70 75 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  put..*/.case OP_
16da2 41 6e 64 3a 20 20 20 20 20 20 20 20 20 20 20 20  And:            
16da3 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
16da4 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f  AND, in1, in2, o
16da5 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f  ut3 */.case OP_O
16da6 72 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  r: {            
16da7 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4f   /* same as TK_O
16da8 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74  R, in1, in2, out
16da9 33 20 2a 2f 0a 20 20 69 6e 74 20 76 31 2c 20 76  3 */.  int v1, v
16daa 32 3b 20 20 20 20 2f 2a 20 30 3d 3d 46 41 4c 53  2;    /* 0==FALS
16dab 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 3d 3d 55  E, 1==TRUE, 2==U
16dac 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c 4c 20 2a  NKNOWN or NULL *
16dad 2f 0a 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66  /..  if( pIn1->f
16dae 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20  lags & MEM_Null 
16daf 29 7b 0a 20 20 20 20 76 31 20 3d 20 32 3b 0a 20  ){.    v1 = 2;. 
16db0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 76 31 20 3d   }else{.    v1 =
16db1 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56   sqlite3VdbeIntV
16db2 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 3b 0a 20  alue(pIn1)!=0;. 
16db3 20 7d 0a 20 20 69 66 28 20 70 49 6e 32 2d 3e 66   }.  if( pIn2->f
16db4 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20  lags & MEM_Null 
16db5 29 7b 0a 20 20 20 20 76 32 20 3d 20 32 3b 0a 20  ){.    v2 = 2;. 
16db6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 76 32 20 3d   }else{.    v2 =
16db7 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56   sqlite3VdbeIntV
16db8 61 6c 75 65 28 70 49 6e 32 29 21 3d 30 3b 0a 20  alue(pIn2)!=0;. 
16db9 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70   }.  if( pOp->op
16dba 63 6f 64 65 3d 3d 4f 50 5f 41 6e 64 20 29 7b 0a  code==OP_And ){.
16dbb 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
16dbc 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61   unsigned char a
16dbd 6e 64 5f 6c 6f 67 69 63 5b 5d 20 3d 20 7b 20 30  nd_logic[] = { 0
16dbe 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 32 2c  , 0, 0, 0, 1, 2,
16dbf 20 30 2c 20 32 2c 20 32 20 7d 3b 0a 20 20 20 20   0, 2, 2 };.    
16dc0 76 31 20 3d 20 61 6e 64 5f 6c 6f 67 69 63 5b 76  v1 = and_logic[v
16dc1 31 2a 33 2b 76 32 5d 3b 0a 20 20 7d 65 6c 73 65  1*3+v2];.  }else
16dc2 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e  {.    static con
16dc3 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
16dc4 20 6f 72 5f 6c 6f 67 69 63 5b 5d 20 3d 20 7b 20   or_logic[] = { 
16dc5 30 2c 20 31 2c 20 32 2c 20 31 2c 20 31 2c 20 31  0, 1, 2, 1, 1, 1
16dc6 2c 20 32 2c 20 31 2c 20 32 20 7d 3b 0a 20 20 20  , 2, 1, 2 };.   
16dc7 20 76 31 20 3d 20 6f 72 5f 6c 6f 67 69 63 5b 76   v1 = or_logic[v
16dc8 31 2a 33 2b 76 32 5d 3b 0a 20 20 7d 0a 20 20 69  1*3+v2];.  }.  i
16dc9 66 28 20 76 31 3d 3d 32 20 29 7b 0a 20 20 20 20  f( v1==2 ){.    
16dca 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
16dcb 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a  Out, MEM_Null);.
16dcc 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4f 75    }else{.    pOu
16dcd 74 2d 3e 75 2e 69 20 3d 20 76 31 3b 0a 20 20 20  t->u.i = v1;.   
16dce 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28   MemSetTypeFlag(
16dcf 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a  pOut, MEM_Int);.
16dd0 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
16dd1 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 20 50  /* Opcode: Not P
16dd2 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  1 * * * *.**.** 
16dd3 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 76 61  Interpret the va
16dd4 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
16dd5 50 31 20 61 73 20 61 20 62 6f 6f 6c 65 61 6e 20  P1 as a boolean 
16dd6 76 61 6c 75 65 2e 20 20 52 65 70 6c 61 63 65 20  value.  Replace 
16dd7 69 74 0a 2a 2a 20 77 69 74 68 20 69 74 73 20 63  it.** with its c
16dd8 6f 6d 70 6c 65 6d 65 6e 74 2e 20 20 49 66 20 74  omplement.  If t
16dd9 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
16dda 73 74 65 72 20 50 31 20 69 73 20 4e 55 4c 4c 20  ster P1 is NULL 
16ddb 69 74 73 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20  its value.** is 
16ddc 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 63 61  unchanged..*/.ca
16ddd 73 65 20 4f 50 5f 4e 6f 74 3a 20 7b 20 20 20 20  se OP_Not: {    
16dde 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
16ddf 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 2c 20 69  ame as TK_NOT, i
16de0 6e 31 20 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31  n1 */.  if( pIn1
16de1 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  ->flags & MEM_Nu
16de2 6c 6c 20 29 20 62 72 65 61 6b 3b 20 20 2f 2a 20  ll ) break;  /* 
16de3 44 6f 20 6e 6f 74 68 69 6e 67 20 74 6f 20 4e 55  Do nothing to NU
16de4 4c 4c 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  LLs */.  sqlite3
16de5 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66  VdbeMemIntegerif
16de6 79 28 70 49 6e 31 29 3b 0a 20 20 70 49 6e 31 2d  y(pIn1);.  pIn1-
16de7 3e 75 2e 69 20 3d 20 21 70 49 6e 31 2d 3e 75 2e  >u.i = !pIn1->u.
16de8 69 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e  i;.  assert( pIn
16de9 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74  1->flags&MEM_Int
16dea 20 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a   );.  break;.}..
16deb 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 4e 6f  /* Opcode: BitNo
16dec 74 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a  t P1 * * * *.**.
16ded 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68 65  ** Interpret the
16dee 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69   content of regi
16def 73 74 65 72 20 50 31 20 61 73 20 61 6e 20 69 6e  ster P1 as an in
16df0 74 65 67 65 72 2e 20 20 52 65 70 6c 61 63 65 20  teger.  Replace 
16df1 69 74 0a 2a 2a 20 77 69 74 68 20 69 74 73 20 6f  it.** with its o
16df2 6e 65 73 2d 63 6f 6d 70 6c 65 6d 65 6e 74 2e 20  nes-complement. 
16df3 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73   If the value is
16df4 20 6f 72 69 67 69 6e 61 6c 6c 79 20 4e 55 4c 4c   originally NULL
16df5 2c 20 6c 65 61 76 65 0a 2a 2a 20 69 74 20 75 6e  , leave.** it un
16df6 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 63 61 73 65  changed..*/.case
16df7 20 4f 50 5f 42 69 74 4e 6f 74 3a 20 7b 20 20 20   OP_BitNot: {   
16df8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
16df9 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f 54 2c 20  e as TK_BITNOT, 
16dfa 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 70 49 6e  in1 */.  if( pIn
16dfb 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e  1->flags & MEM_N
16dfc 75 6c 6c 20 29 20 62 72 65 61 6b 3b 20 20 2f 2a  ull ) break;  /*
16dfd 20 44 6f 20 6e 6f 74 68 69 6e 67 20 74 6f 20 4e   Do nothing to N
16dfe 55 4c 4c 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ULLs */.  sqlite
16dff 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69  3VdbeMemIntegeri
16e00 66 79 28 70 49 6e 31 29 3b 0a 20 20 70 49 6e 31  fy(pIn1);.  pIn1
16e01 2d 3e 75 2e 69 20 3d 20 7e 70 49 6e 31 2d 3e 75  ->u.i = ~pIn1->u
16e02 2e 69 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49  .i;.  assert( pI
16e03 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e  n1->flags&MEM_In
16e04 74 20 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  t );.  break;.}.
16e05 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 20 50  ./* Opcode: If P
16e06 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
16e07 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20  * Jump to P2 if 
16e08 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
16e09 69 73 74 65 72 20 50 31 20 69 73 20 74 72 75 65  ister P1 is true
16e0a 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 0a  .  The value is.
16e0b 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  ** is considered
16e0c 20 74 72 75 65 20 69 66 20 69 74 20 69 73 20 6e   true if it is n
16e0d 75 6d 65 72 69 63 20 61 6e 64 20 6e 6f 6e 2d 7a  umeric and non-z
16e0e 65 72 6f 2e 20 20 49 66 20 74 68 65 20 76 61 6c  ero.  If the val
16e0f 75 65 0a 2a 2a 20 69 6e 20 50 31 20 69 73 20 4e  ue.** in P1 is N
16e10 55 4c 4c 20 74 68 65 6e 20 74 61 6b 65 20 74 68  ULL then take th
16e11 65 20 6a 75 6d 70 20 69 66 20 50 33 20 69 73 20  e jump if P3 is 
16e12 74 72 75 65 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f  true..*/./* Opco
16e13 64 65 3a 20 49 66 4e 6f 74 20 50 31 20 50 32 20  de: IfNot P1 P2 
16e14 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d  P3 * *.**.** Jum
16e15 70 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 76  p to P2 if the v
16e16 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
16e17 20 50 31 20 69 73 20 46 61 6c 73 65 2e 20 20 54   P1 is False.  T
16e18 68 65 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 69  he value is.** i
16e19 73 20 63 6f 6e 73 69 64 65 72 65 64 20 74 72 75  s considered tru
16e1a 65 20 69 66 20 69 74 20 68 61 73 20 61 20 6e 75  e if it has a nu
16e1b 6d 65 72 69 63 20 76 61 6c 75 65 20 6f 66 20 7a  meric value of z
16e1c 65 72 6f 2e 20 20 49 66 20 74 68 65 20 76 61 6c  ero.  If the val
16e1d 75 65 0a 2a 2a 20 69 6e 20 50 31 20 69 73 20 4e  ue.** in P1 is N
16e1e 55 4c 4c 20 74 68 65 6e 20 74 61 6b 65 20 74 68  ULL then take th
16e1f 65 20 6a 75 6d 70 20 69 66 20 50 33 20 69 73 20  e jump if P3 is 
16e20 74 72 75 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  true..*/.case OP
16e21 5f 49 66 3a 20 20 20 20 20 20 20 20 20 20 20 20  _If:            
16e22 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
16e23 31 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 4e  1 */.case OP_IfN
16e24 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  ot: {           
16e25 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f   /* jump, in1 */
16e26 0a 20 20 69 6e 74 20 63 3b 0a 20 20 69 66 28 20  .  int c;.  if( 
16e27 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
16e28 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 63 20  M_Null ){.    c 
16e29 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 7d 65 6c  = pOp->p3;.  }el
16e2a 73 65 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  se{.#ifdef SQLIT
16e2b 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
16e2c 50 4f 49 4e 54 0a 20 20 20 20 63 20 3d 20 73 71  POINT.    c = sq
16e2d 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75  lite3VdbeIntValu
16e2e 65 28 70 49 6e 31 29 3b 0a 23 65 6c 73 65 0a 20  e(pIn1);.#else. 
16e2f 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 64     c = sqlite3Vd
16e30 62 65 52 65 61 6c 56 61 6c 75 65 28 70 49 6e 31  beRealValue(pIn1
16e31 29 21 3d 30 2e 30 3b 0a 23 65 6e 64 69 66 0a 20  )!=0.0;.#endif. 
16e32 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f     if( pOp->opco
16e33 64 65 3d 3d 4f 50 5f 49 66 4e 6f 74 20 29 20 63  de==OP_IfNot ) c
16e34 20 3d 20 21 63 3b 0a 20 20 7d 0a 20 20 69 66 28   = !c;.  }.  if(
16e35 20 63 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70   c ){.    pc = p
16e36 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20 20  Op->p2-1;.  }.  
16e37 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
16e38 6f 64 65 3a 20 49 73 4e 75 6c 6c 20 50 31 20 50  ode: IsNull P1 P
16e39 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a  2 P3 * *.**.** J
16e3a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 74 68 65  ump to P2 if the
16e3b 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
16e3c 65 72 20 50 31 20 69 73 20 4e 55 4c 4c 2e 20 20  er P1 is NULL.  
16e3d 49 66 20 50 33 20 69 73 20 67 72 65 61 74 65 72  If P3 is greater
16e3e 0a 2a 2a 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74  .** than zero, t
16e3f 68 65 6e 20 63 68 65 63 6b 20 61 6c 6c 20 76 61  hen check all va
16e40 6c 75 65 73 20 72 65 67 28 50 31 29 2c 20 72 65  lues reg(P1), re
16e41 67 28 50 31 2b 31 29 2c 20 0a 2a 2a 20 72 65 67  g(P1+1), .** reg
16e42 28 50 31 2b 32 29 2c 20 2e 2e 2e 2c 20 72 65 67  (P1+2), ..., reg
16e43 28 50 31 2b 50 33 2d 31 29 2e 0a 2a 2f 0a 63 61  (P1+P3-1)..*/.ca
16e44 73 65 20 4f 50 5f 49 73 4e 75 6c 6c 3a 20 7b 20  se OP_IsNull: { 
16e45 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
16e46 6d 65 20 61 73 20 54 4b 5f 49 53 4e 55 4c 4c 2c  me as TK_ISNULL,
16e47 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20   jump, in1 */.  
16e48 69 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b  int n = pOp->p3;
16e49 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
16e4a 70 33 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 31  p3==0 || pOp->p1
16e4b 3e 30 20 29 3b 0a 20 20 64 6f 7b 0a 20 20 20 20  >0 );.  do{.    
16e4c 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73  if( (pIn1->flags
16e4d 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20   & MEM_Null)!=0 
16e4e 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f  ){.      pc = pO
16e4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20  p->p2 - 1;.     
16e50 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
16e51 20 20 70 49 6e 31 2b 2b 3b 0a 20 20 7d 77 68 69    pIn1++;.  }whi
16e52 6c 65 28 20 2d 2d 6e 20 3e 20 30 20 29 3b 0a 20  le( --n > 0 );. 
16e53 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
16e54 63 6f 64 65 3a 20 4e 6f 74 4e 75 6c 6c 20 50 31  code: NotNull P1
16e55 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
16e56 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 74 68  Jump to P2 if th
16e57 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
16e58 74 65 72 20 50 31 20 69 73 20 6e 6f 74 20 4e 55  ter P1 is not NU
16e59 4c 4c 2e 20 20 0a 2a 2f 0a 63 61 73 65 20 4f 50  LL.  .*/.case OP
16e5a 5f 4e 6f 74 4e 75 6c 6c 3a 20 7b 20 20 20 20 20  _NotNull: {     
16e5b 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
16e5c 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 6a 75  s TK_NOTNULL, ju
16e5d 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28  mp, in1 */.  if(
16e5e 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20   (pIn1->flags & 
16e5f 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a  MEM_Null)==0 ){.
16e60 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32      pc = pOp->p2
16e61 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61   - 1;.  }.  brea
16e62 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
16e63 20 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 2a   SetNumColumns *
16e64 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
16e65 54 68 69 73 20 6f 70 63 6f 64 65 20 73 65 74 73  This opcode sets
16e66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
16e67 6f 6c 75 6d 6e 73 20 66 6f 72 20 74 68 65 20 63  olumns for the c
16e68 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 62 79 20  ursor opened by 
16e69 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67  the.** following
16e6a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20   instruction to 
16e6b 50 32 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 4f 50 5f  P2..**.** An OP_
16e6c 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 69 73  SetNumColumns is
16e6d 20 6f 6e 6c 79 20 75 73 65 66 75 6c 20 69 66 20   only useful if 
16e6e 69 74 20 6f 63 63 75 72 73 20 69 6d 6d 65 64 69  it occurs immedi
16e6f 61 74 65 6c 79 20 62 65 66 6f 72 65 20 0a 2a 2a  ately before .**
16e70 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
16e71 6f 77 69 6e 67 20 6f 70 63 6f 64 65 73 3a 0a 2a  owing opcodes:.*
16e72 2a 0a 2a 2a 20 20 20 20 20 4f 70 65 6e 52 65 61  *.**     OpenRea
16e73 64 0a 2a 2a 20 20 20 20 20 4f 70 65 6e 57 72 69  d.**     OpenWri
16e74 74 65 0a 2a 2a 20 20 20 20 20 4f 70 65 6e 50 73  te.**     OpenPs
16e75 65 75 64 6f 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  eudo.**.** If th
16e76 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f  e OP_Column opco
16e77 64 65 20 69 73 20 74 6f 20 62 65 20 65 78 65 63  de is to be exec
16e78 75 74 65 64 20 6f 6e 20 61 20 63 75 72 73 6f 72  uted on a cursor
16e79 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 6f  , then.** this o
16e7a 70 63 6f 64 65 20 6d 75 73 74 20 62 65 20 70 72  pcode must be pr
16e7b 65 73 65 6e 74 20 69 6d 6d 65 64 69 61 74 65 6c  esent immediatel
16e7c 79 20 62 65 66 6f 72 65 20 74 68 65 20 6f 70 63  y before the opc
16e7d 6f 64 65 20 74 68 61 74 0a 2a 2a 20 6f 70 65 6e  ode that.** open
16e7e 73 20 74 68 65 20 63 75 72 73 6f 72 2e 0a 2a 2f  s the cursor..*/
16e7f 0a 63 61 73 65 20 4f 50 5f 53 65 74 4e 75 6d 43  .case OP_SetNumC
16e80 6f 6c 75 6d 6e 73 3a 20 7b 0a 20 20 62 72 65 61  olumns: {.  brea
16e81 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
16e82 20 43 6f 6c 75 6d 6e 20 50 31 20 50 32 20 50 33   Column P1 P2 P3
16e83 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 74 65   P4 *.**.** Inte
16e84 72 70 72 65 74 20 74 68 65 20 64 61 74 61 20 74  rpret the data t
16e85 68 61 74 20 63 75 72 73 6f 72 20 50 31 20 70 6f  hat cursor P1 po
16e86 69 6e 74 73 20 74 6f 20 61 73 20 61 20 73 74 72  ints to as a str
16e87 75 63 74 75 72 65 20 62 75 69 6c 74 20 75 73 69  ucture built usi
16e88 6e 67 0a 2a 2a 20 74 68 65 20 4d 61 6b 65 52 65  ng.** the MakeRe
16e89 63 6f 72 64 20 69 6e 73 74 72 75 63 74 69 6f 6e  cord instruction
16e8a 2e 20 20 28 53 65 65 20 74 68 65 20 4d 61 6b 65  .  (See the Make
16e8b 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 66 6f  Record opcode fo
16e8c 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20  r additional.** 
16e8d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
16e8e 74 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20  t the format of 
16e8f 74 68 65 20 64 61 74 61 2e 29 20 20 45 78 74 72  the data.)  Extr
16e90 61 63 74 20 74 68 65 20 50 32 2d 74 68 20 63 6f  act the P2-th co
16e91 6c 75 6d 6e 0a 2a 2a 20 66 72 6f 6d 20 74 68 69  lumn.** from thi
16e92 73 20 72 65 63 6f 72 64 2e 20 20 49 66 20 74 68  s record.  If th
16e93 65 72 65 20 61 72 65 20 6c 65 73 73 20 74 68 61  ere are less tha
16e94 74 20 28 50 32 2b 31 29 20 0a 2a 2a 20 76 61 6c  t (P2+1) .** val
16e95 75 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72  ues in the recor
16e96 64 2c 20 65 78 74 72 61 63 74 20 61 20 4e 55 4c  d, extract a NUL
16e97 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c  L..**.** The val
16e98 75 65 20 65 78 74 72 61 63 74 65 64 20 69 73 20  ue extracted is 
16e99 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74  stored in regist
16e9a 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  er P3..**.** If 
16e9b 74 68 65 20 4b 65 79 41 73 44 61 74 61 20 6f 70  the KeyAsData op
16e9c 63 6f 64 65 20 68 61 73 20 70 72 65 76 69 6f 75  code has previou
16e9d 73 6c 79 20 65 78 65 63 75 74 65 64 20 6f 6e 20  sly executed on 
16e9e 74 68 69 73 20 63 75 72 73 6f 72 2c 20 74 68 65  this cursor, the
16e9f 6e 20 74 68 65 0a 2a 2a 20 66 69 65 6c 64 20 6d  n the.** field m
16ea0 69 67 68 74 20 62 65 20 65 78 74 72 61 63 74 65  ight be extracte
16ea1 64 20 66 72 6f 6d 20 74 68 65 20 6b 65 79 20 72  d from the key r
16ea2 61 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 64  ather than the d
16ea3 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ata..**.** If th
16ea4 65 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e  e column contain
16ea5 73 20 66 65 77 65 72 20 74 68 61 6e 20 50 32 20  s fewer than P2 
16ea6 66 69 65 6c 64 73 2c 20 74 68 65 6e 20 65 78 74  fields, then ext
16ea7 72 61 63 74 20 61 20 4e 55 4c 4c 2e 20 20 4f 72  ract a NULL.  Or
16ea8 2c 0a 2a 2a 20 69 66 20 74 68 65 20 50 34 20 61  ,.** if the P4 a
16ea9 72 67 75 6d 65 6e 74 20 69 73 20 61 20 50 34 5f  rgument is a P4_
16eaa 4d 45 4d 20 75 73 65 20 74 68 65 20 76 61 6c 75  MEM use the valu
16eab 65 20 6f 66 20 74 68 65 20 50 34 20 61 72 67 75  e of the P4 argu
16eac 6d 65 6e 74 20 61 73 0a 2a 2a 20 74 68 65 20 72  ment as.** the r
16ead 65 73 75 6c 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f  esult..*/.case O
16eae 50 5f 43 6f 6c 75 6d 6e 3a 20 7b 0a 20 20 75 33  P_Column: {.  u3
16eaf 32 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b 20 20  2 payloadSize;  
16eb0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
16eb1 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72  tes in the recor
16eb2 64 20 2a 2f 0a 20 20 69 6e 74 20 70 31 20 3d 20  d */.  int p1 = 
16eb3 70 4f 70 2d 3e 70 31 3b 20 20 2f 2a 20 50 31 20  pOp->p1;  /* P1 
16eb4 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 70 63  value of the opc
16eb5 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70 32 20  ode */.  int p2 
16eb6 3d 20 70 4f 70 2d 3e 70 32 3b 20 20 2f 2a 20 63  = pOp->p2;  /* c
16eb7 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 74 6f 20  olumn number to 
16eb8 72 65 74 72 69 65 76 65 20 2a 2f 0a 20 20 43 75  retrieve */.  Cu
16eb9 72 73 6f 72 20 2a 70 43 20 3d 20 30 3b 20 20 20  rsor *pC = 0;   
16eba 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72   /* The VDBE cur
16ebb 73 6f 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  sor */.  char *z
16ebc 52 65 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 50  Rec;        /* P
16ebd 6f 69 6e 74 65 72 20 74 6f 20 63 6f 6d 70 6c 65  ointer to comple
16ebe 74 65 20 72 65 63 6f 72 64 2d 64 61 74 61 20 2a  te record-data *
16ebf 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  /.  BtCursor *pC
16ec0 72 73 72 3b 20 20 20 2f 2a 20 54 68 65 20 42 54  rsr;   /* The BT
16ec1 72 65 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20  ree cursor */.  
16ec2 75 33 32 20 2a 61 54 79 70 65 3b 20 20 20 20 20  u32 *aType;     
16ec3 20 20 20 2f 2a 20 61 54 79 70 65 5b 69 5d 20 68     /* aType[i] h
16ec4 6f 6c 64 73 20 74 68 65 20 6e 75 6d 65 72 69 63  olds the numeric
16ec5 20 74 79 70 65 20 6f 66 20 74 68 65 20 69 2d 74   type of the i-t
16ec6 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 75 33  h column */.  u3
16ec7 32 20 2a 61 4f 66 66 73 65 74 3b 20 20 20 20 20  2 *aOffset;     
16ec8 20 2f 2a 20 61 4f 66 66 73 65 74 5b 69 5d 20 69   /* aOffset[i] i
16ec9 73 20 6f 66 66 73 65 74 20 74 6f 20 73 74 61 72  s offset to star
16eca 74 20 6f 66 20 64 61 74 61 20 66 6f 72 20 69 2d  t of data for i-
16ecb 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 75  th column */.  u
16ecc 33 32 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20  32 nField;      
16ecd 20 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 20 66    /* number of f
16ece 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65 63  ields in the rec
16ecf 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e  ord */.  int len
16ed0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
16ed1 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65  he length of the
16ed2 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61 74 61   serialized data
16ed3 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20   for the column 
16ed4 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  */.  int i;     
16ed5 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
16ed6 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 63 68 61  counter */.  cha
16ed7 72 20 2a 7a 44 61 74 61 3b 20 20 20 20 20 20 20  r *zData;       
16ed8 2f 2a 20 50 61 72 74 20 6f 66 20 74 68 65 20 72  /* Part of the r
16ed9 65 63 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f  ecord being deco
16eda 64 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44  ded */.  Mem *pD
16edb 65 73 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 57  est;        /* W
16edc 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 74 68  here to write th
16edd 65 20 65 78 74 72 61 63 74 65 64 20 76 61 6c 75  e extracted valu
16ede 65 20 2a 2f 0a 20 20 4d 65 6d 20 73 4d 65 6d 3b  e */.  Mem sMem;
16edf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
16ee0 20 73 74 6f 72 69 6e 67 20 74 68 65 20 72 65 63   storing the rec
16ee1 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f 64 65  ord being decode
16ee2 64 20 2a 2f 0a 0a 20 20 73 4d 65 6d 2e 66 6c 61  d */..  sMem.fla
16ee3 67 73 20 3d 20 30 3b 0a 20 20 73 4d 65 6d 2e 64  gs = 0;.  sMem.d
16ee4 62 20 3d 20 30 3b 0a 20 20 73 4d 65 6d 2e 7a 4d  b = 0;.  sMem.zM
16ee5 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 61 73 73  alloc = 0;.  ass
16ee6 65 72 74 28 20 70 31 3c 70 2d 3e 6e 43 75 72 73  ert( p1<p->nCurs
16ee7 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  or );.  assert( 
16ee8 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70  pOp->p3>0 && pOp
16ee9 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b  ->p3<=p->nMem );
16eea 0a 20 20 70 44 65 73 74 20 3d 20 26 70 2d 3e 61  .  pDest = &p->a
16eeb 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20  Mem[pOp->p3];.  
16eec 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
16eed 44 65 73 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b  Dest, MEM_Null);
16eee 0a 0a 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63  ..  /* This bloc
16eef 6b 20 73 65 74 73 20 74 68 65 20 76 61 72 69 61  k sets the varia
16ef0 62 6c 65 20 70 61 79 6c 6f 61 64 53 69 7a 65 20  ble payloadSize 
16ef1 74 6f 20 62 65 20 74 68 65 20 74 6f 74 61 6c 20  to be the total 
16ef2 6e 75 6d 62 65 72 20 6f 66 0a 20 20 2a 2a 20 62  number of.  ** b
16ef3 79 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f  ytes in the reco
16ef4 72 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 7a 52  rd..  **.  ** zR
16ef5 65 63 20 69 73 20 73 65 74 20 74 6f 20 62 65 20  ec is set to be 
16ef6 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78  the complete tex
16ef7 74 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20  t of the record 
16ef8 69 66 20 69 74 20 69 73 20 61 76 61 69 6c 61 62  if it is availab
16ef9 6c 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 6f 6d  le..  ** The com
16efa 70 6c 65 74 65 20 72 65 63 6f 72 64 20 74 65 78  plete record tex
16efb 74 20 69 73 20 61 6c 77 61 79 73 20 61 76 61 69  t is always avai
16efc 6c 61 62 6c 65 20 66 6f 72 20 70 73 65 75 64 6f  lable for pseudo
16efd 2d 74 61 62 6c 65 73 0a 20 20 2a 2a 20 49 66 20  -tables.  ** If 
16efe 74 68 65 20 72 65 63 6f 72 64 20 69 73 20 73 74  the record is st
16eff 6f 72 65 64 20 69 6e 20 61 20 63 75 72 73 6f 72  ored in a cursor
16f00 2c 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 72  , the complete r
16f01 65 63 6f 72 64 20 74 65 78 74 0a 20 20 2a 2a 20  ecord text.  ** 
16f02 6d 69 67 68 74 20 62 65 20 61 76 61 69 6c 61 62  might be availab
16f03 6c 65 20 69 6e 20 74 68 65 20 20 70 43 2d 3e 61  le in the  pC->a
16f04 52 6f 77 20 63 61 63 68 65 2e 20 20 4f 72 20 69  Row cache.  Or i
16f05 74 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 2e 0a  t might not be..
16f06 20 20 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61    ** If the data
16f07 20 69 73 20 75 6e 61 76 61 69 6c 61 62 6c 65 2c   is unavailable,
16f08 20 20 7a 52 65 63 20 69 73 20 73 65 74 20 74 6f    zRec is set to
16f09 20 4e 55 4c 4c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a   NULL..  **.  **
16f0a 20 57 65 20 61 6c 73 6f 20 63 6f 6d 70 75 74 65   We also compute
16f0b 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
16f0c 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65  olumns in the re
16f0d 63 6f 72 64 2e 20 20 46 6f 72 20 63 75 72 73 6f  cord.  For curso
16f0e 72 73 2c 0a 20 20 2a 2a 20 74 68 65 20 6e 75 6d  rs,.  ** the num
16f0f 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
16f10 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  s stored in the 
16f11 43 75 72 73 6f 72 2e 6e 46 69 65 6c 64 20 65 6c  Cursor.nField el
16f12 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 70 43  ement..  */.  pC
16f13 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 31 5d 3b   = p->apCsr[p1];
16f14 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d 30  .  assert( pC!=0
16f15 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   );.#ifndef SQLI
16f16 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
16f17 41 42 4c 45 0a 20 20 61 73 73 65 72 74 28 20 70  ABLE.  assert( p
16f18 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 3d 3d  C->pVtabCursor==
16f19 30 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66  0 );.#endif.  if
16f1a 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30  ( pC->pCursor!=0
16f1b 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72   ){.    /* The r
16f1c 65 63 6f 72 64 20 69 73 20 73 74 6f 72 65 64 20  ecord is stored 
16f1d 69 6e 20 61 20 42 2d 54 72 65 65 20 2a 2f 0a 20  in a B-Tree */. 
16f1e 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
16f1f 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28  dbeCursorMoveto(
16f20 70 43 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  pC);.    if( rc 
16f21 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
16f22 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7a  _to_error;.    z
16f23 52 65 63 20 3d 20 30 3b 0a 20 20 20 20 70 43 72  Rec = 0;.    pCr
16f24 73 72 20 3d 20 70 43 2d 3e 70 43 75 72 73 6f 72  sr = pC->pCursor
16f25 3b 0a 20 20 20 20 69 66 28 20 70 43 2d 3e 6e 75  ;.    if( pC->nu
16f26 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 70  llRow ){.      p
16f27 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 30 3b 0a  ayloadSize = 0;.
16f28 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43      }else if( pC
16f29 2d 3e 63 61 63 68 65 53 74 61 74 75 73 3d 3d 70  ->cacheStatus==p
16f2a 2d 3e 63 61 63 68 65 43 74 72 20 29 7b 0a 20 20  ->cacheCtr ){.  
16f2b 20 20 20 20 70 61 79 6c 6f 61 64 53 69 7a 65 20      payloadSize 
16f2c 3d 20 70 43 2d 3e 70 61 79 6c 6f 61 64 53 69 7a  = pC->payloadSiz
16f2d 65 3b 0a 20 20 20 20 20 20 7a 52 65 63 20 3d 20  e;.      zRec = 
16f2e 28 63 68 61 72 2a 29 70 43 2d 3e 61 52 6f 77 3b  (char*)pC->aRow;
16f2f 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70  .    }else if( p
16f30 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20  C->isIndex ){.  
16f31 20 20 20 20 69 36 34 20 70 61 79 6c 6f 61 64 53      i64 payloadS
16f32 69 7a 65 36 34 3b 0a 20 20 20 20 20 20 73 71 6c  ize64;.      sql
16f33 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65  ite3BtreeKeySize
16f34 28 70 43 72 73 72 2c 20 26 70 61 79 6c 6f 61 64  (pCrsr, &payload
16f35 53 69 7a 65 36 34 29 3b 0a 20 20 20 20 20 20 70  Size64);.      p
16f36 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 70 61 79  ayloadSize = pay
16f37 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20 20 20 20  loadSize64;.    
16f38 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
16f39 69 74 65 33 42 74 72 65 65 44 61 74 61 53 69 7a  ite3BtreeDataSiz
16f3a 65 28 70 43 72 73 72 2c 20 26 70 61 79 6c 6f 61  e(pCrsr, &payloa
16f3b 64 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20  dSize);.    }.  
16f3c 20 20 6e 46 69 65 6c 64 20 3d 20 70 43 2d 3e 6e    nField = pC->n
16f3d 46 69 65 6c 64 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Field;.  }else{.
16f3e 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e      assert( pC->
16f3f 70 73 65 75 64 6f 54 61 62 6c 65 20 29 3b 0a 20  pseudoTable );. 
16f40 20 20 20 2f 2a 20 54 68 65 20 72 65 63 6f 72 64     /* The record
16f41 20 69 73 20 74 68 65 20 73 6f 6c 65 20 65 6e 74   is the sole ent
16f42 72 79 20 6f 66 20 61 20 70 73 65 75 64 6f 2d 74  ry of a pseudo-t
16f43 61 62 6c 65 20 2a 2f 0a 20 20 20 20 70 61 79 6c  able */.    payl
16f44 6f 61 64 53 69 7a 65 20 3d 20 70 43 2d 3e 6e 44  oadSize = pC->nD
16f45 61 74 61 3b 0a 20 20 20 20 7a 52 65 63 20 3d 20  ata;.    zRec = 
16f46 70 43 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20 70  pC->pData;.    p
16f47 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  C->cacheStatus =
16f48 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20   CACHE_STALE;.  
16f49 20 20 61 73 73 65 72 74 28 20 70 61 79 6c 6f 61    assert( payloa
16f4a 64 53 69 7a 65 3d 3d 30 20 7c 7c 20 7a 52 65 63  dSize==0 || zRec
16f4b 21 3d 30 20 29 3b 0a 20 20 20 20 6e 46 69 65 6c  !=0 );.    nFiel
16f4c 64 20 3d 20 70 43 2d 3e 6e 46 69 65 6c 64 3b 0a  d = pC->nField;.
16f4d 20 20 20 20 70 43 72 73 72 20 3d 20 30 3b 0a 20      pCrsr = 0;. 
16f4e 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 70 61 79 6c   }..  /* If payl
16f4f 6f 61 64 53 69 7a 65 20 69 73 20 30 2c 20 74 68  oadSize is 0, th
16f50 65 6e 20 6a 75 73 74 20 73 74 6f 72 65 20 61 20  en just store a 
16f51 4e 55 4c 4c 20 2a 2f 0a 20 20 69 66 28 20 70 61  NULL */.  if( pa
16f52 79 6c 6f 61 64 53 69 7a 65 3d 3d 30 20 29 7b 0a  yloadSize==0 ){.
16f53 20 20 20 20 61 73 73 65 72 74 28 20 70 44 65 73      assert( pDes
16f54 74 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c  t->flags&MEM_Nul
16f55 6c 20 29 3b 0a 20 20 20 20 67 6f 74 6f 20 6f 70  l );.    goto op
16f56 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 7d  _column_out;.  }
16f57 0a 20 20 69 66 28 20 70 61 79 6c 6f 61 64 53 69  .  if( payloadSi
16f58 7a 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  ze>db->aLimit[SQ
16f59 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
16f5a 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74  H] ){.    goto t
16f5b 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a 20 20 61  oo_big;.  }..  a
16f5c 73 73 65 72 74 28 20 70 32 3c 6e 46 69 65 6c 64  ssert( p2<nField
16f5d 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 61   );..  /* Read a
16f5e 6e 64 20 70 61 72 73 65 20 74 68 65 20 74 61 62  nd parse the tab
16f5f 6c 65 20 68 65 61 64 65 72 2e 20 20 53 74 6f 72  le header.  Stor
16f60 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66  e the results of
16f61 20 74 68 65 20 70 61 72 73 65 0a 20 20 2a 2a 20   the parse.  ** 
16f62 69 6e 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20  into the record 
16f63 68 65 61 64 65 72 20 63 61 63 68 65 20 66 69 65  header cache fie
16f64 6c 64 73 20 6f 66 20 74 68 65 20 63 75 72 73 6f  lds of the curso
16f65 72 2e 0a 20 20 2a 2f 0a 20 20 61 54 79 70 65 20  r..  */.  aType 
16f66 3d 20 70 43 2d 3e 61 54 79 70 65 3b 0a 20 20 69  = pC->aType;.  i
16f67 66 28 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74  f( pC->cacheStat
16f68 75 73 3d 3d 70 2d 3e 63 61 63 68 65 43 74 72 20  us==p->cacheCtr 
16f69 29 7b 0a 20 20 20 20 61 4f 66 66 73 65 74 20 3d  ){.    aOffset =
16f6a 20 70 43 2d 3e 61 4f 66 66 73 65 74 3b 0a 20 20   pC->aOffset;.  
16f6b 7d 65 6c 73 65 7b 0a 20 20 20 20 75 38 20 2a 7a  }else{.    u8 *z
16f6c 49 64 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 49  Idx;        /* I
16f6d 6e 64 65 78 20 69 6e 74 6f 20 68 65 61 64 65 72  ndex into header
16f6e 20 2a 2f 0a 20 20 20 20 75 38 20 2a 7a 45 6e 64   */.    u8 *zEnd
16f6f 48 64 72 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e  Hdr;     /* Poin
16f70 74 65 72 20 74 6f 20 66 69 72 73 74 20 62 79 74  ter to first byt
16f71 65 20 61 66 74 65 72 20 74 68 65 20 68 65 61 64  e after the head
16f72 65 72 20 2a 2f 0a 20 20 20 20 75 33 32 20 6f 66  er */.    u32 of
16f73 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 4f 66  fset;      /* Of
16f74 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20 64 61  fset into the da
16f75 74 61 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a  ta */.    int sz
16f76 48 64 72 53 7a 3b 20 20 20 20 20 2f 2a 20 53 69  HdrSz;     /* Si
16f77 7a 65 20 6f 66 20 74 68 65 20 68 65 61 64 65 72  ze of the header
16f78 20 73 69 7a 65 20 66 69 65 6c 64 20 61 74 20 73   size field at s
16f79 74 61 72 74 20 6f 66 20 72 65 63 6f 72 64 20 2a  tart of record *
16f7a 2f 0a 20 20 20 20 69 6e 74 20 61 76 61 69 6c 3b  /.    int avail;
16f7b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
16f7c 20 6f 66 20 62 79 74 65 73 20 6f 66 20 61 76 61   of bytes of ava
16f7d 69 6c 61 62 6c 65 20 64 61 74 61 20 2a 2f 0a 0a  ilable data */..
16f7e 20 20 20 20 61 73 73 65 72 74 28 61 54 79 70 65      assert(aType
16f7f 29 3b 0a 20 20 20 20 70 43 2d 3e 61 4f 66 66 73  );.    pC->aOffs
16f80 65 74 20 3d 20 61 4f 66 66 73 65 74 20 3d 20 26  et = aOffset = &
16f81 61 54 79 70 65 5b 6e 46 69 65 6c 64 5d 3b 0a 20  aType[nField];. 
16f82 20 20 20 70 43 2d 3e 70 61 79 6c 6f 61 64 53 69     pC->payloadSi
16f83 7a 65 20 3d 20 70 61 79 6c 6f 61 64 53 69 7a 65  ze = payloadSize
16f84 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 65 53  ;.    pC->cacheS
16f85 74 61 74 75 73 20 3d 20 70 2d 3e 63 61 63 68 65  tatus = p->cache
16f86 43 74 72 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 67  Ctr;..    /* Fig
16f87 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79  ure out how many
16f88 20 62 79 74 65 73 20 61 72 65 20 69 6e 20 74 68   bytes are in th
16f89 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20  e header */.    
16f8a 69 66 28 20 7a 52 65 63 20 29 7b 0a 20 20 20 20  if( zRec ){.    
16f8b 20 20 7a 44 61 74 61 20 3d 20 7a 52 65 63 3b 0a    zData = zRec;.
16f8c 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
16f8d 20 69 66 28 20 70 43 2d 3e 69 73 49 6e 64 65 78   if( pC->isIndex
16f8e 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 44 61 74   ){.        zDat
16f8f 61 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74  a = (char*)sqlit
16f90 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28  e3BtreeKeyFetch(
16f91 70 43 72 73 72 2c 20 26 61 76 61 69 6c 29 3b 0a  pCrsr, &avail);.
16f92 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
16f93 20 20 20 20 20 7a 44 61 74 61 20 3d 20 28 63 68       zData = (ch
16f94 61 72 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65  ar*)sqlite3Btree
16f95 44 61 74 61 46 65 74 63 68 28 70 43 72 73 72 2c  DataFetch(pCrsr,
16f96 20 26 61 76 61 69 6c 29 3b 0a 20 20 20 20 20 20   &avail);.      
16f97 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 4b 65  }.      /* If Ke
16f98 79 46 65 74 63 68 28 29 2f 44 61 74 61 46 65 74  yFetch()/DataFet
16f99 63 68 28 29 20 6d 61 6e 61 67 65 64 20 74 6f 20  ch() managed to 
16f9a 67 65 74 20 74 68 65 20 65 6e 74 69 72 65 20 70  get the entire p
16f9b 61 79 6c 6f 61 64 2c 0a 20 20 20 20 20 20 2a 2a  ayload,.      **
16f9c 20 73 61 76 65 20 74 68 65 20 70 61 79 6c 6f 61   save the payloa
16f9d 64 20 69 6e 20 74 68 65 20 70 43 2d 3e 61 52 6f  d in the pC->aRo
16f9e 77 20 63 61 63 68 65 2e 20 20 54 68 61 74 20 77  w cache.  That w
16f9f 69 6c 6c 20 73 61 76 65 20 75 73 20 66 72 6f 6d  ill save us from
16fa0 0a 20 20 20 20 20 20 2a 2a 20 68 61 76 69 6e 67  .      ** having
16fa1 20 74 6f 20 6d 61 6b 65 20 61 64 64 69 74 69 6f   to make additio
16fa2 6e 61 6c 20 63 61 6c 6c 73 20 74 6f 20 66 65 74  nal calls to fet
16fa3 63 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 70  ch the content p
16fa4 6f 72 74 69 6f 6e 20 6f 66 0a 20 20 20 20 20 20  ortion of.      
16fa5 2a 2a 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20  ** the record.. 
16fa6 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
16fa7 28 20 61 76 61 69 6c 3e 3d 70 61 79 6c 6f 61 64  ( avail>=payload
16fa8 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Size ){.        
16fa9 7a 52 65 63 20 3d 20 7a 44 61 74 61 3b 0a 20 20  zRec = zData;.  
16faa 20 20 20 20 20 20 70 43 2d 3e 61 52 6f 77 20 3d        pC->aRow =
16fab 20 28 75 38 2a 29 7a 44 61 74 61 3b 0a 20 20 20   (u8*)zData;.   
16fac 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
16fad 20 20 70 43 2d 3e 61 52 6f 77 20 3d 20 30 3b 0a    pC->aRow = 0;.
16fae 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
16faf 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
16fb0 6e 67 20 61 73 73 65 72 74 20 69 73 20 74 72 75  ng assert is tru
16fb1 65 20 69 6e 20 61 6c 6c 20 63 61 73 65 73 20 61  e in all cases a
16fb2 63 63 65 70 74 20 77 68 65 6e 0a 20 20 20 20 2a  ccept when.    *
16fb3 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  * the database f
16fb4 69 6c 65 20 68 61 73 20 62 65 65 6e 20 63 6f 72  ile has been cor
16fb5 72 75 70 74 65 64 20 65 78 74 65 72 6e 61 6c 6c  rupted externall
16fb6 79 2e 0a 20 20 20 20 2a 2a 20 20 20 20 61 73 73  y..    **    ass
16fb7 65 72 74 28 20 7a 52 65 63 21 3d 30 20 7c 7c 20  ert( zRec!=0 || 
16fb8 61 76 61 69 6c 3e 3d 70 61 79 6c 6f 61 64 53 69  avail>=payloadSi
16fb9 7a 65 20 7c 7c 20 61 76 61 69 6c 3e 3d 39 20 29  ze || avail>=9 )
16fba 3b 20 2a 2f 0a 20 20 20 20 73 7a 48 64 72 53 7a  ; */.    szHdrSz
16fbb 20 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 28   = getVarint32((
16fbc 75 38 2a 29 7a 44 61 74 61 2c 20 6f 66 66 73 65  u8*)zData, offse
16fbd 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20  t);..    /* The 
16fbe 4b 65 79 46 65 74 63 68 28 29 20 6f 72 20 44 61  KeyFetch() or Da
16fbf 74 61 46 65 74 63 68 28 29 20 61 62 6f 76 65 20  taFetch() above 
16fc0 61 72 65 20 66 61 73 74 20 61 6e 64 20 77 69 6c  are fast and wil
16fc1 6c 20 67 65 74 20 74 68 65 20 65 6e 74 69 72 65  l get the entire
16fc2 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20 68  .    ** record h
16fc3 65 61 64 65 72 20 69 6e 20 6d 6f 73 74 20 63 61  eader in most ca
16fc4 73 65 73 2e 20 20 42 75 74 20 74 68 65 79 20 77  ses.  But they w
16fc5 69 6c 6c 20 66 61 69 6c 20 74 6f 20 67 65 74 20  ill fail to get 
16fc6 74 68 65 20 63 6f 6d 70 6c 65 74 65 0a 20 20 20  the complete.   
16fc7 20 2a 2a 20 72 65 63 6f 72 64 20 68 65 61 64 65   ** record heade
16fc8 72 20 69 66 20 74 68 65 20 72 65 63 6f 72 64 20  r if the record 
16fc9 68 65 61 64 65 72 20 64 6f 65 73 20 6e 6f 74 20  header does not 
16fca 66 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20  fit on a single 
16fcb 70 61 67 65 0a 20 20 20 20 2a 2a 20 69 6e 20 74  page.    ** in t
16fcc 68 65 20 42 2d 54 72 65 65 2e 20 20 57 68 65 6e  he B-Tree.  When
16fcd 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c 20 75   that happens, u
16fce 73 65 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  se sqlite3VdbeMe
16fcf 6d 46 72 6f 6d 42 74 72 65 65 28 29 20 74 6f 0a  mFromBtree() to.
16fd0 20 20 20 20 2a 2a 20 61 63 71 75 69 72 65 20 74      ** acquire t
16fd1 68 65 20 63 6f 6d 70 6c 65 74 65 20 68 65 61 64  he complete head
16fd2 65 72 20 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a  er text..    */.
16fd3 20 20 20 20 69 66 28 20 21 7a 52 65 63 20 26 26      if( !zRec &&
16fd4 20 61 76 61 69 6c 3c 6f 66 66 73 65 74 20 29 7b   avail<offset ){
16fd5 0a 20 20 20 20 20 20 73 4d 65 6d 2e 66 6c 61 67  .      sMem.flag
16fd6 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 4d 65  s = 0;.      sMe
16fd7 6d 2e 64 62 20 3d 20 30 3b 0a 20 20 20 20 20 20  m.db = 0;.      
16fd8 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
16fd9 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43 72  MemFromBtree(pCr
16fda 73 72 2c 20 30 2c 20 6f 66 66 73 65 74 2c 20 70  sr, 0, offset, p
16fdb 43 2d 3e 69 73 49 6e 64 65 78 2c 20 26 73 4d 65  C->isIndex, &sMe
16fdc 6d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  m);.      if( rc
16fdd 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
16fde 20 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63         goto op_c
16fdf 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 20  olumn_out;.     
16fe0 20 7d 0a 20 20 20 20 20 20 7a 44 61 74 61 20 3d   }.      zData =
16fe1 20 73 4d 65 6d 2e 7a 3b 0a 20 20 20 20 7d 0a 20   sMem.z;.    }. 
16fe2 20 20 20 7a 45 6e 64 48 64 72 20 3d 20 28 75 38     zEndHdr = (u8
16fe3 20 2a 29 26 7a 44 61 74 61 5b 6f 66 66 73 65 74   *)&zData[offset
16fe4 5d 3b 0a 20 20 20 20 7a 49 64 78 20 3d 20 28 75  ];.    zIdx = (u
16fe5 38 20 2a 29 26 7a 44 61 74 61 5b 73 7a 48 64 72  8 *)&zData[szHdr
16fe6 53 7a 5d 3b 0a 0a 20 20 20 20 2f 2a 20 53 63 61  Sz];..    /* Sca
16fe7 6e 20 74 68 65 20 68 65 61 64 65 72 20 61 6e 64  n the header and
16fe8 20 75 73 65 20 69 74 20 74 6f 20 66 69 6c 6c 20   use it to fill 
16fe9 69 6e 20 74 68 65 20 61 54 79 70 65 5b 5d 20 61  in the aType[] a
16fea 6e 64 20 61 4f 66 66 73 65 74 5b 5d 0a 20 20 20  nd aOffset[].   
16feb 20 2a 2a 20 61 72 72 61 79 73 2e 20 20 61 54 79   ** arrays.  aTy
16fec 70 65 5b 69 5d 20 77 69 6c 6c 20 63 6f 6e 74 61  pe[i] will conta
16fed 69 6e 20 74 68 65 20 74 79 70 65 20 69 6e 74 65  in the type inte
16fee 67 65 72 20 66 6f 72 20 74 68 65 20 69 2d 74 68  ger for the i-th
16fef 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 61  .    ** column a
16ff0 6e 64 20 61 4f 66 66 73 65 74 5b 69 5d 20 77 69  nd aOffset[i] wi
16ff1 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 6f  ll contain the o
16ff2 66 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20 62  ffset from the b
16ff3 65 67 69 6e 6e 69 6e 67 0a 20 20 20 20 2a 2a 20  eginning.    ** 
16ff4 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f  of the record to
16ff5 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68   the start of th
16ff6 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 69  e data for the i
16ff7 2d 74 68 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a  -th column.    *
16ff8 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  /.    for(i=0; i
16ff9 3c 6e 46 69 65 6c 64 3b 20 69 2b 2b 29 7b 0a 20  <nField; i++){. 
16ffa 20 20 20 20 20 69 66 28 20 7a 49 64 78 3c 7a 45       if( zIdx<zE
16ffb 6e 64 48 64 72 20 29 7b 0a 20 20 20 20 20 20 20  ndHdr ){.       
16ffc 20 61 4f 66 66 73 65 74 5b 69 5d 20 3d 20 6f 66   aOffset[i] = of
16ffd 66 73 65 74 3b 0a 20 20 20 20 20 20 20 20 7a 49  fset;.        zI
16ffe 64 78 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33  dx += getVarint3
16fff 32 28 7a 49 64 78 2c 20 61 54 79 70 65 5b 69 5d  2(zIdx, aType[i]
17000 29 3b 0a 20 20 20 20 20 20 20 20 6f 66 66 73 65  );.        offse
17001 74 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65  t += sqlite3Vdbe
17002 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 61 54  SerialTypeLen(aT
17003 79 70 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 7d  ype[i]);.      }
17004 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a  else{.        /*
17005 20 49 66 20 69 20 69 73 20 6c 65 73 73 20 74 68   If i is less th
17006 61 74 20 6e 46 69 65 6c 64 2c 20 74 68 65 6e 20  at nField, then 
17007 74 68 65 72 65 20 61 72 65 20 6c 65 73 73 20 66  there are less f
17008 69 65 6c 64 73 20 69 6e 20 74 68 69 73 0a 20 20  ields in this.  
17009 20 20 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20        ** record 
1700a 74 68 61 6e 20 53 65 74 4e 75 6d 43 6f 6c 75 6d  than SetNumColum
1700b 6e 73 20 69 6e 64 69 63 61 74 65 64 20 74 68 65  ns indicated the
1700c 72 65 20 61 72 65 20 63 6f 6c 75 6d 6e 73 20 69  re are columns i
1700d 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a  n the.        **
1700e 20 74 61 62 6c 65 2e 20 53 65 74 20 74 68 65 20   table. Set the 
1700f 6f 66 66 73 65 74 20 66 6f 72 20 61 6e 79 20 65  offset for any e
17010 78 74 72 61 20 63 6f 6c 75 6d 6e 73 20 6e 6f 74  xtra columns not
17011 20 70 72 65 73 65 6e 74 20 69 6e 0a 20 20 20 20   present in.    
17012 20 20 20 20 2a 2a 20 74 68 65 20 72 65 63 6f 72      ** the recor
17013 64 20 74 6f 20 30 2e 20 54 68 69 73 20 74 65 6c  d to 0. This tel
17014 6c 73 20 63 6f 64 65 20 62 65 6c 6f 77 20 74 6f  ls code below to
17015 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 0a 20 20   store a NULL.  
17016 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 65 61 64        ** instead
17017 20 6f 66 20 64 65 73 65 72 69 61 6c 69 7a 69 6e   of deserializin
17018 67 20 61 20 76 61 6c 75 65 20 66 72 6f 6d 20 74  g a value from t
17019 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 20 20 20  he record..     
1701a 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 4f     */.        aO
1701b 66 66 73 65 74 5b 69 5d 20 3d 20 30 3b 0a 20 20  ffset[i] = 0;.  
1701c 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1701d 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
1701e 6c 65 61 73 65 28 26 73 4d 65 6d 29 3b 0a 20 20  lease(&sMem);.  
1701f 20 20 73 4d 65 6d 2e 66 6c 61 67 73 20 3d 20 4d    sMem.flags = M
17020 45 4d 5f 4e 75 6c 6c 3b 0a 0a 20 20 20 20 2f 2a  EM_Null;..    /*
17021 20 49 66 20 77 65 20 68 61 76 65 20 72 65 61 64   If we have read
17022 20 6d 6f 72 65 20 68 65 61 64 65 72 20 64 61 74   more header dat
17023 61 20 74 68 61 6e 20 77 61 73 20 63 6f 6e 74 61  a than was conta
17024 69 6e 65 64 20 69 6e 20 74 68 65 20 68 65 61 64  ined in the head
17025 65 72 2c 0a 20 20 20 20 2a 2a 20 6f 72 20 69 66  er,.    ** or if
17026 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
17027 6c 61 73 74 20 66 69 65 6c 64 20 61 70 70 65 61  last field appea
17028 72 73 20 74 6f 20 62 65 20 70 61 73 74 20 74 68  rs to be past th
17029 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 20 20 20  e end of the.   
1702a 20 2a 2a 20 72 65 63 6f 72 64 2c 20 6f 72 20 69   ** record, or i
1702b 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  f the end of the
1702c 20 6c 61 73 74 20 66 69 65 6c 64 20 61 70 70 65   last field appe
1702d 61 72 73 20 74 6f 20 62 65 20 62 65 66 6f 72 65  ars to be before
1702e 20 74 68 65 20 65 6e 64 0a 20 20 20 20 2a 2a 20   the end.    ** 
1702f 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 28 77  of the record (w
17030 68 65 6e 20 61 6c 6c 20 66 69 65 6c 64 73 20 70  hen all fields p
17031 72 65 73 65 6e 74 29 2c 20 74 68 65 6e 20 77 65  resent), then we
17032 20 6d 75 73 74 20 62 65 20 64 65 61 6c 69 6e 67   must be dealing
17033 20 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 61 20   .    ** with a 
17034 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65  corrupt database
17035 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
17036 20 7a 49 64 78 3e 7a 45 6e 64 48 64 72 20 7c 7c   zIdx>zEndHdr ||
17037 20 6f 66 66 73 65 74 3e 70 61 79 6c 6f 61 64 53   offset>payloadS
17038 69 7a 65 20 7c 7c 20 28 7a 49 64 78 3d 3d 7a 45  ize || (zIdx==zE
17039 6e 64 48 64 72 20 26 26 20 6f 66 66 73 65 74 21  ndHdr && offset!
1703a 3d 70 61 79 6c 6f 61 64 53 69 7a 65 29 20 29 7b  =payloadSize) ){
1703b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
1703c 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
1703d 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63  .      goto op_c
1703e 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d  olumn_out;.    }
1703f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 74  .  }..  /* Get t
17040 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 66 6f 72 6d  he column inform
17041 61 74 69 6f 6e 2e 20 49 66 20 61 4f 66 66 73 65  ation. If aOffse
17042 74 5b 70 32 5d 20 69 73 20 6e 6f 6e 2d 7a 65 72  t[p2] is non-zer
17043 6f 2c 20 74 68 65 6e 20 0a 20 20 2a 2a 20 64 65  o, then .  ** de
17044 73 65 72 69 61 6c 69 7a 65 20 74 68 65 20 76 61  serialize the va
17045 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63  lue from the rec
17046 6f 72 64 2e 20 49 66 20 61 4f 66 66 73 65 74 5b  ord. If aOffset[
17047 70 32 5d 20 69 73 20 7a 65 72 6f 2c 0a 20 20 2a  p2] is zero,.  *
17048 2a 20 74 68 65 6e 20 74 68 65 72 65 20 61 72 65  * then there are
17049 20 6e 6f 74 20 65 6e 6f 75 67 68 20 66 69 65 6c   not enough fiel
1704a 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64  ds in the record
1704b 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 0a   to satisfy the.
1704c 20 20 2a 2a 20 72 65 71 75 65 73 74 2e 20 20 49    ** request.  I
1704d 6e 20 74 68 69 73 20 63 61 73 65 2c 20 73 65 74  n this case, set
1704e 20 74 68 65 20 76 61 6c 75 65 20 4e 55 4c 4c 20   the value NULL 
1704f 6f 72 20 74 6f 20 50 34 20 69 66 20 50 34 20 69  or to P4 if P4 i
17050 73 0a 20 20 2a 2a 20 61 20 70 6f 69 6e 74 65 72  s.  ** a pointer
17051 20 74 6f 20 61 20 4d 65 6d 20 6f 62 6a 65 63 74   to a Mem object
17052 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61 4f 66  ..  */.  if( aOf
17053 66 73 65 74 5b 70 32 5d 20 29 7b 0a 20 20 20 20  fset[p2] ){.    
17054 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
17055 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69 66 28  TE_OK );.    if(
17056 20 7a 52 65 63 20 29 7b 0a 20 20 20 20 20 20 69   zRec ){.      i
17057 66 28 20 70 44 65 73 74 2d 3e 66 6c 61 67 73 26  f( pDest->flags&
17058 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20  MEM_Dyn ){.     
17059 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
1705a 72 69 61 6c 47 65 74 28 28 75 38 20 2a 29 26 7a  rialGet((u8 *)&z
1705b 52 65 63 5b 61 4f 66 66 73 65 74 5b 70 32 5d 5d  Rec[aOffset[p2]]
1705c 2c 20 61 54 79 70 65 5b 70 32 5d 2c 20 26 73 4d  , aType[p2], &sM
1705d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 73 4d 65  em);.        sMe
1705e 6d 2e 64 62 20 3d 20 64 62 3b 20 0a 20 20 20 20  m.db = db; .    
1705f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
17060 56 64 62 65 4d 65 6d 43 6f 70 79 28 70 44 65 73  VdbeMemCopy(pDes
17061 74 2c 20 26 73 4d 65 6d 29 3b 0a 20 20 20 20 20  t, &sMem);.     
17062 20 20 20 61 73 73 65 72 74 28 20 21 28 73 4d 65     assert( !(sMe
17063 6d 2e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29  m.flags&MEM_Dyn)
17064 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
17065 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
17066 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
17067 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20  op_column_out;. 
17068 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
17069 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
1706a 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47  lite3VdbeSerialG
1706b 65 74 28 28 75 38 20 2a 29 26 7a 52 65 63 5b 61  et((u8 *)&zRec[a
1706c 4f 66 66 73 65 74 5b 70 32 5d 5d 2c 20 61 54 79  Offset[p2]], aTy
1706d 70 65 5b 70 32 5d 2c 20 70 44 65 73 74 29 3b 0a  pe[p2], pDest);.
1706e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
1706f 65 7b 0a 20 20 20 20 20 20 6c 65 6e 20 3d 20 73  e{.      len = s
17070 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
17071 54 79 70 65 4c 65 6e 28 61 54 79 70 65 5b 70 32  TypeLen(aType[p2
17072 5d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ]);.      sqlite
17073 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 73 4d  3VdbeMemMove(&sM
17074 65 6d 2c 20 70 44 65 73 74 29 3b 0a 20 20 20 20  em, pDest);.    
17075 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
17076 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 70  beMemFromBtree(p
17077 43 72 73 72 2c 20 61 4f 66 66 73 65 74 5b 70 32  Crsr, aOffset[p2
17078 5d 2c 20 6c 65 6e 2c 20 70 43 2d 3e 69 73 49 6e  ], len, pC->isIn
17079 64 65 78 2c 20 26 73 4d 65 6d 29 3b 0a 20 20 20  dex, &sMem);.   
1707a 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1707b 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1707c 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f  goto op_column_o
1707d 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ut;.      }.    
1707e 20 20 7a 44 61 74 61 20 3d 20 73 4d 65 6d 2e 7a    zData = sMem.z
1707f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
17080 64 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38  dbeSerialGet((u8
17081 2a 29 7a 44 61 74 61 2c 20 61 54 79 70 65 5b 70  *)zData, aType[p
17082 32 5d 2c 20 70 44 65 73 74 29 3b 0a 20 20 20 20  2], pDest);.    
17083 7d 0a 20 20 20 20 70 44 65 73 74 2d 3e 65 6e 63  }.    pDest->enc
17084 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 7d   = encoding;.  }
17085 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 70 4f  else{.    if( pO
17086 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4d 45  p->p4type==P4_ME
17087 4d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  M ){.      sqlit
17088 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77  e3VdbeMemShallow
17089 43 6f 70 79 28 70 44 65 73 74 2c 20 70 4f 70 2d  Copy(pDest, pOp-
1708a 3e 70 34 2e 70 4d 65 6d 2c 20 4d 45 4d 5f 53 74  >p4.pMem, MEM_St
1708b 61 74 69 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65  atic);.    }else
1708c 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
1708d 70 44 65 73 74 2d 3e 66 6c 61 67 73 26 4d 45 4d  pDest->flags&MEM
1708e 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 7d 0a 20  _Null );.    }. 
1708f 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 64   }..  /* If we d
17090 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63  ynamically alloc
17091 61 74 65 64 20 73 70 61 63 65 20 74 6f 20 68 6f  ated space to ho
17092 6c 64 20 74 68 65 20 64 61 74 61 20 28 69 6e 20  ld the data (in 
17093 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33  the.  ** sqlite3
17094 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65  VdbeMemFromBtree
17095 28 29 20 63 61 6c 6c 20 61 62 6f 76 65 29 20 74  () call above) t
17096 68 65 6e 20 74 72 61 6e 73 66 65 72 20 63 6f 6e  hen transfer con
17097 74 72 6f 6c 20 6f 66 20 74 68 61 74 0a 20 20 2a  trol of that.  *
17098 2a 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  * dynamically al
17099 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 6f 76  located space ov
1709a 65 72 20 74 6f 20 74 68 65 20 70 44 65 73 74 20  er to the pDest 
1709b 73 74 72 75 63 74 75 72 65 2e 0a 20 20 2a 2a 20  structure..  ** 
1709c 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 61 20  This prevents a 
1709d 6d 65 6d 6f 72 79 20 63 6f 70 79 2e 0a 20 20 2a  memory copy..  *
1709e 2f 0a 20 20 69 66 28 20 73 4d 65 6d 2e 7a 4d 61  /.  if( sMem.zMa
1709f 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 61 73 73 65  lloc ){.    asse
170a0 72 74 28 20 73 4d 65 6d 2e 7a 3d 3d 73 4d 65 6d  rt( sMem.z==sMem
170a1 2e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 20 20  .zMalloc );.    
170a2 61 73 73 65 72 74 28 20 21 28 70 44 65 73 74 2d  assert( !(pDest-
170a3 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e  >flags & MEM_Dyn
170a4 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ) );.    assert(
170a5 20 21 28 70 44 65 73 74 2d 3e 66 6c 61 67 73 20   !(pDest->flags 
170a6 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f  & (MEM_Blob|MEM_
170a7 53 74 72 29 29 20 7c 7c 20 70 44 65 73 74 2d 3e  Str)) || pDest->
170a8 7a 3d 3d 73 4d 65 6d 2e 7a 20 29 3b 0a 20 20 20  z==sMem.z );.   
170a9 20 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 26 3d   pDest->flags &=
170aa 20 7e 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d   ~(MEM_Ephem|MEM
170ab 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20 70 44  _Static);.    pD
170ac 65 73 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  est->flags |= ME
170ad 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 70 44 65 73  M_Term;.    pDes
170ae 74 2d 3e 7a 20 3d 20 73 4d 65 6d 2e 7a 3b 0a 20  t->z = sMem.z;. 
170af 20 20 20 70 44 65 73 74 2d 3e 7a 4d 61 6c 6c 6f     pDest->zMallo
170b0 63 20 3d 20 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63  c = sMem.zMalloc
170b1 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71  ;.  }..  rc = sq
170b2 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65  lite3VdbeMemMake
170b3 57 72 69 74 65 61 62 6c 65 28 70 44 65 73 74 29  Writeable(pDest)
170b4 3b 0a 0a 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74  ;..op_column_out
170b5 3a 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42  :.  UPDATE_MAX_B
170b6 4c 4f 42 53 49 5a 45 28 70 44 65 73 74 29 3b 0a  LOBSIZE(pDest);.
170b7 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
170b8 28 70 4f 70 2d 3e 70 33 2c 20 70 44 65 73 74 29  (pOp->p3, pDest)
170b9 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
170ba 20 4f 70 63 6f 64 65 3a 20 41 66 66 69 6e 69 74   Opcode: Affinit
170bb 79 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a  y P1 P2 * P4 *.*
170bc 2a 0a 2a 2a 20 41 70 70 6c 79 20 61 66 66 69 6e  *.** Apply affin
170bd 69 74 69 65 73 20 74 6f 20 61 20 72 61 6e 67 65  ities to a range
170be 20 6f 66 20 50 32 20 72 65 67 69 73 74 65 72 73   of P2 registers
170bf 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 50   starting with P
170c0 31 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61  1..**.** P4 is a
170c1 20 73 74 72 69 6e 67 20 74 68 61 74 20 69 73 20   string that is 
170c2 50 32 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f  P2 characters lo
170c3 6e 67 2e 20 54 68 65 20 6e 74 68 20 63 68 61 72  ng. The nth char
170c4 61 63 74 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20  acter of the.** 
170c5 73 74 72 69 6e 67 20 69 6e 64 69 63 61 74 65 73  string indicates
170c6 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69   the column affi
170c7 6e 69 74 79 20 74 68 61 74 20 73 68 6f 75 6c 64  nity that should
170c8 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65   be used for the
170c9 20 6e 74 68 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63   nth.** memory c
170ca 65 6c 6c 20 69 6e 20 74 68 65 20 72 61 6e 67 65  ell in the range
170cb 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 66 66  ..*/.case OP_Aff
170cc 69 6e 69 74 79 3a 20 7b 0a 20 20 63 68 61 72 20  inity: {.  char 
170cd 2a 7a 41 66 66 69 6e 69 74 79 20 3d 20 70 4f 70  *zAffinity = pOp
170ce 2d 3e 70 34 2e 7a 3b 0a 20 20 4d 65 6d 20 2a 70  ->p4.z;.  Mem *p
170cf 44 61 74 61 30 20 3d 20 26 70 2d 3e 61 4d 65 6d  Data0 = &p->aMem
170d0 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 4d 65 6d  [pOp->p1];.  Mem
170d1 20 2a 70 4c 61 73 74 20 3d 20 26 70 44 61 74 61   *pLast = &pData
170d2 30 5b 70 4f 70 2d 3e 70 32 2d 31 5d 3b 0a 20 20  0[pOp->p2-1];.  
170d3 4d 65 6d 20 2a 70 52 65 63 3b 0a 0a 20 20 66 6f  Mem *pRec;..  fo
170d4 72 28 70 52 65 63 3d 70 44 61 74 61 30 3b 20 70  r(pRec=pData0; p
170d5 52 65 63 3c 3d 70 4c 61 73 74 3b 20 70 52 65 63  Rec<=pLast; pRec
170d6 2b 2b 29 7b 0a 20 20 20 20 45 78 70 61 6e 64 42  ++){.    ExpandB
170d7 6c 6f 62 28 70 52 65 63 29 3b 0a 20 20 20 20 61  lob(pRec);.    a
170d8 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 52 65  pplyAffinity(pRe
170d9 63 2c 20 7a 41 66 66 69 6e 69 74 79 5b 70 52 65  c, zAffinity[pRe
170da 63 2d 70 44 61 74 61 30 5d 2c 20 65 6e 63 6f 64  c-pData0], encod
170db 69 6e 67 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61  ing);.  }.  brea
170dc 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
170dd 20 4d 61 6b 65 52 65 63 6f 72 64 20 50 31 20 50   MakeRecord P1 P
170de 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20  2 P3 P4 *.**.** 
170df 43 6f 6e 76 65 72 74 20 50 32 20 72 65 67 69 73  Convert P2 regis
170e0 74 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 77  ters beginning w
170e1 69 74 68 20 50 31 20 69 6e 74 6f 20 61 20 73 69  ith P1 into a si
170e2 6e 67 6c 65 20 65 6e 74 72 79 0a 2a 2a 20 73 75  ngle entry.** su
170e3 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20 61  itable for use a
170e4 73 20 61 20 64 61 74 61 20 72 65 63 6f 72 64 20  s a data record 
170e5 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 61  in a database ta
170e6 62 6c 65 20 6f 72 20 61 73 20 61 20 6b 65 79 0a  ble or as a key.
170e7 2a 2a 20 69 6e 20 61 6e 20 69 6e 64 65 78 2e 20  ** in an index. 
170e8 20 54 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20   The details of 
170e9 74 68 65 20 66 6f 72 6d 61 74 20 61 72 65 20 69  the format are i
170ea 72 72 65 6c 61 76 61 6e 74 20 61 73 20 6c 6f 6e  rrelavant as lon
170eb 67 20 61 73 0a 2a 2a 20 74 68 65 20 4f 50 5f 43  g as.** the OP_C
170ec 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 20 63 61 6e  olumn opcode can
170ed 20 64 65 63 6f 64 65 20 74 68 65 20 72 65 63 6f   decode the reco
170ee 72 64 20 6c 61 74 65 72 2e 0a 2a 2a 20 52 65 66  rd later..** Ref
170ef 65 72 20 74 6f 20 73 6f 75 72 63 65 20 63 6f 64  er to source cod
170f0 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 74  e comments for t
170f1 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68  he details of th
170f2 65 20 72 65 63 6f 72 64 0a 2a 2a 20 66 6f 72 6d  e record.** form
170f3 61 74 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 6d 61 79  at..**.** P4 may
170f4 20 62 65 20 61 20 73 74 72 69 6e 67 20 74 68 61   be a string tha
170f5 74 20 69 73 20 50 32 20 63 68 61 72 61 63 74 65  t is P2 characte
170f6 72 73 20 6c 6f 6e 67 2e 20 20 54 68 65 20 6e 74  rs long.  The nt
170f7 68 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 74  h character of t
170f8 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 64  he.** string ind
170f9 69 63 61 74 65 73 20 74 68 65 20 63 6f 6c 75 6d  icates the colum
170fa 6e 20 61 66 66 69 6e 69 74 79 20 74 68 61 74 20  n affinity that 
170fb 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66  should be used f
170fc 6f 72 20 74 68 65 20 6e 74 68 0a 2a 2a 20 66 69  or the nth.** fi
170fd 65 6c 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78  eld of the index
170fe 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20   key..**.** The 
170ff 6d 61 70 70 69 6e 67 20 66 72 6f 6d 20 63 68 61  mapping from cha
17100 72 61 63 74 65 72 20 74 6f 20 61 66 66 69 6e 69  racter to affini
17101 74 79 20 69 73 20 67 69 76 65 6e 20 62 79 20 74  ty is given by t
17102 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 0a 2a  he SQLITE_AFF_.*
17103 2a 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 64  * macros defined
17104 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e   in sqliteInt.h.
17105 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73 20  .**.** If P4 is 
17106 4e 55 4c 4c 20 74 68 65 6e 20 61 6c 6c 20 69 6e  NULL then all in
17107 64 65 78 20 66 69 65 6c 64 73 20 68 61 76 65 20  dex fields have 
17108 74 68 65 20 61 66 66 69 6e 69 74 79 20 4e 4f 4e  the affinity NON
17109 45 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 61  E..*/.case OP_Ma
1710a 6b 65 52 65 63 6f 72 64 3a 20 7b 0a 20 20 2f 2a  keRecord: {.  /*
1710b 20 41 73 73 75 6d 69 6e 67 20 74 68 65 20 72 65   Assuming the re
1710c 63 6f 72 64 20 63 6f 6e 74 61 69 6e 73 20 4e 20  cord contains N 
1710d 66 69 65 6c 64 73 2c 20 74 68 65 20 72 65 63 6f  fields, the reco
1710e 72 64 20 66 6f 72 6d 61 74 20 6c 6f 6f 6b 73 0a  rd format looks.
1710f 20 20 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a    ** like this:.
17110 20 20 2a 2a 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d    **.  ** ------
17111 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
17112 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
17113 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
17114 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
17115 2d 2d 0a 20 20 2a 2a 20 7c 20 68 64 72 2d 73 69  --.  ** | hdr-si
17116 7a 65 20 7c 20 74 79 70 65 20 30 20 7c 20 74 79  ze | type 0 | ty
17117 70 65 20 31 20 7c 20 2e 2e 2e 20 7c 20 74 79 70  pe 1 | ... | typ
17118 65 20 4e 2d 31 20 7c 20 64 61 74 61 30 20 7c 20  e N-1 | data0 | 
17119 2e 2e 2e 20 7c 20 64 61 74 61 20 4e 2d 31 20 7c  ... | data N-1 |
1711a 20 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d   .  ** ---------
1711b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1711c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1711d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1711e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
1711f 20 20 2a 2a 0a 20 20 2a 2a 20 44 61 74 61 28 30    **.  ** Data(0
17120 29 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20  ) is taken from 
17121 72 65 67 69 73 74 65 72 20 50 31 2e 20 20 44 61  register P1.  Da
17122 74 61 28 31 29 20 63 6f 6d 65 73 20 66 72 6f 6d  ta(1) comes from
17123 20 72 65 67 69 73 74 65 72 20 50 31 2b 31 0a 20   register P1+1. 
17124 20 2a 2a 20 61 6e 64 20 73 6f 20 66 72 6f 74 68   ** and so froth
17125 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 61 63 68  ..  **.  ** Each
17126 20 74 79 70 65 20 66 69 65 6c 64 20 69 73 20 61   type field is a
17127 20 76 61 72 69 6e 74 20 72 65 70 72 65 73 65 6e   varint represen
17128 74 69 6e 67 20 74 68 65 20 73 65 72 69 61 6c 20  ting the serial 
17129 74 79 70 65 20 6f 66 20 74 68 65 20 0a 20 20 2a  type of the .  *
1712a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  * corresponding 
1712b 64 61 74 61 20 65 6c 65 6d 65 6e 74 20 28 73 65  data element (se
1712c 65 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  e sqlite3VdbeSer
1712d 69 61 6c 54 79 70 65 28 29 29 2e 20 54 68 65 0a  ialType()). The.
1712e 20 20 2a 2a 20 68 64 72 2d 73 69 7a 65 20 66 69    ** hdr-size fi
1712f 65 6c 64 20 69 73 20 61 6c 73 6f 20 61 20 76 61  eld is also a va
17130 72 69 6e 74 20 77 68 69 63 68 20 69 73 20 74 68  rint which is th
17131 65 20 6f 66 66 73 65 74 20 66 72 6f 6d 20 74 68  e offset from th
17132 65 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 2a 2a  e beginning.  **
17133 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 74   of the record t
17134 6f 20 64 61 74 61 30 2e 0a 20 20 2a 2f 0a 20 20  o data0..  */.  
17135 75 38 20 2a 7a 4e 65 77 52 65 63 6f 72 64 3b 20  u8 *zNewRecord; 
17136 20 20 20 20 20 20 20 2f 2a 20 41 20 62 75 66 66         /* A buff
17137 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 64  er to hold the d
17138 61 74 61 20 66 6f 72 20 74 68 65 20 6e 65 77 20  ata for the new 
17139 72 65 63 6f 72 64 20 2a 2f 0a 20 20 4d 65 6d 20  record */.  Mem 
1713a 2a 70 52 65 63 3b 20 20 20 20 20 20 20 20 20 20  *pRec;          
1713b 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 65     /* The new re
1713c 63 6f 72 64 20 2a 2f 0a 20 20 75 36 34 20 6e 44  cord */.  u64 nD
1713d 61 74 61 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ata = 0;        
1713e 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1713f 74 65 73 20 6f 66 20 64 61 74 61 20 73 70 61 63  tes of data spac
17140 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 64 72 20  e */.  int nHdr 
17141 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  = 0;          /*
17142 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
17143 20 6f 66 20 68 65 61 64 65 72 20 73 70 61 63 65   of header space
17144 20 2a 2f 0a 20 20 75 36 34 20 6e 42 79 74 65 20   */.  u64 nByte 
17145 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  = 0;         /* 
17146 44 61 74 61 20 73 70 61 63 65 20 72 65 71 75 69  Data space requi
17147 72 65 64 20 66 6f 72 20 74 68 69 73 20 72 65 63  red for this rec
17148 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65  ord */.  int nZe
17149 72 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ro = 0;         
1714a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72  /* Number of zer
1714b 6f 20 62 79 74 65 73 20 61 74 20 74 68 65 20 65  o bytes at the e
1714c 6e 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64  nd of the record
1714d 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 69 6e   */.  int nVarin
1714e 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t;           /* 
1714f 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
17150 69 6e 20 61 20 76 61 72 69 6e 74 20 2a 2f 0a 20  in a varint */. 
17151 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65   u32 serial_type
17152 3b 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20  ;       /* Type 
17153 66 69 65 6c 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a  field */.  Mem *
17154 70 44 61 74 61 30 3b 20 20 20 20 20 20 20 20 20  pData0;         
17155 20 20 2f 2a 20 46 69 72 73 74 20 66 69 65 6c 64    /* First field
17156 20 74 6f 20 62 65 20 63 6f 6d 62 69 6e 65 64 20   to be combined 
17157 69 6e 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20  into the record 
17158 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4c 61 73 74 3b  */.  Mem *pLast;
17159 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1715a 61 73 74 20 66 69 65 6c 64 20 6f 66 20 74 68 65  ast field of the
1715b 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74   record */.  int
1715c 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20   nField;        
1715d 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1715e 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72   fields in the r
1715f 65 63 6f 72 64 20 2a 2f 0a 20 20 63 68 61 72 20  ecord */.  char 
17160 2a 7a 41 66 66 69 6e 69 74 79 3b 20 20 20 20 20  *zAffinity;     
17161 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74    /* The affinit
17162 79 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65  y string for the
17163 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74   record */.  int
17164 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 20 20 20   file_format;   
17165 20 20 20 20 2f 2a 20 46 69 6c 65 20 66 6f 72 6d      /* File form
17166 61 74 20 74 6f 20 75 73 65 20 66 6f 72 20 65 6e  at to use for en
17167 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  coding */.  int 
17168 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
17169 20 20 20 2f 2a 20 53 70 61 63 65 20 75 73 65 64     /* Space used
1716a 20 69 6e 20 7a 4e 65 77 52 65 63 6f 72 64 5b 5d   in zNewRecord[]
1716b 20 2a 2f 0a 0a 20 20 6e 46 69 65 6c 64 20 3d 20   */..  nField = 
1716c 70 4f 70 2d 3e 70 31 3b 0a 20 20 7a 41 66 66 69  pOp->p1;.  zAffi
1716d 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a  nity = pOp->p4.z
1716e 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 46 69 65  ;.  assert( nFie
1716f 6c 64 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 3e  ld>0 && pOp->p2>
17170 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 6e 46 69  0 && pOp->p2+nFi
17171 65 6c 64 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  eld<=p->nMem );.
17172 20 20 70 44 61 74 61 30 20 3d 20 26 70 2d 3e 61    pData0 = &p->a
17173 4d 65 6d 5b 6e 46 69 65 6c 64 5d 3b 0a 20 20 6e  Mem[nField];.  n
17174 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 32 3b  Field = pOp->p2;
17175 0a 20 20 70 4c 61 73 74 20 3d 20 26 70 44 61 74  .  pLast = &pDat
17176 61 30 5b 6e 46 69 65 6c 64 2d 31 5d 3b 0a 20 20  a0[nField-1];.  
17177 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 70 2d  file_format = p-
17178 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72  >minWriteFileFor
17179 6d 61 74 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 20  mat;..  /* Loop 
1717a 74 68 72 6f 75 67 68 20 74 68 65 20 65 6c 65 6d  through the elem
1717b 65 6e 74 73 20 74 68 61 74 20 77 69 6c 6c 20 6d  ents that will m
1717c 61 6b 65 20 75 70 20 74 68 65 20 72 65 63 6f 72  ake up the recor
1717d 64 20 74 6f 20 66 69 67 75 72 65 0a 20 20 2a 2a  d to figure.  **
1717e 20 6f 75 74 20 68 6f 77 20 6d 75 63 68 20 73 70   out how much sp
1717f 61 63 65 20 69 73 20 72 65 71 75 69 72 65 64 20  ace is required 
17180 66 6f 72 20 74 68 65 20 6e 65 77 20 72 65 63 6f  for the new reco
17181 72 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70  rd..  */.  for(p
17182 52 65 63 3d 70 44 61 74 61 30 3b 20 70 52 65 63  Rec=pData0; pRec
17183 3c 3d 70 4c 61 73 74 3b 20 70 52 65 63 2b 2b 29  <=pLast; pRec++)
17184 7b 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20  {.    int len;. 
17185 20 20 20 69 66 28 20 7a 41 66 66 69 6e 69 74 79     if( zAffinity
17186 20 29 7b 0a 20 20 20 20 20 20 61 70 70 6c 79 41   ){.      applyA
17187 66 66 69 6e 69 74 79 28 70 52 65 63 2c 20 7a 41  ffinity(pRec, zA
17188 66 66 69 6e 69 74 79 5b 70 52 65 63 2d 70 44 61  ffinity[pRec-pDa
17189 74 61 30 5d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b  ta0], encoding);
1718a 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
1718b 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 5a  Rec->flags&MEM_Z
1718c 65 72 6f 20 26 26 20 70 52 65 63 2d 3e 6e 3e 30  ero && pRec->n>0
1718d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1718e 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c  3VdbeMemExpandBl
1718f 6f 62 28 70 52 65 63 29 3b 0a 20 20 20 20 7d 0a  ob(pRec);.    }.
17190 20 20 20 20 73 65 72 69 61 6c 5f 74 79 70 65 20      serial_type 
17191 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  = sqlite3VdbeSer
17192 69 61 6c 54 79 70 65 28 70 52 65 63 2c 20 66 69  ialType(pRec, fi
17193 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 20 20  le_format);.    
17194 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 56 64 62  len = sqlite3Vdb
17195 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73  eSerialTypeLen(s
17196 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20  erial_type);.   
17197 20 6e 44 61 74 61 20 2b 3d 20 6c 65 6e 3b 0a 20   nData += len;. 
17198 20 20 20 6e 48 64 72 20 2b 3d 20 73 71 6c 69 74     nHdr += sqlit
17199 65 33 56 61 72 69 6e 74 4c 65 6e 28 73 65 72 69  e3VarintLen(seri
1719a 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 69 66  al_type);.    if
1719b 28 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20  ( pRec->flags & 
1719c 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20  MEM_Zero ){.    
1719d 20 20 2f 2a 20 4f 6e 6c 79 20 70 75 72 65 20 7a    /* Only pure z
1719e 65 72 6f 2d 66 69 6c 6c 65 64 20 42 4c 4f 42 73  ero-filled BLOBs
1719f 20 63 61 6e 20 62 65 20 69 6e 70 75 74 20 74 6f   can be input to
171a0 20 74 68 69 73 20 4f 70 63 6f 64 65 2e 0a 20 20   this Opcode..  
171a1 20 20 20 20 2a 2a 20 57 65 20 64 6f 20 6e 6f 74      ** We do not
171a2 20 61 6c 6c 6f 77 20 62 6c 6f 62 73 20 77 69 74   allow blobs wit
171a3 68 20 61 20 70 72 65 66 69 78 20 61 6e 64 20 61  h a prefix and a
171a4 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69   zero-filled tai
171a5 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 6e 5a 65 72  l. */.      nZer
171a6 6f 20 2b 3d 20 70 52 65 63 2d 3e 75 2e 69 3b 0a  o += pRec->u.i;.
171a7 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 65      }else if( le
171a8 6e 20 29 7b 0a 20 20 20 20 20 20 6e 5a 65 72 6f  n ){.      nZero
171a9 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
171aa 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 69 6e  .  /* Add the in
171ab 69 74 69 61 6c 20 68 65 61 64 65 72 20 76 61 72  itial header var
171ac 69 6e 74 20 61 6e 64 20 74 6f 74 61 6c 20 74 68  int and total th
171ad 65 20 73 69 7a 65 20 2a 2f 0a 20 20 6e 48 64 72  e size */.  nHdr
171ae 20 2b 3d 20 6e 56 61 72 69 6e 74 20 3d 20 73 71   += nVarint = sq
171af 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 6e  lite3VarintLen(n
171b0 48 64 72 29 3b 0a 20 20 69 66 28 20 6e 56 61 72  Hdr);.  if( nVar
171b1 69 6e 74 3c 73 71 6c 69 74 65 33 56 61 72 69 6e  int<sqlite3Varin
171b2 74 4c 65 6e 28 6e 48 64 72 29 20 29 7b 0a 20 20  tLen(nHdr) ){.  
171b3 20 20 6e 48 64 72 2b 2b 3b 0a 20 20 7d 0a 20 20    nHdr++;.  }.  
171b4 6e 42 79 74 65 20 3d 20 6e 48 64 72 2b 6e 44 61  nByte = nHdr+nDa
171b5 74 61 2d 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 20  ta-nZero;.  if( 
171b6 6e 42 79 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74  nByte>db->aLimit
171b7 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45  [SQLITE_LIMIT_LE
171b8 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74  NGTH] ){.    got
171b9 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a  o too_big;.  }..
171ba 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74    /* Make sure t
171bb 68 65 20 6f 75 74 70 75 74 20 72 65 67 69 73 74  he output regist
171bc 65 72 20 68 61 73 20 61 20 62 75 66 66 65 72 20  er has a buffer 
171bd 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20  large enough to 
171be 73 74 6f 72 65 20 0a 20 20 2a 2a 20 74 68 65 20  store .  ** the 
171bf 6e 65 77 20 72 65 63 6f 72 64 2e 20 54 68 65 20  new record. The 
171c0 6f 75 74 70 75 74 20 72 65 67 69 73 74 65 72 20  output register 
171c1 28 70 4f 70 2d 3e 70 33 29 20 69 73 20 6e 6f 74  (pOp->p3) is not
171c2 20 61 6c 6c 6f 77 65 64 20 74 6f 0a 20 20 2a 2a   allowed to.  **
171c3 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 69   be one of the i
171c4 6e 70 75 74 20 72 65 67 69 73 74 65 72 73 20 28  nput registers (
171c5 62 65 63 61 75 73 65 20 74 68 65 20 66 6f 6c 6c  because the foll
171c6 6f 77 69 6e 67 20 63 61 6c 6c 20 74 6f 0a 20 20  owing call to.  
171c7 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  ** sqlite3VdbeMe
171c8 6d 47 72 6f 77 28 29 20 63 6f 75 6c 64 20 63 6c  mGrow() could cl
171c9 6f 62 62 65 72 20 74 68 65 20 76 61 6c 75 65 20  obber the value 
171ca 62 65 66 6f 72 65 20 69 74 20 69 73 20 75 73 65  before it is use
171cb 64 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  d)..  */.  asser
171cc 74 28 20 70 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e  t( pOp->p3<pOp->
171cd 70 31 20 7c 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70  p1 || pOp->p3>=p
171ce 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 32 20 29  Op->p1+pOp->p2 )
171cf 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61  ;.  pOut = &p->a
171d0 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20  Mem[pOp->p3];.  
171d1 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d  if( sqlite3VdbeM
171d2 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20 6e 42 79  emGrow(pOut, nBy
171d3 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f  te, 0) ){.    go
171d4 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20  to no_mem;.  }. 
171d5 20 7a 4e 65 77 52 65 63 6f 72 64 20 3d 20 28 75   zNewRecord = (u
171d6 38 20 2a 29 70 4f 75 74 2d 3e 7a 3b 0a 0a 20 20  8 *)pOut->z;..  
171d7 2f 2a 20 57 72 69 74 65 20 74 68 65 20 72 65 63  /* Write the rec
171d8 6f 72 64 20 2a 2f 0a 20 20 69 20 3d 20 70 75 74  ord */.  i = put
171d9 56 61 72 69 6e 74 33 32 28 7a 4e 65 77 52 65 63  Varint32(zNewRec
171da 6f 72 64 2c 20 6e 48 64 72 29 3b 0a 20 20 66 6f  ord, nHdr);.  fo
171db 72 28 70 52 65 63 3d 70 44 61 74 61 30 3b 20 70  r(pRec=pData0; p
171dc 52 65 63 3c 3d 70 4c 61 73 74 3b 20 70 52 65 63  Rec<=pLast; pRec
171dd 2b 2b 29 7b 0a 20 20 20 20 73 65 72 69 61 6c 5f  ++){.    serial_
171de 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 56 64  type = sqlite3Vd
171df 62 65 53 65 72 69 61 6c 54 79 70 65 28 70 52 65  beSerialType(pRe
171e0 63 2c 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b  c, file_format);
171e1 0a 20 20 20 20 69 20 2b 3d 20 70 75 74 56 61 72  .    i += putVar
171e2 69 6e 74 33 32 28 26 7a 4e 65 77 52 65 63 6f 72  int32(&zNewRecor
171e3 64 5b 69 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70  d[i], serial_typ
171e4 65 29 3b 20 20 20 20 20 20 2f 2a 20 73 65 72 69  e);      /* seri
171e5 61 6c 20 74 79 70 65 20 2a 2f 0a 20 20 7d 0a 20  al type */.  }. 
171e6 20 66 6f 72 28 70 52 65 63 3d 70 44 61 74 61 30   for(pRec=pData0
171e7 3b 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b 20 70  ; pRec<=pLast; p
171e8 52 65 63 2b 2b 29 7b 20 20 2f 2a 20 73 65 72 69  Rec++){  /* seri
171e9 61 6c 20 64 61 74 61 20 2a 2f 0a 20 20 20 20 69  al data */.    i
171ea 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53   += sqlite3VdbeS
171eb 65 72 69 61 6c 50 75 74 28 26 7a 4e 65 77 52 65  erialPut(&zNewRe
171ec 63 6f 72 64 5b 69 5d 2c 20 6e 42 79 74 65 2d 69  cord[i], nByte-i
171ed 2c 20 70 52 65 63 2c 20 66 69 6c 65 5f 66 6f 72  , pRec, file_for
171ee 6d 61 74 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65  mat);.  }.  asse
171ef 72 74 28 20 69 3d 3d 6e 42 79 74 65 20 29 3b 0a  rt( i==nByte );.
171f0 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
171f1 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c  p3>0 && pOp->p3<
171f2 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f  =p->nMem );.  pO
171f3 75 74 2d 3e 6e 20 3d 20 6e 42 79 74 65 3b 0a 20  ut->n = nByte;. 
171f4 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d   pOut->flags = M
171f5 45 4d 5f 42 6c 6f 62 20 7c 20 4d 45 4d 5f 44 79  EM_Blob | MEM_Dy
171f6 6e 3b 0a 20 20 70 4f 75 74 2d 3e 78 44 65 6c 20  n;.  pOut->xDel 
171f7 3d 20 30 3b 0a 20 20 69 66 28 20 6e 5a 65 72 6f  = 0;.  if( nZero
171f8 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e   ){.    pOut->u.
171f9 69 20 3d 20 6e 5a 65 72 6f 3b 0a 20 20 20 20 70  i = nZero;.    p
171fa 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  Out->flags |= ME
171fb 4d 5f 5a 65 72 6f 3b 0a 20 20 7d 0a 20 20 70 4f  M_Zero;.  }.  pO
171fc 75 74 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45  ut->enc = SQLITE
171fd 5f 55 54 46 38 3b 20 20 2f 2a 20 49 6e 20 63 61  _UTF8;  /* In ca
171fe 73 65 20 74 68 65 20 62 6c 6f 62 20 69 73 20 65  se the blob is e
171ff 76 65 72 20 63 6f 6e 76 65 72 74 65 64 20 74 6f  ver converted to
17200 20 74 65 78 74 20 2a 2f 0a 20 20 52 45 47 49 53   text */.  REGIS
17201 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
17202 33 2c 20 70 4f 75 74 29 3b 0a 20 20 55 50 44 41  3, pOut);.  UPDA
17203 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28  TE_MAX_BLOBSIZE(
17204 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  pOut);.  break;.
17205 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 74  }../* Opcode: St
17206 61 74 65 6d 65 6e 74 20 50 31 20 2a 20 2a 20 2a  atement P1 * * *
17207 20 2a 0a 2a 2a 0a 2a 2a 20 42 65 67 69 6e 20 61   *.**.** Begin a
17208 6e 20 69 6e 64 69 76 69 64 75 61 6c 20 73 74 61  n individual sta
17209 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
1720a 6f 6e 20 77 68 69 63 68 20 69 73 20 70 61 72 74  on which is part
1720b 20 6f 66 20 61 20 6c 61 72 67 65 72 0a 2a 2a 20   of a larger.** 
1720c 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54 68  transaction.  Th
1720d 69 73 20 69 73 20 6e 65 65 64 65 64 20 73 6f 20  is is needed so 
1720e 74 68 61 74 20 74 68 65 20 73 74 61 74 65 6d 65  that the stateme
1720f 6e 74 0a 2a 2a 20 63 61 6e 20 62 65 20 72 6f 6c  nt.** can be rol
17210 6c 65 64 20 62 61 63 6b 20 61 66 74 65 72 20 61  led back after a
17211 6e 20 65 72 72 6f 72 20 77 69 74 68 6f 75 74 20  n error without 
17212 68 61 76 69 6e 67 20 74 6f 20 72 6f 6c 6c 20 62  having to roll b
17213 61 63 6b 20 74 68 65 0a 2a 2a 20 65 6e 74 69 72  ack the.** entir
17214 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20  e transaction.  
17215 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72  The statement tr
17216 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20 61  ansaction will a
17217 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20  utomatically.** 
17218 63 6f 6d 6d 69 74 20 77 68 65 6e 20 74 68 65 20  commit when the 
17219 56 44 42 45 20 68 61 6c 74 73 2e 0a 2a 2a 0a 2a  VDBE halts..**.*
1721a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  * If the databas
1721b 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
1721c 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 61 75 74  currently in aut
1721d 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 28 74 68  ocommit mode (th
1721e 61 74 20 0a 2a 2a 20 69 73 20 74 6f 20 73 61 79  at .** is to say
1721f 2c 20 69 66 20 69 74 20 69 73 20 69 6e 20 62 65  , if it is in be
17220 74 77 65 65 6e 20 42 45 47 49 4e 20 61 6e 64 20  tween BEGIN and 
17221 43 4f 4d 4d 49 54 29 0a 2a 2a 20 61 6e 64 20 69  COMMIT).** and i
17222 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6f  f there are no o
17223 74 68 65 72 20 61 63 74 69 76 65 20 73 74 61 74  ther active stat
17224 65 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20 73 61  ements on the sa
17225 6d 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63  me database.** c
17226 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 65 6e 20  onnection, then 
17227 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 69  this operation i
17228 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 4e 6f 20 73  s a no-op.  No s
17229 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63  tatement transac
1722a 74 69 6f 6e 0a 2a 2a 20 69 73 20 6e 65 65 64 65  tion.** is neede
1722b 64 20 73 69 6e 63 65 20 61 6e 79 20 65 72 72 6f  d since any erro
1722c 72 20 63 61 6e 20 75 73 65 20 74 68 65 20 6e 6f  r can use the no
1722d 72 6d 61 6c 20 52 4f 4c 4c 42 41 43 4b 20 70 72  rmal ROLLBACK pr
1722e 6f 63 65 73 73 20 74 6f 0a 2a 2a 20 75 6e 64 6f  ocess to.** undo
1722f 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 0a 2a 2a 20   changes..**.** 
17230 49 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74  If a statement t
17231 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74  ransaction is st
17232 61 72 74 65 64 2c 20 74 68 65 6e 20 61 20 73 74  arted, then a st
17233 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20  atement journal 
17234 66 69 6c 65 0a 2a 2a 20 77 69 6c 6c 20 62 65 20  file.** will be 
17235 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 69 6e  allocated and in
17236 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a  itialized..**.**
17237 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69   The statement i
17238 73 20 62 65 67 75 6e 20 6f 6e 20 74 68 65 20 64  s begun on the d
17239 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 69 74  atabase file wit
1723a 68 20 69 6e 64 65 78 20 50 31 2e 20 20 54 68 65  h index P1.  The
1723b 20 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62 61 73   main.** databas
1723c 65 20 66 69 6c 65 20 68 61 73 20 61 6e 20 69 6e  e file has an in
1723d 64 65 78 20 6f 66 20 30 20 61 6e 64 20 74 68 65  dex of 0 and the
1723e 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20 74   file used for t
1723f 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 0a  emporary tables.
17240 2a 2a 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20  ** has an index 
17241 6f 66 20 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  of 1..*/.case OP
17242 5f 53 74 61 74 65 6d 65 6e 74 3a 20 7b 0a 20 20  _Statement: {.  
17243 69 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d  if( db->autoComm
17244 69 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 63 74  it==0 || db->act
17245 69 76 65 56 64 62 65 43 6e 74 3e 31 20 29 7b 0a  iveVdbeCnt>1 ){.
17246 20 20 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d      int i = pOp-
17247 3e 70 31 3b 0a 20 20 20 20 42 74 72 65 65 20 2a  >p1;.    Btree *
17248 70 42 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28  pBt;.    assert(
17249 20 69 3e 3d 30 20 26 26 20 69 3c 64 62 2d 3e 6e   i>=0 && i<db->n
1724a 44 62 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  Db );.    assert
1724b 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74  ( db->aDb[i].pBt
1724c 21 3d 30 20 29 3b 0a 20 20 20 20 70 42 74 20 3d  !=0 );.    pBt =
1724d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b   db->aDb[i].pBt;
1724e 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
1724f 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61  ite3BtreeIsInTra
17250 6e 73 28 70 42 74 29 20 29 3b 0a 20 20 20 20 61  ns(pBt) );.    a
17251 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65  ssert( (p->btree
17252 4d 61 73 6b 20 26 20 28 31 3c 3c 69 29 29 21 3d  Mask & (1<<i))!=
17253 30 20 29 3b 0a 20 20 20 20 69 66 28 20 21 73 71  0 );.    if( !sq
17254 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 53 74  lite3BtreeIsInSt
17255 6d 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20  mt(pBt) ){.     
17256 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
17257 65 65 42 65 67 69 6e 53 74 6d 74 28 70 42 74 29  eeBeginStmt(pBt)
17258 3b 0a 20 20 20 20 20 20 70 2d 3e 6f 70 65 6e 65  ;.      p->opene
17259 64 53 74 61 74 65 6d 65 6e 74 20 3d 20 31 3b 0a  dStatement = 1;.
1725a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61      }.  }.  brea
1725b 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1725c 20 41 75 74 6f 43 6f 6d 6d 69 74 20 50 31 20 50   AutoCommit P1 P
1725d 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 65  2 * * *.**.** Se
1725e 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61  t the database a
1725f 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 20  uto-commit flag 
17260 74 6f 20 50 31 20 28 31 20 6f 72 20 30 29 2e 20  to P1 (1 or 0). 
17261 49 66 20 50 32 20 69 73 20 74 72 75 65 2c 20 72  If P2 is true, r
17262 6f 6c 6c 0a 2a 2a 20 62 61 63 6b 20 61 6e 79 20  oll.** back any 
17263 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76 65  currently active
17264 20 62 74 72 65 65 20 74 72 61 6e 73 61 63 74 69   btree transacti
17265 6f 6e 73 2e 20 49 66 20 74 68 65 72 65 20 61 72  ons. If there ar
17266 65 20 61 6e 79 20 61 63 74 69 76 65 0a 2a 2a 20  e any active.** 
17267 56 4d 73 20 28 61 70 61 72 74 20 66 72 6f 6d 20  VMs (apart from 
17268 74 68 69 73 20 6f 6e 65 29 2c 20 74 68 65 6e 20  this one), then 
17269 74 68 65 20 43 4f 4d 4d 49 54 20 6f 72 20 52 4f  the COMMIT or RO
1726a 4c 4c 42 41 43 4b 20 73 74 61 74 65 6d 65 6e 74  LLBACK statement
1726b 20 66 61 69 6c 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   fails..**.** Th
1726c 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 63  is instruction c
1726d 61 75 73 65 73 20 74 68 65 20 56 4d 20 74 6f 20  auses the VM to 
1726e 68 61 6c 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  halt..*/.case OP
1726f 5f 41 75 74 6f 43 6f 6d 6d 69 74 3a 20 7b 0a 20  _AutoCommit: {. 
17270 20 75 38 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b   u8 i = pOp->p1;
17271 0a 20 20 75 38 20 72 6f 6c 6c 62 61 63 6b 20 3d  .  u8 rollback =
17272 20 70 4f 70 2d 3e 70 32 3b 0a 0a 20 20 61 73 73   pOp->p2;..  ass
17273 65 72 74 28 20 69 3d 3d 31 20 7c 7c 20 69 3d 3d  ert( i==1 || i==
17274 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  0 );.  assert( i
17275 3d 3d 31 20 7c 7c 20 72 6f 6c 6c 62 61 63 6b 3d  ==1 || rollback=
17276 3d 30 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28  =0 );..  assert(
17277 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43   db->activeVdbeC
17278 6e 74 3e 30 20 29 3b 20 20 2f 2a 20 41 74 20 6c  nt>0 );  /* At l
17279 65 61 73 74 20 74 68 69 73 20 6f 6e 65 20 56 4d  east this one VM
1727a 20 69 73 20 61 63 74 69 76 65 20 2a 2f 0a 0a 20   is active */.. 
1727b 20 69 66 28 20 64 62 2d 3e 61 63 74 69 76 65 56   if( db->activeV
1727c 64 62 65 43 6e 74 3e 31 20 26 26 20 69 20 26 26  dbeCnt>1 && i &&
1727d 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74   !db->autoCommit
1727e 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   ){.    /* If th
1727f 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69  is instruction i
17280 6d 70 6c 65 6d 65 6e 74 73 20 61 20 43 4f 4d 4d  mplements a COMM
17281 49 54 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 2c 20  IT or ROLLBACK, 
17282 6f 74 68 65 72 20 56 4d 73 20 61 72 65 0a 20 20  other VMs are.  
17283 20 20 2a 2a 20 73 74 69 6c 6c 20 72 75 6e 6e 69    ** still runni
17284 6e 67 2c 20 61 6e 64 20 61 20 74 72 61 6e 73 61  ng, and a transa
17285 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2c  ction is active,
17286 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   return an error
17287 20 69 6e 64 69 63 61 74 69 6e 67 0a 20 20 20 20   indicating.    
17288 2a 2a 20 74 68 61 74 20 74 68 65 20 6f 74 68 65  ** that the othe
17289 72 20 56 4d 73 20 6d 75 73 74 20 63 6f 6d 70 6c  r VMs must compl
1728a 65 74 65 20 66 69 72 73 74 2e 20 0a 20 20 20 20  ete first. .    
1728b 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  */.    sqlite3Se
1728c 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72  tString(&p->zErr
1728d 4d 73 67 2c 20 22 63 61 6e 6e 6f 74 20 22 2c 20  Msg, "cannot ", 
1728e 72 6f 6c 6c 62 61 63 6b 3f 22 72 6f 6c 6c 62 61  rollback?"rollba
1728f 63 6b 22 3a 22 63 6f 6d 6d 69 74 22 2c 20 0a 20  ck":"commit", . 
17290 20 20 20 20 20 20 20 22 20 74 72 61 6e 73 61 63         " transac
17291 74 69 6f 6e 20 2d 20 53 51 4c 20 73 74 61 74 65  tion - SQL state
17292 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73  ments in progres
17293 73 22 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20  s", (char*)0);. 
17294 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
17295 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 20 69 66  RROR;.  }else if
17296 28 20 69 21 3d 64 62 2d 3e 61 75 74 6f 43 6f 6d  ( i!=db->autoCom
17297 6d 69 74 20 29 7b 0a 20 20 20 20 69 66 28 20 70  mit ){.    if( p
17298 4f 70 2d 3e 70 32 20 29 7b 0a 20 20 20 20 20 20  Op->p2 ){.      
17299 61 73 73 65 72 74 28 20 69 3d 3d 31 20 29 3b 0a  assert( i==1 );.
1729a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c        sqlite3Rol
1729b 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20  lbackAll(db);.  
1729c 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d      db->autoComm
1729d 69 74 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73  it = 1;.    }els
1729e 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e 61 75 74  e{.      db->aut
1729f 6f 43 6f 6d 6d 69 74 20 3d 20 69 3b 0a 20 20 20  oCommit = i;.   
172a0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64     if( sqlite3Vd
172a1 62 65 48 61 6c 74 28 70 29 3d 3d 53 51 4c 49 54  beHalt(p)==SQLIT
172a2 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20  E_BUSY ){.      
172a3 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20    p->pc = pc;.  
172a4 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f        db->autoCo
172a5 6d 6d 69 74 20 3d 20 31 2d 69 3b 0a 20 20 20 20  mmit = 1-i;.    
172a6 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d      p->rc = rc =
172a7 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
172a8 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f        goto vdbe_
172a9 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
172aa 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d      }.    if( p-
172ab 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  >rc==SQLITE_OK )
172ac 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
172ad 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 65  ITE_DONE;.    }e
172ae 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
172af 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
172b0 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 76 64 62    }.    goto vdb
172b1 65 5f 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73  e_return;.  }els
172b2 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  e{.    sqlite3Se
172b3 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72  tString(&p->zErr
172b4 4d 73 67 2c 0a 20 20 20 20 20 20 20 20 28 21 69  Msg,.        (!i
172b5 29 3f 22 63 61 6e 6e 6f 74 20 73 74 61 72 74 20  )?"cannot start 
172b6 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69  a transaction wi
172b7 74 68 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69  thin a transacti
172b8 6f 6e 22 3a 28 0a 20 20 20 20 20 20 20 20 28 72  on":(.        (r
172b9 6f 6c 6c 62 61 63 6b 29 3f 22 63 61 6e 6e 6f 74  ollback)?"cannot
172ba 20 72 6f 6c 6c 62 61 63 6b 20 2d 20 6e 6f 20 74   rollback - no t
172bb 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63  ransaction is ac
172bc 74 69 76 65 22 3a 0a 20 20 20 20 20 20 20 20 20  tive":.         
172bd 20 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f            "canno
172be 74 20 63 6f 6d 6d 69 74 20 2d 20 6e 6f 20 74 72  t commit - no tr
172bf 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74  ansaction is act
172c0 69 76 65 22 29 2c 20 28 63 68 61 72 2a 29 30 29  ive"), (char*)0)
172c1 3b 0a 20 20 20 20 20 20 20 20 20 0a 20 20 20 20  ;.         .    
172c2 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
172c3 52 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  R;.  }.  break;.
172c4 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 72  }../* Opcode: Tr
172c5 61 6e 73 61 63 74 69 6f 6e 20 50 31 20 50 32 20  ansaction P1 P2 
172c6 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 65 67 69  * * *.**.** Begi
172c7 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  n a transaction.
172c8 20 20 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f    The transactio
172c9 6e 20 65 6e 64 73 20 77 68 65 6e 20 61 20 43 6f  n ends when a Co
172ca 6d 6d 69 74 20 6f 72 20 52 6f 6c 6c 62 61 63 6b  mmit or Rollback
172cb 0a 2a 2a 20 6f 70 63 6f 64 65 20 69 73 20 65 6e  .** opcode is en
172cc 63 6f 75 6e 74 65 72 65 64 2e 20 20 44 65 70 65  countered.  Depe
172cd 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 4f 4e 20  nding on the ON 
172ce 43 4f 4e 46 4c 49 43 54 20 73 65 74 74 69 6e 67  CONFLICT setting
172cf 2c 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73 61 63  , the.** transac
172d0 74 69 6f 6e 20 6d 69 67 68 74 20 61 6c 73 6f 20  tion might also 
172d1 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69  be rolled back i
172d2 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20 65 6e  f an error is en
172d3 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a  countered..**.**
172d4 20 50 31 20 69 73 20 74 68 65 20 69 6e 64 65 78   P1 is the index
172d5 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
172d6 20 66 69 6c 65 20 6f 6e 20 77 68 69 63 68 20 74   file on which t
172d7 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  he transaction i
172d8 73 0a 2a 2a 20 73 74 61 72 74 65 64 2e 20 20 49  s.** started.  I
172d9 6e 64 65 78 20 30 20 69 73 20 74 68 65 20 6d 61  ndex 0 is the ma
172da 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
172db 20 61 6e 64 20 69 6e 64 65 78 20 31 20 69 73 20   and index 1 is 
172dc 74 68 65 0a 2a 2a 20 66 69 6c 65 20 75 73 65 64  the.** file used
172dd 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 74   for temporary t
172de 61 62 6c 65 73 2e 20 20 49 6e 64 69 63 65 73 20  ables.  Indices 
172df 6f 66 20 32 20 6f 72 20 6d 6f 72 65 20 61 72 65  of 2 or more are
172e0 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 61 74 74   used for.** att
172e1 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 2e  ached databases.
172e2 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 20 69 73 20  .**.** If P2 is 
172e3 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61  non-zero, then a
172e4 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69   write-transacti
172e5 6f 6e 20 69 73 20 73 74 61 72 74 65 64 2e 20 20  on is started.  
172e6 41 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  A RESERVED lock 
172e7 69 73 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 6f  is.** obtained o
172e8 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
172e9 69 6c 65 20 77 68 65 6e 20 61 20 77 72 69 74 65  ile when a write
172ea 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20  -transaction is 
172eb 73 74 61 72 74 65 64 2e 20 20 4e 6f 0a 2a 2a 20  started.  No.** 
172ec 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 63 61  other process ca
172ed 6e 20 73 74 61 72 74 20 61 6e 6f 74 68 65 72 20  n start another 
172ee 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f  write transactio
172ef 6e 20 77 68 69 6c 65 20 74 68 69 73 20 74 72 61  n while this tra
172f0 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 75  nsaction is.** u
172f1 6e 64 65 72 77 61 79 2e 20 20 53 74 61 72 74 69  nderway.  Starti
172f2 6e 67 20 61 20 77 72 69 74 65 20 74 72 61 6e 73  ng a write trans
172f3 61 63 74 69 6f 6e 20 61 6c 73 6f 20 63 72 65 61  action also crea
172f4 74 65 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a  tes a rollback j
172f5 6f 75 72 6e 61 6c 2e 20 41 0a 2a 2a 20 77 72 69  ournal. A.** wri
172f6 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d  te transaction m
172f7 75 73 74 20 62 65 20 73 74 61 72 74 65 64 20 62  ust be started b
172f8 65 66 6f 72 65 20 61 6e 79 20 63 68 61 6e 67 65  efore any change
172f9 73 20 63 61 6e 20 62 65 20 6d 61 64 65 20 74 6f  s can be made to
172fa 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
172fb 2e 20 20 49 66 20 50 32 20 69 73 20 32 20 6f 72  .  If P2 is 2 or
172fc 20 67 72 65 61 74 65 72 20 74 68 65 6e 20 61 6e   greater then an
172fd 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20   EXCLUSIVE lock 
172fe 69 73 20 61 6c 73 6f 20 6f 62 74 61 69 6e 65 64  is also obtained
172ff 0a 2a 2a 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e  .** on the file.
17300 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 20 69 73 20  .**.** If P2 is 
17301 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 72 65 61  zero, then a rea
17302 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e  d-lock is obtain
17303 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ed on the databa
17304 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 63 61 73 65  se file..*/.case
17305 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 3a   OP_Transaction:
17306 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70   {.  int i = pOp
17307 2d 3e 70 31 3b 0a 20 20 42 74 72 65 65 20 2a 70  ->p1;.  Btree *p
17308 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69  Bt;..  assert( i
17309 3e 3d 30 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62  >=0 && i<db->nDb
1730a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   );.  assert( (p
1730b 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31  ->btreeMask & (1
1730c 3c 3c 69 29 29 21 3d 30 20 29 3b 0a 20 20 70 42  <<i))!=0 );.  pB
1730d 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70  t = db->aDb[i].p
1730e 42 74 3b 0a 0a 20 20 69 66 28 20 70 42 74 20 29  Bt;..  if( pBt )
1730f 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
17310 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e  e3BtreeBeginTran
17311 73 28 70 42 74 2c 20 70 4f 70 2d 3e 70 32 29 3b  s(pBt, pOp->p2);
17312 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
17313 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20  ITE_BUSY ){.    
17314 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20    p->pc = pc;.  
17315 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d      p->rc = rc =
17316 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
17317 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65      goto vdbe_re
17318 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20  turn;.    }.    
17319 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1731a 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f  K && rc!=SQLITE_
1731b 52 45 41 44 4f 4e 4c 59 20 2f 2a 20 26 26 20 72  READONLY /* && r
1731c 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 2a  c!=SQLITE_BUSY *
1731d 2f 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  / ){.      goto 
1731e 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
1731f 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  or;.    }.  }.  
17320 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
17321 6f 64 65 3a 20 52 65 61 64 43 6f 6f 6b 69 65 20  ode: ReadCookie 
17322 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
17323 2a 2a 20 52 65 61 64 20 63 6f 6f 6b 69 65 20 6e  ** Read cookie n
17324 75 6d 62 65 72 20 50 33 20 66 72 6f 6d 20 64 61  umber P3 from da
17325 74 61 62 61 73 65 20 50 31 20 61 6e 64 20 77 72  tabase P1 and wr
17326 69 74 65 20 69 74 20 69 6e 74 6f 20 72 65 67 69  ite it into regi
17327 73 74 65 72 20 50 32 2e 0a 2a 2a 20 50 33 3d 3d  ster P2..** P3==
17328 30 20 69 73 20 74 68 65 20 73 63 68 65 6d 61 20  0 is the schema 
17329 76 65 72 73 69 6f 6e 2e 20 20 50 33 3d 3d 31 20  version.  P3==1 
1732a 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  is the database 
1732b 66 6f 72 6d 61 74 2e 0a 2a 2a 20 50 33 3d 3d 32  format..** P3==2
1732c 20 69 73 20 74 68 65 20 72 65 63 6f 6d 6d 65 6e   is the recommen
1732d 64 65 64 20 70 61 67 65 72 20 63 61 63 68 65 20  ded pager cache 
1732e 73 69 7a 65 2c 20 61 6e 64 20 73 6f 20 66 6f 72  size, and so for
1732f 74 68 2e 20 20 50 31 3d 3d 30 20 69 73 0a 2a 2a  th.  P1==0 is.**
17330 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
17331 73 65 20 66 69 6c 65 20 61 6e 64 20 50 31 3d 3d  se file and P1==
17332 31 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73  1 is the databas
17333 65 20 66 69 6c 65 20 75 73 65 64 20 74 6f 20 73  e file used to s
17334 74 6f 72 65 0a 2a 2a 20 74 65 6d 70 6f 72 61 72  tore.** temporar
17335 79 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  y tables..**.** 
17336 49 66 20 50 31 20 69 73 20 6e 65 67 61 74 69 76  If P1 is negativ
17337 65 2c 20 74 68 65 6e 20 74 68 69 73 20 69 73 20  e, then this is 
17338 61 20 72 65 71 75 65 73 74 20 74 6f 20 72 65 61  a request to rea
17339 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 0a  d the size of a.
1733a 2a 2a 20 64 61 74 61 62 61 73 65 73 20 66 72 65  ** databases fre
1733b 65 2d 6c 69 73 74 2e 20 50 33 20 6d 75 73 74 20  e-list. P3 must 
1733c 62 65 20 73 65 74 20 74 6f 20 31 20 69 6e 20 74  be set to 1 in t
1733d 68 69 73 20 63 61 73 65 2e 20 54 68 65 20 61 63  his case. The ac
1733e 74 75 61 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65  tual.** database
1733f 20 61 63 63 65 73 73 65 64 20 69 73 20 28 28 50   accessed is ((P
17340 31 2b 31 29 2a 2d 31 29 2e 20 46 6f 72 20 65 78  1+1)*-1). For ex
17341 61 6d 70 6c 65 2c 20 61 20 50 31 20 70 61 72 61  ample, a P1 para
17342 6d 65 74 65 72 20 6f 66 20 2d 31 0a 2a 2a 20 63  meter of -1.** c
17343 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 64 61  orresponds to da
17344 74 61 62 61 73 65 20 30 20 28 22 6d 61 69 6e 22  tabase 0 ("main"
17345 29 2c 20 61 20 50 31 20 6f 66 20 2d 32 20 69 73  ), a P1 of -2 is
17346 20 64 61 74 61 62 61 73 65 20 31 20 28 22 74 65   database 1 ("te
17347 6d 70 22 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  mp")..**.** Ther
17348 65 20 6d 75 73 74 20 62 65 20 61 20 72 65 61 64  e must be a read
17349 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74  -lock on the dat
1734a 61 62 61 73 65 20 28 65 69 74 68 65 72 20 61 20  abase (either a 
1734b 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 6d  transaction.** m
1734c 75 73 74 20 62 65 20 73 74 61 72 74 65 64 20 6f  ust be started o
1734d 72 20 74 68 65 72 65 20 6d 75 73 74 20 62 65 20  r there must be 
1734e 61 6e 20 6f 70 65 6e 20 63 75 72 73 6f 72 29 20  an open cursor) 
1734f 62 65 66 6f 72 65 0a 2a 2a 20 65 78 65 63 75 74  before.** execut
17350 69 6e 67 20 74 68 69 73 20 69 6e 73 74 72 75 63  ing this instruc
17351 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  tion..*/.case OP
17352 5f 52 65 61 64 43 6f 6f 6b 69 65 3a 20 7b 20 20  _ReadCookie: {  
17353 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17354 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
17355 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a  */.  int iMeta;.
17356 20 20 69 6e 74 20 69 44 62 20 3d 20 70 4f 70 2d    int iDb = pOp-
17357 3e 70 31 3b 0a 20 20 69 6e 74 20 69 43 6f 6f 6b  >p1;.  int iCook
17358 69 65 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 0a 20  ie = pOp->p3;.. 
17359 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33   assert( pOp->p3
1735a 3c 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 5f  <SQLITE_N_BTREE_
1735b 4d 45 54 41 20 29 3b 0a 20 20 69 66 28 20 69 44  META );.  if( iD
1735c 62 3c 30 20 29 7b 0a 20 20 20 20 69 44 62 20 3d  b<0 ){.    iDb =
1735d 20 28 2d 31 2a 28 69 44 62 2b 31 29 29 3b 0a 20   (-1*(iDb+1));. 
1735e 20 20 20 69 43 6f 6f 6b 69 65 20 2a 3d 20 2d 31     iCookie *= -1
1735f 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
17360 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62  iDb>=0 && iDb<db
17361 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72  ->nDb );.  asser
17362 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  t( db->aDb[iDb].
17363 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  pBt!=0 );.  asse
17364 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73  rt( (p->btreeMas
17365 6b 20 26 20 28 31 3c 3c 69 44 62 29 29 21 3d 30  k & (1<<iDb))!=0
17366 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20 69 6e 64   );.  /* The ind
17367 65 78 69 6e 67 20 6f 66 20 6d 65 74 61 20 76 61  exing of meta va
17368 6c 75 65 73 20 61 74 20 74 68 65 20 73 63 68 65  lues at the sche
17369 6d 61 20 6c 61 79 65 72 20 69 73 20 6f 66 66 20  ma layer is off 
1736a 62 79 20 6f 6e 65 20 66 72 6f 6d 0a 20 20 2a 2a  by one from.  **
1736b 20 74 68 65 20 69 6e 64 65 78 69 6e 67 20 69 6e   the indexing in
1736c 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65 72   the btree layer
1736d 2e 20 20 54 68 65 20 62 74 72 65 65 20 63 6f 6e  .  The btree con
1736e 73 69 64 65 72 73 20 6d 65 74 61 5b 30 5d 20 74  siders meta[0] t
1736f 6f 0a 20 20 2a 2a 20 62 65 20 74 68 65 20 6e 75  o.  ** be the nu
17370 6d 62 65 72 20 6f 66 20 66 72 65 65 20 70 61 67  mber of free pag
17371 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  es in the databa
17372 73 65 20 28 61 20 72 65 61 64 2d 6f 6e 6c 79 20  se (a read-only 
17373 76 61 6c 75 65 29 0a 20 20 2a 2a 20 61 6e 64 20  value).  ** and 
17374 6d 65 74 61 5b 31 5d 20 74 6f 20 62 65 20 74 68  meta[1] to be th
17375 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e  e schema cookie.
17376 20 20 54 68 65 20 73 63 68 65 6d 61 20 6c 61 79    The schema lay
17377 65 72 20 63 6f 6e 73 69 64 65 72 73 0a 20 20 2a  er considers.  *
17378 2a 20 6d 65 74 61 5b 31 5d 20 74 6f 20 62 65 20  * meta[1] to be 
17379 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69  the schema cooki
1737a 65 2e 20 20 53 6f 20 77 65 20 68 61 76 65 20 74  e.  So we have t
1737b 6f 20 73 68 69 66 74 20 74 68 65 20 69 6e 64 65  o shift the inde
1737c 78 0a 20 20 2a 2a 20 62 79 20 6f 6e 65 20 69 6e  x.  ** by one in
1737d 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
1737e 74 61 74 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20  tatement..  */. 
1737f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
17380 65 65 47 65 74 4d 65 74 61 28 64 62 2d 3e 61 44  eeGetMeta(db->aD
17381 62 5b 69 44 62 5d 2e 70 42 74 2c 20 31 20 2b 20  b[iDb].pBt, 1 + 
17382 69 43 6f 6f 6b 69 65 2c 20 28 75 33 32 20 2a 29  iCookie, (u32 *)
17383 26 69 4d 65 74 61 29 3b 0a 20 20 70 4f 75 74 2d  &iMeta);.  pOut-
17384 3e 75 2e 69 20 3d 20 69 4d 65 74 61 3b 0a 20 20  >u.i = iMeta;.  
17385 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
17386 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20  Out, MEM_Int);. 
17387 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
17388 63 6f 64 65 3a 20 53 65 74 43 6f 6f 6b 69 65 20  code: SetCookie 
17389 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
1738a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6e  ** Write the con
1738b 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72  tent of register
1738c 20 50 33 20 28 69 6e 74 65 72 70 72 65 74 65 64   P3 (interpreted
1738d 20 61 73 20 61 6e 20 69 6e 74 65 67 65 72 29 0a   as an integer).
1738e 2a 2a 20 69 6e 74 6f 20 63 6f 6f 6b 69 65 20 6e  ** into cookie n
1738f 75 6d 62 65 72 20 50 32 20 6f 66 20 64 61 74 61  umber P2 of data
17390 62 61 73 65 20 50 31 2e 0a 2a 2a 20 50 32 3d 3d  base P1..** P2==
17391 30 20 69 73 20 74 68 65 20 73 63 68 65 6d 61 20  0 is the schema 
17392 76 65 72 73 69 6f 6e 2e 20 20 50 32 3d 3d 31 20  version.  P2==1 
17393 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  is the database 
17394 66 6f 72 6d 61 74 2e 0a 2a 2a 20 50 32 3d 3d 32  format..** P2==2
17395 20 69 73 20 74 68 65 20 72 65 63 6f 6d 6d 65 6e   is the recommen
17396 64 65 64 20 70 61 67 65 72 20 63 61 63 68 65 20  ded pager cache 
17397 73 69 7a 65 2c 20 61 6e 64 20 73 6f 20 66 6f 72  size, and so for
17398 74 68 2e 20 20 50 31 3d 3d 30 20 69 73 0a 2a 2a  th.  P1==0 is.**
17399 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
1739a 73 65 20 66 69 6c 65 20 61 6e 64 20 50 31 3d 3d  se file and P1==
1739b 31 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73  1 is the databas
1739c 65 20 66 69 6c 65 20 75 73 65 64 20 74 6f 20 73  e file used to s
1739d 74 6f 72 65 0a 2a 2a 20 74 65 6d 70 6f 72 61 72  tore.** temporar
1739e 79 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  y tables..**.** 
1739f 41 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75  A transaction mu
173a0 73 74 20 62 65 20 73 74 61 72 74 65 64 20 62 65  st be started be
173a1 66 6f 72 65 20 65 78 65 63 75 74 69 6e 67 20 74  fore executing t
173a2 68 69 73 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63  his opcode..*/.c
173a3 61 73 65 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65  ase OP_SetCookie
173a4 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 69 6e 33  : {       /* in3
173a5 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20   */.  Db *pDb;. 
173a6 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32   assert( pOp->p2
173a7 3c 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 5f  <SQLITE_N_BTREE_
173a8 4d 45 54 41 20 29 3b 0a 20 20 61 73 73 65 72 74  META );.  assert
173a9 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20  ( pOp->p1>=0 && 
173aa 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20  pOp->p1<db->nDb 
173ab 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d  );.  assert( (p-
173ac 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c  >btreeMask & (1<
173ad 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b  <pOp->p1))!=0 );
173ae 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44  .  pDb = &db->aD
173af 62 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73  b[pOp->p1];.  as
173b0 73 65 72 74 28 20 70 44 62 2d 3e 70 42 74 21 3d  sert( pDb->pBt!=
173b1 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  0 );.  sqlite3Vd
173b2 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28  beMemIntegerify(
173b3 70 49 6e 33 29 3b 0a 20 20 2f 2a 20 53 65 65 20  pIn3);.  /* See 
173b4 6e 6f 74 65 20 61 62 6f 75 74 20 69 6e 64 65 78  note about index
173b5 20 73 68 69 66 74 69 6e 67 20 6f 6e 20 4f 50 5f   shifting on OP_
173b6 52 65 61 64 43 6f 6f 6b 69 65 20 2a 2f 0a 20 20  ReadCookie */.  
173b7 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
173b8 65 55 70 64 61 74 65 4d 65 74 61 28 70 44 62 2d  eUpdateMeta(pDb-
173b9 3e 70 42 74 2c 20 31 2b 70 4f 70 2d 3e 70 32 2c  >pBt, 1+pOp->p2,
173ba 20 28 69 6e 74 29 70 49 6e 33 2d 3e 75 2e 69 29   (int)pIn3->u.i)
173bb 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d  ;.  if( pOp->p2=
173bc 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68 65  =0 ){.    /* Whe
173bd 6e 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f  n the schema coo
173be 6b 69 65 20 63 68 61 6e 67 65 73 2c 20 72 65 63  kie changes, rec
173bf 6f 72 64 20 74 68 65 20 6e 65 77 20 63 6f 6f 6b  ord the new cook
173c0 69 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2a 2f  ie internally */
173c1 0a 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d  .    pDb->pSchem
173c2 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65  a->schema_cookie
173c3 20 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20   = pIn3->u.i;.  
173c4 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53    db->flags |= S
173c5 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e  QLITE_InternChan
173c6 67 65 73 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ges;.  }else if(
173c7 20 70 4f 70 2d 3e 70 32 3d 3d 31 20 29 7b 0a 20   pOp->p2==1 ){. 
173c8 20 20 20 2f 2a 20 52 65 63 6f 72 64 20 63 68 61     /* Record cha
173c9 6e 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65  nges in the file
173ca 20 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 20 20 70   format */.    p
173cb 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c  Db->pSchema->fil
173cc 65 5f 66 6f 72 6d 61 74 20 3d 20 70 49 6e 33 2d  e_format = pIn3-
173cd 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 69 66 28 20  >u.i;.  }.  if( 
173ce 70 4f 70 2d 3e 70 31 3d 3d 31 20 29 7b 0a 20 20  pOp->p1==1 ){.  
173cf 20 20 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20    /* Invalidate 
173d0 61 6c 6c 20 70 72 65 70 61 72 65 64 20 73 74 61  all prepared sta
173d1 74 65 6d 65 6e 74 73 20 77 68 65 6e 65 76 65 72  tements whenever
173d2 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 62 61   the TEMP databa
173d3 73 65 0a 20 20 20 20 2a 2a 20 73 63 68 65 6d 61  se.    ** schema
173d4 20 69 73 20 63 68 61 6e 67 65 64 2e 20 20 54 69   is changed.  Ti
173d5 63 6b 65 74 20 23 31 36 34 34 20 2a 2f 0a 20 20  cket #1644 */.  
173d6 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50    sqlite3ExpireP
173d7 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74  reparedStatement
173d8 73 28 64 62 29 3b 0a 20 20 7d 0a 20 20 62 72 65  s(db);.  }.  bre
173d9 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
173da 3a 20 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 50  : VerifyCookie P
173db 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65  1 P2 *.**.** Che
173dc 63 6b 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ck the value of 
173dd 67 6c 6f 62 61 6c 20 64 61 74 61 62 61 73 65 20  global database 
173de 70 61 72 61 6d 65 74 65 72 20 6e 75 6d 62 65 72  parameter number
173df 20 30 20 28 74 68 65 0a 2a 2a 20 73 63 68 65 6d   0 (the.** schem
173e0 61 20 76 65 72 73 69 6f 6e 29 20 61 6e 64 20 6d  a version) and m
173e1 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 65  ake sure it is e
173e2 71 75 61 6c 20 74 6f 20 50 32 2e 20 20 0a 2a 2a  qual to P2.  .**
173e3 20 50 31 20 69 73 20 74 68 65 20 64 61 74 61 62   P1 is the datab
173e4 61 73 65 20 6e 75 6d 62 65 72 20 77 68 69 63 68  ase number which
173e5 20 69 73 20 30 20 66 6f 72 20 74 68 65 20 6d 61   is 0 for the ma
173e6 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
173e7 0a 2a 2a 20 61 6e 64 20 31 20 66 6f 72 20 74 68  .** and 1 for th
173e8 65 20 66 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74  e file holding t
173e9 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 20  emporary tables 
173ea 61 6e 64 20 73 6f 6d 65 20 68 69 67 68 65 72 20  and some higher 
173eb 6e 75 6d 62 65 72 0a 2a 2a 20 66 6f 72 20 61 75  number.** for au
173ec 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65  xiliary database
173ed 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6f  s..**.** The coo
173ee 6b 69 65 20 63 68 61 6e 67 65 73 20 69 74 73 20  kie changes its 
173ef 76 61 6c 75 65 20 77 68 65 6e 65 76 65 72 20 74  value whenever t
173f0 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
173f1 6d 61 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 20 54  ma changes..** T
173f2 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 69 73  his operation is
173f3 20 75 73 65 64 20 74 6f 20 64 65 74 65 63 74 20   used to detect 
173f4 77 68 65 6e 20 74 68 61 74 20 74 68 65 20 63 6f  when that the co
173f5 6f 6b 69 65 20 68 61 73 20 63 68 61 6e 67 65 64  okie has changed
173f6 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 74 68 65  .** and that the
173f7 20 63 75 72 72 65 6e 74 20 70 72 6f 63 65 73 73   current process
173f8 20 6e 65 65 64 73 20 74 6f 20 72 65 72 65 61 64   needs to reread
173f9 20 74 68 65 20 73 63 68 65 6d 61 2e 0a 2a 2a 0a   the schema..**.
173fa 2a 2a 20 45 69 74 68 65 72 20 61 20 74 72 61 6e  ** Either a tran
173fb 73 61 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f  saction needs to
173fc 20 68 61 76 65 20 62 65 65 6e 20 73 74 61 72 74   have been start
173fd 65 64 20 6f 72 20 61 6e 20 4f 50 5f 4f 70 65 6e  ed or an OP_Open
173fe 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 62 65 20   needs.** to be 
173ff 65 78 65 63 75 74 65 64 20 28 74 6f 20 65 73 74  executed (to est
17400 61 62 6c 69 73 68 20 61 20 72 65 61 64 20 6c 6f  ablish a read lo
17401 63 6b 29 20 62 65 66 6f 72 65 20 74 68 69 73 20  ck) before this 
17402 6f 70 63 6f 64 65 20 69 73 0a 2a 2a 20 69 6e 76  opcode is.** inv
17403 6f 6b 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  oked..*/.case OP
17404 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 3a 20 7b  _VerifyCookie: {
17405 0a 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20  .  int iMeta;.  
17406 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 61 73  Btree *pBt;.  as
17407 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
17408 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e   && pOp->p1<db->
17409 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28  nDb );.  assert(
1740a 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26   (p->btreeMask &
1740b 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d   (1<<pOp->p1))!=
1740c 30 20 29 3b 0a 20 20 70 42 74 20 3d 20 64 62 2d  0 );.  pBt = db-
1740d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42  >aDb[pOp->p1].pB
1740e 74 3b 0a 20 20 69 66 28 20 70 42 74 20 29 7b 0a  t;.  if( pBt ){.
1740f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
17410 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 42 74  BtreeGetMeta(pBt
17411 2c 20 31 2c 20 28 75 33 32 20 2a 29 26 69 4d 65  , 1, (u32 *)&iMe
17412 74 61 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ta);.  }else{.  
17413 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
17414 3b 0a 20 20 20 20 69 4d 65 74 61 20 3d 20 30 3b  ;.    iMeta = 0;
17415 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
17416 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 4d 65 74  QLITE_OK && iMet
17417 61 21 3d 70 4f 70 2d 3e 70 32 20 29 7b 0a 20 20  a!=pOp->p2 ){.  
17418 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
17419 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  ->zErrMsg);.    
1741a 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c  p->zErrMsg = sql
1741b 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
1741c 20 22 64 61 74 61 62 61 73 65 20 73 63 68 65 6d   "database schem
1741d 61 20 68 61 73 20 63 68 61 6e 67 65 64 22 29 3b  a has changed");
1741e 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73  .    /* If the s
1741f 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 20 66 72 6f  chema-cookie fro
17420 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  m the database f
17421 69 6c 65 20 6d 61 74 63 68 65 73 20 74 68 65 20  ile matches the 
17422 63 6f 6f 6b 69 65 20 0a 20 20 20 20 2a 2a 20 73  cookie .    ** s
17423 74 6f 72 65 64 20 77 69 74 68 20 74 68 65 20 69  tored with the i
17424 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65  n-memory represe
17425 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  ntation of the s
17426 63 68 65 6d 61 2c 20 64 6f 0a 20 20 20 20 2a 2a  chema, do.    **
17427 20 6e 6f 74 20 72 65 6c 6f 61 64 20 74 68 65 20   not reload the 
17428 73 63 68 65 6d 61 20 66 72 6f 6d 20 74 68 65 20  schema from the 
17429 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20  database file.. 
1742a 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20     **.    ** If 
1742b 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 73 20 61  virtual-tables a
1742c 72 65 20 69 6e 20 75 73 65 2c 20 74 68 69 73 20  re in use, this 
1742d 69 73 20 6e 6f 74 20 6a 75 73 74 20 61 6e 20 6f  is not just an o
1742e 70 74 69 6d 69 73 61 74 69 6f 6e 2e 0a 20 20 20  ptimisation..   
1742f 20 2a 2a 20 4f 66 74 65 6e 2c 20 76 2d 74 61 62   ** Often, v-tab
17430 6c 65 73 20 73 74 6f 72 65 20 74 68 65 69 72 20  les store their 
17431 64 61 74 61 20 69 6e 20 6f 74 68 65 72 20 53 51  data in other SQ
17432 4c 69 74 65 20 74 61 62 6c 65 73 2c 20 77 68 69  Lite tables, whi
17433 63 68 0a 20 20 20 20 2a 2a 20 61 72 65 20 71 75  ch.    ** are qu
17434 65 72 69 65 64 20 66 72 6f 6d 20 77 69 74 68 69  eried from withi
17435 6e 20 78 4e 65 78 74 28 29 20 61 6e 64 20 6f 74  n xNext() and ot
17436 68 65 72 20 76 2d 74 61 62 6c 65 20 6d 65 74 68  her v-table meth
17437 6f 64 73 20 75 73 69 6e 67 0a 20 20 20 20 2a 2a  ods using.    **
17438 20 70 72 65 70 61 72 65 64 20 71 75 65 72 69 65   prepared querie
17439 73 2e 20 49 66 20 73 75 63 68 20 61 20 71 75 65  s. If such a que
1743a 72 79 20 69 73 20 6f 75 74 2d 6f 66 2d 64 61 74  ry is out-of-dat
1743b 65 2c 20 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e  e, we do not wan
1743c 74 20 74 6f 0a 20 20 20 20 2a 2a 20 64 69 73 63  t to.    ** disc
1743d 61 72 64 20 74 68 65 20 64 61 74 61 62 61 73 65  ard the database
1743e 20 73 63 68 65 6d 61 2c 20 61 73 20 74 68 65 20   schema, as the 
1743f 75 73 65 72 20 63 6f 64 65 20 69 6d 70 6c 65 6d  user code implem
17440 65 6e 74 69 6e 67 20 74 68 65 0a 20 20 20 20 2a  enting the.    *
17441 2a 20 76 2d 74 61 62 6c 65 20 77 6f 75 6c 64 20  * v-table would 
17442 68 61 76 65 20 74 6f 20 62 65 20 72 65 61 64 79  have to be ready
17443 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 33   for the sqlite3
17444 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65 20  _vtab structure 
17445 69 74 73 65 6c 66 0a 20 20 20 20 2a 2a 20 74 6f  itself.    ** to
17446 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65 64 20   be invalidated 
17447 77 68 65 6e 65 76 65 72 20 73 71 6c 69 74 65 33  whenever sqlite3
17448 5f 73 74 65 70 28 29 20 69 73 20 63 61 6c 6c 65  _step() is calle
17449 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 0a 20  d from within . 
1744a 20 20 20 2a 2a 20 61 20 76 2d 74 61 62 6c 65 20     ** a v-table 
1744b 6d 65 74 68 6f 64 2e 0a 20 20 20 20 2a 2f 0a 20  method..    */. 
1744c 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 70     if( db->aDb[p
1744d 4f 70 2d 3e 70 31 5d 2e 70 53 63 68 65 6d 61 2d  Op->p1].pSchema-
1744e 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 21 3d  >schema_cookie!=
1744f 69 4d 65 74 61 20 29 7b 0a 20 20 20 20 20 20 73  iMeta ){.      s
17450 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
17451 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 70 4f  nalSchema(db, pO
17452 70 2d 3e 70 31 29 3b 0a 20 20 20 20 7d 0a 0a 20  p->p1);.    }.. 
17453 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65     sqlite3Expire
17454 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e  PreparedStatemen
17455 74 73 28 64 62 29 3b 0a 20 20 20 20 72 63 20 3d  ts(db);.    rc =
17456 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a   SQLITE_SCHEMA;.
17457 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
17458 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 52  /* Opcode: OpenR
17459 65 61 64 20 50 31 20 50 32 20 50 33 20 50 34 20  ead P1 P2 P3 P4 
1745a 50 35 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20  P5.**.** Open a 
1745b 72 65 61 64 2d 6f 6e 6c 79 20 63 75 72 73 6f 72  read-only cursor
1745c 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73   for the databas
1745d 65 20 74 61 62 6c 65 20 77 68 6f 73 65 20 72 6f  e table whose ro
1745e 6f 74 20 70 61 67 65 20 69 73 0a 2a 2a 20 50 32  ot page is.** P2
1745f 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 66   in a database f
17460 69 6c 65 2e 20 20 54 68 65 20 64 61 74 61 62 61  ile.  The databa
17461 73 65 20 66 69 6c 65 20 69 73 20 64 65 74 65 72  se file is deter
17462 6d 69 6e 65 64 20 62 79 20 50 33 2e 20 0a 2a 2a  mined by P3. .**
17463 20 50 33 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65   P3==0 means the
17464 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20   main database, 
17465 50 33 3d 3d 31 20 6d 65 61 6e 73 20 74 68 65 20  P3==1 means the 
17466 64 61 74 61 62 61 73 65 20 75 73 65 64 20 66 6f  database used fo
17467 72 20 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20  r .** temporary 
17468 74 61 62 6c 65 73 2c 20 61 6e 64 20 50 33 3e 31  tables, and P3>1
17469 20 6d 65 61 6e 73 20 75 73 65 64 20 74 68 65 20   means used the 
1746a 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 61 74  corresponding at
1746b 74 61 63 68 65 64 0a 2a 2a 20 64 61 74 61 62 61  tached.** databa
1746c 73 65 2e 20 20 47 69 76 65 20 74 68 65 20 6e 65  se.  Give the ne
1746d 77 20 63 75 72 73 6f 72 20 61 6e 20 69 64 65 6e  w cursor an iden
1746e 74 69 66 69 65 72 20 6f 66 20 50 31 2e 20 20 54  tifier of P1.  T
1746f 68 65 20 50 31 0a 2a 2a 20 76 61 6c 75 65 73 20  he P1.** values 
17470 6e 65 65 64 20 6e 6f 74 20 62 65 20 63 6f 6e 74  need not be cont
17471 69 67 75 6f 75 73 20 62 75 74 20 61 6c 6c 20 50  iguous but all P
17472 31 20 76 61 6c 75 65 73 20 73 68 6f 75 6c 64 20  1 values should 
17473 62 65 20 73 6d 61 6c 6c 20 69 6e 74 65 67 65 72  be small integer
17474 73 2e 0a 2a 2a 20 49 74 20 69 73 20 61 6e 20 65  s..** It is an e
17475 72 72 6f 72 20 66 6f 72 20 50 31 20 74 6f 20 62  rror for P1 to b
17476 65 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a 2a  e negative..**.*
17477 2a 20 49 66 20 50 35 21 3d 30 20 74 68 65 6e 20  * If P5!=0 then 
17478 75 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  use the content 
17479 6f 66 20 72 65 67 69 73 74 65 72 20 50 32 20 61  of register P2 a
1747a 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 2c  s the root page,
1747b 20 6e 6f 74 0a 2a 2a 20 74 68 65 20 76 61 6c 75   not.** the valu
1747c 65 20 6f 66 20 50 32 20 69 74 73 65 6c 66 2e 0a  e of P2 itself..
1747d 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 77 69 6c 6c  **.** There will
1747e 20 62 65 20 61 20 72 65 61 64 20 6c 6f 63 6b 20   be a read lock 
1747f 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
17480 77 68 65 6e 65 76 65 72 20 74 68 65 72 65 20 69  whenever there i
17481 73 20 61 6e 0a 2a 2a 20 6f 70 65 6e 20 63 75 72  s an.** open cur
17482 73 6f 72 2e 20 20 49 66 20 74 68 65 20 64 61 74  sor.  If the dat
17483 61 62 61 73 65 20 77 61 73 20 75 6e 6c 6f 63 6b  abase was unlock
17484 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68 69 73  ed prior to this
17485 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20   instruction.** 
17486 74 68 65 6e 20 61 20 72 65 61 64 20 6c 6f 63 6b  then a read lock
17487 20 69 73 20 61 63 71 75 69 72 65 64 20 61 73 20   is acquired as 
17488 70 61 72 74 20 6f 66 20 74 68 69 73 20 69 6e 73  part of this ins
17489 74 72 75 63 74 69 6f 6e 2e 20 20 41 20 72 65 61  truction.  A rea
1748a 64 0a 2a 2a 20 6c 6f 63 6b 20 61 6c 6c 6f 77 73  d.** lock allows
1748b 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73   other processes
1748c 20 74 6f 20 72 65 61 64 20 74 68 65 20 64 61 74   to read the dat
1748d 61 62 61 73 65 20 62 75 74 20 70 72 6f 68 69 62  abase but prohib
1748e 69 74 73 0a 2a 2a 20 61 6e 79 20 6f 74 68 65 72  its.** any other
1748f 20 70 72 6f 63 65 73 73 20 66 72 6f 6d 20 6d 6f   process from mo
17490 64 69 66 79 69 6e 67 20 74 68 65 20 64 61 74 61  difying the data
17491 62 61 73 65 2e 20 20 54 68 65 20 72 65 61 64 20  base.  The read 
17492 6c 6f 63 6b 20 69 73 0a 2a 2a 20 72 65 6c 65 61  lock is.** relea
17493 73 65 64 20 77 68 65 6e 20 61 6c 6c 20 63 75 72  sed when all cur
17494 73 6f 72 73 20 61 72 65 20 63 6c 6f 73 65 64 2e  sors are closed.
17495 20 20 49 66 20 74 68 69 73 20 69 6e 73 74 72 75    If this instru
17496 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 0a 2a  ction attempts.*
17497 2a 20 74 6f 20 67 65 74 20 61 20 72 65 61 64 20  * to get a read 
17498 6c 6f 63 6b 20 62 75 74 20 66 61 69 6c 73 2c 20  lock but fails, 
17499 74 68 65 20 73 63 72 69 70 74 20 74 65 72 6d 69  the script termi
1749a 6e 61 74 65 73 20 77 69 74 68 20 61 6e 0a 2a 2a  nates with an.**
1749b 20 53 51 4c 49 54 45 5f 42 55 53 59 20 65 72 72   SQLITE_BUSY err
1749c 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54  or code..**.** T
1749d 68 65 20 50 34 20 76 61 6c 75 65 20 69 73 20 61  he P4 value is a
1749e 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b 65   pointer to a Ke
1749f 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
174a0 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 65  that defines the
174a1 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20  .** content and 
174a2 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
174a3 63 65 20 6f 66 20 69 6e 64 69 63 65 73 2e 20 20  ce of indices.  
174a4 50 34 20 69 73 20 4e 55 4c 4c 20 66 6f 72 20 63  P4 is NULL for c
174a5 75 72 73 6f 72 73 0a 2a 2a 20 74 68 61 74 20 61  ursors.** that a
174a6 72 65 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20  re not pointing 
174a7 74 6f 20 69 6e 64 69 63 65 73 2e 0a 2a 2a 0a 2a  to indices..**.*
174a8 2a 20 53 65 65 20 61 6c 73 6f 20 4f 70 65 6e 57  * See also OpenW
174a9 72 69 74 65 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f  rite..*/./* Opco
174aa 64 65 3a 20 4f 70 65 6e 57 72 69 74 65 20 50 31  de: OpenWrite P1
174ab 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a   P2 P3 P4 P5.**.
174ac 2a 2a 20 4f 70 65 6e 20 61 20 72 65 61 64 2f 77  ** Open a read/w
174ad 72 69 74 65 20 63 75 72 73 6f 72 20 6e 61 6d 65  rite cursor name
174ae 64 20 50 31 20 6f 6e 20 74 68 65 20 74 61 62 6c  d P1 on the tabl
174af 65 20 6f 72 20 69 6e 64 65 78 20 77 68 6f 73 65  e or index whose
174b0 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73   root.** page is
174b1 20 50 32 2e 20 20 4f 72 20 69 66 20 50 35 21 3d   P2.  Or if P5!=
174b2 30 20 75 73 65 20 74 68 65 20 63 6f 6e 74 65 6e  0 use the conten
174b3 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 32  t of register P2
174b4 20 74 6f 20 66 69 6e 64 20 74 68 65 0a 2a 2a 20   to find the.** 
174b5 72 6f 6f 74 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a  root page..**.**
174b6 20 54 68 65 20 50 34 20 76 61 6c 75 65 20 69 73   The P4 value is
174b7 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
174b8 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
174b9 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74  e that defines t
174ba 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 61 6e  he.** content an
174bb 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  d collating sequ
174bc 65 6e 63 65 20 6f 66 20 69 6e 64 69 63 65 73 2e  ence of indices.
174bd 20 20 50 34 20 69 73 20 4e 55 4c 4c 20 66 6f 72    P4 is NULL for
174be 20 63 75 72 73 6f 72 73 0a 2a 2a 20 74 68 61 74   cursors.** that
174bf 20 61 72 65 20 6e 6f 74 20 70 6f 69 6e 74 69 6e   are not pointin
174c0 67 20 74 6f 20 69 6e 64 69 63 65 73 2e 0a 2a 2a  g to indices..**
174c1 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63  .** This instruc
174c2 74 69 6f 6e 20 77 6f 72 6b 73 20 6a 75 73 74 20  tion works just 
174c3 6c 69 6b 65 20 4f 70 65 6e 52 65 61 64 20 65 78  like OpenRead ex
174c4 63 65 70 74 20 74 68 61 74 20 69 74 20 6f 70 65  cept that it ope
174c5 6e 73 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a  ns the cursor.**
174c6 20 69 6e 20 72 65 61 64 2f 77 72 69 74 65 20 6d   in read/write m
174c7 6f 64 65 2e 20 20 46 6f 72 20 61 20 67 69 76 65  ode.  For a give
174c8 6e 20 74 61 62 6c 65 2c 20 74 68 65 72 65 20 63  n table, there c
174c9 61 6e 20 62 65 20 6f 6e 65 20 6f 72 20 6d 6f 72  an be one or mor
174ca 65 20 72 65 61 64 2d 6f 6e 6c 79 0a 2a 2a 20 63  e read-only.** c
174cb 75 72 73 6f 72 73 20 6f 72 20 61 20 73 69 6e 67  ursors or a sing
174cc 6c 65 20 72 65 61 64 2f 77 72 69 74 65 20 63 75  le read/write cu
174cd 72 73 6f 72 20 62 75 74 20 6e 6f 74 20 62 6f 74  rsor but not bot
174ce 68 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  h..**.** See als
174cf 6f 20 4f 70 65 6e 52 65 61 64 2e 0a 2a 2f 0a 63  o OpenRead..*/.c
174d0 61 73 65 20 4f 50 5f 4f 70 65 6e 52 65 61 64 3a  ase OP_OpenRead:
174d1 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e 57 72 69  .case OP_OpenWri
174d2 74 65 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20  te: {.  int i = 
174d3 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 6e 74 20 70  pOp->p1;.  int p
174d4 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 69  2 = pOp->p2;.  i
174d5 6e 74 20 69 44 62 20 3d 20 70 4f 70 2d 3e 70 33  nt iDb = pOp->p3
174d6 3b 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 3b 0a  ;.  int wrFlag;.
174d7 20 20 42 74 72 65 65 20 2a 70 58 3b 0a 20 20 43    Btree *pX;.  C
174d8 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 44  ursor *pCur;.  D
174d9 62 20 2a 70 44 62 3b 0a 20 20 0a 20 20 61 73 73  b *pDb;.  .  ass
174da 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69  ert( iDb>=0 && i
174db 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  Db<db->nDb );.  
174dc 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65  assert( (p->btre
174dd 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 44 62 29  eMask & (1<<iDb)
174de 29 21 3d 30 20 29 3b 0a 20 20 70 44 62 20 3d 20  )!=0 );.  pDb = 
174df 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20  &db->aDb[iDb];. 
174e0 20 70 58 20 3d 20 70 44 62 2d 3e 70 42 74 3b 0a   pX = pDb->pBt;.
174e1 20 20 61 73 73 65 72 74 28 20 70 58 21 3d 30 20    assert( pX!=0 
174e2 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70  );.  if( pOp->op
174e3 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69  code==OP_OpenWri
174e4 74 65 20 29 7b 0a 20 20 20 20 77 72 46 6c 61 67  te ){.    wrFlag
174e5 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20 70 44   = 1;.    if( pD
174e6 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65  b->pSchema->file
174e7 5f 66 6f 72 6d 61 74 20 3c 20 70 2d 3e 6d 69 6e  _format < p->min
174e8 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 20  WriteFileFormat 
174e9 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6d 69 6e 57  ){.      p->minW
174ea 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 20 3d  riteFileFormat =
174eb 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66   pDb->pSchema->f
174ec 69 6c 65 5f 66 6f 72 6d 61 74 3b 0a 20 20 20 20  ile_format;.    
174ed 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77  }.  }else{.    w
174ee 72 46 6c 61 67 20 3d 20 30 3b 0a 20 20 7d 0a 20  rFlag = 0;.  }. 
174ef 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 7b 0a   if( pOp->p5 ){.
174f0 20 20 20 20 61 73 73 65 72 74 28 20 70 32 3e 30      assert( p2>0
174f1 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
174f2 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20  p2<=p->nMem );. 
174f3 20 20 20 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d     pIn2 = &p->aM
174f4 65 6d 5b 70 32 5d 3b 0a 20 20 20 20 73 71 6c 69  em[p2];.    sqli
174f5 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65  te3VdbeMemIntege
174f6 72 69 66 79 28 70 49 6e 32 29 3b 0a 20 20 20 20  rify(pIn2);.    
174f7 70 32 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b 0a  p2 = pIn2->u.i;.
174f8 20 20 20 20 61 73 73 65 72 74 28 20 70 32 3e 3d      assert( p2>=
174f9 32 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  2 );.  }.  asser
174fa 74 28 20 69 3e 3d 30 20 29 3b 0a 20 20 70 43 75  t( i>=0 );.  pCu
174fb 72 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73  r = allocateCurs
174fc 6f 72 28 70 2c 20 69 2c 20 26 70 4f 70 5b 2d 31  or(p, i, &pOp[-1
174fd 5d 2c 20 69 44 62 2c 20 31 29 3b 0a 20 20 69 66  ], iDb, 1);.  if
174fe 28 20 70 43 75 72 3d 3d 30 20 29 20 67 6f 74 6f  ( pCur==0 ) goto
174ff 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 70 43 75 72 2d   no_mem;.  pCur-
17500 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20  >nullRow = 1;.  
17501 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
17502 65 43 75 72 73 6f 72 28 70 58 2c 20 70 32 2c 20  eCursor(pX, p2, 
17503 77 72 46 6c 61 67 2c 20 70 4f 70 2d 3e 70 34 2e  wrFlag, pOp->p4.
17504 70 2c 20 70 43 75 72 2d 3e 70 43 75 72 73 6f 72  p, pCur->pCursor
17505 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 34  );.  if( pOp->p4
17506 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f  type==P4_KEYINFO
17507 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 70 4b   ){.    pCur->pK
17508 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34  eyInfo = pOp->p4
17509 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 70  .pKeyInfo;.    p
1750a 43 75 72 2d 3e 70 49 6e 63 72 4b 65 79 20 3d 20  Cur->pIncrKey = 
1750b 26 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 2d  &pCur->pKeyInfo-
1750c 3e 69 6e 63 72 4b 65 79 3b 0a 20 20 20 20 70 43  >incrKey;.    pC
1750d 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e  ur->pKeyInfo->en
1750e 63 20 3d 20 45 4e 43 28 70 2d 3e 64 62 29 3b 0a  c = ENC(p->db);.
1750f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 75    }else{.    pCu
17510 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 30 3b  r->pKeyInfo = 0;
17511 0a 20 20 20 20 70 43 75 72 2d 3e 70 49 6e 63 72  .    pCur->pIncr
17512 4b 65 79 20 3d 20 26 70 43 75 72 2d 3e 62 6f 67  Key = &pCur->bog
17513 75 73 49 6e 63 72 4b 65 79 3b 0a 20 20 7d 0a 20  usIncrKey;.  }. 
17514 20 73 77 69 74 63 68 28 20 72 63 20 29 7b 0a 20   switch( rc ){. 
17515 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 42     case SQLITE_B
17516 55 53 59 3a 20 7b 0a 20 20 20 20 20 20 70 2d 3e  USY: {.      p->
17517 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 20 70  pc = pc;.      p
17518 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49  ->rc = rc = SQLI
17519 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 67  TE_BUSY;.      g
1751a 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b  oto vdbe_return;
1751b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1751c 53 51 4c 49 54 45 5f 4f 4b 3a 20 7b 0a 20 20 20  SQLITE_OK: {.   
1751d 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 73     int flags = s
1751e 71 6c 69 74 65 33 42 74 72 65 65 46 6c 61 67 73  qlite3BtreeFlags
1751f 28 70 43 75 72 2d 3e 70 43 75 72 73 6f 72 29 3b  (pCur->pCursor);
17520 0a 20 20 20 20 20 20 2f 2a 20 53 61 6e 69 74 79  .      /* Sanity
17521 20 63 68 65 63 6b 69 6e 67 2e 20 20 4f 6e 6c 79   checking.  Only
17522 20 74 68 65 20 6c 6f 77 65 72 20 66 6f 75 72 20   the lower four 
17523 62 69 74 73 20 6f 66 20 74 68 65 20 66 6c 61 67  bits of the flag
17524 73 20 62 79 74 65 20 73 68 6f 75 6c 64 0a 20 20  s byte should.  
17525 20 20 20 20 2a 2a 20 62 65 20 75 73 65 64 2e 20      ** be used. 
17526 20 42 69 74 20 33 20 28 6d 61 73 6b 20 30 78 30   Bit 3 (mask 0x0
17527 38 29 20 69 73 20 75 6e 70 72 65 64 69 74 61 62  8) is unpreditab
17528 6c 65 2e 20 20 54 68 65 20 6c 6f 77 65 72 20 33  le.  The lower 3
17529 20 62 69 74 73 0a 20 20 20 20 20 20 2a 2a 20 28   bits.      ** (
1752a 6d 61 73 6b 20 30 78 30 37 29 20 73 68 6f 75 6c  mask 0x07) shoul
1752b 64 20 62 65 20 65 69 74 68 65 72 20 35 20 28 69  d be either 5 (i
1752c 6e 74 6b 65 79 2b 6c 65 61 66 64 61 74 61 20 66  ntkey+leafdata f
1752d 6f 72 20 74 61 62 6c 65 73 29 20 6f 72 0a 20 20  or tables) or.  
1752e 20 20 20 20 2a 2a 20 32 20 28 7a 65 72 6f 64 61      ** 2 (zeroda
1752f 74 61 20 66 6f 72 20 69 6e 64 69 63 65 73 29 2e  ta for indices).
17530 20 20 49 66 20 74 68 65 73 65 20 63 6f 6e 64 69    If these condi
17531 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 6d 65  tions are not me
17532 74 20 69 74 20 63 61 6e 0a 20 20 20 20 20 20 2a  t it can.      *
17533 2a 20 6f 6e 6c 79 20 6d 65 61 6e 20 74 68 61 74  * only mean that
17534 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20   we are dealing 
17535 77 69 74 68 20 61 20 63 6f 72 72 75 70 74 20 64  with a corrupt d
17536 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20 20  atabase file.   
17537 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
17538 28 66 6c 61 67 73 20 26 20 30 78 66 30 29 21 3d  (flags & 0xf0)!=
17539 30 20 7c 7c 20 28 28 66 6c 61 67 73 20 26 20 30  0 || ((flags & 0
1753a 78 30 37 29 21 3d 35 20 26 26 20 28 66 6c 61 67  x07)!=5 && (flag
1753b 73 20 26 20 30 78 30 37 29 21 3d 32 29 20 29 7b  s & 0x07)!=2) ){
1753c 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
1753d 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
1753e 54 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  T;.        goto 
1753f 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
17540 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  or;.      }.    
17541 20 20 70 43 75 72 2d 3e 69 73 54 61 62 6c 65 20    pCur->isTable 
17542 3d 20 28 66 6c 61 67 73 20 26 20 42 54 52 45 45  = (flags & BTREE
17543 5f 49 4e 54 4b 45 59 29 21 3d 30 3b 0a 20 20 20  _INTKEY)!=0;.   
17544 20 20 20 70 43 75 72 2d 3e 69 73 49 6e 64 65 78     pCur->isIndex
17545 20 3d 20 28 66 6c 61 67 73 20 26 20 42 54 52 45   = (flags & BTRE
17546 45 5f 5a 45 52 4f 44 41 54 41 29 21 3d 30 3b 0a  E_ZERODATA)!=0;.
17547 20 20 20 20 20 20 2f 2a 20 49 66 20 50 34 3d 3d        /* If P4==
17548 30 20 69 74 20 6d 65 61 6e 73 20 77 65 20 61 72  0 it means we ar
17549 65 20 65 78 70 65 63 74 65 64 20 74 6f 20 6f 70  e expected to op
1754a 65 6e 20 61 20 74 61 62 6c 65 2e 20 20 49 66 20  en a table.  If 
1754b 50 34 21 3d 30 20 74 68 65 6e 0a 20 20 20 20 20  P4!=0 then.     
1754c 20 2a 2a 20 77 65 20 65 78 70 65 63 74 20 74 6f   ** we expect to
1754d 20 62 65 20 6f 70 65 6e 69 6e 67 20 61 6e 20 69   be opening an i
1754e 6e 64 65 78 2e 20 20 49 66 20 74 68 69 73 20 69  ndex.  If this i
1754f 73 20 6e 6f 74 20 77 68 61 74 20 68 61 70 70 65  s not what happe
17550 6e 65 64 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68  ned,.      ** th
17551 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  en the database 
17552 69 73 20 63 6f 72 72 75 70 74 0a 20 20 20 20 20  is corrupt.     
17553 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 28 70   */.      if( (p
17554 43 75 72 2d 3e 69 73 54 61 62 6c 65 20 26 26 20  Cur->isTable && 
17555 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
17556 4b 45 59 49 4e 46 4f 29 0a 20 20 20 20 20 20 20  KEYINFO).       
17557 7c 7c 20 28 70 43 75 72 2d 3e 69 73 49 6e 64 65  || (pCur->isInde
17558 78 20 26 26 20 70 4f 70 2d 3e 70 34 74 79 70 65  x && pOp->p4type
17559 21 3d 50 34 5f 4b 45 59 49 4e 46 4f 29 20 29 7b  !=P4_KEYINFO) ){
1755a 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
1755b 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
1755c 54 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  T;.        goto 
1755d 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
1755e 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  or;.      }.    
1755f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
17560 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 45     case SQLITE_E
17561 4d 50 54 59 3a 20 7b 0a 20 20 20 20 20 20 70 43  MPTY: {.      pC
17562 75 72 2d 3e 69 73 54 61 62 6c 65 20 3d 20 70 4f  ur->isTable = pO
17563 70 2d 3e 70 34 74 79 70 65 21 3d 50 34 5f 4b 45  p->p4type!=P4_KE
17564 59 49 4e 46 4f 3b 0a 20 20 20 20 20 20 70 43 75  YINFO;.      pCu
17565 72 2d 3e 69 73 49 6e 64 65 78 20 3d 20 21 70 43  r->isIndex = !pC
17566 75 72 2d 3e 69 73 54 61 62 6c 65 3b 0a 20 20 20  ur->isTable;.   
17567 20 20 20 70 43 75 72 2d 3e 70 43 75 72 73 6f 72     pCur->pCursor
17568 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d   = 0;.      rc =
17569 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
1756a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1756b 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
1756c 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64      goto abort_d
1756d 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
1756e 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a   }.  }.  break;.
1756f 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70  }../* Opcode: Op
17570 65 6e 45 70 68 65 6d 65 72 61 6c 20 50 31 20 50  enEphemeral P1 P
17571 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f  2 * P4 *.**.** O
17572 70 65 6e 20 61 20 6e 65 77 20 63 75 72 73 6f 72  pen a new cursor
17573 20 50 31 20 74 6f 20 61 20 74 72 61 6e 73 69 65   P1 to a transie
17574 6e 74 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65  nt table..** The
17575 20 63 75 72 73 6f 72 20 69 73 20 61 6c 77 61 79   cursor is alway
17576 73 20 6f 70 65 6e 65 64 20 72 65 61 64 2f 77 72  s opened read/wr
17577 69 74 65 20 65 76 65 6e 20 69 66 20 0a 2a 2a 20  ite even if .** 
17578 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
17579 65 20 69 73 20 72 65 61 64 2d 6f 6e 6c 79 2e 20  e is read-only. 
1757a 20 54 68 65 20 74 72 61 6e 73 69 65 6e 74 20 6f   The transient o
1757b 72 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62  r virtual.** tab
1757c 6c 65 20 69 73 20 64 65 6c 65 74 65 64 20 61 75  le is deleted au
1757d 74 6f 6d 61 74 69 63 61 6c 6c 79 20 77 68 65 6e  tomatically when
1757e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 63   the cursor is c
1757f 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 32 20  losed..**.** P2 
17580 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
17581 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
17582 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a  virtual table..*
17583 2a 20 54 68 65 20 63 75 72 73 6f 72 20 70 6f 69  * The cursor poi
17584 6e 74 73 20 74 6f 20 61 20 42 54 72 65 65 20 74  nts to a BTree t
17585 61 62 6c 65 20 69 66 20 50 34 3d 3d 30 20 61 6e  able if P4==0 an
17586 64 20 74 6f 20 61 20 42 54 72 65 65 20 69 6e 64  d to a BTree ind
17587 65 78 0a 2a 2a 20 69 66 20 50 34 20 69 73 20 6e  ex.** if P4 is n
17588 6f 74 20 30 2e 20 20 49 66 20 50 34 20 69 73 20  ot 0.  If P4 is 
17589 6e 6f 74 20 4e 55 4c 4c 2c 20 69 74 20 70 6f 69  not NULL, it poi
1758a 6e 74 73 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f  nts to a KeyInfo
1758b 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 74 68   structure.** th
1758c 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 66  at defines the f
1758d 6f 72 6d 61 74 20 6f 66 20 6b 65 79 73 20 69 6e  ormat of keys in
1758e 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a   the index..**.*
1758f 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 61  * This opcode wa
17590 73 20 6f 6e 63 65 20 63 61 6c 6c 65 64 20 4f 70  s once called Op
17591 65 6e 54 65 6d 70 2e 20 20 42 75 74 20 74 68 61  enTemp.  But tha
17592 74 20 63 72 65 61 74 65 64 0a 2a 2a 20 63 6f 6e  t created.** con
17593 66 75 73 69 6f 6e 20 62 65 63 61 75 73 65 20 74  fusion because t
17594 68 65 20 74 65 72 6d 20 22 74 65 6d 70 20 74 61  he term "temp ta
17595 62 6c 65 22 2c 20 6d 69 67 68 74 20 72 65 66 65  ble", might refe
17596 72 20 65 69 74 68 65 72 0a 2a 2a 20 74 6f 20 61  r either.** to a
17597 20 54 45 4d 50 20 74 61 62 6c 65 20 61 74 20 74   TEMP table at t
17598 68 65 20 53 51 4c 20 6c 65 76 65 6c 2c 20 6f 72  he SQL level, or
17599 20 74 6f 20 61 20 74 61 62 6c 65 20 6f 70 65 6e   to a table open
1759a 65 64 20 62 79 0a 2a 2a 20 74 68 69 73 20 6f 70  ed by.** this op
1759b 63 6f 64 65 2e 20 20 54 68 65 6e 20 74 68 69 73  code.  Then this
1759c 20 6f 70 63 6f 64 65 20 77 61 73 20 63 61 6c 6c   opcode was call
1759d 20 4f 70 65 6e 56 69 72 74 75 61 6c 2e 20 20 42   OpenVirtual.  B
1759e 75 74 0a 2a 2a 20 74 68 61 74 20 63 72 65 61 74  ut.** that creat
1759f 65 64 20 63 6f 6e 66 75 73 69 6f 6e 20 77 69 74  ed confusion wit
175a0 68 20 74 68 65 20 77 68 6f 6c 65 20 76 69 72 74  h the whole virt
175a1 75 61 6c 2d 74 61 62 6c 65 20 69 64 65 61 2e 0a  ual-table idea..
175a2 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e 45  */.case OP_OpenE
175a3 70 68 65 6d 65 72 61 6c 3a 20 7b 0a 20 20 69 6e  phemeral: {.  in
175a4 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20  t i = pOp->p1;. 
175a5 20 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20   Cursor *pCx;.  
175a6 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 6e 74  static const int
175a7 20 6f 70 65 6e 46 6c 61 67 73 20 3d 20 0a 20 20   openFlags = .  
175a8 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
175a9 52 45 41 44 57 52 49 54 45 20 7c 0a 20 20 20 20  READWRITE |.    
175aa 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52    SQLITE_OPEN_CR
175ab 45 41 54 45 20 7c 0a 20 20 20 20 20 20 53 51 4c  EATE |.      SQL
175ac 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49  ITE_OPEN_EXCLUSI
175ad 56 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54  VE |.      SQLIT
175ae 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43  E_OPEN_DELETEONC
175af 4c 4f 53 45 20 7c 0a 20 20 20 20 20 20 53 51 4c  LOSE |.      SQL
175b0 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45  ITE_OPEN_TRANSIE
175b1 4e 54 5f 44 42 3b 0a 0a 20 20 61 73 73 65 72 74  NT_DB;..  assert
175b2 28 20 69 3e 3d 30 20 29 3b 0a 20 20 70 43 78 20  ( i>=0 );.  pCx 
175b3 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72  = allocateCursor
175b4 28 70 2c 20 69 2c 20 70 4f 70 2c 20 2d 31 2c 20  (p, i, pOp, -1, 
175b5 31 29 3b 0a 20 20 69 66 28 20 70 43 78 3d 3d 30  1);.  if( pCx==0
175b6 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
175b7 20 20 70 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20 3d    pCx->nullRow =
175b8 20 31 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   1;.  rc = sqlit
175b9 65 33 42 74 72 65 65 46 61 63 74 6f 72 79 28 64  e3BtreeFactory(d
175ba 62 2c 20 30 2c 20 31 2c 20 53 51 4c 49 54 45 5f  b, 0, 1, SQLITE_
175bb 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43  DEFAULT_TEMP_CAC
175bc 48 45 5f 53 49 5a 45 2c 20 6f 70 65 6e 46 6c 61  HE_SIZE, openFla
175bd 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  gs,.            
175be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
175bf 70 43 78 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28  pCx->pBt);.  if(
175c0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
175c1 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
175c2 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e  e3BtreeBeginTran
175c3 73 28 70 43 78 2d 3e 70 42 74 2c 20 31 29 3b 0a  s(pCx->pBt, 1);.
175c4 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
175c5 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f  LITE_OK ){.    /
175c6 2a 20 49 66 20 61 20 74 72 61 6e 73 69 65 6e 74  * If a transient
175c7 20 69 6e 64 65 78 20 69 73 20 72 65 71 75 69 72   index is requir
175c8 65 64 2c 20 63 72 65 61 74 65 20 69 74 20 62 79  ed, create it by
175c9 20 63 61 6c 6c 69 6e 67 0a 20 20 20 20 2a 2a 20   calling.    ** 
175ca 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61  sqlite3BtreeCrea
175cb 74 65 54 61 62 6c 65 28 29 20 77 69 74 68 20 74  teTable() with t
175cc 68 65 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54  he BTREE_ZERODAT
175cd 41 20 66 6c 61 67 20 62 65 66 6f 72 65 0a 20 20  A flag before.  
175ce 20 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 69 74 2e    ** opening it.
175cf 20 49 66 20 61 20 74 72 61 6e 73 69 65 6e 74 20   If a transient 
175d0 74 61 62 6c 65 20 69 73 20 72 65 71 75 69 72 65  table is require
175d1 64 2c 20 6a 75 73 74 20 75 73 65 20 74 68 65 0a  d, just use the.
175d2 20 20 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63      ** automatic
175d3 61 6c 6c 79 20 63 72 65 61 74 65 64 20 74 61 62  ally created tab
175d4 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67  le with root-pag
175d5 65 20 31 20 28 61 6e 20 49 4e 54 4b 45 59 20 74  e 1 (an INTKEY t
175d6 61 62 6c 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  able)..    */.  
175d7 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 2e 70 4b    if( pOp->p4.pK
175d8 65 79 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20  eyInfo ){.      
175d9 69 6e 74 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20  int pgno;.      
175da 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74  assert( pOp->p4t
175db 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20  ype==P4_KEYINFO 
175dc 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  );.      rc = sq
175dd 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65  lite3BtreeCreate
175de 54 61 62 6c 65 28 70 43 78 2d 3e 70 42 74 2c 20  Table(pCx->pBt, 
175df 26 70 67 6e 6f 2c 20 42 54 52 45 45 5f 5a 45 52  &pgno, BTREE_ZER
175e0 4f 44 41 54 41 29 3b 20 0a 20 20 20 20 20 20 69  ODATA); .      i
175e1 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
175e2 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
175e3 72 74 28 20 70 67 6e 6f 3d 3d 4d 41 53 54 45 52  rt( pgno==MASTER
175e4 5f 52 4f 4f 54 2b 31 20 29 3b 0a 20 20 20 20 20  _ROOT+1 );.     
175e5 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
175e6 74 72 65 65 43 75 72 73 6f 72 28 70 43 78 2d 3e  treeCursor(pCx->
175e7 70 42 74 2c 20 70 67 6e 6f 2c 20 31 2c 20 0a 20  pBt, pgno, 1, . 
175e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
175e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
175ea 4b 65 79 49 6e 66 6f 2a 29 70 4f 70 2d 3e 70 34  KeyInfo*)pOp->p4
175eb 2e 7a 2c 20 70 43 78 2d 3e 70 43 75 72 73 6f 72  .z, pCx->pCursor
175ec 29 3b 0a 20 20 20 20 20 20 20 20 70 43 78 2d 3e  );.        pCx->
175ed 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e  pKeyInfo = pOp->
175ee 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20  p4.pKeyInfo;.   
175ef 20 20 20 20 20 70 43 78 2d 3e 70 4b 65 79 49 6e       pCx->pKeyIn
175f0 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d  fo->enc = ENC(p-
175f1 3e 64 62 29 3b 0a 20 20 20 20 20 20 20 20 70 43  >db);.        pC
175f2 78 2d 3e 70 49 6e 63 72 4b 65 79 20 3d 20 26 70  x->pIncrKey = &p
175f3 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 69 6e  Cx->pKeyInfo->in
175f4 63 72 4b 65 79 3b 0a 20 20 20 20 20 20 7d 0a 20  crKey;.      }. 
175f5 20 20 20 20 20 70 43 78 2d 3e 69 73 54 61 62 6c       pCx->isTabl
175f6 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65  e = 0;.    }else
175f7 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
175f8 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28  ite3BtreeCursor(
175f9 70 43 78 2d 3e 70 42 74 2c 20 4d 41 53 54 45 52  pCx->pBt, MASTER
175fa 5f 52 4f 4f 54 2c 20 31 2c 20 30 2c 20 70 43 78  _ROOT, 1, 0, pCx
175fb 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  ->pCursor);.    
175fc 20 20 70 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d    pCx->isTable =
175fd 20 31 3b 0a 20 20 20 20 20 20 70 43 78 2d 3e 70   1;.      pCx->p
175fe 49 6e 63 72 4b 65 79 20 3d 20 26 70 43 78 2d 3e  IncrKey = &pCx->
175ff 62 6f 67 75 73 49 6e 63 72 4b 65 79 3b 0a 20 20  bogusIncrKey;.  
17600 20 20 7d 0a 20 20 7d 0a 20 20 70 43 78 2d 3e 69    }.  }.  pCx->i
17601 73 49 6e 64 65 78 20 3d 20 21 70 43 78 2d 3e 69  sIndex = !pCx->i
17602 73 54 61 62 6c 65 3b 0a 20 20 62 72 65 61 6b 3b  sTable;.  break;
17603 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f  .}../* Opcode: O
17604 70 65 6e 50 73 65 75 64 6f 20 50 31 20 50 32 20  penPseudo P1 P2 
17605 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e  * * *.**.** Open
17606 20 61 20 6e 65 77 20 63 75 72 73 6f 72 20 74 68   a new cursor th
17607 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 66  at points to a f
17608 61 6b 65 20 74 61 62 6c 65 20 74 68 61 74 20 63  ake table that c
17609 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65  ontains a single
1760a 0a 2a 2a 20 72 6f 77 20 6f 66 20 64 61 74 61 2e  .** row of data.
1760b 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f    Any attempt to
1760c 20 77 72 69 74 65 20 61 20 73 65 63 6f 6e 64 20   write a second 
1760d 72 6f 77 20 6f 66 20 64 61 74 61 20 63 61 75 73  row of data caus
1760e 65 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20  es the.** first 
1760f 72 6f 77 20 74 6f 20 62 65 20 64 65 6c 65 74 65  row to be delete
17610 64 2e 20 20 41 6c 6c 20 64 61 74 61 20 69 73 20  d.  All data is 
17611 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65  deleted when the
17612 20 63 75 72 73 6f 72 20 69 73 0a 2a 2a 20 63 6c   cursor is.** cl
17613 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 73  osed..**.** A ps
17614 65 75 64 6f 2d 74 61 62 6c 65 20 63 72 65 61 74  eudo-table creat
17615 65 64 20 62 79 20 74 68 69 73 20 6f 70 63 6f 64  ed by this opcod
17616 65 20 69 73 20 75 73 65 66 75 6c 20 66 6f 72 20  e is useful for 
17617 68 6f 6c 64 69 6e 67 20 74 68 65 0a 2a 2a 20 4e  holding the.** N
17618 45 57 20 6f 72 20 4f 4c 44 20 74 61 62 6c 65 73  EW or OLD tables
17619 20 69 6e 20 61 20 74 72 69 67 67 65 72 2e 20 20   in a trigger.  
1761a 41 6c 73 6f 20 75 73 65 64 20 74 6f 20 68 6f 6c  Also used to hol
1761b 64 20 74 68 65 20 61 20 73 69 6e 67 6c 65 0a 2a  d the a single.*
1761c 2a 20 72 6f 77 20 6f 75 74 70 75 74 20 66 72 6f  * row output fro
1761d 6d 20 74 68 65 20 73 6f 72 74 65 72 20 73 6f 20  m the sorter so 
1761e 74 68 61 74 20 74 68 65 20 72 6f 77 20 63 61 6e  that the row can
1761f 20 62 65 20 64 65 63 6f 6d 70 6f 73 65 64 20 69   be decomposed i
17620 6e 74 6f 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61  nto.** individua
17621 6c 20 63 6f 6c 75 6d 6e 73 20 75 73 69 6e 67 20  l columns using 
17622 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70  the OP_Column op
17623 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  code..**.** When
17624 20 4f 50 5f 49 6e 73 65 72 74 20 69 73 20 65 78   OP_Insert is ex
17625 65 63 75 74 65 64 20 74 6f 20 69 6e 73 65 72 74  ecuted to insert
17626 20 61 20 72 6f 77 20 69 6e 20 74 6f 20 74 68 65   a row in to the
17627 20 70 73 65 75 64 6f 20 74 61 62 6c 65 2c 0a 2a   pseudo table,.*
17628 2a 20 74 68 65 20 70 73 65 75 64 6f 2d 74 61 62  * the pseudo-tab
17629 6c 65 20 63 75 72 73 6f 72 20 6d 61 79 20 6f 72  le cursor may or
1762a 20 6d 61 79 20 6e 6f 74 20 6d 61 6b 65 20 69 74   may not make it
1762b 27 73 20 6f 77 6e 20 63 6f 70 79 20 6f 66 20 74  's own copy of t
1762c 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 72  he.** original r
1762d 6f 77 20 64 61 74 61 2e 20 49 66 20 50 32 20 69  ow data. If P2 i
1762e 73 20 30 2c 20 74 68 65 6e 20 74 68 65 20 70 73  s 0, then the ps
1762f 65 75 64 6f 2d 74 61 62 6c 65 20 77 69 6c 6c 20  eudo-table will 
17630 63 6f 70 79 20 74 68 65 0a 2a 2a 20 6f 72 69 67  copy the.** orig
17631 69 6e 61 6c 20 72 6f 77 20 64 61 74 61 2e 20 4f  inal row data. O
17632 74 68 65 72 77 69 73 65 2c 20 61 20 70 6f 69 6e  therwise, a poin
17633 74 65 72 20 74 6f 20 74 68 65 20 6f 72 69 67 69  ter to the origi
17634 6e 61 6c 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 0a  nal memory cell.
17635 2a 2a 20 69 73 20 73 74 6f 72 65 64 2e 20 49 6e  ** is stored. In
17636 20 74 68 69 73 20 63 61 73 65 2c 20 74 68 65 20   this case, the 
17637 76 64 62 65 20 70 72 6f 67 72 61 6d 20 6d 75 73  vdbe program mus
17638 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68  t ensure that th
17639 65 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63 65 6c  e .** memory cel
1763a 6c 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  l containing the
1763b 20 72 6f 77 20 64 61 74 61 20 69 73 20 6e 6f 74   row data is not
1763c 20 6f 76 65 72 77 72 69 74 74 65 6e 20 75 6e 74   overwritten unt
1763d 69 6c 20 74 68 65 0a 2a 2a 20 70 73 65 75 64 6f  il the.** pseudo
1763e 20 74 61 62 6c 65 20 69 73 20 63 6c 6f 73 65 64   table is closed
1763f 20 28 6f 72 20 61 20 6e 65 77 20 72 6f 77 20 69   (or a new row i
17640 73 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20  s inserted into 
17641 69 74 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  it)..*/.case OP_
17642 4f 70 65 6e 50 73 65 75 64 6f 3a 20 7b 0a 20 20  OpenPseudo: {.  
17643 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b  int i = pOp->p1;
17644 0a 20 20 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a  .  Cursor *pCx;.
17645 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 29    assert( i>=0 )
17646 3b 0a 20 20 70 43 78 20 3d 20 61 6c 6c 6f 63 61  ;.  pCx = alloca
17647 74 65 43 75 72 73 6f 72 28 70 2c 20 69 2c 20 26  teCursor(p, i, &
17648 70 4f 70 5b 2d 31 5d 2c 20 2d 31 2c 20 30 29 3b  pOp[-1], -1, 0);
17649 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 20 29 20  .  if( pCx==0 ) 
1764a 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 70  goto no_mem;.  p
1764b 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b  Cx->nullRow = 1;
1764c 0a 20 20 70 43 78 2d 3e 70 73 65 75 64 6f 54 61  .  pCx->pseudoTa
1764d 62 6c 65 20 3d 20 31 3b 0a 20 20 70 43 78 2d 3e  ble = 1;.  pCx->
1764e 65 70 68 65 6d 50 73 65 75 64 6f 54 61 62 6c 65  ephemPseudoTable
1764f 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 70 43   = pOp->p2;.  pC
17650 78 2d 3e 70 49 6e 63 72 4b 65 79 20 3d 20 26 70  x->pIncrKey = &p
17651 43 78 2d 3e 62 6f 67 75 73 49 6e 63 72 4b 65 79  Cx->bogusIncrKey
17652 3b 0a 20 20 70 43 78 2d 3e 69 73 54 61 62 6c 65  ;.  pCx->isTable
17653 20 3d 20 31 3b 0a 20 20 70 43 78 2d 3e 69 73 49   = 1;.  pCx->isI
17654 6e 64 65 78 20 3d 20 30 3b 0a 20 20 62 72 65 61  ndex = 0;.  brea
17655 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
17656 20 43 6c 6f 73 65 20 50 31 20 2a 20 2a 20 2a 20   Close P1 * * * 
17657 2a 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20  *.**.** Close a 
17658 63 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73 6c  cursor previousl
17659 79 20 6f 70 65 6e 65 64 20 61 73 20 50 31 2e 20  y opened as P1. 
1765a 20 49 66 20 50 31 20 69 73 20 6e 6f 74 0a 2a 2a   If P1 is not.**
1765b 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e 2c   currently open,
1765c 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   this instructio
1765d 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  n is a no-op..*/
1765e 0a 63 61 73 65 20 4f 50 5f 43 6c 6f 73 65 3a 20  .case OP_Close: 
1765f 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d  {.  int i = pOp-
17660 3e 70 31 3b 0a 20 20 61 73 73 65 72 74 28 20 69  >p1;.  assert( i
17661 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72  >=0 && i<p->nCur
17662 73 6f 72 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  sor );.  sqlite3
17663 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 70  VdbeFreeCursor(p
17664 2c 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 3b 0a  , p->apCsr[i]);.
17665 20 20 70 2d 3e 61 70 43 73 72 5b 69 5d 20 3d 20    p->apCsr[i] = 
17666 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  0;.  break;.}../
17667 2a 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 47 65  * Opcode: MoveGe
17668 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a   P1 P2 P3 P4 *.*
17669 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50  *.** If cursor P
1766a 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53  1 refers to an S
1766b 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65  QL table (B-Tree
1766c 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67   that uses integ
1766d 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73  er keys), .** us
1766e 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61  e the integer va
1766f 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
17670 50 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66 20  P3 as a key. If 
17671 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73  cursor P1 refers
17672 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69   .** to an SQL i
17673 6e 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73  ndex, then P3 is
17674 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e   the first in an
17675 20 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 67   array of P4 reg
17676 69 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20  isters .** that 
17677 61 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 75  are used as an u
17678 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65  npacked index ke
17679 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69  y. .**.** Reposi
1767a 74 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73  tion cursor P1 s
1767b 6f 20 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74  o that  it point
1767c 73 20 74 6f 20 74 68 65 20 73 6d 61 6c 6c 65 73  s to the smalles
1767d 74 20 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a  t entry that .**
1767e 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
1767f 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65   or equal to the
17680 20 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74   key value. If t
17681 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f  here are no reco
17682 72 64 73 20 0a 2a 2a 20 67 72 65 61 74 65 72 20  rds .** greater 
17683 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
17684 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20   the key and P2 
17685 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65  is not zero, the
17686 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a  n jump to P2..**
17687 0a 2a 2a 20 41 20 73 70 65 63 69 61 6c 20 66 65  .** A special fe
17688 61 74 75 72 65 20 6f 66 20 74 68 69 73 20 6f 70  ature of this op
17689 63 6f 64 65 20 28 61 6e 64 20 64 69 66 66 65 72  code (and differ
1768a 65 6e 74 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20  ent from the.** 
1768b 72 65 6c 61 74 65 64 20 4f 50 5f 4d 6f 76 65 47  related OP_MoveG
1768c 74 2c 20 4f 50 5f 4d 6f 76 65 4c 74 2c 20 61 6e  t, OP_MoveLt, an
1768d 64 20 4f 50 5f 4d 6f 76 65 4c 65 29 20 69 73 20  d OP_MoveLe) is 
1768e 74 68 61 74 20 69 66 20 50 32 20 69 73 0a 2a 2a  that if P2 is.**
1768f 20 7a 65 72 6f 20 61 6e 64 20 50 31 20 69 73 20   zero and P1 is 
17690 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 61 20  an SQL table (a 
17691 62 2d 74 72 65 65 20 77 69 74 68 20 69 6e 74 65  b-tree with inte
17692 67 65 72 20 6b 65 79 73 29 20 74 68 65 6e 0a 2a  ger keys) then.*
17693 2a 20 74 68 65 20 73 65 65 6b 20 69 73 20 64 65  * the seek is de
17694 66 65 72 72 65 64 20 75 6e 74 69 6c 20 69 74 20  ferred until it 
17695 69 73 20 61 63 74 75 61 6c 6c 79 20 6e 65 65 64  is actually need
17696 65 64 2e 20 20 49 74 20 6d 69 67 68 74 20 62 65  ed.  It might be
17697 0a 2a 2a 20 74 68 65 20 63 61 73 65 20 74 68 61  .** the case tha
17698 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  t the cursor is 
17699 6e 65 76 65 72 20 61 63 63 65 73 73 65 64 2e 20  never accessed. 
1769a 20 42 79 20 64 65 66 65 72 72 69 6e 67 20 74 68   By deferring th
1769b 65 0a 2a 2a 20 73 65 65 6b 2c 20 77 65 20 61 76  e.** seek, we av
1769c 6f 69 64 20 75 6e 6e 65 63 65 73 73 61 72 79 20  oid unnecessary 
1769d 73 65 65 6b 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  seeks..**.** See
1769e 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f   also: Found, No
1769f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74  tFound, Distinct
176a0 2c 20 4d 6f 76 65 4c 74 2c 20 4d 6f 76 65 47 74  , MoveLt, MoveGt
176a1 2c 20 4d 6f 76 65 4c 65 0a 2a 2f 0a 2f 2a 20 4f  , MoveLe.*/./* O
176a2 70 63 6f 64 65 3a 20 4d 6f 76 65 47 74 20 50 31  pcode: MoveGt P1
176a3 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a   P2 P3 P4 *.**.*
176a4 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72  * If cursor P1 r
176a5 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51 4c 20  efers to an SQL 
176a6 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20 74 68  table (B-Tree th
176a7 61 74 20 75 73 65 73 20 69 6e 74 65 67 65 72 20  at uses integer 
176a8 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74  keys), .** use t
176a9 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  he integer value
176aa 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20   in register P3 
176ab 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63 75 72  as a key. If cur
176ac 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a  sor P1 refers .*
176ad 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65  * to an SQL inde
176ae 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 68  x, then P3 is th
176af 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72  e first in an ar
176b0 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 74  ray of P4 regist
176b1 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65  ers .** that are
176b2 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61   used as an unpa
176b3 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20  cked index key. 
176b4 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f  .**.** Repositio
176b5 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74  n cursor P1 so t
176b6 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74  hat  it points t
176b7 6f 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65  o the smallest e
176b8 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 73  ntry that .** is
176b9 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68   greater than th
176ba 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20  e key value. If 
176bb 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63  there are no rec
176bc 6f 72 64 73 20 67 72 65 61 74 65 72 20 74 68 61  ords greater tha
176bd 6e 20 0a 2a 2a 20 74 68 65 20 6b 65 79 20 61 6e  n .** the key an
176be 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f  d P2 is not zero
176bf 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50  , then jump to P
176c0 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  2..**.** See als
176c1 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75  o: Found, NotFou
176c2 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 4d 6f  nd, Distinct, Mo
176c3 76 65 4c 74 2c 20 4d 6f 76 65 47 65 2c 20 4d 6f  veLt, MoveGe, Mo
176c4 76 65 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  veLe.*/./* Opcod
176c5 65 3a 20 4d 6f 76 65 4c 74 20 50 31 20 50 32 20  e: MoveLt P1 P2 
176c6 50 33 20 50 34 20 2a 20 0a 2a 2a 0a 2a 2a 20 49  P3 P4 * .**.** I
176c7 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 65  f cursor P1 refe
176c8 72 73 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 62  rs to an SQL tab
176c9 6c 65 20 28 42 2d 54 72 65 65 20 74 68 61 74 20  le (B-Tree that 
176ca 75 73 65 73 20 69 6e 74 65 67 65 72 20 6b 65 79  uses integer key
176cb 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65 20  s), .** use the 
176cc 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 6e  integer value in
176cd 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20   register P3 as 
176ce 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72  a key. If cursor
176cf 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74   P1 refers .** t
176d0 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20  o an SQL index, 
176d1 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 66  then P3 is the f
176d2 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79  irst in an array
176d3 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73   of P4 registers
176d4 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73   .** that are us
176d5 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65  ed as an unpacke
176d6 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a  d index key. .**
176d7 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63  .** Reposition c
176d8 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74  ursor P1 so that
176d9 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74    it points to t
176da 68 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72 79  he largest entry
176db 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 6c 65 73   that .** is les
176dc 73 20 74 68 61 6e 20 74 68 65 20 6b 65 79 20 76  s than the key v
176dd 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 20 61  alue. If there a
176de 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 6c 65  re no records le
176df 73 73 20 74 68 61 6e 20 0a 2a 2a 20 74 68 65 20  ss than .** the 
176e0 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f  key and P2 is no
176e1 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d  t zero, then jum
176e2 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53  p to P2..**.** S
176e3 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20  ee also: Found, 
176e4 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e  NotFound, Distin
176e5 63 74 2c 20 4d 6f 76 65 47 74 2c 20 4d 6f 76 65  ct, MoveGt, Move
176e6 47 65 2c 20 4d 6f 76 65 4c 65 0a 2a 2f 0a 2f 2a  Ge, MoveLe.*/./*
176e7 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 4c 65 20   Opcode: MoveLe 
176e8 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a  P1 P2 P3 P4 *.**
176e9 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31  .** If cursor P1
176ea 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51   refers to an SQ
176eb 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20  L table (B-Tree 
176ec 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 65  that uses intege
176ed 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65  r keys), .** use
176ee 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c   the integer val
176ef 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
176f0 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63  3 as a key. If c
176f1 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20  ursor P1 refers 
176f2 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e  .** to an SQL in
176f3 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20  dex, then P3 is 
176f4 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20  the first in an 
176f5 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 69  array of P4 regi
176f6 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61  sters .** that a
176f7 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e  re used as an un
176f8 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79  packed index key
176f9 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74  . .**.** Reposit
176fa 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f  ion cursor P1 so
176fb 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20   that it points 
176fc 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20 65  to the largest e
176fd 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 73  ntry that .** is
176fe 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71   less than or eq
176ff 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 76  ual to the key v
17700 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 20 61  alue. If there a
17701 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 0a 2a  re no records .*
17702 2a 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65  * less than or e
17703 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20  qual to the key 
17704 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65  and P2 is not ze
17705 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f  ro, then jump to
17706 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61   P2..**.** See a
17707 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46  lso: Found, NotF
17708 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20  ound, Distinct, 
17709 4d 6f 76 65 47 74 2c 20 4d 6f 76 65 47 65 2c 20  MoveGt, MoveGe, 
1770a 4d 6f 76 65 4c 74 0a 2a 2f 0a 63 61 73 65 20 4f  MoveLt.*/.case O
1770b 50 5f 4d 6f 76 65 4c 74 3a 20 20 20 20 20 20 20  P_MoveLt:       
1770c 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a    /* jump, in3 *
1770d 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65 4c 65  /.case OP_MoveLe
1770e 3a 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d  :         /* jum
1770f 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f  p, in3 */.case O
17710 50 5f 4d 6f 76 65 47 65 3a 20 20 20 20 20 20 20  P_MoveGe:       
17711 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a    /* jump, in3 *
17712 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65 47 74  /.case OP_MoveGt
17713 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d  : {       /* jum
17714 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20  p, in3 */.  int 
17715 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 43  i = pOp->p1;.  C
17716 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61 73  ursor *pC;..  as
17717 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
17718 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
17719 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d  pC = p->apCsr[i]
1771a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d  ;.  assert( pC!=
1771b 30 20 29 3b 0a 20 20 69 66 28 20 70 43 2d 3e 70  0 );.  if( pC->p
1771c 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a 20 20 20  Cursor!=0 ){.   
1771d 20 69 6e 74 20 72 65 73 2c 20 6f 63 3b 0a 20 20   int res, oc;.  
1771e 20 20 6f 63 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f    oc = pOp->opco
1771f 64 65 3b 0a 20 20 20 20 70 43 2d 3e 6e 75 6c 6c  de;.    pC->null
17720 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20 2a 70 43  Row = 0;.    *pC
17721 2d 3e 70 49 6e 63 72 4b 65 79 20 3d 20 6f 63 3d  ->pIncrKey = oc=
17722 3d 4f 50 5f 4d 6f 76 65 47 74 20 7c 7c 20 6f 63  =OP_MoveGt || oc
17723 3d 3d 4f 50 5f 4d 6f 76 65 4c 65 3b 0a 20 20 20  ==OP_MoveLe;.   
17724 20 69 66 28 20 70 43 2d 3e 69 73 54 61 62 6c 65   if( pC->isTable
17725 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 69 4b   ){.      i64 iK
17726 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  ey = sqlite3Vdbe
17727 49 6e 74 56 61 6c 75 65 28 70 49 6e 33 29 3b 0a  IntValue(pIn3);.
17728 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70        if( pOp->p
17729 32 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  2==0 ){.        
1772a 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63  assert( pOp->opc
1772b 6f 64 65 3d 3d 4f 50 5f 4d 6f 76 65 47 65 20 29  ode==OP_MoveGe )
1772c 3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 6d 6f  ;.        pC->mo
1772d 76 65 74 6f 54 61 72 67 65 74 20 3d 20 69 4b 65  vetoTarget = iKe
1772e 79 3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 72  y;.        pC->r
1772f 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b  owidIsValid = 0;
17730 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 64 65 66  .        pC->def
17731 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 31 3b  erredMoveto = 1;
17732 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
17733 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63        }.      rc
17734 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d   = sqlite3BtreeM
17735 6f 76 65 74 6f 28 70 43 2d 3e 70 43 75 72 73 6f  oveto(pC->pCurso
17736 72 2c 20 30 2c 20 30 2c 20 28 75 36 34 29 69 4b  r, 0, 0, (u64)iK
17737 65 79 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20 20  ey, 0, &res);.  
17738 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
17739 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1773a 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1773b 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20  to_error;.      
1773c 7d 0a 20 20 20 20 20 20 70 43 2d 3e 6c 61 73 74  }.      pC->last
1773d 52 6f 77 69 64 20 3d 20 69 4b 65 79 3b 0a 20 20  Rowid = iKey;.  
1773e 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56      pC->rowidIsV
1773f 61 6c 69 64 20 3d 20 72 65 73 3d 3d 30 3b 0a 20  alid = res==0;. 
17740 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
17741 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72  UnpackedRecord r
17742 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 69 65  ;.      int nFie
17743 6c 64 20 3d 20 70 4f 70 2d 3e 70 34 2e 69 3b 0a  ld = pOp->p4.i;.
17744 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
17745 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e  p->p4type==P4_IN
17746 54 33 32 20 29 3b 0a 20 20 20 20 20 20 61 73 73  T32 );.      ass
17747 65 72 74 28 20 6e 46 69 65 6c 64 3e 30 20 29 3b  ert( nField>0 );
17748 0a 20 20 20 20 20 20 72 2e 70 4b 65 79 49 6e 66  .      r.pKeyInf
17749 6f 20 3d 20 70 43 2d 3e 70 4b 65 79 49 6e 66 6f  o = pC->pKeyInfo
1774a 3b 0a 20 20 20 20 20 20 72 2e 6e 46 69 65 6c 64  ;.      r.nField
1774b 20 3d 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20 20   = nField;.     
1774c 20 72 2e 6e 65 65 64 46 72 65 65 20 3d 20 30 3b   r.needFree = 0;
1774d 0a 20 20 20 20 20 20 72 2e 6e 65 65 64 44 65 73  .      r.needDes
1774e 74 72 6f 79 20 3d 20 30 3b 0a 20 20 20 20 20 20  troy = 0;.      
1774f 72 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65  r.aMem = &p->aMe
17750 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20  m[pOp->p3];.    
17751 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
17752 72 65 65 4d 6f 76 65 74 6f 28 70 43 2d 3e 70 43  reeMoveto(pC->pC
17753 75 72 73 6f 72 2c 20 30 2c 20 26 72 2c 20 30 2c  ursor, 0, &r, 0,
17754 20 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20   0, &res);.     
17755 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
17756 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  OK ){.        go
17757 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
17758 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20  error;.      }. 
17759 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 49 73       pC->rowidIs
1775a 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 7d  Valid = 0;.    }
1775b 0a 20 20 20 20 70 43 2d 3e 64 65 66 65 72 72 65  .    pC->deferre
1775c 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20  dMoveto = 0;.   
1775d 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73   pC->cacheStatus
1775e 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a   = CACHE_STALE;.
1775f 20 20 20 20 2a 70 43 2d 3e 70 49 6e 63 72 4b 65      *pC->pIncrKe
17760 79 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 53 51  y = 0;.#ifdef SQ
17761 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71  LITE_TEST.    sq
17762 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75  lite3_search_cou
17763 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20  nt++;.#endif.   
17764 20 69 66 28 20 6f 63 3d 3d 4f 50 5f 4d 6f 76 65   if( oc==OP_Move
17765 47 65 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 4d 6f 76  Ge || oc==OP_Mov
17766 65 47 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28  eGt ){.      if(
17767 20 72 65 73 3c 30 20 29 7b 0a 20 20 20 20 20 20   res<0 ){.      
17768 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
17769 72 65 65 4e 65 78 74 28 70 43 2d 3e 70 43 75 72  reeNext(pC->pCur
1776a 73 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20  sor, &res);.    
1776b 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1776c 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f  TE_OK ) goto abo
1776d 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
1776e 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 72 6f 77  .        pC->row
1776f 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20  idIsValid = 0;. 
17770 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
17771 20 20 20 20 72 65 73 20 3d 20 30 3b 0a 20 20 20      res = 0;.   
17772 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
17773 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 63        assert( oc
17774 3d 3d 4f 50 5f 4d 6f 76 65 4c 74 20 7c 7c 20 6f  ==OP_MoveLt || o
17775 63 3d 3d 4f 50 5f 4d 6f 76 65 4c 65 20 29 3b 0a  c==OP_MoveLe );.
17776 20 20 20 20 20 20 69 66 28 20 72 65 73 3e 3d 30        if( res>=0
17777 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
17778 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65   sqlite3BtreePre
17779 76 69 6f 75 73 28 70 43 2d 3e 70 43 75 72 73 6f  vious(pC->pCurso
1777a 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20  r, &res);.      
1777b 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1777c 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  _OK ) goto abort
1777d 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20  _due_to_error;. 
1777e 20 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 64         pC->rowid
1777f 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20  IsValid = 0;.   
17780 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
17781 20 20 2f 2a 20 72 65 73 20 6d 69 67 68 74 20 62    /* res might b
17782 65 20 6e 65 67 61 74 69 76 65 20 62 65 63 61 75  e negative becau
17783 73 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  se the table is 
17784 65 6d 70 74 79 2e 20 20 43 68 65 63 6b 20 74 6f  empty.  Check to
17785 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 65 20  .        ** see 
17786 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63  if this is the c
17787 61 73 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ase..        */.
17788 20 20 20 20 20 20 20 20 72 65 73 20 3d 20 73 71          res = sq
17789 6c 69 74 65 33 42 74 72 65 65 45 6f 66 28 70 43  lite3BtreeEof(pC
1778a 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  ->pCursor);.    
1778b 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73    }.    }.    as
1778c 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20  sert( pOp->p2>0 
1778d 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29  );.    if( res )
1778e 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70  {.      pc = pOp
1778f 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a  ->p2 - 1;.    }.
17790 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
17791 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 6f 75 6e 64  /* Opcode: Found
17792 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a   P1 P2 P3 * *.**
17793 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50 33 20  .** Register P3 
17794 68 6f 6c 64 73 20 61 20 62 6c 6f 62 20 63 6f 6e  holds a blob con
17795 73 74 72 75 63 74 65 64 20 62 79 20 4d 61 6b 65  structed by Make
17796 52 65 63 6f 72 64 2e 20 20 50 31 20 69 73 20 61  Record.  P1 is a
17797 6e 20 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 61  n index..** If a
17798 6e 20 65 6e 74 72 79 20 74 68 61 74 20 6d 61 74  n entry that mat
17799 63 68 65 73 20 74 68 65 20 76 61 6c 75 65 20 69  ches the value i
1779a 6e 20 72 65 67 69 73 74 65 72 20 70 33 20 65 78  n register p3 ex
1779b 69 73 74 73 20 69 6e 20 50 31 20 74 68 65 6e 0a  ists in P1 then.
1779c 2a 2a 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 20  ** jump to P2.  
1779d 49 66 20 74 68 65 20 50 33 20 76 61 6c 75 65 20  If the P3 value 
1779e 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 61  does not match a
1779f 6e 79 20 65 6e 74 72 79 20 69 6e 20 50 31 0a 2a  ny entry in P1.*
177a0 2a 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 75  * then fall thru
177a1 2e 20 20 54 68 65 20 50 31 20 63 75 72 73 6f 72  .  The P1 cursor
177a2 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
177a3 67 20 61 74 20 74 68 65 20 6d 61 74 63 68 69 6e  g at the matchin
177a4 67 20 65 6e 74 72 79 0a 2a 2a 20 69 66 20 69 74  g entry.** if it
177a5 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54   exists..**.** T
177a6 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
177a7 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  is used to imple
177a8 6d 65 6e 74 20 74 68 65 20 49 4e 20 6f 70 65 72  ment the IN oper
177a9 61 74 6f 72 20 77 68 65 72 65 20 74 68 65 0a 2a  ator where the.*
177aa 2a 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65  * left-hand side
177ab 20 69 73 20 61 20 53 45 4c 45 43 54 20 73 74 61   is a SELECT sta
177ac 74 65 6d 65 6e 74 2e 20 20 50 31 20 6d 61 79 20  tement.  P1 may 
177ad 62 65 20 61 20 74 72 75 65 20 69 6e 64 65 78 2c  be a true index,
177ae 20 6f 72 20 69 74 0a 2a 2a 20 6d 61 79 20 62 65   or it.** may be
177af 20 61 20 74 65 6d 70 6f 72 61 72 79 20 69 6e 64   a temporary ind
177b0 65 78 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68  ex that holds th
177b1 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65  e results of the
177b2 20 53 45 4c 45 43 54 0a 2a 2a 20 73 74 61 74 65   SELECT.** state
177b3 6d 65 6e 74 2e 20 20 20 54 68 69 73 20 69 6e 73  ment.   This ins
177b4 74 72 75 63 74 69 6f 6e 20 69 73 20 61 6c 73 6f  truction is also
177b5 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
177b6 6e 74 20 74 68 65 0a 2a 2a 20 44 49 53 54 49 4e  nt the.** DISTIN
177b7 43 54 20 6b 65 79 77 6f 72 64 20 69 6e 20 53 45  CT keyword in SE
177b8 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 2e  LECT statements.
177b9 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74  .**.** This inst
177ba 72 75 63 74 69 6f 6e 20 63 68 65 63 6b 73 20 69  ruction checks i
177bb 66 20 69 6e 64 65 78 20 50 31 20 63 6f 6e 74 61  f index P1 conta
177bc 69 6e 73 20 61 20 72 65 63 6f 72 64 20 66 6f 72  ins a record for
177bd 20 77 68 69 63 68 20 0a 2a 2a 20 74 68 65 20 66   which .** the f
177be 69 72 73 74 20 4e 20 73 65 72 69 61 6c 69 73 65  irst N serialise
177bf 64 20 76 61 6c 75 65 73 20 65 78 61 63 74 6c 79  d values exactly
177c0 20 6d 61 74 63 68 20 74 68 65 20 4e 20 73 65 72   match the N ser
177c1 69 61 6c 69 73 65 64 20 76 61 6c 75 65 73 0a 2a  ialised values.*
177c2 2a 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20  * in the record 
177c3 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2c 20  in register P3, 
177c4 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 74  where N is the t
177c5 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 76  otal number of v
177c6 61 6c 75 65 73 20 69 6e 0a 2a 2a 20 74 68 65 20  alues in.** the 
177c7 50 33 20 72 65 63 6f 72 64 20 28 74 68 65 20 50  P3 record (the P
177c8 33 20 72 65 63 6f 72 64 20 69 73 20 61 20 70 72  3 record is a pr
177c9 65 66 69 78 20 6f 66 20 74 68 65 20 50 31 20 72  efix of the P1 r
177ca 65 63 6f 72 64 29 2e 20 0a 2a 2a 0a 2a 2a 20 53  ecord). .**.** S
177cb 65 65 20 61 6c 73 6f 3a 20 4e 6f 74 46 6f 75 6e  ee also: NotFoun
177cc 64 2c 20 4d 6f 76 65 54 6f 2c 20 49 73 55 6e 69  d, MoveTo, IsUni
177cd 71 75 65 2c 20 4e 6f 74 45 78 69 73 74 73 0a 2a  que, NotExists.*
177ce 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74  /./* Opcode: Not
177cf 46 6f 75 6e 64 20 50 31 20 50 32 20 50 33 20 2a  Found P1 P2 P3 *
177d0 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65   *.**.** Registe
177d1 72 20 50 33 20 68 6f 6c 64 73 20 61 20 62 6c 6f  r P3 holds a blo
177d2 62 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62 79  b constructed by
177d3 20 4d 61 6b 65 52 65 63 6f 72 64 2e 20 20 50 31   MakeRecord.  P1
177d4 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 2e   is.** an index.
177d5 20 20 49 66 20 6e 6f 20 65 6e 74 72 79 20 65 78    If no entry ex
177d6 69 73 74 73 20 69 6e 20 50 31 20 74 68 61 74 20  ists in P1 that 
177d7 6d 61 74 63 68 65 73 20 74 68 65 20 62 6c 6f 62  matches the blob
177d8 20 74 68 65 6e 20 6a 75 6d 70 0a 2a 2a 20 74 6f   then jump.** to
177d9 20 50 32 2e 20 20 49 66 20 61 6e 20 65 6e 74 72   P2.  If an entr
177da 79 20 64 6f 65 73 20 65 78 69 73 74 69 6e 67 2c  y does existing,
177db 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 2e 20 20   fall through.  
177dc 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65  The cursor is le
177dd 66 74 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74  ft.** pointing t
177de 6f 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74  o the entry that
177df 20 6d 61 74 63 68 65 73 2e 0a 2a 2a 0a 2a 2a 20   matches..**.** 
177e0 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c  See also: Found,
177e1 20 4d 6f 76 65 54 6f 2c 20 4e 6f 74 45 78 69 73   MoveTo, NotExis
177e2 74 73 2c 20 49 73 55 6e 69 71 75 65 0a 2a 2f 0a  ts, IsUnique.*/.
177e3 63 61 73 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64  case OP_NotFound
177e4 3a 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c  :       /* jump,
177e5 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f   in3 */.case OP_
177e6 46 6f 75 6e 64 3a 20 7b 20 20 20 20 20 20 20 20  Found: {        
177e7 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a  /* jump, in3 */.
177e8 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70    int i = pOp->p
177e9 31 3b 0a 20 20 69 6e 74 20 61 6c 72 65 61 64 79  1;.  int already
177ea 45 78 69 73 74 73 20 3d 20 30 3b 0a 20 20 43 75  Exists = 0;.  Cu
177eb 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73 65  rsor *pC;.  asse
177ec 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d  rt( i>=0 && i<p-
177ed 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73  >nCursor );.  as
177ee 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69  sert( p->apCsr[i
177ef 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70  ]!=0 );.  if( (p
177f0 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29  C = p->apCsr[i])
177f1 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a  ->pCursor!=0 ){.
177f2 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20      int res;.   
177f3 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 54   assert( pC->isT
177f4 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 61  able==0 );.    a
177f5 73 73 65 72 74 28 20 70 49 6e 33 2d 3e 66 6c 61  ssert( pIn3->fla
177f6 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b  gs & MEM_Blob );
177f7 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70  .    if( pOp->op
177f8 63 6f 64 65 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29  code==OP_Found )
177f9 7b 0a 20 20 20 20 20 20 70 43 2d 3e 70 4b 65 79  {.      pC->pKey
177fa 49 6e 66 6f 2d 3e 70 72 65 66 69 78 49 73 45 71  Info->prefixIsEq
177fb 75 61 6c 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  ual = 1;.    }. 
177fc 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
177fd 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 2d 3e 70  treeMoveto(pC->p
177fe 43 75 72 73 6f 72 2c 20 70 49 6e 33 2d 3e 7a 2c  Cursor, pIn3->z,
177ff 20 30 2c 20 70 49 6e 33 2d 3e 6e 2c 20 30 2c 20   0, pIn3->n, 0, 
17800 26 72 65 73 29 3b 0a 20 20 20 20 70 43 2d 3e 70  &res);.    pC->p
17801 4b 65 79 49 6e 66 6f 2d 3e 70 72 65 66 69 78 49  KeyInfo->prefixI
17802 73 45 71 75 61 6c 20 3d 20 30 3b 0a 20 20 20 20  sEqual = 0;.    
17803 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
17804 4b 20 29 7b 0a 20 20 20 20 20 20 62 72 65 61 6b  K ){.      break
17805 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 6c 72 65  ;.    }.    alre
17806 61 64 79 45 78 69 73 74 73 20 3d 20 28 72 65 73  adyExists = (res
17807 3d 3d 30 29 3b 0a 20 20 20 20 70 43 2d 3e 64 65  ==0);.    pC->de
17808 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30  ferredMoveto = 0
17809 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 65 53  ;.    pC->cacheS
1780a 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54  tatus = CACHE_ST
1780b 41 4c 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  ALE;.  }.  if( p
1780c 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46  Op->opcode==OP_F
1780d 6f 75 6e 64 20 29 7b 0a 20 20 20 20 69 66 28 20  ound ){.    if( 
1780e 61 6c 72 65 61 64 79 45 78 69 73 74 73 20 29 20  alreadyExists ) 
1780f 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
17810 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
17811 66 28 20 21 61 6c 72 65 61 64 79 45 78 69 73 74  f( !alreadyExist
17812 73 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32  s ) pc = pOp->p2
17813 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61   - 1;.  }.  brea
17814 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
17815 20 49 73 55 6e 69 71 75 65 20 50 31 20 50 32 20   IsUnique P1 P2 
17816 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 68  P3 P4 *.**.** Th
17817 65 20 50 33 20 72 65 67 69 73 74 65 72 20 63 6f  e P3 register co
17818 6e 74 61 69 6e 73 20 61 6e 20 69 6e 74 65 67 65  ntains an intege
17819 72 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 2e  r record number.
1781a 20 20 43 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 72    Call this.** r
1781b 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 52 2e 20  ecord number R. 
1781c 20 54 68 65 20 50 34 20 72 65 67 69 73 74 65 72   The P4 register
1781d 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 69 6e 64   contains an ind
1781e 65 78 20 6b 65 79 20 63 72 65 61 74 65 64 0a 2a  ex key created.*
1781f 2a 20 75 73 69 6e 67 20 4d 61 6b 65 49 64 78 52  * using MakeIdxR
17820 65 63 2e 20 20 43 61 6c 6c 20 69 74 20 4b 2e 0a  ec.  Call it K..
17821 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 6e 20 69  **.** P1 is an i
17822 6e 64 65 78 2e 20 20 53 6f 20 69 74 20 68 61 73  ndex.  So it has
17823 20 6e 6f 20 64 61 74 61 20 61 6e 64 20 69 74 73   no data and its
17824 20 6b 65 79 20 63 6f 6e 73 69 73 74 73 20 6f 66   key consists of
17825 20 61 0a 2a 2a 20 72 65 63 6f 72 64 20 67 65 6e   a.** record gen
17826 65 72 61 74 65 64 20 62 79 20 4f 50 5f 4d 61 6b  erated by OP_Mak
17827 65 52 65 63 6f 72 64 20 77 68 65 72 65 20 74 68  eRecord where th
17828 65 20 6c 61 73 74 20 66 69 65 6c 64 20 69 73 20  e last field is 
17829 74 68 65 20 0a 2a 2a 20 72 6f 77 69 64 20 6f 66  the .** rowid of
1782a 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20   the entry that 
1782b 74 68 65 20 69 6e 64 65 78 20 72 65 66 65 72 73  the index refers
1782c 20 74 6f 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 69 73   to..** .** This
1782d 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 73 6b   instruction ask
1782e 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e  s if there is an
1782f 20 65 6e 74 72 79 20 69 6e 20 50 31 20 77 68 65   entry in P1 whe
17830 72 65 20 74 68 65 0a 2a 2a 20 66 69 65 6c 64 73  re the.** fields
17831 20 6d 61 74 63 68 65 73 20 4b 20 62 75 74 20 74   matches K but t
17832 68 65 20 72 6f 77 69 64 20 69 73 20 64 69 66 66  he rowid is diff
17833 65 72 65 6e 74 20 66 72 6f 6d 20 52 2e 0a 2a 2a  erent from R..**
17834 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
17835 73 75 63 68 20 65 6e 74 72 79 2c 20 74 68 65 6e  such entry, then
17836 20 74 68 65 72 65 20 69 73 20 61 6e 20 69 6d 6d   there is an imm
17837 65 64 69 61 74 65 0a 2a 2a 20 6a 75 6d 70 20 74  ediate.** jump t
17838 6f 20 50 32 2e 20 20 49 66 20 61 6e 79 20 65 6e  o P2.  If any en
17839 74 72 79 20 64 6f 65 73 20 65 78 69 73 74 20 77  try does exist w
1783a 68 65 72 65 20 74 68 65 20 69 6e 64 65 78 20 73  here the index s
1783b 74 72 69 6e 67 0a 2a 2a 20 6d 61 74 63 68 65 73  tring.** matches
1783c 20 4b 20 62 75 74 20 74 68 65 20 72 65 63 6f 72   K but the recor
1783d 64 20 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74 20  d number is not 
1783e 52 2c 20 74 68 65 6e 20 74 68 65 20 72 65 63 6f  R, then the reco
1783f 72 64 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72  rd.** number for
17840 20 74 68 61 74 20 65 6e 74 72 79 20 69 73 20 77   that entry is w
17841 72 69 74 74 65 6e 20 69 6e 74 6f 20 50 33 20 61  ritten into P3 a
17842 6e 64 20 63 6f 6e 74 72 6f 6c 0a 2a 2a 20 66 61  nd control.** fa
17843 6c 6c 73 20 74 68 72 6f 75 67 68 20 74 6f 20 74  lls through to t
17844 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74  he next instruct
17845 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  ion..**.** See a
17846 6c 73 6f 3a 20 4e 6f 74 46 6f 75 6e 64 2c 20 4e  lso: NotFound, N
17847 6f 74 45 78 69 73 74 73 2c 20 46 6f 75 6e 64 0a  otExists, Found.
17848 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 73 55 6e 69  */.case OP_IsUni
17849 71 75 65 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a  que: {        /*
1784a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 20 20   jump, in3 */.  
1784b 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b  int i = pOp->p1;
1784c 0a 20 20 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a  .  Cursor *pCx;.
1784d 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73    BtCursor *pCrs
1784e 72 3b 0a 20 20 4d 65 6d 20 2a 70 4b 3b 0a 20 20  r;.  Mem *pK;.  
1784f 69 36 34 20 52 3b 0a 0a 20 20 2f 2a 20 50 6f 70  i64 R;..  /* Pop
17850 20 74 68 65 20 76 61 6c 75 65 20 52 20 6f 66 66   the value R off
17851 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20   the top of the 
17852 73 74 61 63 6b 0a 20 20 2a 2f 0a 20 20 61 73 73  stack.  */.  ass
17853 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65  ert( pOp->p4type
17854 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20  ==P4_INT32 );.  
17855 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e  assert( pOp->p4.
17856 69 3e 30 20 26 26 20 70 4f 70 2d 3e 70 34 2e 69  i>0 && pOp->p4.i
17857 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70  <=p->nMem );.  p
17858 4b 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  K = &p->aMem[pOp
17859 2d 3e 70 34 2e 69 5d 3b 0a 20 20 73 71 6c 69 74  ->p4.i];.  sqlit
1785a 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72  e3VdbeMemInteger
1785b 69 66 79 28 70 49 6e 33 29 3b 0a 20 20 52 20 3d  ify(pIn3);.  R =
1785c 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 61 73   pIn3->u.i;.  as
1785d 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
1785e 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1785f 70 43 78 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69  pCx = p->apCsr[i
17860 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 78  ];.  assert( pCx
17861 21 3d 30 20 29 3b 0a 20 20 70 43 72 73 72 20 3d  !=0 );.  pCrsr =
17862 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 3b 0a 20   pCx->pCursor;. 
17863 20 69 66 28 20 70 43 72 73 72 21 3d 30 20 29 7b   if( pCrsr!=0 ){
17864 0a 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20  .    int res;.  
17865 20 20 69 36 34 20 76 3b 20 20 20 20 20 20 20 20    i64 v;        
17866 20 2f 2a 20 54 68 65 20 72 65 63 6f 72 64 20 6e   /* The record n
17867 75 6d 62 65 72 20 6f 6e 20 74 68 65 20 50 31 20  umber on the P1 
17868 65 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63 68  entry that match
17869 65 73 20 4b 20 2a 2f 0a 20 20 20 20 63 68 61 72  es K */.    char
1786a 20 2a 7a 4b 65 79 3b 20 20 20 20 2f 2a 20 54 68   *zKey;    /* Th
1786b 65 20 76 61 6c 75 65 20 6f 66 20 4b 20 2a 2f 0a  e value of K */.
1786c 20 20 20 20 69 6e 74 20 6e 4b 65 79 3b 20 20 20      int nKey;   
1786d 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1786e 62 79 74 65 73 20 69 6e 20 4b 20 2a 2f 0a 20 20  bytes in K */.  
1786f 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20    int len;      
17870 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
17871 74 65 73 20 69 6e 20 4b 20 77 69 74 68 6f 75 74  tes in K without
17872 20 74 68 65 20 72 6f 77 69 64 20 61 74 20 74 68   the rowid at th
17873 65 20 65 6e 64 20 2a 2f 0a 20 20 20 20 69 6e 74  e end */.    int
17874 20 73 7a 52 6f 77 69 64 3b 20 20 20 2f 2a 20 53   szRowid;   /* S
17875 69 7a 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64  ize of the rowid
17876 20 63 6f 6c 75 6d 6e 20 61 74 20 74 68 65 20 65   column at the e
17877 6e 64 20 6f 66 20 7a 4b 65 79 20 2a 2f 0a 0a 20  nd of zKey */.. 
17878 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20     /* Make sure 
17879 4b 20 69 73 20 61 20 73 74 72 69 6e 67 20 61 6e  K is a string an
1787a 64 20 6d 61 6b 65 20 7a 4b 65 79 20 70 6f 69 6e  d make zKey poin
1787b 74 20 74 6f 20 4b 0a 20 20 20 20 2a 2f 0a 20 20  t to K.    */.  
1787c 20 20 61 73 73 65 72 74 28 20 70 4b 2d 3e 66 6c    assert( pK->fl
1787d 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29  ags & MEM_Blob )
1787e 3b 0a 20 20 20 20 7a 4b 65 79 20 3d 20 70 4b 2d  ;.    zKey = pK-
1787f 3e 7a 3b 0a 20 20 20 20 6e 4b 65 79 20 3d 20 70  >z;.    nKey = p
17880 4b 2d 3e 6e 3b 0a 0a 20 20 20 20 73 7a 52 6f 77  K->n;..    szRow
17881 69 64 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  id = sqlite3Vdbe
17882 49 64 78 52 6f 77 69 64 4c 65 6e 28 28 75 38 2a  IdxRowidLen((u8*
17883 29 7a 4b 65 79 29 3b 0a 20 20 20 20 6c 65 6e 20  )zKey);.    len 
17884 3d 20 6e 4b 65 79 2d 73 7a 52 6f 77 69 64 3b 0a  = nKey-szRowid;.
17885 0a 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20 66  .    /* Search f
17886 6f 72 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 50  or an entry in P
17887 31 20 77 68 65 72 65 20 61 6c 6c 20 62 75 74 20  1 where all but 
17888 74 68 65 20 6c 61 73 74 20 66 6f 75 72 20 62 79  the last four by
17889 74 65 73 20 6d 61 74 63 68 20 4b 2e 0a 20 20 20  tes match K..   
1788a 20 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   ** If there is 
1788b 6e 6f 20 73 75 63 68 20 65 6e 74 72 79 2c 20 6a  no such entry, j
1788c 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  ump immediately 
1788d 74 6f 20 50 32 2e 0a 20 20 20 20 2a 2f 0a 20 20  to P2..    */.  
1788e 20 20 61 73 73 65 72 74 28 20 70 43 78 2d 3e 64    assert( pCx->d
1788f 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30  eferredMoveto==0
17890 20 29 3b 0a 20 20 20 20 70 43 78 2d 3e 63 61 63   );.    pCx->cac
17891 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
17892 5f 53 54 41 4c 45 3b 0a 20 20 20 20 72 63 20 3d  _STALE;.    rc =
17893 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
17894 65 74 6f 28 70 43 72 73 72 2c 20 7a 4b 65 79 2c  eto(pCrsr, zKey,
17895 20 30 2c 20 6c 65 6e 2c 20 30 2c 20 26 72 65 73   0, len, 0, &res
17896 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
17897 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
17898 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65    goto abort_due
17899 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d  _to_error;.    }
1789a 0a 20 20 20 20 69 66 28 20 72 65 73 3c 30 20 29  .    if( res<0 )
1789b 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
1789c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 70 43  ite3BtreeNext(pC
1789d 72 73 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20  rsr, &res);.    
1789e 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20    if( res ){.   
1789f 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70       pc = pOp->p
178a0 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 62  2 - 1;.        b
178a1 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
178a2 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c    }.    rc = sql
178a3 69 74 65 33 56 64 62 65 49 64 78 4b 65 79 43 6f  ite3VdbeIdxKeyCo
178a4 6d 70 61 72 65 28 70 43 78 2c 20 30 2c 20 6c 65  mpare(pCx, 0, le
178a5 6e 2c 20 28 75 38 2a 29 7a 4b 65 79 2c 20 26 72  n, (u8*)zKey, &r
178a6 65 73 29 3b 20 0a 20 20 20 20 69 66 28 20 72 63  es); .    if( rc
178a7 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
178a8 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
178a9 65 72 72 6f 72 3b 0a 20 20 20 20 69 66 28 20 72  error;.    if( r
178aa 65 73 3e 30 20 29 7b 0a 20 20 20 20 20 20 70 63  es>0 ){.      pc
178ab 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
178ac 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
178ad 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20 74 68   }..    /* At th
178ae 69 73 20 70 6f 69 6e 74 2c 20 70 43 72 73 72 20  is point, pCrsr 
178af 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61  is pointing to a
178b0 6e 20 65 6e 74 72 79 20 69 6e 20 50 31 20 77 68  n entry in P1 wh
178b1 65 72 65 20 61 6c 6c 20 62 75 74 0a 20 20 20 20  ere all but.    
178b2 2a 2a 20 74 68 65 20 66 69 6e 61 6c 20 65 6e 74  ** the final ent
178b3 72 79 20 28 74 68 65 20 72 6f 77 69 64 29 20 6d  ry (the rowid) m
178b4 61 74 63 68 65 73 20 4b 2e 20 20 43 68 65 63 6b  atches K.  Check
178b5 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 0a 20   to see if the. 
178b6 20 20 20 2a 2a 20 66 69 6e 61 6c 20 72 6f 77 69     ** final rowi
178b7 64 20 63 6f 6c 75 6d 6e 20 69 73 20 64 69 66 66  d column is diff
178b8 65 72 65 6e 74 20 66 72 6f 6d 20 52 2e 20 20 49  erent from R.  I
178b9 66 20 69 74 20 65 71 75 61 6c 73 20 52 20 74 68  f it equals R th
178ba 65 6e 20 6a 75 6d 70 0a 20 20 20 20 2a 2a 20 69  en jump.    ** i
178bb 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32  mmediately to P2
178bc 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20  ..    */.    rc 
178bd 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78  = sqlite3VdbeIdx
178be 52 6f 77 69 64 28 70 43 72 73 72 2c 20 26 76 29  Rowid(pCrsr, &v)
178bf 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
178c0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
178c1 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
178c2 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a  to_error;.    }.
178c3 20 20 20 20 69 66 28 20 76 3d 3d 52 20 29 7b 0a      if( v==R ){.
178c4 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e        pc = pOp->
178c5 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 62 72  p2 - 1;.      br
178c6 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
178c7 2f 2a 20 54 68 65 20 66 69 6e 61 6c 20 76 61 72  /* The final var
178c8 69 6e 74 20 6f 66 20 74 68 65 20 6b 65 79 20 69  int of the key i
178c9 73 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  s different from
178ca 20 52 2e 20 20 53 74 6f 72 65 20 69 74 20 62 61   R.  Store it ba
178cb 63 6b 0a 20 20 20 20 2a 2a 20 69 6e 74 6f 20 72  ck.    ** into r
178cc 65 67 69 73 74 65 72 20 52 33 2e 20 20 28 54 68  egister R3.  (Th
178cd 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  e record number 
178ce 6f 66 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74  of an entry that
178cf 20 76 69 6f 6c 61 74 65 73 0a 20 20 20 20 2a 2a   violates.    **
178d0 20 61 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72   a UNIQUE constr
178d1 61 69 6e 74 2e 29 0a 20 20 20 20 2a 2f 0a 20 20  aint.).    */.  
178d2 20 20 70 49 6e 33 2d 3e 75 2e 69 20 3d 20 76 3b    pIn3->u.i = v;
178d3 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e  .    assert( pIn
178d4 33 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74  3->flags&MEM_Int
178d5 20 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   );.  }.  break;
178d6 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e  .}../* Opcode: N
178d7 6f 74 45 78 69 73 74 73 20 50 31 20 50 32 20 50  otExists P1 P2 P
178d8 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20  3 * *.**.** Use 
178d9 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72  the content of r
178da 65 67 69 73 74 65 72 20 50 33 20 61 73 20 61 20  egister P3 as a 
178db 69 6e 74 65 67 65 72 20 6b 65 79 2e 20 20 49 66  integer key.  If
178dc 20 61 20 72 65 63 6f 72 64 20 0a 2a 2a 20 77 69   a record .** wi
178dd 74 68 20 74 68 61 74 20 6b 65 79 20 64 6f 65 73  th that key does
178de 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 20 74 61   not exist in ta
178df 62 6c 65 20 6f 66 20 50 31 2c 20 74 68 65 6e 20  ble of P1, then 
178e0 6a 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 20  jump to P2. .** 
178e1 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 64 6f  If the record do
178e2 65 73 20 65 78 69 73 74 2c 20 74 68 65 6e 20 66  es exist, then f
178e3 61 6c 6c 20 74 68 72 75 2e 20 20 54 68 65 20 63  all thru.  The c
178e4 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 0a 2a  ursor is left .*
178e5 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68  * pointing to th
178e6 65 20 72 65 63 6f 72 64 20 69 66 20 69 74 20 65  e record if it e
178e7 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  xists..**.** The
178e8 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77   difference betw
178e9 65 65 6e 20 74 68 69 73 20 6f 70 65 72 61 74 69  een this operati
178ea 6f 6e 20 61 6e 64 20 4e 6f 74 46 6f 75 6e 64 20  on and NotFound 
178eb 69 73 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20  is that this.** 
178ec 6f 70 65 72 61 74 69 6f 6e 20 61 73 73 75 6d 65  operation assume
178ed 73 20 74 68 65 20 6b 65 79 20 69 73 20 61 6e 20  s the key is an 
178ee 69 6e 74 65 67 65 72 20 61 6e 64 20 74 68 61 74  integer and that
178ef 20 50 31 20 69 73 20 61 20 74 61 62 6c 65 20 77   P1 is a table w
178f0 68 65 72 65 61 73 0a 2a 2a 20 4e 6f 74 46 6f 75  hereas.** NotFou
178f1 6e 64 20 61 73 73 75 6d 65 73 20 6b 65 79 20 69  nd assumes key i
178f2 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75  s a blob constru
178f3 63 74 65 64 20 66 72 6f 6d 20 4d 61 6b 65 52 65  cted from MakeRe
178f4 63 6f 72 64 20 61 6e 64 0a 2a 2a 20 50 31 20 69  cord and.** P1 i
178f5 73 20 61 6e 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a  s an index..**.*
178f6 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e  * See also: Foun
178f7 64 2c 20 4d 6f 76 65 54 6f 2c 20 4e 6f 74 46 6f  d, MoveTo, NotFo
178f8 75 6e 64 2c 20 49 73 55 6e 69 71 75 65 0a 2a 2f  und, IsUnique.*/
178f9 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 45 78 69 73  .case OP_NotExis
178fa 74 73 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20  ts: {        /* 
178fb 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69  jump, in3 */.  i
178fc 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a  nt i = pOp->p1;.
178fd 20 20 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20    Cursor *pC;.  
178fe 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b  BtCursor *pCrsr;
178ff 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20  .  assert( i>=0 
17900 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  && i<p->nCursor 
17901 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
17902 61 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20  apCsr[i]!=0 );. 
17903 20 69 66 28 20 28 70 43 72 73 72 20 3d 20 28 70   if( (pCrsr = (p
17904 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29  C = p->apCsr[i])
17905 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29 7b  ->pCursor)!=0 ){
17906 0a 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20  .    int res;.  
17907 20 20 75 36 34 20 69 4b 65 79 3b 0a 20 20 20 20    u64 iKey;.    
17908 61 73 73 65 72 74 28 20 70 49 6e 33 2d 3e 66 6c  assert( pIn3->fl
17909 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b  ags & MEM_Int );
1790a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
1790b 61 70 43 73 72 5b 69 5d 2d 3e 69 73 54 61 62 6c  apCsr[i]->isTabl
1790c 65 20 29 3b 0a 20 20 20 20 69 4b 65 79 20 3d 20  e );.    iKey = 
1790d 69 6e 74 54 6f 4b 65 79 28 70 49 6e 33 2d 3e 75  intToKey(pIn3->u
1790e 2e 69 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  .i);.    rc = sq
1790f 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f  lite3BtreeMoveto
17910 28 70 43 72 73 72 2c 20 30 2c 20 30 2c 20 69 4b  (pCrsr, 0, 0, iK
17911 65 79 2c 20 30 2c 26 72 65 73 29 3b 0a 20 20 20  ey, 0,&res);.   
17912 20 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d   pC->lastRowid =
17913 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20 20   pIn3->u.i;.    
17914 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64  pC->rowidIsValid
17915 20 3d 20 72 65 73 3d 3d 30 3b 0a 20 20 20 20 70   = res==0;.    p
17916 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a  C->nullRow = 0;.
17917 20 20 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61      pC->cacheSta
17918 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c  tus = CACHE_STAL
17919 45 3b 0a 20 20 20 20 2f 2a 20 72 65 73 20 6d 69  E;.    /* res mi
1791a 67 68 74 20 62 65 20 75 6e 69 6e 69 74 69 61 6c  ght be uninitial
1791b 69 7a 65 64 20 69 66 20 72 63 21 3d 53 51 4c 49  ized if rc!=SQLI
1791c 54 45 5f 4f 4b 2e 20 20 42 75 74 20 69 66 20 72  TE_OK.  But if r
1791d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 20  c!=SQLITE_OK.   
1791e 20 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 20 69   ** processing i
1791f 73 20 61 62 6f 75 74 20 74 6f 20 61 62 6f 72 74  s about to abort
17920 20 73 6f 20 77 65 20 72 65 61 6c 6c 79 20 64 6f   so we really do
17921 20 6e 6f 74 20 63 61 72 65 20 77 68 65 74 68 65   not care whethe
17922 72 20 6f 72 20 6e 6f 74 0a 20 20 20 20 2a 2a 20  r or not.    ** 
17923 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6a 75  the following ju
17924 6d 70 20 69 73 20 74 61 6b 65 6e 2e 20 20 28 49  mp is taken.  (I
17925 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 64  n other words, d
17926 6f 20 6e 6f 74 20 73 74 72 65 73 73 20 6f 76 65  o not stress ove
17927 72 0a 20 20 20 20 2a 2a 20 74 68 65 20 65 72 72  r.    ** the err
17928 6f 72 20 74 68 61 74 20 76 61 6c 67 72 69 6e 64  or that valgrind
17929 20 73 6f 6d 65 74 69 6d 65 73 20 73 68 6f 77 73   sometimes shows
1792a 20 6f 6e 20 74 68 65 20 6e 65 78 74 20 73 74 61   on the next sta
1792b 74 65 6d 65 6e 74 20 77 68 65 6e 0a 20 20 20 20  tement when.    
1792c 2a 2a 20 72 75 6e 6e 69 6e 67 20 69 6f 65 72 72  ** running ioerr
1792d 2e 74 65 73 74 20 61 6e 64 20 73 69 6d 69 6c 61  .test and simila
1792e 72 20 66 61 69 6c 75 72 65 2d 72 65 63 6f 76 65  r failure-recove
1792f 72 79 20 74 65 73 74 20 73 63 72 69 70 74 73 2e  ry test scripts.
17930 29 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 65 73  ) */.    if( res
17931 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20  !=0 ){.      pc 
17932 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
17933 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d       assert( pC-
17934 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 3d 3d 30  >rowidIsValid==0
17935 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   );.    }.  }.  
17936 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
17937 6f 64 65 3a 20 53 65 71 75 65 6e 63 65 20 50 31  ode: Sequence P1
17938 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
17939 46 69 6e 64 20 74 68 65 20 6e 65 78 74 20 61 76  Find the next av
1793a 61 69 6c 61 62 6c 65 20 73 65 71 75 65 6e 63 65  ailable sequence
1793b 20 6e 75 6d 62 65 72 20 66 6f 72 20 63 75 72 73   number for curs
1793c 6f 72 20 50 31 2e 0a 2a 2a 20 57 72 69 74 65 20  or P1..** Write 
1793d 74 68 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d  the sequence num
1793e 62 65 72 20 69 6e 74 6f 20 72 65 67 69 73 74 65  ber into registe
1793f 72 20 50 32 2e 0a 2a 2a 20 54 68 65 20 73 65 71  r P2..** The seq
17940 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 6f 6e 20  uence number on 
17941 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 69 6e  the cursor is in
17942 63 72 65 6d 65 6e 74 65 64 20 61 66 74 65 72 20  cremented after 
17943 74 68 69 73 0a 2a 2a 20 69 6e 73 74 72 75 63 74  this.** instruct
17944 69 6f 6e 2e 20 20 0a 2a 2f 0a 63 61 73 65 20 4f  ion.  .*/.case O
17945 50 5f 53 65 71 75 65 6e 63 65 3a 20 7b 20 20 20  P_Sequence: {   
17946 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d          /* out2-
17947 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20  prerelease */.  
17948 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b  int i = pOp->p1;
17949 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20  .  assert( i>=0 
1794a 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  && i<p->nCursor 
1794b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
1794c 61 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20  apCsr[i]!=0 );. 
1794d 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 70 2d 3e   pOut->u.i = p->
1794e 61 70 43 73 72 5b 69 5d 2d 3e 73 65 71 43 6f 75  apCsr[i]->seqCou
1794f 6e 74 2b 2b 3b 0a 20 20 4d 65 6d 53 65 74 54 79  nt++;.  MemSetTy
17950 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d  peFlag(pOut, MEM
17951 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  _Int);.  break;.
17952 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e  }.../* Opcode: N
17953 65 77 52 6f 77 69 64 20 50 31 20 50 32 20 50 33  ewRowid P1 P2 P3
17954 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 47 65 74 20 61   * *.**.** Get a
17955 20 6e 65 77 20 69 6e 74 65 67 65 72 20 72 65 63   new integer rec
17956 6f 72 64 20 6e 75 6d 62 65 72 20 28 61 2e 6b 2e  ord number (a.k.
17957 61 20 22 72 6f 77 69 64 22 29 20 75 73 65 64 20  a "rowid") used 
17958 61 73 20 74 68 65 20 6b 65 79 20 74 6f 20 61 20  as the key to a 
17959 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 72 65  table..** The re
1795a 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20 6e  cord number is n
1795b 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 75 73  ot previously us
1795c 65 64 20 61 73 20 61 20 6b 65 79 20 69 6e 20 74  ed as a key in t
1795d 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74  he database.** t
1795e 61 62 6c 65 20 74 68 61 74 20 63 75 72 73 6f 72  able that cursor
1795f 20 50 31 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20   P1 points to.  
17960 54 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 6e  The new record n
17961 75 6d 62 65 72 20 69 73 20 77 72 69 74 74 65 6e  umber is written
17962 0a 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 72  .** written to r
17963 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a  egister P2..**.*
17964 2a 20 49 66 20 50 33 3e 30 20 74 68 65 6e 20 50  * If P3>0 then P
17965 33 20 69 73 20 61 20 72 65 67 69 73 74 65 72 20  3 is a register 
17966 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6c  that holds the l
17967 61 72 67 65 73 74 20 70 72 65 76 69 6f 75 73 6c  argest previousl
17968 79 0a 2a 2a 20 67 65 6e 65 72 61 74 65 64 20 72  y.** generated r
17969 65 63 6f 72 64 20 6e 75 6d 62 65 72 2e 20 20 4e  ecord number.  N
1796a 6f 20 6e 65 77 20 72 65 63 6f 72 64 20 6e 75 6d  o new record num
1796b 62 65 72 73 20 61 72 65 20 61 6c 6c 6f 77 65 64  bers are allowed
1796c 20 74 6f 20 62 65 20 6c 65 73 73 0a 2a 2a 20 74   to be less.** t
1796d 68 61 6e 20 74 68 69 73 20 76 61 6c 75 65 2e 20  han this value. 
1796e 20 57 68 65 6e 20 74 68 69 73 20 76 61 6c 75 65   When this value
1796f 20 72 65 61 63 68 65 73 20 69 74 73 20 6d 61 78   reaches its max
17970 69 6d 75 6d 2c 20 61 20 53 51 4c 49 54 45 5f 46  imum, a SQLITE_F
17971 55 4c 4c 0a 2a 2a 20 65 72 72 6f 72 20 69 73 20  ULL.** error is 
17972 67 65 6e 65 72 61 74 65 64 2e 20 20 54 68 65 20  generated.  The 
17973 50 33 20 72 65 67 69 73 74 65 72 20 69 73 20 75  P3 register is u
17974 70 64 61 74 65 64 20 77 69 74 68 20 74 68 65 20  pdated with the 
17975 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 72 65 63  generated.** rec
17976 6f 72 64 20 6e 75 6d 62 65 72 2e 20 20 54 68 69  ord number.  Thi
17977 73 20 50 33 20 6d 65 63 68 61 6e 69 73 6d 20 69  s P3 mechanism i
17978 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 69  s used to help i
17979 6d 70 6c 65 6d 65 6e 74 20 74 68 65 0a 2a 2a 20  mplement the.** 
1797a 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 66 65  AUTOINCREMENT fe
1797b 61 74 75 72 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f  ature..*/.case O
1797c 50 5f 4e 65 77 52 6f 77 69 64 3a 20 7b 20 20 20  P_NewRowid: {   
1797d 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d          /* out2-
1797e 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20  prerelease */.  
1797f 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b  int i = pOp->p1;
17980 0a 20 20 69 36 34 20 76 20 3d 20 30 3b 0a 20 20  .  i64 v = 0;.  
17981 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73  Cursor *pC;.  as
17982 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
17983 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
17984 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72  assert( p->apCsr
17985 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  [i]!=0 );.  if( 
17986 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69  (pC = p->apCsr[i
17987 5d 29 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20 29  ])->pCursor==0 )
17988 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 7a 65 72  {.    /* The zer
17989 6f 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  o initialization
1798a 20 61 62 6f 76 65 20 69 73 20 61 6c 6c 20 74 68   above is all th
1798b 61 74 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a  at is needed */.
1798c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
1798d 54 68 65 20 6e 65 78 74 20 72 6f 77 69 64 20 6f  The next rowid o
1798e 72 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  r record number 
1798f 28 64 69 66 66 65 72 65 6e 74 20 74 65 72 6d 73  (different terms
17990 20 66 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 20   for the same.  
17991 20 20 2a 2a 20 74 68 69 6e 67 29 20 69 73 20 6f    ** thing) is o
17992 62 74 61 69 6e 65 64 20 69 6e 20 61 20 74 77 6f  btained in a two
17993 2d 73 74 65 70 20 61 6c 67 6f 72 69 74 68 6d 2e  -step algorithm.
17994 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46  .    **.    ** F
17995 69 72 73 74 20 77 65 20 61 74 74 65 6d 70 74 20  irst we attempt 
17996 74 6f 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67  to find the larg
17997 65 73 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77  est existing row
17998 69 64 20 61 6e 64 20 61 64 64 20 6f 6e 65 0a 20  id and add one. 
17999 20 20 20 2a 2a 20 74 6f 20 74 68 61 74 2e 20 20     ** to that.  
1799a 42 75 74 20 69 66 20 74 68 65 20 6c 61 72 67 65  But if the large
1799b 73 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69  st existing rowi
1799c 64 20 69 73 20 61 6c 72 65 61 64 79 20 74 68 65  d is already the
1799d 20 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a 20   maximum.    ** 
1799e 70 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72  positive integer
1799f 2c 20 77 65 20 68 61 76 65 20 74 6f 20 66 61 6c  , we have to fal
179a0 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65  l through to the
179a1 20 73 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20 70   second.    ** p
179a2 72 6f 62 61 62 69 6c 69 73 74 69 63 20 61 6c 67  robabilistic alg
179a3 6f 72 69 74 68 6d 0a 20 20 20 20 2a 2a 0a 20 20  orithm.    **.  
179a4 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20    ** The second 
179a5 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 74 6f 20  algorithm is to 
179a6 73 65 6c 65 63 74 20 61 20 72 6f 77 69 64 20 61  select a rowid a
179a7 74 20 72 61 6e 64 6f 6d 20 61 6e 64 20 73 65 65  t random and see
179a8 20 69 66 0a 20 20 20 20 2a 2a 20 69 74 20 61 6c   if.    ** it al
179a9 72 65 61 64 79 20 65 78 69 73 74 73 20 69 6e 20  ready exists in 
179aa 74 68 65 20 74 61 62 6c 65 2e 20 20 49 66 20 69  the table.  If i
179ab 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74  t does not exist
179ac 2c 20 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a  , we have.    **
179ad 20 73 75 63 63 65 65 64 65 64 2e 20 20 49 66 20   succeeded.  If 
179ae 74 68 65 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64  the random rowid
179af 20 64 6f 65 73 20 65 78 69 73 74 2c 20 77 65 20   does exist, we 
179b0 73 65 6c 65 63 74 20 61 20 6e 65 77 20 6f 6e 65  select a new one
179b1 0a 20 20 20 20 2a 2a 20 61 6e 64 20 74 72 79 20  .    ** and try 
179b2 61 67 61 69 6e 2c 20 75 70 20 74 6f 20 31 30 30  again, up to 100
179b3 30 20 74 69 6d 65 73 2e 0a 20 20 20 20 2a 2a 0a  0 times..    **.
179b4 20 20 20 20 2a 2a 20 46 6f 72 20 61 20 74 61 62      ** For a tab
179b5 6c 65 20 77 69 74 68 20 6c 65 73 73 20 74 68 61  le with less tha
179b6 6e 20 32 20 62 69 6c 6c 69 6f 6e 20 65 6e 74 72  n 2 billion entr
179b7 69 65 73 2c 20 74 68 65 20 70 72 6f 62 61 62 69  ies, the probabi
179b8 6c 69 74 79 0a 20 20 20 20 2a 2a 20 6f 66 20 6e  lity.    ** of n
179b9 6f 74 20 66 69 6e 64 69 6e 67 20 61 20 75 6e 75  ot finding a unu
179ba 73 65 64 20 72 6f 77 69 64 20 69 73 20 61 62 6f  sed rowid is abo
179bb 75 74 20 31 2e 30 65 2d 33 30 30 2e 20 20 54 68  ut 1.0e-300.  Th
179bc 69 73 20 69 73 20 61 20 0a 20 20 20 20 2a 2a 20  is is a .    ** 
179bd 6e 6f 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62 69  non-zero probabi
179be 6c 69 74 79 2c 20 62 75 74 20 69 74 20 69 73 20  lity, but it is 
179bf 73 74 69 6c 6c 20 76 61 6e 69 73 68 69 6e 67 6c  still vanishingl
179c0 79 20 73 6d 61 6c 6c 20 61 6e 64 20 73 68 6f 75  y small and shou
179c1 6c 64 0a 20 20 20 20 2a 2a 20 6e 65 76 65 72 20  ld.    ** never 
179c2 63 61 75 73 65 20 61 20 70 72 6f 62 6c 65 6d 2e  cause a problem.
179c3 20 20 59 6f 75 20 61 72 65 20 6d 75 63 68 2c 20    You are much, 
179c4 6d 75 63 68 20 6d 6f 72 65 20 6c 69 6b 65 6c 79  much more likely
179c5 20 74 6f 20 68 61 76 65 20 61 0a 20 20 20 20 2a   to have a.    *
179c6 2a 20 68 61 72 64 77 61 72 65 20 66 61 69 6c 75  * hardware failu
179c7 72 65 20 74 68 61 6e 20 66 6f 72 20 74 68 69 73  re than for this
179c8 20 61 6c 67 6f 72 69 74 68 6d 20 74 6f 20 66 61   algorithm to fa
179c9 69 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  il..    **.    *
179ca 2a 20 54 68 65 20 61 6e 61 6c 79 73 69 73 20 69  * The analysis i
179cb 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 70  n the previous p
179cc 61 72 61 67 72 61 70 68 20 61 73 73 75 6d 65 73  aragraph assumes
179cd 20 74 68 61 74 20 79 6f 75 20 68 61 76 65 20 61   that you have a
179ce 20 67 6f 6f 64 0a 20 20 20 20 2a 2a 20 73 6f 75   good.    ** sou
179cf 72 63 65 20 6f 66 20 72 61 6e 64 6f 6d 20 6e 75  rce of random nu
179d0 6d 62 65 72 73 2e 20 20 49 73 20 61 20 6c 69 62  mbers.  Is a lib
179d1 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 6c 69  rary function li
179d2 6b 65 20 6c 72 61 6e 64 34 38 28 29 0a 20 20 20  ke lrand48().   
179d3 20 2a 2a 20 67 6f 6f 64 20 65 6e 6f 75 67 68 3f   ** good enough?
179d4 20 20 4d 61 79 62 65 2e 20 4d 61 79 62 65 20 6e    Maybe. Maybe n
179d5 6f 74 2e 20 49 74 27 73 20 68 61 72 64 20 74 6f  ot. It's hard to
179d6 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 74 68   know whether th
179d7 65 72 65 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74  ere.    ** might
179d8 20 62 65 20 73 75 62 74 6c 65 20 62 75 67 73 20   be subtle bugs 
179d9 69 73 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e  is some implemen
179da 74 61 74 69 6f 6e 73 20 6f 66 20 6c 72 61 6e 64  tations of lrand
179db 34 38 28 29 20 74 68 61 74 0a 20 20 20 20 2a 2a  48() that.    **
179dc 20 63 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f   could cause pro
179dd 62 6c 65 6d 73 2e 20 54 6f 20 61 76 6f 69 64 20  blems. To avoid 
179de 75 6e 63 65 72 74 61 69 6e 74 79 2c 20 53 51 4c  uncertainty, SQL
179df 69 74 65 20 75 73 65 73 20 69 74 73 20 6f 77 6e  ite uses its own
179e0 20 0a 20 20 20 20 2a 2a 20 72 61 6e 64 6f 6d 20   .    ** random 
179e1 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72  number generator
179e2 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 52 43   based on the RC
179e3 34 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 20 20 20  4 algorithm..   
179e4 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 6f 20 70 72   **.    ** To pr
179e5 6f 6d 6f 74 65 20 6c 6f 63 61 6c 69 74 79 20 6f  omote locality o
179e6 66 20 72 65 66 65 72 65 6e 63 65 20 66 6f 72 20  f reference for 
179e7 72 65 70 65 74 69 74 69 76 65 20 69 6e 73 65 72  repetitive inser
179e8 74 73 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 66  ts, the.    ** f
179e9 69 72 73 74 20 66 65 77 20 61 74 74 65 6d 70 74  irst few attempt
179ea 73 20 61 74 20 63 68 6f 73 69 6e 67 20 61 20 72  s at chosing a r
179eb 61 6e 64 6f 6d 20 72 6f 77 69 64 20 70 69 63 6b  andom rowid pick
179ec 20 76 61 6c 75 65 73 20 6a 75 73 74 20 61 20 6c   values just a l
179ed 69 74 74 6c 65 0a 20 20 20 20 2a 2a 20 6c 61 72  ittle.    ** lar
179ee 67 65 72 20 74 68 61 6e 20 74 68 65 20 70 72 65  ger than the pre
179ef 76 69 6f 75 73 20 72 6f 77 69 64 2e 20 20 54 68  vious rowid.  Th
179f0 69 73 20 68 61 73 20 62 65 65 6e 20 73 68 6f 77  is has been show
179f1 6e 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 6c 79  n experimentally
179f2 0a 20 20 20 20 2a 2a 20 74 6f 20 64 6f 75 62 6c  .    ** to doubl
179f3 65 20 74 68 65 20 73 70 65 65 64 20 6f 66 20 74  e the speed of t
179f4 68 65 20 43 4f 50 59 20 6f 70 65 72 61 74 69 6f  he COPY operatio
179f5 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e  n..    */.    in
179f6 74 20 72 65 73 2c 20 72 78 3d 53 51 4c 49 54 45  t res, rx=SQLITE
179f7 5f 4f 4b 2c 20 63 6e 74 3b 0a 20 20 20 20 69 36  _OK, cnt;.    i6
179f8 34 20 78 3b 0a 20 20 20 20 63 6e 74 20 3d 20 30  4 x;.    cnt = 0
179f9 3b 0a 20 20 20 20 69 66 28 20 28 73 71 6c 69 74  ;.    if( (sqlit
179fa 65 33 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d  e3BtreeFlags(pC-
179fb 3e 70 43 75 72 73 6f 72 29 26 28 42 54 52 45 45  >pCursor)&(BTREE
179fc 5f 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f 5a 45  _INTKEY|BTREE_ZE
179fd 52 4f 44 41 54 41 29 29 20 21 3d 0a 20 20 20 20  RODATA)) !=.    
179fe 20 20 20 20 20 20 42 54 52 45 45 5f 49 4e 54 4b        BTREE_INTK
179ff 45 59 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  EY ){.      rc =
17a00 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
17a01 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f  BKPT;.      goto
17a02 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
17a03 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  ror;.    }.    a
17a04 73 73 65 72 74 28 20 28 73 71 6c 69 74 65 33 42  ssert( (sqlite3B
17a05 74 72 65 65 46 6c 61 67 73 28 70 43 2d 3e 70 43  treeFlags(pC->pC
17a06 75 72 73 6f 72 29 20 26 20 42 54 52 45 45 5f 49  ursor) & BTREE_I
17a07 4e 54 4b 45 59 29 21 3d 30 20 29 3b 0a 20 20 20  NTKEY)!=0 );.   
17a08 20 61 73 73 65 72 74 28 20 28 73 71 6c 69 74 65   assert( (sqlite
17a09 33 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d 3e  3BtreeFlags(pC->
17a0a 70 43 75 72 73 6f 72 29 20 26 20 42 54 52 45 45  pCursor) & BTREE
17a0b 5f 5a 45 52 4f 44 41 54 41 29 3d 3d 30 20 29 3b  _ZERODATA)==0 );
17a0c 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
17a0d 33 32 42 49 54 5f 52 4f 57 49 44 0a 23 20 20 20  32BIT_ROWID.#   
17a0e 64 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44  define MAX_ROWID
17a0f 20 30 78 37 66 66 66 66 66 66 66 0a 23 65 6c 73   0x7fffffff.#els
17a10 65 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f  e.    /* Some co
17a11 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 6e  mpilers complain
17a12 20 61 62 6f 75 74 20 63 6f 6e 73 74 61 6e 74 73   about constants
17a13 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 30 78 37   of the form 0x7
17a14 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 2e  fffffffffffffff.
17a15 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 73 20 63  .    ** Others c
17a16 6f 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 30 78  omplain about 0x
17a17 37 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66  7fffffffffffffff
17a18 66 4c 4c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  fLL.  The follow
17a19 69 6e 67 20 6d 61 63 72 6f 20 73 65 65 6d 73 0a  ing macro seems.
17a1a 20 20 20 20 2a 2a 20 74 6f 20 70 72 6f 76 69 64      ** to provid
17a1b 65 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 77  e the constant w
17a1c 68 69 6c 65 20 6d 61 6b 69 6e 67 20 61 6c 6c 20  hile making all 
17a1d 63 6f 6d 70 69 6c 65 72 73 20 68 61 70 70 79 2e  compilers happy.
17a1e 0a 20 20 20 20 2a 2f 0a 23 20 20 20 64 65 66 69  .    */.#   defi
17a1f 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20 20 28 20  ne MAX_ROWID  ( 
17a20 28 28 28 75 36 34 29 30 78 37 66 66 66 66 66 66  (((u64)0x7ffffff
17a21 66 29 3c 3c 33 32 29 20 7c 20 28 75 36 34 29 30  f)<<32) | (u64)0
17a22 78 66 66 66 66 66 66 66 66 20 29 0a 23 65 6e 64  xffffffff ).#end
17a23 69 66 0a 0a 20 20 20 20 69 66 28 20 21 70 43 2d  if..    if( !pC-
17a24 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 20  >useRandomRowid 
17a25 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 2d  ){.      if( pC-
17a26 3e 6e 65 78 74 52 6f 77 69 64 56 61 6c 69 64 20  >nextRowidValid 
17a27 29 7b 0a 20 20 20 20 20 20 20 20 76 20 3d 20 70  ){.        v = p
17a28 43 2d 3e 6e 65 78 74 52 6f 77 69 64 3b 0a 20 20  C->nextRowid;.  
17a29 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
17a2a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
17a2b 74 72 65 65 4c 61 73 74 28 70 43 2d 3e 70 43 75  treeLast(pC->pCu
17a2c 72 73 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20  rsor, &res);.   
17a2d 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
17a2e 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
17a2f 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64      goto abort_d
17a30 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
17a31 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
17a32 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20  f( res ){.      
17a33 20 20 20 20 76 20 3d 20 31 3b 0a 20 20 20 20 20      v = 1;.     
17a34 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
17a35 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
17a36 4b 65 79 53 69 7a 65 28 70 43 2d 3e 70 43 75 72  KeySize(pC->pCur
17a37 73 6f 72 2c 20 26 76 29 3b 0a 20 20 20 20 20 20  sor, &v);.      
17a38 20 20 20 20 76 20 3d 20 6b 65 79 54 6f 49 6e 74      v = keyToInt
17a39 28 76 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  (v);.          i
17a3a 66 28 20 76 3d 3d 4d 41 58 5f 52 4f 57 49 44 20  f( v==MAX_ROWID 
17a3b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  ){.            p
17a3c 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69  C->useRandomRowi
17a3d 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  d = 1;.         
17a3e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
17a3f 20 20 20 20 76 2b 2b 3b 0a 20 20 20 20 20 20 20      v++;.       
17a40 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
17a41 20 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20       }..#ifndef 
17a42 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
17a43 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 20 20  INCREMENT.      
17a44 69 66 28 20 70 4f 70 2d 3e 70 33 20 29 7b 0a 20  if( pOp->p3 ){. 
17a45 20 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d         Mem *pMem
17a46 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
17a47 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70  ( pOp->p3>0 && p
17a48 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p3<=p->nMem 
17a49 29 3b 20 2f 2a 20 50 33 20 69 73 20 61 20 76 61  ); /* P3 is a va
17a4a 6c 69 64 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  lid memory cell 
17a4b 2a 2f 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 20  */.        pMem 
17a4c 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  = &p->aMem[pOp->
17a4d 70 33 5d 3b 0a 09 52 45 47 49 53 54 45 52 5f 54  p3];..REGISTER_T
17a4e 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 4d  RACE(pOp->p3, pM
17a4f 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  em);.        sql
17a50 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67  ite3VdbeMemInteg
17a51 65 72 69 66 79 28 70 4d 65 6d 29 3b 0a 20 20 20  erify(pMem);.   
17a52 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 4d       assert( (pM
17a53 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
17a54 49 6e 74 29 21 3d 30 20 29 3b 20 20 2f 2a 20 6d  Int)!=0 );  /* m
17a55 65 6d 28 50 33 29 20 68 6f 6c 64 73 20 61 6e 20  em(P3) holds an 
17a56 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20  integer */.     
17a57 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 75 2e 69     if( pMem->u.i
17a58 3d 3d 4d 41 58 5f 52 4f 57 49 44 20 7c 7c 20 70  ==MAX_ROWID || p
17a59 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69  C->useRandomRowi
17a5a 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  d ){.          r
17a5b 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b  c = SQLITE_FULL;
17a5c 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
17a5d 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
17a5e 6f 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  or;.        }.  
17a5f 20 20 20 20 20 20 69 66 28 20 76 3c 70 4d 65 6d        if( v<pMem
17a60 2d 3e 75 2e 69 2b 31 20 29 7b 0a 20 20 20 20 20  ->u.i+1 ){.     
17a61 20 20 20 20 20 76 20 3d 20 70 4d 65 6d 2d 3e 75       v = pMem->u
17a62 2e 69 20 2b 20 31 3b 0a 20 20 20 20 20 20 20 20  .i + 1;.        
17a63 7d 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e  }.        pMem->
17a64 75 2e 69 20 3d 20 76 3b 0a 20 20 20 20 20 20 7d  u.i = v;.      }
17a65 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 69  .#endif..      i
17a66 66 28 20 76 3c 4d 41 58 5f 52 4f 57 49 44 20 29  f( v<MAX_ROWID )
17a67 7b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 6e 65  {.        pC->ne
17a68 78 74 52 6f 77 69 64 56 61 6c 69 64 20 3d 20 31  xtRowidValid = 1
17a69 3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 6e 65  ;.        pC->ne
17a6a 78 74 52 6f 77 69 64 20 3d 20 76 2b 31 3b 0a 20  xtRowid = v+1;. 
17a6b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
17a6c 20 20 20 20 70 43 2d 3e 6e 65 78 74 52 6f 77 69      pC->nextRowi
17a6d 64 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20  dValid = 0;.    
17a6e 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
17a6f 28 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52  ( pC->useRandomR
17a70 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 61 73  owid ){.      as
17a71 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3d 3d 30  sert( pOp->p3==0
17a72 20 29 3b 20 20 2f 2a 20 53 51 4c 49 54 45 5f 46   );  /* SQLITE_F
17a73 55 4c 4c 20 6d 75 73 74 20 68 61 76 65 20 6f 63  ULL must have oc
17a74 63 75 72 72 65 64 20 70 72 69 6f 72 20 74 6f 20  curred prior to 
17a75 74 68 69 73 20 2a 2f 0a 20 20 20 20 20 20 76 20  this */.      v 
17a76 3d 20 64 62 2d 3e 70 72 69 6f 72 4e 65 77 52 6f  = db->priorNewRo
17a77 77 69 64 3b 0a 20 20 20 20 20 20 63 6e 74 20 3d  wid;.      cnt =
17a78 20 30 3b 0a 20 20 20 20 20 20 64 6f 7b 0a 20 20   0;.      do{.  
17a79 20 20 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30        if( cnt==0
17a7a 20 26 26 20 28 76 26 30 78 66 66 66 66 66 66 29   && (v&0xffffff)
17a7b 3d 3d 76 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==v ){.         
17a7c 20 76 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65   v++;.        }e
17a7d 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73  lse{.          s
17a7e 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73  qlite3_randomnes
17a7f 73 28 73 69 7a 65 6f 66 28 76 29 2c 20 26 76 29  s(sizeof(v), &v)
17a80 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
17a81 63 6e 74 3c 35 20 29 20 76 20 26 3d 20 30 78 66  cnt<5 ) v &= 0xf
17a82 66 66 66 66 66 3b 0a 20 20 20 20 20 20 20 20 7d  fffff;.        }
17a83 0a 20 20 20 20 20 20 20 20 69 66 28 20 76 3d 3d  .        if( v==
17a84 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
17a85 20 20 20 20 20 20 78 20 3d 20 69 6e 74 54 6f 4b        x = intToK
17a86 65 79 28 76 29 3b 0a 20 20 20 20 20 20 20 20 72  ey(v);.        r
17a87 78 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  x = sqlite3Btree
17a88 4d 6f 76 65 74 6f 28 70 43 2d 3e 70 43 75 72 73  Moveto(pC->pCurs
17a89 6f 72 2c 20 30 2c 20 30 2c 20 28 75 36 34 29 78  or, 0, 0, (u64)x
17a8a 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 20  , 0, &res);.    
17a8b 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20      cnt++;.     
17a8c 20 7d 77 68 69 6c 65 28 20 63 6e 74 3c 31 30 30   }while( cnt<100
17a8d 20 26 26 20 72 78 3d 3d 53 51 4c 49 54 45 5f 4f   && rx==SQLITE_O
17a8e 4b 20 26 26 20 72 65 73 3d 3d 30 20 29 3b 0a 20  K && res==0 );. 
17a8f 20 20 20 20 20 64 62 2d 3e 70 72 69 6f 72 4e 65       db->priorNe
17a90 77 52 6f 77 69 64 20 3d 20 76 3b 0a 20 20 20 20  wRowid = v;.    
17a91 20 20 69 66 28 20 72 78 3d 3d 53 51 4c 49 54 45    if( rx==SQLITE
17a92 5f 4f 4b 20 26 26 20 72 65 73 3d 3d 30 20 29 7b  _OK && res==0 ){
17a93 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
17a94 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20  LITE_FULL;.     
17a95 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75     goto abort_du
17a96 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20  e_to_error;.    
17a97 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 43    }.    }.    pC
17a98 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d  ->rowidIsValid =
17a99 20 30 3b 0a 20 20 20 20 70 43 2d 3e 64 65 66 65   0;.    pC->defe
17a9a 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a  rredMoveto = 0;.
17a9b 20 20 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61      pC->cacheSta
17a9c 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c  tus = CACHE_STAL
17a9d 45 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54  E;.  }.  MemSetT
17a9e 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45  ypeFlag(pOut, ME
17a9f 4d 5f 49 6e 74 29 3b 0a 20 20 70 4f 75 74 2d 3e  M_Int);.  pOut->
17aa0 75 2e 69 20 3d 20 76 3b 0a 20 20 62 72 65 61 6b  u.i = v;.  break
17aa1 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
17aa2 49 6e 73 65 72 74 20 50 31 20 50 32 20 50 33 20  Insert P1 P2 P3 
17aa3 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 57 72 69 74  P4 P5.**.** Writ
17aa4 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20  e an entry into 
17aa5 74 68 65 20 74 61 62 6c 65 20 6f 66 20 63 75 72  the table of cur
17aa6 73 6f 72 20 50 31 2e 20 20 41 20 6e 65 77 20 65  sor P1.  A new e
17aa7 6e 74 72 79 20 69 73 0a 2a 2a 20 63 72 65 61 74  ntry is.** creat
17aa8 65 64 20 69 66 20 69 74 20 64 6f 65 73 6e 27 74  ed if it doesn't
17aa9 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 20 6f   already exist o
17aaa 72 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 61  r the data for a
17aab 6e 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 65 6e  n existing.** en
17aac 74 72 79 20 69 73 20 6f 76 65 72 77 72 69 74 74  try is overwritt
17aad 65 6e 2e 20 20 54 68 65 20 64 61 74 61 20 69 73  en.  The data is
17aae 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65   the value store
17aaf 64 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6e 75  d register.** nu
17ab0 6d 62 65 72 20 50 32 2e 20 54 68 65 20 6b 65 79  mber P2. The key
17ab1 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65   is stored in re
17ab2 67 69 73 74 65 72 20 50 33 2e 20 54 68 65 20 6b  gister P3. The k
17ab3 65 79 20 6d 75 73 74 0a 2a 2a 20 62 65 20 61 6e  ey must.** be an
17ab4 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20   integer..**.** 
17ab5 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 4e 43  If the OPFLAG_NC
17ab6 48 41 4e 47 45 20 66 6c 61 67 20 6f 66 20 50 35  HANGE flag of P5
17ab7 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68   is set, then th
17ab8 65 20 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75  e row change cou
17ab9 6e 74 20 69 73 0a 2a 2a 20 69 6e 63 72 65 6d 65  nt is.** increme
17aba 6e 74 65 64 20 28 6f 74 68 65 72 77 69 73 65 20  nted (otherwise 
17abb 6e 6f 74 29 2e 20 20 49 66 20 74 68 65 20 4f 50  not).  If the OP
17abc 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 20 66  FLAG_LASTROWID f
17abd 6c 61 67 20 6f 66 20 50 35 20 69 73 20 73 65 74  lag of P5 is set
17abe 2c 0a 2a 2a 20 74 68 65 6e 20 72 6f 77 69 64 20  ,.** then rowid 
17abf 69 73 20 73 74 6f 72 65 64 20 66 6f 72 20 73 75  is stored for su
17ac0 62 73 65 71 75 65 6e 74 20 72 65 74 75 72 6e 20  bsequent return 
17ac1 62 79 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65  by the.** sqlite
17ac2 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
17ac3 77 69 64 28 29 20 66 75 6e 63 74 69 6f 6e 20 28  wid() function (
17ac4 6f 74 68 65 72 77 69 73 65 20 69 74 20 69 73 20  otherwise it is 
17ac5 75 6e 6d 6f 64 69 66 69 65 64 29 2e 0a 2a 2a 0a  unmodified)..**.
17ac6 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 50 34 20  ** Parameter P4 
17ac7 6d 61 79 20 70 6f 69 6e 74 20 74 6f 20 61 20 73  may point to a s
17ac8 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67  tring containing
17ac9 20 74 68 65 20 74 61 62 6c 65 2d 6e 61 6d 65 2c   the table-name,
17aca 20 6f 72 0a 2a 2a 20 6d 61 79 20 62 65 20 4e 55   or.** may be NU
17acb 4c 4c 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74  LL. If it is not
17acc 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20   NULL, then the 
17acd 75 70 64 61 74 65 2d 68 6f 6f 6b 20 0a 2a 2a 20  update-hook .** 
17ace 28 73 71 6c 69 74 65 33 2e 78 55 70 64 61 74 65  (sqlite3.xUpdate
17acf 43 61 6c 6c 62 61 63 6b 29 20 69 73 20 69 6e 76  Callback) is inv
17ad0 6f 6b 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61  oked following a
17ad1 20 73 75 63 63 65 73 73 66 75 6c 20 69 6e 73 65   successful inse
17ad2 72 74 2e 0a 2a 2a 0a 2a 2a 20 28 57 41 52 4e 49  rt..**.** (WARNI
17ad3 4e 47 2f 54 4f 44 4f 3a 20 49 66 20 50 31 20 69  NG/TODO: If P1 i
17ad4 73 20 61 20 70 73 65 75 64 6f 2d 63 75 72 73 6f  s a pseudo-curso
17ad5 72 20 61 6e 64 20 50 32 20 69 73 20 64 79 6e 61  r and P2 is dyna
17ad6 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61 6c 6c 6f 63  mically.** alloc
17ad7 61 74 65 64 2c 20 74 68 65 6e 20 6f 77 6e 65 72  ated, then owner
17ad8 73 68 69 70 20 6f 66 20 50 32 20 69 73 20 74 72  ship of P2 is tr
17ad9 61 6e 73 66 65 72 72 65 64 20 74 6f 20 74 68 65  ansferred to the
17ada 20 70 73 65 75 64 6f 2d 63 75 72 73 6f 72 0a 2a   pseudo-cursor.*
17adb 2a 20 61 6e 64 20 72 65 67 69 73 74 65 72 20 50  * and register P
17adc 32 20 62 65 63 6f 6d 65 73 20 65 70 68 65 6d 65  2 becomes epheme
17add 72 61 6c 2e 20 20 49 66 20 74 68 65 20 63 75 72  ral.  If the cur
17ade 73 6f 72 20 69 73 20 63 68 61 6e 67 65 64 2c 20  sor is changed, 
17adf 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20  the.** value of 
17ae0 72 65 67 69 73 74 65 72 20 50 32 20 77 69 6c 6c  register P2 will
17ae1 20 74 68 65 6e 20 63 68 61 6e 67 65 2e 20 20 4d   then change.  M
17ae2 61 6b 65 20 73 75 72 65 20 74 68 69 73 20 64 6f  ake sure this do
17ae3 65 73 20 6e 6f 74 0a 2a 2a 20 63 61 75 73 65 20  es not.** cause 
17ae4 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 29 0a 2a  any problems.).*
17ae5 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75  *.** This instru
17ae6 63 74 69 6f 6e 20 6f 6e 6c 79 20 77 6f 72 6b 73  ction only works
17ae7 20 6f 6e 20 74 61 62 6c 65 73 2e 20 20 54 68 65   on tables.  The
17ae8 20 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 73 74   equivalent inst
17ae9 72 75 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 69  ruction.** for i
17aea 6e 64 69 63 65 73 20 69 73 20 4f 50 5f 49 64 78  ndices is OP_Idx
17aeb 49 6e 73 65 72 74 2e 0a 2a 2f 0a 63 61 73 65 20  Insert..*/.case 
17aec 4f 50 5f 49 6e 73 65 72 74 3a 20 7b 0a 20 20 4d  OP_Insert: {.  M
17aed 65 6d 20 2a 70 44 61 74 61 20 3d 20 26 70 2d 3e  em *pData = &p->
17aee 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20  aMem[pOp->p2];. 
17aef 20 4d 65 6d 20 2a 70 4b 65 79 20 3d 20 26 70 2d   Mem *pKey = &p-
17af0 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  >aMem[pOp->p3];.
17af1 0a 20 20 69 36 34 20 69 4b 65 79 3b 20 20 20 2f  .  i64 iKey;   /
17af2 2a 20 54 68 65 20 69 6e 74 65 67 65 72 20 52 4f  * The integer RO
17af3 57 49 44 20 6f 72 20 6b 65 79 20 66 6f 72 20 74  WID or key for t
17af4 68 65 20 72 65 63 6f 72 64 20 74 6f 20 62 65 20  he record to be 
17af5 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 69 6e  inserted */.  in
17af6 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20  t i = pOp->p1;. 
17af7 20 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61   Cursor *pC;.  a
17af8 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69  ssert( i>=0 && i
17af9 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
17afa 20 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69   pC = p->apCsr[i
17afb 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 21  ];.  assert( pC!
17afc 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
17afd 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 7c  pC->pCursor!=0 |
17afe 7c 20 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c  | pC->pseudoTabl
17aff 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  e );.  assert( p
17b00 4b 65 79 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Key->flags & MEM
17b01 5f 49 6e 74 20 29 3b 0a 20 20 61 73 73 65 72 74  _Int );.  assert
17b02 28 20 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b  ( pC->isTable );
17b03 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43  .  REGISTER_TRAC
17b04 45 28 70 4f 70 2d 3e 70 32 2c 20 70 44 61 74 61  E(pOp->p2, pData
17b05 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52  );.  REGISTER_TR
17b06 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 4b 65  ACE(pOp->p3, pKe
17b07 79 29 3b 0a 0a 20 20 69 4b 65 79 20 3d 20 69 6e  y);..  iKey = in
17b08 74 54 6f 4b 65 79 28 70 4b 65 79 2d 3e 75 2e 69  tToKey(pKey->u.i
17b09 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35  );.  if( pOp->p5
17b0a 20 26 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47   & OPFLAG_NCHANG
17b0b 45 20 29 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b  E ) p->nChange++
17b0c 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20  ;.  if( pOp->p5 
17b0d 26 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57  & OPFLAG_LASTROW
17b0e 49 44 20 29 20 64 62 2d 3e 6c 61 73 74 52 6f 77  ID ) db->lastRow
17b0f 69 64 20 3d 20 70 4b 65 79 2d 3e 75 2e 69 3b 0a  id = pKey->u.i;.
17b10 20 20 69 66 28 20 70 43 2d 3e 6e 65 78 74 52 6f    if( pC->nextRo
17b11 77 69 64 56 61 6c 69 64 20 26 26 20 70 4b 65 79  widValid && pKey
17b12 2d 3e 75 2e 69 3e 3d 70 43 2d 3e 6e 65 78 74 52  ->u.i>=pC->nextR
17b13 6f 77 69 64 20 29 7b 0a 20 20 20 20 70 43 2d 3e  owid ){.    pC->
17b14 6e 65 78 74 52 6f 77 69 64 56 61 6c 69 64 20 3d  nextRowidValid =
17b15 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 44   0;.  }.  if( pD
17b16 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  ata->flags & MEM
17b17 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 70 44 61  _Null ){.    pDa
17b18 74 61 2d 3e 7a 20 3d 20 30 3b 0a 20 20 20 20 70  ta->z = 0;.    p
17b19 44 61 74 61 2d 3e 6e 20 3d 20 30 3b 0a 20 20 7d  Data->n = 0;.  }
17b1a 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
17b1b 28 20 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 26  ( pData->flags &
17b1c 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53   (MEM_Blob|MEM_S
17b1d 74 72 29 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28  tr) );.  }.  if(
17b1e 20 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65   pC->pseudoTable
17b1f 20 29 7b 0a 20 20 20 20 69 66 28 20 21 70 43 2d   ){.    if( !pC-
17b20 3e 65 70 68 65 6d 50 73 65 75 64 6f 54 61 62 6c  >ephemPseudoTabl
17b21 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  e ){.      sqlit
17b22 65 33 5f 66 72 65 65 28 70 43 2d 3e 70 44 61 74  e3_free(pC->pDat
17b23 61 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43  a);.    }.    pC
17b24 2d 3e 69 4b 65 79 20 3d 20 69 4b 65 79 3b 0a 20  ->iKey = iKey;. 
17b25 20 20 20 70 43 2d 3e 6e 44 61 74 61 20 3d 20 70     pC->nData = p
17b26 44 61 74 61 2d 3e 6e 3b 0a 20 20 20 20 69 66 28  Data->n;.    if(
17b27 20 70 44 61 74 61 2d 3e 7a 3d 3d 70 44 61 74 61   pData->z==pData
17b28 2d 3e 7a 4d 61 6c 6c 6f 63 20 7c 7c 20 70 43 2d  ->zMalloc || pC-
17b29 3e 65 70 68 65 6d 50 73 65 75 64 6f 54 61 62 6c  >ephemPseudoTabl
17b2a 65 20 29 7b 0a 20 20 20 20 20 20 70 43 2d 3e 70  e ){.      pC->p
17b2b 44 61 74 61 20 3d 20 70 44 61 74 61 2d 3e 7a 3b  Data = pData->z;
17b2c 0a 20 20 20 20 20 20 69 66 28 20 21 70 43 2d 3e  .      if( !pC->
17b2d 65 70 68 65 6d 50 73 65 75 64 6f 54 61 62 6c 65  ephemPseudoTable
17b2e 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 61 74   ){.        pDat
17b2f 61 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d  a->flags &= ~MEM
17b30 5f 44 79 6e 3b 0a 20 20 20 20 20 20 20 20 70 44  _Dyn;.        pD
17b31 61 74 61 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  ata->flags |= ME
17b32 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20 20 20  M_Ephem;.       
17b33 20 70 44 61 74 61 2d 3e 7a 4d 61 6c 6c 6f 63 20   pData->zMalloc 
17b34 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
17b35 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 43   }else{.      pC
17b36 2d 3e 70 44 61 74 61 20 3d 20 73 71 6c 69 74 65  ->pData = sqlite
17b37 33 5f 6d 61 6c 6c 6f 63 28 20 70 43 2d 3e 6e 44  3_malloc( pC->nD
17b38 61 74 61 2b 32 20 29 3b 0a 20 20 20 20 20 20 69  ata+2 );.      i
17b39 66 28 20 21 70 43 2d 3e 70 44 61 74 61 20 29 20  f( !pC->pData ) 
17b3a 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20  goto no_mem;.   
17b3b 20 20 20 6d 65 6d 63 70 79 28 70 43 2d 3e 70 44     memcpy(pC->pD
17b3c 61 74 61 2c 20 70 44 61 74 61 2d 3e 7a 2c 20 70  ata, pData->z, p
17b3d 43 2d 3e 6e 44 61 74 61 29 3b 0a 20 20 20 20 20  C->nData);.     
17b3e 20 70 43 2d 3e 70 44 61 74 61 5b 70 43 2d 3e 6e   pC->pData[pC->n
17b3f 44 61 74 61 5d 20 3d 20 30 3b 0a 20 20 20 20 20  Data] = 0;.     
17b40 20 70 43 2d 3e 70 44 61 74 61 5b 70 43 2d 3e 6e   pC->pData[pC->n
17b41 44 61 74 61 2b 31 5d 20 3d 20 30 3b 0a 20 20 20  Data+1] = 0;.   
17b42 20 7d 0a 20 20 20 20 70 43 2d 3e 6e 75 6c 6c 52   }.    pC->nullR
17b43 6f 77 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  ow = 0;.  }else{
17b44 0a 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 0a  .    int nZero;.
17b45 20 20 20 20 69 66 28 20 70 44 61 74 61 2d 3e 66      if( pData->f
17b46 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20  lags & MEM_Zero 
17b47 29 7b 0a 20 20 20 20 20 20 6e 5a 65 72 6f 20 3d  ){.      nZero =
17b48 20 70 44 61 74 61 2d 3e 75 2e 69 3b 0a 20 20 20   pData->u.i;.   
17b49 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 5a   }else{.      nZ
17b4a 65 72 6f 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  ero = 0;.    }. 
17b4b 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
17b4c 74 72 65 65 49 6e 73 65 72 74 28 70 43 2d 3e 70  treeInsert(pC->p
17b4d 43 75 72 73 6f 72 2c 20 30 2c 20 69 4b 65 79 2c  Cursor, 0, iKey,
17b4e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
17b4f 20 20 20 20 20 20 20 20 20 20 20 20 20 70 44 61               pDa
17b50 74 61 2d 3e 7a 2c 20 70 44 61 74 61 2d 3e 6e 2c  ta->z, pData->n,
17b51 20 6e 5a 65 72 6f 2c 0a 20 20 20 20 20 20 20 20   nZero,.        
17b52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17b53 20 20 20 20 70 4f 70 2d 3e 70 35 20 26 20 4f 50      pOp->p5 & OP
17b54 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20  FLAG_APPEND);.  
17b55 7d 0a 20 20 0a 20 20 70 43 2d 3e 72 6f 77 69 64  }.  .  pC->rowid
17b56 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 70  IsValid = 0;.  p
17b57 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  C->deferredMovet
17b58 6f 20 3d 20 30 3b 0a 20 20 70 43 2d 3e 63 61 63  o = 0;.  pC->cac
17b59 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
17b5a 5f 53 54 41 4c 45 3b 0a 0a 20 20 2f 2a 20 49 6e  _STALE;..  /* In
17b5b 76 6f 6b 65 20 74 68 65 20 75 70 64 61 74 65 2d  voke the update-
17b5c 68 6f 6f 6b 20 69 66 20 72 65 71 75 69 72 65 64  hook if required
17b5d 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53  . */.  if( rc==S
17b5e 51 4c 49 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e  QLITE_OK && db->
17b5f 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 20  xUpdateCallback 
17b60 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a  && pOp->p4.z ){.
17b61 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
17b62 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 70 43  zDb = db->aDb[pC
17b63 2d 3e 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  ->iDb].zName;.  
17b64 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
17b65 62 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a  bl = pOp->p4.z;.
17b66 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 28 28 70      int op = ((p
17b67 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f  Op->p5 & OPFLAG_
17b68 49 53 55 50 44 41 54 45 29 20 3f 20 53 51 4c 49  ISUPDATE) ? SQLI
17b69 54 45 5f 55 50 44 41 54 45 20 3a 20 53 51 4c 49  TE_UPDATE : SQLI
17b6a 54 45 5f 49 4e 53 45 52 54 29 3b 0a 20 20 20 20  TE_INSERT);.    
17b6b 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 54 61  assert( pC->isTa
17b6c 62 6c 65 20 29 3b 0a 20 20 20 20 64 62 2d 3e 78  ble );.    db->x
17b6d 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 28 64  UpdateCallback(d
17b6e 62 2d 3e 70 55 70 64 61 74 65 41 72 67 2c 20 6f  b->pUpdateArg, o
17b6f 70 2c 20 7a 44 62 2c 20 7a 54 62 6c 2c 20 69 4b  p, zDb, zTbl, iK
17b70 65 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ey);.    assert(
17b71 20 70 43 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a 20   pC->iDb>=0 );. 
17b72 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
17b73 2a 20 4f 70 63 6f 64 65 3a 20 44 65 6c 65 74 65  * Opcode: Delete
17b74 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a   P1 P2 * P4 *.**
17b75 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 72  .** Delete the r
17b76 65 63 6f 72 64 20 61 74 20 77 68 69 63 68 20 74  ecord at which t
17b77 68 65 20 50 31 20 63 75 72 73 6f 72 20 69 73 20  he P1 cursor is 
17b78 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69  currently pointi
17b79 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 75  ng..**.** The cu
17b7a 72 73 6f 72 20 77 69 6c 6c 20 62 65 20 6c 65 66  rsor will be lef
17b7b 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 65 69  t pointing at ei
17b7c 74 68 65 72 20 74 68 65 20 6e 65 78 74 20 6f 72  ther the next or
17b7d 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a 2a 2a   the previous.**
17b7e 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65 20 74   record in the t
17b7f 61 62 6c 65 2e 20 49 66 20 69 74 20 69 73 20 6c  able. If it is l
17b80 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  eft pointing at 
17b81 74 68 65 20 6e 65 78 74 20 72 65 63 6f 72 64 2c  the next record,
17b82 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 78   then.** the nex
17b83 74 20 4e 65 78 74 20 69 6e 73 74 72 75 63 74 69  t Next instructi
17b84 6f 6e 20 77 69 6c 6c 20 62 65 20 61 20 6e 6f 2d  on will be a no-
17b85 6f 70 2e 20 20 48 65 6e 63 65 20 69 74 20 69 73  op.  Hence it is
17b86 20 4f 4b 20 74 6f 20 64 65 6c 65 74 65 0a 2a 2a   OK to delete.**
17b87 20 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 77   a record from w
17b88 69 74 68 69 6e 20 61 6e 20 4e 65 78 74 20 6c 6f  ithin an Next lo
17b89 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  op..**.** If the
17b8a 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20   OPFLAG_NCHANGE 
17b8b 66 6c 61 67 20 6f 66 20 50 32 20 69 73 20 73 65  flag of P2 is se
17b8c 74 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77 20  t, then the row 
17b8d 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 69 73 0a  change count is.
17b8e 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 28  ** incremented (
17b8f 6f 74 68 65 72 77 69 73 65 20 6e 6f 74 29 2e 0a  otherwise not)..
17b90 2a 2a 0a 2a 2a 20 50 31 20 6d 75 73 74 20 6e 6f  **.** P1 must no
17b91 74 20 62 65 20 70 73 65 75 64 6f 2d 74 61 62 6c  t be pseudo-tabl
17b92 65 2e 20 20 49 74 20 68 61 73 20 74 6f 20 62 65  e.  It has to be
17b93 20 61 20 72 65 61 6c 20 74 61 62 6c 65 20 77 69   a real table wi
17b94 74 68 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 72  th.** multiple r
17b95 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34  ows..**.** If P4
17b96 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   is not NULL, th
17b97 65 6e 20 69 74 20 69 73 20 74 68 65 20 6e 61 6d  en it is the nam
17b98 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  e of the table t
17b99 68 61 74 20 50 31 20 69 73 0a 2a 2a 20 70 6f 69  hat P1 is.** poi
17b9a 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 20 75  nting to.  The u
17b9b 70 64 61 74 65 20 68 6f 6f 6b 20 77 69 6c 6c 20  pdate hook will 
17b9c 62 65 20 69 6e 76 6f 6b 65 64 2c 20 69 66 20 69  be invoked, if i
17b9d 74 20 65 78 69 73 74 73 2e 0a 2a 2a 20 49 66 20  t exists..** If 
17b9e 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74  P4 is not NULL t
17b9f 68 65 6e 20 74 68 65 20 50 31 20 63 75 72 73 6f  hen the P1 curso
17ba0 72 20 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e  r must have been
17ba1 20 70 6f 73 69 74 69 6f 6e 65 64 0a 2a 2a 20 75   positioned.** u
17ba2 73 69 6e 67 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64  sing OP_NotFound
17ba3 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69   prior to invoki
17ba4 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 0a  ng this opcode..
17ba5 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 65 6c 65 74  */.case OP_Delet
17ba6 65 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70  e: {.  int i = p
17ba7 4f 70 2d 3e 70 31 3b 0a 20 20 69 36 34 20 69 4b  Op->p1;.  i64 iK
17ba8 65 79 3b 0a 20 20 43 75 72 73 6f 72 20 2a 70 43  ey;.  Cursor *pC
17ba9 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d  ;..  assert( i>=
17baa 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f  0 && i<p->nCurso
17bab 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61  r );.  pC = p->a
17bac 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72  pCsr[i];.  asser
17bad 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 61 73  t( pC!=0 );.  as
17bae 73 65 72 74 28 20 70 43 2d 3e 70 43 75 72 73 6f  sert( pC->pCurso
17baf 72 21 3d 30 20 29 3b 20 20 2f 2a 20 4f 6e 6c 79  r!=0 );  /* Only
17bb0 20 76 61 6c 69 64 20 66 6f 72 20 72 65 61 6c 20   valid for real 
17bb1 74 61 62 6c 65 73 2c 20 6e 6f 20 70 73 65 75 64  tables, no pseud
17bb2 6f 74 61 62 6c 65 73 20 2a 2f 0a 0a 20 20 2f 2a  otables */..  /*
17bb3 20 49 66 20 74 68 65 20 75 70 64 61 74 65 2d 68   If the update-h
17bb4 6f 6f 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f  ook will be invo
17bb5 6b 65 64 2c 20 73 65 74 20 69 4b 65 79 20 74 6f  ked, set iKey to
17bb6 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68   the rowid of th
17bb7 65 0a 20 20 2a 2a 20 72 6f 77 20 62 65 69 6e 67  e.  ** row being
17bb8 20 64 65 6c 65 74 65 64 2e 0a 20 20 2a 2f 0a 20   deleted..  */. 
17bb9 20 69 66 28 20 64 62 2d 3e 78 55 70 64 61 74 65   if( db->xUpdate
17bba 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d  Callback && pOp-
17bbb 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 61 73 73  >p4.z ){.    ass
17bbc 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c 65  ert( pC->isTable
17bbd 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
17bbe 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64  pC->rowidIsValid
17bbf 20 29 3b 20 20 2f 2a 20 6c 61 73 74 52 6f 77 69   );  /* lastRowi
17bc0 64 20 73 65 74 20 62 79 20 70 72 65 76 69 6f 75  d set by previou
17bc1 73 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 2a 2f  s OP_NotFound */
17bc2 0a 20 20 20 20 69 4b 65 79 20 3d 20 70 43 2d 3e  .    iKey = pC->
17bc3 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 7d 0a 0a  lastRowid;.  }..
17bc4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
17bc5 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 70  beCursorMoveto(p
17bc6 43 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67  C);.  if( rc ) g
17bc7 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
17bc8 5f 65 72 72 6f 72 3b 0a 20 20 72 63 20 3d 20 73  _error;.  rc = s
17bc9 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74  qlite3BtreeDelet
17bca 65 28 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a  e(pC->pCursor);.
17bcb 20 20 70 43 2d 3e 6e 65 78 74 52 6f 77 69 64 56    pC->nextRowidV
17bcc 61 6c 69 64 20 3d 20 30 3b 0a 20 20 70 43 2d 3e  alid = 0;.  pC->
17bcd 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41  cacheStatus = CA
17bce 43 48 45 5f 53 54 41 4c 45 3b 0a 0a 20 20 2f 2a  CHE_STALE;..  /*
17bcf 20 49 6e 76 6f 6b 65 20 74 68 65 20 75 70 64 61   Invoke the upda
17bd0 74 65 2d 68 6f 6f 6b 20 69 66 20 72 65 71 75 69  te-hook if requi
17bd1 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63  red. */.  if( rc
17bd2 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 64  ==SQLITE_OK && d
17bd3 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 61  b->xUpdateCallba
17bd4 63 6b 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 20  ck && pOp->p4.z 
17bd5 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ){.    const cha
17bd6 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62  r *zDb = db->aDb
17bd7 5b 70 43 2d 3e 69 44 62 5d 2e 7a 4e 61 6d 65 3b  [pC->iDb].zName;
17bd8 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
17bd9 2a 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e  *zTbl = pOp->p4.
17bda 7a 3b 0a 20 20 20 20 64 62 2d 3e 78 55 70 64 61  z;.    db->xUpda
17bdb 74 65 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70  teCallback(db->p
17bdc 55 70 64 61 74 65 41 72 67 2c 20 53 51 4c 49 54  UpdateArg, SQLIT
17bdd 45 5f 44 45 4c 45 54 45 2c 20 7a 44 62 2c 20 7a  E_DELETE, zDb, z
17bde 54 62 6c 2c 20 69 4b 65 79 29 3b 0a 20 20 20 20  Tbl, iKey);.    
17bdf 61 73 73 65 72 74 28 20 70 43 2d 3e 69 44 62 3e  assert( pC->iDb>
17be0 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  =0 );.  }.  if( 
17be1 70 4f 70 2d 3e 70 32 20 26 20 4f 50 46 4c 41 47  pOp->p2 & OPFLAG
17be2 5f 4e 43 48 41 4e 47 45 20 29 20 70 2d 3e 6e 43  _NCHANGE ) p->nC
17be3 68 61 6e 67 65 2b 2b 3b 0a 20 20 62 72 65 61 6b  hange++;.  break
17be4 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
17be5 52 65 73 65 74 43 6f 75 6e 74 20 50 31 20 2a 20  ResetCount P1 * 
17be6 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63  *.**.** This opc
17be7 6f 64 65 20 72 65 73 65 74 73 20 74 68 65 20 56  ode resets the V
17be8 4d 73 20 69 6e 74 65 72 6e 61 6c 20 63 68 61 6e  Ms internal chan
17be9 67 65 20 63 6f 75 6e 74 65 72 20 74 6f 20 30 2e  ge counter to 0.
17bea 20 49 66 20 50 31 20 69 73 20 74 72 75 65 2c 0a   If P1 is true,.
17beb 2a 2a 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75  ** then the valu
17bec 65 20 6f 66 20 74 68 65 20 63 68 61 6e 67 65 20  e of the change 
17bed 63 6f 75 6e 74 65 72 20 69 73 20 63 6f 70 69 65  counter is copie
17bee 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  d to the databas
17bef 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 63 68 61 6e  e handle.** chan
17bf0 67 65 20 63 6f 75 6e 74 65 72 20 28 72 65 74 75  ge counter (retu
17bf1 72 6e 65 64 20 62 79 20 73 75 62 73 65 71 75 65  rned by subseque
17bf2 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69  nt calls to sqli
17bf3 74 65 33 5f 63 68 61 6e 67 65 73 28 29 29 0a 2a  te3_changes()).*
17bf4 2a 20 62 65 66 6f 72 65 20 69 74 20 69 73 20 72  * before it is r
17bf5 65 73 65 74 2e 20 54 68 69 73 20 69 73 20 75 73  eset. This is us
17bf6 65 64 20 62 79 20 74 72 69 67 67 65 72 20 70 72  ed by trigger pr
17bf7 6f 67 72 61 6d 73 2e 0a 2a 2f 0a 63 61 73 65 20  ograms..*/.case 
17bf8 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 3a 20 7b  OP_ResetCount: {
17bf9 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 20 29  .  if( pOp->p1 )
17bfa 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
17bfb 65 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c 20  eSetChanges(db, 
17bfc 70 2d 3e 6e 43 68 61 6e 67 65 29 3b 0a 20 20 7d  p->nChange);.  }
17bfd 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20  .  p->nChange = 
17bfe 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  0;.  break;.}../
17bff 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 44 61 74  * Opcode: RowDat
17c00 61 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  a P1 P2 * * *.**
17c01 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 72  .** Write into r
17c02 65 67 69 73 74 65 72 20 50 32 20 74 68 65 20 63  egister P2 the c
17c03 6f 6d 70 6c 65 74 65 20 72 6f 77 20 64 61 74 61  omplete row data
17c04 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e 0a   for cursor P1..
17c05 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 69  ** There is no i
17c06 6e 74 65 72 70 72 65 74 61 74 69 6f 6e 20 6f 66  nterpretation of
17c07 20 74 68 65 20 64 61 74 61 2e 20 20 0a 2a 2a 20   the data.  .** 
17c08 49 74 20 69 73 20 6a 75 73 74 20 63 6f 70 69 65  It is just copie
17c09 64 20 6f 6e 74 6f 20 74 68 65 20 50 32 20 72 65  d onto the P2 re
17c0a 67 69 73 74 65 72 20 65 78 61 63 74 6c 79 20 61  gister exactly a
17c0b 73 20 0a 2a 2a 20 69 74 20 69 73 20 66 6f 75 6e  s .** it is foun
17c0c 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  d in the databas
17c0d 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  e file..**.** If
17c0e 20 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d   the P1 cursor m
17c0f 75 73 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20  ust be pointing 
17c10 74 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 20 28  to a valid row (
17c11 6e 6f 74 20 61 20 4e 55 4c 4c 20 72 6f 77 29 0a  not a NULL row).
17c12 2a 2a 20 6f 66 20 61 20 72 65 61 6c 20 74 61 62  ** of a real tab
17c13 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f  le, not a pseudo
17c14 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a 20 4f 70  -table..*/./* Op
17c15 63 6f 64 65 3a 20 52 6f 77 4b 65 79 20 50 31 20  code: RowKey P1 
17c16 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57  P2 * * *.**.** W
17c17 72 69 74 65 20 69 6e 74 6f 20 72 65 67 69 73 74  rite into regist
17c18 65 72 20 50 32 20 74 68 65 20 63 6f 6d 70 6c 65  er P2 the comple
17c19 74 65 20 72 6f 77 20 6b 65 79 20 66 6f 72 20 63  te row key for c
17c1a 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 54 68 65  ursor P1..** The
17c1b 72 65 20 69 73 20 6e 6f 20 69 6e 74 65 72 70 72  re is no interpr
17c1c 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64  etation of the d
17c1d 61 74 61 2e 20 20 0a 2a 2a 20 54 68 65 20 6b 65  ata.  .** The ke
17c1e 79 20 69 73 20 63 6f 70 69 65 64 20 6f 6e 74 6f  y is copied onto
17c1f 20 74 68 65 20 50 33 20 72 65 67 69 73 74 65 72   the P3 register
17c20 20 65 78 61 63 74 6c 79 20 61 73 20 0a 2a 2a 20   exactly as .** 
17c21 69 74 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 74  it is found in t
17c22 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
17c23 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50  ..**.** If the P
17c24 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65  1 cursor must be
17c25 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 76   pointing to a v
17c26 61 6c 69 64 20 72 6f 77 20 28 6e 6f 74 20 61 20  alid row (not a 
17c27 4e 55 4c 4c 20 72 6f 77 29 0a 2a 2a 20 6f 66 20  NULL row).** of 
17c28 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f  a real table, no
17c29 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  t a pseudo-table
17c2a 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77  ..*/.case OP_Row
17c2b 4b 65 79 3a 0a 63 61 73 65 20 4f 50 5f 52 6f 77  Key:.case OP_Row
17c2c 44 61 74 61 3a 20 7b 0a 20 20 69 6e 74 20 69 20  Data: {.  int i 
17c2d 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 43 75 72  = pOp->p1;.  Cur
17c2e 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72  sor *pC;.  BtCur
17c2f 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 75 33  sor *pCrsr;.  u3
17c30 32 20 6e 3b 0a 0a 20 20 70 4f 75 74 20 3d 20 26  2 n;..  pOut = &
17c31 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d  p->aMem[pOp->p2]
17c32 3b 0a 0a 20 20 2f 2a 20 4e 6f 74 65 20 74 68 61  ;..  /* Note tha
17c33 74 20 52 6f 77 4b 65 79 20 61 6e 64 20 52 6f 77  t RowKey and Row
17c34 44 61 74 61 20 61 72 65 20 72 65 61 6c 6c 79 20  Data are really 
17c35 65 78 61 63 74 6c 79 20 74 68 65 20 73 61 6d 65  exactly the same
17c36 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a   instruction */.
17c37 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
17c38 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  & i<p->nCursor )
17c39 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 73  ;.  pC = p->apCs
17c3a 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  r[i];.  assert( 
17c3b 70 43 2d 3e 69 73 54 61 62 6c 65 20 7c 7c 20 70  pC->isTable || p
17c3c 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52  Op->opcode==OP_R
17c3d 6f 77 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 72  owKey );.  asser
17c3e 74 28 20 70 43 2d 3e 69 73 49 6e 64 65 78 20 7c  t( pC->isIndex |
17c3f 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  | pOp->opcode==O
17c40 50 5f 52 6f 77 44 61 74 61 20 29 3b 0a 20 20 61  P_RowData );.  a
17c41 73 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a  ssert( pC!=0 );.
17c42 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 6e 75    assert( pC->nu
17c43 6c 6c 52 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73  llRow==0 );.  as
17c44 73 65 72 74 28 20 70 43 2d 3e 70 73 65 75 64 6f  sert( pC->pseudo
17c45 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 61 73  Table==0 );.  as
17c46 73 65 72 74 28 20 70 43 2d 3e 70 43 75 72 73 6f  sert( pC->pCurso
17c47 72 21 3d 30 20 29 3b 0a 20 20 70 43 72 73 72 20  r!=0 );.  pCrsr 
17c48 3d 20 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20  = pC->pCursor;. 
17c49 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
17c4a 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 70 43  eCursorMoveto(pC
17c4b 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f  );.  if( rc ) go
17c4c 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
17c4d 65 72 72 6f 72 3b 0a 20 20 69 66 28 20 70 43 2d  error;.  if( pC-
17c4e 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20  >isIndex ){.    
17c4f 69 36 34 20 6e 36 34 3b 0a 20 20 20 20 61 73 73  i64 n64;.    ass
17c50 65 72 74 28 20 21 70 43 2d 3e 69 73 54 61 62 6c  ert( !pC->isTabl
17c51 65 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  e );.    sqlite3
17c52 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 72  BtreeKeySize(pCr
17c53 73 72 2c 20 26 6e 36 34 29 3b 0a 20 20 20 20 69  sr, &n64);.    i
17c54 66 28 20 6e 36 34 3e 64 62 2d 3e 61 4c 69 6d 69  f( n64>db->aLimi
17c55 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
17c56 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 20 20  ENGTH] ){.      
17c57 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20  goto too_big;.  
17c58 20 20 7d 0a 20 20 20 20 6e 20 3d 20 6e 36 34 3b    }.    n = n64;
17c59 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
17c5a 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 53 69  lite3BtreeDataSi
17c5b 7a 65 28 70 43 72 73 72 2c 20 26 6e 29 3b 0a 20  ze(pCrsr, &n);. 
17c5c 20 20 20 69 66 28 20 6e 3e 64 62 2d 3e 61 4c 69     if( n>db->aLi
17c5d 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
17c5e 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20  _LENGTH] ){.    
17c5f 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a    goto too_big;.
17c60 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
17c61 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
17c62 6f 77 28 70 4f 75 74 2c 20 6e 2c 20 30 29 20 29  ow(pOut, n, 0) )
17c63 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65  {.    goto no_me
17c64 6d 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 6e  m;.  }.  pOut->n
17c65 20 3d 20 6e 3b 0a 20 20 4d 65 6d 53 65 74 54 79   = n;.  MemSetTy
17c66 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d  peFlag(pOut, MEM
17c67 5f 42 6c 6f 62 29 3b 0a 20 20 69 66 28 20 70 43  _Blob);.  if( pC
17c68 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20  ->isIndex ){.   
17c69 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
17c6a 65 65 4b 65 79 28 70 43 72 73 72 2c 20 30 2c 20  eeKey(pCrsr, 0, 
17c6b 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d  n, pOut->z);.  }
17c6c 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73  else{.    rc = s
17c6d 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28  qlite3BtreeData(
17c6e 70 43 72 73 72 2c 20 30 2c 20 6e 2c 20 70 4f 75  pCrsr, 0, n, pOu
17c6f 74 2d 3e 7a 29 3b 0a 20 20 7d 0a 20 20 70 4f 75  t->z);.  }.  pOu
17c70 74 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f  t->enc = SQLITE_
17c71 55 54 46 38 3b 20 20 2f 2a 20 49 6e 20 63 61 73  UTF8;  /* In cas
17c72 65 20 74 68 65 20 62 6c 6f 62 20 69 73 20 65 76  e the blob is ev
17c73 65 72 20 63 61 73 74 20 74 6f 20 74 65 78 74 20  er cast to text 
17c74 2a 2f 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f  */.  UPDATE_MAX_
17c75 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a  BLOBSIZE(pOut);.
17c76 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
17c77 70 63 6f 64 65 3a 20 52 6f 77 69 64 20 50 31 20  pcode: Rowid P1 
17c78 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53  P2 * * *.**.** S
17c79 74 6f 72 65 20 69 6e 20 72 65 67 69 73 74 65 72  tore in register
17c7a 20 50 32 20 61 6e 20 69 6e 74 65 67 65 72 20 77   P2 an integer w
17c7b 68 69 63 68 20 69 73 20 74 68 65 20 6b 65 79 20  hich is the key 
17c7c 6f 66 20 74 68 65 20 74 61 62 6c 65 20 65 6e 74  of the table ent
17c7d 72 79 20 74 68 61 74 0a 2a 2a 20 50 31 20 69 73  ry that.** P1 is
17c7e 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
17c7f 20 74 6f 2e 20 20 49 66 20 70 32 3d 3d 30 20 74   to.  If p2==0 t
17c80 68 65 6e 20 70 75 73 68 20 74 68 65 20 69 6e 74  hen push the int
17c81 65 67 65 72 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  eger..*/.case OP
17c82 5f 52 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20  _Rowid: {       
17c83 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74            /* out
17c84 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a  2-prerelease */.
17c85 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70    int i = pOp->p
17c86 31 3b 0a 20 20 43 75 72 73 6f 72 20 2a 70 43 3b  1;.  Cursor *pC;
17c87 0a 20 20 69 36 34 20 76 3b 0a 0a 20 20 61 73 73  .  i64 v;..  ass
17c88 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70  ert( i>=0 && i<p
17c89 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70  ->nCursor );.  p
17c8a 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b  C = p->apCsr[i];
17c8b 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d 30  .  assert( pC!=0
17c8c 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   );.  rc = sqlit
17c8d 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65  e3VdbeCursorMove
17c8e 74 6f 28 70 43 29 3b 0a 20 20 69 66 28 20 72 63  to(pC);.  if( rc
17c8f 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
17c90 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 69 66  e_to_error;.  if
17c91 28 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c  ( pC->rowidIsVal
17c92 69 64 20 29 7b 0a 20 20 20 20 76 20 3d 20 70 43  id ){.    v = pC
17c93 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 7d  ->lastRowid;.  }
17c94 65 6c 73 65 20 69 66 28 20 70 43 2d 3e 70 73 65  else if( pC->pse
17c95 75 64 6f 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  udoTable ){.    
17c96 76 20 3d 20 6b 65 79 54 6f 49 6e 74 28 70 43 2d  v = keyToInt(pC-
17c97 3e 69 4b 65 79 29 3b 0a 20 20 7d 65 6c 73 65 20  >iKey);.  }else 
17c98 69 66 28 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20  if( pC->nullRow 
17c99 29 7b 0a 20 20 20 20 2f 2a 20 4c 65 61 76 65 20  ){.    /* Leave 
17c9a 74 68 65 20 72 6f 77 69 64 20 73 65 74 20 74 6f  the rowid set to
17c9b 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 62   a NULL */.    b
17c9c 72 65 61 6b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  reak;.  }else{. 
17c9d 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 70     assert( pC->p
17c9e 43 75 72 73 6f 72 21 3d 30 20 29 3b 0a 20 20 20  Cursor!=0 );.   
17c9f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79   sqlite3BtreeKey
17ca0 53 69 7a 65 28 70 43 2d 3e 70 43 75 72 73 6f 72  Size(pC->pCursor
17ca1 2c 20 26 76 29 3b 0a 20 20 20 20 76 20 3d 20 6b  , &v);.    v = k
17ca2 65 79 54 6f 49 6e 74 28 76 29 3b 0a 20 20 7d 0a  eyToInt(v);.  }.
17ca3 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 76 3b    pOut->u.i = v;
17ca4 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61  .  MemSetTypeFla
17ca5 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29  g(pOut, MEM_Int)
17ca6 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
17ca7 20 4f 70 63 6f 64 65 3a 20 4e 75 6c 6c 52 6f 77   Opcode: NullRow
17ca8 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a   P1 * * * *.**.*
17ca9 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f  * Move the curso
17caa 72 20 50 31 20 74 6f 20 61 20 6e 75 6c 6c 20 72  r P1 to a null r
17cab 6f 77 2e 20 20 41 6e 79 20 4f 50 5f 43 6f 6c 75  ow.  Any OP_Colu
17cac 6d 6e 20 6f 70 65 72 61 74 69 6f 6e 73 0a 2a 2a  mn operations.**
17cad 20 74 68 61 74 20 6f 63 63 75 72 20 77 68 69 6c   that occur whil
17cae 65 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  e the cursor is 
17caf 6f 6e 20 74 68 65 20 6e 75 6c 6c 20 72 6f 77 20  on the null row 
17cb0 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 77  will always.** w
17cb1 72 69 74 65 20 61 20 4e 55 4c 4c 2e 0a 2a 2f 0a  rite a NULL..*/.
17cb2 63 61 73 65 20 4f 50 5f 4e 75 6c 6c 52 6f 77 3a  case OP_NullRow:
17cb3 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70   {.  int i = pOp
17cb4 2d 3e 70 31 3b 0a 20 20 43 75 72 73 6f 72 20 2a  ->p1;.  Cursor *
17cb5 70 43 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69  pC;..  assert( i
17cb6 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72  >=0 && i<p->nCur
17cb7 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d  sor );.  pC = p-
17cb8 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73  >apCsr[i];.  ass
17cb9 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20  ert( pC!=0 );.  
17cba 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b  pC->nullRow = 1;
17cbb 0a 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61  .  pC->rowidIsVa
17cbc 6c 69 64 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b  lid = 0;.  break
17cbd 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
17cbe 4c 61 73 74 20 50 31 20 50 32 20 2a 20 2a 20 2a  Last P1 P2 * * *
17cbf 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20  .**.** The next 
17cc0 75 73 65 20 6f 66 20 74 68 65 20 52 6f 77 69 64  use of the Rowid
17cc1 20 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65   or Column or Ne
17cc2 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66  xt instruction f
17cc3 6f 72 20 50 31 20 0a 2a 2a 20 77 69 6c 6c 20 72  or P1 .** will r
17cc4 65 66 65 72 20 74 6f 20 74 68 65 20 6c 61 73 74  efer to the last
17cc5 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61   entry in the da
17cc6 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20  tabase table or 
17cc7 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 74 68 65  index..** If the
17cc8 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20   table or index 
17cc9 69 73 20 65 6d 70 74 79 20 61 6e 64 20 50 32 3e  is empty and P2>
17cca 30 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d  0, then jump imm
17ccb 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a  ediately to P2..
17ccc 2a 2a 20 49 66 20 50 32 20 69 73 20 30 20 6f 72  ** If P2 is 0 or
17ccd 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72   if the table or
17cce 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20 65 6d   index is not em
17ccf 70 74 79 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67  pty, fall throug
17cd0 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c  h.** to the foll
17cd1 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f  owing instructio
17cd2 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 61  n..*/.case OP_La
17cd3 73 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20  st: {        /* 
17cd4 6a 75 6d 70 20 2a 2f 0a 20 20 69 6e 74 20 69 20  jump */.  int i 
17cd5 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 43 75 72  = pOp->p1;.  Cur
17cd6 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72  sor *pC;.  BtCur
17cd7 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e  sor *pCrsr;.  in
17cd8 74 20 72 65 73 3b 0a 0a 20 20 61 73 73 65 72 74  t res;..  assert
17cd9 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e  ( i>=0 && i<p->n
17cda 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d  Cursor );.  pC =
17cdb 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20   p->apCsr[i];.  
17cdc 61 73 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b  assert( pC!=0 );
17cdd 0a 20 20 70 43 72 73 72 20 3d 20 70 43 2d 3e 70  .  pCrsr = pC->p
17cde 43 75 72 73 6f 72 3b 0a 20 20 61 73 73 65 72 74  Cursor;.  assert
17cdf 28 20 70 43 72 73 72 21 3d 30 20 29 3b 0a 20 20  ( pCrsr!=0 );.  
17ce0 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
17ce1 65 4c 61 73 74 28 70 43 72 73 72 2c 20 26 72 65  eLast(pCrsr, &re
17ce2 73 29 3b 0a 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f  s);.  pC->nullRo
17ce3 77 20 3d 20 72 65 73 3b 0a 20 20 70 43 2d 3e 64  w = res;.  pC->d
17ce4 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20  eferredMoveto = 
17ce5 30 3b 0a 20 20 70 43 2d 3e 63 61 63 68 65 53 74  0;.  pC->cacheSt
17ce6 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41  atus = CACHE_STA
17ce7 4c 45 3b 0a 20 20 69 66 28 20 72 65 73 20 26 26  LE;.  if( res &&
17ce8 20 70 4f 70 2d 3e 70 32 3e 30 20 29 7b 0a 20 20   pOp->p2>0 ){.  
17ce9 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
17cea 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   1;.  }.  break;
17ceb 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  .}.../* Opcode: 
17cec 53 6f 72 74 20 50 31 20 50 32 20 2a 20 2a 20 2a  Sort P1 P2 * * *
17ced 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f  .**.** This opco
17cee 64 65 20 64 6f 65 73 20 65 78 61 63 74 6c 79 20  de does exactly 
17cef 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 20 61  the same thing a
17cf0 73 20 4f 50 5f 52 65 77 69 6e 64 20 65 78 63 65  s OP_Rewind exce
17cf1 70 74 20 74 68 61 74 0a 2a 2a 20 69 74 20 69 6e  pt that.** it in
17cf2 63 72 65 6d 65 6e 74 73 20 61 6e 20 75 6e 64 6f  crements an undo
17cf3 63 75 6d 65 6e 74 65 64 20 67 6c 6f 62 61 6c 20  cumented global 
17cf4 76 61 72 69 61 62 6c 65 20 75 73 65 64 20 66 6f  variable used fo
17cf5 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a  r testing..**.**
17cf6 20 53 6f 72 74 69 6e 67 20 69 73 20 61 63 63 6f   Sorting is acco
17cf7 6d 70 6c 69 73 68 65 64 20 62 79 20 77 72 69 74  mplished by writ
17cf8 69 6e 67 20 72 65 63 6f 72 64 73 20 69 6e 74 6f  ing records into
17cf9 20 61 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78   a sorting index
17cfa 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 77 69 6e 64  ,.** then rewind
17cfb 69 6e 67 20 74 68 61 74 20 69 6e 64 65 78 20 61  ing that index a
17cfc 6e 64 20 70 6c 61 79 69 6e 67 20 69 74 20 62 61  nd playing it ba
17cfd 63 6b 20 66 72 6f 6d 20 62 65 67 69 6e 6e 69 6e  ck from beginnin
17cfe 67 20 74 6f 0a 2a 2a 20 65 6e 64 2e 20 20 57 65  g to.** end.  We
17cff 20 75 73 65 20 74 68 65 20 4f 50 5f 53 6f 72 74   use the OP_Sort
17d00 20 6f 70 63 6f 64 65 20 69 6e 73 74 65 61 64 20   opcode instead 
17d01 6f 66 20 4f 50 5f 52 65 77 69 6e 64 20 74 6f 20  of OP_Rewind to 
17d02 64 6f 20 74 68 65 0a 2a 2a 20 72 65 77 69 6e 64  do the.** rewind
17d03 69 6e 67 20 73 6f 20 74 68 61 74 20 74 68 65 20  ing so that the 
17d04 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
17d05 77 69 6c 6c 20 62 65 20 69 6e 63 72 65 6d 65 6e  will be incremen
17d06 74 65 64 20 61 6e 64 0a 2a 2a 20 72 65 67 72 65  ted and.** regre
17d07 73 73 69 6f 6e 20 74 65 73 74 73 20 63 61 6e 20  ssion tests can 
17d08 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65  determine whethe
17d09 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6f 70 74  r or not the opt
17d0a 69 6d 69 7a 65 72 20 69 73 0a 2a 2a 20 63 6f 72  imizer is.** cor
17d0b 72 65 63 74 6c 79 20 6f 70 74 69 6d 69 7a 69 6e  rectly optimizin
17d0c 67 20 6f 75 74 20 73 6f 72 74 73 2e 0a 2a 2f 0a  g out sorts..*/.
17d0d 63 61 73 65 20 4f 50 5f 53 6f 72 74 3a 20 7b 20  case OP_Sort: { 
17d0e 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a         /* jump *
17d0f 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
17d10 54 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 73  TEST.  sqlite3_s
17d11 6f 72 74 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 73  ort_count++;.  s
17d12 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f  qlite3_search_co
17d13 75 6e 74 2d 2d 3b 0a 23 65 6e 64 69 66 0a 20 20  unt--;.#endif.  
17d14 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20  /* Fall through 
17d15 69 6e 74 6f 20 4f 50 5f 52 65 77 69 6e 64 20 2a  into OP_Rewind *
17d16 2f 0a 7d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52  /.}./* Opcode: R
17d17 65 77 69 6e 64 20 50 31 20 50 32 20 2a 20 2a 20  ewind P1 P2 * * 
17d18 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74  *.**.** The next
17d19 20 75 73 65 20 6f 66 20 74 68 65 20 52 6f 77 69   use of the Rowi
17d1a 64 20 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e  d or Column or N
17d1b 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ext instruction 
17d1c 66 6f 72 20 50 31 20 0a 2a 2a 20 77 69 6c 6c 20  for P1 .** will 
17d1d 72 65 66 65 72 20 74 6f 20 74 68 65 20 66 69 72  refer to the fir
17d1e 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  st entry in the 
17d1f 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f  database table o
17d20 72 20 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 74  r index..** If t
17d21 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65  he table or inde
17d22 78 20 69 73 20 65 6d 70 74 79 20 61 6e 64 20 50  x is empty and P
17d23 32 3e 30 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69  2>0, then jump i
17d24 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32  mmediately to P2
17d25 2e 0a 2a 2a 20 49 66 20 50 32 20 69 73 20 30 20  ..** If P2 is 0 
17d26 6f 72 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  or if the table 
17d27 6f 72 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20  or index is not 
17d28 65 6d 70 74 79 2c 20 66 61 6c 6c 20 74 68 72 6f  empty, fall thro
17d29 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f  ugh.** to the fo
17d2a 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74  llowing instruct
17d2b 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ion..*/.case OP_
17d2c 52 65 77 69 6e 64 3a 20 7b 20 20 20 20 20 20 20  Rewind: {       
17d2d 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 69 6e   /* jump */.  in
17d2e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20  t i = pOp->p1;. 
17d2f 20 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42   Cursor *pC;.  B
17d30 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
17d31 20 20 69 6e 74 20 72 65 73 3b 0a 0a 20 20 61 73    int res;..  as
17d32 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
17d33 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
17d34 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d  pC = p->apCsr[i]
17d35 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d  ;.  assert( pC!=
17d36 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43 72 73  0 );.  if( (pCrs
17d37 72 20 3d 20 70 43 2d 3e 70 43 75 72 73 6f 72 29  r = pC->pCursor)
17d38 21 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  !=0 ){.    rc = 
17d39 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 73  sqlite3BtreeFirs
17d3a 74 28 70 43 72 73 72 2c 20 26 72 65 73 29 3b 0a  t(pCrsr, &res);.
17d3b 20 20 20 20 70 43 2d 3e 61 74 46 69 72 73 74 20      pC->atFirst 
17d3c 3d 20 72 65 73 3d 3d 30 3b 0a 20 20 20 20 70 43  = res==0;.    pC
17d3d 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f  ->deferredMoveto
17d3e 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61   = 0;.    pC->ca
17d3f 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48  cheStatus = CACH
17d40 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 65 6c 73 65  E_STALE;.  }else
17d41 7b 0a 20 20 20 20 72 65 73 20 3d 20 31 3b 0a 20  {.    res = 1;. 
17d42 20 7d 0a 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77   }.  pC->nullRow
17d43 20 3d 20 72 65 73 3b 0a 20 20 61 73 73 65 72 74   = res;.  assert
17d44 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70  ( pOp->p2>0 && p
17d45 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4f 70 20 29 3b  Op->p2<p->nOp );
17d46 0a 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20  .  if( res ){.  
17d47 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
17d48 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   1;.  }.  break;
17d49 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e  .}../* Opcode: N
17d4a 65 78 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  ext P1 P2 * * *.
17d4b 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 63 75  **.** Advance cu
17d4c 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20  rsor P1 so that 
17d4d 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  it points to the
17d4e 20 6e 65 78 74 20 6b 65 79 2f 64 61 74 61 20 70   next key/data p
17d4f 61 69 72 20 69 6e 20 69 74 73 0a 2a 2a 20 74 61  air in its.** ta
17d50 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 20 20 49  ble or index.  I
17d51 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6d  f there are no m
17d52 6f 72 65 20 6b 65 79 2f 76 61 6c 75 65 20 70 61  ore key/value pa
17d53 69 72 73 20 74 68 65 6e 20 66 61 6c 6c 20 74 68  irs then fall th
17d54 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20  rough.** to the 
17d55 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75  following instru
17d56 63 74 69 6f 6e 2e 20 20 42 75 74 20 69 66 20 74  ction.  But if t
17d57 68 65 20 63 75 72 73 6f 72 20 61 64 76 61 6e 63  he cursor advanc
17d58 65 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c  e was successful
17d59 2c 0a 2a 2a 20 6a 75 6d 70 20 69 6d 6d 65 64 69  ,.** jump immedi
17d5a 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a  ately to P2..**.
17d5b 2a 2a 20 54 68 65 20 50 31 20 63 75 72 73 6f 72  ** The P1 cursor
17d5c 20 6d 75 73 74 20 62 65 20 66 6f 72 20 61 20 72   must be for a r
17d5d 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61  eal table, not a
17d5e 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a   pseudo-table..*
17d5f 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 50  *.** See also: P
17d60 72 65 76 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  rev.*/./* Opcode
17d61 3a 20 50 72 65 76 20 50 31 20 50 32 20 2a 20 2a  : Prev P1 P2 * *
17d62 20 2a 0a 2a 2a 0a 2a 2a 20 42 61 63 6b 20 75 70   *.**.** Back up
17d63 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68   cursor P1 so th
17d64 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20  at it points to 
17d65 74 68 65 20 70 72 65 76 69 6f 75 73 20 6b 65 79  the previous key
17d66 2f 64 61 74 61 20 70 61 69 72 20 69 6e 20 69 74  /data pair in it
17d67 73 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 69 6e  s.** table or in
17d68 64 65 78 2e 20 20 49 66 20 74 68 65 72 65 20 69  dex.  If there i
17d69 73 20 6e 6f 20 70 72 65 76 69 6f 75 73 20 6b 65  s no previous ke
17d6a 79 2f 76 61 6c 75 65 20 70 61 69 72 73 20 74 68  y/value pairs th
17d6b 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a  en fall through.
17d6c 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77  ** to the follow
17d6d 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  ing instruction.
17d6e 20 20 42 75 74 20 69 66 20 74 68 65 20 63 75 72    But if the cur
17d6f 73 6f 72 20 62 61 63 6b 75 70 20 77 61 73 20 73  sor backup was s
17d70 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 6a 75  uccessful,.** ju
17d71 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74  mp immediately t
17d72 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  o P2..**.** The 
17d73 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62  P1 cursor must b
17d74 65 20 66 6f 72 20 61 20 72 65 61 6c 20 74 61 62  e for a real tab
17d75 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f  le, not a pseudo
17d76 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20  -table..*/.case 
17d77 4f 50 5f 50 72 65 76 3a 20 20 20 20 20 20 20 20  OP_Prev:        
17d78 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 63 61 73    /* jump */.cas
17d79 65 20 4f 50 5f 4e 65 78 74 3a 20 7b 20 20 20 20  e OP_Next: {    
17d7a 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20      /* jump */. 
17d7b 20 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42   Cursor *pC;.  B
17d7c 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
17d7d 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54  .  CHECK_FOR_INT
17d7e 45 52 52 55 50 54 3b 0a 20 20 61 73 73 65 72 74  ERRUPT;.  assert
17d7f 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20  ( pOp->p1>=0 && 
17d80 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73  pOp->p1<p->nCurs
17d81 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e  or );.  pC = p->
17d82 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  apCsr[pOp->p1];.
17d83 20 20 69 66 28 20 70 43 3d 3d 30 20 29 7b 0a 20    if( pC==0 ){. 
17d84 20 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20 53 65     break;  /* Se
17d85 65 20 74 69 63 6b 65 74 20 23 32 32 37 33 20 2a  e ticket #2273 *
17d86 2f 0a 20 20 7d 0a 20 20 70 43 72 73 72 20 3d 20  /.  }.  pCrsr = 
17d87 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 61  pC->pCursor;.  a
17d88 73 73 65 72 74 28 20 70 43 72 73 72 20 29 3b 0a  ssert( pCrsr );.
17d89 20 20 69 66 28 20 70 43 2d 3e 6e 75 6c 6c 52 6f    if( pC->nullRo
17d8a 77 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  w==0 ){.    int 
17d8b 72 65 73 20 3d 20 31 3b 0a 20 20 20 20 61 73 73  res = 1;.    ass
17d8c 65 72 74 28 20 70 43 2d 3e 64 65 66 65 72 72 65  ert( pC->deferre
17d8d 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20  dMoveto==0 );.  
17d8e 20 20 72 63 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f    rc = pOp->opco
17d8f 64 65 3d 3d 4f 50 5f 4e 65 78 74 20 3f 20 73 71  de==OP_Next ? sq
17d90 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 70  lite3BtreeNext(p
17d91 43 72 73 72 2c 20 26 72 65 73 29 20 3a 0a 20 20  Crsr, &res) :.  
17d92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17d93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
17d94 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f  lite3BtreePrevio
17d95 75 73 28 70 43 72 73 72 2c 20 26 72 65 73 29 3b  us(pCrsr, &res);
17d96 0a 20 20 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77  .    pC->nullRow
17d97 20 3d 20 72 65 73 3b 0a 20 20 20 20 70 43 2d 3e   = res;.    pC->
17d98 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41  cacheStatus = CA
17d99 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 69  CHE_STALE;.    i
17d9a 66 28 20 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20  f( res==0 ){.   
17d9b 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
17d9c 2d 20 31 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  - 1;.#ifdef SQLI
17d9d 54 45 5f 54 45 53 54 0a 20 20 20 20 20 20 73 71  TE_TEST.      sq
17d9e 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75  lite3_search_cou
17d9f 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20  nt++;.#endif.   
17da0 20 7d 0a 20 20 7d 0a 20 20 70 43 2d 3e 72 6f 77   }.  }.  pC->row
17da1 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20  idIsValid = 0;. 
17da2 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
17da3 63 6f 64 65 3a 20 49 64 78 49 6e 73 65 72 74 20  code: IdxInsert 
17da4 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
17da5 2a 2a 20 52 65 67 69 73 74 65 72 20 50 32 20 68  ** Register P2 h
17da6 6f 6c 64 73 20 61 20 53 51 4c 20 69 6e 64 65 78  olds a SQL index
17da7 20 6b 65 79 20 6d 61 64 65 20 75 73 69 6e 67 20   key made using 
17da8 74 68 65 0a 2a 2a 20 4d 61 6b 65 49 64 78 52 65  the.** MakeIdxRe
17da9 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20  c instructions. 
17daa 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 72 69   This opcode wri
17dab 74 65 73 20 74 68 61 74 20 6b 65 79 0a 2a 2a 20  tes that key.** 
17dac 69 6e 74 6f 20 74 68 65 20 69 6e 64 65 78 20 50  into the index P
17dad 31 2e 20 20 44 61 74 61 20 66 6f 72 20 74 68 65  1.  Data for the
17dae 20 65 6e 74 72 79 20 69 73 20 6e 69 6c 2e 0a 2a   entry is nil..*
17daf 2a 0a 2a 2a 20 50 33 20 69 73 20 61 20 66 6c 61  *.** P3 is a fla
17db0 67 20 74 68 61 74 20 70 72 6f 76 69 64 65 73 20  g that provides 
17db1 61 20 68 69 6e 74 20 74 6f 20 74 68 65 20 62 2d  a hint to the b-
17db2 74 72 65 65 20 6c 61 79 65 72 20 74 68 61 74 20  tree layer that 
17db3 74 68 69 73 0a 2a 2a 20 69 6e 73 65 72 74 20 69  this.** insert i
17db4 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61  s likely to be a
17db5 6e 20 61 70 70 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20  n append..**.** 
17db6 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  This instruction
17db7 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20   only works for 
17db8 69 6e 64 69 63 65 73 2e 20 20 54 68 65 20 65 71  indices.  The eq
17db9 75 69 76 61 6c 65 6e 74 20 69 6e 73 74 72 75 63  uivalent instruc
17dba 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 61 62 6c  tion.** for tabl
17dbb 65 73 20 69 73 20 4f 50 5f 49 6e 73 65 72 74 2e  es is OP_Insert.
17dbc 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 49  .*/.case OP_IdxI
17dbd 6e 73 65 72 74 3a 20 7b 20 20 20 20 20 20 20 20  nsert: {        
17dbe 2f 2a 20 69 6e 32 20 2a 2f 0a 20 20 69 6e 74 20  /* in2 */.  int 
17dbf 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 43  i = pOp->p1;.  C
17dc0 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43  ursor *pC;.  BtC
17dc1 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20  ursor *pCrsr;.  
17dc2 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
17dc3 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  i<p->nCursor );.
17dc4 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43    assert( p->apC
17dc5 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 61 73  sr[i]!=0 );.  as
17dc6 73 65 72 74 28 20 70 49 6e 32 2d 3e 66 6c 61 67  sert( pIn2->flag
17dc7 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a  s & MEM_Blob );.
17dc8 20 20 69 66 28 20 28 70 43 72 73 72 20 3d 20 28    if( (pCrsr = (
17dc9 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d  pC = p->apCsr[i]
17dca 29 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29  )->pCursor)!=0 )
17dcb 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  {.    assert( pC
17dcc 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a  ->isTable==0 );.
17dcd 20 20 20 20 72 63 20 3d 20 45 78 70 61 6e 64 42      rc = ExpandB
17dce 6c 6f 62 28 70 49 6e 32 29 3b 0a 20 20 20 20 69  lob(pIn2);.    i
17dcf 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
17dd0 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4b   ){.      int nK
17dd1 65 79 20 3d 20 70 49 6e 32 2d 3e 6e 3b 0a 20 20  ey = pIn2->n;.  
17dd2 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
17dd3 7a 4b 65 79 20 3d 20 70 49 6e 32 2d 3e 7a 3b 0a  zKey = pIn2->z;.
17dd4 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
17dd5 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 70 43  e3BtreeInsert(pC
17dd6 72 73 72 2c 20 7a 4b 65 79 2c 20 6e 4b 65 79 2c  rsr, zKey, nKey,
17dd7 20 22 22 2c 20 30 2c 20 30 2c 20 70 4f 70 2d 3e   "", 0, 0, pOp->
17dd8 70 33 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  p3);.      asser
17dd9 74 28 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d  t( pC->deferredM
17dda 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20  oveto==0 );.    
17ddb 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75    pC->cacheStatu
17ddc 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b  s = CACHE_STALE;
17ddd 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65  .    }.  }.  bre
17dde 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
17ddf 3a 20 49 64 78 44 65 6c 65 74 65 4d 20 50 31 20  : IdxDeleteM P1 
17de0 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  P2 P3 * *.**.** 
17de1 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 50  The content of P
17de2 33 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72  3 registers star
17de3 74 69 6e 67 20 61 74 20 72 65 67 69 73 74 65 72  ting at register
17de4 20 50 32 20 66 6f 72 6d 0a 2a 2a 20 61 6e 20 75   P2 form.** an u
17de5 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65  npacked index ke
17de6 79 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20 72  y. This opcode r
17de7 65 6d 6f 76 65 73 20 74 68 61 74 20 65 6e 74 72  emoves that entr
17de8 79 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 69  y from the .** i
17de9 6e 64 65 78 20 6f 70 65 6e 65 64 20 62 79 20 63  ndex opened by c
17dea 75 72 73 6f 72 20 50 31 2e 0a 2a 2f 0a 63 61 73  ursor P1..*/.cas
17deb 65 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 3a 20  e OP_IdxDelete: 
17dec 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d  {.  int i = pOp-
17ded 3e 70 31 3b 0a 20 20 43 75 72 73 6f 72 20 2a 70  >p1;.  Cursor *p
17dee 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  C;.  BtCursor *p
17def 43 72 73 72 3b 0a 20 20 61 73 73 65 72 74 28 20  Crsr;.  assert( 
17df0 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 61  pOp->p3>0 );.  a
17df1 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30  ssert( pOp->p2>0
17df2 20 26 26 20 70 4f 70 2d 3e 70 32 2b 70 4f 70 2d   && pOp->p2+pOp-
17df3 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p3<=p->nMem );.
17df4 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
17df5 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  & i<p->nCursor )
17df6 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61  ;.  assert( p->a
17df7 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20  pCsr[i]!=0 );.  
17df8 69 66 28 20 28 70 43 72 73 72 20 3d 20 28 70 43  if( (pCrsr = (pC
17df9 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 2d   = p->apCsr[i])-
17dfa 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29 7b 0a  >pCursor)!=0 ){.
17dfb 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20      int res;.   
17dfc 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
17dfd 72 3b 0a 20 20 20 20 72 2e 70 4b 65 79 49 6e 66  r;.    r.pKeyInf
17dfe 6f 20 3d 20 70 43 2d 3e 70 4b 65 79 49 6e 66 6f  o = pC->pKeyInfo
17dff 3b 0a 20 20 20 20 72 2e 6e 46 69 65 6c 64 20 3d  ;.    r.nField =
17e00 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 72 2e   pOp->p3;.    r.
17e01 6e 65 65 64 46 72 65 65 20 3d 20 30 3b 0a 20 20  needFree = 0;.  
17e02 20 20 72 2e 6e 65 65 64 44 65 73 74 72 6f 79 20    r.needDestroy 
17e03 3d 20 30 3b 0a 20 20 20 20 72 2e 61 4d 65 6d 20  = 0;.    r.aMem 
17e04 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  = &p->aMem[pOp->
17e05 70 32 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  p2];.    rc = sq
17e06 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f  lite3BtreeMoveto
17e07 28 70 43 72 73 72 2c 20 30 2c 20 26 72 2c 20 30  (pCrsr, 0, &r, 0
17e08 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 20  , 0, &res);.    
17e09 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
17e0a 4b 20 26 26 20 72 65 73 3d 3d 30 20 29 7b 0a 20  K && res==0 ){. 
17e0b 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
17e0c 33 42 74 72 65 65 44 65 6c 65 74 65 28 70 43 72  3BtreeDelete(pCr
17e0d 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  sr);.    }.    a
17e0e 73 73 65 72 74 28 20 70 43 2d 3e 64 65 66 65 72  ssert( pC->defer
17e0f 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a  redMoveto==0 );.
17e10 20 20 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61      pC->cacheSta
17e11 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c  tus = CACHE_STAL
17e12 45 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  E;.  }.  break;.
17e13 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64  }../* Opcode: Id
17e14 78 52 6f 77 69 64 20 50 31 20 50 32 20 2a 20 2a  xRowid P1 P2 * *
17e15 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69   *.**.** Write i
17e16 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 20  nto register P2 
17e17 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68  an integer which
17e18 20 69 73 20 74 68 65 20 6c 61 73 74 20 65 6e 74   is the last ent
17e19 72 79 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64  ry in the record
17e1a 20 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f   at.** the end o
17e1b 66 20 74 68 65 20 69 6e 64 65 78 20 6b 65 79 20  f the index key 
17e1c 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 63 75  pointed to by cu
17e1d 72 73 6f 72 20 50 31 2e 20 20 54 68 69 73 20 69  rsor P1.  This i
17e1e 6e 74 65 67 65 72 20 73 68 6f 75 6c 64 20 62 65  nteger should be
17e1f 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64 20 6f 66  .** the rowid of
17e20 20 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79   the table entry
17e21 20 74 6f 20 77 68 69 63 68 20 74 68 69 73 20 69   to which this i
17e22 6e 64 65 78 20 65 6e 74 72 79 20 70 6f 69 6e 74  ndex entry point
17e23 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  s..**.** See als
17e24 6f 3a 20 52 6f 77 69 64 2c 20 4d 61 6b 65 49 64  o: Rowid, MakeId
17e25 78 52 65 63 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  xRec..*/.case OP
17e26 5f 49 64 78 52 6f 77 69 64 3a 20 7b 20 20 20 20  _IdxRowid: {    
17e27 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74            /* out
17e28 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a  2-prerelease */.
17e29 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70    int i = pOp->p
17e2a 31 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  1;.  BtCursor *p
17e2b 43 72 73 72 3b 0a 20 20 43 75 72 73 6f 72 20 2a  Crsr;.  Cursor *
17e2c 70 43 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69  pC;..  assert( i
17e2d 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72  >=0 && i<p->nCur
17e2e 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28  sor );.  assert(
17e2f 20 70 2d 3e 61 70 43 73 72 5b 69 5d 21 3d 30 20   p->apCsr[i]!=0 
17e30 29 3b 0a 20 20 69 66 28 20 28 70 43 72 73 72 20  );.  if( (pCrsr 
17e31 3d 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 72  = (pC = p->apCsr
17e32 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 29 21 3d  [i])->pCursor)!=
17e33 30 20 29 7b 0a 20 20 20 20 69 36 34 20 72 6f 77  0 ){.    i64 row
17e34 69 64 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28  id;..    assert(
17e35 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76   pC->deferredMov
17e36 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73  eto==0 );.    as
17e37 73 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c  sert( pC->isTabl
17e38 65 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  e==0 );.    if( 
17e39 21 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a  !pC->nullRow ){.
17e3a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
17e3b 65 33 56 64 62 65 49 64 78 52 6f 77 69 64 28 70  e3VdbeIdxRowid(p
17e3c 43 72 73 72 2c 20 26 72 6f 77 69 64 29 3b 0a 20  Crsr, &rowid);. 
17e3d 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
17e3e 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
17e3f 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65    goto abort_due
17e40 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20  _to_error;.     
17e41 20 7d 0a 20 20 20 20 20 20 4d 65 6d 53 65 74 54   }.      MemSetT
17e42 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45  ypeFlag(pOut, ME
17e43 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 20 20 70 4f  M_Int);.      pO
17e44 75 74 2d 3e 75 2e 69 20 3d 20 72 6f 77 69 64 3b  ut->u.i = rowid;
17e45 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65  .    }.  }.  bre
17e46 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
17e47 3a 20 49 64 78 47 45 20 50 31 20 50 32 20 50 33  : IdxGE P1 P2 P3
17e48 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 65   P4 P5.**.** The
17e49 20 50 34 20 72 65 67 69 73 74 65 72 20 76 61 6c   P4 register val
17e4a 75 65 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69  ues beginning wi
17e4b 74 68 20 50 33 20 66 6f 72 6d 20 61 6e 20 75 6e  th P3 form an un
17e4c 70 61 63 6b 65 64 20 69 6e 64 65 78 20 0a 2a 2a  packed index .**
17e4d 20 6b 65 79 20 74 68 61 74 20 6f 6d 69 74 73 20   key that omits 
17e4e 74 68 65 20 52 4f 57 49 44 2e 20 20 43 6f 6d 70  the ROWID.  Comp
17e4f 61 72 65 20 74 68 69 73 20 6b 65 79 20 76 61 6c  are this key val
17e50 75 65 20 61 67 61 69 6e 73 74 20 74 68 65 20 69  ue against the i
17e51 6e 64 65 78 20 0a 2a 2a 20 74 68 61 74 20 50 31  ndex .** that P1
17e52 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f   is currently po
17e53 69 6e 74 69 6e 67 20 74 6f 2c 20 69 67 6e 6f 72  inting to, ignor
17e54 69 6e 67 20 74 68 65 20 52 4f 57 49 44 20 6f 6e  ing the ROWID on
17e55 20 74 68 65 20 50 31 20 69 6e 64 65 78 2e 0a 2a   the P1 index..*
17e56 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 31 20 69  *.** If the P1 i
17e57 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20 67 72  ndex entry is gr
17e58 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71  eater than or eq
17e59 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 76  ual to the key v
17e5a 61 6c 75 65 0a 2a 2a 20 74 68 65 6e 20 6a 75 6d  alue.** then jum
17e5b 70 20 74 6f 20 50 32 2e 20 20 4f 74 68 65 72 77  p to P2.  Otherw
17e5c 69 73 65 20 66 61 6c 6c 20 74 68 72 6f 75 67 68  ise fall through
17e5d 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73   to the next ins
17e5e 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  truction..**.** 
17e5f 49 66 20 50 35 20 69 73 20 6e 6f 6e 2d 7a 65 72  If P5 is non-zer
17e60 6f 20 74 68 65 6e 20 74 68 65 20 6b 65 79 20 76  o then the key v
17e61 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61 73 65  alue is increase
17e62 64 20 62 79 20 61 6e 20 65 70 73 69 6c 6f 6e 20  d by an epsilon 
17e63 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 74 68 65  .** prior to the
17e64 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 54 68   comparison.  Th
17e65 69 73 20 6d 61 6b 65 20 74 68 65 20 6f 70 63 6f  is make the opco
17e66 64 65 20 77 6f 72 6b 20 6c 69 6b 65 20 49 64 78  de work like Idx
17e67 47 54 20 65 78 63 65 70 74 0a 2a 2a 20 74 68 61  GT except.** tha
17e68 74 20 69 66 20 74 68 65 20 6b 65 79 20 66 72 6f  t if the key fro
17e69 6d 20 72 65 67 69 73 74 65 72 20 50 33 20 69 73  m register P3 is
17e6a 20 61 20 70 72 65 66 69 78 20 6f 66 20 74 68 65   a prefix of the
17e6b 20 6b 65 79 20 69 6e 20 74 68 65 20 63 75 72 73   key in the curs
17e6c 6f 72 2c 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c  or,.** the resul
17e6d 74 20 69 73 20 66 61 6c 73 65 20 77 68 65 72 65  t is false where
17e6e 61 73 20 69 74 20 77 6f 75 6c 64 20 62 65 20 74  as it would be t
17e6f 72 75 65 20 77 69 74 68 20 49 64 78 47 54 2e 0a  rue with IdxGT..
17e70 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64  */./* Opcode: Id
17e71 78 4c 54 20 50 31 20 50 32 20 50 33 20 2a 20 50  xLT P1 P2 P3 * P
17e72 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20 72  5.**.** The P4 r
17e73 65 67 69 73 74 65 72 20 76 61 6c 75 65 73 20 62  egister values b
17e74 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 50 33  eginning with P3
17e75 20 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63 6b 65   form an unpacke
17e76 64 20 69 6e 64 65 78 20 0a 2a 2a 20 6b 65 79 20  d index .** key 
17e77 74 68 61 74 20 6f 6d 69 74 73 20 74 68 65 20 52  that omits the R
17e78 4f 57 49 44 2e 20 20 43 6f 6d 70 61 72 65 20 74  OWID.  Compare t
17e79 68 69 73 20 6b 65 79 20 76 61 6c 75 65 20 61 67  his key value ag
17e7a 61 69 6e 73 74 20 74 68 65 20 69 6e 64 65 78 20  ainst the index 
17e7b 0a 2a 2a 20 74 68 61 74 20 50 31 20 69 73 20 63  .** that P1 is c
17e7c 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e  urrently pointin
17e7d 67 20 74 6f 2c 20 69 67 6e 6f 72 69 6e 67 20 74  g to, ignoring t
17e7e 68 65 20 52 4f 57 49 44 20 6f 6e 20 74 68 65 20  he ROWID on the 
17e7f 50 31 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20  P1 index..**.** 
17e80 49 66 20 74 68 65 20 50 31 20 69 6e 64 65 78 20  If the P1 index 
17e81 65 6e 74 72 79 20 69 73 20 6c 65 73 73 20 74 68  entry is less th
17e82 61 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65  an the key value
17e83 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32   then jump to P2
17e84 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 66  ..** Otherwise f
17e85 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74  all through to t
17e86 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74  he next instruct
17e87 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35  ion..**.** If P5
17e88 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65   is non-zero the
17e89 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 20  n the key value 
17e8a 69 73 20 69 6e 63 72 65 61 73 65 64 20 62 79 20  is increased by 
17e8b 61 6e 20 65 70 73 69 6c 6f 6e 20 70 72 69 6f 72  an epsilon prior
17e8c 20 0a 2a 2a 20 74 6f 20 74 68 65 20 63 6f 6d 70   .** to the comp
17e8d 61 72 69 73 6f 6e 2e 20 20 54 68 69 73 20 6d 61  arison.  This ma
17e8e 6b 65 73 20 74 68 65 20 6f 70 63 6f 64 65 20 77  kes the opcode w
17e8f 6f 72 6b 20 6c 69 6b 65 20 49 64 78 4c 45 2e 0a  ork like IdxLE..
17e90 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 4c 54  */.case OP_IdxLT
17e91 3a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75  :          /* ju
17e92 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20  mp, in3 */.case 
17e93 4f 50 5f 49 64 78 47 45 3a 20 7b 20 20 20 20 20  OP_IdxGE: {     
17e94 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20     /* jump, in3 
17e95 2a 2f 0a 20 20 69 6e 74 20 69 3d 20 70 4f 70 2d  */.  int i= pOp-
17e96 3e 70 31 3b 0a 20 20 43 75 72 73 6f 72 20 2a 70  >p1;.  Cursor *p
17e97 43 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e  C;..  assert( i>
17e98 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73  =0 && i<p->nCurs
17e99 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  or );.  assert( 
17e9a 70 2d 3e 61 70 43 73 72 5b 69 5d 21 3d 30 20 29  p->apCsr[i]!=0 )
17e9b 3b 0a 20 20 69 66 28 20 28 70 43 20 3d 20 70 2d  ;.  if( (pC = p-
17e9c 3e 61 70 43 73 72 5b 69 5d 29 2d 3e 70 43 75 72  >apCsr[i])->pCur
17e9d 73 6f 72 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e  sor!=0 ){.    in
17e9e 74 20 72 65 73 3b 0a 20 20 20 20 55 6e 70 61 63  t res;.    Unpac
17e9f 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20  kedRecord r;.   
17ea0 20 61 73 73 65 72 74 28 20 70 43 2d 3e 64 65 66   assert( pC->def
17ea1 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29  erredMoveto==0 )
17ea2 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f  ;.    assert( pO
17ea3 70 2d 3e 70 35 3d 3d 30 20 7c 7c 20 70 4f 70 2d  p->p5==0 || pOp-
17ea4 3e 70 35 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73  >p5==1 );.    as
17ea5 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70  sert( pOp->p4typ
17ea6 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20  e==P4_INT32 );. 
17ea7 20 20 20 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20     r.pKeyInfo = 
17ea8 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  pC->pKeyInfo;.  
17ea9 20 20 72 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70    r.nField = pOp
17eaa 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 72 2e 6e 65  ->p4.i;.    r.ne
17eab 65 64 46 72 65 65 20 3d 20 30 3b 0a 20 20 20 20  edFree = 0;.    
17eac 72 2e 6e 65 65 64 44 65 73 74 72 6f 79 20 3d 20  r.needDestroy = 
17ead 30 3b 0a 20 20 20 20 72 2e 61 4d 65 6d 20 3d 20  0;.    r.aMem = 
17eae 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33  &p->aMem[pOp->p3
17eaf 5d 3b 0a 20 20 20 20 2a 70 43 2d 3e 70 49 6e 63  ];.    *pC->pInc
17eb0 72 4b 65 79 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a  rKey = pOp->p5;.
17eb1 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
17eb2 56 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 72  VdbeIdxKeyCompar
17eb3 65 28 70 43 2c 20 26 72 2c 20 30 2c 20 30 2c 20  e(pC, &r, 0, 0, 
17eb4 26 72 65 73 29 3b 0a 20 20 20 20 2a 70 43 2d 3e  &res);.    *pC->
17eb5 70 49 6e 63 72 4b 65 79 20 3d 20 30 3b 0a 20 20  pIncrKey = 0;.  
17eb6 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64    if( pOp->opcod
17eb7 65 3d 3d 4f 50 5f 49 64 78 4c 54 20 29 7b 0a 20  e==OP_IdxLT ){. 
17eb8 20 20 20 20 20 72 65 73 20 3d 20 2d 72 65 73 3b       res = -res;
17eb9 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
17eba 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f    assert( pOp->o
17ebb 70 63 6f 64 65 3d 3d 4f 50 5f 49 64 78 47 45 20  pcode==OP_IdxGE 
17ebc 29 3b 0a 20 20 20 20 20 20 72 65 73 2b 2b 3b 0a  );.      res++;.
17ebd 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 65      }.    if( re
17ebe 73 3e 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20  s>0 ){.      pc 
17ebf 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 20 3b 0a  = pOp->p2 - 1 ;.
17ec0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61      }.  }.  brea
17ec1 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
17ec2 20 44 65 73 74 72 6f 79 20 50 31 20 50 32 20 50   Destroy P1 P2 P
17ec3 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 6c 65  3 * *.**.** Dele
17ec4 74 65 20 61 6e 20 65 6e 74 69 72 65 20 64 61 74  te an entire dat
17ec5 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69  abase table or i
17ec6 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f 74 20  ndex whose root 
17ec7 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61  page in the data
17ec8 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69 73 20  base.** file is 
17ec9 67 69 76 65 6e 20 62 79 20 50 31 2e 0a 2a 2a 0a  given by P1..**.
17eca 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69  ** The table bei
17ecb 6e 67 20 64 65 73 74 72 6f 79 65 64 20 69 73 20  ng destroyed is 
17ecc 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  in the main data
17ecd 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 33 3d  base file if P3=
17ece 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 33 3d 3d 31  =0.  If.** P3==1
17ecf 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20   then the table 
17ed0 74 6f 20 62 65 20 63 6c 65 61 72 20 69 73 20 69  to be clear is i
17ed1 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20  n the auxiliary 
17ed2 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a  database file.**
17ed3 20 74 68 61 74 20 69 73 20 75 73 65 64 20 74 6f   that is used to
17ed4 20 73 74 6f 72 65 20 74 61 62 6c 65 73 20 63 72   store tables cr
17ed5 65 61 74 65 20 75 73 69 6e 67 20 43 52 45 41 54  eate using CREAT
17ed6 45 20 54 45 4d 50 4f 52 41 52 59 20 54 41 42 4c  E TEMPORARY TABL
17ed7 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55 54 4f  E..**.** If AUTO
17ed8 56 41 43 55 55 4d 20 69 73 20 65 6e 61 62 6c 65  VACUUM is enable
17ed9 64 20 74 68 65 6e 20 69 74 20 69 73 20 70 6f 73  d then it is pos
17eda 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f 74 68  sible that anoth
17edb 65 72 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a 20  er root page.** 
17edc 6d 69 67 68 74 20 62 65 20 6d 6f 76 65 64 20 69  might be moved i
17edd 6e 74 6f 20 74 68 65 20 6e 65 77 6c 79 20 64 65  nto the newly de
17ede 6c 65 74 65 64 20 72 6f 6f 74 20 70 61 67 65 20  leted root page 
17edf 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70  in order to keep
17ee0 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 61 67   all.** root pag
17ee1 65 73 20 63 6f 6e 74 69 67 75 6f 75 73 20 61 74  es contiguous at
17ee2 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
17ee3 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  f the database. 
17ee4 20 54 68 65 20 66 6f 72 6d 65 72 0a 2a 2a 20 76   The former.** v
17ee5 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f 6f 74  alue of the root
17ee6 20 70 61 67 65 20 74 68 61 74 20 6d 6f 76 65 64   page that moved
17ee7 20 2d 20 69 74 73 20 76 61 6c 75 65 20 62 65 66   - its value bef
17ee8 6f 72 65 20 74 68 65 20 6d 6f 76 65 20 6f 63 63  ore the move occ
17ee9 75 72 72 65 64 20 2d 0a 2a 2a 20 69 73 20 73 74  urred -.** is st
17eea 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72  ored in register
17eeb 20 50 32 2e 20 20 49 66 20 6e 6f 20 70 61 67 65   P2.  If no page
17eec 20 0a 2a 2a 20 6d 6f 76 65 6d 65 6e 74 20 77 61   .** movement wa
17eed 73 20 72 65 71 75 69 72 65 64 20 28 62 65 63 61  s required (beca
17eee 75 73 65 20 74 68 65 20 74 61 62 6c 65 20 62 65  use the table be
17eef 69 6e 67 20 64 72 6f 70 70 65 64 20 77 61 73 20  ing dropped was 
17ef0 61 6c 72 65 61 64 79 20 0a 2a 2a 20 74 68 65 20  already .** the 
17ef1 6c 61 73 74 20 6f 6e 65 20 69 6e 20 74 68 65 20  last one in the 
17ef2 64 61 74 61 62 61 73 65 29 20 74 68 65 6e 20 61  database) then a
17ef3 20 7a 65 72 6f 20 69 73 20 73 74 6f 72 65 64 20   zero is stored 
17ef4 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a  in register P2..
17ef5 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 55 55 4d  ** If AUTOVACUUM
17ef6 20 69 73 20 64 69 73 61 62 6c 65 64 20 74 68 65   is disabled the
17ef7 6e 20 61 20 7a 65 72 6f 20 69 73 20 73 74 6f 72  n a zero is stor
17ef8 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ed in register P
17ef9 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  2..**.** See als
17efa 6f 3a 20 43 6c 65 61 72 0a 2a 2f 0a 63 61 73 65  o: Clear.*/.case
17efb 20 4f 50 5f 44 65 73 74 72 6f 79 3a 20 7b 20 20   OP_Destroy: {  
17efc 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65     /* out2-prere
17efd 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69  lease */.  int i
17efe 4d 6f 76 65 64 3b 0a 20 20 69 6e 74 20 69 43 6e  Moved;.  int iCn
17eff 74 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  t;.#ifndef SQLIT
17f00 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
17f01 42 4c 45 0a 20 20 56 64 62 65 20 2a 70 56 64 62  BLE.  Vdbe *pVdb
17f02 65 3b 0a 20 20 69 43 6e 74 20 3d 20 30 3b 0a 20  e;.  iCnt = 0;. 
17f03 20 66 6f 72 28 70 56 64 62 65 3d 64 62 2d 3e 70   for(pVdbe=db->p
17f04 56 64 62 65 3b 20 70 56 64 62 65 3b 20 70 56 64  Vdbe; pVdbe; pVd
17f05 62 65 3d 70 56 64 62 65 2d 3e 70 4e 65 78 74 29  be=pVdbe->pNext)
17f06 7b 0a 20 20 20 20 69 66 28 20 70 56 64 62 65 2d  {.    if( pVdbe-
17f07 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47  >magic==VDBE_MAG
17f08 49 43 5f 52 55 4e 20 26 26 20 70 56 64 62 65 2d  IC_RUN && pVdbe-
17f09 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 3c 32 20  >inVtabMethod<2 
17f0a 26 26 20 70 56 64 62 65 2d 3e 70 63 3e 3d 30 20  && pVdbe->pc>=0 
17f0b 29 7b 0a 20 20 20 20 20 20 69 43 6e 74 2b 2b 3b  ){.      iCnt++;
17f0c 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65  .    }.  }.#else
17f0d 0a 20 20 69 43 6e 74 20 3d 20 64 62 2d 3e 61 63  .  iCnt = db->ac
17f0e 74 69 76 65 56 64 62 65 43 6e 74 3b 0a 23 65 6e  tiveVdbeCnt;.#en
17f0f 64 69 66 0a 20 20 69 66 28 20 69 43 6e 74 3e 31  dif.  if( iCnt>1
17f10 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
17f11 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20 20  ITE_LOCKED;.    
17f12 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20 3d  p->errorAction =
17f13 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 7d 65 6c   OE_Abort;.  }el
17f14 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 44 62 20  se{.    int iDb 
17f15 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 61  = pOp->p3;.    a
17f16 73 73 65 72 74 28 20 69 43 6e 74 3d 3d 31 20 29  ssert( iCnt==1 )
17f17 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  ;.    assert( (p
17f18 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31  ->btreeMask & (1
17f19 3c 3c 69 44 62 29 29 21 3d 30 20 29 3b 0a 20 20  <<iDb))!=0 );.  
17f1a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
17f1b 72 65 65 44 72 6f 70 54 61 62 6c 65 28 64 62 2d  reeDropTable(db-
17f1c 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 2c 20 70  >aDb[iDb].pBt, p
17f1d 4f 70 2d 3e 70 31 2c 20 26 69 4d 6f 76 65 64 29  Op->p1, &iMoved)
17f1e 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65  ;.    MemSetType
17f1f 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49  Flag(pOut, MEM_I
17f20 6e 74 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75  nt);.    pOut->u
17f21 2e 69 20 3d 20 69 4d 6f 76 65 64 3b 0a 23 69 66  .i = iMoved;.#if
17f22 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
17f23 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
17f24 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
17f25 4b 20 26 26 20 69 4d 6f 76 65 64 21 3d 30 20 29  K && iMoved!=0 )
17f26 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  {.      sqlite3R
17f27 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 26 64 62  ootPageMoved(&db
17f28 2d 3e 61 44 62 5b 69 44 62 5d 2c 20 69 4d 6f 76  ->aDb[iDb], iMov
17f29 65 64 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20  ed, pOp->p1);.  
17f2a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20    }.#endif.  }. 
17f2b 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
17f2c 63 6f 64 65 3a 20 43 6c 65 61 72 20 50 31 20 50  code: Clear P1 P
17f2d 32 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65  2 *.**.** Delete
17f2e 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 73 20 6f 66   all contents of
17f2f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61   the database ta
17f30 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 77 68 6f  ble or index who
17f31 73 65 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a 20  se root page.** 
17f32 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
17f33 66 69 6c 65 20 69 73 20 67 69 76 65 6e 20 62 79  file is given by
17f34 20 50 31 2e 20 20 42 75 74 2c 20 75 6e 6c 69 6b   P1.  But, unlik
17f35 65 20 44 65 73 74 72 6f 79 2c 20 64 6f 20 6e 6f  e Destroy, do no
17f36 74 0a 2a 2a 20 72 65 6d 6f 76 65 20 74 68 65 20  t.** remove the 
17f37 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 66  table or index f
17f38 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
17f39 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   file..**.** The
17f3a 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63 6c 65   table being cle
17f3b 61 72 20 69 73 20 69 6e 20 74 68 65 20 6d 61 69  ar is in the mai
17f3c 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  n database file 
17f3d 69 66 20 50 32 3d 3d 30 2e 20 20 49 66 0a 2a 2a  if P2==0.  If.**
17f3e 20 50 32 3d 3d 31 20 74 68 65 6e 20 74 68 65 20   P2==1 then the 
17f3f 74 61 62 6c 65 20 74 6f 20 62 65 20 63 6c 65 61  table to be clea
17f40 72 20 69 73 20 69 6e 20 74 68 65 20 61 75 78 69  r is in the auxi
17f41 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66  liary database f
17f42 69 6c 65 0a 2a 2a 20 74 68 61 74 20 69 73 20 75  ile.** that is u
17f43 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 61 62  sed to store tab
17f44 6c 65 73 20 63 72 65 61 74 65 20 75 73 69 6e 67  les create using
17f45 20 43 52 45 41 54 45 20 54 45 4d 50 4f 52 41 52   CREATE TEMPORAR
17f46 59 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 53  Y TABLE..**.** S
17f47 65 65 20 61 6c 73 6f 3a 20 44 65 73 74 72 6f 79  ee also: Destroy
17f48 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6c 65 61  .*/.case OP_Clea
17f49 72 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 20 28  r: {.  assert( (
17f4a 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28  p->btreeMask & (
17f4b 31 3c 3c 70 4f 70 2d 3e 70 32 29 29 21 3d 30 20  1<<pOp->p2))!=0 
17f4c 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
17f4d 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65  3BtreeClearTable
17f4e 28 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 32  (db->aDb[pOp->p2
17f4f 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 31 29 3b  ].pBt, pOp->p1);
17f50 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
17f51 4f 70 63 6f 64 65 3a 20 43 72 65 61 74 65 54 61  Opcode: CreateTa
17f52 62 6c 65 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  ble P1 P2 * * *.
17f53 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61  **.** Allocate a
17f54 20 6e 65 77 20 74 61 62 6c 65 20 69 6e 20 74 68   new table in th
17f55 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
17f56 66 69 6c 65 20 69 66 20 50 31 3d 3d 30 20 6f 72  file if P1==0 or
17f57 20 69 6e 20 74 68 65 0a 2a 2a 20 61 75 78 69 6c   in the.** auxil
17f58 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66 69  iary database fi
17f59 6c 65 20 69 66 20 50 31 3d 3d 31 20 6f 72 20 69  le if P1==1 or i
17f5a 6e 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61  n an attached da
17f5b 74 61 62 61 73 65 20 69 66 0a 2a 2a 20 50 31 3e  tabase if.** P1>
17f5c 31 2e 20 20 57 72 69 74 65 20 74 68 65 20 72 6f  1.  Write the ro
17f5d 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  ot page number o
17f5e 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20  f the new table 
17f5f 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72  into.** register
17f60 20 50 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69   P2.**.** The di
17f61 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e  fference between
17f62 20 61 20 74 61 62 6c 65 20 61 6e 64 20 61 6e 20   a table and an 
17f63 69 6e 64 65 78 20 69 73 20 74 68 69 73 3a 20 20  index is this:  
17f64 41 20 74 61 62 6c 65 20 6d 75 73 74 0a 2a 2a 20  A table must.** 
17f65 68 61 76 65 20 61 20 34 2d 62 79 74 65 20 69 6e  have a 4-byte in
17f66 74 65 67 65 72 20 6b 65 79 20 61 6e 64 20 63 61  teger key and ca
17f67 6e 20 68 61 76 65 20 61 72 62 69 74 72 61 72 79  n have arbitrary
17f68 20 64 61 74 61 2e 20 20 41 6e 20 69 6e 64 65 78   data.  An index
17f69 0a 2a 2a 20 68 61 73 20 61 6e 20 61 72 62 69 74  .** has an arbit
17f6a 72 61 72 79 20 6b 65 79 20 62 75 74 20 6e 6f 20  rary key but no 
17f6b 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  data..**.** See 
17f6c 61 6c 73 6f 3a 20 43 72 65 61 74 65 49 6e 64 65  also: CreateInde
17f6d 78 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  x.*/./* Opcode: 
17f6e 43 72 65 61 74 65 49 6e 64 65 78 20 50 31 20 50  CreateIndex P1 P
17f6f 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6c  2 * * *.**.** Al
17f70 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 69 6e 64  locate a new ind
17f71 65 78 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64  ex in the main d
17f72 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20  atabase file if 
17f73 50 31 3d 3d 30 20 6f 72 20 69 6e 20 74 68 65 0a  P1==0 or in the.
17f74 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64 61 74  ** auxiliary dat
17f75 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 31  abase file if P1
17f76 3d 3d 31 20 6f 72 20 69 6e 20 61 6e 20 61 74 74  ==1 or in an att
17f77 61 63 68 65 64 20 64 61 74 61 62 61 73 65 20 69  ached database i
17f78 66 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 72 69 74  f.** P1>1.  Writ
17f79 65 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  e the root page 
17f7a 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65  number of the ne
17f7b 77 20 74 61 62 6c 65 20 69 6e 74 6f 0a 2a 2a 20  w table into.** 
17f7c 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a  register P2..**.
17f7d 2a 2a 20 53 65 65 20 64 6f 63 75 6d 65 6e 74 61  ** See documenta
17f7e 74 69 6f 6e 20 6f 6e 20 4f 50 5f 43 72 65 61 74  tion on OP_Creat
17f7f 65 54 61 62 6c 65 20 66 6f 72 20 61 64 64 69 74  eTable for addit
17f80 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
17f81 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 72  n..*/.case OP_Cr
17f82 65 61 74 65 49 6e 64 65 78 3a 20 20 20 20 20 20  eateIndex:      
17f83 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
17f84 65 72 65 6c 65 61 73 65 20 2a 2f 0a 63 61 73 65  erelease */.case
17f85 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 3a   OP_CreateTable:
17f86 20 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f   {          /* o
17f87 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a  ut2-prerelease *
17f88 2f 0a 20 20 69 6e 74 20 70 67 6e 6f 3b 0a 20 20  /.  int pgno;.  
17f89 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 44 62 20  int flags;.  Db 
17f8a 2a 70 44 62 3b 0a 20 20 61 73 73 65 72 74 28 20  *pDb;.  assert( 
17f8b 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f  pOp->p1>=0 && pO
17f8c 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b  p->p1<db->nDb );
17f8d 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62  .  assert( (p->b
17f8e 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70  treeMask & (1<<p
17f8f 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20  Op->p1))!=0 );. 
17f90 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b   pDb = &db->aDb[
17f91 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65  pOp->p1];.  asse
17f92 72 74 28 20 70 44 62 2d 3e 70 42 74 21 3d 30 20  rt( pDb->pBt!=0 
17f93 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70  );.  if( pOp->op
17f94 63 6f 64 65 3d 3d 4f 50 5f 43 72 65 61 74 65 54  code==OP_CreateT
17f95 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 66  able ){.    /* f
17f96 6c 61 67 73 20 3d 20 42 54 52 45 45 5f 49 4e 54  lags = BTREE_INT
17f97 4b 45 59 3b 20 2a 2f 0a 20 20 20 20 66 6c 61 67  KEY; */.    flag
17f98 73 20 3d 20 42 54 52 45 45 5f 4c 45 41 46 44 41  s = BTREE_LEAFDA
17f99 54 41 7c 42 54 52 45 45 5f 49 4e 54 4b 45 59 3b  TA|BTREE_INTKEY;
17f9a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6c  .  }else{.    fl
17f9b 61 67 73 20 3d 20 42 54 52 45 45 5f 5a 45 52 4f  ags = BTREE_ZERO
17f9c 44 41 54 41 3b 0a 20 20 7d 0a 20 20 72 63 20 3d  DATA;.  }.  rc =
17f9d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65   sqlite3BtreeCre
17f9e 61 74 65 54 61 62 6c 65 28 70 44 62 2d 3e 70 42  ateTable(pDb->pB
17f9f 74 2c 20 26 70 67 6e 6f 2c 20 66 6c 61 67 73 29  t, &pgno, flags)
17fa0 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
17fa1 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 4f 75  TE_OK ){.    pOu
17fa2 74 2d 3e 75 2e 69 20 3d 20 70 67 6e 6f 3b 0a 20  t->u.i = pgno;. 
17fa3 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61     MemSetTypeFla
17fa4 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29  g(pOut, MEM_Int)
17fa5 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
17fa6 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 61 72  ../* Opcode: Par
17fa7 73 65 53 63 68 65 6d 61 20 50 31 20 50 32 20 2a  seSchema P1 P2 *
17fa8 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64   P4 *.**.** Read
17fa9 20 61 6e 64 20 70 61 72 73 65 20 61 6c 6c 20 65   and parse all e
17faa 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 68 65 20  ntries from the 
17fab 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61  SQLITE_MASTER ta
17fac 62 6c 65 20 6f 66 20 64 61 74 61 62 61 73 65 20  ble of database 
17fad 50 31 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63 68  P1.** that match
17fae 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
17faf 65 20 50 34 2e 20 20 50 32 20 69 73 20 74 68 65  e P4.  P2 is the
17fb0 20 22 66 6f 72 63 65 22 20 66 6c 61 67 2e 20 20   "force" flag.  
17fb1 20 41 6c 77 61 79 73 20 64 6f 0a 2a 2a 20 74 68   Always do.** th
17fb2 65 20 70 61 72 73 69 6e 67 20 69 66 20 50 32 20  e parsing if P2 
17fb3 69 73 20 74 72 75 65 2e 20 20 49 66 20 50 32 20  is true.  If P2 
17fb4 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 74  is false, then t
17fb5 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
17fb6 0a 2a 2a 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65  .** no-op if the
17fb7 20 73 63 68 65 6d 61 20 69 73 20 6e 6f 74 20 63   schema is not c
17fb8 75 72 72 65 6e 74 6c 79 20 6c 6f 61 64 65 64 2e  urrently loaded.
17fb9 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
17fba 2c 20 69 66 20 50 32 0a 2a 2a 20 69 73 20 66 61  , if P2.** is fa
17fbb 6c 73 65 2c 20 74 68 65 20 53 51 4c 49 54 45 5f  lse, the SQLITE_
17fbc 4d 41 53 54 45 52 20 74 61 62 6c 65 20 69 73 20  MASTER table is 
17fbd 6f 6e 6c 79 20 70 61 72 73 65 64 20 69 66 20 74  only parsed if t
17fbe 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 0a 2a  he rest of the.*
17fbf 2a 20 73 63 68 65 6d 61 20 69 73 20 61 6c 72 65  * schema is alre
17fc0 61 64 79 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20  ady loaded into 
17fc1 74 68 65 20 73 79 6d 62 6f 6c 20 74 61 62 6c 65  the symbol table
17fc2 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63  ..**.** This opc
17fc3 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20  ode invokes the 
17fc4 70 61 72 73 65 72 20 74 6f 20 63 72 65 61 74 65  parser to create
17fc5 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20 6d   a new virtual m
17fc6 61 63 68 69 6e 65 2c 0a 2a 2a 20 74 68 65 6e 20  achine,.** then 
17fc7 72 75 6e 73 20 74 68 65 20 6e 65 77 20 76 69 72  runs the new vir
17fc8 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 49  tual machine.  I
17fc9 74 20 69 73 20 74 68 75 73 20 61 20 72 65 65 6e  t is thus a reen
17fca 74 72 61 6e 74 20 6f 70 63 6f 64 65 2e 0a 2a 2f  trant opcode..*/
17fcb 0a 63 61 73 65 20 4f 50 5f 50 61 72 73 65 53 63  .case OP_ParseSc
17fcc 68 65 6d 61 3a 20 7b 0a 20 20 63 68 61 72 20 2a  hema: {.  char *
17fcd 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 69 44 62 20  zSql;.  int iDb 
17fce 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 63 6f 6e  = pOp->p1;.  con
17fcf 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72  st char *zMaster
17fd0 3b 0a 20 20 49 6e 69 74 44 61 74 61 20 69 6e 69  ;.  InitData ini
17fd1 74 44 61 74 61 3b 0a 0a 20 20 61 73 73 65 72 74  tData;..  assert
17fd2 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c  ( iDb>=0 && iDb<
17fd3 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 69 66 28  db->nDb );.  if(
17fd4 20 21 70 4f 70 2d 3e 70 32 20 26 26 20 21 44 62   !pOp->p2 && !Db
17fd5 48 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20  HasProperty(db, 
17fd6 69 44 62 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f  iDb, DB_SchemaLo
17fd7 61 64 65 64 29 20 29 7b 0a 20 20 20 20 62 72 65  aded) ){.    bre
17fd8 61 6b 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65  ak;.  }.  zMaste
17fd9 72 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45  r = SCHEMA_TABLE
17fda 28 69 44 62 29 3b 0a 20 20 69 6e 69 74 44 61 74  (iDb);.  initDat
17fdb 61 2e 64 62 20 3d 20 64 62 3b 0a 20 20 69 6e 69  a.db = db;.  ini
17fdc 74 44 61 74 61 2e 69 44 62 20 3d 20 70 4f 70 2d  tData.iDb = pOp-
17fdd 3e 70 31 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e  >p1;.  initData.
17fde 70 7a 45 72 72 4d 73 67 20 3d 20 26 70 2d 3e 7a  pzErrMsg = &p->z
17fdf 45 72 72 4d 73 67 3b 0a 20 20 7a 53 71 6c 20 3d  ErrMsg;.  zSql =
17fe0 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
17fe1 64 62 2c 0a 20 20 20 20 20 22 53 45 4c 45 43 54  db,.     "SELECT
17fe2 20 6e 61 6d 65 2c 20 72 6f 6f 74 70 61 67 65 2c   name, rootpage,
17fe3 20 73 71 6c 20 46 52 4f 4d 20 27 25 71 27 2e 25   sql FROM '%q'.%
17fe4 73 20 57 48 45 52 45 20 25 73 22 2c 0a 20 20 20  s WHERE %s",.   
17fe5 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a    db->aDb[iDb].z
17fe6 4e 61 6d 65 2c 20 7a 4d 61 73 74 65 72 2c 20 70  Name, zMaster, p
17fe7 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 69 66 28  Op->p4.z);.  if(
17fe8 20 7a 53 71 6c 3d 3d 30 20 29 20 67 6f 74 6f 20   zSql==0 ) goto 
17fe9 6e 6f 5f 6d 65 6d 3b 0a 20 20 28 76 6f 69 64 29  no_mem;.  (void)
17fea 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
17feb 28 64 62 29 3b 0a 20 20 61 73 73 65 72 74 28 20  (db);.  assert( 
17fec 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30  db->init.busy==0
17fed 20 29 3b 0a 20 20 64 62 2d 3e 69 6e 69 74 2e 62   );.  db->init.b
17fee 75 73 79 20 3d 20 31 3b 0a 20 20 61 73 73 65 72  usy = 1;.  asser
17fef 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  t( !db->mallocFa
17ff0 69 6c 65 64 20 29 3b 0a 20 20 72 63 20 3d 20 73  iled );.  rc = s
17ff1 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20  qlite3_exec(db, 
17ff2 7a 53 71 6c 2c 20 73 71 6c 69 74 65 33 49 6e 69  zSql, sqlite3Ini
17ff3 74 43 61 6c 6c 62 61 63 6b 2c 20 26 69 6e 69 74  tCallback, &init
17ff4 44 61 74 61 2c 20 30 29 3b 0a 20 20 69 66 28 20  Data, 0);.  if( 
17ff5 72 63 3d 3d 53 51 4c 49 54 45 5f 41 42 4f 52 54  rc==SQLITE_ABORT
17ff6 20 29 20 72 63 20 3d 20 69 6e 69 74 44 61 74 61   ) rc = initData
17ff7 2e 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  .rc;.  sqlite3_f
17ff8 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20 64 62 2d  ree(zSql);.  db-
17ff9 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 30 3b 0a  >init.busy = 0;.
17ffa 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
17ffb 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 69  afetyOn(db);.  i
17ffc 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
17ffd 4d 45 4d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  MEM ){.    goto 
17ffe 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 62 72  no_mem;.  }.  br
17fff 65 61 6b 3b 20 20 0a 7d 0a 0a 23 69 66 20 21 64  eak;  .}..#if !d
18000 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
18001 49 54 5f 41 4e 41 4c 59 5a 45 29 20 26 26 20 21  IT_ANALYZE) && !
18002 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
18003 4d 49 54 5f 50 41 52 53 45 52 29 0a 2f 2a 20 4f  MIT_PARSER)./* O
18004 70 63 6f 64 65 3a 20 4c 6f 61 64 41 6e 61 6c 79  pcode: LoadAnaly
18005 73 69 73 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a  sis P1 * * * *.*
18006 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 73 71  *.** Read the sq
18007 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65  lite_stat1 table
18008 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 50 31   for database P1
18009 20 61 6e 64 20 6c 6f 61 64 20 74 68 65 20 63 6f   and load the co
1800a 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 68 61 74  ntent.** of that
1800b 20 74 61 62 6c 65 20 69 6e 74 6f 20 74 68 65 20   table into the 
1800c 69 6e 74 65 72 6e 61 6c 20 69 6e 64 65 78 20 68  internal index h
1800d 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68 69 73  ash table.  This
1800e 20 77 69 6c 6c 20 63 61 75 73 65 0a 2a 2a 20 74   will cause.** t
1800f 68 65 20 61 6e 61 6c 79 73 69 73 20 74 6f 20 62  he analysis to b
18010 65 20 75 73 65 64 20 77 68 65 6e 20 70 72 65 70  e used when prep
18011 61 72 69 6e 67 20 61 6c 6c 20 73 75 62 73 65 71  aring all subseq
18012 75 65 6e 74 20 71 75 65 72 69 65 73 2e 0a 2a 2f  uent queries..*/
18013 0a 63 61 73 65 20 4f 50 5f 4c 6f 61 64 41 6e 61  .case OP_LoadAna
18014 6c 79 73 69 73 3a 20 7b 0a 20 20 69 6e 74 20 69  lysis: {.  int i
18015 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20  Db = pOp->p1;.  
18016 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26  assert( iDb>=0 &
18017 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b  & iDb<db->nDb );
18018 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41  .  rc = sqlite3A
18019 6e 61 6c 79 73 69 73 4c 6f 61 64 28 64 62 2c 20  nalysisLoad(db, 
1801a 69 44 62 29 3b 0a 20 20 62 72 65 61 6b 3b 20 20  iDb);.  break;  
1801b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  .}.#endif /* !de
1801c 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
1801d 54 5f 41 4e 41 4c 59 5a 45 29 20 26 26 20 21 64  T_ANALYZE) && !d
1801e 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
1801f 49 54 5f 50 41 52 53 45 52 29 20 20 2a 2f 0a 0a  IT_PARSER)  */..
18020 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f 70 54  /* Opcode: DropT
18021 61 62 6c 65 20 50 31 20 2a 20 2a 20 50 34 20 2a  able P1 * * P4 *
18022 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68  .**.** Remove th
18023 65 20 69 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d  e internal (in-m
18024 65 6d 6f 72 79 29 20 64 61 74 61 20 73 74 72 75  emory) data stru
18025 63 74 75 72 65 73 20 74 68 61 74 20 64 65 73 63  ctures that desc
18026 72 69 62 65 0a 2a 2a 20 74 68 65 20 74 61 62 6c  ribe.** the tabl
18027 65 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61  e named P4 in da
18028 74 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 73  tabase P1.  This
18029 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72   is called after
1802a 20 61 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 64   a table.** is d
1802b 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72 20  ropped in order 
1802c 74 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74 65  to keep the inte
1802d 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74  rnal representat
1802e 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63  ion of the.** sc
1802f 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74 20  hema consistent 
18030 77 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e 20  with what is on 
18031 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  disk..*/.case OP
18032 5f 44 72 6f 70 54 61 62 6c 65 3a 20 7b 0a 20 20  _DropTable: {.  
18033 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64  sqlite3UnlinkAnd
18034 44 65 6c 65 74 65 54 61 62 6c 65 28 64 62 2c 20  DeleteTable(db, 
18035 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34  pOp->p1, pOp->p4
18036 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .z);.  break;.}.
18037 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f 70  ./* Opcode: Drop
18038 49 6e 64 65 78 20 50 31 20 2a 20 2a 20 50 34 20  Index P1 * * P4 
18039 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74  *.**.** Remove t
1803a 68 65 20 69 6e 74 65 72 6e 61 6c 20 28 69 6e 2d  he internal (in-
1803b 6d 65 6d 6f 72 79 29 20 64 61 74 61 20 73 74 72  memory) data str
1803c 75 63 74 75 72 65 73 20 74 68 61 74 20 64 65 73  uctures that des
1803d 63 72 69 62 65 0a 2a 2a 20 74 68 65 20 69 6e 64  cribe.** the ind
1803e 65 78 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64  ex named P4 in d
1803f 61 74 61 62 61 73 65 20 50 31 2e 20 20 54 68 69  atabase P1.  Thi
18040 73 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65  s is called afte
18041 72 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 69 73  r an index.** is
18042 20 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 65   dropped in orde
18043 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69 6e  r to keep the in
18044 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74  ternal represent
18045 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20  ation of the.** 
18046 73 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e  schema consisten
18047 74 20 77 69 74 68 20 77 68 61 74 20 69 73 20 6f  t with what is o
18048 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20  n disk..*/.case 
18049 4f 50 5f 44 72 6f 70 49 6e 64 65 78 3a 20 7b 0a  OP_DropIndex: {.
1804a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41    sqlite3UnlinkA
1804b 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 28 64 62  ndDeleteIndex(db
1804c 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e  , pOp->p1, pOp->
1804d 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  p4.z);.  break;.
1804e 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72  }../* Opcode: Dr
1804f 6f 70 54 72 69 67 67 65 72 20 50 31 20 2a 20 2a  opTrigger P1 * *
18050 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f   P4 *.**.** Remo
18051 76 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  ve the internal 
18052 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61  (in-memory) data
18053 20 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74   structures that
18054 20 64 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 65   describe.** the
18055 20 74 72 69 67 67 65 72 20 6e 61 6d 65 64 20 50   trigger named P
18056 34 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 31  4 in database P1
18057 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65  .  This is calle
18058 64 20 61 66 74 65 72 20 61 20 74 72 69 67 67 65  d after a trigge
18059 72 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20  r.** is dropped 
1805a 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70  in order to keep
1805b 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65   the internal re
1805c 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20  presentation of 
1805d 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f  the.** schema co
1805e 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 68  nsistent with wh
1805f 61 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a  at is on disk..*
18060 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 54 72  /.case OP_DropTr
18061 69 67 67 65 72 3a 20 7b 0a 20 20 73 71 6c 69 74  igger: {.  sqlit
18062 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74  e3UnlinkAndDelet
18063 65 54 72 69 67 67 65 72 28 64 62 2c 20 70 4f 70  eTrigger(db, pOp
18064 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29  ->p1, pOp->p4.z)
18065 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23  ;.  break;.}...#
18066 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
18067 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  IT_INTEGRITY_CHE
18068 43 4b 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e  CK./* Opcode: In
18069 74 65 67 72 69 74 79 43 6b 20 50 31 20 50 32 20  tegrityCk P1 P2 
1806a 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 44 6f  P3 * P5.**.** Do
1806b 20 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20   an analysis of 
1806c 74 68 65 20 63 75 72 72 65 6e 74 6c 79 20 6f 70  the currently op
1806d 65 6e 20 64 61 74 61 62 61 73 65 2e 20 20 53 74  en database.  St
1806e 6f 72 65 20 69 6e 0a 2a 2a 20 72 65 67 69 73 74  ore in.** regist
1806f 65 72 20 50 31 20 74 68 65 20 74 65 78 74 20 6f  er P1 the text o
18070 66 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  f an error messa
18071 67 65 20 64 65 73 63 72 69 62 69 6e 67 20 61 6e  ge describing an
18072 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 2a 2a 20 49  y problems..** I
18073 66 20 6e 6f 20 70 72 6f 62 6c 65 6d 73 20 61 72  f no problems ar
18074 65 20 66 6f 75 6e 64 2c 20 73 74 6f 72 65 20 61  e found, store a
18075 20 4e 55 4c 4c 20 69 6e 20 72 65 67 69 73 74 65   NULL in registe
18076 72 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  r P1..**.** The 
18077 72 65 67 69 73 74 65 72 20 50 33 20 63 6f 6e 74  register P3 cont
18078 61 69 6e 73 20 74 68 65 20 6d 61 78 69 6d 75 6d  ains the maximum
18079 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 77   number of allow
1807a 65 64 20 65 72 72 6f 72 73 2e 0a 2a 2a 20 41 74  ed errors..** At
1807b 20 6d 6f 73 74 20 72 65 67 28 50 33 29 20 65 72   most reg(P3) er
1807c 72 6f 72 73 20 77 69 6c 6c 20 62 65 20 72 65 70  rors will be rep
1807d 6f 72 74 65 64 2e 0a 2a 2a 20 49 6e 20 6f 74 68  orted..** In oth
1807e 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 61 6e  er words, the an
1807f 61 6c 79 73 69 73 20 73 74 6f 70 73 20 61 73 20  alysis stops as 
18080 73 6f 6f 6e 20 61 73 20 72 65 67 28 50 31 29 20  soon as reg(P1) 
18081 65 72 72 6f 72 73 20 61 72 65 20 0a 2a 2a 20 73  errors are .** s
18082 65 65 6e 2e 20 20 52 65 67 28 50 31 29 20 69 73  een.  Reg(P1) is
18083 20 75 70 64 61 74 65 64 20 77 69 74 68 20 74 68   updated with th
18084 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f  e number of erro
18085 72 73 20 72 65 6d 61 69 6e 69 6e 67 2e 0a 2a 2a  rs remaining..**
18086 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67  .** The root pag
18087 65 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 6c 6c  e numbers of all
18088 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 64   tables in the d
18089 61 74 61 62 61 73 65 20 61 72 65 20 69 6e 74 65  atabase are inte
1808a 67 65 72 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e  ger.** stored in
1808b 20 72 65 67 28 50 31 29 2c 20 72 65 67 28 50 31   reg(P1), reg(P1
1808c 2b 31 29 2c 20 72 65 67 28 50 31 2b 32 29 2c 20  +1), reg(P1+2), 
1808d 2e 2e 2e 2e 20 20 54 68 65 72 65 20 61 72 65 20  ....  There are 
1808e 50 32 20 74 61 62 6c 65 73 0a 2a 2a 20 74 6f 74  P2 tables.** tot
1808f 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20  al..**.** If P5 
18090 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65  is not zero, the
18091 20 63 68 65 63 6b 20 69 73 20 64 6f 6e 65 20 6f   check is done o
18092 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20  n the auxiliary 
18093 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65  database.** file
18094 2c 20 6e 6f 74 20 74 68 65 20 6d 61 69 6e 20 64  , not the main d
18095 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a  atabase file..**
18096 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20  .** This opcode 
18097 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  is used to imple
18098 6d 65 6e 74 20 74 68 65 20 69 6e 74 65 67 72 69  ment the integri
18099 74 79 5f 63 68 65 63 6b 20 70 72 61 67 6d 61 2e  ty_check pragma.
1809a 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 65  .*/.case OP_Inte
1809b 67 72 69 74 79 43 6b 3a 20 7b 0a 20 20 69 6e 74  grityCk: {.  int
1809c 20 6e 52 6f 6f 74 3b 20 20 20 20 20 20 2f 2a 20   nRoot;      /* 
1809d 4e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73  Number of tables
1809e 20 74 6f 20 63 68 65 63 6b 2e 20 20 28 4e 75 6d   to check.  (Num
1809f 62 65 72 20 6f 66 20 72 6f 6f 74 20 70 61 67 65  ber of root page
180a0 73 2e 29 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52  s.) */.  int *aR
180a1 6f 6f 74 3b 20 20 20 20 20 2f 2a 20 41 72 72 61  oot;     /* Arra
180a2 79 20 6f 66 20 72 6f 6f 74 70 61 67 65 20 6e 75  y of rootpage nu
180a3 6d 62 65 72 73 20 66 6f 72 20 74 61 62 6c 65 73  mbers for tables
180a4 20 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a   to be checked *
180a5 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20  /.  int j;      
180a6 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
180a7 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72  ter */.  int nEr
180a8 72 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  r;       /* Numb
180a9 65 72 20 6f 66 20 65 72 72 6f 72 73 20 72 65 70  er of errors rep
180aa 6f 72 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20  orted */.  char 
180ab 2a 7a 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 65  *z;        /* Te
180ac 78 74 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20  xt of the error 
180ad 72 65 70 6f 72 74 20 2a 2f 0a 20 20 4d 65 6d 20  report */.  Mem 
180ae 2a 70 6e 45 72 72 3b 20 20 20 20 20 2f 2a 20 52  *pnErr;     /* R
180af 65 67 69 73 74 65 72 20 6b 65 65 70 69 6e 67 20  egister keeping 
180b0 74 72 61 63 6b 20 6f 66 20 65 72 72 6f 72 73 20  track of errors 
180b1 72 65 6d 61 69 6e 69 6e 67 20 2a 2f 0a 20 20 0a  remaining */.  .
180b2 20 20 6e 52 6f 6f 74 20 3d 20 70 4f 70 2d 3e 70    nRoot = pOp->p
180b3 32 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 52 6f  2;.  assert( nRo
180b4 6f 74 3e 30 20 29 3b 0a 20 20 61 52 6f 6f 74 20  ot>0 );.  aRoot 
180b5 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
180b6 28 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28 6e  ( sizeof(int)*(n
180b7 52 6f 6f 74 2b 31 29 20 29 3b 0a 20 20 69 66 28  Root+1) );.  if(
180b8 20 61 52 6f 6f 74 3d 3d 30 20 29 20 67 6f 74 6f   aRoot==0 ) goto
180b9 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 61 73 73 65 72   no_mem;.  asser
180ba 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20  t( pOp->p3>0 && 
180bb 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p3<=p->nMem
180bc 20 29 3b 0a 20 20 70 6e 45 72 72 20 3d 20 26 70   );.  pnErr = &p
180bd 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b  ->aMem[pOp->p3];
180be 0a 20 20 61 73 73 65 72 74 28 20 28 70 6e 45 72  .  assert( (pnEr
180bf 72 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49  r->flags & MEM_I
180c0 6e 74 29 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  nt)!=0 );.  asse
180c1 72 74 28 20 28 70 6e 45 72 72 2d 3e 66 6c 61 67  rt( (pnErr->flag
180c2 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d  s & (MEM_Str|MEM
180c3 5f 42 6c 6f 62 29 29 3d 3d 30 20 29 3b 0a 20 20  _Blob))==0 );.  
180c4 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pIn1 = &p->aMem[
180c5 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28  pOp->p1];.  for(
180c6 6a 3d 30 3b 20 6a 3c 6e 52 6f 6f 74 3b 20 6a 2b  j=0; j<nRoot; j+
180c7 2b 29 7b 0a 20 20 20 20 61 52 6f 6f 74 5b 6a 5d  +){.    aRoot[j]
180c8 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e   = sqlite3VdbeIn
180c9 74 56 61 6c 75 65 28 26 70 49 6e 31 5b 6a 5d 29  tValue(&pIn1[j])
180ca 3b 0a 20 20 7d 0a 20 20 61 52 6f 6f 74 5b 6a 5d  ;.  }.  aRoot[j]
180cb 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
180cc 70 4f 70 2d 3e 70 35 3c 64 62 2d 3e 6e 44 62 20  pOp->p5<db->nDb 
180cd 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d  );.  assert( (p-
180ce 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c  >btreeMask & (1<
180cf 3c 70 4f 70 2d 3e 70 35 29 29 21 3d 30 20 29 3b  <pOp->p5))!=0 );
180d0 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 42 74  .  z = sqlite3Bt
180d1 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 63  reeIntegrityChec
180d2 6b 28 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70  k(db->aDb[pOp->p
180d3 35 5d 2e 70 42 74 2c 20 61 52 6f 6f 74 2c 20 6e  5].pBt, aRoot, n
180d4 52 6f 6f 74 2c 0a 20 20 20 20 20 20 20 20 20 20  Root,.          
180d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
180d6 20 20 20 20 20 20 20 70 6e 45 72 72 2d 3e 75 2e         pnErr->u.
180d7 69 2c 20 26 6e 45 72 72 29 3b 0a 20 20 70 6e 45  i, &nErr);.  pnE
180d8 72 72 2d 3e 75 2e 69 20 2d 3d 20 6e 45 72 72 3b  rr->u.i -= nErr;
180d9 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
180da 6d 53 65 74 4e 75 6c 6c 28 70 49 6e 31 29 3b 0a  mSetNull(pIn1);.
180db 20 20 69 66 28 20 6e 45 72 72 3d 3d 30 20 29 7b    if( nErr==0 ){
180dc 0a 20 20 20 20 61 73 73 65 72 74 28 20 7a 3d 3d  .    assert( z==
180dd 30 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  0 );.  }else{.  
180de 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
180df 53 65 74 53 74 72 28 70 49 6e 31 2c 20 7a 2c 20  SetStr(pIn1, z, 
180e0 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  -1, SQLITE_UTF8,
180e1 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
180e2 20 20 7d 0a 20 20 55 50 44 41 54 45 5f 4d 41 58    }.  UPDATE_MAX
180e3 5f 42 4c 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b  _BLOBSIZE(pIn1);
180e4 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
180e5 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 49 6e  angeEncoding(pIn
180e6 31 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20  1, encoding);.  
180e7 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 52 6f  sqlite3_free(aRo
180e8 6f 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  ot);.  break;.}.
180e9 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
180ea 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f  _OMIT_INTEGRITY_
180eb 43 48 45 43 4b 20 2a 2f 0a 0a 2f 2a 20 4f 70 63  CHECK */../* Opc
180ec 6f 64 65 3a 20 46 69 66 6f 57 72 69 74 65 20 50  ode: FifoWrite P
180ed 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  1 * * * *.**.** 
180ee 57 72 69 74 65 20 74 68 65 20 69 6e 74 65 67 65  Write the intege
180ef 72 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20  r from register 
180f0 50 31 20 69 6e 74 6f 20 74 68 65 20 46 69 66 6f  P1 into the Fifo
180f1 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 69 66  ..*/.case OP_Fif
180f2 6f 57 72 69 74 65 3a 20 7b 20 20 20 20 20 20 20  oWrite: {       
180f3 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28   /* in1 */.  if(
180f4 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 66 6f   sqlite3VdbeFifo
180f5 50 75 73 68 28 26 70 2d 3e 73 46 69 66 6f 2c 20  Push(&p->sFifo, 
180f6 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61  sqlite3VdbeIntVa
180f7 6c 75 65 28 70 49 6e 31 29 29 3d 3d 53 51 4c 49  lue(pIn1))==SQLI
180f8 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20  TE_NOMEM ){.    
180f9 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d  goto no_mem;.  }
180fa 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
180fb 4f 70 63 6f 64 65 3a 20 46 69 66 6f 52 65 61 64  Opcode: FifoRead
180fc 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
180fd 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 72 65  ** Attempt to re
180fe 61 64 20 61 20 73 69 6e 67 6c 65 20 69 6e 74 65  ad a single inte
180ff 67 65 72 20 66 72 6f 6d 20 74 68 65 20 46 69 66  ger from the Fif
18100 6f 2e 20 20 53 74 6f 72 65 20 74 68 61 74 0a 2a  o.  Store that.*
18101 2a 20 69 6e 74 65 67 65 72 20 69 6e 20 72 65 67  * integer in reg
18102 69 73 74 65 72 20 50 31 2e 0a 2a 2a 20 0a 2a 2a  ister P1..** .**
18103 20 49 66 20 74 68 65 20 46 69 66 6f 20 69 73 20   If the Fifo is 
18104 65 6d 70 74 79 20 6a 75 6d 70 20 74 6f 20 50 32  empty jump to P2
18105 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 69 66  ..*/.case OP_Fif
18106 6f 52 65 61 64 3a 20 7b 20 20 20 20 20 20 20 20  oRead: {        
18107 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 43 48   /* jump */.  CH
18108 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50  ECK_FOR_INTERRUP
18109 54 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  T;.  assert( pOp
1810a 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ->p1>0 && pOp->p
1810b 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  1<=p->nMem );.  
1810c 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pOut = &p->aMem[
1810d 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 4d 65 6d 53  pOp->p1];.  MemS
1810e 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c  etTypeFlag(pOut,
1810f 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 69 66 28   MEM_Int);.  if(
18110 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 66 6f   sqlite3VdbeFifo
18111 50 6f 70 28 26 70 2d 3e 73 46 69 66 6f 2c 20 26  Pop(&p->sFifo, &
18112 70 4f 75 74 2d 3e 75 2e 69 29 3d 3d 53 51 4c 49  pOut->u.i)==SQLI
18113 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 70  TE_DONE ){.    p
18114 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
18115 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
18116 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
18117 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 2f 2a 20  OMIT_TRIGGER./* 
18118 4f 70 63 6f 64 65 3a 20 43 6f 6e 74 65 78 74 50  Opcode: ContextP
18119 75 73 68 20 2a 20 2a 20 2a 20 0a 2a 2a 0a 2a 2a  ush * * * .**.**
1811a 20 53 61 76 65 20 74 68 65 20 63 75 72 72 65 6e   Save the curren
1811b 74 20 56 64 62 65 20 63 6f 6e 74 65 78 74 20 73  t Vdbe context s
1811c 75 63 68 20 74 68 61 74 20 69 74 20 63 61 6e 20  uch that it can 
1811d 62 65 20 72 65 73 74 6f 72 65 64 20 62 79 20 61  be restored by a
1811e 20 43 6f 6e 74 65 78 74 50 6f 70 0a 2a 2a 20 6f   ContextPop.** o
1811f 70 63 6f 64 65 2e 20 54 68 65 20 63 6f 6e 74 65  pcode. The conte
18120 78 74 20 73 74 6f 72 65 73 20 74 68 65 20 6c 61  xt stores the la
18121 73 74 20 69 6e 73 65 72 74 20 72 6f 77 20 69 64  st insert row id
18122 2c 20 74 68 65 20 6c 61 73 74 20 73 74 61 74 65  , the last state
18123 6d 65 6e 74 20 63 68 61 6e 67 65 0a 2a 2a 20 63  ment change.** c
18124 6f 75 6e 74 2c 20 61 6e 64 20 74 68 65 20 63 75  ount, and the cu
18125 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e 74 20  rrent statement 
18126 63 68 61 6e 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f  change count..*/
18127 0a 63 61 73 65 20 4f 50 5f 43 6f 6e 74 65 78 74  .case OP_Context
18128 50 75 73 68 3a 20 7b 0a 20 20 69 6e 74 20 69 20  Push: {.  int i 
18129 3d 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63  = p->contextStac
1812a 6b 54 6f 70 2b 2b 3b 0a 20 20 43 6f 6e 74 65 78  kTop++;.  Contex
1812b 74 20 2a 70 43 6f 6e 74 65 78 74 3b 0a 0a 20 20  t *pContext;..  
1812c 61 73 73 65 72 74 28 20 69 3e 3d 30 20 29 3b 0a  assert( i>=0 );.
1812d 20 20 2f 2a 20 46 49 58 20 4d 45 3a 20 54 68 69    /* FIX ME: Thi
1812e 73 20 73 68 6f 75 6c 64 20 62 65 20 61 6c 6c 6f  s should be allo
1812f 63 61 74 65 64 20 61 73 20 70 61 72 74 20 6f 66  cated as part of
18130 20 74 68 65 20 76 64 62 65 20 61 74 20 63 6f 6d   the vdbe at com
18131 70 69 6c 65 2d 74 69 6d 65 20 2a 2f 0a 20 20 69  pile-time */.  i
18132 66 28 20 69 3e 3d 70 2d 3e 63 6f 6e 74 65 78 74  f( i>=p->context
18133 53 74 61 63 6b 44 65 70 74 68 20 29 7b 0a 20 20  StackDepth ){.  
18134 20 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63    p->contextStac
18135 6b 44 65 70 74 68 20 3d 20 69 2b 31 3b 0a 20 20  kDepth = i+1;.  
18136 20 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63    p->contextStac
18137 6b 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61  k = sqlite3DbRea
18138 6c 6c 6f 63 4f 72 46 72 65 65 28 64 62 2c 20 70  llocOrFree(db, p
18139 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 2c 0a  ->contextStack,.
1813a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1813b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1813c 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66            sizeof
1813d 28 43 6f 6e 74 65 78 74 29 2a 28 69 2b 31 29 29  (Context)*(i+1))
1813e 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 63 6f 6e  ;.    if( p->con
1813f 74 65 78 74 53 74 61 63 6b 3d 3d 30 20 29 20 67  textStack==0 ) g
18140 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a  oto no_mem;.  }.
18141 20 20 70 43 6f 6e 74 65 78 74 20 3d 20 26 70 2d    pContext = &p-
18142 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 5b 69 5d  >contextStack[i]
18143 3b 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e 6c 61  ;.  pContext->la
18144 73 74 52 6f 77 69 64 20 3d 20 64 62 2d 3e 6c 61  stRowid = db->la
18145 73 74 52 6f 77 69 64 3b 0a 20 20 70 43 6f 6e 74  stRowid;.  pCont
18146 65 78 74 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 70  ext->nChange = p
18147 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 70 43 6f  ->nChange;.  pCo
18148 6e 74 65 78 74 2d 3e 73 46 69 66 6f 20 3d 20 70  ntext->sFifo = p
18149 2d 3e 73 46 69 66 6f 3b 0a 20 20 73 71 6c 69 74  ->sFifo;.  sqlit
1814a 65 33 56 64 62 65 46 69 66 6f 49 6e 69 74 28 26  e3VdbeFifoInit(&
1814b 70 2d 3e 73 46 69 66 6f 29 3b 0a 20 20 62 72 65  p->sFifo);.  bre
1814c 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1814d 3a 20 43 6f 6e 74 65 78 74 50 6f 70 20 2a 20 2a  : ContextPop * *
1814e 20 2a 20 0a 2a 2a 0a 2a 2a 20 52 65 73 74 6f 72   * .**.** Restor
1814f 65 20 74 68 65 20 56 64 62 65 20 63 6f 6e 74 65  e the Vdbe conte
18150 78 74 20 74 6f 20 74 68 65 20 73 74 61 74 65 20  xt to the state 
18151 69 74 20 77 61 73 20 69 6e 20 77 68 65 6e 20 63  it was in when c
18152 6f 6e 74 65 78 74 50 75 73 68 20 77 61 73 20 6c  ontextPush was l
18153 61 73 74 0a 2a 2a 20 65 78 65 63 75 74 65 64 2e  ast.** executed.
18154 20 54 68 65 20 63 6f 6e 74 65 78 74 20 73 74 6f   The context sto
18155 72 65 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73  res the last ins
18156 65 72 74 20 72 6f 77 20 69 64 2c 20 74 68 65 20  ert row id, the 
18157 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74 0a 2a  last statement.*
18158 2a 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 2c 20  * change count, 
18159 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20  and the current 
1815a 73 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 65  statement change
1815b 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 63 61 73 65 20   count..*/.case 
1815c 4f 50 5f 43 6f 6e 74 65 78 74 50 6f 70 3a 20 7b  OP_ContextPop: {
1815d 0a 20 20 43 6f 6e 74 65 78 74 20 2a 70 43 6f 6e  .  Context *pCon
1815e 74 65 78 74 20 3d 20 26 70 2d 3e 63 6f 6e 74 65  text = &p->conte
1815f 78 74 53 74 61 63 6b 5b 2d 2d 70 2d 3e 63 6f 6e  xtStack[--p->con
18160 74 65 78 74 53 74 61 63 6b 54 6f 70 5d 3b 0a 20  textStackTop];. 
18161 20 61 73 73 65 72 74 28 20 70 2d 3e 63 6f 6e 74   assert( p->cont
18162 65 78 74 53 74 61 63 6b 54 6f 70 3e 3d 30 20 29  extStackTop>=0 )
18163 3b 0a 20 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69  ;.  db->lastRowi
18164 64 20 3d 20 70 43 6f 6e 74 65 78 74 2d 3e 6c 61  d = pContext->la
18165 73 74 52 6f 77 69 64 3b 0a 20 20 70 2d 3e 6e 43  stRowid;.  p->nC
18166 68 61 6e 67 65 20 3d 20 70 43 6f 6e 74 65 78 74  hange = pContext
18167 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 73 71 6c  ->nChange;.  sql
18168 69 74 65 33 56 64 62 65 46 69 66 6f 43 6c 65 61  ite3VdbeFifoClea
18169 72 28 26 70 2d 3e 73 46 69 66 6f 29 3b 0a 20 20  r(&p->sFifo);.  
1816a 70 2d 3e 73 46 69 66 6f 20 3d 20 70 43 6f 6e 74  p->sFifo = pCont
1816b 65 78 74 2d 3e 73 46 69 66 6f 3b 0a 20 20 62 72  ext->sFifo;.  br
1816c 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  eak;.}.#endif /*
1816d 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f   #ifndef SQLITE_
1816e 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a  OMIT_TRIGGER */.
1816f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
18170 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45  OMIT_AUTOINCREME
18171 4e 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 65  NT./* Opcode: Me
18172 6d 4d 61 78 20 50 31 20 50 32 20 2a 20 2a 20 2a  mMax P1 P2 * * *
18173 0a 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76  .**.** Set the v
18174 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 72  alue of register
18175 20 50 31 20 74 6f 20 74 68 65 20 6d 61 78 69 6d   P1 to the maxim
18176 75 6d 20 6f 66 20 69 74 73 20 63 75 72 72 65 6e  um of its curren
18177 74 20 76 61 6c 75 65 0a 2a 2a 20 61 6e 64 20 74  t value.** and t
18178 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
18179 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54  ster P2..**.** T
1817a 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1817b 74 68 72 6f 77 73 20 61 6e 20 65 72 72 6f 72 20  throws an error 
1817c 69 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65  if the memory ce
1817d 6c 6c 20 69 73 20 6e 6f 74 20 69 6e 69 74 69 61  ll is not initia
1817e 6c 6c 79 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65  lly.** an intege
1817f 72 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 65  r..*/.case OP_Me
18180 6d 4d 61 78 3a 20 7b 20 20 20 20 20 20 20 20 2f  mMax: {        /
18181 2a 20 69 6e 31 2c 20 69 6e 32 20 2a 2f 0a 20 20  * in1, in2 */.  
18182 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e  sqlite3VdbeMemIn
18183 74 65 67 65 72 69 66 79 28 70 49 6e 31 29 3b 0a  tegerify(pIn1);.
18184 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
18185 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 32 29  Integerify(pIn2)
18186 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e  ;.  if( pIn1->u.
18187 69 3c 70 49 6e 32 2d 3e 75 2e 69 29 7b 0a 20 20  i<pIn2->u.i){.  
18188 20 20 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 49    pIn1->u.i = pI
18189 6e 32 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 62  n2->u.i;.  }.  b
1818a 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  reak;.}.#endif /
1818b 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  * SQLITE_OMIT_AU
1818c 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 0a  TOINCREMENT */..
1818d 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 50 6f 73  /* Opcode: IfPos
1818e 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1818f 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20  ** If the value 
18190 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20 69  of register P1 i
18191 73 20 31 20 6f 72 20 67 72 65 61 74 65 72 2c 20  s 1 or greater, 
18192 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a  jump to P2..**.*
18193 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20  * It is illegal 
18194 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 74  to use this inst
18195 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67  ruction on a reg
18196 69 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a  ister that does.
18197 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61  ** not contain a
18198 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61  n integer.  An a
18199 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77  ssertion fault w
1819a 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f  ill result if yo
1819b 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f  u try..*/.case O
1819c 50 5f 49 66 50 6f 73 3a 20 7b 20 20 20 20 20 20  P_IfPos: {      
1819d 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a    /* jump, in1 *
1819e 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31  /.  assert( pIn1
1819f 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20  ->flags&MEM_Int 
181a0 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75  );.  if( pIn1->u
181a1 2e 69 3e 30 20 29 7b 0a 20 20 20 20 20 70 63 20  .i>0 ){.     pc 
181a2 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
181a3 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
181a4 2a 20 4f 70 63 6f 64 65 3a 20 49 66 4e 65 67 20  * Opcode: IfNeg 
181a5 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
181a6 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f  * If the value o
181a7 66 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73  f register P1 is
181a8 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c   less than zero,
181a9 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a   jump to P2. .**
181aa 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61  .** It is illega
181ab 6c 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e  l to use this in
181ac 73 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72  struction on a r
181ad 65 67 69 73 74 65 72 20 74 68 61 74 20 64 6f 65  egister that doe
181ae 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e  s.** not contain
181af 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 6e   an integer.  An
181b0 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74   assertion fault
181b1 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 20   will result if 
181b2 79 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65  you try..*/.case
181b3 20 4f 50 5f 49 66 4e 65 67 3a 20 7b 20 20 20 20   OP_IfNeg: {    
181b4 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31      /* jump, in1
181b5 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49   */.  assert( pI
181b6 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e  n1->flags&MEM_In
181b7 74 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d  t );.  if( pIn1-
181b8 3e 75 2e 69 3c 30 20 29 7b 0a 20 20 20 20 20 70  >u.i<0 ){.     p
181b9 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
181ba 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
181bb 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 5a 65  ./* Opcode: IfZe
181bc 72 6f 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  ro P1 P2 * * *.*
181bd 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75  *.** If the valu
181be 65 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31  e of register P1
181bf 20 69 73 20 65 78 61 63 74 6c 79 20 30 2c 20 6a   is exactly 0, j
181c0 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a 2a  ump to P2. .**.*
181c1 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20  * It is illegal 
181c2 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 74  to use this inst
181c3 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67  ruction on a reg
181c4 69 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a  ister that does.
181c5 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61  ** not contain a
181c6 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61  n integer.  An a
181c7 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77  ssertion fault w
181c8 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f  ill result if yo
181c9 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f  u try..*/.case O
181ca 50 5f 49 66 5a 65 72 6f 3a 20 7b 20 20 20 20 20  P_IfZero: {     
181cb 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20     /* jump, in1 
181cc 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e  */.  assert( pIn
181cd 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74  1->flags&MEM_Int
181ce 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e   );.  if( pIn1->
181cf 75 2e 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20 70  u.i==0 ){.     p
181d0 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
181d1 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
181d2 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 67 53  ./* Opcode: AggS
181d3 74 65 70 20 2a 20 50 32 20 50 33 20 50 34 20 50  tep * P2 P3 P4 P
181d4 35 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20  5.**.** Execute 
181d5 74 68 65 20 73 74 65 70 20 66 75 6e 63 74 69 6f  the step functio
181d6 6e 20 66 6f 72 20 61 6e 20 61 67 67 72 65 67 61  n for an aggrega
181d7 74 65 2e 20 20 54 68 65 0a 2a 2a 20 66 75 6e 63  te.  The.** func
181d8 74 69 6f 6e 20 68 61 73 20 50 35 20 61 72 67 75  tion has P5 argu
181d9 6d 65 6e 74 73 2e 20 20 20 50 34 20 69 73 20 61  ments.   P4 is a
181da 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
181db 46 75 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75 63  FuncDef.** struc
181dc 74 75 72 65 20 74 68 61 74 20 73 70 65 63 69 66  ture that specif
181dd 69 65 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  ies the function
181de 2e 20 20 55 73 65 20 72 65 67 69 73 74 65 72 0a  .  Use register.
181df 2a 2a 20 50 33 20 61 73 20 74 68 65 20 61 63 63  ** P3 as the acc
181e0 75 6d 75 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20  umulator..**.** 
181e1 54 68 65 20 50 35 20 61 72 67 75 6d 65 6e 74 73  The P5 arguments
181e2 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20   are taken from 
181e3 72 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 20  register P2 and 
181e4 69 74 73 0a 2a 2a 20 73 75 63 63 65 73 73 6f 72  its.** successor
181e5 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 67  s..*/.case OP_Ag
181e6 67 53 74 65 70 3a 20 7b 0a 20 20 69 6e 74 20 6e  gStep: {.  int n
181e7 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 20 20 69 6e   = pOp->p5;.  in
181e8 74 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d  t i;.  Mem *pMem
181e9 2c 20 2a 70 52 65 63 3b 0a 20 20 73 71 6c 69 74  , *pRec;.  sqlit
181ea 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a  e3_context ctx;.
181eb 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
181ec 2a 2a 61 70 56 61 6c 3b 0a 0a 20 20 61 73 73 65  **apVal;..  asse
181ed 72 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 70 52  rt( n>=0 );.  pR
181ee 65 63 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  ec = &p->aMem[pO
181ef 70 2d 3e 70 32 5d 3b 0a 20 20 61 70 56 61 6c 20  p->p2];.  apVal 
181f0 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 61 73  = p->apArg;.  as
181f1 73 65 72 74 28 20 61 70 56 61 6c 20 7c 7c 20 6e  sert( apVal || n
181f2 3d 3d 30 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30  ==0 );.  for(i=0
181f3 3b 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 52 65 63  ; i<n; i++, pRec
181f4 2b 2b 29 7b 0a 20 20 20 20 61 70 56 61 6c 5b 69  ++){.    apVal[i
181f5 5d 20 3d 20 70 52 65 63 3b 0a 20 20 20 20 73 74  ] = pRec;.    st
181f6 6f 72 65 54 79 70 65 49 6e 66 6f 28 70 52 65 63  oreTypeInfo(pRec
181f7 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 7d  , encoding);.  }
181f8 0a 20 20 63 74 78 2e 70 46 75 6e 63 20 3d 20 70  .  ctx.pFunc = p
181f9 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20  Op->p4.pFunc;.  
181fa 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e  assert( pOp->p3>
181fb 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d  0 && pOp->p3<=p-
181fc 3e 6e 4d 65 6d 20 29 3b 0a 20 20 63 74 78 2e 70  >nMem );.  ctx.p
181fd 4d 65 6d 20 3d 20 70 4d 65 6d 20 3d 20 26 70 2d  Mem = pMem = &p-
181fe 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  >aMem[pOp->p3];.
181ff 20 20 70 4d 65 6d 2d 3e 6e 2b 2b 3b 0a 20 20 63    pMem->n++;.  c
18200 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d  tx.s.flags = MEM
18201 5f 4e 75 6c 6c 3b 0a 20 20 63 74 78 2e 73 2e 7a  _Null;.  ctx.s.z
18202 20 3d 20 30 3b 0a 20 20 63 74 78 2e 73 2e 7a 4d   = 0;.  ctx.s.zM
18203 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 63 74 78  alloc = 0;.  ctx
18204 2e 73 2e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 63  .s.xDel = 0;.  c
18205 74 78 2e 73 2e 64 62 20 3d 20 64 62 3b 0a 20 20  tx.s.db = db;.  
18206 63 74 78 2e 69 73 45 72 72 6f 72 20 3d 20 30 3b  ctx.isError = 0;
18207 0a 20 20 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 30  .  ctx.pColl = 0
18208 3b 0a 20 20 69 66 28 20 63 74 78 2e 70 46 75 6e  ;.  if( ctx.pFun
18209 63 2d 3e 6e 65 65 64 43 6f 6c 6c 53 65 71 20 29  c->needCollSeq )
1820a 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f  {.    assert( pO
1820b 70 3e 70 2d 3e 61 4f 70 20 29 3b 0a 20 20 20 20  p>p->aOp );.    
1820c 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e  assert( pOp[-1].
1820d 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53  p4type==P4_COLLS
1820e 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  EQ );.    assert
1820f 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65  ( pOp[-1].opcode
18210 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29 3b 0a  ==OP_CollSeq );.
18211 20 20 20 20 63 74 78 2e 70 43 6f 6c 6c 20 3d 20      ctx.pColl = 
18212 70 4f 70 5b 2d 31 5d 2e 70 34 2e 70 43 6f 6c 6c  pOp[-1].p4.pColl
18213 3b 0a 20 20 7d 0a 20 20 28 63 74 78 2e 70 46 75  ;.  }.  (ctx.pFu
18214 6e 63 2d 3e 78 53 74 65 70 29 28 26 63 74 78 2c  nc->xStep)(&ctx,
18215 20 6e 2c 20 61 70 56 61 6c 29 3b 0a 20 20 69 66   n, apVal);.  if
18216 28 20 63 74 78 2e 69 73 45 72 72 6f 72 20 29 7b  ( ctx.isError ){
18217 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
18218 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
18219 67 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  g, sqlite3_value
1821a 5f 74 65 78 74 28 26 63 74 78 2e 73 29 2c 20 28  _text(&ctx.s), (
1821b 63 68 61 72 2a 29 30 29 3b 0a 20 20 20 20 72 63  char*)0);.    rc
1821c 20 3d 20 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a   = ctx.isError;.
1821d 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
1821e 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 63 74 78  eMemRelease(&ctx
1821f 2e 73 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .s);.  break;.}.
18220 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 67 46  ./* Opcode: AggF
18221 69 6e 61 6c 20 50 31 20 50 32 20 2a 20 50 34 20  inal P1 P2 * P4 
18222 2a 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20  *.**.** Execute 
18223 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 66 75  the finalizer fu
18224 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e 20 61 67  nction for an ag
18225 67 72 65 67 61 74 65 2e 20 20 50 31 20 69 73 0a  gregate.  P1 is.
18226 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f  ** the memory lo
18227 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 74  cation that is t
18228 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 66  he accumulator f
18229 6f 72 20 74 68 65 20 61 67 67 72 65 67 61 74 65  or the aggregate
1822a 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20 74 68  ..**.** P2 is th
1822b 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  e number of argu
1822c 6d 65 6e 74 73 20 74 68 61 74 20 74 68 65 20 73  ments that the s
1822d 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b  tep function tak
1822e 65 73 20 61 6e 64 0a 2a 2a 20 50 34 20 69 73 20  es and.** P4 is 
1822f 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
18230 20 46 75 6e 63 44 65 66 20 66 6f 72 20 74 68 69   FuncDef for thi
18231 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65  s function.  The
18232 20 50 32 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20   P2.** argument 
18233 69 73 20 6e 6f 74 20 75 73 65 64 20 62 79 20 74  is not used by t
18234 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 49 74 20  his opcode.  It 
18235 69 73 20 6f 6e 6c 79 20 74 68 65 72 65 20 74 6f  is only there to
18236 20 64 69 73 61 6d 62 69 67 75 61 74 65 0a 2a 2a   disambiguate.**
18237 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20   functions that 
18238 63 61 6e 20 74 61 6b 65 20 76 61 72 79 69 6e 67  can take varying
18239 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 72 67 75   numbers of argu
1823a 6d 65 6e 74 73 2e 20 20 54 68 65 0a 2a 2a 20 50  ments.  The.** P
1823b 34 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e  4 argument is on
1823c 6c 79 20 6e 65 65 64 65 64 20 66 6f 72 20 74 68  ly needed for th
1823d 65 20 64 65 67 65 6e 65 72 61 74 65 20 63 61 73  e degenerate cas
1823e 65 20 77 68 65 72 65 0a 2a 2a 20 74 68 65 20 73  e where.** the s
1823f 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 77 61 73  tep function was
18240 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20   not previously 
18241 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 63 61 73 65 20  called..*/.case 
18242 4f 50 5f 41 67 67 46 69 6e 61 6c 3a 20 7b 0a 20  OP_AggFinal: {. 
18243 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 61 73   Mem *pMem;.  as
18244 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20  sert( pOp->p1>0 
18245 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e  && pOp->p1<=p->n
18246 4d 65 6d 20 29 3b 0a 20 20 70 4d 65 6d 20 3d 20  Mem );.  pMem = 
18247 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31  &p->aMem[pOp->p1
18248 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d  ];.  assert( (pM
18249 65 6d 2d 3e 66 6c 61 67 73 20 26 20 7e 28 4d 45  em->flags & ~(ME
1824a 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 41 67 67 29 29  M_Null|MEM_Agg))
1824b 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  ==0 );.  rc = sq
1824c 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61  lite3VdbeMemFina
1824d 6c 69 7a 65 28 70 4d 65 6d 2c 20 70 4f 70 2d 3e  lize(pMem, pOp->
1824e 70 34 2e 70 46 75 6e 63 29 3b 0a 20 20 69 66 28  p4.pFunc);.  if(
1824f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 52 52 4f   rc==SQLITE_ERRO
18250 52 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  R ){.    sqlite3
18251 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
18252 72 72 4d 73 67 2c 20 73 71 6c 69 74 65 33 5f 76  rrMsg, sqlite3_v
18253 61 6c 75 65 5f 74 65 78 74 28 70 4d 65 6d 29 2c  alue_text(pMem),
18254 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20 7d 0a   (char*)0);.  }.
18255 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
18256 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d  ngeEncoding(pMem
18257 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 55  , encoding);.  U
18258 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49  PDATE_MAX_BLOBSI
18259 5a 45 28 70 4d 65 6d 29 3b 0a 20 20 69 66 28 20  ZE(pMem);.  if( 
1825a 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f  sqlite3VdbeMemTo
1825b 6f 42 69 67 28 70 4d 65 6d 29 20 29 7b 0a 20 20  oBig(pMem) ){.  
1825c 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a    goto too_big;.
1825d 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1825e 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
1825f 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d  LITE_OMIT_VACUUM
18260 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
18261 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48  LITE_OMIT_ATTACH
18262 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61 63  )./* Opcode: Vac
18263 75 75 6d 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a  uum * * * * *.**
18264 0a 2a 2a 20 56 61 63 75 75 6d 20 74 68 65 20 65  .** Vacuum the e
18265 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 2e 20  ntire database. 
18266 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 69 6c   This opcode wil
18267 6c 20 63 61 75 73 65 20 6f 74 68 65 72 20 76 69  l cause other vi
18268 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65  rtual.** machine
18269 73 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20  s to be created 
1826a 61 6e 64 20 72 75 6e 2e 20 20 49 74 20 6d 61 79  and run.  It may
1826b 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 66   not be called f
1826c 72 6f 6d 20 77 69 74 68 69 6e 0a 2a 2a 20 61 20  rom within.** a 
1826d 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a  transaction..*/.
1826e 63 61 73 65 20 4f 50 5f 56 61 63 75 75 6d 3a 20  case OP_Vacuum: 
1826f 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  {.  if( sqlite3S
18270 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67  afetyOff(db) ) g
18271 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
18272 5f 6d 69 73 75 73 65 3b 20 0a 20 20 72 63 20 3d  _misuse; .  rc =
18273 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75 75   sqlite3RunVacuu
18274 6d 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64  m(&p->zErrMsg, d
18275 62 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  b);.  if( sqlite
18276 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20  3SafetyOn(db) ) 
18277 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
18278 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 62 72 65 61  o_misuse;.  brea
18279 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  k;.}.#endif..#if
1827a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1827b 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
1827c 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 63  )./* Opcode: Inc
1827d 72 56 61 63 75 75 6d 20 50 31 20 50 32 20 2a 20  rVacuum P1 P2 * 
1827e 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 65 72 66 6f 72  * *.**.** Perfor
1827f 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70 20  m a single step 
18280 6f 66 20 74 68 65 20 69 6e 63 72 65 6d 65 6e 74  of the increment
18281 61 6c 20 76 61 63 75 75 6d 20 70 72 6f 63 65 64  al vacuum proced
18282 75 72 65 20 6f 6e 0a 2a 2a 20 74 68 65 20 50 31  ure on.** the P1
18283 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 74 68   database. If th
18284 65 20 76 61 63 75 75 6d 20 68 61 73 20 66 69 6e  e vacuum has fin
18285 69 73 68 65 64 2c 20 6a 75 6d 70 20 74 6f 20 69  ished, jump to i
18286 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 50 32  nstruction.** P2
18287 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61 6c  . Otherwise, fal
18288 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65  l through to the
18289 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f   next instructio
1828a 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e  n..*/.case OP_In
1828b 63 72 56 61 63 75 75 6d 3a 20 7b 20 20 20 20 20  crVacuum: {     
1828c 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20     /* jump */.  
1828d 42 74 72 65 65 20 2a 70 42 74 3b 0a 0a 20 20 61  Btree *pBt;..  a
1828e 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d  ssert( pOp->p1>=
1828f 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d  0 && pOp->p1<db-
18290 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74  >nDb );.  assert
18291 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20  ( (p->btreeMask 
18292 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21  & (1<<pOp->p1))!
18293 3d 30 20 29 3b 0a 20 20 70 42 74 20 3d 20 64 62  =0 );.  pBt = db
18294 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70  ->aDb[pOp->p1].p
18295 42 74 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  Bt;.  rc = sqlit
18296 65 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75  e3BtreeIncrVacuu
18297 6d 28 70 42 74 29 3b 0a 20 20 69 66 28 20 72 63  m(pBt);.  if( rc
18298 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
18299 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70  .    pc = pOp->p
1829a 32 20 2d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20  2 - 1;.    rc = 
1829b 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
1829c 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1829d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 45 78 70  ../* Opcode: Exp
1829e 69 72 65 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a  ire P1 * * * *.*
1829f 2a 0a 2a 2a 20 43 61 75 73 65 20 70 72 65 63 6f  *.** Cause preco
182a0 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74  mpiled statement
182a1 73 20 74 6f 20 62 65 63 6f 6d 65 20 65 78 70 69  s to become expi
182a2 72 65 64 2e 20 41 6e 20 65 78 70 69 72 65 64 20  red. An expired 
182a3 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 66 61 69  statement.** fai
182a4 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72  ls with an error
182a5 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 5f   code of SQLITE_
182a6 53 43 48 45 4d 41 20 69 66 20 69 74 20 69 73 20  SCHEMA if it is 
182a7 65 76 65 72 20 65 78 65 63 75 74 65 64 20 0a 2a  ever executed .*
182a8 2a 20 28 76 69 61 20 73 71 6c 69 74 65 33 5f 73  * (via sqlite3_s
182a9 74 65 70 28 29 29 2e 0a 2a 2a 20 0a 2a 2a 20 49  tep())..** .** I
182aa 66 20 50 31 20 69 73 20 30 2c 20 74 68 65 6e 20  f P1 is 0, then 
182ab 61 6c 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  all SQL statemen
182ac 74 73 20 62 65 63 6f 6d 65 20 65 78 70 69 72 65  ts become expire
182ad 64 2e 20 49 66 20 50 31 20 69 73 20 6e 6f 6e 2d  d. If P1 is non-
182ae 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 6f 6e  zero,.** then on
182af 6c 79 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79  ly the currently
182b0 20 65 78 65 63 75 74 69 6e 67 20 73 74 61 74 65   executing state
182b1 6d 65 6e 74 20 69 73 20 61 66 66 65 63 74 65 64  ment is affected
182b2 2e 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 45 78  . .*/.case OP_Ex
182b3 70 69 72 65 3a 20 7b 0a 20 20 69 66 28 20 21 70  pire: {.  if( !p
182b4 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 73 71  Op->p1 ){.    sq
182b5 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61  lite3ExpirePrepa
182b6 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62  redStatements(db
182b7 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
182b8 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b 0a  p->expired = 1;.
182b9 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
182ba 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
182bb 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
182bc 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 61 62 6c  ./* Opcode: Tabl
182bd 65 4c 6f 63 6b 20 50 31 20 50 32 20 50 33 20 50  eLock P1 P2 P3 P
182be 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 62 74 61 69 6e  4 *.**.** Obtain
182bf 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 70 61 72   a lock on a par
182c0 74 69 63 75 6c 61 72 20 74 61 62 6c 65 2e 20 54  ticular table. T
182c1 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
182c2 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65  is only used whe
182c3 6e 0a 2a 2a 20 74 68 65 20 73 68 61 72 65 64 2d  n.** the shared-
182c4 63 61 63 68 65 20 66 65 61 74 75 72 65 20 69 73  cache feature is
182c5 20 65 6e 61 62 6c 65 64 2e 20 0a 2a 2a 0a 2a 2a   enabled. .**.**
182c6 20 49 66 20 50 31 20 69 73 20 20 74 68 65 20 69   If P1 is  the i
182c7 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
182c8 62 61 73 65 20 69 6e 20 73 71 6c 69 74 65 33 2e  base in sqlite3.
182c9 61 44 62 5b 5d 20 6f 66 20 74 68 65 20 64 61 74  aDb[] of the dat
182ca 61 62 61 73 65 0a 2a 2a 20 6f 6e 20 77 68 69 63  abase.** on whic
182cb 68 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 63  h the lock is ac
182cc 71 75 69 72 65 64 2e 20 20 41 20 72 65 61 64 6c  quired.  A readl
182cd 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20  ock is obtained 
182ce 69 66 20 50 33 3d 3d 30 20 6f 72 0a 2a 2a 20 61  if P3==0 or.** a
182cf 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 50   write lock if P
182d0 33 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 63  3==1..**.** P2 c
182d1 6f 6e 74 61 69 6e 73 20 74 68 65 20 72 6f 6f 74  ontains the root
182d2 2d 70 61 67 65 20 6f 66 20 74 68 65 20 74 61 62  -page of the tab
182d3 6c 65 20 74 6f 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a  le to lock..**.*
182d4 2a 20 50 34 20 63 6f 6e 74 61 69 6e 73 20 61 20  * P4 contains a 
182d5 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e  pointer to the n
182d6 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
182d7 20 62 65 69 6e 67 20 6c 6f 63 6b 65 64 2e 20 54   being locked. T
182d8 68 69 73 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 75  his is only.** u
182d9 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20  sed to generate 
182da 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
182db 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e   if the lock can
182dc 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2e  not be obtained.
182dd 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 61 62 6c  .*/.case OP_Tabl
182de 65 4c 6f 63 6b 3a 20 7b 0a 20 20 69 6e 74 20 70  eLock: {.  int p
182df 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 0a 20 20  1 = pOp->p1; .  
182e0 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 20 3d  u8 isWriteLock =
182e1 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 61 73 73 65   pOp->p3;.  asse
182e2 72 74 28 20 70 31 3e 3d 30 20 26 26 20 70 31 3c  rt( p1>=0 && p1<
182e3 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73  db->nDb );.  ass
182e4 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61  ert( (p->btreeMa
182e5 73 6b 20 26 20 28 31 3c 3c 70 31 29 29 21 3d 30  sk & (1<<p1))!=0
182e6 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73   );.  assert( is
182e7 57 72 69 74 65 4c 6f 63 6b 3d 3d 30 20 7c 7c 20  WriteLock==0 || 
182e8 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 31 20 29  isWriteLock==1 )
182e9 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
182ea 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 64  BtreeLockTable(d
182eb 62 2d 3e 61 44 62 5b 70 31 5d 2e 70 42 74 2c 20  b->aDb[p1].pBt, 
182ec 70 4f 70 2d 3e 70 32 2c 20 69 73 57 72 69 74 65  pOp->p2, isWrite
182ed 4c 6f 63 6b 29 3b 0a 20 20 69 66 28 20 72 63 3d  Lock);.  if( rc=
182ee 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 29  =SQLITE_LOCKED )
182ef 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
182f0 20 2a 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b   *z = pOp->p4.z;
182f1 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
182f2 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
182f3 67 2c 20 22 64 61 74 61 62 61 73 65 20 74 61 62  g, "database tab
182f4 6c 65 20 69 73 20 6c 6f 63 6b 65 64 3a 20 22 2c  le is locked: ",
182f5 20 7a 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20   z, (char*)0);. 
182f6 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65   }.  break;.}.#e
182f7 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
182f8 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
182f9 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
182fa 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
182fb 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a  TABLE./* Opcode:
182fc 20 56 42 65 67 69 6e 20 2a 20 2a 20 2a 20 50 34   VBegin * * * P4
182fd 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 61 20 70 6f   *.**.** P4 a po
182fe 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69  inter to an sqli
182ff 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75  te3_vtab structu
18300 72 65 2e 20 43 61 6c 6c 20 74 68 65 20 78 42 65  re. Call the xBe
18301 67 69 6e 20 6d 65 74 68 6f 64 20 0a 2a 2a 20 66  gin method .** f
18302 6f 72 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a  or that table..*
18303 2f 0a 63 61 73 65 20 4f 50 5f 56 42 65 67 69 6e  /.case OP_VBegin
18304 3a 20 7b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  : {.  rc = sqlit
18305 65 33 56 74 61 62 42 65 67 69 6e 28 64 62 2c 20  e3VtabBegin(db, 
18306 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 29 3b 0a  pOp->p4.pVtab);.
18307 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69    break;.}.#endi
18308 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
18309 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
1830a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1830b 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1830c 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 43  LE./* Opcode: VC
1830d 72 65 61 74 65 20 50 31 20 2a 20 2a 20 50 34 20  reate P1 * * P4 
1830e 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 74 68  *.**.** P4 is th
1830f 65 20 6e 61 6d 65 20 6f 66 20 61 20 76 69 72 74  e name of a virt
18310 75 61 6c 20 74 61 62 6c 65 20 69 6e 20 64 61 74  ual table in dat
18311 61 62 61 73 65 20 50 31 2e 20 43 61 6c 6c 20 74  abase P1. Call t
18312 68 65 20 78 43 72 65 61 74 65 20 6d 65 74 68 6f  he xCreate metho
18313 64 0a 2a 2a 20 66 6f 72 20 74 68 61 74 20 74 61  d.** for that ta
18314 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ble..*/.case OP_
18315 56 43 72 65 61 74 65 3a 20 7b 0a 20 20 72 63 20  VCreate: {.  rc 
18316 3d 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c  = sqlite3VtabCal
18317 6c 43 72 65 61 74 65 28 64 62 2c 20 70 4f 70 2d  lCreate(db, pOp-
18318 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20  >p1, pOp->p4.z, 
18319 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20  &p->zErrMsg);.  
1831a 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20  break;.}.#endif 
1831b 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
1831c 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a  IRTUALTABLE */..
1831d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1831e 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1831f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 44 65 73  ./* Opcode: VDes
18320 74 72 6f 79 20 50 31 20 2a 20 2a 20 50 34 20 2a  troy P1 * * P4 *
18321 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 74 68 65  .**.** P4 is the
18322 20 6e 61 6d 65 20 6f 66 20 61 20 76 69 72 74 75   name of a virtu
18323 61 6c 20 74 61 62 6c 65 20 69 6e 20 64 61 74 61  al table in data
18324 62 61 73 65 20 50 31 2e 20 20 43 61 6c 6c 20 74  base P1.  Call t
18325 68 65 20 78 44 65 73 74 72 6f 79 20 6d 65 74 68  he xDestroy meth
18326 6f 64 0a 2a 2a 20 6f 66 20 74 68 61 74 20 74 61  od.** of that ta
18327 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ble..*/.case OP_
18328 56 44 65 73 74 72 6f 79 3a 20 7b 0a 20 20 70 2d  VDestroy: {.  p-
18329 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20  >inVtabMethod = 
1832a 32 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  2;.  rc = sqlite
1832b 33 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f 79  3VtabCallDestroy
1832c 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f  (db, pOp->p1, pO
1832d 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 70 2d 3e 69  p->p4.z);.  p->i
1832e 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b  nVtabMethod = 0;
1832f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
18330 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
18331 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a  T_VIRTUALTABLE *
18332 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
18333 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
18334 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56  BLE./* Opcode: V
18335 4f 70 65 6e 20 50 31 20 2a 20 2a 20 50 34 20 2a  Open P1 * * P4 *
18336 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70  .**.** P4 is a p
18337 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72 74  ointer to a virt
18338 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74  ual table object
18339 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61  , an sqlite3_vta
1833a 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20  b structure..** 
1833b 50 31 20 69 73 20 61 20 63 75 72 73 6f 72 20 6e  P1 is a cursor n
1833c 75 6d 62 65 72 2e 20 20 54 68 69 73 20 6f 70 63  umber.  This opc
1833d 6f 64 65 20 6f 70 65 6e 73 20 61 20 63 75 72 73  ode opens a curs
1833e 6f 72 20 74 6f 20 74 68 65 20 76 69 72 74 75 61  or to the virtua
1833f 6c 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 73  l.** table and s
18340 74 6f 72 65 73 20 74 68 61 74 20 63 75 72 73 6f  tores that curso
18341 72 20 69 6e 20 50 31 2e 0a 2a 2f 0a 63 61 73 65  r in P1..*/.case
18342 20 4f 50 5f 56 4f 70 65 6e 3a 20 7b 0a 20 20 43   OP_VOpen: {.  C
18343 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 30 3b  ursor *pCur = 0;
18344 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  .  sqlite3_vtab_
18345 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72  cursor *pVtabCur
18346 73 6f 72 20 3d 20 30 3b 0a 0a 20 20 73 71 6c 69  sor = 0;..  sqli
18347 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 20  te3_vtab *pVtab 
18348 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3b  = pOp->p4.pVtab;
18349 0a 20 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  .  sqlite3_modul
1834a 65 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 28 73 71  e *pModule = (sq
1834b 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 70  lite3_module *)p
1834c 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 0a  Vtab->pModule;..
1834d 20 20 61 73 73 65 72 74 28 70 56 74 61 62 20 26    assert(pVtab &
1834e 26 20 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 69 66  & pModule);.  if
1834f 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
18350 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62  ff(db) ) goto ab
18351 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
18352 65 3b 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c  e;.  rc = pModul
18353 65 2d 3e 78 4f 70 65 6e 28 70 56 74 61 62 2c 20  e->xOpen(pVtab, 
18354 26 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20  &pVtabCursor);. 
18355 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
18356 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20  tyOn(db) ) goto 
18357 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73  abort_due_to_mis
18358 75 73 65 3b 0a 20 20 69 66 28 20 53 51 4c 49 54  use;.  if( SQLIT
18359 45 5f 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20  E_OK==rc ){.    
1835a 2f 2a 20 49 6e 69 74 69 61 6c 69 73 65 20 73 71  /* Initialise sq
1835b 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
1835c 72 20 62 61 73 65 20 63 6c 61 73 73 20 2a 2f 0a  r base class */.
1835d 20 20 20 20 70 56 74 61 62 43 75 72 73 6f 72 2d      pVtabCursor-
1835e 3e 70 56 74 61 62 20 3d 20 70 56 74 61 62 3b 0a  >pVtab = pVtab;.
1835f 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  .    /* Initiali
18360 73 65 20 76 64 62 65 20 63 75 72 73 6f 72 20 6f  se vdbe cursor o
18361 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 70 43 75  bject */.    pCu
18362 72 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73  r = allocateCurs
18363 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 26  or(p, pOp->p1, &
18364 70 4f 70 5b 2d 31 5d 2c 20 2d 31 2c 20 30 29 3b  pOp[-1], -1, 0);
18365 0a 20 20 20 20 69 66 28 20 70 43 75 72 20 29 7b  .    if( pCur ){
18366 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 56 74  .      pCur->pVt
18367 61 62 43 75 72 73 6f 72 20 3d 20 70 56 74 61 62  abCursor = pVtab
18368 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 70 43  Cursor;.      pC
18369 75 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 56  ur->pModule = pV
1836a 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62  tabCursor->pVtab
1836b 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d  ->pModule;.    }
1836c 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e  else{.      db->
1836d 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
1836e 3b 0a 20 20 20 20 20 20 70 4d 6f 64 75 6c 65 2d  ;.      pModule-
1836f 3e 78 43 6c 6f 73 65 28 70 56 74 61 62 43 75 72  >xClose(pVtabCur
18370 73 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  sor);.    }.  }.
18371 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69    break;.}.#endi
18372 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
18373 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
18374 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
18375 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
18376 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 46  LE./* Opcode: VF
18377 69 6c 74 65 72 20 50 31 20 50 32 20 50 33 20 50  ilter P1 P2 P3 P
18378 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20  4 *.**.** P1 is 
18379 61 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20  a cursor opened 
1837a 75 73 69 6e 67 20 56 4f 70 65 6e 2e 20 20 50 32  using VOpen.  P2
1837b 20 69 73 20 61 6e 20 61 64 64 72 65 73 73 20 74   is an address t
1837c 6f 20 6a 75 6d 70 20 74 6f 20 69 66 0a 2a 2a 20  o jump to if.** 
1837d 74 68 65 20 66 69 6c 74 65 72 65 64 20 72 65 73  the filtered res
1837e 75 6c 74 20 73 65 74 20 69 73 20 65 6d 70 74 79  ult set is empty
1837f 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 65 69  ..**.** P4 is ei
18380 74 68 65 72 20 4e 55 4c 4c 20 6f 72 20 61 20 73  ther NULL or a s
18381 74 72 69 6e 67 20 74 68 61 74 20 77 61 73 20 67  tring that was g
18382 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 20  enerated by the 
18383 78 42 65 73 74 49 6e 64 65 78 0a 2a 2a 20 6d 65  xBestIndex.** me
18384 74 68 6f 64 20 6f 66 20 74 68 65 20 6d 6f 64 75  thod of the modu
18385 6c 65 2e 20 20 54 68 65 20 69 6e 74 65 72 70 72  le.  The interpr
18386 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 50  etation of the P
18387 34 20 73 74 72 69 6e 67 20 69 73 20 6c 65 66 74  4 string is left
18388 0a 2a 2a 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c  .** to the modul
18389 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
1838a 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63  ..**.** This opc
1838b 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20  ode invokes the 
1838c 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 20 6f  xFilter method o
1838d 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  n the virtual ta
1838e 62 6c 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  ble specified.**
1838f 20 62 79 20 50 31 2e 20 20 54 68 65 20 69 6e 74   by P1.  The int
18390 65 67 65 72 20 71 75 65 72 79 20 70 6c 61 6e 20  eger query plan 
18391 70 61 72 61 6d 65 74 65 72 20 74 6f 20 78 46 69  parameter to xFi
18392 6c 74 65 72 20 69 73 20 73 74 6f 72 65 64 20 69  lter is stored i
18393 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 50 33  n register.** P3
18394 2e 20 52 65 67 69 73 74 65 72 20 50 33 2b 31 20  . Register P3+1 
18395 73 74 6f 72 65 73 20 74 68 65 20 61 72 67 63 20  stores the argc 
18396 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62 65 20  parameter to be 
18397 70 61 73 73 65 64 20 74 6f 20 74 68 65 0a 2a 2a  passed to the.**
18398 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 2e   xFilter method.
18399 20 52 65 67 69 73 74 65 72 73 20 50 33 2b 32 2e   Registers P3+2.
1839a 2e 50 33 2b 31 2b 61 72 67 63 20 61 72 65 20 74  .P3+1+argc are t
1839b 68 65 20 61 72 67 63 20 61 64 64 69 74 69 6f 6e  he argc addition
1839c 61 6c 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73  al.** parameters
1839d 6e 65 61 74 68 20 61 64 64 69 74 69 6f 6e 61 6c  neath additional
1839e 20 70 61 72 61 6d 65 74 65 72 73 20 77 68 69 63   parameters whic
1839f 68 20 61 72 65 20 70 61 73 73 65 64 20 74 6f 0a  h are passed to.
183a0 2a 2a 20 78 46 69 6c 74 65 72 20 61 73 20 61 72  ** xFilter as ar
183a1 67 76 2e 20 52 65 67 69 73 74 65 72 20 50 33 2b  gv. Register P3+
183a2 32 20 62 65 63 6f 6d 65 73 20 61 72 67 76 5b 30  2 becomes argv[0
183a3 5d 20 77 68 65 6e 20 70 61 73 73 65 64 20 74 6f  ] when passed to
183a4 20 78 46 69 6c 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   xFilter..**.** 
183a5 41 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 20 74  A jump is made t
183a6 6f 20 50 32 20 69 66 20 74 68 65 20 72 65 73 75  o P2 if the resu
183a7 6c 74 20 73 65 74 20 61 66 74 65 72 20 66 69 6c  lt set after fil
183a8 74 65 72 69 6e 67 20 77 6f 75 6c 64 20 62 65 20  tering would be 
183a9 65 6d 70 74 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f  empty..*/.case O
183aa 50 5f 56 46 69 6c 74 65 72 3a 20 7b 20 20 20 2f  P_VFilter: {   /
183ab 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 69 6e 74 20  * jump */.  int 
183ac 6e 41 72 67 3b 0a 20 20 69 6e 74 20 69 51 75 65  nArg;.  int iQue
183ad 72 79 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69  ry;.  const sqli
183ae 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
183af 75 6c 65 3b 0a 20 20 4d 65 6d 20 2a 70 51 75 65  ule;.  Mem *pQue
183b0 72 79 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  ry = &p->aMem[pO
183b1 70 2d 3e 70 33 5d 3b 0a 20 20 4d 65 6d 20 2a 70  p->p3];.  Mem *p
183b2 41 72 67 63 20 3d 20 26 70 51 75 65 72 79 5b 31  Argc = &pQuery[1
183b3 5d 3b 0a 0a 20 20 43 75 72 73 6f 72 20 2a 70 43  ];..  Cursor *pC
183b4 75 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f  ur = p->apCsr[pO
183b5 70 2d 3e 70 31 5d 3b 0a 0a 20 20 52 45 47 49 53  p->p1];..  REGIS
183b6 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
183b7 33 2c 20 70 51 75 65 72 79 29 3b 0a 20 20 61 73  3, pQuery);.  as
183b8 73 65 72 74 28 20 70 43 75 72 2d 3e 70 56 74 61  sert( pCur->pVta
183b9 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 4d 6f  bCursor );.  pMo
183ba 64 75 6c 65 20 3d 20 70 43 75 72 2d 3e 70 56 74  dule = pCur->pVt
183bb 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 2d  abCursor->pVtab-
183bc 3e 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 2f 2a 20  >pModule;..  /* 
183bd 47 72 61 62 20 74 68 65 20 69 6e 64 65 78 20 6e  Grab the index n
183be 75 6d 62 65 72 20 61 6e 64 20 61 72 67 63 20 70  umber and argc p
183bf 61 72 61 6d 65 74 65 72 73 20 2a 2f 0a 20 20 61  arameters */.  a
183c0 73 73 65 72 74 28 20 28 70 51 75 65 72 79 2d 3e  ssert( (pQuery->
183c1 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 29 21 3d  flags&MEM_Int)!=
183c2 30 20 26 26 20 70 41 72 67 63 2d 3e 66 6c 61 67  0 && pArgc->flag
183c3 73 3d 3d 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20  s==MEM_Int );.  
183c4 6e 41 72 67 20 3d 20 70 41 72 67 63 2d 3e 75 2e  nArg = pArgc->u.
183c5 69 3b 0a 20 20 69 51 75 65 72 79 20 3d 20 70 51  i;.  iQuery = pQ
183c6 75 65 72 79 2d 3e 75 2e 69 3b 0a 0a 20 20 2f 2a  uery->u.i;..  /*
183c7 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 46 69 6c   Invoke the xFil
183c8 74 65 72 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  ter method */.  
183c9 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 20 3d 20  {.    int res = 
183ca 30 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  0;.    int i;.  
183cb 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 20 3d 20    Mem **apArg = 
183cc 70 2d 3e 61 70 41 72 67 3b 0a 20 20 20 20 66 6f  p->apArg;.    fo
183cd 72 28 69 20 3d 20 30 3b 20 69 3c 6e 41 72 67 3b  r(i = 0; i<nArg;
183ce 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 70 41   i++){.      apA
183cf 72 67 5b 69 5d 20 3d 20 26 70 41 72 67 63 5b 69  rg[i] = &pArgc[i
183d0 2b 31 5d 3b 0a 20 20 20 20 20 20 73 74 6f 72 65  +1];.      store
183d1 54 79 70 65 49 6e 66 6f 28 61 70 41 72 67 5b 69  TypeInfo(apArg[i
183d2 5d 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  ], 0);.    }..  
183d3 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
183d4 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74  etyOff(db) ) got
183d5 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d  o abort_due_to_m
183d6 69 73 75 73 65 3b 0a 20 20 20 20 70 2d 3e 69 6e  isuse;.    p->in
183d7 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a  VtabMethod = 1;.
183d8 20 20 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65      rc = pModule
183d9 2d 3e 78 46 69 6c 74 65 72 28 70 43 75 72 2d 3e  ->xFilter(pCur->
183da 70 56 74 61 62 43 75 72 73 6f 72 2c 20 69 51 75  pVtabCursor, iQu
183db 65 72 79 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20  ery, pOp->p4.z, 
183dc 6e 41 72 67 2c 20 61 70 41 72 67 29 3b 0a 20 20  nArg, apArg);.  
183dd 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f    p->inVtabMetho
183de 64 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 72  d = 0;.    if( r
183df 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
183e0 20 20 20 20 20 20 72 65 73 20 3d 20 70 4d 6f 64        res = pMod
183e1 75 6c 65 2d 3e 78 45 6f 66 28 70 43 75 72 2d 3e  ule->xEof(pCur->
183e2 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20  pVtabCursor);.  
183e3 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
183e4 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20  te3SafetyOn(db) 
183e5 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
183e6 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 20  _to_misuse;..   
183e7 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20   if( res ){.    
183e8 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
183e9 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
183ea 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20  pCur->nullRow = 
183eb 30 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23  0;..  break;.}.#
183ec 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
183ed 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
183ee 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  E */..#ifndef SQ
183ef 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
183f0 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65  LTABLE./* Opcode
183f1 3a 20 56 52 6f 77 69 64 20 50 31 20 50 32 20 2a  : VRowid P1 P2 *
183f2 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65   * *.**.** Store
183f3 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50   into register P
183f4 32 20 20 74 68 65 20 72 6f 77 69 64 20 6f 66 0a  2  the rowid of.
183f5 2a 2a 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74  ** the virtual-t
183f6 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 50 31  able that the P1
183f7 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74   cursor is point
183f8 69 6e 67 20 74 6f 2e 0a 2a 2f 0a 63 61 73 65 20  ing to..*/.case 
183f9 4f 50 5f 56 52 6f 77 69 64 3a 20 7b 20 20 20 20  OP_VRowid: {    
183fa 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32           /* out2
183fb 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20  -prerelease */. 
183fc 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
183fd 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a  odule *pModule;.
183fe 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
183ff 52 6f 77 3b 0a 20 20 43 75 72 73 6f 72 20 2a 70  Row;.  Cursor *p
18400 43 75 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70  Cur = p->apCsr[p
18401 4f 70 2d 3e 70 31 5d 3b 0a 0a 20 20 61 73 73 65  Op->p1];..  asse
18402 72 74 28 20 70 43 75 72 2d 3e 70 56 74 61 62 43  rt( pCur->pVtabC
18403 75 72 73 6f 72 20 29 3b 0a 20 20 69 66 28 20 70  ursor );.  if( p
18404 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a  Cur->nullRow ){.
18405 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20      break;.  }. 
18406 20 70 4d 6f 64 75 6c 65 20 3d 20 70 43 75 72 2d   pModule = pCur-
18407 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56  >pVtabCursor->pV
18408 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20  tab->pModule;.  
18409 61 73 73 65 72 74 28 20 70 4d 6f 64 75 6c 65 2d  assert( pModule-
1840a 3e 78 52 6f 77 69 64 20 29 3b 0a 20 20 69 66 28  >xRowid );.  if(
1840b 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
1840c 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  f(db) ) goto abo
1840d 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1840e 3b 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65  ;.  rc = pModule
1840f 2d 3e 78 52 6f 77 69 64 28 70 43 75 72 2d 3e 70  ->xRowid(pCur->p
18410 56 74 61 62 43 75 72 73 6f 72 2c 20 26 69 52 6f  VtabCursor, &iRo
18411 77 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  w);.  if( sqlite
18412 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20  3SafetyOn(db) ) 
18413 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
18414 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 4d 65 6d 53  o_misuse;.  MemS
18415 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c  etTypeFlag(pOut,
18416 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 70 4f 75   MEM_Int);.  pOu
18417 74 2d 3e 75 2e 69 20 3d 20 69 52 6f 77 3b 0a 20  t->u.i = iRow;. 
18418 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
18419 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1841a 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a  VIRTUALTABLE */.
1841b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1841c 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1841d 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 43 6f  E./* Opcode: VCo
1841e 6c 75 6d 6e 20 50 31 20 50 32 20 50 33 20 2a 20  lumn P1 P2 P3 * 
1841f 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 74 68  *.**.** Store th
18420 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50  e value of the P
18421 32 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 0a 2a  2-th column of.*
18422 2a 20 74 68 65 20 72 6f 77 20 6f 66 20 74 68 65  * the row of the
18423 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 74   virtual-table t
18424 68 61 74 20 74 68 65 20 0a 2a 2a 20 50 31 20 63  hat the .** P1 c
18425 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e  ursor is pointin
18426 67 20 74 6f 20 69 6e 74 6f 20 72 65 67 69 73 74  g to into regist
18427 65 72 20 50 33 2e 0a 2a 2f 0a 63 61 73 65 20 4f  er P3..*/.case O
18428 50 5f 56 43 6f 6c 75 6d 6e 3a 20 7b 0a 20 20 63  P_VColumn: {.  c
18429 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
1842a 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20  ule *pModule;.  
1842b 4d 65 6d 20 2a 70 44 65 73 74 3b 0a 20 20 73 71  Mem *pDest;.  sq
1842c 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 43  lite3_context sC
1842d 6f 6e 74 65 78 74 3b 0a 0a 20 20 43 75 72 73 6f  ontext;..  Curso
1842e 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e 61 70 43  r *pCur = p->apC
1842f 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61  sr[pOp->p1];.  a
18430 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70 56 74  ssert( pCur->pVt
18431 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73  abCursor );.  as
18432 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20  sert( pOp->p3>0 
18433 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e  && pOp->p3<=p->n
18434 4d 65 6d 20 29 3b 0a 20 20 70 44 65 73 74 20 3d  Mem );.  pDest =
18435 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
18436 33 5d 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e  3];.  if( pCur->
18437 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 73  nullRow ){.    s
18438 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
18439 4e 75 6c 6c 28 70 44 65 73 74 29 3b 0a 20 20 20  Null(pDest);.   
1843a 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 70 4d   break;.  }.  pM
1843b 6f 64 75 6c 65 20 3d 20 70 43 75 72 2d 3e 70 56  odule = pCur->pV
1843c 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62  tabCursor->pVtab
1843d 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 73 73  ->pModule;.  ass
1843e 65 72 74 28 20 70 4d 6f 64 75 6c 65 2d 3e 78 43  ert( pModule->xC
1843f 6f 6c 75 6d 6e 20 29 3b 0a 20 20 6d 65 6d 73 65  olumn );.  memse
18440 74 28 26 73 43 6f 6e 74 65 78 74 2c 20 30 2c 20  t(&sContext, 0, 
18441 73 69 7a 65 6f 66 28 73 43 6f 6e 74 65 78 74 29  sizeof(sContext)
18442 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6f 75 74  );..  /* The out
18443 70 75 74 20 63 65 6c 6c 20 6d 61 79 20 61 6c 72  put cell may alr
18444 65 61 64 79 20 68 61 76 65 20 61 20 62 75 66 66  eady have a buff
18445 65 72 20 61 6c 6c 6f 63 61 74 65 64 2e 20 4d 6f  er allocated. Mo
18446 76 65 0a 20 20 2a 2a 20 74 68 65 20 63 75 72 72  ve.  ** the curr
18447 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20 74 6f 20  ent contents to 
18448 73 43 6f 6e 74 65 78 74 2e 73 20 73 6f 20 69 6e  sContext.s so in
18449 20 63 61 73 65 20 74 68 65 20 75 73 65 72 2d 66   case the user-f
1844a 75 6e 63 74 69 6f 6e 20 0a 20 20 2a 2a 20 63 61  unction .  ** ca
1844b 6e 20 75 73 65 20 74 68 65 20 61 6c 72 65 61 64  n use the alread
1844c 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66  y allocated buff
1844d 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 6c  er instead of al
1844e 6c 6f 63 61 74 69 6e 67 20 61 20 0a 20 20 2a 2a  locating a .  **
1844f 20 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20   new one..  */. 
18450 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d   sqlite3VdbeMemM
18451 6f 76 65 28 26 73 43 6f 6e 74 65 78 74 2e 73 2c  ove(&sContext.s,
18452 20 70 44 65 73 74 29 3b 0a 20 20 4d 65 6d 53 65   pDest);.  MemSe
18453 74 54 79 70 65 46 6c 61 67 28 26 73 43 6f 6e 74  tTypeFlag(&sCont
18454 65 78 74 2e 73 2c 20 4d 45 4d 5f 4e 75 6c 6c 29  ext.s, MEM_Null)
18455 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ;..  if( sqlite3
18456 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20  SafetyOff(db) ) 
18457 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
18458 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 72 63 20 3d  o_misuse;.  rc =
18459 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6f 6c 75 6d   pModule->xColum
1845a 6e 28 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72  n(pCur->pVtabCur
1845b 73 6f 72 2c 20 26 73 43 6f 6e 74 65 78 74 2c 20  sor, &sContext, 
1845c 70 4f 70 2d 3e 70 32 29 3b 0a 0a 20 20 2f 2a 20  pOp->p2);..  /* 
1845d 43 6f 70 79 20 74 68 65 20 72 65 73 75 6c 74 20  Copy the result 
1845e 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  of the function 
1845f 74 6f 20 74 68 65 20 50 33 20 72 65 67 69 73 74  to the P3 regist
18460 65 72 2e 20 57 65 0a 20 20 2a 2a 20 64 6f 20 74  er. We.  ** do t
18461 68 69 73 20 72 65 67 61 72 64 6c 65 73 73 20 6f  his regardless o
18462 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  f whether or not
18463 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 65   an error occure
18464 64 20 74 6f 20 65 6e 73 75 72 65 20 61 6e 79 0a  d to ensure any.
18465 20 20 2a 2a 20 64 79 6e 61 6d 69 63 20 61 6c 6c    ** dynamic all
18466 6f 63 61 74 69 6f 6e 20 69 6e 20 73 43 6f 6e 74  ocation in sCont
18467 65 78 74 2e 73 20 28 61 20 4d 65 6d 20 73 74 72  ext.s (a Mem str
18468 75 63 74 29 20 69 73 20 20 72 65 6c 65 61 73 65  uct) is  release
18469 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  d..  */.  sqlite
1846a 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64  3VdbeChangeEncod
1846b 69 6e 67 28 26 73 43 6f 6e 74 65 78 74 2e 73 2c  ing(&sContext.s,
1846c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 52 45   encoding);.  RE
1846d 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70  GISTER_TRACE(pOp
1846e 2d 3e 70 33 2c 20 70 44 65 73 74 29 3b 0a 20 20  ->p3, pDest);.  
1846f 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f  sqlite3VdbeMemMo
18470 76 65 28 70 44 65 73 74 2c 20 26 73 43 6f 6e 74  ve(pDest, &sCont
18471 65 78 74 2e 73 29 3b 0a 20 20 55 50 44 41 54 45  ext.s);.  UPDATE
18472 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 44  _MAX_BLOBSIZE(pD
18473 65 73 74 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c  est);..  if( sql
18474 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
18475 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f   ){.    goto abo
18476 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
18477 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69  ;.  }.  if( sqli
18478 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67  te3VdbeMemTooBig
18479 28 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20 67  (pDest) ){.    g
1847a 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d  oto too_big;.  }
1847b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
1847c 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1847d 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a  T_VIRTUALTABLE *
1847e 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
1847f 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
18480 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56  BLE./* Opcode: V
18481 4e 65 78 74 20 50 31 20 50 32 20 2a 20 2a 20 2a  Next P1 P2 * * *
18482 0a 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 76  .**.** Advance v
18483 69 72 74 75 61 6c 20 74 61 62 6c 65 20 50 31 20  irtual table P1 
18484 74 6f 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20  to the next row 
18485 69 6e 20 69 74 73 20 72 65 73 75 6c 74 20 73 65  in its result se
18486 74 20 61 6e 64 0a 2a 2a 20 6a 75 6d 70 20 74 6f  t and.** jump to
18487 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e   instruction P2.
18488 20 20 4f 72 2c 20 69 66 20 74 68 65 20 76 69 72    Or, if the vir
18489 74 75 61 6c 20 74 61 62 6c 65 20 68 61 73 20 72  tual table has r
1848a 65 61 63 68 65 64 0a 2a 2a 20 74 68 65 20 65 6e  eached.** the en
1848b 64 20 6f 66 20 69 74 73 20 72 65 73 75 6c 74 20  d of its result 
1848c 73 65 74 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74  set, then fall t
1848d 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65  hrough to the ne
1848e 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  xt instruction..
1848f 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 4e 65 78 74  */.case OP_VNext
18490 3a 20 7b 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f  : {   /* jump */
18491 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
18492 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65  _module *pModule
18493 3b 0a 20 20 69 6e 74 20 72 65 73 20 3d 20 30 3b  ;.  int res = 0;
18494 0a 0a 20 20 43 75 72 73 6f 72 20 2a 70 43 75 72  ..  Cursor *pCur
18495 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d   = p->apCsr[pOp-
18496 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  >p1];.  assert( 
18497 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f  pCur->pVtabCurso
18498 72 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d  r );.  if( pCur-
18499 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20  >nullRow ){.    
1849a 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 70 4d 6f  break;.  }.  pMo
1849b 64 75 6c 65 20 3d 20 70 43 75 72 2d 3e 70 56 74  dule = pCur->pVt
1849c 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 2d  abCursor->pVtab-
1849d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 73 73 65  >pModule;.  asse
1849e 72 74 28 20 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65  rt( pModule->xNe
1849f 78 74 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f  xt );..  /* Invo
184a0 6b 65 20 74 68 65 20 78 4e 65 78 74 28 29 20 6d  ke the xNext() m
184a1 65 74 68 6f 64 20 6f 66 20 74 68 65 20 6d 6f 64  ethod of the mod
184a2 75 6c 65 2e 20 54 68 65 72 65 20 69 73 20 6e 6f  ule. There is no
184a3 20 77 61 79 20 66 6f 72 20 74 68 65 0a 20 20 2a   way for the.  *
184a4 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 69 6d 70  * underlying imp
184a5 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 6f 20 72  lementation to r
184a6 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69  eturn an error i
184a7 66 20 6f 6e 65 20 6f 63 63 75 72 73 20 64 75 72  f one occurs dur
184a8 69 6e 67 0a 20 20 2a 2a 20 78 4e 65 78 74 28 29  ing.  ** xNext()
184a9 2e 20 49 6e 73 74 65 61 64 2c 20 69 66 20 61 6e  . Instead, if an
184aa 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74   error occurs, t
184ab 72 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 20  rue is returned 
184ac 28 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74  (indicating that
184ad 20 0a 20 20 2a 2a 20 64 61 74 61 20 69 73 20 61   .  ** data is a
184ae 76 61 69 6c 61 62 6c 65 29 20 61 6e 64 20 74 68  vailable) and th
184af 65 20 65 72 72 6f 72 20 63 6f 64 65 20 72 65 74  e error code ret
184b0 75 72 6e 65 64 20 77 68 65 6e 20 78 43 6f 6c 75  urned when xColu
184b1 6d 6e 20 6f 72 0a 20 20 2a 2a 20 73 6f 6d 65 20  mn or.  ** some 
184b2 6f 74 68 65 72 20 6d 65 74 68 6f 64 20 69 73 20  other method is 
184b3 6e 65 78 74 20 69 6e 76 6f 6b 65 64 20 6f 6e 20  next invoked on 
184b4 74 68 65 20 73 61 76 65 20 76 69 72 74 75 61 6c  the save virtual
184b5 20 74 61 62 6c 65 20 63 75 72 73 6f 72 2e 0a 20   table cursor.. 
184b6 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
184b7 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29  3SafetyOff(db) )
184b8 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
184b9 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 70 2d 3e  to_misuse;.  p->
184ba 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 31  inVtabMethod = 1
184bb 3b 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65  ;.  rc = pModule
184bc 2d 3e 78 4e 65 78 74 28 70 43 75 72 2d 3e 70 56  ->xNext(pCur->pV
184bd 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 70 2d  tabCursor);.  p-
184be 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20  >inVtabMethod = 
184bf 30 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  0;.  if( rc==SQL
184c0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
184c1 73 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 45 6f  s = pModule->xEo
184c2 66 28 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72  f(pCur->pVtabCur
184c3 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  sor);.  }.  if( 
184c4 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
184c5 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  db) ) goto abort
184c6 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a  _due_to_misuse;.
184c7 0a 20 20 69 66 28 20 21 72 65 73 20 29 7b 0a 20  .  if( !res ){. 
184c8 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69     /* If there i
184c9 73 20 64 61 74 61 2c 20 6a 75 6d 70 20 74 6f 20  s data, jump to 
184ca 50 32 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70  P2 */.    pc = p
184cb 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a  Op->p2 - 1;.  }.
184cc 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69    break;.}.#endi
184cd 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
184ce 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
184cf 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
184d0 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
184d1 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 52  LE./* Opcode: VR
184d2 65 6e 61 6d 65 20 50 31 20 2a 20 2a 20 50 34 20  ename P1 * * P4 
184d3 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20  *.**.** P4 is a 
184d4 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72  pointer to a vir
184d5 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63  tual table objec
184d6 74 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74  t, an sqlite3_vt
184d7 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ab structure..**
184d8 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76   This opcode inv
184d9 6f 6b 65 73 20 74 68 65 20 63 6f 72 72 65 73 70  okes the corresp
184da 6f 6e 64 69 6e 67 20 78 52 65 6e 61 6d 65 20 6d  onding xRename m
184db 65 74 68 6f 64 2e 20 54 68 65 20 76 61 6c 75 65  ethod. The value
184dc 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74 65 72 20  .** in register 
184dd 50 31 20 69 73 20 70 61 73 73 65 64 20 61 73 20  P1 is passed as 
184de 74 68 65 20 7a 4e 61 6d 65 20 61 72 67 75 6d 65  the zName argume
184df 6e 74 20 74 6f 20 74 68 65 20 78 52 65 6e 61 6d  nt to the xRenam
184e0 65 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 63 61 73  e method..*/.cas
184e1 65 20 4f 50 5f 56 52 65 6e 61 6d 65 3a 20 7b 0a  e OP_VRename: {.
184e2 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
184e3 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e  pVtab = pOp->p4.
184e4 70 56 74 61 62 3b 0a 20 20 4d 65 6d 20 2a 70 4e  pVtab;.  Mem *pN
184e5 61 6d 65 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  ame = &p->aMem[p
184e6 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
184e7 74 28 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c  t( pVtab->pModul
184e8 65 2d 3e 78 52 65 6e 61 6d 65 20 29 3b 0a 20 20  e->xRename );.  
184e9 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
184ea 4f 70 2d 3e 70 31 2c 20 70 4e 61 6d 65 29 3b 0a  Op->p1, pName);.
184eb 0a 20 20 53 74 72 69 6e 67 69 66 79 28 70 4e 61  .  Stringify(pNa
184ec 6d 65 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 0a  me, encoding);..
184ed 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
184ee 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74  etyOff(db) ) got
184ef 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d  o abort_due_to_m
184f0 69 73 75 73 65 3b 0a 20 20 73 71 6c 69 74 65 33  isuse;.  sqlite3
184f1 56 74 61 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b  VtabLock(pVtab);
184f2 0a 20 20 72 63 20 3d 20 70 56 74 61 62 2d 3e 70  .  rc = pVtab->p
184f3 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 28  Module->xRename(
184f4 70 56 74 61 62 2c 20 70 4e 61 6d 65 2d 3e 7a 29  pVtab, pName->z)
184f5 3b 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 55  ;.  sqlite3VtabU
184f6 6e 6c 6f 63 6b 28 64 62 2c 20 70 56 74 61 62 29  nlock(db, pVtab)
184f7 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ;.  if( sqlite3S
184f8 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f  afetyOn(db) ) go
184f9 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
184fa 6d 69 73 75 73 65 3b 0a 0a 20 20 62 72 65 61 6b  misuse;..  break
184fb 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  ;.}.#endif..#ifn
184fc 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
184fd 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20  VIRTUALTABLE./* 
184fe 4f 70 63 6f 64 65 3a 20 56 55 70 64 61 74 65 20  Opcode: VUpdate 
184ff 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a  P1 P2 P3 P4 *.**
18500 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e  .** P4 is a poin
18501 74 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c  ter to a virtual
18502 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61   table object, a
18503 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73  n sqlite3_vtab s
18504 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 69  tructure..** Thi
18505 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73  s opcode invokes
18506 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
18507 6e 67 20 78 55 70 64 61 74 65 20 6d 65 74 68 6f  ng xUpdate metho
18508 64 2e 20 50 32 20 76 61 6c 75 65 73 0a 2a 2a 20  d. P2 values.** 
18509 61 72 65 20 63 6f 6e 74 69 67 75 6f 75 73 20 6d  are contiguous m
1850a 65 6d 6f 72 79 20 63 65 6c 6c 73 20 73 74 61 72  emory cells star
1850b 74 69 6e 67 20 61 74 20 50 33 20 74 6f 20 70 61  ting at P3 to pa
1850c 73 73 20 74 6f 20 74 68 65 20 78 55 70 64 61 74  ss to the xUpdat
1850d 65 20 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e  e .** invocation
1850e 2e 20 54 68 65 20 76 61 6c 75 65 20 69 6e 20 72  . The value in r
1850f 65 67 69 73 74 65 72 20 28 50 33 2b 50 32 2d 31  egister (P3+P2-1
18510 29 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f  ) corresponds to
18511 20 74 68 65 20 0a 2a 2a 20 70 32 74 68 20 65 6c   the .** p2th el
18512 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 72 67  ement of the arg
18513 76 20 61 72 72 61 79 20 70 61 73 73 65 64 20 74  v array passed t
18514 6f 20 78 55 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a  o xUpdate..**.**
18515 20 54 68 65 20 78 55 70 64 61 74 65 20 6d 65 74   The xUpdate met
18516 68 6f 64 20 77 69 6c 6c 20 64 6f 20 61 20 44 45  hod will do a DE
18517 4c 45 54 45 20 6f 72 20 61 6e 20 49 4e 53 45 52  LETE or an INSER
18518 54 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20 54 68  T or both..** Th
18519 65 20 61 72 67 76 5b 30 5d 20 65 6c 65 6d 65 6e  e argv[0] elemen
1851a 74 20 28 77 68 69 63 68 20 63 6f 72 72 65 73 70  t (which corresp
1851b 6f 6e 64 73 20 74 6f 20 6d 65 6d 6f 72 79 20 63  onds to memory c
1851c 65 6c 6c 20 50 33 29 0a 2a 2a 20 69 73 20 74 68  ell P3).** is th
1851d 65 20 72 6f 77 69 64 20 6f 66 20 61 20 72 6f 77  e rowid of a row
1851e 20 74 6f 20 64 65 6c 65 74 65 2e 20 20 49 66 20   to delete.  If 
1851f 61 72 67 76 5b 30 5d 20 69 73 20 4e 55 4c 4c 20  argv[0] is NULL 
18520 74 68 65 6e 20 6e 6f 20 0a 2a 2a 20 64 65 6c 65  then no .** dele
18521 74 69 6f 6e 20 6f 63 63 75 72 73 2e 20 20 54 68  tion occurs.  Th
18522 65 20 61 72 67 76 5b 31 5d 20 65 6c 65 6d 65 6e  e argv[1] elemen
18523 74 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 6f  t is the rowid o
18524 66 20 74 68 65 20 6e 65 77 20 0a 2a 2a 20 72 6f  f the new .** ro
18525 77 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20  w.  This can be 
18526 4e 55 4c 4c 20 74 6f 20 68 61 76 65 20 74 68 65  NULL to have the
18527 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73   virtual table s
18528 65 6c 65 63 74 20 74 68 65 20 6e 65 77 20 0a 2a  elect the new .*
18529 2a 20 72 6f 77 69 64 20 66 6f 72 20 69 74 73 65  * rowid for itse
1852a 6c 66 2e 20 20 54 68 65 20 73 75 62 73 65 71 75  lf.  The subsequ
1852b 65 6e 74 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20  ent elements in 
1852c 74 68 65 20 61 72 72 61 79 20 61 72 65 20 0a 2a  the array are .*
1852d 2a 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20  * the values of 
1852e 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 6e  columns in the n
1852f 65 77 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 49 66  ew row..**.** If
18530 20 50 32 3d 3d 31 20 74 68 65 6e 20 6e 6f 20 69   P2==1 then no i
18531 6e 73 65 72 74 20 69 73 20 70 65 72 66 6f 72 6d  nsert is perform
18532 65 64 2e 20 20 61 72 67 76 5b 30 5d 20 69 73 20  ed.  argv[0] is 
18533 74 68 65 20 72 6f 77 69 64 20 6f 66 0a 2a 2a 20  the rowid of.** 
18534 61 20 72 6f 77 20 74 6f 20 64 65 6c 65 74 65 2e  a row to delete.
18535 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 62  .**.** P1 is a b
18536 6f 6f 6c 65 61 6e 20 66 6c 61 67 2e 20 49 66 20  oolean flag. If 
18537 69 74 20 69 73 20 73 65 74 20 74 6f 20 74 72 75  it is set to tru
18538 65 20 61 6e 64 20 74 68 65 20 78 55 70 64 61 74  e and the xUpdat
18539 65 20 63 61 6c 6c 0a 2a 2a 20 69 73 20 73 75 63  e call.** is suc
1853a 63 65 73 73 66 75 6c 2c 20 74 68 65 6e 20 74 68  cessful, then th
1853b 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
1853c 20 62 79 20 73 71 6c 69 74 65 33 5f 6c 61 73 74   by sqlite3_last
1853d 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20  _insert_rowid() 
1853e 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68  .** is set to th
1853f 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72  e value of the r
18540 6f 77 69 64 20 66 6f 72 20 74 68 65 20 72 6f 77  owid for the row
18541 20 6a 75 73 74 20 69 6e 73 65 72 74 65 64 2e 0a   just inserted..
18542 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 55 70 64 61  */.case OP_VUpda
18543 74 65 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  te: {.  sqlite3_
18544 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20 70 4f  vtab *pVtab = pO
18545 70 2d 3e 70 34 2e 70 56 74 61 62 3b 0a 20 20 73  p->p4.pVtab;.  s
18546 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
18547 4d 6f 64 75 6c 65 20 3d 20 28 73 71 6c 69 74 65  Module = (sqlite
18548 33 5f 6d 6f 64 75 6c 65 20 2a 29 70 56 74 61 62  3_module *)pVtab
18549 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74  ->pModule;.  int
1854a 20 6e 41 72 67 20 3d 20 70 4f 70 2d 3e 70 32 3b   nArg = pOp->p2;
1854b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1854c 70 34 74 79 70 65 3d 3d 50 34 5f 56 54 41 42 20  p4type==P4_VTAB 
1854d 29 3b 0a 20 20 69 66 28 20 70 4d 6f 64 75 6c 65  );.  if( pModule
1854e 2d 3e 78 55 70 64 61 74 65 3d 3d 30 20 29 7b 0a  ->xUpdate==0 ){.
1854f 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
18550 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67  ring(&p->zErrMsg
18551 2c 20 22 72 65 61 64 2d 6f 6e 6c 79 20 74 61 62  , "read-only tab
18552 6c 65 22 2c 20 30 29 3b 0a 20 20 20 20 72 63 20  le", 0);.    rc 
18553 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
18554 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74    }else{.    int
18555 20 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 5f 69   i;.    sqlite_i
18556 6e 74 36 34 20 72 6f 77 69 64 3b 0a 20 20 20 20  nt64 rowid;.    
18557 4d 65 6d 20 2a 2a 61 70 41 72 67 20 3d 20 70 2d  Mem **apArg = p-
18558 3e 61 70 41 72 67 3b 0a 20 20 20 20 4d 65 6d 20  >apArg;.    Mem 
18559 2a 70 58 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  *pX = &p->aMem[p
1855a 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 66 6f 72  Op->p3];.    for
1855b 28 69 3d 30 3b 20 69 3c 6e 41 72 67 3b 20 69 2b  (i=0; i<nArg; i+
1855c 2b 29 7b 0a 20 20 20 20 20 20 73 74 6f 72 65 54  +){.      storeT
1855d 79 70 65 49 6e 66 6f 28 70 58 2c 20 30 29 3b 0a  ypeInfo(pX, 0);.
1855e 20 20 20 20 20 20 61 70 41 72 67 5b 69 5d 20 3d        apArg[i] =
1855f 20 70 58 3b 0a 20 20 20 20 20 20 70 58 2b 2b 3b   pX;.      pX++;
18560 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
18561 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
18562 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  db) ) goto abort
18563 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a  _due_to_misuse;.
18564 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 4c      sqlite3VtabL
18565 6f 63 6b 28 70 56 74 61 62 29 3b 0a 20 20 20 20  ock(pVtab);.    
18566 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 55  rc = pModule->xU
18567 70 64 61 74 65 28 70 56 74 61 62 2c 20 6e 41 72  pdate(pVtab, nAr
18568 67 2c 20 61 70 41 72 67 2c 20 26 72 6f 77 69 64  g, apArg, &rowid
18569 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74  );.    sqlite3Vt
1856a 61 62 55 6e 6c 6f 63 6b 28 64 62 2c 20 70 56 74  abUnlock(db, pVt
1856b 61 62 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  ab);.    if( sql
1856c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
1856d 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
1856e 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20  e_to_misuse;.   
1856f 20 69 66 28 20 70 4f 70 2d 3e 70 31 20 26 26 20   if( pOp->p1 && 
18570 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
18571 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e  .      assert( n
18572 41 72 67 3e 31 20 26 26 20 61 70 41 72 67 5b 30  Arg>1 && apArg[0
18573 5d 20 26 26 20 28 61 70 41 72 67 5b 30 5d 2d 3e  ] && (apArg[0]->
18574 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 29 20  flags&MEM_Null) 
18575 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 61 73  );.      db->las
18576 74 52 6f 77 69 64 20 3d 20 72 6f 77 69 64 3b 0a  tRowid = rowid;.
18577 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 43 68      }.    p->nCh
18578 61 6e 67 65 2b 2b 3b 0a 20 20 7d 0a 20 20 62 72  ange++;.  }.  br
18579 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  eak;.}.#endif /*
1857a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1857b 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69  TUALTABLE */..#i
1857c 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1857d 54 5f 54 52 41 43 45 0a 2f 2a 20 4f 70 63 6f 64  T_TRACE./* Opcod
1857e 65 3a 20 54 72 61 63 65 20 2a 20 2a 20 2a 20 50  e: Trace * * * P
1857f 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 72 61  4 *.**.** If tra
18580 63 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 20  cing is enabled 
18581 28 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f  (by the sqlite3_
18582 74 72 61 63 65 28 29 29 20 69 6e 74 65 72 66 61  trace()) interfa
18583 63 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20  ce, then.** the 
18584 55 54 46 2d 38 20 73 74 72 69 6e 67 20 63 6f 6e  UTF-8 string con
18585 74 61 69 6e 65 64 20 69 6e 20 50 34 20 69 73 20  tained in P4 is 
18586 65 6d 69 74 74 65 64 20 6f 6e 20 74 68 65 20 74  emitted on the t
18587 72 61 63 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a  race callback..*
18588 2f 0a 63 61 73 65 20 4f 50 5f 54 72 61 63 65 3a  /.case OP_Trace:
18589 20 7b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 34   {.  if( pOp->p4
1858a 2e 7a 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62  .z ){.    if( db
1858b 2d 3e 78 54 72 61 63 65 20 29 7b 0a 20 20 20 20  ->xTrace ){.    
1858c 20 20 64 62 2d 3e 78 54 72 61 63 65 28 64 62 2d    db->xTrace(db-
1858d 3e 70 54 72 61 63 65 41 72 67 2c 20 70 4f 70 2d  >pTraceArg, pOp-
1858e 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 7d 0a 23 69  >p4.z);.    }.#i
1858f 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
18590 47 0a 20 20 20 20 69 66 28 20 28 64 62 2d 3e 66  G.    if( (db->f
18591 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 71  lags & SQLITE_Sq
18592 6c 54 72 61 63 65 29 21 3d 30 20 29 7b 0a 20 20  lTrace)!=0 ){.  
18593 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
18594 50 72 69 6e 74 66 28 22 53 51 4c 2d 74 72 61 63  Printf("SQL-trac
18595 65 3a 20 25 73 5c 6e 22 2c 20 70 4f 70 2d 3e 70  e: %s\n", pOp->p
18596 34 2e 7a 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  4.z);.    }.#end
18597 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42  if /* SQLITE_DEB
18598 55 47 20 2a 2f 0a 20 20 7d 0a 20 20 62 72 65 61  UG */.  }.  brea
18599 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  k;.}.#endif.../*
1859a 20 4f 70 63 6f 64 65 3a 20 4e 6f 6f 70 20 2a 20   Opcode: Noop * 
1859b 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 6f  * * * *.**.** Do
1859c 20 6e 6f 74 68 69 6e 67 2e 20 20 54 68 69 73 20   nothing.  This 
1859d 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 6f  instruction is o
1859e 66 74 65 6e 20 75 73 65 66 75 6c 20 61 73 20 61  ften useful as a
1859f 20 6a 75 6d 70 0a 2a 2a 20 64 65 73 74 69 6e 61   jump.** destina
185a0 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 54  tion..*/./*.** T
185a1 68 65 20 6d 61 67 69 63 20 45 78 70 6c 61 69 6e  he magic Explain
185a2 20 6f 70 63 6f 64 65 20 61 72 65 20 6f 6e 6c 79   opcode are only
185a3 20 69 6e 73 65 72 74 65 64 20 77 68 65 6e 20 65   inserted when e
185a4 78 70 6c 61 69 6e 3d 3d 32 20 28 77 68 69 63 68  xplain==2 (which
185a5 0a 2a 2a 20 69 73 20 74 6f 20 73 61 79 20 77 68  .** is to say wh
185a6 65 6e 20 74 68 65 20 45 58 50 4c 41 49 4e 20 51  en the EXPLAIN Q
185a7 55 45 52 59 20 50 4c 41 4e 20 73 79 6e 74 61 78  UERY PLAN syntax
185a8 20 69 73 20 75 73 65 64 2e 29 0a 2a 2a 20 54 68   is used.).** Th
185a9 69 73 20 6f 70 63 6f 64 65 20 72 65 63 6f 72 64  is opcode record
185aa 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72  s information fr
185ab 6f 6d 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 72  om the optimizer
185ac 2e 20 20 49 74 20 69 73 20 74 68 65 0a 2a 2a 20  .  It is the.** 
185ad 74 68 65 20 73 61 6d 65 20 61 73 20 61 20 6e 6f  the same as a no
185ae 2d 6f 70 2e 20 20 54 68 69 73 20 6f 70 63 6f 64  -op.  This opcod
185af 65 73 6e 65 76 65 72 20 61 70 70 65 61 72 73 20  esnever appears 
185b0 69 6e 20 61 20 72 65 61 6c 20 56 4d 20 70 72 6f  in a real VM pro
185b1 67 72 61 6d 2e 0a 2a 2f 0a 64 65 66 61 75 6c 74  gram..*/.default
185b2 3a 20 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  : {          /* 
185b3 54 68 69 73 20 69 73 20 72 65 61 6c 6c 79 20 4f  This is really O
185b4 50 5f 4e 6f 6f 70 20 61 6e 64 20 4f 50 5f 45 78  P_Noop and OP_Ex
185b5 70 6c 61 69 6e 20 2a 2f 0a 20 20 62 72 65 61 6b  plain */.  break
185b6 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
185b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
185b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
185b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
185ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
185bb 2a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 73 65 73  ***.** The cases
185bc 20 6f 66 20 74 68 65 20 73 77 69 74 63 68 20 73   of the switch s
185bd 74 61 74 65 6d 65 6e 74 20 61 62 6f 76 65 20 74  tatement above t
185be 68 69 73 20 6c 69 6e 65 20 73 68 6f 75 6c 64 20  his line should 
185bf 61 6c 6c 20 62 65 20 69 6e 64 65 6e 74 65 64 0a  all be indented.
185c0 2a 2a 20 62 79 20 36 20 73 70 61 63 65 73 2e 20  ** by 6 spaces. 
185c1 20 42 75 74 20 74 68 65 20 6c 65 66 74 2d 6d 6f   But the left-mo
185c2 73 74 20 36 20 73 70 61 63 65 73 20 68 61 76 65  st 6 spaces have
185c3 20 62 65 65 6e 20 72 65 6d 6f 76 65 64 20 74 6f   been removed to
185c4 20 69 6d 70 72 6f 76 65 20 74 68 65 0a 2a 2a 20   improve the.** 
185c5 72 65 61 64 61 62 69 6c 69 74 79 2e 20 20 46 72  readability.  Fr
185c6 6f 6d 20 74 68 69 73 20 70 6f 69 6e 74 20 6f 6e  om this point on
185c7 20 64 6f 77 6e 2c 20 74 68 65 20 6e 6f 72 6d 61   down, the norma
185c8 6c 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 72 75  l indentation ru
185c9 6c 65 73 20 61 72 65 0a 2a 2a 20 72 65 73 74 6f  les are.** resto
185ca 72 65 64 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  red..***********
185cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
185cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
185cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
185ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
185cf 2a 2a 2f 0a 20 20 20 20 7d 0a 0a 23 69 66 64 65  **/.    }..#ifde
185d0 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20  f VDBE_PROFILE. 
185d1 20 20 20 7b 0a 20 20 20 20 20 20 6c 6f 6e 67 20     {.      long 
185d2 6c 6f 6e 67 20 65 6c 61 70 73 65 20 3d 20 68 77  long elapse = hw
185d3 74 69 6d 65 28 29 20 2d 20 73 74 61 72 74 3b 0a  time() - start;.
185d4 20 20 20 20 20 20 70 4f 70 2d 3e 63 79 63 6c 65        pOp->cycle
185d5 73 20 2b 3d 20 65 6c 61 70 73 65 3b 0a 20 20 20  s += elapse;.   
185d6 20 20 20 70 4f 70 2d 3e 63 6e 74 2b 2b 3b 0a 23     pOp->cnt++;.#
185d7 69 66 20 30 0a 20 20 20 20 20 20 20 20 66 70 72  if 0.        fpr
185d8 69 6e 74 66 28 73 74 64 6f 75 74 2c 20 22 25 31  intf(stdout, "%1
185d9 30 6c 6c 64 20 22 2c 20 65 6c 61 70 73 65 29 3b  0lld ", elapse);
185da 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
185db 56 64 62 65 50 72 69 6e 74 4f 70 28 73 74 64 6f  VdbePrintOp(stdo
185dc 75 74 2c 20 6f 72 69 67 50 63 2c 20 26 70 2d 3e  ut, origPc, &p->
185dd 61 4f 70 5b 6f 72 69 67 50 63 5d 29 3b 0a 23 65  aOp[origPc]);.#e
185de 6e 64 69 66 0a 20 20 20 20 7d 0a 23 65 6e 64 69  ndif.    }.#endi
185df 66 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f  f..    /* The fo
185e0 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 61 64 64  llowing code add
185e1 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 65  s nothing to the
185e2 20 61 63 74 75 61 6c 20 66 75 6e 63 74 69 6f 6e   actual function
185e3 61 6c 69 74 79 0a 20 20 20 20 2a 2a 20 6f 66 20  ality.    ** of 
185e4 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 49 74  the program.  It
185e5 20 69 73 20 6f 6e 6c 79 20 68 65 72 65 20 66 6f   is only here fo
185e6 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65  r testing and de
185e7 62 75 67 67 69 6e 67 2e 0a 20 20 20 20 2a 2a 20  bugging..    ** 
185e8 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e  On the other han
185e9 64 2c 20 69 74 20 64 6f 65 73 20 62 75 72 6e 20  d, it does burn 
185ea 43 50 55 20 63 79 63 6c 65 73 20 65 76 65 72 79  CPU cycles every
185eb 20 74 69 6d 65 20 74 68 72 6f 75 67 68 0a 20 20   time through.  
185ec 20 20 2a 2a 20 74 68 65 20 65 76 61 6c 75 61 74    ** the evaluat
185ed 6f 72 20 6c 6f 6f 70 2e 20 20 53 6f 20 77 65 20  or loop.  So we 
185ee 63 61 6e 20 6c 65 61 76 65 20 69 74 20 6f 75 74  can leave it out
185ef 20 77 68 65 6e 20 4e 44 45 42 55 47 20 69 73 20   when NDEBUG is 
185f0 64 65 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a  defined..    */.
185f1 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
185f2 20 20 20 61 73 73 65 72 74 28 20 70 63 3e 3d 2d     assert( pc>=-
185f3 31 20 26 26 20 70 63 3c 70 2d 3e 6e 4f 70 20 29  1 && pc<p->nOp )
185f4 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
185f5 5f 44 45 42 55 47 0a 20 20 20 20 69 66 28 20 70  _DEBUG.    if( p
185f6 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20  ->trace ){.     
185f7 20 69 66 28 20 72 63 21 3d 30 20 29 20 66 70 72   if( rc!=0 ) fpr
185f8 69 6e 74 66 28 70 2d 3e 74 72 61 63 65 2c 22 72  intf(p->trace,"r
185f9 63 3d 25 64 5c 6e 22 2c 72 63 29 3b 0a 20 20 20  c=%d\n",rc);.   
185fa 20 20 20 69 66 28 20 6f 70 50 72 6f 70 65 72 74     if( opPropert
185fb 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50  y & OPFLG_OUT2_P
185fc 52 45 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 20  RERELEASE ){.   
185fd 20 20 20 20 20 72 65 67 69 73 74 65 72 54 72 61       registerTra
185fe 63 65 28 70 2d 3e 74 72 61 63 65 2c 20 70 4f 70  ce(p->trace, pOp
185ff 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 20  ->p2, pOut);.   
18600 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6f     }.      if( o
18601 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c  pProperty & OPFL
18602 47 5f 4f 55 54 33 20 29 7b 0a 20 20 20 20 20 20  G_OUT3 ){.      
18603 20 20 72 65 67 69 73 74 65 72 54 72 61 63 65 28    registerTrace(
18604 70 2d 3e 74 72 61 63 65 2c 20 70 4f 70 2d 3e 70  p->trace, pOp->p
18605 33 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 20 20  3, pOut);.      
18606 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 20  }.    }.#endif  
18607 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  /* SQLITE_DEBUG 
18608 2a 2f 0a 23 65 6e 64 69 66 20 20 2f 2a 20 4e 44  */.#endif  /* ND
18609 45 42 55 47 20 2a 2f 0a 20 20 7d 20 20 2f 2a 20  EBUG */.  }  /* 
1860a 54 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66  The end of the f
1860b 6f 72 28 3b 3b 29 20 6c 6f 6f 70 20 74 68 65 20  or(;;) loop the 
1860c 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 6f 70  loops through op
1860d 63 6f 64 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 49  codes */..  /* I
1860e 66 20 77 65 20 72 65 61 63 68 20 74 68 69 73 20  f we reach this 
1860f 70 6f 69 6e 74 2c 20 69 74 20 6d 65 61 6e 73 20  point, it means 
18610 74 68 61 74 20 65 78 65 63 75 74 69 6f 6e 20 69  that execution i
18611 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 0a  s finished with.
18612 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 66    ** an error of
18613 20 73 6f 6d 65 20 6b 69 6e 64 2e 0a 20 20 2a 2f   some kind..  */
18614 0a 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74  .vdbe_error_halt
18615 3a 0a 20 20 61 73 73 65 72 74 28 20 72 63 20 29  :.  assert( rc )
18616 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a  ;.  p->rc = rc;.
18617 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
18618 52 4f 52 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ROR;.  sqlite3Vd
18619 62 65 48 61 6c 74 28 70 29 3b 0a 0a 20 20 2f 2a  beHalt(p);..  /*
1861a 20 54 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c   This is the onl
1861b 79 20 77 61 79 20 6f 75 74 20 6f 66 20 74 68 69  y way out of thi
1861c 73 20 70 72 6f 63 65 64 75 72 65 2e 20 20 57 65  s procedure.  We
1861d 20 68 61 76 65 20 74 6f 0a 20 20 2a 2a 20 72 65   have to.  ** re
1861e 6c 65 61 73 65 20 74 68 65 20 6d 75 74 65 78 65  lease the mutexe
1861f 73 20 6f 6e 20 62 74 72 65 65 73 20 74 68 61 74  s on btrees that
18620 20 77 65 72 65 20 61 63 71 75 69 72 65 64 20 61   were acquired a
18621 74 20 74 68 65 0a 20 20 2a 2a 20 74 6f 70 2e 20  t the.  ** top. 
18622 2a 2f 0a 76 64 62 65 5f 72 65 74 75 72 6e 3a 0a  */.vdbe_return:.
18623 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75    sqlite3BtreeMu
18624 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 26 70  texArrayLeave(&p
18625 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20 72 65 74  ->aMutex);.  ret
18626 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 4a 75  urn rc;..  /* Ju
18627 6d 70 20 74 6f 20 68 65 72 65 20 69 66 20 61 20  mp to here if a 
18628 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 6c  string or blob l
18629 61 72 67 65 72 20 74 68 61 6e 20 53 51 4c 49 54  arger than SQLIT
1862a 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a 20 20 2a  E_MAX_LENGTH.  *
1862b 2a 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  * is encountered
1862c 2e 0a 20 20 2a 2f 0a 74 6f 6f 5f 62 69 67 3a 0a  ..  */.too_big:.
1862d 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
1862e 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  ng(&p->zErrMsg, 
1862f 22 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20  "string or blob 
18630 74 6f 6f 20 62 69 67 22 2c 20 28 63 68 61 72 2a  too big", (char*
18631 29 30 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49  )0);.  rc = SQLI
18632 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 67 6f 74  TE_TOOBIG;.  got
18633 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c  o vdbe_error_hal
18634 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f  t;..  /* Jump to
18635 20 68 65 72 65 20 69 66 20 61 20 6d 61 6c 6c 6f   here if a mallo
18636 63 28 29 20 66 61 69 6c 73 2e 0a 20 20 2a 2f 0a  c() fails..  */.
18637 6e 6f 5f 6d 65 6d 3a 0a 20 20 64 62 2d 3e 6d 61  no_mem:.  db->ma
18638 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
18639 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
1863a 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  ng(&p->zErrMsg, 
1863b 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 2c  "out of memory",
1863c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20 72 63   (char*)0);.  rc
1863d 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
1863e 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72  .  goto vdbe_err
1863f 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a  or_halt;..  /* J
18640 75 6d 70 20 74 6f 20 68 65 72 65 20 66 6f 72 20  ump to here for 
18641 61 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  an SQLITE_MISUSE
18642 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 61 62 6f   error..  */.abo
18643 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
18644 3a 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  :.  rc = SQLITE_
18645 4d 49 53 55 53 45 3b 0a 20 20 2f 2a 20 46 61 6c  MISUSE;.  /* Fal
18646 6c 20 74 68 72 75 20 69 6e 74 6f 20 61 62 6f 72  l thru into abor
18647 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 20 2a  t_due_to_error *
18648 2f 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20  /..  /* Jump to 
18649 68 65 72 65 20 66 6f 72 20 61 6e 79 20 6f 74 68  here for any oth
1864a 65 72 20 6b 69 6e 64 20 6f 66 20 66 61 74 61 6c  er kind of fatal
1864b 20 65 72 72 6f 72 2e 20 20 54 68 65 20 22 72 63   error.  The "rc
1864c 22 20 76 61 72 69 61 62 6c 65 0a 20 20 2a 2a 20  " variable.  ** 
1864d 73 68 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20  should hold the 
1864e 65 72 72 6f 72 20 6e 75 6d 62 65 72 2e 0a 20 20  error number..  
1864f 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  */.abort_due_to_
18650 65 72 72 6f 72 3a 0a 20 20 61 73 73 65 72 74 28  error:.  assert(
18651 20 70 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 20 29   p->zErrMsg==0 )
18652 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  ;.  if( db->mall
18653 6f 63 46 61 69 6c 65 64 20 29 20 72 63 20 3d 20  ocFailed ) rc = 
18654 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
18655 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
18656 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 73 71  (&p->zErrMsg, sq
18657 6c 69 74 65 33 45 72 72 53 74 72 28 72 63 29 2c  lite3ErrStr(rc),
18658 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20 67 6f   (char*)0);.  go
18659 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61  to vdbe_error_ha
1865a 6c 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74  lt;..  /* Jump t
1865b 6f 20 68 65 72 65 20 69 66 20 74 68 65 20 73 71  o here if the sq
1865c 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28  lite3_interrupt(
1865d 29 20 41 50 49 20 73 65 74 73 20 74 68 65 20 69  ) API sets the i
1865e 6e 74 65 72 72 75 70 74 0a 20 20 2a 2a 20 66 6c  nterrupt.  ** fl
1865f 61 67 2e 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f 64  ag..  */.abort_d
18660 75 65 5f 74 6f 5f 69 6e 74 65 72 72 75 70 74 3a  ue_to_interrupt:
18661 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 75  .  assert( db->u
18662 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20  1.isInterrupted 
18663 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45  );.  rc = SQLITE
18664 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 70 2d  _INTERRUPT;.  p-
18665 3e 72 63 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69  >rc = rc;.  sqli
18666 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
18667 3e 7a 45 72 72 4d 73 67 2c 20 73 71 6c 69 74 65  >zErrMsg, sqlite
18668 33 45 72 72 53 74 72 28 72 63 29 2c 20 28 63 68  3ErrStr(rc), (ch
18669 61 72 2a 29 30 29 3b 0a 20 20 67 6f 74 6f 20 76  ar*)0);.  goto v
1866a 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a  dbe_error_halt;.
1866b 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
1866c 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 2e 63  ** End of vdbe.c
1866d 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1866e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1866f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18670 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
18671 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64  ** Begin file vd
18672 62 65 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a  beblob.c *******
18673 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18674 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18675 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61  */./*.** 2007 Ma
18676 79 20 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75  y 1.**.** The au
18677 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
18678 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
18679 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
1867a 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
1867b 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
1867c 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
1867d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
1867e 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
1867f 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
18680 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
18681 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
18682 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
18683 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
18684 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
18685 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
18686 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
18687 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
18688 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18689 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1868a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1868b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1868c 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
1868d 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
1868e 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
1868f 6e 74 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 42  nt incremental B
18690 4c 4f 42 20 49 2f 4f 2e 0a 2a 2a 0a 2a 2a 20 24  LOB I/O..**.** $
18691 49 64 3a 20 76 64 62 65 62 6c 6f 62 2e 63 2c 76  Id: vdbeblob.c,v
18692 20 31 2e 32 32 20 32 30 30 38 2f 30 34 2f 32 34   1.22 2008/04/24
18693 20 30 39 3a 34 39 3a 35 35 20 64 61 6e 69 65 6c   09:49:55 daniel
18694 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a  k1977 Exp $.*/..
18695 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
18696 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 0a 2f  OMIT_INCRBLOB../
18697 2a 0a 2a 2a 20 56 61 6c 69 64 20 73 71 6c 69 74  *.** Valid sqlit
18698 65 33 5f 62 6c 6f 62 2a 20 68 61 6e 64 6c 65 73  e3_blob* handles
18699 20 70 6f 69 6e 74 20 74 6f 20 49 6e 63 72 62 6c   point to Incrbl
1869a 6f 62 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a  ob structures..*
1869b 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
1869c 20 49 6e 63 72 62 6c 6f 62 20 49 6e 63 72 62 6c   Incrblob Incrbl
1869d 6f 62 3b 0a 73 74 72 75 63 74 20 49 6e 63 72 62  ob;.struct Incrb
1869e 6c 6f 62 20 7b 0a 20 20 69 6e 74 20 66 6c 61 67  lob {.  int flag
1869f 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
186a0 2f 2a 20 43 6f 70 79 20 6f 66 20 22 66 6c 61 67  /* Copy of "flag
186a1 73 22 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c  s" passed to sql
186a2 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29  ite3_blob_open()
186a3 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b   */.  int nByte;
186a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
186a5 20 53 69 7a 65 20 6f 66 20 6f 70 65 6e 20 62 6c   Size of open bl
186a6 6f 62 2c 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  ob, in bytes */.
186a7 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 20    int iOffset;  
186a8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
186a9 65 20 6f 66 66 73 65 74 20 6f 66 20 62 6c 6f 62  e offset of blob
186aa 20 69 6e 20 63 75 72 73 6f 72 20 64 61 74 61 20   in cursor data 
186ab 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  */.  BtCursor *p
186ac 43 73 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Csr;         /* 
186ad 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20  Cursor pointing 
186ae 61 74 20 62 6c 6f 62 20 72 6f 77 20 2a 2f 0a 20  at blob row */. 
186af 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
186b0 53 74 6d 74 3b 20 20 20 20 2f 2a 20 53 74 61 74  Stmt;    /* Stat
186b1 65 6d 65 6e 74 20 68 6f 6c 64 69 6e 67 20 63 75  ement holding cu
186b2 72 73 6f 72 20 6f 70 65 6e 20 2a 2f 0a 20 20 73  rsor open */.  s
186b3 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
186b4 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 73         /* The as
186b5 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73  sociated databas
186b6 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4f  e */.};../*.** O
186b7 70 65 6e 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c  pen a blob handl
186b8 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
186b9 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f   int sqlite3_blo
186ba 62 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65  b_open(.  sqlite
186bb 33 2a 20 64 62 2c 20 20 20 20 20 20 20 20 20 20  3* db,          
186bc 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
186bd 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
186be 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
186bf 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  b,        /* The
186c0 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
186c1 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  se containing th
186c2 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73  e blob */.  cons
186c3 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c 20  t char *zTable, 
186c4 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
186c5 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
186c6 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  blob */.  const 
186c7 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 2c 20 20  char *zColumn,  
186c8 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20    /* The column 
186c9 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 62  containing the b
186ca 6c 6f 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f  lob */.  sqlite_
186cb 69 6e 74 36 34 20 69 52 6f 77 2c 20 20 20 20 20  int64 iRow,     
186cc 20 2f 2a 20 54 68 65 20 72 6f 77 20 63 6f 6e 74   /* The row cont
186cd 61 69 6e 69 6e 67 20 74 68 65 20 67 6c 6f 62 20  aining the glob 
186ce 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20  */.  int flags, 
186cf 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
186d0 54 72 75 65 20 2d 3e 20 72 65 61 64 2f 77 72 69  True -> read/wri
186d1 74 65 20 61 63 63 65 73 73 2c 20 66 61 6c 73 65  te access, false
186d2 20 2d 3e 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f   -> read-only */
186d3 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20  .  sqlite3_blob 
186d4 2a 2a 70 70 42 6c 6f 62 20 20 20 2f 2a 20 48 61  **ppBlob   /* Ha
186d5 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69  ndle for accessi
186d6 6e 67 20 74 68 65 20 62 6c 6f 62 20 72 65 74 75  ng the blob retu
186d7 72 6e 65 64 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  rned here */.){.
186d8 20 20 69 6e 74 20 6e 41 74 74 65 6d 70 74 20 3d    int nAttempt =
186d9 20 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20   0;.  int iCol; 
186da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
186db 20 49 6e 64 65 78 20 6f 66 20 7a 43 6f 6c 75 6d   Index of zColum
186dc 6e 20 69 6e 20 72 6f 77 2d 72 65 63 6f 72 64 20  n in row-record 
186dd 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 56 44  */..  /* This VD
186de 42 45 20 70 72 6f 67 72 61 6d 20 73 65 65 6b 73  BE program seeks
186df 20 61 20 62 74 72 65 65 20 63 75 72 73 6f 72 20   a btree cursor 
186e0 74 6f 20 74 68 65 20 69 64 65 6e 74 69 66 69 65  to the identifie
186e1 64 20 0a 20 20 2a 2a 20 64 62 2f 74 61 62 6c 65  d .  ** db/table
186e2 2f 72 6f 77 20 65 6e 74 72 79 2e 20 54 68 65 20  /row entry. The 
186e3 72 65 61 73 6f 6e 20 66 6f 72 20 75 73 69 6e 67  reason for using
186e4 20 61 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20   a vdbe program 
186e5 69 6e 73 74 65 61 64 0a 20 20 2a 2a 20 6f 66 20  instead.  ** of 
186e6 77 72 69 74 69 6e 67 20 63 6f 64 65 20 74 6f 20  writing code to 
186e7 75 73 65 20 74 68 65 20 62 2d 74 72 65 65 20 6c  use the b-tree l
186e8 61 79 65 72 20 64 69 72 65 63 74 6c 79 20 69 73  ayer directly is
186e9 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 76   that the.  ** v
186ea 64 62 65 20 70 72 6f 67 72 61 6d 20 77 69 6c 6c  dbe program will
186eb 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20   take advantage 
186ec 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 74  of the various t
186ed 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 20 20 2a 2a  ransaction,.  **
186ee 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 65 72 72   locking and err
186ef 6f 72 20 68 61 6e 64 6c 69 6e 67 20 69 6e 66 72  or handling infr
186f0 61 73 74 72 75 63 74 75 72 65 20 62 75 69 6c 74  astructure built
186f1 20 69 6e 74 6f 20 74 68 65 20 76 64 62 65 2e 0a   into the vdbe..
186f2 20 20 2a 2a 0a 20 20 2a 2a 20 41 66 74 65 72 20    **.  ** After 
186f3 73 65 65 6b 69 6e 67 20 74 68 65 20 63 75 72 73  seeking the curs
186f4 6f 72 2c 20 74 68 65 20 76 64 62 65 20 65 78 65  or, the vdbe exe
186f5 63 75 74 65 73 20 61 6e 20 4f 50 5f 52 65 73 75  cutes an OP_Resu
186f6 6c 74 52 6f 77 2e 0a 20 20 2a 2a 20 43 6f 64 65  ltRow..  ** Code
186f7 20 65 78 74 65 72 6e 61 6c 20 74 6f 20 74 68 65   external to the
186f8 20 56 64 62 65 20 74 68 65 6e 20 22 62 6f 72 72   Vdbe then "borr
186f9 6f 77 73 22 20 74 68 65 20 62 2d 74 72 65 65 20  ows" the b-tree 
186fa 63 75 72 73 6f 72 20 61 6e 64 0a 20 20 2a 2a 20  cursor and.  ** 
186fb 75 73 65 73 20 69 74 20 74 6f 20 69 6d 70 6c 65  uses it to imple
186fc 6d 65 6e 74 20 74 68 65 20 62 6c 6f 62 5f 72 65  ment the blob_re
186fd 61 64 28 29 2c 20 62 6c 6f 62 5f 77 72 69 74 65  ad(), blob_write
186fe 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 62 6c 6f  () and .  ** blo
186ff 62 5f 62 79 74 65 73 28 29 20 66 75 6e 63 74 69  b_bytes() functi
18700 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  ons..  **.  ** T
18701 68 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  he sqlite3_blob_
18702 63 6c 6f 73 65 28 29 20 66 75 6e 63 74 69 6f 6e  close() function
18703 20 66 69 6e 61 6c 69 7a 65 73 20 74 68 65 20 76   finalizes the v
18704 64 62 65 20 70 72 6f 67 72 61 6d 2c 0a 20 20 2a  dbe program,.  *
18705 2a 20 77 68 69 63 68 20 63 6c 6f 73 65 73 20 74  * which closes t
18706 68 65 20 62 2d 74 72 65 65 20 63 75 72 73 6f 72  he b-tree cursor
18707 20 61 6e 64 20 28 70 6f 73 73 69 62 6c 79 29 20   and (possibly) 
18708 63 6f 6d 6d 69 74 73 20 74 68 65 20 0a 20 20 2a  commits the .  *
18709 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20  * transaction.. 
1870a 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   */.  static con
1870b 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 6f 70  st VdbeOpList op
1870c 65 6e 42 6c 6f 62 5b 5d 20 3d 20 7b 0a 20 20 20  enBlob[] = {.   
1870d 20 7b 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e   {OP_Transaction
1870e 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20  , 0, 0, 0},     
1870f 2f 2a 20 30 3a 20 53 74 61 72 74 20 61 20 74 72  /* 0: Start a tr
18710 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 20  ansaction */.   
18711 20 7b 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69   {OP_VerifyCooki
18712 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20  e, 0, 0, 0},    
18713 2f 2a 20 31 3a 20 43 68 65 63 6b 20 74 68 65 20  /* 1: Check the 
18714 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 2a 2f  schema cookie */
18715 0a 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20  ..    /* One of 
18716 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77  the following tw
18717 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 69  o instructions i
18718 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 61 6e  s replaced by an
18719 0a 20 20 20 20 2a 2a 20 4f 50 5f 4e 6f 6f 70 20  .    ** OP_Noop 
1871a 62 65 66 6f 72 65 20 65 78 65 63 74 69 6f 6e 2e  before exection.
1871b 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f  .    */.    {OP_
1871c 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30  SetNumColumns, 0
1871d 2c 20 30 2c 20 30 7d 2c 20 20 20 2f 2a 20 32 3a  , 0, 0},   /* 2:
1871e 20 4e 75 6d 20 63 6f 6c 73 20 66 6f 72 20 63 75   Num cols for cu
1871f 72 73 6f 72 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f  rsor */.    {OP_
18720 4f 70 65 6e 52 65 61 64 2c 20 30 2c 20 30 2c 20  OpenRead, 0, 0, 
18721 30 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20 33 3a  0},        /* 3:
18722 20 4f 70 65 6e 20 63 75 72 73 6f 72 20 30 20 66   Open cursor 0 f
18723 6f 72 20 72 65 61 64 69 6e 67 20 2a 2f 0a 20 20  or reading */.  
18724 20 20 7b 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75    {OP_SetNumColu
18725 6d 6e 73 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20  mns, 0, 0, 0},  
18726 20 2f 2a 20 34 3a 20 4e 75 6d 20 63 6f 6c 73 20   /* 4: Num cols 
18727 66 6f 72 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20  for cursor */.  
18728 20 20 7b 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c    {OP_OpenWrite,
18729 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20   0, 0, 0},      
1872a 20 2f 2a 20 35 3a 20 4f 70 65 6e 20 63 75 72 73   /* 5: Open curs
1872b 6f 72 20 30 20 66 6f 72 20 72 65 61 64 2f 77 72  or 0 for read/wr
1872c 69 74 65 20 2a 2f 0a 0a 20 20 20 20 7b 4f 50 5f  ite */..    {OP_
1872d 56 61 72 69 61 62 6c 65 2c 20 31 2c 20 31 2c 20  Variable, 1, 1, 
1872e 30 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20 36 3a  0},        /* 6:
1872f 20 50 75 73 68 20 74 68 65 20 72 6f 77 69 64 20   Push the rowid 
18730 74 6f 20 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a  to the stack */.
18731 20 20 20 20 7b 4f 50 5f 4e 6f 74 45 78 69 73 74      {OP_NotExist
18732 73 2c 20 30 2c 20 31 30 2c 20 31 7d 2c 20 20 20  s, 0, 10, 1},   
18733 20 20 20 2f 2a 20 37 3a 20 53 65 65 6b 20 74 68     /* 7: Seek th
18734 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20  e cursor */.    
18735 7b 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30  {OP_Column, 0, 0
18736 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 20 20 2f  , 1},          /
18737 2a 20 38 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f  * 8  */.    {OP_
18738 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 30 2c  ResultRow, 1, 0,
18739 20 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20 39 20   0},       /* 9 
1873a 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 43 6c 6f 73   */.    {OP_Clos
1873b 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20  e, 0, 0, 0},    
1873c 20 20 20 20 20 20 20 2f 2a 20 31 30 20 20 2a 2f         /* 10  */
1873d 0a 20 20 20 20 7b 4f 50 5f 48 61 6c 74 2c 20 30  .    {OP_Halt, 0
1873e 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 20  , 0, 0},        
1873f 20 20 20 20 2f 2a 20 31 31 20 2a 2f 0a 20 20 7d      /* 11 */.  }
18740 3b 0a 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 30  ;..  Vdbe *v = 0
18741 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
18742 49 54 45 5f 4f 4b 3b 0a 20 20 63 68 61 72 20 7a  ITE_OK;.  char z
18743 45 72 72 5b 31 32 38 5d 3b 0a 0a 20 20 7a 45 72  Err[128];..  zEr
18744 72 5b 30 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69  r[0] = 0;.  sqli
18745 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
18746 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 6f  db->mutex);.  do
18747 20 7b 0a 20 20 20 20 50 61 72 73 65 20 73 50 61   {.    Parse sPa
18748 72 73 65 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a  rse;.    Table *
18749 70 54 61 62 3b 0a 0a 20 20 20 20 6d 65 6d 73 65  pTab;..    memse
1874a 74 28 26 73 50 61 72 73 65 2c 20 30 2c 20 73 69  t(&sParse, 0, si
1874b 7a 65 6f 66 28 50 61 72 73 65 29 29 3b 0a 20 20  zeof(Parse));.  
1874c 20 20 73 50 61 72 73 65 2e 64 62 20 3d 20 64 62    sParse.db = db
1874d 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ;..    rc = sqli
1874e 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b  te3SafetyOn(db);
1874f 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
18750 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
18751 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
18752 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
18753 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
18754 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69  .    }..    sqli
18755 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c  te3BtreeEnterAll
18756 28 64 62 29 3b 0a 20 20 20 20 70 54 61 62 20 3d  (db);.    pTab =
18757 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61   sqlite3LocateTa
18758 62 6c 65 28 26 73 50 61 72 73 65 2c 20 30 2c 20  ble(&sParse, 0, 
18759 7a 54 61 62 6c 65 2c 20 7a 44 62 29 3b 0a 20 20  zTable, zDb);.  
1875a 20 20 69 66 28 20 70 54 61 62 20 26 26 20 49 73    if( pTab && Is
1875b 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b  Virtual(pTab) ){
1875c 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 30 3b  .      pTab = 0;
1875d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
1875e 72 6f 72 4d 73 67 28 26 73 50 61 72 73 65 2c 20  rorMsg(&sParse, 
1875f 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76 69 72  "cannot open vir
18760 74 75 61 6c 20 74 61 62 6c 65 3a 20 25 73 22 2c  tual table: %s",
18761 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a   zTable);.    }.
18762 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
18763 4d 49 54 5f 56 49 45 57 0a 20 20 20 20 69 66 28  MIT_VIEW.    if(
18764 20 70 54 61 62 20 26 26 20 70 54 61 62 2d 3e 70   pTab && pTab->p
18765 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20  Select ){.      
18766 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20  pTab = 0;.      
18767 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
18768 26 73 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74  &sParse, "cannot
18769 20 6f 70 65 6e 20 76 69 65 77 3a 20 25 73 22 2c   open view: %s",
1876a 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a   zTable);.    }.
1876b 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 21  #endif.    if( !
1876c 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 69 66  pTab ){.      if
1876d 28 20 73 50 61 72 73 65 2e 7a 45 72 72 4d 73 67  ( sParse.zErrMsg
1876e 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1876f 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
18770 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c  eof(zErr), zErr,
18771 20 22 25 73 22 2c 20 73 50 61 72 73 65 2e 7a 45   "%s", sParse.zE
18772 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a  rrMsg);.      }.
18773 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
18774 65 65 28 73 50 61 72 73 65 2e 7a 45 72 72 4d 73  ee(sParse.zErrMs
18775 67 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  g);.      rc = S
18776 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
18777 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33     (void)sqlite3
18778 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20  SafetyOff(db);. 
18779 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
1877a 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20  eLeaveAll(db);. 
1877b 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f       goto blob_o
1877c 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a  pen_out;.    }..
1877d 20 20 20 20 2f 2a 20 4e 6f 77 20 73 65 61 72 63      /* Now searc
1877e 68 20 70 54 61 62 20 66 6f 72 20 74 68 65 20 65  h pTab for the e
1877f 78 61 63 74 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a  xact column. */.
18780 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20      for(iCol=0; 
18781 69 43 6f 6c 20 3c 20 70 54 61 62 2d 3e 6e 43 6f  iCol < pTab->nCo
18782 6c 3b 20 69 43 6f 6c 2b 2b 29 20 7b 0a 20 20 20  l; iCol++) {.   
18783 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
18784 72 49 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f 6c  rICmp(pTab->aCol
18785 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 7a 43  [iCol].zName, zC
18786 6f 6c 75 6d 6e 29 3d 3d 30 20 29 7b 0a 20 20 20  olumn)==0 ){.   
18787 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
18788 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
18789 28 20 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e 6e 43  ( iCol==pTab->nC
1878a 6f 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ol ){.      sqli
1878b 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
1878c 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c  eof(zErr), zErr,
1878d 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 75 6d 6e   "no such column
1878e 3a 20 5c 22 25 73 5c 22 22 2c 20 7a 43 6f 6c 75  : \"%s\"", zColu
1878f 6d 6e 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  mn);.      rc = 
18790 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
18791 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
18792 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
18793 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
18794 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a  eeLeaveAll(db);.
18795 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f        goto blob_
18796 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  open_out;.    }.
18797 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 76  .    /* If the v
18798 61 6c 75 65 20 69 73 20 62 65 69 6e 67 20 6f 70  alue is being op
18799 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67  ened for writing
1879a 2c 20 63 68 65 63 6b 20 74 68 61 74 20 74 68 65  , check that the
1879b 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69  .    ** column i
1879c 73 20 6e 6f 74 20 69 6e 64 65 78 65 64 2e 20 49  s not indexed. I
1879d 74 20 69 73 20 61 67 61 69 6e 73 74 20 74 68 65  t is against the
1879e 20 72 75 6c 65 73 20 74 6f 20 6f 70 65 6e 20 61   rules to open a
1879f 6e 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78 65 64  n.    ** indexed
187a0 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 72 69 74   column for writ
187a1 69 6e 67 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ing..    */.    
187a2 69 66 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20  if( flags ){.   
187a3 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a     Index *pIdx;.
187a4 20 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70        for(pIdx=p
187a5 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
187a6 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
187a7 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  ext){.        in
187a8 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  t j;.        for
187a9 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43  (j=0; j<pIdx->nC
187aa 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20  olumn; j++){.   
187ab 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d         if( pIdx-
187ac 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3d 3d 69 43  >aiColumn[j]==iC
187ad 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ol ){.          
187ae 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
187af 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c  tf(sizeof(zErr),
187b0 20 7a 45 72 72 2c 0a 20 20 20 20 20 20 20 20 20   zErr,.         
187b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
187b2 20 20 20 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e      "cannot open
187b3 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20   indexed column 
187b4 66 6f 72 20 77 72 69 74 69 6e 67 22 29 3b 0a 20  for writing");. 
187b5 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
187b6 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
187b7 20 20 20 20 20 20 20 20 20 20 28 76 6f 69 64 29            (void)
187b8 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
187b9 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20  (db);.          
187ba 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
187bb 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20  aveAll(db);.    
187bc 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f          goto blo
187bd 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20  b_open_out;.    
187be 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
187bf 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
187c0 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33  .    v = sqlite3
187c1 56 64 62 65 43 72 65 61 74 65 28 64 62 29 3b 0a  VdbeCreate(db);.
187c2 20 20 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20      if( v ){.   
187c3 20 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c     int iDb = sql
187c4 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
187c5 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  x(db, pTab->pSch
187c6 65 6d 61 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ema);.      sqli
187c7 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
187c8 28 76 2c 20 73 69 7a 65 6f 66 28 6f 70 65 6e 42  (v, sizeof(openB
187c9 6c 6f 62 29 2f 73 69 7a 65 6f 66 28 56 64 62 65  lob)/sizeof(Vdbe
187ca 4f 70 4c 69 73 74 29 2c 20 6f 70 65 6e 42 6c 6f  OpList), openBlo
187cb 62 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f  b);..      /* Co
187cc 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50 5f 54  nfigure the OP_T
187cd 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20  ransaction */.  
187ce 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
187cf 68 61 6e 67 65 50 31 28 76 2c 20 30 2c 20 69 44  hangeP1(v, 0, iD
187d0 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
187d1 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c  3VdbeChangeP2(v,
187d2 20 30 2c 20 28 66 6c 61 67 73 20 3f 20 31 20 3a   0, (flags ? 1 :
187d3 20 30 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20   0));..      /* 
187d4 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50  Configure the OP
187d5 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 2a 2f  _VerifyCookie */
187d6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
187d7 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 31 2c  beChangeP1(v, 1,
187d8 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c   iDb);.      sql
187d9 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32  ite3VdbeChangeP2
187da 28 76 2c 20 31 2c 20 70 54 61 62 2d 3e 70 53 63  (v, 1, pTab->pSc
187db 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f  hema->schema_coo
187dc 6b 69 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  kie);..      /* 
187dd 4d 61 6b 65 20 73 75 72 65 20 61 20 6d 75 74 65  Make sure a mute
187de 78 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65  x is held on the
187df 20 74 61 62 6c 65 20 74 6f 20 62 65 20 61 63 63   table to be acc
187e0 65 73 73 65 64 20 2a 2f 0a 20 20 20 20 20 20 73  essed */.      s
187e1 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74  qlite3VdbeUsesBt
187e2 72 65 65 28 76 2c 20 69 44 62 29 3b 20 0a 0a 20  ree(v, iDb); .. 
187e3 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 65       /* Remove e
187e4 69 74 68 65 72 20 74 68 65 20 4f 50 5f 4f 70 65  ither the OP_Ope
187e5 6e 57 72 69 74 65 20 6f 72 20 4f 70 65 6e 52 65  nWrite or OpenRe
187e6 61 64 2e 20 53 65 74 20 74 68 65 20 50 32 20 0a  ad. Set the P2 .
187e7 20 20 20 20 20 20 2a 2a 20 70 61 72 61 6d 65 74        ** paramet
187e8 65 72 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20  er of the other 
187e9 74 6f 20 70 54 61 62 2d 3e 74 6e 75 6d 2e 20 0a  to pTab->tnum. .
187ea 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
187eb 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
187ec 54 6f 4e 6f 6f 70 28 76 2c 20 28 66 6c 61 67 73  ToNoop(v, (flags
187ed 20 3f 20 33 20 3a 20 35 29 2c 20 31 29 3b 0a 20   ? 3 : 5), 1);. 
187ee 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
187ef 43 68 61 6e 67 65 50 32 28 76 2c 20 28 66 6c 61  ChangeP2(v, (fla
187f0 67 73 20 3f 20 35 20 3a 20 33 29 2c 20 70 54 61  gs ? 5 : 3), pTa
187f1 62 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20  b->tnum);.      
187f2 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
187f3 65 50 33 28 76 2c 20 28 66 6c 61 67 73 20 3f 20  eP3(v, (flags ? 
187f4 35 20 3a 20 33 29 2c 20 69 44 62 29 3b 0a 0a 20  5 : 3), iDb);.. 
187f5 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72       /* Configur
187f6 65 20 74 68 65 20 4f 50 5f 53 65 74 4e 75 6d 43  e the OP_SetNumC
187f7 6f 6c 75 6d 6e 73 2e 20 43 6f 6e 66 69 67 75 72  olumns. Configur
187f8 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 0a  e the cursor to.
187f9 20 20 20 20 20 20 2a 2a 20 74 68 69 6e 6b 20 74        ** think t
187fa 68 61 74 20 74 68 65 20 74 61 62 6c 65 20 68 61  hat the table ha
187fb 73 20 6f 6e 65 20 6d 6f 72 65 20 63 6f 6c 75 6d  s one more colum
187fc 6e 20 74 68 61 6e 20 69 74 20 72 65 61 6c 6c 79  n than it really
187fd 0a 20 20 20 20 20 20 2a 2a 20 64 6f 65 73 2e 20  .      ** does. 
187fe 41 6e 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20  An OP_Column to 
187ff 72 65 74 72 69 65 76 65 20 74 68 69 73 20 69 6d  retrieve this im
18800 61 67 69 6e 61 72 79 20 63 6f 6c 75 6d 6e 20 77  aginary column w
18801 69 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 61 6c 77  ill.      ** alw
18802 61 79 73 20 72 65 74 75 72 6e 20 61 6e 20 53 51  ays return an SQ
18803 4c 20 4e 55 4c 4c 2e 20 54 68 69 73 20 69 73 20  L NULL. This is 
18804 75 73 65 66 75 6c 20 62 65 63 61 75 73 65 20 69  useful because i
18805 74 20 6d 65 61 6e 73 0a 20 20 20 20 20 20 2a 2a  t means.      **
18806 20 77 65 20 63 61 6e 20 69 6e 76 6f 6b 65 20 4f   we can invoke O
18807 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 66 69 6c 6c  P_Column to fill
18808 20 69 6e 20 74 68 65 20 76 64 62 65 20 63 75 72   in the vdbe cur
18809 73 6f 72 73 20 74 79 70 65 20 0a 20 20 20 20 20  sors type .     
1880a 20 2a 2a 20 61 6e 64 20 6f 66 66 73 65 74 20 63   ** and offset c
1880b 61 63 68 65 20 77 69 74 68 6f 75 74 20 63 61 75  ache without cau
1880c 73 69 6e 67 20 61 6e 79 20 49 4f 2e 0a 20 20 20  sing any IO..   
1880d 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69     */.      sqli
1880e 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28  te3VdbeChangeP2(
1880f 76 2c 20 66 6c 61 67 73 20 3f 20 34 20 3a 20 32  v, flags ? 4 : 2
18810 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 3b  , pTab->nCol+1);
18811 0a 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e  .      if( !db->
18812 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
18813 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
18814 64 62 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20  dbeMakeReady(v, 
18815 31 2c 20 31 2c 20 31 2c 20 30 29 3b 0a 20 20 20  1, 1, 1, 0);.   
18816 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 0a 20     }.    }.   . 
18817 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
18818 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20  eaveAll(db);.   
18819 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 61 66   rc = sqlite3Saf
1881a 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20  etyOff(db);.    
1881b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1881c 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  K || db->mallocF
1881d 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 67  ailed ){.      g
1881e 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75  oto blob_open_ou
1881f 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71  t;.    }..    sq
18820 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
18821 28 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  ((sqlite3_stmt *
18822 29 76 2c 20 31 2c 20 69 52 6f 77 29 3b 0a 20 20  )v, 1, iRow);.  
18823 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73    rc = sqlite3_s
18824 74 65 70 28 28 73 71 6c 69 74 65 33 5f 73 74 6d  tep((sqlite3_stm
18825 74 20 2a 29 76 29 3b 0a 20 20 20 20 69 66 28 20  t *)v);.    if( 
18826 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  rc!=SQLITE_ROW )
18827 7b 0a 20 20 20 20 20 20 6e 41 74 74 65 6d 70 74  {.      nAttempt
18828 2b 2b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  ++;.      rc = s
18829 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
1882a 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29  (sqlite3_stmt *)
1882b 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  v);.      sqlite
1882c 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
1882d 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 73  f(zErr), zErr, s
1882e 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64 62  qlite3_errmsg(db
1882f 29 29 3b 0a 20 20 20 20 20 20 76 20 3d 20 30 3b  ));.      v = 0;
18830 0a 20 20 20 20 7d 0a 20 20 7d 20 77 68 69 6c 65  .    }.  } while
18831 28 20 6e 41 74 74 65 6d 70 74 3c 35 20 26 26 20  ( nAttempt<5 && 
18832 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d  rc==SQLITE_SCHEM
18833 41 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d  A );..  if( rc==
18834 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
18835 20 20 2f 2a 20 54 68 65 20 72 6f 77 2d 72 65 63    /* The row-rec
18836 6f 72 64 20 68 61 73 20 62 65 65 6e 20 6f 70 65  ord has been ope
18837 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79  ned successfully
18838 2e 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65  . Check that the
18839 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69  .    ** column i
1883a 6e 20 71 75 65 73 74 69 6f 6e 20 63 6f 6e 74 61  n question conta
1883b 69 6e 73 20 74 65 78 74 20 6f 72 20 61 20 62 6c  ins text or a bl
1883c 6f 62 2e 20 49 66 20 69 74 20 63 6f 6e 74 61 69  ob. If it contai
1883d 6e 73 0a 20 20 20 20 2a 2a 20 74 65 78 74 2c 20  ns.    ** text, 
1883e 69 74 20 69 73 20 75 70 20 74 6f 20 74 68 65 20  it is up to the 
1883f 63 61 6c 6c 65 72 20 74 6f 20 67 65 74 20 74 68  caller to get th
18840 65 20 65 6e 63 6f 64 69 6e 67 20 72 69 67 68 74  e encoding right
18841 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 49 6e 63  ..    */.    Inc
18842 72 62 6c 6f 62 20 2a 70 42 6c 6f 62 3b 0a 20 20  rblob *pBlob;.  
18843 20 20 75 33 32 20 74 79 70 65 20 3d 20 76 2d 3e    u32 type = v->
18844 61 70 43 73 72 5b 30 5d 2d 3e 61 54 79 70 65 5b  apCsr[0]->aType[
18845 69 43 6f 6c 5d 3b 0a 0a 20 20 20 20 69 66 28 20  iCol];..    if( 
18846 74 79 70 65 3c 31 32 20 29 7b 0a 20 20 20 20 20  type<12 ){.     
18847 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
18848 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20  f(sizeof(zErr), 
18849 7a 45 72 72 2c 20 22 63 61 6e 6e 6f 74 20 6f 70  zErr, "cannot op
1884a 65 6e 20 76 61 6c 75 65 20 6f 66 20 74 79 70 65  en value of type
1884b 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20   %s",.          
1884c 74 79 70 65 3d 3d 30 3f 22 6e 75 6c 6c 22 3a 20  type==0?"null": 
1884d 74 79 70 65 3d 3d 37 3f 22 72 65 61 6c 22 3a 20  type==7?"real": 
1884e 22 69 6e 74 65 67 65 72 22 0a 20 20 20 20 20 20  "integer".      
1884f 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  );.      rc = SQ
18850 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
18851 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e    goto blob_open
18852 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _out;.    }.    
18853 70 42 6c 6f 62 20 3d 20 28 49 6e 63 72 62 6c 6f  pBlob = (Incrblo
18854 62 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c  b *)sqlite3DbMal
18855 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
18856 6f 66 28 49 6e 63 72 62 6c 6f 62 29 29 3b 0a 20  of(Incrblob));. 
18857 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f     if( db->mallo
18858 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20  cFailed ){.     
18859 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42   sqlite3_free(pB
1885a 6c 6f 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f  lob);.      goto
1885b 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a   blob_open_out;.
1885c 20 20 20 20 7d 0a 20 20 20 20 70 42 6c 6f 62 2d      }.    pBlob-
1885d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a  >flags = flags;.
1885e 20 20 20 20 70 42 6c 6f 62 2d 3e 70 43 73 72 20      pBlob->pCsr 
1885f 3d 20 20 76 2d 3e 61 70 43 73 72 5b 30 5d 2d 3e  =  v->apCsr[0]->
18860 70 43 75 72 73 6f 72 3b 0a 20 20 20 20 73 71 6c  pCursor;.    sql
18861 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 43 75  ite3BtreeEnterCu
18862 72 73 6f 72 28 70 42 6c 6f 62 2d 3e 70 43 73 72  rsor(pBlob->pCsr
18863 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  );.    sqlite3Bt
18864 72 65 65 43 61 63 68 65 4f 76 65 72 66 6c 6f 77  reeCacheOverflow
18865 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a 20  (pBlob->pCsr);. 
18866 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
18867 65 61 76 65 43 75 72 73 6f 72 28 70 42 6c 6f 62  eaveCursor(pBlob
18868 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 70 42 6c  ->pCsr);.    pBl
18869 6f 62 2d 3e 70 53 74 6d 74 20 3d 20 28 73 71 6c  ob->pStmt = (sql
1886a 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 3b 0a 20  ite3_stmt *)v;. 
1886b 20 20 20 70 42 6c 6f 62 2d 3e 69 4f 66 66 73 65     pBlob->iOffse
1886c 74 20 3d 20 76 2d 3e 61 70 43 73 72 5b 30 5d 2d  t = v->apCsr[0]-
1886d 3e 61 4f 66 66 73 65 74 5b 69 43 6f 6c 5d 3b 0a  >aOffset[iCol];.
1886e 20 20 20 20 70 42 6c 6f 62 2d 3e 6e 42 79 74 65      pBlob->nByte
1886f 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65   = sqlite3VdbeSe
18870 72 69 61 6c 54 79 70 65 4c 65 6e 28 74 79 70 65  rialTypeLen(type
18871 29 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 64 62  );.    pBlob->db
18872 20 3d 20 64 62 3b 0a 20 20 20 20 2a 70 70 42 6c   = db;.    *ppBl
18873 6f 62 20 3d 20 28 73 71 6c 69 74 65 33 5f 62 6c  ob = (sqlite3_bl
18874 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 20 20  ob *)pBlob;.    
18875 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
18876 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d    }else if( rc==
18877 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
18878 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
18879 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20  f(sizeof(zErr), 
1887a 7a 45 72 72 2c 20 22 6e 6f 20 73 75 63 68 20 72  zErr, "no such r
1887b 6f 77 69 64 3a 20 25 6c 6c 64 22 2c 20 69 52 6f  owid: %lld", iRo
1887c 77 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  w);.    rc = SQL
1887d 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a  ITE_ERROR;.  }..
1887e 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3a 0a 20  blob_open_out:. 
1887f 20 7a 45 72 72 5b 73 69 7a 65 6f 66 28 7a 45 72   zErr[sizeof(zEr
18880 72 29 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20  r)-1] = '\0';.  
18881 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
18882 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  K || db->mallocF
18883 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c  ailed ){.    sql
18884 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 28 73  ite3_finalize((s
18885 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 29  qlite3_stmt *)v)
18886 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45  ;.  }.  sqlite3E
18887 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 28 72 63  rror(db, rc, (rc
18888 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3f 30 3a 7a 45  ==SQLITE_OK?0:zE
18889 72 72 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  rr));.  rc = sql
1888a 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20  ite3ApiExit(db, 
1888b 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  rc);.  sqlite3_m
1888c 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
1888d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
1888e 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f  rc;.}../*.** Clo
1888f 73 65 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65  se a blob handle
18890 20 74 68 61 74 20 77 61 73 20 70 72 65 76 69 6f   that was previo
18891 75 73 6c 79 20 63 72 65 61 74 65 64 20 75 73 69  usly created usi
18892 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 6c  ng.** sqlite3_bl
18893 6f 62 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a 53 51  ob_open()..*/.SQ
18894 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
18895 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28  ite3_blob_close(
18896 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42  sqlite3_blob *pB
18897 6c 6f 62 29 7b 0a 20 20 49 6e 63 72 62 6c 6f 62  lob){.  Incrblob
18898 20 2a 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20   *p = (Incrblob 
18899 2a 29 70 42 6c 6f 62 3b 0a 20 20 69 6e 74 20 72  *)pBlob;.  int r
1889a 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  c;..  rc = sqlit
1889b 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 2d 3e 70  e3_finalize(p->p
1889c 53 74 6d 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  Stmt);.  sqlite3
1889d 5f 66 72 65 65 28 70 29 3b 0a 20 20 72 65 74 75  _free(p);.  retu
1889e 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1889f 50 65 72 66 6f 72 6d 20 61 20 72 65 61 64 20 6f  Perform a read o
188a0 72 20 77 72 69 74 65 20 6f 70 65 72 61 74 69 6f  r write operatio
188a1 6e 20 6f 6e 20 61 20 62 6c 6f 62 0a 2a 2f 0a 73  n on a blob.*/.s
188a2 74 61 74 69 63 20 69 6e 74 20 62 6c 6f 62 52 65  tatic int blobRe
188a3 61 64 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74  adWrite(.  sqlit
188a4 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20  e3_blob *pBlob, 
188a5 0a 20 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69  .  void *z, .  i
188a6 6e 74 20 6e 2c 20 0a 20 20 69 6e 74 20 69 4f 66  nt n, .  int iOf
188a7 66 73 65 74 2c 20 0a 20 20 69 6e 74 20 28 2a 78  fset, .  int (*x
188a8 43 61 6c 6c 29 28 42 74 43 75 72 73 6f 72 2a 2c  Call)(BtCursor*,
188a9 20 75 33 32 2c 20 75 33 32 2c 20 76 6f 69 64 2a   u32, u32, void*
188aa 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ).){.  int rc;. 
188ab 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28   Incrblob *p = (
188ac 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62  Incrblob *)pBlob
188ad 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73  ;.  Vdbe *v;.  s
188ae 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e  qlite3 *db = p->
188af 64 62 3b 20 20 0a 0a 20 20 2f 2a 20 52 65 71 75  db;  ..  /* Requ
188b0 65 73 74 20 69 73 20 6f 75 74 20 6f 66 20 72 61  est is out of ra
188b1 6e 67 65 2e 20 52 65 74 75 72 6e 20 61 20 74 72  nge. Return a tr
188b2 61 6e 73 69 65 6e 74 20 65 72 72 6f 72 2e 20 2a  ansient error. *
188b3 2f 0a 20 20 69 66 28 20 28 69 4f 66 66 73 65 74  /.  if( (iOffset
188b4 2b 6e 29 3e 70 2d 3e 6e 42 79 74 65 20 29 7b 0a  +n)>p->nByte ){.
188b5 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
188b6 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 73  E_ERROR;.  }.  s
188b7 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
188b8 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 0a  er(db->mutex);..
188b9 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
188ba 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 68 61   no statement ha
188bb 6e 64 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 62  ndle, then the b
188bc 6c 6f 62 2d 68 61 6e 64 6c 65 20 68 61 73 0a 20  lob-handle has. 
188bd 20 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e   ** already been
188be 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 20 52 65   invalidated. Re
188bf 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52  turn SQLITE_ABOR
188c0 54 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a  T in this case..
188c1 20 20 2a 2f 0a 20 20 76 20 3d 20 28 56 64 62 65    */.  v = (Vdbe
188c2 2a 29 70 2d 3e 70 53 74 6d 74 3b 0a 20 20 69 66  *)p->pStmt;.  if
188c3 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63  ( v==0 ){.    rc
188c4 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b   = SQLITE_ABORT;
188c5 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
188c6 20 43 61 6c 6c 20 65 69 74 68 65 72 20 42 74 72   Call either Btr
188c7 65 65 44 61 74 61 28 29 20 6f 72 20 42 74 72 65  eeData() or Btre
188c8 65 50 75 74 44 61 74 61 28 29 2e 20 49 66 20 53  ePutData(). If S
188c9 51 4c 49 54 45 5f 41 42 4f 52 54 20 69 73 0a 20  QLITE_ABORT is. 
188ca 20 20 20 2a 2a 20 72 65 74 75 72 6e 65 64 2c 20     ** returned, 
188cb 63 6c 65 61 6e 2d 75 70 20 74 68 65 20 73 74 61  clean-up the sta
188cc 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2e 0a 20  tement handle.. 
188cd 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
188ce 28 20 64 62 20 3d 3d 20 76 2d 3e 64 62 20 29 3b  ( db == v->db );
188cf 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
188d0 65 45 6e 74 65 72 43 75 72 73 6f 72 28 70 2d 3e  eEnterCursor(p->
188d1 70 43 73 72 29 3b 0a 20 20 20 20 72 63 20 3d 20  pCsr);.    rc = 
188d2 78 43 61 6c 6c 28 70 2d 3e 70 43 73 72 2c 20 69  xCall(p->pCsr, i
188d3 4f 66 66 73 65 74 2b 70 2d 3e 69 4f 66 66 73 65  Offset+p->iOffse
188d4 74 2c 20 6e 2c 20 7a 29 3b 0a 20 20 20 20 73 71  t, n, z);.    sq
188d5 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 43  lite3BtreeLeaveC
188d6 75 72 73 6f 72 28 70 2d 3e 70 43 73 72 29 3b 0a  ursor(p->pCsr);.
188d7 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
188d8 54 45 5f 41 42 4f 52 54 20 29 7b 0a 20 20 20 20  TE_ABORT ){.    
188d9 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e    sqlite3VdbeFin
188da 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20 20 20 20  alize(v);.      
188db 70 2d 3e 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20  p->pStmt = 0;.  
188dc 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64    }else{.      d
188dd 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b  b->errCode = rc;
188de 0a 20 20 20 20 20 20 76 2d 3e 72 63 20 3d 20 72  .      v->rc = r
188df 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  c;.    }.  }.  r
188e0 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
188e1 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71  it(db, rc);.  sq
188e2 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
188e3 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
188e4 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
188e5 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 72  .** Read data fr
188e6 6f 6d 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65  om a blob handle
188e7 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
188e8 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  int sqlite3_blob
188e9 5f 72 65 61 64 28 73 71 6c 69 74 65 33 5f 62 6c  _read(sqlite3_bl
188ea 6f 62 20 2a 70 42 6c 6f 62 2c 20 76 6f 69 64 20  ob *pBlob, void 
188eb 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69  *z, int n, int i
188ec 4f 66 66 73 65 74 29 7b 0a 20 20 72 65 74 75 72  Offset){.  retur
188ed 6e 20 62 6c 6f 62 52 65 61 64 57 72 69 74 65 28  n blobReadWrite(
188ee 70 42 6c 6f 62 2c 20 7a 2c 20 6e 2c 20 69 4f 66  pBlob, z, n, iOf
188ef 66 73 65 74 2c 20 73 71 6c 69 74 65 33 42 74 72  fset, sqlite3Btr
188f0 65 65 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  eeData);.}../*.*
188f1 2a 20 57 72 69 74 65 20 64 61 74 61 20 74 6f 20  * Write data to 
188f2 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a  a blob handle..*
188f3 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
188f4 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72   sqlite3_blob_wr
188f5 69 74 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ite(sqlite3_blob
188f6 20 2a 70 42 6c 6f 62 2c 20 63 6f 6e 73 74 20 76   *pBlob, const v
188f7 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69  oid *z, int n, i
188f8 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a 20 20 72  nt iOffset){.  r
188f9 65 74 75 72 6e 20 62 6c 6f 62 52 65 61 64 57 72  eturn blobReadWr
188fa 69 74 65 28 70 42 6c 6f 62 2c 20 28 76 6f 69 64  ite(pBlob, (void
188fb 20 2a 29 7a 2c 20 6e 2c 20 69 4f 66 66 73 65 74   *)z, n, iOffset
188fc 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75  , sqlite3BtreePu
188fd 74 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  tData);.}../*.**
188fe 20 51 75 65 72 79 20 61 20 62 6c 6f 62 20 68 61   Query a blob ha
188ff 6e 64 6c 65 20 66 6f 72 20 74 68 65 20 73 69 7a  ndle for the siz
18900 65 20 6f 66 20 74 68 65 20 64 61 74 61 2e 0a 2a  e of the data..*
18901 2a 0a 2a 2a 20 54 68 65 20 49 6e 63 72 62 6c 6f  *.** The Incrblo
18902 62 2e 6e 42 79 74 65 20 66 69 65 6c 64 20 69 73  b.nByte field is
18903 20 66 69 78 65 64 20 66 6f 72 20 74 68 65 20 6c   fixed for the l
18904 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65 20 49  ifetime of the I
18905 6e 63 72 62 6c 6f 62 0a 2a 2a 20 73 6f 20 6e 6f  ncrblob.** so no
18906 20 6d 75 74 65 78 20 69 73 20 72 65 71 75 69 72   mutex is requir
18907 65 64 20 66 6f 72 20 61 63 63 65 73 73 2e 0a 2a  ed for access..*
18908 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
18909 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79   sqlite3_blob_by
1890a 74 65 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62  tes(sqlite3_blob
1890b 20 2a 70 42 6c 6f 62 29 7b 0a 20 20 49 6e 63 72   *pBlob){.  Incr
1890c 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62  blob *p = (Incrb
1890d 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 72  lob *)pBlob;.  r
1890e 65 74 75 72 6e 20 70 2d 3e 6e 42 79 74 65 3b 0a  eturn p->nByte;.
1890f 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66  }..#endif /* #if
18910 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
18911 5f 49 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a  _INCRBLOB */../*
18912 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
18913 64 20 6f 66 20 76 64 62 65 62 6c 6f 62 2e 63 20  d of vdbeblob.c 
18914 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18915 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18916 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
18917 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
18918 67 69 6e 20 66 69 6c 65 20 6a 6f 75 72 6e 61 6c  gin file journal
18919 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1891a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1891b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1891c 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20  .** 2007 August 
1891d 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  22.**.** The aut
1891e 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
1891f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
18920 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
18921 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
18922 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
18923 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
18924 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
18925 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
18926 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
18927 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
18928 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
18929 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
1892a 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
1892b 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
1892c 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
1892d 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
1892e 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
1892f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18931 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18932 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18933 2a 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64  *.**.** @(#) $Id
18934 3a 20 6a 6f 75 72 6e 61 6c 2e 63 2c 76 20 31 2e  : journal.c,v 1.
18935 38 20 32 30 30 38 2f 30 35 2f 30 31 20 31 38 3a  8 2008/05/01 18:
18936 30 31 3a 34 37 20 64 72 68 20 45 78 70 20 24 0a  01:47 drh Exp $.
18937 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
18938 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f  E_ENABLE_ATOMIC_
18939 57 52 49 54 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  WRITE../*.** Thi
1893a 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  s file implement
1893b 73 20 61 20 73 70 65 63 69 61 6c 20 6b 69 6e 64  s a special kind
1893c 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65   of sqlite3_file
1893d 20 6f 62 6a 65 63 74 20 75 73 65 64 0a 2a 2a 20   object used.** 
1893e 62 79 20 53 51 4c 69 74 65 20 74 6f 20 63 72 65  by SQLite to cre
1893f 61 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ate journal file
18940 73 20 69 66 20 74 68 65 20 61 74 6f 6d 69 63 2d  s if the atomic-
18941 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69  write optimizati
18942 6f 6e 0a 2a 2a 20 69 73 20 65 6e 61 62 6c 65 64  on.** is enabled
18943 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 73 74  ..**.** The dist
18944 69 6e 63 74 69 76 65 20 63 68 61 72 61 63 74 65  inctive characte
18945 72 69 73 74 69 63 20 6f 66 20 74 68 69 73 20 73  ristic of this s
18946 71 6c 69 74 65 33 5f 66 69 6c 65 20 69 73 20 74  qlite3_file is t
18947 68 61 74 20 74 68 65 0a 2a 2a 20 61 63 74 75 61  hat the.** actua
18948 6c 20 6f 6e 20 64 69 73 6b 20 66 69 6c 65 20 69  l on disk file i
18949 73 20 63 72 65 61 74 65 64 20 6c 61 7a 69 6c 79  s created lazily
1894a 2e 20 57 68 65 6e 20 74 68 65 20 66 69 6c 65 20  . When the file 
1894b 69 73 20 63 72 65 61 74 65 64 2c 0a 2a 2a 20 74  is created,.** t
1894c 68 65 20 63 61 6c 6c 65 72 20 73 70 65 63 69 66  he caller specif
1894d 69 65 73 20 61 20 62 75 66 66 65 72 20 73 69 7a  ies a buffer siz
1894e 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f  e for an in-memo
1894f 72 79 20 62 75 66 66 65 72 20 74 6f 0a 2a 2a 20  ry buffer to.** 
18950 62 65 20 75 73 65 64 20 74 6f 20 73 65 72 76 69  be used to servi
18951 63 65 20 72 65 61 64 28 29 20 61 6e 64 20 77 72  ce read() and wr
18952 69 74 65 28 29 20 72 65 71 75 65 73 74 73 2e 20  ite() requests. 
18953 54 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 0a  The actual file.
18954 2a 2a 20 6f 6e 20 64 69 73 6b 20 69 73 20 6e 6f  ** on disk is no
18955 74 20 63 72 65 61 74 65 64 20 6f 72 20 70 6f 70  t created or pop
18956 75 6c 61 74 65 64 20 75 6e 74 69 6c 20 65 69 74  ulated until eit
18957 68 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20  her:.**.**   1) 
18958 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65  The in-memory re
18959 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 67 72 6f  presentation gro
1895a 77 73 20 74 6f 6f 20 6c 61 72 67 65 20 66 6f 72  ws too large for
1895b 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 0a   the allocated .
1895c 2a 2a 20 20 20 20 20 20 62 75 66 66 65 72 2c 20  **      buffer, 
1895d 6f 72 0a 2a 2a 20 20 20 32 29 20 54 68 65 20 78  or.**   2) The x
1895e 53 79 6e 63 28 29 20 6d 65 74 68 6f 64 20 69 73  Sync() method is
1895f 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 0a 0a 0a 2f   called..*/..../
18960 2a 0a 2a 2a 20 41 20 4a 6f 75 72 6e 61 6c 46 69  *.** A JournalFi
18961 6c 65 20 6f 62 6a 65 63 74 20 69 73 20 61 20 73  le object is a s
18962 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 74  ubclass of sqlit
18963 65 33 5f 66 69 6c 65 20 75 73 65 64 20 62 79 0a  e3_file used by.
18964 2a 2a 20 61 73 20 61 6e 20 6f 70 65 6e 20 66 69  ** as an open fi
18965 6c 65 20 68 61 6e 64 6c 65 20 66 6f 72 20 6a 6f  le handle for jo
18966 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f 0a  urnal files..*/.
18967 73 74 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46 69  struct JournalFi
18968 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69  le {.  sqlite3_i
18969 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68  o_methods *pMeth
1896a 6f 64 3b 20 20 20 20 2f 2a 20 49 2f 4f 20 6d 65  od;    /* I/O me
1896b 74 68 6f 64 73 20 6f 6e 20 6a 6f 75 72 6e 61 6c  thods on journal
1896c 20 66 69 6c 65 73 20 2a 2f 0a 20 20 69 6e 74 20   files */.  int 
1896d 6e 42 75 66 3b 20 20 20 20 20 20 20 20 20 20 20  nBuf;           
1896e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
1896f 69 7a 65 20 6f 66 20 7a 42 75 66 5b 5d 20 69 6e  ize of zBuf[] in
18970 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 72   bytes */.  char
18971 20 2a 7a 42 75 66 3b 20 20 20 20 20 20 20 20 20   *zBuf;         
18972 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
18973 70 61 63 65 20 74 6f 20 62 75 66 66 65 72 20 6a  pace to buffer j
18974 6f 75 72 6e 61 6c 20 77 72 69 74 65 73 20 2a 2f  ournal writes */
18975 0a 20 20 69 6e 74 20 69 53 69 7a 65 3b 20 20 20  .  int iSize;   
18976 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18977 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20     /* Amount of 
18978 7a 42 75 66 5b 5d 20 63 75 72 72 65 6e 74 6c 79  zBuf[] currently
18979 20 75 73 65 64 20 2a 2f 0a 20 20 69 6e 74 20 66   used */.  int f
1897a 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
1897b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f             /* xO
1897c 70 65 6e 20 66 6c 61 67 73 20 2a 2f 0a 20 20 73  pen flags */.  s
1897d 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
1897e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1897f 2a 20 54 68 65 20 22 72 65 61 6c 22 20 75 6e 64  * The "real" und
18980 65 72 6c 79 69 6e 67 20 56 46 53 20 2a 2f 0a 20  erlying VFS */. 
18981 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
18982 52 65 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20  Real;           
18983 20 2f 2a 20 54 68 65 20 22 72 65 61 6c 22 20 75   /* The "real" u
18984 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c 65 20 64  nderlying file d
18985 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 63  escriptor */.  c
18986 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4a 6f 75 72  onst char *zJour
18987 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f  nal;           /
18988 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f  * Name of the jo
18989 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 7d 3b  urnal file */.};
1898a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
1898b 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 4a 6f 75 72  JournalFile Jour
1898c 6e 61 6c 46 69 6c 65 3b 0a 0a 2f 2a 0a 2a 2a 20  nalFile;../*.** 
1898d 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 61  If it does not a
1898e 6c 72 65 61 64 79 20 65 78 69 73 74 73 2c 20 63  lready exists, c
1898f 72 65 61 74 65 20 61 6e 64 20 70 6f 70 75 6c 61  reate and popula
18990 74 65 20 74 68 65 20 6f 6e 2d 64 69 73 6b 20 66  te the on-disk f
18991 69 6c 65 20 0a 2a 2a 20 66 6f 72 20 4a 6f 75 72  ile .** for Jour
18992 6e 61 6c 46 69 6c 65 20 70 2e 0a 2a 2f 0a 73 74  nalFile p..*/.st
18993 61 74 69 63 20 69 6e 74 20 63 72 65 61 74 65 46  atic int createF
18994 69 6c 65 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20  ile(JournalFile 
18995 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  *p){.  int rc = 
18996 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
18997 20 21 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20   !p->pReal ){.  
18998 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
18999 70 52 65 61 6c 20 3d 20 28 73 71 6c 69 74 65 33  pReal = (sqlite3
1899a 5f 66 69 6c 65 20 2a 29 26 70 5b 31 5d 3b 0a 20  _file *)&p[1];. 
1899b 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
1899c 73 4f 70 65 6e 28 70 2d 3e 70 56 66 73 2c 20 70  sOpen(p->pVfs, p
1899d 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 52 65 61  ->zJournal, pRea
1899e 6c 2c 20 70 2d 3e 66 6c 61 67 73 2c 20 30 29 3b  l, p->flags, 0);
1899f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
189a0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
189a1 70 2d 3e 70 52 65 61 6c 20 3d 20 70 52 65 61 6c  p->pReal = pReal
189a2 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69  ;.      if( p->i
189a3 53 69 7a 65 3e 30 20 29 7b 0a 20 20 20 20 20 20  Size>0 ){.      
189a4 20 20 61 73 73 65 72 74 28 70 2d 3e 69 53 69 7a    assert(p->iSiz
189a5 65 3c 3d 70 2d 3e 6e 42 75 66 29 3b 0a 20 20 20  e<=p->nBuf);.   
189a6 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
189a7 33 4f 73 57 72 69 74 65 28 70 2d 3e 70 52 65 61  3OsWrite(p->pRea
189a8 6c 2c 20 70 2d 3e 7a 42 75 66 2c 20 70 2d 3e 69  l, p->zBuf, p->i
189a9 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Size, 0);.      
189aa 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
189ab 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
189ac 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 69 6c 65  * Close the file
189ad 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
189ae 6a 72 6e 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65  jrnlClose(sqlite
189af 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20  3_file *pJfd){. 
189b0 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20   JournalFile *p 
189b1 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a  = (JournalFile *
189b2 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e  )pJfd;.  if( p->
189b3 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c  pReal ){.    sql
189b4 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 2d 3e 70  ite3OsClose(p->p
189b5 52 65 61 6c 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  Real);.  }.  sql
189b6 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 42 75  ite3_free(p->zBu
189b7 66 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  f);.  return SQL
189b8 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
189b9 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20   Read data from 
189ba 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  the file..*/.sta
189bb 74 69 63 20 69 6e 74 20 6a 72 6e 6c 52 65 61 64  tic int jrnlRead
189bc 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  (.  sqlite3_file
189bd 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68   *pJfd,    /* Th
189be 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66  e journal file f
189bf 72 6f 6d 20 77 68 69 63 68 20 74 6f 20 72 65 61  rom which to rea
189c0 64 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 7a 42 75  d */.  void *zBu
189c1 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  f,            /*
189c2 20 50 75 74 20 74 68 65 20 72 65 73 75 6c 74 73   Put the results
189c3 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69   here */.  int i
189c4 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  Amt,            
189c5 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
189c6 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a  ytes to read */.
189c7 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
189c8 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69  Ofst     /* Begi
189c9 6e 20 72 65 61 64 69 6e 67 20 61 74 20 74 68 69  n reading at thi
189ca 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20  s offset */.){. 
189cb 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
189cc 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69  _OK;.  JournalFi
189cd 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c  le *p = (Journal
189ce 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69  File *)pJfd;.  i
189cf 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20  f( p->pReal ){. 
189d0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
189d1 73 52 65 61 64 28 70 2d 3e 70 52 65 61 6c 2c 20  sRead(p->pReal, 
189d2 7a 42 75 66 2c 20 69 41 6d 74 2c 20 69 4f 66 73  zBuf, iAmt, iOfs
189d3 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  t);.  }else{.   
189d4 20 61 73 73 65 72 74 28 20 69 41 6d 74 2b 69 4f   assert( iAmt+iO
189d5 66 73 74 3c 3d 70 2d 3e 69 53 69 7a 65 20 29 3b  fst<=p->iSize );
189d6 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 42 75 66  .    memcpy(zBuf
189d7 2c 20 26 70 2d 3e 7a 42 75 66 5b 69 4f 66 73 74  , &p->zBuf[iOfst
189d8 5d 2c 20 69 41 6d 74 29 3b 0a 20 20 7d 0a 20 20  ], iAmt);.  }.  
189d9 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
189da 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 74  .** Write data t
189db 6f 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73  o the file..*/.s
189dc 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 57 72  tatic int jrnlWr
189dd 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66  ite(.  sqlite3_f
189de 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a  ile *pJfd,    /*
189df 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   The journal fil
189e0 65 20 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20  e into which to 
189e1 77 72 69 74 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  write */.  const
189e2 20 76 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20 20   void *zBuf,    
189e3 20 20 2f 2a 20 54 61 6b 65 20 64 61 74 61 20 74    /* Take data t
189e4 6f 20 62 65 20 77 72 69 74 74 65 6e 20 66 72 6f  o be written fro
189e5 6d 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20  m here */.  int 
189e6 69 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20  iAmt,           
189e7 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
189e8 62 79 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a  bytes to write *
189e9 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  /.  sqlite_int64
189ea 20 69 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65   iOfst     /* Be
189eb 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20 74  gin writing at t
189ec 68 69 73 20 6f 66 66 73 65 74 20 69 6e 74 6f 20  his offset into 
189ed 74 68 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20  the file */.){. 
189ee 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
189ef 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69  _OK;.  JournalFi
189f0 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c  le *p = (Journal
189f1 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69  File *)pJfd;.  i
189f2 66 28 20 21 70 2d 3e 70 52 65 61 6c 20 26 26 20  f( !p->pReal && 
189f3 28 69 4f 66 73 74 2b 69 41 6d 74 29 3e 70 2d 3e  (iOfst+iAmt)>p->
189f4 6e 42 75 66 20 29 7b 0a 20 20 20 20 72 63 20 3d  nBuf ){.    rc =
189f5 20 63 72 65 61 74 65 46 69 6c 65 28 70 29 3b 0a   createFile(p);.
189f6 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
189f7 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
189f8 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20  f( p->pReal ){. 
189f9 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
189fa 33 4f 73 57 72 69 74 65 28 70 2d 3e 70 52 65 61  3OsWrite(p->pRea
189fb 6c 2c 20 7a 42 75 66 2c 20 69 41 6d 74 2c 20 69  l, zBuf, iAmt, i
189fc 4f 66 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Ofst);.    }else
189fd 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26  {.      memcpy(&
189fe 70 2d 3e 7a 42 75 66 5b 69 4f 66 73 74 5d 2c 20  p->zBuf[iOfst], 
189ff 7a 42 75 66 2c 20 69 41 6d 74 29 3b 0a 20 20 20  zBuf, iAmt);.   
18a00 20 20 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c     if( p->iSize<
18a01 28 69 4f 66 73 74 2b 69 41 6d 74 29 20 29 7b 0a  (iOfst+iAmt) ){.
18a02 20 20 20 20 20 20 20 20 70 2d 3e 69 53 69 7a 65          p->iSize
18a03 20 3d 20 28 69 4f 66 73 74 2b 69 41 6d 74 29 3b   = (iOfst+iAmt);
18a04 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
18a05 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
18a06 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74  }../*.** Truncat
18a07 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73  e the file..*/.s
18a08 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 54 72  tatic int jrnlTr
18a09 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66  uncate(sqlite3_f
18a0a 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74  ile *pJfd, sqlit
18a0b 65 5f 69 6e 74 36 34 20 73 69 7a 65 29 7b 0a 20  e_int64 size){. 
18a0c 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
18a0d 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69  _OK;.  JournalFi
18a0e 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c  le *p = (Journal
18a0f 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69  File *)pJfd;.  i
18a10 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20  f( p->pReal ){. 
18a11 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
18a12 73 54 72 75 6e 63 61 74 65 28 70 2d 3e 70 52 65  sTruncate(p->pRe
18a13 61 6c 2c 20 73 69 7a 65 29 3b 0a 20 20 7d 65 6c  al, size);.  }el
18a14 73 65 20 69 66 28 20 73 69 7a 65 3c 70 2d 3e 69  se if( size<p->i
18a15 53 69 7a 65 20 29 7b 0a 20 20 20 20 70 2d 3e 69  Size ){.    p->i
18a16 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 7d  Size = size;.  }
18a17 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
18a18 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20  ./*.** Sync the 
18a19 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
18a1a 69 6e 74 20 6a 72 6e 6c 53 79 6e 63 28 73 71 6c  int jrnlSync(sql
18a1b 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c  ite3_file *pJfd,
18a1c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 69   int flags){.  i
18a1d 6e 74 20 72 63 3b 0a 20 20 4a 6f 75 72 6e 61 6c  nt rc;.  Journal
18a1e 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e  File *p = (Journ
18a1f 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20  alFile *)pJfd;. 
18a20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b   if( p->pReal ){
18a21 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
18a22 33 4f 73 53 79 6e 63 28 70 2d 3e 70 52 65 61 6c  3OsSync(p->pReal
18a23 2c 20 66 6c 61 67 73 29 3b 0a 20 20 7d 65 6c 73  , flags);.  }els
18a24 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  e{.    rc = SQLI
18a25 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74  TE_OK;.  }.  ret
18a26 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
18a27 20 51 75 65 72 79 20 74 68 65 20 73 69 7a 65 20   Query the size 
18a28 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e 20 62  of the file in b
18a29 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ytes..*/.static 
18a2a 69 6e 74 20 6a 72 6e 6c 46 69 6c 65 53 69 7a 65  int jrnlFileSize
18a2b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70  (sqlite3_file *p
18a2c 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36  Jfd, sqlite_int6
18a2d 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74  4 *pSize){.  int
18a2e 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
18a2f 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a  .  JournalFile *
18a30 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65  p = (JournalFile
18a31 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70   *)pJfd;.  if( p
18a32 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72  ->pReal ){.    r
18a33 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c  c = sqlite3OsFil
18a34 65 53 69 7a 65 28 70 2d 3e 70 52 65 61 6c 2c 20  eSize(p->pReal, 
18a35 70 53 69 7a 65 29 3b 0a 20 20 7d 65 6c 73 65 7b  pSize);.  }else{
18a36 0a 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 28 73  .    *pSize = (s
18a37 71 6c 69 74 65 5f 69 6e 74 36 34 29 20 70 2d 3e  qlite_int64) p->
18a38 69 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74  iSize;.  }.  ret
18a39 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
18a3a 20 54 61 62 6c 65 20 6f 66 20 6d 65 74 68 6f 64   Table of method
18a3b 73 20 66 6f 72 20 4a 6f 75 72 6e 61 6c 46 69 6c  s for JournalFil
18a3c 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6f  e sqlite3_file o
18a3d 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  bject..*/.static
18a3e 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
18a3f 69 6f 5f 6d 65 74 68 6f 64 73 20 4a 6f 75 72 6e  io_methods Journ
18a40 61 6c 46 69 6c 65 4d 65 74 68 6f 64 73 20 3d 20  alFileMethods = 
18a41 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20  {.  1,          
18a42 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a     /* iVersion *
18a43 2f 0a 20 20 6a 72 6e 6c 43 6c 6f 73 65 2c 20 20  /.  jrnlClose,  
18a44 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 0a     /* xClose */.
18a45 20 20 6a 72 6e 6c 52 65 61 64 2c 20 20 20 20 20    jrnlRead,     
18a46 20 2f 2a 20 78 52 65 61 64 20 2a 2f 0a 20 20 6a   /* xRead */.  j
18a47 72 6e 6c 57 72 69 74 65 2c 20 20 20 20 20 2f 2a  rnlWrite,     /*
18a48 20 78 57 72 69 74 65 20 2a 2f 0a 20 20 6a 72 6e   xWrite */.  jrn
18a49 6c 54 72 75 6e 63 61 74 65 2c 20 20 2f 2a 20 78  lTruncate,  /* x
18a4a 54 72 75 6e 63 61 74 65 20 2a 2f 0a 20 20 6a 72  Truncate */.  jr
18a4b 6e 6c 53 79 6e 63 2c 20 20 20 20 20 20 2f 2a 20  nlSync,      /* 
18a4c 78 53 79 6e 63 20 2a 2f 0a 20 20 6a 72 6e 6c 46  xSync */.  jrnlF
18a4d 69 6c 65 53 69 7a 65 2c 20 20 2f 2a 20 78 46 69  ileSize,  /* xFi
18a4e 6c 65 53 69 7a 65 20 2a 2f 0a 20 20 30 2c 20 20  leSize */.  0,  
18a4f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c             /* xL
18a50 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20  ock */.  0,     
18a51 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f          /* xUnlo
18a52 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20  ck */.  0,      
18a53 20 20 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b         /* xCheck
18a54 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 2a 2f 0a  ReservedLock */.
18a55 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
18a56 20 2f 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c   /* xFileControl
18a57 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20   */.  0,        
18a58 20 20 20 20 20 2f 2a 20 78 53 65 63 74 6f 72 53       /* xSectorS
18a59 69 7a 65 20 2a 2f 0a 20 20 30 20 20 20 20 20 20  ize */.  0      
18a5a 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 76 69          /* xDevi
18a5b 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
18a5c 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 0a 2a 2a 20  s */.};../* .** 
18a5d 4f 70 65 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 66  Open a journal f
18a5e 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ile..*/.SQLITE_P
18a5f 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
18a60 65 33 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 0a 20  e3JournalOpen(. 
18a61 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
18a62 66 73 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  fs,         /* T
18a63 68 65 20 56 46 53 20 74 6f 20 75 73 65 20 66 6f  he VFS to use fo
18a64 72 20 61 63 74 75 61 6c 20 66 69 6c 65 20 49 2f  r actual file I/
18a65 4f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  O */.  const cha
18a66 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20  r *zName,       
18a67 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
18a68 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f   journal file */
18a69 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  .  sqlite3_file 
18a6a 2a 70 4a 66 64 2c 20 20 20 20 20 20 20 20 2f 2a  *pJfd,        /*
18a6b 20 50 72 65 61 6c 6c 6f 63 61 74 65 64 2c 20 62   Preallocated, b
18a6c 6c 61 6e 6b 20 66 69 6c 65 20 68 61 6e 64 6c 65  lank file handle
18a6d 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c   */.  int flags,
18a6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18a6f 20 2f 2a 20 4f 70 65 6e 69 6e 67 20 66 6c 61 67   /* Opening flag
18a70 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 75 66 20  s */.  int nBuf 
18a71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18a72 20 20 2f 2a 20 42 79 74 65 73 20 62 75 66 66 65    /* Bytes buffe
18a73 72 65 64 20 62 65 66 6f 72 65 20 6f 70 65 6e 69  red before openi
18a74 6e 67 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 29  ng the file */.)
18a75 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20  {.  JournalFile 
18a76 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c  *p = (JournalFil
18a77 65 20 2a 29 70 4a 66 64 3b 0a 20 20 6d 65 6d 73  e *)pJfd;.  mems
18a78 65 74 28 70 2c 20 30 2c 20 73 71 6c 69 74 65 33  et(p, 0, sqlite3
18a79 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73  JournalSize(pVfs
18a7a 29 29 3b 0a 20 20 69 66 28 20 6e 42 75 66 3e 30  ));.  if( nBuf>0
18a7b 20 29 7b 0a 20 20 20 20 70 2d 3e 7a 42 75 66 20   ){.    p->zBuf 
18a7c 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a  = sqlite3MallocZ
18a7d 65 72 6f 28 6e 42 75 66 29 3b 0a 20 20 20 20 69  ero(nBuf);.    i
18a7e 66 28 20 21 70 2d 3e 7a 42 75 66 20 29 7b 0a 20  f( !p->zBuf ){. 
18a7f 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
18a80 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
18a81 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
18a82 75 72 6e 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  urn sqlite3OsOpe
18a83 6e 28 70 56 66 73 2c 20 7a 4e 61 6d 65 2c 20 70  n(pVfs, zName, p
18a84 4a 66 64 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a  Jfd, flags, 0);.
18a85 20 20 7d 0a 20 20 70 2d 3e 70 4d 65 74 68 6f 64    }.  p->pMethod
18a86 20 3d 20 26 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d   = &JournalFileM
18a87 65 74 68 6f 64 73 3b 0a 20 20 70 2d 3e 6e 42 75  ethods;.  p->nBu
18a88 66 20 3d 20 6e 42 75 66 3b 0a 20 20 70 2d 3e 66  f = nBuf;.  p->f
18a89 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20  lags = flags;.  
18a8a 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 3d 20 7a 4e  p->zJournal = zN
18a8b 61 6d 65 3b 0a 20 20 70 2d 3e 70 56 66 73 20 3d  ame;.  p->pVfs =
18a8c 20 70 56 66 73 3b 0a 20 20 72 65 74 75 72 6e 20   pVfs;.  return 
18a8d 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
18a8e 0a 2a 2a 20 49 66 20 74 68 65 20 61 72 67 75 6d  .** If the argum
18a8f 65 6e 74 20 70 20 70 6f 69 6e 74 73 20 74 6f 20  ent p points to 
18a90 61 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 73 74  a JournalFile st
18a91 72 75 63 74 75 72 65 2c 20 61 6e 64 20 74 68 65  ructure, and the
18a92 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 66   underlying.** f
18a93 69 6c 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20  ile has not yet 
18a94 62 65 65 6e 20 63 72 65 61 74 65 64 2c 20 63 72  been created, cr
18a95 65 61 74 65 20 69 74 20 6e 6f 77 2e 0a 2a 2f 0a  eate it now..*/.
18a96 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
18a97 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61  nt sqlite3Journa
18a98 6c 43 72 65 61 74 65 28 73 71 6c 69 74 65 33 5f  lCreate(sqlite3_
18a99 66 69 6c 65 20 2a 70 29 7b 0a 20 20 69 66 28 20  file *p){.  if( 
18a9a 70 2d 3e 70 4d 65 74 68 6f 64 73 21 3d 26 4a 6f  p->pMethods!=&Jo
18a9b 75 72 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 73  urnalFileMethods
18a9c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
18a9d 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
18a9e 72 65 74 75 72 6e 20 63 72 65 61 74 65 46 69 6c  return createFil
18a9f 65 28 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a  e((JournalFile *
18aa0 29 70 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52  )p);.}../* .** R
18aa1 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
18aa2 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 69 72   of bytes requir
18aa3 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20 4a 6f  ed to store a Jo
18aa4 75 72 6e 61 6c 46 69 6c 65 20 74 68 61 74 20 75  urnalFile that u
18aa5 73 65 73 20 76 66 73 0a 2a 2a 20 70 56 66 73 20  ses vfs.** pVfs 
18aa6 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 75 6e  to create the un
18aa7 64 65 72 6c 79 69 6e 67 20 6f 6e 2d 64 69 73 6b  derlying on-disk
18aa8 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54   files..*/.SQLIT
18aa9 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
18aaa 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65  lite3JournalSize
18aab 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
18aac 66 73 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70  fs){.  return (p
18aad 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 2b 73 69  Vfs->szOsFile+si
18aae 7a 65 6f 66 28 4a 6f 75 72 6e 61 6c 46 69 6c 65  zeof(JournalFile
18aaf 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ));.}.#endif../*
18ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
18ab1 64 20 6f 66 20 6a 6f 75 72 6e 61 6c 2e 63 20 2a  d of journal.c *
18ab2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18ab3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18ab4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
18ab5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
18ab6 67 69 6e 20 66 69 6c 65 20 65 78 70 72 2e 63 20  gin file expr.c 
18ab7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18ab8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18ab9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
18aba 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
18abb 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
18abc 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
18abd 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
18abe 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
18abf 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
18ac0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
18ac1 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
18ac2 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
18ac3 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
18ac4 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
18ac5 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
18ac6 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
18ac7 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
18ac8 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
18ac9 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
18aca 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
18acb 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
18acc 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
18acd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18ace 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18acf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18ad1 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
18ad2 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74 69  e contains routi
18ad3 6e 65 73 20 75 73 65 64 20 66 6f 72 20 61 6e 61  nes used for ana
18ad4 6c 79 7a 69 6e 67 20 65 78 70 72 65 73 73 69 6f  lyzing expressio
18ad5 6e 73 20 61 6e 64 0a 2a 2a 20 66 6f 72 20 67 65  ns and.** for ge
18ad6 6e 65 72 61 74 69 6e 67 20 56 44 42 45 20 63 6f  nerating VDBE co
18ad7 64 65 20 74 68 61 74 20 65 76 61 6c 75 61 74 65  de that evaluate
18ad8 73 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e  s expressions in
18ad9 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24   SQLite..**.** $
18ada 49 64 3a 20 65 78 70 72 2e 63 2c 76 20 31 2e 33  Id: expr.c,v 1.3
18adb 37 31 20 32 30 30 38 2f 30 35 2f 30 31 20 31 37  71 2008/05/01 17
18adc 3a 31 36 3a 35 33 20 64 72 68 20 45 78 70 20 24  :16:53 drh Exp $
18add 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  .*/../*.** Retur
18ade 6e 20 74 68 65 20 27 61 66 66 69 6e 69 74 79 27  n the 'affinity'
18adf 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
18ae0 6f 6e 20 70 45 78 70 72 20 69 66 20 61 6e 79 2e  on pExpr if any.
18ae1 0a 2a 2a 0a 2a 2a 20 49 66 20 70 45 78 70 72 20  .**.** If pExpr 
18ae2 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 61 20 72  is a column, a r
18ae3 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 63 6f  eference to a co
18ae4 6c 75 6d 6e 20 76 69 61 20 61 6e 20 27 41 53 27  lumn via an 'AS'
18ae5 20 61 6c 69 61 73 2c 0a 2a 2a 20 6f 72 20 61 20   alias,.** or a 
18ae6 73 75 62 2d 73 65 6c 65 63 74 20 77 69 74 68 20  sub-select with 
18ae7 61 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20  a column as the 
18ae8 72 65 74 75 72 6e 20 76 61 6c 75 65 2c 20 74 68  return value, th
18ae9 65 6e 20 74 68 65 20 0a 2a 2a 20 61 66 66 69 6e  en the .** affin
18aea 69 74 79 20 6f 66 20 74 68 61 74 20 63 6f 6c 75  ity of that colu
18aeb 6d 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  mn is returned. 
18aec 4f 74 68 65 72 77 69 73 65 2c 20 30 78 30 30 20  Otherwise, 0x00 
18aed 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 2a 2a 20  is returned,.** 
18aee 69 6e 64 69 63 61 74 69 6e 67 20 6e 6f 20 61 66  indicating no af
18aef 66 69 6e 69 74 79 20 66 6f 72 20 74 68 65 20 65  finity for the e
18af0 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  xpression..**.**
18af1 20 69 2e 65 2e 20 74 68 65 20 57 48 45 52 45 20   i.e. the WHERE 
18af2 63 6c 61 75 73 65 20 65 78 70 72 65 73 73 73 69  clause expresssi
18af3 6f 6e 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f  ons in the follo
18af4 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 20  wing statements 
18af5 61 6c 6c 0a 2a 2a 20 68 61 76 65 20 61 6e 20 61  all.** have an a
18af6 66 66 69 6e 69 74 79 3a 0a 2a 2a 0a 2a 2a 20 43  ffinity:.**.** C
18af7 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 61  REATE TABLE t1(a
18af8 29 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a 20 46  );.** SELECT * F
18af9 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61 3b 0a  ROM t1 WHERE a;.
18afa 2a 2a 20 53 45 4c 45 43 54 20 61 20 41 53 20 62  ** SELECT a AS b
18afb 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 62   FROM t1 WHERE b
18afc 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a 20 46 52  ;.** SELECT * FR
18afd 4f 4d 20 74 31 20 57 48 45 52 45 20 28 73 65 6c  OM t1 WHERE (sel
18afe 65 63 74 20 61 20 66 72 6f 6d 20 74 31 29 3b 0a  ect a from t1);.
18aff 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
18b00 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 45 78  E char sqlite3Ex
18b01 70 72 41 66 66 69 6e 69 74 79 28 45 78 70 72 20  prAffinity(Expr 
18b02 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 20 6f  *pExpr){.  int o
18b03 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20  p = pExpr->op;. 
18b04 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 45 4c 45   if( op==TK_SELE
18b05 43 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  CT ){.    return
18b06 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69   sqlite3ExprAffi
18b07 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 53 65 6c  nity(pExpr->pSel
18b08 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30  ect->pEList->a[0
18b09 5d 2e 70 45 78 70 72 29 3b 0a 20 20 7d 0a 23 69  ].pExpr);.  }.#i
18b0a 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
18b0b 54 5f 43 41 53 54 0a 20 20 69 66 28 20 6f 70 3d  T_CAST.  if( op=
18b0c 3d 54 4b 5f 43 41 53 54 20 29 7b 0a 20 20 20 20  =TK_CAST ){.    
18b0d 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 66  return sqlite3Af
18b0e 66 69 6e 69 74 79 54 79 70 65 28 26 70 45 78 70  finityType(&pExp
18b0f 72 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 7d 0a 23  r->token);.  }.#
18b10 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 70  endif.  return p
18b11 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3b 0a  Expr->affinity;.
18b12 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  }../*.** Set the
18b13 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
18b14 6e 63 65 20 66 6f 72 20 65 78 70 72 65 73 73 69  nce for expressi
18b15 6f 6e 20 70 45 78 70 72 20 74 6f 20 62 65 20 74  on pExpr to be t
18b16 68 65 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20  he collating.** 
18b17 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 64 20 62  sequence named b
18b18 79 20 70 54 6f 6b 65 6e 2e 20 20 20 52 65 74 75  y pToken.   Retu
18b19 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
18b1a 74 68 65 20 72 65 76 69 73 65 64 20 65 78 70 72  the revised expr
18b1b 65 73 73 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 63  ession..** The c
18b1c 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
18b1d 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 22  e is marked as "
18b1e 65 78 70 6c 69 63 69 74 22 20 75 73 69 6e 67 20  explicit" using 
18b1f 74 68 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74  the EP_ExpCollat
18b20 65 0a 2a 2a 20 66 6c 61 67 2e 20 20 41 6e 20 65  e.** flag.  An e
18b21 78 70 6c 69 63 69 74 20 63 6f 6c 6c 61 74 69 6e  xplicit collatin
18b22 67 20 73 65 71 75 65 6e 63 65 20 77 69 6c 6c 20  g sequence will 
18b23 6f 76 65 72 72 69 64 65 20 69 6d 70 6c 69 63 69  override implici
18b24 74 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73  t.** collating s
18b25 65 71 75 65 6e 63 65 73 2e 0a 2a 2f 0a 53 51 4c  equences..*/.SQL
18b26 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
18b27 20 2a 73 71 6c 69 74 65 33 45 78 70 72 53 65 74   *sqlite3ExprSet
18b28 43 6f 6c 6c 28 50 61 72 73 65 20 2a 70 50 61 72  Coll(Parse *pPar
18b29 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c  se, Expr *pExpr,
18b2a 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a   Token *pName){.
18b2b 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 20 3d 20    char *zColl = 
18b2c 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  0;            /*
18b2d 20 44 65 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f   Dequoted name o
18b2e 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  f collation sequ
18b2f 65 6e 63 65 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65  ence */.  CollSe
18b30 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 7a 43 6f 6c  q *pColl;.  zCol
18b31 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  l = sqlite3NameF
18b32 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d  romToken(pParse-
18b33 3e 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69  >db, pName);.  i
18b34 66 28 20 70 45 78 70 72 20 26 26 20 7a 43 6f 6c  f( pExpr && zCol
18b35 6c 20 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d  l ){.    pColl =
18b36 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f   sqlite3LocateCo
18b37 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 7a 43  llSeq(pParse, zC
18b38 6f 6c 6c 2c 20 2d 31 29 3b 0a 20 20 20 20 69 66  oll, -1);.    if
18b39 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20  ( pColl ){.     
18b3a 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20   pExpr->pColl = 
18b3b 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 70 45 78  pColl;.      pEx
18b3c 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f  pr->flags |= EP_
18b3d 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20  ExpCollate;.    
18b3e 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  }.  }.  sqlite3_
18b3f 66 72 65 65 28 7a 43 6f 6c 6c 29 3b 0a 20 20 72  free(zColl);.  r
18b40 65 74 75 72 6e 20 70 45 78 70 72 3b 0a 7d 0a 0a  eturn pExpr;.}..
18b41 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
18b42 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69   default collati
18b43 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20  on sequence for 
18b44 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70  the expression p
18b45 45 78 70 72 2e 20 49 66 0a 2a 2a 20 74 68 65 72  Expr. If.** ther
18b46 65 20 69 73 20 6e 6f 20 64 65 66 61 75 6c 74 20  e is no default 
18b47 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 2c 20  collation type, 
18b48 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c  return 0..*/.SQL
18b49 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c  ITE_PRIVATE Coll
18b4a 53 65 71 20 2a 73 71 6c 69 74 65 33 45 78 70 72  Seq *sqlite3Expr
18b4b 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70  CollSeq(Parse *p
18b4c 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78  Parse, Expr *pEx
18b4d 70 72 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a  pr){.  CollSeq *
18b4e 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 69 66 28  pColl = 0;.  if(
18b4f 20 70 45 78 70 72 20 29 7b 0a 20 20 20 20 69 6e   pExpr ){.    in
18b50 74 20 6f 70 3b 0a 20 20 20 20 70 43 6f 6c 6c 20  t op;.    pColl 
18b51 3d 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b 0a  = pExpr->pColl;.
18b52 20 20 20 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e      op = pExpr->
18b53 6f 70 3b 0a 20 20 20 20 69 66 28 20 28 6f 70 3d  op;.    if( (op=
18b54 3d 54 4b 5f 43 41 53 54 20 7c 7c 20 6f 70 3d 3d  =TK_CAST || op==
18b55 54 4b 5f 55 50 4c 55 53 29 20 26 26 20 21 70 43  TK_UPLUS) && !pC
18b56 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 72 65 74  oll ){.      ret
18b57 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72 43  urn sqlite3ExprC
18b58 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70  ollSeq(pParse, p
18b59 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20  Expr->pLeft);.  
18b5a 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 73 71    }.  }.  if( sq
18b5b 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c 6c 53 65  lite3CheckCollSe
18b5c 71 28 70 50 61 72 73 65 2c 20 70 43 6f 6c 6c 29  q(pParse, pColl)
18b5d 20 29 7b 20 0a 20 20 20 20 70 43 6f 6c 6c 20 3d   ){ .    pColl =
18b5e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   0;.  }.  return
18b5f 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   pColl;.}../*.**
18b60 20 70 45 78 70 72 20 69 73 20 61 6e 20 6f 70 65   pExpr is an ope
18b61 72 61 6e 64 20 6f 66 20 61 20 63 6f 6d 70 61 72  rand of a compar
18b62 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20  ison operator.  
18b63 61 66 66 32 20 69 73 20 74 68 65 0a 2a 2a 20 74  aff2 is the.** t
18b64 79 70 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20  ype affinity of 
18b65 74 68 65 20 6f 74 68 65 72 20 6f 70 65 72 61 6e  the other operan
18b66 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  d.  This routine
18b67 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20   returns the.** 
18b68 74 79 70 65 20 61 66 66 69 6e 69 74 79 20 74 68  type affinity th
18b69 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65  at should be use
18b6a 64 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 61 72  d for the compar
18b6b 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 0a 2a  ison operator..*
18b6c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18b6d 20 63 68 61 72 20 73 71 6c 69 74 65 33 43 6f 6d   char sqlite3Com
18b6e 70 61 72 65 41 66 66 69 6e 69 74 79 28 45 78 70  pareAffinity(Exp
18b6f 72 20 2a 70 45 78 70 72 2c 20 63 68 61 72 20 61  r *pExpr, char a
18b70 66 66 32 29 7b 0a 20 20 63 68 61 72 20 61 66 66  ff2){.  char aff
18b71 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  1 = sqlite3ExprA
18b72 66 66 69 6e 69 74 79 28 70 45 78 70 72 29 3b 0a  ffinity(pExpr);.
18b73 20 20 69 66 28 20 61 66 66 31 20 26 26 20 61 66    if( aff1 && af
18b74 66 32 20 29 7b 0a 20 20 20 20 2f 2a 20 42 6f 74  f2 ){.    /* Bot
18b75 68 20 73 69 64 65 73 20 6f 66 20 74 68 65 20 63  h sides of the c
18b76 6f 6d 70 61 72 69 73 6f 6e 20 61 72 65 20 63 6f  omparison are co
18b77 6c 75 6d 6e 73 2e 20 49 66 20 6f 6e 65 20 68 61  lumns. If one ha
18b78 73 20 6e 75 6d 65 72 69 63 0a 20 20 20 20 2a 2a  s numeric.    **
18b79 20 61 66 66 69 6e 69 74 79 2c 20 75 73 65 20 74   affinity, use t
18b7a 68 61 74 2e 20 4f 74 68 65 72 77 69 73 65 20 75  hat. Otherwise u
18b7b 73 65 20 6e 6f 20 61 66 66 69 6e 69 74 79 2e 0a  se no affinity..
18b7c 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73      */.    if( s
18b7d 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 69 63 41  qlite3IsNumericA
18b7e 66 66 69 6e 69 74 79 28 61 66 66 31 29 20 7c 7c  ffinity(aff1) ||
18b7f 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 69   sqlite3IsNumeri
18b80 63 41 66 66 69 6e 69 74 79 28 61 66 66 32 29 20  cAffinity(aff2) 
18b81 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
18b82 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52  SQLITE_AFF_NUMER
18b83 49 43 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  IC;.    }else{. 
18b84 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
18b85 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20  TE_AFF_NONE;.   
18b86 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21   }.  }else if( !
18b87 61 66 66 31 20 26 26 20 21 61 66 66 32 20 29 7b  aff1 && !aff2 ){
18b88 0a 20 20 20 20 2f 2a 20 4e 65 69 74 68 65 72 20  .    /* Neither 
18b89 73 69 64 65 20 6f 66 20 74 68 65 20 63 6f 6d 70  side of the comp
18b8a 61 72 69 73 6f 6e 20 69 73 20 61 20 63 6f 6c 75  arison is a colu
18b8b 6d 6e 2e 20 20 43 6f 6d 70 61 72 65 20 74 68 65  mn.  Compare the
18b8c 0a 20 20 20 20 2a 2a 20 72 65 73 75 6c 74 73 20  .    ** results 
18b8d 64 69 72 65 63 74 6c 79 2e 0a 20 20 20 20 2a 2f  directly..    */
18b8e 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
18b8f 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d  TE_AFF_NONE;.  }
18b90 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4f 6e 65  else{.    /* One
18b91 20 73 69 64 65 20 69 73 20 61 20 63 6f 6c 75 6d   side is a colum
18b92 6e 2c 20 74 68 65 20 6f 74 68 65 72 20 69 73 20  n, the other is 
18b93 6e 6f 74 2e 20 55 73 65 20 74 68 65 20 63 6f 6c  not. Use the col
18b94 75 6d 6e 73 20 61 66 66 69 6e 69 74 79 2e 20 2a  umns affinity. *
18b95 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 61 66  /.    assert( af
18b96 66 31 3d 3d 30 20 7c 7c 20 61 66 66 32 3d 3d 30  f1==0 || aff2==0
18b97 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 28   );.    return (
18b98 61 66 66 31 20 2b 20 61 66 66 32 29 3b 0a 20 20  aff1 + aff2);.  
18b99 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72  }.}../*.** pExpr
18b9a 20 69 73 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e   is a comparison
18b9b 20 6f 70 65 72 61 74 6f 72 2e 20 20 52 65 74 75   operator.  Retu
18b9c 72 6e 20 74 68 65 20 74 79 70 65 20 61 66 66 69  rn the type affi
18b9d 6e 69 74 79 20 74 68 61 74 20 73 68 6f 75 6c 64  nity that should
18b9e 0a 2a 2a 20 62 65 20 61 70 70 6c 69 65 64 20 74  .** be applied t
18b9f 6f 20 62 6f 74 68 20 6f 70 65 72 61 6e 64 73 20  o both operands 
18ba0 70 72 69 6f 72 20 74 6f 20 64 6f 69 6e 67 20 74  prior to doing t
18ba1 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 0a 2a  he comparison..*
18ba2 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 63 6f  /.static char co
18ba3 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 74 79  mparisonAffinity
18ba4 28 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20  (Expr *pExpr){. 
18ba5 20 63 68 61 72 20 61 66 66 3b 0a 20 20 61 73 73   char aff;.  ass
18ba6 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d  ert( pExpr->op==
18ba7 54 4b 5f 45 51 20 7c 7c 20 70 45 78 70 72 2d 3e  TK_EQ || pExpr->
18ba8 6f 70 3d 3d 54 4b 5f 49 4e 20 7c 7c 20 70 45 78  op==TK_IN || pEx
18ba9 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c 54 20 7c 7c  pr->op==TK_LT ||
18baa 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72  .          pExpr
18bab 2d 3e 6f 70 3d 3d 54 4b 5f 47 54 20 7c 7c 20 70  ->op==TK_GT || p
18bac 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 47 45 20  Expr->op==TK_GE 
18bad 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  || pExpr->op==TK
18bae 5f 4c 45 20 7c 7c 0a 20 20 20 20 20 20 20 20 20  _LE ||.         
18baf 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4e   pExpr->op==TK_N
18bb0 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  E );.  assert( p
18bb1 45 78 70 72 2d 3e 70 4c 65 66 74 20 29 3b 0a 20  Expr->pLeft );. 
18bb2 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 45 78   aff = sqlite3Ex
18bb3 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72  prAffinity(pExpr
18bb4 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 69 66 28 20  ->pLeft);.  if( 
18bb5 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 29 7b  pExpr->pRight ){
18bb6 0a 20 20 20 20 61 66 66 20 3d 20 73 71 6c 69 74  .    aff = sqlit
18bb7 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74  e3CompareAffinit
18bb8 79 28 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  y(pExpr->pRight,
18bb9 20 61 66 66 29 3b 0a 20 20 7d 0a 20 20 65 6c 73   aff);.  }.  els
18bba 65 20 69 66 28 20 70 45 78 70 72 2d 3e 70 53 65  e if( pExpr->pSe
18bbb 6c 65 63 74 20 29 7b 0a 20 20 20 20 61 66 66 20  lect ){.    aff 
18bbc 3d 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65  = sqlite3Compare
18bbd 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e  Affinity(pExpr->
18bbe 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d  pSelect->pEList-
18bbf 3e 61 5b 30 5d 2e 70 45 78 70 72 2c 20 61 66 66  >a[0].pExpr, aff
18bc0 29 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 20 69 66  );.  }.  else if
18bc1 28 20 21 61 66 66 20 29 7b 0a 20 20 20 20 61 66  ( !aff ){.    af
18bc2 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  f = SQLITE_AFF_N
18bc3 4f 4e 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ONE;.  }.  retur
18bc4 6e 20 61 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n aff;.}../*.** 
18bc5 70 45 78 70 72 20 69 73 20 61 20 63 6f 6d 70 61  pExpr is a compa
18bc6 72 69 73 6f 6e 20 65 78 70 72 65 73 73 69 6f 6e  rison expression
18bc7 2c 20 65 67 2e 20 27 3d 27 2c 20 27 3c 27 2c 20  , eg. '=', '<', 
18bc8 49 4e 28 2e 2e 2e 29 20 65 74 63 2e 0a 2a 2a 20  IN(...) etc..** 
18bc9 69 64 78 5f 61 66 66 69 6e 69 74 79 20 69 73 20  idx_affinity is 
18bca 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20  the affinity of 
18bcb 61 6e 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d  an indexed colum
18bcc 6e 2e 20 52 65 74 75 72 6e 20 74 72 75 65 0a 2a  n. Return true.*
18bcd 2a 20 69 66 20 74 68 65 20 69 6e 64 65 78 20 77  * if the index w
18bce 69 74 68 20 61 66 66 69 6e 69 74 79 20 69 64 78  ith affinity idx
18bcf 5f 61 66 66 69 6e 69 74 79 20 6d 61 79 20 62 65  _affinity may be
18bd0 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
18bd1 6e 74 0a 2a 2a 20 74 68 65 20 63 6f 6d 70 61 72  nt.** the compar
18bd2 69 73 6f 6e 20 69 6e 20 70 45 78 70 72 2e 0a 2a  ison in pExpr..*
18bd3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18bd4 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65   int sqlite3Inde
18bd5 78 41 66 66 69 6e 69 74 79 4f 6b 28 45 78 70 72  xAffinityOk(Expr
18bd6 20 2a 70 45 78 70 72 2c 20 63 68 61 72 20 69 64   *pExpr, char id
18bd7 78 5f 61 66 66 69 6e 69 74 79 29 7b 0a 20 20 63  x_affinity){.  c
18bd8 68 61 72 20 61 66 66 20 3d 20 63 6f 6d 70 61 72  har aff = compar
18bd9 69 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 45 78  isonAffinity(pEx
18bda 70 72 29 3b 0a 20 20 73 77 69 74 63 68 28 20 61  pr);.  switch( a
18bdb 66 66 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53  ff ){.    case S
18bdc 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a 0a  QLITE_AFF_NONE:.
18bdd 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
18bde 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
18bdf 41 46 46 5f 54 45 58 54 3a 0a 20 20 20 20 20 20  AFF_TEXT:.      
18be0 72 65 74 75 72 6e 20 69 64 78 5f 61 66 66 69 6e  return idx_affin
18be1 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  ity==SQLITE_AFF_
18be2 54 45 58 54 3b 0a 20 20 20 20 64 65 66 61 75 6c  TEXT;.    defaul
18be3 74 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  t:.      return 
18be4 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 69 63  sqlite3IsNumeric
18be5 41 66 66 69 6e 69 74 79 28 69 64 78 5f 61 66 66  Affinity(idx_aff
18be6 69 6e 69 74 79 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  inity);.  }.}../
18be7 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
18be8 50 35 20 76 61 6c 75 65 20 74 68 61 74 20 73 68  P5 value that sh
18be9 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72  ould be used for
18bea 20 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61 72   a binary compar
18beb 69 73 6f 6e 0a 2a 2a 20 6f 70 63 6f 64 65 20 28  ison.** opcode (
18bec 4f 50 5f 45 71 2c 20 4f 50 5f 47 65 20 65 74 63  OP_Eq, OP_Ge etc
18bed 2e 29 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 61  .) used to compa
18bee 72 65 20 70 45 78 70 72 31 20 61 6e 64 20 70 45  re pExpr1 and pE
18bef 78 70 72 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  xpr2..*/.static 
18bf0 75 38 20 62 69 6e 61 72 79 43 6f 6d 70 61 72 65  u8 binaryCompare
18bf1 50 35 28 45 78 70 72 20 2a 70 45 78 70 72 31 2c  P5(Expr *pExpr1,
18bf2 20 45 78 70 72 20 2a 70 45 78 70 72 32 2c 20 69   Expr *pExpr2, i
18bf3 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a  nt jumpIfNull){.
18bf4 20 20 75 38 20 61 66 66 20 3d 20 28 63 68 61 72    u8 aff = (char
18bf5 29 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69  )sqlite3ExprAffi
18bf6 6e 69 74 79 28 70 45 78 70 72 32 29 3b 0a 20 20  nity(pExpr2);.  
18bf7 61 66 66 20 3d 20 73 71 6c 69 74 65 33 43 6f 6d  aff = sqlite3Com
18bf8 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 45 78  pareAffinity(pEx
18bf9 70 72 31 2c 20 61 66 66 29 20 7c 20 6a 75 6d 70  pr1, aff) | jump
18bfa 49 66 4e 75 6c 6c 3b 0a 20 20 72 65 74 75 72 6e  IfNull;.  return
18bfb 20 61 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   aff;.}../*.** R
18bfc 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
18bfd 74 6f 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  to the collation
18bfe 20 73 65 71 75 65 6e 63 65 20 74 68 61 74 20 73   sequence that s
18bff 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 62 79  hould be used by
18c00 0a 2a 2a 20 61 20 62 69 6e 61 72 79 20 63 6f 6d  .** a binary com
18c01 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72  parison operator
18c02 20 63 6f 6d 70 61 72 69 6e 67 20 70 4c 65 66 74   comparing pLeft
18c03 20 61 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2a 0a   and pRight..**.
18c04 2a 2a 20 49 66 20 74 68 65 20 6c 65 66 74 20 68  ** If the left h
18c05 61 6e 64 20 65 78 70 72 65 73 73 69 6f 6e 20 68  and expression h
18c06 61 73 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73  as a collating s
18c07 65 71 75 65 6e 63 65 20 74 79 70 65 2c 20 74 68  equence type, th
18c08 65 6e 20 69 74 20 69 73 0a 2a 2a 20 75 73 65 64  en it is.** used
18c09 2e 20 4f 74 68 65 72 77 69 73 65 20 74 68 65 20  . Otherwise the 
18c0a 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
18c0b 63 65 20 66 6f 72 20 74 68 65 20 72 69 67 68 74  ce for the right
18c0c 20 68 61 6e 64 20 65 78 70 72 65 73 73 69 6f 6e   hand expression
18c0d 0a 2a 2a 20 69 73 20 75 73 65 64 2c 20 6f 72 20  .** is used, or 
18c0e 74 68 65 20 64 65 66 61 75 6c 74 20 28 42 49 4e  the default (BIN
18c0f 41 52 59 29 20 69 66 20 6e 65 69 74 68 65 72 20  ARY) if neither 
18c10 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73 20 61  expression has a
18c11 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 74 79   collating.** ty
18c12 70 65 2e 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65  pe..**.** Argume
18c13 6e 74 20 70 52 69 67 68 74 20 28 62 75 74 20 6e  nt pRight (but n
18c14 6f 74 20 70 4c 65 66 74 29 20 6d 61 79 20 62 65  ot pLeft) may be
18c15 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 2e   a null pointer.
18c16 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 0a 2a   In this case,.*
18c17 2a 20 69 74 20 69 73 20 6e 6f 74 20 63 6f 6e 73  * it is not cons
18c18 69 64 65 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  idered..*/.SQLIT
18c19 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65  E_PRIVATE CollSe
18c1a 71 20 2a 73 71 6c 69 74 65 33 42 69 6e 61 72 79  q *sqlite3Binary
18c1b 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 0a  CompareCollSeq(.
18c1c 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
18c1d 20 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c   .  Expr *pLeft,
18c1e 20 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74   .  Expr *pRight
18c1f 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70  .){.  CollSeq *p
18c20 43 6f 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20  Coll;.  assert( 
18c21 70 4c 65 66 74 20 29 3b 0a 20 20 69 66 28 20 70  pLeft );.  if( p
18c22 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 45 50  Left->flags & EP
18c23 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20  _ExpCollate ){. 
18c24 20 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 74     assert( pLeft
18c25 2d 3e 70 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 70  ->pColl );.    p
18c26 43 6f 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e 70 43  Coll = pLeft->pC
18c27 6f 6c 6c 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  oll;.  }else if(
18c28 20 70 52 69 67 68 74 20 26 26 20 70 52 69 67 68   pRight && pRigh
18c29 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78  t->flags & EP_Ex
18c2a 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20  pCollate ){.    
18c2b 61 73 73 65 72 74 28 20 70 52 69 67 68 74 2d 3e  assert( pRight->
18c2c 70 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f  pColl );.    pCo
18c2d 6c 6c 20 3d 20 70 52 69 67 68 74 2d 3e 70 43 6f  ll = pRight->pCo
18c2e 6c 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ll;.  }else{.   
18c2f 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   pColl = sqlite3
18c30 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72  ExprCollSeq(pPar
18c31 73 65 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20  se, pLeft);.    
18c32 69 66 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20  if( !pColl ){.  
18c33 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69      pColl = sqli
18c34 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70  te3ExprCollSeq(p
18c35 50 61 72 73 65 2c 20 70 52 69 67 68 74 29 3b 0a  Parse, pRight);.
18c36 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
18c37 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a  rn pColl;.}../*.
18c38 2a 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65 20  ** Generate the 
18c39 6f 70 65 72 61 6e 64 73 20 66 6f 72 20 61 20 63  operands for a c
18c3a 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74  omparison operat
18c3b 69 6f 6e 2e 20 20 42 65 66 6f 72 65 0a 2a 2a 20  ion.  Before.** 
18c3c 67 65 6e 65 72 61 74 69 6e 67 20 74 68 65 20 63  generating the c
18c3d 6f 64 65 20 66 6f 72 20 65 61 63 68 20 6f 70 65  ode for each ope
18c3e 72 61 6e 64 2c 20 73 65 74 20 74 68 65 20 45 50  rand, set the EP
18c3f 5f 41 6e 79 41 66 66 0a 2a 2a 20 66 6c 61 67 20  _AnyAff.** flag 
18c40 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  on the expressio
18c41 6e 20 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c  n so that it wil
18c42 6c 20 62 65 20 61 62 6c 65 20 74 6f 20 75 73 65  l be able to use
18c43 64 20 61 0a 2a 2a 20 63 61 63 68 65 64 20 63 6f  d a.** cached co
18c44 6c 75 6d 6e 20 76 61 6c 75 65 20 74 68 61 74 20  lumn value that 
18c45 68 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 75  has previously u
18c46 6e 64 65 72 67 6f 6e 65 20 61 6e 0a 2a 2a 20 61  ndergone an.** a
18c47 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 2e 0a  ffinity change..
18c48 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
18c49 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e  odeCompareOperan
18c4a 64 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ds(.  Parse *pPa
18c4b 72 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69  rse,    /* Parsi
18c4c 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65  ng and code gene
18c4d 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  rating context *
18c4e 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c  /.  Expr *pLeft,
18c4f 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 66        /* The lef
18c50 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69  t operand */.  i
18c51 6e 74 20 2a 70 52 65 67 4c 65 66 74 2c 20 20 20  nt *pRegLeft,   
18c52 20 2f 2a 20 52 65 67 69 73 74 65 72 20 77 68 65   /* Register whe
18c53 72 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20  re left operand 
18c54 69 73 20 73 74 6f 72 65 64 20 2a 2f 0a 20 20 69  is stored */.  i
18c55 6e 74 20 2a 70 46 72 65 65 4c 65 66 74 2c 20 20  nt *pFreeLeft,  
18c56 20 2f 2a 20 46 72 65 65 20 74 68 69 73 20 72 65   /* Free this re
18c57 67 69 73 74 65 72 20 77 68 65 6e 20 64 6f 6e 65  gister when done
18c58 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67   */.  Expr *pRig
18c59 68 74 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 72  ht,     /* The r
18c5a 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a  ight operand */.
18c5b 20 20 69 6e 74 20 2a 70 52 65 67 52 69 67 68 74    int *pRegRight
18c5c 2c 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20  ,   /* Register 
18c5d 77 68 65 72 65 20 72 69 67 68 74 20 6f 70 65 72  where right oper
18c5e 61 6e 64 20 69 73 20 73 74 6f 72 65 64 20 2a 2f  and is stored */
18c5f 0a 20 20 69 6e 74 20 2a 70 46 72 65 65 52 69 67  .  int *pFreeRig
18c60 68 74 20 20 20 2f 2a 20 57 72 69 74 65 20 74 65  ht   /* Write te
18c61 6d 70 20 72 65 67 69 73 74 65 72 20 66 6f 72 20  mp register for 
18c62 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 74 68  right operand th
18c63 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c  ere */.){.  whil
18c64 65 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b  e( pLeft->op==TK
18c65 5f 55 50 4c 55 53 20 29 20 70 4c 65 66 74 20 3d  _UPLUS ) pLeft =
18c66 20 70 4c 65 66 74 2d 3e 70 4c 65 66 74 3b 0a 20   pLeft->pLeft;. 
18c67 20 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20 7c 3d   pLeft->flags |=
18c68 20 45 50 5f 41 6e 79 41 66 66 3b 0a 20 20 2a 70   EP_AnyAff;.  *p
18c69 52 65 67 4c 65 66 74 20 3d 20 73 71 6c 69 74 65  RegLeft = sqlite
18c6a 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
18c6b 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 46 72  arse, pLeft, pFr
18c6c 65 65 4c 65 66 74 29 3b 0a 20 20 77 68 69 6c 65  eeLeft);.  while
18c6d 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b  ( pRight->op==TK
18c6e 5f 55 50 4c 55 53 20 29 20 70 52 69 67 68 74 20  _UPLUS ) pRight 
18c6f 3d 20 70 52 69 67 68 74 2d 3e 70 4c 65 66 74 3b  = pRight->pLeft;
18c70 0a 20 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 73  .  pRight->flags
18c71 20 7c 3d 20 45 50 5f 41 6e 79 41 66 66 3b 0a 20   |= EP_AnyAff;. 
18c72 20 2a 70 52 65 67 52 69 67 68 74 20 3d 20 73 71   *pRegRight = sq
18c73 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d  lite3ExprCodeTem
18c74 70 28 70 50 61 72 73 65 2c 20 70 52 69 67 68 74  p(pParse, pRight
18c75 2c 20 70 46 72 65 65 52 69 67 68 74 29 3b 0a 7d  , pFreeRight);.}
18c76 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
18c77 20 63 6f 64 65 20 66 6f 72 20 61 20 63 6f 6d 70   code for a comp
18c78 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e  arison operator.
18c79 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
18c7a 6f 64 65 43 6f 6d 70 61 72 65 28 0a 20 20 50 61  odeCompare(.  Pa
18c7b 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
18c7c 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 28  /* The parsing (
18c7d 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74  and code generat
18c7e 69 6e 67 29 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  ing) context */.
18c7f 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20    Expr *pLeft,  
18c80 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20      /* The left 
18c81 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 45 78 70  operand */.  Exp
18c82 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20 20 2f  r *pRight,     /
18c83 2a 20 54 68 65 20 72 69 67 68 74 20 6f 70 65 72  * The right oper
18c84 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 63  and */.  int opc
18c85 6f 64 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 68  ode,       /* Th
18c86 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 63  e comparison opc
18c87 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 31  ode */.  int in1
18c88 2c 20 69 6e 74 20 69 6e 32 2c 20 2f 2a 20 52 65  , int in2, /* Re
18c89 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 6f  gister holding o
18c8a 70 65 72 61 6e 64 73 20 2a 2f 0a 20 20 69 6e 74  perands */.  int
18c8b 20 64 65 73 74 2c 20 20 20 20 20 20 20 20 20 2f   dest,         /
18c8c 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66 20 74  * Jump here if t
18c8d 72 75 65 2e 20 20 2a 2f 0a 20 20 69 6e 74 20 6a  rue.  */.  int j
18c8e 75 6d 70 49 66 4e 75 6c 6c 20 20 20 20 2f 2a 20  umpIfNull    /* 
18c8f 49 66 20 74 72 75 65 2c 20 6a 75 6d 70 20 69 66  If true, jump if
18c90 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20   either operand 
18c91 69 73 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20  is NULL */.){.  
18c92 69 6e 74 20 70 35 3b 0a 20 20 69 6e 74 20 61 64  int p5;.  int ad
18c93 64 72 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70  dr;.  CollSeq *p
18c94 34 3b 0a 0a 20 20 70 34 20 3d 20 73 71 6c 69 74  4;..  p4 = sqlit
18c95 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43  e3BinaryCompareC
18c96 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70  ollSeq(pParse, p
18c97 4c 65 66 74 2c 20 70 52 69 67 68 74 29 3b 0a 20  Left, pRight);. 
18c98 20 70 35 20 3d 20 62 69 6e 61 72 79 43 6f 6d 70   p5 = binaryComp
18c99 61 72 65 50 35 28 70 4c 65 66 74 2c 20 70 52 69  areP5(pLeft, pRi
18c9a 67 68 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29  ght, jumpIfNull)
18c9b 3b 0a 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74  ;.  addr = sqlit
18c9c 65 33 56 64 62 65 41 64 64 4f 70 34 28 70 50 61  e3VdbeAddOp4(pPa
18c9d 72 73 65 2d 3e 70 56 64 62 65 2c 20 6f 70 63 6f  rse->pVdbe, opco
18c9e 64 65 2c 20 69 6e 32 2c 20 64 65 73 74 2c 20 69  de, in2, dest, i
18c9f 6e 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n1,.            
18ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
18ca1 76 6f 69 64 2a 29 70 34 2c 20 50 34 5f 43 4f 4c  void*)p4, P4_COL
18ca2 4c 53 45 51 29 3b 0a 20 20 73 71 6c 69 74 65 33  LSEQ);.  sqlite3
18ca3 56 64 62 65 43 68 61 6e 67 65 50 35 28 70 50 61  VdbeChangeP5(pPa
18ca4 72 73 65 2d 3e 70 56 64 62 65 2c 20 70 35 29 3b  rse->pVdbe, p5);
18ca5 0a 20 20 69 66 28 20 70 35 20 26 20 53 51 4c 49  .  if( p5 & SQLI
18ca6 54 45 5f 41 46 46 5f 4d 41 53 4b 20 29 7b 0a 20  TE_AFF_MASK ){. 
18ca7 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
18ca8 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67  cheAffinityChang
18ca9 65 28 70 50 61 72 73 65 2c 20 69 6e 31 2c 20 31  e(pParse, in1, 1
18caa 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
18cab 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43  prCacheAffinityC
18cac 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69 6e  hange(pParse, in
18cad 32 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74  2, 1);.  }.  ret
18cae 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a  urn addr;.}../*.
18caf 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 20 6e  ** Construct a n
18cb0 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f  ew expression no
18cb1 64 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20  de and return a 
18cb2 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20  pointer to it.  
18cb3 4d 65 6d 6f 72 79 0a 2a 2a 20 66 6f 72 20 74 68  Memory.** for th
18cb4 69 73 20 6e 6f 64 65 20 69 73 20 6f 62 74 61 69  is node is obtai
18cb5 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
18cb6 5f 6d 61 6c 6c 6f 63 28 29 2e 20 20 54 68 65 20  _malloc().  The 
18cb7 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  calling function
18cb8 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 62  .** is responsib
18cb9 6c 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73 75  le for making su
18cba 72 65 20 74 68 65 20 6e 6f 64 65 20 65 76 65 6e  re the node even
18cbb 74 75 61 6c 6c 79 20 67 65 74 73 20 66 72 65 65  tually gets free
18cbc 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
18cbd 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74  VATE Expr *sqlit
18cbe 65 33 45 78 70 72 28 0a 20 20 73 71 6c 69 74 65  e3Expr(.  sqlite
18cbf 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
18cc0 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20    /* Handle for 
18cc1 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
18cc2 65 72 6f 28 29 20 28 6d 61 79 20 62 65 20 6e 75  ero() (may be nu
18cc3 6c 6c 29 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c  ll) */.  int op,
18cc4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18cc5 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 6f   /* Expression o
18cc6 70 63 6f 64 65 20 2a 2f 0a 20 20 45 78 70 72 20  pcode */.  Expr 
18cc7 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 20 20 20  *pLeft,         
18cc8 20 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61     /* Left opera
18cc9 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52  nd */.  Expr *pR
18cca 69 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 20  ight,           
18ccb 2f 2a 20 52 69 67 68 74 20 6f 70 65 72 61 6e 64  /* Right operand
18ccc 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65   */.  const Toke
18ccd 6e 20 2a 70 54 6f 6b 65 6e 20 20 20 20 20 2f 2a  n *pToken     /*
18cce 20 41 72 67 75 6d 65 6e 74 20 74 6f 6b 65 6e 20   Argument token 
18ccf 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e  */.){.  Expr *pN
18cd0 65 77 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c  ew;.  pNew = sql
18cd1 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
18cd2 28 64 62 2c 20 73 69 7a 65 6f 66 28 45 78 70 72  (db, sizeof(Expr
18cd3 29 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d  ));.  if( pNew==
18cd4 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68 65 6e  0 ){.    /* When
18cd5 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 64   malloc fails, d
18cd6 65 6c 65 74 65 20 70 4c 65 66 74 20 61 6e 64 20  elete pLeft and 
18cd7 70 52 69 67 68 74 2e 20 45 78 70 72 65 73 73 69  pRight. Expressi
18cd8 6f 6e 73 20 70 61 73 73 65 64 20 74 6f 20 0a 20  ons passed to . 
18cd9 20 20 20 2a 2a 20 74 68 69 73 20 66 75 6e 63 74     ** this funct
18cda 69 6f 6e 20 6d 75 73 74 20 61 6c 77 61 79 73 20  ion must always 
18cdb 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 77 69 74  be allocated wit
18cdc 68 20 73 71 6c 69 74 65 33 45 78 70 72 28 29 20  h sqlite3Expr() 
18cdd 66 6f 72 20 74 68 69 73 20 0a 20 20 20 20 2a 2a  for this .    **
18cde 20 72 65 61 73 6f 6e 2e 20 0a 20 20 20 20 2a 2f   reason. .    */
18cdf 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
18ce0 44 65 6c 65 74 65 28 70 4c 65 66 74 29 3b 0a 20  Delete(pLeft);. 
18ce1 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
18ce2 6c 65 74 65 28 70 52 69 67 68 74 29 3b 0a 20 20  lete(pRight);.  
18ce3 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
18ce4 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 6f 70 3b    pNew->op = op;
18ce5 0a 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d  .  pNew->pLeft =
18ce6 20 70 4c 65 66 74 3b 0a 20 20 70 4e 65 77 2d 3e   pLeft;.  pNew->
18ce7 70 52 69 67 68 74 20 3d 20 70 52 69 67 68 74 3b  pRight = pRight;
18ce8 0a 20 20 70 4e 65 77 2d 3e 69 41 67 67 20 3d 20  .  pNew->iAgg = 
18ce9 2d 31 3b 0a 20 20 69 66 28 20 70 54 6f 6b 65 6e  -1;.  if( pToken
18cea 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
18ceb 70 54 6f 6b 65 6e 2d 3e 64 79 6e 3d 3d 30 20 29  pToken->dyn==0 )
18cec 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 73 70 61 6e  ;.    pNew->span
18ced 20 3d 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 20 3d   = pNew->token =
18cee 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20 7d 65 6c 73   *pToken;.  }els
18cef 65 20 69 66 28 20 70 4c 65 66 74 20 29 7b 0a 20  e if( pLeft ){. 
18cf0 20 20 20 69 66 28 20 70 52 69 67 68 74 20 29 7b     if( pRight ){
18cf1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
18cf2 70 72 53 70 61 6e 28 70 4e 65 77 2c 20 26 70 4c  prSpan(pNew, &pL
18cf3 65 66 74 2d 3e 73 70 61 6e 2c 20 26 70 52 69 67  eft->span, &pRig
18cf4 68 74 2d 3e 73 70 61 6e 29 3b 0a 20 20 20 20 20  ht->span);.     
18cf5 20 69 66 28 20 70 52 69 67 68 74 2d 3e 66 6c 61   if( pRight->fla
18cf6 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61  gs & EP_ExpColla
18cf7 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4e  te ){.        pN
18cf8 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f  ew->flags |= EP_
18cf9 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20  ExpCollate;.    
18cfa 20 20 20 20 70 4e 65 77 2d 3e 70 43 6f 6c 6c 20      pNew->pColl 
18cfb 3d 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 3b  = pRight->pColl;
18cfc 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
18cfd 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 66 6c     if( pLeft->fl
18cfe 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c  ags & EP_ExpColl
18cff 61 74 65 20 29 7b 0a 20 20 20 20 20 20 70 4e 65  ate ){.      pNe
18d00 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45  w->flags |= EP_E
18d01 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 20  xpCollate;.     
18d02 20 70 4e 65 77 2d 3e 70 43 6f 6c 6c 20 3d 20 70   pNew->pColl = p
18d03 4c 65 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20  Left->pColl;.   
18d04 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65   }.  }..  sqlite
18d05 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70  3ExprSetHeight(p
18d06 4e 65 77 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  New);.  return p
18d07 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 6f  New;.}../*.** Wo
18d08 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 74 65 33  rks like sqlite3
18d09 45 78 70 72 28 29 20 65 78 63 65 70 74 20 74 68  Expr() except th
18d0a 61 74 20 69 74 20 74 61 6b 65 73 20 61 6e 20 65  at it takes an e
18d0b 78 74 72 61 20 50 61 72 73 65 2a 0a 2a 2a 20 61  xtra Parse*.** a
18d0c 72 67 75 6d 65 6e 74 20 61 6e 64 20 6e 6f 74 69  rgument and noti
18d0d 66 69 65 73 20 74 68 65 20 61 73 73 6f 63 69 61  fies the associa
18d0e 74 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f  ted connection o
18d0f 62 6a 65 63 74 20 69 66 20 6d 61 6c 6c 6f 63 20  bject if malloc 
18d10 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  fails..*/.SQLITE
18d11 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73  _PRIVATE Expr *s
18d12 71 6c 69 74 65 33 50 45 78 70 72 28 0a 20 20 50  qlite3PExpr(.  P
18d13 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
18d14 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
18d15 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69  g context */.  i
18d16 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20  nt op,          
18d17 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73         /* Expres
18d18 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20  sion opcode */. 
18d19 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20   Expr *pLeft,   
18d1a 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 66 74           /* Left
18d1b 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 45 78   operand */.  Ex
18d1c 70 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20 20  pr *pRight,     
18d1d 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6f        /* Right o
18d1e 70 65 72 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73  perand */.  cons
18d1f 74 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20  t Token *pToken 
18d20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20      /* Argument 
18d21 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 72 65  token */.){.  re
18d22 74 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72  turn sqlite3Expr
18d23 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 6f 70 2c  (pParse->db, op,
18d24 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20   pLeft, pRight, 
18d25 70 54 6f 6b 65 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  pToken);.}../*.*
18d26 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20 61 20 6e  * When doing a n
18d27 65 73 74 65 64 20 70 61 72 73 65 2c 20 79 6f 75  ested parse, you
18d28 20 63 61 6e 20 69 6e 63 6c 75 64 65 20 74 65 72   can include ter
18d29 6d 73 20 69 6e 20 61 6e 20 65 78 70 72 65 73 73  ms in an express
18d2a 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 6c 6f 6f 6b  ion.** that look
18d2b 20 6c 69 6b 65 20 74 68 69 73 3a 20 20 20 23 31   like this:   #1
18d2c 20 23 32 20 2e 2e 2e 20 20 54 68 65 73 65 20 74   #2 ...  These t
18d2d 65 72 6d 73 20 72 65 66 65 72 20 74 6f 20 72 65  erms refer to re
18d2e 67 69 73 74 65 72 73 0a 2a 2a 20 69 6e 20 74 68  gisters.** in th
18d2f 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  e virtual machin
18d30 65 2e 20 20 23 4e 20 69 73 20 74 68 65 20 4e 2d  e.  #N is the N-
18d31 74 68 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a 0a  th register..**.
18d32 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
18d33 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65  is called by the
18d34 20 70 61 72 73 65 72 20 74 6f 20 64 65 61 6c 20   parser to deal 
18d35 77 69 74 68 20 6f 6e 20 6f 66 20 74 68 6f 73 65  with on of those
18d36 20 74 65 72 6d 73 2e 0a 2a 2a 20 49 74 20 69 6d   terms..** It im
18d37 6d 65 64 69 61 74 65 6c 79 20 67 65 6e 65 72 61  mediately genera
18d38 74 65 73 20 63 6f 64 65 20 74 6f 20 73 74 6f 72  tes code to stor
18d39 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 61  e the value in a
18d3a 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e   memory location
18d3b 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 73  ..** The returns
18d3c 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74   an expression t
18d3d 68 61 74 20 77 69 6c 6c 20 63 6f 64 65 20 74 6f  hat will code to
18d3e 20 65 78 74 72 61 63 74 20 74 68 65 20 76 61 6c   extract the val
18d3f 75 65 20 66 72 6f 6d 0a 2a 2a 20 74 68 61 74 20  ue from.** that 
18d40 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20  memory location 
18d41 61 73 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 53 51  as needed..*/.SQ
18d42 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
18d43 72 20 2a 73 71 6c 69 74 65 33 52 65 67 69 73 74  r *sqlite3Regist
18d44 65 72 45 78 70 72 28 50 61 72 73 65 20 2a 70 50  erExpr(Parse *pP
18d45 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f  arse, Token *pTo
18d46 6b 65 6e 29 7b 0a 20 20 56 64 62 65 20 2a 76 20  ken){.  Vdbe *v 
18d47 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
18d48 0a 20 20 45 78 70 72 20 2a 70 3b 0a 20 20 69 66  .  Expr *p;.  if
18d49 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64  ( pParse->nested
18d4a 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
18d4b 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
18d4c 65 2c 20 22 6e 65 61 72 20 5c 22 25 54 5c 22 3a  e, "near \"%T\":
18d4d 20 73 79 6e 74 61 78 20 65 72 72 6f 72 22 2c 20   syntax error", 
18d4e 70 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 72 65 74  pToken);.    ret
18d4f 75 72 6e 20 73 71 6c 69 74 65 33 50 45 78 70 72  urn sqlite3PExpr
18d50 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 55 4c 4c  (pParse, TK_NULL
18d51 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a  , 0, 0, 0);.  }.
18d52 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74    if( v==0 ) ret
18d53 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20 73 71 6c  urn 0;.  p = sql
18d54 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
18d55 2c 20 54 4b 5f 52 45 47 49 53 54 45 52 2c 20 30  , TK_REGISTER, 0
18d56 2c 20 30 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20  , 0, pToken);.  
18d57 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20  if( p==0 ){.    
18d58 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 4d 61  return 0;  /* Ma
18d59 6c 6c 6f 63 20 66 61 69 6c 65 64 20 2a 2f 0a 20  lloc failed */. 
18d5a 20 7d 0a 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d   }.  p->iTable =
18d5b 20 61 74 6f 69 28 28 63 68 61 72 2a 29 26 70 54   atoi((char*)&pT
18d5c 6f 6b 65 6e 2d 3e 7a 5b 31 5d 29 3b 0a 20 20 72  oken->z[1]);.  r
18d5d 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
18d5e 2a 20 4a 6f 69 6e 20 74 77 6f 20 65 78 70 72 65  * Join two expre
18d5f 73 73 69 6f 6e 73 20 75 73 69 6e 67 20 61 6e 20  ssions using an 
18d60 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 20 20 49  AND operator.  I
18d61 66 20 65 69 74 68 65 72 20 65 78 70 72 65 73 73  f either express
18d62 69 6f 6e 20 69 73 0a 2a 2a 20 4e 55 4c 4c 2c 20  ion is.** NULL, 
18d63 74 68 65 6e 20 6a 75 73 74 20 72 65 74 75 72 6e  then just return
18d64 20 74 68 65 20 6f 74 68 65 72 20 65 78 70 72 65   the other expre
18d65 73 73 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ssion..*/.SQLITE
18d66 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73  _PRIVATE Expr *s
18d67 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 73 71  qlite3ExprAnd(sq
18d68 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20  lite3 *db, Expr 
18d69 2a 70 4c 65 66 74 2c 20 45 78 70 72 20 2a 70 52  *pLeft, Expr *pR
18d6a 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 4c 65  ight){.  if( pLe
18d6b 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ft==0 ){.    ret
18d6c 75 72 6e 20 70 52 69 67 68 74 3b 0a 20 20 7d 65  urn pRight;.  }e
18d6d 6c 73 65 20 69 66 28 20 70 52 69 67 68 74 3d 3d  lse if( pRight==
18d6e 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
18d6f 70 4c 65 66 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a  pLeft;.  }else{.
18d70 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
18d71 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 41 4e  e3Expr(db, TK_AN
18d72 44 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74  D, pLeft, pRight
18d73 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  , 0);.  }.}../*.
18d74 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 2e  ** Set the Expr.
18d75 73 70 61 6e 20 66 69 65 6c 64 20 6f 66 20 74 68  span field of th
18d76 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 73 69  e given expressi
18d77 6f 6e 20 74 6f 20 73 70 61 6e 20 61 6c 6c 0a 2a  on to span all.*
18d78 2a 20 74 65 78 74 20 62 65 74 77 65 65 6e 20 74  * text between t
18d79 68 65 20 74 77 6f 20 67 69 76 65 6e 20 74 6f 6b  he two given tok
18d7a 65 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ens..*/.SQLITE_P
18d7b 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
18d7c 74 65 33 45 78 70 72 53 70 61 6e 28 45 78 70 72  te3ExprSpan(Expr
18d7d 20 2a 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 2a   *pExpr, Token *
18d7e 70 4c 65 66 74 2c 20 54 6f 6b 65 6e 20 2a 70 52  pLeft, Token *pR
18d7f 69 67 68 74 29 7b 0a 20 20 61 73 73 65 72 74 28  ight){.  assert(
18d80 20 70 52 69 67 68 74 21 3d 30 20 29 3b 0a 20 20   pRight!=0 );.  
18d81 61 73 73 65 72 74 28 20 70 4c 65 66 74 21 3d 30  assert( pLeft!=0
18d82 20 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 20   );.  if( pExpr 
18d83 26 26 20 70 52 69 67 68 74 2d 3e 7a 20 26 26 20  && pRight->z && 
18d84 70 4c 65 66 74 2d 3e 7a 20 29 7b 0a 20 20 20 20  pLeft->z ){.    
18d85 61 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e 64  assert( pLeft->d
18d86 79 6e 3d 3d 30 20 7c 7c 20 70 4c 65 66 74 2d 3e  yn==0 || pLeft->
18d87 7a 5b 70 4c 65 66 74 2d 3e 6e 5d 3d 3d 30 20 29  z[pLeft->n]==0 )
18d88 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d  ;.    if( pLeft-
18d89 3e 64 79 6e 3d 3d 30 20 26 26 20 70 52 69 67 68  >dyn==0 && pRigh
18d8a 74 2d 3e 64 79 6e 3d 3d 30 20 29 7b 0a 20 20 20  t->dyn==0 ){.   
18d8b 20 20 20 70 45 78 70 72 2d 3e 73 70 61 6e 2e 7a     pExpr->span.z
18d8c 20 3d 20 70 4c 65 66 74 2d 3e 7a 3b 0a 20 20 20   = pLeft->z;.   
18d8d 20 20 20 70 45 78 70 72 2d 3e 73 70 61 6e 2e 6e     pExpr->span.n
18d8e 20 3d 20 70 52 69 67 68 74 2d 3e 6e 20 2b 20 28   = pRight->n + (
18d8f 70 52 69 67 68 74 2d 3e 7a 20 2d 20 70 4c 65 66  pRight->z - pLef
18d90 74 2d 3e 7a 29 3b 0a 20 20 20 20 7d 65 6c 73 65  t->z);.    }else
18d91 7b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 73  {.      pExpr->s
18d92 70 61 6e 2e 7a 20 3d 20 30 3b 0a 20 20 20 20 7d  pan.z = 0;.    }
18d93 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  .  }.}../*.** Co
18d94 6e 73 74 72 75 63 74 20 61 20 6e 65 77 20 65 78  nstruct a new ex
18d95 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f  pression node fo
18d96 72 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74  r a function wit
18d97 68 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 61 72  h multiple.** ar
18d98 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49  guments..*/.SQLI
18d99 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
18d9a 2a 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63  *sqlite3ExprFunc
18d9b 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72  tion(Parse *pPar
18d9c 73 65 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c  se, ExprList *pL
18d9d 69 73 74 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b  ist, Token *pTok
18d9e 65 6e 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e 65  en){.  Expr *pNe
18d9f 77 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f  w;.  assert( pTo
18da0 6b 65 6e 20 29 3b 0a 20 20 70 4e 65 77 20 3d 20  ken );.  pNew = 
18da1 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
18da2 65 72 6f 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ero(pParse->db, 
18da3 73 69 7a 65 6f 66 28 45 78 70 72 29 20 29 3b 0a  sizeof(Expr) );.
18da4 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b    if( pNew==0 ){
18da5 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
18da6 4c 69 73 74 44 65 6c 65 74 65 28 70 4c 69 73 74  ListDelete(pList
18da7 29 3b 20 2f 2a 20 41 76 6f 69 64 20 6c 65 61 6b  ); /* Avoid leak
18da8 69 6e 67 20 6d 65 6d 6f 72 79 20 77 68 65 6e 20  ing memory when 
18da9 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20 2a 2f 0a  malloc fails */.
18daa 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
18dab 7d 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 54  }.  pNew->op = T
18dac 4b 5f 46 55 4e 43 54 49 4f 4e 3b 0a 20 20 70 4e  K_FUNCTION;.  pN
18dad 65 77 2d 3e 70 4c 69 73 74 20 3d 20 70 4c 69 73  ew->pList = pLis
18dae 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f  t;.  assert( pTo
18daf 6b 65 6e 2d 3e 64 79 6e 3d 3d 30 20 29 3b 0a 20  ken->dyn==0 );. 
18db0 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 20 3d 20 2a   pNew->token = *
18db1 70 54 6f 6b 65 6e 3b 0a 20 20 70 4e 65 77 2d 3e  pToken;.  pNew->
18db2 73 70 61 6e 20 3d 20 70 4e 65 77 2d 3e 74 6f 6b  span = pNew->tok
18db3 65 6e 3b 0a 0a 20 20 73 71 6c 69 74 65 33 45 78  en;..  sqlite3Ex
18db4 70 72 53 65 74 48 65 69 67 68 74 28 70 4e 65 77  prSetHeight(pNew
18db5 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77  );.  return pNew
18db6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 69 67  ;.}../*.** Assig
18db7 6e 20 61 20 76 61 72 69 61 62 6c 65 20 6e 75 6d  n a variable num
18db8 62 65 72 20 74 6f 20 61 6e 20 65 78 70 72 65 73  ber to an expres
18db9 73 69 6f 6e 20 74 68 61 74 20 65 6e 63 6f 64 65  sion that encode
18dba 73 20 61 20 77 69 6c 64 63 61 72 64 0a 2a 2a 20  s a wildcard.** 
18dbb 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  in the original 
18dbc 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  SQL statement.  
18dbd 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73  .**.** Wildcards
18dbe 20 63 6f 6e 73 69 73 74 69 6e 67 20 6f 66 20 61   consisting of a
18dbf 20 73 69 6e 67 6c 65 20 22 3f 22 20 61 72 65 20   single "?" are 
18dc0 61 73 73 69 67 6e 65 64 20 74 68 65 20 6e 65 78  assigned the nex
18dc1 74 20 73 65 71 75 65 6e 74 69 61 6c 0a 2a 2a 20  t sequential.** 
18dc2 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 2e  variable number.
18dc3 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73  .**.** Wildcards
18dc4 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 6e   of the form "?n
18dc5 6e 6e 22 20 61 72 65 20 61 73 73 69 67 6e 65 64  nn" are assigned
18dc6 20 74 68 65 20 6e 75 6d 62 65 72 20 22 6e 6e 6e   the number "nnn
18dc7 22 2e 20 20 57 65 20 6d 61 6b 65 0a 2a 2a 20 73  ".  We make.** s
18dc8 75 72 65 20 22 6e 6e 6e 22 20 69 73 20 6e 6f 74  ure "nnn" is not
18dc9 20 74 6f 6f 20 62 65 20 74 6f 20 61 76 6f 69 64   too be to avoid
18dca 20 61 20 64 65 6e 69 61 6c 20 6f 66 20 73 65 72   a denial of ser
18dcb 76 69 63 65 20 61 74 74 61 63 6b 20 77 68 65 6e  vice attack when
18dcc 0a 2a 2a 20 74 68 65 20 53 51 4c 20 73 74 61 74  .** the SQL stat
18dcd 65 6d 65 6e 74 20 63 6f 6d 65 73 20 66 72 6f 6d  ement comes from
18dce 20 61 6e 20 65 78 74 65 72 6e 61 6c 20 73 6f 75   an external sou
18dcf 72 63 65 2e 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63  rce..**.** Wildc
18dd0 61 72 64 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  ards of the form
18dd1 20 22 3a 61 61 61 22 20 6f 72 20 22 24 61 61 61   ":aaa" or "$aaa
18dd2 22 20 61 72 65 20 61 73 73 69 67 6e 65 64 20 74  " are assigned t
18dd3 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 0a 2a  he same number.*
18dd4 2a 20 61 73 20 74 68 65 20 70 72 65 76 69 6f 75  * as the previou
18dd5 73 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  s instance of th
18dd6 65 20 73 61 6d 65 20 77 69 6c 64 63 61 72 64 2e  e same wildcard.
18dd7 20 20 4f 72 20 69 66 20 74 68 69 73 20 69 73 20    Or if this is 
18dd8 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e 73  the first.** ins
18dd9 74 61 6e 63 65 20 6f 66 20 74 68 65 20 77 69 6c  tance of the wil
18dda 64 63 61 72 64 2c 20 74 68 65 20 6e 65 78 74 20  dcard, the next 
18ddb 73 65 71 75 65 6e 69 61 6c 20 76 61 72 69 61 62  sequenial variab
18ddc 6c 65 20 6e 75 6d 62 65 72 20 69 73 0a 2a 2a 20  le number is.** 
18ddd 61 73 73 69 67 6e 65 64 2e 0a 2a 2f 0a 53 51 4c  assigned..*/.SQL
18dde 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
18ddf 20 73 71 6c 69 74 65 33 45 78 70 72 41 73 73 69   sqlite3ExprAssi
18de0 67 6e 56 61 72 4e 75 6d 62 65 72 28 50 61 72 73  gnVarNumber(Pars
18de1 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
18de2 2a 70 45 78 70 72 29 7b 0a 20 20 54 6f 6b 65 6e  *pExpr){.  Token
18de3 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20 73 71 6c 69   *pToken;.  sqli
18de4 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
18de5 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 70 45 78  ->db;..  if( pEx
18de6 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  pr==0 ) return;.
18de7 20 20 70 54 6f 6b 65 6e 20 3d 20 26 70 45 78 70    pToken = &pExp
18de8 72 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 61 73 73 65  r->token;.  asse
18de9 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 6e 3e 3d 31  rt( pToken->n>=1
18dea 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54   );.  assert( pT
18deb 6f 6b 65 6e 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20  oken->z!=0 );.  
18dec 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 2d 3e  assert( pToken->
18ded 7a 5b 30 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28  z[0]!=0 );.  if(
18dee 20 70 54 6f 6b 65 6e 2d 3e 6e 3d 3d 31 20 29 7b   pToken->n==1 ){
18def 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64  .    /* Wildcard
18df0 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 22   of the form "?"
18df1 2e 20 20 41 73 73 69 67 6e 20 74 68 65 20 6e 65  .  Assign the ne
18df2 78 74 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62  xt variable numb
18df3 65 72 20 2a 2f 0a 20 20 20 20 70 45 78 70 72 2d  er */.    pExpr-
18df4 3e 69 54 61 62 6c 65 20 3d 20 2b 2b 70 50 61 72  >iTable = ++pPar
18df5 73 65 2d 3e 6e 56 61 72 3b 0a 20 20 7d 65 6c 73  se->nVar;.  }els
18df6 65 20 69 66 28 20 70 54 6f 6b 65 6e 2d 3e 7a 5b  e if( pToken->z[
18df7 30 5d 3d 3d 27 3f 27 20 29 7b 0a 20 20 20 20 2f  0]=='?' ){.    /
18df8 2a 20 57 69 6c 64 63 61 72 64 20 6f 66 20 74 68  * Wildcard of th
18df9 65 20 66 6f 72 6d 20 22 3f 6e 6e 6e 22 2e 20 20  e form "?nnn".  
18dfa 43 6f 6e 76 65 72 74 20 22 6e 6e 6e 22 20 74 6f  Convert "nnn" to
18dfb 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 0a   an integer and.
18dfc 20 20 20 20 2a 2a 20 75 73 65 20 69 74 20 61 73      ** use it as
18dfd 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 6e 75   the variable nu
18dfe 6d 62 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  mber */.    int 
18dff 69 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 54  i;.    pExpr->iT
18e00 61 62 6c 65 20 3d 20 69 20 3d 20 61 74 6f 69 28  able = i = atoi(
18e01 28 63 68 61 72 2a 29 26 70 54 6f 6b 65 6e 2d 3e  (char*)&pToken->
18e02 7a 5b 31 5d 29 3b 0a 20 20 20 20 74 65 73 74 63  z[1]);.    testc
18e03 61 73 65 28 20 69 3d 3d 30 20 29 3b 0a 20 20 20  ase( i==0 );.   
18e04 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 20   testcase( i==1 
18e05 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
18e06 20 69 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53   i==db->aLimit[S
18e07 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49  QLITE_LIMIT_VARI
18e08 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 2d 31 20 29  ABLE_NUMBER]-1 )
18e09 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
18e0a 69 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  i==db->aLimit[SQ
18e0b 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41  LITE_LIMIT_VARIA
18e0c 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 3b 0a 20  BLE_NUMBER] );. 
18e0d 20 20 20 69 66 28 20 69 3c 31 20 7c 7c 20 69 3e     if( i<1 || i>
18e0e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
18e0f 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45  E_LIMIT_VARIABLE
18e10 5f 4e 55 4d 42 45 52 5d 20 29 7b 0a 20 20 20 20  _NUMBER] ){.    
18e11 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
18e12 67 28 70 50 61 72 73 65 2c 20 22 76 61 72 69 61  g(pParse, "varia
18e13 62 6c 65 20 6e 75 6d 62 65 72 20 6d 75 73 74 20  ble number must 
18e14 62 65 20 62 65 74 77 65 65 6e 20 3f 31 20 61 6e  be between ?1 an
18e15 64 20 3f 25 64 22 2c 0a 20 20 20 20 20 20 20 20  d ?%d",.        
18e16 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c    db->aLimit[SQL
18e17 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42  ITE_LIMIT_VARIAB
18e18 4c 45 5f 4e 55 4d 42 45 52 5d 29 3b 0a 20 20 20  LE_NUMBER]);.   
18e19 20 7d 0a 20 20 20 20 69 66 28 20 69 3e 70 50 61   }.    if( i>pPa
18e1a 72 73 65 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20  rse->nVar ){.   
18e1b 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 20     pParse->nVar 
18e1c 3d 20 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  = i;.    }.  }el
18e1d 73 65 7b 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63  se{.    /* Wildc
18e1e 61 72 64 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  ards of the form
18e1f 20 22 3a 61 61 61 22 20 6f 72 20 22 24 61 61 61   ":aaa" or "$aaa
18e20 22 2e 20 20 52 65 75 73 65 20 74 68 65 20 73 61  ".  Reuse the sa
18e21 6d 65 20 76 61 72 69 61 62 6c 65 0a 20 20 20 20  me variable.    
18e22 2a 2a 20 6e 75 6d 62 65 72 20 61 73 20 74 68 65  ** number as the
18e23 20 70 72 69 6f 72 20 61 70 70 65 61 72 61 6e 63   prior appearanc
18e24 65 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6e 61  e of the same na
18e25 6d 65 2c 20 6f 72 20 69 66 20 74 68 65 20 6e 61  me, or if the na
18e26 6d 65 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e 65  me.    ** has ne
18e27 76 65 72 20 61 70 70 65 61 72 65 64 20 62 65 66  ver appeared bef
18e28 6f 72 65 2c 20 72 65 75 73 65 20 74 68 65 20 73  ore, reuse the s
18e29 61 6d 65 20 76 61 72 69 61 62 6c 65 20 6e 75 6d  ame variable num
18e2a 62 65 72 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ber.    */.    i
18e2b 6e 74 20 69 2c 20 6e 3b 0a 20 20 20 20 6e 20 3d  nt i, n;.    n =
18e2c 20 70 54 6f 6b 65 6e 2d 3e 6e 3b 0a 20 20 20 20   pToken->n;.    
18e2d 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73  for(i=0; i<pPars
18e2e 65 2d 3e 6e 56 61 72 45 78 70 72 3b 20 69 2b 2b  e->nVarExpr; i++
18e2f 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  ){.      Expr *p
18e30 45 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70 45  E;.      if( (pE
18e31 20 3d 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72   = pParse->apVar
18e32 45 78 70 72 5b 69 5d 29 21 3d 30 0a 20 20 20 20  Expr[i])!=0.    
18e33 20 20 20 20 20 20 26 26 20 70 45 2d 3e 74 6f 6b        && pE->tok
18e34 65 6e 2e 6e 3d 3d 6e 0a 20 20 20 20 20 20 20 20  en.n==n.        
18e35 20 20 26 26 20 6d 65 6d 63 6d 70 28 70 45 2d 3e    && memcmp(pE->
18e36 74 6f 6b 65 6e 2e 7a 2c 20 70 54 6f 6b 65 6e 2d  token.z, pToken-
18e37 3e 7a 2c 20 6e 29 3d 3d 30 20 29 7b 0a 20 20 20  >z, n)==0 ){.   
18e38 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62       pExpr->iTab
18e39 6c 65 20 3d 20 70 45 2d 3e 69 54 61 62 6c 65 3b  le = pE->iTable;
18e3a 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
18e3b 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
18e3c 20 20 69 66 28 20 69 3e 3d 70 50 61 72 73 65 2d    if( i>=pParse-
18e3d 3e 6e 56 61 72 45 78 70 72 20 29 7b 0a 20 20 20  >nVarExpr ){.   
18e3e 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65     pExpr->iTable
18e3f 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61   = ++pParse->nVa
18e40 72 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61  r;.      if( pPa
18e41 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 3e 3d 70  rse->nVarExpr>=p
18e42 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41  Parse->nVarExprA
18e43 6c 6c 6f 63 2d 31 20 29 7b 0a 20 20 20 20 20 20  lloc-1 ){.      
18e44 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78    pParse->nVarEx
18e45 70 72 41 6c 6c 6f 63 20 2b 3d 20 70 50 61 72 73  prAlloc += pPars
18e46 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63  e->nVarExprAlloc
18e47 20 2b 20 31 30 3b 0a 20 20 20 20 20 20 20 20 70   + 10;.        p
18e48 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72  Parse->apVarExpr
18e49 20 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73   =.            s
18e4a 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f  qlite3DbReallocO
18e4b 72 46 72 65 65 28 0a 20 20 20 20 20 20 20 20 20  rFree(.         
18e4c 20 20 20 20 20 64 62 2c 0a 20 20 20 20 20 20 20       db,.       
18e4d 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61         pParse->a
18e4e 70 56 61 72 45 78 70 72 2c 0a 20 20 20 20 20 20  pVarExpr,.      
18e4f 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e          pParse->
18e50 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 2a 73 69  nVarExprAlloc*si
18e51 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61 70 56  zeof(pParse->apV
18e52 61 72 45 78 70 72 5b 30 5d 29 0a 20 20 20 20 20  arExpr[0]).     
18e53 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20         );.      
18e54 7d 0a 20 20 20 20 20 20 69 66 28 20 21 64 62 2d  }.      if( !db-
18e55 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
18e56 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
18e57 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78   pParse->apVarEx
18e58 70 72 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  pr!=0 );.       
18e59 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78   pParse->apVarEx
18e5a 70 72 5b 70 50 61 72 73 65 2d 3e 6e 56 61 72 45  pr[pParse->nVarE
18e5b 78 70 72 2b 2b 5d 20 3d 20 70 45 78 70 72 3b 0a  xpr++] = pExpr;.
18e5c 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
18e5d 7d 20 0a 20 20 69 66 28 20 21 70 50 61 72 73 65  } .  if( !pParse
18e5e 2d 3e 6e 45 72 72 20 26 26 20 70 50 61 72 73 65  ->nErr && pParse
18e5f 2d 3e 6e 56 61 72 3e 64 62 2d 3e 61 4c 69 6d 69  ->nVar>db->aLimi
18e60 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56  t[SQLITE_LIMIT_V
18e61 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20  ARIABLE_NUMBER] 
18e62 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
18e63 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
18e64 74 6f 6f 20 6d 61 6e 79 20 53 51 4c 20 76 61 72  too many SQL var
18e65 69 61 62 6c 65 73 22 29 3b 0a 20 20 7d 0a 7d 0a  iables");.  }.}.
18e66 0a 2f 2a 0a 2a 2a 20 52 65 63 75 72 73 69 76 65  ./*.** Recursive
18e67 6c 79 20 64 65 6c 65 74 65 20 61 6e 20 65 78 70  ly delete an exp
18e68 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 0a 2a 2f  ression tree..*/
18e69 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
18e6a 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
18e6b 44 65 6c 65 74 65 28 45 78 70 72 20 2a 70 29 7b  Delete(Expr *p){
18e6c 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
18e6d 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 73  turn;.  if( p->s
18e6e 70 61 6e 2e 64 79 6e 20 29 20 73 71 6c 69 74 65  pan.dyn ) sqlite
18e6f 33 5f 66 72 65 65 28 28 63 68 61 72 2a 29 70 2d  3_free((char*)p-
18e70 3e 73 70 61 6e 2e 7a 29 3b 0a 20 20 69 66 28 20  >span.z);.  if( 
18e71 70 2d 3e 74 6f 6b 65 6e 2e 64 79 6e 20 29 20 73  p->token.dyn ) s
18e72 71 6c 69 74 65 33 5f 66 72 65 65 28 28 63 68 61  qlite3_free((cha
18e73 72 2a 29 70 2d 3e 74 6f 6b 65 6e 2e 7a 29 3b 0a  r*)p->token.z);.
18e74 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
18e75 65 74 65 28 70 2d 3e 70 4c 65 66 74 29 3b 0a 20  ete(p->pLeft);. 
18e76 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
18e77 74 65 28 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20  te(p->pRight);. 
18e78 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
18e79 44 65 6c 65 74 65 28 70 2d 3e 70 4c 69 73 74 29  Delete(p->pList)
18e7a 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ;.  sqlite3Selec
18e7b 74 44 65 6c 65 74 65 28 70 2d 3e 70 53 65 6c 65  tDelete(p->pSele
18e7c 63 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ct);.  sqlite3_f
18e7d 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ree(p);.}../*.**
18e7e 20 54 68 65 20 45 78 70 72 2e 74 6f 6b 65 6e 20   The Expr.token 
18e7f 66 69 65 6c 64 20 6d 69 67 68 74 20 62 65 20 61  field might be a
18e80 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 20   string literal 
18e81 74 68 61 74 20 69 73 20 71 75 6f 74 65 64 2e 0a  that is quoted..
18e82 2a 2a 20 49 66 20 73 6f 2c 20 72 65 6d 6f 76 65  ** If so, remove
18e83 20 74 68 65 20 71 75 6f 74 61 74 69 6f 6e 20 6d   the quotation m
18e84 61 72 6b 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  arks..*/.SQLITE_
18e85 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
18e86 69 74 65 33 44 65 71 75 6f 74 65 45 78 70 72 28  ite3DequoteExpr(
18e87 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70  sqlite3 *db, Exp
18e88 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 45 78 70  r *p){.  if( Exp
18e89 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28  rHasAnyProperty(
18e8a 70 2c 20 45 50 5f 44 65 71 75 6f 74 65 64 29 20  p, EP_Dequoted) 
18e8b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
18e8c 20 7d 0a 20 20 45 78 70 72 53 65 74 50 72 6f 70   }.  ExprSetProp
18e8d 65 72 74 79 28 70 2c 20 45 50 5f 44 65 71 75 6f  erty(p, EP_Dequo
18e8e 74 65 64 29 3b 0a 20 20 69 66 28 20 70 2d 3e 74  ted);.  if( p->t
18e8f 6f 6b 65 6e 2e 64 79 6e 3d 3d 30 20 29 7b 0a 20  oken.dyn==0 ){. 
18e90 20 20 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43     sqlite3TokenC
18e91 6f 70 79 28 64 62 2c 20 26 70 2d 3e 74 6f 6b 65  opy(db, &p->toke
18e92 6e 2c 20 26 70 2d 3e 74 6f 6b 65 6e 29 3b 0a 20  n, &p->token);. 
18e93 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 65 71 75   }.  sqlite3Dequ
18e94 6f 74 65 28 28 63 68 61 72 2a 29 70 2d 3e 74 6f  ote((char*)p->to
18e95 6b 65 6e 2e 7a 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  ken.z);.}.../*.*
18e96 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
18e97 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e 65  group of routine
18e98 73 20 6d 61 6b 65 20 64 65 65 70 20 63 6f 70 69  s make deep copi
18e99 65 73 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e  es of expression
18e9a 73 2c 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  s,.** expression
18e9b 20 6c 69 73 74 73 2c 20 49 44 20 6c 69 73 74 73   lists, ID lists
18e9c 2c 20 61 6e 64 20 73 65 6c 65 63 74 20 73 74 61  , and select sta
18e9d 74 65 6d 65 6e 74 73 2e 20 20 54 68 65 20 63 6f  tements.  The co
18e9e 70 69 65 73 20 63 61 6e 0a 2a 2a 20 62 65 20 64  pies can.** be d
18e9f 65 6c 65 74 65 64 20 28 62 79 20 62 65 69 6e 67  eleted (by being
18ea0 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 69 72   passed to their
18ea1 20 72 65 73 70 65 63 74 69 76 65 20 2e 2e 2e 44   respective ...D
18ea2 65 6c 65 74 65 28 29 20 72 6f 75 74 69 6e 65 73  elete() routines
18ea3 29 0a 2a 2a 20 77 69 74 68 6f 75 74 20 65 66 66  ).** without eff
18ea4 65 63 74 69 6e 67 20 74 68 65 20 6f 72 69 67 69  ecting the origi
18ea5 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  nals..**.** The 
18ea6 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2c  expression list,
18ea7 20 49 44 2c 20 61 6e 64 20 73 6f 75 72 63 65 20   ID, and source 
18ea8 6c 69 73 74 73 20 72 65 74 75 72 6e 20 62 79 20  lists return by 
18ea9 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
18eaa 75 70 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33  up(),.** sqlite3
18eab 49 64 4c 69 73 74 44 75 70 28 29 2c 20 61 6e 64  IdListDup(), and
18eac 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44   sqlite3SrcListD
18ead 75 70 28 29 20 63 61 6e 20 6e 6f 74 20 62 65 20  up() can not be 
18eae 66 75 72 74 68 65 72 20 65 78 70 61 6e 64 65 64  further expanded
18eaf 20 0a 2a 2a 20 62 79 20 73 75 62 73 65 71 75 65   .** by subseque
18eb0 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69  nt calls to sqli
18eb1 74 65 2a 4c 69 73 74 41 70 70 65 6e 64 28 29 20  te*ListAppend() 
18eb2 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20  routines..**.** 
18eb3 41 6e 79 20 74 61 62 6c 65 73 20 74 68 61 74 20  Any tables that 
18eb4 74 68 65 20 53 72 63 4c 69 73 74 20 6d 69 67 68  the SrcList migh
18eb5 74 20 70 6f 69 6e 74 20 74 6f 20 61 72 65 20 6e  t point to are n
18eb6 6f 74 20 64 75 70 6c 69 63 61 74 65 64 2e 0a 2a  ot duplicated..*
18eb7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18eb8 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78   Expr *sqlite3Ex
18eb9 70 72 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64  prDup(sqlite3 *d
18eba 62 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 45  b, Expr *p){.  E
18ebb 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 69 66 28  xpr *pNew;.  if(
18ebc 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   p==0 ) return 0
18ebd 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74  ;.  pNew = sqlit
18ebe 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
18ebf 2c 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a  , sizeof(*p) );.
18ec0 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20    if( pNew==0 ) 
18ec1 72 65 74 75 72 6e 20 30 3b 0a 20 20 6d 65 6d 63  return 0;.  memc
18ec2 70 79 28 70 4e 65 77 2c 20 70 2c 20 73 69 7a 65  py(pNew, p, size
18ec3 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20 20 69 66  of(*pNew));.  if
18ec4 28 20 70 2d 3e 74 6f 6b 65 6e 2e 7a 21 3d 30 20  ( p->token.z!=0 
18ec5 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 74 6f 6b  ){.    pNew->tok
18ec6 65 6e 2e 7a 20 3d 20 28 75 38 2a 29 73 71 6c 69  en.z = (u8*)sqli
18ec7 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c  te3DbStrNDup(db,
18ec8 20 28 63 68 61 72 2a 29 70 2d 3e 74 6f 6b 65 6e   (char*)p->token
18ec9 2e 7a 2c 20 70 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b  .z, p->token.n);
18eca 0a 20 20 20 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e  .    pNew->token
18ecb 2e 64 79 6e 20 3d 20 31 3b 0a 20 20 7d 65 6c 73  .dyn = 1;.  }els
18ecc 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
18ecd 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 7a 3d 3d 30 20  New->token.z==0 
18ece 29 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 73  );.  }.  pNew->s
18ecf 70 61 6e 2e 7a 20 3d 20 30 3b 0a 20 20 70 4e 65  pan.z = 0;.  pNe
18ed0 77 2d 3e 70 4c 65 66 74 20 3d 20 73 71 6c 69 74  w->pLeft = sqlit
18ed1 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d  e3ExprDup(db, p-
18ed2 3e 70 4c 65 66 74 29 3b 0a 20 20 70 4e 65 77 2d  >pLeft);.  pNew-
18ed3 3e 70 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65  >pRight = sqlite
18ed4 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e  3ExprDup(db, p->
18ed5 70 52 69 67 68 74 29 3b 0a 20 20 70 4e 65 77 2d  pRight);.  pNew-
18ed6 3e 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33  >pList = sqlite3
18ed7 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20  ExprListDup(db, 
18ed8 70 2d 3e 70 4c 69 73 74 29 3b 0a 20 20 70 4e 65  p->pList);.  pNe
18ed9 77 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c  w->pSelect = sql
18eda 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62  ite3SelectDup(db
18edb 2c 20 70 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20  , p->pSelect);. 
18edc 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a   return pNew;.}.
18edd 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
18ede 6f 69 64 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e  oid sqlite3Token
18edf 43 6f 70 79 28 73 71 6c 69 74 65 33 20 2a 64 62  Copy(sqlite3 *db
18ee0 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 2c 20 54 6f  , Token *pTo, To
18ee1 6b 65 6e 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69  ken *pFrom){.  i
18ee2 66 28 20 70 54 6f 2d 3e 64 79 6e 20 29 20 73 71  f( pTo->dyn ) sq
18ee3 6c 69 74 65 33 5f 66 72 65 65 28 28 63 68 61 72  lite3_free((char
18ee4 2a 29 70 54 6f 2d 3e 7a 29 3b 0a 20 20 69 66 28  *)pTo->z);.  if(
18ee5 20 70 46 72 6f 6d 2d 3e 7a 20 29 7b 0a 20 20 20   pFrom->z ){.   
18ee6 20 70 54 6f 2d 3e 6e 20 3d 20 70 46 72 6f 6d 2d   pTo->n = pFrom-
18ee7 3e 6e 3b 0a 20 20 20 20 70 54 6f 2d 3e 7a 20 3d  >n;.    pTo->z =
18ee8 20 28 75 38 2a 29 73 71 6c 69 74 65 33 44 62 53   (u8*)sqlite3DbS
18ee9 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72  trNDup(db, (char
18eea 2a 29 70 46 72 6f 6d 2d 3e 7a 2c 20 70 46 72 6f  *)pFrom->z, pFro
18eeb 6d 2d 3e 6e 29 3b 0a 20 20 20 20 70 54 6f 2d 3e  m->n);.    pTo->
18eec 64 79 6e 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  dyn = 1;.  }else
18eed 7b 0a 20 20 20 20 70 54 6f 2d 3e 7a 20 3d 20 30  {.    pTo->z = 0
18eee 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50  ;.  }.}.SQLITE_P
18eef 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 20  RIVATE ExprList 
18ef0 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74  *sqlite3ExprList
18ef1 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  Dup(sqlite3 *db,
18ef2 20 45 78 70 72 4c 69 73 74 20 2a 70 29 7b 0a 20   ExprList *p){. 
18ef3 20 45 78 70 72 4c 69 73 74 20 2a 70 4e 65 77 3b   ExprList *pNew;
18ef4 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69  .  struct ExprLi
18ef5 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 2c 20  st_item *pItem, 
18ef6 2a 70 4f 6c 64 49 74 65 6d 3b 0a 20 20 69 6e 74  *pOldItem;.  int
18ef7 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29   i;.  if( p==0 )
18ef8 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65   return 0;.  pNe
18ef9 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  w = sqlite3DbMal
18efa 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f  locRaw(db, sizeo
18efb 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20 69 66  f(*pNew) );.  if
18efc 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75  ( pNew==0 ) retu
18efd 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 69 45  rn 0;.  pNew->iE
18efe 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 70 4e  Cursor = 0;.  pN
18eff 65 77 2d 3e 6e 45 78 70 72 20 3d 20 70 4e 65 77  ew->nExpr = pNew
18f00 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 45  ->nAlloc = p->nE
18f01 78 70 72 3b 0a 20 20 70 4e 65 77 2d 3e 61 20 3d  xpr;.  pNew->a =
18f02 20 70 49 74 65 6d 20 3d 20 73 71 6c 69 74 65 33   pItem = sqlite3
18f03 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
18f04 20 70 2d 3e 6e 45 78 70 72 2a 73 69 7a 65 6f 66   p->nExpr*sizeof
18f05 28 70 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 69  (p->a[0]) );.  i
18f06 66 28 20 70 49 74 65 6d 3d 3d 30 20 29 7b 0a 20  f( pItem==0 ){. 
18f07 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
18f08 70 4e 65 77 29 3b 0a 20 20 20 20 72 65 74 75 72  pNew);.    retur
18f09 6e 20 30 3b 0a 20 20 7d 20 0a 20 20 70 4f 6c 64  n 0;.  } .  pOld
18f0a 49 74 65 6d 20 3d 20 70 2d 3e 61 3b 0a 20 20 66  Item = p->a;.  f
18f0b 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78  or(i=0; i<p->nEx
18f0c 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b  pr; i++, pItem++
18f0d 2c 20 70 4f 6c 64 49 74 65 6d 2b 2b 29 7b 0a 20  , pOldItem++){. 
18f0e 20 20 20 45 78 70 72 20 2a 70 4e 65 77 45 78 70     Expr *pNewExp
18f0f 72 2c 20 2a 70 4f 6c 64 45 78 70 72 3b 0a 20 20  r, *pOldExpr;.  
18f10 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d    pItem->pExpr =
18f11 20 70 4e 65 77 45 78 70 72 20 3d 20 73 71 6c 69   pNewExpr = sqli
18f12 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
18f13 4f 6c 64 45 78 70 72 20 3d 20 70 4f 6c 64 49 74  OldExpr = pOldIt
18f14 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20  em->pExpr);.    
18f15 69 66 28 20 70 4f 6c 64 45 78 70 72 2d 3e 73 70  if( pOldExpr->sp
18f16 61 6e 2e 7a 21 3d 30 20 26 26 20 70 4e 65 77 45  an.z!=0 && pNewE
18f17 78 70 72 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  xpr ){.      /* 
18f18 41 6c 77 61 79 73 20 6d 61 6b 65 20 61 20 63 6f  Always make a co
18f19 70 79 20 6f 66 20 74 68 65 20 73 70 61 6e 20 66  py of the span f
18f1a 6f 72 20 74 6f 70 2d 6c 65 76 65 6c 20 65 78 70  or top-level exp
18f1b 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 0a  ressions in the.
18f1c 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73        ** express
18f1d 69 6f 6e 20 6c 69 73 74 2e 20 20 54 68 65 20 6c  ion list.  The l
18f1e 6f 67 69 63 20 69 6e 20 53 45 4c 45 43 54 20 70  ogic in SELECT p
18f1f 72 6f 63 65 73 73 69 6e 67 20 74 68 61 74 20 64  rocessing that d
18f20 65 74 65 72 6d 69 6e 65 73 0a 20 20 20 20 20 20  etermines.      
18f21 2a 2a 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20  ** the names of 
18f22 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72  columns in the r
18f23 65 73 75 6c 74 20 73 65 74 20 6e 65 65 64 73 20  esult set needs 
18f24 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  this information
18f25 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
18f26 33 54 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26  3TokenCopy(db, &
18f27 70 4e 65 77 45 78 70 72 2d 3e 73 70 61 6e 2c 20  pNewExpr->span, 
18f28 26 70 4f 6c 64 45 78 70 72 2d 3e 73 70 61 6e 29  &pOldExpr->span)
18f29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
18f2a 72 74 28 20 70 4e 65 77 45 78 70 72 3d 3d 30 20  rt( pNewExpr==0 
18f2b 7c 7c 20 70 4e 65 77 45 78 70 72 2d 3e 73 70 61  || pNewExpr->spa
18f2c 6e 2e 7a 21 3d 30 20 0a 20 20 20 20 20 20 20 20  n.z!=0 .        
18f2d 20 20 20 20 7c 7c 20 70 4f 6c 64 45 78 70 72 2d      || pOldExpr-
18f2e 3e 73 70 61 6e 2e 7a 3d 3d 30 0a 20 20 20 20 20  >span.z==0.     
18f2f 20 20 20 20 20 20 20 7c 7c 20 64 62 2d 3e 6d 61         || db->ma
18f30 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
18f31 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d    pItem->zName =
18f32 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
18f33 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a  (db, pOldItem->z
18f34 4e 61 6d 65 29 3b 0a 20 20 20 20 70 49 74 65 6d  Name);.    pItem
18f35 2d 3e 73 6f 72 74 4f 72 64 65 72 20 3d 20 70 4f  ->sortOrder = pO
18f36 6c 64 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65  ldItem->sortOrde
18f37 72 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 73  r;.    pItem->is
18f38 41 67 67 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e  Agg = pOldItem->
18f39 69 73 41 67 67 3b 0a 20 20 20 20 70 49 74 65 6d  isAgg;.    pItem
18f3a 2d 3e 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d 0a  ->done = 0;.  }.
18f3b 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d    return pNew;.}
18f3c 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f  ../*.** If curso
18f3d 72 73 2c 20 74 72 69 67 67 65 72 73 2c 20 76 69  rs, triggers, vi
18f3e 65 77 73 20 61 6e 64 20 73 75 62 71 75 65 72 69  ews and subqueri
18f3f 65 73 20 61 72 65 20 61 6c 6c 20 6f 6d 69 74 74  es are all omitt
18f40 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 62  ed from.** the b
18f41 75 69 6c 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 20  uild, then none 
18f42 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
18f43 20 72 6f 75 74 69 6e 65 73 2c 20 65 78 63 65 70   routines, excep
18f44 74 20 66 6f 72 20 0a 2a 2a 20 73 71 6c 69 74 65  t for .** sqlite
18f45 33 53 65 6c 65 63 74 44 75 70 28 29 2c 20 63 61  3SelectDup(), ca
18f46 6e 20 62 65 20 63 61 6c 6c 65 64 2e 20 73 71 6c  n be called. sql
18f47 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 29 20  ite3SelectDup() 
18f48 69 73 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20  is sometimes.** 
18f49 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 4e 55  called with a NU
18f4a 4c 4c 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a  LL argument..*/.
18f4b 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
18f4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c  ITE_OMIT_VIEW) |
18f4d 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  | !defined(SQLIT
18f4e 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20  E_OMIT_TRIGGER) 
18f4f 5c 0a 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53  \. || !defined(S
18f50 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
18f51 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 49 56  ERY).SQLITE_PRIV
18f52 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c  ATE SrcList *sql
18f53 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 73  ite3SrcListDup(s
18f54 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 72 63 4c  qlite3 *db, SrcL
18f55 69 73 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69  ist *p){.  SrcLi
18f56 73 74 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20  st *pNew;.  int 
18f57 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a  i;.  int nByte;.
18f58 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74    if( p==0 ) ret
18f59 75 72 6e 20 30 3b 0a 20 20 6e 42 79 74 65 20 3d  urn 0;.  nByte =
18f5a 20 73 69 7a 65 6f 66 28 2a 70 29 20 2b 20 28 70   sizeof(*p) + (p
18f5b 2d 3e 6e 53 72 63 3e 30 20 3f 20 73 69 7a 65 6f  ->nSrc>0 ? sizeo
18f5c 66 28 70 2d 3e 61 5b 30 5d 29 20 2a 20 28 70 2d  f(p->a[0]) * (p-
18f5d 3e 6e 53 72 63 2d 31 29 20 3a 20 30 29 3b 0a 20  >nSrc-1) : 0);. 
18f5e 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   pNew = sqlite3D
18f5f 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e  bMallocRaw(db, n
18f60 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 70 4e  Byte );.  if( pN
18f61 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  ew==0 ) return 0
18f62 3b 0a 20 20 70 4e 65 77 2d 3e 6e 53 72 63 20 3d  ;.  pNew->nSrc =
18f63 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20   pNew->nAlloc = 
18f64 70 2d 3e 6e 53 72 63 3b 0a 20 20 66 6f 72 28 69  p->nSrc;.  for(i
18f65 3d 30 3b 20 69 3c 70 2d 3e 6e 53 72 63 3b 20 69  =0; i<p->nSrc; i
18f66 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  ++){.    struct 
18f67 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4e  SrcList_item *pN
18f68 65 77 49 74 65 6d 20 3d 20 26 70 4e 65 77 2d 3e  ewItem = &pNew->
18f69 61 5b 69 5d 3b 0a 20 20 20 20 73 74 72 75 63 74  a[i];.    struct
18f6a 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
18f6b 4f 6c 64 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b  OldItem = &p->a[
18f6c 69 5d 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70  i];.    Table *p
18f6d 54 61 62 3b 0a 20 20 20 20 70 4e 65 77 49 74 65  Tab;.    pNewIte
18f6e 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d 20 73  m->zDatabase = s
18f6f 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
18f70 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 44 61  b, pOldItem->zDa
18f71 74 61 62 61 73 65 29 3b 0a 20 20 20 20 70 4e 65  tabase);.    pNe
18f72 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73  wItem->zName = s
18f73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
18f74 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61  b, pOldItem->zNa
18f75 6d 65 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65  me);.    pNewIte
18f76 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20 73 71 6c 69  m->zAlias = sqli
18f77 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
18f78 70 4f 6c 64 49 74 65 6d 2d 3e 7a 41 6c 69 61 73  pOldItem->zAlias
18f79 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d  );.    pNewItem-
18f7a 3e 6a 6f 69 6e 74 79 70 65 20 3d 20 70 4f 6c 64  >jointype = pOld
18f7b 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 3b 0a  Item->jointype;.
18f7c 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69 43      pNewItem->iC
18f7d 75 72 73 6f 72 20 3d 20 70 4f 6c 64 49 74 65 6d  ursor = pOldItem
18f7e 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 70  ->iCursor;.    p
18f7f 4e 65 77 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c  NewItem->isPopul
18f80 61 74 65 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d  ated = pOldItem-
18f81 3e 69 73 50 6f 70 75 6c 61 74 65 64 3b 0a 20 20  >isPopulated;.  
18f82 20 20 70 54 61 62 20 3d 20 70 4e 65 77 49 74 65    pTab = pNewIte
18f83 6d 2d 3e 70 54 61 62 20 3d 20 70 4f 6c 64 49 74  m->pTab = pOldIt
18f84 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69 66  em->pTab;.    if
18f85 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20  ( pTab ){.      
18f86 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20  pTab->nRef++;.  
18f87 20 20 7d 0a 20 20 20 20 70 4e 65 77 49 74 65 6d    }.    pNewItem
18f88 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69  ->pSelect = sqli
18f89 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c  te3SelectDup(db,
18f8a 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 53 65 6c 65   pOldItem->pSele
18f8b 63 74 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65  ct);.    pNewIte
18f8c 6d 2d 3e 70 4f 6e 20 3d 20 73 71 6c 69 74 65 33  m->pOn = sqlite3
18f8d 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 6c 64  ExprDup(db, pOld
18f8e 49 74 65 6d 2d 3e 70 4f 6e 29 3b 0a 20 20 20 20  Item->pOn);.    
18f8f 70 4e 65 77 49 74 65 6d 2d 3e 70 55 73 69 6e 67  pNewItem->pUsing
18f90 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74   = sqlite3IdList
18f91 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d  Dup(db, pOldItem
18f92 2d 3e 70 55 73 69 6e 67 29 3b 0a 20 20 20 20 70  ->pUsing);.    p
18f93 4e 65 77 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64  NewItem->colUsed
18f94 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 63 6f 6c   = pOldItem->col
18f95 55 73 65 64 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Used;.  }.  retu
18f96 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54  rn pNew;.}.SQLIT
18f97 45 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73 74  E_PRIVATE IdList
18f98 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44   *sqlite3IdListD
18f99 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  up(sqlite3 *db, 
18f9a 49 64 4c 69 73 74 20 2a 70 29 7b 0a 20 20 49 64  IdList *p){.  Id
18f9b 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 69 6e  List *pNew;.  in
18f9c 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  t i;.  if( p==0 
18f9d 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e  ) return 0;.  pN
18f9e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ew = sqlite3DbMa
18f9f 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65  llocRaw(db, size
18fa0 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20 69  of(*pNew) );.  i
18fa1 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74  f( pNew==0 ) ret
18fa2 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e  urn 0;.  pNew->n
18fa3 49 64 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f  Id = pNew->nAllo
18fa4 63 20 3d 20 70 2d 3e 6e 49 64 3b 0a 20 20 70 4e  c = p->nId;.  pN
18fa5 65 77 2d 3e 61 20 3d 20 73 71 6c 69 74 65 33 44  ew->a = sqlite3D
18fa6 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 70  bMallocRaw(db, p
18fa7 2d 3e 6e 49 64 2a 73 69 7a 65 6f 66 28 70 2d 3e  ->nId*sizeof(p->
18fa8 61 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 70  a[0]) );.  if( p
18fa9 4e 65 77 2d 3e 61 3d 3d 30 20 29 7b 0a 20 20 20  New->a==0 ){.   
18faa 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e   sqlite3_free(pN
18fab 65 77 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ew);.    return 
18fac 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  0;.  }.  for(i=0
18fad 3b 20 69 3c 70 2d 3e 6e 49 64 3b 20 69 2b 2b 29  ; i<p->nId; i++)
18fae 7b 0a 20 20 20 20 73 74 72 75 63 74 20 49 64 4c  {.    struct IdL
18faf 69 73 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49 74  ist_item *pNewIt
18fb0 65 6d 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 5d  em = &pNew->a[i]
18fb1 3b 0a 20 20 20 20 73 74 72 75 63 74 20 49 64 4c  ;.    struct IdL
18fb2 69 73 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74  ist_item *pOldIt
18fb3 65 6d 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20  em = &p->a[i];. 
18fb4 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 4e 61     pNewItem->zNa
18fb5 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  me = sqlite3DbSt
18fb6 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65  rDup(db, pOldIte
18fb7 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70  m->zName);.    p
18fb8 4e 65 77 49 74 65 6d 2d 3e 69 64 78 20 3d 20 70  NewItem->idx = p
18fb9 4f 6c 64 49 74 65 6d 2d 3e 69 64 78 3b 0a 20 20  OldItem->idx;.  
18fba 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b  }.  return pNew;
18fbb 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
18fbc 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65  E Select *sqlite
18fbd 33 53 65 6c 65 63 74 44 75 70 28 73 71 6c 69 74  3SelectDup(sqlit
18fbe 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a  e3 *db, Select *
18fbf 70 29 7b 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e  p){.  Select *pN
18fc0 65 77 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29  ew;.  if( p==0 )
18fc1 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65   return 0;.  pNe
18fc2 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  w = sqlite3DbMal
18fc3 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f  locRaw(db, sizeo
18fc4 66 28 2a 70 29 20 29 3b 0a 20 20 69 66 28 20 70  f(*p) );.  if( p
18fc5 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  New==0 ) return 
18fc6 30 3b 0a 20 20 70 4e 65 77 2d 3e 69 73 44 69 73  0;.  pNew->isDis
18fc7 74 69 6e 63 74 20 3d 20 70 2d 3e 69 73 44 69 73  tinct = p->isDis
18fc8 74 69 6e 63 74 3b 0a 20 20 70 4e 65 77 2d 3e 70  tinct;.  pNew->p
18fc9 45 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45  EList = sqlite3E
18fca 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70  xprListDup(db, p
18fcb 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 70 4e 65  ->pEList);.  pNe
18fcc 77 2d 3e 70 53 72 63 20 3d 20 73 71 6c 69 74 65  w->pSrc = sqlite
18fcd 33 53 72 63 4c 69 73 74 44 75 70 28 64 62 2c 20  3SrcListDup(db, 
18fce 70 2d 3e 70 53 72 63 29 3b 0a 20 20 70 4e 65 77  p->pSrc);.  pNew
18fcf 2d 3e 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74  ->pWhere = sqlit
18fd0 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d  e3ExprDup(db, p-
18fd1 3e 70 57 68 65 72 65 29 3b 0a 20 20 70 4e 65 77  >pWhere);.  pNew
18fd2 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20 73 71 6c  ->pGroupBy = sql
18fd3 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
18fd4 64 62 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 29  db, p->pGroupBy)
18fd5 3b 0a 20 20 70 4e 65 77 2d 3e 70 48 61 76 69 6e  ;.  pNew->pHavin
18fd6 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  g = sqlite3ExprD
18fd7 75 70 28 64 62 2c 20 70 2d 3e 70 48 61 76 69 6e  up(db, p->pHavin
18fd8 67 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 72 64  g);.  pNew->pOrd
18fd9 65 72 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78  erBy = sqlite3Ex
18fda 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d  prListDup(db, p-
18fdb 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 70 4e  >pOrderBy);.  pN
18fdc 65 77 2d 3e 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a  ew->op = p->op;.
18fdd 20 20 70 4e 65 77 2d 3e 70 50 72 69 6f 72 20 3d    pNew->pPrior =
18fde 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75   sqlite3SelectDu
18fdf 70 28 64 62 2c 20 70 2d 3e 70 50 72 69 6f 72 29  p(db, p->pPrior)
18fe0 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c 69 6d 69 74  ;.  pNew->pLimit
18fe1 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
18fe2 70 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29  p(db, p->pLimit)
18fe3 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 66 66 73 65  ;.  pNew->pOffse
18fe4 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  t = sqlite3ExprD
18fe5 75 70 28 64 62 2c 20 70 2d 3e 70 4f 66 66 73 65  up(db, p->pOffse
18fe6 74 29 3b 0a 20 20 70 4e 65 77 2d 3e 69 4c 69 6d  t);.  pNew->iLim
18fe7 69 74 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d  it = -1;.  pNew-
18fe8 3e 69 4f 66 66 73 65 74 20 3d 20 2d 31 3b 0a 20  >iOffset = -1;. 
18fe9 20 70 4e 65 77 2d 3e 69 73 52 65 73 6f 6c 76 65   pNew->isResolve
18fea 64 20 3d 20 70 2d 3e 69 73 52 65 73 6f 6c 76 65  d = p->isResolve
18feb 64 3b 0a 20 20 70 4e 65 77 2d 3e 69 73 41 67 67  d;.  pNew->isAgg
18fec 20 3d 20 70 2d 3e 69 73 41 67 67 3b 0a 20 20 70   = p->isAgg;.  p
18fed 4e 65 77 2d 3e 75 73 65 73 45 70 68 6d 20 3d 20  New->usesEphm = 
18fee 30 3b 0a 20 20 70 4e 65 77 2d 3e 64 69 73 61 6c  0;.  pNew->disal
18fef 6c 6f 77 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a  lowOrderBy = 0;.
18ff0 20 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 6d 6f    pNew->pRightmo
18ff1 73 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e  st = 0;.  pNew->
18ff2 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20  addrOpenEphm[0] 
18ff3 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64  = -1;.  pNew->ad
18ff4 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 20  drOpenEphm[1] = 
18ff5 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72  -1;.  pNew->addr
18ff6 4f 70 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 2d 31  OpenEphm[2] = -1
18ff7 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b  ;.  return pNew;
18ff8 0a 7d 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f  .}.#else.SQLITE_
18ff9 50 52 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a  PRIVATE Select *
18ffa 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70  sqlite3SelectDup
18ffb 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 65  (sqlite3 *db, Se
18ffc 6c 65 63 74 20 2a 70 29 7b 0a 20 20 61 73 73 65  lect *p){.  asse
18ffd 72 74 28 20 70 3d 3d 30 20 29 3b 0a 20 20 72 65  rt( p==0 );.  re
18ffe 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66  turn 0;.}.#endif
18fff 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e  .../*.** Add a n
19000 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68  ew element to th
19001 65 20 65 6e 64 20 6f 66 20 61 6e 20 65 78 70 72  e end of an expr
19002 65 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20 49 66  ession list.  If
19003 20 70 4c 69 73 74 20 69 73 0a 2a 2a 20 69 6e 69   pList is.** ini
19004 74 69 61 6c 6c 79 20 4e 55 4c 4c 2c 20 74 68 65  tially NULL, the
19005 6e 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 65  n create a new e
19006 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a  xpression list..
19007 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
19008 45 20 45 78 70 72 4c 69 73 74 20 2a 73 71 6c 69  E ExprList *sqli
19009 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
1900a 64 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  d(.  Parse *pPar
1900b 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  se,          /* 
1900c 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
1900d 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
1900e 4c 69 73 74 2c 20 20 20 20 20 20 20 20 2f 2a 20  List,        /* 
1900f 4c 69 73 74 20 74 6f 20 77 68 69 63 68 20 74 6f  List to which to
19010 20 61 70 70 65 6e 64 2e 20 4d 69 67 68 74 20 62   append. Might b
19011 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78 70 72  e NULL */.  Expr
19012 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20   *pExpr,        
19013 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
19014 6e 20 74 6f 20 62 65 20 61 70 70 65 6e 64 65 64  n to be appended
19015 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
19016 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  me            /*
19017 20 41 53 20 6b 65 79 77 6f 72 64 20 66 6f 72 20   AS keyword for 
19018 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  the expression *
19019 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  /.){.  sqlite3 *
1901a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
1901b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20  .  if( pList==0 
1901c 29 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73  ){.    pList = s
1901d 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
1901e 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 45 78  ro(db, sizeof(Ex
1901f 70 72 4c 69 73 74 29 20 29 3b 0a 20 20 20 20 69  prList) );.    i
19020 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20  f( pList==0 ){. 
19021 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d       goto no_mem
19022 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
19023 72 74 28 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f  rt( pList->nAllo
19024 63 3d 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 69 66  c==0 );.  }.  if
19025 28 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3c  ( pList->nAlloc<
19026 3d 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b  =pList->nExpr ){
19027 0a 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72  .    struct Expr
19028 4c 69 73 74 5f 69 74 65 6d 20 2a 61 3b 0a 20 20  List_item *a;.  
19029 20 20 69 6e 74 20 6e 20 3d 20 70 4c 69 73 74 2d    int n = pList-
1902a 3e 6e 41 6c 6c 6f 63 2a 32 20 2b 20 34 3b 0a 20  >nAlloc*2 + 4;. 
1902b 20 20 20 61 20 3d 20 73 71 6c 69 74 65 33 44 62     a = sqlite3Db
1902c 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 4c 69 73  Realloc(db, pLis
1902d 74 2d 3e 61 2c 20 6e 2a 73 69 7a 65 6f 66 28 70  t->a, n*sizeof(p
1902e 4c 69 73 74 2d 3e 61 5b 30 5d 29 29 3b 0a 20 20  List->a[0]));.  
1902f 20 20 69 66 28 20 61 3d 3d 30 20 29 7b 0a 20 20    if( a==0 ){.  
19030 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b      goto no_mem;
19031 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 69 73 74  .    }.    pList
19032 2d 3e 61 20 3d 20 61 3b 0a 20 20 20 20 70 4c 69  ->a = a;.    pLi
19033 73 74 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 6e 3b 0a  st->nAlloc = n;.
19034 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4c    }.  assert( pL
19035 69 73 74 2d 3e 61 21 3d 30 20 29 3b 0a 20 20 69  ist->a!=0 );.  i
19036 66 28 20 70 45 78 70 72 20 7c 7c 20 70 4e 61 6d  f( pExpr || pNam
19037 65 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  e ){.    struct 
19038 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
19039 49 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61  Item = &pList->a
1903a 5b 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2b 2b 5d  [pList->nExpr++]
1903b 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 49 74  ;.    memset(pIt
1903c 65 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70  em, 0, sizeof(*p
1903d 49 74 65 6d 29 29 3b 0a 20 20 20 20 70 49 74 65  Item));.    pIte
1903e 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  m->zName = sqlit
1903f 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
19040 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20 20  db, pName);.    
19041 70 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70  pItem->pExpr = p
19042 45 78 70 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Expr;.  }.  retu
19043 72 6e 20 70 4c 69 73 74 3b 0a 0a 6e 6f 5f 6d 65  rn pList;..no_me
19044 6d 3a 20 20 20 20 20 0a 20 20 2f 2a 20 41 76 6f  m:     .  /* Avo
19045 69 64 20 6c 65 61 6b 69 6e 67 20 6d 65 6d 6f 72  id leaking memor
19046 79 20 69 66 20 6d 61 6c 6c 6f 63 20 68 61 73 20  y if malloc has 
19047 66 61 69 6c 65 64 2e 20 2a 2f 0a 20 20 73 71 6c  failed. */.  sql
19048 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70  ite3ExprDelete(p
19049 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  Expr);.  sqlite3
1904a 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70  ExprListDelete(p
1904b 4c 69 73 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  List);.  return 
1904c 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  0;.}../*.** If t
1904d 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  he expression li
1904e 73 74 20 70 45 4c 69 73 74 20 63 6f 6e 74 61 69  st pEList contai
1904f 6e 73 20 6d 6f 72 65 20 74 68 61 6e 20 69 4c 69  ns more than iLi
19050 6d 69 74 20 65 6c 65 6d 65 6e 74 73 2c 0a 2a 2a  mit elements,.**
19051 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20   leave an error 
19052 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73  message in pPars
19053 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
19054 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
19055 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65  3ExprListCheckLe
19056 6e 67 74 68 28 0a 20 20 50 61 72 73 65 20 2a 70  ngth(.  Parse *p
19057 50 61 72 73 65 2c 0a 20 20 45 78 70 72 4c 69 73  Parse,.  ExprLis
19058 74 20 2a 70 45 4c 69 73 74 2c 0a 20 20 63 6f 6e  t *pEList,.  con
19059 73 74 20 63 68 61 72 20 2a 7a 4f 62 6a 65 63 74  st char *zObject
1905a 0a 29 7b 0a 20 20 69 6e 74 20 6d 78 20 3d 20 70  .){.  int mx = p
1905b 50 61 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d 69  Parse->db->aLimi
1905c 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43  t[SQLITE_LIMIT_C
1905d 4f 4c 55 4d 4e 5d 3b 0a 20 20 74 65 73 74 63 61  OLUMN];.  testca
1905e 73 65 28 20 70 45 4c 69 73 74 20 26 26 20 70 45  se( pEList && pE
1905f 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78 20  List->nExpr==mx 
19060 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70  );.  testcase( p
19061 45 4c 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d  EList && pEList-
19062 3e 6e 45 78 70 72 3d 3d 6d 78 2b 31 20 29 3b 0a  >nExpr==mx+1 );.
19063 20 20 69 66 28 20 70 45 4c 69 73 74 20 26 26 20    if( pEList && 
19064 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 6d 78  pEList->nExpr>mx
19065 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
19066 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
19067 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e  "too many column
19068 73 20 69 6e 20 25 73 22 2c 20 7a 4f 62 6a 65 63  s in %s", zObjec
19069 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 20 54  t);.  }.}.../* T
1906a 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 72  he following thr
1906b 65 65 20 66 75 6e 63 74 69 6f 6e 73 2c 20 68 65  ee functions, he
1906c 69 67 68 74 4f 66 45 78 70 72 28 29 2c 20 68 65  ightOfExpr(), he
1906d 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 29  ightOfExprList()
1906e 0a 2a 2a 20 61 6e 64 20 68 65 69 67 68 74 4f 66  .** and heightOf
1906f 53 65 6c 65 63 74 28 29 2c 20 61 72 65 20 75 73  Select(), are us
19070 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ed to determine 
19071 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67  the maximum heig
19072 68 74 0a 2a 2a 20 6f 66 20 61 6e 79 20 65 78 70  ht.** of any exp
19073 72 65 73 73 69 6f 6e 20 74 72 65 65 20 72 65 66  ression tree ref
19074 65 72 65 6e 63 65 64 20 62 79 20 74 68 65 20 73  erenced by the s
19075 74 72 75 63 74 75 72 65 20 70 61 73 73 65 64 20  tructure passed 
19076 61 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20  as the.** first 
19077 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  argument..**.** 
19078 49 66 20 74 68 69 73 20 6d 61 78 69 6d 75 6d 20  If this maximum 
19079 68 65 69 67 68 74 20 69 73 20 67 72 65 61 74 65  height is greate
1907a 72 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 65  r than the curre
1907b 6e 74 20 76 61 6c 75 65 20 70 6f 69 6e 74 65 64  nt value pointed
1907c 0a 2a 2a 20 74 6f 20 62 79 20 70 6e 48 65 69 67  .** to by pnHeig
1907d 68 74 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 70  ht, the second p
1907e 61 72 61 6d 65 74 65 72 2c 20 74 68 65 6e 20 73  arameter, then s
1907f 65 74 20 2a 70 6e 48 65 69 67 68 74 20 74 6f 20  et *pnHeight to 
19080 74 68 61 74 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a  that.** value..*
19081 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 65  /.static void he
19082 69 67 68 74 4f 66 45 78 70 72 28 45 78 70 72 20  ightOfExpr(Expr 
19083 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 68  *p, int *pnHeigh
19084 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  t){.  if( p ){. 
19085 20 20 20 69 66 28 20 70 2d 3e 6e 48 65 69 67 68     if( p->nHeigh
19086 74 3e 2a 70 6e 48 65 69 67 68 74 20 29 7b 0a 20  t>*pnHeight ){. 
19087 20 20 20 20 20 2a 70 6e 48 65 69 67 68 74 20 3d       *pnHeight =
19088 20 70 2d 3e 6e 48 65 69 67 68 74 3b 0a 20 20 20   p->nHeight;.   
19089 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20   }.  }.}.static 
1908a 76 6f 69 64 20 68 65 69 67 68 74 4f 66 45 78 70  void heightOfExp
1908b 72 4c 69 73 74 28 45 78 70 72 4c 69 73 74 20 2a  rList(ExprList *
1908c 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 68 74  p, int *pnHeight
1908d 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  ){.  if( p ){.  
1908e 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72    int i;.    for
1908f 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78 70 72  (i=0; i<p->nExpr
19090 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 68 65  ; i++){.      he
19091 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 61 5b  ightOfExpr(p->a[
19092 69 5d 2e 70 45 78 70 72 2c 20 70 6e 48 65 69 67  i].pExpr, pnHeig
19093 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  ht);.    }.  }.}
19094 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 65 69  .static void hei
19095 67 68 74 4f 66 53 65 6c 65 63 74 28 53 65 6c 65  ghtOfSelect(Sele
19096 63 74 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65  ct *p, int *pnHe
19097 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29  ight){.  if( p )
19098 7b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78  {.    heightOfEx
19099 70 72 28 70 2d 3e 70 57 68 65 72 65 2c 20 70 6e  pr(p->pWhere, pn
1909a 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69  Height);.    hei
1909b 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 48 61  ghtOfExpr(p->pHa
1909c 76 69 6e 67 2c 20 70 6e 48 65 69 67 68 74 29 3b  ving, pnHeight);
1909d 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70  .    heightOfExp
1909e 72 28 70 2d 3e 70 4c 69 6d 69 74 2c 20 70 6e 48  r(p->pLimit, pnH
1909f 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67  eight);.    heig
190a0 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 4f 66 66  htOfExpr(p->pOff
190a1 73 65 74 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a  set, pnHeight);.
190a2 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72      heightOfExpr
190a3 4c 69 73 74 28 70 2d 3e 70 45 4c 69 73 74 2c 20  List(p->pEList, 
190a4 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68  pnHeight);.    h
190a5 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28  eightOfExprList(
190a6 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 70 6e 48  p->pGroupBy, pnH
190a7 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67  eight);.    heig
190a8 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d 3e  htOfExprList(p->
190a9 70 4f 72 64 65 72 42 79 2c 20 70 6e 48 65 69 67  pOrderBy, pnHeig
190aa 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f  ht);.    heightO
190ab 66 53 65 6c 65 63 74 28 70 2d 3e 70 50 72 69 6f  fSelect(p->pPrio
190ac 72 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20  r, pnHeight);.  
190ad 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  }.}../*.** Set t
190ae 68 65 20 45 78 70 72 2e 6e 48 65 69 67 68 74 20  he Expr.nHeight 
190af 76 61 72 69 61 62 6c 65 20 69 6e 20 74 68 65 20  variable in the 
190b0 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65 64  structure passed
190b1 20 61 73 20 61 6e 20 0a 2a 2a 20 61 72 67 75 6d   as an .** argum
190b2 65 6e 74 2e 20 41 6e 20 65 78 70 72 65 73 73 69  ent. An expressi
190b3 6f 6e 20 77 69 74 68 20 6e 6f 20 63 68 69 6c 64  on with no child
190b4 72 65 6e 2c 20 45 78 70 72 2e 70 4c 69 73 74 20  ren, Expr.pList 
190b5 6f 72 20 0a 2a 2a 20 45 78 70 72 2e 70 53 65 6c  or .** Expr.pSel
190b6 65 63 74 20 6d 65 6d 62 65 72 20 68 61 73 20 61  ect member has a
190b7 20 68 65 69 67 68 74 20 6f 66 20 31 2e 20 41 6e   height of 1. An
190b8 79 20 6f 74 68 65 72 20 65 78 70 72 65 73 73 69  y other expressi
190b9 6f 6e 0a 2a 2a 20 68 61 73 20 61 20 68 65 69 67  on.** has a heig
190ba 68 74 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20  ht equal to the 
190bb 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 20 6f  maximum height o
190bc 66 20 61 6e 79 20 6f 74 68 65 72 20 0a 2a 2a 20  f any other .** 
190bd 72 65 66 65 72 65 6e 63 65 64 20 45 78 70 72 20  referenced Expr 
190be 70 6c 75 73 20 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c  plus one..*/.SQL
190bf 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
190c0 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48   sqlite3ExprSetH
190c1 65 69 67 68 74 28 45 78 70 72 20 2a 70 29 7b 0a  eight(Expr *p){.
190c2 20 20 69 6e 74 20 6e 48 65 69 67 68 74 20 3d 20    int nHeight = 
190c3 30 3b 0a 20 20 68 65 69 67 68 74 4f 66 45 78 70  0;.  heightOfExp
190c4 72 28 70 2d 3e 70 4c 65 66 74 2c 20 26 6e 48 65  r(p->pLeft, &nHe
190c5 69 67 68 74 29 3b 0a 20 20 68 65 69 67 68 74 4f  ight);.  heightO
190c6 66 45 78 70 72 28 70 2d 3e 70 52 69 67 68 74 2c  fExpr(p->pRight,
190c7 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 68 65   &nHeight);.  he
190c8 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70  ightOfExprList(p
190c9 2d 3e 70 4c 69 73 74 2c 20 26 6e 48 65 69 67 68  ->pList, &nHeigh
190ca 74 29 3b 0a 20 20 68 65 69 67 68 74 4f 66 53 65  t);.  heightOfSe
190cb 6c 65 63 74 28 70 2d 3e 70 53 65 6c 65 63 74 2c  lect(p->pSelect,
190cc 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 70 2d   &nHeight);.  p-
190cd 3e 6e 48 65 69 67 68 74 20 3d 20 6e 48 65 69 67  >nHeight = nHeig
190ce 68 74 20 2b 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ht + 1;.}../*.**
190cf 20 52 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69   Return the maxi
190d0 6d 75 6d 20 68 65 69 67 68 74 20 6f 66 20 61 6e  mum height of an
190d1 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  y expression tre
190d2 65 20 72 65 66 65 72 65 6e 63 65 64 0a 2a 2a 20  e referenced.** 
190d3 62 79 20 74 68 65 20 73 65 6c 65 63 74 20 73 74  by the select st
190d4 61 74 65 6d 65 6e 74 20 70 61 73 73 65 64 20 61  atement passed a
190d5 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a  s an argument..*
190d6 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
190d7 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c 65   int sqlite3Sele
190d8 63 74 45 78 70 72 48 65 69 67 68 74 28 53 65 6c  ctExprHeight(Sel
190d9 65 63 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e  ect *p){.  int n
190da 48 65 69 67 68 74 20 3d 20 30 3b 0a 20 20 68 65  Height = 0;.  he
190db 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70 2c 20  ightOfSelect(p, 
190dc 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 72 65 74  &nHeight);.  ret
190dd 75 72 6e 20 6e 48 65 69 67 68 74 3b 0a 7d 0a 0a  urn nHeight;.}..
190de 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20  /*.** Delete an 
190df 65 6e 74 69 72 65 20 65 78 70 72 65 73 73 69 6f  entire expressio
190e0 6e 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54  n list..*/.SQLIT
190e1 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
190e2 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
190e3 6c 65 74 65 28 45 78 70 72 4c 69 73 74 20 2a 70  lete(ExprList *p
190e4 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  List){.  int i;.
190e5 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
190e6 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20  t_item *pItem;. 
190e7 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20   if( pList==0 ) 
190e8 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74  return;.  assert
190e9 28 20 70 4c 69 73 74 2d 3e 61 21 3d 30 20 7c 7c  ( pList->a!=0 ||
190ea 20 28 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d   (pList->nExpr==
190eb 30 20 26 26 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c  0 && pList->nAll
190ec 6f 63 3d 3d 30 29 20 29 3b 0a 20 20 61 73 73 65  oc==0) );.  asse
190ed 72 74 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72  rt( pList->nExpr
190ee 3c 3d 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20  <=pList->nAlloc 
190ef 29 3b 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70  );.  for(pItem=p
190f0 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c  List->a, i=0; i<
190f1 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b  pList->nExpr; i+
190f2 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  +, pItem++){.   
190f3 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
190f4 74 65 28 70 49 74 65 6d 2d 3e 70 45 78 70 72 29  te(pItem->pExpr)
190f5 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
190f6 65 65 28 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29  ee(pItem->zName)
190f7 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
190f8 66 72 65 65 28 70 4c 69 73 74 2d 3e 61 29 3b 0a  free(pList->a);.
190f9 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
190fa 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  List);.}../*.** 
190fb 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 69  Walk an expressi
190fc 6f 6e 20 74 72 65 65 2e 20 20 43 61 6c 6c 20 78  on tree.  Call x
190fd 46 75 6e 63 20 66 6f 72 20 65 61 63 68 20 6e 6f  Func for each no
190fe 64 65 20 76 69 73 69 74 65 64 2e 20 20 78 46 75  de visited.  xFu
190ff 6e 63 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20  nc.** is called 
19100 6f 6e 20 74 68 65 20 6e 6f 64 65 20 62 65 66 6f  on the node befo
19101 72 65 20 78 46 75 6e 63 20 69 73 20 63 61 6c 6c  re xFunc is call
19102 65 64 20 6f 6e 20 74 68 65 20 6e 6f 64 65 73 20  ed on the nodes 
19103 63 68 69 6c 64 72 65 6e 2e 0a 2a 2a 0a 2a 2a 20  children..**.** 
19104 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
19105 20 66 72 6f 6d 20 78 46 75 6e 63 20 64 65 74 65   from xFunc dete
19106 72 6d 69 6e 65 73 20 77 68 65 74 68 65 72 20 74  rmines whether t
19107 68 65 20 74 72 65 65 20 77 61 6c 6b 20 63 6f 6e  he tree walk con
19108 74 69 6e 75 65 73 2e 0a 2a 2a 20 30 20 6d 65 61  tinues..** 0 mea
19109 6e 73 20 63 6f 6e 74 69 6e 75 65 20 77 61 6c 6b  ns continue walk
1910a 69 6e 67 20 74 68 65 20 74 72 65 65 2e 20 20 31  ing the tree.  1
1910b 20 6d 65 61 6e 73 20 64 6f 20 6e 6f 74 20 77 61   means do not wa
1910c 6c 6b 20 63 68 69 6c 64 72 65 6e 0a 2a 2a 20 6f  lk children.** o
1910d 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e 6f  f the current no
1910e 64 65 20 62 75 74 20 63 6f 6e 74 69 6e 75 65 20  de but continue 
1910f 77 69 74 68 20 73 69 62 6c 69 6e 67 73 2e 20 20  with siblings.  
19110 32 20 6d 65 61 6e 73 20 61 62 61 6e 64 6f 6e 0a  2 means abandon.
19111 2a 2a 20 74 68 65 20 74 72 65 65 20 77 61 6c 6b  ** the tree walk
19112 20 63 6f 6d 70 6c 65 74 65 6c 79 2e 0a 2a 2a 0a   completely..**.
19113 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  ** The return va
19114 6c 75 65 20 66 72 6f 6d 20 74 68 69 73 20 72 6f  lue from this ro
19115 75 74 69 6e 65 20 69 73 20 31 20 74 6f 20 61 62  utine is 1 to ab
19116 61 6e 64 6f 6e 20 74 68 65 20 74 72 65 65 20 77  andon the tree w
19117 61 6c 6b 0a 2a 2a 20 61 6e 64 20 30 20 74 6f 20  alk.** and 0 to 
19118 63 6f 6e 74 69 6e 75 65 2e 0a 2a 2a 0a 2a 2a 20  continue..**.** 
19119 4e 4f 54 49 43 45 3a 20 20 54 68 69 73 20 72 6f  NOTICE:  This ro
1911a 75 74 69 6e 65 20 64 6f 65 73 20 2a 6e 6f 74 2a  utine does *not*
1911b 20 64 65 73 63 65 6e 64 20 69 6e 74 6f 20 73 75   descend into su
1911c 62 71 75 65 72 69 65 73 2e 0a 2a 2f 0a 73 74 61  bqueries..*/.sta
1911d 74 69 63 20 69 6e 74 20 77 61 6c 6b 45 78 70 72  tic int walkExpr
1911e 4c 69 73 74 28 45 78 70 72 4c 69 73 74 20 2a 2c  List(ExprList *,
1911f 20 69 6e 74 20 28 2a 29 28 76 6f 69 64 20 2a 2c   int (*)(void *,
19120 20 45 78 70 72 2a 29 2c 20 76 6f 69 64 20 2a 29   Expr*), void *)
19121 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 77 61 6c  ;.static int wal
19122 6b 45 78 70 72 54 72 65 65 28 45 78 70 72 20 2a  kExprTree(Expr *
19123 70 45 78 70 72 2c 20 69 6e 74 20 28 2a 78 46 75  pExpr, int (*xFu
19124 6e 63 29 28 76 6f 69 64 2a 2c 45 78 70 72 2a 29  nc)(void*,Expr*)
19125 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20  , void *pArg){. 
19126 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70   int rc;.  if( p
19127 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e  Expr==0 ) return
19128 20 30 3b 0a 20 20 72 63 20 3d 20 28 2a 78 46 75   0;.  rc = (*xFu
19129 6e 63 29 28 70 41 72 67 2c 20 70 45 78 70 72 29  nc)(pArg, pExpr)
1912a 3b 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b  ;.  if( rc==0 ){
1912b 0a 20 20 20 20 69 66 28 20 77 61 6c 6b 45 78 70  .    if( walkExp
1912c 72 54 72 65 65 28 70 45 78 70 72 2d 3e 70 4c 65  rTree(pExpr->pLe
1912d 66 74 2c 20 78 46 75 6e 63 2c 20 70 41 72 67 29  ft, xFunc, pArg)
1912e 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20   ) return 1;.   
1912f 20 69 66 28 20 77 61 6c 6b 45 78 70 72 54 72 65   if( walkExprTre
19130 65 28 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  e(pExpr->pRight,
19131 20 78 46 75 6e 63 2c 20 70 41 72 67 29 20 29 20   xFunc, pArg) ) 
19132 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 69 66  return 1;.    if
19133 28 20 77 61 6c 6b 45 78 70 72 4c 69 73 74 28 70  ( walkExprList(p
19134 45 78 70 72 2d 3e 70 4c 69 73 74 2c 20 78 46 75  Expr->pList, xFu
19135 6e 63 2c 20 70 41 72 67 29 20 29 20 72 65 74 75  nc, pArg) ) retu
19136 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rn 1;.  }.  retu
19137 72 6e 20 72 63 3e 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn rc>1;.}../*.*
19138 2a 20 43 61 6c 6c 20 77 61 6c 6b 45 78 70 72 54  * Call walkExprT
19139 72 65 65 28 29 20 66 6f 72 20 65 76 65 72 79 20  ree() for every 
1913a 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 6c 69  expression in li
1913b 73 74 20 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  st p..*/.static 
1913c 69 6e 74 20 77 61 6c 6b 45 78 70 72 4c 69 73 74  int walkExprList
1913d 28 45 78 70 72 4c 69 73 74 20 2a 70 2c 20 69 6e  (ExprList *p, in
1913e 74 20 28 2a 78 46 75 6e 63 29 28 76 6f 69 64 20  t (*xFunc)(void 
1913f 2a 2c 20 45 78 70 72 2a 29 2c 20 76 6f 69 64 20  *, Expr*), void 
19140 2a 70 41 72 67 29 7b 0a 20 20 69 6e 74 20 69 3b  *pArg){.  int i;
19141 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69  .  struct ExprLi
19142 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
19143 20 20 69 66 28 20 21 70 20 29 20 72 65 74 75 72    if( !p ) retur
19144 6e 20 30 3b 0a 20 20 66 6f 72 28 69 3d 70 2d 3e  n 0;.  for(i=p->
19145 6e 45 78 70 72 2c 20 70 49 74 65 6d 3d 70 2d 3e  nExpr, pItem=p->
19146 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74  a; i>0; i--, pIt
19147 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 77  em++){.    if( w
19148 61 6c 6b 45 78 70 72 54 72 65 65 28 70 49 74 65  alkExprTree(pIte
19149 6d 2d 3e 70 45 78 70 72 2c 20 78 46 75 6e 63 2c  m->pExpr, xFunc,
1914a 20 70 41 72 67 29 20 29 20 72 65 74 75 72 6e 20   pArg) ) return 
1914b 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  1;.  }.  return 
1914c 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c  0;.}../*.** Call
1914d 20 77 61 6c 6b 45 78 70 72 54 72 65 65 28 29 20   walkExprTree() 
1914e 66 6f 72 20 65 76 65 72 79 20 65 78 70 72 65 73  for every expres
1914f 73 69 6f 6e 20 69 6e 20 53 65 6c 65 63 74 20 70  sion in Select p
19150 2c 20 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 0a  , not including.
19151 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74  ** expressions t
19152 68 61 74 20 61 72 65 20 70 61 72 74 20 6f 66 20  hat are part of 
19153 73 75 62 2d 73 65 6c 65 63 74 73 20 69 6e 20 61  sub-selects in a
19154 6e 79 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  ny FROM clause o
19155 72 20 74 68 65 20 4c 49 4d 49 54 0a 2a 2a 20 6f  r the LIMIT.** o
19156 72 20 4f 46 46 53 45 54 20 65 78 70 72 65 73 73  r OFFSET express
19157 69 6f 6e 73 2e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ions...*/.static
19158 20 69 6e 74 20 77 61 6c 6b 53 65 6c 65 63 74 45   int walkSelectE
19159 78 70 72 28 53 65 6c 65 63 74 20 2a 70 2c 20 69  xpr(Select *p, i
1915a 6e 74 20 28 2a 78 46 75 6e 63 29 28 76 6f 69 64  nt (*xFunc)(void
1915b 20 2a 2c 20 45 78 70 72 2a 29 2c 20 76 6f 69 64   *, Expr*), void
1915c 20 2a 70 41 72 67 29 7b 0a 20 20 77 61 6c 6b 45   *pArg){.  walkE
1915d 78 70 72 4c 69 73 74 28 70 2d 3e 70 45 4c 69 73  xprList(p->pELis
1915e 74 2c 20 78 46 75 6e 63 2c 20 70 41 72 67 29 3b  t, xFunc, pArg);
1915f 0a 20 20 77 61 6c 6b 45 78 70 72 54 72 65 65 28  .  walkExprTree(
19160 70 2d 3e 70 57 68 65 72 65 2c 20 78 46 75 6e 63  p->pWhere, xFunc
19161 2c 20 70 41 72 67 29 3b 0a 20 20 77 61 6c 6b 45  , pArg);.  walkE
19162 78 70 72 4c 69 73 74 28 70 2d 3e 70 47 72 6f 75  xprList(p->pGrou
19163 70 42 79 2c 20 78 46 75 6e 63 2c 20 70 41 72 67  pBy, xFunc, pArg
19164 29 3b 0a 20 20 77 61 6c 6b 45 78 70 72 54 72 65  );.  walkExprTre
19165 65 28 70 2d 3e 70 48 61 76 69 6e 67 2c 20 78 46  e(p->pHaving, xF
19166 75 6e 63 2c 20 70 41 72 67 29 3b 0a 20 20 77 61  unc, pArg);.  wa
19167 6c 6b 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 4f  lkExprList(p->pO
19168 72 64 65 72 42 79 2c 20 78 46 75 6e 63 2c 20 70  rderBy, xFunc, p
19169 41 72 67 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70  Arg);.  if( p->p
1916a 50 72 69 6f 72 20 29 7b 0a 20 20 20 20 77 61 6c  Prior ){.    wal
1916b 6b 53 65 6c 65 63 74 45 78 70 72 28 70 2d 3e 70  kSelectExpr(p->p
1916c 50 72 69 6f 72 2c 20 78 46 75 6e 63 2c 20 70 41  Prior, xFunc, pA
1916d 72 67 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  rg);.  }.  retur
1916e 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54  n 0;.}.../*.** T
1916f 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 64  his routine is d
19170 65 73 69 67 6e 65 64 20 61 73 20 61 6e 20 78 46  esigned as an xF
19171 75 6e 63 20 66 6f 72 20 77 61 6c 6b 45 78 70 72  unc for walkExpr
19172 54 72 65 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 70 41  Tree()..**.** pA
19173 72 67 20 69 73 20 72 65 61 6c 6c 79 20 61 20 70  rg is really a p
19174 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 74  ointer to an int
19175 65 67 65 72 2e 20 20 49 66 20 77 65 20 63 61 6e  eger.  If we can
19176 20 74 65 6c 6c 20 62 79 20 6c 6f 6f 6b 69 6e 67   tell by looking
19177 0a 2a 2a 20 61 74 20 70 45 78 70 72 20 74 68 61  .** at pExpr tha
19178 74 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  t the expression
19179 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 70   that contains p
1917a 45 78 70 72 20 69 73 20 6e 6f 74 20 61 20 63 6f  Expr is not a co
1917b 6e 73 74 61 6e 74 0a 2a 2a 20 65 78 70 72 65 73  nstant.** expres
1917c 73 69 6f 6e 2c 20 74 68 65 6e 20 73 65 74 20 2a  sion, then set *
1917d 70 41 72 67 20 74 6f 20 30 20 61 6e 64 20 72 65  pArg to 0 and re
1917e 74 75 72 6e 20 32 20 74 6f 20 61 62 61 6e 64 6f  turn 2 to abando
1917f 6e 20 74 68 65 20 74 72 65 65 20 77 61 6c 6b 2e  n the tree walk.
19180 0a 2a 2a 20 49 66 20 70 45 78 70 72 20 64 6f 65  .** If pExpr doe
19181 73 20 64 6f 65 73 20 6e 6f 74 20 64 69 73 71 75  s does not disqu
19182 61 6c 69 66 79 20 74 68 65 20 65 78 70 72 65 73  alify the expres
19183 73 69 6f 6e 20 66 72 6f 6d 20 62 65 69 6e 67 20  sion from being 
19184 61 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 74 68  a constant.** th
19185 65 6e 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 0a 2a  en do nothing..*
19186 2a 0a 2a 2a 20 41 66 74 65 72 20 77 61 6c 6b 69  *.** After walki
19187 6e 67 20 74 68 65 20 77 68 6f 6c 65 20 74 72 65  ng the whole tre
19188 65 2c 20 69 66 20 6e 6f 20 6e 6f 64 65 73 20 61  e, if no nodes a
19189 72 65 20 66 6f 75 6e 64 20 74 68 61 74 20 64 69  re found that di
1918a 73 71 75 61 6c 69 66 79 0a 2a 2a 20 74 68 65 20  squalify.** the 
1918b 65 78 70 72 65 73 73 69 6f 6e 20 61 73 20 63 6f  expression as co
1918c 6e 73 74 61 6e 74 2c 20 74 68 65 6e 20 77 65 20  nstant, then we 
1918d 61 73 73 75 6d 65 20 74 68 65 20 77 68 6f 6c 65  assume the whole
1918e 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69   expression.** i
1918f 73 20 63 6f 6e 73 74 61 6e 74 2e 20 20 53 65 65  s constant.  See
19190 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f   sqlite3ExprIsCo
19191 6e 73 74 61 6e 74 28 29 20 66 6f 72 20 61 64 64  nstant() for add
19192 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
19193 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ion..*/.static i
19194 6e 74 20 65 78 70 72 4e 6f 64 65 49 73 43 6f 6e  nt exprNodeIsCon
19195 73 74 61 6e 74 28 76 6f 69 64 20 2a 70 41 72 67  stant(void *pArg
19196 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a  , Expr *pExpr){.
19197 20 20 69 6e 74 20 2a 70 4e 20 3d 20 28 69 6e 74    int *pN = (int
19198 2a 29 70 41 72 67 3b 0a 0a 20 20 2f 2a 20 49 66  *)pArg;..  /* If
19199 20 2a 70 41 72 67 20 69 73 20 33 20 74 68 65 6e   *pArg is 3 then
1919a 20 61 6e 79 20 74 65 72 6d 20 6f 66 20 74 68 65   any term of the
1919b 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74   expression that
1919c 20 63 6f 6d 65 73 20 66 72 6f 6d 0a 20 20 2a 2a   comes from.  **
1919d 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47   the ON or USING
1919e 20 63 6c 61 75 73 65 73 20 6f 66 20 61 20 6a 6f   clauses of a jo
1919f 69 6e 20 64 69 73 71 75 61 6c 69 66 69 65 73 20  in disqualifies 
191a0 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 20  the expression. 
191a1 20 2a 2a 20 66 72 6f 6d 20 62 65 69 6e 67 20 63   ** from being c
191a2 6f 6e 73 69 64 65 72 65 64 20 63 6f 6e 73 74 61  onsidered consta
191a3 6e 74 2e 20 2a 2f 0a 20 20 69 66 28 20 28 2a 70  nt. */.  if( (*p
191a4 4e 29 3d 3d 33 20 26 26 20 45 78 70 72 48 61 73  N)==3 && ExprHas
191a5 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70  AnyProperty(pExp
191a6 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20  r, EP_FromJoin) 
191a7 29 7b 0a 20 20 20 20 2a 70 4e 20 3d 20 30 3b 0a  ){.    *pN = 0;.
191a8 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
191a9 7d 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 78  }..  switch( pEx
191aa 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 2f 2a  pr->op ){.    /*
191ab 20 43 6f 6e 73 69 64 65 72 20 66 75 6e 63 74 69   Consider functi
191ac 6f 6e 73 20 74 6f 20 62 65 20 63 6f 6e 73 74 61  ons to be consta
191ad 6e 74 20 69 66 20 61 6c 6c 20 74 68 65 69 72 20  nt if all their 
191ae 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 63 6f  arguments are co
191af 6e 73 74 61 6e 74 0a 20 20 20 20 2a 2a 20 61 6e  nstant.    ** an
191b0 64 20 2a 70 41 72 67 3d 3d 32 20 2a 2f 0a 20 20  d *pArg==2 */.  
191b1 20 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49    case TK_FUNCTI
191b2 4f 4e 3a 0a 20 20 20 20 20 20 69 66 28 20 28 2a  ON:.      if( (*
191b3 70 4e 29 3d 3d 32 20 29 20 72 65 74 75 72 6e 20  pN)==2 ) return 
191b4 30 3b 0a 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c  0;.      /* Fall
191b5 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20 20 20 20   through */.    
191b6 63 61 73 65 20 54 4b 5f 49 44 3a 0a 20 20 20 20  case TK_ID:.    
191b7 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 0a  case TK_COLUMN:.
191b8 20 20 20 20 63 61 73 65 20 54 4b 5f 44 4f 54 3a      case TK_DOT:
191b9 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47  .    case TK_AGG
191ba 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 63  _FUNCTION:.    c
191bb 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d  ase TK_AGG_COLUM
191bc 4e 3a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  N:.#ifndef SQLIT
191bd 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a  E_OMIT_SUBQUERY.
191be 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45      case TK_SELE
191bf 43 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  CT:.    case TK_
191c0 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 74 65  EXISTS:.      te
191c1 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f  stcase( pExpr->o
191c2 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b 0a  p==TK_SELECT );.
191c3 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
191c4 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58  pExpr->op==TK_EX
191c5 49 53 54 53 20 29 3b 0a 23 65 6e 64 69 66 0a 20  ISTS );.#endif. 
191c6 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
191c7 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20  Expr->op==TK_ID 
191c8 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
191c9 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  e( pExpr->op==TK
191ca 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20  _COLUMN );.     
191cb 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72   testcase( pExpr
191cc 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 29 3b 0a  ->op==TK_DOT );.
191cd 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
191ce 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47  pExpr->op==TK_AG
191cf 47 5f 46 55 4e 43 54 49 4f 4e 20 29 3b 0a 20 20  G_FUNCTION );.  
191d0 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45      testcase( pE
191d1 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f  xpr->op==TK_AGG_
191d2 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20  COLUMN );.      
191d3 2a 70 4e 20 3d 20 30 3b 0a 20 20 20 20 20 20 72  *pN = 0;.      r
191d4 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 63 61 73  eturn 2;.    cas
191d5 65 20 54 4b 5f 49 4e 3a 0a 20 20 20 20 20 20 69  e TK_IN:.      i
191d6 66 28 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 63  f( pExpr->pSelec
191d7 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 4e  t ){.        *pN
191d8 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 72 65   = 0;.        re
191d9 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 0a  turn 2;.      }.
191da 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20      default:.   
191db 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
191dc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61  .}../*.** Walk a
191dd 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  n expression tre
191de 65 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20  e.  Return 1 if 
191df 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
191e0 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61 6e  s constant.** an
191e1 64 20 30 20 69 66 20 69 74 20 69 6e 76 6f 6c 76  d 0 if it involv
191e2 65 73 20 76 61 72 69 61 62 6c 65 73 20 6f 72 20  es variables or 
191e3 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 2e 0a  function calls..
191e4 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75  **.** For the pu
191e5 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 66  rposes of this f
191e6 75 6e 63 74 69 6f 6e 2c 20 61 20 64 6f 75 62 6c  unction, a doubl
191e7 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20  e-quoted string 
191e8 28 65 78 3a 20 22 61 62 63 22 29 0a 2a 2a 20 69  (ex: "abc").** i
191e9 73 20 63 6f 6e 73 69 64 65 72 65 64 20 61 20 76  s considered a v
191ea 61 72 69 61 62 6c 65 20 62 75 74 20 61 20 73 69  ariable but a si
191eb 6e 67 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69  ngle-quoted stri
191ec 6e 67 20 28 65 78 3a 20 27 61 62 63 27 29 20 69  ng (ex: 'abc') i
191ed 73 0a 2a 2a 20 61 20 63 6f 6e 73 74 61 6e 74 2e  s.** a constant.
191ee 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
191ef 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78  TE int sqlite3Ex
191f0 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 70  prIsConstant(Exp
191f1 72 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 73 43  r *p){.  int isC
191f2 6f 6e 73 74 20 3d 20 31 3b 0a 20 20 77 61 6c 6b  onst = 1;.  walk
191f3 45 78 70 72 54 72 65 65 28 70 2c 20 65 78 70 72  ExprTree(p, expr
191f4 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 2c 20  NodeIsConstant, 
191f5 26 69 73 43 6f 6e 73 74 29 3b 0a 20 20 72 65 74  &isConst);.  ret
191f6 75 72 6e 20 69 73 43 6f 6e 73 74 3b 0a 7d 0a 0a  urn isConst;.}..
191f7 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78  /*.** Walk an ex
191f8 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20  pression tree.  
191f9 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20  Return 1 if the 
191fa 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f  expression is co
191fb 6e 73 74 61 6e 74 0a 2a 2a 20 74 68 61 74 20 64  nstant.** that d
191fc 6f 65 73 20 6e 6f 20 6f 72 69 67 69 6e 61 74 65  oes no originate
191fd 20 66 72 6f 6d 20 74 68 65 20 4f 4e 20 6f 72 20   from the ON or 
191fe 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 6f 66  USING clauses of
191ff 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 20 52 65 74 75   a join..** Retu
19200 72 6e 20 30 20 69 66 20 69 74 20 69 6e 76 6f 6c  rn 0 if it invol
19201 76 65 73 20 76 61 72 69 61 62 6c 65 73 20 6f 72  ves variables or
19202 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20   function calls 
19203 6f 72 20 74 65 72 6d 73 20 66 72 6f 6d 0a 2a 2a  or terms from.**
19204 20 61 6e 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20   an ON or USING 
19205 63 6c 61 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  clause..*/.SQLIT
19206 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
19207 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
19208 61 6e 74 4e 6f 74 4a 6f 69 6e 28 45 78 70 72 20  antNotJoin(Expr 
19209 2a 70 29 7b 0a 20 20 69 6e 74 20 69 73 43 6f 6e  *p){.  int isCon
1920a 73 74 20 3d 20 33 3b 0a 20 20 77 61 6c 6b 45 78  st = 3;.  walkEx
1920b 70 72 54 72 65 65 28 70 2c 20 65 78 70 72 4e 6f  prTree(p, exprNo
1920c 64 65 49 73 43 6f 6e 73 74 61 6e 74 2c 20 26 69  deIsConstant, &i
1920d 73 43 6f 6e 73 74 29 3b 0a 20 20 72 65 74 75 72  sConst);.  retur
1920e 6e 20 69 73 43 6f 6e 73 74 21 3d 30 3b 0a 7d 0a  n isConst!=0;.}.
1920f 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65  ./*.** Walk an e
19210 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20  xpression tree. 
19211 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65   Return 1 if the
19212 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 63   expression is c
19213 6f 6e 73 74 61 6e 74 0a 2a 2a 20 6f 72 20 61 20  onstant.** or a 
19214 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 77 69  function call wi
19215 74 68 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75  th constant argu
19216 6d 65 6e 74 73 2e 20 20 52 65 74 75 72 6e 20 61  ments.  Return a
19217 6e 64 20 30 20 69 66 20 74 68 65 72 65 0a 2a 2a  nd 0 if there.**
19218 20 61 72 65 20 61 6e 79 20 76 61 72 69 61 62 6c   are any variabl
19219 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68  es..**.** For th
1921a 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68  e purposes of th
1921b 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64  is function, a d
1921c 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 72  ouble-quoted str
1921d 69 6e 67 20 28 65 78 3a 20 22 61 62 63 22 29 0a  ing (ex: "abc").
1921e 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  ** is considered
1921f 20 61 20 76 61 72 69 61 62 6c 65 20 62 75 74 20   a variable but 
19220 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 64 20  a single-quoted 
19221 73 74 72 69 6e 67 20 28 65 78 3a 20 27 61 62 63  string (ex: 'abc
19222 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 74  ') is.** a const
19223 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ant..*/.SQLITE_P
19224 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19225 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74  e3ExprIsConstant
19226 4f 72 46 75 6e 63 74 69 6f 6e 28 45 78 70 72 20  OrFunction(Expr 
19227 2a 70 29 7b 0a 20 20 69 6e 74 20 69 73 43 6f 6e  *p){.  int isCon
19228 73 74 20 3d 20 32 3b 0a 20 20 77 61 6c 6b 45 78  st = 2;.  walkEx
19229 70 72 54 72 65 65 28 70 2c 20 65 78 70 72 4e 6f  prTree(p, exprNo
1922a 64 65 49 73 43 6f 6e 73 74 61 6e 74 2c 20 26 69  deIsConstant, &i
1922b 73 43 6f 6e 73 74 29 3b 0a 20 20 72 65 74 75 72  sConst);.  retur
1922c 6e 20 69 73 43 6f 6e 73 74 21 3d 30 3b 0a 7d 0a  n isConst!=0;.}.
1922d 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78  ./*.** If the ex
1922e 70 72 65 73 73 69 6f 6e 20 70 20 63 6f 64 65 73  pression p codes
1922f 20 61 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65   a constant inte
19230 67 65 72 20 74 68 61 74 20 69 73 20 73 6d 61 6c  ger that is smal
19231 6c 20 65 6e 6f 75 67 68 0a 2a 2a 20 74 6f 20 66  l enough.** to f
19232 69 74 20 69 6e 20 61 20 33 32 2d 62 69 74 20 69  it in a 32-bit i
19233 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20 31  nteger, return 1
19234 20 61 6e 64 20 70 75 74 20 74 68 65 20 76 61 6c   and put the val
19235 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65  ue of the intege
19236 72 0a 2a 2a 20 69 6e 20 2a 70 56 61 6c 75 65 2e  r.** in *pValue.
19237 20 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73    If the express
19238 69 6f 6e 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e  ion is not an in
19239 74 65 67 65 72 20 6f 72 20 69 66 20 69 74 20 69  teger or if it i
1923a 73 20 74 6f 6f 20 62 69 67 0a 2a 2a 20 74 6f 20  s too big.** to 
1923b 66 69 74 20 69 6e 20 61 20 73 69 67 6e 65 64 20  fit in a signed 
1923c 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 2c 20  32-bit integer, 
1923d 72 65 74 75 72 6e 20 30 20 61 6e 64 20 6c 65 61  return 0 and lea
1923e 76 65 20 2a 70 56 61 6c 75 65 20 75 6e 63 68 61  ve *pValue uncha
1923f 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  nged..*/.SQLITE_
19240 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
19241 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72  te3ExprIsInteger
19242 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 2a 70  (Expr *p, int *p
19243 56 61 6c 75 65 29 7b 0a 20 20 73 77 69 74 63 68  Value){.  switch
19244 28 20 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63  ( p->op ){.    c
19245 61 73 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20  ase TK_INTEGER: 
19246 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  {.      if( sqli
19247 74 65 33 47 65 74 49 6e 74 33 32 28 28 63 68 61  te3GetInt32((cha
19248 72 2a 29 70 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70  r*)p->token.z, p
19249 56 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 20 20  Value) ){.      
1924a 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
1924b 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
1924c 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1924d 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a 20 20 20 20  TK_UPLUS: {.    
1924e 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
1924f 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 2d  ExprIsInteger(p-
19250 3e 70 4c 65 66 74 2c 20 70 56 61 6c 75 65 29 3b  >pLeft, pValue);
19251 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
19252 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20  TK_UMINUS: {.   
19253 20 20 20 69 6e 74 20 76 3b 0a 20 20 20 20 20 20     int v;.      
19254 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49  if( sqlite3ExprI
19255 73 49 6e 74 65 67 65 72 28 70 2d 3e 70 4c 65 66  sInteger(p->pLef
19256 74 2c 20 26 76 29 20 29 7b 0a 20 20 20 20 20 20  t, &v) ){.      
19257 20 20 2a 70 56 61 6c 75 65 20 3d 20 2d 76 3b 0a    *pValue = -v;.
19258 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31          return 1
19259 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1925a 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1925b 20 64 65 66 61 75 6c 74 3a 20 62 72 65 61 6b 3b   default: break;
1925c 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
1925d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1925e 20 54 52 55 45 20 69 66 20 74 68 65 20 67 69 76   TRUE if the giv
1925f 65 6e 20 73 74 72 69 6e 67 20 69 73 20 61 20 72  en string is a r
19260 6f 77 2d 69 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d  ow-id column nam
19261 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
19262 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
19263 49 73 52 6f 77 69 64 28 63 6f 6e 73 74 20 63 68  IsRowid(const ch
19264 61 72 20 2a 7a 29 7b 0a 20 20 69 66 28 20 73 71  ar *z){.  if( sq
19265 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20  lite3StrICmp(z, 
19266 22 5f 52 4f 57 49 44 5f 22 29 3d 3d 30 20 29 20  "_ROWID_")==0 ) 
19267 72 65 74 75 72 6e 20 31 3b 0a 20 20 69 66 28 20  return 1;.  if( 
19268 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
19269 2c 20 22 52 4f 57 49 44 22 29 3d 3d 30 20 29 20  , "ROWID")==0 ) 
1926a 72 65 74 75 72 6e 20 31 3b 0a 20 20 69 66 28 20  return 1;.  if( 
1926b 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
1926c 2c 20 22 4f 49 44 22 29 3d 3d 30 20 29 20 72 65  , "OID")==0 ) re
1926d 74 75 72 6e 20 31 3b 0a 20 20 72 65 74 75 72 6e  turn 1;.  return
1926e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76   0;.}../*.** Giv
1926f 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61  en the name of a
19270 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 66   column of the f
19271 6f 72 6d 20 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a  orm X.Y.Z or Y.Z
19272 20 6f 72 20 6a 75 73 74 20 5a 2c 20 6c 6f 6f 6b   or just Z, look
19273 20 75 70 0a 2a 2a 20 74 68 61 74 20 6e 61 6d 65   up.** that name
19274 20 69 6e 20 74 68 65 20 73 65 74 20 6f 66 20 73   in the set of s
19275 6f 75 72 63 65 20 74 61 62 6c 65 73 20 69 6e 20  ource tables in 
19276 70 53 72 63 4c 69 73 74 20 61 6e 64 20 6d 61 6b  pSrcList and mak
19277 65 20 74 68 65 20 70 45 78 70 72 20 0a 2a 2a 20  e the pExpr .** 
19278 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20  expression node 
19279 72 65 66 65 72 20 62 61 63 6b 20 74 6f 20 74 68  refer back to th
1927a 61 74 20 73 6f 75 72 63 65 20 63 6f 6c 75 6d 6e  at source column
1927b 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
1927c 20 63 68 61 6e 67 65 73 0a 2a 2a 20 61 72 65 20   changes.** are 
1927d 6d 61 64 65 20 74 6f 20 70 45 78 70 72 3a 0a 2a  made to pExpr:.*
1927e 2a 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 69  *.**    pExpr->i
1927f 44 62 20 20 20 20 20 20 20 20 20 20 20 53 65 74  Db           Set
19280 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20 64 62   the index in db
19281 2d 3e 61 44 62 5b 5d 20 6f 66 20 74 68 65 20 64  ->aDb[] of the d
19282 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 0a  atabase holding.
19283 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
19284 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 74             the t
19285 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 70 45 78 70  able..**    pExp
19286 72 2d 3e 69 54 61 62 6c 65 20 20 20 20 20 20 20  r->iTable       
19287 20 53 65 74 20 74 6f 20 74 68 65 20 63 75 72 73   Set to the curs
19288 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  or number for th
19289 65 20 74 61 62 6c 65 20 6f 62 74 61 69 6e 65 64  e table obtained
1928a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1928b 20 20 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d              from
1928c 20 70 53 72 63 4c 69 73 74 2e 0a 2a 2a 20 20 20   pSrcList..**   
1928d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20   pExpr->iColumn 
1928e 20 20 20 20 20 20 53 65 74 20 74 6f 20 74 68 65        Set to the
1928f 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77   column number w
19290 69 74 68 69 6e 20 74 68 65 20 74 61 62 6c 65 2e  ithin the table.
19291 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70  .**    pExpr->op
19292 20 20 20 20 20 20 20 20 20 20 20 20 53 65 74 20              Set 
19293 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 2e 0a 2a 2a  to TK_COLUMN..**
19294 20 20 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 74      pExpr->pLeft
19295 20 20 20 20 20 20 20 20 20 41 6e 79 20 65 78 70           Any exp
19296 72 65 73 73 69 6f 6e 20 74 68 69 73 20 70 6f 69  ression this poi
19297 6e 74 73 20 74 6f 20 69 73 20 64 65 6c 65 74 65  nts to is delete
19298 64 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 70  d.**    pExpr->p
19299 52 69 67 68 74 20 20 20 20 20 20 20 20 41 6e 79  Right        Any
1929a 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 69 73   expression this
1929b 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20 64 65   points to is de
1929c 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  leted..**.** The
1929d 20 70 44 62 54 6f 6b 65 6e 20 69 73 20 74 68 65   pDbToken is the
1929e 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   name of the dat
1929f 61 62 61 73 65 20 28 74 68 65 20 22 58 22 29 2e  abase (the "X").
192a0 20 20 54 68 69 73 20 76 61 6c 75 65 20 6d 61 79    This value may
192a1 20 62 65 0a 2a 2a 20 4e 55 4c 4c 20 6d 65 61 6e   be.** NULL mean
192a2 69 6e 67 20 74 68 61 74 20 6e 61 6d 65 20 69 73  ing that name is
192a3 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 59 2e 5a   of the form Y.Z
192a4 20 6f 72 20 5a 2e 20 20 41 6e 79 20 61 76 61 69   or Z.  Any avai
192a5 6c 61 62 6c 65 20 64 61 74 61 62 61 73 65 0a 2a  lable database.*
192a6 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2e 20 20  * can be used.  
192a7 54 68 65 20 70 54 61 62 6c 65 54 6f 6b 65 6e 20  The pTableToken 
192a8 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
192a9 68 65 20 74 61 62 6c 65 20 28 74 68 65 20 22 59  he table (the "Y
192aa 22 29 2e 20 20 54 68 69 73 0a 2a 2a 20 76 61 6c  ").  This.** val
192ab 75 65 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20 69  ue can be NULL i
192ac 66 20 70 44 62 54 6f 6b 65 6e 20 69 73 20 61 6c  f pDbToken is al
192ad 73 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 70 54 61  so NULL.  If pTa
192ae 62 6c 65 54 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c  bleToken is NULL
192af 20 69 74 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61   it.** means tha
192b0 74 20 74 68 65 20 66 6f 72 6d 20 6f 66 20 74 68  t the form of th
192b1 65 20 6e 61 6d 65 20 69 73 20 5a 20 61 6e 64 20  e name is Z and 
192b2 74 68 61 74 20 63 6f 6c 75 6d 6e 73 20 66 72 6f  that columns fro
192b3 6d 20 61 6e 79 20 74 61 62 6c 65 0a 2a 2a 20 63  m any table.** c
192b4 61 6e 20 62 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a  an be used..**.*
192b5 2a 20 49 66 20 74 68 65 20 6e 61 6d 65 20 63 61  * If the name ca
192b6 6e 6e 6f 74 20 62 65 20 72 65 73 6f 6c 76 65 64  nnot be resolved
192b7 20 75 6e 61 6d 62 69 67 75 6f 75 73 6c 79 2c 20   unambiguously, 
192b8 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d  leave an error m
192b9 65 73 73 61 67 65 0a 2a 2a 20 69 6e 20 70 50 61  essage.** in pPa
192ba 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e 20 6e  rse and return n
192bb 6f 6e 2d 7a 65 72 6f 2e 20 20 52 65 74 75 72 6e  on-zero.  Return
192bc 20 7a 65 72 6f 20 6f 6e 20 73 75 63 63 65 73 73   zero on success
192bd 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
192be 6c 6f 6f 6b 75 70 4e 61 6d 65 28 0a 20 20 50 61  lookupName(.  Pa
192bf 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
192c0 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e     /* The parsin
192c1 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  g context */.  T
192c2 6f 6b 65 6e 20 2a 70 44 62 54 6f 6b 65 6e 2c 20  oken *pDbToken, 
192c3 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
192c4 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74  he database cont
192c5 61 69 6e 69 6e 67 20 74 61 62 6c 65 2c 20 6f 72  aining table, or
192c6 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e   NULL */.  Token
192c7 20 2a 70 54 61 62 6c 65 54 6f 6b 65 6e 2c 20 20   *pTableToken,  
192c8 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65  /* Name of table
192c9 20 63 6f 6e 74 61 69 6e 69 6e 67 20 63 6f 6c 75   containing colu
192ca 6d 6e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20  mn, or NULL */. 
192cb 20 54 6f 6b 65 6e 20 2a 70 43 6f 6c 75 6d 6e 54   Token *pColumnT
192cc 6f 6b 65 6e 2c 20 2f 2a 20 4e 61 6d 65 20 6f 66  oken, /* Name of
192cd 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a   the column. */.
192ce 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70    NameContext *p
192cf 4e 43 2c 20 20 20 20 2f 2a 20 54 68 65 20 6e 61  NC,    /* The na
192d0 6d 65 20 63 6f 6e 74 65 78 74 20 75 73 65 64 20  me context used 
192d1 74 6f 20 72 65 73 6f 6c 76 65 20 74 68 65 20 6e  to resolve the n
192d2 61 6d 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ame */.  Expr *p
192d3 45 78 70 72 20 20 20 20 20 20 20 20 20 20 2f 2a  Expr          /*
192d4 20 4d 61 6b 65 20 74 68 69 73 20 45 58 50 52 20   Make this EXPR 
192d5 6e 6f 64 65 20 70 6f 69 6e 74 20 74 6f 20 74 68  node point to th
192d6 65 20 73 65 6c 65 63 74 65 64 20 63 6f 6c 75 6d  e selected colum
192d7 6e 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a  n */.){.  char *
192d8 7a 44 62 20 3d 20 30 3b 20 20 20 20 20 20 20 2f  zDb = 0;       /
192d9 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  * Name of the da
192da 74 61 62 61 73 65 2e 20 20 54 68 65 20 22 58 22  tabase.  The "X"
192db 20 69 6e 20 58 2e 59 2e 5a 20 2a 2f 0a 20 20 63   in X.Y.Z */.  c
192dc 68 61 72 20 2a 7a 54 61 62 20 3d 20 30 3b 20 20  har *zTab = 0;  
192dd 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
192de 68 65 20 74 61 62 6c 65 2e 20 20 54 68 65 20 22  he table.  The "
192df 59 22 20 69 6e 20 58 2e 59 2e 5a 20 6f 72 20 59  Y" in X.Y.Z or Y
192e0 2e 5a 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43  .Z */.  char *zC
192e1 6f 6c 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20  ol = 0;      /* 
192e2 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75  Name of the colu
192e3 6d 6e 2e 20 20 54 68 65 20 22 5a 22 20 2a 2f 0a  mn.  The "Z" */.
192e4 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20    int i, j;     
192e5 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
192e6 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74  ounters */.  int
192e7 20 63 6e 74 20 3d 20 30 3b 20 20 20 20 20 20 20   cnt = 0;       
192e8 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d    /* Number of m
192e9 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 20 6e  atching column n
192ea 61 6d 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e  ames */.  int cn
192eb 74 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 2f  tTab = 0;      /
192ec 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61 74 63  * Number of matc
192ed 68 69 6e 67 20 74 61 62 6c 65 20 6e 61 6d 65 73  hing table names
192ee 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
192ef 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20  b = pParse->db; 
192f0 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
192f1 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63   */.  struct Src
192f2 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
192f3 3b 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 66  ;       /* Use f
192f4 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
192f5 70 53 72 63 4c 69 73 74 20 69 74 65 6d 73 20 2a  pSrcList items *
192f6 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  /.  struct SrcLi
192f7 73 74 5f 69 74 65 6d 20 2a 70 4d 61 74 63 68 20  st_item *pMatch 
192f8 3d 20 30 3b 20 20 2f 2a 20 54 68 65 20 6d 61 74  = 0;  /* The mat
192f9 63 68 69 6e 67 20 70 53 72 63 4c 69 73 74 20 69  ching pSrcList i
192fa 74 65 6d 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e  tem */.  NameCon
192fb 74 65 78 74 20 2a 70 54 6f 70 4e 43 20 3d 20 70  text *pTopNC = p
192fc 4e 43 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69  NC;        /* Fi
192fd 72 73 74 20 6e 61 6d 65 63 6f 6e 74 65 78 74 20  rst namecontext 
192fe 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20  in the list */. 
192ff 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61   Schema *pSchema
19300 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
19301 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 6f 66 20     /* Schema of 
19302 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  the expression *
19303 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43 6f  /..  assert( pCo
19304 6c 75 6d 6e 54 6f 6b 65 6e 20 26 26 20 70 43 6f  lumnToken && pCo
19305 6c 75 6d 6e 54 6f 6b 65 6e 2d 3e 7a 20 29 3b 20  lumnToken->z ); 
19306 2f 2a 20 54 68 65 20 5a 20 69 6e 20 58 2e 59 2e  /* The Z in X.Y.
19307 5a 20 63 61 6e 6e 6f 74 20 62 65 20 4e 55 4c 4c  Z cannot be NULL
19308 20 2a 2f 0a 20 20 7a 44 62 20 3d 20 73 71 6c 69   */.  zDb = sqli
19309 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
1930a 28 64 62 2c 20 70 44 62 54 6f 6b 65 6e 29 3b 0a  (db, pDbToken);.
1930b 20 20 7a 54 61 62 20 3d 20 73 71 6c 69 74 65 33    zTab = sqlite3
1930c 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62  NameFromToken(db
1930d 2c 20 70 54 61 62 6c 65 54 6f 6b 65 6e 29 3b 0a  , pTableToken);.
1930e 20 20 7a 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33    zCol = sqlite3
1930f 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62  NameFromToken(db
19310 2c 20 70 43 6f 6c 75 6d 6e 54 6f 6b 65 6e 29 3b  , pColumnToken);
19311 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
19312 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67  cFailed ){.    g
19313 6f 74 6f 20 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65  oto lookupname_e
19314 6e 64 3b 0a 20 20 7d 0a 0a 20 20 70 45 78 70 72  nd;.  }..  pExpr
19315 2d 3e 69 54 61 62 6c 65 20 3d 20 2d 31 3b 0a 20  ->iTable = -1;. 
19316 20 77 68 69 6c 65 28 20 70 4e 43 20 26 26 20 63   while( pNC && c
19317 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 45 78 70  nt==0 ){.    Exp
19318 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20  rList *pEList;. 
19319 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63     SrcList *pSrc
1931a 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63  List = pNC->pSrc
1931b 4c 69 73 74 3b 0a 0a 20 20 20 20 69 66 28 20 70  List;..    if( p
1931c 53 72 63 4c 69 73 74 20 29 7b 0a 20 20 20 20 20  SrcList ){.     
1931d 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d   for(i=0, pItem=
1931e 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20 69 3c 70  pSrcList->a; i<p
1931f 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69  SrcList->nSrc; i
19320 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  ++, pItem++){.  
19321 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61        Table *pTa
19322 62 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  b;.        int i
19323 44 62 3b 0a 20 20 20 20 20 20 20 20 43 6f 6c 75  Db;.        Colu
19324 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 0a 20 20 20  mn *pCol;.  .   
19325 20 20 20 20 20 70 54 61 62 20 3d 20 70 49 74 65       pTab = pIte
19326 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 20  m->pTab;.       
19327 20 61 73 73 65 72 74 28 20 70 54 61 62 21 3d 30   assert( pTab!=0
19328 20 29 3b 0a 20 20 20 20 20 20 20 20 69 44 62 20   );.        iDb 
19329 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
1932a 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d  oIndex(db, pTab-
1932b 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20  >pSchema);.     
1932c 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d     assert( pTab-
1932d 3e 6e 43 6f 6c 3e 30 20 29 3b 0a 20 20 20 20 20  >nCol>0 );.     
1932e 20 20 20 69 66 28 20 7a 54 61 62 20 29 7b 0a 20     if( zTab ){. 
1932f 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 74           if( pIt
19330 65 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a 20 20  em->zAlias ){.  
19331 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
19332 7a 54 61 62 4e 61 6d 65 20 3d 20 70 49 74 65 6d  zTabName = pItem
19333 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20 20 20 20 20  ->zAlias;.      
19334 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
19335 33 53 74 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d  3StrICmp(zTabNam
19336 65 2c 20 7a 54 61 62 29 21 3d 30 20 29 20 63 6f  e, zTab)!=0 ) co
19337 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20  ntinue;.        
19338 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
19339 20 20 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e       char *zTabN
1933a 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d  ame = pTab->zNam
1933b 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  e;.            i
1933c 66 28 20 7a 54 61 62 4e 61 6d 65 3d 3d 30 20 7c  f( zTabName==0 |
1933d 7c 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  | sqlite3StrICmp
1933e 28 7a 54 61 62 4e 61 6d 65 2c 20 7a 54 61 62 29  (zTabName, zTab)
1933f 21 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  !=0 ) continue;.
19340 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
19341 7a 44 62 21 3d 30 20 26 26 20 73 71 6c 69 74 65  zDb!=0 && sqlite
19342 33 53 74 72 49 43 6d 70 28 64 62 2d 3e 61 44 62  3StrICmp(db->aDb
19343 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 44 62  [iDb].zName, zDb
19344 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
19345 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
19346 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
19347 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
19348 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
19349 30 3d 3d 28 63 6e 74 54 61 62 2b 2b 29 20 29 7b  0==(cntTab++) ){
1934a 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72  .          pExpr
1934b 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49 74 65 6d  ->iTable = pItem
1934c 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 20  ->iCursor;.     
1934d 20 20 20 20 20 70 53 63 68 65 6d 61 20 3d 20 70       pSchema = p
1934e 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20  Tab->pSchema;.  
1934f 20 20 20 20 20 20 20 20 70 4d 61 74 63 68 20 3d          pMatch =
19350 20 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 20 20   pItem;.        
19351 7d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d  }.        for(j=
19352 30 2c 20 70 43 6f 6c 3d 70 54 61 62 2d 3e 61 43  0, pCol=pTab->aC
19353 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c  ol; j<pTab->nCol
19354 3b 20 6a 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a  ; j++, pCol++){.
19355 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71            if( sq
19356 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f  lite3StrICmp(pCo
19357 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d  l->zName, zCol)=
19358 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
19359 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43    const char *zC
1935a 6f 6c 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c  oll = pTab->aCol
1935b 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20  [j].zColl;.     
1935c 20 20 20 20 20 20 20 49 64 4c 69 73 74 20 2a 70         IdList *p
1935d 55 73 69 6e 67 3b 0a 20 20 20 20 20 20 20 20 20  Using;.         
1935e 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20     cnt++;.      
1935f 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61        pExpr->iTa
19360 62 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75  ble = pItem->iCu
19361 72 73 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20  rsor;.          
19362 20 20 70 4d 61 74 63 68 20 3d 20 70 49 74 65 6d    pMatch = pItem
19363 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 53  ;.            pS
19364 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53  chema = pTab->pS
19365 63 68 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 20  chema;.         
19366 20 20 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65     /* Substitute
19367 20 74 68 65 20 72 6f 77 69 64 20 28 63 6f 6c 75   the rowid (colu
19368 6d 6e 20 2d 31 29 20 66 6f 72 20 74 68 65 20 49  mn -1) for the I
19369 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
1936a 45 59 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  EY */.          
1936b 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e    pExpr->iColumn
1936c 20 3d 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65   = j==pTab->iPKe
1936d 79 20 3f 20 2d 31 20 3a 20 6a 3b 0a 20 20 20 20  y ? -1 : j;.    
1936e 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61          pExpr->a
1936f 66 66 69 6e 69 74 79 20 3d 20 70 54 61 62 2d 3e  ffinity = pTab->
19370 61 43 6f 6c 5b 6a 5d 2e 61 66 66 69 6e 69 74 79  aCol[j].affinity
19371 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  ;.            if
19372 28 20 28 70 45 78 70 72 2d 3e 66 6c 61 67 73 20  ( (pExpr->flags 
19373 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 29  & EP_ExpCollate)
19374 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
19375 20 20 20 20 20 70 45 78 70 72 2d 3e 70 43 6f 6c       pExpr->pCol
19376 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43  l = sqlite3FindC
19377 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64  ollSeq(db, ENC(d
19378 62 29 2c 20 7a 43 6f 6c 6c 2c 2d 31 2c 20 30 29  b), zColl,-1, 0)
19379 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
1937a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
1937b 69 3c 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63  i<pSrcList->nSrc
1937c 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  -1 ){.          
1937d 20 20 20 20 69 66 28 20 70 49 74 65 6d 5b 31 5d      if( pItem[1]
1937e 2e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4e  .jointype & JT_N
1937f 41 54 55 52 41 4c 20 29 7b 0a 20 20 20 20 20 20  ATURAL ){.      
19380 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20            /* If 
19381 74 68 69 73 20 6d 61 74 63 68 20 6f 63 63 75 72  this match occur
19382 72 65 64 20 69 6e 20 74 68 65 20 6c 65 66 74 20  red in the left 
19383 74 61 62 6c 65 20 6f 66 20 61 20 6e 61 74 75 72  table of a natur
19384 61 6c 20 6a 6f 69 6e 2c 0a 20 20 20 20 20 20 20  al join,.       
19385 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e           ** then
19386 20 73 6b 69 70 20 74 68 65 20 72 69 67 68 74 20   skip the right 
19387 74 61 62 6c 65 20 74 6f 20 61 76 6f 69 64 20 61  table to avoid a
19388 20 64 75 70 6c 69 63 61 74 65 20 6d 61 74 63 68   duplicate match
19389 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
1938a 20 20 20 20 70 49 74 65 6d 2b 2b 3b 0a 20 20 20      pItem++;.   
1938b 20 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b               i++
1938c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
1938d 7d 65 6c 73 65 20 69 66 28 20 28 70 55 73 69 6e  }else if( (pUsin
1938e 67 20 3d 20 70 49 74 65 6d 5b 31 5d 2e 70 55 73  g = pItem[1].pUs
1938f 69 6e 67 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ing)!=0 ){.     
19390 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
19391 20 74 68 69 73 20 6d 61 74 63 68 20 6f 63 63 75   this match occu
19392 72 73 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20 74  rs on a column t
19393 68 61 74 20 69 73 20 69 6e 20 74 68 65 20 55 53  hat is in the US
19394 49 4e 47 20 63 6c 61 75 73 65 0a 20 20 20 20 20  ING clause.     
19395 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 66             ** of
19396 20 61 20 6a 6f 69 6e 2c 20 73 6b 69 70 20 74 68   a join, skip th
19397 65 20 73 65 61 72 63 68 20 6f 66 20 74 68 65 20  e search of the 
19398 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20 74  right table of t
19399 68 65 20 6a 6f 69 6e 0a 20 20 20 20 20 20 20 20  he join.        
1939a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 61 76          ** to av
1939b 6f 69 64 20 61 20 64 75 70 6c 69 63 61 74 65 20  oid a duplicate 
1939c 6d 61 74 63 68 20 74 68 65 72 65 2e 20 2a 2f 0a  match there. */.
1939d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1939e 69 6e 74 20 6b 3b 0a 20 20 20 20 20 20 20 20 20  int k;.         
1939f 20 20 20 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20         for(k=0; 
193a0 6b 3c 70 55 73 69 6e 67 2d 3e 6e 49 64 3b 20 6b  k<pUsing->nId; k
193a1 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ++){.           
193a2 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
193a3 65 33 53 74 72 49 43 6d 70 28 70 55 73 69 6e 67  e3StrICmp(pUsing
193a4 2d 3e 61 5b 6b 5d 2e 7a 4e 61 6d 65 2c 20 7a 43  ->a[k].zName, zC
193a5 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ol)==0 ){.      
193a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49                pI
193a7 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20  tem++;.         
193a8 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a             i++;.
193a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
193aa 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
193ab 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
193ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d                 }
193ad 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
193ae 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
193af 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
193b0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
193b1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
193b2 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53      }..#ifndef S
193b3 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
193b4 45 52 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20  ER.    /* If we 
193b5 68 61 76 65 20 6e 6f 74 20 61 6c 72 65 61 64 79  have not already
193b6 20 72 65 73 6f 6c 76 65 64 20 74 68 65 20 6e 61   resolved the na
193b7 6d 65 2c 20 74 68 65 6e 20 6d 61 79 62 65 20 0a  me, then maybe .
193b8 20 20 20 20 2a 2a 20 69 74 20 69 73 20 61 20 6e      ** it is a n
193b9 65 77 2e 2a 20 6f 72 20 6f 6c 64 2e 2a 20 74 72  ew.* or old.* tr
193ba 69 67 67 65 72 20 61 72 67 75 6d 65 6e 74 20 72  igger argument r
193bb 65 66 65 72 65 6e 63 65 0a 20 20 20 20 2a 2f 0a  eference.    */.
193bc 20 20 20 20 69 66 28 20 7a 44 62 3d 3d 30 20 26      if( zDb==0 &
193bd 26 20 7a 54 61 62 21 3d 30 20 26 26 20 63 6e 74  & zTab!=0 && cnt
193be 3d 3d 30 20 26 26 20 70 50 61 72 73 65 2d 3e 74  ==0 && pParse->t
193bf 72 69 67 53 74 61 63 6b 21 3d 30 20 29 7b 0a 20  rigStack!=0 ){. 
193c0 20 20 20 20 20 54 72 69 67 67 65 72 53 74 61 63       TriggerStac
193c1 6b 20 2a 70 54 72 69 67 67 65 72 53 74 61 63 6b  k *pTriggerStack
193c2 20 3d 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53   = pParse->trigS
193c3 74 61 63 6b 3b 0a 20 20 20 20 20 20 54 61 62 6c  tack;.      Tabl
193c4 65 20 2a 70 54 61 62 20 3d 20 30 3b 0a 20 20 20  e *pTab = 0;.   
193c5 20 20 20 75 33 32 20 2a 70 69 43 6f 6c 4d 61 73     u32 *piColMas
193c6 6b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 72  k;.      if( pTr
193c7 69 67 67 65 72 53 74 61 63 6b 2d 3e 6e 65 77 49  iggerStack->newI
193c8 64 78 20 21 3d 20 2d 31 20 26 26 20 73 71 6c 69  dx != -1 && sqli
193c9 74 65 33 53 74 72 49 43 6d 70 28 22 6e 65 77 22  te3StrICmp("new"
193ca 2c 20 7a 54 61 62 29 20 3d 3d 20 30 20 29 7b 0a  , zTab) == 0 ){.
193cb 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69          pExpr->i
193cc 54 61 62 6c 65 20 3d 20 70 54 72 69 67 67 65 72  Table = pTrigger
193cd 53 74 61 63 6b 2d 3e 6e 65 77 49 64 78 3b 0a 20  Stack->newIdx;. 
193ce 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
193cf 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 70 54  TriggerStack->pT
193d0 61 62 20 29 3b 0a 20 20 20 20 20 20 20 20 70 54  ab );.        pT
193d1 61 62 20 3d 20 70 54 72 69 67 67 65 72 53 74 61  ab = pTriggerSta
193d2 63 6b 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20  ck->pTab;.      
193d3 20 20 70 69 43 6f 6c 4d 61 73 6b 20 3d 20 26 28    piColMask = &(
193d4 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 6e  pTriggerStack->n
193d5 65 77 43 6f 6c 4d 61 73 6b 29 3b 0a 20 20 20 20  ewColMask);.    
193d6 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54 72 69    }else if( pTri
193d7 67 67 65 72 53 74 61 63 6b 2d 3e 6f 6c 64 49 64  ggerStack->oldId
193d8 78 20 21 3d 20 2d 31 20 26 26 20 73 71 6c 69 74  x != -1 && sqlit
193d9 65 33 53 74 72 49 43 6d 70 28 22 6f 6c 64 22 2c  e3StrICmp("old",
193da 20 7a 54 61 62 29 3d 3d 30 20 29 7b 0a 20 20 20   zTab)==0 ){.   
193db 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62       pExpr->iTab
193dc 6c 65 20 3d 20 70 54 72 69 67 67 65 72 53 74 61  le = pTriggerSta
193dd 63 6b 2d 3e 6f 6c 64 49 64 78 3b 0a 20 20 20 20  ck->oldIdx;.    
193de 20 20 20 20 61 73 73 65 72 74 28 20 70 54 72 69      assert( pTri
193df 67 67 65 72 53 74 61 63 6b 2d 3e 70 54 61 62 20  ggerStack->pTab 
193e0 29 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20  );.        pTab 
193e1 3d 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d  = pTriggerStack-
193e2 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 70  >pTab;.        p
193e3 69 43 6f 6c 4d 61 73 6b 20 3d 20 26 28 70 54 72  iColMask = &(pTr
193e4 69 67 67 65 72 53 74 61 63 6b 2d 3e 6f 6c 64 43  iggerStack->oldC
193e5 6f 6c 4d 61 73 6b 29 3b 0a 20 20 20 20 20 20 7d  olMask);.      }
193e6 0a 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62  ..      if( pTab
193e7 20 29 7b 20 0a 20 20 20 20 20 20 20 20 69 6e 74   ){ .        int
193e8 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 43   iCol;.        C
193e9 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 70 54  olumn *pCol = pT
193ea 61 62 2d 3e 61 43 6f 6c 3b 0a 0a 20 20 20 20 20  ab->aCol;..     
193eb 20 20 20 70 53 63 68 65 6d 61 20 3d 20 70 54 61     pSchema = pTa
193ec 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20  b->pSchema;.    
193ed 20 20 20 20 63 6e 74 54 61 62 2b 2b 3b 0a 20 20      cntTab++;.  
193ee 20 20 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30        for(iCol=0
193ef 3b 20 69 43 6f 6c 20 3c 20 70 54 61 62 2d 3e 6e  ; iCol < pTab->n
193f0 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 2c 20 70 43 6f  Col; iCol++, pCo
193f1 6c 2b 2b 29 20 7b 0a 20 20 20 20 20 20 20 20 20  l++) {.         
193f2 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
193f3 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c  Cmp(pCol->zName,
193f4 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20   zCol)==0 ){.   
193f5 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
193f6 68 61 72 20 2a 7a 43 6f 6c 6c 20 3d 20 70 54 61  har *zColl = pTa
193f7 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 43  b->aCol[iCol].zC
193f8 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 20  oll;.           
193f9 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20   cnt++;.        
193fa 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75      pExpr->iColu
193fb 6d 6e 20 3d 20 69 43 6f 6c 3d 3d 70 54 61 62 2d  mn = iCol==pTab-
193fc 3e 69 50 4b 65 79 20 3f 20 2d 31 20 3a 20 69 43  >iPKey ? -1 : iC
193fd 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ol;.            
193fe 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20  pExpr->affinity 
193ff 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f  = pTab->aCol[iCo
19400 6c 5d 2e 61 66 66 69 6e 69 74 79 3b 0a 20 20 20  l].affinity;.   
19401 20 20 20 20 20 20 20 20 20 69 66 28 20 28 70 45           if( (pE
19402 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  xpr->flags & EP_
19403 45 78 70 43 6f 6c 6c 61 74 65 29 3d 3d 30 20 29  ExpCollate)==0 )
19404 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
19405 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20 73  pExpr->pColl = s
19406 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65  qlite3FindCollSe
19407 71 28 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 7a  q(db, ENC(db), z
19408 43 6f 6c 6c 2c 2d 31 2c 20 30 29 3b 0a 20 20 20  Coll,-1, 0);.   
19409 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1940a 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 54         pExpr->pT
1940b 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20 20 20  ab = pTab;.     
1940c 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3e         if( iCol>
1940d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
1940e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 43      testcase( iC
1940f 6f 6c 3d 3d 33 31 20 29 3b 0a 20 20 20 20 20 20  ol==31 );.      
19410 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
19411 28 20 69 43 6f 6c 3d 3d 33 32 20 29 3b 0a 20 20  ( iCol==32 );.  
19412 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 69 43              *piC
19413 6f 6c 4d 61 73 6b 20 7c 3d 20 28 28 75 33 32 29  olMask |= ((u32)
19414 31 3c 3c 69 43 6f 6c 29 20 7c 20 28 69 43 6f 6c  1<<iCol) | (iCol
19415 3e 3d 33 32 3f 30 78 66 66 66 66 66 66 66 66 3a  >=32?0xffffffff:
19416 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  0);.            
19417 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72  }.            br
19418 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  eak;.          }
19419 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1941a 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20   }.    }.#endif 
1941b 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
1941c 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29  TE_OMIT_TRIGGER)
1941d 20 2a 2f 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20   */..    /*.    
1941e 2a 2a 20 50 65 72 68 61 70 73 20 74 68 65 20 6e  ** Perhaps the n
1941f 61 6d 65 20 69 73 20 61 20 72 65 66 65 72 65 6e  ame is a referen
19420 63 65 20 74 6f 20 74 68 65 20 52 4f 57 49 44 0a  ce to the ROWID.
19421 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63      */.    if( c
19422 6e 74 3d 3d 30 20 26 26 20 63 6e 74 54 61 62 3d  nt==0 && cntTab=
19423 3d 31 20 26 26 20 73 71 6c 69 74 65 33 49 73 52  =1 && sqlite3IsR
19424 6f 77 69 64 28 7a 43 6f 6c 29 20 29 7b 0a 20 20  owid(zCol) ){.  
19425 20 20 20 20 63 6e 74 20 3d 20 31 3b 0a 20 20 20      cnt = 1;.   
19426 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d     pExpr->iColum
19427 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 70 45  n = -1;.      pE
19428 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20  xpr->affinity = 
19429 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47  SQLITE_AFF_INTEG
1942a 45 52 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  ER;.    }..    /
1942b 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20  *.    ** If the 
1942c 69 6e 70 75 74 20 69 73 20 6f 66 20 74 68 65 20  input is of the 
1942d 66 6f 72 6d 20 5a 20 28 6e 6f 74 20 59 2e 5a 20  form Z (not Y.Z 
1942e 6f 72 20 58 2e 59 2e 5a 29 20 74 68 65 6e 20 74  or X.Y.Z) then t
1942f 68 65 20 6e 61 6d 65 20 5a 0a 20 20 20 20 2a 2a  he name Z.    **
19430 20 6d 69 67 68 74 20 72 65 66 65 72 20 74 6f 20   might refer to 
19431 61 6e 20 72 65 73 75 6c 74 2d 73 65 74 20 61 6c  an result-set al
19432 69 61 73 2e 20 20 54 68 69 73 20 68 61 70 70 65  ias.  This happe
19433 6e 73 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c  ns, for example,
19434 20 77 68 65 6e 0a 20 20 20 20 2a 2a 20 77 65 20   when.    ** we 
19435 61 72 65 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61  are resolving na
19436 6d 65 73 20 69 6e 20 74 68 65 20 57 48 45 52 45  mes in the WHERE
19437 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 66   clause of the f
19438 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d 6d 61 6e 64  ollowing command
19439 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  :.    **.    ** 
1943a 20 20 20 20 53 45 4c 45 43 54 20 61 2b 62 20 41      SELECT a+b A
1943b 53 20 78 20 46 52 4f 4d 20 74 61 62 6c 65 20 57  S x FROM table W
1943c 48 45 52 45 20 78 3c 31 30 3b 0a 20 20 20 20 2a  HERE x<10;.    *
1943d 2a 0a 20 20 20 20 2a 2a 20 49 6e 20 63 61 73 65  *.    ** In case
1943e 73 20 6c 69 6b 65 20 74 68 69 73 2c 20 72 65 70  s like this, rep
1943f 6c 61 63 65 20 70 45 78 70 72 20 77 69 74 68 20  lace pExpr with 
19440 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65 78  a copy of the ex
19441 70 72 65 73 73 69 6f 6e 20 74 68 61 74 0a 20 20  pression that.  
19442 20 20 2a 2a 20 66 6f 72 6d 73 20 74 68 65 20 72    ** forms the r
19443 65 73 75 6c 74 20 73 65 74 20 65 6e 74 72 79 20  esult set entry 
19444 28 22 61 2b 62 22 20 69 6e 20 74 68 65 20 65 78  ("a+b" in the ex
19445 61 6d 70 6c 65 29 20 61 6e 64 20 72 65 74 75 72  ample) and retur
19446 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20  n immediately.. 
19447 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20     ** Note that 
19448 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
19449 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
1944a 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61 6c 72   should have alr
1944b 65 61 64 79 20 62 65 65 6e 0a 20 20 20 20 2a 2a  eady been.    **
1944c 20 72 65 73 6f 6c 76 65 64 20 62 79 20 74 68 65   resolved by the
1944d 20 74 69 6d 65 20 74 68 65 20 57 48 45 52 45 20   time the WHERE 
1944e 63 6c 61 75 73 65 20 69 73 20 72 65 73 6f 6c 76  clause is resolv
1944f 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ed..    */.    i
19450 66 28 20 63 6e 74 3d 3d 30 20 26 26 20 28 70 45  f( cnt==0 && (pE
19451 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 45 4c 69  List = pNC->pELi
19452 73 74 29 21 3d 30 20 26 26 20 7a 54 61 62 3d 3d  st)!=0 && zTab==
19453 30 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6a  0 ){.      for(j
19454 3d 30 3b 20 6a 3c 70 45 4c 69 73 74 2d 3e 6e 45  =0; j<pEList->nE
19455 78 70 72 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  xpr; j++){.     
19456 20 20 20 63 68 61 72 20 2a 7a 41 73 20 3d 20 70     char *zAs = p
19457 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 7a 4e 61 6d  EList->a[j].zNam
19458 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a  e;.        if( z
19459 41 73 21 3d 30 20 26 26 20 73 71 6c 69 74 65 33  As!=0 && sqlite3
1945a 53 74 72 49 43 6d 70 28 7a 41 73 2c 20 7a 43 6f  StrICmp(zAs, zCo
1945b 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  l)==0 ){.       
1945c 20 20 20 45 78 70 72 20 2a 70 44 75 70 2c 20 2a     Expr *pDup, *
1945d 70 4f 72 69 67 3b 0a 20 20 20 20 20 20 20 20 20  pOrig;.         
1945e 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
1945f 70 4c 65 66 74 3d 3d 30 20 26 26 20 70 45 78 70  pLeft==0 && pExp
19460 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a  r->pRight==0 );.
19461 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
19462 28 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3d 3d  ( pExpr->pList==
19463 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61  0 );.          a
19464 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 53  ssert( pExpr->pS
19465 65 6c 65 63 74 3d 3d 30 20 29 3b 0a 20 20 20 20  elect==0 );.    
19466 20 20 20 20 20 20 70 4f 72 69 67 20 3d 20 70 45        pOrig = pE
19467 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72  List->a[j].pExpr
19468 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
19469 21 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 26  !pNC->allowAgg &
1946a 26 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  & ExprHasPropert
1946b 79 28 70 4f 72 69 67 2c 20 45 50 5f 41 67 67 29  y(pOrig, EP_Agg)
1946c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1946d 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1946e 70 50 61 72 73 65 2c 20 22 6d 69 73 75 73 65 20  pParse, "misuse 
1946f 6f 66 20 61 6c 69 61 73 65 64 20 61 67 67 72 65  of aliased aggre
19470 67 61 74 65 20 25 73 22 2c 20 7a 41 73 29 3b 0a  gate %s", zAs);.
19471 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
19472 74 65 33 5f 66 72 65 65 28 7a 43 6f 6c 29 3b 0a  te3_free(zCol);.
19473 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75              retu
19474 72 6e 20 32 3b 0a 20 20 20 20 20 20 20 20 20 20  rn 2;.          
19475 7d 0a 20 20 20 20 20 20 20 20 20 20 70 44 75 70  }.          pDup
19476 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
19477 70 28 64 62 2c 20 70 4f 72 69 67 29 3b 0a 20 20  p(db, pOrig);.  
19478 20 20 20 20 20 20 20 20 69 66 28 20 70 45 78 70          if( pExp
19479 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78  r->flags & EP_Ex
1947a 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20  pCollate ){.    
1947b 20 20 20 20 20 20 20 20 70 44 75 70 2d 3e 70 43          pDup->pC
1947c 6f 6c 6c 20 3d 20 70 45 78 70 72 2d 3e 70 43 6f  oll = pExpr->pCo
1947d 6c 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ll;.            
1947e 70 44 75 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 45  pDup->flags |= E
1947f 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20  P_ExpCollate;.  
19480 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
19481 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 73      if( pExpr->s
19482 70 61 6e 2e 64 79 6e 20 29 20 73 71 6c 69 74 65  pan.dyn ) sqlite
19483 33 5f 66 72 65 65 28 28 63 68 61 72 2a 29 70 45  3_free((char*)pE
19484 78 70 72 2d 3e 73 70 61 6e 2e 7a 29 3b 0a 20 20  xpr->span.z);.  
19485 20 20 20 20 20 20 20 20 69 66 28 20 70 45 78 70          if( pExp
19486 72 2d 3e 74 6f 6b 65 6e 2e 64 79 6e 20 29 20 73  r->token.dyn ) s
19487 71 6c 69 74 65 33 5f 66 72 65 65 28 28 63 68 61  qlite3_free((cha
19488 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e  r*)pExpr->token.
19489 7a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6d 65  z);.          me
1948a 6d 63 70 79 28 70 45 78 70 72 2c 20 70 44 75 70  mcpy(pExpr, pDup
1948b 2c 20 73 69 7a 65 6f 66 28 2a 70 45 78 70 72 29  , sizeof(*pExpr)
1948c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
1948d 69 74 65 33 5f 66 72 65 65 28 70 44 75 70 29 3b  ite3_free(pDup);
1948e 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 20 3d  .          cnt =
1948f 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4d   1;.          pM
19490 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 20 20  atch = 0;.      
19491 20 20 20 20 61 73 73 65 72 74 28 20 7a 54 61 62      assert( zTab
19492 3d 3d 30 20 26 26 20 7a 44 62 3d 3d 30 20 29 3b  ==0 && zDb==0 );
19493 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
19494 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 5f 32  lookupname_end_2
19495 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
19496 20 20 7d 20 0a 20 20 20 20 7d 0a 0a 20 20 20 20    } .    }..    
19497 2f 2a 20 41 64 76 61 6e 63 65 20 74 6f 20 74 68  /* Advance to th
19498 65 20 6e 65 78 74 20 6e 61 6d 65 20 63 6f 6e 74  e next name cont
19499 65 78 74 2e 20 20 54 68 65 20 6c 6f 6f 70 20 77  ext.  The loop w
1949a 69 6c 6c 20 65 78 69 74 20 77 68 65 6e 20 65 69  ill exit when ei
1949b 74 68 65 72 0a 20 20 20 20 2a 2a 20 77 65 20 68  ther.    ** we h
1949c 61 76 65 20 61 20 6d 61 74 63 68 20 28 63 6e 74  ave a match (cnt
1949d 3e 30 29 20 6f 72 20 77 68 65 6e 20 77 65 20 72  >0) or when we r
1949e 75 6e 20 6f 75 74 20 6f 66 20 6e 61 6d 65 20 63  un out of name c
1949f 6f 6e 74 65 78 74 73 2e 0a 20 20 20 20 2a 2f 0a  ontexts..    */.
194a0 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 29      if( cnt==0 )
194a1 7b 0a 20 20 20 20 20 20 70 4e 43 20 3d 20 70 4e  {.      pNC = pN
194a2 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a  C->pNext;.    }.
194a3 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 49    }..  /*.  ** I
194a4 66 20 58 20 61 6e 64 20 59 20 61 72 65 20 4e 55  f X and Y are NU
194a5 4c 4c 20 28 69 6e 20 6f 74 68 65 72 20 77 6f 72  LL (in other wor
194a6 64 73 20 69 66 20 6f 6e 6c 79 20 74 68 65 20 63  ds if only the c
194a7 6f 6c 75 6d 6e 20 6e 61 6d 65 20 5a 20 69 73 0a  olumn name Z is.
194a8 20 20 2a 2a 20 73 75 70 70 6c 69 65 64 29 20 61    ** supplied) a
194a9 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  nd the value of 
194aa 5a 20 69 73 20 65 6e 63 6c 6f 73 65 64 20 69 6e  Z is enclosed in
194ab 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 73 2c 20   double-quotes, 
194ac 74 68 65 6e 0a 20 20 2a 2a 20 5a 20 69 73 20 61  then.  ** Z is a
194ad 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 20   string literal 
194ae 69 66 20 69 74 20 64 6f 65 73 6e 27 74 20 6d 61  if it doesn't ma
194af 74 63 68 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 6e  tch any column n
194b0 61 6d 65 73 2e 20 20 49 6e 20 74 68 61 74 0a 20  ames.  In that. 
194b1 20 2a 2a 20 63 61 73 65 2c 20 77 65 20 6e 65 65   ** case, we nee
194b2 64 20 74 6f 20 72 65 74 75 72 6e 20 72 69 67 68  d to return righ
194b3 74 20 61 77 61 79 20 61 6e 64 20 6e 6f 74 20 6d  t away and not m
194b4 61 6b 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20  ake any changes 
194b5 74 6f 0a 20 20 2a 2a 20 70 45 78 70 72 2e 0a 20  to.  ** pExpr.. 
194b6 20 2a 2a 0a 20 20 2a 2a 20 42 65 63 61 75 73 65   **.  ** Because
194b7 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 20 77 61   no reference wa
194b8 73 20 6d 61 64 65 20 74 6f 20 6f 75 74 65 72 20  s made to outer 
194b9 63 6f 6e 74 65 78 74 73 2c 20 74 68 65 20 70 4e  contexts, the pN
194ba 43 2d 3e 6e 52 65 66 0a 20 20 2a 2a 20 66 69 65  C->nRef.  ** fie
194bb 6c 64 73 20 61 72 65 20 6e 6f 74 20 63 68 61 6e  lds are not chan
194bc 67 65 64 20 69 6e 20 61 6e 79 20 63 6f 6e 74 65  ged in any conte
194bd 78 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63  xt..  */.  if( c
194be 6e 74 3d 3d 30 20 26 26 20 7a 54 61 62 3d 3d 30  nt==0 && zTab==0
194bf 20 26 26 20 70 43 6f 6c 75 6d 6e 54 6f 6b 65 6e   && pColumnToken
194c0 2d 3e 7a 5b 30 5d 3d 3d 27 22 27 20 29 7b 0a 20  ->z[0]=='"' ){. 
194c1 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
194c2 7a 43 6f 6c 29 3b 0a 20 20 20 20 72 65 74 75 72  zCol);.    retur
194c3 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20  n 0;.  }..  /*. 
194c4 20 2a 2a 20 63 6e 74 3d 3d 30 20 6d 65 61 6e 73   ** cnt==0 means
194c5 20 74 68 65 72 65 20 77 61 73 20 6e 6f 74 20 6d   there was not m
194c6 61 74 63 68 2e 20 20 63 6e 74 3e 31 20 6d 65 61  atch.  cnt>1 mea
194c7 6e 73 20 74 68 65 72 65 20 77 65 72 65 20 74 77  ns there were tw
194c8 6f 20 6f 72 0a 20 20 2a 2a 20 6d 6f 72 65 20 6d  o or.  ** more m
194c9 61 74 63 68 65 73 2e 20 20 45 69 74 68 65 72 20  atches.  Either 
194ca 77 61 79 2c 20 77 65 20 68 61 76 65 20 61 6e 20  way, we have an 
194cb 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66  error..  */.  if
194cc 28 20 63 6e 74 21 3d 31 20 29 7b 0a 20 20 20 20  ( cnt!=1 ){.    
194cd 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 72 72  const char *zErr
194ce 3b 0a 20 20 20 20 7a 45 72 72 20 3d 20 63 6e 74  ;.    zErr = cnt
194cf 3d 3d 30 20 3f 20 22 6e 6f 20 73 75 63 68 20 63  ==0 ? "no such c
194d0 6f 6c 75 6d 6e 22 20 3a 20 22 61 6d 62 69 67 75  olumn" : "ambigu
194d1 6f 75 73 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 22  ous column name"
194d2 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 20 29 7b  ;.    if( zDb ){
194d3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
194d4 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
194d5 25 73 3a 20 25 73 2e 25 73 2e 25 73 22 2c 20 7a  %s: %s.%s.%s", z
194d6 45 72 72 2c 20 7a 44 62 2c 20 7a 54 61 62 2c 20  Err, zDb, zTab, 
194d7 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65  zCol);.    }else
194d8 20 69 66 28 20 7a 54 61 62 20 29 7b 0a 20 20 20   if( zTab ){.   
194d9 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
194da 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20  sg(pParse, "%s: 
194db 25 73 2e 25 73 22 2c 20 7a 45 72 72 2c 20 7a 54  %s.%s", zErr, zT
194dc 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d  ab, zCol);.    }
194dd 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
194de 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
194df 73 65 2c 20 22 25 73 3a 20 25 73 22 2c 20 7a 45  se, "%s: %s", zE
194e0 72 72 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d  rr, zCol);.    }
194e1 0a 20 20 20 20 70 54 6f 70 4e 43 2d 3e 6e 45 72  .    pTopNC->nEr
194e2 72 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  r++;.  }..  /* I
194e3 66 20 61 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20  f a column from 
194e4 61 20 74 61 62 6c 65 20 69 6e 20 70 53 72 63 4c  a table in pSrcL
194e5 69 73 74 20 69 73 20 72 65 66 65 72 65 6e 63 65  ist is reference
194e6 64 2c 20 74 68 65 6e 20 72 65 63 6f 72 64 0a 20  d, then record. 
194e7 20 2a 2a 20 74 68 69 73 20 66 61 63 74 20 69 6e   ** this fact in
194e8 20 74 68 65 20 70 53 72 63 4c 69 73 74 2e 61 5b   the pSrcList.a[
194e9 5d 2e 63 6f 6c 55 73 65 64 20 62 69 74 6d 61 73  ].colUsed bitmas
194ea 6b 2e 20 20 43 6f 6c 75 6d 6e 20 30 20 63 61 75  k.  Column 0 cau
194eb 73 65 73 0a 20 20 2a 2a 20 62 69 74 20 30 20 74  ses.  ** bit 0 t
194ec 6f 20 62 65 20 73 65 74 2e 20 20 43 6f 6c 75 6d  o be set.  Colum
194ed 6e 20 31 20 73 65 74 73 20 62 69 74 20 31 2e 20  n 1 sets bit 1. 
194ee 20 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20   And so forth.  
194ef 49 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6c 75  If the.  ** colu
194f0 6d 6e 20 6e 75 6d 62 65 72 20 69 73 20 67 72 65  mn number is gre
194f1 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 6e 75  ater than the nu
194f2 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69 6e 20  mber of bits in 
194f3 74 68 65 20 62 69 74 6d 61 73 6b 0a 20 20 2a 2a  the bitmask.  **
194f4 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 68 69   then set the hi
194f5 67 68 2d 6f 72 64 65 72 20 62 69 74 20 6f 66 20  gh-order bit of 
194f6 74 68 65 20 62 69 74 6d 61 73 6b 2e 0a 20 20 2a  the bitmask..  *
194f7 2f 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69  /.  if( pExpr->i
194f8 43 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20 70 4d 61  Column>=0 && pMa
194f9 74 63 68 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e  tch!=0 ){.    in
194fa 74 20 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f  t n = pExpr->iCo
194fb 6c 75 6d 6e 3b 0a 20 20 20 20 74 65 73 74 63 61  lumn;.    testca
194fc 73 65 28 20 6e 3d 3d 73 69 7a 65 6f 66 28 42 69  se( n==sizeof(Bi
194fd 74 6d 61 73 6b 29 2a 38 2d 31 20 29 3b 0a 20 20  tmask)*8-1 );.  
194fe 20 20 69 66 28 20 6e 3e 3d 73 69 7a 65 6f 66 28    if( n>=sizeof(
194ff 42 69 74 6d 61 73 6b 29 2a 38 20 29 7b 0a 20 20  Bitmask)*8 ){.  
19500 20 20 20 20 6e 20 3d 20 73 69 7a 65 6f 66 28 42      n = sizeof(B
19501 69 74 6d 61 73 6b 29 2a 38 2d 31 3b 0a 20 20 20  itmask)*8-1;.   
19502 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
19503 4d 61 74 63 68 2d 3e 69 43 75 72 73 6f 72 3d 3d  Match->iCursor==
19504 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 29 3b  pExpr->iTable );
19505 0a 20 20 20 20 70 4d 61 74 63 68 2d 3e 63 6f 6c  .    pMatch->col
19506 55 73 65 64 20 7c 3d 20 28 28 42 69 74 6d 61 73  Used |= ((Bitmas
19507 6b 29 31 29 3c 3c 6e 3b 0a 20 20 7d 0a 0a 6c 6f  k)1)<<n;.  }..lo
19508 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 3a 0a 20 20  okupname_end:.  
19509 2f 2a 20 43 6c 65 61 6e 20 75 70 20 61 6e 64 20  /* Clean up and 
1950a 72 65 74 75 72 6e 0a 20 20 2a 2f 0a 20 20 73 71  return.  */.  sq
1950b 6c 69 74 65 33 5f 66 72 65 65 28 7a 44 62 29 3b  lite3_free(zDb);
1950c 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
1950d 7a 54 61 62 29 3b 0a 20 20 73 71 6c 69 74 65 33  zTab);.  sqlite3
1950e 45 78 70 72 44 65 6c 65 74 65 28 70 45 78 70 72  ExprDelete(pExpr
1950f 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 70 45 78 70  ->pLeft);.  pExp
19510 72 2d 3e 70 4c 65 66 74 20 3d 20 30 3b 0a 20 20  r->pLeft = 0;.  
19511 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
19512 65 28 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29  e(pExpr->pRight)
19513 3b 0a 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68  ;.  pExpr->pRigh
19514 74 20 3d 20 30 3b 0a 20 20 70 45 78 70 72 2d 3e  t = 0;.  pExpr->
19515 6f 70 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a  op = TK_COLUMN;.
19516 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 5f 32  lookupname_end_2
19517 3a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  :.  sqlite3_free
19518 28 7a 43 6f 6c 29 3b 0a 20 20 69 66 28 20 63 6e  (zCol);.  if( cn
19519 74 3d 3d 31 20 29 7b 0a 20 20 20 20 61 73 73 65  t==1 ){.    asse
1951a 72 74 28 20 70 4e 43 21 3d 30 20 29 3b 0a 20 20  rt( pNC!=0 );.  
1951b 20 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61    sqlite3AuthRea
1951c 64 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c  d(pParse, pExpr,
1951d 20 70 53 63 68 65 6d 61 2c 20 70 4e 43 2d 3e 70   pSchema, pNC->p
1951e 53 72 63 4c 69 73 74 29 3b 0a 20 20 20 20 69 66  SrcList);.    if
1951f 28 20 70 4d 61 74 63 68 20 26 26 20 21 70 4d 61  ( pMatch && !pMa
19520 74 63 68 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a  tch->pSelect ){.
19521 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61        pExpr->pTa
19522 62 20 3d 20 70 4d 61 74 63 68 2d 3e 70 54 61 62  b = pMatch->pTab
19523 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 49  ;.    }.    /* I
19524 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 6e 52 65  ncrement the nRe
19525 66 20 76 61 6c 75 65 20 6f 6e 20 61 6c 6c 20 6e  f value on all n
19526 61 6d 65 20 63 6f 6e 74 65 78 74 73 20 66 72 6f  ame contexts fro
19527 6d 20 54 6f 70 4e 43 20 75 70 20 74 6f 0a 20 20  m TopNC up to.  
19528 20 20 2a 2a 20 74 68 65 20 70 6f 69 6e 74 20 77    ** the point w
19529 68 65 72 65 20 74 68 65 20 6e 61 6d 65 20 6d 61  here the name ma
1952a 74 63 68 65 64 2e 20 2a 2f 0a 20 20 20 20 66 6f  tched. */.    fo
1952b 72 28 3b 3b 29 7b 0a 20 20 20 20 20 20 61 73 73  r(;;){.      ass
1952c 65 72 74 28 20 70 54 6f 70 4e 43 21 3d 30 20 29  ert( pTopNC!=0 )
1952d 3b 0a 20 20 20 20 20 20 70 54 6f 70 4e 43 2d 3e  ;.      pTopNC->
1952e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20 69 66  nRef++;.      if
1952f 28 20 70 54 6f 70 4e 43 3d 3d 70 4e 43 20 29 20  ( pTopNC==pNC ) 
19530 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 70 54 6f  break;.      pTo
19531 70 4e 43 20 3d 20 70 54 6f 70 4e 43 2d 3e 70 4e  pNC = pTopNC->pN
19532 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  ext;.    }.    r
19533 65 74 75 72 6e 20 30 3b 0a 20 20 7d 20 65 6c 73  eturn 0;.  } els
19534 65 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  e {.    return 1
19535 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
19536 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 64  his routine is d
19537 65 73 69 67 6e 65 64 20 61 73 20 61 6e 20 78 46  esigned as an xF
19538 75 6e 63 20 66 6f 72 20 77 61 6c 6b 45 78 70 72  unc for walkExpr
19539 54 72 65 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65  Tree()..**.** Re
1953a 73 6f 6c 76 65 20 73 79 6d 62 6f 6c 69 63 20 6e  solve symbolic n
1953b 61 6d 65 73 20 69 6e 74 6f 20 54 4b 5f 43 4f 4c  ames into TK_COL
1953c 55 4d 4e 20 6f 70 65 72 61 74 6f 72 73 20 66 6f  UMN operators fo
1953d 72 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a  r the current.**
1953e 20 6e 6f 64 65 20 69 6e 20 74 68 65 20 65 78 70   node in the exp
1953f 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52  ression tree.  R
19540 65 74 75 72 6e 20 30 20 74 6f 20 63 6f 6e 74 69  eturn 0 to conti
19541 6e 75 65 20 74 68 65 20 73 65 61 72 63 68 20 64  nue the search d
19542 6f 77 6e 0a 2a 2a 20 74 68 65 20 74 72 65 65 20  own.** the tree 
19543 6f 72 20 32 20 74 6f 20 61 62 6f 72 74 20 74 68  or 2 to abort th
19544 65 20 74 72 65 65 20 77 61 6c 6b 2e 0a 2a 2a 0a  e tree walk..**.
19545 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
19546 61 6c 73 6f 20 64 6f 65 73 20 65 72 72 6f 72 20  also does error 
19547 63 68 65 63 6b 69 6e 67 20 61 6e 64 20 6e 61 6d  checking and nam
19548 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 66 6f 72  e resolution for
19549 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d  .** function nam
1954a 65 73 2e 20 20 54 68 65 20 6f 70 65 72 61 74 6f  es.  The operato
1954b 72 20 66 6f 72 20 61 67 67 72 65 67 61 74 65 20  r for aggregate 
1954c 66 75 6e 63 74 69 6f 6e 73 20 69 73 20 63 68 61  functions is cha
1954d 6e 67 65 64 0a 2a 2a 20 74 6f 20 54 4b 5f 41 47  nged.** to TK_AG
1954e 47 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2f 0a 73  G_FUNCTION..*/.s
1954f 74 61 74 69 63 20 69 6e 74 20 6e 61 6d 65 52 65  tatic int nameRe
19550 73 6f 6c 76 65 72 53 74 65 70 28 76 6f 69 64 20  solverStep(void 
19551 2a 70 41 72 67 2c 20 45 78 70 72 20 2a 70 45 78  *pArg, Expr *pEx
19552 70 72 29 7b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65  pr){.  NameConte
19553 78 74 20 2a 70 4e 43 20 3d 20 28 4e 61 6d 65 43  xt *pNC = (NameC
19554 6f 6e 74 65 78 74 2a 29 70 41 72 67 3b 0a 20 20  ontext*)pArg;.  
19555 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 0a  Parse *pParse;..
19556 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29    if( pExpr==0 )
19557 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 61 73 73   return 1;.  ass
19558 65 72 74 28 20 70 4e 43 21 3d 30 20 29 3b 0a 20  ert( pNC!=0 );. 
19559 20 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70   pParse = pNC->p
1955a 50 61 72 73 65 3b 0a 0a 20 20 69 66 28 20 45 78  Parse;..  if( Ex
1955b 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  prHasAnyProperty
1955c 28 70 45 78 70 72 2c 20 45 50 5f 52 65 73 6f 6c  (pExpr, EP_Resol
1955d 76 65 64 29 20 29 20 72 65 74 75 72 6e 20 31 3b  ved) ) return 1;
1955e 0a 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72  .  ExprSetProper
1955f 74 79 28 70 45 78 70 72 2c 20 45 50 5f 52 65 73  ty(pExpr, EP_Res
19560 6f 6c 76 65 64 29 3b 0a 23 69 66 6e 64 65 66 20  olved);.#ifndef 
19561 4e 44 45 42 55 47 0a 20 20 69 66 28 20 70 4e 43  NDEBUG.  if( pNC
19562 2d 3e 70 53 72 63 4c 69 73 74 20 26 26 20 70 4e  ->pSrcList && pN
19563 43 2d 3e 70 53 72 63 4c 69 73 74 2d 3e 6e 41 6c  C->pSrcList->nAl
19564 6c 6f 63 3e 30 20 29 7b 0a 20 20 20 20 53 72 63  loc>0 ){.    Src
19565 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 20 3d  List *pSrcList =
19566 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a   pNC->pSrcList;.
19567 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66      int i;.    f
19568 6f 72 28 69 3d 30 3b 20 69 3c 70 4e 43 2d 3e 70  or(i=0; i<pNC->p
19569 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69  SrcList->nSrc; i
1956a 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  ++){.      asser
1956b 74 28 20 70 53 72 63 4c 69 73 74 2d 3e 61 5b 69  t( pSrcList->a[i
1956c 5d 2e 69 43 75 72 73 6f 72 3e 3d 30 20 26 26 20  ].iCursor>=0 && 
1956d 70 53 72 63 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69  pSrcList->a[i].i
1956e 43 75 72 73 6f 72 3c 70 50 61 72 73 65 2d 3e 6e  Cursor<pParse->n
1956f 54 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Tab);.    }.  }.
19570 23 65 6e 64 69 66 0a 20 20 73 77 69 74 63 68 28  #endif.  switch(
19571 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20   pExpr->op ){.  
19572 20 20 2f 2a 20 44 6f 75 62 6c 65 2d 71 75 6f 74    /* Double-quot
19573 65 64 20 73 74 72 69 6e 67 73 20 28 65 78 3a 20  ed strings (ex: 
19574 22 61 62 63 22 29 20 61 72 65 20 75 73 65 64 20  "abc") are used 
19575 61 73 20 69 64 65 6e 74 69 66 69 65 72 73 20 69  as identifiers i
19576 66 0a 20 20 20 20 2a 2a 20 70 6f 73 73 69 62 6c  f.    ** possibl
19577 65 2e 20 20 4f 74 68 65 72 77 69 73 65 20 74 68  e.  Otherwise th
19578 65 79 20 72 65 6d 61 69 6e 20 61 73 20 73 74 72  ey remain as str
19579 69 6e 67 73 2e 20 20 53 69 6e 67 6c 65 2d 71 75  ings.  Single-qu
1957a 6f 74 65 64 0a 20 20 20 20 2a 2a 20 73 74 72 69  oted.    ** stri
1957b 6e 67 73 20 28 65 78 3a 20 27 61 62 63 27 29 20  ngs (ex: 'abc') 
1957c 61 72 65 20 61 6c 77 61 79 73 20 73 74 72 69 6e  are always strin
1957d 67 20 6c 69 74 65 72 61 6c 73 2e 0a 20 20 20 20  g literals..    
1957e 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53  */.    case TK_S
1957f 54 52 49 4e 47 3a 20 7b 0a 20 20 20 20 20 20 69  TRING: {.      i
19580 66 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e  f( pExpr->token.
19581 7a 5b 30 5d 3d 3d 27 5c 27 27 20 29 20 62 72 65  z[0]=='\'' ) bre
19582 61 6b 3b 0a 20 20 20 20 20 20 2f 2a 20 46 61 6c  ak;.      /* Fal
19583 6c 20 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20  l thru into the 
19584 54 4b 5f 49 44 20 63 61 73 65 20 69 66 20 74 68  TK_ID case if th
19585 69 73 20 69 73 20 61 20 64 6f 75 62 6c 65 2d 71  is is a double-q
19586 75 6f 74 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a  uoted string */.
19587 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 41 20 6c      }.    /* A l
19588 6f 6e 65 20 69 64 65 6e 74 69 66 69 65 72 20 69  one identifier i
19589 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20  s the name of a 
1958a 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20  column..    */. 
1958b 20 20 20 63 61 73 65 20 54 4b 5f 49 44 3a 20 7b     case TK_ID: {
1958c 0a 20 20 20 20 20 20 6c 6f 6f 6b 75 70 4e 61 6d  .      lookupNam
1958d 65 28 70 50 61 72 73 65 2c 20 30 2c 20 30 2c 20  e(pParse, 0, 0, 
1958e 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2c 20 70  &pExpr->token, p
1958f 4e 43 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20  NC, pExpr);.    
19590 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
19591 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 41 20 74 61  }.  .    /* A ta
19592 62 6c 65 20 6e 61 6d 65 20 61 6e 64 20 63 6f 6c  ble name and col
19593 75 6d 6e 20 6e 61 6d 65 3a 20 20 20 20 20 49 44  umn name:     ID
19594 2e 49 44 0a 20 20 20 20 2a 2a 20 4f 72 20 61 20  .ID.    ** Or a 
19595 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 20  database, table 
19596 61 6e 64 20 63 6f 6c 75 6d 6e 3a 20 20 49 44 2e  and column:  ID.
19597 49 44 2e 49 44 0a 20 20 20 20 2a 2f 0a 20 20 20  ID.ID.    */.   
19598 20 63 61 73 65 20 54 4b 5f 44 4f 54 3a 20 7b 0a   case TK_DOT: {.
19599 20 20 20 20 20 20 54 6f 6b 65 6e 20 2a 70 43 6f        Token *pCo
1959a 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 54 6f 6b 65  lumn;.      Toke
1959b 6e 20 2a 70 54 61 62 6c 65 3b 0a 20 20 20 20 20  n *pTable;.     
1959c 20 54 6f 6b 65 6e 20 2a 70 44 62 3b 0a 20 20 20   Token *pDb;.   
1959d 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 3b     Expr *pRight;
1959e 0a 0a 20 20 20 20 20 20 2f 2a 20 69 66 28 20 70  ..      /* if( p
1959f 53 72 63 4c 69 73 74 3d 3d 30 20 29 20 62 72 65  SrcList==0 ) bre
195a0 61 6b 3b 20 2a 2f 0a 20 20 20 20 20 20 70 52 69  ak; */.      pRi
195a1 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 70 52 69  ght = pExpr->pRi
195a2 67 68 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ght;.      if( p
195a3 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 44  Right->op==TK_ID
195a4 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 62 20   ){.        pDb 
195a5 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70 54 61  = 0;.        pTa
195a6 62 6c 65 20 3d 20 26 70 45 78 70 72 2d 3e 70 4c  ble = &pExpr->pL
195a7 65 66 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20  eft->token;.    
195a8 20 20 20 20 70 43 6f 6c 75 6d 6e 20 3d 20 26 70      pColumn = &p
195a9 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 20  Right->token;.  
195aa 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
195ab 20 20 20 61 73 73 65 72 74 28 20 70 52 69 67 68     assert( pRigh
195ac 74 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 29 3b  t->op==TK_DOT );
195ad 0a 20 20 20 20 20 20 20 20 70 44 62 20 3d 20 26  .        pDb = &
195ae 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 74 6f  pExpr->pLeft->to
195af 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 70 54 61  ken;.        pTa
195b0 62 6c 65 20 3d 20 26 70 52 69 67 68 74 2d 3e 70  ble = &pRight->p
195b1 4c 65 66 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20  Left->token;.   
195b2 20 20 20 20 20 70 43 6f 6c 75 6d 6e 20 3d 20 26       pColumn = &
195b3 70 52 69 67 68 74 2d 3e 70 52 69 67 68 74 2d 3e  pRight->pRight->
195b4 74 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  token;.      }. 
195b5 20 20 20 20 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28       lookupName(
195b6 70 50 61 72 73 65 2c 20 70 44 62 2c 20 70 54 61  pParse, pDb, pTa
195b7 62 6c 65 2c 20 70 43 6f 6c 75 6d 6e 2c 20 70 4e  ble, pColumn, pN
195b8 43 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20  C, pExpr);.     
195b9 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
195ba 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65  ..    /* Resolve
195bb 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 0a   function names.
195bc 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
195bd 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20  TK_CONST_FUNC:. 
195be 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 54     case TK_FUNCT
195bf 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 45 78 70  ION: {.      Exp
195c0 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70  rList *pList = p
195c1 45 78 70 72 2d 3e 70 4c 69 73 74 3b 20 20 20 20  Expr->pList;    
195c2 2f 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20  /* The argument 
195c3 6c 69 73 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e  list */.      in
195c4 74 20 6e 20 3d 20 70 4c 69 73 74 20 3f 20 70 4c  t n = pList ? pL
195c5 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 20  ist->nExpr : 0; 
195c6 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72   /* Number of ar
195c7 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20  guments */.     
195c8 20 69 6e 74 20 6e 6f 5f 73 75 63 68 5f 66 75 6e   int no_such_fun
195c9 63 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20  c = 0;       /* 
195ca 54 72 75 65 20 69 66 20 6e 6f 20 73 75 63 68 20  True if no such 
195cb 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 20  function exists 
195cc 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 77 72 6f  */.      int wro
195cd 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 30 3b  ng_num_args = 0;
195ce 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
195cf 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f 66 20  wrong number of 
195d0 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20  arguments */.   
195d1 20 20 20 69 6e 74 20 69 73 5f 61 67 67 20 3d 20     int is_agg = 
195d2 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0;             /
195d3 2a 20 54 72 75 65 20 69 66 20 69 73 20 61 6e 20  * True if is an 
195d4 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
195d5 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  on */.      int 
195d6 69 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 75 74  i;.      int aut
195d7 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h;              
195d8 20 20 20 20 20 2f 2a 20 41 75 74 68 6f 72 69 7a       /* Authoriz
195d9 61 74 69 6f 6e 20 74 6f 20 75 73 65 20 74 68 65  ation to use the
195da 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20   function */.   
195db 20 20 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20     int nId;     
195dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
195dd 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72  * Number of char
195de 61 63 74 65 72 73 20 69 6e 20 66 75 6e 63 74 69  acters in functi
195df 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20  on name */.     
195e0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 64   const char *zId
195e1 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
195e2 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d  The function nam
195e3 65 2e 20 2a 2f 0a 20 20 20 20 20 20 46 75 6e 63  e. */.      Func
195e4 44 65 66 20 2a 70 44 65 66 3b 20 20 20 20 20 20  Def *pDef;      
195e5 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72          /* Infor
195e6 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mation about the
195e7 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20   function */.   
195e8 20 20 20 69 6e 74 20 65 6e 63 20 3d 20 45 4e 43     int enc = ENC
195e9 28 70 50 61 72 73 65 2d 3e 64 62 29 3b 20 20 2f  (pParse->db);  /
195ea 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 65  * The database e
195eb 6e 63 6f 64 69 6e 67 20 2a 2f 0a 0a 20 20 20 20  ncoding */..    
195ec 20 20 7a 49 64 20 3d 20 28 63 68 61 72 2a 29 70    zId = (char*)p
195ed 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 3b 0a 20  Expr->token.z;. 
195ee 20 20 20 20 20 6e 49 64 20 3d 20 70 45 78 70 72       nId = pExpr
195ef 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 20  ->token.n;.     
195f0 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46   pDef = sqlite3F
195f1 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61 72  indFunction(pPar
195f2 73 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 64  se->db, zId, nId
195f3 2c 20 6e 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20  , n, enc, 0);.  
195f4 20 20 20 20 69 66 28 20 70 44 65 66 3d 3d 30 20      if( pDef==0 
195f5 29 7b 0a 20 20 20 20 20 20 20 20 70 44 65 66 20  ){.        pDef 
195f6 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e  = sqlite3FindFun
195f7 63 74 69 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62  ction(pParse->db
195f8 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 2d 31 2c 20  , zId, nId, -1, 
195f9 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  enc, 0);.       
195fa 20 69 66 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a   if( pDef==0 ){.
195fb 20 20 20 20 20 20 20 20 20 20 6e 6f 5f 73 75 63            no_suc
195fc 68 5f 66 75 6e 63 20 3d 20 31 3b 0a 20 20 20 20  h_func = 1;.    
195fd 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
195fe 20 20 20 20 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61       wrong_num_a
195ff 72 67 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  rgs = 1;.       
19600 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a   }.      }else{.
19601 20 20 20 20 20 20 20 20 69 73 5f 61 67 67 20 3d          is_agg =
19602 20 70 44 65 66 2d 3e 78 46 75 6e 63 3d 3d 30 3b   pDef->xFunc==0;
19603 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66  .      }.#ifndef
19604 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
19605 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 20  HORIZATION.     
19606 20 69 66 28 20 70 44 65 66 20 29 7b 0a 20 20 20   if( pDef ){.   
19607 20 20 20 20 20 61 75 74 68 20 3d 20 73 71 6c 69       auth = sqli
19608 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
19609 72 73 65 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43  rse, SQLITE_FUNC
1960a 54 49 4f 4e 2c 20 30 2c 20 70 44 65 66 2d 3e 7a  TION, 0, pDef->z
1960b 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Name, 0);.      
1960c 20 20 69 66 28 20 61 75 74 68 21 3d 53 51 4c 49    if( auth!=SQLI
1960d 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1960e 20 20 20 69 66 28 20 61 75 74 68 3d 3d 53 51 4c     if( auth==SQL
1960f 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20  ITE_DENY ){.    
19610 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
19611 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
19612 22 6e 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 20  "not authorized 
19613 74 6f 20 75 73 65 20 66 75 6e 63 74 69 6f 6e 3a  to use function:
19614 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20   %s",.          
19615 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19616 20 20 20 20 20 20 20 20 20 20 70 44 65 66 2d 3e            pDef->
19617 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  zName);.        
19618 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b      pNC->nErr++;
19619 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1961a 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70         pExpr->op
1961b 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 20 20   = TK_NULL;.    
1961c 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
1961d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1961e 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69  }.#endif.      i
1961f 66 28 20 69 73 5f 61 67 67 20 26 26 20 21 70 4e  f( is_agg && !pN
19620 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 29 7b 0a 20  C->allowAgg ){. 
19621 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
19622 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
19623 6d 69 73 75 73 65 20 6f 66 20 61 67 67 72 65 67  misuse of aggreg
19624 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 25 2e 2a  ate function %.*
19625 73 28 29 22 2c 20 6e 49 64 2c 7a 49 64 29 3b 0a  s()", nId,zId);.
19626 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72          pNC->nEr
19627 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 69 73 5f  r++;.        is_
19628 61 67 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  agg = 0;.      }
19629 65 6c 73 65 20 69 66 28 20 6e 6f 5f 73 75 63 68  else if( no_such
1962a 5f 66 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 20  _func ){.       
1962b 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1962c 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63  (pParse, "no suc
1962d 68 20 66 75 6e 63 74 69 6f 6e 3a 20 25 2e 2a 73  h function: %.*s
1962e 22 2c 20 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20  ", nId, zId);.  
1962f 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b        pNC->nErr+
19630 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  +;.      }else i
19631 66 28 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67  f( wrong_num_arg
19632 73 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  s ){.        sql
19633 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
19634 72 73 65 2c 22 77 72 6f 6e 67 20 6e 75 6d 62 65  rse,"wrong numbe
19635 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74  r of arguments t
19636 6f 20 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28  o function %.*s(
19637 29 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  )",.            
19638 20 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20   nId, zId);.    
19639 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b      pNC->nErr++;
1963a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
1963b 66 28 20 69 73 5f 61 67 67 20 29 7b 0a 20 20 20  f( is_agg ){.   
1963c 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d       pExpr->op =
1963d 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e   TK_AGG_FUNCTION
1963e 3b 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 68  ;.        pNC->h
1963f 61 73 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 20  asAgg = 1;.     
19640 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f   }.      if( is_
19641 61 67 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77  agg ) pNC->allow
19642 41 67 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 66  Agg = 0;.      f
19643 6f 72 28 69 3d 30 3b 20 70 4e 43 2d 3e 6e 45 72  or(i=0; pNC->nEr
19644 72 3d 3d 30 20 26 26 20 69 3c 6e 3b 20 69 2b 2b  r==0 && i<n; i++
19645 29 7b 0a 20 20 20 20 20 20 20 20 77 61 6c 6b 45  ){.        walkE
19646 78 70 72 54 72 65 65 28 70 4c 69 73 74 2d 3e 61  xprTree(pList->a
19647 5b 69 5d 2e 70 45 78 70 72 2c 20 6e 61 6d 65 52  [i].pExpr, nameR
19648 65 73 6f 6c 76 65 72 53 74 65 70 2c 20 70 4e 43  esolverStep, pNC
19649 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1964a 20 69 66 28 20 69 73 5f 61 67 67 20 29 20 70 4e   if( is_agg ) pN
1964b 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b  C->allowAgg = 1;
1964c 0a 20 20 20 20 20 20 2f 2a 20 46 49 58 20 4d 45  .      /* FIX ME
1964d 3a 20 20 43 6f 6d 70 75 74 65 20 70 45 78 70 72  :  Compute pExpr
1964e 2d 3e 61 66 66 69 6e 69 74 79 20 62 61 73 65 64  ->affinity based
1964f 20 6f 6e 20 74 68 65 20 65 78 70 65 63 74 65 64   on the expected
19650 20 72 65 74 75 72 6e 0a 20 20 20 20 20 20 2a 2a   return.      **
19651 20 74 79 70 65 20 6f 66 20 74 68 65 20 66 75 6e   type of the fun
19652 63 74 69 6f 6e 20 0a 20 20 20 20 20 20 2a 2f 0a  ction .      */.
19653 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 73 5f        return is_
19654 61 67 67 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64  agg;.    }.#ifnd
19655 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
19656 55 42 51 55 45 52 59 0a 20 20 20 20 63 61 73 65  UBQUERY.    case
19657 20 54 4b 5f 53 45 4c 45 43 54 3a 0a 20 20 20 20   TK_SELECT:.    
19658 63 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a  case TK_EXISTS:.
19659 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20  #endif.    case 
1965a 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 69  TK_IN: {.      i
1965b 66 28 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 63  f( pExpr->pSelec
1965c 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  t ){.        int
1965d 20 6e 52 65 66 20 3d 20 70 4e 43 2d 3e 6e 52 65   nRef = pNC->nRe
1965e 66 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f;.#ifndef SQLIT
1965f 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 20  E_OMIT_CHECK.   
19660 20 20 20 20 20 69 66 28 20 70 4e 43 2d 3e 69 73       if( pNC->is
19661 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 20 20 20  Check ){.       
19662 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
19663 73 67 28 70 50 61 72 73 65 2c 22 73 75 62 71 75  sg(pParse,"subqu
19664 65 72 69 65 73 20 70 72 6f 68 69 62 69 74 65 64  eries prohibited
19665 20 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73 74 72   in CHECK constr
19666 61 69 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 20  aints");.       
19667 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20   }.#endif.      
19668 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 52    sqlite3SelectR
19669 65 73 6f 6c 76 65 28 70 50 61 72 73 65 2c 20 70  esolve(pParse, p
1966a 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 2c 20 70  Expr->pSelect, p
1966b 4e 43 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  NC);.        ass
1966c 65 72 74 28 20 70 4e 43 2d 3e 6e 52 65 66 3e 3d  ert( pNC->nRef>=
1966d 6e 52 65 66 20 29 3b 0a 20 20 20 20 20 20 20 20  nRef );.        
1966e 69 66 28 20 6e 52 65 66 21 3d 70 4e 43 2d 3e 6e  if( nRef!=pNC->n
1966f 52 65 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Ref ){.         
19670 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79   ExprSetProperty
19671 28 70 45 78 70 72 2c 20 45 50 5f 56 61 72 53 65  (pExpr, EP_VarSe
19672 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20 7d  lect);.        }
19673 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
19674 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e  reak;.    }.#ifn
19675 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
19676 43 48 45 43 4b 0a 20 20 20 20 63 61 73 65 20 54  CHECK.    case T
19677 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b 0a 20 20  K_VARIABLE: {.  
19678 20 20 20 20 69 66 28 20 70 4e 43 2d 3e 69 73 43      if( pNC->isC
19679 68 65 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20  heck ){.        
1967a 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1967b 70 50 61 72 73 65 2c 22 70 61 72 61 6d 65 74 65  pParse,"paramete
1967c 72 73 20 70 72 6f 68 69 62 69 74 65 64 20 69 6e  rs prohibited in
1967d 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e   CHECK constrain
1967e 74 73 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ts");.      }.  
1967f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
19680 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65  .#endif.  }.  re
19681 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
19682 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61   This routine wa
19683 6c 6b 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f  lks an expressio
19684 6e 20 74 72 65 65 20 61 6e 64 20 72 65 73 6f 6c  n tree and resol
19685 76 65 73 20 72 65 66 65 72 65 6e 63 65 73 20 74  ves references t
19686 6f 0a 2a 2a 20 74 61 62 6c 65 20 63 6f 6c 75 6d  o.** table colum
19687 6e 73 2e 20 20 4e 6f 64 65 73 20 6f 66 20 74 68  ns.  Nodes of th
19688 65 20 66 6f 72 6d 20 49 44 2e 49 44 20 6f 72 20  e form ID.ID or 
19689 49 44 20 72 65 73 6f 6c 76 65 20 69 6e 74 6f 20  ID resolve into 
1968a 61 6e 0a 2a 2a 20 69 6e 64 65 78 20 74 6f 20 74  an.** index to t
1968b 68 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20  he table in the 
1968c 74 61 62 6c 65 20 6c 69 73 74 20 61 6e 64 20 61  table list and a
1968d 20 63 6f 6c 75 6d 6e 20 6f 66 66 73 65 74 2e 20   column offset. 
1968e 20 54 68 65 20 0a 2a 2a 20 45 78 70 72 2e 6f 70   The .** Expr.op
1968f 63 6f 64 65 20 66 6f 72 20 73 75 63 68 20 6e 6f  code for such no
19690 64 65 73 20 69 73 20 63 68 61 6e 67 65 64 20 74  des is changed t
19691 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 2e 20 20 54 68  o TK_COLUMN.  Th
19692 65 20 45 78 70 72 2e 69 54 61 62 6c 65 0a 2a 2a  e Expr.iTable.**
19693 20 76 61 6c 75 65 20 69 73 20 63 68 61 6e 67 65   value is change
19694 64 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f  d to the index o
19695 66 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 64  f the referenced
19696 20 74 61 62 6c 65 20 69 6e 20 70 54 61 62 4c 69   table in pTabLi
19697 73 74 0a 2a 2a 20 70 6c 75 73 20 74 68 65 20 22  st.** plus the "
19698 62 61 73 65 22 20 76 61 6c 75 65 2e 20 20 54 68  base" value.  Th
19699 65 20 62 61 73 65 20 76 61 6c 75 65 20 77 69 6c  e base value wil
1969a 6c 20 75 6c 74 69 6d 61 74 65 6c 79 20 62 65 63  l ultimately bec
1969b 6f 6d 65 20 74 68 65 0a 2a 2a 20 56 44 42 45 20  ome the.** VDBE 
1969c 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f  cursor number fo
1969d 72 20 61 20 63 75 72 73 6f 72 20 74 68 61 74 20  r a cursor that 
1969e 69 73 20 70 6f 69 6e 74 69 6e 67 20 69 6e 74 6f  is pointing into
1969f 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 64 0a   the referenced.
196a0 2a 2a 20 74 61 62 6c 65 2e 20 20 54 68 65 20 45  ** table.  The E
196a1 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 76 61 6c 75  xpr.iColumn valu
196a2 65 20 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20  e is changed to 
196a3 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
196a4 20 63 6f 6c 75 6d 6e 20 0a 2a 2a 20 6f 66 20 74   column .** of t
196a5 68 65 20 72 65 66 65 72 65 6e 63 65 64 20 74 61  he referenced ta
196a6 62 6c 65 2e 20 20 54 68 65 20 45 78 70 72 2e 69  ble.  The Expr.i
196a7 43 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 66 6f 72  Column value for
196a8 20 74 68 65 20 73 70 65 63 69 61 6c 0a 2a 2a 20   the special.** 
196a9 52 4f 57 49 44 20 63 6f 6c 75 6d 6e 20 69 73 20  ROWID column is 
196aa 2d 31 2e 20 20 41 6e 79 20 49 4e 54 45 47 45 52  -1.  Any INTEGER
196ab 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c   PRIMARY KEY col
196ac 75 6d 6e 20 69 73 20 74 72 69 65 64 20 61 73 20  umn is tried as 
196ad 61 6e 0a 2a 2a 20 61 6c 69 61 73 20 66 6f 72 20  an.** alias for 
196ae 52 4f 57 49 44 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73  ROWID..**.** Als
196af 6f 20 72 65 73 6f 6c 76 65 20 66 75 6e 63 74 69  o resolve functi
196b0 6f 6e 20 6e 61 6d 65 73 20 61 6e 64 20 63 68 65  on names and che
196b1 63 6b 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73  ck the functions
196b2 20 66 6f 72 20 70 72 6f 70 65 72 0a 2a 2a 20 75   for proper.** u
196b3 73 61 67 65 2e 20 20 4d 61 6b 65 20 73 75 72 65  sage.  Make sure
196b4 20 61 6c 6c 20 66 75 6e 63 74 69 6f 6e 20 6e 61   all function na
196b5 6d 65 73 20 61 72 65 20 72 65 63 6f 67 6e 69 7a  mes are recogniz
196b6 65 64 20 61 6e 64 20 61 6c 6c 20 66 75 6e 63 74  ed and all funct
196b7 69 6f 6e 73 0a 2a 2a 20 68 61 76 65 20 74 68 65  ions.** have the
196b8 20 63 6f 72 72 65 63 74 20 6e 75 6d 62 65 72 20   correct number 
196b9 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 4c  of arguments.  L
196ba 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65  eave an error me
196bb 73 73 61 67 65 0a 2a 2a 20 69 6e 20 70 50 61 72  ssage.** in pPar
196bc 73 65 2d 3e 7a 45 72 72 4d 73 67 20 69 66 20 61  se->zErrMsg if a
196bd 6e 79 74 68 69 6e 67 20 69 73 20 61 6d 69 73 73  nything is amiss
196be 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  .  Return the nu
196bf 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 0a  mber of errors..
196c0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70  **.** If the exp
196c1 72 65 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73  ression contains
196c2 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
196c3 69 6f 6e 73 20 74 68 65 6e 20 73 65 74 20 74 68  ions then set th
196c4 65 20 45 50 5f 41 67 67 0a 2a 2a 20 70 72 6f 70  e EP_Agg.** prop
196c5 65 72 74 79 20 6f 6e 20 74 68 65 20 65 78 70 72  erty on the expr
196c6 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  ession..*/.SQLIT
196c7 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
196c8 6c 69 74 65 33 45 78 70 72 52 65 73 6f 6c 76 65  lite3ExprResolve
196c9 4e 61 6d 65 73 28 20 0a 20 20 4e 61 6d 65 43 6f  Names( .  NameCo
196ca 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 20 20  ntext *pNC,     
196cb 20 20 2f 2a 20 4e 61 6d 65 73 70 61 63 65 20 74    /* Namespace t
196cc 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 72 65 73  o resolve expres
196cd 73 69 6f 6e 73 20 69 6e 2e 20 2a 2f 0a 20 20 45  sions in. */.  E
196ce 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 20 20  xpr *pExpr      
196cf 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78         /* The ex
196d0 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 61  pression to be a
196d1 6e 61 6c 79 7a 65 64 2e 20 2a 2f 0a 29 7b 0a 20  nalyzed. */.){. 
196d2 20 69 6e 74 20 73 61 76 65 64 48 61 73 41 67 67   int savedHasAgg
196d3 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d  ;..  if( pExpr==
196d4 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 69  0 ) return 0;.#i
196d5 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50  f SQLITE_MAX_EXP
196d6 52 5f 44 45 50 54 48 3e 30 0a 20 20 7b 0a 20 20  R_DEPTH>0.  {.  
196d7 20 20 69 6e 74 20 6d 78 44 65 70 74 68 20 3d 20    int mxDepth = 
196d8 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 64 62 2d  pNC->pParse->db-
196d9 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
196da 49 4d 49 54 5f 45 58 50 52 5f 44 45 50 54 48 5d  IMIT_EXPR_DEPTH]
196db 3b 0a 20 20 20 20 69 66 28 20 28 70 45 78 70 72  ;.    if( (pExpr
196dc 2d 3e 6e 48 65 69 67 68 74 2b 70 4e 43 2d 3e 70  ->nHeight+pNC->p
196dd 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 29 3e  Parse->nHeight)>
196de 6d 78 44 65 70 74 68 20 29 7b 0a 20 20 20 20 20  mxDepth ){.     
196df 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
196e0 28 70 4e 43 2d 3e 70 50 61 72 73 65 2c 20 0a 20  (pNC->pParse, . 
196e1 20 20 20 20 20 20 20 20 22 45 78 70 72 65 73 73          "Express
196e2 69 6f 6e 20 74 72 65 65 20 69 73 20 74 6f 6f 20  ion tree is too 
196e3 6c 61 72 67 65 20 28 6d 61 78 69 6d 75 6d 20 64  large (maximum d
196e4 65 70 74 68 20 25 64 29 22 2c 20 6d 78 44 65 70  epth %d)", mxDep
196e5 74 68 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20  th.      );.    
196e6 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
196e7 7d 0a 20 20 20 20 70 4e 43 2d 3e 70 50 61 72 73  }.    pNC->pPars
196e8 65 2d 3e 6e 48 65 69 67 68 74 20 2b 3d 20 70 45  e->nHeight += pE
196e9 78 70 72 2d 3e 6e 48 65 69 67 68 74 3b 0a 20 20  xpr->nHeight;.  
196ea 7d 0a 23 65 6e 64 69 66 0a 20 20 73 61 76 65 64  }.#endif.  saved
196eb 48 61 73 41 67 67 20 3d 20 70 4e 43 2d 3e 68 61  HasAgg = pNC->ha
196ec 73 41 67 67 3b 0a 20 20 70 4e 43 2d 3e 68 61 73  sAgg;.  pNC->has
196ed 41 67 67 20 3d 20 30 3b 0a 20 20 77 61 6c 6b 45  Agg = 0;.  walkE
196ee 78 70 72 54 72 65 65 28 70 45 78 70 72 2c 20 6e  xprTree(pExpr, n
196ef 61 6d 65 52 65 73 6f 6c 76 65 72 53 74 65 70 2c  ameResolverStep,
196f0 20 70 4e 43 29 3b 0a 23 69 66 20 53 51 4c 49 54   pNC);.#if SQLIT
196f1 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48  E_MAX_EXPR_DEPTH
196f2 3e 30 0a 20 20 70 4e 43 2d 3e 70 50 61 72 73 65  >0.  pNC->pParse
196f3 2d 3e 6e 48 65 69 67 68 74 20 2d 3d 20 70 45 78  ->nHeight -= pEx
196f4 70 72 2d 3e 6e 48 65 69 67 68 74 3b 0a 23 65 6e  pr->nHeight;.#en
196f5 64 69 66 0a 20 20 69 66 28 20 70 4e 43 2d 3e 6e  dif.  if( pNC->n
196f6 45 72 72 3e 30 20 29 7b 0a 20 20 20 20 45 78 70  Err>0 ){.    Exp
196f7 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 78  rSetProperty(pEx
196f8 70 72 2c 20 45 50 5f 45 72 72 6f 72 29 3b 0a 20  pr, EP_Error);. 
196f9 20 7d 0a 20 20 69 66 28 20 70 4e 43 2d 3e 68 61   }.  if( pNC->ha
196fa 73 41 67 67 20 29 7b 0a 20 20 20 20 45 78 70 72  sAgg ){.    Expr
196fb 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70  SetProperty(pExp
196fc 72 2c 20 45 50 5f 41 67 67 29 3b 0a 20 20 7d 65  r, EP_Agg);.  }e
196fd 6c 73 65 20 69 66 28 20 73 61 76 65 64 48 61 73  lse if( savedHas
196fe 41 67 67 20 29 7b 0a 20 20 20 20 70 4e 43 2d 3e  Agg ){.    pNC->
196ff 68 61 73 41 67 67 20 3d 20 31 3b 0a 20 20 7d 0a  hasAgg = 1;.  }.
19700 20 20 72 65 74 75 72 6e 20 45 78 70 72 48 61 73    return ExprHas
19701 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
19702 45 50 5f 45 72 72 6f 72 29 3b 0a 7d 0a 0a 2f 2a  EP_Error);.}../*
19703 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 69 6e  .** A pointer in
19704 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
19705 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64  tructure is used
19706 20 74 6f 20 70 61 73 73 20 69 6e 66 6f 72 6d 61   to pass informa
19707 74 69 6f 6e 0a 2a 2a 20 74 68 72 6f 75 67 68 20  tion.** through 
19708 77 61 6c 6b 45 78 70 72 54 72 65 65 20 69 6e 74  walkExprTree int
19709 6f 20 63 6f 64 65 53 75 62 71 75 65 72 79 53 74  o codeSubquerySt
1970a 65 70 28 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ep()..*/.typedef
1970b 20 73 74 72 75 63 74 20 51 75 65 72 79 43 6f 64   struct QueryCod
1970c 65 72 20 51 75 65 72 79 43 6f 64 65 72 3b 0a 73  er QueryCoder;.s
1970d 74 72 75 63 74 20 51 75 65 72 79 43 6f 64 65 72  truct QueryCoder
1970e 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72   {.  Parse *pPar
1970f 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  se;       /* The
19710 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   parsing context
19711 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78   */.  NameContex
19712 74 20 2a 70 4e 43 3b 20 20 20 20 2f 2a 20 4e 61  t *pNC;    /* Na
19713 6d 65 73 70 61 63 65 20 6f 66 20 66 69 72 73 74  mespace of first
19714 20 65 6e 63 6c 6f 73 69 6e 67 20 71 75 65 72 79   enclosing query
19715 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 64 65 66 20 53   */.};..#ifdef S
19716 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 6e 74  QLITE_TEST.  int
19717 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f   sqlite3_enable_
19718 69 6e 5f 6f 70 74 20 3d 20 31 3b 0a 23 65 6c 73  in_opt = 1;.#els
19719 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69  e.  #define sqli
1971a 74 65 33 5f 65 6e 61 62 6c 65 5f 69 6e 5f 6f 70  te3_enable_in_op
1971b 74 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  t 1.#endif../*.*
1971c 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66  * Return true if
1971d 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72   the IN operator
1971e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73   optimization is
1971f 20 65 6e 61 62 6c 65 64 20 61 6e 64 0a 2a 2a 20   enabled and.** 
19720 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  the SELECT state
19721 6d 65 6e 74 20 70 20 65 78 69 73 74 73 20 61 6e  ment p exists an
19722 64 20 69 73 20 6f 66 20 74 68 65 0a 2a 2a 20 73  d is of the.** s
19723 69 6d 70 6c 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a  imple form:.**.*
19724 2a 20 20 20 20 20 53 45 4c 45 43 54 20 3c 63 6f  *     SELECT <co
19725 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62 6c  lumn> FROM <tabl
19726 65 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73  e>.**.** If this
19727 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 69 74   is the case, it
19728 20 6d 61 79 20 62 65 20 70 6f 73 73 69 62 6c 65   may be possible
19729 20 74 6f 20 75 73 65 20 61 6e 20 65 78 69 73 74   to use an exist
1972a 69 6e 67 20 74 61 62 6c 65 0a 2a 2a 20 6f 72 20  ing table.** or 
1972b 69 6e 64 65 78 20 69 6e 73 74 65 61 64 20 6f 66  index instead of
1972c 20 67 65 6e 65 72 61 74 69 6e 67 20 61 6e 20 65   generating an e
1972d 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 2e 0a  pheremal table..
1972e 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
1972f 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a  E_OMIT_SUBQUERY.
19730 73 74 61 74 69 63 20 69 6e 74 20 69 73 43 61 6e  static int isCan
19731 64 69 64 61 74 65 46 6f 72 49 6e 4f 70 74 28 53  didateForInOpt(S
19732 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 53 72 63  elect *p){.  Src
19733 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20 20 45 78  List *pSrc;.  Ex
19734 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a  prList *pEList;.
19735 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20    Table *pTab;. 
19736 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f 65 6e   if( !sqlite3_en
19737 61 62 6c 65 5f 69 6e 5f 6f 70 74 20 29 20 72 65  able_in_opt ) re
19738 74 75 72 6e 20 30 3b 20 2f 2a 20 49 4e 20 6f 70  turn 0; /* IN op
19739 74 69 6d 69 7a 61 74 69 6f 6e 20 6d 75 73 74 20  timization must 
1973a 62 65 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20  be enabled */.  
1973b 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
1973c 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  n 0;            
1973d 20 20 20 20 20 20 20 2f 2a 20 72 69 67 68 74 2d         /* right-
1973e 68 61 6e 64 20 73 69 64 65 20 6f 66 20 49 4e 20  hand side of IN 
1973f 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69  is SELECT */.  i
19740 66 28 20 70 2d 3e 70 50 72 69 6f 72 20 29 20 72  f( p->pPrior ) r
19741 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20  eturn 0;        
19742 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 61 20 63        /* Not a c
19743 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 2a  ompound SELECT *
19744 2f 0a 20 20 69 66 28 20 70 2d 3e 69 73 44 69 73  /.  if( p->isDis
19745 74 69 6e 63 74 20 29 20 72 65 74 75 72 6e 20 30  tinct ) return 0
19746 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f  ;          /* No
19747 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72   DISTINCT keywor
19748 64 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 73  d */.  if( p->is
19749 41 67 67 20 29 20 72 65 74 75 72 6e 20 30 3b 20  Agg ) return 0; 
1974a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1974b 20 43 6f 6e 74 61 69 6e 73 20 6e 6f 20 61 67 67   Contains no agg
1974c 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  regate functions
1974d 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 47 72   */.  if( p->pGr
1974e 6f 75 70 42 79 20 29 20 72 65 74 75 72 6e 20 30  oupBy ) return 0
1974f 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
19750 48 61 73 20 6e 6f 20 47 52 4f 55 50 20 42 59 20  Has no GROUP BY 
19751 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 66 28 20  clause */.  if( 
19752 70 2d 3e 70 4c 69 6d 69 74 20 29 20 72 65 74 75  p->pLimit ) retu
19753 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20  rn 0;           
19754 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 4c 49 4d     /* Has no LIM
19755 49 54 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69  IT clause */.  i
19756 66 28 20 70 2d 3e 70 4f 66 66 73 65 74 20 29 20  f( p->pOffset ) 
19757 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
19758 70 2d 3e 70 57 68 65 72 65 20 29 20 72 65 74 75  p->pWhere ) retu
19759 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20  rn 0;           
1975a 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 57 48 45     /* Has no WHE
1975b 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 70  RE clause */.  p
1975c 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20  Src = p->pSrc;. 
1975d 20 69 66 28 20 70 53 72 63 3d 3d 30 20 29 20 72   if( pSrc==0 ) r
1975e 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20  eturn 0;        
1975f 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e          /* A sin
19760 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65  gle table in the
19761 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a   FROM clause */.
19762 20 20 69 66 28 20 70 53 72 63 2d 3e 6e 53 72 63    if( pSrc->nSrc
19763 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  !=1 ) return 0;.
19764 20 20 69 66 28 20 70 53 72 63 2d 3e 61 5b 30 5d    if( pSrc->a[0]
19765 2e 70 53 65 6c 65 63 74 20 29 20 72 65 74 75 72  .pSelect ) retur
19766 6e 20 30 3b 20 20 20 20 20 2f 2a 20 46 52 4f 4d  n 0;     /* FROM
19767 20 63 6c 61 75 73 65 20 69 73 20 6e 6f 74 20 61   clause is not a
19768 20 73 75 62 71 75 65 72 79 20 2a 2f 0a 20 20 70   subquery */.  p
19769 54 61 62 20 3d 20 70 53 72 63 2d 3e 61 5b 30 5d  Tab = pSrc->a[0]
1976a 2e 70 54 61 62 3b 0a 20 20 69 66 28 20 70 54 61  .pTab;.  if( pTa
1976b 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  b==0 ) return 0;
1976c 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65  .  if( pTab->pSe
1976d 6c 65 63 74 20 29 20 72 65 74 75 72 6e 20 30 3b  lect ) return 0;
1976e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 52 4f            /* FRO
1976f 4d 20 63 6c 61 75 73 65 20 69 73 20 6e 6f 74 20  M clause is not 
19770 61 20 76 69 65 77 20 2a 2f 0a 20 20 69 66 28 20  a view */.  if( 
19771 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
19772 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20  ) return 0;     
19773 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73     /* FROM claus
19774 65 20 6e 6f 74 20 61 20 76 69 72 74 75 61 6c 20  e not a virtual 
19775 74 61 62 6c 65 20 2a 2f 0a 20 20 70 45 4c 69 73  table */.  pELis
19776 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20  t = p->pEList;. 
19777 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 6e 45 78   if( pEList->nEx
19778 70 72 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30  pr!=1 ) return 0
19779 3b 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63  ;       /* One c
1977a 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 73  olumn in the res
1977b 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 69 66 28  ult set */.  if(
1977c 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45   pEList->a[0].pE
1977d 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55  xpr->op!=TK_COLU
1977e 4d 4e 20 29 20 72 65 74 75 72 6e 20 30 3b 20 2f  MN ) return 0; /
1977f 2a 20 52 65 73 75 6c 74 20 69 73 20 61 20 63 6f  * Result is a co
19780 6c 75 6d 6e 20 2a 2f 0a 20 20 72 65 74 75 72 6e  lumn */.  return
19781 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20   1;.}.#endif /* 
19782 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
19783 55 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  UERY */../*.** T
19784 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
19785 75 73 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c  used by the impl
19786 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
19787 65 20 49 4e 20 28 2e 2e 2e 29 20 6f 70 65 72 61  e IN (...) opera
19788 74 6f 72 2e 0a 2a 2a 20 49 74 27 73 20 6a 6f 62  tor..** It's job
19789 20 69 73 20 74 6f 20 66 69 6e 64 20 6f 72 20 63   is to find or c
1978a 72 65 61 74 65 20 61 20 62 2d 74 72 65 65 20 73  reate a b-tree s
1978b 74 72 75 63 74 75 72 65 20 74 68 61 74 20 6d 61  tructure that ma
1978c 79 20 62 65 20 75 73 65 64 0a 2a 2a 20 65 69 74  y be used.** eit
1978d 68 65 72 20 74 6f 20 74 65 73 74 20 66 6f 72 20  her to test for 
1978e 6d 65 6d 62 65 72 73 68 69 70 20 6f 66 20 74 68  membership of th
1978f 65 20 28 2e 2e 2e 29 20 73 65 74 20 6f 72 20 74  e (...) set or t
19790 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67  o iterate throug
19791 68 0a 2a 2a 20 69 74 73 20 6d 65 6d 62 65 72 73  h.** its members
19792 2c 20 73 6b 69 70 70 69 6e 67 20 64 75 70 6c 69  , skipping dupli
19793 63 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  cates..**.** The
19794 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 6f   cursor opened o
19795 6e 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20  n the structure 
19796 28 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2c  (database table,
19797 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 20   database index 
19798 0a 2a 2a 20 6f 72 20 65 70 68 65 72 6d 61 6c 20  .** or ephermal 
19799 74 61 62 6c 65 29 20 69 73 20 73 74 6f 72 65 64  table) is stored
1979a 20 69 6e 20 70 58 2d 3e 69 54 61 62 6c 65 20 62   in pX->iTable b
1979b 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74  efore this funct
1979c 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 20  ion returns..** 
1979d 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c  The returned val
1979e 75 65 20 69 6e 64 69 63 61 74 65 73 20 74 68 65  ue indicates the
1979f 20 73 74 72 75 63 74 75 72 65 20 74 79 70 65 2c   structure type,
197a0 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
197a1 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 52 4f  **   IN_INDEX_RO
197a2 57 49 44 20 2d 20 54 68 65 20 63 75 72 73 6f 72  WID - The cursor
197a3 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61   was opened on a
197a4 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e   database table.
197a5 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 49  .**   IN_INDEX_I
197a6 4e 44 45 58 20 2d 20 54 68 65 20 63 75 72 73 6f  NDEX - The curso
197a7 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20  r was opened on 
197a8 61 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78  a database index
197a9 2e 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f  ..**   IN_INDEX_
197aa 45 50 48 20 2d 20 20 20 54 68 65 20 63 75 72 73  EPH -   The curs
197ab 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e  or was opened on
197ac 20 61 20 73 70 65 63 69 61 6c 6c 79 20 63 72 65   a specially cre
197ad 61 74 65 64 20 61 6e 64 0a 2a 2a 20 20 20 20 20  ated and.**     
197ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
197af 6f 70 75 6c 61 74 65 64 20 65 70 68 65 72 65 6d  opulated epherem
197b0 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  al table..**.** 
197b1 41 6e 20 65 78 69 73 74 69 6e 67 20 73 74 72 75  An existing stru
197b2 63 74 75 72 65 20 6d 61 79 20 6f 6e 6c 79 20 62  cture may only b
197b3 65 20 75 73 65 64 20 69 66 20 74 68 65 20 53 45  e used if the SE
197b4 4c 45 43 54 20 69 73 20 6f 66 20 74 68 65 20 73  LECT is of the s
197b5 69 6d 70 6c 65 0a 2a 2a 20 66 6f 72 6d 3a 0a 2a  imple.** form:.*
197b6 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20  *.**     SELECT 
197b7 3c 63 6f 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74  <column> FROM <t
197b8 61 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  able>.**.** If t
197b9 68 65 20 6d 75 73 74 42 65 55 6e 69 71 75 65 20  he mustBeUnique 
197ba 70 61 72 61 6d 65 74 65 72 20 69 73 20 66 61 6c  parameter is fal
197bb 73 65 2c 20 74 68 65 20 73 74 72 75 63 74 75 72  se, the structur
197bc 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 0a  e will be used .
197bd 2a 2a 20 66 6f 72 20 66 61 73 74 20 73 65 74 20  ** for fast set 
197be 6d 65 6d 62 65 72 73 68 69 70 20 74 65 73 74 73  membership tests
197bf 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 61  . In this case a
197c0 6e 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62 6c  n epheremal tabl
197c1 65 20 6d 75 73 74 20 0a 2a 2a 20 62 65 20 75 73  e must .** be us
197c2 65 64 20 75 6e 6c 65 73 73 20 3c 63 6f 6c 75 6d  ed unless <colum
197c3 6e 3e 20 69 73 20 61 6e 20 49 4e 54 45 47 45 52  n> is an INTEGER
197c4 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20   PRIMARY KEY or 
197c5 61 6e 20 69 6e 64 65 78 20 63 61 6e 20 0a 2a 2a  an index can .**
197c6 20 62 65 20 66 6f 75 6e 64 20 77 69 74 68 20 3c   be found with <
197c7 63 6f 6c 75 6d 6e 3e 20 61 73 20 69 74 73 20 6c  column> as its l
197c8 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 2e  eft-most column.
197c9 0a 2a 2a 0a 2a 2a 20 49 66 20 6d 75 73 74 42 65  .**.** If mustBe
197ca 55 6e 69 71 75 65 20 69 73 20 74 72 75 65 2c 20  Unique is true, 
197cb 74 68 65 6e 20 74 68 65 20 73 74 72 75 63 74 75  then the structu
197cc 72 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20  re will be used 
197cd 74 6f 20 69 74 65 72 61 74 65 0a 2a 2a 20 74 68  to iterate.** th
197ce 72 6f 75 67 68 20 74 68 65 20 73 65 74 20 6d 65  rough the set me
197cf 6d 62 65 72 73 2c 20 73 6b 69 70 70 69 6e 67 20  mbers, skipping 
197d0 61 6e 79 20 64 75 70 6c 69 63 61 74 65 73 2e 20  any duplicates. 
197d1 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 6e 0a  In this case an.
197d2 2a 2a 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62  ** epheremal tab
197d3 6c 65 20 6d 75 73 74 20 62 65 20 75 73 65 64 20  le must be used 
197d4 75 6e 6c 65 73 73 20 74 68 65 20 73 65 6c 65 63  unless the selec
197d5 74 65 64 20 3c 63 6f 6c 75 6d 6e 3e 20 69 73 20  ted <column> is 
197d6 67 75 61 72 61 6e 74 65 65 64 0a 2a 2a 20 74 6f  guaranteed.** to
197d7 20 62 65 20 75 6e 69 71 75 65 20 2d 20 65 69 74   be unique - eit
197d8 68 65 72 20 62 65 63 61 75 73 65 20 69 74 20 69  her because it i
197d9 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49  s an INTEGER PRI
197da 4d 41 52 59 20 4b 45 59 20 6f 72 20 69 74 0a 2a  MARY KEY or it.*
197db 2a 20 69 73 20 75 6e 69 71 75 65 20 62 79 20 76  * is unique by v
197dc 69 72 74 75 65 20 6f 66 20 61 20 63 6f 6e 73 74  irtue of a const
197dd 72 61 69 6e 74 20 6f 72 20 69 6d 70 6c 69 63 69  raint or implici
197de 74 20 69 6e 64 65 78 2e 0a 2a 2f 0a 23 69 66 6e  t index..*/.#ifn
197df 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
197e0 53 55 42 51 55 45 52 59 0a 53 51 4c 49 54 45 5f  SUBQUERY.SQLITE_
197e1 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
197e2 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 50  te3FindInIndex(P
197e3 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78  arse *pParse, Ex
197e4 70 72 20 2a 70 58 2c 20 69 6e 74 20 6d 75 73 74  pr *pX, int must
197e5 42 65 55 6e 69 71 75 65 29 7b 0a 20 20 53 65 6c  BeUnique){.  Sel
197e6 65 63 74 20 2a 70 3b 0a 20 20 69 6e 74 20 65 54  ect *p;.  int eT
197e7 79 70 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69  ype = 0;.  int i
197e8 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  Tab = pParse->nT
197e9 61 62 2b 2b 3b 0a 0a 20 20 2f 2a 20 54 68 65 20  ab++;..  /* The 
197ea 66 6f 6c 6c 77 69 6e 67 20 69 66 28 2e 2e 2e 29  follwing if(...)
197eb 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74   expression is t
197ec 72 75 65 20 69 66 20 74 68 65 20 53 45 4c 45 43  rue if the SELEC
197ed 54 20 69 73 20 6f 66 20 74 68 65 20 0a 20 20 2a  T is of the .  *
197ee 2a 20 73 69 6d 70 6c 65 20 66 6f 72 6d 3a 0a 20  * simple form:. 
197ef 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c   **.  **     SEL
197f0 45 43 54 20 3c 63 6f 6c 75 6d 6e 3e 20 46 52 4f  ECT <column> FRO
197f1 4d 20 3c 74 61 62 6c 65 3e 0a 20 20 2a 2a 0a 20  M <table>.  **. 
197f2 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 74   ** If this is t
197f3 68 65 20 63 61 73 65 2c 20 69 74 20 6d 61 79 20  he case, it may 
197f4 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 75  be possible to u
197f5 73 65 20 61 6e 20 65 78 69 73 74 69 6e 67 20 74  se an existing t
197f6 61 62 6c 65 0a 20 20 2a 2a 20 6f 72 20 69 6e 64  able.  ** or ind
197f7 65 78 20 69 6e 73 74 65 61 64 20 6f 66 20 67 65  ex instead of ge
197f8 6e 65 72 61 74 69 6e 67 20 61 6e 20 65 70 68 65  nerating an ephe
197f9 72 65 6d 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a  remal table..  *
197fa 2f 0a 20 20 70 20 3d 20 70 58 2d 3e 70 53 65 6c  /.  p = pX->pSel
197fb 65 63 74 3b 0a 20 20 69 66 28 20 69 73 43 61 6e  ect;.  if( isCan
197fc 64 69 64 61 74 65 46 6f 72 49 6e 4f 70 74 28 70  didateForInOpt(p
197fd 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
197fe 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
197ff 62 3b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49  b;.    Index *pI
19800 64 78 3b 0a 20 20 20 20 45 78 70 72 20 2a 70 45  dx;.    Expr *pE
19801 78 70 72 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d  xpr = p->pEList-
19802 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[0].pExpr;.   
19803 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 45 78 70   int iCol = pExp
19804 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  r->iColumn;.    
19805 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65  Vdbe *v = sqlite
19806 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
19807 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 66  ;..    /* This f
19808 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20  unction is only 
19809 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 77 6f 20  called from two 
1980a 70 6c 61 63 65 73 2e 20 49 6e 20 62 6f 74 68 20  places. In both 
1980b 63 61 73 65 73 20 74 68 65 20 76 64 62 65 0a 20  cases the vdbe. 
1980c 20 20 20 2a 2a 20 68 61 73 20 61 6c 72 65 61 64     ** has alread
1980d 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64  y been allocated
1980e 2e 20 53 6f 20 61 73 73 75 6d 65 20 73 71 6c 69  . So assume sqli
1980f 74 65 33 47 65 74 56 64 62 65 28 29 20 69 73 20  te3GetVdbe() is 
19810 61 6c 77 61 79 73 0a 20 20 20 20 2a 2a 20 73 75  always.    ** su
19811 63 63 65 73 73 66 75 6c 20 68 65 72 65 2e 0a 20  ccessful here.. 
19812 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
19813 28 76 29 3b 0a 20 20 20 20 69 66 28 20 69 43 6f  (v);.    if( iCo
19814 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  l<0 ){.      int
19815 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65   iMem = ++pParse
19816 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 69 6e  ->nMem;.      in
19817 74 20 69 41 64 64 72 3b 0a 20 20 20 20 20 20 54  t iAddr;.      T
19818 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 2d 3e  able *pTab = p->
19819 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 62 3b  pSrc->a[0].pTab;
1981a 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d  .      int iDb =
1981b 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
1981c 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e  Index(db, pTab->
1981d 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20  pSchema);.      
1981e 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42  sqlite3VdbeUsesB
1981f 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 0a 20  tree(v, iDb);.. 
19820 20 20 20 20 20 69 41 64 64 72 20 3d 20 73 71 6c       iAddr = sql
19821 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
19822 2c 20 4f 50 5f 49 66 2c 20 69 4d 65 6d 29 3b 0a  , OP_If, iMem);.
19823 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
19824 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
19825 74 65 67 65 72 2c 20 31 2c 20 69 4d 65 6d 29 3b  teger, 1, iMem);
19826 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  ..      sqlite3O
19827 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c  penTable(pParse,
19828 20 69 54 61 62 2c 20 69 44 62 2c 20 70 54 61 62   iTab, iDb, pTab
19829 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0a  , OP_OpenRead);.
1982a 20 20 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e        eType = IN
1982b 5f 49 4e 44 45 58 5f 52 4f 57 49 44 3b 0a 0a 20  _INDEX_ROWID;.. 
1982c 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1982d 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 41 64 64  JumpHere(v, iAdd
1982e 72 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  r);.    }else{. 
1982f 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c       /* The coll
19830 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 75  ation sequence u
19831 73 65 64 20 62 79 20 74 68 65 20 63 6f 6d 70 61  sed by the compa
19832 72 69 73 6f 6e 2e 20 49 66 20 61 6e 20 69 6e 64  rison. If an ind
19833 65 78 20 69 73 20 74 6f 20 0a 20 20 20 20 20 20  ex is to .      
19834 2a 2a 20 62 65 20 75 73 65 64 20 69 6e 20 70 6c  ** be used in pl
19835 61 63 65 20 6f 66 20 61 20 74 65 6d 70 2d 74 61  ace of a temp-ta
19836 62 6c 65 2c 20 69 74 20 6d 75 73 74 20 62 65 20  ble, it must be 
19837 6f 72 64 65 72 65 64 20 61 63 63 6f 72 64 69 6e  ordered accordin
19838 67 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68  g.      ** to th
19839 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  is collation seq
1983a 75 65 6e 63 65 2e 0a 20 20 20 20 20 20 2a 2f 0a  uence..      */.
1983b 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70        CollSeq *p
1983c 52 65 71 20 3d 20 73 71 6c 69 74 65 33 42 69 6e  Req = sqlite3Bin
1983d 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65  aryCompareCollSe
1983e 71 28 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 4c  q(pParse, pX->pL
1983f 65 66 74 2c 20 70 45 78 70 72 29 3b 0a 0a 20 20  eft, pExpr);..  
19840 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61      /* Check tha
19841 74 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 74  t the affinity t
19842 68 61 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64  hat will be used
19843 20 74 6f 20 70 65 72 66 6f 72 6d 20 74 68 65 20   to perform the 
19844 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72  .      ** compar
19845 69 73 6f 6e 20 69 73 20 74 68 65 20 73 61 6d 65  ison is the same
19846 20 61 73 20 74 68 65 20 61 66 66 69 6e 69 74 79   as the affinity
19847 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20   of the column. 
19848 49 66 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 69  If.      ** it i
19849 73 20 6e 6f 74 2c 20 69 74 20 69 73 20 6e 6f 74  s not, it is not
1984a 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65   possible to use
1984b 20 61 6e 79 20 69 6e 64 65 78 2e 0a 20 20 20 20   any index..    
1984c 20 20 2a 2f 0a 20 20 20 20 20 20 54 61 62 6c 65    */.      Table
1984d 20 2a 70 54 61 62 20 3d 20 70 2d 3e 70 53 72 63   *pTab = p->pSrc
1984e 2d 3e 61 5b 30 5d 2e 70 54 61 62 3b 0a 20 20 20  ->a[0].pTab;.   
1984f 20 20 20 63 68 61 72 20 61 66 66 20 3d 20 63 6f     char aff = co
19850 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 74 79  mparisonAffinity
19851 28 70 58 29 3b 0a 20 20 20 20 20 20 69 6e 74 20  (pX);.      int 
19852 61 66 66 69 6e 69 74 79 5f 6f 6b 20 3d 20 28 70  affinity_ok = (p
19853 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e  Tab->aCol[iCol].
19854 61 66 66 69 6e 69 74 79 3d 3d 61 66 66 7c 7c 61  affinity==aff||a
19855 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ff==SQLITE_AFF_N
19856 4f 4e 45 29 3b 0a 0a 20 20 20 20 20 20 66 6f 72  ONE);..      for
19857 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64  (pIdx=pTab->pInd
19858 65 78 3b 20 70 49 64 78 20 26 26 20 65 54 79 70  ex; pIdx && eTyp
19859 65 3d 3d 30 20 26 26 20 61 66 66 69 6e 69 74 79  e==0 && affinity
1985a 5f 6f 6b 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  _ok; pIdx=pIdx->
1985b 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20  pNext){.        
1985c 69 66 28 20 28 70 49 64 78 2d 3e 61 69 43 6f 6c  if( (pIdx->aiCol
1985d 75 6d 6e 5b 30 5d 3d 3d 69 43 6f 6c 29 0a 20 20  umn[0]==iCol).  
1985e 20 20 20 20 20 20 20 26 26 20 28 70 52 65 71 3d         && (pReq=
1985f 3d 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c  =sqlite3FindColl
19860 53 65 71 28 64 62 2c 20 45 4e 43 28 64 62 29 2c  Seq(db, ENC(db),
19861 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 30 5d   pIdx->azColl[0]
19862 2c 20 2d 31 2c 20 30 29 29 0a 20 20 20 20 20 20  , -1, 0)).      
19863 20 20 20 26 26 20 28 21 6d 75 73 74 42 65 55 6e     && (!mustBeUn
19864 69 71 75 65 20 7c 7c 20 28 70 49 64 78 2d 3e 6e  ique || (pIdx->n
19865 43 6f 6c 75 6d 6e 3d 3d 31 20 26 26 20 70 49 64  Column==1 && pId
19866 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e  x->onError!=OE_N
19867 6f 6e 65 29 29 0a 20 20 20 20 20 20 20 20 29 7b  one)).        ){
19868 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69  .          int i
19869 44 62 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e  Db;.          in
1986a 74 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73  t iMem = ++pPars
1986b 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20  e->nMem;.       
1986c 20 20 20 69 6e 74 20 69 41 64 64 72 3b 0a 20 20     int iAddr;.  
1986d 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 70 4b          char *pK
1986e 65 79 3b 0a 20 20 0a 20 20 20 20 20 20 20 20 20  ey;.  .         
1986f 20 70 4b 65 79 20 3d 20 28 63 68 61 72 20 2a 29   pKey = (char *)
19870 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69  sqlite3IndexKeyi
19871 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49 64 78  nfo(pParse, pIdx
19872 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 44 62  );.          iDb
19873 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
19874 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 49 64 78  ToIndex(db, pIdx
19875 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20  ->pSchema);.    
19876 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
19877 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44  eUsesBtree(v, iD
19878 62 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 69  b);..          i
19879 41 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  Addr = sqlite3Vd
1987a 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
1987b 66 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20  f, iMem);.      
1987c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1987d 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
1987e 67 65 72 2c 20 31 2c 20 69 4d 65 6d 29 3b 0a 20  ger, 1, iMem);. 
1987f 20 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69   .          sqli
19880 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
19881 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e   OP_SetNumColumn
19882 73 2c 20 30 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c  s, 0, pIdx->nCol
19883 75 6d 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20  umn);.          
19884 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
19885 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64  4(v, OP_OpenRead
19886 2c 20 69 54 61 62 2c 20 70 49 64 78 2d 3e 74 6e  , iTab, pIdx->tn
19887 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 20  um, iDb,.       
19888 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19889 20 20 20 20 20 20 20 20 70 4b 65 79 2c 50 34 5f          pKey,P4_
1988a 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29  KEYINFO_HANDOFF)
1988b 3b 0a 20 20 20 20 20 20 20 20 20 20 56 64 62 65  ;.          Vdbe
1988c 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22  Comment((v, "%s"
1988d 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b  , pIdx->zName));
1988e 0a 20 20 20 20 20 20 20 20 20 20 65 54 79 70 65  .          eType
1988f 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 45   = IN_INDEX_INDE
19890 58 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 73 71  X;..          sq
19891 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
19892 65 28 76 2c 20 69 41 64 64 72 29 3b 0a 20 20 20  e(v, iAddr);.   
19893 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
19894 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
19895 65 54 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20  eType==0 ){.    
19896 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65  sqlite3CodeSubse
19897 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 58 29  lect(pParse, pX)
19898 3b 0a 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e  ;.    eType = IN
19899 5f 49 4e 44 45 58 5f 45 50 48 3b 0a 20 20 7d 65  _INDEX_EPH;.  }e
1989a 6c 73 65 7b 0a 20 20 20 20 70 58 2d 3e 69 54 61  lse{.    pX->iTa
1989b 62 6c 65 20 3d 20 69 54 61 62 3b 0a 20 20 7d 0a  ble = iTab;.  }.
1989c 20 20 72 65 74 75 72 6e 20 65 54 79 70 65 3b 0a    return eType;.
1989d 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
1989e 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f  Generate code fo
1989f 72 20 73 63 61 6c 61 72 20 73 75 62 71 75 65 72  r scalar subquer
198a0 69 65 73 20 75 73 65 64 20 61 73 20 61 6e 20 65  ies used as an e
198a1 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 61 6e 64  xpression.** and
198a2 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 2e 20 20   IN operators.  
198a3 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20  Examples:.**.** 
198a4 20 20 20 20 28 53 45 4c 45 43 54 20 61 20 46 52      (SELECT a FR
198a5 4f 4d 20 62 29 20 20 20 20 20 20 20 20 20 20 2d  OM b)          -
198a6 2d 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 20 20  - subquery.**   
198a7 20 20 45 58 49 53 54 53 20 28 53 45 4c 45 43 54    EXISTS (SELECT
198a8 20 61 20 46 52 4f 4d 20 62 29 20 20 20 2d 2d 20   a FROM b)   -- 
198a9 45 58 49 53 54 53 20 73 75 62 71 75 65 72 79 0a  EXISTS subquery.
198aa 2a 2a 20 20 20 20 20 78 20 49 4e 20 28 34 2c 35  **     x IN (4,5
198ab 2c 31 31 29 20 20 20 20 20 20 20 20 20 20 20 20  ,11)            
198ac 20 20 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f 72    -- IN operator
198ad 20 77 69 74 68 20 6c 69 73 74 20 6f 6e 20 72 69   with list on ri
198ae 67 68 74 2d 68 61 6e 64 20 73 69 64 65 0a 2a 2a  ght-hand side.**
198af 20 20 20 20 20 78 20 49 4e 20 28 53 45 4c 45 43       x IN (SELEC
198b0 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20 20 20  T a FROM b)     
198b1 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 77  -- IN operator w
198b2 69 74 68 20 73 75 62 71 75 65 72 79 20 6f 6e 20  ith subquery on 
198b3 74 68 65 20 72 69 67 68 74 0a 2a 2a 0a 2a 2a 20  the right.**.** 
198b4 54 68 65 20 70 45 78 70 72 20 70 61 72 61 6d 65  The pExpr parame
198b5 74 65 72 20 64 65 73 63 72 69 62 65 73 20 74 68  ter describes th
198b6 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61  e expression tha
198b7 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 49  t contains the I
198b8 4e 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 6f 72  N.** operator or
198b9 20 73 75 62 71 75 65 72 79 2e 0a 2a 2f 0a 23 69   subquery..*/.#i
198ba 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
198bb 54 5f 53 55 42 51 55 45 52 59 0a 53 51 4c 49 54  T_SUBQUERY.SQLIT
198bc 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
198bd 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c  qlite3CodeSubsel
198be 65 63 74 28 50 61 72 73 65 20 2a 70 50 61 72 73  ect(Parse *pPars
198bf 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b  e, Expr *pExpr){
198c0 0a 20 20 69 6e 74 20 74 65 73 74 41 64 64 72 20  .  int testAddr 
198c1 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
198c2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
198c3 65 2d 74 69 6d 65 20 74 65 73 74 20 61 64 64 72  e-time test addr
198c4 65 73 73 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76  ess */.  Vdbe *v
198c5 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
198c6 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
198c7 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   v==0 ) return;.
198c8 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f 64 65  ..  /* This code
198c9 20 6d 75 73 74 20 62 65 20 72 75 6e 20 69 6e 20   must be run in 
198ca 69 74 73 20 65 6e 74 69 72 65 74 79 20 65 76 65  its entirety eve
198cb 72 79 20 74 69 6d 65 20 69 74 20 69 73 20 65 6e  ry time it is en
198cc 63 6f 75 6e 74 65 72 65 64 0a 20 20 2a 2a 20 69  countered.  ** i
198cd 66 20 61 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c  f any of the fol
198ce 6c 6f 77 69 6e 67 20 69 73 20 74 72 75 65 3a 0a  lowing is true:.
198cf 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 2a 20 20    **.  **    *  
198d0 54 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73  The right-hand s
198d1 69 64 65 20 69 73 20 61 20 63 6f 72 72 65 6c 61  ide is a correla
198d2 74 65 64 20 73 75 62 71 75 65 72 79 0a 20 20 2a  ted subquery.  *
198d3 2a 20 20 20 20 2a 20 20 54 68 65 20 72 69 67 68  *    *  The righ
198d4 74 2d 68 61 6e 64 20 73 69 64 65 20 69 73 20 61  t-hand side is a
198d5 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  n expression lis
198d6 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 76 61 72  t containing var
198d7 69 61 62 6c 65 73 0a 20 20 2a 2a 20 20 20 20 2a  iables.  **    *
198d8 20 20 57 65 20 61 72 65 20 69 6e 73 69 64 65 20    We are inside 
198d9 61 20 74 72 69 67 67 65 72 0a 20 20 2a 2a 0a 20  a trigger.  **. 
198da 20 2a 2a 20 49 66 20 61 6c 6c 20 6f 66 20 74 68   ** If all of th
198db 65 20 61 62 6f 76 65 20 61 72 65 20 66 61 6c 73  e above are fals
198dc 65 2c 20 74 68 65 6e 20 77 65 20 63 61 6e 20 72  e, then we can r
198dd 75 6e 20 74 68 69 73 20 63 6f 64 65 20 6a 75 73  un this code jus
198de 74 20 6f 6e 63 65 0a 20 20 2a 2a 20 73 61 76 65  t once.  ** save
198df 20 74 68 65 20 72 65 73 75 6c 74 73 2c 20 61 6e   the results, an
198e0 64 20 72 65 75 73 65 20 74 68 65 20 73 61 6d 65  d reuse the same
198e1 20 72 65 73 75 6c 74 20 6f 6e 20 73 75 62 73 65   result on subse
198e2 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e  quent invocation
198e3 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 45  s..  */.  if( !E
198e4 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74  xprHasAnyPropert
198e5 79 28 70 45 78 70 72 2c 20 45 50 5f 56 61 72 53  y(pExpr, EP_VarS
198e6 65 6c 65 63 74 29 20 26 26 20 21 70 50 61 72 73  elect) && !pPars
198e7 65 2d 3e 74 72 69 67 53 74 61 63 6b 20 29 7b 0a  e->trigStack ){.
198e8 20 20 20 20 69 6e 74 20 6d 65 6d 20 3d 20 2b 2b      int mem = ++
198e9 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
198ea 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
198eb 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 6d 65  Op1(v, OP_If, me
198ec 6d 29 3b 0a 20 20 20 20 74 65 73 74 41 64 64 72  m);.    testAddr
198ed 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
198ee 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
198ef 65 72 2c 20 31 2c 20 6d 65 6d 29 3b 0a 20 20 20  er, 1, mem);.   
198f0 20 61 73 73 65 72 74 28 20 74 65 73 74 41 64 64   assert( testAdd
198f1 72 3e 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64  r>0 || pParse->d
198f2 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
198f3 29 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 74 63 68  );.  }..  switch
198f4 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20  ( pExpr->op ){. 
198f5 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a 20 7b     case TK_IN: {
198f6 0a 20 20 20 20 20 20 63 68 61 72 20 61 66 66 69  .      char affi
198f7 6e 69 74 79 3b 0a 20 20 20 20 20 20 4b 65 79 49  nity;.      KeyI
198f8 6e 66 6f 20 6b 65 79 49 6e 66 6f 3b 0a 20 20 20  nfo keyInfo;.   
198f9 20 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20     int addr;    
198fa 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
198fb 66 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  f OP_OpenEphemer
198fc 61 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a  al instruction *
198fd 2f 0a 0a 20 20 20 20 20 20 61 66 66 69 6e 69 74  /..      affinit
198fe 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  y = sqlite3ExprA
198ff 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 70  ffinity(pExpr->p
19900 4c 65 66 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  Left);..      /*
19901 20 57 68 65 74 68 65 72 20 74 68 69 73 20 69 73   Whether this is
19902 20 61 6e 20 27 78 20 49 4e 28 53 45 4c 45 43 54   an 'x IN(SELECT
19903 2e 2e 2e 29 27 20 6f 72 20 61 6e 20 27 78 20 49  ...)' or an 'x I
19904 4e 28 3c 65 78 70 72 6c 69 73 74 3e 29 27 0a 20  N(<exprlist>)'. 
19905 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69       ** expressi
19906 6f 6e 20 69 74 20 69 73 20 68 61 6e 64 6c 65 64  on it is handled
19907 20 74 68 65 20 73 61 6d 65 20 77 61 79 2e 20 41   the same way. A
19908 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69   virtual table i
19909 73 20 0a 20 20 20 20 20 20 2a 2a 20 66 69 6c 6c  s .      ** fill
1990a 65 64 20 77 69 74 68 20 73 69 6e 67 6c 65 2d 66  ed with single-f
1990b 69 65 6c 64 20 69 6e 64 65 78 20 6b 65 79 73 20  ield index keys 
1990c 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65  representing the
1990d 20 72 65 73 75 6c 74 73 0a 20 20 20 20 20 20 2a   results.      *
1990e 2a 20 66 72 6f 6d 20 74 68 65 20 53 45 4c 45 43  * from the SELEC
1990f 54 20 6f 72 20 74 68 65 20 3c 65 78 70 72 6c 69  T or the <exprli
19910 73 74 3e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20  st>..      **.  
19911 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 27 78      ** If the 'x
19912 27 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  ' expression is 
19913 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20  a column value, 
19914 6f 72 20 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e  or the SELECT...
19915 0a 20 20 20 20 20 20 2a 2a 20 73 74 61 74 65 6d  .      ** statem
19916 65 6e 74 20 72 65 74 75 72 6e 73 20 61 20 63 6f  ent returns a co
19917 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 74 68 65 6e  lumn value, then
19918 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66   the affinity of
19919 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 63   that.      ** c
1991a 6f 6c 75 6d 6e 20 69 73 20 75 73 65 64 20 74 6f  olumn is used to
1991b 20 62 75 69 6c 64 20 74 68 65 20 69 6e 64 65 78   build the index
1991c 20 6b 65 79 73 2e 20 49 66 20 62 6f 74 68 20 27   keys. If both '
1991d 78 27 20 61 6e 64 20 74 68 65 0a 20 20 20 20 20  x' and the.     
1991e 20 2a 2a 20 53 45 4c 45 43 54 2e 2e 2e 20 73 74   ** SELECT... st
1991f 61 74 65 6d 65 6e 74 20 61 72 65 20 63 6f 6c 75  atement are colu
19920 6d 6e 73 2c 20 74 68 65 6e 20 6e 75 6d 65 72 69  mns, then numeri
19921 63 20 61 66 66 69 6e 69 74 79 20 69 73 20 75 73  c affinity is us
19922 65 64 0a 20 20 20 20 20 20 2a 2a 20 69 66 20 65  ed.      ** if e
19923 69 74 68 65 72 20 63 6f 6c 75 6d 6e 20 68 61 73  ither column has
19924 20 4e 55 4d 45 52 49 43 20 6f 72 20 49 4e 54 45   NUMERIC or INTE
19925 47 45 52 20 61 66 66 69 6e 69 74 79 2e 20 49 66  GER affinity. If
19926 20 6e 65 69 74 68 65 72 0a 20 20 20 20 20 20 2a   neither.      *
19927 2a 20 27 78 27 20 6e 6f 72 20 74 68 65 20 53 45  * 'x' nor the SE
19928 4c 45 43 54 2e 2e 2e 20 73 74 61 74 65 6d 65 6e  LECT... statemen
19929 74 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 74  t are columns, t
1992a 68 65 6e 20 6e 75 6d 65 72 69 63 20 61 66 66 69  hen numeric affi
1992b 6e 69 74 79 0a 20 20 20 20 20 20 2a 2a 20 69 73  nity.      ** is
1992c 20 75 73 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a   used..      */.
1992d 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61        pExpr->iTa
1992e 62 6c 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  ble = pParse->nT
1992f 61 62 2b 2b 3b 0a 20 20 20 20 20 20 61 64 64 72  ab++;.      addr
19930 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
19931 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45  dOp2(v, OP_OpenE
19932 70 68 65 6d 65 72 61 6c 2c 20 70 45 78 70 72 2d  phemeral, pExpr-
19933 3e 69 54 61 62 6c 65 2c 20 31 29 3b 0a 20 20 20  >iTable, 1);.   
19934 20 20 20 6d 65 6d 73 65 74 28 26 6b 65 79 49 6e     memset(&keyIn
19935 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6b 65  fo, 0, sizeof(ke
19936 79 49 6e 66 6f 29 29 3b 0a 20 20 20 20 20 20 6b  yInfo));.      k
19937 65 79 49 6e 66 6f 2e 6e 46 69 65 6c 64 20 3d 20  eyInfo.nField = 
19938 31 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 70 45  1;..      if( pE
19939 78 70 72 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a  xpr->pSelect ){.
1993a 20 20 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20          /* Case 
1993b 31 3a 20 20 20 20 20 65 78 70 72 20 49 4e 20 28  1:     expr IN (
1993c 53 45 4c 45 43 54 20 2e 2e 2e 29 0a 20 20 20 20  SELECT ...).    
1993d 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
1993e 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
1993f 74 6f 20 77 72 69 74 65 20 74 68 65 20 72 65 73  to write the res
19940 75 6c 74 73 20 6f 66 20 74 68 65 20 73 65 6c 65  ults of the sele
19941 63 74 20 69 6e 74 6f 20 74 68 65 20 74 65 6d 70  ct into the temp
19942 6f 72 61 72 79 0a 20 20 20 20 20 20 20 20 2a 2a  orary.        **
19943 20 74 61 62 6c 65 20 61 6c 6c 6f 63 61 74 65 64   table allocated
19944 20 61 6e 64 20 6f 70 65 6e 65 64 20 61 62 6f 76   and opened abov
19945 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  e..        */.  
19946 20 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 74        SelectDest
19947 20 64 65 73 74 3b 0a 20 20 20 20 20 20 20 20 45   dest;.        E
19948 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b  xprList *pEList;
19949 0a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ..        sqlite
1994a 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28  3SelectDestInit(
1994b 26 64 65 73 74 2c 20 53 52 54 5f 53 65 74 2c 20  &dest, SRT_Set, 
1994c 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 29 3b 0a  pExpr->iTable);.
1994d 20 20 20 20 20 20 20 20 64 65 73 74 2e 61 66 66          dest.aff
1994e 69 6e 69 74 79 20 3d 20 28 69 6e 74 29 61 66 66  inity = (int)aff
1994f 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 20 20 61  inity;.        a
19950 73 73 65 72 74 28 20 28 70 45 78 70 72 2d 3e 69  ssert( (pExpr->i
19951 54 61 62 6c 65 26 30 78 30 30 30 30 46 46 46 46  Table&0x0000FFFF
19952 29 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  )==pExpr->iTable
19953 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
19954 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50  sqlite3Select(pP
19955 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 53 65  arse, pExpr->pSe
19956 6c 65 63 74 2c 20 26 64 65 73 74 2c 20 30 2c 20  lect, &dest, 0, 
19957 30 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20  0, 0, 0) ){.    
19958 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
19959 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1995a 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e  pEList = pExpr->
1995b 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b  pSelect->pEList;
1995c 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 45 4c  .        if( pEL
1995d 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e  ist && pEList->n
1995e 45 78 70 72 3e 30 20 29 7b 20 0a 20 20 20 20 20  Expr>0 ){ .     
1995f 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e 61 43 6f       keyInfo.aCo
19960 6c 6c 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 42  ll[0] = sqlite3B
19961 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c  inaryCompareColl
19962 53 65 71 28 70 50 61 72 73 65 2c 20 70 45 78 70  Seq(pParse, pExp
19963 72 2d 3e 70 4c 65 66 74 2c 0a 20 20 20 20 20 20  r->pLeft,.      
19964 20 20 20 20 20 20 20 20 70 45 4c 69 73 74 2d 3e          pEList->
19965 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20  a[0].pExpr);.   
19966 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
19967 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e 70 4c  se if( pExpr->pL
19968 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ist ){.        /
19969 2a 20 43 61 73 65 20 32 3a 20 20 20 20 20 65 78  * Case 2:     ex
1996a 70 72 20 49 4e 20 28 65 78 70 72 6c 69 73 74 29  pr IN (exprlist)
1996b 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
1996c 20 20 20 20 2a 2a 20 46 6f 72 20 65 61 63 68 20      ** For each 
1996d 65 78 70 72 65 73 73 69 6f 6e 2c 20 62 75 69 6c  expression, buil
1996e 64 20 61 6e 20 69 6e 64 65 78 20 6b 65 79 20 66  d an index key f
1996f 72 6f 6d 20 74 68 65 20 65 76 61 6c 75 61 74 69  rom the evaluati
19970 6f 6e 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a  on and.        *
19971 2a 20 73 74 6f 72 65 20 69 74 20 69 6e 20 74 68  * store it in th
19972 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  e temporary tabl
19973 65 2e 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20  e. If <expr> is 
19974 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 75  a column, then u
19975 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  se.        ** th
19976 61 74 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e  at columns affin
19977 69 74 79 20 77 68 65 6e 20 62 75 69 6c 64 69 6e  ity when buildin
19978 67 20 69 6e 64 65 78 20 6b 65 79 73 2e 20 49 66  g index keys. If
19979 20 3c 65 78 70 72 3e 20 69 73 20 6e 6f 74 0a 20   <expr> is not. 
1997a 20 20 20 20 20 20 20 2a 2a 20 61 20 63 6f 6c 75         ** a colu
1997b 6d 6e 2c 20 75 73 65 20 6e 75 6d 65 72 69 63 20  mn, use numeric 
1997c 61 66 66 69 6e 69 74 79 2e 0a 20 20 20 20 20 20  affinity..      
1997d 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74    */.        int
1997e 20 69 3b 0a 20 20 20 20 20 20 20 20 45 78 70 72   i;.        Expr
1997f 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45  List *pList = pE
19980 78 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 20 20  xpr->pList;.    
19981 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
19982 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
19983 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 31 2c  .        int r1,
19984 20 72 32 3b 0a 0a 20 20 20 20 20 20 20 20 69 66   r2;..        if
19985 28 20 21 61 66 66 69 6e 69 74 79 20 29 7b 0a 20  ( !affinity ){. 
19986 20 20 20 20 20 20 20 20 20 61 66 66 69 6e 69 74           affinit
19987 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  y = SQLITE_AFF_N
19988 4f 4e 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ONE;.        }. 
19989 20 20 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e 61         keyInfo.a
1998a 43 6f 6c 6c 5b 30 5d 20 3d 20 70 45 78 70 72 2d  Coll[0] = pExpr-
1998b 3e 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 0a  >pLeft->pColl;..
1998c 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
1998d 74 68 72 6f 75 67 68 20 65 61 63 68 20 65 78 70  through each exp
1998e 72 65 73 73 69 6f 6e 20 69 6e 20 3c 65 78 70 72  ression in <expr
1998f 6c 69 73 74 3e 2e 20 2a 2f 0a 20 20 20 20 20 20  list>. */.      
19990 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65    r1 = sqlite3Ge
19991 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
19992 3b 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 73  ;.        r2 = s
19993 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
19994 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
19995 20 20 66 6f 72 28 69 3d 70 4c 69 73 74 2d 3e 6e    for(i=pList->n
19996 45 78 70 72 2c 20 70 49 74 65 6d 3d 70 4c 69 73  Expr, pItem=pLis
19997 74 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20  t->a; i>0; i--, 
19998 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pItem++){.      
19999 20 20 20 20 45 78 70 72 20 2a 70 45 32 20 3d 20      Expr *pE2 = 
1999a 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 0a 20  pItem->pExpr;.. 
1999b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74           /* If t
1999c 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
1999d 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 20 74 68   not constant th
1999e 65 6e 20 77 65 20 77 69 6c 6c 20 6e 65 65 64 20  en we will need 
1999f 74 6f 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  to.          ** 
199a0 64 69 73 61 62 6c 65 20 74 68 65 20 74 65 73 74  disable the test
199a1 20 74 68 61 74 20 77 61 73 20 67 65 6e 65 72 61   that was genera
199a2 74 65 64 20 61 62 6f 76 65 20 74 68 61 74 20 6d  ted above that m
199a3 61 6b 65 73 20 73 75 72 65 0a 20 20 20 20 20 20  akes sure.      
199a4 20 20 20 20 2a 2a 20 74 68 69 73 20 63 6f 64 65      ** this code
199a5 20 6f 6e 6c 79 20 65 78 65 63 75 74 65 73 20 6f   only executes o
199a6 6e 63 65 2e 20 20 42 65 63 61 75 73 65 20 66 6f  nce.  Because fo
199a7 72 20 61 20 6e 6f 6e 2d 63 6f 6e 73 74 61 6e 74  r a non-constant
199a8 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 65 78  .          ** ex
199a9 70 72 65 73 73 69 6f 6e 20 77 65 20 6e 65 65 64  pression we need
199aa 20 74 6f 20 72 65 72 75 6e 20 74 68 69 73 20 63   to rerun this c
199ab 6f 64 65 20 65 61 63 68 20 74 69 6d 65 2e 0a 20  ode each time.. 
199ac 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
199ad 20 20 20 20 20 20 69 66 28 20 74 65 73 74 41 64        if( testAd
199ae 64 72 20 26 26 20 21 73 71 6c 69 74 65 33 45 78  dr && !sqlite3Ex
199af 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 70 45 32  prIsConstant(pE2
199b0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
199b1 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
199b2 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 74 65 73 74  geToNoop(v, test
199b3 41 64 64 72 2d 31 2c 20 32 29 3b 0a 20 20 20 20  Addr-1, 2);.    
199b4 20 20 20 20 20 20 20 20 74 65 73 74 41 64 64 72          testAddr
199b5 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
199b6 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  }..          /* 
199b7 45 76 61 6c 75 61 74 65 20 74 68 65 20 65 78 70  Evaluate the exp
199b8 72 65 73 73 69 6f 6e 20 61 6e 64 20 69 6e 73 65  ression and inse
199b9 72 74 20 69 74 20 69 6e 74 6f 20 74 68 65 20 74  rt it into the t
199ba 65 6d 70 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20  emp table */.   
199bb 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64         pParse->d
199bc 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 2b 2b  isableColCache++
199bd 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
199be 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
199bf 73 65 2c 20 70 45 32 2c 20 72 31 29 3b 0a 20 20  se, pE2, r1);.  
199c0 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
199c1 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43  pParse->disableC
199c2 6f 6c 43 61 63 68 65 3e 30 20 29 3b 0a 20 20 20  olCache>0 );.   
199c3 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64         pParse->d
199c4 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 2d 2d  isableColCache--
199c5 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
199c6 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
199c7 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20   OP_MakeRecord, 
199c8 72 31 2c 20 31 2c 20 72 32 2c 20 26 61 66 66 69  r1, 1, r2, &affi
199c9 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20 20 20 20  nity, 1);.      
199ca 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
199cb 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e  acheAffinityChan
199cc 67 65 28 70 50 61 72 73 65 2c 20 72 31 2c 20 31  ge(pParse, r1, 1
199cd 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
199ce 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
199cf 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20  , OP_IdxInsert, 
199d0 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72  pExpr->iTable, r
199d1 32 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  2);.        }.  
199d2 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c        sqlite3Rel
199d3 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
199d4 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20  se, r1);.       
199d5 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
199d6 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
199d7 32 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  2);.      }.    
199d8 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
199d9 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2c 20 28  ngeP4(v, addr, (
199da 76 6f 69 64 20 2a 29 26 6b 65 79 49 6e 66 6f 2c  void *)&keyInfo,
199db 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20 20   P4_KEYINFO);.  
199dc 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
199dd 0a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58  ..    case TK_EX
199de 49 53 54 53 3a 0a 20 20 20 20 63 61 73 65 20 54  ISTS:.    case T
199df 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20  K_SELECT: {.    
199e0 20 20 2f 2a 20 54 68 69 73 20 68 61 73 20 74 6f    /* This has to
199e1 20 62 65 20 61 20 73 63 61 6c 61 72 20 53 45 4c   be a scalar SEL
199e2 45 43 54 2e 20 20 47 65 6e 65 72 61 74 65 20 63  ECT.  Generate c
199e3 6f 64 65 20 74 6f 20 70 75 74 20 74 68 65 0a 20  ode to put the. 
199e4 20 20 20 20 20 2a 2a 20 76 61 6c 75 65 20 6f 66       ** value of
199e5 20 74 68 69 73 20 73 65 6c 65 63 74 20 69 6e 20   this select in 
199e6 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e  a memory cell an
199e7 64 20 72 65 63 6f 72 64 20 74 68 65 20 6e 75 6d  d record the num
199e8 62 65 72 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20  ber.      ** of 
199e9 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  the memory cell 
199ea 69 6e 20 69 43 6f 6c 75 6d 6e 2e 0a 20 20 20 20  in iColumn..    
199eb 20 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69    */.      stati
199ec 63 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 6f 6e  c const Token on
199ed 65 20 3d 20 7b 20 28 75 38 2a 29 22 31 22 2c 20  e = { (u8*)"1", 
199ee 30 2c 20 31 20 7d 3b 0a 20 20 20 20 20 20 53 65  0, 1 };.      Se
199ef 6c 65 63 74 20 2a 70 53 65 6c 3b 0a 20 20 20 20  lect *pSel;.    
199f0 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73    SelectDest des
199f1 74 3b 0a 0a 20 20 20 20 20 20 70 53 65 6c 20 3d  t;..      pSel =
199f2 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 3b   pExpr->pSelect;
199f3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65  .      sqlite3Se
199f4 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65  lectDestInit(&de
199f5 73 74 2c 20 30 2c 20 2b 2b 70 50 61 72 73 65 2d  st, 0, ++pParse-
199f6 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 20 20 69 66  >nMem);.      if
199f7 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
199f8 53 45 4c 45 43 54 20 29 7b 0a 20 20 20 20 20 20  SELECT ){.      
199f9 20 20 64 65 73 74 2e 65 44 65 73 74 20 3d 20 53    dest.eDest = S
199fa 52 54 5f 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20  RT_Mem;.        
199fb 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
199fc 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
199fd 20 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a 20 20   dest.iParm);.  
199fe 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
199ff 74 28 28 76 2c 20 22 49 6e 69 74 20 73 75 62 71  t((v, "Init subq
19a00 75 65 72 79 20 72 65 73 75 6c 74 22 29 29 3b 0a  uery result"));.
19a01 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
19a02 20 20 20 20 20 64 65 73 74 2e 65 44 65 73 74 20       dest.eDest 
19a03 3d 20 53 52 54 5f 45 78 69 73 74 73 3b 0a 20 20  = SRT_Exists;.  
19a04 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
19a05 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
19a06 74 65 67 65 72 2c 20 30 2c 20 64 65 73 74 2e 69  teger, 0, dest.i
19a07 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 20 20 56  Parm);.        V
19a08 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
19a09 49 6e 69 74 20 45 58 49 53 54 53 20 72 65 73 75  Init EXISTS resu
19a0a 6c 74 22 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20  lt"));.      }. 
19a0b 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
19a0c 44 65 6c 65 74 65 28 70 53 65 6c 2d 3e 70 4c 69  Delete(pSel->pLi
19a0d 6d 69 74 29 3b 0a 20 20 20 20 20 20 70 53 65 6c  mit);.      pSel
19a0e 2d 3e 70 4c 69 6d 69 74 20 3d 20 73 71 6c 69 74  ->pLimit = sqlit
19a0f 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
19a10 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30 2c 20 30  TK_INTEGER, 0, 0
19a11 2c 20 26 6f 6e 65 29 3b 0a 20 20 20 20 20 20 69  , &one);.      i
19a12 66 28 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  f( sqlite3Select
19a13 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2c 20 26  (pParse, pSel, &
19a14 64 65 73 74 2c 20 30 2c 20 30 2c 20 30 2c 20 30  dest, 0, 0, 0, 0
19a15 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  ) ){.        ret
19a16 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
19a17 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d     pExpr->iColum
19a18 6e 20 3d 20 64 65 73 74 2e 69 50 61 72 6d 3b 0a  n = dest.iParm;.
19a19 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
19a1a 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 74 65   }.  }..  if( te
19a1b 73 74 41 64 64 72 20 29 7b 0a 20 20 20 20 73 71  stAddr ){.    sq
19a1c 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
19a1d 65 28 76 2c 20 74 65 73 74 41 64 64 72 2d 31 29  e(v, testAddr-1)
19a1e 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 3b  ;.  }..  return;
19a1f 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
19a20 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
19a21 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 75 70 6c  Y */../*.** Dupl
19a22 69 63 61 74 65 20 61 6e 20 38 2d 62 79 74 65 20  icate an 8-byte 
19a23 76 61 6c 75 65 0a 2a 2f 0a 73 74 61 74 69 63 20  value.*/.static 
19a24 63 68 61 72 20 2a 64 75 70 38 62 79 74 65 73 28  char *dup8bytes(
19a25 56 64 62 65 20 2a 76 2c 20 63 6f 6e 73 74 20 63  Vdbe *v, const c
19a26 68 61 72 20 2a 69 6e 29 7b 0a 20 20 63 68 61 72  har *in){.  char
19a27 20 2a 6f 75 74 20 3d 20 73 71 6c 69 74 65 33 44   *out = sqlite3D
19a28 62 4d 61 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74  bMallocRaw(sqlit
19a29 65 33 56 64 62 65 44 62 28 76 29 2c 20 38 29 3b  e3VdbeDb(v), 8);
19a2a 0a 20 20 69 66 28 20 6f 75 74 20 29 7b 0a 20 20  .  if( out ){.  
19a2b 20 20 6d 65 6d 63 70 79 28 6f 75 74 2c 20 69 6e    memcpy(out, in
19a2c 2c 20 38 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , 8);.  }.  retu
19a2d 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rn out;.}../*.**
19a2e 20 47 65 6e 65 72 61 74 65 20 61 6e 20 69 6e 73   Generate an ins
19a2f 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 77 69  truction that wi
19a30 6c 6c 20 70 75 74 20 74 68 65 20 66 6c 6f 61 74  ll put the float
19a31 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 76 61 6c  ing point.** val
19a32 75 65 20 64 65 73 63 72 69 62 65 64 20 62 79 20  ue described by 
19a33 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e 74 6f 20 72  z[0..n-1] into r
19a34 65 67 69 73 74 65 72 20 69 4d 65 6d 2e 0a 2a 2a  egister iMem..**
19a35 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20 73 74 72 69  .** The z[] stri
19a36 6e 67 20 77 69 6c 6c 20 70 72 6f 62 61 62 6c 79  ng will probably
19a37 20 6e 6f 74 20 62 65 20 7a 65 72 6f 2d 74 65 72   not be zero-ter
19a38 6d 69 6e 61 74 65 64 2e 20 20 42 75 74 20 74 68  minated.  But th
19a39 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63 68 61 72 61  e .** z[n] chara
19a3a 63 74 65 72 20 69 73 20 67 75 61 72 61 6e 74 65  cter is guarante
19a3b 65 64 20 74 6f 20 62 65 20 73 6f 6d 65 74 68 69  ed to be somethi
19a3c 6e 67 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74  ng that does not
19a3d 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b 65 20 74 68   look.** like th
19a3e 65 20 63 6f 6e 74 69 6e 75 61 74 69 6f 6e 20 6f  e continuation o
19a3f 66 20 74 68 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f  f the number..*/
19a40 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64  .static void cod
19a41 65 52 65 61 6c 28 56 64 62 65 20 2a 76 2c 20 63  eReal(Vdbe *v, c
19a42 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e  onst char *z, in
19a43 74 20 6e 2c 20 69 6e 74 20 6e 65 67 61 74 65 46  t n, int negateF
19a44 6c 61 67 2c 20 69 6e 74 20 69 4d 65 6d 29 7b 0a  lag, int iMem){.
19a45 20 20 61 73 73 65 72 74 28 20 7a 20 7c 7c 20 76    assert( z || v
19a46 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 56 64  ==0 || sqlite3Vd
19a47 62 65 44 62 28 76 29 2d 3e 6d 61 6c 6c 6f 63 46  beDb(v)->mallocF
19a48 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 7a  ailed );.  if( z
19a49 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 76   ){.    double v
19a4a 61 6c 75 65 3b 0a 20 20 20 20 63 68 61 72 20 2a  alue;.    char *
19a4b 7a 56 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  zV;.    assert( 
19a4c 21 69 73 64 69 67 69 74 28 7a 5b 6e 5d 29 20 29  !isdigit(z[n]) )
19a4d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f  ;.    sqlite3Ato
19a4e 46 28 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20  F(z, &value);.  
19a4f 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e    if( sqlite3IsN
19a50 61 4e 28 76 61 6c 75 65 29 20 29 7b 0a 20 20 20  aN(value) ){.   
19a51 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
19a52 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
19a53 20 30 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 7d   0, iMem);.    }
19a54 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20  else{.      if( 
19a55 6e 65 67 61 74 65 46 6c 61 67 20 29 20 76 61 6c  negateFlag ) val
19a56 75 65 20 3d 20 2d 76 61 6c 75 65 3b 0a 20 20 20  ue = -value;.   
19a57 20 20 20 7a 56 20 3d 20 64 75 70 38 62 79 74 65     zV = dup8byte
19a58 73 28 76 2c 20 28 63 68 61 72 2a 29 26 76 61 6c  s(v, (char*)&val
19a59 75 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ue);.      sqlit
19a5a 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
19a5b 4f 50 5f 52 65 61 6c 2c 20 30 2c 20 69 4d 65 6d  OP_Real, 0, iMem
19a5c 2c 20 30 2c 20 7a 56 2c 20 50 34 5f 52 45 41 4c  , 0, zV, P4_REAL
19a5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  );.    }.  }.}..
19a5e 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
19a5f 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74  an instruction t
19a60 68 61 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65  hat will put the
19a61 20 69 6e 74 65 67 65 72 20 64 65 73 63 72 69 62   integer describ
19a62 65 20 62 79 0a 2a 2a 20 74 65 78 74 20 7a 5b 30  e by.** text z[0
19a63 2e 2e 6e 2d 31 5d 20 69 6e 74 6f 20 72 65 67 69  ..n-1] into regi
19a64 73 74 65 72 20 69 4d 65 6d 2e 0a 2a 2a 0a 2a 2a  ster iMem..**.**
19a65 20 54 68 65 20 7a 5b 5d 20 73 74 72 69 6e 67 20   The z[] string 
19a66 77 69 6c 6c 20 70 72 6f 62 61 62 6c 79 20 6e 6f  will probably no
19a67 74 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e  t be zero-termin
19a68 61 74 65 64 2e 20 20 42 75 74 20 74 68 65 20 0a  ated.  But the .
19a69 2a 2a 20 7a 5b 6e 5d 20 63 68 61 72 61 63 74 65  ** z[n] characte
19a6a 72 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20  r is guaranteed 
19a6b 74 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 20  to be something 
19a6c 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6c 6f  that does not lo
19a6d 6f 6b 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20 63  ok.** like the c
19a6e 6f 6e 74 69 6e 75 61 74 69 6f 6e 20 6f 66 20 74  ontinuation of t
19a6f 68 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74  he number..*/.st
19a70 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 49 6e  atic void codeIn
19a71 74 65 67 65 72 28 56 64 62 65 20 2a 76 2c 20 63  teger(Vdbe *v, c
19a72 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e  onst char *z, in
19a73 74 20 6e 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67  t n, int negFlag
19a74 2c 20 69 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 61  , int iMem){.  a
19a75 73 73 65 72 74 28 20 7a 20 7c 7c 20 76 3d 3d 30  ssert( z || v==0
19a76 20 7c 7c 20 73 71 6c 69 74 65 33 56 64 62 65 44   || sqlite3VdbeD
19a77 62 28 76 29 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  b(v)->mallocFail
19a78 65 64 20 29 3b 0a 20 20 69 66 28 20 7a 20 29 7b  ed );.  if( z ){
19a79 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
19a7a 61 73 73 65 72 74 28 20 21 69 73 64 69 67 69 74  assert( !isdigit
19a7b 28 7a 5b 6e 5d 29 20 29 3b 0a 20 20 20 20 69 66  (z[n]) );.    if
19a7c 28 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33  ( sqlite3GetInt3
19a7d 32 28 7a 2c 20 26 69 29 20 29 7b 0a 20 20 20 20  2(z, &i) ){.    
19a7e 20 20 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20    if( negFlag ) 
19a7f 69 20 3d 20 2d 69 3b 0a 20 20 20 20 20 20 73 71  i = -i;.      sq
19a80 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
19a81 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69  v, OP_Integer, i
19a82 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 7d 65 6c  , iMem);.    }el
19a83 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 46 69  se if( sqlite3Fi
19a84 74 73 49 6e 36 34 42 69 74 73 28 7a 2c 20 6e 65  tsIn64Bits(z, ne
19a85 67 46 6c 61 67 29 20 29 7b 0a 20 20 20 20 20 20  gFlag) ){.      
19a86 69 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 20 20  i64 value;.     
19a87 20 63 68 61 72 20 2a 7a 56 3b 0a 20 20 20 20 20   char *zV;.     
19a88 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 7a   sqlite3Atoi64(z
19a89 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 20  , &value);.     
19a8a 20 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 76   if( negFlag ) v
19a8b 61 6c 75 65 20 3d 20 2d 76 61 6c 75 65 3b 0a 20  alue = -value;. 
19a8c 20 20 20 20 20 7a 56 20 3d 20 64 75 70 38 62 79       zV = dup8by
19a8d 74 65 73 28 76 2c 20 28 63 68 61 72 2a 29 26 76  tes(v, (char*)&v
19a8e 61 6c 75 65 29 3b 0a 20 20 20 20 20 20 73 71 6c  alue);.      sql
19a8f 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
19a90 2c 20 4f 50 5f 49 6e 74 36 34 2c 20 30 2c 20 69  , OP_Int64, 0, i
19a91 4d 65 6d 2c 20 30 2c 20 7a 56 2c 20 50 34 5f 49  Mem, 0, zV, P4_I
19a92 4e 54 36 34 29 3b 0a 20 20 20 20 7d 65 6c 73 65  NT64);.    }else
19a93 7b 0a 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c  {.      codeReal
19a94 28 76 2c 20 7a 2c 20 6e 2c 20 6e 65 67 46 6c 61  (v, z, n, negFla
19a95 67 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a  g, iMem);.    }.
19a96 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65    }.}.../*.** Ge
19a97 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
19a98 20 77 69 6c 6c 20 65 78 74 72 61 63 74 20 74 68   will extract th
19a99 65 20 69 43 6f 6c 75 6d 6e 2d 74 68 20 63 6f 6c  e iColumn-th col
19a9a 75 6d 6e 20 66 72 6f 6d 0a 2a 2a 20 74 61 62 6c  umn from.** tabl
19a9b 65 20 70 54 61 62 20 61 6e 64 20 73 74 6f 72 65  e pTab and store
19a9c 20 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75   the column valu
19a9d 65 20 69 6e 20 61 20 72 65 67 69 73 74 65 72 2e  e in a register.
19a9e 20 20 41 6e 20 65 66 66 6f 72 74 0a 2a 2a 20 69    An effort.** i
19a9f 73 20 6d 61 64 65 20 74 6f 20 73 74 6f 72 65 20  s made to store 
19aa0 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65  the column value
19aa1 20 69 6e 20 72 65 67 69 73 74 65 72 20 69 52 65   in register iRe
19aa2 67 2c 20 62 75 74 20 74 68 69 73 20 69 73 0a 2a  g, but this is.*
19aa3 2a 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 64  * not guaranteed
19aa4 2e 20 20 54 68 65 20 6c 6f 63 61 74 69 6f 6e 20  .  The location 
19aa5 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61  of the column va
19aa6 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  lue is returned.
19aa7 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73  .**.** There mus
19aa8 74 20 62 65 20 61 6e 20 6f 70 65 6e 20 63 75 72  t be an open cur
19aa9 73 6f 72 20 74 6f 20 70 54 61 62 20 69 6e 20 69  sor to pTab in i
19aaa 54 61 62 6c 65 20 77 68 65 6e 20 74 68 69 73 20  Table when this 
19aab 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61  routine.** is ca
19aac 6c 6c 65 64 2e 20 20 49 66 20 69 43 6f 6c 75 6d  lled.  If iColum
19aad 6e 3c 30 20 74 68 65 6e 20 63 6f 64 65 20 69 73  n<0 then code is
19aae 20 67 65 6e 65 72 61 74 65 64 20 74 68 61 74 20   generated that 
19aaf 65 78 74 72 61 63 74 73 20 74 68 65 20 72 6f 77  extracts the row
19ab0 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  id..**.** This r
19ab1 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 61 74 74  outine might att
19ab2 65 6d 70 74 20 74 6f 20 72 65 75 73 65 20 74 68  empt to reuse th
19ab3 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63  e value of the c
19ab4 6f 6c 75 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 61  olumn that.** ha
19ab5 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6c  s already been l
19ab6 6f 61 64 65 64 20 69 6e 74 6f 20 61 20 72 65 67  oaded into a reg
19ab7 69 73 74 65 72 2e 20 20 54 68 65 20 76 61 6c 75  ister.  The valu
19ab8 65 20 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a  e will always.**
19ab9 20 62 65 20 75 73 65 64 20 69 66 20 69 74 20 68   be used if it h
19aba 61 73 20 6e 6f 74 20 75 6e 64 65 72 67 6f 6e 65  as not undergone
19abb 20 61 6e 79 20 61 66 66 69 6e 69 74 79 20 63 68   any affinity ch
19abc 61 6e 67 65 73 2e 20 20 42 75 74 20 69 66 0a 2a  anges.  But if.*
19abd 2a 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68  * an affinity ch
19abe 61 6e 67 65 20 68 61 73 20 6f 63 63 75 72 72 65  ange has occurre
19abf 64 2c 20 74 68 65 6e 20 74 68 65 20 63 61 63 68  d, then the cach
19ac0 65 64 20 76 61 6c 75 65 20 77 69 6c 6c 20 6f 6e  ed value will on
19ac1 6c 79 20 62 65 0a 2a 2a 20 75 73 65 64 20 69 66  ly be.** used if
19ac2 20 61 6c 6c 6f 77 41 66 66 43 68 6e 67 20 69 73   allowAffChng is
19ac3 20 74 72 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   true..*/.SQLITE
19ac4 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
19ac5 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43  ite3ExprCodeGetC
19ac6 6f 6c 75 6d 6e 28 0a 20 20 50 61 72 73 65 20 2a  olumn(.  Parse *
19ac7 70 50 61 72 73 65 2c 20 20 20 2f 2a 20 50 61 72  pParse,   /* Par
19ac8 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65  sing and code ge
19ac9 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74  nerating context
19aca 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
19acb 62 2c 20 20 20 20 20 2f 2a 20 44 65 73 63 72 69  b,     /* Descri
19acc 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 74 61 62  ption of the tab
19acd 6c 65 20 77 65 20 61 72 65 20 72 65 61 64 69 6e  le we are readin
19ace 67 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20  g from */.  int 
19acf 69 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 2f 2a 20  iColumn,     /* 
19ad0 49 6e 64 65 78 20 6f 66 20 74 68 65 20 74 61 62  Index of the tab
19ad1 6c 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69  le column */.  i
19ad2 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20  nt iTable,      
19ad3 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 70 6f  /* The cursor po
19ad4 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 74 61  inting to the ta
19ad5 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65  ble */.  int iRe
19ad6 67 2c 20 20 20 20 20 20 20 20 2f 2a 20 53 74 6f  g,        /* Sto
19ad7 72 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20  re results here 
19ad8 2a 2f 0a 20 20 69 6e 74 20 61 6c 6c 6f 77 41 66  */.  int allowAf
19ad9 66 43 68 6e 67 20 2f 2a 20 54 72 75 65 20 69 66  fChng /* True if
19ada 20 70 72 69 6f 72 20 61 66 66 69 6e 69 74 79 20   prior affinity 
19adb 63 68 61 6e 67 65 73 20 61 72 65 20 4f 4b 20 2a  changes are OK *
19adc 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  /.){.  Vdbe *v =
19add 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
19ade 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63    int i;.  struc
19adf 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a  t yColCache *p;.
19ae0 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50  .  for(i=0, p=pP
19ae1 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b  arse->aColCache;
19ae2 20 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43   i<pParse->nColC
19ae3 61 63 68 65 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b  ache; i++, p++){
19ae4 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 54 61 62  .    if( p->iTab
19ae5 6c 65 3d 3d 69 54 61 62 6c 65 20 26 26 20 70 2d  le==iTable && p-
19ae6 3e 69 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 75 6d  >iColumn==iColum
19ae7 6e 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20  n.           && 
19ae8 28 21 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 7c  (!p->affChange |
19ae9 7c 20 61 6c 6c 6f 77 41 66 66 43 68 6e 67 29 20  | allowAffChng) 
19aea 29 7b 0a 23 69 66 20 30 0a 20 20 20 20 20 20 73  ){.#if 0.      s
19aeb 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30  qlite3VdbeAddOp0
19aec 28 76 2c 20 4f 50 5f 4e 6f 6f 70 29 3b 0a 20 20  (v, OP_Noop);.  
19aed 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
19aee 28 76 2c 20 22 4f 50 54 3a 20 74 61 62 25 64 2e  (v, "OPT: tab%d.
19aef 63 6f 6c 25 64 20 2d 3e 20 72 25 64 22 2c 20 69  col%d -> r%d", i
19af0 54 61 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20  Table, iColumn, 
19af1 70 2d 3e 69 52 65 67 29 29 3b 0a 23 65 6e 64 69  p->iReg));.#endi
19af2 66 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70  f.      return p
19af3 2d 3e 69 52 65 67 3b 0a 20 20 20 20 7d 0a 20 20  ->iReg;.    }.  
19af4 7d 20 20 0a 20 20 61 73 73 65 72 74 28 20 76 21  }  .  assert( v!
19af5 3d 30 20 29 3b 0a 20 20 69 66 28 20 69 43 6f 6c  =0 );.  if( iCol
19af6 75 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 69 6e 74  umn<0 ){.    int
19af7 20 6f 70 20 3d 20 28 70 54 61 62 20 26 26 20 49   op = (pTab && I
19af8 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 29 20  sVirtual(pTab)) 
19af9 3f 20 4f 50 5f 56 52 6f 77 69 64 20 3a 20 4f 50  ? OP_VRowid : OP
19afa 5f 52 6f 77 69 64 3b 0a 20 20 20 20 73 71 6c 69  _Rowid;.    sqli
19afb 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
19afc 20 6f 70 2c 20 69 54 61 62 6c 65 2c 20 69 52 65   op, iTable, iRe
19afd 67 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  g);.  }else if( 
19afe 70 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 73  pTab==0 ){.    s
19aff 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
19b00 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69  (v, OP_Column, i
19b01 54 61 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20  Table, iColumn, 
19b02 69 52 65 67 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  iReg);.  }else{.
19b03 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 49 73 56      int op = IsV
19b04 69 72 74 75 61 6c 28 70 54 61 62 29 20 3f 20 4f  irtual(pTab) ? O
19b05 50 5f 56 43 6f 6c 75 6d 6e 20 3a 20 4f 50 5f 43  P_VColumn : OP_C
19b06 6f 6c 75 6d 6e 3b 0a 20 20 20 20 73 71 6c 69 74  olumn;.    sqlit
19b07 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
19b08 6f 70 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c  op, iTable, iCol
19b09 75 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 20 20 20  umn, iReg);.    
19b0a 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66  sqlite3ColumnDef
19b0b 61 75 6c 74 28 76 2c 20 70 54 61 62 2c 20 69 43  ault(v, pTab, iC
19b0c 6f 6c 75 6d 6e 29 3b 0a 23 69 66 6e 64 65 66 20  olumn);.#ifndef 
19b0d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
19b0e 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 69  TING_POINT.    i
19b0f 66 28 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43  f( pTab->aCol[iC
19b10 6f 6c 75 6d 6e 5d 2e 61 66 66 69 6e 69 74 79 3d  olumn].affinity=
19b11 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c  =SQLITE_AFF_REAL
19b12 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
19b13 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
19b14 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 2c 20  P_RealAffinity, 
19b15 69 52 65 67 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  iReg);.    }.#en
19b16 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 70 50  dif.  }.  if( pP
19b17 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c  arse->disableCol
19b18 43 61 63 68 65 3d 3d 30 20 29 7b 0a 20 20 20 20  Cache==0 ){.    
19b19 69 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 6f 6c  i = pParse->iCol
19b1a 43 61 63 68 65 3b 0a 20 20 20 20 70 20 3d 20 26  Cache;.    p = &
19b1b 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68  pParse->aColCach
19b1c 65 5b 69 5d 3b 0a 20 20 20 20 70 2d 3e 69 54 61  e[i];.    p->iTa
19b1d 62 6c 65 20 3d 20 69 54 61 62 6c 65 3b 0a 20 20  ble = iTable;.  
19b1e 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69    p->iColumn = i
19b1f 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 70 2d 3e 69  Column;.    p->i
19b20 52 65 67 20 3d 20 69 52 65 67 3b 0a 20 20 20 20  Reg = iReg;.    
19b21 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d 20 30  p->affChange = 0
19b22 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 69  ;.    i++;.    i
19b23 66 28 20 69 3e 3d 41 72 72 61 79 53 69 7a 65 28  f( i>=ArraySize(
19b24 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68  pParse->aColCach
19b25 65 29 20 29 20 69 20 3d 20 30 3b 0a 20 20 20 20  e) ) i = 0;.    
19b26 69 66 28 20 69 3e 70 50 61 72 73 65 2d 3e 6e 43  if( i>pParse->nC
19b27 6f 6c 43 61 63 68 65 20 29 20 70 50 61 72 73 65  olCache ) pParse
19b28 2d 3e 6e 43 6f 6c 43 61 63 68 65 20 3d 20 69 3b  ->nColCache = i;
19b29 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 69 43 6f  .    pParse->iCo
19b2a 6c 43 61 63 68 65 20 3d 20 69 3b 0a 20 20 7d 0a  lCache = i;.  }.
19b2b 20 20 72 65 74 75 72 6e 20 69 52 65 67 3b 0a 7d    return iReg;.}
19b2c 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6c  ../*.** Clear al
19b2d 6c 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 65  l column cache e
19b2e 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 65  ntries associate
19b2f 64 20 77 69 74 68 20 74 68 65 20 76 64 62 65 0a  d with the vdbe.
19b30 2a 2a 20 63 75 72 73 6f 72 20 77 69 74 68 20 63  ** cursor with c
19b31 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 54 61  ursor number iTa
19b32 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ble..*/.SQLITE_P
19b33 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
19b34 74 65 33 45 78 70 72 43 6c 65 61 72 43 6f 6c 75  te3ExprClearColu
19b35 6d 6e 43 61 63 68 65 28 50 61 72 73 65 20 2a 70  mnCache(Parse *p
19b36 50 61 72 73 65 2c 20 69 6e 74 20 69 54 61 62 6c  Parse, int iTabl
19b37 65 29 7b 0a 20 20 69 66 28 20 69 54 61 62 6c 65  e){.  if( iTable
19b38 3c 30 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65  <0 ){.    pParse
19b39 2d 3e 6e 43 6f 6c 43 61 63 68 65 20 3d 20 30 3b  ->nColCache = 0;
19b3a 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 69 43 6f  .    pParse->iCo
19b3b 6c 43 61 63 68 65 20 3d 20 30 3b 0a 20 20 7d 65  lCache = 0;.  }e
19b3c 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  lse{.    int i;.
19b3d 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
19b3e 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65  Parse->nColCache
19b3f 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
19b40 28 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61  ( pParse->aColCa
19b41 63 68 65 5b 69 5d 2e 69 54 61 62 6c 65 3d 3d 69  che[i].iTable==i
19b42 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20  Table ){.       
19b43 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 70 50   testcase( i==pP
19b44 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 2d  arse->nColCache-
19b45 31 20 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61  1 );.        pPa
19b46 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 69  rse->aColCache[i
19b47 5d 20 3d 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c  ] = pParse->aCol
19b48 43 61 63 68 65 5b 2d 2d 70 50 61 72 73 65 2d 3e  Cache[--pParse->
19b49 6e 43 6f 6c 43 61 63 68 65 5d 3b 0a 20 20 20 20  nColCache];.    
19b4a 20 20 20 20 70 50 61 72 73 65 2d 3e 69 43 6f 6c      pParse->iCol
19b4b 43 61 63 68 65 20 3d 20 70 50 61 72 73 65 2d 3e  Cache = pParse->
19b4c 6e 43 6f 6c 43 61 63 68 65 3b 0a 20 20 20 20 20  nColCache;.     
19b4d 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a   }.    }.  }.}..
19b4e 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 74 68 65  /*.** Record the
19b4f 20 66 61 63 74 20 74 68 61 74 20 61 6e 20 61 66   fact that an af
19b50 66 69 6e 69 74 79 20 63 68 61 6e 67 65 20 68 61  finity change ha
19b51 73 20 6f 63 63 75 72 72 65 64 20 6f 6e 20 69 43  s occurred on iC
19b52 6f 75 6e 74 0a 2a 2a 20 72 65 67 69 73 74 65 72  ount.** register
19b53 73 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20  s starting with 
19b54 69 53 74 61 72 74 2e 0a 2a 2f 0a 53 51 4c 49 54  iStart..*/.SQLIT
19b55 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
19b56 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
19b57 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 50 61  ffinityChange(Pa
19b58 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
19b59 20 69 53 74 61 72 74 2c 20 69 6e 74 20 69 43 6f   iStart, int iCo
19b5a 75 6e 74 29 7b 0a 20 20 69 6e 74 20 69 45 6e 64  unt){.  int iEnd
19b5b 20 3d 20 69 53 74 61 72 74 20 2b 20 69 43 6f 75   = iStart + iCou
19b5c 6e 74 20 2d 20 31 3b 0a 20 20 69 6e 74 20 69 3b  nt - 1;.  int i;
19b5d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50  .  for(i=0; i<pP
19b5e 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b  arse->nColCache;
19b5f 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72   i++){.    int r
19b60 20 3d 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43   = pParse->aColC
19b61 61 63 68 65 5b 69 5d 2e 69 52 65 67 3b 0a 20 20  ache[i].iReg;.  
19b62 20 20 69 66 28 20 72 3e 3d 69 53 74 61 72 74 20    if( r>=iStart 
19b63 26 26 20 72 3c 3d 69 45 6e 64 20 29 7b 0a 20 20  && r<=iEnd ){.  
19b64 20 20 20 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c      pParse->aCol
19b65 43 61 63 68 65 5b 69 5d 2e 61 66 66 43 68 61 6e  Cache[i].affChan
19b66 67 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  ge = 1;.    }.  
19b67 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  }.}../*.** Gener
19b68 61 74 65 20 63 6f 64 65 20 74 6f 20 6d 6f 76 65  ate code to move
19b69 73 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 6f  s content from o
19b6a 6e 65 20 72 65 67 69 73 74 65 72 20 74 6f 20 61  ne register to a
19b6b 6e 6f 74 68 65 72 2e 0a 2a 2a 20 4b 65 65 70 20  nother..** Keep 
19b6c 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65  the column cache
19b6d 20 75 70 2d 74 6f 2d 64 61 74 65 2e 0a 2a 2f 0a   up-to-date..*/.
19b6e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
19b6f 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
19b70 6f 64 65 4d 6f 76 65 28 50 61 72 73 65 20 2a 70  odeMove(Parse *p
19b71 50 61 72 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d  Parse, int iFrom
19b72 2c 20 69 6e 74 20 69 54 6f 29 7b 0a 20 20 69 6e  , int iTo){.  in
19b73 74 20 69 3b 0a 20 20 69 66 28 20 69 46 72 6f 6d  t i;.  if( iFrom
19b74 3d 3d 69 54 6f 20 29 20 72 65 74 75 72 6e 3b 0a  ==iTo ) return;.
19b75 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
19b76 4f 70 32 28 70 50 61 72 73 65 2d 3e 70 56 64 62  Op2(pParse->pVdb
19b77 65 2c 20 4f 50 5f 4d 6f 76 65 2c 20 69 46 72 6f  e, OP_Move, iFro
19b78 6d 2c 20 69 54 6f 29 3b 0a 20 20 66 6f 72 28 69  m, iTo);.  for(i
19b79 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 43  =0; i<pParse->nC
19b7a 6f 6c 43 61 63 68 65 3b 20 69 2b 2b 29 7b 0a 20  olCache; i++){. 
19b7b 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 61     if( pParse->a
19b7c 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 69 52 65 67  ColCache[i].iReg
19b7d 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20  ==iFrom ){.     
19b7e 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63   pParse->aColCac
19b7f 68 65 5b 69 5d 2e 69 52 65 67 20 3d 20 69 54 6f  he[i].iReg = iTo
19b80 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
19b81 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
19b82 20 69 66 20 61 6e 79 20 72 65 67 69 73 74 65 72   if any register
19b83 20 69 6e 20 74 68 65 20 72 61 6e 67 65 20 69 46   in the range iF
19b84 72 6f 6d 2e 2e 69 54 6f 20 28 69 6e 63 6c 75 73  rom..iTo (inclus
19b85 69 76 65 29 0a 2a 2a 20 69 73 20 75 73 65 64 20  ive).** is used 
19b86 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 63  as part of the c
19b87 6f 6c 75 6d 6e 20 63 61 63 68 65 2e 0a 2a 2f 0a  olumn cache..*/.
19b88 73 74 61 74 69 63 20 69 6e 74 20 75 73 65 64 41  static int usedA
19b89 73 43 6f 6c 75 6d 6e 43 61 63 68 65 28 50 61 72  sColumnCache(Par
19b8a 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
19b8b 69 46 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 29 7b  iFrom, int iTo){
19b8c 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28  .  int i;.  for(
19b8d 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e  i=0; i<pParse->n
19b8e 43 6f 6c 43 61 63 68 65 3b 20 69 2b 2b 29 7b 0a  ColCache; i++){.
19b8f 20 20 20 20 69 6e 74 20 72 20 3d 20 70 50 61 72      int r = pPar
19b90 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 69 5d  se->aColCache[i]
19b91 2e 69 52 65 67 3b 0a 20 20 20 20 69 66 28 20 72  .iReg;.    if( r
19b92 3e 3d 69 46 72 6f 6d 20 26 26 20 72 3c 3d 69 54  >=iFrom && r<=iT
19b93 6f 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  o ) return 1;.  
19b94 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
19b95 0a 2f 2a 0a 2a 2a 20 54 68 65 72 65 73 20 69 73  ./*.** Theres is
19b96 20 61 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69   a value in regi
19b97 73 74 65 72 20 69 43 75 72 72 65 6e 74 2e 20 20  ster iCurrent.  
19b98 57 65 20 75 6c 74 69 6d 61 74 65 6c 79 20 77 61  We ultimately wa
19b99 6e 74 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20  nt.** the value 
19b9a 74 6f 20 62 65 20 69 6e 20 72 65 67 69 73 74 65  to be in registe
19b9b 72 20 69 54 61 72 67 65 74 2e 20 20 49 74 20 6d  r iTarget.  It m
19b9c 69 67 68 74 20 62 65 20 74 68 61 74 0a 2a 2a 20  ight be that.** 
19b9d 69 43 75 72 72 65 6e 74 20 61 6e 64 20 69 54 61  iCurrent and iTa
19b9e 72 67 65 74 20 61 72 65 20 74 68 65 20 73 61 6d  rget are the sam
19b9f 65 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a 0a 2a  e register..**.*
19ba0 2a 20 57 65 20 61 72 65 20 67 6f 69 6e 67 20 74  * We are going t
19ba1 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 76 61 6c  o modify the val
19ba2 75 65 2c 20 73 6f 20 77 65 20 6e 65 65 64 20 74  ue, so we need t
19ba3 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74 0a 2a  o make sure it.*
19ba4 2a 20 69 73 20 6e 6f 74 20 61 20 63 61 63 68 65  * is not a cache
19ba5 64 20 72 65 67 69 73 74 65 72 2e 20 20 49 66 20  d register.  If 
19ba6 69 43 75 72 72 65 6e 74 20 69 73 20 61 20 63 61  iCurrent is a ca
19ba7 63 68 65 64 20 72 65 67 69 73 74 65 72 2c 0a 2a  ched register,.*
19ba8 2a 20 74 68 65 6e 20 74 72 79 20 74 6f 20 6d 6f  * then try to mo
19ba9 76 65 20 74 68 65 20 76 61 6c 75 65 20 6f 76 65  ve the value ove
19baa 72 20 74 6f 20 69 54 61 72 67 65 74 2e 20 20 49  r to iTarget.  I
19bab 66 20 69 54 61 72 67 65 74 20 69 73 20 61 0a 2a  f iTarget is a.*
19bac 2a 20 63 61 63 68 65 64 20 72 65 67 69 73 74 65  * cached registe
19bad 72 2c 20 74 68 65 6e 20 63 6c 65 61 72 20 74 68  r, then clear th
19bae 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
19baf 63 61 63 68 65 20 6c 69 6e 65 2e 0a 2a 2a 0a 2a  cache line..**.*
19bb0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 67  * Return the reg
19bb1 69 73 74 65 72 20 74 68 61 74 20 74 68 65 20 76  ister that the v
19bb2 61 6c 75 65 20 65 6e 64 73 20 75 70 20 69 6e 2e  alue ends up in.
19bb3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19bb4 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78  TE int sqlite3Ex
19bb5 70 72 57 72 69 74 61 62 6c 65 52 65 67 69 73 74  prWritableRegist
19bb6 65 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  er(Parse *pParse
19bb7 2c 20 69 6e 74 20 69 43 75 72 72 65 6e 74 2c 20  , int iCurrent, 
19bb8 69 6e 74 20 69 54 61 72 67 65 74 29 7b 0a 20 20  int iTarget){.  
19bb9 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28  int i;.  assert(
19bba 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 21 3d   pParse->pVdbe!=
19bbb 30 20 29 3b 0a 20 20 69 66 28 20 21 75 73 65 64  0 );.  if( !used
19bbc 41 73 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50  AsColumnCache(pP
19bbd 61 72 73 65 2c 20 69 43 75 72 72 65 6e 74 2c 20  arse, iCurrent, 
19bbe 69 43 75 72 72 65 6e 74 29 20 29 7b 0a 20 20 20  iCurrent) ){.   
19bbf 20 72 65 74 75 72 6e 20 69 43 75 72 72 65 6e 74   return iCurrent
19bc0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 43 75 72  ;.  }.  if( iCur
19bc1 72 65 6e 74 21 3d 69 54 61 72 67 65 74 20 29 7b  rent!=iTarget ){
19bc2 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
19bc3 41 64 64 4f 70 32 28 70 50 61 72 73 65 2d 3e 70  AddOp2(pParse->p
19bc4 56 64 62 65 2c 20 4f 50 5f 53 43 6f 70 79 2c 20  Vdbe, OP_SCopy, 
19bc5 69 43 75 72 72 65 6e 74 2c 20 69 54 61 72 67 65  iCurrent, iTarge
19bc6 74 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  t);.  }.  for(i=
19bc7 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f  0; i<pParse->nCo
19bc8 6c 43 61 63 68 65 3b 20 69 2b 2b 29 7b 0a 20 20  lCache; i++){.  
19bc9 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 61 43    if( pParse->aC
19bca 6f 6c 43 61 63 68 65 5b 69 5d 2e 69 52 65 67 3d  olCache[i].iReg=
19bcb 3d 69 54 61 72 67 65 74 20 29 7b 0a 20 20 20 20  =iTarget ){.    
19bcc 20 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61    pParse->aColCa
19bcd 63 68 65 5b 69 5d 20 3d 20 70 50 61 72 73 65 2d  che[i] = pParse-
19bce 3e 61 43 6f 6c 43 61 63 68 65 5b 2d 2d 70 50 61  >aColCache[--pPa
19bcf 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 5d 3b  rse->nColCache];
19bd0 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 69  .      pParse->i
19bd1 43 6f 6c 43 61 63 68 65 20 3d 20 70 50 61 72 73  ColCache = pPars
19bd2 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b 0a 20 20  e->nColCache;.  
19bd3 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
19bd4 20 69 54 61 72 67 65 74 3b 0a 7d 0a 0a 2f 2a 0a   iTarget;.}../*.
19bd5 2a 2a 20 49 66 20 74 68 65 20 6c 61 73 74 20 69  ** If the last i
19bd6 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f 64 65 64  nstruction coded
19bd7 20 69 73 20 61 6e 20 65 70 68 65 6d 65 72 61 6c   is an ephemeral
19bd8 20 63 6f 70 79 20 6f 66 20 61 6e 79 20 6f 66 0a   copy of any of.
19bd9 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65 72 73  ** the registers
19bda 20 69 6e 20 74 68 65 20 6e 52 65 67 20 72 65 67   in the nReg reg
19bdb 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69 6e 67  isters beginning
19bdc 20 77 69 74 68 20 69 52 65 67 2c 20 74 68 65 6e   with iReg, then
19bdd 0a 2a 2a 20 63 6f 6e 76 65 72 74 20 74 68 65 20  .** convert the 
19bde 6c 61 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  last instruction
19bdf 20 66 72 6f 6d 20 4f 50 5f 53 43 6f 70 79 20 74   from OP_SCopy t
19be0 6f 20 4f 50 5f 43 6f 70 79 2e 0a 2a 2f 0a 53 51  o OP_Copy..*/.SQ
19be1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
19be2 64 20 73 71 6c 69 74 65 33 45 78 70 72 48 61 72  d sqlite3ExprHar
19be3 64 43 6f 70 79 28 50 61 72 73 65 20 2a 70 50 61  dCopy(Parse *pPa
19be4 72 73 65 2c 20 69 6e 74 20 69 52 65 67 2c 20 69  rse, int iReg, i
19be5 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20  nt nReg){.  int 
19be6 61 64 64 72 3b 0a 20 20 56 64 62 65 4f 70 20 2a  addr;.  VdbeOp *
19be7 70 4f 70 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a  pOp;.  Vdbe *v;.
19be8 0a 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  .  v = pParse->p
19be9 56 64 62 65 3b 0a 20 20 61 64 64 72 20 3d 20 73  Vdbe;.  addr = s
19bea 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
19beb 74 41 64 64 72 28 76 29 3b 0a 20 20 70 4f 70 20  tAddr(v);.  pOp 
19bec 3d 20 73 71 6c 69 74 65 33 56 64 62 65 47 65 74  = sqlite3VdbeGet
19bed 4f 70 28 76 2c 20 61 64 64 72 2d 31 29 3b 0a 20  Op(v, addr-1);. 
19bee 20 61 73 73 65 72 74 28 20 70 4f 70 20 7c 7c 20   assert( pOp || 
19bef 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c  pParse->db->mall
19bf0 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66  ocFailed );.  if
19bf1 28 20 70 4f 70 20 26 26 20 70 4f 70 2d 3e 6f 70  ( pOp && pOp->op
19bf2 63 6f 64 65 3d 3d 4f 50 5f 53 43 6f 70 79 20 26  code==OP_SCopy &
19bf3 26 20 70 4f 70 2d 3e 70 31 3e 3d 69 52 65 67 20  & pOp->p1>=iReg 
19bf4 26 26 20 70 4f 70 2d 3e 70 31 3c 69 52 65 67 2b  && pOp->p1<iReg+
19bf5 6e 52 65 67 20 29 7b 0a 20 20 20 20 70 4f 70 2d  nReg ){.    pOp-
19bf6 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 43 6f 70  >opcode = OP_Cop
19bf7 79 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  y;.  }.}../*.** 
19bf8 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e  Generate code in
19bf9 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 56  to the current V
19bfa 64 62 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20  dbe to evaluate 
19bfb 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 65 78 70  the given.** exp
19bfc 72 65 73 73 69 6f 6e 2e 20 20 41 74 74 65 6d 70  ression.  Attemp
19bfd 74 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 72  t to store the r
19bfe 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69 73 74  esults in regist
19bff 65 72 20 22 74 61 72 67 65 74 22 2e 0a 2a 2a 20  er "target"..** 
19c00 52 65 74 75 72 6e 20 74 68 65 20 72 65 67 69 73  Return the regis
19c01 74 65 72 20 77 68 65 72 65 20 72 65 73 75 6c 74  ter where result
19c02 73 20 61 72 65 20 73 74 6f 72 65 64 2e 0a 2a 2a  s are stored..**
19c03 0a 2a 2a 20 57 69 74 68 20 74 68 69 73 20 72 6f  .** With this ro
19c04 75 74 69 6e 65 2c 20 74 68 65 72 65 20 69 73 20  utine, there is 
19c05 6e 6f 20 67 75 61 72 61 6e 74 65 65 64 20 74 68  no guaranteed th
19c06 61 74 20 72 65 73 75 6c 74 73 20 77 69 6c 6c 0a  at results will.
19c07 2a 2a 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20  ** be stored in 
19c08 74 61 72 67 65 74 2e 20 20 54 68 65 20 72 65 73  target.  The res
19c09 75 6c 74 20 6d 69 67 68 74 20 62 65 20 73 74 6f  ult might be sto
19c0a 72 65 64 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65  red in some othe
19c0b 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 69 66  r.** register if
19c0c 20 69 74 20 69 73 20 63 6f 6e 76 65 6e 69 65 6e   it is convenien
19c0d 74 20 74 6f 20 64 6f 20 73 6f 2e 20 20 54 68 65  t to do so.  The
19c0e 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f   calling functio
19c0f 6e 0a 2a 2a 20 6d 75 73 74 20 63 68 65 63 6b 20  n.** must check 
19c10 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20  the return code 
19c11 61 6e 64 20 6d 6f 76 65 20 74 68 65 20 72 65 73  and move the res
19c12 75 6c 74 73 20 74 6f 20 74 68 65 20 64 65 73 69  ults to the desi
19c13 72 65 64 0a 2a 2a 20 72 65 67 69 73 74 65 72 2e  red.** register.
19c14 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19c15 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78  TE int sqlite3Ex
19c16 70 72 43 6f 64 65 54 61 72 67 65 74 28 50 61 72  prCodeTarget(Par
19c17 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
19c18 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 74 61 72   *pExpr, int tar
19c19 67 65 74 29 7b 0a 20 20 56 64 62 65 20 2a 76 20  get){.  Vdbe *v 
19c1a 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
19c1b 20 20 2f 2a 20 54 68 65 20 56 4d 20 75 6e 64 65    /* The VM unde
19c1c 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a  r construction *
19c1d 2f 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20  /.  int op;     
19c1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19c1f 20 54 68 65 20 6f 70 63 6f 64 65 20 62 65 69 6e   The opcode bein
19c20 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74  g coded */.  int
19c21 20 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b   inReg = target;
19c22 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
19c23 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69  s stored in regi
19c24 73 74 65 72 20 69 6e 52 65 67 20 2a 2f 0a 20 20  ster inReg */.  
19c25 69 6e 74 20 72 65 67 46 72 65 65 31 20 3d 20 30  int regFree1 = 0
19c26 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20  ;         /* If 
19c27 6e 6f 6e 2d 7a 65 72 6f 20 66 72 65 65 20 74 68  non-zero free th
19c28 69 73 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67  is temporary reg
19c29 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72  ister */.  int r
19c2a 65 67 46 72 65 65 32 20 3d 20 30 3b 20 20 20 20  egFree2 = 0;    
19c2b 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a       /* If non-z
19c2c 65 72 6f 20 66 72 65 65 20 74 68 69 73 20 74 65  ero free this te
19c2d 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72  mporary register
19c2e 20 2a 2f 0a 20 20 69 6e 74 20 72 31 2c 20 72 32   */.  int r1, r2
19c2f 2c 20 72 33 2c 20 72 34 3b 20 20 20 20 20 20 20  , r3, r4;       
19c30 2f 2a 20 56 61 72 69 6f 75 73 20 72 65 67 69 73  /* Various regis
19c31 74 65 72 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 0a  ter numbers */..
19c32 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 7c    assert( v!=0 |
19c33 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61  | pParse->db->ma
19c34 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
19c35 61 73 73 65 72 74 28 20 74 61 72 67 65 74 3e 30  assert( target>0
19c36 20 26 26 20 74 61 72 67 65 74 3c 3d 70 50 61 72   && target<=pPar
19c37 73 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 69 66  se->nMem );.  if
19c38 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( v==0 ) return 
19c39 30 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72 3d  0;..  if( pExpr=
19c3a 3d 30 20 29 7b 0a 20 20 20 20 6f 70 20 3d 20 54  =0 ){.    op = T
19c3b 4b 5f 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b  K_NULL;.  }else{
19c3c 0a 20 20 20 20 6f 70 20 3d 20 70 45 78 70 72 2d  .    op = pExpr-
19c3d 3e 6f 70 3b 0a 20 20 7d 0a 20 20 73 77 69 74 63  >op;.  }.  switc
19c3e 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73  h( op ){.    cas
19c3f 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a  e TK_AGG_COLUMN:
19c40 20 7b 0a 20 20 20 20 20 20 41 67 67 49 6e 66 6f   {.      AggInfo
19c41 20 2a 70 41 67 67 49 6e 66 6f 20 3d 20 70 45 78   *pAggInfo = pEx
19c42 70 72 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 20 20  pr->pAggInfo;.  
19c43 20 20 20 20 73 74 72 75 63 74 20 41 67 67 49 6e      struct AggIn
19c44 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 20 3d 20 26  fo_col *pCol = &
19c45 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 70  pAggInfo->aCol[p
19c46 45 78 70 72 2d 3e 69 41 67 67 5d 3b 0a 20 20 20  Expr->iAgg];.   
19c47 20 20 20 69 66 28 20 21 70 41 67 67 49 6e 66 6f     if( !pAggInfo
19c48 2d 3e 64 69 72 65 63 74 4d 6f 64 65 20 29 7b 0a  ->directMode ){.
19c49 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
19c4a 70 43 6f 6c 2d 3e 69 4d 65 6d 3e 30 20 29 3b 0a  pCol->iMem>0 );.
19c4b 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20          inReg = 
19c4c 70 43 6f 6c 2d 3e 69 4d 65 6d 3b 0a 20 20 20 20  pCol->iMem;.    
19c4d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
19c4e 20 7d 65 6c 73 65 20 69 66 28 20 70 41 67 67 49   }else if( pAggI
19c4f 6e 66 6f 2d 3e 75 73 65 53 6f 72 74 69 6e 67 49  nfo->useSortingI
19c50 64 78 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  dx ){.        sq
19c51 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
19c52 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 70 41  v, OP_Column, pA
19c53 67 67 49 6e 66 6f 2d 3e 73 6f 72 74 69 6e 67 49  ggInfo->sortingI
19c54 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  dx,.            
19c55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19c56 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43    pCol->iSorterC
19c57 6f 6c 75 6d 6e 2c 20 74 61 72 67 65 74 29 3b 0a  olumn, target);.
19c58 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
19c59 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20       }.      /* 
19c5a 4f 74 68 65 72 77 69 73 65 2c 20 66 61 6c 6c 20  Otherwise, fall 
19c5b 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20 54 4b  thru into the TK
19c5c 5f 43 4f 4c 55 4d 4e 20 63 61 73 65 20 2a 2f 0a  _COLUMN case */.
19c5d 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
19c5e 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20  K_COLUMN: {.    
19c5f 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 54 61    if( pExpr->iTa
19c60 62 6c 65 3c 30 20 29 7b 0a 20 20 20 20 20 20 20  ble<0 ){.       
19c61 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 68 61   /* This only ha
19c62 70 70 65 6e 73 20 77 68 65 6e 20 63 6f 64 69 6e  ppens when codin
19c63 67 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69  g check constrai
19c64 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 61  nts */.        a
19c65 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 63  ssert( pParse->c
19c66 6b 42 61 73 65 3e 30 20 29 3b 0a 20 20 20 20 20  kBase>0 );.     
19c67 20 20 20 69 6e 52 65 67 20 3d 20 70 45 78 70 72     inReg = pExpr
19c68 2d 3e 69 43 6f 6c 75 6d 6e 20 2b 20 70 50 61 72  ->iColumn + pPar
19c69 73 65 2d 3e 63 6b 42 61 73 65 3b 0a 20 20 20 20  se->ckBase;.    
19c6a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
19c6b 20 74 65 73 74 63 61 73 65 28 20 28 70 45 78 70   testcase( (pExp
19c6c 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 41 6e  r->flags & EP_An
19c6d 79 41 66 66 29 21 3d 30 20 29 3b 0a 20 20 20 20  yAff)!=0 );.    
19c6e 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69      inReg = sqli
19c6f 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f  te3ExprCodeGetCo
19c70 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 70 45 78  lumn(pParse, pEx
19c71 70 72 2d 3e 70 54 61 62 2c 0a 20 20 20 20 20 20  pr->pTab,.      
19c72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19c73 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
19c74 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70 45 78 70 72  ->iColumn, pExpr
19c75 2d 3e 69 54 61 62 6c 65 2c 20 74 61 72 67 65 74  ->iTable, target
19c76 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
19c77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19c78 20 20 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20     pExpr->flags 
19c79 26 20 45 50 5f 41 6e 79 41 66 66 29 3b 0a 20 20  & EP_AnyAff);.  
19c7a 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
19c7b 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
19c7c 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a  e TK_INTEGER: {.
19c7d 20 20 20 20 20 20 63 6f 64 65 49 6e 74 65 67 65        codeIntege
19c7e 72 28 76 2c 20 28 63 68 61 72 2a 29 70 45 78 70  r(v, (char*)pExp
19c7f 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70  r->token.z, pExp
19c80 72 2d 3e 74 6f 6b 65 6e 2e 6e 2c 20 30 2c 20 74  r->token.n, 0, t
19c81 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72  arget);.      br
19c82 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
19c83 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 20 7b 0a  ase TK_FLOAT: {.
19c84 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c 28 76        codeReal(v
19c85 2c 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e  , (char*)pExpr->
19c86 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e  token.z, pExpr->
19c87 74 6f 6b 65 6e 2e 6e 2c 20 30 2c 20 74 61 72 67  token.n, 0, targ
19c88 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  et);.      break
19c89 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
19c8a 20 54 4b 5f 53 54 52 49 4e 47 3a 20 7b 0a 20 20   TK_STRING: {.  
19c8b 20 20 20 20 73 71 6c 69 74 65 33 44 65 71 75 6f      sqlite3Dequo
19c8c 74 65 45 78 70 72 28 70 50 61 72 73 65 2d 3e 64  teExpr(pParse->d
19c8d 62 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20  b, pExpr);.     
19c8e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
19c8f 70 34 28 76 2c 4f 50 5f 53 74 72 69 6e 67 38 2c  p4(v,OP_String8,
19c90 20 30 2c 20 74 61 72 67 65 74 2c 20 30 2c 0a 20   0, target, 0,. 
19c91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19c92 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 45         (char*)pE
19c93 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45  xpr->token.z, pE
19c94 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b 0a 20  xpr->token.n);. 
19c95 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
19c96 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 55  }.    case TK_NU
19c97 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  LL: {.      sqli
19c98 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
19c99 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 74 61 72   OP_Null, 0, tar
19c9a 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61  get);.      brea
19c9b 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66  k;.    }.#ifndef
19c9c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f   SQLITE_OMIT_BLO
19c9d 42 5f 4c 49 54 45 52 41 4c 0a 20 20 20 20 63 61  B_LITERAL.    ca
19c9e 73 65 20 54 4b 5f 42 4c 4f 42 3a 20 7b 0a 20 20  se TK_BLOB: {.  
19c9f 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20      int n;.     
19ca0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a   const char *z;.
19ca1 20 20 20 20 20 20 63 68 61 72 20 2a 7a 42 6c 6f        char *zBlo
19ca2 62 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  b;.      assert(
19ca3 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3e   pExpr->token.n>
19ca4 3d 33 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  =3 );.      asse
19ca5 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  rt( pExpr->token
19ca6 2e 7a 5b 30 5d 3d 3d 27 78 27 20 7c 7c 20 70 45  .z[0]=='x' || pE
19ca7 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 3d  xpr->token.z[0]=
19ca8 3d 27 58 27 20 29 3b 0a 20 20 20 20 20 20 61 73  ='X' );.      as
19ca9 73 65 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b  sert( pExpr->tok
19caa 65 6e 2e 7a 5b 31 5d 3d 3d 27 5c 27 27 20 29 3b  en.z[1]=='\'' );
19cab 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
19cac 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 70 45  Expr->token.z[pE
19cad 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2d 31 5d 3d  xpr->token.n-1]=
19cae 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 20 20 6e  ='\'' );.      n
19caf 20 3d 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e   = pExpr->token.
19cb0 6e 20 2d 20 33 3b 0a 20 20 20 20 20 20 7a 20 3d  n - 3;.      z =
19cb1 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74   (char*)pExpr->t
19cb2 6f 6b 65 6e 2e 7a 20 2b 20 32 3b 0a 20 20 20 20  oken.z + 2;.    
19cb3 20 20 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65    zBlob = sqlite
19cb4 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74  3HexToBlob(sqlit
19cb5 65 33 56 64 62 65 44 62 28 76 29 2c 20 7a 2c 20  e3VdbeDb(v), z, 
19cb6 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  n);.      sqlite
19cb7 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
19cb8 50 5f 42 6c 6f 62 2c 20 6e 2f 32 2c 20 74 61 72  P_Blob, n/2, tar
19cb9 67 65 74 2c 20 30 2c 20 7a 42 6c 6f 62 2c 20 50  get, 0, zBlob, P
19cba 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20  4_DYNAMIC);.    
19cbb 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
19cbc 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 54  endif.    case T
19cbd 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b 0a 20 20  K_VARIABLE: {.  
19cbe 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
19cbf 64 64 4f 70 32 28 76 2c 20 4f 50 5f 56 61 72 69  ddOp2(v, OP_Vari
19cc0 61 62 6c 65 2c 20 70 45 78 70 72 2d 3e 69 54 61  able, pExpr->iTa
19cc1 62 6c 65 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  ble, target);.  
19cc2 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 74      if( pExpr->t
19cc3 6f 6b 65 6e 2e 6e 3e 31 20 29 7b 0a 20 20 20 20  oken.n>1 ){.    
19cc4 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
19cc5 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28  hangeP4(v, -1, (
19cc6 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b  char*)pExpr->tok
19cc7 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b  en.z, pExpr->tok
19cc8 65 6e 2e 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20  en.n);.      }. 
19cc9 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
19cca 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45  }.    case TK_RE
19ccb 47 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20 20  GISTER: {.      
19ccc 69 6e 52 65 67 20 3d 20 70 45 78 70 72 2d 3e 69  inReg = pExpr->i
19ccd 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 62 72 65  Table;.      bre
19cce 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65  ak;.    }.#ifnde
19ccf 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41  f SQLITE_OMIT_CA
19cd0 53 54 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43  ST.    case TK_C
19cd1 41 53 54 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  AST: {.      /* 
19cd2 45 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74  Expressions of t
19cd3 68 65 20 66 6f 72 6d 3a 20 20 20 43 41 53 54 28  he form:   CAST(
19cd4 70 4c 65 66 74 20 41 53 20 74 6f 6b 65 6e 29 20  pLeft AS token) 
19cd5 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 66 66  */.      int aff
19cd6 2c 20 74 6f 5f 6f 70 3b 0a 20 20 20 20 20 20 69  , to_op;.      i
19cd7 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78  nReg = sqlite3Ex
19cd8 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61  prCodeTarget(pPa
19cd9 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
19cda 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  t, target);.    
19cdb 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 41    aff = sqlite3A
19cdc 66 66 69 6e 69 74 79 54 79 70 65 28 26 70 45 78  ffinityType(&pEx
19cdd 70 72 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 20 20  pr->token);.    
19cde 20 20 74 6f 5f 6f 70 20 3d 20 61 66 66 20 2d 20    to_op = aff - 
19cdf 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20  SQLITE_AFF_TEXT 
19ce0 2b 20 4f 50 5f 54 6f 54 65 78 74 3b 0a 20 20 20  + OP_ToText;.   
19ce1 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70     assert( to_op
19ce2 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 20 20 20 7c  ==OP_ToText    |
19ce3 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46  | aff!=SQLITE_AF
19ce4 46 5f 54 45 58 54 20 20 20 20 29 3b 0a 20 20 20  F_TEXT    );.   
19ce5 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70     assert( to_op
19ce6 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 20 20 20 20 7c  ==OP_ToBlob    |
19ce7 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46  | aff!=SQLITE_AF
19ce8 46 5f 4e 4f 4e 45 20 20 20 20 29 3b 0a 20 20 20  F_NONE    );.   
19ce9 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70     assert( to_op
19cea 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 20 7c  ==OP_ToNumeric |
19ceb 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46  | aff!=SQLITE_AF
19cec 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20 20 20  F_NUMERIC );.   
19ced 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70     assert( to_op
19cee 3d 3d 4f 50 5f 54 6f 49 6e 74 20 20 20 20 20 7c  ==OP_ToInt     |
19cef 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46  | aff!=SQLITE_AF
19cf0 46 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20 20 20  F_INTEGER );.   
19cf1 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70     assert( to_op
19cf2 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20 20 20 20 7c  ==OP_ToReal    |
19cf3 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46  | aff!=SQLITE_AF
19cf4 46 5f 52 45 41 4c 20 20 20 20 29 3b 0a 20 20 20  F_REAL    );.   
19cf5 20 20 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f     testcase( to_
19cf6 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 29 3b  op==OP_ToText );
19cf7 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
19cf8 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 42 6c 6f   to_op==OP_ToBlo
19cf9 62 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  b );.      testc
19cfa 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54  ase( to_op==OP_T
19cfb 6f 4e 75 6d 65 72 69 63 20 29 3b 0a 20 20 20 20  oNumeric );.    
19cfc 20 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f    testcase( to_o
19cfd 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 29 3b 0a 20  p==OP_ToInt );. 
19cfe 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74       testcase( t
19cff 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20  o_op==OP_ToReal 
19d00 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
19d01 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 74 6f  VdbeAddOp1(v, to
19d02 5f 6f 70 2c 20 69 6e 52 65 67 29 3b 0a 20 20 20  _op, inReg);.   
19d03 20 20 20 74 65 73 74 63 61 73 65 28 20 75 73 65     testcase( use
19d04 64 41 73 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70  dAsColumnCache(p
19d05 50 61 72 73 65 2c 20 69 6e 52 65 67 2c 20 69 6e  Parse, inReg, in
19d06 52 65 67 29 20 29 3b 0a 20 20 20 20 20 20 73 71  Reg) );.      sq
19d07 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66  lite3ExprCacheAf
19d08 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61  finityChange(pPa
19d09 72 73 65 2c 20 69 6e 52 65 67 2c 20 31 29 3b 0a  rse, inReg, 1);.
19d0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
19d0b 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c   }.#endif /* SQL
19d0c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f  ITE_OMIT_CAST */
19d0d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 54 3a  .    case TK_LT:
19d0e 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 45 3a  .    case TK_LE:
19d0f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 54 3a  .    case TK_GT:
19d10 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 45 3a  .    case TK_GE:
19d11 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 45 3a  .    case TK_NE:
19d12 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 51 3a  .    case TK_EQ:
19d13 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
19d14 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c 74 20 29 3b   TK_LT==OP_Lt );
19d15 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
19d16 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20 29 3b 0a 20  K_LE==OP_Le );. 
19d17 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
19d18 47 54 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20 20 20  GT==OP_Gt );.   
19d19 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47 45     assert( TK_GE
19d1a 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 20 20 20  ==OP_Ge );.     
19d1b 20 61 73 73 65 72 74 28 20 54 4b 5f 45 51 3d 3d   assert( TK_EQ==
19d1c 4f 50 5f 45 71 20 29 3b 0a 20 20 20 20 20 20 61  OP_Eq );.      a
19d1d 73 73 65 72 74 28 20 54 4b 5f 4e 45 3d 3d 4f 50  ssert( TK_NE==OP
19d1e 5f 4e 65 20 29 3b 0a 20 20 20 20 20 20 74 65 73  _Ne );.      tes
19d1f 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 54  tcase( op==TK_LT
19d20 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
19d21 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 45 20 29 3b  se( op==TK_LE );
19d22 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
19d23 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 3b 0a 20 20   op==TK_GT );.  
19d24 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
19d25 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20 20 20 20 20  ==TK_GE );.     
19d26 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
19d27 4b 5f 45 51 20 29 3b 0a 20 20 20 20 20 20 74 65  K_EQ );.      te
19d28 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e  stcase( op==TK_N
19d29 45 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43  E );.      codeC
19d2a 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28 70  ompareOperands(p
19d2b 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
19d2c 65 66 74 2c 20 26 72 31 2c 20 26 72 65 67 46 72  eft, &r1, &regFr
19d2d 65 65 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ee1,.           
19d2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19d2f 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 52         pExpr->pR
19d30 69 67 68 74 2c 20 26 72 32 2c 20 26 72 65 67 46  ight, &r2, &regF
19d31 72 65 65 32 29 3b 0a 20 20 20 20 20 20 63 6f 64  ree2);.      cod
19d32 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c  eCompare(pParse,
19d33 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70   pExpr->pLeft, p
19d34 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70  Expr->pRight, op
19d35 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
19d36 20 20 20 20 72 31 2c 20 72 32 2c 20 69 6e 52 65      r1, r2, inRe
19d37 67 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50  g, SQLITE_STOREP
19d38 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  2);.      testca
19d39 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20  se( regFree1==0 
19d3a 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
19d3b 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29  e( regFree2==0 )
19d3c 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
19d3d 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
19d3e 5f 41 4e 44 3a 0a 20 20 20 20 63 61 73 65 20 54  _AND:.    case T
19d3f 4b 5f 4f 52 3a 0a 20 20 20 20 63 61 73 65 20 54  K_OR:.    case T
19d40 4b 5f 50 4c 55 53 3a 0a 20 20 20 20 63 61 73 65  K_PLUS:.    case
19d41 20 54 4b 5f 53 54 41 52 3a 0a 20 20 20 20 63 61   TK_STAR:.    ca
19d42 73 65 20 54 4b 5f 4d 49 4e 55 53 3a 0a 20 20 20  se TK_MINUS:.   
19d43 20 63 61 73 65 20 54 4b 5f 52 45 4d 3a 0a 20 20   case TK_REM:.  
19d44 20 20 63 61 73 65 20 54 4b 5f 42 49 54 41 4e 44    case TK_BITAND
19d45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 49  :.    case TK_BI
19d46 54 4f 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  TOR:.    case TK
19d47 5f 53 4c 41 53 48 3a 0a 20 20 20 20 63 61 73 65  _SLASH:.    case
19d48 20 54 4b 5f 4c 53 48 49 46 54 3a 0a 20 20 20 20   TK_LSHIFT:.    
19d49 63 61 73 65 20 54 4b 5f 52 53 48 49 46 54 3a 20  case TK_RSHIFT: 
19d4a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e  .    case TK_CON
19d4b 43 41 54 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  CAT: {.      ass
19d4c 65 72 74 28 20 54 4b 5f 41 4e 44 3d 3d 4f 50 5f  ert( TK_AND==OP_
19d4d 41 6e 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73  And );.      ass
19d4e 65 72 74 28 20 54 4b 5f 4f 52 3d 3d 4f 50 5f 4f  ert( TK_OR==OP_O
19d4f 72 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  r );.      asser
19d50 74 28 20 54 4b 5f 50 4c 55 53 3d 3d 4f 50 5f 41  t( TK_PLUS==OP_A
19d51 64 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  dd );.      asse
19d52 72 74 28 20 54 4b 5f 4d 49 4e 55 53 3d 3d 4f 50  rt( TK_MINUS==OP
19d53 5f 53 75 62 74 72 61 63 74 20 29 3b 0a 20 20 20  _Subtract );.   
19d54 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 52 45     assert( TK_RE
19d55 4d 3d 3d 4f 50 5f 52 65 6d 61 69 6e 64 65 72 20  M==OP_Remainder 
19d56 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
19d57 20 54 4b 5f 42 49 54 41 4e 44 3d 3d 4f 50 5f 42   TK_BITAND==OP_B
19d58 69 74 41 6e 64 20 29 3b 0a 20 20 20 20 20 20 61  itAnd );.      a
19d59 73 73 65 72 74 28 20 54 4b 5f 42 49 54 4f 52 3d  ssert( TK_BITOR=
19d5a 3d 4f 50 5f 42 69 74 4f 72 20 29 3b 0a 20 20 20  =OP_BitOr );.   
19d5b 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 53 4c     assert( TK_SL
19d5c 41 53 48 3d 3d 4f 50 5f 44 69 76 69 64 65 20 29  ASH==OP_Divide )
19d5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
19d5e 54 4b 5f 4c 53 48 49 46 54 3d 3d 4f 50 5f 53 68  TK_LSHIFT==OP_Sh
19d5f 69 66 74 4c 65 66 74 20 29 3b 0a 20 20 20 20 20  iftLeft );.     
19d60 20 61 73 73 65 72 74 28 20 54 4b 5f 52 53 48 49   assert( TK_RSHI
19d61 46 54 3d 3d 4f 50 5f 53 68 69 66 74 52 69 67 68  FT==OP_ShiftRigh
19d62 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  t );.      asser
19d63 74 28 20 54 4b 5f 43 4f 4e 43 41 54 3d 3d 4f 50  t( TK_CONCAT==OP
19d64 5f 43 6f 6e 63 61 74 20 29 3b 0a 20 20 20 20 20  _Concat );.     
19d65 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
19d66 4b 5f 41 4e 44 20 29 3b 0a 20 20 20 20 20 20 74  K_AND );.      t
19d67 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
19d68 4f 52 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  OR );.      test
19d69 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 50 4c 55  case( op==TK_PLU
19d6a 53 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  S );.      testc
19d6b 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4d 49 4e 55  ase( op==TK_MINU
19d6c 53 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  S );.      testc
19d6d 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 52 45 4d 20  ase( op==TK_REM 
19d6e 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
19d6f 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 41 4e 44  e( op==TK_BITAND
19d70 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
19d71 73 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 4f 52  se( op==TK_BITOR
19d72 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
19d73 73 65 28 20 6f 70 3d 3d 54 4b 5f 53 4c 41 53 48  se( op==TK_SLASH
19d74 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
19d75 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 53 48 49 46  se( op==TK_LSHIF
19d76 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  T );.      testc
19d77 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 52 53 48 49  ase( op==TK_RSHI
19d78 46 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  FT );.      test
19d79 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 43 4f 4e  case( op==TK_CON
19d7a 43 41 54 20 29 3b 0a 20 20 20 20 20 20 72 31 20  CAT );.      r1 
19d7b 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
19d7c 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45  eTemp(pParse, pE
19d7d 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67  xpr->pLeft, &reg
19d7e 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 72 32  Free1);.      r2
19d7f 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
19d80 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70  deTemp(pParse, p
19d81 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72  Expr->pRight, &r
19d82 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20  egFree2);.      
19d83 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
19d84 33 28 76 2c 20 6f 70 2c 20 72 32 2c 20 72 31 2c  3(v, op, r2, r1,
19d85 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20   target);.      
19d86 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
19d87 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74  e1==0 );.      t
19d88 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
19d89 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72  2==0 );.      br
19d8a 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
19d8b 61 73 65 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b  ase TK_UMINUS: {
19d8c 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65  .      Expr *pLe
19d8d 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66  ft = pExpr->pLef
19d8e 74 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  t;.      assert(
19d8f 20 70 4c 65 66 74 20 29 3b 0a 20 20 20 20 20 20   pLeft );.      
19d90 69 66 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54  if( pLeft->op==T
19d91 4b 5f 46 4c 4f 41 54 20 7c 7c 20 70 4c 65 66 74  K_FLOAT || pLeft
19d92 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52  ->op==TK_INTEGER
19d93 20 29 7b 0a 20 20 20 20 20 20 20 20 54 6f 6b 65   ){.        Toke
19d94 6e 20 2a 70 20 3d 20 26 70 4c 65 66 74 2d 3e 74  n *p = &pLeft->t
19d95 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 69 66  oken;.        if
19d96 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f  ( pLeft->op==TK_
19d97 46 4c 4f 41 54 20 29 7b 0a 20 20 20 20 20 20 20  FLOAT ){.       
19d98 20 20 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 28     codeReal(v, (
19d99 63 68 61 72 2a 29 70 2d 3e 7a 2c 20 70 2d 3e 6e  char*)p->z, p->n
19d9a 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  , 1, target);.  
19d9b 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
19d9c 20 20 20 20 20 20 20 63 6f 64 65 49 6e 74 65 67         codeInteg
19d9d 65 72 28 76 2c 20 28 63 68 61 72 2a 29 70 2d 3e  er(v, (char*)p->
19d9e 7a 2c 20 70 2d 3e 6e 2c 20 31 2c 20 74 61 72 67  z, p->n, 1, targ
19d9f 65 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  et);.        }. 
19da0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
19da1 20 20 20 20 72 65 67 46 72 65 65 31 20 3d 20 72      regFree1 = r
19da2 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  1 = sqlite3GetTe
19da3 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
19da4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
19da5 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
19da6 6e 74 65 67 65 72 2c 20 30 2c 20 72 31 29 3b 0a  nteger, 0, r1);.
19da7 20 20 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c          r2 = sql
19da8 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70  ite3ExprCodeTemp
19da9 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
19daa 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 32  pLeft, &regFree2
19dab 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
19dac 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
19dad 4f 50 5f 53 75 62 74 72 61 63 74 2c 20 72 32 2c  OP_Subtract, r2,
19dae 20 72 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20   r1, target);.  
19daf 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
19db0 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20  regFree2==0 );. 
19db1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 6e 52       }.      inR
19db2 65 67 20 3d 20 74 61 72 67 65 74 3b 0a 20 20 20  eg = target;.   
19db3 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
19db4 20 20 20 20 63 61 73 65 20 54 4b 5f 42 49 54 4e      case TK_BITN
19db5 4f 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  OT:.    case TK_
19db6 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  NOT: {.      ass
19db7 65 72 74 28 20 54 4b 5f 42 49 54 4e 4f 54 3d 3d  ert( TK_BITNOT==
19db8 4f 50 5f 42 69 74 4e 6f 74 20 29 3b 0a 20 20 20  OP_BitNot );.   
19db9 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f     assert( TK_NO
19dba 54 3d 3d 4f 50 5f 4e 6f 74 20 29 3b 0a 20 20 20  T==OP_Not );.   
19dbb 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
19dbc 3d 54 4b 5f 42 49 54 4e 4f 54 20 29 3b 0a 20 20  =TK_BITNOT );.  
19dbd 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
19dbe 3d 3d 54 4b 5f 4e 4f 54 20 29 3b 0a 20 20 20 20  ==TK_NOT );.    
19dbf 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65    inReg = sqlite
19dc0 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28  3ExprCodeTarget(
19dc1 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
19dc2 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b 0a 20  Left, target);. 
19dc3 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
19dc4 6e 52 65 67 3d 3d 74 61 72 67 65 74 20 29 3b 0a  nReg==target );.
19dc5 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
19dc6 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63 68  usedAsColumnCach
19dc7 65 28 70 50 61 72 73 65 2c 20 69 6e 52 65 67 2c  e(pParse, inReg,
19dc8 20 69 6e 52 65 67 29 20 29 3b 0a 20 20 20 20 20   inReg) );.     
19dc9 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33   inReg = sqlite3
19dca 45 78 70 72 57 72 69 74 61 62 6c 65 52 65 67 69  ExprWritableRegi
19dcb 73 74 65 72 28 70 50 61 72 73 65 2c 20 69 6e 52  ster(pParse, inR
19dcc 65 67 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20  eg, target);.   
19dcd 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
19dce 64 4f 70 31 28 76 2c 20 6f 70 2c 20 69 6e 52 65  dOp1(v, op, inRe
19dcf 67 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  g);.      break;
19dd0 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
19dd1 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63  TK_ISNULL:.    c
19dd2 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20  ase TK_NOTNULL: 
19dd3 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72  {.      int addr
19dd4 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
19dd5 54 4b 5f 49 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73  TK_ISNULL==OP_Is
19dd6 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20 61 73  Null );.      as
19dd7 73 65 72 74 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c  sert( TK_NOTNULL
19dd8 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a  ==OP_NotNull );.
19dd9 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
19dda 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b  op==TK_ISNULL );
19ddb 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
19ddc 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20   op==TK_NOTNULL 
19ddd 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
19dde 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
19ddf 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 74 61 72  _Integer, 1, tar
19de0 67 65 74 29 3b 0a 20 20 20 20 20 20 72 31 20 3d  get);.      r1 =
19de1 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
19de2 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78  Temp(pParse, pEx
19de3 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46  pr->pLeft, &regF
19de4 72 65 65 31 29 3b 0a 20 20 20 20 20 20 74 65 73  ree1);.      tes
19de5 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d  tcase( regFree1=
19de6 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 64 64 72  =0 );.      addr
19de7 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
19de8 64 4f 70 31 28 76 2c 20 6f 70 2c 20 72 31 29 3b  dOp1(v, op, r1);
19de9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
19dea 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
19deb 64 64 49 6d 6d 2c 20 74 61 72 67 65 74 2c 20 2d  ddImm, target, -
19dec 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
19ded 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
19dee 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20 62 72   addr);.      br
19def 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
19df0 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54  ase TK_AGG_FUNCT
19df1 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 41 67 67  ION: {.      Agg
19df2 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 70 45  Info *pInfo = pE
19df3 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 20  xpr->pAggInfo;. 
19df4 20 20 20 20 20 69 66 28 20 70 49 6e 66 6f 3d 3d       if( pInfo==
19df5 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  0 ){.        sql
19df6 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
19df7 72 73 65 2c 20 22 6d 69 73 75 73 65 20 6f 66 20  rse, "misuse of 
19df8 61 67 67 72 65 67 61 74 65 3a 20 25 54 22 2c 0a  aggregate: %T",.
19df9 20 20 20 20 20 20 20 20 20 20 20 20 26 70 45 78              &pEx
19dfa 70 72 2d 3e 73 70 61 6e 29 3b 0a 20 20 20 20 20  pr->span);.     
19dfb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
19dfc 69 6e 52 65 67 20 3d 20 70 49 6e 66 6f 2d 3e 61  inReg = pInfo->a
19dfd 46 75 6e 63 5b 70 45 78 70 72 2d 3e 69 41 67 67  Func[pExpr->iAgg
19dfe 5d 2e 69 4d 65 6d 3b 0a 20 20 20 20 20 20 7d 0a  ].iMem;.      }.
19dff 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
19e00 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43   }.    case TK_C
19e01 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20 20 63  ONST_FUNC:.    c
19e02 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a  ase TK_FUNCTION:
19e03 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73   {.      ExprLis
19e04 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 70 72  t *pList = pExpr
19e05 2d 3e 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 69  ->pList;.      i
19e06 6e 74 20 6e 45 78 70 72 20 3d 20 70 4c 69 73 74  nt nExpr = pList
19e07 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20   ? pList->nExpr 
19e08 3a 20 30 3b 0a 20 20 20 20 20 20 46 75 6e 63 44  : 0;.      FuncD
19e09 65 66 20 2a 70 44 65 66 3b 0a 20 20 20 20 20 20  ef *pDef;.      
19e0a 69 6e 74 20 6e 49 64 3b 0a 20 20 20 20 20 20 63  int nId;.      c
19e0b 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 64 3b 0a  onst char *zId;.
19e0c 20 20 20 20 20 20 69 6e 74 20 63 6f 6e 73 74 4d        int constM
19e0d 61 73 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  ask = 0;.      i
19e0e 6e 74 20 69 3b 0a 20 20 20 20 20 20 73 71 6c 69  nt i;.      sqli
19e0f 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
19e10 2d 3e 64 62 3b 0a 20 20 20 20 20 20 75 38 20 65  ->db;.      u8 e
19e11 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20  nc = ENC(db);.  
19e12 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
19e13 6c 6c 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 74  ll = 0;..      t
19e14 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
19e15 43 4f 4e 53 54 5f 46 55 4e 43 20 29 3b 0a 20 20  CONST_FUNC );.  
19e16 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
19e17 3d 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 29 3b  ==TK_FUNCTION );
19e18 0a 20 20 20 20 20 20 7a 49 64 20 3d 20 28 63 68  .      zId = (ch
19e19 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  ar*)pExpr->token
19e1a 2e 7a 3b 0a 20 20 20 20 20 20 6e 49 64 20 3d 20  .z;.      nId = 
19e1b 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a  pExpr->token.n;.
19e1c 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c        pDef = sql
19e1d 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e  ite3FindFunction
19e1e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 49 64  (pParse->db, zId
19e1f 2c 20 6e 49 64 2c 20 6e 45 78 70 72 2c 20 65 6e  , nId, nExpr, en
19e20 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 61 73 73  c, 0);.      ass
19e21 65 72 74 28 20 70 44 65 66 21 3d 30 20 29 3b 0a  ert( pDef!=0 );.
19e22 20 20 20 20 20 20 69 66 28 20 70 4c 69 73 74 20        if( pList 
19e23 29 7b 0a 20 20 20 20 20 20 20 20 6e 45 78 70 72  ){.        nExpr
19e24 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b   = pList->nExpr;
19e25 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20 73 71  .        r1 = sq
19e26 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67  lite3GetTempRang
19e27 65 28 70 50 61 72 73 65 2c 20 6e 45 78 70 72 29  e(pParse, nExpr)
19e28 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
19e29 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73  3ExprCodeExprLis
19e2a 74 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c  t(pParse, pList,
19e2b 20 72 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d   r1, 1);.      }
19e2c 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6e 45  else{.        nE
19e2d 78 70 72 20 3d 20 72 31 20 3d 20 30 3b 0a 20 20  xpr = r1 = 0;.  
19e2e 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
19e2f 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
19e30 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 2f 2a 20  LTABLE.      /* 
19e31 50 6f 73 73 69 62 6c 79 20 6f 76 65 72 6c 6f 61  Possibly overloa
19e32 64 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69  d the function i
19e33 66 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  f the first argu
19e34 6d 65 6e 74 20 69 73 0a 20 20 20 20 20 20 2a 2a  ment is.      **
19e35 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
19e36 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a   column..      *
19e37 2a 0a 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 69  *.      ** For i
19e38 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e 73 20 28  nfix functions (
19e39 4c 49 4b 45 2c 20 47 4c 4f 42 2c 20 52 45 47 45  LIKE, GLOB, REGE
19e3a 58 50 2c 20 61 6e 64 20 4d 41 54 43 48 29 20 75  XP, and MATCH) u
19e3b 73 65 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  se the.      ** 
19e3c 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c  second argument,
19e3d 20 6e 6f 74 20 74 68 65 20 66 69 72 73 74 2c 20   not the first, 
19e3e 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  as the argument 
19e3f 74 6f 20 74 65 73 74 20 74 6f 0a 20 20 20 20 20  to test to.     
19e40 20 2a 2a 20 73 65 65 20 69 66 20 69 74 20 69 73   ** see if it is
19e41 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76   a column in a v
19e42 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54  irtual table.  T
19e43 68 69 73 20 69 73 20 64 6f 6e 65 20 62 65 63 61  his is done beca
19e44 75 73 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  use.      ** the
19e45 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 6f 66   left operand of
19e46 20 69 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e 73   infix functions
19e47 20 28 74 68 65 20 6f 70 65 72 61 6e 64 20 77 65   (the operand we
19e48 20 77 61 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a   want to.      *
19e49 2a 20 63 6f 6e 74 72 6f 6c 20 6f 76 65 72 6c 6f  * control overlo
19e4a 61 64 69 6e 67 29 20 65 6e 64 73 20 75 70 20 61  ading) ends up a
19e4b 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  s the second arg
19e4c 75 6d 65 6e 74 20 74 6f 20 74 68 65 0a 20 20 20  ument to the.   
19e4d 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20     ** function. 
19e4e 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   The expression 
19e4f 22 41 20 67 6c 6f 62 20 42 22 20 69 73 20 65 71  "A glob B" is eq
19e50 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20  uivalent to .   
19e51 20 20 20 2a 2a 20 22 67 6c 6f 62 28 42 2c 41 29     ** "glob(B,A)
19e52 2e 20 20 57 65 20 77 61 6e 74 20 74 6f 20 75 73  .  We want to us
19e53 65 20 74 68 65 20 41 20 69 6e 20 22 41 20 67 6c  e the A in "A gl
19e54 6f 62 20 42 22 20 74 6f 20 74 65 73 74 0a 20 20  ob B" to test.  
19e55 20 20 20 20 2a 2a 20 66 6f 72 20 66 75 6e 63 74      ** for funct
19e56 69 6f 6e 20 6f 76 65 72 6c 6f 61 64 69 6e 67 2e  ion overloading.
19e57 20 20 42 75 74 20 77 65 20 75 73 65 20 74 68 65    But we use the
19e58 20 42 20 74 65 72 6d 20 69 6e 20 22 67 6c 6f 62   B term in "glob
19e59 28 42 2c 41 29 22 2e 0a 20 20 20 20 20 20 2a 2f  (B,A)"..      */
19e5a 0a 20 20 20 20 20 20 69 66 28 20 6e 45 78 70 72  .      if( nExpr
19e5b 3e 3d 32 20 26 26 20 28 70 45 78 70 72 2d 3e 66  >=2 && (pExpr->f
19e5c 6c 61 67 73 20 26 20 45 50 5f 49 6e 66 69 78 46  lags & EP_InfixF
19e5d 75 6e 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20  unc) ){.        
19e5e 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 56 74  pDef = sqlite3Vt
19e5f 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69  abOverloadFuncti
19e60 6f 6e 28 64 62 2c 20 70 44 65 66 2c 20 6e 45 78  on(db, pDef, nEx
19e61 70 72 2c 20 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e  pr, pList->a[1].
19e62 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 7d 65  pExpr);.      }e
19e63 6c 73 65 20 69 66 28 20 6e 45 78 70 72 3e 30 20  lse if( nExpr>0 
19e64 29 7b 0a 20 20 20 20 20 20 20 20 70 44 65 66 20  ){.        pDef 
19e65 3d 20 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65  = sqlite3VtabOve
19e66 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 64 62  rloadFunction(db
19e67 2c 20 70 44 65 66 2c 20 6e 45 78 70 72 2c 20 70  , pDef, nExpr, p
19e68 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72  List->a[0].pExpr
19e69 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  );.      }.#endi
19e6a 66 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  f.      for(i=0;
19e6b 20 69 3c 6e 45 78 70 72 20 26 26 20 69 3c 33 32   i<nExpr && i<32
19e6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
19e6d 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49  if( sqlite3ExprI
19e6e 73 43 6f 6e 73 74 61 6e 74 28 70 4c 69 73 74 2d  sConstant(pList-
19e6f 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20 29 7b 0a  >a[i].pExpr) ){.
19e70 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 4d            constM
19e71 61 73 6b 20 7c 3d 20 28 31 3c 3c 69 29 3b 0a 20  ask |= (1<<i);. 
19e72 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
19e73 20 69 66 28 20 70 44 65 66 2d 3e 6e 65 65 64 43   if( pDef->needC
19e74 6f 6c 6c 53 65 71 20 26 26 20 21 70 43 6f 6c 6c  ollSeq && !pColl
19e75 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43   ){.          pC
19e76 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70  oll = sqlite3Exp
19e77 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  rCollSeq(pParse,
19e78 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78   pList->a[i].pEx
19e79 70 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  pr);.        }. 
19e7a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
19e7b 20 70 44 65 66 2d 3e 6e 65 65 64 43 6f 6c 6c 53   pDef->needCollS
19e7c 65 71 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  eq ){.        if
19e7d 28 20 21 70 43 6f 6c 6c 20 29 20 70 43 6f 6c 6c  ( !pColl ) pColl
19e7e 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 70   = pParse->db->p
19e7f 44 66 6c 74 43 6f 6c 6c 3b 20 0a 20 20 20 20 20  DfltColl; .     
19e80 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
19e81 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6c 6c 53  dOp4(v, OP_CollS
19e82 65 71 2c 20 30 2c 20 30 2c 20 30 2c 20 28 63 68  eq, 0, 0, 0, (ch
19e83 61 72 20 2a 29 70 43 6f 6c 6c 2c 20 50 34 5f 43  ar *)pColl, P4_C
19e84 4f 4c 4c 53 45 51 29 3b 0a 20 20 20 20 20 20 7d  OLLSEQ);.      }
19e85 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
19e86 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 46  beAddOp4(v, OP_F
19e87 75 6e 63 74 69 6f 6e 2c 20 63 6f 6e 73 74 4d 61  unction, constMa
19e88 73 6b 2c 20 72 31 2c 20 74 61 72 67 65 74 2c 0a  sk, r1, target,.
19e89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19e8a 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70          (char*)p
19e8b 44 65 66 2c 20 50 34 5f 46 55 4e 43 44 45 46 29  Def, P4_FUNCDEF)
19e8c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
19e8d 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 6e  dbeChangeP5(v, n
19e8e 45 78 70 72 29 3b 0a 20 20 20 20 20 20 69 66 28  Expr);.      if(
19e8f 20 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20   nExpr ){.      
19e90 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
19e91 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65  TempRange(pParse
19e92 2c 20 72 31 2c 20 6e 45 78 70 72 29 3b 0a 20 20  , r1, nExpr);.  
19e93 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
19e94 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69  te3ExprCacheAffi
19e95 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73  nityChange(pPars
19e96 65 2c 20 72 31 2c 20 6e 45 78 70 72 29 3b 0a 20  e, r1, nExpr);. 
19e97 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
19e98 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
19e99 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20  _OMIT_SUBQUERY. 
19e9a 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49 53 54     case TK_EXIST
19e9b 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53  S:.    case TK_S
19e9c 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20 20 74  ELECT: {.      t
19e9d 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
19e9e 45 58 49 53 54 53 20 29 3b 0a 20 20 20 20 20 20  EXISTS );.      
19e9f 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
19ea0 5f 53 45 4c 45 43 54 20 29 3b 0a 20 20 20 20 20  _SELECT );.     
19ea1 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c   if( pExpr->iCol
19ea2 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  umn==0 ){.      
19ea3 20 20 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62    sqlite3CodeSub
19ea4 73 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  select(pParse, p
19ea5 45 78 70 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Expr);.      }. 
19ea6 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 45 78       inReg = pEx
19ea7 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20  pr->iColumn;.   
19ea8 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
19ea9 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a 20      case TK_IN: 
19eaa 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 31 2c 20  {.      int j1, 
19eab 6a 32 2c 20 6a 33 2c 20 6a 34 2c 20 6a 35 3b 0a  j2, j3, j4, j5;.
19eac 20 20 20 20 20 20 63 68 61 72 20 61 66 66 69 6e        char affin
19ead 69 74 79 3b 0a 20 20 20 20 20 20 69 6e 74 20 65  ity;.      int e
19eae 54 79 70 65 3b 0a 0a 20 20 20 20 20 20 65 54 79  Type;..      eTy
19eaf 70 65 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  pe = sqlite3Find
19eb0 49 6e 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  InIndex(pParse, 
19eb1 70 45 78 70 72 2c 20 30 29 3b 0a 0a 20 20 20 20  pExpr, 0);..    
19eb2 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20    /* Figure out 
19eb3 74 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20  the affinity to 
19eb4 75 73 65 20 74 6f 20 63 72 65 61 74 65 20 61 20  use to create a 
19eb5 6b 65 79 20 66 72 6f 6d 20 74 68 65 20 72 65 73  key from the res
19eb6 75 6c 74 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66  ults.      ** of
19eb7 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e   the expression.
19eb8 20 61 66 66 69 6e 69 74 79 53 74 72 20 73 74 6f   affinityStr sto
19eb9 72 65 73 20 61 20 73 74 61 74 69 63 20 73 74 72  res a static str
19eba 69 6e 67 20 73 75 69 74 61 62 6c 65 20 66 6f 72  ing suitable for
19ebb 0a 20 20 20 20 20 20 2a 2a 20 50 34 20 6f 66 20  .      ** P4 of 
19ebc 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2e 0a 20  OP_MakeRecord.. 
19ebd 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 66       */.      af
19ebe 66 69 6e 69 74 79 20 3d 20 63 6f 6d 70 61 72 69  finity = compari
19ebf 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 45 78 70  sonAffinity(pExp
19ec0 72 29 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74  r);..      sqlit
19ec1 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
19ec2 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 74  OP_Integer, 1, t
19ec3 61 72 67 65 74 29 3b 0a 0a 20 20 20 20 20 20 2f  arget);..      /
19ec4 2a 20 43 6f 64 65 20 74 68 65 20 3c 65 78 70 72  * Code the <expr
19ec5 3e 20 66 72 6f 6d 20 22 3c 65 78 70 72 3e 20 49  > from "<expr> I
19ec6 4e 20 28 2e 2e 2e 29 22 2e 20 54 68 65 20 74 65  N (...)". The te
19ec7 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 0a 20 20  mporary table.  
19ec8 20 20 20 20 2a 2a 20 70 45 78 70 72 2d 3e 69 54      ** pExpr->iT
19ec9 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  able contains th
19eca 65 20 76 61 6c 75 65 73 20 74 68 61 74 20 6d 61  e values that ma
19ecb 6b 65 20 75 70 20 74 68 65 20 28 2e 2e 2e 29 20  ke up the (...) 
19ecc 73 65 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  set..      */.  
19ecd 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
19ece 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61  ExprCodeTemp(pPa
19ecf 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
19ed0 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20  t, &regFree1);. 
19ed1 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
19ed2 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20  egFree1==0 );.  
19ed3 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33      j1 = sqlite3
19ed4 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
19ed5 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 31 29 3b 0a 20  _NotNull, r1);. 
19ed6 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
19ed7 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
19ed8 6c 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20  l, 0, target);. 
19ed9 20 20 20 20 20 6a 32 20 20 3d 20 73 71 6c 69 74       j2  = sqlit
19eda 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20  e3VdbeAddOp0(v, 
19edb 4f 50 5f 47 6f 74 6f 29 3b 0a 20 20 20 20 20 20  OP_Goto);.      
19edc 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
19edd 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20  ere(v, j1);.    
19ede 20 20 69 66 28 20 65 54 79 70 65 3d 3d 49 4e 5f    if( eType==IN_
19edf 49 4e 44 45 58 5f 52 4f 57 49 44 20 29 7b 0a 20  INDEX_ROWID ){. 
19ee0 20 20 20 20 20 20 20 6a 33 20 3d 20 73 71 6c 69         j3 = sqli
19ee1 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
19ee2 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72   OP_MustBeInt, r
19ee3 31 29 3b 0a 20 20 20 20 20 20 20 20 6a 34 20 3d  1);.        j4 =
19ee4 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
19ee5 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73  p3(v, OP_NotExis
19ee6 74 73 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  ts, pExpr->iTabl
19ee7 65 2c 20 30 2c 20 72 31 29 3b 0a 20 20 20 20 20  e, 0, r1);.     
19ee8 20 20 20 6a 35 20 3d 20 73 71 6c 69 74 65 33 56     j5 = sqlite3V
19ee9 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f  dbeAddOp0(v, OP_
19eea 47 6f 74 6f 29 3b 0a 20 20 20 20 20 20 20 20 73  Goto);.        s
19eeb 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
19eec 72 65 28 76 2c 20 6a 33 29 3b 0a 20 20 20 20 20  re(v, j3);.     
19eed 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
19eee 6d 70 48 65 72 65 28 76 2c 20 6a 34 29 3b 0a 20  mpHere(v, j4);. 
19eef 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
19ef0 20 20 20 20 72 32 20 3d 20 72 65 67 46 72 65 65      r2 = regFree
19ef1 32 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  2 = sqlite3GetTe
19ef2 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
19ef3 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
19ef4 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d  beAddOp4(v, OP_M
19ef5 61 6b 65 52 65 63 6f 72 64 2c 20 72 31 2c 20 31  akeRecord, r1, 1
19ef6 2c 20 72 32 2c 20 26 61 66 66 69 6e 69 74 79 2c  , r2, &affinity,
19ef7 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   1);.        sql
19ef8 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66  ite3ExprCacheAff
19ef9 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72  inityChange(pPar
19efa 73 65 2c 20 72 31 2c 20 31 29 3b 0a 20 20 20 20  se, r1, 1);.    
19efb 20 20 20 20 6a 35 20 3d 20 73 71 6c 69 74 65 33      j5 = sqlite3
19efc 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
19efd 5f 46 6f 75 6e 64 2c 20 70 45 78 70 72 2d 3e 69  _Found, pExpr->i
19efe 54 61 62 6c 65 2c 20 30 2c 20 72 32 29 3b 0a 20  Table, 0, r2);. 
19eff 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
19f00 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
19f01 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 74 61 72  , OP_AddImm, tar
19f02 67 65 74 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  get, -1);.      
19f03 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
19f04 65 72 65 28 76 2c 20 6a 32 29 3b 0a 20 20 20 20  ere(v, j2);.    
19f05 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
19f06 70 48 65 72 65 28 76 2c 20 6a 35 29 3b 0a 20 20  pHere(v, j5);.  
19f07 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
19f08 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f 2a 0a 20  .#endif.    /*. 
19f09 20 20 20 2a 2a 20 20 20 20 78 20 42 45 54 57 45     **    x BETWE
19f0a 45 4e 20 79 20 41 4e 44 20 7a 0a 20 20 20 20 2a  EN y AND z.    *
19f0b 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73  *.    ** This is
19f0c 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 0a 20   equivalent to. 
19f0d 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20     **.    **    
19f0e 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a 20 20  x>=y AND x<=z.  
19f0f 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 58 20 69 73    **.    ** X is
19f10 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72   stored in pExpr
19f11 2d 3e 70 4c 65 66 74 2e 0a 20 20 20 20 2a 2a 20  ->pLeft..    ** 
19f12 59 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70  Y is stored in p
19f13 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30  Expr->pList->a[0
19f14 5d 2e 70 45 78 70 72 2e 0a 20 20 20 20 2a 2a 20  ].pExpr..    ** 
19f15 5a 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70  Z is stored in p
19f16 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31  Expr->pList->a[1
19f17 5d 2e 70 45 78 70 72 2e 0a 20 20 20 20 2a 2f 0a  ].pExpr..    */.
19f18 20 20 20 20 63 61 73 65 20 54 4b 5f 42 45 54 57      case TK_BETW
19f19 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20 45 78 70  EEN: {.      Exp
19f1a 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72  r *pLeft = pExpr
19f1b 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 73  ->pLeft;.      s
19f1c 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
19f1d 74 65 6d 20 2a 70 4c 49 74 65 6d 20 3d 20 70 45  tem *pLItem = pE
19f1e 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 3b 0a 20  xpr->pList->a;. 
19f1f 20 20 20 20 20 45 78 70 72 20 2a 70 52 69 67 68       Expr *pRigh
19f20 74 20 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 78 70  t = pLItem->pExp
19f21 72 3b 0a 0a 20 20 20 20 20 20 63 6f 64 65 43 6f  r;..      codeCo
19f22 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50  mpareOperands(pP
19f23 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 26 72 31  arse, pLeft, &r1
19f24 2c 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20  , &regFree1,.   
19f25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19f26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
19f27 52 69 67 68 74 2c 20 26 72 32 2c 20 26 72 65 67  Right, &r2, &reg
19f28 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 74 65  Free2);.      te
19f29 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31  stcase( regFree1
19f2a 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73  ==0 );.      tes
19f2b 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d  tcase( regFree2=
19f2c 3d 30 20 29 3b 0a 20 20 20 20 20 20 72 33 20 3d  =0 );.      r3 =
19f2d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
19f2e 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
19f2f 20 20 72 34 20 3d 20 73 71 6c 69 74 65 33 47 65    r4 = sqlite3Ge
19f30 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
19f31 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70  ;.      codeComp
19f32 61 72 65 28 70 50 61 72 73 65 2c 20 70 4c 65 66  are(pParse, pLef
19f33 74 2c 20 70 52 69 67 68 74 2c 20 4f 50 5f 47 65  t, pRight, OP_Ge
19f34 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
19f35 20 20 20 20 72 31 2c 20 72 32 2c 20 72 33 2c 20      r1, r2, r3, 
19f36 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 29 3b  SQLITE_STOREP2);
19f37 0a 20 20 20 20 20 20 70 4c 49 74 65 6d 2b 2b 3b  .      pLItem++;
19f38 0a 20 20 20 20 20 20 70 52 69 67 68 74 20 3d 20  .      pRight = 
19f39 70 4c 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20  pLItem->pExpr;. 
19f3a 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
19f3b 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
19f3c 65 2c 20 72 65 67 46 72 65 65 32 29 3b 0a 20 20  e, regFree2);.  
19f3d 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33      r2 = sqlite3
19f3e 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61  ExprCodeTemp(pPa
19f3f 72 73 65 2c 20 70 52 69 67 68 74 2c 20 26 72 65  rse, pRight, &re
19f40 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 74  gFree2);.      t
19f41 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
19f42 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 63 6f  2==0 );.      co
19f43 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65  deCompare(pParse
19f44 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c  , pLeft, pRight,
19f45 20 4f 50 5f 4c 65 2c 20 72 31 2c 20 72 32 2c 20   OP_Le, r1, r2, 
19f46 72 34 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 45  r4, SQLITE_STORE
19f47 50 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  P2);.      sqlit
19f48 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
19f49 4f 50 5f 41 6e 64 2c 20 72 33 2c 20 72 34 2c 20  OP_And, r3, r4, 
19f4a 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 73  target);.      s
19f4b 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
19f4c 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 33 29  pReg(pParse, r3)
19f4d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  ;.      sqlite3R
19f4e 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
19f4f 61 72 73 65 2c 20 72 34 29 3b 0a 20 20 20 20 20  arse, r4);.     
19f50 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
19f51 20 20 63 61 73 65 20 54 4b 5f 55 50 4c 55 53 3a    case TK_UPLUS:
19f52 20 7b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d   {.      inReg =
19f53 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
19f54 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70  Target(pParse, p
19f55 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72  Expr->pLeft, tar
19f56 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61  get);.      brea
19f57 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  k;.    }..    /*
19f58 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20 41 3a 0a  .    ** Form A:.
19f59 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20 78 20      **   CASE x 
19f5a 57 48 45 4e 20 65 31 20 54 48 45 4e 20 72 31 20  WHEN e1 THEN r1 
19f5b 57 48 45 4e 20 65 32 20 54 48 45 4e 20 72 32 20  WHEN e2 THEN r2 
19f5c 2e 2e 2e 20 57 48 45 4e 20 65 4e 20 54 48 45 4e  ... WHEN eN THEN
19f5d 20 72 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a 20   rN ELSE y END. 
19f5e 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f 72     **.    ** For
19f5f 6d 20 42 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41  m B:.    **   CA
19f60 53 45 20 57 48 45 4e 20 65 31 20 54 48 45 4e 20  SE WHEN e1 THEN 
19f61 72 31 20 57 48 45 4e 20 65 32 20 54 48 45 4e 20  r1 WHEN e2 THEN 
19f62 72 32 20 2e 2e 2e 20 57 48 45 4e 20 65 4e 20 54  r2 ... WHEN eN T
19f63 48 45 4e 20 72 4e 20 45 4c 53 45 20 79 20 45 4e  HEN rN ELSE y EN
19f64 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  D.    **.    ** 
19f65 46 6f 72 6d 20 41 20 69 73 20 63 61 6e 20 62 65  Form A is can be
19f66 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 69 6e 74   transformed int
19f67 6f 20 74 68 65 20 65 71 75 69 76 61 6c 65 6e 74  o the equivalent
19f68 20 66 6f 72 6d 20 42 20 61 73 20 66 6f 6c 6c 6f   form B as follo
19f69 77 73 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41 53  ws:.    **   CAS
19f6a 45 20 57 48 45 4e 20 78 3d 65 31 20 54 48 45 4e  E WHEN x=e1 THEN
19f6b 20 72 31 20 57 48 45 4e 20 78 3d 65 32 20 54 48   r1 WHEN x=e2 TH
19f6c 45 4e 20 72 32 20 2e 2e 2e 0a 20 20 20 20 2a 2a  EN r2 ....    **
19f6d 20 20 20 20 20 20 20 20 57 48 45 4e 20 78 3d 65          WHEN x=e
19f6e 4e 20 54 48 45 4e 20 72 4e 20 45 4c 53 45 20 79  N THEN rN ELSE y
19f6f 20 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20   END.    **.    
19f70 2a 2a 20 58 20 28 69 66 20 69 74 20 65 78 69 73  ** X (if it exis
19f71 74 73 29 20 69 73 20 69 6e 20 70 45 78 70 72 2d  ts) is in pExpr-
19f72 3e 70 4c 65 66 74 2e 0a 20 20 20 20 2a 2a 20 59  >pLeft..    ** Y
19f73 20 69 73 20 69 6e 20 70 45 78 70 72 2d 3e 70 52   is in pExpr->pR
19f74 69 67 68 74 2e 20 20 54 68 65 20 59 20 69 73 20  ight.  The Y is 
19f75 61 6c 73 6f 20 6f 70 74 69 6f 6e 61 6c 2e 20 20  also optional.  
19f76 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 0a 20  If there is no. 
19f77 20 20 20 2a 2a 20 45 4c 53 45 20 63 6c 61 75 73     ** ELSE claus
19f78 65 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 74  e and no other t
19f79 65 72 6d 20 6d 61 74 63 68 65 73 2c 20 74 68 65  erm matches, the
19f7a 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  n the result of 
19f7b 74 68 65 0a 20 20 20 20 2a 2a 20 65 78 70 72 73  the.    ** exprs
19f7c 73 69 6f 6e 20 69 73 20 4e 55 4c 4c 2e 0a 20 20  sion is NULL..  
19f7d 20 20 2a 2a 20 45 69 20 69 73 20 69 6e 20 70 45    ** Ei is in pE
19f7e 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a  xpr->pList->a[i*
19f7f 32 5d 20 61 6e 64 20 52 69 20 69 73 20 70 45 78  2] and Ri is pEx
19f80 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a 32  pr->pList->a[i*2
19f81 2b 31 5d 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  +1]..    **.    
19f82 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 6f 66  ** The result of
19f83 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
19f84 69 73 20 74 68 65 20 52 69 20 66 6f 72 20 74 68  is the Ri for th
19f85 65 20 66 69 72 73 74 20 6d 61 74 63 68 69 6e 67  e first matching
19f86 20 45 69 2c 0a 20 20 20 20 2a 2a 20 6f 72 20 69   Ei,.    ** or i
19f87 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61  f there is no ma
19f88 74 63 68 69 6e 67 20 45 69 2c 20 74 68 65 20 45  tching Ei, the E
19f89 4c 53 45 20 74 65 72 6d 20 59 2c 20 6f 72 20 69  LSE term Y, or i
19f8a 66 20 74 68 65 72 65 20 69 73 0a 20 20 20 20 2a  f there is.    *
19f8b 2a 20 6e 6f 20 45 4c 53 45 20 74 65 72 6d 2c 20  * no ELSE term, 
19f8c 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  NULL..    */.   
19f8d 20 63 61 73 65 20 54 4b 5f 43 41 53 45 3a 20 7b   case TK_CASE: {
19f8e 0a 20 20 20 20 20 20 69 6e 74 20 65 6e 64 4c 61  .      int endLa
19f8f 62 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  bel;            
19f90 20 20 20 20 20 20 20 20 20 2f 2a 20 47 4f 54 4f           /* GOTO
19f91 20 6c 61 62 65 6c 20 66 6f 72 20 65 6e 64 20 6f   label for end o
19f92 66 20 43 41 53 45 20 73 74 6d 74 20 2a 2f 0a 20  f CASE stmt */. 
19f93 20 20 20 20 20 69 6e 74 20 6e 65 78 74 43 61 73       int nextCas
19f94 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
19f95 20 20 20 20 20 20 20 2f 2a 20 47 4f 54 4f 20 6c         /* GOTO l
19f96 61 62 65 6c 20 66 6f 72 20 6e 65 78 74 20 57 48  abel for next WH
19f97 45 4e 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20  EN clause */.   
19f98 20 20 20 69 6e 74 20 6e 45 78 70 72 3b 20 20 20     int nExpr;   
19f99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19f9a 20 20 20 20 20 2f 2a 20 32 78 20 6e 75 6d 62 65       /* 2x numbe
19f9b 72 20 6f 66 20 57 48 45 4e 20 74 65 72 6d 73 20  r of WHEN terms 
19f9c 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20  */.      int i; 
19f9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19f9e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
19f9f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
19fa0 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45      ExprList *pE
19fa1 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  List;           
19fa2 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66        /* List of
19fa3 20 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a 20   WHEN terms */. 
19fa4 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72       struct Expr
19fa5 4c 69 73 74 5f 69 74 65 6d 20 2a 61 4c 69 73 74  List_item *aList
19fa6 65 6c 65 6d 3b 20 20 2f 2a 20 41 72 72 61 79 20  elem;  /* Array 
19fa7 6f 66 20 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f  of WHEN terms */
19fa8 0a 20 20 20 20 20 20 45 78 70 72 20 6f 70 43 6f  .      Expr opCo
19fa9 6d 70 61 72 65 3b 20 20 20 20 20 20 20 20 20 20  mpare;          
19faa 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
19fab 58 3d 3d 45 69 20 65 78 70 72 65 73 73 69 6f 6e  X==Ei expression
19fac 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 63   */.      Expr c
19fad 61 63 68 65 58 3b 20 20 20 20 20 20 20 20 20 20  acheX;          
19fae 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
19faf 61 63 68 65 64 20 65 78 70 72 65 73 73 69 6f 6e  ached expression
19fb0 20 58 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72   X */.      Expr
19fb1 20 2a 70 58 3b 20 20 20 20 20 20 20 20 20 20 20   *pX;           
19fb2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19fb3 20 54 68 65 20 58 20 65 78 70 72 65 73 73 69 6f   The X expressio
19fb4 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20  n */.      Expr 
19fb5 2a 70 54 65 73 74 3b 20 20 20 20 20 20 20 20 20  *pTest;         
19fb6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19fb7 58 3d 3d 45 69 20 28 66 6f 72 6d 20 41 29 20 6f  X==Ei (form A) o
19fb8 72 20 6a 75 73 74 20 45 69 20 28 66 6f 72 6d 20  r just Ei (form 
19fb9 42 29 20 2a 2f 0a 0a 20 20 20 20 20 20 61 73 73  B) */..      ass
19fba 65 72 74 28 70 45 78 70 72 2d 3e 70 4c 69 73 74  ert(pExpr->pList
19fbb 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
19fbc 28 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 6e  (pExpr->pList->n
19fbd 45 78 70 72 20 25 20 32 29 20 3d 3d 20 30 29 3b  Expr % 2) == 0);
19fbe 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 70 45  .      assert(pE
19fbf 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70  xpr->pList->nExp
19fc0 72 20 3e 20 30 29 3b 0a 20 20 20 20 20 20 70 45  r > 0);.      pE
19fc1 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c  List = pExpr->pL
19fc2 69 73 74 3b 0a 20 20 20 20 20 20 61 4c 69 73 74  ist;.      aList
19fc3 65 6c 65 6d 20 3d 20 70 45 4c 69 73 74 2d 3e 61  elem = pEList->a
19fc4 3b 0a 20 20 20 20 20 20 6e 45 78 70 72 20 3d 20  ;.      nExpr = 
19fc5 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20  pEList->nExpr;. 
19fc6 20 20 20 20 20 65 6e 64 4c 61 62 65 6c 20 3d 20       endLabel = 
19fc7 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
19fc8 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 69  abel(v);.      i
19fc9 66 28 20 28 70 58 20 3d 20 70 45 78 70 72 2d 3e  f( (pX = pExpr->
19fca 70 4c 65 66 74 29 21 3d 30 20 29 7b 0a 20 20 20  pLeft)!=0 ){.   
19fcb 20 20 20 20 20 63 61 63 68 65 58 20 3d 20 2a 70       cacheX = *p
19fcc 58 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63  X;.        testc
19fcd 61 73 65 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f  ase( pX->op==TK_
19fce 43 4f 4c 55 4d 4e 20 7c 7c 20 70 58 2d 3e 6f 70  COLUMN || pX->op
19fcf 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 29 3b  ==TK_REGISTER );
19fd0 0a 20 20 20 20 20 20 20 20 63 61 63 68 65 58 2e  .        cacheX.
19fd1 69 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33  iTable = sqlite3
19fd2 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61  ExprCodeTemp(pPa
19fd3 72 73 65 2c 20 70 58 2c 20 26 72 65 67 46 72 65  rse, pX, &regFre
19fd4 65 31 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73  e1);.        tes
19fd5 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d  tcase( regFree1=
19fd6 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 63 61  =0 );.        ca
19fd7 63 68 65 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 47  cheX.op = TK_REG
19fd8 49 53 54 45 52 3b 0a 20 20 20 20 20 20 20 20 63  ISTER;.        c
19fd9 61 63 68 65 58 2e 69 43 6f 6c 75 6d 6e 20 3d 20  acheX.iColumn = 
19fda 30 3b 0a 20 20 20 20 20 20 20 20 6f 70 43 6f 6d  0;.        opCom
19fdb 70 61 72 65 2e 6f 70 20 3d 20 54 4b 5f 45 51 3b  pare.op = TK_EQ;
19fdc 0a 20 20 20 20 20 20 20 20 6f 70 43 6f 6d 70 61  .        opCompa
19fdd 72 65 2e 70 4c 65 66 74 20 3d 20 26 63 61 63 68  re.pLeft = &cach
19fde 65 58 3b 0a 20 20 20 20 20 20 20 20 70 54 65 73  eX;.        pTes
19fdf 74 20 3d 20 26 6f 70 43 6f 6d 70 61 72 65 3b 0a  t = &opCompare;.
19fe0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50        }.      pP
19fe1 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c  arse->disableCol
19fe2 43 61 63 68 65 2b 2b 3b 0a 20 20 20 20 20 20 66  Cache++;.      f
19fe3 6f 72 28 69 3d 30 3b 20 69 3c 6e 45 78 70 72 3b  or(i=0; i<nExpr;
19fe4 20 69 3d 69 2b 32 29 7b 0a 20 20 20 20 20 20 20   i=i+2){.       
19fe5 20 69 66 28 20 70 58 20 29 7b 0a 20 20 20 20 20   if( pX ){.     
19fe6 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 70       opCompare.p
19fe7 52 69 67 68 74 20 3d 20 61 4c 69 73 74 65 6c 65  Right = aListele
19fe8 6d 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  m[i].pExpr;.    
19fe9 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
19fea 20 20 20 20 20 70 54 65 73 74 20 3d 20 61 4c 69       pTest = aLi
19feb 73 74 65 6c 65 6d 5b 69 5d 2e 70 45 78 70 72 3b  stelem[i].pExpr;
19fec 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
19fed 20 20 20 6e 65 78 74 43 61 73 65 20 3d 20 73 71     nextCase = sq
19fee 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
19fef 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 20 74  el(v);.        t
19ff0 65 73 74 63 61 73 65 28 20 70 54 65 73 74 2d 3e  estcase( pTest->
19ff1 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c  op==TK_COLUMN ||
19ff2 20 70 54 65 73 74 2d 3e 6f 70 3d 3d 54 4b 5f 52   pTest->op==TK_R
19ff3 45 47 49 53 54 45 52 20 29 3b 0a 20 20 20 20 20  EGISTER );.     
19ff4 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66     sqlite3ExprIf
19ff5 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 54  False(pParse, pT
19ff6 65 73 74 2c 20 6e 65 78 74 43 61 73 65 2c 20 53  est, nextCase, S
19ff7 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
19ff8 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63  );.        testc
19ff9 61 73 65 28 20 61 4c 69 73 74 65 6c 65 6d 5b 69  ase( aListelem[i
19ffa 2b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  +1].pExpr->op==T
19ffb 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20  K_COLUMN );.    
19ffc 20 20 20 20 74 65 73 74 63 61 73 65 28 20 61 4c      testcase( aL
19ffd 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78  istelem[i+1].pEx
19ffe 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53  pr->op==TK_REGIS
19fff 54 45 52 20 29 3b 0a 20 20 20 20 20 20 20 20 73  TER );.        s
1a000 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
1a001 50 61 72 73 65 2c 20 61 4c 69 73 74 65 6c 65 6d  Parse, aListelem
1a002 5b 69 2b 31 5d 2e 70 45 78 70 72 2c 20 74 61 72  [i+1].pExpr, tar
1a003 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71  get);.        sq
1a004 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1a005 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 65  v, OP_Goto, 0, e
1a006 6e 64 4c 61 62 65 6c 29 3b 0a 20 20 20 20 20 20  ndLabel);.      
1a007 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
1a008 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 6e 65 78  olveLabel(v, nex
1a009 74 43 61 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a  tCase);.      }.
1a00a 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
1a00b 3e 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  >pRight ){.     
1a00c 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
1a00d 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  de(pParse, pExpr
1a00e 2d 3e 70 52 69 67 68 74 2c 20 74 61 72 67 65 74  ->pRight, target
1a00f 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
1a010 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1a011 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1a012 4e 75 6c 6c 2c 20 30 2c 20 74 61 72 67 65 74 29  Null, 0, target)
1a013 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1a014 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
1a015 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4c 61  veLabel(v, endLa
1a016 62 65 6c 29 3b 0a 20 20 20 20 20 20 61 73 73 65  bel);.      asse
1a017 72 74 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61  rt( pParse->disa
1a018 62 6c 65 43 6f 6c 43 61 63 68 65 3e 30 20 29 3b  bleColCache>0 );
1a019 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64  .      pParse->d
1a01a 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 2d 2d  isableColCache--
1a01b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1a01c 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
1a01d 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
1a01e 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 41 49  .    case TK_RAI
1a01f 53 45 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20  SE: {.      if( 
1a020 21 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61  !pParse->trigSta
1a021 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ck ){.        sq
1a022 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1a023 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20  arse,.          
1a024 20 20 20 20 20 20 20 20 20 20 20 20 20 22 52 41               "RA
1a025 49 53 45 28 29 20 6d 61 79 20 6f 6e 6c 79 20 62  ISE() may only b
1a026 65 20 75 73 65 64 20 77 69 74 68 69 6e 20 61 20  e used within a 
1a027 74 72 69 67 67 65 72 2d 70 72 6f 67 72 61 6d 22  trigger-program"
1a028 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
1a029 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 0;.      }.   
1a02a 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43     if( pExpr->iC
1a02b 6f 6c 75 6d 6e 21 3d 4f 45 5f 49 67 6e 6f 72 65  olumn!=OE_Ignore
1a02c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 61 73 73   ){.         ass
1a02d 65 72 74 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c  ert( pExpr->iCol
1a02e 75 6d 6e 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b  umn==OE_Rollback
1a02f 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20   ||.            
1a030 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c       pExpr->iCol
1a031 75 6d 6e 20 3d 3d 20 4f 45 5f 41 62 6f 72 74 20  umn == OE_Abort 
1a032 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ||.             
1a033 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75      pExpr->iColu
1a034 6d 6e 20 3d 3d 20 4f 45 5f 46 61 69 6c 20 29 3b  mn == OE_Fail );
1a035 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  .         sqlite
1a036 33 44 65 71 75 6f 74 65 45 78 70 72 28 70 50 61  3DequoteExpr(pPa
1a037 72 73 65 2d 3e 64 62 2c 20 70 45 78 70 72 29 3b  rse->db, pExpr);
1a038 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  .         sqlite
1a039 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
1a03a 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f 43  P_Halt, SQLITE_C
1a03b 4f 4e 53 54 52 41 49 4e 54 2c 20 70 45 78 70 72  ONSTRAINT, pExpr
1a03c 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 30 2c 0a 20 20  ->iColumn, 0,.  
1a03d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a03e 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 45 78        (char*)pEx
1a03f 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78  pr->token.z, pEx
1a040 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b 0a 20 20  pr->token.n);.  
1a041 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20      } else {.   
1a042 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45        assert( pE
1a043 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 3d 20  xpr->iColumn == 
1a044 4f 45 5f 49 67 6e 6f 72 65 20 29 3b 0a 20 20 20  OE_Ignore );.   
1a045 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1a046 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f  eAddOp2(v, OP_Co
1a047 6e 74 65 78 74 50 6f 70 2c 20 30 2c 20 30 29 3b  ntextPop, 0, 0);
1a048 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  .         sqlite
1a049 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1a04a 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 50 61 72 73  P_Goto, 0, pPars
1a04b 65 2d 3e 74 72 69 67 53 74 61 63 6b 2d 3e 69 67  e->trigStack->ig
1a04c 6e 6f 72 65 4a 75 6d 70 29 3b 0a 20 20 20 20 20  noreJump);.     
1a04d 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
1a04e 28 76 2c 20 22 72 61 69 73 65 28 49 47 4e 4f 52  (v, "raise(IGNOR
1a04f 45 29 22 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20  E)"));.      }. 
1a050 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1a051 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 73  }.#endif.  }.  s
1a052 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
1a053 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
1a054 46 72 65 65 31 29 3b 0a 20 20 73 71 6c 69 74 65  Free1);.  sqlite
1a055 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
1a056 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 32  pParse, regFree2
1a057 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 6e 52 65  );.  return inRe
1a058 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  g;.}../*.** Gene
1a059 72 61 74 65 20 63 6f 64 65 20 74 6f 20 65 76 61  rate code to eva
1a05a 6c 75 61 74 65 20 61 6e 20 65 78 70 72 65 73 73  luate an express
1a05b 69 6f 6e 20 61 6e 64 20 73 74 6f 72 65 20 74 68  ion and store th
1a05c 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 69 6e 74  e results.** int
1a05d 6f 20 61 20 72 65 67 69 73 74 65 72 2e 20 20 52  o a register.  R
1a05e 65 74 75 72 6e 20 74 68 65 20 72 65 67 69 73 74  eturn the regist
1a05f 65 72 20 6e 75 6d 62 65 72 20 77 68 65 72 65 20  er number where 
1a060 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 61  the results.** a
1a061 72 65 20 73 74 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a  re stored..**.**
1a062 20 49 66 20 74 68 65 20 72 65 67 69 73 74 65 72   If the register
1a063 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20   is a temporary 
1a064 72 65 67 69 73 74 65 72 20 74 68 61 74 20 63 61  register that ca
1a065 6e 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64  n be deallocated
1a066 2c 0a 2a 2a 20 74 68 65 6e 20 77 72 69 74 65 20  ,.** then write 
1a067 69 74 73 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20  its number into 
1a068 2a 70 52 65 67 2e 20 20 49 66 20 74 68 65 20 72  *pReg.  If the r
1a069 65 73 75 6c 74 20 72 65 67 69 73 74 65 72 20 69  esult register i
1a06a 73 20 6e 6f 74 0a 2a 2a 20 61 20 74 65 6d 70 6f  s not.** a tempo
1a06b 72 61 72 79 2c 20 74 68 65 6e 20 73 65 74 20 2a  rary, then set *
1a06c 70 52 65 67 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2f  pReg to zero..*/
1a06d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a06e 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43  int sqlite3ExprC
1a06f 6f 64 65 54 65 6d 70 28 50 61 72 73 65 20 2a 70  odeTemp(Parse *p
1a070 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78  Parse, Expr *pEx
1a071 70 72 2c 20 69 6e 74 20 2a 70 52 65 67 29 7b 0a  pr, int *pReg){.
1a072 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74    int r1 = sqlit
1a073 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
1a074 72 73 65 29 3b 0a 20 20 69 6e 74 20 72 32 20 3d  rse);.  int r2 =
1a075 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
1a076 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70  Target(pParse, p
1a077 45 78 70 72 2c 20 72 31 29 3b 0a 20 20 69 66 28  Expr, r1);.  if(
1a078 20 72 32 3d 3d 72 31 20 29 7b 0a 20 20 20 20 2a   r2==r1 ){.    *
1a079 70 52 65 67 20 3d 20 72 31 3b 0a 20 20 7d 65 6c  pReg = r1;.  }el
1a07a 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52  se{.    sqlite3R
1a07b 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
1a07c 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 2a  arse, r1);.    *
1a07d 70 52 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  pReg = 0;.  }.  
1a07e 72 65 74 75 72 6e 20 72 32 3b 0a 7d 0a 0a 2f 2a  return r2;.}../*
1a07f 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
1a080 65 20 74 68 61 74 20 77 69 6c 6c 20 65 76 61 6c  e that will eval
1a081 75 61 74 65 20 65 78 70 72 65 73 73 69 6f 6e 20  uate expression 
1a082 70 45 78 70 72 20 61 6e 64 20 73 74 6f 72 65 20  pExpr and store 
1a083 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69  the.** results i
1a084 6e 20 72 65 67 69 73 74 65 72 20 74 61 72 67 65  n register targe
1a085 74 2e 20 20 54 68 65 20 72 65 73 75 6c 74 73 20  t.  The results 
1a086 61 72 65 20 67 75 61 72 61 6e 74 65 65 64 20 74  are guaranteed t
1a087 6f 20 61 70 70 65 61 72 0a 2a 2a 20 69 6e 20 72  o appear.** in r
1a088 65 67 69 73 74 65 72 20 74 61 72 67 65 74 2e 0a  egister target..
1a089 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1a08a 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
1a08b 72 43 6f 64 65 28 50 61 72 73 65 20 2a 70 50 61  rCode(Parse *pPa
1a08c 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72  rse, Expr *pExpr
1a08d 2c 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 20  , int target){. 
1a08e 20 69 6e 74 20 69 6e 52 65 67 3b 0a 0a 20 20 61   int inReg;..  a
1a08f 73 73 65 72 74 28 20 74 61 72 67 65 74 3e 30 20  ssert( target>0 
1a090 26 26 20 74 61 72 67 65 74 3c 3d 70 50 61 72 73  && target<=pPars
1a091 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 69 6e 52  e->nMem );.  inR
1a092 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  eg = sqlite3Expr
1a093 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73  CodeTarget(pPars
1a094 65 2c 20 70 45 78 70 72 2c 20 74 61 72 67 65 74  e, pExpr, target
1a095 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
1a096 72 73 65 2d 3e 70 56 64 62 65 20 7c 7c 20 70 50  rse->pVdbe || pP
1a097 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  arse->db->malloc
1a098 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20  Failed );.  if( 
1a099 69 6e 52 65 67 21 3d 74 61 72 67 65 74 20 26 26  inReg!=target &&
1a09a 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20 29   pParse->pVdbe )
1a09b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1a09c 65 41 64 64 4f 70 32 28 70 50 61 72 73 65 2d 3e  eAddOp2(pParse->
1a09d 70 56 64 62 65 2c 20 4f 50 5f 53 43 6f 70 79 2c  pVdbe, OP_SCopy,
1a09e 20 69 6e 52 65 67 2c 20 74 61 72 67 65 74 29 3b   inReg, target);
1a09f 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 74 61  .  }.  return ta
1a0a0 72 67 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  rget;.}../*.** G
1a0a1 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
1a0a2 74 20 65 76 61 6c 75 74 65 73 20 74 68 65 20 67  t evalutes the g
1a0a3 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  iven expression 
1a0a4 61 6e 64 20 70 75 74 73 20 74 68 65 20 72 65 73  and puts the res
1a0a5 75 6c 74 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74  ult.** in regist
1a0a6 65 72 20 74 61 72 67 65 74 2e 0a 2a 2a 0a 2a 2a  er target..**.**
1a0a7 20 41 6c 73 6f 20 6d 61 6b 65 20 61 20 63 6f 70   Also make a cop
1a0a8 79 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73  y of the express
1a0a9 69 6f 6e 20 72 65 73 75 6c 74 73 20 69 6e 74 6f  ion results into
1a0aa 20 61 6e 6f 74 68 65 72 20 22 63 61 63 68 65 22   another "cache"
1a0ab 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 61 6e 64   register.** and
1a0ac 20 6d 6f 64 69 66 79 20 74 68 65 20 65 78 70 72   modify the expr
1a0ad 65 73 73 69 6f 6e 20 73 6f 20 74 68 61 74 20 74  ession so that t
1a0ae 68 65 20 6e 65 78 74 20 74 69 6d 65 20 69 74 20  he next time it 
1a0af 69 73 20 65 76 61 6c 75 61 74 65 64 2c 0a 2a 2a  is evaluated,.**
1a0b0 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61   the result is a
1a0b1 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63 61 63   copy of the cac
1a0b2 68 65 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a 0a  he register..**.
1a0b3 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1a0b4 69 73 20 75 73 65 64 20 66 6f 72 20 65 78 70 72  is used for expr
1a0b5 65 73 73 69 6f 6e 73 20 74 68 61 74 20 61 72 65  essions that are
1a0b6 20 75 73 65 64 20 6d 75 6c 74 69 70 6c 65 20 0a   used multiple .
1a0b7 2a 2a 20 74 69 6d 65 73 2e 20 20 54 68 65 79 20  ** times.  They 
1a0b8 61 72 65 20 65 76 61 6c 75 61 74 65 64 20 6f 6e  are evaluated on
1a0b9 63 65 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c  ce and the resul
1a0ba 74 73 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  ts of the expres
1a0bb 73 69 6f 6e 0a 2a 2a 20 61 72 65 20 72 65 75 73  sion.** are reus
1a0bc 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
1a0bd 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1a0be 33 45 78 70 72 43 6f 64 65 41 6e 64 43 61 63 68  3ExprCodeAndCach
1a0bf 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
1a0c0 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e   Expr *pExpr, in
1a0c1 74 20 74 61 72 67 65 74 29 7b 0a 20 20 56 64 62  t target){.  Vdb
1a0c2 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
1a0c3 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 6e 52 65  Vdbe;.  int inRe
1a0c4 67 3b 0a 20 20 69 6e 52 65 67 20 3d 20 73 71 6c  g;.  inReg = sql
1a0c5 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
1a0c6 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61 72 67  rse, pExpr, targ
1a0c7 65 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 74  et);.  assert( t
1a0c8 61 72 67 65 74 3e 30 20 29 3b 0a 20 20 69 66 28  arget>0 );.  if(
1a0c9 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 52   pExpr->op!=TK_R
1a0ca 45 47 49 53 54 45 52 20 29 7b 20 20 0a 20 20 20  EGISTER ){  .   
1a0cb 20 69 6e 74 20 69 4d 65 6d 3b 0a 20 20 20 20 69   int iMem;.    i
1a0cc 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  Mem = ++pParse->
1a0cd 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65  nMem;.    sqlite
1a0ce 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1a0cf 50 5f 43 6f 70 79 2c 20 69 6e 52 65 67 2c 20 69  P_Copy, inReg, i
1a0d0 4d 65 6d 29 3b 0a 20 20 20 20 70 45 78 70 72 2d  Mem);.    pExpr-
1a0d1 3e 69 54 61 62 6c 65 20 3d 20 69 4d 65 6d 3b 0a  >iTable = iMem;.
1a0d2 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75      pExpr->iColu
1a0d3 6d 6e 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a  mn = pExpr->op;.
1a0d4 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20      pExpr->op = 
1a0d5 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 7d  TK_REGISTER;.  }
1a0d6 0a 20 20 72 65 74 75 72 6e 20 69 6e 52 65 67 3b  .  return inReg;
1a0d7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1a0d8 20 54 52 55 45 20 69 66 20 70 45 78 70 72 20 69   TRUE if pExpr i
1a0d9 73 20 61 6e 20 63 6f 6e 73 74 61 6e 74 20 65 78  s an constant ex
1a0da 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73  pression that is
1a0db 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20   appropriate.** 
1a0dc 66 6f 72 20 66 61 63 74 6f 72 69 6e 67 20 6f 75  for factoring ou
1a0dd 74 20 6f 66 20 61 20 6c 6f 6f 70 2e 20 20 41 70  t of a loop.  Ap
1a0de 70 72 6f 70 72 69 61 74 65 20 65 78 70 72 65 73  propriate expres
1a0df 73 69 6f 6e 73 20 61 72 65 3a 0a 2a 2a 0a 2a 2a  sions are:.**.**
1a0e0 20 20 20 20 2a 20 20 41 6e 79 20 65 78 70 72 65      *  Any expre
1a0e1 73 73 69 6f 6e 20 74 68 61 74 20 65 76 61 6c 75  ssion that evalu
1a0e2 61 74 65 73 20 74 6f 20 74 77 6f 20 6f 72 20 6d  ates to two or m
1a0e3 6f 72 65 20 6f 70 63 6f 64 65 73 2e 0a 2a 2a 0a  ore opcodes..**.
1a0e4 2a 2a 20 20 20 20 2a 20 20 41 6e 79 20 4f 50 5f  **    *  Any OP_
1a0e5 49 6e 74 65 67 65 72 2c 20 4f 50 5f 52 65 61 6c  Integer, OP_Real
1a0e6 2c 20 4f 50 5f 53 74 72 69 6e 67 2c 20 4f 50 5f  , OP_String, OP_
1a0e7 42 6c 6f 62 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 0a  Blob, OP_Null, .
1a0e8 2a 2a 20 20 20 20 20 20 20 6f 72 20 4f 50 5f 56  **       or OP_V
1a0e9 61 72 69 61 62 6c 65 20 74 68 61 74 20 64 6f 65  ariable that doe
1a0ea 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65  s not need to be
1a0eb 20 70 6c 61 63 65 64 20 69 6e 20 61 20 0a 2a 2a   placed in a .**
1a0ec 20 20 20 20 20 20 20 73 70 65 63 69 66 69 63 20         specific 
1a0ed 72 65 67 69 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20  register..**.** 
1a0ee 54 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 69 6e  There is no poin
1a0ef 74 20 69 6e 20 66 61 63 74 6f 72 69 6e 67 20 6f  t in factoring o
1a0f0 75 74 20 73 69 6e 67 6c 65 2d 69 6e 73 74 72 75  ut single-instru
1a0f1 63 74 69 6f 6e 20 63 6f 6e 73 74 61 6e 74 0a 2a  ction constant.*
1a0f2 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68  * expressions th
1a0f3 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20 70 6c  at need to be pl
1a0f4 61 63 65 64 20 69 6e 20 61 20 70 61 72 74 69 63  aced in a partic
1a0f5 75 6c 61 72 20 72 65 67 69 73 74 65 72 2e 20 20  ular register.  
1a0f6 0a 2a 2a 20 57 65 20 63 6f 75 6c 64 20 66 61 63  .** We could fac
1a0f7 74 6f 72 20 74 68 65 6d 20 6f 75 74 2c 20 62 75  tor them out, bu
1a0f8 74 20 74 68 65 6e 20 77 65 20 77 6f 75 6c 64 20  t then we would 
1a0f9 65 6e 64 20 75 70 20 61 64 64 69 6e 67 20 61 6e  end up adding an
1a0fa 0a 2a 2a 20 4f 50 5f 53 43 6f 70 79 20 69 6e 73  .** OP_SCopy ins
1a0fb 74 72 75 63 74 69 6f 6e 20 74 6f 20 6d 6f 76 65  truction to move
1a0fc 20 74 68 65 20 76 61 6c 75 65 20 69 6e 74 6f 20   the value into 
1a0fd 74 68 65 20 63 6f 72 72 65 63 74 20 72 65 67 69  the correct regi
1a0fe 73 74 65 72 0a 2a 2a 20 6c 61 74 65 72 2e 20 20  ster.** later.  
1a0ff 57 65 20 6d 69 67 68 74 20 61 73 20 77 65 6c 6c  We might as well
1a100 20 6a 75 73 74 20 75 73 65 20 74 68 65 20 6f 72   just use the or
1a101 69 67 69 6e 61 6c 20 69 6e 73 74 72 75 63 74 69  iginal instructi
1a102 6f 6e 20 61 6e 64 0a 2a 2a 20 61 76 6f 69 64 20  on and.** avoid 
1a103 74 68 65 20 4f 50 5f 53 43 6f 70 79 2e 0a 2a 2f  the OP_SCopy..*/
1a104 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 41 70  .static int isAp
1a105 70 72 6f 70 72 69 61 74 65 46 6f 72 46 61 63 74  propriateForFact
1a106 6f 72 69 6e 67 28 45 78 70 72 20 2a 70 29 7b 0a  oring(Expr *p){.
1a107 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 78    if( !sqlite3Ex
1a108 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a  prIsConstantNotJ
1a109 6f 69 6e 28 70 29 20 29 7b 0a 20 20 20 20 72 65  oin(p) ){.    re
1a10a 74 75 72 6e 20 30 3b 20 20 2f 2a 20 4f 6e 6c 79  turn 0;  /* Only
1a10b 20 63 6f 6e 73 74 61 6e 74 20 65 78 70 72 65 73   constant expres
1a10c 73 69 6f 6e 73 20 61 72 65 20 61 70 70 72 6f 70  sions are approp
1a10d 72 69 61 74 65 20 66 6f 72 20 66 61 63 74 6f 72  riate for factor
1a10e 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28  ing */.  }.  if(
1a10f 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f   (p->flags & EP_
1a110 46 69 78 65 64 44 65 73 74 29 3d 3d 30 20 29 7b  FixedDest)==0 ){
1a111 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 20 20  .    return 1;  
1a112 2f 2a 20 41 6e 79 20 63 6f 6e 73 74 61 6e 74 20  /* Any constant 
1a113 77 69 74 68 6f 75 74 20 61 20 66 69 78 65 64 20  without a fixed 
1a114 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73 20 61  destination is a
1a115 70 70 72 6f 70 72 69 61 74 65 20 2a 2f 0a 20 20  ppropriate */.  
1a116 7d 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 6f 70  }.  while( p->op
1a117 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20 70 20 3d  ==TK_UPLUS ) p =
1a118 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 73 77 69   p->pLeft;.  swi
1a119 74 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 23 69  tch( p->op ){.#i
1a11a 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1a11b 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a 20  T_BLOB_LITERAL. 
1a11c 20 20 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42 3a     case TK_BLOB:
1a11d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65  .#endif.    case
1a11e 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 0a 20 20   TK_VARIABLE:.  
1a11f 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 45 47 45    case TK_INTEGE
1a120 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46  R:.    case TK_F
1a121 4c 4f 41 54 3a 0a 20 20 20 20 63 61 73 65 20 54  LOAT:.    case T
1a122 4b 5f 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65  K_NULL:.    case
1a123 20 54 4b 5f 53 54 52 49 4e 47 3a 20 7b 0a 20 20   TK_STRING: {.  
1a124 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 2d      testcase( p-
1a125 3e 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 20 29 3b 0a  >op==TK_BLOB );.
1a126 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1a127 70 2d 3e 6f 70 3d 3d 54 4b 5f 56 41 52 49 41 42  p->op==TK_VARIAB
1a128 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  LE );.      test
1a129 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f  case( p->op==TK_
1a12a 49 4e 54 45 47 45 52 20 29 3b 0a 20 20 20 20 20  INTEGER );.     
1a12b 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70   testcase( p->op
1a12c 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 3b 0a 20 20  ==TK_FLOAT );.  
1a12d 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 2d      testcase( p-
1a12e 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c 20 29 3b 0a  >op==TK_NULL );.
1a12f 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1a130 70 2d 3e 6f 70 3d 3d 54 4b 5f 53 54 52 49 4e 47  p->op==TK_STRING
1a131 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 53 69 6e   );.      /* Sin
1a132 67 6c 65 2d 69 6e 73 74 72 75 63 74 69 6f 6e 20  gle-instruction 
1a133 63 6f 6e 73 74 61 6e 74 73 20 77 69 74 68 20 61  constants with a
1a134 20 66 69 78 65 64 20 64 65 73 74 69 6e 61 74 69   fixed destinati
1a135 6f 6e 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20  on are.      ** 
1a136 62 65 74 74 65 72 20 64 6f 6e 65 20 69 6e 2d 6c  better done in-l
1a137 69 6e 65 2e 20 20 49 66 20 77 65 20 66 61 63 74  ine.  If we fact
1a138 6f 72 20 74 68 65 6d 2c 20 74 68 65 79 20 77 69  or them, they wi
1a139 6c 6c 20 6a 75 73 74 20 65 6e 64 0a 20 20 20 20  ll just end.    
1a13a 20 20 2a 2a 20 75 70 20 67 65 6e 65 72 61 74 69    ** up generati
1a13b 6e 67 20 61 6e 20 4f 50 5f 53 43 6f 70 79 20 74  ng an OP_SCopy t
1a13c 6f 20 6d 6f 76 65 20 74 68 65 20 76 61 6c 75 65  o move the value
1a13d 20 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74   to the destinat
1a13e 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 72 65 67  ion.      ** reg
1a13f 69 73 74 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20  ister. */.      
1a140 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a  return 0;.    }.
1a141 20 20 20 20 63 61 73 65 20 54 4b 5f 55 4d 49 4e      case TK_UMIN
1a142 55 53 3a 20 7b 0a 20 20 20 20 20 20 20 69 66 28  US: {.       if(
1a143 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54   p->pLeft->op==T
1a144 4b 5f 46 4c 4f 41 54 20 7c 7c 20 70 2d 3e 70 4c  K_FLOAT || p->pL
1a145 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45  eft->op==TK_INTE
1a146 47 45 52 20 29 7b 0a 20 20 20 20 20 20 20 20 20  GER ){.         
1a147 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
1a148 20 7d 0a 20 20 20 20 20 20 20 62 72 65 61 6b 3b   }.       break;
1a149 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75  .    }.    defau
1a14a 6c 74 3a 20 7b 0a 20 20 20 20 20 20 62 72 65 61  lt: {.      brea
1a14b 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  k;.    }.  }.  r
1a14c 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 1;.}../*.*
1a14d 2a 20 49 66 20 70 45 78 70 72 20 69 73 20 61 20  * If pExpr is a 
1a14e 63 6f 6e 73 74 61 6e 74 20 65 78 70 72 65 73 73  constant express
1a14f 69 6f 6e 20 74 68 61 74 20 69 73 20 61 70 70 72  ion that is appr
1a150 6f 70 72 69 61 74 65 20 66 6f 72 0a 2a 2a 20 66  opriate for.** f
1a151 61 63 74 6f 72 69 6e 67 20 6f 75 74 20 6f 66 20  actoring out of 
1a152 61 20 6c 6f 6f 70 2c 20 74 68 65 6e 20 65 76 61  a loop, then eva
1a153 6c 75 61 74 65 20 74 68 65 20 65 78 70 72 65 73  luate the expres
1a154 73 69 6f 6e 0a 2a 2a 20 69 6e 74 6f 20 61 20 72  sion.** into a r
1a155 65 67 69 73 74 65 72 20 61 6e 64 20 63 6f 6e 76  egister and conv
1a156 65 72 74 20 74 68 65 20 65 78 70 72 65 73 73 69  ert the expressi
1a157 6f 6e 20 69 6e 74 6f 20 61 20 54 4b 5f 52 45 47  on into a TK_REG
1a158 49 53 54 45 52 0a 2a 2a 20 65 78 70 72 65 73 73  ISTER.** express
1a159 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ion..*/.static i
1a15a 6e 74 20 65 76 61 6c 43 6f 6e 73 74 45 78 70 72  nt evalConstExpr
1a15b 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 45 78 70  (void *pArg, Exp
1a15c 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 50 61 72  r *pExpr){.  Par
1a15d 73 65 20 2a 70 50 61 72 73 65 20 3d 20 28 50 61  se *pParse = (Pa
1a15e 72 73 65 2a 29 70 41 72 67 3b 0a 20 20 73 77 69  rse*)pArg;.  swi
1a15f 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29  tch( pExpr->op )
1a160 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45  {.    case TK_RE
1a161 47 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20 20  GISTER: {.      
1a162 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
1a163 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e 43      case TK_FUNC
1a164 54 49 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20 54  TION:.    case T
1a165 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a  K_AGG_FUNCTION:.
1a166 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 53      case TK_CONS
1a167 54 5f 46 55 4e 43 3a 20 7b 0a 20 20 20 20 20 20  T_FUNC: {.      
1a168 2f 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 73  /* The arguments
1a169 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 68   to a function h
1a16a 61 76 65 20 61 20 66 69 78 65 64 20 64 65 73 74  ave a fixed dest
1a16b 69 6e 61 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a  ination..      *
1a16c 2a 20 4d 61 72 6b 20 74 68 65 6d 20 74 68 69 73  * Mark them this
1a16d 20 77 61 79 20 74 6f 20 61 76 6f 69 64 20 67 65   way to avoid ge
1a16e 6e 65 72 61 74 65 64 20 75 6e 6e 65 65 64 65 64  nerated unneeded
1a16f 20 4f 50 5f 53 43 6f 70 79 0a 20 20 20 20 20 20   OP_SCopy.      
1a170 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 2e  ** instructions.
1a171 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20   .      */.     
1a172 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
1a173 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b   = pExpr->pList;
1a174 0a 20 20 20 20 20 20 69 66 28 20 70 4c 69 73 74  .      if( pList
1a175 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
1a176 69 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72  i = pList->nExpr
1a177 3b 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74  ;.        struct
1a178 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
1a179 70 49 74 65 6d 20 3d 20 70 4c 69 73 74 2d 3e 61  pItem = pList->a
1a17a 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20  ;.        for(; 
1a17b 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b  i>0; i--, pItem+
1a17c 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
1a17d 28 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 20 29  ( pItem->pExpr )
1a17e 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 66   pItem->pExpr->f
1a17f 6c 61 67 73 20 7c 3d 20 45 50 5f 46 69 78 65 64  lags |= EP_Fixed
1a180 44 65 73 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Dest;.        }.
1a181 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
1a182 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
1a183 20 69 66 28 20 69 73 41 70 70 72 6f 70 72 69 61   if( isAppropria
1a184 74 65 46 6f 72 46 61 63 74 6f 72 69 6e 67 28 70  teForFactoring(p
1a185 45 78 70 72 29 20 29 7b 0a 20 20 20 20 69 6e 74  Expr) ){.    int
1a186 20 72 31 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e   r1 = ++pParse->
1a187 6e 4d 65 6d 3b 0a 20 20 20 20 69 6e 74 20 72 32  nMem;.    int r2
1a188 3b 0a 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74  ;.    r2 = sqlit
1a189 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74  e3ExprCodeTarget
1a18a 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20  (pParse, pExpr, 
1a18b 72 31 29 3b 0a 20 20 20 20 69 66 28 20 72 31 21  r1);.    if( r1!
1a18c 3d 72 32 20 29 20 73 71 6c 69 74 65 33 52 65 6c  =r2 ) sqlite3Rel
1a18d 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
1a18e 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 70 45 78  se, r1);.    pEx
1a18f 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 70 45  pr->iColumn = pE
1a190 78 70 72 2d 3e 6f 70 3b 0a 20 20 20 20 70 45 78  xpr->op;.    pEx
1a191 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45 47 49  pr->op = TK_REGI
1a192 53 54 45 52 3b 0a 20 20 20 20 70 45 78 70 72 2d  STER;.    pExpr-
1a193 3e 69 54 61 62 6c 65 20 3d 20 72 32 3b 0a 20 20  >iTable = r2;.  
1a194 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
1a195 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
1a196 2a 0a 2a 2a 20 50 72 65 65 76 61 6c 75 61 74 65  *.** Preevaluate
1a197 20 63 6f 6e 73 74 61 6e 74 20 73 75 62 65 78 70   constant subexp
1a198 72 65 73 73 69 6f 6e 73 20 77 69 74 68 69 6e 20  ressions within 
1a199 70 45 78 70 72 20 61 6e 64 20 73 74 6f 72 65 20  pExpr and store 
1a19a 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69  the.** results i
1a19b 6e 20 72 65 67 69 73 74 65 72 73 2e 20 20 4d 6f  n registers.  Mo
1a19c 64 69 66 79 20 70 45 78 70 72 20 73 6f 20 74 68  dify pExpr so th
1a19d 61 74 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20  at the constant 
1a19e 73 75 62 65 78 70 72 65 73 69 6f 6e 73 0a 2a 2a  subexpresions.**
1a19f 20 61 72 65 20 54 4b 5f 52 45 47 49 53 54 45 52   are TK_REGISTER
1a1a0 20 6f 70 63 6f 64 65 73 20 74 68 61 74 20 72 65   opcodes that re
1a1a1 66 65 72 20 74 6f 20 74 68 65 20 70 72 65 63 6f  fer to the preco
1a1a2 6d 70 75 74 65 64 20 76 61 6c 75 65 73 2e 0a 2a  mputed values..*
1a1a3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1a1a4 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
1a1a5 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28 50  rCodeConstants(P
1a1a6 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78  arse *pParse, Ex
1a1a7 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 20 77  pr *pExpr){.   w
1a1a8 61 6c 6b 45 78 70 72 54 72 65 65 28 70 45 78 70  alkExprTree(pExp
1a1a9 72 2c 20 65 76 61 6c 43 6f 6e 73 74 45 78 70 72  r, evalConstExpr
1a1aa 2c 20 70 50 61 72 73 65 29 3b 0a 7d 0a 0a 0a 2f  , pParse);.}.../
1a1ab 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
1a1ac 64 65 20 74 68 61 74 20 70 75 73 68 65 73 20 74  de that pushes t
1a1ad 68 65 20 76 61 6c 75 65 20 6f 66 20 65 76 65 72  he value of ever
1a1ae 79 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65  y element of the
1a1af 20 67 69 76 65 6e 0a 2a 2a 20 65 78 70 72 65 73   given.** expres
1a1b0 73 69 6f 6e 20 6c 69 73 74 20 69 6e 74 6f 20 61  sion list into a
1a1b1 20 73 65 71 75 65 6e 63 65 20 6f 66 20 72 65 67   sequence of reg
1a1b2 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69 6e 67  isters beginning
1a1b3 20 61 74 20 74 61 72 67 65 74 2e 0a 2a 2a 0a 2a   at target..**.*
1a1b4 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
1a1b5 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
1a1b6 65 76 61 6c 75 61 74 65 64 2e 0a 2a 2f 0a 53 51  evaluated..*/.SQ
1a1b7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1a1b8 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
1a1b9 45 78 70 72 4c 69 73 74 28 0a 20 20 50 61 72 73  ExprList(.  Pars
1a1ba 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f  e *pParse,     /
1a1bb 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
1a1bc 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  t */.  ExprList 
1a1bd 2a 70 4c 69 73 74 2c 20 20 20 2f 2a 20 54 68 65  *pList,   /* The
1a1be 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
1a1bf 20 74 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a   to be coded */.
1a1c0 20 20 69 6e 74 20 74 61 72 67 65 74 2c 20 20 20    int target,   
1a1c1 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f       /* Where to
1a1c2 20 77 72 69 74 65 20 72 65 73 75 6c 74 73 20 2a   write results *
1a1c3 2f 0a 20 20 69 6e 74 20 64 6f 48 61 72 64 43 6f  /.  int doHardCo
1a1c4 70 79 20 20 20 20 20 2f 2a 20 43 61 6c 6c 20 73  py     /* Call s
1a1c5 71 6c 69 74 65 33 45 78 70 72 48 61 72 64 43 6f  qlite3ExprHardCo
1a1c6 70 79 20 6f 6e 20 65 61 63 68 20 65 6c 65 6d 65  py on each eleme
1a1c7 6e 74 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b  nt if true */.){
1a1c8 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69  .  struct ExprLi
1a1c9 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
1a1ca 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 61 73    int i, n;.  as
1a1cb 73 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20 7c  sert( pList!=0 |
1a1cc 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61  | pParse->db->ma
1a1cd 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
1a1ce 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a  if( pList==0 ){.
1a1cf 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
1a1d0 7d 0a 20 20 61 73 73 65 72 74 28 20 74 61 72 67  }.  assert( targ
1a1d1 65 74 3e 30 20 29 3b 0a 20 20 6e 20 3d 20 70 4c  et>0 );.  n = pL
1a1d2 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 66 6f  ist->nExpr;.  fo
1a1d3 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61  r(pItem=pList->a
1a1d4 2c 20 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 2c  , i=0; i<n; i++,
1a1d5 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 73   pItem++){.    s
1a1d6 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
1a1d7 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70 45  Parse, pItem->pE
1a1d8 78 70 72 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a  xpr, target+i);.
1a1d9 20 20 20 20 69 66 28 20 64 6f 48 61 72 64 43 6f      if( doHardCo
1a1da 70 79 20 29 20 73 71 6c 69 74 65 33 45 78 70 72  py ) sqlite3Expr
1a1db 48 61 72 64 43 6f 70 79 28 70 50 61 72 73 65 2c  HardCopy(pParse,
1a1dc 20 74 61 72 67 65 74 2c 20 6e 29 3b 0a 20 20 7d   target, n);.  }
1a1dd 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a  .  return n;.}..
1a1de 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
1a1df 6f 64 65 20 66 6f 72 20 61 20 62 6f 6f 6c 65 61  ode for a boolea
1a1e0 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 73 75 63  n expression suc
1a1e1 68 20 74 68 61 74 20 61 20 6a 75 6d 70 20 69 73  h that a jump is
1a1e2 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 74 68 65 20   made.** to the 
1a1e3 6c 61 62 65 6c 20 22 64 65 73 74 22 20 69 66 20  label "dest" if 
1a1e4 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
1a1e5 73 20 74 72 75 65 20 62 75 74 20 65 78 65 63 75  s true but execu
1a1e6 74 69 6f 6e 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65  tion.** continue
1a1e7 73 20 73 74 72 61 69 67 68 74 20 74 68 72 75 20  s straight thru 
1a1e8 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  if the expressio
1a1e9 6e 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a  n is false..**.*
1a1ea 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73  * If the express
1a1eb 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 20 74 6f  ion evaluates to
1a1ec 20 4e 55 4c 4c 20 28 6e 65 69 74 68 65 72 20 74   NULL (neither t
1a1ed 72 75 65 20 6e 6f 72 20 66 61 6c 73 65 29 2c 20  rue nor false), 
1a1ee 74 68 65 6e 0a 2a 2a 20 74 61 6b 65 20 74 68 65  then.** take the
1a1ef 20 6a 75 6d 70 20 69 66 20 74 68 65 20 6a 75 6d   jump if the jum
1a1f0 70 49 66 4e 75 6c 6c 20 66 6c 61 67 20 69 73 20  pIfNull flag is 
1a1f1 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c  SQLITE_JUMPIFNUL
1a1f2 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 6f  L..**.** This co
1a1f3 64 65 20 64 65 70 65 6e 64 73 20 6f 6e 20 74 68  de depends on th
1a1f4 65 20 66 61 63 74 20 74 68 61 74 20 63 65 72 74  e fact that cert
1a1f5 61 69 6e 20 74 6f 6b 65 6e 20 76 61 6c 75 65 73  ain token values
1a1f6 20 28 65 78 3a 20 54 4b 5f 45 51 29 0a 2a 2a 20   (ex: TK_EQ).** 
1a1f7 61 72 65 20 74 68 65 20 73 61 6d 65 20 61 73 20  are the same as 
1a1f8 6f 70 63 6f 64 65 20 76 61 6c 75 65 73 20 28 65  opcode values (e
1a1f9 78 3a 20 4f 50 5f 45 71 29 20 74 68 61 74 20 69  x: OP_Eq) that i
1a1fa 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 63 6f 72  mplement the cor
1a1fb 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 6f 70  responding.** op
1a1fc 65 72 61 74 69 6f 6e 2e 20 20 53 70 65 63 69 61  eration.  Specia
1a1fd 6c 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 76 64  l comments in vd
1a1fe 62 65 2e 63 20 61 6e 64 20 74 68 65 20 6d 6b 6f  be.c and the mko
1a1ff 70 63 6f 64 65 68 2e 61 77 6b 20 73 63 72 69 70  pcodeh.awk scrip
1a200 74 20 69 6e 0a 2a 2a 20 74 68 65 20 6d 61 6b 65  t in.** the make
1a201 20 70 72 6f 63 65 73 73 20 63 61 75 73 65 20 74   process cause t
1a202 68 65 73 65 20 76 61 6c 75 65 73 20 74 6f 20 61  hese values to a
1a203 6c 69 67 6e 2e 20 20 41 73 73 65 72 74 28 29 73  lign.  Assert()s
1a204 20 69 6e 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20   in the code.** 
1a205 62 65 6c 6f 77 20 76 65 72 69 66 79 20 74 68 61  below verify tha
1a206 74 20 74 68 65 20 6e 75 6d 62 65 72 73 20 61 72  t the numbers ar
1a207 65 20 61 6c 69 67 6e 65 64 20 63 6f 72 72 65 63  e aligned correc
1a208 74 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  tly..*/.SQLITE_P
1a209 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1a20a 74 65 33 45 78 70 72 49 66 54 72 75 65 28 50 61  te3ExprIfTrue(Pa
1a20b 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70  rse *pParse, Exp
1a20c 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 64 65  r *pExpr, int de
1a20d 73 74 2c 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75  st, int jumpIfNu
1a20e 6c 6c 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  ll){.  Vdbe *v =
1a20f 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
1a210 20 20 69 6e 74 20 6f 70 20 3d 20 30 3b 0a 20 20    int op = 0;.  
1a211 69 6e 74 20 72 65 67 46 72 65 65 31 20 3d 20 30  int regFree1 = 0
1a212 3b 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65 32  ;.  int regFree2
1a213 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 31 2c 20   = 0;.  int r1, 
1a214 72 32 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6a  r2;..  assert( j
1a215 75 6d 70 49 66 4e 75 6c 6c 3d 3d 53 51 4c 49 54  umpIfNull==SQLIT
1a216 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 7c 7c 20  E_JUMPIFNULL || 
1a217 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b  jumpIfNull==0 );
1a218 0a 20 20 69 66 28 20 76 3d 3d 30 20 7c 7c 20 70  .  if( v==0 || p
1a219 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e  Expr==0 ) return
1a21a 3b 0a 20 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e  ;.  op = pExpr->
1a21b 6f 70 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70  op;.  switch( op
1a21c 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   ){.    case TK_
1a21d 41 4e 44 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  AND: {.      int
1a21e 20 64 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62   d2 = sqlite3Vdb
1a21f 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
1a220 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a       testcase( j
1a221 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a  umpIfNull==0 );.
1a222 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1a223 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43  pParse->disableC
1a224 6f 6c 43 61 63 68 65 3d 3d 30 20 29 3b 0a 20 20  olCache==0 );.  
1a225 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
1a226 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70  fFalse(pParse, p
1a227 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 32 2c  Expr->pLeft, d2,
1a228 6a 75 6d 70 49 66 4e 75 6c 6c 5e 53 51 4c 49 54  jumpIfNull^SQLIT
1a229 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20  E_JUMPIFNULL);. 
1a22a 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73       pParse->dis
1a22b 61 62 6c 65 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a  ableColCache++;.
1a22c 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
1a22d 72 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20  rIfTrue(pParse, 
1a22e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 64  pExpr->pRight, d
1a22f 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29  est, jumpIfNull)
1a230 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1a231 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43  pParse->disableC
1a232 6f 6c 43 61 63 68 65 3e 30 20 29 3b 0a 20 20 20  olCache>0 );.   
1a233 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62     pParse->disab
1a234 6c 65 43 6f 6c 43 61 63 68 65 2d 2d 3b 0a 20 20  leColCache--;.  
1a235 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
1a236 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64  esolveLabel(v, d
1a237 32 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  2);.      break;
1a238 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1a239 54 4b 5f 4f 52 3a 20 7b 0a 20 20 20 20 20 20 74  TK_OR: {.      t
1a23a 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e  estcase( jumpIfN
1a23b 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ull==0 );.      
1a23c 74 65 73 74 63 61 73 65 28 20 70 50 61 72 73 65  testcase( pParse
1a23d 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68  ->disableColCach
1a23e 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71  e==0 );.      sq
1a23f 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28  lite3ExprIfTrue(
1a240 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
1a241 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70  Left, dest, jump
1a242 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 70  IfNull);.      p
1a243 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f  Parse->disableCo
1a244 6c 43 61 63 68 65 2b 2b 3b 0a 20 20 20 20 20 20  lCache++;.      
1a245 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75  sqlite3ExprIfTru
1a246 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
1a247 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a  >pRight, dest, j
1a248 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20  umpIfNull);.    
1a249 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
1a24a 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68  ->disableColCach
1a24b 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 70 50 61  e>0 );.      pPa
1a24c 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43  rse->disableColC
1a24d 61 63 68 65 2d 2d 3b 0a 20 20 20 20 20 20 62 72  ache--;.      br
1a24e 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
1a24f 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20  ase TK_NOT: {.  
1a250 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75      testcase( ju
1a251 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20  mpIfNull==0 );. 
1a252 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1a253 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20  IfFalse(pParse, 
1a254 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65  pExpr->pLeft, de
1a255 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b  st, jumpIfNull);
1a256 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1a257 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
1a258 4c 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  LT:.    case TK_
1a259 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  LE:.    case TK_
1a25a 47 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  GT:.    case TK_
1a25b 47 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  GE:.    case TK_
1a25c 4e 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  NE:.    case TK_
1a25d 45 51 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65  EQ: {.      asse
1a25e 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c 74  rt( TK_LT==OP_Lt
1a25f 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
1a260 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20 29  ( TK_LE==OP_Le )
1a261 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1a262 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 3b 0a  TK_GT==OP_Gt );.
1a263 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
1a264 5f 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20  _GE==OP_Ge );.  
1a265 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 45      assert( TK_E
1a266 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 20 20  Q==OP_Eq );.    
1a267 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 45 3d    assert( TK_NE=
1a268 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20 20  =OP_Ne );.      
1a269 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
1a26a 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73  _LT );.      tes
1a26b 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 45  tcase( op==TK_LE
1a26c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1a26d 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 3b  se( op==TK_GT );
1a26e 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1a26f 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20 20   op==TK_GE );.  
1a270 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
1a271 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20 20 20  ==TK_EQ );.     
1a272 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
1a273 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20 74 65  K_NE );.      te
1a274 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75  stcase( jumpIfNu
1a275 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 63  ll==0 );.      c
1a276 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e  odeCompareOperan
1a277 64 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  ds(pParse, pExpr
1a278 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72  ->pLeft, &r1, &r
1a279 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20  egFree1,.       
1a27a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a27b 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
1a27c 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26  ->pRight, &r2, &
1a27d 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20  regFree2);.     
1a27e 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61   codeCompare(pPa
1a27f 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
1a280 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  t, pExpr->pRight
1a281 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 20  , op,.          
1a282 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20          r1, r2, 
1a283 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c  dest, jumpIfNull
1a284 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1a285 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29  e( regFree1==0 )
1a286 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1a287 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b  ( regFree2==0 );
1a288 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1a289 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
1a28a 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65  ISNULL:.    case
1a28b 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20   TK_NOTNULL: {. 
1a28c 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
1a28d 49 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 4e 75 6c  ISNULL==OP_IsNul
1a28e 6c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  l );.      asser
1a28f 74 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f  t( TK_NOTNULL==O
1a290 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 20  P_NotNull );.   
1a291 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
1a292 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20  =TK_ISNULL );.  
1a293 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
1a294 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a  ==TK_NOTNULL );.
1a295 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74        r1 = sqlit
1a296 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70  e3ExprCodeTemp(p
1a297 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
1a298 65 66 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b  eft, &regFree1);
1a299 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1a29a 62 65 41 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20  beAddOp2(v, op, 
1a29b 72 31 2c 20 64 65 73 74 29 3b 0a 20 20 20 20 20  r1, dest);.     
1a29c 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72   testcase( regFr
1a29d 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ee1==0 );.      
1a29e 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1a29f 20 63 61 73 65 20 54 4b 5f 42 45 54 57 45 45 4e   case TK_BETWEEN
1a2a0 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 20 20 20  : {.      /*    
1a2a1 78 20 42 45 54 57 45 45 4e 20 79 20 41 4e 44 20  x BETWEEN y AND 
1a2a2 7a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  z.      **.     
1a2a3 20 2a 2a 20 49 73 20 65 71 75 69 76 61 6c 65 6e   ** Is equivalen
1a2a4 74 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 0a 20  t to .      **. 
1a2a5 20 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d 79 20       **    x>=y 
1a2a6 41 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 20 20 2a  AND x<=z.      *
1a2a7 2a 0a 20 20 20 20 20 20 2a 2a 20 43 6f 64 65 20  *.      ** Code 
1a2a8 69 74 20 61 73 20 73 75 63 68 2c 20 74 61 6b 69  it as such, taki
1a2a9 6e 67 20 63 61 72 65 20 74 6f 20 64 6f 20 74 68  ng care to do th
1a2aa 65 20 63 6f 6d 6d 6f 6e 20 73 75 62 65 78 70 72  e common subexpr
1a2ab 65 73 73 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20  ession.      ** 
1a2ac 65 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  elementation of 
1a2ad 78 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  x..      */.    
1a2ae 20 20 45 78 70 72 20 65 78 70 72 41 6e 64 3b 0a    Expr exprAnd;.
1a2af 20 20 20 20 20 20 45 78 70 72 20 63 6f 6d 70 4c        Expr compL
1a2b0 65 66 74 3b 0a 20 20 20 20 20 20 45 78 70 72 20  eft;.      Expr 
1a2b1 63 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20 20 20  compRight;.     
1a2b2 20 45 78 70 72 20 65 78 70 72 58 3b 0a 0a 20 20   Expr exprX;..  
1a2b3 20 20 20 20 65 78 70 72 58 20 3d 20 2a 70 45 78      exprX = *pEx
1a2b4 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20  pr->pLeft;.     
1a2b5 20 65 78 70 72 41 6e 64 2e 6f 70 20 3d 20 54 4b   exprAnd.op = TK
1a2b6 5f 41 4e 44 3b 0a 20 20 20 20 20 20 65 78 70 72  _AND;.      expr
1a2b7 41 6e 64 2e 70 4c 65 66 74 20 3d 20 26 63 6f 6d  And.pLeft = &com
1a2b8 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 65 78 70  pLeft;.      exp
1a2b9 72 41 6e 64 2e 70 52 69 67 68 74 20 3d 20 26 63  rAnd.pRight = &c
1a2ba 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20  ompRight;.      
1a2bb 63 6f 6d 70 4c 65 66 74 2e 6f 70 20 3d 20 54 4b  compLeft.op = TK
1a2bc 5f 47 45 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c  _GE;.      compL
1a2bd 65 66 74 2e 70 4c 65 66 74 20 3d 20 26 65 78 70  eft.pLeft = &exp
1a2be 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65  rX;.      compLe
1a2bf 66 74 2e 70 52 69 67 68 74 20 3d 20 70 45 78 70  ft.pRight = pExp
1a2c0 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70  r->pList->a[0].p
1a2c1 45 78 70 72 3b 0a 20 20 20 20 20 20 63 6f 6d 70  Expr;.      comp
1a2c2 52 69 67 68 74 2e 6f 70 20 3d 20 54 4b 5f 4c 45  Right.op = TK_LE
1a2c3 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68  ;.      compRigh
1a2c4 74 2e 70 4c 65 66 74 20 3d 20 26 65 78 70 72 58  t.pLeft = &exprX
1a2c5 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68  ;.      compRigh
1a2c6 74 2e 70 52 69 67 68 74 20 3d 20 70 45 78 70 72  t.pRight = pExpr
1a2c7 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45  ->pList->a[1].pE
1a2c8 78 70 72 3b 0a 20 20 20 20 20 20 65 78 70 72 58  xpr;.      exprX
1a2c9 2e 69 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65  .iTable = sqlite
1a2ca 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
1a2cb 61 72 73 65 2c 20 26 65 78 70 72 58 2c 20 26 72  arse, &exprX, &r
1a2cc 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20  egFree1);.      
1a2cd 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
1a2ce 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 65  e1==0 );.      e
1a2cf 78 70 72 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 47  xprX.op = TK_REG
1a2d0 49 53 54 45 52 3b 0a 20 20 20 20 20 20 74 65 73  ISTER;.      tes
1a2d1 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c  tcase( jumpIfNul
1a2d2 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71  l==0 );.      sq
1a2d3 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28  lite3ExprIfTrue(
1a2d4 70 50 61 72 73 65 2c 20 26 65 78 70 72 41 6e 64  pParse, &exprAnd
1a2d5 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75  , dest, jumpIfNu
1a2d6 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ll);.      break
1a2d7 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
1a2d8 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 31 20  ult: {.      r1 
1a2d9 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
1a2da 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45  eTemp(pParse, pE
1a2db 78 70 72 2c 20 26 72 65 67 46 72 65 65 31 29 3b  xpr, &regFree1);
1a2dc 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1a2dd 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49  beAddOp3(v, OP_I
1a2de 66 2c 20 72 31 2c 20 64 65 73 74 2c 20 6a 75 6d  f, r1, dest, jum
1a2df 70 49 66 4e 75 6c 6c 21 3d 30 29 3b 0a 20 20 20  pIfNull!=0);.   
1a2e0 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
1a2e1 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20  Free1==0 );.    
1a2e2 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70    testcase( jump
1a2e3 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20  IfNull==0 );.   
1a2e4 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1a2e5 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 6c    }.  sqlite3Rel
1a2e6 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
1a2e7 73 65 2c 20 72 65 67 46 72 65 65 31 29 3b 0a 20  se, regFree1);. 
1a2e8 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
1a2e9 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
1a2ea 65 67 46 72 65 65 32 29 3b 20 20 0a 7d 0a 0a 2f  egFree2);  .}../
1a2eb 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
1a2ec 64 65 20 66 6f 72 20 61 20 62 6f 6f 6c 65 61 6e  de for a boolean
1a2ed 20 65 78 70 72 65 73 73 69 6f 6e 20 73 75 63 68   expression such
1a2ee 20 74 68 61 74 20 61 20 6a 75 6d 70 20 69 73 20   that a jump is 
1a2ef 6d 61 64 65 0a 2a 2a 20 74 6f 20 74 68 65 20 6c  made.** to the l
1a2f0 61 62 65 6c 20 22 64 65 73 74 22 20 69 66 20 74  abel "dest" if t
1a2f1 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
1a2f2 20 66 61 6c 73 65 20 62 75 74 20 65 78 65 63 75   false but execu
1a2f3 74 69 6f 6e 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65  tion.** continue
1a2f4 73 20 73 74 72 61 69 67 68 74 20 74 68 72 75 20  s straight thru 
1a2f5 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  if the expressio
1a2f6 6e 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a  n is true..**.**
1a2f7 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69   If the expressi
1a2f8 6f 6e 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20  on evaluates to 
1a2f9 4e 55 4c 4c 20 28 6e 65 69 74 68 65 72 20 74 72  NULL (neither tr
1a2fa 75 65 20 6e 6f 72 20 66 61 6c 73 65 29 20 74 68  ue nor false) th
1a2fb 65 6e 0a 2a 2a 20 6a 75 6d 70 20 69 66 20 6a 75  en.** jump if ju
1a2fc 6d 70 49 66 4e 75 6c 6c 20 69 73 20 53 51 4c 49  mpIfNull is SQLI
1a2fd 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 6f 72  TE_JUMPIFNULL or
1a2fe 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 66   fall through if
1a2ff 20 6a 75 6d 70 49 66 4e 75 6c 6c 0a 2a 2a 20 69   jumpIfNull.** i
1a300 73 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  s 0..*/.SQLITE_P
1a301 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1a302 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 50  te3ExprIfFalse(P
1a303 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78  arse *pParse, Ex
1a304 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 64  pr *pExpr, int d
1a305 65 73 74 2c 20 69 6e 74 20 6a 75 6d 70 49 66 4e  est, int jumpIfN
1a306 75 6c 6c 29 7b 0a 20 20 56 64 62 65 20 2a 76 20  ull){.  Vdbe *v 
1a307 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
1a308 0a 20 20 69 6e 74 20 6f 70 20 3d 20 30 3b 0a 20  .  int op = 0;. 
1a309 20 69 6e 74 20 72 65 67 46 72 65 65 31 20 3d 20   int regFree1 = 
1a30a 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65  0;.  int regFree
1a30b 32 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 31 2c  2 = 0;.  int r1,
1a30c 20 72 32 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   r2;..  assert( 
1a30d 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 53 51 4c 49  jumpIfNull==SQLI
1a30e 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 7c 7c  TE_JUMPIFNULL ||
1a30f 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29   jumpIfNull==0 )
1a310 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 7c 7c 20  ;.  if( v==0 || 
1a311 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72  pExpr==0 ) retur
1a312 6e 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 76 61 6c  n;..  /* The val
1a313 75 65 20 6f 66 20 70 45 78 70 72 2d 3e 6f 70 20  ue of pExpr->op 
1a314 61 6e 64 20 6f 70 20 61 72 65 20 72 65 6c 61 74  and op are relat
1a315 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20  ed as follows:. 
1a316 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 70   **.  **       p
1a317 45 78 70 72 2d 3e 6f 70 20 20 20 20 20 20 20 20  Expr->op        
1a318 20 20 20 20 6f 70 0a 20 20 2a 2a 20 20 20 20 20      op.  **     
1a319 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20    ---------     
1a31a 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a       ----------.
1a31b 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 49 53    **       TK_IS
1a31c 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 4f 50  NULL          OP
1a31d 5f 4e 6f 74 4e 75 6c 6c 0a 20 20 2a 2a 20 20 20  _NotNull.  **   
1a31e 20 20 20 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20      TK_NOTNULL  
1a31f 20 20 20 20 20 20 20 4f 50 5f 49 73 4e 75 6c 6c         OP_IsNull
1a320 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4e  .  **       TK_N
1a321 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f  E              O
1a322 50 5f 45 71 0a 20 20 2a 2a 20 20 20 20 20 20 20  P_Eq.  **       
1a323 54 4b 5f 45 51 20 20 20 20 20 20 20 20 20 20 20  TK_EQ           
1a324 20 20 20 4f 50 5f 4e 65 0a 20 20 2a 2a 20 20 20     OP_Ne.  **   
1a325 20 20 20 20 54 4b 5f 47 54 20 20 20 20 20 20 20      TK_GT       
1a326 20 20 20 20 20 20 20 4f 50 5f 4c 65 0a 20 20 2a         OP_Le.  *
1a327 2a 20 20 20 20 20 20 20 54 4b 5f 4c 45 20 20 20  *       TK_LE   
1a328 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 47 74             OP_Gt
1a329 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 47  .  **       TK_G
1a32a 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f  E              O
1a32b 50 5f 4c 74 0a 20 20 2a 2a 20 20 20 20 20 20 20  P_Lt.  **       
1a32c 54 4b 5f 4c 54 20 20 20 20 20 20 20 20 20 20 20  TK_LT           
1a32d 20 20 20 4f 50 5f 47 65 0a 20 20 2a 2a 0a 20 20     OP_Ge.  **.  
1a32e 2a 2a 20 46 6f 72 20 6f 74 68 65 72 20 76 61 6c  ** For other val
1a32f 75 65 73 20 6f 66 20 70 45 78 70 72 2d 3e 6f 70  ues of pExpr->op
1a330 2c 20 6f 70 20 69 73 20 75 6e 64 65 66 69 6e 65  , op is undefine
1a331 64 20 61 6e 64 20 75 6e 75 73 65 64 2e 0a 20 20  d and unused..  
1a332 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20  ** The value of 
1a333 54 4b 5f 20 61 6e 64 20 4f 50 5f 20 63 6f 6e 73  TK_ and OP_ cons
1a334 74 61 6e 74 73 20 61 72 65 20 61 72 72 61 6e 67  tants are arrang
1a335 65 64 20 73 75 63 68 20 74 68 61 74 20 77 65 0a  ed such that we.
1a336 20 20 2a 2a 20 63 61 6e 20 63 6f 6d 70 75 74 65    ** can compute
1a337 20 74 68 65 20 6d 61 70 70 69 6e 67 20 61 62 6f   the mapping abo
1a338 76 65 20 75 73 69 6e 67 20 74 68 65 20 66 6f 6c  ve using the fol
1a339 6c 6f 77 69 6e 67 20 65 78 70 72 65 73 73 69 6f  lowing expressio
1a33a 6e 2e 0a 20 20 2a 2a 20 41 73 73 65 72 74 28 29  n..  ** Assert()
1a33b 73 20 76 65 72 69 66 79 20 74 68 61 74 20 74 68  s verify that th
1a33c 65 20 63 6f 6d 70 75 74 61 74 69 6f 6e 20 69 73  e computation is
1a33d 20 63 6f 72 72 65 63 74 2e 0a 20 20 2a 2f 0a 20   correct..  */. 
1a33e 20 6f 70 20 3d 20 28 28 70 45 78 70 72 2d 3e 6f   op = ((pExpr->o
1a33f 70 2b 28 54 4b 5f 49 53 4e 55 4c 4c 26 31 29 29  p+(TK_ISNULL&1))
1a340 5e 31 29 2d 28 54 4b 5f 49 53 4e 55 4c 4c 26 31  ^1)-(TK_ISNULL&1
1a341 29 3b 0a 0a 20 20 2f 2a 20 56 65 72 69 66 79 20  );..  /* Verify 
1a342 63 6f 72 72 65 63 74 20 61 6c 69 67 6e 6d 65 6e  correct alignmen
1a343 74 20 6f 66 20 54 4b 5f 20 61 6e 64 20 4f 50 5f  t of TK_ and OP_
1a344 20 63 6f 6e 73 74 61 6e 74 73 0a 20 20 2a 2f 0a   constants.  */.
1a345 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
1a346 3e 6f 70 21 3d 54 4b 5f 49 53 4e 55 4c 4c 20 7c  >op!=TK_ISNULL |
1a347 7c 20 6f 70 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c  | op==OP_NotNull
1a348 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45   );.  assert( pE
1a349 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4e 4f 54 4e  xpr->op!=TK_NOTN
1a34a 55 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 49 73  ULL || op==OP_Is
1a34b 4e 75 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 74  Null );.  assert
1a34c 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f  ( pExpr->op!=TK_
1a34d 4e 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 45 71 20  NE || op==OP_Eq 
1a34e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78  );.  assert( pEx
1a34f 70 72 2d 3e 6f 70 21 3d 54 4b 5f 45 51 20 7c 7c  pr->op!=TK_EQ ||
1a350 20 6f 70 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20   op==OP_Ne );.  
1a351 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f  assert( pExpr->o
1a352 70 21 3d 54 4b 5f 4c 54 20 7c 7c 20 6f 70 3d 3d  p!=TK_LT || op==
1a353 4f 50 5f 47 65 20 29 3b 0a 20 20 61 73 73 65 72  OP_Ge );.  asser
1a354 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b  t( pExpr->op!=TK
1a355 5f 4c 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 47 74  _LE || op==OP_Gt
1a356 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45   );.  assert( pE
1a357 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 47 54 20 7c  xpr->op!=TK_GT |
1a358 7c 20 6f 70 3d 3d 4f 50 5f 4c 65 20 29 3b 0a 20  | op==OP_Le );. 
1a359 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
1a35a 6f 70 21 3d 54 4b 5f 47 45 20 7c 7c 20 6f 70 3d  op!=TK_GE || op=
1a35b 3d 4f 50 5f 4c 74 20 29 3b 0a 0a 20 20 73 77 69  =OP_Lt );..  swi
1a35c 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29  tch( pExpr->op )
1a35d 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4e  {.    case TK_AN
1a35e 44 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63  D: {.      testc
1a35f 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d  ase( jumpIfNull=
1a360 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  =0 );.      test
1a361 63 61 73 65 28 20 70 50 61 72 73 65 2d 3e 64 69  case( pParse->di
1a362 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3d 3d 30  sableColCache==0
1a363 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
1a364 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61  3ExprIfFalse(pPa
1a365 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
1a366 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e  t, dest, jumpIfN
1a367 75 6c 6c 29 3b 0a 20 20 20 20 20 20 70 50 61 72  ull);.      pPar
1a368 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61  se->disableColCa
1a369 63 68 65 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c  che++;.      sql
1a36a 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
1a36b 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
1a36c 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d  Right, dest, jum
1a36d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20  pIfNull);.      
1a36e 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
1a36f 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3e  disableColCache>
1a370 30 20 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73  0 );.      pPars
1a371 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63  e->disableColCac
1a372 68 65 2d 2d 3b 0a 20 20 20 20 20 20 62 72 65 61  he--;.      brea
1a373 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
1a374 65 20 54 4b 5f 4f 52 3a 20 7b 0a 20 20 20 20 20  e TK_OR: {.     
1a375 20 69 6e 74 20 64 32 20 3d 20 73 71 6c 69 74 65   int d2 = sqlite
1a376 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
1a377 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1a378 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30  e( jumpIfNull==0
1a379 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1a37a 73 65 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61  se( pParse->disa
1a37b 62 6c 65 43 6f 6c 43 61 63 68 65 3d 3d 30 20 29  bleColCache==0 )
1a37c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
1a37d 78 70 72 49 66 54 72 75 65 28 70 50 61 72 73 65  xprIfTrue(pParse
1a37e 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
1a37f 64 32 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 5e 53  d2, jumpIfNull^S
1a380 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
1a381 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  );.      pParse-
1a382 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65  >disableColCache
1a383 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ++;.      sqlite
1a384 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61  3ExprIfFalse(pPa
1a385 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67  rse, pExpr->pRig
1a386 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66  ht, dest, jumpIf
1a387 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 61 73 73  Null);.      ass
1a388 65 72 74 28 20 70 50 61 72 73 65 2d 3e 64 69 73  ert( pParse->dis
1a389 61 62 6c 65 43 6f 6c 43 61 63 68 65 3e 30 20 29  ableColCache>0 )
1a38a 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
1a38b 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 2d  disableColCache-
1a38c 2d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  -;.      sqlite3
1a38d 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
1a38e 28 76 2c 20 64 32 29 3b 0a 20 20 20 20 20 20 62  (v, d2);.      b
1a38f 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1a390 63 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20  case TK_NOT: {. 
1a391 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1a392 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 70  IfTrue(pParse, p
1a393 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73  Expr->pLeft, des
1a394 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a  t, jumpIfNull);.
1a395 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1a396 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c   }.    case TK_L
1a397 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c  T:.    case TK_L
1a398 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47  E:.    case TK_G
1a399 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47  T:.    case TK_G
1a39a 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e  E:.    case TK_N
1a39b 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45  E:.    case TK_E
1a39c 51 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63  Q: {.      testc
1a39d 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29  ase( op==TK_LT )
1a39e 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1a39f 28 20 6f 70 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20  ( op==TK_LE );. 
1a3a0 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
1a3a1 70 3d 3d 54 4b 5f 47 54 20 29 3b 0a 20 20 20 20  p==TK_GT );.    
1a3a2 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
1a3a3 54 4b 5f 47 45 20 29 3b 0a 20 20 20 20 20 20 74  TK_GE );.      t
1a3a4 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
1a3a5 45 51 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  EQ );.      test
1a3a6 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20  case( op==TK_NE 
1a3a7 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1a3a8 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30  e( jumpIfNull==0
1a3a9 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f   );.      codeCo
1a3aa 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50  mpareOperands(pP
1a3ab 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
1a3ac 66 74 2c 20 26 72 31 2c 20 26 72 65 67 46 72 65  ft, &r1, &regFre
1a3ad 65 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  e1,.            
1a3ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a3af 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69        pExpr->pRi
1a3b0 67 68 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72  ght, &r2, &regFr
1a3b1 65 65 32 29 3b 0a 20 20 20 20 20 20 63 6f 64 65  ee2);.      code
1a3b2 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20  Compare(pParse, 
1a3b3 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45  pExpr->pLeft, pE
1a3b4 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c  xpr->pRight, op,
1a3b5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1a3b6 20 20 20 72 31 2c 20 72 32 2c 20 64 65 73 74 2c     r1, r2, dest,
1a3b7 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20   jumpIfNull);.  
1a3b8 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65      testcase( re
1a3b9 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20  gFree1==0 );.   
1a3ba 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
1a3bb 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20  Free2==0 );.    
1a3bc 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1a3bd 20 20 20 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c     case TK_ISNUL
1a3be 4c 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e  L:.    case TK_N
1a3bf 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20  OTNULL: {.      
1a3c0 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
1a3c1 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20  _ISNULL );.     
1a3c2 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
1a3c3 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20 20 20  K_NOTNULL );.   
1a3c4 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45     r1 = sqlite3E
1a3c5 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72  xprCodeTemp(pPar
1a3c6 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
1a3c7 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20  , &regFree1);.  
1a3c8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1a3c9 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72 31 2c  ddOp2(v, op, r1,
1a3ca 20 64 65 73 74 29 3b 0a 20 20 20 20 20 20 74 65   dest);.      te
1a3cb 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31  stcase( regFree1
1a3cc 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65  ==0 );.      bre
1a3cd 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
1a3ce 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a 20 7b  se TK_BETWEEN: {
1a3cf 0a 20 20 20 20 20 20 2f 2a 20 20 20 20 78 20 42  .      /*    x B
1a3d0 45 54 57 45 45 4e 20 79 20 41 4e 44 20 7a 0a 20  ETWEEN y AND z. 
1a3d1 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
1a3d2 20 49 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74   Is equivalent t
1a3d3 6f 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  o .      **.    
1a3d4 20 20 2a 2a 20 20 20 20 78 3e 3d 79 20 41 4e 44    **    x>=y AND
1a3d5 20 78 3c 3d 7a 0a 20 20 20 20 20 20 2a 2a 0a 20   x<=z.      **. 
1a3d6 20 20 20 20 20 2a 2a 20 43 6f 64 65 20 69 74 20       ** Code it 
1a3d7 61 73 20 73 75 63 68 2c 20 74 61 6b 69 6e 67 20  as such, taking 
1a3d8 63 61 72 65 20 74 6f 20 64 6f 20 74 68 65 20 63  care to do the c
1a3d9 6f 6d 6d 6f 6e 20 73 75 62 65 78 70 72 65 73 73  ommon subexpress
1a3da 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 65 6c 65  ion.      ** ele
1a3db 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78 2e 0a  mentation of x..
1a3dc 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 45        */.      E
1a3dd 78 70 72 20 65 78 70 72 41 6e 64 3b 0a 20 20 20  xpr exprAnd;.   
1a3de 20 20 20 45 78 70 72 20 63 6f 6d 70 4c 65 66 74     Expr compLeft
1a3df 3b 0a 20 20 20 20 20 20 45 78 70 72 20 63 6f 6d  ;.      Expr com
1a3e0 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 45 78  pRight;.      Ex
1a3e1 70 72 20 65 78 70 72 58 3b 0a 0a 20 20 20 20 20  pr exprX;..     
1a3e2 20 65 78 70 72 58 20 3d 20 2a 70 45 78 70 72 2d   exprX = *pExpr-
1a3e3 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 65 78  >pLeft;.      ex
1a3e4 70 72 41 6e 64 2e 6f 70 20 3d 20 54 4b 5f 41 4e  prAnd.op = TK_AN
1a3e5 44 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e 64  D;.      exprAnd
1a3e6 2e 70 4c 65 66 74 20 3d 20 26 63 6f 6d 70 4c 65  .pLeft = &compLe
1a3e7 66 74 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e  ft;.      exprAn
1a3e8 64 2e 70 52 69 67 68 74 20 3d 20 26 63 6f 6d 70  d.pRight = &comp
1a3e9 52 69 67 68 74 3b 0a 20 20 20 20 20 20 63 6f 6d  Right;.      com
1a3ea 70 4c 65 66 74 2e 6f 70 20 3d 20 54 4b 5f 47 45  pLeft.op = TK_GE
1a3eb 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74  ;.      compLeft
1a3ec 2e 70 4c 65 66 74 20 3d 20 26 65 78 70 72 58 3b  .pLeft = &exprX;
1a3ed 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e  .      compLeft.
1a3ee 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e  pRight = pExpr->
1a3ef 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70  pList->a[0].pExp
1a3f0 72 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 67  r;.      compRig
1a3f1 68 74 2e 6f 70 20 3d 20 54 4b 5f 4c 45 3b 0a 20  ht.op = TK_LE;. 
1a3f2 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74 2e 70       compRight.p
1a3f3 4c 65 66 74 20 3d 20 26 65 78 70 72 58 3b 0a 20  Left = &exprX;. 
1a3f4 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74 2e 70       compRight.p
1a3f5 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 70  Right = pExpr->p
1a3f6 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72  List->a[1].pExpr
1a3f7 3b 0a 20 20 20 20 20 20 65 78 70 72 58 2e 69 54  ;.      exprX.iT
1a3f8 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 45 78  able = sqlite3Ex
1a3f9 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
1a3fa 65 2c 20 26 65 78 70 72 58 2c 20 26 72 65 67 46  e, &exprX, &regF
1a3fb 72 65 65 31 29 3b 0a 20 20 20 20 20 20 74 65 73  ree1);.      tes
1a3fc 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d  tcase( regFree1=
1a3fd 3d 30 20 29 3b 0a 20 20 20 20 20 20 65 78 70 72  =0 );.      expr
1a3fe 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 54  X.op = TK_REGIST
1a3ff 45 52 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  ER;.      testca
1a400 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d  se( jumpIfNull==
1a401 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 );.      sqlit
1a402 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50  e3ExprIfFalse(pP
1a403 61 72 73 65 2c 20 26 65 78 70 72 41 6e 64 2c 20  arse, &exprAnd, 
1a404 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c  dest, jumpIfNull
1a405 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
1a406 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
1a407 74 3a 20 7b 0a 20 20 20 20 20 20 72 31 20 3d 20  t: {.      r1 = 
1a408 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
1a409 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70  emp(pParse, pExp
1a40a 72 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20  r, &regFree1);. 
1a40b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1a40c 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 66 4e  AddOp3(v, OP_IfN
1a40d 6f 74 2c 20 72 31 2c 20 64 65 73 74 2c 20 6a 75  ot, r1, dest, ju
1a40e 6d 70 49 66 4e 75 6c 6c 21 3d 30 29 3b 0a 20 20  mpIfNull!=0);.  
1a40f 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65      testcase( re
1a410 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20  gFree1==0 );.   
1a411 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d     testcase( jum
1a412 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20  pIfNull==0 );.  
1a413 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1a414 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65  .  }.  sqlite3Re
1a415 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
1a416 72 73 65 2c 20 72 65 67 46 72 65 65 31 29 3b 0a  rse, regFree1);.
1a417 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
1a418 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
1a419 72 65 67 46 72 65 65 32 29 3b 0a 7d 0a 0a 2f 2a  regFree2);.}../*
1a41a 0a 2a 2a 20 44 6f 20 61 20 64 65 65 70 20 63 6f  .** Do a deep co
1a41b 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74 77 6f 20  mparison of two 
1a41c 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 73  expression trees
1a41d 2e 20 20 52 65 74 75 72 6e 20 54 52 55 45 20 28  .  Return TRUE (
1a41e 6e 6f 6e 2d 7a 65 72 6f 29 0a 2a 2a 20 69 66 20  non-zero).** if 
1a41f 74 68 65 79 20 61 72 65 20 69 64 65 6e 74 69 63  they are identic
1a420 61 6c 20 61 6e 64 20 72 65 74 75 72 6e 20 46 41  al and return FA
1a421 4c 53 45 20 69 66 20 74 68 65 79 20 64 69 66 66  LSE if they diff
1a422 65 72 20 69 6e 20 61 6e 79 20 77 61 79 2e 0a 2a  er in any way..*
1a423 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 74  *.** Sometimes t
1a424 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
1a425 20 72 65 74 75 72 6e 20 46 41 4c 53 45 20 65 76   return FALSE ev
1a426 65 6e 20 69 66 20 74 68 65 20 74 77 6f 20 65 78  en if the two ex
1a427 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 72 65 61  pressions.** rea
1a428 6c 6c 79 20 61 72 65 20 65 71 75 69 76 61 6c 65  lly are equivale
1a429 6e 74 2e 20 20 49 66 20 77 65 20 63 61 6e 6e 6f  nt.  If we canno
1a42a 74 20 70 72 6f 76 65 20 74 68 61 74 20 74 68 65  t prove that the
1a42b 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65   expressions are
1a42c 0a 2a 2a 20 69 64 65 6e 74 69 63 61 6c 2c 20 77  .** identical, w
1a42d 65 20 72 65 74 75 72 6e 20 46 41 4c 53 45 20 6a  e return FALSE j
1a42e 75 73 74 20 74 6f 20 62 65 20 73 61 66 65 2e 20  ust to be safe. 
1a42f 20 53 6f 20 69 66 20 74 68 69 73 20 72 6f 75 74   So if this rout
1a430 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73 20 66  ine.** returns f
1a431 61 6c 73 65 2c 20 74 68 65 6e 20 79 6f 75 20 64  alse, then you d
1a432 6f 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6b 6e 6f  o not really kno
1a433 77 20 66 6f 72 20 63 65 72 74 61 69 6e 20 69 66  w for certain if
1a434 20 74 68 65 20 74 77 6f 0a 2a 2a 20 65 78 70 72   the two.** expr
1a435 65 73 73 69 6f 6e 73 20 61 72 65 20 74 68 65 20  essions are the 
1a436 73 61 6d 65 2e 20 20 42 75 74 20 69 66 20 79 6f  same.  But if yo
1a437 75 20 67 65 74 20 61 20 54 52 55 45 20 72 65 74  u get a TRUE ret
1a438 75 72 6e 2c 20 74 68 65 6e 20 79 6f 75 0a 2a 2a  urn, then you.**
1a439 20 63 61 6e 20 62 65 20 73 75 72 65 20 74 68 65   can be sure the
1a43a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65   expressions are
1a43b 20 74 68 65 20 73 61 6d 65 2e 20 20 49 6e 20 74   the same.  In t
1a43c 68 65 20 70 6c 61 63 65 73 20 77 68 65 72 65 0a  he places where.
1a43d 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ** this routine 
1a43e 69 73 20 75 73 65 64 2c 20 69 74 20 64 6f 65 73  is used, it does
1a43f 20 6e 6f 74 20 68 75 72 74 20 74 6f 20 67 65 74   not hurt to get
1a440 20 61 6e 20 65 78 74 72 61 20 46 41 4c 53 45 20   an extra FALSE 
1a441 2d 20 74 68 61 74 0a 2a 2a 20 6a 75 73 74 20 6d  - that.** just m
1a442 69 67 68 74 20 72 65 73 75 6c 74 20 69 6e 20 73  ight result in s
1a443 6f 6d 65 20 73 6c 69 67 68 74 6c 79 20 73 6c 6f  ome slightly slo
1a444 77 65 72 20 63 6f 64 65 2e 20 20 42 75 74 20 72  wer code.  But r
1a445 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 61 6e 20 69  eturning.** an i
1a446 6e 63 6f 72 72 65 63 74 20 54 52 55 45 20 63 6f  ncorrect TRUE co
1a447 75 6c 64 20 6c 65 61 64 20 74 6f 20 61 20 6d 61  uld lead to a ma
1a448 6c 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51  lfunction..*/.SQ
1a449 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1a44a 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70   sqlite3ExprComp
1a44b 61 72 65 28 45 78 70 72 20 2a 70 41 2c 20 45 78  are(Expr *pA, Ex
1a44c 70 72 20 2a 70 42 29 7b 0a 20 20 69 6e 74 20 69  pr *pB){.  int i
1a44d 3b 0a 20 20 69 66 28 20 70 41 3d 3d 30 7c 7c 70  ;.  if( pA==0||p
1a44e 42 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  B==0 ){.    retu
1a44f 72 6e 20 70 42 3d 3d 70 41 3b 0a 20 20 7d 0a 20  rn pB==pA;.  }. 
1a450 20 69 66 28 20 70 41 2d 3e 6f 70 21 3d 70 42 2d   if( pA->op!=pB-
1a451 3e 6f 70 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  >op ) return 0;.
1a452 20 20 69 66 28 20 28 70 41 2d 3e 66 6c 61 67 73    if( (pA->flags
1a453 20 26 20 45 50 5f 44 69 73 74 69 6e 63 74 29 21   & EP_Distinct)!
1a454 3d 28 70 42 2d 3e 66 6c 61 67 73 20 26 20 45 50  =(pB->flags & EP
1a455 5f 44 69 73 74 69 6e 63 74 29 20 29 20 72 65 74  _Distinct) ) ret
1a456 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 21 73 71  urn 0;.  if( !sq
1a457 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65  lite3ExprCompare
1a458 28 70 41 2d 3e 70 4c 65 66 74 2c 20 70 42 2d 3e  (pA->pLeft, pB->
1a459 70 4c 65 66 74 29 20 29 20 72 65 74 75 72 6e 20  pLeft) ) return 
1a45a 30 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65  0;.  if( !sqlite
1a45b 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70 41 2d  3ExprCompare(pA-
1a45c 3e 70 52 69 67 68 74 2c 20 70 42 2d 3e 70 52 69  >pRight, pB->pRi
1a45d 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 30 3b  ght) ) return 0;
1a45e 0a 20 20 69 66 28 20 70 41 2d 3e 70 4c 69 73 74  .  if( pA->pList
1a45f 20 29 7b 0a 20 20 20 20 69 66 28 20 70 42 2d 3e   ){.    if( pB->
1a460 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72  pList==0 ) retur
1a461 6e 20 30 3b 0a 20 20 20 20 69 66 28 20 70 41 2d  n 0;.    if( pA-
1a462 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 70  >pList->nExpr!=p
1a463 42 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20  B->pList->nExpr 
1a464 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
1a465 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 2d 3e 70  for(i=0; i<pA->p
1a466 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  List->nExpr; i++
1a467 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 73 71  ){.      if( !sq
1a468 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65  lite3ExprCompare
1a469 28 70 41 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 5d  (pA->pList->a[i]
1a46a 2e 70 45 78 70 72 2c 20 70 42 2d 3e 70 4c 69 73  .pExpr, pB->pLis
1a46b 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20 29  t->a[i].pExpr) )
1a46c 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
1a46d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
1a46e 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42  }.  }else if( pB
1a46f 2d 3e 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 72  ->pList ){.    r
1a470 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69  eturn 0;.  }.  i
1a471 66 28 20 70 41 2d 3e 70 53 65 6c 65 63 74 20 7c  f( pA->pSelect |
1a472 7c 20 70 42 2d 3e 70 53 65 6c 65 63 74 20 29 20  | pB->pSelect ) 
1a473 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
1a474 70 41 2d 3e 69 54 61 62 6c 65 21 3d 70 42 2d 3e  pA->iTable!=pB->
1a475 69 54 61 62 6c 65 20 7c 7c 20 70 41 2d 3e 69 43  iTable || pA->iC
1a476 6f 6c 75 6d 6e 21 3d 70 42 2d 3e 69 43 6f 6c 75  olumn!=pB->iColu
1a477 6d 6e 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  mn ) return 0;. 
1a478 20 69 66 28 20 70 41 2d 3e 6f 70 21 3d 54 4b 5f   if( pA->op!=TK_
1a479 43 4f 4c 55 4d 4e 20 26 26 20 70 41 2d 3e 74 6f  COLUMN && pA->to
1a47a 6b 65 6e 2e 7a 20 29 7b 0a 20 20 20 20 69 66 28  ken.z ){.    if(
1a47b 20 70 42 2d 3e 74 6f 6b 65 6e 2e 7a 3d 3d 30 20   pB->token.z==0 
1a47c 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
1a47d 69 66 28 20 70 42 2d 3e 74 6f 6b 65 6e 2e 6e 21  if( pB->token.n!
1a47e 3d 70 41 2d 3e 74 6f 6b 65 6e 2e 6e 20 29 20 72  =pA->token.n ) r
1a47f 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 69 66 28  eturn 0;.    if(
1a480 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
1a481 28 28 63 68 61 72 2a 29 70 41 2d 3e 74 6f 6b 65  ((char*)pA->toke
1a482 6e 2e 7a 2c 28 63 68 61 72 2a 29 70 42 2d 3e 74  n.z,(char*)pB->t
1a483 6f 6b 65 6e 2e 7a 2c 70 42 2d 3e 74 6f 6b 65 6e  oken.z,pB->token
1a484 2e 6e 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  .n)!=0 ){.      
1a485 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a  return 0;.    }.
1a486 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a    }.  return 1;.
1a487 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20  }.../*.** Add a 
1a488 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74  new element to t
1a489 68 65 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f  he pAggInfo->aCo
1a48a 6c 5b 5d 20 61 72 72 61 79 2e 20 20 52 65 74 75  l[] array.  Retu
1a48b 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 0a  rn the index of.
1a48c 2a 2a 20 74 68 65 20 6e 65 77 20 65 6c 65 6d 65  ** the new eleme
1a48d 6e 74 2e 20 20 52 65 74 75 72 6e 20 61 20 6e 65  nt.  Return a ne
1a48e 67 61 74 69 76 65 20 6e 75 6d 62 65 72 20 69 66  gative number if
1a48f 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a   malloc fails..*
1a490 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 64 64  /.static int add
1a491 41 67 67 49 6e 66 6f 43 6f 6c 75 6d 6e 28 73 71  AggInfoColumn(sq
1a492 6c 69 74 65 33 20 2a 64 62 2c 20 41 67 67 49 6e  lite3 *db, AggIn
1a493 66 6f 20 2a 70 49 6e 66 6f 29 7b 0a 20 20 69 6e  fo *pInfo){.  in
1a494 74 20 69 3b 0a 20 20 70 49 6e 66 6f 2d 3e 61 43  t i;.  pInfo->aC
1a495 6f 6c 20 3d 20 73 71 6c 69 74 65 33 41 72 72 61  ol = sqlite3Arra
1a496 79 41 6c 6c 6f 63 61 74 65 28 0a 20 20 20 20 20  yAllocate(.     
1a497 20 20 64 62 2c 0a 20 20 20 20 20 20 20 70 49 6e    db,.       pIn
1a498 66 6f 2d 3e 61 43 6f 6c 2c 0a 20 20 20 20 20 20  fo->aCol,.      
1a499 20 73 69 7a 65 6f 66 28 70 49 6e 66 6f 2d 3e 61   sizeof(pInfo->a
1a49a 43 6f 6c 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20  Col[0]),.       
1a49b 33 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66 6f  3,.       &pInfo
1a49c 2d 3e 6e 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20  ->nColumn,.     
1a49d 20 20 26 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d    &pInfo->nColum
1a49e 6e 41 6c 6c 6f 63 2c 0a 20 20 20 20 20 20 20 26  nAlloc,.       &
1a49f 69 0a 20 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  i.  );.  return 
1a4a0 69 3b 0a 7d 20 20 20 20 0a 0a 2f 2a 0a 2a 2a 20  i;.}    ../*.** 
1a4a1 41 64 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e  Add a new elemen
1a4a2 74 20 74 6f 20 74 68 65 20 70 41 67 67 49 6e 66  t to the pAggInf
1a4a3 6f 2d 3e 61 46 75 6e 63 5b 5d 20 61 72 72 61 79  o->aFunc[] array
1a4a4 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e  .  Return the in
1a4a5 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65  dex of.** the ne
1a4a6 77 20 65 6c 65 6d 65 6e 74 2e 20 20 52 65 74 75  w element.  Retu
1a4a7 72 6e 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75  rn a negative nu
1a4a8 6d 62 65 72 20 69 66 20 6d 61 6c 6c 6f 63 20 66  mber if malloc f
1a4a9 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ails..*/.static 
1a4aa 69 6e 74 20 61 64 64 41 67 67 49 6e 66 6f 46 75  int addAggInfoFu
1a4ab 6e 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  nc(sqlite3 *db, 
1a4ac 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b  AggInfo *pInfo){
1a4ad 0a 20 20 69 6e 74 20 69 3b 0a 20 20 70 49 6e 66  .  int i;.  pInf
1a4ae 6f 2d 3e 61 46 75 6e 63 20 3d 20 73 71 6c 69 74  o->aFunc = sqlit
1a4af 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74 65 28  e3ArrayAllocate(
1a4b0 0a 20 20 20 20 20 20 20 64 62 2c 20 0a 20 20 20  .       db, .   
1a4b1 20 20 20 20 70 49 6e 66 6f 2d 3e 61 46 75 6e 63      pInfo->aFunc
1a4b2 2c 0a 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28  ,.       sizeof(
1a4b3 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 30 5d 29  pInfo->aFunc[0])
1a4b4 2c 0a 20 20 20 20 20 20 20 33 2c 0a 20 20 20 20  ,.       3,.    
1a4b5 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 46 75 6e 63     &pInfo->nFunc
1a4b6 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66 6f 2d  ,.       &pInfo-
1a4b7 3e 6e 46 75 6e 63 41 6c 6c 6f 63 2c 0a 20 20 20  >nFuncAlloc,.   
1a4b8 20 20 20 20 26 69 0a 20 20 29 3b 0a 20 20 72 65      &i.  );.  re
1a4b9 74 75 72 6e 20 69 3b 0a 7d 20 20 20 20 0a 0a 2f  turn i;.}    ../
1a4ba 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20  *.** This is an 
1a4bb 78 46 75 6e 63 20 66 6f 72 20 77 61 6c 6b 45 78  xFunc for walkEx
1a4bc 70 72 54 72 65 65 28 29 20 75 73 65 64 20 74 6f  prTree() used to
1a4bd 20 69 6d 70 6c 65 6d 65 6e 74 20 0a 2a 2a 20 73   implement .** s
1a4be 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a  qlite3ExprAnalyz
1a4bf 65 41 67 67 72 65 67 61 74 65 73 28 29 2e 20 20  eAggregates().  
1a4c0 53 65 65 20 73 71 6c 69 74 65 33 45 78 70 72 41  See sqlite3ExprA
1a4c1 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73  nalyzeAggregates
1a4c2 0a 2a 2a 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  .** for addition
1a4c3 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  al information..
1a4c4 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
1a4c5 6e 65 20 61 6e 61 6c 79 7a 65 73 20 74 68 65 20  ne analyzes the 
1a4c6 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
1a4c7 6f 6e 20 61 74 20 70 45 78 70 72 2e 0a 2a 2f 0a  on at pExpr..*/.
1a4c8 73 74 61 74 69 63 20 69 6e 74 20 61 6e 61 6c 79  static int analy
1a4c9 7a 65 41 67 67 72 65 67 61 74 65 28 76 6f 69 64  zeAggregate(void
1a4ca 20 2a 70 41 72 67 2c 20 45 78 70 72 20 2a 70 45   *pArg, Expr *pE
1a4cb 78 70 72 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  xpr){.  int i;. 
1a4cc 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e   NameContext *pN
1a4cd 43 20 3d 20 28 4e 61 6d 65 43 6f 6e 74 65 78 74  C = (NameContext
1a4ce 20 2a 29 70 41 72 67 3b 0a 20 20 50 61 72 73 65   *)pArg;.  Parse
1a4cf 20 2a 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e   *pParse = pNC->
1a4d0 70 50 61 72 73 65 3b 0a 20 20 53 72 63 4c 69 73  pParse;.  SrcLis
1a4d1 74 20 2a 70 53 72 63 4c 69 73 74 20 3d 20 70 4e  t *pSrcList = pN
1a4d2 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 20 20 41  C->pSrcList;.  A
1a4d3 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f  ggInfo *pAggInfo
1a4d4 20 3d 20 70 4e 43 2d 3e 70 41 67 67 49 6e 66 6f   = pNC->pAggInfo
1a4d5 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 78  ;..  switch( pEx
1a4d6 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61  pr->op ){.    ca
1a4d7 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  se TK_AGG_COLUMN
1a4d8 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f  :.    case TK_CO
1a4d9 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a  LUMN: {.      /*
1a4da 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
1a4db 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 69   the column is i
1a4dc 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 61 62  n one of the tab
1a4dd 6c 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 0a  les in the FROM.
1a4de 20 20 20 20 20 20 2a 2a 20 63 6c 61 75 73 65 20        ** clause 
1a4df 6f 66 20 74 68 65 20 61 67 67 72 65 67 61 74 65  of the aggregate
1a4e0 20 71 75 65 72 79 20 2a 2f 0a 20 20 20 20 20 20   query */.      
1a4e1 69 66 28 20 70 53 72 63 4c 69 73 74 20 29 7b 0a  if( pSrcList ){.
1a4e2 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20 53          struct S
1a4e3 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  rcList_item *pIt
1a4e4 65 6d 20 3d 20 70 53 72 63 4c 69 73 74 2d 3e 61  em = pSrcList->a
1a4e5 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d  ;.        for(i=
1a4e6 30 3b 20 69 3c 70 53 72 63 4c 69 73 74 2d 3e 6e  0; i<pSrcList->n
1a4e7 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b  Src; i++, pItem+
1a4e8 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 74  +){.          st
1a4e9 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c  ruct AggInfo_col
1a4ea 20 2a 70 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20   *pCol;.        
1a4eb 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 54 61    if( pExpr->iTa
1a4ec 62 6c 65 3d 3d 70 49 74 65 6d 2d 3e 69 43 75 72  ble==pItem->iCur
1a4ed 73 6f 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20  sor ){.         
1a4ee 20 20 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63     /* If we reac
1a4ef 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 69 74  h this point, it
1a4f0 20 6d 65 61 6e 73 20 74 68 61 74 20 70 45 78 70   means that pExp
1a4f1 72 20 72 65 66 65 72 73 20 74 6f 20 61 20 74 61  r refers to a ta
1a4f2 62 6c 65 0a 20 20 20 20 20 20 20 20 20 20 20 20  ble.            
1a4f3 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 20 74 68  ** that is in th
1a4f4 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66  e FROM clause of
1a4f5 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 71   the aggregate q
1a4f6 75 65 72 79 2e 20 20 0a 20 20 20 20 20 20 20 20  uery.  .        
1a4f7 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 20      **.         
1a4f8 20 20 20 2a 2a 20 4d 61 6b 65 20 61 6e 20 65 6e     ** Make an en
1a4f9 74 72 79 20 66 6f 72 20 74 68 65 20 63 6f 6c 75  try for the colu
1a4fa 6d 6e 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e  mn in pAggInfo->
1a4fb 61 43 6f 6c 5b 5d 20 69 66 20 74 68 65 72 65 0a  aCol[] if there.
1a4fc 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69              ** i
1a4fd 73 20 6e 6f 74 20 61 6e 20 65 6e 74 72 79 20 74  s not an entry t
1a4fe 68 65 72 65 20 61 6c 72 65 61 64 79 2e 0a 20 20  here already..  
1a4ff 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
1a500 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a           int k;.
1a501 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c              pCol
1a502 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f   = pAggInfo->aCo
1a503 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66  l;.            f
1a504 6f 72 28 6b 3d 30 3b 20 6b 3c 70 41 67 67 49 6e  or(k=0; k<pAggIn
1a505 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b  fo->nColumn; k++
1a506 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20  , pCol++){.     
1a507 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43 6f           if( pCo
1a508 6c 2d 3e 69 54 61 62 6c 65 3d 3d 70 45 78 70 72  l->iTable==pExpr
1a509 2d 3e 69 54 61 62 6c 65 20 26 26 0a 20 20 20 20  ->iTable &&.    
1a50a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43                pC
1a50b 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78  ol->iColumn==pEx
1a50c 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20  pr->iColumn ){. 
1a50d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
1a50e 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
1a50f 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1a510 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
1a511 69 66 28 20 28 6b 3e 3d 70 41 67 67 49 6e 66 6f  if( (k>=pAggInfo
1a512 2d 3e 6e 43 6f 6c 75 6d 6e 29 0a 20 20 20 20 20  ->nColumn).     
1a513 20 20 20 20 20 20 20 20 26 26 20 28 6b 20 3d 20          && (k = 
1a514 61 64 64 41 67 67 49 6e 66 6f 43 6f 6c 75 6d 6e  addAggInfoColumn
1a515 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 41 67  (pParse->db, pAg
1a516 67 49 6e 66 6f 29 29 3e 3d 30 20 0a 20 20 20 20  gInfo))>=0 .    
1a517 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20          ){.     
1a518 20 20 20 20 20 20 20 20 20 70 43 6f 6c 20 3d 20           pCol = 
1a519 26 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b  &pAggInfo->aCol[
1a51a 6b 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  k];.            
1a51b 20 20 70 43 6f 6c 2d 3e 70 54 61 62 20 3d 20 70    pCol->pTab = p
1a51c 45 78 70 72 2d 3e 70 54 61 62 3b 0a 20 20 20 20  Expr->pTab;.    
1a51d 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e            pCol->
1a51e 69 54 61 62 6c 65 20 3d 20 70 45 78 70 72 2d 3e  iTable = pExpr->
1a51f 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 20 20  iTable;.        
1a520 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 43 6f 6c        pCol->iCol
1a521 75 6d 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f  umn = pExpr->iCo
1a522 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20 20 20  lumn;.          
1a523 20 20 20 20 70 43 6f 6c 2d 3e 69 4d 65 6d 20 3d      pCol->iMem =
1a524 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
1a525 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
1a526 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75  Col->iSorterColu
1a527 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20  mn = -1;.       
1a528 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70 45 78         pCol->pEx
1a529 70 72 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20  pr = pExpr;.    
1a52a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 41            if( pA
1a52b 67 67 49 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 79  ggInfo->pGroupBy
1a52c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1a52d 20 20 20 20 69 6e 74 20 6a 2c 20 6e 3b 0a 20 20      int j, n;.  
1a52e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45 78                Ex
1a52f 70 72 4c 69 73 74 20 2a 70 47 42 20 3d 20 70 41  prList *pGB = pA
1a530 67 67 49 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 79  ggInfo->pGroupBy
1a531 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
1a532 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
1a533 74 5f 69 74 65 6d 20 2a 70 54 65 72 6d 20 3d 20  t_item *pTerm = 
1a534 70 47 42 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20  pGB->a;.        
1a535 20 20 20 20 20 20 20 20 6e 20 3d 20 70 47 42 2d          n = pGB-
1a536 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20  >nExpr;.        
1a537 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b          for(j=0;
1a538 20 6a 3c 6e 3b 20 6a 2b 2b 2c 20 70 54 65 72 6d   j<n; j++, pTerm
1a539 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ++){.           
1a53a 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 20         Expr *pE 
1a53b 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a  = pTerm->pExpr;.
1a53c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a53d 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b    if( pE->op==TK
1a53e 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 45 2d 3e 69  _COLUMN && pE->i
1a53f 54 61 62 6c 65 3d 3d 70 45 78 70 72 2d 3e 69 54  Table==pExpr->iT
1a540 61 62 6c 65 20 26 26 0a 20 20 20 20 20 20 20 20  able &&.        
1a541 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
1a542 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72  ->iColumn==pExpr
1a543 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ->iColumn ){.   
1a544 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a545 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f   pCol->iSorterCo
1a546 6c 75 6d 6e 20 3d 20 6a 3b 0a 20 20 20 20 20 20  lumn = j;.      
1a547 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72                br
1a548 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  eak;.           
1a549 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1a54a 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1a54b 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1a54c 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43 6f           if( pCo
1a54d 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e  l->iSorterColumn
1a54e 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  <0 ){.          
1a54f 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72        pCol->iSor
1a550 74 65 72 43 6f 6c 75 6d 6e 20 3d 20 70 41 67 67  terColumn = pAgg
1a551 49 6e 66 6f 2d 3e 6e 53 6f 72 74 69 6e 67 43 6f  Info->nSortingCo
1a552 6c 75 6d 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20  lumn++;.        
1a553 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1a554 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1a555 20 20 2f 2a 20 54 68 65 72 65 20 69 73 20 6e 6f    /* There is no
1a556 77 20 61 6e 20 65 6e 74 72 79 20 66 6f 72 20 70  w an entry for p
1a557 45 78 70 72 20 69 6e 20 70 41 67 67 49 6e 66 6f  Expr in pAggInfo
1a558 2d 3e 61 43 6f 6c 5b 5d 20 28 65 69 74 68 65 72  ->aCol[] (either
1a559 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
1a55a 62 65 63 61 75 73 65 20 69 74 20 77 61 73 20 74  because it was t
1a55b 68 65 72 65 20 62 65 66 6f 72 65 20 6f 72 20 62  here before or b
1a55c 65 63 61 75 73 65 20 77 65 20 6a 75 73 74 20 63  ecause we just c
1a55d 72 65 61 74 65 64 20 69 74 29 2e 0a 20 20 20 20  reated it)..    
1a55e 20 20 20 20 20 20 20 20 2a 2a 20 43 6f 6e 76 65          ** Conve
1a55f 72 74 20 74 68 65 20 70 45 78 70 72 20 74 6f 20  rt the pExpr to 
1a560 62 65 20 61 20 54 4b 5f 41 47 47 5f 43 4f 4c 55  be a TK_AGG_COLU
1a561 4d 4e 20 72 65 66 65 72 72 69 6e 67 20 74 6f 20  MN referring to 
1a562 74 68 61 74 0a 20 20 20 20 20 20 20 20 20 20 20  that.           
1a563 20 2a 2a 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43   ** pAggInfo->aC
1a564 6f 6c 5b 5d 20 65 6e 74 72 79 2e 0a 20 20 20 20  ol[] entry..    
1a565 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
1a566 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 41         pExpr->pA
1a567 67 67 49 6e 66 6f 20 3d 20 70 41 67 67 49 6e 66  ggInfo = pAggInf
1a568 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  o;.            p
1a569 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 41 47  Expr->op = TK_AG
1a56a 47 5f 43 4f 4c 55 4d 4e 3b 0a 20 20 20 20 20 20  G_COLUMN;.      
1a56b 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 41 67        pExpr->iAg
1a56c 67 20 3d 20 6b 3b 0a 20 20 20 20 20 20 20 20 20  g = k;.         
1a56d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1a56e 20 20 20 20 7d 20 2f 2a 20 65 6e 64 69 66 20 70      } /* endif p
1a56f 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 70 49  Expr->iTable==pI
1a570 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20 2a 2f 0a  tem->iCursor */.
1a571 20 20 20 20 20 20 20 20 7d 20 2f 2a 20 65 6e 64          } /* end
1a572 20 6c 6f 6f 70 20 6f 76 65 72 20 70 53 72 63 4c   loop over pSrcL
1a573 69 73 74 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20  ist */.      }. 
1a574 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
1a575 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
1a576 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b  _AGG_FUNCTION: {
1a577 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 4e  .      /* The pN
1a578 43 2d 3e 6e 44 65 70 74 68 3d 3d 30 20 74 65 73  C->nDepth==0 tes
1a579 74 20 63 61 75 73 65 73 20 61 67 67 72 65 67 61  t causes aggrega
1a57a 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20  te functions in 
1a57b 73 75 62 71 75 65 72 69 65 73 0a 20 20 20 20 20  subqueries.     
1a57c 20 2a 2a 20 74 6f 20 62 65 20 69 67 6e 6f 72 65   ** to be ignore
1a57d 64 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70  d */.      if( p
1a57e 4e 43 2d 3e 6e 44 65 70 74 68 3d 3d 30 20 29 7b  NC->nDepth==0 ){
1a57f 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63  .        /* Chec
1a580 6b 20 74 6f 20 73 65 65 20 69 66 20 70 45 78 70  k to see if pExp
1a581 72 20 69 73 20 61 20 64 75 70 6c 69 63 61 74 65  r is a duplicate
1a582 20 6f 66 20 61 6e 6f 74 68 65 72 20 61 67 67 72   of another aggr
1a583 65 67 61 74 65 20 0a 20 20 20 20 20 20 20 20 2a  egate .        *
1a584 2a 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20  * function that 
1a585 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68  is already in th
1a586 65 20 70 41 67 67 49 6e 66 6f 20 73 74 72 75 63  e pAggInfo struc
1a587 74 75 72 65 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ture.        */.
1a588 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20 41          struct A
1a589 67 67 49 6e 66 6f 5f 66 75 6e 63 20 2a 70 49 74  ggInfo_func *pIt
1a58a 65 6d 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e 61  em = pAggInfo->a
1a58b 46 75 6e 63 3b 0a 20 20 20 20 20 20 20 20 66 6f  Func;.        fo
1a58c 72 28 69 3d 30 3b 20 69 3c 70 41 67 67 49 6e 66  r(i=0; i<pAggInf
1a58d 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20 70  o->nFunc; i++, p
1a58e 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Item++){.       
1a58f 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78     if( sqlite3Ex
1a590 70 72 43 6f 6d 70 61 72 65 28 70 49 74 65 6d 2d  prCompare(pItem-
1a591 3e 70 45 78 70 72 2c 20 70 45 78 70 72 29 20 29  >pExpr, pExpr) )
1a592 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72  {.            br
1a593 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  eak;.          }
1a594 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1a595 20 20 20 69 66 28 20 69 3e 3d 70 41 67 67 49 6e     if( i>=pAggIn
1a596 66 6f 2d 3e 6e 46 75 6e 63 20 29 7b 0a 20 20 20  fo->nFunc ){.   
1a597 20 20 20 20 20 20 20 2f 2a 20 70 45 78 70 72 20         /* pExpr 
1a598 69 73 20 6f 72 69 67 69 6e 61 6c 2e 20 20 4d 61  is original.  Ma
1a599 6b 65 20 61 20 6e 65 77 20 65 6e 74 72 79 20 69  ke a new entry i
1a59a 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e  n pAggInfo->aFun
1a59b 63 5b 5d 0a 20 20 20 20 20 20 20 20 20 20 2a 2f  c[].          */
1a59c 0a 20 20 20 20 20 20 20 20 20 20 75 38 20 65 6e  .          u8 en
1a59d 63 20 3d 20 45 4e 43 28 70 50 61 72 73 65 2d 3e  c = ENC(pParse->
1a59e 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  db);.          i
1a59f 20 3d 20 61 64 64 41 67 67 49 6e 66 6f 46 75 6e   = addAggInfoFun
1a5a0 63 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 41  c(pParse->db, pA
1a5a1 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20  ggInfo);.       
1a5a2 20 20 20 69 66 28 20 69 3e 3d 30 20 29 7b 0a 20     if( i>=0 ){. 
1a5a3 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d             pItem
1a5a4 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61 46   = &pAggInfo->aF
1a5a5 75 6e 63 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20  unc[i];.        
1a5a6 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72      pItem->pExpr
1a5a7 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20   = pExpr;.      
1a5a8 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 4d 65        pItem->iMe
1a5a9 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  m = ++pParse->nM
1a5aa 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  em;.            
1a5ab 70 49 74 65 6d 2d 3e 70 46 75 6e 63 20 3d 20 73  pItem->pFunc = s
1a5ac 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69  qlite3FindFuncti
1a5ad 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 0a 20  on(pParse->db,. 
1a5ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a5af 20 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e    (char*)pExpr->
1a5b0 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e  token.z, pExpr->
1a5b1 74 6f 6b 65 6e 2e 6e 2c 0a 20 20 20 20 20 20 20  token.n,.       
1a5b2 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70              pExp
1a5b3 72 2d 3e 70 4c 69 73 74 20 3f 20 70 45 78 70 72  r->pList ? pExpr
1a5b4 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a  ->pList->nExpr :
1a5b5 20 30 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20   0, enc, 0);.   
1a5b6 20 20 20 20 20 20 20 20 20 69 66 28 20 70 45 78           if( pEx
1a5b7 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 44  pr->flags & EP_D
1a5b8 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 20  istinct ){.     
1a5b9 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e           pItem->
1a5ba 69 44 69 73 74 69 6e 63 74 20 3d 20 70 50 61 72  iDistinct = pPar
1a5bb 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20  se->nTab++;.    
1a5bc 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
1a5bd 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74               pIt
1a5be 65 6d 2d 3e 69 44 69 73 74 69 6e 63 74 20 3d 20  em->iDistinct = 
1a5bf 2d 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  -1;.            
1a5c0 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  }.          }.  
1a5c1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1a5c2 2f 2a 20 4d 61 6b 65 20 70 45 78 70 72 20 70 6f  /* Make pExpr po
1a5c3 69 6e 74 20 74 6f 20 74 68 65 20 61 70 70 72 6f  int to the appro
1a5c4 70 72 69 61 74 65 20 70 41 67 67 49 6e 66 6f 2d  priate pAggInfo-
1a5c5 3e 61 46 75 6e 63 5b 5d 20 65 6e 74 72 79 0a 20  >aFunc[] entry. 
1a5c6 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
1a5c7 20 20 70 45 78 70 72 2d 3e 69 41 67 67 20 3d 20    pExpr->iAgg = 
1a5c8 69 3b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72  i;.        pExpr
1a5c9 2d 3e 70 41 67 67 49 6e 66 6f 20 3d 20 70 41 67  ->pAggInfo = pAg
1a5ca 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 72  gInfo;.        r
1a5cb 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d  eturn 1;.      }
1a5cc 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
1a5cd 20 52 65 63 75 72 73 69 76 65 6c 79 20 77 61 6c   Recursively wal
1a5ce 6b 20 73 75 62 71 75 65 72 69 65 73 20 6c 6f 6f  k subqueries loo
1a5cf 6b 69 6e 67 20 66 6f 72 20 54 4b 5f 43 4f 4c 55  king for TK_COLU
1a5d0 4d 4e 20 6e 6f 64 65 73 20 74 68 61 74 20 6e 65  MN nodes that ne
1a5d1 65 64 0a 20 20 2a 2a 20 74 6f 20 62 65 20 63 68  ed.  ** to be ch
1a5d2 61 6e 67 65 64 20 74 6f 20 54 4b 5f 41 47 47 5f  anged to TK_AGG_
1a5d3 43 4f 4c 55 4d 4e 2e 20 20 42 75 74 20 69 6e 63  COLUMN.  But inc
1a5d4 72 65 6d 65 6e 74 20 6e 44 65 70 74 68 20 73 6f  rement nDepth so
1a5d5 20 74 68 61 74 0a 20 20 2a 2a 20 54 4b 5f 41 47   that.  ** TK_AG
1a5d6 47 5f 46 55 4e 43 54 49 4f 4e 20 6e 6f 64 65 73  G_FUNCTION nodes
1a5d7 20 69 6e 20 73 75 62 71 75 65 72 69 65 73 20 77   in subqueries w
1a5d8 69 6c 6c 20 62 65 20 75 6e 63 68 61 6e 67 65 64  ill be unchanged
1a5d9 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 45 78  ..  */.  if( pEx
1a5da 70 72 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20  pr->pSelect ){. 
1a5db 20 20 20 70 4e 43 2d 3e 6e 44 65 70 74 68 2b 2b     pNC->nDepth++
1a5dc 3b 0a 20 20 20 20 77 61 6c 6b 53 65 6c 65 63 74  ;.    walkSelect
1a5dd 45 78 70 72 28 70 45 78 70 72 2d 3e 70 53 65 6c  Expr(pExpr->pSel
1a5de 65 63 74 2c 20 61 6e 61 6c 79 7a 65 41 67 67 72  ect, analyzeAggr
1a5df 65 67 61 74 65 2c 20 70 4e 43 29 3b 0a 20 20 20  egate, pNC);.   
1a5e0 20 70 4e 43 2d 3e 6e 44 65 70 74 68 2d 2d 3b 0a   pNC->nDepth--;.
1a5e1 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
1a5e2 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 7a 65  }../*.** Analyze
1a5e3 20 74 68 65 20 67 69 76 65 6e 20 65 78 70 72 65   the given expre
1a5e4 73 73 69 6f 6e 20 6c 6f 6f 6b 69 6e 67 20 66 6f  ssion looking fo
1a5e5 72 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  r aggregate func
1a5e6 74 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20 66 6f 72  tions and.** for
1a5e7 20 76 61 72 69 61 62 6c 65 73 20 74 68 61 74 20   variables that 
1a5e8 6e 65 65 64 20 74 6f 20 62 65 20 61 64 64 65 64  need to be added
1a5e9 20 74 6f 20 74 68 65 20 70 50 61 72 73 65 2d 3e   to the pParse->
1a5ea 61 41 67 67 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a  aAgg[] array..**
1a5eb 20 4d 61 6b 65 20 61 64 64 69 74 69 6f 6e 61 6c   Make additional
1a5ec 20 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65 20   entries to the 
1a5ed 70 50 61 72 73 65 2d 3e 61 41 67 67 5b 5d 20 61  pParse->aAgg[] a
1a5ee 72 72 61 79 20 61 73 20 6e 65 63 65 73 73 61 72  rray as necessar
1a5ef 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  y..**.** This ro
1a5f0 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 6f 6e 6c  utine should onl
1a5f1 79 20 62 65 20 63 61 6c 6c 65 64 20 61 66 74 65  y be called afte
1a5f2 72 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  r the expression
1a5f3 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 61 6e 61   has been.** ana
1a5f4 6c 79 7a 65 64 20 62 79 20 73 71 6c 69 74 65 33  lyzed by sqlite3
1a5f5 45 78 70 72 52 65 73 6f 6c 76 65 4e 61 6d 65 73  ExprResolveNames
1a5f6 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ()..*/.SQLITE_PR
1a5f7 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1a5f8 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67  e3ExprAnalyzeAgg
1a5f9 72 65 67 61 74 65 73 28 4e 61 6d 65 43 6f 6e 74  regates(NameCont
1a5fa 65 78 74 20 2a 70 4e 43 2c 20 45 78 70 72 20 2a  ext *pNC, Expr *
1a5fb 70 45 78 70 72 29 7b 0a 20 20 77 61 6c 6b 45 78  pExpr){.  walkEx
1a5fc 70 72 54 72 65 65 28 70 45 78 70 72 2c 20 61 6e  prTree(pExpr, an
1a5fd 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 2c 20  alyzeAggregate, 
1a5fe 70 4e 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  pNC);.}../*.** C
1a5ff 61 6c 6c 20 73 71 6c 69 74 65 33 45 78 70 72 41  all sqlite3ExprA
1a600 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73  nalyzeAggregates
1a601 28 29 20 66 6f 72 20 65 76 65 72 79 20 65 78 70  () for every exp
1a602 72 65 73 73 69 6f 6e 20 69 6e 20 61 6e 0a 2a 2a  ression in an.**
1a603 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
1a604 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  .  Return the nu
1a605 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 0a  mber of errors..
1a606 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f  **.** If an erro
1a607 72 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 65 20  r is found, the 
1a608 61 6e 61 6c 79 73 69 73 20 69 73 20 63 75 74 20  analysis is cut 
1a609 73 68 6f 72 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  short..*/.SQLITE
1a60a 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1a60b 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65  lite3ExprAnalyze
1a60c 41 67 67 4c 69 73 74 28 4e 61 6d 65 43 6f 6e 74  AggList(NameCont
1a60d 65 78 74 20 2a 70 4e 43 2c 20 45 78 70 72 4c 69  ext *pNC, ExprLi
1a60e 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 73 74  st *pList){.  st
1a60f 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
1a610 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74  em *pItem;.  int
1a611 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 20   i;.  if( pList 
1a612 29 7b 0a 20 20 20 20 66 6f 72 28 70 49 74 65 6d  ){.    for(pItem
1a613 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20  =pList->a, i=0; 
1a614 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  i<pList->nExpr; 
1a615 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i++, pItem++){. 
1a616 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1a617 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65  AnalyzeAggregate
1a618 73 28 70 4e 43 2c 20 70 49 74 65 6d 2d 3e 70 45  s(pNC, pItem->pE
1a619 78 70 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  xpr);.    }.  }.
1a61a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  }../*.** Allocat
1a61b 65 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 20  e or deallocate 
1a61c 74 65 6d 70 6f 72 61 72 79 20 75 73 65 20 72 65  temporary use re
1a61d 67 69 73 74 65 72 73 20 64 75 72 69 6e 67 20 63  gisters during c
1a61e 6f 64 65 20 67 65 6e 65 72 61 74 69 6f 6e 2e 0a  ode generation..
1a61f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1a620 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74  E int sqlite3Get
1a621 54 65 6d 70 52 65 67 28 50 61 72 73 65 20 2a 70  TempReg(Parse *p
1a622 50 61 72 73 65 29 7b 0a 20 20 69 6e 74 20 69 2c  Parse){.  int i,
1a623 20 72 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65   r;.  if( pParse
1a624 2d 3e 6e 54 65 6d 70 52 65 67 3d 3d 30 20 29 7b  ->nTempReg==0 ){
1a625 0a 20 20 20 20 72 65 74 75 72 6e 20 2b 2b 70 50  .    return ++pP
1a626 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 7d 0a  arse->nMem;.  }.
1a627 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61    for(i=0; i<pPa
1a628 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 3b 20 69  rse->nTempReg; i
1a629 2b 2b 29 7b 0a 20 20 20 20 72 20 3d 20 70 50 61  ++){.    r = pPa
1a62a 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 5b 69 5d  rse->aTempReg[i]
1a62b 3b 0a 20 20 20 20 69 66 28 20 75 73 65 64 41 73  ;.    if( usedAs
1a62c 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50 61 72  ColumnCache(pPar
1a62d 73 65 2c 20 72 2c 20 72 29 20 29 20 63 6f 6e 74  se, r, r) ) cont
1a62e 69 6e 75 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20  inue;.  }.  if( 
1a62f 69 3e 3d 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70  i>=pParse->nTemp
1a630 52 65 67 20 29 7b 0a 20 20 20 20 72 65 74 75 72  Reg ){.    retur
1a631 6e 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  n ++pParse->nMem
1a632 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 69  ;.  }.  while( i
1a633 3c 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65  <pParse->nTempRe
1a634 67 2d 31 20 29 7b 0a 20 20 20 20 70 50 61 72 73  g-1 ){.    pPars
1a635 65 2d 3e 61 54 65 6d 70 52 65 67 5b 69 5d 20 3d  e->aTempReg[i] =
1a636 20 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65   pParse->aTempRe
1a637 67 5b 69 2b 31 5d 3b 0a 20 20 7d 0a 20 20 70 50  g[i+1];.  }.  pP
1a638 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 2d 2d  arse->nTempReg--
1a639 3b 0a 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a  ;.  return r;.}.
1a63a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1a63b 6f 69 64 20 73 71 6c 69 74 65 33 52 65 6c 65 61  oid sqlite3Relea
1a63c 73 65 54 65 6d 70 52 65 67 28 50 61 72 73 65 20  seTempReg(Parse 
1a63d 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65  *pParse, int iRe
1a63e 67 29 7b 0a 20 20 69 66 28 20 69 52 65 67 20 26  g){.  if( iReg &
1a63f 26 20 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52  & pParse->nTempR
1a640 65 67 3c 41 72 72 61 79 53 69 7a 65 28 70 50 61  eg<ArraySize(pPa
1a641 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 29 20 29  rse->aTempReg) )
1a642 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 61 54  {.    pParse->aT
1a643 65 6d 70 52 65 67 5b 70 50 61 72 73 65 2d 3e 6e  empReg[pParse->n
1a644 54 65 6d 70 52 65 67 2b 2b 5d 20 3d 20 69 52 65  TempReg++] = iRe
1a645 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  g;.  }.}../*.** 
1a646 41 6c 6c 6f 63 61 74 65 20 6f 72 20 64 65 61 6c  Allocate or deal
1a647 6c 6f 63 61 74 65 20 61 20 62 6c 6f 63 6b 20 6f  locate a block o
1a648 66 20 6e 52 65 67 20 63 6f 6e 73 65 63 75 74 69  f nReg consecuti
1a649 76 65 20 72 65 67 69 73 74 65 72 73 0a 2a 2f 0a  ve registers.*/.
1a64a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1a64b 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d  nt sqlite3GetTem
1a64c 70 52 61 6e 67 65 28 50 61 72 73 65 20 2a 70 50  pRange(Parse *pP
1a64d 61 72 73 65 2c 20 69 6e 74 20 6e 52 65 67 29 7b  arse, int nReg){
1a64e 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 69  .  int i, n;.  i
1a64f 20 3d 20 70 50 61 72 73 65 2d 3e 69 52 61 6e 67   = pParse->iRang
1a650 65 52 65 67 3b 0a 20 20 6e 20 3d 20 70 50 61 72  eReg;.  n = pPar
1a651 73 65 2d 3e 6e 52 61 6e 67 65 52 65 67 3b 0a 20  se->nRangeReg;. 
1a652 20 69 66 28 20 6e 52 65 67 3c 3d 6e 20 26 26 20   if( nReg<=n && 
1a653 21 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63  !usedAsColumnCac
1a654 68 65 28 70 50 61 72 73 65 2c 20 69 2c 20 69 2b  he(pParse, i, i+
1a655 6e 2d 31 29 20 29 7b 0a 20 20 20 20 70 50 61 72  n-1) ){.    pPar
1a656 73 65 2d 3e 69 52 61 6e 67 65 52 65 67 20 2b 3d  se->iRangeReg +=
1a657 20 6e 52 65 67 3b 0a 20 20 20 20 70 50 61 72 73   nReg;.    pPars
1a658 65 2d 3e 6e 52 61 6e 67 65 52 65 67 20 2d 3d 20  e->nRangeReg -= 
1a659 6e 52 65 67 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  nReg;.  }else{. 
1a65a 20 20 20 69 20 3d 20 70 50 61 72 73 65 2d 3e 6e     i = pParse->n
1a65b 4d 65 6d 2b 31 3b 0a 20 20 20 20 70 50 61 72 73  Mem+1;.    pPars
1a65c 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e 52 65 67 3b  e->nMem += nReg;
1a65d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 3b  .  }.  return i;
1a65e 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
1a65f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65  E void sqlite3Re
1a660 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 50  leaseTempRange(P
1a661 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
1a662 74 20 69 52 65 67 2c 20 69 6e 74 20 6e 52 65 67  t iReg, int nReg
1a663 29 7b 0a 20 20 69 66 28 20 6e 52 65 67 3e 70 50  ){.  if( nReg>pP
1a664 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65 67 20  arse->nRangeReg 
1a665 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  ){.    pParse->n
1a666 52 61 6e 67 65 52 65 67 20 3d 20 6e 52 65 67 3b  RangeReg = nReg;
1a667 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 69 52 61  .    pParse->iRa
1a668 6e 67 65 52 65 67 20 3d 20 69 52 65 67 3b 0a 20  ngeReg = iReg;. 
1a669 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a   }.}../*********
1a66a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 65 78 70  ***** End of exp
1a66b 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  r.c ************
1a66c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a66d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a66e 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1a66f 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
1a670 20 61 6c 74 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a   alter.c *******
1a671 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a672 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a673 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35  ****/./*.** 2005
1a674 20 46 65 62 72 75 61 72 79 20 31 35 0a 2a 2a 0a   February 15.**.
1a675 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
1a676 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
1a677 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
1a678 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
1a679 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
1a67a 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
1a67b 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
1a67c 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
1a67d 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
1a67e 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1a67f 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
1a680 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
1a681 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
1a682 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1a683 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
1a684 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
1a685 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
1a686 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
1a687 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a688 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a689 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a68a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
1a68b 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
1a68c 73 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65  s C code routine
1a68d 73 20 74 68 61 74 20 75 73 65 64 20 74 6f 20 67  s that used to g
1a68e 65 6e 65 72 61 74 65 20 56 44 42 45 20 63 6f 64  enerate VDBE cod
1a68f 65 0a 2a 2a 20 74 68 61 74 20 69 6d 70 6c 65 6d  e.** that implem
1a690 65 6e 74 73 20 74 68 65 20 41 4c 54 45 52 20 54  ents the ALTER T
1a691 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a  ABLE command..**
1a692 0a 2a 2a 20 24 49 64 3a 20 61 6c 74 65 72 2e 63  .** $Id: alter.c
1a693 2c 76 20 31 2e 34 34 20 32 30 30 38 2f 30 35 2f  ,v 1.44 2008/05/
1a694 30 39 20 31 34 3a 31 37 3a 35 32 20 64 72 68 20  09 14:17:52 drh 
1a695 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
1a696 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73  The code in this
1a697 20 66 69 6c 65 20 6f 6e 6c 79 20 65 78 69 73 74   file only exist
1a698 73 20 69 66 20 77 65 20 61 72 65 20 6e 6f 74 20  s if we are not 
1a699 6f 6d 69 74 74 69 6e 67 20 74 68 65 0a 2a 2a 20  omitting the.** 
1a69a 41 4c 54 45 52 20 54 41 42 4c 45 20 6c 6f 67 69  ALTER TABLE logi
1a69b 63 20 66 72 6f 6d 20 74 68 65 20 62 75 69 6c 64  c from the build
1a69c 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
1a69d 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41  ITE_OMIT_ALTERTA
1a69e 42 4c 45 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  BLE.../*.** This
1a69f 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
1a6a0 64 20 62 79 20 53 51 4c 20 67 65 6e 65 72 61 74  d by SQL generat
1a6a1 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
1a6a2 74 68 65 20 0a 2a 2a 20 41 4c 54 45 52 20 54 41  the .** ALTER TA
1a6a3 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65  BLE command. The
1a6a4 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
1a6a5 69 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 61  is the text of a
1a6a6 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f 72   CREATE TABLE or
1a6a7 0a 2a 2a 20 43 52 45 41 54 45 20 49 4e 44 45 58  .** CREATE INDEX
1a6a8 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65 20 73 65   command. The se
1a6a9 63 6f 6e 64 20 69 73 20 61 20 74 61 62 6c 65 20  cond is a table 
1a6aa 6e 61 6d 65 2e 20 54 68 65 20 74 61 62 6c 65 20  name. The table 
1a6ab 6e 61 6d 65 20 69 6e 20 0a 2a 2a 20 74 68 65 20  name in .** the 
1a6ac 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f 72 20  CREATE TABLE or 
1a6ad 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 74 61  CREATE INDEX sta
1a6ae 74 65 6d 65 6e 74 20 69 73 20 72 65 70 6c 61 63  tement is replac
1a6af 65 64 20 77 69 74 68 20 74 68 65 20 74 68 69 72  ed with the thir
1a6b0 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 61 6e  d.** argument an
1a6b1 64 20 74 68 65 20 72 65 73 75 6c 74 20 72 65 74  d the result ret
1a6b2 75 72 6e 65 64 2e 20 45 78 61 6d 70 6c 65 73 3a  urned. Examples:
1a6b3 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 5f 72 65  .**.** sqlite_re
1a6b4 6e 61 6d 65 5f 74 61 62 6c 65 28 27 43 52 45 41  name_table('CREA
1a6b5 54 45 20 54 41 42 4c 45 20 61 62 63 28 61 2c 20  TE TABLE abc(a, 
1a6b6 62 2c 20 63 29 27 2c 20 27 64 65 66 27 29 0a 2a  b, c)', 'def').*
1a6b7 2a 20 20 20 20 20 2d 3e 20 27 43 52 45 41 54 45  *     -> 'CREATE
1a6b8 20 54 41 42 4c 45 20 64 65 66 28 61 2c 20 62 2c   TABLE def(a, b,
1a6b9 20 63 29 27 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74   c)'.**.** sqlit
1a6ba 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 27  e_rename_table('
1a6bb 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 20 4f  CREATE INDEX i O
1a6bc 4e 20 61 62 63 28 61 29 27 2c 20 27 64 65 66 27  N abc(a)', 'def'
1a6bd 29 0a 2a 2a 20 20 20 20 20 2d 3e 20 27 43 52 45  ).**     -> 'CRE
1a6be 41 54 45 20 49 4e 44 45 58 20 69 20 4f 4e 20 64  ATE INDEX i ON d
1a6bf 65 66 28 61 2c 20 62 2c 20 63 29 27 0a 2a 2f 0a  ef(a, b, c)'.*/.
1a6c0 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6e 61  static void rena
1a6c1 6d 65 54 61 62 6c 65 46 75 6e 63 28 0a 20 20 73  meTableFunc(.  s
1a6c2 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1a6c3 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
1a6c4 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
1a6c5 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
1a6c6 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63   unsigned char c
1a6c7 6f 6e 73 74 20 2a 7a 53 71 6c 20 3d 20 73 71 6c  onst *zSql = sql
1a6c8 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
1a6c9 61 72 67 76 5b 30 5d 29 3b 0a 20 20 75 6e 73 69  argv[0]);.  unsi
1a6ca 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20  gned char const 
1a6cb 2a 7a 54 61 62 6c 65 4e 61 6d 65 20 3d 20 73 71  *zTableName = sq
1a6cc 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1a6cd 28 61 72 67 76 5b 31 5d 29 3b 0a 0a 20 20 69 6e  (argv[1]);..  in
1a6ce 74 20 74 6f 6b 65 6e 3b 0a 20 20 54 6f 6b 65 6e  t token;.  Token
1a6cf 20 74 6e 61 6d 65 3b 0a 20 20 75 6e 73 69 67 6e   tname;.  unsign
1a6d0 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a  ed char const *z
1a6d1 43 73 72 20 3d 20 7a 53 71 6c 3b 0a 20 20 69 6e  Csr = zSql;.  in
1a6d2 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 63 68 61  t len = 0;.  cha
1a6d3 72 20 2a 7a 52 65 74 3b 0a 0a 20 20 73 71 6c 69  r *zRet;..  sqli
1a6d4 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65  te3 *db = sqlite
1a6d5 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
1a6d6 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20  dle(context);.. 
1a6d7 20 2f 2a 20 54 68 65 20 70 72 69 6e 63 69 70 6c   /* The principl
1a6d8 65 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65  e used to locate
1a6d9 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20   the table name 
1a6da 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 41  in the CREATE TA
1a6db 42 4c 45 20 0a 20 20 2a 2a 20 73 74 61 74 65 6d  BLE .  ** statem
1a6dc 65 6e 74 20 69 73 20 74 68 61 74 20 74 68 65 20  ent is that the 
1a6dd 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 74 68  table name is th
1a6de 65 20 66 69 72 73 74 20 6e 6f 6e 2d 73 70 61 63  e first non-spac
1a6df 65 20 74 6f 6b 65 6e 20 74 68 61 74 0a 20 20 2a  e token that.  *
1a6e0 2a 20 69 73 20 69 6d 6d 65 64 69 61 74 65 6c 79  * is immediately
1a6e1 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 6c   followed by a l
1a6e2 65 66 74 20 70 61 72 65 6e 74 68 65 73 69 73 20  eft parenthesis 
1a6e3 2d 20 54 4b 5f 4c 50 20 2d 20 6f 72 20 22 55 53  - TK_LP - or "US
1a6e4 49 4e 47 22 20 54 4b 5f 55 53 49 4e 47 2e 0a 20  ING" TK_USING.. 
1a6e5 20 2a 2f 0a 20 20 69 66 28 20 7a 53 71 6c 20 29   */.  if( zSql )
1a6e6 7b 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20  {.    do {.     
1a6e7 20 69 66 28 20 21 2a 7a 43 73 72 20 29 7b 0a 20   if( !*zCsr ){. 
1a6e8 20 20 20 20 20 20 20 2f 2a 20 52 61 6e 20 6f 75         /* Ran ou
1a6e9 74 20 6f 66 20 69 6e 70 75 74 20 62 65 66 6f 72  t of input befor
1a6ea 65 20 66 69 6e 64 69 6e 67 20 61 6e 20 6f 70 65  e finding an ope
1a6eb 6e 69 6e 67 20 62 72 61 63 6b 65 74 2e 20 52 65  ning bracket. Re
1a6ec 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20  turn NULL. */.  
1a6ed 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
1a6ee 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
1a6ef 53 74 6f 72 65 20 74 68 65 20 74 6f 6b 65 6e 20  Store the token 
1a6f0 74 68 61 74 20 7a 43 73 72 20 70 6f 69 6e 74 73  that zCsr points
1a6f1 20 74 6f 20 69 6e 20 74 6e 61 6d 65 2e 20 2a 2f   to in tname. */
1a6f2 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e 7a 20 3d  .      tname.z =
1a6f3 20 7a 43 73 72 3b 0a 20 20 20 20 20 20 74 6e 61   zCsr;.      tna
1a6f4 6d 65 2e 6e 20 3d 20 6c 65 6e 3b 0a 0a 20 20 20  me.n = len;..   
1a6f5 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 7a 43     /* Advance zC
1a6f6 73 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 74  sr to the next t
1a6f7 6f 6b 65 6e 2e 20 53 74 6f 72 65 20 74 68 61 74  oken. Store that
1a6f8 20 74 6f 6b 65 6e 20 74 79 70 65 20 69 6e 20 27   token type in '
1a6f9 74 6f 6b 65 6e 27 2c 0a 20 20 20 20 20 20 2a 2a  token',.      **
1a6fa 20 61 6e 64 20 69 74 73 20 6c 65 6e 67 74 68 20   and its length 
1a6fb 69 6e 20 27 6c 65 6e 27 20 28 74 6f 20 62 65 20  in 'len' (to be 
1a6fc 75 73 65 64 20 6e 65 78 74 20 69 74 65 72 61 74  used next iterat
1a6fd 69 6f 6e 20 6f 66 20 74 68 69 73 20 6c 6f 6f 70  ion of this loop
1a6fe 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  )..      */.    
1a6ff 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 7a    do {.        z
1a700 43 73 72 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20  Csr += len;.    
1a701 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65      len = sqlite
1a702 33 47 65 74 54 6f 6b 65 6e 28 7a 43 73 72 2c 20  3GetToken(zCsr, 
1a703 26 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d  &token);.      }
1a704 20 77 68 69 6c 65 28 20 74 6f 6b 65 6e 3d 3d 54   while( token==T
1a705 4b 5f 53 50 41 43 45 20 7c 7c 20 74 6f 6b 65 6e  K_SPACE || token
1a706 3d 3d 54 4b 5f 43 4f 4d 4d 45 4e 54 20 29 3b 0a  ==TK_COMMENT );.
1a707 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65        assert( le
1a708 6e 3e 30 20 29 3b 0a 20 20 20 20 7d 20 77 68 69  n>0 );.    } whi
1a709 6c 65 28 20 74 6f 6b 65 6e 21 3d 54 4b 5f 4c 50  le( token!=TK_LP
1a70a 20 26 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 55 53   && token!=TK_US
1a70b 49 4e 47 20 29 3b 0a 0a 20 20 20 20 7a 52 65 74  ING );..    zRet
1a70c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
1a70d 66 28 64 62 2c 20 22 25 2e 2a 73 5c 22 25 77 5c  f(db, "%.*s\"%w\
1a70e 22 25 73 22 2c 20 74 6e 61 6d 65 2e 7a 20 2d 20  "%s", tname.z - 
1a70f 7a 53 71 6c 2c 20 7a 53 71 6c 2c 20 0a 20 20 20  zSql, zSql, .   
1a710 20 20 20 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20      zTableName, 
1a711 74 6e 61 6d 65 2e 7a 2b 74 6e 61 6d 65 2e 6e 29  tname.z+tname.n)
1a712 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
1a713 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
1a714 74 2c 20 7a 52 65 74 2c 20 2d 31 2c 20 73 71 6c  t, zRet, -1, sql
1a715 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 7d 0a  ite3_free);.  }.
1a716 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
1a717 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 2f  E_OMIT_TRIGGER./
1a718 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
1a719 69 73 20 75 73 65 64 20 62 79 20 53 51 4c 20 67  is used by SQL g
1a71a 65 6e 65 72 61 74 65 64 20 74 6f 20 69 6d 70 6c  enerated to impl
1a71b 65 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 41 4c 54  ement the.** ALT
1a71c 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64  ER TABLE command
1a71d 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75  . The first argu
1a71e 6d 65 6e 74 20 69 73 20 74 68 65 20 74 65 78 74  ment is the text
1a71f 20 6f 66 20 61 20 43 52 45 41 54 45 20 54 52 49   of a CREATE TRI
1a720 47 47 45 52 20 0a 2a 2a 20 73 74 61 74 65 6d 65  GGER .** stateme
1a721 6e 74 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 69  nt. The second i
1a722 73 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20  s a table name. 
1a723 54 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69  The table name i
1a724 6e 20 74 68 65 20 43 52 45 41 54 45 20 0a 2a 2a  n the CREATE .**
1a725 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65   TRIGGER stateme
1a726 6e 74 20 69 73 20 72 65 70 6c 61 63 65 64 20 77  nt is replaced w
1a727 69 74 68 20 74 68 65 20 74 68 69 72 64 20 61 72  ith the third ar
1a728 67 75 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 72  gument and the r
1a729 65 73 75 6c 74 20 0a 2a 2a 20 72 65 74 75 72 6e  esult .** return
1a72a 65 64 2e 20 54 68 69 73 20 69 73 20 61 6e 61 6c  ed. This is anal
1a72b 61 67 6f 75 73 20 74 6f 20 72 65 6e 61 6d 65 54  agous to renameT
1a72c 61 62 6c 65 46 75 6e 63 28 29 20 61 62 6f 76 65  ableFunc() above
1a72d 2c 20 65 78 63 65 70 74 20 66 6f 72 20 43 52 45  , except for CRE
1a72e 41 54 45 0a 2a 2a 20 54 52 49 47 47 45 52 2c 20  ATE.** TRIGGER, 
1a72f 6e 6f 74 20 43 52 45 41 54 45 20 49 4e 44 45 58  not CREATE INDEX
1a730 20 61 6e 64 20 43 52 45 41 54 45 20 54 41 42 4c   and CREATE TABL
1a731 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  E..*/.static voi
1a732 64 20 72 65 6e 61 6d 65 54 72 69 67 67 65 72 46  d renameTriggerF
1a733 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
1a734 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
1a735 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
1a736 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
1a737 72 67 76 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65  rgv.){.  unsigne
1a738 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 53  d char const *zS
1a739 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  ql = sqlite3_val
1a73a 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
1a73b 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ;.  unsigned cha
1a73c 72 20 63 6f 6e 73 74 20 2a 7a 54 61 62 6c 65 4e  r const *zTableN
1a73d 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  ame = sqlite3_va
1a73e 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d  lue_text(argv[1]
1a73f 29 3b 0a 0a 20 20 69 6e 74 20 74 6f 6b 65 6e 3b  );..  int token;
1a740 0a 20 20 54 6f 6b 65 6e 20 74 6e 61 6d 65 3b 0a  .  Token tname;.
1a741 20 20 69 6e 74 20 64 69 73 74 20 3d 20 33 3b 0a    int dist = 3;.
1a742 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
1a743 63 6f 6e 73 74 20 2a 7a 43 73 72 20 3d 20 7a 53  const *zCsr = zS
1a744 71 6c 3b 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20  ql;.  int len = 
1a745 30 3b 0a 20 20 63 68 61 72 20 2a 7a 52 65 74 3b  0;.  char *zRet;
1a746 0a 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ..  sqlite3 *db 
1a747 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  = sqlite3_contex
1a748 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74  t_db_handle(cont
1a749 65 78 74 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20  ext);..  /* The 
1a74a 70 72 69 6e 63 69 70 6c 65 20 75 73 65 64 20 74  principle used t
1a74b 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 74 61 62  o locate the tab
1a74c 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 43  le name in the C
1a74d 52 45 41 54 45 20 54 52 49 47 47 45 52 20 0a 20  REATE TRIGGER . 
1a74e 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 73   ** statement is
1a74f 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 20   that the table 
1a750 6e 61 6d 65 20 69 73 20 74 68 65 20 66 69 72 73  name is the firs
1a751 74 20 74 6f 6b 65 6e 20 74 68 61 74 20 69 73 20  t token that is 
1a752 69 6d 6d 65 64 69 61 74 65 64 6c 79 0a 20 20 2a  immediatedly.  *
1a753 2a 20 70 72 65 63 65 64 65 64 20 62 79 20 65 69  * preceded by ei
1a754 74 68 65 72 20 54 4b 5f 4f 4e 20 6f 72 20 54 4b  ther TK_ON or TK
1a755 5f 44 4f 54 20 61 6e 64 20 69 6d 6d 65 64 69 61  _DOT and immedia
1a756 74 65 64 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62  tedly followed b
1a757 79 20 6f 6e 65 0a 20 20 2a 2a 20 6f 66 20 54 4b  y one.  ** of TK
1a758 5f 57 48 45 4e 2c 20 54 4b 5f 42 45 47 49 4e 20  _WHEN, TK_BEGIN 
1a759 6f 72 20 54 4b 5f 46 4f 52 2e 0a 20 20 2a 2f 0a  or TK_FOR..  */.
1a75a 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20    if( zSql ){.  
1a75b 20 20 64 6f 20 7b 0a 0a 20 20 20 20 20 20 69 66    do {..      if
1a75c 28 20 21 2a 7a 43 73 72 20 29 7b 0a 20 20 20 20  ( !*zCsr ){.    
1a75d 20 20 20 20 2f 2a 20 52 61 6e 20 6f 75 74 20 6f      /* Ran out o
1a75e 66 20 69 6e 70 75 74 20 62 65 66 6f 72 65 20 66  f input before f
1a75f 69 6e 64 69 6e 67 20 74 68 65 20 74 61 62 6c 65  inding the table
1a760 20 6e 61 6d 65 2e 20 52 65 74 75 72 6e 20 4e 55   name. Return NU
1a761 4c 4c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 72  LL. */.        r
1a762 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 0a  eturn;.      }..
1a763 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74        /* Store t
1a764 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 7a 43  he token that zC
1a765 73 72 20 70 6f 69 6e 74 73 20 74 6f 20 69 6e 20  sr points to in 
1a766 74 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20  tname. */.      
1a767 74 6e 61 6d 65 2e 7a 20 3d 20 7a 43 73 72 3b 0a  tname.z = zCsr;.
1a768 20 20 20 20 20 20 74 6e 61 6d 65 2e 6e 20 3d 20        tname.n = 
1a769 6c 65 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 41  len;..      /* A
1a76a 64 76 61 6e 63 65 20 7a 43 73 72 20 74 6f 20 74  dvance zCsr to t
1a76b 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 2e 20 53  he next token. S
1a76c 74 6f 72 65 20 74 68 61 74 20 74 6f 6b 65 6e 20  tore that token 
1a76d 74 79 70 65 20 69 6e 20 27 74 6f 6b 65 6e 27 2c  type in 'token',
1a76e 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 69 74  .      ** and it
1a76f 73 20 6c 65 6e 67 74 68 20 69 6e 20 27 6c 65 6e  s length in 'len
1a770 27 20 28 74 6f 20 62 65 20 75 73 65 64 20 6e 65  ' (to be used ne
1a771 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20  xt iteration of 
1a772 74 68 69 73 20 6c 6f 6f 70 29 2e 0a 20 20 20 20  this loop)..    
1a773 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f 20 7b 0a    */.      do {.
1a774 20 20 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20          zCsr += 
1a775 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e  len;.        len
1a776 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b   = sqlite3GetTok
1a777 65 6e 28 7a 43 73 72 2c 20 26 74 6f 6b 65 6e 29  en(zCsr, &token)
1a778 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20  ;.      }while( 
1a779 74 6f 6b 65 6e 3d 3d 54 4b 5f 53 50 41 43 45 20  token==TK_SPACE 
1a77a 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
1a77b 20 6c 65 6e 3e 30 20 29 3b 0a 0a 20 20 20 20 20   len>0 );..     
1a77c 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 27 64 69   /* Variable 'di
1a77d 73 74 27 20 73 74 6f 72 65 73 20 74 68 65 20 6e  st' stores the n
1a77e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20  umber of tokens 
1a77f 72 65 61 64 20 73 69 6e 63 65 20 74 68 65 20 6d  read since the m
1a780 6f 73 74 0a 20 20 20 20 20 20 2a 2a 20 72 65 63  ost.      ** rec
1a781 65 6e 74 20 54 4b 5f 44 4f 54 20 6f 72 20 54 4b  ent TK_DOT or TK
1a782 5f 4f 4e 2e 20 54 68 69 73 20 6d 65 61 6e 73 20  _ON. This means 
1a783 74 68 61 74 20 77 68 65 6e 20 61 20 57 48 45 4e  that when a WHEN
1a784 2c 20 46 4f 52 20 6f 72 20 42 45 47 49 4e 20 0a  , FOR or BEGIN .
1a785 20 20 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 20 69        ** token i
1a786 73 20 72 65 61 64 20 61 6e 64 20 27 64 69 73 74  s read and 'dist
1a787 27 20 65 71 75 61 6c 73 20 32 2c 20 74 68 65 20  ' equals 2, the 
1a788 63 6f 6e 64 69 74 69 6f 6e 20 73 74 61 74 65 64  condition stated
1a789 20 61 62 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20   above.      ** 
1a78a 74 6f 20 62 65 20 6d 65 74 2e 0a 20 20 20 20 20  to be met..     
1a78b 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 74   **.      ** Not
1a78c 65 20 74 68 61 74 20 4f 4e 20 63 61 6e 6e 6f 74  e that ON cannot
1a78d 20 62 65 20 61 20 64 61 74 61 62 61 73 65 2c 20   be a database, 
1a78e 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 20  table or column 
1a78f 6e 61 6d 65 2c 20 73 6f 0a 20 20 20 20 20 20 2a  name, so.      *
1a790 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65  * there is no ne
1a791 65 64 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75  ed to worry abou
1a792 74 20 73 79 6e 74 61 78 20 6c 69 6b 65 20 0a 20  t syntax like . 
1a793 20 20 20 20 20 2a 2a 20 22 43 52 45 41 54 45 20       ** "CREATE 
1a794 54 52 49 47 47 45 52 20 2e 2e 2e 20 4f 4e 20 4f  TRIGGER ... ON O
1a795 4e 2e 4f 4e 20 42 45 47 49 4e 20 2e 2e 2e 22 20  N.ON BEGIN ..." 
1a796 65 74 63 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  etc..      */.  
1a797 20 20 20 20 64 69 73 74 2b 2b 3b 0a 20 20 20 20      dist++;.    
1a798 20 20 69 66 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f    if( token==TK_
1a799 44 4f 54 20 7c 7c 20 74 6f 6b 65 6e 3d 3d 54 4b  DOT || token==TK
1a79a 5f 4f 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 64  _ON ){.        d
1a79b 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  ist = 0;.      }
1a79c 0a 20 20 20 20 7d 20 77 68 69 6c 65 28 20 64 69  .    } while( di
1a79d 73 74 21 3d 32 20 7c 7c 20 28 74 6f 6b 65 6e 21  st!=2 || (token!
1a79e 3d 54 4b 5f 57 48 45 4e 20 26 26 20 74 6f 6b 65  =TK_WHEN && toke
1a79f 6e 21 3d 54 4b 5f 46 4f 52 20 26 26 20 74 6f 6b  n!=TK_FOR && tok
1a7a0 65 6e 21 3d 54 4b 5f 42 45 47 49 4e 29 20 29 3b  en!=TK_BEGIN) );
1a7a1 0a 0a 20 20 20 20 2f 2a 20 56 61 72 69 61 62 6c  ..    /* Variabl
1a7a2 65 20 74 6e 61 6d 65 20 6e 6f 77 20 63 6f 6e 74  e tname now cont
1a7a3 61 69 6e 73 20 74 68 65 20 74 6f 6b 65 6e 20 74  ains the token t
1a7a4 68 61 74 20 69 73 20 74 68 65 20 6f 6c 64 20 74  hat is the old t
1a7a5 61 62 6c 65 2d 6e 61 6d 65 0a 20 20 20 20 2a 2a  able-name.    **
1a7a6 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54   in the CREATE T
1a7a7 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74  RIGGER statement
1a7a8 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 52 65  ..    */.    zRe
1a7a9 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  t = sqlite3MPrin
1a7aa 74 66 28 64 62 2c 20 22 25 2e 2a 73 5c 22 25 77  tf(db, "%.*s\"%w
1a7ab 5c 22 25 73 22 2c 20 74 6e 61 6d 65 2e 7a 20 2d  \"%s", tname.z -
1a7ac 20 7a 53 71 6c 2c 20 7a 53 71 6c 2c 20 0a 20 20   zSql, zSql, .  
1a7ad 20 20 20 20 20 7a 54 61 62 6c 65 4e 61 6d 65 2c       zTableName,
1a7ae 20 74 6e 61 6d 65 2e 7a 2b 74 6e 61 6d 65 2e 6e   tname.z+tname.n
1a7af 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
1a7b0 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
1a7b1 78 74 2c 20 7a 52 65 74 2c 20 2d 31 2c 20 73 71  xt, zRet, -1, sq
1a7b2 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 7d  lite3_free);.  }
1a7b3 0a 7d 0a 23 65 6e 64 69 66 20 20 20 2f 2a 20 21  .}.#endif   /* !
1a7b4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
1a7b5 47 45 52 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65  GER */../*.** Re
1a7b6 67 69 73 74 65 72 20 62 75 69 6c 74 2d 69 6e 20  gister built-in 
1a7b7 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74  functions used t
1a7b8 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65 6e 74  o help implement
1a7b9 20 41 4c 54 45 52 20 54 41 42 4c 45 0a 2a 2f 0a   ALTER TABLE.*/.
1a7ba 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1a7bb 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72  oid sqlite3Alter
1a7bc 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65  Functions(sqlite
1a7bd 33 20 2a 64 62 29 7b 0a 20 20 73 74 61 74 69 63  3 *db){.  static
1a7be 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 7b 0a   const struct {.
1a7bf 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65       char *zName
1a7c0 3b 0a 20 20 20 20 20 73 69 67 6e 65 64 20 63 68  ;.     signed ch
1a7c1 61 72 20 6e 41 72 67 3b 0a 20 20 20 20 20 76 6f  ar nArg;.     vo
1a7c2 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69  id (*xFunc)(sqli
1a7c3 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
1a7c4 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  ,sqlite3_value *
1a7c5 2a 29 3b 0a 20 20 7d 20 61 46 75 6e 63 73 5b 5d  *);.  } aFuncs[]
1a7c6 20 3d 20 7b 0a 20 20 20 20 7b 20 22 73 71 6c 69   = {.    { "sqli
1a7c7 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 22  te_rename_table"
1a7c8 2c 20 20 20 20 32 2c 20 72 65 6e 61 6d 65 54 61  ,    2, renameTa
1a7c9 62 6c 65 46 75 6e 63 7d 2c 0a 23 69 66 6e 64 65  bleFunc},.#ifnde
1a7ca 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
1a7cb 49 47 47 45 52 0a 20 20 20 20 7b 20 22 73 71 6c  IGGER.    { "sql
1a7cc 69 74 65 5f 72 65 6e 61 6d 65 5f 74 72 69 67 67  ite_rename_trigg
1a7cd 65 72 22 2c 20 20 32 2c 20 72 65 6e 61 6d 65 54  er",  2, renameT
1a7ce 72 69 67 67 65 72 46 75 6e 63 7d 2c 0a 23 65 6e  riggerFunc},.#en
1a7cf 64 69 66 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69  dif.  };.  int i
1a7d0 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  ;..  for(i=0; i<
1a7d1 73 69 7a 65 6f 66 28 61 46 75 6e 63 73 29 2f 73  sizeof(aFuncs)/s
1a7d2 69 7a 65 6f 66 28 61 46 75 6e 63 73 5b 30 5d 29  izeof(aFuncs[0])
1a7d3 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69  ; i++){.    sqli
1a7d4 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62  te3CreateFunc(db
1a7d5 2c 20 61 46 75 6e 63 73 5b 69 5d 2e 7a 4e 61 6d  , aFuncs[i].zNam
1a7d6 65 2c 20 61 46 75 6e 63 73 5b 69 5d 2e 6e 41 72  e, aFuncs[i].nAr
1a7d7 67 2c 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54  g,.        SQLIT
1a7d8 45 5f 55 54 46 38 2c 20 30 2c 20 61 46 75 6e 63  E_UTF8, 0, aFunc
1a7d9 73 5b 69 5d 2e 78 46 75 6e 63 2c 20 30 2c 20 30  s[i].xFunc, 0, 0
1a7da 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
1a7db 47 65 6e 65 72 61 74 65 20 74 68 65 20 74 65 78  Generate the tex
1a7dc 74 20 6f 66 20 61 20 57 48 45 52 45 20 65 78 70  t of a WHERE exp
1a7dd 72 65 73 73 69 6f 6e 20 77 68 69 63 68 20 63 61  ression which ca
1a7de 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 6c  n be used to sel
1a7df 65 63 74 20 61 6c 6c 0a 2a 2a 20 74 65 6d 70 6f  ect all.** tempo
1a7e0 72 61 72 79 20 74 72 69 67 67 65 72 73 20 6f 6e  rary triggers on
1a7e1 20 74 61 62 6c 65 20 70 54 61 62 20 66 72 6f 6d   table pTab from
1a7e2 20 74 68 65 20 73 71 6c 69 74 65 5f 74 65 6d 70   the sqlite_temp
1a7e3 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20 49  _master table. I
1a7e4 66 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 20  f.** table pTab 
1a7e5 68 61 73 20 6e 6f 20 74 65 6d 70 6f 72 61 72 79  has no temporary
1a7e6 20 74 72 69 67 67 65 72 73 2c 20 6f 72 20 69 73   triggers, or is
1a7e7 20 69 74 73 65 6c 66 20 73 74 6f 72 65 64 20 69   itself stored i
1a7e8 6e 20 74 68 65 20 0a 2a 2a 20 74 65 6d 70 6f 72  n the .** tempor
1a7e9 61 72 79 20 64 61 74 61 62 61 73 65 2c 20 4e 55  ary database, NU
1a7ea 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  LL is returned..
1a7eb 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a  */.static char *
1a7ec 77 68 65 72 65 54 65 6d 70 54 72 69 67 67 65 72  whereTempTrigger
1a7ed 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  s(Parse *pParse,
1a7ee 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20   Table *pTab){. 
1a7ef 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b   Trigger *pTrig;
1a7f0 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65 20  .  char *zWhere 
1a7f1 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 74 6d 70  = 0;.  char *tmp
1a7f2 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 53 63   = 0;.  const Sc
1a7f3 68 65 6d 61 20 2a 70 54 65 6d 70 53 63 68 65 6d  hema *pTempSchem
1a7f4 61 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  a = pParse->db->
1a7f5 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 3b 20  aDb[1].pSchema; 
1a7f6 2f 2a 20 54 65 6d 70 20 64 62 20 73 63 68 65 6d  /* Temp db schem
1a7f7 61 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68  a */..  /* If th
1a7f8 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 6c  e table is not l
1a7f9 6f 63 61 74 65 64 20 69 6e 20 74 68 65 20 74 65  ocated in the te
1a7fa 6d 70 2d 64 62 20 28 69 6e 20 77 68 69 63 68 20  mp-db (in which 
1a7fb 63 61 73 65 20 4e 55 4c 4c 20 69 73 20 0a 20 20  case NULL is .  
1a7fc 2a 2a 20 72 65 74 75 72 6e 65 64 2c 20 6c 6f 6f  ** returned, loo
1a7fd 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 61  p through the ta
1a7fe 62 6c 65 73 20 6c 69 73 74 20 6f 66 20 74 72 69  bles list of tri
1a7ff 67 67 65 72 73 2e 20 46 6f 72 20 65 61 63 68 20  ggers. For each 
1a800 74 72 69 67 67 65 72 0a 20 20 2a 2a 20 74 68 61  trigger.  ** tha
1a801 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66  t is not part of
1a802 20 74 68 65 20 74 65 6d 70 2d 64 62 20 73 63 68   the temp-db sch
1a803 65 6d 61 2c 20 61 64 64 20 61 20 63 6c 61 75 73  ema, add a claus
1a804 65 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 0a  e to the WHERE .
1a805 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20    ** expression 
1a806 62 65 69 6e 67 20 62 75 69 6c 74 20 75 70 20 69  being built up i
1a807 6e 20 7a 57 68 65 72 65 2e 0a 20 20 2a 2f 0a 20  n zWhere..  */. 
1a808 20 69 66 28 20 70 54 61 62 2d 3e 70 53 63 68 65   if( pTab->pSche
1a809 6d 61 21 3d 70 54 65 6d 70 53 63 68 65 6d 61 20  ma!=pTempSchema 
1a80a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a  ){.    sqlite3 *
1a80b 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
1a80c 0a 20 20 20 20 66 6f 72 28 20 70 54 72 69 67 3d  .    for( pTrig=
1a80d 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72 3b 20  pTab->pTrigger; 
1a80e 70 54 72 69 67 3b 20 70 54 72 69 67 3d 70 54 72  pTrig; pTrig=pTr
1a80f 69 67 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20  ig->pNext ){.   
1a810 20 20 20 69 66 28 20 70 54 72 69 67 2d 3e 70 53     if( pTrig->pS
1a811 63 68 65 6d 61 3d 3d 70 54 65 6d 70 53 63 68 65  chema==pTempSche
1a812 6d 61 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  ma ){.        if
1a813 28 20 21 7a 57 68 65 72 65 20 29 7b 0a 20 20 20  ( !zWhere ){.   
1a814 20 20 20 20 20 20 20 7a 57 68 65 72 65 20 3d 20         zWhere = 
1a815 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
1a816 62 2c 20 22 6e 61 6d 65 3d 25 51 22 2c 20 70 54  b, "name=%Q", pT
1a817 72 69 67 2d 3e 6e 61 6d 65 29 3b 0a 20 20 20 20  rig->name);.    
1a818 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1a819 20 20 20 20 20 74 6d 70 20 3d 20 7a 57 68 65 72       tmp = zWher
1a81a 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 57 68  e;.          zWh
1a81b 65 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  ere = sqlite3MPr
1a81c 69 6e 74 66 28 64 62 2c 20 22 25 73 20 4f 52 20  intf(db, "%s OR 
1a81d 6e 61 6d 65 3d 25 51 22 2c 20 7a 57 68 65 72 65  name=%Q", zWhere
1a81e 2c 20 70 54 72 69 67 2d 3e 6e 61 6d 65 29 3b 0a  , pTrig->name);.
1a81f 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1a820 33 5f 66 72 65 65 28 74 6d 70 29 3b 0a 20 20 20  3_free(tmp);.   
1a821 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
1a822 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
1a823 6e 20 7a 57 68 65 72 65 3b 0a 7d 0a 0a 2f 2a 0a  n zWhere;.}../*.
1a824 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
1a825 20 74 6f 20 64 72 6f 70 20 61 6e 64 20 72 65 6c   to drop and rel
1a826 6f 61 64 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  oad the internal
1a827 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
1a828 6f 66 20 74 61 62 6c 65 0a 2a 2a 20 70 54 61 62  of table.** pTab
1a829 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61   from the databa
1a82a 73 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20 74 72  se, including tr
1a82b 69 67 67 65 72 73 20 61 6e 64 20 74 65 6d 70 6f  iggers and tempo
1a82c 72 61 72 79 20 74 72 69 67 67 65 72 73 2e 0a 2a  rary triggers..*
1a82d 2a 20 41 72 67 75 6d 65 6e 74 20 7a 4e 61 6d 65  * Argument zName
1a82e 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
1a82f 74 68 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65  the table in the
1a830 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
1a831 20 61 74 0a 2a 2a 20 74 68 65 20 74 69 6d 65 20   at.** the time 
1a832 74 68 65 20 67 65 6e 65 72 61 74 65 64 20 63 6f  the generated co
1a833 64 65 20 69 73 20 65 78 65 63 75 74 65 64 2e 20  de is executed. 
1a834 54 68 69 73 20 63 61 6e 20 62 65 20 64 69 66 66  This can be diff
1a835 65 72 65 6e 74 20 66 72 6f 6d 0a 2a 2a 20 70 54  erent from.** pT
1a836 61 62 2d 3e 7a 4e 61 6d 65 20 69 66 20 74 68 69  ab->zName if thi
1a837 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65  s function is be
1a838 69 6e 67 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f  ing called to co
1a839 64 65 20 70 61 72 74 20 6f 66 20 61 6e 20 0a 2a  de part of an .*
1a83a 2a 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 52  * "ALTER TABLE R
1a83b 45 4e 41 4d 45 20 54 4f 22 20 73 74 61 74 65 6d  ENAME TO" statem
1a83c 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ent..*/.static v
1a83d 6f 69 64 20 72 65 6c 6f 61 64 54 61 62 6c 65 53  oid reloadTableS
1a83e 63 68 65 6d 61 28 50 61 72 73 65 20 2a 70 50 61  chema(Parse *pPa
1a83f 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62  rse, Table *pTab
1a840 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
1a841 61 6d 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b  ame){.  Vdbe *v;
1a842 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65 3b  .  char *zWhere;
1a843 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20  .  int iDb;     
1a844 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a845 20 49 6e 64 65 78 20 6f 66 20 64 61 74 61 62 61   Index of databa
1a846 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54  se containing pT
1a847 61 62 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  ab */.#ifndef SQ
1a848 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
1a849 52 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72  R.  Trigger *pTr
1a84a 69 67 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 76 20  ig;.#endif..  v 
1a84b 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
1a84c 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
1a84d 21 76 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61  !v ) return;.  a
1a84e 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
1a84f 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78  reeHoldsAllMutex
1a850 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20 29  es(pParse->db) )
1a851 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  ;.  iDb = sqlite
1a852 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
1a853 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d  Parse->db, pTab-
1a854 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73 73  >pSchema);.  ass
1a855 65 72 74 28 20 69 44 62 3e 3d 30 20 29 3b 0a 0a  ert( iDb>=0 );..
1a856 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1a857 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a  MIT_TRIGGER.  /*
1a858 20 44 72 6f 70 20 61 6e 79 20 74 61 62 6c 65 20   Drop any table 
1a859 74 72 69 67 67 65 72 73 20 66 72 6f 6d 20 74 68  triggers from th
1a85a 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d  e internal schem
1a85b 61 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 54 72 69  a. */.  for(pTri
1a85c 67 3d 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72  g=pTab->pTrigger
1a85d 3b 20 70 54 72 69 67 3b 20 70 54 72 69 67 3d 70  ; pTrig; pTrig=p
1a85e 54 72 69 67 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  Trig->pNext){.  
1a85f 20 20 69 6e 74 20 69 54 72 69 67 44 62 20 3d 20    int iTrigDb = 
1a860 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
1a861 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c  ndex(pParse->db,
1a862 20 70 54 72 69 67 2d 3e 70 53 63 68 65 6d 61 29   pTrig->pSchema)
1a863 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 54  ;.    assert( iT
1a864 72 69 67 44 62 3d 3d 69 44 62 20 7c 7c 20 69 54  rigDb==iDb || iT
1a865 72 69 67 44 62 3d 3d 31 20 29 3b 0a 20 20 20 20  rigDb==1 );.    
1a866 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1a867 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 72 69 67  4(v, OP_DropTrig
1a868 67 65 72 2c 20 69 54 72 69 67 44 62 2c 20 30 2c  ger, iTrigDb, 0,
1a869 20 30 2c 20 70 54 72 69 67 2d 3e 6e 61 6d 65 2c   0, pTrig->name,
1a86a 20 30 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a   0);.  }.#endif.
1a86b 0a 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 74  .  /* Drop the t
1a86c 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 66  able and index f
1a86d 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  rom the internal
1a86e 20 73 63 68 65 6d 61 20 2a 2f 0a 20 20 73 71 6c   schema */.  sql
1a86f 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
1a870 2c 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 2c 20  , OP_DropTable, 
1a871 69 44 62 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d  iDb, 0, 0, pTab-
1a872 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 0a 20 20 2f  >zName, 0);..  /
1a873 2a 20 52 65 6c 6f 61 64 20 74 68 65 20 74 61 62  * Reload the tab
1a874 6c 65 2c 20 69 6e 64 65 78 20 61 6e 64 20 70 65  le, index and pe
1a875 72 6d 61 6e 65 6e 74 20 74 72 69 67 67 65 72 20  rmanent trigger 
1a876 73 63 68 65 6d 61 73 2e 20 2a 2f 0a 20 20 7a 57  schemas. */.  zW
1a877 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 50  here = sqlite3MP
1a878 72 69 6e 74 66 28 70 50 61 72 73 65 2d 3e 64 62  rintf(pParse->db
1a879 2c 20 22 74 62 6c 5f 6e 61 6d 65 3d 25 51 22 2c  , "tbl_name=%Q",
1a87a 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21   zName);.  if( !
1a87b 7a 57 68 65 72 65 20 29 20 72 65 74 75 72 6e 3b  zWhere ) return;
1a87c 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
1a87d 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 73 65  dOp4(v, OP_Parse
1a87e 53 63 68 65 6d 61 2c 20 69 44 62 2c 20 30 2c 20  Schema, iDb, 0, 
1a87f 30 2c 20 7a 57 68 65 72 65 2c 20 50 34 5f 44 59  0, zWhere, P4_DY
1a880 4e 41 4d 49 43 29 3b 0a 0a 23 69 66 6e 64 65 66  NAMIC);..#ifndef
1a881 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
1a882 47 47 45 52 0a 20 20 2f 2a 20 4e 6f 77 2c 20 69  GGER.  /* Now, i
1a883 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 6e  f the table is n
1a884 6f 74 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  ot stored in the
1a885 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 2c 20   temp database, 
1a886 72 65 6c 6f 61 64 20 61 6e 79 20 74 65 6d 70 20  reload any temp 
1a887 0a 20 20 2a 2a 20 74 72 69 67 67 65 72 73 2e 20  .  ** triggers. 
1a888 44 6f 6e 27 74 20 75 73 65 20 49 4e 28 2e 2e 2e  Don't use IN(...
1a889 29 20 69 6e 20 63 61 73 65 20 53 51 4c 49 54 45  ) in case SQLITE
1a88a 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 69  _OMIT_SUBQUERY i
1a88b 73 20 64 65 66 69 6e 65 64 2e 20 0a 20 20 2a 2f  s defined. .  */
1a88c 0a 20 20 69 66 28 20 28 7a 57 68 65 72 65 3d 77  .  if( (zWhere=w
1a88d 68 65 72 65 54 65 6d 70 54 72 69 67 67 65 72 73  hereTempTriggers
1a88e 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 29 21  (pParse, pTab))!
1a88f 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
1a890 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
1a891 50 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20 31  P_ParseSchema, 1
1a892 2c 20 30 2c 20 30 2c 20 7a 57 68 65 72 65 2c 20  , 0, 0, zWhere, 
1a893 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d  P4_DYNAMIC);.  }
1a894 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
1a895 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
1a896 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  o implement the 
1a897 22 41 4c 54 45 52 20 54 41 42 4c 45 20 78 78 78  "ALTER TABLE xxx
1a898 20 52 45 4e 41 4d 45 20 54 4f 20 79 79 79 22 20   RENAME TO yyy" 
1a899 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2e 20 0a 2a 2f  .** command. .*/
1a89a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a89b 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65  void sqlite3Alte
1a89c 72 52 65 6e 61 6d 65 54 61 62 6c 65 28 0a 20 20  rRenameTable(.  
1a89d 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
1a89e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
1a89f 73 65 72 20 63 6f 6e 74 65 78 74 2e 20 2a 2f 0a  ser context. */.
1a8a0 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c    SrcList *pSrc,
1a8a1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1a8a2 68 65 20 74 61 62 6c 65 20 74 6f 20 72 65 6e 61  he table to rena
1a8a3 6d 65 2e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  me. */.  Token *
1a8a4 70 4e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20  pName           
1a8a5 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 74 61     /* The new ta
1a8a6 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f 0a 29 7b 0a  ble name. */.){.
1a8a7 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
1a8a8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
1a8a9 61 74 61 62 61 73 65 20 74 68 61 74 20 63 6f 6e  atabase that con
1a8aa 74 61 69 6e 73 20 74 68 65 20 74 61 62 6c 65 20  tains the table 
1a8ab 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44 62 3b 20  */.  char *zDb; 
1a8ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1a8ad 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61  * Name of databa
1a8ae 73 65 20 69 44 62 20 2a 2f 0a 20 20 54 61 62 6c  se iDb */.  Tabl
1a8af 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20  e *pTab;        
1a8b0 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 62        /* Table b
1a8b1 65 69 6e 67 20 72 65 6e 61 6d 65 64 20 2a 2f 0a  eing renamed */.
1a8b2 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20    char *zName = 
1a8b3 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  0;          /* N
1a8b4 55 4c 4c 2d 74 65 72 6d 69 6e 61 74 65 64 20 76  ULL-terminated v
1a8b5 65 72 73 69 6f 6e 20 6f 66 20 70 4e 61 6d 65 20  ersion of pName 
1a8b6 2a 2f 20 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  */ .  sqlite3 *d
1a8b7 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20  b = pParse->db; 
1a8b8 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e  /* Database conn
1a8b9 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ection */.  int 
1a8ba 6e 54 61 62 4e 61 6d 65 3b 20 20 20 20 20 20 20  nTabName;       
1a8bb 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1a8bc 6f 66 20 55 54 46 2d 38 20 63 68 61 72 61 63 74  of UTF-8 charact
1a8bd 65 72 73 20 69 6e 20 7a 54 61 62 4e 61 6d 65 20  ers in zTabName 
1a8be 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
1a8bf 2a 7a 54 61 62 4e 61 6d 65 3b 20 20 20 20 20 2f  *zTabName;     /
1a8c0 2a 20 4f 72 69 67 69 6e 61 6c 20 6e 61 6d 65 20  * Original name 
1a8c1 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  of the table */.
1a8c2 20 20 56 64 62 65 20 2a 76 3b 0a 23 69 66 6e 64    Vdbe *v;.#ifnd
1a8c3 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
1a8c4 52 49 47 47 45 52 0a 20 20 63 68 61 72 20 2a 7a  RIGGER.  char *z
1a8c5 57 68 65 72 65 20 3d 20 30 3b 20 20 20 20 20 20  Where = 0;      
1a8c6 20 20 20 2f 2a 20 57 68 65 72 65 20 63 6c 61 75     /* Where clau
1a8c7 73 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 65 6d  se to locate tem
1a8c8 70 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 23 65  p triggers */.#e
1a8c9 6e 64 69 66 0a 20 20 69 6e 74 20 69 73 56 69 72  ndif.  int isVir
1a8ca 74 75 61 6c 52 65 6e 61 6d 65 20 3d 20 30 3b 20  tualRename = 0; 
1a8cb 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73   /* True if this
1a8cc 20 69 73 20 61 20 76 2d 74 61 62 6c 65 20 77 69   is a v-table wi
1a8cd 74 68 20 61 6e 20 78 52 65 6e 61 6d 65 28 29 20  th an xRename() 
1a8ce 2a 2f 0a 20 20 0a 20 20 69 66 28 20 64 62 2d 3e  */.  .  if( db->
1a8cf 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67  mallocFailed ) g
1a8d0 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f  oto exit_rename_
1a8d1 74 61 62 6c 65 3b 0a 20 20 61 73 73 65 72 74 28  table;.  assert(
1a8d2 20 70 53 72 63 2d 3e 6e 53 72 63 3d 3d 31 20 29   pSrc->nSrc==1 )
1a8d3 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
1a8d4 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c  te3BtreeHoldsAll
1a8d5 4d 75 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e  Mutexes(pParse->
1a8d6 64 62 29 20 29 3b 0a 0a 20 20 70 54 61 62 20 3d  db) );..  pTab =
1a8d7 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61   sqlite3LocateTa
1a8d8 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70  ble(pParse, 0, p
1a8d9 53 72 63 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c  Src->a[0].zName,
1a8da 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 44 61 74   pSrc->a[0].zDat
1a8db 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 21 70  abase);.  if( !p
1a8dc 54 61 62 20 29 20 67 6f 74 6f 20 65 78 69 74 5f  Tab ) goto exit_
1a8dd 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20  rename_table;.  
1a8de 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
1a8df 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73  emaToIndex(pPars
1a8e0 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  e->db, pTab->pSc
1a8e1 68 65 6d 61 29 3b 0a 20 20 7a 44 62 20 3d 20 64  hema);.  zDb = d
1a8e2 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
1a8e3 65 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 61 20 4e  e;..  /* Get a N
1a8e4 55 4c 4c 20 74 65 72 6d 69 6e 61 74 65 64 20 76  ULL terminated v
1a8e5 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6e 65  ersion of the ne
1a8e6 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f  w table name. */
1a8e7 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  .  zName = sqlit
1a8e8 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
1a8e9 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66  db, pName);.  if
1a8ea 28 20 21 7a 4e 61 6d 65 20 29 20 67 6f 74 6f 20  ( !zName ) goto 
1a8eb 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c  exit_rename_tabl
1a8ec 65 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74  e;..  /* Check t
1a8ed 68 61 74 20 61 20 74 61 62 6c 65 20 6f 72 20 69  hat a table or i
1a8ee 6e 64 65 78 20 6e 61 6d 65 64 20 27 7a 4e 61 6d  ndex named 'zNam
1a8ef 65 27 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65  e' does not alre
1a8f0 61 64 79 20 65 78 69 73 74 0a 20 20 2a 2a 20 69  ady exist.  ** i
1a8f1 6e 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 20  n database iDb. 
1a8f2 49 66 20 73 6f 2c 20 74 68 69 73 20 69 73 20 61  If so, this is a
1a8f3 6e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20  n error..  */.  
1a8f4 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54  if( sqlite3FindT
1a8f5 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  able(db, zName, 
1a8f6 7a 44 62 29 20 7c 7c 20 73 71 6c 69 74 65 33 46  zDb) || sqlite3F
1a8f7 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61  indIndex(db, zNa
1a8f8 6d 65 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20  me, zDb) ){.    
1a8f9 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1a8fa 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20  pParse, .       
1a8fb 20 22 74 68 65 72 65 20 69 73 20 61 6c 72 65 61   "there is alrea
1a8fc 64 79 20 61 6e 6f 74 68 65 72 20 74 61 62 6c 65  dy another table
1a8fd 20 6f 72 20 69 6e 64 65 78 20 77 69 74 68 20 74   or index with t
1a8fe 68 69 73 20 6e 61 6d 65 3a 20 25 73 22 2c 20 7a  his name: %s", z
1a8ff 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20  Name);.    goto 
1a900 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c  exit_rename_tabl
1a901 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b  e;.  }..  /* Mak
1a902 65 20 73 75 72 65 20 69 74 20 69 73 20 6e 6f 74  e sure it is not
1a903 20 61 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20   a system table 
1a904 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2c 20 6f  being altered, o
1a905 72 20 61 20 72 65 73 65 72 76 65 64 20 6e 61 6d  r a reserved nam
1a906 65 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20  e.  ** that the 
1a907 74 61 62 6c 65 20 69 73 20 62 65 69 6e 67 20 72  table is being r
1a908 65 6e 61 6d 65 64 20 74 6f 2e 0a 20 20 2a 2f 0a  enamed to..  */.
1a909 20 20 69 66 28 20 73 74 72 6c 65 6e 28 70 54 61    if( strlen(pTa
1a90a 62 2d 3e 7a 4e 61 6d 65 29 3e 36 20 26 26 20 30  b->zName)>6 && 0
1a90b 3d 3d 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  ==sqlite3StrNICm
1a90c 70 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22  p(pTab->zName, "
1a90d 73 71 6c 69 74 65 5f 22 2c 20 37 29 20 29 7b 0a  sqlite_", 7) ){.
1a90e 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1a90f 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62  Msg(pParse, "tab
1a910 6c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65  le %s may not be
1a911 20 61 6c 74 65 72 65 64 22 2c 20 70 54 61 62 2d   altered", pTab-
1a912 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74  >zName);.    got
1a913 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  o exit_rename_ta
1a914 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53  ble;.  }.  if( S
1a915 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65  QLITE_OK!=sqlite
1a916 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65  3CheckObjectName
1a917 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20  (pParse, zName) 
1a918 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  ){.    goto exit
1a919 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20  _rename_table;. 
1a91a 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
1a91b 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 69  TE_OMIT_VIEW.  i
1a91c 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  f( pTab->pSelect
1a91d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
1a91e 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1a91f 22 76 69 65 77 20 25 73 20 6d 61 79 20 6e 6f 74  "view %s may not
1a920 20 62 65 20 61 6c 74 65 72 65 64 22 2c 20 70 54   be altered", pT
1a921 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ab->zName);.    
1a922 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65  goto exit_rename
1a923 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64  _table;.  }.#end
1a924 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
1a925 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
1a926 41 54 49 4f 4e 0a 20 20 2f 2a 20 49 6e 76 6f 6b  ATION.  /* Invok
1a927 65 20 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74  e the authorizat
1a928 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f  ion callback. */
1a929 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75  .  if( sqlite3Au
1a92a 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
1a92b 53 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42  SQLITE_ALTER_TAB
1a92c 4c 45 2c 20 7a 44 62 2c 20 70 54 61 62 2d 3e 7a  LE, zDb, pTab->z
1a92d 4e 61 6d 65 2c 20 30 29 20 29 7b 0a 20 20 20 20  Name, 0) ){.    
1a92e 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65  goto exit_rename
1a92f 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64  _table;.  }.#end
1a930 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
1a931 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
1a932 41 42 4c 45 0a 20 20 69 66 28 20 73 71 6c 69 74  ABLE.  if( sqlit
1a933 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e  e3ViewGetColumnN
1a934 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54 61  ames(pParse, pTa
1a935 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65  b) ){.    goto e
1a936 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65  xit_rename_table
1a937 3b 0a 20 20 7d 0a 20 20 69 66 28 20 49 73 56 69  ;.  }.  if( IsVi
1a938 72 74 75 61 6c 28 70 54 61 62 29 20 26 26 20 70  rtual(pTab) && p
1a939 54 61 62 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f 64 75  Tab->pMod->pModu
1a93a 6c 65 2d 3e 78 52 65 6e 61 6d 65 20 29 7b 0a 20  le->xRename ){. 
1a93b 20 20 20 69 73 56 69 72 74 75 61 6c 52 65 6e 61     isVirtualRena
1a93c 6d 65 20 3d 20 31 3b 0a 20 20 7d 0a 23 65 6e 64  me = 1;.  }.#end
1a93d 69 66 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61  if..  /* Begin a
1a93e 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64   transaction and
1a93f 20 63 6f 64 65 20 74 68 65 20 56 65 72 69 66 79   code the Verify
1a940 43 6f 6f 6b 69 65 20 66 6f 72 20 64 61 74 61 62  Cookie for datab
1a941 61 73 65 20 69 44 62 2e 20 0a 20 20 2a 2a 20 54  ase iDb. .  ** T
1a942 68 65 6e 20 6d 6f 64 69 66 79 20 74 68 65 20 73  hen modify the s
1a943 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 28 73 69  chema cookie (si
1a944 6e 63 65 20 74 68 65 20 41 4c 54 45 52 20 54 41  nce the ALTER TA
1a945 42 4c 45 20 6d 6f 64 69 66 69 65 73 20 74 68 65  BLE modifies the
1a946 0a 20 20 2a 2a 20 73 63 68 65 6d 61 29 2e 20 4f  .  ** schema). O
1a947 70 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20  pen a statement 
1a948 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 74  transaction if t
1a949 68 65 20 74 61 62 6c 65 20 69 73 20 61 20 76 69  he table is a vi
1a94a 72 74 75 61 6c 0a 20 20 2a 2a 20 74 61 62 6c 65  rtual.  ** table
1a94b 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c  ..  */.  v = sql
1a94c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
1a94d 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20  se);.  if( v==0 
1a94e 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  ){.    goto exit
1a94f 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20  _rename_table;. 
1a950 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69   }.  sqlite3Begi
1a951 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
1a952 70 50 61 72 73 65 2c 20 69 73 56 69 72 74 75 61  pParse, isVirtua
1a953 6c 52 65 6e 61 6d 65 2c 20 69 44 62 29 3b 0a 20  lRename, iDb);. 
1a954 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f   sqlite3ChangeCo
1a955 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44 62  okie(pParse, iDb
1a956 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73  );..  /* If this
1a957 20 69 73 20 61 20 76 69 72 74 75 61 6c 20 74 61   is a virtual ta
1a958 62 6c 65 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20  ble, invoke the 
1a959 78 52 65 6e 61 6d 65 28 29 20 66 75 6e 63 74 69  xRename() functi
1a95a 6f 6e 20 69 66 0a 20 20 2a 2a 20 6f 6e 65 20 69  on if.  ** one i
1a95b 73 20 64 65 66 69 6e 65 64 2e 20 54 68 65 20 78  s defined. The x
1a95c 52 65 6e 61 6d 65 28 29 20 63 61 6c 6c 62 61 63  Rename() callbac
1a95d 6b 20 77 69 6c 6c 20 6d 6f 64 69 66 79 20 74 68  k will modify th
1a95e 65 20 6e 61 6d 65 73 0a 20 20 2a 2a 20 6f 66 20  e names.  ** of 
1a95f 61 6e 79 20 72 65 73 6f 75 72 63 65 73 20 75 73  any resources us
1a960 65 64 20 62 79 20 74 68 65 20 76 2d 74 61 62 6c  ed by the v-tabl
1a961 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
1a962 20 28 69 6e 63 6c 75 64 69 6e 67 20 6f 74 68 65   (including othe
1a963 72 0a 20 20 2a 2a 20 53 51 4c 69 74 65 20 74 61  r.  ** SQLite ta
1a964 62 6c 65 73 29 20 74 68 61 74 20 61 72 65 20 69  bles) that are i
1a965 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65  dentified by the
1a966 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 76 69 72   name of the vir
1a967 74 75 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f  tual table..  */
1a968 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1a969 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1a96a 45 0a 20 20 69 66 28 20 69 73 56 69 72 74 75 61  E.  if( isVirtua
1a96b 6c 52 65 6e 61 6d 65 20 29 7b 0a 20 20 20 20 69  lRename ){.    i
1a96c 6e 74 20 69 20 3d 20 2b 2b 70 50 61 72 73 65 2d  nt i = ++pParse-
1a96d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74  >nMem;.    sqlit
1a96e 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
1a96f 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 69  OP_String8, 0, i
1a970 2c 20 30 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b 0a  , 0, zName, 0);.
1a971 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1a972 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 52 65 6e  ddOp4(v, OP_VRen
1a973 61 6d 65 2c 20 69 2c 20 30 2c 20 30 2c 28 63 6f  ame, i, 0, 0,(co
1a974 6e 73 74 20 63 68 61 72 2a 29 70 54 61 62 2d 3e  nst char*)pTab->
1a975 70 56 74 61 62 2c 20 50 34 5f 56 54 41 42 29 3b  pVtab, P4_VTAB);
1a976 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
1a977 2a 20 66 69 67 75 72 65 20 6f 75 74 20 68 6f 77  * figure out how
1a978 20 6d 61 6e 79 20 55 54 46 2d 38 20 63 68 61 72   many UTF-8 char
1a979 61 63 74 65 72 73 20 61 72 65 20 69 6e 20 7a 4e  acters are in zN
1a97a 61 6d 65 20 2a 2f 0a 20 20 7a 54 61 62 4e 61 6d  ame */.  zTabNam
1a97b 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b  e = pTab->zName;
1a97c 0a 20 20 6e 54 61 62 4e 61 6d 65 20 3d 20 73 71  .  nTabName = sq
1a97d 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e  lite3Utf8CharLen
1a97e 28 7a 54 61 62 4e 61 6d 65 2c 20 2d 31 29 3b 0a  (zTabName, -1);.
1a97f 0a 20 20 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65  .  /* Modify the
1a980 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
1a981 61 62 6c 65 20 74 6f 20 75 73 65 20 74 68 65 20  able to use the 
1a982 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20  new table name. 
1a983 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4e 65 73 74  */.  sqlite3Nest
1a984 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a  edParse(pParse,.
1a985 20 20 20 20 20 20 22 55 50 44 41 54 45 20 25 51        "UPDATE %Q
1a986 2e 25 73 20 53 45 54 20 22 0a 23 69 66 64 65 66  .%s SET ".#ifdef
1a987 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
1a988 47 47 45 52 0a 20 20 20 20 20 20 20 20 20 20 22  GGER.          "
1a989 73 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e  sql = sqlite_ren
1a98a 61 6d 65 5f 74 61 62 6c 65 28 73 71 6c 2c 20 25  ame_table(sql, %
1a98b 51 29 2c 20 22 0a 23 65 6c 73 65 0a 20 20 20 20  Q), ".#else.    
1a98c 20 20 20 20 20 20 22 73 71 6c 20 3d 20 43 41 53        "sql = CAS
1a98d 45 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  E ".            
1a98e 22 57 48 45 4e 20 74 79 70 65 20 3d 20 27 74 72  "WHEN type = 'tr
1a98f 69 67 67 65 72 27 20 54 48 45 4e 20 73 71 6c 69  igger' THEN sqli
1a990 74 65 5f 72 65 6e 61 6d 65 5f 74 72 69 67 67 65  te_rename_trigge
1a991 72 28 73 71 6c 2c 20 25 51 29 22 0a 20 20 20 20  r(sql, %Q)".    
1a992 20 20 20 20 20 20 20 20 22 45 4c 53 45 20 73 71          "ELSE sq
1a993 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c  lite_rename_tabl
1a994 65 28 73 71 6c 2c 20 25 51 29 20 45 4e 44 2c 20  e(sql, %Q) END, 
1a995 22 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  ".#endif.       
1a996 20 20 20 22 74 62 6c 5f 6e 61 6d 65 20 3d 20 25     "tbl_name = %
1a997 51 2c 20 22 0a 20 20 20 20 20 20 20 20 20 20 22  Q, ".          "
1a998 6e 61 6d 65 20 3d 20 43 41 53 45 20 22 0a 20 20  name = CASE ".  
1a999 20 20 20 20 20 20 20 20 20 20 22 57 48 45 4e 20            "WHEN 
1a99a 74 79 70 65 3d 27 74 61 62 6c 65 27 20 54 48 45  type='table' THE
1a99b 4e 20 25 51 20 22 0a 20 20 20 20 20 20 20 20 20  N %Q ".         
1a99c 20 20 20 22 57 48 45 4e 20 6e 61 6d 65 20 4c 49     "WHEN name LI
1a99d 4b 45 20 27 73 71 6c 69 74 65 5f 61 75 74 6f 69  KE 'sqlite_autoi
1a99e 6e 64 65 78 25 25 27 20 41 4e 44 20 74 79 70 65  ndex%%' AND type
1a99f 3d 27 69 6e 64 65 78 27 20 54 48 45 4e 20 22 0a  ='index' THEN ".
1a9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 22 27 73               "'s
1a9a1 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 5f  qlite_autoindex_
1a9a2 27 20 7c 7c 20 25 51 20 7c 7c 20 73 75 62 73 74  ' || %Q || subst
1a9a3 72 28 6e 61 6d 65 2c 25 64 2b 31 38 29 20 22 0a  r(name,%d+18) ".
1a9a4 20 20 20 20 20 20 20 20 20 20 20 20 22 45 4c 53              "ELS
1a9a5 45 20 6e 61 6d 65 20 45 4e 44 20 22 0a 20 20 20  E name END ".   
1a9a6 20 20 20 22 57 48 45 52 45 20 74 62 6c 5f 6e 61     "WHERE tbl_na
1a9a7 6d 65 3d 25 51 20 41 4e 44 20 22 0a 20 20 20 20  me=%Q AND ".    
1a9a8 20 20 20 20 20 20 22 28 74 79 70 65 3d 27 74 61        "(type='ta
1a9a9 62 6c 65 27 20 4f 52 20 74 79 70 65 3d 27 69 6e  ble' OR type='in
1a9aa 64 65 78 27 20 4f 52 20 74 79 70 65 3d 27 74 72  dex' OR type='tr
1a9ab 69 67 67 65 72 27 29 3b 22 2c 20 0a 20 20 20 20  igger');", .    
1a9ac 20 20 7a 44 62 2c 20 53 43 48 45 4d 41 5f 54 41    zDb, SCHEMA_TA
1a9ad 42 4c 45 28 69 44 62 29 2c 20 7a 4e 61 6d 65 2c  BLE(iDb), zName,
1a9ae 20 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 0a   zName, zName, .
1a9af 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1a9b0 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20  MIT_TRIGGER.    
1a9b1 20 20 7a 4e 61 6d 65 2c 0a 23 65 6e 64 69 66 0a    zName,.#endif.
1a9b2 20 20 20 20 20 20 7a 4e 61 6d 65 2c 20 6e 54 61        zName, nTa
1a9b3 62 4e 61 6d 65 2c 20 7a 54 61 62 4e 61 6d 65 0a  bName, zTabName.
1a9b4 20 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51    );..#ifndef SQ
1a9b5 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
1a9b6 43 52 45 4d 45 4e 54 0a 20 20 2f 2a 20 49 66 20  CREMENT.  /* If 
1a9b7 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65  the sqlite_seque
1a9b8 6e 63 65 20 74 61 62 6c 65 20 65 78 69 73 74 73  nce table exists
1a9b9 20 69 6e 20 74 68 69 73 20 64 61 74 61 62 61 73   in this databas
1a9ba 65 2c 20 74 68 65 6e 20 75 70 64 61 74 65 20 0a  e, then update .
1a9bb 20 20 2a 2a 20 69 74 20 77 69 74 68 20 74 68 65    ** it with the
1a9bc 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e   new table name.
1a9bd 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
1a9be 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c  te3FindTable(db,
1a9bf 20 22 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63   "sqlite_sequenc
1a9c0 65 22 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20  e", zDb) ){.    
1a9c1 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
1a9c2 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20  se(pParse,.     
1a9c3 20 20 20 22 55 50 44 41 54 45 20 5c 22 25 77 5c     "UPDATE \"%w\
1a9c4 22 2e 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  ".sqlite_sequenc
1a9c5 65 20 73 65 74 20 6e 61 6d 65 20 3d 20 25 51 20  e set name = %Q 
1a9c6 57 48 45 52 45 20 6e 61 6d 65 20 3d 20 25 51 22  WHERE name = %Q"
1a9c7 2c 0a 20 20 20 20 20 20 20 20 7a 44 62 2c 20 7a  ,.        zDb, z
1a9c8 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  Name, pTab->zNam
1a9c9 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  e);.  }.#endif..
1a9ca 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1a9cb 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a  MIT_TRIGGER.  /*
1a9cc 20 49 66 20 74 68 65 72 65 20 61 72 65 20 54 45   If there are TE
1a9cd 4d 50 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74  MP triggers on t
1a9ce 68 69 73 20 74 61 62 6c 65 2c 20 6d 6f 64 69 66  his table, modif
1a9cf 79 20 74 68 65 20 73 71 6c 69 74 65 5f 74 65 6d  y the sqlite_tem
1a9d0 70 5f 6d 61 73 74 65 72 0a 20 20 2a 2a 20 74 61  p_master.  ** ta
1a9d1 62 6c 65 2e 20 44 6f 6e 27 74 20 64 6f 20 74 68  ble. Don't do th
1a9d2 69 73 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  is if the table 
1a9d3 62 65 69 6e 67 20 41 4c 54 45 52 65 64 20 69 73  being ALTERed is
1a9d4 20 69 74 73 65 6c 66 20 6c 6f 63 61 74 65 64 20   itself located 
1a9d5 69 6e 0a 20 20 2a 2a 20 74 68 65 20 74 65 6d 70  in.  ** the temp
1a9d6 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a   database..  */.
1a9d7 20 20 69 66 28 20 28 7a 57 68 65 72 65 3d 77 68    if( (zWhere=wh
1a9d8 65 72 65 54 65 6d 70 54 72 69 67 67 65 72 73 28  ereTempTriggers(
1a9d9 70 50 61 72 73 65 2c 20 70 54 61 62 29 29 21 3d  pParse, pTab))!=
1a9da 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
1a9db 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72  NestedParse(pPar
1a9dc 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 55 50  se, .        "UP
1a9dd 44 41 54 45 20 73 71 6c 69 74 65 5f 74 65 6d 70  DATE sqlite_temp
1a9de 5f 6d 61 73 74 65 72 20 53 45 54 20 22 0a 20 20  _master SET ".  
1a9df 20 20 20 20 20 20 20 20 20 20 22 73 71 6c 20 3d            "sql =
1a9e0 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74   sqlite_rename_t
1a9e1 72 69 67 67 65 72 28 73 71 6c 2c 20 25 51 29 2c  rigger(sql, %Q),
1a9e2 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22   ".            "
1a9e3 74 62 6c 5f 6e 61 6d 65 20 3d 20 25 51 20 22 0a  tbl_name = %Q ".
1a9e4 20 20 20 20 20 20 20 20 20 20 20 20 22 57 48 45              "WHE
1a9e5 52 45 20 25 73 3b 22 2c 20 7a 4e 61 6d 65 2c 20  RE %s;", zName, 
1a9e6 7a 4e 61 6d 65 2c 20 7a 57 68 65 72 65 29 3b 0a  zName, zWhere);.
1a9e7 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
1a9e8 28 7a 57 68 65 72 65 29 3b 0a 20 20 7d 0a 23 65  (zWhere);.  }.#e
1a9e9 6e 64 69 66 0a 0a 20 20 2f 2a 20 44 72 6f 70 20  ndif..  /* Drop 
1a9ea 61 6e 64 20 72 65 6c 6f 61 64 20 74 68 65 20 69  and reload the i
1a9eb 6e 74 65 72 6e 61 6c 20 74 61 62 6c 65 20 73 63  nternal table sc
1a9ec 68 65 6d 61 2e 20 2a 2f 0a 20 20 72 65 6c 6f 61  hema. */.  reloa
1a9ed 64 54 61 62 6c 65 53 63 68 65 6d 61 28 70 50 61  dTableSchema(pPa
1a9ee 72 73 65 2c 20 70 54 61 62 2c 20 7a 4e 61 6d 65  rse, pTab, zName
1a9ef 29 3b 0a 0a 65 78 69 74 5f 72 65 6e 61 6d 65 5f  );..exit_rename_
1a9f0 74 61 62 6c 65 3a 0a 20 20 73 71 6c 69 74 65 33  table:.  sqlite3
1a9f1 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 70 53  SrcListDelete(pS
1a9f2 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  rc);.  sqlite3_f
1a9f3 72 65 65 28 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a 0a  ree(zName);.}...
1a9f4 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
1a9f5 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66  ion is called af
1a9f6 74 65 72 20 61 6e 20 22 41 4c 54 45 52 20 54 41  ter an "ALTER TA
1a9f7 42 4c 45 20 2e 2e 2e 20 41 44 44 22 20 73 74 61  BLE ... ADD" sta
1a9f8 74 65 6d 65 6e 74 0a 2a 2a 20 68 61 73 20 62 65  tement.** has be
1a9f9 65 6e 20 70 61 72 73 65 64 2e 20 41 72 67 75 6d  en parsed. Argum
1a9fa 65 6e 74 20 70 43 6f 6c 44 65 66 20 63 6f 6e 74  ent pColDef cont
1a9fb 61 69 6e 73 20 74 68 65 20 74 65 78 74 20 6f 66  ains the text of
1a9fc 20 74 68 65 20 6e 65 77 0a 2a 2a 20 63 6f 6c 75   the new.** colu
1a9fd 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a  mn definition..*
1a9fe 2a 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65 20 73  *.** The Table s
1a9ff 74 72 75 63 74 75 72 65 20 70 50 61 72 73 65 2d  tructure pParse-
1aa00 3e 70 4e 65 77 54 61 62 6c 65 20 77 61 73 20 65  >pNewTable was e
1aa01 78 74 65 6e 64 65 64 20 74 6f 20 69 6e 63 6c 75  xtended to inclu
1aa02 64 65 0a 2a 2a 20 74 68 65 20 6e 65 77 20 63 6f  de.** the new co
1aa03 6c 75 6d 6e 20 64 75 72 69 6e 67 20 70 61 72 73  lumn during pars
1aa04 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ing..*/.SQLITE_P
1aa05 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1aa06 74 65 33 41 6c 74 65 72 46 69 6e 69 73 68 41 64  te3AlterFinishAd
1aa07 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 70  dColumn(Parse *p
1aa08 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 43  Parse, Token *pC
1aa09 6f 6c 44 65 66 29 7b 0a 20 20 54 61 62 6c 65 20  olDef){.  Table 
1aa0a 2a 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20  *pNew;          
1aa0b 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70      /* Copy of p
1aa0c 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
1aa0d 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
1aa0e 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
1aa0f 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 20 61  /* Table being a
1aa10 6c 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20  ltered */.  int 
1aa11 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
1aa12 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
1aa13 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 63 6f  e number */.  co
1aa14 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20  nst char *zDb;  
1aa15 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
1aa16 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f  ase name */.  co
1aa17 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 3b 20  nst char *zTab; 
1aa18 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
1aa19 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 61 72 20   name */.  char 
1aa1a 2a 7a 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20  *zCol;          
1aa1b 20 20 20 20 20 2f 2a 20 4e 75 6c 6c 2d 74 65 72       /* Null-ter
1aa1c 6d 69 6e 61 74 65 64 20 63 6f 6c 75 6d 6e 20 64  minated column d
1aa1d 65 66 69 6e 69 74 69 6f 6e 20 2a 2f 0a 20 20 43  efinition */.  C
1aa1e 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 20 20 20 20  olumn *pCol;    
1aa1f 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1aa20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20  new column */.  
1aa21 45 78 70 72 20 2a 70 44 66 6c 74 3b 20 20 20 20  Expr *pDflt;    
1aa22 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66            /* Def
1aa23 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74  ault value for t
1aa24 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f  he new column */
1aa25 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
1aa26 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1aa27 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
1aa28 6e 65 63 74 69 6f 6e 3b 20 2a 2f 0a 0a 20 20 69  nection; */..  i
1aa29 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20  f( pParse->nErr 
1aa2a 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 4e 65 77  ) return;.  pNew
1aa2b 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54   = pParse->pNewT
1aa2c 61 62 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20  able;.  assert( 
1aa2d 70 4e 65 77 20 29 3b 0a 0a 20 20 64 62 20 3d 20  pNew );..  db = 
1aa2e 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73  pParse->db;.  as
1aa2f 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
1aa30 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65  eeHoldsAllMutexe
1aa31 73 28 64 62 29 20 29 3b 0a 20 20 69 44 62 20 3d  s(db) );.  iDb =
1aa32 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
1aa33 49 6e 64 65 78 28 64 62 2c 20 70 4e 65 77 2d 3e  Index(db, pNew->
1aa34 70 53 63 68 65 6d 61 29 3b 0a 20 20 7a 44 62 20  pSchema);.  zDb 
1aa35 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
1aa36 4e 61 6d 65 3b 0a 20 20 7a 54 61 62 20 3d 20 70  Name;.  zTab = p
1aa37 4e 65 77 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 70 43  New->zName;.  pC
1aa38 6f 6c 20 3d 20 26 70 4e 65 77 2d 3e 61 43 6f 6c  ol = &pNew->aCol
1aa39 5b 70 4e 65 77 2d 3e 6e 43 6f 6c 2d 31 5d 3b 0a  [pNew->nCol-1];.
1aa3a 20 20 70 44 66 6c 74 20 3d 20 70 43 6f 6c 2d 3e    pDflt = pCol->
1aa3b 70 44 66 6c 74 3b 0a 20 20 70 54 61 62 20 3d 20  pDflt;.  pTab = 
1aa3c 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65  sqlite3FindTable
1aa3d 28 64 62 2c 20 7a 54 61 62 2c 20 7a 44 62 29 3b  (db, zTab, zDb);
1aa3e 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 20  .  assert( pTab 
1aa3f 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
1aa40 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
1aa41 41 54 49 4f 4e 0a 20 20 2f 2a 20 49 6e 76 6f 6b  ATION.  /* Invok
1aa42 65 20 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74  e the authorizat
1aa43 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f  ion callback. */
1aa44 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75  .  if( sqlite3Au
1aa45 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
1aa46 53 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42  SQLITE_ALTER_TAB
1aa47 4c 45 2c 20 7a 44 62 2c 20 70 54 61 62 2d 3e 7a  LE, zDb, pTab->z
1aa48 4e 61 6d 65 2c 20 30 29 20 29 7b 0a 20 20 20 20  Name, 0) ){.    
1aa49 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64  return;.  }.#end
1aa4a 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  if..  /* If the 
1aa4b 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f  default value fo
1aa4c 72 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e  r the new column
1aa4d 20 77 61 73 20 73 70 65 63 69 66 69 65 64 20 77   was specified w
1aa4e 69 74 68 20 61 20 0a 20 20 2a 2a 20 6c 69 74 65  ith a .  ** lite
1aa4f 72 61 6c 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 73  ral NULL, then s
1aa50 65 74 20 70 44 66 6c 74 20 74 6f 20 30 2e 20 54  et pDflt to 0. T
1aa51 68 69 73 20 73 69 6d 70 6c 69 66 69 65 73 20 63  his simplifies c
1aa52 68 65 63 6b 69 6e 67 0a 20 20 2a 2a 20 66 6f 72  hecking.  ** for
1aa53 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 64 65 66   an SQL NULL def
1aa54 61 75 6c 74 20 62 65 6c 6f 77 2e 0a 20 20 2a 2f  ault below..  */
1aa55 0a 20 20 69 66 28 20 70 44 66 6c 74 20 26 26 20  .  if( pDflt && 
1aa56 70 44 66 6c 74 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55  pDflt->op==TK_NU
1aa57 4c 4c 20 29 7b 0a 20 20 20 20 70 44 66 6c 74 20  LL ){.    pDflt 
1aa58 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  = 0;.  }..  /* C
1aa59 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 6e 65  heck that the ne
1aa5a 77 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20  w column is not 
1aa5b 73 70 65 63 69 66 69 65 64 20 61 73 20 50 52 49  specified as PRI
1aa5c 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51  MARY KEY or UNIQ
1aa5d 55 45 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 72  UE..  ** If ther
1aa5e 65 20 69 73 20 61 20 4e 4f 54 20 4e 55 4c 4c 20  e is a NOT NULL 
1aa5f 63 6f 6e 73 74 72 61 69 6e 74 2c 20 74 68 65 6e  constraint, then
1aa60 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c   the default val
1aa61 75 65 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20  ue for the.  ** 
1aa62 63 6f 6c 75 6d 6e 20 6d 75 73 74 20 6e 6f 74 20  column must not 
1aa63 62 65 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20  be NULL..  */.  
1aa64 69 66 28 20 70 43 6f 6c 2d 3e 69 73 50 72 69 6d  if( pCol->isPrim
1aa65 4b 65 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Key ){.    sqlit
1aa66 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1aa67 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61  e, "Cannot add a
1aa68 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c   PRIMARY KEY col
1aa69 75 6d 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72  umn");.    retur
1aa6a 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e 65  n;.  }.  if( pNe
1aa6b 77 2d 3e 70 49 6e 64 65 78 20 29 7b 0a 20 20 20  w->pIndex ){.   
1aa6c 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1aa6d 28 70 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74  (pParse, "Cannot
1aa6e 20 61 64 64 20 61 20 55 4e 49 51 55 45 20 63 6f   add a UNIQUE co
1aa6f 6c 75 6d 6e 22 29 3b 0a 20 20 20 20 72 65 74 75  lumn");.    retu
1aa70 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43  rn;.  }.  if( pC
1aa71 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c 20 26 26 20 21  ol->notNull && !
1aa72 70 44 66 6c 74 20 29 7b 0a 20 20 20 20 73 71 6c  pDflt ){.    sql
1aa73 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1aa74 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 43  rse, .        "C
1aa75 61 6e 6e 6f 74 20 61 64 64 20 61 20 4e 4f 54 20  annot add a NOT 
1aa76 4e 55 4c 4c 20 63 6f 6c 75 6d 6e 20 77 69 74 68  NULL column with
1aa77 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 4e   default value N
1aa78 55 4c 4c 22 29 3b 0a 20 20 20 20 72 65 74 75 72  ULL");.    retur
1aa79 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73  n;.  }..  /* Ens
1aa7a 75 72 65 20 74 68 65 20 64 65 66 61 75 6c 74 20  ure the default 
1aa7b 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73 6f  expression is so
1aa7c 6d 65 74 68 69 6e 67 20 74 68 61 74 20 73 71 6c  mething that sql
1aa7d 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70  ite3ValueFromExp
1aa7e 72 28 29 0a 20 20 2a 2a 20 63 61 6e 20 68 61 6e  r().  ** can han
1aa7f 64 6c 65 20 28 69 2e 65 2e 20 6e 6f 74 20 43 55  dle (i.e. not CU
1aa80 52 52 45 4e 54 5f 54 49 4d 45 20 65 74 63 2e 29  RRENT_TIME etc.)
1aa81 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 66 6c  .  */.  if( pDfl
1aa82 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
1aa83 5f 76 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20 20  _value *pVal;.  
1aa84 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 61 6c    if( sqlite3Val
1aa85 75 65 46 72 6f 6d 45 78 70 72 28 64 62 2c 20 70  ueFromExpr(db, p
1aa86 44 66 6c 74 2c 20 53 51 4c 49 54 45 5f 55 54 46  Dflt, SQLITE_UTF
1aa87 38 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f  8, SQLITE_AFF_NO
1aa88 4e 45 2c 20 26 70 56 61 6c 29 20 29 7b 0a 20 20  NE, &pVal) ){.  
1aa89 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
1aa8a 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
1aa8b 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
1aa8c 20 20 69 66 28 20 21 70 56 61 6c 20 29 7b 0a 20    if( !pVal ){. 
1aa8d 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1aa8e 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 43 61  rMsg(pParse, "Ca
1aa8f 6e 6e 6f 74 20 61 64 64 20 61 20 63 6f 6c 75 6d  nnot add a colum
1aa90 6e 20 77 69 74 68 20 6e 6f 6e 2d 63 6f 6e 73 74  n with non-const
1aa91 61 6e 74 20 64 65 66 61 75 6c 74 22 29 3b 0a 20  ant default");. 
1aa92 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
1aa93 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61   }.    sqlite3Va
1aa94 6c 75 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 20  lueFree(pVal);. 
1aa95 20 7d 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66 79 20   }..  /* Modify 
1aa96 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45  the CREATE TABLE
1aa97 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20   statement. */. 
1aa98 20 7a 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 44   zCol = sqlite3D
1aa99 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68  bStrNDup(db, (ch
1aa9a 61 72 2a 29 70 43 6f 6c 44 65 66 2d 3e 7a 2c 20  ar*)pColDef->z, 
1aa9b 70 43 6f 6c 44 65 66 2d 3e 6e 29 3b 0a 20 20 69  pColDef->n);.  i
1aa9c 66 28 20 7a 43 6f 6c 20 29 7b 0a 20 20 20 20 63  f( zCol ){.    c
1aa9d 68 61 72 20 2a 7a 45 6e 64 20 3d 20 26 7a 43 6f  har *zEnd = &zCo
1aa9e 6c 5b 70 43 6f 6c 44 65 66 2d 3e 6e 2d 31 5d 3b  l[pColDef->n-1];
1aa9f 0a 20 20 20 20 77 68 69 6c 65 28 20 28 7a 45 6e  .    while( (zEn
1aaa0 64 3e 7a 43 6f 6c 20 26 26 20 2a 7a 45 6e 64 3d  d>zCol && *zEnd=
1aaa1 3d 27 3b 27 29 20 7c 7c 20 69 73 73 70 61 63 65  =';') || isspace
1aaa2 28 2a 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  (*(unsigned char
1aaa3 20 2a 29 7a 45 6e 64 29 20 29 7b 0a 20 20 20 20   *)zEnd) ){.    
1aaa4 20 20 2a 7a 45 6e 64 2d 2d 20 3d 20 27 5c 30 27    *zEnd-- = '\0'
1aaa5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
1aaa6 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70  te3NestedParse(p
1aaa7 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20  Parse, .        
1aaa8 22 55 50 44 41 54 45 20 5c 22 25 77 5c 22 2e 25  "UPDATE \"%w\".%
1aaa9 73 20 53 45 54 20 22 0a 20 20 20 20 20 20 20 20  s SET ".        
1aaaa 20 20 22 73 71 6c 20 3d 20 73 75 62 73 74 72 28    "sql = substr(
1aaab 73 71 6c 2c 31 2c 25 64 29 20 7c 7c 20 27 2c 20  sql,1,%d) || ', 
1aaac 27 20 7c 7c 20 25 51 20 7c 7c 20 73 75 62 73 74  ' || %Q || subst
1aaad 72 28 73 71 6c 2c 25 64 29 20 22 0a 20 20 20 20  r(sql,%d) ".    
1aaae 20 20 20 20 22 57 48 45 52 45 20 74 79 70 65 20      "WHERE type 
1aaaf 3d 20 27 74 61 62 6c 65 27 20 41 4e 44 20 6e 61  = 'table' AND na
1aab0 6d 65 20 3d 20 25 51 22 2c 20 0a 20 20 20 20 20  me = %Q", .     
1aab1 20 7a 44 62 2c 20 53 43 48 45 4d 41 5f 54 41 42   zDb, SCHEMA_TAB
1aab2 4c 45 28 69 44 62 29 2c 20 70 4e 65 77 2d 3e 61  LE(iDb), pNew->a
1aab3 64 64 43 6f 6c 4f 66 66 73 65 74 2c 20 7a 43 6f  ddColOffset, zCo
1aab4 6c 2c 20 70 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f  l, pNew->addColO
1aab5 66 66 73 65 74 2b 31 2c 0a 20 20 20 20 20 20 7a  ffset+1,.      z
1aab6 54 61 62 0a 20 20 20 20 29 3b 0a 20 20 20 20 73  Tab.    );.    s
1aab7 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6c  qlite3_free(zCol
1aab8 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  );.  }..  /* If 
1aab9 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75  the default valu
1aaba 65 20 6f 66 20 74 68 65 20 6e 65 77 20 63 6f 6c  e of the new col
1aabb 75 6d 6e 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65  umn is NULL, the
1aabc 6e 20 73 65 74 20 74 68 65 20 66 69 6c 65 0a 20  n set the file. 
1aabd 20 2a 2a 20 66 6f 72 6d 61 74 20 74 6f 20 32 2e   ** format to 2.
1aabe 20 49 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   If the default 
1aabf 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6e 65 77  value of the new
1aac0 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 4e   column is not N
1aac1 55 4c 4c 2c 0a 20 20 2a 2a 20 74 68 65 20 66 69  ULL,.  ** the fi
1aac2 6c 65 20 66 6f 72 6d 61 74 20 62 65 63 6f 6d 65  le format become
1aac3 73 20 33 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  s 3..  */.  sqli
1aac4 74 65 33 4d 69 6e 69 6d 75 6d 46 69 6c 65 46 6f  te3MinimumFileFo
1aac5 72 6d 61 74 28 70 50 61 72 73 65 2c 20 69 44 62  rmat(pParse, iDb
1aac6 2c 20 70 44 66 6c 74 20 3f 20 33 20 3a 20 32 29  , pDflt ? 3 : 2)
1aac7 3b 0a 0a 20 20 2f 2a 20 52 65 6c 6f 61 64 20 74  ;..  /* Reload t
1aac8 68 65 20 73 63 68 65 6d 61 20 6f 66 20 74 68 65  he schema of the
1aac9 20 6d 6f 64 69 66 69 65 64 20 74 61 62 6c 65 2e   modified table.
1aaca 20 2a 2f 0a 20 20 72 65 6c 6f 61 64 54 61 62 6c   */.  reloadTabl
1aacb 65 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20  eSchema(pParse, 
1aacc 70 54 61 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  pTab, pTab->zNam
1aacd 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  e);.}../*.** Thi
1aace 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
1aacf 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
1aad0 65 72 20 61 66 74 65 72 20 74 68 65 20 74 61 62  er after the tab
1aad1 6c 65 2d 6e 61 6d 65 20 69 6e 0a 2a 2a 20 61 6e  le-name in.** an
1aad2 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 3c 74   "ALTER TABLE <t
1aad3 61 62 6c 65 2d 6e 61 6d 65 3e 20 41 44 44 22 20  able-name> ADD" 
1aad4 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70 61 72  statement is par
1aad5 73 65 64 2e 20 41 72 67 75 6d 65 6e 74 20 0a 2a  sed. Argument .*
1aad6 2a 20 70 53 72 63 20 69 73 20 74 68 65 20 66 75  * pSrc is the fu
1aad7 6c 6c 2d 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  ll-name of the t
1aad8 61 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72  able being alter
1aad9 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ed..**.** This r
1aada 6f 75 74 69 6e 65 20 6d 61 6b 65 73 20 61 20 28  outine makes a (
1aadb 70 61 72 74 69 61 6c 29 20 63 6f 70 79 20 6f 66  partial) copy of
1aadc 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63   the Table struc
1aadd 74 75 72 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20  ture.** for the 
1aade 74 61 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65  table being alte
1aadf 72 65 64 20 61 6e 64 20 73 65 74 73 20 50 61 72  red and sets Par
1aae0 73 65 2e 70 4e 65 77 54 61 62 6c 65 20 74 6f 20  se.pNewTable to 
1aae1 70 6f 69 6e 74 0a 2a 2a 20 74 6f 20 69 74 2e 20  point.** to it. 
1aae2 52 6f 75 74 69 6e 65 73 20 63 61 6c 6c 65 64 20  Routines called 
1aae3 62 79 20 74 68 65 20 70 61 72 73 65 72 20 61 73  by the parser as
1aae4 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 66 69   the column defi
1aae5 6e 69 74 69 6f 6e 0a 2a 2a 20 69 73 20 70 61 72  nition.** is par
1aae6 73 65 64 20 28 69 2e 65 2e 20 73 71 6c 69 74 65  sed (i.e. sqlite
1aae7 33 41 64 64 43 6f 6c 75 6d 6e 28 29 29 20 61 64  3AddColumn()) ad
1aae8 64 20 74 68 65 20 6e 65 77 20 43 6f 6c 75 6d 6e  d the new Column
1aae9 20 64 61 74 61 20 74 6f 20 0a 2a 2a 20 74 68 65   data to .** the
1aaea 20 63 6f 70 79 2e 20 54 68 65 20 63 6f 70 79 20   copy. The copy 
1aaeb 6f 66 20 74 68 65 20 54 61 62 6c 65 20 73 74 72  of the Table str
1aaec 75 63 74 75 72 65 20 69 73 20 64 65 6c 65 74 65  ucture is delete
1aaed 64 20 62 79 20 74 6f 6b 65 6e 69 7a 65 2e 63 20  d by tokenize.c 
1aaee 0a 2a 2a 20 61 66 74 65 72 20 70 61 72 73 69 6e  .** after parsin
1aaef 67 20 69 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a  g is finished..*
1aaf0 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 20 73 71 6c  *.** Routine sql
1aaf1 69 74 65 33 41 6c 74 65 72 46 69 6e 69 73 68 41  ite3AlterFinishA
1aaf2 64 64 43 6f 6c 75 6d 6e 28 29 20 77 69 6c 6c 20  ddColumn() will 
1aaf3 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 6d  be called to com
1aaf4 70 6c 65 74 65 0a 2a 2a 20 63 6f 64 69 6e 67 20  plete.** coding 
1aaf5 74 68 65 20 22 41 4c 54 45 52 20 54 41 42 4c 45  the "ALTER TABLE
1aaf6 20 2e 2e 2e 20 41 44 44 22 20 73 74 61 74 65 6d   ... ADD" statem
1aaf7 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ent..*/.SQLITE_P
1aaf8 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1aaf9 74 65 33 41 6c 74 65 72 42 65 67 69 6e 41 64 64  te3AlterBeginAdd
1aafa 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 70 50  Column(Parse *pP
1aafb 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70  arse, SrcList *p
1aafc 53 72 63 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  Src){.  Table *p
1aafd 4e 65 77 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54  New;.  Table *pT
1aafe 61 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20  ab;.  Vdbe *v;. 
1aaff 20 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20   int iDb;.  int 
1ab00 69 3b 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b  i;.  int nAlloc;
1ab01 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1ab02 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20   pParse->db;..  
1ab03 2f 2a 20 4c 6f 6f 6b 20 75 70 20 74 68 65 20 74  /* Look up the t
1ab04 61 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72  able being alter
1ab05 65 64 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ed. */.  assert(
1ab06 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
1ab07 6c 65 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  le==0 );.  asser
1ab08 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
1ab09 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 64  oldsAllMutexes(d
1ab0a 62 29 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e  b) );.  if( db->
1ab0b 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67  mallocFailed ) g
1ab0c 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61  oto exit_begin_a
1ab0d 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 70 54 61  dd_column;.  pTa
1ab0e 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74  b = sqlite3Locat
1ab0f 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30  eTable(pParse, 0
1ab10 2c 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 4e 61  , pSrc->a[0].zNa
1ab11 6d 65 2c 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a  me, pSrc->a[0].z
1ab12 44 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28  Database);.  if(
1ab13 20 21 70 54 61 62 20 29 20 67 6f 74 6f 20 65 78   !pTab ) goto ex
1ab14 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c  it_begin_add_col
1ab15 75 6d 6e 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  umn;..#ifndef SQ
1ab16 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1ab17 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 49 73 56  LTABLE.  if( IsV
1ab18 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
1ab19 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1ab1a 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 69 72  Msg(pParse, "vir
1ab1b 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 61 79 20  tual tables may 
1ab1c 6e 6f 74 20 62 65 20 61 6c 74 65 72 65 64 22 29  not be altered")
1ab1d 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  ;.    goto exit_
1ab1e 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e  begin_add_column
1ab1f 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
1ab20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69  /* Make sure thi
1ab21 73 20 69 73 20 6e 6f 74 20 61 6e 20 61 74 74 65  s is not an atte
1ab22 6d 70 74 20 74 6f 20 41 4c 54 45 52 20 61 20 76  mpt to ALTER a v
1ab23 69 65 77 2e 20 2a 2f 0a 20 20 69 66 28 20 70 54  iew. */.  if( pT
1ab24 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20  ab->pSelect ){. 
1ab25 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1ab26 73 67 28 70 50 61 72 73 65 2c 20 22 43 61 6e 6e  sg(pParse, "Cann
1ab27 6f 74 20 61 64 64 20 61 20 63 6f 6c 75 6d 6e 20  ot add a column 
1ab28 74 6f 20 61 20 76 69 65 77 22 29 3b 0a 20 20 20  to a view");.   
1ab29 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e   goto exit_begin
1ab2a 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d  _add_column;.  }
1ab2b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  ..  assert( pTab
1ab2c 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 3e 30  ->addColOffset>0
1ab2d 20 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69   );.  iDb = sqli
1ab2e 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
1ab2f 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  (db, pTab->pSche
1ab30 6d 61 29 3b 0a 0a 20 20 2f 2a 20 50 75 74 20 61  ma);..  /* Put a
1ab31 20 63 6f 70 79 20 6f 66 20 74 68 65 20 54 61 62   copy of the Tab
1ab32 6c 65 20 73 74 72 75 63 74 20 69 6e 20 50 61 72  le struct in Par
1ab33 73 65 2e 70 4e 65 77 54 61 62 6c 65 20 66 6f 72  se.pNewTable for
1ab34 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65   the.  ** sqlite
1ab35 33 41 64 64 43 6f 6c 75 6d 6e 28 29 20 66 75 6e  3AddColumn() fun
1ab36 63 74 69 6f 6e 20 61 6e 64 20 66 72 69 65 6e 64  ction and friend
1ab37 73 20 74 6f 20 6d 6f 64 69 66 79 2e 0a 20 20 2a  s to modify..  *
1ab38 2f 0a 20 20 70 4e 65 77 20 3d 20 28 54 61 62 6c  /.  pNew = (Tabl
1ab39 65 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c  e*)sqlite3DbMall
1ab3a 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
1ab3b 66 28 54 61 62 6c 65 29 29 3b 0a 20 20 69 66 28  f(Table));.  if(
1ab3c 20 21 70 4e 65 77 20 29 20 67 6f 74 6f 20 65 78   !pNew ) goto ex
1ab3d 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c  it_begin_add_col
1ab3e 75 6d 6e 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70  umn;.  pParse->p
1ab3f 4e 65 77 54 61 62 6c 65 20 3d 20 70 4e 65 77 3b  NewTable = pNew;
1ab40 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20 3d 20  .  pNew->nRef = 
1ab41 31 3b 0a 20 20 70 4e 65 77 2d 3e 6e 43 6f 6c 20  1;.  pNew->nCol 
1ab42 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20  = pTab->nCol;.  
1ab43 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 43  assert( pNew->nC
1ab44 6f 6c 3e 30 20 29 3b 0a 20 20 6e 41 6c 6c 6f 63  ol>0 );.  nAlloc
1ab45 20 3d 20 28 28 28 70 4e 65 77 2d 3e 6e 43 6f 6c   = (((pNew->nCol
1ab46 2d 31 29 2f 38 29 2a 38 29 2b 38 3b 0a 20 20 61  -1)/8)*8)+8;.  a
1ab47 73 73 65 72 74 28 20 6e 41 6c 6c 6f 63 3e 3d 70  ssert( nAlloc>=p
1ab48 4e 65 77 2d 3e 6e 43 6f 6c 20 26 26 20 6e 41 6c  New->nCol && nAl
1ab49 6c 6f 63 25 38 3d 3d 30 20 26 26 20 6e 41 6c 6c  loc%8==0 && nAll
1ab4a 6f 63 2d 70 4e 65 77 2d 3e 6e 43 6f 6c 3c 38 20  oc-pNew->nCol<8 
1ab4b 29 3b 0a 20 20 70 4e 65 77 2d 3e 61 43 6f 6c 20  );.  pNew->aCol 
1ab4c 3d 20 28 43 6f 6c 75 6d 6e 2a 29 73 71 6c 69 74  = (Column*)sqlit
1ab4d 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
1ab4e 62 2c 20 73 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e  b, sizeof(Column
1ab4f 29 2a 6e 41 6c 6c 6f 63 29 3b 0a 20 20 70 4e 65  )*nAlloc);.  pNe
1ab50 77 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  w->zName = sqlit
1ab51 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70  e3DbStrDup(db, p
1ab52 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69  Tab->zName);.  i
1ab53 66 28 20 21 70 4e 65 77 2d 3e 61 43 6f 6c 20 7c  f( !pNew->aCol |
1ab54 7c 20 21 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 29  | !pNew->zName )
1ab55 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63  {.    db->malloc
1ab56 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
1ab57 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f  goto exit_begin_
1ab58 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a  add_column;.  }.
1ab59 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e 61    memcpy(pNew->a
1ab5a 43 6f 6c 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 2c  Col, pTab->aCol,
1ab5b 20 73 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a   sizeof(Column)*
1ab5c 70 4e 65 77 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 66  pNew->nCol);.  f
1ab5d 6f 72 28 69 3d 30 3b 20 69 3c 70 4e 65 77 2d 3e  or(i=0; i<pNew->
1ab5e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
1ab5f 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26  Column *pCol = &
1ab60 70 4e 65 77 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a 20  pNew->aCol[i];. 
1ab61 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d     pCol->zName =
1ab62 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
1ab63 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65  (db, pCol->zName
1ab64 29 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 43 6f  );.    pCol->zCo
1ab65 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 70 43 6f 6c  ll = 0;.    pCol
1ab66 2d 3e 7a 54 79 70 65 20 3d 20 30 3b 0a 20 20 20  ->zType = 0;.   
1ab67 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 20 3d 20 30   pCol->pDflt = 0
1ab68 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 70 53  ;.  }.  pNew->pS
1ab69 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b  chema = db->aDb[
1ab6a 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20  iDb].pSchema;.  
1ab6b 70 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f 66 66 73  pNew->addColOffs
1ab6c 65 74 20 3d 20 70 54 61 62 2d 3e 61 64 64 43 6f  et = pTab->addCo
1ab6d 6c 4f 66 66 73 65 74 3b 0a 20 20 70 4e 65 77 2d  lOffset;.  pNew-
1ab6e 3e 6e 52 65 66 20 3d 20 31 3b 0a 0a 20 20 2f 2a  >nRef = 1;..  /*
1ab6f 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63   Begin a transac
1ab70 74 69 6f 6e 20 61 6e 64 20 69 6e 63 72 65 6d 65  tion and increme
1ab71 6e 74 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f  nt the schema co
1ab72 6f 6b 69 65 2e 20 20 2a 2f 0a 20 20 73 71 6c 69  okie.  */.  sqli
1ab73 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
1ab74 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30  ration(pParse, 0
1ab75 2c 20 69 44 62 29 3b 0a 20 20 76 20 3d 20 73 71  , iDb);.  v = sq
1ab76 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
1ab77 72 73 65 29 3b 0a 20 20 69 66 28 20 21 76 20 29  rse);.  if( !v )
1ab78 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e   goto exit_begin
1ab79 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 73  _add_column;.  s
1ab7a 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b  qlite3ChangeCook
1ab7b 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b  ie(pParse, iDb);
1ab7c 0a 0a 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64  ..exit_begin_add
1ab7d 5f 63 6f 6c 75 6d 6e 3a 0a 20 20 73 71 6c 69 74  _column:.  sqlit
1ab7e 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28  e3SrcListDelete(
1ab7f 70 53 72 63 29 3b 0a 20 20 72 65 74 75 72 6e 3b  pSrc);.  return;
1ab80 0a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51  .}.#endif  /* SQ
1ab81 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45  LITE_ALTER_TABLE
1ab82 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
1ab83 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 6c 74 65  **** End of alte
1ab84 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  r.c ************
1ab85 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ab86 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ab87 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
1ab88 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
1ab89 61 6e 61 6c 79 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a  analyze.c ******
1ab8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ab8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ab8c 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20  ***/./*.** 2005 
1ab8d 4a 75 6c 79 20 38 0a 2a 2a 0a 2a 2a 20 54 68 65  July 8.**.** The
1ab8e 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
1ab8f 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
1ab90 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
1ab91 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
1ab92 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
1ab93 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
1ab94 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
1ab95 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
1ab96 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
1ab97 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
1ab98 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
1ab99 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
1ab9a 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
1ab9b 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
1ab9c 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
1ab9d 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
1ab9e 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
1ab9f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aba1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aba2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aba3 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
1aba4 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
1aba5 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
1aba6 20 74 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d   the ANALYZE com
1aba7 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29  mand..**.** @(#)
1aba8 20 24 49 64 3a 20 61 6e 61 6c 79 7a 65 2e 63 2c   $Id: analyze.c,
1aba9 76 20 31 2e 34 32 20 32 30 30 38 2f 30 33 2f 32  v 1.42 2008/03/2
1abaa 35 20 30 39 3a 34 37 3a 33 35 20 64 61 6e 69 65  5 09:47:35 danie
1abab 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
1abac 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1abad 4d 49 54 5f 41 4e 41 4c 59 5a 45 0a 0a 2f 2a 0a  MIT_ANALYZE../*.
1abae 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1abaf 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74  generates code t
1abb0 68 61 74 20 6f 70 65 6e 73 20 74 68 65 20 73 71  hat opens the sq
1abb1 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65  lite_stat1 table
1abb2 20 6f 6e 20 63 75 72 73 6f 72 0a 2a 2a 20 69 53   on cursor.** iS
1abb3 74 61 74 43 75 72 2e 0a 2a 2a 0a 2a 2a 20 49 66  tatCur..**.** If
1abb4 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
1abb5 31 20 74 61 62 6c 65 73 20 64 6f 65 73 20 6e 6f  1 tables does no
1abb6 74 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69  t previously exi
1abb7 73 74 2c 20 69 74 20 69 73 20 63 72 65 61 74 65  st, it is create
1abb8 64 2e 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 73  d..** If it does
1abb9 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69 73   previously exis
1abba 74 2c 20 61 6c 6c 20 65 6e 74 69 72 65 73 20 61  t, all entires a
1abbb 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
1abbc 61 62 6c 65 20 7a 57 68 65 72 65 0a 2a 2a 20 61  able zWhere.** a
1abbd 72 65 20 72 65 6d 6f 76 65 64 2e 20 20 49 66 20  re removed.  If 
1abbe 7a 57 68 65 72 65 3d 3d 30 20 74 68 65 6e 20 61  zWhere==0 then a
1abbf 6c 6c 20 65 6e 74 72 69 65 73 20 61 72 65 20 72  ll entries are r
1abc0 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  emoved..*/.stati
1abc1 63 20 76 6f 69 64 20 6f 70 65 6e 53 74 61 74 54  c void openStatT
1abc2 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70  able(.  Parse *p
1abc3 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
1abc4 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
1abc5 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c  xt */.  int iDb,
1abc6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1abc7 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
1abc8 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 69  we are looking i
1abc9 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 74  n */.  int iStat
1abca 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 20 2f  Cur,           /
1abcb 2a 20 4f 70 65 6e 20 74 68 65 20 73 71 6c 69 74  * Open the sqlit
1abcc 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 6f 6e  e_stat1 table on
1abcd 20 74 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a   this cursor */.
1abce 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 57    const char *zW
1abcf 68 65 72 65 20 20 20 20 20 20 2f 2a 20 44 65 6c  here      /* Del
1abd0 65 74 65 20 65 6e 74 72 69 65 73 20 61 73 73 6f  ete entries asso
1abd1 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73  ciated with this
1abd2 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73   table */.){.  s
1abd3 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
1abd4 72 73 65 2d 3e 64 62 3b 0a 20 20 44 62 20 2a 70  rse->db;.  Db *p
1abd5 44 62 3b 0a 20 20 69 6e 74 20 69 52 6f 6f 74 50  Db;.  int iRootP
1abd6 61 67 65 3b 0a 20 20 69 6e 74 20 63 72 65 61 74  age;.  int creat
1abd7 65 53 74 61 74 31 20 3d 20 30 3b 0a 20 20 54 61  eStat1 = 0;.  Ta
1abd8 62 6c 65 20 2a 70 53 74 61 74 3b 0a 20 20 56 64  ble *pStat;.  Vd
1abd9 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47  be *v = sqlite3G
1abda 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
1abdb 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65  .  if( v==0 ) re
1abdc 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20  turn;.  assert( 
1abdd 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
1abde 73 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20  sAllMutexes(db) 
1abdf 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
1abe0 69 74 65 33 56 64 62 65 44 62 28 76 29 3d 3d 64  ite3VdbeDb(v)==d
1abe1 62 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64 62  b );.  pDb = &db
1abe2 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 69 66  ->aDb[iDb];.  if
1abe3 28 20 28 70 53 74 61 74 20 3d 20 73 71 6c 69 74  ( (pStat = sqlit
1abe4 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20  e3FindTable(db, 
1abe5 22 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 20  "sqlite_stat1", 
1abe6 70 44 62 2d 3e 7a 4e 61 6d 65 29 29 3d 3d 30 20  pDb->zName))==0 
1abe7 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 73 71  ){.    /* The sq
1abe8 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65  lite_stat1 table
1abe9 73 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74  s does not exist
1abea 2e 20 20 43 72 65 61 74 65 20 69 74 2e 20 20 0a  .  Create it.  .
1abeb 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74      ** Note that
1abec 20 61 20 73 69 64 65 2d 65 66 66 65 63 74 20 6f   a side-effect o
1abed 66 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42  f the CREATE TAB
1abee 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  LE statement is 
1abef 74 6f 20 6c 65 61 76 65 0a 20 20 20 20 2a 2a 20  to leave.    ** 
1abf0 74 68 65 20 72 6f 6f 74 70 61 67 65 20 6f 66 20  the rootpage of 
1abf1 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 69 6e  the new table in
1abf2 20 72 65 67 69 73 74 65 72 20 70 50 61 72 73 65   register pParse
1abf3 2d 3e 72 65 67 52 6f 6f 74 2e 20 20 54 68 69 73  ->regRoot.  This
1abf4 20 69 73 0a 20 20 20 20 2a 2a 20 69 6d 70 6f 72   is.    ** impor
1abf5 74 61 6e 74 20 62 65 63 61 75 73 65 20 74 68 65  tant because the
1abf6 20 4f 70 65 6e 57 72 69 74 65 20 6f 70 63 6f 64   OpenWrite opcod
1abf7 65 20 62 65 6c 6f 77 20 77 69 6c 6c 20 62 65 20  e below will be 
1abf8 6e 65 65 64 69 6e 67 20 69 74 2e 20 2a 2f 0a 20  needing it. */. 
1abf9 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64     sqlite3Nested
1abfa 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20  Parse(pParse,.  
1abfb 20 20 20 20 22 43 52 45 41 54 45 20 54 41 42 4c      "CREATE TABL
1abfc 45 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74  E %Q.sqlite_stat
1abfd 31 28 74 62 6c 2c 69 64 78 2c 73 74 61 74 29 22  1(tbl,idx,stat)"
1abfe 2c 0a 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61  ,.      pDb->zNa
1abff 6d 65 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 52  me.    );.    iR
1ac00 6f 6f 74 50 61 67 65 20 3d 20 70 50 61 72 73 65  ootPage = pParse
1ac01 2d 3e 72 65 67 52 6f 6f 74 3b 0a 20 20 20 20 63  ->regRoot;.    c
1ac02 72 65 61 74 65 53 74 61 74 31 20 3d 20 31 3b 20  reateStat1 = 1; 
1ac03 20 2f 2a 20 43 61 75 73 65 20 72 6f 6f 74 70 61   /* Cause rootpa
1ac04 67 65 20 74 6f 20 62 65 20 74 61 6b 65 6e 20 66  ge to be taken f
1ac05 72 6f 6d 20 74 6f 70 20 6f 66 20 73 74 61 63 6b  rom top of stack
1ac06 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   */.  }else if( 
1ac07 7a 57 68 65 72 65 20 29 7b 0a 20 20 20 20 2f 2a  zWhere ){.    /*
1ac08 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   The sqlite_stat
1ac09 31 20 74 61 62 6c 65 20 65 78 69 73 74 73 2e 20  1 table exists. 
1ac0a 20 44 65 6c 65 74 65 20 61 6c 6c 20 65 6e 74 72   Delete all entr
1ac0b 69 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ies associated w
1ac0c 69 74 68 0a 20 20 20 20 2a 2a 20 74 68 65 20 74  ith.    ** the t
1ac0d 61 62 6c 65 20 7a 57 68 65 72 65 2e 20 2a 2f 0a  able zWhere. */.
1ac0e 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65      sqlite3Neste
1ac0f 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20  dParse(pParse,. 
1ac10 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46 52        "DELETE FR
1ac11 4f 4d 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61  OM %Q.sqlite_sta
1ac12 74 31 20 57 48 45 52 45 20 74 62 6c 3d 25 51 22  t1 WHERE tbl=%Q"
1ac13 2c 0a 20 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e  ,.       pDb->zN
1ac14 61 6d 65 2c 20 7a 57 68 65 72 65 0a 20 20 20 20  ame, zWhere.    
1ac15 29 3b 0a 20 20 20 20 69 52 6f 6f 74 50 61 67 65  );.    iRootPage
1ac16 20 3d 20 70 53 74 61 74 2d 3e 74 6e 75 6d 3b 0a   = pStat->tnum;.
1ac17 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
1ac18 54 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31  The sqlite_stat1
1ac19 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65   table already e
1ac1a 78 69 73 74 73 2e 20 20 44 65 6c 65 74 65 20 61  xists.  Delete a
1ac1b 6c 6c 20 72 6f 77 73 2e 20 2a 2f 0a 20 20 20 20  ll rows. */.    
1ac1c 69 52 6f 6f 74 50 61 67 65 20 3d 20 70 53 74 61  iRootPage = pSta
1ac1d 74 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 73 71 6c  t->tnum;.    sql
1ac1e 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1ac1f 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 70 53 74 61  , OP_Clear, pSta
1ac20 74 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20  t->tnum, iDb);. 
1ac21 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68   }..  /* Open th
1ac22 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74  e sqlite_stat1 t
1ac23 61 62 6c 65 20 66 6f 72 20 77 72 69 74 69 6e 67  able for writing
1ac24 2e 20 55 6e 6c 65 73 73 20 69 74 20 77 61 73 20  . Unless it was 
1ac25 63 72 65 61 74 65 64 0a 20 20 2a 2a 20 62 79 20  created.  ** by 
1ac26 74 68 69 73 20 76 64 62 65 20 70 72 6f 67 72 61  this vdbe progra
1ac27 6d 2c 20 6c 6f 63 6b 20 69 74 20 66 6f 72 20 77  m, lock it for w
1ac28 72 69 74 69 6e 67 20 61 74 20 74 68 65 20 73 68  riting at the sh
1ac29 61 72 65 64 2d 63 61 63 68 65 20 6c 65 76 65 6c  ared-cache level
1ac2a 2e 20 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20  . .  ** If this 
1ac2b 76 64 62 65 20 64 69 64 20 63 72 65 61 74 65 20  vdbe did create 
1ac2c 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31  the sqlite_stat1
1ac2d 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 69 74 20   table, then it 
1ac2e 6d 75 73 74 20 68 61 76 65 20 0a 20 20 2a 2a 20  must have .  ** 
1ac2f 61 6c 72 65 61 64 79 20 6f 62 74 61 69 6e 65 64  already obtained
1ac30 20 61 20 73 63 68 65 6d 61 2d 6c 6f 63 6b 2c 20   a schema-lock, 
1ac31 6d 61 6b 69 6e 67 20 74 68 65 20 77 72 69 74 65  making the write
1ac32 2d 6c 6f 63 6b 20 72 65 64 75 6e 64 61 6e 74 2e  -lock redundant.
1ac33 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 63 72 65  .  */.  if( !cre
1ac34 61 74 65 53 74 61 74 31 20 29 7b 0a 20 20 20 20  ateStat1 ){.    
1ac35 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b  sqlite3TableLock
1ac36 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 69 52  (pParse, iDb, iR
1ac37 6f 6f 74 50 61 67 65 2c 20 31 2c 20 22 73 71 6c  ootPage, 1, "sql
1ac38 69 74 65 5f 73 74 61 74 31 22 29 3b 0a 20 20 7d  ite_stat1");.  }
1ac39 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
1ac3a 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 74 4e 75  dOp2(v, OP_SetNu
1ac3b 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 33 29 3b  mColumns, 0, 3);
1ac3c 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
1ac3d 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 57  dOp3(v, OP_OpenW
1ac3e 72 69 74 65 2c 20 69 53 74 61 74 43 75 72 2c 20  rite, iStatCur, 
1ac3f 69 52 6f 6f 74 50 61 67 65 2c 20 69 44 62 29 3b  iRootPage, iDb);
1ac40 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
1ac41 61 6e 67 65 50 35 28 76 2c 20 63 72 65 61 74 65  angeP5(v, create
1ac42 53 74 61 74 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Stat1);.}../*.**
1ac43 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
1ac44 6f 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73  o do an analysis
1ac45 20 6f 66 20 61 6c 6c 20 69 6e 64 69 63 65 73 20   of all indices 
1ac46 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a  associated with.
1ac47 2a 2a 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c  ** a single tabl
1ac48 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
1ac49 64 20 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c  d analyzeOneTabl
1ac4a 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  e(.  Parse *pPar
1ac4b 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 65 72 20  se,   /* Parser 
1ac4c 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62  context */.  Tab
1ac4d 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a  le *pTab,     /*
1ac4e 20 54 61 62 6c 65 20 77 68 6f 73 65 20 69 6e 64   Table whose ind
1ac4f 69 63 65 73 20 61 72 65 20 74 6f 20 62 65 20 61  ices are to be a
1ac50 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 69 6e 74  nalyzed */.  int
1ac51 20 69 53 74 61 74 43 75 72 2c 20 20 20 20 2f 2a   iStatCur,    /*
1ac52 20 43 75 72 73 6f 72 20 74 68 61 74 20 77 72 69   Cursor that wri
1ac53 74 65 73 20 74 6f 20 74 68 65 20 73 71 6c 69 74  tes to the sqlit
1ac54 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 2a 2f  e_stat1 table */
1ac55 0a 20 20 69 6e 74 20 69 4d 65 6d 20 20 20 20 20  .  int iMem     
1ac56 20 20 20 20 2f 2a 20 41 76 61 69 6c 61 62 6c 65      /* Available
1ac57 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e   memory location
1ac58 73 20 62 65 67 69 6e 20 68 65 72 65 20 2a 2f 0a  s begin here */.
1ac59 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  ){.  Index *pIdx
1ac5a 3b 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e 64 65  ;     /* An inde
1ac5b 78 20 74 6f 20 62 65 69 6e 67 20 61 6e 61 6c 79  x to being analy
1ac5c 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64  zed */.  int iId
1ac5d 78 43 75 72 3b 20 20 20 20 20 2f 2a 20 43 75 72  xCur;     /* Cur
1ac5e 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 69  sor number for i
1ac5f 6e 64 65 78 20 62 65 69 6e 67 20 61 6e 61 6c 79  ndex being analy
1ac60 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  zed */.  int nCo
1ac61 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  l;        /* Num
1ac62 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
1ac63 6e 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20  n the index */. 
1ac64 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20   Vdbe *v;       
1ac65 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c    /* The virtual
1ac66 20 6d 61 63 68 69 6e 65 20 62 65 69 6e 67 20 62   machine being b
1ac67 75 69 6c 74 20 75 70 20 2a 2f 0a 20 20 69 6e 74  uilt up */.  int
1ac68 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a   i;           /*
1ac69 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
1ac6a 0a 20 20 69 6e 74 20 74 6f 70 4f 66 4c 6f 6f 70  .  int topOfLoop
1ac6b 3b 20 20 20 2f 2a 20 54 68 65 20 74 6f 70 20 6f  ;   /* The top o
1ac6c 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20  f the loop */.  
1ac6d 69 6e 74 20 65 6e 64 4f 66 4c 6f 6f 70 3b 20 20  int endOfLoop;  
1ac6e 20 2f 2a 20 54 68 65 20 65 6e 64 20 6f 66 20 74   /* The end of t
1ac6f 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74  he loop */.  int
1ac70 20 61 64 64 72 3b 20 20 20 20 20 20 20 20 2f 2a   addr;        /*
1ac71 20 54 68 65 20 61 64 64 72 65 73 73 20 6f 66 20   The address of 
1ac72 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a  an instruction *
1ac73 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  /.  int iDb;    
1ac74 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
1ac75 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
1ac76 6e 69 6e 67 20 70 54 61 62 20 2a 2f 0a 0a 20 20  ning pTab */..  
1ac77 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
1ac78 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
1ac79 28 20 76 3d 3d 30 20 7c 7c 20 70 54 61 62 3d 3d  ( v==0 || pTab==
1ac7a 30 20 7c 7c 20 70 54 61 62 2d 3e 70 49 6e 64 65  0 || pTab->pInde
1ac7b 78 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 44  x==0 ){.    /* D
1ac7c 6f 20 6e 6f 20 61 6e 61 6c 79 73 69 73 20 66 6f  o no analysis fo
1ac7d 72 20 74 61 62 6c 65 73 20 74 68 61 74 20 68 61  r tables that ha
1ac7e 76 65 20 6e 6f 20 69 6e 64 69 63 65 73 20 2a 2f  ve no indices */
1ac7f 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
1ac80 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1ac81 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d  e3BtreeHoldsAllM
1ac82 75 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64  utexes(pParse->d
1ac83 62 29 20 29 3b 0a 20 20 69 44 62 20 3d 20 73 71  b) );.  iDb = sq
1ac84 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
1ac85 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  ex(pParse->db, p
1ac86 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  Tab->pSchema);. 
1ac87 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20   assert( iDb>=0 
1ac88 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
1ac89 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41  E_OMIT_AUTHORIZA
1ac8a 54 49 4f 4e 0a 20 20 69 66 28 20 73 71 6c 69 74  TION.  if( sqlit
1ac8b 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
1ac8c 73 65 2c 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59  se, SQLITE_ANALY
1ac8d 5a 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  ZE, pTab->zName,
1ac8e 20 30 2c 0a 20 20 20 20 20 20 70 50 61 72 73 65   0,.      pParse
1ac8f 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  ->db->aDb[iDb].z
1ac90 4e 61 6d 65 20 29 20 29 7b 0a 20 20 20 20 72 65  Name ) ){.    re
1ac91 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  turn;.  }.#endif
1ac92 0a 0a 20 20 2f 2a 20 45 73 74 61 62 6c 69 73 68  ..  /* Establish
1ac93 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20   a read-lock on 
1ac94 74 68 65 20 74 61 62 6c 65 20 61 74 20 74 68 65  the table at the
1ac95 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c 65   shared-cache le
1ac96 76 65 6c 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  vel. */.  sqlite
1ac97 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73  3TableLock(pPars
1ac98 65 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e  e, iDb, pTab->tn
1ac99 75 6d 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61  um, 0, pTab->zNa
1ac9a 6d 65 29 3b 0a 0a 20 20 69 49 64 78 43 75 72 20  me);..  iIdxCur 
1ac9b 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a  = pParse->nTab;.
1ac9c 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d    for(pIdx=pTab-
1ac9d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
1ac9e 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
1ac9f 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70  {.    KeyInfo *p
1aca0 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64  Key = sqlite3Ind
1aca1 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65  exKeyinfo(pParse
1aca2 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 69 6e 74  , pIdx);.    int
1aca3 20 72 65 67 46 69 65 6c 64 73 3b 20 20 20 20 2f   regFields;    /
1aca4 2a 20 52 65 67 69 73 74 65 72 20 62 6c 6f 63 6b  * Register block
1aca5 20 66 6f 72 20 62 75 69 6c 64 69 6e 67 20 72 65   for building re
1aca6 63 6f 72 64 73 20 2a 2f 0a 20 20 20 20 69 6e 74  cords */.    int
1aca7 20 72 65 67 52 65 63 3b 20 20 20 20 20 20 20 2f   regRec;       /
1aca8 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69  * Register holdi
1aca9 6e 67 20 63 6f 6d 70 6c 65 74 65 64 20 72 65 63  ng completed rec
1acaa 6f 72 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72  ord */.    int r
1acab 65 67 54 65 6d 70 3b 20 20 20 20 20 20 2f 2a 20  egTemp;      /* 
1acac 54 65 6d 70 6f 72 61 72 79 20 75 73 65 20 72 65  Temporary use re
1acad 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e  gister */.    in
1acae 74 20 72 65 67 43 6f 6c 3b 20 20 20 20 20 20 20  t regCol;       
1acaf 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 61 20  /* Content of a 
1acb0 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 74 68 65 20  column from the 
1acb1 74 61 62 6c 65 20 62 65 69 6e 67 20 61 6e 61 6c  table being anal
1acb2 79 7a 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20  yzed */.    int 
1acb3 72 65 67 52 6f 77 69 64 3b 20 20 20 20 20 2f 2a  regRowid;     /*
1acb4 20 52 6f 77 69 64 20 66 6f 72 20 74 68 65 20 69   Rowid for the i
1acb5 6e 73 65 72 74 65 64 20 72 65 63 6f 72 64 20 2a  nserted record *
1acb6 2f 0a 20 20 20 20 69 6e 74 20 72 65 67 46 32 3b  /.    int regF2;
1acb7 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 61 20  ..    /* Open a 
1acb8 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 69 6e  cursor to the in
1acb9 64 65 78 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a  dex to be analyz
1acba 65 64 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73  ed.    */.    as
1acbb 73 65 72 74 28 20 69 44 62 3d 3d 73 71 6c 69 74  sert( iDb==sqlit
1acbc 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
1acbd 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 49 64 78  pParse->db, pIdx
1acbe 2d 3e 70 53 63 68 65 6d 61 29 20 29 3b 0a 20 20  ->pSchema) );.  
1acbf 20 20 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e 6e    nCol = pIdx->n
1acc0 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 73 71 6c 69  Column;.    sqli
1acc1 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1acc2 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e   OP_SetNumColumn
1acc3 73 2c 20 30 2c 20 6e 43 6f 6c 2b 31 29 3b 0a 20  s, 0, nCol+1);. 
1acc4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1acc5 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52  dOp4(v, OP_OpenR
1acc6 65 61 64 2c 20 69 49 64 78 43 75 72 2c 20 70 49  ead, iIdxCur, pI
1acc7 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a 20  dx->tnum, iDb,. 
1acc8 20 20 20 20 20 20 20 28 63 68 61 72 20 2a 29 70         (char *)p
1acc9 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f  Key, P4_KEYINFO_
1acca 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 56 64  HANDOFF);.    Vd
1accb 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25  beComment((v, "%
1accc 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29  s", pIdx->zName)
1accd 29 3b 0a 20 20 20 20 72 65 67 46 69 65 6c 64 73  );.    regFields
1acce 20 3d 20 69 4d 65 6d 2b 6e 43 6f 6c 2a 32 3b 0a   = iMem+nCol*2;.
1accf 20 20 20 20 72 65 67 54 65 6d 70 20 3d 20 72 65      regTemp = re
1acd0 67 52 6f 77 69 64 20 3d 20 72 65 67 43 6f 6c 20  gRowid = regCol 
1acd1 3d 20 72 65 67 46 69 65 6c 64 73 2b 33 3b 0a 20  = regFields+3;. 
1acd2 20 20 20 72 65 67 52 65 63 20 3d 20 72 65 67 43     regRec = regC
1acd3 6f 6c 2b 31 3b 0a 20 20 20 20 69 66 28 20 72 65  ol+1;.    if( re
1acd4 67 52 65 63 3e 70 50 61 72 73 65 2d 3e 6e 4d 65  gRec>pParse->nMe
1acd5 6d 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73  m ){.      pPars
1acd6 65 2d 3e 6e 4d 65 6d 20 3d 20 72 65 67 52 65 63  e->nMem = regRec
1acd7 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
1acd8 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 61 72 65  Memory cells are
1acd9 20 75 73 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73   used as follows
1acda 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  :.    **.    ** 
1acdb 20 20 20 6d 65 6d 5b 69 4d 65 6d 5d 3a 20 20 20     mem[iMem]:   
1acdc 20 20 20 20 20 20 20 20 20 20 54 68 65 20 74 6f            The to
1acdd 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  tal number of ro
1acde 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e  ws in the table.
1acdf 0a 20 20 20 20 2a 2a 20 20 20 20 6d 65 6d 5b 69  .    **    mem[i
1ace0 4d 65 6d 2b 31 5d 3a 20 20 20 20 20 20 20 20 20  Mem+1]:         
1ace1 20 20 4e 75 6d 62 65 72 20 6f 66 20 64 69 73 74    Number of dist
1ace2 69 6e 63 74 20 76 61 6c 75 65 73 20 69 6e 20 63  inct values in c
1ace3 6f 6c 75 6d 6e 20 31 0a 20 20 20 20 2a 2a 20 20  olumn 1.    **  
1ace4 20 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20 20 20    ....    **    
1ace5 6d 65 6d 5b 69 4d 65 6d 2b 6e 43 6f 6c 5d 3a 20  mem[iMem+nCol]: 
1ace6 20 20 20 20 20 20 20 4e 75 6d 62 65 72 20 6f 66         Number of
1ace7 20 64 69 73 74 69 6e 63 74 20 76 61 6c 75 65 73   distinct values
1ace8 20 69 6e 20 63 6f 6c 75 6d 6e 20 4e 0a 20 20 20   in column N.   
1ace9 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d 2b   **    mem[iMem+
1acea 6e 43 6f 6c 2b 31 5d 20 20 20 20 20 20 20 4c 61  nCol+1]       La
1aceb 73 74 20 6f 62 73 65 72 76 65 64 20 76 61 6c 75  st observed valu
1acec 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 31 0a 20 20  e of column 1.  
1aced 20 20 2a 2a 20 20 20 20 2e 2e 2e 0a 20 20 20 20    **    ....    
1acee 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d 2b 6e  **    mem[iMem+n
1acef 43 6f 6c 2b 6e 43 6f 6c 5d 3a 20 20 20 4c 61 73  Col+nCol]:   Las
1acf0 74 20 6f 62 73 65 72 76 65 64 20 76 61 6c 75 65  t observed value
1acf1 20 6f 66 20 63 6f 6c 75 6d 6e 20 4e 0a 20 20 20   of column N.   
1acf2 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 65 6c 6c 73   **.    ** Cells
1acf3 20 69 4d 65 6d 20 74 68 72 6f 75 67 68 20 69 4d   iMem through iM
1acf4 65 6d 2b 6e 43 6f 6c 20 61 72 65 20 69 6e 69 74  em+nCol are init
1acf5 69 61 6c 69 7a 65 64 20 74 6f 20 30 2e 20 20 54  ialized to 0.  T
1acf6 68 65 20 6f 74 68 65 72 73 0a 20 20 20 20 2a 2a  he others.    **
1acf7 20 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64   are initialized
1acf8 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f   to NULL..    */
1acf9 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
1acfa 3d 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  =nCol; i++){.   
1acfb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1acfc 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
1acfd 65 72 2c 20 30 2c 20 69 4d 65 6d 2b 69 29 3b 0a  er, 0, iMem+i);.
1acfe 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d      }.    for(i=
1acff 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  0; i<nCol; i++){
1ad00 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1ad01 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
1ad02 75 6c 6c 2c 20 30 2c 20 69 4d 65 6d 2b 6e 43 6f  ull, 0, iMem+nCo
1ad03 6c 2b 69 2b 31 29 3b 0a 20 20 20 20 7d 0a 0a 20  l+i+1);.    }.. 
1ad04 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 61 6e 61     /* Do the ana
1ad05 6c 79 73 69 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  lysis..    */.  
1ad06 20 20 65 6e 64 4f 66 4c 6f 6f 70 20 3d 20 73 71    endOfLoop = sq
1ad07 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
1ad08 65 6c 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74  el(v);.    sqlit
1ad09 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1ad0a 4f 50 5f 52 65 77 69 6e 64 2c 20 69 49 64 78 43  OP_Rewind, iIdxC
1ad0b 75 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a  ur, endOfLoop);.
1ad0c 20 20 20 20 74 6f 70 4f 66 4c 6f 6f 70 20 3d 20      topOfLoop = 
1ad0d 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
1ad0e 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 73  ntAddr(v);.    s
1ad0f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1ad10 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 69  (v, OP_AddImm, i
1ad11 4d 65 6d 2c 20 31 29 3b 0a 20 20 20 20 66 6f 72  Mem, 1);.    for
1ad12 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b  (i=0; i<nCol; i+
1ad13 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  +){.      sqlite
1ad14 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
1ad15 50 5f 43 6f 6c 75 6d 6e 2c 20 69 49 64 78 43 75  P_Column, iIdxCu
1ad16 72 2c 20 69 2c 20 72 65 67 43 6f 6c 29 3b 0a 20  r, i, regCol);. 
1ad17 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1ad18 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65 2c  AddOp3(v, OP_Ne,
1ad19 20 72 65 67 43 6f 6c 2c 20 30 2c 20 69 4d 65 6d   regCol, 0, iMem
1ad1a 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 20  +nCol+i+1);.    
1ad1b 20 20 2f 2a 2a 2a 2a 20 54 4f 44 4f 3a 20 20 61    /**** TODO:  a
1ad1c 64 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  dd collating seq
1ad1d 75 65 6e 63 65 20 2a 2a 2a 2a 2a 2f 0a 20 20 20  uence *****/.   
1ad1e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
1ad1f 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54 45  angeP5(v, SQLITE
1ad20 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20  _JUMPIFNULL);.  
1ad21 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
1ad22 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1ad23 47 6f 74 6f 2c 20 30 2c 20 65 6e 64 4f 66 4c 6f  Goto, 0, endOfLo
1ad24 6f 70 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  op);.    for(i=0
1ad25 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a  ; i<nCol; i++){.
1ad26 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1ad27 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 74 6f 70  eJumpHere(v, top
1ad28 4f 66 4c 6f 6f 70 20 2b 20 32 2a 28 69 20 2b 20  OfLoop + 2*(i + 
1ad29 31 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  1));.      sqlit
1ad2a 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1ad2b 4f 50 5f 41 64 64 49 6d 6d 2c 20 69 4d 65 6d 2b  OP_AddImm, iMem+
1ad2c 69 2b 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 73  i+1, 1);.      s
1ad2d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
1ad2e 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69  (v, OP_Column, i
1ad2f 49 64 78 43 75 72 2c 20 69 2c 20 69 4d 65 6d 2b  IdxCur, i, iMem+
1ad30 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 20 7d  nCol+i+1);.    }
1ad31 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1ad32 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
1ad33 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20  endOfLoop);.    
1ad34 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1ad35 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 49  2(v, OP_Next, iI
1ad36 64 78 43 75 72 2c 20 74 6f 70 4f 66 4c 6f 6f 70  dxCur, topOfLoop
1ad37 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1ad38 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43  beAddOp1(v, OP_C
1ad39 6c 6f 73 65 2c 20 69 49 64 78 43 75 72 29 3b 0a  lose, iIdxCur);.
1ad3a 0a 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68  .    /* Store th
1ad3b 65 20 72 65 73 75 6c 74 73 2e 20 20 0a 20 20 20  e results.  .   
1ad3c 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 72   **.    ** The r
1ad3d 65 73 75 6c 74 20 69 73 20 61 20 73 69 6e 67 6c  esult is a singl
1ad3e 65 20 72 6f 77 20 6f 66 20 74 68 65 20 73 71 6c  e row of the sql
1ad3f 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2e  ite_stat1 table.
1ad40 20 20 54 68 65 20 66 69 72 73 74 0a 20 20 20 20    The first.    
1ad41 2a 2a 20 74 77 6f 20 63 6f 6c 75 6d 6e 73 20 61  ** two columns a
1ad42 72 65 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20  re the names of 
1ad43 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 69 6e  the table and in
1ad44 64 65 78 2e 20 20 54 68 65 20 74 68 69 72 64 20  dex.  The third 
1ad45 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2a 20 69 73  column.    ** is
1ad46 20 61 20 73 74 72 69 6e 67 20 63 6f 6d 70 6f 73   a string compos
1ad47 65 64 20 6f 66 20 61 20 6c 69 73 74 20 6f 66 20  ed of a list of 
1ad48 69 6e 74 65 67 65 72 20 73 74 61 74 69 73 74 69  integer statisti
1ad49 63 73 20 61 62 6f 75 74 20 74 68 65 0a 20 20 20  cs about the.   
1ad4a 20 2a 2a 20 69 6e 64 65 78 2e 20 20 54 68 65 20   ** index.  The 
1ad4b 66 69 72 73 74 20 69 6e 74 65 67 65 72 20 69 6e  first integer in
1ad4c 20 74 68 65 20 6c 69 73 74 20 69 73 20 74 68 65   the list is the
1ad4d 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
1ad4e 20 65 6e 74 69 72 65 73 0a 20 20 20 20 2a 2a 20   entires.    ** 
1ad4f 69 6e 20 74 68 65 20 69 6e 64 65 78 2e 20 20 54  in the index.  T
1ad50 68 65 72 65 20 69 73 20 6f 6e 65 20 61 64 64 69  here is one addi
1ad51 74 69 6f 6e 61 6c 20 69 6e 74 65 67 65 72 20 69  tional integer i
1ad52 6e 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20 65  n the list for e
1ad53 61 63 68 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d  ach.    ** colum
1ad54 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20  n of the table. 
1ad55 20 54 68 69 73 20 61 64 64 69 74 69 6f 6e 61 6c   This additional
1ad56 20 69 6e 74 65 67 65 72 20 69 73 20 61 20 67 75   integer is a gu
1ad57 65 73 73 20 6f 66 20 68 6f 77 20 6d 61 6e 79 0a  ess of how many.
1ad58 20 20 20 20 2a 2a 20 72 6f 77 73 20 6f 66 20 74      ** rows of t
1ad59 68 65 20 74 61 62 6c 65 20 74 68 65 20 69 6e 64  he table the ind
1ad5a 65 78 20 77 69 6c 6c 20 73 65 6c 65 63 74 2e 20  ex will select. 
1ad5b 20 49 66 20 44 20 69 73 20 74 68 65 20 63 6f 75   If D is the cou
1ad5c 6e 74 20 6f 66 20 64 69 73 74 69 6e 63 74 0a 20  nt of distinct. 
1ad5d 20 20 20 2a 2a 20 76 61 6c 75 65 73 20 61 6e 64     ** values and
1ad5e 20 4b 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20   K is the total 
1ad5f 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 2c 20  number of rows, 
1ad60 74 68 65 6e 20 74 68 65 20 69 6e 74 65 67 65 72  then the integer
1ad61 20 69 73 20 63 6f 6d 70 75 74 65 64 0a 20 20 20   is computed.   
1ad62 20 2a 2a 20 61 73 3a 0a 20 20 20 20 2a 2a 0a 20   ** as:.    **. 
1ad63 20 20 20 2a 2a 20 20 20 20 20 20 20 20 49 20 3d     **        I =
1ad64 20 28 4b 2b 44 2d 31 29 2f 44 0a 20 20 20 20 2a   (K+D-1)/D.    *
1ad65 2a 0a 20 20 20 20 2a 2a 20 49 66 20 4b 3d 3d 30  *.    ** If K==0
1ad66 20 74 68 65 6e 20 6e 6f 20 65 6e 74 72 79 20 69   then no entry i
1ad67 73 20 6d 61 64 65 20 69 6e 74 6f 20 74 68 65 20  s made into the 
1ad68 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62  sqlite_stat1 tab
1ad69 6c 65 2e 20 20 0a 20 20 20 20 2a 2a 20 49 66 20  le.  .    ** If 
1ad6a 4b 3e 30 20 74 68 65 6e 20 69 74 20 69 73 20 61  K>0 then it is a
1ad6b 6c 77 61 79 73 20 74 68 65 20 63 61 73 65 20 74  lways the case t
1ad6c 68 65 20 44 3e 30 20 73 6f 20 64 69 76 69 73 69  he D>0 so divisi
1ad6d 6f 6e 20 62 79 20 7a 65 72 6f 0a 20 20 20 20 2a  on by zero.    *
1ad6e 2a 20 69 73 20 6e 65 76 65 72 20 70 6f 73 73 69  * is never possi
1ad6f 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ble..    */.    
1ad70 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
1ad71 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
1ad72 66 4e 6f 74 2c 20 69 4d 65 6d 29 3b 0a 20 20 20  fNot, iMem);.   
1ad73 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1ad74 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
1ad75 2c 20 30 2c 20 72 65 67 46 69 65 6c 64 73 2c 20  , 0, regFields, 
1ad76 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  0, pTab->zName, 
1ad77 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
1ad78 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
1ad79 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67 46  String8, 0, regF
1ad7a 69 65 6c 64 73 2b 31 2c 20 30 2c 20 70 49 64 78  ields+1, 0, pIdx
1ad7b 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  ->zName, 0);.   
1ad7c 20 72 65 67 46 32 20 3d 20 72 65 67 46 69 65 6c   regF2 = regFiel
1ad7d 64 73 2b 32 3b 0a 20 20 20 20 73 71 6c 69 74 65  ds+2;.    sqlite
1ad7e 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1ad7f 50 5f 53 43 6f 70 79 2c 20 69 4d 65 6d 2c 20 72  P_SCopy, iMem, r
1ad80 65 67 46 32 29 3b 0a 20 20 20 20 66 6f 72 28 69  egF2);.    for(i
1ad81 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29  =0; i<nCol; i++)
1ad82 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
1ad83 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
1ad84 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67 54  String8, 0, regT
1ad85 65 6d 70 2c 20 30 2c 20 22 20 22 2c 20 30 29 3b  emp, 0, " ", 0);
1ad86 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1ad87 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
1ad88 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d 70 2c 20  oncat, regTemp, 
1ad89 72 65 67 46 32 2c 20 72 65 67 46 32 29 3b 0a 20  regF2, regF2);. 
1ad8a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1ad8b 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 64 64  AddOp3(v, OP_Add
1ad8c 2c 20 69 4d 65 6d 2c 20 69 4d 65 6d 2b 69 2b 31  , iMem, iMem+i+1
1ad8d 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20  , regTemp);.    
1ad8e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1ad8f 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d  Op2(v, OP_AddImm
1ad90 2c 20 72 65 67 54 65 6d 70 2c 20 2d 31 29 3b 0a  , regTemp, -1);.
1ad91 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1ad92 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 44 69  eAddOp3(v, OP_Di
1ad93 76 69 64 65 2c 20 69 4d 65 6d 2b 69 2b 31 2c 20  vide, iMem+i+1, 
1ad94 72 65 67 54 65 6d 70 2c 20 72 65 67 54 65 6d 70  regTemp, regTemp
1ad95 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1ad96 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
1ad97 5f 54 6f 49 6e 74 2c 20 72 65 67 54 65 6d 70 29  _ToInt, regTemp)
1ad98 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1ad99 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
1ad9a 43 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d 70 2c  Concat, regTemp,
1ad9b 20 72 65 67 46 32 2c 20 72 65 67 46 32 29 3b 0a   regF2, regF2);.
1ad9c 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
1ad9d 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
1ad9e 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65  P_MakeRecord, re
1ad9f 67 46 69 65 6c 64 73 2c 20 33 2c 20 72 65 67 52  gFields, 3, regR
1ada0 65 63 2c 20 22 61 61 61 22 2c 20 30 29 3b 0a 20  ec, "aaa", 0);. 
1ada1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1ada2 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f  dOp2(v, OP_NewRo
1ada3 77 69 64 2c 20 69 53 74 61 74 43 75 72 2c 20 72  wid, iStatCur, r
1ada4 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71  egRowid);.    sq
1ada5 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
1ada6 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69 53  v, OP_Insert, iS
1ada7 74 61 74 43 75 72 2c 20 72 65 67 52 65 63 2c 20  tatCur, regRec, 
1ada8 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73  regRowid);.    s
1ada9 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1adaa 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50  P5(v, OPFLAG_APP
1adab 45 4e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  END);.    sqlite
1adac 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
1adad 20 61 64 64 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f   addr);.  }.}../
1adae 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
1adaf 64 65 20 74 68 61 74 20 77 69 6c 6c 20 63 61 75  de that will cau
1adb0 73 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  se the most rece
1adb1 6e 74 20 69 6e 64 65 78 20 61 6e 61 6c 79 73 69  nt index analysi
1adb2 73 20 74 6f 0a 2a 2a 20 62 65 20 6c 61 6f 64 65  s to.** be laode
1adb3 64 20 69 6e 74 6f 20 69 6e 74 65 72 6e 61 6c 20  d into internal 
1adb4 68 61 73 68 20 74 61 62 6c 65 73 20 77 68 65 72  hash tables wher
1adb5 65 20 69 73 20 63 61 6e 20 62 65 20 75 73 65 64  e is can be used
1adb6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1adb7 20 6c 6f 61 64 41 6e 61 6c 79 73 69 73 28 50 61   loadAnalysis(Pa
1adb8 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
1adb9 20 69 44 62 29 7b 0a 20 20 56 64 62 65 20 2a 76   iDb){.  Vdbe *v
1adba 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
1adbb 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
1adbc 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65   v ){.    sqlite
1adbd 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
1adbe 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 2c 20  P_LoadAnalysis, 
1adbf 69 44 62 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  iDb);.  }.}../*.
1adc0 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
1adc1 20 74 68 61 74 20 77 69 6c 6c 20 64 6f 20 61 6e   that will do an
1adc2 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 61 6e 20   analysis of an 
1adc3 65 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 0a  entire database.
1adc4 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
1adc5 6e 61 6c 79 7a 65 44 61 74 61 62 61 73 65 28 50  nalyzeDatabase(P
1adc6 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
1adc7 74 20 69 44 62 29 7b 0a 20 20 73 71 6c 69 74 65  t iDb){.  sqlite
1adc8 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
1adc9 64 62 3b 0a 20 20 53 63 68 65 6d 61 20 2a 70 53  db;.  Schema *pS
1adca 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b  chema = db->aDb[
1adcb 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b 20 20 20  iDb].pSchema;   
1adcc 20 2f 2a 20 53 63 68 65 6d 61 20 6f 66 20 64 61   /* Schema of da
1adcd 74 61 62 61 73 65 20 69 44 62 20 2a 2f 0a 20 20  tabase iDb */.  
1adce 48 61 73 68 45 6c 65 6d 20 2a 6b 3b 0a 20 20 69  HashElem *k;.  i
1adcf 6e 74 20 69 53 74 61 74 43 75 72 3b 0a 20 20 69  nt iStatCur;.  i
1add0 6e 74 20 69 4d 65 6d 3b 0a 0a 20 20 73 71 6c 69  nt iMem;..  sqli
1add1 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
1add2 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30  ration(pParse, 0
1add3 2c 20 69 44 62 29 3b 0a 20 20 69 53 74 61 74 43  , iDb);.  iStatC
1add4 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  ur = pParse->nTa
1add5 62 2b 2b 3b 0a 20 20 6f 70 65 6e 53 74 61 74 54  b++;.  openStatT
1add6 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44 62  able(pParse, iDb
1add7 2c 20 69 53 74 61 74 43 75 72 2c 20 30 29 3b 0a  , iStatCur, 0);.
1add8 20 20 69 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d    iMem = pParse-
1add9 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 66 6f 72 28 6b  >nMem+1;.  for(k
1adda 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74  =sqliteHashFirst
1addb 28 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61  (&pSchema->tblHa
1addc 73 68 29 3b 20 6b 3b 20 6b 3d 73 71 6c 69 74 65  sh); k; k=sqlite
1addd 48 61 73 68 4e 65 78 74 28 6b 29 29 7b 0a 20 20  HashNext(k)){.  
1adde 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
1addf 28 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 48 61  (Table*)sqliteHa
1ade0 73 68 44 61 74 61 28 6b 29 3b 0a 20 20 20 20 61  shData(k);.    a
1ade1 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28 70  nalyzeOneTable(p
1ade2 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 53 74  Parse, pTab, iSt
1ade3 61 74 43 75 72 2c 20 69 4d 65 6d 29 3b 0a 20 20  atCur, iMem);.  
1ade4 7d 0a 20 20 6c 6f 61 64 41 6e 61 6c 79 73 69 73  }.  loadAnalysis
1ade5 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 7d  (pParse, iDb);.}
1ade6 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
1ade7 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20   code that will 
1ade8 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f  do an analysis o
1ade9 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65  f a single table
1adea 20 69 6e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73   in.** a databas
1adeb 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
1adec 64 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28 50  d analyzeTable(P
1aded 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61  arse *pParse, Ta
1adee 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 69 6e  ble *pTab){.  in
1adef 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 69 53 74  t iDb;.  int iSt
1adf0 61 74 43 75 72 3b 0a 0a 20 20 61 73 73 65 72 74  atCur;..  assert
1adf1 28 20 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 61  ( pTab!=0 );.  a
1adf2 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
1adf3 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78  reeHoldsAllMutex
1adf4 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20 29  es(pParse->db) )
1adf5 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  ;.  iDb = sqlite
1adf6 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
1adf7 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d  Parse->db, pTab-
1adf8 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 73 71 6c  >pSchema);.  sql
1adf9 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
1adfa 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
1adfb 30 2c 20 69 44 62 29 3b 0a 20 20 69 53 74 61 74  0, iDb);.  iStat
1adfc 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  Cur = pParse->nT
1adfd 61 62 2b 2b 3b 0a 20 20 6f 70 65 6e 53 74 61 74  ab++;.  openStat
1adfe 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44  Table(pParse, iD
1adff 62 2c 20 69 53 74 61 74 43 75 72 2c 20 70 54 61  b, iStatCur, pTa
1ae00 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 61 6e 61  b->zName);.  ana
1ae01 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28 70 50 61  lyzeOneTable(pPa
1ae02 72 73 65 2c 20 70 54 61 62 2c 20 69 53 74 61 74  rse, pTab, iStat
1ae03 43 75 72 2c 20 70 50 61 72 73 65 2d 3e 6e 4d 65  Cur, pParse->nMe
1ae04 6d 2b 31 29 3b 0a 20 20 6c 6f 61 64 41 6e 61 6c  m+1);.  loadAnal
1ae05 79 73 69 73 28 70 50 61 72 73 65 2c 20 69 44 62  ysis(pParse, iDb
1ae06 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  );.}../*.** Gene
1ae07 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74 68  rate code for th
1ae08 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e  e ANALYZE comman
1ae09 64 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 63  d.  The parser c
1ae0a 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e  alls this routin
1ae0b 65 0a 2a 2a 20 77 68 65 6e 20 69 74 20 72 65 63  e.** when it rec
1ae0c 6f 67 6e 69 7a 65 73 20 61 6e 20 41 4e 41 4c 59  ognizes an ANALY
1ae0d 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a  ZE command..**.*
1ae0e 2a 20 20 20 20 20 20 20 20 41 4e 41 4c 59 5a 45  *        ANALYZE
1ae0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ae10 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 31              -- 1
1ae11 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e 41 4c 59  .**        ANALY
1ae12 5a 45 20 20 3c 64 61 74 61 62 61 73 65 3e 20 20  ZE  <database>  
1ae13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d                --
1ae14 20 32 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e 41   2.**        ANA
1ae15 4c 59 5a 45 20 20 3f 3c 64 61 74 61 62 61 73 65  LYZE  ?<database
1ae16 3e 2e 3f 3c 74 61 62 6c 65 6e 61 6d 65 3e 20 20  >.?<tablename>  
1ae17 2d 2d 20 33 0a 2a 2a 0a 2a 2a 20 46 6f 72 6d 20  -- 3.**.** Form 
1ae18 31 20 63 61 75 73 65 73 20 61 6c 6c 20 69 6e 64  1 causes all ind
1ae19 69 63 65 73 20 69 6e 20 61 6c 6c 20 61 74 74 61  ices in all atta
1ae1a 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20 74  ched databases t
1ae1b 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e 0a 2a  o be analyzed..*
1ae1c 2a 20 46 6f 72 6d 20 32 20 61 6e 61 6c 79 7a 65  * Form 2 analyze
1ae1d 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 74 68  s all indices th
1ae1e 65 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73  e single databas
1ae1f 65 20 6e 61 6d 65 64 2e 0a 2a 2a 20 46 6f 72 6d  e named..** Form
1ae20 20 33 20 61 6e 61 6c 79 7a 65 73 20 61 6c 6c 20   3 analyzes all 
1ae21 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69 61 74  indices associat
1ae22 65 64 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65  ed with the name
1ae23 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49  d table..*/.SQLI
1ae24 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1ae25 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28 50  sqlite3Analyze(P
1ae26 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f  arse *pParse, To
1ae27 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 54 6f 6b  ken *pName1, Tok
1ae28 65 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a 20 20 73  en *pName2){.  s
1ae29 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
1ae2a 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69  rse->db;.  int i
1ae2b 44 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63  Db;.  int i;.  c
1ae2c 68 61 72 20 2a 7a 2c 20 2a 7a 44 62 3b 0a 20 20  har *z, *zDb;.  
1ae2d 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 54  Table *pTab;.  T
1ae2e 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65  oken *pTableName
1ae2f 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65  ;..  /* Read the
1ae30 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
1ae31 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  . If an error oc
1ae32 63 75 72 73 2c 20 6c 65 61 76 65 20 61 6e 20 65  curs, leave an e
1ae33 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20 2a  rror message.  *
1ae34 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e 20 70 50  * and code in pP
1ae35 61 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e 20  arse and return 
1ae36 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73 73 65 72  NULL. */.  asser
1ae37 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
1ae38 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 70  oldsAllMutexes(p
1ae39 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20 20  Parse->db) );.  
1ae3a 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73  if( SQLITE_OK!=s
1ae3b 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
1ae3c 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20  (pParse) ){.    
1ae3d 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 69  return;.  }..  i
1ae3e 66 28 20 70 4e 61 6d 65 31 3d 3d 30 20 29 7b 0a  f( pName1==0 ){.
1ae3f 20 20 20 20 2f 2a 20 46 6f 72 6d 20 31 3a 20 20      /* Form 1:  
1ae40 41 6e 61 6c 79 7a 65 20 65 76 65 72 79 74 68 69  Analyze everythi
1ae41 6e 67 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d  ng */.    for(i=
1ae42 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
1ae43 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 3d  +){.      if( i=
1ae44 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20  =1 ) continue;  
1ae45 2f 2a 20 44 6f 20 6e 6f 74 20 61 6e 61 6c 79 7a  /* Do not analyz
1ae46 65 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 62  e the TEMP datab
1ae47 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 61 6e 61  ase */.      ana
1ae48 6c 79 7a 65 44 61 74 61 62 61 73 65 28 70 50 61  lyzeDatabase(pPa
1ae49 72 73 65 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20  rse, i);.    }. 
1ae4a 20 7d 65 6c 73 65 20 69 66 28 20 70 4e 61 6d 65   }else if( pName
1ae4b 32 3d 3d 30 20 7c 7c 20 70 4e 61 6d 65 32 2d 3e  2==0 || pName2->
1ae4c 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 46  n==0 ){.    /* F
1ae4d 6f 72 6d 20 32 3a 20 20 41 6e 61 6c 79 7a 65 20  orm 2:  Analyze 
1ae4e 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 20  the database or 
1ae4f 74 61 62 6c 65 20 6e 61 6d 65 64 20 2a 2f 0a 20  table named */. 
1ae50 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33     iDb = sqlite3
1ae51 46 69 6e 64 44 62 28 64 62 2c 20 70 4e 61 6d 65  FindDb(db, pName
1ae52 31 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3e  1);.    if( iDb>
1ae53 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 6e 61 6c  =0 ){.      anal
1ae54 79 7a 65 44 61 74 61 62 61 73 65 28 70 50 61 72  yzeDatabase(pPar
1ae55 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 7d 65  se, iDb);.    }e
1ae56 6c 73 65 7b 0a 20 20 20 20 20 20 7a 20 3d 20 73  lse{.      z = s
1ae57 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
1ae58 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 31 29 3b  ken(db, pName1);
1ae59 0a 20 20 20 20 20 20 69 66 28 20 7a 20 29 7b 0a  .      if( z ){.
1ae5a 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 73          pTab = s
1ae5b 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c  qlite3LocateTabl
1ae5c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 7a 2c 20  e(pParse, 0, z, 
1ae5d 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  0);.        sqli
1ae5e 74 65 33 5f 66 72 65 65 28 7a 29 3b 0a 20 20 20  te3_free(z);.   
1ae5f 20 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b       if( pTab ){
1ae60 0a 20 20 20 20 20 20 20 20 20 20 61 6e 61 6c 79  .          analy
1ae61 7a 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  zeTable(pParse, 
1ae62 70 54 61 62 29 3b 0a 20 20 20 20 20 20 20 20 7d  pTab);.        }
1ae63 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1ae64 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 46   }else{.    /* F
1ae65 6f 72 6d 20 33 3a 20 41 6e 61 6c 79 7a 65 20 74  orm 3: Analyze t
1ae66 68 65 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69  he fully qualifi
1ae67 65 64 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f  ed table name */
1ae68 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74  .    iDb = sqlit
1ae69 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50  e3TwoPartName(pP
1ae6a 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e  arse, pName1, pN
1ae6b 61 6d 65 32 2c 20 26 70 54 61 62 6c 65 4e 61 6d  ame2, &pTableNam
1ae6c 65 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3e  e);.    if( iDb>
1ae6d 3d 30 20 29 7b 0a 20 20 20 20 20 20 7a 44 62 20  =0 ){.      zDb 
1ae6e 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
1ae6f 4e 61 6d 65 3b 0a 20 20 20 20 20 20 7a 20 3d 20  Name;.      z = 
1ae70 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
1ae71 6f 6b 65 6e 28 64 62 2c 20 70 54 61 62 6c 65 4e  oken(db, pTableN
1ae72 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ame);.      if( 
1ae73 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 61  z ){.        pTa
1ae74 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74  b = sqlite3Locat
1ae75 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30  eTable(pParse, 0
1ae76 2c 20 7a 2c 20 7a 44 62 29 3b 0a 20 20 20 20 20  , z, zDb);.     
1ae77 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
1ae78 7a 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  z);.        if( 
1ae79 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20  pTab ){.        
1ae7a 20 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28 70    analyzeTable(p
1ae7b 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20  Parse, pTab);.  
1ae7c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1ae7d 20 20 20 20 7d 20 20 20 0a 20 20 7d 0a 7d 0a 0a      }   .  }.}..
1ae7e 2f 2a 0a 2a 2a 20 55 73 65 64 20 74 6f 20 70 61  /*.** Used to pa
1ae7f 73 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  ss information f
1ae80 72 6f 6d 20 74 68 65 20 61 6e 61 6c 79 7a 65 72  rom the analyzer
1ae81 20 72 65 61 64 65 72 20 74 68 72 6f 75 67 68 20   reader through 
1ae82 74 6f 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 62 61  to the.** callba
1ae83 63 6b 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 74  ck routine..*/.t
1ae84 79 70 65 64 65 66 20 73 74 72 75 63 74 20 61 6e  ypedef struct an
1ae85 61 6c 79 73 69 73 49 6e 66 6f 20 61 6e 61 6c 79  alysisInfo analy
1ae86 73 69 73 49 6e 66 6f 3b 0a 73 74 72 75 63 74 20  sisInfo;.struct 
1ae87 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 7b 0a 20  analysisInfo {. 
1ae88 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20   sqlite3 *db;.  
1ae89 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74  const char *zDat
1ae8a 61 62 61 73 65 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  abase;.};../*.**
1ae8b 20 54 68 69 73 20 63 61 6c 6c 62 61 63 6b 20 69   This callback i
1ae8c 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66  s invoked once f
1ae8d 6f 72 20 65 61 63 68 20 69 6e 64 65 78 20 77 68  or each index wh
1ae8e 65 6e 20 72 65 61 64 69 6e 67 20 74 68 65 0a 2a  en reading the.*
1ae8f 2a 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74  * sqlite_stat1 t
1ae90 61 62 6c 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 20 20  able.  .**.**   
1ae91 20 20 61 72 67 76 5b 30 5d 20 3d 20 6e 61 6d 65    argv[0] = name
1ae92 20 6f 66 20 74 68 65 20 69 6e 64 65 78 0a 2a 2a   of the index.**
1ae93 20 20 20 20 20 61 72 67 76 5b 31 5d 20 3d 20 72       argv[1] = r
1ae94 65 73 75 6c 74 73 20 6f 66 20 61 6e 61 6c 79 73  esults of analys
1ae95 69 73 20 2d 20 6f 6e 20 69 6e 74 65 67 65 72 20  is - on integer 
1ae96 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 0a  for each column.
1ae97 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6e  */.static int an
1ae98 61 6c 79 73 69 73 4c 6f 61 64 65 72 28 76 6f 69  alysisLoader(voi
1ae99 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 61 72  d *pData, int ar
1ae9a 67 63 2c 20 63 68 61 72 20 2a 2a 61 72 67 76 2c  gc, char **argv,
1ae9b 20 63 68 61 72 20 2a 2a 61 7a 4e 6f 74 55 73 65   char **azNotUse
1ae9c 64 29 7b 0a 20 20 61 6e 61 6c 79 73 69 73 49 6e  d){.  analysisIn
1ae9d 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 28 61 6e 61  fo *pInfo = (ana
1ae9e 6c 79 73 69 73 49 6e 66 6f 2a 29 70 44 61 74 61  lysisInfo*)pData
1ae9f 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65  ;.  Index *pInde
1aea0 78 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20  x;.  int i, c;. 
1aea1 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 76 3b   unsigned int v;
1aea2 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1aea3 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67  ;..  assert( arg
1aea4 63 3d 3d 32 20 29 3b 0a 20 20 69 66 28 20 61 72  c==2 );.  if( ar
1aea5 67 76 3d 3d 30 20 7c 7c 20 61 72 67 76 5b 30 5d  gv==0 || argv[0]
1aea6 3d 3d 30 20 7c 7c 20 61 72 67 76 5b 31 5d 3d 3d  ==0 || argv[1]==
1aea7 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
1aea8 30 3b 0a 20 20 7d 0a 20 20 70 49 6e 64 65 78 20  0;.  }.  pIndex 
1aea9 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64  = sqlite3FindInd
1aeaa 65 78 28 70 49 6e 66 6f 2d 3e 64 62 2c 20 61 72  ex(pInfo->db, ar
1aeab 67 76 5b 30 5d 2c 20 70 49 6e 66 6f 2d 3e 7a 44  gv[0], pInfo->zD
1aeac 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20  atabase);.  if( 
1aead 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20  pIndex==0 ){.   
1aeae 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
1aeaf 20 7a 20 3d 20 61 72 67 76 5b 31 5d 3b 0a 20 20   z = argv[1];.  
1aeb0 66 6f 72 28 69 3d 30 3b 20 2a 7a 20 26 26 20 69  for(i=0; *z && i
1aeb1 3c 3d 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d  <=pIndex->nColum
1aeb2 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20 3d  n; i++){.    v =
1aeb3 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28   0;.    while( (
1aeb4 63 3d 7a 5b 30 5d 29 3e 3d 27 30 27 20 26 26 20  c=z[0])>='0' && 
1aeb5 63 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20 20 20  c<='9' ){.      
1aeb6 76 20 3d 20 76 2a 31 30 20 2b 20 63 20 2d 20 27  v = v*10 + c - '
1aeb7 30 27 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20  0';.      z++;. 
1aeb8 20 20 20 7d 0a 20 20 20 20 70 49 6e 64 65 78 2d     }.    pIndex-
1aeb9 3e 61 69 52 6f 77 45 73 74 5b 69 5d 20 3d 20 76  >aiRowEst[i] = v
1aeba 3b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27 20  ;.    if( *z==' 
1aebb 27 20 29 20 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 72  ' ) z++;.  }.  r
1aebc 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
1aebd 2a 20 4c 6f 61 64 20 74 68 65 20 63 6f 6e 74 65  * Load the conte
1aebe 6e 74 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  nt of the sqlite
1aebf 5f 73 74 61 74 31 20 74 61 62 6c 65 20 69 6e 74  _stat1 table int
1aec0 6f 20 74 68 65 20 69 6e 64 65 78 20 68 61 73 68  o the index hash
1aec1 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49   tables..*/.SQLI
1aec2 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1aec3 71 6c 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f  qlite3AnalysisLo
1aec4 61 64 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ad(sqlite3 *db, 
1aec5 69 6e 74 20 69 44 62 29 7b 0a 20 20 61 6e 61 6c  int iDb){.  anal
1aec6 79 73 69 73 49 6e 66 6f 20 73 49 6e 66 6f 3b 0a  ysisInfo sInfo;.
1aec7 20 20 48 61 73 68 45 6c 65 6d 20 2a 69 3b 0a 20    HashElem *i;. 
1aec8 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 69   char *zSql;.  i
1aec9 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74  nt rc;..  assert
1aeca 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c  ( iDb>=0 && iDb<
1aecb 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73  db->nDb );.  ass
1aecc 65 72 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 62  ert( db->aDb[iDb
1aecd 5d 2e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73  ].pBt!=0 );.  as
1aece 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
1aecf 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 64 62 2d  eeHoldsMutex(db-
1aed0 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 29 20 29  >aDb[iDb].pBt) )
1aed1 3b 0a 0a 20 20 2f 2a 20 43 6c 65 61 72 20 61 6e  ;..  /* Clear an
1aed2 79 20 70 72 69 6f 72 20 73 74 61 74 69 73 74 69  y prior statisti
1aed3 63 73 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 73 71  cs */.  for(i=sq
1aed4 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26 64  liteHashFirst(&d
1aed5 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68  b->aDb[iDb].pSch
1aed6 65 6d 61 2d 3e 69 64 78 48 61 73 68 29 3b 69 3b  ema->idxHash);i;
1aed7 69 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74  i=sqliteHashNext
1aed8 28 69 29 29 7b 0a 20 20 20 20 49 6e 64 65 78 20  (i)){.    Index 
1aed9 2a 70 49 64 78 20 3d 20 73 71 6c 69 74 65 48 61  *pIdx = sqliteHa
1aeda 73 68 44 61 74 61 28 69 29 3b 0a 20 20 20 20 73  shData(i);.    s
1aedb 71 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f 77  qlite3DefaultRow
1aedc 45 73 74 28 70 49 64 78 29 3b 0a 20 20 7d 0a 0a  Est(pIdx);.  }..
1aedd 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61    /* Check to ma
1aede 6b 65 20 73 75 72 65 20 74 68 65 20 73 71 6c 69  ke sure the sqli
1aedf 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 65  te_stat1 table e
1aee0 78 69 73 74 73 73 20 2a 2f 0a 20 20 73 49 6e 66  xistss */.  sInf
1aee1 6f 2e 64 62 20 3d 20 64 62 3b 0a 20 20 73 49 6e  o.db = db;.  sIn
1aee2 66 6f 2e 7a 44 61 74 61 62 61 73 65 20 3d 20 64  fo.zDatabase = d
1aee3 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
1aee4 65 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  e;.  if( sqlite3
1aee5 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 22 73  FindTable(db, "s
1aee6 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 20 73 49  qlite_stat1", sI
1aee7 6e 66 6f 2e 7a 44 61 74 61 62 61 73 65 29 3d 3d  nfo.zDatabase)==
1aee8 30 20 29 7b 0a 20 20 20 20 20 72 65 74 75 72 6e  0 ){.     return
1aee9 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1aeea 20 7d 0a 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 6e   }...  /* Load n
1aeeb 65 77 20 73 74 61 74 69 73 74 69 63 73 20 6f 75  ew statistics ou
1aeec 74 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f  t of the sqlite_
1aeed 73 74 61 74 31 20 74 61 62 6c 65 20 2a 2f 0a 20  stat1 table */. 
1aeee 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 4d   zSql = sqlite3M
1aeef 50 72 69 6e 74 66 28 64 62 2c 20 22 53 45 4c 45  Printf(db, "SELE
1aef0 43 54 20 69 64 78 2c 20 73 74 61 74 20 46 52 4f  CT idx, stat FRO
1aef1 4d 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74  M %Q.sqlite_stat
1aef2 31 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  1",.            
1aef3 20 20 20 20 20 20 20 20 20 20 20 20 73 49 6e 66              sInf
1aef4 6f 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20  o.zDatabase);.  
1aef5 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
1aef6 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 72 63  etyOff(db);.  rc
1aef7 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28   = sqlite3_exec(
1aef8 64 62 2c 20 7a 53 71 6c 2c 20 61 6e 61 6c 79 73  db, zSql, analys
1aef9 69 73 4c 6f 61 64 65 72 2c 20 26 73 49 6e 66 6f  isLoader, &sInfo
1aefa 2c 20 30 29 3b 0a 20 20 28 76 6f 69 64 29 73 71  , 0);.  (void)sq
1aefb 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
1aefc 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
1aefd 65 28 7a 53 71 6c 29 3b 0a 20 20 72 65 74 75 72  e(zSql);.  retur
1aefe 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 65 6e 64 69 66  n rc;.}...#endif
1aeff 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1af00 41 4e 41 4c 59 5a 45 20 2a 2f 0a 0a 2f 2a 2a 2a  ANALYZE */../***
1af01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
1af02 6f 66 20 61 6e 61 6c 79 7a 65 2e 63 20 2a 2a 2a  of analyze.c ***
1af03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1af04 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1af05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
1af06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
1af07 6e 20 66 69 6c 65 20 61 74 74 61 63 68 2e 63 20  n file attach.c 
1af08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1af09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1af0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
1af0b 2a 20 32 30 30 33 20 41 70 72 69 6c 20 36 0a 2a  * 2003 April 6.*
1af0c 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
1af0d 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
1af0e 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
1af0f 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
1af10 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
1af11 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
1af12 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
1af13 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
1af14 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
1af15 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
1af16 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
1af17 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
1af18 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
1af19 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
1af1a 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
1af1b 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
1af1c 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
1af1d 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
1af1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1af1f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1af20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1af21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
1af22 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
1af23 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f  ins code used to
1af24 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 41   implement the A
1af25 54 54 41 43 48 20 61 6e 64 20 44 45 54 41 43 48  TTACH and DETACH
1af26 20 63 6f 6d 6d 61 6e 64 73 2e 0a 2a 2a 0a 2a 2a   commands..**.**
1af27 20 24 49 64 3a 20 61 74 74 61 63 68 2e 63 2c 76   $Id: attach.c,v
1af28 20 31 2e 37 35 20 32 30 30 38 2f 30 34 2f 31 37   1.75 2008/04/17
1af29 20 31 37 3a 30 32 3a 30 31 20 64 72 68 20 45 78   17:02:01 drh Ex
1af2a 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  p $.*/..#ifndef 
1af2b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41  SQLITE_OMIT_ATTA
1af2c 43 48 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65  CH./*.** Resolve
1af2d 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74   an expression t
1af2e 68 61 74 20 77 61 73 20 70 61 72 74 20 6f 66 20  hat was part of 
1af2f 61 6e 20 41 54 54 41 43 48 20 6f 72 20 44 45 54  an ATTACH or DET
1af30 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 20 54  ACH statement. T
1af31 68 69 73 0a 2a 2a 20 69 73 20 73 6c 69 67 68 74  his.** is slight
1af32 6c 79 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f  ly different fro
1af33 6d 20 72 65 73 6f 6c 76 69 6e 67 20 61 20 6e 6f  m resolving a no
1af34 72 6d 61 6c 20 53 51 4c 20 65 78 70 72 65 73 73  rmal SQL express
1af35 69 6f 6e 2c 20 62 65 63 61 75 73 65 20 73 69 6d  ion, because sim
1af36 70 6c 65 0a 2a 2a 20 69 64 65 6e 74 69 66 69 65  ple.** identifie
1af37 72 73 20 61 72 65 20 74 72 65 61 74 65 64 20 61  rs are treated a
1af38 73 20 73 74 72 69 6e 67 73 2c 20 6e 6f 74 20 70  s strings, not p
1af39 6f 73 73 69 62 6c 65 20 63 6f 6c 75 6d 6e 20 6e  ossible column n
1af3a 61 6d 65 73 20 6f 72 20 61 6c 69 61 73 65 73 2e  ames or aliases.
1af3b 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e 20 69 66 20 74  .**.** i.e. if t
1af3c 68 65 20 70 61 72 73 65 72 20 73 65 65 73 3a 0a  he parser sees:.
1af3d 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48  **.**     ATTACH
1af3e 20 44 41 54 41 42 41 53 45 20 61 62 63 20 41 53   DATABASE abc AS
1af3f 20 64 65 66 0a 2a 2a 0a 2a 2a 20 69 74 20 74 72   def.**.** it tr
1af40 65 61 74 73 20 74 68 65 20 74 77 6f 20 65 78 70  eats the two exp
1af41 72 65 73 73 69 6f 6e 73 20 61 73 20 6c 69 74 65  ressions as lite
1af42 72 61 6c 20 73 74 72 69 6e 67 73 20 27 61 62 63  ral strings 'abc
1af43 27 20 61 6e 64 20 27 64 65 66 27 20 69 6e 73 74  ' and 'def' inst
1af44 65 61 64 20 6f 66 0a 2a 2a 20 6c 6f 6f 6b 69 6e  ead of.** lookin
1af45 67 20 66 6f 72 20 63 6f 6c 75 6d 6e 73 20 6f 66  g for columns of
1af46 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 0a   the same name..
1af47 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 6e 6c 79 20  **.** This only 
1af48 61 70 70 6c 69 65 73 20 74 6f 20 74 68 65 20 72  applies to the r
1af49 6f 6f 74 20 6e 6f 64 65 20 6f 66 20 70 45 78 70  oot node of pExp
1af4a 72 2c 20 73 6f 20 74 68 65 20 73 74 61 74 65 6d  r, so the statem
1af4b 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41  ent:.**.**     A
1af4c 54 54 41 43 48 20 44 41 54 41 42 41 53 45 20 61  TTACH DATABASE a
1af4d 62 63 7c 7c 64 65 66 20 41 53 20 27 64 62 32 27  bc||def AS 'db2'
1af4e 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 66 61 69 6c  .**.** will fail
1af4f 20 62 65 63 61 75 73 65 20 6e 65 69 74 68 65 72   because neither
1af50 20 61 62 63 20 6f 72 20 64 65 66 20 63 61 6e 20   abc or def can 
1af51 62 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a 2f 0a  be resolved..*/.
1af52 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c  static int resol
1af53 76 65 41 74 74 61 63 68 45 78 70 72 28 4e 61 6d  veAttachExpr(Nam
1af54 65 43 6f 6e 74 65 78 74 20 2a 70 4e 61 6d 65 2c  eContext *pName,
1af55 20 45 78 70 72 20 2a 70 45 78 70 72 29 0a 7b 0a   Expr *pExpr).{.
1af56 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1af57 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 45 78 70  E_OK;.  if( pExp
1af58 72 20 29 7b 0a 20 20 20 20 69 66 28 20 70 45 78  r ){.    if( pEx
1af59 70 72 2d 3e 6f 70 21 3d 54 4b 5f 49 44 20 29 7b  pr->op!=TK_ID ){
1af5a 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1af5b 74 65 33 45 78 70 72 52 65 73 6f 6c 76 65 4e 61  te3ExprResolveNa
1af5c 6d 65 73 28 70 4e 61 6d 65 2c 20 70 45 78 70 72  mes(pName, pExpr
1af5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
1af5e 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 73  =SQLITE_OK && !s
1af5f 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73  qlite3ExprIsCons
1af60 74 61 6e 74 28 70 45 78 70 72 29 20 29 7b 0a 20  tant(pExpr) ){. 
1af61 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
1af62 72 6f 72 4d 73 67 28 70 4e 61 6d 65 2d 3e 70 50  rorMsg(pName->pP
1af63 61 72 73 65 2c 20 22 69 6e 76 61 6c 69 64 20 6e  arse, "invalid n
1af64 61 6d 65 3a 20 5c 22 25 54 5c 22 22 2c 20 26 70  ame: \"%T\"", &p
1af65 45 78 70 72 2d 3e 73 70 61 6e 29 3b 0a 20 20 20  Expr->span);.   
1af66 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
1af67 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20  TE_ERROR;.      
1af68 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
1af69 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54     pExpr->op = T
1af6a 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 7d 0a  K_STRING;.    }.
1af6b 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1af6c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51 4c  .}../*.** An SQL
1af6d 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 72   user-function r
1af6e 65 67 69 73 74 65 72 65 64 20 74 6f 20 64 6f 20  egistered to do 
1af6f 74 68 65 20 77 6f 72 6b 20 6f 66 20 61 6e 20 41  the work of an A
1af70 54 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e  TTACH statement.
1af71 20 54 68 65 0a 2a 2a 20 74 68 72 65 65 20 61 72   The.** three ar
1af72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66  guments to the f
1af73 75 6e 63 74 69 6f 6e 20 63 6f 6d 65 20 64 69 72  unction come dir
1af74 65 63 74 6c 79 20 66 72 6f 6d 20 61 6e 20 61 74  ectly from an at
1af75 74 61 63 68 20 73 74 61 74 65 6d 65 6e 74 3a 0a  tach statement:.
1af76 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48  **.**     ATTACH
1af77 20 44 41 54 41 42 41 53 45 20 78 20 41 53 20 79   DATABASE x AS y
1af78 20 4b 45 59 20 7a 0a 2a 2a 0a 2a 2a 20 20 20 20   KEY z.**.**    
1af79 20 53 45 4c 45 43 54 20 73 71 6c 69 74 65 5f 61   SELECT sqlite_a
1af7a 74 74 61 63 68 28 78 2c 20 79 2c 20 7a 29 0a 2a  ttach(x, y, z).*
1af7b 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69  *.** If the opti
1af7c 6f 6e 61 6c 20 22 4b 45 59 20 7a 22 20 73 79 6e  onal "KEY z" syn
1af7d 74 61 78 20 69 73 20 6f 6d 69 74 74 65 64 2c 20  tax is omitted, 
1af7e 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 69 73 20 70  an SQL NULL is p
1af7f 61 73 73 65 64 20 61 73 20 74 68 65 0a 2a 2a 20  assed as the.** 
1af80 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 2e 0a  third argument..
1af81 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
1af82 74 74 61 63 68 46 75 6e 63 28 0a 20 20 73 71 6c  ttachFunc(.  sql
1af83 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
1af84 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
1af85 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
1af86 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69  ue **argv.){.  i
1af87 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d  nt i;.  int rc =
1af88 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   0;.  sqlite3 *d
1af89 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  b = sqlite3_cont
1af8a 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
1af8b 6e 74 65 78 74 29 3b 0a 20 20 63 6f 6e 73 74 20  ntext);.  const 
1af8c 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 63  char *zName;.  c
1af8d 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
1af8e 3b 0a 20 20 44 62 20 2a 61 4e 65 77 3b 0a 20 20  ;.  Db *aNew;.  
1af8f 63 68 61 72 20 2a 7a 45 72 72 44 79 6e 20 3d 20  char *zErrDyn = 
1af90 30 3b 0a 20 20 63 68 61 72 20 7a 45 72 72 5b 31  0;.  char zErr[1
1af91 32 38 5d 3b 0a 0a 20 20 7a 46 69 6c 65 20 3d 20  28];..  zFile = 
1af92 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71  (const char *)sq
1af93 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1af94 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 7a 4e 61  (argv[0]);.  zNa
1af95 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  me = (const char
1af96 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
1af97 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a  _text(argv[1]);.
1af98 20 20 69 66 28 20 7a 46 69 6c 65 3d 3d 30 20 29    if( zFile==0 )
1af99 20 7a 46 69 6c 65 20 3d 20 22 22 3b 0a 20 20 69   zFile = "";.  i
1af9a 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 7a 4e  f( zName==0 ) zN
1af9b 61 6d 65 20 3d 20 22 22 3b 0a 0a 20 20 2f 2a 20  ame = "";..  /* 
1af9c 43 68 65 63 6b 20 66 6f 72 20 74 68 65 20 66 6f  Check for the fo
1af9d 6c 6c 6f 77 69 6e 67 20 65 72 72 6f 72 73 3a 0a  llowing errors:.
1af9e 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 2a 20    **.  **     * 
1af9f 54 6f 6f 20 6d 61 6e 79 20 61 74 74 61 63 68 65  Too many attache
1afa0 64 20 64 61 74 61 62 61 73 65 73 2c 0a 20 20 2a  d databases,.  *
1afa1 2a 20 20 20 20 20 2a 20 54 72 61 6e 73 61 63 74  *     * Transact
1afa2 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 6f 70  ion currently op
1afa3 65 6e 0a 20 20 2a 2a 20 20 20 20 20 2a 20 53 70  en.  **     * Sp
1afa4 65 63 69 66 69 65 64 20 64 61 74 61 62 61 73 65  ecified database
1afa5 20 6e 61 6d 65 20 61 6c 72 65 61 64 79 20 62 65   name already be
1afa6 69 6e 67 20 75 73 65 64 2e 0a 20 20 2a 2f 0a 20  ing used..  */. 
1afa7 20 69 66 28 20 64 62 2d 3e 6e 44 62 3e 3d 64 62   if( db->nDb>=db
1afa8 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
1afa9 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 44 5d 2b  LIMIT_ATTACHED]+
1afaa 32 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  2 ){.    sqlite3
1afab 5f 73 6e 70 72 69 6e 74 66 28 0a 20 20 20 20 20  _snprintf(.     
1afac 20 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a   sizeof(zErr), z
1afad 45 72 72 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 61  Err, "too many a
1afae 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
1afaf 73 20 2d 20 6d 61 78 20 25 64 22 2c 20 0a 20 20  s - max %d", .  
1afb0 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53      db->aLimit[S
1afb1 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41  QLITE_LIMIT_ATTA
1afb2 43 48 45 44 5d 0a 20 20 20 20 29 3b 0a 20 20 20  CHED].    );.   
1afb3 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 72 72   goto attach_err
1afb4 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 64  or;.  }.  if( !d
1afb5 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b  b->autoCommit ){
1afb6 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
1afb7 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72  rintf(sizeof(zEr
1afb8 72 29 2c 20 7a 45 72 72 2c 0a 20 20 20 20 20 20  r), zErr,.      
1afb9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
1afba 63 61 6e 6e 6f 74 20 41 54 54 41 43 48 20 64 61  cannot ATTACH da
1afbb 74 61 62 61 73 65 20 77 69 74 68 69 6e 20 74 72  tabase within tr
1afbc 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20 20  ansaction");.   
1afbd 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 72 72   goto attach_err
1afbe 6f 72 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  or;.  }.  for(i=
1afbf 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
1afc0 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 20  +){.    char *z 
1afc1 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61  = db->aDb[i].zNa
1afc2 6d 65 3b 0a 20 20 20 20 69 66 28 20 7a 20 26 26  me;.    if( z &&
1afc3 20 7a 4e 61 6d 65 20 26 26 20 73 71 6c 69 74 65   zName && sqlite
1afc4 33 53 74 72 49 43 6d 70 28 7a 2c 20 7a 4e 61 6d  3StrICmp(z, zNam
1afc5 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  e)==0 ){.      s
1afc6 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1afc7 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45  sizeof(zErr), zE
1afc8 72 72 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  rr, .           
1afc9 20 20 20 20 20 20 20 20 20 20 20 20 22 64 61 74              "dat
1afca 61 62 61 73 65 20 25 73 20 69 73 20 61 6c 72 65  abase %s is alre
1afcb 61 64 79 20 69 6e 20 75 73 65 22 2c 20 7a 4e 61  ady in use", zNa
1afcc 6d 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  me);.      goto 
1afcd 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20  attach_error;.  
1afce 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c    }.  }..  /* Al
1afcf 6c 6f 63 61 74 65 20 74 68 65 20 6e 65 77 20 65  locate the new e
1afd0 6e 74 72 79 20 69 6e 20 74 68 65 20 64 62 2d 3e  ntry in the db->
1afd1 61 44 62 5b 5d 20 61 72 72 61 79 20 61 6e 64 20  aDb[] array and 
1afd2 69 6e 69 74 69 61 6c 69 73 65 20 74 68 65 20 73  initialise the s
1afd3 63 68 65 6d 61 0a 20 20 2a 2a 20 68 61 73 68 20  chema.  ** hash 
1afd4 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69  tables..  */.  i
1afd5 66 28 20 64 62 2d 3e 61 44 62 3d 3d 64 62 2d 3e  f( db->aDb==db->
1afd6 61 44 62 53 74 61 74 69 63 20 29 7b 0a 20 20 20  aDbStatic ){.   
1afd7 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f   aNew = sqlite3_
1afd8 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 64  malloc( sizeof(d
1afd9 62 2d 3e 61 44 62 5b 30 5d 29 2a 33 20 29 3b 0a  b->aDb[0])*3 );.
1afda 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20      if( aNew==0 
1afdb 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c  ){.      db->mal
1afdc 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
1afdd 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
1afde 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 4e   }.    memcpy(aN
1afdf 65 77 2c 20 64 62 2d 3e 61 44 62 2c 20 73 69 7a  ew, db->aDb, siz
1afe0 65 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d 29 2a  eof(db->aDb[0])*
1afe1 32 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  2);.  }else{.   
1afe2 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f   aNew = sqlite3_
1afe3 72 65 61 6c 6c 6f 63 28 64 62 2d 3e 61 44 62 2c  realloc(db->aDb,
1afe4 20 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b   sizeof(db->aDb[
1afe5 30 5d 29 2a 28 64 62 2d 3e 6e 44 62 2b 31 29 20  0])*(db->nDb+1) 
1afe6 29 3b 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d  );.    if( aNew=
1afe7 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e  =0 ){.      db->
1afe8 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
1afe9 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
1afea 20 20 20 20 7d 20 0a 20 20 7d 0a 20 20 64 62 2d      } .  }.  db-
1afeb 3e 61 44 62 20 3d 20 61 4e 65 77 3b 0a 20 20 61  >aDb = aNew;.  a
1afec 4e 65 77 20 3d 20 26 64 62 2d 3e 61 44 62 5b 64  New = &db->aDb[d
1afed 62 2d 3e 6e 44 62 2b 2b 5d 3b 0a 20 20 6d 65 6d  b->nDb++];.  mem
1afee 73 65 74 28 61 4e 65 77 2c 20 30 2c 20 73 69 7a  set(aNew, 0, siz
1afef 65 6f 66 28 2a 61 4e 65 77 29 29 3b 0a 0a 20 20  eof(*aNew));..  
1aff0 2f 2a 20 4f 70 65 6e 20 74 68 65 20 64 61 74 61  /* Open the data
1aff1 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68  base file. If th
1aff2 65 20 62 74 72 65 65 20 69 73 20 73 75 63 63 65  e btree is succe
1aff3 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 2c 20  ssfully opened, 
1aff4 75 73 65 0a 20 20 2a 2a 20 69 74 20 74 6f 20 6f  use.  ** it to o
1aff5 62 74 61 69 6e 20 74 68 65 20 64 61 74 61 62 61  btain the databa
1aff6 73 65 20 73 63 68 65 6d 61 2e 20 41 74 20 74 68  se schema. At th
1aff7 69 73 20 70 6f 69 6e 74 20 74 68 65 20 73 63 68  is point the sch
1aff8 65 6d 61 20 6d 61 79 0a 20 20 2a 2a 20 6f 72 20  ema may.  ** or 
1aff9 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e 69 74 69  may not be initi
1affa 61 6c 69 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 72  alised..  */.  r
1affb 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1affc 46 61 63 74 6f 72 79 28 64 62 2c 20 7a 46 69 6c  Factory(db, zFil
1affd 65 2c 20 30 2c 20 53 51 4c 49 54 45 5f 44 45 46  e, 0, SQLITE_DEF
1affe 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 2c  AULT_CACHE_SIZE,
1afff 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1b000 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e              db->
1b001 6f 70 65 6e 46 6c 61 67 73 20 7c 20 53 51 4c 49  openFlags | SQLI
1b002 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 2c  TE_OPEN_MAIN_DB,
1b003 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1b004 20 20 20 20 20 20 20 20 20 20 20 20 26 61 4e 65              &aNe
1b005 77 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28 20 72  w->pBt);.  if( r
1b006 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1b007 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65      Pager *pPage
1b008 72 3b 0a 20 20 20 20 61 4e 65 77 2d 3e 70 53 63  r;.    aNew->pSc
1b009 68 65 6d 61 20 3d 20 73 71 6c 69 74 65 33 53 63  hema = sqlite3Sc
1b00a 68 65 6d 61 47 65 74 28 64 62 2c 20 61 4e 65 77  hemaGet(db, aNew
1b00b 2d 3e 70 42 74 29 3b 0a 20 20 20 20 69 66 28 20  ->pBt);.    if( 
1b00c 21 61 4e 65 77 2d 3e 70 53 63 68 65 6d 61 20 29  !aNew->pSchema )
1b00d 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
1b00e 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
1b00f 65 6c 73 65 20 69 66 28 20 61 4e 65 77 2d 3e 70  else if( aNew->p
1b010 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72  Schema->file_for
1b011 6d 61 74 20 26 26 20 61 4e 65 77 2d 3e 70 53 63  mat && aNew->pSc
1b012 68 65 6d 61 2d 3e 65 6e 63 21 3d 45 4e 43 28 64  hema->enc!=ENC(d
1b013 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  b) ){.      sqli
1b014 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
1b015 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c  eof(zErr), zErr,
1b016 20 0a 20 20 20 20 20 20 20 20 22 61 74 74 61 63   .        "attac
1b017 68 65 64 20 64 61 74 61 62 61 73 65 73 20 6d 75  hed databases mu
1b018 73 74 20 75 73 65 20 74 68 65 20 73 61 6d 65 20  st use the same 
1b019 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 61 73  text encoding as
1b01a 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 22 29   main database")
1b01b 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 74 74  ;.      goto att
1b01c 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d  ach_error;.    }
1b01d 0a 20 20 20 20 70 50 61 67 65 72 20 3d 20 73 71  .    pPager = sq
1b01e 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
1b01f 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 20 20  aNew->pBt);.    
1b020 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b  sqlite3PagerLock
1b021 69 6e 67 4d 6f 64 65 28 70 50 61 67 65 72 2c 20  ingMode(pPager, 
1b022 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65  db->dfltLockMode
1b023 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61  );.    sqlite3Pa
1b024 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 70  gerJournalMode(p
1b025 50 61 67 65 72 2c 20 64 62 2d 3e 64 66 6c 74 4a  Pager, db->dfltJ
1b026 6f 75 72 6e 61 6c 4d 6f 64 65 29 3b 0a 20 20 7d  ournalMode);.  }
1b027 0a 20 20 61 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d  .  aNew->zName =
1b028 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
1b029 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 61  (db, zName);.  a
1b02a 4e 65 77 2d 3e 73 61 66 65 74 79 5f 6c 65 76 65  New->safety_leve
1b02b 6c 20 3d 20 33 3b 0a 0a 23 69 66 20 53 51 4c 49  l = 3;..#if SQLI
1b02c 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 7b  TE_HAS_CODEC.  {
1b02d 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e 74 20  .    extern int 
1b02e 73 71 6c 69 74 65 33 43 6f 64 65 63 41 74 74 61  sqlite3CodecAtta
1b02f 63 68 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  ch(sqlite3*, int
1b030 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69  , const void*, i
1b031 6e 74 29 3b 0a 20 20 20 20 65 78 74 65 72 6e 20  nt);.    extern 
1b032 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65  void sqlite3Code
1b033 63 47 65 74 4b 65 79 28 73 71 6c 69 74 65 33 2a  cGetKey(sqlite3*
1b034 2c 20 69 6e 74 2c 20 76 6f 69 64 2a 2a 2c 20 69  , int, void**, i
1b035 6e 74 2a 29 3b 0a 20 20 20 20 69 6e 74 20 6e 4b  nt*);.    int nK
1b036 65 79 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4b  ey;.    char *zK
1b037 65 79 3b 0a 20 20 20 20 69 6e 74 20 74 20 3d 20  ey;.    int t = 
1b038 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
1b039 70 65 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20  pe(argv[2]);.   
1b03a 20 73 77 69 74 63 68 28 20 74 20 29 7b 0a 20 20   switch( t ){.  
1b03b 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
1b03c 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 20 20 63  INTEGER:.      c
1b03d 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54  ase SQLITE_FLOAT
1b03e 3a 0a 20 20 20 20 20 20 20 20 7a 45 72 72 44 79  :.        zErrDy
1b03f 6e 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  n = sqlite3DbStr
1b040 44 75 70 28 64 62 2c 20 22 49 6e 76 61 6c 69 64  Dup(db, "Invalid
1b041 20 6b 65 79 20 76 61 6c 75 65 22 29 3b 0a 20 20   key value");.  
1b042 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1b043 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 20  E_ERROR;.       
1b044 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
1b045 0a 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49  .      case SQLI
1b046 54 45 5f 54 45 58 54 3a 0a 20 20 20 20 20 20 63  TE_TEXT:.      c
1b047 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a  ase SQLITE_BLOB:
1b048 0a 20 20 20 20 20 20 20 20 6e 4b 65 79 20 3d 20  .        nKey = 
1b049 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
1b04a 74 65 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20  tes(argv[2]);.  
1b04b 20 20 20 20 20 20 7a 4b 65 79 20 3d 20 28 63 68        zKey = (ch
1b04c 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
1b04d 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 32 5d 29  ue_blob(argv[2])
1b04e 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1b04f 33 43 6f 64 65 63 41 74 74 61 63 68 28 64 62 2c  3CodecAttach(db,
1b050 20 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a 4b 65 79   db->nDb-1, zKey
1b051 2c 20 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20  , nKey);.       
1b052 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 20 20 63   break;..      c
1b053 61 73 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3a  ase SQLITE_NULL:
1b054 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 20 6b  .        /* No k
1b055 65 79 20 73 70 65 63 69 66 69 65 64 2e 20 20 55  ey specified.  U
1b056 73 65 20 74 68 65 20 6b 65 79 20 66 72 6f 6d 20  se the key from 
1b057 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
1b058 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c  e */.        sql
1b059 69 74 65 33 43 6f 64 65 63 47 65 74 4b 65 79 28  ite3CodecGetKey(
1b05a 64 62 2c 20 30 2c 20 28 76 6f 69 64 2a 2a 29 26  db, 0, (void**)&
1b05b 7a 4b 65 79 2c 20 26 6e 4b 65 79 29 3b 0a 20 20  zKey, &nKey);.  
1b05c 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64        sqlite3Cod
1b05d 65 63 41 74 74 61 63 68 28 64 62 2c 20 64 62 2d  ecAttach(db, db-
1b05e 3e 6e 44 62 2d 31 2c 20 7a 4b 65 79 2c 20 6e 4b  >nDb-1, zKey, nK
1b05f 65 79 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  ey);.        bre
1b060 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  ak;.    }.  }.#e
1b061 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68  ndif..  /* If th
1b062 65 20 66 69 6c 65 20 77 61 73 20 6f 70 65 6e 65  e file was opene
1b063 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20  d successfully, 
1b064 72 65 61 64 20 74 68 65 20 73 63 68 65 6d 61 20  read the schema 
1b065 66 6f 72 20 74 68 65 20 6e 65 77 20 64 61 74 61  for the new data
1b066 62 61 73 65 2e 0a 20 20 2a 2a 20 49 66 20 74 68  base..  ** If th
1b067 69 73 20 66 61 69 6c 73 2c 20 6f 72 20 69 66 20  is fails, or if 
1b068 6f 70 65 6e 69 6e 67 20 74 68 65 20 66 69 6c 65  opening the file
1b069 20 66 61 69 6c 65 64 2c 20 74 68 65 6e 20 63 6c   failed, then cl
1b06a 6f 73 65 20 74 68 65 20 66 69 6c 65 20 61 6e 64  ose the file and
1b06b 20 0a 20 20 2a 2a 20 72 65 6d 6f 76 65 20 74 68   .  ** remove th
1b06c 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65  e entry from the
1b06d 20 64 62 2d 3e 61 44 62 5b 5d 20 61 72 72 61 79   db->aDb[] array
1b06e 2e 20 69 2e 65 2e 20 70 75 74 20 65 76 65 72 79  . i.e. put every
1b06f 74 68 69 6e 67 20 62 61 63 6b 20 74 68 65 20 77  thing back the w
1b070 61 79 0a 20 20 2a 2a 20 77 65 20 66 6f 75 6e 64  ay.  ** we found
1b071 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20   it..  */.  if( 
1b072 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1b073 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74  .    (void)sqlit
1b074 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a  e3SafetyOn(db);.
1b075 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1b076 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20  EnterAll(db);.  
1b077 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49 6e    rc = sqlite3In
1b078 69 74 28 64 62 2c 20 26 7a 45 72 72 44 79 6e 29  it(db, &zErrDyn)
1b079 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
1b07a 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a  eeLeaveAll(db);.
1b07b 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
1b07c 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
1b07d 20 20 7d 0a 20 20 69 66 28 20 72 63 20 29 7b 0a    }.  if( rc ){.
1b07e 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20 64 62      int iDb = db
1b07f 2d 3e 6e 44 62 20 2d 20 31 3b 0a 20 20 20 20 61  ->nDb - 1;.    a
1b080 73 73 65 72 74 28 20 69 44 62 3e 3d 32 20 29 3b  ssert( iDb>=2 );
1b081 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62  .    if( db->aDb
1b082 5b 69 44 62 5d 2e 70 42 74 20 29 7b 0a 20 20 20  [iDb].pBt ){.   
1b083 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43     sqlite3BtreeC
1b084 6c 6f 73 65 28 64 62 2d 3e 61 44 62 5b 69 44 62  lose(db->aDb[iDb
1b085 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20 20 64 62  ].pBt);.      db
1b086 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 20 3d  ->aDb[iDb].pBt =
1b087 20 30 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61 44   0;.      db->aD
1b088 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 20 3d  b[iDb].pSchema =
1b089 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71   0;.    }.    sq
1b08a 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e  lite3ResetIntern
1b08b 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b  alSchema(db, 0);
1b08c 0a 20 20 20 20 64 62 2d 3e 6e 44 62 20 3d 20 69  .    db->nDb = i
1b08d 44 62 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  Db;.    if( rc==
1b08e 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20  SQLITE_NOMEM || 
1b08f 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc==SQLITE_IOERR
1b090 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20  _NOMEM ){.      
1b091 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1b092 20 3d 20 31 3b 0a 20 20 20 20 20 20 73 71 6c 69   = 1;.      sqli
1b093 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
1b094 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c 20  eof(zErr),zErr, 
1b095 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29  "out of memory")
1b096 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1b097 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
1b098 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29  ntf(sizeof(zErr)
1b099 2c 7a 45 72 72 2c 20 22 75 6e 61 62 6c 65 20 74  ,zErr, "unable t
1b09a 6f 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 3a  o open database:
1b09b 20 25 73 22 2c 20 7a 46 69 6c 65 29 3b 0a 20 20   %s", zFile);.  
1b09c 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 61 74 74    }.    goto att
1b09d 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20  ach_error;.  }. 
1b09e 20 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 61 74 74   .  return;..att
1b09f 61 63 68 5f 65 72 72 6f 72 3a 0a 20 20 2f 2a 20  ach_error:.  /* 
1b0a0 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  Return an error 
1b0a1 69 66 20 77 65 20 67 65 74 20 68 65 72 65 20 2a  if we get here *
1b0a2 2f 0a 20 20 69 66 28 20 7a 45 72 72 44 79 6e 20  /.  if( zErrDyn 
1b0a3 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
1b0a4 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74  esult_error(cont
1b0a5 65 78 74 2c 20 7a 45 72 72 44 79 6e 2c 20 2d 31  ext, zErrDyn, -1
1b0a6 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
1b0a7 72 65 65 28 7a 45 72 72 44 79 6e 29 3b 0a 20 20  ree(zErrDyn);.  
1b0a8 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 45 72 72 5b  }else{.    zErr[
1b0a9 73 69 7a 65 6f 66 28 7a 45 72 72 29 2d 31 5d 20  sizeof(zErr)-1] 
1b0aa 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 0;.    sqlite3
1b0ab 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f  _result_error(co
1b0ac 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29  ntext, zErr, -1)
1b0ad 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 20 29  ;.  }.  if( rc )
1b0ae 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1b0af 65 72 72 6f 72 5f 63 6f 64 65 28 63 6f 6e 74 65  error_code(conte
1b0b0 78 74 2c 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  xt, rc);.}../*.*
1b0b1 2a 20 41 6e 20 53 51 4c 20 75 73 65 72 2d 66 75  * An SQL user-fu
1b0b2 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65  nction registere
1b0b3 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b  d to do the work
1b0b4 20 6f 66 20 61 6e 20 44 45 54 41 43 48 20 73 74   of an DETACH st
1b0b5 61 74 65 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20  atement. The.** 
1b0b6 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 20  three arguments 
1b0b7 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  to the function 
1b0b8 63 6f 6d 65 20 64 69 72 65 63 74 6c 79 20 66 72  come directly fr
1b0b9 6f 6d 20 61 20 64 65 74 61 63 68 20 73 74 61 74  om a detach stat
1b0ba 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ement:.**.**    
1b0bb 20 44 45 54 41 43 48 20 44 41 54 41 42 41 53 45   DETACH DATABASE
1b0bc 20 78 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c   x.**.**     SEL
1b0bd 45 43 54 20 73 71 6c 69 74 65 5f 64 65 74 61 63  ECT sqlite_detac
1b0be 68 28 78 29 0a 2a 2f 0a 73 74 61 74 69 63 20 76  h(x).*/.static v
1b0bf 6f 69 64 20 64 65 74 61 63 68 46 75 6e 63 28 0a  oid detachFunc(.
1b0c0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
1b0c1 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
1b0c2 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
1b0c3 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
1b0c4 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  {.  const char *
1b0c5 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63  zName = (const c
1b0c6 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  har *)sqlite3_va
1b0c7 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
1b0c8 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  );.  sqlite3 *db
1b0c9 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
1b0ca 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
1b0cb 74 65 78 74 29 3b 0a 20 20 69 6e 74 20 69 3b 0a  text);.  int i;.
1b0cc 20 20 44 62 20 2a 70 44 62 20 3d 20 30 3b 0a 20    Db *pDb = 0;. 
1b0cd 20 63 68 61 72 20 7a 45 72 72 5b 31 32 38 5d 3b   char zErr[128];
1b0ce 0a 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30  ..  if( zName==0
1b0cf 20 29 20 7a 4e 61 6d 65 20 3d 20 22 22 3b 0a 20   ) zName = "";. 
1b0d0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
1b0d1 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70  nDb; i++){.    p
1b0d2 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d  Db = &db->aDb[i]
1b0d3 3b 0a 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70  ;.    if( pDb->p
1b0d4 42 74 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  Bt==0 ) continue
1b0d5 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
1b0d6 33 53 74 72 49 43 6d 70 28 70 44 62 2d 3e 7a 4e  3StrICmp(pDb->zN
1b0d7 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29  ame, zName)==0 )
1b0d8 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 69   break;.  }..  i
1b0d9 66 28 20 69 3e 3d 64 62 2d 3e 6e 44 62 20 29 7b  f( i>=db->nDb ){
1b0da 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
1b0db 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72  rintf(sizeof(zEr
1b0dc 72 29 2c 7a 45 72 72 2c 20 22 6e 6f 20 73 75 63  r),zErr, "no suc
1b0dd 68 20 64 61 74 61 62 61 73 65 3a 20 25 73 22 2c  h database: %s",
1b0de 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74   zName);.    got
1b0df 6f 20 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a  o detach_error;.
1b0e0 20 20 7d 0a 20 20 69 66 28 20 69 3c 32 20 29 7b    }.  if( i<2 ){
1b0e1 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
1b0e2 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72  rintf(sizeof(zEr
1b0e3 72 29 2c 7a 45 72 72 2c 20 22 63 61 6e 6e 6f 74  r),zErr, "cannot
1b0e4 20 64 65 74 61 63 68 20 64 61 74 61 62 61 73 65   detach database
1b0e5 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20   %s", zName);.  
1b0e6 20 20 67 6f 74 6f 20 64 65 74 61 63 68 5f 65 72    goto detach_er
1b0e7 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21  ror;.  }.  if( !
1b0e8 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29  db->autoCommit )
1b0e9 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  {.    sqlite3_sn
1b0ea 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45  printf(sizeof(zE
1b0eb 72 72 29 2c 20 7a 45 72 72 2c 0a 20 20 20 20 20  rr), zErr,.     
1b0ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b0ed 22 63 61 6e 6e 6f 74 20 44 45 54 41 43 48 20 64  "cannot DETACH d
1b0ee 61 74 61 62 61 73 65 20 77 69 74 68 69 6e 20 74  atabase within t
1b0ef 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20  ransaction");.  
1b0f0 20 20 67 6f 74 6f 20 64 65 74 61 63 68 5f 65 72    goto detach_er
1b0f1 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  ror;.  }.  if( s
1b0f2 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52  qlite3BtreeIsInR
1b0f3 65 61 64 54 72 61 6e 73 28 70 44 62 2d 3e 70 42  eadTrans(pDb->pB
1b0f4 74 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  t) ){.    sqlite
1b0f5 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
1b0f6 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c 20 22 64  f(zErr),zErr, "d
1b0f7 61 74 61 62 61 73 65 20 25 73 20 69 73 20 6c 6f  atabase %s is lo
1b0f8 63 6b 65 64 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  cked", zName);. 
1b0f9 20 20 20 67 6f 74 6f 20 64 65 74 61 63 68 5f 65     goto detach_e
1b0fa 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c  rror;.  }..  sql
1b0fb 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 70  ite3BtreeClose(p
1b0fc 44 62 2d 3e 70 42 74 29 3b 0a 20 20 70 44 62 2d  Db->pBt);.  pDb-
1b0fd 3e 70 42 74 20 3d 20 30 3b 0a 20 20 70 44 62 2d  >pBt = 0;.  pDb-
1b0fe 3e 70 53 63 68 65 6d 61 20 3d 20 30 3b 0a 20 20  >pSchema = 0;.  
1b0ff 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65  sqlite3ResetInte
1b100 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30  rnalSchema(db, 0
1b101 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 64 65  );.  return;..de
1b102 74 61 63 68 5f 65 72 72 6f 72 3a 0a 20 20 73 71  tach_error:.  sq
1b103 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
1b104 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72  or(context, zErr
1b105 2c 20 2d 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  , -1);.}../*.** 
1b106 54 68 69 73 20 70 72 6f 63 65 64 75 72 65 20 67  This procedure g
1b107 65 6e 65 72 61 74 65 73 20 56 44 42 45 20 63 6f  enerates VDBE co
1b108 64 65 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20  de for a single 
1b109 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 65 69  invocation of ei
1b10a 74 68 65 72 20 74 68 65 0a 2a 2a 20 73 71 6c 69  ther the.** sqli
1b10b 74 65 5f 64 65 74 61 63 68 28 29 20 6f 72 20 73  te_detach() or s
1b10c 71 6c 69 74 65 5f 61 74 74 61 63 68 28 29 20 53  qlite_attach() S
1b10d 51 4c 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e  QL user function
1b10e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
1b10f 64 20 63 6f 64 65 41 74 74 61 63 68 28 0a 20 20  d codeAttach(.  
1b110 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
1b111 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
1b112 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
1b113 69 6e 74 20 74 79 70 65 2c 20 20 20 20 20 20 20  int type,       
1b114 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 53       /* Either S
1b115 51 4c 49 54 45 5f 41 54 54 41 43 48 20 6f 72 20  QLITE_ATTACH or 
1b116 53 51 4c 49 54 45 5f 44 45 54 41 43 48 20 2a 2f  SQLITE_DETACH */
1b117 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1b118 46 75 6e 63 2c 20 20 20 2f 2a 20 45 69 74 68 65  Func,   /* Eithe
1b119 72 20 22 73 71 6c 69 74 65 5f 61 74 74 61 63 68  r "sqlite_attach
1b11a 22 20 6f 72 20 22 73 71 6c 69 74 65 5f 64 65 74  " or "sqlite_det
1b11b 61 63 68 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75  ach */.  int nFu
1b11c 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  nc,           /*
1b11d 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 73 20   Number of args 
1b11e 74 6f 20 70 61 73 73 20 74 6f 20 7a 46 75 6e 63  to pass to zFunc
1b11f 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 41 75 74   */.  Expr *pAut
1b120 68 41 72 67 2c 20 20 20 20 20 20 2f 2a 20 45 78  hArg,      /* Ex
1b121 70 72 65 73 73 69 6f 6e 20 74 6f 20 70 61 73 73  pression to pass
1b122 20 74 6f 20 61 75 74 68 6f 72 69 7a 61 74 69 6f   to authorizatio
1b123 6e 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20  n callback */.  
1b124 45 78 70 72 20 2a 70 46 69 6c 65 6e 61 6d 65 2c  Expr *pFilename,
1b125 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
1b126 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
1b127 0a 20 20 45 78 70 72 20 2a 70 44 62 6e 61 6d 65  .  Expr *pDbname
1b128 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  ,       /* Name 
1b129 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
1b12a 74 6f 20 75 73 65 20 69 6e 74 65 72 6e 61 6c 6c  to use internall
1b12b 79 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4b 65  y */.  Expr *pKe
1b12c 79 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44  y           /* D
1b12d 61 74 61 62 61 73 65 20 6b 65 79 20 66 6f 72 20  atabase key for 
1b12e 65 6e 63 72 79 70 74 69 6f 6e 20 65 78 74 65 6e  encryption exten
1b12f 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  sion */.){.  int
1b130 20 72 63 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65   rc;.  NameConte
1b131 78 74 20 73 4e 61 6d 65 3b 0a 20 20 56 64 62 65  xt sName;.  Vdbe
1b132 20 2a 76 3b 0a 20 20 46 75 6e 63 44 65 66 20 2a   *v;.  FuncDef *
1b133 70 46 75 6e 63 3b 0a 20 20 73 71 6c 69 74 65 33  pFunc;.  sqlite3
1b134 2a 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64  * db = pParse->d
1b135 62 3b 0a 20 20 69 6e 74 20 72 65 67 41 72 67 73  b;.  int regArgs
1b136 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
1b137 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41  E_OMIT_AUTHORIZA
1b138 54 49 4f 4e 0a 20 20 61 73 73 65 72 74 28 20 64  TION.  assert( d
1b139 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1b13a 7c 7c 20 70 41 75 74 68 41 72 67 20 29 3b 0a 20  || pAuthArg );. 
1b13b 20 69 66 28 20 70 41 75 74 68 41 72 67 20 29 7b   if( pAuthArg ){
1b13c 0a 20 20 20 20 63 68 61 72 20 2a 7a 41 75 74 68  .    char *zAuth
1b13d 41 72 67 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  Arg = sqlite3Nam
1b13e 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 26  eFromToken(db, &
1b13f 70 41 75 74 68 41 72 67 2d 3e 73 70 61 6e 29 3b  pAuthArg->span);
1b140 0a 20 20 20 20 69 66 28 20 21 7a 41 75 74 68 41  .    if( !zAuthA
1b141 72 67 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  rg ){.      goto
1b142 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20 20   attach_end;.   
1b143 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69   }.    rc = sqli
1b144 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
1b145 72 73 65 2c 20 74 79 70 65 2c 20 7a 41 75 74 68  rse, type, zAuth
1b146 41 72 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  Arg, 0, 0);.    
1b147 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 41 75  sqlite3_free(zAu
1b148 74 68 41 72 67 29 3b 0a 20 20 20 20 69 66 28 72  thArg);.    if(r
1b149 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1b14a 20 20 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63        goto attac
1b14b 68 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 7d  h_end;.    }.  }
1b14c 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1b14d 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41  E_OMIT_AUTHORIZA
1b14e 54 49 4f 4e 20 2a 2f 0a 0a 20 20 6d 65 6d 73 65  TION */..  memse
1b14f 74 28 26 73 4e 61 6d 65 2c 20 30 2c 20 73 69 7a  t(&sName, 0, siz
1b150 65 6f 66 28 4e 61 6d 65 43 6f 6e 74 65 78 74 29  eof(NameContext)
1b151 29 3b 0a 20 20 73 4e 61 6d 65 2e 70 50 61 72 73  );.  sName.pPars
1b152 65 20 3d 20 70 50 61 72 73 65 3b 0a 0a 20 20 69  e = pParse;..  i
1b153 66 28 20 0a 20 20 20 20 20 20 53 51 4c 49 54 45  f( .      SQLITE
1b154 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 73 6f 6c  _OK!=(rc = resol
1b155 76 65 41 74 74 61 63 68 45 78 70 72 28 26 73 4e  veAttachExpr(&sN
1b156 61 6d 65 2c 20 70 46 69 6c 65 6e 61 6d 65 29 29  ame, pFilename))
1b157 20 7c 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45   ||.      SQLITE
1b158 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 73 6f 6c  _OK!=(rc = resol
1b159 76 65 41 74 74 61 63 68 45 78 70 72 28 26 73 4e  veAttachExpr(&sN
1b15a 61 6d 65 2c 20 70 44 62 6e 61 6d 65 29 29 20 7c  ame, pDbname)) |
1b15b 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f  |.      SQLITE_O
1b15c 4b 21 3d 28 72 63 20 3d 20 72 65 73 6f 6c 76 65  K!=(rc = resolve
1b15d 41 74 74 61 63 68 45 78 70 72 28 26 73 4e 61 6d  AttachExpr(&sNam
1b15e 65 2c 20 70 4b 65 79 29 29 0a 20 20 29 7b 0a 20  e, pKey)).  ){. 
1b15f 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b     pParse->nErr+
1b160 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 61 74 74 61  +;.    goto atta
1b161 63 68 5f 65 6e 64 3b 0a 20 20 7d 0a 0a 20 20 76  ch_end;.  }..  v
1b162 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
1b163 65 28 70 50 61 72 73 65 29 3b 0a 20 20 72 65 67  e(pParse);.  reg
1b164 41 72 67 73 20 3d 20 73 71 6c 69 74 65 33 47 65  Args = sqlite3Ge
1b165 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73  tTempRange(pPars
1b166 65 2c 20 34 29 3b 0a 20 20 73 71 6c 69 74 65 33  e, 4);.  sqlite3
1b167 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
1b168 20 70 46 69 6c 65 6e 61 6d 65 2c 20 72 65 67 41   pFilename, regA
1b169 72 67 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  rgs);.  sqlite3E
1b16a 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20  xprCode(pParse, 
1b16b 70 44 62 6e 61 6d 65 2c 20 72 65 67 41 72 67 73  pDbname, regArgs
1b16c 2b 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  +1);.  sqlite3Ex
1b16d 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
1b16e 4b 65 79 2c 20 72 65 67 41 72 67 73 2b 32 29 3b  Key, regArgs+2);
1b16f 0a 0a 20 20 61 73 73 65 72 74 28 20 76 20 7c 7c  ..  assert( v ||
1b170 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1b171 64 20 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a  d );.  if( v ){.
1b172 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1b173 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 75 6e 63  ddOp3(v, OP_Func
1b174 74 69 6f 6e 2c 20 30 2c 20 72 65 67 41 72 67 73  tion, 0, regArgs
1b175 2b 33 2d 6e 46 75 6e 63 2c 20 72 65 67 41 72 67  +3-nFunc, regArg
1b176 73 2b 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  s+3);.    sqlite
1b177 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
1b178 20 6e 46 75 6e 63 29 3b 0a 20 20 20 20 70 46 75   nFunc);.    pFu
1b179 6e 63 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  nc = sqlite3Find
1b17a 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 46 75  Function(db, zFu
1b17b 6e 63 2c 20 73 74 72 6c 65 6e 28 7a 46 75 6e 63  nc, strlen(zFunc
1b17c 29 2c 20 6e 46 75 6e 63 2c 20 53 51 4c 49 54 45  ), nFunc, SQLITE
1b17d 5f 55 54 46 38 2c 30 29 3b 0a 20 20 20 20 73 71  _UTF8,0);.    sq
1b17e 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1b17f 34 28 76 2c 20 2d 31 2c 20 28 63 68 61 72 20 2a  4(v, -1, (char *
1b180 29 70 46 75 6e 63 2c 20 50 34 5f 46 55 4e 43 44  )pFunc, P4_FUNCD
1b181 45 46 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 64  EF);..    /* Cod
1b182 65 20 61 6e 20 4f 50 5f 45 78 70 69 72 65 2e 20  e an OP_Expire. 
1b183 46 6f 72 20 61 6e 20 41 54 54 41 43 48 20 73 74  For an ATTACH st
1b184 61 74 65 6d 65 6e 74 2c 20 73 65 74 20 50 31 20  atement, set P1 
1b185 74 6f 20 74 72 75 65 20 28 65 78 70 69 72 65 20  to true (expire 
1b186 74 68 69 73 0a 20 20 20 20 2a 2a 20 73 74 61 74  this.    ** stat
1b187 65 6d 65 6e 74 20 6f 6e 6c 79 29 2e 20 46 6f 72  ement only). For
1b188 20 44 45 54 41 43 48 2c 20 73 65 74 20 69 74 20   DETACH, set it 
1b189 74 6f 20 66 61 6c 73 65 20 28 65 78 70 69 72 65  to false (expire
1b18a 20 61 6c 6c 20 65 78 69 73 74 69 6e 67 0a 20 20   all existing.  
1b18b 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 29    ** statements)
1b18c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  ..    */.    sql
1b18d 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
1b18e 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20 28 74 79  , OP_Expire, (ty
1b18f 70 65 3d 3d 53 51 4c 49 54 45 5f 41 54 54 41 43  pe==SQLITE_ATTAC
1b190 48 29 29 3b 0a 20 20 7d 0a 20 20 0a 61 74 74 61  H));.  }.  .atta
1b191 63 68 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65  ch_end:.  sqlite
1b192 33 45 78 70 72 44 65 6c 65 74 65 28 70 46 69 6c  3ExprDelete(pFil
1b193 65 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65  ename);.  sqlite
1b194 33 45 78 70 72 44 65 6c 65 74 65 28 70 44 62 6e  3ExprDelete(pDbn
1b195 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ame);.  sqlite3E
1b196 78 70 72 44 65 6c 65 74 65 28 70 4b 65 79 29 3b  xprDelete(pKey);
1b197 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64  .}../*.** Called
1b198 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 74   by the parser t
1b199 6f 20 63 6f 6d 70 69 6c 65 20 61 20 44 45 54 41  o compile a DETA
1b19a 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  CH statement..**
1b19b 0a 2a 2a 20 20 20 20 20 44 45 54 41 43 48 20 70  .**     DETACH p
1b19c 44 62 6e 61 6d 65 0a 2a 2f 0a 53 51 4c 49 54 45  Dbname.*/.SQLITE
1b19d 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1b19e 6c 69 74 65 33 44 65 74 61 63 68 28 50 61 72 73  lite3Detach(Pars
1b19f 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
1b1a0 2a 70 44 62 6e 61 6d 65 29 7b 0a 20 20 63 6f 64  *pDbname){.  cod
1b1a1 65 41 74 74 61 63 68 28 70 50 61 72 73 65 2c 20  eAttach(pParse, 
1b1a2 53 51 4c 49 54 45 5f 44 45 54 41 43 48 2c 20 22  SQLITE_DETACH, "
1b1a3 73 71 6c 69 74 65 5f 64 65 74 61 63 68 22 2c 20  sqlite_detach", 
1b1a4 31 2c 20 70 44 62 6e 61 6d 65 2c 20 30 2c 20 30  1, pDbname, 0, 0
1b1a5 2c 20 70 44 62 6e 61 6d 65 29 3b 0a 7d 0a 0a 2f  , pDbname);.}../
1b1a6 2a 0a 2a 2a 20 43 61 6c 6c 65 64 20 62 79 20 74  *.** Called by t
1b1a7 68 65 20 70 61 72 73 65 72 20 74 6f 20 63 6f 6d  he parser to com
1b1a8 70 69 6c 65 20 61 6e 20 41 54 54 41 43 48 20 73  pile an ATTACH s
1b1a9 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  tatement..**.** 
1b1aa 20 20 20 20 41 54 54 41 43 48 20 70 20 41 53 20      ATTACH p AS 
1b1ab 70 44 62 6e 61 6d 65 20 4b 45 59 20 70 4b 65 79  pDbname KEY pKey
1b1ac 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1b1ad 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
1b1ae 74 74 61 63 68 28 50 61 72 73 65 20 2a 70 50 61  ttach(Parse *pPa
1b1af 72 73 65 2c 20 45 78 70 72 20 2a 70 2c 20 45 78  rse, Expr *p, Ex
1b1b0 70 72 20 2a 70 44 62 6e 61 6d 65 2c 20 45 78 70  pr *pDbname, Exp
1b1b1 72 20 2a 70 4b 65 79 29 7b 0a 20 20 63 6f 64 65  r *pKey){.  code
1b1b2 41 74 74 61 63 68 28 70 50 61 72 73 65 2c 20 53  Attach(pParse, S
1b1b3 51 4c 49 54 45 5f 41 54 54 41 43 48 2c 20 22 73  QLITE_ATTACH, "s
1b1b4 71 6c 69 74 65 5f 61 74 74 61 63 68 22 2c 20 33  qlite_attach", 3
1b1b5 2c 20 70 2c 20 70 2c 20 70 44 62 6e 61 6d 65 2c  , p, p, pDbname,
1b1b6 20 70 4b 65 79 29 3b 0a 7d 0a 23 65 6e 64 69 66   pKey);.}.#endif
1b1b7 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1b1b8 41 54 54 41 43 48 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  ATTACH */../*.**
1b1b9 20 52 65 67 69 73 74 65 72 20 74 68 65 20 66 75   Register the fu
1b1ba 6e 63 74 69 6f 6e 73 20 73 71 6c 69 74 65 5f 61  nctions sqlite_a
1b1bb 74 74 61 63 68 20 61 6e 64 20 73 71 6c 69 74 65  ttach and sqlite
1b1bc 5f 64 65 74 61 63 68 2e 0a 2a 2f 0a 53 51 4c 49  _detach..*/.SQLI
1b1bd 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1b1be 73 71 6c 69 74 65 33 41 74 74 61 63 68 46 75 6e  sqlite3AttachFun
1b1bf 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a  ctions(sqlite3 *
1b1c0 64 62 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  db){.#ifndef SQL
1b1c1 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48 0a  ITE_OMIT_ATTACH.
1b1c2 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69    static const i
1b1c3 6e 74 20 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f  nt enc = SQLITE_
1b1c4 55 54 46 38 3b 0a 20 20 73 71 6c 69 74 65 33 43  UTF8;.  sqlite3C
1b1c5 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22 73  reateFunc(db, "s
1b1c6 71 6c 69 74 65 5f 61 74 74 61 63 68 22 2c 20 33  qlite_attach", 3
1b1c7 2c 20 65 6e 63 2c 20 30 2c 20 61 74 74 61 63 68  , enc, 0, attach
1b1c8 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 20 20 73  Func, 0, 0);.  s
1b1c9 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63  qlite3CreateFunc
1b1ca 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 64 65 74  (db, "sqlite_det
1b1cb 61 63 68 22 2c 20 31 2c 20 65 6e 63 2c 20 30 2c  ach", 1, enc, 0,
1b1cc 20 64 65 74 61 63 68 46 75 6e 63 2c 20 30 2c 20   detachFunc, 0, 
1b1cd 30 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a  0);.#endif.}../*
1b1ce 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61  .** Initialize a
1b1cf 20 44 62 46 69 78 65 72 20 73 74 72 75 63 74 75   DbFixer structu
1b1d0 72 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  re.  This routin
1b1d1 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64  e must be called
1b1d2 20 70 72 69 6f 72 0a 2a 2a 20 74 6f 20 70 61 73   prior.** to pas
1b1d3 73 69 6e 67 20 74 68 65 20 73 74 72 75 63 74 75  sing the structu
1b1d4 72 65 20 74 6f 20 6f 6e 65 20 6f 66 20 74 68 65  re to one of the
1b1d5 20 73 71 6c 69 74 65 46 69 78 41 41 41 41 28 29   sqliteFixAAAA()
1b1d6 20 72 6f 75 74 69 6e 65 73 20 62 65 6c 6f 77 2e   routines below.
1b1d7 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  .**.** The retur
1b1d8 6e 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65  n value indicate
1b1d9 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  s whether or not
1b1da 20 66 69 78 61 74 69 6f 6e 20 69 73 20 72 65 71   fixation is req
1b1db 75 69 72 65 64 2e 20 20 54 52 55 45 0a 2a 2a 20  uired.  TRUE.** 
1b1dc 6d 65 61 6e 73 20 77 65 20 64 6f 20 6e 65 65 64  means we do need
1b1dd 20 74 6f 20 66 69 78 20 74 68 65 20 64 61 74 61   to fix the data
1b1de 62 61 73 65 20 72 65 66 65 72 65 6e 63 65 73 2c  base references,
1b1df 20 46 41 4c 53 45 20 6d 65 61 6e 73 20 77 65 20   FALSE means we 
1b1e0 64 6f 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54  do not..*/.SQLIT
1b1e1 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1b1e2 6c 69 74 65 33 46 69 78 49 6e 69 74 28 0a 20 20  lite3FixInit(.  
1b1e3 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20  DbFixer *pFix,  
1b1e4 20 20 20 20 2f 2a 20 54 68 65 20 66 69 78 65 72      /* The fixer
1b1e5 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c 69 7a   to be initializ
1b1e6 65 64 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70  ed */.  Parse *p
1b1e7 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 45  Parse,      /* E
1b1e8 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 77 69  rror messages wi
1b1e9 6c 6c 20 62 65 20 77 72 69 74 74 65 6e 20 68 65  ll be written he
1b1ea 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c  re */.  int iDb,
1b1eb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1b1ec 68 69 73 20 69 73 20 74 68 65 20 64 61 74 61 62  his is the datab
1b1ed 61 73 65 20 74 68 61 74 20 6d 75 73 74 20 62 65  ase that must be
1b1ee 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74   used */.  const
1b1ef 20 63 68 61 72 20 2a 7a 54 79 70 65 2c 20 20 2f   char *zType,  /
1b1f0 2a 20 22 76 69 65 77 22 2c 20 22 74 72 69 67 67  * "view", "trigg
1b1f1 65 72 22 2c 20 6f 72 20 22 69 6e 64 65 78 22 20  er", or "index" 
1b1f2 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e  */.  const Token
1b1f3 20 2a 70 4e 61 6d 65 20 20 2f 2a 20 4e 61 6d 65   *pName  /* Name
1b1f4 20 6f 66 20 74 68 65 20 76 69 65 77 2c 20 74 72   of the view, tr
1b1f5 69 67 67 65 72 2c 20 6f 72 20 69 6e 64 65 78 20  igger, or index 
1b1f6 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  */.){.  sqlite3 
1b1f7 2a 64 62 3b 0a 0a 20 20 69 66 28 20 69 44 62 3c  *db;..  if( iDb<
1b1f8 30 20 7c 7c 20 69 44 62 3d 3d 31 20 29 20 72 65  0 || iDb==1 ) re
1b1f9 74 75 72 6e 20 30 3b 0a 20 20 64 62 20 3d 20 70  turn 0;.  db = p
1b1fa 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73  Parse->db;.  ass
1b1fb 65 72 74 28 20 64 62 2d 3e 6e 44 62 3e 69 44 62  ert( db->nDb>iDb
1b1fc 20 29 3b 0a 20 20 70 46 69 78 2d 3e 70 50 61 72   );.  pFix->pPar
1b1fd 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 70  se = pParse;.  p
1b1fe 46 69 78 2d 3e 7a 44 62 20 3d 20 64 62 2d 3e 61  Fix->zDb = db->a
1b1ff 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20  Db[iDb].zName;. 
1b200 20 70 46 69 78 2d 3e 7a 54 79 70 65 20 3d 20 7a   pFix->zType = z
1b201 54 79 70 65 3b 0a 20 20 70 46 69 78 2d 3e 70 4e  Type;.  pFix->pN
1b202 61 6d 65 20 3d 20 70 4e 61 6d 65 3b 0a 20 20 72  ame = pName;.  r
1b203 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 1;.}../*.*
1b204 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
1b205 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 20  set of routines 
1b206 77 61 6c 6b 20 74 68 72 6f 75 67 68 20 74 68 65  walk through the
1b207 20 70 61 72 73 65 20 74 72 65 65 20 61 6e 64 20   parse tree and 
1b208 61 73 73 69 67 6e 0a 2a 2a 20 61 20 73 70 65 63  assign.** a spec
1b209 69 66 69 63 20 64 61 74 61 62 61 73 65 20 74 6f  ific database to
1b20a 20 61 6c 6c 20 74 61 62 6c 65 20 72 65 66 65 72   all table refer
1b20b 65 6e 63 65 73 20 77 68 65 72 65 20 74 68 65 20  ences where the 
1b20c 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a  database name.**
1b20d 20 77 61 73 20 6c 65 66 74 20 75 6e 73 70 65 63   was left unspec
1b20e 69 66 69 65 64 20 69 6e 20 74 68 65 20 6f 72 69  ified in the ori
1b20f 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 74 65 6d  ginal SQL statem
1b210 65 6e 74 2e 20 20 54 68 65 20 70 46 69 78 20 73  ent.  The pFix s
1b211 74 72 75 63 74 75 72 65 0a 2a 2a 20 6d 75 73 74  tructure.** must
1b212 20 68 61 76 65 20 62 65 65 6e 20 69 6e 69 74 69   have been initi
1b213 61 6c 69 7a 65 64 20 62 79 20 61 20 70 72 69 6f  alized by a prio
1b214 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  r call to sqlite
1b215 33 46 69 78 49 6e 69 74 28 29 2e 0a 2a 2a 0a 2a  3FixInit()..**.*
1b216 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
1b217 20 61 72 65 20 75 73 65 64 20 74 6f 20 6d 61 6b   are used to mak
1b218 65 20 73 75 72 65 20 74 68 61 74 20 61 6e 20 69  e sure that an i
1b219 6e 64 65 78 2c 20 74 72 69 67 67 65 72 2c 20 6f  ndex, trigger, o
1b21a 72 0a 2a 2a 20 76 69 65 77 20 69 6e 20 6f 6e 65  r.** view in one
1b21b 20 64 61 74 61 62 61 73 65 20 64 6f 65 73 20 6e   database does n
1b21c 6f 74 20 72 65 66 65 72 20 74 6f 20 6f 62 6a 65  ot refer to obje
1b21d 63 74 73 20 69 6e 20 61 20 64 69 66 66 65 72 65  cts in a differe
1b21e 6e 74 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20  nt database..** 
1b21f 28 45 78 63 65 70 74 69 6f 6e 3a 20 69 6e 64 69  (Exception: indi
1b220 63 65 73 2c 20 74 72 69 67 67 65 72 73 2c 20 61  ces, triggers, a
1b221 6e 64 20 76 69 65 77 73 20 69 6e 20 74 68 65 20  nd views in the 
1b222 54 45 4d 50 20 64 61 74 61 62 61 73 65 20 61 72  TEMP database ar
1b223 65 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 74 6f 20  e.** allowed to 
1b224 72 65 66 65 72 20 74 6f 20 61 6e 79 74 68 69 6e  refer to anythin
1b225 67 2e 29 20 20 49 66 20 61 20 72 65 66 65 72 65  g.)  If a refere
1b226 6e 63 65 20 69 73 20 65 78 70 6c 69 63 69 74 6c  nce is explicitl
1b227 79 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 61 6e 20  y made.** to an 
1b228 6f 62 6a 65 63 74 20 69 6e 20 61 20 64 69 66 66  object in a diff
1b229 65 72 65 6e 74 20 64 61 74 61 62 61 73 65 2c 20  erent database, 
1b22a 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
1b22b 20 69 73 20 61 64 64 65 64 20 74 6f 0a 2a 2a 20   is added to.** 
1b22c 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20  pParse->zErrMsg 
1b22d 61 6e 64 20 74 68 65 73 65 20 72 6f 75 74 69 6e  and these routin
1b22e 65 73 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65  es return non-ze
1b22f 72 6f 2e 20 20 49 66 20 65 76 65 72 79 74 68 69  ro.  If everythi
1b230 6e 67 0a 2a 2a 20 63 68 65 63 6b 73 20 6f 75 74  ng.** checks out
1b231 2c 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  , these routines
1b232 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51   return 0..*/.SQ
1b233 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1b234 20 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69   sqlite3FixSrcLi
1b235 73 74 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70  st(.  DbFixer *p
1b236 46 69 78 2c 20 20 20 20 20 20 20 2f 2a 20 43 6f  Fix,       /* Co
1b237 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66 69 78  ntext of the fix
1b238 61 74 69 6f 6e 20 2a 2f 0a 20 20 53 72 63 4c 69  ation */.  SrcLi
1b239 73 74 20 2a 70 4c 69 73 74 20 20 20 20 20 20 20  st *pList       
1b23a 2f 2a 20 54 68 65 20 53 6f 75 72 63 65 20 6c 69  /* The Source li
1b23b 73 74 20 74 6f 20 63 68 65 63 6b 20 61 6e 64 20  st to check and 
1b23c 6d 6f 64 69 66 79 20 2a 2f 0a 29 7b 0a 20 20 69  modify */.){.  i
1b23d 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74 20 63 68  nt i;.  const ch
1b23e 61 72 20 2a 7a 44 62 3b 0a 20 20 73 74 72 75 63  ar *zDb;.  struc
1b23f 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
1b240 70 49 74 65 6d 3b 0a 0a 20 20 69 66 28 20 70 4c  pItem;..  if( pL
1b241 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ist==0 ) return 
1b242 30 3b 0a 20 20 7a 44 62 20 3d 20 70 46 69 78 2d  0;.  zDb = pFix-
1b243 3e 7a 44 62 3b 0a 20 20 66 6f 72 28 69 3d 30 2c  >zDb;.  for(i=0,
1b244 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b   pItem=pList->a;
1b245 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20   i<pList->nSrc; 
1b246 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i++, pItem++){. 
1b247 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 44     if( pItem->zD
1b248 61 74 61 62 61 73 65 3d 3d 30 20 29 7b 0a 20 20  atabase==0 ){.  
1b249 20 20 20 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61      pItem->zData
1b24a 62 61 73 65 20 3d 20 73 71 6c 69 74 65 33 44 62  base = sqlite3Db
1b24b 53 74 72 44 75 70 28 70 46 69 78 2d 3e 70 50 61  StrDup(pFix->pPa
1b24c 72 73 65 2d 3e 64 62 2c 20 7a 44 62 29 3b 0a 20  rse->db, zDb);. 
1b24d 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c     }else if( sql
1b24e 69 74 65 33 53 74 72 49 43 6d 70 28 70 49 74 65  ite3StrICmp(pIte
1b24f 6d 2d 3e 7a 44 61 74 61 62 61 73 65 2c 7a 44 62  m->zDatabase,zDb
1b250 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  )!=0 ){.      sq
1b251 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 46  lite3ErrorMsg(pF
1b252 69 78 2d 3e 70 50 61 72 73 65 2c 0a 20 20 20 20  ix->pParse,.    
1b253 20 20 20 20 20 22 25 73 20 25 54 20 63 61 6e 6e       "%s %T cann
1b254 6f 74 20 72 65 66 65 72 65 6e 63 65 20 6f 62 6a  ot reference obj
1b255 65 63 74 73 20 69 6e 20 64 61 74 61 62 61 73 65  ects in database
1b256 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 70   %s",.         p
1b257 46 69 78 2d 3e 7a 54 79 70 65 2c 20 70 46 69 78  Fix->zType, pFix
1b258 2d 3e 70 4e 61 6d 65 2c 20 70 49 74 65 6d 2d 3e  ->pName, pItem->
1b259 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20  zDatabase);.    
1b25a 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
1b25b 7d 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  }.#if !defined(S
1b25c 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29  QLITE_OMIT_VIEW)
1b25d 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
1b25e 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
1b25f 29 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ).    if( sqlite
1b260 33 46 69 78 53 65 6c 65 63 74 28 70 46 69 78 2c  3FixSelect(pFix,
1b261 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29   pItem->pSelect)
1b262 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20   ) return 1;.   
1b263 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45   if( sqlite3FixE
1b264 78 70 72 28 70 46 69 78 2c 20 70 49 74 65 6d 2d  xpr(pFix, pItem-
1b265 3e 70 4f 6e 29 20 29 20 72 65 74 75 72 6e 20 31  >pOn) ) return 1
1b266 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72  ;.#endif.  }.  r
1b267 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 69 66 20 21  eturn 0;.}.#if !
1b268 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
1b269 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65  MIT_VIEW) || !de
1b26a 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
1b26b 54 5f 54 52 49 47 47 45 52 29 0a 53 51 4c 49 54  T_TRIGGER).SQLIT
1b26c 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1b26d 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 0a  lite3FixSelect(.
1b26e 20 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c    DbFixer *pFix,
1b26f 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78         /* Contex
1b270 74 20 6f 66 20 74 68 65 20 66 69 78 61 74 69 6f  t of the fixatio
1b271 6e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  n */.  Select *p
1b272 53 65 6c 65 63 74 20 20 20 20 20 20 2f 2a 20 54  Select      /* T
1b273 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
1b274 65 6e 74 20 74 6f 20 62 65 20 66 69 78 65 64 20  ent to be fixed 
1b275 74 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20  to one database 
1b276 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70  */.){.  while( p
1b277 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 69 66  Select ){.    if
1b278 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72  ( sqlite3FixExpr
1b279 4c 69 73 74 28 70 46 69 78 2c 20 70 53 65 6c 65  List(pFix, pSele
1b27a 63 74 2d 3e 70 45 4c 69 73 74 29 20 29 7b 0a 20  ct->pEList) ){. 
1b27b 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
1b27c 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
1b27d 69 74 65 33 46 69 78 53 72 63 4c 69 73 74 28 70  ite3FixSrcList(p
1b27e 46 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 53  Fix, pSelect->pS
1b27f 72 63 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  rc) ){.      ret
1b280 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
1b281 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45   if( sqlite3FixE
1b282 78 70 72 28 70 46 69 78 2c 20 70 53 65 6c 65 63  xpr(pFix, pSelec
1b283 74 2d 3e 70 57 68 65 72 65 29 20 29 7b 0a 20 20  t->pWhere) ){.  
1b284 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
1b285 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
1b286 74 65 33 46 69 78 45 78 70 72 28 70 46 69 78 2c  te3FixExpr(pFix,
1b287 20 70 53 65 6c 65 63 74 2d 3e 70 48 61 76 69 6e   pSelect->pHavin
1b288 67 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  g) ){.      retu
1b289 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
1b28a 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63  pSelect = pSelec
1b28b 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20  t->pPrior;.  }. 
1b28c 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c   return 0;.}.SQL
1b28d 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1b28e 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 0a  sqlite3FixExpr(.
1b28f 20 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c    DbFixer *pFix,
1b290 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20       /* Context 
1b291 6f 66 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20  of the fixation 
1b292 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  */.  Expr *pExpr
1b293 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65          /* The e
1b294 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20  xpression to be 
1b295 66 69 78 65 64 20 74 6f 20 6f 6e 65 20 64 61 74  fixed to one dat
1b296 61 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 77 68  abase */.){.  wh
1b297 69 6c 65 28 20 70 45 78 70 72 20 29 7b 0a 20 20  ile( pExpr ){.  
1b298 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78    if( sqlite3Fix
1b299 53 65 6c 65 63 74 28 70 46 69 78 2c 20 70 45 78  Select(pFix, pEx
1b29a 70 72 2d 3e 70 53 65 6c 65 63 74 29 20 29 7b 0a  pr->pSelect) ){.
1b29b 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
1b29c 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71      }.    if( sq
1b29d 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69 73 74  lite3FixExprList
1b29e 28 70 46 69 78 2c 20 70 45 78 70 72 2d 3e 70 4c  (pFix, pExpr->pL
1b29f 69 73 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65  ist) ){.      re
1b2a0 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
1b2a1 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78    if( sqlite3Fix
1b2a2 45 78 70 72 28 70 46 69 78 2c 20 70 45 78 70 72  Expr(pFix, pExpr
1b2a3 2d 3e 70 52 69 67 68 74 29 20 29 7b 0a 20 20 20  ->pRight) ){.   
1b2a4 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
1b2a5 20 7d 0a 20 20 20 20 70 45 78 70 72 20 3d 20 70   }.    pExpr = p
1b2a6 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 7d  Expr->pLeft;.  }
1b2a7 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53  .  return 0;.}.S
1b2a8 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1b2a9 74 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72  t sqlite3FixExpr
1b2aa 4c 69 73 74 28 0a 20 20 44 62 46 69 78 65 72 20  List(.  DbFixer 
1b2ab 2a 70 46 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f  *pFix,     /* Co
1b2ac 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66 69 78  ntext of the fix
1b2ad 61 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 4c  ation */.  ExprL
1b2ae 69 73 74 20 2a 70 4c 69 73 74 20 20 20 20 2f 2a  ist *pList    /*
1b2af 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   The expression 
1b2b0 74 6f 20 62 65 20 66 69 78 65 64 20 74 6f 20 6f  to be fixed to o
1b2b1 6e 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 29  ne database */.)
1b2b2 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72  {.  int i;.  str
1b2b3 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
1b2b4 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 20  m *pItem;.  if( 
1b2b5 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72  pList==0 ) retur
1b2b6 6e 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20  n 0;.  for(i=0, 
1b2b7 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20  pItem=pList->a; 
1b2b8 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  i<pList->nExpr; 
1b2b9 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i++, pItem++){. 
1b2ba 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
1b2bb 78 45 78 70 72 28 70 46 69 78 2c 20 70 49 74 65  xExpr(pFix, pIte
1b2bc 6d 2d 3e 70 45 78 70 72 29 20 29 7b 0a 20 20 20  m->pExpr) ){.   
1b2bd 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
1b2be 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1b2bf 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  0;.}.#endif..#if
1b2c0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1b2c1 5f 54 52 49 47 47 45 52 0a 53 51 4c 49 54 45 5f  _TRIGGER.SQLITE_
1b2c2 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1b2c3 74 65 33 46 69 78 54 72 69 67 67 65 72 53 74 65  te3FixTriggerSte
1b2c4 70 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46  p(.  DbFixer *pF
1b2c5 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65  ix,     /* Conte
1b2c6 78 74 20 6f 66 20 74 68 65 20 66 69 78 61 74 69  xt of the fixati
1b2c7 6f 6e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53  on */.  TriggerS
1b2c8 74 65 70 20 2a 70 53 74 65 70 20 2f 2a 20 54 68  tep *pStep /* Th
1b2c9 65 20 74 72 69 67 67 65 72 20 73 74 65 70 20 62  e trigger step b
1b2ca 65 20 66 69 78 65 64 20 74 6f 20 6f 6e 65 20 64  e fixed to one d
1b2cb 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20  atabase */.){.  
1b2cc 77 68 69 6c 65 28 20 70 53 74 65 70 20 29 7b 0a  while( pStep ){.
1b2cd 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
1b2ce 69 78 53 65 6c 65 63 74 28 70 46 69 78 2c 20 70  ixSelect(pFix, p
1b2cf 53 74 65 70 2d 3e 70 53 65 6c 65 63 74 29 20 29  Step->pSelect) )
1b2d0 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
1b2d1 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1b2d2 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 70  sqlite3FixExpr(p
1b2d3 46 69 78 2c 20 70 53 74 65 70 2d 3e 70 57 68 65  Fix, pStep->pWhe
1b2d4 72 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  re) ){.      ret
1b2d5 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
1b2d6 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45   if( sqlite3FixE
1b2d7 78 70 72 4c 69 73 74 28 70 46 69 78 2c 20 70 53  xprList(pFix, pS
1b2d8 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 29 20  tep->pExprList) 
1b2d9 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
1b2da 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 74  1;.    }.    pSt
1b2db 65 70 20 3d 20 70 53 74 65 70 2d 3e 70 4e 65 78  ep = pStep->pNex
1b2dc 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  t;.  }.  return 
1b2dd 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a  0;.}.#endif../**
1b2de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
1b2df 20 6f 66 20 61 74 74 61 63 68 2e 63 20 2a 2a 2a   of attach.c ***
1b2e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b2e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b2e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
1b2e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
1b2e4 69 6e 20 66 69 6c 65 20 61 75 74 68 2e 63 20 2a  in file auth.c *
1b2e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b2e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b2e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
1b2e8 2a 2a 20 32 30 30 33 20 4a 61 6e 75 61 72 79 20  ** 2003 January 
1b2e9 31 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  11.**.** The aut
1b2ea 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
1b2eb 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
1b2ec 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
1b2ed 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
1b2ee 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
1b2ef 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
1b2f0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
1b2f1 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
1b2f2 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
1b2f3 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
1b2f4 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
1b2f5 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
1b2f6 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
1b2f7 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
1b2f8 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
1b2f9 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
1b2fa 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
1b2fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b2fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b2fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b2fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b2ff 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
1b300 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65  ontains code use
1b301 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  d to implement t
1b302 68 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  he sqlite3_set_a
1b303 75 74 68 6f 72 69 7a 65 72 28 29 0a 2a 2a 20 41  uthorizer().** A
1b304 50 49 2e 20 20 54 68 69 73 20 66 61 63 69 6c 69  PI.  This facili
1b305 74 79 20 69 73 20 61 6e 20 6f 70 74 69 6f 6e 61  ty is an optiona
1b306 6c 20 66 65 61 74 75 72 65 20 6f 66 20 74 68 65  l feature of the
1b307 20 6c 69 62 72 61 72 79 2e 20 20 45 6d 62 65 64   library.  Embed
1b308 64 65 64 0a 2a 2a 20 73 79 73 74 65 6d 73 20 74  ded.** systems t
1b309 68 61 74 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20  hat do not need 
1b30a 74 68 69 73 20 66 61 63 69 6c 69 74 79 20 6d 61  this facility ma
1b30b 79 20 6f 6d 69 74 20 69 74 20 62 79 20 72 65 63  y omit it by rec
1b30c 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 74 68 65 20  ompiling.** the 
1b30d 6c 69 62 72 61 72 79 20 77 69 74 68 20 2d 44 53  library with -DS
1b30e 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
1b30f 52 49 5a 41 54 49 4f 4e 3d 31 0a 2a 2a 0a 2a 2a  RIZATION=1.**.**
1b310 20 24 49 64 3a 20 61 75 74 68 2e 63 2c 76 20 31   $Id: auth.c,v 1
1b311 2e 32 39 20 32 30 30 37 2f 30 39 2f 31 38 20 31  .29 2007/09/18 1
1b312 35 3a 35 35 3a 30 37 20 64 72 68 20 45 78 70 20  5:55:07 drh Exp 
1b313 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20  $.*/../*.** All 
1b314 6f 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74  of the code in t
1b315 68 69 73 20 66 69 6c 65 20 6d 61 79 20 62 65 20  his file may be 
1b316 6f 6d 69 74 74 65 64 20 62 79 20 64 65 66 69 6e  omitted by defin
1b317 69 6e 67 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20  ing a single.** 
1b318 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65  macro..*/.#ifnde
1b319 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
1b31a 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 0a 2f 2a 0a  THORIZATION../*.
1b31b 2a 2a 20 53 65 74 20 6f 72 20 63 6c 65 61 72 20  ** Set or clear 
1b31c 74 68 65 20 61 63 63 65 73 73 20 61 75 74 68 6f  the access autho
1b31d 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  rization functio
1b31e 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 63  n..**.** The acc
1b31f 65 73 73 20 61 75 74 68 6f 72 69 7a 61 74 69 6f  ess authorizatio
1b320 6e 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65  n function is be
1b321 20 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 74   called during t
1b322 68 65 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 0a 2a  he compilation.*
1b323 2a 20 70 68 61 73 65 20 74 6f 20 76 65 72 69 66  * phase to verif
1b324 79 20 74 68 61 74 20 74 68 65 20 75 73 65 72 20  y that the user 
1b325 68 61 73 20 72 65 61 64 20 61 6e 64 2f 6f 72 20  has read and/or 
1b326 77 72 69 74 65 20 61 63 63 65 73 73 20 70 65 72  write access per
1b327 6d 69 73 73 69 6f 6e 20 6f 6e 0a 2a 2a 20 76 61  mission on.** va
1b328 72 69 6f 75 73 20 66 69 65 6c 64 73 20 6f 66 20  rious fields of 
1b329 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54  the database.  T
1b32a 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
1b32b 74 20 74 6f 20 74 68 65 20 61 75 74 68 20 66 75  t to the auth fu
1b32c 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 61 20 63  nction.** is a c
1b32d 6f 70 79 20 6f 66 20 74 68 65 20 33 72 64 20 61  opy of the 3rd a
1b32e 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20  rgument to this 
1b32f 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 73 65  routine.  The se
1b330 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  cond argument.**
1b331 20 74 6f 20 74 68 65 20 61 75 74 68 20 66 75 6e   to the auth fun
1b332 63 74 69 6f 6e 20 69 73 20 6f 6e 65 20 6f 66 20  ction is one of 
1b333 74 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 3a  these constants:
1b334 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  .**.**       SQL
1b335 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45 58  ITE_CREATE_INDEX
1b336 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
1b337 5f 43 52 45 41 54 45 5f 54 41 42 4c 45 0a 2a 2a  _CREATE_TABLE.**
1b338 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52         SQLITE_CR
1b339 45 41 54 45 5f 54 45 4d 50 5f 49 4e 44 45 58 0a  EATE_TEMP_INDEX.
1b33a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
1b33b 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 41 42 4c  CREATE_TEMP_TABL
1b33c 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  E.**       SQLIT
1b33d 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52  E_CREATE_TEMP_TR
1b33e 49 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53  IGGER.**       S
1b33f 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d  QLITE_CREATE_TEM
1b340 50 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20  P_VIEW.**       
1b341 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 52  SQLITE_CREATE_TR
1b342 49 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53  IGGER.**       S
1b343 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 49 45  QLITE_CREATE_VIE
1b344 57 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  W.**       SQLIT
1b345 45 5f 44 45 4c 45 54 45 0a 2a 2a 20 20 20 20 20  E_DELETE.**     
1b346 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 49 4e    SQLITE_DROP_IN
1b347 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  DEX.**       SQL
1b348 49 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45 0a 2a  ITE_DROP_TABLE.*
1b349 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44  *       SQLITE_D
1b34a 52 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 0a 2a  ROP_TEMP_INDEX.*
1b34b 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44  *       SQLITE_D
1b34c 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 0a 2a  ROP_TEMP_TABLE.*
1b34d 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44  *       SQLITE_D
1b34e 52 4f 50 5f 54 45 4d 50 5f 54 52 49 47 47 45 52  ROP_TEMP_TRIGGER
1b34f 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
1b350 5f 44 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 0a  _DROP_TEMP_VIEW.
1b351 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
1b352 44 52 4f 50 5f 54 52 49 47 47 45 52 0a 2a 2a 20  DROP_TRIGGER.** 
1b353 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f        SQLITE_DRO
1b354 50 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20  P_VIEW.**       
1b355 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 0a 2a 2a  SQLITE_INSERT.**
1b356 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 50 52         SQLITE_PR
1b357 41 47 4d 41 0a 2a 2a 20 20 20 20 20 20 20 53 51  AGMA.**       SQ
1b358 4c 49 54 45 5f 52 45 41 44 0a 2a 2a 20 20 20 20  LITE_READ.**    
1b359 20 20 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54     SQLITE_SELECT
1b35a 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
1b35b 5f 54 52 41 4e 53 41 43 54 49 4f 4e 0a 2a 2a 20  _TRANSACTION.** 
1b35c 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55 50 44        SQLITE_UPD
1b35d 41 54 45 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68  ATE.**.** The th
1b35e 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 61  ird and fourth a
1b35f 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20  rguments to the 
1b360 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 61 72  auth function ar
1b361 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 0a 2a 2a  e the name of.**
1b362 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 74   the table and t
1b363 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 61  he column that a
1b364 72 65 20 62 65 69 6e 67 20 61 63 63 65 73 73 65  re being accesse
1b365 64 2e 20 20 54 68 65 20 61 75 74 68 20 66 75 6e  d.  The auth fun
1b366 63 74 69 6f 6e 0a 2a 2a 20 73 68 6f 75 6c 64 20  ction.** should 
1b367 72 65 74 75 72 6e 20 65 69 74 68 65 72 20 53 51  return either SQ
1b368 4c 49 54 45 5f 4f 4b 2c 20 53 51 4c 49 54 45 5f  LITE_OK, SQLITE_
1b369 44 45 4e 59 2c 20 6f 72 20 53 51 4c 49 54 45 5f  DENY, or SQLITE_
1b36a 49 47 4e 4f 52 45 2e 20 20 49 66 0a 2a 2a 20 53  IGNORE.  If.** S
1b36b 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
1b36c 72 6e 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 74  rned, it means t
1b36d 68 61 74 20 61 63 63 65 73 73 20 69 73 20 61 6c  hat access is al
1b36e 6c 6f 77 65 64 2e 20 20 53 51 4c 49 54 45 5f 44  lowed.  SQLITE_D
1b36f 45 4e 59 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61  ENY.** means tha
1b370 74 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  t the SQL statem
1b371 65 6e 74 20 77 69 6c 6c 20 6e 65 76 65 72 2d 72  ent will never-r
1b372 75 6e 20 2d 20 74 68 65 20 73 71 6c 69 74 65 33  un - the sqlite3
1b373 5f 65 78 65 63 28 29 20 63 61 6c 6c 0a 2a 2a 20  _exec() call.** 
1b374 77 69 6c 6c 20 72 65 74 75 72 6e 20 77 69 74 68  will return with
1b375 20 61 6e 20 65 72 72 6f 72 2e 20 20 53 51 4c 49   an error.  SQLI
1b376 54 45 5f 49 47 4e 4f 52 45 20 6d 65 61 6e 73 20  TE_IGNORE means 
1b377 74 68 61 74 20 74 68 65 20 53 51 4c 20 73 74 61  that the SQL sta
1b378 74 65 6d 65 6e 74 0a 2a 2a 20 73 68 6f 75 6c 64  tement.** should
1b379 20 72 75 6e 20 62 75 74 20 61 74 74 65 6d 70 74   run but attempt
1b37a 73 20 74 6f 20 72 65 61 64 20 74 68 65 20 73 70  s to read the sp
1b37b 65 63 69 66 69 65 64 20 63 6f 6c 75 6d 6e 20 77  ecified column w
1b37c 69 6c 6c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 0a  ill return NULL.
1b37d 2a 2a 20 61 6e 64 20 61 74 74 65 6d 70 74 73 20  ** and attempts 
1b37e 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 6f 6c  to write the col
1b37f 75 6d 6e 20 77 69 6c 6c 20 62 65 20 69 67 6e 6f  umn will be igno
1b380 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69  red..**.** Setti
1b381 6e 67 20 74 68 65 20 61 75 74 68 20 66 75 6e 63  ng the auth func
1b382 74 69 6f 6e 20 74 6f 20 4e 55 4c 4c 20 64 69 73  tion to NULL dis
1b383 61 62 6c 65 73 20 74 68 69 73 20 68 6f 6f 6b 2e  ables this hook.
1b384 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a    The default.**
1b385 20 73 65 74 74 69 6e 67 20 6f 66 20 74 68 65 20   setting of the 
1b386 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 69 73  auth function is
1b387 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45   NULL..*/.SQLITE
1b388 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1b389 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28  _set_authorizer(
1b38a 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a  .  sqlite3 *db,.
1b38b 20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76    int (*xAuth)(v
1b38c 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63  oid*,int,const c
1b38d 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
1b38e 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
1b38f 73 74 20 63 68 61 72 2a 29 2c 0a 20 20 76 6f 69  st char*),.  voi
1b390 64 20 2a 70 41 72 67 0a 29 7b 0a 20 20 73 71 6c  d *pArg.){.  sql
1b391 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
1b392 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64  (db->mutex);.  d
1b393 62 2d 3e 78 41 75 74 68 20 3d 20 78 41 75 74 68  b->xAuth = xAuth
1b394 3b 0a 20 20 64 62 2d 3e 70 41 75 74 68 41 72 67  ;.  db->pAuthArg
1b395 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74   = pArg;.  sqlit
1b396 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64  e3ExpirePrepared
1b397 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a  Statements(db);.
1b398 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1b399 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
1b39a 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
1b39b 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  E_OK;.}../*.** W
1b39c 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65  rite an error me
1b39d 73 73 61 67 65 20 69 6e 74 6f 20 70 50 61 72 73  ssage into pPars
1b39e 65 2d 3e 7a 45 72 72 4d 73 67 20 74 68 61 74 20  e->zErrMsg that 
1b39f 65 78 70 6c 61 69 6e 73 20 74 68 61 74 20 74 68  explains that th
1b3a0 65 0a 2a 2a 20 75 73 65 72 2d 73 75 70 70 6c 69  e.** user-suppli
1b3a1 65 64 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  ed authorization
1b3a2 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
1b3a3 65 64 20 61 6e 20 69 6c 6c 65 67 61 6c 20 76 61  ed an illegal va
1b3a4 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  lue..*/.static v
1b3a5 6f 69 64 20 73 71 6c 69 74 65 41 75 74 68 42 61  oid sqliteAuthBa
1b3a6 64 52 65 74 75 72 6e 43 6f 64 65 28 50 61 72 73  dReturnCode(Pars
1b3a7 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 72  e *pParse, int r
1b3a8 63 29 7b 0a 20 20 73 71 6c 69 74 65 33 45 72 72  c){.  sqlite3Err
1b3a9 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69  orMsg(pParse, "i
1b3aa 6c 6c 65 67 61 6c 20 72 65 74 75 72 6e 20 76 61  llegal return va
1b3ab 6c 75 65 20 28 25 64 29 20 66 72 6f 6d 20 74 68  lue (%d) from th
1b3ac 65 20 22 0a 20 20 20 20 22 61 75 74 68 6f 72 69  e ".    "authori
1b3ad 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  zation function 
1b3ae 2d 20 73 68 6f 75 6c 64 20 62 65 20 53 51 4c 49  - should be SQLI
1b3af 54 45 5f 4f 4b 2c 20 53 51 4c 49 54 45 5f 49 47  TE_OK, SQLITE_IG
1b3b0 4e 4f 52 45 2c 20 22 0a 20 20 20 20 22 6f 72 20  NORE, ".    "or 
1b3b1 53 51 4c 49 54 45 5f 44 45 4e 59 22 2c 20 72 63  SQLITE_DENY", rc
1b3b2 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 72 63 20  );.  pParse->rc 
1b3b3 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
1b3b4 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 45 78  }../*.** The pEx
1b3b5 70 72 20 73 68 6f 75 6c 64 20 62 65 20 61 20 54  pr should be a T
1b3b6 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70 72 65 73 73  K_COLUMN express
1b3b7 69 6f 6e 2e 20 20 54 68 65 20 74 61 62 6c 65 20  ion.  The table 
1b3b8 72 65 66 65 72 72 65 64 20 74 6f 0a 2a 2a 20 69  referred to.** i
1b3b9 73 20 69 6e 20 70 54 61 62 4c 69 73 74 20 6f 72  s in pTabList or
1b3ba 20 65 6c 73 65 20 69 74 20 69 73 20 74 68 65 20   else it is the 
1b3bb 4e 45 57 20 6f 72 20 4f 4c 44 20 74 61 62 6c 65  NEW or OLD table
1b3bc 20 6f 66 20 61 20 74 72 69 67 67 65 72 2e 20 20   of a trigger.  
1b3bd 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65  .** Check to see
1b3be 20 69 66 20 69 74 20 69 73 20 4f 4b 20 74 6f 20   if it is OK to 
1b3bf 72 65 61 64 20 74 68 69 73 20 70 61 72 74 69 63  read this partic
1b3c0 75 6c 61 72 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a  ular column..**.
1b3c1 2a 2a 20 49 66 20 74 68 65 20 61 75 74 68 20 66  ** If the auth f
1b3c2 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
1b3c3 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 2c 20 63  SQLITE_IGNORE, c
1b3c4 68 61 6e 67 65 20 74 68 65 20 54 4b 5f 43 4f 4c  hange the TK_COL
1b3c5 55 4d 4e 20 0a 2a 2a 20 69 6e 73 74 72 75 63 74  UMN .** instruct
1b3c6 69 6f 6e 20 69 6e 74 6f 20 61 20 54 4b 5f 4e 55  ion into a TK_NU
1b3c7 4c 4c 2e 20 20 49 66 20 74 68 65 20 61 75 74 68  LL.  If the auth
1b3c8 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
1b3c9 73 20 53 51 4c 49 54 45 5f 44 45 4e 59 2c 0a 2a  s SQLITE_DENY,.*
1b3ca 2a 20 74 68 65 6e 20 67 65 6e 65 72 61 74 65 20  * then generate 
1b3cb 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c  an error..*/.SQL
1b3cc 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1b3cd 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64   sqlite3AuthRead
1b3ce 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
1b3cf 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  e,        /* The
1b3d0 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   parser context 
1b3d1 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  */.  Expr *pExpr
1b3d2 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ,          /* Th
1b3d3 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20  e expression to 
1b3d4 63 68 65 63 6b 20 61 75 74 68 6f 72 69 7a 61 74  check authorizat
1b3d5 69 6f 6e 20 6f 6e 20 2a 2f 0a 20 20 53 63 68 65  ion on */.  Sche
1b3d6 6d 61 20 2a 70 53 63 68 65 6d 61 2c 20 20 20 20  ma *pSchema,    
1b3d7 20 20 2f 2a 20 54 68 65 20 73 63 68 65 6d 61 20    /* The schema 
1b3d8 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  of the expressio
1b3d9 6e 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  n */.  SrcList *
1b3da 70 54 61 62 4c 69 73 74 20 20 20 20 20 2f 2a 20  pTabList     /* 
1b3db 41 6c 6c 20 74 61 62 6c 65 20 74 68 61 74 20 70  All table that p
1b3dc 45 78 70 72 20 6d 69 67 68 74 20 72 65 66 65 72  Expr might refer
1b3dd 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69   to */.){.  sqli
1b3de 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
1b3df 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  ->db;.  int rc;.
1b3e0 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
1b3e1 30 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74  0;      /* The t
1b3e2 61 62 6c 65 20 62 65 69 6e 67 20 72 65 61 64 20  able being read 
1b3e3 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
1b3e4 2a 7a 43 6f 6c 3b 20 20 20 20 20 2f 2a 20 4e 61  *zCol;     /* Na
1b3e5 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  me of the column
1b3e6 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f   of the table */
1b3e7 0a 20 20 69 6e 74 20 69 53 72 63 3b 20 20 20 20  .  int iSrc;    
1b3e8 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
1b3e9 78 20 69 6e 20 70 54 61 62 4c 69 73 74 2d 3e 61  x in pTabList->a
1b3ea 5b 5d 20 6f 66 20 74 61 62 6c 65 20 62 65 69 6e  [] of table bein
1b3eb 67 20 72 65 61 64 20 2a 2f 0a 20 20 63 6f 6e 73  g read */.  cons
1b3ec 74 20 63 68 61 72 20 2a 7a 44 42 61 73 65 3b 20  t char *zDBase; 
1b3ed 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74    /* Name of dat
1b3ee 61 62 61 73 65 20 62 65 69 6e 67 20 61 63 63 65  abase being acce
1b3ef 73 73 65 64 20 2a 2f 0a 20 20 54 72 69 67 67 65  ssed */.  Trigge
1b3f0 72 53 74 61 63 6b 20 2a 70 53 74 61 63 6b 3b 20  rStack *pStack; 
1b3f1 2f 2a 20 54 68 65 20 73 74 61 63 6b 20 6f 66 20  /* The stack of 
1b3f2 63 75 72 72 65 6e 74 20 74 72 69 67 67 65 72 73  current triggers
1b3f3 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20   */.  int iDb;  
1b3f4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1b3f5 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
1b3f6 64 61 74 61 62 61 73 65 20 74 68 65 20 65 78 70  database the exp
1b3f7 72 65 73 73 69 6f 6e 20 72 65 66 65 72 73 20 74  ression refers t
1b3f8 6f 20 2a 2f 0a 0a 20 20 69 66 28 20 64 62 2d 3e  o */..  if( db->
1b3f9 78 41 75 74 68 3d 3d 30 20 29 20 72 65 74 75 72  xAuth==0 ) retur
1b3fa 6e 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  n;.  if( pExpr->
1b3fb 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20  op!=TK_COLUMN ) 
1b3fc 72 65 74 75 72 6e 3b 0a 20 20 69 44 62 20 3d 20  return;.  iDb = 
1b3fd 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
1b3fe 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c  ndex(pParse->db,
1b3ff 20 70 53 63 68 65 6d 61 29 3b 0a 20 20 69 66 28   pSchema);.  if(
1b400 20 69 44 62 3c 30 20 29 7b 0a 20 20 20 20 2f 2a   iDb<0 ){.    /*
1b401 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 72   An attempt to r
1b402 65 61 64 20 61 20 63 6f 6c 75 6d 6e 20 6f 75 74  ead a column out
1b403 20 6f 66 20 61 20 73 75 62 71 75 65 72 79 20 6f   of a subquery o
1b404 72 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 74  r other.    ** t
1b405 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2e 20  emporary table. 
1b406 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  */.    return;. 
1b407 20 7d 0a 20 20 66 6f 72 28 69 53 72 63 3d 30 3b   }.  for(iSrc=0;
1b408 20 70 54 61 62 4c 69 73 74 20 26 26 20 69 53 72   pTabList && iSr
1b409 63 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  c<pTabList->nSrc
1b40a 3b 20 69 53 72 63 2b 2b 29 7b 0a 20 20 20 20 69  ; iSrc++){.    i
1b40b 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  f( pExpr->iTable
1b40c 3d 3d 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 53  ==pTabList->a[iS
1b40d 72 63 5d 2e 69 43 75 72 73 6f 72 20 29 20 62 72  rc].iCursor ) br
1b40e 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69  eak;.  }.  if( i
1b40f 53 72 63 3e 3d 30 20 26 26 20 70 54 61 62 4c 69  Src>=0 && pTabLi
1b410 73 74 20 26 26 20 69 53 72 63 3c 70 54 61 62 4c  st && iSrc<pTabL
1b411 69 73 74 2d 3e 6e 53 72 63 20 29 7b 0a 20 20 20  ist->nSrc ){.   
1b412 20 70 54 61 62 20 3d 20 70 54 61 62 4c 69 73 74   pTab = pTabList
1b413 2d 3e 61 5b 69 53 72 63 5d 2e 70 54 61 62 3b 0a  ->a[iSrc].pTab;.
1b414 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 53 74    }else if( (pSt
1b415 61 63 6b 20 3d 20 70 50 61 72 73 65 2d 3e 74 72  ack = pParse->tr
1b416 69 67 53 74 61 63 6b 29 21 3d 30 20 29 7b 0a 20  igStack)!=0 ){. 
1b417 20 20 20 2f 2a 20 54 68 69 73 20 6d 75 73 74 20     /* This must 
1b418 62 65 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f  be an attempt to
1b419 20 72 65 61 64 20 74 68 65 20 4e 45 57 20 6f 72   read the NEW or
1b41a 20 4f 4c 44 20 70 73 65 75 64 6f 2d 74 61 62 6c   OLD pseudo-tabl
1b41b 65 73 0a 20 20 20 20 2a 2a 20 6f 66 20 61 20 74  es.    ** of a t
1b41c 72 69 67 67 65 72 2e 0a 20 20 20 20 2a 2f 0a 20  rigger..    */. 
1b41d 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
1b41e 2d 3e 69 54 61 62 6c 65 3d 3d 70 53 74 61 63 6b  ->iTable==pStack
1b41f 2d 3e 6e 65 77 49 64 78 20 7c 7c 20 70 45 78 70  ->newIdx || pExp
1b420 72 2d 3e 69 54 61 62 6c 65 3d 3d 70 53 74 61 63  r->iTable==pStac
1b421 6b 2d 3e 6f 6c 64 49 64 78 20 29 3b 0a 20 20 20  k->oldIdx );.   
1b422 20 70 54 61 62 20 3d 20 70 53 74 61 63 6b 2d 3e   pTab = pStack->
1b423 70 54 61 62 3b 0a 20 20 7d 0a 20 20 69 66 28 20  pTab;.  }.  if( 
1b424 70 54 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e  pTab==0 ) return
1b425 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69  ;.  if( pExpr->i
1b426 43 6f 6c 75 6d 6e 3e 3d 30 20 29 7b 0a 20 20 20  Column>=0 ){.   
1b427 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
1b428 69 43 6f 6c 75 6d 6e 3c 70 54 61 62 2d 3e 6e 43  iColumn<pTab->nC
1b429 6f 6c 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 20 3d  ol );.    zCol =
1b42a 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 45 78 70   pTab->aCol[pExp
1b42b 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e 7a 4e 61 6d  r->iColumn].zNam
1b42c 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  e;.  }else if( p
1b42d 54 61 62 2d 3e 69 50 4b 65 79 3e 3d 30 20 29 7b  Tab->iPKey>=0 ){
1b42e 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61  .    assert( pTa
1b42f 62 2d 3e 69 50 4b 65 79 3c 70 54 61 62 2d 3e 6e  b->iPKey<pTab->n
1b430 43 6f 6c 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 20  Col );.    zCol 
1b431 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 54 61  = pTab->aCol[pTa
1b432 62 2d 3e 69 50 4b 65 79 5d 2e 7a 4e 61 6d 65 3b  b->iPKey].zName;
1b433 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 43  .  }else{.    zC
1b434 6f 6c 20 3d 20 22 52 4f 57 49 44 22 3b 0a 20 20  ol = "ROWID";.  
1b435 7d 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e  }.  assert( iDb>
1b436 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44  =0 && iDb<db->nD
1b437 62 20 29 3b 0a 20 20 7a 44 42 61 73 65 20 3d 20  b );.  zDBase = 
1b438 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
1b439 6d 65 3b 0a 20 20 72 63 20 3d 20 64 62 2d 3e 78  me;.  rc = db->x
1b43a 41 75 74 68 28 64 62 2d 3e 70 41 75 74 68 41 72  Auth(db->pAuthAr
1b43b 67 2c 20 53 51 4c 49 54 45 5f 52 45 41 44 2c 20  g, SQLITE_READ, 
1b43c 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f  pTab->zName, zCo
1b43d 6c 2c 20 7a 44 42 61 73 65 2c 20 0a 20 20 20 20  l, zDBase, .    
1b43e 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61               pPa
1b43f 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78  rse->zAuthContex
1b440 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  t);.  if( rc==SQ
1b441 4c 49 54 45 5f 49 47 4e 4f 52 45 20 29 7b 0a 20  LITE_IGNORE ){. 
1b442 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54     pExpr->op = T
1b443 4b 5f 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 20  K_NULL;.  }else 
1b444 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  if( rc==SQLITE_D
1b445 45 4e 59 20 29 7b 0a 20 20 20 20 69 66 28 20 64  ENY ){.    if( d
1b446 62 2d 3e 6e 44 62 3e 32 20 7c 7c 20 69 44 62 21  b->nDb>2 || iDb!
1b447 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
1b448 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1b449 73 65 2c 20 22 61 63 63 65 73 73 20 74 6f 20 25  se, "access to %
1b44a 73 2e 25 73 2e 25 73 20 69 73 20 70 72 6f 68 69  s.%s.%s is prohi
1b44b 62 69 74 65 64 22 2c 20 0a 20 20 20 20 20 20 20  bited", .       
1b44c 20 20 7a 44 42 61 73 65 2c 20 70 54 61 62 2d 3e    zDBase, pTab->
1b44d 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3b 0a 20 20  zName, zCol);.  
1b44e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
1b44f 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1b450 50 61 72 73 65 2c 20 22 61 63 63 65 73 73 20 74  Parse, "access t
1b451 6f 20 25 73 2e 25 73 20 69 73 20 70 72 6f 68 69  o %s.%s is prohi
1b452 62 69 74 65 64 22 2c 70 54 61 62 2d 3e 7a 4e 61  bited",pTab->zNa
1b453 6d 65 2c 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a  me,zCol);.    }.
1b454 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d      pParse->rc =
1b455 20 53 51 4c 49 54 45 5f 41 55 54 48 3b 0a 20 20   SQLITE_AUTH;.  
1b456 7d 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51  }else if( rc!=SQ
1b457 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73  LITE_OK ){.    s
1b458 71 6c 69 74 65 41 75 74 68 42 61 64 52 65 74 75  qliteAuthBadRetu
1b459 72 6e 43 6f 64 65 28 70 50 61 72 73 65 2c 20 72  rnCode(pParse, r
1b45a 63 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  c);.  }.}../*.**
1b45b 20 44 6f 20 61 6e 20 61 75 74 68 6f 72 69 7a 61   Do an authoriza
1b45c 74 69 6f 6e 20 63 68 65 63 6b 20 75 73 69 6e 67  tion check using
1b45d 20 74 68 65 20 63 6f 64 65 20 61 6e 64 20 61 72   the code and ar
1b45e 67 75 6d 65 6e 74 73 20 67 69 76 65 6e 2e 20 20  guments given.  
1b45f 52 65 74 75 72 6e 0a 2a 2a 20 65 69 74 68 65 72  Return.** either
1b460 20 53 51 4c 49 54 45 5f 4f 4b 20 28 7a 65 72 6f   SQLITE_OK (zero
1b461 29 20 6f 72 20 53 51 4c 49 54 45 5f 49 47 4e 4f  ) or SQLITE_IGNO
1b462 52 45 20 6f 72 20 53 51 4c 49 54 45 5f 44 45 4e  RE or SQLITE_DEN
1b463 59 2e 20 20 49 66 20 53 51 4c 49 54 45 5f 44 45  Y.  If SQLITE_DE
1b464 4e 59 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65  NY.** is returne
1b465 64 2c 20 74 68 65 6e 20 74 68 65 20 65 72 72 6f  d, then the erro
1b466 72 20 63 6f 75 6e 74 20 61 6e 64 20 65 72 72 6f  r count and erro
1b467 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61  r message in pPa
1b468 72 73 65 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66  rse are.** modif
1b469 69 65 64 20 61 70 70 72 6f 70 72 69 61 74 65 6c  ied appropriatel
1b46a 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
1b46b 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1b46c 41 75 74 68 43 68 65 63 6b 28 0a 20 20 50 61 72  AuthCheck(.  Par
1b46d 73 65 20 2a 70 50 61 72 73 65 2c 0a 20 20 69 6e  se *pParse,.  in
1b46e 74 20 63 6f 64 65 2c 0a 20 20 63 6f 6e 73 74 20  t code,.  const 
1b46f 63 68 61 72 20 2a 7a 41 72 67 31 2c 0a 20 20 63  char *zArg1,.  c
1b470 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67 32  onst char *zArg2
1b471 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
1b472 7a 41 72 67 33 0a 29 7b 0a 20 20 73 71 6c 69 74  zArg3.){.  sqlit
1b473 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
1b474 3e 64 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  >db;.  int rc;..
1b475 20 20 2f 2a 20 44 6f 6e 27 74 20 64 6f 20 61 6e    /* Don't do an
1b476 79 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  y authorization 
1b477 63 68 65 63 6b 73 20 69 66 20 74 68 65 20 64 61  checks if the da
1b478 74 61 62 61 73 65 20 69 73 20 69 6e 69 74 69 61  tabase is initia
1b479 6c 69 73 69 6e 67 0a 20 20 2a 2a 20 6f 72 20 69  lising.  ** or i
1b47a 66 20 74 68 65 20 70 61 72 73 65 72 20 69 73 20  f the parser is 
1b47b 62 65 69 6e 67 20 69 6e 76 6f 6b 65 64 20 66 72  being invoked fr
1b47c 6f 6d 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65  om within sqlite
1b47d 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 2e 0a  3_declare_vtab..
1b47e 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69    */.  if( db->i
1b47f 6e 69 74 2e 62 75 73 79 20 7c 7c 20 49 4e 5f 44  nit.busy || IN_D
1b480 45 43 4c 41 52 45 5f 56 54 41 42 20 29 7b 0a 20  ECLARE_VTAB ){. 
1b481 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1b482 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  _OK;.  }..  if( 
1b483 64 62 2d 3e 78 41 75 74 68 3d 3d 30 20 29 7b 0a  db->xAuth==0 ){.
1b484 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1b485 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 63 20 3d  E_OK;.  }.  rc =
1b486 20 64 62 2d 3e 78 41 75 74 68 28 64 62 2d 3e 70   db->xAuth(db->p
1b487 41 75 74 68 41 72 67 2c 20 63 6f 64 65 2c 20 7a  AuthArg, code, z
1b488 41 72 67 31 2c 20 7a 41 72 67 32 2c 20 7a 41 72  Arg1, zArg2, zAr
1b489 67 33 2c 20 70 50 61 72 73 65 2d 3e 7a 41 75 74  g3, pParse->zAut
1b48a 68 43 6f 6e 74 65 78 74 29 3b 0a 20 20 69 66 28  hContext);.  if(
1b48b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59   rc==SQLITE_DENY
1b48c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
1b48d 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1b48e 22 6e 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 22  "not authorized"
1b48f 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72  );.    pParse->r
1b490 63 20 3d 20 53 51 4c 49 54 45 5f 41 55 54 48 3b  c = SQLITE_AUTH;
1b491 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 21  .  }else if( rc!
1b492 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63  =SQLITE_OK && rc
1b493 21 3d 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20  !=SQLITE_IGNORE 
1b494 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
1b495 54 45 5f 44 45 4e 59 3b 0a 20 20 20 20 73 71 6c  TE_DENY;.    sql
1b496 69 74 65 41 75 74 68 42 61 64 52 65 74 75 72 6e  iteAuthBadReturn
1b497 43 6f 64 65 28 70 50 61 72 73 65 2c 20 72 63 29  Code(pParse, rc)
1b498 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1b499 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 75 73 68  c;.}../*.** Push
1b49a 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f   an authorizatio
1b49b 6e 20 63 6f 6e 74 65 78 74 2e 20 20 41 66 74 65  n context.  Afte
1b49c 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  r this routine i
1b49d 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 0a 2a 2a  s called, the.**
1b49e 20 7a 41 72 67 33 20 61 72 67 75 6d 65 6e 74 20   zArg3 argument 
1b49f 74 6f 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  to authorization
1b4a0 20 63 61 6c 6c 62 61 63 6b 73 20 77 69 6c 6c 20   callbacks will 
1b4a1 62 65 20 7a 43 6f 6e 74 65 78 74 20 75 6e 74 69  be zContext unti
1b4a2 6c 0a 2a 2a 20 70 6f 70 70 65 64 2e 20 20 4f 72  l.** popped.  Or
1b4a3 20 69 66 20 70 50 61 72 73 65 3d 3d 30 2c 20 74   if pParse==0, t
1b4a4 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
1b4a5 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54   no-op..*/.SQLIT
1b4a6 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1b4a7 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78  qlite3AuthContex
1b4a8 74 50 75 73 68 28 0a 20 20 50 61 72 73 65 20 2a  tPush(.  Parse *
1b4a9 70 50 61 72 73 65 2c 0a 20 20 41 75 74 68 43 6f  pParse,.  AuthCo
1b4aa 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c  ntext *pContext,
1b4ab 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   .  const char *
1b4ac 7a 43 6f 6e 74 65 78 74 0a 29 7b 0a 20 20 70 43  zContext.){.  pC
1b4ad 6f 6e 74 65 78 74 2d 3e 70 50 61 72 73 65 20 3d  ontext->pParse =
1b4ae 20 70 50 61 72 73 65 3b 0a 20 20 69 66 28 20 70   pParse;.  if( p
1b4af 50 61 72 73 65 20 29 7b 0a 20 20 20 20 70 43 6f  Parse ){.    pCo
1b4b0 6e 74 65 78 74 2d 3e 7a 41 75 74 68 43 6f 6e 74  ntext->zAuthCont
1b4b1 65 78 74 20 3d 20 70 50 61 72 73 65 2d 3e 7a 41  ext = pParse->zA
1b4b2 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20  uthContext;.    
1b4b3 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e  pParse->zAuthCon
1b4b4 74 65 78 74 20 3d 20 7a 43 6f 6e 74 65 78 74 3b  text = zContext;
1b4b5 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f  .  }.}../*.** Po
1b4b6 70 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69  p an authorizati
1b4b7 6f 6e 20 63 6f 6e 74 65 78 74 20 74 68 61 74 20  on context that 
1b4b8 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 70  was previously p
1b4b9 75 73 68 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69  ushed.** by sqli
1b4ba 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75  te3AuthContextPu
1b4bb 73 68 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  sh.*/.SQLITE_PRI
1b4bc 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1b4bd 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 28  3AuthContextPop(
1b4be 41 75 74 68 43 6f 6e 74 65 78 74 20 2a 70 43 6f  AuthContext *pCo
1b4bf 6e 74 65 78 74 29 7b 0a 20 20 69 66 28 20 70 43  ntext){.  if( pC
1b4c0 6f 6e 74 65 78 74 2d 3e 70 50 61 72 73 65 20 29  ontext->pParse )
1b4c1 7b 0a 20 20 20 20 70 43 6f 6e 74 65 78 74 2d 3e  {.    pContext->
1b4c2 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e  pParse->zAuthCon
1b4c3 74 65 78 74 20 3d 20 70 43 6f 6e 74 65 78 74 2d  text = pContext-
1b4c4 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20  >zAuthContext;. 
1b4c5 20 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61     pContext->pPa
1b4c6 72 73 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a  rse = 0;.  }.}..
1b4c7 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1b4c8 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
1b4c9 49 4f 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  ION */../*******
1b4ca 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61  ******* End of a
1b4cb 75 74 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  uth.c **********
1b4cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b4cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b4ce 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
1b4cf 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
1b4d0 6c 65 20 62 75 69 6c 64 2e 63 20 2a 2a 2a 2a 2a  le build.c *****
1b4d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b4d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b4d3 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
1b4d4 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
1b4d5 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
1b4d6 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
1b4d7 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
1b4d8 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
1b4d9 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
1b4da 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
1b4db 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
1b4dc 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
1b4dd 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
1b4de 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
1b4df 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
1b4e0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
1b4e1 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
1b4e2 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
1b4e3 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
1b4e4 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
1b4e5 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
1b4e6 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
1b4e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b4e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b4e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b4ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
1b4eb 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
1b4ec 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74  ains C code rout
1b4ed 69 6e 65 73 20 74 68 61 74 20 61 72 65 20 63 61  ines that are ca
1b4ee 6c 6c 65 64 20 62 79 20 74 68 65 20 53 51 4c 69  lled by the SQLi
1b4ef 74 65 20 70 61 72 73 65 72 0a 2a 2a 20 77 68 65  te parser.** whe
1b4f0 6e 20 73 79 6e 74 61 78 20 72 75 6c 65 73 20 61  n syntax rules a
1b4f1 72 65 20 72 65 64 75 63 65 64 2e 20 20 54 68 65  re reduced.  The
1b4f2 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69   routines in thi
1b4f3 73 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 74 68  s file handle th
1b4f4 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 6b  e.** following k
1b4f5 69 6e 64 73 20 6f 66 20 53 51 4c 20 73 79 6e 74  inds of SQL synt
1b4f6 61 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52  ax:.**.**     CR
1b4f7 45 41 54 45 20 54 41 42 4c 45 0a 2a 2a 20 20 20  EATE TABLE.**   
1b4f8 20 20 44 52 4f 50 20 54 41 42 4c 45 0a 2a 2a 20    DROP TABLE.** 
1b4f9 20 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 58      CREATE INDEX
1b4fa 0a 2a 2a 20 20 20 20 20 44 52 4f 50 20 49 4e 44  .**     DROP IND
1b4fb 45 58 0a 2a 2a 20 20 20 20 20 63 72 65 61 74 69  EX.**     creati
1b4fc 6e 67 20 49 44 20 6c 69 73 74 73 0a 2a 2a 20 20  ng ID lists.**  
1b4fd 20 20 20 42 45 47 49 4e 20 54 52 41 4e 53 41 43     BEGIN TRANSAC
1b4fe 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 43 4f 4d 4d  TION.**     COMM
1b4ff 49 54 0a 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 41  IT.**     ROLLBA
1b500 43 4b 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62 75  CK.**.** $Id: bu
1b501 69 6c 64 2e 63 2c 76 20 31 2e 34 38 34 20 32 30  ild.c,v 1.484 20
1b502 30 38 2f 30 35 2f 30 31 20 31 37 3a 31 36 3a 35  08/05/01 17:16:5
1b503 33 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  3 drh Exp $.*/..
1b504 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
1b505 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65  ne is called whe
1b506 6e 20 61 20 6e 65 77 20 53 51 4c 20 73 74 61 74  n a new SQL stat
1b507 65 6d 65 6e 74 20 69 73 20 62 65 67 69 6e 6e 69  ement is beginni
1b508 6e 67 20 74 6f 0a 2a 2a 20 62 65 20 70 61 72 73  ng to.** be pars
1b509 65 64 2e 20 20 49 6e 69 74 69 61 6c 69 7a 65 20  ed.  Initialize 
1b50a 74 68 65 20 70 50 61 72 73 65 20 73 74 72 75 63  the pParse struc
1b50b 74 75 72 65 20 61 73 20 6e 65 65 64 65 64 2e 0a  ture as needed..
1b50c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1b50d 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65  E void sqlite3Be
1b50e 67 69 6e 50 61 72 73 65 28 50 61 72 73 65 20 2a  ginParse(Parse *
1b50f 70 50 61 72 73 65 2c 20 69 6e 74 20 65 78 70 6c  pParse, int expl
1b510 61 69 6e 46 6c 61 67 29 7b 0a 20 20 70 50 61 72  ainFlag){.  pPar
1b511 73 65 2d 3e 65 78 70 6c 61 69 6e 20 3d 20 65 78  se->explain = ex
1b512 70 6c 61 69 6e 46 6c 61 67 3b 0a 20 20 70 50 61  plainFlag;.  pPa
1b513 72 73 65 2d 3e 6e 56 61 72 20 3d 20 30 3b 0a 7d  rse->nVar = 0;.}
1b514 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1b515 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
1b516 48 45 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 61 62  HE./*.** The Tab
1b517 6c 65 4c 6f 63 6b 20 73 74 72 75 63 74 75 72 65  leLock structure
1b518 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79   is only used by
1b519 20 74 68 65 20 73 71 6c 69 74 65 33 54 61 62 6c   the sqlite3Tabl
1b51a 65 4c 6f 63 6b 28 29 20 61 6e 64 0a 2a 2a 20 63  eLock() and.** c
1b51b 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 29 20  odeTableLocks() 
1b51c 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74  functions..*/.st
1b51d 72 75 63 74 20 54 61 62 6c 65 4c 6f 63 6b 20 7b  ruct TableLock {
1b51e 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20  .  int iDb;     
1b51f 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
1b520 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69  atabase containi
1b521 6e 67 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  ng the table to 
1b522 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 69  be locked */.  i
1b523 6e 74 20 69 54 61 62 3b 20 20 20 20 20 20 20 20  nt iTab;        
1b524 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20      /* The root 
1b525 70 61 67 65 20 6f 66 20 74 68 65 20 74 61 62 6c  page of the tabl
1b526 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a  e to be locked *
1b527 2f 0a 20 20 75 38 20 69 73 57 72 69 74 65 4c 6f  /.  u8 isWriteLo
1b528 63 6b 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ck;      /* True
1b529 20 66 6f 72 20 77 72 69 74 65 20 6c 6f 63 6b 2e   for write lock.
1b52a 20 20 46 61 6c 73 65 20 66 6f 72 20 61 20 72 65    False for a re
1b52b 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 63 6f 6e  ad lock */.  con
1b52c 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20  st char *zName; 
1b52d 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
1b52e 20 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   table */.};../*
1b52f 0a 2a 2a 20 52 65 63 6f 72 64 20 74 68 65 20 66  .** Record the f
1b530 61 63 74 20 74 68 61 74 20 77 65 20 77 61 6e 74  act that we want
1b531 20 74 6f 20 6c 6f 63 6b 20 61 20 74 61 62 6c 65   to lock a table
1b532 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 0a   at run-time.  .
1b533 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20  **.** The table 
1b534 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 68 61 73  to be locked has
1b535 20 72 6f 6f 74 20 70 61 67 65 20 69 54 61 62 20   root page iTab 
1b536 61 6e 64 20 69 73 20 66 6f 75 6e 64 20 69 6e 20  and is found in 
1b537 64 61 74 61 62 61 73 65 20 69 44 62 2e 0a 2a 2a  database iDb..**
1b538 20 41 20 72 65 61 64 20 6f 72 20 61 20 77 72 69   A read or a wri
1b539 74 65 20 6c 6f 63 6b 20 63 61 6e 20 62 65 20 74  te lock can be t
1b53a 61 6b 65 6e 20 64 65 70 65 6e 64 69 6e 67 20 6f  aken depending o
1b53b 6e 20 69 73 57 72 69 74 65 6c 6f 63 6b 2e 0a 2a  n isWritelock..*
1b53c 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1b53d 65 20 6a 75 73 74 20 72 65 63 6f 72 64 73 20 74  e just records t
1b53e 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65  he fact that the
1b53f 20 6c 6f 63 6b 20 69 73 20 64 65 73 69 72 65 64   lock is desired
1b540 2e 20 20 54 68 65 0a 2a 2a 20 63 6f 64 65 20 74  .  The.** code t
1b541 6f 20 6d 61 6b 65 20 74 68 65 20 6c 6f 63 6b 20  o make the lock 
1b542 6f 63 63 75 72 20 69 73 20 67 65 6e 65 72 61 74  occur is generat
1b543 65 64 20 62 79 20 61 20 6c 61 74 65 72 20 63 61  ed by a later ca
1b544 6c 6c 20 74 6f 0a 2a 2a 20 63 6f 64 65 54 61 62  ll to.** codeTab
1b545 6c 65 4c 6f 63 6b 73 28 29 20 77 68 69 63 68 20  leLocks() which 
1b546 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 73 71  occurs during sq
1b547 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e  lite3FinishCodin
1b548 67 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  g()..*/.SQLITE_P
1b549 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1b54a 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 0a 20 20  te3TableLock(.  
1b54b 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
1b54c 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
1b54d 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69  ntext */.  int i
1b54e 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  Db,           /*
1b54f 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61   Index of the da
1b550 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
1b551 67 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 6c  g the table to l
1b552 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61  ock */.  int iTa
1b553 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52  b,          /* R
1b554 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20  oot page number 
1b555 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  of the table to 
1b556 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75  be locked */.  u
1b557 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 2c 20 20  8 isWriteLock,  
1b558 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20    /* True for a 
1b559 77 72 69 74 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20  write lock */.  
1b55a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
1b55b 65 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68  e  /* Name of th
1b55c 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f  e table to be lo
1b55d 63 6b 65 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  cked */.){.  int
1b55e 20 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 73   i;.  int nBytes
1b55f 3b 0a 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 70  ;.  TableLock *p
1b560 3b 0a 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29  ;..  if( iDb<0 )
1b561 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
1b562 7d 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  }..  for(i=0; i<
1b563 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f  pParse->nTableLo
1b564 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20  ck; i++){.    p 
1b565 3d 20 26 70 50 61 72 73 65 2d 3e 61 54 61 62 6c  = &pParse->aTabl
1b566 65 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 20 20 69 66  eLock[i];.    if
1b567 28 20 70 2d 3e 69 44 62 3d 3d 69 44 62 20 26 26  ( p->iDb==iDb &&
1b568 20 70 2d 3e 69 54 61 62 3d 3d 69 54 61 62 20 29   p->iTab==iTab )
1b569 7b 0a 20 20 20 20 20 20 70 2d 3e 69 73 57 72 69  {.      p->isWri
1b56a 74 65 4c 6f 63 6b 20 3d 20 28 70 2d 3e 69 73 57  teLock = (p->isW
1b56b 72 69 74 65 4c 6f 63 6b 20 7c 7c 20 69 73 57 72  riteLock || isWr
1b56c 69 74 65 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20  iteLock);.      
1b56d 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
1b56e 7d 0a 0a 20 20 6e 42 79 74 65 73 20 3d 20 73 69  }..  nBytes = si
1b56f 7a 65 6f 66 28 54 61 62 6c 65 4c 6f 63 6b 29 20  zeof(TableLock) 
1b570 2a 20 28 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c  * (pParse->nTabl
1b571 65 4c 6f 63 6b 2b 31 29 3b 0a 20 20 70 50 61 72  eLock+1);.  pPar
1b572 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 20 3d  se->aTableLock =
1b573 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44   .      sqlite3D
1b574 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70  bReallocOrFree(p
1b575 50 61 72 73 65 2d 3e 64 62 2c 20 70 50 61 72 73  Parse->db, pPars
1b576 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 2c 20 6e  e->aTableLock, n
1b577 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20 70 50  Bytes);.  if( pP
1b578 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b  arse->aTableLock
1b579 20 29 7b 0a 20 20 20 20 70 20 3d 20 26 70 50 61   ){.    p = &pPa
1b57a 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b  rse->aTableLock[
1b57b 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f  pParse->nTableLo
1b57c 63 6b 2b 2b 5d 3b 0a 20 20 20 20 70 2d 3e 69 44  ck++];.    p->iD
1b57d 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 70 2d 3e  b = iDb;.    p->
1b57e 69 54 61 62 20 3d 20 69 54 61 62 3b 0a 20 20 20  iTab = iTab;.   
1b57f 20 70 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20   p->isWriteLock 
1b580 3d 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b 0a 20  = isWriteLock;. 
1b581 20 20 20 70 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e     p->zName = zN
1b582 61 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ame;.  }else{.  
1b583 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65    pParse->nTable
1b584 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 70 50  Lock = 0;.    pP
1b585 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  arse->db->malloc
1b586 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a  Failed = 1;.  }.
1b587 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e  }../*.** Code an
1b588 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 69 6e   OP_TableLock in
1b589 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 65 61  struction for ea
1b58a 63 68 20 74 61 62 6c 65 20 6c 6f 63 6b 65 64 20  ch table locked 
1b58b 62 79 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d  by the.** statem
1b58c 65 6e 74 20 28 63 6f 6e 66 69 67 75 72 65 64 20  ent (configured 
1b58d 62 79 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69  by calls to sqli
1b58e 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2e  te3TableLock()).
1b58f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1b590 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 50  codeTableLocks(P
1b591 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20  arse *pParse){. 
1b592 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 20 2a   int i;.  Vdbe *
1b593 70 56 64 62 65 3b 20 0a 0a 20 20 69 66 28 20 30  pVdbe; ..  if( 0
1b594 3d 3d 28 70 56 64 62 65 20 3d 20 73 71 6c 69 74  ==(pVdbe = sqlit
1b595 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
1b596 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  )) ){.    return
1b597 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69 3d 30  ;.  }..  for(i=0
1b598 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 54 61 62  ; i<pParse->nTab
1b599 6c 65 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20  leLock; i++){.  
1b59a 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 70 20 3d    TableLock *p =
1b59b 20 26 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65   &pParse->aTable
1b59c 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 20 20 69 6e 74  Lock[i];.    int
1b59d 20 70 31 20 3d 20 70 2d 3e 69 44 62 3b 0a 20 20   p1 = p->iDb;.  
1b59e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1b59f 4f 70 34 28 70 56 64 62 65 2c 20 4f 50 5f 54 61  Op4(pVdbe, OP_Ta
1b5a0 62 6c 65 4c 6f 63 6b 2c 20 70 31 2c 20 70 2d 3e  bleLock, p1, p->
1b5a1 69 54 61 62 2c 20 70 2d 3e 69 73 57 72 69 74 65  iTab, p->isWrite
1b5a2 4c 6f 63 6b 2c 0a 20 20 20 20 20 20 20 20 20 20  Lock,.          
1b5a3 20 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 7a              p->z
1b5a4 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29  Name, P4_STATIC)
1b5a5 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 20 20  ;.  }.}.#else.  
1b5a6 23 64 65 66 69 6e 65 20 63 6f 64 65 54 61 62 6c  #define codeTabl
1b5a7 65 4c 6f 63 6b 73 28 78 29 0a 23 65 6e 64 69 66  eLocks(x).#endif
1b5a8 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
1b5a9 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 61  tine is called a
1b5aa 66 74 65 72 20 61 20 73 69 6e 67 6c 65 20 53 51  fter a single SQ
1b5ab 4c 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20  L statement has 
1b5ac 62 65 65 6e 0a 2a 2a 20 70 61 72 73 65 64 20 61  been.** parsed a
1b5ad 6e 64 20 61 20 56 44 42 45 20 70 72 6f 67 72 61  nd a VDBE progra
1b5ae 6d 20 74 6f 20 65 78 65 63 75 74 65 20 74 68 61  m to execute tha
1b5af 74 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20  t statement has 
1b5b0 62 65 65 6e 0a 2a 2a 20 70 72 65 70 61 72 65 64  been.** prepared
1b5b1 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
1b5b2 70 75 74 73 20 74 68 65 20 66 69 6e 69 73 68 69  puts the finishi
1b5b3 6e 67 20 74 6f 75 63 68 65 73 20 6f 6e 20 74 68  ng touches on th
1b5b4 65 0a 2a 2a 20 56 44 42 45 20 70 72 6f 67 72 61  e.** VDBE progra
1b5b5 6d 20 61 6e 64 20 72 65 73 65 74 73 20 74 68 65  m and resets the
1b5b6 20 70 50 61 72 73 65 20 73 74 72 75 63 74 75 72   pParse structur
1b5b7 65 20 66 6f 72 20 74 68 65 20 6e 65 78 74 0a 2a  e for the next.*
1b5b8 2a 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 4e  * parse..**.** N
1b5b9 6f 74 65 20 74 68 61 74 20 69 66 20 61 6e 20 65  ote that if an e
1b5ba 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 69  rror occurred, i
1b5bb 74 20 6d 69 67 68 74 20 62 65 20 74 68 65 20 63  t might be the c
1b5bc 61 73 65 20 74 68 61 74 0a 2a 2a 20 6e 6f 20 56  ase that.** no V
1b5bd 44 42 45 20 63 6f 64 65 20 77 61 73 20 67 65 6e  DBE code was gen
1b5be 65 72 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  erated..*/.SQLIT
1b5bf 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1b5c0 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69  qlite3FinishCodi
1b5c1 6e 67 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ng(Parse *pParse
1b5c2 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
1b5c3 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20  ;.  Vdbe *v;..  
1b5c4 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
1b5c5 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
1b5c6 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e  cFailed ) return
1b5c7 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
1b5c8 6e 65 73 74 65 64 20 29 20 72 65 74 75 72 6e 3b  nested ) return;
1b5c9 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
1b5ca 45 72 72 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  Err ) return;.  
1b5cb 69 66 28 20 21 70 50 61 72 73 65 2d 3e 70 56 64  if( !pParse->pVd
1b5cc 62 65 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50  be ){.    if( pP
1b5cd 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45  arse->rc==SQLITE
1b5ce 5f 4f 4b 20 26 26 20 70 50 61 72 73 65 2d 3e 6e  _OK && pParse->n
1b5cf 45 72 72 20 29 7b 0a 20 20 20 20 20 20 70 50 61  Err ){.      pPa
1b5d0 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45  rse->rc = SQLITE
1b5d1 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 72 65  _ERROR;.      re
1b5d2 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  turn;.    }.  }.
1b5d3 0a 20 20 2f 2a 20 42 65 67 69 6e 20 62 79 20 67  .  /* Begin by g
1b5d4 65 6e 65 72 61 74 69 6e 67 20 73 6f 6d 65 20 74  enerating some t
1b5d5 65 72 6d 69 6e 61 74 69 6f 6e 20 63 6f 64 65 20  ermination code 
1b5d6 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  at the end of th
1b5d7 65 0a 20 20 2a 2a 20 76 64 62 65 20 70 72 6f 67  e.  ** vdbe prog
1b5d8 72 61 6d 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73  ram.  */.  v = s
1b5d9 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
1b5da 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29  arse);.  if( v )
1b5db 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1b5dc 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 48 61  eAddOp0(v, OP_Ha
1b5dd 6c 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65  lt);..    /* The
1b5de 20 63 6f 6f 6b 69 65 20 6d 61 73 6b 20 63 6f 6e   cookie mask con
1b5df 74 61 69 6e 73 20 6f 6e 65 20 62 69 74 20 66 6f  tains one bit fo
1b5e0 72 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20  r each database 
1b5e1 66 69 6c 65 20 6f 70 65 6e 2e 0a 20 20 20 20 2a  file open..    *
1b5e2 2a 20 28 42 69 74 20 30 20 69 73 20 66 6f 72 20  * (Bit 0 is for 
1b5e3 6d 61 69 6e 2c 20 62 69 74 20 31 20 69 73 20 66  main, bit 1 is f
1b5e4 6f 72 20 74 65 6d 70 2c 20 61 6e 64 20 73 6f 20  or temp, and so 
1b5e5 66 6f 72 74 68 2e 29 20 20 42 69 74 73 20 61 72  forth.)  Bits ar
1b5e6 65 0a 20 20 20 20 2a 2a 20 73 65 74 20 66 6f 72  e.    ** set for
1b5e7 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20 74   each database t
1b5e8 68 61 74 20 69 73 20 75 73 65 64 2e 20 20 47 65  hat is used.  Ge
1b5e9 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 73  nerate code to s
1b5ea 74 61 72 74 20 61 0a 20 20 20 20 2a 2a 20 74 72  tart a.    ** tr
1b5eb 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 65 61 63  ansaction on eac
1b5ec 68 20 75 73 65 64 20 64 61 74 61 62 61 73 65 20  h used database 
1b5ed 61 6e 64 20 74 6f 20 76 65 72 69 66 79 20 74 68  and to verify th
1b5ee 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 0a  e schema cookie.
1b5ef 20 20 20 20 2a 2a 20 6f 6e 20 65 61 63 68 20 75      ** on each u
1b5f0 73 65 64 20 64 61 74 61 62 61 73 65 2e 0a 20 20  sed database..  
1b5f1 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61    */.    if( pPa
1b5f2 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 3e  rse->cookieGoto>
1b5f3 30 20 29 7b 0a 20 20 20 20 20 20 75 33 32 20 6d  0 ){.      u32 m
1b5f4 61 73 6b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69  ask;.      int i
1b5f5 44 62 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  Db;.      sqlite
1b5f6 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
1b5f7 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47   pParse->cookieG
1b5f8 6f 74 6f 2d 31 29 3b 0a 20 20 20 20 20 20 66 6f  oto-1);.      fo
1b5f9 72 28 69 44 62 3d 30 2c 20 6d 61 73 6b 3d 31 3b  r(iDb=0, mask=1;
1b5fa 20 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 6d 61   iDb<db->nDb; ma
1b5fb 73 6b 3c 3c 3d 31 2c 20 69 44 62 2b 2b 29 7b 0a  sk<<=1, iDb++){.
1b5fc 20 20 20 20 20 20 20 20 69 66 28 20 28 6d 61 73          if( (mas
1b5fd 6b 20 26 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b  k & pParse->cook
1b5fe 69 65 4d 61 73 6b 29 3d 3d 30 20 29 20 63 6f 6e  ieMask)==0 ) con
1b5ff 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 73  tinue;.        s
1b600 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74  qlite3VdbeUsesBt
1b601 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 20 20 20  ree(v, iDb);.   
1b602 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1b603 41 64 64 4f 70 32 28 76 2c 4f 50 5f 54 72 61 6e  AddOp2(v,OP_Tran
1b604 73 61 63 74 69 6f 6e 2c 20 69 44 62 2c 20 28 6d  saction, iDb, (m
1b605 61 73 6b 20 26 20 70 50 61 72 73 65 2d 3e 77 72  ask & pParse->wr
1b606 69 74 65 4d 61 73 6b 29 21 3d 30 29 3b 0a 20 20  iteMask)!=0);.  
1b607 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1b608 65 41 64 64 4f 70 32 28 76 2c 4f 50 5f 56 65 72  eAddOp2(v,OP_Ver
1b609 69 66 79 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20  ifyCookie, iDb, 
1b60a 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 56 61  pParse->cookieVa
1b60b 6c 75 65 5b 69 44 62 5d 29 3b 0a 20 20 20 20 20  lue[iDb]);.     
1b60c 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
1b60d 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1b60e 42 4c 45 0a 20 20 20 20 20 20 7b 0a 20 20 20 20  BLE.      {.    
1b60f 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20      int i;.     
1b610 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50     for(i=0; i<pP
1b611 61 72 73 65 2d 3e 6e 56 74 61 62 4c 6f 63 6b 3b  arse->nVtabLock;
1b612 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
1b613 20 63 68 61 72 20 2a 76 74 61 62 20 3d 20 28 63   char *vtab = (c
1b614 68 61 72 20 2a 29 70 50 61 72 73 65 2d 3e 61 70  har *)pParse->ap
1b615 56 74 61 62 4c 6f 63 6b 5b 69 5d 2d 3e 70 56 74  VtabLock[i]->pVt
1b616 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  ab;.          sq
1b617 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
1b618 76 2c 20 4f 50 5f 56 42 65 67 69 6e 2c 20 30 2c  v, OP_VBegin, 0,
1b619 20 30 2c 20 30 2c 20 76 74 61 62 2c 20 50 34 5f   0, 0, vtab, P4_
1b61a 56 54 41 42 29 3b 0a 20 20 20 20 20 20 20 20 7d  VTAB);.        }
1b61b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d  .        pParse-
1b61c 3e 6e 56 74 61 62 4c 6f 63 6b 20 3d 20 30 3b 0a  >nVtabLock = 0;.
1b61d 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a        }.#endif..
1b61e 20 20 20 20 20 20 2f 2a 20 4f 6e 63 65 20 61 6c        /* Once al
1b61f 6c 20 74 68 65 20 63 6f 6f 6b 69 65 73 20 68 61  l the cookies ha
1b620 76 65 20 62 65 65 6e 20 76 65 72 69 66 69 65 64  ve been verified
1b621 20 61 6e 64 20 74 72 61 6e 73 61 63 74 69 6f 6e   and transaction
1b622 73 20 6f 70 65 6e 65 64 2c 20 0a 20 20 20 20 20  s opened, .     
1b623 20 2a 2a 20 6f 62 74 61 69 6e 20 74 68 65 20 72   ** obtain the r
1b624 65 71 75 69 72 65 64 20 74 61 62 6c 65 2d 6c 6f  equired table-lo
1b625 63 6b 73 2e 20 54 68 69 73 20 69 73 20 61 20 6e  cks. This is a n
1b626 6f 2d 6f 70 20 75 6e 6c 65 73 73 20 74 68 65 20  o-op unless the 
1b627 0a 20 20 20 20 20 20 2a 2a 20 73 68 61 72 65 64  .      ** shared
1b628 2d 63 61 63 68 65 20 66 65 61 74 75 72 65 20 69  -cache feature i
1b629 73 20 65 6e 61 62 6c 65 64 2e 0a 20 20 20 20 20  s enabled..     
1b62a 20 2a 2f 0a 20 20 20 20 20 20 63 6f 64 65 54 61   */.      codeTa
1b62b 62 6c 65 4c 6f 63 6b 73 28 70 50 61 72 73 65 29  bleLocks(pParse)
1b62c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1b62d 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1b62e 47 6f 74 6f 2c 20 30 2c 20 70 50 61 72 73 65 2d  Goto, 0, pParse-
1b62f 3e 63 6f 6f 6b 69 65 47 6f 74 6f 29 3b 0a 20 20  >cookieGoto);.  
1b630 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
1b631 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20  ITE_OMIT_TRACE. 
1b632 20 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74     if( !db->init
1b633 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20 20 2f  .busy ){.      /
1b634 2a 20 43 68 61 6e 67 65 20 74 68 65 20 50 34 20  * Change the P4 
1b635 61 72 67 75 6d 65 6e 74 20 6f 66 20 74 68 65 20  argument of the 
1b636 66 69 72 73 74 20 6f 70 63 6f 64 65 20 28 77 68  first opcode (wh
1b637 69 63 68 20 77 69 6c 6c 20 61 6c 77 61 79 73 20  ich will always 
1b638 62 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 4f  be.      ** an O
1b639 50 5f 54 72 61 63 65 29 20 74 6f 20 62 65 20 74  P_Trace) to be t
1b63a 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74  he complete text
1b63b 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   of the current 
1b63c 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20  SQL statement.. 
1b63d 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 56 64       */.      Vd
1b63e 62 65 4f 70 20 2a 70 4f 70 20 3d 20 73 71 6c 69  beOp *pOp = sqli
1b63f 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20  te3VdbeGetOp(v, 
1b640 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f  0);.      if( pO
1b641 70 20 26 26 20 70 4f 70 2d 3e 6f 70 63 6f 64 65  p && pOp->opcode
1b642 3d 3d 4f 50 5f 54 72 61 63 65 20 29 7b 0a 20 20  ==OP_Trace ){.  
1b643 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1b644 65 43 68 61 6e 67 65 50 34 28 76 2c 20 30 2c 20  eChangeP4(v, 0, 
1b645 70 50 61 72 73 65 2d 3e 7a 53 71 6c 2c 20 70 50  pParse->zSql, pP
1b646 61 72 73 65 2d 3e 7a 54 61 69 6c 2d 70 50 61 72  arse->zTail-pPar
1b647 73 65 2d 3e 7a 53 71 6c 29 3b 0a 20 20 20 20 20  se->zSql);.     
1b648 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20   }.    }.#endif 
1b649 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  /* SQLITE_OMIT_T
1b64a 52 41 43 45 20 2a 2f 0a 20 20 7d 0a 0a 0a 20 20  RACE */.  }...  
1b64b 2f 2a 20 47 65 74 20 74 68 65 20 56 44 42 45 20  /* Get the VDBE 
1b64c 70 72 6f 67 72 61 6d 20 72 65 61 64 79 20 66 6f  program ready fo
1b64d 72 20 65 78 65 63 75 74 69 6f 6e 0a 20 20 2a 2f  r execution.  */
1b64e 0a 20 20 69 66 28 20 76 20 26 26 20 70 50 61 72  .  if( v && pPar
1b64f 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 26 26 20 21  se->nErr==0 && !
1b650 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1b651 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54   ){.#ifdef SQLIT
1b652 45 5f 44 45 42 55 47 0a 20 20 20 20 46 49 4c 45  E_DEBUG.    FILE
1b653 20 2a 74 72 61 63 65 20 3d 20 28 64 62 2d 3e 66   *trace = (db->f
1b654 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 56 64  lags & SQLITE_Vd
1b655 62 65 54 72 61 63 65 29 21 3d 30 20 3f 20 73 74  beTrace)!=0 ? st
1b656 64 6f 75 74 20 3a 20 30 3b 0a 20 20 20 20 73 71  dout : 0;.    sq
1b657 6c 69 74 65 33 56 64 62 65 54 72 61 63 65 28 76  lite3VdbeTrace(v
1b658 2c 20 74 72 61 63 65 29 3b 0a 23 65 6e 64 69 66  , trace);.#endif
1b659 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
1b65a 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43  rse->disableColC
1b65b 61 63 68 65 3d 3d 30 20 29 3b 20 20 2f 2a 20 44  ache==0 );  /* D
1b65c 69 73 61 62 6c 65 73 20 61 6e 64 20 72 65 2d 65  isables and re-e
1b65d 6e 61 62 6c 65 73 20 6d 61 74 63 68 20 2a 2f 0a  nables match */.
1b65e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1b65f 61 6b 65 52 65 61 64 79 28 76 2c 20 70 50 61 72  akeReady(v, pPar
1b660 73 65 2d 3e 6e 56 61 72 2c 20 70 50 61 72 73 65  se->nVar, pParse
1b661 2d 3e 6e 4d 65 6d 2b 33 2c 0a 20 20 20 20 20 20  ->nMem+3,.      
1b662 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b663 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b     pParse->nTab+
1b664 33 2c 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61  3, pParse->expla
1b665 69 6e 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  in);.    pParse-
1b666 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e  >rc = SQLITE_DON
1b667 45 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63  E;.    pParse->c
1b668 6f 6c 4e 61 6d 65 73 53 65 74 20 3d 20 30 3b 0a  olNamesSet = 0;.
1b669 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 72    }else if( pPar
1b66a 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  se->rc==SQLITE_O
1b66b 4b 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d  K ){.    pParse-
1b66c 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52  >rc = SQLITE_ERR
1b66d 4f 52 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73 65  OR;.  }.  pParse
1b66e 2d 3e 6e 54 61 62 20 3d 20 30 3b 0a 20 20 70 50  ->nTab = 0;.  pP
1b66f 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 30 3b 0a  arse->nMem = 0;.
1b670 20 20 70 50 61 72 73 65 2d 3e 6e 53 65 74 20 3d    pParse->nSet =
1b671 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 56   0;.  pParse->nV
1b672 61 72 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65  ar = 0;.  pParse
1b673 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 3d 20 30  ->cookieMask = 0
1b674 3b 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b  ;.  pParse->cook
1b675 69 65 47 6f 74 6f 20 3d 20 30 3b 0a 7d 0a 0a 2f  ieGoto = 0;.}../
1b676 2a 0a 2a 2a 20 52 75 6e 20 74 68 65 20 70 61 72  *.** Run the par
1b677 73 65 72 20 61 6e 64 20 63 6f 64 65 20 67 65 6e  ser and code gen
1b678 65 72 61 74 6f 72 20 72 65 63 75 72 73 69 76 65  erator recursive
1b679 6c 79 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67  ly in order to g
1b67a 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20  enerate.** code 
1b67b 66 6f 72 20 74 68 65 20 53 51 4c 20 73 74 61 74  for the SQL stat
1b67c 65 6d 65 6e 74 20 67 69 76 65 6e 20 6f 6e 74 6f  ement given onto
1b67d 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
1b67e 70 50 61 72 73 65 20 63 6f 6e 74 65 78 74 0a 2a  pParse context.*
1b67f 2a 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65  * currently unde
1b680 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 20  r construction. 
1b681 20 57 68 65 6e 20 74 68 65 20 70 61 72 73 65 72   When the parser
1b682 20 69 73 20 72 75 6e 20 72 65 63 75 72 73 69 76   is run recursiv
1b683 65 6c 79 0a 2a 2a 20 74 68 69 73 20 77 61 79 2c  ely.** this way,
1b684 20 74 68 65 20 66 69 6e 61 6c 20 4f 50 5f 48 61   the final OP_Ha
1b685 6c 74 20 69 73 20 6e 6f 74 20 61 70 70 65 6e 64  lt is not append
1b686 65 64 20 61 6e 64 20 6f 74 68 65 72 20 69 6e 69  ed and other ini
1b687 74 69 61 6c 69 7a 61 74 69 6f 6e 0a 2a 2a 20 61  tialization.** a
1b688 6e 64 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20  nd finalization 
1b689 73 74 65 70 73 20 61 72 65 20 6f 6d 69 74 74 65  steps are omitte
1b68a 64 20 62 65 63 61 75 73 65 20 74 68 6f 73 65 20  d because those 
1b68b 61 72 65 20 68 61 6e 64 6c 69 6e 67 20 62 79 20  are handling by 
1b68c 74 68 65 0a 2a 2a 20 6f 75 74 65 72 6d 6f 73 74  the.** outermost
1b68d 20 70 61 72 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e   parser..**.** N
1b68e 6f 74 20 65 76 65 72 79 74 68 69 6e 67 20 69 73  ot everything is
1b68f 20 6e 65 73 74 61 62 6c 65 2e 20 20 54 68 69 73   nestable.  This
1b690 20 66 61 63 69 6c 69 74 79 20 69 73 20 64 65 73   facility is des
1b691 69 67 6e 65 64 20 74 6f 20 70 65 72 6d 69 74 0a  igned to permit.
1b692 2a 2a 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54  ** INSERT, UPDAT
1b693 45 2c 20 61 6e 64 20 44 45 4c 45 54 45 20 6f 70  E, and DELETE op
1b694 65 72 61 74 69 6f 6e 73 20 61 67 61 69 6e 73 74  erations against
1b695 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 2e 20   SQLITE_MASTER. 
1b696 20 55 73 65 0a 2a 2a 20 63 61 72 65 20 69 66 20   Use.** care if 
1b697 79 6f 75 20 64 65 63 69 64 65 20 74 6f 20 74 72  you decide to tr
1b698 79 20 74 6f 20 75 73 65 20 74 68 69 73 20 72 6f  y to use this ro
1b699 75 74 69 6e 65 20 66 6f 72 20 73 6f 6d 65 20 6f  utine for some o
1b69a 74 68 65 72 20 70 75 72 70 6f 73 65 73 2e 0a 2a  ther purposes..*
1b69b 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1b69c 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4e 65 73   void sqlite3Nes
1b69d 74 65 64 50 61 72 73 65 28 50 61 72 73 65 20 2a  tedParse(Parse *
1b69e 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68  pParse, const ch
1b69f 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e  ar *zFormat, ...
1b6a0 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b  ){.  va_list ap;
1b6a1 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 23  .  char *zSql;.#
1b6a2 20 64 65 66 69 6e 65 20 53 41 56 45 5f 53 5a 20   define SAVE_SZ 
1b6a3 20 28 73 69 7a 65 6f 66 28 50 61 72 73 65 29 20   (sizeof(Parse) 
1b6a4 2d 20 6f 66 66 73 65 74 6f 66 28 50 61 72 73 65  - offsetof(Parse
1b6a5 2c 6e 56 61 72 29 29 0a 20 20 63 68 61 72 20 73  ,nVar)).  char s
1b6a6 61 76 65 42 75 66 5b 53 41 56 45 5f 53 5a 5d 3b  aveBuf[SAVE_SZ];
1b6a7 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ..  if( pParse->
1b6a8 6e 45 72 72 20 29 20 72 65 74 75 72 6e 3b 0a 20  nErr ) return;. 
1b6a9 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
1b6aa 3e 6e 65 73 74 65 64 3c 31 30 20 29 3b 20 20 2f  >nested<10 );  /
1b6ab 2a 20 4e 65 73 74 69 6e 67 20 73 68 6f 75 6c 64  * Nesting should
1b6ac 20 6f 6e 6c 79 20 62 65 20 6f 66 20 6c 69 6d 69   only be of limi
1b6ad 74 65 64 20 64 65 70 74 68 20 2a 2f 0a 20 20 76  ted depth */.  v
1b6ae 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72  a_start(ap, zFor
1b6af 6d 61 74 29 3b 0a 20 20 7a 53 71 6c 20 3d 20 73  mat);.  zSql = s
1b6b0 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 70  qlite3VMPrintf(p
1b6b1 50 61 72 73 65 2d 3e 64 62 2c 20 7a 46 6f 72 6d  Parse->db, zForm
1b6b2 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e  at, ap);.  va_en
1b6b3 64 28 61 70 29 3b 0a 20 20 69 66 28 20 7a 53 71  d(ap);.  if( zSq
1b6b4 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 70 50 61 72  l==0 ){.    pPar
1b6b5 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  se->db->mallocFa
1b6b6 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65  iled = 1;.    re
1b6b7 74 75 72 6e 3b 20 20 20 2f 2a 20 41 20 6d 61 6c  turn;   /* A mal
1b6b8 6c 6f 63 20 6d 75 73 74 20 68 61 76 65 20 66 61  loc must have fa
1b6b9 69 6c 65 64 20 2a 2f 0a 20 20 7d 0a 20 20 70 50  iled */.  }.  pP
1b6ba 61 72 73 65 2d 3e 6e 65 73 74 65 64 2b 2b 3b 0a  arse->nested++;.
1b6bb 20 20 6d 65 6d 63 70 79 28 73 61 76 65 42 75 66    memcpy(saveBuf
1b6bc 2c 20 26 70 50 61 72 73 65 2d 3e 6e 56 61 72 2c  , &pParse->nVar,
1b6bd 20 53 41 56 45 5f 53 5a 29 3b 0a 20 20 6d 65 6d   SAVE_SZ);.  mem
1b6be 73 65 74 28 26 70 50 61 72 73 65 2d 3e 6e 56 61  set(&pParse->nVa
1b6bf 72 2c 20 30 2c 20 53 41 56 45 5f 53 5a 29 3b 0a  r, 0, SAVE_SZ);.
1b6c0 20 20 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73    sqlite3RunPars
1b6c1 65 72 28 70 50 61 72 73 65 2c 20 7a 53 71 6c 2c  er(pParse, zSql,
1b6c2 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66   0);.  sqlite3_f
1b6c3 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20 6d 65 6d  ree(zSql);.  mem
1b6c4 63 70 79 28 26 70 50 61 72 73 65 2d 3e 6e 56 61  cpy(&pParse->nVa
1b6c5 72 2c 20 73 61 76 65 42 75 66 2c 20 53 41 56 45  r, saveBuf, SAVE
1b6c6 5f 53 5a 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  _SZ);.  pParse->
1b6c7 6e 65 73 74 65 64 2d 2d 3b 0a 7d 0a 0a 2f 2a 0a  nested--;.}../*.
1b6c8 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e  ** Locate the in
1b6c9 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72  -memory structur
1b6ca 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73  e that describes
1b6cb 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61   a particular da
1b6cc 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20  tabase.** table 
1b6cd 67 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f  given the name o
1b6ce 66 20 74 68 61 74 20 74 61 62 6c 65 20 61 6e 64  f that table and
1b6cf 20 28 6f 70 74 69 6f 6e 61 6c 6c 79 29 20 74 68   (optionally) th
1b6d0 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a  e name of the.**
1b6d1 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
1b6d2 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2e 20  ning the table. 
1b6d3 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20   Return NULL if 
1b6d4 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a  not found..**.**
1b6d5 20 49 66 20 7a 44 61 74 61 62 61 73 65 20 69 73   If zDatabase is
1b6d6 20 30 2c 20 61 6c 6c 20 64 61 74 61 62 61 73 65   0, all database
1b6d7 73 20 61 72 65 20 73 65 61 72 63 68 65 64 20 66  s are searched f
1b6d8 6f 72 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64  or the table and
1b6d9 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 6d 61   the.** first ma
1b6da 74 63 68 69 6e 67 20 74 61 62 6c 65 20 69 73 20  tching table is 
1b6db 72 65 74 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63  returned.  (No c
1b6dc 68 65 63 6b 69 6e 67 20 66 6f 72 20 64 75 70 6c  hecking for dupl
1b6dd 69 63 61 74 65 20 74 61 62 6c 65 0a 2a 2a 20 6e  icate table.** n
1b6de 61 6d 65 73 20 69 73 20 64 6f 6e 65 2e 29 20 20  ames is done.)  
1b6df 54 68 65 20 73 65 61 72 63 68 20 6f 72 64 65 72  The search order
1b6e0 20 69 73 20 54 45 4d 50 20 66 69 72 73 74 2c 20   is TEMP first, 
1b6e1 74 68 65 6e 20 4d 41 49 4e 2c 20 74 68 65 6e 20  then MAIN, then 
1b6e2 61 6e 79 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79  any.** auxiliary
1b6e3 20 64 61 74 61 62 61 73 65 73 20 61 64 64 65 64   databases added
1b6e4 20 75 73 69 6e 67 20 74 68 65 20 41 54 54 41 43   using the ATTAC
1b6e5 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a  H command..**.**
1b6e6 20 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 65   See also sqlite
1b6e7 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 29 2e 0a  3LocateTable()..
1b6e8 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1b6e9 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33  E Table *sqlite3
1b6ea 46 69 6e 64 54 61 62 6c 65 28 73 71 6c 69 74 65  FindTable(sqlite
1b6eb 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
1b6ec 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e 73 74 20  r *zName, const 
1b6ed 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 29  char *zDatabase)
1b6ee 7b 0a 20 20 54 61 62 6c 65 20 2a 70 20 3d 20 30  {.  Table *p = 0
1b6ef 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73  ;.  int i;.  ass
1b6f0 65 72 74 28 20 7a 4e 61 6d 65 21 3d 30 20 29 3b  ert( zName!=0 );
1b6f1 0a 20 20 66 6f 72 28 69 3d 4f 4d 49 54 5f 54 45  .  for(i=OMIT_TE
1b6f2 4d 50 44 42 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  MPDB; i<db->nDb;
1b6f3 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6a   i++){.    int j
1b6f4 20 3d 20 28 69 3c 32 29 20 3f 20 69 5e 31 20 3a   = (i<2) ? i^1 :
1b6f5 20 69 3b 20 20 20 2f 2a 20 53 65 61 72 63 68 20   i;   /* Search 
1b6f6 54 45 4d 50 20 62 65 66 6f 72 65 20 4d 41 49 4e  TEMP before MAIN
1b6f7 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a 44 61 74   */.    if( zDat
1b6f8 61 62 61 73 65 21 3d 30 20 26 26 20 73 71 6c 69  abase!=0 && sqli
1b6f9 74 65 33 53 74 72 49 43 6d 70 28 7a 44 61 74 61  te3StrICmp(zData
1b6fa 62 61 73 65 2c 20 64 62 2d 3e 61 44 62 5b 6a 5d  base, db->aDb[j]
1b6fb 2e 7a 4e 61 6d 65 29 20 29 20 63 6f 6e 74 69 6e  .zName) ) contin
1b6fc 75 65 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69  ue;.    p = sqli
1b6fd 74 65 33 48 61 73 68 46 69 6e 64 28 26 64 62 2d  te3HashFind(&db-
1b6fe 3e 61 44 62 5b 6a 5d 2e 70 53 63 68 65 6d 61 2d  >aDb[j].pSchema-
1b6ff 3e 74 62 6c 48 61 73 68 2c 20 7a 4e 61 6d 65 2c  >tblHash, zName,
1b700 20 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 2b 31   strlen(zName)+1
1b701 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29 20 62  );.    if( p ) b
1b702 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75  reak;.  }.  retu
1b703 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  rn p;.}../*.** L
1b704 6f 63 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d  ocate the in-mem
1b705 6f 72 79 20 73 74 72 75 63 74 75 72 65 20 74 68  ory structure th
1b706 61 74 20 64 65 73 63 72 69 62 65 73 20 61 20 70  at describes a p
1b707 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61  articular databa
1b708 73 65 0a 2a 2a 20 74 61 62 6c 65 20 67 69 76 65  se.** table give
1b709 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  n the name of th
1b70a 61 74 20 74 61 62 6c 65 20 61 6e 64 20 28 6f 70  at table and (op
1b70b 74 69 6f 6e 61 6c 6c 79 29 20 74 68 65 20 6e 61  tionally) the na
1b70c 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74  me of the.** dat
1b70d 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67  abase containing
1b70e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74   the table.  Ret
1b70f 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20  urn NULL if not 
1b710 66 6f 75 6e 64 2e 20 20 41 6c 73 6f 20 6c 65 61  found.  Also lea
1b711 76 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 6d  ve an.** error m
1b712 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65  essage in pParse
1b713 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a  ->zErrMsg..**.**
1b714 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20   The difference 
1b715 62 65 74 77 65 65 6e 20 74 68 69 73 20 72 6f 75  between this rou
1b716 74 69 6e 65 20 61 6e 64 20 73 71 6c 69 74 65 33  tine and sqlite3
1b717 46 69 6e 64 54 61 62 6c 65 28 29 20 69 73 20 74  FindTable() is t
1b718 68 61 74 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74  hat this.** rout
1b719 69 6e 65 20 6c 65 61 76 65 73 20 61 6e 20 65 72  ine leaves an er
1b71a 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70  ror message in p
1b71b 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 77  Parse->zErrMsg w
1b71c 68 65 72 65 0a 2a 2a 20 73 71 6c 69 74 65 33 46  here.** sqlite3F
1b71d 69 6e 64 54 61 62 6c 65 28 29 20 64 6f 65 73 20  indTable() does 
1b71e 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  not..*/.SQLITE_P
1b71f 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71  RIVATE Table *sq
1b720 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
1b721 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
1b722 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f  e,         /* co
1b723 6e 74 65 78 74 20 69 6e 20 77 68 69 63 68 20 74  ntext in which t
1b724 6f 20 72 65 70 6f 72 74 20 65 72 72 6f 72 73 20  o report errors 
1b725 2a 2f 0a 20 20 69 6e 74 20 69 73 56 69 65 77 2c  */.  int isView,
1b726 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1b727 72 75 65 20 69 66 20 6c 6f 6f 6b 69 6e 67 20 66  rue if looking f
1b728 6f 72 20 61 20 56 49 45 57 20 72 61 74 68 65 72  or a VIEW rather
1b729 20 74 68 61 6e 20 61 20 54 41 42 4c 45 20 2a 2f   than a TABLE */
1b72a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1b72b 4e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61 6d  Name,     /* Nam
1b72c 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 77  e of the table w
1b72d 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f  e are looking fo
1b72e 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  r */.  const cha
1b72f 72 20 2a 7a 44 62 61 73 65 20 20 20 20 20 2f 2a  r *zDbase     /*
1b730 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   Name of the dat
1b731 61 62 61 73 65 2e 20 20 4d 69 67 68 74 20 62 65  abase.  Might be
1b732 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 54 61   NULL */.){.  Ta
1b733 62 6c 65 20 2a 70 3b 0a 0a 20 20 2f 2a 20 52 65  ble *p;..  /* Re
1b734 61 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ad the database 
1b735 73 63 68 65 6d 61 2e 20 49 66 20 61 6e 20 65 72  schema. If an er
1b736 72 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65 61 76  ror occurs, leav
1b737 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  e an error messa
1b738 67 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f 64 65  ge.  ** and code
1b739 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72   in pParse and r
1b73a 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20  eturn NULL. */. 
1b73b 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
1b73c 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
1b73d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20  a(pParse) ){.   
1b73e 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a   return 0;.  }..
1b73f 20 20 70 20 3d 20 73 71 6c 69 74 65 33 46 69 6e    p = sqlite3Fin
1b740 64 54 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 64  dTable(pParse->d
1b741 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44 62 61 73 65  b, zName, zDbase
1b742 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b  );.  if( p==0 ){
1b743 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
1b744 2a 7a 4d 73 67 20 3d 20 69 73 56 69 65 77 20 3f  *zMsg = isView ?
1b745 20 22 6e 6f 20 73 75 63 68 20 76 69 65 77 22 20   "no such view" 
1b746 3a 20 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65  : "no such table
1b747 22 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 61 73  ";.    if( zDbas
1b748 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  e ){.      sqlit
1b749 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1b74a 65 2c 20 22 25 73 3a 20 25 73 2e 25 73 22 2c 20  e, "%s: %s.%s", 
1b74b 7a 4d 73 67 2c 20 7a 44 62 61 73 65 2c 20 7a 4e  zMsg, zDbase, zN
1b74c 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ame);.    }else{
1b74d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
1b74e 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1b74f 25 73 3a 20 25 73 22 2c 20 7a 4d 73 67 2c 20 7a  %s: %s", zMsg, z
1b750 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Name);.    }.   
1b751 20 70 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63   pParse->checkSc
1b752 68 65 6d 61 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  hema = 1;.  }.  
1b753 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
1b754 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e  ** Locate the in
1b755 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72  -memory structur
1b756 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73  e that describes
1b757 20 0a 2a 2a 20 61 20 70 61 72 74 69 63 75 6c 61   .** a particula
1b758 72 20 69 6e 64 65 78 20 67 69 76 65 6e 20 74 68  r index given th
1b759 65 20 6e 61 6d 65 20 6f 66 20 74 68 61 74 20 69  e name of that i
1b75a 6e 64 65 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20  ndex.** and the 
1b75b 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  name of the data
1b75c 62 61 73 65 20 74 68 61 74 20 63 6f 6e 74 61 69  base that contai
1b75d 6e 73 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a  ns the index..**
1b75e 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20   Return NULL if 
1b75f 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a  not found..**.**
1b760 20 49 66 20 7a 44 61 74 61 62 61 73 65 20 69 73   If zDatabase is
1b761 20 30 2c 20 61 6c 6c 20 64 61 74 61 62 61 73 65   0, all database
1b762 73 20 61 72 65 20 73 65 61 72 63 68 65 64 20 66  s are searched f
1b763 6f 72 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20  or the.** table 
1b764 61 6e 64 20 74 68 65 20 66 69 72 73 74 20 6d 61  and the first ma
1b765 74 63 68 69 6e 67 20 69 6e 64 65 78 20 69 73 20  tching index is 
1b766 72 65 74 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63  returned.  (No c
1b767 68 65 63 6b 69 6e 67 0a 2a 2a 20 66 6f 72 20 64  hecking.** for d
1b768 75 70 6c 69 63 61 74 65 20 69 6e 64 65 78 20 6e  uplicate index n
1b769 61 6d 65 73 20 69 73 20 64 6f 6e 65 2e 29 20 20  ames is done.)  
1b76a 54 68 65 20 73 65 61 72 63 68 20 6f 72 64 65 72  The search order
1b76b 20 69 73 0a 2a 2a 20 54 45 4d 50 20 66 69 72 73   is.** TEMP firs
1b76c 74 2c 20 74 68 65 6e 20 4d 41 49 4e 2c 20 74 68  t, then MAIN, th
1b76d 65 6e 20 61 6e 79 20 61 75 78 69 6c 69 61 72 79  en any auxiliary
1b76e 20 64 61 74 61 62 61 73 65 73 20 61 64 64 65 64   databases added
1b76f 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 41 54  .** using the AT
1b770 54 41 43 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f  TACH command..*/
1b771 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1b772 49 6e 64 65 78 20 2a 73 71 6c 69 74 65 33 46 69  Index *sqlite3Fi
1b773 6e 64 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20  ndIndex(sqlite3 
1b774 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *db, const char 
1b775 2a 7a 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68  *zName, const ch
1b776 61 72 20 2a 7a 44 62 29 7b 0a 20 20 49 6e 64 65  ar *zDb){.  Inde
1b777 78 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20  x *p = 0;.  int 
1b778 69 3b 0a 20 20 66 6f 72 28 69 3d 4f 4d 49 54 5f  i;.  for(i=OMIT_
1b779 54 45 4d 50 44 42 3b 20 69 3c 64 62 2d 3e 6e 44  TEMPDB; i<db->nD
1b77a 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74  b; i++){.    int
1b77b 20 6a 20 3d 20 28 69 3c 32 29 20 3f 20 69 5e 31   j = (i<2) ? i^1
1b77c 20 3a 20 69 3b 20 20 2f 2a 20 53 65 61 72 63 68   : i;  /* Search
1b77d 20 54 45 4d 50 20 62 65 66 6f 72 65 20 4d 41 49   TEMP before MAI
1b77e 4e 20 2a 2f 0a 20 20 20 20 53 63 68 65 6d 61 20  N */.    Schema 
1b77f 2a 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61  *pSchema = db->a
1b780 44 62 5b 6a 5d 2e 70 53 63 68 65 6d 61 3b 0a 20  Db[j].pSchema;. 
1b781 20 20 20 69 66 28 20 7a 44 62 20 26 26 20 73 71     if( zDb && sq
1b782 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44 62  lite3StrICmp(zDb
1b783 2c 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61  , db->aDb[j].zNa
1b784 6d 65 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  me) ) continue;.
1b785 20 20 20 20 61 73 73 65 72 74 28 20 70 53 63 68      assert( pSch
1b786 65 6d 61 20 7c 7c 20 28 6a 3d 3d 31 20 26 26 20  ema || (j==1 && 
1b787 21 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29  !db->aDb[1].pBt)
1b788 20 29 3b 0a 20 20 20 20 69 66 28 20 70 53 63 68   );.    if( pSch
1b789 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 70 20 3d  ema ){.      p =
1b78a 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64   sqlite3HashFind
1b78b 28 26 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61  (&pSchema->idxHa
1b78c 73 68 2c 20 7a 4e 61 6d 65 2c 20 73 74 72 6c 65  sh, zName, strle
1b78d 6e 28 7a 4e 61 6d 65 29 2b 31 29 3b 0a 20 20 20  n(zName)+1);.   
1b78e 20 7d 0a 20 20 20 20 69 66 28 20 70 20 29 20 62   }.    if( p ) b
1b78f 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75  reak;.  }.  retu
1b790 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  rn p;.}../*.** R
1b791 65 63 6c 61 69 6d 20 74 68 65 20 6d 65 6d 6f 72  eclaim the memor
1b792 79 20 75 73 65 64 20 62 79 20 61 6e 20 69 6e 64  y used by an ind
1b793 65 78 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ex.*/.static voi
1b794 64 20 66 72 65 65 49 6e 64 65 78 28 49 6e 64 65  d freeIndex(Inde
1b795 78 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33  x *p){.  sqlite3
1b796 5f 66 72 65 65 28 70 2d 3e 7a 43 6f 6c 41 66 66  _free(p->zColAff
1b797 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
1b798 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  e(p);.}../*.** R
1b799 65 6d 6f 76 65 20 74 68 65 20 67 69 76 65 6e 20  emove the given 
1b79a 69 6e 64 65 78 20 66 72 6f 6d 20 74 68 65 20 69  index from the i
1b79b 6e 64 65 78 20 68 61 73 68 20 74 61 62 6c 65 2c  ndex hash table,
1b79c 20 61 6e 64 20 66 72 65 65 0a 2a 2a 20 69 74 73   and free.** its
1b79d 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72   memory structur
1b79e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e  es..**.** The in
1b79f 64 65 78 20 69 73 20 72 65 6d 6f 76 65 64 20 66  dex is removed f
1b7a0 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
1b7a1 20 68 61 73 68 20 74 61 62 6c 65 73 20 62 75 74   hash tables but
1b7a2 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74 20 75 6e  .** it is not un
1b7a3 6c 69 6e 6b 65 64 20 66 72 6f 6d 20 74 68 65 20  linked from the 
1b7a4 54 61 62 6c 65 20 74 68 61 74 20 69 74 20 69 6e  Table that it in
1b7a5 64 65 78 65 73 2e 0a 2a 2a 20 55 6e 6c 69 6e 6b  dexes..** Unlink
1b7a6 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 54 61 62  ing from the Tab
1b7a7 6c 65 20 6d 75 73 74 20 62 65 20 64 6f 6e 65 20  le must be done 
1b7a8 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66  by the calling f
1b7a9 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  unction..*/.stat
1b7aa 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 44 65  ic void sqliteDe
1b7ab 6c 65 74 65 49 6e 64 65 78 28 49 6e 64 65 78 20  leteIndex(Index 
1b7ac 2a 70 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 4f  *p){.  Index *pO
1b7ad 6c 64 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  ld;.  const char
1b7ae 20 2a 7a 4e 61 6d 65 20 3d 20 70 2d 3e 7a 4e 61   *zName = p->zNa
1b7af 6d 65 3b 0a 0a 20 20 70 4f 6c 64 20 3d 20 73 71  me;..  pOld = sq
1b7b0 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
1b7b1 26 70 2d 3e 70 53 63 68 65 6d 61 2d 3e 69 64 78  &p->pSchema->idx
1b7b2 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 73 74 72  Hash, zName, str
1b7b3 6c 65 6e 28 20 7a 4e 61 6d 65 29 2b 31 2c 20 30  len( zName)+1, 0
1b7b4 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 6c  );.  assert( pOl
1b7b5 64 3d 3d 30 20 7c 7c 20 70 4f 6c 64 3d 3d 70 20  d==0 || pOld==p 
1b7b6 29 3b 0a 20 20 66 72 65 65 49 6e 64 65 78 28 70  );.  freeIndex(p
1b7b7 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20  );.}../*.** For 
1b7b8 74 68 65 20 69 6e 64 65 78 20 63 61 6c 6c 65 64  the index called
1b7b9 20 7a 49 64 78 4e 61 6d 65 20 77 68 69 63 68 20   zIdxName which 
1b7ba 69 73 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20  is found in the 
1b7bb 64 61 74 61 62 61 73 65 20 69 44 62 2c 0a 2a 2a  database iDb,.**
1b7bc 20 75 6e 6c 69 6b 65 20 74 68 61 74 20 69 6e 64   unlike that ind
1b7bd 65 78 20 66 72 6f 6d 20 69 74 73 20 54 61 62 6c  ex from its Tabl
1b7be 65 20 74 68 65 6e 20 72 65 6d 6f 76 65 20 74 68  e then remove th
1b7bf 65 20 69 6e 64 65 78 20 66 72 6f 6d 0a 2a 2a 20  e index from.** 
1b7c0 74 68 65 20 69 6e 64 65 78 20 68 61 73 68 20 74  the index hash t
1b7c1 61 62 6c 65 20 61 6e 64 20 66 72 65 65 20 61 6c  able and free al
1b7c2 6c 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75  l memory structu
1b7c3 72 65 73 20 61 73 73 6f 63 69 61 74 65 64 0a 2a  res associated.*
1b7c4 2a 20 77 69 74 68 20 74 68 65 20 69 6e 64 65 78  * with the index
1b7c5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1b7c6 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1b7c7 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 49  UnlinkAndDeleteI
1b7c8 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62  ndex(sqlite3 *db
1b7c9 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74  , int iDb, const
1b7ca 20 63 68 61 72 20 2a 7a 49 64 78 4e 61 6d 65 29   char *zIdxName)
1b7cb 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65  {.  Index *pInde
1b7cc 78 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20  x;.  int len;.  
1b7cd 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26 64  Hash *pHash = &d
1b7ce 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68  b->aDb[iDb].pSch
1b7cf 65 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0a 0a 20  ema->idxHash;.. 
1b7d0 20 6c 65 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 49   len = strlen(zI
1b7d1 64 78 4e 61 6d 65 29 3b 0a 20 20 70 49 6e 64 65  dxName);.  pInde
1b7d2 78 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49  x = sqlite3HashI
1b7d3 6e 73 65 72 74 28 70 48 61 73 68 2c 20 7a 49 64  nsert(pHash, zId
1b7d4 78 4e 61 6d 65 2c 20 6c 65 6e 2b 31 2c 20 30 29  xName, len+1, 0)
1b7d5 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78 20 29  ;.  if( pIndex )
1b7d6 7b 0a 20 20 20 20 69 66 28 20 70 49 6e 64 65 78  {.    if( pIndex
1b7d7 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78  ->pTable->pIndex
1b7d8 3d 3d 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20  ==pIndex ){.    
1b7d9 20 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65    pIndex->pTable
1b7da 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49 6e 64 65  ->pIndex = pInde
1b7db 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65  x->pNext;.    }e
1b7dc 6c 73 65 7b 0a 20 20 20 20 20 20 49 6e 64 65 78  lse{.      Index
1b7dd 20 2a 70 3b 0a 20 20 20 20 20 20 66 6f 72 28 70   *p;.      for(p
1b7de 3d 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 2d  =pIndex->pTable-
1b7df 3e 70 49 6e 64 65 78 3b 20 70 20 26 26 20 70 2d  >pIndex; p && p-
1b7e0 3e 70 4e 65 78 74 21 3d 70 49 6e 64 65 78 3b 20  >pNext!=pIndex; 
1b7e1 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 7d 0a 20 20  p=p->pNext){}.  
1b7e2 20 20 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e      if( p && p->
1b7e3 70 4e 65 78 74 3d 3d 70 49 6e 64 65 78 20 29 7b  pNext==pIndex ){
1b7e4 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 78  .        p->pNex
1b7e5 74 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78  t = pIndex->pNex
1b7e6 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  t;.      }.    }
1b7e7 0a 20 20 20 20 66 72 65 65 49 6e 64 65 78 28 70  .    freeIndex(p
1b7e8 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 20 20 64 62  Index);.  }.  db
1b7e9 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54  ->flags |= SQLIT
1b7ea 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b  E_InternChanges;
1b7eb 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 20  .}../*.** Erase 
1b7ec 61 6c 6c 20 73 63 68 65 6d 61 20 69 6e 66 6f 72  all schema infor
1b7ed 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20  mation from the 
1b7ee 69 6e 2d 6d 65 6d 6f 72 79 20 68 61 73 68 20 74  in-memory hash t
1b7ef 61 62 6c 65 73 20 6f 66 0a 2a 2a 20 61 20 73 69  ables of.** a si
1b7f0 6e 67 6c 65 20 64 61 74 61 62 61 73 65 2e 20 20  ngle database.  
1b7f1 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
1b7f2 63 61 6c 6c 65 64 20 74 6f 20 72 65 63 6c 61 69  called to reclai
1b7f3 6d 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f  m memory.** befo
1b7f4 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  re the database 
1b7f5 63 6c 6f 73 65 73 2e 20 20 49 74 20 69 73 20 61  closes.  It is a
1b7f6 6c 73 6f 20 63 61 6c 6c 65 64 20 64 75 72 69 6e  lso called durin
1b7f7 67 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20  g a rollback.** 
1b7f8 69 66 20 74 68 65 72 65 20 77 65 72 65 20 73 63  if there were sc
1b7f9 68 65 6d 61 20 63 68 61 6e 67 65 73 20 64 75 72  hema changes dur
1b7fa 69 6e 67 20 74 68 65 20 74 72 61 6e 73 61 63 74  ing the transact
1b7fb 69 6f 6e 20 6f 72 20 69 66 20 61 0a 2a 2a 20 73  ion or if a.** s
1b7fc 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 20 6d 69 73  chema-cookie mis
1b7fd 6d 61 74 63 68 20 6f 63 63 75 72 73 2e 0a 2a 2a  match occurs..**
1b7fe 0a 2a 2a 20 49 66 20 69 44 62 3c 3d 30 20 74 68  .** If iDb<=0 th
1b7ff 65 6e 20 72 65 73 65 74 20 74 68 65 20 69 6e 74  en reset the int
1b800 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 74 61 62  ernal schema tab
1b801 6c 65 73 20 66 6f 72 20 61 6c 6c 20 64 61 74 61  les for all data
1b802 62 61 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 20 20  base.** files.  
1b803 49 66 20 69 44 62 3e 3d 32 20 74 68 65 6e 20 72  If iDb>=2 then r
1b804 65 73 65 74 20 74 68 65 20 69 6e 74 65 72 6e 61  eset the interna
1b805 6c 20 73 63 68 65 6d 61 20 66 6f 72 20 6f 6e 6c  l schema for onl
1b806 79 20 74 68 65 0a 2a 2a 20 73 69 6e 67 6c 65 20  y the.** single 
1b807 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 2e 0a  file indicated..
1b808 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1b809 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65  E void sqlite3Re
1b80a 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d  setInternalSchem
1b80b 61 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  a(sqlite3 *db, i
1b80c 6e 74 20 69 44 62 29 7b 0a 20 20 69 6e 74 20 69  nt iDb){.  int i
1b80d 2c 20 6a 3b 0a 20 20 61 73 73 65 72 74 28 20 69  , j;.  assert( i
1b80e 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d  Db>=0 && iDb<db-
1b80f 3e 6e 44 62 20 29 3b 0a 0a 20 20 69 66 28 20 69  >nDb );..  if( i
1b810 44 62 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  Db==0 ){.    sql
1b811 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c  ite3BtreeEnterAl
1b812 6c 28 64 62 29 3b 0a 20 20 7d 0a 20 20 66 6f 72  l(db);.  }.  for
1b813 28 69 3d 69 44 62 3b 20 69 3c 64 62 2d 3e 6e 44  (i=iDb; i<db->nD
1b814 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 44 62 20  b; i++){.    Db 
1b815 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b  *pDb = &db->aDb[
1b816 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 62 2d  i];.    if( pDb-
1b817 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20  >pSchema ){.    
1b818 20 20 61 73 73 65 72 74 28 69 3d 3d 31 20 7c 7c    assert(i==1 ||
1b819 20 28 70 44 62 2d 3e 70 42 74 20 26 26 20 73 71   (pDb->pBt && sq
1b81a 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d  lite3BtreeHoldsM
1b81b 75 74 65 78 28 70 44 62 2d 3e 70 42 74 29 29 29  utex(pDb->pBt)))
1b81c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  ;.      sqlite3S
1b81d 63 68 65 6d 61 46 72 65 65 28 70 44 62 2d 3e 70  chemaFree(pDb->p
1b81e 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d 0a 20  Schema);.    }. 
1b81f 20 20 20 69 66 28 20 69 44 62 3e 30 20 29 20 72     if( iDb>0 ) r
1b820 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73  eturn;.  }.  ass
1b821 65 72 74 28 20 69 44 62 3d 3d 30 20 29 3b 0a 20  ert( iDb==0 );. 
1b822 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53   db->flags &= ~S
1b823 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e  QLITE_InternChan
1b824 67 65 73 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  ges;.  sqlite3Bt
1b825 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b  reeLeaveAll(db);
1b826 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 6f 72  ..  /* If one or
1b827 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 61 75 78   more of the aux
1b828 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20  iliary database 
1b829 66 69 6c 65 73 20 68 61 73 20 62 65 65 6e 20 63  files has been c
1b82a 6c 6f 73 65 64 2c 0a 20 20 2a 2a 20 74 68 65 6e  losed,.  ** then
1b82b 20 72 65 6d 6f 76 65 20 74 68 65 6d 20 66 72 6f   remove them fro
1b82c 6d 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20  m the auxiliary 
1b82d 64 61 74 61 62 61 73 65 20 6c 69 73 74 2e 20 20  database list.  
1b82e 57 65 20 74 61 6b 65 20 74 68 65 0a 20 20 2a 2a  We take the.  **
1b82f 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 20   opportunity to 
1b830 64 6f 20 74 68 69 73 20 68 65 72 65 20 73 69 6e  do this here sin
1b831 63 65 20 77 65 20 68 61 76 65 20 6a 75 73 74 20  ce we have just 
1b832 64 65 6c 65 74 65 64 20 61 6c 6c 20 6f 66 20 74  deleted all of t
1b833 68 65 0a 20 20 2a 2a 20 73 63 68 65 6d 61 20 68  he.  ** schema h
1b834 61 73 68 20 74 61 62 6c 65 73 20 61 6e 64 20 74  ash tables and t
1b835 68 65 72 65 66 6f 72 65 20 64 6f 20 6e 6f 74 20  herefore do not 
1b836 68 61 76 65 20 74 6f 20 6d 61 6b 65 20 61 6e 79  have to make any
1b837 20 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20 74 6f   changes.  ** to
1b838 20 61 6e 79 20 6f 66 20 74 68 6f 73 65 20 74 61   any of those ta
1b839 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  bles..  */.  for
1b83a 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  (i=0; i<db->nDb;
1b83b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63   i++){.    struc
1b83c 74 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62 2d  t Db *pDb = &db-
1b83d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 66 28  >aDb[i];.    if(
1b83e 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 7b 0a   pDb->pBt==0 ){.
1b83f 20 20 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70        if( pDb->p
1b840 41 75 78 20 26 26 20 70 44 62 2d 3e 78 46 72 65  Aux && pDb->xFre
1b841 65 41 75 78 20 29 20 70 44 62 2d 3e 78 46 72 65  eAux ) pDb->xFre
1b842 65 41 75 78 28 70 44 62 2d 3e 70 41 75 78 29 3b  eAux(pDb->pAux);
1b843 0a 20 20 20 20 20 20 70 44 62 2d 3e 70 41 75 78  .      pDb->pAux
1b844 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
1b845 20 20 66 6f 72 28 69 3d 6a 3d 32 3b 20 69 3c 64    for(i=j=2; i<d
1b846 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20  b->nDb; i++){.  
1b847 20 20 73 74 72 75 63 74 20 44 62 20 2a 70 44 62    struct Db *pDb
1b848 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a   = &db->aDb[i];.
1b849 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 42 74      if( pDb->pBt
1b84a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
1b84b 69 74 65 33 5f 66 72 65 65 28 70 44 62 2d 3e 7a  ite3_free(pDb->z
1b84c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 44 62  Name);.      pDb
1b84d 2d 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20  ->zName = 0;.   
1b84e 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
1b84f 20 7d 0a 20 20 20 20 69 66 28 20 6a 3c 69 20 29   }.    if( j<i )
1b850 7b 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b  {.      db->aDb[
1b851 6a 5d 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 3b  j] = db->aDb[i];
1b852 0a 20 20 20 20 7d 0a 20 20 20 20 6a 2b 2b 3b 0a  .    }.    j++;.
1b853 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 64 62    }.  memset(&db
1b854 2d 3e 61 44 62 5b 6a 5d 2c 20 30 2c 20 28 64 62  ->aDb[j], 0, (db
1b855 2d 3e 6e 44 62 2d 6a 29 2a 73 69 7a 65 6f 66 28  ->nDb-j)*sizeof(
1b856 64 62 2d 3e 61 44 62 5b 6a 5d 29 29 3b 0a 20 20  db->aDb[j]));.  
1b857 64 62 2d 3e 6e 44 62 20 3d 20 6a 3b 0a 20 20 69  db->nDb = j;.  i
1b858 66 28 20 64 62 2d 3e 6e 44 62 3c 3d 32 20 26 26  f( db->nDb<=2 &&
1b859 20 64 62 2d 3e 61 44 62 21 3d 64 62 2d 3e 61 44   db->aDb!=db->aD
1b85a 62 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 6d  bStatic ){.    m
1b85b 65 6d 63 70 79 28 64 62 2d 3e 61 44 62 53 74 61  emcpy(db->aDbSta
1b85c 74 69 63 2c 20 64 62 2d 3e 61 44 62 2c 20 32 2a  tic, db->aDb, 2*
1b85d 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 30  sizeof(db->aDb[0
1b85e 5d 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ]));.    sqlite3
1b85f 5f 66 72 65 65 28 64 62 2d 3e 61 44 62 29 3b 0a  _free(db->aDb);.
1b860 20 20 20 20 64 62 2d 3e 61 44 62 20 3d 20 64 62      db->aDb = db
1b861 2d 3e 61 44 62 53 74 61 74 69 63 3b 0a 20 20 7d  ->aDbStatic;.  }
1b862 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
1b863 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
1b864 20 77 68 65 6e 20 61 20 63 6f 6d 6d 69 74 20 6f   when a commit o
1b865 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ccurs..*/.SQLITE
1b866 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1b867 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72  lite3CommitInter
1b868 6e 61 6c 43 68 61 6e 67 65 73 28 73 71 6c 69 74  nalChanges(sqlit
1b869 65 33 20 2a 64 62 29 7b 0a 20 20 64 62 2d 3e 66  e3 *db){.  db->f
1b86a 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f  lags &= ~SQLITE_
1b86b 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 7d  InternChanges;.}
1b86c 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68  ../*.** Clear th
1b86d 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66  e column names f
1b86e 72 6f 6d 20 61 20 74 61 62 6c 65 20 6f 72 20 76  rom a table or v
1b86f 69 65 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  iew..*/.static v
1b870 6f 69 64 20 73 71 6c 69 74 65 52 65 73 65 74 43  oid sqliteResetC
1b871 6f 6c 75 6d 6e 4e 61 6d 65 73 28 54 61 62 6c 65  olumnNames(Table
1b872 20 2a 70 54 61 62 6c 65 29 7b 0a 20 20 69 6e 74   *pTable){.  int
1b873 20 69 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43   i;.  Column *pC
1b874 6f 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54  ol;.  assert( pT
1b875 61 62 6c 65 21 3d 30 20 29 3b 0a 20 20 69 66 28  able!=0 );.  if(
1b876 20 28 70 43 6f 6c 20 3d 20 70 54 61 62 6c 65 2d   (pCol = pTable-
1b877 3e 61 43 6f 6c 29 21 3d 30 20 29 7b 0a 20 20 20  >aCol)!=0 ){.   
1b878 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62   for(i=0; i<pTab
1b879 6c 65 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70  le->nCol; i++, p
1b87a 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  Col++){.      sq
1b87b 6c 69 74 65 33 5f 66 72 65 65 28 70 43 6f 6c 2d  lite3_free(pCol-
1b87c 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 73  >zName);.      s
1b87d 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
1b87e 28 70 43 6f 6c 2d 3e 70 44 66 6c 74 29 3b 0a 20  (pCol->pDflt);. 
1b87f 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
1b880 65 28 70 43 6f 6c 2d 3e 7a 54 79 70 65 29 3b 0a  e(pCol->zType);.
1b881 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
1b882 65 65 28 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 29 3b  ee(pCol->zColl);
1b883 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
1b884 65 33 5f 66 72 65 65 28 70 54 61 62 6c 65 2d 3e  e3_free(pTable->
1b885 61 43 6f 6c 29 3b 0a 20 20 7d 0a 20 20 70 54 61  aCol);.  }.  pTa
1b886 62 6c 65 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20  ble->aCol = 0;. 
1b887 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20   pTable->nCol = 
1b888 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f  0;.}../*.** Remo
1b889 76 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 64 61  ve the memory da
1b88a 74 61 20 73 74 72 75 63 74 75 72 65 73 20 61 73  ta structures as
1b88b 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
1b88c 65 20 67 69 76 65 6e 0a 2a 2a 20 54 61 62 6c 65  e given.** Table
1b88d 2e 20 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 72  .  No changes ar
1b88e 65 20 6d 61 64 65 20 74 6f 20 64 69 73 6b 20 62  e made to disk b
1b88f 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a  y this routine..
1b890 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
1b891 6e 65 20 6a 75 73 74 20 64 65 6c 65 74 65 73 20  ne just deletes 
1b892 74 68 65 20 64 61 74 61 20 73 74 72 75 63 74 75  the data structu
1b893 72 65 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74  re.  It does not
1b894 20 75 6e 6c 69 6e 6b 0a 2a 2a 20 74 68 65 20 74   unlink.** the t
1b895 61 62 6c 65 20 64 61 74 61 20 73 74 72 75 63 74  able data struct
1b896 75 72 65 20 66 72 6f 6d 20 74 68 65 20 68 61 73  ure from the has
1b897 68 20 74 61 62 6c 65 2e 20 20 4e 6f 72 20 64 6f  h table.  Nor do
1b898 65 73 20 69 74 20 72 65 6d 6f 76 65 0a 2a 2a 20  es it remove.** 
1b899 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 66 72 6f  foreign keys fro
1b89a 6d 20 74 68 65 20 73 71 6c 69 74 65 2e 61 46 4b  m the sqlite.aFK
1b89b 65 79 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20  ey hash table.  
1b89c 42 75 74 20 69 74 20 64 6f 65 73 20 64 65 73 74  But it does dest
1b89d 72 6f 79 0a 2a 2a 20 6d 65 6d 6f 72 79 20 73 74  roy.** memory st
1b89e 72 75 63 74 75 72 65 73 20 6f 66 20 74 68 65 20  ructures of the 
1b89f 69 6e 64 69 63 65 73 20 61 6e 64 20 66 6f 72 65  indices and fore
1b8a0 69 67 6e 20 6b 65 79 73 20 61 73 73 6f 63 69 61  ign keys associa
1b8a1 74 65 64 20 77 69 74 68 20 0a 2a 2a 20 74 68 65  ted with .** the
1b8a2 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54   table..*/.SQLIT
1b8a3 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1b8a4 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c  qlite3DeleteTabl
1b8a5 65 28 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 29  e(Table *pTable)
1b8a6 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65  {.  Index *pInde
1b8a7 78 2c 20 2a 70 4e 65 78 74 3b 0a 20 20 46 4b 65  x, *pNext;.  FKe
1b8a8 79 20 2a 70 46 4b 65 79 2c 20 2a 70 4e 65 78 74  y *pFKey, *pNext
1b8a9 46 4b 65 79 3b 0a 0a 20 20 69 66 28 20 70 54 61  FKey;..  if( pTa
1b8aa 62 6c 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ble==0 ) return;
1b8ab 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 64 65  ..  /* Do not de
1b8ac 6c 65 74 65 20 74 68 65 20 74 61 62 6c 65 20 75  lete the table u
1b8ad 6e 74 69 6c 20 74 68 65 20 72 65 66 65 72 65 6e  ntil the referen
1b8ae 63 65 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73  ce count reaches
1b8af 20 7a 65 72 6f 2e 20 2a 2f 0a 20 20 70 54 61 62   zero. */.  pTab
1b8b0 6c 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66  le->nRef--;.  if
1b8b1 28 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66 3e 30  ( pTable->nRef>0
1b8b2 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
1b8b3 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54    }.  assert( pT
1b8b4 61 62 6c 65 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b  able->nRef==0 );
1b8b5 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 61 6c  ..  /* Delete al
1b8b6 6c 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69  l indices associ
1b8b7 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 74  ated with this t
1b8b8 61 62 6c 65 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  able.  */.  for(
1b8b9 70 49 6e 64 65 78 20 3d 20 70 54 61 62 6c 65 2d  pIndex = pTable-
1b8ba 3e 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 3b  >pIndex; pIndex;
1b8bb 20 70 49 6e 64 65 78 3d 70 4e 65 78 74 29 7b 0a   pIndex=pNext){.
1b8bc 20 20 20 20 70 4e 65 78 74 20 3d 20 70 49 6e 64      pNext = pInd
1b8bd 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 61  ex->pNext;.    a
1b8be 73 73 65 72 74 28 20 70 49 6e 64 65 78 2d 3e 70  ssert( pIndex->p
1b8bf 53 63 68 65 6d 61 3d 3d 70 54 61 62 6c 65 2d 3e  Schema==pTable->
1b8c0 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 73  pSchema );.    s
1b8c1 71 6c 69 74 65 44 65 6c 65 74 65 49 6e 64 65 78  qliteDeleteIndex
1b8c2 28 70 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 0a 23  (pIndex);.  }..#
1b8c3 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1b8c4 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20  IT_FOREIGN_KEY. 
1b8c5 20 2f 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 66   /* Delete all f
1b8c6 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 73 73 6f  oreign keys asso
1b8c7 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73  ciated with this
1b8c8 20 74 61 62 6c 65 2e 20 20 54 68 65 20 6b 65 79   table.  The key
1b8c9 73 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 68 61  s.  ** should ha
1b8ca 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ve already been 
1b8cb 75 6e 6c 69 6e 6b 65 64 20 66 72 6f 6d 20 74 68  unlinked from th
1b8cc 65 20 70 53 63 68 65 6d 61 2d 3e 61 46 4b 65 79  e pSchema->aFKey
1b8cd 20 68 61 73 68 20 74 61 62 6c 65 20 0a 20 20 2a   hash table .  *
1b8ce 2f 0a 20 20 66 6f 72 28 70 46 4b 65 79 3d 70 54  /.  for(pFKey=pT
1b8cf 61 62 6c 65 2d 3e 70 46 4b 65 79 3b 20 70 46 4b  able->pFKey; pFK
1b8d0 65 79 3b 20 70 46 4b 65 79 3d 70 4e 65 78 74 46  ey; pFKey=pNextF
1b8d1 4b 65 79 29 7b 0a 20 20 20 20 70 4e 65 78 74 46  Key){.    pNextF
1b8d2 4b 65 79 20 3d 20 70 46 4b 65 79 2d 3e 70 4e 65  Key = pFKey->pNe
1b8d3 78 74 46 72 6f 6d 3b 0a 20 20 20 20 61 73 73 65  xtFrom;.    asse
1b8d4 72 74 28 20 73 71 6c 69 74 65 33 48 61 73 68 46  rt( sqlite3HashF
1b8d5 69 6e 64 28 26 70 54 61 62 6c 65 2d 3e 70 53 63  ind(&pTable->pSc
1b8d6 68 65 6d 61 2d 3e 61 46 4b 65 79 2c 0a 20 20 20  hema->aFKey,.   
1b8d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b8d8 20 20 20 20 20 20 20 20 70 46 4b 65 79 2d 3e 7a          pFKey->z
1b8d9 54 6f 2c 20 73 74 72 6c 65 6e 28 70 46 4b 65 79  To, strlen(pFKey
1b8da 2d 3e 7a 54 6f 29 2b 31 29 21 3d 70 46 4b 65 79  ->zTo)+1)!=pFKey
1b8db 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   );.    sqlite3_
1b8dc 66 72 65 65 28 70 46 4b 65 79 29 3b 0a 20 20 7d  free(pFKey);.  }
1b8dd 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 44 65  .#endif..  /* De
1b8de 6c 65 74 65 20 74 68 65 20 54 61 62 6c 65 20 73  lete the Table s
1b8df 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 2e  tructure itself.
1b8e0 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 52 65  .  */.  sqliteRe
1b8e1 73 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70  setColumnNames(p
1b8e2 54 61 62 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65  Table);.  sqlite
1b8e3 33 5f 66 72 65 65 28 70 54 61 62 6c 65 2d 3e 7a  3_free(pTable->z
1b8e4 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Name);.  sqlite3
1b8e5 5f 66 72 65 65 28 70 54 61 62 6c 65 2d 3e 7a 43  _free(pTable->zC
1b8e6 6f 6c 41 66 66 29 3b 0a 20 20 73 71 6c 69 74 65  olAff);.  sqlite
1b8e7 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 54  3SelectDelete(pT
1b8e8 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74 29 3b 0a  able->pSelect);.
1b8e9 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1b8ea 4d 49 54 5f 43 48 45 43 4b 0a 20 20 73 71 6c 69  MIT_CHECK.  sqli
1b8eb 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 54  te3ExprDelete(pT
1b8ec 61 62 6c 65 2d 3e 70 43 68 65 63 6b 29 3b 0a 23  able->pCheck);.#
1b8ed 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 56  endif.  sqlite3V
1b8ee 74 61 62 43 6c 65 61 72 28 70 54 61 62 6c 65 29  tabClear(pTable)
1b8ef 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
1b8f0 28 70 54 61 62 6c 65 29 3b 0a 7d 0a 0a 2f 2a 0a  (pTable);.}../*.
1b8f1 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 67 69  ** Unlink the gi
1b8f2 76 65 6e 20 74 61 62 6c 65 20 66 72 6f 6d 20 74  ven table from t
1b8f3 68 65 20 68 61 73 68 20 74 61 62 6c 65 73 20 61  he hash tables a
1b8f4 6e 64 20 74 68 65 20 64 65 6c 65 74 65 20 74 68  nd the delete th
1b8f5 65 0a 2a 2a 20 74 61 62 6c 65 20 73 74 72 75 63  e.** table struc
1b8f6 74 75 72 65 20 77 69 74 68 20 61 6c 6c 20 69 74  ture with all it
1b8f7 73 20 69 6e 64 69 63 65 73 20 61 6e 64 20 66 6f  s indices and fo
1b8f8 72 65 69 67 6e 20 6b 65 79 73 2e 0a 2a 2f 0a 53  reign keys..*/.S
1b8f9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1b8fa 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b  id sqlite3Unlink
1b8fb 41 6e 64 44 65 6c 65 74 65 54 61 62 6c 65 28 73  AndDeleteTable(s
1b8fc 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
1b8fd 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  iDb, const char 
1b8fe 2a 7a 54 61 62 4e 61 6d 65 29 7b 0a 20 20 54 61  *zTabName){.  Ta
1b8ff 62 6c 65 20 2a 70 3b 0a 20 20 46 4b 65 79 20 2a  ble *p;.  FKey *
1b900 70 46 31 2c 20 2a 70 46 32 3b 0a 20 20 44 62 20  pF1, *pF2;.  Db 
1b901 2a 70 44 62 3b 0a 0a 20 20 61 73 73 65 72 74 28  *pDb;..  assert(
1b902 20 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65   db!=0 );.  asse
1b903 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44  rt( iDb>=0 && iD
1b904 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61  b<db->nDb );.  a
1b905 73 73 65 72 74 28 20 7a 54 61 62 4e 61 6d 65 20  ssert( zTabName 
1b906 26 26 20 7a 54 61 62 4e 61 6d 65 5b 30 5d 20 29  && zTabName[0] )
1b907 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61  ;.  pDb = &db->a
1b908 44 62 5b 69 44 62 5d 3b 0a 20 20 70 20 3d 20 73  Db[iDb];.  p = s
1b909 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74  qlite3HashInsert
1b90a 28 26 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e  (&pDb->pSchema->
1b90b 74 62 6c 48 61 73 68 2c 20 7a 54 61 62 4e 61 6d  tblHash, zTabNam
1b90c 65 2c 20 73 74 72 6c 65 6e 28 7a 54 61 62 4e 61  e, strlen(zTabNa
1b90d 6d 65 29 2b 31 2c 30 29 3b 0a 20 20 69 66 28 20  me)+1,0);.  if( 
1b90e 70 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  p ){.#ifndef SQL
1b90f 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e  ITE_OMIT_FOREIGN
1b910 5f 4b 45 59 0a 20 20 20 20 66 6f 72 28 70 46 31  _KEY.    for(pF1
1b911 3d 70 2d 3e 70 46 4b 65 79 3b 20 70 46 31 3b 20  =p->pFKey; pF1; 
1b912 70 46 31 3d 70 46 31 2d 3e 70 4e 65 78 74 46 72  pF1=pF1->pNextFr
1b913 6f 6d 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e  om){.      int n
1b914 54 6f 20 3d 20 73 74 72 6c 65 6e 28 70 46 31 2d  To = strlen(pF1-
1b915 3e 7a 54 6f 29 20 2b 20 31 3b 0a 20 20 20 20 20  >zTo) + 1;.     
1b916 20 70 46 32 20 3d 20 73 71 6c 69 74 65 33 48 61   pF2 = sqlite3Ha
1b917 73 68 46 69 6e 64 28 26 70 44 62 2d 3e 70 53 63  shFind(&pDb->pSc
1b918 68 65 6d 61 2d 3e 61 46 4b 65 79 2c 20 70 46 31  hema->aFKey, pF1
1b919 2d 3e 7a 54 6f 2c 20 6e 54 6f 29 3b 0a 20 20 20  ->zTo, nTo);.   
1b91a 20 20 20 69 66 28 20 70 46 32 3d 3d 70 46 31 20     if( pF2==pF1 
1b91b 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1b91c 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 44  e3HashInsert(&pD
1b91d 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 61 46 4b 65  b->pSchema->aFKe
1b91e 79 2c 20 70 46 31 2d 3e 7a 54 6f 2c 20 6e 54 6f  y, pF1->zTo, nTo
1b91f 2c 20 70 46 31 2d 3e 70 4e 65 78 74 54 6f 29 3b  , pF1->pNextTo);
1b920 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1b921 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 46 32        while( pF2
1b922 20 26 26 20 70 46 32 2d 3e 70 4e 65 78 74 54 6f   && pF2->pNextTo
1b923 21 3d 70 46 31 20 29 7b 20 70 46 32 3d 70 46 32  !=pF1 ){ pF2=pF2
1b924 2d 3e 70 4e 65 78 74 54 6f 3b 20 7d 0a 20 20 20  ->pNextTo; }.   
1b925 20 20 20 20 20 69 66 28 20 70 46 32 20 29 7b 0a       if( pF2 ){.
1b926 20 20 20 20 20 20 20 20 20 20 70 46 32 2d 3e 70            pF2->p
1b927 4e 65 78 74 54 6f 20 3d 20 70 46 31 2d 3e 70 4e  NextTo = pF1->pN
1b928 65 78 74 54 6f 3b 0a 20 20 20 20 20 20 20 20 7d  extTo;.        }
1b929 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23  .      }.    }.#
1b92a 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 74 65  endif.    sqlite
1b92b 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 29 3b  3DeleteTable(p);
1b92c 0a 20 20 7d 0a 20 20 64 62 2d 3e 66 6c 61 67 73  .  }.  db->flags
1b92d 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72   |= SQLITE_Inter
1b92e 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a  nChanges;.}../*.
1b92f 2a 2a 20 47 69 76 65 6e 20 61 20 74 6f 6b 65 6e  ** Given a token
1b930 2c 20 72 65 74 75 72 6e 20 61 20 73 74 72 69 6e  , return a strin
1b931 67 20 74 68 61 74 20 63 6f 6e 73 69 73 74 73 20  g that consists 
1b932 6f 66 20 74 68 65 20 74 65 78 74 20 6f 66 20 74  of the text of t
1b933 68 61 74 0a 2a 2a 20 74 6f 6b 65 6e 20 77 69 74  hat.** token wit
1b934 68 20 61 6e 79 20 71 75 6f 74 61 74 69 6f 6e 73  h any quotations
1b935 20 72 65 6d 6f 76 65 64 2e 20 20 53 70 61 63 65   removed.  Space
1b936 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74   to hold the ret
1b937 75 72 6e 65 64 20 73 74 72 69 6e 67 0a 2a 2a 20  urned string.** 
1b938 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  is obtained from
1b939 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20   sqliteMalloc() 
1b93a 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65 65  and must be free
1b93b 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67  d by the calling
1b93c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a  .** function..**
1b93d 0a 2a 2a 20 54 6f 6b 65 6e 73 20 61 72 65 20 6f  .** Tokens are o
1b93e 66 74 65 6e 20 6a 75 73 74 20 70 6f 69 6e 74 65  ften just pointe
1b93f 72 73 20 69 6e 74 6f 20 74 68 65 20 6f 72 69 67  rs into the orig
1b940 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 61 6e  inal SQL text an
1b941 64 20 73 6f 0a 2a 2a 20 61 72 65 20 6e 6f 74 20  d so.** are not 
1b942 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 20  \000 terminated 
1b943 61 6e 64 20 61 72 65 20 6e 6f 74 20 70 65 72 73  and are not pers
1b944 69 73 74 65 6e 74 2e 20 20 54 68 65 20 72 65 74  istent.  The ret
1b945 75 72 6e 65 64 20 73 74 72 69 6e 67 0a 2a 2a 20  urned string.** 
1b946 69 73 20 5c 30 30 30 20 74 65 72 6d 69 6e 61 74  is \000 terminat
1b947 65 64 20 61 6e 64 20 69 73 20 70 65 72 73 69 73  ed and is persis
1b948 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tent..*/.SQLITE_
1b949 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71  PRIVATE char *sq
1b94a 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
1b94b 65 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  en(sqlite3 *db, 
1b94c 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20  Token *pName){. 
1b94d 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20   char *zName;.  
1b94e 69 66 28 20 70 4e 61 6d 65 20 29 7b 0a 20 20 20  if( pName ){.   
1b94f 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   zName = sqlite3
1b950 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63  DbStrNDup(db, (c
1b951 68 61 72 2a 29 70 4e 61 6d 65 2d 3e 7a 2c 20 70  har*)pName->z, p
1b952 4e 61 6d 65 2d 3e 6e 29 3b 0a 20 20 20 20 73 71  Name->n);.    sq
1b953 6c 69 74 65 33 44 65 71 75 6f 74 65 28 7a 4e 61  lite3Dequote(zNa
1b954 6d 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  me);.  }else{.  
1b955 20 20 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d    zName = 0;.  }
1b956 0a 20 20 72 65 74 75 72 6e 20 7a 4e 61 6d 65 3b  .  return zName;
1b957 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74  .}../*.** Open t
1b958 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
1b959 20 74 61 62 6c 65 20 73 74 6f 72 65 64 20 69 6e   table stored in
1b95a 20 64 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72   database number
1b95b 20 69 44 62 20 66 6f 72 0a 2a 2a 20 77 72 69 74   iDb for.** writ
1b95c 69 6e 67 2e 20 54 68 65 20 74 61 62 6c 65 20 69  ing. The table i
1b95d 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 63  s opened using c
1b95e 75 72 73 6f 72 20 30 2e 0a 2a 2f 0a 53 51 4c 49  ursor 0..*/.SQLI
1b95f 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1b960 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65  sqlite3OpenMaste
1b961 72 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 2c  rTable(Parse *p,
1b962 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 64 62   int iDb){.  Vdb
1b963 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65  e *v = sqlite3Ge
1b964 74 56 64 62 65 28 70 29 3b 0a 20 20 73 71 6c 69  tVdbe(p);.  sqli
1b965 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20  te3TableLock(p, 
1b966 69 44 62 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54  iDb, MASTER_ROOT
1b967 2c 20 31 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c  , 1, SCHEMA_TABL
1b968 45 28 69 44 62 29 29 3b 0a 20 20 73 71 6c 69 74  E(iDb));.  sqlit
1b969 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1b96a 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73  OP_SetNumColumns
1b96b 2c 20 30 2c 20 35 29 3b 2f 2a 20 73 71 6c 69 74  , 0, 5);/* sqlit
1b96c 65 5f 6d 61 73 74 65 72 20 68 61 73 20 35 20 63  e_master has 5 c
1b96d 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 73 71 6c 69  olumns */.  sqli
1b96e 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
1b96f 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 30   OP_OpenWrite, 0
1b970 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 69  , MASTER_ROOT, i
1b971 44 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  Db);.}../*.** Th
1b972 65 20 74 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 63  e token *pName c
1b973 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 61 6d 65  ontains the name
1b974 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 28   of a database (
1b975 65 69 74 68 65 72 20 22 6d 61 69 6e 22 20 6f 72  either "main" or
1b976 0a 2a 2a 20 22 74 65 6d 70 22 20 6f 72 20 74 68  .** "temp" or th
1b977 65 20 6e 61 6d 65 20 6f 66 20 61 6e 20 61 74 74  e name of an att
1b978 61 63 68 65 64 20 64 62 29 2e 20 54 68 69 73 20  ached db). This 
1b979 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
1b97a 74 68 65 0a 2a 2a 20 69 6e 64 65 78 20 6f 66 20  the.** index of 
1b97b 74 68 65 20 6e 61 6d 65 64 20 64 61 74 61 62 61  the named databa
1b97c 73 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 2c  se in db->aDb[],
1b97d 20 6f 72 20 2d 31 20 69 66 20 74 68 65 20 6e 61   or -1 if the na
1b97e 6d 65 64 20 64 62 20 0a 2a 2a 20 64 6f 65 73 20  med db .** does 
1b97f 6e 6f 74 20 65 78 69 73 74 2e 0a 2a 2f 0a 53 51  not exist..*/.SQ
1b980 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1b981 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28 73   sqlite3FindDb(s
1b982 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 6f 6b 65  qlite3 *db, Toke
1b983 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 69 6e 74  n *pName){.  int
1b984 20 69 20 3d 20 2d 31 3b 20 20 20 20 2f 2a 20 44   i = -1;    /* D
1b985 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a  atabase number *
1b986 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20  /.  int n;      
1b987 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1b988 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 74 68  characters in th
1b989 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 44 62 20 2a  e name */.  Db *
1b98a 70 44 62 3b 20 20 20 20 20 20 20 2f 2a 20 41 20  pDb;       /* A 
1b98b 64 61 74 61 62 61 73 65 20 77 68 6f 73 65 20 6e  database whose n
1b98c 61 6d 65 20 73 70 61 63 65 20 69 73 20 62 65 69  ame space is bei
1b98d 6e 67 20 73 65 61 72 63 68 65 64 20 2a 2f 0a 20  ng searched */. 
1b98e 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
1b98f 2f 2a 20 4e 61 6d 65 20 77 65 20 61 72 65 20 73  /* Name we are s
1b990 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0a  earching for */.
1b991 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  .  zName = sqlit
1b992 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
1b993 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66  db, pName);.  if
1b994 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 6e  ( zName ){.    n
1b995 20 3d 20 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29   = strlen(zName)
1b996 3b 0a 20 20 20 20 66 6f 72 28 69 3d 28 64 62 2d  ;.    for(i=(db-
1b997 3e 6e 44 62 2d 31 29 2c 20 70 44 62 3d 26 64 62  >nDb-1), pDb=&db
1b998 2d 3e 61 44 62 5b 69 5d 3b 20 69 3e 3d 30 3b 20  ->aDb[i]; i>=0; 
1b999 69 2d 2d 2c 20 70 44 62 2d 2d 29 7b 0a 20 20 20  i--, pDb--){.   
1b99a 20 20 20 69 66 28 20 28 21 4f 4d 49 54 5f 54 45     if( (!OMIT_TE
1b99b 4d 50 44 42 20 7c 7c 20 69 21 3d 31 20 29 20 26  MPDB || i!=1 ) &
1b99c 26 20 6e 3d 3d 73 74 72 6c 65 6e 28 70 44 62 2d  & n==strlen(pDb-
1b99d 3e 7a 4e 61 6d 65 29 20 26 26 20 0a 20 20 20 20  >zName) && .    
1b99e 20 20 20 20 20 20 30 3d 3d 73 71 6c 69 74 65 33        0==sqlite3
1b99f 53 74 72 49 43 6d 70 28 70 44 62 2d 3e 7a 4e 61  StrICmp(pDb->zNa
1b9a0 6d 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20  me, zName) ){.  
1b9a1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1b9a2 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
1b9a3 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4e 61 6d  qlite3_free(zNam
1b9a4 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  e);.  }.  return
1b9a5 20 69 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 74 61   i;.}../* The ta
1b9a6 62 6c 65 20 6f 72 20 76 69 65 77 20 6f 72 20 74  ble or view or t
1b9a7 72 69 67 67 65 72 20 6e 61 6d 65 20 69 73 20 70  rigger name is p
1b9a8 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 72 6f  assed to this ro
1b9a9 75 74 69 6e 65 20 76 69 61 20 74 6f 6b 65 6e 73  utine via tokens
1b9aa 0a 2a 2a 20 70 4e 61 6d 65 31 20 61 6e 64 20 70  .** pName1 and p
1b9ab 4e 61 6d 65 32 2e 20 49 66 20 74 68 65 20 74 61  Name2. If the ta
1b9ac 62 6c 65 20 6e 61 6d 65 20 77 61 73 20 66 75 6c  ble name was ful
1b9ad 6c 79 20 71 75 61 6c 69 66 69 65 64 2c 20 66 6f  ly qualified, fo
1b9ae 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a  r example:.**.**
1b9af 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 78 78   CREATE TABLE xx
1b9b0 78 2e 79 79 79 20 28 2e 2e 2e 29 3b 0a 2a 2a 20  x.yyy (...);.** 
1b9b1 0a 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20  .** Then pName1 
1b9b2 69 73 20 73 65 74 20 74 6f 20 22 78 78 78 22 20  is set to "xxx" 
1b9b3 61 6e 64 20 70 4e 61 6d 65 32 20 22 79 79 79 22  and pName2 "yyy"
1b9b4 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68  . On the other h
1b9b5 61 6e 64 20 69 66 0a 2a 2a 20 74 68 65 20 74 61  and if.** the ta
1b9b6 62 6c 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20  ble name is not 
1b9b7 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 2c  fully qualified,
1b9b8 20 69 2e 65 2e 3a 0a 2a 2a 0a 2a 2a 20 43 52 45   i.e.:.**.** CRE
1b9b9 41 54 45 20 54 41 42 4c 45 20 79 79 79 28 2e 2e  ATE TABLE yyy(..
1b9ba 2e 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 70  .);.**.** Then p
1b9bb 4e 61 6d 65 31 20 69 73 20 73 65 74 20 74 6f 20  Name1 is set to 
1b9bc 22 79 79 79 22 20 61 6e 64 20 70 4e 61 6d 65 32  "yyy" and pName2
1b9bd 20 69 73 20 22 22 2e 0a 2a 2a 0a 2a 2a 20 54 68   is ""..**.** Th
1b9be 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20  is routine sets 
1b9bf 74 68 65 20 2a 70 70 55 6e 71 75 61 6c 20 70 6f  the *ppUnqual po
1b9c0 69 6e 74 65 72 20 74 6f 20 70 6f 69 6e 74 20 61  inter to point a
1b9c1 74 20 74 68 65 20 74 6f 6b 65 6e 20 28 70 4e 61  t the token (pNa
1b9c2 6d 65 31 20 6f 72 0a 2a 2a 20 70 4e 61 6d 65 32  me1 or.** pName2
1b9c3 29 20 74 68 61 74 20 73 74 6f 72 65 73 20 74 68  ) that stores th
1b9c4 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74 61  e unqualified ta
1b9c5 62 6c 65 20 6e 61 6d 65 2e 20 20 54 68 65 20 69  ble name.  The i
1b9c6 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20 64  ndex of the.** d
1b9c7 61 74 61 62 61 73 65 20 22 78 78 78 22 20 69 73  atabase "xxx" is
1b9c8 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51   returned..*/.SQ
1b9c9 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1b9ca 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e   sqlite3TwoPartN
1b9cb 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ame(.  Parse *pP
1b9cc 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 50 61  arse,      /* Pa
1b9cd 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67  rsing and code g
1b9ce 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78  enerating contex
1b9cf 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e  t */.  Token *pN
1b9d0 61 6d 65 31 2c 20 20 20 20 20 20 2f 2a 20 54 68  ame1,      /* Th
1b9d1 65 20 22 78 78 78 22 20 69 6e 20 74 68 65 20 6e  e "xxx" in the n
1b9d2 61 6d 65 20 22 78 78 78 2e 79 79 79 22 20 6f 72  ame "xxx.yyy" or
1b9d3 20 22 78 78 78 22 20 2a 2f 0a 20 20 54 6f 6b 65   "xxx" */.  Toke
1b9d4 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 20  n *pName2,      
1b9d5 2f 2a 20 54 68 65 20 22 79 79 79 22 20 69 6e 20  /* The "yyy" in 
1b9d6 74 68 65 20 6e 61 6d 65 20 22 78 78 78 2e 79 79  the name "xxx.yy
1b9d7 79 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 2a  y" */.  Token **
1b9d8 70 55 6e 71 75 61 6c 20 20 20 20 20 2f 2a 20 57  pUnqual     /* W
1b9d9 72 69 74 65 20 74 68 65 20 75 6e 71 75 61 6c 69  rite the unquali
1b9da 66 69 65 64 20 6f 62 6a 65 63 74 20 6e 61 6d 65  fied object name
1b9db 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e   here */.){.  in
1b9dc 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20  t iDb;          
1b9dd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
1b9de 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 68  abase holding th
1b9df 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 73 71  e object */.  sq
1b9e0 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
1b9e1 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 70  se->db;..  if( p
1b9e2 4e 61 6d 65 32 20 26 26 20 70 4e 61 6d 65 32 2d  Name2 && pName2-
1b9e3 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 61 73 73 65  >n>0 ){.    asse
1b9e4 72 74 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75  rt( !db->init.bu
1b9e5 73 79 20 29 3b 0a 20 20 20 20 2a 70 55 6e 71 75  sy );.    *pUnqu
1b9e6 61 6c 20 3d 20 70 4e 61 6d 65 32 3b 0a 20 20 20  al = pName2;.   
1b9e7 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 46 69   iDb = sqlite3Fi
1b9e8 6e 64 44 62 28 64 62 2c 20 70 4e 61 6d 65 31 29  ndDb(db, pName1)
1b9e9 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3c 30 20  ;.    if( iDb<0 
1b9ea 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1b9eb 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
1b9ec 20 22 75 6e 6b 6e 6f 77 6e 20 64 61 74 61 62 61   "unknown databa
1b9ed 73 65 20 25 54 22 2c 20 70 4e 61 6d 65 31 29 3b  se %T", pName1);
1b9ee 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  .      pParse->n
1b9ef 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74  Err++;.      ret
1b9f0 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20  urn -1;.    }.  
1b9f1 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
1b9f2 74 28 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3d  t( db->init.iDb=
1b9f3 3d 30 20 7c 7c 20 64 62 2d 3e 69 6e 69 74 2e 62  =0 || db->init.b
1b9f4 75 73 79 20 29 3b 0a 20 20 20 20 69 44 62 20 3d  usy );.    iDb =
1b9f5 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3b 0a 20   db->init.iDb;. 
1b9f6 20 20 20 2a 70 55 6e 71 75 61 6c 20 3d 20 70 4e     *pUnqual = pN
1b9f7 61 6d 65 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ame1;.  }.  retu
1b9f8 72 6e 20 69 44 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rn iDb;.}../*.**
1b9f9 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
1b9fa 20 75 73 65 64 20 74 6f 20 63 68 65 63 6b 20 69   used to check i
1b9fb 66 20 74 68 65 20 55 54 46 2d 38 20 73 74 72 69  f the UTF-8 stri
1b9fc 6e 67 20 7a 4e 61 6d 65 20 69 73 20 61 20 6c 65  ng zName is a le
1b9fd 67 61 6c 0a 2a 2a 20 75 6e 71 75 61 6c 69 66 69  gal.** unqualifi
1b9fe 65 64 20 6e 61 6d 65 20 66 6f 72 20 61 20 6e 65  ed name for a ne
1b9ff 77 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 20  w schema object 
1ba00 28 74 61 62 6c 65 2c 20 69 6e 64 65 78 2c 20 76  (table, index, v
1ba01 69 65 77 20 6f 72 0a 2a 2a 20 74 72 69 67 67 65  iew or.** trigge
1ba02 72 29 2e 20 41 6c 6c 20 6e 61 6d 65 73 20 61 72  r). All names ar
1ba03 65 20 6c 65 67 61 6c 20 65 78 63 65 70 74 20 74  e legal except t
1ba04 68 6f 73 65 20 74 68 61 74 20 62 65 67 69 6e 20  hose that begin 
1ba05 77 69 74 68 20 74 68 65 20 73 74 72 69 6e 67 0a  with the string.
1ba06 2a 2a 20 22 73 71 6c 69 74 65 5f 22 20 28 69 6e  ** "sqlite_" (in
1ba07 20 75 70 70 65 72 2c 20 6c 6f 77 65 72 20 6f 72   upper, lower or
1ba08 20 6d 69 78 65 64 20 63 61 73 65 29 2e 20 54 68   mixed case). Th
1ba09 69 73 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68  is portion of th
1ba0a 65 20 6e 61 6d 65 73 70 61 63 65 0a 2a 2a 20 69  e namespace.** i
1ba0b 73 20 72 65 73 65 72 76 65 64 20 66 6f 72 20 69  s reserved for i
1ba0c 6e 74 65 72 6e 61 6c 20 75 73 65 2e 0a 2a 2f 0a  nternal use..*/.
1ba0d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1ba0e 6e 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b 4f  nt sqlite3CheckO
1ba0f 62 6a 65 63 74 4e 61 6d 65 28 50 61 72 73 65 20  bjectName(Parse 
1ba10 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63  *pParse, const c
1ba11 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69  har *zName){.  i
1ba12 66 28 20 21 70 50 61 72 73 65 2d 3e 64 62 2d 3e  f( !pParse->db->
1ba13 69 6e 69 74 2e 62 75 73 79 20 26 26 20 70 50 61  init.busy && pPa
1ba14 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 0a  rse->nested==0 .
1ba15 20 20 20 20 20 20 20 20 20 20 26 26 20 28 70 50            && (pP
1ba16 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20  arse->db->flags 
1ba17 26 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63  & SQLITE_WriteSc
1ba18 68 65 6d 61 29 3d 3d 30 0a 20 20 20 20 20 20 20  hema)==0.       
1ba19 20 20 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33     && 0==sqlite3
1ba1a 53 74 72 4e 49 43 6d 70 28 7a 4e 61 6d 65 2c 20  StrNICmp(zName, 
1ba1b 22 73 71 6c 69 74 65 5f 22 2c 20 37 29 20 29 7b  "sqlite_", 7) ){
1ba1c 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
1ba1d 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6f 62  rMsg(pParse, "ob
1ba1e 6a 65 63 74 20 6e 61 6d 65 20 72 65 73 65 72 76  ject name reserv
1ba1f 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20  ed for internal 
1ba20 75 73 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29  use: %s", zName)
1ba21 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
1ba22 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20  ITE_ERROR;.  }. 
1ba23 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1ba24 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69  K;.}../*.** Begi
1ba25 6e 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 61  n constructing a
1ba26 20 6e 65 77 20 74 61 62 6c 65 20 72 65 70 72 65   new table repre
1ba27 73 65 6e 74 61 74 69 6f 6e 20 69 6e 20 6d 65 6d  sentation in mem
1ba28 6f 72 79 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a  ory.  This is.**
1ba29 20 74 68 65 20 66 69 72 73 74 20 6f 66 20 73 65   the first of se
1ba2a 76 65 72 61 6c 20 61 63 74 69 6f 6e 20 72 6f 75  veral action rou
1ba2b 74 69 6e 65 73 20 74 68 61 74 20 67 65 74 20 63  tines that get c
1ba2c 61 6c 6c 65 64 20 69 6e 20 72 65 73 70 6f 6e 73  alled in respons
1ba2d 65 0a 2a 2a 20 74 6f 20 61 20 43 52 45 41 54 45  e.** to a CREATE
1ba2e 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
1ba2f 2e 20 20 49 6e 20 70 61 72 74 69 63 75 6c 61 72  .  In particular
1ba30 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  , this routine i
1ba31 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 61 66 74 65  s called.** afte
1ba32 72 20 73 65 65 69 6e 67 20 74 6f 6b 65 6e 73 20  r seeing tokens 
1ba33 22 43 52 45 41 54 45 22 20 61 6e 64 20 22 54 41  "CREATE" and "TA
1ba34 42 4c 45 22 20 61 6e 64 20 74 68 65 20 74 61 62  BLE" and the tab
1ba35 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 69 73 54  le name. The isT
1ba36 65 6d 70 0a 2a 2a 20 66 6c 61 67 20 69 73 20 74  emp.** flag is t
1ba37 72 75 65 20 69 66 20 74 68 65 20 74 61 62 6c 65  rue if the table
1ba38 20 73 68 6f 75 6c 64 20 62 65 20 73 74 6f 72 65   should be store
1ba39 64 20 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61  d in the auxilia
1ba3a 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66  ry database.** f
1ba3b 69 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20 69  ile instead of i
1ba3c 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  n the main datab
1ba3d 61 73 65 20 66 69 6c 65 2e 20 20 54 68 69 73 20  ase file.  This 
1ba3e 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 74 68 65 20  is normally the 
1ba3f 63 61 73 65 0a 2a 2a 20 77 68 65 6e 20 74 68 65  case.** when the
1ba40 20 22 54 45 4d 50 22 20 6f 72 20 22 54 45 4d 50   "TEMP" or "TEMP
1ba41 4f 52 41 52 59 22 20 6b 65 79 77 6f 72 64 20 6f  ORARY" keyword o
1ba42 63 63 75 72 73 20 69 6e 20 62 65 74 77 65 65 6e  ccurs in between
1ba43 0a 2a 2a 20 43 52 45 41 54 45 20 61 6e 64 20 54  .** CREATE and T
1ba44 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ABLE..**.** The 
1ba45 6e 65 77 20 74 61 62 6c 65 20 72 65 63 6f 72 64  new table record
1ba46 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   is initialized 
1ba47 61 6e 64 20 70 75 74 20 69 6e 20 70 50 61 72 73  and put in pPars
1ba48 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2e 0a 2a 2a  e->pNewTable..**
1ba49 20 41 73 20 6d 6f 72 65 20 6f 66 20 74 68 65 20   As more of the 
1ba4a 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
1ba4b 74 65 6d 65 6e 74 20 69 73 20 70 61 72 73 65 64  tement is parsed
1ba4c 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 61 63 74  , additional act
1ba4d 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20  ion.** routines 
1ba4e 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 74  will be called t
1ba4f 6f 20 61 64 64 20 6d 6f 72 65 20 69 6e 66 6f 72  o add more infor
1ba50 6d 61 74 69 6f 6e 20 74 6f 20 74 68 69 73 20 72  mation to this r
1ba51 65 63 6f 72 64 2e 0a 2a 2a 20 41 74 20 74 68 65  ecord..** At the
1ba52 20 65 6e 64 20 6f 66 20 74 68 65 20 43 52 45 41   end of the CREA
1ba53 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  TE TABLE stateme
1ba54 6e 74 2c 20 74 68 65 20 73 71 6c 69 74 65 33 45  nt, the sqlite3E
1ba55 6e 64 54 61 62 6c 65 28 29 20 72 6f 75 74 69 6e  ndTable() routin
1ba56 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 74  e.** is called t
1ba57 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 63  o complete the c
1ba58 6f 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 74  onstruction of t
1ba59 68 65 20 6e 65 77 20 74 61 62 6c 65 20 72 65 63  he new table rec
1ba5a 6f 72 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ord..*/.SQLITE_P
1ba5b 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1ba5c 74 65 33 53 74 61 72 74 54 61 62 6c 65 28 0a 20  te3StartTable(. 
1ba5d 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
1ba5e 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74    /* Parser cont
1ba5f 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ext */.  Token *
1ba60 70 4e 61 6d 65 31 2c 20 20 20 2f 2a 20 46 69 72  pName1,   /* Fir
1ba61 73 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 6e  st part of the n
1ba62 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
1ba63 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 54 6f   or view */.  To
1ba64 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 2f  ken *pName2,   /
1ba65 2a 20 53 65 63 6f 6e 64 20 70 61 72 74 20 6f 66  * Second part of
1ba66 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
1ba67 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 2a   table or view *
1ba68 2f 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 2c 20  /.  int isTemp, 
1ba69 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
1ba6a 74 68 69 73 20 69 73 20 61 20 54 45 4d 50 20 74  this is a TEMP t
1ba6b 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73  able */.  int is
1ba6c 56 69 65 77 2c 20 20 20 20 20 20 2f 2a 20 54 72  View,      /* Tr
1ba6d 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20  ue if this is a 
1ba6e 56 49 45 57 20 2a 2f 0a 20 20 69 6e 74 20 69 73  VIEW */.  int is
1ba6f 56 69 72 74 75 61 6c 2c 20 20 20 2f 2a 20 54 72  Virtual,   /* Tr
1ba70 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20  ue if this is a 
1ba71 56 49 52 54 55 41 4c 20 74 61 62 6c 65 20 2a 2f  VIRTUAL table */
1ba72 0a 20 20 69 6e 74 20 6e 6f 45 72 72 20 20 20 20  .  int noErr    
1ba73 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e      /* Do nothin
1ba74 67 20 69 66 20 74 61 62 6c 65 20 61 6c 72 65 61  g if table alrea
1ba75 64 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a  dy exists */.){.
1ba76 20 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b    Table *pTable;
1ba77 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d  .  char *zName =
1ba78 20 30 3b 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20   0; /* The name 
1ba79 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  of the new table
1ba7a 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
1ba7b 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
1ba7c 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74    Vdbe *v;.  int
1ba7d 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 2f 2a   iDb;         /*
1ba7e 20 44 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72   Database number
1ba7f 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 74   to create the t
1ba80 61 62 6c 65 20 69 6e 20 2a 2f 0a 20 20 54 6f 6b  able in */.  Tok
1ba81 65 6e 20 2a 70 4e 61 6d 65 3b 20 20 20 20 2f 2a  en *pName;    /*
1ba82 20 55 6e 71 75 61 6c 69 66 69 65 64 20 6e 61 6d   Unqualified nam
1ba83 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  e of the table t
1ba84 6f 20 63 72 65 61 74 65 20 2a 2f 0a 0a 20 20 2f  o create */..  /
1ba85 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76  * The table or v
1ba86 69 65 77 20 6e 61 6d 65 20 74 6f 20 63 72 65 61  iew name to crea
1ba87 74 65 20 69 73 20 70 61 73 73 65 64 20 74 6f 20  te is passed to 
1ba88 74 68 69 73 20 72 6f 75 74 69 6e 65 20 76 69 61  this routine via
1ba89 20 74 6f 6b 65 6e 73 0a 20 20 2a 2a 20 70 4e 61   tokens.  ** pNa
1ba8a 6d 65 31 20 61 6e 64 20 70 4e 61 6d 65 32 2e 20  me1 and pName2. 
1ba8b 49 66 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d  If the table nam
1ba8c 65 20 77 61 73 20 66 75 6c 6c 79 20 71 75 61 6c  e was fully qual
1ba8d 69 66 69 65 64 2c 20 66 6f 72 20 65 78 61 6d 70  ified, for examp
1ba8e 6c 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 52  le:.  **.  ** CR
1ba8f 45 41 54 45 20 54 41 42 4c 45 20 78 78 78 2e 79  EATE TABLE xxx.y
1ba90 79 79 20 28 2e 2e 2e 29 3b 0a 20 20 2a 2a 20 0a  yy (...);.  ** .
1ba91 20 20 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31    ** Then pName1
1ba92 20 69 73 20 73 65 74 20 74 6f 20 22 78 78 78 22   is set to "xxx"
1ba93 20 61 6e 64 20 70 4e 61 6d 65 32 20 22 79 79 79   and pName2 "yyy
1ba94 22 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20  ". On the other 
1ba95 68 61 6e 64 20 69 66 0a 20 20 2a 2a 20 74 68 65  hand if.  ** the
1ba96 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 6e   table name is n
1ba97 6f 74 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69  ot fully qualifi
1ba98 65 64 2c 20 69 2e 65 2e 3a 0a 20 20 2a 2a 0a 20  ed, i.e.:.  **. 
1ba99 20 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45   ** CREATE TABLE
1ba9a 20 79 79 79 28 2e 2e 2e 29 3b 0a 20 20 2a 2a 0a   yyy(...);.  **.
1ba9b 20 20 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31    ** Then pName1
1ba9c 20 69 73 20 73 65 74 20 74 6f 20 22 79 79 79 22   is set to "yyy"
1ba9d 20 61 6e 64 20 70 4e 61 6d 65 32 20 69 73 20 22   and pName2 is "
1ba9e 22 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  "..  **.  ** The
1ba9f 20 63 61 6c 6c 20 62 65 6c 6f 77 20 73 65 74 73   call below sets
1baa0 20 74 68 65 20 70 4e 61 6d 65 20 70 6f 69 6e 74   the pName point
1baa1 65 72 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74  er to point at t
1baa2 68 65 20 74 6f 6b 65 6e 20 28 70 4e 61 6d 65 31  he token (pName1
1baa3 20 6f 72 0a 20 20 2a 2a 20 70 4e 61 6d 65 32 29   or.  ** pName2)
1baa4 20 74 68 61 74 20 73 74 6f 72 65 73 20 74 68 65   that stores the
1baa5 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74 61 62   unqualified tab
1baa6 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 76 61 72  le name. The var
1baa7 69 61 62 6c 65 20 69 44 62 20 69 73 0a 20 20 2a  iable iDb is.  *
1baa8 2a 20 73 65 74 20 74 6f 20 74 68 65 20 69 6e 64  * set to the ind
1baa9 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ex of the databa
1baaa 73 65 20 74 68 61 74 20 74 68 65 20 74 61 62 6c  se that the tabl
1baab 65 20 6f 72 20 76 69 65 77 20 69 73 20 74 6f 20  e or view is to 
1baac 62 65 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20  be.  ** created 
1baad 69 6e 2e 0a 20 20 2a 2f 0a 20 20 69 44 62 20 3d  in..  */.  iDb =
1baae 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e   sqlite3TwoPartN
1baaf 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d  ame(pParse, pNam
1bab0 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e 61  e1, pName2, &pNa
1bab1 6d 65 29 3b 0a 20 20 69 66 28 20 69 44 62 3c 30  me);.  if( iDb<0
1bab2 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
1bab3 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26   !OMIT_TEMPDB &&
1bab4 20 69 73 54 65 6d 70 20 26 26 20 69 44 62 3e 31   isTemp && iDb>1
1bab5 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 63 72   ){.    /* If cr
1bab6 65 61 74 69 6e 67 20 61 20 74 65 6d 70 20 74 61  eating a temp ta
1bab7 62 6c 65 2c 20 74 68 65 20 6e 61 6d 65 20 6d 61  ble, the name ma
1bab8 79 20 6e 6f 74 20 62 65 20 71 75 61 6c 69 66 69  y not be qualifi
1bab9 65 64 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  ed */.    sqlite
1baba 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1babb 2c 20 22 74 65 6d 70 6f 72 61 72 79 20 74 61 62  , "temporary tab
1babc 6c 65 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20  le name must be 
1babd 75 6e 71 75 61 6c 69 66 69 65 64 22 29 3b 0a 20  unqualified");. 
1babe 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
1babf 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44   if( !OMIT_TEMPD
1bac0 42 20 26 26 20 69 73 54 65 6d 70 20 29 20 69 44  B && isTemp ) iD
1bac1 62 20 3d 20 31 3b 0a 0a 20 20 70 50 61 72 73 65  b = 1;..  pParse
1bac2 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 20 3d 20 2a  ->sNameToken = *
1bac3 70 4e 61 6d 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d  pName;.  zName =
1bac4 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
1bac5 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29  Token(db, pName)
1bac6 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30  ;.  if( zName==0
1bac7 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
1bac8 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69   SQLITE_OK!=sqli
1bac9 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61  te3CheckObjectNa
1baca 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65  me(pParse, zName
1bacb 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 62 65  ) ){.    goto be
1bacc 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b  gin_table_error;
1bacd 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 69  .  }.  if( db->i
1bace 6e 69 74 2e 69 44 62 3d 3d 31 20 29 20 69 73 54  nit.iDb==1 ) isT
1bacf 65 6d 70 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66  emp = 1;.#ifndef
1bad0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
1bad1 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 61 73 73  HORIZATION.  ass
1bad2 65 72 74 28 20 28 69 73 54 65 6d 70 20 26 20 31  ert( (isTemp & 1
1bad3 29 3d 3d 69 73 54 65 6d 70 20 29 3b 0a 20 20 7b  )==isTemp );.  {
1bad4 0a 20 20 20 20 69 6e 74 20 63 6f 64 65 3b 0a 20  .    int code;. 
1bad5 20 20 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64     char *zDb = d
1bad6 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
1bad7 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  e;.    if( sqlit
1bad8 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
1bad9 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45 52  se, SQLITE_INSER
1bada 54 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28  T, SCHEMA_TABLE(
1badb 69 73 54 65 6d 70 29 2c 20 30 2c 20 7a 44 62 29  isTemp), 0, zDb)
1badc 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62   ){.      goto b
1badd 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72  egin_table_error
1bade 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1badf 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20 20  isView ){.      
1bae0 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42  if( !OMIT_TEMPDB
1bae1 20 26 26 20 69 73 54 65 6d 70 20 29 7b 0a 20 20   && isTemp ){.  
1bae2 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c        code = SQL
1bae3 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f  ITE_CREATE_TEMP_
1bae4 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 65 6c 73  VIEW;.      }els
1bae5 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20  e{.        code 
1bae6 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f  = SQLITE_CREATE_
1bae7 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 0a 20 20  VIEW;.      }.  
1bae8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
1bae9 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20  f( !OMIT_TEMPDB 
1baea 26 26 20 69 73 54 65 6d 70 20 29 7b 0a 20 20 20  && isTemp ){.   
1baeb 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49       code = SQLI
1baec 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54  TE_CREATE_TEMP_T
1baed 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ABLE;.      }els
1baee 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20  e{.        code 
1baef 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f  = SQLITE_CREATE_
1baf0 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d 0a 20  TABLE;.      }. 
1baf1 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 69 73     }.    if( !is
1baf2 56 69 72 74 75 61 6c 20 26 26 20 73 71 6c 69 74  Virtual && sqlit
1baf3 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
1baf4 73 65 2c 20 63 6f 64 65 2c 20 7a 4e 61 6d 65 2c  se, code, zName,
1baf5 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20   0, zDb) ){.    
1baf6 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62    goto begin_tab
1baf7 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a  le_error;.    }.
1baf8 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
1baf9 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6e   Make sure the n
1bafa 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 20 64 6f  ew table name do
1bafb 65 73 20 6e 6f 74 20 63 6f 6c 6c 69 64 65 20 77  es not collide w
1bafc 69 74 68 20 61 6e 20 65 78 69 73 74 69 6e 67 0a  ith an existing.
1bafd 20 20 2a 2a 20 69 6e 64 65 78 20 6f 72 20 74 61    ** index or ta
1bafe 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20  ble name in the 
1baff 73 61 6d 65 20 64 61 74 61 62 61 73 65 2e 20 20  same database.  
1bb00 49 73 73 75 65 20 61 6e 20 65 72 72 6f 72 20 6d  Issue an error m
1bb01 65 73 73 61 67 65 20 69 66 0a 20 20 2a 2a 20 69  essage if.  ** i
1bb02 74 20 64 6f 65 73 2e 20 54 68 65 20 65 78 63 65  t does. The exce
1bb03 70 74 69 6f 6e 20 69 73 20 69 66 20 74 68 65 20  ption is if the 
1bb04 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20  statement being 
1bb05 70 61 72 73 65 64 20 77 61 73 20 70 61 73 73 65  parsed was passe
1bb06 64 0a 20 20 2a 2a 20 74 6f 20 61 6e 20 73 71 6c  d.  ** to an sql
1bb07 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61  ite3_declare_vta
1bb08 62 28 29 20 63 61 6c 6c 2e 20 49 6e 20 74 68 61  b() call. In tha
1bb09 74 20 63 61 73 65 20 6f 6e 6c 79 20 74 68 65 20  t case only the 
1bb0a 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 0a 20 20 2a  column names.  *
1bb0b 2a 20 61 6e 64 20 74 79 70 65 73 20 77 69 6c 6c  * and types will
1bb0c 20 62 65 20 75 73 65 64 2c 20 73 6f 20 74 68 65   be used, so the
1bb0d 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f  re is no need to
1bb0e 20 74 65 73 74 20 66 6f 72 20 6e 61 6d 65 73 70   test for namesp
1bb0f 61 63 65 0a 20 20 2a 2a 20 63 6f 6c 6c 69 73 69  ace.  ** collisi
1bb10 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ons..  */.  if( 
1bb11 21 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42  !IN_DECLARE_VTAB
1bb12 20 29 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49   ){.    if( SQLI
1bb13 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65  TE_OK!=sqlite3Re
1bb14 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
1bb15 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62   ){.      goto b
1bb16 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72  egin_table_error
1bb17 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 61 62  ;.    }.    pTab
1bb18 6c 65 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  le = sqlite3Find
1bb19 54 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c  Table(db, zName,
1bb1a 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
1bb1b 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70 54  ame);.    if( pT
1bb1c 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 69 66  able ){.      if
1bb1d 28 20 21 6e 6f 45 72 72 20 29 7b 0a 20 20 20 20  ( !noErr ){.    
1bb1e 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1bb1f 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62  Msg(pParse, "tab
1bb20 6c 65 20 25 54 20 61 6c 72 65 61 64 79 20 65 78  le %T already ex
1bb21 69 73 74 73 22 2c 20 70 4e 61 6d 65 29 3b 0a 20  ists", pName);. 
1bb22 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74       }.      got
1bb23 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72  o begin_table_er
1bb24 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ror;.    }.    i
1bb25 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e  f( sqlite3FindIn
1bb26 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 30  dex(db, zName, 0
1bb27 29 21 3d 30 20 26 26 20 28 69 44 62 3d 3d 30 20  )!=0 && (iDb==0 
1bb28 7c 7c 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73  || !db->init.bus
1bb29 79 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  y) ){.      sqli
1bb2a 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1bb2b 73 65 2c 20 22 74 68 65 72 65 20 69 73 20 61 6c  se, "there is al
1bb2c 72 65 61 64 79 20 61 6e 20 69 6e 64 65 78 20 6e  ready an index n
1bb2d 61 6d 65 64 20 25 73 22 2c 20 7a 4e 61 6d 65 29  amed %s", zName)
1bb2e 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65 67  ;.      goto beg
1bb2f 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a  in_table_error;.
1bb30 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 54 61      }.  }..  pTa
1bb31 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  ble = sqlite3DbM
1bb32 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
1bb33 7a 65 6f 66 28 54 61 62 6c 65 29 29 3b 0a 20 20  zeof(Table));.  
1bb34 69 66 28 20 70 54 61 62 6c 65 3d 3d 30 20 29 7b  if( pTable==0 ){
1bb35 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  .    db->mallocF
1bb36 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 70  ailed = 1;.    p
1bb37 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49  Parse->rc = SQLI
1bb38 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 70 50  TE_NOMEM;.    pP
1bb39 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20  arse->nErr++;.  
1bb3a 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62    goto begin_tab
1bb3b 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20  le_error;.  }.  
1bb3c 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 20 3d 20  pTable->zName = 
1bb3d 7a 4e 61 6d 65 3b 0a 20 20 70 54 61 62 6c 65 2d  zName;.  pTable-
1bb3e 3e 69 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20 70  >iPKey = -1;.  p
1bb3f 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 20 3d  Table->pSchema =
1bb40 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53   db->aDb[iDb].pS
1bb41 63 68 65 6d 61 3b 0a 20 20 70 54 61 62 6c 65 2d  chema;.  pTable-
1bb42 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 69 66 28  >nRef = 1;.  if(
1bb43 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
1bb44 6c 65 20 29 20 73 71 6c 69 74 65 33 44 65 6c 65  le ) sqlite3Dele
1bb45 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2d 3e  teTable(pParse->
1bb46 70 4e 65 77 54 61 62 6c 65 29 3b 0a 20 20 70 50  pNewTable);.  pP
1bb47 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20  arse->pNewTable 
1bb48 3d 20 70 54 61 62 6c 65 3b 0a 0a 20 20 2f 2a 20  = pTable;..  /* 
1bb49 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 6d  If this is the m
1bb4a 61 67 69 63 20 73 71 6c 69 74 65 5f 73 65 71 75  agic sqlite_sequ
1bb4b 65 6e 63 65 20 74 61 62 6c 65 20 75 73 65 64 20  ence table used 
1bb4c 62 79 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74  by autoincrement
1bb4d 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 72 65 63 6f  ,.  ** then reco
1bb4e 72 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rd a pointer to 
1bb4f 74 68 69 73 20 74 61 62 6c 65 20 69 6e 20 74 68  this table in th
1bb50 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
1bb51 73 74 72 75 63 74 75 72 65 0a 20 20 2a 2a 20 73  structure.  ** s
1bb52 6f 20 74 68 61 74 20 49 4e 53 45 52 54 20 63 61  o that INSERT ca
1bb53 6e 20 66 69 6e 64 20 74 68 65 20 74 61 62 6c 65  n find the table
1bb54 20 65 61 73 69 6c 79 2e 0a 20 20 2a 2f 0a 23 69   easily..  */.#i
1bb55 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1bb56 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a  T_AUTOINCREMENT.
1bb57 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e 6e    if( !pParse->n
1bb58 65 73 74 65 64 20 26 26 20 73 74 72 63 6d 70 28  ested && strcmp(
1bb59 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f 73  zName, "sqlite_s
1bb5a 65 71 75 65 6e 63 65 22 29 3d 3d 30 20 29 7b 0a  equence")==0 ){.
1bb5b 20 20 20 20 70 54 61 62 6c 65 2d 3e 70 53 63 68      pTable->pSch
1bb5c 65 6d 61 2d 3e 70 53 65 71 54 61 62 20 3d 20 70  ema->pSeqTab = p
1bb5d 54 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69  Table;.  }.#endi
1bb5e 66 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67 65  f..  /* Begin ge
1bb5f 6e 65 72 61 74 69 6e 67 20 74 68 65 20 63 6f 64  nerating the cod
1bb60 65 20 74 68 61 74 20 77 69 6c 6c 20 69 6e 73 65  e that will inse
1bb61 72 74 20 74 68 65 20 74 61 62 6c 65 20 72 65 63  rt the table rec
1bb62 6f 72 64 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68  ord into.  ** th
1bb63 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20  e SQLITE_MASTER 
1bb64 74 61 62 6c 65 2e 20 20 4e 6f 74 65 20 69 6e 20  table.  Note in 
1bb65 70 61 72 74 69 63 75 6c 61 72 20 74 68 61 74 20  particular that 
1bb66 77 65 20 6d 75 73 74 20 67 6f 20 61 68 65 61 64  we must go ahead
1bb67 0a 20 20 2a 2a 20 61 6e 64 20 61 6c 6c 6f 63 61  .  ** and alloca
1bb68 74 65 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75  te the record nu
1bb69 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74 61 62  mber for the tab
1bb6a 6c 65 20 65 6e 74 72 79 20 6e 6f 77 2e 20 20 42  le entry now.  B
1bb6b 65 66 6f 72 65 20 61 6e 79 0a 20 20 2a 2a 20 50  efore any.  ** P
1bb6c 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e  RIMARY KEY or UN
1bb6d 49 51 55 45 20 6b 65 79 77 6f 72 64 73 20 61 72  IQUE keywords ar
1bb6e 65 20 70 61 72 73 65 64 2e 20 20 54 68 6f 73 65  e parsed.  Those
1bb6f 20 6b 65 79 77 6f 72 64 73 20 77 69 6c 6c 20 63   keywords will c
1bb70 61 75 73 65 0a 20 20 2a 2a 20 69 6e 64 69 63 65  ause.  ** indice
1bb71 73 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20  s to be created 
1bb72 61 6e 64 20 74 68 65 20 74 61 62 6c 65 20 72 65  and the table re
1bb73 63 6f 72 64 20 6d 75 73 74 20 63 6f 6d 65 20 62  cord must come b
1bb74 65 66 6f 72 65 20 74 68 65 20 0a 20 20 2a 2a 20  efore the .  ** 
1bb75 69 6e 64 69 63 65 73 2e 20 20 48 65 6e 63 65 2c  indices.  Hence,
1bb76 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62   the record numb
1bb77 65 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  er for the table
1bb78 20 6d 75 73 74 20 62 65 20 61 6c 6c 6f 63 61 74   must be allocat
1bb79 65 64 0a 20 20 2a 2a 20 6e 6f 77 2e 0a 20 20 2a  ed.  ** now..  *
1bb7a 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69  /.  if( !db->ini
1bb7b 74 2e 62 75 73 79 20 26 26 20 28 76 20 3d 20 73  t.busy && (v = s
1bb7c 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
1bb7d 61 72 73 65 29 29 21 3d 30 20 29 7b 0a 20 20 20  arse))!=0 ){.   
1bb7e 20 69 6e 74 20 6a 31 3b 0a 20 20 20 20 69 6e 74   int j1;.    int
1bb7f 20 66 69 6c 65 46 6f 72 6d 61 74 3b 0a 20 20 20   fileFormat;.   
1bb80 20 69 6e 74 20 72 65 67 31 2c 20 72 65 67 32 2c   int reg1, reg2,
1bb81 20 72 65 67 33 3b 0a 20 20 20 20 73 71 6c 69 74   reg3;.    sqlit
1bb82 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
1bb83 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c  ation(pParse, 0,
1bb84 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20   iDb);..#ifndef 
1bb85 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1bb86 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 66 28  UALTABLE.    if(
1bb87 20 69 73 56 69 72 74 75 61 6c 20 29 7b 0a 20 20   isVirtual ){.  
1bb88 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1bb89 64 64 4f 70 30 28 76 2c 20 4f 50 5f 56 42 65 67  ddOp0(v, OP_VBeg
1bb8a 69 6e 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  in);.    }.#endi
1bb8b 66 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  f..    /* If the
1bb8c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 61 6e 64   file format and
1bb8d 20 65 6e 63 6f 64 69 6e 67 20 69 6e 20 74 68 65   encoding in the
1bb8e 20 64 61 74 61 62 61 73 65 20 68 61 76 65 20 6e   database have n
1bb8f 6f 74 20 62 65 65 6e 20 73 65 74 2c 20 0a 20 20  ot been set, .  
1bb90 20 20 2a 2a 20 73 65 74 20 74 68 65 6d 20 6e 6f    ** set them no
1bb91 77 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 65  w..    */.    re
1bb92 67 31 20 3d 20 70 50 61 72 73 65 2d 3e 72 65 67  g1 = pParse->reg
1bb93 52 6f 77 69 64 20 3d 20 2b 2b 70 50 61 72 73 65  Rowid = ++pParse
1bb94 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 72 65 67 32  ->nMem;.    reg2
1bb95 20 3d 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f   = pParse->regRo
1bb96 6f 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  ot = ++pParse->n
1bb97 4d 65 6d 3b 0a 20 20 20 20 72 65 67 33 20 3d 20  Mem;.    reg3 = 
1bb98 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
1bb99 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1bb9a 64 64 4f 70 33 28 76 2c 20 4f 50 5f 52 65 61 64  ddOp3(v, OP_Read
1bb9b 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 72 65 67  Cookie, iDb, reg
1bb9c 33 2c 20 31 29 3b 20 20 20 2f 2a 20 66 69 6c 65  3, 1);   /* file
1bb9d 5f 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 20 20 73  _format */.    s
1bb9e 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74  qlite3VdbeUsesBt
1bb9f 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 20 20 20  ree(v, iDb);.   
1bba0 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j1 = sqlite3Vdb
1bba1 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
1bba2 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 66 69 6c  , reg3);.    fil
1bba3 65 46 6f 72 6d 61 74 20 3d 20 28 64 62 2d 3e 66  eFormat = (db->f
1bba4 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4c 65  lags & SQLITE_Le
1bba5 67 61 63 79 46 69 6c 65 46 6d 74 29 21 3d 30 20  gacyFileFmt)!=0 
1bba6 3f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ?.              
1bba7 20 20 20 20 31 20 3a 20 53 51 4c 49 54 45 5f 4d      1 : SQLITE_M
1bba8 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 3b 0a  AX_FILE_FORMAT;.
1bba9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1bbaa 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
1bbab 67 65 72 2c 20 66 69 6c 65 46 6f 72 6d 61 74 2c  ger, fileFormat,
1bbac 20 72 65 67 33 29 3b 0a 20 20 20 20 73 71 6c 69   reg3);.    sqli
1bbad 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
1bbae 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69   OP_SetCookie, i
1bbaf 44 62 2c 20 31 2c 20 72 65 67 33 29 3b 0a 20 20  Db, 1, reg3);.  
1bbb0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1bbb1 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
1bbb2 72 2c 20 45 4e 43 28 64 62 29 2c 20 72 65 67 33  r, ENC(db), reg3
1bbb3 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1bbb4 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53  beAddOp3(v, OP_S
1bbb5 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 34  etCookie, iDb, 4
1bbb6 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 73 71 6c  , reg3);.    sql
1bbb7 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
1bbb8 28 76 2c 20 6a 31 29 3b 0a 0a 20 20 20 20 2f 2a  (v, j1);..    /*
1bbb9 20 54 68 69 73 20 6a 75 73 74 20 63 72 65 61 74   This just creat
1bbba 65 73 20 61 20 70 6c 61 63 65 2d 68 6f 6c 64 65  es a place-holde
1bbbb 72 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65 20  r record in the 
1bbbc 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
1bbbd 62 6c 65 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20  ble..    ** The 
1bbbe 72 65 63 6f 72 64 20 63 72 65 61 74 65 64 20 64  record created d
1bbbf 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20  oes not contain 
1bbc0 61 6e 79 74 68 69 6e 67 20 79 65 74 2e 20 20 49  anything yet.  I
1bbc1 74 20 77 69 6c 6c 20 62 65 20 72 65 70 6c 61 63  t will be replac
1bbc2 65 64 0a 20 20 20 20 2a 2a 20 62 79 20 74 68 65  ed.    ** by the
1bbc3 20 72 65 61 6c 20 65 6e 74 72 79 20 69 6e 20 63   real entry in c
1bbc4 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 61 74  ode generated at
1bbc5 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65   sqlite3EndTable
1bbc6 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ()..    **.    *
1bbc7 2a 20 54 68 65 20 72 6f 77 69 64 20 66 6f 72 20  * The rowid for 
1bbc8 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 69 73  the new entry is
1bbc9 20 6c 65 66 74 20 6f 6e 20 74 68 65 20 74 6f 70   left on the top
1bbca 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e 0a 20   of the stack.. 
1bbcb 20 20 20 2a 2a 20 54 68 65 20 72 6f 77 69 64 20     ** The rowid 
1bbcc 76 61 6c 75 65 20 69 73 20 6e 65 65 64 65 64 20  value is needed 
1bbcd 62 79 20 74 68 65 20 63 6f 64 65 20 74 68 61 74  by the code that
1bbce 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65   sqlite3EndTable
1bbcf 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 67 65 6e   will.    ** gen
1bbd0 65 72 61 74 65 2e 0a 20 20 20 20 2a 2f 0a 23 69  erate..    */.#i
1bbd1 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
1bbd2 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20  E_OMIT_VIEW) || 
1bbd3 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
1bbd4 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1bbd5 45 29 0a 20 20 20 20 69 66 28 20 69 73 56 69 65  E).    if( isVie
1bbd6 77 20 7c 7c 20 69 73 56 69 72 74 75 61 6c 20 29  w || isVirtual )
1bbd7 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
1bbd8 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1bbd9 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 32  Integer, 0, reg2
1bbda 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e  );.    }else.#en
1bbdb 64 69 66 0a 20 20 20 20 7b 0a 20 20 20 20 20 20  dif.    {.      
1bbdc 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1bbdd 32 28 76 2c 20 4f 50 5f 43 72 65 61 74 65 54 61  2(v, OP_CreateTa
1bbde 62 6c 65 2c 20 69 44 62 2c 20 72 65 67 32 29 3b  ble, iDb, reg2);
1bbdf 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
1bbe0 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61 62 6c  e3OpenMasterTabl
1bbe1 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a  e(pParse, iDb);.
1bbe2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1bbe3 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52  ddOp2(v, OP_NewR
1bbe4 6f 77 69 64 2c 20 30 2c 20 72 65 67 31 29 3b 0a  owid, 0, reg1);.
1bbe5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1bbe6 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
1bbe7 2c 20 30 2c 20 72 65 67 33 29 3b 0a 20 20 20 20  , 0, reg3);.    
1bbe8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1bbe9 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20  3(v, OP_Insert, 
1bbea 30 2c 20 72 65 67 33 2c 20 72 65 67 31 29 3b 0a  0, reg3, reg1);.
1bbeb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
1bbec 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41  hangeP5(v, OPFLA
1bbed 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 73  G_APPEND);.    s
1bbee 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30  qlite3VdbeAddOp0
1bbef 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 29 3b 0a 20  (v, OP_Close);. 
1bbf0 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 20   }..  /* Normal 
1bbf1 28 6e 6f 6e 2d 65 72 72 6f 72 29 20 72 65 74 75  (non-error) retu
1bbf2 72 6e 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 3b  rn. */.  return;
1bbf3 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72  ..  /* If an err
1bbf4 6f 72 20 6f 63 63 75 72 73 2c 20 77 65 20 6a 75  or occurs, we ju
1bbf5 6d 70 20 68 65 72 65 20 2a 2f 0a 62 65 67 69 6e  mp here */.begin
1bbf6 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3a 0a 20 20  _table_error:.  
1bbf7 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4e 61  sqlite3_free(zNa
1bbf8 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d  me);.  return;.}
1bbf9 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63  ../*.** This mac
1bbfa 72 6f 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f  ro is used to co
1bbfb 6d 70 61 72 65 20 74 77 6f 20 73 74 72 69 6e 67  mpare two string
1bbfc 73 20 69 6e 20 61 20 63 61 73 65 2d 69 6e 73 65  s in a case-inse
1bbfd 6e 73 69 74 69 76 65 20 6d 61 6e 6e 65 72 2e 0a  nsitive manner..
1bbfe 2a 2a 20 49 74 20 69 73 20 73 6c 69 67 68 74 6c  ** It is slightl
1bbff 79 20 66 61 73 74 65 72 20 74 68 61 6e 20 63 61  y faster than ca
1bc00 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 53 74 72  lling sqlite3Str
1bc01 49 43 6d 70 28 29 20 64 69 72 65 63 74 6c 79 2c  ICmp() directly,
1bc02 20 62 75 74 0a 2a 2a 20 70 72 6f 64 75 63 65 73   but.** produces
1bc03 20 6c 61 72 67 65 72 20 63 6f 64 65 2e 0a 2a 2a   larger code..**
1bc04 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a 20 54 68 69  .** WARNING: Thi
1bc05 73 20 6d 61 63 72 6f 20 69 73 20 6e 6f 74 20 63  s macro is not c
1bc06 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 68 20 74  ompatible with t
1bc07 68 65 20 73 74 72 63 6d 70 28 29 20 66 61 6d 69  he strcmp() fami
1bc08 6c 79 2e 20 49 74 0a 2a 2a 20 72 65 74 75 72 6e  ly. It.** return
1bc09 73 20 74 72 75 65 20 69 66 20 74 68 65 20 74 77  s true if the tw
1bc0a 6f 20 73 74 72 69 6e 67 73 20 61 72 65 20 65 71  o strings are eq
1bc0b 75 61 6c 2c 20 6f 74 68 65 72 77 69 73 65 20 66  ual, otherwise f
1bc0c 61 6c 73 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  alse..*/.#define
1bc0d 20 53 54 52 49 43 4d 50 28 78 2c 20 79 29 20 28   STRICMP(x, y) (
1bc0e 5c 0a 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f  \.sqlite3UpperTo
1bc0f 4c 6f 77 65 72 5b 2a 28 75 6e 73 69 67 6e 65 64  Lower[*(unsigned
1bc10 20 63 68 61 72 20 2a 29 28 78 29 5d 3d 3d 20 20   char *)(x)]==  
1bc11 20 5c 0a 73 71 6c 69 74 65 33 55 70 70 65 72 54   \.sqlite3UpperT
1bc12 6f 4c 6f 77 65 72 5b 2a 28 75 6e 73 69 67 6e 65  oLower[*(unsigne
1bc13 64 20 63 68 61 72 20 2a 29 28 79 29 5d 20 20 20  d char *)(y)]   
1bc14 20 20 5c 0a 26 26 20 73 71 6c 69 74 65 33 53 74    \.&& sqlite3St
1bc15 72 49 43 6d 70 28 28 78 29 2b 31 2c 28 79 29 2b  rICmp((x)+1,(y)+
1bc16 31 29 3d 3d 30 20 29 0a 0a 2f 2a 0a 2a 2a 20 41  1)==0 )../*.** A
1bc17 64 64 20 61 20 6e 65 77 20 63 6f 6c 75 6d 6e 20  dd a new column 
1bc18 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75 72  to the table cur
1bc19 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 63 6f 6e  rently being con
1bc1a 73 74 72 75 63 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  structed..**.** 
1bc1b 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73  The parser calls
1bc1c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e   this routine on
1bc1d 63 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75  ce for each colu
1bc1e 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 0a 2a  mn declaration.*
1bc1f 2a 20 69 6e 20 61 20 43 52 45 41 54 45 20 54 41  * in a CREATE TA
1bc20 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  BLE statement.  
1bc21 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c  sqlite3StartTabl
1bc22 65 28 29 20 67 65 74 73 20 63 61 6c 6c 65 64 0a  e() gets called.
1bc23 2a 2a 20 66 69 72 73 74 20 74 6f 20 67 65 74 20  ** first to get 
1bc24 74 68 69 6e 67 73 20 67 6f 69 6e 67 2e 20 20 54  things going.  T
1bc25 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
1bc26 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 65   is called for e
1bc27 61 63 68 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a 2a  ach.** column..*
1bc28 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1bc29 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64   void sqlite3Add
1bc2a 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 70 50  Column(Parse *pP
1bc2b 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61  arse, Token *pNa
1bc2c 6d 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b  me){.  Table *p;
1bc2d 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 68 61 72  .  int i;.  char
1bc2e 20 2a 7a 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70   *z;.  Column *p
1bc2f 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  Col;.  sqlite3 *
1bc30 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
1bc31 0a 20 20 69 66 28 20 28 70 20 3d 20 70 50 61 72  .  if( (p = pPar
1bc32 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d  se->pNewTable)==
1bc33 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66 20  0 ) return;.#if 
1bc34 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d  SQLITE_MAX_COLUM
1bc35 4e 0a 20 20 69 66 28 20 70 2d 3e 6e 43 6f 6c 2b  N.  if( p->nCol+
1bc36 31 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  1>db->aLimit[SQL
1bc37 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e  ITE_LIMIT_COLUMN
1bc38 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ] ){.    sqlite3
1bc39 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
1bc3a 20 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d   "too many colum
1bc3b 6e 73 20 6f 6e 20 25 73 22 2c 20 70 2d 3e 7a 4e  ns on %s", p->zN
1bc3c 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ame);.    return
1bc3d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7a  ;.  }.#endif.  z
1bc3e 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
1bc3f 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d 3e  omToken(pParse->
1bc40 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66  db, pName);.  if
1bc41 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( z==0 ) return;
1bc42 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d  .  for(i=0; i<p-
1bc43 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nCol; i++){.   
1bc44 20 69 66 28 20 53 54 52 49 43 4d 50 28 7a 2c 20   if( STRICMP(z, 
1bc45 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65  p->aCol[i].zName
1bc46 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) ){.      sqlit
1bc47 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1bc48 65 2c 20 22 64 75 70 6c 69 63 61 74 65 20 63 6f  e, "duplicate co
1bc49 6c 75 6d 6e 20 6e 61 6d 65 3a 20 25 73 22 2c 20  lumn name: %s", 
1bc4a 7a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  z);.      sqlite
1bc4b 33 5f 66 72 65 65 28 7a 29 3b 0a 20 20 20 20 20  3_free(z);.     
1bc4c 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
1bc4d 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e 6e 43 6f   }.  if( (p->nCo
1bc4e 6c 20 26 20 30 78 37 29 3d 3d 30 20 29 7b 0a 20  l & 0x7)==0 ){. 
1bc4f 20 20 20 43 6f 6c 75 6d 6e 20 2a 61 4e 65 77 3b     Column *aNew;
1bc50 0a 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c 69  .    aNew = sqli
1bc51 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 70 50 61  te3DbRealloc(pPa
1bc52 72 73 65 2d 3e 64 62 2c 70 2d 3e 61 43 6f 6c 2c  rse->db,p->aCol,
1bc53 28 70 2d 3e 6e 43 6f 6c 2b 38 29 2a 73 69 7a 65  (p->nCol+8)*size
1bc54 6f 66 28 70 2d 3e 61 43 6f 6c 5b 30 5d 29 29 3b  of(p->aCol[0]));
1bc55 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30  .    if( aNew==0
1bc56 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1bc57 33 5f 66 72 65 65 28 7a 29 3b 0a 20 20 20 20 20  3_free(z);.     
1bc58 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
1bc59 20 20 20 70 2d 3e 61 43 6f 6c 20 3d 20 61 4e 65     p->aCol = aNe
1bc5a 77 3b 0a 20 20 7d 0a 20 20 70 43 6f 6c 20 3d 20  w;.  }.  pCol = 
1bc5b 26 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c  &p->aCol[p->nCol
1bc5c 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 70 43 6f 6c  ];.  memset(pCol
1bc5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 61  , 0, sizeof(p->a
1bc5e 43 6f 6c 5b 30 5d 29 29 3b 0a 20 20 70 43 6f 6c  Col[0]));.  pCol
1bc5f 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 3b 0a 20 0a 20  ->zName = z;. . 
1bc60 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
1bc61 6e 6f 20 74 79 70 65 20 73 70 65 63 69 66 69 65  no type specifie
1bc62 64 2c 20 63 6f 6c 75 6d 6e 73 20 68 61 76 65 20  d, columns have 
1bc63 74 68 65 20 64 65 66 61 75 6c 74 20 61 66 66 69  the default affi
1bc64 6e 69 74 79 0a 20 20 2a 2a 20 27 4e 4f 4e 45 27  nity.  ** 'NONE'
1bc65 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20  . If there is a 
1bc66 74 79 70 65 20 73 70 65 63 69 66 69 65 64 2c 20  type specified, 
1bc67 74 68 65 6e 20 73 71 6c 69 74 65 33 41 64 64 43  then sqlite3AddC
1bc68 6f 6c 75 6d 6e 54 79 70 65 28 29 20 77 69 6c 6c  olumnType() will
1bc69 0a 20 20 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20  .  ** be called 
1bc6a 6e 65 78 74 20 74 6f 20 73 65 74 20 70 43 6f 6c  next to set pCol
1bc6b 2d 3e 61 66 66 69 6e 69 74 79 20 63 6f 72 72 65  ->affinity corre
1bc6c 63 74 6c 79 2e 0a 20 20 2a 2f 0a 20 20 70 43 6f  ctly..  */.  pCo
1bc6d 6c 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51  l->affinity = SQ
1bc6e 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20  LITE_AFF_NONE;. 
1bc6f 20 70 2d 3e 6e 43 6f 6c 2b 2b 3b 0a 7d 0a 0a 2f   p->nCol++;.}../
1bc70 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1bc71 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74  e is called by t
1bc72 68 65 20 70 61 72 73 65 72 20 77 68 69 6c 65 20  he parser while 
1bc73 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
1bc74 0a 2a 2a 20 70 61 72 73 69 6e 67 20 61 20 43 52  .** parsing a CR
1bc75 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
1bc76 6d 65 6e 74 2e 20 20 41 20 22 4e 4f 54 20 4e 55  ment.  A "NOT NU
1bc77 4c 4c 22 20 63 6f 6e 73 74 72 61 69 6e 74 20 68  LL" constraint h
1bc78 61 73 0a 2a 2a 20 62 65 65 6e 20 73 65 65 6e 20  as.** been seen 
1bc79 6f 6e 20 61 20 63 6f 6c 75 6d 6e 2e 20 20 54 68  on a column.  Th
1bc7a 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20  is routine sets 
1bc7b 74 68 65 20 6e 6f 74 4e 75 6c 6c 20 66 6c 61 67  the notNull flag
1bc7c 20 6f 6e 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d   on.** the colum
1bc7d 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65  n currently unde
1bc7e 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  r construction..
1bc7f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1bc80 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64  E void sqlite3Ad
1bc81 64 4e 6f 74 4e 75 6c 6c 28 50 61 72 73 65 20 2a  dNotNull(Parse *
1bc82 70 50 61 72 73 65 2c 20 69 6e 74 20 6f 6e 45 72  pParse, int onEr
1bc83 72 6f 72 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  ror){.  Table *p
1bc84 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28  ;.  int i;.  if(
1bc85 20 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e   (p = pParse->pN
1bc86 65 77 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65  ewTable)==0 ) re
1bc87 74 75 72 6e 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e  turn;.  i = p->n
1bc88 43 6f 6c 2d 31 3b 0a 20 20 69 66 28 20 69 3e 3d  Col-1;.  if( i>=
1bc89 30 20 29 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e  0 ) p->aCol[i].n
1bc8a 6f 74 4e 75 6c 6c 20 3d 20 6f 6e 45 72 72 6f 72  otNull = onError
1bc8b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 63 61 6e 20  ;.}../*.** Scan 
1bc8c 74 68 65 20 63 6f 6c 75 6d 6e 20 74 79 70 65 20  the column type 
1bc8d 6e 61 6d 65 20 7a 54 79 70 65 20 28 6c 65 6e 67  name zType (leng
1bc8e 74 68 20 6e 54 79 70 65 29 20 61 6e 64 20 72 65  th nType) and re
1bc8f 74 75 72 6e 20 74 68 65 0a 2a 2a 20 61 73 73 6f  turn the.** asso
1bc90 63 69 61 74 65 64 20 61 66 66 69 6e 69 74 79 20  ciated affinity 
1bc91 74 79 70 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  type..**.** This
1bc92 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 61 20   routine does a 
1bc93 63 61 73 65 2d 69 6e 64 65 70 65 6e 64 65 6e 74  case-independent
1bc94 20 73 65 61 72 63 68 20 6f 66 20 7a 54 79 70 65   search of zType
1bc95 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 73 75 62   for the .** sub
1bc96 73 74 72 69 6e 67 73 20 69 6e 20 74 68 65 20 66  strings in the f
1bc97 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 2e 20  ollowing table. 
1bc98 49 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 75  If one of the su
1bc99 62 73 74 72 69 6e 67 73 20 69 73 0a 2a 2a 20 66  bstrings is.** f
1bc9a 6f 75 6e 64 2c 20 74 68 65 20 63 6f 72 72 65 73  ound, the corres
1bc9b 70 6f 6e 64 69 6e 67 20 61 66 66 69 6e 69 74 79  ponding affinity
1bc9c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66   is returned. If
1bc9d 20 7a 54 79 70 65 20 63 6f 6e 74 61 69 6e 73 0a   zType contains.
1bc9e 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  ** more than one
1bc9f 20 6f 66 20 74 68 65 20 73 75 62 73 74 72 69 6e   of the substrin
1bca0 67 73 2c 20 65 6e 74 72 69 65 73 20 74 6f 77 61  gs, entries towa
1bca1 72 64 20 74 68 65 20 74 6f 70 20 6f 66 20 0a 2a  rd the top of .*
1bca2 2a 20 74 68 65 20 74 61 62 6c 65 20 74 61 6b 65  * the table take
1bca3 20 70 72 69 6f 72 69 74 79 2e 20 46 6f 72 20 65   priority. For e
1bca4 78 61 6d 70 6c 65 2c 20 69 66 20 7a 54 79 70 65  xample, if zType
1bca5 20 69 73 20 27 42 4c 4f 42 49 4e 54 27 2c 20 0a   is 'BLOBINT', .
1bca6 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e  ** SQLITE_AFF_IN
1bca7 54 45 47 45 52 20 69 73 20 72 65 74 75 72 6e 65  TEGER is returne
1bca8 64 2e 0a 2a 2a 0a 2a 2a 20 53 75 62 73 74 72 69  d..**.** Substri
1bca9 6e 67 20 20 20 20 20 7c 20 41 66 66 69 6e 69 74  ng     | Affinit
1bcaa 79 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  y.** -----------
1bcab 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1bcac 2d 2d 2d 2d 2d 0a 2a 2a 20 27 49 4e 54 27 20 20  -----.** 'INT'  
1bcad 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f         | SQLITE_
1bcae 41 46 46 5f 49 4e 54 45 47 45 52 0a 2a 2a 20 27  AFF_INTEGER.** '
1bcaf 43 48 41 52 27 20 20 20 20 20 20 20 20 7c 20 53  CHAR'        | S
1bcb0 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 0a 2a  QLITE_AFF_TEXT.*
1bcb1 2a 20 27 43 4c 4f 42 27 20 20 20 20 20 20 20 20  * 'CLOB'        
1bcb2 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  | SQLITE_AFF_TEX
1bcb3 54 0a 2a 2a 20 27 54 45 58 54 27 20 20 20 20 20  T.** 'TEXT'     
1bcb4 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f     | SQLITE_AFF_
1bcb5 54 45 58 54 0a 2a 2a 20 27 42 4c 4f 42 27 20 20  TEXT.** 'BLOB'  
1bcb6 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41        | SQLITE_A
1bcb7 46 46 5f 4e 4f 4e 45 0a 2a 2a 20 27 52 45 41 4c  FF_NONE.** 'REAL
1bcb8 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54  '        | SQLIT
1bcb9 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a 20 27 46  E_AFF_REAL.** 'F
1bcba 4c 4f 41 27 20 20 20 20 20 20 20 20 7c 20 53 51  LOA'        | SQ
1bcbb 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a  LITE_AFF_REAL.**
1bcbc 20 27 44 4f 55 42 27 20 20 20 20 20 20 20 20 7c   'DOUB'        |
1bcbd 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c   SQLITE_AFF_REAL
1bcbe 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 6e 65 20 6f  .**.** If none o
1bcbf 66 20 74 68 65 20 73 75 62 73 74 72 69 6e 67 73  f the substrings
1bcc0 20 69 6e 20 74 68 65 20 61 62 6f 76 65 20 74 61   in the above ta
1bcc1 62 6c 65 20 61 72 65 20 66 6f 75 6e 64 2c 0a 2a  ble are found,.*
1bcc2 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d  * SQLITE_AFF_NUM
1bcc3 45 52 49 43 20 69 73 20 72 65 74 75 72 6e 65 64  ERIC is returned
1bcc4 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1bcc5 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33  ATE char sqlite3
1bcc6 41 66 66 69 6e 69 74 79 54 79 70 65 28 63 6f 6e  AffinityType(con
1bcc7 73 74 20 54 6f 6b 65 6e 20 2a 70 54 79 70 65 29  st Token *pType)
1bcc8 7b 0a 20 20 75 33 32 20 68 20 3d 20 30 3b 0a 20  {.  u32 h = 0;. 
1bcc9 20 63 68 61 72 20 61 66 66 20 3d 20 53 51 4c 49   char aff = SQLI
1bcca 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 3b 0a  TE_AFF_NUMERIC;.
1bccb 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
1bccc 20 63 68 61 72 20 2a 7a 49 6e 20 3d 20 70 54 79   char *zIn = pTy
1bccd 70 65 2d 3e 7a 3b 0a 20 20 63 6f 6e 73 74 20 75  pe->z;.  const u
1bcce 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 45  nsigned char *zE
1bccf 6e 64 20 3d 20 26 70 54 79 70 65 2d 3e 7a 5b 70  nd = &pType->z[p
1bcd0 54 79 70 65 2d 3e 6e 5d 3b 0a 0a 20 20 77 68 69  Type->n];..  whi
1bcd1 6c 65 28 20 7a 49 6e 21 3d 7a 45 6e 64 20 29 7b  le( zIn!=zEnd ){
1bcd2 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c 38 29 20  .    h = (h<<8) 
1bcd3 2b 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f  + sqlite3UpperTo
1bcd4 4c 6f 77 65 72 5b 2a 7a 49 6e 5d 3b 0a 20 20 20  Lower[*zIn];.   
1bcd5 20 7a 49 6e 2b 2b 3b 0a 20 20 20 20 69 66 28 20   zIn++;.    if( 
1bcd6 68 3d 3d 28 28 27 63 27 3c 3c 32 34 29 2b 28 27  h==(('c'<<24)+('
1bcd7 68 27 3c 3c 31 36 29 2b 28 27 61 27 3c 3c 38 29  h'<<16)+('a'<<8)
1bcd8 2b 27 72 27 29 20 29 7b 20 20 20 20 20 20 20 20  +'r') ){        
1bcd9 20 20 20 20 20 2f 2a 20 43 48 41 52 20 2a 2f 0a       /* CHAR */.
1bcda 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49        aff = SQLI
1bcdb 54 45 5f 41 46 46 5f 54 45 58 54 3b 20 0a 20 20  TE_AFF_TEXT; .  
1bcdc 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28    }else if( h==(
1bcdd 28 27 63 27 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c  ('c'<<24)+('l'<<
1bcde 31 36 29 2b 28 27 6f 27 3c 3c 38 29 2b 27 62 27  16)+('o'<<8)+'b'
1bcdf 29 20 29 7b 20 20 20 20 20 20 20 2f 2a 20 43 4c  ) ){       /* CL
1bce0 4f 42 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 20  OB */.      aff 
1bce1 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  = SQLITE_AFF_TEX
1bce2 54 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  T;.    }else if(
1bce3 20 68 3d 3d 28 28 27 74 27 3c 3c 32 34 29 2b 28   h==(('t'<<24)+(
1bce4 27 65 27 3c 3c 31 36 29 2b 28 27 78 27 3c 3c 38  'e'<<16)+('x'<<8
1bce5 29 2b 27 74 27 29 20 29 7b 20 20 20 20 20 20 20  )+'t') ){       
1bce6 2f 2a 20 54 45 58 54 20 2a 2f 0a 20 20 20 20 20  /* TEXT */.     
1bce7 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46   aff = SQLITE_AF
1bce8 46 5f 54 45 58 54 3b 0a 20 20 20 20 7d 65 6c 73  F_TEXT;.    }els
1bce9 65 20 69 66 28 20 68 3d 3d 28 28 27 62 27 3c 3c  e if( h==(('b'<<
1bcea 32 34 29 2b 28 27 6c 27 3c 3c 31 36 29 2b 28 27  24)+('l'<<16)+('
1bceb 6f 27 3c 3c 38 29 2b 27 62 27 29 20 20 20 20 20  o'<<8)+'b')     
1bcec 20 20 20 20 20 2f 2a 20 42 4c 4f 42 20 2a 2f 0a       /* BLOB */.
1bced 20 20 20 20 20 20 20 20 26 26 20 28 61 66 66 3d          && (aff=
1bcee 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45  =SQLITE_AFF_NUME
1bcef 52 49 43 20 7c 7c 20 61 66 66 3d 3d 53 51 4c 49  RIC || aff==SQLI
1bcf0 54 45 5f 41 46 46 5f 52 45 41 4c 29 20 29 7b 0a  TE_AFF_REAL) ){.
1bcf1 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49        aff = SQLI
1bcf2 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 23 69 66  TE_AFF_NONE;.#if
1bcf3 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1bcf4 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
1bcf5 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d      }else if( h=
1bcf6 3d 28 28 27 72 27 3c 3c 32 34 29 2b 28 27 65 27  =(('r'<<24)+('e'
1bcf7 3c 3c 31 36 29 2b 28 27 61 27 3c 3c 38 29 2b 27  <<16)+('a'<<8)+'
1bcf8 6c 27 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20  l')          /* 
1bcf9 52 45 41 4c 20 2a 2f 0a 20 20 20 20 20 20 20 20  REAL */.        
1bcfa 26 26 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41  && aff==SQLITE_A
1bcfb 46 46 5f 4e 55 4d 45 52 49 43 20 29 7b 0a 20 20  FF_NUMERIC ){.  
1bcfc 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45      aff = SQLITE
1bcfd 5f 41 46 46 5f 52 45 41 4c 3b 0a 20 20 20 20 7d  _AFF_REAL;.    }
1bcfe 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 66  else if( h==(('f
1bcff 27 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 36 29  '<<24)+('l'<<16)
1bd00 2b 28 27 6f 27 3c 3c 38 29 2b 27 61 27 29 20 20  +('o'<<8)+'a')  
1bd01 20 20 20 20 20 20 20 20 2f 2a 20 46 4c 4f 41 20          /* FLOA 
1bd02 2a 2f 0a 20 20 20 20 20 20 20 20 26 26 20 61 66  */.        && af
1bd03 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55  f==SQLITE_AFF_NU
1bd04 4d 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20 61  MERIC ){.      a
1bd05 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  ff = SQLITE_AFF_
1bd06 52 45 41 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 20  REAL;.    }else 
1bd07 69 66 28 20 68 3d 3d 28 28 27 64 27 3c 3c 32 34  if( h==(('d'<<24
1bd08 29 2b 28 27 6f 27 3c 3c 31 36 29 2b 28 27 75 27  )+('o'<<16)+('u'
1bd09 3c 3c 38 29 2b 27 62 27 29 20 20 20 20 20 20 20  <<8)+'b')       
1bd0a 20 20 20 2f 2a 20 44 4f 55 42 20 2a 2f 0a 20 20     /* DOUB */.  
1bd0b 20 20 20 20 20 20 26 26 20 61 66 66 3d 3d 53 51        && aff==SQ
1bd0c 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43  LITE_AFF_NUMERIC
1bd0d 20 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20   ){.      aff = 
1bd0e 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3b  SQLITE_AFF_REAL;
1bd0f 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73  .#endif.    }els
1bd10 65 20 69 66 28 20 28 68 26 30 78 30 30 46 46 46  e if( (h&0x00FFF
1bd11 46 46 46 29 3d 3d 28 28 27 69 27 3c 3c 31 36 29  FFF)==(('i'<<16)
1bd12 2b 28 27 6e 27 3c 3c 38 29 2b 27 74 27 29 20 29  +('n'<<8)+'t') )
1bd13 7b 20 20 20 20 2f 2a 20 49 4e 54 20 2a 2f 0a 20  {    /* INT */. 
1bd14 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54       aff = SQLIT
1bd15 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20  E_AFF_INTEGER;. 
1bd16 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1bd17 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  }.  }..  return 
1bd18 61 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  aff;.}../*.** Th
1bd19 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
1bd1a 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
1bd1b 65 72 20 77 68 69 6c 65 20 69 6e 20 74 68 65 20  er while in the 
1bd1c 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a 20 70 61 72  middle of.** par
1bd1d 73 69 6e 67 20 61 20 43 52 45 41 54 45 20 54 41  sing a CREATE TA
1bd1e 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  BLE statement.  
1bd1f 54 68 65 20 70 46 69 72 73 74 20 74 6f 6b 65 6e  The pFirst token
1bd20 20 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a   is the first.**
1bd21 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 73 65   token in the se
1bd22 71 75 65 6e 63 65 20 6f 66 20 74 6f 6b 65 6e 73  quence of tokens
1bd23 20 74 68 61 74 20 64 65 73 63 72 69 62 65 20 74   that describe t
1bd24 68 65 20 74 79 70 65 20 6f 66 20 74 68 65 0a 2a  he type of the.*
1bd25 2a 20 63 6f 6c 75 6d 6e 20 63 75 72 72 65 6e 74  * column current
1bd26 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75  ly under constru
1bd27 63 74 69 6f 6e 2e 20 20 20 70 4c 61 73 74 20 69  ction.   pLast i
1bd28 73 20 74 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e  s the last token
1bd29 0a 2a 2a 20 69 6e 20 74 68 65 20 73 65 71 75 65  .** in the seque
1bd2a 6e 63 65 2e 20 20 55 73 65 20 74 68 69 73 20 69  nce.  Use this i
1bd2b 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 63 6f  nformation to co
1bd2c 6e 73 74 72 75 63 74 20 61 20 73 74 72 69 6e 67  nstruct a string
1bd2d 0a 2a 2a 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  .** that contain
1bd2e 73 20 74 68 65 20 74 79 70 65 6e 61 6d 65 20 6f  s the typename o
1bd2f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 6e 64  f the column and
1bd30 20 73 74 6f 72 65 20 74 68 61 74 20 73 74 72 69   store that stri
1bd31 6e 67 0a 2a 2a 20 69 6e 20 7a 54 79 70 65 2e 0a  ng.** in zType..
1bd32 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */ .SQLITE_PRIVA
1bd33 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
1bd34 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 50 61 72  ddColumnType(Par
1bd35 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65  se *pParse, Toke
1bd36 6e 20 2a 70 54 79 70 65 29 7b 0a 20 20 54 61 62  n *pType){.  Tab
1bd37 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a  le *p;.  int i;.
1bd38 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a    Column *pCol;.
1bd39 0a 20 20 69 66 28 20 28 70 20 3d 20 70 50 61 72  .  if( (p = pPar
1bd3a 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d  se->pNewTable)==
1bd3b 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 20  0 ) return;.  i 
1bd3c 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 69  = p->nCol-1;.  i
1bd3d 66 28 20 69 3c 30 20 29 20 72 65 74 75 72 6e 3b  f( i<0 ) return;
1bd3e 0a 20 20 70 43 6f 6c 20 3d 20 26 70 2d 3e 61 43  .  pCol = &p->aC
1bd3f 6f 6c 5b 69 5d 3b 0a 20 20 73 71 6c 69 74 65 33  ol[i];.  sqlite3
1bd40 5f 66 72 65 65 28 70 43 6f 6c 2d 3e 7a 54 79 70  _free(pCol->zTyp
1bd41 65 29 3b 0a 20 20 70 43 6f 6c 2d 3e 7a 54 79 70  e);.  pCol->zTyp
1bd42 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  e = sqlite3NameF
1bd43 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d  romToken(pParse-
1bd44 3e 64 62 2c 20 70 54 79 70 65 29 3b 0a 20 20 70  >db, pType);.  p
1bd45 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20  Col->affinity = 
1bd46 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54  sqlite3AffinityT
1bd47 79 70 65 28 70 54 79 70 65 29 3b 0a 7d 0a 0a 2f  ype(pType);.}../
1bd48 2a 0a 2a 2a 20 54 68 65 20 65 78 70 72 65 73 73  *.** The express
1bd49 69 6f 6e 20 69 73 20 74 68 65 20 64 65 66 61 75  ion is the defau
1bd4a 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  lt value for the
1bd4b 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61   most recently a
1bd4c 64 64 65 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f  dded column.** o
1bd4d 66 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 72  f the table curr
1bd4e 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73  ently under cons
1bd4f 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  truction..**.** 
1bd50 44 65 66 61 75 6c 74 20 76 61 6c 75 65 20 65 78  Default value ex
1bd51 70 72 65 73 73 69 6f 6e 73 20 6d 75 73 74 20 62  pressions must b
1bd52 65 20 63 6f 6e 73 74 61 6e 74 2e 20 20 52 61 69  e constant.  Rai
1bd53 73 65 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20  se an exception 
1bd54 69 66 20 74 68 69 73 0a 2a 2a 20 69 73 20 6e 6f  if this.** is no
1bd55 74 20 74 68 65 20 63 61 73 65 2e 0a 2a 2a 0a 2a  t the case..**.*
1bd56 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
1bd57 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  s called by the 
1bd58 70 61 72 73 65 72 20 77 68 69 6c 65 20 69 6e 20  parser while in 
1bd59 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a  the middle of.**
1bd5a 20 70 61 72 73 69 6e 67 20 61 20 43 52 45 41 54   parsing a CREAT
1bd5b 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  E TABLE statemen
1bd5c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
1bd5d 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1bd5e 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65  3AddDefaultValue
1bd5f 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1bd60 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20  Expr *pExpr){.  
1bd61 54 61 62 6c 65 20 2a 70 3b 0a 20 20 43 6f 6c 75  Table *p;.  Colu
1bd62 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 69 66 28 20  mn *pCol;.  if( 
1bd63 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65  (p = pParse->pNe
1bd64 77 54 61 62 6c 65 29 21 3d 30 20 29 7b 0a 20 20  wTable)!=0 ){.  
1bd65 20 20 70 43 6f 6c 20 3d 20 26 28 70 2d 3e 61 43    pCol = &(p->aC
1bd66 6f 6c 5b 70 2d 3e 6e 43 6f 6c 2d 31 5d 29 3b 0a  ol[p->nCol-1]);.
1bd67 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33      if( !sqlite3
1bd68 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72  ExprIsConstantOr
1bd69 46 75 6e 63 74 69 6f 6e 28 70 45 78 70 72 29 20  Function(pExpr) 
1bd6a 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1bd6b 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
1bd6c 20 22 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20   "default value 
1bd6d 6f 66 20 63 6f 6c 75 6d 6e 20 5b 25 73 5d 20 69  of column [%s] i
1bd6e 73 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 22 2c  s not constant",
1bd6f 0a 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d  .          pCol-
1bd70 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c  >zName);.    }el
1bd71 73 65 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a  se{.      Expr *
1bd72 70 43 6f 70 79 3b 0a 20 20 20 20 20 20 73 71 6c  pCopy;.      sql
1bd73 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
1bd74 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 73 71 6c  e->db;.      sql
1bd75 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70  ite3ExprDelete(p
1bd76 43 6f 6c 2d 3e 70 44 66 6c 74 29 3b 0a 20 20 20  Col->pDflt);.   
1bd77 20 20 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 20 3d     pCol->pDflt =
1bd78 20 70 43 6f 70 79 20 3d 20 73 71 6c 69 74 65 33   pCopy = sqlite3
1bd79 45 78 70 72 44 75 70 28 64 62 2c 20 70 45 78 70  ExprDup(db, pExp
1bd7a 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 43  r);.      if( pC
1bd7b 6f 70 79 20 29 7b 0a 20 20 20 20 20 20 20 20 73  opy ){.        s
1bd7c 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70 79 28  qlite3TokenCopy(
1bd7d 64 62 2c 20 26 70 43 6f 70 79 2d 3e 73 70 61 6e  db, &pCopy->span
1bd7e 2c 20 26 70 45 78 70 72 2d 3e 73 70 61 6e 29 3b  , &pExpr->span);
1bd7f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1bd80 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72   }.  sqlite3Expr
1bd81 44 65 6c 65 74 65 28 70 45 78 70 72 29 3b 0a 7d  Delete(pExpr);.}
1bd82 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 69 67 6e 61 74  ../*.** Designat
1bd83 65 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45  e the PRIMARY KE
1bd84 59 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 2e  Y for the table.
1bd85 20 20 70 4c 69 73 74 20 69 73 20 61 20 6c 69 73    pList is a lis
1bd86 74 20 6f 66 20 6e 61 6d 65 73 20 0a 2a 2a 20 6f  t of names .** o
1bd87 66 20 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 66  f columns that f
1bd88 6f 72 6d 20 74 68 65 20 70 72 69 6d 61 72 79 20  orm the primary 
1bd89 6b 65 79 2e 20 20 49 66 20 70 4c 69 73 74 20 69  key.  If pList i
1bd8a 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65  s NULL, then the
1bd8b 0a 2a 2a 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  .** most recentl
1bd8c 79 20 61 64 64 65 64 20 63 6f 6c 75 6d 6e 20 6f  y added column o
1bd8d 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 74  f the table is t
1bd8e 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e 0a  he primary key..
1bd8f 2a 2a 0a 2a 2a 20 41 20 74 61 62 6c 65 20 63 61  **.** A table ca
1bd90 6e 20 68 61 76 65 20 61 74 20 6d 6f 73 74 20 6f  n have at most o
1bd91 6e 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e 20  ne primary key. 
1bd92 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 61 6c   If the table al
1bd93 72 65 61 64 79 20 68 61 73 0a 2a 2a 20 61 20 70  ready has.** a p
1bd94 72 69 6d 61 72 79 20 6b 65 79 20 28 61 6e 64 20  rimary key (and 
1bd95 74 68 69 73 20 69 73 20 74 68 65 20 73 65 63 6f  this is the seco
1bd96 6e 64 20 70 72 69 6d 61 72 79 20 6b 65 79 29 20  nd primary key) 
1bd97 74 68 65 6e 20 63 72 65 61 74 65 20 61 6e 0a 2a  then create an.*
1bd98 2a 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49  * error..**.** I
1bd99 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45  f the PRIMARY KE
1bd9a 59 20 69 73 20 6f 6e 20 61 20 73 69 6e 67 6c 65  Y is on a single
1bd9b 20 63 6f 6c 75 6d 6e 20 77 68 6f 73 65 20 64 61   column whose da
1bd9c 74 61 74 79 70 65 20 69 73 20 49 4e 54 45 47 45  tatype is INTEGE
1bd9d 52 2c 0a 2a 2a 20 74 68 65 6e 20 77 65 20 77 69  R,.** then we wi
1bd9e 6c 6c 20 74 72 79 20 74 6f 20 75 73 65 20 74 68  ll try to use th
1bd9f 61 74 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65  at column as the
1bda0 20 72 6f 77 69 64 2e 20 20 53 65 74 20 74 68 65   rowid.  Set the
1bda1 20 54 61 62 6c 65 2e 69 50 4b 65 79 0a 2a 2a 20   Table.iPKey.** 
1bda2 66 69 65 6c 64 20 6f 66 20 74 68 65 20 74 61 62  field of the tab
1bda3 6c 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75  le under constru
1bda4 63 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20  ction to be the 
1bda5 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20  index of the.** 
1bda6 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
1bda7 4b 45 59 20 63 6f 6c 75 6d 6e 2e 20 20 54 61 62  KEY column.  Tab
1bda8 6c 65 2e 69 50 4b 65 79 20 69 73 20 73 65 74 20  le.iPKey is set 
1bda9 74 6f 20 2d 31 20 69 66 20 74 68 65 72 65 20 69  to -1 if there i
1bdaa 73 0a 2a 2a 20 6e 6f 20 49 4e 54 45 47 45 52 20  s.** no INTEGER 
1bdab 50 52 49 4d 41 52 59 20 4b 45 59 2e 0a 2a 2a 0a  PRIMARY KEY..**.
1bdac 2a 2a 20 49 66 20 74 68 65 20 6b 65 79 20 69 73  ** If the key is
1bdad 20 6e 6f 74 20 61 6e 20 49 4e 54 45 47 45 52 20   not an INTEGER 
1bdae 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 74 68 65  PRIMARY KEY, the
1bdaf 6e 20 63 72 65 61 74 65 20 61 20 75 6e 69 71 75  n create a uniqu
1bdb0 65 0a 2a 2a 20 69 6e 64 65 78 20 66 6f 72 20 74  e.** index for t
1bdb1 68 65 20 6b 65 79 2e 20 20 4e 6f 20 69 6e 64 65  he key.  No inde
1bdb2 78 20 69 73 20 63 72 65 61 74 65 64 20 66 6f 72  x is created for
1bdb3 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
1bdb4 20 4b 45 59 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45   KEYs..*/.SQLITE
1bdb5 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1bdb6 6c 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b  lite3AddPrimaryK
1bdb7 65 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ey(.  Parse *pPa
1bdb8 72 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69  rse,    /* Parsi
1bdb9 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
1bdba 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c  ExprList *pList,
1bdbb 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 66 69 65    /* List of fie
1bdbc 6c 64 20 6e 61 6d 65 73 20 74 6f 20 62 65 20 69  ld names to be i
1bdbd 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20  ndexed */.  int 
1bdbe 6f 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 2f 2a  onError,      /*
1bdbf 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68   What to do with
1bdc0 20 61 20 75 6e 69 71 75 65 6e 65 73 73 20 63 6f   a uniqueness co
1bdc1 6e 66 6c 69 63 74 20 2a 2f 0a 20 20 69 6e 74 20  nflict */.  int 
1bdc2 61 75 74 6f 49 6e 63 2c 20 20 20 20 20 20 2f 2a  autoInc,      /*
1bdc3 20 54 72 75 65 20 69 66 20 74 68 65 20 41 55 54   True if the AUT
1bdc4 4f 49 4e 43 52 45 4d 45 4e 54 20 6b 65 79 77 6f  OINCREMENT keywo
1bdc5 72 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f  rd is present */
1bdc6 0a 20 20 69 6e 74 20 73 6f 72 74 4f 72 64 65 72  .  int sortOrder
1bdc7 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 53       /* SQLITE_S
1bdc8 4f 5f 41 53 43 20 6f 72 20 53 51 4c 49 54 45 5f  O_ASC or SQLITE_
1bdc9 53 4f 5f 44 45 53 43 20 2a 2f 0a 29 7b 0a 20 20  SO_DESC */.){.  
1bdca 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 50  Table *pTab = pP
1bdcb 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b  arse->pNewTable;
1bdcc 0a 20 20 63 68 61 72 20 2a 7a 54 79 70 65 20 3d  .  char *zType =
1bdcd 20 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 20 3d   0;.  int iCol =
1bdce 20 2d 31 2c 20 69 3b 0a 20 20 69 66 28 20 70 54   -1, i;.  if( pT
1bdcf 61 62 3d 3d 30 20 7c 7c 20 49 4e 5f 44 45 43 4c  ab==0 || IN_DECL
1bdd0 41 52 45 5f 56 54 41 42 20 29 20 67 6f 74 6f 20  ARE_VTAB ) goto 
1bdd1 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78 69 74  primary_key_exit
1bdd2 3b 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 68 61  ;.  if( pTab->ha
1bdd3 73 50 72 69 6d 4b 65 79 20 29 7b 0a 20 20 20 20  sPrimKey ){.    
1bdd4 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1bdd5 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 22  pParse, .      "
1bdd6 74 61 62 6c 65 20 5c 22 25 73 5c 22 20 68 61 73  table \"%s\" has
1bdd7 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 70   more than one p
1bdd8 72 69 6d 61 72 79 20 6b 65 79 22 2c 20 70 54 61  rimary key", pTa
1bdd9 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67  b->zName);.    g
1bdda 6f 74 6f 20 70 72 69 6d 61 72 79 5f 6b 65 79 5f  oto primary_key_
1bddb 65 78 69 74 3b 0a 20 20 7d 0a 20 20 70 54 61 62  exit;.  }.  pTab
1bddc 2d 3e 68 61 73 50 72 69 6d 4b 65 79 20 3d 20 31  ->hasPrimKey = 1
1bddd 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30  ;.  if( pList==0
1bdde 20 29 7b 0a 20 20 20 20 69 43 6f 6c 20 3d 20 70   ){.    iCol = p
1bddf 54 61 62 2d 3e 6e 43 6f 6c 20 2d 20 31 3b 0a 20  Tab->nCol - 1;. 
1bde0 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43     pTab->aCol[iC
1bde1 6f 6c 5d 2e 69 73 50 72 69 6d 4b 65 79 20 3d 20  ol].isPrimKey = 
1bde2 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  1;.  }else{.    
1bde3 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74  for(i=0; i<pList
1bde4 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20  ->nExpr; i++){. 
1bde5 20 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b       for(iCol=0;
1bde6 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c   iCol<pTab->nCol
1bde7 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20  ; iCol++){.     
1bde8 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
1bde9 72 49 43 6d 70 28 70 4c 69 73 74 2d 3e 61 5b 69  rICmp(pList->a[i
1bdea 5d 2e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 61  ].zName, pTab->a
1bdeb 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 29  Col[iCol].zName)
1bdec 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
1bded 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
1bdee 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
1bdef 69 66 28 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e  if( iCol<pTab->n
1bdf0 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70  Col ){.        p
1bdf1 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e  Tab->aCol[iCol].
1bdf2 69 73 50 72 69 6d 4b 65 79 20 3d 20 31 3b 0a 20  isPrimKey = 1;. 
1bdf3 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1bdf4 20 69 66 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70   if( pList->nExp
1bdf5 72 3e 31 20 29 20 69 43 6f 6c 20 3d 20 2d 31 3b  r>1 ) iCol = -1;
1bdf6 0a 20 20 7d 0a 20 20 69 66 28 20 69 43 6f 6c 3e  .  }.  if( iCol>
1bdf7 3d 30 20 26 26 20 69 43 6f 6c 3c 70 54 61 62 2d  =0 && iCol<pTab-
1bdf8 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 7a 54 79  >nCol ){.    zTy
1bdf9 70 65 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  pe = pTab->aCol[
1bdfa 69 43 6f 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20 7d  iCol].zType;.  }
1bdfb 0a 20 20 69 66 28 20 7a 54 79 70 65 20 26 26 20  .  if( zType && 
1bdfc 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
1bdfd 54 79 70 65 2c 20 22 49 4e 54 45 47 45 52 22 29  Type, "INTEGER")
1bdfe 3d 3d 30 0a 20 20 20 20 20 20 20 20 26 26 20 73  ==0.        && s
1bdff 6f 72 74 4f 72 64 65 72 3d 3d 53 51 4c 49 54 45  ortOrder==SQLITE
1be00 5f 53 4f 5f 41 53 43 20 29 7b 0a 20 20 20 20 70  _SO_ASC ){.    p
1be01 54 61 62 2d 3e 69 50 4b 65 79 20 3d 20 69 43 6f  Tab->iPKey = iCo
1be02 6c 3b 0a 20 20 20 20 70 54 61 62 2d 3e 6b 65 79  l;.    pTab->key
1be03 43 6f 6e 66 20 3d 20 6f 6e 45 72 72 6f 72 3b 0a  Conf = onError;.
1be04 20 20 20 20 70 54 61 62 2d 3e 61 75 74 6f 49 6e      pTab->autoIn
1be05 63 20 3d 20 61 75 74 6f 49 6e 63 3b 0a 20 20 7d  c = autoInc;.  }
1be06 65 6c 73 65 20 69 66 28 20 61 75 74 6f 49 6e 63  else if( autoInc
1be07 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   ){.#ifndef SQLI
1be08 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52  TE_OMIT_AUTOINCR
1be09 45 4d 45 4e 54 0a 20 20 20 20 73 71 6c 69 74 65  EMENT.    sqlite
1be0a 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1be0b 2c 20 22 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54  , "AUTOINCREMENT
1be0c 20 69 73 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64   is only allowed
1be0d 20 6f 6e 20 61 6e 20 22 0a 20 20 20 20 20 20 20   on an ".       
1be0e 22 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59  "INTEGER PRIMARY
1be0f 20 4b 45 59 22 29 3b 0a 23 65 6e 64 69 66 0a 20   KEY");.#endif. 
1be10 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
1be11 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 28 70  te3CreateIndex(p
1be12 50 61 72 73 65 2c 20 30 2c 20 30 2c 20 30 2c 20  Parse, 0, 0, 0, 
1be13 70 4c 69 73 74 2c 20 6f 6e 45 72 72 6f 72 2c 20  pList, onError, 
1be14 30 2c 20 30 2c 20 73 6f 72 74 4f 72 64 65 72 2c  0, 0, sortOrder,
1be15 20 30 29 3b 0a 20 20 20 20 70 4c 69 73 74 20 3d   0);.    pList =
1be16 20 30 3b 0a 20 20 7d 0a 0a 70 72 69 6d 61 72 79   0;.  }..primary
1be17 5f 6b 65 79 5f 65 78 69 74 3a 0a 20 20 73 71 6c  _key_exit:.  sql
1be18 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
1be19 74 65 28 70 4c 69 73 74 29 3b 0a 20 20 72 65 74  te(pList);.  ret
1be1a 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  urn;.}../*.** Ad
1be1b 64 20 61 20 6e 65 77 20 43 48 45 43 4b 20 63 6f  d a new CHECK co
1be1c 6e 73 74 72 61 69 6e 74 20 74 6f 20 74 68 65 20  nstraint to the 
1be1d 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20  table currently 
1be1e 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69  under constructi
1be1f 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
1be20 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1be21 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74 72  e3AddCheckConstr
1be22 61 69 6e 74 28 0a 20 20 50 61 72 73 65 20 2a 70  aint(.  Parse *p
1be23 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 50 61 72  Parse,    /* Par
1be24 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
1be25 20 20 45 78 70 72 20 2a 70 43 68 65 63 6b 45 78    Expr *pCheckEx
1be26 70 72 20 20 2f 2a 20 54 68 65 20 63 68 65 63 6b  pr  /* The check
1be27 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 29   expression */.)
1be28 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
1be29 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 54 61  _OMIT_CHECK.  Ta
1be2a 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61 72  ble *pTab = pPar
1be2b 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20  se->pNewTable;. 
1be2c 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
1be2d 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28  Parse->db;.  if(
1be2e 20 70 54 61 62 20 26 26 20 21 49 4e 5f 44 45 43   pTab && !IN_DEC
1be2f 4c 41 52 45 5f 56 54 41 42 20 29 7b 0a 20 20 20  LARE_VTAB ){.   
1be30 20 2f 2a 20 54 68 65 20 43 48 45 43 4b 20 65 78   /* The CHECK ex
1be31 70 72 65 73 73 69 6f 6e 20 6d 75 73 74 20 62 65  pression must be
1be32 20 64 75 70 6c 69 63 61 74 65 64 20 73 6f 20 74   duplicated so t
1be33 68 61 74 20 74 6f 6b 65 6e 73 20 72 65 66 65 72  hat tokens refer
1be34 0a 20 20 20 20 2a 2a 20 74 6f 20 6d 61 6c 6c 6f  .    ** to mallo
1be35 63 65 64 20 73 70 61 63 65 20 61 6e 64 20 6e 6f  ced space and no
1be36 74 20 74 68 65 20 28 65 70 68 65 6d 65 72 61 6c  t the (ephemeral
1be37 29 20 74 65 78 74 20 6f 66 20 74 68 65 20 43 52  ) text of the CR
1be38 45 41 54 45 20 54 41 42 4c 45 0a 20 20 20 20 2a  EATE TABLE.    *
1be39 2a 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  * statement */. 
1be3a 20 20 20 70 54 61 62 2d 3e 70 43 68 65 63 6b 20     pTab->pCheck 
1be3b 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64  = sqlite3ExprAnd
1be3c 28 64 62 2c 20 70 54 61 62 2d 3e 70 43 68 65 63  (db, pTab->pChec
1be3d 6b 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  k, .            
1be3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1be3f 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
1be40 72 44 75 70 28 64 62 2c 20 70 43 68 65 63 6b 45  rDup(db, pCheckE
1be41 78 70 72 29 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  xpr));.  }.#endi
1be42 66 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44  f.  sqlite3ExprD
1be43 65 6c 65 74 65 28 70 43 68 65 63 6b 45 78 70 72  elete(pCheckExpr
1be44 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  );.}../*.** Set 
1be45 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75  the collation fu
1be46 6e 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f  nction of the mo
1be47 73 74 20 72 65 63 65 6e 74 6c 79 20 70 61 72 73  st recently pars
1be48 65 64 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 0a  ed table column.
1be49 2a 2a 20 74 6f 20 74 68 65 20 43 6f 6c 6c 53 65  ** to the CollSe
1be4a 71 20 67 69 76 65 6e 2e 0a 2a 2f 0a 53 51 4c 49  q given..*/.SQLI
1be4b 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1be4c 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 74  sqlite3AddCollat
1be4d 65 54 79 70 65 28 50 61 72 73 65 20 2a 70 50 61  eType(Parse *pPa
1be4e 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b  rse, Token *pTok
1be4f 65 6e 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b  en){.  Table *p;
1be50 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 68 61 72  .  int i;.  char
1be51 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20   *zColl;        
1be52 20 20 20 20 20 20 2f 2a 20 44 65 71 75 6f 74 65        /* Dequote
1be53 64 20 6e 61 6d 65 20 6f 66 20 63 6f 6c 6c 61 74  d name of collat
1be54 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a  ion sequence */.
1be55 0a 20 20 69 66 28 20 28 70 20 3d 20 70 50 61 72  .  if( (p = pPar
1be56 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d  se->pNewTable)==
1be57 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 20  0 ) return;.  i 
1be58 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 0a 20 20  = p->nCol-1;..  
1be59 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4e  zColl = sqlite3N
1be5a 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 61  ameFromToken(pPa
1be5b 72 73 65 2d 3e 64 62 2c 20 70 54 6f 6b 65 6e 29  rse->db, pToken)
1be5c 3b 0a 20 20 69 66 28 20 21 7a 43 6f 6c 6c 20 29  ;.  if( !zColl )
1be5d 20 72 65 74 75 72 6e 3b 0a 0a 20 20 69 66 28 20   return;..  if( 
1be5e 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c  sqlite3LocateCol
1be5f 6c 53 65 71 28 70 50 61 72 73 65 2c 20 7a 43 6f  lSeq(pParse, zCo
1be60 6c 6c 2c 20 2d 31 29 20 29 7b 0a 20 20 20 20 49  ll, -1) ){.    I
1be61 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20  ndex *pIdx;.    
1be62 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c  p->aCol[i].zColl
1be63 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 20 0a 20 20 20   = zColl;.  .   
1be64 20 2f 2a 20 49 66 20 74 68 65 20 63 6f 6c 75 6d   /* If the colum
1be65 6e 20 69 73 20 64 65 63 6c 61 72 65 64 20 61 73  n is declared as
1be66 20 22 3c 6e 61 6d 65 3e 20 50 52 49 4d 41 52 59   "<name> PRIMARY
1be67 20 4b 45 59 20 43 4f 4c 4c 41 54 45 20 3c 74 79   KEY COLLATE <ty
1be68 70 65 3e 22 2c 0a 20 20 20 20 2a 2a 20 74 68 65  pe>",.    ** the
1be69 6e 20 61 6e 20 69 6e 64 65 78 20 6d 61 79 20 68  n an index may h
1be6a 61 76 65 20 62 65 65 6e 20 63 72 65 61 74 65 64  ave been created
1be6b 20 6f 6e 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20   on this column 
1be6c 62 65 66 6f 72 65 20 74 68 65 0a 20 20 20 20 2a  before the.    *
1be6d 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65  * collation type
1be6e 20 77 61 73 20 61 64 64 65 64 2e 20 43 6f 72 72   was added. Corr
1be6f 65 63 74 20 74 68 69 73 20 69 66 20 69 74 20 69  ect this if it i
1be70 73 20 74 68 65 20 63 61 73 65 2e 0a 20 20 20 20  s the case..    
1be71 2a 2f 0a 20 20 20 20 66 6f 72 28 70 49 64 78 3d  */.    for(pIdx=
1be72 70 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  p->pIndex; pIdx;
1be73 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
1be74 74 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  t){.      assert
1be75 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d  ( pIdx->nColumn=
1be76 3d 31 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  =1 );.      if( 
1be77 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30  pIdx->aiColumn[0
1be78 5d 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20 20 20  ]==i ){.        
1be79 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 30 5d 20  pIdx->azColl[0] 
1be7a 3d 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f  = p->aCol[i].zCo
1be7b 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ll;.      }.    
1be7c 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  }.  }else{.    s
1be7d 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6c  qlite3_free(zCol
1be7e 6c 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  l);.  }.}../*.**
1be7f 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   This function r
1be80 65 74 75 72 6e 73 20 74 68 65 20 63 6f 6c 6c 61  eturns the colla
1be81 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 6f  tion sequence fo
1be82 72 20 64 61 74 61 62 61 73 65 20 6e 61 74 69 76  r database nativ
1be83 65 20 74 65 78 74 0a 2a 2a 20 65 6e 63 6f 64 69  e text.** encodi
1be84 6e 67 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  ng identified by
1be85 20 74 68 65 20 73 74 72 69 6e 67 20 7a 4e 61 6d   the string zNam
1be86 65 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2e  e, length nName.
1be87 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65  .**.** If the re
1be88 71 75 65 73 74 65 64 20 63 6f 6c 6c 61 74 69 6f  quested collatio
1be89 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 6e 6f  n sequence is no
1be8a 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 6f 72 20  t available, or 
1be8b 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a  not available.**
1be8c 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
1be8d 20 6e 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67   native encoding
1be8e 2c 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  , the collation 
1be8f 66 61 63 74 6f 72 79 20 69 73 20 69 6e 76 6f 6b  factory is invok
1be90 65 64 20 74 6f 0a 2a 2a 20 72 65 71 75 65 73 74  ed to.** request
1be91 20 69 74 2e 20 49 66 20 74 68 65 20 63 6f 6c 6c   it. If the coll
1be92 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 64 6f  ation factory do
1be93 65 73 20 6e 6f 74 20 73 75 70 70 6c 79 20 73 75  es not supply su
1be94 63 68 20 61 20 73 65 71 75 65 6e 63 65 2c 0a 2a  ch a sequence,.*
1be95 2a 20 61 6e 64 20 74 68 65 20 73 65 71 75 65 6e  * and the sequen
1be96 63 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20  ce is available 
1be97 69 6e 20 61 6e 6f 74 68 65 72 20 74 65 78 74 20  in another text 
1be98 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20 74  encoding, then t
1be99 68 61 74 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e  hat is.** return
1be9a 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a  ed instead..**.*
1be9b 2a 20 49 66 20 6e 6f 20 76 65 72 73 69 6f 6e 73  * If no versions
1be9c 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65   of the requeste
1be9d 64 20 63 6f 6c 6c 61 74 69 6f 6e 73 20 73 65 71  d collations seq
1be9e 75 65 6e 63 65 20 61 72 65 20 61 76 61 69 6c 61  uence are availa
1be9f 62 6c 65 2c 20 6f 72 0a 2a 2a 20 61 6e 6f 74 68  ble, or.** anoth
1bea0 65 72 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  er error occurs,
1bea1 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65   NULL is returne
1bea2 64 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d  d and an error m
1bea3 65 73 73 61 67 65 20 77 72 69 74 74 65 6e 20 69  essage written i
1bea4 6e 74 6f 0a 2a 2a 20 70 50 61 72 73 65 2e 0a 2a  nto.** pParse..*
1bea5 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1bea6 65 20 69 73 20 61 20 77 72 61 70 70 65 72 20 61  e is a wrapper a
1bea7 72 6f 75 6e 64 20 73 71 6c 69 74 65 33 46 69 6e  round sqlite3Fin
1bea8 64 43 6f 6c 6c 53 65 71 28 29 2e 20 20 54 68 69  dCollSeq().  Thi
1bea9 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 6e 76  s routine.** inv
1beaa 6f 6b 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69  okes the collati
1beab 6f 6e 20 66 61 63 74 6f 72 79 20 69 66 20 74 68  on factory if th
1beac 65 20 6e 61 6d 65 64 20 63 6f 6c 6c 61 74 69 6f  e named collatio
1bead 6e 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e  n cannot be foun
1beae 64 0a 2a 2a 20 61 6e 64 20 67 65 6e 65 72 61 74  d.** and generat
1beaf 65 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  es an error mess
1beb0 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  age..*/.SQLITE_P
1beb1 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a  RIVATE CollSeq *
1beb2 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c  sqlite3LocateCol
1beb3 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50 61 72  lSeq(Parse *pPar
1beb4 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  se, const char *
1beb5 7a 4e 61 6d 65 2c 20 69 6e 74 20 6e 4e 61 6d 65  zName, int nName
1beb6 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
1beb7 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
1beb8 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 64 62   u8 enc = ENC(db
1beb9 29 3b 0a 20 20 75 38 20 69 6e 69 74 62 75 73 79  );.  u8 initbusy
1beba 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79   = db->init.busy
1bebb 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  ;.  CollSeq *pCo
1bebc 6c 6c 3b 0a 0a 20 20 70 43 6f 6c 6c 20 3d 20 73  ll;..  pColl = s
1bebd 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65  qlite3FindCollSe
1bebe 71 28 64 62 2c 20 65 6e 63 2c 20 7a 4e 61 6d 65  q(db, enc, zName
1bebf 2c 20 6e 4e 61 6d 65 2c 20 69 6e 69 74 62 75 73  , nName, initbus
1bec0 79 29 3b 0a 20 20 69 66 28 20 21 69 6e 69 74 62  y);.  if( !initb
1bec1 75 73 79 20 26 26 20 28 21 70 43 6f 6c 6c 20 7c  usy && (!pColl |
1bec2 7c 20 21 70 43 6f 6c 6c 2d 3e 78 43 6d 70 29 20  | !pColl->xCmp) 
1bec3 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73  ){.    pColl = s
1bec4 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71  qlite3GetCollSeq
1bec5 28 64 62 2c 20 70 43 6f 6c 6c 2c 20 7a 4e 61 6d  (db, pColl, zNam
1bec6 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 69  e, nName);.    i
1bec7 66 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20  f( !pColl ){.   
1bec8 20 20 20 69 66 28 20 6e 4e 61 6d 65 3c 30 20 29     if( nName<0 )
1bec9 7b 0a 20 20 20 20 20 20 20 20 6e 4e 61 6d 65 20  {.        nName 
1beca 3d 20 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 3b  = strlen(zName);
1becb 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
1becc 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1becd 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20  Parse, "no such 
1bece 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
1becf 63 65 3a 20 25 2e 2a 73 22 2c 20 6e 4e 61 6d 65  ce: %.*s", nName
1bed0 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  , zName);.      
1bed1 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 7d  pColl = 0;.    }
1bed2 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70  .  }..  return p
1bed3 43 6f 6c 6c 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  Coll;.}.../*.** 
1bed4 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  Generate code th
1bed5 61 74 20 77 69 6c 6c 20 69 6e 63 72 65 6d 65 6e  at will incremen
1bed6 74 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f  t the schema coo
1bed7 6b 69 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  kie..**.** The s
1bed8 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 69 73 20  chema cookie is 
1bed9 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e  used to determin
1beda 65 20 77 68 65 6e 20 74 68 65 20 73 63 68 65 6d  e when the schem
1bedb 61 20 66 6f 72 20 74 68 65 0a 2a 2a 20 64 61 74  a for the.** dat
1bedc 61 62 61 73 65 20 63 68 61 6e 67 65 73 2e 20 20  abase changes.  
1bedd 41 66 74 65 72 20 65 61 63 68 20 73 63 68 65 6d  After each schem
1bede 61 20 63 68 61 6e 67 65 2c 20 74 68 65 20 63 6f  a change, the co
1bedf 6f 6b 69 65 20 76 61 6c 75 65 0a 2a 2a 20 63 68  okie value.** ch
1bee0 61 6e 67 65 73 2e 20 20 57 68 65 6e 20 61 20 70  anges.  When a p
1bee1 72 6f 63 65 73 73 20 66 69 72 73 74 20 72 65 61  rocess first rea
1bee2 64 73 20 74 68 65 20 73 63 68 65 6d 61 20 69 74  ds the schema it
1bee3 20 72 65 63 6f 72 64 73 20 74 68 65 0a 2a 2a 20   records the.** 
1bee4 63 6f 6f 6b 69 65 2e 20 20 54 68 65 72 65 61 66  cookie.  Thereaf
1bee5 74 65 72 2c 20 77 68 65 6e 65 76 65 72 20 69 74  ter, whenever it
1bee6 20 67 6f 65 73 20 74 6f 20 61 63 63 65 73 73 20   goes to access 
1bee7 74 68 65 20 64 61 74 61 62 61 73 65 2c 0a 2a 2a  the database,.**
1bee8 20 69 74 20 63 68 65 63 6b 73 20 74 68 65 20 63   it checks the c
1bee9 6f 6f 6b 69 65 20 74 6f 20 6d 61 6b 65 20 73 75  ookie to make su
1beea 72 65 20 74 68 65 20 73 63 68 65 6d 61 20 68 61  re the schema ha
1beeb 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 0a 2a 2a  s not changed.**
1beec 20 73 69 6e 63 65 20 69 74 20 77 61 73 20 6c 61   since it was la
1beed 73 74 20 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 54  st read..**.** T
1beee 68 69 73 20 70 6c 61 6e 20 69 73 20 6e 6f 74 20  his plan is not 
1beef 63 6f 6d 70 6c 65 74 65 6c 79 20 62 75 6c 6c 65  completely bulle
1bef0 74 2d 70 72 6f 6f 66 2e 20 20 49 74 20 69 73 20  t-proof.  It is 
1bef1 70 6f 73 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20  possible for.** 
1bef2 74 68 65 20 73 63 68 65 6d 61 20 74 6f 20 63 68  the schema to ch
1bef3 61 6e 67 65 20 6d 75 6c 74 69 70 6c 65 20 74 69  ange multiple ti
1bef4 6d 65 73 20 61 6e 64 20 66 6f 72 20 74 68 65 20  mes and for the 
1bef5 63 6f 6f 6b 69 65 20 74 6f 20 62 65 0a 2a 2a 20  cookie to be.** 
1bef6 73 65 74 20 62 61 63 6b 20 74 6f 20 70 72 69 6f  set back to prio
1bef7 72 20 76 61 6c 75 65 2e 20 20 42 75 74 20 73 63  r value.  But sc
1bef8 68 65 6d 61 20 63 68 61 6e 67 65 73 20 61 72 65  hema changes are
1bef9 20 69 6e 66 72 65 71 75 65 6e 74 0a 2a 2a 20 61   infrequent.** a
1befa 6e 64 20 74 68 65 20 70 72 6f 62 61 62 69 6c 69  nd the probabili
1befb 74 79 20 6f 66 20 68 69 74 74 69 6e 67 20 74 68  ty of hitting th
1befc 65 20 73 61 6d 65 20 63 6f 6f 6b 69 65 20 76 61  e same cookie va
1befd 6c 75 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 31  lue is only.** 1
1befe 20 63 68 61 6e 63 65 20 69 6e 20 32 5e 33 32 2e   chance in 2^32.
1beff 20 20 53 6f 20 77 65 27 72 65 20 73 61 66 65 20    So we're safe 
1bf00 65 6e 6f 75 67 68 2e 0a 2a 2f 0a 53 51 4c 49 54  enough..*/.SQLIT
1bf01 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1bf02 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b  qlite3ChangeCook
1bf03 69 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ie(Parse *pParse
1bf04 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 69 6e  , int iDb){.  in
1bf05 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65  t r1 = sqlite3Ge
1bf06 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
1bf07 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
1bf08 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
1bf09 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
1bf0a 2d 3e 70 56 64 62 65 3b 0a 20 20 73 71 6c 69 74  ->pVdbe;.  sqlit
1bf0b 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1bf0c 4f 50 5f 49 6e 74 65 67 65 72 2c 20 64 62 2d 3e  OP_Integer, db->
1bf0d 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61  aDb[iDb].pSchema
1bf0e 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 2b  ->schema_cookie+
1bf0f 31 2c 20 72 31 29 3b 0a 20 20 73 71 6c 69 74 65  1, r1);.  sqlite
1bf10 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
1bf11 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62  P_SetCookie, iDb
1bf12 2c 20 30 2c 20 72 31 29 3b 0a 20 20 73 71 6c 69  , 0, r1);.  sqli
1bf13 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
1bf14 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 7d  g(pParse, r1);.}
1bf15 0a 0a 2f 2a 0a 2a 2a 20 4d 65 61 73 75 72 65 20  ../*.** Measure 
1bf16 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68  the number of ch
1bf17 61 72 61 63 74 65 72 73 20 6e 65 65 64 65 64 20  aracters needed 
1bf18 74 6f 20 6f 75 74 70 75 74 20 74 68 65 20 67 69  to output the gi
1bf19 76 65 6e 0a 2a 2a 20 69 64 65 6e 74 69 66 69 65  ven.** identifie
1bf1a 72 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 20 72  r.  The number r
1bf1b 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 64 65 73  eturned includes
1bf1c 20 61 6e 79 20 71 75 6f 74 65 73 20 75 73 65 64   any quotes used
1bf1d 0a 2a 2a 20 62 75 74 20 64 6f 65 73 20 6e 6f 74  .** but does not
1bf1e 20 69 6e 63 6c 75 64 65 20 74 68 65 20 6e 75 6c   include the nul
1bf1f 6c 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a  l terminator..**
1bf20 0a 2a 2a 20 54 68 65 20 65 73 74 69 6d 61 74 65  .** The estimate
1bf21 20 69 73 20 63 6f 6e 73 65 72 76 61 74 69 76 65   is conservative
1bf22 2e 20 20 49 74 20 6d 69 67 68 74 20 62 65 20 6c  .  It might be l
1bf23 61 72 67 65 72 20 74 68 61 74 20 77 68 61 74 20  arger that what 
1bf24 69 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 6e 65 65  is.** really nee
1bf25 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ded..*/.static i
1bf26 6e 74 20 69 64 65 6e 74 4c 65 6e 67 74 68 28 63  nt identLength(c
1bf27 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20  onst char *z){. 
1bf28 20 69 6e 74 20 6e 3b 0a 20 20 66 6f 72 28 6e 3d   int n;.  for(n=
1bf29 30 3b 20 2a 7a 3b 20 6e 2b 2b 2c 20 7a 2b 2b 29  0; *z; n++, z++)
1bf2a 7b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27 22  {.    if( *z=='"
1bf2b 27 20 29 7b 20 6e 2b 2b 3b 20 7d 0a 20 20 7d 0a  ' ){ n++; }.  }.
1bf2c 20 20 72 65 74 75 72 6e 20 6e 20 2b 20 32 3b 0a    return n + 2;.
1bf2d 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61  }../*.** Write a
1bf2e 6e 20 69 64 65 6e 74 69 66 69 65 72 20 6f 6e 74  n identifier ont
1bf2f 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  o the end of the
1bf30 20 67 69 76 65 6e 20 73 74 72 69 6e 67 2e 20 20   given string.  
1bf31 41 64 64 0a 2a 2a 20 71 75 6f 74 65 20 63 68 61  Add.** quote cha
1bf32 72 61 63 74 65 72 73 20 61 73 20 6e 65 65 64 65  racters as neede
1bf33 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
1bf34 64 20 69 64 65 6e 74 50 75 74 28 63 68 61 72 20  d identPut(char 
1bf35 2a 7a 2c 20 69 6e 74 20 2a 70 49 64 78 2c 20 63  *z, int *pIdx, c
1bf36 68 61 72 20 2a 7a 53 69 67 6e 65 64 49 64 65 6e  har *zSignedIden
1bf37 74 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  t){.  unsigned c
1bf38 68 61 72 20 2a 7a 49 64 65 6e 74 20 3d 20 28 75  har *zIdent = (u
1bf39 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 7a 53  nsigned char*)zS
1bf3a 69 67 6e 65 64 49 64 65 6e 74 3b 0a 20 20 69 6e  ignedIdent;.  in
1bf3b 74 20 69 2c 20 6a 2c 20 6e 65 65 64 51 75 6f 74  t i, j, needQuot
1bf3c 65 3b 0a 20 20 69 20 3d 20 2a 70 49 64 78 3b 0a  e;.  i = *pIdx;.
1bf3d 20 20 66 6f 72 28 6a 3d 30 3b 20 7a 49 64 65 6e    for(j=0; zIden
1bf3e 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  t[j]; j++){.    
1bf3f 69 66 28 20 21 69 73 61 6c 6e 75 6d 28 7a 49 64  if( !isalnum(zId
1bf40 65 6e 74 5b 6a 5d 29 20 26 26 20 7a 49 64 65 6e  ent[j]) && zIden
1bf41 74 5b 6a 5d 21 3d 27 5f 27 20 29 20 62 72 65 61  t[j]!='_' ) brea
1bf42 6b 3b 0a 20 20 7d 0a 20 20 6e 65 65 64 51 75 6f  k;.  }.  needQuo
1bf43 74 65 20 3d 20 20 7a 49 64 65 6e 74 5b 6a 5d 21  te =  zIdent[j]!
1bf44 3d 30 20 7c 7c 20 69 73 64 69 67 69 74 28 7a 49  =0 || isdigit(zI
1bf45 64 65 6e 74 5b 30 5d 29 0a 20 20 20 20 20 20 20  dent[0]).       
1bf46 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71             || sq
1bf47 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65  lite3KeywordCode
1bf48 28 7a 49 64 65 6e 74 2c 20 6a 29 21 3d 54 4b 5f  (zIdent, j)!=TK_
1bf49 49 44 3b 0a 20 20 69 66 28 20 6e 65 65 64 51 75  ID;.  if( needQu
1bf4a 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27  ote ) z[i++] = '
1bf4b 22 27 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 7a  "';.  for(j=0; z
1bf4c 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a  Ident[j]; j++){.
1bf4d 20 20 20 20 7a 5b 69 2b 2b 5d 20 3d 20 7a 49 64      z[i++] = zId
1bf4e 65 6e 74 5b 6a 5d 3b 0a 20 20 20 20 69 66 28 20  ent[j];.    if( 
1bf4f 7a 49 64 65 6e 74 5b 6a 5d 3d 3d 27 22 27 20 29  zIdent[j]=='"' )
1bf50 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b 0a 20   z[i++] = '"';. 
1bf51 20 7d 0a 20 20 69 66 28 20 6e 65 65 64 51 75 6f   }.  if( needQuo
1bf52 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22  te ) z[i++] = '"
1bf53 27 3b 0a 20 20 7a 5b 69 5d 20 3d 20 30 3b 0a 20  ';.  z[i] = 0;. 
1bf54 20 2a 70 49 64 78 20 3d 20 69 3b 0a 7d 0a 0a 2f   *pIdx = i;.}../
1bf55 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 20  *.** Generate a 
1bf56 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
1bf57 74 65 6d 65 6e 74 20 61 70 70 72 6f 70 72 69 61  tement appropria
1bf58 74 65 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e  te for the given
1bf59 0a 2a 2a 20 74 61 62 6c 65 2e 20 20 4d 65 6d 6f  .** table.  Memo
1bf5a 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 74  ry to hold the t
1bf5b 65 78 74 20 6f 66 20 74 68 65 20 73 74 61 74 65  ext of the state
1bf5c 6d 65 6e 74 20 69 73 20 6f 62 74 61 69 6e 65 64  ment is obtained
1bf5d 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d  .** from sqliteM
1bf5e 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 75 73 74  alloc() and must
1bf5f 20 62 65 20 66 72 65 65 64 20 62 79 20 74 68 65   be freed by the
1bf60 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f   calling functio
1bf61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61  n..*/.static cha
1bf62 72 20 2a 63 72 65 61 74 65 54 61 62 6c 65 53 74  r *createTableSt
1bf63 6d 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  mt(sqlite3 *db, 
1bf64 54 61 62 6c 65 20 2a 70 2c 20 69 6e 74 20 69 73  Table *p, int is
1bf65 54 65 6d 70 29 7b 0a 20 20 69 6e 74 20 69 2c 20  Temp){.  int i, 
1bf66 6b 2c 20 6e 3b 0a 20 20 63 68 61 72 20 2a 7a 53  k, n;.  char *zS
1bf67 74 6d 74 3b 0a 20 20 63 68 61 72 20 2a 7a 53 65  tmt;.  char *zSe
1bf68 70 2c 20 2a 7a 53 65 70 32 2c 20 2a 7a 45 6e 64  p, *zSep2, *zEnd
1bf69 2c 20 2a 7a 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a  , *z;.  Column *
1bf6a 70 43 6f 6c 3b 0a 20 20 6e 20 3d 20 30 3b 0a 20  pCol;.  n = 0;. 
1bf6b 20 66 6f 72 28 70 43 6f 6c 20 3d 20 70 2d 3e 61   for(pCol = p->a
1bf6c 43 6f 6c 2c 20 69 3d 30 3b 20 69 3c 70 2d 3e 6e  Col, i=0; i<p->n
1bf6d 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b  Col; i++, pCol++
1bf6e 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 69 64 65 6e  ){.    n += iden
1bf6f 74 4c 65 6e 67 74 68 28 70 43 6f 6c 2d 3e 7a 4e  tLength(pCol->zN
1bf70 61 6d 65 29 3b 0a 20 20 20 20 7a 20 3d 20 70 43  ame);.    z = pC
1bf71 6f 6c 2d 3e 7a 54 79 70 65 3b 0a 20 20 20 20 69  ol->zType;.    i
1bf72 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 6e 20  f( z ){.      n 
1bf73 2b 3d 20 28 73 74 72 6c 65 6e 28 7a 29 20 2b 20  += (strlen(z) + 
1bf74 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  1);.    }.  }.  
1bf75 6e 20 2b 3d 20 69 64 65 6e 74 4c 65 6e 67 74 68  n += identLength
1bf76 28 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66  (p->zName);.  if
1bf77 28 20 6e 3c 35 30 20 29 7b 0a 20 20 20 20 7a 53  ( n<50 ){.    zS
1bf78 65 70 20 3d 20 22 22 3b 0a 20 20 20 20 7a 53 65  ep = "";.    zSe
1bf79 70 32 20 3d 20 22 2c 22 3b 0a 20 20 20 20 7a 45  p2 = ",";.    zE
1bf7a 6e 64 20 3d 20 22 29 22 3b 0a 20 20 7d 65 6c 73  nd = ")";.  }els
1bf7b 65 7b 0a 20 20 20 20 7a 53 65 70 20 3d 20 22 5c  e{.    zSep = "\
1bf7c 6e 20 20 22 3b 0a 20 20 20 20 7a 53 65 70 32 20  n  ";.    zSep2 
1bf7d 3d 20 22 2c 5c 6e 20 20 22 3b 0a 20 20 20 20 7a  = ",\n  ";.    z
1bf7e 45 6e 64 20 3d 20 22 5c 6e 29 22 3b 0a 20 20 7d  End = "\n)";.  }
1bf7f 0a 20 20 6e 20 2b 3d 20 33 35 20 2b 20 36 2a 70  .  n += 35 + 6*p
1bf80 2d 3e 6e 43 6f 6c 3b 0a 20 20 7a 53 74 6d 74 20  ->nCol;.  zStmt 
1bf81 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
1bf82 28 20 6e 20 29 3b 0a 20 20 69 66 28 20 7a 53 74  ( n );.  if( zSt
1bf83 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d  mt==0 ){.    db-
1bf84 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
1bf85 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  1;.    return 0;
1bf86 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 73  .  }.  sqlite3_s
1bf87 6e 70 72 69 6e 74 66 28 6e 2c 20 7a 53 74 6d 74  nprintf(n, zStmt
1bf88 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1bf89 20 20 20 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42      !OMIT_TEMPDB
1bf8a 26 26 69 73 54 65 6d 70 20 3f 20 22 43 52 45 41  &&isTemp ? "CREA
1bf8b 54 45 20 54 45 4d 50 20 54 41 42 4c 45 20 22 3a  TE TEMP TABLE ":
1bf8c 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 22 29  "CREATE TABLE ")
1bf8d 3b 0a 20 20 6b 20 3d 20 73 74 72 6c 65 6e 28 7a  ;.  k = strlen(z
1bf8e 53 74 6d 74 29 3b 0a 20 20 69 64 65 6e 74 50 75  Stmt);.  identPu
1bf8f 74 28 7a 53 74 6d 74 2c 20 26 6b 2c 20 70 2d 3e  t(zStmt, &k, p->
1bf90 7a 4e 61 6d 65 29 3b 0a 20 20 7a 53 74 6d 74 5b  zName);.  zStmt[
1bf91 6b 2b 2b 5d 20 3d 20 27 28 27 3b 0a 20 20 66 6f  k++] = '(';.  fo
1bf92 72 28 70 43 6f 6c 3d 70 2d 3e 61 43 6f 6c 2c 20  r(pCol=p->aCol, 
1bf93 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20  i=0; i<p->nCol; 
1bf94 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20  i++, pCol++){.  
1bf95 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
1bf96 74 66 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b 6b  tf(n-k, &zStmt[k
1bf97 5d 2c 20 7a 53 65 70 29 3b 0a 20 20 20 20 6b 20  ], zSep);.    k 
1bf98 2b 3d 20 73 74 72 6c 65 6e 28 26 7a 53 74 6d 74  += strlen(&zStmt
1bf99 5b 6b 5d 29 3b 0a 20 20 20 20 7a 53 65 70 20 3d  [k]);.    zSep =
1bf9a 20 7a 53 65 70 32 3b 0a 20 20 20 20 69 64 65 6e   zSep2;.    iden
1bf9b 74 50 75 74 28 7a 53 74 6d 74 2c 20 26 6b 2c 20  tPut(zStmt, &k, 
1bf9c 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pCol->zName);.  
1bf9d 20 20 69 66 28 20 28 7a 20 3d 20 70 43 6f 6c 2d    if( (z = pCol-
1bf9e 3e 7a 54 79 70 65 29 21 3d 30 20 29 7b 0a 20 20  >zType)!=0 ){.  
1bf9f 20 20 20 20 7a 53 74 6d 74 5b 6b 2b 2b 5d 20 3d      zStmt[k++] =
1bfa0 20 27 20 27 3b 0a 20 20 20 20 20 20 61 73 73 65   ' ';.      asse
1bfa1 72 74 28 20 73 74 72 6c 65 6e 28 7a 29 2b 6b 2b  rt( strlen(z)+k+
1bfa2 31 3c 3d 6e 20 29 3b 0a 20 20 20 20 20 20 73 71  1<=n );.      sq
1bfa3 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e  lite3_snprintf(n
1bfa4 2d 6b 2c 20 26 7a 53 74 6d 74 5b 6b 5d 2c 20 22  -k, &zStmt[k], "
1bfa5 25 73 22 2c 20 7a 29 3b 0a 20 20 20 20 20 20 6b  %s", z);.      k
1bfa6 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20   += strlen(z);. 
1bfa7 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
1bfa8 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 2d 6b 2c  e3_snprintf(n-k,
1bfa9 20 26 7a 53 74 6d 74 5b 6b 5d 2c 20 22 25 73 22   &zStmt[k], "%s"
1bfaa 2c 20 7a 45 6e 64 29 3b 0a 20 20 72 65 74 75 72  , zEnd);.  retur
1bfab 6e 20 7a 53 74 6d 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  n zStmt;.}../*.*
1bfac 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
1bfad 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 70 6f  s called to repo
1bfae 72 74 20 74 68 65 20 66 69 6e 61 6c 20 22 29 22  rt the final ")"
1bfaf 20 74 68 61 74 20 74 65 72 6d 69 6e 61 74 65 73   that terminates
1bfb0 0a 2a 2a 20 61 20 43 52 45 41 54 45 20 54 41 42  .** a CREATE TAB
1bfb1 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  LE statement..**
1bfb2 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 73 74  .** The table st
1bfb3 72 75 63 74 75 72 65 20 74 68 61 74 20 6f 74 68  ructure that oth
1bfb4 65 72 20 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e  er action routin
1bfb5 65 73 20 68 61 76 65 20 62 65 65 6e 20 62 75 69  es have been bui
1bfb6 6c 64 69 6e 67 0a 2a 2a 20 69 73 20 61 64 64 65  lding.** is adde
1bfb7 64 20 74 6f 20 74 68 65 20 69 6e 74 65 72 6e 61  d to the interna
1bfb8 6c 20 68 61 73 68 20 74 61 62 6c 65 73 2c 20 61  l hash tables, a
1bfb9 73 73 75 6d 69 6e 67 20 6e 6f 20 65 72 72 6f 72  ssuming no error
1bfba 73 20 68 61 76 65 0a 2a 2a 20 6f 63 63 75 72 72  s have.** occurr
1bfbb 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 6e 74  ed..**.** An ent
1bfbc 72 79 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  ry for the table
1bfbd 20 69 73 20 6d 61 64 65 20 69 6e 20 74 68 65 20   is made in the 
1bfbe 6d 61 73 74 65 72 20 74 61 62 6c 65 20 6f 6e 20  master table on 
1bfbf 64 69 73 6b 2c 20 75 6e 6c 65 73 73 0a 2a 2a 20  disk, unless.** 
1bfc0 74 68 69 73 20 69 73 20 61 20 74 65 6d 70 6f 72  this is a tempor
1bfc1 61 72 79 20 74 61 62 6c 65 20 6f 72 20 64 62 2d  ary table or db-
1bfc2 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 31 2e 20 20  >init.busy==1.  
1bfc3 57 68 65 6e 20 64 62 2d 3e 69 6e 69 74 2e 62 75  When db->init.bu
1bfc4 73 79 3d 3d 31 0a 2a 2a 20 69 74 20 6d 65 61 6e  sy==1.** it mean
1bfc5 73 20 77 65 20 61 72 65 20 72 65 61 64 69 6e 67  s we are reading
1bfc6 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74   the sqlite_mast
1bfc7 65 72 20 74 61 62 6c 65 20 62 65 63 61 75 73 65  er table because
1bfc8 20 77 65 20 6a 75 73 74 0a 2a 2a 20 63 6f 6e 6e   we just.** conn
1bfc9 65 63 74 65 64 20 74 6f 20 74 68 65 20 64 61 74  ected to the dat
1bfca 61 62 61 73 65 20 6f 72 20 62 65 63 61 75 73 65  abase or because
1bfcb 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74   the sqlite_mast
1bfcc 65 72 20 74 61 62 6c 65 20 68 61 73 0a 2a 2a 20  er table has.** 
1bfcd 72 65 63 65 6e 74 6c 79 20 63 68 61 6e 67 65 64  recently changed
1bfce 2c 20 73 6f 20 74 68 65 20 65 6e 74 72 79 20 66  , so the entry f
1bfcf 6f 72 20 74 68 69 73 20 74 61 62 6c 65 20 61 6c  or this table al
1bfd0 72 65 61 64 79 20 65 78 69 73 74 73 20 69 6e 0a  ready exists in.
1bfd1 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  ** the sqlite_ma
1bfd2 73 74 65 72 20 74 61 62 6c 65 2e 20 20 57 65 20  ster table.  We 
1bfd3 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 6f 20 63  do not want to c
1bfd4 72 65 61 74 65 20 69 74 20 61 67 61 69 6e 2e 0a  reate it again..
1bfd5 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 53 65  **.** If the pSe
1bfd6 6c 65 63 74 20 61 72 67 75 6d 65 6e 74 20 69 73  lect argument is
1bfd7 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69 74 20 6d 65   not NULL, it me
1bfd8 61 6e 73 20 74 68 61 74 20 74 68 69 73 20 72 6f  ans that this ro
1bfd9 75 74 69 6e 65 0a 2a 2a 20 77 61 73 20 63 61 6c  utine.** was cal
1bfda 6c 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20  led to create a 
1bfdb 74 61 62 6c 65 20 67 65 6e 65 72 61 74 65 64 20  table generated 
1bfdc 66 72 6f 6d 20 61 20 0a 2a 2a 20 22 43 52 45 41  from a .** "CREA
1bfdd 54 45 20 54 41 42 4c 45 20 2e 2e 2e 20 41 53 20  TE TABLE ... AS 
1bfde 53 45 4c 45 43 54 20 2e 2e 2e 22 20 73 74 61 74  SELECT ..." stat
1bfdf 65 6d 65 6e 74 2e 20 20 54 68 65 20 63 6f 6c 75  ement.  The colu
1bfe0 6d 6e 20 6e 61 6d 65 73 20 6f 66 0a 2a 2a 20 74  mn names of.** t
1bfe1 68 65 20 6e 65 77 20 74 61 62 6c 65 20 77 69 6c  he new table wil
1bfe2 6c 20 6d 61 74 63 68 20 74 68 65 20 72 65 73 75  l match the resu
1bfe3 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20 53 45  lt set of the SE
1bfe4 4c 45 43 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  LECT..*/.SQLITE_
1bfe5 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1bfe6 69 74 65 33 45 6e 64 54 61 62 6c 65 28 0a 20 20  ite3EndTable(.  
1bfe7 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
1bfe8 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65          /* Parse
1bfe9 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f   context */.  To
1bfea 6b 65 6e 20 2a 70 43 6f 6e 73 2c 20 20 20 20 20  ken *pCons,     
1bfeb 20 20 20 20 20 20 2f 2a 20 54 68 65 20 27 2c 27        /* The ','
1bfec 20 74 6f 6b 65 6e 20 61 66 74 65 72 20 74 68 65   token after the
1bfed 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 64 65 66   last column def
1bfee 6e 2e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  n. */.  Token *p
1bfef 45 6e 64 2c 20 20 20 20 20 20 20 20 20 20 20 20  End,            
1bff0 2f 2a 20 54 68 65 20 66 69 6e 61 6c 20 27 29 27  /* The final ')'
1bff1 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 43 52   token in the CR
1bff2 45 41 54 45 20 54 41 42 4c 45 20 2a 2f 0a 20 20  EATE TABLE */.  
1bff3 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20  Select *pSelect 
1bff4 20 20 20 20 20 20 20 20 2f 2a 20 53 65 6c 65 63          /* Selec
1bff5 74 20 66 72 6f 6d 20 61 20 22 43 52 45 41 54 45  t from a "CREATE
1bff6 20 2e 2e 2e 20 41 53 20 53 45 4c 45 43 54 22 20   ... AS SELECT" 
1bff7 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  */.){.  Table *p
1bff8 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
1bff9 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
1bffa 69 6e 74 20 69 44 62 3b 0a 0a 20 20 69 66 28 20  int iDb;..  if( 
1bffb 28 70 45 6e 64 3d 3d 30 20 26 26 20 70 53 65 6c  (pEnd==0 && pSel
1bffc 65 63 74 3d 3d 30 29 20 7c 7c 20 70 50 61 72 73  ect==0) || pPars
1bffd 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d  e->nErr || db->m
1bffe 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 7b 0a  allocFailed ) {.
1bfff 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
1c000 20 20 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e    p = pParse->pN
1c001 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 70  ewTable;.  if( p
1c002 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20  ==0 ) return;.. 
1c003 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 69 6e   assert( !db->in
1c004 69 74 2e 62 75 73 79 20 7c 7c 20 21 70 53 65 6c  it.busy || !pSel
1c005 65 63 74 20 29 3b 0a 0a 20 20 69 44 62 20 3d 20  ect );..  iDb = 
1c006 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
1c007 6e 64 65 78 28 64 62 2c 20 70 2d 3e 70 53 63 68  ndex(db, p->pSch
1c008 65 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  ema);..#ifndef S
1c009 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b  QLITE_OMIT_CHECK
1c00a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 6e 61  .  /* Resolve na
1c00b 6d 65 73 20 69 6e 20 61 6c 6c 20 43 48 45 43 4b  mes in all CHECK
1c00c 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 78 70 72   constraint expr
1c00d 65 73 73 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20  essions..  */.  
1c00e 69 66 28 20 70 2d 3e 70 43 68 65 63 6b 20 29 7b  if( p->pCheck ){
1c00f 0a 20 20 20 20 53 72 63 4c 69 73 74 20 73 53 72  .    SrcList sSr
1c010 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
1c011 20 20 20 20 20 2f 2a 20 46 61 6b 65 20 53 72 63       /* Fake Src
1c012 4c 69 73 74 20 66 6f 72 20 70 50 61 72 73 65 2d  List for pParse-
1c013 3e 70 4e 65 77 54 61 62 6c 65 20 2a 2f 0a 20 20  >pNewTable */.  
1c014 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e    NameContext sN
1c015 43 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  C;              
1c016 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78    /* Name contex
1c017 74 20 66 6f 72 20 70 50 61 72 73 65 2d 3e 70 4e  t for pParse->pN
1c018 65 77 54 61 62 6c 65 20 2a 2f 0a 0a 20 20 20 20  ewTable */..    
1c019 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20  memset(&sNC, 0, 
1c01a 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20  sizeof(sNC));.  
1c01b 20 20 6d 65 6d 73 65 74 28 26 73 53 72 63 2c 20    memset(&sSrc, 
1c01c 30 2c 20 73 69 7a 65 6f 66 28 73 53 72 63 29 29  0, sizeof(sSrc))
1c01d 3b 0a 20 20 20 20 73 53 72 63 2e 6e 53 72 63 20  ;.    sSrc.nSrc 
1c01e 3d 20 31 3b 0a 20 20 20 20 73 53 72 63 2e 61 5b  = 1;.    sSrc.a[
1c01f 30 5d 2e 7a 4e 61 6d 65 20 3d 20 70 2d 3e 7a 4e  0].zName = p->zN
1c020 61 6d 65 3b 0a 20 20 20 20 73 53 72 63 2e 61 5b  ame;.    sSrc.a[
1c021 30 5d 2e 70 54 61 62 20 3d 20 70 3b 0a 20 20 20  0].pTab = p;.   
1c022 20 73 53 72 63 2e 61 5b 30 5d 2e 69 43 75 72 73   sSrc.a[0].iCurs
1c023 6f 72 20 3d 20 2d 31 3b 0a 20 20 20 20 73 4e 43  or = -1;.    sNC
1c024 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
1c025 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69  ;.    sNC.pSrcLi
1c026 73 74 20 3d 20 26 73 53 72 63 3b 0a 20 20 20 20  st = &sSrc;.    
1c027 73 4e 43 2e 69 73 43 68 65 63 6b 20 3d 20 31 3b  sNC.isCheck = 1;
1c028 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1c029 45 78 70 72 52 65 73 6f 6c 76 65 4e 61 6d 65 73  ExprResolveNames
1c02a 28 26 73 4e 43 2c 20 70 2d 3e 70 43 68 65 63 6b  (&sNC, p->pCheck
1c02b 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
1c02c 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  n;.    }.  }.#en
1c02d 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
1c02e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43  SQLITE_OMIT_CHEC
1c02f 4b 29 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74  K) */..  /* If t
1c030 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  he db->init.busy
1c031 20 69 73 20 31 20 69 74 20 6d 65 61 6e 73 20 77   is 1 it means w
1c032 65 20 61 72 65 20 72 65 61 64 69 6e 67 20 74 68  e are reading th
1c033 65 20 53 51 4c 20 6f 66 66 20 74 68 65 0a 20 20  e SQL off the.  
1c034 2a 2a 20 22 73 71 6c 69 74 65 5f 6d 61 73 74 65  ** "sqlite_maste
1c035 72 22 20 6f 72 20 22 73 71 6c 69 74 65 5f 74 65  r" or "sqlite_te
1c036 6d 70 5f 6d 61 73 74 65 72 22 20 74 61 62 6c 65  mp_master" table
1c037 20 6f 6e 20 74 68 65 20 64 69 73 6b 2e 0a 20 20   on the disk..  
1c038 2a 2a 20 53 6f 20 64 6f 20 6e 6f 74 20 77 72 69  ** So do not wri
1c039 74 65 20 74 6f 20 74 68 65 20 64 69 73 6b 20 61  te to the disk a
1c03a 67 61 69 6e 2e 20 20 45 78 74 72 61 63 74 20 74  gain.  Extract t
1c03b 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d  he root page num
1c03c 62 65 72 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65  ber.  ** for the
1c03d 20 74 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20   table from the 
1c03e 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e 75 6d  db->init.newTnum
1c03f 20 66 69 65 6c 64 2e 20 20 28 54 68 65 20 70 61   field.  (The pa
1c040 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 73  ge number.  ** s
1c041 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20  hould have been 
1c042 70 75 74 20 74 68 65 72 65 20 62 79 20 74 68 65  put there by the
1c043 20 73 71 6c 69 74 65 4f 70 65 6e 43 62 20 72 6f   sqliteOpenCb ro
1c044 75 74 69 6e 65 2e 29 0a 20 20 2a 2f 0a 20 20 69  utine.).  */.  i
1c045 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  f( db->init.busy
1c046 20 29 7b 0a 20 20 20 20 70 2d 3e 74 6e 75 6d 20   ){.    p->tnum 
1c047 3d 20 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e  = db->init.newTn
1c048 75 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  um;.  }..  /* If
1c049 20 6e 6f 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e   not initializin
1c04a 67 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 61  g, then create a
1c04b 20 72 65 63 6f 72 64 20 66 6f 72 20 74 68 65 20   record for the 
1c04c 6e 65 77 20 74 61 62 6c 65 0a 20 20 2a 2a 20 69  new table.  ** i
1c04d 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53  n the SQLITE_MAS
1c04e 54 45 52 20 74 61 62 6c 65 20 6f 66 20 74 68 65  TER table of the
1c04f 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 20   database.  The 
1c050 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 0a 20 20  record number.  
1c051 2a 2a 20 66 6f 72 20 74 68 65 20 6e 65 77 20 74  ** for the new t
1c052 61 62 6c 65 20 65 6e 74 72 79 20 73 68 6f 75 6c  able entry shoul
1c053 64 20 61 6c 72 65 61 64 79 20 62 65 20 6f 6e 20  d already be on 
1c054 74 68 65 20 73 74 61 63 6b 2e 0a 20 20 2a 2a 0a  the stack..  **.
1c055 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20    ** If this is 
1c056 61 20 54 45 4d 50 4f 52 41 52 59 20 74 61 62 6c  a TEMPORARY tabl
1c057 65 2c 20 77 72 69 74 65 20 74 68 65 20 65 6e 74  e, write the ent
1c058 72 79 20 69 6e 74 6f 20 74 68 65 20 61 75 78 69  ry into the auxi
1c059 6c 69 61 72 79 0a 20 20 2a 2a 20 66 69 6c 65 20  liary.  ** file 
1c05a 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 74 6f 20  instead of into 
1c05b 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
1c05c 65 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69  e file..  */.  i
1c05d 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73  f( !db->init.bus
1c05e 79 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a  y ){.    int n;.
1c05f 20 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20      Vdbe *v;.   
1c060 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 20 20   char *zType;   
1c061 20 2f 2a 20 22 76 69 65 77 22 20 6f 72 20 22 74   /* "view" or "t
1c062 61 62 6c 65 22 20 2a 2f 0a 20 20 20 20 63 68 61  able" */.    cha
1c063 72 20 2a 7a 54 79 70 65 32 3b 20 20 20 2f 2a 20  r *zType2;   /* 
1c064 22 56 49 45 57 22 20 6f 72 20 22 54 41 42 4c 45  "VIEW" or "TABLE
1c065 22 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a  " */.    char *z
1c066 53 74 6d 74 3b 20 20 20 20 2f 2a 20 54 65 78 74  Stmt;    /* Text
1c067 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 54   of the CREATE T
1c068 41 42 4c 45 20 6f 72 20 43 52 45 41 54 45 20 56  ABLE or CREATE V
1c069 49 45 57 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  IEW statement */
1c06a 0a 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65  ..    v = sqlite
1c06b 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
1c06c 3b 0a 20 20 20 20 69 66 28 20 76 3d 3d 30 20 29  ;.    if( v==0 )
1c06d 20 72 65 74 75 72 6e 3b 0a 0a 20 20 20 20 73 71   return;..    sq
1c06e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
1c06f 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 30 29 3b  v, OP_Close, 0);
1c070 0a 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20  ..    /* Create 
1c071 74 68 65 20 72 6f 6f 74 70 61 67 65 20 66 6f 72  the rootpage for
1c072 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 61   the new table a
1c073 6e 64 20 70 75 73 68 20 69 74 20 6f 6e 74 6f 20  nd push it onto 
1c074 74 68 65 20 73 74 61 63 6b 2e 0a 20 20 20 20 2a  the stack..    *
1c075 2a 20 41 20 76 69 65 77 20 68 61 73 20 6e 6f 20  * A view has no 
1c076 72 6f 6f 74 70 61 67 65 2c 20 73 6f 20 6a 75 73  rootpage, so jus
1c077 74 20 70 75 73 68 20 61 20 7a 65 72 6f 20 6f 6e  t push a zero on
1c078 74 6f 20 74 68 65 20 73 74 61 63 6b 20 66 6f 72  to the stack for
1c079 0a 20 20 20 20 2a 2a 20 76 69 65 77 73 2e 20 20  .    ** views.  
1c07a 49 6e 69 74 69 61 6c 69 7a 65 20 7a 54 79 70 65  Initialize zType
1c07b 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   at the same tim
1c07c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  e..    */.    if
1c07d 28 20 70 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20  ( p->pSelect==0 
1c07e 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 72 65  ){.      /* A re
1c07f 67 75 6c 61 72 20 74 61 62 6c 65 20 2a 2f 0a 20  gular table */. 
1c080 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 74 61       zType = "ta
1c081 62 6c 65 22 3b 0a 20 20 20 20 20 20 7a 54 79 70  ble";.      zTyp
1c082 65 32 20 3d 20 22 54 41 42 4c 45 22 3b 0a 23 69  e2 = "TABLE";.#i
1c083 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1c084 54 5f 56 49 45 57 0a 20 20 20 20 7d 65 6c 73 65  T_VIEW.    }else
1c085 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 76 69 65  {.      /* A vie
1c086 77 20 2a 2f 0a 20 20 20 20 20 20 7a 54 79 70 65  w */.      zType
1c087 20 3d 20 22 76 69 65 77 22 3b 0a 20 20 20 20 20   = "view";.     
1c088 20 7a 54 79 70 65 32 20 3d 20 22 56 49 45 57 22   zType2 = "VIEW"
1c089 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 0a  ;.#endif.    }..
1c08a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
1c08b 73 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45  s a CREATE TABLE
1c08c 20 78 78 20 41 53 20 53 45 4c 45 43 54 20 2e 2e   xx AS SELECT ..
1c08d 2e 2c 20 65 78 65 63 75 74 65 20 74 68 65 20 53  ., execute the S
1c08e 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 73 74 61  ELECT.    ** sta
1c08f 74 65 6d 65 6e 74 20 74 6f 20 70 6f 70 75 6c 61  tement to popula
1c090 74 65 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  te the new table
1c091 2e 20 54 68 65 20 72 6f 6f 74 2d 70 61 67 65 20  . The root-page 
1c092 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 0a 20  number for the. 
1c093 20 20 20 2a 2a 20 6e 65 77 20 74 61 62 6c 65 20     ** new table 
1c094 69 73 20 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66  is on the top of
1c095 20 74 68 65 20 76 64 62 65 20 73 74 61 63 6b 2e   the vdbe stack.
1c096 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f  .    **.    ** O
1c097 6e 63 65 20 74 68 65 20 53 45 4c 45 43 54 20 68  nce the SELECT h
1c098 61 73 20 62 65 65 6e 20 63 6f 64 65 64 20 62 79  as been coded by
1c099 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29   sqlite3Select()
1c09a 2c 20 69 74 20 69 73 20 69 6e 20 61 0a 20 20 20  , it is in a.   
1c09b 20 2a 2a 20 73 75 69 74 61 62 6c 65 20 73 74 61   ** suitable sta
1c09c 74 65 20 74 6f 20 71 75 65 72 79 20 66 6f 72 20  te to query for 
1c09d 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  the column names
1c09e 20 61 6e 64 20 74 79 70 65 73 20 74 6f 20 62 65   and types to be
1c09f 20 75 73 65 64 0a 20 20 20 20 2a 2a 20 62 79 20   used.    ** by 
1c0a0 74 68 65 20 6e 65 77 20 74 61 62 6c 65 2e 0a 20  the new table.. 
1c0a1 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 20 73     **.    ** A s
1c0a2 68 61 72 65 64 2d 63 61 63 68 65 20 77 72 69 74  hared-cache writ
1c0a3 65 2d 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 72 65  e-lock is not re
1c0a4 71 75 69 72 65 64 20 74 6f 20 77 72 69 74 65 20  quired to write 
1c0a5 74 6f 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  to the new table
1c0a6 2c 0a 20 20 20 20 2a 2a 20 61 73 20 61 20 73 63  ,.    ** as a sc
1c0a7 68 65 6d 61 2d 6c 6f 63 6b 20 6d 75 73 74 20 68  hema-lock must h
1c0a8 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e  ave already been
1c0a9 20 6f 62 74 61 69 6e 65 64 20 74 6f 20 63 72 65   obtained to cre
1c0aa 61 74 65 20 69 74 2e 20 53 69 6e 63 65 0a 20 20  ate it. Since.  
1c0ab 20 20 2a 2a 20 61 20 73 63 68 65 6d 61 2d 6c 6f    ** a schema-lo
1c0ac 63 6b 20 65 78 63 6c 75 64 65 73 20 61 6c 6c 20  ck excludes all 
1c0ad 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 75  other database u
1c0ae 73 65 72 73 2c 20 74 68 65 20 77 72 69 74 65 2d  sers, the write-
1c0af 6c 6f 63 6b 20 77 6f 75 6c 64 0a 20 20 20 20 2a  lock would.    *
1c0b0 2a 20 62 65 20 72 65 64 75 6e 64 61 6e 74 2e 0a  * be redundant..
1c0b1 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
1c0b2 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20  Select ){.      
1c0b3 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b  SelectDest dest;
1c0b4 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 53  .      Table *pS
1c0b5 65 6c 54 61 62 3b 0a 0a 20 20 20 20 20 20 73 71  elTab;..      sq
1c0b6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
1c0b7 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c  v, OP_OpenWrite,
1c0b8 20 31 2c 20 70 50 61 72 73 65 2d 3e 72 65 67 52   1, pParse->regR
1c0b9 6f 6f 74 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  oot, iDb);.     
1c0ba 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1c0bb 67 65 50 35 28 76 2c 20 31 29 3b 0a 20 20 20 20  geP5(v, 1);.    
1c0bc 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d    pParse->nTab =
1c0bd 20 32 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   2;.      sqlite
1c0be 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28  3SelectDestInit(
1c0bf 26 64 65 73 74 2c 20 53 52 54 5f 54 61 62 6c 65  &dest, SRT_Table
1c0c0 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
1c0c1 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
1c0c2 2c 20 70 53 65 6c 65 63 74 2c 20 26 64 65 73 74  , pSelect, &dest
1c0c3 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  , 0, 0, 0, 0);. 
1c0c4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1c0c5 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp1(v, OP_Clo
1c0c6 73 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 69 66  se, 1);.      if
1c0c7 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d  ( pParse->nErr==
1c0c8 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 53 65  0 ){.        pSe
1c0c9 6c 54 61 62 20 3d 20 73 71 6c 69 74 65 33 52 65  lTab = sqlite3Re
1c0ca 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28  sultSetOfSelect(
1c0cb 70 50 61 72 73 65 2c 20 30 2c 20 70 53 65 6c 65  pParse, 0, pSele
1c0cc 63 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ct);.        if(
1c0cd 20 70 53 65 6c 54 61 62 3d 3d 30 20 29 20 72 65   pSelTab==0 ) re
1c0ce 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 61 73  turn;.        as
1c0cf 73 65 72 74 28 20 70 2d 3e 61 43 6f 6c 3d 3d 30  sert( p->aCol==0
1c0d0 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e   );.        p->n
1c0d1 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e 6e  Col = pSelTab->n
1c0d2 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  Col;.        p->
1c0d3 61 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e  aCol = pSelTab->
1c0d4 61 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 53  aCol;.        pS
1c0d5 65 6c 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 30 3b  elTab->nCol = 0;
1c0d6 0a 20 20 20 20 20 20 20 20 70 53 65 6c 54 61 62  .        pSelTab
1c0d7 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20  ->aCol = 0;.    
1c0d8 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74      sqlite3Delet
1c0d9 65 54 61 62 6c 65 28 70 53 65 6c 54 61 62 29 3b  eTable(pSelTab);
1c0da 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
1c0db 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74      /* Compute t
1c0dc 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74  he complete text
1c0dd 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 73   of the CREATE s
1c0de 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20  tatement */.    
1c0df 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20  if( pSelect ){. 
1c0e0 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 63 72 65       zStmt = cre
1c0e1 61 74 65 54 61 62 6c 65 53 74 6d 74 28 64 62 2c  ateTableStmt(db,
1c0e2 20 70 2c 20 70 2d 3e 70 53 63 68 65 6d 61 3d 3d   p, p->pSchema==
1c0e3 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65  db->aDb[1].pSche
1c0e4 6d 61 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ma);.    }else{.
1c0e5 20 20 20 20 20 20 6e 20 3d 20 70 45 6e 64 2d 3e        n = pEnd->
1c0e6 7a 20 2d 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d  z - pParse->sNam
1c0e7 65 54 6f 6b 65 6e 2e 7a 20 2b 20 31 3b 0a 20 20  eToken.z + 1;.  
1c0e8 20 20 20 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69      zStmt = sqli
1c0e9 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 0a  te3MPrintf(db, .
1c0ea 20 20 20 20 20 20 20 20 20 20 22 43 52 45 41 54            "CREAT
1c0eb 45 20 25 73 20 25 2e 2a 73 22 2c 20 7a 54 79 70  E %s %.*s", zTyp
1c0ec 65 32 2c 20 6e 2c 20 70 50 61 72 73 65 2d 3e 73  e2, n, pParse->s
1c0ed 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 0a 20 20 20 20  NameToken.z.    
1c0ee 20 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20    );.    }..    
1c0ef 2f 2a 20 41 20 73 6c 6f 74 20 66 6f 72 20 74 68  /* A slot for th
1c0f0 65 20 72 65 63 6f 72 64 20 68 61 73 20 61 6c 72  e record has alr
1c0f1 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61  eady been alloca
1c0f2 74 65 64 20 69 6e 20 74 68 65 20 0a 20 20 20 20  ted in the .    
1c0f3 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52  ** SQLITE_MASTER
1c0f4 20 74 61 62 6c 65 2e 20 20 57 65 20 6a 75 73 74   table.  We just
1c0f5 20 6e 65 65 64 20 74 6f 20 75 70 64 61 74 65 20   need to update 
1c0f6 74 68 61 74 20 73 6c 6f 74 20 77 69 74 68 20 61  that slot with a
1c0f7 6c 6c 0a 20 20 20 20 2a 2a 20 74 68 65 20 69 6e  ll.    ** the in
1c0f8 66 6f 72 6d 61 74 69 6f 6e 20 77 65 27 76 65 20  formation we've 
1c0f9 63 6f 6c 6c 65 63 74 65 64 2e 20 20 54 68 65 20  collected.  The 
1c0fa 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 70 72  rowid for the pr
1c0fb 65 61 6c 6c 6f 63 61 74 65 64 0a 20 20 20 20 2a  eallocated.    *
1c0fc 2a 20 73 6c 6f 74 20 69 73 20 74 68 65 20 32 6e  * slot is the 2n
1c0fd 64 20 69 74 65 6d 20 6f 6e 20 74 68 65 20 73 74  d item on the st
1c0fe 61 63 6b 2e 20 20 54 68 65 20 74 6f 70 20 6f 66  ack.  The top of
1c0ff 20 74 68 65 20 73 74 61 63 6b 20 69 73 20 74 68   the stack is th
1c100 65 0a 20 20 20 20 2a 2a 20 72 6f 6f 74 20 70 61  e.    ** root pa
1c101 67 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 74  ge for the new t
1c102 61 62 6c 65 20 28 6f 72 20 61 20 30 20 69 66 20  able (or a 0 if 
1c103 74 68 69 73 20 69 73 20 61 20 76 69 65 77 29 2e  this is a view).
1c104 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
1c105 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70  te3NestedParse(p
1c106 50 61 72 73 65 2c 0a 20 20 20 20 20 20 22 55 50  Parse,.      "UP
1c107 44 41 54 45 20 25 51 2e 25 73 20 22 0a 20 20 20  DATE %Q.%s ".   
1c108 20 20 20 20 20 20 22 53 45 54 20 74 79 70 65 3d        "SET type=
1c109 27 25 73 27 2c 20 6e 61 6d 65 3d 25 51 2c 20 74  '%s', name=%Q, t
1c10a 62 6c 5f 6e 61 6d 65 3d 25 51 2c 20 72 6f 6f 74  bl_name=%Q, root
1c10b 70 61 67 65 3d 23 25 64 2c 20 73 71 6c 3d 25 51  page=#%d, sql=%Q
1c10c 20 22 0a 20 20 20 20 20 20 20 22 57 48 45 52 45   ".       "WHERE
1c10d 20 72 6f 77 69 64 3d 23 25 64 22 2c 0a 20 20 20   rowid=#%d",.   
1c10e 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e     db->aDb[iDb].
1c10f 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41  zName, SCHEMA_TA
1c110 42 4c 45 28 69 44 62 29 2c 0a 20 20 20 20 20 20  BLE(iDb),.      
1c111 7a 54 79 70 65 2c 0a 20 20 20 20 20 20 70 2d 3e  zType,.      p->
1c112 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 70 2d 3e  zName,.      p->
1c113 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 70 50 61  zName,.      pPa
1c114 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2c 0a 20 20  rse->regRoot,.  
1c115 20 20 20 20 7a 53 74 6d 74 2c 0a 20 20 20 20 20      zStmt,.     
1c116 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 77 69   pParse->regRowi
1c117 64 0a 20 20 20 20 29 3b 0a 20 20 20 20 73 71 6c  d.    );.    sql
1c118 69 74 65 33 5f 66 72 65 65 28 7a 53 74 6d 74 29  ite3_free(zStmt)
1c119 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61  ;.    sqlite3Cha
1c11a 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65  ngeCookie(pParse
1c11b 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66  , iDb);..#ifndef
1c11c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
1c11d 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 2f  OINCREMENT.    /
1c11e 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
1c11f 66 20 77 65 20 6e 65 65 64 20 74 6f 20 63 72 65  f we need to cre
1c120 61 74 65 20 61 6e 20 73 71 6c 69 74 65 5f 73 65  ate an sqlite_se
1c121 71 75 65 6e 63 65 20 74 61 62 6c 65 20 66 6f 72  quence table for
1c122 0a 20 20 20 20 2a 2a 20 6b 65 65 70 69 6e 67 20  .    ** keeping 
1c123 74 72 61 63 6b 20 6f 66 20 61 75 74 6f 69 6e 63  track of autoinc
1c124 72 65 6d 65 6e 74 20 6b 65 79 73 2e 0a 20 20 20  rement keys..   
1c125 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 61   */.    if( p->a
1c126 75 74 6f 49 6e 63 20 29 7b 0a 20 20 20 20 20 20  utoInc ){.      
1c127 44 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61  Db *pDb = &db->a
1c128 44 62 5b 69 44 62 5d 3b 0a 20 20 20 20 20 20 69  Db[iDb];.      i
1c129 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  f( pDb->pSchema-
1c12a 3e 70 53 65 71 54 61 62 3d 3d 30 20 29 7b 0a 20  >pSeqTab==0 ){. 
1c12b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e 65         sqlite3Ne
1c12c 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
1c12d 2c 0a 20 20 20 20 20 20 20 20 20 20 22 43 52 45  ,.          "CRE
1c12e 41 54 45 20 54 41 42 4c 45 20 25 51 2e 73 71 6c  ATE TABLE %Q.sql
1c12f 69 74 65 5f 73 65 71 75 65 6e 63 65 28 6e 61 6d  ite_sequence(nam
1c130 65 2c 73 65 71 29 22 2c 0a 20 20 20 20 20 20 20  e,seq)",.       
1c131 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 0a 20 20     pDb->zName.  
1c132 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 7d        );.      }
1c133 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  .    }.#endif.. 
1c134 20 20 20 2f 2a 20 52 65 70 61 72 73 65 20 65 76     /* Reparse ev
1c135 65 72 79 74 68 69 6e 67 20 74 6f 20 75 70 64 61  erything to upda
1c136 74 65 20 6f 75 72 20 69 6e 74 65 72 6e 61 6c 20  te our internal 
1c137 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20  data structures 
1c138 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  */.    sqlite3Vd
1c139 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50  beAddOp4(v, OP_P
1c13a 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44 62 2c  arseSchema, iDb,
1c13b 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 73   0, 0,.        s
1c13c 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
1c13d 2c 20 22 74 62 6c 5f 6e 61 6d 65 3d 27 25 71 27  , "tbl_name='%q'
1c13e 22 2c 70 2d 3e 7a 4e 61 6d 65 29 2c 20 50 34 5f  ",p->zName), P4_
1c13f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 0a 0a  DYNAMIC);.  }...
1c140 20 20 2f 2a 20 41 64 64 20 74 68 65 20 74 61 62    /* Add the tab
1c141 6c 65 20 74 6f 20 74 68 65 20 69 6e 2d 6d 65 6d  le to the in-mem
1c142 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69  ory representati
1c143 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  on of the databa
1c144 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64  se..  */.  if( d
1c145 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20  b->init.busy && 
1c146 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20  pParse->nErr==0 
1c147 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 4f  ){.    Table *pO
1c148 6c 64 3b 0a 20 20 20 20 46 4b 65 79 20 2a 70 46  ld;.    FKey *pF
1c149 4b 65 79 3b 20 0a 20 20 20 20 53 63 68 65 6d 61  Key; .    Schema
1c14a 20 2a 70 53 63 68 65 6d 61 20 3d 20 70 2d 3e 70   *pSchema = p->p
1c14b 53 63 68 65 6d 61 3b 0a 20 20 20 20 70 4f 6c 64  Schema;.    pOld
1c14c 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e   = sqlite3HashIn
1c14d 73 65 72 74 28 26 70 53 63 68 65 6d 61 2d 3e 74  sert(&pSchema->t
1c14e 62 6c 48 61 73 68 2c 20 70 2d 3e 7a 4e 61 6d 65  blHash, p->zName
1c14f 2c 20 73 74 72 6c 65 6e 28 70 2d 3e 7a 4e 61 6d  , strlen(p->zNam
1c150 65 29 2b 31 2c 70 29 3b 0a 20 20 20 20 69 66 28  e)+1,p);.    if(
1c151 20 70 4f 6c 64 20 29 7b 0a 20 20 20 20 20 20 61   pOld ){.      a
1c152 73 73 65 72 74 28 20 70 3d 3d 70 4f 6c 64 20 29  ssert( p==pOld )
1c153 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 75 73  ;  /* Malloc mus
1c154 74 20 68 61 76 65 20 66 61 69 6c 65 64 20 69 6e  t have failed in
1c155 73 69 64 65 20 48 61 73 68 49 6e 73 65 72 74 28  side HashInsert(
1c156 29 20 2a 2f 0a 20 20 20 20 20 20 64 62 2d 3e 6d  ) */.      db->m
1c157 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
1c158 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
1c159 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
1c15a 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e  ITE_OMIT_FOREIGN
1c15b 5f 4b 45 59 0a 20 20 20 20 66 6f 72 28 70 46 4b  _KEY.    for(pFK
1c15c 65 79 3d 70 2d 3e 70 46 4b 65 79 3b 20 70 46 4b  ey=p->pFKey; pFK
1c15d 65 79 3b 20 70 46 4b 65 79 3d 70 46 4b 65 79 2d  ey; pFKey=pFKey-
1c15e 3e 70 4e 65 78 74 46 72 6f 6d 29 7b 0a 20 20 20  >pNextFrom){.   
1c15f 20 20 20 76 6f 69 64 20 2a 64 61 74 61 3b 0a 20     void *data;. 
1c160 20 20 20 20 20 69 6e 74 20 6e 54 6f 20 3d 20 73       int nTo = s
1c161 74 72 6c 65 6e 28 70 46 4b 65 79 2d 3e 7a 54 6f  trlen(pFKey->zTo
1c162 29 20 2b 20 31 3b 0a 20 20 20 20 20 20 70 46 4b  ) + 1;.      pFK
1c163 65 79 2d 3e 70 4e 65 78 74 54 6f 20 3d 20 73 71  ey->pNextTo = sq
1c164 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 70  lite3HashFind(&p
1c165 53 63 68 65 6d 61 2d 3e 61 46 4b 65 79 2c 20 70  Schema->aFKey, p
1c166 46 4b 65 79 2d 3e 7a 54 6f 2c 20 6e 54 6f 29 3b  FKey->zTo, nTo);
1c167 0a 20 20 20 20 20 20 64 61 74 61 20 3d 20 73 71  .      data = sq
1c168 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
1c169 26 70 53 63 68 65 6d 61 2d 3e 61 46 4b 65 79 2c  &pSchema->aFKey,
1c16a 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20 6e 54 6f   pFKey->zTo, nTo
1c16b 2c 20 70 46 4b 65 79 29 3b 0a 20 20 20 20 20 20  , pFKey);.      
1c16c 69 66 28 20 64 61 74 61 3d 3d 28 76 6f 69 64 20  if( data==(void 
1c16d 2a 29 70 46 4b 65 79 20 29 7b 0a 20 20 20 20 20  *)pFKey ){.     
1c16e 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
1c16f 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  led = 1;.      }
1c170 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
1c171 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61    pParse->pNewTa
1c172 62 6c 65 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d  ble = 0;.    db-
1c173 3e 6e 54 61 62 6c 65 2b 2b 3b 0a 20 20 20 20 64  >nTable++;.    d
1c174 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49  b->flags |= SQLI
1c175 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73  TE_InternChanges
1c176 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
1c177 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c  E_OMIT_ALTERTABL
1c178 45 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 70 53  E.    if( !p->pS
1c179 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 63  elect ){.      c
1c17a 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
1c17b 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
1c17c 29 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f  )pParse->sNameTo
1c17d 6b 65 6e 2e 7a 3b 0a 20 20 20 20 20 20 69 6e 74  ken.z;.      int
1c17e 20 6e 4e 61 6d 65 3b 0a 20 20 20 20 20 20 61 73   nName;.      as
1c17f 73 65 72 74 28 20 21 70 53 65 6c 65 63 74 20 26  sert( !pSelect &
1c180 26 20 70 43 6f 6e 73 20 26 26 20 70 45 6e 64 20  & pCons && pEnd 
1c181 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 43 6f  );.      if( pCo
1c182 6e 73 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20  ns->z==0 ){.    
1c183 20 20 20 20 70 43 6f 6e 73 20 3d 20 70 45 6e 64      pCons = pEnd
1c184 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1c185 6e 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63  nName = (const c
1c186 68 61 72 20 2a 29 70 43 6f 6e 73 2d 3e 7a 20 2d  har *)pCons->z -
1c187 20 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 70 2d   zName;.      p-
1c188 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 20 3d 20  >addColOffset = 
1c189 31 33 20 2b 20 73 71 6c 69 74 65 33 55 74 66 38  13 + sqlite3Utf8
1c18a 43 68 61 72 4c 65 6e 28 7a 4e 61 6d 65 2c 20 6e  CharLen(zName, n
1c18b 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  Name);.    }.#en
1c18c 64 69 66 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64  dif.  }.}..#ifnd
1c18d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
1c18e 49 45 57 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61  IEW./*.** The pa
1c18f 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20  rser calls this 
1c190 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 72  routine in order
1c191 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77   to create a new
1c192 20 56 49 45 57 0a 2a 2f 0a 53 51 4c 49 54 45 5f   VIEW.*/.SQLITE_
1c193 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1c194 69 74 65 33 43 72 65 61 74 65 56 69 65 77 28 0a  ite3CreateView(.
1c195 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
1c196 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
1c197 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
1c198 20 54 6f 6b 65 6e 20 2a 70 42 65 67 69 6e 2c 20   Token *pBegin, 
1c199 20 20 20 20 2f 2a 20 54 68 65 20 43 52 45 41 54      /* The CREAT
1c19a 45 20 74 6f 6b 65 6e 20 74 68 61 74 20 62 65 67  E token that beg
1c19b 69 6e 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e  ins the statemen
1c19c 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e  t */.  Token *pN
1c19d 61 6d 65 31 2c 20 20 20 20 20 2f 2a 20 54 68 65  ame1,     /* The
1c19e 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 6f 6c 64   token that hold
1c19f 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
1c1a0 65 20 76 69 65 77 20 2a 2f 0a 20 20 54 6f 6b 65  e view */.  Toke
1c1a1 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 2f  n *pName2,     /
1c1a2 2a 20 54 68 65 20 74 6f 6b 65 6e 20 74 68 61 74  * The token that
1c1a3 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 20   holds the name 
1c1a4 6f 66 20 74 68 65 20 76 69 65 77 20 2a 2f 0a 20  of the view */. 
1c1a5 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
1c1a6 2c 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20  ,   /* A SELECT 
1c1a7 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 77  statement that w
1c1a8 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 6e  ill become the n
1c1a9 65 77 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74  ew view */.  int
1c1aa 20 69 73 54 65 6d 70 2c 20 20 20 20 20 20 20 20   isTemp,        
1c1ab 2f 2a 20 54 52 55 45 20 66 6f 72 20 61 20 54 45  /* TRUE for a TE
1c1ac 4d 50 4f 52 41 52 59 20 76 69 65 77 20 2a 2f 0a  MPORARY view */.
1c1ad 20 20 69 6e 74 20 6e 6f 45 72 72 20 20 20 20 20    int noErr     
1c1ae 20 20 20 20 20 2f 2a 20 53 75 70 70 72 65 73 73       /* Suppress
1c1af 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20   error messages 
1c1b0 69 66 20 56 49 45 57 20 61 6c 72 65 61 64 79 20  if VIEW already 
1c1b1 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 20 54  exists */.){.  T
1c1b2 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20 6e  able *p;.  int n
1c1b3 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ;.  const unsign
1c1b4 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 54 6f  ed char *z;.  To
1c1b5 6b 65 6e 20 73 45 6e 64 3b 0a 20 20 44 62 46 69  ken sEnd;.  DbFi
1c1b6 78 65 72 20 73 46 69 78 3b 0a 20 20 54 6f 6b 65  xer sFix;.  Toke
1c1b7 6e 20 2a 70 4e 61 6d 65 3b 0a 20 20 69 6e 74 20  n *pName;.  int 
1c1b8 69 44 62 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  iDb;.  sqlite3 *
1c1b9 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
1c1ba 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ..  if( pParse->
1c1bb 6e 56 61 72 3e 30 20 29 7b 0a 20 20 20 20 73 71  nVar>0 ){.    sq
1c1bc 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1c1bd 61 72 73 65 2c 20 22 70 61 72 61 6d 65 74 65 72  arse, "parameter
1c1be 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65  s are not allowe
1c1bf 64 20 69 6e 20 76 69 65 77 73 22 29 3b 0a 20 20  d in views");.  
1c1c0 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
1c1c1 65 6c 65 74 65 28 70 53 65 6c 65 63 74 29 3b 0a  elete(pSelect);.
1c1c2 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
1c1c3 20 20 73 71 6c 69 74 65 33 53 74 61 72 74 54 61    sqlite3StartTa
1c1c4 62 6c 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d  ble(pParse, pNam
1c1c5 65 31 2c 20 70 4e 61 6d 65 32 2c 20 69 73 54 65  e1, pName2, isTe
1c1c6 6d 70 2c 20 31 2c 20 30 2c 20 6e 6f 45 72 72 29  mp, 1, 0, noErr)
1c1c7 3b 0a 20 20 70 20 3d 20 70 50 61 72 73 65 2d 3e  ;.  p = pParse->
1c1c8 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28  pNewTable;.  if(
1c1c9 20 70 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65 2d   p==0 || pParse-
1c1ca 3e 6e 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c  >nErr ){.    sql
1c1cb 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
1c1cc 28 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 72  (pSelect);.    r
1c1cd 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c  eturn;.  }.  sql
1c1ce 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28  ite3TwoPartName(
1c1cf 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20  pParse, pName1, 
1c1d0 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 65 29 3b  pName2, &pName);
1c1d1 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  .  iDb = sqlite3
1c1d2 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62  SchemaToIndex(db
1c1d3 2c 20 70 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  , p->pSchema);. 
1c1d4 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 49   if( sqlite3FixI
1c1d5 6e 69 74 28 26 73 46 69 78 2c 20 70 50 61 72 73  nit(&sFix, pPars
1c1d6 65 2c 20 69 44 62 2c 20 22 76 69 65 77 22 2c 20  e, iDb, "view", 
1c1d7 70 4e 61 6d 65 29 0a 20 20 20 20 26 26 20 73 71  pName).    && sq
1c1d8 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 26  lite3FixSelect(&
1c1d9 73 46 69 78 2c 20 70 53 65 6c 65 63 74 29 0a 20  sFix, pSelect). 
1c1da 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
1c1db 65 6c 65 63 74 44 65 6c 65 74 65 28 70 53 65 6c  electDelete(pSel
1c1dc 65 63 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ect);.    return
1c1dd 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65  ;.  }..  /* Make
1c1de 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65   a copy of the e
1c1df 6e 74 69 72 65 20 53 45 4c 45 43 54 20 73 74 61  ntire SELECT sta
1c1e0 74 65 6d 65 6e 74 20 74 68 61 74 20 64 65 66 69  tement that defi
1c1e1 6e 65 73 20 74 68 65 20 76 69 65 77 2e 0a 20 20  nes the view..  
1c1e2 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 66 6f 72  ** This will for
1c1e3 63 65 20 61 6c 6c 20 74 68 65 20 45 78 70 72 2e  ce all the Expr.
1c1e4 74 6f 6b 65 6e 2e 7a 20 76 61 6c 75 65 73 20 74  token.z values t
1c1e5 6f 20 62 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79  o be dynamically
1c1e6 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20  .  ** allocated 
1c1e7 72 61 74 68 65 72 20 74 68 61 6e 20 70 6f 69 6e  rather than poin
1c1e8 74 20 74 6f 20 74 68 65 20 69 6e 70 75 74 20 73  t to the input s
1c1e9 74 72 69 6e 67 20 2d 20 77 68 69 63 68 20 6d 65  tring - which me
1c1ea 61 6e 73 20 74 68 61 74 0a 20 20 2a 2a 20 74 68  ans that.  ** th
1c1eb 65 79 20 77 69 6c 6c 20 70 65 72 73 69 73 74 20  ey will persist 
1c1ec 61 66 74 65 72 20 74 68 65 20 63 75 72 72 65 6e  after the curren
1c1ed 74 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29  t sqlite3_exec()
1c1ee 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e 0a 20   call returns.. 
1c1ef 20 2a 2f 0a 20 20 70 2d 3e 70 53 65 6c 65 63 74   */.  p->pSelect
1c1f0 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
1c1f1 44 75 70 28 64 62 2c 20 70 53 65 6c 65 63 74 29  Dup(db, pSelect)
1c1f2 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ;.  sqlite3Selec
1c1f3 74 44 65 6c 65 74 65 28 70 53 65 6c 65 63 74 29  tDelete(pSelect)
1c1f4 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  ;.  if( db->mall
1c1f5 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
1c1f6 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66  return;.  }.  if
1c1f7 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  ( !db->init.busy
1c1f8 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1c1f9 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65  iewGetColumnName
1c1fa 73 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20  s(pParse, p);.  
1c1fb 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74  }..  /* Locate t
1c1fc 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 43 52  he end of the CR
1c1fd 45 41 54 45 20 56 49 45 57 20 73 74 61 74 65 6d  EATE VIEW statem
1c1fe 65 6e 74 2e 20 20 4d 61 6b 65 20 73 45 6e 64 20  ent.  Make sEnd 
1c1ff 70 6f 69 6e 74 20 74 6f 0a 20 20 2a 2a 20 74 68  point to.  ** th
1c200 65 20 65 6e 64 2e 0a 20 20 2a 2f 0a 20 20 73 45  e end..  */.  sE
1c201 6e 64 20 3d 20 70 50 61 72 73 65 2d 3e 73 4c 61  nd = pParse->sLa
1c202 73 74 54 6f 6b 65 6e 3b 0a 20 20 69 66 28 20 73  stToken;.  if( s
1c203 45 6e 64 2e 7a 5b 30 5d 21 3d 30 20 26 26 20 73  End.z[0]!=0 && s
1c204 45 6e 64 2e 7a 5b 30 5d 21 3d 27 3b 27 20 29 7b  End.z[0]!=';' ){
1c205 0a 20 20 20 20 73 45 6e 64 2e 7a 20 2b 3d 20 73  .    sEnd.z += s
1c206 45 6e 64 2e 6e 3b 0a 20 20 7d 0a 20 20 73 45 6e  End.n;.  }.  sEn
1c207 64 2e 6e 20 3d 20 30 3b 0a 20 20 6e 20 3d 20 73  d.n = 0;.  n = s
1c208 45 6e 64 2e 7a 20 2d 20 70 42 65 67 69 6e 2d 3e  End.z - pBegin->
1c209 7a 3b 0a 20 20 7a 20 3d 20 28 63 6f 6e 73 74 20  z;.  z = (const 
1c20a 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 70  unsigned char*)p
1c20b 42 65 67 69 6e 2d 3e 7a 3b 0a 20 20 77 68 69 6c  Begin->z;.  whil
1c20c 65 28 20 6e 3e 30 20 26 26 20 28 7a 5b 6e 2d 31  e( n>0 && (z[n-1
1c20d 5d 3d 3d 27 3b 27 20 7c 7c 20 69 73 73 70 61 63  ]==';' || isspac
1c20e 65 28 7a 5b 6e 2d 31 5d 29 29 20 29 7b 20 6e 2d  e(z[n-1])) ){ n-
1c20f 2d 3b 20 7d 0a 20 20 73 45 6e 64 2e 7a 20 3d 20  -; }.  sEnd.z = 
1c210 26 7a 5b 6e 2d 31 5d 3b 0a 20 20 73 45 6e 64 2e  &z[n-1];.  sEnd.
1c211 6e 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 55 73 65  n = 1;..  /* Use
1c212 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65   sqlite3EndTable
1c213 28 29 20 74 6f 20 61 64 64 20 74 68 65 20 76 69  () to add the vi
1c214 65 77 20 74 6f 20 74 68 65 20 53 51 4c 49 54 45  ew to the SQLITE
1c215 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 2a 2f  _MASTER table */
1c216 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62  .  sqlite3EndTab
1c217 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 26 73  le(pParse, 0, &s
1c218 45 6e 64 2c 20 30 29 3b 0a 20 20 72 65 74 75 72  End, 0);.  retur
1c219 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  n;.}.#endif /* S
1c21a 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20  QLITE_OMIT_VIEW 
1c21b 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  */..#if !defined
1c21c 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45  (SQLITE_OMIT_VIE
1c21d 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53  W) || !defined(S
1c21e 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1c21f 41 4c 54 41 42 4c 45 29 0a 2f 2a 0a 2a 2a 20 54  ALTABLE)./*.** T
1c220 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75  he Table structu
1c221 72 65 20 70 54 61 62 6c 65 20 69 73 20 72 65 61  re pTable is rea
1c222 6c 6c 79 20 61 20 56 49 45 57 2e 20 20 46 69 6c  lly a VIEW.  Fil
1c223 6c 20 69 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f  l in the names o
1c224 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 73  f.** the columns
1c225 20 6f 66 20 74 68 65 20 76 69 65 77 20 69 6e 20   of the view in 
1c226 74 68 65 20 70 54 61 62 6c 65 20 73 74 72 75 63  the pTable struc
1c227 74 75 72 65 2e 20 20 52 65 74 75 72 6e 20 74 68  ture.  Return th
1c228 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 65  e number.** of e
1c229 72 72 6f 72 73 2e 20 20 49 66 20 61 6e 20 65 72  rrors.  If an er
1c22a 72 6f 72 20 69 73 20 73 65 65 6e 20 6c 65 61 76  ror is seen leav
1c22b 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  e an error messa
1c22c 67 65 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45  ge in pParse->zE
1c22d 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45  rrMsg..*/.SQLITE
1c22e 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1c22f 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d  ite3ViewGetColum
1c230 6e 4e 61 6d 65 73 28 50 61 72 73 65 20 2a 70 50  nNames(Parse *pP
1c231 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61  arse, Table *pTa
1c232 62 6c 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  ble){.  Table *p
1c233 53 65 6c 54 61 62 3b 20 20 20 2f 2a 20 41 20 66  SelTab;   /* A f
1c234 61 6b 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77  ake table from w
1c235 68 69 63 68 20 77 65 20 67 65 74 20 74 68 65 20  hich we get the 
1c236 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20  result set */.  
1c237 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20 20 20  Select *pSel;   
1c238 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 74 68 65    /* Copy of the
1c239 20 53 45 4c 45 43 54 20 74 68 61 74 20 69 6d 70   SELECT that imp
1c23a 6c 65 6d 65 6e 74 73 20 74 68 65 20 76 69 65 77  lements the view
1c23b 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 20 3d   */.  int nErr =
1c23c 20 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65   0;     /* Numbe
1c23d 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 6f  r of errors enco
1c23e 75 6e 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e 74  untered */.  int
1c23f 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f   n;            /
1c240 2a 20 54 65 6d 70 6f 72 61 72 69 6c 79 20 68 6f  * Temporarily ho
1c241 6c 64 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  lds the number o
1c242 66 20 63 75 72 73 6f 72 73 20 61 73 73 69 67 6e  f cursors assign
1c243 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  ed */.  sqlite3 
1c244 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
1c245 3b 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63  ;  /* Database c
1c246 6f 6e 6e 65 63 74 69 6f 6e 20 66 6f 72 20 6d 61  onnection for ma
1c247 6c 6c 6f 63 20 65 72 72 6f 72 73 20 2a 2f 0a 20  lloc errors */. 
1c248 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f   int (*xAuth)(vo
1c249 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68  id*,int,const ch
1c24a 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ar*,const char*,
1c24b 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
1c24c 74 20 63 68 61 72 2a 29 3b 0a 0a 20 20 61 73 73  t char*);..  ass
1c24d 65 72 74 28 20 70 54 61 62 6c 65 20 29 3b 0a 0a  ert( pTable );..
1c24e 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1c24f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1c250 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 74  .  if( sqlite3Vt
1c251 61 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 70 50  abCallConnect(pP
1c252 61 72 73 65 2c 20 70 54 61 62 6c 65 29 20 29 7b  arse, pTable) ){
1c253 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1c254 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
1c255 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
1c256 61 62 6c 65 29 20 29 20 72 65 74 75 72 6e 20 30  able) ) return 0
1c257 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  ;.#endif..#ifnde
1c258 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
1c259 45 57 0a 20 20 2f 2a 20 41 20 70 6f 73 69 74 69  EW.  /* A positi
1c25a 76 65 20 6e 43 6f 6c 20 6d 65 61 6e 73 20 74 68  ve nCol means th
1c25b 65 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 20  e columns names 
1c25c 66 6f 72 20 74 68 69 73 20 76 69 65 77 20 61 72  for this view ar
1c25d 65 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 6b  e.  ** already k
1c25e 6e 6f 77 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  nown..  */.  if(
1c25f 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3e 30 20   pTable->nCol>0 
1c260 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f  ) return 0;..  /
1c261 2a 20 41 20 6e 65 67 61 74 69 76 65 20 6e 43 6f  * A negative nCo
1c262 6c 20 69 73 20 61 20 73 70 65 63 69 61 6c 20 6d  l is a special m
1c263 61 72 6b 65 72 20 6d 65 61 6e 69 6e 67 20 74 68  arker meaning th
1c264 61 74 20 77 65 20 61 72 65 20 63 75 72 72 65 6e  at we are curren
1c265 74 6c 79 0a 20 20 2a 2a 20 74 72 79 69 6e 67 20  tly.  ** trying 
1c266 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 63  to compute the c
1c267 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 49 66  olumn names.  If
1c268 20 77 65 20 65 6e 74 65 72 20 74 68 69 73 20 72   we enter this r
1c269 6f 75 74 69 6e 65 20 77 69 74 68 0a 20 20 2a 2a  outine with.  **
1c26a 20 61 20 6e 65 67 61 74 69 76 65 20 6e 43 6f 6c   a negative nCol
1c26b 2c 20 69 74 20 6d 65 61 6e 73 20 74 77 6f 20 6f  , it means two o
1c26c 72 20 6d 6f 72 65 20 76 69 65 77 73 20 66 6f 72  r more views for
1c26d 6d 20 61 20 6c 6f 6f 70 2c 20 6c 69 6b 65 20 74  m a loop, like t
1c26e 68 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  his:.  **.  **  
1c26f 20 20 20 43 52 45 41 54 45 20 56 49 45 57 20 6f     CREATE VIEW o
1c270 6e 65 20 41 53 20 53 45 4c 45 43 54 20 2a 20 46  ne AS SELECT * F
1c271 52 4f 4d 20 74 77 6f 3b 0a 20 20 2a 2a 20 20 20  ROM two;.  **   
1c272 20 20 43 52 45 41 54 45 20 56 49 45 57 20 74 77    CREATE VIEW tw
1c273 6f 20 41 53 20 53 45 4c 45 43 54 20 2a 20 46 52  o AS SELECT * FR
1c274 4f 4d 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20 20 2a  OM one;.  **.  *
1c275 2a 20 41 63 74 75 61 6c 6c 79 2c 20 74 68 69 73  * Actually, this
1c276 20 65 72 72 6f 72 20 69 73 20 63 61 75 67 68 74   error is caught
1c277 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6e 64 20   previously and 
1c278 73 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  so the following
1c279 20 74 65 73 74 0a 20 20 2a 2a 20 73 68 6f 75 6c   test.  ** shoul
1c27a 64 20 61 6c 77 61 79 73 20 66 61 69 6c 2e 20 20  d always fail.  
1c27b 42 75 74 20 77 65 20 77 69 6c 6c 20 6c 65 61 76  But we will leav
1c27c 65 20 69 74 20 69 6e 20 70 6c 61 63 65 20 6a 75  e it in place ju
1c27d 73 74 20 74 6f 20 62 65 20 73 61 66 65 2e 0a 20  st to be safe.. 
1c27e 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 6c 65   */.  if( pTable
1c27f 2d 3e 6e 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20  ->nCol<0 ){.    
1c280 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1c281 70 50 61 72 73 65 2c 20 22 76 69 65 77 20 25 73  pParse, "view %s
1c282 20 69 73 20 63 69 72 63 75 6c 61 72 6c 79 20 64   is circularly d
1c283 65 66 69 6e 65 64 22 2c 20 70 54 61 62 6c 65 2d  efined", pTable-
1c284 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74  >zName);.    ret
1c285 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 61 73 73  urn 1;.  }.  ass
1c286 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f  ert( pTable->nCo
1c287 6c 3e 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66  l>=0 );..  /* If
1c288 20 77 65 20 67 65 74 20 74 68 69 73 20 66 61 72   we get this far
1c289 2c 20 69 74 20 6d 65 61 6e 73 20 77 65 20 6e 65  , it means we ne
1c28a 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68  ed to compute th
1c28b 65 20 74 61 62 6c 65 20 6e 61 6d 65 73 2e 0a 20  e table names.. 
1c28c 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68   ** Note that th
1c28d 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  e call to sqlite
1c28e 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65  3ResultSetOfSele
1c28f 63 74 28 29 20 77 69 6c 6c 20 65 78 70 61 6e 64  ct() will expand
1c290 20 61 6e 79 0a 20 20 2a 2a 20 22 2a 22 20 65 6c   any.  ** "*" el
1c291 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 72 65  ements in the re
1c292 73 75 6c 74 73 20 73 65 74 20 6f 66 20 74 68 65  sults set of the
1c293 20 76 69 65 77 20 61 6e 64 20 77 69 6c 6c 20 61   view and will a
1c294 73 73 69 67 6e 20 63 75 72 73 6f 72 73 0a 20 20  ssign cursors.  
1c295 2a 2a 20 74 6f 20 74 68 65 20 65 6c 65 6d 65 6e  ** to the elemen
1c296 74 73 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63  ts of the FROM c
1c297 6c 61 75 73 65 2e 20 20 42 75 74 20 77 65 20 64  lause.  But we d
1c298 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 73 65  o not want these
1c299 20 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20 74 6f   changes.  ** to
1c29a 20 62 65 20 70 65 72 6d 61 6e 65 6e 74 2e 20 20   be permanent.  
1c29b 53 6f 20 74 68 65 20 63 6f 6d 70 75 74 61 74 69  So the computati
1c29c 6f 6e 20 69 73 20 64 6f 6e 65 20 6f 6e 20 61 20  on is done on a 
1c29d 63 6f 70 79 20 6f 66 20 74 68 65 20 53 45 4c 45  copy of the SELE
1c29e 43 54 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e  CT.  ** statemen
1c29f 74 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74  t that defines t
1c2a0 68 65 20 76 69 65 77 2e 0a 20 20 2a 2f 0a 20 20  he view..  */.  
1c2a1 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e  assert( pTable->
1c2a2 70 53 65 6c 65 63 74 20 29 3b 0a 20 20 70 53 65  pSelect );.  pSe
1c2a3 6c 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63  l = sqlite3Selec
1c2a4 74 44 75 70 28 64 62 2c 20 70 54 61 62 6c 65 2d  tDup(db, pTable-
1c2a5 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 69 66 28  >pSelect);.  if(
1c2a6 20 70 53 65 6c 20 29 7b 0a 20 20 20 20 6e 20 3d   pSel ){.    n =
1c2a7 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a 20   pParse->nTab;. 
1c2a8 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73     sqlite3SrcLis
1c2a9 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28 70  tAssignCursors(p
1c2aa 50 61 72 73 65 2c 20 70 53 65 6c 2d 3e 70 53 72  Parse, pSel->pSr
1c2ab 63 29 3b 0a 20 20 20 20 70 54 61 62 6c 65 2d 3e  c);.    pTable->
1c2ac 6e 43 6f 6c 20 3d 20 2d 31 3b 0a 23 69 66 6e 64  nCol = -1;.#ifnd
1c2ad 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1c2ae 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20  UTHORIZATION.   
1c2af 20 78 41 75 74 68 20 3d 20 64 62 2d 3e 78 41 75   xAuth = db->xAu
1c2b0 74 68 3b 0a 20 20 20 20 64 62 2d 3e 78 41 75 74  th;.    db->xAut
1c2b1 68 20 3d 20 30 3b 0a 20 20 20 20 70 53 65 6c 54  h = 0;.    pSelT
1c2b2 61 62 20 3d 20 73 71 6c 69 74 65 33 52 65 73 75  ab = sqlite3Resu
1c2b3 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28 70 50  ltSetOfSelect(pP
1c2b4 61 72 73 65 2c 20 30 2c 20 70 53 65 6c 29 3b 0a  arse, 0, pSel);.
1c2b5 20 20 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20      db->xAuth = 
1c2b6 78 41 75 74 68 3b 0a 23 65 6c 73 65 0a 20 20 20  xAuth;.#else.   
1c2b7 20 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 74   pSelTab = sqlit
1c2b8 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c  e3ResultSetOfSel
1c2b9 65 63 74 28 70 50 61 72 73 65 2c 20 30 2c 20 70  ect(pParse, 0, p
1c2ba 53 65 6c 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20  Sel);.#endif.   
1c2bb 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d 20   pParse->nTab = 
1c2bc 6e 3b 0a 20 20 20 20 69 66 28 20 70 53 65 6c 54  n;.    if( pSelT
1c2bd 61 62 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  ab ){.      asse
1c2be 72 74 28 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c  rt( pTable->aCol
1c2bf 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 54 61  ==0 );.      pTa
1c2c0 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 70 53 65 6c  ble->nCol = pSel
1c2c1 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20  Tab->nCol;.     
1c2c2 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 20 3d 20   pTable->aCol = 
1c2c3 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 3b 0a 20  pSelTab->aCol;. 
1c2c4 20 20 20 20 20 70 53 65 6c 54 61 62 2d 3e 6e 43       pSelTab->nC
1c2c5 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 53  ol = 0;.      pS
1c2c6 65 6c 54 61 62 2d 3e 61 43 6f 6c 20 3d 20 30 3b  elTab->aCol = 0;
1c2c7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65  .      sqlite3De
1c2c8 6c 65 74 65 54 61 62 6c 65 28 70 53 65 6c 54 61  leteTable(pSelTa
1c2c9 62 29 3b 0a 20 20 20 20 20 20 70 54 61 62 6c 65  b);.      pTable
1c2ca 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73  ->pSchema->flags
1c2cb 20 7c 3d 20 44 42 5f 55 6e 72 65 73 65 74 56 69   |= DB_UnresetVi
1c2cc 65 77 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ews;.    }else{.
1c2cd 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e 43        pTable->nC
1c2ce 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 6e 45  ol = 0;.      nE
1c2cf 72 72 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rr++;.    }.    
1c2d0 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
1c2d1 65 74 65 28 70 53 65 6c 29 3b 0a 20 20 7d 20 65  ete(pSel);.  } e
1c2d2 6c 73 65 20 7b 0a 20 20 20 20 6e 45 72 72 2b 2b  lse {.    nErr++
1c2d3 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;.  }.#endif /* 
1c2d4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
1c2d5 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 6e 45 72   */.  return nEr
1c2d6 72 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  r;  .}.#endif /*
1c2d7 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1c2d8 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21  _OMIT_VIEW) || !
1c2d9 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
1c2da 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1c2db 29 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  ) */..#ifndef SQ
1c2dc 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 2f  LITE_OMIT_VIEW./
1c2dd 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63  *.** Clear the c
1c2de 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 72 6f 6d  olumn names from
1c2df 20 65 76 65 72 79 20 56 49 45 57 20 69 6e 20 64   every VIEW in d
1c2e0 61 74 61 62 61 73 65 20 69 64 78 2e 0a 2a 2f 0a  atabase idx..*/.
1c2e1 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69  static void sqli
1c2e2 74 65 56 69 65 77 52 65 73 65 74 41 6c 6c 28 73  teViewResetAll(s
1c2e3 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
1c2e4 69 64 78 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d  idx){.  HashElem
1c2e5 20 2a 69 3b 0a 20 20 69 66 28 20 21 44 62 48 61   *i;.  if( !DbHa
1c2e6 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 64  sProperty(db, id
1c2e7 78 2c 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65  x, DB_UnresetVie
1c2e8 77 73 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ws) ) return;.  
1c2e9 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68  for(i=sqliteHash
1c2ea 46 69 72 73 74 28 26 64 62 2d 3e 61 44 62 5b 69  First(&db->aDb[i
1c2eb 64 78 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c  dx].pSchema->tbl
1c2ec 48 61 73 68 29 3b 20 69 3b 69 3d 73 71 6c 69 74  Hash); i;i=sqlit
1c2ed 65 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20  eHashNext(i)){. 
1c2ee 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
1c2ef 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28   sqliteHashData(
1c2f0 69 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62  i);.    if( pTab
1c2f1 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ->pSelect ){.   
1c2f2 20 20 20 73 71 6c 69 74 65 52 65 73 65 74 43 6f     sqliteResetCo
1c2f3 6c 75 6d 6e 4e 61 6d 65 73 28 70 54 61 62 29 3b  lumnNames(pTab);
1c2f4 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 44 62 43  .    }.  }.  DbC
1c2f5 6c 65 61 72 50 72 6f 70 65 72 74 79 28 64 62 2c  learProperty(db,
1c2f6 20 69 64 78 2c 20 44 42 5f 55 6e 72 65 73 65 74   idx, DB_Unreset
1c2f7 56 69 65 77 73 29 3b 0a 7d 0a 23 65 6c 73 65 0a  Views);.}.#else.
1c2f8 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 56  # define sqliteV
1c2f9 69 65 77 52 65 73 65 74 41 6c 6c 28 41 2c 42 29  iewResetAll(A,B)
1c2fa 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1c2fb 45 5f 4f 4d 49 54 5f 56 49 45 57 20 2a 2f 0a 0a  E_OMIT_VIEW */..
1c2fc 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
1c2fd 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79  ion is called by
1c2fe 20 74 68 65 20 56 44 42 45 20 74 6f 20 61 64 6a   the VDBE to adj
1c2ff 75 73 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  ust the internal
1c300 20 73 63 68 65 6d 61 0a 2a 2a 20 75 73 65 64 20   schema.** used 
1c301 62 79 20 53 51 4c 69 74 65 20 77 68 65 6e 20 74  by SQLite when t
1c302 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 6d  he btree layer m
1c303 6f 76 65 73 20 61 20 74 61 62 6c 65 20 72 6f 6f  oves a table roo
1c304 74 20 70 61 67 65 2e 20 54 68 65 0a 2a 2a 20 72  t page. The.** r
1c305 6f 6f 74 2d 70 61 67 65 20 6f 66 20 61 20 74 61  oot-page of a ta
1c306 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 6e 20  ble or index in 
1c307 64 61 74 61 62 61 73 65 20 69 44 62 20 68 61 73  database iDb has
1c308 20 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 69 46   changed from iF
1c309 72 6f 6d 0a 2a 2a 20 74 6f 20 69 54 6f 2e 0a 2a  rom.** to iTo..*
1c30a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 31 37 32  *.** Ticket #172
1c30b 38 3a 20 20 54 68 65 20 73 79 6d 62 6f 6c 20 74  8:  The symbol t
1c30c 61 62 6c 65 20 6d 69 67 68 74 20 73 74 69 6c 6c  able might still
1c30d 20 63 6f 6e 74 61 69 6e 20 69 6e 66 6f 72 6d 61   contain informa
1c30e 74 69 6f 6e 0a 2a 2a 20 6f 6e 20 74 61 62 6c 65  tion.** on table
1c30f 73 20 61 6e 64 2f 6f 72 20 69 6e 64 69 63 65 73  s and/or indices
1c310 20 74 68 61 74 20 61 72 65 20 74 68 65 20 70 72   that are the pr
1c311 6f 63 65 73 73 20 6f 66 20 62 65 69 6e 67 20 64  ocess of being d
1c312 65 6c 65 74 65 64 2e 0a 2a 2a 20 49 66 20 79 6f  eleted..** If yo
1c313 75 20 61 72 65 20 75 6e 6c 75 63 6b 79 2c 20 6f  u are unlucky, o
1c314 6e 65 20 6f 66 20 74 68 6f 73 65 20 64 65 6c 65  ne of those dele
1c315 74 65 64 20 69 6e 64 69 63 65 73 20 6f 72 20 74  ted indices or t
1c316 61 62 6c 65 73 20 6d 69 67 68 74 0a 2a 2a 20 68  ables might.** h
1c317 61 76 65 20 74 68 65 20 73 61 6d 65 20 72 6f 6f  ave the same roo
1c318 74 70 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20  tpage number as 
1c319 74 68 65 20 72 65 61 6c 20 74 61 62 6c 65 20 6f  the real table o
1c31a 72 20 69 6e 64 65 78 20 74 68 61 74 20 69 73 0a  r index that is.
1c31b 2a 2a 20 62 65 69 6e 67 20 6d 6f 76 65 64 2e 20  ** being moved. 
1c31c 20 53 6f 20 77 65 20 63 61 6e 6e 6f 74 20 73 74   So we cannot st
1c31d 6f 70 20 73 65 61 72 63 68 69 6e 67 20 61 66 74  op searching aft
1c31e 65 72 20 74 68 65 20 66 69 72 73 74 20 6d 61 74  er the first mat
1c31f 63 68 20 0a 2a 2a 20 62 65 63 61 75 73 65 20 74  ch .** because t
1c320 68 65 20 66 69 72 73 74 20 6d 61 74 63 68 20 6d  he first match m
1c321 69 67 68 74 20 62 65 20 66 6f 72 20 6f 6e 65 20  ight be for one 
1c322 6f 66 20 74 68 65 20 64 65 6c 65 74 65 64 20 69  of the deleted i
1c323 6e 64 69 63 65 73 0a 2a 2a 20 6f 72 20 74 61 62  ndices.** or tab
1c324 6c 65 73 20 61 6e 64 20 6e 6f 74 20 74 68 65 20  les and not the 
1c325 74 61 62 6c 65 2f 69 6e 64 65 78 20 74 68 61 74  table/index that
1c326 20 69 73 20 61 63 74 75 61 6c 6c 79 20 62 65 69   is actually bei
1c327 6e 67 20 6d 6f 76 65 64 2e 0a 2a 2a 20 57 65 20  ng moved..** We 
1c328 6d 75 73 74 20 63 6f 6e 74 69 6e 75 65 20 6c 6f  must continue lo
1c329 6f 70 69 6e 67 20 75 6e 74 69 6c 20 61 6c 6c 20  oping until all 
1c32a 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63  tables and indic
1c32b 65 73 20 77 69 74 68 0a 2a 2a 20 72 6f 6f 74 70  es with.** rootp
1c32c 61 67 65 3d 3d 69 46 72 6f 6d 20 68 61 76 65 20  age==iFrom have 
1c32d 62 65 65 6e 20 63 6f 6e 76 65 72 74 65 64 20 74  been converted t
1c32e 6f 20 68 61 76 65 20 61 20 72 6f 6f 74 70 61 67  o have a rootpag
1c32f 65 20 6f 66 20 69 54 6f 0a 2a 2a 20 69 6e 20 6f  e of iTo.** in o
1c330 72 64 65 72 20 74 6f 20 62 65 20 63 65 72 74 61  rder to be certa
1c331 69 6e 20 74 68 61 74 20 77 65 20 67 6f 74 20 74  in that we got t
1c332 68 65 20 72 69 67 68 74 20 6f 6e 65 2e 0a 2a 2f  he right one..*/
1c333 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1c334 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
1c335 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1c336 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6f 74 50  oid sqlite3RootP
1c337 61 67 65 4d 6f 76 65 64 28 44 62 20 2a 70 44 62  ageMoved(Db *pDb
1c338 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74  , int iFrom, int
1c339 20 69 54 6f 29 7b 0a 20 20 48 61 73 68 45 6c 65   iTo){.  HashEle
1c33a 6d 20 2a 70 45 6c 65 6d 3b 0a 20 20 48 61 73 68  m *pElem;.  Hash
1c33b 20 2a 70 48 61 73 68 3b 0a 0a 20 20 70 48 61 73   *pHash;..  pHas
1c33c 68 20 3d 20 26 70 44 62 2d 3e 70 53 63 68 65 6d  h = &pDb->pSchem
1c33d 61 2d 3e 74 62 6c 48 61 73 68 3b 0a 20 20 66 6f  a->tblHash;.  fo
1c33e 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61  r(pElem=sqliteHa
1c33f 73 68 46 69 72 73 74 28 70 48 61 73 68 29 3b 20  shFirst(pHash); 
1c340 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c  pElem; pElem=sql
1c341 69 74 65 48 61 73 68 4e 65 78 74 28 70 45 6c 65  iteHashNext(pEle
1c342 6d 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a  m)){.    Table *
1c343 70 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73  pTab = sqliteHas
1c344 68 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a 20 20  hData(pElem);.  
1c345 20 20 69 66 28 20 70 54 61 62 2d 3e 74 6e 75 6d    if( pTab->tnum
1c346 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20  ==iFrom ){.     
1c347 20 70 54 61 62 2d 3e 74 6e 75 6d 20 3d 20 69 54   pTab->tnum = iT
1c348 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  o;.    }.  }.  p
1c349 48 61 73 68 20 3d 20 26 70 44 62 2d 3e 70 53 63  Hash = &pDb->pSc
1c34a 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0a 20  hema->idxHash;. 
1c34b 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74   for(pElem=sqlit
1c34c 65 48 61 73 68 46 69 72 73 74 28 70 48 61 73 68  eHashFirst(pHash
1c34d 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d  ); pElem; pElem=
1c34e 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70  sqliteHashNext(p
1c34f 45 6c 65 6d 29 29 7b 0a 20 20 20 20 49 6e 64 65  Elem)){.    Inde
1c350 78 20 2a 70 49 64 78 20 3d 20 73 71 6c 69 74 65  x *pIdx = sqlite
1c351 48 61 73 68 44 61 74 61 28 70 45 6c 65 6d 29 3b  HashData(pElem);
1c352 0a 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 74  .    if( pIdx->t
1c353 6e 75 6d 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20  num==iFrom ){.  
1c354 20 20 20 20 70 49 64 78 2d 3e 74 6e 75 6d 20 3d      pIdx->tnum =
1c355 20 69 54 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   iTo;.    }.  }.
1c356 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
1c357 57 72 69 74 65 20 63 6f 64 65 20 74 6f 20 65 72  Write code to er
1c358 61 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69  ase the table wi
1c359 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61  th root-page iTa
1c35a 62 6c 65 20 66 72 6f 6d 20 64 61 74 61 62 61 73  ble from databas
1c35b 65 20 69 44 62 2e 0a 2a 2a 20 41 6c 73 6f 20 77  e iDb..** Also w
1c35c 72 69 74 65 20 63 6f 64 65 20 74 6f 20 6d 6f 64  rite code to mod
1c35d 69 66 79 20 74 68 65 20 73 71 6c 69 74 65 5f 6d  ify the sqlite_m
1c35e 61 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20  aster table and 
1c35f 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 0a  internal schema.
1c360 2a 2a 20 69 66 20 61 20 72 6f 6f 74 2d 70 61 67  ** if a root-pag
1c361 65 20 6f 66 20 61 6e 6f 74 68 65 72 20 74 61 62  e of another tab
1c362 6c 65 20 69 73 20 6d 6f 76 65 64 20 62 79 20 74  le is moved by t
1c363 68 65 20 62 74 72 65 65 2d 6c 61 79 65 72 20 77  he btree-layer w
1c364 68 69 6c 73 74 0a 2a 2a 20 65 72 61 73 69 6e 67  hilst.** erasing
1c365 20 69 54 61 62 6c 65 20 28 74 68 69 73 20 63 61   iTable (this ca
1c366 6e 20 68 61 70 70 65 6e 20 77 69 74 68 20 61 6e  n happen with an
1c367 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74   auto-vacuum dat
1c368 61 62 61 73 65 29 2e 0a 2a 2f 20 0a 73 74 61 74  abase)..*/ .stat
1c369 69 63 20 76 6f 69 64 20 64 65 73 74 72 6f 79 52  ic void destroyR
1c36a 6f 6f 74 50 61 67 65 28 50 61 72 73 65 20 2a 70  ootPage(Parse *p
1c36b 50 61 72 73 65 2c 20 69 6e 74 20 69 54 61 62 6c  Parse, int iTabl
1c36c 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56  e, int iDb){.  V
1c36d 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33  dbe *v = sqlite3
1c36e 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
1c36f 0a 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69  .  int r1 = sqli
1c370 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
1c371 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  arse);.  sqlite3
1c372 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
1c373 5f 44 65 73 74 72 6f 79 2c 20 69 54 61 62 6c 65  _Destroy, iTable
1c374 2c 20 72 31 2c 20 69 44 62 29 3b 0a 23 69 66 6e  , r1, iDb);.#ifn
1c375 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1c376 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 2f 2a 20  AUTOVACUUM.  /* 
1c377 4f 50 5f 44 65 73 74 72 6f 79 20 73 74 6f 72 65  OP_Destroy store
1c378 73 20 61 6e 20 69 6e 20 69 6e 74 65 67 65 72 20  s an in integer 
1c379 72 31 2e 20 49 66 20 74 68 69 73 20 69 6e 74 65  r1. If this inte
1c37a 67 65 72 0a 20 20 2a 2a 20 69 73 20 6e 6f 6e 2d  ger.  ** is non-
1c37b 7a 65 72 6f 2c 20 74 68 65 6e 20 69 74 20 69 73  zero, then it is
1c37c 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e   the root page n
1c37d 75 6d 62 65 72 20 6f 66 20 61 20 74 61 62 6c 65  umber of a table
1c37e 20 6d 6f 76 65 64 20 74 6f 0a 20 20 2a 2a 20 6c   moved to.  ** l
1c37f 6f 63 61 74 69 6f 6e 20 69 54 61 62 6c 65 2e 20  ocation iTable. 
1c380 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  The following co
1c381 64 65 20 6d 6f 64 69 66 69 65 73 20 74 68 65 20  de modifies the 
1c382 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
1c383 62 6c 65 20 74 6f 0a 20 20 2a 2a 20 72 65 66 6c  ble to.  ** refl
1c384 65 63 74 20 74 68 69 73 2e 0a 20 20 2a 2a 0a 20  ect this..  **. 
1c385 20 2a 2a 20 54 68 65 20 22 23 25 64 22 20 69 6e   ** The "#%d" in
1c386 20 74 68 65 20 53 51 4c 20 69 73 20 61 20 73 70   the SQL is a sp
1c387 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20 74  ecial constant t
1c388 68 61 74 20 6d 65 61 6e 73 20 77 68 61 74 65 76  hat means whatev
1c389 65 72 20 76 61 6c 75 65 0a 20 20 2a 2a 20 69 73  er value.  ** is
1c38a 20 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74   on the top of t
1c38b 68 65 20 73 74 61 63 6b 2e 20 20 53 65 65 20 73  he stack.  See s
1c38c 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 45 78  qlite3RegisterEx
1c38d 70 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  pr()..  */.  sql
1c38e 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28  ite3NestedParse(
1c38f 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 22 55  pParse, .     "U
1c390 50 44 41 54 45 20 25 51 2e 25 73 20 53 45 54 20  PDATE %Q.%s SET 
1c391 72 6f 6f 74 70 61 67 65 3d 25 64 20 57 48 45 52  rootpage=%d WHER
1c392 45 20 23 25 64 20 41 4e 44 20 72 6f 6f 74 70 61  E #%d AND rootpa
1c393 67 65 3d 23 25 64 22 2c 0a 20 20 20 20 20 70 50  ge=#%d",.     pP
1c394 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44  arse->db->aDb[iD
1c395 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41  b].zName, SCHEMA
1c396 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 69 54 61  _TABLE(iDb), iTa
1c397 62 6c 65 2c 20 72 31 2c 20 72 31 29 3b 0a 23 65  ble, r1, r1);.#e
1c398 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 52 65  ndif.  sqlite3Re
1c399 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
1c39a 72 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a  rse, r1);.}../*.
1c39b 2a 2a 20 57 72 69 74 65 20 56 44 42 45 20 63 6f  ** Write VDBE co
1c39c 64 65 20 74 6f 20 65 72 61 73 65 20 74 61 62 6c  de to erase tabl
1c39d 65 20 70 54 61 62 20 61 6e 64 20 61 6c 6c 20 61  e pTab and all a
1c39e 73 73 6f 63 69 61 74 65 64 20 69 6e 64 69 63 65  ssociated indice
1c39f 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 20 43 6f  s on disk..** Co
1c3a0 64 65 20 74 6f 20 75 70 64 61 74 65 20 74 68 65  de to update the
1c3a1 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
1c3a2 61 62 6c 65 73 20 61 6e 64 20 69 6e 74 65 72 6e  ables and intern
1c3a3 61 6c 20 73 63 68 65 6d 61 20 64 65 66 69 6e 69  al schema defini
1c3a4 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 63 61 73 65  tions.** in case
1c3a5 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 62 65 6c   a root-page bel
1c3a6 6f 6e 67 69 6e 67 20 74 6f 20 61 6e 6f 74 68 65  onging to anothe
1c3a7 72 20 74 61 62 6c 65 20 69 73 20 6d 6f 76 65 64  r table is moved
1c3a8 20 62 79 20 74 68 65 20 62 74 72 65 65 20 6c 61   by the btree la
1c3a9 79 65 72 0a 2a 2a 20 69 73 20 61 6c 73 6f 20 61  yer.** is also a
1c3aa 64 64 65 64 20 28 74 68 69 73 20 63 61 6e 20 68  dded (this can h
1c3ab 61 70 70 65 6e 20 77 69 74 68 20 61 6e 20 61 75  appen with an au
1c3ac 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61  to-vacuum databa
1c3ad 73 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  se)..*/.static v
1c3ae 6f 69 64 20 64 65 73 74 72 6f 79 54 61 62 6c 65  oid destroyTable
1c3af 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1c3b0 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 23 69  Table *pTab){.#i
1c3b1 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
1c3b2 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 49 6e  _AUTOVACUUM.  In
1c3b3 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 69 6e 74  dex *pIdx;.  int
1c3b4 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
1c3b5 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72  hemaToIndex(pPar
1c3b6 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53  se->db, pTab->pS
1c3b7 63 68 65 6d 61 29 3b 0a 20 20 64 65 73 74 72 6f  chema);.  destro
1c3b8 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65  yRootPage(pParse
1c3b9 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69 44  , pTab->tnum, iD
1c3ba 62 29 3b 0a 20 20 66 6f 72 28 70 49 64 78 3d 70  b);.  for(pIdx=p
1c3bb 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
1c3bc 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
1c3bd 65 78 74 29 7b 0a 20 20 20 20 64 65 73 74 72 6f  ext){.    destro
1c3be 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65  yRootPage(pParse
1c3bf 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44  , pIdx->tnum, iD
1c3c0 62 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20  b);.  }.#else.  
1c3c1 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61  /* If the databa
1c3c2 73 65 20 6d 61 79 20 62 65 20 61 75 74 6f 2d 76  se may be auto-v
1c3c3 61 63 75 75 6d 20 63 61 70 61 62 6c 65 20 28 69  acuum capable (i
1c3c4 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
1c3c5 54 4f 56 41 43 55 55 4d 0a 20 20 2a 2a 20 69 73  TOVACUUM.  ** is
1c3c6 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 74   not defined), t
1c3c7 68 65 6e 20 69 74 20 69 73 20 69 6d 70 6f 72 74  hen it is import
1c3c8 61 6e 74 20 74 6f 20 63 61 6c 6c 20 4f 50 5f 44  ant to call OP_D
1c3c9 65 73 74 72 6f 79 20 6f 6e 20 74 68 65 0a 20 20  estroy on the.  
1c3ca 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64  ** table and ind
1c3cb 65 78 20 72 6f 6f 74 2d 70 61 67 65 73 20 69 6e  ex root-pages in
1c3cc 20 6f 72 64 65 72 2c 20 73 74 61 72 74 69 6e 67   order, starting
1c3cd 20 77 69 74 68 20 74 68 65 20 6e 75 6d 65 72 69   with the numeri
1c3ce 63 61 6c 6c 79 20 0a 20 20 2a 2a 20 6c 61 72 67  cally .  ** larg
1c3cf 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75  est root-page nu
1c3d0 6d 62 65 72 2e 20 54 68 69 73 20 67 75 61 72 61  mber. This guara
1c3d1 6e 74 65 65 73 20 74 68 61 74 20 6e 6f 6e 65 20  ntees that none 
1c3d2 6f 66 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65  of the root-page
1c3d3 73 0a 20 20 2a 2a 20 74 6f 20 62 65 20 64 65 73  s.  ** to be des
1c3d4 74 72 6f 79 65 64 20 69 73 20 72 65 6c 6f 63 61  troyed is reloca
1c3d5 74 65 64 20 62 79 20 61 6e 20 65 61 72 6c 69 65  ted by an earlie
1c3d6 72 20 4f 50 5f 44 65 73 74 72 6f 79 2e 20 69 2e  r OP_Destroy. i.
1c3d7 65 2e 20 69 66 20 74 68 65 0a 20 20 2a 2a 20 66  e. if the.  ** f
1c3d8 6f 6c 6c 6f 77 69 6e 67 20 77 65 72 65 20 63 6f  ollowing were co
1c3d9 64 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f  ded:.  **.  ** O
1c3da 50 5f 44 65 73 74 72 6f 79 20 34 20 30 0a 20 20  P_Destroy 4 0.  
1c3db 2a 2a 20 2e 2e 2e 0a 20 20 2a 2a 20 4f 50 5f 44  ** ....  ** OP_D
1c3dc 65 73 74 72 6f 79 20 35 20 30 0a 20 20 2a 2a 0a  estroy 5 0.  **.
1c3dd 20 20 2a 2a 20 61 6e 64 20 72 6f 6f 74 20 70 61    ** and root pa
1c3de 67 65 20 35 20 68 61 70 70 65 6e 65 64 20 74 6f  ge 5 happened to
1c3df 20 62 65 20 74 68 65 20 6c 61 72 67 65 73 74 20   be the largest 
1c3e0 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72  root-page number
1c3e1 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74   in the.  ** dat
1c3e2 61 62 61 73 65 2c 20 74 68 65 6e 20 72 6f 6f 74  abase, then root
1c3e3 20 70 61 67 65 20 35 20 77 6f 75 6c 64 20 62 65   page 5 would be
1c3e4 20 6d 6f 76 65 64 20 74 6f 20 70 61 67 65 20 34   moved to page 4
1c3e5 20 62 79 20 74 68 65 20 0a 20 20 2a 2a 20 22 4f   by the .  ** "O
1c3e6 50 5f 44 65 73 74 72 6f 79 20 34 20 30 22 20 6f  P_Destroy 4 0" o
1c3e7 70 63 6f 64 65 2e 20 54 68 65 20 73 75 62 73 65  pcode. The subse
1c3e8 71 75 65 6e 74 20 22 4f 50 5f 44 65 73 74 72 6f  quent "OP_Destro
1c3e9 79 20 35 20 30 22 20 77 6f 75 6c 64 20 68 69 74  y 5 0" would hit
1c3ea 0a 20 20 2a 2a 20 61 20 66 72 65 65 2d 6c 69 73  .  ** a free-lis
1c3eb 74 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69  t page..  */.  i
1c3ec 6e 74 20 69 54 61 62 20 3d 20 70 54 61 62 2d 3e  nt iTab = pTab->
1c3ed 74 6e 75 6d 3b 0a 20 20 69 6e 74 20 69 44 65 73  tnum;.  int iDes
1c3ee 74 72 6f 79 65 64 20 3d 20 30 3b 0a 0a 20 20 77  troyed = 0;..  w
1c3ef 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 49  hile( 1 ){.    I
1c3f0 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20  ndex *pIdx;.    
1c3f1 69 6e 74 20 69 4c 61 72 67 65 73 74 20 3d 20 30  int iLargest = 0
1c3f2 3b 0a 0a 20 20 20 20 69 66 28 20 69 44 65 73 74  ;..    if( iDest
1c3f3 72 6f 79 65 64 3d 3d 30 20 7c 7c 20 69 54 61 62  royed==0 || iTab
1c3f4 3c 69 44 65 73 74 72 6f 79 65 64 20 29 7b 0a 20  <iDestroyed ){. 
1c3f5 20 20 20 20 20 69 4c 61 72 67 65 73 74 20 3d 20       iLargest = 
1c3f6 69 54 61 62 3b 0a 20 20 20 20 7d 0a 20 20 20 20  iTab;.    }.    
1c3f7 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70  for(pIdx=pTab->p
1c3f8 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
1c3f9 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  x=pIdx->pNext){.
1c3fa 20 20 20 20 20 20 69 6e 74 20 69 49 64 78 20 3d        int iIdx =
1c3fb 20 70 49 64 78 2d 3e 74 6e 75 6d 3b 0a 20 20 20   pIdx->tnum;.   
1c3fc 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d     assert( pIdx-
1c3fd 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e  >pSchema==pTab->
1c3fe 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 20  pSchema );.     
1c3ff 20 69 66 28 20 28 69 44 65 73 74 72 6f 79 65 64   if( (iDestroyed
1c400 3d 3d 30 20 7c 7c 20 28 69 49 64 78 3c 69 44 65  ==0 || (iIdx<iDe
1c401 73 74 72 6f 79 65 64 29 29 20 26 26 20 69 49 64  stroyed)) && iId
1c402 78 3e 69 4c 61 72 67 65 73 74 20 29 7b 0a 20 20  x>iLargest ){.  
1c403 20 20 20 20 20 20 69 4c 61 72 67 65 73 74 20 3d        iLargest =
1c404 20 69 49 64 78 3b 0a 20 20 20 20 20 20 7d 0a 20   iIdx;.      }. 
1c405 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 4c 61     }.    if( iLa
1c406 72 67 65 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20  rgest==0 ){.    
1c407 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 65    return;.    }e
1c408 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  lse{.      int i
1c409 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
1c40a 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65  maToIndex(pParse
1c40b 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  ->db, pTab->pSch
1c40c 65 6d 61 29 3b 0a 20 20 20 20 20 20 64 65 73 74  ema);.      dest
1c40d 72 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72  royRootPage(pPar
1c40e 73 65 2c 20 69 4c 61 72 67 65 73 74 2c 20 69 44  se, iLargest, iD
1c40f 62 29 3b 0a 20 20 20 20 20 20 69 44 65 73 74 72  b);.      iDestr
1c410 6f 79 65 64 20 3d 20 69 4c 61 72 67 65 73 74 3b  oyed = iLargest;
1c411 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
1c412 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  f.}../*.** This 
1c413 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
1c414 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b  d to do the work
1c415 20 6f 66 20 61 20 44 52 4f 50 20 54 41 42 4c 45   of a DROP TABLE
1c416 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 70   statement..** p
1c417 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65  Name is the name
1c418 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f   of the table to
1c419 20 62 65 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a   be dropped..*/.
1c41a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1c41b 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54  oid sqlite3DropT
1c41c 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72  able(Parse *pPar
1c41d 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e 61  se, SrcList *pNa
1c41e 6d 65 2c 20 69 6e 74 20 69 73 56 69 65 77 2c 20  me, int isView, 
1c41f 69 6e 74 20 6e 6f 45 72 72 29 7b 0a 20 20 54 61  int noErr){.  Ta
1c420 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 56 64 62  ble *pTab;.  Vdb
1c421 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 20  e *v;.  sqlite3 
1c422 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
1c423 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20 20  ;.  int iDb;..  
1c424 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
1c425 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
1c426 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f  iled ){.    goto
1c427 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65   exit_drop_table
1c428 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
1c429 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29  pName->nSrc==1 )
1c42a 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74  ;.  pTab = sqlit
1c42b 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50  e3LocateTable(pP
1c42c 61 72 73 65 2c 20 69 73 56 69 65 77 2c 20 0a 20  arse, isView, . 
1c42d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c42e 20 20 20 20 20 20 20 20 20 20 20 70 4e 61 6d 65             pName
1c42f 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 4e  ->a[0].zName, pN
1c430 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62  ame->a[0].zDatab
1c431 61 73 65 29 3b 0a 0a 20 20 69 66 28 20 70 54 61  ase);..  if( pTa
1c432 62 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  b==0 ){.    if( 
1c433 6e 6f 45 72 72 20 29 7b 0a 20 20 20 20 20 20 73  noErr ){.      s
1c434 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72  qlite3ErrorClear
1c435 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 7d 0a  (pParse);.    }.
1c436 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72      goto exit_dr
1c437 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20  op_table;.  }.  
1c438 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
1c439 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70  emaToIndex(db, p
1c43a 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  Tab->pSchema);. 
1c43b 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20   assert( iDb>=0 
1c43c 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29  && iDb<db->nDb )
1c43d 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 54 61 62 20  ;..  /* If pTab 
1c43e 69 73 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  is a virtual tab
1c43f 6c 65 2c 20 63 61 6c 6c 20 56 69 65 77 47 65 74  le, call ViewGet
1c440 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29 20 74 6f  ColumnNames() to
1c441 20 65 6e 73 75 72 65 0a 20 20 2a 2a 20 69 74 20   ensure.  ** it 
1c442 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a  is initialized..
1c443 20 20 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72    */.  if( IsVir
1c444 74 75 61 6c 28 70 54 61 62 29 20 26 26 20 73 71  tual(pTab) && sq
1c445 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75  lite3ViewGetColu
1c446 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20  mnNames(pParse, 
1c447 70 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74  pTab) ){.    got
1c448 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c  o exit_drop_tabl
1c449 65 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  e;.  }.#ifndef S
1c44a 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
1c44b 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20  RIZATION.  {.   
1c44c 20 69 6e 74 20 63 6f 64 65 3b 0a 20 20 20 20 63   int code;.    c
1c44d 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20  onst char *zTab 
1c44e 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  = SCHEMA_TABLE(i
1c44f 44 62 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  Db);.    const c
1c450 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61  har *zDb = db->a
1c451 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20  Db[iDb].zName;. 
1c452 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
1c453 41 72 67 32 20 3d 20 30 3b 0a 20 20 20 20 69 66  Arg2 = 0;.    if
1c454 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  ( sqlite3AuthChe
1c455 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54  ck(pParse, SQLIT
1c456 45 5f 44 45 4c 45 54 45 2c 20 7a 54 61 62 2c 20  E_DELETE, zTab, 
1c457 30 2c 20 7a 44 62 29 29 7b 0a 20 20 20 20 20 20  0, zDb)){.      
1c458 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74  goto exit_drop_t
1c459 61 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20  able;.    }.    
1c45a 69 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20  if( isView ){.  
1c45b 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45      if( !OMIT_TE
1c45c 4d 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29  MPDB && iDb==1 )
1c45d 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d  {.        code =
1c45e 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d   SQLITE_DROP_TEM
1c45f 50 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 65  P_VIEW;.      }e
1c460 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64  lse{.        cod
1c461 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f  e = SQLITE_DROP_
1c462 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 0a 23 69  VIEW;.      }.#i
1c463 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1c464 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
1c465 20 20 20 7d 65 6c 73 65 20 69 66 28 20 49 73 56     }else if( IsV
1c466 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
1c467 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c        code = SQL
1c468 49 54 45 5f 44 52 4f 50 5f 56 54 41 42 4c 45 3b  ITE_DROP_VTABLE;
1c469 0a 20 20 20 20 20 20 7a 41 72 67 32 20 3d 20 70  .      zArg2 = p
1c46a 54 61 62 2d 3e 70 4d 6f 64 2d 3e 7a 4e 61 6d 65  Tab->pMod->zName
1c46b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c  ;.#endif.    }el
1c46c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 21 4f  se{.      if( !O
1c46d 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44  MIT_TEMPDB && iD
1c46e 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  b==1 ){.        
1c46f 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52  code = SQLITE_DR
1c470 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 3b 0a 20  OP_TEMP_TABLE;. 
1c471 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1c472 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54      code = SQLIT
1c473 45 5f 44 52 4f 50 5f 54 41 42 4c 45 3b 0a 20 20  E_DROP_TABLE;.  
1c474 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1c475 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43  if( sqlite3AuthC
1c476 68 65 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64  heck(pParse, cod
1c477 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  e, pTab->zName, 
1c478 7a 41 72 67 32 2c 20 7a 44 62 29 20 29 7b 0a 20  zArg2, zDb) ){. 
1c479 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64       goto exit_d
1c47a 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 20 20 7d  rop_table;.    }
1c47b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1c47c 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
1c47d 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c  , SQLITE_DELETE,
1c47e 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c   pTab->zName, 0,
1c47f 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67   zDb) ){.      g
1c480 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61  oto exit_drop_ta
1c481 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  ble;.    }.  }.#
1c482 65 6e 64 69 66 0a 20 20 69 66 28 20 70 54 61 62  endif.  if( pTab
1c483 2d 3e 72 65 61 64 4f 6e 6c 79 20 7c 7c 20 70 54  ->readOnly || pT
1c484 61 62 3d 3d 64 62 2d 3e 61 44 62 5b 69 44 62 5d  ab==db->aDb[iDb]
1c485 2e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 54 61  .pSchema->pSeqTa
1c486 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  b ){.    sqlite3
1c487 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
1c488 20 22 74 61 62 6c 65 20 25 73 20 6d 61 79 20 6e   "table %s may n
1c489 6f 74 20 62 65 20 64 72 6f 70 70 65 64 22 2c 20  ot be dropped", 
1c48a 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pTab->zName);.  
1c48b 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
1c48c 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66  _table;.  }..#if
1c48d 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1c48e 5f 56 49 45 57 0a 20 20 2f 2a 20 45 6e 73 75 72  _VIEW.  /* Ensur
1c48f 65 20 44 52 4f 50 20 54 41 42 4c 45 20 69 73 20  e DROP TABLE is 
1c490 6e 6f 74 20 75 73 65 64 20 6f 6e 20 61 20 76 69  not used on a vi
1c491 65 77 2c 20 61 6e 64 20 44 52 4f 50 20 56 49 45  ew, and DROP VIE
1c492 57 20 69 73 20 6e 6f 74 20 75 73 65 64 0a 20 20  W is not used.  
1c493 2a 2a 20 6f 6e 20 61 20 74 61 62 6c 65 2e 0a 20  ** on a table.. 
1c494 20 2a 2f 0a 20 20 69 66 28 20 69 73 56 69 65 77   */.  if( isView
1c495 20 26 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63   && pTab->pSelec
1c496 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  t==0 ){.    sqli
1c497 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1c498 73 65 2c 20 22 75 73 65 20 44 52 4f 50 20 54 41  se, "use DROP TA
1c499 42 4c 45 20 74 6f 20 64 65 6c 65 74 65 20 74 61  BLE to delete ta
1c49a 62 6c 65 20 25 73 22 2c 20 70 54 61 62 2d 3e 7a  ble %s", pTab->z
1c49b 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20  Name);.    goto 
1c49c 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b  exit_drop_table;
1c49d 0a 20 20 7d 0a 20 20 69 66 28 20 21 69 73 56 69  .  }.  if( !isVi
1c49e 65 77 20 26 26 20 70 54 61 62 2d 3e 70 53 65 6c  ew && pTab->pSel
1c49f 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ect ){.    sqlit
1c4a0 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1c4a1 65 2c 20 22 75 73 65 20 44 52 4f 50 20 56 49 45  e, "use DROP VIE
1c4a2 57 20 74 6f 20 64 65 6c 65 74 65 20 76 69 65 77  W to delete view
1c4a3 20 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d   %s", pTab->zNam
1c4a4 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69  e);.    goto exi
1c4a5 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20  t_drop_table;.  
1c4a6 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47  }.#endif..  /* G
1c4a7 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
1c4a8 72 65 6d 6f 76 65 20 74 68 65 20 74 61 62 6c 65  remove the table
1c4a9 20 66 72 6f 6d 20 74 68 65 20 6d 61 73 74 65 72   from the master
1c4aa 20 74 61 62 6c 65 0a 20 20 2a 2a 20 6f 6e 20 64   table.  ** on d
1c4ab 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20  isk..  */.  v = 
1c4ac 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
1c4ad 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20  Parse);.  if( v 
1c4ae 29 7b 0a 20 20 20 20 54 72 69 67 67 65 72 20 2a  ){.    Trigger *
1c4af 70 54 72 69 67 67 65 72 3b 0a 20 20 20 20 44 62  pTrigger;.    Db
1c4b0 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62   *pDb = &db->aDb
1c4b1 5b 69 44 62 5d 3b 0a 20 20 20 20 73 71 6c 69 74  [iDb];.    sqlit
1c4b2 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
1c4b3 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c  ation(pParse, 1,
1c4b4 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20   iDb);..#ifndef 
1c4b5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1c4b6 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 66 28  UALTABLE.    if(
1c4b7 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
1c4b8 20 29 7b 0a 20 20 20 20 20 20 56 64 62 65 20 2a   ){.      Vdbe *
1c4b9 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
1c4ba 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  be(pParse);.    
1c4bb 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 20    if( v ){.     
1c4bc 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1c4bd 64 4f 70 30 28 76 2c 20 4f 50 5f 56 42 65 67 69  dOp0(v, OP_VBegi
1c4be 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  n);.      }.    
1c4bf 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a  }.#endif..    /*
1c4c0 20 44 72 6f 70 20 61 6c 6c 20 74 72 69 67 67 65   Drop all trigge
1c4c1 72 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  rs associated wi
1c4c2 74 68 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  th the table bei
1c4c3 6e 67 20 64 72 6f 70 70 65 64 2e 20 43 6f 64 65  ng dropped. Code
1c4c4 0a 20 20 20 20 2a 2a 20 69 73 20 67 65 6e 65 72  .    ** is gener
1c4c5 61 74 65 64 20 74 6f 20 72 65 6d 6f 76 65 20 65  ated to remove e
1c4c6 6e 74 72 69 65 73 20 66 72 6f 6d 20 73 71 6c 69  ntries from sqli
1c4c7 74 65 5f 6d 61 73 74 65 72 20 61 6e 64 2f 6f 72  te_master and/or
1c4c8 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 5f 74  .    ** sqlite_t
1c4c9 65 6d 70 5f 6d 61 73 74 65 72 20 69 66 20 72 65  emp_master if re
1c4ca 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20  quired..    */. 
1c4cb 20 20 20 70 54 72 69 67 67 65 72 20 3d 20 70 54     pTrigger = pT
1c4cc 61 62 2d 3e 70 54 72 69 67 67 65 72 3b 0a 20 20  ab->pTrigger;.  
1c4cd 20 20 77 68 69 6c 65 28 20 70 54 72 69 67 67 65    while( pTrigge
1c4ce 72 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  r ){.      asser
1c4cf 74 28 20 70 54 72 69 67 67 65 72 2d 3e 70 53 63  t( pTrigger->pSc
1c4d0 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68  hema==pTab->pSch
1c4d1 65 6d 61 20 7c 7c 20 0a 20 20 20 20 20 20 20 20  ema || .        
1c4d2 20 20 70 54 72 69 67 67 65 72 2d 3e 70 53 63 68    pTrigger->pSch
1c4d3 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b 31 5d 2e  ema==db->aDb[1].
1c4d4 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 20  pSchema );.     
1c4d5 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67   sqlite3DropTrig
1c4d6 67 65 72 50 74 72 28 70 50 61 72 73 65 2c 20 70  gerPtr(pParse, p
1c4d7 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 20 20  Trigger);.      
1c4d8 70 54 72 69 67 67 65 72 20 3d 20 70 54 72 69 67  pTrigger = pTrig
1c4d9 67 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  ger->pNext;.    
1c4da 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
1c4db 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45  E_OMIT_AUTOINCRE
1c4dc 4d 45 4e 54 0a 20 20 20 20 2f 2a 20 52 65 6d 6f  MENT.    /* Remo
1c4dd 76 65 20 61 6e 79 20 65 6e 74 72 69 65 73 20 6f  ve any entries o
1c4de 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71  f the sqlite_seq
1c4df 75 65 6e 63 65 20 74 61 62 6c 65 20 61 73 73 6f  uence table asso
1c4e0 63 69 61 74 65 64 20 77 69 74 68 0a 20 20 20 20  ciated with.    
1c4e1 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  ** the table bei
1c4e2 6e 67 20 64 72 6f 70 70 65 64 2e 20 54 68 69 73  ng dropped. This
1c4e3 20 69 73 20 64 6f 6e 65 20 62 65 66 6f 72 65 20   is done before 
1c4e4 74 68 65 20 74 61 62 6c 65 20 69 73 20 64 72 6f  the table is dro
1c4e5 70 70 65 64 0a 20 20 20 20 2a 2a 20 61 74 20 74  pped.    ** at t
1c4e6 68 65 20 62 74 72 65 65 20 6c 65 76 65 6c 2c 20  he btree level, 
1c4e7 69 6e 20 63 61 73 65 20 74 68 65 20 73 71 6c 69  in case the sqli
1c4e8 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c  te_sequence tabl
1c4e9 65 20 6e 65 65 64 73 20 74 6f 0a 20 20 20 20 2a  e needs to.    *
1c4ea 2a 20 6d 6f 76 65 20 61 73 20 61 20 72 65 73 75  * move as a resu
1c4eb 6c 74 20 6f 66 20 74 68 65 20 64 72 6f 70 20 28  lt of the drop (
1c4ec 63 61 6e 20 68 61 70 70 65 6e 20 69 6e 20 61 75  can happen in au
1c4ed 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 29 2e  to-vacuum mode).
1c4ee 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1c4ef 70 54 61 62 2d 3e 61 75 74 6f 49 6e 63 20 29 7b  pTab->autoInc ){
1c4f0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e 65  .      sqlite3Ne
1c4f1 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
1c4f2 2c 0a 20 20 20 20 20 20 20 20 22 44 45 4c 45 54  ,.        "DELET
1c4f3 45 20 46 52 4f 4d 20 25 73 2e 73 71 6c 69 74 65  E FROM %s.sqlite
1c4f4 5f 73 65 71 75 65 6e 63 65 20 57 48 45 52 45 20  _sequence WHERE 
1c4f5 6e 61 6d 65 3d 25 51 22 2c 0a 20 20 20 20 20 20  name=%Q",.      
1c4f6 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 70 54    pDb->zName, pT
1c4f7 61 62 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 20 20  ab->zName.      
1c4f8 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
1c4f9 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 61 6c 6c  .    /* Drop all
1c4fa 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74   SQLITE_MASTER t
1c4fb 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 65  able and index e
1c4fc 6e 74 72 69 65 73 20 74 68 61 74 20 72 65 66 65  ntries that refe
1c4fd 72 20 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20  r to the.    ** 
1c4fe 74 61 62 6c 65 2e 20 54 68 65 20 70 72 6f 67 72  table. The progr
1c4ff 61 6d 20 6e 61 6d 65 20 6c 6f 6f 70 73 20 74 68  am name loops th
1c500 72 6f 75 67 68 20 74 68 65 20 6d 61 73 74 65 72  rough the master
1c501 20 74 61 62 6c 65 20 61 6e 64 20 64 65 6c 65 74   table and delet
1c502 65 73 0a 20 20 20 20 2a 2a 20 65 76 65 72 79 20  es.    ** every 
1c503 72 6f 77 20 74 68 61 74 20 72 65 66 65 72 73 20  row that refers 
1c504 74 6f 20 61 20 74 61 62 6c 65 20 6f 66 20 74 68  to a table of th
1c505 65 20 73 61 6d 65 20 6e 61 6d 65 20 61 73 20 74  e same name as t
1c506 68 65 20 6f 6e 65 20 62 65 69 6e 67 0a 20 20 20  he one being.   
1c507 20 2a 2a 20 64 72 6f 70 70 65 64 2e 20 54 72 69   ** dropped. Tri
1c508 67 67 65 72 73 20 61 72 65 20 68 61 6e 64 6c 65  ggers are handle
1c509 64 20 73 65 70 65 72 61 74 65 6c 79 20 62 65 63  d seperately bec
1c50a 61 75 73 65 20 61 20 74 72 69 67 67 65 72 20 63  ause a trigger c
1c50b 61 6e 20 62 65 0a 20 20 20 20 2a 2a 20 63 72 65  an be.    ** cre
1c50c 61 74 65 64 20 69 6e 20 74 68 65 20 74 65 6d 70  ated in the temp
1c50d 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 72   database that r
1c50e 65 66 65 72 73 20 74 6f 20 61 20 74 61 62 6c 65  efers to a table
1c50f 20 69 6e 20 61 6e 6f 74 68 65 72 0a 20 20 20 20   in another.    
1c510 2a 2a 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20  ** database..   
1c511 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e   */.    sqlite3N
1c512 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73  estedParse(pPars
1c513 65 2c 20 0a 20 20 20 20 20 20 20 20 22 44 45 4c  e, .        "DEL
1c514 45 54 45 20 46 52 4f 4d 20 25 51 2e 25 73 20 57  ETE FROM %Q.%s W
1c515 48 45 52 45 20 74 62 6c 5f 6e 61 6d 65 3d 25 51  HERE tbl_name=%Q
1c516 20 61 6e 64 20 74 79 70 65 21 3d 27 74 72 69 67   and type!='trig
1c517 67 65 72 27 22 2c 0a 20 20 20 20 20 20 20 20 70  ger'",.        p
1c518 44 62 2d 3e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d  Db->zName, SCHEM
1c519 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 70 54  A_TABLE(iDb), pT
1c51a 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 20  ab->zName);..   
1c51b 20 2f 2a 20 44 72 6f 70 20 61 6e 79 20 73 74 61   /* Drop any sta
1c51c 74 69 73 74 69 63 73 20 66 72 6f 6d 20 74 68 65  tistics from the
1c51d 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
1c51e 62 6c 65 2c 20 69 66 20 69 74 20 65 78 69 73 74  ble, if it exist
1c51f 73 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c  s */.    if( sql
1c520 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62  ite3FindTable(db
1c521 2c 20 22 73 71 6c 69 74 65 5f 73 74 61 74 31 22  , "sqlite_stat1"
1c522 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  , db->aDb[iDb].z
1c523 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 73  Name) ){.      s
1c524 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
1c525 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20  e(pParse,.      
1c526 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25    "DELETE FROM %
1c527 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 31 20 57  Q.sqlite_stat1 W
1c528 48 45 52 45 20 74 62 6c 3d 25 51 22 2c 20 70 44  HERE tbl=%Q", pD
1c529 62 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e  b->zName, pTab->
1c52a 7a 4e 61 6d 65 0a 20 20 20 20 20 20 29 3b 0a 20  zName.      );. 
1c52b 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 21 69     }..    if( !i
1c52c 73 56 69 65 77 20 26 26 20 21 49 73 56 69 72 74  sView && !IsVirt
1c52d 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
1c52e 20 20 20 64 65 73 74 72 6f 79 54 61 62 6c 65 28     destroyTable(
1c52f 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20  pParse, pTab);. 
1c530 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6d     }..    /* Rem
1c531 6f 76 65 20 74 68 65 20 74 61 62 6c 65 20 65 6e  ove the table en
1c532 74 72 79 20 66 72 6f 6d 20 53 51 4c 69 74 65 27  try from SQLite'
1c533 73 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d  s internal schem
1c534 61 20 61 6e 64 20 6d 6f 64 69 66 79 0a 20 20 20  a and modify.   
1c535 20 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 20 63   ** the schema c
1c536 6f 6f 6b 69 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  ookie..    */.  
1c537 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
1c538 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 73  pTab) ){.      s
1c539 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
1c53a 28 76 2c 20 4f 50 5f 56 44 65 73 74 72 6f 79 2c  (v, OP_VDestroy,
1c53b 20 69 44 62 2c 20 30 2c 20 30 2c 20 70 54 61 62   iDb, 0, 0, pTab
1c53c 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  ->zName, 0);.   
1c53d 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
1c53e 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44  beAddOp4(v, OP_D
1c53f 72 6f 70 54 61 62 6c 65 2c 20 69 44 62 2c 20 30  ropTable, iDb, 0
1c540 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  , 0, pTab->zName
1c541 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
1c542 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50  3ChangeCookie(pP
1c543 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 7d 0a  arse, iDb);.  }.
1c544 20 20 73 71 6c 69 74 65 56 69 65 77 52 65 73 65    sqliteViewRese
1c545 74 41 6c 6c 28 64 62 2c 20 69 44 62 29 3b 0a 0a  tAll(db, iDb);..
1c546 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3a  exit_drop_table:
1c547 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
1c548 74 44 65 6c 65 74 65 28 70 4e 61 6d 65 29 3b 0a  tDelete(pName);.
1c549 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
1c54a 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
1c54b 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20  to create a new 
1c54c 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 74  foreign key on t
1c54d 68 65 20 74 61 62 6c 65 0a 2a 2a 20 63 75 72 72  he table.** curr
1c54e 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73  ently under cons
1c54f 74 72 75 63 74 69 6f 6e 2e 20 20 70 46 72 6f 6d  truction.  pFrom
1c550 43 6f 6c 20 64 65 74 65 72 6d 69 6e 65 73 20 77  Col determines w
1c551 68 69 63 68 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20  hich columns.** 
1c552 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  in the current t
1c553 61 62 6c 65 20 70 6f 69 6e 74 20 74 6f 20 74 68  able point to th
1c554 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 2e 20 20  e foreign key.  
1c555 49 66 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 74  If pFromCol==0 t
1c556 68 65 6e 0a 2a 2a 20 63 6f 6e 6e 65 63 74 20 74  hen.** connect t
1c557 68 65 20 6b 65 79 20 74 6f 20 74 68 65 20 6c 61  he key to the la
1c558 73 74 20 63 6f 6c 75 6d 6e 20 69 6e 73 65 72 74  st column insert
1c559 65 64 2e 20 20 70 54 6f 20 69 73 20 74 68 65 20  ed.  pTo is the 
1c55a 6e 61 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 74  name of.** the t
1c55b 61 62 6c 65 20 72 65 66 65 72 72 65 64 20 74 6f  able referred to
1c55c 2e 20 20 70 54 6f 43 6f 6c 20 69 73 20 61 20 6c  .  pToCol is a l
1c55d 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20 69 6e  ist of tables in
1c55e 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 70 54   the other.** pT
1c55f 6f 20 74 61 62 6c 65 20 74 68 61 74 20 74 68 65  o table that the
1c560 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 70 6f 69   foreign key poi
1c561 6e 74 73 20 74 6f 2e 20 20 66 6c 61 67 73 20 63  nts to.  flags c
1c562 6f 6e 74 61 69 6e 73 20 61 6c 6c 0a 2a 2a 20 69  ontains all.** i
1c563 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
1c564 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 20 72 65   the conflict re
1c565 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69 74  solution algorit
1c566 68 6d 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  hms specified.**
1c567 20 69 6e 20 74 68 65 20 4f 4e 20 44 45 4c 45 54   in the ON DELET
1c568 45 2c 20 4f 4e 20 55 50 44 41 54 45 20 61 6e 64  E, ON UPDATE and
1c569 20 4f 4e 20 49 4e 53 45 52 54 20 63 6c 61 75 73   ON INSERT claus
1c56a 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 46 4b 65  es..**.** An FKe
1c56b 79 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63  y structure is c
1c56c 72 65 61 74 65 64 20 61 6e 64 20 61 64 64 65 64  reated and added
1c56d 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75   to the table cu
1c56e 72 72 65 6e 74 6c 79 0a 2a 2a 20 75 6e 64 65 72  rrently.** under
1c56f 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e   construction in
1c570 20 74 68 65 20 70 50 61 72 73 65 2d 3e 70 4e 65   the pParse->pNe
1c571 77 54 61 62 6c 65 20 66 69 65 6c 64 2e 20 20 54  wTable field.  T
1c572 68 65 20 6e 65 77 20 46 4b 65 79 0a 2a 2a 20 69  he new FKey.** i
1c573 73 20 6e 6f 74 20 6c 69 6e 6b 65 64 20 69 6e 74  s not linked int
1c574 6f 20 64 62 2d 3e 61 46 4b 65 79 20 61 74 20 74  o db->aFKey at t
1c575 68 69 73 20 70 6f 69 6e 74 20 2d 20 74 68 61 74  his point - that
1c576 20 64 6f 65 73 20 6e 6f 74 20 68 61 70 70 65 6e   does not happen
1c577 0a 2a 2a 20 75 6e 74 69 6c 20 73 71 6c 69 74 65  .** until sqlite
1c578 33 45 6e 64 54 61 62 6c 65 28 29 2e 0a 2a 2a 0a  3EndTable()..**.
1c579 2a 2a 20 54 68 65 20 66 6f 72 65 69 67 6e 20 6b  ** The foreign k
1c57a 65 79 20 69 73 20 73 65 74 20 66 6f 72 20 49 4d  ey is set for IM
1c57b 4d 45 44 49 41 54 45 20 70 72 6f 63 65 73 73 69  MEDIATE processi
1c57c 6e 67 2e 20 20 41 20 73 75 62 73 65 71 75 65 6e  ng.  A subsequen
1c57d 74 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 73 71 6c  t call.** to sql
1c57e 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e  ite3DeferForeign
1c57f 4b 65 79 28 29 20 6d 69 67 68 74 20 63 68 61 6e  Key() might chan
1c580 67 65 20 74 68 69 73 20 74 6f 20 44 45 46 45 52  ge this to DEFER
1c581 52 45 44 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  RED..*/.SQLITE_P
1c582 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1c583 74 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e  te3CreateForeign
1c584 4b 65 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50  Key(.  Parse *pP
1c585 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50  arse,       /* P
1c586 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
1c587 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 46  /.  ExprList *pF
1c588 72 6f 6d 43 6f 6c 2c 20 20 2f 2a 20 43 6f 6c 75  romCol,  /* Colu
1c589 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c  mns in this tabl
1c58a 65 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20  e that point to 
1c58b 6f 74 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20  other table */. 
1c58c 20 54 6f 6b 65 6e 20 2a 70 54 6f 2c 20 20 20 20   Token *pTo,    
1c58d 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
1c58e 20 74 68 65 20 6f 74 68 65 72 20 74 61 62 6c 65   the other table
1c58f 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
1c590 70 54 6f 43 6f 6c 2c 20 20 20 20 2f 2a 20 43 6f  pToCol,    /* Co
1c591 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 6f 74 68  lumns in the oth
1c592 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  er table */.  in
1c593 74 20 66 6c 61 67 73 20 20 20 20 20 20 20 20 20  t flags         
1c594 20 20 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 20 72     /* Conflict r
1c595 65 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69  esolution algori
1c596 74 68 6d 73 2e 20 2a 2f 0a 29 7b 0a 23 69 66 6e  thms. */.){.#ifn
1c597 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1c598 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 46 4b  FOREIGN_KEY.  FK
1c599 65 79 20 2a 70 46 4b 65 79 20 3d 20 30 3b 0a 20  ey *pFKey = 0;. 
1c59a 20 54 61 62 6c 65 20 2a 70 20 3d 20 70 50 61 72   Table *p = pPar
1c59b 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20  se->pNewTable;. 
1c59c 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 69 6e   int nByte;.  in
1c59d 74 20 69 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b  t i;.  int nCol;
1c59e 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 61  .  char *z;..  a
1c59f 73 73 65 72 74 28 20 70 54 6f 21 3d 30 20 29 3b  ssert( pTo!=0 );
1c5a0 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 70  .  if( p==0 || p
1c5a1 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 49  Parse->nErr || I
1c5a2 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29  N_DECLARE_VTAB )
1c5a3 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20   goto fk_end;.  
1c5a4 69 66 28 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20  if( pFromCol==0 
1c5a5 29 7b 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 20  ){.    int iCol 
1c5a6 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 20  = p->nCol-1;.   
1c5a7 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 20 67 6f   if( iCol<0 ) go
1c5a8 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 20 20 69  to fk_end;.    i
1c5a9 66 28 20 70 54 6f 43 6f 6c 20 26 26 20 70 54 6f  f( pToCol && pTo
1c5aa 43 6f 6c 2d 3e 6e 45 78 70 72 21 3d 31 20 29 7b  Col->nExpr!=1 ){
1c5ab 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
1c5ac 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1c5ad 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 25  foreign key on %
1c5ae 73 22 0a 20 20 20 20 20 20 20 20 20 22 20 73 68  s".         " sh
1c5af 6f 75 6c 64 20 72 65 66 65 72 65 6e 63 65 20 6f  ould reference o
1c5b0 6e 6c 79 20 6f 6e 65 20 63 6f 6c 75 6d 6e 20 6f  nly one column o
1c5b1 66 20 74 61 62 6c 65 20 25 54 22 2c 0a 20 20 20  f table %T",.   
1c5b2 20 20 20 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 43        p->aCol[iC
1c5b3 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 70 54 6f 29 3b  ol].zName, pTo);
1c5b4 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65  .      goto fk_e
1c5b5 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 43  nd;.    }.    nC
1c5b6 6f 6c 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 20  ol = 1;.  }else 
1c5b7 69 66 28 20 70 54 6f 43 6f 6c 20 26 26 20 70 54  if( pToCol && pT
1c5b8 6f 43 6f 6c 2d 3e 6e 45 78 70 72 21 3d 70 46 72  oCol->nExpr!=pFr
1c5b9 6f 6d 43 6f 6c 2d 3e 6e 45 78 70 72 20 29 7b 0a  omCol->nExpr ){.
1c5ba 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1c5bb 4d 73 67 28 70 50 61 72 73 65 2c 0a 20 20 20 20  Msg(pParse,.    
1c5bc 20 20 20 20 22 6e 75 6d 62 65 72 20 6f 66 20 63      "number of c
1c5bd 6f 6c 75 6d 6e 73 20 69 6e 20 66 6f 72 65 69 67  olumns in foreig
1c5be 6e 20 6b 65 79 20 64 6f 65 73 20 6e 6f 74 20 6d  n key does not m
1c5bf 61 74 63 68 20 74 68 65 20 6e 75 6d 62 65 72 20  atch the number 
1c5c0 6f 66 20 22 0a 20 20 20 20 20 20 20 20 22 63 6f  of ".        "co
1c5c1 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 66  lumns in the ref
1c5c2 65 72 65 6e 63 65 64 20 74 61 62 6c 65 22 29 3b  erenced table");
1c5c3 0a 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64  .    goto fk_end
1c5c4 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e  ;.  }else{.    n
1c5c5 43 6f 6c 20 3d 20 70 46 72 6f 6d 43 6f 6c 2d 3e  Col = pFromCol->
1c5c6 6e 45 78 70 72 3b 0a 20 20 7d 0a 20 20 6e 42 79  nExpr;.  }.  nBy
1c5c7 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 46 4b  te = sizeof(*pFK
1c5c8 65 79 29 20 2b 20 6e 43 6f 6c 2a 73 69 7a 65 6f  ey) + nCol*sizeo
1c5c9 66 28 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d  f(pFKey->aCol[0]
1c5ca 29 20 2b 20 70 54 6f 2d 3e 6e 20 2b 20 31 3b 0a  ) + pTo->n + 1;.
1c5cb 20 20 69 66 28 20 70 54 6f 43 6f 6c 20 29 7b 0a    if( pToCol ){.
1c5cc 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
1c5cd 54 6f 43 6f 6c 2d 3e 6e 45 78 70 72 3b 20 69 2b  ToCol->nExpr; i+
1c5ce 2b 29 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20  +){.      nByte 
1c5cf 2b 3d 20 73 74 72 6c 65 6e 28 70 54 6f 43 6f 6c  += strlen(pToCol
1c5d0 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 20 2b 20  ->a[i].zName) + 
1c5d1 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  1;.    }.  }.  p
1c5d2 46 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 44 62  FKey = sqlite3Db
1c5d3 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 50 61 72 73  MallocZero(pPars
1c5d4 65 2d 3e 64 62 2c 20 6e 42 79 74 65 20 29 3b 0a  e->db, nByte );.
1c5d5 20 20 69 66 28 20 70 46 4b 65 79 3d 3d 30 20 29    if( pFKey==0 )
1c5d6 7b 0a 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e  {.    goto fk_en
1c5d7 64 3b 0a 20 20 7d 0a 20 20 70 46 4b 65 79 2d 3e  d;.  }.  pFKey->
1c5d8 70 46 72 6f 6d 20 3d 20 70 3b 0a 20 20 70 46 4b  pFrom = p;.  pFK
1c5d9 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 20 3d 20  ey->pNextFrom = 
1c5da 70 2d 3e 70 46 4b 65 79 3b 0a 20 20 7a 20 3d 20  p->pFKey;.  z = 
1c5db 28 63 68 61 72 2a 29 26 70 46 4b 65 79 5b 31 5d  (char*)&pFKey[1]
1c5dc 3b 0a 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 20  ;.  pFKey->aCol 
1c5dd 3d 20 28 73 74 72 75 63 74 20 73 43 6f 6c 4d 61  = (struct sColMa
1c5de 70 2a 29 7a 3b 0a 20 20 7a 20 2b 3d 20 73 69 7a  p*)z;.  z += siz
1c5df 65 6f 66 28 73 74 72 75 63 74 20 73 43 6f 6c 4d  eof(struct sColM
1c5e0 61 70 29 2a 6e 43 6f 6c 3b 0a 20 20 70 46 4b 65  ap)*nCol;.  pFKe
1c5e1 79 2d 3e 7a 54 6f 20 3d 20 7a 3b 0a 20 20 6d 65  y->zTo = z;.  me
1c5e2 6d 63 70 79 28 7a 2c 20 70 54 6f 2d 3e 7a 2c 20  mcpy(z, pTo->z, 
1c5e3 70 54 6f 2d 3e 6e 29 3b 0a 20 20 7a 5b 70 54 6f  pTo->n);.  z[pTo
1c5e4 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 7a 20 2b 3d  ->n] = 0;.  z +=
1c5e5 20 70 54 6f 2d 3e 6e 2b 31 3b 0a 20 20 70 46 4b   pTo->n+1;.  pFK
1c5e6 65 79 2d 3e 70 4e 65 78 74 54 6f 20 3d 20 30 3b  ey->pNextTo = 0;
1c5e7 0a 20 20 70 46 4b 65 79 2d 3e 6e 43 6f 6c 20 3d  .  pFKey->nCol =
1c5e8 20 6e 43 6f 6c 3b 0a 20 20 69 66 28 20 70 46 72   nCol;.  if( pFr
1c5e9 6f 6d 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 20  omCol==0 ){.    
1c5ea 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d 2e 69  pFKey->aCol[0].i
1c5eb 46 72 6f 6d 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31  From = p->nCol-1
1c5ec 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66  ;.  }else{.    f
1c5ed 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20  or(i=0; i<nCol; 
1c5ee 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  i++){.      int 
1c5ef 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  j;.      for(j=0
1c5f0 3b 20 6a 3c 70 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b  ; j<p->nCol; j++
1c5f1 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  ){.        if( s
1c5f2 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 2d  qlite3StrICmp(p-
1c5f3 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20  >aCol[j].zName, 
1c5f4 70 46 72 6f 6d 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a  pFromCol->a[i].z
1c5f5 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20  Name)==0 ){.    
1c5f6 20 20 20 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f        pFKey->aCo
1c5f7 6c 5b 69 5d 2e 69 46 72 6f 6d 20 3d 20 6a 3b 0a  l[i].iFrom = j;.
1c5f8 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
1c5f9 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1c5fa 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6a 3e 3d   }.      if( j>=
1c5fb 70 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20  p->nCol ){.     
1c5fc 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1c5fd 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  sg(pParse, .    
1c5fe 20 20 20 20 20 20 22 75 6e 6b 6e 6f 77 6e 20 63        "unknown c
1c5ff 6f 6c 75 6d 6e 20 5c 22 25 73 5c 22 20 69 6e 20  olumn \"%s\" in 
1c600 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 65 66 69  foreign key defi
1c601 6e 69 74 69 6f 6e 22 2c 20 0a 20 20 20 20 20 20  nition", .      
1c602 20 20 20 20 70 46 72 6f 6d 43 6f 6c 2d 3e 61 5b      pFromCol->a[
1c603 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  i].zName);.     
1c604 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a     goto fk_end;.
1c605 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1c606 7d 0a 20 20 69 66 28 20 70 54 6f 43 6f 6c 20 29  }.  if( pToCol )
1c607 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  {.    for(i=0; i
1c608 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nCol; i++){.   
1c609 20 20 20 69 6e 74 20 6e 20 3d 20 73 74 72 6c 65     int n = strle
1c60a 6e 28 70 54 6f 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a  n(pToCol->a[i].z
1c60b 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 46 4b  Name);.      pFK
1c60c 65 79 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c  ey->aCol[i].zCol
1c60d 20 3d 20 7a 3b 0a 20 20 20 20 20 20 6d 65 6d 63   = z;.      memc
1c60e 70 79 28 7a 2c 20 70 54 6f 43 6f 6c 2d 3e 61 5b  py(z, pToCol->a[
1c60f 69 5d 2e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20  i].zName, n);.  
1c610 20 20 20 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20 20      z[n] = 0;.  
1c611 20 20 20 20 7a 20 2b 3d 20 6e 2b 31 3b 0a 20 20      z += n+1;.  
1c612 20 20 7d 0a 20 20 7d 0a 20 20 70 46 4b 65 79 2d    }.  }.  pFKey-
1c613 3e 69 73 44 65 66 65 72 72 65 64 20 3d 20 30 3b  >isDeferred = 0;
1c614 0a 20 20 70 46 4b 65 79 2d 3e 64 65 6c 65 74 65  .  pFKey->delete
1c615 43 6f 6e 66 20 3d 20 66 6c 61 67 73 20 26 20 30  Conf = flags & 0
1c616 78 66 66 3b 0a 20 20 70 46 4b 65 79 2d 3e 75 70  xff;.  pFKey->up
1c617 64 61 74 65 43 6f 6e 66 20 3d 20 28 66 6c 61 67  dateConf = (flag
1c618 73 20 3e 3e 20 38 20 29 20 26 20 30 78 66 66 3b  s >> 8 ) & 0xff;
1c619 0a 20 20 70 46 4b 65 79 2d 3e 69 6e 73 65 72 74  .  pFKey->insert
1c61a 43 6f 6e 66 20 3d 20 28 66 6c 61 67 73 20 3e 3e  Conf = (flags >>
1c61b 20 31 36 20 29 20 26 20 30 78 66 66 3b 0a 0a 20   16 ) & 0xff;.. 
1c61c 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 66 6f 72   /* Link the for
1c61d 65 69 67 6e 20 6b 65 79 20 74 6f 20 74 68 65 20  eign key to the 
1c61e 74 61 62 6c 65 20 61 73 20 74 68 65 20 6c 61 73  table as the las
1c61f 74 20 73 74 65 70 2e 0a 20 20 2a 2f 0a 20 20 70  t step..  */.  p
1c620 2d 3e 70 46 4b 65 79 20 3d 20 70 46 4b 65 79 3b  ->pFKey = pFKey;
1c621 0a 20 20 70 46 4b 65 79 20 3d 20 30 3b 0a 0a 66  .  pFKey = 0;..f
1c622 6b 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33  k_end:.  sqlite3
1c623 5f 66 72 65 65 28 70 46 4b 65 79 29 3b 0a 23 65  _free(pFKey);.#e
1c624 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
1c625 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52  (SQLITE_OMIT_FOR
1c626 45 49 47 4e 5f 4b 45 59 29 20 2a 2f 0a 20 20 73  EIGN_KEY) */.  s
1c627 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
1c628 6c 65 74 65 28 70 46 72 6f 6d 43 6f 6c 29 3b 0a  lete(pFromCol);.
1c629 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
1c62a 74 44 65 6c 65 74 65 28 70 54 6f 43 6f 6c 29 3b  tDelete(pToCol);
1c62b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
1c62c 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
1c62d 20 77 68 65 6e 20 61 6e 20 49 4e 49 54 49 41 4c   when an INITIAL
1c62e 4c 59 20 49 4d 4d 45 44 49 41 54 45 20 6f 72 20  LY IMMEDIATE or 
1c62f 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52  INITIALLY DEFERR
1c630 45 44 0a 2a 2a 20 63 6c 61 75 73 65 20 69 73 20  ED.** clause is 
1c631 73 65 65 6e 20 61 73 20 70 61 72 74 20 6f 66 20  seen as part of 
1c632 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 65  a foreign key de
1c633 66 69 6e 69 74 69 6f 6e 2e 20 20 54 68 65 20 69  finition.  The i
1c634 73 44 65 66 65 72 72 65 64 0a 2a 2a 20 70 61 72  sDeferred.** par
1c635 61 6d 65 74 65 72 20 69 73 20 31 20 66 6f 72 20  ameter is 1 for 
1c636 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52  INITIALLY DEFERR
1c637 45 44 20 61 6e 64 20 30 20 66 6f 72 20 49 4e 49  ED and 0 for INI
1c638 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49 41 54 45  TIALLY IMMEDIATE
1c639 2e 0a 2a 2a 20 54 68 65 20 62 65 68 61 76 69 6f  ..** The behavio
1c63a 72 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65  r of the most re
1c63b 63 65 6e 74 6c 79 20 63 72 65 61 74 65 64 20 66  cently created f
1c63c 6f 72 65 69 67 6e 20 6b 65 79 20 69 73 20 61 64  oreign key is ad
1c63d 6a 75 73 74 65 64 0a 2a 2a 20 61 63 63 6f 72 64  justed.** accord
1c63e 69 6e 67 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ingly..*/.SQLITE
1c63f 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1c640 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67  lite3DeferForeig
1c641 6e 4b 65 79 28 50 61 72 73 65 20 2a 70 50 61 72  nKey(Parse *pPar
1c642 73 65 2c 20 69 6e 74 20 69 73 44 65 66 65 72 72  se, int isDeferr
1c643 65 64 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  ed){.#ifndef SQL
1c644 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e  ITE_OMIT_FOREIGN
1c645 5f 4b 45 59 0a 20 20 54 61 62 6c 65 20 2a 70 54  _KEY.  Table *pT
1c646 61 62 3b 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65  ab;.  FKey *pFKe
1c647 79 3b 0a 20 20 69 66 28 20 28 70 54 61 62 20 3d  y;.  if( (pTab =
1c648 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
1c649 6c 65 29 3d 3d 30 20 7c 7c 20 28 70 46 4b 65 79  le)==0 || (pFKey
1c64a 20 3d 20 70 54 61 62 2d 3e 70 46 4b 65 79 29 3d   = pTab->pFKey)=
1c64b 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70  =0 ) return;.  p
1c64c 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64  FKey->isDeferred
1c64d 20 3d 20 69 73 44 65 66 65 72 72 65 64 3b 0a 23   = isDeferred;.#
1c64e 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  endif.}../*.** G
1c64f 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
1c650 74 20 77 69 6c 6c 20 65 72 61 73 65 20 61 6e 64  t will erase and
1c651 20 72 65 66 69 6c 6c 20 69 6e 64 65 78 20 2a 70   refill index *p
1c652 49 64 78 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a  Idx.  This is.**
1c653 20 75 73 65 64 20 74 6f 20 69 6e 69 74 69 61 6c   used to initial
1c654 69 7a 65 20 61 20 6e 65 77 6c 79 20 63 72 65 61  ize a newly crea
1c655 74 65 64 20 69 6e 64 65 78 20 6f 72 20 74 6f 20  ted index or to 
1c656 72 65 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a  recompute the.**
1c657 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20 69   content of an i
1c658 6e 64 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65  ndex in response
1c659 20 74 6f 20 61 20 52 45 49 4e 44 45 58 20 63 6f   to a REINDEX co
1c65a 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 69 66 20  mmand..**.** if 
1c65b 6d 65 6d 52 6f 6f 74 50 61 67 65 20 69 73 20 6e  memRootPage is n
1c65c 6f 74 20 6e 65 67 61 74 69 76 65 2c 20 69 74 20  ot negative, it 
1c65d 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 69  means that the i
1c65e 6e 64 65 78 20 69 73 20 6e 65 77 6c 79 0a 2a 2a  ndex is newly.**
1c65f 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20 72   created.  The r
1c660 65 67 69 73 74 65 72 20 73 70 65 63 69 66 69 65  egister specifie
1c661 64 20 62 79 20 6d 65 6d 52 6f 6f 74 50 61 67 65  d by memRootPage
1c662 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a   contains the.**
1c663 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65   root page numbe
1c664 72 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e 20  r of the index. 
1c665 20 49 66 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20   If memRootPage 
1c666 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65  is negative, the
1c667 6e 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 61  n.** the index a
1c668 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 61 6e  lready exists an
1c669 64 20 6d 75 73 74 20 62 65 20 63 6c 65 61 72 65  d must be cleare
1c66a 64 20 62 65 66 6f 72 65 20 62 65 69 6e 67 20 72  d before being r
1c66b 65 66 69 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 74  efilled and.** t
1c66c 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d  he root page num
1c66d 62 65 72 20 6f 66 20 74 68 65 20 69 6e 64 65 78  ber of the index
1c66e 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 70   is taken from p
1c66f 49 6e 64 65 78 2d 3e 74 6e 75 6d 2e 0a 2a 2f 0a  Index->tnum..*/.
1c670 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69  static void sqli
1c671 74 65 33 52 65 66 69 6c 6c 49 6e 64 65 78 28 50  te3RefillIndex(P
1c672 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 49 6e  arse *pParse, In
1c673 64 65 78 20 2a 70 49 6e 64 65 78 2c 20 69 6e 74  dex *pIndex, int
1c674 20 6d 65 6d 52 6f 6f 74 50 61 67 65 29 7b 0a 20   memRootPage){. 
1c675 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70   Table *pTab = p
1c676 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 3b 20 20  Index->pTable;  
1c677 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68 61  /* The table tha
1c678 74 20 69 73 20 69 6e 64 65 78 65 64 20 2a 2f 0a  t is indexed */.
1c679 20 20 69 6e 74 20 69 54 61 62 20 3d 20 70 50 61    int iTab = pPa
1c67a 72 73 65 2d 3e 6e 54 61 62 3b 20 20 20 20 20 20  rse->nTab;      
1c67b 20 2f 2a 20 42 74 72 65 65 20 63 75 72 73 6f 72   /* Btree cursor
1c67c 20 75 73 65 64 20 66 6f 72 20 70 54 61 62 20 2a   used for pTab *
1c67d 2f 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20 70  /.  int iIdx = p
1c67e 50 61 72 73 65 2d 3e 6e 54 61 62 2b 31 3b 20 20  Parse->nTab+1;  
1c67f 20 20 20 2f 2a 20 42 74 72 65 65 20 63 75 72 73     /* Btree curs
1c680 6f 72 20 75 73 65 64 20 66 6f 72 20 70 49 6e 64  or used for pInd
1c681 65 78 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  ex */.  int addr
1c682 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
1c683 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
1c684 73 20 6f 66 20 74 6f 70 20 6f 66 20 6c 6f 6f 70  s of top of loop
1c685 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20   */.  int tnum; 
1c686 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c687 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67       /* Root pag
1c688 65 20 6f 66 20 69 6e 64 65 78 20 2a 2f 0a 20 20  e of index */.  
1c689 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20  Vdbe *v;        
1c68a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1c68b 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
1c68c 69 6e 74 6f 20 74 68 69 73 20 76 69 72 74 75 61  into this virtua
1c68d 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 4b  l machine */.  K
1c68e 65 79 49 6e 66 6f 20 2a 70 4b 65 79 3b 20 20 20  eyInfo *pKey;   
1c68f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1c690 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 69 6e 64   KeyInfo for ind
1c691 65 78 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 49  ex */.  int regI
1c692 64 78 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20  dxKey;          
1c693 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
1c694 65 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ers containing t
1c695 68 65 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a  he index key */.
1c696 20 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64 3b    int regRecord;
1c697 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c698 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
1c699 64 69 6e 67 20 61 73 73 65 6d 62 6c 69 65 64 20  ding assemblied 
1c69a 69 6e 64 65 78 20 72 65 63 6f 72 64 20 2a 2f 0a  index record */.
1c69b 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1c69c 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20  pParse->db;     
1c69d 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
1c69e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
1c69f 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74   int iDb = sqlit
1c6a0 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
1c6a1 64 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53 63 68  db, pIndex->pSch
1c6a2 65 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  ema);..#ifndef S
1c6a3 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
1c6a4 52 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28 20 73  RIZATION.  if( s
1c6a5 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
1c6a6 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 52  pParse, SQLITE_R
1c6a7 45 49 4e 44 45 58 2c 20 70 49 6e 64 65 78 2d 3e  EINDEX, pIndex->
1c6a8 7a 4e 61 6d 65 2c 20 30 2c 0a 20 20 20 20 20 20  zName, 0,.      
1c6a9 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
1c6aa 6d 65 20 29 20 29 7b 0a 20 20 20 20 72 65 74 75  me ) ){.    retu
1c6ab 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  rn;.  }.#endif..
1c6ac 20 20 2f 2a 20 52 65 71 75 69 72 65 20 61 20 77    /* Require a w
1c6ad 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65  rite-lock on the
1c6ae 20 74 61 62 6c 65 20 74 6f 20 70 65 72 66 6f 72   table to perfor
1c6af 6d 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e  m this operation
1c6b0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 54 61 62   */.  sqlite3Tab
1c6b1 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69  leLock(pParse, i
1c6b2 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20  Db, pTab->tnum, 
1c6b3 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  1, pTab->zName);
1c6b4 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  ..  v = sqlite3G
1c6b5 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
1c6b6 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74    if( v==0 ) ret
1c6b7 75 72 6e 3b 0a 20 20 69 66 28 20 6d 65 6d 52 6f  urn;.  if( memRo
1c6b8 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20  otPage>=0 ){.   
1c6b9 20 74 6e 75 6d 20 3d 20 6d 65 6d 52 6f 6f 74 50   tnum = memRootP
1c6ba 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  age;.  }else{.  
1c6bb 20 20 74 6e 75 6d 20 3d 20 70 49 6e 64 65 78 2d    tnum = pIndex-
1c6bc 3e 74 6e 75 6d 3b 0a 20 20 20 20 73 71 6c 69 74  >tnum;.    sqlit
1c6bd 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1c6be 4f 50 5f 43 6c 65 61 72 2c 20 74 6e 75 6d 2c 20  OP_Clear, tnum, 
1c6bf 69 44 62 29 3b 0a 20 20 7d 0a 20 20 70 4b 65 79  iDb);.  }.  pKey
1c6c0 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b   = sqlite3IndexK
1c6c1 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70  eyinfo(pParse, p
1c6c2 49 6e 64 65 78 29 3b 0a 20 20 73 71 6c 69 74 65  Index);.  sqlite
1c6c3 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
1c6c4 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 49 64  P_OpenWrite, iId
1c6c5 78 2c 20 74 6e 75 6d 2c 20 69 44 62 2c 20 0a 20  x, tnum, iDb, . 
1c6c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c6c7 20 20 20 28 63 68 61 72 20 2a 29 70 4b 65 79 2c     (char *)pKey,
1c6c8 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
1c6c9 4f 46 46 29 3b 0a 20 20 69 66 28 20 6d 65 6d 52  OFF);.  if( memR
1c6ca 6f 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20  ootPage>=0 ){.  
1c6cb 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1c6cc 6e 67 65 50 35 28 76 2c 20 31 29 3b 0a 20 20 7d  ngeP5(v, 1);.  }
1c6cd 0a 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61  .  sqlite3OpenTa
1c6ce 62 6c 65 28 70 50 61 72 73 65 2c 20 69 54 61 62  ble(pParse, iTab
1c6cf 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f  , iDb, pTab, OP_
1c6d0 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 61 64 64  OpenRead);.  add
1c6d1 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  r1 = sqlite3Vdbe
1c6d2 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77  AddOp2(v, OP_Rew
1c6d3 69 6e 64 2c 20 69 54 61 62 2c 20 30 29 3b 0a 20  ind, iTab, 0);. 
1c6d4 20 72 65 67 52 65 63 6f 72 64 20 3d 20 73 71 6c   regRecord = sql
1c6d5 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
1c6d6 50 61 72 73 65 29 3b 0a 20 20 72 65 67 49 64 78  Parse);.  regIdx
1c6d7 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 47 65 6e  Key = sqlite3Gen
1c6d8 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 70 50  erateIndexKey(pP
1c6d9 61 72 73 65 2c 20 70 49 6e 64 65 78 2c 20 69 54  arse, pIndex, iT
1c6da 61 62 2c 20 72 65 67 52 65 63 6f 72 64 2c 20 31  ab, regRecord, 1
1c6db 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78 2d  );.  if( pIndex-
1c6dc 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e  >onError!=OE_Non
1c6dd 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 31 2c  e ){.    int j1,
1c6de 20 6a 32 3b 0a 20 20 20 20 69 6e 74 20 72 65 67   j2;.    int reg
1c6df 52 6f 77 69 64 3b 0a 0a 20 20 20 20 72 65 67 52  Rowid;..    regR
1c6e0 6f 77 69 64 20 3d 20 72 65 67 49 64 78 4b 65 79  owid = regIdxKey
1c6e1 20 2b 20 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75   + pIndex->nColu
1c6e2 6d 6e 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c  mn;.    j1 = sql
1c6e3 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
1c6e4 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67  , OP_IsNull, reg
1c6e5 49 64 78 4b 65 79 2c 20 30 2c 20 70 49 6e 64 65  IdxKey, 0, pInde
1c6e6 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20  x->nColumn);.   
1c6e7 20 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j2 = sqlite3Vdb
1c6e8 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49 73  eAddOp4(v, OP_Is
1c6e9 55 6e 69 71 75 65 2c 20 69 49 64 78 2c 0a 20 20  Unique, iIdx,.  
1c6ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c6eb 20 20 20 20 20 20 20 20 20 30 2c 20 72 65 67 52           0, regR
1c6ec 6f 77 69 64 2c 20 28 63 68 61 72 2a 29 72 65 67  owid, (char*)reg
1c6ed 52 65 63 6f 72 64 2c 20 50 34 5f 49 4e 54 33 32  Record, P4_INT32
1c6ee 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1c6ef 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 48  beAddOp4(v, OP_H
1c6f0 61 6c 74 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53  alt, SQLITE_CONS
1c6f1 54 52 41 49 4e 54 2c 20 4f 45 5f 41 62 6f 72 74  TRAINT, OE_Abort
1c6f2 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
1c6f3 20 20 20 20 20 20 20 20 20 22 69 6e 64 65 78 65           "indexe
1c6f4 64 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 6e 6f  d columns are no
1c6f5 74 20 75 6e 69 71 75 65 22 2c 20 50 34 5f 53 54  t unique", P4_ST
1c6f6 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74  ATIC);.    sqlit
1c6f7 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
1c6f8 2c 20 6a 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  , j1);.    sqlit
1c6f9 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
1c6fa 2c 20 6a 32 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  , j2);.  }.  sql
1c6fb 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1c6fc 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20  , OP_IdxInsert, 
1c6fd 69 49 64 78 2c 20 72 65 67 52 65 63 6f 72 64 29  iIdx, regRecord)
1c6fe 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61  ;.  sqlite3Relea
1c6ff 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
1c700 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20  , regRecord);.  
1c701 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1c702 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 54  2(v, OP_Next, iT
1c703 61 62 2c 20 61 64 64 72 31 2b 31 29 3b 0a 20 20  ab, addr1+1);.  
1c704 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
1c705 65 72 65 28 76 2c 20 61 64 64 72 31 29 3b 0a 20  ere(v, addr1);. 
1c706 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1c707 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p1(v, OP_Close, 
1c708 69 54 61 62 29 3b 0a 20 20 73 71 6c 69 74 65 33  iTab);.  sqlite3
1c709 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
1c70a 5f 43 6c 6f 73 65 2c 20 69 49 64 78 29 3b 0a 7d  _Close, iIdx);.}
1c70b 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
1c70c 20 6e 65 77 20 69 6e 64 65 78 20 66 6f 72 20 61   new index for a
1c70d 6e 20 53 51 4c 20 74 61 62 6c 65 2e 20 20 70 4e  n SQL table.  pN
1c70e 61 6d 65 31 2e 70 4e 61 6d 65 32 20 69 73 20 74  ame1.pName2 is t
1c70f 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 69  he name of the i
1c710 6e 64 65 78 20 0a 2a 2a 20 61 6e 64 20 70 54 62  ndex .** and pTb
1c711 6c 4c 69 73 74 20 69 73 20 74 68 65 20 6e 61 6d  lList is the nam
1c712 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  e of the table t
1c713 68 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e 64  hat is to be ind
1c714 65 78 65 64 2e 20 20 42 6f 74 68 20 77 69 6c 6c  exed.  Both will
1c715 20 0a 2a 2a 20 62 65 20 4e 55 4c 4c 20 66 6f 72   .** be NULL for
1c716 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 6f   a primary key o
1c717 72 20 61 6e 20 69 6e 64 65 78 20 74 68 61 74 20  r an index that 
1c718 69 73 20 63 72 65 61 74 65 64 20 74 6f 20 73 61  is created to sa
1c719 74 69 73 66 79 20 61 0a 2a 2a 20 55 4e 49 51 55  tisfy a.** UNIQU
1c71a 45 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 20 49  E constraint.  I
1c71b 66 20 70 54 61 62 6c 65 20 61 6e 64 20 70 49 6e  f pTable and pIn
1c71c 64 65 78 20 61 72 65 20 4e 55 4c 4c 2c 20 75 73  dex are NULL, us
1c71d 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  e pParse->pNewTa
1c71e 62 6c 65 0a 2a 2a 20 61 73 20 74 68 65 20 74 61  ble.** as the ta
1c71f 62 6c 65 20 74 6f 20 62 65 20 69 6e 64 65 78 65  ble to be indexe
1c720 64 2e 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  d.  pParse->pNew
1c721 54 61 62 6c 65 20 69 73 20 61 20 74 61 62 6c 65  Table is a table
1c722 20 74 68 61 74 20 69 73 0a 2a 2a 20 63 75 72 72   that is.** curr
1c723 65 6e 74 6c 79 20 62 65 69 6e 67 20 63 6f 6e 73  ently being cons
1c724 74 72 75 63 74 65 64 20 62 79 20 61 20 43 52 45  tructed by a CRE
1c725 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
1c726 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 70 4c 69 73 74  ent..**.** pList
1c727 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 63 6f   is a list of co
1c728 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e 64 65  lumns to be inde
1c729 78 65 64 2e 20 20 70 4c 69 73 74 20 77 69 6c 6c  xed.  pList will
1c72a 20 62 65 20 4e 55 4c 4c 20 69 66 20 74 68 69 73   be NULL if this
1c72b 0a 2a 2a 20 69 73 20 61 20 70 72 69 6d 61 72 79  .** is a primary
1c72c 20 6b 65 79 20 6f 72 20 75 6e 69 71 75 65 2d 63   key or unique-c
1c72d 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20 74 68 65  onstraint on the
1c72e 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 6f 6c   most recent col
1c72f 75 6d 6e 20 61 64 64 65 64 0a 2a 2a 20 74 6f 20  umn added.** to 
1c730 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e  the table curren
1c731 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  tly under constr
1c732 75 63 74 69 6f 6e 2e 20 20 0a 2a 2f 0a 53 51 4c  uction.  .*/.SQL
1c733 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1c734 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 6e   sqlite3CreateIn
1c735 64 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70 50  dex(.  Parse *pP
1c736 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 41 6c 6c  arse,     /* All
1c737 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
1c738 75 74 20 74 68 69 73 20 70 61 72 73 65 20 2a 2f  ut this parse */
1c739 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31  .  Token *pName1
1c73a 2c 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 70  ,     /* First p
1c73b 61 72 74 20 6f 66 20 69 6e 64 65 78 20 6e 61 6d  art of index nam
1c73c 65 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a  e. May be NULL *
1c73d 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  /.  Token *pName
1c73e 32 2c 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64  2,     /* Second
1c73f 20 70 61 72 74 20 6f 66 20 69 6e 64 65 78 20 6e   part of index n
1c740 61 6d 65 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c  ame. May be NULL
1c741 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
1c742 54 62 6c 4e 61 6d 65 2c 20 2f 2a 20 54 61 62 6c  TblName, /* Tabl
1c743 65 20 74 6f 20 69 6e 64 65 78 2e 20 55 73 65 20  e to index. Use 
1c744 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
1c745 65 20 69 66 20 30 20 2a 2f 0a 20 20 45 78 70 72  e if 0 */.  Expr
1c746 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 2f  List *pList,   /
1c747 2a 20 41 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75  * A list of colu
1c748 6d 6e 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65  mns to be indexe
1c749 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72  d */.  int onErr
1c74a 6f 72 2c 20 20 20 20 20 20 20 2f 2a 20 4f 45 5f  or,       /* OE_
1c74b 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65  Abort, OE_Ignore
1c74c 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20 6f 72  , OE_Replace, or
1c74d 20 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a 20 20 54 6f   OE_None */.  To
1c74e 6b 65 6e 20 2a 70 53 74 61 72 74 2c 20 20 20 20  ken *pStart,    
1c74f 20 2f 2a 20 54 68 65 20 43 52 45 41 54 45 20 74   /* The CREATE t
1c750 6f 6b 65 6e 20 74 68 61 74 20 62 65 67 69 6e 73  oken that begins
1c751 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20   this statement 
1c752 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 45 6e 64  */.  Token *pEnd
1c753 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22  ,       /* The "
1c754 29 22 20 74 68 61 74 20 63 6c 6f 73 65 73 20 74  )" that closes t
1c755 68 65 20 43 52 45 41 54 45 20 49 4e 44 45 58 20  he CREATE INDEX 
1c756 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69  statement */.  i
1c757 6e 74 20 73 6f 72 74 4f 72 64 65 72 2c 20 20 20  nt sortOrder,   
1c758 20 20 2f 2a 20 53 6f 72 74 20 6f 72 64 65 72 20    /* Sort order 
1c759 6f 66 20 70 72 69 6d 61 72 79 20 6b 65 79 20 77  of primary key w
1c75a 68 65 6e 20 70 4c 69 73 74 3d 3d 4e 55 4c 4c 20  hen pList==NULL 
1c75b 2a 2f 0a 20 20 69 6e 74 20 69 66 4e 6f 74 45 78  */.  int ifNotEx
1c75c 69 73 74 20 20 20 20 20 2f 2a 20 4f 6d 69 74 20  ist     /* Omit 
1c75d 65 72 72 6f 72 20 69 66 20 69 6e 64 65 78 20 61  error if index a
1c75e 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 2a 2f  lready exists */
1c75f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  .){.  Table *pTa
1c760 62 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 61  b = 0;     /* Ta
1c761 62 6c 65 20 74 6f 20 62 65 20 69 6e 64 65 78 65  ble to be indexe
1c762 64 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49  d */.  Index *pI
1c763 6e 64 65 78 20 3d 20 30 3b 20 20 20 2f 2a 20 54  ndex = 0;   /* T
1c764 68 65 20 69 6e 64 65 78 20 74 6f 20 62 65 20 63  he index to be c
1c765 72 65 61 74 65 64 20 2a 2f 0a 20 20 63 68 61 72  reated */.  char
1c766 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20   *zName = 0;    
1c767 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
1c768 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 6e  index */.  int n
1c769 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  Name;           
1c76a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61  /* Number of cha
1c76b 72 61 63 74 65 72 73 20 69 6e 20 7a 4e 61 6d 65  racters in zName
1c76c 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a   */.  int i, j;.
1c76d 20 20 54 6f 6b 65 6e 20 6e 75 6c 6c 49 64 3b 20    Token nullId; 
1c76e 20 20 20 20 20 20 20 2f 2a 20 46 61 6b 65 20 74         /* Fake t
1c76f 6f 6b 65 6e 20 66 6f 72 20 61 6e 20 65 6d 70 74  oken for an empt
1c770 79 20 49 44 20 6c 69 73 74 20 2a 2f 0a 20 20 44  y ID list */.  D
1c771 62 46 69 78 65 72 20 73 46 69 78 3b 20 20 20 20  bFixer sFix;    
1c772 20 20 20 20 2f 2a 20 46 6f 72 20 61 73 73 69 67      /* For assig
1c773 6e 69 6e 67 20 64 61 74 61 62 61 73 65 20 6e 61  ning database na
1c774 6d 65 73 20 74 6f 20 70 54 61 62 6c 65 20 2a 2f  mes to pTable */
1c775 0a 20 20 69 6e 74 20 73 6f 72 74 4f 72 64 65 72  .  int sortOrder
1c776 4d 61 73 6b 3b 20 20 20 2f 2a 20 31 20 74 6f 20  Mask;   /* 1 to 
1c777 68 6f 6e 6f 72 20 44 45 53 43 20 69 6e 20 69 6e  honor DESC in in
1c778 64 65 78 2e 20 20 30 20 74 6f 20 69 67 6e 6f 72  dex.  0 to ignor
1c779 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  e. */.  sqlite3 
1c77a 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
1c77b 3b 0a 20 20 44 62 20 2a 70 44 62 3b 20 20 20 20  ;.  Db *pDb;    
1c77c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1c77d 73 70 65 63 69 66 69 63 20 74 61 62 6c 65 20 63  specific table c
1c77e 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 69 6e  ontaining the in
1c77f 64 65 78 65 64 20 64 61 74 61 62 61 73 65 20 2a  dexed database *
1c780 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  /.  int iDb;    
1c781 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
1c782 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  x of the databas
1c783 65 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 20  e that is being 
1c784 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 54 6f 6b  written */.  Tok
1c785 65 6e 20 2a 70 4e 61 6d 65 20 3d 20 30 3b 20 20  en *pName = 0;  
1c786 20 20 2f 2a 20 55 6e 71 75 61 6c 69 66 69 65 64    /* Unqualified
1c787 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64   name of the ind
1c788 65 78 20 74 6f 20 63 72 65 61 74 65 20 2a 2f 0a  ex to create */.
1c789 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
1c78a 74 5f 69 74 65 6d 20 2a 70 4c 69 73 74 49 74 65  t_item *pListIte
1c78b 6d 3b 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e  m; /* For loopin
1c78c 67 20 6f 76 65 72 20 70 4c 69 73 74 20 2a 2f 0a  g over pList */.
1c78d 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 69 6e    int nCol;.  in
1c78e 74 20 6e 45 78 74 72 61 20 3d 20 30 3b 0a 20 20  t nExtra = 0;.  
1c78f 63 68 61 72 20 2a 7a 45 78 74 72 61 3b 0a 0a 20  char *zExtra;.. 
1c790 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
1c791 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  r || db->mallocF
1c792 61 69 6c 65 64 20 7c 7c 20 49 4e 5f 44 45 43 4c  ailed || IN_DECL
1c793 41 52 45 5f 56 54 41 42 20 29 7b 0a 20 20 20 20  ARE_VTAB ){.    
1c794 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65  goto exit_create
1c795 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 0a 20 20 2f  _index;.  }..  /
1c796 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20  *.  ** Find the 
1c797 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 74 6f  table that is to
1c798 20 62 65 20 69 6e 64 65 78 65 64 2e 20 20 52 65   be indexed.  Re
1c799 74 75 72 6e 20 65 61 72 6c 79 20 69 66 20 6e 6f  turn early if no
1c79a 74 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a 20 20  t found..  */.  
1c79b 69 66 28 20 70 54 62 6c 4e 61 6d 65 21 3d 30 20  if( pTblName!=0 
1c79c 29 7b 0a 0a 20 20 20 20 2f 2a 20 55 73 65 20 74  ){..    /* Use t
1c79d 68 65 20 74 77 6f 2d 70 61 72 74 20 69 6e 64 65  he two-part inde
1c79e 78 20 6e 61 6d 65 20 74 6f 20 64 65 74 65 72 6d  x name to determ
1c79f 69 6e 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ine the database
1c7a0 20 0a 20 20 20 20 2a 2a 20 74 6f 20 73 65 61 72   .    ** to sear
1c7a1 63 68 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  ch for the table
1c7a2 2e 20 27 46 69 78 27 20 74 68 65 20 74 61 62 6c  . 'Fix' the tabl
1c7a3 65 20 6e 61 6d 65 20 74 6f 20 74 68 69 73 20 64  e name to this d
1c7a4 62 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 20  b.    ** before 
1c7a5 6c 6f 6f 6b 69 6e 67 20 75 70 20 74 68 65 20 74  looking up the t
1c7a6 61 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  able..    */.   
1c7a7 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 31 20   assert( pName1 
1c7a8 26 26 20 70 4e 61 6d 65 32 20 29 3b 0a 20 20 20  && pName2 );.   
1c7a9 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77   iDb = sqlite3Tw
1c7aa 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65  oPartName(pParse
1c7ab 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32  , pName1, pName2
1c7ac 2c 20 26 70 4e 61 6d 65 29 3b 0a 20 20 20 20 69  , &pName);.    i
1c7ad 66 28 20 69 44 62 3c 30 20 29 20 67 6f 74 6f 20  f( iDb<0 ) goto 
1c7ae 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
1c7af 78 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  x;..#ifndef SQLI
1c7b0 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 20  TE_OMIT_TEMPDB. 
1c7b1 20 20 20 2f 2a 20 49 66 20 74 68 65 20 69 6e 64     /* If the ind
1c7b2 65 78 20 6e 61 6d 65 20 77 61 73 20 75 6e 71 75  ex name was unqu
1c7b3 61 6c 69 66 69 65 64 2c 20 63 68 65 63 6b 20 69  alified, check i
1c7b4 66 20 74 68 65 20 74 68 65 20 74 61 62 6c 65 0a  f the the table.
1c7b5 20 20 20 20 2a 2a 20 69 73 20 61 20 74 65 6d 70      ** is a temp
1c7b6 20 74 61 62 6c 65 2e 20 49 66 20 73 6f 2c 20 73   table. If so, s
1c7b7 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20  et the database 
1c7b8 74 6f 20 31 2e 20 44 6f 20 6e 6f 74 20 64 6f 20  to 1. Do not do 
1c7b9 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 66 20 69  this.    ** if i
1c7ba 6e 69 74 69 61 6c 69 73 69 6e 67 20 61 20 64 61  nitialising a da
1c7bb 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 0a 20  tabase schema.. 
1c7bc 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 64     */.    if( !d
1c7bd 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a  b->init.busy ){.
1c7be 20 20 20 20 20 20 70 54 61 62 20 3d 20 73 71 6c        pTab = sql
1c7bf 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75  ite3SrcListLooku
1c7c0 70 28 70 50 61 72 73 65 2c 20 70 54 62 6c 4e 61  p(pParse, pTblNa
1c7c1 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  me);.      if( p
1c7c2 4e 61 6d 65 32 20 26 26 20 70 4e 61 6d 65 32 2d  Name2 && pName2-
1c7c3 3e 6e 3d 3d 30 20 26 26 20 70 54 61 62 20 26 26  >n==0 && pTab &&
1c7c4 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3d 3d   pTab->pSchema==
1c7c5 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65  db->aDb[1].pSche
1c7c6 6d 61 20 29 7b 0a 20 20 20 20 20 20 20 20 69 44  ma ){.        iD
1c7c7 62 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  b = 1;.      }. 
1c7c8 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
1c7c9 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 49   if( sqlite3FixI
1c7ca 6e 69 74 28 26 73 46 69 78 2c 20 70 50 61 72 73  nit(&sFix, pPars
1c7cb 65 2c 20 69 44 62 2c 20 22 69 6e 64 65 78 22 2c  e, iDb, "index",
1c7cc 20 70 4e 61 6d 65 29 20 26 26 0a 20 20 20 20 20   pName) &&.     
1c7cd 20 20 20 73 71 6c 69 74 65 33 46 69 78 53 72 63     sqlite3FixSrc
1c7ce 4c 69 73 74 28 26 73 46 69 78 2c 20 70 54 62 6c  List(&sFix, pTbl
1c7cf 4e 61 6d 65 29 0a 20 20 20 20 29 7b 0a 20 20 20  Name).    ){.   
1c7d0 20 20 20 2f 2a 20 42 65 63 61 75 73 65 20 74 68     /* Because th
1c7d1 65 20 70 61 72 73 65 72 20 63 6f 6e 73 74 72 75  e parser constru
1c7d2 63 74 73 20 70 54 62 6c 4e 61 6d 65 20 66 72 6f  cts pTblName fro
1c7d3 6d 20 61 20 73 69 6e 67 6c 65 20 69 64 65 6e 74  m a single ident
1c7d4 69 66 69 65 72 2c 0a 20 20 20 20 20 20 2a 2a 20  ifier,.      ** 
1c7d5 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73  sqlite3FixSrcLis
1c7d6 74 20 63 61 6e 20 6e 65 76 65 72 20 66 61 69 6c  t can never fail
1c7d7 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72  . */.      asser
1c7d8 74 28 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  t(0);.    }.    
1c7d9 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f  pTab = sqlite3Lo
1c7da 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65  cateTable(pParse
1c7db 2c 20 30 2c 20 70 54 62 6c 4e 61 6d 65 2d 3e 61  , 0, pTblName->a
1c7dc 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 0a 20 20 20 20  [0].zName, .    
1c7dd 20 20 20 20 70 54 62 6c 4e 61 6d 65 2d 3e 61 5b      pTblName->a[
1c7de 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  0].zDatabase);. 
1c7df 20 20 20 69 66 28 20 21 70 54 61 62 20 29 20 67     if( !pTab ) g
1c7e0 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f  oto exit_create_
1c7e1 69 6e 64 65 78 3b 0a 20 20 20 20 61 73 73 65 72  index;.    asser
1c7e2 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  t( db->aDb[iDb].
1c7e3 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70  pSchema==pTab->p
1c7e4 53 63 68 65 6d 61 20 29 3b 0a 20 20 7d 65 6c 73  Schema );.  }els
1c7e5 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
1c7e6 4e 61 6d 65 3d 3d 30 20 29 3b 0a 20 20 20 20 70  Name==0 );.    p
1c7e7 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e  Tab = pParse->pN
1c7e8 65 77 54 61 62 6c 65 3b 0a 20 20 20 20 69 66 28  ewTable;.    if(
1c7e9 20 21 70 54 61 62 20 29 20 67 6f 74 6f 20 65 78   !pTab ) goto ex
1c7ea 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
1c7eb 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74  .    iDb = sqlit
1c7ec 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
1c7ed 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
1c7ee 61 29 3b 0a 20 20 7d 0a 20 20 70 44 62 20 3d 20  a);.  }.  pDb = 
1c7ef 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a  &db->aDb[iDb];..
1c7f0 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 7c 7c    if( pTab==0 ||
1c7f1 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 20   pParse->nErr ) 
1c7f2 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65  goto exit_create
1c7f3 5f 69 6e 64 65 78 3b 0a 20 20 69 66 28 20 70 54  _index;.  if( pT
1c7f4 61 62 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a  ab->readOnly ){.
1c7f5 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1c7f6 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62  Msg(pParse, "tab
1c7f7 6c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65  le %s may not be
1c7f8 20 69 6e 64 65 78 65 64 22 2c 20 70 54 61 62 2d   indexed", pTab-
1c7f9 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74  >zName);.    got
1c7fa 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
1c7fb 64 65 78 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66  dex;.  }.#ifndef
1c7fc 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45   SQLITE_OMIT_VIE
1c7fd 57 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53  W.  if( pTab->pS
1c7fe 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c  elect ){.    sql
1c7ff 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1c800 72 73 65 2c 20 22 76 69 65 77 73 20 6d 61 79 20  rse, "views may 
1c801 6e 6f 74 20 62 65 20 69 6e 64 65 78 65 64 22 29  not be indexed")
1c802 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  ;.    goto exit_
1c803 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
1c804 7d 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  }.#endif.#ifndef
1c805 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1c806 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20  TUALTABLE.  if( 
1c807 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
1c808 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
1c809 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1c80a 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d  virtual tables m
1c80b 61 79 20 6e 6f 74 20 62 65 20 69 6e 64 65 78 65  ay not be indexe
1c80c 64 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78  d");.    goto ex
1c80d 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
1c80e 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
1c80f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20  *.  ** Find the 
1c810 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65  name of the inde
1c811 78 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68  x.  Make sure th
1c812 65 72 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61  ere is not alrea
1c813 64 79 20 61 6e 6f 74 68 65 72 0a 20 20 2a 2a 20  dy another.  ** 
1c814 69 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 20 77  index or table w
1c815 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d  ith the same nam
1c816 65 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45  e.  .  **.  ** E
1c817 78 63 65 70 74 69 6f 6e 3a 20 20 49 66 20 77 65  xception:  If we
1c818 20 61 72 65 20 72 65 61 64 69 6e 67 20 74 68 65   are reading the
1c819 20 6e 61 6d 65 73 20 6f 66 20 70 65 72 6d 61 6e   names of perman
1c81a 65 6e 74 20 69 6e 64 69 63 65 73 20 66 72 6f 6d  ent indices from
1c81b 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65   the.  ** sqlite
1c81c 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 28 62  _master table (b
1c81d 65 63 61 75 73 65 20 73 6f 6d 65 20 6f 74 68 65  ecause some othe
1c81e 72 20 70 72 6f 63 65 73 73 20 63 68 61 6e 67 65  r process change
1c81f 64 20 74 68 65 20 73 63 68 65 6d 61 29 20 61 6e  d the schema) an
1c820 64 0a 20 20 2a 2a 20 6f 6e 65 20 6f 66 20 74 68  d.  ** one of th
1c821 65 20 69 6e 64 65 78 20 6e 61 6d 65 73 20 63 6f  e index names co
1c822 6c 6c 69 64 65 73 20 77 69 74 68 20 74 68 65 20  llides with the 
1c823 6e 61 6d 65 20 6f 66 20 61 20 74 65 6d 70 6f 72  name of a tempor
1c824 61 72 79 20 74 61 62 6c 65 20 6f 72 0a 20 20 2a  ary table or.  *
1c825 2a 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 77 65  * index, then we
1c826 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74   will continue t
1c827 6f 20 70 72 6f 63 65 73 73 20 74 68 69 73 20 69  o process this i
1c828 6e 64 65 78 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ndex..  **.  ** 
1c829 49 66 20 70 4e 61 6d 65 3d 3d 30 20 69 74 20 6d  If pName==0 it m
1c82a 65 61 6e 73 20 74 68 61 74 20 77 65 20 61 72 65  eans that we are
1c82b 0a 20 20 2a 2a 20 64 65 61 6c 69 6e 67 20 77 69  .  ** dealing wi
1c82c 74 68 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79  th a primary key
1c82d 20 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e 73 74   or UNIQUE const
1c82e 72 61 69 6e 74 2e 20 20 57 65 20 68 61 76 65 20  raint.  We have 
1c82f 74 6f 20 69 6e 76 65 6e 74 20 6f 75 72 0a 20 20  to invent our.  
1c830 2a 2a 20 6f 77 6e 20 6e 61 6d 65 2e 0a 20 20 2a  ** own name..  *
1c831 2f 0a 20 20 69 66 28 20 70 4e 61 6d 65 20 29 7b  /.  if( pName ){
1c832 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c  .    zName = sql
1c833 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
1c834 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20  n(db, pName);.  
1c835 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
1c836 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65  =sqlite3ReadSche
1c837 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74  ma(pParse) ) got
1c838 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
1c839 64 65 78 3b 0a 20 20 20 20 69 66 28 20 7a 4e 61  dex;.    if( zNa
1c83a 6d 65 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78 69  me==0 ) goto exi
1c83b 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
1c83c 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f      if( SQLITE_O
1c83d 4b 21 3d 73 71 6c 69 74 65 33 43 68 65 63 6b 4f  K!=sqlite3CheckO
1c83e 62 6a 65 63 74 4e 61 6d 65 28 70 50 61 72 73 65  bjectName(pParse
1c83f 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20  , zName) ){.    
1c840 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
1c841 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a  te_index;.    }.
1c842 20 20 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69      if( !db->ini
1c843 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20 20  t.busy ){.      
1c844 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73  if( SQLITE_OK!=s
1c845 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
1c846 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20  (pParse) ) goto 
1c847 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
1c848 78 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  x;.      if( sql
1c849 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62  ite3FindTable(db
1c84a 2c 20 7a 4e 61 6d 65 2c 20 30 29 21 3d 30 20 29  , zName, 0)!=0 )
1c84b 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1c84c 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1c84d 2c 20 22 74 68 65 72 65 20 69 73 20 61 6c 72 65  , "there is alre
1c84e 61 64 79 20 61 20 74 61 62 6c 65 20 6e 61 6d 65  ady a table name
1c84f 64 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  d %s", zName);. 
1c850 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74         goto exit
1c851 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
1c852 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1c853 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64   if( sqlite3Find
1c854 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c  Index(db, zName,
1c855 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 21 3d 30 20   pDb->zName)!=0 
1c856 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 69 66  ){.      if( !if
1c857 4e 6f 74 45 78 69 73 74 20 29 7b 0a 20 20 20 20  NotExist ){.    
1c858 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1c859 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69 6e 64  Msg(pParse, "ind
1c85a 65 78 20 25 73 20 61 6c 72 65 61 64 79 20 65 78  ex %s already ex
1c85b 69 73 74 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  ists", zName);. 
1c85c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74       }.      got
1c85d 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
1c85e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  dex;.    }.  }el
1c85f 73 65 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75  se{.    char zBu
1c860 66 5b 33 30 5d 3b 0a 20 20 20 20 69 6e 74 20 6e  f[30];.    int n
1c861 3b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 4c 6f  ;.    Index *pLo
1c862 6f 70 3b 0a 20 20 20 20 66 6f 72 28 70 4c 6f 6f  op;.    for(pLoo
1c863 70 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 2c 20  p=pTab->pIndex, 
1c864 6e 3d 31 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f  n=1; pLoop; pLoo
1c865 70 3d 70 4c 6f 6f 70 2d 3e 70 4e 65 78 74 2c 20  p=pLoop->pNext, 
1c866 6e 2b 2b 29 7b 7d 0a 20 20 20 20 73 71 6c 69 74  n++){}.    sqlit
1c867 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65  e3_snprintf(size
1c868 6f 66 28 7a 42 75 66 29 2c 7a 42 75 66 2c 22 5f  of(zBuf),zBuf,"_
1c869 25 64 22 2c 6e 29 3b 0a 20 20 20 20 7a 4e 61 6d  %d",n);.    zNam
1c86a 65 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74  e = 0;.    sqlit
1c86b 65 33 53 65 74 53 74 72 69 6e 67 28 26 7a 4e 61  e3SetString(&zNa
1c86c 6d 65 2c 20 22 73 71 6c 69 74 65 5f 61 75 74 6f  me, "sqlite_auto
1c86d 69 6e 64 65 78 5f 22 2c 20 70 54 61 62 2d 3e 7a  index_", pTab->z
1c86e 4e 61 6d 65 2c 20 7a 42 75 66 2c 20 28 63 68 61  Name, zBuf, (cha
1c86f 72 2a 29 30 29 3b 0a 20 20 20 20 69 66 28 20 7a  r*)0);.    if( z
1c870 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Name==0 ){.     
1c871 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1c872 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 67 6f 74  d = 1;.      got
1c873 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
1c874 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  dex;.    }.  }..
1c875 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 61    /* Check for a
1c876 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 74 6f 20  uthorization to 
1c877 63 72 65 61 74 65 20 61 6e 20 69 6e 64 65 78 2e  create an index.
1c878 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  .  */.#ifndef SQ
1c879 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
1c87a 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20  IZATION.  {.    
1c87b 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20  const char *zDb 
1c87c 3d 20 70 44 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20  = pDb->zName;.  
1c87d 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
1c87e 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53  hCheck(pParse, S
1c87f 51 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20 53 43  QLITE_INSERT, SC
1c880 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c  HEMA_TABLE(iDb),
1c881 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20   0, zDb) ){.    
1c882 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
1c883 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a  te_index;.    }.
1c884 20 20 20 20 69 20 3d 20 53 51 4c 49 54 45 5f 43      i = SQLITE_C
1c885 52 45 41 54 45 5f 49 4e 44 45 58 3b 0a 20 20 20  REATE_INDEX;.   
1c886 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44   if( !OMIT_TEMPD
1c887 42 20 26 26 20 69 44 62 3d 3d 31 20 29 20 69 20  B && iDb==1 ) i 
1c888 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f  = SQLITE_CREATE_
1c889 54 45 4d 50 5f 49 4e 44 45 58 3b 0a 20 20 20 20  TEMP_INDEX;.    
1c88a 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43  if( sqlite3AuthC
1c88b 68 65 63 6b 28 70 50 61 72 73 65 2c 20 69 2c 20  heck(pParse, i, 
1c88c 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61  zName, pTab->zNa
1c88d 6d 65 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20  me, zDb) ){.    
1c88e 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
1c88f 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a  te_index;.    }.
1c890 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
1c891 20 49 66 20 70 4c 69 73 74 3d 3d 30 2c 20 69 74   If pList==0, it
1c892 20 6d 65 61 6e 73 20 74 68 69 73 20 72 6f 75 74   means this rout
1c893 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 20 74  ine was called t
1c894 6f 20 6d 61 6b 65 20 61 20 70 72 69 6d 61 72 79  o make a primary
1c895 0a 20 20 2a 2a 20 6b 65 79 20 6f 75 74 20 6f 66  .  ** key out of
1c896 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e   the last column
1c897 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 74 61   added to the ta
1c898 62 6c 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  ble under constr
1c899 75 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53 6f 20  uction..  ** So 
1c89a 63 72 65 61 74 65 20 61 20 66 61 6b 65 20 6c 69  create a fake li
1c89b 73 74 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 74  st to simulate t
1c89c 68 69 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  his..  */.  if( 
1c89d 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20  pList==0 ){.    
1c89e 6e 75 6c 6c 49 64 2e 7a 20 3d 20 28 75 38 2a 29  nullId.z = (u8*)
1c89f 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 54 61 62 2d  pTab->aCol[pTab-
1c8a0 3e 6e 43 6f 6c 2d 31 5d 2e 7a 4e 61 6d 65 3b 0a  >nCol-1].zName;.
1c8a1 20 20 20 20 6e 75 6c 6c 49 64 2e 6e 20 3d 20 73      nullId.n = s
1c8a2 74 72 6c 65 6e 28 28 63 68 61 72 2a 29 6e 75 6c  trlen((char*)nul
1c8a3 6c 49 64 2e 7a 29 3b 0a 20 20 20 20 70 4c 69 73  lId.z);.    pLis
1c8a4 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  t = sqlite3ExprL
1c8a5 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
1c8a6 2c 20 30 2c 20 30 2c 20 26 6e 75 6c 6c 49 64 29  , 0, 0, &nullId)
1c8a7 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d  ;.    if( pList=
1c8a8 3d 30 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63  =0 ) goto exit_c
1c8a9 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20  reate_index;.   
1c8aa 20 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 73 6f 72   pList->a[0].sor
1c8ab 74 4f 72 64 65 72 20 3d 20 73 6f 72 74 4f 72 64  tOrder = sortOrd
1c8ac 65 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69  er;.  }..  /* Fi
1c8ad 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e  gure out how man
1c8ae 79 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65  y bytes of space
1c8af 20 61 72 65 20 72 65 71 75 69 72 65 64 20 74 6f   are required to
1c8b0 20 73 74 6f 72 65 20 65 78 70 6c 69 63 69 74 6c   store explicitl
1c8b1 79 0a 20 20 2a 2a 20 73 70 65 63 69 66 69 65 64  y.  ** specified
1c8b2 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
1c8b3 6e 63 65 20 6e 61 6d 65 73 2e 0a 20 20 2a 2f 0a  nce names..  */.
1c8b4 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69    for(i=0; i<pLi
1c8b5 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  st->nExpr; i++){
1c8b6 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72  .    Expr *pExpr
1c8b7 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70   = pList->a[i].p
1c8b8 45 78 70 72 3b 0a 20 20 20 20 69 66 28 20 70 45  Expr;.    if( pE
1c8b9 78 70 72 20 29 7b 0a 20 20 20 20 20 20 6e 45 78  xpr ){.      nEx
1c8ba 74 72 61 20 2b 3d 20 28 31 20 2b 20 73 74 72 6c  tra += (1 + strl
1c8bb 65 6e 28 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 2d  en(pExpr->pColl-
1c8bc 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 7d 0a  >zName));.    }.
1c8bd 20 20 7d 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20    }..  /* .  ** 
1c8be 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 69 6e 64  Allocate the ind
1c8bf 65 78 20 73 74 72 75 63 74 75 72 65 2e 20 0a 20  ex structure. . 
1c8c0 20 2a 2f 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 74   */.  nName = st
1c8c1 72 6c 65 6e 28 7a 4e 61 6d 65 29 3b 0a 20 20 6e  rlen(zName);.  n
1c8c2 43 6f 6c 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78  Col = pList->nEx
1c8c3 70 72 3b 0a 20 20 70 49 6e 64 65 78 20 3d 20 73  pr;.  pIndex = s
1c8c4 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
1c8c5 72 6f 28 64 62 2c 20 0a 20 20 20 20 20 20 73 69  ro(db, .      si
1c8c6 7a 65 6f 66 28 49 6e 64 65 78 29 20 2b 20 20 20  zeof(Index) +   
1c8c7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
1c8c8 64 65 78 20 73 74 72 75 63 74 75 72 65 20 20 2a  dex structure  *
1c8c9 2f 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 69  /.      sizeof(i
1c8ca 6e 74 29 2a 6e 43 6f 6c 20 2b 20 20 20 20 20 20  nt)*nCol +      
1c8cb 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 69       /* Index.ai
1c8cc 43 6f 6c 75 6d 6e 20 20 20 2a 2f 0a 20 20 20 20  Column   */.    
1c8cd 20 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28 6e    sizeof(int)*(n
1c8ce 43 6f 6c 2b 31 29 20 2b 20 20 20 20 20 20 20 2f  Col+1) +       /
1c8cf 2a 20 49 6e 64 65 78 2e 61 69 52 6f 77 45 73 74  * Index.aiRowEst
1c8d0 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65     */.      size
1c8d1 6f 66 28 63 68 61 72 20 2a 29 2a 6e 43 6f 6c 20  of(char *)*nCol 
1c8d2 2b 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65  +        /* Inde
1c8d3 78 2e 61 7a 43 6f 6c 6c 20 20 20 20 20 2a 2f 0a  x.azColl     */.
1c8d4 20 20 20 20 20 20 73 69 7a 65 6f 66 28 75 38 29        sizeof(u8)
1c8d5 2a 6e 43 6f 6c 20 2b 20 20 20 20 20 20 20 20 20  *nCol +         
1c8d6 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 53 6f 72     /* Index.aSor
1c8d7 74 4f 72 64 65 72 20 2a 2f 0a 20 20 20 20 20 20  tOrder */.      
1c8d8 6e 4e 61 6d 65 20 2b 20 31 20 2b 20 20 20 20 20  nName + 1 +     
1c8d9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c8da 49 6e 64 65 78 2e 7a 4e 61 6d 65 20 20 20 20 20  Index.zName     
1c8db 20 2a 2f 0a 20 20 20 20 20 20 6e 45 78 74 72 61   */.      nExtra
1c8dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c8dd 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74         /* Collat
1c8de 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d  ion sequence nam
1c8df 65 73 20 2a 2f 0a 20 20 29 3b 0a 20 20 69 66 28  es */.  );.  if(
1c8e0 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1c8e1 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78  d ){.    goto ex
1c8e2 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
1c8e3 0a 20 20 7d 0a 20 20 70 49 6e 64 65 78 2d 3e 61  .  }.  pIndex->a
1c8e4 7a 43 6f 6c 6c 20 3d 20 28 63 68 61 72 2a 2a 29  zColl = (char**)
1c8e5 28 26 70 49 6e 64 65 78 5b 31 5d 29 3b 0a 20 20  (&pIndex[1]);.  
1c8e6 70 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e  pIndex->aiColumn
1c8e7 20 3d 20 28 69 6e 74 20 2a 29 28 26 70 49 6e 64   = (int *)(&pInd
1c8e8 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 6e 43 6f 6c 5d  ex->azColl[nCol]
1c8e9 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 61 69 52  );.  pIndex->aiR
1c8ea 6f 77 45 73 74 20 3d 20 28 75 6e 73 69 67 6e 65  owEst = (unsigne
1c8eb 64 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61 69  d *)(&pIndex->ai
1c8ec 43 6f 6c 75 6d 6e 5b 6e 43 6f 6c 5d 29 3b 0a 20  Column[nCol]);. 
1c8ed 20 70 49 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72   pIndex->aSortOr
1c8ee 64 65 72 20 3d 20 28 75 38 20 2a 29 28 26 70 49  der = (u8 *)(&pI
1c8ef 6e 64 65 78 2d 3e 61 69 52 6f 77 45 73 74 5b 6e  ndex->aiRowEst[n
1c8f0 43 6f 6c 2b 31 5d 29 3b 0a 20 20 70 49 6e 64 65  Col+1]);.  pInde
1c8f1 78 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72  x->zName = (char
1c8f2 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61 53 6f   *)(&pIndex->aSo
1c8f3 72 74 4f 72 64 65 72 5b 6e 43 6f 6c 5d 29 3b 0a  rtOrder[nCol]);.
1c8f4 20 20 7a 45 78 74 72 61 20 3d 20 28 63 68 61 72    zExtra = (char
1c8f5 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 7a 4e 61   *)(&pIndex->zNa
1c8f6 6d 65 5b 6e 4e 61 6d 65 2b 31 5d 29 3b 0a 20 20  me[nName+1]);.  
1c8f7 6d 65 6d 63 70 79 28 70 49 6e 64 65 78 2d 3e 7a  memcpy(pIndex->z
1c8f8 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  Name, zName, nNa
1c8f9 6d 65 2b 31 29 3b 0a 20 20 70 49 6e 64 65 78 2d  me+1);.  pIndex-
1c8fa 3e 70 54 61 62 6c 65 20 3d 20 70 54 61 62 3b 0a  >pTable = pTab;.
1c8fb 20 20 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d    pIndex->nColum
1c8fc 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72  n = pList->nExpr
1c8fd 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 72  ;.  pIndex->onEr
1c8fe 72 6f 72 20 3d 20 6f 6e 45 72 72 6f 72 3b 0a 20  ror = onError;. 
1c8ff 20 70 49 6e 64 65 78 2d 3e 61 75 74 6f 49 6e 64   pIndex->autoInd
1c900 65 78 20 3d 20 70 4e 61 6d 65 3d 3d 30 3b 0a 20  ex = pName==0;. 
1c901 20 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61   pIndex->pSchema
1c902 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
1c903 70 53 63 68 65 6d 61 3b 0a 0a 20 20 2f 2a 20 43  pSchema;..  /* C
1c904 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 77  heck to see if w
1c905 65 20 73 68 6f 75 6c 64 20 68 6f 6e 6f 72 20 44  e should honor D
1c906 45 53 43 20 72 65 71 75 65 73 74 73 20 6f 6e 20  ESC requests on 
1c907 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 73 0a 20 20  index columns.  
1c908 2a 2f 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 53  */.  if( pDb->pS
1c909 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d  chema->file_form
1c90a 61 74 3e 3d 34 20 29 7b 0a 20 20 20 20 73 6f 72  at>=4 ){.    sor
1c90b 74 4f 72 64 65 72 4d 61 73 6b 20 3d 20 2d 31 3b  tOrderMask = -1;
1c90c 20 20 20 2f 2a 20 48 6f 6e 6f 72 20 44 45 53 43     /* Honor DESC
1c90d 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   */.  }else{.   
1c90e 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 20 3d   sortOrderMask =
1c90f 20 30 3b 20 20 20 20 2f 2a 20 49 67 6e 6f 72 65   0;    /* Ignore
1c910 20 44 45 53 43 20 2a 2f 0a 20 20 7d 0a 0a 20 20   DESC */.  }..  
1c911 2f 2a 20 53 63 61 6e 20 74 68 65 20 6e 61 6d 65  /* Scan the name
1c912 73 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73  s of the columns
1c913 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f   of the table to
1c914 20 62 65 20 69 6e 64 65 78 65 64 20 61 6e 64 0a   be indexed and.
1c915 20 20 2a 2a 20 6c 6f 61 64 20 74 68 65 20 63 6f    ** load the co
1c916 6c 75 6d 6e 20 69 6e 64 69 63 65 73 20 69 6e 74  lumn indices int
1c917 6f 20 74 68 65 20 49 6e 64 65 78 20 73 74 72 75  o the Index stru
1c918 63 74 75 72 65 2e 20 20 52 65 70 6f 72 74 20 61  cture.  Report a
1c919 6e 20 65 72 72 6f 72 0a 20 20 2a 2a 20 69 66 20  n error.  ** if 
1c91a 61 6e 79 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f  any column is no
1c91b 74 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a 20 20  t found..  */.  
1c91c 66 6f 72 28 69 3d 30 2c 20 70 4c 69 73 74 49 74  for(i=0, pListIt
1c91d 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 70  em=pList->a; i<p
1c91e 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  List->nExpr; i++
1c91f 2c 20 70 4c 69 73 74 49 74 65 6d 2b 2b 29 7b 0a  , pListItem++){.
1c920 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
1c921 7a 43 6f 6c 4e 61 6d 65 20 3d 20 70 4c 69 73 74  zColName = pList
1c922 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20  Item->zName;.   
1c923 20 43 6f 6c 75 6d 6e 20 2a 70 54 61 62 43 6f 6c   Column *pTabCol
1c924 3b 0a 20 20 20 20 69 6e 74 20 72 65 71 75 65 73  ;.    int reques
1c925 74 65 64 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20  tedSortOrder;.  
1c926 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20 20    char *zColl;  
1c927 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c928 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65   /* Collation se
1c929 71 75 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a 0a  quence name */..
1c92a 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 54 61      for(j=0, pTa
1c92b 62 43 6f 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c 3b  bCol=pTab->aCol;
1c92c 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a   j<pTab->nCol; j
1c92d 2b 2b 2c 20 70 54 61 62 43 6f 6c 2b 2b 29 7b 0a  ++, pTabCol++){.
1c92e 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
1c92f 33 53 74 72 49 43 6d 70 28 7a 43 6f 6c 4e 61 6d  3StrICmp(zColNam
1c930 65 2c 20 70 54 61 62 43 6f 6c 2d 3e 7a 4e 61 6d  e, pTabCol->zNam
1c931 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20  e)==0 ) break;. 
1c932 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6a 3e 3d     }.    if( j>=
1c933 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20  pTab->nCol ){.  
1c934 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1c935 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62  Msg(pParse, "tab
1c936 6c 65 20 25 73 20 68 61 73 20 6e 6f 20 63 6f 6c  le %s has no col
1c937 75 6d 6e 20 6e 61 6d 65 64 20 25 73 22 2c 0a 20  umn named %s",. 
1c938 20 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61         pTab->zNa
1c939 6d 65 2c 20 7a 43 6f 6c 4e 61 6d 65 29 3b 0a 20  me, zColName);. 
1c93a 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63       goto exit_c
1c93b 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20  reate_index;.   
1c93c 20 7d 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20   }.    /* TODO: 
1c93d 20 41 64 64 20 61 20 74 65 73 74 20 74 6f 20 6d   Add a test to m
1c93e 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68  ake sure that th
1c93f 65 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 20 69 73  e same column is
1c940 20 6e 6f 74 20 6e 61 6d 65 64 0a 20 20 20 20 2a   not named.    *
1c941 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65  * more than once
1c942 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65   within the same
1c943 20 69 6e 64 65 78 2e 20 20 4f 6e 6c 79 20 74 68   index.  Only th
1c944 65 20 66 69 72 73 74 20 69 6e 73 74 61 6e 63 65  e first instance
1c945 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 20 63   of.    ** the c
1c946 6f 6c 75 6d 6e 20 77 69 6c 6c 20 65 76 65 72 20  olumn will ever 
1c947 62 65 20 75 73 65 64 20 62 79 20 74 68 65 20 6f  be used by the o
1c948 70 74 69 6d 69 7a 65 72 2e 20 20 4e 6f 74 65 20  ptimizer.  Note 
1c949 74 68 61 74 20 75 73 69 6e 67 20 74 68 65 0a 20  that using the. 
1c94a 20 20 20 2a 2a 20 73 61 6d 65 20 63 6f 6c 75 6d     ** same colum
1c94b 6e 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65  n more than once
1c94c 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e 20 65 72   cannot be an er
1c94d 72 6f 72 20 62 65 63 61 75 73 65 20 74 68 61 74  ror because that
1c94e 20 77 6f 75 6c 64 20 0a 20 20 20 20 2a 2a 20 62   would .    ** b
1c94f 72 65 61 6b 20 62 61 63 6b 77 61 72 64 73 20 63  reak backwards c
1c950 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2d 20 69  ompatibility - i
1c951 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61 20  t needs to be a 
1c952 77 61 72 6e 69 6e 67 2e 0a 20 20 20 20 2a 2f 0a  warning..    */.
1c953 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 69 43 6f      pIndex->aiCo
1c954 6c 75 6d 6e 5b 69 5d 20 3d 20 6a 3b 0a 20 20 20  lumn[i] = j;.   
1c955 20 69 66 28 20 70 4c 69 73 74 49 74 65 6d 2d 3e   if( pListItem->
1c956 70 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 61  pExpr ){.      a
1c957 73 73 65 72 74 28 20 70 4c 69 73 74 49 74 65 6d  ssert( pListItem
1c958 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 29  ->pExpr->pColl )
1c959 3b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20  ;.      zColl = 
1c95a 7a 45 78 74 72 61 3b 0a 20 20 20 20 20 20 73 71  zExtra;.      sq
1c95b 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e  lite3_snprintf(n
1c95c 45 78 74 72 61 2c 20 7a 45 78 74 72 61 2c 20 22  Extra, zExtra, "
1c95d 25 73 22 2c 20 70 4c 69 73 74 49 74 65 6d 2d 3e  %s", pListItem->
1c95e 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 2d 3e 7a 4e  pExpr->pColl->zN
1c95f 61 6d 65 29 3b 0a 20 20 20 20 20 20 7a 45 78 74  ame);.      zExt
1c960 72 61 20 2b 3d 20 28 73 74 72 6c 65 6e 28 7a 43  ra += (strlen(zC
1c961 6f 6c 6c 29 20 2b 20 31 29 3b 0a 20 20 20 20 7d  oll) + 1);.    }
1c962 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 43 6f 6c  else{.      zCol
1c963 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a  l = pTab->aCol[j
1c964 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 69  ].zColl;.      i
1c965 66 28 20 21 7a 43 6f 6c 6c 20 29 7b 0a 20 20 20  f( !zColl ){.   
1c966 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 64 62 2d       zColl = db-
1c967 3e 70 44 66 6c 74 43 6f 6c 6c 2d 3e 7a 4e 61 6d  >pDfltColl->zNam
1c968 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  e;.      }.    }
1c969 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e 69 6e  .    if( !db->in
1c96a 69 74 2e 62 75 73 79 20 26 26 20 21 73 71 6c 69  it.busy && !sqli
1c96b 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71  te3LocateCollSeq
1c96c 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 2c 20  (pParse, zColl, 
1c96d 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  -1) ){.      got
1c96e 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
1c96f 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  dex;.    }.    p
1c970 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d  Index->azColl[i]
1c971 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 20 20 20 72 65   = zColl;.    re
1c972 71 75 65 73 74 65 64 53 6f 72 74 4f 72 64 65 72  questedSortOrder
1c973 20 3d 20 70 4c 69 73 74 49 74 65 6d 2d 3e 73 6f   = pListItem->so
1c974 72 74 4f 72 64 65 72 20 26 20 73 6f 72 74 4f 72  rtOrder & sortOr
1c975 64 65 72 4d 61 73 6b 3b 0a 20 20 20 20 70 49 6e  derMask;.    pIn
1c976 64 65 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b  dex->aSortOrder[
1c977 69 5d 20 3d 20 72 65 71 75 65 73 74 65 64 53 6f  i] = requestedSo
1c978 72 74 4f 72 64 65 72 3b 0a 20 20 7d 0a 20 20 73  rtOrder;.  }.  s
1c979 71 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f 77  qlite3DefaultRow
1c97a 45 73 74 28 70 49 6e 64 65 78 29 3b 0a 0a 20 20  Est(pIndex);..  
1c97b 69 66 28 20 70 54 61 62 3d 3d 70 50 61 72 73 65  if( pTab==pParse
1c97c 2d 3e 70 4e 65 77 54 61 62 6c 65 20 29 7b 0a 20  ->pNewTable ){. 
1c97d 20 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69     /* This routi
1c97e 6e 65 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c  ne has been call
1c97f 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 6e 20  ed to create an 
1c980 61 75 74 6f 6d 61 74 69 63 20 69 6e 64 65 78 20  automatic index 
1c981 61 73 20 61 0a 20 20 20 20 2a 2a 20 72 65 73 75  as a.    ** resu
1c982 6c 74 20 6f 66 20 61 20 50 52 49 4d 41 52 59 20  lt of a PRIMARY 
1c983 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 20 63 6c  KEY or UNIQUE cl
1c984 61 75 73 65 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e  ause on a column
1c985 20 64 65 66 69 6e 69 74 69 6f 6e 2c 20 6f 72 0a   definition, or.
1c986 20 20 20 20 2a 2a 20 61 20 50 52 49 4d 41 52 59      ** a PRIMARY
1c987 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 20 63   KEY or UNIQUE c
1c988 6c 61 75 73 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  lause following 
1c989 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e  the column defin
1c98a 69 74 69 6f 6e 73 2e 0a 20 20 20 20 2a 2a 20 69  itions..    ** i
1c98b 2e 65 2e 20 6f 6e 65 20 6f 66 3a 0a 20 20 20 20  .e. one of:.    
1c98c 2a 2a 0a 20 20 20 20 2a 2a 20 43 52 45 41 54 45  **.    ** CREATE
1c98d 20 54 41 42 4c 45 20 74 28 78 20 50 52 49 4d 41   TABLE t(x PRIMA
1c98e 52 59 20 4b 45 59 2c 20 79 29 3b 0a 20 20 20 20  RY KEY, y);.    
1c98f 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  ** CREATE TABLE 
1c990 74 28 78 2c 20 79 2c 20 55 4e 49 51 55 45 28 78  t(x, y, UNIQUE(x
1c991 2c 20 79 29 29 3b 0a 20 20 20 20 2a 2a 0a 20 20  , y));.    **.  
1c992 20 20 2a 2a 20 45 69 74 68 65 72 20 77 61 79 2c    ** Either way,
1c993 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   check to see if
1c994 20 74 68 65 20 74 61 62 6c 65 20 61 6c 72 65 61   the table alrea
1c995 64 79 20 68 61 73 20 73 75 63 68 20 61 6e 20 69  dy has such an i
1c996 6e 64 65 78 2e 20 49 66 0a 20 20 20 20 2a 2a 20  ndex. If.    ** 
1c997 73 6f 2c 20 64 6f 6e 27 74 20 62 6f 74 68 65 72  so, don't bother
1c998 20 63 72 65 61 74 69 6e 67 20 74 68 69 73 20 6f   creating this o
1c999 6e 65 2e 20 54 68 69 73 20 6f 6e 6c 79 20 61 70  ne. This only ap
1c99a 70 6c 69 65 73 20 74 6f 0a 20 20 20 20 2a 2a 20  plies to.    ** 
1c99b 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72  automatically cr
1c99c 65 61 74 65 64 20 69 6e 64 69 63 65 73 2e 20 55  eated indices. U
1c99d 73 65 72 73 20 63 61 6e 20 64 6f 20 61 73 20 74  sers can do as t
1c99e 68 65 79 20 77 69 73 68 20 77 69 74 68 0a 20 20  hey wish with.  
1c99f 20 20 2a 2a 20 65 78 70 6c 69 63 69 74 20 69 6e    ** explicit in
1c9a0 64 69 63 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  dices..    */.  
1c9a1 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20    Index *pIdx;. 
1c9a2 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62     for(pIdx=pTab
1c9a3 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
1c9a4 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
1c9a5 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a  ){.      int k;.
1c9a6 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49        assert( pI
1c9a7 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f  dx->onError!=OE_
1c9a8 4e 6f 6e 65 20 29 3b 0a 20 20 20 20 20 20 61 73  None );.      as
1c9a9 73 65 72 74 28 20 70 49 64 78 2d 3e 61 75 74 6f  sert( pIdx->auto
1c9aa 49 6e 64 65 78 20 29 3b 0a 20 20 20 20 20 20 61  Index );.      a
1c9ab 73 73 65 72 74 28 20 70 49 6e 64 65 78 2d 3e 6f  ssert( pIndex->o
1c9ac 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20  nError!=OE_None 
1c9ad 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 70 49  );..      if( pI
1c9ae 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d 70 49 6e  dx->nColumn!=pIn
1c9af 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 20 63  dex->nColumn ) c
1c9b0 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 66  ontinue;.      f
1c9b1 6f 72 28 6b 3d 30 3b 20 6b 3c 70 49 64 78 2d 3e  or(k=0; k<pIdx->
1c9b2 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b 29 7b 0a 20  nColumn; k++){. 
1c9b3 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
1c9b4 72 20 2a 7a 31 20 3d 20 70 49 64 78 2d 3e 61 7a  r *z1 = pIdx->az
1c9b5 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20 20  Coll[k];.       
1c9b6 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 20   const char *z2 
1c9b7 3d 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c  = pIndex->azColl
1c9b8 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28  [k];.        if(
1c9b9 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b   pIdx->aiColumn[
1c9ba 6b 5d 21 3d 70 49 6e 64 65 78 2d 3e 61 69 43 6f  k]!=pIndex->aiCo
1c9bb 6c 75 6d 6e 5b 6b 5d 20 29 20 62 72 65 61 6b 3b  lumn[k] ) break;
1c9bc 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64  .        if( pId
1c9bd 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6b 5d  x->aSortOrder[k]
1c9be 21 3d 70 49 6e 64 65 78 2d 3e 61 53 6f 72 74 4f  !=pIndex->aSortO
1c9bf 72 64 65 72 5b 6b 5d 20 29 20 62 72 65 61 6b 3b  rder[k] ) break;
1c9c0 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 31 21  .        if( z1!
1c9c1 3d 7a 32 20 26 26 20 73 71 6c 69 74 65 33 53 74  =z2 && sqlite3St
1c9c2 72 49 43 6d 70 28 7a 31 2c 20 7a 32 29 20 29 20  rICmp(z1, z2) ) 
1c9c3 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
1c9c4 20 20 20 20 20 69 66 28 20 6b 3d 3d 70 49 64 78       if( k==pIdx
1c9c5 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ->nColumn ){.   
1c9c6 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 6f       if( pIdx->o
1c9c7 6e 45 72 72 6f 72 21 3d 70 49 6e 64 65 78 2d 3e  nError!=pIndex->
1c9c8 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 20  onError ){.     
1c9c9 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 6f 6e       /* This con
1c9ca 73 74 72 61 69 6e 74 20 63 72 65 61 74 65 73 20  straint creates 
1c9cb 74 68 65 20 73 61 6d 65 20 69 6e 64 65 78 20 61  the same index a
1c9cc 73 20 61 20 70 72 65 76 69 6f 75 73 0a 20 20 20  s a previous.   
1c9cd 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72         ** constr
1c9ce 61 69 6e 74 20 73 70 65 63 69 66 69 65 64 20 73  aint specified s
1c9cf 6f 6d 65 77 68 65 72 65 20 69 6e 20 74 68 65 20  omewhere in the 
1c9d0 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
1c9d1 74 65 6d 65 6e 74 2e 0a 20 20 20 20 20 20 20 20  tement..        
1c9d2 20 20 2a 2a 20 48 6f 77 65 76 65 72 20 74 68 65    ** However the
1c9d3 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61   ON CONFLICT cla
1c9d4 75 73 65 73 20 61 72 65 20 64 69 66 66 65 72 65  uses are differe
1c9d5 6e 74 2e 20 49 66 20 62 6f 74 68 20 74 68 69 73  nt. If both this
1c9d6 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63   .          ** c
1c9d7 6f 6e 73 74 72 61 69 6e 74 20 61 6e 64 20 74 68  onstraint and th
1c9d8 65 20 70 72 65 76 69 6f 75 73 20 65 71 75 69 76  e previous equiv
1c9d9 61 6c 65 6e 74 20 63 6f 6e 73 74 72 61 69 6e 74  alent constraint
1c9da 20 68 61 76 65 20 65 78 70 6c 69 63 69 74 0a 20   have explicit. 
1c9db 20 20 20 20 20 20 20 20 20 2a 2a 20 4f 4e 20 43           ** ON C
1c9dc 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73 65 73 20  ONFLICT clauses 
1c9dd 74 68 69 73 20 69 73 20 61 6e 20 65 72 72 6f 72  this is an error
1c9de 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 75 73 65  . Otherwise, use
1c9df 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a   the.          *
1c9e0 2a 20 65 78 70 6c 69 63 69 74 6c 79 20 73 70 65  * explicitly spe
1c9e1 63 69 66 69 65 64 20 62 65 68 61 76 69 6f 75 72  cified behaviour
1c9e2 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 2e 0a   for the index..
1c9e3 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
1c9e4 20 20 20 20 20 20 20 69 66 28 20 21 28 70 49 64         if( !(pId
1c9e5 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44  x->onError==OE_D
1c9e6 65 66 61 75 6c 74 20 7c 7c 20 70 49 6e 64 65 78  efault || pIndex
1c9e7 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65  ->onError==OE_De
1c9e8 66 61 75 6c 74 29 20 29 7b 0a 20 20 20 20 20 20  fault) ){.      
1c9e9 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1c9ea 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20  orMsg(pParse, . 
1c9eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
1c9ec 63 6f 6e 66 6c 69 63 74 69 6e 67 20 4f 4e 20 43  conflicting ON C
1c9ed 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73 65 73 20  ONFLICT clauses 
1c9ee 73 70 65 63 69 66 69 65 64 22 2c 20 30 29 3b 0a  specified", 0);.
1c9ef 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1c9f0 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e        if( pIdx->
1c9f1 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61  onError==OE_Defa
1c9f2 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ult ){.         
1c9f3 20 20 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72     pIdx->onError
1c9f4 20 3d 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72   = pIndex->onErr
1c9f5 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  or;.          }.
1c9f6 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1c9f7 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
1c9f8 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 20 20  te_index;.      
1c9f9 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  }.    }.  }..  /
1c9fa 2a 20 4c 69 6e 6b 20 74 68 65 20 6e 65 77 20 49  * Link the new I
1c9fb 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20 74  ndex structure t
1c9fc 6f 20 69 74 73 20 74 61 62 6c 65 20 61 6e 64 20  o its table and 
1c9fd 74 6f 20 74 68 65 20 6f 74 68 65 72 0a 20 20 2a  to the other.  *
1c9fe 2a 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  * in-memory data
1c9ff 62 61 73 65 20 73 74 72 75 63 74 75 72 65 73 2e  base structures.
1ca00 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d   .  */.  if( db-
1ca01 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20  >init.busy ){.  
1ca02 20 20 49 6e 64 65 78 20 2a 70 3b 0a 20 20 20 20    Index *p;.    
1ca03 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49  p = sqlite3HashI
1ca04 6e 73 65 72 74 28 26 70 49 6e 64 65 78 2d 3e 70  nsert(&pIndex->p
1ca05 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 2c  Schema->idxHash,
1ca06 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
1ca07 20 20 20 20 20 20 20 20 20 20 20 70 49 6e 64 65             pInde
1ca08 78 2d 3e 7a 4e 61 6d 65 2c 20 73 74 72 6c 65 6e  x->zName, strlen
1ca09 28 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 29 2b  (pIndex->zName)+
1ca0a 31 2c 20 70 49 6e 64 65 78 29 3b 0a 20 20 20 20  1, pIndex);.    
1ca0b 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 61  if( p ){.      a
1ca0c 73 73 65 72 74 28 20 70 3d 3d 70 49 6e 64 65 78  ssert( p==pIndex
1ca0d 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d   );  /* Malloc m
1ca0e 75 73 74 20 68 61 76 65 20 66 61 69 6c 65 64 20  ust have failed 
1ca0f 2a 2f 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c  */.      db->mal
1ca10 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
1ca11 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63       goto exit_c
1ca12 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20  reate_index;.   
1ca13 20 7d 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73   }.    db->flags
1ca14 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72   |= SQLITE_Inter
1ca15 6e 43 68 61 6e 67 65 73 3b 0a 20 20 20 20 69 66  nChanges;.    if
1ca16 28 20 70 54 62 6c 4e 61 6d 65 21 3d 30 20 29 7b  ( pTblName!=0 ){
1ca17 0a 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 74  .      pIndex->t
1ca18 6e 75 6d 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 6e  num = db->init.n
1ca19 65 77 54 6e 75 6d 3b 0a 20 20 20 20 7d 0a 20 20  ewTnum;.    }.  
1ca1a 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64  }..  /* If the d
1ca1b 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20  b->init.busy is 
1ca1c 30 20 74 68 65 6e 20 63 72 65 61 74 65 20 74 68  0 then create th
1ca1d 65 20 69 6e 64 65 78 20 6f 6e 20 64 69 73 6b 2e  e index on disk.
1ca1e 20 20 54 68 69 73 0a 20 20 2a 2a 20 69 6e 76 6f    This.  ** invo
1ca1f 6c 76 65 73 20 77 72 69 74 69 6e 67 20 74 68 65  lves writing the
1ca20 20 69 6e 64 65 78 20 69 6e 74 6f 20 74 68 65 20   index into the 
1ca21 6d 61 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64  master table and
1ca22 20 66 69 6c 6c 69 6e 67 20 69 6e 20 74 68 65 0a   filling in the.
1ca23 20 20 2a 2a 20 69 6e 64 65 78 20 77 69 74 68 20    ** index with 
1ca24 74 68 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c  the current tabl
1ca25 65 20 63 6f 6e 74 65 6e 74 73 2e 0a 20 20 2a 2a  e contents..  **
1ca26 0a 20 20 2a 2a 20 54 68 65 20 64 62 2d 3e 69 6e  .  ** The db->in
1ca27 69 74 2e 62 75 73 79 20 69 73 20 30 20 77 68 65  it.busy is 0 whe
1ca28 6e 20 74 68 65 20 75 73 65 72 20 66 69 72 73 74  n the user first
1ca29 20 65 6e 74 65 72 73 20 61 20 43 52 45 41 54 45   enters a CREATE
1ca2a 20 49 4e 44 45 58 20 0a 20 20 2a 2a 20 63 6f 6d   INDEX .  ** com
1ca2b 6d 61 6e 64 2e 20 20 64 62 2d 3e 69 6e 69 74 2e  mand.  db->init.
1ca2c 62 75 73 79 20 69 73 20 31 20 77 68 65 6e 20 61  busy is 1 when a
1ca2d 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65   database is ope
1ca2e 6e 65 64 20 61 6e 64 20 0a 20 20 2a 2a 20 43 52  ned and .  ** CR
1ca2f 45 41 54 45 20 49 4e 44 45 58 20 73 74 61 74 65  EATE INDEX state
1ca30 6d 65 6e 74 73 20 61 72 65 20 72 65 61 64 20 6f  ments are read o
1ca31 75 74 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72  ut of the master
1ca32 20 74 61 62 6c 65 2e 20 20 49 6e 0a 20 20 2a 2a   table.  In.  **
1ca33 20 74 68 65 20 6c 61 74 74 65 72 20 63 61 73 65   the latter case
1ca34 20 74 68 65 20 69 6e 64 65 78 20 61 6c 72 65 61   the index alrea
1ca35 64 79 20 65 78 69 73 74 73 20 6f 6e 20 64 69 73  dy exists on dis
1ca36 6b 2c 20 77 68 69 63 68 20 69 73 20 77 68 79 0a  k, which is why.
1ca37 20 20 2a 2a 20 77 65 20 64 6f 6e 27 74 20 77 61    ** we don't wa
1ca38 6e 74 20 74 6f 20 72 65 63 72 65 61 74 65 20 69  nt to recreate i
1ca39 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20  t..  **.  ** If 
1ca3a 70 54 62 6c 4e 61 6d 65 3d 3d 30 20 69 74 20 6d  pTblName==0 it m
1ca3b 65 61 6e 73 20 74 68 69 73 20 69 6e 64 65 78 20  eans this index 
1ca3c 69 73 20 67 65 6e 65 72 61 74 65 64 20 61 73 20  is generated as 
1ca3d 61 20 70 72 69 6d 61 72 79 20 6b 65 79 0a 20 20  a primary key.  
1ca3e 2a 2a 20 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e  ** or UNIQUE con
1ca3f 73 74 72 61 69 6e 74 20 6f 66 20 61 20 43 52 45  straint of a CRE
1ca40 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
1ca41 65 6e 74 2e 20 20 53 69 6e 63 65 20 74 68 65 20  ent.  Since the 
1ca42 74 61 62 6c 65 0a 20 20 2a 2a 20 68 61 73 20 6a  table.  ** has j
1ca43 75 73 74 20 62 65 65 6e 20 63 72 65 61 74 65 64  ust been created
1ca44 2c 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f  , it contains no
1ca45 20 64 61 74 61 20 61 6e 64 20 74 68 65 20 69 6e   data and the in
1ca46 64 65 78 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  dex initializati
1ca47 6f 6e 0a 20 20 2a 2a 20 73 74 65 70 20 63 61 6e  on.  ** step can
1ca48 20 62 65 20 73 6b 69 70 70 65 64 2e 0a 20 20 2a   be skipped..  *
1ca49 2f 0a 20 20 65 6c 73 65 20 69 66 28 20 64 62 2d  /.  else if( db-
1ca4a 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29 7b  >init.busy==0 ){
1ca4b 0a 20 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  .    Vdbe *v;.  
1ca4c 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 0a 20    char *zStmt;. 
1ca4d 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b     int iMem = ++
1ca4e 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 0a 20  pParse->nMem;.. 
1ca4f 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65     v = sqlite3Ge
1ca50 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
1ca51 20 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67 6f     if( v==0 ) go
1ca52 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
1ca53 6e 64 65 78 3b 0a 0a 0a 20 20 20 20 2f 2a 20 43  ndex;...    /* C
1ca54 72 65 61 74 65 20 74 68 65 20 72 6f 6f 74 70 61  reate the rootpa
1ca55 67 65 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78  ge for the index
1ca56 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
1ca57 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
1ca58 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 31  ration(pParse, 1
1ca59 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69  , iDb);.    sqli
1ca5a 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1ca5b 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78 2c   OP_CreateIndex,
1ca5c 20 69 44 62 2c 20 69 4d 65 6d 29 3b 0a 0a 20 20   iDb, iMem);..  
1ca5d 20 20 2f 2a 20 47 61 74 68 65 72 20 74 68 65 20    /* Gather the 
1ca5e 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66  complete text of
1ca5f 20 74 68 65 20 43 52 45 41 54 45 20 49 4e 44 45   the CREATE INDE
1ca60 58 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 74 6f  X statement into
1ca61 0a 20 20 20 20 2a 2a 20 74 68 65 20 7a 53 74 6d  .    ** the zStm
1ca62 74 20 76 61 72 69 61 62 6c 65 0a 20 20 20 20 2a  t variable.    *
1ca63 2f 0a 20 20 20 20 69 66 28 20 70 53 74 61 72 74  /.    if( pStart
1ca64 20 26 26 20 70 45 6e 64 20 29 7b 0a 20 20 20 20   && pEnd ){.    
1ca65 20 20 2f 2a 20 41 20 6e 61 6d 65 64 20 69 6e 64    /* A named ind
1ca66 65 78 20 77 69 74 68 20 61 6e 20 65 78 70 6c 69  ex with an expli
1ca67 63 69 74 20 43 52 45 41 54 45 20 49 4e 44 45 58  cit CREATE INDEX
1ca68 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
1ca69 20 20 20 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69      zStmt = sqli
1ca6a 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
1ca6b 43 52 45 41 54 45 25 73 20 49 4e 44 45 58 20 25  CREATE%s INDEX %
1ca6c 2e 2a 73 22 2c 0a 20 20 20 20 20 20 20 20 6f 6e  .*s",.        on
1ca6d 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65 20 3f  Error==OE_None ?
1ca6e 20 22 22 20 3a 20 22 20 55 4e 49 51 55 45 22 2c   "" : " UNIQUE",
1ca6f 0a 20 20 20 20 20 20 20 20 70 45 6e 64 2d 3e 7a  .        pEnd->z
1ca70 20 2d 20 70 4e 61 6d 65 2d 3e 7a 20 2b 20 31 2c   - pName->z + 1,
1ca71 0a 20 20 20 20 20 20 20 20 70 4e 61 6d 65 2d 3e  .        pName->
1ca72 7a 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  z);.    }else{. 
1ca73 20 20 20 20 20 2f 2a 20 41 6e 20 61 75 74 6f 6d       /* An autom
1ca74 61 74 69 63 20 69 6e 64 65 78 20 63 72 65 61 74  atic index creat
1ca75 65 64 20 62 79 20 61 20 50 52 49 4d 41 52 59 20  ed by a PRIMARY 
1ca76 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 20 63 6f  KEY or UNIQUE co
1ca77 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20  nstraint */.    
1ca78 20 20 2f 2a 20 7a 53 74 6d 74 20 3d 20 73 71 6c    /* zStmt = sql
1ca79 69 74 65 33 4d 50 72 69 6e 74 66 28 22 22 29 3b  ite3MPrintf("");
1ca7a 20 2a 2f 0a 20 20 20 20 20 20 7a 53 74 6d 74 20   */.      zStmt 
1ca7b 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  = 0;.    }..    
1ca7c 2f 2a 20 41 64 64 20 61 6e 20 65 6e 74 72 79 20  /* Add an entry 
1ca7d 69 6e 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  in sqlite_master
1ca7e 20 66 6f 72 20 74 68 69 73 20 69 6e 64 65 78 0a   for this index.
1ca7f 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
1ca80 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50  e3NestedParse(pP
1ca81 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22  arse, .        "
1ca82 49 4e 53 45 52 54 20 49 4e 54 4f 20 25 51 2e 25  INSERT INTO %Q.%
1ca83 73 20 56 41 4c 55 45 53 28 27 69 6e 64 65 78 27  s VALUES('index'
1ca84 2c 25 51 2c 25 51 2c 23 25 64 2c 25 51 29 3b 22  ,%Q,%Q,#%d,%Q);"
1ca85 2c 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 44  ,.        db->aD
1ca86 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43  b[iDb].zName, SC
1ca87 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c  HEMA_TABLE(iDb),
1ca88 0a 20 20 20 20 20 20 20 20 70 49 6e 64 65 78 2d  .        pIndex-
1ca89 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  >zName,.        
1ca8a 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20  pTab->zName,.   
1ca8b 20 20 20 20 20 69 4d 65 6d 2c 0a 20 20 20 20 20       iMem,.     
1ca8c 20 20 20 7a 53 74 6d 74 0a 20 20 20 20 29 3b 0a     zStmt.    );.
1ca8d 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
1ca8e 28 7a 53 74 6d 74 29 3b 0a 0a 20 20 20 20 2f 2a  (zStmt);..    /*
1ca8f 20 46 69 6c 6c 20 74 68 65 20 69 6e 64 65 78 20   Fill the index 
1ca90 77 69 74 68 20 64 61 74 61 20 61 6e 64 20 72 65  with data and re
1ca91 70 61 72 73 65 20 74 68 65 20 73 63 68 65 6d 61  parse the schema
1ca92 2e 20 43 6f 64 65 20 61 6e 20 4f 50 5f 45 78 70  . Code an OP_Exp
1ca93 69 72 65 0a 20 20 20 20 2a 2a 20 74 6f 20 69 6e  ire.    ** to in
1ca94 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 70 72 65  validate all pre
1ca95 2d 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d  -compiled statem
1ca96 65 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ents..    */.   
1ca97 20 69 66 28 20 70 54 62 6c 4e 61 6d 65 20 29 7b   if( pTblName ){
1ca98 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
1ca99 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72 73 65  fillIndex(pParse
1ca9a 2c 20 70 49 6e 64 65 78 2c 20 69 4d 65 6d 29 3b  , pIndex, iMem);
1ca9b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 68  .      sqlite3Ch
1ca9c 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73  angeCookie(pPars
1ca9d 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73  e, iDb);.      s
1ca9e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
1ca9f 28 76 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 65  (v, OP_ParseSche
1caa0 6d 61 2c 20 69 44 62 2c 20 30 2c 20 30 2c 0a 20  ma, iDb, 0, 0,. 
1caa1 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d          sqlite3M
1caa2 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 61 6d 65  Printf(db, "name
1caa3 3d 27 25 71 27 22 2c 20 70 49 6e 64 65 78 2d 3e  ='%q'", pIndex->
1caa4 7a 4e 61 6d 65 29 2c 20 50 34 5f 44 59 4e 41 4d  zName), P4_DYNAM
1caa5 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
1caa6 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
1caa7 4f 50 5f 45 78 70 69 72 65 2c 20 30 29 3b 0a 20  OP_Expire, 0);. 
1caa8 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57     }.  }..  /* W
1caa9 68 65 6e 20 61 64 64 69 6e 67 20 61 6e 20 69 6e  hen adding an in
1caaa 64 65 78 20 74 6f 20 74 68 65 20 6c 69 73 74 20  dex to the list 
1caab 6f 66 20 69 6e 64 69 63 65 73 20 66 6f 72 20 61  of indices for a
1caac 20 74 61 62 6c 65 2c 20 6d 61 6b 65 0a 20 20 2a   table, make.  *
1caad 2a 20 73 75 72 65 20 61 6c 6c 20 69 6e 64 69 63  * sure all indic
1caae 65 73 20 6c 61 62 65 6c 65 64 20 4f 45 5f 52 65  es labeled OE_Re
1caaf 70 6c 61 63 65 20 63 6f 6d 65 20 61 66 74 65 72  place come after
1cab0 20 61 6c 6c 20 74 68 6f 73 65 20 6c 61 62 65 6c   all those label
1cab1 65 64 0a 20 20 2a 2a 20 4f 45 5f 49 67 6e 6f 72  ed.  ** OE_Ignor
1cab2 65 2e 20 20 54 68 69 73 20 69 73 20 6e 65 63 65  e.  This is nece
1cab3 73 73 61 72 79 20 66 6f 72 20 74 68 65 20 63 6f  ssary for the co
1cab4 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20  rrect operation 
1cab5 6f 66 20 55 50 44 41 54 45 0a 20 20 2a 2a 20 61  of UPDATE.  ** a
1cab6 6e 64 20 49 4e 53 45 52 54 2e 0a 20 20 2a 2f 0a  nd INSERT..  */.
1cab7 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62    if( db->init.b
1cab8 75 73 79 20 7c 7c 20 70 54 62 6c 4e 61 6d 65 3d  usy || pTblName=
1cab9 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6f 6e  =0 ){.    if( on
1caba 45 72 72 6f 72 21 3d 4f 45 5f 52 65 70 6c 61 63  Error!=OE_Replac
1cabb 65 20 7c 7c 20 70 54 61 62 2d 3e 70 49 6e 64 65  e || pTab->pInde
1cabc 78 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 7c 7c  x==0.         ||
1cabd 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 2d 3e 6f   pTab->pIndex->o
1cabe 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61  nError==OE_Repla
1cabf 63 65 29 7b 0a 20 20 20 20 20 20 70 49 6e 64 65  ce){.      pInde
1cac0 78 2d 3e 70 4e 65 78 74 20 3d 20 70 54 61 62 2d  x->pNext = pTab-
1cac1 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 20 20 70  >pIndex;.      p
1cac2 54 61 62 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49  Tab->pIndex = pI
1cac3 6e 64 65 78 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ndex;.    }else{
1cac4 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 4f  .      Index *pO
1cac5 74 68 65 72 20 3d 20 70 54 61 62 2d 3e 70 49 6e  ther = pTab->pIn
1cac6 64 65 78 3b 0a 20 20 20 20 20 20 77 68 69 6c 65  dex;.      while
1cac7 28 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 20  ( pOther->pNext 
1cac8 26 26 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74  && pOther->pNext
1cac9 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 52 65  ->onError!=OE_Re
1caca 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20  place ){.       
1cacb 20 70 4f 74 68 65 72 20 3d 20 70 4f 74 68 65 72   pOther = pOther
1cacc 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d  ->pNext;.      }
1cacd 0a 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 70  .      pIndex->p
1cace 4e 65 78 74 20 3d 20 70 4f 74 68 65 72 2d 3e 70  Next = pOther->p
1cacf 4e 65 78 74 3b 0a 20 20 20 20 20 20 70 4f 74 68  Next;.      pOth
1cad0 65 72 2d 3e 70 4e 65 78 74 20 3d 20 70 49 6e 64  er->pNext = pInd
1cad1 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49  ex;.    }.    pI
1cad2 6e 64 65 78 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  ndex = 0;.  }.. 
1cad3 20 2f 2a 20 43 6c 65 61 6e 20 75 70 20 62 65 66   /* Clean up bef
1cad4 6f 72 65 20 65 78 69 74 69 6e 67 20 2a 2f 0a 65  ore exiting */.e
1cad5 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
1cad6 3a 0a 20 20 69 66 28 20 70 49 6e 64 65 78 20 29  :.  if( pIndex )
1cad7 7b 0a 20 20 20 20 66 72 65 65 49 6e 64 65 78 28  {.    freeIndex(
1cad8 70 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 20 20 73  pIndex);.  }.  s
1cad9 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
1cada 6c 65 74 65 28 70 4c 69 73 74 29 3b 0a 20 20 73  lete(pList);.  s
1cadb 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c  qlite3SrcListDel
1cadc 65 74 65 28 70 54 62 6c 4e 61 6d 65 29 3b 0a 20  ete(pTblName);. 
1cadd 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4e   sqlite3_free(zN
1cade 61 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a  ame);.  return;.
1cadf 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
1cae0 65 20 63 6f 64 65 20 74 6f 20 6d 61 6b 65 20 73  e code to make s
1cae1 75 72 65 20 74 68 65 20 66 69 6c 65 20 66 6f 72  ure the file for
1cae2 6d 61 74 20 6e 75 6d 62 65 72 20 69 73 20 61 74  mat number is at
1cae3 20 6c 65 61 73 74 20 6d 69 6e 46 6f 72 6d 61 74   least minFormat
1cae4 2e 0a 2a 2a 20 54 68 65 20 67 65 6e 65 72 61 74  ..** The generat
1cae5 65 64 20 63 6f 64 65 20 77 69 6c 6c 20 69 6e 63  ed code will inc
1cae6 72 65 61 73 65 20 74 68 65 20 66 69 6c 65 20 66  rease the file f
1cae7 6f 72 6d 61 74 20 6e 75 6d 62 65 72 20 69 66 20  ormat number if 
1cae8 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 53 51  necessary..*/.SQ
1cae9 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1caea 64 20 73 71 6c 69 74 65 33 4d 69 6e 69 6d 75 6d  d sqlite3Minimum
1caeb 46 69 6c 65 46 6f 72 6d 61 74 28 50 61 72 73 65  FileFormat(Parse
1caec 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44   *pParse, int iD
1caed 62 2c 20 69 6e 74 20 6d 69 6e 46 6f 72 6d 61 74  b, int minFormat
1caee 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  ){.  Vdbe *v;.  
1caef 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
1caf0 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
1caf1 28 20 76 20 29 7b 0a 20 20 20 20 69 6e 74 20 72  ( v ){.    int r
1caf2 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  1 = sqlite3GetTe
1caf3 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
1caf4 20 20 20 69 6e 74 20 72 32 20 3d 20 73 71 6c 69     int r2 = sqli
1caf5 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
1caf6 61 72 73 65 29 3b 0a 20 20 20 20 69 6e 74 20 6a  arse);.    int j
1caf7 31 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  1;.    sqlite3Vd
1caf8 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 52  beAddOp3(v, OP_R
1caf9 65 61 64 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20  eadCookie, iDb, 
1cafa 72 31 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  r1, 1);.    sqli
1cafb 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65  te3VdbeUsesBtree
1cafc 28 76 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71  (v, iDb);.    sq
1cafd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1cafe 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 6d  v, OP_Integer, m
1caff 69 6e 46 6f 72 6d 61 74 2c 20 72 32 29 3b 0a 20  inFormat, r2);. 
1cb00 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56     j1 = sqlite3V
1cb01 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
1cb02 47 65 2c 20 72 32 2c 20 30 2c 20 72 31 29 3b 0a  Ge, r2, 0, r1);.
1cb03 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1cb04 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43  ddOp3(v, OP_SetC
1cb05 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 31 2c 20 72  ookie, iDb, 1, r
1cb06 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  2);.    sqlite3V
1cb07 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
1cb08 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52  1);.    sqlite3R
1cb09 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
1cb0a 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 73  arse, r1);.    s
1cb0b 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
1cb0c 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 32 29  pReg(pParse, r2)
1cb0d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  ;.  }.}../*.** F
1cb0e 69 6c 6c 20 74 68 65 20 49 6e 64 65 78 2e 61 69  ill the Index.ai
1cb0f 52 6f 77 45 73 74 5b 5d 20 61 72 72 61 79 20 77  RowEst[] array w
1cb10 69 74 68 20 64 65 66 61 75 6c 74 20 69 6e 66 6f  ith default info
1cb11 72 6d 61 74 69 6f 6e 20 2d 20 69 6e 66 6f 72 6d  rmation - inform
1cb12 61 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 75  ation.** to be u
1cb13 73 65 64 20 77 68 65 6e 20 77 65 20 68 61 76 65  sed when we have
1cb14 20 6e 6f 74 20 72 75 6e 20 74 68 65 20 41 4e 41   not run the ANA
1cb15 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a  LYZE command..**
1cb16 0a 2a 2a 20 61 69 52 6f 77 45 73 74 5b 30 5d 20  .** aiRowEst[0] 
1cb17 69 73 20 73 75 70 70 6f 73 65 20 74 6f 20 63 6f  is suppose to co
1cb18 6e 74 61 69 6e 20 74 68 65 20 6e 75 6d 62 65 72  ntain the number
1cb19 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20   of elements in 
1cb1a 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 53 69  the index..** Si
1cb1b 6e 63 65 20 77 65 20 64 6f 20 6e 6f 74 20 6b 6e  nce we do not kn
1cb1c 6f 77 2c 20 67 75 65 73 73 20 31 20 6d 69 6c 6c  ow, guess 1 mill
1cb1d 69 6f 6e 2e 20 20 61 69 52 6f 77 45 73 74 5b 31  ion.  aiRowEst[1
1cb1e 5d 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65  ] is an estimate
1cb1f 20 6f 66 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65   of the.** numbe
1cb20 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65  r of rows in the
1cb21 20 74 61 62 6c 65 20 74 68 61 74 20 6d 61 74 63   table that matc
1cb22 68 20 61 6e 79 20 70 61 72 74 69 63 75 6c 61 72  h any particular
1cb23 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a   value of the.**
1cb24 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20 6f 66   first column of
1cb25 20 74 68 65 20 69 6e 64 65 78 2e 20 20 61 69 52   the index.  aiR
1cb26 6f 77 45 73 74 5b 32 5d 20 69 73 20 61 6e 20 65  owEst[2] is an e
1cb27 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20 6e  stimate of the n
1cb28 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 72 6f 77 73  umber.** of rows
1cb29 20 74 68 61 74 20 6d 61 74 63 68 20 61 6e 79 20   that match any 
1cb2a 70 61 72 74 69 63 75 6c 61 72 20 63 6f 6d 62 69  particular combi
1cb2b 6e 69 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 66  niation of the f
1cb2c 69 72 73 74 20 32 20 63 6f 6c 75 6d 6e 73 0a 2a  irst 2 columns.*
1cb2d 2a 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e 20  * of the index. 
1cb2e 20 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20   And so forth.  
1cb2f 49 74 20 6d 75 73 74 20 61 6c 77 61 79 73 20 62  It must always b
1cb30 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 0a  e the case that.
1cb31 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61  *.**           a
1cb32 69 52 6f 77 45 73 74 5b 4e 5d 3c 3d 61 69 52 6f  iRowEst[N]<=aiRo
1cb33 77 45 73 74 5b 4e 2d 31 5d 0a 2a 2a 20 20 20 20  wEst[N-1].**    
1cb34 20 20 20 20 20 20 20 61 69 52 6f 77 45 73 74 5b         aiRowEst[
1cb35 4e 5d 3e 3d 31 0a 2a 2a 0a 2a 2a 20 41 70 61 72  N]>=1.**.** Apar
1cb36 74 20 66 72 6f 6d 20 74 68 61 74 2c 20 77 65 20  t from that, we 
1cb37 68 61 76 65 20 6c 69 74 74 6c 65 20 74 6f 20 67  have little to g
1cb38 6f 20 6f 6e 20 62 65 73 69 64 65 73 20 69 6e 74  o on besides int
1cb39 75 69 74 69 6f 6e 20 61 73 20 74 6f 0a 2a 2a 20  uition as to.** 
1cb3a 68 6f 77 20 61 69 52 6f 77 45 73 74 5b 5d 20 73  how aiRowEst[] s
1cb3b 68 6f 75 6c 64 20 62 65 20 69 6e 69 74 69 61 6c  hould be initial
1cb3c 69 7a 65 64 2e 20 20 54 68 65 20 6e 75 6d 62 65  ized.  The numbe
1cb3d 72 73 20 67 65 6e 65 72 61 74 65 64 20 68 65 72  rs generated her
1cb3e 65 0a 2a 2a 20 61 72 65 20 62 61 73 65 64 20 6f  e.** are based o
1cb3f 6e 20 74 79 70 69 63 61 6c 20 76 61 6c 75 65 73  n typical values
1cb40 20 66 6f 75 6e 64 20 69 6e 20 61 63 74 75 61 6c   found in actual
1cb41 20 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a 53 51 4c   indices..*/.SQL
1cb42 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1cb43 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 52   sqlite3DefaultR
1cb44 6f 77 45 73 74 28 49 6e 64 65 78 20 2a 70 49 64  owEst(Index *pId
1cb45 78 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 2a  x){.  unsigned *
1cb46 61 20 3d 20 70 49 64 78 2d 3e 61 69 52 6f 77 45  a = pIdx->aiRowE
1cb47 73 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61  st;.  int i;.  a
1cb48 73 73 65 72 74 28 20 61 21 3d 30 20 29 3b 0a 20  ssert( a!=0 );. 
1cb49 20 61 5b 30 5d 20 3d 20 31 30 30 30 30 30 30 3b   a[0] = 1000000;
1cb4a 0a 20 20 66 6f 72 28 69 3d 70 49 64 78 2d 3e 6e  .  for(i=pIdx->n
1cb4b 43 6f 6c 75 6d 6e 3b 20 69 3e 3d 35 3b 20 69 2d  Column; i>=5; i-
1cb4c 2d 29 7b 0a 20 20 20 20 61 5b 69 5d 20 3d 20 35  -){.    a[i] = 5
1cb4d 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 69  ;.  }.  while( i
1cb4e 3e 3d 31 20 29 7b 0a 20 20 20 20 61 5b 69 5d 20  >=1 ){.    a[i] 
1cb4f 3d 20 31 31 20 2d 20 69 3b 0a 20 20 20 20 69 2d  = 11 - i;.    i-
1cb50 2d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 64  -;.  }.  if( pId
1cb51 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e  x->onError!=OE_N
1cb52 6f 6e 65 20 29 7b 0a 20 20 20 20 61 5b 70 49 64  one ){.    a[pId
1cb53 78 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20 31 3b  x->nColumn] = 1;
1cb54 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
1cb55 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
1cb56 64 72 6f 70 20 61 6e 20 65 78 69 73 74 69 6e 67  drop an existing
1cb57 20 6e 61 6d 65 64 20 69 6e 64 65 78 2e 20 20 54   named index.  T
1cb58 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69  his routine.** i
1cb59 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 44 52  mplements the DR
1cb5a 4f 50 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65  OP INDEX stateme
1cb5b 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nt..*/.SQLITE_PR
1cb5c 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1cb5d 65 33 44 72 6f 70 49 6e 64 65 78 28 50 61 72 73  e3DropIndex(Pars
1cb5e 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69  e *pParse, SrcLi
1cb5f 73 74 20 2a 70 4e 61 6d 65 2c 20 69 6e 74 20 69  st *pName, int i
1cb60 66 45 78 69 73 74 73 29 7b 0a 20 20 49 6e 64 65  fExists){.  Inde
1cb61 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 56 64 62  x *pIndex;.  Vdb
1cb62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 20  e *v;.  sqlite3 
1cb63 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
1cb64 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20 20  ;.  int iDb;..  
1cb65 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
1cb66 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
1cb67 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f  iled ){.    goto
1cb68 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78   exit_drop_index
1cb69 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
1cb6a 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29  pName->nSrc==1 )
1cb6b 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ;.  if( SQLITE_O
1cb6c 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63  K!=sqlite3ReadSc
1cb6d 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a  hema(pParse) ){.
1cb6e 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72      goto exit_dr
1cb6f 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20  op_index;.  }.  
1cb70 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33  pIndex = sqlite3
1cb71 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 70 4e  FindIndex(db, pN
1cb72 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c  ame->a[0].zName,
1cb73 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61   pName->a[0].zDa
1cb74 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70  tabase);.  if( p
1cb75 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20  Index==0 ){.    
1cb76 69 66 28 20 21 69 66 45 78 69 73 74 73 20 29 7b  if( !ifExists ){
1cb77 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
1cb78 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1cb79 6e 6f 20 73 75 63 68 20 69 6e 64 65 78 3a 20 25  no such index: %
1cb7a 53 22 2c 20 70 4e 61 6d 65 2c 20 30 29 3b 0a 20  S", pName, 0);. 
1cb7b 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d     }.    pParse-
1cb7c 3e 63 68 65 63 6b 53 63 68 65 6d 61 20 3d 20 31  >checkSchema = 1
1cb7d 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  ;.    goto exit_
1cb7e 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a  drop_index;.  }.
1cb7f 20 20 69 66 28 20 70 49 6e 64 65 78 2d 3e 61 75    if( pIndex->au
1cb80 74 6f 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 73  toIndex ){.    s
1cb81 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1cb82 50 61 72 73 65 2c 20 22 69 6e 64 65 78 20 61 73  Parse, "index as
1cb83 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 55 4e  sociated with UN
1cb84 49 51 55 45 20 22 0a 20 20 20 20 20 20 22 6f 72  IQUE ".      "or
1cb85 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6e   PRIMARY KEY con
1cb86 73 74 72 61 69 6e 74 20 63 61 6e 6e 6f 74 20 62  straint cannot b
1cb87 65 20 64 72 6f 70 70 65 64 22 2c 20 30 29 3b 0a  e dropped", 0);.
1cb88 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72      goto exit_dr
1cb89 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20  op_index;.  }.  
1cb8a 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
1cb8b 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70  emaToIndex(db, p
1cb8c 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 29 3b  Index->pSchema);
1cb8d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1cb8e 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
1cb8f 4f 4e 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 63  ON.  {.    int c
1cb90 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f  ode = SQLITE_DRO
1cb91 50 5f 49 4e 44 45 58 3b 0a 20 20 20 20 54 61 62  P_INDEX;.    Tab
1cb92 6c 65 20 2a 70 54 61 62 20 3d 20 70 49 6e 64 65  le *pTab = pInde
1cb93 78 2d 3e 70 54 61 62 6c 65 3b 0a 20 20 20 20 63  x->pTable;.    c
1cb94 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d  onst char *zDb =
1cb95 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
1cb96 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ame;.    const c
1cb97 68 61 72 20 2a 7a 54 61 62 20 3d 20 53 43 48 45  har *zTab = SCHE
1cb98 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 20  MA_TABLE(iDb);. 
1cb99 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75     if( sqlite3Au
1cb9a 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
1cb9b 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a  SQLITE_DELETE, z
1cb9c 54 61 62 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a  Tab, 0, zDb) ){.
1cb9d 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f        goto exit_
1cb9e 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 20 20  drop_index;.    
1cb9f 7d 0a 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f  }.    if( !OMIT_
1cba0 54 45 4d 50 44 42 20 26 26 20 69 44 62 20 29 20  TEMPDB && iDb ) 
1cba1 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52  code = SQLITE_DR
1cba2 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 3b 0a 20  OP_TEMP_INDEX;. 
1cba3 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75     if( sqlite3Au
1cba4 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
1cba5 63 6f 64 65 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e  code, pIndex->zN
1cba6 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ame, pTab->zName
1cba7 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20  , zDb) ){.      
1cba8 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69  goto exit_drop_i
1cba9 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ndex;.    }.  }.
1cbaa 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47 65 6e  #endif..  /* Gen
1cbab 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 72 65  erate code to re
1cbac 6d 6f 76 65 20 74 68 65 20 69 6e 64 65 78 20 61  move the index a
1cbad 6e 64 20 66 72 6f 6d 20 74 68 65 20 6d 61 73 74  nd from the mast
1cbae 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76 20  er table */.  v 
1cbaf 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
1cbb0 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
1cbb1 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  v ){.    sqlite3
1cbb2 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74  BeginWriteOperat
1cbb3 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69  ion(pParse, 1, i
1cbb4 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Db);.    sqlite3
1cbb5 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72  NestedParse(pPar
1cbb6 73 65 2c 0a 20 20 20 20 20 20 20 22 44 45 4c 45  se,.       "DELE
1cbb7 54 45 20 46 52 4f 4d 20 25 51 2e 25 73 20 57 48  TE FROM %Q.%s WH
1cbb8 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c 0a 20 20  ERE name=%Q",.  
1cbb9 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62       db->aDb[iDb
1cbba 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f  ].zName, SCHEMA_
1cbbb 54 41 42 4c 45 28 69 44 62 29 2c 0a 20 20 20 20  TABLE(iDb),.    
1cbbc 20 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65     pIndex->zName
1cbbd 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20  .    );.    if( 
1cbbe 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65  sqlite3FindTable
1cbbf 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 74 61  (db, "sqlite_sta
1cbc0 74 31 22 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62  t1", db->aDb[iDb
1cbc1 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20  ].zName) ){.    
1cbc2 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50    sqlite3NestedP
1cbc3 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20  arse(pParse,.   
1cbc4 20 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f       "DELETE FRO
1cbc5 4d 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74  M %Q.sqlite_stat
1cbc6 31 20 57 48 45 52 45 20 69 64 78 3d 25 51 22 2c  1 WHERE idx=%Q",
1cbc7 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62  .        db->aDb
1cbc8 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 70 49 6e  [iDb].zName, pIn
1cbc9 64 65 78 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 20  dex->zName.     
1cbca 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71   );.    }.    sq
1cbcb 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69  lite3ChangeCooki
1cbcc 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a  e(pParse, iDb);.
1cbcd 20 20 20 20 64 65 73 74 72 6f 79 52 6f 6f 74 50      destroyRootP
1cbce 61 67 65 28 70 50 61 72 73 65 2c 20 70 49 6e 64  age(pParse, pInd
1cbcf 65 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a  ex->tnum, iDb);.
1cbd0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1cbd1 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70  ddOp4(v, OP_Drop
1cbd2 49 6e 64 65 78 2c 20 69 44 62 2c 20 30 2c 20 30  Index, iDb, 0, 0
1cbd3 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c  , pIndex->zName,
1cbd4 20 30 29 3b 0a 20 20 7d 0a 0a 65 78 69 74 5f 64   0);.  }..exit_d
1cbd5 72 6f 70 5f 69 6e 64 65 78 3a 0a 20 20 73 71 6c  rop_index:.  sql
1cbd6 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
1cbd7 65 28 70 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a  e(pName);.}../*.
1cbd8 2a 2a 20 70 41 72 72 61 79 20 69 73 20 61 20 70  ** pArray is a p
1cbd9 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72  ointer to an arr
1cbda 61 79 20 6f 66 20 6f 62 6a 65 63 74 73 2e 20 20  ay of objects.  
1cbdb 45 61 63 68 20 6f 62 6a 65 63 74 20 69 6e 20 74  Each object in t
1cbdc 68 65 0a 2a 2a 20 61 72 72 61 79 20 69 73 20 73  he.** array is s
1cbdd 7a 45 6e 74 72 79 20 62 79 74 65 73 20 69 6e 20  zEntry bytes in 
1cbde 73 69 7a 65 2e 20 20 54 68 69 73 20 72 6f 75 74  size.  This rout
1cbdf 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20  ine allocates a 
1cbe0 6e 65 77 0a 2a 2a 20 6f 62 6a 65 63 74 20 6f 6e  new.** object on
1cbe1 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
1cbe2 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 2a 70 6e  array..**.** *pn
1cbe3 45 6e 74 72 79 20 69 73 20 74 68 65 20 6e 75 6d  Entry is the num
1cbe4 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61  ber of entries a
1cbe5 6c 72 65 61 64 79 20 69 6e 20 75 73 65 2e 20 20  lready in use.  
1cbe6 2a 70 6e 41 6c 6c 6f 63 20 69 73 0a 2a 2a 20 74  *pnAlloc is.** t
1cbe7 68 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c  he previously al
1cbe8 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20  located size of 
1cbe9 74 68 65 20 61 72 72 61 79 2e 20 20 69 6e 69 74  the array.  init
1cbea 53 69 7a 65 20 69 73 20 74 68 65 0a 2a 2a 20 73  Size is the.** s
1cbeb 75 67 67 65 73 74 65 64 20 69 6e 69 74 69 61 6c  uggested initial
1cbec 20 61 72 72 61 79 20 73 69 7a 65 20 61 6c 6c 6f   array size allo
1cbed 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  cation..**.** Th
1cbee 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6e  e index of the n
1cbef 65 77 20 65 6e 74 72 79 20 69 73 20 72 65 74 75  ew entry is retu
1cbf0 72 6e 65 64 20 69 6e 20 2a 70 49 64 78 2e 0a 2a  rned in *pIdx..*
1cbf1 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1cbf2 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  e returns a poin
1cbf3 74 65 72 20 74 6f 20 74 68 65 20 61 72 72 61 79  ter to the array
1cbf4 20 6f 66 20 6f 62 6a 65 63 74 73 2e 20 20 54 68   of objects.  Th
1cbf5 69 73 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 74  is.** might be t
1cbf6 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 70  he same as the p
1cbf7 41 72 72 61 79 20 70 61 72 61 6d 65 74 65 72 20  Array parameter 
1cbf8 6f 72 20 69 74 20 6d 69 67 68 74 20 62 65 20 61  or it might be a
1cbf9 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 70 6f   different.** po
1cbfa 69 6e 74 65 72 20 69 66 20 74 68 65 20 61 72 72  inter if the arr
1cbfb 61 79 20 77 61 73 20 72 65 73 69 7a 65 64 2e 0a  ay was resized..
1cbfc 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1cbfd 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 41  E void *sqlite3A
1cbfe 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20 20  rrayAllocate(.  
1cbff 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
1cc00 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20    /* Connection 
1cc01 74 6f 20 6e 6f 74 69 66 79 20 6f 66 20 6d 61 6c  to notify of mal
1cc02 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 2a 2f 0a  loc failures */.
1cc03 20 20 76 6f 69 64 20 2a 70 41 72 72 61 79 2c 20    void *pArray, 
1cc04 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20      /* Array of 
1cc05 6f 62 6a 65 63 74 73 2e 20 20 4d 69 67 68 74 20  objects.  Might 
1cc06 62 65 20 72 65 61 6c 6c 6f 63 61 74 65 64 20 2a  be reallocated *
1cc07 2f 0a 20 20 69 6e 74 20 73 7a 45 6e 74 72 79 2c  /.  int szEntry,
1cc08 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
1cc09 20 65 61 63 68 20 6f 62 6a 65 63 74 20 69 6e 20   each object in 
1cc0a 74 68 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 69  the array */.  i
1cc0b 6e 74 20 69 6e 69 74 53 69 7a 65 2c 20 20 20 20  nt initSize,    
1cc0c 20 2f 2a 20 53 75 67 67 65 73 74 65 64 20 69 6e   /* Suggested in
1cc0d 69 74 69 61 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e  itial allocation
1cc0e 2c 20 69 6e 20 65 6c 65 6d 65 6e 74 73 20 2a 2f  , in elements */
1cc0f 0a 20 20 69 6e 74 20 2a 70 6e 45 6e 74 72 79 2c  .  int *pnEntry,
1cc10 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1cc11 66 20 6f 62 6a 65 63 74 73 20 63 75 72 72 65 6e  f objects curren
1cc12 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a 20 20  tly in use */.  
1cc13 69 6e 74 20 2a 70 6e 41 6c 6c 6f 63 2c 20 20 20  int *pnAlloc,   
1cc14 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 73 69 7a    /* Current siz
1cc15 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74  e of the allocat
1cc16 69 6f 6e 2c 20 69 6e 20 65 6c 65 6d 65 6e 74 73  ion, in elements
1cc17 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 49 64 78 20   */.  int *pIdx 
1cc18 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
1cc19 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 61 20   the index of a 
1cc1a 6e 65 77 20 73 6c 6f 74 20 68 65 72 65 20 2a 2f  new slot here */
1cc1b 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20  .){.  char *z;. 
1cc1c 20 69 66 28 20 2a 70 6e 45 6e 74 72 79 20 3e 3d   if( *pnEntry >=
1cc1d 20 2a 70 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20   *pnAlloc ){.   
1cc1e 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 20   void *pNew;.   
1cc1f 20 69 6e 74 20 6e 65 77 53 69 7a 65 3b 0a 20 20   int newSize;.  
1cc20 20 20 6e 65 77 53 69 7a 65 20 3d 20 28 2a 70 6e    newSize = (*pn
1cc21 41 6c 6c 6f 63 29 2a 32 20 2b 20 69 6e 69 74 53  Alloc)*2 + initS
1cc22 69 7a 65 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20  ize;.    pNew = 
1cc23 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
1cc24 28 64 62 2c 20 70 41 72 72 61 79 2c 20 6e 65 77  (db, pArray, new
1cc25 53 69 7a 65 2a 73 7a 45 6e 74 72 79 29 3b 0a 20  Size*szEntry);. 
1cc26 20 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29     if( pNew==0 )
1cc27 7b 0a 20 20 20 20 20 20 2a 70 49 64 78 20 3d 20  {.      *pIdx = 
1cc28 2d 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  -1;.      return
1cc29 20 70 41 72 72 61 79 3b 0a 20 20 20 20 7d 0a 20   pArray;.    }. 
1cc2a 20 20 20 2a 70 6e 41 6c 6c 6f 63 20 3d 20 6e 65     *pnAlloc = ne
1cc2b 77 53 69 7a 65 3b 0a 20 20 20 20 70 41 72 72 61  wSize;.    pArra
1cc2c 79 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20  y = pNew;.  }.  
1cc2d 7a 20 3d 20 28 63 68 61 72 2a 29 70 41 72 72 61  z = (char*)pArra
1cc2e 79 3b 0a 20 20 6d 65 6d 73 65 74 28 26 7a 5b 2a  y;.  memset(&z[*
1cc2f 70 6e 45 6e 74 72 79 20 2a 20 73 7a 45 6e 74 72  pnEntry * szEntr
1cc30 79 5d 2c 20 30 2c 20 73 7a 45 6e 74 72 79 29 3b  y], 0, szEntry);
1cc31 0a 20 20 2a 70 49 64 78 20 3d 20 2a 70 6e 45 6e  .  *pIdx = *pnEn
1cc32 74 72 79 3b 0a 20 20 2b 2b 2a 70 6e 45 6e 74 72  try;.  ++*pnEntr
1cc33 79 3b 0a 20 20 72 65 74 75 72 6e 20 70 41 72 72  y;.  return pArr
1cc34 61 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 70  ay;.}../*.** App
1cc35 65 6e 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e  end a new elemen
1cc36 74 20 74 6f 20 74 68 65 20 67 69 76 65 6e 20 49  t to the given I
1cc37 64 4c 69 73 74 2e 20 20 43 72 65 61 74 65 20 61  dList.  Create a
1cc38 20 6e 65 77 20 49 64 4c 69 73 74 20 69 66 0a 2a   new IdList if.*
1cc39 2a 20 6e 65 65 64 20 62 65 2e 0a 2a 2a 0a 2a 2a  * need be..**.**
1cc3a 20 41 20 6e 65 77 20 49 64 4c 69 73 74 20 69 73   A new IdList is
1cc3b 20 72 65 74 75 72 6e 65 64 2c 20 6f 72 20 4e 55   returned, or NU
1cc3c 4c 4c 20 69 66 20 6d 61 6c 6c 6f 63 28 29 20 66  LL if malloc() f
1cc3d 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ails..*/.SQLITE_
1cc3e 50 52 49 56 41 54 45 20 49 64 4c 69 73 74 20 2a  PRIVATE IdList *
1cc3f 73 71 6c 69 74 65 33 49 64 4c 69 73 74 41 70 70  sqlite3IdListApp
1cc40 65 6e 64 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  end(sqlite3 *db,
1cc41 20 49 64 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20   IdList *pList, 
1cc42 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a  Token *pToken){.
1cc43 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70    int i;.  if( p
1cc44 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70  List==0 ){.    p
1cc45 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62  List = sqlite3Db
1cc46 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
1cc47 69 7a 65 6f 66 28 49 64 4c 69 73 74 29 20 29 3b  izeof(IdList) );
1cc48 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d  .    if( pList==
1cc49 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
1cc4a 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20    pList->nAlloc 
1cc4b 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4c 69 73 74  = 0;.  }.  pList
1cc4c 2d 3e 61 20 3d 20 73 71 6c 69 74 65 33 41 72 72  ->a = sqlite3Arr
1cc4d 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20 20 20 20  ayAllocate(.    
1cc4e 20 20 64 62 2c 0a 20 20 20 20 20 20 70 4c 69 73    db,.      pLis
1cc4f 74 2d 3e 61 2c 0a 20 20 20 20 20 20 73 69 7a 65  t->a,.      size
1cc50 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30 5d 29 2c  of(pList->a[0]),
1cc51 0a 20 20 20 20 20 20 35 2c 0a 20 20 20 20 20 20  .      5,.      
1cc52 26 70 4c 69 73 74 2d 3e 6e 49 64 2c 0a 20 20 20  &pList->nId,.   
1cc53 20 20 20 26 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f     &pList->nAllo
1cc54 63 2c 0a 20 20 20 20 20 20 26 69 0a 20 20 29 3b  c,.      &i.  );
1cc55 0a 20 20 69 66 28 20 69 3c 30 20 29 7b 0a 20 20  .  if( i<0 ){.  
1cc56 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44    sqlite3IdListD
1cc57 65 6c 65 74 65 28 70 4c 69 73 74 29 3b 0a 20 20  elete(pList);.  
1cc58 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
1cc59 20 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e    pList->a[i].zN
1cc5a 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  ame = sqlite3Nam
1cc5b 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70  eFromToken(db, p
1cc5c 54 6f 6b 65 6e 29 3b 0a 20 20 72 65 74 75 72 6e  Token);.  return
1cc5d 20 70 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   pList;.}../*.**
1cc5e 20 44 65 6c 65 74 65 20 61 6e 20 49 64 4c 69 73   Delete an IdLis
1cc5f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
1cc60 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1cc61 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 49 64  3IdListDelete(Id
1cc62 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20  List *pList){.  
1cc63 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 4c 69  int i;.  if( pLi
1cc64 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  st==0 ) return;.
1cc65 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69    for(i=0; i<pLi
1cc66 73 74 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20  st->nId; i++){. 
1cc67 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
1cc68 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  pList->a[i].zNam
1cc69 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  e);.  }.  sqlite
1cc6a 33 5f 66 72 65 65 28 70 4c 69 73 74 2d 3e 61 29  3_free(pList->a)
1cc6b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
1cc6c 28 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  (pList);.}../*.*
1cc6d 2a 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64  * Return the ind
1cc6e 65 78 20 69 6e 20 70 4c 69 73 74 20 6f 66 20 74  ex in pList of t
1cc6f 68 65 20 69 64 65 6e 74 69 66 69 65 72 20 6e 61  he identifier na
1cc70 6d 65 64 20 7a 49 64 2e 20 20 52 65 74 75 72 6e  med zId.  Return
1cc71 20 2d 31 0a 2a 2a 20 69 66 20 6e 6f 74 20 66 6f   -1.** if not fo
1cc72 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  und..*/.SQLITE_P
1cc73 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1cc74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28 49 64  e3IdListIndex(Id
1cc75 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 63 6f 6e  List *pList, con
1cc76 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b  st char *zName){
1cc77 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
1cc78 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72  pList==0 ) retur
1cc79 6e 20 2d 31 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  n -1;.  for(i=0;
1cc7a 20 69 3c 70 4c 69 73 74 2d 3e 6e 49 64 3b 20 69   i<pList->nId; i
1cc7b 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  ++){.    if( sql
1cc7c 69 74 65 33 53 74 72 49 43 6d 70 28 70 4c 69 73  ite3StrICmp(pLis
1cc7d 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 7a  t->a[i].zName, z
1cc7e 4e 61 6d 65 29 3d 3d 30 20 29 20 72 65 74 75 72  Name)==0 ) retur
1cc7f 6e 20 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  n i;.  }.  retur
1cc80 6e 20 2d 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  n -1;.}../*.** A
1cc81 70 70 65 6e 64 20 61 20 6e 65 77 20 74 61 62 6c  ppend a new tabl
1cc82 65 20 6e 61 6d 65 20 74 6f 20 74 68 65 20 67 69  e name to the gi
1cc83 76 65 6e 20 53 72 63 4c 69 73 74 2e 20 20 43 72  ven SrcList.  Cr
1cc84 65 61 74 65 20 61 20 6e 65 77 20 53 72 63 4c 69  eate a new SrcLi
1cc85 73 74 20 69 66 0a 2a 2a 20 6e 65 65 64 20 62 65  st if.** need be
1cc86 2e 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20 69  .  A new entry i
1cc87 73 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 65  s created in the
1cc88 20 53 72 63 4c 69 73 74 20 65 76 65 6e 20 69 66   SrcList even if
1cc89 20 70 54 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c 2e   pToken is NULL.
1cc8a 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 77 20 53 72 63  .**.** A new Src
1cc8b 4c 69 73 74 20 69 73 20 72 65 74 75 72 6e 65 64  List is returned
1cc8c 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 6d 61 6c  , or NULL if mal
1cc8d 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2a 0a  loc() fails..**.
1cc8e 2a 2a 20 49 66 20 70 44 61 74 61 62 61 73 65 20  ** If pDatabase 
1cc8f 69 73 20 6e 6f 74 20 6e 75 6c 6c 2c 20 69 74 20  is not null, it 
1cc90 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 74  means that the t
1cc91 61 62 6c 65 20 68 61 73 20 61 6e 20 6f 70 74 69  able has an opti
1cc92 6f 6e 61 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65  onal.** database
1cc93 20 6e 61 6d 65 20 70 72 65 66 69 78 2e 20 20 4c   name prefix.  L
1cc94 69 6b 65 20 74 68 69 73 3a 20 20 22 64 61 74 61  ike this:  "data
1cc95 62 61 73 65 2e 74 61 62 6c 65 22 2e 20 20 54 68  base.table".  Th
1cc96 65 20 70 44 61 74 61 62 61 73 65 0a 2a 2a 20 70  e pDatabase.** p
1cc97 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 74 61 62  oints to the tab
1cc98 6c 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 65 20  le name and the 
1cc99 70 54 61 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f  pTable points to
1cc9a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61   the database na
1cc9b 6d 65 2e 0a 2a 2a 20 54 68 65 20 53 72 63 4c 69  me..** The SrcLi
1cc9c 73 74 2e 61 5b 5d 2e 7a 4e 61 6d 65 20 66 69 65  st.a[].zName fie
1cc9d 6c 64 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74  ld is filled wit
1cc9e 68 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  h the table name
1cc9f 20 77 68 69 63 68 20 6d 69 67 68 74 0a 2a 2a 20   which might.** 
1cca0 63 6f 6d 65 20 66 72 6f 6d 20 70 54 61 62 6c 65  come from pTable
1cca1 20 28 69 66 20 70 44 61 74 61 62 61 73 65 20 69   (if pDatabase i
1cca2 73 20 4e 55 4c 4c 29 20 6f 72 20 66 72 6f 6d 20  s NULL) or from 
1cca3 70 44 61 74 61 62 61 73 65 2e 20 20 0a 2a 2a 20  pDatabase.  .** 
1cca4 53 72 63 4c 69 73 74 2e 61 5b 5d 2e 7a 44 61 74  SrcList.a[].zDat
1cca5 61 62 61 73 65 20 69 73 20 66 69 6c 6c 65 64 20  abase is filled 
1cca6 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73  with the databas
1cca7 65 20 6e 61 6d 65 20 66 72 6f 6d 20 70 54 61 62  e name from pTab
1cca8 6c 65 2c 0a 2a 2a 20 6f 72 20 77 69 74 68 20 4e  le,.** or with N
1cca9 55 4c 4c 20 69 66 20 6e 6f 20 64 61 74 61 62 61  ULL if no databa
1ccaa 73 65 20 69 73 20 73 70 65 63 69 66 69 65 64 2e  se is specified.
1ccab 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20  .**.** In other 
1ccac 77 6f 72 64 73 2c 20 69 66 20 63 61 6c 6c 20 6c  words, if call l
1ccad 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
1ccae 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
1ccaf 72 63 4c 69 73 74 41 70 70 65 6e 64 28 44 2c 41  rcListAppend(D,A
1ccb0 2c 42 2c 30 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65  ,B,0);.**.** The
1ccb1 6e 20 42 20 69 73 20 61 20 74 61 62 6c 65 20 6e  n B is a table n
1ccb2 61 6d 65 20 61 6e 64 20 74 68 65 20 64 61 74 61  ame and the data
1ccb3 62 61 73 65 20 6e 61 6d 65 20 69 73 20 75 6e 73  base name is uns
1ccb4 70 65 63 69 66 69 65 64 2e 20 20 49 66 20 63 61  pecified.  If ca
1ccb5 6c 6c 65 64 0a 2a 2a 20 6c 69 6b 65 20 74 68 69  lled.** like thi
1ccb6 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  s:.**.**        
1ccb7 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
1ccb8 70 70 65 6e 64 28 44 2c 41 2c 42 2c 43 29 3b 0a  ppend(D,A,B,C);.
1ccb9 2a 2a 0a 2a 2a 20 54 68 65 6e 20 43 20 69 73 20  **.** Then C is 
1ccba 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 61  the table name a
1ccbb 6e 64 20 42 20 69 73 20 74 68 65 20 64 61 74 61  nd B is the data
1ccbc 62 61 73 65 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51  base name..*/.SQ
1ccbd 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 63  LITE_PRIVATE Src
1ccbe 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63  List *sqlite3Src
1ccbf 4c 69 73 74 41 70 70 65 6e 64 28 0a 20 20 73 71  ListAppend(.  sq
1ccc0 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
1ccc1 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20    /* Connection 
1ccc2 74 6f 20 6e 6f 74 69 66 79 20 6f 66 20 6d 61 6c  to notify of mal
1ccc3 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 2a 2f 0a  loc failures */.
1ccc4 20 20 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 74    SrcList *pList
1ccc5 2c 20 20 20 20 20 2f 2a 20 41 70 70 65 6e 64 20  ,     /* Append 
1ccc6 74 6f 20 74 68 69 73 20 53 72 63 4c 69 73 74 2e  to this SrcList.
1ccc7 20 4e 55 4c 4c 20 63 72 65 61 74 65 73 20 61 20   NULL creates a 
1ccc8 6e 65 77 20 53 72 63 4c 69 73 74 20 2a 2f 0a 20  new SrcList */. 
1ccc9 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 2c 20   Token *pTable, 
1ccca 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f       /* Table to
1cccb 20 61 70 70 65 6e 64 20 2a 2f 0a 20 20 54 6f 6b   append */.  Tok
1cccc 65 6e 20 2a 70 44 61 74 61 62 61 73 65 20 20 20  en *pDatabase   
1cccd 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6f 66 20   /* Database of 
1ccce 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a  the table */.){.
1cccf 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
1ccd0 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20  _item *pItem;.  
1ccd1 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a  if( pList==0 ){.
1ccd2 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69      pList = sqli
1ccd3 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
1ccd4 64 62 2c 20 73 69 7a 65 6f 66 28 53 72 63 4c 69  db, sizeof(SrcLi
1ccd5 73 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70  st) );.    if( p
1ccd6 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e  List==0 ) return
1ccd7 20 30 3b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 6e   0;.    pList->n
1ccd8 41 6c 6c 6f 63 20 3d 20 31 3b 0a 20 20 7d 0a 20  Alloc = 1;.  }. 
1ccd9 20 69 66 28 20 70 4c 69 73 74 2d 3e 6e 53 72 63   if( pList->nSrc
1ccda 3e 3d 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20  >=pList->nAlloc 
1ccdb 29 7b 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a  ){.    SrcList *
1ccdc 70 4e 65 77 3b 0a 20 20 20 20 70 4c 69 73 74 2d  pNew;.    pList-
1ccdd 3e 6e 41 6c 6c 6f 63 20 2a 3d 20 32 3b 0a 20 20  >nAlloc *= 2;.  
1ccde 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
1ccdf 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 4c  DbRealloc(db, pL
1cce0 69 73 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ist,.           
1cce1 20 20 20 20 73 69 7a 65 6f 66 28 2a 70 4c 69 73      sizeof(*pLis
1cce2 74 29 20 2b 20 28 70 4c 69 73 74 2d 3e 6e 41 6c  t) + (pList->nAl
1cce3 6c 6f 63 2d 31 29 2a 73 69 7a 65 6f 66 28 70 4c  loc-1)*sizeof(pL
1cce4 69 73 74 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20  ist->a[0]) );.  
1cce5 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b    if( pNew==0 ){
1cce6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 72  .      sqlite3Sr
1cce7 63 4c 69 73 74 44 65 6c 65 74 65 28 70 4c 69 73  cListDelete(pLis
1cce8 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  t);.      return
1cce9 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c   0;.    }.    pL
1ccea 69 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a  ist = pNew;.  }.
1cceb 20 20 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 74    pItem = &pList
1ccec 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e 53 72 63 5d  ->a[pList->nSrc]
1cced 3b 0a 20 20 6d 65 6d 73 65 74 28 70 49 74 65 6d  ;.  memset(pItem
1ccee 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 4c 69 73  , 0, sizeof(pLis
1ccef 74 2d 3e 61 5b 30 5d 29 29 3b 0a 20 20 69 66 28  t->a[0]));.  if(
1ccf0 20 70 44 61 74 61 62 61 73 65 20 26 26 20 70 44   pDatabase && pD
1ccf1 61 74 61 62 61 73 65 2d 3e 7a 3d 3d 30 20 29 7b  atabase->z==0 ){
1ccf2 0a 20 20 20 20 70 44 61 74 61 62 61 73 65 20 3d  .    pDatabase =
1ccf3 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 44   0;.  }.  if( pD
1ccf4 61 74 61 62 61 73 65 20 26 26 20 70 54 61 62 6c  atabase && pTabl
1ccf5 65 20 29 7b 0a 20 20 20 20 54 6f 6b 65 6e 20 2a  e ){.    Token *
1ccf6 70 54 65 6d 70 20 3d 20 70 44 61 74 61 62 61 73  pTemp = pDatabas
1ccf7 65 3b 0a 20 20 20 20 70 44 61 74 61 62 61 73 65  e;.    pDatabase
1ccf8 20 3d 20 70 54 61 62 6c 65 3b 0a 20 20 20 20 70   = pTable;.    p
1ccf9 54 61 62 6c 65 20 3d 20 70 54 65 6d 70 3b 0a 20  Table = pTemp;. 
1ccfa 20 7d 0a 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d   }.  pItem->zNam
1ccfb 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  e = sqlite3NameF
1ccfc 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 61  romToken(db, pTa
1ccfd 62 6c 65 29 3b 0a 20 20 70 49 74 65 6d 2d 3e 7a  ble);.  pItem->z
1ccfe 44 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74  Database = sqlit
1ccff 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
1cd00 64 62 2c 20 70 44 61 74 61 62 61 73 65 29 3b 0a  db, pDatabase);.
1cd01 20 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72    pItem->iCursor
1cd02 20 3d 20 2d 31 3b 0a 20 20 70 49 74 65 6d 2d 3e   = -1;.  pItem->
1cd03 69 73 50 6f 70 75 6c 61 74 65 64 20 3d 20 30 3b  isPopulated = 0;
1cd04 0a 20 20 70 4c 69 73 74 2d 3e 6e 53 72 63 2b 2b  .  pList->nSrc++
1cd05 3b 0a 20 20 72 65 74 75 72 6e 20 70 4c 69 73 74  ;.  return pList
1cd06 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 69 67  ;.}../*.** Assig
1cd07 6e 20 63 75 72 73 6f 72 73 20 74 6f 20 61 6c 6c  n cursors to all
1cd08 20 74 61 62 6c 65 73 20 69 6e 20 61 20 53 72 63   tables in a Src
1cd09 4c 69 73 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  List.*/.SQLITE_P
1cd0a 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1cd0b 74 65 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e  te3SrcListAssign
1cd0c 43 75 72 73 6f 72 73 28 50 61 72 73 65 20 2a 70  Cursors(Parse *p
1cd0d 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a  Parse, SrcList *
1cd0e 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b  pList){.  int i;
1cd0f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
1cd10 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20  t_item *pItem;. 
1cd11 20 61 73 73 65 72 74 28 70 4c 69 73 74 20 7c 7c   assert(pList ||
1cd12 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c   pParse->db->mal
1cd13 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69  locFailed );.  i
1cd14 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20  f( pList ){.    
1cd15 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70  for(i=0, pItem=p
1cd16 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74  List->a; i<pList
1cd17 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74  ->nSrc; i++, pIt
1cd18 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28  em++){.      if(
1cd19 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3e   pItem->iCursor>
1cd1a 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =0 ) break;.    
1cd1b 20 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72    pItem->iCursor
1cd1c 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
1cd1d 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 74  +;.      if( pIt
1cd1e 65 6d 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20  em->pSelect ){. 
1cd1f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 72         sqlite3Sr
1cd20 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f  cListAssignCurso
1cd21 72 73 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d  rs(pParse, pItem
1cd22 2d 3e 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 29  ->pSelect->pSrc)
1cd23 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1cd24 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c    }.}../*.** Del
1cd25 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20 53 72  ete an entire Sr
1cd26 63 4c 69 73 74 20 69 6e 63 6c 75 64 69 6e 67 20  cList including 
1cd27 61 6c 6c 20 69 74 73 20 73 75 62 73 74 72 75 63  all its substruc
1cd28 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ture..*/.SQLITE_
1cd29 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1cd2a 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
1cd2b 65 28 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 74  e(SrcList *pList
1cd2c 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74  ){.  int i;.  st
1cd2d 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
1cd2e 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 20  m *pItem;.  if( 
1cd2f 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72  pList==0 ) retur
1cd30 6e 3b 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70  n;.  for(pItem=p
1cd31 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c  List->a, i=0; i<
1cd32 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  pList->nSrc; i++
1cd33 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
1cd34 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 49 74  sqlite3_free(pIt
1cd35 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a  em->zDatabase);.
1cd36 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
1cd37 28 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a  (pItem->zName);.
1cd38 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
1cd39 28 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b  (pItem->zAlias);
1cd3a 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65  .    sqlite3Dele
1cd3b 74 65 54 61 62 6c 65 28 70 49 74 65 6d 2d 3e 70  teTable(pItem->p
1cd3c 54 61 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Tab);.    sqlite
1cd3d 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 49  3SelectDelete(pI
1cd3e 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20  tem->pSelect);. 
1cd3f 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
1cd40 6c 65 74 65 28 70 49 74 65 6d 2d 3e 70 4f 6e 29  lete(pItem->pOn)
1cd41 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c  ;.    sqlite3IdL
1cd42 69 73 74 44 65 6c 65 74 65 28 70 49 74 65 6d 2d  istDelete(pItem-
1cd43 3e 70 55 73 69 6e 67 29 3b 0a 20 20 7d 0a 20 20  >pUsing);.  }.  
1cd44 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4c 69  sqlite3_free(pLi
1cd45 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  st);.}../*.** Th
1cd46 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
1cd47 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
1cd48 65 72 20 74 6f 20 61 64 64 20 61 20 6e 65 77 20  er to add a new 
1cd49 74 65 72 6d 20 74 6f 20 74 68 65 0a 2a 2a 20 65  term to the.** e
1cd4a 6e 64 20 6f 66 20 61 20 67 72 6f 77 69 6e 67 20  nd of a growing 
1cd4b 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54 68  FROM clause.  Th
1cd4c 65 20 22 70 22 20 70 61 72 61 6d 65 74 65 72 20  e "p" parameter 
1cd4d 69 73 20 74 68 65 20 70 61 72 74 20 6f 66 0a 2a  is the part of.*
1cd4e 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  * the FROM claus
1cd4f 65 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61  e that has alrea
1cd50 64 79 20 62 65 65 6e 20 63 6f 6e 73 74 72 75 63  dy been construc
1cd51 74 65 64 2e 20 20 22 70 22 20 69 73 20 4e 55 4c  ted.  "p" is NUL
1cd52 4c 0a 2a 2a 20 69 66 20 74 68 69 73 20 69 73 20  L.** if this is 
1cd53 74 68 65 20 66 69 72 73 74 20 74 65 72 6d 20 6f  the first term o
1cd54 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  f the FROM claus
1cd55 65 2e 20 20 70 54 61 62 6c 65 20 61 6e 64 20 70  e.  pTable and p
1cd56 44 61 74 61 62 61 73 65 0a 2a 2a 20 61 72 65 20  Database.** are 
1cd57 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
1cd58 74 61 62 6c 65 20 61 6e 64 20 64 61 74 61 62 61  table and databa
1cd59 73 65 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20  se named in the 
1cd5a 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d  FROM clause term
1cd5b 2e 0a 2a 2a 20 70 44 61 74 61 62 61 73 65 20 69  ..** pDatabase i
1cd5c 73 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 64 61  s NULL if the da
1cd5d 74 61 62 61 73 65 20 6e 61 6d 65 20 71 75 61 6c  tabase name qual
1cd5e 69 66 69 65 72 20 69 73 20 6d 69 73 73 69 6e 67  ifier is missing
1cd5f 20 2d 20 74 68 65 0a 2a 2a 20 75 73 75 61 6c 20   - the.** usual 
1cd60 63 61 73 65 2e 20 20 49 66 20 74 68 65 20 74 65  case.  If the te
1cd61 72 6d 20 68 61 73 20 61 20 61 6c 69 61 73 2c 20  rm has a alias, 
1cd62 74 68 65 6e 20 70 41 6c 69 61 73 20 70 6f 69 6e  then pAlias poin
1cd63 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 61 6c 69  ts to the.** ali
1cd64 61 73 20 74 6f 6b 65 6e 2e 20 20 49 66 20 74 68  as token.  If th
1cd65 65 20 74 65 72 6d 20 69 73 20 61 20 73 75 62 71  e term is a subq
1cd66 75 65 72 79 2c 20 74 68 65 6e 20 70 53 75 62 71  uery, then pSubq
1cd67 75 65 72 79 20 69 73 20 74 68 65 0a 2a 2a 20 53  uery is the.** S
1cd68 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
1cd69 74 68 61 74 20 74 68 65 20 73 75 62 71 75 65 72  that the subquer
1cd6a 79 20 65 6e 63 6f 64 65 73 2e 20 20 54 68 65 20  y encodes.  The 
1cd6b 70 54 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 70 44  pTable and.** pD
1cd6c 61 74 61 62 61 73 65 20 70 61 72 61 6d 65 74 65  atabase paramete
1cd6d 72 73 20 61 72 65 20 4e 55 4c 4c 20 66 6f 72 20  rs are NULL for 
1cd6e 73 75 62 71 75 65 72 69 65 73 2e 20 20 54 68 65  subqueries.  The
1cd6f 20 70 4f 6e 20 61 6e 64 20 70 55 73 69 6e 67 0a   pOn and pUsing.
1cd70 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61 72  ** parameters ar
1cd71 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  e the content of
1cd72 20 74 68 65 20 4f 4e 20 61 6e 64 20 55 53 49 4e   the ON and USIN
1cd73 47 20 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a 2a  G clauses..**.**
1cd74 20 52 65 74 75 72 6e 20 61 20 6e 65 77 20 53 72   Return a new Sr
1cd75 63 4c 69 73 74 20 77 68 69 63 68 20 65 6e 63 6f  cList which enco
1cd76 64 65 73 20 69 73 20 74 68 65 20 46 52 4f 4d 20  des is the FROM 
1cd77 77 69 74 68 20 74 68 65 20 6e 65 77 0a 2a 2a 20  with the new.** 
1cd78 74 65 72 6d 20 61 64 64 65 64 2e 0a 2a 2f 0a 53  term added..*/.S
1cd79 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 72  QLITE_PRIVATE Sr
1cd7a 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72  cList *sqlite3Sr
1cd7b 63 4c 69 73 74 41 70 70 65 6e 64 46 72 6f 6d 54  cListAppendFromT
1cd7c 65 72 6d 28 0a 20 20 50 61 72 73 65 20 2a 70 50  erm(.  Parse *pP
1cd7d 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  arse,          /
1cd7e 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
1cd7f 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  t */.  SrcList *
1cd80 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  p,             /
1cd81 2a 20 54 68 65 20 6c 65 66 74 20 70 61 72 74 20  * The left part 
1cd82 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  of the FROM clau
1cd83 73 65 20 61 6c 72 65 61 64 79 20 73 65 65 6e 20  se already seen 
1cd84 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62  */.  Token *pTab
1cd85 6c 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  le,          /* 
1cd86 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  Name of the tabl
1cd87 65 20 74 6f 20 61 64 64 20 74 6f 20 74 68 65 20  e to add to the 
1cd88 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20  FROM clause */. 
1cd89 20 54 6f 6b 65 6e 20 2a 70 44 61 74 61 62 61 73   Token *pDatabas
1cd8a 65 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  e,       /* Name
1cd8b 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1cd8c 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61 62   containing pTab
1cd8d 6c 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  le */.  Token *p
1cd8e 41 6c 69 61 73 2c 20 20 20 20 20 20 20 20 20 20  Alias,          
1cd8f 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 68 61 6e  /* The right-han
1cd90 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 41 53  d side of the AS
1cd91 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 2a   subexpression *
1cd92 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 75 62  /.  Select *pSub
1cd93 71 75 65 72 79 2c 20 20 20 20 20 20 2f 2a 20 41  query,      /* A
1cd94 20 73 75 62 71 75 65 72 79 20 75 73 65 64 20 69   subquery used i
1cd95 6e 20 70 6c 61 63 65 20 6f 66 20 61 20 74 61 62  n place of a tab
1cd96 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 45 78 70  le name */.  Exp
1cd97 72 20 2a 70 4f 6e 2c 20 20 20 20 20 20 20 20 20  r *pOn,         
1cd98 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 4e 20 63       /* The ON c
1cd99 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20  lause of a join 
1cd9a 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 55 73  */.  IdList *pUs
1cd9b 69 6e 67 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ing          /* 
1cd9c 54 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65  The USING clause
1cd9d 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 29 7b   of a join */.){
1cd9e 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
1cd9f 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20  t_item *pItem;. 
1cda0 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
1cda1 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 20 3d  Parse->db;.  p =
1cda2 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
1cda3 70 70 65 6e 64 28 64 62 2c 20 70 2c 20 70 54 61  ppend(db, p, pTa
1cda4 62 6c 65 2c 20 70 44 61 74 61 62 61 73 65 29 3b  ble, pDatabase);
1cda5 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 70  .  if( p==0 || p
1cda6 2d 3e 6e 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20  ->nSrc==0 ){.   
1cda7 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
1cda8 74 65 28 70 4f 6e 29 3b 0a 20 20 20 20 73 71 6c  te(pOn);.    sql
1cda9 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65  ite3IdListDelete
1cdaa 28 70 55 73 69 6e 67 29 3b 0a 20 20 20 20 73 71  (pUsing);.    sq
1cdab 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
1cdac 65 28 70 53 75 62 71 75 65 72 79 29 3b 0a 20 20  e(pSubquery);.  
1cdad 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 7d 0a    return p;.  }.
1cdae 20 20 70 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b    pItem = &p->a[
1cdaf 70 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20 69 66  p->nSrc-1];.  if
1cdb0 28 20 70 41 6c 69 61 73 20 26 26 20 70 41 6c 69  ( pAlias && pAli
1cdb1 61 73 2d 3e 6e 20 29 7b 0a 20 20 20 20 70 49 74  as->n ){.    pIt
1cdb2 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20 73 71 6c  em->zAlias = sql
1cdb3 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
1cdb4 6e 28 64 62 2c 20 70 41 6c 69 61 73 29 3b 0a 20  n(db, pAlias);. 
1cdb5 20 7d 0a 20 20 70 49 74 65 6d 2d 3e 70 53 65 6c   }.  pItem->pSel
1cdb6 65 63 74 20 3d 20 70 53 75 62 71 75 65 72 79 3b  ect = pSubquery;
1cdb7 0a 20 20 70 49 74 65 6d 2d 3e 70 4f 6e 20 3d 20  .  pItem->pOn = 
1cdb8 70 4f 6e 3b 0a 20 20 70 49 74 65 6d 2d 3e 70 55  pOn;.  pItem->pU
1cdb9 73 69 6e 67 20 3d 20 70 55 73 69 6e 67 3b 0a 20  sing = pUsing;. 
1cdba 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
1cdbb 0a 2a 2a 20 57 68 65 6e 20 62 75 69 6c 64 69 6e  .** When buildin
1cdbc 67 20 75 70 20 61 20 46 52 4f 4d 20 63 6c 61 75  g up a FROM clau
1cdbd 73 65 20 69 6e 20 74 68 65 20 70 61 72 73 65 72  se in the parser
1cdbe 2c 20 74 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61  , the join opera
1cdbf 74 6f 72 0a 2a 2a 20 69 73 20 69 6e 69 74 69 61  tor.** is initia
1cdc0 6c 6c 79 20 61 74 74 61 63 68 65 64 20 74 6f 20  lly attached to 
1cdc1 74 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64  the left operand
1cdc2 2e 20 20 42 75 74 20 74 68 65 20 63 6f 64 65 20  .  But the code 
1cdc3 67 65 6e 65 72 61 74 6f 72 0a 2a 2a 20 65 78 70  generator.** exp
1cdc4 65 63 74 73 20 74 68 65 20 6a 6f 69 6e 20 6f 70  ects the join op
1cdc5 65 72 61 74 6f 72 20 74 6f 20 62 65 20 6f 6e 20  erator to be on 
1cdc6 74 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e  the right operan
1cdc7 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  d.  This routine
1cdc8 0a 2a 2a 20 53 68 69 66 74 73 20 61 6c 6c 20 6a  .** Shifts all j
1cdc9 6f 69 6e 20 6f 70 65 72 61 74 6f 72 73 20 66 72  oin operators fr
1cdca 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67 68 74  om left to right
1cdcb 20 66 6f 72 20 61 6e 20 65 6e 74 69 72 65 20 46   for an entire F
1cdcc 52 4f 4d 0a 2a 2a 20 63 6c 61 75 73 65 2e 0a 2a  ROM.** clause..*
1cdcd 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a 20 53 75  *.** Example: Su
1cdce 70 70 6f 73 65 20 74 68 65 20 6a 6f 69 6e 20 69  ppose the join i
1cdcf 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a  s like this:.**.
1cdd0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 41 20 6e  **           A n
1cdd1 61 74 75 72 61 6c 20 63 72 6f 73 73 20 6a 6f 69  atural cross joi
1cdd2 6e 20 42 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70  n B.**.** The op
1cdd3 65 72 61 74 6f 72 20 69 73 20 22 6e 61 74 75 72  erator is "natur
1cdd4 61 6c 20 63 72 6f 73 73 20 6a 6f 69 6e 22 2e 20  al cross join". 
1cdd5 20 54 68 65 20 41 20 61 6e 64 20 42 20 6f 70 65   The A and B ope
1cdd6 72 61 6e 64 73 20 61 72 65 20 73 74 6f 72 65 64  rands are stored
1cdd7 0a 2a 2a 20 69 6e 20 70 2d 3e 61 5b 30 5d 20 61  .** in p->a[0] a
1cdd8 6e 64 20 70 2d 3e 61 5b 31 5d 2c 20 72 65 73 70  nd p->a[1], resp
1cdd9 65 63 74 69 76 65 6c 79 2e 20 20 54 68 65 20 70  ectively.  The p
1cdda 61 72 73 65 72 20 69 6e 69 74 69 61 6c 6c 79 20  arser initially 
1cddb 73 74 6f 72 65 73 20 74 68 65 0a 2a 2a 20 6f 70  stores the.** op
1cddc 65 72 61 74 6f 72 20 77 69 74 68 20 41 2e 20 20  erator with A.  
1cddd 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 69  This routine shi
1cdde 66 74 73 20 74 68 61 74 20 6f 70 65 72 61 74 6f  fts that operato
1cddf 72 20 6f 76 65 72 20 74 6f 20 42 2e 0a 2a 2f 0a  r over to B..*/.
1cde0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1cde1 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69  oid sqlite3SrcLi
1cde2 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28  stShiftJoinType(
1cde3 53 72 63 4c 69 73 74 20 2a 70 29 7b 0a 20 20 69  SrcList *p){.  i
1cde4 66 28 20 70 20 26 26 20 70 2d 3e 61 20 29 7b 0a  f( p && p->a ){.
1cde5 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66      int i;.    f
1cde6 6f 72 28 69 3d 70 2d 3e 6e 53 72 63 2d 31 3b 20  or(i=p->nSrc-1; 
1cde7 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 20  i>0; i--){.     
1cde8 20 70 2d 3e 61 5b 69 5d 2e 6a 6f 69 6e 74 79 70   p->a[i].jointyp
1cde9 65 20 3d 20 70 2d 3e 61 5b 69 2d 31 5d 2e 6a 6f  e = p->a[i-1].jo
1cdea 69 6e 74 79 70 65 3b 0a 20 20 20 20 7d 0a 20 20  intype;.    }.  
1cdeb 20 20 70 2d 3e 61 5b 30 5d 2e 6a 6f 69 6e 74 79    p->a[0].jointy
1cdec 70 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f  pe = 0;.  }.}../
1cded 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20 74 72 61  *.** Begin a tra
1cdee 6e 73 61 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49  nsaction.*/.SQLI
1cdef 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1cdf0 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 61 6e  sqlite3BeginTran
1cdf1 73 61 63 74 69 6f 6e 28 50 61 72 73 65 20 2a 70  saction(Parse *p
1cdf2 50 61 72 73 65 2c 20 69 6e 74 20 74 79 70 65 29  Parse, int type)
1cdf3 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  {.  sqlite3 *db;
1cdf4 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e  .  Vdbe *v;.  in
1cdf5 74 20 69 3b 0a 0a 20 20 69 66 28 20 70 50 61 72  t i;..  if( pPar
1cdf6 73 65 3d 3d 30 20 7c 7c 20 28 64 62 3d 70 50 61  se==0 || (db=pPa
1cdf7 72 73 65 2d 3e 64 62 29 3d 3d 30 20 7c 7c 20 64  rse->db)==0 || d
1cdf8 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 3d 3d 30  b->aDb[0].pBt==0
1cdf9 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
1cdfa 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c   pParse->nErr ||
1cdfb 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1cdfc 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  d ) return;.  if
1cdfd 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  ( sqlite3AuthChe
1cdfe 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54  ck(pParse, SQLIT
1cdff 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e 2c 20 22  E_TRANSACTION, "
1ce00 42 45 47 49 4e 22 2c 20 30 2c 20 30 29 20 29 20  BEGIN", 0, 0) ) 
1ce01 72 65 74 75 72 6e 3b 0a 0a 20 20 76 20 3d 20 73  return;..  v = s
1ce02 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
1ce03 61 72 73 65 29 3b 0a 20 20 69 66 28 20 21 76 20  arse);.  if( !v 
1ce04 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
1ce05 74 79 70 65 21 3d 54 4b 5f 44 45 46 45 52 52 45  type!=TK_DEFERRE
1ce06 44 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  D ){.    for(i=0
1ce07 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
1ce08 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1ce09 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1ce0a 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 2c  _Transaction, i,
1ce0b 20 28 74 79 70 65 3d 3d 54 4b 5f 45 58 43 4c 55   (type==TK_EXCLU
1ce0c 53 49 56 45 29 2b 31 29 3b 0a 20 20 20 20 20 20  SIVE)+1);.      
1ce0d 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42  sqlite3VdbeUsesB
1ce0e 74 72 65 65 28 76 2c 20 69 29 3b 0a 20 20 20 20  tree(v, i);.    
1ce0f 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56  }.  }.  sqlite3V
1ce10 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1ce11 41 75 74 6f 43 6f 6d 6d 69 74 2c 20 30 2c 20 30  AutoCommit, 0, 0
1ce12 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d  );.}../*.** Comm
1ce13 69 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  it a transaction
1ce14 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1ce15 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
1ce16 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e  ommitTransaction
1ce17 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b  (Parse *pParse){
1ce18 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a  .  sqlite3 *db;.
1ce19 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 69 66    Vdbe *v;..  if
1ce1a 28 20 70 50 61 72 73 65 3d 3d 30 20 7c 7c 20 28  ( pParse==0 || (
1ce1b 64 62 3d 70 50 61 72 73 65 2d 3e 64 62 29 3d 3d  db=pParse->db)==
1ce1c 30 20 7c 7c 20 64 62 2d 3e 61 44 62 5b 30 5d 2e  0 || db->aDb[0].
1ce1d 70 42 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  pBt==0 ) return;
1ce1e 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
1ce1f 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  Err || db->mallo
1ce20 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e  cFailed ) return
1ce21 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41  ;.  if( sqlite3A
1ce22 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
1ce23 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 54   SQLITE_TRANSACT
1ce24 49 4f 4e 2c 20 22 43 4f 4d 4d 49 54 22 2c 20 30  ION, "COMMIT", 0
1ce25 2c 20 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 0a  , 0) ) return;..
1ce26 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
1ce27 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
1ce28 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c  if( v ){.    sql
1ce29 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1ce2a 2c 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c  , OP_AutoCommit,
1ce2b 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f   1, 0);.  }.}../
1ce2c 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 20  *.** Rollback a 
1ce2d 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2f 0a 53  transaction.*/.S
1ce2e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1ce2f 69 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61  id sqlite3Rollba
1ce30 63 6b 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61  ckTransaction(Pa
1ce31 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20  rse *pParse){.  
1ce32 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56  sqlite3 *db;.  V
1ce33 64 62 65 20 2a 76 3b 0a 0a 20 20 69 66 28 20 70  dbe *v;..  if( p
1ce34 50 61 72 73 65 3d 3d 30 20 7c 7c 20 28 64 62 3d  Parse==0 || (db=
1ce35 70 50 61 72 73 65 2d 3e 64 62 29 3d 3d 30 20 7c  pParse->db)==0 |
1ce36 7c 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74  | db->aDb[0].pBt
1ce37 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
1ce38 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
1ce39 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
1ce3a 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20  iled ) return;. 
1ce3b 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68   if( sqlite3Auth
1ce3c 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51  Check(pParse, SQ
1ce3d 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e  LITE_TRANSACTION
1ce3e 2c 20 22 52 4f 4c 4c 42 41 43 4b 22 2c 20 30 2c  , "ROLLBACK", 0,
1ce3f 20 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20   0) ) return;.. 
1ce40 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
1ce41 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69  dbe(pParse);.  i
1ce42 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69  f( v ){.    sqli
1ce43 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1ce44 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c 20   OP_AutoCommit, 
1ce45 31 2c 20 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  1, 1);.  }.}../*
1ce46 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68  .** Make sure th
1ce47 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 20  e TEMP database 
1ce48 69 73 20 6f 70 65 6e 20 61 6e 64 20 61 76 61 69  is open and avai
1ce49 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 2e 20 20  lable for use.  
1ce4a 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 6e 75  Return.** the nu
1ce4b 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 20  mber of errors. 
1ce4c 20 4c 65 61 76 65 20 61 6e 79 20 65 72 72 6f 72   Leave any error
1ce4d 20 6d 65 73 73 61 67 65 73 20 69 6e 20 74 68 65   messages in the
1ce4e 20 70 50 61 72 73 65 20 73 74 72 75 63 74 75 72   pParse structur
1ce4f 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1ce50 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1ce51 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65  OpenTempDatabase
1ce52 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b  (Parse *pParse){
1ce53 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1ce54 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
1ce55 66 28 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42  f( db->aDb[1].pB
1ce56 74 3d 3d 30 20 26 26 20 21 70 50 61 72 73 65 2d  t==0 && !pParse-
1ce57 3e 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20  >explain ){.    
1ce58 69 6e 74 20 72 63 3b 0a 20 20 20 20 73 74 61 74  int rc;.    stat
1ce59 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 66 6c 61  ic const int fla
1ce5a 67 73 20 3d 20 0a 20 20 20 20 20 20 20 20 20 20  gs = .          
1ce5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
1ce5c 57 52 49 54 45 20 7c 0a 20 20 20 20 20 20 20 20  WRITE |.        
1ce5d 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52    SQLITE_OPEN_CR
1ce5e 45 41 54 45 20 7c 0a 20 20 20 20 20 20 20 20 20  EATE |.         
1ce5f 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43   SQLITE_OPEN_EXC
1ce60 4c 55 53 49 56 45 20 7c 0a 20 20 20 20 20 20 20  LUSIVE |.       
1ce61 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44     SQLITE_OPEN_D
1ce62 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 7c 0a 20  ELETEONCLOSE |. 
1ce63 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
1ce64 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 3b 0a 0a 20  OPEN_TEMP_DB;.. 
1ce65 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1ce66 74 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c 20  treeFactory(db, 
1ce67 30 2c 20 30 2c 20 53 51 4c 49 54 45 5f 44 45 46  0, 0, SQLITE_DEF
1ce68 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 2c  AULT_CACHE_SIZE,
1ce69 20 66 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20   flags,.        
1ce6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ce6b 20 20 20 20 20 20 20 20 20 26 64 62 2d 3e 61 44           &db->aD
1ce6c 62 5b 31 5d 2e 70 42 74 29 3b 0a 20 20 20 20 69  b[1].pBt);.    i
1ce6d 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1ce6e 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1ce6f 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1ce70 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65  , "unable to ope
1ce71 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 64 61  n a temporary da
1ce72 74 61 62 61 73 65 20 22 0a 20 20 20 20 20 20 20  tabase ".       
1ce73 20 22 66 69 6c 65 20 66 6f 72 20 73 74 6f 72 69   "file for stori
1ce74 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  ng temporary tab
1ce75 6c 65 73 22 29 3b 0a 20 20 20 20 20 20 70 50 61  les");.      pPa
1ce76 72 73 65 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20  rse->rc = rc;.  
1ce77 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
1ce78 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
1ce79 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  (db->flags & SQL
1ce7a 49 54 45 5f 49 6e 54 72 61 6e 73 29 3d 3d 30 20  ITE_InTrans)==0 
1ce7b 7c 7c 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69  || db->autoCommi
1ce7c 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  t );.    assert(
1ce7d 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68   db->aDb[1].pSch
1ce7e 65 6d 61 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ema );.    sqlit
1ce7f 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f  e3PagerJournalMo
1ce80 64 65 28 73 71 6c 69 74 65 33 42 74 72 65 65 50  de(sqlite3BtreeP
1ce81 61 67 65 72 28 64 62 2d 3e 61 44 62 5b 31 5d 2e  ager(db->aDb[1].
1ce82 70 42 74 29 2c 0a 20 20 20 20 20 20 20 20 20 20  pBt),.          
1ce83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ce84 20 20 64 62 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61    db->dfltJourna
1ce85 6c 4d 6f 64 65 29 3b 0a 20 20 7d 0a 20 20 72 65  lMode);.  }.  re
1ce86 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
1ce87 20 47 65 6e 65 72 61 74 65 20 56 44 42 45 20 63   Generate VDBE c
1ce88 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 76 65  ode that will ve
1ce89 72 69 66 79 20 74 68 65 20 73 63 68 65 6d 61 20  rify the schema 
1ce8a 63 6f 6f 6b 69 65 20 61 6e 64 20 73 74 61 72 74  cookie and start
1ce8b 0a 2a 2a 20 61 20 72 65 61 64 2d 74 72 61 6e 73  .** a read-trans
1ce8c 61 63 74 69 6f 6e 20 66 6f 72 20 61 6c 6c 20 6e  action for all n
1ce8d 61 6d 65 64 20 64 61 74 61 62 61 73 65 20 66 69  amed database fi
1ce8e 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73  les..**.** It is
1ce8f 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20   important that 
1ce90 61 6c 6c 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69  all schema cooki
1ce91 65 73 20 62 65 20 76 65 72 69 66 69 65 64 20 61  es be verified a
1ce92 6e 64 20 61 6c 6c 0a 2a 2a 20 72 65 61 64 20 74  nd all.** read t
1ce93 72 61 6e 73 61 63 74 69 6f 6e 73 20 62 65 20 73  ransactions be s
1ce94 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 61 6e  tarted before an
1ce95 79 74 68 69 6e 67 20 65 6c 73 65 20 68 61 70 70  ything else happ
1ce96 65 6e 73 20 69 6e 0a 2a 2a 20 74 68 65 20 56 44  ens in.** the VD
1ce97 42 45 20 70 72 6f 67 72 61 6d 2e 20 20 42 75 74  BE program.  But
1ce98 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61   this routine ca
1ce99 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 66 74 65  n be called afte
1ce9a 72 20 6d 75 63 68 20 6f 74 68 65 72 0a 2a 2a 20  r much other.** 
1ce9b 63 6f 64 65 20 68 61 73 20 62 65 65 6e 20 67 65  code has been ge
1ce9c 6e 65 72 61 74 65 64 2e 20 20 53 6f 20 68 65 72  nerated.  So her
1ce9d 65 20 69 73 20 77 68 61 74 20 77 65 20 64 6f 3a  e is what we do:
1ce9e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
1ce9f 20 74 69 6d 65 20 74 68 69 73 20 72 6f 75 74 69   time this routi
1cea0 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 77 65  ne is called, we
1cea1 20 63 6f 64 65 20 61 6e 20 4f 50 5f 47 6f 74 6f   code an OP_Goto
1cea2 20 74 68 61 74 0a 2a 2a 20 77 69 6c 6c 20 6a 75   that.** will ju
1cea3 6d 70 20 74 6f 20 61 20 73 75 62 72 6f 75 74 69  mp to a subrouti
1cea4 6e 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  ne at the end of
1cea5 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 54   the program.  T
1cea6 68 65 6e 20 77 65 0a 2a 2a 20 72 65 63 6f 72 64  hen we.** record
1cea7 20 65 76 65 72 79 20 64 61 74 61 62 61 73 65 20   every database 
1cea8 74 68 61 74 20 6e 65 65 64 73 20 69 74 73 20 73  that needs its s
1cea9 63 68 65 6d 61 20 76 65 72 69 66 69 65 64 20 69  chema verified i
1ceaa 6e 20 74 68 65 0a 2a 2a 20 70 50 61 72 73 65 2d  n the.** pParse-
1ceab 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 66 69 65 6c  >cookieMask fiel
1ceac 64 2e 20 20 4c 61 74 65 72 2c 20 61 66 74 65 72  d.  Later, after
1cead 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65 20   all other code 
1ceae 68 61 73 20 62 65 65 6e 0a 2a 2a 20 67 65 6e 65  has been.** gene
1ceaf 72 61 74 65 64 2c 20 74 68 65 20 73 75 62 72 6f  rated, the subro
1ceb0 75 74 69 6e 65 20 74 68 61 74 20 64 6f 65 73 20  utine that does 
1ceb1 74 68 65 20 63 6f 6f 6b 69 65 20 76 65 72 69 66  the cookie verif
1ceb2 69 63 61 74 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20  ications and.** 
1ceb3 73 74 61 72 74 73 20 74 68 65 20 74 72 61 6e 73  starts the trans
1ceb4 61 63 74 69 6f 6e 73 20 77 69 6c 6c 20 62 65 20  actions will be 
1ceb5 63 6f 64 65 64 20 61 6e 64 20 74 68 65 20 4f 50  coded and the OP
1ceb6 5f 47 6f 74 6f 20 50 32 20 76 61 6c 75 65 0a 2a  _Goto P2 value.*
1ceb7 2a 20 77 69 6c 6c 20 62 65 20 6d 61 64 65 20 74  * will be made t
1ceb8 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20  o point to that 
1ceb9 73 75 62 72 6f 75 74 69 6e 65 2e 20 20 54 68 65  subroutine.  The
1ceba 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 66 20 74   generation of t
1cebb 68 65 0a 2a 2a 20 63 6f 6f 6b 69 65 20 76 65 72  he.** cookie ver
1cebc 69 66 69 63 61 74 69 6f 6e 20 73 75 62 72 6f 75  ification subrou
1cebd 74 69 6e 65 20 63 6f 64 65 20 68 61 70 70 65 6e  tine code happen
1cebe 73 20 69 6e 20 73 71 6c 69 74 65 33 46 69 6e 69  s in sqlite3Fini
1cebf 73 68 43 6f 64 69 6e 67 28 29 2e 0a 2a 2a 0a 2a  shCoding()..**.*
1cec0 2a 20 49 66 20 69 44 62 3c 30 20 74 68 65 6e 20  * If iDb<0 then 
1cec1 63 6f 64 65 20 74 68 65 20 4f 50 5f 47 6f 74 6f  code the OP_Goto
1cec2 20 6f 6e 6c 79 20 2d 20 64 6f 6e 27 74 20 73 65   only - don't se
1cec3 74 20 66 6c 61 67 20 74 6f 20 76 65 72 69 66 79  t flag to verify
1cec4 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 6f   the.** schema o
1cec5 6e 20 61 6e 79 20 64 61 74 61 62 61 73 65 73 2e  n any databases.
1cec6 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73    This can be us
1cec7 65 64 20 74 6f 20 70 6f 73 69 74 69 6f 6e 20 74  ed to position t
1cec8 68 65 20 4f 50 5f 47 6f 74 6f 0a 2a 2a 20 65 61  he OP_Goto.** ea
1cec9 72 6c 79 20 69 6e 20 74 68 65 20 63 6f 64 65 2c  rly in the code,
1ceca 20 62 65 66 6f 72 65 20 77 65 20 6b 6e 6f 77 20   before we know 
1cecb 69 66 20 61 6e 79 20 64 61 74 61 62 61 73 65 20  if any database 
1cecc 74 61 62 6c 65 73 20 77 69 6c 6c 20 62 65 20 75  tables will be u
1cecd 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  sed..*/.SQLITE_P
1cece 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1cecf 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68  te3CodeVerifySch
1ced0 65 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72 73  ema(Parse *pPars
1ced1 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 73  e, int iDb){.  s
1ced2 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64  qlite3 *db;.  Vd
1ced3 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 6d 61 73  be *v;.  int mas
1ced4 6b 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65  k;..  v = sqlite
1ced5 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
1ced6 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72  ;.  if( v==0 ) r
1ced7 65 74 75 72 6e 3b 20 20 2f 2a 20 54 68 69 73 20  eturn;  /* This 
1ced8 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 69 66 20  only happens if 
1ced9 74 68 65 72 65 20 77 61 73 20 61 20 70 72 69 6f  there was a prio
1ceda 72 20 65 72 72 6f 72 20 2a 2f 0a 20 20 64 62 20  r error */.  db 
1cedb 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
1cedc 69 66 28 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b  if( pParse->cook
1cedd 69 65 47 6f 74 6f 3d 3d 30 20 29 7b 0a 20 20 20  ieGoto==0 ){.   
1cede 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47   pParse->cookieG
1cedf 6f 74 6f 20 3d 20 73 71 6c 69 74 65 33 56 64 62  oto = sqlite3Vdb
1cee0 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
1cee1 74 6f 2c 20 30 2c 20 30 29 2b 31 3b 0a 20 20 7d  to, 0, 0)+1;.  }
1cee2 0a 20 20 69 66 28 20 69 44 62 3e 3d 30 20 29 7b  .  if( iDb>=0 ){
1cee3 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44 62  .    assert( iDb
1cee4 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 20 20  <db->nDb );.    
1cee5 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b  assert( db->aDb[
1cee6 69 44 62 5d 2e 70 42 74 21 3d 30 20 7c 7c 20 69  iDb].pBt!=0 || i
1cee7 44 62 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73  Db==1 );.    ass
1cee8 65 72 74 28 20 69 44 62 3c 53 51 4c 49 54 45 5f  ert( iDb<SQLITE_
1cee9 4d 41 58 5f 41 54 54 41 43 48 45 44 2b 32 20 29  MAX_ATTACHED+2 )
1ceea 3b 0a 20 20 20 20 6d 61 73 6b 20 3d 20 31 3c 3c  ;.    mask = 1<<
1ceeb 69 44 62 3b 0a 20 20 20 20 69 66 28 20 28 70 50  iDb;.    if( (pP
1ceec 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b  arse->cookieMask
1ceed 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29 7b 0a 20   & mask)==0 ){. 
1ceee 20 20 20 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f       pParse->coo
1ceef 6b 69 65 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b  kieMask |= mask;
1cef0 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 63  .      pParse->c
1cef1 6f 6f 6b 69 65 56 61 6c 75 65 5b 69 44 62 5d 20  ookieValue[iDb] 
1cef2 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  = db->aDb[iDb].p
1cef3 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63  Schema->schema_c
1cef4 6f 6f 6b 69 65 3b 0a 20 20 20 20 20 20 69 66 28  ookie;.      if(
1cef5 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26   !OMIT_TEMPDB &&
1cef6 20 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20   iDb==1 ){.     
1cef7 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 65     sqlite3OpenTe
1cef8 6d 70 44 61 74 61 62 61 73 65 28 70 50 61 72 73  mpDatabase(pPars
1cef9 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
1cefa 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  }.  }.}../*.** G
1cefb 65 6e 65 72 61 74 65 20 56 44 42 45 20 63 6f 64  enerate VDBE cod
1cefc 65 20 74 68 61 74 20 70 72 65 70 61 72 65 73 20  e that prepares 
1cefd 66 6f 72 20 64 6f 69 6e 67 20 61 6e 20 6f 70 65  for doing an ope
1cefe 72 61 74 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 6d  ration that.** m
1ceff 69 67 68 74 20 63 68 61 6e 67 65 20 74 68 65 20  ight change the 
1cf00 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  database..**.** 
1cf01 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74 61  This routine sta
1cf02 72 74 73 20 61 20 6e 65 77 20 74 72 61 6e 73 61  rts a new transa
1cf03 63 74 69 6f 6e 20 69 66 20 77 65 20 61 72 65 20  ction if we are 
1cf04 6e 6f 74 20 61 6c 72 65 61 64 79 20 77 69 74 68  not already with
1cf05 69 6e 0a 2a 2a 20 61 20 74 72 61 6e 73 61 63 74  in.** a transact
1cf06 69 6f 6e 2e 20 20 49 66 20 77 65 20 61 72 65 20  ion.  If we are 
1cf07 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 61  already within a
1cf08 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68   transaction, th
1cf09 65 6e 20 61 20 63 68 65 63 6b 70 6f 69 6e 74 0a  en a checkpoint.
1cf0a 2a 2a 20 69 73 20 73 65 74 20 69 66 20 74 68 65  ** is set if the
1cf0b 20 73 65 74 53 74 61 74 65 6d 65 6e 74 20 70 61   setStatement pa
1cf0c 72 61 6d 65 74 65 72 20 69 73 20 74 72 75 65 2e  rameter is true.
1cf0d 20 20 41 20 63 68 65 63 6b 70 6f 69 6e 74 20 73    A checkpoint s
1cf0e 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 73 65 74 20  hould.** be set 
1cf0f 66 6f 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 74  for operations t
1cf10 68 61 74 20 6d 69 67 68 74 20 66 61 69 6c 20 28  hat might fail (
1cf11 64 75 65 20 74 6f 20 61 20 63 6f 6e 73 74 72 61  due to a constra
1cf12 69 6e 74 29 20 70 61 72 74 20 6f 66 0a 2a 2a 20  int) part of.** 
1cf13 74 68 65 20 77 61 79 20 74 68 72 6f 75 67 68 20  the way through 
1cf14 61 6e 64 20 77 68 69 63 68 20 77 69 6c 6c 20 6e  and which will n
1cf15 65 65 64 20 74 6f 20 75 6e 64 6f 20 73 6f 6d 65  eed to undo some
1cf16 20 77 72 69 74 65 73 20 77 69 74 68 6f 75 74 20   writes without 
1cf17 68 61 76 69 6e 67 20 74 6f 0a 2a 2a 20 72 6f 6c  having to.** rol
1cf18 6c 62 61 63 6b 20 74 68 65 20 77 68 6f 6c 65 20  lback the whole 
1cf19 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 46 6f  transaction.  Fo
1cf1a 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 77 68 65  r operations whe
1cf1b 72 65 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e  re all constrain
1cf1c 74 73 0a 2a 2a 20 63 61 6e 20 62 65 20 63 68 65  ts.** can be che
1cf1d 63 6b 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20  cked before any 
1cf1e 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65  changes are made
1cf1f 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
1cf20 2c 20 69 74 20 69 73 20 6e 65 76 65 72 0a 2a 2a  , it is never.**
1cf21 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 75 6e   necessary to un
1cf22 64 6f 20 61 20 77 72 69 74 65 20 61 6e 64 20 74  do a write and t
1cf23 68 65 20 63 68 65 63 6b 70 6f 69 6e 74 20 73 68  he checkpoint sh
1cf24 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 65 74 2e  ould not be set.
1cf25 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 64 61 74 61  .**.** Only data
1cf26 62 61 73 65 20 69 44 62 20 61 6e 64 20 74 68 65  base iDb and the
1cf27 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 20 61   temp database a
1cf28 72 65 20 6d 61 64 65 20 77 72 69 74 61 62 6c 65  re made writable
1cf29 20 62 79 20 74 68 69 73 20 63 61 6c 6c 2e 0a 2a   by this call..*
1cf2a 2a 20 49 66 20 69 44 62 3d 3d 30 2c 20 74 68 65  * If iDb==0, the
1cf2b 6e 20 74 68 65 20 6d 61 69 6e 20 61 6e 64 20 74  n the main and t
1cf2c 65 6d 70 20 64 61 74 61 62 61 73 65 73 20 61 72  emp databases ar
1cf2d 65 20 6d 61 64 65 20 77 72 69 74 61 62 6c 65 2e  e made writable.
1cf2e 20 20 20 49 66 0a 2a 2a 20 69 44 62 3d 3d 31 20     If.** iDb==1 
1cf2f 74 68 65 6e 20 6f 6e 6c 79 20 74 68 65 20 74 65  then only the te
1cf30 6d 70 20 64 61 74 61 62 61 73 65 20 69 73 20 6d  mp database is m
1cf31 61 64 65 20 77 72 69 74 61 62 6c 65 2e 20 20 49  ade writable.  I
1cf32 66 20 69 44 62 3e 31 20 74 68 65 6e 20 74 68 65  f iDb>1 then the
1cf33 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 61 75  .** specified au
1cf34 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65  xiliary database
1cf35 20 61 6e 64 20 74 68 65 20 74 65 6d 70 20 64 61   and the temp da
1cf36 74 61 62 61 73 65 20 61 72 65 20 6d 61 64 65 20  tabase are made 
1cf37 77 72 69 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c  writable..*/.SQL
1cf38 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1cf39 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69   sqlite3BeginWri
1cf3a 74 65 4f 70 65 72 61 74 69 6f 6e 28 50 61 72 73  teOperation(Pars
1cf3b 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 73  e *pParse, int s
1cf3c 65 74 53 74 61 74 65 6d 65 6e 74 2c 20 69 6e 74  etStatement, int
1cf3d 20 69 44 62 29 7b 0a 20 20 56 64 62 65 20 2a 76   iDb){.  Vdbe *v
1cf3e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
1cf3f 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
1cf40 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   v==0 ) return;.
1cf41 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72    sqlite3CodeVer
1cf42 69 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 65  ifySchema(pParse
1cf43 2c 20 69 44 62 29 3b 0a 20 20 70 50 61 72 73 65  , iDb);.  pParse
1cf44 2d 3e 77 72 69 74 65 4d 61 73 6b 20 7c 3d 20 31  ->writeMask |= 1
1cf45 3c 3c 69 44 62 3b 0a 20 20 69 66 28 20 73 65 74  <<iDb;.  if( set
1cf46 53 74 61 74 65 6d 65 6e 74 20 26 26 20 70 50 61  Statement && pPa
1cf47 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29  rse->nested==0 )
1cf48 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1cf49 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 53 74  eAddOp1(v, OP_St
1cf4a 61 74 65 6d 65 6e 74 2c 20 69 44 62 29 3b 0a 20  atement, iDb);. 
1cf4b 20 7d 0a 20 20 69 66 28 20 28 4f 4d 49 54 5f 54   }.  if( (OMIT_T
1cf4c 45 4d 50 44 42 20 7c 7c 20 69 44 62 21 3d 31 29  EMPDB || iDb!=1)
1cf4d 20 26 26 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e   && pParse->db->
1cf4e 61 44 62 5b 31 5d 2e 70 42 74 21 3d 30 20 29 7b  aDb[1].pBt!=0 ){
1cf4f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69  .    sqlite3Begi
1cf50 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
1cf51 70 50 61 72 73 65 2c 20 73 65 74 53 74 61 74 65  pParse, setState
1cf52 6d 65 6e 74 2c 20 31 29 3b 0a 20 20 7d 0a 7d 0a  ment, 1);.  }.}.
1cf53 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
1cf54 73 65 65 20 69 66 20 70 49 6e 64 65 78 20 75 73  see if pIndex us
1cf55 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67  es the collating
1cf56 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 2e   sequence pColl.
1cf57 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 72 75 65    Return.** true
1cf58 20 69 66 20 69 74 20 64 6f 65 73 20 61 6e 64 20   if it does and 
1cf59 66 61 6c 73 65 20 69 66 20 69 74 20 64 6f 65 73  false if it does
1cf5a 20 6e 6f 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66   not..*/.#ifndef
1cf5b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49   SQLITE_OMIT_REI
1cf5c 4e 44 45 58 0a 73 74 61 74 69 63 20 69 6e 74 20  NDEX.static int 
1cf5d 63 6f 6c 6c 61 74 69 6f 6e 4d 61 74 63 68 28 63  collationMatch(c
1cf5e 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c  onst char *zColl
1cf5f 2c 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 29  , Index *pIndex)
1cf60 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72  {.  int i;.  for
1cf61 28 69 3d 30 3b 20 69 3c 70 49 6e 64 65 78 2d 3e  (i=0; i<pIndex->
1cf62 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20  nColumn; i++){. 
1cf63 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
1cf64 20 3d 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c   = pIndex->azCol
1cf65 6c 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 7a 3d  l[i];.    if( z=
1cf66 3d 7a 43 6f 6c 6c 20 7c 7c 20 28 7a 20 26 26 20  =zColl || (z && 
1cf67 7a 43 6f 6c 6c 20 26 26 20 30 3d 3d 73 71 6c 69  zColl && 0==sqli
1cf68 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 7a 43  te3StrICmp(z, zC
1cf69 6f 6c 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 72  oll)) ){.      r
1cf6a 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
1cf6b 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
1cf6c 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52  .#endif../*.** R
1cf6d 65 63 6f 6d 70 75 74 65 20 61 6c 6c 20 69 6e 64  ecompute all ind
1cf6e 69 63 65 73 20 6f 66 20 70 54 61 62 20 74 68 61  ices of pTab tha
1cf6f 74 20 75 73 65 20 74 68 65 20 63 6f 6c 6c 61 74  t use the collat
1cf70 69 6e 67 20 73 65 71 75 65 6e 63 65 20 70 43 6f  ing sequence pCo
1cf71 6c 6c 2e 0a 2a 2a 20 49 66 20 70 43 6f 6c 6c 3d  ll..** If pColl=
1cf72 3d 30 20 74 68 65 6e 20 72 65 63 6f 6d 70 75 74  =0 then recomput
1cf73 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66  e all indices of
1cf74 20 70 54 61 62 2e 0a 2a 2f 0a 23 69 66 6e 64 65   pTab..*/.#ifnde
1cf75 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45  f SQLITE_OMIT_RE
1cf76 49 4e 44 45 58 0a 73 74 61 74 69 63 20 76 6f 69  INDEX.static voi
1cf77 64 20 72 65 69 6e 64 65 78 54 61 62 6c 65 28 50  d reindexTable(P
1cf78 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61  arse *pParse, Ta
1cf79 62 6c 65 20 2a 70 54 61 62 2c 20 63 68 61 72 20  ble *pTab, char 
1cf7a 63 6f 6e 73 74 20 2a 7a 43 6f 6c 6c 29 7b 0a 20  const *zColl){. 
1cf7b 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 20   Index *pIndex; 
1cf7c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1cf7d 41 6e 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61  An index associa
1cf7e 74 65 64 20 77 69 74 68 20 70 54 61 62 20 2a 2f  ted with pTab */
1cf7f 0a 0a 20 20 66 6f 72 28 70 49 6e 64 65 78 3d 70  ..  for(pIndex=p
1cf80 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 6e  Tab->pIndex; pIn
1cf81 64 65 78 3b 20 70 49 6e 64 65 78 3d 70 49 6e 64  dex; pIndex=pInd
1cf82 65 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  ex->pNext){.    
1cf83 69 66 28 20 7a 43 6f 6c 6c 3d 3d 30 20 7c 7c 20  if( zColl==0 || 
1cf84 63 6f 6c 6c 61 74 69 6f 6e 4d 61 74 63 68 28 7a  collationMatch(z
1cf85 43 6f 6c 6c 2c 20 70 49 6e 64 65 78 29 20 29 7b  Coll, pIndex) ){
1cf86 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d  .      int iDb =
1cf87 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
1cf88 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62  Index(pParse->db
1cf89 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29  , pTab->pSchema)
1cf8a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  ;.      sqlite3B
1cf8b 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
1cf8c 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44  on(pParse, 0, iD
1cf8d 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
1cf8e 33 52 65 66 69 6c 6c 49 6e 64 65 78 28 70 50 61  3RefillIndex(pPa
1cf8f 72 73 65 2c 20 70 49 6e 64 65 78 2c 20 2d 31 29  rse, pIndex, -1)
1cf90 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65  ;.    }.  }.}.#e
1cf91 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f  ndif../*.** Reco
1cf92 6d 70 75 74 65 20 61 6c 6c 20 69 6e 64 69 63 65  mpute all indice
1cf93 73 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20  s of all tables 
1cf94 69 6e 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73  in all databases
1cf95 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20 69 6e   where the.** in
1cf96 64 69 63 65 73 20 75 73 65 20 74 68 65 20 63 6f  dices use the co
1cf97 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
1cf98 20 70 43 6f 6c 6c 2e 20 20 49 66 20 70 43 6f 6c   pColl.  If pCol
1cf99 6c 3d 3d 30 20 74 68 65 6e 20 72 65 63 6f 6d 70  l==0 then recomp
1cf9a 75 74 65 0a 2a 2a 20 61 6c 6c 20 69 6e 64 69 63  ute.** all indic
1cf9b 65 73 20 65 76 65 72 79 77 68 65 72 65 2e 0a 2a  es everywhere..*
1cf9c 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1cf9d 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a 73 74  _OMIT_REINDEX.st
1cf9e 61 74 69 63 20 76 6f 69 64 20 72 65 69 6e 64 65  atic void reinde
1cf9f 78 44 61 74 61 62 61 73 65 73 28 50 61 72 73 65  xDatabases(Parse
1cfa0 20 2a 70 50 61 72 73 65 2c 20 63 68 61 72 20 63   *pParse, char c
1cfa1 6f 6e 73 74 20 2a 7a 43 6f 6c 6c 29 7b 0a 20 20  onst *zColl){.  
1cfa2 44 62 20 2a 70 44 62 3b 20 20 20 20 20 20 20 20  Db *pDb;        
1cfa3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
1cfa4 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65   single database
1cfa5 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20   */.  int iDb;  
1cfa6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cfa7 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
1cfa8 65 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 20 2a  e index number *
1cfa9 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
1cfaa 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20  = pParse->db;   
1cfab 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
1cfac 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
1cfad 48 61 73 68 45 6c 65 6d 20 2a 6b 3b 20 20 20 20  HashElem *k;    
1cfae 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
1cfaf 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
1cfb0 74 61 62 6c 65 73 20 69 6e 20 70 44 62 20 2a 2f  tables in pDb */
1cfb1 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20  .  Table *pTab; 
1cfb2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1cfb3 2a 20 41 20 74 61 62 6c 65 20 69 6e 20 74 68 65  * A table in the
1cfb4 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 0a 20 20   database */..  
1cfb5 66 6f 72 28 69 44 62 3d 30 2c 20 70 44 62 3d 64  for(iDb=0, pDb=d
1cfb6 62 2d 3e 61 44 62 3b 20 69 44 62 3c 64 62 2d 3e  b->aDb; iDb<db->
1cfb7 6e 44 62 3b 20 69 44 62 2b 2b 2c 20 70 44 62 2b  nDb; iDb++, pDb+
1cfb8 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  +){.    assert( 
1cfb9 70 44 62 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f  pDb!=0 );.    fo
1cfba 72 28 6b 3d 73 71 6c 69 74 65 48 61 73 68 46 69  r(k=sqliteHashFi
1cfbb 72 73 74 28 26 70 44 62 2d 3e 70 53 63 68 65 6d  rst(&pDb->pSchem
1cfbc 61 2d 3e 74 62 6c 48 61 73 68 29 3b 20 20 6b 3b  a->tblHash);  k;
1cfbd 20 6b 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78   k=sqliteHashNex
1cfbe 74 28 6b 29 29 7b 0a 20 20 20 20 20 20 70 54 61  t(k)){.      pTa
1cfbf 62 20 3d 20 28 54 61 62 6c 65 2a 29 73 71 6c 69  b = (Table*)sqli
1cfc0 74 65 48 61 73 68 44 61 74 61 28 6b 29 3b 0a 20  teHashData(k);. 
1cfc1 20 20 20 20 20 72 65 69 6e 64 65 78 54 61 62 6c       reindexTabl
1cfc2 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  e(pParse, pTab, 
1cfc3 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20  zColl);.    }.  
1cfc4 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  }.}.#endif../*.*
1cfc5 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
1cfc6 66 6f 72 20 74 68 65 20 52 45 49 4e 44 45 58 20  for the REINDEX 
1cfc7 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20  command..**.**  
1cfc8 20 20 20 20 20 20 52 45 49 4e 44 45 58 20 20 20        REINDEX   
1cfc9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cfca 20 20 20 20 20 20 20 20 20 2d 2d 20 31 0a 2a 2a           -- 1.**
1cfcb 20 20 20 20 20 20 20 20 52 45 49 4e 44 45 58 20          REINDEX 
1cfcc 20 3c 63 6f 6c 6c 61 74 69 6f 6e 3e 20 20 20 20   <collation>    
1cfcd 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 32 0a             -- 2.
1cfce 2a 2a 20 20 20 20 20 20 20 20 52 45 49 4e 44 45  **        REINDE
1cfcf 58 20 20 3f 3c 64 61 74 61 62 61 73 65 3e 2e 3f  X  ?<database>.?
1cfd0 3c 74 61 62 6c 65 6e 61 6d 65 3e 20 20 2d 2d 20  <tablename>  -- 
1cfd1 33 0a 2a 2a 20 20 20 20 20 20 20 20 52 45 49 4e  3.**        REIN
1cfd2 44 45 58 20 20 3f 3c 64 61 74 61 62 61 73 65 3e  DEX  ?<database>
1cfd3 2e 3f 3c 69 6e 64 65 78 6e 61 6d 65 3e 20 20 2d  .?<indexname>  -
1cfd4 2d 20 34 0a 2a 2a 0a 2a 2a 20 46 6f 72 6d 20 31  - 4.**.** Form 1
1cfd5 20 63 61 75 73 65 73 20 61 6c 6c 20 69 6e 64 69   causes all indi
1cfd6 63 65 73 20 69 6e 20 61 6c 6c 20 61 74 74 61 63  ces in all attac
1cfd7 68 65 64 20 64 61 74 61 62 61 73 65 73 20 74 6f  hed databases to
1cfd8 20 62 65 20 72 65 62 75 69 6c 74 2e 0a 2a 2a 20   be rebuilt..** 
1cfd9 46 6f 72 6d 20 32 20 72 65 62 75 69 6c 64 73 20  Form 2 rebuilds 
1cfda 61 6c 6c 20 69 6e 64 69 63 65 73 20 69 6e 20 61  all indices in a
1cfdb 6c 6c 20 64 61 74 61 62 61 73 65 73 20 74 68 61  ll databases tha
1cfdc 74 20 75 73 65 20 74 68 65 20 6e 61 6d 65 64 0a  t use the named.
1cfdd 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e  ** collating fun
1cfde 63 74 69 6f 6e 2e 20 20 46 6f 72 6d 73 20 33 20  ction.  Forms 3 
1cfdf 61 6e 64 20 34 20 72 65 62 75 69 6c 64 20 74 68  and 4 rebuild th
1cfe0 65 20 6e 61 6d 65 64 20 69 6e 64 65 78 20 6f 72  e named index or
1cfe1 20 61 6c 6c 0a 2a 2a 20 69 6e 64 69 63 65 73 20   all.** indices 
1cfe2 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
1cfe3 74 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e  the named table.
1cfe4 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
1cfe5 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a  TE_OMIT_REINDEX.
1cfe6 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1cfe7 6f 69 64 20 73 71 6c 69 74 65 33 52 65 69 6e 64  oid sqlite3Reind
1cfe8 65 78 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ex(Parse *pParse
1cfe9 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c  , Token *pName1,
1cfea 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 29 7b   Token *pName2){
1cfeb 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  .  CollSeq *pCol
1cfec 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  l;             /
1cfed 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  * Collating sequ
1cfee 65 6e 63 65 20 74 6f 20 62 65 20 72 65 69 6e 64  ence to be reind
1cfef 65 78 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f  exed, or NULL */
1cff0 0a 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20  .  char *z;     
1cff1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1cff2 2a 20 4e 61 6d 65 20 6f 66 20 61 20 74 61 62 6c  * Name of a tabl
1cff3 65 20 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20  e or index */.  
1cff4 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b  const char *zDb;
1cff5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1cff6 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
1cff7 61 73 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ase */.  Table *
1cff8 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20  pTab;           
1cff9 20 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20       /* A table 
1cffa 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
1cffb 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64  */.  Index *pInd
1cffc 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ex;             
1cffd 20 2f 2a 20 41 6e 20 69 6e 64 65 78 20 61 73 73   /* An index ass
1cffe 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 54 61  ociated with pTa
1cfff 62 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  b */.  int iDb; 
1d000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d001 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
1d002 73 65 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 20  se index number 
1d003 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
1d004 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20   = pParse->db;  
1d005 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
1d006 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
1d007 20 54 6f 6b 65 6e 20 2a 70 4f 62 6a 4e 61 6d 65   Token *pObjName
1d008 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
1d009 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  Name of the tabl
1d00a 65 20 6f 72 20 69 6e 64 65 78 20 74 6f 20 62 65  e or index to be
1d00b 20 72 65 69 6e 64 65 78 65 64 20 2a 2f 0a 0a 20   reindexed */.. 
1d00c 20 2f 2a 20 52 65 61 64 20 74 68 65 20 64 61 74   /* Read the dat
1d00d 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 49 66  abase schema. If
1d00e 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
1d00f 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72  , leave an error
1d010 20 6d 65 73 73 61 67 65 0a 20 20 2a 2a 20 61 6e   message.  ** an
1d011 64 20 63 6f 64 65 20 69 6e 20 70 50 61 72 73 65  d code in pParse
1d012 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c   and return NULL
1d013 2e 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54  . */.  if( SQLIT
1d014 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61  E_OK!=sqlite3Rea
1d015 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
1d016 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
1d017 20 7d 0a 0a 20 20 69 66 28 20 70 4e 61 6d 65 31   }..  if( pName1
1d018 3d 3d 30 20 7c 7c 20 70 4e 61 6d 65 31 2d 3e 7a  ==0 || pName1->z
1d019 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 69 6e 64  ==0 ){.    reind
1d01a 65 78 44 61 74 61 62 61 73 65 73 28 70 50 61 72  exDatabases(pPar
1d01b 73 65 2c 20 30 29 3b 0a 20 20 20 20 72 65 74 75  se, 0);.    retu
1d01c 72 6e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  rn;.  }else if( 
1d01d 70 4e 61 6d 65 32 3d 3d 30 20 7c 7c 20 70 4e 61  pName2==0 || pNa
1d01e 6d 65 32 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20  me2->z==0 ){.   
1d01f 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 0a 20 20   char *zColl;.  
1d020 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 31    assert( pName1
1d021 2d 3e 7a 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 6c  ->z );.    zColl
1d022 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
1d023 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d 3e  omToken(pParse->
1d024 64 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20  db, pName1);.   
1d025 20 69 66 28 20 21 7a 43 6f 6c 6c 20 29 20 72 65   if( !zColl ) re
1d026 74 75 72 6e 3b 0a 20 20 20 20 70 43 6f 6c 6c 20  turn;.    pColl 
1d027 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c  = sqlite3FindCol
1d028 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64 62 29  lSeq(db, ENC(db)
1d029 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 2c 20 30 29 3b  , zColl, -1, 0);
1d02a 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29  .    if( pColl )
1d02b 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 43 6f 6c  {.      if( zCol
1d02c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 69  l ){.        rei
1d02d 6e 64 65 78 44 61 74 61 62 61 73 65 73 28 70 50  ndexDatabases(pP
1d02e 61 72 73 65 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20  arse, zColl);.  
1d02f 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
1d030 65 65 28 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20  ee(zColl);.     
1d031 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b   }.      return;
1d032 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
1d033 65 33 5f 66 72 65 65 28 7a 43 6f 6c 6c 29 3b 0a  e3_free(zColl);.
1d034 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69    }.  iDb = sqli
1d035 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70  te3TwoPartName(p
1d036 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70  Parse, pName1, p
1d037 4e 61 6d 65 32 2c 20 26 70 4f 62 6a 4e 61 6d 65  Name2, &pObjName
1d038 29 3b 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29  );.  if( iDb<0 )
1d039 20 72 65 74 75 72 6e 3b 0a 20 20 7a 20 3d 20 73   return;.  z = s
1d03a 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
1d03b 6b 65 6e 28 64 62 2c 20 70 4f 62 6a 4e 61 6d 65  ken(db, pObjName
1d03c 29 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 20  );.  if( z==0 ) 
1d03d 72 65 74 75 72 6e 3b 0a 20 20 7a 44 62 20 3d 20  return;.  zDb = 
1d03e 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
1d03f 6d 65 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c  me;.  pTab = sql
1d040 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62  ite3FindTable(db
1d041 2c 20 7a 2c 20 7a 44 62 29 3b 0a 20 20 69 66 28  , z, zDb);.  if(
1d042 20 70 54 61 62 20 29 7b 0a 20 20 20 20 72 65 69   pTab ){.    rei
1d043 6e 64 65 78 54 61 62 6c 65 28 70 50 61 72 73 65  ndexTable(pParse
1d044 2c 20 70 54 61 62 2c 20 30 29 3b 0a 20 20 20 20  , pTab, 0);.    
1d045 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 29 3b  sqlite3_free(z);
1d046 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
1d047 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 71 6c 69  .  pIndex = sqli
1d048 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c  te3FindIndex(db,
1d049 20 7a 2c 20 7a 44 62 29 3b 0a 20 20 73 71 6c 69   z, zDb);.  sqli
1d04a 74 65 33 5f 66 72 65 65 28 7a 29 3b 0a 20 20 69  te3_free(z);.  i
1d04b 66 28 20 70 49 6e 64 65 78 20 29 7b 0a 20 20 20  f( pIndex ){.   
1d04c 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69   sqlite3BeginWri
1d04d 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72  teOperation(pPar
1d04e 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20  se, 0, iDb);.   
1d04f 20 73 71 6c 69 74 65 33 52 65 66 69 6c 6c 49 6e   sqlite3RefillIn
1d050 64 65 78 28 70 50 61 72 73 65 2c 20 70 49 6e 64  dex(pParse, pInd
1d051 65 78 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65 74  ex, -1);.    ret
1d052 75 72 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  urn;.  }.  sqlit
1d053 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1d054 65 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20 69 64  e, "unable to id
1d055 65 6e 74 69 66 79 20 74 68 65 20 6f 62 6a 65 63  entify the objec
1d056 74 20 74 6f 20 62 65 20 72 65 69 6e 64 65 78 65  t to be reindexe
1d057 64 22 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  d");.}.#endif../
1d058 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 64 79  *.** Return a dy
1d059 6e 61 6d 69 63 6c 79 20 61 6c 6c 6f 63 61 74 65  namicly allocate
1d05a 64 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74  d KeyInfo struct
1d05b 75 72 65 20 74 68 61 74 20 63 61 6e 20 62 65 20  ure that can be 
1d05c 75 73 65 64 0a 2a 2a 20 77 69 74 68 20 4f 50 5f  used.** with OP_
1d05d 4f 70 65 6e 52 65 61 64 20 6f 72 20 4f 50 5f 4f  OpenRead or OP_O
1d05e 70 65 6e 57 72 69 74 65 20 74 6f 20 61 63 63 65  penWrite to acce
1d05f 73 73 20 64 61 74 61 62 61 73 65 20 69 6e 64 65  ss database inde
1d060 78 20 70 49 64 78 2e 0a 2a 2a 0a 2a 2a 20 49 66  x pIdx..**.** If
1d061 20 73 75 63 63 65 73 73 66 75 6c 2c 20 61 20 70   successful, a p
1d062 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65  ointer to the ne
1d063 77 20 73 74 72 75 63 74 75 72 65 20 69 73 20 72  w structure is r
1d064 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73  eturned. In this
1d065 20 63 61 73 65 0a 2a 2a 20 74 68 65 20 63 61 6c   case.** the cal
1d066 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62  ler is responsib
1d067 6c 65 20 66 6f 72 20 63 61 6c 6c 69 6e 67 20 73  le for calling s
1d068 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 6f 6e  qlite3_free() on
1d069 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 0a 2a   the returned .*
1d06a 2a 20 70 6f 69 6e 74 65 72 2e 20 49 66 20 61 6e  * pointer. If an
1d06b 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 28 6f   error occurs (o
1d06c 75 74 20 6f 66 20 6d 65 6d 6f 72 79 20 6f 72 20  ut of memory or 
1d06d 6d 69 73 73 69 6e 67 20 63 6f 6c 6c 61 74 69 6f  missing collatio
1d06e 6e 20 0a 2a 2a 20 73 65 71 75 65 6e 63 65 29 2c  n .** sequence),
1d06f 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65   NULL is returne
1d070 64 20 61 6e 64 20 74 68 65 20 73 74 61 74 65 20  d and the state 
1d071 6f 66 20 70 50 61 72 73 65 20 75 70 64 61 74 65  of pParse update
1d072 64 20 74 6f 20 72 65 66 6c 65 63 74 0a 2a 2a 20  d to reflect.** 
1d073 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51  the error..*/.SQ
1d074 4c 49 54 45 5f 50 52 49 56 41 54 45 20 4b 65 79  LITE_PRIVATE Key
1d075 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 49 6e 64  Info *sqlite3Ind
1d076 65 78 4b 65 79 69 6e 66 6f 28 50 61 72 73 65 20  exKeyinfo(Parse 
1d077 2a 70 50 61 72 73 65 2c 20 49 6e 64 65 78 20 2a  *pParse, Index *
1d078 70 49 64 78 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  pIdx){.  int i;.
1d079 20 20 69 6e 74 20 6e 43 6f 6c 20 3d 20 70 49 64    int nCol = pId
1d07a 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69 6e  x->nColumn;.  in
1d07b 74 20 6e 42 79 74 65 73 20 3d 20 73 69 7a 65 6f  t nBytes = sizeo
1d07c 66 28 4b 65 79 49 6e 66 6f 29 20 2b 20 28 6e 43  f(KeyInfo) + (nC
1d07d 6f 6c 2d 31 29 2a 73 69 7a 65 6f 66 28 43 6f 6c  ol-1)*sizeof(Col
1d07e 6c 53 65 71 2a 29 20 2b 20 6e 43 6f 6c 3b 0a 20  lSeq*) + nCol;. 
1d07f 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d   KeyInfo *pKey =
1d080 20 28 4b 65 79 49 6e 66 6f 20 2a 29 73 71 6c 69   (KeyInfo *)sqli
1d081 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
1d082 70 50 61 72 73 65 2d 3e 64 62 2c 20 6e 42 79 74  pParse->db, nByt
1d083 65 73 29 3b 0a 0a 20 20 69 66 28 20 70 4b 65 79  es);..  if( pKey
1d084 20 29 7b 0a 20 20 20 20 70 4b 65 79 2d 3e 64 62   ){.    pKey->db
1d085 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
1d086 20 20 20 70 4b 65 79 2d 3e 61 53 6f 72 74 4f 72     pKey->aSortOr
1d087 64 65 72 20 3d 20 28 75 38 20 2a 29 26 28 70 4b  der = (u8 *)&(pK
1d088 65 79 2d 3e 61 43 6f 6c 6c 5b 6e 43 6f 6c 5d 29  ey->aColl[nCol])
1d089 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 26 70  ;.    assert( &p
1d08a 4b 65 79 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b  Key->aSortOrder[
1d08b 6e 43 6f 6c 5d 3d 3d 26 28 28 28 75 38 20 2a 29  nCol]==&(((u8 *)
1d08c 70 4b 65 79 29 5b 6e 42 79 74 65 73 5d 29 20 29  pKey)[nBytes]) )
1d08d 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
1d08e 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nCol; i++){.   
1d08f 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 20 3d     char *zColl =
1d090 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d   pIdx->azColl[i]
1d091 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1d092 7a 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 20 20 70  zColl );.      p
1d093 4b 65 79 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3d 20  Key->aColl[i] = 
1d094 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c  sqlite3LocateCol
1d095 6c 53 65 71 28 70 50 61 72 73 65 2c 20 7a 43 6f  lSeq(pParse, zCo
1d096 6c 6c 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 70  ll, -1);.      p
1d097 4b 65 79 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b  Key->aSortOrder[
1d098 69 5d 20 3d 20 70 49 64 78 2d 3e 61 53 6f 72 74  i] = pIdx->aSort
1d099 4f 72 64 65 72 5b 69 5d 3b 0a 20 20 20 20 7d 0a  Order[i];.    }.
1d09a 20 20 20 20 70 4b 65 79 2d 3e 6e 46 69 65 6c 64      pKey->nField
1d09b 20 3d 20 6e 43 6f 6c 3b 0a 20 20 7d 0a 0a 20 20   = nCol;.  }..  
1d09c 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
1d09d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
1d09e 66 72 65 65 28 70 4b 65 79 29 3b 0a 20 20 20 20  free(pKey);.    
1d09f 70 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  pKey = 0;.  }.  
1d0a0 72 65 74 75 72 6e 20 70 4b 65 79 3b 0a 7d 0a 0a  return pKey;.}..
1d0a1 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1d0a2 45 6e 64 20 6f 66 20 62 75 69 6c 64 2e 63 20 2a  End of build.c *
1d0a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d0a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d0a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1d0a6 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1d0a7 42 65 67 69 6e 20 66 69 6c 65 20 63 61 6c 6c 62  Begin file callb
1d0a8 61 63 6b 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ack.c **********
1d0a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d0aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1d0ab 2f 2a 0a 2a 2a 20 32 30 30 35 20 4d 61 79 20 32  /*.** 2005 May 2
1d0ac 33 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  3 .**.** The aut
1d0ad 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
1d0ae 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
1d0af 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
1d0b0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
1d0b1 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
1d0b2 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
1d0b3 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
1d0b4 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
1d0b5 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
1d0b6 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
1d0b7 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
1d0b8 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
1d0b9 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
1d0ba 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
1d0bb 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
1d0bc 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
1d0bd 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
1d0be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d0bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d0c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d0c2 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
1d0c3 65 20 63 6f 6e 74 61 69 6e 73 20 66 75 6e 63 74  e contains funct
1d0c4 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 61 63 63  ions used to acc
1d0c5 65 73 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  ess the internal
1d0c6 20 68 61 73 68 20 74 61 62 6c 65 73 0a 2a 2a 20   hash tables.** 
1d0c7 6f 66 20 75 73 65 72 20 64 65 66 69 6e 65 64 20  of user defined 
1d0c8 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 63 6f  functions and co
1d0c9 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
1d0ca 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 63 61  s..**.** $Id: ca
1d0cb 6c 6c 62 61 63 6b 2e 63 2c 76 20 31 2e 32 33 20  llback.c,v 1.23 
1d0cc 32 30 30 37 2f 30 38 2f 32 39 20 31 32 3a 33 31  2007/08/29 12:31
1d0cd 3a 32 36 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :26 danielk1977 
1d0ce 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a  Exp $.*/.../*.**
1d0cf 20 49 6e 76 6f 6b 65 20 74 68 65 20 27 63 6f 6c   Invoke the 'col
1d0d0 6c 61 74 69 6f 6e 20 6e 65 65 64 65 64 27 20 63  lation needed' c
1d0d1 61 6c 6c 62 61 63 6b 20 74 6f 20 72 65 71 75 65  allback to reque
1d0d2 73 74 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  st a collation s
1d0d3 65 71 75 65 6e 63 65 0a 2a 2a 20 69 6e 20 74 68  equence.** in th
1d0d4 65 20 64 61 74 61 62 61 73 65 20 74 65 78 74 20  e database text 
1d0d5 65 6e 63 6f 64 69 6e 67 20 6f 66 20 6e 61 6d 65  encoding of name
1d0d6 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e   zName, length n
1d0d7 4e 61 6d 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20  Name..** If the 
1d0d8 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
1d0d9 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ce.*/.static voi
1d0da 64 20 63 61 6c 6c 43 6f 6c 6c 4e 65 65 64 65 64  d callCollNeeded
1d0db 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
1d0dc 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
1d0dd 20 69 6e 74 20 6e 4e 61 6d 65 29 7b 0a 20 20 61   int nName){.  a
1d0de 73 73 65 72 74 28 20 21 64 62 2d 3e 78 43 6f 6c  ssert( !db->xCol
1d0df 6c 4e 65 65 64 65 64 20 7c 7c 20 21 64 62 2d 3e  lNeeded || !db->
1d0e0 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 20 29 3b  xCollNeeded16 );
1d0e1 0a 20 20 69 66 28 20 6e 4e 61 6d 65 3c 30 20 29  .  if( nName<0 )
1d0e2 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28   nName = strlen(
1d0e3 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 64 62  zName);.  if( db
1d0e4 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20 29 7b  ->xCollNeeded ){
1d0e5 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 78 74 65  .    char *zExte
1d0e6 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 44 62  rnal = sqlite3Db
1d0e7 53 74 72 4e 44 75 70 28 64 62 2c 20 7a 4e 61 6d  StrNDup(db, zNam
1d0e8 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 69  e, nName);.    i
1d0e9 66 28 20 21 7a 45 78 74 65 72 6e 61 6c 20 29 20  f( !zExternal ) 
1d0ea 72 65 74 75 72 6e 3b 0a 20 20 20 20 64 62 2d 3e  return;.    db->
1d0eb 78 43 6f 6c 6c 4e 65 65 64 65 64 28 64 62 2d 3e  xCollNeeded(db->
1d0ec 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 2c 20  pCollNeededArg, 
1d0ed 64 62 2c 20 28 69 6e 74 29 45 4e 43 28 64 62 29  db, (int)ENC(db)
1d0ee 2c 20 7a 45 78 74 65 72 6e 61 6c 29 3b 0a 20 20  , zExternal);.  
1d0ef 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
1d0f0 45 78 74 65 72 6e 61 6c 29 3b 0a 20 20 7d 0a 23  External);.  }.#
1d0f1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1d0f2 49 54 5f 55 54 46 31 36 0a 20 20 69 66 28 20 64  IT_UTF16.  if( d
1d0f3 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36  b->xCollNeeded16
1d0f4 20 29 7b 0a 20 20 20 20 63 68 61 72 20 63 6f 6e   ){.    char con
1d0f5 73 74 20 2a 7a 45 78 74 65 72 6e 61 6c 3b 0a 20  st *zExternal;. 
1d0f6 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65     sqlite3_value
1d0f7 20 2a 70 54 6d 70 20 3d 20 73 71 6c 69 74 65 33   *pTmp = sqlite3
1d0f8 56 61 6c 75 65 4e 65 77 28 64 62 29 3b 0a 20 20  ValueNew(db);.  
1d0f9 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65    sqlite3ValueSe
1d0fa 74 53 74 72 28 70 54 6d 70 2c 20 6e 4e 61 6d 65  tStr(pTmp, nName
1d0fb 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c 49 54 45 5f  , zName, SQLITE_
1d0fc 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41  UTF8, SQLITE_STA
1d0fd 54 49 43 29 3b 0a 20 20 20 20 7a 45 78 74 65 72  TIC);.    zExter
1d0fe 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c  nal = sqlite3Val
1d0ff 75 65 54 65 78 74 28 70 54 6d 70 2c 20 53 51 4c  ueText(pTmp, SQL
1d100 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 29  ITE_UTF16NATIVE)
1d101 3b 0a 20 20 20 20 69 66 28 20 7a 45 78 74 65 72  ;.    if( zExter
1d102 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 64 62 2d  nal ){.      db-
1d103 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 28 64  >xCollNeeded16(d
1d104 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72  b->pCollNeededAr
1d105 67 2c 20 64 62 2c 20 28 69 6e 74 29 45 4e 43 28  g, db, (int)ENC(
1d106 64 62 29 2c 20 7a 45 78 74 65 72 6e 61 6c 29 3b  db), zExternal);
1d107 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
1d108 65 33 56 61 6c 75 65 46 72 65 65 28 70 54 6d 70  e3ValueFree(pTmp
1d109 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a  );.  }.#endif.}.
1d10a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
1d10b 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 69 66  ine is called if
1d10c 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66   the collation f
1d10d 61 63 74 6f 72 79 20 66 61 69 6c 73 20 74 6f 20  actory fails to 
1d10e 64 65 6c 69 76 65 72 20 61 0a 2a 2a 20 63 6f 6c  deliver a.** col
1d10f 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  lation function 
1d110 69 6e 20 74 68 65 20 62 65 73 74 20 65 6e 63 6f  in the best enco
1d111 64 69 6e 67 20 62 75 74 20 74 68 65 72 65 20 6d  ding but there m
1d112 61 79 20 62 65 20 6f 74 68 65 72 20 76 65 72 73  ay be other vers
1d113 69 6f 6e 73 0a 2a 2a 20 6f 66 20 74 68 69 73 20  ions.** of this 
1d114 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69  collation functi
1d115 6f 6e 20 28 66 6f 72 20 6f 74 68 65 72 20 74 65  on (for other te
1d116 78 74 20 65 6e 63 6f 64 69 6e 67 73 29 20 61 76  xt encodings) av
1d117 61 69 6c 61 62 6c 65 2e 20 55 73 65 20 6f 6e 65  ailable. Use one
1d118 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20 69 6e 73  .** of these ins
1d119 74 65 61 64 20 69 66 20 74 68 65 79 20 65 78 69  tead if they exi
1d11a 73 74 2e 20 41 76 6f 69 64 20 61 20 55 54 46 2d  st. Avoid a UTF-
1d11b 38 20 3c 2d 3e 20 55 54 46 2d 31 36 20 63 6f 6e  8 <-> UTF-16 con
1d11c 76 65 72 73 69 6f 6e 20 69 66 0a 2a 2a 20 70 6f  version if.** po
1d11d 73 73 69 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  ssible..*/.stati
1d11e 63 20 69 6e 74 20 73 79 6e 74 68 43 6f 6c 6c 53  c int synthCollS
1d11f 65 71 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  eq(sqlite3 *db, 
1d120 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 29 7b  CollSeq *pColl){
1d121 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  .  CollSeq *pCol
1d122 6c 32 3b 0a 20 20 63 68 61 72 20 2a 7a 20 3d 20  l2;.  char *z = 
1d123 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20  pColl->zName;.  
1d124 69 6e 74 20 6e 20 3d 20 73 74 72 6c 65 6e 28 7a  int n = strlen(z
1d125 29 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74  );.  int i;.  st
1d126 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 61 45  atic const u8 aE
1d127 6e 63 5b 5d 20 3d 20 7b 20 53 51 4c 49 54 45 5f  nc[] = { SQLITE_
1d128 55 54 46 31 36 42 45 2c 20 53 51 4c 49 54 45 5f  UTF16BE, SQLITE_
1d129 55 54 46 31 36 4c 45 2c 20 53 51 4c 49 54 45 5f  UTF16LE, SQLITE_
1d12a 55 54 46 38 20 7d 3b 0a 20 20 66 6f 72 28 69 3d  UTF8 };.  for(i=
1d12b 30 3b 20 69 3c 33 3b 20 69 2b 2b 29 7b 0a 20 20  0; i<3; i++){.  
1d12c 20 20 70 43 6f 6c 6c 32 20 3d 20 73 71 6c 69 74    pColl2 = sqlit
1d12d 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62  e3FindCollSeq(db
1d12e 2c 20 61 45 6e 63 5b 69 5d 2c 20 7a 2c 20 6e 2c  , aEnc[i], z, n,
1d12f 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 43 6f   0);.    if( pCo
1d130 6c 6c 32 2d 3e 78 43 6d 70 21 3d 30 20 29 7b 0a  ll2->xCmp!=0 ){.
1d131 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 43 6f        memcpy(pCo
1d132 6c 6c 2c 20 70 43 6f 6c 6c 32 2c 20 73 69 7a 65  ll, pColl2, size
1d133 6f 66 28 43 6f 6c 6c 53 65 71 29 29 3b 0a 20 20  of(CollSeq));.  
1d134 20 20 20 20 70 43 6f 6c 6c 2d 3e 78 44 65 6c 20      pColl->xDel 
1d135 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  = 0;         /* 
1d136 44 6f 20 6e 6f 74 20 63 6f 70 79 20 74 68 65 20  Do not copy the 
1d137 64 65 73 74 72 75 63 74 6f 72 20 2a 2f 0a 20 20  destructor */.  
1d138 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1d139 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  E_OK;.    }.  }.
1d13a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1d13b 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ERROR;.}../*.** 
1d13c 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
1d13d 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72   responsible for
1d13e 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 6f   invoking the co
1d13f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 20  llation factory 
1d140 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 72 20 73  callback.** or s
1d141 75 62 73 74 69 74 75 74 69 6e 67 20 61 20 63 6f  ubstituting a co
1d142 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
1d143 20 6f 66 20 61 20 64 69 66 66 65 72 65 6e 74 20   of a different 
1d144 65 6e 63 6f 64 69 6e 67 20 77 68 65 6e 20 74 68  encoding when th
1d145 65 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 20 63  e.** requested c
1d146 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
1d147 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62  e is not availab
1d148 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  le in the databa
1d149 73 65 20 6e 61 74 69 76 65 0a 2a 2a 20 65 6e 63  se native.** enc
1d14a 6f 64 69 6e 67 2e 0a 2a 2a 20 0a 2a 2a 20 49 66  oding..** .** If
1d14b 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c   it is not NULL,
1d14c 20 74 68 65 6e 20 70 43 6f 6c 6c 20 6d 75 73 74   then pColl must
1d14d 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 64 61   point to the da
1d14e 74 61 62 61 73 65 20 6e 61 74 69 76 65 20 65 6e  tabase native en
1d14f 63 6f 64 69 6e 67 20 0a 2a 2a 20 63 6f 6c 6c 61  coding .** colla
1d150 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69  tion sequence wi
1d151 74 68 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20 6c  th name zName, l
1d152 65 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 0a  ength nName..**.
1d153 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  ** The return va
1d154 6c 75 65 20 69 73 20 65 69 74 68 65 72 20 74 68  lue is either th
1d155 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  e collation sequ
1d156 65 6e 63 65 20 74 6f 20 62 65 20 75 73 65 64 20  ence to be used 
1d157 69 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 64  in database.** d
1d158 62 20 66 6f 72 20 63 6f 6c 6c 61 74 69 6f 6e 20  b for collation 
1d159 74 79 70 65 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c  type name zName,
1d15a 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2c 20 6f   length nName, o
1d15b 72 20 4e 55 4c 4c 2c 20 69 66 20 6e 6f 20 63 6f  r NULL, if no co
1d15c 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65  llation.** seque
1d15d 6e 63 65 20 63 61 6e 20 62 65 20 66 6f 75 6e 64  nce can be found
1d15e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1d15f 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c  ATE CollSeq *sql
1d160 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 0a  ite3GetCollSeq(.
1d161 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 0a    sqlite3* db, .
1d162 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
1d163 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
1d164 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20 6e  *zName, .  int n
1d165 4e 61 6d 65 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65  Name.){.  CollSe
1d166 71 20 2a 70 3b 0a 0a 20 20 70 20 3d 20 70 43 6f  q *p;..  p = pCo
1d167 6c 6c 3b 0a 20 20 69 66 28 20 21 70 20 29 7b 0a  ll;.  if( !p ){.
1d168 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 46      p = sqlite3F
1d169 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45  indCollSeq(db, E
1d16a 4e 43 28 64 62 29 2c 20 7a 4e 61 6d 65 2c 20 6e  NC(db), zName, n
1d16b 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  Name, 0);.  }.  
1d16c 69 66 28 20 21 70 20 7c 7c 20 21 70 2d 3e 78 43  if( !p || !p->xC
1d16d 6d 70 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 20  mp ){.    /* No 
1d16e 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
1d16f 63 65 20 6f 66 20 74 68 69 73 20 74 79 70 65 20  ce of this type 
1d170 66 6f 72 20 74 68 69 73 20 65 6e 63 6f 64 69 6e  for this encodin
1d171 67 20 69 73 20 72 65 67 69 73 74 65 72 65 64 2e  g is registered.
1d172 0a 20 20 20 20 2a 2a 20 43 61 6c 6c 20 74 68 65  .    ** Call the
1d173 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f   collation facto
1d174 72 79 20 74 6f 20 73 65 65 20 69 66 20 69 74 20  ry to see if it 
1d175 63 61 6e 20 73 75 70 70 6c 79 20 75 73 20 77 69  can supply us wi
1d176 74 68 20 6f 6e 65 2e 0a 20 20 20 20 2a 2f 0a 20  th one..    */. 
1d177 20 20 20 63 61 6c 6c 43 6f 6c 6c 4e 65 65 64 65     callCollNeede
1d178 64 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  d(db, zName, nNa
1d179 6d 65 29 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c  me);.    p = sql
1d17a 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28  ite3FindCollSeq(
1d17b 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 7a 4e 61  db, ENC(db), zNa
1d17c 6d 65 2c 20 6e 4e 61 6d 65 2c 20 30 29 3b 0a 20  me, nName, 0);. 
1d17d 20 7d 0a 20 20 69 66 28 20 70 20 26 26 20 21 70   }.  if( p && !p
1d17e 2d 3e 78 43 6d 70 20 26 26 20 73 79 6e 74 68 43  ->xCmp && synthC
1d17f 6f 6c 6c 53 65 71 28 64 62 2c 20 70 29 20 29 7b  ollSeq(db, p) ){
1d180 0a 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 7d 0a  .    p = 0;.  }.
1d181 20 20 61 73 73 65 72 74 28 20 21 70 20 7c 7c 20    assert( !p || 
1d182 70 2d 3e 78 43 6d 70 20 29 3b 0a 20 20 72 65 74  p->xCmp );.  ret
1d183 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn p;.}../*.** 
1d184 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
1d185 63 61 6c 6c 65 64 20 6f 6e 20 61 20 63 6f 6c 6c  called on a coll
1d186 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 62  ation sequence b
1d187 65 66 6f 72 65 20 69 74 20 69 73 20 75 73 65 64  efore it is used
1d188 20 74 6f 0a 2a 2a 20 63 68 65 63 6b 20 74 68 61   to.** check tha
1d189 74 20 69 74 20 69 73 20 64 65 66 69 6e 65 64 2e  t it is defined.
1d18a 20 41 6e 20 75 6e 64 65 66 69 6e 65 64 20 63 6f   An undefined co
1d18b 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
1d18c 20 65 78 69 73 74 73 20 77 68 65 6e 0a 2a 2a 20   exists when.** 
1d18d 61 20 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f  a database is lo
1d18e 61 64 65 64 20 74 68 61 74 20 63 6f 6e 74 61 69  aded that contai
1d18f 6e 73 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f  ns references to
1d190 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
1d191 6e 63 65 73 0a 2a 2a 20 74 68 61 74 20 68 61 76  nces.** that hav
1d192 65 20 6e 6f 74 20 62 65 65 6e 20 64 65 66 69 6e  e not been defin
1d193 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 72  ed by sqlite3_cr
1d194 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29  eate_collation()
1d195 20 65 74 63 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72   etc..**.** If r
1d196 65 71 75 69 72 65 64 2c 20 74 68 69 73 20 72 6f  equired, this ro
1d197 75 74 69 6e 65 20 63 61 6c 6c 73 20 74 68 65 20  utine calls the 
1d198 27 63 6f 6c 6c 61 74 69 6f 6e 20 6e 65 65 64 65  'collation neede
1d199 64 27 20 63 61 6c 6c 62 61 63 6b 20 74 6f 0a 2a  d' callback to.*
1d19a 2a 20 72 65 71 75 65 73 74 20 61 20 64 65 66 69  * request a defi
1d19b 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f  nition of the co
1d19c 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
1d19d 2e 20 49 66 20 74 68 69 73 20 64 6f 65 73 6e 27  . If this doesn'
1d19e 74 20 77 6f 72 6b 2c 20 0a 2a 2a 20 61 6e 20 65  t work, .** an e
1d19f 71 75 69 76 61 6c 65 6e 74 20 63 6f 6c 6c 61 74  quivalent collat
1d1a0 69 6e 67 20 73 65 71 75 65 6e 63 65 20 74 68 61  ing sequence tha
1d1a1 74 20 75 73 65 73 20 61 20 74 65 78 74 20 65 6e  t uses a text en
1d1a2 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e 74  coding different
1d1a3 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 6d 61 69  .** from the mai
1d1a4 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 73 75  n database is su
1d1a5 62 73 74 69 74 75 74 65 64 2c 20 69 66 20 6f 6e  bstituted, if on
1d1a6 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a  e is available..
1d1a7 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1d1a8 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68 65  E int sqlite3Che
1d1a9 63 6b 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20  ckCollSeq(Parse 
1d1aa 2a 70 50 61 72 73 65 2c 20 43 6f 6c 6c 53 65 71  *pParse, CollSeq
1d1ab 20 2a 70 43 6f 6c 6c 29 7b 0a 20 20 69 66 28 20   *pColl){.  if( 
1d1ac 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 63 6f 6e  pColl ){.    con
1d1ad 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d  st char *zName =
1d1ae 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20   pColl->zName;. 
1d1af 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 20 3d 20     CollSeq *p = 
1d1b0 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65  sqlite3GetCollSe
1d1b1 71 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 43  q(pParse->db, pC
1d1b2 6f 6c 6c 2c 20 7a 4e 61 6d 65 2c 20 2d 31 29 3b  oll, zName, -1);
1d1b3 0a 20 20 20 20 69 66 28 20 21 70 20 29 7b 0a 20  .    if( !p ){. 
1d1b4 20 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d       if( pParse-
1d1b5 3e 6e 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20  >nErr==0 ){.    
1d1b6 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1d1b7 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20  Msg(pParse, "no 
1d1b8 73 75 63 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  such collation s
1d1b9 65 71 75 65 6e 63 65 3a 20 25 73 22 2c 20 7a 4e  equence: %s", zN
1d1ba 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ame);.      }.  
1d1bb 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72      pParse->nErr
1d1bc 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ++;.      return
1d1bd 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1d1be 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
1d1bf 20 70 3d 3d 70 43 6f 6c 6c 20 29 3b 0a 20 20 7d   p==pColl );.  }
1d1c0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1d1c1 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20  _OK;.}..../*.** 
1d1c2 4c 6f 63 61 74 65 20 61 6e 64 20 72 65 74 75 72  Locate and retur
1d1c3 6e 20 61 6e 20 65 6e 74 72 79 20 66 72 6f 6d 20  n an entry from 
1d1c4 74 68 65 20 64 62 2e 61 43 6f 6c 6c 53 65 71 20  the db.aCollSeq 
1d1c5 68 61 73 68 20 74 61 62 6c 65 2e 20 49 66 20 74  hash table. If t
1d1c6 68 65 20 65 6e 74 72 79 0a 2a 2a 20 73 70 65 63  he entry.** spec
1d1c7 69 66 69 65 64 20 62 79 20 7a 4e 61 6d 65 20 61  ified by zName a
1d1c8 6e 64 20 6e 4e 61 6d 65 20 69 73 20 6e 6f 74 20  nd nName is not 
1d1c9 66 6f 75 6e 64 20 61 6e 64 20 70 61 72 61 6d 65  found and parame
1d1ca 74 65 72 20 27 63 72 65 61 74 65 27 20 69 73 0a  ter 'create' is.
1d1cb 2a 2a 20 74 72 75 65 2c 20 74 68 65 6e 20 63 72  ** true, then cr
1d1cc 65 61 74 65 20 61 20 6e 65 77 20 65 6e 74 72 79  eate a new entry
1d1cd 2e 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75  . Otherwise retu
1d1ce 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 45  rn NULL..**.** E
1d1cf 61 63 68 20 70 6f 69 6e 74 65 72 20 73 74 6f 72  ach pointer stor
1d1d0 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65  ed in the sqlite
1d1d1 33 2e 61 43 6f 6c 6c 53 65 71 20 68 61 73 68 20  3.aCollSeq hash 
1d1d2 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20 61  table contains a
1d1d3 6e 0a 2a 2a 20 61 72 72 61 79 20 6f 66 20 74 68  n.** array of th
1d1d4 72 65 65 20 43 6f 6c 6c 53 65 71 20 73 74 72 75  ree CollSeq stru
1d1d5 63 74 75 72 65 73 2e 20 54 68 65 20 66 69 72 73  ctures. The firs
1d1d6 74 20 69 73 20 74 68 65 20 63 6f 6c 6c 61 74 69  t is the collati
1d1d7 6f 6e 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20 70  on sequence.** p
1d1d8 72 65 66 66 65 72 72 65 64 20 66 6f 72 20 55 54  refferred for UT
1d1d9 46 2d 38 2c 20 74 68 65 20 73 65 63 6f 6e 64 20  F-8, the second 
1d1da 55 54 46 2d 31 36 6c 65 2c 20 61 6e 64 20 74 68  UTF-16le, and th
1d1db 65 20 74 68 69 72 64 20 55 54 46 2d 31 36 62 65  e third UTF-16be
1d1dc 2e 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 64 20 69  ..**.** Stored i
1d1dd 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 72  mmediately after
1d1de 20 74 68 65 20 74 68 72 65 65 20 63 6f 6c 6c 61   the three colla
1d1df 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 69  tion sequences i
1d1e0 73 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74  s a copy of.** t
1d1e1 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  he collation seq
1d1e2 75 65 6e 63 65 20 6e 61 6d 65 2e 20 41 20 70 6f  uence name. A po
1d1e3 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74  inter to this st
1d1e4 72 69 6e 67 20 69 73 20 73 74 6f 72 65 64 20 69  ring is stored i
1d1e5 6e 0a 2a 2a 20 65 61 63 68 20 63 6f 6c 6c 61 74  n.** each collat
1d1e6 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 73 74 72  ion sequence str
1d1e7 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69  ucture..*/.stati
1d1e8 63 20 43 6f 6c 6c 53 65 71 20 2a 66 69 6e 64 43  c CollSeq *findC
1d1e9 6f 6c 6c 53 65 71 45 6e 74 72 79 28 0a 20 20 73  ollSeqEntry(.  s
1d1ea 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f  qlite3 *db,.  co
1d1eb 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
1d1ec 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 2c 0a 20 20  .  int nName,.  
1d1ed 69 6e 74 20 63 72 65 61 74 65 0a 29 7b 0a 20 20  int create.){.  
1d1ee 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a  CollSeq *pColl;.
1d1ef 20 20 69 66 28 20 6e 4e 61 6d 65 3c 30 20 29 20    if( nName<0 ) 
1d1f0 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a  nName = strlen(z
1d1f1 4e 61 6d 65 29 3b 0a 20 20 70 43 6f 6c 6c 20 3d  Name);.  pColl =
1d1f2 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64   sqlite3HashFind
1d1f3 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 2c 20  (&db->aCollSeq, 
1d1f4 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 0a  zName, nName);..
1d1f5 20 20 69 66 28 20 30 3d 3d 70 43 6f 6c 6c 20 26    if( 0==pColl &
1d1f6 26 20 63 72 65 61 74 65 20 29 7b 0a 20 20 20 20  & create ){.    
1d1f7 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 44  pColl = sqlite3D
1d1f8 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
1d1f9 33 2a 73 69 7a 65 6f 66 28 2a 70 43 6f 6c 6c 29  3*sizeof(*pColl)
1d1fa 20 2b 20 6e 4e 61 6d 65 20 2b 20 31 20 29 3b 0a   + nName + 1 );.
1d1fb 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b      if( pColl ){
1d1fc 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  .      CollSeq *
1d1fd 70 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20  pDel = 0;.      
1d1fe 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 20 3d  pColl[0].zName =
1d1ff 20 28 63 68 61 72 2a 29 26 70 43 6f 6c 6c 5b 33   (char*)&pColl[3
1d200 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30  ];.      pColl[0
1d201 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55  ].enc = SQLITE_U
1d202 54 46 38 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c  TF8;.      pColl
1d203 5b 31 5d 2e 7a 4e 61 6d 65 20 3d 20 28 63 68 61  [1].zName = (cha
1d204 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d 3b 0a 20 20  r*)&pColl[3];.  
1d205 20 20 20 20 70 43 6f 6c 6c 5b 31 5d 2e 65 6e 63      pColl[1].enc
1d206 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c   = SQLITE_UTF16L
1d207 45 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 32  E;.      pColl[2
1d208 5d 2e 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a  ].zName = (char*
1d209 29 26 70 43 6f 6c 6c 5b 33 5d 3b 0a 20 20 20 20  )&pColl[3];.    
1d20a 20 20 70 43 6f 6c 6c 5b 32 5d 2e 65 6e 63 20 3d    pColl[2].enc =
1d20b 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3b   SQLITE_UTF16BE;
1d20c 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 43  .      memcpy(pC
1d20d 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e  oll[0].zName, zN
1d20e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20  ame, nName);.   
1d20f 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d     pColl[0].zNam
1d210 65 5b 6e 4e 61 6d 65 5d 20 3d 20 30 3b 0a 20 20  e[nName] = 0;.  
1d211 20 20 20 20 70 44 65 6c 20 3d 20 73 71 6c 69 74      pDel = sqlit
1d212 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 64 62  e3HashInsert(&db
1d213 2d 3e 61 43 6f 6c 6c 53 65 71 2c 20 70 43 6f 6c  ->aCollSeq, pCol
1d214 6c 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  l[0].zName, nNam
1d215 65 2c 20 70 43 6f 6c 6c 29 3b 0a 0a 20 20 20 20  e, pColl);..    
1d216 20 20 2f 2a 20 49 66 20 61 20 6d 61 6c 6c 6f 63    /* If a malloc
1d217 28 29 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72  () failure occur
1d218 65 64 20 69 6e 20 73 71 6c 69 74 65 33 48 61 73  ed in sqlite3Has
1d219 68 49 6e 73 65 72 74 28 29 2c 20 69 74 20 77 69  hInsert(), it wi
1d21a 6c 6c 20 0a 20 20 20 20 20 20 2a 2a 20 72 65 74  ll .      ** ret
1d21b 75 72 6e 20 74 68 65 20 70 43 6f 6c 6c 20 70 6f  urn the pColl po
1d21c 69 6e 74 65 72 20 74 6f 20 62 65 20 64 65 6c 65  inter to be dele
1d21d 74 65 64 20 28 62 65 63 61 75 73 65 20 69 74 20  ted (because it 
1d21e 77 61 73 6e 27 74 20 61 64 64 65 64 0a 20 20 20  wasn't added.   
1d21f 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 68 61 73     ** to the has
1d220 68 20 74 61 62 6c 65 29 2e 0a 20 20 20 20 20 20  h table)..      
1d221 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
1d222 20 70 44 65 6c 3d 3d 30 20 7c 7c 20 70 44 65 6c   pDel==0 || pDel
1d223 3d 3d 70 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 20  ==pColl );.     
1d224 20 69 66 28 20 70 44 65 6c 21 3d 30 20 29 7b 0a   if( pDel!=0 ){.
1d225 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c          db->mall
1d226 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
1d227 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
1d228 65 65 28 70 44 65 6c 29 3b 0a 20 20 20 20 20 20  ee(pDel);.      
1d229 20 20 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20    pColl = 0;.   
1d22a 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
1d22b 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d   return pColl;.}
1d22c 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65  ../*.** Paramete
1d22d 72 20 7a 4e 61 6d 65 20 70 6f 69 6e 74 73 20 74  r zName points t
1d22e 6f 20 61 20 55 54 46 2d 38 20 65 6e 63 6f 64 65  o a UTF-8 encode
1d22f 64 20 73 74 72 69 6e 67 20 6e 4e 61 6d 65 20 62  d string nName b
1d230 79 74 65 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 52 65  ytes long..** Re
1d231 74 75 72 6e 20 74 68 65 20 43 6f 6c 6c 53 65 71  turn the CollSeq
1d232 2a 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 74 68  * pointer for th
1d233 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  e collation sequ
1d234 65 6e 63 65 20 6e 61 6d 65 64 20 7a 4e 61 6d 65  ence named zName
1d235 0a 2a 2a 20 66 6f 72 20 74 68 65 20 65 6e 63 6f  .** for the enco
1d236 64 69 6e 67 20 27 65 6e 63 27 20 66 72 6f 6d 20  ding 'enc' from 
1d237 74 68 65 20 64 61 74 61 62 61 73 65 20 27 64 62  the database 'db
1d238 27 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  '..**.** If the 
1d239 65 6e 74 72 79 20 73 70 65 63 69 66 69 65 64 20  entry specified 
1d23a 69 73 20 6e 6f 74 20 66 6f 75 6e 64 20 61 6e 64  is not found and
1d23b 20 27 63 72 65 61 74 65 27 20 69 73 20 74 72 75   'create' is tru
1d23c 65 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 61  e, then create a
1d23d 0a 2a 2a 20 6e 65 77 20 65 6e 74 72 79 2e 20 20  .** new entry.  
1d23e 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72 6e  Otherwise return
1d23f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41 20 73   NULL..**.** A s
1d240 65 70 61 72 61 74 65 20 66 75 6e 63 74 69 6f 6e  eparate function
1d241 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f   sqlite3LocateCo
1d242 6c 6c 53 65 71 28 29 20 69 73 20 61 20 77 72 61  llSeq() is a wra
1d243 70 70 65 72 20 61 72 6f 75 6e 64 0a 2a 2a 20 74  pper around.** t
1d244 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 73 71  his routine.  sq
1d245 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53  lite3LocateCollS
1d246 65 71 28 29 20 69 6e 76 6f 6b 65 73 20 74 68 65  eq() invokes the
1d247 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f   collation facto
1d248 72 79 0a 2a 2a 20 69 66 20 6e 65 63 65 73 73 61  ry.** if necessa
1d249 72 79 20 61 6e 64 20 67 65 6e 65 72 61 74 65 73  ry and generates
1d24a 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
1d24b 65 20 69 66 20 74 68 65 20 63 6f 6c 6c 61 74 69  e if the collati
1d24c 6e 67 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20 63  ng sequence.** c
1d24d 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a  annot be found..
1d24e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1d24f 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74  E CollSeq *sqlit
1d250 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 0a 20  e3FindCollSeq(. 
1d251 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20   sqlite3 *db,.  
1d252 75 38 20 65 6e 63 2c 0a 20 20 63 6f 6e 73 74 20  u8 enc,.  const 
1d253 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 69  char *zName,.  i
1d254 6e 74 20 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20  nt nName,.  int 
1d255 63 72 65 61 74 65 0a 29 7b 0a 20 20 43 6f 6c 6c  create.){.  Coll
1d256 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 66  Seq *pColl;.  if
1d257 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 70  ( zName ){.    p
1d258 43 6f 6c 6c 20 3d 20 66 69 6e 64 43 6f 6c 6c 53  Coll = findCollS
1d259 65 71 45 6e 74 72 79 28 64 62 2c 20 7a 4e 61 6d  eqEntry(db, zNam
1d25a 65 2c 20 6e 4e 61 6d 65 2c 20 63 72 65 61 74 65  e, nName, create
1d25b 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1d25c 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c  pColl = db->pDfl
1d25d 74 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 20 61 73 73  tColl;.  }.  ass
1d25e 65 72 74 28 20 53 51 4c 49 54 45 5f 55 54 46 38  ert( SQLITE_UTF8
1d25f 3d 3d 31 20 26 26 20 53 51 4c 49 54 45 5f 55 54  ==1 && SQLITE_UT
1d260 46 31 36 4c 45 3d 3d 32 20 26 26 20 53 51 4c 49  F16LE==2 && SQLI
1d261 54 45 5f 55 54 46 31 36 42 45 3d 3d 33 20 29 3b  TE_UTF16BE==3 );
1d262 0a 20 20 61 73 73 65 72 74 28 20 65 6e 63 3e 3d  .  assert( enc>=
1d263 53 51 4c 49 54 45 5f 55 54 46 38 20 26 26 20 65  SQLITE_UTF8 && e
1d264 6e 63 3c 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  nc<=SQLITE_UTF16
1d265 42 45 20 29 3b 0a 20 20 69 66 28 20 70 43 6f 6c  BE );.  if( pCol
1d266 6c 20 29 20 70 43 6f 6c 6c 20 2b 3d 20 65 6e 63  l ) pColl += enc
1d267 2d 31 3b 0a 20 20 72 65 74 75 72 6e 20 70 43 6f  -1;.  return pCo
1d268 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63  ll;.}../*.** Loc
1d269 61 74 65 20 61 20 75 73 65 72 20 66 75 6e 63 74  ate a user funct
1d26a 69 6f 6e 20 67 69 76 65 6e 20 61 20 6e 61 6d 65  ion given a name
1d26b 2c 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 61 72  , a number of ar
1d26c 67 75 6d 65 6e 74 73 20 61 6e 64 20 61 20 66 6c  guments and a fl
1d26d 61 67 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67  ag.** indicating
1d26e 20 77 68 65 74 68 65 72 20 74 68 65 20 66 75 6e   whether the fun
1d26f 63 74 69 6f 6e 20 70 72 65 66 65 72 73 20 55 54  ction prefers UT
1d270 46 2d 31 36 20 6f 76 65 72 20 55 54 46 2d 38 2e  F-16 over UTF-8.
1d271 20 20 52 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f    Return a.** po
1d272 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46 75 6e  inter to the Fun
1d273 63 44 65 66 20 73 74 72 75 63 74 75 72 65 20 74  cDef structure t
1d274 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 61 74  hat defines that
1d275 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 72 20 72 65   function, or re
1d276 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c 20 69 66 20  turn.** NULL if 
1d277 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65  the function doe
1d278 73 20 6e 6f 74 20 65 78 69 73 74 2e 0a 2a 2a 0a  s not exist..**.
1d279 2a 2a 20 49 66 20 74 68 65 20 63 72 65 61 74 65  ** If the create
1d27a 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 20 69 73  Flag argument is
1d27b 20 74 72 75 65 2c 20 74 68 65 6e 20 61 20 6e 65   true, then a ne
1d27c 77 20 28 62 6c 61 6e 6b 29 20 46 75 6e 63 44 65  w (blank) FuncDe
1d27d 66 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69  f.** structure i
1d27e 73 20 63 72 65 61 74 65 64 20 61 6e 64 20 6c 69  s created and li
1d27f 6b 65 64 20 69 6e 74 6f 20 74 68 65 20 22 64 62  ked into the "db
1d280 22 20 73 74 72 75 63 74 75 72 65 20 69 66 20 61  " structure if a
1d281 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20  .** no matching 
1d282 66 75 6e 63 74 69 6f 6e 20 70 72 65 76 69 6f 75  function previou
1d283 73 6c 79 20 65 78 69 73 74 65 64 2e 20 20 57 68  sly existed.  Wh
1d284 65 6e 20 63 72 65 61 74 65 46 6c 61 67 20 69 73  en createFlag is
1d285 20 74 72 75 65 0a 2a 2a 20 61 6e 64 20 74 68 65   true.** and the
1d286 20 6e 41 72 67 20 70 61 72 61 6d 65 74 65 72 20   nArg parameter 
1d287 69 73 20 2d 31 2c 20 74 68 65 6e 20 6f 6e 6c 79  is -1, then only
1d288 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74   a function that
1d289 20 61 63 63 65 70 74 73 0a 2a 2a 20 61 6e 79 20   accepts.** any 
1d28a 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  number of argume
1d28b 6e 74 73 20 77 69 6c 6c 20 62 65 20 72 65 74 75  nts will be retu
1d28c 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63  rned..**.** If c
1d28d 72 65 61 74 65 46 6c 61 67 20 69 73 20 66 61 6c  reateFlag is fal
1d28e 73 65 20 61 6e 64 20 6e 41 72 67 20 69 73 20 2d  se and nArg is -
1d28f 31 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72 73  1, then the firs
1d290 74 20 76 61 6c 69 64 0a 2a 2a 20 66 75 6e 63 74  t valid.** funct
1d291 69 6f 6e 20 66 6f 75 6e 64 20 69 73 20 72 65 74  ion found is ret
1d292 75 72 6e 65 64 2e 20 20 41 20 66 75 6e 63 74 69  urned.  A functi
1d293 6f 6e 20 69 73 20 76 61 6c 69 64 20 69 66 20 65  on is valid if e
1d294 69 74 68 65 72 20 78 46 75 6e 63 0a 2a 2a 20 6f  ither xFunc.** o
1d295 72 20 78 53 74 65 70 20 69 73 20 6e 6f 6e 2d 7a  r xStep is non-z
1d296 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 72  ero..**.** If cr
1d297 65 61 74 65 46 6c 61 67 20 69 73 20 66 61 6c 73  eateFlag is fals
1d298 65 2c 20 74 68 65 6e 20 61 20 66 75 6e 63 74 69  e, then a functi
1d299 6f 6e 20 77 69 74 68 20 74 68 65 20 72 65 71 75  on with the requ
1d29a 69 72 65 64 20 6e 61 6d 65 20 61 6e 64 0a 2a 2a  ired name and.**
1d29b 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   number of argum
1d29c 65 6e 74 73 20 6d 61 79 20 62 65 20 72 65 74 75  ents may be retu
1d29d 72 6e 65 64 20 65 76 65 6e 20 69 66 20 74 68 65  rned even if the
1d29e 20 65 54 65 78 74 52 65 70 20 66 6c 61 67 20 64   eTextRep flag d
1d29f 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6d 61 74 63 68  oes not.** match
1d2a0 20 74 68 61 74 20 72 65 71 75 65 73 74 65 64 2e   that requested.
1d2a1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1d2a2 54 45 20 46 75 6e 63 44 65 66 20 2a 73 71 6c 69  TE FuncDef *sqli
1d2a3 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28  te3FindFunction(
1d2a4 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
1d2a5 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65 6e        /* An open
1d2a6 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 63   database */.  c
1d2a7 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
1d2a8 2c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65  , /* Name of the
1d2a9 20 66 75 6e 63 74 69 6f 6e 2e 20 20 4e 6f 74 20   function.  Not 
1d2aa 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20  null-terminated 
1d2ab 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 2c 20  */.  int nName, 
1d2ac 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1d2ad 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  r of characters 
1d2ae 69 6e 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 20  in the name */. 
1d2af 20 69 6e 74 20 6e 41 72 67 2c 20 20 20 20 20 20   int nArg,      
1d2b0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1d2b1 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 2d 31 20   arguments.  -1 
1d2b2 6d 65 61 6e 73 20 61 6e 79 20 6e 75 6d 62 65 72  means any number
1d2b3 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20   */.  u8 enc,   
1d2b4 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66           /* Pref
1d2b5 65 72 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64  erred text encod
1d2b6 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 63 72 65  ing */.  int cre
1d2b7 61 74 65 46 6c 61 67 20 20 20 20 20 2f 2a 20 43  ateFlag     /* C
1d2b8 72 65 61 74 65 20 6e 65 77 20 65 6e 74 72 79 20  reate new entry 
1d2b9 69 66 20 74 72 75 65 20 61 6e 64 20 64 6f 65 73  if true and does
1d2ba 20 6e 6f 74 20 6f 74 68 65 72 77 69 73 65 20 65   not otherwise e
1d2bb 78 69 73 74 20 2a 2f 0a 29 7b 0a 20 20 46 75 6e  xist */.){.  Fun
1d2bc 63 44 65 66 20 2a 70 3b 20 20 20 20 20 20 20 20  cDef *p;        
1d2bd 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72   /* Iterator var
1d2be 69 61 62 6c 65 20 2a 2f 0a 20 20 46 75 6e 63 44  iable */.  FuncD
1d2bf 65 66 20 2a 70 46 69 72 73 74 3b 20 20 20 20 2f  ef *pFirst;    /
1d2c0 2a 20 46 69 72 73 74 20 66 75 6e 63 74 69 6f 6e  * First function
1d2c1 20 77 69 74 68 20 74 68 69 73 20 6e 61 6d 65 20   with this name 
1d2c2 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 42  */.  FuncDef *pB
1d2c3 65 73 74 20 3d 20 30 3b 20 2f 2a 20 42 65 73 74  est = 0; /* Best
1d2c4 20 6d 61 74 63 68 20 66 6f 75 6e 64 20 73 6f 20   match found so 
1d2c5 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 62 65 73  far */.  int bes
1d2c6 74 6d 61 74 63 68 20 3d 20 30 3b 20 20 0a 0a 0a  tmatch = 0;  ...
1d2c7 20 20 61 73 73 65 72 74 28 20 65 6e 63 3d 3d 53    assert( enc==S
1d2c8 51 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 65 6e  QLITE_UTF8 || en
1d2c9 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  c==SQLITE_UTF16L
1d2ca 45 20 7c 7c 20 65 6e 63 3d 3d 53 51 4c 49 54 45  E || enc==SQLITE
1d2cb 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 69 66  _UTF16BE );.  if
1d2cc 28 20 6e 41 72 67 3c 2d 31 20 29 20 6e 41 72 67  ( nArg<-1 ) nArg
1d2cd 20 3d 20 2d 31 3b 0a 0a 20 20 70 46 69 72 73 74   = -1;..  pFirst
1d2ce 20 3d 20 28 46 75 6e 63 44 65 66 2a 29 73 71 6c   = (FuncDef*)sql
1d2cf 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 64 62  ite3HashFind(&db
1d2d0 2d 3e 61 46 75 6e 63 2c 20 7a 4e 61 6d 65 2c 20  ->aFunc, zName, 
1d2d1 6e 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 28 70 3d  nName);.  for(p=
1d2d2 70 46 69 72 73 74 3b 20 70 3b 20 70 3d 70 2d 3e  pFirst; p; p=p->
1d2d3 70 4e 65 78 74 29 7b 0a 20 20 20 20 2f 2a 20 44  pNext){.    /* D
1d2d4 75 72 69 6e 67 20 74 68 65 20 73 65 61 72 63 68  uring the search
1d2d5 20 66 6f 72 20 74 68 65 20 62 65 73 74 20 66 75   for the best fu
1d2d6 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f  nction definitio
1d2d7 6e 2c 20 62 65 73 74 6d 61 74 63 68 20 69 73 20  n, bestmatch is 
1d2d8 73 65 74 0a 20 20 20 20 2a 2a 20 61 73 20 66 6f  set.    ** as fo
1d2d9 6c 6c 6f 77 73 20 74 6f 20 69 6e 64 69 63 61 74  llows to indicat
1d2da 65 20 74 68 65 20 71 75 61 6c 69 74 79 20 6f 66  e the quality of
1d2db 20 74 68 65 20 6d 61 74 63 68 20 77 69 74 68 20   the match with 
1d2dc 74 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 0a 20  the definition. 
1d2dd 20 20 20 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f     ** pointed to
1d2de 20 62 79 20 70 42 65 73 74 3a 0a 20 20 20 20 2a   by pBest:.    *
1d2df 2a 0a 20 20 20 20 2a 2a 20 30 3a 20 70 42 65 73  *.    ** 0: pBes
1d2e0 74 20 69 73 20 4e 55 4c 4c 2e 20 4e 6f 20 6d 61  t is NULL. No ma
1d2e1 74 63 68 20 68 61 73 20 62 65 65 6e 20 66 6f 75  tch has been fou
1d2e2 6e 64 2e 0a 20 20 20 20 2a 2a 20 31 3a 20 41 20  nd..    ** 1: A 
1d2e3 76 61 72 69 61 62 6c 65 20 61 72 67 75 6d 65 6e  variable argumen
1d2e4 74 73 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74  ts function that
1d2e5 20 70 72 65 66 65 72 73 20 55 54 46 2d 38 20 77   prefers UTF-8 w
1d2e6 68 65 6e 20 61 20 55 54 46 2d 31 36 0a 20 20 20  hen a UTF-16.   
1d2e7 20 2a 2a 20 20 20 20 65 6e 63 6f 64 69 6e 67 20   **    encoding 
1d2e8 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 6f 72  is requested, or
1d2e9 20 76 69 63 65 20 76 65 72 73 61 2e 0a 20 20 20   vice versa..   
1d2ea 20 2a 2a 20 32 3a 20 41 20 76 61 72 69 61 62 6c   ** 2: A variabl
1d2eb 65 20 61 72 67 75 6d 65 6e 74 73 20 66 75 6e 63  e arguments func
1d2ec 74 69 6f 6e 20 74 68 61 74 20 75 73 65 73 20 55  tion that uses U
1d2ed 54 46 2d 31 36 42 45 20 77 68 65 6e 20 55 54 46  TF-16BE when UTF
1d2ee 2d 31 36 4c 45 20 69 73 0a 20 20 20 20 2a 2a 20  -16LE is.    ** 
1d2ef 20 20 20 72 65 71 75 65 73 74 65 64 2c 20 6f 72     requested, or
1d2f0 20 76 69 63 65 20 76 65 72 73 61 2e 0a 20 20 20   vice versa..   
1d2f1 20 2a 2a 20 33 3a 20 41 20 76 61 72 69 61 62 6c   ** 3: A variabl
1d2f2 65 20 61 72 67 75 6d 65 6e 74 73 20 66 75 6e 63  e arguments func
1d2f3 74 69 6f 6e 20 75 73 69 6e 67 20 74 68 65 20 73  tion using the s
1d2f4 61 6d 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  ame text encodin
1d2f5 67 2e 0a 20 20 20 20 2a 2a 20 34 3a 20 41 20 66  g..    ** 4: A f
1d2f6 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65  unction with the
1d2f7 20 65 78 61 63 74 20 6e 75 6d 62 65 72 20 6f 66   exact number of
1d2f8 20 61 72 67 75 6d 65 6e 74 73 20 72 65 71 75 65   arguments reque
1d2f9 73 74 65 64 20 74 68 61 74 0a 20 20 20 20 2a 2a  sted that.    **
1d2fa 20 20 20 20 70 72 65 66 65 72 73 20 55 54 46 2d      prefers UTF-
1d2fb 38 20 77 68 65 6e 20 61 20 55 54 46 2d 31 36 20  8 when a UTF-16 
1d2fc 65 6e 63 6f 64 69 6e 67 20 69 73 20 72 65 71 75  encoding is requ
1d2fd 65 73 74 65 64 2c 20 6f 72 20 76 69 63 65 20 76  ested, or vice v
1d2fe 65 72 73 61 2e 0a 20 20 20 20 2a 2a 20 35 3a 20  ersa..    ** 5: 
1d2ff 41 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20  A function with 
1d300 74 68 65 20 65 78 61 63 74 20 6e 75 6d 62 65 72  the exact number
1d301 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 72 65   of arguments re
1d302 71 75 65 73 74 65 64 20 74 68 61 74 0a 20 20 20  quested that.   
1d303 20 2a 2a 20 20 20 20 70 72 65 66 65 72 73 20 55   **    prefers U
1d304 54 46 2d 31 36 4c 45 20 77 68 65 6e 20 55 54 46  TF-16LE when UTF
1d305 2d 31 36 42 45 20 69 73 20 72 65 71 75 65 73 74  -16BE is request
1d306 65 64 2c 20 6f 72 20 76 69 63 65 20 76 65 72 73  ed, or vice vers
1d307 61 2e 0a 20 20 20 20 2a 2a 20 36 3a 20 41 6e 20  a..    ** 6: An 
1d308 65 78 61 63 74 20 6d 61 74 63 68 2e 0a 20 20 20  exact match..   
1d309 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 20 6c 61 72   **.    ** A lar
1d30a 67 65 72 20 76 61 6c 75 65 20 6f 66 20 27 6d 61  ger value of 'ma
1d30b 74 63 68 71 75 61 6c 27 20 69 6e 64 69 63 61 74  tchqual' indicat
1d30c 65 73 20 61 20 6d 6f 72 65 20 64 65 73 69 72 61  es a more desira
1d30d 62 6c 65 20 6d 61 74 63 68 2e 0a 20 20 20 20 2a  ble match..    *
1d30e 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 41 72  /.    if( p->nAr
1d30f 67 3d 3d 2d 31 20 7c 7c 20 70 2d 3e 6e 41 72 67  g==-1 || p->nArg
1d310 3d 3d 6e 41 72 67 20 7c 7c 20 6e 41 72 67 3d 3d  ==nArg || nArg==
1d311 2d 31 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  -1 ){.      int 
1d312 6d 61 74 63 68 20 3d 20 31 3b 20 20 20 20 20 20  match = 1;      
1d313 20 20 20 20 2f 2a 20 51 75 61 6c 69 74 79 20 6f      /* Quality o
1d314 66 20 74 68 69 73 20 6d 61 74 63 68 20 2a 2f 0a  f this match */.
1d315 20 20 20 20 20 20 69 66 28 20 70 2d 3e 6e 41 72        if( p->nAr
1d316 67 3d 3d 6e 41 72 67 20 7c 7c 20 6e 41 72 67 3d  g==nArg || nArg=
1d317 3d 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 6d  =-1 ){.        m
1d318 61 74 63 68 20 3d 20 34 3b 0a 20 20 20 20 20 20  atch = 4;.      
1d319 7d 0a 20 20 20 20 20 20 69 66 28 20 65 6e 63 3d  }.      if( enc=
1d31a 3d 70 2d 3e 69 50 72 65 66 45 6e 63 20 29 7b 0a  =p->iPrefEnc ){.
1d31b 20 20 20 20 20 20 20 20 6d 61 74 63 68 20 2b 3d          match +=
1d31c 20 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   2;.      }.    
1d31d 20 20 65 6c 73 65 20 69 66 28 20 28 65 6e 63 3d    else if( (enc=
1d31e 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20  =SQLITE_UTF16LE 
1d31f 26 26 20 70 2d 3e 69 50 72 65 66 45 6e 63 3d 3d  && p->iPrefEnc==
1d320 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 29 20  SQLITE_UTF16BE) 
1d321 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ||.             
1d322 20 20 28 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55    (enc==SQLITE_U
1d323 54 46 31 36 42 45 20 26 26 20 70 2d 3e 69 50 72  TF16BE && p->iPr
1d324 65 66 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  efEnc==SQLITE_UT
1d325 46 31 36 4c 45 29 20 29 7b 0a 20 20 20 20 20 20  F16LE) ){.      
1d326 20 20 6d 61 74 63 68 20 2b 3d 20 31 3b 0a 20 20    match += 1;.  
1d327 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28      }..      if(
1d328 20 6d 61 74 63 68 3e 62 65 73 74 6d 61 74 63 68   match>bestmatch
1d329 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 65 73   ){.        pBes
1d32a 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 62  t = p;.        b
1d32b 65 73 74 6d 61 74 63 68 20 3d 20 6d 61 74 63 68  estmatch = match
1d32c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1d32d 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
1d32e 20 63 72 65 61 74 65 46 6c 61 67 20 70 61 72 61   createFlag para
1d32f 6d 65 74 65 72 20 69 73 20 74 72 75 65 2c 20 61  meter is true, a
1d330 6e 64 20 74 68 65 20 73 65 61 63 68 20 64 69 64  nd the seach did
1d331 20 6e 6f 74 20 72 65 76 65 61 6c 20 61 6e 0a 20   not reveal an. 
1d332 20 2a 2a 20 65 78 61 63 74 20 6d 61 74 63 68 20   ** exact match 
1d333 66 6f 72 20 74 68 65 20 6e 61 6d 65 2c 20 6e 75  for the name, nu
1d334 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
1d335 73 20 61 6e 64 20 65 6e 63 6f 64 69 6e 67 2c 20  s and encoding, 
1d336 74 68 65 6e 20 61 64 64 20 61 0a 20 20 2a 2a 20  then add a.  ** 
1d337 6e 65 77 20 65 6e 74 72 79 20 74 6f 20 74 68 65  new entry to the
1d338 20 68 61 73 68 20 74 61 62 6c 65 20 61 6e 64 20   hash table and 
1d339 72 65 74 75 72 6e 20 69 74 2e 0a 20 20 2a 2f 0a  return it..  */.
1d33a 20 20 69 66 28 20 63 72 65 61 74 65 46 6c 61 67    if( createFlag
1d33b 20 26 26 20 62 65 73 74 6d 61 74 63 68 3c 36 20   && bestmatch<6 
1d33c 26 26 20 0a 20 20 20 20 20 20 28 70 42 65 73 74  && .      (pBest
1d33d 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
1d33e 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
1d33f 66 28 2a 70 42 65 73 74 29 2b 6e 4e 61 6d 65 29  f(*pBest)+nName)
1d340 29 21 3d 30 20 29 7b 0a 20 20 20 20 70 42 65 73  )!=0 ){.    pBes
1d341 74 2d 3e 6e 41 72 67 20 3d 20 6e 41 72 67 3b 0a  t->nArg = nArg;.
1d342 20 20 20 20 70 42 65 73 74 2d 3e 70 4e 65 78 74      pBest->pNext
1d343 20 3d 20 70 46 69 72 73 74 3b 0a 20 20 20 20 70   = pFirst;.    p
1d344 42 65 73 74 2d 3e 69 50 72 65 66 45 6e 63 20 3d  Best->iPrefEnc =
1d345 20 65 6e 63 3b 0a 20 20 20 20 6d 65 6d 63 70 79   enc;.    memcpy
1d346 28 70 42 65 73 74 2d 3e 7a 4e 61 6d 65 2c 20 7a  (pBest->zName, z
1d347 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20  Name, nName);.  
1d348 20 20 70 42 65 73 74 2d 3e 7a 4e 61 6d 65 5b 6e    pBest->zName[n
1d349 4e 61 6d 65 5d 20 3d 20 30 3b 0a 20 20 20 20 69  Name] = 0;.    i
1d34a 66 28 20 70 42 65 73 74 3d 3d 73 71 6c 69 74 65  f( pBest==sqlite
1d34b 33 48 61 73 68 49 6e 73 65 72 74 28 26 64 62 2d  3HashInsert(&db-
1d34c 3e 61 46 75 6e 63 2c 70 42 65 73 74 2d 3e 7a 4e  >aFunc,pBest->zN
1d34d 61 6d 65 2c 6e 4e 61 6d 65 2c 28 76 6f 69 64 2a  ame,nName,(void*
1d34e 29 70 42 65 73 74 29 20 29 7b 0a 20 20 20 20 20  )pBest) ){.     
1d34f 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1d350 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 73 71 6c  d = 1;.      sql
1d351 69 74 65 33 5f 66 72 65 65 28 70 42 65 73 74 29  ite3_free(pBest)
1d352 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  ;.      return 0
1d353 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69  ;.    }.  }..  i
1d354 66 28 20 70 42 65 73 74 20 26 26 20 28 70 42 65  f( pBest && (pBe
1d355 73 74 2d 3e 78 53 74 65 70 20 7c 7c 20 70 42 65  st->xStep || pBe
1d356 73 74 2d 3e 78 46 75 6e 63 20 7c 7c 20 63 72 65  st->xFunc || cre
1d357 61 74 65 46 6c 61 67 29 20 29 7b 0a 20 20 20 20  ateFlag) ){.    
1d358 72 65 74 75 72 6e 20 70 42 65 73 74 3b 0a 20 20  return pBest;.  
1d359 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
1d35a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6c 6c 20  ./*.** Free all 
1d35b 72 65 73 6f 75 72 63 65 73 20 68 65 6c 64 20 62  resources held b
1d35c 79 20 74 68 65 20 73 63 68 65 6d 61 20 73 74 72  y the schema str
1d35d 75 63 74 75 72 65 2e 20 54 68 65 20 76 6f 69 64  ucture. The void
1d35e 2a 20 61 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74  * argument point
1d35f 73 0a 2a 2a 20 61 74 20 61 20 53 63 68 65 6d 61  s.** at a Schema
1d360 20 73 74 72 75 63 74 2e 20 54 68 69 73 20 66 75   struct. This fu
1d361 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20  nction does not 
1d362 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 66 72 65  call sqlite3_fre
1d363 65 28 29 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 70  e() on the .** p
1d364 6f 69 6e 74 65 72 20 69 74 73 65 6c 66 2c 20 69  ointer itself, i
1d365 74 20 6a 75 73 74 20 63 6c 65 61 6e 73 20 75 70  t just cleans up
1d366 20 73 75 62 73 69 64 75 61 72 79 20 72 65 73 6f   subsiduary reso
1d367 75 72 63 65 73 20 28 69 2e 65 2e 20 74 68 65 20  urces (i.e. the 
1d368 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 20 74  contents.** of t
1d369 68 65 20 73 63 68 65 6d 61 20 68 61 73 68 20 74  he schema hash t
1d36a 61 62 6c 65 73 29 2e 0a 2a 2f 0a 53 51 4c 49 54  ables)..*/.SQLIT
1d36b 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1d36c 71 6c 69 74 65 33 53 63 68 65 6d 61 46 72 65 65  qlite3SchemaFree
1d36d 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 48 61 73  (void *p){.  Has
1d36e 68 20 74 65 6d 70 31 3b 0a 20 20 48 61 73 68 20  h temp1;.  Hash 
1d36f 74 65 6d 70 32 3b 0a 20 20 48 61 73 68 45 6c 65  temp2;.  HashEle
1d370 6d 20 2a 70 45 6c 65 6d 3b 0a 20 20 53 63 68 65  m *pElem;.  Sche
1d371 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 28 53  ma *pSchema = (S
1d372 63 68 65 6d 61 20 2a 29 70 3b 0a 0a 20 20 74 65  chema *)p;..  te
1d373 6d 70 31 20 3d 20 70 53 63 68 65 6d 61 2d 3e 74  mp1 = pSchema->t
1d374 62 6c 48 61 73 68 3b 0a 20 20 74 65 6d 70 32 20  blHash;.  temp2 
1d375 3d 20 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48  = pSchema->trigH
1d376 61 73 68 3b 0a 20 20 73 71 6c 69 74 65 33 48 61  ash;.  sqlite3Ha
1d377 73 68 49 6e 69 74 28 26 70 53 63 68 65 6d 61 2d  shInit(&pSchema-
1d378 3e 74 72 69 67 48 61 73 68 2c 20 53 51 4c 49 54  >trigHash, SQLIT
1d379 45 5f 48 41 53 48 5f 53 54 52 49 4e 47 2c 20 30  E_HASH_STRING, 0
1d37a 29 3b 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68  );.  sqlite3Hash
1d37b 43 6c 65 61 72 28 26 70 53 63 68 65 6d 61 2d 3e  Clear(&pSchema->
1d37c 61 46 4b 65 79 29 3b 0a 20 20 73 71 6c 69 74 65  aFKey);.  sqlite
1d37d 33 48 61 73 68 43 6c 65 61 72 28 26 70 53 63 68  3HashClear(&pSch
1d37e 65 6d 61 2d 3e 69 64 78 48 61 73 68 29 3b 0a 20  ema->idxHash);. 
1d37f 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74   for(pElem=sqlit
1d380 65 48 61 73 68 46 69 72 73 74 28 26 74 65 6d 70  eHashFirst(&temp
1d381 32 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d  2); pElem; pElem
1d382 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28  =sqliteHashNext(
1d383 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20 73 71 6c  pElem)){.    sql
1d384 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65  ite3DeleteTrigge
1d385 72 28 28 54 72 69 67 67 65 72 2a 29 73 71 6c 69  r((Trigger*)sqli
1d386 74 65 48 61 73 68 44 61 74 61 28 70 45 6c 65 6d  teHashData(pElem
1d387 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ));.  }.  sqlite
1d388 33 48 61 73 68 43 6c 65 61 72 28 26 74 65 6d 70  3HashClear(&temp
1d389 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 48 61 73  2);.  sqlite3Has
1d38a 68 49 6e 69 74 28 26 70 53 63 68 65 6d 61 2d 3e  hInit(&pSchema->
1d38b 74 62 6c 48 61 73 68 2c 20 53 51 4c 49 54 45 5f  tblHash, SQLITE_
1d38c 48 41 53 48 5f 53 54 52 49 4e 47 2c 20 30 29 3b  HASH_STRING, 0);
1d38d 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c  .  for(pElem=sql
1d38e 69 74 65 48 61 73 68 46 69 72 73 74 28 26 74 65  iteHashFirst(&te
1d38f 6d 70 31 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c  mp1); pElem; pEl
1d390 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78  em=sqliteHashNex
1d391 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20 54  t(pElem)){.    T
1d392 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c  able *pTab = sql
1d393 69 74 65 48 61 73 68 44 61 74 61 28 70 45 6c 65  iteHashData(pEle
1d394 6d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  m);.    sqlite3D
1d395 65 6c 65 74 65 54 61 62 6c 65 28 70 54 61 62 29  eleteTable(pTab)
1d396 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 48  ;.  }.  sqlite3H
1d397 61 73 68 43 6c 65 61 72 28 26 74 65 6d 70 31 29  ashClear(&temp1)
1d398 3b 0a 20 20 70 53 63 68 65 6d 61 2d 3e 70 53 65  ;.  pSchema->pSe
1d399 71 54 61 62 20 3d 20 30 3b 0a 20 20 70 53 63 68  qTab = 0;.  pSch
1d39a 65 6d 61 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 44  ema->flags &= ~D
1d39b 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 3b 0a  B_SchemaLoaded;.
1d39c 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 61 6e  }../*.** Find an
1d39d 64 20 72 65 74 75 72 6e 20 74 68 65 20 73 63 68  d return the sch
1d39e 65 6d 61 20 61 73 73 6f 63 69 61 74 65 64 20 77  ema associated w
1d39f 69 74 68 20 61 20 42 54 72 65 65 2e 20 20 43 72  ith a BTree.  Cr
1d3a0 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6f 6e  eate.** a new on
1d3a1 65 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a  e if necessary..
1d3a2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1d3a3 45 20 53 63 68 65 6d 61 20 2a 73 71 6c 69 74 65  E Schema *sqlite
1d3a4 33 53 63 68 65 6d 61 47 65 74 28 73 71 6c 69 74  3SchemaGet(sqlit
1d3a5 65 33 20 2a 64 62 2c 20 42 74 72 65 65 20 2a 70  e3 *db, Btree *p
1d3a6 42 74 29 7b 0a 20 20 53 63 68 65 6d 61 20 2a 20  Bt){.  Schema * 
1d3a7 70 3b 0a 20 20 69 66 28 20 70 42 74 20 29 7b 0a  p;.  if( pBt ){.
1d3a8 20 20 20 20 70 20 3d 20 28 53 63 68 65 6d 61 20      p = (Schema 
1d3a9 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 53 63  *)sqlite3BtreeSc
1d3aa 68 65 6d 61 28 70 42 74 2c 20 73 69 7a 65 6f 66  hema(pBt, sizeof
1d3ab 28 53 63 68 65 6d 61 29 2c 20 73 71 6c 69 74 65  (Schema), sqlite
1d3ac 33 53 63 68 65 6d 61 46 72 65 65 29 3b 0a 20 20  3SchemaFree);.  
1d3ad 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 28  }else{.    p = (
1d3ae 53 63 68 65 6d 61 20 2a 29 73 71 6c 69 74 65 33  Schema *)sqlite3
1d3af 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f  MallocZero(sizeo
1d3b0 66 28 53 63 68 65 6d 61 29 29 3b 0a 20 20 7d 0a  f(Schema));.  }.
1d3b1 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20    if( !p ){.    
1d3b2 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1d3b3 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66   = 1;.  }else if
1d3b4 20 28 20 30 3d 3d 70 2d 3e 66 69 6c 65 5f 66 6f   ( 0==p->file_fo
1d3b5 72 6d 61 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  rmat ){.    sqli
1d3b6 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 2d 3e  te3HashInit(&p->
1d3b7 74 62 6c 48 61 73 68 2c 20 53 51 4c 49 54 45 5f  tblHash, SQLITE_
1d3b8 48 41 53 48 5f 53 54 52 49 4e 47 2c 20 30 29 3b  HASH_STRING, 0);
1d3b9 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68  .    sqlite3Hash
1d3ba 49 6e 69 74 28 26 70 2d 3e 69 64 78 48 61 73 68  Init(&p->idxHash
1d3bb 2c 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 53 54  , SQLITE_HASH_ST
1d3bc 52 49 4e 47 2c 20 30 29 3b 0a 20 20 20 20 73 71  RING, 0);.    sq
1d3bd 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70  lite3HashInit(&p
1d3be 2d 3e 74 72 69 67 48 61 73 68 2c 20 53 51 4c 49  ->trigHash, SQLI
1d3bf 54 45 5f 48 41 53 48 5f 53 54 52 49 4e 47 2c 20  TE_HASH_STRING, 
1d3c0 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 48  0);.    sqlite3H
1d3c1 61 73 68 49 6e 69 74 28 26 70 2d 3e 61 46 4b 65  ashInit(&p->aFKe
1d3c2 79 2c 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 53  y, SQLITE_HASH_S
1d3c3 54 52 49 4e 47 2c 20 31 29 3b 0a 20 20 20 20 70  TRING, 1);.    p
1d3c4 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55  ->enc = SQLITE_U
1d3c5 54 46 38 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  TF8;.  }.  retur
1d3c6 6e 20 70 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  n p;.}../*******
1d3c7 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 63  ******* End of c
1d3c8 61 6c 6c 62 61 63 6b 2e 63 20 2a 2a 2a 2a 2a 2a  allback.c ******
1d3c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d3ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d3cb 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
1d3cc 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
1d3cd 6c 65 20 64 65 6c 65 74 65 2e 63 20 2a 2a 2a 2a  le delete.c ****
1d3ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d3cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d3d0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
1d3d1 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
1d3d2 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
1d3d3 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
1d3d4 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
1d3d5 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
1d3d6 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
1d3d7 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
1d3d8 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
1d3d9 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
1d3da 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
1d3db 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
1d3dc 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
1d3dd 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
1d3de 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
1d3df 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
1d3e0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
1d3e1 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
1d3e2 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
1d3e3 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
1d3e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d3e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d3e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d3e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
1d3e8 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
1d3e9 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74  ains C code rout
1d3ea 69 6e 65 73 20 74 68 61 74 20 61 72 65 20 63 61  ines that are ca
1d3eb 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
1d3ec 65 72 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74  er.** in order t
1d3ed 6f 20 67 65 6e 65 72 61 74 65 20 63 6f 64 65 20  o generate code 
1d3ee 66 6f 72 20 44 45 4c 45 54 45 20 46 52 4f 4d 20  for DELETE FROM 
1d3ef 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a  statements..**.*
1d3f0 2a 20 24 49 64 3a 20 64 65 6c 65 74 65 2e 63 2c  * $Id: delete.c,
1d3f1 76 20 31 2e 31 36 39 20 32 30 30 38 2f 30 34 2f  v 1.169 2008/04/
1d3f2 32 38 20 31 38 3a 34 36 3a 34 33 20 64 72 68 20  28 18:46:43 drh 
1d3f3 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
1d3f4 4c 6f 6f 6b 20 75 70 20 65 76 65 72 79 20 74 61  Look up every ta
1d3f5 62 6c 65 20 74 68 61 74 20 69 73 20 6e 61 6d 65  ble that is name
1d3f6 64 20 69 6e 20 70 53 72 63 2e 20 20 49 66 20 61  d in pSrc.  If a
1d3f7 6e 79 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20  ny table is not 
1d3f8 66 6f 75 6e 64 2c 0a 2a 2a 20 61 64 64 20 61 6e  found,.** add an
1d3f9 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74   error message t
1d3fa 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73  o pParse->zErrMs
1d3fb 67 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c  g and return NUL
1d3fc 4c 2e 20 20 49 66 20 61 6c 6c 20 74 61 62 6c 65  L.  If all table
1d3fd 73 0a 2a 2a 20 61 72 65 20 66 6f 75 6e 64 2c 20  s.** are found, 
1d3fe 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  return a pointer
1d3ff 20 74 6f 20 74 68 65 20 6c 61 73 74 20 74 61 62   to the last tab
1d400 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
1d401 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c  IVATE Table *sql
1d402 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75  ite3SrcListLooku
1d403 70 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  p(Parse *pParse,
1d404 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 29 7b   SrcList *pSrc){
1d405 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d  .  Table *pTab =
1d406 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73   0;.  int i;.  s
1d407 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
1d408 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 66 6f 72  em *pItem;.  for
1d409 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 53 72 63  (i=0, pItem=pSrc
1d40a 2d 3e 61 3b 20 69 3c 70 53 72 63 2d 3e 6e 53 72  ->a; i<pSrc->nSr
1d40b 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29  c; i++, pItem++)
1d40c 7b 0a 20 20 20 20 70 54 61 62 20 3d 20 73 71 6c  {.    pTab = sql
1d40d 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28  ite3LocateTable(
1d40e 70 50 61 72 73 65 2c 20 30 2c 20 70 49 74 65 6d  pParse, 0, pItem
1d40f 2d 3e 7a 4e 61 6d 65 2c 20 70 49 74 65 6d 2d 3e  ->zName, pItem->
1d410 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20  zDatabase);.    
1d411 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62  sqlite3DeleteTab
1d412 6c 65 28 70 49 74 65 6d 2d 3e 70 54 61 62 29 3b  le(pItem->pTab);
1d413 0a 20 20 20 20 70 49 74 65 6d 2d 3e 70 54 61 62  .    pItem->pTab
1d414 20 3d 20 70 54 61 62 3b 0a 20 20 20 20 69 66 28   = pTab;.    if(
1d415 20 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 70   pTab ){.      p
1d416 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20  Tab->nRef++;.   
1d417 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1d418 70 54 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  pTab;.}../*.** C
1d419 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72  heck to make sur
1d41a 65 20 74 68 65 20 67 69 76 65 6e 20 74 61 62 6c  e the given tabl
1d41b 65 20 69 73 20 77 72 69 74 61 62 6c 65 2e 20 20  e is writable.  
1d41c 49 66 20 69 74 20 69 73 20 6e 6f 74 0a 2a 2a 20  If it is not.** 
1d41d 77 72 69 74 61 62 6c 65 2c 20 67 65 6e 65 72 61  writable, genera
1d41e 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  te an error mess
1d41f 61 67 65 20 61 6e 64 20 72 65 74 75 72 6e 20 31  age and return 1
1d420 2e 20 20 49 66 20 69 74 20 69 73 0a 2a 2a 20 77  .  If it is.** w
1d421 72 69 74 61 62 6c 65 20 72 65 74 75 72 6e 20 30  ritable return 0
1d422 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ;.*/.SQLITE_PRIV
1d423 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
1d424 73 52 65 61 64 4f 6e 6c 79 28 50 61 72 73 65 20  sReadOnly(Parse 
1d425 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a  *pParse, Table *
1d426 70 54 61 62 2c 20 69 6e 74 20 76 69 65 77 4f 6b  pTab, int viewOk
1d427 29 7b 0a 20 20 69 66 28 20 28 70 54 61 62 2d 3e  ){.  if( (pTab->
1d428 72 65 61 64 4f 6e 6c 79 20 26 26 20 28 70 50 61  readOnly && (pPa
1d429 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26  rse->db->flags &
1d42a 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68   SQLITE_WriteSch
1d42b 65 6d 61 29 3d 3d 30 0a 20 20 20 20 20 20 20 20  ema)==0.        
1d42c 26 26 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  && pParse->neste
1d42d 64 3d 3d 30 29 20 0a 23 69 66 6e 64 65 66 20 53  d==0) .#ifndef S
1d42e 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1d42f 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 7c 7c  ALTABLE.      ||
1d430 20 28 70 54 61 62 2d 3e 70 4d 6f 64 20 26 26 20   (pTab->pMod && 
1d431 70 54 61 62 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f 64  pTab->pMod->pMod
1d432 75 6c 65 2d 3e 78 55 70 64 61 74 65 3d 3d 30 29  ule->xUpdate==0)
1d433 0a 23 65 6e 64 69 66 0a 20 20 29 7b 0a 20 20 20  .#endif.  ){.   
1d434 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1d435 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20  (pParse, "table 
1d436 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f  %s may not be mo
1d437 64 69 66 69 65 64 22 2c 20 70 54 61 62 2d 3e 7a  dified", pTab->z
1d438 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72  Name);.    retur
1d439 6e 20 31 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66  n 1;.  }.#ifndef
1d43a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45   SQLITE_OMIT_VIE
1d43b 57 0a 20 20 69 66 28 20 21 76 69 65 77 4f 6b 20  W.  if( !viewOk 
1d43c 26 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  && pTab->pSelect
1d43d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
1d43e 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22  rrorMsg(pParse,"
1d43f 63 61 6e 6e 6f 74 20 6d 6f 64 69 66 79 20 25 73  cannot modify %s
1d440 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20 61   because it is a
1d441 20 76 69 65 77 22 2c 70 54 61 62 2d 3e 7a 4e 61   view",pTab->zNa
1d442 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  me);.    return 
1d443 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  1;.  }.#endif.  
1d444 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
1d445 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
1d446 20 74 68 61 74 20 77 69 6c 6c 20 6f 70 65 6e 20   that will open 
1d447 61 20 74 61 62 6c 65 20 66 6f 72 20 72 65 61 64  a table for read
1d448 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ing..*/.SQLITE_P
1d449 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1d44a 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 0a 20 20  te3OpenTable(.  
1d44b 50 61 72 73 65 20 2a 70 2c 20 20 20 20 20 20 20  Parse *p,       
1d44c 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
1d44d 20 69 6e 74 6f 20 74 68 69 73 20 56 44 42 45 20   into this VDBE 
1d44e 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20  */.  int iCur,  
1d44f 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73       /* The curs
1d450 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  or number of the
1d451 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
1d452 69 44 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 54  iDb,        /* T
1d453 68 65 20 64 61 74 61 62 61 73 65 20 69 6e 64 65  he database inde
1d454 78 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 44 62  x in sqlite3.aDb
1d455 5b 5d 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  [] */.  Table *p
1d456 54 61 62 2c 20 20 20 20 2f 2a 20 54 68 65 20 74  Tab,    /* The t
1d457 61 62 6c 65 20 74 6f 20 62 65 20 6f 70 65 6e 65  able to be opene
1d458 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 63 6f 64  d */.  int opcod
1d459 65 20 20 20 20 20 20 2f 2a 20 4f 50 5f 4f 70 65  e      /* OP_Ope
1d45a 6e 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70 65 6e  nRead or OP_Open
1d45b 57 72 69 74 65 20 2a 2f 0a 29 7b 0a 20 20 56 64  Write */.){.  Vd
1d45c 62 65 20 2a 76 3b 0a 20 20 69 66 28 20 49 73 56  be *v;.  if( IsV
1d45d 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 20 72  irtual(pTab) ) r
1d45e 65 74 75 72 6e 3b 0a 20 20 76 20 3d 20 73 71 6c  eturn;.  v = sql
1d45f 69 74 65 33 47 65 74 56 64 62 65 28 70 29 3b 0a  ite3GetVdbe(p);.
1d460 20 20 61 73 73 65 72 74 28 20 6f 70 63 6f 64 65    assert( opcode
1d461 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 7c  ==OP_OpenWrite |
1d462 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65  | opcode==OP_Ope
1d463 6e 52 65 61 64 20 29 3b 0a 20 20 73 71 6c 69 74  nRead );.  sqlit
1d464 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69  e3TableLock(p, i
1d465 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20  Db, pTab->tnum, 
1d466 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e  (opcode==OP_Open
1d467 57 72 69 74 65 29 2c 20 70 54 61 62 2d 3e 7a 4e  Write), pTab->zN
1d468 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  ame);.  sqlite3V
1d469 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1d46a 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30  SetNumColumns, 0
1d46b 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20  , pTab->nCol);. 
1d46c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1d46d 70 33 28 76 2c 20 6f 70 63 6f 64 65 2c 20 69 43  p3(v, opcode, iC
1d46e 75 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20  ur, pTab->tnum, 
1d46f 69 44 62 29 3b 0a 20 20 56 64 62 65 43 6f 6d 6d  iDb);.  VdbeComm
1d470 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70 54  ent((v, "%s", pT
1d471 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 7d 0a 0a  ab->zName));.}..
1d472 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
1d473 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20  LITE_OMIT_VIEW) 
1d474 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
1d475 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29  TE_OMIT_TRIGGER)
1d476 0a 2f 2a 0a 2a 2a 20 45 76 61 6c 75 61 74 65 20  ./*.** Evaluate 
1d477 61 20 76 69 65 77 20 61 6e 64 20 73 74 6f 72 65  a view and store
1d478 20 69 74 73 20 72 65 73 75 6c 74 20 69 6e 20 61   its result in a
1d479 6e 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c  n ephemeral tabl
1d47a 65 2e 20 20 54 68 65 0a 2a 2a 20 70 57 68 65 72  e.  The.** pWher
1d47b 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e  e argument is an
1d47c 20 6f 70 74 69 6f 6e 61 6c 20 57 48 45 52 45 20   optional WHERE 
1d47d 63 6c 61 75 73 65 20 74 68 61 74 20 72 65 73 74  clause that rest
1d47e 72 69 63 74 73 20 74 68 65 0a 2a 2a 20 73 65 74  ricts the.** set
1d47f 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20   of rows in the 
1d480 76 69 65 77 20 74 68 61 74 20 61 72 65 20 74 6f  view that are to
1d481 20 62 65 20 61 64 64 65 64 20 74 6f 20 74 68 65   be added to the
1d482 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65   ephemeral table
1d483 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1d484 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1d485 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65 77 28  MaterializeView(
1d486 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
1d487 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69  ,       /* Parsi
1d488 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
1d489 53 65 6c 65 63 74 20 2a 70 56 69 65 77 2c 20 20  Select *pView,  
1d48a 20 20 20 20 20 2f 2a 20 56 69 65 77 20 64 65 66       /* View def
1d48b 69 6e 69 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70  inition */.  Exp
1d48c 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20 20 20  r *pWhere,      
1d48d 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 57 48    /* Optional WH
1d48e 45 52 45 20 63 6c 61 75 73 65 20 74 6f 20 62 65  ERE clause to be
1d48f 20 61 64 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20   added */.  int 
1d490 69 43 75 72 20 20 20 20 20 20 20 20 20 20 20 20  iCur            
1d491 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65   /* Cursor numbe
1d492 72 20 66 6f 72 20 65 70 68 65 6d 65 72 69 61 6c  r for ephemerial
1d493 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 53   table */.){.  S
1d494 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b 0a  electDest dest;.
1d495 20 20 53 65 6c 65 63 74 20 2a 70 44 75 70 3b 0a    Select *pDup;.
1d496 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1d497 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 70  pParse->db;..  p
1d498 44 75 70 20 3d 20 73 71 6c 69 74 65 33 53 65 6c  Dup = sqlite3Sel
1d499 65 63 74 44 75 70 28 64 62 2c 20 70 56 69 65 77  ectDup(db, pView
1d49a 29 3b 0a 20 20 69 66 28 20 70 57 68 65 72 65 20  );.  if( pWhere 
1d49b 29 7b 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a  ){.    SrcList *
1d49c 70 46 72 6f 6d 3b 0a 20 20 20 20 0a 20 20 20 20  pFrom;.    .    
1d49d 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33  pWhere = sqlite3
1d49e 45 78 70 72 44 75 70 28 64 62 2c 20 70 57 68 65  ExprDup(db, pWhe
1d49f 72 65 29 3b 0a 20 20 20 20 70 46 72 6f 6d 20 3d  re);.    pFrom =
1d4a0 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
1d4a1 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28 70 50  ppendFromTerm(pP
1d4a2 61 72 73 65 2c 20 30 2c 20 30 2c 20 30 2c 20 30  arse, 0, 0, 0, 0
1d4a3 2c 20 70 44 75 70 2c 20 30 2c 20 30 29 3b 0a 20  , pDup, 0, 0);. 
1d4a4 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65     pDup = sqlite
1d4a5 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73  3SelectNew(pPars
1d4a6 65 2c 20 30 2c 20 70 46 72 6f 6d 2c 20 70 57 68  e, 0, pFrom, pWh
1d4a7 65 72 65 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  ere, 0, 0, 0, 0,
1d4a8 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 73 71   0, 0);.  }.  sq
1d4a9 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49  lite3SelectDestI
1d4aa 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54 5f 45  nit(&dest, SRT_E
1d4ab 70 68 65 6d 54 61 62 2c 20 69 43 75 72 29 3b 0a  phemTab, iCur);.
1d4ac 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28    sqlite3Select(
1d4ad 70 50 61 72 73 65 2c 20 70 44 75 70 2c 20 26 64  pParse, pDup, &d
1d4ae 65 73 74 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29  est, 0, 0, 0, 0)
1d4af 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ;.  sqlite3Selec
1d4b0 74 44 65 6c 65 74 65 28 70 44 75 70 29 3b 0a 7d  tDelete(pDup);.}
1d4b1 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
1d4b2 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
1d4b3 56 49 45 57 29 20 26 26 20 21 64 65 66 69 6e 65  VIEW) && !define
1d4b4 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  d(SQLITE_OMIT_TR
1d4b5 49 47 47 45 52 29 20 2a 2f 0a 0a 0a 2f 2a 0a 2a  IGGER) */.../*.*
1d4b6 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
1d4b7 66 6f 72 20 61 20 44 45 4c 45 54 45 20 46 52 4f  for a DELETE FRO
1d4b8 4d 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a  M statement..**.
1d4b9 2a 2a 20 20 20 20 20 44 45 4c 45 54 45 20 46 52  **     DELETE FR
1d4ba 4f 4d 20 74 61 62 6c 65 5f 77 78 79 7a 20 57 48  OM table_wxyz WH
1d4bb 45 52 45 20 61 3c 35 20 41 4e 44 20 62 20 4e 4f  ERE a<5 AND b NO
1d4bc 54 20 4e 55 4c 4c 3b 0a 2a 2a 20 20 20 20 20 20  T NULL;.**      
1d4bd 20 20 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f             \____
1d4be 5f 5f 5f 5f 2f 20 20 20 20 20 20 20 5c 5f 5f 5f  ____/       \___
1d4bf 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a  _____________/.*
1d4c0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
1d4c1 20 20 20 70 54 61 62 4c 69 73 74 20 20 20 20 20     pTabList     
1d4c2 20 20 20 20 20 20 20 20 20 70 57 68 65 72 65 0a           pWhere.
1d4c3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1d4c4 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65  E void sqlite3De
1d4c5 6c 65 74 65 46 72 6f 6d 28 0a 20 20 50 61 72 73  leteFrom(.  Pars
1d4c6 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
1d4c7 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
1d4c8 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72   context */.  Sr
1d4c9 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c  cList *pTabList,
1d4ca 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
1d4cb 65 20 66 72 6f 6d 20 77 68 69 63 68 20 77 65 20  e from which we 
1d4cc 73 68 6f 75 6c 64 20 64 65 6c 65 74 65 20 74 68  should delete th
1d4cd 69 6e 67 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a  ings */.  Expr *
1d4ce 70 57 68 65 72 65 20 20 20 20 20 20 20 20 20 20  pWhere          
1d4cf 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c   /* The WHERE cl
1d4d0 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e 75  ause.  May be nu
1d4d1 6c 6c 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20  ll */.){.  Vdbe 
1d4d2 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *v;             
1d4d3 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c    /* The virtual
1d4d4 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65   database engine
1d4d5 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
1d4d6 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  b;           /* 
1d4d7 54 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77  The table from w
1d4d8 68 69 63 68 20 72 65 63 6f 72 64 73 20 77 69 6c  hich records wil
1d4d9 6c 20 62 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a  l be deleted */.
1d4da 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
1d4db 62 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  b;       /* Name
1d4dc 20 6f 66 20 64 61 74 61 62 61 73 65 20 68 6f 6c   of database hol
1d4dd 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20 69  ding pTab */.  i
1d4de 6e 74 20 65 6e 64 2c 20 61 64 64 72 20 3d 20 30  nt end, addr = 0
1d4df 3b 20 20 20 20 20 2f 2a 20 41 20 63 6f 75 70 6c  ;     /* A coupl
1d4e0 65 20 61 64 64 72 65 73 73 65 73 20 6f 66 20 67  e addresses of g
1d4e1 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20 2a 2f  enerated code */
1d4e2 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
1d4e3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
1d4e4 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 57  p counter */.  W
1d4e5 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f  hereInfo *pWInfo
1d4e6 3b 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61  ;     /* Informa
1d4e7 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 57  tion about the W
1d4e8 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
1d4e9 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20   Index *pIdx;   
1d4ea 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c          /* For l
1d4eb 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69 6e 64 69  ooping over indi
1d4ec 63 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ces of the table
1d4ed 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b 20   */.  int iCur; 
1d4ee 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1d4ef 56 44 42 45 20 43 75 72 73 6f 72 20 6e 75 6d 62  VDBE Cursor numb
1d4f0 65 72 20 66 6f 72 20 70 54 61 62 20 2a 2f 0a 20  er for pTab */. 
1d4f1 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20   sqlite3 *db;   
1d4f2 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 69 6e 20          /* Main 
1d4f3 64 61 74 61 62 61 73 65 20 73 74 72 75 63 74 75  database structu
1d4f4 72 65 20 2a 2f 0a 20 20 41 75 74 68 43 6f 6e 74  re */.  AuthCont
1d4f5 65 78 74 20 73 43 6f 6e 74 65 78 74 3b 20 20 2f  ext sContext;  /
1d4f6 2a 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  * Authorization 
1d4f7 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74  context */.  int
1d4f8 20 6f 6c 64 49 64 78 20 3d 20 2d 31 3b 20 20 20   oldIdx = -1;   
1d4f9 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 66 6f      /* Cursor fo
1d4fa 72 20 74 68 65 20 4f 4c 44 20 74 61 62 6c 65 20  r the OLD table 
1d4fb 6f 66 20 41 46 54 45 52 20 74 72 69 67 67 65 72  of AFTER trigger
1d4fc 73 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65  s */.  NameConte
1d4fd 78 74 20 73 4e 43 3b 20 20 20 20 20 20 20 2f 2a  xt sNC;       /*
1d4fe 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 74 6f   Name context to
1d4ff 20 72 65 73 6f 6c 76 65 20 65 78 70 72 65 73 73   resolve express
1d500 69 6f 6e 73 20 69 6e 20 2a 2f 0a 20 20 69 6e 74  ions in */.  int
1d501 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20   iDb;           
1d502 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
1d503 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20  number */.  int 
1d504 6d 65 6d 43 6e 74 20 3d 20 30 3b 20 20 20 20 20  memCnt = 0;     
1d505 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c     /* Memory cel
1d506 6c 20 75 73 65 64 20 66 6f 72 20 63 68 61 6e 67  l used for chang
1d507 65 20 63 6f 75 6e 74 69 6e 67 20 2a 2f 0a 0a 23  e counting */..#
1d508 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1d509 49 54 5f 54 52 49 47 47 45 52 0a 20 20 69 6e 74  IT_TRIGGER.  int
1d50a 20 69 73 56 69 65 77 3b 20 20 20 20 20 20 20 20   isView;        
1d50b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
1d50c 65 20 69 66 20 61 74 74 65 6d 70 74 69 6e 67 20  e if attempting 
1d50d 74 6f 20 64 65 6c 65 74 65 20 66 72 6f 6d 20 61  to delete from a
1d50e 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 74   view */.  int t
1d50f 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 3d 20  riggers_exist = 
1d510 30 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  0;      /* True 
1d511 69 66 20 61 6e 79 20 74 72 69 67 67 65 72 73 20  if any triggers 
1d512 65 78 69 73 74 20 2a 2f 0a 23 65 6e 64 69 66 0a  exist */.#endif.
1d513 20 20 69 6e 74 20 69 42 65 67 69 6e 41 66 74 65    int iBeginAfte
1d514 72 54 72 69 67 67 65 72 3b 20 20 20 20 20 20 2f  rTrigger;      /
1d515 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 66 74  * Address of aft
1d516 65 72 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  er trigger progr
1d517 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 45 6e 64  am */.  int iEnd
1d518 41 66 74 65 72 54 72 69 67 67 65 72 3b 20 20 20  AfterTrigger;   
1d519 20 20 20 20 20 2f 2a 20 45 78 69 74 20 6f 66 20       /* Exit of 
1d51a 61 66 74 65 72 20 74 72 69 67 67 65 72 20 70 72  after trigger pr
1d51b 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 69  ogram */.  int i
1d51c 42 65 67 69 6e 42 65 66 6f 72 65 54 72 69 67 67  BeginBeforeTrigg
1d51d 65 72 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 65  er;     /* Addre
1d51e 73 73 20 6f 66 20 62 65 66 6f 72 65 20 74 72 69  ss of before tri
1d51f 67 67 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a  gger program */.
1d520 20 20 69 6e 74 20 69 45 6e 64 42 65 66 6f 72 65    int iEndBefore
1d521 54 72 69 67 67 65 72 3b 20 20 20 20 20 20 20 2f  Trigger;       /
1d522 2a 20 45 78 69 74 20 6f 66 20 62 65 66 6f 72 65  * Exit of before
1d523 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d   trigger program
1d524 20 2a 2f 0a 20 20 75 33 32 20 6f 6c 64 5f 63 6f   */.  u32 old_co
1d525 6c 5f 6d 61 73 6b 20 3d 20 30 3b 20 20 20 20 20  l_mask = 0;     
1d526 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 4f 4c     /* Mask of OL
1d527 44 2e 2a 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 75  D.* columns in u
1d528 73 65 20 2a 2f 0a 0a 20 20 73 43 6f 6e 74 65 78  se */..  sContex
1d529 74 2e 70 50 61 72 73 65 20 3d 20 30 3b 0a 20 20  t.pParse = 0;.  
1d52a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
1d52b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
1d52c 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  Err || db->mallo
1d52d 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67  cFailed ){.    g
1d52e 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f  oto delete_from_
1d52f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 61  cleanup;.  }.  a
1d530 73 73 65 72 74 28 20 70 54 61 62 4c 69 73 74 2d  ssert( pTabList-
1d531 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 0a 20 20 2f  >nSrc==1 );..  /
1d532 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61 62  * Locate the tab
1d533 6c 65 20 77 68 69 63 68 20 77 65 20 77 61 6e 74  le which we want
1d534 20 74 6f 20 64 65 6c 65 74 65 2e 20 20 54 68 69   to delete.  Thi
1d535 73 20 74 61 62 6c 65 20 68 61 73 20 74 6f 20 62  s table has to b
1d536 65 0a 20 20 2a 2a 20 70 75 74 20 69 6e 20 61 6e  e.  ** put in an
1d537 20 53 72 63 4c 69 73 74 20 73 74 72 75 63 74 75   SrcList structu
1d538 72 65 20 62 65 63 61 75 73 65 20 73 6f 6d 65 20  re because some 
1d539 6f 66 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e  of the subroutin
1d53a 65 73 20 77 65 0a 20 20 2a 2a 20 77 69 6c 6c 20  es we.  ** will 
1d53b 62 65 20 63 61 6c 6c 69 6e 67 20 61 72 65 20 64  be calling are d
1d53c 65 73 69 67 6e 65 64 20 74 6f 20 77 6f 72 6b 20  esigned to work 
1d53d 77 69 74 68 20 6d 75 6c 74 69 70 6c 65 20 74 61  with multiple ta
1d53e 62 6c 65 73 20 61 6e 64 20 65 78 70 65 63 74 0a  bles and expect.
1d53f 20 20 2a 2a 20 61 6e 20 53 72 63 4c 69 73 74 2a    ** an SrcList*
1d540 20 70 61 72 61 6d 65 74 65 72 20 69 6e 73 74 65   parameter inste
1d541 61 64 20 6f 66 20 6a 75 73 74 20 61 20 54 61 62  ad of just a Tab
1d542 6c 65 2a 20 70 61 72 61 6d 65 74 65 72 2e 0a 20  le* parameter.. 
1d543 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 73 71 6c   */.  pTab = sql
1d544 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75  ite3SrcListLooku
1d545 70 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69  p(pParse, pTabLi
1d546 73 74 29 3b 0a 20 20 69 66 28 20 70 54 61 62 3d  st);.  if( pTab=
1d547 3d 30 20 29 20 20 67 6f 74 6f 20 64 65 6c 65 74  =0 )  goto delet
1d548 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a  e_from_cleanup;.
1d549 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74  .  /* Figure out
1d54a 20 69 66 20 77 65 20 68 61 76 65 20 61 6e 79 20   if we have any 
1d54b 74 72 69 67 67 65 72 73 20 61 6e 64 20 69 66 20  triggers and if 
1d54c 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 0a  the table being.
1d54d 20 20 2a 2a 20 64 65 6c 65 74 65 64 20 66 72 6f    ** deleted fro
1d54e 6d 20 69 73 20 61 20 76 69 65 77 0a 20 20 2a 2f  m is a view.  */
1d54f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1d550 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 74  OMIT_TRIGGER.  t
1d551 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 3d 20  riggers_exist = 
1d552 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 73 45  sqlite3TriggersE
1d553 78 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 61  xist(pParse, pTa
1d554 62 2c 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30 29  b, TK_DELETE, 0)
1d555 3b 0a 20 20 69 73 56 69 65 77 20 3d 20 70 54 61  ;.  isView = pTa
1d556 62 2d 3e 70 53 65 6c 65 63 74 21 3d 30 3b 0a 23  b->pSelect!=0;.#
1d557 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 74 72  else.# define tr
1d558 69 67 67 65 72 73 5f 65 78 69 73 74 20 30 0a 23  iggers_exist 0.#
1d559 20 64 65 66 69 6e 65 20 69 73 56 69 65 77 20 30   define isView 0
1d55a 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
1d55b 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a  QLITE_OMIT_VIEW.
1d55c 23 20 75 6e 64 65 66 20 69 73 56 69 65 77 0a 23  # undef isView.#
1d55d 20 64 65 66 69 6e 65 20 69 73 56 69 65 77 20 30   define isView 0
1d55e 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 73  .#endif..  if( s
1d55f 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79  qlite3IsReadOnly
1d560 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 74  (pParse, pTab, t
1d561 72 69 67 67 65 72 73 5f 65 78 69 73 74 29 20 29  riggers_exist) )
1d562 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74  {.    goto delet
1d563 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a  e_from_cleanup;.
1d564 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69    }.  iDb = sqli
1d565 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
1d566 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  (db, pTab->pSche
1d567 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  ma);.  assert( i
1d568 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  Db<db->nDb );.  
1d569 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44  zDb = db->aDb[iD
1d56a 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20  b].zName;.  if( 
1d56b 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
1d56c 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
1d56d 44 45 4c 45 54 45 2c 20 70 54 61 62 2d 3e 7a 4e  DELETE, pTab->zN
1d56e 61 6d 65 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a  ame, 0, zDb) ){.
1d56f 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f      goto delete_
1d570 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  from_cleanup;.  
1d571 7d 0a 0a 20 20 2f 2a 20 49 66 20 70 54 61 62 20  }..  /* If pTab 
1d572 69 73 20 72 65 61 6c 6c 79 20 61 20 76 69 65 77  is really a view
1d573 2c 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 68  , make sure it h
1d574 61 73 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69  as been initiali
1d575 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  zed..  */.  if( 
1d576 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f  sqlite3ViewGetCo
1d577 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65  lumnNames(pParse
1d578 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20 67  , pTab) ){.    g
1d579 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f  oto delete_from_
1d57a 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20  cleanup;.  }..  
1d57b 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 63 75  /* Allocate a cu
1d57c 72 73 6f 72 20 75 73 65 64 20 74 6f 20 73 74 6f  rsor used to sto
1d57d 72 65 20 74 68 65 20 6f 6c 64 2e 2a 20 64 61 74  re the old.* dat
1d57e 61 20 66 6f 72 20 61 20 74 72 69 67 67 65 72 2e  a for a trigger.
1d57f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 74 72 69 67  .  */.  if( trig
1d580 67 65 72 73 5f 65 78 69 73 74 20 29 7b 20 0a 20  gers_exist ){ . 
1d581 20 20 20 6f 6c 64 49 64 78 20 3d 20 70 50 61 72     oldIdx = pPar
1d582 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a  se->nTab++;.  }.
1d583 0a 20 20 2f 2a 20 41 73 73 69 67 6e 20 20 63 75  .  /* Assign  cu
1d584 72 73 6f 72 20 6e 75 6d 62 65 72 20 74 6f 20 74  rsor number to t
1d585 68 65 20 74 61 62 6c 65 20 61 6e 64 20 61 6c 6c  he table and all
1d586 20 69 74 73 20 69 6e 64 69 63 65 73 2e 0a 20 20   its indices..  
1d587 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54 61  */.  assert( pTa
1d588 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20 29  bList->nSrc==1 )
1d589 3b 0a 20 20 69 43 75 72 20 3d 20 70 54 61 62 4c  ;.  iCur = pTabL
1d58a 69 73 74 2d 3e 61 5b 30 5d 2e 69 43 75 72 73 6f  ist->a[0].iCurso
1d58b 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  r = pParse->nTab
1d58c 2b 2b 3b 0a 20 20 66 6f 72 28 70 49 64 78 3d 70  ++;.  for(pIdx=p
1d58d 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
1d58e 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
1d58f 65 78 74 29 7b 0a 20 20 20 20 70 50 61 72 73 65  ext){.    pParse
1d590 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 20  ->nTab++;.  }.. 
1d591 20 2f 2a 20 53 74 61 72 74 20 74 68 65 20 76 69   /* Start the vi
1d592 65 77 20 63 6f 6e 74 65 78 74 0a 20 20 2a 2f 0a  ew context.  */.
1d593 20 20 69 66 28 20 69 73 56 69 65 77 20 29 7b 0a    if( isView ){.
1d594 20 20 20 20 73 71 6c 69 74 65 33 41 75 74 68 43      sqlite3AuthC
1d595 6f 6e 74 65 78 74 50 75 73 68 28 70 50 61 72 73  ontextPush(pPars
1d596 65 2c 20 26 73 43 6f 6e 74 65 78 74 2c 20 70 54  e, &sContext, pT
1d597 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a  ab->zName);.  }.
1d598 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67 65 6e 65  .  /* Begin gene
1d599 72 61 74 69 6e 67 20 63 6f 64 65 2e 0a 20 20 2a  rating code..  *
1d59a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  /.  v = sqlite3G
1d59b 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
1d59c 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20    if( v==0 ){.  
1d59d 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72    goto delete_fr
1d59e 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  om_cleanup;.  }.
1d59f 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65    if( pParse->ne
1d5a0 73 74 65 64 3d 3d 30 20 29 20 73 71 6c 69 74 65  sted==0 ) sqlite
1d5a1 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65  3VdbeCountChange
1d5a2 73 28 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  s(v);.  sqlite3B
1d5a3 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
1d5a4 6f 6e 28 70 50 61 72 73 65 2c 20 74 72 69 67 67  on(pParse, trigg
1d5a5 65 72 73 5f 65 78 69 73 74 2c 20 69 44 62 29 3b  ers_exist, iDb);
1d5a6 0a 0a 20 20 69 66 28 20 74 72 69 67 67 65 72 73  ..  if( triggers
1d5a7 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20 69 6e  _exist ){.    in
1d5a8 74 20 6f 72 63 6f 6e 66 20 3d 20 28 28 70 50 61  t orconf = ((pPa
1d5a9 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 29 3f  rse->trigStack)?
1d5aa 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63  pParse->trigStac
1d5ab 6b 2d 3e 6f 72 63 6f 6e 66 3a 4f 45 5f 44 65 66  k->orconf:OE_Def
1d5ac 61 75 6c 74 29 3b 0a 20 20 20 20 69 6e 74 20 69  ault);.    int i
1d5ad 47 6f 74 6f 20 3d 20 73 71 6c 69 74 65 33 56 64  Goto = sqlite3Vd
1d5ae 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47  beAddOp0(v, OP_G
1d5af 6f 74 6f 29 3b 0a 20 20 20 20 61 64 64 72 20 3d  oto);.    addr =
1d5b0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
1d5b1 4c 61 62 65 6c 28 76 29 3b 0a 0a 20 20 20 20 69  Label(v);..    i
1d5b2 42 65 67 69 6e 42 65 66 6f 72 65 54 72 69 67 67  BeginBeforeTrigg
1d5b3 65 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  er = sqlite3Vdbe
1d5b4 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
1d5b5 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
1d5b6 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28  3CodeRowTrigger(
1d5b7 70 50 61 72 73 65 2c 20 54 4b 5f 44 45 4c 45 54  pParse, TK_DELET
1d5b8 45 2c 20 30 2c 20 54 52 49 47 47 45 52 5f 42 45  E, 0, TRIGGER_BE
1d5b9 46 4f 52 45 2c 20 70 54 61 62 2c 0a 20 20 20 20  FORE, pTab,.    
1d5ba 20 20 20 20 2d 31 2c 20 6f 6c 64 49 64 78 2c 20      -1, oldIdx, 
1d5bb 6f 72 63 6f 6e 66 2c 20 61 64 64 72 2c 20 26 6f  orconf, addr, &o
1d5bc 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 2c 20 30 29 3b  ld_col_mask, 0);
1d5bd 0a 20 20 20 20 69 45 6e 64 42 65 66 6f 72 65 54  .    iEndBeforeT
1d5be 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33  rigger = sqlite3
1d5bf 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50  VdbeAddOp0(v, OP
1d5c0 5f 47 6f 74 6f 29 3b 0a 0a 20 20 20 20 69 42 65  _Goto);..    iBe
1d5c1 67 69 6e 41 66 74 65 72 54 72 69 67 67 65 72 20  ginAfterTrigger 
1d5c2 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
1d5c3 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20  rentAddr(v);.   
1d5c4 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 43 6f   (void)sqlite3Co
1d5c5 64 65 52 6f 77 54 72 69 67 67 65 72 28 70 50 61  deRowTrigger(pPa
1d5c6 72 73 65 2c 20 54 4b 5f 44 45 4c 45 54 45 2c 20  rse, TK_DELETE, 
1d5c7 30 2c 20 54 52 49 47 47 45 52 5f 41 46 54 45 52  0, TRIGGER_AFTER
1d5c8 2c 20 70 54 61 62 2c 20 2d 31 2c 0a 20 20 20 20  , pTab, -1,.    
1d5c9 20 20 20 20 6f 6c 64 49 64 78 2c 20 6f 72 63 6f      oldIdx, orco
1d5ca 6e 66 2c 20 61 64 64 72 2c 20 26 6f 6c 64 5f 63  nf, addr, &old_c
1d5cb 6f 6c 5f 6d 61 73 6b 2c 20 30 29 3b 0a 20 20 20  ol_mask, 0);.   
1d5cc 20 69 45 6e 64 41 66 74 65 72 54 72 69 67 67 65   iEndAfterTrigge
1d5cd 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
1d5ce 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp0(v, OP_Goto
1d5cf 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56  );..    sqlite3V
1d5d0 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69  dbeJumpHere(v, i
1d5d1 47 6f 74 6f 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  Goto);.  }..  /*
1d5d2 20 49 66 20 77 65 20 61 72 65 20 74 72 79 69 6e   If we are tryin
1d5d3 67 20 74 6f 20 64 65 6c 65 74 65 20 66 72 6f 6d  g to delete from
1d5d4 20 61 20 76 69 65 77 2c 20 72 65 61 6c 69 7a 65   a view, realize
1d5d5 20 74 68 61 74 20 76 69 65 77 20 69 6e 74 6f 0a   that view into.
1d5d6 20 20 2a 2a 20 61 20 65 70 68 65 6d 65 72 61 6c    ** a ephemeral
1d5d7 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69   table..  */.  i
1d5d8 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20  f( isView ){.   
1d5d9 20 73 71 6c 69 74 65 33 4d 61 74 65 72 69 61 6c   sqlite3Material
1d5da 69 7a 65 56 69 65 77 28 70 50 61 72 73 65 2c 20  izeView(pParse, 
1d5db 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 2c 20 70  pTab->pSelect, p
1d5dc 57 68 65 72 65 2c 20 69 43 75 72 29 3b 0a 20 20  Where, iCur);.  
1d5dd 7d 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20  }..  /* Resolve 
1d5de 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  the column names
1d5df 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c   in the WHERE cl
1d5e0 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d  ause..  */.  mem
1d5e1 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a  set(&sNC, 0, siz
1d5e2 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 73 4e 43  eof(sNC));.  sNC
1d5e3 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
1d5e4 3b 0a 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74  ;.  sNC.pSrcList
1d5e5 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a 20 20 69   = pTabList;.  i
1d5e6 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 52 65  f( sqlite3ExprRe
1d5e7 73 6f 6c 76 65 4e 61 6d 65 73 28 26 73 4e 43 2c  solveNames(&sNC,
1d5e8 20 70 57 68 65 72 65 29 20 29 7b 0a 20 20 20 20   pWhere) ){.    
1d5e9 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d  goto delete_from
1d5ea 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20  _cleanup;.  }.. 
1d5eb 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74   /* Initialize t
1d5ec 68 65 20 63 6f 75 6e 74 65 72 20 6f 66 20 74 68  he counter of th
1d5ed 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
1d5ee 20 64 65 6c 65 74 65 64 2c 20 69 66 0a 20 20 2a   deleted, if.  *
1d5ef 2a 20 77 65 20 61 72 65 20 63 6f 75 6e 74 69 6e  * we are countin
1d5f0 67 20 72 6f 77 73 2e 0a 20 20 2a 2f 0a 20 20 69  g rows..  */.  i
1d5f1 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  f( db->flags & S
1d5f2 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20  QLITE_CountRows 
1d5f3 29 7b 0a 20 20 20 20 6d 65 6d 43 6e 74 20 3d 20  ){.    memCnt = 
1d5f4 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
1d5f5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1d5f6 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
1d5f7 67 65 72 2c 20 30 2c 20 6d 65 6d 43 6e 74 29 3b  ger, 0, memCnt);
1d5f8 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 70 65 63 69  .  }..  /* Speci
1d5f9 61 6c 20 63 61 73 65 3a 20 41 20 44 45 4c 45 54  al case: A DELET
1d5fa 45 20 77 69 74 68 6f 75 74 20 61 20 57 48 45 52  E without a WHER
1d5fb 45 20 63 6c 61 75 73 65 20 64 65 6c 65 74 65 73  E clause deletes
1d5fc 20 65 76 65 72 79 74 68 69 6e 67 2e 0a 20 20 2a   everything..  *
1d5fd 2a 20 49 74 20 69 73 20 65 61 73 69 65 72 20 6a  * It is easier j
1d5fe 75 73 74 20 74 6f 20 65 72 61 73 65 20 74 68 65  ust to erase the
1d5ff 20 77 68 6f 6c 65 20 74 61 62 6c 65 2e 20 20 4e   whole table.  N
1d600 6f 74 65 2c 20 68 6f 77 65 76 65 72 2c 20 74 68  ote, however, th
1d601 61 74 0a 20 20 2a 2a 20 74 68 69 73 20 6d 65 61  at.  ** this mea
1d602 6e 73 20 74 68 61 74 20 74 68 65 20 72 6f 77 20  ns that the row 
1d603 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 77 69 6c  change count wil
1d604 6c 20 62 65 20 69 6e 63 6f 72 72 65 63 74 2e 0a  l be incorrect..
1d605 20 20 2a 2f 0a 20 20 69 66 28 20 70 57 68 65 72    */.  if( pWher
1d606 65 3d 3d 30 20 26 26 20 21 74 72 69 67 67 65 72  e==0 && !trigger
1d607 73 5f 65 78 69 73 74 20 26 26 20 21 49 73 56 69  s_exist && !IsVi
1d608 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20  rtual(pTab) ){. 
1d609 20 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73     if( db->flags
1d60a 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52   & SQLITE_CountR
1d60b 6f 77 73 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ows ){.      /* 
1d60c 49 66 20 63 6f 75 6e 74 69 6e 67 20 72 6f 77 73  If counting rows
1d60d 20 64 65 6c 65 74 65 64 2c 20 6a 75 73 74 20 63   deleted, just c
1d60e 6f 75 6e 74 20 74 68 65 20 74 6f 74 61 6c 20 6e  ount the total n
1d60f 75 6d 62 65 72 20 6f 66 0a 20 20 20 20 20 20 2a  umber of.      *
1d610 2a 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65  * entries in the
1d611 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 20   table. */.     
1d612 20 69 6e 74 20 61 64 64 72 32 3b 0a 20 20 20 20   int addr2;.    
1d613 20 20 69 66 28 20 21 69 73 56 69 65 77 20 29 7b    if( !isView ){
1d614 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1d615 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65  OpenTable(pParse
1d616 2c 20 69 43 75 72 2c 20 69 44 62 2c 20 70 54 61  , iCur, iDb, pTa
1d617 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b  b, OP_OpenRead);
1d618 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
1d619 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1d61a 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69  (v, OP_Rewind, i
1d61b 43 75 72 2c 20 73 71 6c 69 74 65 33 56 64 62 65  Cur, sqlite3Vdbe
1d61c 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 2b 32  CurrentAddr(v)+2
1d61d 29 3b 0a 20 20 20 20 20 20 61 64 64 72 32 20 3d  );.      addr2 =
1d61e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1d61f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c  p2(v, OP_AddImm,
1d620 20 6d 65 6d 43 6e 74 2c 20 31 29 3b 0a 20 20 20   memCnt, 1);.   
1d621 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1d622 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c  dOp2(v, OP_Next,
1d623 20 69 43 75 72 2c 20 61 64 64 72 32 29 3b 0a 20   iCur, addr2);. 
1d624 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1d625 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp1(v, OP_Clo
1d626 73 65 2c 20 69 43 75 72 29 3b 0a 20 20 20 20 7d  se, iCur);.    }
1d627 0a 20 20 20 20 69 66 28 20 21 69 73 56 69 65 77  .    if( !isView
1d628 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1d629 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1d62a 50 5f 43 6c 65 61 72 2c 20 70 54 61 62 2d 3e 74  P_Clear, pTab->t
1d62b 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  num, iDb);.     
1d62c 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e 6e 65   if( !pParse->ne
1d62d 73 74 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20  sted ){.        
1d62e 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1d62f 65 50 34 28 76 2c 20 2d 31 2c 20 70 54 61 62 2d  eP4(v, -1, pTab-
1d630 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49  >zName, P4_STATI
1d631 43 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  C);.      }.    
1d632 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d    for(pIdx=pTab-
1d633 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
1d634 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
1d635 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
1d636 28 20 70 49 64 78 2d 3e 70 53 63 68 65 6d 61 3d  ( pIdx->pSchema=
1d637 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29  =pTab->pSchema )
1d638 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1d639 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1d63a 50 5f 43 6c 65 61 72 2c 20 70 49 64 78 2d 3e 74  P_Clear, pIdx->t
1d63b 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  num, iDb);.     
1d63c 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 20 0a 20 20   }.    }.  } .  
1d63d 2f 2a 20 54 68 65 20 75 73 75 61 6c 20 63 61 73  /* The usual cas
1d63e 65 3a 20 54 68 65 72 65 20 69 73 20 61 20 57 48  e: There is a WH
1d63f 45 52 45 20 63 6c 61 75 73 65 20 73 6f 20 77 65  ERE clause so we
1d640 20 68 61 76 65 20 74 6f 20 73 63 61 6e 20 74 68   have to scan th
1d641 72 6f 75 67 68 0a 20 20 2a 2a 20 74 68 65 20 74  rough.  ** the t
1d642 61 62 6c 65 20 61 6e 64 20 70 69 63 6b 20 77 68  able and pick wh
1d643 69 63 68 20 72 65 63 6f 72 64 73 20 74 6f 20 64  ich records to d
1d644 65 6c 65 74 65 2e 0a 20 20 2a 2f 0a 20 20 65 6c  elete..  */.  el
1d645 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 52 6f 77  se{.    int iRow
1d646 69 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  id = ++pParse->n
1d647 4d 65 6d 3b 20 20 20 20 2f 2a 20 55 73 65 64 20  Mem;    /* Used 
1d648 66 6f 72 20 73 74 6f 72 69 6e 67 20 72 6f 77 69  for storing rowi
1d649 64 20 76 61 6c 75 65 73 2e 20 2a 2f 0a 0a 20 20  d values. */..  
1d64a 20 20 2f 2a 20 42 65 67 69 6e 20 74 68 65 20 64    /* Begin the d
1d64b 61 74 61 62 61 73 65 20 73 63 61 6e 0a 20 20 20  atabase scan.   
1d64c 20 2a 2f 0a 20 20 20 20 70 57 49 6e 66 6f 20 3d   */.    pWInfo =
1d64d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
1d64e 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c  in(pParse, pTabL
1d64f 69 73 74 2c 20 70 57 68 65 72 65 2c 20 30 2c 20  ist, pWhere, 0, 
1d650 30 29 3b 0a 20 20 20 20 69 66 28 20 70 57 49 6e  0);.    if( pWIn
1d651 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 64 65 6c  fo==0 ) goto del
1d652 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70  ete_from_cleanup
1d653 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 65 6d 62  ;..    /* Rememb
1d654 65 72 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20  er the rowid of 
1d655 65 76 65 72 79 20 69 74 65 6d 20 74 6f 20 62 65  every item to be
1d656 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a 2f   deleted..    */
1d657 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1d658 41 64 64 4f 70 32 28 76 2c 20 49 73 56 69 72 74  AddOp2(v, IsVirt
1d659 75 61 6c 28 70 54 61 62 29 20 3f 20 4f 50 5f 56  ual(pTab) ? OP_V
1d65a 52 6f 77 69 64 20 3a 20 4f 50 5f 52 6f 77 69 64  Rowid : OP_Rowid
1d65b 2c 20 69 43 75 72 2c 20 69 52 6f 77 69 64 29 3b  , iCur, iRowid);
1d65c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1d65d 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 46 69 66  AddOp1(v, OP_Fif
1d65e 6f 57 72 69 74 65 2c 20 69 52 6f 77 69 64 29 3b  oWrite, iRowid);
1d65f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 66 6c 61  .    if( db->fla
1d660 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e  gs & SQLITE_Coun
1d661 74 52 6f 77 73 20 29 7b 0a 20 20 20 20 20 20 73  tRows ){.      s
1d662 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1d663 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 6d  (v, OP_AddImm, m
1d664 65 6d 43 6e 74 2c 20 31 29 3b 0a 20 20 20 20 7d  emCnt, 1);.    }
1d665 0a 0a 20 20 20 20 2f 2a 20 45 6e 64 20 74 68 65  ..    /* End the
1d666 20 64 61 74 61 62 61 73 65 20 73 63 61 6e 20 6c   database scan l
1d667 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  oop..    */.    
1d668 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28  sqlite3WhereEnd(
1d669 70 57 49 6e 66 6f 29 3b 0a 0a 20 20 20 20 2f 2a  pWInfo);..    /*
1d66a 20 4f 70 65 6e 20 74 68 65 20 70 73 65 75 64 6f   Open the pseudo
1d66b 2d 74 61 62 6c 65 20 75 73 65 64 20 74 6f 20 73  -table used to s
1d66c 74 6f 72 65 20 4f 4c 44 20 69 66 20 74 68 65 72  tore OLD if ther
1d66d 65 20 61 72 65 20 74 72 69 67 67 65 72 73 2e 0a  e are triggers..
1d66e 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 74      */.    if( t
1d66f 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 29 7b  riggers_exist ){
1d670 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1d671 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
1d672 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c  etNumColumns, 0,
1d673 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20   pTab->nCol);.  
1d674 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1d675 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp1(v, OP_Open
1d676 50 73 65 75 64 6f 2c 20 6f 6c 64 49 64 78 29 3b  Pseudo, oldIdx);
1d677 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44  .    }..    /* D
1d678 65 6c 65 74 65 20 65 76 65 72 79 20 69 74 65 6d  elete every item
1d679 20 77 68 6f 73 65 20 6b 65 79 20 77 61 73 20 77   whose key was w
1d67a 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6c 69  ritten to the li
1d67b 73 74 20 64 75 72 69 6e 67 20 74 68 65 0a 20 20  st during the.  
1d67c 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63    ** database sc
1d67d 61 6e 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20  an.  We have to 
1d67e 64 65 6c 65 74 65 20 69 74 65 6d 73 20 61 66 74  delete items aft
1d67f 65 72 20 74 68 65 20 73 63 61 6e 20 69 73 20 63  er the scan is c
1d680 6f 6d 70 6c 65 74 65 0a 20 20 20 20 2a 2a 20 62  omplete.    ** b
1d681 65 63 61 75 73 65 20 64 65 6c 65 74 69 6e 67 20  ecause deleting 
1d682 61 6e 20 69 74 65 6d 20 63 61 6e 20 63 68 61 6e  an item can chan
1d683 67 65 20 74 68 65 20 73 63 61 6e 20 6f 72 64 65  ge the scan orde
1d684 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 65 6e  r..    */.    en
1d685 64 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  d = sqlite3VdbeM
1d686 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 20 20  akeLabel(v);..  
1d687 20 20 69 66 28 20 21 69 73 56 69 65 77 20 29 7b    if( !isView ){
1d688 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 63  .      /* Open c
1d689 75 72 73 6f 72 73 20 66 6f 72 20 74 68 65 20 74  ursors for the t
1d68a 61 62 6c 65 20 77 65 20 61 72 65 20 64 65 6c 65  able we are dele
1d68b 74 69 6e 67 20 66 72 6f 6d 20 61 6e 64 20 0a 20  ting from and . 
1d68c 20 20 20 20 20 2a 2a 20 61 6c 6c 20 69 74 73 20       ** all its 
1d68d 69 6e 64 69 63 65 73 2e 0a 20 20 20 20 20 20 2a  indices..      *
1d68e 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  /.      sqlite3O
1d68f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69 63  penTableAndIndic
1d690 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  es(pParse, pTab,
1d691 20 69 43 75 72 2c 20 4f 50 5f 4f 70 65 6e 57 72   iCur, OP_OpenWr
1d692 69 74 65 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ite);.    }..   
1d693 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 20   /* This is the 
1d694 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65  beginning of the
1d695 20 64 65 6c 65 74 65 20 6c 6f 6f 70 2e 20 49 66   delete loop. If
1d696 20 61 20 74 72 69 67 67 65 72 20 65 6e 63 6f 75   a trigger encou
1d697 6e 74 65 72 73 0a 20 20 20 20 2a 2a 20 61 6e 20  nters.    ** an 
1d698 49 47 4e 4f 52 45 20 63 6f 6e 73 74 72 61 69 6e  IGNORE constrain
1d699 74 2c 20 69 74 20 6a 75 6d 70 73 20 62 61 63 6b  t, it jumps back
1d69a 20 74 6f 20 68 65 72 65 2e 0a 20 20 20 20 2a 2f   to here..    */
1d69b 0a 20 20 20 20 69 66 28 20 74 72 69 67 67 65 72  .    if( trigger
1d69c 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20 20  s_exist ){.     
1d69d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
1d69e 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72  lveLabel(v, addr
1d69f 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 64 64  );.    }.    add
1d6a0 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
1d6a1 64 64 4f 70 32 28 76 2c 20 4f 50 5f 46 69 66 6f  ddOp2(v, OP_Fifo
1d6a2 52 65 61 64 2c 20 69 52 6f 77 69 64 2c 20 65 6e  Read, iRowid, en
1d6a3 64 29 3b 0a 0a 20 20 20 20 69 66 28 20 74 72 69  d);..    if( tri
1d6a4 67 67 65 72 73 5f 65 78 69 73 74 20 29 7b 0a 20  ggers_exist ){. 
1d6a5 20 20 20 20 20 69 6e 74 20 69 44 61 74 61 20 3d       int iData =
1d6a6 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
1d6a7 20 20 20 2f 2a 20 46 6f 72 20 73 74 6f 72 69 6e     /* For storin
1d6a8 67 20 72 6f 77 20 64 61 74 61 20 6f 66 20 4f 4c  g row data of OL
1d6a9 44 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 20 20  D table */..    
1d6aa 20 20 2f 2a 20 49 66 20 74 68 65 20 72 65 63 6f    /* If the reco
1d6ab 72 64 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20  rd is no longer 
1d6ac 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20 74  present in the t
1d6ad 61 62 6c 65 2c 20 6a 75 6d 70 20 74 6f 20 74 68  able, jump to th
1d6ae 65 0a 20 20 20 20 20 20 2a 2a 20 6e 65 78 74 20  e.      ** next 
1d6af 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  iteration of the
1d6b0 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68   loop through th
1d6b1 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
1d6b2 65 20 66 69 66 6f 2e 0a 20 20 20 20 20 20 2a 2f  e fifo..      */
1d6b3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1d6b4 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e  beAddOp3(v, OP_N
1d6b5 6f 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20  otExists, iCur, 
1d6b6 61 64 64 72 2c 20 69 52 6f 77 69 64 29 3b 0a 0a  addr, iRowid);..
1d6b7 20 20 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74        /* Populat
1d6b8 65 20 74 68 65 20 4f 4c 44 2e 2a 20 70 73 65 75  e the OLD.* pseu
1d6b9 64 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  do-table */.    
1d6ba 20 20 69 66 28 20 6f 6c 64 5f 63 6f 6c 5f 6d 61    if( old_col_ma
1d6bb 73 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  sk ){.        sq
1d6bc 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1d6bd 76 2c 20 4f 50 5f 52 6f 77 44 61 74 61 2c 20 69  v, OP_RowData, i
1d6be 43 75 72 2c 20 69 44 61 74 61 29 3b 0a 20 20 20  Cur, iData);.   
1d6bf 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1d6c0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1d6c1 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
1d6c2 30 2c 20 69 44 61 74 61 29 3b 0a 20 20 20 20 20  0, iData);.     
1d6c3 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
1d6c4 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
1d6c5 5f 49 6e 73 65 72 74 2c 20 6f 6c 64 49 64 78 2c  _Insert, oldIdx,
1d6c6 20 69 44 61 74 61 2c 20 69 52 6f 77 69 64 29 3b   iData, iRowid);
1d6c7 0a 0a 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20  ..      /* Jump 
1d6c8 62 61 63 6b 20 61 6e 64 20 72 75 6e 20 74 68 65  back and run the
1d6c9 20 42 45 46 4f 52 45 20 74 72 69 67 67 65 72 73   BEFORE triggers
1d6ca 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
1d6cb 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1d6cc 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 42 65 67 69  P_Goto, 0, iBegi
1d6cd 6e 42 65 66 6f 72 65 54 72 69 67 67 65 72 29 3b  nBeforeTrigger);
1d6ce 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1d6cf 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 45  beJumpHere(v, iE
1d6d0 6e 64 42 65 66 6f 72 65 54 72 69 67 67 65 72 29  ndBeforeTrigger)
1d6d1 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
1d6d2 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20   !isView ){.    
1d6d3 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20    /* Delete the 
1d6d4 72 6f 77 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  row */.#ifndef S
1d6d5 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1d6d6 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 69 66  ALTABLE.      if
1d6d7 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  ( IsVirtual(pTab
1d6d8 29 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e  ) ){.        con
1d6d9 73 74 20 63 68 61 72 20 2a 70 56 74 61 62 20 3d  st char *pVtab =
1d6da 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70   (const char *)p
1d6db 54 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20  Tab->pVtab;.    
1d6dc 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 4d      sqlite3VtabM
1d6dd 61 6b 65 57 72 69 74 61 62 6c 65 28 70 50 61 72  akeWritable(pPar
1d6de 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20  se, pTab);.     
1d6df 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1d6e0 64 4f 70 34 28 76 2c 20 4f 50 5f 56 55 70 64 61  dOp4(v, OP_VUpda
1d6e1 74 65 2c 20 30 2c 20 31 2c 20 69 52 6f 77 69 64  te, 0, 1, iRowid
1d6e2 2c 20 70 56 74 61 62 2c 20 50 34 5f 56 54 41 42  , pVtab, P4_VTAB
1d6e3 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 23  );.      }else.#
1d6e4 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20  endif.      {.  
1d6e5 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e        sqlite3Gen
1d6e6 65 72 61 74 65 52 6f 77 44 65 6c 65 74 65 28 70  erateRowDelete(p
1d6e7 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 43 75  Parse, pTab, iCu
1d6e8 72 2c 20 69 52 6f 77 69 64 2c 20 70 50 61 72 73  r, iRowid, pPars
1d6e9 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 29 3b 0a 20  e->nested==0);. 
1d6ea 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
1d6eb 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72    /* If there ar
1d6ec 65 20 72 6f 77 20 74 72 69 67 67 65 72 73 2c 20  e row triggers, 
1d6ed 63 6c 6f 73 65 20 61 6c 6c 20 63 75 72 73 6f 72  close all cursor
1d6ee 73 20 74 68 65 6e 20 69 6e 76 6f 6b 65 0a 20 20  s then invoke.  
1d6ef 20 20 2a 2a 20 74 68 65 20 41 46 54 45 52 20 74    ** the AFTER t
1d6f0 72 69 67 67 65 72 73 0a 20 20 20 20 2a 2f 0a 20  riggers.    */. 
1d6f1 20 20 20 69 66 28 20 74 72 69 67 67 65 72 73 5f     if( triggers_
1d6f2 65 78 69 73 74 20 29 7b 0a 20 20 20 20 20 20 2f  exist ){.      /
1d6f3 2a 20 4a 75 6d 70 20 62 61 63 6b 20 61 6e 64 20  * Jump back and 
1d6f4 72 75 6e 20 74 68 65 20 41 46 54 45 52 20 74 72  run the AFTER tr
1d6f5 69 67 67 65 72 73 20 2a 2f 0a 20 20 20 20 20 20  iggers */.      
1d6f6 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1d6f7 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
1d6f8 20 69 42 65 67 69 6e 41 66 74 65 72 54 72 69 67   iBeginAfterTrig
1d6f9 67 65 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ger);.      sqli
1d6fa 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
1d6fb 76 2c 20 69 45 6e 64 41 66 74 65 72 54 72 69 67  v, iEndAfterTrig
1d6fc 67 65 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ger);.    }..   
1d6fd 20 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 64   /* End of the d
1d6fe 65 6c 65 74 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20  elete loop */.  
1d6ff 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1d700 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
1d701 30 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 73 71  0, addr);.    sq
1d702 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
1d703 4c 61 62 65 6c 28 76 2c 20 65 6e 64 29 3b 0a 0a  Label(v, end);..
1d704 20 20 20 20 2f 2a 20 43 6c 6f 73 65 20 74 68 65      /* Close the
1d705 20 63 75 72 73 6f 72 73 20 61 66 74 65 72 20 74   cursors after t
1d706 68 65 20 6c 6f 6f 70 20 69 66 20 74 68 65 72 65  he loop if there
1d707 20 61 72 65 20 6e 6f 20 72 6f 77 20 74 72 69 67   are no row trig
1d708 67 65 72 73 20 2a 2f 0a 20 20 20 20 69 66 28 20  gers */.    if( 
1d709 21 69 73 56 69 65 77 20 20 26 26 20 21 49 73 56  !isView  && !IsV
1d70a 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
1d70b 20 20 20 20 20 20 66 6f 72 28 69 3d 31 2c 20 70        for(i=1, p
1d70c 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78  Idx=pTab->pIndex
1d70d 3b 20 70 49 64 78 3b 20 69 2b 2b 2c 20 70 49 64  ; pIdx; i++, pId
1d70e 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  x=pIdx->pNext){.
1d70f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1d710 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1d711 43 6c 6f 73 65 2c 20 69 43 75 72 20 2b 20 69 2c  Close, iCur + i,
1d712 20 70 49 64 78 2d 3e 74 6e 75 6d 29 3b 0a 20 20   pIdx->tnum);.  
1d713 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
1d714 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
1d715 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 29   OP_Close, iCur)
1d716 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
1d717 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 74 68  *.  ** Return th
1d718 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
1d719 20 74 68 61 74 20 77 65 72 65 20 64 65 6c 65 74   that were delet
1d71a 65 64 2e 20 49 66 20 74 68 69 73 20 72 6f 75 74  ed. If this rout
1d71b 69 6e 65 20 69 73 20 0a 20 20 2a 2a 20 67 65 6e  ine is .  ** gen
1d71c 65 72 61 74 69 6e 67 20 63 6f 64 65 20 62 65 63  erating code bec
1d71d 61 75 73 65 20 6f 66 20 61 20 63 61 6c 6c 20 74  ause of a call t
1d71e 6f 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50  o sqlite3NestedP
1d71f 61 72 73 65 28 29 2c 20 64 6f 20 6e 6f 74 0a 20  arse(), do not. 
1d720 20 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 63   ** invoke the c
1d721 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
1d722 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d  ..  */.  if( db-
1d723 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
1d724 43 6f 75 6e 74 52 6f 77 73 20 26 26 20 70 50 61  CountRows && pPa
1d725 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 26  rse->nested==0 &
1d726 26 20 21 70 50 61 72 73 65 2d 3e 74 72 69 67 53  & !pParse->trigS
1d727 74 61 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69  tack ){.    sqli
1d728 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1d729 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 6d   OP_ResultRow, m
1d72a 65 6d 43 6e 74 2c 20 31 29 3b 0a 20 20 20 20 73  emCnt, 1);.    s
1d72b 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
1d72c 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20  Cols(v, 1);.    
1d72d 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
1d72e 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
1d72f 41 4d 45 5f 4e 41 4d 45 2c 20 22 72 6f 77 73 20  AME_NAME, "rows 
1d730 64 65 6c 65 74 65 64 22 2c 20 50 34 5f 53 54 41  deleted", P4_STA
1d731 54 49 43 29 3b 0a 20 20 7d 0a 0a 64 65 6c 65 74  TIC);.  }..delet
1d732 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3a 0a  e_from_cleanup:.
1d733 20 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e    sqlite3AuthCon
1d734 74 65 78 74 50 6f 70 28 26 73 43 6f 6e 74 65 78  textPop(&sContex
1d735 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 72 63  t);.  sqlite3Src
1d736 4c 69 73 74 44 65 6c 65 74 65 28 70 54 61 62 4c  ListDelete(pTabL
1d737 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ist);.  sqlite3E
1d738 78 70 72 44 65 6c 65 74 65 28 70 57 68 65 72 65  xprDelete(pWhere
1d739 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a  );.  return;.}..
1d73a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
1d73b 6e 65 20 67 65 6e 65 72 61 74 65 73 20 56 44 42  ne generates VDB
1d73c 45 20 63 6f 64 65 20 74 68 61 74 20 63 61 75 73  E code that caus
1d73d 65 73 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20  es a single row 
1d73e 6f 66 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 74  of a.** single t
1d73f 61 62 6c 65 20 74 6f 20 62 65 20 64 65 6c 65 74  able to be delet
1d740 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 44  ed..**.** The VD
1d741 42 45 20 6d 75 73 74 20 62 65 20 69 6e 20 61 20  BE must be in a 
1d742 70 61 72 74 69 63 75 6c 61 72 20 73 74 61 74 65  particular state
1d743 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   when this routi
1d744 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a  ne is called..**
1d745 20 54 68 65 73 65 20 61 72 65 20 74 68 65 20 72   These are the r
1d746 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 0a  equirements:.**.
1d747 2a 2a 20 20 20 31 2e 20 20 41 20 72 65 61 64 2f  **   1.  A read/
1d748 77 72 69 74 65 20 63 75 72 73 6f 72 20 70 6f 69  write cursor poi
1d749 6e 74 69 6e 67 20 74 6f 20 70 54 61 62 2c 20 74  nting to pTab, t
1d74a 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e  he table contain
1d74b 69 6e 67 20 74 68 65 20 72 6f 77 0a 2a 2a 20 20  ing the row.**  
1d74c 20 20 20 20 20 74 6f 20 62 65 20 64 65 6c 65 74       to be delet
1d74d 65 64 2c 20 6d 75 73 74 20 62 65 20 6f 70 65 6e  ed, must be open
1d74e 65 64 20 61 73 20 63 75 72 73 6f 72 20 6e 75 6d  ed as cursor num
1d74f 62 65 72 20 22 62 61 73 65 22 2e 0a 2a 2a 0a 2a  ber "base"..**.*
1d750 2a 20 20 20 32 2e 20 20 52 65 61 64 2f 77 72 69  *   2.  Read/wri
1d751 74 65 20 63 75 72 73 6f 72 73 20 66 6f 72 20 61  te cursors for a
1d752 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 70 54  ll indices of pT
1d753 61 62 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20  ab must be open 
1d754 61 73 0a 2a 2a 20 20 20 20 20 20 20 63 75 72 73  as.**       curs
1d755 6f 72 20 6e 75 6d 62 65 72 20 62 61 73 65 2b 69  or number base+i
1d756 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 69 6e   for the i-th in
1d757 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20  dex..**.**   3. 
1d758 20 54 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62   The record numb
1d759 65 72 20 6f 66 20 74 68 65 20 72 6f 77 20 74 6f  er of the row to
1d75a 20 62 65 20 64 65 6c 65 74 65 64 20 6d 75 73 74   be deleted must
1d75b 20 62 65 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a   be stored in.**
1d75c 20 20 20 20 20 20 20 6d 65 6d 6f 72 79 20 63 65         memory ce
1d75d 6c 6c 20 69 52 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a  ll iRowid..**.**
1d75e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 70 6f   This routine po
1d75f 70 73 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  ps the top of th
1d760 65 20 73 74 61 63 6b 20 74 6f 20 72 65 6d 6f 76  e stack to remov
1d761 65 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d  e the record num
1d762 62 65 72 0a 2a 2a 20 61 6e 64 20 74 68 65 6e 20  ber.** and then 
1d763 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74  generates code t
1d764 6f 20 72 65 6d 6f 76 65 20 62 6f 74 68 20 74 68  o remove both th
1d765 65 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20 61  e table record a
1d766 6e 64 20 61 6c 6c 20 69 6e 64 65 78 0a 2a 2a 20  nd all index.** 
1d767 65 6e 74 72 69 65 73 20 74 68 61 74 20 70 6f 69  entries that poi
1d768 6e 74 20 74 6f 20 74 68 61 74 20 72 65 63 6f 72  nt to that recor
1d769 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
1d76a 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1d76b 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c 65  3GenerateRowDele
1d76c 74 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  te(.  Parse *pPa
1d76d 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73  rse,     /* Pars
1d76e 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
1d76f 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
1d770 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 6f 6e      /* Table con
1d771 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 20  taining the row 
1d772 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 2a 2f  to be deleted */
1d773 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20 20  .  int iCur,    
1d774 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
1d775 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74  number for the t
1d776 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 52  able */.  int iR
1d777 6f 77 69 64 2c 20 20 20 20 20 20 20 20 2f 2a 20  owid,        /* 
1d778 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 74  Memory cell that
1d779 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 72 6f   contains the ro
1d77a 77 69 64 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f  wid to delete */
1d77b 0a 20 20 69 6e 74 20 63 6f 75 6e 74 20 20 20 20  .  int count    
1d77c 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65        /* Increme
1d77d 6e 74 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67  nt the row chang
1d77e 65 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 29 7b 0a  e counter */.){.
1d77f 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 56 64    int addr;.  Vd
1d780 62 65 20 2a 76 3b 0a 0a 20 20 76 20 3d 20 70 50  be *v;..  v = pP
1d781 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 61  arse->pVdbe;.  a
1d782 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
1d783 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f  eAddOp3(v, OP_No
1d784 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20 30  tExists, iCur, 0
1d785 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20 73 71 6c  , iRowid);.  sql
1d786 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 49  ite3GenerateRowI
1d787 6e 64 65 78 44 65 6c 65 74 65 28 70 50 61 72 73  ndexDelete(pPars
1d788 65 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20 30  e, pTab, iCur, 0
1d789 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
1d78a 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 44 65 6c  AddOp2(v, OP_Del
1d78b 65 74 65 2c 20 69 43 75 72 2c 20 28 63 6f 75 6e  ete, iCur, (coun
1d78c 74 3f 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45  t?OPFLAG_NCHANGE
1d78d 3a 30 29 29 3b 0a 20 20 69 66 28 20 63 6f 75 6e  :0));.  if( coun
1d78e 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
1d78f 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
1d790 2d 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  -1, pTab->zName,
1d791 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d   P4_STATIC);.  }
1d792 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75  .  sqlite3VdbeJu
1d793 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b  mpHere(v, addr);
1d794 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
1d795 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73  outine generates
1d796 20 56 44 42 45 20 63 6f 64 65 20 74 68 61 74 20   VDBE code that 
1d797 63 61 75 73 65 73 20 74 68 65 20 64 65 6c 65 74  causes the delet
1d798 69 6f 6e 20 6f 66 20 61 6c 6c 0a 2a 2a 20 69 6e  ion of all.** in
1d799 64 65 78 20 65 6e 74 72 69 65 73 20 61 73 73 6f  dex entries asso
1d79a 63 69 61 74 65 64 20 77 69 74 68 20 61 20 73 69  ciated with a si
1d79b 6e 67 6c 65 20 72 6f 77 20 6f 66 20 61 20 73 69  ngle row of a si
1d79c 6e 67 6c 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a  ngle table..**.*
1d79d 2a 20 54 68 65 20 56 44 42 45 20 6d 75 73 74 20  * The VDBE must 
1d79e 62 65 20 69 6e 20 61 20 70 61 72 74 69 63 75 6c  be in a particul
1d79f 61 72 20 73 74 61 74 65 20 77 68 65 6e 20 74 68  ar state when th
1d7a0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
1d7a1 6c 6c 65 64 2e 0a 2a 2a 20 54 68 65 73 65 20 61  lled..** These a
1d7a2 72 65 20 74 68 65 20 72 65 71 75 69 72 65 6d 65  re the requireme
1d7a3 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20  nts:.**.**   1. 
1d7a4 20 41 20 72 65 61 64 2f 77 72 69 74 65 20 63 75   A read/write cu
1d7a5 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f  rsor pointing to
1d7a6 20 70 54 61 62 2c 20 74 68 65 20 74 61 62 6c 65   pTab, the table
1d7a7 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
1d7a8 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 74 6f 20  row.**       to 
1d7a9 62 65 20 64 65 6c 65 74 65 64 2c 20 6d 75 73 74  be deleted, must
1d7aa 20 62 65 20 6f 70 65 6e 65 64 20 61 73 20 63 75   be opened as cu
1d7ab 72 73 6f 72 20 6e 75 6d 62 65 72 20 22 69 43 75  rsor number "iCu
1d7ac 72 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 20  r"..**.**   2.  
1d7ad 52 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f  Read/write curso
1d7ae 72 73 20 66 6f 72 20 61 6c 6c 20 69 6e 64 69 63  rs for all indic
1d7af 65 73 20 6f 66 20 70 54 61 62 20 6d 75 73 74 20  es of pTab must 
1d7b0 62 65 20 6f 70 65 6e 20 61 73 0a 2a 2a 20 20 20  be open as.**   
1d7b1 20 20 20 20 63 75 72 73 6f 72 20 6e 75 6d 62 65      cursor numbe
1d7b2 72 20 69 43 75 72 2b 69 20 66 6f 72 20 74 68 65  r iCur+i for the
1d7b3 20 69 2d 74 68 20 69 6e 64 65 78 2e 0a 2a 2a 0a   i-th index..**.
1d7b4 2a 2a 20 20 20 33 2e 20 20 54 68 65 20 22 69 43  **   3.  The "iC
1d7b5 75 72 22 20 63 75 72 73 6f 72 20 6d 75 73 74 20  ur" cursor must 
1d7b6 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74  be pointing to t
1d7b7 68 65 20 72 6f 77 20 74 68 61 74 20 69 73 20 74  he row that is t
1d7b8 6f 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 64 65  o be.**       de
1d7b9 6c 65 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  leted..*/.SQLITE
1d7ba 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1d7bb 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77  lite3GenerateRow
1d7bc 49 6e 64 65 78 44 65 6c 65 74 65 28 0a 20 20 50  IndexDelete(.  P
1d7bd 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
1d7be 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64    /* Parsing and
1d7bf 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67   code generating
1d7c0 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61   context */.  Ta
1d7c1 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20  ble *pTab,      
1d7c2 20 2f 2a 20 54 61 62 6c 65 20 63 6f 6e 74 61 69   /* Table contai
1d7c3 6e 69 6e 67 20 74 68 65 20 72 6f 77 20 74 6f 20  ning the row to 
1d7c4 62 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20  be deleted */.  
1d7c5 69 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20 20  int iCur,       
1d7c6 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d     /* Cursor num
1d7c7 62 65 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c  ber for the tabl
1d7c8 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67  e */.  int *aReg
1d7c9 49 64 78 20 20 20 20 20 20 20 2f 2a 20 4f 6e 6c  Idx       /* Onl
1d7ca 79 20 64 65 6c 65 74 65 20 69 66 20 61 52 65 67  y delete if aReg
1d7cb 49 64 78 21 3d 30 20 26 26 20 61 52 65 67 49 64  Idx!=0 && aRegId
1d7cc 78 5b 69 5d 3e 30 20 2a 2f 0a 29 7b 0a 20 20 69  x[i]>0 */.){.  i
1d7cd 6e 74 20 69 3b 0a 20 20 49 6e 64 65 78 20 2a 70  nt i;.  Index *p
1d7ce 49 64 78 3b 0a 20 20 69 6e 74 20 72 31 3b 0a 0a  Idx;.  int r1;..
1d7cf 20 20 66 6f 72 28 69 3d 31 2c 20 70 49 64 78 3d    for(i=1, pIdx=
1d7d0 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
1d7d1 64 78 3b 20 69 2b 2b 2c 20 70 49 64 78 3d 70 49  dx; i++, pIdx=pI
1d7d2 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  dx->pNext){.    
1d7d3 69 66 28 20 61 52 65 67 49 64 78 21 3d 30 20 26  if( aRegIdx!=0 &
1d7d4 26 20 61 52 65 67 49 64 78 5b 69 2d 31 5d 3d 3d  & aRegIdx[i-1]==
1d7d5 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
1d7d6 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65    r1 = sqlite3Ge
1d7d7 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 70  nerateIndexKey(p
1d7d8 50 61 72 73 65 2c 20 70 49 64 78 2c 20 69 43 75  Parse, pIdx, iCu
1d7d9 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  r, 0, 0);.    sq
1d7da 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
1d7db 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f  pParse->pVdbe, O
1d7dc 50 5f 49 64 78 44 65 6c 65 74 65 2c 20 69 43 75  P_IdxDelete, iCu
1d7dd 72 2b 69 2c 20 72 31 2c 70 49 64 78 2d 3e 6e 43  r+i, r1,pIdx->nC
1d7de 6f 6c 75 6d 6e 2b 31 29 3b 0a 20 20 7d 0a 7d 0a  olumn+1);.  }.}.
1d7df 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
1d7e0 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 61  code that will a
1d7e1 73 73 65 6d 62 6c 65 20 61 6e 20 69 6e 64 65 78  ssemble an index
1d7e2 20 6b 65 79 20 61 6e 64 20 70 75 74 20 69 74 20   key and put it 
1d7e3 69 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 72  in register.** r
1d7e4 65 67 4f 75 74 2e 20 20 54 68 65 20 6b 65 79 20  egOut.  The key 
1d7e5 77 69 74 68 20 62 65 20 66 6f 72 20 69 6e 64 65  with be for inde
1d7e6 78 20 70 49 64 78 20 77 68 69 63 68 20 69 73 20  x pIdx which is 
1d7e7 61 6e 20 69 6e 64 65 78 20 6f 6e 20 70 54 61 62  an index on pTab
1d7e8 2e 0a 2a 2a 20 69 43 75 72 20 69 73 20 74 68 65  ..** iCur is the
1d7e9 20 69 6e 64 65 78 20 6f 66 20 61 20 63 75 72 73   index of a curs
1d7ea 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 70  or open on the p
1d7eb 54 61 62 20 74 61 62 6c 65 20 61 6e 64 20 70 6f  Tab table and po
1d7ec 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a 20 74 68 65  inting to.** the
1d7ed 20 65 6e 74 72 79 20 74 68 61 74 20 6e 65 65 64   entry that need
1d7ee 73 20 69 6e 64 65 78 69 6e 67 2e 0a 2a 2a 0a 2a  s indexing..**.*
1d7ef 2a 20 52 65 74 75 72 6e 20 61 20 72 65 67 69 73  * Return a regis
1d7f0 74 65 72 20 6e 75 6d 62 65 72 20 77 68 69 63 68  ter number which
1d7f1 20 69 73 20 74 68 65 20 66 69 72 73 74 20 69 6e   is the first in
1d7f2 20 61 20 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 72   a block of.** r
1d7f3 65 67 69 73 74 65 72 73 20 74 68 61 74 20 68 6f  egisters that ho
1d7f4 6c 64 73 20 74 68 65 20 65 6c 65 6d 65 6e 74 73  lds the elements
1d7f5 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6b 65   of the index ke
1d7f6 79 2e 20 20 54 68 65 0a 2a 2a 20 62 6c 6f 63 6b  y.  The.** block
1d7f7 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 68 61   of registers ha
1d7f8 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 64  s already been d
1d7f9 65 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68  eallocated by th
1d7fa 65 20 74 69 6d 65 0a 2a 2a 20 74 68 69 73 20 72  e time.** this r
1d7fb 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a  outine returns..
1d7fc 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1d7fd 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 6e  E int sqlite3Gen
1d7fe 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 0a 20  erateIndexKey(. 
1d7ff 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
1d800 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
1d801 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 49 6e 64 65  ontext */.  Inde
1d802 78 20 2a 70 49 64 78 2c 20 20 20 20 20 20 20 2f  x *pIdx,       /
1d803 2a 20 54 68 65 20 69 6e 64 65 78 20 66 6f 72 20  * The index for 
1d804 77 68 69 63 68 20 74 6f 20 67 65 6e 65 72 61 74  which to generat
1d805 65 20 61 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74  e a key */.  int
1d806 20 69 43 75 72 2c 20 20 20 20 20 20 20 20 20 20   iCur,          
1d807 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72  /* Cursor number
1d808 20 66 6f 72 20 74 68 65 20 70 49 64 78 2d 3e 70   for the pIdx->p
1d809 54 61 62 6c 65 20 74 61 62 6c 65 20 2a 2f 0a 20  Table table */. 
1d80a 20 69 6e 74 20 72 65 67 4f 75 74 2c 20 20 20 20   int regOut,    
1d80b 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
1d80c 20 6e 65 77 20 69 6e 64 65 78 20 6b 65 79 20 74   new index key t
1d80d 6f 20 74 68 69 73 20 72 65 67 69 73 74 65 72 20  o this register 
1d80e 2a 2f 0a 20 20 69 6e 74 20 64 6f 4d 61 6b 65 52  */.  int doMakeR
1d80f 65 63 20 20 20 20 20 20 2f 2a 20 52 75 6e 20 74  ec      /* Run t
1d810 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  he OP_MakeRecord
1d811 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 66 20   instruction if 
1d812 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 56 64 62  true */.){.  Vdb
1d813 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
1d814 56 64 62 65 3b 0a 20 20 69 6e 74 20 6a 3b 0a 20  Vdbe;.  int j;. 
1d815 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70   Table *pTab = p
1d816 49 64 78 2d 3e 70 54 61 62 6c 65 3b 0a 20 20 69  Idx->pTable;.  i
1d817 6e 74 20 72 65 67 42 61 73 65 3b 0a 20 20 69 6e  nt regBase;.  in
1d818 74 20 6e 43 6f 6c 3b 0a 0a 20 20 6e 43 6f 6c 20  t nCol;..  nCol 
1d819 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b  = pIdx->nColumn;
1d81a 0a 20 20 72 65 67 42 61 73 65 20 3d 20 73 71 6c  .  regBase = sql
1d81b 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65  ite3GetTempRange
1d81c 28 70 50 61 72 73 65 2c 20 6e 43 6f 6c 2b 31 29  (pParse, nCol+1)
1d81d 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
1d81e 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69  ddOp2(v, OP_Rowi
1d81f 64 2c 20 69 43 75 72 2c 20 72 65 67 42 61 73 65  d, iCur, regBase
1d820 2b 6e 43 6f 6c 29 3b 0a 20 20 66 6f 72 28 6a 3d  +nCol);.  for(j=
1d821 30 3b 20 6a 3c 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b  0; j<nCol; j++){
1d822 0a 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70  .    int idx = p
1d823 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d  Idx->aiColumn[j]
1d824 3b 0a 20 20 20 20 69 66 28 20 69 64 78 3d 3d 70  ;.    if( idx==p
1d825 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20  Tab->iPKey ){.  
1d826 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1d827 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70  ddOp2(v, OP_SCop
1d828 79 2c 20 72 65 67 42 61 73 65 2b 6e 43 6f 6c 2c  y, regBase+nCol,
1d829 20 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20 20 20   regBase+j);.   
1d82a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
1d82b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
1d82c 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 43  v, OP_Column, iC
1d82d 75 72 2c 20 69 64 78 2c 20 72 65 67 42 61 73 65  ur, idx, regBase
1d82e 2b 6a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  +j);.      sqlit
1d82f 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28  e3ColumnDefault(
1d830 76 2c 20 70 54 61 62 2c 20 69 64 78 29 3b 0a 20  v, pTab, idx);. 
1d831 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 64     }.  }.  if( d
1d832 6f 4d 61 6b 65 52 65 63 20 29 7b 0a 20 20 20 20  oMakeRec ){.    
1d833 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1d834 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  3(v, OP_MakeReco
1d835 72 64 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f  rd, regBase, nCo
1d836 6c 2b 31 2c 20 72 65 67 4f 75 74 29 3b 0a 20 20  l+1, regOut);.  
1d837 20 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66    sqlite3IndexAf
1d838 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70 49 64  finityStr(v, pId
1d839 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  x);.    sqlite3E
1d83a 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79  xprCacheAffinity
1d83b 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  Change(pParse, r
1d83c 65 67 42 61 73 65 2c 20 6e 43 6f 6c 2b 31 29 3b  egBase, nCol+1);
1d83d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65  .  }.  sqlite3Re
1d83e 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70  leaseTempRange(p
1d83f 50 61 72 73 65 2c 20 72 65 67 42 61 73 65 2c 20  Parse, regBase, 
1d840 6e 43 6f 6c 2b 31 29 3b 0a 20 20 72 65 74 75 72  nCol+1);.  retur
1d841 6e 20 72 65 67 42 61 73 65 3b 0a 7d 0a 0a 2f 2a  n regBase;.}../*
1d842 20 4d 61 6b 65 20 73 75 72 65 20 22 69 73 56 69   Make sure "isVi
1d843 65 77 22 20 67 65 74 73 20 75 6e 64 65 66 69 6e  ew" gets undefin
1d844 65 64 20 69 6e 20 63 61 73 65 20 74 68 69 73 20  ed in case this 
1d845 66 69 6c 65 20 62 65 63 6f 6d 65 73 20 70 61 72  file becomes par
1d846 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61 6d 61 6c  t of.** the amal
1d847 67 61 6d 61 74 69 6f 6e 20 2d 20 73 6f 20 74 68  gamation - so th
1d848 61 74 20 73 75 62 73 65 71 75 65 6e 74 20 66 69  at subsequent fi
1d849 6c 65 73 20 64 6f 20 6e 6f 74 20 73 65 65 20 69  les do not see i
1d84a 73 56 69 65 77 20 61 73 20 61 0a 2a 2a 20 6d 61  sView as a.** ma
1d84b 63 72 6f 2e 20 2a 2f 0a 23 75 6e 64 65 66 20 69  cro. */.#undef i
1d84c 73 56 69 65 77 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  sView../********
1d84d 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 64 65  ****** End of de
1d84e 6c 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  lete.c *********
1d84f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d851 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
1d852 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
1d853 65 20 66 75 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a  e func.c *******
1d854 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d855 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d856 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
1d857 32 20 46 65 62 72 75 61 72 79 20 32 33 0a 2a 2a  2 February 23.**
1d858 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
1d859 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
1d85a 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
1d85b 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
1d85c 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
1d85d 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
1d85e 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
1d85f 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
1d860 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
1d861 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
1d862 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
1d863 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
1d864 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
1d865 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
1d866 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
1d867 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
1d868 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
1d869 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
1d86a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d86b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d86c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d86d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
1d86e 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
1d86f 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f  ns the C functio
1d870 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  ns that implemen
1d871 74 20 76 61 72 69 6f 75 73 20 53 51 4c 0a 2a 2a  t various SQL.**
1d872 20 66 75 6e 63 74 69 6f 6e 73 20 6f 66 20 53 51   functions of SQ
1d873 4c 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68  Lite.  .**.** Th
1d874 65 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e 65 20  ere is only one 
1d875 65 78 70 6f 72 74 65 64 20 73 79 6d 62 6f 6c 20  exported symbol 
1d876 69 6e 20 74 68 69 73 20 66 69 6c 65 20 2d 20 74  in this file - t
1d877 68 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73  he function.** s
1d878 71 6c 69 74 65 52 65 67 69 73 74 65 72 42 75 69  qliteRegisterBui
1d879 6c 64 69 6e 46 75 6e 63 74 69 6f 6e 73 28 29 20  ldinFunctions() 
1d87a 66 6f 75 6e 64 20 61 74 20 74 68 65 20 62 6f 74  found at the bot
1d87b 74 6f 6d 20 6f 66 20 74 68 65 20 66 69 6c 65 2e  tom of the file.
1d87c 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 63 6f  .** All other co
1d87d 64 65 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70  de has file scop
1d87e 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 66 75  e..**.** $Id: fu
1d87f 6e 63 2e 63 2c 76 20 31 2e 31 39 32 20 32 30 30  nc.c,v 1.192 200
1d880 38 2f 30 34 2f 32 37 20 31 38 3a 34 30 3a 31 32  8/04/27 18:40:12
1d881 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a   drh Exp $.*/...
1d882 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
1d883 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74   collating funct
1d884 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77  ion associated w
1d885 69 74 68 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a  ith a function..
1d886 2a 2f 0a 73 74 61 74 69 63 20 43 6f 6c 6c 53 65  */.static CollSe
1d887 71 20 2a 73 71 6c 69 74 65 33 47 65 74 46 75 6e  q *sqlite3GetFun
1d888 63 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33  cCollSeq(sqlite3
1d889 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
1d88a 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6e  t){.  return con
1d88b 74 65 78 74 2d 3e 70 43 6f 6c 6c 3b 0a 7d 0a 0a  text->pColl;.}..
1d88c 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
1d88d 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 6f 6e 2d  tion of the non-
1d88e 61 67 67 72 65 67 61 74 65 20 6d 69 6e 28 29 20  aggregate min() 
1d88f 61 6e 64 20 6d 61 78 28 29 20 66 75 6e 63 74 69  and max() functi
1d890 6f 6e 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ons.*/.static vo
1d891 69 64 20 6d 69 6e 6d 61 78 46 75 6e 63 28 0a 20  id minmaxFunc(. 
1d892 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1d893 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
1d894 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
1d895 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
1d896 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
1d897 6d 61 73 6b 3b 20 20 20 20 2f 2a 20 30 20 66 6f  mask;    /* 0 fo
1d898 72 20 6d 69 6e 28 29 20 6f 72 20 30 78 66 66 66  r min() or 0xfff
1d899 66 66 66 66 66 20 66 6f 72 20 6d 61 78 28 29 20  fffff for max() 
1d89a 2a 2f 0a 20 20 69 6e 74 20 69 42 65 73 74 3b 0a  */.  int iBest;.
1d89b 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
1d89c 3b 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 30  ;..  if( argc==0
1d89d 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d 61 73   ) return;.  mas
1d89e 6b 20 3d 20 73 71 6c 69 74 65 33 5f 75 73 65 72  k = sqlite3_user
1d89f 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3d 3d  _data(context)==
1d8a0 30 20 3f 20 30 20 3a 20 2d 31 3b 0a 20 20 70 43  0 ? 0 : -1;.  pC
1d8a1 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74  oll = sqlite3Get
1d8a2 46 75 6e 63 43 6f 6c 6c 53 65 71 28 63 6f 6e 74  FuncCollSeq(cont
1d8a3 65 78 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ext);.  assert( 
1d8a4 70 43 6f 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72  pColl );.  asser
1d8a5 74 28 20 6d 61 73 6b 3d 3d 2d 31 20 7c 7c 20 6d  t( mask==-1 || m
1d8a6 61 73 6b 3d 3d 30 20 29 3b 0a 20 20 69 42 65 73  ask==0 );.  iBes
1d8a7 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c  t = 0;.  if( sql
1d8a8 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
1d8a9 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45  argv[0])==SQLITE
1d8aa 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a  _NULL ) return;.
1d8ab 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 61 72 67    for(i=1; i<arg
1d8ac 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  c; i++){.    if(
1d8ad 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
1d8ae 79 70 65 28 61 72 67 76 5b 69 5d 29 3d 3d 53 51  ype(argv[i])==SQ
1d8af 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75  LITE_NULL ) retu
1d8b0 72 6e 3b 0a 20 20 20 20 69 66 28 20 28 73 71 6c  rn;.    if( (sql
1d8b1 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 61  ite3MemCompare(a
1d8b2 72 67 76 5b 69 42 65 73 74 5d 2c 20 61 72 67 76  rgv[iBest], argv
1d8b3 5b 69 5d 2c 20 70 43 6f 6c 6c 29 5e 6d 61 73 6b  [i], pColl)^mask
1d8b4 29 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 42  )>=0 ){.      iB
1d8b5 65 73 74 20 3d 20 69 3b 0a 20 20 20 20 7d 0a 20  est = i;.    }. 
1d8b6 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73   }.  sqlite3_res
1d8b7 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78  ult_value(contex
1d8b8 74 2c 20 61 72 67 76 5b 69 42 65 73 74 5d 29 3b  t, argv[iBest]);
1d8b9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1d8ba 20 74 68 65 20 74 79 70 65 20 6f 66 20 74 68 65   the type of the
1d8bb 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74   argument..*/.st
1d8bc 61 74 69 63 20 76 6f 69 64 20 74 79 70 65 6f 66  atic void typeof
1d8bd 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
1d8be 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
1d8bf 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
1d8c0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
1d8c1 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  argv.){.  const 
1d8c2 63 68 61 72 20 2a 7a 20 3d 20 30 3b 0a 20 20 73  char *z = 0;.  s
1d8c3 77 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76  witch( sqlite3_v
1d8c4 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30  alue_type(argv[0
1d8c5 5d 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53  ]) ){.    case S
1d8c6 51 4c 49 54 45 5f 4e 55 4c 4c 3a 20 20 20 20 7a  QLITE_NULL:    z
1d8c7 20 3d 20 22 6e 75 6c 6c 22 3b 20 20 20 20 62 72   = "null";    br
1d8c8 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51  eak;.    case SQ
1d8c9 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 7a 20  LITE_INTEGER: z 
1d8ca 3d 20 22 69 6e 74 65 67 65 72 22 3b 20 62 72 65  = "integer"; bre
1d8cb 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c  ak;.    case SQL
1d8cc 49 54 45 5f 54 45 58 54 3a 20 20 20 20 7a 20 3d  ITE_TEXT:    z =
1d8cd 20 22 74 65 78 74 22 3b 20 20 20 20 62 72 65 61   "text";    brea
1d8ce 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  k;.    case SQLI
1d8cf 54 45 5f 46 4c 4f 41 54 3a 20 20 20 7a 20 3d 20  TE_FLOAT:   z = 
1d8d0 22 72 65 61 6c 22 3b 20 20 20 20 62 72 65 61 6b  "real";    break
1d8d1 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
1d8d2 45 5f 42 4c 4f 42 3a 20 20 20 20 7a 20 3d 20 22  E_BLOB:    z = "
1d8d3 62 6c 6f 62 22 3b 20 20 20 20 62 72 65 61 6b 3b  blob";    break;
1d8d4 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72  .  }.  sqlite3_r
1d8d5 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
1d8d6 78 74 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 54  xt, z, -1, SQLIT
1d8d7 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 0a 2f  E_STATIC);.}.../
1d8d8 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
1d8d9 69 6f 6e 20 6f 66 20 74 68 65 20 6c 65 6e 67 74  ion of the lengt
1d8da 68 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a  h() function.*/.
1d8db 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65 6e 67  static void leng
1d8dc 74 68 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  thFunc(.  sqlite
1d8dd 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
1d8de 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
1d8df 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
1d8e0 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20  **argv.){.  int 
1d8e1 6c 65 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  len;..  assert( 
1d8e2 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 73 77 69  argc==1 );.  swi
1d8e3 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61 6c  tch( sqlite3_val
1d8e4 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29  ue_type(argv[0])
1d8e5 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c   ){.    case SQL
1d8e6 49 54 45 5f 42 4c 4f 42 3a 0a 20 20 20 20 63 61  ITE_BLOB:.    ca
1d8e7 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  se SQLITE_INTEGE
1d8e8 52 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  R:.    case SQLI
1d8e9 54 45 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20  TE_FLOAT: {.    
1d8ea 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
1d8eb 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 73 71  _int(context, sq
1d8ec 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
1d8ed 73 28 61 72 67 76 5b 30 5d 29 29 3b 0a 20 20 20  s(argv[0]));.   
1d8ee 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1d8ef 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
1d8f0 54 45 58 54 3a 20 7b 0a 20 20 20 20 20 20 63 6f  TEXT: {.      co
1d8f1 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
1d8f2 72 20 2a 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76  r *z = sqlite3_v
1d8f3 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
1d8f4 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 3d  ]);.      if( z=
1d8f5 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20  =0 ) return;.   
1d8f6 20 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20     len = 0;.    
1d8f7 20 20 77 68 69 6c 65 28 20 2a 7a 20 29 7b 0a 20    while( *z ){. 
1d8f8 20 20 20 20 20 20 20 6c 65 6e 2b 2b 3b 0a 20 20         len++;.  
1d8f9 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49        SQLITE_SKI
1d8fa 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20  P_UTF8(z);.     
1d8fb 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
1d8fc 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74  _result_int(cont
1d8fd 65 78 74 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 20  ext, len);.     
1d8fe 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1d8ff 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
1d900 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
1d901 74 5f 6e 75 6c 6c 28 63 6f 6e 74 65 78 74 29 3b  t_null(context);
1d902 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1d903 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
1d904 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
1d905 6f 66 20 74 68 65 20 61 62 73 28 29 20 66 75 6e  of the abs() fun
1d906 63 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20  ction.*/.static 
1d907 76 6f 69 64 20 61 62 73 46 75 6e 63 28 73 71 6c  void absFunc(sql
1d908 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
1d909 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
1d90a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
1d90b 2a 61 72 67 76 29 7b 0a 20 20 61 73 73 65 72 74  *argv){.  assert
1d90c 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 73  ( argc==1 );.  s
1d90d 77 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76  witch( sqlite3_v
1d90e 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30  alue_type(argv[0
1d90f 5d 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53  ]) ){.    case S
1d910 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 7b  QLITE_INTEGER: {
1d911 0a 20 20 20 20 20 20 69 36 34 20 69 56 61 6c 20  .      i64 iVal 
1d912 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
1d913 69 6e 74 36 34 28 61 72 67 76 5b 30 5d 29 3b 0a  int64(argv[0]);.
1d914 20 20 20 20 20 20 69 66 28 20 69 56 61 6c 3c 30        if( iVal<0
1d915 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
1d916 28 69 56 61 6c 3c 3c 31 29 3d 3d 30 20 29 7b 0a  (iVal<<1)==0 ){.
1d917 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1d918 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63  3_result_error(c
1d919 6f 6e 74 65 78 74 2c 20 22 69 6e 74 65 67 65 72  ontext, "integer
1d91a 20 6f 76 65 72 66 6c 6f 77 22 2c 20 2d 31 29 3b   overflow", -1);
1d91b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
1d91c 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  n;.        }.   
1d91d 20 20 20 20 20 69 56 61 6c 20 3d 20 2d 69 56 61       iVal = -iVa
1d91e 6c 3b 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20  l;.      } .    
1d91f 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
1d920 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c 20  _int64(context, 
1d921 69 56 61 6c 29 3b 0a 20 20 20 20 20 20 62 72 65  iVal);.      bre
1d922 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
1d923 73 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3a 20  se SQLITE_NULL: 
1d924 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
1d925 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 63 6f 6e 74  result_null(cont
1d926 65 78 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61  ext);.      brea
1d927 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66  k;.    }.    def
1d928 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 64 6f  ault: {.      do
1d929 75 62 6c 65 20 72 56 61 6c 20 3d 20 73 71 6c 69  uble rVal = sqli
1d92a 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65  te3_value_double
1d92b 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20  (argv[0]);.     
1d92c 20 69 66 28 20 72 56 61 6c 3c 30 20 29 20 72 56   if( rVal<0 ) rV
1d92d 61 6c 20 3d 20 2d 72 56 61 6c 3b 0a 20 20 20 20  al = -rVal;.    
1d92e 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
1d92f 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c  _double(context,
1d930 20 72 56 61 6c 29 3b 0a 20 20 20 20 20 20 62 72   rVal);.      br
1d931 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  eak;.    }.  }.}
1d932 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
1d933 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 75  tation of the su
1d934 62 73 74 72 28 29 20 66 75 6e 63 74 69 6f 6e 2e  bstr() function.
1d935 0a 2a 2a 0a 2a 2a 20 73 75 62 73 74 72 28 78 2c  .**.** substr(x,
1d936 70 31 2c 70 32 29 20 20 72 65 74 75 72 6e 73 20  p1,p2)  returns 
1d937 70 32 20 63 68 61 72 61 63 74 65 72 73 20 6f 66  p2 characters of
1d938 20 78 5b 5d 20 62 65 67 69 6e 6e 69 6e 67 20 77   x[] beginning w
1d939 69 74 68 20 70 31 2e 0a 2a 2a 20 70 31 20 69 73  ith p1..** p1 is
1d93a 20 31 2d 69 6e 64 65 78 65 64 2e 20 20 53 6f 20   1-indexed.  So 
1d93b 73 75 62 73 74 72 28 78 2c 31 2c 31 29 20 72 65  substr(x,1,1) re
1d93c 74 75 72 6e 73 20 74 68 65 20 66 69 72 73 74 20  turns the first 
1d93d 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 6f 66 20  character.** of 
1d93e 78 2e 20 20 49 66 20 78 20 69 73 20 74 65 78 74  x.  If x is text
1d93f 2c 20 74 68 65 6e 20 77 65 20 61 63 74 75 61 6c  , then we actual
1d940 6c 79 20 63 6f 75 6e 74 20 55 54 46 2d 38 20 63  ly count UTF-8 c
1d941 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 20 49 66  haracters..** If
1d942 20 78 20 69 73 20 61 20 62 6c 6f 62 2c 20 74 68   x is a blob, th
1d943 65 6e 20 77 65 20 63 6f 75 6e 74 20 62 79 74 65  en we count byte
1d944 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 31 20 69  s..**.** If p1 i
1d945 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e  s negative, then
1d946 20 77 65 20 62 65 67 69 6e 20 61 62 73 28 70 31   we begin abs(p1
1d947 29 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f  ) from the end o
1d948 66 20 78 5b 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63  f x[]..*/.static
1d949 20 76 6f 69 64 20 73 75 62 73 74 72 46 75 6e 63   void substrFunc
1d94a 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
1d94b 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
1d94c 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
1d94d 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
1d94e 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69  .){.  const unsi
1d94f 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20  gned char *z;.  
1d950 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
1d951 68 61 72 20 2a 7a 32 3b 0a 20 20 69 6e 74 20 6c  har *z2;.  int l
1d952 65 6e 3b 0a 20 20 69 6e 74 20 70 30 74 79 70 65  en;.  int p0type
1d953 3b 0a 20 20 69 36 34 20 70 31 2c 20 70 32 3b 0a  ;.  i64 p1, p2;.
1d954 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
1d955 3d 33 20 7c 7c 20 61 72 67 63 3d 3d 32 20 29 3b  =3 || argc==2 );
1d956 0a 20 20 70 30 74 79 70 65 20 3d 20 73 71 6c 69  .  p0type = sqli
1d957 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
1d958 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 70  rgv[0]);.  if( p
1d959 30 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f 42 4c  0type==SQLITE_BL
1d95a 4f 42 20 29 7b 0a 20 20 20 20 6c 65 6e 20 3d 20  OB ){.    len = 
1d95b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
1d95c 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  tes(argv[0]);.  
1d95d 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61    z = sqlite3_va
1d95e 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d  lue_blob(argv[0]
1d95f 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20  );.    if( z==0 
1d960 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 61 73  ) return;.    as
1d961 73 65 72 74 28 20 6c 65 6e 3d 3d 73 71 6c 69 74  sert( len==sqlit
1d962 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
1d963 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 7d 65 6c  rgv[0]) );.  }el
1d964 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69  se{.    z = sqli
1d965 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
1d966 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28  rgv[0]);.    if(
1d967 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   z==0 ) return;.
1d968 20 20 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20      len = 0;.   
1d969 20 66 6f 72 28 7a 32 3d 7a 3b 20 2a 7a 32 3b 20   for(z2=z; *z2; 
1d96a 6c 65 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 53 51  len++){.      SQ
1d96b 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a  LITE_SKIP_UTF8(z
1d96c 32 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  2);.    }.  }.  
1d96d 70 31 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  p1 = sqlite3_val
1d96e 75 65 5f 69 6e 74 28 61 72 67 76 5b 31 5d 29 3b  ue_int(argv[1]);
1d96f 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 33 20 29  .  if( argc==3 )
1d970 7b 0a 20 20 20 20 70 32 20 3d 20 73 71 6c 69 74  {.    p2 = sqlit
1d971 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67  e3_value_int(arg
1d972 76 5b 32 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  v[2]);.  }else{.
1d973 20 20 20 20 70 32 20 3d 20 73 71 6c 69 74 65 33      p2 = sqlite3
1d974 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
1d975 6c 65 28 63 6f 6e 74 65 78 74 29 2d 3e 61 4c 69  le(context)->aLi
1d976 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
1d977 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a 20 20  _LENGTH];.  }.  
1d978 69 66 28 20 70 31 3c 30 20 29 7b 0a 20 20 20 20  if( p1<0 ){.    
1d979 70 31 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 69  p1 += len;.    i
1d97a 66 28 20 70 31 3c 30 20 29 7b 0a 20 20 20 20 20  f( p1<0 ){.     
1d97b 20 70 32 20 2b 3d 20 70 31 3b 0a 20 20 20 20 20   p2 += p1;.     
1d97c 20 70 31 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20   p1 = 0;.    }. 
1d97d 20 7d 65 6c 73 65 20 69 66 28 20 70 31 3e 30 20   }else if( p1>0 
1d97e 29 7b 0a 20 20 20 20 70 31 2d 2d 3b 0a 20 20 7d  ){.    p1--;.  }
1d97f 0a 20 20 69 66 28 20 70 31 2b 70 32 3e 6c 65 6e  .  if( p1+p2>len
1d980 20 29 7b 0a 20 20 20 20 70 32 20 3d 20 6c 65 6e   ){.    p2 = len
1d981 2d 70 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  -p1;.  }.  if( p
1d982 30 74 79 70 65 21 3d 53 51 4c 49 54 45 5f 42 4c  0type!=SQLITE_BL
1d983 4f 42 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28  OB ){.    while(
1d984 20 2a 7a 20 26 26 20 70 31 20 29 7b 0a 20 20 20   *z && p1 ){.   
1d985 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55     SQLITE_SKIP_U
1d986 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 20 70 31  TF8(z);.      p1
1d987 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f  --;.    }.    fo
1d988 72 28 7a 32 3d 7a 3b 20 2a 7a 32 20 26 26 20 70  r(z2=z; *z2 && p
1d989 32 3b 20 70 32 2d 2d 29 7b 0a 20 20 20 20 20 20  2; p2--){.      
1d98a 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38  SQLITE_SKIP_UTF8
1d98b 28 7a 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  (z2);.    }.    
1d98c 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
1d98d 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 28 63 68  ext(context, (ch
1d98e 61 72 2a 29 7a 2c 20 7a 32 2d 7a 2c 20 53 51 4c  ar*)z, z2-z, SQL
1d98f 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
1d990 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28    }else{.    if(
1d991 20 70 32 3c 30 20 29 20 70 32 20 3d 20 30 3b 0a   p2<0 ) p2 = 0;.
1d992 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
1d993 6c 74 5f 62 6c 6f 62 28 63 6f 6e 74 65 78 74 2c  lt_blob(context,
1d994 20 28 63 68 61 72 2a 29 26 7a 5b 70 31 5d 2c 20   (char*)&z[p1], 
1d995 70 32 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  p2, SQLITE_TRANS
1d996 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  IENT);.  }.}../*
1d997 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
1d998 6f 6e 20 6f 66 20 74 68 65 20 72 6f 75 6e 64 28  on of the round(
1d999 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a 73 74  ) function.*/.st
1d99a 61 74 69 63 20 76 6f 69 64 20 72 6f 75 6e 64 46  atic void roundF
1d99b 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  unc(sqlite3_cont
1d99c 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e  ext *context, in
1d99d 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
1d99e 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20  value **argv){. 
1d99f 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 64 6f   int n = 0;.  do
1d9a0 75 62 6c 65 20 72 3b 0a 20 20 63 68 61 72 20 7a  uble r;.  char z
1d9a1 42 75 66 5b 35 30 30 5d 3b 20 20 2f 2a 20 6c 61  Buf[500];  /* la
1d9a2 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 25 66  rger than the %f
1d9a3 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
1d9a4 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 64  of the largest d
1d9a5 6f 75 62 6c 65 20 2a 2f 0a 20 20 61 73 73 65 72  ouble */.  asser
1d9a6 74 28 20 61 72 67 63 3d 3d 31 20 7c 7c 20 61 72  t( argc==1 || ar
1d9a7 67 63 3d 3d 32 20 29 3b 0a 20 20 69 66 28 20 61  gc==2 );.  if( a
1d9a8 72 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20 69 66  rgc==2 ){.    if
1d9a9 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73  ( SQLITE_NULL==s
1d9aa 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
1d9ab 65 28 61 72 67 76 5b 31 5d 29 20 29 20 72 65 74  e(argv[1]) ) ret
1d9ac 75 72 6e 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c  urn;.    n = sql
1d9ad 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61  ite3_value_int(a
1d9ae 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 69 66 28  rgv[1]);.    if(
1d9af 20 6e 3e 33 30 20 29 20 6e 20 3d 20 33 30 3b 0a   n>30 ) n = 30;.
1d9b0 20 20 20 20 69 66 28 20 6e 3c 30 20 29 20 6e 20      if( n<0 ) n 
1d9b1 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  = 0;.  }.  if( s
1d9b2 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
1d9b3 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49  e(argv[0])==SQLI
1d9b4 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e  TE_NULL ) return
1d9b5 3b 0a 20 20 72 20 3d 20 73 71 6c 69 74 65 33 5f  ;.  r = sqlite3_
1d9b6 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67  value_double(arg
1d9b7 76 5b 30 5d 29 3b 0a 20 20 73 71 6c 69 74 65 33  v[0]);.  sqlite3
1d9b8 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66  _snprintf(sizeof
1d9b9 28 7a 42 75 66 29 2c 7a 42 75 66 2c 22 25 2e 2a  (zBuf),zBuf,"%.*
1d9ba 66 22 2c 6e 2c 72 29 3b 0a 20 20 73 71 6c 69 74  f",n,r);.  sqlit
1d9bb 65 33 41 74 6f 46 28 7a 42 75 66 2c 20 26 72 29  e3AtoF(zBuf, &r)
1d9bc 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ;.  sqlite3_resu
1d9bd 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78  lt_double(contex
1d9be 74 2c 20 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  t, r);.}../*.** 
1d9bf 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 20 62  Allocate nByte b
1d9c0 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 75 73  ytes of space us
1d9c1 69 6e 67 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  ing sqlite3_mall
1d9c2 6f 63 28 29 2e 20 49 66 20 74 68 65 0a 2a 2a 20  oc(). If the.** 
1d9c3 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73  allocation fails
1d9c4 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 72  , call sqlite3_r
1d9c5 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65  esult_error_nome
1d9c6 6d 28 29 20 74 6f 20 6e 6f 74 69 66 79 0a 2a 2a  m() to notify.**
1d9c7 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
1d9c8 6e 64 6c 65 20 74 68 61 74 20 6d 61 6c 6c 6f 63  ndle that malloc
1d9c9 28 29 20 68 61 73 20 66 61 69 6c 65 64 2e 0a 2a  () has failed..*
1d9ca 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 63  /.static void *c
1d9cb 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 73 71 6c  ontextMalloc(sql
1d9cc 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
1d9cd 6e 74 65 78 74 2c 20 69 36 34 20 6e 42 79 74 65  ntext, i64 nByte
1d9ce 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20  ){.  char *z;.  
1d9cf 69 66 28 20 6e 42 79 74 65 3e 73 71 6c 69 74 65  if( nByte>sqlite
1d9d0 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
1d9d1 64 6c 65 28 63 6f 6e 74 65 78 74 29 2d 3e 61 4c  dle(context)->aL
1d9d2 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
1d9d3 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20  T_LENGTH] ){.   
1d9d4 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1d9d5 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e  error_toobig(con
1d9d6 74 65 78 74 29 3b 0a 20 20 20 20 7a 20 3d 20 30  text);.    z = 0
1d9d7 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a  ;.  }else{.    z
1d9d8 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
1d9d9 63 28 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 66  c(nByte);.    if
1d9da 28 20 21 7a 20 26 26 20 6e 42 79 74 65 3e 30 20  ( !z && nByte>0 
1d9db 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1d9dc 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f  _result_error_no
1d9dd 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  mem(context);.  
1d9de 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
1d9df 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70   z;.}../*.** Imp
1d9e0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
1d9e1 68 65 20 75 70 70 65 72 28 29 20 61 6e 64 20 6c  he upper() and l
1d9e2 6f 77 65 72 28 29 20 53 51 4c 20 66 75 6e 63 74  ower() SQL funct
1d9e3 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ions..*/.static 
1d9e4 76 6f 69 64 20 75 70 70 65 72 46 75 6e 63 28 73  void upperFunc(s
1d9e5 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1d9e6 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67  context, int arg
1d9e7 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  c, sqlite3_value
1d9e8 20 2a 2a 61 72 67 76 29 7b 0a 20 20 63 68 61 72   **argv){.  char
1d9e9 20 2a 7a 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68   *z1;.  const ch
1d9ea 61 72 20 2a 7a 32 3b 0a 20 20 69 6e 74 20 69 2c  ar *z2;.  int i,
1d9eb 20 6e 3b 0a 20 20 69 66 28 20 61 72 67 63 3c 31   n;.  if( argc<1
1d9ec 20 7c 7c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d   || SQLITE_NULL=
1d9ed 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  =sqlite3_value_t
1d9ee 79 70 65 28 61 72 67 76 5b 30 5d 29 20 29 20 72  ype(argv[0]) ) r
1d9ef 65 74 75 72 6e 3b 0a 20 20 7a 32 20 3d 20 28 63  eturn;.  z2 = (c
1d9f0 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  har*)sqlite3_val
1d9f1 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
1d9f2 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f  ;.  n = sqlite3_
1d9f3 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
1d9f4 5b 30 5d 29 3b 0a 20 20 2f 2a 20 56 65 72 69 66  [0]);.  /* Verif
1d9f5 79 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 20  y that the call 
1d9f6 74 6f 20 5f 62 79 74 65 73 28 29 20 64 6f 65 73  to _bytes() does
1d9f7 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 65 20   not invalidate 
1d9f8 74 68 65 20 5f 74 65 78 74 28 29 20 70 6f 69 6e  the _text() poin
1d9f9 74 65 72 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ter */.  assert(
1d9fa 20 7a 32 3d 3d 28 63 68 61 72 2a 29 73 71 6c 69   z2==(char*)sqli
1d9fb 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
1d9fc 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28  rgv[0]) );.  if(
1d9fd 20 7a 32 20 29 7b 0a 20 20 20 20 7a 31 20 3d 20   z2 ){.    z1 = 
1d9fe 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f  contextMalloc(co
1d9ff 6e 74 65 78 74 2c 20 28 28 69 36 34 29 6e 29 2b  ntext, ((i64)n)+
1da00 31 29 3b 0a 20 20 20 20 69 66 28 20 7a 31 20 29  1);.    if( z1 )
1da01 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a  {.      memcpy(z
1da02 31 2c 20 7a 32 2c 20 6e 2b 31 29 3b 0a 20 20 20  1, z2, n+1);.   
1da03 20 20 20 66 6f 72 28 69 3d 30 3b 20 7a 31 5b 69     for(i=0; z1[i
1da04 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  ]; i++){.       
1da05 20 7a 31 5b 69 5d 20 3d 20 74 6f 75 70 70 65 72   z1[i] = toupper
1da06 28 7a 31 5b 69 5d 29 3b 0a 20 20 20 20 20 20 7d  (z1[i]);.      }
1da07 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
1da08 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
1da09 78 74 2c 20 7a 31 2c 20 2d 31 2c 20 73 71 6c 69  xt, z1, -1, sqli
1da0a 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 7d  te3_free);.    }
1da0b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f  .  }.}.static vo
1da0c 69 64 20 6c 6f 77 65 72 46 75 6e 63 28 73 71 6c  id lowerFunc(sql
1da0d 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
1da0e 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
1da0f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
1da10 2a 61 72 67 76 29 7b 0a 20 20 63 68 61 72 20 2a  *argv){.  char *
1da11 7a 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  z1;.  const char
1da12 20 2a 7a 32 3b 0a 20 20 69 6e 74 20 69 2c 20 6e   *z2;.  int i, n
1da13 3b 0a 20 20 69 66 28 20 61 72 67 63 3c 31 20 7c  ;.  if( argc<1 |
1da14 7c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73  | SQLITE_NULL==s
1da15 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
1da16 65 28 61 72 67 76 5b 30 5d 29 20 29 20 72 65 74  e(argv[0]) ) ret
1da17 75 72 6e 3b 0a 20 20 7a 32 20 3d 20 28 63 68 61  urn;.  z2 = (cha
1da18 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  r*)sqlite3_value
1da19 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
1da1a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61    n = sqlite3_va
1da1b 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30  lue_bytes(argv[0
1da1c 5d 29 3b 0a 20 20 2f 2a 20 56 65 72 69 66 79 20  ]);.  /* Verify 
1da1d 74 68 61 74 20 74 68 65 20 63 61 6c 6c 20 74 6f  that the call to
1da1e 20 5f 62 79 74 65 73 28 29 20 64 6f 65 73 20 6e   _bytes() does n
1da1f 6f 74 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68  ot invalidate th
1da20 65 20 5f 74 65 78 74 28 29 20 70 6f 69 6e 74 65  e _text() pointe
1da21 72 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 7a  r */.  assert( z
1da22 32 3d 3d 28 63 68 61 72 2a 29 73 71 6c 69 74 65  2==(char*)sqlite
1da23 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
1da24 76 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 7a  v[0]) );.  if( z
1da25 32 20 29 7b 0a 20 20 20 20 7a 31 20 3d 20 63 6f  2 ){.    z1 = co
1da26 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74  ntextMalloc(cont
1da27 65 78 74 2c 20 28 28 69 36 34 29 6e 29 2b 31 29  ext, ((i64)n)+1)
1da28 3b 0a 20 20 20 20 69 66 28 20 7a 31 20 29 7b 0a  ;.    if( z1 ){.
1da29 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 31 2c        memcpy(z1,
1da2a 20 7a 32 2c 20 6e 2b 31 29 3b 0a 20 20 20 20 20   z2, n+1);.     
1da2b 20 66 6f 72 28 69 3d 30 3b 20 7a 31 5b 69 5d 3b   for(i=0; z1[i];
1da2c 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 7a   i++){.        z
1da2d 31 5b 69 5d 20 3d 20 74 6f 6c 6f 77 65 72 28 7a  1[i] = tolower(z
1da2e 31 5b 69 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20  1[i]);.      }. 
1da2f 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
1da30 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
1da31 2c 20 7a 31 2c 20 2d 31 2c 20 73 71 6c 69 74 65  , z1, -1, sqlite
1da32 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20  3_free);.    }. 
1da33 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c   }.}../*.** Impl
1da34 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
1da35 65 20 49 46 4e 55 4c 4c 28 29 2c 20 4e 56 4c 28  e IFNULL(), NVL(
1da36 29 2c 20 61 6e 64 20 43 4f 41 4c 45 53 43 45 28  ), and COALESCE(
1da37 29 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 0a 2a  ) functions.  .*
1da38 2a 20 41 6c 6c 20 74 68 72 65 65 20 64 6f 20 74  * All three do t
1da39 68 65 20 73 61 6d 65 20 74 68 69 6e 67 2e 20 20  he same thing.  
1da3a 54 68 65 79 20 72 65 74 75 72 6e 20 74 68 65 20  They return the 
1da3b 66 69 72 73 74 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a  first non-NULL.*
1da3c 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73  * argument..*/.s
1da3d 74 61 74 69 63 20 76 6f 69 64 20 69 66 6e 75 6c  tatic void ifnul
1da3e 6c 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  lFunc(.  sqlite3
1da3f 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
1da40 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
1da41 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
1da42 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69  *argv.){.  int i
1da43 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 61  ;.  for(i=0; i<a
1da44 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  rgc; i++){.    i
1da45 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 21 3d  f( SQLITE_NULL!=
1da46 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
1da47 70 65 28 61 72 67 76 5b 69 5d 29 20 29 7b 0a 20  pe(argv[i]) ){. 
1da48 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
1da49 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78  ult_value(contex
1da4a 74 2c 20 61 72 67 76 5b 69 5d 29 3b 0a 20 20 20  t, argv[i]);.   
1da4b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1da4c 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70    }.}../*.** Imp
1da4d 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 72  lementation of r
1da4e 61 6e 64 6f 6d 28 29 2e 20 20 52 65 74 75 72 6e  andom().  Return
1da4f 20 61 20 72 61 6e 64 6f 6d 20 69 6e 74 65 67 65   a random intege
1da50 72 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76  r.  .*/.static v
1da51 6f 69 64 20 72 61 6e 64 6f 6d 46 75 6e 63 28 0a  oid randomFunc(.
1da52 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
1da53 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
1da54 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
1da55 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
1da56 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  {.  sqlite_int64
1da57 20 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 61   r;.  sqlite3_ra
1da58 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28  ndomness(sizeof(
1da59 72 29 2c 20 26 72 29 3b 0a 20 20 69 66 28 20 28  r), &r);.  if( (
1da5a 72 3c 3c 31 29 3d 3d 30 20 29 20 72 20 3d 20 30  r<<1)==0 ) r = 0
1da5b 3b 20 20 2f 2a 20 50 72 65 76 65 6e 74 20 30 78  ;  /* Prevent 0x
1da5c 38 30 30 30 2e 2e 2e 2e 20 61 73 20 74 68 65 20  8000.... as the 
1da5d 72 65 73 75 6c 74 20 73 6f 20 74 68 61 74 20 77  result so that w
1da5e 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  e */.           
1da5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1da60 2a 20 63 61 6e 20 61 6c 77 61 79 73 20 64 6f 20  * can always do 
1da61 61 62 73 28 29 20 6f 66 20 74 68 65 20 72 65 73  abs() of the res
1da62 75 6c 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ult */.  sqlite3
1da63 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f  _result_int64(co
1da64 6e 74 65 78 74 2c 20 72 29 3b 0a 7d 0a 0a 2f 2a  ntext, r);.}../*
1da65 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
1da66 6f 6e 20 6f 66 20 72 61 6e 64 6f 6d 62 6c 6f 62  on of randomblob
1da67 28 4e 29 2e 20 20 52 65 74 75 72 6e 20 61 20 72  (N).  Return a r
1da68 61 6e 64 6f 6d 20 62 6c 6f 62 0a 2a 2a 20 74 68  andom blob.** th
1da69 61 74 20 69 73 20 4e 20 62 79 74 65 73 20 6c 6f  at is N bytes lo
1da6a 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ng..*/.static vo
1da6b 69 64 20 72 61 6e 64 6f 6d 42 6c 6f 62 28 0a 20  id randomBlob(. 
1da6c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1da6d 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
1da6e 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
1da6f 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
1da70 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73 69  .  int n;.  unsi
1da71 67 6e 65 64 20 63 68 61 72 20 2a 70 3b 0a 20 20  gned char *p;.  
1da72 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20  assert( argc==1 
1da73 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33  );.  n = sqlite3
1da74 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b  _value_int(argv[
1da75 30 5d 29 3b 0a 20 20 69 66 28 20 6e 3c 31 20 29  0]);.  if( n<1 )
1da76 7b 0a 20 20 20 20 6e 20 3d 20 31 3b 0a 20 20 7d  {.    n = 1;.  }
1da77 0a 20 20 70 20 3d 20 63 6f 6e 74 65 78 74 4d 61  .  p = contextMa
1da78 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 6e 29  lloc(context, n)
1da79 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  ;.  if( p ){.   
1da7a 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
1da7b 65 73 73 28 6e 2c 20 70 29 3b 0a 20 20 20 20 73  ess(n, p);.    s
1da7c 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c  qlite3_result_bl
1da7d 6f 62 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61  ob(context, (cha
1da7e 72 2a 29 70 2c 20 6e 2c 20 73 71 6c 69 74 65 33  r*)p, n, sqlite3
1da7f 5f 66 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  _free);.  }.}../
1da80 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
1da81 69 6f 6e 20 6f 66 20 74 68 65 20 6c 61 73 74 5f  ion of the last_
1da82 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 53  insert_rowid() S
1da83 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  QL function.  Th
1da84 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75  e return.** valu
1da85 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  e is the same as
1da86 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6c 61 73   the sqlite3_las
1da87 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29  t_insert_rowid()
1da88 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a   API function..*
1da89 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 61  /.static void la
1da8a 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28  st_insert_rowid(
1da8b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
1da8c 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20  xt *context, .  
1da8d 69 6e 74 20 61 72 67 2c 20 0a 20 20 73 71 6c 69  int arg, .  sqli
1da8e 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
1da8f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
1da90 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  b = sqlite3_cont
1da91 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
1da92 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 65  ntext);.  sqlite
1da93 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63  3_result_int64(c
1da94 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f  ontext, sqlite3_
1da95 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
1da96 64 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  d(db));.}../*.**
1da97 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
1da98 6f 66 20 74 68 65 20 63 68 61 6e 67 65 73 28 29  of the changes()
1da99 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20   SQL function.  
1da9a 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
1da9b 20 69 73 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20   is the.** same 
1da9c 61 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63  as the sqlite3_c
1da9d 68 61 6e 67 65 73 28 29 20 41 50 49 20 66 75 6e  hanges() API fun
1da9e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ction..*/.static
1da9f 20 76 6f 69 64 20 63 68 61 6e 67 65 73 28 0a 20   void changes(. 
1daa0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1daa1 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
1daa2 20 61 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f   arg,.  sqlite3_
1daa3 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
1daa4 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1daa5 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
1daa6 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78  db_handle(contex
1daa7 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  t);.  sqlite3_re
1daa8 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74  sult_int(context
1daa9 2c 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65  , sqlite3_change
1daaa 73 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  s(db));.}../*.**
1daab 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
1daac 6f 66 20 74 68 65 20 74 6f 74 61 6c 5f 63 68 61  of the total_cha
1daad 6e 67 65 73 28 29 20 53 51 4c 20 66 75 6e 63 74  nges() SQL funct
1daae 69 6f 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e  ion.  The return
1daaf 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 74 68 65   value is.** the
1dab0 20 73 61 6d 65 20 61 73 20 74 68 65 20 73 71 6c   same as the sql
1dab1 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67  ite3_total_chang
1dab2 65 73 28 29 20 41 50 49 20 66 75 6e 63 74 69 6f  es() API functio
1dab3 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
1dab4 64 20 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28  d total_changes(
1dab5 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
1dab6 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
1dab7 6e 74 20 61 72 67 2c 0a 20 20 73 71 6c 69 74 65  nt arg,.  sqlite
1dab8 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
1dab9 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
1daba 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  = sqlite3_contex
1dabb 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74  t_db_handle(cont
1dabc 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ext);.  sqlite3_
1dabd 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65  result_int(conte
1dabe 78 74 2c 20 73 71 6c 69 74 65 33 5f 74 6f 74 61  xt, sqlite3_tota
1dabf 6c 5f 63 68 61 6e 67 65 73 28 64 62 29 29 3b 0a  l_changes(db));.
1dac0 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74 72 75 63  }../*.** A struc
1dac1 74 75 72 65 20 64 65 66 69 6e 69 6e 67 20 68 6f  ture defining ho
1dac2 77 20 74 6f 20 64 6f 20 47 4c 4f 42 2d 73 74 79  w to do GLOB-sty
1dac3 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a  le comparisons..
1dac4 2a 2f 0a 73 74 72 75 63 74 20 63 6f 6d 70 61 72  */.struct compar
1dac5 65 49 6e 66 6f 20 7b 0a 20 20 75 38 20 6d 61 74  eInfo {.  u8 mat
1dac6 63 68 41 6c 6c 3b 0a 20 20 75 38 20 6d 61 74 63  chAll;.  u8 matc
1dac7 68 4f 6e 65 3b 0a 20 20 75 38 20 6d 61 74 63 68  hOne;.  u8 match
1dac8 53 65 74 3b 0a 20 20 75 38 20 6e 6f 43 61 73 65  Set;.  u8 noCase
1dac9 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20  ;.};../*.** For 
1daca 4c 49 4b 45 20 61 6e 64 20 47 4c 4f 42 20 6d 61  LIKE and GLOB ma
1dacb 74 63 68 69 6e 67 20 6f 6e 20 45 42 43 44 49 43  tching on EBCDIC
1dacc 20 6d 61 63 68 69 6e 65 73 2c 20 61 73 73 75 6d   machines, assum
1dacd 65 20 74 68 61 74 20 65 76 65 72 79 0a 2a 2a 20  e that every.** 
1dace 63 68 61 72 61 63 74 65 72 20 69 73 20 65 78 61  character is exa
1dacf 63 74 6c 79 20 6f 6e 65 20 62 79 74 65 20 69 6e  ctly one byte in
1dad0 20 73 69 7a 65 2e 20 20 41 6c 73 6f 2c 20 61 6c   size.  Also, al
1dad1 6c 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65  l characters are
1dad2 0a 2a 2a 20 61 62 6c 65 20 74 6f 20 70 61 72 74  .** able to part
1dad3 69 63 69 70 61 74 65 20 69 6e 20 75 70 70 65 72  icipate in upper
1dad4 2d 63 61 73 65 2d 74 6f 2d 6c 6f 77 65 72 2d 63  -case-to-lower-c
1dad5 61 73 65 20 6d 61 70 70 69 6e 67 73 20 69 6e 20  ase mappings in 
1dad6 45 42 43 44 49 43 0a 2a 2a 20 77 68 65 72 65 61  EBCDIC.** wherea
1dad7 73 20 6f 6e 6c 79 20 63 68 61 72 61 63 74 65 72  s only character
1dad8 73 20 6c 65 73 73 20 74 68 61 6e 20 30 78 38 30  s less than 0x80
1dad9 20 64 6f 20 69 6e 20 41 53 43 49 49 2e 0a 2a 2f   do in ASCII..*/
1dada 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
1dadb 49 54 45 5f 45 42 43 44 49 43 29 0a 23 20 64 65  ITE_EBCDIC).# de
1dadc 66 69 6e 65 20 73 71 6c 69 74 65 33 55 74 66 38  fine sqlite3Utf8
1dadd 52 65 61 64 28 41 2c 42 2c 43 29 20 20 28 2a 28  Read(A,B,C)  (*(
1dade 41 2b 2b 29 29 0a 23 20 64 65 66 69 6e 65 20 47  A++)).# define G
1dadf 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28  logUpperToLower(
1dae0 41 29 20 20 20 20 20 41 20 3d 20 73 71 6c 69 74  A)     A = sqlit
1dae1 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 41  e3UpperToLower[A
1dae2 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ].#else.# define
1dae3 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65   GlogUpperToLowe
1dae4 72 28 41 29 20 20 20 20 20 69 66 28 20 41 3c 30  r(A)     if( A<0
1dae5 78 38 30 20 29 7b 20 41 20 3d 20 73 71 6c 69 74  x80 ){ A = sqlit
1dae6 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 41  e3UpperToLower[A
1dae7 5d 3b 20 7d 0a 23 65 6e 64 69 66 0a 0a 73 74 61  ]; }.#endif..sta
1dae8 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74  tic const struct
1dae9 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 67 6c 6f   compareInfo glo
1daea 62 49 6e 66 6f 20 3d 20 7b 20 27 2a 27 2c 20 27  bInfo = { '*', '
1daeb 3f 27 2c 20 27 5b 27 2c 20 30 20 7d 3b 0a 2f 2a  ?', '[', 0 };./*
1daec 20 54 68 65 20 63 6f 72 72 65 63 74 20 53 51 4c   The correct SQL
1daed 2d 39 32 20 62 65 68 61 76 69 6f 72 20 69 73 20  -92 behavior is 
1daee 66 6f 72 20 74 68 65 20 4c 49 4b 45 20 6f 70 65  for the LIKE ope
1daef 72 61 74 6f 72 20 74 6f 20 69 67 6e 6f 72 65 0a  rator to ignore.
1daf0 2a 2a 20 63 61 73 65 2e 20 20 54 68 75 73 20 20  ** case.  Thus  
1daf1 27 61 27 20 4c 49 4b 45 20 27 41 27 20 77 6f 75  'a' LIKE 'A' wou
1daf2 6c 64 20 62 65 20 74 72 75 65 2e 20 2a 2f 0a 73  ld be true. */.s
1daf3 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75  tatic const stru
1daf4 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 6c  ct compareInfo l
1daf5 69 6b 65 49 6e 66 6f 4e 6f 72 6d 20 3d 20 7b 20  ikeInfoNorm = { 
1daf6 27 25 27 2c 20 27 5f 27 2c 20 20 20 30 2c 20 31  '%', '_',   0, 1
1daf7 20 7d 3b 0a 2f 2a 20 49 66 20 53 51 4c 49 54 45   };./* If SQLITE
1daf8 5f 43 41 53 45 5f 53 45 4e 53 49 54 49 56 45 5f  _CASE_SENSITIVE_
1daf9 4c 49 4b 45 20 69 73 20 64 65 66 69 6e 65 64 2c  LIKE is defined,
1dafa 20 74 68 65 6e 20 74 68 65 20 4c 49 4b 45 20 6f   then the LIKE o
1dafb 70 65 72 61 74 6f 72 0a 2a 2a 20 69 73 20 63 61  perator.** is ca
1dafc 73 65 20 73 65 6e 73 69 74 69 76 65 20 63 61 75  se sensitive cau
1dafd 73 69 6e 67 20 27 61 27 20 4c 49 4b 45 20 27 41  sing 'a' LIKE 'A
1dafe 27 20 74 6f 20 62 65 20 66 61 6c 73 65 20 2a 2f  ' to be false */
1daff 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74  .static const st
1db00 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f  ruct compareInfo
1db01 20 6c 69 6b 65 49 6e 66 6f 41 6c 74 20 3d 20 7b   likeInfoAlt = {
1db02 20 27 25 27 2c 20 27 5f 27 2c 20 20 20 30 2c 20   '%', '_',   0, 
1db03 30 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  0 };../*.** Comp
1db04 61 72 65 20 74 77 6f 20 55 54 46 2d 38 20 73 74  are two UTF-8 st
1db05 72 69 6e 67 73 20 66 6f 72 20 65 71 75 61 6c 69  rings for equali
1db06 74 79 20 77 68 65 72 65 20 74 68 65 20 66 69 72  ty where the fir
1db07 73 74 20 73 74 72 69 6e 67 20 63 61 6e 0a 2a 2a  st string can.**
1db08 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 62 65 20   potentially be 
1db09 61 20 22 67 6c 6f 62 22 20 65 78 70 72 65 73 73  a "glob" express
1db0a 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20 74 72 75  ion.  Return tru
1db0b 65 20 28 31 29 20 69 66 20 74 68 65 79 0a 2a 2a  e (1) if they.**
1db0c 20 61 72 65 20 74 68 65 20 73 61 6d 65 20 61 6e   are the same an
1db0d 64 20 66 61 6c 73 65 20 28 30 29 20 69 66 20 74  d false (0) if t
1db0e 68 65 79 20 61 72 65 20 64 69 66 66 65 72 65 6e  hey are differen
1db0f 74 2e 0a 2a 2a 0a 2a 2a 20 47 6c 6f 62 62 69 6e  t..**.** Globbin
1db10 67 20 72 75 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20  g rules:.**.**  
1db11 20 20 20 20 27 2a 27 20 20 20 20 20 20 20 4d 61      '*'       Ma
1db12 74 63 68 65 73 20 61 6e 79 20 73 65 71 75 65 6e  tches any sequen
1db13 63 65 20 6f 66 20 7a 65 72 6f 20 6f 72 20 6d 6f  ce of zero or mo
1db14 72 65 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a  re characters..*
1db15 2a 0a 2a 2a 20 20 20 20 20 20 27 3f 27 20 20 20  *.**      '?'   
1db16 20 20 20 20 4d 61 74 63 68 65 73 20 65 78 61 63      Matches exac
1db17 74 6c 79 20 6f 6e 65 20 63 68 61 72 61 63 74 65  tly one characte
1db18 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 5b 2e 2e  r..**.**     [..
1db19 2e 5d 20 20 20 20 20 20 4d 61 74 63 68 65 73 20  .]      Matches 
1db1a 6f 6e 65 20 63 68 61 72 61 63 74 65 72 20 66 72  one character fr
1db1b 6f 6d 20 74 68 65 20 65 6e 63 6c 6f 73 65 64 20  om the enclosed 
1db1c 6c 69 73 74 20 6f 66 0a 2a 2a 20 20 20 20 20 20  list of.**      
1db1d 20 20 20 20 20 20 20 20 20 20 63 68 61 72 61 63            charac
1db1e 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ters..**.**     
1db1f 5b 5e 2e 2e 2e 5d 20 20 20 20 20 4d 61 74 63 68  [^...]     Match
1db20 65 73 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72  es one character
1db21 20 6e 6f 74 20 69 6e 20 74 68 65 20 65 6e 63 6c   not in the encl
1db22 6f 73 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a  osed list..**.**
1db23 20 57 69 74 68 20 74 68 65 20 5b 2e 2e 2e 5d 20   With the [...] 
1db24 61 6e 64 20 5b 5e 2e 2e 2e 5d 20 6d 61 74 63 68  and [^...] match
1db25 69 6e 67 2c 20 61 20 27 5d 27 20 63 68 61 72 61  ing, a ']' chara
1db26 63 74 65 72 20 63 61 6e 20 62 65 20 69 6e 63 6c  cter can be incl
1db27 75 64 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 6c  uded.** in the l
1db28 69 73 74 20 62 79 20 6d 61 6b 69 6e 67 20 69 74  ist by making it
1db29 20 74 68 65 20 66 69 72 73 74 20 63 68 61 72 61   the first chara
1db2a 63 74 65 72 20 61 66 74 65 72 20 27 5b 27 20 6f  cter after '[' o
1db2b 72 20 27 5e 27 2e 20 20 41 0a 2a 2a 20 72 61 6e  r '^'.  A.** ran
1db2c 67 65 20 6f 66 20 63 68 61 72 61 63 74 65 72 73  ge of characters
1db2d 20 63 61 6e 20 62 65 20 73 70 65 63 69 66 69 65   can be specifie
1db2e 64 20 75 73 69 6e 67 20 27 2d 27 2e 20 20 45 78  d using '-'.  Ex
1db2f 61 6d 70 6c 65 3a 0a 2a 2a 20 22 5b 61 2d 7a 5d  ample:.** "[a-z]
1db30 22 20 6d 61 74 63 68 65 73 20 61 6e 79 20 73 69  " matches any si
1db31 6e 67 6c 65 20 6c 6f 77 65 72 2d 63 61 73 65 20  ngle lower-case 
1db32 6c 65 74 74 65 72 2e 20 20 54 6f 20 6d 61 74 63  letter.  To matc
1db33 68 20 61 20 27 2d 27 2c 20 6d 61 6b 65 0a 2a 2a  h a '-', make.**
1db34 20 69 74 20 74 68 65 20 6c 61 73 74 20 63 68 61   it the last cha
1db35 72 61 63 74 65 72 20 69 6e 20 74 68 65 20 6c 69  racter in the li
1db36 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  st..**.** This r
1db37 6f 75 74 69 6e 65 20 69 73 20 75 73 75 61 6c 6c  outine is usuall
1db38 79 20 71 75 69 63 6b 2c 20 62 75 74 20 63 61 6e  y quick, but can
1db39 20 62 65 20 4e 2a 2a 32 20 69 6e 20 74 68 65 20   be N**2 in the 
1db3a 77 6f 72 73 74 20 63 61 73 65 2e 0a 2a 2a 0a 2a  worst case..**.*
1db3b 2a 20 48 69 6e 74 73 3a 20 74 6f 20 6d 61 74 63  * Hints: to matc
1db3c 68 20 27 2a 27 20 6f 72 20 27 3f 27 2c 20 70 75  h '*' or '?', pu
1db3d 74 20 74 68 65 6d 20 69 6e 20 22 5b 5d 22 2e 20  t them in "[]". 
1db3e 20 4c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   Like this:.**.*
1db3f 2a 20 20 20 20 20 20 20 20 20 61 62 63 5b 2a 5d  *         abc[*]
1db40 78 79 7a 20 20 20 20 20 20 20 20 4d 61 74 63 68  xyz        Match
1db41 65 73 20 22 61 62 63 2a 78 79 7a 22 20 6f 6e 6c  es "abc*xyz" onl
1db42 79 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  y.*/.static int 
1db43 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 0a  patternCompare(.
1db44 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 50 61 74    const u8 *zPat
1db45 74 65 72 6e 2c 20 20 20 20 20 20 20 20 20 20 20  tern,           
1db46 20 20 20 2f 2a 20 54 68 65 20 67 6c 6f 62 20 70     /* The glob p
1db47 61 74 74 65 72 6e 20 2a 2f 0a 20 20 63 6f 6e 73  attern */.  cons
1db48 74 20 75 38 20 2a 7a 53 74 72 69 6e 67 2c 20 20  t u8 *zString,  
1db49 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1db4a 54 68 65 20 73 74 72 69 6e 67 20 74 6f 20 63 6f  The string to co
1db4b 6d 70 61 72 65 20 61 67 61 69 6e 73 74 20 74 68  mpare against th
1db4c 65 20 67 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73  e glob */.  cons
1db4d 74 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65  t struct compare
1db4e 49 6e 66 6f 20 2a 70 49 6e 66 6f 2c 20 2f 2a 20  Info *pInfo, /* 
1db4f 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  Information abou
1db50 74 20 68 6f 77 20 74 6f 20 64 6f 20 74 68 65 20  t how to do the 
1db51 63 6f 6d 70 61 72 65 20 2a 2f 0a 20 20 63 6f 6e  compare */.  con
1db52 73 74 20 69 6e 74 20 65 73 63 20 20 20 20 20 20  st int esc      
1db53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1db54 20 54 68 65 20 65 73 63 61 70 65 20 63 68 61 72   The escape char
1db55 61 63 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e  acter */.){.  in
1db56 74 20 63 2c 20 63 32 3b 0a 20 20 69 6e 74 20 69  t c, c2;.  int i
1db57 6e 76 65 72 74 3b 0a 20 20 69 6e 74 20 73 65 65  nvert;.  int see
1db58 6e 3b 0a 20 20 75 38 20 6d 61 74 63 68 4f 6e 65  n;.  u8 matchOne
1db59 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 4f   = pInfo->matchO
1db5a 6e 65 3b 0a 20 20 75 38 20 6d 61 74 63 68 41 6c  ne;.  u8 matchAl
1db5b 6c 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68  l = pInfo->match
1db5c 41 6c 6c 3b 0a 20 20 75 38 20 6d 61 74 63 68 53  All;.  u8 matchS
1db5d 65 74 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74 63  et = pInfo->matc
1db5e 68 53 65 74 3b 0a 20 20 75 38 20 6e 6f 43 61 73  hSet;.  u8 noCas
1db5f 65 20 3d 20 70 49 6e 66 6f 2d 3e 6e 6f 43 61 73  e = pInfo->noCas
1db60 65 3b 20 0a 20 20 69 6e 74 20 70 72 65 76 45 73  e; .  int prevEs
1db61 63 61 70 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a  cape = 0;     /*
1db62 20 54 72 75 65 20 69 66 20 74 68 65 20 70 72 65   True if the pre
1db63 76 69 6f 75 73 20 63 68 61 72 61 63 74 65 72 20  vious character 
1db64 77 61 73 20 27 65 73 63 61 70 65 27 20 2a 2f 0a  was 'escape' */.
1db65 0a 20 20 77 68 69 6c 65 28 20 28 63 20 3d 20 73  .  while( (c = s
1db66 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
1db67 50 61 74 74 65 72 6e 2c 30 2c 26 7a 50 61 74 74  Pattern,0,&zPatt
1db68 65 72 6e 29 29 21 3d 30 20 29 7b 0a 20 20 20 20  ern))!=0 ){.    
1db69 69 66 28 20 21 70 72 65 76 45 73 63 61 70 65 20  if( !prevEscape 
1db6a 26 26 20 63 3d 3d 6d 61 74 63 68 41 6c 6c 20 29  && c==matchAll )
1db6b 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 28  {.      while( (
1db6c 63 3d 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  c=sqlite3Utf8Rea
1db6d 64 28 7a 50 61 74 74 65 72 6e 2c 30 2c 26 7a 50  d(zPattern,0,&zP
1db6e 61 74 74 65 72 6e 29 29 20 3d 3d 20 6d 61 74 63  attern)) == matc
1db6f 68 41 6c 6c 0a 20 20 20 20 20 20 20 20 20 20 20  hAll.           
1db70 20 20 20 20 7c 7c 20 63 20 3d 3d 20 6d 61 74 63      || c == matc
1db71 68 4f 6e 65 20 29 7b 0a 20 20 20 20 20 20 20 20  hOne ){.        
1db72 69 66 28 20 63 3d 3d 6d 61 74 63 68 4f 6e 65 20  if( c==matchOne 
1db73 26 26 20 73 71 6c 69 74 65 33 55 74 66 38 52 65  && sqlite3Utf8Re
1db74 61 64 28 7a 53 74 72 69 6e 67 2c 20 30 2c 20 26  ad(zString, 0, &
1db75 7a 53 74 72 69 6e 67 29 3d 3d 30 20 29 7b 0a 20  zString)==0 ){. 
1db76 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
1db77 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  0;.        }.   
1db78 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63     }.      if( c
1db79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
1db7a 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d  eturn 1;.      }
1db7b 65 6c 73 65 20 69 66 28 20 63 3d 3d 65 73 63 20  else if( c==esc 
1db7c 29 7b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 73  ){.        c = s
1db7d 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
1db7e 50 61 74 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61  Pattern, 0, &zPa
1db7f 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 20 20  ttern);.        
1db80 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20  if( c==0 ){.    
1db81 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
1db82 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1db83 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74  }else if( c==mat
1db84 63 68 53 65 74 20 29 7b 0a 20 20 20 20 20 20 20  chSet ){.       
1db85 20 61 73 73 65 72 74 28 20 65 73 63 3d 3d 30 20   assert( esc==0 
1db86 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  );         /* Th
1db87 69 73 20 69 73 20 47 4c 4f 42 2c 20 6e 6f 74 20  is is GLOB, not 
1db88 4c 49 4b 45 20 2a 2f 0a 20 20 20 20 20 20 20 20  LIKE */.        
1db89 61 73 73 65 72 74 28 20 6d 61 74 63 68 53 65 74  assert( matchSet
1db8a 3c 30 78 38 30 20 29 3b 20 20 2f 2a 20 27 5b 27  <0x80 );  /* '['
1db8b 20 69 73 20 61 20 73 69 6e 67 6c 65 2d 62 79 74   is a single-byt
1db8c 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20  e character */. 
1db8d 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a         while( *z
1db8e 53 74 72 69 6e 67 20 26 26 20 70 61 74 74 65 72  String && patter
1db8f 6e 43 6f 6d 70 61 72 65 28 26 7a 50 61 74 74 65  nCompare(&zPatte
1db90 72 6e 5b 2d 31 5d 2c 7a 53 74 72 69 6e 67 2c 70  rn[-1],zString,p
1db91 49 6e 66 6f 2c 65 73 63 29 3d 3d 30 20 29 7b 0a  Info,esc)==0 ){.
1db92 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
1db93 5f 53 4b 49 50 5f 55 54 46 38 28 7a 53 74 72 69  _SKIP_UTF8(zStri
1db94 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ng);.        }. 
1db95 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 2a 7a         return *z
1db96 53 74 72 69 6e 67 21 3d 30 3b 0a 20 20 20 20 20  String!=0;.     
1db97 20 7d 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20   }.      while( 
1db98 28 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66  (c2 = sqlite3Utf
1db99 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 30 2c  8Read(zString,0,
1db9a 26 7a 53 74 72 69 6e 67 29 29 21 3d 30 20 29 7b  &zString))!=0 ){
1db9b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 6f 43  .        if( noC
1db9c 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ase ){.         
1db9d 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65   GlogUpperToLowe
1db9e 72 28 63 32 29 3b 0a 20 20 20 20 20 20 20 20 20  r(c2);.         
1db9f 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65   GlogUpperToLowe
1dba0 72 28 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20  r(c);.          
1dba1 77 68 69 6c 65 28 20 63 32 20 21 3d 20 30 20 26  while( c2 != 0 &
1dba2 26 20 63 32 20 21 3d 20 63 20 29 7b 0a 20 20 20  & c2 != c ){.   
1dba3 20 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71           c2 = sq
1dba4 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 53  lite3Utf8Read(zS
1dba5 74 72 69 6e 67 2c 20 30 2c 20 26 7a 53 74 72 69  tring, 0, &zStri
1dba6 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ng);.           
1dba7 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65   GlogUpperToLowe
1dba8 72 28 63 32 29 3b 0a 20 20 20 20 20 20 20 20 20  r(c2);.         
1dba9 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65   }.        }else
1dbaa 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c  {.          whil
1dbab 65 28 20 63 32 20 21 3d 20 30 20 26 26 20 63 32  e( c2 != 0 && c2
1dbac 20 21 3d 20 63 20 29 7b 0a 20 20 20 20 20 20 20   != c ){.       
1dbad 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65       c2 = sqlite
1dbae 33 55 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e  3Utf8Read(zStrin
1dbaf 67 2c 20 30 2c 20 26 7a 53 74 72 69 6e 67 29 3b  g, 0, &zString);
1dbb0 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1dbb1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
1dbb2 66 28 20 63 32 3d 3d 30 20 29 20 72 65 74 75 72  f( c2==0 ) retur
1dbb3 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28  n 0;.        if(
1dbb4 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28   patternCompare(
1dbb5 7a 50 61 74 74 65 72 6e 2c 7a 53 74 72 69 6e 67  zPattern,zString
1dbb6 2c 70 49 6e 66 6f 2c 65 73 63 29 20 29 20 72 65  ,pInfo,esc) ) re
1dbb7 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a  turn 1;.      }.
1dbb8 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
1dbb9 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70      }else if( !p
1dbba 72 65 76 45 73 63 61 70 65 20 26 26 20 63 3d 3d  revEscape && c==
1dbbb 6d 61 74 63 68 4f 6e 65 20 29 7b 0a 20 20 20 20  matchOne ){.    
1dbbc 20 20 69 66 28 20 73 71 6c 69 74 65 33 55 74 66    if( sqlite3Utf
1dbbd 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 30  8Read(zString, 0
1dbbe 2c 20 26 7a 53 74 72 69 6e 67 29 3d 3d 30 20 29  , &zString)==0 )
1dbbf 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
1dbc0 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
1dbc1 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74  }else if( c==mat
1dbc2 63 68 53 65 74 20 29 7b 0a 20 20 20 20 20 20 69  chSet ){.      i
1dbc3 6e 74 20 70 72 69 6f 72 5f 63 20 3d 20 30 3b 0a  nt prior_c = 0;.
1dbc4 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65 73        assert( es
1dbc5 63 3d 3d 30 20 29 3b 20 20 20 20 2f 2a 20 54 68  c==0 );    /* Th
1dbc6 69 73 20 6f 6e 6c 79 20 6f 63 63 75 72 73 20 66  is only occurs f
1dbc7 6f 72 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c 49 4b  or GLOB, not LIK
1dbc8 45 20 2a 2f 0a 20 20 20 20 20 20 73 65 65 6e 20  E */.      seen 
1dbc9 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e 76 65 72  = 0;.      inver
1dbca 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 20 3d  t = 0;.      c =
1dbcb 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
1dbcc 28 7a 53 74 72 69 6e 67 2c 20 30 2c 20 26 7a 53  (zString, 0, &zS
1dbcd 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66  tring);.      if
1dbce 28 20 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( c==0 ) return 
1dbcf 30 3b 0a 20 20 20 20 20 20 63 32 20 3d 20 73 71  0;.      c2 = sq
1dbd0 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50  lite3Utf8Read(zP
1dbd1 61 74 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74  attern, 0, &zPat
1dbd2 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 69 66 28  tern);.      if(
1dbd3 20 63 32 3d 3d 27 5e 27 20 29 7b 0a 20 20 20 20   c2=='^' ){.    
1dbd4 20 20 20 20 69 6e 76 65 72 74 20 3d 20 31 3b 0a      invert = 1;.
1dbd5 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c          c2 = sql
1dbd6 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61  ite3Utf8Read(zPa
1dbd7 74 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74  ttern, 0, &zPatt
1dbd8 65 72 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ern);.      }.  
1dbd9 20 20 20 20 69 66 28 20 63 32 3d 3d 27 5d 27 20      if( c2==']' 
1dbda 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63  ){.        if( c
1dbdb 3d 3d 27 5d 27 20 29 20 73 65 65 6e 20 3d 20 31  ==']' ) seen = 1
1dbdc 3b 0a 20 20 20 20 20 20 20 20 63 32 20 3d 20 73  ;.        c2 = s
1dbdd 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
1dbde 50 61 74 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61  Pattern, 0, &zPa
1dbdf 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 7d 0a  ttern);.      }.
1dbe0 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 32 20        while( c2 
1dbe1 26 26 20 63 32 21 3d 27 5d 27 20 29 7b 0a 20 20  && c2!=']' ){.  
1dbe2 20 20 20 20 20 20 69 66 28 20 63 32 3d 3d 27 2d        if( c2=='-
1dbe3 27 20 26 26 20 7a 50 61 74 74 65 72 6e 5b 30 5d  ' && zPattern[0]
1dbe4 21 3d 27 5d 27 20 26 26 20 7a 50 61 74 74 65 72  !=']' && zPatter
1dbe5 6e 5b 30 5d 21 3d 30 20 26 26 20 70 72 69 6f 72  n[0]!=0 && prior
1dbe6 5f 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  _c>0 ){.        
1dbe7 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74    c2 = sqlite3Ut
1dbe8 66 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c  f8Read(zPattern,
1dbe9 20 30 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a   0, &zPattern);.
1dbea 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 3e            if( c>
1dbeb 3d 70 72 69 6f 72 5f 63 20 26 26 20 63 3c 3d 63  =prior_c && c<=c
1dbec 32 20 29 20 73 65 65 6e 20 3d 20 31 3b 0a 20 20  2 ) seen = 1;.  
1dbed 20 20 20 20 20 20 20 20 70 72 69 6f 72 5f 63 20          prior_c 
1dbee 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  = 0;.        }el
1dbef 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  se{.          if
1dbf0 28 20 63 3d 3d 63 32 20 29 7b 0a 20 20 20 20 20  ( c==c2 ){.     
1dbf1 20 20 20 20 20 20 20 73 65 65 6e 20 3d 20 31 3b         seen = 1;
1dbf2 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1dbf3 20 20 20 20 20 20 20 70 72 69 6f 72 5f 63 20 3d         prior_c =
1dbf4 20 63 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   c2;.        }. 
1dbf5 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69         c2 = sqli
1dbf6 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74  te3Utf8Read(zPat
1dbf7 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74 65  tern, 0, &zPatte
1dbf8 72 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rn);.      }.   
1dbf9 20 20 20 69 66 28 20 63 32 3d 3d 30 20 7c 7c 20     if( c2==0 || 
1dbfa 28 73 65 65 6e 20 5e 20 69 6e 76 65 72 74 29 3d  (seen ^ invert)=
1dbfb 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  =0 ){.        re
1dbfc 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a  turn 0;.      }.
1dbfd 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65 73      }else if( es
1dbfe 63 3d 3d 63 20 26 26 20 21 70 72 65 76 45 73 63  c==c && !prevEsc
1dbff 61 70 65 20 29 7b 0a 20 20 20 20 20 20 70 72 65  ape ){.      pre
1dc00 76 45 73 63 61 70 65 20 3d 20 31 3b 0a 20 20 20  vEscape = 1;.   
1dc01 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 32   }else{.      c2
1dc02 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65   = sqlite3Utf8Re
1dc03 61 64 28 7a 53 74 72 69 6e 67 2c 20 30 2c 20 26  ad(zString, 0, &
1dc04 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20  zString);.      
1dc05 69 66 28 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20  if( noCase ){.  
1dc06 20 20 20 20 20 20 47 6c 6f 67 55 70 70 65 72 54        GlogUpperT
1dc07 6f 4c 6f 77 65 72 28 63 29 3b 0a 20 20 20 20 20  oLower(c);.     
1dc08 20 20 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f     GlogUpperToLo
1dc09 77 65 72 28 63 32 29 3b 0a 20 20 20 20 20 20 7d  wer(c2);.      }
1dc0a 0a 20 20 20 20 20 20 69 66 28 20 63 21 3d 63 32  .      if( c!=c2
1dc0b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
1dc0c 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 0;.      }.  
1dc0d 20 20 20 20 70 72 65 76 45 73 63 61 70 65 20 3d      prevEscape =
1dc0e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   0;.    }.  }.  
1dc0f 72 65 74 75 72 6e 20 2a 7a 53 74 72 69 6e 67 3d  return *zString=
1dc10 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 75  =0;.}../*.** Cou
1dc11 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  nt the number of
1dc12 20 74 69 6d 65 73 20 74 68 61 74 20 74 68 65 20   times that the 
1dc13 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 28 6f  LIKE operator (o
1dc14 72 20 47 4c 4f 42 20 77 68 69 63 68 20 69 73 0a  r GLOB which is.
1dc15 2a 2a 20 6a 75 73 74 20 61 20 76 61 72 69 61 74  ** just a variat
1dc16 69 6f 6e 20 6f 66 20 4c 49 4b 45 29 20 67 65 74  ion of LIKE) get
1dc17 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 69 73 20  s called.  This 
1dc18 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74  is used for test
1dc19 69 6e 67 0a 2a 2a 20 6f 6e 6c 79 2e 0a 2a 2f 0a  ing.** only..*/.
1dc1a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
1dc1b 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
1dc1c 74 20 73 71 6c 69 74 65 33 5f 6c 69 6b 65 5f 63  t sqlite3_like_c
1dc1d 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66  ount = 0;.#endif
1dc1e 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  .../*.** Impleme
1dc1f 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c  ntation of the l
1dc20 69 6b 65 28 29 20 53 51 4c 20 66 75 6e 63 74 69  ike() SQL functi
1dc21 6f 6e 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69  on.  This functi
1dc22 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a  on implements.**
1dc23 20 74 68 65 20 62 75 69 6c 64 2d 69 6e 20 4c 49   the build-in LI
1dc24 4b 45 20 6f 70 65 72 61 74 6f 72 2e 20 20 54 68  KE operator.  Th
1dc25 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
1dc26 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   to the function
1dc27 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 74 74 65   is the.** patte
1dc28 72 6e 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e  rn and the secon
1dc29 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  d argument is th
1dc2a 65 20 73 74 72 69 6e 67 2e 20 20 53 6f 2c 20 74  e string.  So, t
1dc2b 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
1dc2c 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 41  s:.**.**       A
1dc2d 20 4c 49 4b 45 20 42 0a 2a 2a 0a 2a 2a 20 69 73   LIKE B.**.** is
1dc2e 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20   implemented as 
1dc2f 6c 69 6b 65 28 42 2c 41 29 2e 0a 2a 2a 0a 2a 2a  like(B,A)..**.**
1dc30 20 54 68 69 73 20 73 61 6d 65 20 66 75 6e 63 74   This same funct
1dc31 69 6f 6e 20 28 77 69 74 68 20 61 20 64 69 66 66  ion (with a diff
1dc32 65 72 65 6e 74 20 63 6f 6d 70 61 72 65 49 6e 66  erent compareInf
1dc33 6f 20 73 74 72 75 63 74 75 72 65 29 20 63 6f 6d  o structure) com
1dc34 70 75 74 65 73 0a 2a 2a 20 74 68 65 20 47 4c 4f  putes.** the GLO
1dc35 42 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73  B operator..*/.s
1dc36 74 61 74 69 63 20 76 6f 69 64 20 6c 69 6b 65 46  tatic void likeF
1dc37 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
1dc38 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
1dc39 20 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 0a 20   .  int argc, . 
1dc3a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
1dc3b 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74  *argv.){.  const
1dc3c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
1dc3d 7a 41 2c 20 2a 7a 42 3b 0a 20 20 69 6e 74 20 65  zA, *zB;.  int e
1dc3e 73 63 61 70 65 20 3d 20 30 3b 0a 20 20 73 71 6c  scape = 0;.  sql
1dc3f 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74  ite3 *db = sqlit
1dc40 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
1dc41 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 0a  ndle(context);..
1dc42 20 20 7a 42 20 3d 20 73 71 6c 69 74 65 33 5f 76    zB = sqlite3_v
1dc43 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
1dc44 5d 29 3b 0a 20 20 7a 41 20 3d 20 73 71 6c 69 74  ]);.  zA = sqlit
1dc45 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
1dc46 67 76 5b 31 5d 29 3b 0a 0a 20 20 2f 2a 20 4c 69  gv[1]);..  /* Li
1dc47 6d 69 74 20 74 68 65 20 6c 65 6e 67 74 68 20 6f  mit the length o
1dc48 66 20 74 68 65 20 4c 49 4b 45 20 6f 72 20 47 4c  f the LIKE or GL
1dc49 4f 42 20 70 61 74 74 65 72 6e 20 74 6f 20 61 76  OB pattern to av
1dc4a 6f 69 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a  oid problems.  *
1dc4b 2a 20 6f 66 20 64 65 65 70 20 72 65 63 75 72 73  * of deep recurs
1dc4c 69 6f 6e 20 61 6e 64 20 4e 2a 4e 20 62 65 68 61  ion and N*N beha
1dc4d 76 69 6f 72 20 69 6e 20 70 61 74 74 65 72 6e 43  vior in patternC
1dc4e 6f 6d 70 61 72 65 28 29 2e 0a 20 20 2a 2f 0a 20  ompare()..  */. 
1dc4f 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
1dc50 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
1dc51 29 20 3e 0a 20 20 20 20 20 20 20 20 64 62 2d 3e  ) >.        db->
1dc52 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
1dc53 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e  MIT_LIKE_PATTERN
1dc54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20  _LENGTH] ){.    
1dc55 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
1dc56 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 4c  rror(context, "L
1dc57 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61 74 74  IKE or GLOB patt
1dc58 65 72 6e 20 74 6f 6f 20 63 6f 6d 70 6c 65 78 22  ern too complex"
1dc59 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65 74 75 72  , -1);.    retur
1dc5a 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  n;.  }.  assert(
1dc5b 20 7a 42 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c   zB==sqlite3_val
1dc5c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
1dc5d 20 29 3b 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67   );  /* Encoding
1dc5e 20 64 69 64 20 6e 6f 74 20 63 68 61 6e 67 65 20   did not change 
1dc5f 2a 2f 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d  */..  if( argc==
1dc60 33 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  3 ){.    /* The 
1dc61 65 73 63 61 70 65 20 63 68 61 72 61 63 74 65 72  escape character
1dc62 20 73 74 72 69 6e 67 20 6d 75 73 74 20 63 6f 6e   string must con
1dc63 73 69 73 74 20 6f 66 20 61 20 73 69 6e 67 6c 65  sist of a single
1dc64 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72   UTF-8 character
1dc65 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69  ..    ** Otherwi
1dc66 73 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72  se, return an er
1dc67 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ror..    */.    
1dc68 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
1dc69 68 61 72 20 2a 7a 45 73 63 20 3d 20 73 71 6c 69  har *zEsc = sqli
1dc6a 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
1dc6b 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 69 66 28  rgv[2]);.    if(
1dc6c 20 7a 45 73 63 3d 3d 30 20 29 20 72 65 74 75 72   zEsc==0 ) retur
1dc6d 6e 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  n;.    if( sqlit
1dc6e 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 28 63  e3Utf8CharLen((c
1dc6f 68 61 72 2a 29 7a 45 73 63 2c 20 2d 31 29 21 3d  har*)zEsc, -1)!=
1dc70 31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  1 ){.      sqlit
1dc71 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
1dc72 63 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20  context, .      
1dc73 20 20 20 20 22 45 53 43 41 50 45 20 65 78 70 72      "ESCAPE expr
1dc74 65 73 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 61  ession must be a
1dc75 20 73 69 6e 67 6c 65 20 63 68 61 72 61 63 74 65   single characte
1dc76 72 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 72  r", -1);.      r
1dc77 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20  eturn;.    }.   
1dc78 20 65 73 63 61 70 65 20 3d 20 73 71 6c 69 74 65   escape = sqlite
1dc79 33 55 74 66 38 52 65 61 64 28 7a 45 73 63 2c 20  3Utf8Read(zEsc, 
1dc7a 30 2c 20 26 7a 45 73 63 29 3b 0a 20 20 7d 0a 20  0, &zEsc);.  }. 
1dc7b 20 69 66 28 20 7a 41 20 26 26 20 7a 42 20 29 7b   if( zA && zB ){
1dc7c 0a 20 20 20 20 73 74 72 75 63 74 20 63 6f 6d 70  .    struct comp
1dc7d 61 72 65 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d  areInfo *pInfo =
1dc7e 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61   sqlite3_user_da
1dc7f 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a 23 69 66  ta(context);.#if
1dc80 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
1dc81 20 20 20 20 73 71 6c 69 74 65 33 5f 6c 69 6b 65      sqlite3_like
1dc82 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66  _count++;.#endif
1dc83 0a 20 20 20 20 0a 20 20 20 20 73 71 6c 69 74 65  .    .    sqlite
1dc84 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e  3_result_int(con
1dc85 74 65 78 74 2c 20 70 61 74 74 65 72 6e 43 6f 6d  text, patternCom
1dc86 70 61 72 65 28 7a 42 2c 20 7a 41 2c 20 70 49 6e  pare(zB, zA, pIn
1dc87 66 6f 2c 20 65 73 63 61 70 65 29 29 3b 0a 20 20  fo, escape));.  
1dc88 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  }.}../*.** Imple
1dc89 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
1dc8a 20 4e 55 4c 4c 49 46 28 78 2c 79 29 20 66 75 6e   NULLIF(x,y) fun
1dc8b 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 73 75  ction.  The resu
1dc8c 6c 74 20 69 73 20 74 68 65 20 66 69 72 73 74 0a  lt is the first.
1dc8d 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 66 20 74  ** argument if t
1dc8e 68 65 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65  he arguments are
1dc8f 20 64 69 66 66 65 72 65 6e 74 2e 20 20 54 68 65   different.  The
1dc90 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20   result is NULL 
1dc91 69 66 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65  if the.** argume
1dc92 6e 74 73 20 61 72 65 20 65 71 75 61 6c 20 74 6f  nts are equal to
1dc93 20 65 61 63 68 20 6f 74 68 65 72 2e 0a 2a 2f 0a   each other..*/.
1dc94 73 74 61 74 69 63 20 76 6f 69 64 20 6e 75 6c 6c  static void null
1dc95 69 66 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  ifFunc(.  sqlite
1dc96 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
1dc97 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
1dc98 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
1dc99 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 43 6f 6c 6c  **argv.){.  Coll
1dc9a 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 73 71 6c  Seq *pColl = sql
1dc9b 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53  ite3GetFuncCollS
1dc9c 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 69  eq(context);.  i
1dc9d 66 28 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d  f( sqlite3MemCom
1dc9e 70 61 72 65 28 61 72 67 76 5b 30 5d 2c 20 61 72  pare(argv[0], ar
1dc9f 67 76 5b 31 5d 2c 20 70 43 6f 6c 6c 29 21 3d 30  gv[1], pColl)!=0
1dca0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
1dca1 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e  result_value(con
1dca2 74 65 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0a  text, argv[0]);.
1dca3 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70    }.}../*.** Imp
1dca4 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
1dca5 68 65 20 56 45 52 53 49 4f 4e 28 2a 29 20 66 75  he VERSION(*) fu
1dca6 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 73  nction.  The res
1dca7 75 6c 74 20 69 73 20 74 68 65 20 76 65 72 73 69  ult is the versi
1dca8 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c  on.** of the SQL
1dca9 69 74 65 20 6c 69 62 72 61 72 79 20 74 68 61 74  ite library that
1dcaa 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2f 0a   is running..*/.
1dcab 73 74 61 74 69 63 20 76 6f 69 64 20 76 65 72 73  static void vers
1dcac 69 6f 6e 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  ionFunc(.  sqlit
1dcad 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
1dcae 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c  ext,.  int argc,
1dcaf 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
1dcb0 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 73 71 6c   **argv.){.  sql
1dcb1 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
1dcb2 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65  (context, sqlite
1dcb3 33 5f 76 65 72 73 69 6f 6e 2c 20 2d 31 2c 20 53  3_version, -1, S
1dcb4 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d  QLITE_STATIC);.}
1dcb5 0a 0a 2f 2a 20 41 72 72 61 79 20 66 6f 72 20 63  ../* Array for c
1dcb6 6f 6e 76 65 72 74 69 6e 67 20 66 72 6f 6d 20 68  onverting from h
1dcb7 61 6c 66 2d 62 79 74 65 73 20 28 6e 79 62 62 6c  alf-bytes (nybbl
1dcb8 65 73 29 20 69 6e 74 6f 20 41 53 43 49 49 20 68  es) into ASCII h
1dcb9 65 78 0a 2a 2a 20 64 69 67 69 74 73 2e 20 2a 2f  ex.** digits. */
1dcba 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68  .static const ch
1dcbb 61 72 20 68 65 78 64 69 67 69 74 73 5b 5d 20 3d  ar hexdigits[] =
1dcbc 20 7b 0a 20 20 27 30 27 2c 20 27 31 27 2c 20 27   {.  '0', '1', '
1dcbd 32 27 2c 20 27 33 27 2c 20 27 34 27 2c 20 27 35  2', '3', '4', '5
1dcbe 27 2c 20 27 36 27 2c 20 27 37 27 2c 0a 20 20 27  ', '6', '7',.  '
1dcbf 38 27 2c 20 27 39 27 2c 20 27 41 27 2c 20 27 42  8', '9', 'A', 'B
1dcc0 27 2c 20 27 43 27 2c 20 27 44 27 2c 20 27 45 27  ', 'C', 'D', 'E'
1dcc1 2c 20 27 46 27 20 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  , 'F' .};../*.**
1dcc2 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 2d 20   EXPERIMENTAL - 
1dcc3 54 68 69 73 20 69 73 20 6e 6f 74 20 61 6e 20 6f  This is not an o
1dcc4 66 66 69 63 69 61 6c 20 66 75 6e 63 74 69 6f 6e  fficial function
1dcc5 2e 20 20 54 68 65 20 69 6e 74 65 72 66 61 63 65  .  The interface
1dcc6 20 6d 61 79 0a 2a 2a 20 63 68 61 6e 67 65 2e 20   may.** change. 
1dcc7 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d   This function m
1dcc8 61 79 20 64 69 73 61 70 70 65 61 72 2e 20 20 44  ay disappear.  D
1dcc9 6f 20 6e 6f 74 20 77 72 69 74 65 20 63 6f 64 65  o not write code
1dcca 20 74 68 61 74 20 64 65 70 65 6e 64 73 0a 2a 2a   that depends.**
1dccb 20 6f 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f   on this functio
1dccc 6e 2e 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  n..**.** Impleme
1dccd 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 51  ntation of the Q
1dcce 55 4f 54 45 28 29 20 66 75 6e 63 74 69 6f 6e 2e  UOTE() function.
1dccf 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20    This function 
1dcd0 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 0a 2a  takes a single.*
1dcd1 2a 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20  * argument.  If 
1dcd2 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20  the argument is 
1dcd3 6e 75 6d 65 72 69 63 2c 20 74 68 65 20 72 65 74  numeric, the ret
1dcd4 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65  urn value is the
1dcd5 20 73 61 6d 65 20 61 73 0a 2a 2a 20 74 68 65 20   same as.** the 
1dcd6 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 74 68  argument.  If th
1dcd7 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 4e 55  e argument is NU
1dcd8 4c 4c 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76  LL, the return v
1dcd9 61 6c 75 65 20 69 73 20 74 68 65 20 73 74 72 69  alue is the stri
1dcda 6e 67 0a 2a 2a 20 22 4e 55 4c 4c 22 2e 20 20 4f  ng.** "NULL".  O
1dcdb 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 61 72  therwise, the ar
1dcdc 67 75 6d 65 6e 74 20 69 73 20 65 6e 63 6c 6f 73  gument is enclos
1dcdd 65 64 20 69 6e 20 73 69 6e 67 6c 65 20 71 75 6f  ed in single quo
1dcde 74 65 73 20 77 69 74 68 0a 2a 2a 20 73 69 6e 67  tes with.** sing
1dcdf 6c 65 2d 71 75 6f 74 65 20 65 73 63 61 70 65 73  le-quote escapes
1dce0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1dce1 20 71 75 6f 74 65 46 75 6e 63 28 73 71 6c 69 74   quoteFunc(sqlit
1dce2 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
1dce3 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
1dce4 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
1dce5 72 67 76 29 7b 0a 20 20 69 66 28 20 61 72 67 63  rgv){.  if( argc
1dce6 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73  <1 ) return;.  s
1dce7 77 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76  witch( sqlite3_v
1dce8 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30  alue_type(argv[0
1dce9 5d 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53  ]) ){.    case S
1dcea 51 4c 49 54 45 5f 4e 55 4c 4c 3a 20 7b 0a 20 20  QLITE_NULL: {.  
1dceb 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
1dcec 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
1dced 20 22 4e 55 4c 4c 22 2c 20 34 2c 20 53 51 4c 49   "NULL", 4, SQLI
1dcee 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
1dcef 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1dcf0 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49     case SQLITE_I
1dcf1 4e 54 45 47 45 52 3a 0a 20 20 20 20 63 61 73 65  NTEGER:.    case
1dcf2 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 7b   SQLITE_FLOAT: {
1dcf3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
1dcf4 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74  esult_value(cont
1dcf5 65 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0a 20  ext, argv[0]);. 
1dcf6 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1dcf7 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  }.    case SQLIT
1dcf8 45 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20  E_BLOB: {.      
1dcf9 63 68 61 72 20 2a 7a 54 65 78 74 20 3d 20 30 3b  char *zText = 0;
1dcfa 0a 20 20 20 20 20 20 63 68 61 72 20 63 6f 6e 73  .      char cons
1dcfb 74 20 2a 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 74  t *zBlob = sqlit
1dcfc 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72  e3_value_blob(ar
1dcfd 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 6e  gv[0]);.      in
1dcfe 74 20 6e 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65  t nBlob = sqlite
1dcff 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
1dd00 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 61 73  gv[0]);.      as
1dd01 73 65 72 74 28 20 7a 42 6c 6f 62 3d 3d 73 71 6c  sert( zBlob==sql
1dd02 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28  ite3_value_blob(
1dd03 61 72 67 76 5b 30 5d 29 20 29 3b 20 2f 2a 20 4e  argv[0]) ); /* N
1dd04 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67  o encoding chang
1dd05 65 20 2a 2f 0a 20 20 20 20 20 20 7a 54 65 78 74  e */.      zText
1dd06 20 3d 20 28 63 68 61 72 20 2a 29 63 6f 6e 74 65   = (char *)conte
1dd07 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74  xtMalloc(context
1dd08 2c 20 28 32 2a 28 69 36 34 29 6e 42 6c 6f 62 29  , (2*(i64)nBlob)
1dd09 2b 34 29 3b 20 0a 20 20 20 20 20 20 69 66 28 20  +4); .      if( 
1dd0a 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20  zText ){.       
1dd0b 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20   int i;.        
1dd0c 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 42 6c 6f 62  for(i=0; i<nBlob
1dd0d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
1dd0e 20 20 7a 54 65 78 74 5b 28 69 2a 32 29 2b 32 5d    zText[(i*2)+2]
1dd0f 20 3d 20 68 65 78 64 69 67 69 74 73 5b 28 7a 42   = hexdigits[(zB
1dd10 6c 6f 62 5b 69 5d 3e 3e 34 29 26 30 78 30 46 5d  lob[i]>>4)&0x0F]
1dd11 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 54 65 78  ;.          zTex
1dd12 74 5b 28 69 2a 32 29 2b 33 5d 20 3d 20 68 65 78  t[(i*2)+3] = hex
1dd13 64 69 67 69 74 73 5b 28 7a 42 6c 6f 62 5b 69 5d  digits[(zBlob[i]
1dd14 29 26 30 78 30 46 5d 3b 0a 20 20 20 20 20 20 20  )&0x0F];.       
1dd15 20 7d 0a 20 20 20 20 20 20 20 20 7a 54 65 78 74   }.        zText
1dd16 5b 28 6e 42 6c 6f 62 2a 32 29 2b 32 5d 20 3d 20  [(nBlob*2)+2] = 
1dd17 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 7a 54  '\'';.        zT
1dd18 65 78 74 5b 28 6e 42 6c 6f 62 2a 32 29 2b 33 5d  ext[(nBlob*2)+3]
1dd19 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20   = '\0';.       
1dd1a 20 7a 54 65 78 74 5b 30 5d 20 3d 20 27 58 27 3b   zText[0] = 'X';
1dd1b 0a 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b 31  .        zText[1
1dd1c 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20  ] = '\'';.      
1dd1d 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
1dd1e 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a  _text(context, z
1dd1f 54 65 78 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45  Text, -1, SQLITE
1dd20 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20  _TRANSIENT);.   
1dd21 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
1dd22 65 28 7a 54 65 78 74 29 3b 0a 20 20 20 20 20 20  e(zText);.      
1dd23 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
1dd24 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51     }.    case SQ
1dd25 4c 49 54 45 5f 54 45 58 54 3a 20 7b 0a 20 20 20  LITE_TEXT: {.   
1dd26 20 20 20 69 6e 74 20 69 2c 6a 3b 0a 20 20 20 20     int i,j;.    
1dd27 20 20 75 36 34 20 6e 3b 0a 20 20 20 20 20 20 63    u64 n;.      c
1dd28 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
1dd29 61 72 20 2a 7a 41 72 67 20 3d 20 73 71 6c 69 74  ar *zArg = sqlit
1dd2a 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
1dd2b 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 63 68  gv[0]);.      ch
1dd2c 61 72 20 2a 7a 3b 0a 0a 20 20 20 20 20 20 69 66  ar *z;..      if
1dd2d 28 20 7a 41 72 67 3d 3d 30 20 29 20 72 65 74 75  ( zArg==0 ) retu
1dd2e 72 6e 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  rn;.      for(i=
1dd2f 30 2c 20 6e 3d 30 3b 20 7a 41 72 67 5b 69 5d 3b  0, n=0; zArg[i];
1dd30 20 69 2b 2b 29 7b 20 69 66 28 20 7a 41 72 67 5b   i++){ if( zArg[
1dd31 69 5d 3d 3d 27 5c 27 27 20 29 20 6e 2b 2b 3b 20  i]=='\'' ) n++; 
1dd32 7d 0a 20 20 20 20 20 20 7a 20 3d 20 63 6f 6e 74  }.      z = cont
1dd33 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78  extMalloc(contex
1dd34 74 2c 20 28 28 69 36 34 29 69 29 2b 28 28 69 36  t, ((i64)i)+((i6
1dd35 34 29 6e 29 2b 33 29 3b 0a 20 20 20 20 20 20 69  4)n)+3);.      i
1dd36 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 20 20  f( z ){.        
1dd37 7a 5b 30 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20  z[0] = '\'';.   
1dd38 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d       for(i=0, j=
1dd39 31 3b 20 7a 41 72 67 5b 69 5d 3b 20 69 2b 2b 29  1; zArg[i]; i++)
1dd3a 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b  {.          z[j+
1dd3b 2b 5d 20 3d 20 7a 41 72 67 5b 69 5d 3b 0a 20 20  +] = zArg[i];.  
1dd3c 20 20 20 20 20 20 20 20 69 66 28 20 7a 41 72 67          if( zArg
1dd3d 5b 69 5d 3d 3d 27 5c 27 27 20 29 7b 0a 20 20 20  [i]=='\'' ){.   
1dd3e 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20           z[j++] 
1dd3f 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20  = '\'';.        
1dd40 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
1dd41 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27        z[j++] = '
1dd42 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 7a 5b 6a  \'';.        z[j
1dd43 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 73  ] = 0;.        s
1dd44 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
1dd45 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 6a  xt(context, z, j
1dd46 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b  , sqlite3_free);
1dd47 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1dd48 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20   }.}../*.** The 
1dd49 68 65 78 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20  hex() function. 
1dd4a 20 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 61   Interpret the a
1dd4b 72 67 75 6d 65 6e 74 20 61 73 20 61 20 62 6c 6f  rgument as a blo
1dd4c 62 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20  b.  Return.** a 
1dd4d 68 65 78 61 64 65 63 69 6d 61 6c 20 72 65 6e 64  hexadecimal rend
1dd4e 65 72 69 6e 67 20 61 73 20 74 65 78 74 2e 0a 2a  ering as text..*
1dd4f 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 65  /.static void he
1dd50 78 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  xFunc(.  sqlite3
1dd51 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
1dd52 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
1dd53 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
1dd54 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69  *argv.){.  int i
1dd55 2c 20 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73  , n;.  const uns
1dd56 69 67 6e 65 64 20 63 68 61 72 20 2a 70 42 6c 6f  igned char *pBlo
1dd57 62 3b 0a 20 20 63 68 61 72 20 2a 7a 48 65 78 2c  b;.  char *zHex,
1dd58 20 2a 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 61   *z;.  assert( a
1dd59 72 67 63 3d 3d 31 20 29 3b 0a 20 20 70 42 6c 6f  rgc==1 );.  pBlo
1dd5a 62 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  b = sqlite3_valu
1dd5b 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b  e_blob(argv[0]);
1dd5c 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76  .  n = sqlite3_v
1dd5d 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
1dd5e 30 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0]);.  assert( p
1dd5f 42 6c 6f 62 3d 3d 73 71 6c 69 74 65 33 5f 76 61  Blob==sqlite3_va
1dd60 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d  lue_blob(argv[0]
1dd61 29 20 29 3b 20 20 2f 2a 20 4e 6f 20 65 6e 63 6f  ) );  /* No enco
1dd62 64 69 6e 67 20 63 68 61 6e 67 65 20 2a 2f 0a 20  ding change */. 
1dd63 20 7a 20 3d 20 7a 48 65 78 20 3d 20 63 6f 6e 74   z = zHex = cont
1dd64 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78  extMalloc(contex
1dd65 74 2c 20 28 28 69 36 34 29 6e 29 2a 32 20 2b 20  t, ((i64)n)*2 + 
1dd66 31 29 3b 0a 20 20 69 66 28 20 7a 48 65 78 20 29  1);.  if( zHex )
1dd67 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  {.    for(i=0; i
1dd68 3c 6e 3b 20 69 2b 2b 2c 20 70 42 6c 6f 62 2b 2b  <n; i++, pBlob++
1dd69 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65  ){.      unsigne
1dd6a 64 20 63 68 61 72 20 63 20 3d 20 2a 70 42 6c 6f  d char c = *pBlo
1dd6b 62 3b 0a 20 20 20 20 20 20 2a 28 7a 2b 2b 29 20  b;.      *(z++) 
1dd6c 3d 20 68 65 78 64 69 67 69 74 73 5b 28 63 3e 3e  = hexdigits[(c>>
1dd6d 34 29 26 30 78 66 5d 3b 0a 20 20 20 20 20 20 2a  4)&0xf];.      *
1dd6e 28 7a 2b 2b 29 20 3d 20 68 65 78 64 69 67 69 74  (z++) = hexdigit
1dd6f 73 5b 63 26 30 78 66 5d 3b 0a 20 20 20 20 7d 0a  s[c&0xf];.    }.
1dd70 20 20 20 20 2a 7a 20 3d 20 30 3b 0a 20 20 20 20      *z = 0;.    
1dd71 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
1dd72 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 48 65  ext(context, zHe
1dd73 78 2c 20 6e 2a 32 2c 20 73 71 6c 69 74 65 33 5f  x, n*2, sqlite3_
1dd74 66 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  free);.  }.}../*
1dd75 0a 2a 2a 20 54 68 65 20 7a 65 72 6f 62 6c 6f 62  .** The zeroblob
1dd76 28 4e 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  (N) function ret
1dd77 75 72 6e 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c  urns a zero-fill
1dd78 65 64 20 62 6c 6f 62 20 6f 66 20 73 69 7a 65 20  ed blob of size 
1dd79 4e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74  N bytes..*/.stat
1dd7a 69 63 20 76 6f 69 64 20 7a 65 72 6f 62 6c 6f 62  ic void zeroblob
1dd7b 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
1dd7c 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
1dd7d 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
1dd7e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
1dd7f 61 72 67 76 0a 29 7b 0a 20 20 69 36 34 20 6e 3b  argv.){.  i64 n;
1dd80 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
1dd81 3d 31 20 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69  =1 );.  n = sqli
1dd82 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28  te3_value_int64(
1dd83 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20  argv[0]);.  if( 
1dd84 6e 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e  n>SQLITE_MAX_LEN
1dd85 47 54 48 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  GTH ){.    sqlit
1dd86 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
1dd87 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b  toobig(context);
1dd88 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
1dd89 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72  lite3_result_zer
1dd8a 6f 62 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20 6e  oblob(context, n
1dd8b 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
1dd8c 54 68 65 20 72 65 70 6c 61 63 65 28 29 20 66 75  The replace() fu
1dd8d 6e 63 74 69 6f 6e 2e 20 20 54 68 72 65 65 20 61  nction.  Three a
1dd8e 72 67 75 6d 65 6e 74 73 20 61 72 65 20 61 6c 6c  rguments are all
1dd8f 20 73 74 72 69 6e 67 73 3a 20 63 61 6c 6c 0a 2a   strings: call.*
1dd90 2a 20 74 68 65 6d 20 41 2c 20 42 2c 20 61 6e 64  * them A, B, and
1dd91 20 43 2e 20 54 68 65 20 72 65 73 75 6c 74 20 69   C. The result i
1dd92 73 20 61 6c 73 6f 20 61 20 73 74 72 69 6e 67 20  s also a string 
1dd93 77 68 69 63 68 20 69 73 20 64 65 72 69 76 65 64  which is derived
1dd94 0a 2a 2a 20 66 72 6f 6d 20 41 20 62 79 20 72 65  .** from A by re
1dd95 70 6c 61 63 69 6e 67 20 65 76 65 72 79 20 6f 63  placing every oc
1dd96 63 75 72 61 6e 63 65 20 6f 66 20 42 20 77 69 74  curance of B wit
1dd97 68 20 43 2e 20 20 54 68 65 20 6d 61 74 63 68 0a  h C.  The match.
1dd98 2a 2a 20 6d 75 73 74 20 62 65 20 65 78 61 63 74  ** must be exact
1dd99 2e 20 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71  .  Collating seq
1dd9a 75 65 6e 63 65 73 20 61 72 65 20 6e 6f 74 20 75  uences are not u
1dd9b 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  sed..*/.static v
1dd9c 6f 69 64 20 72 65 70 6c 61 63 65 46 75 6e 63 28  oid replaceFunc(
1dd9d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
1dd9e 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
1dd9f 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
1dda0 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
1dda1 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  ){.  const unsig
1dda2 6e 65 64 20 63 68 61 72 20 2a 7a 53 74 72 3b 20  ned char *zStr; 
1dda3 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e         /* The in
1dda4 70 75 74 20 73 74 72 69 6e 67 20 41 20 2a 2f 0a  put string A */.
1dda5 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
1dda6 20 63 68 61 72 20 2a 7a 50 61 74 74 65 72 6e 3b   char *zPattern;
1dda7 20 20 20 20 2f 2a 20 54 68 65 20 70 61 74 74 65      /* The patte
1dda8 72 6e 20 73 74 72 69 6e 67 20 42 20 2a 2f 0a 20  rn string B */. 
1dda9 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
1ddaa 63 68 61 72 20 2a 7a 52 65 70 3b 20 20 20 20 20  char *zRep;     
1ddab 20 20 20 2f 2a 20 54 68 65 20 72 65 70 6c 61 63     /* The replac
1ddac 65 6d 65 6e 74 20 73 74 72 69 6e 67 20 43 20 2a  ement string C *
1ddad 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
1ddae 72 20 2a 7a 4f 75 74 3b 20 20 20 20 20 20 20 20  r *zOut;        
1ddaf 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 75 74        /* The out
1ddb0 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 74  put */.  int nSt
1ddb1 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
1ddb2 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 53 74    /* Size of zSt
1ddb3 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 74 74  r */.  int nPatt
1ddb4 65 72 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  ern;            
1ddb5 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 50 61 74 74  /* Size of zPatt
1ddb6 65 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65  ern */.  int nRe
1ddb7 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
1ddb8 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 52 65    /* Size of zRe
1ddb9 70 20 2a 2f 0a 20 20 69 36 34 20 6e 4f 75 74 3b  p */.  i64 nOut;
1ddba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ddbb 2f 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20  /* Maximum size 
1ddbc 6f 66 20 7a 4f 75 74 20 2a 2f 0a 20 20 69 6e 74  of zOut */.  int
1ddbd 20 6c 6f 6f 70 4c 69 6d 69 74 3b 20 20 20 20 20   loopLimit;     
1ddbe 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 7a 53        /* Last zS
1ddbf 74 72 5b 5d 20 74 68 61 74 20 6d 69 67 68 74 20  tr[] that might 
1ddc0 6d 61 74 63 68 20 7a 50 61 74 74 65 72 6e 5b 5d  match zPattern[]
1ddc1 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20   */.  int i, j; 
1ddc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1ddc3 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20  * Loop counters 
1ddc4 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72  */..  assert( ar
1ddc5 67 63 3d 3d 33 20 29 3b 0a 20 20 7a 53 74 72 20  gc==3 );.  zStr 
1ddc6 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
1ddc7 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20  text(argv[0]);. 
1ddc8 20 69 66 28 20 7a 53 74 72 3d 3d 30 20 29 20 72   if( zStr==0 ) r
1ddc9 65 74 75 72 6e 3b 0a 20 20 6e 53 74 72 20 3d 20  eturn;.  nStr = 
1ddca 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
1ddcb 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  tes(argv[0]);.  
1ddcc 61 73 73 65 72 74 28 20 7a 53 74 72 3d 3d 73 71  assert( zStr==sq
1ddcd 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1ddce 28 61 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a  (argv[0]) );  /*
1ddcf 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61   No encoding cha
1ddd0 6e 67 65 20 2a 2f 0a 20 20 7a 50 61 74 74 65 72  nge */.  zPatter
1ddd1 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  n = sqlite3_valu
1ddd2 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b  e_text(argv[1]);
1ddd3 0a 20 20 69 66 28 20 7a 50 61 74 74 65 72 6e 3d  .  if( zPattern=
1ddd4 3d 30 20 7c 7c 20 7a 50 61 74 74 65 72 6e 5b 30  =0 || zPattern[0
1ddd5 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  ]==0 ) return;. 
1ddd6 20 6e 50 61 74 74 65 72 6e 20 3d 20 73 71 6c 69   nPattern = sqli
1ddd7 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
1ddd8 61 72 67 76 5b 31 5d 29 3b 0a 20 20 61 73 73 65  argv[1]);.  asse
1ddd9 72 74 28 20 7a 50 61 74 74 65 72 6e 3d 3d 73 71  rt( zPattern==sq
1ddda 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1dddb 28 61 72 67 76 5b 31 5d 29 20 29 3b 20 20 2f 2a  (argv[1]) );  /*
1dddc 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61   No encoding cha
1dddd 6e 67 65 20 2a 2f 0a 20 20 7a 52 65 70 20 3d 20  nge */.  zRep = 
1ddde 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1dddf 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 69  xt(argv[2]);.  i
1dde0 66 28 20 7a 52 65 70 3d 3d 30 20 29 20 72 65 74  f( zRep==0 ) ret
1dde1 75 72 6e 3b 0a 20 20 6e 52 65 70 20 3d 20 73 71  urn;.  nRep = sq
1dde2 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
1dde3 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 61 73  s(argv[2]);.  as
1dde4 73 65 72 74 28 20 7a 52 65 70 3d 3d 73 71 6c 69  sert( zRep==sqli
1dde5 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
1dde6 72 67 76 5b 32 5d 29 20 29 3b 0a 20 20 6e 4f 75  rgv[2]) );.  nOu
1dde7 74 20 3d 20 6e 53 74 72 20 2b 20 31 3b 0a 20 20  t = nStr + 1;.  
1dde8 61 73 73 65 72 74 28 20 6e 4f 75 74 3c 53 51 4c  assert( nOut<SQL
1dde9 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29  ITE_MAX_LENGTH )
1ddea 3b 0a 20 20 7a 4f 75 74 20 3d 20 63 6f 6e 74 65  ;.  zOut = conte
1ddeb 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74  xtMalloc(context
1ddec 2c 20 28 69 36 34 29 6e 4f 75 74 29 3b 0a 20 20  , (i64)nOut);.  
1dded 69 66 28 20 7a 4f 75 74 3d 3d 30 20 29 7b 0a 20  if( zOut==0 ){. 
1ddee 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
1ddef 20 6c 6f 6f 70 4c 69 6d 69 74 20 3d 20 6e 53 74   loopLimit = nSt
1ddf0 72 20 2d 20 6e 50 61 74 74 65 72 6e 3b 20 20 0a  r - nPattern;  .
1ddf1 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 69 3c 3d    for(i=j=0; i<=
1ddf2 6c 6f 6f 70 4c 69 6d 69 74 3b 20 69 2b 2b 29 7b  loopLimit; i++){
1ddf3 0a 20 20 20 20 69 66 28 20 7a 53 74 72 5b 69 5d  .    if( zStr[i]
1ddf4 21 3d 7a 50 61 74 74 65 72 6e 5b 30 5d 20 7c 7c  !=zPattern[0] ||
1ddf5 20 6d 65 6d 63 6d 70 28 26 7a 53 74 72 5b 69 5d   memcmp(&zStr[i]
1ddf6 2c 20 7a 50 61 74 74 65 72 6e 2c 20 6e 50 61 74  , zPattern, nPat
1ddf7 74 65 72 6e 29 20 29 7b 0a 20 20 20 20 20 20 7a  tern) ){.      z
1ddf8 4f 75 74 5b 6a 2b 2b 5d 20 3d 20 7a 53 74 72 5b  Out[j++] = zStr[
1ddf9 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  i];.    }else{. 
1ddfa 20 20 20 20 20 75 38 20 2a 7a 4f 6c 64 3b 0a 20       u8 *zOld;. 
1ddfb 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62       sqlite3 *db
1ddfc 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
1ddfd 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
1ddfe 74 65 78 74 29 3b 0a 20 20 20 20 20 20 6e 4f 75  text);.      nOu
1ddff 74 20 2b 3d 20 6e 52 65 70 20 2d 20 6e 50 61 74  t += nRep - nPat
1de00 74 65 72 6e 3b 0a 20 20 20 20 20 20 69 66 28 20  tern;.      if( 
1de01 6e 4f 75 74 3e 3d 64 62 2d 3e 61 4c 69 6d 69 74  nOut>=db->aLimit
1de02 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45  [SQLITE_LIMIT_LE
1de03 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 20 20 20  NGTH] ){.       
1de04 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1de05 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e  error_toobig(con
1de06 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 73  text);.        s
1de07 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4f 75 74  qlite3_free(zOut
1de08 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
1de09 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  n;.      }.     
1de0a 20 7a 4f 6c 64 20 3d 20 7a 4f 75 74 3b 0a 20 20   zOld = zOut;.  
1de0b 20 20 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74      zOut = sqlit
1de0c 65 33 5f 72 65 61 6c 6c 6f 63 28 7a 4f 75 74 2c  e3_realloc(zOut,
1de0d 20 28 69 6e 74 29 6e 4f 75 74 29 3b 0a 20 20 20   (int)nOut);.   
1de0e 20 20 20 69 66 28 20 7a 4f 75 74 3d 3d 30 20 29     if( zOut==0 )
1de0f 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1de10 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e  3_result_error_n
1de11 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20  omem(context);. 
1de12 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
1de13 72 65 65 28 7a 4f 6c 64 29 3b 0a 20 20 20 20 20  ree(zOld);.     
1de14 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20     return;.     
1de15 20 7d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28   }.      memcpy(
1de16 26 7a 4f 75 74 5b 6a 5d 2c 20 7a 52 65 70 2c 20  &zOut[j], zRep, 
1de17 6e 52 65 70 29 3b 0a 20 20 20 20 20 20 6a 20 2b  nRep);.      j +
1de18 3d 20 6e 52 65 70 3b 0a 20 20 20 20 20 20 69 20  = nRep;.      i 
1de19 2b 3d 20 6e 50 61 74 74 65 72 6e 2d 31 3b 0a 20  += nPattern-1;. 
1de1a 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72     }.  }.  asser
1de1b 74 28 20 6a 2b 6e 53 74 72 2d 69 2b 31 3d 3d 6e  t( j+nStr-i+1==n
1de1c 4f 75 74 20 29 3b 0a 20 20 6d 65 6d 63 70 79 28  Out );.  memcpy(
1de1d 26 7a 4f 75 74 5b 6a 5d 2c 20 26 7a 53 74 72 5b  &zOut[j], &zStr[
1de1e 69 5d 2c 20 6e 53 74 72 2d 69 29 3b 0a 20 20 6a  i], nStr-i);.  j
1de1f 20 2b 3d 20 6e 53 74 72 20 2d 20 69 3b 0a 20 20   += nStr - i;.  
1de20 61 73 73 65 72 74 28 20 6a 3c 3d 6e 4f 75 74 20  assert( j<=nOut 
1de21 29 3b 0a 20 20 7a 4f 75 74 5b 6a 5d 20 3d 20 30  );.  zOut[j] = 0
1de22 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ;.  sqlite3_resu
1de23 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
1de24 20 28 63 68 61 72 2a 29 7a 4f 75 74 2c 20 6a 2c   (char*)zOut, j,
1de25 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
1de26 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
1de27 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 54  ntation of the T
1de28 52 49 4d 28 29 2c 20 4c 54 52 49 4d 28 29 2c 20  RIM(), LTRIM(), 
1de29 61 6e 64 20 52 54 52 49 4d 28 29 20 66 75 6e 63  and RTRIM() func
1de2a 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 75 73  tions..** The us
1de2b 65 72 64 61 74 61 20 69 73 20 30 78 31 20 66 6f  erdata is 0x1 fo
1de2c 72 20 6c 65 66 74 20 74 72 69 6d 2c 20 30 78 32  r left trim, 0x2
1de2d 20 66 6f 72 20 72 69 67 68 74 20 74 72 69 6d 2c   for right trim,
1de2e 20 30 78 33 20 66 6f 72 20 62 6f 74 68 2e 0a 2a   0x3 for both..*
1de2f 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 72  /.static void tr
1de30 69 6d 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  imFunc(.  sqlite
1de31 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
1de32 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
1de33 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
1de34 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73  **argv.){.  cons
1de35 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
1de36 2a 7a 49 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a  *zIn;         /*
1de37 20 49 6e 70 75 74 20 73 74 72 69 6e 67 20 2a 2f   Input string */
1de38 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  .  const unsigne
1de39 64 20 63 68 61 72 20 2a 7a 43 68 61 72 53 65 74  d char *zCharSet
1de3a 3b 20 20 20 20 2f 2a 20 53 65 74 20 6f 66 20 63  ;    /* Set of c
1de3b 68 61 72 61 63 74 65 72 73 20 74 6f 20 74 72 69  haracters to tri
1de3c 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 3b 20  m */.  int nIn; 
1de3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1de3e 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1de3f 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 69  er of bytes in i
1de40 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 66 6c  nput */.  int fl
1de41 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ags;            
1de42 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31              /* 1
1de43 3a 20 74 72 69 6d 6c 65 66 74 20 20 32 3a 20 74  : trimleft  2: t
1de44 72 69 6d 72 69 67 68 74 20 20 33 3a 20 74 72 69  rimright  3: tri
1de45 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  m */.  int i;   
1de46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1de47 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
1de48 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 6e   counter */.  un
1de49 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4c 65  signed char *aLe
1de4a 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
1de4b 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 65 61 63  /* Length of eac
1de4c 68 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 7a  h character in z
1de4d 43 68 61 72 53 65 74 20 2a 2f 0a 20 20 75 6e 73  CharSet */.  uns
1de4e 69 67 6e 65 64 20 63 68 61 72 20 2a 2a 61 7a 43  igned char **azC
1de4f 68 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f  har;           /
1de50 2a 20 49 6e 64 69 76 69 64 75 61 6c 20 63 68 61  * Individual cha
1de51 72 61 63 74 65 72 73 20 69 6e 20 7a 43 68 61 72  racters in zChar
1de52 53 65 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68  Set */.  int nCh
1de53 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ar;             
1de54 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1de55 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65  mber of characte
1de56 72 73 20 69 6e 20 7a 43 68 61 72 53 65 74 20 2a  rs in zCharSet *
1de57 2f 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  /..  if( sqlite3
1de58 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
1de59 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c  [0])==SQLITE_NUL
1de5a 4c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  L ){.    return;
1de5b 0a 20 20 7d 0a 20 20 7a 49 6e 20 3d 20 73 71 6c  .  }.  zIn = sql
1de5c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
1de5d 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20  argv[0]);.  if( 
1de5e 7a 49 6e 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  zIn==0 ) return;
1de5f 0a 20 20 6e 49 6e 20 3d 20 73 71 6c 69 74 65 33  .  nIn = sqlite3
1de60 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
1de61 76 5b 30 5d 29 3b 0a 20 20 61 73 73 65 72 74 28  v[0]);.  assert(
1de62 20 7a 49 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 61   zIn==sqlite3_va
1de63 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
1de64 29 20 29 3b 0a 20 20 69 66 28 20 61 72 67 63 3d  ) );.  if( argc=
1de65 3d 31 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63  =1 ){.    static
1de66 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
1de67 63 68 61 72 20 6c 65 6e 4f 6e 65 5b 5d 20 3d 20  char lenOne[] = 
1de68 7b 20 31 20 7d 3b 0a 20 20 20 20 73 74 61 74 69  { 1 };.    stati
1de69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  c const unsigned
1de6a 20 63 68 61 72 20 2a 61 7a 4f 6e 65 5b 5d 20 3d   char *azOne[] =
1de6b 20 7b 20 28 75 38 2a 29 22 20 22 20 7d 3b 0a 20   { (u8*)" " };. 
1de6c 20 20 20 6e 43 68 61 72 20 3d 20 31 3b 0a 20 20     nChar = 1;.  
1de6d 20 20 61 4c 65 6e 20 3d 20 28 75 38 2a 29 6c 65    aLen = (u8*)le
1de6e 6e 4f 6e 65 3b 0a 20 20 20 20 61 7a 43 68 61 72  nOne;.    azChar
1de6f 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
1de70 72 20 2a 2a 29 61 7a 4f 6e 65 3b 0a 20 20 20 20  r **)azOne;.    
1de71 7a 43 68 61 72 53 65 74 20 3d 20 30 3b 0a 20 20  zCharSet = 0;.  
1de72 7d 65 6c 73 65 20 69 66 28 20 28 7a 43 68 61 72  }else if( (zChar
1de73 53 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  Set = sqlite3_va
1de74 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d  lue_text(argv[1]
1de75 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ))==0 ){.    ret
1de76 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  urn;.  }else{.  
1de77 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
1de78 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 66 6f   char *z;.    fo
1de79 72 28 7a 3d 7a 43 68 61 72 53 65 74 2c 20 6e 43  r(z=zCharSet, nC
1de7a 68 61 72 3d 30 3b 20 2a 7a 3b 20 6e 43 68 61 72  har=0; *z; nChar
1de7b 2b 2b 29 7b 0a 20 20 20 20 20 20 53 51 4c 49 54  ++){.      SQLIT
1de7c 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a  E_SKIP_UTF8(z);.
1de7d 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 43      }.    if( nC
1de7e 68 61 72 3e 30 20 29 7b 0a 20 20 20 20 20 20 61  har>0 ){.      a
1de7f 7a 43 68 61 72 20 3d 20 63 6f 6e 74 65 78 74 4d  zChar = contextM
1de80 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28  alloc(context, (
1de81 28 69 36 34 29 6e 43 68 61 72 29 2a 28 73 69 7a  (i64)nChar)*(siz
1de82 65 6f 66 28 63 68 61 72 2a 29 2b 31 29 29 3b 0a  eof(char*)+1));.
1de83 20 20 20 20 20 20 69 66 28 20 61 7a 43 68 61 72        if( azChar
1de84 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
1de85 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
1de86 20 20 20 20 20 61 4c 65 6e 20 3d 20 28 75 6e 73       aLen = (uns
1de87 69 67 6e 65 64 20 63 68 61 72 2a 29 26 61 7a 43  igned char*)&azC
1de88 68 61 72 5b 6e 43 68 61 72 5d 3b 0a 20 20 20 20  har[nChar];.    
1de89 20 20 66 6f 72 28 7a 3d 7a 43 68 61 72 53 65 74    for(z=zCharSet
1de8a 2c 20 6e 43 68 61 72 3d 30 3b 20 2a 7a 3b 20 6e  , nChar=0; *z; n
1de8b 43 68 61 72 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Char++){.       
1de8c 20 61 7a 43 68 61 72 5b 6e 43 68 61 72 5d 20 3d   azChar[nChar] =
1de8d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
1de8e 2a 29 7a 3b 0a 20 20 20 20 20 20 20 20 53 51 4c  *)z;.        SQL
1de8f 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29  ITE_SKIP_UTF8(z)
1de90 3b 0a 20 20 20 20 20 20 20 20 61 4c 65 6e 5b 6e  ;.        aLen[n
1de91 43 68 61 72 5d 20 3d 20 7a 20 2d 20 61 7a 43 68  Char] = z - azCh
1de92 61 72 5b 6e 43 68 61 72 5d 3b 0a 20 20 20 20 20  ar[nChar];.     
1de93 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69   }.    }.  }.  i
1de94 66 28 20 6e 43 68 61 72 3e 30 20 29 7b 0a 20 20  f( nChar>0 ){.  
1de95 20 20 66 6c 61 67 73 20 3d 20 28 69 6e 74 29 73    flags = (int)s
1de96 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
1de97 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 69  (context);.    i
1de98 66 28 20 66 6c 61 67 73 20 26 20 31 20 29 7b 0a  f( flags & 1 ){.
1de99 20 20 20 20 20 20 77 68 69 6c 65 28 20 6e 49 6e        while( nIn
1de9a 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  >0 ){.        in
1de9b 74 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 66  t len;.        f
1de9c 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 68 61 72 3b  or(i=0; i<nChar;
1de9d 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
1de9e 20 6c 65 6e 20 3d 20 61 4c 65 6e 5b 69 5d 3b 0a   len = aLen[i];.
1de9f 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6d 65            if( me
1dea0 6d 63 6d 70 28 7a 49 6e 2c 20 61 7a 43 68 61 72  mcmp(zIn, azChar
1dea1 5b 69 5d 2c 20 6c 65 6e 29 3d 3d 30 20 29 20 62  [i], len)==0 ) b
1dea2 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
1dea3 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d 6e          if( i>=n
1dea4 43 68 61 72 20 29 20 62 72 65 61 6b 3b 0a 20 20  Char ) break;.  
1dea5 20 20 20 20 20 20 7a 49 6e 20 2b 3d 20 6c 65 6e        zIn += len
1dea6 3b 0a 20 20 20 20 20 20 20 20 6e 49 6e 20 2d 3d  ;.        nIn -=
1dea7 20 6c 65 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20   len;.      }.  
1dea8 20 20 7d 0a 20 20 20 20 69 66 28 20 66 6c 61 67    }.    if( flag
1dea9 73 20 26 20 32 20 29 7b 0a 20 20 20 20 20 20 77  s & 2 ){.      w
1deaa 68 69 6c 65 28 20 6e 49 6e 3e 30 20 29 7b 0a 20  hile( nIn>0 ){. 
1deab 20 20 20 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a         int len;.
1deac 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
1dead 20 69 3c 6e 43 68 61 72 3b 20 69 2b 2b 29 7b 0a   i<nChar; i++){.
1deae 20 20 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20            len = 
1deaf 61 4c 65 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 20  aLen[i];.       
1deb0 20 20 20 69 66 28 20 6c 65 6e 3c 3d 6e 49 6e 20     if( len<=nIn 
1deb1 26 26 20 6d 65 6d 63 6d 70 28 26 7a 49 6e 5b 6e  && memcmp(&zIn[n
1deb2 49 6e 2d 6c 65 6e 5d 2c 61 7a 43 68 61 72 5b 69  In-len],azChar[i
1deb3 5d 2c 6c 65 6e 29 3d 3d 30 20 29 20 62 72 65 61  ],len)==0 ) brea
1deb4 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
1deb5 20 20 20 20 20 69 66 28 20 69 3e 3d 6e 43 68 61       if( i>=nCha
1deb6 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  r ) break;.     
1deb7 20 20 20 6e 49 6e 20 2d 3d 20 6c 65 6e 3b 0a 20     nIn -= len;. 
1deb8 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1deb9 20 69 66 28 20 7a 43 68 61 72 53 65 74 20 29 7b   if( zCharSet ){
1deba 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
1debb 72 65 65 28 61 7a 43 68 61 72 29 3b 0a 20 20 20  ree(azChar);.   
1debc 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
1debd 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
1debe 74 65 78 74 2c 20 28 63 68 61 72 2a 29 7a 49 6e  text, (char*)zIn
1debf 2c 20 6e 49 6e 2c 20 53 51 4c 49 54 45 5f 54 52  , nIn, SQLITE_TR
1dec0 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 0a 23 69 66  ANSIENT);.}..#if
1dec1 64 65 66 20 53 51 4c 49 54 45 5f 53 4f 55 4e 44  def SQLITE_SOUND
1dec2 45 58 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65  EX./*.** Compute
1dec3 20 74 68 65 20 73 6f 75 6e 64 65 78 20 65 6e 63   the soundex enc
1dec4 6f 64 69 6e 67 20 6f 66 20 61 20 77 6f 72 64 2e  oding of a word.
1dec5 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1dec6 73 6f 75 6e 64 65 78 46 75 6e 63 28 0a 20 20 73  soundexFunc(.  s
1dec7 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1dec8 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
1dec9 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
1deca 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
1decb 20 63 68 61 72 20 7a 52 65 73 75 6c 74 5b 38 5d   char zResult[8]
1decc 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 49  ;.  const u8 *zI
1decd 6e 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20  n;.  int i, j;. 
1dece 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
1decf 73 69 67 6e 65 64 20 63 68 61 72 20 69 43 6f 64  signed char iCod
1ded0 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 30 2c 20 30  e[] = {.    0, 0
1ded1 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
1ded2 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
1ded3 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20  0, 0, 0, 0,.    
1ded4 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
1ded5 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
1ded6 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a   0, 0, 0, 0, 0,.
1ded7 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20      0, 0, 0, 0, 
1ded8 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
1ded9 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
1deda 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c   0,.    0, 0, 0,
1dedb 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
1dedc 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
1dedd 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30  , 0, 0,.    0, 0
1dede 2c 20 31 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c  , 1, 2, 3, 0, 1,
1dedf 20 32 2c 20 30 2c 20 30 2c 20 32 2c 20 32 2c 20   2, 0, 0, 2, 2, 
1dee0 34 2c 20 35 2c 20 35 2c 20 30 2c 0a 20 20 20 20  4, 5, 5, 0,.    
1dee1 31 2c 20 32 2c 20 36 2c 20 32 2c 20 33 2c 20 30  1, 2, 6, 2, 3, 0
1dee2 2c 20 31 2c 20 30 2c 20 32 2c 20 30 2c 20 32 2c  , 1, 0, 2, 0, 2,
1dee3 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a   0, 0, 0, 0, 0,.
1dee4 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20      0, 0, 1, 2, 
1dee5 33 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20 30  3, 0, 1, 2, 0, 0
1dee6 2c 20 32 2c 20 32 2c 20 34 2c 20 35 2c 20 35 2c  , 2, 2, 4, 5, 5,
1dee7 20 30 2c 0a 20 20 20 20 31 2c 20 32 2c 20 36 2c   0,.    1, 2, 6,
1dee8 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20 30 2c 20   2, 3, 0, 1, 0, 
1dee9 32 2c 20 30 2c 20 32 2c 20 30 2c 20 30 2c 20 30  2, 0, 2, 0, 0, 0
1deea 2c 20 30 2c 20 30 2c 0a 20 20 7d 3b 0a 20 20 61  , 0, 0,.  };.  a
1deeb 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29  ssert( argc==1 )
1deec 3b 0a 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 73  ;.  zIn = (u8*)s
1deed 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
1deee 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66  t(argv[0]);.  if
1deef 28 20 7a 49 6e 3d 3d 30 20 29 20 7a 49 6e 20 3d  ( zIn==0 ) zIn =
1def0 20 28 75 38 2a 29 22 22 3b 0a 20 20 66 6f 72 28   (u8*)"";.  for(
1def1 69 3d 30 3b 20 7a 49 6e 5b 69 5d 20 26 26 20 21  i=0; zIn[i] && !
1def2 69 73 61 6c 70 68 61 28 7a 49 6e 5b 69 5d 29 3b  isalpha(zIn[i]);
1def3 20 69 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 7a 49   i++){}.  if( zI
1def4 6e 5b 69 5d 20 29 7b 0a 20 20 20 20 75 38 20 70  n[i] ){.    u8 p
1def5 72 65 76 63 6f 64 65 20 3d 20 69 43 6f 64 65 5b  revcode = iCode[
1def6 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a 20 20  zIn[i]&0x7f];.  
1def7 20 20 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20 74    zResult[0] = t
1def8 6f 75 70 70 65 72 28 7a 49 6e 5b 69 5d 29 3b 0a  oupper(zIn[i]);.
1def9 20 20 20 20 66 6f 72 28 6a 3d 31 3b 20 6a 3c 34      for(j=1; j<4
1defa 20 26 26 20 7a 49 6e 5b 69 5d 3b 20 69 2b 2b 29   && zIn[i]; i++)
1defb 7b 0a 20 20 20 20 20 20 69 6e 74 20 63 6f 64 65  {.      int code
1defc 20 3d 20 69 43 6f 64 65 5b 7a 49 6e 5b 69 5d 26   = iCode[zIn[i]&
1defd 30 78 37 66 5d 3b 0a 20 20 20 20 20 20 69 66 28  0x7f];.      if(
1defe 20 63 6f 64 65 3e 30 20 29 7b 0a 20 20 20 20 20   code>0 ){.     
1deff 20 20 20 69 66 28 20 63 6f 64 65 21 3d 70 72 65     if( code!=pre
1df00 76 63 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 20  vcode ){.       
1df01 20 20 20 70 72 65 76 63 6f 64 65 20 3d 20 63 6f     prevcode = co
1df02 64 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 52  de;.          zR
1df03 65 73 75 6c 74 5b 6a 2b 2b 5d 20 3d 20 63 6f 64  esult[j++] = cod
1df04 65 20 2b 20 27 30 27 3b 0a 20 20 20 20 20 20 20  e + '0';.       
1df05 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a   }.      }else{.
1df06 20 20 20 20 20 20 20 20 70 72 65 76 63 6f 64 65          prevcode
1df07 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
1df08 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 6a    }.    while( j
1df09 3c 34 20 29 7b 0a 20 20 20 20 20 20 7a 52 65 73  <4 ){.      zRes
1df0a 75 6c 74 5b 6a 2b 2b 5d 20 3d 20 27 30 27 3b 0a  ult[j++] = '0';.
1df0b 20 20 20 20 7d 0a 20 20 20 20 7a 52 65 73 75 6c      }.    zResul
1df0c 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71  t[j] = 0;.    sq
1df0d 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
1df0e 74 28 63 6f 6e 74 65 78 74 2c 20 7a 52 65 73 75  t(context, zResu
1df0f 6c 74 2c 20 34 2c 20 53 51 4c 49 54 45 5f 54 52  lt, 4, SQLITE_TR
1df10 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 65 6c 73  ANSIENT);.  }els
1df11 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  e{.    sqlite3_r
1df12 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
1df13 78 74 2c 20 22 3f 30 30 30 22 2c 20 34 2c 20 53  xt, "?000", 4, S
1df14 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
1df15 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66   }.}.#endif..#if
1df16 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1df17 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a  _LOAD_EXTENSION.
1df18 2f 2a 0a 2a 2a 20 41 20 66 75 6e 63 74 69 6f 6e  /*.** A function
1df19 20 74 68 61 74 20 6c 6f 61 64 73 20 61 20 73 68   that loads a sh
1df1a 61 72 65 64 2d 6c 69 62 72 61 72 79 20 65 78 74  ared-library ext
1df1b 65 6e 73 69 6f 6e 20 74 68 65 6e 20 72 65 74 75  ension then retu
1df1c 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61  rns NULL..*/.sta
1df1d 74 69 63 20 76 6f 69 64 20 6c 6f 61 64 45 78 74  tic void loadExt
1df1e 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
1df1f 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61   *context, int a
1df20 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
1df21 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 63 6f  ue **argv){.  co
1df22 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 20  nst char *zFile 
1df23 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
1df24 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1df25 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 63  xt(argv[0]);.  c
1df26 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 72 6f 63  onst char *zProc
1df27 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
1df28 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  = sqlite3_contex
1df29 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74  t_db_handle(cont
1df2a 65 78 74 29 3b 0a 20 20 63 68 61 72 20 2a 7a 45  ext);.  char *zE
1df2b 72 72 4d 73 67 20 3d 20 30 3b 0a 0a 20 20 69 66  rrMsg = 0;..  if
1df2c 28 20 61 72 67 63 3d 3d 32 20 29 7b 0a 20 20 20  ( argc==2 ){.   
1df2d 20 7a 50 72 6f 63 20 3d 20 28 63 6f 6e 73 74 20   zProc = (const 
1df2e 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76  char *)sqlite3_v
1df2f 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31  alue_text(argv[1
1df30 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ]);.  }else{.   
1df31 20 7a 50 72 6f 63 20 3d 20 30 3b 0a 20 20 7d 0a   zProc = 0;.  }.
1df32 20 20 69 66 28 20 7a 46 69 6c 65 20 26 26 20 73    if( zFile && s
1df33 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65  qlite3_load_exte
1df34 6e 73 69 6f 6e 28 64 62 2c 20 7a 46 69 6c 65 2c  nsion(db, zFile,
1df35 20 7a 50 72 6f 63 2c 20 26 7a 45 72 72 4d 73 67   zProc, &zErrMsg
1df36 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
1df37 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f  _result_error(co
1df38 6e 74 65 78 74 2c 20 7a 45 72 72 4d 73 67 2c 20  ntext, zErrMsg, 
1df39 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  -1);.    sqlite3
1df3a 5f 66 72 65 65 28 7a 45 72 72 4d 73 67 29 3b 0a  _free(zErrMsg);.
1df3b 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f    }.}.#endif.../
1df3c 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
1df3d 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
1df3e 67 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64  g structure hold
1df3f 73 20 74 68 65 20 63 6f 6e 74 65 78 74 20 6f 66  s the context of
1df40 20 61 0a 2a 2a 20 73 75 6d 28 29 20 6f 72 20 61   a.** sum() or a
1df41 76 67 28 29 20 61 67 67 72 65 67 61 74 65 20 63  vg() aggregate c
1df42 6f 6d 70 75 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 74  omputation..*/.t
1df43 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 75  ypedef struct Su
1df44 6d 43 74 78 20 53 75 6d 43 74 78 3b 0a 73 74 72  mCtx SumCtx;.str
1df45 75 63 74 20 53 75 6d 43 74 78 20 7b 0a 20 20 64  uct SumCtx {.  d
1df46 6f 75 62 6c 65 20 72 53 75 6d 3b 20 20 20 20 20  ouble rSum;     
1df47 20 2f 2a 20 46 6c 6f 61 74 69 6e 67 20 70 6f 69   /* Floating poi
1df48 6e 74 20 73 75 6d 20 2a 2f 0a 20 20 69 36 34 20  nt sum */.  i64 
1df49 69 53 75 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a  iSum;         /*
1df4a 20 49 6e 74 65 67 65 72 20 73 75 6d 20 2a 2f 20   Integer sum */ 
1df4b 20 20 0a 20 20 69 36 34 20 63 6e 74 3b 20 20 20    .  i64 cnt;   
1df4c 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1df4d 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 73 75 6d   of elements sum
1df4e 6d 65 64 20 2a 2f 0a 20 20 75 38 20 6f 76 65 72  med */.  u8 over
1df4f 66 6c 6f 77 3b 20 20 20 20 20 20 2f 2a 20 54 72  flow;      /* Tr
1df50 75 65 20 69 66 20 69 6e 74 65 67 65 72 20 6f 76  ue if integer ov
1df51 65 72 66 6c 6f 77 20 73 65 65 6e 20 2a 2f 0a 20  erflow seen */. 
1df52 20 75 38 20 61 70 70 72 6f 78 3b 20 20 20 20 20   u8 approx;     
1df53 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f     /* True if no
1df54 6e 2d 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  n-integer value 
1df55 77 61 73 20 69 6e 70 75 74 20 74 6f 20 74 68 65  was input to the
1df56 20 73 75 6d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a   sum */.};../*.*
1df57 2a 20 52 6f 75 74 69 6e 65 73 20 75 73 65 64 20  * Routines used 
1df58 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 73  to compute the s
1df59 75 6d 2c 20 61 76 65 72 61 67 65 2c 20 61 6e 64  um, average, and
1df5a 20 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68   total..**.** Th
1df5b 65 20 53 55 4d 28 29 20 66 75 6e 63 74 69 6f 6e  e SUM() function
1df5c 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20 28 62 72   follows the (br
1df5d 6f 6b 65 6e 29 20 53 51 4c 20 73 74 61 6e 64 61  oken) SQL standa
1df5e 72 64 20 77 68 69 63 68 20 6d 65 61 6e 73 0a 2a  rd which means.*
1df5f 2a 20 74 68 61 74 20 69 74 20 72 65 74 75 72 6e  * that it return
1df60 73 20 4e 55 4c 4c 20 69 66 20 69 74 20 73 75 6d  s NULL if it sum
1df61 73 20 6f 76 65 72 20 6e 6f 20 69 6e 70 75 74 73  s over no inputs
1df62 2e 20 20 54 4f 54 41 4c 20 72 65 74 75 72 6e 73  .  TOTAL returns
1df63 0a 2a 2a 20 30 2e 30 20 69 6e 20 74 68 61 74 20  .** 0.0 in that 
1df64 63 61 73 65 2e 20 20 49 6e 20 61 64 64 69 74 69  case.  In additi
1df65 6f 6e 2c 20 54 4f 54 41 4c 20 61 6c 77 61 79 73  on, TOTAL always
1df66 20 72 65 74 75 72 6e 73 20 61 20 66 6c 6f 61 74   returns a float
1df67 20 77 68 65 72 65 0a 2a 2a 20 53 55 4d 20 6d 69   where.** SUM mi
1df68 67 68 74 20 72 65 74 75 72 6e 20 61 6e 20 69 6e  ght return an in
1df69 74 65 67 65 72 20 69 66 20 69 74 20 6e 65 76 65  teger if it neve
1df6a 72 20 65 6e 63 6f 75 6e 74 65 72 73 20 61 20 66  r encounters a f
1df6b 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a  loating point.**
1df6c 20 76 61 6c 75 65 2e 20 20 54 4f 54 41 4c 20 6e   value.  TOTAL n
1df6d 65 76 65 72 20 66 61 69 6c 73 2c 20 62 75 74 20  ever fails, but 
1df6e 53 55 4d 20 6d 69 67 68 74 20 74 68 72 6f 75 67  SUM might throug
1df6f 68 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 69  h an exception i
1df70 66 0a 2a 2a 20 69 74 20 6f 76 65 72 66 6c 6f 77  f.** it overflow
1df71 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f  s an integer..*/
1df72 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 75 6d  .static void sum
1df73 53 74 65 70 28 73 71 6c 69 74 65 33 5f 63 6f 6e  Step(sqlite3_con
1df74 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69  text *context, i
1df75 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
1df76 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a  _value **argv){.
1df77 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20 20 69    SumCtx *p;.  i
1df78 6e 74 20 74 79 70 65 3b 0a 20 20 61 73 73 65 72  nt type;.  asser
1df79 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20  t( argc==1 );.  
1df7a 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72  p = sqlite3_aggr
1df7b 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f  egate_context(co
1df7c 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70  ntext, sizeof(*p
1df7d 29 29 3b 0a 20 20 74 79 70 65 20 3d 20 73 71 6c  ));.  type = sql
1df7e 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72  ite3_value_numer
1df7f 69 63 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29  ic_type(argv[0])
1df80 3b 0a 20 20 69 66 28 20 70 20 26 26 20 74 79 70  ;.  if( p && typ
1df81 65 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29  e!=SQLITE_NULL )
1df82 7b 0a 20 20 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a  {.    p->cnt++;.
1df83 20 20 20 20 69 66 28 20 74 79 70 65 3d 3d 53 51      if( type==SQ
1df84 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 29 7b 0a  LITE_INTEGER ){.
1df85 20 20 20 20 20 20 69 36 34 20 76 20 3d 20 73 71        i64 v = sq
1df86 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36  lite3_value_int6
1df87 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20  4(argv[0]);.    
1df88 20 20 70 2d 3e 72 53 75 6d 20 2b 3d 20 76 3b 0a    p->rSum += v;.
1df89 20 20 20 20 20 20 69 66 28 20 28 70 2d 3e 61 70        if( (p->ap
1df8a 70 72 6f 78 7c 70 2d 3e 6f 76 65 72 66 6c 6f 77  prox|p->overflow
1df8b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
1df8c 69 36 34 20 69 4e 65 77 53 75 6d 20 3d 20 70 2d  i64 iNewSum = p-
1df8d 3e 69 53 75 6d 20 2b 20 76 3b 0a 20 20 20 20 20  >iSum + v;.     
1df8e 20 20 20 69 6e 74 20 73 31 20 3d 20 70 2d 3e 69     int s1 = p->i
1df8f 53 75 6d 20 3e 3e 20 28 73 69 7a 65 6f 66 28 69  Sum >> (sizeof(i
1df90 36 34 29 2a 38 2d 31 29 3b 0a 20 20 20 20 20 20  64)*8-1);.      
1df91 20 20 69 6e 74 20 73 32 20 3d 20 76 20 20 20 20    int s2 = v    
1df92 20 20 20 3e 3e 20 28 73 69 7a 65 6f 66 28 69 36     >> (sizeof(i6
1df93 34 29 2a 38 2d 31 29 3b 0a 20 20 20 20 20 20 20  4)*8-1);.       
1df94 20 69 6e 74 20 73 33 20 3d 20 69 4e 65 77 53 75   int s3 = iNewSu
1df95 6d 20 3e 3e 20 28 73 69 7a 65 6f 66 28 69 36 34  m >> (sizeof(i64
1df96 29 2a 38 2d 31 29 3b 0a 20 20 20 20 20 20 20 20  )*8-1);.        
1df97 70 2d 3e 6f 76 65 72 66 6c 6f 77 20 3d 20 28 73  p->overflow = (s
1df98 31 26 73 32 26 7e 73 33 29 20 7c 20 28 7e 73 31  1&s2&~s3) | (~s1
1df99 26 7e 73 32 26 73 33 29 3b 0a 20 20 20 20 20 20  &~s2&s3);.      
1df9a 20 20 70 2d 3e 69 53 75 6d 20 3d 20 69 4e 65 77    p->iSum = iNew
1df9b 53 75 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  Sum;.      }.   
1df9c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 2d   }else{.      p-
1df9d 3e 72 53 75 6d 20 2b 3d 20 73 71 6c 69 74 65 33  >rSum += sqlite3
1df9e 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72  _value_double(ar
1df9f 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 70 2d  gv[0]);.      p-
1dfa0 3e 61 70 70 72 6f 78 20 3d 20 31 3b 0a 20 20 20  >approx = 1;.   
1dfa1 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20   }.  }.}.static 
1dfa2 76 6f 69 64 20 73 75 6d 46 69 6e 61 6c 69 7a 65  void sumFinalize
1dfa3 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
1dfa4 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75   *context){.  Su
1dfa5 6d 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73  mCtx *p;.  p = s
1dfa6 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
1dfa7 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74  _context(context
1dfa8 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 20 26 26  , 0);.  if( p &&
1dfa9 20 70 2d 3e 63 6e 74 3e 30 20 29 7b 0a 20 20 20   p->cnt>0 ){.   
1dfaa 20 69 66 28 20 70 2d 3e 6f 76 65 72 66 6c 6f 77   if( p->overflow
1dfab 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1dfac 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63  3_result_error(c
1dfad 6f 6e 74 65 78 74 2c 22 69 6e 74 65 67 65 72 20  ontext,"integer 
1dfae 6f 76 65 72 66 6c 6f 77 22 2c 2d 31 29 3b 0a 20  overflow",-1);. 
1dfaf 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e     }else if( p->
1dfb0 61 70 70 72 6f 78 20 29 7b 0a 20 20 20 20 20 20  approx ){.      
1dfb1 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64  sqlite3_result_d
1dfb2 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70  ouble(context, p
1dfb3 2d 3e 72 53 75 6d 29 3b 0a 20 20 20 20 7d 65 6c  ->rSum);.    }el
1dfb4 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
1dfb5 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63  3_result_int64(c
1dfb6 6f 6e 74 65 78 74 2c 20 70 2d 3e 69 53 75 6d 29  ontext, p->iSum)
1dfb7 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74  ;.    }.  }.}.st
1dfb8 61 74 69 63 20 76 6f 69 64 20 61 76 67 46 69 6e  atic void avgFin
1dfb9 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f  alize(sqlite3_co
1dfba 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b  ntext *context){
1dfbb 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20 20  .  SumCtx *p;.  
1dfbc 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72  p = sqlite3_aggr
1dfbd 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f  egate_context(co
1dfbe 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 69 66 28  ntext, 0);.  if(
1dfbf 20 70 20 26 26 20 70 2d 3e 63 6e 74 3e 30 20 29   p && p->cnt>0 )
1dfc0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
1dfc1 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74  sult_double(cont
1dfc2 65 78 74 2c 20 70 2d 3e 72 53 75 6d 2f 28 64 6f  ext, p->rSum/(do
1dfc3 75 62 6c 65 29 70 2d 3e 63 6e 74 29 3b 0a 20 20  uble)p->cnt);.  
1dfc4 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  }.}.static void 
1dfc5 74 6f 74 61 6c 46 69 6e 61 6c 69 7a 65 28 73 71  totalFinalize(sq
1dfc6 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
1dfc7 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75 6d 43 74  ontext){.  SumCt
1dfc8 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69  x *p;.  p = sqli
1dfc9 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
1dfca 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30  ntext(context, 0
1dfcb 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  );.  sqlite3_res
1dfcc 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65  ult_double(conte
1dfcd 78 74 2c 20 70 20 3f 20 70 2d 3e 72 53 75 6d 20  xt, p ? p->rSum 
1dfce 3a 20 30 2e 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  : 0.0);.}../*.**
1dfcf 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   The following s
1dfd0 74 72 75 63 74 75 72 65 20 6b 65 65 70 73 20 74  tructure keeps t
1dfd1 72 61 63 6b 20 6f 66 20 73 74 61 74 65 20 69 6e  rack of state in
1dfd2 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68  formation for th
1dfd3 65 0a 2a 2a 20 63 6f 75 6e 74 28 29 20 61 67 67  e.** count() agg
1dfd4 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e  regate function.
1dfd5 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
1dfd6 63 74 20 43 6f 75 6e 74 43 74 78 20 43 6f 75 6e  ct CountCtx Coun
1dfd7 74 43 74 78 3b 0a 73 74 72 75 63 74 20 43 6f 75  tCtx;.struct Cou
1dfd8 6e 74 43 74 78 20 7b 0a 20 20 69 36 34 20 6e 3b  ntCtx {.  i64 n;
1dfd9 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69  .};../*.** Routi
1dfda 6e 65 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  nes to implement
1dfdb 20 74 68 65 20 63 6f 75 6e 74 28 29 20 61 67 67   the count() agg
1dfdc 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e  regate function.
1dfdd 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1dfde 63 6f 75 6e 74 53 74 65 70 28 73 71 6c 69 74 65  countStep(sqlite
1dfdf 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
1dfe0 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
1dfe1 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
1dfe2 67 76 29 7b 0a 20 20 43 6f 75 6e 74 43 74 78 20  gv){.  CountCtx 
1dfe3 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65  *p;.  p = sqlite
1dfe4 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74  3_aggregate_cont
1dfe5 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 69 7a  ext(context, siz
1dfe6 65 6f 66 28 2a 70 29 29 3b 0a 20 20 69 66 28 20  eof(*p));.  if( 
1dfe7 28 61 72 67 63 3d 3d 30 20 7c 7c 20 53 51 4c 49  (argc==0 || SQLI
1dfe8 54 45 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74 65 33  TE_NULL!=sqlite3
1dfe9 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
1dfea 5b 30 5d 29 29 20 26 26 20 70 20 29 7b 0a 20 20  [0])) && p ){.  
1dfeb 20 20 70 2d 3e 6e 2b 2b 3b 0a 20 20 7d 0a 7d 20    p->n++;.  }.} 
1dfec 20 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63    .static void c
1dfed 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 28 73 71 6c  ountFinalize(sql
1dfee 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
1dfef 6e 74 65 78 74 29 7b 0a 20 20 43 6f 75 6e 74 43  ntext){.  CountC
1dff0 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c  tx *p;.  p = sql
1dff1 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
1dff2 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  ontext(context, 
1dff3 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  0);.  sqlite3_re
1dff4 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65  sult_int64(conte
1dff5 78 74 2c 20 70 20 3f 20 70 2d 3e 6e 20 3a 20 30  xt, p ? p->n : 0
1dff6 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74  );.}../*.** Rout
1dff7 69 6e 65 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  ines to implemen
1dff8 74 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78 28  t min() and max(
1dff9 29 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  ) aggregate func
1dffa 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  tions..*/.static
1dffb 20 76 6f 69 64 20 6d 69 6e 6d 61 78 53 74 65 70   void minmaxStep
1dffc 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
1dffd 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61   *context, int a
1dffe 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
1dfff 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 4d 65  ue **argv){.  Me
1e000 6d 20 2a 70 41 72 67 20 20 3d 20 28 4d 65 6d 20  m *pArg  = (Mem 
1e001 2a 29 61 72 67 76 5b 30 5d 3b 0a 20 20 4d 65 6d  *)argv[0];.  Mem
1e002 20 2a 70 42 65 73 74 3b 0a 0a 20 20 69 66 28 20   *pBest;..  if( 
1e003 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
1e004 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c  pe(argv[0])==SQL
1e005 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72  ITE_NULL ) retur
1e006 6e 3b 0a 20 20 70 42 65 73 74 20 3d 20 28 4d 65  n;.  pBest = (Me
1e007 6d 20 2a 29 73 71 6c 69 74 65 33 5f 61 67 67 72  m *)sqlite3_aggr
1e008 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f  egate_context(co
1e009 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70  ntext, sizeof(*p
1e00a 42 65 73 74 29 29 3b 0a 20 20 69 66 28 20 21 70  Best));.  if( !p
1e00b 42 65 73 74 20 29 20 72 65 74 75 72 6e 3b 0a 0a  Best ) return;..
1e00c 20 20 69 66 28 20 70 42 65 73 74 2d 3e 66 6c 61    if( pBest->fla
1e00d 67 73 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 61  gs ){.    int ma
1e00e 78 3b 0a 20 20 20 20 69 6e 74 20 63 6d 70 3b 0a  x;.    int cmp;.
1e00f 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
1e010 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 46  ll = sqlite3GetF
1e011 75 6e 63 43 6f 6c 6c 53 65 71 28 63 6f 6e 74 65  uncCollSeq(conte
1e012 78 74 29 3b 0a 20 20 20 20 2f 2a 20 54 68 69 73  xt);.    /* This
1e013 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 69   step function i
1e014 73 20 75 73 65 64 20 66 6f 72 20 62 6f 74 68 20  s used for both 
1e015 74 68 65 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61  the min() and ma
1e016 78 28 29 20 61 67 67 72 65 67 61 74 65 73 2c 0a  x() aggregates,.
1e017 20 20 20 20 2a 2a 20 74 68 65 20 6f 6e 6c 79 20      ** the only 
1e018 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65  difference betwe
1e019 65 6e 20 74 68 65 20 74 77 6f 20 62 65 69 6e 67  en the two being
1e01a 20 74 68 61 74 20 74 68 65 20 73 65 6e 73 65 20   that the sense 
1e01b 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f  of the.    ** co
1e01c 6d 70 61 72 69 73 6f 6e 20 69 73 20 69 6e 76 65  mparison is inve
1e01d 72 74 65 64 2e 20 46 6f 72 20 74 68 65 20 6d 61  rted. For the ma
1e01e 78 28 29 20 61 67 67 72 65 67 61 74 65 2c 20 74  x() aggregate, t
1e01f 68 65 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  he.    ** sqlite
1e020 33 5f 75 73 65 72 5f 64 61 74 61 28 29 20 66 75  3_user_data() fu
1e021 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 28  nction returns (
1e022 76 6f 69 64 20 2a 29 2d 31 2e 20 46 6f 72 20 6d  void *)-1. For m
1e023 69 6e 28 29 20 69 74 0a 20 20 20 20 2a 2a 20 72  in() it.    ** r
1e024 65 74 75 72 6e 73 20 28 76 6f 69 64 20 2a 29 64  eturns (void *)d
1e025 62 2c 20 77 68 65 72 65 20 64 62 20 69 73 20 74  b, where db is t
1e026 68 65 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61  he sqlite3* data
1e027 62 61 73 65 20 70 6f 69 6e 74 65 72 2e 0a 20 20  base pointer..  
1e028 20 20 2a 2a 20 54 68 65 72 65 66 6f 72 65 20 74    ** Therefore t
1e029 68 65 20 6e 65 78 74 20 73 74 61 74 65 6d 65 6e  he next statemen
1e02a 74 20 73 65 74 73 20 76 61 72 69 61 62 6c 65 20  t sets variable 
1e02b 27 6d 61 78 27 20 74 6f 20 31 20 66 6f 72 20 74  'max' to 1 for t
1e02c 68 65 20 6d 61 78 28 29 0a 20 20 20 20 2a 2a 20  he max().    ** 
1e02d 61 67 67 72 65 67 61 74 65 2c 20 6f 72 20 30 20  aggregate, or 0 
1e02e 66 6f 72 20 6d 69 6e 28 29 2e 0a 20 20 20 20 2a  for min()..    *
1e02f 2f 0a 20 20 20 20 6d 61 78 20 3d 20 73 71 6c 69  /.    max = sqli
1e030 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f  te3_user_data(co
1e031 6e 74 65 78 74 29 21 3d 30 3b 0a 20 20 20 20 63  ntext)!=0;.    c
1e032 6d 70 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43  mp = sqlite3MemC
1e033 6f 6d 70 61 72 65 28 70 42 65 73 74 2c 20 70 41  ompare(pBest, pA
1e034 72 67 2c 20 70 43 6f 6c 6c 29 3b 0a 20 20 20 20  rg, pColl);.    
1e035 69 66 28 20 28 6d 61 78 20 26 26 20 63 6d 70 3c  if( (max && cmp<
1e036 30 29 20 7c 7c 20 28 21 6d 61 78 20 26 26 20 63  0) || (!max && c
1e037 6d 70 3e 30 29 20 29 7b 0a 20 20 20 20 20 20 73  mp>0) ){.      s
1e038 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70  qlite3VdbeMemCop
1e039 79 28 70 42 65 73 74 2c 20 70 41 72 67 29 3b 0a  y(pBest, pArg);.
1e03a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
1e03b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1e03c 6d 43 6f 70 79 28 70 42 65 73 74 2c 20 70 41 72  mCopy(pBest, pAr
1e03d 67 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63  g);.  }.}.static
1e03e 20 76 6f 69 64 20 6d 69 6e 4d 61 78 46 69 6e 61   void minMaxFina
1e03f 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e  lize(sqlite3_con
1e040 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a  text *context){.
1e041 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
1e042 2a 70 52 65 73 3b 0a 20 20 70 52 65 73 20 3d 20  *pRes;.  pRes = 
1e043 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  (sqlite3_value *
1e044 29 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61  )sqlite3_aggrega
1e045 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65  te_context(conte
1e046 78 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 52  xt, 0);.  if( pR
1e047 65 73 20 29 7b 0a 20 20 20 20 69 66 28 20 70 52  es ){.    if( pR
1e048 65 73 2d 3e 66 6c 61 67 73 20 29 7b 0a 20 20 20  es->flags ){.   
1e049 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
1e04a 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c  t_value(context,
1e04b 20 70 52 65 73 29 3b 0a 20 20 20 20 7d 0a 20 20   pRes);.    }.  
1e04c 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1e04d 52 65 6c 65 61 73 65 28 70 52 65 73 29 3b 0a 20  Release(pRes);. 
1e04e 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 67 72 6f 75   }.}../*.** grou
1e04f 70 5f 63 6f 6e 63 61 74 28 45 58 50 52 2c 20 3f  p_concat(EXPR, ?
1e050 53 45 50 41 52 41 54 4f 52 3f 29 0a 2a 2f 0a 73  SEPARATOR?).*/.s
1e051 74 61 74 69 63 20 76 6f 69 64 20 67 72 6f 75 70  tatic void group
1e052 43 6f 6e 63 61 74 53 74 65 70 28 0a 20 20 73 71  ConcatStep(.  sq
1e053 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
1e054 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
1e055 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
1e056 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
1e057 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 61 6c  const char *zVal
1e058 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 2a 70 41  ;.  StrAccum *pA
1e059 63 63 75 6d 3b 0a 20 20 63 6f 6e 73 74 20 63 68  ccum;.  const ch
1e05a 61 72 20 2a 7a 53 65 70 3b 0a 20 20 69 6e 74 20  ar *zSep;.  int 
1e05b 6e 56 61 6c 2c 20 6e 53 65 70 3b 0a 20 20 69 66  nVal, nSep;.  if
1e05c 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
1e05d 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53  type(argv[0])==S
1e05e 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74  QLITE_NULL ) ret
1e05f 75 72 6e 3b 0a 20 20 70 41 63 63 75 6d 20 3d 20  urn;.  pAccum = 
1e060 28 53 74 72 41 63 63 75 6d 2a 29 73 71 6c 69 74  (StrAccum*)sqlit
1e061 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
1e062 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 69  text(context, si
1e063 7a 65 6f 66 28 2a 70 41 63 63 75 6d 29 29 3b 0a  zeof(*pAccum));.
1e064 0a 20 20 69 66 28 20 70 41 63 63 75 6d 20 29 7b  .  if( pAccum ){
1e065 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62  .    sqlite3 *db
1e066 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
1e067 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
1e068 74 65 78 74 29 3b 0a 20 20 20 20 70 41 63 63 75  text);.    pAccu
1e069 6d 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 31  m->useMalloc = 1
1e06a 3b 0a 20 20 20 20 70 41 63 63 75 6d 2d 3e 6d 78  ;.    pAccum->mx
1e06b 41 6c 6c 6f 63 20 3d 20 64 62 2d 3e 61 4c 69 6d  Alloc = db->aLim
1e06c 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
1e06d 4c 45 4e 47 54 48 5d 3b 0a 20 20 20 20 69 66 28  LENGTH];.    if(
1e06e 20 70 41 63 63 75 6d 2d 3e 6e 43 68 61 72 20 29   pAccum->nChar )
1e06f 7b 0a 20 20 20 20 20 20 69 66 28 20 61 72 67 63  {.      if( argc
1e070 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  ==2 ){.        z
1e071 53 65 70 20 3d 20 28 63 68 61 72 2a 29 73 71 6c  Sep = (char*)sql
1e072 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
1e073 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 20 20  argv[1]);.      
1e074 20 20 6e 53 65 70 20 3d 20 73 71 6c 69 74 65 33    nSep = sqlite3
1e075 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
1e076 76 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d 65 6c  v[1]);.      }el
1e077 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 53 65 70  se{.        zSep
1e078 20 3d 20 22 2c 22 3b 0a 20 20 20 20 20 20 20 20   = ",";.        
1e079 6e 53 65 70 20 3d 20 31 3b 0a 20 20 20 20 20 20  nSep = 1;.      
1e07a 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  }.      sqlite3S
1e07b 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41  trAccumAppend(pA
1e07c 63 63 75 6d 2c 20 7a 53 65 70 2c 20 6e 53 65 70  ccum, zSep, nSep
1e07d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 56 61  );.    }.    zVa
1e07e 6c 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74  l = (char*)sqlit
1e07f 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
1e080 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 56 61 6c  gv[0]);.    nVal
1e081 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
1e082 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b  _bytes(argv[0]);
1e083 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41  .    sqlite3StrA
1e084 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75  ccumAppend(pAccu
1e085 6d 2c 20 7a 56 61 6c 2c 20 6e 56 61 6c 29 3b 0a  m, zVal, nVal);.
1e086 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69    }.}.static voi
1e087 64 20 67 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e  d groupConcatFin
1e088 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f  alize(sqlite3_co
1e089 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b  ntext *context){
1e08a 0a 20 20 53 74 72 41 63 63 75 6d 20 2a 70 41 63  .  StrAccum *pAc
1e08b 63 75 6d 3b 0a 20 20 70 41 63 63 75 6d 20 3d 20  cum;.  pAccum = 
1e08c 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
1e08d 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78  e_context(contex
1e08e 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 41 63  t, 0);.  if( pAc
1e08f 63 75 6d 20 29 7b 0a 20 20 20 20 69 66 28 20 70  cum ){.    if( p
1e090 41 63 63 75 6d 2d 3e 74 6f 6f 42 69 67 20 29 7b  Accum->tooBig ){
1e091 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
1e092 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62  esult_error_toob
1e093 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  ig(context);.   
1e094 20 7d 65 6c 73 65 20 69 66 28 20 70 41 63 63 75   }else if( pAccu
1e095 6d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  m->mallocFailed 
1e096 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1e097 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f  _result_error_no
1e098 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  mem(context);.  
1e099 20 20 7d 65 6c 73 65 7b 20 20 20 20 0a 20 20 20    }else{    .   
1e09a 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
1e09b 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
1e09c 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46  sqlite3StrAccumF
1e09d 69 6e 69 73 68 28 70 41 63 63 75 6d 29 2c 20 2d  inish(pAccum), -
1e09e 31 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  1, .            
1e09f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
1e0a0 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 20  lite3_free);.   
1e0a1 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
1e0a2 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
1e0a3 67 69 73 74 65 72 65 64 20 61 6c 6c 20 6f 66 20  gistered all of 
1e0a4 74 68 65 20 61 62 6f 76 65 20 43 20 66 75 6e 63  the above C func
1e0a5 74 69 6f 6e 73 20 61 73 20 53 51 4c 0a 2a 2a 20  tions as SQL.** 
1e0a6 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 69 73  functions.  This
1e0a7 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 20 6f   should be the o
1e0a8 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74  nly routine in t
1e0a9 68 69 73 20 66 69 6c 65 20 77 69 74 68 0a 2a 2a  his file with.**
1e0aa 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67   external linkag
1e0ab 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1e0ac 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1e0ad 33 52 65 67 69 73 74 65 72 42 75 69 6c 74 69 6e  3RegisterBuiltin
1e0ae 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65  Functions(sqlite
1e0af 33 20 2a 64 62 29 7b 0a 20 20 73 74 61 74 69 63  3 *db){.  static
1e0b0 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 7b 0a   const struct {.
1e0b1 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65       char *zName
1e0b2 3b 0a 20 20 20 20 20 73 69 67 6e 65 64 20 63 68  ;.     signed ch
1e0b3 61 72 20 6e 41 72 67 3b 0a 20 20 20 20 20 75 38  ar nArg;.     u8
1e0b4 20 61 72 67 54 79 70 65 3b 20 20 20 20 20 20 20   argType;       
1e0b5 20 20 20 20 2f 2a 20 31 3a 20 30 2c 20 32 3a 20      /* 1: 0, 2: 
1e0b6 31 2c 20 33 3a 20 32 2c 2e 2e 2e 20 20 4e 3a 20  1, 3: 2,...  N: 
1e0b7 20 4e 2d 31 2e 20 2a 2f 0a 20 20 20 20 20 75 38   N-1. */.     u8
1e0b8 20 65 54 65 78 74 52 65 70 3b 20 20 20 20 20 20   eTextRep;      
1e0b9 20 20 20 20 2f 2a 20 31 3a 20 55 54 46 2d 31 36      /* 1: UTF-16
1e0ba 2e 20 20 30 3a 20 55 54 46 2d 38 20 2a 2f 0a 20  .  0: UTF-8 */. 
1e0bb 20 20 20 20 75 38 20 6e 65 65 64 43 6f 6c 6c 53      u8 needCollS
1e0bc 65 71 3b 0a 20 20 20 20 20 76 6f 69 64 20 28 2a  eq;.     void (*
1e0bd 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63  xFunc)(sqlite3_c
1e0be 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
1e0bf 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 3b 0a 20  te3_value **);. 
1e0c0 20 7d 20 61 46 75 6e 63 73 5b 5d 20 3d 20 7b 0a   } aFuncs[] = {.
1e0c1 20 20 20 20 7b 20 22 6d 69 6e 22 2c 20 20 20 20      { "min",    
1e0c2 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30             -1, 0
1e0c3 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20  , SQLITE_UTF8,  
1e0c4 20 20 31 2c 20 6d 69 6e 6d 61 78 46 75 6e 63 20    1, minmaxFunc 
1e0c5 7d 2c 0a 20 20 20 20 7b 20 22 6d 69 6e 22 2c 20  },.    { "min", 
1e0c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
1e0c7 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  , 0, SQLITE_UTF8
1e0c8 2c 20 20 20 20 31 2c 20 30 20 20 20 20 20 20 20  ,    1, 0       
1e0c9 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 6d 61 78     },.    { "max
1e0ca 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ",              
1e0cb 20 2d 31 2c 20 31 2c 20 53 51 4c 49 54 45 5f 55   -1, 1, SQLITE_U
1e0cc 54 46 38 2c 20 20 20 20 31 2c 20 6d 69 6e 6d 61  TF8,    1, minma
1e0cd 78 46 75 6e 63 20 7d 2c 0a 20 20 20 20 7b 20 22  xFunc },.    { "
1e0ce 6d 61 78 22 2c 20 20 20 20 20 20 20 20 20 20 20  max",           
1e0cf 20 20 20 20 20 30 2c 20 31 2c 20 53 51 4c 49 54       0, 1, SQLIT
1e0d0 45 5f 55 54 46 38 2c 20 20 20 20 31 2c 20 30 20  E_UTF8,    1, 0 
1e0d1 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20           },.    
1e0d2 7b 20 22 74 79 70 65 6f 66 22 2c 20 20 20 20 20  { "typeof",     
1e0d3 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 53 51          1, 0, SQ
1e0d4 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 30 2c  LITE_UTF8,    0,
1e0d5 20 74 79 70 65 6f 66 46 75 6e 63 20 7d 2c 0a 20   typeofFunc },. 
1e0d6 20 20 20 7b 20 22 6c 65 6e 67 74 68 22 2c 20 20     { "length",  
1e0d7 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c             1, 0,
1e0d8 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20   SQLITE_UTF8,   
1e0d9 20 30 2c 20 6c 65 6e 67 74 68 46 75 6e 63 20 7d   0, lengthFunc }
1e0da 2c 0a 20 20 20 20 7b 20 22 73 75 62 73 74 72 22  ,.    { "substr"
1e0db 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c  ,             2,
1e0dc 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   0, SQLITE_UTF8,
1e0dd 20 20 20 20 30 2c 20 73 75 62 73 74 72 46 75 6e      0, substrFun
1e0de 63 20 7d 2c 0a 20 20 20 20 7b 20 22 73 75 62 73  c },.    { "subs
1e0df 74 72 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  tr",            
1e0e0 20 33 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54   3, 0, SQLITE_UT
1e0e1 46 38 2c 20 20 20 20 30 2c 20 73 75 62 73 74 72  F8,    0, substr
1e0e2 46 75 6e 63 20 7d 2c 0a 20 20 20 20 7b 20 22 61  Func },.    { "a
1e0e3 62 73 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  bs",            
1e0e4 20 20 20 20 31 2c 20 30 2c 20 53 51 4c 49 54 45      1, 0, SQLITE
1e0e5 5f 55 54 46 38 2c 20 20 20 20 30 2c 20 61 62 73  _UTF8,    0, abs
1e0e6 46 75 6e 63 20 20 20 20 7d 2c 0a 20 20 20 20 7b  Func    },.    {
1e0e7 20 22 72 6f 75 6e 64 22 2c 20 20 20 20 20 20 20   "round",       
1e0e8 20 20 20 20 20 20 20 31 2c 20 30 2c 20 53 51 4c         1, 0, SQL
1e0e9 49 54 45 5f 55 54 46 38 2c 20 20 20 20 30 2c 20  ITE_UTF8,    0, 
1e0ea 72 6f 75 6e 64 46 75 6e 63 20 20 7d 2c 0a 20 20  roundFunc  },.  
1e0eb 20 20 7b 20 22 72 6f 75 6e 64 22 2c 20 20 20 20    { "round",    
1e0ec 20 20 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20            2, 0, 
1e0ed 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20  SQLITE_UTF8,    
1e0ee 30 2c 20 72 6f 75 6e 64 46 75 6e 63 20 20 7d 2c  0, roundFunc  },
1e0ef 0a 20 20 20 20 7b 20 22 75 70 70 65 72 22 2c 20  .    { "upper", 
1e0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
1e0f1 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  0, SQLITE_UTF8, 
1e0f2 20 20 20 30 2c 20 75 70 70 65 72 46 75 6e 63 20     0, upperFunc 
1e0f3 20 7d 2c 0a 20 20 20 20 7b 20 22 6c 6f 77 65 72   },.    { "lower
1e0f4 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ",              
1e0f5 31 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46  1, 0, SQLITE_UTF
1e0f6 38 2c 20 20 20 20 30 2c 20 6c 6f 77 65 72 46 75  8,    0, lowerFu
1e0f7 6e 63 20 20 7d 2c 0a 20 20 20 20 7b 20 22 63 6f  nc  },.    { "co
1e0f8 61 6c 65 73 63 65 22 2c 20 20 20 20 20 20 20 20  alesce",        
1e0f9 20 20 2d 31 2c 20 30 2c 20 53 51 4c 49 54 45 5f    -1, 0, SQLITE_
1e0fa 55 54 46 38 2c 20 20 20 20 30 2c 20 69 66 6e 75  UTF8,    0, ifnu
1e0fb 6c 6c 46 75 6e 63 20 7d 2c 0a 20 20 20 20 7b 20  llFunc },.    { 
1e0fc 22 63 6f 61 6c 65 73 63 65 22 2c 20 20 20 20 20  "coalesce",     
1e0fd 20 20 20 20 20 20 30 2c 20 30 2c 20 53 51 4c 49        0, 0, SQLI
1e0fe 54 45 5f 55 54 46 38 2c 20 20 20 20 30 2c 20 30  TE_UTF8,    0, 0
1e0ff 20 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20            },.   
1e100 20 7b 20 22 63 6f 61 6c 65 73 63 65 22 2c 20 20   { "coalesce",  
1e101 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 53           1, 0, S
1e102 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 30  QLITE_UTF8,    0
1e103 2c 20 30 20 20 20 20 20 20 20 20 20 20 7d 2c 0a  , 0          },.
1e104 20 20 20 20 7b 20 22 68 65 78 22 2c 20 20 20 20      { "hex",    
1e105 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30              1, 0
1e106 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20  , SQLITE_UTF8,  
1e107 20 20 30 2c 20 68 65 78 46 75 6e 63 20 20 20 20    0, hexFunc    
1e108 7d 2c 0a 20 20 20 20 7b 20 22 69 66 6e 75 6c 6c  },.    { "ifnull
1e109 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 32  ",             2
1e10a 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  , 0, SQLITE_UTF8
1e10b 2c 20 20 20 20 31 2c 20 69 66 6e 75 6c 6c 46 75  ,    1, ifnullFu
1e10c 6e 63 20 7d 2c 0a 20 20 20 20 7b 20 22 72 61 6e  nc },.    { "ran
1e10d 64 6f 6d 22 2c 20 20 20 20 20 20 20 20 20 20 20  dom",           
1e10e 20 2d 31 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55   -1, 0, SQLITE_U
1e10f 54 46 38 2c 20 20 20 20 30 2c 20 72 61 6e 64 6f  TF8,    0, rando
1e110 6d 46 75 6e 63 20 7d 2c 0a 20 20 20 20 7b 20 22  mFunc },.    { "
1e111 72 61 6e 64 6f 6d 62 6c 6f 62 22 2c 20 20 20 20  randomblob",    
1e112 20 20 20 20 20 31 2c 20 30 2c 20 53 51 4c 49 54       1, 0, SQLIT
1e113 45 5f 55 54 46 38 2c 20 20 20 20 30 2c 20 72 61  E_UTF8,    0, ra
1e114 6e 64 6f 6d 42 6c 6f 62 20 7d 2c 0a 20 20 20 20  ndomBlob },.    
1e115 7b 20 22 6e 75 6c 6c 69 66 22 2c 20 20 20 20 20  { "nullif",     
1e116 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20 53 51          2, 0, SQ
1e117 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 31 2c  LITE_UTF8,    1,
1e118 20 6e 75 6c 6c 69 66 46 75 6e 63 20 7d 2c 0a 20   nullifFunc },. 
1e119 20 20 20 7b 20 22 73 71 6c 69 74 65 5f 76 65 72     { "sqlite_ver
1e11a 73 69 6f 6e 22 2c 20 20 20 20 20 30 2c 20 30 2c  sion",     0, 0,
1e11b 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20   SQLITE_UTF8,   
1e11c 20 30 2c 20 76 65 72 73 69 6f 6e 46 75 6e 63 7d   0, versionFunc}
1e11d 2c 0a 20 20 20 20 7b 20 22 71 75 6f 74 65 22 2c  ,.    { "quote",
1e11e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c                1,
1e11f 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   0, SQLITE_UTF8,
1e120 20 20 20 20 30 2c 20 71 75 6f 74 65 46 75 6e 63      0, quoteFunc
1e121 20 20 7d 2c 0a 20 20 20 20 7b 20 22 6c 61 73 74    },.    { "last
1e122 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 22 2c 20  _insert_rowid", 
1e123 20 30 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54   0, 0, SQLITE_UT
1e124 46 38 2c 20 30 2c 20 6c 61 73 74 5f 69 6e 73 65  F8, 0, last_inse
1e125 72 74 5f 72 6f 77 69 64 20 7d 2c 0a 20 20 20 20  rt_rowid },.    
1e126 7b 20 22 63 68 61 6e 67 65 73 22 2c 20 20 20 20  { "changes",    
1e127 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20 53 51          0, 0, SQ
1e128 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 63 68  LITE_UTF8, 0, ch
1e129 61 6e 67 65 73 20 20 20 20 20 20 20 20 20 20 20  anges           
1e12a 7d 2c 0a 20 20 20 20 7b 20 22 74 6f 74 61 6c 5f  },.    { "total_
1e12b 63 68 61 6e 67 65 73 22 2c 20 20 20 20 20 20 30  changes",      0
1e12c 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  , 0, SQLITE_UTF8
1e12d 2c 20 30 2c 20 74 6f 74 61 6c 5f 63 68 61 6e 67  , 0, total_chang
1e12e 65 73 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20  es     },.    { 
1e12f 22 72 65 70 6c 61 63 65 22 2c 20 20 20 20 20 20  "replace",      
1e130 20 20 20 20 20 20 33 2c 20 30 2c 20 53 51 4c 49        3, 0, SQLI
1e131 54 45 5f 55 54 46 38 2c 20 20 20 20 30 2c 20 72  TE_UTF8,    0, r
1e132 65 70 6c 61 63 65 46 75 6e 63 20 20 20 20 20 20  eplaceFunc      
1e133 20 7d 2c 0a 20 20 20 20 7b 20 22 6c 74 72 69 6d   },.    { "ltrim
1e134 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ",              
1e135 31 2c 20 31 2c 20 53 51 4c 49 54 45 5f 55 54 46  1, 1, SQLITE_UTF
1e136 38 2c 20 20 20 20 30 2c 20 74 72 69 6d 46 75 6e  8,    0, trimFun
1e137 63 20 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 20  c          },.  
1e138 20 20 7b 20 22 6c 74 72 69 6d 22 2c 20 20 20 20    { "ltrim",    
1e139 20 20 20 20 20 20 20 20 20 20 32 2c 20 31 2c 20            2, 1, 
1e13a 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20  SQLITE_UTF8,    
1e13b 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20  0, trimFunc     
1e13c 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 72       },.    { "r
1e13d 74 72 69 6d 22 2c 20 20 20 20 20 20 20 20 20 20  trim",          
1e13e 20 20 20 20 31 2c 20 32 2c 20 53 51 4c 49 54 45      1, 2, SQLITE
1e13f 5f 55 54 46 38 2c 20 20 20 20 30 2c 20 74 72 69  _UTF8,    0, tri
1e140 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20 20 7d  mFunc          }
1e141 2c 0a 20 20 20 20 7b 20 22 72 74 72 69 6d 22 2c  ,.    { "rtrim",
1e142 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c                2,
1e143 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   2, SQLITE_UTF8,
1e144 20 20 20 20 30 2c 20 74 72 69 6d 46 75 6e 63 20      0, trimFunc 
1e145 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20           },.    
1e146 7b 20 22 74 72 69 6d 22 2c 20 20 20 20 20 20 20  { "trim",       
1e147 20 20 20 20 20 20 20 20 31 2c 20 33 2c 20 53 51          1, 3, SQ
1e148 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 30 2c  LITE_UTF8,    0,
1e149 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20   trimFunc       
1e14a 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 74 72 69     },.    { "tri
1e14b 6d 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  m",             
1e14c 20 20 32 2c 20 33 2c 20 53 51 4c 49 54 45 5f 55    2, 3, SQLITE_U
1e14d 54 46 38 2c 20 20 20 20 30 2c 20 74 72 69 6d 46  TF8,    0, trimF
1e14e 75 6e 63 20 20 20 20 20 20 20 20 20 20 7d 2c 0a  unc          },.
1e14f 20 20 20 20 7b 20 22 7a 65 72 6f 62 6c 6f 62 22      { "zeroblob"
1e150 2c 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30  ,           1, 0
1e151 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20  , SQLITE_UTF8,  
1e152 20 20 30 2c 20 7a 65 72 6f 62 6c 6f 62 46 75 6e    0, zeroblobFun
1e153 63 20 20 20 20 20 20 7d 2c 0a 23 69 66 64 65 66  c      },.#ifdef
1e154 20 53 51 4c 49 54 45 5f 53 4f 55 4e 44 45 58 0a   SQLITE_SOUNDEX.
1e155 20 20 20 20 7b 20 22 73 6f 75 6e 64 65 78 22 2c      { "soundex",
1e156 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30              1, 0
1e157 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20  , SQLITE_UTF8,  
1e158 20 20 30 2c 20 73 6f 75 6e 64 65 78 46 75 6e 63    0, soundexFunc
1e159 7d 2c 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65  },.#endif.#ifnde
1e15a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
1e15b 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 20 20 20  AD_EXTENSION.   
1e15c 20 7b 20 22 6c 6f 61 64 5f 65 78 74 65 6e 73 69   { "load_extensi
1e15d 6f 6e 22 2c 20 20 20 20 20 31 2c 20 30 2c 20 53  on",     1, 0, S
1e15e 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 6c  QLITE_UTF8, 0, l
1e15f 6f 61 64 45 78 74 20 7d 2c 0a 20 20 20 20 7b 20  oadExt },.    { 
1e160 22 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 22  "load_extension"
1e161 2c 20 20 20 20 20 32 2c 20 30 2c 20 53 51 4c 49  ,     2, 0, SQLI
1e162 54 45 5f 55 54 46 38 2c 20 30 2c 20 6c 6f 61 64  TE_UTF8, 0, load
1e163 45 78 74 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 20  Ext },.#endif.  
1e164 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  };.  static cons
1e165 74 20 73 74 72 75 63 74 20 7b 0a 20 20 20 20 63  t struct {.    c
1e166 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 20 20  har *zName;.    
1e167 73 69 67 6e 65 64 20 63 68 61 72 20 6e 41 72 67  signed char nArg
1e168 3b 0a 20 20 20 20 75 38 20 61 72 67 54 79 70 65  ;.    u8 argType
1e169 3b 0a 20 20 20 20 75 38 20 6e 65 65 64 43 6f 6c  ;.    u8 needCol
1e16a 6c 53 65 71 3b 0a 20 20 20 20 76 6f 69 64 20 28  lSeq;.    void (
1e16b 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f  *xStep)(sqlite3_
1e16c 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
1e16d 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 0a 20  ite3_value**);. 
1e16e 20 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c     void (*xFinal
1e16f 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  ize)(sqlite3_con
1e170 74 65 78 74 2a 29 3b 0a 20 20 7d 20 61 41 67 67  text*);.  } aAgg
1e171 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22 6d  s[] = {.    { "m
1e172 69 6e 22 2c 20 20 20 20 31 2c 20 30 2c 20 31 2c  in",    1, 0, 1,
1e173 20 6d 69 6e 6d 61 78 53 74 65 70 2c 20 20 20 6d   minmaxStep,   m
1e174 69 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 20 7d 2c  inMaxFinalize },
1e175 0a 20 20 20 20 7b 20 22 6d 61 78 22 2c 20 20 20  .    { "max",   
1e176 20 31 2c 20 31 2c 20 31 2c 20 6d 69 6e 6d 61 78   1, 1, 1, minmax
1e177 53 74 65 70 2c 20 20 20 6d 69 6e 4d 61 78 46 69  Step,   minMaxFi
1e178 6e 61 6c 69 7a 65 20 7d 2c 0a 20 20 20 20 7b 20  nalize },.    { 
1e179 22 73 75 6d 22 2c 20 20 20 20 31 2c 20 30 2c 20  "sum",    1, 0, 
1e17a 30 2c 20 73 75 6d 53 74 65 70 2c 20 20 20 20 20  0, sumStep,     
1e17b 20 73 75 6d 46 69 6e 61 6c 69 7a 65 20 20 20 20   sumFinalize    
1e17c 7d 2c 0a 20 20 20 20 7b 20 22 74 6f 74 61 6c 22  },.    { "total"
1e17d 2c 20 20 31 2c 20 30 2c 20 30 2c 20 73 75 6d 53  ,  1, 0, 0, sumS
1e17e 74 65 70 2c 20 20 20 20 20 20 74 6f 74 61 6c 46  tep,      totalF
1e17f 69 6e 61 6c 69 7a 65 20 20 20 20 7d 2c 0a 20 20  inalize    },.  
1e180 20 20 7b 20 22 61 76 67 22 2c 20 20 20 20 31 2c    { "avg",    1,
1e181 20 30 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c 20   0, 0, sumStep, 
1e182 20 20 20 20 20 61 76 67 46 69 6e 61 6c 69 7a 65       avgFinalize
1e183 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 63 6f      },.    { "co
1e184 75 6e 74 22 2c 20 20 30 2c 20 30 2c 20 30 2c 20  unt",  0, 0, 0, 
1e185 63 6f 75 6e 74 53 74 65 70 2c 20 20 20 20 63 6f  countStep,    co
1e186 75 6e 74 46 69 6e 61 6c 69 7a 65 20 20 7d 2c 0a  untFinalize  },.
1e187 20 20 20 20 7b 20 22 63 6f 75 6e 74 22 2c 20 20      { "count",  
1e188 31 2c 20 30 2c 20 30 2c 20 63 6f 75 6e 74 53 74  1, 0, 0, countSt
1e189 65 70 2c 20 20 20 20 63 6f 75 6e 74 46 69 6e 61  ep,    countFina
1e18a 6c 69 7a 65 20 20 7d 2c 0a 20 20 20 20 7b 20 22  lize  },.    { "
1e18b 67 72 6f 75 70 5f 63 6f 6e 63 61 74 22 2c 20 31  group_concat", 1
1e18c 2c 20 30 2c 20 30 2c 20 67 72 6f 75 70 43 6f 6e  , 0, 0, groupCon
1e18d 63 61 74 53 74 65 70 2c 20 67 72 6f 75 70 43 6f  catStep, groupCo
1e18e 6e 63 61 74 46 69 6e 61 6c 69 7a 65 20 7d 2c 0a  ncatFinalize },.
1e18f 20 20 20 20 7b 20 22 67 72 6f 75 70 5f 63 6f 6e      { "group_con
1e190 63 61 74 22 2c 20 32 2c 20 30 2c 20 30 2c 20 67  cat", 2, 0, 0, g
1e191 72 6f 75 70 43 6f 6e 63 61 74 53 74 65 70 2c 20  roupConcatStep, 
1e192 67 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c  groupConcatFinal
1e193 69 7a 65 20 7d 2c 0a 20 20 7d 3b 0a 20 20 69 6e  ize },.  };.  in
1e194 74 20 69 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b  t i;..  for(i=0;
1e195 20 69 3c 73 69 7a 65 6f 66 28 61 46 75 6e 63 73   i<sizeof(aFuncs
1e196 29 2f 73 69 7a 65 6f 66 28 61 46 75 6e 63 73 5b  )/sizeof(aFuncs[
1e197 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76  0]); i++){.    v
1e198 6f 69 64 20 2a 70 41 72 67 3b 0a 20 20 20 20 75  oid *pArg;.    u
1e199 38 20 61 72 67 54 79 70 65 20 3d 20 61 46 75 6e  8 argType = aFun
1e19a 63 73 5b 69 5d 2e 61 72 67 54 79 70 65 3b 0a 20  cs[i].argType;. 
1e19b 20 20 20 70 41 72 67 20 3d 20 28 76 6f 69 64 2a     pArg = (void*
1e19c 29 28 69 6e 74 29 61 72 67 54 79 70 65 3b 0a 20  )(int)argType;. 
1e19d 20 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65     sqlite3Create
1e19e 46 75 6e 63 28 64 62 2c 20 61 46 75 6e 63 73 5b  Func(db, aFuncs[
1e19f 69 5d 2e 7a 4e 61 6d 65 2c 20 61 46 75 6e 63 73  i].zName, aFuncs
1e1a0 5b 69 5d 2e 6e 41 72 67 2c 0a 20 20 20 20 20 20  [i].nArg,.      
1e1a1 20 20 61 46 75 6e 63 73 5b 69 5d 2e 65 54 65 78    aFuncs[i].eTex
1e1a2 74 52 65 70 2c 20 70 41 72 67 2c 20 61 46 75 6e  tRep, pArg, aFun
1e1a3 63 73 5b 69 5d 2e 78 46 75 6e 63 2c 20 30 2c 20  cs[i].xFunc, 0, 
1e1a4 30 29 3b 0a 20 20 20 20 69 66 28 20 61 46 75 6e  0);.    if( aFun
1e1a5 63 73 5b 69 5d 2e 6e 65 65 64 43 6f 6c 6c 53 65  cs[i].needCollSe
1e1a6 71 20 29 7b 0a 20 20 20 20 20 20 46 75 6e 63 44  q ){.      FuncD
1e1a7 65 66 20 2a 70 46 75 6e 63 20 3d 20 73 71 6c 69  ef *pFunc = sqli
1e1a8 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28  te3FindFunction(
1e1a9 64 62 2c 20 61 46 75 6e 63 73 5b 69 5d 2e 7a 4e  db, aFuncs[i].zN
1e1aa 61 6d 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20  ame, .          
1e1ab 73 74 72 6c 65 6e 28 61 46 75 6e 63 73 5b 69 5d  strlen(aFuncs[i]
1e1ac 2e 7a 4e 61 6d 65 29 2c 20 61 46 75 6e 63 73 5b  .zName), aFuncs[
1e1ad 69 5d 2e 6e 41 72 67 2c 20 61 46 75 6e 63 73 5b  i].nArg, aFuncs[
1e1ae 69 5d 2e 65 54 65 78 74 52 65 70 2c 20 30 29 3b  i].eTextRep, 0);
1e1af 0a 20 20 20 20 20 20 69 66 28 20 70 46 75 6e 63  .      if( pFunc
1e1b0 20 26 26 20 61 46 75 6e 63 73 5b 69 5d 2e 6e 65   && aFuncs[i].ne
1e1b1 65 64 43 6f 6c 6c 53 65 71 20 29 7b 0a 20 20 20  edCollSeq ){.   
1e1b2 20 20 20 20 20 70 46 75 6e 63 2d 3e 6e 65 65 64       pFunc->need
1e1b3 43 6f 6c 6c 53 65 71 20 3d 20 31 3b 0a 20 20 20  CollSeq = 1;.   
1e1b4 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23     }.    }.  }.#
1e1b5 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1e1b6 49 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20 20  IT_ALTERTABLE.  
1e1b7 73 71 6c 69 74 65 33 41 6c 74 65 72 46 75 6e 63  sqlite3AlterFunc
1e1b8 74 69 6f 6e 73 28 64 62 29 3b 0a 23 65 6e 64 69  tions(db);.#endi
1e1b9 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  f.#ifndef SQLITE
1e1ba 5f 4f 4d 49 54 5f 50 41 52 53 45 52 0a 20 20 73  _OMIT_PARSER.  s
1e1bb 71 6c 69 74 65 33 41 74 74 61 63 68 46 75 6e 63  qlite3AttachFunc
1e1bc 74 69 6f 6e 73 28 64 62 29 3b 0a 23 65 6e 64 69  tions(db);.#endi
1e1bd 66 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73  f.  for(i=0; i<s
1e1be 69 7a 65 6f 66 28 61 41 67 67 73 29 2f 73 69 7a  izeof(aAggs)/siz
1e1bf 65 6f 66 28 61 41 67 67 73 5b 30 5d 29 3b 20 69  eof(aAggs[0]); i
1e1c0 2b 2b 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70  ++){.    void *p
1e1c1 41 72 67 20 3d 20 28 76 6f 69 64 2a 29 28 69 6e  Arg = (void*)(in
1e1c2 74 29 61 41 67 67 73 5b 69 5d 2e 61 72 67 54 79  t)aAggs[i].argTy
1e1c3 70 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43  pe;.    sqlite3C
1e1c4 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 61 41  reateFunc(db, aA
1e1c5 67 67 73 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 61 41  ggs[i].zName, aA
1e1c6 67 67 73 5b 69 5d 2e 6e 41 72 67 2c 20 53 51 4c  ggs[i].nArg, SQL
1e1c7 49 54 45 5f 55 54 46 38 2c 20 0a 20 20 20 20 20  ITE_UTF8, .     
1e1c8 20 20 20 70 41 72 67 2c 20 30 2c 20 61 41 67 67     pArg, 0, aAgg
1e1c9 73 5b 69 5d 2e 78 53 74 65 70 2c 20 61 41 67 67  s[i].xStep, aAgg
1e1ca 73 5b 69 5d 2e 78 46 69 6e 61 6c 69 7a 65 29 3b  s[i].xFinalize);
1e1cb 0a 20 20 20 20 69 66 28 20 61 41 67 67 73 5b 69  .    if( aAggs[i
1e1cc 5d 2e 6e 65 65 64 43 6f 6c 6c 53 65 71 20 29 7b  ].needCollSeq ){
1e1cd 0a 20 20 20 20 20 20 46 75 6e 63 44 65 66 20 2a  .      FuncDef *
1e1ce 70 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 46  pFunc = sqlite3F
1e1cf 69 6e 64 46 75 6e 63 74 69 6f 6e 28 20 64 62 2c  indFunction( db,
1e1d0 20 61 41 67 67 73 5b 69 5d 2e 7a 4e 61 6d 65 2c   aAggs[i].zName,
1e1d1 0a 20 20 20 20 20 20 20 20 20 20 73 74 72 6c 65  .          strle
1e1d2 6e 28 61 41 67 67 73 5b 69 5d 2e 7a 4e 61 6d 65  n(aAggs[i].zName
1e1d3 29 2c 20 61 41 67 67 73 5b 69 5d 2e 6e 41 72 67  ), aAggs[i].nArg
1e1d4 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30  , SQLITE_UTF8, 0
1e1d5 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 46 75  );.      if( pFu
1e1d6 6e 63 20 26 26 20 61 41 67 67 73 5b 69 5d 2e 6e  nc && aAggs[i].n
1e1d7 65 65 64 43 6f 6c 6c 53 65 71 20 29 7b 0a 20 20  eedCollSeq ){.  
1e1d8 20 20 20 20 20 20 70 46 75 6e 63 2d 3e 6e 65 65        pFunc->nee
1e1d9 64 43 6f 6c 6c 53 65 71 20 3d 20 31 3b 0a 20 20  dCollSeq = 1;.  
1e1da 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1e1db 20 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65    sqlite3Registe
1e1dc 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f  rDateTimeFunctio
1e1dd 6e 73 28 64 62 29 3b 0a 20 20 69 66 28 20 21 64  ns(db);.  if( !d
1e1de 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1e1df 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20  ){.    int rc = 
1e1e0 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64  sqlite3_overload
1e1e1 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 4d  _function(db, "M
1e1e2 41 54 43 48 22 2c 20 32 29 3b 0a 20 20 20 20 61  ATCH", 2);.    a
1e1e3 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
1e1e4 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53  E_NOMEM || rc==S
1e1e5 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20  QLITE_OK );.    
1e1e6 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  if( rc==SQLITE_N
1e1e7 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 64 62  OMEM ){.      db
1e1e8 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
1e1e9 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69   1;.    }.  }.#i
1e1ea 66 64 65 66 20 53 51 4c 49 54 45 5f 53 53 45 0a  fdef SQLITE_SSE.
1e1eb 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
1e1ec 73 65 46 75 6e 63 74 69 6f 6e 73 28 64 62 29 3b  seFunctions(db);
1e1ed 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
1e1ee 51 4c 49 54 45 5f 43 41 53 45 5f 53 45 4e 53 49  QLITE_CASE_SENSI
1e1ef 54 49 56 45 5f 4c 49 4b 45 0a 20 20 73 71 6c 69  TIVE_LIKE.  sqli
1e1f0 74 65 33 52 65 67 69 73 74 65 72 4c 69 6b 65 46  te3RegisterLikeF
1e1f1 75 6e 63 74 69 6f 6e 73 28 64 62 2c 20 31 29 3b  unctions(db, 1);
1e1f2 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 74 65 33  .#else.  sqlite3
1e1f3 52 65 67 69 73 74 65 72 4c 69 6b 65 46 75 6e 63  RegisterLikeFunc
1e1f4 74 69 6f 6e 73 28 64 62 2c 20 30 29 3b 0a 23 65  tions(db, 0);.#e
1e1f5 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65  ndif.}../*.** Se
1e1f6 74 20 74 68 65 20 4c 49 4b 45 4f 50 54 20 66 6c  t the LIKEOPT fl
1e1f7 61 67 20 6f 6e 20 74 68 65 20 32 2d 61 72 67 75  ag on the 2-argu
1e1f8 6d 65 6e 74 20 66 75 6e 63 74 69 6f 6e 20 77 69  ment function wi
1e1f9 74 68 20 74 68 65 20 67 69 76 65 6e 20 6e 61 6d  th the given nam
1e1fa 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
1e1fb 64 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c 61 67  d setLikeOptFlag
1e1fc 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
1e1fd 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
1e1fe 20 69 6e 74 20 66 6c 61 67 56 61 6c 29 7b 0a 20   int flagVal){. 
1e1ff 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 0a   FuncDef *pDef;.
1e200 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33    pDef = sqlite3
1e201 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c  FindFunction(db,
1e202 20 7a 4e 61 6d 65 2c 20 73 74 72 6c 65 6e 28 7a   zName, strlen(z
1e203 4e 61 6d 65 29 2c 20 32 2c 20 53 51 4c 49 54 45  Name), 2, SQLITE
1e204 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20 69 66 28  _UTF8, 0);.  if(
1e205 20 70 44 65 66 20 29 7b 0a 20 20 20 20 70 44 65   pDef ){.    pDe
1e206 66 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 56  f->flags = flagV
1e207 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  al;.  }.}../*.**
1e208 20 52 65 67 69 73 74 65 72 20 74 68 65 20 62 75   Register the bu
1e209 69 6c 74 2d 69 6e 20 4c 49 4b 45 20 61 6e 64 20  ilt-in LIKE and 
1e20a 47 4c 4f 42 20 66 75 6e 63 74 69 6f 6e 73 2e 20  GLOB functions. 
1e20b 20 54 68 65 20 63 61 73 65 53 65 6e 73 69 74 69   The caseSensiti
1e20c 76 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  ve.** parameter 
1e20d 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68  determines wheth
1e20e 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 4c 49  er or not the LI
1e20f 4b 45 20 6f 70 65 72 61 74 6f 72 20 69 73 20 63  KE operator is c
1e210 61 73 65 0a 2a 2a 20 73 65 6e 73 69 74 69 76 65  ase.** sensitive
1e211 2e 20 20 47 4c 4f 42 20 69 73 20 61 6c 77 61 79  .  GLOB is alway
1e212 73 20 63 61 73 65 20 73 65 6e 73 69 74 69 76 65  s case sensitive
1e213 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1e214 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1e215 52 65 67 69 73 74 65 72 4c 69 6b 65 46 75 6e 63  RegisterLikeFunc
1e216 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64  tions(sqlite3 *d
1e217 62 2c 20 69 6e 74 20 63 61 73 65 53 65 6e 73 69  b, int caseSensi
1e218 74 69 76 65 29 7b 0a 20 20 73 74 72 75 63 74 20  tive){.  struct 
1e219 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70 49 6e  compareInfo *pIn
1e21a 66 6f 3b 0a 20 20 69 66 28 20 63 61 73 65 53 65  fo;.  if( caseSe
1e21b 6e 73 69 74 69 76 65 20 29 7b 0a 20 20 20 20 70  nsitive ){.    p
1e21c 49 6e 66 6f 20 3d 20 28 73 74 72 75 63 74 20 63  Info = (struct c
1e21d 6f 6d 70 61 72 65 49 6e 66 6f 2a 29 26 6c 69 6b  ompareInfo*)&lik
1e21e 65 49 6e 66 6f 41 6c 74 3b 0a 20 20 7d 65 6c 73  eInfoAlt;.  }els
1e21f 65 7b 0a 20 20 20 20 70 49 6e 66 6f 20 3d 20 28  e{.    pInfo = (
1e220 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e  struct compareIn
1e221 66 6f 2a 29 26 6c 69 6b 65 49 6e 66 6f 4e 6f 72  fo*)&likeInfoNor
1e222 6d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  m;.  }.  sqlite3
1e223 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22  CreateFunc(db, "
1e224 6c 69 6b 65 22 2c 20 32 2c 20 53 51 4c 49 54 45  like", 2, SQLITE
1e225 5f 55 54 46 38 2c 20 70 49 6e 66 6f 2c 20 6c 69  _UTF8, pInfo, li
1e226 6b 65 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 20  keFunc, 0, 0);. 
1e227 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75   sqlite3CreateFu
1e228 6e 63 28 64 62 2c 20 22 6c 69 6b 65 22 2c 20 33  nc(db, "like", 3
1e229 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 70  , SQLITE_UTF8, p
1e22a 49 6e 66 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c 20  Info, likeFunc, 
1e22b 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  0, 0);.  sqlite3
1e22c 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22  CreateFunc(db, "
1e22d 67 6c 6f 62 22 2c 20 32 2c 20 53 51 4c 49 54 45  glob", 2, SQLITE
1e22e 5f 55 54 46 38 2c 20 0a 20 20 20 20 20 20 28 73  _UTF8, .      (s
1e22f 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66  truct compareInf
1e230 6f 2a 29 26 67 6c 6f 62 49 6e 66 6f 2c 20 6c 69  o*)&globInfo, li
1e231 6b 65 46 75 6e 63 2c 20 30 2c 30 29 3b 0a 20 20  keFunc, 0,0);.  
1e232 73 65 74 4c 69 6b 65 4f 70 74 46 6c 61 67 28 64  setLikeOptFlag(d
1e233 62 2c 20 22 67 6c 6f 62 22 2c 20 53 51 4c 49 54  b, "glob", SQLIT
1e234 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 7c 20 53 51  E_FUNC_LIKE | SQ
1e235 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 3b  LITE_FUNC_CASE);
1e236 0a 20 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c 61  .  setLikeOptFla
1e237 67 28 64 62 2c 20 22 6c 69 6b 65 22 2c 20 0a 20  g(db, "like", . 
1e238 20 20 20 20 20 63 61 73 65 53 65 6e 73 69 74 69       caseSensiti
1e239 76 65 20 3f 20 28 53 51 4c 49 54 45 5f 46 55 4e  ve ? (SQLITE_FUN
1e23a 43 5f 4c 49 4b 45 20 7c 20 53 51 4c 49 54 45 5f  C_LIKE | SQLITE_
1e23b 46 55 4e 43 5f 43 41 53 45 29 20 3a 20 53 51 4c  FUNC_CASE) : SQL
1e23c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29 3b 0a  ITE_FUNC_LIKE);.
1e23d 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 70  }../*.** pExpr p
1e23e 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65 78 70 72  oints to an expr
1e23f 65 73 73 69 6f 6e 20 77 68 69 63 68 20 69 6d 70  ession which imp
1e240 6c 65 6d 65 6e 74 73 20 61 20 66 75 6e 63 74 69  lements a functi
1e241 6f 6e 2e 20 20 49 66 0a 2a 2a 20 69 74 20 69 73  on.  If.** it is
1e242 20 61 70 70 72 6f 70 72 69 61 74 65 20 74 6f 20   appropriate to 
1e243 61 70 70 6c 79 20 74 68 65 20 4c 49 4b 45 20 6f  apply the LIKE o
1e244 70 74 69 6d 69 7a 61 74 69 6f 6e 20 74 6f 20 74  ptimization to t
1e245 68 61 74 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  hat function.** 
1e246 74 68 65 6e 20 73 65 74 20 61 57 63 5b 30 5d 20  then set aWc[0] 
1e247 74 68 72 6f 75 67 68 20 61 57 63 5b 32 5d 20 74  through aWc[2] t
1e248 6f 20 74 68 65 20 77 69 6c 64 63 61 72 64 20 63  o the wildcard c
1e249 68 61 72 61 63 74 65 72 73 20 61 6e 64 0a 2a 2a  haracters and.**
1e24a 20 72 65 74 75 72 6e 20 54 52 55 45 2e 20 20 49   return TRUE.  I
1e24b 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69  f the function i
1e24c 73 20 6e 6f 74 20 61 20 4c 49 4b 45 2d 73 74 79  s not a LIKE-sty
1e24d 6c 65 20 66 75 6e 63 74 69 6f 6e 20 74 68 65 6e  le function then
1e24e 0a 2a 2a 20 72 65 74 75 72 6e 20 46 41 4c 53 45  .** return FALSE
1e24f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1e250 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
1e251 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 28 73 71  sLikeFunction(sq
1e252 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20  lite3 *db, Expr 
1e253 2a 70 45 78 70 72 2c 20 69 6e 74 20 2a 70 49 73  *pExpr, int *pIs
1e254 4e 6f 63 61 73 65 2c 20 63 68 61 72 20 2a 61 57  Nocase, char *aW
1e255 63 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70  c){.  FuncDef *p
1e256 44 65 66 3b 0a 20 20 69 66 28 20 70 45 78 70 72  Def;.  if( pExpr
1e257 2d 3e 6f 70 21 3d 54 4b 5f 46 55 4e 43 54 49 4f  ->op!=TK_FUNCTIO
1e258 4e 20 7c 7c 20 21 70 45 78 70 72 2d 3e 70 4c 69  N || !pExpr->pLi
1e259 73 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  st ){.    return
1e25a 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 45   0;.  }.  if( pE
1e25b 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70  xpr->pList->nExp
1e25c 72 21 3d 32 20 29 7b 0a 20 20 20 20 72 65 74 75  r!=2 ){.    retu
1e25d 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 44 65 66  rn 0;.  }.  pDef
1e25e 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75   = sqlite3FindFu
1e25f 6e 63 74 69 6f 6e 28 64 62 2c 20 28 63 68 61 72  nction(db, (char
1e260 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a  *)pExpr->token.z
1e261 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e  , pExpr->token.n
1e262 2c 20 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 2,.           
1e263 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e264 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30    SQLITE_UTF8, 0
1e265 29 3b 0a 20 20 69 66 28 20 70 44 65 66 3d 3d 30  );.  if( pDef==0
1e266 20 7c 7c 20 28 70 44 65 66 2d 3e 66 6c 61 67 73   || (pDef->flags
1e267 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c   & SQLITE_FUNC_L
1e268 49 4b 45 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72  IKE)==0 ){.    r
1e269 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20  eturn 0;.  }..  
1e26a 2f 2a 20 54 68 65 20 6d 65 6d 63 70 79 28 29 20  /* The memcpy() 
1e26b 73 74 61 74 65 6d 65 6e 74 20 61 73 73 75 6d 65  statement assume
1e26c 73 20 74 68 61 74 20 74 68 65 20 77 69 6c 64 63  s that the wildc
1e26d 61 72 64 20 63 68 61 72 61 63 74 65 72 73 20 61  ard characters a
1e26e 72 65 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73  re.  ** the firs
1e26f 74 20 74 68 72 65 65 20 73 74 61 74 65 6d 65 6e  t three statemen
1e270 74 73 20 69 6e 20 74 68 65 20 63 6f 6d 70 61 72  ts in the compar
1e271 65 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e  eInfo structure.
1e272 20 20 54 68 65 0a 20 20 2a 2a 20 61 73 73 65 72    The.  ** asser
1e273 74 73 28 29 20 74 68 61 74 20 66 6f 6c 6c 6f 77  ts() that follow
1e274 20 76 65 72 69 66 79 20 74 68 61 74 20 61 73 73   verify that ass
1e275 75 6d 70 74 69 6f 6e 0a 20 20 2a 2f 0a 20 20 6d  umption.  */.  m
1e276 65 6d 63 70 79 28 61 57 63 2c 20 70 44 65 66 2d  emcpy(aWc, pDef-
1e277 3e 70 55 73 65 72 44 61 74 61 2c 20 33 29 3b 0a  >pUserData, 3);.
1e278 20 20 61 73 73 65 72 74 28 20 28 63 68 61 72 2a    assert( (char*
1e279 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 20 3d 3d  )&likeInfoAlt ==
1e27a 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66   (char*)&likeInf
1e27b 6f 41 6c 74 2e 6d 61 74 63 68 41 6c 6c 20 29 3b  oAlt.matchAll );
1e27c 0a 20 20 61 73 73 65 72 74 28 20 26 28 28 63 68  .  assert( &((ch
1e27d 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74  ar*)&likeInfoAlt
1e27e 29 5b 31 5d 20 3d 3d 20 28 63 68 61 72 2a 29 26  )[1] == (char*)&
1e27f 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e 6d 61 74 63  likeInfoAlt.matc
1e280 68 4f 6e 65 20 29 3b 0a 20 20 61 73 73 65 72 74  hOne );.  assert
1e281 28 20 26 28 28 63 68 61 72 2a 29 26 6c 69 6b 65  ( &((char*)&like
1e282 49 6e 66 6f 41 6c 74 29 5b 32 5d 20 3d 3d 20 28  InfoAlt)[2] == (
1e283 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41  char*)&likeInfoA
1e284 6c 74 2e 6d 61 74 63 68 53 65 74 20 29 3b 0a 20  lt.matchSet );. 
1e285 20 2a 70 49 73 4e 6f 63 61 73 65 20 3d 20 28 70   *pIsNocase = (p
1e286 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  Def->flags & SQL
1e287 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 3d 3d  ITE_FUNC_CASE)==
1e288 30 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d  0;.  return 1;.}
1e289 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
1e28a 2a 20 45 6e 64 20 6f 66 20 66 75 6e 63 2e 63 20  * End of func.c 
1e28b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e28c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e28d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e28e 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
1e28f 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 69 6e 73  * Begin file ins
1e290 65 72 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ert.c **********
1e291 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e292 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e293 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
1e294 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
1e295 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
1e296 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
1e297 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
1e298 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
1e299 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
1e29a 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
1e29b 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
1e29c 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
1e29d 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
1e29e 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
1e29f 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
1e2a0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
1e2a1 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
1e2a2 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
1e2a3 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
1e2a4 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
1e2a5 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
1e2a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e2a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e2a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e2a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e2aa 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
1e2ab 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43   file contains C
1e2ac 20 63 6f 64 65 20 72 6f 75 74 69 6e 65 73 20 74   code routines t
1e2ad 68 61 74 20 61 72 65 20 63 61 6c 6c 65 64 20 62  hat are called b
1e2ae 79 20 74 68 65 20 70 61 72 73 65 72 0a 2a 2a 20  y the parser.** 
1e2af 74 6f 20 68 61 6e 64 6c 65 20 49 4e 53 45 52 54  to handle INSERT
1e2b0 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 53   statements in S
1e2b1 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  QLite..**.** $Id
1e2b2 3a 20 69 6e 73 65 72 74 2e 63 2c 76 20 31 2e 32  : insert.c,v 1.2
1e2b3 33 38 20 32 30 30 38 2f 30 34 2f 32 38 20 31 38  38 2008/04/28 18
1e2b4 3a 34 36 3a 34 33 20 64 72 68 20 45 78 70 20 24  :46:43 drh Exp $
1e2b5 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 50  .*/../*.** Set P
1e2b6 34 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65  4 of the most re
1e2b7 63 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64 20  cently inserted 
1e2b8 6f 70 63 6f 64 65 20 74 6f 20 61 20 63 6f 6c 75  opcode to a colu
1e2b9 6d 6e 20 61 66 66 69 6e 69 74 79 0a 2a 2a 20 73  mn affinity.** s
1e2ba 74 72 69 6e 67 20 66 6f 72 20 69 6e 64 65 78 20  tring for index 
1e2bb 70 49 64 78 2e 20 41 20 63 6f 6c 75 6d 6e 20 61  pIdx. A column a
1e2bc 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 68  ffinity string h
1e2bd 61 73 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72  as one character
1e2be 0a 2a 2a 20 66 6f 72 20 65 61 63 68 20 63 6f 6c  .** for each col
1e2bf 75 6d 6e 20 69 6e 20 74 68 65 20 74 61 62 6c 65  umn in the table
1e2c0 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74  , according to t
1e2c1 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74  he affinity of t
1e2c2 68 65 20 63 6f 6c 75 6d 6e 3a 0a 2a 2a 0a 2a 2a  he column:.**.**
1e2c3 20 20 43 68 61 72 61 63 74 65 72 20 20 20 20 20    Character     
1e2c4 20 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79   Column affinity
1e2c5 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .**  -----------
1e2c6 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1e2c7 2d 2d 2d 0a 2a 2a 20 20 27 61 27 20 20 20 20 20  ---.**  'a'     
1e2c8 20 20 20 20 20 20 20 54 45 58 54 0a 2a 2a 20 20         TEXT.**  
1e2c9 27 62 27 20 20 20 20 20 20 20 20 20 20 20 20 4e  'b'            N
1e2ca 4f 4e 45 0a 2a 2a 20 20 27 63 27 20 20 20 20 20  ONE.**  'c'     
1e2cb 20 20 20 20 20 20 20 4e 55 4d 45 52 49 43 0a 2a         NUMERIC.*
1e2cc 2a 20 20 27 64 27 20 20 20 20 20 20 20 20 20 20  *  'd'          
1e2cd 20 20 49 4e 54 45 47 45 52 0a 2a 2a 20 20 27 65    INTEGER.**  'e
1e2ce 27 20 20 20 20 20 20 20 20 20 20 20 20 52 45 41  '            REA
1e2cf 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 74 72 61  L.**.** An extra
1e2d0 20 27 62 27 20 69 73 20 61 70 70 65 6e 64 65 64   'b' is appended
1e2d1 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74   to the end of t
1e2d2 68 65 20 73 74 72 69 6e 67 20 74 6f 20 63 6f 76  he string to cov
1e2d3 65 72 20 74 68 65 0a 2a 2a 20 72 6f 77 69 64 20  er the.** rowid 
1e2d4 74 68 61 74 20 61 70 70 65 61 72 73 20 61 73 20  that appears as 
1e2d5 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20  the last column 
1e2d6 69 6e 20 65 76 65 72 79 20 69 6e 64 65 78 2e 0a  in every index..
1e2d7 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1e2d8 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e  E void sqlite3In
1e2d9 64 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 56  dexAffinityStr(V
1e2da 64 62 65 20 2a 76 2c 20 49 6e 64 65 78 20 2a 70  dbe *v, Index *p
1e2db 49 64 78 29 7b 0a 20 20 69 66 28 20 21 70 49 64  Idx){.  if( !pId
1e2dc 78 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20  x->zColAff ){.  
1e2dd 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74    /* The first t
1e2de 69 6d 65 20 61 20 63 6f 6c 75 6d 6e 20 61 66 66  ime a column aff
1e2df 69 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72  inity string for
1e2e0 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e   a particular in
1e2e1 64 65 78 20 69 73 0a 20 20 20 20 2a 2a 20 72 65  dex is.    ** re
1e2e2 71 75 69 72 65 64 2c 20 69 74 20 69 73 20 61 6c  quired, it is al
1e2e3 6c 6f 63 61 74 65 64 20 61 6e 64 20 70 6f 70 75  located and popu
1e2e4 6c 61 74 65 64 20 68 65 72 65 2e 20 49 74 20 69  lated here. It i
1e2e5 73 20 74 68 65 6e 20 73 74 6f 72 65 64 20 61 73  s then stored as
1e2e6 0a 20 20 20 20 2a 2a 20 61 20 6d 65 6d 62 65 72  .    ** a member
1e2e7 20 6f 66 20 74 68 65 20 49 6e 64 65 78 20 73 74   of the Index st
1e2e8 72 75 63 74 75 72 65 20 66 6f 72 20 73 75 62 73  ructure for subs
1e2e9 65 71 75 65 6e 74 20 75 73 65 2e 0a 20 20 20 20  equent use..    
1e2ea 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 63 6f  **.    ** The co
1e2eb 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74  lumn affinity st
1e2ec 72 69 6e 67 20 77 69 6c 6c 20 65 76 65 6e 74 75  ring will eventu
1e2ed 61 6c 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20  ally be deleted 
1e2ee 62 79 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  by.    ** sqlite
1e2ef 44 65 6c 65 74 65 49 6e 64 65 78 28 29 20 77 68  DeleteIndex() wh
1e2f0 65 6e 20 74 68 65 20 49 6e 64 65 78 20 73 74 72  en the Index str
1e2f1 75 63 74 75 72 65 20 69 74 73 65 6c 66 20 69 73  ucture itself is
1e2f2 20 63 6c 65 61 6e 65 64 0a 20 20 20 20 2a 2a 20   cleaned.    ** 
1e2f3 75 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  up..    */.    i
1e2f4 6e 74 20 6e 3b 0a 20 20 20 20 54 61 62 6c 65 20  nt n;.    Table 
1e2f5 2a 70 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54  *pTab = pIdx->pT
1e2f6 61 62 6c 65 3b 0a 20 20 20 20 73 71 6c 69 74 65  able;.    sqlite
1e2f7 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 56  3 *db = sqlite3V
1e2f8 64 62 65 44 62 28 76 29 3b 0a 20 20 20 20 70 49  dbeDb(v);.    pI
1e2f9 64 78 2d 3e 7a 43 6f 6c 41 66 66 20 3d 20 28 63  dx->zColAff = (c
1e2fa 68 61 72 20 2a 29 73 71 6c 69 74 65 33 44 62 4d  har *)sqlite3DbM
1e2fb 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 70 49 64  allocRaw(db, pId
1e2fc 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32 29 3b 0a 20  x->nColumn+2);. 
1e2fd 20 20 20 69 66 28 20 21 70 49 64 78 2d 3e 7a 43     if( !pIdx->zC
1e2fe 6f 6c 41 66 66 20 29 7b 0a 20 20 20 20 20 20 72  olAff ){.      r
1e2ff 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20  eturn;.    }.   
1e300 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 70 49 64 78   for(n=0; n<pIdx
1e301 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6e 2b 2b 29 7b  ->nColumn; n++){
1e302 0a 20 20 20 20 20 20 70 49 64 78 2d 3e 7a 43 6f  .      pIdx->zCo
1e303 6c 41 66 66 5b 6e 5d 20 3d 20 70 54 61 62 2d 3e  lAff[n] = pTab->
1e304 61 43 6f 6c 5b 70 49 64 78 2d 3e 61 69 43 6f 6c  aCol[pIdx->aiCol
1e305 75 6d 6e 5b 6e 5d 5d 2e 61 66 66 69 6e 69 74 79  umn[n]].affinity
1e306 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 64 78  ;.    }.    pIdx
1e307 2d 3e 7a 43 6f 6c 41 66 66 5b 6e 2b 2b 5d 20 3d  ->zColAff[n++] =
1e308 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45   SQLITE_AFF_NONE
1e309 3b 0a 20 20 20 20 70 49 64 78 2d 3e 7a 43 6f 6c  ;.    pIdx->zCol
1e30a 41 66 66 5b 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a  Aff[n] = 0;.  }.
1e30b 20 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43   .  sqlite3VdbeC
1e30c 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 70  hangeP4(v, -1, p
1e30d 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 2c 20 30 29  Idx->zColAff, 0)
1e30e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 50  ;.}../*.** Set P
1e30f 34 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65  4 of the most re
1e310 63 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64 20  cently inserted 
1e311 6f 70 63 6f 64 65 20 74 6f 20 61 20 63 6f 6c 75  opcode to a colu
1e312 6d 6e 20 61 66 66 69 6e 69 74 79 0a 2a 2a 20 73  mn affinity.** s
1e313 74 72 69 6e 67 20 66 6f 72 20 74 61 62 6c 65 20  tring for table 
1e314 70 54 61 62 2e 20 41 20 63 6f 6c 75 6d 6e 20 61  pTab. A column a
1e315 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 68  ffinity string h
1e316 61 73 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72  as one character
1e317 0a 2a 2a 20 66 6f 72 20 65 61 63 68 20 63 6f 6c  .** for each col
1e318 75 6d 6e 20 69 6e 64 65 78 65 64 20 62 79 20 74  umn indexed by t
1e319 68 65 20 69 6e 64 65 78 2c 20 61 63 63 6f 72 64  he index, accord
1e31a 69 6e 67 20 74 6f 20 74 68 65 20 61 66 66 69 6e  ing to the affin
1e31b 69 74 79 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f  ity of the.** co
1e31c 6c 75 6d 6e 3a 0a 2a 2a 0a 2a 2a 20 20 43 68 61  lumn:.**.**  Cha
1e31d 72 61 63 74 65 72 20 20 20 20 20 20 43 6f 6c 75  racter      Colu
1e31e 6d 6e 20 61 66 66 69 6e 69 74 79 0a 2a 2a 20 20  mn affinity.**  
1e31f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1e320 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a  --------------.*
1e321 2a 20 20 27 61 27 20 20 20 20 20 20 20 20 20 20  *  'a'          
1e322 20 20 54 45 58 54 0a 2a 2a 20 20 27 62 27 20 20    TEXT.**  'b'  
1e323 20 20 20 20 20 20 20 20 20 20 4e 4f 4e 45 0a 2a            NONE.*
1e324 2a 20 20 27 63 27 20 20 20 20 20 20 20 20 20 20  *  'c'          
1e325 20 20 4e 55 4d 45 52 49 43 0a 2a 2a 20 20 27 64    NUMERIC.**  'd
1e326 27 20 20 20 20 20 20 20 20 20 20 20 20 49 4e 54  '            INT
1e327 45 47 45 52 0a 2a 2a 20 20 27 65 27 20 20 20 20  EGER.**  'e'    
1e328 20 20 20 20 20 20 20 20 52 45 41 4c 0a 2a 2f 0a          REAL.*/.
1e329 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1e32a 6f 69 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65  oid sqlite3Table
1e32b 41 66 66 69 6e 69 74 79 53 74 72 28 56 64 62 65  AffinityStr(Vdbe
1e32c 20 2a 76 2c 20 54 61 62 6c 65 20 2a 70 54 61 62   *v, Table *pTab
1e32d 29 7b 0a 20 20 2f 2a 20 54 68 65 20 66 69 72 73  ){.  /* The firs
1e32e 74 20 74 69 6d 65 20 61 20 63 6f 6c 75 6d 6e 20  t time a column 
1e32f 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20  affinity string 
1e330 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72  for a particular
1e331 20 74 61 62 6c 65 0a 20 20 2a 2a 20 69 73 20 72   table.  ** is r
1e332 65 71 75 69 72 65 64 2c 20 69 74 20 69 73 20 61  equired, it is a
1e333 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 70 6f 70  llocated and pop
1e334 75 6c 61 74 65 64 20 68 65 72 65 2e 20 49 74 20  ulated here. It 
1e335 69 73 20 74 68 65 6e 20 0a 20 20 2a 2a 20 73 74  is then .  ** st
1e336 6f 72 65 64 20 61 73 20 61 20 6d 65 6d 62 65 72  ored as a member
1e337 20 6f 66 20 74 68 65 20 54 61 62 6c 65 20 73 74   of the Table st
1e338 72 75 63 74 75 72 65 20 66 6f 72 20 73 75 62 73  ructure for subs
1e339 65 71 75 65 6e 74 20 75 73 65 2e 0a 20 20 2a 2a  equent use..  **
1e33a 0a 20 20 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e  .  ** The column
1e33b 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67   affinity string
1e33c 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79   will eventually
1e33d 20 62 65 20 64 65 6c 65 74 65 64 20 62 79 0a 20   be deleted by. 
1e33e 20 2a 2a 20 73 71 6c 69 74 65 33 44 65 6c 65 74   ** sqlite3Delet
1e33f 65 54 61 62 6c 65 28 29 20 77 68 65 6e 20 74 68  eTable() when th
1e340 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  e Table structur
1e341 65 20 69 74 73 65 6c 66 20 69 73 20 63 6c 65 61  e itself is clea
1e342 6e 65 64 20 75 70 2e 0a 20 20 2a 2f 0a 20 20 69  ned up..  */.  i
1e343 66 28 20 21 70 54 61 62 2d 3e 7a 43 6f 6c 41 66  f( !pTab->zColAf
1e344 66 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a  f ){.    char *z
1e345 43 6f 6c 41 66 66 3b 0a 20 20 20 20 69 6e 74 20  ColAff;.    int 
1e346 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a  i;.    sqlite3 *
1e347 64 62 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  db = sqlite3Vdbe
1e348 44 62 28 76 29 3b 0a 0a 20 20 20 20 7a 43 6f 6c  Db(v);..    zCol
1e349 41 66 66 20 3d 20 28 63 68 61 72 20 2a 29 73 71  Aff = (char *)sq
1e34a 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
1e34b 28 64 62 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b  (db, pTab->nCol+
1e34c 31 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 43 6f  1);.    if( !zCo
1e34d 6c 41 66 66 20 29 7b 0a 20 20 20 20 20 20 72 65  lAff ){.      re
1e34e 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  turn;.    }..   
1e34f 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62   for(i=0; i<pTab
1e350 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  ->nCol; i++){.  
1e351 20 20 20 20 7a 43 6f 6c 41 66 66 5b 69 5d 20 3d      zColAff[i] =
1e352 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 61   pTab->aCol[i].a
1e353 66 66 69 6e 69 74 79 3b 0a 20 20 20 20 7d 0a 20  ffinity;.    }. 
1e354 20 20 20 7a 43 6f 6c 41 66 66 5b 70 54 61 62 2d     zColAff[pTab-
1e355 3e 6e 43 6f 6c 5d 20 3d 20 27 5c 30 27 3b 0a 0a  >nCol] = '\0';..
1e356 20 20 20 20 70 54 61 62 2d 3e 7a 43 6f 6c 41 66      pTab->zColAf
1e357 66 20 3d 20 7a 43 6f 6c 41 66 66 3b 0a 20 20 7d  f = zColAff;.  }
1e358 0a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43  ..  sqlite3VdbeC
1e359 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 70  hangeP4(v, -1, p
1e35a 54 61 62 2d 3e 7a 43 6f 6c 41 66 66 2c 20 30 29  Tab->zColAff, 0)
1e35b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
1e35c 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68  n non-zero if th
1e35d 65 20 74 61 62 6c 65 20 70 54 61 62 20 69 6e 20  e table pTab in 
1e35e 64 61 74 61 62 61 73 65 20 69 44 62 20 6f 72 20  database iDb or 
1e35f 61 6e 79 20 6f 66 20 69 74 73 20 69 6e 64 69 63  any of its indic
1e360 65 73 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20  es.** have been 
1e361 6f 70 65 6e 65 64 20 61 74 20 61 6e 79 20 70 6f  opened at any po
1e362 69 6e 74 20 69 6e 20 74 68 65 20 56 44 42 45 20  int in the VDBE 
1e363 70 72 6f 67 72 61 6d 20 62 65 67 69 6e 6e 69 6e  program beginnin
1e364 67 20 61 74 20 6c 6f 63 61 74 69 6f 6e 0a 2a 2a  g at location.**
1e365 20 69 53 74 61 72 74 41 64 64 72 20 74 68 72 6f   iStartAddr thro
1e366 75 67 68 74 20 74 68 65 20 65 6e 64 20 6f 66 20  ught the end of 
1e367 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 54 68  the program.  Th
1e368 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 73 65  is is used to se
1e369 65 20 69 66 20 0a 2a 2a 20 61 20 73 74 61 74 65  e if .** a state
1e36a 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 6f 72 6d  ment of the form
1e36b 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 3c    "INSERT INTO <
1e36c 69 44 62 2c 20 70 54 61 62 3e 20 53 45 4c 45 43  iDb, pTab> SELEC
1e36d 54 20 2e 2e 2e 22 20 63 61 6e 20 0a 2a 2a 20 72  T ..." can .** r
1e36e 75 6e 20 77 69 74 68 6f 75 74 20 75 73 69 6e 67  un without using
1e36f 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
1e370 20 66 6f 72 20 74 68 65 20 72 65 73 75 6c 74 73   for the results
1e371 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2e 20   of the SELECT. 
1e372 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
1e373 65 61 64 73 54 61 62 6c 65 28 56 64 62 65 20 2a  eadsTable(Vdbe *
1e374 76 2c 20 69 6e 74 20 69 53 74 61 72 74 41 64 64  v, int iStartAdd
1e375 72 2c 20 69 6e 74 20 69 44 62 2c 20 54 61 62 6c  r, int iDb, Tabl
1e376 65 20 2a 70 54 61 62 29 7b 0a 20 20 69 6e 74 20  e *pTab){.  int 
1e377 69 3b 0a 20 20 69 6e 74 20 69 45 6e 64 20 3d 20  i;.  int iEnd = 
1e378 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
1e379 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 66 6f 72  ntAddr(v);.  for
1e37a 28 69 3d 69 53 74 61 72 74 41 64 64 72 3b 20 69  (i=iStartAddr; i
1e37b 3c 69 45 6e 64 3b 20 69 2b 2b 29 7b 0a 20 20 20  <iEnd; i++){.   
1e37c 20 56 64 62 65 4f 70 20 2a 70 4f 70 20 3d 20 73   VdbeOp *pOp = s
1e37d 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28  qlite3VdbeGetOp(
1e37e 76 2c 20 69 29 3b 0a 20 20 20 20 61 73 73 65 72  v, i);.    asser
1e37f 74 28 20 70 4f 70 21 3d 30 20 29 3b 0a 20 20 20  t( pOp!=0 );.   
1e380 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
1e381 3d 3d 4f 50 5f 4f 70 65 6e 52 65 61 64 20 26 26  ==OP_OpenRead &&
1e382 20 70 4f 70 2d 3e 70 33 3d 3d 69 44 62 20 29 7b   pOp->p3==iDb ){
1e383 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49  .      Index *pI
1e384 6e 64 65 78 3b 0a 20 20 20 20 20 20 69 6e 74 20  ndex;.      int 
1e385 74 6e 75 6d 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a  tnum = pOp->p2;.
1e386 20 20 20 20 20 20 69 66 28 20 74 6e 75 6d 3d 3d        if( tnum==
1e387 70 54 61 62 2d 3e 74 6e 75 6d 20 29 7b 0a 20 20  pTab->tnum ){.  
1e388 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
1e389 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f        }.      fo
1e38a 72 28 70 49 6e 64 65 78 3d 70 54 61 62 2d 3e 70  r(pIndex=pTab->p
1e38b 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 3b 20 70  Index; pIndex; p
1e38c 49 6e 64 65 78 3d 70 49 6e 64 65 78 2d 3e 70 4e  Index=pIndex->pN
1e38d 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 69 66  ext){.        if
1e38e 28 20 74 6e 75 6d 3d 3d 70 49 6e 64 65 78 2d 3e  ( tnum==pIndex->
1e38f 74 6e 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  tnum ){.        
1e390 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
1e391 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
1e392 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
1e393 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
1e394 41 42 4c 45 0a 20 20 20 20 69 66 28 20 70 4f 70  ABLE.    if( pOp
1e395 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 4f 70  ->opcode==OP_VOp
1e396 65 6e 20 26 26 20 70 4f 70 2d 3e 70 34 2e 70 56  en && pOp->p4.pV
1e397 74 61 62 3d 3d 70 54 61 62 2d 3e 70 56 74 61 62  tab==pTab->pVtab
1e398 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
1e399 28 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 21  ( pOp->p4.pVtab!
1e39a 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  =0 );.      asse
1e39b 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d  rt( pOp->p4type=
1e39c 3d 50 34 5f 56 54 41 42 20 29 3b 0a 20 20 20 20  =P4_VTAB );.    
1e39d 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
1e39e 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72  }.#endif.  }.  r
1e39f 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 23 69 66 6e  eturn 0;.}..#ifn
1e3a0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1e3a1 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 2f 2a  AUTOINCREMENT./*
1e3a2 0a 2a 2a 20 57 72 69 74 65 20 6f 75 74 20 63 6f  .** Write out co
1e3a3 64 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65  de to initialize
1e3a4 20 74 68 65 20 61 75 74 6f 69 6e 63 72 65 6d 65   the autoincreme
1e3a5 6e 74 20 6c 6f 67 69 63 2e 20 20 54 68 69 73 20  nt logic.  This 
1e3a6 63 6f 64 65 0a 2a 2a 20 6c 6f 6f 6b 73 20 75 70  code.** looks up
1e3a7 20 74 68 65 20 63 75 72 72 65 6e 74 20 61 75 74   the current aut
1e3a8 6f 69 6e 63 72 65 6d 65 6e 74 20 76 61 6c 75 65  oincrement value
1e3a9 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 73   in the sqlite_s
1e3aa 65 71 75 65 6e 63 65 0a 2a 2a 20 74 61 62 6c 65  equence.** table
1e3ab 20 61 6e 64 20 73 74 6f 72 65 73 20 74 68 61 74   and stores that
1e3ac 20 76 61 6c 75 65 20 69 6e 20 61 20 72 65 67 69   value in a regi
1e3ad 73 74 65 72 2e 20 20 43 6f 64 65 20 67 65 6e 65  ster.  Code gene
1e3ae 72 61 74 65 64 20 62 79 0a 2a 2a 20 61 75 74 6f  rated by.** auto
1e3af 49 6e 63 53 74 65 70 28 29 20 77 69 6c 6c 20 6b  IncStep() will k
1e3b0 65 65 70 20 74 68 61 74 20 72 65 67 69 73 74 65  eep that registe
1e3b1 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 6c 61  r holding the la
1e3b2 72 67 65 73 74 0a 2a 2a 20 72 6f 77 69 64 20 76  rgest.** rowid v
1e3b3 61 6c 75 65 2e 20 20 43 6f 64 65 20 67 65 6e 65  alue.  Code gene
1e3b4 72 61 74 65 64 20 62 79 20 61 75 74 6f 49 6e 63  rated by autoInc
1e3b5 45 6e 64 28 29 20 77 69 6c 6c 20 77 72 69 74 65  End() will write
1e3b6 20 74 68 65 20 6e 65 77 0a 2a 2a 20 6c 61 72 67   the new.** larg
1e3b7 65 73 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65  est value of the
1e3b8 20 63 6f 75 6e 74 65 72 20 62 61 63 6b 20 69 6e   counter back in
1e3b9 74 6f 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65  to the sqlite_se
1e3ba 71 75 65 6e 63 65 20 74 61 62 6c 65 2e 0a 2a 2a  quence table..**
1e3bb 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1e3bc 20 72 65 74 75 72 6e 73 20 74 68 65 20 69 6e 64   returns the ind
1e3bd 65 78 20 6f 66 20 74 68 65 20 6d 65 6d 5b 5d 20  ex of the mem[] 
1e3be 63 65 6c 6c 20 74 68 61 74 20 63 6f 6e 74 61 69  cell that contai
1e3bf 6e 73 0a 2a 2a 20 74 68 65 20 6d 61 78 69 6d 75  ns.** the maximu
1e3c0 6d 20 72 6f 77 69 64 20 63 6f 75 6e 74 65 72 2e  m rowid counter.
1e3c1 0a 2a 2a 0a 2a 2a 20 54 68 72 65 65 20 63 6f 6e  .**.** Three con
1e3c2 73 65 63 75 74 69 76 65 20 72 65 67 69 73 74 65  secutive registe
1e3c3 72 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64  rs are allocated
1e3c4 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65   by this routine
1e3c5 2e 20 20 54 68 65 0a 2a 2a 20 66 69 72 73 74 20  .  The.** first 
1e3c6 74 77 6f 20 68 6f 6c 64 20 74 68 65 20 6e 61 6d  two hold the nam
1e3c7 65 20 6f 66 20 74 68 65 20 74 61 72 67 65 74 20  e of the target 
1e3c8 74 61 62 6c 65 20 61 6e 64 20 74 68 65 20 6d 61  table and the ma
1e3c9 78 69 6d 75 6d 20 72 6f 77 69 64 20 0a 2a 2a 20  ximum rowid .** 
1e3ca 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68  inserted into th
1e3cb 65 20 74 61 72 67 65 74 20 74 61 62 6c 65 2c 20  e target table, 
1e3cc 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a  respectively..**
1e3cd 20 54 68 65 20 74 68 69 72 64 20 68 6f 6c 64 73   The third holds
1e3ce 20 74 68 65 20 72 6f 77 69 64 20 69 6e 20 73 71   the rowid in sq
1e3cf 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 77 68  lite_sequence wh
1e3d0 65 72 65 20 77 65 20 77 69 6c 6c 0a 2a 2a 20 77  ere we will.** w
1e3d1 72 69 74 65 20 62 61 63 6b 20 74 68 65 20 72 65  rite back the re
1e3d2 76 69 73 65 64 20 6d 61 78 69 6d 75 6d 20 72 6f  vised maximum ro
1e3d3 77 69 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69  wid.  This routi
1e3d4 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a  ne returns the.*
1e3d5 2a 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 73  * index of the s
1e3d6 65 63 6f 6e 64 20 6f 66 20 74 68 65 73 65 20 74  econd of these t
1e3d7 68 72 65 65 20 72 65 67 69 73 74 65 72 73 2e 0a  hree registers..
1e3d8 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 75  */.static int au
1e3d9 74 6f 49 6e 63 42 65 67 69 6e 28 0a 20 20 50 61  toIncBegin(.  Pa
1e3da 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
1e3db 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
1e3dc 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44  text */.  int iD
1e3dd 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b,            /*
1e3de 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61   Index of the da
1e3df 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 70  tabase holding p
1e3e0 54 61 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  Tab */.  Table *
1e3e1 70 54 61 62 20 20 20 20 20 20 20 20 20 2f 2a 20  pTab         /* 
1e3e2 54 68 65 20 74 61 62 6c 65 20 77 65 20 61 72 65  The table we are
1e3e3 20 77 72 69 74 69 6e 67 20 74 6f 20 2a 2f 0a 29   writing to */.)
1e3e4 7b 0a 20 20 69 6e 74 20 6d 65 6d 49 64 20 3d 20  {.  int memId = 
1e3e5 30 3b 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73  0;      /* Regis
1e3e6 74 65 72 20 68 6f 6c 64 69 6e 67 20 6d 61 78 69  ter holding maxi
1e3e7 6d 75 6d 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69  mum rowid */.  i
1e3e8 66 28 20 70 54 61 62 2d 3e 61 75 74 6f 49 6e 63  f( pTab->autoInc
1e3e9 20 29 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 20   ){.    Vdbe *v 
1e3ea 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
1e3eb 0a 20 20 20 20 44 62 20 2a 70 44 62 20 3d 20 26  .    Db *pDb = &
1e3ec 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b  pParse->db->aDb[
1e3ed 69 44 62 5d 3b 0a 20 20 20 20 69 6e 74 20 69 43  iDb];.    int iC
1e3ee 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  ur = pParse->nTa
1e3ef 62 3b 0a 20 20 20 20 69 6e 74 20 61 64 64 72 3b  b;.    int addr;
1e3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1e3f1 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65  * Address of the
1e3f2 20 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70   top of the loop
1e3f3 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
1e3f4 76 20 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  v );.    pParse-
1e3f5 3e 6e 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20  >nMem++;        
1e3f6 20 2f 2a 20 48 6f 6c 64 73 20 6e 61 6d 65 20 6f   /* Holds name o
1e3f7 66 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 6d  f table */.    m
1e3f8 65 6d 49 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d  emId = ++pParse-
1e3f9 3e 6e 4d 65 6d 3b 0a 20 20 20 20 70 50 61 72 73  >nMem;.    pPars
1e3fa 65 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20 20 20 73  e->nMem++;.    s
1e3fb 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28  qlite3OpenTable(
1e3fc 70 50 61 72 73 65 2c 20 69 43 75 72 2c 20 69 44  pParse, iCur, iD
1e3fd 62 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  b, pDb->pSchema-
1e3fe 3e 70 53 65 71 54 61 62 2c 20 4f 50 5f 4f 70 65  >pSeqTab, OP_Ope
1e3ff 6e 52 65 61 64 29 3b 0a 20 20 20 20 61 64 64 72  nRead);.    addr
1e400 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
1e401 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20  rrentAddr(v);.  
1e402 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1e403 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
1e404 38 2c 20 30 2c 20 6d 65 6d 49 64 2d 31 2c 20 30  8, 0, memId-1, 0
1e405 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30  , pTab->zName, 0
1e406 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1e407 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
1e408 65 77 69 6e 64 2c 20 69 43 75 72 2c 20 61 64 64  ewind, iCur, add
1e409 72 2b 38 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  r+8);.    sqlite
1e40a 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
1e40b 50 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20  P_Column, iCur, 
1e40c 30 2c 20 6d 65 6d 49 64 29 3b 0a 20 20 20 20 73  0, memId);.    s
1e40d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
1e40e 28 76 2c 20 4f 50 5f 4e 65 2c 20 6d 65 6d 49 64  (v, OP_Ne, memId
1e40f 2d 31 2c 20 61 64 64 72 2b 37 2c 20 6d 65 6d 49  -1, addr+7, memI
1e410 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  d);.    sqlite3V
1e411 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 53  dbeChangeP5(v, S
1e412 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
1e413 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1e414 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
1e415 6f 77 69 64 2c 20 69 43 75 72 2c 20 6d 65 6d 49  owid, iCur, memI
1e416 64 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  d+1);.    sqlite
1e417 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
1e418 50 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20  P_Column, iCur, 
1e419 31 2c 20 6d 65 6d 49 64 29 3b 0a 20 20 20 20 73  1, memId);.    s
1e41a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1e41b 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
1e41c 61 64 64 72 2b 38 29 3b 0a 20 20 20 20 73 71 6c  addr+8);.    sql
1e41d 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1e41e 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 43 75 72 2c  , OP_Next, iCur,
1e41f 20 61 64 64 72 2b 32 29 3b 0a 20 20 20 20 73 71   addr+2);.    sq
1e420 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1e421 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75  v, OP_Close, iCu
1e422 72 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74  r, 0);.  }.  ret
1e423 75 72 6e 20 6d 65 6d 49 64 3b 0a 7d 0a 0a 2f 2a  urn memId;.}../*
1e424 0a 2a 2a 20 55 70 64 61 74 65 20 74 68 65 20 6d  .** Update the m
1e425 61 78 69 6d 75 6d 20 72 6f 77 69 64 20 66 6f 72  aximum rowid for
1e426 20 61 6e 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e   an autoincremen
1e427 74 20 63 61 6c 63 75 6c 61 74 69 6f 6e 2e 0a 2a  t calculation..*
1e428 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1e429 65 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c  e should be call
1e42a 65 64 20 77 68 65 6e 20 74 68 65 20 74 6f 70 20  ed when the top 
1e42b 6f 66 20 74 68 65 20 73 74 61 63 6b 20 68 6f 6c  of the stack hol
1e42c 64 73 20 61 0a 2a 2a 20 6e 65 77 20 72 6f 77 69  ds a.** new rowi
1e42d 64 20 74 68 61 74 20 69 73 20 61 62 6f 75 74 20  d that is about 
1e42e 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 2e 20  to be inserted. 
1e42f 20 49 66 20 74 68 61 74 20 6e 65 77 20 72 6f 77   If that new row
1e430 69 64 20 69 73 0a 2a 2a 20 6c 61 72 67 65 72 20  id is.** larger 
1e431 74 68 61 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d  than the maximum
1e432 20 72 6f 77 69 64 20 69 6e 20 74 68 65 20 6d 65   rowid in the me
1e433 6d 49 64 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2c  mId memory cell,
1e434 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6d 65 6d   then the.** mem
1e435 6f 72 79 20 63 65 6c 6c 20 69 73 20 75 70 64 61  ory cell is upda
1e436 74 65 64 2e 20 20 54 68 65 20 73 74 61 63 6b 20  ted.  The stack 
1e437 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f  is unchanged..*/
1e438 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 75 74  .static void aut
1e439 6f 49 6e 63 53 74 65 70 28 50 61 72 73 65 20 2a  oIncStep(Parse *
1e43a 70 50 61 72 73 65 2c 20 69 6e 74 20 6d 65 6d 49  pParse, int memI
1e43b 64 2c 20 69 6e 74 20 72 65 67 52 6f 77 69 64 29  d, int regRowid)
1e43c 7b 0a 20 20 69 66 28 20 6d 65 6d 49 64 3e 30 20  {.  if( memId>0 
1e43d 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1e43e 62 65 41 64 64 4f 70 32 28 70 50 61 72 73 65 2d  beAddOp2(pParse-
1e43f 3e 70 56 64 62 65 2c 20 4f 50 5f 4d 65 6d 4d 61  >pVdbe, OP_MemMa
1e440 78 2c 20 6d 65 6d 49 64 2c 20 72 65 67 52 6f 77  x, memId, regRow
1e441 69 64 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  id);.  }.}../*.*
1e442 2a 20 41 66 74 65 72 20 64 6f 69 6e 67 20 6f 6e  * After doing on
1e443 65 20 6f 72 20 6d 6f 72 65 20 69 6e 73 65 72 74  e or more insert
1e444 73 2c 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 72  s, the maximum r
1e445 6f 77 69 64 20 69 73 20 73 74 6f 72 65 64 0a 2a  owid is stored.*
1e446 2a 20 69 6e 20 72 65 67 5b 6d 65 6d 49 64 5d 2e  * in reg[memId].
1e447 20 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20    Generate code 
1e448 74 6f 20 77 72 69 74 65 20 74 68 69 73 20 76 61  to write this va
1e449 6c 75 65 20 62 61 63 6b 20 69 6e 74 6f 20 74 68  lue back into th
1e44a 65 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 5f  e.** the sqlite_
1e44b 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 2e 0a  sequence table..
1e44c 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
1e44d 75 74 6f 49 6e 63 45 6e 64 28 0a 20 20 50 61 72  utoIncEnd(.  Par
1e44e 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
1e44f 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63  /* The parsing c
1e450 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20  ontext */.  int 
1e451 69 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 2f  iDb,           /
1e452 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64  * Index of the d
1e453 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20  atabase holding 
1e454 70 54 61 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20  pTab */.  Table 
1e455 2a 70 54 61 62 2c 20 20 20 20 20 20 20 2f 2a 20  *pTab,       /* 
1e456 54 61 62 6c 65 20 77 65 20 61 72 65 20 69 6e 73  Table we are ins
1e457 65 72 74 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a 20  erting into */. 
1e458 20 69 6e 74 20 6d 65 6d 49 64 20 20 20 20 20 20   int memId      
1e459 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65      /* Memory ce
1e45a 6c 6c 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 6d  ll holding the m
1e45b 61 78 69 6d 75 6d 20 72 6f 77 69 64 20 2a 2f 0a  aximum rowid */.
1e45c 29 7b 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 61  ){.  if( pTab->a
1e45d 75 74 6f 49 6e 63 20 29 7b 0a 20 20 20 20 69 6e  utoInc ){.    in
1e45e 74 20 69 43 75 72 20 3d 20 70 50 61 72 73 65 2d  t iCur = pParse-
1e45f 3e 6e 54 61 62 3b 0a 20 20 20 20 56 64 62 65 20  >nTab;.    Vdbe 
1e460 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
1e461 62 65 3b 0a 20 20 20 20 44 62 20 2a 70 44 62 20  be;.    Db *pDb 
1e462 3d 20 26 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61  = &pParse->db->a
1e463 44 62 5b 69 44 62 5d 3b 0a 20 20 20 20 69 6e 74  Db[iDb];.    int
1e464 20 6a 31 3b 0a 20 20 20 20 69 6e 74 20 69 52 65   j1;.    int iRe
1e465 63 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  c = ++pParse->nM
1e466 65 6d 3b 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79  em;    /* Memory
1e467 20 63 65 6c 6c 20 75 73 65 64 20 66 6f 72 20 72   cell used for r
1e468 65 63 6f 72 64 20 2a 2f 0a 0a 20 20 20 20 61 73  ecord */..    as
1e469 73 65 72 74 28 20 76 20 29 3b 0a 20 20 20 20 73  sert( v );.    s
1e46a 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28  qlite3OpenTable(
1e46b 70 50 61 72 73 65 2c 20 69 43 75 72 2c 20 69 44  pParse, iCur, iD
1e46c 62 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  b, pDb->pSchema-
1e46d 3e 70 53 65 71 54 61 62 2c 20 4f 50 5f 4f 70 65  >pSeqTab, OP_Ope
1e46e 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 6a 31 20  nWrite);.    j1 
1e46f 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
1e470 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c  Op1(v, OP_NotNul
1e471 6c 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a 20 20 20  l, memId+1);.   
1e472 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1e473 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69  p2(v, OP_NewRowi
1e474 64 2c 20 69 43 75 72 2c 20 6d 65 6d 49 64 2b 31  d, iCur, memId+1
1e475 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1e476 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31  beJumpHere(v, j1
1e477 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1e478 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
1e479 61 6b 65 52 65 63 6f 72 64 2c 20 6d 65 6d 49 64  akeRecord, memId
1e47a 2d 31 2c 20 32 2c 20 69 52 65 63 29 3b 0a 20 20  -1, 2, iRec);.  
1e47b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1e47c 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74  Op3(v, OP_Insert
1e47d 2c 20 69 43 75 72 2c 20 69 52 65 63 2c 20 6d 65  , iCur, iRec, me
1e47e 6d 49 64 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69  mId+1);.    sqli
1e47f 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
1e480 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44  v, OPFLAG_APPEND
1e481 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1e482 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43  beAddOp1(v, OP_C
1e483 6c 6f 73 65 2c 20 69 43 75 72 29 3b 0a 20 20 7d  lose, iCur);.  }
1e484 0a 7d 0a 23 65 6c 73 65 0a 2f 2a 0a 2a 2a 20 49  .}.#else./*.** I
1e485 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
1e486 54 4f 49 4e 43 52 45 4d 45 4e 54 20 69 73 20 64  TOINCREMENT is d
1e487 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 65  efined, then the
1e488 20 74 68 72 65 65 20 72 6f 75 74 69 6e 65 73 0a   three routines.
1e489 2a 2a 20 61 62 6f 76 65 20 61 72 65 20 61 6c 6c  ** above are all
1e48a 20 6e 6f 2d 6f 70 73 0a 2a 2f 0a 23 20 64 65 66   no-ops.*/.# def
1e48b 69 6e 65 20 61 75 74 6f 49 6e 63 42 65 67 69 6e  ine autoIncBegin
1e48c 28 41 2c 42 2c 43 29 20 28 30 29 0a 23 20 64 65  (A,B,C) (0).# de
1e48d 66 69 6e 65 20 61 75 74 6f 49 6e 63 53 74 65 70  fine autoIncStep
1e48e 28 41 2c 42 2c 43 29 0a 23 20 64 65 66 69 6e 65  (A,B,C).# define
1e48f 20 61 75 74 6f 49 6e 63 45 6e 64 28 41 2c 42 2c   autoIncEnd(A,B,
1e490 43 2c 44 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53  C,D).#endif /* S
1e491 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
1e492 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 0a 0a 2f 2a  NCREMENT */.../*
1e493 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61   Forward declara
1e494 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69  tion */.static i
1e495 6e 74 20 78 66 65 72 4f 70 74 69 6d 69 7a 61 74  nt xferOptimizat
1e496 69 6f 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ion(.  Parse *pP
1e497 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
1e498 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  Parser context *
1e499 2f 0a 20 20 54 61 62 6c 65 20 2a 70 44 65 73 74  /.  Table *pDest
1e49a 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
1e49b 20 74 61 62 6c 65 20 77 65 20 61 72 65 20 69 6e   table we are in
1e49c 73 65 72 74 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a  serting into */.
1e49d 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
1e49e 74 2c 20 20 20 20 20 20 2f 2a 20 41 20 53 45 4c  t,      /* A SEL
1e49f 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  ECT statement to
1e4a0 20 75 73 65 20 61 73 20 74 68 65 20 64 61 74 61   use as the data
1e4a1 20 73 6f 75 72 63 65 20 2a 2f 0a 20 20 69 6e 74   source */.  int
1e4a2 20 6f 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 20   onError,       
1e4a3 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e     /* How to han
1e4a4 64 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 65  dle constraint e
1e4a5 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69  rrors */.  int i
1e4a6 44 62 44 65 73 74 20 20 20 20 20 20 20 20 20 20  DbDest          
1e4a7 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
1e4a8 20 6f 66 20 70 44 65 73 74 20 2a 2f 0a 29 3b 0a   of pDest */.);.
1e4a9 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
1e4aa 69 6e 65 20 69 73 20 63 61 6c 6c 20 74 6f 20 68  ine is call to h
1e4ab 61 6e 64 6c 65 20 53 51 4c 20 6f 66 20 74 68 65  andle SQL of the
1e4ac 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 73   following forms
1e4ad 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 69 6e 73 65 72  :.**.**    inser
1e4ae 74 20 69 6e 74 6f 20 54 41 42 4c 45 20 28 49 44  t into TABLE (ID
1e4af 4c 49 53 54 29 20 76 61 6c 75 65 73 28 45 58 50  LIST) values(EXP
1e4b0 52 4c 49 53 54 29 0a 2a 2a 20 20 20 20 69 6e 73  RLIST).**    ins
1e4b1 65 72 74 20 69 6e 74 6f 20 54 41 42 4c 45 20 28  ert into TABLE (
1e4b2 49 44 4c 49 53 54 29 20 73 65 6c 65 63 74 0a 2a  IDLIST) select.*
1e4b3 2a 0a 2a 2a 20 54 68 65 20 49 44 4c 49 53 54 20  *.** The IDLIST 
1e4b4 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 74 61  following the ta
1e4b5 62 6c 65 20 6e 61 6d 65 20 69 73 20 61 6c 77 61  ble name is alwa
1e4b6 79 73 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 49 66  ys optional.  If
1e4b7 20 6f 6d 69 74 74 65 64 2c 0a 2a 2a 20 74 68 65   omitted,.** the
1e4b8 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  n a list of all 
1e4b9 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 74 68 65 20  columns for the 
1e4ba 74 61 62 6c 65 20 69 73 20 73 75 62 73 74 69 74  table is substit
1e4bb 75 74 65 64 2e 20 20 54 68 65 20 49 44 4c 49 53  uted.  The IDLIS
1e4bc 54 0a 2a 2a 20 61 70 70 65 61 72 73 20 69 6e 20  T.** appears in 
1e4bd 74 68 65 20 70 43 6f 6c 75 6d 6e 20 70 61 72 61  the pColumn para
1e4be 6d 65 74 65 72 2e 20 20 70 43 6f 6c 75 6d 6e 20  meter.  pColumn 
1e4bf 69 73 20 4e 55 4c 4c 20 69 66 20 49 44 4c 49 53  is NULL if IDLIS
1e4c0 54 20 69 73 20 6f 6d 69 74 74 65 64 2e 0a 2a 2a  T is omitted..**
1e4c1 0a 2a 2a 20 54 68 65 20 70 4c 69 73 74 20 70 61  .** The pList pa
1e4c2 72 61 6d 65 74 65 72 20 68 6f 6c 64 73 20 45 58  rameter holds EX
1e4c3 50 52 4c 49 53 54 20 69 6e 20 74 68 65 20 66 69  PRLIST in the fi
1e4c4 72 73 74 20 66 6f 72 6d 20 6f 66 20 74 68 65 20  rst form of the 
1e4c5 49 4e 53 45 52 54 0a 2a 2a 20 73 74 61 74 65 6d  INSERT.** statem
1e4c6 65 6e 74 20 61 62 6f 76 65 2c 20 61 6e 64 20 70  ent above, and p
1e4c7 53 65 6c 65 63 74 20 69 73 20 4e 55 4c 4c 2e 20  Select is NULL. 
1e4c8 20 46 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20   For the second 
1e4c9 66 6f 72 6d 2c 20 70 4c 69 73 74 20 69 73 0a 2a  form, pList is.*
1e4ca 2a 20 4e 55 4c 4c 20 61 6e 64 20 70 53 65 6c 65  * NULL and pSele
1e4cb 63 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  ct is a pointer 
1e4cc 74 6f 20 74 68 65 20 73 65 6c 65 63 74 20 73 74  to the select st
1e4cd 61 74 65 6d 65 6e 74 20 75 73 65 64 20 74 6f 20  atement used to 
1e4ce 67 65 6e 65 72 61 74 65 0a 2a 2a 20 64 61 74 61  generate.** data
1e4cf 20 66 6f 72 20 74 68 65 20 69 6e 73 65 72 74 2e   for the insert.
1e4d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  .**.** The code 
1e4d1 67 65 6e 65 72 61 74 65 64 20 66 6f 6c 6c 6f 77  generated follow
1e4d2 73 20 6f 6e 65 20 6f 66 20 66 6f 75 72 20 74 65  s one of four te
1e4d3 6d 70 6c 61 74 65 73 2e 20 20 46 6f 72 20 61 20  mplates.  For a 
1e4d4 73 69 6d 70 6c 65 0a 2a 2a 20 73 65 6c 65 63 74  simple.** select
1e4d5 20 77 69 74 68 20 64 61 74 61 20 63 6f 6d 69 6e   with data comin
1e4d6 67 20 66 72 6f 6d 20 61 20 56 41 4c 55 45 53 20  g from a VALUES 
1e4d7 63 6c 61 75 73 65 2c 20 74 68 65 20 63 6f 64 65  clause, the code
1e4d8 20 65 78 65 63 75 74 65 73 0a 2a 2a 20 6f 6e 63   executes.** onc
1e4d9 65 20 73 74 72 61 69 67 68 74 20 64 6f 77 6e 20  e straight down 
1e4da 74 68 72 6f 75 67 68 2e 20 20 54 68 65 20 74 65  through.  The te
1e4db 6d 70 6c 61 74 65 20 6c 6f 6f 6b 73 20 6c 69 6b  mplate looks lik
1e4dc 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
1e4dd 20 20 20 20 20 20 6f 70 65 6e 20 77 72 69 74 65        open write
1e4de 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c   cursor to <tabl
1e4df 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64 69 63  e> and its indic
1e4e0 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 70 75  es.**         pu
1e4e1 74 73 20 56 41 4c 55 45 53 20 63 6c 61 75 73 65  ts VALUES clause
1e4e2 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e 74   expressions ont
1e4e3 6f 20 74 68 65 20 73 74 61 63 6b 0a 2a 2a 20 20  o the stack.**  
1e4e4 20 20 20 20 20 20 20 77 72 69 74 65 20 74 68 65         write the
1e4e5 20 72 65 73 75 6c 74 69 6e 67 20 72 65 63 6f 72   resulting recor
1e4e6 64 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a  d into <table>.*
1e4e7 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75  *         cleanu
1e4e8 70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 72 65  p.**.** The thre
1e4e9 65 20 72 65 6d 61 69 6e 69 6e 67 20 74 65 6d 70  e remaining temp
1e4ea 6c 61 74 65 73 20 61 73 73 75 6d 65 20 74 68 65  lates assume the
1e4eb 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6f 66   statement is of
1e4ec 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20   the form.**.** 
1e4ed 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 3c 74    INSERT INTO <t
1e4ee 61 62 6c 65 3e 20 53 45 4c 45 43 54 20 2e 2e 2e  able> SELECT ...
1e4ef 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 45  .**.** If the SE
1e4f0 4c 45 43 54 20 63 6c 61 75 73 65 20 69 73 20 6f  LECT clause is o
1e4f1 66 20 74 68 65 20 72 65 73 74 72 69 63 74 65 64  f the restricted
1e4f2 20 66 6f 72 6d 20 22 53 45 4c 45 43 54 20 2a 20   form "SELECT * 
1e4f3 46 52 4f 4d 20 3c 74 61 62 6c 65 32 3e 22 20 2d  FROM <table2>" -
1e4f4 0a 2a 2a 20 69 6e 20 6f 74 68 65 72 20 77 6f 72  .** in other wor
1e4f5 64 73 20 69 66 20 74 68 65 20 53 45 4c 45 43 54  ds if the SELECT
1e4f6 20 70 75 6c 6c 73 20 61 6c 6c 20 63 6f 6c 75 6d   pulls all colum
1e4f7 6e 73 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65  ns from a single
1e4f8 20 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68   table.** and th
1e4f9 65 72 65 20 69 73 20 6e 6f 20 57 48 45 52 45 20  ere is no WHERE 
1e4fa 6f 72 20 4c 49 4d 49 54 20 6f 72 20 47 52 4f 55  or LIMIT or GROU
1e4fb 50 20 42 59 20 6f 72 20 4f 52 44 45 52 20 42 59  P BY or ORDER BY
1e4fc 20 63 6c 61 75 73 65 73 2c 20 61 6e 64 0a 2a 2a   clauses, and.**
1e4fd 20 69 66 20 3c 74 61 62 6c 65 32 3e 20 61 6e 64   if <table2> and
1e4fe 20 3c 74 61 62 6c 65 31 3e 20 61 72 65 20 64 69   <table1> are di
1e4ff 73 74 69 6e 63 74 20 74 61 62 6c 65 73 20 62 75  stinct tables bu
1e500 74 20 68 61 76 65 20 69 64 65 6e 74 69 63 61 6c  t have identical
1e501 0a 2a 2a 20 73 63 68 65 6d 61 73 2c 20 69 6e 63  .** schemas, inc
1e502 6c 75 64 69 6e 67 20 61 6c 6c 20 74 68 65 20 73  luding all the s
1e503 61 6d 65 20 69 6e 64 69 63 65 73 2c 20 74 68 65  ame indices, the
1e504 6e 20 61 20 73 70 65 63 69 61 6c 20 6f 70 74 69  n a special opti
1e505 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 69  mization.** is i
1e506 6e 76 6f 6b 65 64 20 74 68 61 74 20 63 6f 70 69  nvoked that copi
1e507 65 73 20 72 61 77 20 72 65 63 6f 72 64 73 20 66  es raw records f
1e508 72 6f 6d 20 3c 74 61 62 6c 65 32 3e 20 6f 76 65  rom <table2> ove
1e509 72 20 74 6f 20 3c 74 61 62 6c 65 31 3e 2e 0a 2a  r to <table1>..*
1e50a 2a 20 53 65 65 20 74 68 65 20 78 66 65 72 4f 70  * See the xferOp
1e50b 74 69 6d 69 7a 61 74 69 6f 6e 28 29 20 66 75 6e  timization() fun
1e50c 63 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 69 6d  ction for the im
1e50d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
1e50e 74 68 69 73 0a 2a 2a 20 74 65 6d 70 6c 61 74 65  this.** template
1e50f 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 73  .  This is the s
1e510 65 63 6f 6e 64 20 74 65 6d 70 6c 61 74 65 2e 0a  econd template..
1e511 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 70  **.**         op
1e512 65 6e 20 61 20 77 72 69 74 65 20 63 75 72 73 6f  en a write curso
1e513 72 20 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20  r to <table>.** 
1e514 20 20 20 20 20 20 20 20 6f 70 65 6e 20 72 65 61          open rea
1e515 64 20 63 75 72 73 6f 72 20 6f 6e 20 3c 74 61 62  d cursor on <tab
1e516 6c 65 32 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20  le2>.**         
1e517 74 72 61 6e 73 66 65 72 20 61 6c 6c 20 72 65 63  transfer all rec
1e518 6f 72 64 73 20 69 6e 20 3c 74 61 62 6c 65 32 3e  ords in <table2>
1e519 20 6f 76 65 72 20 74 6f 20 3c 74 61 62 6c 65 3e   over to <table>
1e51a 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 6c 6f 73  .**         clos
1e51b 65 20 63 75 72 73 6f 72 73 0a 2a 2a 20 20 20 20  e cursors.**    
1e51c 20 20 20 20 20 66 6f 72 65 61 63 68 20 69 6e 64       foreach ind
1e51d 65 78 20 6f 6e 20 3c 74 61 62 6c 65 3e 0a 2a 2a  ex on <table>.**
1e51e 20 20 20 20 20 20 20 20 20 20 20 6f 70 65 6e 20             open 
1e51f 61 20 77 72 69 74 65 20 63 75 72 73 6f 72 20 6f  a write cursor o
1e520 6e 20 74 68 65 20 3c 74 61 62 6c 65 3e 20 69 6e  n the <table> in
1e521 64 65 78 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  dex.**          
1e522 20 6f 70 65 6e 20 61 20 72 65 61 64 20 63 75 72   open a read cur
1e523 73 6f 72 20 6f 6e 20 74 68 65 20 63 6f 72 72 65  sor on the corre
1e524 73 70 6f 6e 64 69 6e 67 20 3c 74 61 62 6c 65 32  sponding <table2
1e525 3e 20 69 6e 64 65 78 0a 2a 2a 20 20 20 20 20 20  > index.**      
1e526 20 20 20 20 20 74 72 61 6e 73 66 65 72 20 61 6c       transfer al
1e527 6c 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74  l records from t
1e528 68 65 20 72 65 61 64 20 74 6f 20 74 68 65 20 77  he read to the w
1e529 72 69 74 65 20 63 75 72 73 6f 72 73 0a 2a 2a 20  rite cursors.** 
1e52a 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 20            close 
1e52b 63 75 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20 20  cursors.**      
1e52c 20 20 20 65 6e 64 20 66 6f 72 65 61 63 68 0a 2a     end foreach.*
1e52d 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 74  *.** The third t
1e52e 65 6d 70 6c 61 74 65 20 69 73 20 66 6f 72 20 77  emplate is for w
1e52f 68 65 6e 20 74 68 65 20 73 65 63 6f 6e 64 20 74  hen the second t
1e530 65 6d 70 6c 61 74 65 20 64 6f 65 73 20 6e 6f 74  emplate does not
1e531 20 61 70 70 6c 79 0a 2a 2a 20 61 6e 64 20 74 68   apply.** and th
1e532 65 20 53 45 4c 45 43 54 20 63 6c 61 75 73 65 20  e SELECT clause 
1e533 64 6f 65 73 20 6e 6f 74 20 72 65 61 64 20 66 72  does not read fr
1e534 6f 6d 20 3c 74 61 62 6c 65 3e 20 61 74 20 61 6e  om <table> at an
1e535 79 20 74 69 6d 65 2e 0a 2a 2a 20 54 68 65 20 67  y time..** The g
1e536 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20 66 6f  enerated code fo
1e537 6c 6c 6f 77 73 20 74 68 69 73 20 74 65 6d 70 6c  llows this templ
1e538 61 74 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  ate:.**.**      
1e539 20 20 20 67 6f 74 6f 20 42 0a 2a 2a 20 20 20 20     goto B.**    
1e53a 20 20 41 3a 20 73 65 74 75 70 20 66 6f 72 20 74    A: setup for t
1e53b 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20  he SELECT.**    
1e53c 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72 20 74       loop over t
1e53d 68 65 20 72 6f 77 73 20 69 6e 20 74 68 65 20 53  he rows in the S
1e53e 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20  ELECT.**        
1e53f 20 20 20 67 6f 73 75 62 20 43 0a 2a 2a 20 20 20     gosub C.**   
1e540 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 2a        end loop.*
1e541 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75  *         cleanu
1e542 70 20 61 66 74 65 72 20 74 68 65 20 53 45 4c 45  p after the SELE
1e543 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 67 6f  CT.**         go
1e544 74 6f 20 44 0a 2a 2a 20 20 20 20 20 20 42 3a 20  to D.**      B: 
1e545 6f 70 65 6e 20 77 72 69 74 65 20 63 75 72 73 6f  open write curso
1e546 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20 61 6e 64  r to <table> and
1e547 20 69 74 73 20 69 6e 64 69 63 65 73 0a 2a 2a 20   its indices.** 
1e548 20 20 20 20 20 20 20 20 67 6f 74 6f 20 41 0a 2a          goto A.*
1e549 2a 20 20 20 20 20 20 43 3a 20 69 6e 73 65 72 74  *      C: insert
1e54a 20 74 68 65 20 73 65 6c 65 63 74 20 72 65 73 75   the select resu
1e54b 6c 74 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a  lt into <table>.
1e54c 2a 2a 20 20 20 20 20 20 20 20 20 72 65 74 75 72  **         retur
1e54d 6e 0a 2a 2a 20 20 20 20 20 20 44 3a 20 63 6c 65  n.**      D: cle
1e54e 61 6e 75 70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  anup.**.** The f
1e54f 6f 75 72 74 68 20 74 65 6d 70 6c 61 74 65 20 69  ourth template i
1e550 73 20 75 73 65 64 20 69 66 20 74 68 65 20 69 6e  s used if the in
1e551 73 65 72 74 20 73 74 61 74 65 6d 65 6e 74 20 74  sert statement t
1e552 61 6b 65 73 20 69 74 73 0a 2a 2a 20 76 61 6c 75  akes its.** valu
1e553 65 73 20 66 72 6f 6d 20 61 20 53 45 4c 45 43 54  es from a SELECT
1e554 20 62 75 74 20 74 68 65 20 64 61 74 61 20 69 73   but the data is
1e555 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20   being inserted 
1e556 69 6e 74 6f 20 61 20 74 61 62 6c 65 0a 2a 2a 20  into a table.** 
1e557 74 68 61 74 20 69 73 20 61 6c 73 6f 20 72 65 61  that is also rea
1e558 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65  d as part of the
1e559 20 53 45 4c 45 43 54 2e 20 20 49 6e 20 74 68 65   SELECT.  In the
1e55a 20 74 68 69 72 64 20 66 6f 72 6d 2c 0a 2a 2a 20   third form,.** 
1e55b 77 65 20 68 61 76 65 20 74 6f 20 75 73 65 20 61  we have to use a
1e55c 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61   intermediate ta
1e55d 62 6c 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65  ble to store the
1e55e 20 72 65 73 75 6c 74 73 20 6f 66 0a 2a 2a 20 74   results of.** t
1e55f 68 65 20 73 65 6c 65 63 74 2e 20 20 54 68 65 20  he select.  The 
1e560 74 65 6d 70 6c 61 74 65 20 69 73 20 6c 69 6b 65  template is like
1e561 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   this:.**.**    
1e562 20 20 20 20 20 67 6f 74 6f 20 42 0a 2a 2a 20 20       goto B.**  
1e563 20 20 20 20 41 3a 20 73 65 74 75 70 20 66 6f 72      A: setup for
1e564 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20   the SELECT.**  
1e565 20 20 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72         loop over
1e566 20 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20 74   the tables in t
1e567 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20  he SELECT.**    
1e568 20 20 20 20 20 20 20 67 6f 73 75 62 20 43 0a 2a         gosub C.*
1e569 2a 20 20 20 20 20 20 20 20 20 65 6e 64 20 6c 6f  *         end lo
1e56a 6f 70 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 6c  op.**         cl
1e56b 65 61 6e 75 70 20 61 66 74 65 72 20 74 68 65 20  eanup after the 
1e56c 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20  SELECT.**       
1e56d 20 20 67 6f 74 6f 20 44 0a 2a 2a 20 20 20 20 20    goto D.**     
1e56e 20 43 3a 20 69 6e 73 65 72 74 20 74 68 65 20 73   C: insert the s
1e56f 65 6c 65 63 74 20 72 65 73 75 6c 74 20 69 6e 74  elect result int
1e570 6f 20 74 68 65 20 69 6e 74 65 72 6d 65 64 69 61  o the intermedia
1e571 74 65 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20  te table.**     
1e572 20 20 20 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20      return.**   
1e573 20 20 20 42 3a 20 6f 70 65 6e 20 61 20 63 75 72     B: open a cur
1e574 73 6f 72 20 74 6f 20 61 6e 20 69 6e 74 65 72 6d  sor to an interm
1e575 65 64 69 61 74 65 20 74 61 62 6c 65 0a 2a 2a 20  ediate table.** 
1e576 20 20 20 20 20 20 20 20 67 6f 74 6f 20 41 0a 2a          goto A.*
1e577 2a 20 20 20 20 20 20 44 3a 20 6f 70 65 6e 20 77  *      D: open w
1e578 72 69 74 65 20 63 75 72 73 6f 72 20 74 6f 20 3c  rite cursor to <
1e579 74 61 62 6c 65 3e 20 61 6e 64 20 69 74 73 20 69  table> and its i
1e57a 6e 64 69 63 65 73 0a 2a 2a 20 20 20 20 20 20 20  ndices.**       
1e57b 20 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20    loop over the 
1e57c 69 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61 62  intermediate tab
1e57d 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  le.**           
1e57e 74 72 61 6e 73 66 65 72 20 76 61 6c 75 65 73 20  transfer values 
1e57f 66 6f 72 6d 20 69 6e 74 65 72 6d 65 64 69 61 74  form intermediat
1e580 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 3c 74 61  e table into <ta
1e581 62 6c 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20  ble>.**         
1e582 65 6e 64 20 74 68 65 20 6c 6f 6f 70 0a 2a 2a 20  end the loop.** 
1e583 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75 70 0a          cleanup.
1e584 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1e585 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e  E void sqlite3In
1e586 73 65 72 74 28 0a 20 20 50 61 72 73 65 20 2a 70  sert(.  Parse *p
1e587 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a  Parse,        /*
1e588 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   Parser context 
1e589 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54  */.  SrcList *pT
1e58a 61 62 4c 69 73 74 2c 20 20 20 20 2f 2a 20 4e 61  abList,    /* Na
1e58b 6d 65 20 6f 66 20 74 61 62 6c 65 20 69 6e 74 6f  me of table into
1e58c 20 77 68 69 63 68 20 77 65 20 61 72 65 20 69 6e   which we are in
1e58d 73 65 72 74 69 6e 67 20 2a 2f 0a 20 20 45 78 70  serting */.  Exp
1e58e 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20  rList *pList,   
1e58f 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 76 61     /* List of va
1e590 6c 75 65 73 20 74 6f 20 62 65 20 69 6e 73 65 72  lues to be inser
1e591 74 65 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ted */.  Select 
1e592 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20 2f  *pSelect,      /
1e593 2a 20 41 20 53 45 4c 45 43 54 20 73 74 61 74 65  * A SELECT state
1e594 6d 65 6e 74 20 74 6f 20 75 73 65 20 61 73 20 74  ment to use as t
1e595 68 65 20 64 61 74 61 20 73 6f 75 72 63 65 20 2a  he data source *
1e596 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c  /.  IdList *pCol
1e597 75 6d 6e 2c 20 20 20 20 20 20 2f 2a 20 43 6f 6c  umn,      /* Col
1e598 75 6d 6e 20 6e 61 6d 65 73 20 63 6f 72 72 65 73  umn names corres
1e599 70 6f 6e 64 69 6e 67 20 74 6f 20 49 44 4c 49 53  ponding to IDLIS
1e59a 54 2e 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72  T. */.  int onEr
1e59b 72 6f 72 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ror           /*
1e59c 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c 65 20 63   How to handle c
1e59d 6f 6e 73 74 72 61 69 6e 74 20 65 72 72 6f 72 73  onstraint errors
1e59e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
1e59f 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 2f   *db;          /
1e5a0 2a 20 54 68 65 20 6d 61 69 6e 20 64 61 74 61 62  * The main datab
1e5a1 61 73 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ase structure */
1e5a2 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20  .  Table *pTab; 
1e5a3 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1e5a4 74 61 62 6c 65 20 74 6f 20 69 6e 73 65 72 74 20  table to insert 
1e5a5 69 6e 74 6f 2e 20 20 61 6b 61 20 54 41 42 4c 45  into.  aka TABLE
1e5a6 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 61 62   */.  char *zTab
1e5a7 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
1e5a8 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
1e5a9 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20 61   into which we a
1e5aa 72 65 20 69 6e 73 65 72 74 69 6e 67 20 2a 2f 0a  re inserting */.
1e5ab 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
1e5ac 62 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  b;      /* Name 
1e5ad 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
1e5ae 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 74 61 62  holding this tab
1e5af 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a  le */.  int i, j
1e5b0 2c 20 69 64 78 3b 20 20 20 20 20 20 20 20 2f 2a  , idx;        /*
1e5b1 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a   Loop counters *
1e5b2 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20  /.  Vdbe *v;    
1e5b3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e            /* Gen
1e5b4 65 72 61 74 65 20 63 6f 64 65 20 69 6e 74 6f 20  erate code into 
1e5b5 74 68 69 73 20 76 69 72 74 75 61 6c 20 6d 61 63  this virtual mac
1e5b6 68 69 6e 65 20 2a 2f 0a 20 20 49 6e 64 65 78 20  hine */.  Index 
1e5b7 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20 20  *pIdx;          
1e5b8 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f  /* For looping o
1e5b9 76 65 72 20 69 6e 64 69 63 65 73 20 6f 66 20 74  ver indices of t
1e5ba 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  he table */.  in
1e5bb 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20  t nColumn;      
1e5bc 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1e5bd 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
1e5be 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 48  data */.  int nH
1e5bf 69 64 64 65 6e 20 3d 20 30 3b 20 20 20 20 20 20  idden = 0;      
1e5c0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 68 69 64  /* Number of hid
1e5c1 64 65 6e 20 63 6f 6c 75 6d 6e 73 20 69 66 20 54  den columns if T
1e5c2 41 42 4c 45 20 69 73 20 76 69 72 74 75 61 6c 20  ABLE is virtual 
1e5c3 2a 2f 0a 20 20 69 6e 74 20 62 61 73 65 43 75 72  */.  int baseCur
1e5c4 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 56 44   = 0;      /* VD
1e5c5 42 45 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72  BE Cursor number
1e5c6 20 66 6f 72 20 70 54 61 62 20 2a 2f 0a 20 20 69   for pTab */.  i
1e5c7 6e 74 20 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 2d  nt keyColumn = -
1e5c8 31 3b 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 74  1;   /* Column t
1e5c9 68 61 74 20 69 73 20 74 68 65 20 49 4e 54 45 47  hat is the INTEG
1e5ca 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a  ER PRIMARY KEY *
1e5cb 2f 0a 20 20 69 6e 74 20 65 6e 64 4f 66 4c 6f 6f  /.  int endOfLoo
1e5cc 70 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 62  p;        /* Lab
1e5cd 65 6c 20 66 6f 72 20 74 68 65 20 65 6e 64 20 6f  el for the end o
1e5ce 66 20 74 68 65 20 69 6e 73 65 72 74 69 6f 6e 20  f the insertion 
1e5cf 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 75 73  loop */.  int us
1e5d0 65 54 65 6d 70 54 61 62 6c 65 20 3d 20 30 3b 20  eTempTable = 0; 
1e5d1 2f 2a 20 53 74 6f 72 65 20 53 45 4c 45 43 54 20  /* Store SELECT 
1e5d2 72 65 73 75 6c 74 73 20 69 6e 20 69 6e 74 65 72  results in inter
1e5d3 6d 65 64 69 61 74 65 20 74 61 62 6c 65 20 2a 2f  mediate table */
1e5d4 0a 20 20 69 6e 74 20 73 72 63 54 61 62 20 3d 20  .  int srcTab = 
1e5d5 30 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61  0;       /* Data
1e5d6 20 63 6f 6d 65 73 20 66 72 6f 6d 20 74 68 69 73   comes from this
1e5d7 20 74 65 6d 70 6f 72 61 72 79 20 63 75 72 73 6f   temporary curso
1e5d8 72 20 69 66 20 3e 3d 30 20 2a 2f 0a 20 20 69 6e  r if >=0 */.  in
1e5d9 74 20 69 43 6f 6e 74 3d 30 2c 69 42 72 65 61 6b  t iCont=0,iBreak
1e5da 3d 30 3b 20 2f 2a 20 42 65 67 69 6e 6e 69 6e 67  =0; /* Beginning
1e5db 20 61 6e 64 20 65 6e 64 20 6f 66 20 74 68 65 20   and end of the 
1e5dc 6c 6f 6f 70 20 6f 76 65 72 20 73 72 63 54 61 62  loop over srcTab
1e5dd 20 2a 2f 0a 20 20 69 6e 74 20 69 53 65 6c 65 63   */.  int iSelec
1e5de 74 4c 6f 6f 70 20 3d 20 30 3b 20 20 2f 2a 20 41  tLoop = 0;  /* A
1e5df 64 64 72 65 73 73 20 6f 66 20 63 6f 64 65 20 74  ddress of code t
1e5e0 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  hat implements t
1e5e1 68 65 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69  he SELECT */.  i
1e5e2 6e 74 20 69 43 6c 65 61 6e 75 70 20 3d 20 30 3b  nt iCleanup = 0;
1e5e3 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
1e5e4 6f 66 20 74 68 65 20 63 6c 65 61 6e 75 70 20 63  of the cleanup c
1e5e5 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 49 6e  ode */.  int iIn
1e5e6 73 65 72 74 42 6c 6f 63 6b 20 3d 20 30 3b 20 2f  sertBlock = 0; /
1e5e7 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65  * Address of the
1e5e8 20 73 75 62 72 6f 75 74 69 6e 65 20 75 73 65 64   subroutine used
1e5e9 20 74 6f 20 69 6e 73 65 72 74 20 64 61 74 61 20   to insert data 
1e5ea 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 49 64 78 20  */.  int newIdx 
1e5eb 3d 20 2d 31 3b 20 20 20 20 20 20 2f 2a 20 43 75  = -1;      /* Cu
1e5ec 72 73 6f 72 20 66 6f 72 20 74 68 65 20 4e 45 57  rsor for the NEW
1e5ed 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 2a 2f   pseudo-table */
1e5ee 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20  .  int iDb;     
1e5ef 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
1e5f0 78 20 6f 66 20 64 61 74 61 62 61 73 65 20 68 6f  x of database ho
1e5f1 6c 64 69 6e 67 20 54 41 42 4c 45 20 2a 2f 0a 20  lding TABLE */. 
1e5f2 20 44 62 20 2a 70 44 62 3b 20 20 20 20 20 20 20   Db *pDb;       
1e5f3 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
1e5f4 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
1e5f5 67 20 74 61 62 6c 65 20 62 65 69 6e 67 20 69 6e  g table being in
1e5f6 73 65 72 74 65 64 20 69 6e 74 6f 20 2a 2f 0a 20  serted into */. 
1e5f7 20 69 6e 74 20 61 70 70 65 6e 64 46 6c 61 67 20   int appendFlag 
1e5f8 3d 20 30 3b 20 20 20 2f 2a 20 54 72 75 65 20 69  = 0;   /* True i
1e5f9 66 20 74 68 65 20 69 6e 73 65 72 74 20 69 73 20  f the insert is 
1e5fa 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20  likely to be an 
1e5fb 61 70 70 65 6e 64 20 2a 2f 0a 0a 20 20 2f 2a 20  append */..  /* 
1e5fc 52 65 67 69 73 74 65 72 20 61 6c 6c 6f 63 61 74  Register allocat
1e5fd 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65  ions */.  int re
1e5fe 67 46 72 6f 6d 53 65 6c 65 63 74 3b 20 20 20 20  gFromSelect;    
1e5ff 2f 2a 20 42 61 73 65 20 72 65 67 69 73 74 65 72  /* Base register
1e600 20 66 6f 72 20 64 61 74 61 20 63 6f 6d 69 6e 67   for data coming
1e601 20 66 72 6f 6d 20 53 45 4c 45 43 54 20 2a 2f 0a   from SELECT */.
1e602 20 20 69 6e 74 20 72 65 67 41 75 74 6f 69 6e 63    int regAutoinc
1e603 20 3d 20 30 3b 20 20 20 2f 2a 20 52 65 67 69 73   = 0;   /* Regis
1e604 74 65 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 20  ter holding the 
1e605 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 63 6f  AUTOINCREMENT co
1e606 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72  unter */.  int r
1e607 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20 30 3b 20  egRowCount = 0; 
1e608 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20   /* Memory cell 
1e609 75 73 65 64 20 66 6f 72 20 74 68 65 20 72 6f 77  used for the row
1e60a 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
1e60b 74 20 72 65 67 49 6e 73 3b 20 20 20 20 20 20 20  t regIns;       
1e60c 20 20 20 20 2f 2a 20 42 6c 6f 63 6b 20 6f 66 20      /* Block of 
1e60d 72 65 67 73 20 68 6f 6c 64 69 6e 67 20 72 6f 77  regs holding row
1e60e 69 64 2b 64 61 74 61 20 62 65 69 6e 67 20 69 6e  id+data being in
1e60f 73 65 72 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20  serted */.  int 
1e610 72 65 67 52 6f 77 69 64 3b 20 20 20 20 20 20 20  regRowid;       
1e611 20 20 2f 2a 20 72 65 67 69 73 74 65 72 73 20 68    /* registers h
1e612 6f 6c 64 69 6e 67 20 69 6e 73 65 72 74 20 72 6f  olding insert ro
1e613 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  wid */.  int reg
1e614 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 2f  Data;          /
1e615 2a 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64 69  * register holdi
1e616 6e 67 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20  ng first column 
1e617 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20 69  to insert */.  i
1e618 6e 74 20 72 65 67 52 65 63 6f 72 64 3b 20 20 20  nt regRecord;   
1e619 20 20 20 20 20 2f 2a 20 48 6f 6c 64 73 20 74 68       /* Holds th
1e61a 65 20 61 73 73 65 6d 62 6c 69 65 64 20 72 6f 77  e assemblied row
1e61b 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74   record */.  int
1e61c 20 2a 61 52 65 67 49 64 78 20 3d 20 30 3b 20 20   *aRegIdx = 0;  
1e61d 20 20 20 2f 2a 20 4f 6e 65 20 72 65 67 69 73 74     /* One regist
1e61e 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20  er allocated to 
1e61f 65 61 63 68 20 69 6e 64 65 78 20 2a 2f 0a 0a 0a  each index */...
1e620 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1e621 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 69 6e  MIT_TRIGGER.  in
1e622 74 20 69 73 56 69 65 77 3b 20 20 20 20 20 20 20  t isView;       
1e623 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
1e624 65 20 69 66 20 61 74 74 65 6d 70 74 69 6e 67 20  e if attempting 
1e625 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 61  to insert into a
1e626 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 74   view */.  int t
1e627 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 3d 20  riggers_exist = 
1e628 30 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  0;     /* True i
1e629 66 20 74 68 65 72 65 20 61 72 65 20 46 4f 52 20  f there are FOR 
1e62a 45 41 43 48 20 52 4f 57 20 74 72 69 67 67 65 72  EACH ROW trigger
1e62b 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 64  s */.#endif..  d
1e62c 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
1e62d 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45    if( pParse->nE
1e62e 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  rr || db->malloc
1e62f 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f  Failed ){.    go
1e630 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
1e631 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63  p;.  }..  /* Loc
1e632 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 69 6e  ate the table in
1e633 74 6f 20 77 68 69 63 68 20 77 65 20 77 69 6c 6c  to which we will
1e634 20 62 65 20 69 6e 73 65 72 74 69 6e 67 20 6e 65   be inserting ne
1e635 77 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20  w information.. 
1e636 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54   */.  assert( pT
1e637 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20  abList->nSrc==1 
1e638 29 3b 0a 20 20 7a 54 61 62 20 3d 20 70 54 61 62  );.  zTab = pTab
1e639 4c 69 73 74 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65  List->a[0].zName
1e63a 3b 0a 20 20 69 66 28 20 7a 54 61 62 3d 3d 30 20  ;.  if( zTab==0 
1e63b 29 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c  ) goto insert_cl
1e63c 65 61 6e 75 70 3b 0a 20 20 70 54 61 62 20 3d 20  eanup;.  pTab = 
1e63d 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f  sqlite3SrcListLo
1e63e 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54 61  okup(pParse, pTa
1e63f 62 4c 69 73 74 29 3b 0a 20 20 69 66 28 20 70 54  bList);.  if( pT
1e640 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74  ab==0 ){.    got
1e641 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70  o insert_cleanup
1e642 3b 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71  ;.  }.  iDb = sq
1e643 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
1e644 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  ex(db, pTab->pSc
1e645 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28  hema);.  assert(
1e646 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a   iDb<db->nDb );.
1e647 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62    pDb = &db->aDb
1e648 5b 69 44 62 5d 3b 0a 20 20 7a 44 62 20 3d 20 70  [iDb];.  zDb = p
1e649 44 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28  Db->zName;.  if(
1e64a 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
1e64b 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
1e64c 5f 49 4e 53 45 52 54 2c 20 70 54 61 62 2d 3e 7a  _INSERT, pTab->z
1e64d 4e 61 6d 65 2c 20 30 2c 20 7a 44 62 29 20 29 7b  Name, 0, zDb) ){
1e64e 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74  .    goto insert
1e64f 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20  _cleanup;.  }.. 
1e650 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 69   /* Figure out i
1e651 66 20 77 65 20 68 61 76 65 20 61 6e 79 20 74 72  f we have any tr
1e652 69 67 67 65 72 73 20 61 6e 64 20 69 66 20 74 68  iggers and if th
1e653 65 20 74 61 62 6c 65 20 62 65 69 6e 67 0a 20 20  e table being.  
1e654 2a 2a 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f  ** inserted into
1e655 20 69 73 20 61 20 76 69 65 77 0a 20 20 2a 2f 0a   is a view.  */.
1e656 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1e657 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 74 72  MIT_TRIGGER.  tr
1e658 69 67 67 65 72 73 5f 65 78 69 73 74 20 3d 20 73  iggers_exist = s
1e659 71 6c 69 74 65 33 54 72 69 67 67 65 72 73 45 78  qlite3TriggersEx
1e65a 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 61 62  ist(pParse, pTab
1e65b 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 30 29 3b  , TK_INSERT, 0);
1e65c 0a 20 20 69 73 56 69 65 77 20 3d 20 70 54 61 62  .  isView = pTab
1e65d 2d 3e 70 53 65 6c 65 63 74 21 3d 30 3b 0a 23 65  ->pSelect!=0;.#e
1e65e 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 74 72 69  lse.# define tri
1e65f 67 67 65 72 73 5f 65 78 69 73 74 20 30 0a 23 20  ggers_exist 0.# 
1e660 64 65 66 69 6e 65 20 69 73 56 69 65 77 20 30 0a  define isView 0.
1e661 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51  #endif.#ifdef SQ
1e662 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 23  LITE_OMIT_VIEW.#
1e663 20 75 6e 64 65 66 20 69 73 56 69 65 77 0a 23 20   undef isView.# 
1e664 64 65 66 69 6e 65 20 69 73 56 69 65 77 20 30 0a  define isView 0.
1e665 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 45 6e 73  #endif..  /* Ens
1e666 75 72 65 20 74 68 61 74 3a 0a 20 20 2a 20 20 28  ure that:.  *  (
1e667 61 29 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  a) the table is 
1e668 6e 6f 74 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 0a  not read-only, .
1e669 20 20 2a 20 20 28 62 29 20 74 68 61 74 20 69 66    *  (b) that if
1e66a 20 69 74 20 69 73 20 61 20 76 69 65 77 20 74 68   it is a view th
1e66b 65 6e 20 4f 4e 20 49 4e 53 45 52 54 20 74 72 69  en ON INSERT tri
1e66c 67 67 65 72 73 20 65 78 69 73 74 0a 20 20 2a 2f  ggers exist.  */
1e66d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73  .  if( sqlite3Is
1e66e 52 65 61 64 4f 6e 6c 79 28 70 50 61 72 73 65 2c  ReadOnly(pParse,
1e66f 20 70 54 61 62 2c 20 74 72 69 67 67 65 72 73 5f   pTab, triggers_
1e670 65 78 69 73 74 29 20 29 7b 0a 20 20 20 20 67 6f  exist) ){.    go
1e671 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
1e672 70 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  p;.  }.  assert(
1e673 20 70 54 61 62 21 3d 30 20 29 3b 0a 0a 20 20 2f   pTab!=0 );..  /
1e674 2a 20 49 66 20 70 54 61 62 20 69 73 20 72 65 61  * If pTab is rea
1e675 6c 6c 79 20 61 20 76 69 65 77 2c 20 6d 61 6b 65  lly a view, make
1e676 20 73 75 72 65 20 69 74 20 68 61 73 20 62 65 65   sure it has bee
1e677 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20  n initialized.. 
1e678 20 2a 2a 20 56 69 65 77 47 65 74 43 6f 6c 75 6d   ** ViewGetColum
1e679 6e 4e 61 6d 65 73 28 29 20 69 73 20 61 20 6e 6f  nNames() is a no
1e67a 2d 6f 70 20 69 66 20 70 54 61 62 20 69 73 20 6e  -op if pTab is n
1e67b 6f 74 20 61 20 76 69 65 77 20 28 6f 72 20 76 69  ot a view (or vi
1e67c 72 74 75 61 6c 20 0a 20 20 2a 2a 20 6d 6f 64 75  rtual .  ** modu
1e67d 6c 65 20 74 61 62 6c 65 29 2e 0a 20 20 2a 2f 0a  le table)..  */.
1e67e 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 69 65    if( sqlite3Vie
1e67f 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  wGetColumnNames(
1e680 70 50 61 72 73 65 2c 20 70 54 61 62 29 20 29 7b  pParse, pTab) ){
1e681 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74  .    goto insert
1e682 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20  _cleanup;.  }.. 
1e683 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 56   /* Allocate a V
1e684 44 42 45 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73  DBE.  */.  v = s
1e685 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
1e686 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d  arse);.  if( v==
1e687 30 20 29 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f  0 ) goto insert_
1e688 63 6c 65 61 6e 75 70 3b 0a 20 20 69 66 28 20 70  cleanup;.  if( p
1e689 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30  Parse->nested==0
1e68a 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f   ) sqlite3VdbeCo
1e68b 75 6e 74 43 68 61 6e 67 65 73 28 76 29 3b 0a 20  untChanges(v);. 
1e68c 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69   sqlite3BeginWri
1e68d 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72  teOperation(pPar
1e68e 73 65 2c 20 70 53 65 6c 65 63 74 20 7c 7c 20 74  se, pSelect || t
1e68f 72 69 67 67 65 72 73 5f 65 78 69 73 74 2c 20 69  riggers_exist, i
1e690 44 62 29 3b 0a 0a 20 20 2f 2a 20 69 66 20 74 68  Db);..  /* if th
1e691 65 72 65 20 61 72 65 20 72 6f 77 20 74 72 69 67  ere are row trig
1e692 67 65 72 73 2c 20 61 6c 6c 6f 63 61 74 65 20 61  gers, allocate a
1e693 20 74 65 6d 70 20 74 61 62 6c 65 20 66 6f 72 20   temp table for 
1e694 6e 65 77 2e 2a 20 72 65 66 65 72 65 6e 63 65 73  new.* references
1e695 2e 20 2a 2f 0a 20 20 69 66 28 20 74 72 69 67 67  . */.  if( trigg
1e696 65 72 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20  ers_exist ){.   
1e697 20 6e 65 77 49 64 78 20 3d 20 70 50 61 72 73 65   newIdx = pParse
1e698 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 23  ->nTab++;.  }..#
1e699 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1e69a 49 54 5f 58 46 45 52 5f 4f 50 54 0a 20 20 2f 2a  IT_XFER_OPT.  /*
1e69b 20 49 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e   If the statemen
1e69c 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  t is of the form
1e69d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20  .  **.  **      
1e69e 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 3c 74 61   INSERT INTO <ta
1e69f 62 6c 65 31 3e 20 53 45 4c 45 43 54 20 2a 20 46  ble1> SELECT * F
1e6a0 52 4f 4d 20 3c 74 61 62 6c 65 32 3e 3b 0a 20 20  ROM <table2>;.  
1e6a1 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e 20 73 70 65  **.  ** Then spe
1e6a2 63 69 61 6c 20 6f 70 74 69 6d 69 7a 61 74 69 6f  cial optimizatio
1e6a3 6e 73 20 63 61 6e 20 62 65 20 61 70 70 6c 69 65  ns can be applie
1e6a4 64 20 74 68 61 74 20 6d 61 6b 65 20 74 68 65 20  d that make the 
1e6a5 74 72 61 6e 73 66 65 72 0a 20 20 2a 2a 20 76 65  transfer.  ** ve
1e6a6 72 79 20 66 61 73 74 20 61 6e 64 20 77 68 69 63  ry fast and whic
1e6a7 68 20 72 65 64 75 63 65 20 66 72 61 67 6d 65 6e  h reduce fragmen
1e6a8 74 61 74 69 6f 6e 20 6f 66 20 69 6e 64 69 63 65  tation of indice
1e6a9 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 43  s..  */.  if( pC
1e6aa 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 78 66 65 72  olumn==0 && xfer
1e6ab 4f 70 74 69 6d 69 7a 61 74 69 6f 6e 28 70 50 61  Optimization(pPa
1e6ac 72 73 65 2c 20 70 54 61 62 2c 20 70 53 65 6c 65  rse, pTab, pSele
1e6ad 63 74 2c 20 6f 6e 45 72 72 6f 72 2c 20 69 44 62  ct, onError, iDb
1e6ae 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  ) ){.    assert(
1e6af 20 21 74 72 69 67 67 65 72 73 5f 65 78 69 73 74   !triggers_exist
1e6b0 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
1e6b1 70 4c 69 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20  pList==0 );.    
1e6b2 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61  goto insert_clea
1e6b3 6e 75 70 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20  nup;.  }.#endif 
1e6b4 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58  /* SQLITE_OMIT_X
1e6b5 46 45 52 5f 4f 50 54 20 2a 2f 0a 0a 20 20 2f 2a  FER_OPT */..  /*
1e6b6 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 41   If this is an A
1e6b7 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 74 61 62  UTOINCREMENT tab
1e6b8 6c 65 2c 20 6c 6f 6f 6b 20 75 70 20 74 68 65 20  le, look up the 
1e6b9 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20  sequence number 
1e6ba 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69  in the.  ** sqli
1e6bb 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c  te_sequence tabl
1e6bc 65 20 61 6e 64 20 73 74 6f 72 65 20 69 74 20 69  e and store it i
1e6bd 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 72 65  n memory cell re
1e6be 67 41 75 74 6f 69 6e 63 2e 0a 20 20 2a 2f 0a 20  gAutoinc..  */. 
1e6bf 20 72 65 67 41 75 74 6f 69 6e 63 20 3d 20 61 75   regAutoinc = au
1e6c0 74 6f 49 6e 63 42 65 67 69 6e 28 70 50 61 72 73  toIncBegin(pPars
1e6c1 65 2c 20 69 44 62 2c 20 70 54 61 62 29 3b 0a 0a  e, iDb, pTab);..
1e6c2 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20    /* Figure out 
1e6c3 68 6f 77 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73  how many columns
1e6c4 20 6f 66 20 64 61 74 61 20 61 72 65 20 73 75 70   of data are sup
1e6c5 70 6c 69 65 64 2e 20 20 49 66 20 74 68 65 20 64  plied.  If the d
1e6c6 61 74 61 0a 20 20 2a 2a 20 69 73 20 63 6f 6d 69  ata.  ** is comi
1e6c7 6e 67 20 66 72 6f 6d 20 61 20 53 45 4c 45 43 54  ng from a SELECT
1e6c8 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e   statement, then
1e6c9 20 74 68 69 73 20 73 74 65 70 20 61 6c 73 6f 20   this step also 
1e6ca 67 65 6e 65 72 61 74 65 73 0a 20 20 2a 2a 20 61  generates.  ** a
1e6cb 6c 6c 20 74 68 65 20 63 6f 64 65 20 74 6f 20 69  ll the code to i
1e6cc 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 45 4c  mplement the SEL
1e6cd 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 61 6e  ECT statement an
1e6ce 64 20 69 6e 76 6f 6b 65 20 61 20 73 75 62 72 6f  d invoke a subro
1e6cf 75 74 69 6e 65 0a 20 20 2a 2a 20 74 6f 20 70 72  utine.  ** to pr
1e6d0 6f 63 65 73 73 20 65 61 63 68 20 72 6f 77 20 6f  ocess each row o
1e6d1 66 20 74 68 65 20 72 65 73 75 6c 74 2e 20 28 54  f the result. (T
1e6d2 65 6d 70 6c 61 74 65 20 32 2e 29 20 49 66 20 74  emplate 2.) If t
1e6d3 68 65 20 53 45 4c 45 43 54 0a 20 20 2a 2a 20 73  he SELECT.  ** s
1e6d4 74 61 74 65 6d 65 6e 74 20 75 73 65 73 20 74 68  tatement uses th
1e6d5 65 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74  e the table that
1e6d6 20 69 73 20 62 65 69 6e 67 20 69 6e 73 65 72 74   is being insert
1e6d7 65 64 20 69 6e 74 6f 2c 20 74 68 65 6e 20 74 68  ed into, then th
1e6d8 65 0a 20 20 2a 2a 20 73 75 62 72 6f 75 74 69 6e  e.  ** subroutin
1e6d9 65 20 69 73 20 61 6c 73 6f 20 63 6f 64 65 64 20  e is also coded 
1e6da 68 65 72 65 2e 20 20 54 68 61 74 20 73 75 62 72  here.  That subr
1e6db 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20 74 68  outine stores th
1e6dc 65 20 53 45 4c 45 43 54 0a 20 20 2a 2a 20 72 65  e SELECT.  ** re
1e6dd 73 75 6c 74 73 20 69 6e 20 61 20 74 65 6d 70 6f  sults in a tempo
1e6de 72 61 72 79 20 74 61 62 6c 65 2e 20 28 54 65 6d  rary table. (Tem
1e6df 70 6c 61 74 65 20 33 2e 29 0a 20 20 2a 2f 0a 20  plate 3.).  */. 
1e6e0 20 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a   if( pSelect ){.
1e6e1 20 20 20 20 2f 2a 20 44 61 74 61 20 69 73 20 63      /* Data is c
1e6e2 6f 6d 69 6e 67 20 66 72 6f 6d 20 61 20 53 45 4c  oming from a SEL
1e6e3 45 43 54 2e 20 20 47 65 6e 65 72 61 74 65 20 63  ECT.  Generate c
1e6e4 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  ode to implement
1e6e5 20 74 68 61 74 20 53 45 4c 45 43 54 0a 20 20 20   that SELECT.   
1e6e6 20 2a 2f 0a 20 20 20 20 53 65 6c 65 63 74 44 65   */.    SelectDe
1e6e7 73 74 20 64 65 73 74 3b 0a 20 20 20 20 69 6e 74  st dest;.    int
1e6e8 20 72 63 2c 20 69 49 6e 69 74 43 6f 64 65 3b 0a   rc, iInitCode;.
1e6e9 0a 20 20 20 20 69 49 6e 69 74 43 6f 64 65 20 3d  .    iInitCode =
1e6ea 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1e6eb 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
1e6ec 2c 20 30 29 3b 0a 20 20 20 20 69 53 65 6c 65 63  , 0);.    iSelec
1e6ed 74 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56  tLoop = sqlite3V
1e6ee 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
1e6ef 29 3b 0a 20 20 20 20 69 49 6e 73 65 72 74 42 6c  );.    iInsertBl
1e6f0 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ock = sqlite3Vdb
1e6f1 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
1e6f2 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
1e6f3 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20  DestInit(&dest, 
1e6f4 53 52 54 5f 53 75 62 72 6f 75 74 69 6e 65 2c 20  SRT_Subroutine, 
1e6f5 69 49 6e 73 65 72 74 42 6c 6f 63 6b 29 3b 0a 0a  iInsertBlock);..
1e6f6 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74      /* Resolve t
1e6f7 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69  he expressions i
1e6f8 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  n the SELECT sta
1e6f9 74 65 6d 65 6e 74 20 61 6e 64 20 65 78 65 63 75  tement and execu
1e6fa 74 65 20 69 74 2e 20 2a 2f 0a 20 20 20 20 72 63  te it. */.    rc
1e6fb 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
1e6fc 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74  (pParse, pSelect
1e6fd 2c 20 26 64 65 73 74 2c 20 30 2c 20 30 2c 20 30  , &dest, 0, 0, 0
1e6fe 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63  , 0);.    if( rc
1e6ff 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72   || pParse->nErr
1e700 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
1e701 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 67 6f  iled ){.      go
1e702 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
1e703 70 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 65  p;.    }..    re
1e704 67 46 72 6f 6d 53 65 6c 65 63 74 20 3d 20 64 65  gFromSelect = de
1e705 73 74 2e 69 4d 65 6d 3b 0a 20 20 20 20 69 43 6c  st.iMem;.    iCl
1e706 65 61 6e 75 70 20 3d 20 73 71 6c 69 74 65 33 56  eanup = sqlite3V
1e707 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
1e708 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1e709 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
1e70a 6f 2c 20 30 2c 20 69 43 6c 65 61 6e 75 70 29 3b  o, 0, iCleanup);
1e70b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 53 65  .    assert( pSe
1e70c 6c 65 63 74 2d 3e 70 45 4c 69 73 74 20 29 3b 0a  lect->pEList );.
1e70d 20 20 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 53      nColumn = pS
1e70e 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e  elect->pEList->n
1e70f 45 78 70 72 3b 0a 0a 20 20 20 20 2f 2a 20 53 65  Expr;..    /* Se
1e710 74 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 74  t useTempTable t
1e711 6f 20 54 52 55 45 20 69 66 20 74 68 65 20 72 65  o TRUE if the re
1e712 73 75 6c 74 20 6f 66 20 74 68 65 20 53 45 4c 45  sult of the SELE
1e713 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20  CT statement.   
1e714 20 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 77 72   ** should be wr
1e715 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 74 65 6d  itten into a tem
1e716 70 6f 72 61 72 79 20 74 61 62 6c 65 2e 20 20 53  porary table.  S
1e717 65 74 20 74 6f 20 46 41 4c 53 45 20 69 66 20 65  et to FALSE if e
1e718 61 63 68 0a 20 20 20 20 2a 2a 20 72 6f 77 20 6f  ach.    ** row o
1e719 66 20 74 68 65 20 53 45 4c 45 43 54 20 63 61 6e  f the SELECT can
1e71a 20 62 65 20 77 72 69 74 74 65 6e 20 64 69 72 65   be written dire
1e71b 63 74 6c 79 20 69 6e 74 6f 20 74 68 65 20 72 65  ctly into the re
1e71c 73 75 6c 74 20 74 61 62 6c 65 2e 0a 20 20 20 20  sult table..    
1e71d 2a 2a 0a 20 20 20 20 2a 2a 20 41 20 74 65 6d 70  **.    ** A temp
1e71e 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20 75   table must be u
1e71f 73 65 64 20 69 66 20 74 68 65 20 74 61 62 6c 65  sed if the table
1e720 20 62 65 69 6e 67 20 75 70 64 61 74 65 64 20 69   being updated i
1e721 73 20 61 6c 73 6f 20 6f 6e 65 0a 20 20 20 20 2a  s also one.    *
1e722 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65 73 20  * of the tables 
1e723 62 65 69 6e 67 20 72 65 61 64 20 62 79 20 74 68  being read by th
1e724 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
1e725 6e 74 2e 20 20 41 6c 73 6f 20 75 73 65 20 61 20  nt.  Also use a 
1e726 0a 20 20 20 20 2a 2a 20 74 65 6d 70 20 74 61 62  .    ** temp tab
1e727 6c 65 20 69 6e 20 74 68 65 20 63 61 73 65 20 6f  le in the case o
1e728 66 20 72 6f 77 20 74 72 69 67 67 65 72 73 2e 0a  f row triggers..
1e729 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 74      */.    if( t
1e72a 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 7c 7c  riggers_exist ||
1e72b 20 72 65 61 64 73 54 61 62 6c 65 28 76 2c 20 69   readsTable(v, i
1e72c 53 65 6c 65 63 74 4c 6f 6f 70 2c 20 69 44 62 2c  SelectLoop, iDb,
1e72d 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20   pTab) ){.      
1e72e 75 73 65 54 65 6d 70 54 61 62 6c 65 20 3d 20 31  useTempTable = 1
1e72f 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
1e730 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 29 7b   useTempTable ){
1e731 0a 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61  .      /* Genera
1e732 74 65 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e  te the subroutin
1e733 65 20 74 68 61 74 20 53 45 4c 45 43 54 20 63 61  e that SELECT ca
1e734 6c 6c 73 20 74 6f 20 70 72 6f 63 65 73 73 20 65  lls to process e
1e735 61 63 68 20 72 6f 77 20 6f 66 0a 20 20 20 20 20  ach row of.     
1e736 20 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 2e 20   ** the result. 
1e737 20 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   Store the resul
1e738 74 20 69 6e 20 61 20 74 65 6d 70 6f 72 61 72 79  t in a temporary
1e739 20 74 61 62 6c 65 0a 20 20 20 20 20 20 2a 2f 0a   table.      */.
1e73a 20 20 20 20 20 20 69 6e 74 20 72 65 67 52 65 63        int regRec
1e73b 2c 20 72 65 67 52 6f 77 69 64 3b 0a 0a 20 20 20  , regRowid;..   
1e73c 20 20 20 73 72 63 54 61 62 20 3d 20 70 50 61 72     srcTab = pPar
1e73d 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20  se->nTab++;.    
1e73e 20 20 72 65 67 52 65 63 20 3d 20 73 71 6c 69 74    regRec = sqlit
1e73f 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
1e740 72 73 65 29 3b 0a 20 20 20 20 20 20 72 65 67 52  rse);.      regR
1e741 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 47 65  owid = sqlite3Ge
1e742 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
1e743 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1e744 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
1e745 76 2c 20 69 49 6e 73 65 72 74 42 6c 6f 63 6b 29  v, iInsertBlock)
1e746 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1e747 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
1e748 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 46  MakeRecord, regF
1e749 72 6f 6d 53 65 6c 65 63 74 2c 20 6e 43 6f 6c 75  romSelect, nColu
1e74a 6d 6e 2c 20 72 65 67 52 65 63 29 3b 0a 20 20 20  mn, regRec);.   
1e74b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1e74c 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f  dOp2(v, OP_NewRo
1e74d 77 69 64 2c 20 73 72 63 54 61 62 2c 20 72 65 67  wid, srcTab, reg
1e74e 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 73 71  Rowid);.      sq
1e74f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
1e750 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 73 72  v, OP_Insert, sr
1e751 63 54 61 62 2c 20 72 65 67 52 65 63 2c 20 72 65  cTab, regRec, re
1e752 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 73  gRowid);.      s
1e753 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1e754 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c 20 30  (v, OP_Return, 0
1e755 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 0);.      sqli
1e756 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
1e757 67 28 70 50 61 72 73 65 2c 20 72 65 67 52 65 63  g(pParse, regRec
1e758 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1e759 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
1e75a 50 61 72 73 65 2c 20 72 65 67 52 6f 77 69 64 29  Parse, regRowid)
1e75b 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;..      /* The 
1e75c 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 72  following code r
1e75d 75 6e 73 20 66 69 72 73 74 20 62 65 63 61 75 73  uns first becaus
1e75e 65 20 74 68 65 20 47 4f 54 4f 20 61 74 20 74 68  e the GOTO at th
1e75f 65 20 76 65 72 79 20 74 6f 70 0a 20 20 20 20 20  e very top.     
1e760 20 2a 2a 20 6f 66 20 74 68 65 20 70 72 6f 67 72   ** of the progr
1e761 61 6d 20 6a 75 6d 70 73 20 74 6f 20 69 74 2e 20  am jumps to it. 
1e762 20 43 72 65 61 74 65 20 74 68 65 20 74 65 6d 70   Create the temp
1e763 6f 72 61 72 79 20 74 61 62 6c 65 2c 20 74 68 65  orary table, the
1e764 6e 20 6a 75 6d 70 0a 20 20 20 20 20 20 2a 2a 20  n jump.      ** 
1e765 62 61 63 6b 20 75 70 20 61 6e 64 20 65 78 65 63  back up and exec
1e766 75 74 65 20 74 68 65 20 53 45 4c 45 43 54 20 63  ute the SELECT c
1e767 6f 64 65 20 61 62 6f 76 65 2e 0a 20 20 20 20 20  ode above..     
1e768 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
1e769 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
1e76a 20 69 49 6e 69 74 43 6f 64 65 29 3b 0a 20 20 20   iInitCode);.   
1e76b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1e76c 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45  dOp2(v, OP_OpenE
1e76d 70 68 65 6d 65 72 61 6c 2c 20 73 72 63 54 61 62  phemeral, srcTab
1e76e 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  , nColumn);.    
1e76f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1e770 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
1e771 30 2c 20 69 53 65 6c 65 63 74 4c 6f 6f 70 29 3b  0, iSelectLoop);
1e772 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1e773 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
1e774 2c 20 69 43 6c 65 61 6e 75 70 29 3b 0a 20 20 20  , iCleanup);.   
1e775 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
1e776 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
1e777 65 28 76 2c 20 69 49 6e 69 74 43 6f 64 65 29 3b  e(v, iInitCode);
1e778 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
1e779 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74      /* This is t
1e77a 68 65 20 63 61 73 65 20 69 66 20 74 68 65 20 64  he case if the d
1e77b 61 74 61 20 66 6f 72 20 74 68 65 20 49 4e 53 45  ata for the INSE
1e77c 52 54 20 69 73 20 63 6f 6d 69 6e 67 20 66 72 6f  RT is coming fro
1e77d 6d 20 61 20 56 41 4c 55 45 53 0a 20 20 20 20 2a  m a VALUES.    *
1e77e 2a 20 63 6c 61 75 73 65 0a 20 20 20 20 2a 2f 0a  * clause.    */.
1e77f 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20      NameContext 
1e780 73 4e 43 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  sNC;.    memset(
1e781 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28  &sNC, 0, sizeof(
1e782 73 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70  sNC));.    sNC.p
1e783 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a  Parse = pParse;.
1e784 20 20 20 20 73 72 63 54 61 62 20 3d 20 2d 31 3b      srcTab = -1;
1e785 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 73 65  .    assert( use
1e786 54 65 6d 70 54 61 62 6c 65 3d 3d 30 20 29 3b 0a  TempTable==0 );.
1e787 20 20 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 4c      nColumn = pL
1e788 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 78  ist ? pList->nEx
1e789 70 72 20 3a 20 30 3b 0a 20 20 20 20 66 6f 72 28  pr : 0;.    for(
1e78a 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20  i=0; i<nColumn; 
1e78b 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
1e78c 73 71 6c 69 74 65 33 45 78 70 72 52 65 73 6f 6c  sqlite3ExprResol
1e78d 76 65 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 4c  veNames(&sNC, pL
1e78e 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29  ist->a[i].pExpr)
1e78f 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   ){.        goto
1e790 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b   insert_cleanup;
1e791 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1e792 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75   }..  /* Make su
1e793 72 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  re the number of
1e794 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
1e795 73 6f 75 72 63 65 20 64 61 74 61 20 6d 61 74 63  source data matc
1e796 68 65 73 20 74 68 65 20 6e 75 6d 62 65 72 0a 20  hes the number. 
1e797 20 2a 2a 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74   ** of columns t
1e798 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e  o be inserted in
1e799 74 6f 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20  to the table..  
1e79a 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74 75  */.  if( IsVirtu
1e79b 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
1e79c 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d  for(i=0; i<pTab-
1e79d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nCol; i++){.   
1e79e 20 20 20 6e 48 69 64 64 65 6e 20 2b 3d 20 28 49     nHidden += (I
1e79f 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 26 70  sHiddenColumn(&p
1e7a0 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 29 20 3f 20  Tab->aCol[i]) ? 
1e7a1 31 20 3a 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  1 : 0);.    }.  
1e7a2 7d 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d  }.  if( pColumn=
1e7a3 3d 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 20 26 26  =0 && nColumn &&
1e7a4 20 6e 43 6f 6c 75 6d 6e 21 3d 28 70 54 61 62 2d   nColumn!=(pTab-
1e7a5 3e 6e 43 6f 6c 2d 6e 48 69 64 64 65 6e 29 20 29  >nCol-nHidden) )
1e7a6 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
1e7a7 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20  orMsg(pParse, . 
1e7a8 20 20 20 20 20 20 22 74 61 62 6c 65 20 25 53 20        "table %S 
1e7a9 68 61 73 20 25 64 20 63 6f 6c 75 6d 6e 73 20 62  has %d columns b
1e7aa 75 74 20 25 64 20 76 61 6c 75 65 73 20 77 65 72  ut %d values wer
1e7ab 65 20 73 75 70 70 6c 69 65 64 22 2c 0a 20 20 20  e supplied",.   
1e7ac 20 20 20 20 70 54 61 62 4c 69 73 74 2c 20 30 2c      pTabList, 0,
1e7ad 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 6e 43 6f   pTab->nCol, nCo
1e7ae 6c 75 6d 6e 29 3b 0a 20 20 20 20 67 6f 74 6f 20  lumn);.    goto 
1e7af 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a  insert_cleanup;.
1e7b0 20 20 7d 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d    }.  if( pColum
1e7b1 6e 21 3d 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 21  n!=0 && nColumn!
1e7b2 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 20 29 7b  =pColumn->nId ){
1e7b3 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
1e7b4 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 64  rMsg(pParse, "%d
1e7b5 20 76 61 6c 75 65 73 20 66 6f 72 20 25 64 20 63   values for %d c
1e7b6 6f 6c 75 6d 6e 73 22 2c 20 6e 43 6f 6c 75 6d 6e  olumns", nColumn
1e7b7 2c 20 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 29 3b  , pColumn->nId);
1e7b8 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74  .    goto insert
1e7b9 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20  _cleanup;.  }.. 
1e7ba 20 2f 2a 20 49 66 20 74 68 65 20 49 4e 53 45 52   /* If the INSER
1e7bb 54 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 63 6c  T statement incl
1e7bc 75 64 65 64 20 61 6e 20 49 44 4c 49 53 54 20 74  uded an IDLIST t
1e7bd 65 72 6d 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73  erm, then make s
1e7be 75 72 65 0a 20 20 2a 2a 20 61 6c 6c 20 65 6c 65  ure.  ** all ele
1e7bf 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 49 44 4c  ments of the IDL
1e7c0 49 53 54 20 72 65 61 6c 6c 79 20 61 72 65 20 63  IST really are c
1e7c1 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 74 61  olumns of the ta
1e7c2 62 6c 65 20 61 6e 64 20 0a 20 20 2a 2a 20 72 65  ble and .  ** re
1e7c3 6d 65 6d 62 65 72 20 74 68 65 20 63 6f 6c 75 6d  member the colum
1e7c4 6e 20 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2a 0a  n indices..  **.
1e7c5 20 20 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c    ** If the tabl
1e7c6 65 20 68 61 73 20 61 6e 20 49 4e 54 45 47 45 52  e has an INTEGER
1e7c7 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c   PRIMARY KEY col
1e7c8 75 6d 6e 20 61 6e 64 20 74 68 61 74 20 63 6f 6c  umn and that col
1e7c9 75 6d 6e 0a 20 20 2a 2a 20 69 73 20 6e 61 6d 65  umn.  ** is name
1e7ca 64 20 69 6e 20 74 68 65 20 49 44 4c 49 53 54 2c  d in the IDLIST,
1e7cb 20 74 68 65 6e 20 72 65 63 6f 72 64 20 69 6e 20   then record in 
1e7cc 74 68 65 20 6b 65 79 43 6f 6c 75 6d 6e 20 76 61  the keyColumn va
1e7cd 72 69 61 62 6c 65 0a 20 20 2a 2a 20 74 68 65 20  riable.  ** the 
1e7ce 69 6e 64 65 78 20 69 6e 74 6f 20 49 44 4c 49 53  index into IDLIS
1e7cf 54 20 6f 66 20 74 68 65 20 70 72 69 6d 61 72 79  T of the primary
1e7d0 20 6b 65 79 20 63 6f 6c 75 6d 6e 2e 20 20 6b 65   key column.  ke
1e7d1 79 43 6f 6c 75 6d 6e 20 69 73 0a 20 20 2a 2a 20  yColumn is.  ** 
1e7d2 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
1e7d3 20 70 72 69 6d 61 72 79 20 6b 65 79 20 61 73 20   primary key as 
1e7d4 69 74 20 61 70 70 65 61 72 73 20 69 6e 20 49 44  it appears in ID
1e7d5 4c 49 53 54 2c 20 6e 6f 74 20 61 73 0a 20 20 2a  LIST, not as.  *
1e7d6 2a 20 69 73 20 61 70 70 65 61 72 73 20 69 6e 20  * is appears in 
1e7d7 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 61 62  the original tab
1e7d8 6c 65 2e 20 20 28 54 68 65 20 69 6e 64 65 78 20  le.  (The index 
1e7d9 6f 66 20 74 68 65 20 70 72 69 6d 61 72 79 0a 20  of the primary. 
1e7da 20 2a 2a 20 6b 65 79 20 69 6e 20 74 68 65 20 6f   ** key in the o
1e7db 72 69 67 69 6e 61 6c 20 74 61 62 6c 65 20 69 73  riginal table is
1e7dc 20 70 54 61 62 2d 3e 69 50 4b 65 79 2e 29 0a 20   pTab->iPKey.). 
1e7dd 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d   */.  if( pColum
1e7de 6e 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  n ){.    for(i=0
1e7df 3b 20 69 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64  ; i<pColumn->nId
1e7e0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 43  ; i++){.      pC
1e7e1 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 69 64 78 20  olumn->a[i].idx 
1e7e2 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  = -1;.    }.    
1e7e3 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 6f 6c 75  for(i=0; i<pColu
1e7e4 6d 6e 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20  mn->nId; i++){. 
1e7e5 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
1e7e6 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29  pTab->nCol; j++)
1e7e7 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71  {.        if( sq
1e7e8 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f  lite3StrICmp(pCo
1e7e9 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  lumn->a[i].zName
1e7ea 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e  , pTab->aCol[j].
1e7eb 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20  zName)==0 ){.   
1e7ec 20 20 20 20 20 20 20 70 43 6f 6c 75 6d 6e 2d 3e         pColumn->
1e7ed 61 5b 69 5d 2e 69 64 78 20 3d 20 6a 3b 0a 20 20  a[i].idx = j;.  
1e7ee 20 20 20 20 20 20 20 20 69 66 28 20 6a 3d 3d 70          if( j==p
1e7ef 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20  Tab->iPKey ){.  
1e7f0 20 20 20 20 20 20 20 20 20 20 6b 65 79 43 6f 6c            keyCol
1e7f1 75 6d 6e 20 3d 20 69 3b 0a 20 20 20 20 20 20 20  umn = i;.       
1e7f2 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62     }.          b
1e7f3 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
1e7f4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
1e7f5 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20  ( j>=pTab->nCol 
1e7f6 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  ){.        if( s
1e7f7 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 70 43  qlite3IsRowid(pC
1e7f8 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  olumn->a[i].zNam
1e7f9 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  e) ){.          
1e7fa 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 69 3b 0a 20  keyColumn = i;. 
1e7fb 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
1e7fc 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
1e7fd 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1e7fe 22 74 61 62 6c 65 20 25 53 20 68 61 73 20 6e 6f  "table %S has no
1e7ff 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 25 73   column named %s
1e800 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
1e801 20 70 54 61 62 4c 69 73 74 2c 20 30 2c 20 70 43   pTabList, 0, pC
1e802 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  olumn->a[i].zNam
1e803 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50  e);.          pP
1e804 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20  arse->nErr++;.  
1e805 20 20 20 20 20 20 20 20 67 6f 74 6f 20 69 6e 73          goto ins
1e806 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  ert_cleanup;.   
1e807 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
1e808 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
1e809 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 49 44  f there is no ID
1e80a 4c 49 53 54 20 74 65 72 6d 20 62 75 74 20 74 68  LIST term but th
1e80b 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20 69  e table has an i
1e80c 6e 74 65 67 65 72 20 70 72 69 6d 61 72 79 0a 20  nteger primary. 
1e80d 20 2a 2a 20 6b 65 79 2c 20 74 68 65 20 73 65 74   ** key, the set
1e80e 20 74 68 65 20 6b 65 79 43 6f 6c 75 6d 6e 20 76   the keyColumn v
1e80f 61 72 69 61 62 6c 65 20 74 6f 20 74 68 65 20 70  ariable to the p
1e810 72 69 6d 61 72 79 20 6b 65 79 20 63 6f 6c 75 6d  rimary key colum
1e811 6e 20 69 6e 64 65 78 0a 20 20 2a 2a 20 69 6e 20  n index.  ** in 
1e812 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 61 62  the original tab
1e813 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 20  le definition.. 
1e814 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d   */.  if( pColum
1e815 6e 3d 3d 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 3e  n==0 && nColumn>
1e816 30 20 29 7b 0a 20 20 20 20 6b 65 79 43 6f 6c 75  0 ){.    keyColu
1e817 6d 6e 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79  mn = pTab->iPKey
1e818 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e  ;.  }..  /* Open
1e819 20 74 68 65 20 74 65 6d 70 20 74 61 62 6c 65 20   the temp table 
1e81a 66 6f 72 20 46 4f 52 20 45 41 43 48 20 52 4f 57  for FOR EACH ROW
1e81b 20 74 72 69 67 67 65 72 73 0a 20 20 2a 2f 0a 20   triggers.  */. 
1e81c 20 69 66 28 20 74 72 69 67 67 65 72 73 5f 65 78   if( triggers_ex
1e81d 69 73 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ist ){.    sqlit
1e81e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1e81f 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73  OP_SetNumColumns
1e820 2c 20 30 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29  , 0, pTab->nCol)
1e821 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1e822 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70  eAddOp2(v, OP_Op
1e823 65 6e 50 73 65 75 64 6f 2c 20 6e 65 77 49 64 78  enPseudo, newIdx
1e824 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 20 20 0a 20  , 0);.  }.    . 
1e825 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74   /* Initialize t
1e826 68 65 20 63 6f 75 6e 74 20 6f 66 20 72 6f 77 73  he count of rows
1e827 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 0a   to be inserted.
1e828 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 66    */.  if( db->f
1e829 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f  lags & SQLITE_Co
1e82a 75 6e 74 52 6f 77 73 20 29 7b 0a 20 20 20 20 72  untRows ){.    r
1e82b 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20 2b 2b 70  egRowCount = ++p
1e82c 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
1e82d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1e82e 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
1e82f 2c 20 30 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74  , 0, regRowCount
1e830 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  );.  }..  /* If 
1e831 74 68 69 73 20 69 73 20 6e 6f 74 20 61 20 76 69  this is not a vi
1e832 65 77 2c 20 6f 70 65 6e 20 74 68 65 20 74 61 62  ew, open the tab
1e833 6c 65 20 61 6e 64 20 61 6e 64 20 61 6c 6c 20 69  le and and all i
1e834 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69 66 28 20  ndices */.  if( 
1e835 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 69  !isView ){.    i
1e836 6e 74 20 6e 49 64 78 3b 0a 20 20 20 20 69 6e 74  nt nIdx;.    int
1e837 20 69 3b 0a 0a 20 20 20 20 62 61 73 65 43 75 72   i;..    baseCur
1e838 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b   = pParse->nTab;
1e839 0a 20 20 20 20 6e 49 64 78 20 3d 20 73 71 6c 69  .    nIdx = sqli
1e83a 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49  te3OpenTableAndI
1e83b 6e 64 69 63 65 73 28 70 50 61 72 73 65 2c 20 70  ndices(pParse, p
1e83c 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20 4f 50  Tab, baseCur, OP
1e83d 5f 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20 20 20  _OpenWrite);.   
1e83e 20 61 52 65 67 49 64 78 20 3d 20 73 71 6c 69 74   aRegIdx = sqlit
1e83f 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
1e840 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28 6e  , sizeof(int)*(n
1e841 49 64 78 2b 31 29 29 3b 0a 20 20 20 20 69 66 28  Idx+1));.    if(
1e842 20 61 52 65 67 49 64 78 3d 3d 30 20 29 7b 0a 20   aRegIdx==0 ){. 
1e843 20 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74       goto insert
1e844 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a  _cleanup;.    }.
1e845 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
1e846 49 64 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Idx; i++){.     
1e847 20 61 52 65 67 49 64 78 5b 69 5d 20 3d 20 2b 2b   aRegIdx[i] = ++
1e848 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
1e849 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66    }.  }..  /* If
1e84a 20 74 68 65 20 64 61 74 61 20 73 6f 75 72 63 65   the data source
1e84b 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20   is a temporary 
1e84c 74 61 62 6c 65 2c 20 74 68 65 6e 20 77 65 20 68  table, then we h
1e84d 61 76 65 20 74 6f 20 63 72 65 61 74 65 0a 20 20  ave to create.  
1e84e 2a 2a 20 61 20 6c 6f 6f 70 20 62 65 63 61 75 73  ** a loop becaus
1e84f 65 20 74 68 65 72 65 20 6d 69 67 68 74 20 62 65  e there might be
1e850 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77 73 20 6f   multiple rows o
1e851 66 20 64 61 74 61 2e 20 20 49 66 20 74 68 65 20  f data.  If the 
1e852 64 61 74 61 0a 20 20 2a 2a 20 73 6f 75 72 63 65  data.  ** source
1e853 20 69 73 20 61 20 73 75 62 72 6f 75 74 69 6e 65   is a subroutine
1e854 20 63 61 6c 6c 20 66 72 6f 6d 20 74 68 65 20 53   call from the S
1e855 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2c  ELECT statement,
1e856 20 74 68 65 6e 20 77 65 20 6e 65 65 64 0a 20 20   then we need.  
1e857 2a 2a 20 74 6f 20 6c 61 75 6e 63 68 20 74 68 65  ** to launch the
1e858 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1e859 74 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a 20 20  t processing..  
1e85a 2a 2f 0a 20 20 69 66 28 20 75 73 65 54 65 6d 70  */.  if( useTemp
1e85b 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 69 42 72  Table ){.    iBr
1e85c 65 61 6b 20 3d 20 73 71 6c 69 74 65 33 56 64 62  eak = sqlite3Vdb
1e85d 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
1e85e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1e85f 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e  dOp2(v, OP_Rewin
1e860 64 2c 20 73 72 63 54 61 62 2c 20 69 42 72 65 61  d, srcTab, iBrea
1e861 6b 29 3b 0a 20 20 20 20 69 43 6f 6e 74 20 3d 20  k);.    iCont = 
1e862 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
1e863 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 7d 65 6c  ntAddr(v);.  }el
1e864 73 65 20 69 66 28 20 70 53 65 6c 65 63 74 20 29  se if( pSelect )
1e865 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1e866 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
1e867 74 6f 2c 20 30 2c 20 69 53 65 6c 65 63 74 4c 6f  to, 0, iSelectLo
1e868 6f 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  op);.    sqlite3
1e869 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
1e86a 28 76 2c 20 69 49 6e 73 65 72 74 42 6c 6f 63 6b  (v, iInsertBlock
1e86b 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c  );.  }..  /* All
1e86c 6f 63 61 74 65 20 72 65 67 69 73 74 65 72 73 20  ocate registers 
1e86d 66 6f 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 20  for holding the 
1e86e 72 6f 77 69 64 20 6f 66 20 74 68 65 20 6e 65 77  rowid of the new
1e86f 20 72 6f 77 2c 0a 20 20 2a 2a 20 74 68 65 20 63   row,.  ** the c
1e870 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 6e 65  ontent of the ne
1e871 77 20 72 6f 77 2c 20 61 6e 64 20 74 68 65 20 61  w row, and the a
1e872 73 73 65 6d 62 6c 69 65 64 20 72 6f 77 20 72 65  ssemblied row re
1e873 63 6f 72 64 2e 0a 20 20 2a 2f 0a 20 20 72 65 67  cord..  */.  reg
1e874 52 65 63 6f 72 64 20 3d 20 2b 2b 70 50 61 72 73  Record = ++pPars
1e875 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 52 6f  e->nMem;.  regRo
1e876 77 69 64 20 3d 20 72 65 67 49 6e 73 20 3d 20 70  wid = regIns = p
1e877 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20  Parse->nMem+1;. 
1e878 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d   pParse->nMem +=
1e879 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 2b 20 31 3b   pTab->nCol + 1;
1e87a 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c  .  if( IsVirtual
1e87b 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 72 65  (pTab) ){.    re
1e87c 67 52 6f 77 69 64 2b 2b 3b 0a 20 20 20 20 70 50  gRowid++;.    pP
1e87d 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20  arse->nMem++;.  
1e87e 7d 0a 20 20 72 65 67 44 61 74 61 20 3d 20 72 65  }.  regData = re
1e87f 67 52 6f 77 69 64 2b 31 3b 0a 0a 20 20 2f 2a 20  gRowid+1;..  /* 
1e880 52 75 6e 20 74 68 65 20 42 45 46 4f 52 45 20 61  Run the BEFORE a
1e881 6e 64 20 49 4e 53 54 45 41 44 20 4f 46 20 74 72  nd INSTEAD OF tr
1e882 69 67 67 65 72 73 2c 20 69 66 20 74 68 65 72 65  iggers, if there
1e883 20 61 72 65 20 61 6e 79 0a 20 20 2a 2f 0a 20 20   are any.  */.  
1e884 65 6e 64 4f 66 4c 6f 6f 70 20 3d 20 73 71 6c 69  endOfLoop = sqli
1e885 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
1e886 28 76 29 3b 0a 20 20 69 66 28 20 74 72 69 67 67  (v);.  if( trigg
1e887 65 72 73 5f 65 78 69 73 74 20 26 20 54 52 49 47  ers_exist & TRIG
1e888 47 45 52 5f 42 45 46 4f 52 45 20 29 7b 0a 20 20  GER_BEFORE ){.  
1e889 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 0a    int regRowid;.
1e88a 20 20 20 20 69 6e 74 20 72 65 67 43 6f 6c 73 3b      int regCols;
1e88b 0a 20 20 20 20 69 6e 74 20 72 65 67 52 65 63 3b  .    int regRec;
1e88c 0a 0a 20 20 20 20 2f 2a 20 62 75 69 6c 64 20 74  ..    /* build t
1e88d 68 65 20 4e 45 57 2e 2a 20 72 65 66 65 72 65 6e  he NEW.* referen
1e88e 63 65 20 72 6f 77 2e 20 20 4e 6f 74 65 20 74 68  ce row.  Note th
1e88f 61 74 20 69 66 20 74 68 65 72 65 20 69 73 20 61  at if there is a
1e890 6e 20 49 4e 54 45 47 45 52 0a 20 20 20 20 2a 2a  n INTEGER.    **
1e891 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 6e 74   PRIMARY KEY int
1e892 6f 20 77 68 69 63 68 20 61 20 4e 55 4c 4c 20 69  o which a NULL i
1e893 73 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64  s being inserted
1e894 2c 20 74 68 61 74 20 4e 55 4c 4c 20 77 69 6c 6c  , that NULL will
1e895 20 62 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73   be.    ** trans
1e896 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 75 6e 69  lated into a uni
1e897 71 75 65 20 49 44 20 66 6f 72 20 74 68 65 20 72  que ID for the r
1e898 6f 77 2e 20 20 42 75 74 20 6f 6e 20 61 20 42 45  ow.  But on a BE
1e899 46 4f 52 45 20 74 72 69 67 67 65 72 2c 0a 20 20  FORE trigger,.  
1e89a 20 20 2a 2a 20 77 65 20 64 6f 20 6e 6f 74 20 6b    ** we do not k
1e89b 6e 6f 77 20 77 68 61 74 20 74 68 65 20 75 6e 69  now what the uni
1e89c 71 75 65 20 49 44 20 77 69 6c 6c 20 62 65 20 28  que ID will be (
1e89d 62 65 63 61 75 73 65 20 74 68 65 20 69 6e 73 65  because the inse
1e89e 72 74 20 68 61 73 0a 20 20 20 20 2a 2a 20 6e 6f  rt has.    ** no
1e89f 74 20 68 61 70 70 65 6e 65 64 20 79 65 74 29 20  t happened yet) 
1e8a0 73 6f 20 77 65 20 73 75 62 73 74 69 74 75 74 65  so we substitute
1e8a1 20 61 20 72 6f 77 69 64 20 6f 66 20 2d 31 0a 20   a rowid of -1. 
1e8a2 20 20 20 2a 2f 0a 20 20 20 20 72 65 67 52 6f 77     */.    regRow
1e8a3 69 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  id = sqlite3GetT
1e8a4 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
1e8a5 20 20 20 20 69 66 28 20 6b 65 79 43 6f 6c 75 6d      if( keyColum
1e8a6 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  n<0 ){.      sql
1e8a7 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1e8a8 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31  , OP_Integer, -1
1e8a9 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
1e8aa 20 7d 65 6c 73 65 20 69 66 28 20 75 73 65 54 65   }else if( useTe
1e8ab 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20  mpTable ){.     
1e8ac 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1e8ad 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
1e8ae 20 73 72 63 54 61 62 2c 20 6b 65 79 43 6f 6c 75   srcTab, keyColu
1e8af 6d 6e 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  mn, regRowid);. 
1e8b0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1e8b1 69 6e 74 20 6a 31 3b 0a 20 20 20 20 20 20 61 73  int j1;.      as
1e8b2 73 65 72 74 28 20 70 53 65 6c 65 63 74 3d 3d 30  sert( pSelect==0
1e8b3 20 29 3b 20 20 2f 2a 20 4f 74 68 65 72 77 69 73   );  /* Otherwis
1e8b4 65 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 69  e useTempTable i
1e8b5 73 20 74 72 75 65 20 2a 2f 0a 20 20 20 20 20 20  s true */.      
1e8b6 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
1e8b7 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61  pParse, pList->a
1e8b8 5b 6b 65 79 43 6f 6c 75 6d 6e 5d 2e 70 45 78 70  [keyColumn].pExp
1e8b9 72 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  r, regRowid);.  
1e8ba 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33      j1 = sqlite3
1e8bb 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
1e8bc 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 52 6f 77  _NotNull, regRow
1e8bd 69 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  id);.      sqlit
1e8be 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1e8bf 4f 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20  OP_Integer, -1, 
1e8c0 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  regRowid);.     
1e8c1 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
1e8c2 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20  Here(v, j1);.   
1e8c3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1e8c4 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42  dOp1(v, OP_MustB
1e8c5 65 49 6e 74 2c 20 72 65 67 52 6f 77 69 64 29 3b  eInt, regRowid);
1e8c6 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43  .    }..    /* C
1e8c7 61 6e 6e 6f 74 20 68 61 76 65 20 74 72 69 67 67  annot have trigg
1e8c8 65 72 73 20 6f 6e 20 61 20 76 69 72 74 75 61 6c  ers on a virtual
1e8c9 20 74 61 62 6c 65 2e 20 49 66 20 69 74 20 77 65   table. If it we
1e8ca 72 65 20 70 6f 73 73 69 62 6c 65 2c 0a 20 20 20  re possible,.   
1e8cb 20 2a 2a 20 74 68 69 73 20 62 6c 6f 63 6b 20 77   ** this block w
1e8cc 6f 75 6c 64 20 68 61 76 65 20 74 6f 20 61 63 63  ould have to acc
1e8cd 6f 75 6e 74 20 66 6f 72 20 68 69 64 64 65 6e 20  ount for hidden 
1e8ce 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20  column..    */. 
1e8cf 20 20 20 61 73 73 65 72 74 28 21 49 73 56 69 72     assert(!IsVir
1e8d0 74 75 61 6c 28 70 54 61 62 29 29 3b 0a 0a 20 20  tual(pTab));..  
1e8d1 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20    /* Create the 
1e8d2 6e 65 77 20 63 6f 6c 75 6d 6e 20 64 61 74 61 0a  new column data.
1e8d3 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 67 43 6f      */.    regCo
1e8d4 6c 73 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  ls = sqlite3GetT
1e8d5 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
1e8d6 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20   pTab->nCol);.  
1e8d7 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61    for(i=0; i<pTa
1e8d8 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20  b->nCol; i++){. 
1e8d9 20 20 20 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e       if( pColumn
1e8da 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6a  ==0 ){.        j
1e8db 20 3d 20 69 3b 0a 20 20 20 20 20 20 7d 65 6c 73   = i;.      }els
1e8dc 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a  e{.        for(j
1e8dd 3d 30 3b 20 6a 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e  =0; j<pColumn->n
1e8de 49 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  Id; j++){.      
1e8df 20 20 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 2d      if( pColumn-
1e8e0 3e 61 5b 6a 5d 2e 69 64 78 3d 3d 69 20 29 20 62  >a[j].idx==i ) b
1e8e1 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
1e8e2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
1e8e3 28 20 70 43 6f 6c 75 6d 6e 20 26 26 20 6a 3e 3d  ( pColumn && j>=
1e8e4 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 20 29 7b 0a  pColumn->nId ){.
1e8e5 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
1e8e6 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20  xprCode(pParse, 
1e8e7 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44  pTab->aCol[i].pD
1e8e8 66 6c 74 2c 20 72 65 67 43 6f 6c 73 2b 69 29 3b  flt, regCols+i);
1e8e9 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
1e8ea 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 29 7b   useTempTable ){
1e8eb 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1e8ec 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
1e8ed 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c  _Column, srcTab,
1e8ee 20 6a 2c 20 72 65 67 43 6f 6c 73 2b 69 29 3b 20   j, regCols+i); 
1e8ef 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1e8f0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53        assert( pS
1e8f1 65 6c 65 63 74 3d 3d 30 20 29 3b 20 2f 2a 20 4f  elect==0 ); /* O
1e8f2 74 68 65 72 77 69 73 65 20 75 73 65 54 65 6d 70  therwise useTemp
1e8f3 54 61 62 6c 65 20 69 73 20 74 72 75 65 20 2a 2f  Table is true */
1e8f4 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1e8f5 45 78 70 72 43 6f 64 65 41 6e 64 43 61 63 68 65  ExprCodeAndCache
1e8f6 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e  (pParse, pList->
1e8f7 61 5b 6a 5d 2e 70 45 78 70 72 2c 20 72 65 67 43  a[j].pExpr, regC
1e8f8 6f 6c 73 2b 69 29 3b 0a 20 20 20 20 20 20 7d 0a  ols+i);.      }.
1e8f9 20 20 20 20 7d 0a 20 20 20 20 72 65 67 52 65 63      }.    regRec
1e8fa 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
1e8fb 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
1e8fc 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1e8fd 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op3(v, OP_MakeRe
1e8fe 63 6f 72 64 2c 20 72 65 67 43 6f 6c 73 2c 20 70  cord, regCols, p
1e8ff 54 61 62 2d 3e 6e 43 6f 6c 2c 20 72 65 67 52 65  Tab->nCol, regRe
1e900 63 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74  c);..    /* If t
1e901 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54  his is an INSERT
1e902 20 6f 6e 20 61 20 76 69 65 77 20 77 69 74 68 20   on a view with 
1e903 61 6e 20 49 4e 53 54 45 41 44 20 4f 46 20 49 4e  an INSTEAD OF IN
1e904 53 45 52 54 20 74 72 69 67 67 65 72 2c 0a 20 20  SERT trigger,.  
1e905 20 20 2a 2a 20 64 6f 20 6e 6f 74 20 61 74 74 65    ** do not atte
1e906 6d 70 74 20 61 6e 79 20 63 6f 6e 76 65 72 73 69  mpt any conversi
1e907 6f 6e 73 20 62 65 66 6f 72 65 20 61 73 73 65 6d  ons before assem
1e908 62 6c 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64  bling the record
1e909 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73  ..    ** If this
1e90a 20 69 73 20 61 20 72 65 61 6c 20 74 61 62 6c 65   is a real table
1e90b 2c 20 61 74 74 65 6d 70 74 20 63 6f 6e 76 65 72  , attempt conver
1e90c 73 69 6f 6e 73 20 61 73 20 72 65 71 75 69 72 65  sions as require
1e90d 64 20 62 79 20 74 68 65 0a 20 20 20 20 2a 2a 20  d by the.    ** 
1e90e 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 61 66 66  table column aff
1e90f 69 6e 69 74 69 65 73 2e 0a 20 20 20 20 2a 2f 0a  inities..    */.
1e910 20 20 20 20 69 66 28 20 21 69 73 56 69 65 77 20      if( !isView 
1e911 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1e912 54 61 62 6c 65 41 66 66 69 6e 69 74 79 53 74 72  TableAffinityStr
1e913 28 76 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 7d  (v, pTab);.    }
1e914 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1e915 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73  AddOp3(v, OP_Ins
1e916 65 72 74 2c 20 6e 65 77 49 64 78 2c 20 72 65 67  ert, newIdx, reg
1e917 52 65 63 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  Rec, regRowid);.
1e918 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
1e919 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
1e91a 2c 20 72 65 67 52 65 63 29 3b 0a 20 20 20 20 73  , regRec);.    s
1e91b 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
1e91c 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
1e91d 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69  Rowid);.    sqli
1e91e 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61  te3ReleaseTempRa
1e91f 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 43  nge(pParse, regC
1e920 6f 6c 73 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29  ols, pTab->nCol)
1e921 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 72 65 20 42  ;..    /* Fire B
1e922 45 46 4f 52 45 20 6f 72 20 49 4e 53 54 45 41 44  EFORE or INSTEAD
1e923 20 4f 46 20 74 72 69 67 67 65 72 73 20 2a 2f 0a   OF triggers */.
1e924 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 43      if( sqlite3C
1e925 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 70 50  odeRowTrigger(pP
1e926 61 72 73 65 2c 20 54 4b 5f 49 4e 53 45 52 54 2c  arse, TK_INSERT,
1e927 20 30 2c 20 54 52 49 47 47 45 52 5f 42 45 46 4f   0, TRIGGER_BEFO
1e928 52 45 2c 20 70 54 61 62 2c 20 0a 20 20 20 20 20  RE, pTab, .     
1e929 20 20 20 6e 65 77 49 64 78 2c 20 2d 31 2c 20 6f     newIdx, -1, o
1e92a 6e 45 72 72 6f 72 2c 20 65 6e 64 4f 66 4c 6f 6f  nError, endOfLoo
1e92b 70 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20  p, 0, 0) ){.    
1e92c 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c    goto insert_cl
1e92d 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 7d  eanup;.    }.  }
1e92e 0a 0a 20 20 2f 2a 20 50 75 73 68 20 74 68 65 20  ..  /* Push the 
1e92f 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 66 6f  record number fo
1e930 72 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20  r the new entry 
1e931 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 2e 20  onto the stack. 
1e932 20 54 68 65 0a 20 20 2a 2a 20 72 65 63 6f 72 64   The.  ** record
1e933 20 6e 75 6d 62 65 72 20 69 73 20 61 20 72 61 6e   number is a ran
1e934 64 6f 6d 6c 79 20 67 65 6e 65 72 61 74 65 20 69  domly generate i
1e935 6e 74 65 67 65 72 20 63 72 65 61 74 65 64 20 62  nteger created b
1e936 79 20 4e 65 77 52 6f 77 69 64 0a 20 20 2a 2a 20  y NewRowid.  ** 
1e937 65 78 63 65 70 74 20 77 68 65 6e 20 74 68 65 20  except when the 
1e938 74 61 62 6c 65 20 68 61 73 20 61 6e 20 49 4e 54  table has an INT
1e939 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
1e93a 20 63 6f 6c 75 6d 6e 2c 20 69 6e 20 77 68 69 63   column, in whic
1e93b 68 0a 20 20 2a 2a 20 63 61 73 65 20 74 68 65 20  h.  ** case the 
1e93c 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73  record number is
1e93d 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 61   the same as tha
1e93e 74 20 63 6f 6c 75 6d 6e 2e 20 0a 20 20 2a 2f 0a  t column. .  */.
1e93f 20 20 69 66 28 20 21 69 73 56 69 65 77 20 29 7b    if( !isView ){
1e940 0a 20 20 20 20 69 66 28 20 49 73 56 69 72 74 75  .    if( IsVirtu
1e941 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
1e942 20 20 2f 2a 20 54 68 65 20 72 6f 77 20 74 68 61    /* The row tha
1e943 74 20 74 68 65 20 56 55 70 64 61 74 65 20 6f 70  t the VUpdate op
1e944 63 6f 64 65 20 77 69 6c 6c 20 64 65 6c 65 74 65  code will delete
1e945 3a 20 6e 6f 6e 65 20 2a 2f 0a 20 20 20 20 20 20  : none */.      
1e946 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1e947 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
1e948 20 72 65 67 49 6e 73 29 3b 0a 20 20 20 20 7d 0a   regIns);.    }.
1e949 20 20 20 20 69 66 28 20 6b 65 79 43 6f 6c 75 6d      if( keyColum
1e94a 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66  n>=0 ){.      if
1e94b 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 29  ( useTempTable )
1e94c 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1e94d 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
1e94e 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63 54 61 62  P_Column, srcTab
1e94f 2c 20 6b 65 79 43 6f 6c 75 6d 6e 2c 20 72 65 67  , keyColumn, reg
1e950 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 7d 65  Rowid);.      }e
1e951 6c 73 65 20 69 66 28 20 70 53 65 6c 65 63 74 20  lse if( pSelect 
1e952 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1e953 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1e954 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 46 72 6f  OP_SCopy, regFro
1e955 6d 53 65 6c 65 63 74 2b 6b 65 79 43 6f 6c 75 6d  mSelect+keyColum
1e956 6e 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  n, regRowid);.  
1e957 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1e958 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a     VdbeOp *pOp;.
1e959 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
1e95a 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20  xprCode(pParse, 
1e95b 70 4c 69 73 74 2d 3e 61 5b 6b 65 79 43 6f 6c 75  pList->a[keyColu
1e95c 6d 6e 5d 2e 70 45 78 70 72 2c 20 72 65 67 52 6f  mn].pExpr, regRo
1e95d 77 69 64 29 3b 0a 20 20 20 20 20 20 20 20 70 4f  wid);.        pO
1e95e 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 47  p = sqlite3VdbeG
1e95f 65 74 4f 70 28 76 2c 20 73 71 6c 69 74 65 33 56  etOp(v, sqlite3V
1e960 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
1e961 29 20 2d 20 31 29 3b 0a 20 20 20 20 20 20 20 20  ) - 1);.        
1e962 69 66 28 20 70 4f 70 20 26 26 20 70 4f 70 2d 3e  if( pOp && pOp->
1e963 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 75 6c 6c 20  opcode==OP_Null 
1e964 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 70 70  ){.          app
1e965 65 6e 64 46 6c 61 67 20 3d 20 31 3b 0a 20 20 20  endFlag = 1;.   
1e966 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f         pOp->opco
1e967 64 65 20 3d 20 4f 50 5f 4e 65 77 52 6f 77 69 64  de = OP_NewRowid
1e968 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d  ;.          pOp-
1e969 3e 70 31 20 3d 20 62 61 73 65 43 75 72 3b 0a 20  >p1 = baseCur;. 
1e96a 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 32           pOp->p2
1e96b 20 3d 20 72 65 67 52 6f 77 69 64 3b 0a 20 20 20   = regRowid;.   
1e96c 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 33 20 3d         pOp->p3 =
1e96d 20 72 65 67 41 75 74 6f 69 6e 63 3b 0a 20 20 20   regAutoinc;.   
1e96e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
1e96f 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50       /* If the P
1e970 52 49 4d 41 52 59 20 4b 45 59 20 65 78 70 72 65  RIMARY KEY expre
1e971 73 73 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 74  ssion is NULL, t
1e972 68 65 6e 20 75 73 65 20 4f 50 5f 4e 65 77 52 6f  hen use OP_NewRo
1e973 77 69 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20  wid.      ** to 
1e974 67 65 6e 65 72 61 74 65 20 61 20 75 6e 69 71 75  generate a uniqu
1e975 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 76 61  e primary key va
1e976 6c 75 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  lue..      */.  
1e977 20 20 20 20 69 66 28 20 21 61 70 70 65 6e 64 46      if( !appendF
1e978 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20 69  lag ){.        i
1e979 6e 74 20 6a 31 3b 0a 20 20 20 20 20 20 20 20 6a  nt j1;.        j
1e97a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
1e97b 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e  ddOp1(v, OP_NotN
1e97c 75 6c 6c 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  ull, regRowid);.
1e97d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1e97e 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
1e97f 4e 65 77 52 6f 77 69 64 2c 20 62 61 73 65 43 75  NewRowid, baseCu
1e980 72 2c 20 72 65 67 52 6f 77 69 64 2c 20 72 65 67  r, regRowid, reg
1e981 41 75 74 6f 69 6e 63 29 3b 0a 20 20 20 20 20 20  Autoinc);.      
1e982 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
1e983 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20  pHere(v, j1);.  
1e984 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1e985 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75  eAddOp1(v, OP_Mu
1e986 73 74 42 65 49 6e 74 2c 20 72 65 67 52 6f 77 69  stBeInt, regRowi
1e987 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  d);.      }.    
1e988 7d 65 6c 73 65 20 69 66 28 20 49 73 56 69 72 74  }else if( IsVirt
1e989 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
1e98a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1e98b 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
1e98c 20 30 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20   0, regRowid);. 
1e98d 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1e98e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1e98f 33 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64  3(v, OP_NewRowid
1e990 2c 20 62 61 73 65 43 75 72 2c 20 72 65 67 52 6f  , baseCur, regRo
1e991 77 69 64 2c 20 72 65 67 41 75 74 6f 69 6e 63 29  wid, regAutoinc)
1e992 3b 0a 20 20 20 20 20 20 61 70 70 65 6e 64 46 6c  ;.      appendFl
1e993 61 67 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  ag = 1;.    }.  
1e994 20 20 61 75 74 6f 49 6e 63 53 74 65 70 28 70 50    autoIncStep(pP
1e995 61 72 73 65 2c 20 72 65 67 41 75 74 6f 69 6e 63  arse, regAutoinc
1e996 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 0a 20 20  , regRowid);..  
1e997 20 20 2f 2a 20 50 75 73 68 20 6f 6e 74 6f 20 74    /* Push onto t
1e998 68 65 20 73 74 61 63 6b 2c 20 64 61 74 61 20 66  he stack, data f
1e999 6f 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 6f  or all columns o
1e99a 66 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 2c  f the new entry,
1e99b 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 20 20 2a   beginning.    *
1e99c 2a 20 77 69 74 68 20 74 68 65 20 66 69 72 73 74  * with the first
1e99d 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 2a 2f 0a   column..    */.
1e99e 20 20 20 20 6e 48 69 64 64 65 6e 20 3d 20 30 3b      nHidden = 0;
1e99f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
1e9a0 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29  pTab->nCol; i++)
1e9a1 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 52 65 67  {.      int iReg
1e9a2 53 74 6f 72 65 20 3d 20 72 65 67 52 6f 77 69 64  Store = regRowid
1e9a3 2b 31 2b 69 3b 0a 20 20 20 20 20 20 69 66 28 20  +1+i;.      if( 
1e9a4 69 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29  i==pTab->iPKey )
1e9a5 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  {.        /* The
1e9a6 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 49 4e   value of the IN
1e9a7 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
1e9a8 59 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6c 77 61  Y column is alwa
1e9a9 79 73 20 61 20 4e 55 4c 4c 2e 0a 20 20 20 20 20  ys a NULL..     
1e9aa 20 20 20 2a 2a 20 57 68 65 6e 65 76 65 72 20 74     ** Whenever t
1e9ab 68 69 73 20 63 6f 6c 75 6d 6e 20 69 73 20 72 65  his column is re
1e9ac 61 64 2c 20 74 68 65 20 72 65 63 6f 72 64 20 6e  ad, the record n
1e9ad 75 6d 62 65 72 20 77 69 6c 6c 20 62 65 20 73 75  umber will be su
1e9ae 62 73 74 69 74 75 74 65 64 0a 20 20 20 20 20 20  bstituted.      
1e9af 20 20 2a 2a 20 69 6e 20 69 74 73 20 70 6c 61 63    ** in its plac
1e9b0 65 2e 20 20 53 6f 20 77 69 6c 6c 20 66 69 6c 6c  e.  So will fill
1e9b1 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 77 69 74   this column wit
1e9b2 68 20 61 20 4e 55 4c 4c 20 74 6f 20 61 76 6f 69  h a NULL to avoi
1e9b3 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 61 6b  d.        ** tak
1e9b4 69 6e 67 20 75 70 20 64 61 74 61 20 73 70 61 63  ing up data spac
1e9b5 65 20 77 69 74 68 20 69 6e 66 6f 72 6d 61 74 69  e with informati
1e9b6 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 6e 65 76  on that will nev
1e9b7 65 72 20 62 65 20 75 73 65 64 2e 20 2a 2f 0a 20  er be used. */. 
1e9b8 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1e9b9 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
1e9ba 75 6c 6c 2c 20 30 2c 20 69 52 65 67 53 74 6f 72  ull, 0, iRegStor
1e9bb 65 29 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74  e);.        cont
1e9bc 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  inue;.      }.  
1e9bd 20 20 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d      if( pColumn=
1e9be 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  =0 ){.        if
1e9bf 28 20 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e  ( IsHiddenColumn
1e9c0 28 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 29  (&pTab->aCol[i])
1e9c1 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73   ){.          as
1e9c2 73 65 72 74 28 20 49 73 56 69 72 74 75 61 6c 28  sert( IsVirtual(
1e9c3 70 54 61 62 29 20 29 3b 0a 20 20 20 20 20 20 20  pTab) );.       
1e9c4 20 20 20 6a 20 3d 20 2d 31 3b 0a 20 20 20 20 20     j = -1;.     
1e9c5 20 20 20 20 20 6e 48 69 64 64 65 6e 2b 2b 3b 0a       nHidden++;.
1e9c6 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
1e9c7 20 20 20 20 20 20 20 20 20 6a 20 3d 20 69 20 2d           j = i -
1e9c8 20 6e 48 69 64 64 65 6e 3b 0a 20 20 20 20 20 20   nHidden;.      
1e9c9 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b    }.      }else{
1e9ca 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  .        for(j=0
1e9cb 3b 20 6a 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64  ; j<pColumn->nId
1e9cc 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
1e9cd 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 2d 3e 61    if( pColumn->a
1e9ce 5b 6a 5d 2e 69 64 78 3d 3d 69 20 29 20 62 72 65  [j].idx==i ) bre
1e9cf 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
1e9d0 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
1e9d1 6a 3c 30 20 7c 7c 20 6e 43 6f 6c 75 6d 6e 3d 3d  j<0 || nColumn==
1e9d2 30 20 7c 7c 20 28 70 43 6f 6c 75 6d 6e 20 26 26  0 || (pColumn &&
1e9d3 20 6a 3e 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64   j>=pColumn->nId
1e9d4 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  ) ){.        sql
1e9d5 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
1e9d6 72 73 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  rse, pTab->aCol[
1e9d7 69 5d 2e 70 44 66 6c 74 2c 20 69 52 65 67 53 74  i].pDflt, iRegSt
1e9d8 6f 72 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ore);.      }els
1e9d9 65 20 69 66 28 20 75 73 65 54 65 6d 70 54 61 62  e if( useTempTab
1e9da 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  le ){.        sq
1e9db 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
1e9dc 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72  v, OP_Column, sr
1e9dd 63 54 61 62 2c 20 6a 2c 20 69 52 65 67 53 74 6f  cTab, j, iRegSto
1e9de 72 65 29 3b 20 0a 20 20 20 20 20 20 7d 65 6c 73  re); .      }els
1e9df 65 20 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b  e if( pSelect ){
1e9e0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1e9e1 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1e9e2 5f 53 43 6f 70 79 2c 20 72 65 67 46 72 6f 6d 53  _SCopy, regFromS
1e9e3 65 6c 65 63 74 2b 6a 2c 20 69 52 65 67 53 74 6f  elect+j, iRegSto
1e9e4 72 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  re);.      }else
1e9e5 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1e9e6 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
1e9e7 2c 20 70 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 45  , pList->a[j].pE
1e9e8 78 70 72 2c 20 69 52 65 67 53 74 6f 72 65 29 3b  xpr, iRegStore);
1e9e9 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
1e9ea 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20      /* Generate 
1e9eb 63 6f 64 65 20 74 6f 20 63 68 65 63 6b 20 63 6f  code to check co
1e9ec 6e 73 74 72 61 69 6e 74 73 20 61 6e 64 20 67 65  nstraints and ge
1e9ed 6e 65 72 61 74 65 20 69 6e 64 65 78 20 6b 65 79  nerate index key
1e9ee 73 20 61 6e 64 0a 20 20 20 20 2a 2a 20 64 6f 20  s and.    ** do 
1e9ef 74 68 65 20 69 6e 73 65 72 74 69 6f 6e 2e 0a 20  the insertion.. 
1e9f0 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51     */.#ifndef SQ
1e9f1 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1e9f2 4c 54 41 42 4c 45 0a 20 20 20 20 69 66 28 20 49  LTABLE.    if( I
1e9f3 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
1e9f4 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
1e9f5 74 61 62 4d 61 6b 65 57 72 69 74 61 62 6c 65 28  tabMakeWritable(
1e9f6 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20  pParse, pTab);. 
1e9f7 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1e9f8 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 55 70  AddOp4(v, OP_VUp
1e9f9 64 61 74 65 2c 20 31 2c 20 70 54 61 62 2d 3e 6e  date, 1, pTab->n
1e9fa 43 6f 6c 2b 32 2c 20 72 65 67 49 6e 73 2c 0a 20  Col+2, regIns,. 
1e9fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e9fc 20 20 20 20 28 63 6f 6e 73 74 20 63 68 61 72 2a      (const char*
1e9fd 29 70 54 61 62 2d 3e 70 56 74 61 62 2c 20 50 34  )pTab->pVtab, P4
1e9fe 5f 56 54 41 42 29 3b 0a 20 20 20 20 7d 65 6c 73  _VTAB);.    }els
1e9ff 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20  e.#endif.    {. 
1ea00 20 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65       sqlite3Gene
1ea01 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68  rateConstraintCh
1ea02 65 63 6b 73 28 0a 20 20 20 20 20 20 20 20 20 20  ecks(.          
1ea03 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20  pParse,.        
1ea04 20 20 70 54 61 62 2c 0a 20 20 20 20 20 20 20 20    pTab,.        
1ea05 20 20 62 61 73 65 43 75 72 2c 0a 20 20 20 20 20    baseCur,.     
1ea06 20 20 20 20 20 72 65 67 49 6e 73 2c 0a 20 20 20       regIns,.   
1ea07 20 20 20 20 20 20 20 61 52 65 67 49 64 78 2c 0a         aRegIdx,.
1ea08 20 20 20 20 20 20 20 20 20 20 6b 65 79 43 6f 6c            keyCol
1ea09 75 6d 6e 3e 3d 30 2c 0a 20 20 20 20 20 20 20 20  umn>=0,.        
1ea0a 20 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 6f    0,.          o
1ea0b 6e 45 72 72 6f 72 2c 0a 20 20 20 20 20 20 20 20  nError,.        
1ea0c 20 20 65 6e 64 4f 66 4c 6f 6f 70 0a 20 20 20 20    endOfLoop.    
1ea0d 20 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74    );.      sqlit
1ea0e 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73 65 72 74  e3CompleteInsert
1ea0f 69 6f 6e 28 0a 20 20 20 20 20 20 20 20 20 20 70  ion(.          p
1ea10 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20  Parse,.         
1ea11 20 70 54 61 62 2c 0a 20 20 20 20 20 20 20 20 20   pTab,.         
1ea12 20 62 61 73 65 43 75 72 2c 0a 20 20 20 20 20 20   baseCur,.      
1ea13 20 20 20 20 72 65 67 49 6e 73 2c 0a 20 20 20 20      regIns,.    
1ea14 20 20 20 20 20 20 61 52 65 67 49 64 78 2c 0a 20        aRegIdx,. 
1ea15 20 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20 20           0,.    
1ea16 20 20 20 20 20 20 30 2c 0a 20 20 20 20 20 20 20        0,.       
1ea17 20 20 20 28 74 72 69 67 67 65 72 73 5f 65 78 69     (triggers_exi
1ea18 73 74 20 26 20 54 52 49 47 47 45 52 5f 41 46 54  st & TRIGGER_AFT
1ea19 45 52 29 21 3d 30 20 3f 20 6e 65 77 49 64 78 20  ER)!=0 ? newIdx 
1ea1a 3a 20 2d 31 2c 0a 20 20 20 20 20 20 20 20 20 20  : -1,.          
1ea1b 61 70 70 65 6e 64 46 6c 61 67 0a 20 20 20 20 20  appendFlag.     
1ea1c 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a    );.    }.  }..
1ea1d 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20    /* Update the 
1ea1e 63 6f 75 6e 74 20 6f 66 20 72 6f 77 73 20 74 68  count of rows th
1ea1f 61 74 20 61 72 65 20 69 6e 73 65 72 74 65 64 0a  at are inserted.
1ea20 20 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e    */.  if( (db->
1ea21 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43  flags & SQLITE_C
1ea22 6f 75 6e 74 52 6f 77 73 29 21 3d 30 20 29 7b 0a  ountRows)!=0 ){.
1ea23 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1ea24 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
1ea25 6d 6d 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74 2c  mm, regRowCount,
1ea26 20 31 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20   1);.  }..  if( 
1ea27 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 29  triggers_exist )
1ea28 7b 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20 41 46  {.    /* Code AF
1ea29 54 45 52 20 74 72 69 67 67 65 72 73 20 2a 2f 0a  TER triggers */.
1ea2a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 43      if( sqlite3C
1ea2b 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 70 50  odeRowTrigger(pP
1ea2c 61 72 73 65 2c 20 54 4b 5f 49 4e 53 45 52 54 2c  arse, TK_INSERT,
1ea2d 20 30 2c 20 54 52 49 47 47 45 52 5f 41 46 54 45   0, TRIGGER_AFTE
1ea2e 52 2c 20 70 54 61 62 2c 0a 20 20 20 20 20 20 20  R, pTab,.       
1ea2f 20 20 20 6e 65 77 49 64 78 2c 20 2d 31 2c 20 6f     newIdx, -1, o
1ea30 6e 45 72 72 6f 72 2c 20 65 6e 64 4f 66 4c 6f 6f  nError, endOfLoo
1ea31 70 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20  p, 0, 0) ){.    
1ea32 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c    goto insert_cl
1ea33 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 7d  eanup;.    }.  }
1ea34 0a 0a 20 20 2f 2a 20 54 68 65 20 62 6f 74 74 6f  ..  /* The botto
1ea35 6d 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2c 20 69  m of the loop, i
1ea36 66 20 74 68 65 20 64 61 74 61 20 73 6f 75 72 63  f the data sourc
1ea37 65 20 69 73 20 61 20 53 45 4c 45 43 54 20 73 74  e is a SELECT st
1ea38 61 74 65 6d 65 6e 74 0a 20 20 2a 2f 0a 20 20 73  atement.  */.  s
1ea39 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
1ea3a 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4f 66 4c  eLabel(v, endOfL
1ea3b 6f 6f 70 29 3b 0a 20 20 69 66 28 20 75 73 65 54  oop);.  if( useT
1ea3c 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  empTable ){.    
1ea3d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1ea3e 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 73 72  2(v, OP_Next, sr
1ea3f 63 54 61 62 2c 20 69 43 6f 6e 74 29 3b 0a 20 20  cTab, iCont);.  
1ea40 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
1ea41 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 42 72  olveLabel(v, iBr
1ea42 65 61 6b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  eak);.    sqlite
1ea43 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1ea44 50 5f 43 6c 6f 73 65 2c 20 73 72 63 54 61 62 2c  P_Close, srcTab,
1ea45 20 30 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28   0);.  }else if(
1ea46 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20   pSelect ){.    
1ea47 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1ea48 32 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c 20  2(v, OP_Return, 
1ea49 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
1ea4a 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
1ea4b 65 6c 28 76 2c 20 69 43 6c 65 61 6e 75 70 29 3b  el(v, iCleanup);
1ea4c 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 49 73 56  .  }..  if( !IsV
1ea4d 69 72 74 75 61 6c 28 70 54 61 62 29 20 26 26 20  irtual(pTab) && 
1ea4e 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 2f  !isView ){.    /
1ea4f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 74 61 62 6c  * Close all tabl
1ea50 65 73 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 20  es opened */.   
1ea51 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1ea52 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p2(v, OP_Close, 
1ea53 62 61 73 65 43 75 72 2c 20 30 29 3b 0a 20 20 20  baseCur, 0);.   
1ea54 20 66 6f 72 28 69 64 78 3d 31 2c 20 70 49 64 78   for(idx=1, pIdx
1ea55 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
1ea56 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
1ea57 70 4e 65 78 74 2c 20 69 64 78 2b 2b 29 7b 0a 20  pNext, idx++){. 
1ea58 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1ea59 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp2(v, OP_Clo
1ea5a 73 65 2c 20 69 64 78 2b 62 61 73 65 43 75 72 2c  se, idx+baseCur,
1ea5b 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a   0);.    }.  }..
1ea5c 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20    /* Update the 
1ea5d 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20  sqlite_sequence 
1ea5e 74 61 62 6c 65 20 62 79 20 73 74 6f 72 69 6e 67  table by storing
1ea5f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
1ea60 74 68 65 0a 20 20 2a 2a 20 63 6f 75 6e 74 65 72  the.  ** counter
1ea61 20 76 61 6c 75 65 20 69 6e 20 6d 65 6d 6f 72 79   value in memory
1ea62 20 72 65 67 41 75 74 6f 69 6e 63 20 62 61 63 6b   regAutoinc back
1ea63 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65   into the sqlite
1ea64 5f 73 65 71 75 65 6e 63 65 0a 20 20 2a 2a 20 74  _sequence.  ** t
1ea65 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 61 75 74  able..  */.  aut
1ea66 6f 49 6e 63 45 6e 64 28 70 50 61 72 73 65 2c 20  oIncEnd(pParse, 
1ea67 69 44 62 2c 20 70 54 61 62 2c 20 72 65 67 41 75  iDb, pTab, regAu
1ea68 74 6f 69 6e 63 29 3b 0a 0a 20 20 2f 2a 0a 20 20  toinc);..  /*.  
1ea69 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
1ea6a 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 73  mber of rows ins
1ea6b 65 72 74 65 64 2e 20 49 66 20 74 68 69 73 20 72  erted. If this r
1ea6c 6f 75 74 69 6e 65 20 69 73 20 0a 20 20 2a 2a 20  outine is .  ** 
1ea6d 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 20  generating code 
1ea6e 62 65 63 61 75 73 65 20 6f 66 20 61 20 63 61 6c  because of a cal
1ea6f 6c 20 74 6f 20 73 71 6c 69 74 65 33 4e 65 73 74  l to sqlite3Nest
1ea70 65 64 50 61 72 73 65 28 29 2c 20 64 6f 20 6e 6f  edParse(), do no
1ea71 74 0a 20 20 2a 2a 20 69 6e 76 6f 6b 65 20 74 68  t.  ** invoke th
1ea72 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  e callback funct
1ea73 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ion..  */.  if( 
1ea74 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
1ea75 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 26 26 20  TE_CountRows && 
1ea76 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d  pParse->nested==
1ea77 30 20 26 26 20 21 70 50 61 72 73 65 2d 3e 74 72  0 && !pParse->tr
1ea78 69 67 53 74 61 63 6b 20 29 7b 0a 20 20 20 20 73  igStack ){.    s
1ea79 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1ea7a 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
1ea7b 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74 2c 20 31  , regRowCount, 1
1ea7c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1ea7d 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
1ea7e 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
1ea7f 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
1ea80 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
1ea81 2c 20 22 72 6f 77 73 20 69 6e 73 65 72 74 65 64  , "rows inserted
1ea82 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  ", P4_STATIC);. 
1ea83 20 7d 0a 0a 69 6e 73 65 72 74 5f 63 6c 65 61 6e   }..insert_clean
1ea84 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63  up:.  sqlite3Src
1ea85 4c 69 73 74 44 65 6c 65 74 65 28 70 54 61 62 4c  ListDelete(pTabL
1ea86 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ist);.  sqlite3E
1ea87 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 4c  xprListDelete(pL
1ea88 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  ist);.  sqlite3S
1ea89 65 6c 65 63 74 44 65 6c 65 74 65 28 70 53 65 6c  electDelete(pSel
1ea8a 65 63 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 49  ect);.  sqlite3I
1ea8b 64 4c 69 73 74 44 65 6c 65 74 65 28 70 43 6f 6c  dListDelete(pCol
1ea8c 75 6d 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  umn);.  sqlite3_
1ea8d 66 72 65 65 28 61 52 65 67 49 64 78 29 3b 0a 7d  free(aRegIdx);.}
1ea8e 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
1ea8f 20 63 6f 64 65 20 74 6f 20 64 6f 20 63 6f 6e 73   code to do cons
1ea90 74 72 61 69 6e 74 20 63 68 65 63 6b 73 20 70 72  traint checks pr
1ea91 69 6f 72 20 74 6f 20 61 6e 20 49 4e 53 45 52 54  ior to an INSERT
1ea92 20 6f 72 20 61 6e 20 55 50 44 41 54 45 2e 0a 2a   or an UPDATE..*
1ea93 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 69  *.** The input i
1ea94 73 20 61 20 72 61 6e 67 65 20 6f 66 20 63 6f 6e  s a range of con
1ea95 73 65 63 75 74 69 76 65 20 72 65 67 69 73 74 65  secutive registe
1ea96 72 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  rs as follows:.*
1ea97 2a 0a 2a 2a 20 20 20 20 31 2e 20 20 54 68 65 20  *.**    1.  The 
1ea98 72 6f 77 69 64 20 6f 66 20 74 68 65 20 72 6f 77  rowid of the row
1ea99 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 62   to be updated b
1ea9a 65 66 6f 72 65 20 74 68 65 20 75 70 64 61 74 65  efore the update
1ea9b 2e 20 20 54 68 69 73 0a 2a 2a 20 20 20 20 20 20  .  This.**      
1ea9c 20 20 76 61 6c 75 65 20 69 73 20 6f 6d 69 74 74    value is omitt
1ea9d 65 64 20 75 6e 6c 65 73 73 20 77 65 20 61 72 65  ed unless we are
1ea9e 20 64 6f 69 6e 67 20 61 6e 20 55 50 44 41 54 45   doing an UPDATE
1ea9f 20 74 68 61 74 20 69 6e 76 6f 6c 76 65 73 20 61   that involves a
1eaa0 0a 2a 2a 20 20 20 20 20 20 20 20 63 68 61 6e 67  .**        chang
1eaa1 65 20 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20  e to the record 
1eaa2 6e 75 6d 62 65 72 20 6f 72 20 77 72 69 74 69 6e  number or writin
1eaa3 67 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 74  g to a virtual t
1eaa4 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 32  able..**.**    2
1eaa5 2e 20 20 54 68 65 20 72 6f 77 69 64 20 6f 66 20  .  The rowid of 
1eaa6 74 68 65 20 72 6f 77 20 61 66 74 65 72 20 74 68  the row after th
1eaa7 65 20 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20  e update..**.** 
1eaa8 20 20 20 33 2e 20 20 54 68 65 20 64 61 74 61 20     3.  The data 
1eaa9 69 6e 20 74 68 65 20 66 69 72 73 74 20 63 6f 6c  in the first col
1eaaa 75 6d 6e 20 6f 66 20 74 68 65 20 65 6e 74 72 79  umn of the entry
1eaab 20 61 66 74 65 72 20 74 68 65 20 75 70 64 61 74   after the updat
1eaac 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 69 2e 20 20  e..**.**    i.  
1eaad 44 61 74 61 20 66 72 6f 6d 20 6d 69 64 64 6c 65  Data from middle
1eaae 20 63 6f 6c 75 6d 6e 73 2e 2e 2e 0a 2a 2a 0a 2a   columns....**.*
1eaaf 2a 20 20 20 20 4e 2e 20 20 54 68 65 20 64 61 74  *    N.  The dat
1eab0 61 20 69 6e 20 74 68 65 20 6c 61 73 74 20 63 6f  a in the last co
1eab1 6c 75 6d 6e 20 6f 66 20 74 68 65 20 65 6e 74 72  lumn of the entr
1eab2 79 20 61 66 74 65 72 20 74 68 65 20 75 70 64 61  y after the upda
1eab3 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  te..**.** The re
1eab4 67 52 6f 77 69 64 20 70 61 72 61 6d 65 74 65 72  gRowid parameter
1eab5 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66   is the index of
1eab6 20 74 68 65 20 72 65 67 69 73 74 65 72 20 63 6f   the register co
1eab7 6e 74 61 69 6e 69 6e 67 20 28 32 29 2e 0a 2a 2a  ntaining (2)..**
1eab8 0a 2a 2a 20 54 68 65 20 6f 6c 64 20 72 6f 77 69  .** The old rowi
1eab9 64 20 73 68 6f 77 6e 20 61 73 20 65 6e 74 72 79  d shown as entry
1eaba 20 28 31 29 20 61 62 6f 76 65 20 69 73 20 6f 6d   (1) above is om
1eabb 69 74 74 65 64 20 75 6e 6c 65 73 73 20 62 6f 74  itted unless bot
1eabc 68 20 69 73 55 70 64 61 74 65 0a 2a 2a 20 61 6e  h isUpdate.** an
1eabd 64 20 72 6f 77 69 64 43 68 6e 67 20 61 72 65 20  d rowidChng are 
1eabe 31 2e 20 20 69 73 55 70 64 61 74 65 20 69 73 20  1.  isUpdate is 
1eabf 74 72 75 65 20 66 6f 72 20 55 50 44 41 54 45 73  true for UPDATEs
1eac0 20 61 6e 64 20 66 61 6c 73 65 20 66 6f 72 0a 2a   and false for.*
1eac1 2a 20 49 4e 53 45 52 54 73 2e 20 20 52 6f 77 69  * INSERTs.  Rowi
1eac2 64 43 68 6e 67 20 6d 65 61 6e 73 20 74 68 61 74  dChng means that
1eac3 20 74 68 65 20 6e 65 77 20 72 6f 77 69 64 20 69   the new rowid i
1eac4 73 20 65 78 70 6c 69 63 69 74 6c 79 20 73 70 65  s explicitly spe
1eac5 63 69 66 69 65 64 20 62 79 0a 2a 2a 20 74 68 65  cified by.** the
1eac6 20 75 70 64 61 74 65 20 6f 72 20 69 6e 73 65 72   update or inser
1eac7 74 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 66  t statement.  If
1eac8 20 72 6f 77 69 64 43 68 6e 67 20 69 73 20 66 61   rowidChng is fa
1eac9 6c 73 65 2c 20 69 74 20 6d 65 61 6e 73 20 74 68  lse, it means th
1eaca 61 74 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64 20  at.** the rowid 
1eacb 69 73 20 63 6f 6d 70 75 74 65 64 20 61 75 74 6f  is computed auto
1eacc 6d 61 74 69 63 61 6c 6c 79 20 69 6e 20 61 6e 20  matically in an 
1eacd 69 6e 73 65 72 74 20 6f 72 20 74 68 61 74 20 74  insert or that t
1eace 68 65 20 72 6f 77 69 64 20 76 61 6c 75 65 0a 2a  he rowid value.*
1eacf 2a 20 69 73 20 6e 6f 74 20 6d 6f 64 69 66 69 65  * is not modifie
1ead0 64 20 62 79 20 74 68 65 20 75 70 64 61 74 65 2e  d by the update.
1ead1 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  .**.** The code 
1ead2 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 69  generated by thi
1ead3 73 20 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 20  s routine store 
1ead4 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72 69 65  new index entrie
1ead5 73 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74  s into.** regist
1ead6 65 72 73 20 69 64 65 6e 74 69 66 69 65 64 20 62  ers identified b
1ead7 79 20 61 52 65 67 49 64 78 5b 5d 2e 20 20 4e 6f  y aRegIdx[].  No
1ead8 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20   index entry is 
1ead9 63 72 65 61 74 65 64 20 66 6f 72 0a 2a 2a 20 69  created for.** i
1eada 6e 64 69 63 65 73 20 77 68 65 72 65 20 61 52 65  ndices where aRe
1eadb 67 49 64 78 5b 69 5d 3d 3d 30 2e 20 20 54 68 65  gIdx[i]==0.  The
1eadc 20 6f 72 64 65 72 20 6f 66 20 69 6e 64 69 63 65   order of indice
1eadd 73 20 69 6e 20 61 52 65 67 49 64 78 5b 5d 20 69  s in aRegIdx[] i
1eade 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73  s.** the same as
1eadf 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20 69 6e   the order of in
1eae0 64 69 63 65 73 20 6f 6e 20 74 68 65 20 6c 69 6e  dices on the lin
1eae1 6b 65 64 20 6c 69 73 74 20 6f 66 20 69 6e 64 69  ked list of indi
1eae2 63 65 73 0a 2a 2a 20 61 74 74 61 63 68 65 64 20  ces.** attached 
1eae3 74 6f 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a  to the table..**
1eae4 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1eae5 20 61 6c 73 6f 20 67 65 6e 65 72 61 74 65 73 20   also generates 
1eae6 63 6f 64 65 20 74 6f 20 63 68 65 63 6b 20 63 6f  code to check co
1eae7 6e 73 74 72 61 69 6e 74 73 2e 20 20 4e 4f 54 20  nstraints.  NOT 
1eae8 4e 55 4c 4c 2c 0a 2a 2a 20 43 48 45 43 4b 2c 20  NULL,.** CHECK, 
1eae9 61 6e 64 20 55 4e 49 51 55 45 20 63 6f 6e 73 74  and UNIQUE const
1eaea 72 61 69 6e 74 73 20 61 72 65 20 61 6c 6c 20 63  raints are all c
1eaeb 68 65 63 6b 65 64 2e 20 20 49 66 20 61 20 63 6f  hecked.  If a co
1eaec 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 73 2c 0a  nstraint fails,.
1eaed 2a 2a 20 74 68 65 6e 20 74 68 65 20 61 70 70 72  ** then the appr
1eaee 6f 70 72 69 61 74 65 20 61 63 74 69 6f 6e 20 69  opriate action i
1eaef 73 20 70 65 72 66 6f 72 6d 65 64 2e 20 20 54 68  s performed.  Th
1eaf0 65 72 65 20 61 72 65 20 66 69 76 65 20 70 6f 73  ere are five pos
1eaf1 73 69 62 6c 65 0a 2a 2a 20 61 63 74 69 6f 6e 73  sible.** actions
1eaf2 3a 20 52 4f 4c 4c 42 41 43 4b 2c 20 41 42 4f 52  : ROLLBACK, ABOR
1eaf3 54 2c 20 46 41 49 4c 2c 20 52 45 50 4c 41 43 45  T, FAIL, REPLACE
1eaf4 2c 20 61 6e 64 20 49 47 4e 4f 52 45 2e 0a 2a 2a  , and IGNORE..**
1eaf5 0a 2a 2a 20 20 43 6f 6e 73 74 72 61 69 6e 74 20  .**  Constraint 
1eaf6 74 79 70 65 20 20 41 63 74 69 6f 6e 20 20 20 20  type  Action    
1eaf7 20 20 20 57 68 61 74 20 48 61 70 70 65 6e 73 0a     What Happens.
1eaf8 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  **  ------------
1eaf9 2d 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20  ---  ---------- 
1eafa 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
1eafb 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1eafc 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 61  ----------.**  a
1eafd 6e 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ny              
1eafe 52 4f 4c 4c 42 41 43 4b 20 20 20 20 20 54 68 65  ROLLBACK     The
1eaff 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63   current transac
1eb00 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62  tion is rolled b
1eb01 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20  ack and.**      
1eb02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eb03 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1eb04 33 5f 65 78 65 63 28 29 20 72 65 74 75 72 6e 73  3_exec() returns
1eb05 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 77 69 74   immediately wit
1eb06 68 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  h a.**          
1eb07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eb08 20 20 20 20 20 20 72 65 74 75 72 6e 20 63 6f 64        return cod
1eb09 65 20 6f 66 20 53 51 4c 49 54 45 5f 43 4f 4e 53  e of SQLITE_CONS
1eb0a 54 52 41 49 4e 54 2e 0a 2a 2a 0a 2a 2a 20 20 61  TRAINT..**.**  a
1eb0b 6e 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ny              
1eb0c 41 42 4f 52 54 20 20 20 20 20 20 20 20 42 61 63  ABORT        Bac
1eb0d 6b 20 6f 75 74 20 63 68 61 6e 67 65 73 20 66 72  k out changes fr
1eb0e 6f 6d 20 74 68 65 20 63 75 72 72 65 6e 74 20 63  om the current c
1eb0f 6f 6d 6d 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20  ommand.**       
1eb10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eb11 20 20 20 20 20 20 20 20 20 6f 6e 6c 79 20 28 64           only (d
1eb12 6f 20 6e 6f 74 20 64 6f 20 61 20 63 6f 6d 70 6c  o not do a compl
1eb13 65 74 65 20 72 6f 6c 6c 62 61 63 6b 29 20 74 68  ete rollback) th
1eb14 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  en.**           
1eb15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eb16 20 20 20 20 20 63 61 75 73 65 20 73 71 6c 69 74       cause sqlit
1eb17 65 33 5f 65 78 65 63 28 29 20 74 6f 20 72 65 74  e3_exec() to ret
1eb18 75 72 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a  urn immediately.
1eb19 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
1eb1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eb1b 20 20 77 69 74 68 20 53 51 4c 49 54 45 5f 43 4f    with SQLITE_CO
1eb1c 4e 53 54 52 41 49 4e 54 2e 0a 2a 2a 0a 2a 2a 20  NSTRAINT..**.** 
1eb1d 20 61 6e 79 20 20 20 20 20 20 20 20 20 20 20 20   any            
1eb1e 20 20 46 41 49 4c 20 20 20 20 20 20 20 20 20 53    FAIL         S
1eb1f 71 6c 69 74 65 5f 65 78 65 63 28 29 20 72 65 74  qlite_exec() ret
1eb20 75 72 6e 73 20 69 6d 6d 65 64 69 61 74 65 6c 79  urns immediately
1eb21 20 77 69 74 68 20 61 0a 2a 2a 20 20 20 20 20 20   with a.**      
1eb22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eb23 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
1eb24 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 5f   code of SQLITE_
1eb25 43 4f 4e 53 54 52 41 49 4e 54 2e 20 20 54 68 65  CONSTRAINT.  The
1eb26 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1eb27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eb28 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69     transaction i
1eb29 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63  s not rolled bac
1eb2a 6b 20 61 6e 64 20 61 6e 79 0a 2a 2a 20 20 20 20  k and any.**    
1eb2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eb2c 20 20 20 20 20 20 20 20 20 20 20 20 70 72 69 6f              prio
1eb2d 72 20 63 68 61 6e 67 65 73 20 61 72 65 20 72 65  r changes are re
1eb2e 74 61 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 61  tained..**.**  a
1eb2f 6e 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ny              
1eb30 49 47 4e 4f 52 45 20 20 20 20 20 20 20 54 68 65  IGNORE       The
1eb31 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 61   record number a
1eb32 6e 64 20 64 61 74 61 20 69 73 20 70 6f 70 70 65  nd data is poppe
1eb33 64 20 66 72 6f 6d 0a 2a 2a 20 20 20 20 20 20 20  d from.**       
1eb34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eb35 20 20 20 20 20 20 20 20 20 74 68 65 20 73 74 61           the sta
1eb36 63 6b 20 61 6e 64 20 74 68 65 72 65 20 69 73 20  ck and there is 
1eb37 61 6e 20 69 6d 6d 65 64 69 61 74 65 20 6a 75 6d  an immediate jum
1eb38 70 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  p.**            
1eb39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eb3a 20 20 20 20 74 6f 20 6c 61 62 65 6c 20 69 67 6e      to label ign
1eb3b 6f 72 65 44 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 20  oreDest..**.**  
1eb3c 4e 4f 54 20 4e 55 4c 4c 20 20 20 20 20 20 20 20  NOT NULL        
1eb3d 20 52 45 50 4c 41 43 45 20 20 20 20 20 20 54 68   REPLACE      Th
1eb3e 65 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20  e NULL value is 
1eb3f 72 65 70 6c 61 63 65 20 62 79 20 74 68 65 20 64  replace by the d
1eb40 65 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20  efault.**       
1eb41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eb42 20 20 20 20 20 20 20 20 20 76 61 6c 75 65 20 66           value f
1eb43 6f 72 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 20  or that column. 
1eb44 20 49 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   If the default 
1eb45 76 61 6c 75 65 0a 2a 2a 20 20 20 20 20 20 20 20  value.**        
1eb46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eb47 20 20 20 20 20 20 20 20 69 73 20 4e 55 4c 4c 2c          is NULL,
1eb48 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20 74   the action is t
1eb49 68 65 20 73 61 6d 65 20 61 73 20 41 42 4f 52 54  he same as ABORT
1eb4a 2e 0a 2a 2a 0a 2a 2a 20 20 55 4e 49 51 55 45 20  ..**.**  UNIQUE 
1eb4b 20 20 20 20 20 20 20 20 20 20 52 45 50 4c 41 43            REPLAC
1eb4c 45 20 20 20 20 20 20 54 68 65 20 6f 74 68 65 72  E      The other
1eb4d 20 72 6f 77 20 74 68 61 74 20 63 6f 6e 66 6c 69   row that confli
1eb4e 63 74 73 20 77 69 74 68 20 74 68 65 20 72 6f 77  cts with the row
1eb4f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1eb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eb51 20 20 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65     being inserte
1eb52 64 20 69 73 20 72 65 6d 6f 76 65 64 2e 0a 2a 2a  d is removed..**
1eb53 0a 2a 2a 20 20 43 48 45 43 4b 20 20 20 20 20 20  .**  CHECK      
1eb54 20 20 20 20 20 20 52 45 50 4c 41 43 45 20 20 20        REPLACE   
1eb55 20 20 20 49 6c 6c 65 67 61 6c 2e 20 20 54 68 65     Illegal.  The
1eb56 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 20 65   results in an e
1eb57 78 63 65 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  xception..**.** 
1eb58 57 68 69 63 68 20 61 63 74 69 6f 6e 20 74 6f 20  Which action to 
1eb59 74 61 6b 65 20 69 73 20 64 65 74 65 72 6d 69 6e  take is determin
1eb5a 65 64 20 62 79 20 74 68 65 20 6f 76 65 72 72 69  ed by the overri
1eb5b 64 65 45 72 72 6f 72 20 70 61 72 61 6d 65 74 65  deError paramete
1eb5c 72 2e 0a 2a 2a 20 4f 72 20 69 66 20 6f 76 65 72  r..** Or if over
1eb5d 72 69 64 65 45 72 72 6f 72 3d 3d 4f 45 5f 44 65  rideError==OE_De
1eb5e 66 61 75 6c 74 2c 20 74 68 65 6e 20 74 68 65 20  fault, then the 
1eb5f 70 50 61 72 73 65 2d 3e 6f 6e 45 72 72 6f 72 20  pParse->onError 
1eb60 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 69 73 20  parameter.** is 
1eb61 75 73 65 64 2e 20 20 4f 72 20 69 66 20 70 50 61  used.  Or if pPa
1eb62 72 73 65 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45  rse->onError==OE
1eb63 5f 44 65 66 61 75 6c 74 20 74 68 65 6e 20 74 68  _Default then th
1eb64 65 20 6f 6e 45 72 72 6f 72 20 76 61 6c 75 65 0a  e onError value.
1eb65 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f 6e 73 74  ** for the const
1eb66 72 61 69 6e 74 20 69 73 20 75 73 65 64 2e 0a 2a  raint is used..*
1eb67 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67  *.** The calling
1eb68 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 6f 70   routine must op
1eb69 65 6e 20 61 20 72 65 61 64 2f 77 72 69 74 65 20  en a read/write 
1eb6a 63 75 72 73 6f 72 20 66 6f 72 20 70 54 61 62 20  cursor for pTab 
1eb6b 77 69 74 68 0a 2a 2a 20 63 75 72 73 6f 72 20 6e  with.** cursor n
1eb6c 75 6d 62 65 72 20 22 62 61 73 65 43 75 72 22 2e  umber "baseCur".
1eb6d 20 20 41 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66    All indices of
1eb6e 20 70 54 61 62 20 6d 75 73 74 20 61 6c 73 6f 20   pTab must also 
1eb6f 68 61 76 65 20 6f 70 65 6e 0a 2a 2a 20 72 65 61  have open.** rea
1eb70 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 73 20  d/write cursors 
1eb71 77 69 74 68 20 63 75 72 73 6f 72 20 6e 75 6d 62  with cursor numb
1eb72 65 72 20 62 61 73 65 43 75 72 2b 69 20 66 6f 72  er baseCur+i for
1eb73 20 74 68 65 20 69 2d 74 68 20 63 75 72 73 6f 72   the i-th cursor
1eb74 2e 0a 2a 2a 20 45 78 63 65 70 74 2c 20 69 66 20  ..** Except, if 
1eb75 74 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 73 73  there is no poss
1eb76 69 62 69 6c 69 74 79 20 6f 66 20 61 20 52 45 50  ibility of a REP
1eb77 4c 41 43 45 20 61 63 74 69 6f 6e 20 74 68 65 6e  LACE action then
1eb78 0a 2a 2a 20 63 75 72 73 6f 72 73 20 64 6f 20 6e  .** cursors do n
1eb79 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 6f 70  ot need to be op
1eb7a 65 6e 20 66 6f 72 20 69 6e 64 69 63 65 73 20 77  en for indices w
1eb7b 68 65 72 65 20 61 52 65 67 49 64 78 5b 69 5d 3d  here aRegIdx[i]=
1eb7c 3d 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  =0..*/.SQLITE_PR
1eb7d 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1eb7e 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72  e3GenerateConstr
1eb7f 61 69 6e 74 43 68 65 63 6b 73 28 0a 20 20 50 61  aintChecks(.  Pa
1eb80 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
1eb81 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
1eb82 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62  context */.  Tab
1eb83 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20  le *pTab,       
1eb84 20 2f 2a 20 74 68 65 20 74 61 62 6c 65 20 69 6e   /* the table in
1eb85 74 6f 20 77 68 69 63 68 20 77 65 20 61 72 65 20  to which we are 
1eb86 69 6e 73 65 72 74 69 6e 67 20 2a 2f 0a 20 20 69  inserting */.  i
1eb87 6e 74 20 62 61 73 65 43 75 72 2c 20 20 20 20 20  nt baseCur,     
1eb88 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 61     /* Index of a
1eb89 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73   read/write curs
1eb8a 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 70  or pointing at p
1eb8b 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  Tab */.  int reg
1eb8c 52 6f 77 69 64 2c 20 20 20 20 20 20 20 2f 2a 20  Rowid,       /* 
1eb8d 49 6e 64 65 78 20 6f 66 20 74 68 65 20 72 61 6e  Index of the ran
1eb8e 67 65 20 6f 66 20 69 6e 70 75 74 20 72 65 67 69  ge of input regi
1eb8f 73 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 2a  sters */.  int *
1eb90 61 52 65 67 49 64 78 2c 20 20 20 20 20 20 20 2f  aRegIdx,       /
1eb91 2a 20 52 65 67 69 73 74 65 72 20 75 73 65 64 20  * Register used 
1eb92 62 79 20 65 61 63 68 20 69 6e 64 65 78 2e 20 20  by each index.  
1eb93 30 20 66 6f 72 20 75 6e 75 73 65 64 20 69 6e 64  0 for unused ind
1eb94 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 6f  ices */.  int ro
1eb95 77 69 64 43 68 6e 67 2c 20 20 20 20 20 20 2f 2a  widChng,      /*
1eb96 20 54 72 75 65 20 69 66 20 74 68 65 20 72 6f 77   True if the row
1eb97 69 64 20 6d 69 67 68 74 20 63 6f 6c 6c 69 64 65  id might collide
1eb98 20 77 69 74 68 20 65 78 69 73 74 69 6e 67 20 65   with existing e
1eb99 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69 73  ntry */.  int is
1eb9a 55 70 64 61 74 65 2c 20 20 20 20 20 20 20 2f 2a  Update,       /*
1eb9b 20 54 72 75 65 20 66 6f 72 20 55 50 44 41 54 45   True for UPDATE
1eb9c 2c 20 46 61 6c 73 65 20 66 6f 72 20 49 4e 53 45  , False for INSE
1eb9d 52 54 20 2a 2f 0a 20 20 69 6e 74 20 6f 76 65 72  RT */.  int over
1eb9e 72 69 64 65 45 72 72 6f 72 2c 20 20 2f 2a 20 4f  rideError,  /* O
1eb9f 76 65 72 72 69 64 65 20 6f 6e 45 72 72 6f 72 20  verride onError 
1eba0 74 6f 20 74 68 69 73 20 69 66 20 6e 6f 74 20 4f  to this if not O
1eba1 45 5f 44 65 66 61 75 6c 74 20 2a 2f 0a 20 20 69  E_Default */.  i
1eba2 6e 74 20 69 67 6e 6f 72 65 44 65 73 74 20 20 20  nt ignoreDest   
1eba3 20 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 74 68     /* Jump to th
1eba4 69 73 20 6c 61 62 65 6c 20 6f 6e 20 61 6e 20 4f  is label on an O
1eba5 45 5f 49 67 6e 6f 72 65 20 72 65 73 6f 6c 75 74  E_Ignore resolut
1eba6 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ion */.){.  int 
1eba7 69 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  i;.  Vdbe *v;.  
1eba8 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 69 6e 74 20  int nCol;.  int 
1eba9 6f 6e 45 72 72 6f 72 3b 0a 20 20 69 6e 74 20 6a  onError;.  int j
1ebaa 31 2c 20 6a 32 2c 20 6a 33 3b 20 20 20 20 20 2f  1, j2, j3;     /
1ebab 2a 20 41 64 64 72 65 73 73 65 73 20 6f 66 20 6a  * Addresses of j
1ebac 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73  ump instructions
1ebad 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74   */.  int regDat
1ebae 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67  a;        /* Reg
1ebaf 69 73 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  ister containing
1ebb0 20 66 69 72 73 74 20 64 61 74 61 20 63 6f 6c 75   first data colu
1ebb1 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72  mn */.  int iCur
1ebb2 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b  ;.  Index *pIdx;
1ebb3 0a 20 20 69 6e 74 20 73 65 65 6e 52 65 70 6c 61  .  int seenRepla
1ebb4 63 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 68 61  ce = 0;.  int ha
1ebb5 73 54 77 6f 52 6f 77 69 64 73 20 3d 20 28 69 73  sTwoRowids = (is
1ebb6 55 70 64 61 74 65 20 26 26 20 72 6f 77 69 64 43  Update && rowidC
1ebb7 68 6e 67 29 3b 0a 0a 20 20 76 20 3d 20 73 71 6c  hng);..  v = sql
1ebb8 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
1ebb9 73 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76  se);.  assert( v
1ebba 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
1ebbb 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d   pTab->pSelect==
1ebbc 30 20 29 3b 20 20 2f 2a 20 54 68 69 73 20 74 61  0 );  /* This ta
1ebbd 62 6c 65 20 69 73 20 6e 6f 74 20 61 20 56 49 45  ble is not a VIE
1ebbe 57 20 2a 2f 0a 20 20 6e 43 6f 6c 20 3d 20 70 54  W */.  nCol = pT
1ebbf 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 72 65 67 44  ab->nCol;.  regD
1ebc0 61 74 61 20 3d 20 72 65 67 52 6f 77 69 64 20 2b  ata = regRowid +
1ebc1 20 31 3b 0a 0a 0a 20 20 2f 2a 20 54 65 73 74 20   1;...  /* Test 
1ebc2 61 6c 6c 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e  all NOT NULL con
1ebc3 73 74 72 61 69 6e 74 73 2e 0a 20 20 2a 2f 0a 20  straints..  */. 
1ebc4 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c   for(i=0; i<nCol
1ebc5 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
1ebc6 69 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29  i==pTab->iPKey )
1ebc7 7b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65  {.      continue
1ebc8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6f 6e 45 72  ;.    }.    onEr
1ebc9 72 6f 72 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c  ror = pTab->aCol
1ebca 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c 3b 0a 20 20 20  [i].notNull;.   
1ebcb 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45   if( onError==OE
1ebcc 5f 4e 6f 6e 65 20 29 20 63 6f 6e 74 69 6e 75 65  _None ) continue
1ebcd 3b 0a 20 20 20 20 69 66 28 20 6f 76 65 72 72 69  ;.    if( overri
1ebce 64 65 45 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61  deError!=OE_Defa
1ebcf 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45  ult ){.      onE
1ebd0 72 72 6f 72 20 3d 20 6f 76 65 72 72 69 64 65 45  rror = overrideE
1ebd1 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 20  rror;.    }else 
1ebd2 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  if( onError==OE_
1ebd3 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20  Default ){.     
1ebd4 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62   onError = OE_Ab
1ebd5 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ort;.    }.    i
1ebd6 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52  f( onError==OE_R
1ebd7 65 70 6c 61 63 65 20 26 26 20 70 54 61 62 2d 3e  eplace && pTab->
1ebd8 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 3d 3d 30  aCol[i].pDflt==0
1ebd9 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f   ){.      onErro
1ebda 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20  r = OE_Abort;.  
1ebdb 20 20 7d 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c    }.    j1 = sql
1ebdc 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
1ebdd 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65  , OP_NotNull, re
1ebde 67 44 61 74 61 2b 69 29 3b 0a 20 20 20 20 61 73  gData+i);.    as
1ebdf 73 65 72 74 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f  sert( onError==O
1ebe0 45 5f 52 6f 6c 6c 62 61 63 6b 20 7c 7c 20 6f 6e  E_Rollback || on
1ebe1 45 72 72 6f 72 3d 3d 4f 45 5f 41 62 6f 72 74 20  Error==OE_Abort 
1ebe2 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 46  || onError==OE_F
1ebe3 61 69 6c 0a 20 20 20 20 20 20 20 20 7c 7c 20 6f  ail.        || o
1ebe4 6e 45 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72  nError==OE_Ignor
1ebe5 65 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45  e || onError==OE
1ebe6 5f 52 65 70 6c 61 63 65 20 29 3b 0a 20 20 20 20  _Replace );.    
1ebe7 73 77 69 74 63 68 28 20 6f 6e 45 72 72 6f 72 20  switch( onError 
1ebe8 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45  ){.      case OE
1ebe9 5f 52 6f 6c 6c 62 61 63 6b 3a 0a 20 20 20 20 20  _Rollback:.     
1ebea 20 63 61 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a   case OE_Abort:.
1ebeb 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 46 61        case OE_Fa
1ebec 69 6c 3a 20 7b 0a 20 20 20 20 20 20 20 20 63 68  il: {.        ch
1ebed 61 72 20 2a 7a 4d 73 67 20 3d 20 30 3b 0a 20 20  ar *zMsg = 0;.  
1ebee 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1ebef 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 48 61  eAddOp2(v, OP_Ha
1ebf0 6c 74 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54  lt, SQLITE_CONST
1ebf1 52 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 29 3b  RAINT, onError);
1ebf2 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1ebf3 53 65 74 53 74 72 69 6e 67 28 26 7a 4d 73 67 2c  SetString(&zMsg,
1ebf4 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 2e   pTab->zName, ".
1ebf5 22 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d  ", pTab->aCol[i]
1ebf6 2e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  .zName,.        
1ebf7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ebf8 22 20 6d 61 79 20 6e 6f 74 20 62 65 20 4e 55 4c  " may not be NUL
1ebf9 4c 22 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20  L", (char*)0);. 
1ebfa 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1ebfb 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31  beChangeP4(v, -1
1ebfc 2c 20 7a 4d 73 67 2c 20 50 34 5f 44 59 4e 41 4d  , zMsg, P4_DYNAM
1ebfd 49 43 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  IC);.        bre
1ebfe 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
1ebff 20 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72 65    case OE_Ignore
1ec00 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  : {.        sqli
1ec01 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1ec02 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 67 6e   OP_Goto, 0, ign
1ec03 6f 72 65 44 65 73 74 29 3b 0a 20 20 20 20 20 20  oreDest);.      
1ec04 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
1ec05 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52  .      case OE_R
1ec06 65 70 6c 61 63 65 3a 20 7b 0a 20 20 20 20 20 20  eplace: {.      
1ec07 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
1ec08 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e  e(pParse, pTab->
1ec09 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 2c 20 72  aCol[i].pDflt, r
1ec0a 65 67 44 61 74 61 2b 69 29 3b 0a 20 20 20 20 20  egData+i);.     
1ec0b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1ec0c 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
1ec0d 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
1ec0e 76 2c 20 6a 31 29 3b 0a 20 20 7d 0a 0a 20 20 2f  v, j1);.  }..  /
1ec0f 2a 20 54 65 73 74 20 61 6c 6c 20 43 48 45 43 4b  * Test all CHECK
1ec10 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a   constraints.  *
1ec11 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1ec12 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 69 66  _OMIT_CHECK.  if
1ec13 28 20 70 54 61 62 2d 3e 70 43 68 65 63 6b 20 26  ( pTab->pCheck &
1ec14 26 20 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66  & (pParse->db->f
1ec15 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 49 67  lags & SQLITE_Ig
1ec16 6e 6f 72 65 43 68 65 63 6b 73 29 3d 3d 30 20 29  noreChecks)==0 )
1ec17 7b 0a 20 20 20 20 69 6e 74 20 61 6c 6c 4f 6b 20  {.    int allOk 
1ec18 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
1ec19 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 70  eLabel(v);.    p
1ec1a 50 61 72 73 65 2d 3e 63 6b 42 61 73 65 20 3d 20  Parse->ckBase = 
1ec1b 72 65 67 44 61 74 61 3b 0a 20 20 20 20 73 71 6c  regData;.    sql
1ec1c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70  ite3ExprIfTrue(p
1ec1d 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 70 43 68  Parse, pTab->pCh
1ec1e 65 63 6b 2c 20 61 6c 6c 4f 6b 2c 20 53 51 4c 49  eck, allOk, SQLI
1ec1f 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a  TE_JUMPIFNULL);.
1ec20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 6f 76      onError = ov
1ec21 65 72 72 69 64 65 45 72 72 6f 72 21 3d 4f 45 5f  errideError!=OE_
1ec22 44 65 66 61 75 6c 74 20 3f 20 6f 76 65 72 72 69  Default ? overri
1ec23 64 65 45 72 72 6f 72 20 3a 20 4f 45 5f 41 62 6f  deError : OE_Abo
1ec24 72 74 3b 0a 20 20 20 20 69 66 28 20 6f 6e 45 72  rt;.    if( onEr
1ec25 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 29  ror==OE_Ignore )
1ec26 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
1ec27 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1ec28 47 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f 72 65 44  Goto, 0, ignoreD
1ec29 65 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  est);.    }else{
1ec2a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1ec2b 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 48  beAddOp2(v, OP_H
1ec2c 61 6c 74 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53  alt, SQLITE_CONS
1ec2d 54 52 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 29  TRAINT, onError)
1ec2e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
1ec2f 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
1ec30 62 65 6c 28 76 2c 20 61 6c 6c 4f 6b 29 3b 0a 20  bel(v, allOk);. 
1ec31 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65   }.#endif /* !de
1ec32 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
1ec33 54 5f 43 48 45 43 4b 29 20 2a 2f 0a 0a 20 20 2f  T_CHECK) */..  /
1ec34 2a 20 49 66 20 77 65 20 68 61 76 65 20 61 6e 20  * If we have an 
1ec35 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
1ec36 4b 45 59 2c 20 6d 61 6b 65 20 73 75 72 65 20 74  KEY, make sure t
1ec37 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 0a 20  he primary key. 
1ec38 20 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77 20 72   ** of the new r
1ec39 65 63 6f 72 64 20 64 6f 65 73 20 6e 6f 74 20 70  ecord does not p
1ec3a 72 65 76 69 6f 75 73 6c 79 20 65 78 69 73 74 2e  reviously exist.
1ec3b 20 20 45 78 63 65 70 74 2c 20 69 66 20 74 68 69    Except, if thi
1ec3c 73 0a 20 20 2a 2a 20 69 73 20 61 6e 20 55 50 44  s.  ** is an UPD
1ec3d 41 54 45 20 61 6e 64 20 74 68 65 20 70 72 69 6d  ATE and the prim
1ec3e 61 72 79 20 6b 65 79 20 69 73 20 6e 6f 74 20 63  ary key is not c
1ec3f 68 61 6e 67 69 6e 67 2c 20 74 68 61 74 20 69 73  hanging, that is
1ec40 20 4f 4b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20   OK..  */.  if( 
1ec41 72 6f 77 69 64 43 68 6e 67 20 29 7b 0a 20 20 20  rowidChng ){.   
1ec42 20 6f 6e 45 72 72 6f 72 20 3d 20 70 54 61 62 2d   onError = pTab-
1ec43 3e 6b 65 79 43 6f 6e 66 3b 0a 20 20 20 20 69 66  >keyConf;.    if
1ec44 28 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 21  ( overrideError!
1ec45 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20  =OE_Default ){. 
1ec46 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 6f       onError = o
1ec47 76 65 72 72 69 64 65 45 72 72 6f 72 3b 0a 20 20  verrideError;.  
1ec48 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 6e 45 72    }else if( onEr
1ec49 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20  ror==OE_Default 
1ec4a 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72  ){.      onError
1ec4b 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20   = OE_Abort;.   
1ec4c 20 7d 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20   }.    .    if( 
1ec4d 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 52 65 70 6c  onError!=OE_Repl
1ec4e 61 63 65 20 7c 7c 20 70 54 61 62 2d 3e 70 49 6e  ace || pTab->pIn
1ec4f 64 65 78 20 29 7b 0a 20 20 20 20 20 20 69 66 28  dex ){.      if(
1ec50 20 69 73 55 70 64 61 74 65 20 29 7b 0a 20 20 20   isUpdate ){.   
1ec51 20 20 20 20 20 6a 32 20 3d 20 73 71 6c 69 74 65       j2 = sqlite
1ec52 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
1ec53 50 5f 45 71 2c 20 72 65 67 52 6f 77 69 64 2c 20  P_Eq, regRowid, 
1ec54 30 2c 20 72 65 67 52 6f 77 69 64 2d 31 29 3b 0a  0, regRowid-1);.
1ec55 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6a 33        }.      j3
1ec56 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
1ec57 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78  dOp3(v, OP_NotEx
1ec58 69 73 74 73 2c 20 62 61 73 65 43 75 72 2c 20 30  ists, baseCur, 0
1ec59 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
1ec5a 20 20 20 73 77 69 74 63 68 28 20 6f 6e 45 72 72     switch( onErr
1ec5b 6f 72 20 29 7b 0a 20 20 20 20 20 20 20 20 64 65  or ){.        de
1ec5c 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20  fault: {.       
1ec5d 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f     onError = OE_
1ec5e 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 20 20 20  Abort;.         
1ec5f 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 69 6e   /* Fall thru in
1ec60 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73 65  to the next case
1ec61 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   */.        }.  
1ec62 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52 6f        case OE_Ro
1ec63 6c 6c 62 61 63 6b 3a 0a 20 20 20 20 20 20 20 20  llback:.        
1ec64 63 61 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a 20  case OE_Abort:. 
1ec65 20 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 46         case OE_F
1ec66 61 69 6c 3a 20 7b 0a 20 20 20 20 20 20 20 20 20  ail: {.         
1ec67 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1ec68 70 34 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53  p4(v, OP_Halt, S
1ec69 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
1ec6a 2c 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 0a 20 20  , onError, 0,.  
1ec6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ec6c 20 20 20 20 20 20 20 20 20 22 50 52 49 4d 41 52           "PRIMAR
1ec6d 59 20 4b 45 59 20 6d 75 73 74 20 62 65 20 75 6e  Y KEY must be un
1ec6e 69 71 75 65 22 2c 20 50 34 5f 53 54 41 54 49 43  ique", P4_STATIC
1ec6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  );.          bre
1ec70 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
1ec71 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52 65        case OE_Re
1ec72 70 6c 61 63 65 3a 20 7b 0a 20 20 20 20 20 20 20  place: {.       
1ec73 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61     sqlite3Genera
1ec74 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74 65  teRowIndexDelete
1ec75 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 62  (pParse, pTab, b
1ec76 61 73 65 43 75 72 2c 20 30 29 3b 0a 20 20 20 20  aseCur, 0);.    
1ec77 20 20 20 20 20 20 73 65 65 6e 52 65 70 6c 61 63        seenReplac
1ec78 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  e = 1;.         
1ec79 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
1ec7a 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 4f  }.        case O
1ec7b 45 5f 49 67 6e 6f 72 65 3a 20 7b 0a 20 20 20 20  E_Ignore: {.    
1ec7c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 65        assert( se
1ec7d 65 6e 52 65 70 6c 61 63 65 3d 3d 30 20 29 3b 0a  enReplace==0 );.
1ec7e 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1ec7f 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1ec80 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f 72  P_Goto, 0, ignor
1ec81 65 44 65 73 74 29 3b 0a 20 20 20 20 20 20 20 20  eDest);.        
1ec82 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
1ec83 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
1ec84 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
1ec85 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a 20 20 20  Here(v, j3);.   
1ec86 20 20 20 69 66 28 20 69 73 55 70 64 61 74 65 20     if( isUpdate 
1ec87 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1ec88 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
1ec89 2c 20 6a 32 29 3b 0a 20 20 20 20 20 20 7d 0a 20  , j2);.      }. 
1ec8a 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54     }.  }..  /* T
1ec8b 65 73 74 20 61 6c 6c 20 55 4e 49 51 55 45 20 63  est all UNIQUE c
1ec8c 6f 6e 73 74 72 61 69 6e 74 73 20 62 79 20 63 72  onstraints by cr
1ec8d 65 61 74 69 6e 67 20 65 6e 74 72 69 65 73 20 66  eating entries f
1ec8e 6f 72 20 65 61 63 68 20 55 4e 49 51 55 45 0a 20  or each UNIQUE. 
1ec8f 20 2a 2a 20 69 6e 64 65 78 20 61 6e 64 20 6d 61   ** index and ma
1ec90 6b 69 6e 67 20 73 75 72 65 20 74 68 61 74 20 64  king sure that d
1ec91 75 70 6c 69 63 61 74 65 20 65 6e 74 72 69 65 73  uplicate entries
1ec92 20 64 6f 20 6e 6f 74 20 61 6c 72 65 61 64 79 20   do not already 
1ec93 65 78 69 73 74 2e 0a 20 20 2a 2a 20 41 64 64 20  exist..  ** Add 
1ec94 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 73 20  the new records 
1ec95 74 6f 20 74 68 65 20 69 6e 64 69 63 65 73 20 61  to the indices a
1ec96 73 20 77 65 20 67 6f 2e 0a 20 20 2a 2f 0a 20 20  s we go..  */.  
1ec97 66 6f 72 28 69 43 75 72 3d 30 2c 20 70 49 64 78  for(iCur=0, pIdx
1ec98 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
1ec99 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
1ec9a 70 4e 65 78 74 2c 20 69 43 75 72 2b 2b 29 7b 0a  pNext, iCur++){.
1ec9b 20 20 20 20 69 6e 74 20 72 65 67 49 64 78 3b 0a      int regIdx;.
1ec9c 20 20 20 20 69 6e 74 20 72 65 67 52 3b 0a 0a 20      int regR;.. 
1ec9d 20 20 20 69 66 28 20 61 52 65 67 49 64 78 5b 69     if( aRegIdx[i
1ec9e 43 75 72 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  Cur]==0 ) contin
1ec9f 75 65 3b 20 20 2f 2a 20 53 6b 69 70 20 75 6e 75  ue;  /* Skip unu
1eca0 73 65 64 20 69 6e 64 69 63 65 73 20 2a 2f 0a 0a  sed indices */..
1eca1 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20      /* Create a 
1eca2 6b 65 79 20 66 6f 72 20 61 63 63 65 73 73 69 6e  key for accessin
1eca3 67 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72  g the index entr
1eca4 79 20 2a 2f 0a 20 20 20 20 72 65 67 49 64 78 20  y */.    regIdx 
1eca5 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
1eca6 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 70 49  Range(pParse, pI
1eca7 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a  dx->nColumn+1);.
1eca8 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
1eca9 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  Idx->nColumn; i+
1ecaa 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 64  +){.      int id
1ecab 78 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75  x = pIdx->aiColu
1ecac 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28  mn[i];.      if(
1ecad 20 69 64 78 3d 3d 70 54 61 62 2d 3e 69 50 4b 65   idx==pTab->iPKe
1ecae 79 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  y ){.        sql
1ecaf 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1ecb0 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 52  , OP_SCopy, regR
1ecb1 6f 77 69 64 2c 20 72 65 67 49 64 78 2b 69 29 3b  owid, regIdx+i);
1ecb2 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1ecb3 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1ecb4 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43  eAddOp2(v, OP_SC
1ecb5 6f 70 79 2c 20 72 65 67 44 61 74 61 2b 69 64 78  opy, regData+idx
1ecb6 2c 20 72 65 67 49 64 78 2b 69 29 3b 0a 20 20 20  , regIdx+i);.   
1ecb7 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
1ecb8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1ecb9 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65  (v, OP_SCopy, re
1ecba 67 52 6f 77 69 64 2c 20 72 65 67 49 64 78 2b 69  gRowid, regIdx+i
1ecbb 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1ecbc 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
1ecbd 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 49 64  akeRecord, regId
1ecbe 78 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  x, pIdx->nColumn
1ecbf 2b 31 2c 20 61 52 65 67 49 64 78 5b 69 43 75 72  +1, aRegIdx[iCur
1ecc0 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49  ]);.    sqlite3I
1ecc1 6e 64 65 78 41 66 66 69 6e 69 74 79 53 74 72 28  ndexAffinityStr(
1ecc2 76 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 73 71  v, pIdx);.    sq
1ecc3 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66  lite3ExprCacheAf
1ecc4 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61  finityChange(pPa
1ecc5 72 73 65 2c 20 72 65 67 49 64 78 2c 20 70 49 64  rse, regIdx, pId
1ecc6 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 20  x->nColumn+1);. 
1ecc7 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
1ecc8 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73  eTempRange(pPars
1ecc9 65 2c 20 72 65 67 49 64 78 2c 20 70 49 64 78 2d  e, regIdx, pIdx-
1ecca 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 0a 20 20  >nColumn+1);..  
1eccb 20 20 2f 2a 20 46 69 6e 64 20 6f 75 74 20 77 68    /* Find out wh
1eccc 61 74 20 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b  at action to tak
1eccd 65 20 69 6e 20 63 61 73 65 20 74 68 65 72 65 20  e in case there 
1ecce 69 73 20 61 6e 20 69 6e 64 65 78 69 6e 67 20 63  is an indexing c
1eccf 6f 6e 66 6c 69 63 74 20 2a 2f 0a 20 20 20 20 6f  onflict */.    o
1ecd0 6e 45 72 72 6f 72 20 3d 20 70 49 64 78 2d 3e 6f  nError = pIdx->o
1ecd1 6e 45 72 72 6f 72 3b 0a 20 20 20 20 69 66 28 20  nError;.    if( 
1ecd2 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65  onError==OE_None
1ecd3 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a   ) continue;  /*
1ecd4 20 70 49 64 78 20 69 73 20 6e 6f 74 20 61 20 55   pIdx is not a U
1ecd5 4e 49 51 55 45 20 69 6e 64 65 78 20 2a 2f 0a 20  NIQUE index */. 
1ecd6 20 20 20 69 66 28 20 6f 76 65 72 72 69 64 65 45     if( overrideE
1ecd7 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75 6c 74  rror!=OE_Default
1ecd8 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f   ){.      onErro
1ecd9 72 20 3d 20 6f 76 65 72 72 69 64 65 45 72 72 6f  r = overrideErro
1ecda 72 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  r;.    }else if(
1ecdb 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66   onError==OE_Def
1ecdc 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e  ault ){.      on
1ecdd 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74  Error = OE_Abort
1ecde 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1ecdf 73 65 65 6e 52 65 70 6c 61 63 65 20 29 7b 0a 20  seenReplace ){. 
1ece0 20 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72       if( onError
1ece1 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 29 20 6f 6e  ==OE_Ignore ) on
1ece2 45 72 72 6f 72 20 3d 20 4f 45 5f 52 65 70 6c 61  Error = OE_Repla
1ece3 63 65 3b 0a 20 20 20 20 20 20 65 6c 73 65 20 69  ce;.      else i
1ece4 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 46  f( onError==OE_F
1ece5 61 69 6c 20 29 20 6f 6e 45 72 72 6f 72 20 3d 20  ail ) onError = 
1ece6 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a  OE_Abort;.    }.
1ece7 20 20 20 20 0a 0a 20 20 20 20 2f 2a 20 43 68 65      ..    /* Che
1ece8 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65  ck to see if the
1ece9 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72 79   new index entry
1ecea 20 77 69 6c 6c 20 62 65 20 75 6e 69 71 75 65 20   will be unique 
1eceb 2a 2f 0a 20 20 20 20 6a 32 20 3d 20 73 71 6c 69  */.    j2 = sqli
1ecec 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
1eced 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 49   OP_IsNull, regI
1ecee 64 78 2c 20 30 2c 20 70 49 64 78 2d 3e 6e 43 6f  dx, 0, pIdx->nCo
1ecef 6c 75 6d 6e 29 3b 0a 20 20 20 20 72 65 67 52 20  lumn);.    regR 
1ecf0 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
1ecf1 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
1ecf2 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1ecf3 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20  p2(v, OP_SCopy, 
1ecf4 72 65 67 52 6f 77 69 64 2d 68 61 73 54 77 6f 52  regRowid-hasTwoR
1ecf5 6f 77 69 64 73 2c 20 72 65 67 52 29 3b 0a 20 20  owids, regR);.  
1ecf6 20 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 64    j3 = sqlite3Vd
1ecf7 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49  beAddOp4(v, OP_I
1ecf8 73 55 6e 69 71 75 65 2c 20 62 61 73 65 43 75 72  sUnique, baseCur
1ecf9 2b 69 43 75 72 2b 31 2c 20 30 2c 0a 20 20 20 20  +iCur+1, 0,.    
1ecfa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ecfb 20 20 20 20 20 20 20 72 65 67 52 2c 20 28 63 68         regR, (ch
1ecfc 61 72 2a 29 61 52 65 67 49 64 78 5b 69 43 75 72  ar*)aRegIdx[iCur
1ecfd 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ],.             
1ecfe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 34                P4
1ecff 5f 49 4e 54 33 32 29 3b 0a 0a 20 20 20 20 2f 2a  _INT32);..    /*
1ed00 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
1ed01 68 61 74 20 65 78 65 63 75 74 65 73 20 69 66 20  hat executes if 
1ed02 74 68 65 20 6e 65 77 20 69 6e 64 65 78 20 65 6e  the new index en
1ed03 74 72 79 20 69 73 20 6e 6f 74 20 75 6e 69 71 75  try is not uniqu
1ed04 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  e */.    assert(
1ed05 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 6f 6c   onError==OE_Rol
1ed06 6c 62 61 63 6b 20 7c 7c 20 6f 6e 45 72 72 6f 72  lback || onError
1ed07 3d 3d 4f 45 5f 41 62 6f 72 74 20 7c 7c 20 6f 6e  ==OE_Abort || on
1ed08 45 72 72 6f 72 3d 3d 4f 45 5f 46 61 69 6c 0a 20  Error==OE_Fail. 
1ed09 20 20 20 20 20 20 20 7c 7c 20 6f 6e 45 72 72 6f         || onErro
1ed0a 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 7c 7c 20  r==OE_Ignore || 
1ed0b 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c  onError==OE_Repl
1ed0c 61 63 65 20 29 3b 0a 20 20 20 20 73 77 69 74 63  ace );.    switc
1ed0d 68 28 20 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20  h( onError ){.  
1ed0e 20 20 20 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c      case OE_Roll
1ed0f 62 61 63 6b 3a 0a 20 20 20 20 20 20 63 61 73 65  back:.      case
1ed10 20 4f 45 5f 41 62 6f 72 74 3a 0a 20 20 20 20 20   OE_Abort:.     
1ed11 20 63 61 73 65 20 4f 45 5f 46 61 69 6c 3a 20 7b   case OE_Fail: {
1ed12 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 2c 20  .        int j, 
1ed13 6e 31 2c 20 6e 32 3b 0a 20 20 20 20 20 20 20 20  n1, n2;.        
1ed14 63 68 61 72 20 7a 45 72 72 4d 73 67 5b 32 30 30  char zErrMsg[200
1ed15 5d 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ];.        sqlit
1ed16 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65  e3_snprintf(size
1ed17 6f 66 28 7a 45 72 72 4d 73 67 29 2c 20 7a 45 72  of(zErrMsg), zEr
1ed18 72 4d 73 67 2c 0a 20 20 20 20 20 20 20 20 20 20  rMsg,.          
1ed19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
1ed1a 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 31 20 3f  Idx->nColumn>1 ?
1ed1b 20 22 63 6f 6c 75 6d 6e 73 20 22 20 3a 20 22 63   "columns " : "c
1ed1c 6f 6c 75 6d 6e 20 22 29 3b 0a 20 20 20 20 20 20  olumn ");.      
1ed1d 20 20 6e 31 20 3d 20 73 74 72 6c 65 6e 28 7a 45    n1 = strlen(zE
1ed1e 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 20 20  rrMsg);.        
1ed1f 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d  for(j=0; j<pIdx-
1ed20 3e 6e 43 6f 6c 75 6d 6e 20 26 26 20 6e 31 3c 73  >nColumn && n1<s
1ed21 69 7a 65 6f 66 28 7a 45 72 72 4d 73 67 29 2d 33  izeof(zErrMsg)-3
1ed22 30 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  0; j++){.       
1ed23 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20     char *zCol = 
1ed24 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 49 64 78 2d  pTab->aCol[pIdx-
1ed25 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 5d 2e 7a 4e  >aiColumn[j]].zN
1ed26 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20 20 6e  ame;.          n
1ed27 32 20 3d 20 73 74 72 6c 65 6e 28 7a 43 6f 6c 29  2 = strlen(zCol)
1ed28 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
1ed29 6a 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  j>0 ){.         
1ed2a 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
1ed2b 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 4d  ntf(sizeof(zErrM
1ed2c 73 67 29 2d 6e 31 2c 20 26 7a 45 72 72 4d 73 67  sg)-n1, &zErrMsg
1ed2d 5b 6e 31 5d 2c 20 22 2c 20 22 29 3b 0a 20 20 20  [n1], ", ");.   
1ed2e 20 20 20 20 20 20 20 20 20 6e 31 20 2b 3d 20 32           n1 += 2
1ed2f 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
1ed30 20 20 20 20 20 20 20 20 69 66 28 20 6e 31 2b 6e          if( n1+n
1ed31 32 3e 73 69 7a 65 6f 66 28 7a 45 72 72 4d 73 67  2>sizeof(zErrMsg
1ed32 29 2d 33 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )-30 ){.        
1ed33 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1ed34 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72  intf(sizeof(zErr
1ed35 4d 73 67 29 2d 6e 31 2c 20 26 7a 45 72 72 4d 73  Msg)-n1, &zErrMs
1ed36 67 5b 6e 31 5d 2c 20 22 2e 2e 2e 22 29 3b 0a 20  g[n1], "...");. 
1ed37 20 20 20 20 20 20 20 20 20 20 20 6e 31 20 2b 3d             n1 +=
1ed38 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   3;.            
1ed39 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
1ed3a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1ed3b 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1ed3c 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72  intf(sizeof(zErr
1ed3d 4d 73 67 29 2d 6e 31 2c 20 26 7a 45 72 72 4d 73  Msg)-n1, &zErrMs
1ed3e 67 5b 6e 31 5d 2c 20 22 25 73 22 2c 20 7a 43 6f  g[n1], "%s", zCo
1ed3f 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  l);.            
1ed40 6e 31 20 2b 3d 20 6e 32 3b 0a 20 20 20 20 20 20  n1 += n2;.      
1ed41 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
1ed42 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1ed43 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
1ed44 7a 45 72 72 4d 73 67 29 2d 6e 31 2c 20 26 7a 45  zErrMsg)-n1, &zE
1ed45 72 72 4d 73 67 5b 6e 31 5d 2c 20 0a 20 20 20 20  rrMsg[n1], .    
1ed46 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e 6e 43          pIdx->nC
1ed47 6f 6c 75 6d 6e 3e 31 20 3f 20 22 20 61 72 65 20  olumn>1 ? " are 
1ed48 6e 6f 74 20 75 6e 69 71 75 65 22 20 3a 20 22 20  not unique" : " 
1ed49 69 73 20 6e 6f 74 20 75 6e 69 71 75 65 22 29 3b  is not unique");
1ed4a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1ed4b 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
1ed4c 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f 43 4f  _Halt, SQLITE_CO
1ed4d 4e 53 54 52 41 49 4e 54 2c 20 6f 6e 45 72 72 6f  NSTRAINT, onErro
1ed4e 72 2c 20 30 2c 20 7a 45 72 72 4d 73 67 2c 30 29  r, 0, zErrMsg,0)
1ed4f 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
1ed50 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
1ed51 61 73 65 20 4f 45 5f 49 67 6e 6f 72 65 3a 20 7b  ase OE_Ignore: {
1ed52 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1ed53 20 73 65 65 6e 52 65 70 6c 61 63 65 3d 3d 30 20   seenReplace==0 
1ed54 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
1ed55 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1ed56 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f  OP_Goto, 0, igno
1ed57 72 65 44 65 73 74 29 3b 0a 20 20 20 20 20 20 20  reDest);.       
1ed58 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
1ed59 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52 65        case OE_Re
1ed5a 70 6c 61 63 65 3a 20 7b 0a 20 20 20 20 20 20 20  place: {.       
1ed5b 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
1ed5c 52 6f 77 44 65 6c 65 74 65 28 70 50 61 72 73 65  RowDelete(pParse
1ed5d 2c 20 70 54 61 62 2c 20 62 61 73 65 43 75 72 2c  , pTab, baseCur,
1ed5e 20 72 65 67 52 2c 20 30 29 3b 0a 20 20 20 20 20   regR, 0);.     
1ed5f 20 20 20 73 65 65 6e 52 65 70 6c 61 63 65 20 3d     seenReplace =
1ed60 20 31 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61   1;.        brea
1ed61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
1ed62 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1ed63 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 32 29 3b  JumpHere(v, j2);
1ed64 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1ed65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 33 29 3b  JumpHere(v, j3);
1ed66 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65  .    sqlite3Rele
1ed67 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
1ed68 65 2c 20 72 65 67 52 29 3b 0a 20 20 7d 0a 7d 0a  e, regR);.  }.}.
1ed69 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
1ed6a 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f  ine generates co
1ed6b 64 65 20 74 6f 20 66 69 6e 69 73 68 20 74 68 65  de to finish the
1ed6c 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41 54   INSERT or UPDAT
1ed6d 45 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 74  E operation.** t
1ed6e 68 61 74 20 77 61 73 20 73 74 61 72 74 65 64 20  hat was started 
1ed6f 62 79 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20  by a prior call 
1ed70 74 6f 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61  to sqlite3Genera
1ed71 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63  teConstraintChec
1ed72 6b 73 2e 0a 2a 2a 20 41 20 63 6f 6e 73 65 63 75  ks..** A consecu
1ed73 74 69 76 65 20 72 61 6e 67 65 20 6f 66 20 72 65  tive range of re
1ed74 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e 67  gisters starting
1ed75 20 61 74 20 72 65 67 52 6f 77 69 64 20 63 6f 6e   at regRowid con
1ed76 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20 72 6f 77  tains the.** row
1ed77 69 64 20 61 6e 64 20 74 68 65 20 63 6f 6e 74 65  id and the conte
1ed78 6e 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65  nt to be inserte
1ed79 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 72 67  d..**.** The arg
1ed7a 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 72  uments to this r
1ed7b 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 65  outine should be
1ed7c 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
1ed7d 20 66 69 72 73 74 20 73 69 78 0a 2a 2a 20 61 72   first six.** ar
1ed7e 67 75 6d 65 6e 74 73 20 74 6f 20 73 71 6c 69 74  guments to sqlit
1ed7f 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72  e3GenerateConstr
1ed80 61 69 6e 74 43 68 65 63 6b 73 2e 0a 2a 2f 0a 53  aintChecks..*/.S
1ed81 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1ed82 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65  id sqlite3Comple
1ed83 74 65 49 6e 73 65 72 74 69 6f 6e 28 0a 20 20 50  teInsertion(.  P
1ed84 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
1ed85 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
1ed86 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61   context */.  Ta
1ed87 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20  ble *pTab,      
1ed88 20 20 2f 2a 20 74 68 65 20 74 61 62 6c 65 20 69    /* the table i
1ed89 6e 74 6f 20 77 68 69 63 68 20 77 65 20 61 72 65  nto which we are
1ed8a 20 69 6e 73 65 72 74 69 6e 67 20 2a 2f 0a 20 20   inserting */.  
1ed8b 69 6e 74 20 62 61 73 65 43 75 72 2c 20 20 20 20  int baseCur,    
1ed8c 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
1ed8d 61 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 72  a read/write cur
1ed8e 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  sor pointing at 
1ed8f 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 72 65  pTab */.  int re
1ed90 67 52 6f 77 69 64 2c 20 20 20 20 20 20 20 2f 2a  gRowid,       /*
1ed91 20 52 61 6e 67 65 20 6f 66 20 63 6f 6e 74 65 6e   Range of conten
1ed92 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67  t */.  int *aReg
1ed93 49 64 78 2c 20 20 20 20 20 20 20 2f 2a 20 52 65  Idx,       /* Re
1ed94 67 69 73 74 65 72 20 75 73 65 64 20 62 79 20 65  gister used by e
1ed95 61 63 68 20 69 6e 64 65 78 2e 20 20 30 20 66 6f  ach index.  0 fo
1ed96 72 20 75 6e 75 73 65 64 20 69 6e 64 69 63 65 73  r unused indices
1ed97 20 2a 2f 0a 20 20 69 6e 74 20 72 6f 77 69 64 43   */.  int rowidC
1ed98 68 6e 67 2c 20 20 20 20 20 20 2f 2a 20 54 72 75  hng,      /* Tru
1ed99 65 20 69 66 20 74 68 65 20 72 65 63 6f 72 64 20  e if the record 
1ed9a 6e 75 6d 62 65 72 20 77 69 6c 6c 20 63 68 61 6e  number will chan
1ed9b 67 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 55 70  ge */.  int isUp
1ed9c 64 61 74 65 2c 20 20 20 20 20 20 20 2f 2a 20 54  date,       /* T
1ed9d 72 75 65 20 66 6f 72 20 55 50 44 41 54 45 2c 20  rue for UPDATE, 
1ed9e 46 61 6c 73 65 20 66 6f 72 20 49 4e 53 45 52 54  False for INSERT
1ed9f 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 49 64 78   */.  int newIdx
1eda0 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64  ,         /* Ind
1eda1 65 78 20 6f 66 20 4e 45 57 20 74 61 62 6c 65 20  ex of NEW table 
1eda2 66 6f 72 20 74 72 69 67 67 65 72 73 2e 20 20 2d  for triggers.  -
1eda3 31 20 69 66 20 6e 6f 6e 65 20 2a 2f 0a 20 20 69  1 if none */.  i
1eda4 6e 74 20 61 70 70 65 6e 64 42 69 61 73 20 20 20  nt appendBias   
1eda5 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
1eda6 69 73 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20  is is likely to 
1eda7 62 65 20 61 6e 20 61 70 70 65 6e 64 20 2a 2f 0a  be an append */.
1eda8 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 56 64  ){.  int i;.  Vd
1eda9 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 6e 49 64  be *v;.  int nId
1edaa 78 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  x;.  Index *pIdx
1edab 3b 0a 20 20 69 6e 74 20 70 69 6b 5f 66 6c 61 67  ;.  int pik_flag
1edac 73 3b 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61  s;.  int regData
1edad 3b 0a 20 20 69 6e 74 20 72 65 67 52 65 63 3b 0a  ;.  int regRec;.
1edae 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65  .  v = sqlite3Ge
1edaf 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
1edb0 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b   assert( v!=0 );
1edb1 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d  .  assert( pTab-
1edb2 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 20 20  >pSelect==0 );  
1edb3 2f 2a 20 54 68 69 73 20 74 61 62 6c 65 20 69 73  /* This table is
1edb4 20 6e 6f 74 20 61 20 56 49 45 57 20 2a 2f 0a 20   not a VIEW */. 
1edb5 20 66 6f 72 28 6e 49 64 78 3d 30 2c 20 70 49 64   for(nIdx=0, pId
1edb6 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
1edb7 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
1edb8 3e 70 4e 65 78 74 2c 20 6e 49 64 78 2b 2b 29 7b  >pNext, nIdx++){
1edb9 7d 0a 20 20 66 6f 72 28 69 3d 6e 49 64 78 2d 31  }.  for(i=nIdx-1
1edba 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20  ; i>=0; i--){.  
1edbb 20 20 69 66 28 20 61 52 65 67 49 64 78 5b 69 5d    if( aRegIdx[i]
1edbc 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==0 ) continue;.
1edbd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1edbe 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49  ddOp2(v, OP_IdxI
1edbf 6e 73 65 72 74 2c 20 62 61 73 65 43 75 72 2b 69  nsert, baseCur+i
1edc0 2b 31 2c 20 61 52 65 67 49 64 78 5b 69 5d 29 3b  +1, aRegIdx[i]);
1edc1 0a 20 20 7d 0a 20 20 72 65 67 44 61 74 61 20 3d  .  }.  regData =
1edc2 20 72 65 67 52 6f 77 69 64 20 2b 20 31 3b 0a 20   regRowid + 1;. 
1edc3 20 72 65 67 52 65 63 20 3d 20 73 71 6c 69 74 65   regRec = sqlite
1edc4 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
1edc5 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  se);.  sqlite3Vd
1edc6 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
1edc7 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 44 61  akeRecord, regDa
1edc8 74 61 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20  ta, pTab->nCol, 
1edc9 72 65 67 52 65 63 29 3b 0a 20 20 73 71 6c 69 74  regRec);.  sqlit
1edca 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79 53  e3TableAffinityS
1edcb 74 72 28 76 2c 20 70 54 61 62 29 3b 0a 20 20 73  tr(v, pTab);.  s
1edcc 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
1edcd 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50  ffinityChange(pP
1edce 61 72 73 65 2c 20 72 65 67 44 61 74 61 2c 20 70  arse, regData, p
1edcf 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 23 69 66 6e  Tab->nCol);.#ifn
1edd0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1edd1 54 52 49 47 47 45 52 0a 20 20 69 66 28 20 6e 65  TRIGGER.  if( ne
1edd2 77 49 64 78 3e 3d 30 20 29 7b 0a 20 20 20 20 73  wIdx>=0 ){.    s
1edd3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
1edd4 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 6e  (v, OP_Insert, n
1edd5 65 77 49 64 78 2c 20 72 65 67 52 65 63 2c 20 72  ewIdx, regRec, r
1edd6 65 67 52 6f 77 69 64 29 3b 0a 20 20 7d 0a 23 65  egRowid);.  }.#e
1edd7 6e 64 69 66 0a 20 20 69 66 28 20 70 50 61 72 73  ndif.  if( pPars
1edd8 65 2d 3e 6e 65 73 74 65 64 20 29 7b 0a 20 20 20  e->nested ){.   
1edd9 20 70 69 6b 5f 66 6c 61 67 73 20 3d 20 30 3b 0a   pik_flags = 0;.
1edda 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 69 6b    }else{.    pik
1eddb 5f 66 6c 61 67 73 20 3d 20 4f 50 46 4c 41 47 5f  _flags = OPFLAG_
1eddc 4e 43 48 41 4e 47 45 3b 0a 20 20 20 20 70 69 6b  NCHANGE;.    pik
1eddd 5f 66 6c 61 67 73 20 7c 3d 20 28 69 73 55 70 64  _flags |= (isUpd
1edde 61 74 65 3f 4f 50 46 4c 41 47 5f 49 53 55 50 44  ate?OPFLAG_ISUPD
1eddf 41 54 45 3a 4f 50 46 4c 41 47 5f 4c 41 53 54 52  ATE:OPFLAG_LASTR
1ede0 4f 57 49 44 29 3b 0a 20 20 7d 0a 20 20 69 66 28  OWID);.  }.  if(
1ede1 20 61 70 70 65 6e 64 42 69 61 73 20 29 7b 0a 20   appendBias ){. 
1ede2 20 20 20 70 69 6b 5f 66 6c 61 67 73 20 7c 3d 20     pik_flags |= 
1ede3 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 3b 0a 20  OPFLAG_APPEND;. 
1ede4 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65   }.  sqlite3Vdbe
1ede5 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73  AddOp3(v, OP_Ins
1ede6 65 72 74 2c 20 62 61 73 65 43 75 72 2c 20 72 65  ert, baseCur, re
1ede7 67 52 65 63 2c 20 72 65 67 52 6f 77 69 64 29 3b  gRec, regRowid);
1ede8 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e  .  if( !pParse->
1ede9 6e 65 73 74 65 64 20 29 7b 0a 20 20 20 20 73 71  nested ){.    sq
1edea 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1edeb 34 28 76 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a  4(v, -1, pTab->z
1edec 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29  Name, P4_STATIC)
1eded 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56  ;.  }.  sqlite3V
1edee 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 70  dbeChangeP5(v, p
1edef 69 6b 5f 66 6c 61 67 73 29 3b 0a 7d 0a 0a 2f 2a  ik_flags);.}../*
1edf0 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
1edf1 65 20 74 68 61 74 20 77 69 6c 6c 20 6f 70 65 6e  e that will open
1edf2 20 63 75 72 73 6f 72 73 20 66 6f 72 20 61 20 74   cursors for a t
1edf3 61 62 6c 65 20 61 6e 64 20 66 6f 72 20 61 6c 6c  able and for all
1edf4 0a 2a 2a 20 69 6e 64 69 63 65 73 20 6f 66 20 74  .** indices of t
1edf5 68 61 74 20 74 61 62 6c 65 2e 20 20 54 68 65 20  hat table.  The 
1edf6 22 62 61 73 65 43 75 72 22 20 70 61 72 61 6d 65  "baseCur" parame
1edf7 74 65 72 20 69 73 20 74 68 65 20 63 75 72 73 6f  ter is the curso
1edf8 72 20 6e 75 6d 62 65 72 20 75 73 65 64 0a 2a 2a  r number used.**
1edf9 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 2e 20   for the table. 
1edfa 20 49 6e 64 69 63 65 73 20 61 72 65 20 6f 70 65   Indices are ope
1edfb 6e 65 64 20 6f 6e 20 73 75 62 73 65 71 75 65 6e  ned on subsequen
1edfc 74 20 63 75 72 73 6f 72 73 2e 0a 2a 2a 0a 2a 2a  t cursors..**.**
1edfd 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
1edfe 65 72 20 6f 66 20 69 6e 64 69 63 65 73 20 6f 6e  er of indices on
1edff 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53   the table..*/.S
1ee00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1ee01 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62  t sqlite3OpenTab
1ee02 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 0a 20 20  leAndIndices(.  
1ee03 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
1ee04 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
1ee05 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ext */.  Table *
1ee06 70 54 61 62 2c 20 20 20 20 20 2f 2a 20 54 61 62  pTab,     /* Tab
1ee07 6c 65 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20  le to be opened 
1ee08 2a 2f 0a 20 20 69 6e 74 20 62 61 73 65 43 75 72  */.  int baseCur
1ee09 2c 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73  ,        /* Curs
1ee0a 6f 72 20 6e 75 6d 62 65 72 20 61 73 73 69 67 6e  or number assign
1ee0b 65 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20  ed to the table 
1ee0c 2a 2f 0a 20 20 69 6e 74 20 6f 70 20 20 20 20 20  */.  int op     
1ee0d 20 20 20 20 20 20 2f 2a 20 4f 50 5f 4f 70 65 6e        /* OP_Open
1ee0e 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70 65 6e 57  Read or OP_OpenW
1ee0f 72 69 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  rite */.){.  int
1ee10 20 69 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20   i;.  int iDb;. 
1ee11 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20   Index *pIdx;.  
1ee12 56 64 62 65 20 2a 76 3b 0a 0a 20 20 69 66 28 20  Vdbe *v;..  if( 
1ee13 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
1ee14 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 44  ) return 0;.  iD
1ee15 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
1ee16 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d  aToIndex(pParse-
1ee17 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  >db, pTab->pSche
1ee18 6d 61 29 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74  ma);.  v = sqlit
1ee19 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
1ee1a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d  );.  assert( v!=
1ee1b 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 70  0 );.  sqlite3Op
1ee1c 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  enTable(pParse, 
1ee1d 62 61 73 65 43 75 72 2c 20 69 44 62 2c 20 70 54  baseCur, iDb, pT
1ee1e 61 62 2c 20 6f 70 29 3b 0a 20 20 66 6f 72 28 69  ab, op);.  for(i
1ee1f 3d 31 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70  =1, pIdx=pTab->p
1ee20 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
1ee21 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69  x=pIdx->pNext, i
1ee22 2b 2b 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f  ++){.    KeyInfo
1ee23 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33   *pKey = sqlite3
1ee24 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61  IndexKeyinfo(pPa
1ee25 72 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 20 20  rse, pIdx);.    
1ee26 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 70 53  assert( pIdx->pS
1ee27 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63  chema==pTab->pSc
1ee28 68 65 6d 61 20 29 3b 0a 20 20 20 20 73 71 6c 69  hema );.    sqli
1ee29 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
1ee2a 20 6f 70 2c 20 69 2b 62 61 73 65 43 75 72 2c 20   op, i+baseCur, 
1ee2b 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c  pIdx->tnum, iDb,
1ee2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1ee2d 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b         (char*)pK
1ee2e 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48  ey, P4_KEYINFO_H
1ee2f 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 56 64 62  ANDOFF);.    Vdb
1ee30 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73  eComment((v, "%s
1ee31 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 29  ", pIdx->zName))
1ee32 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72  ;.  }.  if( pPar
1ee33 73 65 2d 3e 6e 54 61 62 3c 3d 62 61 73 65 43 75  se->nTab<=baseCu
1ee34 72 2b 69 20 29 7b 0a 20 20 20 20 70 50 61 72 73  r+i ){.    pPars
1ee35 65 2d 3e 6e 54 61 62 20 3d 20 62 61 73 65 43 75  e->nTab = baseCu
1ee36 72 2b 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  r+i;.  }.  retur
1ee37 6e 20 69 2d 31 3b 0a 7d 0a 0a 0a 23 69 66 64 65  n i-1;.}...#ifde
1ee38 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a  f SQLITE_TEST./*
1ee39 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
1ee3a 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  g global variabl
1ee3b 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  e is incremented
1ee3c 20 77 68 65 6e 65 76 65 72 20 74 68 65 0a 2a 2a   whenever the.**
1ee3d 20 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69   transfer optimi
1ee3e 7a 61 74 69 6f 6e 20 69 73 20 75 73 65 64 2e 20  zation is used. 
1ee3f 20 54 68 69 73 20 69 73 20 75 73 65 64 20 66 6f   This is used fo
1ee40 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20 70 75 72  r testing.** pur
1ee41 70 6f 73 65 73 20 6f 6e 6c 79 20 2d 20 74 6f 20  poses only - to 
1ee42 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 74 72  make sure the tr
1ee43 61 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74  ansfer optimizat
1ee44 69 6f 6e 20 72 65 61 6c 6c 79 0a 2a 2a 20 69 73  ion really.** is
1ee45 20 68 61 70 70 65 6e 69 6e 67 20 77 68 65 6e 20   happening when 
1ee46 69 74 20 69 73 20 73 75 70 70 6f 73 65 20 74 6f  it is suppose to
1ee47 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1ee48 69 6e 74 20 73 71 6c 69 74 65 33 5f 78 66 65 72  int sqlite3_xfer
1ee49 6f 70 74 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64 69  opt_count;.#endi
1ee4a 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54  f /* SQLITE_TEST
1ee4b 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51   */...#ifndef SQ
1ee4c 4c 49 54 45 5f 4f 4d 49 54 5f 58 46 45 52 5f 4f  LITE_OMIT_XFER_O
1ee4d 50 54 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74  PT./*.** Check t
1ee4e 6f 20 63 6f 6c 6c 61 74 69 6f 6e 20 6e 61 6d 65  o collation name
1ee4f 73 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 79  s to see if they
1ee50 20 61 72 65 20 63 6f 6d 70 61 74 69 62 6c 65 2e   are compatible.
1ee51 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 78  .*/.static int x
1ee52 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 43 6f 6c  ferCompatibleCol
1ee53 6c 61 74 69 6f 6e 28 63 6f 6e 73 74 20 63 68 61  lation(const cha
1ee54 72 20 2a 7a 31 2c 20 63 6f 6e 73 74 20 63 68 61  r *z1, const cha
1ee55 72 20 2a 7a 32 29 7b 0a 20 20 69 66 28 20 7a 31  r *z2){.  if( z1
1ee56 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
1ee57 6e 20 7a 32 3d 3d 30 3b 0a 20 20 7d 0a 20 20 69  n z2==0;.  }.  i
1ee58 66 28 20 7a 32 3d 3d 30 20 29 7b 0a 20 20 20 20  f( z2==0 ){.    
1ee59 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
1ee5a 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 53 74  return sqlite3St
1ee5b 72 49 43 6d 70 28 7a 31 2c 20 7a 32 29 3d 3d 30  rICmp(z1, z2)==0
1ee5c 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  ;.}.../*.** Chec
1ee5d 6b 20 74 6f 20 73 65 65 20 69 66 20 69 6e 64 65  k to see if inde
1ee5e 78 20 70 53 72 63 20 69 73 20 63 6f 6d 70 61 74  x pSrc is compat
1ee5f 69 62 6c 65 20 61 73 20 61 20 73 6f 75 72 63 65  ible as a source
1ee60 20 6f 66 20 64 61 74 61 0a 2a 2a 20 66 6f 72 20   of data.** for 
1ee61 69 6e 64 65 78 20 70 44 65 73 74 20 69 6e 20 61  index pDest in a
1ee62 6e 20 69 6e 73 65 72 74 20 74 72 61 6e 73 66 65  n insert transfe
1ee63 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20  r optimization. 
1ee64 20 54 68 65 20 72 75 6c 65 73 0a 2a 2a 20 66 6f   The rules.** fo
1ee65 72 20 61 20 63 6f 6d 70 61 74 69 62 6c 65 20 69  r a compatible i
1ee66 6e 64 65 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a  ndex:.**.**    *
1ee67 20 20 20 54 68 65 20 69 6e 64 65 78 20 69 73 20     The index is 
1ee68 6f 76 65 72 20 74 68 65 20 73 61 6d 65 20 73 65  over the same se
1ee69 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20  t of columns.** 
1ee6a 20 20 20 2a 20 20 20 54 68 65 20 73 61 6d 65 20     *   The same 
1ee6b 44 45 53 43 20 61 6e 64 20 41 53 43 20 6d 61 72  DESC and ASC mar
1ee6c 6b 69 6e 67 73 20 6f 63 63 75 72 73 20 6f 6e 20  kings occurs on 
1ee6d 61 6c 6c 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 20  all columns.**  
1ee6e 20 20 2a 20 20 20 54 68 65 20 73 61 6d 65 20 6f    *   The same o
1ee6f 6e 45 72 72 6f 72 20 70 72 6f 63 65 73 73 69 6e  nError processin
1ee70 67 20 28 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f  g (OE_Abort, OE_
1ee71 49 67 6e 6f 72 65 2c 20 65 74 63 29 0a 2a 2a 20  Ignore, etc).** 
1ee72 20 20 20 2a 20 20 20 54 68 65 20 73 61 6d 65 20     *   The same 
1ee73 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
1ee74 63 65 20 6f 6e 20 65 61 63 68 20 63 6f 6c 75 6d  ce on each colum
1ee75 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  n.*/.static int 
1ee76 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 49 6e  xferCompatibleIn
1ee77 64 65 78 28 49 6e 64 65 78 20 2a 70 44 65 73 74  dex(Index *pDest
1ee78 2c 20 49 6e 64 65 78 20 2a 70 53 72 63 29 7b 0a  , Index *pSrc){.
1ee79 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72    int i;.  asser
1ee7a 74 28 20 70 44 65 73 74 20 26 26 20 70 53 72 63  t( pDest && pSrc
1ee7b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44   );.  assert( pD
1ee7c 65 73 74 2d 3e 70 54 61 62 6c 65 21 3d 70 53 72  est->pTable!=pSr
1ee7d 63 2d 3e 70 54 61 62 6c 65 20 29 3b 0a 20 20 69  c->pTable );.  i
1ee7e 66 28 20 70 44 65 73 74 2d 3e 6e 43 6f 6c 75 6d  f( pDest->nColum
1ee7f 6e 21 3d 70 53 72 63 2d 3e 6e 43 6f 6c 75 6d 6e  n!=pSrc->nColumn
1ee80 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
1ee81 3b 20 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74  ;   /* Different
1ee82 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
1ee83 6e 73 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  ns */.  }.  if( 
1ee84 70 44 65 73 74 2d 3e 6f 6e 45 72 72 6f 72 21 3d  pDest->onError!=
1ee85 70 53 72 63 2d 3e 6f 6e 45 72 72 6f 72 20 29 7b  pSrc->onError ){
1ee86 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
1ee87 20 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 63 6f   /* Different co
1ee88 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f  nflict resolutio
1ee89 6e 20 73 74 72 61 74 65 67 69 65 73 20 2a 2f 0a  n strategies */.
1ee8a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69    }.  for(i=0; i
1ee8b 3c 70 53 72 63 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20  <pSrc->nColumn; 
1ee8c 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 53  i++){.    if( pS
1ee8d 72 63 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 21  rc->aiColumn[i]!
1ee8e 3d 70 44 65 73 74 2d 3e 61 69 43 6f 6c 75 6d 6e  =pDest->aiColumn
1ee8f 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 72 65 74  [i] ){.      ret
1ee90 75 72 6e 20 30 3b 20 20 20 2f 2a 20 44 69 66 66  urn 0;   /* Diff
1ee91 65 72 65 6e 74 20 63 6f 6c 75 6d 6e 73 20 69 6e  erent columns in
1ee92 64 65 78 65 64 20 2a 2f 0a 20 20 20 20 7d 0a 20  dexed */.    }. 
1ee93 20 20 20 69 66 28 20 70 53 72 63 2d 3e 61 53 6f     if( pSrc->aSo
1ee94 72 74 4f 72 64 65 72 5b 69 5d 21 3d 70 44 65 73  rtOrder[i]!=pDes
1ee95 74 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d  t->aSortOrder[i]
1ee96 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
1ee97 20 30 3b 20 20 20 2f 2a 20 44 69 66 66 65 72 65   0;   /* Differe
1ee98 6e 74 20 73 6f 72 74 20 6f 72 64 65 72 73 20 2a  nt sort orders *
1ee99 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  /.    }.    if( 
1ee9a 70 53 72 63 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 21  pSrc->azColl[i]!
1ee9b 3d 70 44 65 73 74 2d 3e 61 7a 43 6f 6c 6c 5b 69  =pDest->azColl[i
1ee9c 5d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ] ){.      retur
1ee9d 6e 20 30 3b 20 20 20 2f 2a 20 44 69 66 66 65 72  n 0;   /* Differ
1ee9e 65 6e 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  ent collating se
1ee9f 71 75 65 6e 63 65 73 20 2a 2f 0a 20 20 20 20 7d  quences */.    }
1eea0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f  .  }..  /* If no
1eea1 20 74 65 73 74 20 61 62 6f 76 65 20 66 61 69 6c   test above fail
1eea2 73 20 74 68 65 6e 20 74 68 65 20 69 6e 64 69 63  s then the indic
1eea3 65 73 20 6d 75 73 74 20 62 65 20 63 6f 6d 70 61  es must be compa
1eea4 74 69 62 6c 65 20 2a 2f 0a 20 20 72 65 74 75 72  tible */.  retur
1eea5 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74  n 1;.}../*.** At
1eea6 74 65 6d 70 74 20 74 68 65 20 74 72 61 6e 73 66  tempt the transf
1eea7 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  er optimization 
1eea8 6f 6e 20 49 4e 53 45 52 54 73 20 6f 66 20 74 68  on INSERTs of th
1eea9 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20  e form.**.**    
1eeaa 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62   INSERT INTO tab
1eeab 31 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  1 SELECT * FROM 
1eeac 74 61 62 32 3b 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tab2;.**.** This
1eead 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73   optimization is
1eeae 20 6f 6e 6c 79 20 61 74 74 65 6d 70 74 65 64 20   only attempted 
1eeaf 69 66 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20  if.**.**    (1) 
1eeb0 20 74 61 62 31 20 61 6e 64 20 74 61 62 32 20 68   tab1 and tab2 h
1eeb1 61 76 65 20 69 64 65 6e 74 69 63 61 6c 20 73 63  ave identical sc
1eeb2 68 65 6d 61 73 20 69 6e 63 6c 75 64 69 6e 67 20  hemas including 
1eeb3 61 6c 6c 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  all the.**      
1eeb4 20 20 20 73 61 6d 65 20 69 6e 64 69 63 65 73 20     same indices 
1eeb5 61 6e 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a  and constraints.
1eeb6 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 20 74 61  **.**    (2)  ta
1eeb7 62 31 20 61 6e 64 20 74 61 62 32 20 61 72 65 20  b1 and tab2 are 
1eeb8 64 69 66 66 65 72 65 6e 74 20 74 61 62 6c 65 73  different tables
1eeb9 0a 2a 2a 0a 2a 2a 20 20 20 20 28 33 29 20 20 54  .**.**    (3)  T
1eeba 68 65 72 65 20 6d 75 73 74 20 62 65 20 6e 6f 20  here must be no 
1eebb 74 72 69 67 67 65 72 73 20 6f 6e 20 74 61 62 31  triggers on tab1
1eebc 0a 2a 2a 0a 2a 2a 20 20 20 20 28 34 29 20 20 54  .**.**    (4)  T
1eebd 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  he result set of
1eebe 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
1eebf 65 6d 65 6e 74 20 69 73 20 22 2a 22 0a 2a 2a 0a  ement is "*".**.
1eec0 2a 2a 20 20 20 20 28 35 29 20 20 54 68 65 20 53  **    (5)  The S
1eec1 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
1eec2 68 61 73 20 6e 6f 20 57 48 45 52 45 2c 20 48 41  has no WHERE, HA
1eec3 56 49 4e 47 2c 20 4f 52 44 45 52 20 42 59 2c 20  VING, ORDER BY, 
1eec4 47 52 4f 55 50 20 42 59 2c 0a 2a 2a 20 20 20 20  GROUP BY,.**    
1eec5 20 20 20 20 20 6f 72 20 4c 49 4d 49 54 20 63 6c       or LIMIT cl
1eec6 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28  ause..**.**    (
1eec7 36 29 20 20 54 68 65 20 53 45 4c 45 43 54 20 73  6)  The SELECT s
1eec8 74 61 74 65 6d 65 6e 74 20 69 73 20 61 20 73 69  tatement is a si
1eec9 6d 70 6c 65 20 28 6e 6f 74 20 61 20 63 6f 6d 70  mple (not a comp
1eeca 6f 75 6e 64 29 20 73 65 6c 65 63 74 20 74 68 61  ound) select tha
1eecb 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6e  t.**         con
1eecc 74 61 69 6e 73 20 6f 6e 6c 79 20 74 61 62 32 20  tains only tab2 
1eecd 69 6e 20 69 74 73 20 46 52 4f 4d 20 63 6c 61 75  in its FROM clau
1eece 73 65 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 65  se.**.** This me
1eecf 74 68 6f 64 20 66 6f 72 20 69 6d 70 6c 65 6d 65  thod for impleme
1eed0 6e 74 69 6e 67 20 74 68 65 20 49 4e 53 45 52 54  nting the INSERT
1eed1 20 74 72 61 6e 73 66 65 72 73 20 72 61 77 20 72   transfers raw r
1eed2 65 63 6f 72 64 73 20 66 72 6f 6d 0a 2a 2a 20 74  ecords from.** t
1eed3 61 62 32 20 6f 76 65 72 20 74 6f 20 74 61 62 31  ab2 over to tab1
1eed4 2e 20 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 61  .  The columns a
1eed5 72 65 20 6e 6f 74 20 64 65 63 6f 64 65 64 2e 20  re not decoded. 
1eed6 20 52 61 77 20 72 65 63 6f 72 64 73 20 66 72 6f   Raw records fro
1eed7 6d 0a 2a 2a 20 74 68 65 20 69 6e 64 69 63 65 73  m.** the indices
1eed8 20 6f 66 20 74 61 62 32 20 61 72 65 20 74 72 61   of tab2 are tra
1eed9 6e 73 66 65 72 65 64 20 74 6f 20 74 61 62 31 20  nsfered to tab1 
1eeda 61 73 20 77 65 6c 6c 2e 20 20 49 6e 20 73 6f 20  as well.  In so 
1eedb 64 6f 69 6e 67 2c 0a 2a 2a 20 74 68 65 20 72 65  doing,.** the re
1eedc 73 75 6c 74 69 6e 67 20 74 61 62 31 20 68 61 73  sulting tab1 has
1eedd 20 6d 75 63 68 20 6c 65 73 73 20 66 72 61 67 6d   much less fragm
1eede 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  entation..**.** 
1eedf 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  This routine ret
1eee0 75 72 6e 73 20 54 52 55 45 20 69 66 20 74 68 65  urns TRUE if the
1eee1 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73   optimization is
1eee2 20 61 74 74 65 6d 70 74 65 64 2e 20 20 49 66 20   attempted.  If 
1eee3 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 65 20 63 6f  any.** of the co
1eee4 6e 64 69 74 69 6f 6e 73 20 61 62 6f 76 65 20 66  nditions above f
1eee5 61 69 6c 20 73 6f 20 74 68 61 74 20 74 68 65 20  ail so that the 
1eee6 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 73 68 6f  optimization sho
1eee7 75 6c 64 20 6e 6f 74 0a 2a 2a 20 62 65 20 61 74  uld not.** be at
1eee8 74 65 6d 70 74 65 64 2c 20 74 68 65 6e 20 74 68  tempted, then th
1eee9 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
1eeea 6e 73 20 46 41 4c 53 45 2e 0a 2a 2f 0a 73 74 61  ns FALSE..*/.sta
1eeeb 74 69 63 20 69 6e 74 20 78 66 65 72 4f 70 74 69  tic int xferOpti
1eeec 6d 69 7a 61 74 69 6f 6e 28 0a 20 20 50 61 72 73  mization(.  Pars
1eeed 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
1eeee 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74    /* Parser cont
1eeef 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ext */.  Table *
1eef0 70 44 65 73 74 2c 20 20 20 20 20 20 20 20 20 2f  pDest,         /
1eef1 2a 20 54 68 65 20 74 61 62 6c 65 20 77 65 20 61  * The table we a
1eef2 72 65 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 74  re inserting int
1eef3 6f 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  o */.  Select *p
1eef4 53 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20  Select,      /* 
1eef5 41 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  A SELECT stateme
1eef6 6e 74 20 74 6f 20 75 73 65 20 61 73 20 74 68 65  nt to use as the
1eef7 20 64 61 74 61 20 73 6f 75 72 63 65 20 2a 2f 0a   data source */.
1eef8 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20    int onError,  
1eef9 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74          /* How t
1eefa 6f 20 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61  o handle constra
1eefb 69 6e 74 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20  int errors */.  
1eefc 69 6e 74 20 69 44 62 44 65 73 74 20 20 20 20 20  int iDbDest     
1eefd 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
1eefe 61 62 61 73 65 20 6f 66 20 70 44 65 73 74 20 2a  abase of pDest *
1eeff 2f 0a 29 7b 0a 20 20 45 78 70 72 4c 69 73 74 20  /.){.  ExprList 
1ef00 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20 20 20  *pEList;        
1ef01 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
1ef02 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65  esult set of the
1ef03 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 54 61 62   SELECT */.  Tab
1ef04 6c 65 20 2a 70 53 72 63 3b 20 20 20 20 20 20 20  le *pSrc;       
1ef05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ef06 20 54 68 65 20 74 61 62 6c 65 20 69 6e 20 74 68   The table in th
1ef07 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66  e FROM clause of
1ef08 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 49 6e 64   SELECT */.  Ind
1ef09 65 78 20 2a 70 53 72 63 49 64 78 2c 20 2a 70 44  ex *pSrcIdx, *pD
1ef0a 65 73 74 49 64 78 3b 20 20 20 20 20 20 20 2f 2a  estIdx;       /*
1ef0b 20 53 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74   Source and dest
1ef0c 69 6e 61 74 69 6f 6e 20 69 6e 64 69 63 65 73 20  ination indices 
1ef0d 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  */.  struct SrcL
1ef0e 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
1ef0f 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 6c 65 6d        /* An elem
1ef10 65 6e 74 20 6f 66 20 70 53 65 6c 65 63 74 2d 3e  ent of pSelect->
1ef11 70 53 72 63 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  pSrc */.  int i;
1ef12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ef13 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
1ef14 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
1ef15 69 6e 74 20 69 44 62 53 72 63 3b 20 20 20 20 20  int iDbSrc;     
1ef16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ef17 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
1ef18 20 6f 66 20 70 53 72 63 20 2a 2f 0a 20 20 69 6e   of pSrc */.  in
1ef19 74 20 69 53 72 63 2c 20 69 44 65 73 74 3b 20 20  t iSrc, iDest;  
1ef1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1ef1b 2a 20 43 75 72 73 6f 72 73 20 66 72 6f 6d 20 73  * Cursors from s
1ef1c 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e  ource and destin
1ef1d 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 61  ation */.  int a
1ef1e 64 64 72 31 2c 20 61 64 64 72 32 3b 20 20 20 20  ddr1, addr2;    
1ef1f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1ef20 6f 6f 70 20 61 64 64 72 65 73 73 65 73 20 2a 2f  oop addresses */
1ef21 0a 20 20 69 6e 74 20 65 6d 70 74 79 44 65 73 74  .  int emptyDest
1ef22 54 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  Test;           
1ef23 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
1ef24 66 20 74 65 73 74 20 66 6f 72 20 65 6d 70 74 79  f test for empty
1ef25 20 70 44 65 73 74 20 2a 2f 0a 20 20 69 6e 74 20   pDest */.  int 
1ef26 65 6d 70 74 79 53 72 63 54 65 73 74 3b 20 20 20  emptySrcTest;   
1ef27 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1ef28 41 64 64 72 65 73 73 20 6f 66 20 74 65 73 74 20  Address of test 
1ef29 66 6f 72 20 65 6d 70 74 79 20 70 53 72 63 20 2a  for empty pSrc *
1ef2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20  /.  Vdbe *v;    
1ef2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ef2c 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45       /* The VDBE
1ef2d 20 77 65 20 61 72 65 20 62 75 69 6c 64 69 6e 67   we are building
1ef2e 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70   */.  KeyInfo *p
1ef2f 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  Key;            
1ef30 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 20 69 6e         /* Key in
1ef31 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 6e  formation for an
1ef32 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20   index */.  int 
1ef33 72 65 67 41 75 74 6f 69 6e 63 3b 20 20 20 20 20  regAutoinc;     
1ef34 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1ef35 4d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 20  Memory register 
1ef36 75 73 65 64 20 62 79 20 41 55 54 4f 49 4e 43 20  used by AUTOINC 
1ef37 2a 2f 0a 20 20 69 6e 74 20 64 65 73 74 48 61 73  */.  int destHas
1ef38 55 6e 69 71 75 65 49 64 78 20 3d 20 30 3b 20 20  UniqueIdx = 0;  
1ef39 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
1ef3a 20 70 44 65 73 74 20 68 61 73 20 61 20 55 4e 49   pDest has a UNI
1ef3b 51 55 45 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69  QUE index */.  i
1ef3c 6e 74 20 72 65 67 44 61 74 61 2c 20 72 65 67 52  nt regData, regR
1ef3d 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 20  owid;           
1ef3e 2f 2a 20 52 65 67 69 73 74 65 72 73 20 68 6f 6c  /* Registers hol
1ef3f 64 69 6e 67 20 64 61 74 61 20 61 6e 64 20 72 6f  ding data and ro
1ef40 77 69 64 20 2a 2f 0a 0a 20 20 69 66 28 20 70 53  wid */..  if( pS
1ef41 65 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20  elect==0 ){.    
1ef42 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 4d  return 0;   /* M
1ef43 75 73 74 20 62 65 20 6f 66 20 74 68 65 20 66 6f  ust be of the fo
1ef44 72 6d 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20  rm  INSERT INTO 
1ef45 2e 2e 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20 2a  ... SELECT ... *
1ef46 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 65 73  /.  }.  if( pDes
1ef47 74 2d 3e 70 54 72 69 67 67 65 72 20 29 7b 0a 20  t->pTrigger ){. 
1ef48 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
1ef49 2a 20 74 61 62 31 20 6d 75 73 74 20 6e 6f 74 20  * tab1 must not 
1ef4a 68 61 76 65 20 74 72 69 67 67 65 72 73 20 2a 2f  have triggers */
1ef4b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .  }.#ifndef SQL
1ef4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1ef4d 54 41 42 4c 45 0a 20 20 69 66 28 20 70 44 65 73  TABLE.  if( pDes
1ef4e 74 2d 3e 69 73 56 69 72 74 75 61 6c 20 29 7b 0a  t->isVirtual ){.
1ef4f 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
1ef50 2f 2a 20 74 61 62 31 20 6d 75 73 74 20 6e 6f 74  /* tab1 must not
1ef51 20 62 65 20 61 20 76 69 72 74 75 61 6c 20 74 61   be a virtual ta
1ef52 62 6c 65 20 2a 2f 0a 20 20 7d 0a 23 65 6e 64 69  ble */.  }.#endi
1ef53 66 0a 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d  f.  if( onError=
1ef54 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20  =OE_Default ){. 
1ef55 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f     onError = OE_
1ef56 41 62 6f 72 74 3b 0a 20 20 7d 0a 20 20 69 66 28  Abort;.  }.  if(
1ef57 20 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 41 62 6f   onError!=OE_Abo
1ef58 72 74 20 26 26 20 6f 6e 45 72 72 6f 72 21 3d 4f  rt && onError!=O
1ef59 45 5f 52 6f 6c 6c 62 61 63 6b 20 29 7b 0a 20 20  E_Rollback ){.  
1ef5a 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
1ef5b 20 43 61 6e 6e 6f 74 20 64 6f 20 4f 52 20 52 45   Cannot do OR RE
1ef5c 50 4c 41 43 45 20 6f 72 20 4f 52 20 49 47 4e 4f  PLACE or OR IGNO
1ef5d 52 45 20 6f 72 20 4f 52 20 46 41 49 4c 20 2a 2f  RE or OR FAIL */
1ef5e 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 70 53  .  }.  assert(pS
1ef5f 65 6c 65 63 74 2d 3e 70 53 72 63 29 3b 20 20 20  elect->pSrc);   
1ef60 2f 2a 20 61 6c 6c 6f 63 61 74 65 64 20 65 76 65  /* allocated eve
1ef61 6e 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f  n if there is no
1ef62 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a   FROM clause */.
1ef63 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70    if( pSelect->p
1ef64 53 72 63 2d 3e 6e 53 72 63 21 3d 31 20 29 7b 0a  Src->nSrc!=1 ){.
1ef65 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
1ef66 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6d  /* FROM clause m
1ef67 75 73 74 20 68 61 76 65 20 65 78 61 63 74 6c 79  ust have exactly
1ef68 20 6f 6e 65 20 74 65 72 6d 20 2a 2f 0a 20 20 7d   one term */.  }
1ef69 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e  .  if( pSelect->
1ef6a 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 53 65 6c 65  pSrc->a[0].pSele
1ef6b 63 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ct ){.    return
1ef6c 20 30 3b 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c   0;   /* FROM cl
1ef6d 61 75 73 65 20 63 61 6e 6e 6f 74 20 63 6f 6e 74  ause cannot cont
1ef6e 61 69 6e 20 61 20 73 75 62 71 75 65 72 79 20 2a  ain a subquery *
1ef6f 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 65 6c  /.  }.  if( pSel
1ef70 65 63 74 2d 3e 70 57 68 65 72 65 20 29 7b 0a 20  ect->pWhere ){. 
1ef71 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
1ef72 2a 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74  * SELECT may not
1ef73 20 68 61 76 65 20 61 20 57 48 45 52 45 20 63 6c   have a WHERE cl
1ef74 61 75 73 65 20 2a 2f 0a 20 20 7d 0a 20 20 69 66  ause */.  }.  if
1ef75 28 20 70 53 65 6c 65 63 74 2d 3e 70 4f 72 64 65  ( pSelect->pOrde
1ef76 72 42 79 20 29 7b 0a 20 20 20 20 72 65 74 75 72  rBy ){.    retur
1ef77 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54  n 0;   /* SELECT
1ef78 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 61 6e   may not have an
1ef79 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
1ef7a 20 2a 2f 0a 20 20 7d 0a 20 20 2f 2a 20 44 6f 20   */.  }.  /* Do 
1ef7b 6e 6f 74 20 6e 65 65 64 20 74 6f 20 74 65 73 74  not need to test
1ef7c 20 66 6f 72 20 61 20 48 41 56 49 4e 47 20 63 6c   for a HAVING cl
1ef7d 61 75 73 65 2e 20 20 49 66 20 48 41 56 49 4e 47  ause.  If HAVING
1ef7e 20 69 73 20 70 72 65 73 65 6e 74 20 62 75 74 0a   is present but.
1ef7f 20 20 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f    ** there is no
1ef80 20 4f 52 44 45 52 20 42 59 2c 20 77 65 20 77 69   ORDER BY, we wi
1ef81 6c 6c 20 67 65 74 20 61 6e 20 65 72 72 6f 72 2e  ll get an error.
1ef82 20 2a 2f 0a 20 20 69 66 28 20 70 53 65 6c 65 63   */.  if( pSelec
1ef83 74 2d 3e 70 47 72 6f 75 70 42 79 20 29 7b 0a 20  t->pGroupBy ){. 
1ef84 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
1ef85 2a 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74  * SELECT may not
1ef86 20 68 61 76 65 20 61 20 47 52 4f 55 50 20 42 59   have a GROUP BY
1ef87 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 7d 0a 20   clause */.  }. 
1ef88 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 4c   if( pSelect->pL
1ef89 69 6d 69 74 20 29 7b 0a 20 20 20 20 72 65 74 75  imit ){.    retu
1ef8a 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43  rn 0;   /* SELEC
1ef8b 54 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 61  T may not have a
1ef8c 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 2a 2f   LIMIT clause */
1ef8d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
1ef8e 53 65 6c 65 63 74 2d 3e 70 4f 66 66 73 65 74 3d  Select->pOffset=
1ef8f 3d 30 20 29 3b 20 20 2f 2a 20 4d 75 73 74 20 62  =0 );  /* Must b
1ef90 65 20 73 6f 20 69 66 20 70 4c 69 6d 69 74 3d 3d  e so if pLimit==
1ef91 30 20 2a 2f 0a 20 20 69 66 28 20 70 53 65 6c 65  0 */.  if( pSele
1ef92 63 74 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20  ct->pPrior ){.  
1ef93 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
1ef94 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20   SELECT may not 
1ef95 62 65 20 61 20 63 6f 6d 70 6f 75 6e 64 20 71 75  be a compound qu
1ef96 65 72 79 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28  ery */.  }.  if(
1ef97 20 70 53 65 6c 65 63 74 2d 3e 69 73 44 69 73 74   pSelect->isDist
1ef98 69 6e 63 74 20 29 7b 0a 20 20 20 20 72 65 74 75  inct ){.    retu
1ef99 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43  rn 0;   /* SELEC
1ef9a 54 20 6d 61 79 20 6e 6f 74 20 62 65 20 44 49 53  T may not be DIS
1ef9b 54 49 4e 43 54 20 2a 2f 0a 20 20 7d 0a 20 20 70  TINCT */.  }.  p
1ef9c 45 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d  EList = pSelect-
1ef9d 3e 70 45 4c 69 73 74 3b 0a 20 20 61 73 73 65 72  >pEList;.  asser
1ef9e 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a  t( pEList!=0 );.
1ef9f 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 6e 45    if( pEList->nE
1efa0 78 70 72 21 3d 31 20 29 7b 0a 20 20 20 20 72 65  xpr!=1 ){.    re
1efa1 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 54 68 65  turn 0;   /* The
1efa2 20 72 65 73 75 6c 74 20 73 65 74 20 6d 75 73 74   result set must
1efa3 20 68 61 76 65 20 65 78 61 63 74 6c 79 20 6f 6e   have exactly on
1efa4 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 7d 0a  e column */.  }.
1efa5 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74    assert( pEList
1efa6 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 20 29 3b 0a  ->a[0].pExpr );.
1efa7 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 61 5b    if( pEList->a[
1efa8 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b  0].pExpr->op!=TK
1efa9 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 72 65 74 75  _ALL ){.    retu
1efaa 72 6e 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 72  rn 0;   /* The r
1efab 65 73 75 6c 74 20 73 65 74 20 6d 75 73 74 20 62  esult set must b
1efac 65 20 74 68 65 20 73 70 65 63 69 61 6c 20 6f 70  e the special op
1efad 65 72 61 74 6f 72 20 22 2a 22 20 2a 2f 0a 20 20  erator "*" */.  
1efae 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20  }..  /* At this 
1efaf 70 6f 69 6e 74 20 77 65 20 68 61 76 65 20 65 73  point we have es
1efb0 74 61 62 6c 69 73 68 65 64 20 74 68 61 74 20 74  tablished that t
1efb1 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  he statement is 
1efb2 6f 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f 72 72  of the.  ** corr
1efb3 65 63 74 20 73 79 6e 74 61 63 74 69 63 20 66 6f  ect syntactic fo
1efb4 72 6d 20 74 6f 20 70 61 72 74 69 63 69 70 61 74  rm to participat
1efb5 65 20 69 6e 20 74 68 69 73 20 6f 70 74 69 6d 69  e in this optimi
1efb6 7a 61 74 69 6f 6e 2e 20 20 4e 6f 77 0a 20 20 2a  zation.  Now.  *
1efb7 2a 20 77 65 20 68 61 76 65 20 74 6f 20 63 68 65  * we have to che
1efb8 63 6b 20 74 68 65 20 73 65 6d 61 6e 74 69 63 73  ck the semantics
1efb9 2e 0a 20 20 2a 2f 0a 20 20 70 49 74 65 6d 20 3d  ..  */.  pItem =
1efba 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 2d 3e   pSelect->pSrc->
1efbb 61 3b 0a 20 20 70 53 72 63 20 3d 20 73 71 6c 69  a;.  pSrc = sqli
1efbc 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70  te3LocateTable(p
1efbd 50 61 72 73 65 2c 20 30 2c 20 70 49 74 65 6d 2d  Parse, 0, pItem-
1efbe 3e 7a 4e 61 6d 65 2c 20 70 49 74 65 6d 2d 3e 7a  >zName, pItem->z
1efbf 44 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28  Database);.  if(
1efc0 20 70 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20   pSrc==0 ){.    
1efc1 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 46  return 0;   /* F
1efc2 52 4f 4d 20 63 6c 61 75 73 65 20 64 6f 65 73 20  ROM clause does 
1efc3 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 72 65  not contain a re
1efc4 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7d 0a  al table */.  }.
1efc5 20 20 69 66 28 20 70 53 72 63 3d 3d 70 44 65 73    if( pSrc==pDes
1efc6 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
1efc7 30 3b 20 20 20 2f 2a 20 74 61 62 31 20 61 6e 64  0;   /* tab1 and
1efc8 20 74 61 62 32 20 6d 61 79 20 6e 6f 74 20 62 65   tab2 may not be
1efc9 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 20   the same table 
1efca 2a 2f 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  */.  }.#ifndef S
1efcb 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1efcc 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 70 53  ALTABLE.  if( pS
1efcd 72 63 2d 3e 69 73 56 69 72 74 75 61 6c 20 29 7b  rc->isVirtual ){
1efce 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
1efcf 20 2f 2a 20 74 61 62 32 20 6d 75 73 74 20 6e 6f   /* tab2 must no
1efd0 74 20 62 65 20 61 20 76 69 72 74 75 61 6c 20 74  t be a virtual t
1efd1 61 62 6c 65 20 2a 2f 0a 20 20 7d 0a 23 65 6e 64  able */.  }.#end
1efd2 69 66 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 70  if.  if( pSrc->p
1efd3 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 72 65  Select ){.    re
1efd4 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62  turn 0;   /* tab
1efd5 32 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 20 76  2 may not be a v
1efd6 69 65 77 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28  iew */.  }.  if(
1efd7 20 70 44 65 73 74 2d 3e 6e 43 6f 6c 21 3d 70 53   pDest->nCol!=pS
1efd8 72 63 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20  rc->nCol ){.    
1efd9 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 4e  return 0;   /* N
1efda 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
1efdb 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61 6d   must be the sam
1efdc 65 20 69 6e 20 74 61 62 31 20 61 6e 64 20 74 61  e in tab1 and ta
1efdd 62 32 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  b2 */.  }.  if( 
1efde 70 44 65 73 74 2d 3e 69 50 4b 65 79 21 3d 70 53  pDest->iPKey!=pS
1efdf 72 63 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20  rc->iPKey ){.   
1efe0 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
1efe1 42 6f 74 68 20 74 61 62 6c 65 73 20 6d 75 73 74  Both tables must
1efe2 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 49   have the same I
1efe3 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
1efe4 45 59 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28  EY */.  }.  for(
1efe5 69 3d 30 3b 20 69 3c 70 44 65 73 74 2d 3e 6e 43  i=0; i<pDest->nC
1efe6 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  ol; i++){.    if
1efe7 28 20 70 44 65 73 74 2d 3e 61 43 6f 6c 5b 69 5d  ( pDest->aCol[i]
1efe8 2e 61 66 66 69 6e 69 74 79 21 3d 70 53 72 63 2d  .affinity!=pSrc-
1efe9 3e 61 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74  >aCol[i].affinit
1efea 79 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  y ){.      retur
1efeb 6e 20 30 3b 20 20 20 20 2f 2a 20 41 66 66 69 6e  n 0;    /* Affin
1efec 69 74 79 20 6d 75 73 74 20 62 65 20 74 68 65 20  ity must be the 
1efed 73 61 6d 65 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75  same on all colu
1efee 6d 6e 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20  mns */.    }.   
1efef 20 69 66 28 20 21 78 66 65 72 43 6f 6d 70 61 74   if( !xferCompat
1eff0 69 62 6c 65 43 6f 6c 6c 61 74 69 6f 6e 28 70 44  ibleCollation(pD
1eff1 65 73 74 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f  est->aCol[i].zCo
1eff2 6c 6c 2c 20 70 53 72 63 2d 3e 61 43 6f 6c 5b 69  ll, pSrc->aCol[i
1eff3 5d 2e 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20  ].zColl) ){.    
1eff4 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 2f    return 0;    /
1eff5 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  * Collating sequ
1eff6 65 6e 63 65 20 6d 75 73 74 20 62 65 20 74 68 65  ence must be the
1eff7 20 73 61 6d 65 20 6f 6e 20 61 6c 6c 20 63 6f 6c   same on all col
1eff8 75 6d 6e 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20  umns */.    }.  
1eff9 20 20 69 66 28 20 70 44 65 73 74 2d 3e 61 43 6f    if( pDest->aCo
1effa 6c 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c 20 26 26 20  l[i].notNull && 
1effb 21 70 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e  !pSrc->aCol[i].n
1effc 6f 74 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20  otNull ){.      
1effd 72 65 74 75 72 6e 20 30 3b 20 20 20 20 2f 2a 20  return 0;    /* 
1effe 74 61 62 32 20 6d 75 73 74 20 62 65 20 4e 4f 54  tab2 must be NOT
1efff 20 4e 55 4c 4c 20 69 66 20 74 61 62 31 20 69 73   NULL if tab1 is
1f000 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   */.    }.  }.  
1f001 66 6f 72 28 70 44 65 73 74 49 64 78 3d 70 44 65  for(pDestIdx=pDe
1f002 73 74 2d 3e 70 49 6e 64 65 78 3b 20 70 44 65 73  st->pIndex; pDes
1f003 74 49 64 78 3b 20 70 44 65 73 74 49 64 78 3d 70  tIdx; pDestIdx=p
1f004 44 65 73 74 49 64 78 2d 3e 70 4e 65 78 74 29 7b  DestIdx->pNext){
1f005 0a 20 20 20 20 69 66 28 20 70 44 65 73 74 49 64  .    if( pDestId
1f006 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e  x->onError!=OE_N
1f007 6f 6e 65 20 29 7b 0a 20 20 20 20 20 20 64 65 73  one ){.      des
1f008 74 48 61 73 55 6e 69 71 75 65 49 64 78 20 3d 20  tHasUniqueIdx = 
1f009 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  1;.    }.    for
1f00a 28 70 53 72 63 49 64 78 3d 70 53 72 63 2d 3e 70  (pSrcIdx=pSrc->p
1f00b 49 6e 64 65 78 3b 20 70 53 72 63 49 64 78 3b 20  Index; pSrcIdx; 
1f00c 70 53 72 63 49 64 78 3d 70 53 72 63 49 64 78 2d  pSrcIdx=pSrcIdx-
1f00d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69  >pNext){.      i
1f00e 66 28 20 78 66 65 72 43 6f 6d 70 61 74 69 62 6c  f( xferCompatibl
1f00f 65 49 6e 64 65 78 28 70 44 65 73 74 49 64 78 2c  eIndex(pDestIdx,
1f010 20 70 53 72 63 49 64 78 29 20 29 20 62 72 65 61   pSrcIdx) ) brea
1f011 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  k;.    }.    if(
1f012 20 70 53 72 63 49 64 78 3d 3d 30 20 29 7b 0a 20   pSrcIdx==0 ){. 
1f013 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20       return 0;  
1f014 20 20 2f 2a 20 70 44 65 73 74 49 64 78 20 68 61    /* pDestIdx ha
1f015 73 20 6e 6f 20 63 6f 72 72 65 73 70 6f 6e 64 69  s no correspondi
1f016 6e 67 20 69 6e 64 65 78 20 69 6e 20 70 53 72 63  ng index in pSrc
1f017 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69   */.    }.  }.#i
1f018 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1f019 54 5f 43 48 45 43 4b 0a 20 20 69 66 28 20 70 44  T_CHECK.  if( pD
1f01a 65 73 74 2d 3e 70 43 68 65 63 6b 20 26 26 20 21  est->pCheck && !
1f01b 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61  sqlite3ExprCompa
1f01c 72 65 28 70 53 72 63 2d 3e 70 43 68 65 63 6b 2c  re(pSrc->pCheck,
1f01d 20 70 44 65 73 74 2d 3e 70 43 68 65 63 6b 29 20   pDest->pCheck) 
1f01e 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
1f01f 20 20 20 2f 2a 20 54 61 62 6c 65 73 20 68 61 76     /* Tables hav
1f020 65 20 64 69 66 66 65 72 65 6e 74 20 43 48 45 43  e different CHEC
1f021 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20  K constraints.  
1f022 54 69 63 6b 65 74 20 23 32 32 35 32 20 2a 2f 0a  Ticket #2252 */.
1f023 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
1f024 20 49 66 20 77 65 20 67 65 74 20 74 68 69 73 20   If we get this 
1f025 66 61 72 2c 20 69 74 20 6d 65 61 6e 73 20 65 69  far, it means ei
1f026 74 68 65 72 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ther:.  **.  ** 
1f027 20 20 20 2a 20 20 20 57 65 20 63 61 6e 20 61 6c     *   We can al
1f028 77 61 79 73 20 64 6f 20 74 68 65 20 74 72 61 6e  ways do the tran
1f029 73 66 65 72 20 69 66 20 74 68 65 20 74 61 62 6c  sfer if the tabl
1f02a 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e 0a 20 20  e contains an.  
1f02b 2a 2a 20 20 20 20 20 20 20 20 61 6e 20 69 6e 74  **        an int
1f02c 65 67 65 72 20 70 72 69 6d 61 72 79 20 6b 65 79  eger primary key
1f02d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 2a 20  .  **.  **    * 
1f02e 20 20 57 65 20 63 61 6e 20 63 6f 6e 64 69 74 69    We can conditi
1f02f 6f 6e 61 6c 6c 79 20 64 6f 20 74 68 65 20 74 72  onally do the tr
1f030 61 6e 73 66 65 72 20 69 66 20 74 68 65 20 64 65  ansfer if the de
1f031 73 74 69 6e 61 74 69 6f 6e 0a 20 20 2a 2a 20 20  stination.  **  
1f032 20 20 20 20 20 20 74 61 62 6c 65 20 69 73 20 65        table is e
1f033 6d 70 74 79 2e 0a 20 20 2a 2f 0a 23 69 66 64 65  mpty..  */.#ifde
1f034 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
1f035 73 71 6c 69 74 65 33 5f 78 66 65 72 6f 70 74 5f  sqlite3_xferopt_
1f036 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a  count++;.#endif.
1f037 20 20 69 44 62 53 72 63 20 3d 20 73 71 6c 69 74    iDbSrc = sqlit
1f038 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
1f039 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 72 63  pParse->db, pSrc
1f03a 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 76 20  ->pSchema);.  v 
1f03b 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
1f03c 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69  (pParse);.  sqli
1f03d 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68  te3CodeVerifySch
1f03e 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62 53  ema(pParse, iDbS
1f03f 72 63 29 3b 0a 20 20 69 53 72 63 20 3d 20 70 50  rc);.  iSrc = pP
1f040 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
1f041 69 44 65 73 74 20 3d 20 70 50 61 72 73 65 2d 3e  iDest = pParse->
1f042 6e 54 61 62 2b 2b 3b 0a 20 20 72 65 67 41 75 74  nTab++;.  regAut
1f043 6f 69 6e 63 20 3d 20 61 75 74 6f 49 6e 63 42 65  oinc = autoIncBe
1f044 67 69 6e 28 70 50 61 72 73 65 2c 20 69 44 62 44  gin(pParse, iDbD
1f045 65 73 74 2c 20 70 44 65 73 74 29 3b 0a 20 20 73  est, pDest);.  s
1f046 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28  qlite3OpenTable(
1f047 70 50 61 72 73 65 2c 20 69 44 65 73 74 2c 20 69  pParse, iDest, i
1f048 44 62 44 65 73 74 2c 20 70 44 65 73 74 2c 20 4f  DbDest, pDest, O
1f049 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20 20  P_OpenWrite);.  
1f04a 69 66 28 20 28 70 44 65 73 74 2d 3e 69 50 4b 65  if( (pDest->iPKe
1f04b 79 3c 30 20 26 26 20 70 44 65 73 74 2d 3e 70 49  y<0 && pDest->pI
1f04c 6e 64 65 78 21 3d 30 29 20 7c 7c 20 64 65 73 74  ndex!=0) || dest
1f04d 48 61 73 55 6e 69 71 75 65 49 64 78 20 29 7b 0a  HasUniqueIdx ){.
1f04e 20 20 20 20 2f 2a 20 49 66 20 74 61 62 6c 65 73      /* If tables
1f04f 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 61 6e 20   do not have an 
1f050 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
1f051 4b 45 59 20 61 6e 64 20 74 68 65 72 65 0a 20 20  KEY and there.  
1f052 20 20 2a 2a 20 61 72 65 20 69 6e 64 69 63 65 73    ** are indices
1f053 20 74 6f 20 62 65 20 63 6f 70 69 65 64 20 61 6e   to be copied an
1f054 64 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  d the destinatio
1f055 6e 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c 0a  n is not empty,.
1f056 20 20 20 20 2a 2a 20 77 65 20 68 61 76 65 20 74      ** we have t
1f057 6f 20 64 69 73 61 6c 6c 6f 77 20 74 68 65 20 74  o disallow the t
1f058 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a 61  ransfer optimiza
1f059 74 69 6f 6e 20 62 65 63 61 75 73 65 20 74 68 65  tion because the
1f05a 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 6f 77 69  .    ** the rowi
1f05b 64 73 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20  ds might change 
1f05c 77 68 69 63 68 20 77 69 6c 6c 20 6d 65 73 73 20  which will mess 
1f05d 75 70 20 69 6e 64 65 78 69 6e 67 2e 0a 20 20 20  up indexing..   
1f05e 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f 72 20 69 66   **.    ** Or if
1f05f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
1f060 20 68 61 73 20 61 20 55 4e 49 51 55 45 20 69 6e   has a UNIQUE in
1f061 64 65 78 20 61 6e 64 20 69 73 20 6e 6f 74 20 65  dex and is not e
1f062 6d 70 74 79 2c 0a 20 20 20 20 2a 2a 20 77 65 20  mpty,.    ** we 
1f063 61 6c 73 6f 20 64 69 73 61 6c 6c 6f 77 20 74 68  also disallow th
1f064 65 20 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d  e transfer optim
1f065 69 7a 61 74 69 6f 6e 20 62 65 63 61 75 73 65 20  ization because 
1f066 77 65 20 63 61 6e 6e 6f 74 0a 20 20 20 20 2a 2a  we cannot.    **
1f067 20 69 6e 73 75 72 65 20 74 68 61 74 20 61 6c 6c   insure that all
1f068 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20   entries in the 
1f069 75 6e 69 6f 6e 20 6f 66 20 44 45 53 54 20 61 6e  union of DEST an
1f06a 64 20 53 52 43 20 77 69 6c 6c 20 62 65 0a 20 20  d SRC will be.  
1f06b 20 20 2a 2a 20 75 6e 69 71 75 65 2e 0a 20 20 20    ** unique..   
1f06c 20 2a 2f 0a 20 20 20 20 61 64 64 72 31 20 3d 20   */.    addr1 = 
1f06d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1f06e 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20  2(v, OP_Rewind, 
1f06f 69 44 65 73 74 2c 20 30 29 3b 0a 20 20 20 20 65  iDest, 0);.    e
1f070 6d 70 74 79 44 65 73 74 54 65 73 74 20 3d 20 73  mptyDestTest = s
1f071 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1f072 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
1f073 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
1f074 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
1f075 64 64 72 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ddr1);.  }else{.
1f076 20 20 20 20 65 6d 70 74 79 44 65 73 74 54 65 73      emptyDestTes
1f077 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c  t = 0;.  }.  sql
1f078 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50  ite3OpenTable(pP
1f079 61 72 73 65 2c 20 69 53 72 63 2c 20 69 44 62 53  arse, iSrc, iDbS
1f07a 72 63 2c 20 70 53 72 63 2c 20 4f 50 5f 4f 70 65  rc, pSrc, OP_Ope
1f07b 6e 52 65 61 64 29 3b 0a 20 20 65 6d 70 74 79 53  nRead);.  emptyS
1f07c 72 63 54 65 73 74 20 3d 20 73 71 6c 69 74 65 33  rcTest = sqlite3
1f07d 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1f07e 5f 52 65 77 69 6e 64 2c 20 69 53 72 63 2c 20 30  _Rewind, iSrc, 0
1f07f 29 3b 0a 20 20 72 65 67 44 61 74 61 20 3d 20 73  );.  regData = s
1f080 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
1f081 28 70 50 61 72 73 65 29 3b 0a 20 20 72 65 67 52  (pParse);.  regR
1f082 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 47 65  owid = sqlite3Ge
1f083 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
1f084 3b 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 69  ;.  if( pDest->i
1f085 50 4b 65 79 3e 3d 30 20 29 7b 0a 20 20 20 20 61  PKey>=0 ){.    a
1f086 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64  ddr1 = sqlite3Vd
1f087 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
1f088 6f 77 69 64 2c 20 69 53 72 63 2c 20 72 65 67 52  owid, iSrc, regR
1f089 6f 77 69 64 29 3b 0a 20 20 20 20 61 64 64 72 32  owid);.    addr2
1f08a 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
1f08b 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78  dOp3(v, OP_NotEx
1f08c 69 73 74 73 2c 20 69 44 65 73 74 2c 20 30 2c 20  ists, iDest, 0, 
1f08d 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73  regRowid);.    s
1f08e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
1f08f 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c  (v, OP_Halt, SQL
1f090 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20  ITE_CONSTRAINT, 
1f091 6f 6e 45 72 72 6f 72 2c 20 30 2c 0a 20 20 20 20  onError, 0,.    
1f092 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f093 20 20 22 50 52 49 4d 41 52 59 20 4b 45 59 20 6d    "PRIMARY KEY m
1f094 75 73 74 20 62 65 20 75 6e 69 71 75 65 22 2c 20  ust be unique", 
1f095 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P4_STATIC);.    
1f096 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
1f097 65 72 65 28 76 2c 20 61 64 64 72 32 29 3b 0a 20  ere(v, addr2);. 
1f098 20 20 20 61 75 74 6f 49 6e 63 53 74 65 70 28 70     autoIncStep(p
1f099 50 61 72 73 65 2c 20 72 65 67 41 75 74 6f 69 6e  Parse, regAutoin
1f09a 63 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  c, regRowid);.  
1f09b 7d 65 6c 73 65 20 69 66 28 20 70 44 65 73 74 2d  }else if( pDest-
1f09c 3e 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20  >pIndex==0 ){.  
1f09d 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65    addr1 = sqlite
1f09e 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1f09f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69 44 65 73  P_NewRowid, iDes
1f0a0 74 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  t, regRowid);.  
1f0a1 7d 65 6c 73 65 7b 0a 20 20 20 20 61 64 64 72 31  }else{.    addr1
1f0a2 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
1f0a3 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64  dOp2(v, OP_Rowid
1f0a4 2c 20 69 53 72 63 2c 20 72 65 67 52 6f 77 69 64  , iSrc, regRowid
1f0a5 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
1f0a6 44 65 73 74 2d 3e 61 75 74 6f 49 6e 63 3d 3d 30  Dest->autoInc==0
1f0a7 20 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   );.  }.  sqlite
1f0a8 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1f0a9 50 5f 52 6f 77 44 61 74 61 2c 20 69 53 72 63 2c  P_RowData, iSrc,
1f0aa 20 72 65 67 44 61 74 61 29 3b 0a 20 20 73 71 6c   regData);.  sql
1f0ab 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
1f0ac 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69 44 65  , OP_Insert, iDe
1f0ad 73 74 2c 20 72 65 67 44 61 74 61 2c 20 72 65 67  st, regData, reg
1f0ae 52 6f 77 69 64 29 3b 0a 20 20 73 71 6c 69 74 65  Rowid);.  sqlite
1f0af 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
1f0b0 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 7c   OPFLAG_NCHANGE|
1f0b1 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44  OPFLAG_LASTROWID
1f0b2 7c 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b  |OPFLAG_APPEND);
1f0b3 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
1f0b4 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 70 44  angeP4(v, -1, pD
1f0b5 65 73 74 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a  est->zName, 0);.
1f0b6 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1f0b7 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20  Op2(v, OP_Next, 
1f0b8 69 53 72 63 2c 20 61 64 64 72 31 29 3b 0a 20 20  iSrc, addr1);.  
1f0b9 61 75 74 6f 49 6e 63 45 6e 64 28 70 50 61 72 73  autoIncEnd(pPars
1f0ba 65 2c 20 69 44 62 44 65 73 74 2c 20 70 44 65 73  e, iDbDest, pDes
1f0bb 74 2c 20 72 65 67 41 75 74 6f 69 6e 63 29 3b 0a  t, regAutoinc);.
1f0bc 20 20 66 6f 72 28 70 44 65 73 74 49 64 78 3d 70    for(pDestIdx=p
1f0bd 44 65 73 74 2d 3e 70 49 6e 64 65 78 3b 20 70 44  Dest->pIndex; pD
1f0be 65 73 74 49 64 78 3b 20 70 44 65 73 74 49 64 78  estIdx; pDestIdx
1f0bf 3d 70 44 65 73 74 49 64 78 2d 3e 70 4e 65 78 74  =pDestIdx->pNext
1f0c0 29 7b 0a 20 20 20 20 66 6f 72 28 70 53 72 63 49  ){.    for(pSrcI
1f0c1 64 78 3d 70 53 72 63 2d 3e 70 49 6e 64 65 78 3b  dx=pSrc->pIndex;
1f0c2 20 70 53 72 63 49 64 78 3b 20 70 53 72 63 49 64   pSrcIdx; pSrcId
1f0c3 78 3d 70 53 72 63 49 64 78 2d 3e 70 4e 65 78 74  x=pSrcIdx->pNext
1f0c4 29 7b 0a 20 20 20 20 20 20 69 66 28 20 78 66 65  ){.      if( xfe
1f0c5 72 43 6f 6d 70 61 74 69 62 6c 65 49 6e 64 65 78  rCompatibleIndex
1f0c6 28 70 44 65 73 74 49 64 78 2c 20 70 53 72 63 49  (pDestIdx, pSrcI
1f0c7 64 78 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  dx) ) break;.   
1f0c8 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
1f0c9 53 72 63 49 64 78 20 29 3b 0a 20 20 20 20 73 71  SrcIdx );.    sq
1f0ca 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1f0cb 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 53 72  v, OP_Close, iSr
1f0cc 63 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  c, 0);.    sqlit
1f0cd 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1f0ce 4f 50 5f 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c  OP_Close, iDest,
1f0cf 20 30 29 3b 0a 20 20 20 20 70 4b 65 79 20 3d 20   0);.    pKey = 
1f0d0 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69  sqlite3IndexKeyi
1f0d1 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 53 72 63  nfo(pParse, pSrc
1f0d2 49 64 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Idx);.    sqlite
1f0d3 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
1f0d4 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 53 72 63  P_OpenRead, iSrc
1f0d5 2c 20 70 53 72 63 49 64 78 2d 3e 74 6e 75 6d 2c  , pSrcIdx->tnum,
1f0d6 20 69 44 62 53 72 63 2c 0a 20 20 20 20 20 20 20   iDbSrc,.       
1f0d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
1f0d8 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b  char*)pKey, P4_K
1f0d9 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b  EYINFO_HANDOFF);
1f0da 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  .    VdbeComment
1f0db 28 28 76 2c 20 22 25 73 22 2c 20 70 53 72 63 49  ((v, "%s", pSrcI
1f0dc 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20  dx->zName));.   
1f0dd 20 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49   pKey = sqlite3I
1f0de 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72  ndexKeyinfo(pPar
1f0df 73 65 2c 20 70 44 65 73 74 49 64 78 29 3b 0a 20  se, pDestIdx);. 
1f0e0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1f0e1 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 57  dOp4(v, OP_OpenW
1f0e2 72 69 74 65 2c 20 69 44 65 73 74 2c 20 70 44 65  rite, iDest, pDe
1f0e3 73 74 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62  stIdx->tnum, iDb
1f0e4 44 65 73 74 2c 0a 20 20 20 20 20 20 20 20 20 20  Dest,.          
1f0e5 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61              (cha
1f0e6 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49  r*)pKey, P4_KEYI
1f0e7 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20  NFO_HANDOFF);.  
1f0e8 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
1f0e9 2c 20 22 25 73 22 2c 20 70 44 65 73 74 49 64 78  , "%s", pDestIdx
1f0ea 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 61  ->zName));.    a
1f0eb 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64  ddr1 = sqlite3Vd
1f0ec 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
1f0ed 65 77 69 6e 64 2c 20 69 53 72 63 2c 20 30 29 3b  ewind, iSrc, 0);
1f0ee 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1f0ef 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77  AddOp2(v, OP_Row
1f0f0 4b 65 79 2c 20 69 53 72 63 2c 20 72 65 67 44 61  Key, iSrc, regDa
1f0f1 74 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ta);.    sqlite3
1f0f2 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
1f0f3 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 44 65 73  _IdxInsert, iDes
1f0f4 74 2c 20 72 65 67 44 61 74 61 2c 20 31 29 3b 0a  t, regData, 1);.
1f0f5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1f0f6 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74  ddOp2(v, OP_Next
1f0f7 2c 20 69 53 72 63 2c 20 61 64 64 72 31 2b 31 29  , iSrc, addr1+1)
1f0f8 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1f0f9 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
1f0fa 72 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  r1);.  }.  sqlit
1f0fb 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
1f0fc 2c 20 65 6d 70 74 79 53 72 63 54 65 73 74 29 3b  , emptySrcTest);
1f0fd 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
1f0fe 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
1f0ff 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 73 71   regRowid);.  sq
1f100 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
1f101 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 44  Reg(pParse, regD
1f102 61 74 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  ata);.  sqlite3V
1f103 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1f104 43 6c 6f 73 65 2c 20 69 53 72 63 2c 20 30 29 3b  Close, iSrc, 0);
1f105 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
1f106 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp2(v, OP_Close
1f107 2c 20 69 44 65 73 74 2c 20 30 29 3b 0a 20 20 69  , iDest, 0);.  i
1f108 66 28 20 65 6d 70 74 79 44 65 73 74 54 65 73 74  f( emptyDestTest
1f109 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1f10a 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1f10b 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c  Halt, SQLITE_OK,
1f10c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
1f10d 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
1f10e 65 6d 70 74 79 44 65 73 74 54 65 73 74 29 3b 0a  emptyDestTest);.
1f10f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1f110 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp2(v, OP_Clos
1f111 65 2c 20 69 44 65 73 74 2c 20 30 29 3b 0a 20 20  e, iDest, 0);.  
1f112 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65    return 0;.  }e
1f113 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
1f114 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20  1;.  }.}.#endif 
1f115 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58  /* SQLITE_OMIT_X
1f116 46 45 52 5f 4f 50 54 20 2a 2f 0a 0a 2f 2a 20 4d  FER_OPT */../* M
1f117 61 6b 65 20 73 75 72 65 20 22 69 73 56 69 65 77  ake sure "isView
1f118 22 20 67 65 74 73 20 75 6e 64 65 66 69 6e 65 64  " gets undefined
1f119 20 69 6e 20 63 61 73 65 20 74 68 69 73 20 66 69   in case this fi
1f11a 6c 65 20 62 65 63 6f 6d 65 73 20 70 61 72 74 20  le becomes part 
1f11b 6f 66 0a 2a 2a 20 74 68 65 20 61 6d 61 6c 67 61  of.** the amalga
1f11c 6d 61 74 69 6f 6e 20 2d 20 73 6f 20 74 68 61 74  mation - so that
1f11d 20 73 75 62 73 65 71 75 65 6e 74 20 66 69 6c 65   subsequent file
1f11e 73 20 64 6f 20 6e 6f 74 20 73 65 65 20 69 73 56  s do not see isV
1f11f 69 65 77 20 61 73 20 61 0a 2a 2a 20 6d 61 63 72  iew as a.** macr
1f120 6f 2e 20 2a 2f 0a 23 75 6e 64 65 66 20 69 73 56  o. */.#undef isV
1f121 69 65 77 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  iew../**********
1f122 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 69 6e 73 65  **** End of inse
1f123 72 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  rt.c ***********
1f124 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f125 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f126 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
1f127 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
1f128 6c 65 67 61 63 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a  legacy.c *******
1f129 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f12a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f12b 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
1f12c 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a  September 15.**.
1f12d 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
1f12e 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
1f12f 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
1f130 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
1f131 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
1f132 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
1f133 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
1f134 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
1f135 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
1f136 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1f137 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
1f138 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
1f139 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
1f13a 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1f13b 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
1f13c 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
1f13d 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
1f13e 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
1f13f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f141 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f142 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 4d  ***********.** M
1f143 61 69 6e 20 66 69 6c 65 20 66 6f 72 20 74 68 65  ain file for the
1f144 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e   SQLite library.
1f145 20 20 54 68 65 20 72 6f 75 74 69 6e 65 73 20 69    The routines i
1f146 6e 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 69  n this file.** i
1f147 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 70 72 6f  mplement the pro
1f148 67 72 61 6d 6d 65 72 20 69 6e 74 65 72 66 61 63  grammer interfac
1f149 65 20 74 6f 20 74 68 65 20 6c 69 62 72 61 72 79  e to the library
1f14a 2e 20 20 52 6f 75 74 69 6e 65 73 20 69 6e 0a 2a  .  Routines in.*
1f14b 2a 20 6f 74 68 65 72 20 66 69 6c 65 73 20 61 72  * other files ar
1f14c 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75  e for internal u
1f14d 73 65 20 62 79 20 53 51 4c 69 74 65 20 61 6e 64  se by SQLite and
1f14e 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a   should not be.*
1f14f 2a 20 61 63 63 65 73 73 65 64 20 62 79 20 75 73  * accessed by us
1f150 65 72 73 20 6f 66 20 74 68 65 20 6c 69 62 72 61  ers of the libra
1f151 72 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6c  ry..**.** $Id: l
1f152 65 67 61 63 79 2e 63 2c 76 20 31 2e 32 34 20 32  egacy.c,v 1.24 2
1f153 30 30 38 2f 30 33 2f 32 31 20 31 38 3a 30 31 3a  008/03/21 18:01:
1f154 31 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  14 drh Exp $.*/.
1f155 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20  ../*.** Execute 
1f156 53 51 4c 20 63 6f 64 65 2e 20 20 52 65 74 75 72  SQL code.  Retur
1f157 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c  n one of the SQL
1f158 49 54 45 5f 20 73 75 63 63 65 73 73 2f 66 61 69  ITE_ success/fai
1f159 6c 75 72 65 0a 2a 2a 20 63 6f 64 65 73 2e 20 20  lure.** codes.  
1f15a 41 6c 73 6f 20 77 72 69 74 65 20 61 6e 20 65 72  Also write an er
1f15b 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 74 6f  ror message into
1f15c 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
1f15d 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28   from.** malloc(
1f15e 29 20 61 6e 64 20 6d 61 6b 65 20 2a 70 7a 45 72  ) and make *pzEr
1f15f 72 4d 73 67 20 70 6f 69 6e 74 20 74 6f 20 74 68  rMsg point to th
1f160 61 74 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a  at message..**.*
1f161 2a 20 49 66 20 74 68 65 20 53 51 4c 20 69 73 20  * If the SQL is 
1f162 61 20 71 75 65 72 79 2c 20 74 68 65 6e 20 66 6f  a query, then fo
1f163 72 20 65 61 63 68 20 72 6f 77 20 69 6e 20 74 68  r each row in th
1f164 65 20 71 75 65 72 79 20 72 65 73 75 6c 74 0a 2a  e query result.*
1f165 2a 20 74 68 65 20 78 43 61 6c 6c 62 61 63 6b 28  * the xCallback(
1f166 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  ) function is ca
1f167 6c 6c 65 64 2e 20 20 70 41 72 67 20 62 65 63 6f  lled.  pArg beco
1f168 6d 65 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a  mes the first.**
1f169 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 61   argument to xCa
1f16a 6c 6c 62 61 63 6b 28 29 2e 20 20 49 66 20 78 43  llback().  If xC
1f16b 61 6c 6c 62 61 63 6b 3d 4e 55 4c 4c 20 74 68 65  allback=NULL the
1f16c 6e 20 6e 6f 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a  n no callback.**
1f16d 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20 65 76 65   is invoked, eve
1f16e 6e 20 66 6f 72 20 71 75 65 72 69 65 73 2e 0a 2a  n for queries..*
1f16f 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1f170 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 0a 20   sqlite3_exec(. 
1f171 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
1f172 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1f173 54 68 65 20 64 61 74 61 62 61 73 65 20 6f 6e 20  The database on 
1f174 77 68 69 63 68 20 74 68 65 20 53 51 4c 20 65 78  which the SQL ex
1f175 65 63 75 74 65 73 20 2a 2f 0a 20 20 63 6f 6e 73  ecutes */.  cons
1f176 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20  t char *zSql,   
1f177 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53          /* The S
1f178 51 4c 20 74 6f 20 62 65 20 65 78 65 63 75 74 65  QL to be execute
1f179 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 63  d */.  sqlite3_c
1f17a 61 6c 6c 62 61 63 6b 20 78 43 61 6c 6c 62 61 63  allback xCallbac
1f17b 6b 2c 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 69  k, /* Invoke thi
1f17c 73 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69  s callback routi
1f17d 6e 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41  ne */.  void *pA
1f17e 72 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  rg,             
1f17f 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67      /* First arg
1f180 75 6d 65 6e 74 20 74 6f 20 78 43 61 6c 6c 62 61  ument to xCallba
1f181 63 6b 28 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a  ck() */.  char *
1f182 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20 20 20  *pzErrMsg       
1f183 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 65        /* Write e
1f184 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68 65  rror messages he
1f185 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  re */.){.  int r
1f186 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1f187 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65   const char *zLe
1f188 66 74 6f 76 65 72 3b 0a 20 20 73 71 6c 69 74 65  ftover;.  sqlite
1f189 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 20 3d 20  3_stmt *pStmt = 
1f18a 30 3b 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f  0;.  char **azCo
1f18b 6c 73 20 3d 20 30 3b 0a 0a 20 20 69 6e 74 20 6e  ls = 0;..  int n
1f18c 52 65 74 72 79 20 3d 20 30 3b 0a 20 20 69 6e 74  Retry = 0;.  int
1f18d 20 6e 43 61 6c 6c 62 61 63 6b 3b 0a 0a 20 20 69   nCallback;..  i
1f18e 66 28 20 7a 53 71 6c 3d 3d 30 20 29 20 72 65 74  f( zSql==0 ) ret
1f18f 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  urn SQLITE_OK;..
1f190 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1f191 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
1f192 3b 0a 20 20 77 68 69 6c 65 28 20 28 72 63 3d 3d  ;.  while( (rc==
1f193 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28 72 63  SQLITE_OK || (rc
1f194 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20  ==SQLITE_SCHEMA 
1f195 26 26 20 28 2b 2b 6e 52 65 74 72 79 29 3c 32 29  && (++nRetry)<2)
1f196 29 20 26 26 20 7a 53 71 6c 5b 30 5d 20 29 7b 0a  ) && zSql[0] ){.
1f197 20 20 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20      int nCol;.  
1f198 20 20 63 68 61 72 20 2a 2a 61 7a 56 61 6c 73 20    char **azVals 
1f199 3d 20 30 3b 0a 0a 20 20 20 20 70 53 74 6d 74 20  = 0;..    pStmt 
1f19a 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  = 0;.    rc = sq
1f19b 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 64 62  lite3_prepare(db
1f19c 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74  , zSql, -1, &pSt
1f19d 6d 74 2c 20 26 7a 4c 65 66 74 6f 76 65 72 29 3b  mt, &zLeftover);
1f19e 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d  .    assert( rc=
1f19f 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 53  =SQLITE_OK || pS
1f1a0 74 6d 74 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66  tmt==0 );.    if
1f1a1 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1f1a2 29 7b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75  ){.      continu
1f1a3 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  e;.    }.    if(
1f1a4 20 21 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 20   !pStmt ){.     
1f1a5 20 2f 2a 20 74 68 69 73 20 68 61 70 70 65 6e 73   /* this happens
1f1a6 20 66 6f 72 20 61 20 63 6f 6d 6d 65 6e 74 20 6f   for a comment o
1f1a7 72 20 77 68 69 74 65 2d 73 70 61 63 65 20 2a 2f  r white-space */
1f1a8 0a 20 20 20 20 20 20 7a 53 71 6c 20 3d 20 7a 4c  .      zSql = zL
1f1a9 65 66 74 6f 76 65 72 3b 0a 20 20 20 20 20 20 63  eftover;.      c
1f1aa 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a  ontinue;.    }..
1f1ab 20 20 20 20 6e 43 61 6c 6c 62 61 63 6b 20 3d 20      nCallback = 
1f1ac 30 3b 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 73 71  0;.    nCol = sq
1f1ad 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75  lite3_column_cou
1f1ae 6e 74 28 70 53 74 6d 74 29 3b 0a 0a 20 20 20 20  nt(pStmt);..    
1f1af 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20  while( 1 ){.    
1f1b0 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 72    int i;.      r
1f1b1 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
1f1b2 28 70 53 74 6d 74 29 3b 0a 0a 20 20 20 20 20 20  (pStmt);..      
1f1b3 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 63 61  /* Invoke the ca
1f1b4 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
1f1b5 69 66 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20  if required */. 
1f1b6 20 20 20 20 20 69 66 28 20 78 43 61 6c 6c 62 61       if( xCallba
1f1b7 63 6b 20 26 26 20 28 53 51 4c 49 54 45 5f 52 4f  ck && (SQLITE_RO
1f1b8 57 3d 3d 72 63 20 7c 7c 20 0a 20 20 20 20 20 20  W==rc || .      
1f1b9 20 20 20 20 28 53 51 4c 49 54 45 5f 44 4f 4e 45      (SQLITE_DONE
1f1ba 3d 3d 72 63 20 26 26 20 21 6e 43 61 6c 6c 62 61  ==rc && !nCallba
1f1bb 63 6b 20 26 26 20 64 62 2d 3e 66 6c 61 67 73 26  ck && db->flags&
1f1bc 53 51 4c 49 54 45 5f 4e 75 6c 6c 43 61 6c 6c 62  SQLITE_NullCallb
1f1bd 61 63 6b 29 29 20 29 7b 0a 20 20 20 20 20 20 20  ack)) ){.       
1f1be 20 69 66 28 20 30 3d 3d 6e 43 61 6c 6c 62 61 63   if( 0==nCallbac
1f1bf 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  k ){.          i
1f1c0 66 28 20 61 7a 43 6f 6c 73 3d 3d 30 20 29 7b 0a  f( azCols==0 ){.
1f1c1 20 20 20 20 20 20 20 20 20 20 20 20 61 7a 43 6f              azCo
1f1c2 6c 73 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ls = sqlite3DbMa
1f1c3 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 32 2a 6e  llocZero(db, 2*n
1f1c4 43 6f 6c 2a 73 69 7a 65 6f 66 28 63 6f 6e 73 74  Col*sizeof(const
1f1c5 20 63 68 61 72 2a 29 20 2b 20 31 29 3b 0a 20 20   char*) + 1);.  
1f1c6 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61 7a            if( az
1f1c7 43 6f 6c 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Cols==0 ){.     
1f1c8 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78           goto ex
1f1c9 65 63 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20  ec_out;.        
1f1ca 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1f1cb 7d 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28  }.          for(
1f1cc 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b  i=0; i<nCol; i++
1f1cd 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  ){.            a
1f1ce 7a 43 6f 6c 73 5b 69 5d 20 3d 20 28 63 68 61 72  zCols[i] = (char
1f1cf 20 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   *)sqlite3_colum
1f1d0 6e 5f 6e 61 6d 65 28 70 53 74 6d 74 2c 20 69 29  n_name(pStmt, i)
1f1d1 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  ;.            if
1f1d2 28 20 21 61 7a 43 6f 6c 73 5b 69 5d 20 29 7b 0a  ( !azCols[i] ){.
1f1d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62                db
1f1d4 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
1f1d5 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   1;.            
1f1d6 20 20 67 6f 74 6f 20 65 78 65 63 5f 6f 75 74 3b    goto exec_out;
1f1d7 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
1f1d8 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1f1d9 20 20 20 20 20 6e 43 61 6c 6c 62 61 63 6b 2b 2b       nCallback++
1f1da 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1f1db 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1f1dc 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20  TE_ROW ){.      
1f1dd 20 20 20 20 61 7a 56 61 6c 73 20 3d 20 26 61 7a      azVals = &az
1f1de 43 6f 6c 73 5b 6e 43 6f 6c 5d 3b 0a 20 20 20 20  Cols[nCol];.    
1f1df 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
1f1e0 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nCol; i++){.   
1f1e1 20 20 20 20 20 20 20 20 20 61 7a 56 61 6c 73 5b           azVals[
1f1e2 69 5d 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c  i] = (char *)sql
1f1e3 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
1f1e4 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20 20 20  (pStmt, i);.    
1f1e5 20 20 20 20 20 20 20 20 69 66 28 20 21 61 7a 56          if( !azV
1f1e6 61 6c 73 5b 69 5d 20 26 26 20 73 71 6c 69 74 65  als[i] && sqlite
1f1e7 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 70 53  3_column_type(pS
1f1e8 74 6d 74 2c 20 69 29 21 3d 53 51 4c 49 54 45 5f  tmt, i)!=SQLITE_
1f1e9 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20  NULL ){.        
1f1ea 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63        db->malloc
1f1eb 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
1f1ec 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65            goto e
1f1ed 78 65 63 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20  xec_out;.       
1f1ee 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
1f1ef 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
1f1f0 20 20 20 20 20 69 66 28 20 78 43 61 6c 6c 62 61       if( xCallba
1f1f1 63 6b 28 70 41 72 67 2c 20 6e 43 6f 6c 2c 20 61  ck(pArg, nCol, a
1f1f2 7a 56 61 6c 73 2c 20 61 7a 43 6f 6c 73 29 20 29  zVals, azCols) )
1f1f3 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
1f1f4 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20   SQLITE_ABORT;. 
1f1f5 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78           goto ex
1f1f6 65 63 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20  ec_out;.        
1f1f7 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  }.      }..     
1f1f8 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1f1f9 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ROW ){.        r
1f1fa 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  c = sqlite3_fina
1f1fb 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 20 20 20  lize(pStmt);.   
1f1fc 20 20 20 20 20 70 53 74 6d 74 20 3d 20 30 3b 0a       pStmt = 0;.
1f1fd 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
1f1fe 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 29 7b  SQLITE_SCHEMA ){
1f1ff 0a 20 20 20 20 20 20 20 20 20 20 6e 52 65 74 72  .          nRetr
1f200 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  y = 0;.         
1f201 20 7a 53 71 6c 20 3d 20 7a 4c 65 66 74 6f 76 65   zSql = zLeftove
1f202 72 3b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69  r;.          whi
1f203 6c 65 28 20 69 73 73 70 61 63 65 28 28 75 6e 73  le( isspace((uns
1f204 69 67 6e 65 64 20 63 68 61 72 29 7a 53 71 6c 5b  igned char)zSql[
1f205 30 5d 29 20 29 20 7a 53 71 6c 2b 2b 3b 0a 20 20  0]) ) zSql++;.  
1f206 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1f207 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
1f208 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65     }..    sqlite
1f209 33 5f 66 72 65 65 28 61 7a 43 6f 6c 73 29 3b 0a  3_free(azCols);.
1f20a 20 20 20 20 61 7a 43 6f 6c 73 20 3d 20 30 3b 0a      azCols = 0;.
1f20b 20 20 7d 0a 0a 65 78 65 63 5f 6f 75 74 3a 0a 20    }..exec_out:. 
1f20c 20 69 66 28 20 70 53 74 6d 74 20 29 20 73 71 6c   if( pStmt ) sql
1f20d 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53  ite3_finalize(pS
1f20e 74 6d 74 29 3b 0a 20 20 69 66 28 20 61 7a 43 6f  tmt);.  if( azCo
1f20f 6c 73 20 29 20 73 71 6c 69 74 65 33 5f 66 72 65  ls ) sqlite3_fre
1f210 65 28 61 7a 43 6f 6c 73 29 3b 0a 0a 20 20 72 63  e(azCols);..  rc
1f211 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
1f212 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 69 66 28  t(db, rc);.  if(
1f213 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc!=SQLITE_OK &
1f214 26 20 72 63 3d 3d 73 71 6c 69 74 65 33 5f 65 72  & rc==sqlite3_er
1f215 72 63 6f 64 65 28 64 62 29 20 26 26 20 70 7a 45  rcode(db) && pzE
1f216 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 69 6e 74  rrMsg ){.    int
1f217 20 6e 45 72 72 4d 73 67 20 3d 20 31 20 2b 20 73   nErrMsg = 1 + s
1f218 74 72 6c 65 6e 28 73 71 6c 69 74 65 33 5f 65 72  trlen(sqlite3_er
1f219 72 6d 73 67 28 64 62 29 29 3b 0a 20 20 20 20 2a  rmsg(db));.    *
1f21a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74  pzErrMsg = sqlit
1f21b 65 33 5f 6d 61 6c 6c 6f 63 28 6e 45 72 72 4d 73  e3_malloc(nErrMs
1f21c 67 29 3b 0a 20 20 20 20 69 66 28 20 2a 70 7a 45  g);.    if( *pzE
1f21d 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 6d  rrMsg ){.      m
1f21e 65 6d 63 70 79 28 2a 70 7a 45 72 72 4d 73 67 2c  emcpy(*pzErrMsg,
1f21f 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28   sqlite3_errmsg(
1f220 64 62 29 2c 20 6e 45 72 72 4d 73 67 29 3b 0a 20  db), nErrMsg);. 
1f221 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
1f222 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20   pzErrMsg ){.   
1f223 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a   *pzErrMsg = 0;.
1f224 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 28    }..  assert( (
1f225 72 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d  rc&db->errMask)=
1f226 3d 72 63 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  =rc );.  sqlite3
1f227 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
1f228 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  >mutex);.  retur
1f229 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  n rc;.}../******
1f22a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
1f22b 6c 65 67 61 63 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a  legacy.c *******
1f22c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f22d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f22e 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
1f22f 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
1f230 69 6c 65 20 6c 6f 61 64 65 78 74 2e 63 20 2a 2a  ile loadext.c **
1f231 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f232 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f233 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
1f234 30 30 36 20 4a 75 6e 65 20 37 0a 2a 2a 0a 2a 2a  006 June 7.**.**
1f235 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1f236 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1f237 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1f238 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1f239 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
1f23a 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
1f23b 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
1f23c 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
1f23d 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
1f23e 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
1f23f 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
1f240 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
1f241 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
1f242 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1f243 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1f244 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1f245 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1f246 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f247 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f248 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f249 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f24a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
1f24b 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
1f24c 63 6f 64 65 20 75 73 65 64 20 74 6f 20 64 79 6e  code used to dyn
1f24d 61 6d 69 63 61 6c 6c 79 20 6c 6f 61 64 20 65 78  amically load ex
1f24e 74 65 6e 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a  tensions into.**
1f24f 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
1f250 61 72 79 2e 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66  ary..*/..#ifndef
1f251 20 53 51 4c 49 54 45 5f 43 4f 52 45 0a 20 20 23   SQLITE_CORE.  #
1f252 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
1f253 52 45 20 31 20 20 2f 2a 20 44 69 73 61 62 6c 65  RE 1  /* Disable
1f254 20 74 68 65 20 41 50 49 20 72 65 64 65 66 69 6e   the API redefin
1f255 69 74 69 6f 6e 20 69 6e 20 73 71 6c 69 74 65 33  ition in sqlite3
1f256 65 78 74 2e 68 20 2a 2f 0a 23 65 6e 64 69 66 0a  ext.h */.#endif.
1f257 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1f258 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 65 33 65  Include sqlite3e
1f259 78 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  xt.h in the midd
1f25a 6c 65 20 6f 66 20 6c 6f 61 64 65 78 74 2e 63 20  le of loadext.c 
1f25b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1f25c 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1f25d 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74  Begin file sqlit
1f25e 65 33 65 78 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  e3ext.h ********
1f25f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1f261 2f 2a 0a 2a 2a 20 32 30 30 36 20 4a 75 6e 65 20  /*.** 2006 June 
1f262 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  7.**.** The auth
1f263 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
1f264 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
1f265 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
1f266 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
1f267 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
1f268 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
1f269 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
1f26a 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
1f26b 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
1f26c 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
1f26d 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
1f26e 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
1f26f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
1f270 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
1f271 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
1f272 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
1f273 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
1f274 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f275 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f276 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f277 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f278 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20  .** This header 
1f279 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65  file defines the
1f27a 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63   SQLite interfac
1f27b 65 20 66 6f 72 20 75 73 65 20 62 79 0a 2a 2a 20  e for use by.** 
1f27c 73 68 61 72 65 64 20 6c 69 62 72 61 72 69 65 73  shared libraries
1f27d 20 74 68 61 74 20 77 61 6e 74 20 74 6f 20 62 65   that want to be
1f27e 20 69 6d 70 6f 72 74 65 64 20 61 73 20 65 78 74   imported as ext
1f27f 65 6e 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a 20  ensions into.** 
1f280 61 6e 20 53 51 4c 69 74 65 20 69 6e 73 74 61 6e  an SQLite instan
1f281 63 65 2e 20 20 53 68 61 72 65 64 20 6c 69 62 72  ce.  Shared libr
1f282 61 72 69 65 73 20 74 68 61 74 20 69 6e 74 65 6e  aries that inten
1f283 64 20 74 6f 20 62 65 20 6c 6f 61 64 65 64 0a 2a  d to be loaded.*
1f284 2a 20 61 73 20 65 78 74 65 6e 73 69 6f 6e 73 20  * as extensions 
1f285 62 79 20 53 51 4c 69 74 65 20 73 68 6f 75 6c 64  by SQLite should
1f286 20 23 69 6e 63 6c 75 64 65 20 74 68 69 73 20 66   #include this f
1f287 69 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20 0a  ile instead of .
1f288 2a 2a 20 73 71 6c 69 74 65 33 2e 68 2e 0a 2a 2a  ** sqlite3.h..**
1f289 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 73 71  .** @(#) $Id: sq
1f28a 6c 69 74 65 33 65 78 74 2e 68 2c 76 20 31 2e 32  lite3ext.h,v 1.2
1f28b 31 20 32 30 30 38 2f 30 33 2f 31 39 20 32 31 3a  1 2008/03/19 21:
1f28c 34 35 3a 35 31 20 64 72 68 20 45 78 70 20 24 0a  45:51 drh Exp $.
1f28d 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49  */.#ifndef _SQLI
1f28e 54 45 33 45 58 54 5f 48 5f 0a 23 64 65 66 69 6e  TE3EXT_H_.#defin
1f28f 65 20 5f 53 51 4c 49 54 45 33 45 58 54 5f 48 5f  e _SQLITE3EXT_H_
1f290 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
1f291 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75   sqlite3_api_rou
1f292 74 69 6e 65 73 20 73 71 6c 69 74 65 33 5f 61 70  tines sqlite3_ap
1f293 69 5f 72 6f 75 74 69 6e 65 73 3b 0a 0a 2f 2a 0a  i_routines;../*.
1f294 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
1f295 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73   structure holds
1f296 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 61 6c 6c   pointers to all
1f297 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20 41   of the SQLite A
1f298 50 49 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 2e 0a  PI.** routines..
1f299 2a 2a 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a 20 20  **.** WARNING:  
1f29a 49 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61 69 6e  In order to main
1f29b 74 61 69 6e 20 62 61 63 6b 77 61 72 64 73 20 63  tain backwards c
1f29c 6f 6d 70 61 74 69 62 69 6c 69 74 79 2c 20 61 64  ompatibility, ad
1f29d 64 20 6e 65 77 0a 2a 2a 20 69 6e 74 65 72 66 61  d new.** interfa
1f29e 63 65 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  ces to the end o
1f29f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
1f2a0 20 6f 6e 6c 79 2e 20 20 49 66 20 79 6f 75 20 69   only.  If you i
1f2a1 6e 73 65 72 74 20 6e 65 77 0a 2a 2a 20 69 6e 74  nsert new.** int
1f2a2 65 72 66 61 63 65 73 20 69 6e 20 74 68 65 20 6d  erfaces in the m
1f2a3 69 64 64 6c 65 20 6f 66 20 74 68 69 73 20 73 74  iddle of this st
1f2a4 72 75 63 74 75 72 65 2c 20 74 68 65 6e 20 6f 6c  ructure, then ol
1f2a5 64 65 72 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a  der different.**
1f2a6 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c   versions of SQL
1f2a7 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20  ite will not be 
1f2a8 61 62 6c 65 20 74 6f 20 6c 6f 61 64 20 65 61 63  able to load eac
1f2a9 68 20 6f 74 68 65 72 73 27 20 73 68 61 72 65 64  h others' shared
1f2aa 0a 2a 2a 20 6c 69 62 72 61 72 69 65 73 21 0a 2a  .** libraries!.*
1f2ab 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  /.struct sqlite3
1f2ac 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 7b 0a  _api_routines {.
1f2ad 20 20 76 6f 69 64 20 2a 20 28 2a 61 67 67 72 65    void * (*aggre
1f2ae 67 61 74 65 5f 63 6f 6e 74 65 78 74 29 28 73 71  gate_context)(sq
1f2af 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
1f2b0 6e 74 20 6e 42 79 74 65 73 29 3b 0a 20 20 69 6e  nt nBytes);.  in
1f2b1 74 20 20 28 2a 61 67 67 72 65 67 61 74 65 5f 63  t  (*aggregate_c
1f2b2 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 63 6f  ount)(sqlite3_co
1f2b3 6e 74 65 78 74 2a 29 3b 0a 20 20 69 6e 74 20 20  ntext*);.  int  
1f2b4 28 2a 62 69 6e 64 5f 62 6c 6f 62 29 28 73 71 6c  (*bind_blob)(sql
1f2b5 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 63  ite3_stmt*,int,c
1f2b6 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 20 6e  onst void*,int n
1f2b7 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29  ,void(*)(void*))
1f2b8 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f  ;.  int  (*bind_
1f2b9 64 6f 75 62 6c 65 29 28 73 71 6c 69 74 65 33 5f  double)(sqlite3_
1f2ba 73 74 6d 74 2a 2c 69 6e 74 2c 64 6f 75 62 6c 65  stmt*,int,double
1f2bb 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64  );.  int  (*bind
1f2bc 5f 69 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74  _int)(sqlite3_st
1f2bd 6d 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20  mt*,int,int);.  
1f2be 69 6e 74 20 20 28 2a 62 69 6e 64 5f 69 6e 74 36  int  (*bind_int6
1f2bf 34 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  4)(sqlite3_stmt*
1f2c0 2c 69 6e 74 2c 73 71 6c 69 74 65 5f 69 6e 74 36  ,int,sqlite_int6
1f2c1 34 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e  4);.  int  (*bin
1f2c2 64 5f 6e 75 6c 6c 29 28 73 71 6c 69 74 65 33 5f  d_null)(sqlite3_
1f2c3 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e  stmt*,int);.  in
1f2c4 74 20 20 28 2a 62 69 6e 64 5f 70 61 72 61 6d 65  t  (*bind_parame
1f2c5 74 65 72 5f 63 6f 75 6e 74 29 28 73 71 6c 69 74  ter_count)(sqlit
1f2c6 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74  e3_stmt*);.  int
1f2c7 20 20 28 2a 62 69 6e 64 5f 70 61 72 61 6d 65 74    (*bind_paramet
1f2c8 65 72 5f 69 6e 64 65 78 29 28 73 71 6c 69 74 65  er_index)(sqlite
1f2c9 33 5f 73 74 6d 74 2a 2c 63 6f 6e 73 74 20 63 68  3_stmt*,const ch
1f2ca 61 72 2a 7a 4e 61 6d 65 29 3b 0a 20 20 63 6f 6e  ar*zName);.  con
1f2cb 73 74 20 63 68 61 72 20 2a 20 28 2a 62 69 6e 64  st char * (*bind
1f2cc 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 29  _parameter_name)
1f2cd 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
1f2ce 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69  nt);.  int  (*bi
1f2cf 6e 64 5f 74 65 78 74 29 28 73 71 6c 69 74 65 33  nd_text)(sqlite3
1f2d0 5f 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74  _stmt*,int,const
1f2d1 20 63 68 61 72 2a 2c 69 6e 74 20 6e 2c 76 6f 69   char*,int n,voi
1f2d2 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20  d(*)(void*));.  
1f2d3 69 6e 74 20 20 28 2a 62 69 6e 64 5f 74 65 78 74  int  (*bind_text
1f2d4 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  16)(sqlite3_stmt
1f2d5 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
1f2d6 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f  *,int,void(*)(vo
1f2d7 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a  id*));.  int  (*
1f2d8 62 69 6e 64 5f 76 61 6c 75 65 29 28 73 71 6c 69  bind_value)(sqli
1f2d9 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f  te3_stmt*,int,co
1f2da 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  nst sqlite3_valu
1f2db 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 75  e*);.  int  (*bu
1f2dc 73 79 5f 68 61 6e 64 6c 65 72 29 28 73 71 6c 69  sy_handler)(sqli
1f2dd 74 65 33 2a 2c 69 6e 74 28 2a 29 28 76 6f 69 64  te3*,int(*)(void
1f2de 2a 2c 69 6e 74 29 2c 76 6f 69 64 2a 29 3b 0a 20  *,int),void*);. 
1f2df 20 69 6e 74 20 20 28 2a 62 75 73 79 5f 74 69 6d   int  (*busy_tim
1f2e0 65 6f 75 74 29 28 73 71 6c 69 74 65 33 2a 2c 69  eout)(sqlite3*,i
1f2e1 6e 74 20 6d 73 29 3b 0a 20 20 69 6e 74 20 20 28  nt ms);.  int  (
1f2e2 2a 63 68 61 6e 67 65 73 29 28 73 71 6c 69 74 65  *changes)(sqlite
1f2e3 33 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6c  3*);.  int  (*cl
1f2e4 6f 73 65 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a  ose)(sqlite3*);.
1f2e5 20 20 69 6e 74 20 20 28 2a 63 6f 6c 6c 61 74 69    int  (*collati
1f2e6 6f 6e 5f 6e 65 65 64 65 64 29 28 73 71 6c 69 74  on_needed)(sqlit
1f2e7 65 33 2a 2c 76 6f 69 64 2a 2c 76 6f 69 64 28 2a  e3*,void*,void(*
1f2e8 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a  )(void*,sqlite3*
1f2e9 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f  ,int eTextRep,co
1f2ea 6e 73 74 20 63 68 61 72 2a 29 29 3b 0a 20 20 69  nst char*));.  i
1f2eb 6e 74 20 20 28 2a 63 6f 6c 6c 61 74 69 6f 6e 5f  nt  (*collation_
1f2ec 6e 65 65 64 65 64 31 36 29 28 73 71 6c 69 74 65  needed16)(sqlite
1f2ed 33 2a 2c 76 6f 69 64 2a 2c 76 6f 69 64 28 2a 29  3*,void*,void(*)
1f2ee 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c  (void*,sqlite3*,
1f2ef 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e  int eTextRep,con
1f2f0 73 74 20 76 6f 69 64 2a 29 29 3b 0a 20 20 63 6f  st void*));.  co
1f2f1 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c  nst void * (*col
1f2f2 75 6d 6e 5f 62 6c 6f 62 29 28 73 71 6c 69 74 65  umn_blob)(sqlite
1f2f3 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c  3_stmt*,int iCol
1f2f4 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75  );.  int  (*colu
1f2f5 6d 6e 5f 62 79 74 65 73 29 28 73 71 6c 69 74 65  mn_bytes)(sqlite
1f2f6 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c  3_stmt*,int iCol
1f2f7 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75  );.  int  (*colu
1f2f8 6d 6e 5f 62 79 74 65 73 31 36 29 28 73 71 6c 69  mn_bytes16)(sqli
1f2f9 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43  te3_stmt*,int iC
1f2fa 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f  ol);.  int  (*co
1f2fb 6c 75 6d 6e 5f 63 6f 75 6e 74 29 28 73 71 6c 69  lumn_count)(sqli
1f2fc 74 65 33 5f 73 74 6d 74 2a 70 53 74 6d 74 29 3b  te3_stmt*pStmt);
1f2fd 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20  .  const char * 
1f2fe 28 2a 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73  (*column_databas
1f2ff 65 5f 6e 61 6d 65 29 28 73 71 6c 69 74 65 33 5f  e_name)(sqlite3_
1f300 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f  stmt*,int);.  co
1f301 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c  nst void * (*col
1f302 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d  umn_database_nam
1f303 65 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  e16)(sqlite3_stm
1f304 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74  t*,int);.  const
1f305 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e   char * (*column
1f306 5f 64 65 63 6c 74 79 70 65 29 28 73 71 6c 69 74  _decltype)(sqlit
1f307 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 29 3b  e3_stmt*,int i);
1f308 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20  .  const void * 
1f309 28 2a 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70  (*column_decltyp
1f30a 65 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  e16)(sqlite3_stm
1f30b 74 2a 2c 69 6e 74 29 3b 0a 20 20 64 6f 75 62 6c  t*,int);.  doubl
1f30c 65 20 20 28 2a 63 6f 6c 75 6d 6e 5f 64 6f 75 62  e  (*column_doub
1f30d 6c 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  le)(sqlite3_stmt
1f30e 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69  *,int iCol);.  i
1f30f 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 69 6e 74  nt  (*column_int
1f310 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
1f311 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 73 71 6c  int iCol);.  sql
1f312 69 74 65 5f 69 6e 74 36 34 20 20 28 2a 63 6f 6c  ite_int64  (*col
1f313 75 6d 6e 5f 69 6e 74 36 34 29 28 73 71 6c 69 74  umn_int64)(sqlit
1f314 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f  e3_stmt*,int iCo
1f315 6c 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  l);.  const char
1f316 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 6e 61 6d 65   * (*column_name
1f317 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
1f318 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  int);.  const vo
1f319 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 6e 61  id * (*column_na
1f31a 6d 65 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74  me16)(sqlite3_st
1f31b 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73  mt*,int);.  cons
1f31c 74 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d  t char * (*colum
1f31d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 29 28 73  n_origin_name)(s
1f31e 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
1f31f 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  );.  const void 
1f320 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69  * (*column_origi
1f321 6e 5f 6e 61 6d 65 31 36 29 28 73 71 6c 69 74 65  n_name16)(sqlite
1f322 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20  3_stmt*,int);.  
1f323 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 63  const char * (*c
1f324 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65  olumn_table_name
1f325 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
1f326 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  int);.  const vo
1f327 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74 61  id * (*column_ta
1f328 62 6c 65 5f 6e 61 6d 65 31 36 29 28 73 71 6c 69  ble_name16)(sqli
1f329 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a  te3_stmt*,int);.
1f32a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
1f32b 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e   char * (*column
1f32c 5f 74 65 78 74 29 28 73 71 6c 69 74 65 33 5f 73  _text)(sqlite3_s
1f32d 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a  tmt*,int iCol);.
1f32e 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28    const void * (
1f32f 2a 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 29 28  *column_text16)(
1f330 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
1f331 74 20 69 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 20  t iCol);.  int  
1f332 28 2a 63 6f 6c 75 6d 6e 5f 74 79 70 65 29 28 73  (*column_type)(s
1f333 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
1f334 20 69 43 6f 6c 29 3b 0a 20 20 73 71 6c 69 74 65   iCol);.  sqlite
1f335 33 5f 76 61 6c 75 65 2a 20 28 2a 63 6f 6c 75 6d  3_value* (*colum
1f336 6e 5f 76 61 6c 75 65 29 28 73 71 6c 69 74 65 33  n_value)(sqlite3
1f337 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29  _stmt*,int iCol)
1f338 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6d  ;.  void * (*com
1f339 6d 69 74 5f 68 6f 6f 6b 29 28 73 71 6c 69 74 65  mit_hook)(sqlite
1f33a 33 2a 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29  3*,int(*)(void*)
1f33b 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20  ,void*);.  int  
1f33c 28 2a 63 6f 6d 70 6c 65 74 65 29 28 63 6f 6e 73  (*complete)(cons
1f33d 74 20 63 68 61 72 2a 73 71 6c 29 3b 0a 20 20 69  t char*sql);.  i
1f33e 6e 74 20 20 28 2a 63 6f 6d 70 6c 65 74 65 31 36  nt  (*complete16
1f33f 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 73 71 6c  )(const void*sql
1f340 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 72 65 61  );.  int  (*crea
1f341 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 29 28 73 71  te_collation)(sq
1f342 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
1f343 72 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 69 6e 74  r*,int,void*,int
1f344 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  (*)(void*,int,co
1f345 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  nst void*,int,co
1f346 6e 73 74 20 76 6f 69 64 2a 29 29 3b 0a 20 20 69  nst void*));.  i
1f347 6e 74 20 20 28 2a 63 72 65 61 74 65 5f 63 6f 6c  nt  (*create_col
1f348 6c 61 74 69 6f 6e 31 36 29 28 73 71 6c 69 74 65  lation16)(sqlite
1f349 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69  3*,const char*,i
1f34a 6e 74 2c 76 6f 69 64 2a 2c 69 6e 74 28 2a 29 28  nt,void*,int(*)(
1f34b 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
1f34c 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
1f34d 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20 20  void*));.  int  
1f34e 28 2a 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  (*create_functio
1f34f 6e 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73  n)(sqlite3*,cons
1f350 74 20 63 68 61 72 2a 2c 69 6e 74 2c 69 6e 74 2c  t char*,int,int,
1f351 76 6f 69 64 2a 2c 76 6f 69 64 20 28 2a 78 46 75  void*,void (*xFu
1f352 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nc)(sqlite3_cont
1f353 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
1f354 5f 76 61 6c 75 65 2a 2a 29 2c 76 6f 69 64 20 28  _value**),void (
1f355 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f  *xStep)(sqlite3_
1f356 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
1f357 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 76 6f  ite3_value**),vo
1f358 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c  id (*xFinal)(sql
1f359 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 29 3b  ite3_context*));
1f35a 0a 20 20 69 6e 74 20 20 28 2a 63 72 65 61 74 65  .  int  (*create
1f35b 5f 66 75 6e 63 74 69 6f 6e 31 36 29 28 73 71 6c  _function16)(sql
1f35c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 76 6f 69 64  ite3*,const void
1f35d 2a 2c 69 6e 74 2c 69 6e 74 2c 76 6f 69 64 2a 2c  *,int,int,void*,
1f35e 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71  void (*xFunc)(sq
1f35f 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
1f360 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
1f361 2a 2a 29 2c 76 6f 69 64 20 28 2a 78 53 74 65 70  **),void (*xStep
1f362 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
1f363 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
1f364 61 6c 75 65 2a 2a 29 2c 76 6f 69 64 20 28 2a 78  alue**),void (*x
1f365 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63  Final)(sqlite3_c
1f366 6f 6e 74 65 78 74 2a 29 29 3b 0a 20 20 69 6e 74  ontext*));.  int
1f367 20 28 2a 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65   (*create_module
1f368 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  )(sqlite3*,const
1f369 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 73 71 6c   char*,const sql
1f36a 69 74 65 33 5f 6d 6f 64 75 6c 65 2a 2c 76 6f 69  ite3_module*,voi
1f36b 64 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 64 61  d*);.  int  (*da
1f36c 74 61 5f 63 6f 75 6e 74 29 28 73 71 6c 69 74 65  ta_count)(sqlite
1f36d 33 5f 73 74 6d 74 2a 70 53 74 6d 74 29 3b 0a 20  3_stmt*pStmt);. 
1f36e 20 73 71 6c 69 74 65 33 20 2a 20 28 2a 64 62 5f   sqlite3 * (*db_
1f36f 68 61 6e 64 6c 65 29 28 73 71 6c 69 74 65 33 5f  handle)(sqlite3_
1f370 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  stmt*);.  int (*
1f371 64 65 63 6c 61 72 65 5f 76 74 61 62 29 28 73 71  declare_vtab)(sq
1f372 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
1f373 72 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 65 6e  r*);.  int  (*en
1f374 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68  able_shared_cach
1f375 65 29 28 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20  e)(int);.  int  
1f376 28 2a 65 72 72 63 6f 64 65 29 28 73 71 6c 69 74  (*errcode)(sqlit
1f377 65 33 2a 64 62 29 3b 0a 20 20 63 6f 6e 73 74 20  e3*db);.  const 
1f378 63 68 61 72 20 2a 20 28 2a 65 72 72 6d 73 67 29  char * (*errmsg)
1f379 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 63 6f  (sqlite3*);.  co
1f37a 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 65 72 72  nst void * (*err
1f37b 6d 73 67 31 36 29 28 73 71 6c 69 74 65 33 2a 29  msg16)(sqlite3*)
1f37c 3b 0a 20 20 69 6e 74 20 20 28 2a 65 78 65 63 29  ;.  int  (*exec)
1f37d 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
1f37e 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 63 61  char*,sqlite3_ca
1f37f 6c 6c 62 61 63 6b 2c 76 6f 69 64 2a 2c 63 68 61  llback,void*,cha
1f380 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 65  r**);.  int  (*e
1f381 78 70 69 72 65 64 29 28 73 71 6c 69 74 65 33 5f  xpired)(sqlite3_
1f382 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 20 28  stmt*);.  int  (
1f383 2a 66 69 6e 61 6c 69 7a 65 29 28 73 71 6c 69 74  *finalize)(sqlit
1f384 65 33 5f 73 74 6d 74 2a 70 53 74 6d 74 29 3b 0a  e3_stmt*pStmt);.
1f385 20 20 76 6f 69 64 20 20 28 2a 66 72 65 65 29 28    void  (*free)(
1f386 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 20  void*);.  void  
1f387 28 2a 66 72 65 65 5f 74 61 62 6c 65 29 28 63 68  (*free_table)(ch
1f388 61 72 2a 2a 72 65 73 75 6c 74 29 3b 0a 20 20 69  ar**result);.  i
1f389 6e 74 20 20 28 2a 67 65 74 5f 61 75 74 6f 63 6f  nt  (*get_autoco
1f38a 6d 6d 69 74 29 28 73 71 6c 69 74 65 33 2a 29 3b  mmit)(sqlite3*);
1f38b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 67 65 74 5f  .  void * (*get_
1f38c 61 75 78 64 61 74 61 29 28 73 71 6c 69 74 65 33  auxdata)(sqlite3
1f38d 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 29 3b 0a  _context*,int);.
1f38e 20 20 69 6e 74 20 20 28 2a 67 65 74 5f 74 61 62    int  (*get_tab
1f38f 6c 65 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  le)(sqlite3*,con
1f390 73 74 20 63 68 61 72 2a 2c 63 68 61 72 2a 2a 2a  st char*,char***
1f391 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 63 68 61 72 2a  ,int*,int*,char*
1f392 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 67 6c 6f  *);.  int  (*glo
1f393 62 61 6c 5f 72 65 63 6f 76 65 72 29 28 76 6f 69  bal_recover)(voi
1f394 64 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 69 6e  d);.  void  (*in
1f395 74 65 72 72 75 70 74 78 29 28 73 71 6c 69 74 65  terruptx)(sqlite
1f396 33 2a 29 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e  3*);.  sqlite_in
1f397 74 36 34 20 20 28 2a 6c 61 73 74 5f 69 6e 73 65  t64  (*last_inse
1f398 72 74 5f 72 6f 77 69 64 29 28 73 71 6c 69 74 65  rt_rowid)(sqlite
1f399 33 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  3*);.  const cha
1f39a 72 20 2a 20 28 2a 6c 69 62 76 65 72 73 69 6f 6e  r * (*libversion
1f39b 29 28 76 6f 69 64 29 3b 0a 20 20 69 6e 74 20 20  )(void);.  int  
1f39c 28 2a 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d  (*libversion_num
1f39d 62 65 72 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f  ber)(void);.  vo
1f39e 69 64 20 2a 28 2a 6d 61 6c 6c 6f 63 29 28 69 6e  id *(*malloc)(in
1f39f 74 29 3b 0a 20 20 63 68 61 72 20 2a 20 28 2a 6d  t);.  char * (*m
1f3a0 70 72 69 6e 74 66 29 28 63 6f 6e 73 74 20 63 68  printf)(const ch
1f3a1 61 72 2a 2c 2e 2e 2e 29 3b 0a 20 20 69 6e 74 20  ar*,...);.  int 
1f3a2 20 28 2a 6f 70 65 6e 29 28 63 6f 6e 73 74 20 63   (*open)(const c
1f3a3 68 61 72 2a 2c 73 71 6c 69 74 65 33 2a 2a 29 3b  har*,sqlite3**);
1f3a4 0a 20 20 69 6e 74 20 20 28 2a 6f 70 65 6e 31 36  .  int  (*open16
1f3a5 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 73 71  )(const void*,sq
1f3a6 6c 69 74 65 33 2a 2a 29 3b 0a 20 20 69 6e 74 20  lite3**);.  int 
1f3a7 20 28 2a 70 72 65 70 61 72 65 29 28 73 71 6c 69   (*prepare)(sqli
1f3a8 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
1f3a9 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 73 74 6d  ,int,sqlite3_stm
1f3aa 74 2a 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2a  t**,const char**
1f3ab 29 3b 0a 20 20 69 6e 74 20 20 28 2a 70 72 65 70  );.  int  (*prep
1f3ac 61 72 65 31 36 29 28 73 71 6c 69 74 65 33 2a 2c  are16)(sqlite3*,
1f3ad 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
1f3ae 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2a 2c 63  sqlite3_stmt**,c
1f3af 6f 6e 73 74 20 76 6f 69 64 2a 2a 29 3b 0a 20 20  onst void**);.  
1f3b0 76 6f 69 64 20 2a 20 28 2a 70 72 6f 66 69 6c 65  void * (*profile
1f3b1 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 28  )(sqlite3*,void(
1f3b2 2a 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63  *)(void*,const c
1f3b3 68 61 72 2a 2c 73 71 6c 69 74 65 5f 75 69 6e 74  har*,sqlite_uint
1f3b4 36 34 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 76 6f  64),void*);.  vo
1f3b5 69 64 20 20 28 2a 70 72 6f 67 72 65 73 73 5f 68  id  (*progress_h
1f3b6 61 6e 64 6c 65 72 29 28 73 71 6c 69 74 65 33 2a  andler)(sqlite3*
1f3b7 2c 69 6e 74 2c 69 6e 74 28 2a 29 28 76 6f 69 64  ,int,int(*)(void
1f3b8 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69  *),void*);.  voi
1f3b9 64 20 2a 28 2a 72 65 61 6c 6c 6f 63 29 28 76 6f  d *(*realloc)(vo
1f3ba 69 64 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20  id*,int);.  int 
1f3bb 20 28 2a 72 65 73 65 74 29 28 73 71 6c 69 74 65   (*reset)(sqlite
1f3bc 33 5f 73 74 6d 74 2a 70 53 74 6d 74 29 3b 0a 20  3_stmt*pStmt);. 
1f3bd 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f   void  (*result_
1f3be 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f 63 6f  blob)(sqlite3_co
1f3bf 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 76 6f 69  ntext*,const voi
1f3c0 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76  d*,int,void(*)(v
1f3c1 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20  oid*));.  void  
1f3c2 28 2a 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 29  (*result_double)
1f3c3 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
1f3c4 2a 2c 64 6f 75 62 6c 65 29 3b 0a 20 20 76 6f 69  *,double);.  voi
1f3c5 64 20 20 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f  d  (*result_erro
1f3c6 72 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  r)(sqlite3_conte
1f3c7 78 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  xt*,const char*,
1f3c8 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  int);.  void  (*
1f3c9 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 29 28  result_error16)(
1f3ca 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
1f3cb 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
1f3cc 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73  );.  void  (*res
1f3cd 75 6c 74 5f 69 6e 74 29 28 73 71 6c 69 74 65 33  ult_int)(sqlite3
1f3ce 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 29 3b 0a  _context*,int);.
1f3cf 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74    void  (*result
1f3d0 5f 69 6e 74 36 34 29 28 73 71 6c 69 74 65 33 5f  _int64)(sqlite3_
1f3d1 63 6f 6e 74 65 78 74 2a 2c 73 71 6c 69 74 65 5f  context*,sqlite_
1f3d2 69 6e 74 36 34 29 3b 0a 20 20 76 6f 69 64 20 20  int64);.  void  
1f3d3 28 2a 72 65 73 75 6c 74 5f 6e 75 6c 6c 29 28 73  (*result_null)(s
1f3d4 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
1f3d5 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75  ;.  void  (*resu
1f3d6 6c 74 5f 74 65 78 74 29 28 73 71 6c 69 74 65 33  lt_text)(sqlite3
1f3d7 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20  _context*,const 
1f3d8 63 68 61 72 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a  char*,int,void(*
1f3d9 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69  )(void*));.  voi
1f3da 64 20 20 28 2a 72 65 73 75 6c 74 5f 74 65 78 74  d  (*result_text
1f3db 31 36 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  16)(sqlite3_cont
1f3dc 65 78 74 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ext*,const void*
1f3dd 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69  ,int,void(*)(voi
1f3de 64 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  d*));.  void  (*
1f3df 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 29  result_text16be)
1f3e0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
1f3e1 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  *,const void*,in
1f3e2 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  t,void(*)(void*)
1f3e3 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73  );.  void  (*res
1f3e4 75 6c 74 5f 74 65 78 74 31 36 6c 65 29 28 73 71  ult_text16le)(sq
1f3e5 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63  lite3_context*,c
1f3e6 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76  onst void*,int,v
1f3e7 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
1f3e8 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74    void  (*result
1f3e9 5f 76 61 6c 75 65 29 28 73 71 6c 69 74 65 33 5f  _value)(sqlite3_
1f3ea 63 6f 6e 74 65 78 74 2a 2c 73 71 6c 69 74 65 33  context*,sqlite3
1f3eb 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 76 6f 69 64  _value*);.  void
1f3ec 20 2a 20 28 2a 72 6f 6c 6c 62 61 63 6b 5f 68 6f   * (*rollback_ho
1f3ed 6f 6b 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69  ok)(sqlite3*,voi
1f3ee 64 28 2a 29 28 76 6f 69 64 2a 29 2c 76 6f 69 64  d(*)(void*),void
1f3ef 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 73 65 74  *);.  int  (*set
1f3f0 5f 61 75 74 68 6f 72 69 7a 65 72 29 28 73 71 6c  _authorizer)(sql
1f3f1 69 74 65 33 2a 2c 69 6e 74 28 2a 29 28 76 6f 69  ite3*,int(*)(voi
1f3f2 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61  d*,int,const cha
1f3f3 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  r*,const char*,c
1f3f4 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
1f3f5 20 63 68 61 72 2a 29 2c 76 6f 69 64 2a 29 3b 0a   char*),void*);.
1f3f6 20 20 76 6f 69 64 20 20 28 2a 73 65 74 5f 61 75    void  (*set_au
1f3f7 78 64 61 74 61 29 28 73 71 6c 69 74 65 33 5f 63  xdata)(sqlite3_c
1f3f8 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 76 6f 69 64  ontext*,int,void
1f3f9 2a 2c 76 6f 69 64 20 28 2a 29 28 76 6f 69 64 2a  *,void (*)(void*
1f3fa 29 29 3b 0a 20 20 63 68 61 72 20 2a 20 28 2a 73  ));.  char * (*s
1f3fb 6e 70 72 69 6e 74 66 29 28 69 6e 74 2c 63 68 61  nprintf)(int,cha
1f3fc 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e  r*,const char*,.
1f3fd 2e 2e 29 3b 0a 20 20 69 6e 74 20 20 28 2a 73 74  ..);.  int  (*st
1f3fe 65 70 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ep)(sqlite3_stmt
1f3ff 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 74 61 62  *);.  int  (*tab
1f400 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61  le_column_metada
1f401 74 61 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  ta)(sqlite3*,con
1f402 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
1f403 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
1f404 2c 63 68 61 72 20 63 6f 6e 73 74 2a 2a 2c 63 68  ,char const**,ch
1f405 61 72 20 63 6f 6e 73 74 2a 2a 2c 69 6e 74 2a 2c  ar const**,int*,
1f406 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a 20 20 76 6f  int*,int*);.  vo
1f407 69 64 20 20 28 2a 74 68 72 65 61 64 5f 63 6c 65  id  (*thread_cle
1f408 61 6e 75 70 29 28 76 6f 69 64 29 3b 0a 20 20 69  anup)(void);.  i
1f409 6e 74 20 20 28 2a 74 6f 74 61 6c 5f 63 68 61 6e  nt  (*total_chan
1f40a 67 65 73 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a  ges)(sqlite3*);.
1f40b 20 20 76 6f 69 64 20 2a 20 28 2a 74 72 61 63 65    void * (*trace
1f40c 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 28  )(sqlite3*,void(
1f40d 2a 78 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63  *xTrace)(void*,c
1f40e 6f 6e 73 74 20 63 68 61 72 2a 29 2c 76 6f 69 64  onst char*),void
1f40f 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 74 72 61  *);.  int  (*tra
1f410 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 29 28  nsfer_bindings)(
1f411 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 73 71  sqlite3_stmt*,sq
1f412 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20  lite3_stmt*);.  
1f413 76 6f 69 64 20 2a 20 28 2a 75 70 64 61 74 65 5f  void * (*update_
1f414 68 6f 6f 6b 29 28 73 71 6c 69 74 65 33 2a 2c 76  hook)(sqlite3*,v
1f415 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74  oid(*)(void*,int
1f416 20 2c 63 68 61 72 20 63 6f 6e 73 74 2a 2c 63 68   ,char const*,ch
1f417 61 72 20 63 6f 6e 73 74 2a 2c 73 71 6c 69 74 65  ar const*,sqlite
1f418 5f 69 6e 74 36 34 29 2c 76 6f 69 64 2a 29 3b 0a  _int64),void*);.
1f419 20 20 76 6f 69 64 20 2a 20 28 2a 75 73 65 72 5f    void * (*user_
1f41a 64 61 74 61 29 28 73 71 6c 69 74 65 33 5f 63 6f  data)(sqlite3_co
1f41b 6e 74 65 78 74 2a 29 3b 0a 20 20 63 6f 6e 73 74  ntext*);.  const
1f41c 20 76 6f 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f   void * (*value_
1f41d 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f 76 61  blob)(sqlite3_va
1f41e 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  lue*);.  int  (*
1f41f 76 61 6c 75 65 5f 62 79 74 65 73 29 28 73 71 6c  value_bytes)(sql
1f420 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20  ite3_value*);.  
1f421 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f 62 79 74  int  (*value_byt
1f422 65 73 31 36 29 28 73 71 6c 69 74 65 33 5f 76 61  es16)(sqlite3_va
1f423 6c 75 65 2a 29 3b 0a 20 20 64 6f 75 62 6c 65 20  lue*);.  double 
1f424 20 28 2a 76 61 6c 75 65 5f 64 6f 75 62 6c 65 29   (*value_double)
1f425 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
1f426 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c 75 65  ;.  int  (*value
1f427 5f 69 6e 74 29 28 73 71 6c 69 74 65 33 5f 76 61  _int)(sqlite3_va
1f428 6c 75 65 2a 29 3b 0a 20 20 73 71 6c 69 74 65 5f  lue*);.  sqlite_
1f429 69 6e 74 36 34 20 20 28 2a 76 61 6c 75 65 5f 69  int64  (*value_i
1f42a 6e 74 36 34 29 28 73 71 6c 69 74 65 33 5f 76 61  nt64)(sqlite3_va
1f42b 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  lue*);.  int  (*
1f42c 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79  value_numeric_ty
1f42d 70 65 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  pe)(sqlite3_valu
1f42e 65 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73  e*);.  const uns
1f42f 69 67 6e 65 64 20 63 68 61 72 20 2a 20 28 2a 76  igned char * (*v
1f430 61 6c 75 65 5f 74 65 78 74 29 28 73 71 6c 69 74  alue_text)(sqlit
1f431 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 63 6f  e3_value*);.  co
1f432 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 76 61 6c  nst void * (*val
1f433 75 65 5f 74 65 78 74 31 36 29 28 73 71 6c 69 74  ue_text16)(sqlit
1f434 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 63 6f  e3_value*);.  co
1f435 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 76 61 6c  nst void * (*val
1f436 75 65 5f 74 65 78 74 31 36 62 65 29 28 73 71 6c  ue_text16be)(sql
1f437 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20  ite3_value*);.  
1f438 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 76  const void * (*v
1f439 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 29 28 73  alue_text16le)(s
1f43a 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
1f43b 20 20 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f 74    int  (*value_t
1f43c 79 70 65 29 28 73 71 6c 69 74 65 33 5f 76 61 6c  ype)(sqlite3_val
1f43d 75 65 2a 29 3b 0a 20 20 63 68 61 72 20 2a 28 2a  ue*);.  char *(*
1f43e 76 6d 70 72 69 6e 74 66 29 28 63 6f 6e 73 74 20  vmprintf)(const 
1f43f 63 68 61 72 2a 2c 76 61 5f 6c 69 73 74 29 3b 0a  char*,va_list);.
1f440 20 20 2f 2a 20 41 64 64 65 64 20 3f 3f 3f 20 2a    /* Added ??? *
1f441 2f 0a 20 20 69 6e 74 20 28 2a 6f 76 65 72 6c 6f  /.  int (*overlo
1f442 61 64 5f 66 75 6e 63 74 69 6f 6e 29 28 73 71 6c  ad_function)(sql
1f443 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61  ite3*, const cha
1f444 72 20 2a 7a 46 75 6e 63 4e 61 6d 65 2c 20 69 6e  r *zFuncName, in
1f445 74 20 6e 41 72 67 29 3b 0a 20 20 2f 2a 20 41 64  t nArg);.  /* Ad
1f446 64 65 64 20 62 79 20 33 2e 33 2e 31 33 20 2a 2f  ded by 3.3.13 */
1f447 0a 20 20 69 6e 74 20 28 2a 70 72 65 70 61 72 65  .  int (*prepare
1f448 5f 76 32 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f  _v2)(sqlite3*,co
1f449 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 73 71  nst char*,int,sq
1f44a 6c 69 74 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e  lite3_stmt**,con
1f44b 73 74 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e  st char**);.  in
1f44c 74 20 28 2a 70 72 65 70 61 72 65 31 36 5f 76 32  t (*prepare16_v2
1f44d 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  )(sqlite3*,const
1f44e 20 76 6f 69 64 2a 2c 69 6e 74 2c 73 71 6c 69 74   void*,int,sqlit
1f44f 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20  e3_stmt**,const 
1f450 76 6f 69 64 2a 2a 29 3b 0a 20 20 69 6e 74 20 28  void**);.  int (
1f451 2a 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 29  *clear_bindings)
1f452 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b  (sqlite3_stmt*);
1f453 0a 20 20 2f 2a 20 41 64 64 65 64 20 62 79 20 33  .  /* Added by 3
1f454 2e 34 2e 31 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  .4.1 */.  int (*
1f455 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32  create_module_v2
1f456 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  )(sqlite3*,const
1f457 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 73 71 6c   char*,const sql
1f458 69 74 65 33 5f 6d 6f 64 75 6c 65 2a 2c 76 6f 69  ite3_module*,voi
1f459 64 2a 2c 76 6f 69 64 20 28 2a 78 44 65 73 74 72  d*,void (*xDestr
1f45a 6f 79 29 28 76 6f 69 64 20 2a 29 29 3b 0a 20 20  oy)(void *));.  
1f45b 2f 2a 20 41 64 64 65 64 20 62 79 20 33 2e 35 2e  /* Added by 3.5.
1f45c 30 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 62 69 6e  0 */.  int (*bin
1f45d 64 5f 7a 65 72 6f 62 6c 6f 62 29 28 73 71 6c 69  d_zeroblob)(sqli
1f45e 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 69 6e  te3_stmt*,int,in
1f45f 74 29 3b 0a 20 20 69 6e 74 20 28 2a 62 6c 6f 62  t);.  int (*blob
1f460 5f 62 79 74 65 73 29 28 73 71 6c 69 74 65 33 5f  _bytes)(sqlite3_
1f461 62 6c 6f 62 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  blob*);.  int (*
1f462 62 6c 6f 62 5f 63 6c 6f 73 65 29 28 73 71 6c 69  blob_close)(sqli
1f463 74 65 33 5f 62 6c 6f 62 2a 29 3b 0a 20 20 69 6e  te3_blob*);.  in
1f464 74 20 28 2a 62 6c 6f 62 5f 6f 70 65 6e 29 28 73  t (*blob_open)(s
1f465 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
1f466 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ar*,const char*,
1f467 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c 69  const char*,sqli
1f468 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74 2c 73 71  te3_int64,int,sq
1f469 6c 69 74 65 33 5f 62 6c 6f 62 2a 2a 29 3b 0a 20  lite3_blob**);. 
1f46a 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 72 65 61 64   int (*blob_read
1f46b 29 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 2c  )(sqlite3_blob*,
1f46c 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a  void*,int,int);.
1f46d 20 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 77 72 69    int (*blob_wri
1f46e 74 65 29 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62  te)(sqlite3_blob
1f46f 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  *,const void*,in
1f470 74 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a  t,int);.  int (*
1f471 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
1f472 5f 76 32 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f  _v2)(sqlite3*,co
1f473 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 76 6f  nst char*,int,vo
1f474 69 64 2a 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a  id*,int(*)(void*
1f475 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
1f476 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
1f477 29 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  ),void(*)(void*)
1f478 29 3b 0a 20 20 69 6e 74 20 28 2a 66 69 6c 65 5f  );.  int (*file_
1f479 63 6f 6e 74 72 6f 6c 29 28 73 71 6c 69 74 65 33  control)(sqlite3
1f47a 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e  *,const char*,in
1f47b 74 2c 76 6f 69 64 2a 29 3b 0a 20 20 73 71 6c 69  t,void*);.  sqli
1f47c 74 65 33 5f 69 6e 74 36 34 20 28 2a 6d 65 6d 6f  te3_int64 (*memo
1f47d 72 79 5f 68 69 67 68 77 61 74 65 72 29 28 69 6e  ry_highwater)(in
1f47e 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  t);.  sqlite3_in
1f47f 74 36 34 20 28 2a 6d 65 6d 6f 72 79 5f 75 73 65  t64 (*memory_use
1f480 64 29 28 76 6f 69 64 29 3b 0a 20 20 73 71 6c 69  d)(void);.  sqli
1f481 74 65 33 5f 6d 75 74 65 78 20 2a 28 2a 6d 75 74  te3_mutex *(*mut
1f482 65 78 5f 61 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a  ex_alloc)(int);.
1f483 20 20 76 6f 69 64 20 28 2a 6d 75 74 65 78 5f 65    void (*mutex_e
1f484 6e 74 65 72 29 28 73 71 6c 69 74 65 33 5f 6d 75  nter)(sqlite3_mu
1f485 74 65 78 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a  tex*);.  void (*
1f486 6d 75 74 65 78 5f 66 72 65 65 29 28 73 71 6c 69  mutex_free)(sqli
1f487 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 20 20 76  te3_mutex*);.  v
1f488 6f 69 64 20 28 2a 6d 75 74 65 78 5f 6c 65 61 76  oid (*mutex_leav
1f489 65 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  e)(sqlite3_mutex
1f48a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 6d 75 74 65  *);.  int (*mute
1f48b 78 5f 74 72 79 29 28 73 71 6c 69 74 65 33 5f 6d  x_try)(sqlite3_m
1f48c 75 74 65 78 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  utex*);.  int (*
1f48d 6f 70 65 6e 5f 76 32 29 28 63 6f 6e 73 74 20 63  open_v2)(const c
1f48e 68 61 72 2a 2c 73 71 6c 69 74 65 33 2a 2a 2c 69  har*,sqlite3**,i
1f48f 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  nt,const char*);
1f490 0a 20 20 69 6e 74 20 28 2a 72 65 6c 65 61 73 65  .  int (*release
1f491 5f 6d 65 6d 6f 72 79 29 28 69 6e 74 29 3b 0a 20  _memory)(int);. 
1f492 20 76 6f 69 64 20 28 2a 72 65 73 75 6c 74 5f 65   void (*result_e
1f493 72 72 6f 72 5f 6e 6f 6d 65 6d 29 28 73 71 6c 69  rror_nomem)(sqli
1f494 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20  te3_context*);. 
1f495 20 76 6f 69 64 20 28 2a 72 65 73 75 6c 74 5f 65   void (*result_e
1f496 72 72 6f 72 5f 74 6f 6f 62 69 67 29 28 73 71 6c  rror_toobig)(sql
1f497 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a  ite3_context*);.
1f498 20 20 69 6e 74 20 28 2a 73 6c 65 65 70 29 28 69    int (*sleep)(i
1f499 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a 73 6f  nt);.  void (*so
1f49a 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 29 28 69  ft_heap_limit)(i
1f49b 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76  nt);.  sqlite3_v
1f49c 66 73 20 2a 28 2a 76 66 73 5f 66 69 6e 64 29 28  fs *(*vfs_find)(
1f49d 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20  const char*);.  
1f49e 69 6e 74 20 28 2a 76 66 73 5f 72 65 67 69 73 74  int (*vfs_regist
1f49f 65 72 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a  er)(sqlite3_vfs*
1f4a0 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 76  ,int);.  int (*v
1f4a1 66 73 5f 75 6e 72 65 67 69 73 74 65 72 29 28 73  fs_unregister)(s
1f4a2 71 6c 69 74 65 33 5f 76 66 73 2a 29 3b 0a 20 20  qlite3_vfs*);.  
1f4a3 69 6e 74 20 28 2a 78 74 68 72 65 61 64 73 61 66  int (*xthreadsaf
1f4a4 65 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64  e)(void);.  void
1f4a5 20 28 2a 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c   (*result_zerobl
1f4a6 6f 62 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ob)(sqlite3_cont
1f4a7 65 78 74 2a 2c 69 6e 74 29 3b 0a 20 20 76 6f 69  ext*,int);.  voi
1f4a8 64 20 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72  d (*result_error
1f4a9 5f 63 6f 64 65 29 28 73 71 6c 69 74 65 33 5f 63  _code)(sqlite3_c
1f4aa 6f 6e 74 65 78 74 2a 2c 69 6e 74 29 3b 0a 20 20  ontext*,int);.  
1f4ab 69 6e 74 20 28 2a 74 65 73 74 5f 63 6f 6e 74 72  int (*test_contr
1f4ac 6f 6c 29 28 69 6e 74 2c 20 2e 2e 2e 29 3b 0a 20  ol)(int, ...);. 
1f4ad 20 76 6f 69 64 20 28 2a 72 61 6e 64 6f 6d 6e 65   void (*randomne
1f4ae 73 73 29 28 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a  ss)(int,void*);.
1f4af 20 20 73 71 6c 69 74 65 33 20 2a 28 2a 63 6f 6e    sqlite3 *(*con
1f4b0 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 29 28  text_db_handle)(
1f4b1 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
1f4b2 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  );.};../*.** The
1f4b3 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f   following macro
1f4b4 73 20 72 65 64 65 66 69 6e 65 20 74 68 65 20 41  s redefine the A
1f4b5 50 49 20 72 6f 75 74 69 6e 65 73 20 73 6f 20 74  PI routines so t
1f4b6 68 61 74 20 74 68 65 79 20 61 72 65 0a 2a 2a 20  hat they are.** 
1f4b7 72 65 64 69 72 65 63 74 65 64 20 74 68 72 6f 75  redirected throu
1f4b8 67 68 74 20 74 68 65 20 67 6c 6f 62 61 6c 20 73  ght the global s
1f4b9 71 6c 69 74 65 33 5f 61 70 69 20 73 74 72 75 63  qlite3_api struc
1f4ba 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ture..**.** This
1f4bb 20 68 65 61 64 65 72 20 66 69 6c 65 20 69 73 20   header file is 
1f4bc 61 6c 73 6f 20 75 73 65 64 20 62 79 20 74 68 65  also used by the
1f4bd 20 6c 6f 61 64 65 78 74 2e 63 20 73 6f 75 72 63   loadext.c sourc
1f4be 65 20 66 69 6c 65 0a 2a 2a 20 28 70 61 72 74 20  e file.** (part 
1f4bf 6f 66 20 74 68 65 20 6d 61 69 6e 20 53 51 4c 69  of the main SQLi
1f4c0 74 65 20 6c 69 62 72 61 72 79 20 2d 20 6e 6f 74  te library - not
1f4c1 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e 29 20 73   an extension) s
1f4c2 6f 20 74 68 61 74 0a 2a 2a 20 69 74 20 63 61 6e  o that.** it can
1f4c3 20 67 65 74 20 61 63 63 65 73 73 20 74 6f 20 74   get access to t
1f4c4 68 65 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  he sqlite3_api_r
1f4c5 6f 75 74 69 6e 65 73 20 73 74 72 75 63 74 75 72  outines structur
1f4c6 65 0a 2a 2a 20 64 65 66 69 6e 69 74 69 6f 6e 2e  e.** definition.
1f4c7 20 20 42 75 74 20 74 68 65 20 6d 61 69 6e 20 6c    But the main l
1f4c8 69 62 72 61 72 79 20 64 6f 65 73 20 6e 6f 74 20  ibrary does not 
1f4c9 77 61 6e 74 20 74 6f 20 72 65 64 65 66 69 6e 65  want to redefine
1f4ca 0a 2a 2a 20 74 68 65 20 41 50 49 2e 20 20 53 6f  .** the API.  So
1f4cb 20 74 68 65 20 72 65 64 65 66 69 6e 69 74 69 6f   the redefinitio
1f4cc 6e 20 6d 61 63 72 6f 73 20 61 72 65 20 6f 6e 6c  n macros are onl
1f4cd 79 20 76 61 6c 69 64 20 69 66 20 74 68 65 0a 2a  y valid if the.*
1f4ce 2a 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 6d 61  * SQLITE_CORE ma
1f4cf 63 72 6f 73 20 69 73 20 75 6e 64 65 66 69 6e 65  cros is undefine
1f4d0 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  d..*/.#ifndef SQ
1f4d1 4c 49 54 45 5f 43 4f 52 45 0a 23 64 65 66 69 6e  LITE_CORE.#defin
1f4d2 65 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67  e sqlite3_aggreg
1f4d3 61 74 65 5f 63 6f 6e 74 65 78 74 20 20 20 20 20  ate_context     
1f4d4 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 61 67   sqlite3_api->ag
1f4d5 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 0a  gregate_context.
1f4d6 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
1f4d7 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 20  aggregate_count 
1f4d8 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
1f4d9 70 69 2d 3e 61 67 67 72 65 67 61 74 65 5f 63 6f  pi->aggregate_co
1f4da 75 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  unt.#define sqli
1f4db 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 20 20  te3_bind_blob   
1f4dc 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1f4dd 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 62 6c 6f  e3_api->bind_blo
1f4de 62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  b.#define sqlite
1f4df 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 20 20 20  3_bind_double   
1f4e0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1f4e1 5f 61 70 69 2d 3e 62 69 6e 64 5f 64 6f 75 62 6c  _api->bind_doubl
1f4e2 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
1f4e3 33 5f 62 69 6e 64 5f 69 6e 74 20 20 20 20 20 20  3_bind_int      
1f4e4 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1f4e5 5f 61 70 69 2d 3e 62 69 6e 64 5f 69 6e 74 0a 23  _api->bind_int.#
1f4e6 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
1f4e7 69 6e 64 5f 69 6e 74 36 34 20 20 20 20 20 20 20  ind_int64       
1f4e8 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1f4e9 69 2d 3e 62 69 6e 64 5f 69 6e 74 36 34 0a 23 64  i->bind_int64.#d
1f4ea 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
1f4eb 6e 64 5f 6e 75 6c 6c 20 20 20 20 20 20 20 20 20  nd_null         
1f4ec 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1f4ed 2d 3e 62 69 6e 64 5f 6e 75 6c 6c 0a 23 64 65 66  ->bind_null.#def
1f4ee 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ine sqlite3_bind
1f4ef 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74  _parameter_count
1f4f0 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1f4f1 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63  bind_parameter_c
1f4f2 6f 75 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c  ount.#define sql
1f4f3 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
1f4f4 74 65 72 5f 69 6e 64 65 78 20 20 20 73 71 6c 69  ter_index   sqli
1f4f5 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 70 61  te3_api->bind_pa
1f4f6 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 0a 23 64  rameter_index.#d
1f4f7 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
1f4f8 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d  nd_parameter_nam
1f4f9 65 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69  e    sqlite3_api
1f4fa 2d 3e 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  ->bind_parameter
1f4fb 5f 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71  _name.#define sq
1f4fc 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 20  lite3_bind_text 
1f4fd 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
1f4fe 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 74  ite3_api->bind_t
1f4ff 65 78 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ext.#define sqli
1f500 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 20  te3_bind_text16 
1f501 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1f502 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 74 65 78  e3_api->bind_tex
1f503 74 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  t16.#define sqli
1f504 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 20 20  te3_bind_value  
1f505 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1f506 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 76 61 6c  e3_api->bind_val
1f507 75 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ue.#define sqlit
1f508 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 20  e3_busy_handler 
1f509 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1f50a 33 5f 61 70 69 2d 3e 62 75 73 79 5f 68 61 6e 64  3_api->busy_hand
1f50b 6c 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ler.#define sqli
1f50c 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74  te3_busy_timeout
1f50d 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1f50e 65 33 5f 61 70 69 2d 3e 62 75 73 79 5f 74 69 6d  e3_api->busy_tim
1f50f 65 6f 75 74 0a 23 64 65 66 69 6e 65 20 73 71 6c  eout.#define sql
1f510 69 74 65 33 5f 63 68 61 6e 67 65 73 20 20 20 20  ite3_changes    
1f511 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1f512 74 65 33 5f 61 70 69 2d 3e 63 68 61 6e 67 65 73  te3_api->changes
1f513 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1f514 5f 63 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20  _close          
1f515 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1f516 61 70 69 2d 3e 63 6c 6f 73 65 0a 23 64 65 66 69  api->close.#defi
1f517 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61  ne sqlite3_colla
1f518 74 69 6f 6e 5f 6e 65 65 64 65 64 20 20 20 20 20  tion_needed     
1f519 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
1f51a 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 0a  ollation_needed.
1f51b 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
1f51c 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
1f51d 31 36 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  16     sqlite3_a
1f51e 70 69 2d 3e 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65  pi->collation_ne
1f51f 65 64 65 64 31 36 0a 23 64 65 66 69 6e 65 20 73  eded16.#define s
1f520 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
1f521 6f 62 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ob            sq
1f522 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d  lite3_api->colum
1f523 6e 5f 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73  n_blob.#define s
1f524 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
1f525 74 65 73 20 20 20 20 20 20 20 20 20 20 20 73 71  tes           sq
1f526 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d  lite3_api->colum
1f527 6e 5f 62 79 74 65 73 0a 23 64 65 66 69 6e 65 20  n_bytes.#define 
1f528 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
1f529 79 74 65 73 31 36 20 20 20 20 20 20 20 20 20 73  ytes16         s
1f52a 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
1f52b 6d 6e 5f 62 79 74 65 73 31 36 0a 23 64 65 66 69  mn_bytes16.#defi
1f52c 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
1f52d 6e 5f 63 6f 75 6e 74 20 20 20 20 20 20 20 20 20  n_count         
1f52e 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
1f52f 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 0a 23 64 65 66  olumn_count.#def
1f530 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
1f531 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
1f532 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1f533 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f  column_database_
1f534 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  name.#define sql
1f535 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61  ite3_column_data
1f536 62 61 73 65 5f 6e 61 6d 65 31 36 20 73 71 6c 69  base_name16 sqli
1f537 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f  te3_api->column_
1f538 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 0a  database_name16.
1f539 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
1f53a 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 20  column_decltype 
1f53b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
1f53c 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74  pi->column_declt
1f53d 79 70 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ype.#define sqli
1f53e 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74  te3_column_declt
1f53f 79 70 65 31 36 20 20 20 20 20 20 73 71 6c 69 74  ype16      sqlit
1f540 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64  e3_api->column_d
1f541 65 63 6c 74 79 70 65 31 36 0a 23 64 65 66 69 6e  ecltype16.#defin
1f542 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
1f543 5f 64 6f 75 62 6c 65 20 20 20 20 20 20 20 20 20  _double         
1f544 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
1f545 6c 75 6d 6e 5f 64 6f 75 62 6c 65 0a 23 64 65 66  lumn_double.#def
1f546 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
1f547 6d 6e 5f 69 6e 74 20 20 20 20 20 20 20 20 20 20  mn_int          
1f548 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1f549 63 6f 6c 75 6d 6e 5f 69 6e 74 0a 23 64 65 66 69  column_int.#defi
1f54a 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
1f54b 6e 5f 69 6e 74 36 34 20 20 20 20 20 20 20 20 20  n_int64         
1f54c 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
1f54d 6f 6c 75 6d 6e 5f 69 6e 74 36 34 0a 23 64 65 66  olumn_int64.#def
1f54e 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
1f54f 6d 6e 5f 6e 61 6d 65 20 20 20 20 20 20 20 20 20  mn_name         
1f550 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1f551 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 0a 23 64 65 66  column_name.#def
1f552 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
1f553 6d 6e 5f 6e 61 6d 65 31 36 20 20 20 20 20 20 20  mn_name16       
1f554 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1f555 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 0a 23 64  column_name16.#d
1f556 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
1f557 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
1f558 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1f559 2d 3e 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f  ->column_origin_
1f55a 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  name.#define sql
1f55b 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67  ite3_column_orig
1f55c 69 6e 5f 6e 61 6d 65 31 36 20 20 20 73 71 6c 69  in_name16   sqli
1f55d 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f  te3_api->column_
1f55e 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 0a 23 64  origin_name16.#d
1f55f 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
1f560 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 20  lumn_table_name 
1f561 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1f562 2d 3e 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e  ->column_table_n
1f563 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ame.#define sqli
1f564 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65  te3_column_table
1f565 5f 6e 61 6d 65 31 36 20 20 20 20 73 71 6c 69 74  _name16    sqlit
1f566 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74  e3_api->column_t
1f567 61 62 6c 65 5f 6e 61 6d 65 31 36 0a 23 64 65 66  able_name16.#def
1f568 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
1f569 6d 6e 5f 74 65 78 74 20 20 20 20 20 20 20 20 20  mn_text         
1f56a 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1f56b 63 6f 6c 75 6d 6e 5f 74 65 78 74 0a 23 64 65 66  column_text.#def
1f56c 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
1f56d 6d 6e 5f 74 65 78 74 31 36 20 20 20 20 20 20 20  mn_text16       
1f56e 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1f56f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 0a 23 64  column_text16.#d
1f570 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
1f571 6c 75 6d 6e 5f 74 79 70 65 20 20 20 20 20 20 20  lumn_type       
1f572 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1f573 2d 3e 63 6f 6c 75 6d 6e 5f 74 79 70 65 0a 23 64  ->column_type.#d
1f574 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
1f575 6c 75 6d 6e 5f 76 61 6c 75 65 20 20 20 20 20 20  lumn_value      
1f576 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1f577 2d 3e 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 0a 23  ->column_value.#
1f578 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
1f579 6f 6d 6d 69 74 5f 68 6f 6f 6b 20 20 20 20 20 20  ommit_hook      
1f57a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1f57b 69 2d 3e 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 0a 23  i->commit_hook.#
1f57c 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
1f57d 6f 6d 70 6c 65 74 65 20 20 20 20 20 20 20 20 20  omplete         
1f57e 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1f57f 69 2d 3e 63 6f 6d 70 6c 65 74 65 0a 23 64 65 66  i->complete.#def
1f580 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  ine sqlite3_comp
1f581 6c 65 74 65 31 36 20 20 20 20 20 20 20 20 20 20  lete16          
1f582 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1f583 63 6f 6d 70 6c 65 74 65 31 36 0a 23 64 65 66 69  complete16.#defi
1f584 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ne sqlite3_creat
1f585 65 5f 63 6f 6c 6c 61 74 69 6f 6e 20 20 20 20 20  e_collation     
1f586 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
1f587 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 0a  reate_collation.
1f588 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
1f589 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
1f58a 31 36 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  16     sqlite3_a
1f58b 70 69 2d 3e 63 72 65 61 74 65 5f 63 6f 6c 6c 61  pi->create_colla
1f58c 74 69 6f 6e 31 36 0a 23 64 65 66 69 6e 65 20 73  tion16.#define s
1f58d 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
1f58e 6e 63 74 69 6f 6e 20 20 20 20 20 20 20 20 73 71  nction        sq
1f58f 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61 74  lite3_api->creat
1f590 65 5f 66 75 6e 63 74 69 6f 6e 0a 23 64 65 66 69  e_function.#defi
1f591 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ne sqlite3_creat
1f592 65 5f 66 75 6e 63 74 69 6f 6e 31 36 20 20 20 20  e_function16    
1f593 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
1f594 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36  reate_function16
1f595 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1f596 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 20 20  _create_module  
1f597 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1f598 61 70 69 2d 3e 63 72 65 61 74 65 5f 6d 6f 64 75  api->create_modu
1f599 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  le.#define sqlit
1f59a 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
1f59b 5f 76 32 20 20 20 20 20 20 20 73 71 6c 69 74 65  _v2       sqlite
1f59c 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 6d 6f  3_api->create_mo
1f59d 64 75 6c 65 5f 76 32 0a 23 64 65 66 69 6e 65 20  dule_v2.#define 
1f59e 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75  sqlite3_data_cou
1f59f 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 73  nt             s
1f5a0 71 6c 69 74 65 33 5f 61 70 69 2d 3e 64 61 74 61  qlite3_api->data
1f5a1 5f 63 6f 75 6e 74 0a 23 64 65 66 69 6e 65 20 73  _count.#define s
1f5a2 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65  qlite3_db_handle
1f5a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
1f5a4 6c 69 74 65 33 5f 61 70 69 2d 3e 64 62 5f 68 61  lite3_api->db_ha
1f5a5 6e 64 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  ndle.#define sql
1f5a6 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61  ite3_declare_vta
1f5a7 62 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  b           sqli
1f5a8 74 65 33 5f 61 70 69 2d 3e 64 65 63 6c 61 72 65  te3_api->declare
1f5a9 5f 76 74 61 62 0a 23 64 65 66 69 6e 65 20 73 71  _vtab.#define sq
1f5aa 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61  lite3_enable_sha
1f5ab 72 65 64 5f 63 61 63 68 65 20 20 20 20 73 71 6c  red_cache    sql
1f5ac 69 74 65 33 5f 61 70 69 2d 3e 65 6e 61 62 6c 65  ite3_api->enable
1f5ad 5f 73 68 61 72 65 64 5f 63 61 63 68 65 0a 23 64  _shared_cache.#d
1f5ae 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 72  efine sqlite3_er
1f5af 72 63 6f 64 65 20 20 20 20 20 20 20 20 20 20 20  rcode           
1f5b0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1f5b1 2d 3e 65 72 72 63 6f 64 65 0a 23 64 65 66 69 6e  ->errcode.#defin
1f5b2 65 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  e sqlite3_errmsg
1f5b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f5b4 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 72   sqlite3_api->er
1f5b5 72 6d 73 67 0a 23 64 65 66 69 6e 65 20 73 71 6c  rmsg.#define sql
1f5b6 69 74 65 33 5f 65 72 72 6d 73 67 31 36 20 20 20  ite3_errmsg16   
1f5b7 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1f5b8 74 65 33 5f 61 70 69 2d 3e 65 72 72 6d 73 67 31  te3_api->errmsg1
1f5b9 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  6.#define sqlite
1f5ba 33 5f 65 78 65 63 20 20 20 20 20 20 20 20 20 20  3_exec          
1f5bb 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1f5bc 5f 61 70 69 2d 3e 65 78 65 63 0a 23 64 65 66 69  _api->exec.#defi
1f5bd 6e 65 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72  ne sqlite3_expir
1f5be 65 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ed              
1f5bf 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65    sqlite3_api->e
1f5c0 78 70 69 72 65 64 0a 23 64 65 66 69 6e 65 20 73  xpired.#define s
1f5c1 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20  qlite3_finalize 
1f5c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
1f5c3 6c 69 74 65 33 5f 61 70 69 2d 3e 66 69 6e 61 6c  lite3_api->final
1f5c4 69 7a 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ize.#define sqli
1f5c5 74 65 33 5f 66 72 65 65 20 20 20 20 20 20 20 20  te3_free        
1f5c6 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1f5c7 65 33 5f 61 70 69 2d 3e 66 72 65 65 0a 23 64 65  e3_api->free.#de
1f5c8 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 72 65  fine sqlite3_fre
1f5c9 65 5f 74 61 62 6c 65 20 20 20 20 20 20 20 20 20  e_table         
1f5ca 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
1f5cb 3e 66 72 65 65 5f 74 61 62 6c 65 0a 23 64 65 66  >free_table.#def
1f5cc 69 6e 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  ine sqlite3_get_
1f5cd 61 75 74 6f 63 6f 6d 6d 69 74 20 20 20 20 20 20  autocommit      
1f5ce 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1f5cf 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 0a 23  get_autocommit.#
1f5d0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 67  define sqlite3_g
1f5d1 65 74 5f 61 75 78 64 61 74 61 20 20 20 20 20 20  et_auxdata      
1f5d2 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1f5d3 69 2d 3e 67 65 74 5f 61 75 78 64 61 74 61 0a 23  i->get_auxdata.#
1f5d4 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 67  define sqlite3_g
1f5d5 65 74 5f 74 61 62 6c 65 20 20 20 20 20 20 20 20  et_table        
1f5d6 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1f5d7 69 2d 3e 67 65 74 5f 74 61 62 6c 65 0a 23 64 65  i->get_table.#de
1f5d8 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 67 6c 6f  fine sqlite3_glo
1f5d9 62 61 6c 5f 72 65 63 6f 76 65 72 20 20 20 20 20  bal_recover     
1f5da 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
1f5db 3e 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 0a  >global_recover.
1f5dc 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
1f5dd 69 6e 74 65 72 72 75 70 74 20 20 20 20 20 20 20  interrupt       
1f5de 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
1f5df 70 69 2d 3e 69 6e 74 65 72 72 75 70 74 78 0a 23  pi->interruptx.#
1f5e0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c  define sqlite3_l
1f5e1 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64  ast_insert_rowid
1f5e2 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1f5e3 69 2d 3e 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  i->last_insert_r
1f5e4 6f 77 69 64 0a 23 64 65 66 69 6e 65 20 73 71 6c  owid.#define sql
1f5e5 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 20  ite3_libversion 
1f5e6 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1f5e7 74 65 33 5f 61 70 69 2d 3e 6c 69 62 76 65 72 73  te3_api->libvers
1f5e8 69 6f 6e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ion.#define sqli
1f5e9 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e  te3_libversion_n
1f5ea 75 6d 62 65 72 20 20 20 20 20 20 73 71 6c 69 74  umber      sqlit
1f5eb 65 33 5f 61 70 69 2d 3e 6c 69 62 76 65 72 73 69  e3_api->libversi
1f5ec 6f 6e 5f 6e 75 6d 62 65 72 0a 23 64 65 66 69 6e  on_number.#defin
1f5ed 65 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  e sqlite3_malloc
1f5ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f5ef 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 61   sqlite3_api->ma
1f5f0 6c 6c 6f 63 0a 23 64 65 66 69 6e 65 20 73 71 6c  lloc.#define sql
1f5f1 69 74 65 33 5f 6d 70 72 69 6e 74 66 20 20 20 20  ite3_mprintf    
1f5f2 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1f5f3 74 65 33 5f 61 70 69 2d 3e 6d 70 72 69 6e 74 66  te3_api->mprintf
1f5f4 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1f5f5 5f 6f 70 65 6e 20 20 20 20 20 20 20 20 20 20 20  _open           
1f5f6 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1f5f7 61 70 69 2d 3e 6f 70 65 6e 0a 23 64 65 66 69 6e  api->open.#defin
1f5f8 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36  e sqlite3_open16
1f5f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f5fa 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 70   sqlite3_api->op
1f5fb 65 6e 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c  en16.#define sql
1f5fc 69 74 65 33 5f 70 72 65 70 61 72 65 20 20 20 20  ite3_prepare    
1f5fd 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1f5fe 74 65 33 5f 61 70 69 2d 3e 70 72 65 70 61 72 65  te3_api->prepare
1f5ff 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1f600 5f 70 72 65 70 61 72 65 31 36 20 20 20 20 20 20  _prepare16      
1f601 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1f602 61 70 69 2d 3e 70 72 65 70 61 72 65 31 36 0a 23  api->prepare16.#
1f603 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70  define sqlite3_p
1f604 72 65 70 61 72 65 5f 76 32 20 20 20 20 20 20 20  repare_v2       
1f605 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1f606 69 2d 3e 70 72 65 70 61 72 65 5f 76 32 0a 23 64  i->prepare_v2.#d
1f607 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72  efine sqlite3_pr
1f608 65 70 61 72 65 31 36 5f 76 32 20 20 20 20 20 20  epare16_v2      
1f609 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1f60a 2d 3e 70 72 65 70 61 72 65 31 36 5f 76 32 0a 23  ->prepare16_v2.#
1f60b 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70  define sqlite3_p
1f60c 72 6f 66 69 6c 65 20 20 20 20 20 20 20 20 20 20  rofile          
1f60d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1f60e 69 2d 3e 70 72 6f 66 69 6c 65 0a 23 64 65 66 69  i->profile.#defi
1f60f 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72  ne sqlite3_progr
1f610 65 73 73 5f 68 61 6e 64 6c 65 72 20 20 20 20 20  ess_handler     
1f611 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70    sqlite3_api->p
1f612 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72 0a  rogress_handler.
1f613 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
1f614 72 65 61 6c 6c 6f 63 20 20 20 20 20 20 20 20 20  realloc         
1f615 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
1f616 70 69 2d 3e 72 65 61 6c 6c 6f 63 0a 23 64 65 66  pi->realloc.#def
1f617 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 65  ine sqlite3_rese
1f618 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
1f619 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1f61a 72 65 73 65 74 0a 23 64 65 66 69 6e 65 20 73 71  reset.#define sq
1f61b 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f  lite3_result_blo
1f61c 62 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  b            sql
1f61d 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74  ite3_api->result
1f61e 5f 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73 71  _blob.#define sq
1f61f 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75  lite3_result_dou
1f620 62 6c 65 20 20 20 20 20 20 20 20 20 20 73 71 6c  ble          sql
1f621 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74  ite3_api->result
1f622 5f 64 6f 75 62 6c 65 0a 23 64 65 66 69 6e 65 20  _double.#define 
1f623 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
1f624 72 72 6f 72 20 20 20 20 20 20 20 20 20 20 20 73  rror           s
1f625 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75  qlite3_api->resu
1f626 6c 74 5f 65 72 72 6f 72 0a 23 64 65 66 69 6e 65  lt_error.#define
1f627 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1f628 65 72 72 6f 72 31 36 20 20 20 20 20 20 20 20 20  error16         
1f629 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73  sqlite3_api->res
1f62a 75 6c 74 5f 65 72 72 6f 72 31 36 0a 23 64 65 66  ult_error16.#def
1f62b 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ine sqlite3_resu
1f62c 6c 74 5f 69 6e 74 20 20 20 20 20 20 20 20 20 20  lt_int          
1f62d 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1f62e 72 65 73 75 6c 74 5f 69 6e 74 0a 23 64 65 66 69  result_int.#defi
1f62f 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ne sqlite3_resul
1f630 74 5f 69 6e 74 36 34 20 20 20 20 20 20 20 20 20  t_int64         
1f631 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
1f632 65 73 75 6c 74 5f 69 6e 74 36 34 0a 23 64 65 66  esult_int64.#def
1f633 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ine sqlite3_resu
1f634 6c 74 5f 6e 75 6c 6c 20 20 20 20 20 20 20 20 20  lt_null         
1f635 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1f636 72 65 73 75 6c 74 5f 6e 75 6c 6c 0a 23 64 65 66  result_null.#def
1f637 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ine sqlite3_resu
1f638 6c 74 5f 74 65 78 74 20 20 20 20 20 20 20 20 20  lt_text         
1f639 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1f63a 72 65 73 75 6c 74 5f 74 65 78 74 0a 23 64 65 66  result_text.#def
1f63b 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ine sqlite3_resu
1f63c 6c 74 5f 74 65 78 74 31 36 20 20 20 20 20 20 20  lt_text16       
1f63d 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1f63e 72 65 73 75 6c 74 5f 74 65 78 74 31 36 0a 23 64  result_text16.#d
1f63f 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
1f640 73 75 6c 74 5f 74 65 78 74 31 36 62 65 20 20 20  sult_text16be   
1f641 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1f642 2d 3e 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62  ->result_text16b
1f643 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
1f644 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c  3_result_text16l
1f645 65 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  e        sqlite3
1f646 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 74 65 78  _api->result_tex
1f647 74 31 36 6c 65 0a 23 64 65 66 69 6e 65 20 73 71  t16le.#define sq
1f648 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c  lite3_result_val
1f649 75 65 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  ue           sql
1f64a 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74  ite3_api->result
1f64b 5f 76 61 6c 75 65 0a 23 64 65 66 69 6e 65 20 73  _value.#define s
1f64c 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f  qlite3_rollback_
1f64d 68 6f 6f 6b 20 20 20 20 20 20 20 20 20 20 73 71  hook          sq
1f64e 6c 69 74 65 33 5f 61 70 69 2d 3e 72 6f 6c 6c 62  lite3_api->rollb
1f64f 61 63 6b 5f 68 6f 6f 6b 0a 23 64 65 66 69 6e 65  ack_hook.#define
1f650 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74   sqlite3_set_aut
1f651 68 6f 72 69 7a 65 72 20 20 20 20 20 20 20 20 20  horizer         
1f652 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 65 74  sqlite3_api->set
1f653 5f 61 75 74 68 6f 72 69 7a 65 72 0a 23 64 65 66  _authorizer.#def
1f654 69 6e 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f  ine sqlite3_set_
1f655 61 75 78 64 61 74 61 20 20 20 20 20 20 20 20 20  auxdata         
1f656 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1f657 73 65 74 5f 61 75 78 64 61 74 61 0a 23 64 65 66  set_auxdata.#def
1f658 69 6e 65 20 73 71 6c 69 74 65 33 5f 73 6e 70 72  ine sqlite3_snpr
1f659 69 6e 74 66 20 20 20 20 20 20 20 20 20 20 20 20  intf            
1f65a 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1f65b 73 6e 70 72 69 6e 74 66 0a 23 64 65 66 69 6e 65  snprintf.#define
1f65c 20 73 71 6c 69 74 65 33 5f 73 74 65 70 20 20 20   sqlite3_step   
1f65d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f65e 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 74 65  sqlite3_api->ste
1f65f 70 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  p.#define sqlite
1f660 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d  3_table_column_m
1f661 65 74 61 64 61 74 61 20 20 73 71 6c 69 74 65 33  etadata  sqlite3
1f662 5f 61 70 69 2d 3e 74 61 62 6c 65 5f 63 6f 6c 75  _api->table_colu
1f663 6d 6e 5f 6d 65 74 61 64 61 74 61 0a 23 64 65 66  mn_metadata.#def
1f664 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 68 72 65  ine sqlite3_thre
1f665 61 64 5f 63 6c 65 61 6e 75 70 20 20 20 20 20 20  ad_cleanup      
1f666 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1f667 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 0a 23  thread_cleanup.#
1f668 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74  define sqlite3_t
1f669 6f 74 61 6c 5f 63 68 61 6e 67 65 73 20 20 20 20  otal_changes    
1f66a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1f66b 69 2d 3e 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  i->total_changes
1f66c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1f66d 5f 74 72 61 63 65 20 20 20 20 20 20 20 20 20 20  _trace          
1f66e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1f66f 61 70 69 2d 3e 74 72 61 63 65 0a 23 64 65 66 69  api->trace.#defi
1f670 6e 65 20 73 71 6c 69 74 65 33 5f 74 72 61 6e 73  ne sqlite3_trans
1f671 66 65 72 5f 62 69 6e 64 69 6e 67 73 20 20 20 20  fer_bindings    
1f672 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74    sqlite3_api->t
1f673 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67 73  ransfer_bindings
1f674 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1f675 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 20 20 20 20  _update_hook    
1f676 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1f677 61 70 69 2d 3e 75 70 64 61 74 65 5f 68 6f 6f 6b  api->update_hook
1f678 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1f679 5f 75 73 65 72 5f 64 61 74 61 20 20 20 20 20 20  _user_data      
1f67a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1f67b 61 70 69 2d 3e 75 73 65 72 5f 64 61 74 61 0a 23  api->user_data.#
1f67c 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
1f67d 61 6c 75 65 5f 62 6c 6f 62 20 20 20 20 20 20 20  alue_blob       
1f67e 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1f67f 69 2d 3e 76 61 6c 75 65 5f 62 6c 6f 62 0a 23 64  i->value_blob.#d
1f680 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61  efine sqlite3_va
1f681 6c 75 65 5f 62 79 74 65 73 20 20 20 20 20 20 20  lue_bytes       
1f682 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1f683 2d 3e 76 61 6c 75 65 5f 62 79 74 65 73 0a 23 64  ->value_bytes.#d
1f684 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61  efine sqlite3_va
1f685 6c 75 65 5f 62 79 74 65 73 31 36 20 20 20 20 20  lue_bytes16     
1f686 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1f687 2d 3e 76 61 6c 75 65 5f 62 79 74 65 73 31 36 0a  ->value_bytes16.
1f688 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
1f689 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 20 20 20  value_double    
1f68a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
1f68b 70 69 2d 3e 76 61 6c 75 65 5f 64 6f 75 62 6c 65  pi->value_double
1f68c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1f68d 5f 76 61 6c 75 65 5f 69 6e 74 20 20 20 20 20 20  _value_int      
1f68e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1f68f 61 70 69 2d 3e 76 61 6c 75 65 5f 69 6e 74 0a 23  api->value_int.#
1f690 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
1f691 61 6c 75 65 5f 69 6e 74 36 34 20 20 20 20 20 20  alue_int64      
1f692 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1f693 69 2d 3e 76 61 6c 75 65 5f 69 6e 74 36 34 0a 23  i->value_int64.#
1f694 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
1f695 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70  alue_numeric_typ
1f696 65 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70  e     sqlite3_ap
1f697 69 2d 3e 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63  i->value_numeric
1f698 5f 74 79 70 65 0a 23 64 65 66 69 6e 65 20 73 71  _type.#define sq
1f699 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1f69a 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
1f69b 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f  ite3_api->value_
1f69c 74 65 78 74 0a 23 64 65 66 69 6e 65 20 73 71 6c  text.#define sql
1f69d 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
1f69e 36 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  6           sqli
1f69f 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 74  te3_api->value_t
1f6a0 65 78 74 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ext16.#define sq
1f6a1 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1f6a2 31 36 62 65 20 20 20 20 20 20 20 20 20 73 71 6c  16be         sql
1f6a3 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f  ite3_api->value_
1f6a4 74 65 78 74 31 36 62 65 0a 23 64 65 66 69 6e 65  text16be.#define
1f6a5 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
1f6a6 65 78 74 31 36 6c 65 20 20 20 20 20 20 20 20 20  ext16le         
1f6a7 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c  sqlite3_api->val
1f6a8 75 65 5f 74 65 78 74 31 36 6c 65 0a 23 64 65 66  ue_text16le.#def
1f6a9 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ine sqlite3_valu
1f6aa 65 5f 74 79 70 65 20 20 20 20 20 20 20 20 20 20  e_type          
1f6ab 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1f6ac 76 61 6c 75 65 5f 74 79 70 65 0a 23 64 65 66 69  value_type.#defi
1f6ad 6e 65 20 73 71 6c 69 74 65 33 5f 76 6d 70 72 69  ne sqlite3_vmpri
1f6ae 6e 74 66 20 20 20 20 20 20 20 20 20 20 20 20 20  ntf             
1f6af 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76    sqlite3_api->v
1f6b0 6d 70 72 69 6e 74 66 0a 23 64 65 66 69 6e 65 20  mprintf.#define 
1f6b1 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64  sqlite3_overload
1f6b2 5f 66 75 6e 63 74 69 6f 6e 20 20 20 20 20 20 73  _function      s
1f6b3 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 76 65 72  qlite3_api->over
1f6b4 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 0a 23 64  load_function.#d
1f6b5 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72  efine sqlite3_pr
1f6b6 65 70 61 72 65 5f 76 32 20 20 20 20 20 20 20 20  epare_v2        
1f6b7 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1f6b8 2d 3e 70 72 65 70 61 72 65 5f 76 32 0a 23 64 65  ->prepare_v2.#de
1f6b9 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65  fine sqlite3_pre
1f6ba 70 61 72 65 31 36 5f 76 32 20 20 20 20 20 20 20  pare16_v2       
1f6bb 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
1f6bc 3e 70 72 65 70 61 72 65 31 36 5f 76 32 0a 23 64  >prepare16_v2.#d
1f6bd 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6c  efine sqlite3_cl
1f6be 65 61 72 5f 62 69 6e 64 69 6e 67 73 20 20 20 20  ear_bindings    
1f6bf 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1f6c0 2d 3e 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73  ->clear_bindings
1f6c1 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1f6c2 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 20 20  _bind_zeroblob  
1f6c3 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1f6c4 61 70 69 2d 3e 62 69 6e 64 5f 7a 65 72 6f 62 6c  api->bind_zerobl
1f6c5 6f 62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ob.#define sqlit
1f6c6 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 20 20 20  e3_blob_bytes   
1f6c7 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1f6c8 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f 62 79 74 65  3_api->blob_byte
1f6c9 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  s.#define sqlite
1f6ca 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 20 20 20 20  3_blob_close    
1f6cb 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1f6cc 5f 61 70 69 2d 3e 62 6c 6f 62 5f 63 6c 6f 73 65  _api->blob_close
1f6cd 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1f6ce 5f 62 6c 6f 62 5f 6f 70 65 6e 20 20 20 20 20 20  _blob_open      
1f6cf 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1f6d0 61 70 69 2d 3e 62 6c 6f 62 5f 6f 70 65 6e 0a 23  api->blob_open.#
1f6d1 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
1f6d2 6c 6f 62 5f 72 65 61 64 20 20 20 20 20 20 20 20  lob_read        
1f6d3 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1f6d4 69 2d 3e 62 6c 6f 62 5f 72 65 61 64 0a 23 64 65  i->blob_read.#de
1f6d5 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f  fine sqlite3_blo
1f6d6 62 5f 77 72 69 74 65 20 20 20 20 20 20 20 20 20  b_write         
1f6d7 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
1f6d8 3e 62 6c 6f 62 5f 77 72 69 74 65 0a 23 64 65 66  >blob_write.#def
1f6d9 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61  ine sqlite3_crea
1f6da 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 20  te_collation_v2 
1f6db 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
1f6dc 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
1f6dd 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  _v2.#define sqli
1f6de 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c  te3_file_control
1f6df 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1f6e0 65 33 5f 61 70 69 2d 3e 66 69 6c 65 5f 63 6f 6e  e3_api->file_con
1f6e1 74 72 6f 6c 0a 23 64 65 66 69 6e 65 20 73 71 6c  trol.#define sql
1f6e2 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68  ite3_memory_high
1f6e3 77 61 74 65 72 20 20 20 20 20 20 20 73 71 6c 69  water       sqli
1f6e4 74 65 33 5f 61 70 69 2d 3e 6d 65 6d 6f 72 79 5f  te3_api->memory_
1f6e5 68 69 67 68 77 61 74 65 72 0a 23 64 65 66 69 6e  highwater.#defin
1f6e6 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79  e sqlite3_memory
1f6e7 5f 75 73 65 64 20 20 20 20 20 20 20 20 20 20 20  _used           
1f6e8 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 65   sqlite3_api->me
1f6e9 6d 6f 72 79 5f 75 73 65 64 0a 23 64 65 66 69 6e  mory_used.#defin
1f6ea 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
1f6eb 61 6c 6c 6f 63 20 20 20 20 20 20 20 20 20 20 20  alloc           
1f6ec 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75   sqlite3_api->mu
1f6ed 74 65 78 5f 61 6c 6c 6f 63 0a 23 64 65 66 69 6e  tex_alloc.#defin
1f6ee 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
1f6ef 65 6e 74 65 72 20 20 20 20 20 20 20 20 20 20 20  enter           
1f6f0 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75   sqlite3_api->mu
1f6f1 74 65 78 5f 65 6e 74 65 72 0a 23 64 65 66 69 6e  tex_enter.#defin
1f6f2 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
1f6f3 66 72 65 65 20 20 20 20 20 20 20 20 20 20 20 20  free            
1f6f4 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75   sqlite3_api->mu
1f6f5 74 65 78 5f 66 72 65 65 0a 23 64 65 66 69 6e 65  tex_free.#define
1f6f6 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
1f6f7 65 61 76 65 20 20 20 20 20 20 20 20 20 20 20 20  eave            
1f6f8 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75 74  sqlite3_api->mut
1f6f9 65 78 5f 6c 65 61 76 65 0a 23 64 65 66 69 6e 65  ex_leave.#define
1f6fa 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74   sqlite3_mutex_t
1f6fb 72 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ry              
1f6fc 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75 74  sqlite3_api->mut
1f6fd 65 78 5f 74 72 79 0a 23 64 65 66 69 6e 65 20 73  ex_try.#define s
1f6fe 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 20 20  qlite3_open_v2  
1f6ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
1f700 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 70 65 6e 5f  lite3_api->open_
1f701 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  v2.#define sqlit
1f702 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72  e3_release_memor
1f703 79 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  y         sqlite
1f704 33 5f 61 70 69 2d 3e 72 65 6c 65 61 73 65 5f 6d  3_api->release_m
1f705 65 6d 6f 72 79 0a 23 64 65 66 69 6e 65 20 73 71  emory.#define sq
1f706 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
1f707 6f 72 5f 6e 6f 6d 65 6d 20 20 20 20 20 73 71 6c  or_nomem     sql
1f708 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74  ite3_api->result
1f709 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 0a 23 64 65  _error_nomem.#de
1f70a 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73  fine sqlite3_res
1f70b 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67  ult_error_toobig
1f70c 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
1f70d 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f  >result_error_to
1f70e 6f 62 69 67 0a 23 64 65 66 69 6e 65 20 73 71 6c  obig.#define sql
1f70f 69 74 65 33 5f 73 6c 65 65 70 20 20 20 20 20 20  ite3_sleep      
1f710 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1f711 74 65 33 5f 61 70 69 2d 3e 73 6c 65 65 70 0a 23  te3_api->sleep.#
1f712 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73  define sqlite3_s
1f713 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 20 20  oft_heap_limit  
1f714 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1f715 69 2d 3e 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d  i->soft_heap_lim
1f716 69 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  it.#define sqlit
1f717 65 33 5f 76 66 73 5f 66 69 6e 64 20 20 20 20 20  e3_vfs_find     
1f718 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1f719 33 5f 61 70 69 2d 3e 76 66 73 5f 66 69 6e 64 0a  3_api->vfs_find.
1f71a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
1f71b 76 66 73 5f 72 65 67 69 73 74 65 72 20 20 20 20  vfs_register    
1f71c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
1f71d 70 69 2d 3e 76 66 73 5f 72 65 67 69 73 74 65 72  pi->vfs_register
1f71e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1f71f 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 20  _vfs_unregister 
1f720 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1f721 61 70 69 2d 3e 76 66 73 5f 75 6e 72 65 67 69 73  api->vfs_unregis
1f722 74 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ter.#define sqli
1f723 74 65 33 5f 74 68 72 65 61 64 73 61 66 65 20 20  te3_threadsafe  
1f724 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1f725 65 33 5f 61 70 69 2d 3e 78 74 68 72 65 61 64 73  e3_api->xthreads
1f726 61 66 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  afe.#define sqli
1f727 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62  te3_result_zerob
1f728 6c 6f 62 20 20 20 20 20 20 20 20 73 71 6c 69 74  lob        sqlit
1f729 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 7a  e3_api->result_z
1f72a 65 72 6f 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20  eroblob.#define 
1f72b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
1f72c 72 72 6f 72 5f 63 6f 64 65 20 20 20 20 20 20 73  rror_code      s
1f72d 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75  qlite3_api->resu
1f72e 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 0a 23 64  lt_error_code.#d
1f72f 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 65  efine sqlite3_te
1f730 73 74 5f 63 6f 6e 74 72 6f 6c 20 20 20 20 20 20  st_control      
1f731 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1f732 2d 3e 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 0a 23  ->test_control.#
1f733 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
1f734 61 6e 64 6f 6d 6e 65 73 73 20 20 20 20 20 20 20  andomness       
1f735 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
1f736 69 2d 3e 72 61 6e 64 6f 6d 6e 65 73 73 0a 23 64  i->randomness.#d
1f737 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
1f738 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 20  ntext_db_handle 
1f739 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
1f73a 2d 3e 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  ->context_db_han
1f73b 64 6c 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  dle.#endif /* SQ
1f73c 4c 49 54 45 5f 43 4f 52 45 20 2a 2f 0a 0a 23 64  LITE_CORE */..#d
1f73d 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 58 54  efine SQLITE_EXT
1f73e 45 4e 53 49 4f 4e 5f 49 4e 49 54 31 20 20 20 20  ENSION_INIT1    
1f73f 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61   const sqlite3_a
1f740 70 69 5f 72 6f 75 74 69 6e 65 73 20 2a 73 71 6c  pi_routines *sql
1f741 69 74 65 33 5f 61 70 69 3b 0a 23 64 65 66 69 6e  ite3_api;.#defin
1f742 65 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49  e SQLITE_EXTENSI
1f743 4f 4e 5f 49 4e 49 54 32 28 76 29 20 20 73 71 6c  ON_INIT2(v)  sql
1f744 69 74 65 33 5f 61 70 69 20 3d 20 76 3b 0a 0a 23  ite3_api = v;..#
1f745 65 6e 64 69 66 20 2f 2a 20 5f 53 51 4c 49 54 45  endif /* _SQLITE
1f746 33 45 58 54 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a  3EXT_H_ */../***
1f747 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
1f748 6f 66 20 73 71 6c 69 74 65 33 65 78 74 2e 68 20  of sqlite3ext.h 
1f749 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f74a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f74b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
1f74c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
1f74d 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
1f74e 6c 65 66 74 20 6f 66 66 20 69 6e 20 6c 6f 61 64  left off in load
1f74f 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ext.c **********
1f750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66  **********/..#if
1f751 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1f752 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a  _LOAD_EXTENSION.
1f753 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 41 50 49 20  ./*.** Some API 
1f754 72 6f 75 74 69 6e 65 73 20 61 72 65 20 6f 6d 69  routines are omi
1f755 74 74 65 64 20 77 68 65 6e 20 76 61 72 69 6f 75  tted when variou
1f756 73 20 66 65 61 74 75 72 65 73 20 61 72 65 0a 2a  s features are.*
1f757 2a 20 65 78 63 6c 75 64 65 64 20 66 72 6f 6d 20  * excluded from 
1f758 61 20 62 75 69 6c 64 20 6f 66 20 53 51 4c 69 74  a build of SQLit
1f759 65 2e 20 20 53 75 62 73 74 69 74 75 74 65 20 61  e.  Substitute a
1f75a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a   NULL pointer.**
1f75b 20 66 6f 72 20 61 6e 79 20 6d 69 73 73 69 6e 67   for any missing
1f75c 20 41 50 49 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65   APIs..*/.#ifnde
1f75d 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
1f75e 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a  COLUMN_METADATA.
1f75f 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
1f760 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65  _column_database
1f761 5f 6e 61 6d 65 20 20 20 30 0a 23 20 64 65 66 69  _name   0.# defi
1f762 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
1f763 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31  n_database_name1
1f764 36 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  6 0.# define sql
1f765 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c  ite3_column_tabl
1f766 65 5f 6e 61 6d 65 20 20 20 20 20 20 30 0a 23 20  e_name      0.# 
1f767 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
1f768 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65  olumn_table_name
1f769 31 36 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65  16    0.# define
1f76a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1f76b 6f 72 69 67 69 6e 5f 6e 61 6d 65 20 20 20 20 20  origin_name     
1f76c 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
1f76d 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e  e3_column_origin
1f76e 5f 6e 61 6d 65 31 36 20 20 20 30 0a 23 20 64 65  _name16   0.# de
1f76f 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 61 62  fine sqlite3_tab
1f770 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61  le_column_metada
1f771 74 61 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69  ta  0.#endif..#i
1f772 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
1f773 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 23  _AUTHORIZATION.#
1f774 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
1f775 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 20  set_authorizer  
1f776 20 20 20 20 20 20 20 30 0a 23 65 6e 64 69 66 0a         0.#endif.
1f777 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
1f778 4d 49 54 5f 55 54 46 31 36 0a 23 20 64 65 66 69  MIT_UTF16.# defi
1f779 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ne sqlite3_bind_
1f77a 74 65 78 74 31 36 20 20 20 20 20 20 20 20 20 20  text16          
1f77b 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
1f77c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
1f77d 65 65 64 65 64 31 36 20 20 20 20 20 30 0a 23 20  eeded16     0.# 
1f77e 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
1f77f 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36  olumn_decltype16
1f780 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
1f781 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1f782 6e 61 6d 65 31 36 20 20 20 20 20 20 20 20 20 20  name16          
1f783 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
1f784 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
1f785 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65            0.# de
1f786 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d  fine sqlite3_com
1f787 70 6c 65 74 65 31 36 20 20 20 20 20 20 20 20 20  plete16         
1f788 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
1f789 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
1f78a 6c 6c 61 74 69 6f 6e 31 36 20 20 20 20 20 30 0a  llation16     0.
1f78b 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
1f78c 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
1f78d 31 36 20 20 20 20 20 20 30 0a 23 20 64 65 66 69  16      0.# defi
1f78e 6e 65 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  ne sqlite3_errms
1f78f 67 31 36 20 20 20 20 20 20 20 20 20 20 20 20 20  g16             
1f790 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
1f791 69 74 65 33 5f 6f 70 65 6e 31 36 20 20 20 20 20  ite3_open16     
1f792 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 20              0.# 
1f793 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70  define sqlite3_p
1f794 72 65 70 61 72 65 31 36 20 20 20 20 20 20 20 20  repare16        
1f795 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
1f796 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
1f797 31 36 5f 76 32 20 20 20 20 20 20 20 20 20 20 20  16_v2           
1f798 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
1f799 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31  e3_result_error1
1f79a 36 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65  6         0.# de
1f79b 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73  fine sqlite3_res
1f79c 75 6c 74 5f 74 65 78 74 31 36 20 20 20 20 20 20  ult_text16      
1f79d 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
1f79e 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
1f79f 78 74 31 36 62 65 20 20 20 20 20 20 20 20 30 0a  xt16be        0.
1f7a0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
1f7a1 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65  _result_text16le
1f7a2 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69          0.# defi
1f7a3 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ne sqlite3_value
1f7a4 5f 74 65 78 74 31 36 20 20 20 20 20 20 20 20 20  _text16         
1f7a5 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
1f7a6 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
1f7a7 36 62 65 20 20 20 20 20 20 20 20 20 30 0a 23 20  6be         0.# 
1f7a8 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
1f7a9 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 20 20 20  alue_text16le   
1f7aa 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
1f7ab 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1f7ac 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 20  database_name16 
1f7ad 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
1f7ae 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f  e3_column_table_
1f7af 6e 61 6d 65 31 36 20 20 20 20 30 0a 23 20 64 65  name16    0.# de
1f7b0 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
1f7b1 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31  umn_origin_name1
1f7b2 36 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69  6   0.#endif..#i
1f7b3 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
1f7b4 5f 43 4f 4d 50 4c 45 54 45 0a 23 20 64 65 66 69  _COMPLETE.# defi
1f7b5 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  ne sqlite3_compl
1f7b6 65 74 65 20 30 0a 23 20 64 65 66 69 6e 65 20 73  ete 0.# define s
1f7b7 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31  qlite3_complete1
1f7b8 36 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  6 0.#endif..#ifd
1f7b9 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  ef SQLITE_OMIT_P
1f7ba 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b  ROGRESS_CALLBACK
1f7bb 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
1f7bc 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c  3_progress_handl
1f7bd 65 72 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66  er 0.#endif..#if
1f7be 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1f7bf 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 23 20 64  VIRTUALTABLE.# d
1f7c0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72  efine sqlite3_cr
1f7c1 65 61 74 65 5f 6d 6f 64 75 6c 65 20 30 0a 23 20  eate_module 0.# 
1f7c2 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
1f7c3 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 20  reate_module_v2 
1f7c4 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
1f7c5 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 20  e3_declare_vtab 
1f7c6 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  0.#endif..#ifdef
1f7c7 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
1f7c8 52 45 44 5f 43 41 43 48 45 0a 23 20 64 65 66 69  RED_CACHE.# defi
1f7c9 6e 65 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c  ne sqlite3_enabl
1f7ca 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 20 30  e_shared_cache 0
1f7cb 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
1f7cc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
1f7cd 45 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  E.# define sqlit
1f7ce 65 33 5f 70 72 6f 66 69 6c 65 20 20 20 20 20 20  e3_profile      
1f7cf 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
1f7d0 74 65 33 5f 74 72 61 63 65 20 20 20 20 20 20 20  te3_trace       
1f7d1 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64    0.#endif..#ifd
1f7d2 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47  ef SQLITE_OMIT_G
1f7d3 45 54 5f 54 41 42 4c 45 0a 23 20 64 65 66 69 6e  ET_TABLE.# defin
1f7d4 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74  e sqlite3_free_t
1f7d5 61 62 6c 65 20 20 20 20 30 0a 23 20 64 65 66 69  able    0.# defi
1f7d6 6e 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  ne sqlite3_get_t
1f7d7 61 62 6c 65 20 20 20 20 20 30 0a 23 65 6e 64 69  able     0.#endi
1f7d8 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
1f7d9 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 23  _OMIT_INCRBLOB.#
1f7da 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
1f7db 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 20 20 30 0a  ind_zeroblob  0.
1f7dc 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
1f7dd 62 6c 6f 62 5f 62 79 74 65 73 20 20 20 20 20 30  blob_bytes     0
1f7de 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
1f7df 5f 62 6c 6f 62 5f 63 6c 6f 73 65 20 20 20 20 20  _blob_close     
1f7e0 30 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  0.#define sqlite
1f7e1 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20 20 20 20 20  3_blob_open     
1f7e2 20 30 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74   0.#define sqlit
1f7e3 65 33 5f 62 6c 6f 62 5f 72 65 61 64 20 20 20 20  e3_blob_read    
1f7e4 20 20 30 0a 23 64 65 66 69 6e 65 20 73 71 6c 69    0.#define sqli
1f7e5 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 20 20  te3_blob_write  
1f7e6 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a     0.#endif../*.
1f7e7 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
1f7e8 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61   structure conta
1f7e9 69 6e 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  ins pointers to 
1f7ea 61 6c 6c 20 53 51 4c 69 74 65 20 41 50 49 20 72  all SQLite API r
1f7eb 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 41 20 70 6f  outines..** A po
1f7ec 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74  inter to this st
1f7ed 72 75 63 74 75 72 65 20 69 73 20 70 61 73 73 65  ructure is passe
1f7ee 64 20 69 6e 74 6f 20 65 78 74 65 6e 73 69 6f 6e  d into extension
1f7ef 73 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 0a  s when they are.
1f7f0 2a 2a 20 6c 6f 61 64 65 64 20 73 6f 20 74 68 61  ** loaded so tha
1f7f1 74 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20  t the extension 
1f7f2 63 61 6e 20 6d 61 6b 65 20 63 61 6c 6c 73 20 62  can make calls b
1f7f3 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 53 51 4c  ack into the SQL
1f7f4 69 74 65 0a 2a 2a 20 6c 69 62 72 61 72 79 2e 0a  ite.** library..
1f7f5 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 64 64 69 6e  **.** When addin
1f7f6 67 20 6e 65 77 20 41 50 49 73 2c 20 61 64 64 20  g new APIs, add 
1f7f7 74 68 65 6d 20 74 6f 20 74 68 65 20 62 6f 74 74  them to the bott
1f7f8 6f 6d 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  om of this struc
1f7f9 74 75 72 65 0a 2a 2a 20 69 6e 20 6f 72 64 65 72  ture.** in order
1f7fa 20 74 6f 20 70 72 65 73 65 72 76 65 20 62 61 63   to preserve bac
1f7fb 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69  kwards compatibi
1f7fc 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 45 78 74 65  lity..**.** Exte
1f7fd 6e 73 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20  nsions that use 
1f7fe 6e 65 77 65 72 20 41 50 49 73 20 73 68 6f 75 6c  newer APIs shoul
1f7ff 64 20 66 69 72 73 74 20 63 61 6c 6c 20 74 68 65  d first call the
1f800 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 69 62 76  .** sqlite3_libv
1f801 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 29 20  ersion_number() 
1f802 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
1f803 74 20 74 68 65 20 41 50 49 20 74 68 65 79 0a 2a  t the API they.*
1f804 2a 20 69 6e 74 65 6e 64 20 74 6f 20 75 73 65 20  * intend to use 
1f805 69 73 20 73 75 70 70 6f 72 74 65 64 20 62 79 20  is supported by 
1f806 74 68 65 20 6c 69 62 72 61 72 79 2e 20 20 45 78  the library.  Ex
1f807 74 65 6e 73 69 6f 6e 73 20 73 68 6f 75 6c 64 0a  tensions should.
1f808 2a 2a 20 61 6c 73 6f 20 63 68 65 63 6b 20 74 6f  ** also check to
1f809 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   make sure that 
1f80a 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  the pointer to t
1f80b 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a  he function is.*
1f80c 2a 20 6e 6f 74 20 4e 55 4c 4c 20 62 65 66 6f 72  * not NULL befor
1f80d 65 20 63 61 6c 6c 69 6e 67 20 69 74 2e 0a 2a 2f  e calling it..*/
1f80e 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71  .static const sq
1f80f 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e  lite3_api_routin
1f810 65 73 20 73 71 6c 69 74 65 33 41 70 69 73 20 3d  es sqlite3Apis =
1f811 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 61 67 67   {.  sqlite3_agg
1f812 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 2c 0a  regate_context,.
1f813 20 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67    sqlite3_aggreg
1f814 61 74 65 5f 63 6f 75 6e 74 2c 0a 20 20 73 71 6c  ate_count,.  sql
1f815 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 2c 0a  ite3_bind_blob,.
1f816 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64    sqlite3_bind_d
1f817 6f 75 62 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33  ouble,.  sqlite3
1f818 5f 62 69 6e 64 5f 69 6e 74 2c 0a 20 20 73 71 6c  _bind_int,.  sql
1f819 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 2c  ite3_bind_int64,
1f81a 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
1f81b 6e 75 6c 6c 2c 0a 20 20 73 71 6c 69 74 65 33 5f  null,.  sqlite3_
1f81c 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63  bind_parameter_c
1f81d 6f 75 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f  ount,.  sqlite3_
1f81e 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69  bind_parameter_i
1f81f 6e 64 65 78 2c 0a 20 20 73 71 6c 69 74 65 33 5f  ndex,.  sqlite3_
1f820 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e  bind_parameter_n
1f821 61 6d 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  ame,.  sqlite3_b
1f822 69 6e 64 5f 74 65 78 74 2c 0a 20 20 73 71 6c 69  ind_text,.  sqli
1f823 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 2c  te3_bind_text16,
1f824 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
1f825 76 61 6c 75 65 2c 0a 20 20 73 71 6c 69 74 65 33  value,.  sqlite3
1f826 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 2c 0a 20  _busy_handler,. 
1f827 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69   sqlite3_busy_ti
1f828 6d 65 6f 75 74 2c 0a 20 20 73 71 6c 69 74 65 33  meout,.  sqlite3
1f829 5f 63 68 61 6e 67 65 73 2c 0a 20 20 73 71 6c 69  _changes,.  sqli
1f82a 74 65 33 5f 63 6c 6f 73 65 2c 0a 20 20 73 71 6c  te3_close,.  sql
1f82b 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
1f82c 65 65 64 65 64 2c 0a 20 20 73 71 6c 69 74 65 33  eeded,.  sqlite3
1f82d 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  _collation_neede
1f82e 64 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  d16,.  sqlite3_c
1f82f 6f 6c 75 6d 6e 5f 62 6c 6f 62 2c 0a 20 20 73 71  olumn_blob,.  sq
1f830 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
1f831 65 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  es,.  sqlite3_co
1f832 6c 75 6d 6e 5f 62 79 74 65 73 31 36 2c 0a 20 20  lumn_bytes16,.  
1f833 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63  sqlite3_column_c
1f834 6f 75 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f  ount,.  sqlite3_
1f835 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f  column_database_
1f836 6e 61 6d 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  name,.  sqlite3_
1f837 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f  column_database_
1f838 6e 61 6d 65 31 36 2c 0a 20 20 73 71 6c 69 74 65  name16,.  sqlite
1f839 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70  3_column_decltyp
1f83a 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  e,.  sqlite3_col
1f83b 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 2c 0a  umn_decltype16,.
1f83c 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
1f83d 5f 64 6f 75 62 6c 65 2c 0a 20 20 73 71 6c 69 74  _double,.  sqlit
1f83e 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 2c 0a 20  e3_column_int,. 
1f83f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1f840 69 6e 74 36 34 2c 0a 20 20 73 71 6c 69 74 65 33  int64,.  sqlite3
1f841 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 2c 0a 20 20  _column_name,.  
1f842 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
1f843 61 6d 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  ame16,.  sqlite3
1f844 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  _column_origin_n
1f845 61 6d 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  ame,.  sqlite3_c
1f846 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d  olumn_origin_nam
1f847 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  e16,.  sqlite3_c
1f848 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65  olumn_table_name
1f849 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
1f84a 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 2c  mn_table_name16,
1f84b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
1f84c 6e 5f 74 65 78 74 2c 0a 20 20 73 71 6c 69 74 65  n_text,.  sqlite
1f84d 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 2c  3_column_text16,
1f84e 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
1f84f 6e 5f 74 79 70 65 2c 0a 20 20 73 71 6c 69 74 65  n_type,.  sqlite
1f850 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 2c 0a  3_column_value,.
1f851 20 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74    sqlite3_commit
1f852 5f 68 6f 6f 6b 2c 0a 20 20 73 71 6c 69 74 65 33  _hook,.  sqlite3
1f853 5f 63 6f 6d 70 6c 65 74 65 2c 0a 20 20 73 71 6c  _complete,.  sql
1f854 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 2c  ite3_complete16,
1f855 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  .  sqlite3_creat
1f856 65 5f 63 6f 6c 6c 61 74 69 6f 6e 2c 0a 20 20 73  e_collation,.  s
1f857 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
1f858 6c 6c 61 74 69 6f 6e 31 36 2c 0a 20 20 73 71 6c  llation16,.  sql
1f859 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
1f85a 74 69 6f 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f  tion,.  sqlite3_
1f85b 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31  create_function1
1f85c 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65  6,.  sqlite3_cre
1f85d 61 74 65 5f 6d 6f 64 75 6c 65 2c 0a 20 20 73 71  ate_module,.  sq
1f85e 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74  lite3_data_count
1f85f 2c 0a 20 20 73 71 6c 69 74 65 33 5f 64 62 5f 68  ,.  sqlite3_db_h
1f860 61 6e 64 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33  andle,.  sqlite3
1f861 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 2c 0a 20  _declare_vtab,. 
1f862 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f   sqlite3_enable_
1f863 73 68 61 72 65 64 5f 63 61 63 68 65 2c 0a 20 20  shared_cache,.  
1f864 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 2c  sqlite3_errcode,
1f865 0a 20 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  .  sqlite3_errms
1f866 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65 72 72  g,.  sqlite3_err
1f867 6d 73 67 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  msg16,.  sqlite3
1f868 5f 65 78 65 63 2c 0a 20 20 73 71 6c 69 74 65 33  _exec,.  sqlite3
1f869 5f 65 78 70 69 72 65 64 2c 0a 20 20 73 71 6c 69  _expired,.  sqli
1f86a 74 65 33 5f 66 69 6e 61 6c 69 7a 65 2c 0a 20 20  te3_finalize,.  
1f86b 73 71 6c 69 74 65 33 5f 66 72 65 65 2c 0a 20 20  sqlite3_free,.  
1f86c 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62  sqlite3_free_tab
1f86d 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 67 65  le,.  sqlite3_ge
1f86e 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 2c 0a 20 20  t_autocommit,.  
1f86f 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64  sqlite3_get_auxd
1f870 61 74 61 2c 0a 20 20 73 71 6c 69 74 65 33 5f 67  ata,.  sqlite3_g
1f871 65 74 5f 74 61 62 6c 65 2c 0a 20 20 30 2c 20 20  et_table,.  0,  
1f872 20 20 20 2f 2a 20 57 61 73 20 73 71 6c 69 74 65     /* Was sqlite
1f873 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72  3_global_recover
1f874 28 29 2c 20 62 75 74 20 74 68 61 74 20 66 75 6e  (), but that fun
1f875 63 74 69 6f 6e 20 69 73 20 64 65 70 72 65 63 61  ction is depreca
1f876 74 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ted */.  sqlite3
1f877 5f 69 6e 74 65 72 72 75 70 74 2c 0a 20 20 73 71  _interrupt,.  sq
1f878 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72  lite3_last_inser
1f879 74 5f 72 6f 77 69 64 2c 0a 20 20 73 71 6c 69 74  t_rowid,.  sqlit
1f87a 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 2c 0a 20  e3_libversion,. 
1f87b 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73   sqlite3_libvers
1f87c 69 6f 6e 5f 6e 75 6d 62 65 72 2c 0a 20 20 73 71  ion_number,.  sq
1f87d 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2c 0a 20 20  lite3_malloc,.  
1f87e 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 2c  sqlite3_mprintf,
1f87f 0a 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 2c  .  sqlite3_open,
1f880 0a 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31  .  sqlite3_open1
1f881 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 65  6,.  sqlite3_pre
1f882 70 61 72 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  pare,.  sqlite3_
1f883 70 72 65 70 61 72 65 31 36 2c 0a 20 20 73 71 6c  prepare16,.  sql
1f884 69 74 65 33 5f 70 72 6f 66 69 6c 65 2c 0a 20 20  ite3_profile,.  
1f885 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73  sqlite3_progress
1f886 5f 68 61 6e 64 6c 65 72 2c 0a 20 20 73 71 6c 69  _handler,.  sqli
1f887 74 65 33 5f 72 65 61 6c 6c 6f 63 2c 0a 20 20 73  te3_realloc,.  s
1f888 71 6c 69 74 65 33 5f 72 65 73 65 74 2c 0a 20 20  qlite3_reset,.  
1f889 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
1f88a 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  lob,.  sqlite3_r
1f88b 65 73 75 6c 74 5f 64 6f 75 62 6c 65 2c 0a 20 20  esult_double,.  
1f88c 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
1f88d 72 72 6f 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f  rror,.  sqlite3_
1f88e 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 2c 0a  result_error16,.
1f88f 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
1f890 5f 69 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f  _int,.  sqlite3_
1f891 72 65 73 75 6c 74 5f 69 6e 74 36 34 2c 0a 20 20  result_int64,.  
1f892 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
1f893 75 6c 6c 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  ull,.  sqlite3_r
1f894 65 73 75 6c 74 5f 74 65 78 74 2c 0a 20 20 73 71  esult_text,.  sq
1f895 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
1f896 74 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  t16,.  sqlite3_r
1f897 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 2c 0a  esult_text16be,.
1f898 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
1f899 5f 74 65 78 74 31 36 6c 65 2c 0a 20 20 73 71 6c  _text16le,.  sql
1f89a 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75  ite3_result_valu
1f89b 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 6f 6c  e,.  sqlite3_rol
1f89c 6c 62 61 63 6b 5f 68 6f 6f 6b 2c 0a 20 20 73 71  lback_hook,.  sq
1f89d 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72  lite3_set_author
1f89e 69 7a 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f  izer,.  sqlite3_
1f89f 73 65 74 5f 61 75 78 64 61 74 61 2c 0a 20 20 73  set_auxdata,.  s
1f8a0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 2c  qlite3_snprintf,
1f8a1 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 2c  .  sqlite3_step,
1f8a2 0a 20 20 73 71 6c 69 74 65 33 5f 74 61 62 6c 65  .  sqlite3_table
1f8a3 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61  _column_metadata
1f8a4 2c 0a 20 20 73 71 6c 69 74 65 33 5f 74 68 72 65  ,.  sqlite3_thre
1f8a5 61 64 5f 63 6c 65 61 6e 75 70 2c 0a 20 20 73 71  ad_cleanup,.  sq
1f8a6 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e  lite3_total_chan
1f8a7 67 65 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 74  ges,.  sqlite3_t
1f8a8 72 61 63 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  race,.  sqlite3_
1f8a9 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67  transfer_binding
1f8aa 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 75 70 64  s,.  sqlite3_upd
1f8ab 61 74 65 5f 68 6f 6f 6b 2c 0a 20 20 73 71 6c 69  ate_hook,.  sqli
1f8ac 74 65 33 5f 75 73 65 72 5f 64 61 74 61 2c 0a 20  te3_user_data,. 
1f8ad 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
1f8ae 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  lob,.  sqlite3_v
1f8af 61 6c 75 65 5f 62 79 74 65 73 2c 0a 20 20 73 71  alue_bytes,.  sq
1f8b0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
1f8b1 73 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  s16,.  sqlite3_v
1f8b2 61 6c 75 65 5f 64 6f 75 62 6c 65 2c 0a 20 20 73  alue_double,.  s
1f8b3 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
1f8b4 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
1f8b5 65 5f 69 6e 74 36 34 2c 0a 20 20 73 71 6c 69 74  e_int64,.  sqlit
1f8b6 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63  e3_value_numeric
1f8b7 5f 74 79 70 65 2c 0a 20 20 73 71 6c 69 74 65 33  _type,.  sqlite3
1f8b8 5f 76 61 6c 75 65 5f 74 65 78 74 2c 0a 20 20 73  _value_text,.  s
1f8b9 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
1f8ba 74 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  t16,.  sqlite3_v
1f8bb 61 6c 75 65 5f 74 65 78 74 31 36 62 65 2c 0a 20  alue_text16be,. 
1f8bc 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
1f8bd 65 78 74 31 36 6c 65 2c 0a 20 20 73 71 6c 69 74  ext16le,.  sqlit
1f8be 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 2c 0a 20  e3_value_type,. 
1f8bf 20 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74   sqlite3_vmprint
1f8c0 66 2c 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65  f,.  /*.  ** The
1f8c1 20 6f 72 69 67 69 6e 61 6c 20 41 50 49 20 73 65   original API se
1f8c2 74 20 65 6e 64 73 20 68 65 72 65 2e 20 20 41 6c  t ends here.  Al
1f8c3 6c 20 65 78 74 65 6e 73 69 6f 6e 73 20 63 61 6e  l extensions can
1f8c4 20 63 61 6c 6c 20 61 6e 79 0a 20 20 2a 2a 20 6f   call any.  ** o
1f8c5 66 20 74 68 65 20 41 50 49 73 20 61 62 6f 76 65  f the APIs above
1f8c6 20 70 72 6f 76 69 64 65 64 20 74 68 61 74 20 74   provided that t
1f8c7 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f  he pointer is no
1f8c8 74 20 4e 55 4c 4c 2e 20 20 42 75 74 0a 20 20 2a  t NULL.  But.  *
1f8c9 2a 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67  * before calling
1f8ca 20 41 50 49 73 20 74 68 61 74 20 66 6f 6c 6c 6f   APIs that follo
1f8cb 77 2c 20 65 78 74 65 6e 73 69 6f 6e 20 73 68 6f  w, extension sho
1f8cc 75 6c 64 20 63 68 65 63 6b 20 74 68 65 0a 20 20  uld check the.  
1f8cd 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65  ** sqlite3_libve
1f8ce 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 29 20 74  rsion_number() t
1f8cf 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 79  o make sure they
1f8d0 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74   are dealing wit
1f8d1 68 0a 20 20 2a 2a 20 61 20 6c 69 62 72 61 72 79  h.  ** a library
1f8d2 20 74 68 61 74 20 69 73 20 6e 65 77 20 65 6e 6f   that is new eno
1f8d3 75 67 68 20 74 6f 20 73 75 70 70 6f 72 74 20 74  ugh to support t
1f8d4 68 61 74 20 41 50 49 2e 0a 20 20 2a 2a 2a 2a 2a  hat API..  *****
1f8d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8d9 2a 2a 2a 2a 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  ****.  */.  sqli
1f8da 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e  te3_overload_fun
1f8db 63 74 69 6f 6e 2c 0a 0a 20 20 2f 2a 0a 20 20 2a  ction,..  /*.  *
1f8dc 2a 20 41 64 64 65 64 20 61 66 74 65 72 20 33 2e  * Added after 3.
1f8dd 33 2e 31 33 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  3.13.  */.  sqli
1f8de 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 2c 0a  te3_prepare_v2,.
1f8df 20 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72    sqlite3_prepar
1f8e0 65 31 36 5f 76 32 2c 0a 20 20 73 71 6c 69 74 65  e16_v2,.  sqlite
1f8e1 33 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73  3_clear_bindings
1f8e2 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64  ,..  /*.  ** Add
1f8e3 65 64 20 66 6f 72 20 33 2e 34 2e 31 0a 20 20 2a  ed for 3.4.1.  *
1f8e4 2f 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61  /.  sqlite3_crea
1f8e5 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 2c 0a 0a 20  te_module_v2,.. 
1f8e6 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65 64 20 66   /*.  ** Added f
1f8e7 6f 72 20 33 2e 35 2e 30 0a 20 20 2a 2f 0a 20 20  or 3.5.0.  */.  
1f8e8 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72  sqlite3_bind_zer
1f8e9 6f 62 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33  oblob,.  sqlite3
1f8ea 5f 62 6c 6f 62 5f 62 79 74 65 73 2c 0a 20 20 73  _blob_bytes,.  s
1f8eb 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73  qlite3_blob_clos
1f8ec 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f  e,.  sqlite3_blo
1f8ed 62 5f 6f 70 65 6e 2c 0a 20 20 73 71 6c 69 74 65  b_open,.  sqlite
1f8ee 33 5f 62 6c 6f 62 5f 72 65 61 64 2c 0a 20 20 73  3_blob_read,.  s
1f8ef 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74  qlite3_blob_writ
1f8f0 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65  e,.  sqlite3_cre
1f8f1 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32  ate_collation_v2
1f8f2 2c 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ,.  sqlite3_file
1f8f3 5f 63 6f 6e 74 72 6f 6c 2c 0a 20 20 73 71 6c 69  _control,.  sqli
1f8f4 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77  te3_memory_highw
1f8f5 61 74 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f  ater,.  sqlite3_
1f8f6 6d 65 6d 6f 72 79 5f 75 73 65 64 2c 0a 23 69 66  memory_used,.#if
1f8f7 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  def SQLITE_MUTEX
1f8f8 5f 4e 4f 4f 50 0a 20 20 30 2c 20 0a 20 20 30 2c  _NOOP.  0, .  0,
1f8f9 20 0a 20 20 30 2c 0a 20 20 30 2c 0a 20 20 30 2c   .  0,.  0,.  0,
1f8fa 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 74 65 33  .#else.  sqlite3
1f8fb 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 2c 0a 20 20  _mutex_alloc,.  
1f8fc 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
1f8fd 74 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ter,.  sqlite3_m
1f8fe 75 74 65 78 5f 66 72 65 65 2c 0a 20 20 73 71 6c  utex_free,.  sql
1f8ff 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
1f900 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ,.  sqlite3_mute
1f901 78 5f 74 72 79 2c 0a 23 65 6e 64 69 66 0a 20 20  x_try,.#endif.  
1f902 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 2c  sqlite3_open_v2,
1f903 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61  .  sqlite3_relea
1f904 73 65 5f 6d 65 6d 6f 72 79 2c 0a 20 20 73 71 6c  se_memory,.  sql
1f905 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
1f906 72 5f 6e 6f 6d 65 6d 2c 0a 20 20 73 71 6c 69 74  r_nomem,.  sqlit
1f907 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
1f908 74 6f 6f 62 69 67 2c 0a 20 20 73 71 6c 69 74 65  toobig,.  sqlite
1f909 33 5f 73 6c 65 65 70 2c 0a 20 20 73 71 6c 69 74  3_sleep,.  sqlit
1f90a 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d  e3_soft_heap_lim
1f90b 69 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  it,.  sqlite3_vf
1f90c 73 5f 66 69 6e 64 2c 0a 20 20 73 71 6c 69 74 65  s_find,.  sqlite
1f90d 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 2c 0a  3_vfs_register,.
1f90e 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e    sqlite3_vfs_un
1f90f 72 65 67 69 73 74 65 72 2c 0a 0a 20 20 2f 2a 0a  register,..  /*.
1f910 20 20 2a 2a 20 41 64 64 65 64 20 66 6f 72 20 33    ** Added for 3
1f911 2e 35 2e 38 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  .5.8.  */.  sqli
1f912 74 65 33 5f 74 68 72 65 61 64 73 61 66 65 2c 0a  te3_threadsafe,.
1f913 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
1f914 5f 7a 65 72 6f 62 6c 6f 62 2c 0a 20 20 73 71 6c  _zeroblob,.  sql
1f915 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
1f916 72 5f 63 6f 64 65 2c 0a 20 20 73 71 6c 69 74 65  r_code,.  sqlite
1f917 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 2c 0a  3_test_control,.
1f918 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d    sqlite3_random
1f919 6e 65 73 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f  ness,.  sqlite3_
1f91a 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
1f91b 65 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 74 74  e,.};../*.** Att
1f91c 65 6d 70 74 20 74 6f 20 6c 6f 61 64 20 61 6e 20  empt to load an 
1f91d 53 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f 6e  SQLite extension
1f91e 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e   library contain
1f91f 65 64 20 69 6e 20 74 68 65 20 66 69 6c 65 0a 2a  ed in the file.*
1f920 2a 20 7a 46 69 6c 65 2e 20 20 54 68 65 20 65 6e  * zFile.  The en
1f921 74 72 79 20 70 6f 69 6e 74 20 69 73 20 7a 50 72  try point is zPr
1f922 6f 63 2e 20 20 7a 50 72 6f 63 20 6d 61 79 20 62  oc.  zProc may b
1f923 65 20 30 20 69 6e 20 77 68 69 63 68 20 63 61 73  e 0 in which cas
1f924 65 20 61 0a 2a 2a 20 64 65 66 61 75 6c 74 20 65  e a.** default e
1f925 6e 74 72 79 20 70 6f 69 6e 74 20 6e 61 6d 65 20  ntry point name 
1f926 28 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69  (sqlite3_extensi
1f927 6f 6e 5f 69 6e 69 74 29 20 69 73 20 75 73 65 64  on_init) is used
1f928 2e 20 20 55 73 65 0a 2a 2a 20 6f 66 20 74 68 65  .  Use.** of the
1f929 20 64 65 66 61 75 6c 74 20 6e 61 6d 65 20 69 73   default name is
1f92a 20 72 65 63 6f 6d 6d 65 6e 64 65 64 2e 0a 2a 2a   recommended..**
1f92b 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
1f92c 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20  E_OK on success 
1f92d 61 6e 64 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  and SQLITE_ERROR
1f92e 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f   if something go
1f92f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20  es wrong..**.** 
1f930 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
1f931 72 73 20 61 6e 64 20 70 7a 45 72 72 4d 73 67 20  rs and pzErrMsg 
1f932 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 66  is not 0, then f
1f933 69 6c 6c 20 2a 70 7a 45 72 72 4d 73 67 20 77 69  ill *pzErrMsg wi
1f934 74 68 20 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73  th .** error mes
1f935 73 61 67 65 20 74 65 78 74 2e 20 20 54 68 65 20  sage text.  The 
1f936 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  calling function
1f937 20 73 68 6f 75 6c 64 20 66 72 65 65 20 74 68 69   should free thi
1f938 73 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 79 20 63  s memory.** by c
1f939 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 66  alling sqlite3_f
1f93a 72 65 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  ree()..*/.static
1f93b 20 69 6e 74 20 73 71 6c 69 74 65 33 4c 6f 61 64   int sqlite3Load
1f93c 45 78 74 65 6e 73 69 6f 6e 28 0a 20 20 73 71 6c  Extension(.  sql
1f93d 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
1f93e 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65     /* Load the e
1f93f 78 74 65 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 68  xtension into th
1f940 69 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  is database conn
1f941 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73  ection */.  cons
1f942 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20  t char *zFile,  
1f943 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
1f944 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20   shared library 
1f945 63 6f 6e 74 61 69 6e 69 6e 67 20 65 78 74 65 6e  containing exten
1f946 73 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  sion */.  const 
1f947 63 68 61 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20  char *zProc,    
1f948 2f 2a 20 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20  /* Entry point. 
1f949 20 55 73 65 20 22 73 71 6c 69 74 65 33 5f 65 78   Use "sqlite3_ex
1f94a 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 22 20 69 66  tension_init" if
1f94b 20 30 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70   0 */.  char **p
1f94c 7a 45 72 72 4d 73 67 20 20 20 20 20 20 20 2f 2a  zErrMsg       /*
1f94d 20 50 75 74 20 65 72 72 6f 72 20 6d 65 73 73 61   Put error messa
1f94e 67 65 20 68 65 72 65 20 69 66 20 6e 6f 74 20 30  ge here if not 0
1f94f 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
1f950 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 64 62 2d  _vfs *pVfs = db-
1f951 3e 70 56 66 73 3b 0a 20 20 76 6f 69 64 20 2a 68  >pVfs;.  void *h
1f952 61 6e 64 6c 65 3b 0a 20 20 69 6e 74 20 28 2a 78  andle;.  int (*x
1f953 49 6e 69 74 29 28 73 71 6c 69 74 65 33 2a 2c 63  Init)(sqlite3*,c
1f954 68 61 72 2a 2a 2c 63 6f 6e 73 74 20 73 71 6c 69  har**,const sqli
1f955 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73  te3_api_routines
1f956 2a 29 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72  *);.  char *zErr
1f957 6d 73 67 20 3d 20 30 3b 0a 20 20 76 6f 69 64 20  msg = 0;.  void 
1f958 2a 2a 61 48 61 6e 64 6c 65 3b 0a 0a 20 20 2f 2a  **aHandle;..  /*
1f959 20 54 69 63 6b 65 74 20 23 31 38 36 33 2e 20 20   Ticket #1863.  
1f95a 54 6f 20 61 76 6f 69 64 20 61 20 63 72 65 61 74  To avoid a creat
1f95b 69 6e 67 20 73 65 63 75 72 69 74 79 20 70 72 6f  ing security pro
1f95c 62 6c 65 6d 73 20 66 6f 72 20 6f 6c 64 65 72 0a  blems for older.
1f95d 20 20 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e    ** application
1f95e 73 20 74 68 61 74 20 72 65 6c 69 6e 6b 20 61 67  s that relink ag
1f95f 61 69 6e 73 74 20 6e 65 77 65 72 20 76 65 72 73  ainst newer vers
1f960 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2c 20  ions of SQLite, 
1f961 74 68 65 0a 20 20 2a 2a 20 61 62 69 6c 69 74 79  the.  ** ability
1f962 20 74 6f 20 72 75 6e 20 6c 6f 61 64 5f 65 78 74   to run load_ext
1f963 65 6e 73 69 6f 6e 20 69 73 20 74 75 72 6e 65 64  ension is turned
1f964 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c 74 2e   off by default.
1f965 20 20 4f 6e 65 0a 20 20 2a 2a 20 6d 75 73 74 20    One.  ** must 
1f966 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 65 6e 61  call sqlite3_ena
1f967 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69  ble_load_extensi
1f968 6f 6e 28 29 20 74 6f 20 74 75 72 6e 20 6f 6e 20  on() to turn on 
1f969 65 78 74 65 6e 73 69 6f 6e 0a 20 20 2a 2a 20 6c  extension.  ** l
1f96a 6f 61 64 69 6e 67 2e 20 20 4f 74 68 65 72 77 69  oading.  Otherwi
1f96b 73 65 20 79 6f 75 20 67 65 74 20 74 68 65 20 66  se you get the f
1f96c 6f 6c 6c 6f 77 69 6e 67 20 65 72 72 6f 72 2e 0a  ollowing error..
1f96d 20 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e    */.  if( (db->
1f96e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4c  flags & SQLITE_L
1f96f 6f 61 64 45 78 74 65 6e 73 69 6f 6e 29 3d 3d 30  oadExtension)==0
1f970 20 29 7b 0a 20 20 20 20 69 66 28 20 70 7a 45 72   ){.    if( pzEr
1f971 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 2a 70  rMsg ){.      *p
1f972 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65  zErrMsg = sqlite
1f973 33 5f 6d 70 72 69 6e 74 66 28 22 6e 6f 74 20 61  3_mprintf("not a
1f974 75 74 68 6f 72 69 7a 65 64 22 29 3b 0a 20 20 20  uthorized");.   
1f975 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51   }.    return SQ
1f976 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
1f977 0a 20 20 69 66 28 20 7a 50 72 6f 63 3d 3d 30 20  .  if( zProc==0 
1f978 29 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d 20 22  ){.    zProc = "
1f979 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f  sqlite3_extensio
1f97a 6e 5f 69 6e 69 74 22 3b 0a 20 20 7d 0a 0a 20 20  n_init";.  }..  
1f97b 68 61 6e 64 6c 65 20 3d 20 73 71 6c 69 74 65 33  handle = sqlite3
1f97c 4f 73 44 6c 4f 70 65 6e 28 70 56 66 73 2c 20 7a  OsDlOpen(pVfs, z
1f97d 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 68 61 6e  File);.  if( han
1f97e 64 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  dle==0 ){.    if
1f97f 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20  ( pzErrMsg ){.  
1f980 20 20 20 20 63 68 61 72 20 7a 45 72 72 5b 32 35      char zErr[25
1f981 36 5d 3b 0a 20 20 20 20 20 20 7a 45 72 72 5b 73  6];.      zErr[s
1f982 69 7a 65 6f 66 28 7a 45 72 72 29 2d 31 5d 20 3d  izeof(zErr)-1] =
1f983 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 73 71 6c   '\0';.      sql
1f984 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
1f985 7a 65 6f 66 28 7a 45 72 72 29 2d 31 2c 20 7a 45  zeof(zErr)-1, zE
1f986 72 72 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22  rr, .          "
1f987 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 73  unable to open s
1f988 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 5b 25  hared library [%
1f989 73 5d 22 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 20  s]", zFile);.   
1f98a 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72     sqlite3OsDlEr
1f98b 72 6f 72 28 70 56 66 73 2c 20 73 69 7a 65 6f 66  ror(pVfs, sizeof
1f98c 28 7a 45 72 72 29 2d 31 2c 20 7a 45 72 72 29 3b  (zErr)-1, zErr);
1f98d 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67  .      *pzErrMsg
1f98e 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
1f98f 75 70 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20  up(db, zErr);.  
1f990 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 53    }.    return S
1f991 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
1f992 0a 20 20 78 49 6e 69 74 20 3d 20 28 69 6e 74 28  .  xInit = (int(
1f993 2a 29 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72  *)(sqlite3*,char
1f994 2a 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  **,const sqlite3
1f995 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 2a 29 29  _api_routines*))
1f996 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1f997 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 53      sqlite3OsDlS
1f998 79 6d 28 70 56 66 73 2c 20 68 61 6e 64 6c 65 2c  ym(pVfs, handle,
1f999 20 7a 50 72 6f 63 29 3b 0a 20 20 69 66 28 20 78   zProc);.  if( x
1f99a 49 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69  Init==0 ){.    i
1f99b 66 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20  f( pzErrMsg ){. 
1f99c 20 20 20 20 20 63 68 61 72 20 7a 45 72 72 5b 32       char zErr[2
1f99d 35 36 5d 3b 0a 20 20 20 20 20 20 7a 45 72 72 5b  56];.      zErr[
1f99e 73 69 7a 65 6f 66 28 7a 45 72 72 29 2d 31 5d 20  sizeof(zErr)-1] 
1f99f 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 73 71  = '\0';.      sq
1f9a0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
1f9a1 69 7a 65 6f 66 28 7a 45 72 72 29 2d 31 2c 20 7a  izeof(zErr)-1, z
1f9a2 45 72 72 2c 0a 20 20 20 20 20 20 20 20 20 20 22  Err,.          "
1f9a3 6e 6f 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 5b  no entry point [
1f9a4 25 73 5d 20 69 6e 20 73 68 61 72 65 64 20 6c 69  %s] in shared li
1f9a5 62 72 61 72 79 20 5b 25 73 5d 22 2c 20 7a 50 72  brary [%s]", zPr
1f9a6 6f 63 2c 7a 46 69 6c 65 29 3b 0a 20 20 20 20 20  oc,zFile);.     
1f9a7 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f   sqlite3OsDlErro
1f9a8 72 28 70 56 66 73 2c 20 73 69 7a 65 6f 66 28 7a  r(pVfs, sizeof(z
1f9a9 45 72 72 29 2d 31 2c 20 7a 45 72 72 29 3b 0a 20  Err)-1, zErr);. 
1f9aa 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d       *pzErrMsg =
1f9ab 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
1f9ac 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20  (db, zErr);.    
1f9ad 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f    sqlite3OsDlClo
1f9ae 73 65 28 70 56 66 73 2c 20 68 61 6e 64 6c 65 29  se(pVfs, handle)
1f9af 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
1f9b0 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
1f9b1 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 78 49 6e  .  }else if( xIn
1f9b2 69 74 28 64 62 2c 20 26 7a 45 72 72 6d 73 67 2c  it(db, &zErrmsg,
1f9b3 20 26 73 71 6c 69 74 65 33 41 70 69 73 29 20 29   &sqlite3Apis) )
1f9b4 7b 0a 20 20 20 20 69 66 28 20 70 7a 45 72 72 4d  {.    if( pzErrM
1f9b5 73 67 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45  sg ){.      *pzE
1f9b6 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 5f  rrMsg = sqlite3_
1f9b7 6d 70 72 69 6e 74 66 28 22 65 72 72 6f 72 20 64  mprintf("error d
1f9b8 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61  uring initializa
1f9b9 74 69 6f 6e 3a 20 25 73 22 2c 20 7a 45 72 72 6d  tion: %s", zErrm
1f9ba 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  sg);.    }.    s
1f9bb 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72 72  qlite3_free(zErr
1f9bc 6d 73 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  msg);.    sqlite
1f9bd 33 4f 73 44 6c 43 6c 6f 73 65 28 70 56 66 73 2c  3OsDlClose(pVfs,
1f9be 20 68 61 6e 64 6c 65 29 3b 0a 20 20 20 20 72 65   handle);.    re
1f9bf 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
1f9c0 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 70 70  R;.  }..  /* App
1f9c1 65 6e 64 20 74 68 65 20 6e 65 77 20 73 68 61 72  end the new shar
1f9c2 65 64 20 6c 69 62 72 61 72 79 20 68 61 6e 64 6c  ed library handl
1f9c3 65 20 74 6f 20 74 68 65 20 64 62 2d 3e 61 45 78  e to the db->aEx
1f9c4 74 65 6e 73 69 6f 6e 20 61 72 72 61 79 2e 20 2a  tension array. *
1f9c5 2f 0a 20 20 64 62 2d 3e 6e 45 78 74 65 6e 73 69  /.  db->nExtensi
1f9c6 6f 6e 2b 2b 3b 0a 20 20 61 48 61 6e 64 6c 65 20  on++;.  aHandle 
1f9c7 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
1f9c8 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
1f9c9 28 68 61 6e 64 6c 65 29 2a 64 62 2d 3e 6e 45 78  (handle)*db->nEx
1f9ca 74 65 6e 73 69 6f 6e 29 3b 0a 20 20 69 66 28 20  tension);.  if( 
1f9cb 61 48 61 6e 64 6c 65 3d 3d 30 20 29 7b 0a 20 20  aHandle==0 ){.  
1f9cc 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1f9cd 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28  NOMEM;.  }.  if(
1f9ce 20 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 3e   db->nExtension>
1f9cf 30 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28  0 ){.    memcpy(
1f9d0 61 48 61 6e 64 6c 65 2c 20 64 62 2d 3e 61 45 78  aHandle, db->aEx
1f9d1 74 65 6e 73 69 6f 6e 2c 20 73 69 7a 65 6f 66 28  tension, sizeof(
1f9d2 68 61 6e 64 6c 65 29 2a 28 64 62 2d 3e 6e 45 78  handle)*(db->nEx
1f9d3 74 65 6e 73 69 6f 6e 2d 31 29 29 3b 0a 20 20 7d  tension-1));.  }
1f9d4 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
1f9d5 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 29 3b  db->aExtension);
1f9d6 0a 20 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f  .  db->aExtensio
1f9d7 6e 20 3d 20 61 48 61 6e 64 6c 65 3b 0a 0a 20 20  n = aHandle;..  
1f9d8 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 5b 64  db->aExtension[d
1f9d9 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 2d 31 5d  b->nExtension-1]
1f9da 20 3d 20 68 61 6e 64 6c 65 3b 0a 20 20 72 65 74   = handle;.  ret
1f9db 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1f9dc 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1f9dd 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74  sqlite3_load_ext
1f9de 65 6e 73 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65  ension(.  sqlite
1f9df 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
1f9e0 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 78 74 65  /* Load the exte
1f9e1 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 68 69 73 20  nsion into this 
1f9e2 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1f9e3 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ion */.  const c
1f9e4 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20 20 20 2f  har *zFile,    /
1f9e5 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 73 68  * Name of the sh
1f9e6 61 72 65 64 20 6c 69 62 72 61 72 79 20 63 6f 6e  ared library con
1f9e7 74 61 69 6e 69 6e 67 20 65 78 74 65 6e 73 69 6f  taining extensio
1f9e8 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  n */.  const cha
1f9e9 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20 2f 2a 20  r *zProc,    /* 
1f9ea 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20 20 55 73  Entry point.  Us
1f9eb 65 20 22 73 71 6c 69 74 65 33 5f 65 78 74 65 6e  e "sqlite3_exten
1f9ec 73 69 6f 6e 5f 69 6e 69 74 22 20 69 66 20 30 20  sion_init" if 0 
1f9ed 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72  */.  char **pzEr
1f9ee 72 4d 73 67 20 20 20 20 20 20 20 2f 2a 20 50 75  rMsg       /* Pu
1f9ef 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  t error message 
1f9f0 68 65 72 65 20 69 66 20 6e 6f 74 20 30 20 2a 2f  here if not 0 */
1f9f1 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
1f9f2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
1f9f3 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
1f9f4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f    rc = sqlite3Lo
1f9f5 61 64 45 78 74 65 6e 73 69 6f 6e 28 64 62 2c 20  adExtension(db, 
1f9f6 7a 46 69 6c 65 2c 20 7a 50 72 6f 63 2c 20 70 7a  zFile, zProc, pz
1f9f7 45 72 72 4d 73 67 29 3b 0a 20 20 73 71 6c 69 74  ErrMsg);.  sqlit
1f9f8 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
1f9f9 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
1f9fa 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
1f9fb 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69   Call this routi
1f9fc 6e 65 20 77 68 65 6e 20 74 68 65 20 64 61 74 61  ne when the data
1f9fd 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
1f9fe 69 73 20 63 6c 6f 73 69 6e 67 20 69 6e 20 6f 72  is closing in or
1f9ff 64 65 72 0a 2a 2a 20 74 6f 20 63 6c 65 61 6e 20  der.** to clean 
1fa00 75 70 20 6c 6f 61 64 65 64 20 65 78 74 65 6e 73  up loaded extens
1fa01 69 6f 6e 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ions.*/.SQLITE_P
1fa02 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1fa03 74 65 33 43 6c 6f 73 65 45 78 74 65 6e 73 69 6f  te3CloseExtensio
1fa04 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  ns(sqlite3 *db){
1fa05 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65  .  int i;.  asse
1fa06 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1fa07 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
1fa08 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  ) );.  for(i=0; 
1fa09 69 3c 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e  i<db->nExtension
1fa0a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69  ; i++){.    sqli
1fa0b 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28 64 62 2d  te3OsDlClose(db-
1fa0c 3e 70 56 66 73 2c 20 64 62 2d 3e 61 45 78 74 65  >pVfs, db->aExte
1fa0d 6e 73 69 6f 6e 5b 69 5d 29 3b 0a 20 20 7d 0a 20  nsion[i]);.  }. 
1fa0e 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 64 62   sqlite3_free(db
1fa0f 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 29 3b 0a 7d  ->aExtension);.}
1fa10 0a 0a 2f 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f  ../*.** Enable o
1fa11 72 20 64 69 73 61 62 6c 65 20 65 78 74 65 6e 73  r disable extens
1fa12 69 6f 6e 20 6c 6f 61 64 69 6e 67 2e 20 20 45 78  ion loading.  Ex
1fa13 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20  tension loading 
1fa14 69 73 20 64 69 73 61 62 6c 65 64 20 62 79 0a 2a  is disabled by.*
1fa15 2a 20 64 65 66 61 75 6c 74 20 73 6f 20 61 73 20  * default so as 
1fa16 6e 6f 74 20 74 6f 20 6f 70 65 6e 20 73 65 63 75  not to open secu
1fa17 72 69 74 79 20 68 6f 6c 65 73 20 69 6e 20 6f 6c  rity holes in ol
1fa18 64 65 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73  der applications
1fa19 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1fa1a 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62  int sqlite3_enab
1fa1b 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f  le_load_extensio
1fa1c 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  n(sqlite3 *db, i
1fa1d 6e 74 20 6f 6e 6f 66 66 29 7b 0a 20 20 73 71 6c  nt onoff){.  sql
1fa1e 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
1fa1f 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69  (db->mutex);.  i
1fa20 66 28 20 6f 6e 6f 66 66 20 29 7b 0a 20 20 20 20  f( onoff ){.    
1fa21 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c  db->flags |= SQL
1fa22 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f  ITE_LoadExtensio
1fa23 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  n;.  }else{.    
1fa24 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51  db->flags &= ~SQ
1fa25 4c 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69  LITE_LoadExtensi
1fa26 6f 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  on;.  }.  sqlite
1fa27 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
1fa28 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
1fa29 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
1fa2a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1fa2b 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45  E_OMIT_LOAD_EXTE
1fa2c 4e 53 49 4f 4e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  NSION */../*.** 
1fa2d 54 68 65 20 61 75 74 6f 2d 65 78 74 65 6e 73 69  The auto-extensi
1fa2e 6f 6e 20 63 6f 64 65 20 61 64 64 65 64 20 72 65  on code added re
1fa2f 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74  gardless of whet
1fa30 68 65 72 20 6f 72 20 6e 6f 74 20 65 78 74 65 6e  her or not exten
1fa31 73 69 6f 6e 0a 2a 2a 20 6c 6f 61 64 69 6e 67 20  sion.** loading 
1fa32 69 73 20 73 75 70 70 6f 72 74 65 64 2e 20 20 57  is supported.  W
1fa33 65 20 6e 65 65 64 20 61 20 64 75 6d 6d 79 20 73  e need a dummy s
1fa34 71 6c 69 74 65 33 41 70 69 73 20 70 6f 69 6e 74  qlite3Apis point
1fa35 65 72 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63  er for that.** c
1fa36 6f 64 65 20 69 66 20 72 65 67 75 6c 61 72 20 65  ode if regular e
1fa37 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67  xtension loading
1fa38 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c   is not availabl
1fa39 65 2e 20 20 54 68 69 73 20 69 73 20 74 68 61 74  e.  This is that
1fa3a 0a 2a 2a 20 64 75 6d 6d 79 20 70 6f 69 6e 74 65  .** dummy pointe
1fa3b 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  r..*/.#ifdef SQL
1fa3c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58  ITE_OMIT_LOAD_EX
1fa3d 54 45 4e 53 49 4f 4e 0a 73 74 61 74 69 63 20 63  TENSION.static c
1fa3e 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69  onst sqlite3_api
1fa3f 5f 72 6f 75 74 69 6e 65 73 20 73 71 6c 69 74 65  _routines sqlite
1fa40 33 41 70 69 73 20 3d 20 7b 20 30 20 7d 3b 0a 23  3Apis = { 0 };.#
1fa41 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  endif.../*.** Th
1fa42 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 62 6a 65  e following obje
1fa43 63 74 20 68 6f 6c 64 73 20 74 68 65 20 6c 69 73  ct holds the lis
1fa44 74 20 6f 66 20 61 75 74 6f 6d 61 74 69 63 61 6c  t of automatical
1fa45 6c 79 20 6c 6f 61 64 65 64 0a 2a 2a 20 65 78 74  ly loaded.** ext
1fa46 65 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54  ensions..**.** T
1fa47 68 69 73 20 6c 69 73 74 20 69 73 20 73 68 61 72  his list is shar
1fa48 65 64 20 61 63 72 6f 73 73 20 74 68 72 65 61 64  ed across thread
1fa49 73 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 4d  s.  The SQLITE_M
1fa4a 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
1fa4b 45 52 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74  ER.** mutex must
1fa4c 20 62 65 20 68 65 6c 64 20 77 68 69 6c 65 20 61   be held while a
1fa4d 63 63 65 73 73 69 6e 67 20 74 68 69 73 20 6c 69  ccessing this li
1fa4e 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74  st..*/.static st
1fa4f 72 75 63 74 20 7b 0a 20 20 69 6e 74 20 6e 45 78  ruct {.  int nEx
1fa50 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  t;        /* Num
1fa51 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
1fa52 6e 20 61 45 78 74 5b 5d 20 2a 2f 20 20 20 20 20  n aExt[] */     
1fa53 20 20 20 20 20 0a 20 20 76 6f 69 64 20 2a 2a 61       .  void **a
1fa54 45 78 74 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e  Ext;     /* Poin
1fa55 74 65 72 73 20 74 6f 20 74 68 65 20 65 78 74 65  ters to the exte
1fa56 6e 73 69 6f 6e 20 69 6e 69 74 20 66 75 6e 63 74  nsion init funct
1fa57 69 6f 6e 73 20 2a 2f 0a 7d 20 61 75 74 6f 65 78  ions */.} autoex
1fa58 74 20 3d 20 7b 20 30 2c 20 30 20 7d 3b 0a 0a 0a  t = { 0, 0 };...
1fa59 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61  /*.** Register a
1fa5a 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b   statically link
1fa5b 65 64 20 65 78 74 65 6e 73 69 6f 6e 20 74 68 61  ed extension tha
1fa5c 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c  t is automatical
1fa5d 6c 79 0a 2a 2a 20 6c 6f 61 64 65 64 20 62 79 20  ly.** loaded by 
1fa5e 65 76 65 72 79 20 6e 65 77 20 64 61 74 61 62 61  every new databa
1fa5f 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a  se connection..*
1fa60 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1fa61 20 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f 65 78   sqlite3_auto_ex
1fa62 74 65 6e 73 69 6f 6e 28 76 6f 69 64 20 2a 78 49  tension(void *xI
1fa63 6e 69 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  nit){.  int i;. 
1fa64 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1fa65 5f 4f 4b 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  _OK;.#ifndef SQL
1fa66 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a 20  ITE_MUTEX_NOOP. 
1fa67 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
1fa68 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 5f  mutex = sqlite3_
1fa69 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49  mutex_alloc(SQLI
1fa6a 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
1fa6b 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a  MASTER);.#endif.
1fa6c 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1fa6d 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20  enter(mutex);.  
1fa6e 66 6f 72 28 69 3d 30 3b 20 69 3c 61 75 74 6f 65  for(i=0; i<autoe
1fa6f 78 74 2e 6e 45 78 74 3b 20 69 2b 2b 29 7b 0a 20  xt.nExt; i++){. 
1fa70 20 20 20 69 66 28 20 61 75 74 6f 65 78 74 2e 61     if( autoext.a
1fa71 45 78 74 5b 69 5d 3d 3d 78 49 6e 69 74 20 29 20  Ext[i]==xInit ) 
1fa72 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28  break;.  }.  if(
1fa73 20 69 3d 3d 61 75 74 6f 65 78 74 2e 6e 45 78 74   i==autoext.nExt
1fa74 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74   ){.    int nByt
1fa75 65 20 3d 20 28 61 75 74 6f 65 78 74 2e 6e 45 78  e = (autoext.nEx
1fa76 74 2b 31 29 2a 73 69 7a 65 6f 66 28 61 75 74 6f  t+1)*sizeof(auto
1fa77 65 78 74 2e 61 45 78 74 5b 30 5d 29 3b 0a 20 20  ext.aExt[0]);.  
1fa78 20 20 76 6f 69 64 20 2a 2a 61 4e 65 77 3b 0a 20    void **aNew;. 
1fa79 20 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65     aNew = sqlite
1fa7a 33 5f 72 65 61 6c 6c 6f 63 28 61 75 74 6f 65 78  3_realloc(autoex
1fa7b 74 2e 61 45 78 74 2c 20 6e 42 79 74 65 29 3b 0a  t.aExt, nByte);.
1fa7c 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20      if( aNew==0 
1fa7d 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
1fa7e 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
1fa7f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 75 74  }else{.      aut
1fa80 6f 65 78 74 2e 61 45 78 74 20 3d 20 61 4e 65 77  oext.aExt = aNew
1fa81 3b 0a 20 20 20 20 20 20 61 75 74 6f 65 78 74 2e  ;.      autoext.
1fa82 61 45 78 74 5b 61 75 74 6f 65 78 74 2e 6e 45 78  aExt[autoext.nEx
1fa83 74 5d 20 3d 20 78 49 6e 69 74 3b 0a 20 20 20 20  t] = xInit;.    
1fa84 20 20 61 75 74 6f 65 78 74 2e 6e 45 78 74 2b 2b    autoext.nExt++
1fa85 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71  ;.    }.  }.  sq
1fa86 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1fa87 65 28 6d 75 74 65 78 29 3b 0a 20 20 61 73 73 65  e(mutex);.  asse
1fa88 72 74 28 20 28 72 63 26 30 78 66 66 29 3d 3d 72  rt( (rc&0xff)==r
1fa89 63 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  c );.  return rc
1fa8a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74  ;.}../*.** Reset
1fa8b 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63 20 65   the automatic e
1fa8c 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67  xtension loading
1fa8d 20 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f 0a 53   mechanism..*/.S
1fa8e 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
1fa8f 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75 74  qlite3_reset_aut
1fa90 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64  o_extension(void
1fa91 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ){.#ifndef SQLIT
1fa92 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a 20 20 73  E_MUTEX_NOOP.  s
1fa93 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
1fa94 74 65 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 75  tex = sqlite3_mu
1fa95 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45  tex_alloc(SQLITE
1fa96 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
1fa97 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20  STER);.#endif.  
1fa98 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
1fa99 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 73 71  ter(mutex);.  sq
1fa9a 6c 69 74 65 33 5f 66 72 65 65 28 61 75 74 6f 65  lite3_free(autoe
1fa9b 78 74 2e 61 45 78 74 29 3b 0a 20 20 61 75 74 6f  xt.aExt);.  auto
1fa9c 65 78 74 2e 61 45 78 74 20 3d 20 30 3b 0a 20 20  ext.aExt = 0;.  
1fa9d 61 75 74 6f 65 78 74 2e 6e 45 78 74 20 3d 20 30  autoext.nExt = 0
1fa9e 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
1fa9f 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a  x_leave(mutex);.
1faa0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 61 64 20 61 6c  }../*.** Load al
1faa1 6c 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65  l automatic exte
1faa2 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54  nsions..*/.SQLIT
1faa3 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1faa4 6c 69 74 65 33 41 75 74 6f 4c 6f 61 64 45 78 74  lite3AutoLoadExt
1faa5 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 20  ensions(sqlite3 
1faa6 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  *db){.  int i;. 
1faa7 20 69 6e 74 20 67 6f 20 3d 20 31 3b 0a 20 20 69   int go = 1;.  i
1faa8 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1faa9 4b 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69 74  K;.  int (*xInit
1faaa 29 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a  )(sqlite3*,char*
1faab 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  *,const sqlite3_
1faac 61 70 69 5f 72 6f 75 74 69 6e 65 73 2a 29 3b 0a  api_routines*);.
1faad 0a 20 20 69 66 28 20 61 75 74 6f 65 78 74 2e 6e  .  if( autoext.n
1faae 45 78 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  Ext==0 ){.    /*
1faaf 20 43 6f 6d 6d 6f 6e 20 63 61 73 65 3a 20 65 61   Common case: ea
1fab0 72 6c 79 20 6f 75 74 20 77 69 74 68 6f 75 74 20  rly out without 
1fab1 65 76 65 72 79 20 68 61 76 69 6e 67 20 74 6f 20  every having to 
1fab2 61 63 71 75 69 72 65 20 61 20 6d 75 74 65 78 20  acquire a mutex 
1fab3 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  */.    return SQ
1fab4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 66  LITE_OK;.  }.  f
1fab5 6f 72 28 69 3d 30 3b 20 67 6f 3b 20 69 2b 2b 29  or(i=0; go; i++)
1fab6 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72  {.    char *zErr
1fab7 6d 73 67 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66  msg = 0;.#ifndef
1fab8 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f   SQLITE_MUTEX_NO
1fab9 4f 50 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  OP.    sqlite3_m
1faba 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71  utex *mutex = sq
1fabb 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
1fabc 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
1fabd 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23  TATIC_MASTER);.#
1fabe 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 74 65  endif.    sqlite
1fabf 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75  3_mutex_enter(mu
1fac0 74 65 78 29 3b 0a 20 20 20 20 69 66 28 20 69 3e  tex);.    if( i>
1fac1 3d 61 75 74 6f 65 78 74 2e 6e 45 78 74 20 29 7b  =autoext.nExt ){
1fac2 0a 20 20 20 20 20 20 78 49 6e 69 74 20 3d 20 30  .      xInit = 0
1fac3 3b 0a 20 20 20 20 20 20 67 6f 20 3d 20 30 3b 0a  ;.      go = 0;.
1fac4 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1fac5 20 78 49 6e 69 74 20 3d 20 28 69 6e 74 28 2a 29   xInit = (int(*)
1fac6 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a  (sqlite3*,char**
1fac7 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61  ,const sqlite3_a
1fac8 70 69 5f 72 6f 75 74 69 6e 65 73 2a 29 29 0a 20  pi_routines*)). 
1fac9 20 20 20 20 20 20 20 20 20 20 20 20 20 61 75 74               aut
1faca 6f 65 78 74 2e 61 45 78 74 5b 69 5d 3b 0a 20 20  oext.aExt[i];.  
1facb 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
1facc 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65  mutex_leave(mute
1facd 78 29 3b 0a 20 20 20 20 69 66 28 20 78 49 6e 69  x);.    if( xIni
1face 74 20 26 26 20 78 49 6e 69 74 28 64 62 2c 20 26  t && xInit(db, &
1facf 7a 45 72 72 6d 73 67 2c 20 26 73 71 6c 69 74 65  zErrmsg, &sqlite
1fad0 33 41 70 69 73 29 20 29 7b 0a 20 20 20 20 20 20  3Apis) ){.      
1fad1 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
1fad2 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 0a 20   SQLITE_ERROR,. 
1fad3 20 20 20 20 20 20 20 20 20 20 20 22 61 75 74 6f             "auto
1fad4 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 20  matic extension 
1fad5 6c 6f 61 64 69 6e 67 20 66 61 69 6c 65 64 3a 20  loading failed: 
1fad6 25 73 22 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20  %s", zErrmsg);. 
1fad7 20 20 20 20 20 67 6f 20 3d 20 30 3b 0a 20 20 20       go = 0;.   
1fad8 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
1fad9 52 52 4f 52 3b 0a 20 20 20 20 20 20 73 71 6c 69  RROR;.      sqli
1fada 74 65 33 5f 66 72 65 65 28 7a 45 72 72 6d 73 67  te3_free(zErrmsg
1fadb 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  );.    }.  }.  r
1fadc 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a  eturn rc;.}../**
1fadd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
1fade 20 6f 66 20 6c 6f 61 64 65 78 74 2e 63 20 2a 2a   of loadext.c **
1fadf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fae1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
1fae2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
1fae3 69 6e 20 66 69 6c 65 20 70 72 61 67 6d 61 2e 63  in file pragma.c
1fae4 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1fae5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fae6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
1fae7 2a 2a 20 32 30 30 33 20 41 70 72 69 6c 20 36 0a  ** 2003 April 6.
1fae8 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
1fae9 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
1faea 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
1faeb 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
1faec 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
1faed 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
1faee 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
1faef 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
1faf0 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
1faf1 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
1faf2 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
1faf3 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
1faf4 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
1faf5 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
1faf6 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
1faf7 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
1faf8 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
1faf9 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
1fafa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fafb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fafc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fafd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
1fafe 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
1faff 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74  ains code used t
1fb00 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  o implement the 
1fb01 50 52 41 47 4d 41 20 63 6f 6d 6d 61 6e 64 2e 0a  PRAGMA command..
1fb02 2a 2a 0a 2a 2a 20 24 49 64 3a 20 70 72 61 67 6d  **.** $Id: pragm
1fb03 61 2e 63 2c 76 20 31 2e 31 37 36 20 32 30 30 38  a.c,v 1.176 2008
1fb04 2f 30 34 2f 31 37 20 32 30 3a 35 39 3a 33 38 20  /04/17 20:59:38 
1fb05 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  drh Exp $.*/../*
1fb06 20 49 67 6e 6f 72 65 20 74 68 69 73 20 77 68 6f   Ignore this who
1fb07 6c 65 20 66 69 6c 65 20 69 66 20 70 72 61 67 6d  le file if pragm
1fb08 61 73 20 61 72 65 20 64 69 73 61 62 6c 65 64 0a  as are disabled.
1fb09 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  */.#if !defined(
1fb0a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 41 47  SQLITE_OMIT_PRAG
1fb0b 4d 41 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  MA) && !defined(
1fb0c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53  SQLITE_OMIT_PARS
1fb0d 45 52 29 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72  ER)../*.** Inter
1fb0e 70 72 65 74 20 74 68 65 20 67 69 76 65 6e 20 73  pret the given s
1fb0f 74 72 69 6e 67 20 61 73 20 61 20 73 61 66 65 74  tring as a safet
1fb10 79 20 6c 65 76 65 6c 2e 20 20 52 65 74 75 72 6e  y level.  Return
1fb11 20 30 20 66 6f 72 20 4f 46 46 2c 0a 2a 2a 20 31   0 for OFF,.** 1
1fb12 20 66 6f 72 20 4f 4e 20 6f 72 20 4e 4f 52 4d 41   for ON or NORMA
1fb13 4c 20 61 6e 64 20 32 20 66 6f 72 20 46 55 4c 4c  L and 2 for FULL
1fb14 2e 20 20 52 65 74 75 72 6e 20 31 20 66 6f 72 20  .  Return 1 for 
1fb15 61 6e 20 65 6d 70 74 79 20 6f 72 20 0a 2a 2a 20  an empty or .** 
1fb16 75 6e 72 65 63 6f 67 6e 69 7a 65 64 20 73 74 72  unrecognized str
1fb17 69 6e 67 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a  ing argument..**
1fb18 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68  .** Note that th
1fb19 65 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65  e values returne
1fb1a 64 20 61 72 65 20 6f 6e 65 20 6c 65 73 73 20 74  d are one less t
1fb1b 68 61 74 20 74 68 65 20 76 61 6c 75 65 73 20 74  hat the values t
1fb1c 68 61 74 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65  hat.** should be
1fb1d 20 70 61 73 73 65 64 20 69 6e 74 6f 20 73 71 6c   passed into sql
1fb1e 69 74 65 33 42 74 72 65 65 53 65 74 53 61 66 65  ite3BtreeSetSafe
1fb1f 74 79 4c 65 76 65 6c 28 29 2e 20 20 54 68 65 20  tyLevel().  The 
1fb20 69 73 20 64 6f 6e 65 0a 2a 2a 20 74 6f 20 73 75  is done.** to su
1fb21 70 70 6f 72 74 20 6c 65 67 61 63 79 20 53 51 4c  pport legacy SQL
1fb22 20 63 6f 64 65 2e 20 20 54 68 65 20 73 61 66 65   code.  The safe
1fb23 74 79 20 6c 65 76 65 6c 20 75 73 65 64 20 74 6f  ty level used to
1fb24 20 62 65 20 62 6f 6f 6c 65 61 6e 0a 2a 2a 20 61   be boolean.** a
1fb25 6e 64 20 6f 6c 64 65 72 20 73 63 72 69 70 74 73  nd older scripts
1fb26 20 6d 61 79 20 68 61 76 65 20 75 73 65 64 20 6e   may have used n
1fb27 75 6d 62 65 72 73 20 30 20 66 6f 72 20 4f 46 46  umbers 0 for OFF
1fb28 20 61 6e 64 20 31 20 66 6f 72 20 4f 4e 2e 0a 2a   and 1 for ON..*
1fb29 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
1fb2a 53 61 66 65 74 79 4c 65 76 65 6c 28 63 6f 6e 73  SafetyLevel(cons
1fb2b 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 20 20  t char *z){.    
1fb2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fb2d 20 20 20 20 20 20 20 20 20 2f 2a 20 31 32 33 34           /* 1234
1fb2e 35 36 37 38 39 20 31 32 33 34 35 36 37 38 39 20  56789 123456789 
1fb2f 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  */.  static cons
1fb30 74 20 63 68 61 72 20 7a 54 65 78 74 5b 5d 20 3d  t char zText[] =
1fb31 20 22 6f 6e 6f 66 66 61 6c 73 65 79 65 73 74 72   "onoffalseyestr
1fb32 75 65 66 75 6c 6c 22 3b 0a 20 20 73 74 61 74 69  uefull";.  stati
1fb33 63 20 63 6f 6e 73 74 20 75 38 20 69 4f 66 66 73  c const u8 iOffs
1fb34 65 74 5b 5d 20 3d 20 7b 30 2c 20 31 2c 20 32 2c  et[] = {0, 1, 2,
1fb35 20 34 2c 20 39 2c 20 31 32 2c 20 31 36 7d 3b 0a   4, 9, 12, 16};.
1fb36 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
1fb37 38 20 69 4c 65 6e 67 74 68 5b 5d 20 3d 20 7b 32  8 iLength[] = {2
1fb38 2c 20 32 2c 20 33 2c 20 35 2c 20 33 2c 20 34 2c  , 2, 3, 5, 3, 4,
1fb39 20 34 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f   4};.  static co
1fb3a 6e 73 74 20 75 38 20 69 56 61 6c 75 65 5b 5d 20  nst u8 iValue[] 
1fb3b 3d 20 20 7b 31 2c 20 30 2c 20 30 2c 20 30 2c 20  =  {1, 0, 0, 0, 
1fb3c 31 2c 20 31 2c 20 32 7d 3b 0a 20 20 69 6e 74 20  1, 1, 2};.  int 
1fb3d 69 2c 20 6e 3b 0a 20 20 69 66 28 20 69 73 64 69  i, n;.  if( isdi
1fb3e 67 69 74 28 2a 7a 29 20 29 7b 0a 20 20 20 20 72  git(*z) ){.    r
1fb3f 65 74 75 72 6e 20 61 74 6f 69 28 7a 29 3b 0a 20  eturn atoi(z);. 
1fb40 20 7d 0a 20 20 6e 20 3d 20 73 74 72 6c 65 6e 28   }.  n = strlen(
1fb41 7a 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  z);.  for(i=0; i
1fb42 3c 73 69 7a 65 6f 66 28 69 4c 65 6e 67 74 68 29  <sizeof(iLength)
1fb43 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
1fb44 69 4c 65 6e 67 74 68 5b 69 5d 3d 3d 6e 20 26 26  iLength[i]==n &&
1fb45 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
1fb46 28 26 7a 54 65 78 74 5b 69 4f 66 66 73 65 74 5b  (&zText[iOffset[
1fb47 69 5d 5d 2c 7a 2c 6e 29 3d 3d 30 20 29 7b 0a 20  i]],z,n)==0 ){. 
1fb48 20 20 20 20 20 72 65 74 75 72 6e 20 69 56 61 6c       return iVal
1fb49 75 65 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d  ue[i];.    }.  }
1fb4a 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
1fb4b 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20  /*.** Interpret 
1fb4c 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67  the given string
1fb4d 20 61 73 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61   as a boolean va
1fb4e 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  lue..*/.static i
1fb4f 6e 74 20 67 65 74 42 6f 6f 6c 65 61 6e 28 63 6f  nt getBoolean(co
1fb50 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20  nst char *z){.  
1fb51 72 65 74 75 72 6e 20 67 65 74 53 61 66 65 74 79  return getSafety
1fb52 4c 65 76 65 6c 28 7a 29 26 31 3b 0a 7d 0a 0a 2f  Level(z)&1;.}../
1fb53 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74  *.** Interpret t
1fb54 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20  he given string 
1fb55 61 73 20 61 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64  as a locking mod
1fb56 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74  e value..*/.stat
1fb57 69 63 20 69 6e 74 20 67 65 74 4c 6f 63 6b 69 6e  ic int getLockin
1fb58 67 4d 6f 64 65 28 63 6f 6e 73 74 20 63 68 61 72  gMode(const char
1fb59 20 2a 7a 29 7b 0a 20 20 69 66 28 20 7a 20 29 7b   *z){.  if( z ){
1fb5a 0a 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69  .    if( 0==sqli
1fb5b 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 65  te3StrICmp(z, "e
1fb5c 78 63 6c 75 73 69 76 65 22 29 20 29 20 72 65 74  xclusive") ) ret
1fb5d 75 72 6e 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  urn PAGER_LOCKIN
1fb5e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3b  GMODE_EXCLUSIVE;
1fb5f 0a 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69  .    if( 0==sqli
1fb60 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 6e  te3StrICmp(z, "n
1fb61 6f 72 6d 61 6c 22 29 20 29 20 72 65 74 75 72 6e  ormal") ) return
1fb62 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f   PAGER_LOCKINGMO
1fb63 44 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 7d 0a 20  DE_NORMAL;.  }. 
1fb64 20 72 65 74 75 72 6e 20 50 41 47 45 52 5f 4c 4f   return PAGER_LO
1fb65 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 3b  CKINGMODE_QUERY;
1fb66 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
1fb67 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
1fb68 55 4d 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70 72  UM./*.** Interpr
1fb69 65 74 20 74 68 65 20 67 69 76 65 6e 20 73 74 72  et the given str
1fb6a 69 6e 67 20 61 73 20 61 6e 20 61 75 74 6f 2d 76  ing as an auto-v
1fb6b 61 63 75 75 6d 20 6d 6f 64 65 20 76 61 6c 75 65  acuum mode value
1fb6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ..**.** The foll
1fb6d 6f 77 69 6e 67 20 73 74 72 69 6e 67 73 2c 20 22  owing strings, "
1fb6e 6e 6f 6e 65 22 2c 20 22 66 75 6c 6c 22 20 61 6e  none", "full" an
1fb6f 64 20 22 69 6e 63 72 65 6d 65 6e 74 61 6c 22 20  d "incremental" 
1fb70 61 72 65 20 0a 2a 2a 20 61 63 63 65 70 74 61 62  are .** acceptab
1fb71 6c 65 2c 20 61 73 20 61 72 65 20 74 68 65 69 72  le, as are their
1fb72 20 6e 75 6d 65 72 69 63 20 65 71 75 69 76 61 6c   numeric equival
1fb73 65 6e 74 73 3a 20 30 2c 20 31 20 61 6e 64 20 32  ents: 0, 1 and 2
1fb74 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a   respectively..*
1fb75 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
1fb76 41 75 74 6f 56 61 63 75 75 6d 28 63 6f 6e 73 74  AutoVacuum(const
1fb77 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74   char *z){.  int
1fb78 20 69 3b 0a 20 20 69 66 28 20 30 3d 3d 73 71 6c   i;.  if( 0==sql
1fb79 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22  ite3StrICmp(z, "
1fb7a 6e 6f 6e 65 22 29 20 29 20 72 65 74 75 72 6e 20  none") ) return 
1fb7b 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d  BTREE_AUTOVACUUM
1fb7c 5f 4e 4f 4e 45 3b 0a 20 20 69 66 28 20 30 3d 3d  _NONE;.  if( 0==
1fb7d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
1fb7e 2c 20 22 66 75 6c 6c 22 29 20 29 20 72 65 74 75  , "full") ) retu
1fb7f 72 6e 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43  rn BTREE_AUTOVAC
1fb80 55 55 4d 5f 46 55 4c 4c 3b 0a 20 20 69 66 28 20  UUM_FULL;.  if( 
1fb81 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d  0==sqlite3StrICm
1fb82 70 28 7a 2c 20 22 69 6e 63 72 65 6d 65 6e 74 61  p(z, "incrementa
1fb83 6c 22 29 20 29 20 72 65 74 75 72 6e 20 42 54 52  l") ) return BTR
1fb84 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e  EE_AUTOVACUUM_IN
1fb85 43 52 3b 0a 20 20 69 20 3d 20 61 74 6f 69 28 7a  CR;.  i = atoi(z
1fb86 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 69 3e  );.  return ((i>
1fb87 3d 30 26 26 69 3c 3d 32 29 3f 69 3a 30 29 3b 0a  =0&&i<=2)?i:0);.
1fb88 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66 6e 64  }.#endif /* ifnd
1fb89 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1fb8a 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 0a 23 69  UTOVACUUM */..#i
1fb8b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1fb8c 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a  T_PAGER_PRAGMAS.
1fb8d 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20  /*.** Interpret 
1fb8e 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67  the given string
1fb8f 20 61 73 20 61 20 74 65 6d 70 20 64 62 20 6c 6f   as a temp db lo
1fb90 63 61 74 69 6f 6e 2e 20 52 65 74 75 72 6e 20 31  cation. Return 1
1fb91 20 66 6f 72 20 66 69 6c 65 0a 2a 2a 20 62 61 63   for file.** bac
1fb92 6b 65 64 20 74 65 6d 70 6f 72 61 72 79 20 64 61  ked temporary da
1fb93 74 61 62 61 73 65 73 2c 20 32 20 66 6f 72 20 74  tabases, 2 for t
1fb94 68 65 20 52 65 64 2d 42 6c 61 63 6b 20 74 72 65  he Red-Black tre
1fb95 65 20 69 6e 20 6d 65 6d 6f 72 79 20 64 61 74 61  e in memory data
1fb96 62 61 73 65 0a 2a 2a 20 61 6e 64 20 30 20 74 6f  base.** and 0 to
1fb97 20 75 73 65 20 74 68 65 20 63 6f 6d 70 69 6c 65   use the compile
1fb98 2d 74 69 6d 65 20 64 65 66 61 75 6c 74 2e 0a 2a  -time default..*
1fb99 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
1fb9a 54 65 6d 70 53 74 6f 72 65 28 63 6f 6e 73 74 20  TempStore(const 
1fb9b 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 66 28 20  char *z){.  if( 
1fb9c 7a 5b 30 5d 3e 3d 27 30 27 20 26 26 20 7a 5b 30  z[0]>='0' && z[0
1fb9d 5d 3c 3d 27 32 27 20 29 7b 0a 20 20 20 20 72 65  ]<='2' ){.    re
1fb9e 74 75 72 6e 20 7a 5b 30 5d 20 2d 20 27 30 27 3b  turn z[0] - '0';
1fb9f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c  .  }else if( sql
1fba0 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22  ite3StrICmp(z, "
1fba1 66 69 6c 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20  file")==0 ){.   
1fba2 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c   return 1;.  }el
1fba3 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  se if( sqlite3St
1fba4 72 49 43 6d 70 28 7a 2c 20 22 6d 65 6d 6f 72 79  rICmp(z, "memory
1fba5 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ")==0 ){.    ret
1fba6 75 72 6e 20 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a  urn 2;.  }else{.
1fba7 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
1fba8 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  }.}.#endif /* SQ
1fba9 4c 49 54 45 5f 50 41 47 45 52 5f 50 52 41 47 4d  LITE_PAGER_PRAGM
1fbaa 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  AS */..#ifndef S
1fbab 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52  QLITE_OMIT_PAGER
1fbac 5f 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 49  _PRAGMAS./*.** I
1fbad 6e 76 61 6c 69 64 61 74 65 20 74 65 6d 70 20 73  nvalidate temp s
1fbae 74 6f 72 61 67 65 2c 20 65 69 74 68 65 72 20 77  torage, either w
1fbaf 68 65 6e 20 74 68 65 20 74 65 6d 70 20 73 74 6f  hen the temp sto
1fbb0 72 61 67 65 20 69 73 20 63 68 61 6e 67 65 64 0a  rage is changed.
1fbb1 2a 2a 20 66 72 6f 6d 20 64 65 66 61 75 6c 74 2c  ** from default,
1fbb2 20 6f 72 20 77 68 65 6e 20 27 66 69 6c 65 27 20   or when 'file' 
1fbb3 61 6e 64 20 74 68 65 20 74 65 6d 70 5f 73 74 6f  and the temp_sto
1fbb4 72 65 5f 64 69 72 65 63 74 6f 72 79 20 68 61 73  re_directory has
1fbb5 20 63 68 61 6e 67 65 64 0a 2a 2f 0a 73 74 61 74   changed.*/.stat
1fbb6 69 63 20 69 6e 74 20 69 6e 76 61 6c 69 64 61 74  ic int invalidat
1fbb7 65 54 65 6d 70 53 74 6f 72 61 67 65 28 50 61 72  eTempStorage(Par
1fbb8 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73  se *pParse){.  s
1fbb9 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
1fbba 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 64  rse->db;.  if( d
1fbbb 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 21 3d 30  b->aDb[1].pBt!=0
1fbbc 20 29 7b 0a 20 20 20 20 69 66 28 20 21 64 62 2d   ){.    if( !db-
1fbbd 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20  >autoCommit ){. 
1fbbe 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1fbbf 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 65  rMsg(pParse, "te
1fbc0 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65 20  mporary storage 
1fbc1 63 61 6e 6e 6f 74 20 62 65 20 63 68 61 6e 67 65  cannot be change
1fbc2 64 20 22 0a 20 20 20 20 20 20 20 20 22 66 72 6f  d ".        "fro
1fbc3 6d 20 77 69 74 68 69 6e 20 61 20 74 72 61 6e 73  m within a trans
1fbc4 61 63 74 69 6f 6e 22 29 3b 0a 20 20 20 20 20 20  action");.      
1fbc5 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
1fbc6 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ROR;.    }.    s
1fbc7 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65  qlite3BtreeClose
1fbc8 28 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29  (db->aDb[1].pBt)
1fbc9 3b 0a 20 20 20 20 64 62 2d 3e 61 44 62 5b 31 5d  ;.    db->aDb[1]
1fbca 2e 70 42 74 20 3d 20 30 3b 0a 20 20 20 20 73 71  .pBt = 0;.    sq
1fbcb 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e  lite3ResetIntern
1fbcc 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b  alSchema(db, 0);
1fbcd 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
1fbce 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69  LITE_OK;.}.#endi
1fbcf 66 20 2f 2a 20 53 51 4c 49 54 45 5f 50 41 47 45  f /* SQLITE_PAGE
1fbd0 52 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69  R_PRAGMAS */..#i
1fbd1 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1fbd2 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a  T_PAGER_PRAGMAS.
1fbd3 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 54 45 4d  /*.** If the TEM
1fbd4 50 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70  P database is op
1fbd5 65 6e 2c 20 63 6c 6f 73 65 20 69 74 20 61 6e 64  en, close it and
1fbd6 20 6d 61 72 6b 20 74 68 65 20 64 61 74 61 62 61   mark the databa
1fbd7 73 65 20 73 63 68 65 6d 61 0a 2a 2a 20 61 73 20  se schema.** as 
1fbd8 6e 65 65 64 69 6e 67 20 72 65 6c 6f 61 64 69 6e  needing reloadin
1fbd9 67 2e 20 20 54 68 69 73 20 6d 75 73 74 20 62 65  g.  This must be
1fbda 20 64 6f 6e 65 20 77 68 65 6e 20 75 73 69 6e 67   done when using
1fbdb 20 74 68 65 20 54 45 4d 50 5f 53 54 4f 52 45 0a   the TEMP_STORE.
1fbdc 2a 2a 20 6f 72 20 44 45 46 41 55 4c 54 5f 54 45  ** or DEFAULT_TE
1fbdd 4d 50 5f 53 54 4f 52 45 20 70 72 61 67 6d 61 73  MP_STORE pragmas
1fbde 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1fbdf 63 68 61 6e 67 65 54 65 6d 70 53 74 6f 72 61 67  changeTempStorag
1fbe0 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
1fbe1 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74   const char *zSt
1fbe2 6f 72 61 67 65 54 79 70 65 29 7b 0a 20 20 69 6e  orageType){.  in
1fbe3 74 20 74 73 20 3d 20 67 65 74 54 65 6d 70 53 74  t ts = getTempSt
1fbe4 6f 72 65 28 7a 53 74 6f 72 61 67 65 54 79 70 65  ore(zStorageType
1fbe5 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  );.  sqlite3 *db
1fbe6 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
1fbe7 20 69 66 28 20 64 62 2d 3e 74 65 6d 70 5f 73 74   if( db->temp_st
1fbe8 6f 72 65 3d 3d 74 73 20 29 20 72 65 74 75 72 6e  ore==ts ) return
1fbe9 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
1fbea 28 20 69 6e 76 61 6c 69 64 61 74 65 54 65 6d 70  ( invalidateTemp
1fbeb 53 74 6f 72 61 67 65 28 20 70 50 61 72 73 65 20  Storage( pParse 
1fbec 29 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 20 29  ) != SQLITE_OK )
1fbed 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
1fbee 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20  ITE_ERROR;.  }. 
1fbef 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 20   db->temp_store 
1fbf0 3d 20 74 73 3b 0a 20 20 72 65 74 75 72 6e 20 53  = ts;.  return S
1fbf1 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64  QLITE_OK;.}.#end
1fbf2 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 50 41 47  if /* SQLITE_PAG
1fbf3 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 2f  ER_PRAGMAS */../
1fbf4 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
1fbf5 64 65 20 74 6f 20 72 65 74 75 72 6e 20 61 20 73  de to return a s
1fbf6 69 6e 67 6c 65 20 69 6e 74 65 67 65 72 20 76 61  ingle integer va
1fbf7 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  lue..*/.static v
1fbf8 6f 69 64 20 72 65 74 75 72 6e 53 69 6e 67 6c 65  oid returnSingle
1fbf9 49 6e 74 28 50 61 72 73 65 20 2a 70 50 61 72 73  Int(Parse *pPars
1fbfa 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  e, const char *z
1fbfb 4c 61 62 65 6c 2c 20 69 6e 74 20 76 61 6c 75 65  Label, int value
1fbfc 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73  ){.  Vdbe *v = s
1fbfd 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
1fbfe 61 72 73 65 29 3b 0a 20 20 69 6e 74 20 6d 65 6d  arse);.  int mem
1fbff 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
1fc00 6d 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  m;.  sqlite3Vdbe
1fc01 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
1fc02 65 67 65 72 2c 20 76 61 6c 75 65 2c 20 6d 65 6d  eger, value, mem
1fc03 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  );.  if( pParse-
1fc04 3e 65 78 70 6c 61 69 6e 3d 3d 30 20 29 7b 0a 20  >explain==0 ){. 
1fc05 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
1fc06 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a  tNumCols(v, 1);.
1fc07 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
1fc08 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
1fc09 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4c  COLNAME_NAME, zL
1fc0a 61 62 65 6c 2c 20 50 34 5f 53 54 41 54 49 43 29  abel, P4_STATIC)
1fc0b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56  ;.  }.  sqlite3V
1fc0c 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1fc0d 52 65 73 75 6c 74 52 6f 77 2c 20 6d 65 6d 2c 20  ResultRow, mem, 
1fc0e 31 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53  1);.}..#ifndef S
1fc0f 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f  QLITE_OMIT_FLAG_
1fc10 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 43 68  PRAGMAS./*.** Ch
1fc11 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 7a 52  eck to see if zR
1fc12 69 67 68 74 20 61 6e 64 20 7a 4c 65 66 74 20 72  ight and zLeft r
1fc13 65 66 65 72 20 74 6f 20 61 20 70 72 61 67 6d 61  efer to a pragma
1fc14 20 74 68 61 74 20 71 75 65 72 69 65 73 0a 2a 2a   that queries.**
1fc15 20 6f 72 20 63 68 61 6e 67 65 73 20 6f 6e 65 20   or changes one 
1fc16 6f 66 20 74 68 65 20 66 6c 61 67 73 20 69 6e 20  of the flags in 
1fc17 64 62 2d 3e 66 6c 61 67 73 2e 20 20 52 65 74 75  db->flags.  Retu
1fc18 72 6e 20 31 20 69 66 20 73 6f 20 61 6e 64 20 30  rn 1 if so and 0
1fc19 20 69 66 20 6e 6f 74 2e 0a 2a 2a 20 41 6c 73 6f   if not..** Also
1fc1a 2c 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  , implement the 
1fc1b 70 72 61 67 6d 61 2e 0a 2a 2f 0a 73 74 61 74 69  pragma..*/.stati
1fc1c 63 20 69 6e 74 20 66 6c 61 67 50 72 61 67 6d 61  c int flagPragma
1fc1d 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1fc1e 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66  const char *zLef
1fc1f 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  t, const char *z
1fc20 52 69 67 68 74 29 7b 0a 20 20 73 74 61 74 69 63  Right){.  static
1fc21 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73 50   const struct sP
1fc22 72 61 67 6d 61 54 79 70 65 20 7b 0a 20 20 20 20  ragmaType {.    
1fc23 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
1fc24 65 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74  e;  /* Name of t
1fc25 68 65 20 70 72 61 67 6d 61 20 2a 2f 0a 20 20 20  he pragma */.   
1fc26 20 69 6e 74 20 6d 61 73 6b 3b 20 20 20 20 20 20   int mask;      
1fc27 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 66 6f 72       /* Mask for
1fc28 20 74 68 65 20 64 62 2d 3e 66 6c 61 67 73 20 76   the db->flags v
1fc29 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 61 50 72 61  alue */.  } aPra
1fc2a 67 6d 61 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20  gma[] = {.    { 
1fc2b 22 66 75 6c 6c 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d  "full_column_nam
1fc2c 65 73 22 2c 20 20 20 20 20 20 20 20 53 51 4c 49  es",        SQLI
1fc2d 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 20  TE_FullColNames 
1fc2e 20 7d 2c 0a 20 20 20 20 7b 20 22 73 68 6f 72 74   },.    { "short
1fc2f 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 73 22 2c 20  _column_names", 
1fc30 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 68 6f        SQLITE_Sho
1fc31 72 74 43 6f 6c 4e 61 6d 65 73 20 7d 2c 0a 20 20  rtColNames },.  
1fc32 20 20 7b 20 22 63 6f 75 6e 74 5f 63 68 61 6e 67    { "count_chang
1fc33 65 73 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  es",            
1fc34 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73  SQLITE_CountRows
1fc35 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 65       },.    { "e
1fc36 6d 70 74 79 5f 72 65 73 75 6c 74 5f 63 61 6c 6c  mpty_result_call
1fc37 62 61 63 6b 73 22 2c 20 20 20 53 51 4c 49 54 45  backs",   SQLITE
1fc38 5f 4e 75 6c 6c 43 61 6c 6c 62 61 63 6b 20 20 7d  _NullCallback  }
1fc39 2c 0a 20 20 20 20 7b 20 22 6c 65 67 61 63 79 5f  ,.    { "legacy_
1fc3a 66 69 6c 65 5f 66 6f 72 6d 61 74 22 2c 20 20 20  file_format",   
1fc3b 20 20 20 20 53 51 4c 49 54 45 5f 4c 65 67 61 63      SQLITE_Legac
1fc3c 79 46 69 6c 65 46 6d 74 20 7d 2c 0a 20 20 20 20  yFileFmt },.    
1fc3d 7b 20 22 66 75 6c 6c 66 73 79 6e 63 22 2c 20 20  { "fullfsync",  
1fc3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
1fc3f 4c 49 54 45 5f 46 75 6c 6c 46 53 79 6e 63 20 20  LITE_FullFSync  
1fc40 20 20 20 7d 2c 0a 23 69 66 64 65 66 20 53 51 4c     },.#ifdef SQL
1fc41 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 7b 20  ITE_DEBUG.    { 
1fc42 22 73 71 6c 5f 74 72 61 63 65 22 2c 20 20 20 20  "sql_trace",    
1fc43 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
1fc44 54 45 5f 53 71 6c 54 72 61 63 65 20 20 20 20 20  TE_SqlTrace     
1fc45 20 7d 2c 0a 20 20 20 20 7b 20 22 76 64 62 65 5f   },.    { "vdbe_
1fc46 6c 69 73 74 69 6e 67 22 2c 20 20 20 20 20 20 20  listing",       
1fc47 20 20 20 20 20 20 53 51 4c 49 54 45 5f 56 64 62        SQLITE_Vdb
1fc48 65 4c 69 73 74 69 6e 67 20 20 20 7d 2c 0a 20 20  eListing   },.  
1fc49 20 20 7b 20 22 76 64 62 65 5f 74 72 61 63 65 22    { "vdbe_trace"
1fc4a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1fc4b 53 51 4c 49 54 45 5f 56 64 62 65 54 72 61 63 65  SQLITE_VdbeTrace
1fc4c 20 20 20 20 20 7d 2c 0a 23 65 6e 64 69 66 0a 23       },.#endif.#
1fc4d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1fc4e 49 54 5f 43 48 45 43 4b 0a 20 20 20 20 7b 20 22  IT_CHECK.    { "
1fc4f 69 67 6e 6f 72 65 5f 63 68 65 63 6b 5f 63 6f 6e  ignore_check_con
1fc50 73 74 72 61 69 6e 74 73 22 2c 20 53 51 4c 49 54  straints", SQLIT
1fc51 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73 20 20  E_IgnoreChecks  
1fc52 7d 2c 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f 2a  },.#endif.    /*
1fc53 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69   The following i
1fc54 73 20 56 45 52 59 20 65 78 70 65 72 69 6d 65 6e  s VERY experimen
1fc55 74 61 6c 20 2a 2f 0a 20 20 20 20 7b 20 22 77 72  tal */.    { "wr
1fc56 69 74 61 62 6c 65 5f 73 63 68 65 6d 61 22 2c 20  itable_schema", 
1fc57 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
1fc58 57 72 69 74 65 53 63 68 65 6d 61 7c 53 51 4c 49  WriteSchema|SQLI
1fc59 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 20  TE_RecoveryMode 
1fc5a 7d 2c 0a 20 20 20 20 7b 20 22 6f 6d 69 74 5f 72  },.    { "omit_r
1fc5b 65 61 64 6c 6f 63 6b 22 2c 20 20 20 20 20 20 20  eadlock",       
1fc5c 20 20 20 20 20 53 51 4c 49 54 45 5f 4e 6f 52 65       SQLITE_NoRe
1fc5d 61 64 6c 6f 63 6b 20 20 20 20 7d 2c 0a 0a 20 20  adlock    },..  
1fc5e 20 20 2f 2a 20 54 4f 44 4f 3a 20 4d 61 79 62 65    /* TODO: Maybe
1fc5f 20 69 74 20 73 68 6f 75 6c 64 6e 27 74 20 62 65   it shouldn't be
1fc60 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 63 68 61   possible to cha
1fc61 6e 67 65 20 74 68 65 20 52 65 61 64 55 6e 63 6f  nge the ReadUnco
1fc62 6d 6d 69 74 74 65 64 0a 20 20 20 20 2a 2a 20 66  mmitted.    ** f
1fc63 6c 61 67 20 69 66 20 74 68 65 72 65 20 61 72 65  lag if there are
1fc64 20 61 6e 79 20 61 63 74 69 76 65 20 73 74 61 74   any active stat
1fc65 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20 20 20 20 7b  ements. */.    {
1fc66 20 22 72 65 61 64 5f 75 6e 63 6f 6d 6d 69 74 74   "read_uncommitt
1fc67 65 64 22 2c 20 20 20 20 20 20 20 20 20 53 51 4c  ed",         SQL
1fc68 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74  ITE_ReadUncommit
1fc69 74 65 64 20 7d 2c 0a 20 20 7d 3b 0a 20 20 69 6e  ted },.  };.  in
1fc6a 74 20 69 3b 0a 20 20 63 6f 6e 73 74 20 73 74 72  t i;.  const str
1fc6b 75 63 74 20 73 50 72 61 67 6d 61 54 79 70 65 20  uct sPragmaType 
1fc6c 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70  *p;.  for(i=0, p
1fc6d 3d 61 50 72 61 67 6d 61 3b 20 69 3c 73 69 7a 65  =aPragma; i<size
1fc6e 6f 66 28 61 50 72 61 67 6d 61 29 2f 73 69 7a 65  of(aPragma)/size
1fc6f 6f 66 28 61 50 72 61 67 6d 61 5b 30 5d 29 3b 20  of(aPragma[0]); 
1fc70 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69  i++, p++){.    i
1fc71 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
1fc72 70 28 7a 4c 65 66 74 2c 20 70 2d 3e 7a 4e 61 6d  p(zLeft, p->zNam
1fc73 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  e)==0 ){.      s
1fc74 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
1fc75 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 56  rse->db;.      V
1fc76 64 62 65 20 2a 76 3b 0a 20 20 20 20 20 20 76 20  dbe *v;.      v 
1fc77 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
1fc78 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
1fc79 69 66 28 20 76 20 29 7b 0a 20 20 20 20 20 20 20  if( v ){.       
1fc7a 20 69 66 28 20 7a 52 69 67 68 74 3d 3d 30 20 29   if( zRight==0 )
1fc7b 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  {.          retu
1fc7c 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61 72  rnSingleInt(pPar
1fc7d 73 65 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 28 64  se, p->zName, (d
1fc7e 62 2d 3e 66 6c 61 67 73 20 26 20 70 2d 3e 6d 61  b->flags & p->ma
1fc7f 73 6b 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  sk)!=0 );.      
1fc80 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1fc81 20 20 20 69 66 28 20 67 65 74 42 6f 6f 6c 65 61     if( getBoolea
1fc82 6e 28 7a 52 69 67 68 74 29 20 29 7b 0a 20 20 20  n(zRight) ){.   
1fc83 20 20 20 20 20 20 20 20 20 64 62 2d 3e 66 6c 61           db->fla
1fc84 67 73 20 7c 3d 20 70 2d 3e 6d 61 73 6b 3b 0a 20  gs |= p->mask;. 
1fc85 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
1fc86 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e              db->
1fc87 66 6c 61 67 73 20 26 3d 20 7e 70 2d 3e 6d 61 73  flags &= ~p->mas
1fc88 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0a  k;.          }..
1fc89 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6e            /* Man
1fc8a 79 20 6f 66 20 74 68 65 20 66 6c 61 67 2d 70 72  y of the flag-pr
1fc8b 61 67 6d 61 73 20 6d 6f 64 69 66 79 20 74 68 65  agmas modify the
1fc8c 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20   code generated 
1fc8d 62 79 20 74 68 65 20 53 51 4c 20 0a 20 20 20 20  by the SQL .    
1fc8e 20 20 20 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65        ** compile
1fc8f 72 20 28 65 67 2e 20 63 6f 75 6e 74 5f 63 68 61  r (eg. count_cha
1fc90 6e 67 65 73 29 2e 20 53 6f 20 61 64 64 20 61 6e  nges). So add an
1fc91 20 6f 70 63 6f 64 65 20 74 6f 20 65 78 70 69 72   opcode to expir
1fc92 65 20 61 6c 6c 0a 20 20 20 20 20 20 20 20 20 20  e all.          
1fc93 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20  ** compiled SQL 
1fc94 73 74 61 74 65 6d 65 6e 74 73 20 61 66 74 65 72  statements after
1fc95 20 6d 6f 64 69 66 79 69 6e 67 20 61 20 70 72 61   modifying a pra
1fc96 67 6d 61 20 76 61 6c 75 65 2e 0a 20 20 20 20 20  gma value..     
1fc97 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
1fc98 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1fc99 4f 70 32 28 76 2c 20 4f 50 5f 45 78 70 69 72 65  Op2(v, OP_Expire
1fc9a 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  , 0, 0);.       
1fc9b 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20   }.      }..    
1fc9c 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
1fc9d 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  }.  }.  return 0
1fc9e 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
1fc9f 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50  LITE_OMIT_FLAG_P
1fca0 52 41 47 4d 41 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  RAGMAS */../*.**
1fca1 20 50 72 6f 63 65 73 73 20 61 20 70 72 61 67 6d   Process a pragm
1fca2 61 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 0a 2a  a statement.  .*
1fca3 2a 0a 2a 2a 20 50 72 61 67 6d 61 73 20 61 72 65  *.** Pragmas are
1fca4 20 6f 66 20 74 68 69 73 20 66 6f 72 6d 3a 0a 2a   of this form:.*
1fca5 2a 0a 2a 2a 20 20 20 20 20 20 50 52 41 47 4d 41  *.**      PRAGMA
1fca6 20 5b 64 61 74 61 62 61 73 65 2e 5d 69 64 20 5b   [database.]id [
1fca7 3d 20 76 61 6c 75 65 5d 0a 2a 2a 0a 2a 2a 20 54  = value].**.** T
1fca8 68 65 20 69 64 65 6e 74 69 66 69 65 72 20 6d 69  he identifier mi
1fca9 67 68 74 20 61 6c 73 6f 20 62 65 20 61 20 73 74  ght also be a st
1fcaa 72 69 6e 67 2e 20 20 54 68 65 20 76 61 6c 75 65  ring.  The value
1fcab 20 69 73 20 61 20 73 74 72 69 6e 67 2c 20 61 6e   is a string, an
1fcac 64 0a 2a 2a 20 69 64 65 6e 74 69 66 69 65 72 2c  d.** identifier,
1fcad 20 6f 72 20 61 20 6e 75 6d 62 65 72 2e 20 20 49   or a number.  I
1fcae 66 20 6d 69 6e 75 73 46 6c 61 67 20 69 73 20 74  f minusFlag is t
1fcaf 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 76 61  rue, then the va
1fcb0 6c 75 65 20 69 73 0a 2a 2a 20 61 20 6e 75 6d 62  lue is.** a numb
1fcb1 65 72 20 74 68 61 74 20 77 61 73 20 70 72 65 63  er that was prec
1fcb2 65 64 65 64 20 62 79 20 61 20 6d 69 6e 75 73 20  eded by a minus 
1fcb3 73 69 67 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  sign..**.** If t
1fcb4 68 65 20 6c 65 66 74 20 73 69 64 65 20 69 73 20  he left side is 
1fcb5 22 64 61 74 61 62 61 73 65 2e 69 64 22 20 74 68  "database.id" th
1fcb6 65 6e 20 70 49 64 31 20 69 73 20 74 68 65 20 64  en pId1 is the d
1fcb7 61 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a 20  atabase name.** 
1fcb8 61 6e 64 20 70 49 64 32 20 69 73 20 74 68 65 20  and pId2 is the 
1fcb9 69 64 2e 20 20 49 66 20 74 68 65 20 6c 65 66 74  id.  If the left
1fcba 20 73 69 64 65 20 69 73 20 6a 75 73 74 20 22 69   side is just "i
1fcbb 64 22 20 74 68 65 6e 20 70 49 64 31 20 69 73 20  d" then pId1 is 
1fcbc 74 68 65 0a 2a 2a 20 69 64 20 61 6e 64 20 70 49  the.** id and pI
1fcbd 64 32 20 69 73 20 61 6e 79 20 65 6d 70 74 79 20  d2 is any empty 
1fcbe 73 74 72 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54  string..*/.SQLIT
1fcbf 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1fcc0 71 6c 69 74 65 33 50 72 61 67 6d 61 28 0a 20 20  qlite3Pragma(.  
1fcc1 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a  Parse *pParse, .
1fcc2 20 20 54 6f 6b 65 6e 20 2a 70 49 64 31 2c 20 20    Token *pId1,  
1fcc3 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 70        /* First p
1fcc4 61 72 74 20 6f 66 20 5b 64 61 74 61 62 61 73 65  art of [database
1fcc5 2e 5d 69 64 20 66 69 65 6c 64 20 2a 2f 0a 20 20  .]id field */.  
1fcc6 54 6f 6b 65 6e 20 2a 70 49 64 32 2c 20 20 20 20  Token *pId2,    
1fcc7 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61      /* Second pa
1fcc8 72 74 20 6f 66 20 5b 64 61 74 61 62 61 73 65 2e  rt of [database.
1fcc9 5d 69 64 20 66 69 65 6c 64 2c 20 6f 72 20 4e 55  ]id field, or NU
1fcca 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  LL */.  Token *p
1fccb 56 61 6c 75 65 2c 20 20 20 20 20 20 2f 2a 20 54  Value,      /* T
1fccc 6f 6b 65 6e 20 66 6f 72 20 3c 76 61 6c 75 65 3e  oken for <value>
1fccd 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69  , or NULL */.  i
1fcce 6e 74 20 6d 69 6e 75 73 46 6c 61 67 20 20 20 20  nt minusFlag    
1fccf 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20     /* True if a 
1fcd0 27 2d 27 20 73 69 67 6e 20 70 72 65 63 65 64 65  '-' sign precede
1fcd1 64 20 3c 76 61 6c 75 65 3e 20 2a 2f 0a 29 7b 0a  d <value> */.){.
1fcd2 20 20 63 68 61 72 20 2a 7a 4c 65 66 74 20 3d 20    char *zLeft = 
1fcd3 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6c 2d  0;       /* Nul-
1fcd4 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38  terminated UTF-8
1fcd5 20 73 74 72 69 6e 67 20 3c 69 64 3e 20 2a 2f 0a   string <id> */.
1fcd6 20 20 63 68 61 72 20 2a 7a 52 69 67 68 74 20 3d    char *zRight =
1fcd7 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6c 2d   0;      /* Nul-
1fcd8 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38  terminated UTF-8
1fcd9 20 73 74 72 69 6e 67 20 3c 76 61 6c 75 65 3e 2c   string <value>,
1fcda 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f   or NULL */.  co
1fcdb 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20  nst char *zDb = 
1fcdc 30 3b 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61  0;   /* The data
1fcdd 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 54  base name */.  T
1fcde 6f 6b 65 6e 20 2a 70 49 64 3b 20 20 20 20 20 20  oken *pId;      
1fcdf 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
1fce0 20 74 6f 20 3c 69 64 3e 20 74 6f 6b 65 6e 20 2a   to <id> token *
1fce1 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  /.  int iDb;    
1fce2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
1fce3 74 61 62 61 73 65 20 69 6e 64 65 78 20 66 6f 72  tabase index for
1fce4 20 3c 64 61 74 61 62 61 73 65 3e 20 2a 2f 0a 20   <database> */. 
1fce5 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
1fce6 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 44 62 20  Parse->db;.  Db 
1fce7 2a 70 44 62 3b 0a 20 20 56 64 62 65 20 2a 76 20  *pDb;.  Vdbe *v 
1fce8 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20  = pParse->pVdbe 
1fce9 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 72 65  = sqlite3VdbeCre
1fcea 61 74 65 28 64 62 29 3b 0a 20 20 69 66 28 20 76  ate(db);.  if( v
1fceb 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
1fcec 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 32  pParse->nMem = 2
1fced 3b 0a 0a 20 20 2f 2a 20 49 6e 74 65 72 70 72 65  ;..  /* Interpre
1fcee 74 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 2e  t the [database.
1fcef 5d 20 70 61 72 74 20 6f 66 20 74 68 65 20 70 72  ] part of the pr
1fcf0 61 67 6d 61 20 73 74 61 74 65 6d 65 6e 74 2e 20  agma statement. 
1fcf1 69 44 62 20 69 73 20 74 68 65 0a 20 20 2a 2a 20  iDb is the.  ** 
1fcf2 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74  index of the dat
1fcf3 61 62 61 73 65 20 74 68 69 73 20 70 72 61 67 6d  abase this pragm
1fcf4 61 20 69 73 20 62 65 69 6e 67 20 61 70 70 6c 69  a is being appli
1fcf5 65 64 20 74 6f 20 69 6e 20 64 62 2e 61 44 62 5b  ed to in db.aDb[
1fcf6 5d 2e 20 2a 2f 0a 20 20 69 44 62 20 3d 20 73 71  ]. */.  iDb = sq
1fcf7 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65  lite3TwoPartName
1fcf8 28 70 50 61 72 73 65 2c 20 70 49 64 31 2c 20 70  (pParse, pId1, p
1fcf9 49 64 32 2c 20 26 70 49 64 29 3b 0a 20 20 69 66  Id2, &pId);.  if
1fcfa 28 20 69 44 62 3c 30 20 29 20 72 65 74 75 72 6e  ( iDb<0 ) return
1fcfb 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61  ;.  pDb = &db->a
1fcfc 44 62 5b 69 44 62 5d 3b 0a 0a 20 20 2f 2a 20 49  Db[iDb];..  /* I
1fcfd 66 20 74 68 65 20 74 65 6d 70 20 64 61 74 61 62  f the temp datab
1fcfe 61 73 65 20 68 61 73 20 62 65 65 6e 20 65 78 70  ase has been exp
1fcff 6c 69 63 69 74 6c 79 20 6e 61 6d 65 64 20 61 73  licitly named as
1fd00 20 70 61 72 74 20 6f 66 20 74 68 65 20 0a 20 20   part of the .  
1fd01 2a 2a 20 70 72 61 67 6d 61 2c 20 6d 61 6b 65 20  ** pragma, make 
1fd02 73 75 72 65 20 69 74 20 69 73 20 6f 70 65 6e 2e  sure it is open.
1fd03 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 44 62   .  */.  if( iDb
1fd04 3d 3d 31 20 26 26 20 73 71 6c 69 74 65 33 4f 70  ==1 && sqlite3Op
1fd05 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65 28 70  enTempDatabase(p
1fd06 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 72 65  Parse) ){.    re
1fd07 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 7a 4c 65  turn;.  }..  zLe
1fd08 66 74 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65  ft = sqlite3Name
1fd09 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 49  FromToken(db, pI
1fd0a 64 29 3b 0a 20 20 69 66 28 20 21 7a 4c 65 66 74  d);.  if( !zLeft
1fd0b 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
1fd0c 20 6d 69 6e 75 73 46 6c 61 67 20 29 7b 0a 20 20   minusFlag ){.  
1fd0d 20 20 7a 52 69 67 68 74 20 3d 20 73 71 6c 69 74    zRight = sqlit
1fd0e 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 2d  e3MPrintf(db, "-
1fd0f 25 54 22 2c 20 70 56 61 6c 75 65 29 3b 0a 20 20  %T", pValue);.  
1fd10 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 52 69 67 68  }else{.    zRigh
1fd11 74 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  t = sqlite3NameF
1fd12 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 56 61  romToken(db, pVa
1fd13 6c 75 65 29 3b 0a 20 20 7d 0a 0a 20 20 7a 44 62  lue);.  }..  zDb
1fd14 20 3d 20 28 28 69 44 62 3e 30 29 3f 70 44 62 2d   = ((iDb>0)?pDb-
1fd15 3e 7a 4e 61 6d 65 3a 30 29 3b 0a 20 20 69 66 28  >zName:0);.  if(
1fd16 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
1fd17 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
1fd18 5f 50 52 41 47 4d 41 2c 20 7a 4c 65 66 74 2c 20  _PRAGMA, zLeft, 
1fd19 7a 52 69 67 68 74 2c 20 7a 44 62 29 20 29 7b 0a  zRight, zDb) ){.
1fd1a 20 20 20 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f      goto pragma_
1fd1b 6f 75 74 3b 0a 20 20 7d 0a 20 0a 23 69 66 6e 64  out;.  }. .#ifnd
1fd1c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  ef SQLITE_OMIT_P
1fd1d 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 20 20 2f  AGER_PRAGMAS.  /
1fd1e 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  *.  **  PRAGMA [
1fd1f 64 61 74 61 62 61 73 65 2e 5d 64 65 66 61 75 6c  database.]defaul
1fd20 74 5f 63 61 63 68 65 5f 73 69 7a 65 0a 20 20 2a  t_cache_size.  *
1fd21 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62  *  PRAGMA [datab
1fd22 61 73 65 2e 5d 64 65 66 61 75 6c 74 5f 63 61 63  ase.]default_cac
1fd23 68 65 5f 73 69 7a 65 3d 4e 0a 20 20 2a 2a 0a 20  he_size=N.  **. 
1fd24 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 66 6f   ** The first fo
1fd25 72 6d 20 72 65 70 6f 72 74 73 20 74 68 65 20 63  rm reports the c
1fd26 75 72 72 65 6e 74 20 70 65 72 73 69 73 74 65 6e  urrent persisten
1fd27 74 20 73 65 74 74 69 6e 67 20 66 6f 72 20 74 68  t setting for th
1fd28 65 0a 20 20 2a 2a 20 70 61 67 65 20 63 61 63 68  e.  ** page cach
1fd29 65 20 73 69 7a 65 2e 20 20 54 68 65 20 76 61 6c  e size.  The val
1fd2a 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74  ue returned is t
1fd2b 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
1fd2c 72 20 6f 66 0a 20 20 2a 2a 20 70 61 67 65 73 20  r of.  ** pages 
1fd2d 69 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68  in the page cach
1fd2e 65 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 66  e.  The second f
1fd2f 6f 72 6d 20 73 65 74 73 20 62 6f 74 68 20 74 68  orm sets both th
1fd30 65 20 63 75 72 72 65 6e 74 0a 20 20 2a 2a 20 70  e current.  ** p
1fd31 61 67 65 20 63 61 63 68 65 20 73 69 7a 65 20 76  age cache size v
1fd32 61 6c 75 65 20 61 6e 64 20 74 68 65 20 70 65 72  alue and the per
1fd33 73 69 73 74 65 6e 74 20 70 61 67 65 20 63 61 63  sistent page cac
1fd34 68 65 20 73 69 7a 65 20 76 61 6c 75 65 0a 20 20  he size value.  
1fd35 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  ** stored in the
1fd36 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
1fd37 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 64 65    **.  ** The de
1fd38 66 61 75 6c 74 20 63 61 63 68 65 20 73 69 7a 65  fault cache size
1fd39 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 6d 65   is stored in me
1fd3a 74 61 2d 76 61 6c 75 65 20 32 20 6f 66 20 70 61  ta-value 2 of pa
1fd3b 67 65 20 31 20 6f 66 20 74 68 65 0a 20 20 2a 2a  ge 1 of the.  **
1fd3c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
1fd3d 20 54 68 65 20 63 61 63 68 65 20 73 69 7a 65 20   The cache size 
1fd3e 69 73 20 61 63 74 75 61 6c 6c 79 20 74 68 65 20  is actually the 
1fd3f 61 62 73 6f 6c 75 74 65 20 76 61 6c 75 65 20 6f  absolute value o
1fd40 66 0a 20 20 2a 2a 20 74 68 69 73 20 6d 65 6d 6f  f.  ** this memo
1fd41 72 79 20 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68  ry location.  Th
1fd42 65 20 73 69 67 6e 20 6f 66 20 6d 65 74 61 2d 76  e sign of meta-v
1fd43 61 6c 75 65 20 32 20 64 65 74 65 72 6d 69 6e 65  alue 2 determine
1fd44 73 20 74 68 65 0a 20 20 2a 2a 20 73 79 6e 63 68  s the.  ** synch
1fd45 72 6f 6e 6f 75 73 20 73 65 74 74 69 6e 67 2e 20  ronous setting. 
1fd46 20 41 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75   A negative valu
1fd47 65 20 6d 65 61 6e 73 20 73 79 6e 63 68 72 6f 6e  e means synchron
1fd48 6f 75 73 20 69 73 20 6f 66 66 0a 20 20 2a 2a 20  ous is off.  ** 
1fd49 61 6e 64 20 61 20 70 6f 73 69 74 69 76 65 20 76  and a positive v
1fd4a 61 6c 75 65 20 6d 65 61 6e 73 20 73 79 6e 63 68  alue means synch
1fd4b 72 6f 6e 6f 75 73 20 69 73 20 6f 6e 2e 0a 20 20  ronous is on..  
1fd4c 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
1fd4d 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 64  StrICmp(zLeft,"d
1fd4e 65 66 61 75 6c 74 5f 63 61 63 68 65 5f 73 69 7a  efault_cache_siz
1fd4f 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74  e")==0 ){.    st
1fd50 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f  atic const VdbeO
1fd51 70 4c 69 73 74 20 67 65 74 43 61 63 68 65 53 69  pList getCacheSi
1fd52 7a 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b  ze[] = {.      {
1fd53 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20   OP_ReadCookie, 
1fd54 20 30 2c 20 31 2c 20 20 20 20 20 20 20 20 32 7d   0, 1,        2}
1fd55 2c 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20  ,  /* 0 */.     
1fd56 20 7b 20 4f 50 5f 49 66 50 6f 73 2c 20 20 20 20   { OP_IfPos,    
1fd57 20 20 20 31 2c 20 36 2c 20 20 20 20 20 20 20 20     1, 6,        
1fd58 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49  0},.      { OP_I
1fd59 6e 74 65 67 65 72 2c 20 20 20 20 20 30 2c 20 32  nteger,     0, 2
1fd5a 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20  ,        0},.   
1fd5b 20 20 20 7b 20 4f 50 5f 53 75 62 74 72 61 63 74     { OP_Subtract
1fd5c 2c 20 20 20 20 31 2c 20 32 2c 20 20 20 20 20 20  ,    1, 2,      
1fd5d 20 20 31 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50    1},.      { OP
1fd5e 5f 49 66 50 6f 73 2c 20 20 20 20 20 20 20 31 2c  _IfPos,       1,
1fd5f 20 36 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20   6,        0},. 
1fd60 20 20 20 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65       { OP_Intege
1fd61 72 2c 20 20 20 20 20 30 2c 20 31 2c 20 20 20 20  r,     0, 1,    
1fd62 20 20 20 20 30 7d 2c 20 20 2f 2a 20 35 20 2a 2f      0},  /* 5 */
1fd63 0a 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 73 75  .      { OP_Resu
1fd64 6c 74 52 6f 77 2c 20 20 20 31 2c 20 31 2c 20 20  ltRow,   1, 1,  
1fd65 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 7d 3b        0},.    };
1fd66 0a 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20  .    int addr;. 
1fd67 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
1fd68 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
1fd69 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f   ) goto pragma_o
1fd6a 75 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ut;.    sqlite3V
1fd6b 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20  dbeUsesBtree(v, 
1fd6c 69 44 62 29 3b 0a 20 20 20 20 69 66 28 20 21 7a  iDb);.    if( !z
1fd6d 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 73  Right ){.      s
1fd6e 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
1fd6f 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20  Cols(v, 1);.    
1fd70 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
1fd71 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
1fd72 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 63 61 63  LNAME_NAME, "cac
1fd73 68 65 5f 73 69 7a 65 22 2c 20 50 34 5f 53 54 41  he_size", P4_STA
1fd74 54 49 43 29 3b 0a 20 20 20 20 20 20 70 50 61 72  TIC);.      pPar
1fd75 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 32 3b 0a 20  se->nMem += 2;. 
1fd76 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69       addr = sqli
1fd77 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
1fd78 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28 67 65  (v, ArraySize(ge
1fd79 74 43 61 63 68 65 53 69 7a 65 29 2c 20 67 65 74  tCacheSize), get
1fd7a 43 61 63 68 65 53 69 7a 65 29 3b 0a 20 20 20 20  CacheSize);.    
1fd7b 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1fd7c 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2c 20 69  ngeP1(v, addr, i
1fd7d 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Db);.      sqlit
1fd7e 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76  e3VdbeChangeP1(v
1fd7f 2c 20 61 64 64 72 2b 35 2c 20 53 51 4c 49 54 45  , addr+5, SQLITE
1fd80 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53  _DEFAULT_CACHE_S
1fd81 49 5a 45 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  IZE);.    }else{
1fd82 0a 20 20 20 20 20 20 69 6e 74 20 73 69 7a 65 20  .      int size 
1fd83 3d 20 61 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a  = atoi(zRight);.
1fd84 20 20 20 20 20 20 69 66 28 20 73 69 7a 65 3c 30        if( size<0
1fd85 20 29 20 73 69 7a 65 20 3d 20 2d 73 69 7a 65 3b   ) size = -size;
1fd86 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65  .      sqlite3Be
1fd87 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
1fd88 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62  n(pParse, 0, iDb
1fd89 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1fd8a 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1fd8b 5f 49 6e 74 65 67 65 72 2c 20 73 69 7a 65 2c 20  _Integer, size, 
1fd8c 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
1fd8d 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
1fd8e 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 69 44  P_ReadCookie, iD
1fd8f 62 2c 20 32 2c 20 32 29 3b 0a 20 20 20 20 20 20  b, 2, 2);.      
1fd90 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
1fd91 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
1fd92 66 50 6f 73 2c 20 32 2c 20 30 29 3b 0a 20 20 20  fPos, 2, 0);.   
1fd93 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1fd94 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
1fd95 65 72 2c 20 2d 73 69 7a 65 2c 20 31 29 3b 0a 20  er, -size, 1);. 
1fd96 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1fd97 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
1fd98 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1fd99 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
1fd9a 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c  _SetCookie, iDb,
1fd9b 20 32 2c 20 31 29 3b 0a 20 20 20 20 20 20 70 44   2, 1);.      pD
1fd9c 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68  b->pSchema->cach
1fd9d 65 5f 73 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20  e_size = size;. 
1fd9e 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
1fd9f 65 53 65 74 43 61 63 68 65 53 69 7a 65 28 70 44  eSetCacheSize(pD
1fda0 62 2d 3e 70 42 74 2c 20 70 44 62 2d 3e 70 53 63  b->pBt, pDb->pSc
1fda1 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65  hema->cache_size
1fda2 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
1fda3 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41  ..  /*.  **  PRA
1fda4 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 70  GMA [database.]p
1fda5 61 67 65 5f 73 69 7a 65 0a 20 20 2a 2a 20 20 50  age_size.  **  P
1fda6 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
1fda7 5d 70 61 67 65 5f 73 69 7a 65 3d 4e 0a 20 20 2a  ]page_size=N.  *
1fda8 2a 0a 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74  *.  ** The first
1fda9 20 66 6f 72 6d 20 72 65 70 6f 72 74 73 20 74 68   form reports th
1fdaa 65 20 63 75 72 72 65 6e 74 20 73 65 74 74 69 6e  e current settin
1fdab 67 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 64  g for the.  ** d
1fdac 61 74 61 62 61 73 65 20 70 61 67 65 20 73 69 7a  atabase page siz
1fdad 65 20 69 6e 20 62 79 74 65 73 2e 20 20 54 68 65  e in bytes.  The
1fdae 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20 73 65 74   second form set
1fdaf 73 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62  s the.  ** datab
1fdb0 61 73 65 20 70 61 67 65 20 73 69 7a 65 20 76 61  ase page size va
1fdb1 6c 75 65 2e 20 20 54 68 65 20 76 61 6c 75 65 20  lue.  The value 
1fdb2 63 61 6e 20 6f 6e 6c 79 20 62 65 20 73 65 74 20  can only be set 
1fdb3 69 66 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61  if.  ** the data
1fdb4 62 61 73 65 20 68 61 73 20 6e 6f 74 20 79 65 74  base has not yet
1fdb5 20 62 65 65 6e 20 63 72 65 61 74 65 64 2e 0a 20   been created.. 
1fdb6 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
1fdb7 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22  3StrICmp(zLeft,"
1fdb8 70 61 67 65 5f 73 69 7a 65 22 29 3d 3d 30 20 29  page_size")==0 )
1fdb9 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42 74  {.    Btree *pBt
1fdba 20 3d 20 70 44 62 2d 3e 70 42 74 3b 0a 20 20 20   = pDb->pBt;.   
1fdbb 20 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a   if( !zRight ){.
1fdbc 20 20 20 20 20 20 69 6e 74 20 73 69 7a 65 20 3d        int size =
1fdbd 20 70 42 74 20 3f 20 73 71 6c 69 74 65 33 42 74   pBt ? sqlite3Bt
1fdbe 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70  reeGetPageSize(p
1fdbf 42 74 29 20 3a 20 30 3b 0a 20 20 20 20 20 20 72  Bt) : 0;.      r
1fdc0 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70  eturnSingleInt(p
1fdc1 50 61 72 73 65 2c 20 22 70 61 67 65 5f 73 69 7a  Parse, "page_siz
1fdc2 65 22 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20 7d  e", size);.    }
1fdc3 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4d  else{.      /* M
1fdc4 61 6c 6c 6f 63 20 6d 61 79 20 66 61 69 6c 20 77  alloc may fail w
1fdc5 68 65 6e 20 73 65 74 74 69 6e 67 20 74 68 65 20  hen setting the 
1fdc6 70 61 67 65 2d 73 69 7a 65 2c 20 61 73 20 74 68  page-size, as th
1fdc7 65 72 65 20 69 73 20 61 6e 20 69 6e 74 65 72 6e  ere is an intern
1fdc8 61 6c 0a 20 20 20 20 20 20 2a 2a 20 62 75 66 66  al.      ** buff
1fdc9 65 72 20 74 68 61 74 20 74 68 65 20 70 61 67 65  er that the page
1fdca 72 20 6d 6f 64 75 6c 65 20 72 65 73 69 7a 65 73  r module resizes
1fdcb 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 72   using sqlite3_r
1fdcc 65 61 6c 6c 6f 63 28 29 2e 0a 20 20 20 20 20 20  ealloc()..      
1fdcd 2a 2f 0a 20 20 20 20 20 20 64 62 2d 3e 6e 65 78  */.      db->nex
1fdce 74 50 61 67 65 73 69 7a 65 20 3d 20 61 74 6f 69  tPagesize = atoi
1fdcf 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20  (zRight);.      
1fdd0 69 66 28 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  if( SQLITE_NOMEM
1fdd1 3d 3d 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  ==sqlite3BtreeSe
1fdd2 74 50 61 67 65 53 69 7a 65 28 70 42 74 2c 20 64  tPageSize(pBt, d
1fdd3 62 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a 65 2c  b->nextPagesize,
1fdd4 20 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20   -1) ){.        
1fdd5 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1fdd6 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
1fdd7 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f    }.  }else..  /
1fdd8 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  *.  **  PRAGMA [
1fdd9 64 61 74 61 62 61 73 65 2e 5d 6d 61 78 5f 70 61  database.]max_pa
1fdda 67 65 5f 63 6f 75 6e 74 0a 20 20 2a 2a 20 20 50  ge_count.  **  P
1fddb 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
1fddc 5d 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 3d  ]max_page_count=
1fddd 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  N.  **.  ** The 
1fdde 66 69 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72  first form repor
1fddf 74 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  ts the current s
1fde0 65 74 74 69 6e 67 20 66 6f 72 20 74 68 65 0a 20  etting for the. 
1fde1 20 2a 2a 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62   ** maximum numb
1fde2 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74  er of pages in t
1fde3 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1fde4 2e 20 20 54 68 65 20 0a 20 20 2a 2a 20 73 65 63  .  The .  ** sec
1fde5 6f 6e 64 20 66 6f 72 6d 20 61 74 74 65 6d 70 74  ond form attempt
1fde6 73 20 74 6f 20 63 68 61 6e 67 65 20 74 68 69 73  s to change this
1fde7 20 73 65 74 74 69 6e 67 2e 20 20 42 6f 74 68 0a   setting.  Both.
1fde8 20 20 2a 2a 20 66 6f 72 6d 73 20 72 65 74 75 72    ** forms retur
1fde9 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 65  n the current se
1fdea 74 74 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66  tting..  */.  if
1fdeb 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
1fdec 28 7a 4c 65 66 74 2c 22 6d 61 78 5f 70 61 67 65  (zLeft,"max_page
1fded 5f 63 6f 75 6e 74 22 29 3d 3d 30 20 29 7b 0a 20  _count")==0 ){. 
1fdee 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20     Btree *pBt = 
1fdef 70 44 62 2d 3e 70 42 74 3b 0a 20 20 20 20 69 6e  pDb->pBt;.    in
1fdf0 74 20 6e 65 77 4d 61 78 20 3d 20 30 3b 0a 20 20  t newMax = 0;.  
1fdf1 20 20 69 66 28 20 7a 52 69 67 68 74 20 29 7b 0a    if( zRight ){.
1fdf2 20 20 20 20 20 20 6e 65 77 4d 61 78 20 3d 20 61        newMax = a
1fdf3 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20 20 20  toi(zRight);.   
1fdf4 20 7d 0a 20 20 20 20 69 66 28 20 70 42 74 20 29   }.    if( pBt )
1fdf5 7b 0a 20 20 20 20 20 20 6e 65 77 4d 61 78 20 3d  {.      newMax =
1fdf6 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78   sqlite3BtreeMax
1fdf7 50 61 67 65 43 6f 75 6e 74 28 70 42 74 2c 20 6e  PageCount(pBt, n
1fdf8 65 77 4d 61 78 29 3b 0a 20 20 20 20 7d 0a 20 20  ewMax);.    }.  
1fdf9 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e    returnSingleIn
1fdfa 74 28 70 50 61 72 73 65 2c 20 22 6d 61 78 5f 70  t(pParse, "max_p
1fdfb 61 67 65 5f 63 6f 75 6e 74 22 2c 20 6e 65 77 4d  age_count", newM
1fdfc 61 78 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a 20 20  ax);.  }else..  
1fdfd 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20  /*.  **  PRAGMA 
1fdfe 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 69  [database.]locki
1fdff 6e 67 5f 6d 6f 64 65 0a 20 20 2a 2a 20 20 50 52  ng_mode.  **  PR
1fe00 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
1fe01 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 20 3d 20 28  locking_mode = (
1fe02 6e 6f 72 6d 61 6c 7c 65 78 63 6c 75 73 69 76 65  normal|exclusive
1fe03 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ).  */.  if( sql
1fe04 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
1fe05 74 2c 22 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 22  t,"locking_mode"
1fe06 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6e 73  )==0 ){.    cons
1fe07 74 20 63 68 61 72 20 2a 7a 52 65 74 20 3d 20 22  t char *zRet = "
1fe08 6e 6f 72 6d 61 6c 22 3b 0a 20 20 20 20 69 6e 74  normal";.    int
1fe09 20 65 4d 6f 64 65 20 3d 20 67 65 74 4c 6f 63 6b   eMode = getLock
1fe0a 69 6e 67 4d 6f 64 65 28 7a 52 69 67 68 74 29 3b  ingMode(zRight);
1fe0b 0a 0a 20 20 20 20 69 66 28 20 70 49 64 32 2d 3e  ..    if( pId2->
1fe0c 6e 3d 3d 30 20 26 26 20 65 4d 6f 64 65 3d 3d 50  n==0 && eMode==P
1fe0d 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
1fe0e 5f 51 55 45 52 59 20 29 7b 0a 20 20 20 20 20 20  _QUERY ){.      
1fe0f 2f 2a 20 53 69 6d 70 6c 65 20 22 50 52 41 47 4d  /* Simple "PRAGM
1fe10 41 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3b 22  A locking_mode;"
1fe11 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 69 73   statement. This
1fe12 20 69 73 20 61 20 71 75 65 72 79 20 66 6f 72 0a   is a query for.
1fe13 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63 75 72        ** the cur
1fe14 72 65 6e 74 20 64 65 66 61 75 6c 74 20 6c 6f 63  rent default loc
1fe15 6b 69 6e 67 20 6d 6f 64 65 20 28 77 68 69 63 68  king mode (which
1fe16 20 6d 61 79 20 62 65 20 64 69 66 66 65 72 65 6e   may be differen
1fe17 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68  t to.      ** th
1fe18 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 6f  e locking-mode o
1fe19 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  f the main datab
1fe1a 61 73 65 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ase)..      */. 
1fe1b 20 20 20 20 20 65 4d 6f 64 65 20 3d 20 64 62 2d       eMode = db-
1fe1c 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 3b 0a 20  >dfltLockMode;. 
1fe1d 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1fe1e 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a 20  Pager *pPager;. 
1fe1f 20 20 20 20 20 69 66 28 20 70 49 64 32 2d 3e 6e       if( pId2->n
1fe20 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ==0 ){.        /
1fe21 2a 20 54 68 69 73 20 69 6e 64 69 63 61 74 65 73  * This indicates
1fe22 20 74 68 61 74 20 6e 6f 20 64 61 74 61 62 61 73   that no databas
1fe23 65 20 6e 61 6d 65 20 77 61 73 20 73 70 65 63 69  e name was speci
1fe24 66 69 65 64 20 61 73 20 70 61 72 74 0a 20 20 20  fied as part.   
1fe25 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 50       ** of the P
1fe26 52 41 47 4d 41 20 63 6f 6d 6d 61 6e 64 2e 20 49  RAGMA command. I
1fe27 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20  n this case the 
1fe28 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 6d 75 73  locking-mode mus
1fe29 74 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  t be.        ** 
1fe2a 73 65 74 20 6f 6e 20 61 6c 6c 20 61 74 74 61 63  set on all attac
1fe2b 68 65 64 20 64 61 74 61 62 61 73 65 73 2c 20 61  hed databases, a
1fe2c 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20 6d 61  s well as the ma
1fe2d 69 6e 20 64 62 20 66 69 6c 65 2e 0a 20 20 20 20  in db file..    
1fe2e 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
1fe2f 2a 20 41 6c 73 6f 2c 20 74 68 65 20 73 71 6c 69  * Also, the sqli
1fe30 74 65 33 2e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65  te3.dfltLockMode
1fe31 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74   variable is set
1fe32 20 73 6f 20 74 68 61 74 0a 20 20 20 20 20 20 20   so that.       
1fe33 20 2a 2a 20 61 6e 79 20 73 75 62 73 65 71 75 65   ** any subseque
1fe34 6e 74 6c 79 20 61 74 74 61 63 68 65 64 20 64 61  ntly attached da
1fe35 74 61 62 61 73 65 73 20 61 6c 73 6f 20 75 73 65  tabases also use
1fe36 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 20   the specified. 
1fe37 20 20 20 20 20 20 20 2a 2a 20 6c 6f 63 6b 69 6e         ** lockin
1fe38 67 20 6d 6f 64 65 2e 0a 20 20 20 20 20 20 20 20  g mode..        
1fe39 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  */.        int i
1fe3a 69 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  i;.        asser
1fe3b 74 28 70 44 62 3d 3d 26 64 62 2d 3e 61 44 62 5b  t(pDb==&db->aDb[
1fe3c 30 5d 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  0]);.        for
1fe3d 28 69 69 3d 32 3b 20 69 69 3c 64 62 2d 3e 6e 44  (ii=2; ii<db->nD
1fe3e 62 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20  b; ii++){.      
1fe3f 20 20 20 20 70 50 61 67 65 72 20 3d 20 73 71 6c      pPager = sql
1fe40 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 64  ite3BtreePager(d
1fe41 62 2d 3e 61 44 62 5b 69 69 5d 2e 70 42 74 29 3b  b->aDb[ii].pBt);
1fe42 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1fe43 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f  e3PagerLockingMo
1fe44 64 65 28 70 50 61 67 65 72 2c 20 65 4d 6f 64 65  de(pPager, eMode
1fe45 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
1fe46 20 20 20 20 20 64 62 2d 3e 64 66 6c 74 4c 6f 63       db->dfltLoc
1fe47 6b 4d 6f 64 65 20 3d 20 65 4d 6f 64 65 3b 0a 20  kMode = eMode;. 
1fe48 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61       }.      pPa
1fe49 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72  ger = sqlite3Btr
1fe4a 65 65 50 61 67 65 72 28 70 44 62 2d 3e 70 42 74  eePager(pDb->pBt
1fe4b 29 3b 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d  );.      eMode =
1fe4c 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63   sqlite3PagerLoc
1fe4d 6b 69 6e 67 4d 6f 64 65 28 70 50 61 67 65 72 2c  kingMode(pPager,
1fe4e 20 65 4d 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 0a   eMode);.    }..
1fe4f 20 20 20 20 61 73 73 65 72 74 28 65 4d 6f 64 65      assert(eMode
1fe50 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  ==PAGER_LOCKINGM
1fe51 4f 44 45 5f 4e 4f 52 4d 41 4c 7c 7c 65 4d 6f 64  ODE_NORMAL||eMod
1fe52 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  e==PAGER_LOCKING
1fe53 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 29 3b  MODE_EXCLUSIVE);
1fe54 0a 20 20 20 20 69 66 28 20 65 4d 6f 64 65 3d 3d  .    if( eMode==
1fe55 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44  PAGER_LOCKINGMOD
1fe56 45 5f 45 58 43 4c 55 53 49 56 45 20 29 7b 0a 20  E_EXCLUSIVE ){. 
1fe57 20 20 20 20 20 7a 52 65 74 20 3d 20 22 65 78 63       zRet = "exc
1fe58 6c 75 73 69 76 65 22 3b 0a 20 20 20 20 7d 0a 20  lusive";.    }. 
1fe59 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
1fe5a 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a  tNumCols(v, 1);.
1fe5b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
1fe5c 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
1fe5d 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6c  COLNAME_NAME, "l
1fe5e 6f 63 6b 69 6e 67 5f 6d 6f 64 65 22 2c 20 50 34  ocking_mode", P4
1fe5f 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71  _STATIC);.    sq
1fe60 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
1fe61 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
1fe62 2c 20 31 2c 20 30 2c 20 7a 52 65 74 2c 20 30 29  , 1, 0, zRet, 0)
1fe63 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1fe64 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
1fe65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31 29 3b 0a  sultRow, 1, 1);.
1fe66 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20    }else..  /*.  
1fe67 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  **  PRAGMA [data
1fe68 62 61 73 65 2e 5d 6a 6f 75 72 6e 61 6c 5f 6d 6f  base.]journal_mo
1fe69 64 65 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20  de.  **  PRAGMA 
1fe6a 5b 64 61 74 61 62 61 73 65 2e 5d 6a 6f 75 72 6e  [database.]journ
1fe6b 61 6c 5f 6d 6f 64 65 20 3d 20 28 64 65 6c 65 74  al_mode = (delet
1fe6c 65 7c 70 65 72 73 69 73 74 7c 6f 66 66 29 0a 20  e|persist|off). 
1fe6d 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
1fe6e 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22  3StrICmp(zLeft,"
1fe6f 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 22 29 3d 3d  journal_mode")==
1fe70 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 65 4d 6f  0 ){.    int eMo
1fe71 64 65 3b 0a 20 20 20 20 73 74 61 74 69 63 20 63  de;.    static c
1fe72 6f 6e 73 74 20 63 68 61 72 20 2a 61 7a 4d 6f 64  onst char *azMod
1fe73 65 4e 61 6d 65 5b 5d 20 3d 20 7b 22 64 65 6c 65  eName[] = {"dele
1fe74 74 65 22 2c 20 22 70 65 72 73 69 73 74 22 2c 20  te", "persist", 
1fe75 22 6f 66 66 22 7d 3b 0a 0a 20 20 20 20 69 66 28  "off"};..    if(
1fe76 20 7a 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20   zRight==0 ){.  
1fe77 20 20 20 20 65 4d 6f 64 65 20 3d 20 50 41 47 45      eMode = PAGE
1fe78 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55  R_JOURNALMODE_QU
1fe79 45 52 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ERY;.    }else{.
1fe7a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 74        int n = st
1fe7b 72 6c 65 6e 28 7a 52 69 67 68 74 29 3b 0a 20 20  rlen(zRight);.  
1fe7c 20 20 20 20 65 4d 6f 64 65 20 3d 20 32 3b 0a 20      eMode = 2;. 
1fe7d 20 20 20 20 20 77 68 69 6c 65 28 20 65 4d 6f 64       while( eMod
1fe7e 65 3e 3d 30 20 26 26 20 73 71 6c 69 74 65 33 53  e>=0 && sqlite3S
1fe7f 74 72 4e 49 43 6d 70 28 7a 52 69 67 68 74 2c 20  trNICmp(zRight, 
1fe80 61 7a 4d 6f 64 65 4e 61 6d 65 5b 65 4d 6f 64 65  azModeName[eMode
1fe81 5d 2c 20 6e 29 21 3d 30 20 29 7b 0a 20 20 20 20  ], n)!=0 ){.    
1fe82 20 20 20 20 65 4d 6f 64 65 2d 2d 3b 0a 20 20 20      eMode--;.   
1fe83 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
1fe84 66 28 20 70 49 64 32 2d 3e 6e 3d 3d 30 20 26 26  f( pId2->n==0 &&
1fe85 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f   eMode==PAGER_JO
1fe86 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 20  URNALMODE_QUERY 
1fe87 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 69 6d 70  ){.      /* Simp
1fe88 6c 65 20 22 50 52 41 47 4d 41 20 70 65 72 73 69  le "PRAGMA persi
1fe89 73 74 65 6e 74 5f 6a 6f 75 72 6e 61 6c 3b 22 20  stent_journal;" 
1fe8a 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 20  statement. This 
1fe8b 69 73 20 61 20 71 75 65 72 79 20 66 6f 72 0a 20  is a query for. 
1fe8c 20 20 20 20 20 2a 2a 20 74 68 65 20 63 75 72 72       ** the curr
1fe8d 65 6e 74 20 64 65 66 61 75 6c 74 20 6a 6f 75 72  ent default jour
1fe8e 6e 61 6c 20 6d 6f 64 65 20 28 77 68 69 63 68 20  nal mode (which 
1fe8f 6d 61 79 20 62 65 20 64 69 66 66 65 72 65 6e 74  may be different
1fe90 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65   to.      ** the
1fe91 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 6f 66   journal-mode of
1fe92 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
1fe93 73 65 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  se)..      */.  
1fe94 20 20 20 20 65 4d 6f 64 65 20 3d 20 64 62 2d 3e      eMode = db->
1fe95 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 3b  dfltJournalMode;
1fe96 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1fe97 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b    Pager *pPager;
1fe98 0a 20 20 20 20 20 20 69 66 28 20 70 49 64 32 2d  .      if( pId2-
1fe99 3e 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  >n==0 ){.       
1fe9a 20 2f 2a 20 54 68 69 73 20 69 6e 64 69 63 61 74   /* This indicat
1fe9b 65 73 20 74 68 61 74 20 6e 6f 20 64 61 74 61 62  es that no datab
1fe9c 61 73 65 20 6e 61 6d 65 20 77 61 73 20 73 70 65  ase name was spe
1fe9d 63 69 66 69 65 64 20 61 73 20 70 61 72 74 0a 20  cified as part. 
1fe9e 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65         ** of the
1fe9f 20 50 52 41 47 4d 41 20 63 6f 6d 6d 61 6e 64 2e   PRAGMA command.
1fea0 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
1fea1 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 6d  e journal-mode m
1fea2 75 73 74 20 62 65 0a 20 20 20 20 20 20 20 20 2a  ust be.        *
1fea3 2a 20 73 65 74 20 6f 6e 20 61 6c 6c 20 61 74 74  * set on all att
1fea4 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 2c  ached databases,
1fea5 20 61 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20   as well as the 
1fea6 6d 61 69 6e 20 64 62 20 66 69 6c 65 2e 0a 20 20  main db file..  
1fea7 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
1fea8 20 2a 2a 20 41 6c 73 6f 2c 20 74 68 65 20 73 71   ** Also, the sq
1fea9 6c 69 74 65 33 2e 64 66 6c 74 4a 6f 75 72 6e 61  lite3.dfltJourna
1feaa 6c 4d 6f 64 65 20 76 61 72 69 61 62 6c 65 20 69  lMode variable i
1feab 73 20 73 65 74 20 73 6f 20 74 68 61 74 0a 20 20  s set so that.  
1feac 20 20 20 20 20 20 2a 2a 20 61 6e 79 20 73 75 62        ** any sub
1fead 73 65 71 75 65 6e 74 6c 79 20 61 74 74 61 63 68  sequently attach
1feae 65 64 20 64 61 74 61 62 61 73 65 73 20 61 6c 73  ed databases als
1feaf 6f 20 75 73 65 20 74 68 65 20 73 70 65 63 69 66  o use the specif
1feb0 69 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 6a  ied.        ** j
1feb1 6f 75 72 6e 61 6c 20 6d 6f 64 65 2e 0a 20 20 20  ournal mode..   
1feb2 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
1feb3 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 20 20  int ii;.        
1feb4 61 73 73 65 72 74 28 70 44 62 3d 3d 26 64 62 2d  assert(pDb==&db-
1feb5 3e 61 44 62 5b 30 5d 29 3b 0a 20 20 20 20 20 20  >aDb[0]);.      
1feb6 20 20 66 6f 72 28 69 69 3d 31 3b 20 69 69 3c 64    for(ii=1; ii<d
1feb7 62 2d 3e 6e 44 62 3b 20 69 69 2b 2b 29 7b 0a 20  b->nDb; ii++){. 
1feb8 20 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d           if( db-
1feb9 3e 61 44 62 5b 69 69 5d 2e 70 42 74 20 29 7b 0a  >aDb[ii].pBt ){.
1feba 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61 67              pPag
1febb 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  er = sqlite3Btre
1febc 65 50 61 67 65 72 28 64 62 2d 3e 61 44 62 5b 69  ePager(db->aDb[i
1febd 69 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20 20 20  i].pBt);.       
1febe 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
1febf 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 70 50 61  rJournalMode(pPa
1fec0 67 65 72 2c 20 65 4d 6f 64 65 29 3b 0a 20 20 20  ger, eMode);.   
1fec1 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1fec2 20 7d 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 64   }.        db->d
1fec3 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d  fltJournalMode =
1fec4 20 65 4d 6f 64 65 3b 0a 20 20 20 20 20 20 7d 0a   eMode;.      }.
1fec5 20 20 20 20 20 20 70 50 61 67 65 72 20 3d 20 73        pPager = s
1fec6 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
1fec7 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20  (pDb->pBt);.    
1fec8 20 20 65 4d 6f 64 65 20 3d 20 73 71 6c 69 74 65    eMode = sqlite
1fec9 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64  3PagerJournalMod
1feca 65 28 70 50 61 67 65 72 2c 20 65 4d 6f 64 65 29  e(pPager, eMode)
1fecb 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
1fecc 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52  rt( eMode==PAGER
1fecd 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c  _JOURNALMODE_DEL
1fece 45 54 45 0a 20 20 20 20 20 20 20 20 20 20 20 20  ETE.            
1fecf 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45    || eMode==PAGE
1fed0 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45  R_JOURNALMODE_PE
1fed1 52 53 49 53 54 0a 20 20 20 20 20 20 20 20 20 20  RSIST.          
1fed2 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41      || eMode==PA
1fed3 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
1fed4 4f 46 46 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  OFF );.    sqlit
1fed5 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
1fed6 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  (v, 1);.    sqli
1fed7 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
1fed8 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
1fed9 4e 41 4d 45 2c 20 22 6a 6f 75 72 6e 61 6c 5f 6d  NAME, "journal_m
1feda 6f 64 65 22 2c 20 50 34 5f 53 54 41 54 49 43 29  ode", P4_STATIC)
1fedb 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1fedc 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74  eAddOp4(v, OP_St
1fedd 72 69 6e 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20  ring8, 0, 1, 0, 
1fede 0a 20 20 20 20 20 20 20 20 20 20 20 61 7a 4d 6f  .           azMo
1fedf 64 65 4e 61 6d 65 5b 65 4d 6f 64 65 5d 2c 20 50  deName[eMode], P
1fee0 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73  4_STATIC);.    s
1fee1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1fee2 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
1fee3 2c 20 31 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65  , 1, 1);.  }else
1fee4 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1fee5 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41  E_OMIT_PAGER_PRA
1fee6 47 4d 41 53 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20  GMAS */..  /*.  
1fee7 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  **  PRAGMA [data
1fee8 62 61 73 65 2e 5d 61 75 74 6f 5f 76 61 63 75 75  base.]auto_vacuu
1fee9 6d 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  m.  **  PRAGMA [
1feea 64 61 74 61 62 61 73 65 2e 5d 61 75 74 6f 5f 76  database.]auto_v
1feeb 61 63 75 75 6d 3d 4e 0a 20 20 2a 2a 0a 20 20 2a  acuum=N.  **.  *
1feec 2a 20 47 65 74 20 6f 72 20 73 65 74 20 74 68 65  * Get or set the
1feed 20 28 62 6f 6f 6c 65 61 6e 29 20 76 61 6c 75 65   (boolean) value
1feee 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1feef 20 27 61 75 74 6f 2d 76 61 63 75 75 6d 27 20 70   'auto-vacuum' p
1fef0 61 72 61 6d 65 74 65 72 2e 0a 20 20 2a 2f 0a 23  arameter..  */.#
1fef1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1fef2 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
1fef3 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
1fef4 6d 70 28 7a 4c 65 66 74 2c 22 61 75 74 6f 5f 76  mp(zLeft,"auto_v
1fef5 61 63 75 75 6d 22 29 3d 3d 30 20 29 7b 0a 20 20  acuum")==0 ){.  
1fef6 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 70    Btree *pBt = p
1fef7 44 62 2d 3e 70 42 74 3b 0a 20 20 20 20 69 66 28  Db->pBt;.    if(
1fef8 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
1fef9 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20  ma(pParse) ){.  
1fefa 20 20 20 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f      goto pragma_
1fefb 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  out;.    }.    i
1fefc 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a 20 20  f( !zRight ){.  
1fefd 20 20 20 20 69 6e 74 20 61 75 74 6f 5f 76 61 63      int auto_vac
1fefe 75 75 6d 20 3d 20 0a 20 20 20 20 20 20 20 20 20  uum = .         
1feff 20 70 42 74 20 3f 20 73 71 6c 69 74 65 33 42 74   pBt ? sqlite3Bt
1ff00 72 65 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d  reeGetAutoVacuum
1ff01 28 70 42 74 29 20 3a 20 53 51 4c 49 54 45 5f 44  (pBt) : SQLITE_D
1ff02 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55  EFAULT_AUTOVACUU
1ff03 4d 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 53  M;.      returnS
1ff04 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c  ingleInt(pParse,
1ff05 20 22 61 75 74 6f 5f 76 61 63 75 75 6d 22 2c 20   "auto_vacuum", 
1ff06 61 75 74 6f 5f 76 61 63 75 75 6d 29 3b 0a 20 20  auto_vacuum);.  
1ff07 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
1ff08 6e 74 20 65 41 75 74 6f 20 3d 20 67 65 74 41 75  nt eAuto = getAu
1ff09 74 6f 56 61 63 75 75 6d 28 7a 52 69 67 68 74 29  toVacuum(zRight)
1ff0a 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6e 65 78 74  ;.      db->next
1ff0b 41 75 74 6f 76 61 63 20 3d 20 65 41 75 74 6f 3b  Autovac = eAuto;
1ff0c 0a 20 20 20 20 20 20 69 66 28 20 65 41 75 74 6f  .      if( eAuto
1ff0d 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  >=0 ){.        /
1ff0e 2a 20 43 61 6c 6c 20 53 65 74 41 75 74 6f 56 61  * Call SetAutoVa
1ff0f 63 75 75 6d 28 29 20 74 6f 20 73 65 74 20 69 6e  cuum() to set in
1ff10 69 74 69 61 6c 69 7a 65 20 74 68 65 20 69 6e 74  itialize the int
1ff11 65 72 6e 61 6c 20 61 75 74 6f 20 61 6e 64 0a 20  ernal auto and. 
1ff12 20 20 20 20 20 20 20 2a 2a 20 69 6e 63 72 2d 76         ** incr-v
1ff13 61 63 75 75 6d 20 66 6c 61 67 73 2e 20 54 68 69  acuum flags. Thi
1ff14 73 20 69 73 20 72 65 71 75 69 72 65 64 20 69 6e  s is required in
1ff15 20 63 61 73 65 20 74 68 69 73 20 63 6f 6e 6e 65   case this conne
1ff16 63 74 69 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a  ction.        **
1ff17 20 63 72 65 61 74 65 73 20 74 68 65 20 64 61 74   creates the dat
1ff18 61 62 61 73 65 20 66 69 6c 65 2e 20 49 74 20 69  abase file. It i
1ff19 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74  s important that
1ff1a 20 69 74 20 69 73 20 63 72 65 61 74 65 64 0a 20   it is created. 
1ff1b 20 20 20 20 20 20 20 2a 2a 20 61 73 20 61 6e 20         ** as an 
1ff1c 61 75 74 6f 2d 76 61 63 75 75 6d 20 63 61 70 61  auto-vacuum capa
1ff1d 62 6c 65 20 64 62 2e 0a 20 20 20 20 20 20 20 20  ble db..        
1ff1e 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72  */.        int r
1ff1f 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1ff20 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28 70 42  SetAutoVacuum(pB
1ff21 74 2c 20 65 41 75 74 6f 29 3b 0a 20 20 20 20 20  t, eAuto);.     
1ff22 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
1ff23 45 5f 4f 4b 20 26 26 20 28 65 41 75 74 6f 3d 3d  E_OK && (eAuto==
1ff24 31 20 7c 7c 20 65 41 75 74 6f 3d 3d 32 29 20 29  1 || eAuto==2) )
1ff25 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57  {.          /* W
1ff26 68 65 6e 20 73 65 74 74 69 6e 67 20 74 68 65 20  hen setting the 
1ff27 61 75 74 6f 5f 76 61 63 75 75 6d 20 6d 6f 64 65  auto_vacuum mode
1ff28 20 74 6f 20 65 69 74 68 65 72 20 22 66 75 6c 6c   to either "full
1ff29 22 20 6f 72 20 0a 20 20 20 20 20 20 20 20 20 20  " or .          
1ff2a 2a 2a 20 22 69 6e 63 72 65 6d 65 6e 74 61 6c 22  ** "incremental"
1ff2b 2c 20 77 72 69 74 65 20 74 68 65 20 76 61 6c 75  , write the valu
1ff2c 65 20 6f 66 20 6d 65 74 61 5b 36 5d 20 69 6e 20  e of meta[6] in 
1ff2d 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20  the database.   
1ff2e 20 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 2e 20         ** file. 
1ff2f 42 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20 74  Before writing t
1ff30 6f 20 6d 65 74 61 5b 36 5d 2c 20 63 68 65 63 6b  o meta[6], check
1ff31 20 74 68 61 74 20 6d 65 74 61 5b 33 5d 20 69 6e   that meta[3] in
1ff32 64 69 63 61 74 65 73 0a 20 20 20 20 20 20 20 20  dicates.        
1ff33 20 20 2a 2a 20 74 68 61 74 20 74 68 69 73 20 72    ** that this r
1ff34 65 61 6c 6c 79 20 69 73 20 61 6e 20 61 75 74 6f  eally is an auto
1ff35 2d 76 61 63 75 75 6d 20 63 61 70 61 62 6c 65 20  -vacuum capable 
1ff36 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20  database..      
1ff37 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20      */.         
1ff38 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64   static const Vd
1ff39 62 65 4f 70 4c 69 73 74 20 73 65 74 4d 65 74 61  beOpList setMeta
1ff3a 36 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20  6[] = {.        
1ff3b 20 20 20 20 7b 20 4f 50 5f 54 72 61 6e 73 61 63      { OP_Transac
1ff3c 74 69 6f 6e 2c 20 20 20 20 30 2c 20 20 20 20 20  tion,    0,     
1ff3d 20 20 20 20 20 20 20 20 20 20 31 2c 20 20 20 20            1,    
1ff3e 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20      0},    /* 0 
1ff3f 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b  */.            {
1ff40 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20   OP_ReadCookie, 
1ff41 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20      0,          
1ff42 20 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 33       1,        3
1ff43 7d 2c 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20  },    /* 1 */.  
1ff44 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49            { OP_I
1ff45 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 31  f,             1
1ff46 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1ff47 30 2c 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20  0,        0},   
1ff48 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 20 20 20   /* 2 */.       
1ff49 20 20 20 20 20 7b 20 4f 50 5f 48 61 6c 74 2c 20       { OP_Halt, 
1ff4a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
1ff4b 5f 4f 4b 2c 20 20 20 20 20 20 20 4f 45 5f 41 62  _OK,       OE_Ab
1ff4c 6f 72 74 2c 20 30 7d 2c 20 20 20 20 2f 2a 20 33  ort, 0},    /* 3
1ff4d 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
1ff4e 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 20 20  { OP_Integer,   
1ff4f 20 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20       0,         
1ff50 20 20 20 20 20 20 31 2c 20 20 20 20 20 20 20 20        1,        
1ff51 30 7d 2c 20 20 20 20 2f 2a 20 34 20 2a 2f 0a 20  0},    /* 4 */. 
1ff52 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
1ff53 53 65 74 43 6f 6f 6b 69 65 2c 20 20 20 20 20 20  SetCookie,      
1ff54 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
1ff55 20 36 2c 20 20 20 20 20 20 20 20 31 7d 2c 20 20   6,        1},  
1ff56 20 20 2f 2a 20 35 20 2a 2f 0a 20 20 20 20 20 20    /* 5 */.      
1ff57 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 20 20 20      };.         
1ff58 20 69 6e 74 20 69 41 64 64 72 3b 0a 20 20 20 20   int iAddr;.    
1ff59 20 20 20 20 20 20 69 41 64 64 72 20 3d 20 73 71        iAddr = sq
1ff5a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69  lite3VdbeAddOpLi
1ff5b 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28  st(v, ArraySize(
1ff5c 73 65 74 4d 65 74 61 36 29 2c 20 73 65 74 4d 65  setMeta6), setMe
1ff5d 74 61 36 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ta6);.          
1ff5e 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1ff5f 65 50 31 28 76 2c 20 69 41 64 64 72 2c 20 69 44  eP1(v, iAddr, iD
1ff60 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  b);.          sq
1ff61 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1ff62 31 28 76 2c 20 69 41 64 64 72 2b 31 2c 20 69 44  1(v, iAddr+1, iD
1ff63 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  b);.          sq
1ff64 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1ff65 32 28 76 2c 20 69 41 64 64 72 2b 32 2c 20 69 41  2(v, iAddr+2, iA
1ff66 64 64 72 2b 34 29 3b 0a 20 20 20 20 20 20 20 20  ddr+4);.        
1ff67 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1ff68 6e 67 65 50 31 28 76 2c 20 69 41 64 64 72 2b 34  ngeP1(v, iAddr+4
1ff69 2c 20 65 41 75 74 6f 2d 31 29 3b 0a 20 20 20 20  , eAuto-1);.    
1ff6a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1ff6b 65 43 68 61 6e 67 65 50 31 28 76 2c 20 69 41 64  eChangeP1(v, iAd
1ff6c 64 72 2b 35 2c 20 69 44 62 29 3b 0a 20 20 20 20  dr+5, iDb);.    
1ff6d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1ff6e 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44  eUsesBtree(v, iD
1ff6f 62 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  b);.        }.  
1ff70 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65      }.    }.  }e
1ff71 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a  lse.#endif..  /*
1ff72 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
1ff73 61 74 61 62 61 73 65 2e 5d 69 6e 63 72 65 6d 65  atabase.]increme
1ff74 6e 74 61 6c 5f 76 61 63 75 75 6d 28 4e 29 0a 20  ntal_vacuum(N). 
1ff75 20 2a 2a 0a 20 20 2a 2a 20 44 6f 20 4e 20 73 74   **.  ** Do N st
1ff76 65 70 73 20 6f 66 20 69 6e 63 72 65 6d 65 6e 74  eps of increment
1ff77 61 6c 20 76 61 63 75 75 6d 69 6e 67 20 6f 6e 20  al vacuuming on 
1ff78 61 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f  a database..  */
1ff79 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1ff7a 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
1ff7b 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
1ff7c 49 43 6d 70 28 7a 4c 65 66 74 2c 22 69 6e 63 72  ICmp(zLeft,"incr
1ff7d 65 6d 65 6e 74 61 6c 5f 76 61 63 75 75 6d 22 29  emental_vacuum")
1ff7e 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  ==0 ){.    int i
1ff7f 4c 69 6d 69 74 2c 20 61 64 64 72 3b 0a 20 20 20  Limit, addr;.   
1ff80 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64   if( sqlite3Read
1ff81 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
1ff82 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 72 61  {.      goto pra
1ff83 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  gma_out;.    }. 
1ff84 20 20 20 69 66 28 20 7a 52 69 67 68 74 3d 3d 30     if( zRight==0
1ff85 20 7c 7c 20 21 73 71 6c 69 74 65 33 47 65 74 49   || !sqlite3GetI
1ff86 6e 74 33 32 28 7a 52 69 67 68 74 2c 20 26 69 4c  nt32(zRight, &iL
1ff87 69 6d 69 74 29 20 7c 7c 20 69 4c 69 6d 69 74 3c  imit) || iLimit<
1ff88 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 4c 69 6d  =0 ){.      iLim
1ff89 69 74 20 3d 20 30 78 37 66 66 66 66 66 66 66 3b  it = 0x7fffffff;
1ff8a 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
1ff8b 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
1ff8c 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c  ation(pParse, 0,
1ff8d 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74   iDb);.    sqlit
1ff8e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1ff8f 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 4c 69 6d  OP_Integer, iLim
1ff90 69 74 2c 20 31 29 3b 0a 20 20 20 20 61 64 64 72  it, 1);.    addr
1ff91 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
1ff92 64 4f 70 31 28 76 2c 20 4f 50 5f 49 6e 63 72 56  dOp1(v, OP_IncrV
1ff93 61 63 75 75 6d 2c 20 69 44 62 29 3b 0a 20 20 20  acuum, iDb);.   
1ff94 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1ff95 70 31 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p1(v, OP_ResultR
1ff96 6f 77 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  ow, 1);.    sqli
1ff97 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1ff98 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 31 2c 20 2d   OP_AddImm, 1, -
1ff99 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
1ff9a 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1ff9b 49 66 50 6f 73 2c 20 31 2c 20 61 64 64 72 29 3b  IfPos, 1, addr);
1ff9c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1ff9d 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
1ff9e 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69  );.  }else.#endi
1ff9f 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
1ffa0 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41  E_OMIT_PAGER_PRA
1ffa1 47 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20  GMAS.  /*.  **  
1ffa2 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
1ffa3 2e 5d 63 61 63 68 65 5f 73 69 7a 65 0a 20 20 2a  .]cache_size.  *
1ffa4 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62  *  PRAGMA [datab
1ffa5 61 73 65 2e 5d 63 61 63 68 65 5f 73 69 7a 65 3d  ase.]cache_size=
1ffa6 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  N.  **.  ** The 
1ffa7 66 69 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72  first form repor
1ffa8 74 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c  ts the current l
1ffa9 6f 63 61 6c 20 73 65 74 74 69 6e 67 20 66 6f 72  ocal setting for
1ffaa 20 74 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 63   the.  ** page c
1ffab 61 63 68 65 20 73 69 7a 65 2e 20 20 54 68 65 20  ache size.  The 
1ffac 6c 6f 63 61 6c 20 73 65 74 74 69 6e 67 20 63 61  local setting ca
1ffad 6e 20 62 65 20 64 69 66 66 65 72 65 6e 74 20 66  n be different f
1ffae 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 70 65 72  rom.  ** the per
1ffaf 73 69 73 74 65 6e 74 20 63 61 63 68 65 20 73 69  sistent cache si
1ffb0 7a 65 20 76 61 6c 75 65 20 74 68 61 74 20 69 73  ze value that is
1ffb1 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 64   stored in the d
1ffb2 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 66 69 6c  atabase.  ** fil
1ffb3 65 20 69 74 73 65 6c 66 2e 20 20 54 68 65 20 76  e itself.  The v
1ffb4 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73  alue returned is
1ffb5 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   the maximum num
1ffb6 62 65 72 20 6f 66 0a 20 20 2a 2a 20 70 61 67 65  ber of.  ** page
1ffb7 73 20 69 6e 20 74 68 65 20 70 61 67 65 20 63 61  s in the page ca
1ffb8 63 68 65 2e 20 20 54 68 65 20 73 65 63 6f 6e 64  che.  The second
1ffb9 20 66 6f 72 6d 20 73 65 74 73 20 74 68 65 20 6c   form sets the l
1ffba 6f 63 61 6c 0a 20 20 2a 2a 20 70 61 67 65 20 63  ocal.  ** page c
1ffbb 61 63 68 65 20 73 69 7a 65 20 76 61 6c 75 65 2e  ache size value.
1ffbc 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 63 68    It does not ch
1ffbd 61 6e 67 65 20 74 68 65 20 70 65 72 73 69 73 74  ange the persist
1ffbe 65 6e 74 0a 20 20 2a 2a 20 63 61 63 68 65 20 73  ent.  ** cache s
1ffbf 69 7a 65 20 73 74 6f 72 65 64 20 6f 6e 20 74 68  ize stored on th
1ffc0 65 20 64 69 73 6b 20 73 6f 20 74 68 65 20 63 61  e disk so the ca
1ffc1 63 68 65 20 73 69 7a 65 20 77 69 6c 6c 20 72 65  che size will re
1ffc2 76 65 72 74 0a 20 20 2a 2a 20 74 6f 20 69 74 73  vert.  ** to its
1ffc3 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 77   default value w
1ffc4 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
1ffc5 20 69 73 20 63 6c 6f 73 65 64 20 61 6e 64 20 72   is closed and r
1ffc6 65 6f 70 65 6e 65 64 2e 0a 20 20 2a 2a 20 4e 20  eopened..  ** N 
1ffc7 73 68 6f 75 6c 64 20 62 65 20 61 20 70 6f 73 69  should be a posi
1ffc8 74 69 76 65 20 69 6e 74 65 67 65 72 2e 0a 20 20  tive integer..  
1ffc9 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
1ffca 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 63  StrICmp(zLeft,"c
1ffcb 61 63 68 65 5f 73 69 7a 65 22 29 3d 3d 30 20 29  ache_size")==0 )
1ffcc 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
1ffcd 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
1ffce 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d  se) ) goto pragm
1ffcf 61 5f 6f 75 74 3b 0a 20 20 20 20 69 66 28 20 21  a_out;.    if( !
1ffd0 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  zRight ){.      
1ffd1 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28  returnSingleInt(
1ffd2 70 50 61 72 73 65 2c 20 22 63 61 63 68 65 5f 73  pParse, "cache_s
1ffd3 69 7a 65 22 2c 20 70 44 62 2d 3e 70 53 63 68 65  ize", pDb->pSche
1ffd4 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 29 3b  ma->cache_size);
1ffd5 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1ffd6 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 61 74 6f    int size = ato
1ffd7 69 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20  i(zRight);.     
1ffd8 20 69 66 28 20 73 69 7a 65 3c 30 20 29 20 73 69   if( size<0 ) si
1ffd9 7a 65 20 3d 20 2d 73 69 7a 65 3b 0a 20 20 20 20  ze = -size;.    
1ffda 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e    pDb->pSchema->
1ffdb 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 73 69 7a  cache_size = siz
1ffdc 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e;.      sqlite3
1ffdd 42 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a  BtreeSetCacheSiz
1ffde 65 28 70 44 62 2d 3e 70 42 74 2c 20 70 44 62 2d  e(pDb->pBt, pDb-
1ffdf 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f  >pSchema->cache_
1ffe0 73 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  size);.    }.  }
1ffe1 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  else..  /*.  ** 
1ffe2 20 20 50 52 41 47 4d 41 20 74 65 6d 70 5f 73 74    PRAGMA temp_st
1ffe3 6f 72 65 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d  ore.  **   PRAGM
1ffe4 41 20 74 65 6d 70 5f 73 74 6f 72 65 20 3d 20 22  A temp_store = "
1ffe5 64 65 66 61 75 6c 74 22 7c 22 6d 65 6d 6f 72 79  default"|"memory
1ffe6 22 7c 22 66 69 6c 65 22 0a 20 20 2a 2a 0a 20 20  "|"file".  **.  
1ffe7 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20 73 65 74  ** Return or set
1ffe8 20 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65   the local value
1ffe9 20 6f 66 20 74 68 65 20 74 65 6d 70 5f 73 74 6f   of the temp_sto
1ffea 72 65 20 66 6c 61 67 2e 20 20 43 68 61 6e 67 69  re flag.  Changi
1ffeb 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6c 6f 63 61  ng.  ** the loca
1ffec 6c 20 76 61 6c 75 65 20 64 6f 65 73 20 6e 6f 74  l value does not
1ffed 20 6d 61 6b 65 20 63 68 61 6e 67 65 73 20 74 6f   make changes to
1ffee 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65 20 61   the disk file a
1ffef 6e 64 20 74 68 65 20 64 65 66 61 75 6c 74 0a 20  nd the default. 
1fff0 20 2a 2a 20 76 61 6c 75 65 20 77 69 6c 6c 20 62   ** value will b
1fff1 65 20 72 65 73 74 6f 72 65 64 20 74 68 65 20 6e  e restored the n
1fff2 65 78 74 20 74 69 6d 65 20 74 68 65 20 64 61 74  ext time the dat
1fff3 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 2e  abase is opened.
1fff4 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20  .  **.  ** Note 
1fff5 74 68 61 74 20 69 74 20 69 73 20 70 6f 73 73 69  that it is possi
1fff6 62 6c 65 20 66 6f 72 20 74 68 65 20 6c 69 62 72  ble for the libr
1fff7 61 72 79 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  ary compile-time
1fff8 20 6f 70 74 69 6f 6e 73 20 74 6f 0a 20 20 2a 2a   options to.  **
1fff9 20 6f 76 65 72 72 69 64 65 20 74 68 69 73 20 73   override this s
1fffa 65 74 74 69 6e 67 0a 20 20 2a 2f 0a 20 20 69 66  etting.  */.  if
1fffb 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
1fffc 28 7a 4c 65 66 74 2c 20 22 74 65 6d 70 5f 73 74  (zLeft, "temp_st
1fffd 6f 72 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ore")==0 ){.    
1fffe 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a 20  if( !zRight ){. 
1ffff 20 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c       returnSingl
20000 65 49 6e 74 28 70 50 61 72 73 65 2c 20 22 74 65  eInt(pParse, "te
20001 6d 70 5f 73 74 6f 72 65 22 2c 20 64 62 2d 3e 74  mp_store", db->t
20002 65 6d 70 5f 73 74 6f 72 65 29 3b 0a 20 20 20 20  emp_store);.    
20003 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 68 61  }else{.      cha
20004 6e 67 65 54 65 6d 70 53 74 6f 72 61 67 65 28 70  ngeTempStorage(p
20005 50 61 72 73 65 2c 20 7a 52 69 67 68 74 29 3b 0a  Parse, zRight);.
20006 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20      }.  }else.. 
20007 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d   /*.  **   PRAGM
20008 41 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72  A temp_store_dir
20009 65 63 74 6f 72 79 0a 20 20 2a 2a 20 20 20 50 52  ectory.  **   PR
2000a 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 5f  AGMA temp_store_
2000b 64 69 72 65 63 74 6f 72 79 20 3d 20 22 22 7c 22  directory = ""|"
2000c 64 69 72 65 63 74 6f 72 79 5f 6e 61 6d 65 22 0a  directory_name".
2000d 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e    **.  ** Return
2000e 20 6f 72 20 73 65 74 20 74 68 65 20 6c 6f 63 61   or set the loca
2000f 6c 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 74  l value of the t
20010 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74  emp_store_direct
20011 6f 72 79 20 66 6c 61 67 2e 20 20 43 68 61 6e 67  ory flag.  Chang
20012 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 76 61 6c  ing.  ** the val
20013 75 65 20 73 65 74 73 20 61 20 73 70 65 63 69 66  ue sets a specif
20014 69 63 20 64 69 72 65 63 74 6f 72 79 20 74 6f 20  ic directory to 
20015 62 65 20 75 73 65 64 20 66 6f 72 20 74 65 6d 70  be used for temp
20016 6f 72 61 72 79 20 66 69 6c 65 73 2e 0a 20 20 2a  orary files..  *
20017 2a 20 53 65 74 74 69 6e 67 20 74 6f 20 61 20 6e  * Setting to a n
20018 75 6c 6c 20 73 74 72 69 6e 67 20 72 65 76 65 72  ull string rever
20019 74 73 20 74 6f 20 74 68 65 20 64 65 66 61 75 6c  ts to the defaul
2001a 74 20 74 65 6d 70 6f 72 61 72 79 20 64 69 72 65  t temporary dire
2001b 63 74 6f 72 79 20 73 65 61 72 63 68 2e 0a 20 20  ctory search..  
2001c 2a 2a 20 49 66 20 74 65 6d 70 6f 72 61 72 79 20  ** If temporary 
2001d 64 69 72 65 63 74 6f 72 79 20 69 73 20 63 68 61  directory is cha
2001e 6e 67 65 64 2c 20 74 68 65 6e 20 69 6e 76 61 6c  nged, then inval
2001f 69 64 61 74 65 54 65 6d 70 53 74 6f 72 61 67 65  idateTempStorage
20020 2e 0a 20 20 2a 2a 0a 20 20 2a 2f 0a 20 20 69 66  ..  **.  */.  if
20021 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
20022 28 7a 4c 65 66 74 2c 20 22 74 65 6d 70 5f 73 74  (zLeft, "temp_st
20023 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 22 29 3d  ore_directory")=
20024 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21 7a  =0 ){.    if( !z
20025 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 69  Right ){.      i
20026 66 28 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f  f( sqlite3_temp_
20027 64 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20  directory ){.   
20028 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
20029 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29  SetNumCols(v, 1)
2002a 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2002b 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
2002c 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 0, COLNAME_NA
2002d 4d 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  ME, .           
2002e 20 22 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72   "temp_store_dir
2002f 65 63 74 6f 72 79 22 2c 20 50 34 5f 53 54 41 54  ectory", P4_STAT
20030 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  IC);.        sql
20031 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
20032 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
20033 20 31 2c 20 30 2c 20 73 71 6c 69 74 65 33 5f 74   1, 0, sqlite3_t
20034 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 2c 20 30  emp_directory, 0
20035 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
20036 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
20037 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c  OP_ResultRow, 1,
20038 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   1);.      }.   
20039 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66   }else{.      if
2003a 28 20 7a 52 69 67 68 74 5b 30 5d 20 0a 20 20 20  ( zRight[0] .   
2003b 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 4f 73      && sqlite3Os
2003c 41 63 63 65 73 73 28 64 62 2d 3e 70 56 66 73 2c  Access(db->pVfs,
2003d 20 7a 52 69 67 68 74 2c 20 53 51 4c 49 54 45 5f   zRight, SQLITE_
2003e 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 45  ACCESS_READWRITE
2003f 29 3d 3d 30 20 0a 20 20 20 20 20 20 29 7b 0a 20  )==0 .      ){. 
20040 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
20041 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
20042 6e 6f 74 20 61 20 77 72 69 74 61 62 6c 65 20 64  not a writable d
20043 69 72 65 63 74 6f 72 79 22 29 3b 0a 20 20 20 20  irectory");.    
20044 20 20 20 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f      goto pragma_
20045 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  out;.      }.   
20046 20 20 20 69 66 28 20 54 45 4d 50 5f 53 54 4f 52     if( TEMP_STOR
20047 45 3d 3d 30 0a 20 20 20 20 20 20 20 7c 7c 20 28  E==0.       || (
20048 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 31 20 26 26  TEMP_STORE==1 &&
20049 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 3c   db->temp_store<
2004a 3d 31 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 54  =1).       || (T
2004b 45 4d 50 5f 53 54 4f 52 45 3d 3d 32 20 26 26 20  EMP_STORE==2 && 
2004c 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 3d 3d  db->temp_store==
2004d 31 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  1).      ){.    
2004e 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 54 65      invalidateTe
2004f 6d 70 53 74 6f 72 61 67 65 28 70 50 61 72 73 65  mpStorage(pParse
20050 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
20051 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 73 71   sqlite3_free(sq
20052 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63  lite3_temp_direc
20053 74 6f 72 79 29 3b 0a 20 20 20 20 20 20 69 66 28  tory);.      if(
20054 20 7a 52 69 67 68 74 5b 30 5d 20 29 7b 0a 20 20   zRight[0] ){.  
20055 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 74 65        sqlite3_te
20056 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 3d 20 7a  mp_directory = z
20057 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20 7a  Right;.        z
20058 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 20 20 20  Right = 0;.     
20059 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2005a 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72  sqlite3_temp_dir
2005b 65 63 74 6f 72 79 20 3d 20 30 3b 0a 20 20 20 20  ectory = 0;.    
2005c 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73    }.    }.  }els
2005d 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50  e..  /*.  **   P
2005e 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
2005f 5d 73 79 6e 63 68 72 6f 6e 6f 75 73 0a 20 20 2a  ]synchronous.  *
20060 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  *   PRAGMA [data
20061 62 61 73 65 2e 5d 73 79 6e 63 68 72 6f 6e 6f 75  base.]synchronou
20062 73 3d 4f 46 46 7c 4f 4e 7c 4e 4f 52 4d 41 4c 7c  s=OFF|ON|NORMAL|
20063 46 55 4c 4c 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52  FULL.  **.  ** R
20064 65 74 75 72 6e 20 6f 72 20 73 65 74 20 74 68 65  eturn or set the
20065 20 6c 6f 63 61 6c 20 76 61 6c 75 65 20 6f 66 20   local value of 
20066 74 68 65 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20  the synchronous 
20067 66 6c 61 67 2e 20 20 43 68 61 6e 67 69 6e 67 0a  flag.  Changing.
20068 20 20 2a 2a 20 74 68 65 20 6c 6f 63 61 6c 20 76    ** the local v
20069 61 6c 75 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61  alue does not ma
2006a 6b 65 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68  ke changes to th
2006b 65 20 64 69 73 6b 20 66 69 6c 65 20 61 6e 64 20  e disk file and 
2006c 74 68 65 0a 20 20 2a 2a 20 64 65 66 61 75 6c 74  the.  ** default
2006d 20 76 61 6c 75 65 20 77 69 6c 6c 20 62 65 20 72   value will be r
2006e 65 73 74 6f 72 65 64 20 74 68 65 20 6e 65 78 74  estored the next
2006f 20 74 69 6d 65 20 74 68 65 20 64 61 74 61 62 61   time the databa
20070 73 65 20 69 73 0a 20 20 2a 2a 20 6f 70 65 6e 65  se is.  ** opene
20071 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  d..  */.  if( sq
20072 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
20073 66 74 2c 22 73 79 6e 63 68 72 6f 6e 6f 75 73 22  ft,"synchronous"
20074 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  )==0 ){.    if( 
20075 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
20076 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f  a(pParse) ) goto
20077 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20   pragma_out;.   
20078 20 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a   if( !zRight ){.
20079 20 20 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67        returnSing
2007a 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20 22 73  leInt(pParse, "s
2007b 79 6e 63 68 72 6f 6e 6f 75 73 22 2c 20 70 44 62  ynchronous", pDb
2007c 2d 3e 73 61 66 65 74 79 5f 6c 65 76 65 6c 2d 31  ->safety_level-1
2007d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
2007e 20 20 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74      if( !db->aut
2007f 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 20  oCommit ){.     
20080 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
20081 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  sg(pParse, .    
20082 20 20 20 20 20 20 20 20 22 53 61 66 65 74 79 20          "Safety 
20083 6c 65 76 65 6c 20 6d 61 79 20 6e 6f 74 20 62 65  level may not be
20084 20 63 68 61 6e 67 65 64 20 69 6e 73 69 64 65 20   changed inside 
20085 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b  a transaction");
20086 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
20087 20 20 20 20 20 20 70 44 62 2d 3e 73 61 66 65 74        pDb->safet
20088 79 5f 6c 65 76 65 6c 20 3d 20 67 65 74 53 61 66  y_level = getSaf
20089 65 74 79 4c 65 76 65 6c 28 7a 52 69 67 68 74 29  etyLevel(zRight)
2008a 2b 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  +1;.      }.    
2008b 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66  }.  }else.#endif
2008c 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2008d 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f  PAGER_PRAGMAS */
2008e 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2008f 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41 47 4d  _OMIT_FLAG_PRAGM
20090 41 53 0a 20 20 69 66 28 20 66 6c 61 67 50 72 61  AS.  if( flagPra
20091 67 6d 61 28 70 50 61 72 73 65 2c 20 7a 4c 65 66  gma(pParse, zLef
20092 74 2c 20 7a 52 69 67 68 74 29 20 29 7b 0a 20 20  t, zRight) ){.  
20093 20 20 2f 2a 20 54 68 65 20 66 6c 61 67 50 72 61    /* The flagPra
20094 67 6d 61 28 29 20 73 75 62 72 6f 75 74 69 6e 65  gma() subroutine
20095 20 61 6c 73 6f 20 67 65 6e 65 72 61 74 65 73 20   also generates 
20096 61 6e 79 20 6e 65 63 65 73 73 61 72 79 20 63 6f  any necessary co
20097 64 65 0a 20 20 20 20 2a 2a 20 74 68 65 72 65 20  de.    ** there 
20098 69 73 20 6e 6f 74 68 69 6e 67 20 6d 6f 72 65 20  is nothing more 
20099 74 6f 20 64 6f 20 68 65 72 65 20 2a 2f 0a 20 20  to do here */.  
2009a 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20  }else.#endif /* 
2009b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 41 47  SQLITE_OMIT_FLAG
2009c 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66  _PRAGMAS */..#if
2009d 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2009e 5f 53 43 48 45 4d 41 5f 50 52 41 47 4d 41 53 0a  _SCHEMA_PRAGMAS.
2009f 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47    /*.  **   PRAG
200a0 4d 41 20 74 61 62 6c 65 5f 69 6e 66 6f 28 3c 74  MA table_info(<t
200a1 61 62 6c 65 3e 29 0a 20 20 2a 2a 0a 20 20 2a 2a  able>).  **.  **
200a2 20 52 65 74 75 72 6e 20 61 20 73 69 6e 67 6c 65   Return a single
200a3 20 72 6f 77 20 66 6f 72 20 65 61 63 68 20 63 6f   row for each co
200a4 6c 75 6d 6e 20 6f 66 20 74 68 65 20 6e 61 6d 65  lumn of the name
200a5 64 20 74 61 62 6c 65 2e 20 54 68 65 20 63 6f 6c  d table. The col
200a6 75 6d 6e 73 20 6f 66 0a 20 20 2a 2a 20 74 68 65  umns of.  ** the
200a7 20 72 65 74 75 72 6e 65 64 20 64 61 74 61 20 73   returned data s
200a8 65 74 20 61 72 65 3a 0a 20 20 2a 2a 0a 20 20 2a  et are:.  **.  *
200a9 2a 20 63 69 64 3a 20 20 20 20 20 20 20 20 43 6f  * cid:        Co
200aa 6c 75 6d 6e 20 69 64 20 28 6e 75 6d 62 65 72 65  lumn id (numbere
200ab 64 20 66 72 6f 6d 20 6c 65 66 74 20 74 6f 20 72  d from left to r
200ac 69 67 68 74 2c 20 73 74 61 72 74 69 6e 67 20 61  ight, starting a
200ad 74 20 30 29 0a 20 20 2a 2a 20 6e 61 6d 65 3a 20  t 0).  ** name: 
200ae 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 6e 61 6d        Column nam
200af 65 0a 20 20 2a 2a 20 74 79 70 65 3a 20 20 20 20  e.  ** type:    
200b0 20 20 20 43 6f 6c 75 6d 6e 20 64 65 63 6c 61 72     Column declar
200b1 61 74 69 6f 6e 20 74 79 70 65 2e 0a 20 20 2a 2a  ation type..  **
200b2 20 6e 6f 74 6e 75 6c 6c 3a 20 20 20 20 54 72 75   notnull:    Tru
200b3 65 20 69 66 20 27 4e 4f 54 20 4e 55 4c 4c 27 20  e if 'NOT NULL' 
200b4 69 73 20 70 61 72 74 20 6f 66 20 63 6f 6c 75 6d  is part of colum
200b5 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 0a 20 20  n declaration.  
200b6 2a 2a 20 64 66 6c 74 5f 76 61 6c 75 65 3a 20 54  ** dflt_value: T
200b7 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
200b8 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 2c   for the column,
200b9 20 69 66 20 61 6e 79 2e 0a 20 20 2a 2f 0a 20 20   if any..  */.  
200ba 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
200bb 6d 70 28 7a 4c 65 66 74 2c 20 22 74 61 62 6c 65  mp(zLeft, "table
200bc 5f 69 6e 66 6f 22 29 3d 3d 30 20 26 26 20 7a 52  _info")==0 && zR
200bd 69 67 68 74 20 29 7b 0a 20 20 20 20 54 61 62 6c  ight ){.    Tabl
200be 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 69 66 28  e *pTab;.    if(
200bf 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
200c0 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74  ma(pParse) ) got
200c1 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20  o pragma_out;.  
200c2 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
200c3 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 52  FindTable(db, zR
200c4 69 67 68 74 2c 20 7a 44 62 29 3b 0a 20 20 20 20  ight, zDb);.    
200c5 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20  if( pTab ){.    
200c6 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 69    int i;.      i
200c7 6e 74 20 6e 48 69 64 64 65 6e 20 3d 20 30 3b 0a  nt nHidden = 0;.
200c8 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43        Column *pC
200c9 6f 6c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ol;.      sqlite
200ca 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
200cb 76 2c 20 36 29 3b 0a 20 20 20 20 20 20 70 50 61  v, 6);.      pPa
200cc 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 36 3b 0a 20  rse->nMem = 6;. 
200cd 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
200ce 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
200cf 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
200d0 63 69 64 22 2c 20 50 34 5f 53 54 41 54 49 43 29  cid", P4_STATIC)
200d1 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
200d2 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
200d3 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   1, COLNAME_NAME
200d4 2c 20 22 6e 61 6d 65 22 2c 20 50 34 5f 53 54 41  , "name", P4_STA
200d5 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  TIC);.      sqli
200d6 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
200d7 65 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 2, COLNAME_
200d8 4e 41 4d 45 2c 20 22 74 79 70 65 22 2c 20 50 34  NAME, "type", P4
200d9 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
200da 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
200db 6c 4e 61 6d 65 28 76 2c 20 33 2c 20 43 4f 4c 4e  lName(v, 3, COLN
200dc 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 6f 74 6e 75  AME_NAME, "notnu
200dd 6c 6c 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b  ll", P4_STATIC);
200de 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
200df 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
200e0 34 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  4, COLNAME_NAME,
200e1 20 22 64 66 6c 74 5f 76 61 6c 75 65 22 2c 20 50   "dflt_value", P
200e2 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  4_STATIC);.     
200e3 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
200e4 6f 6c 4e 61 6d 65 28 76 2c 20 35 2c 20 43 4f 4c  olName(v, 5, COL
200e5 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 6b 22 2c  NAME_NAME, "pk",
200e6 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P4_STATIC);.   
200e7 20 20 20 73 71 6c 69 74 65 33 56 69 65 77 47 65     sqlite3ViewGe
200e8 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61  tColumnNames(pPa
200e9 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20  rse, pTab);.    
200ea 20 20 66 6f 72 28 69 3d 30 2c 20 70 43 6f 6c 3d    for(i=0, pCol=
200eb 70 54 61 62 2d 3e 61 43 6f 6c 3b 20 69 3c 70 54  pTab->aCol; i<pT
200ec 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70  ab->nCol; i++, p
200ed 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  Col++){.        
200ee 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 44 66  const Token *pDf
200ef 6c 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  lt;.        if( 
200f0 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 70  IsHiddenColumn(p
200f1 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20  Col) ){.        
200f2 20 20 6e 48 69 64 64 65 6e 2b 2b 3b 0a 20 20 20    nHidden++;.   
200f3 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
200f4 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
200f5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
200f6 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
200f7 65 72 2c 20 69 2d 6e 48 69 64 64 65 6e 2c 20 31  er, i-nHidden, 1
200f8 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
200f9 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
200fa 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 32  OP_String8, 0, 2
200fb 2c 20 30 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65  , 0, pCol->zName
200fc 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 0);.        sq
200fd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
200fe 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
200ff 2c 20 33 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  , 3, 0,.        
20100 20 20 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3f     pCol->zType ?
20101 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3a 20 22   pCol->zType : "
20102 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73  ", 0);.        s
20103 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
20104 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
20105 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c 2c 20 34  pCol->notNull, 4
20106 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
20107 43 6f 6c 2d 3e 70 44 66 6c 74 20 26 26 20 28 70  Col->pDflt && (p
20108 44 66 6c 74 20 3d 20 26 70 43 6f 6c 2d 3e 70 44  Dflt = &pCol->pD
20109 66 6c 74 2d 3e 73 70 61 6e 29 2d 3e 7a 20 29 7b  flt->span)->z ){
2010a 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2010b 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2010c 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 35  OP_String8, 0, 5
2010d 2c 20 30 2c 20 28 63 68 61 72 2a 29 70 44 66 6c  , 0, (char*)pDfl
2010e 74 2d 3e 7a 2c 20 70 44 66 6c 74 2d 3e 6e 29 3b  t->z, pDflt->n);
2010f 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
20110 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
20111 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
20112 50 5f 4e 75 6c 6c 2c 20 30 2c 20 35 29 3b 0a 20  P_Null, 0, 5);. 
20113 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
20114 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
20115 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
20116 2c 20 70 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b 65  , pCol->isPrimKe
20117 79 2c 20 36 29 3b 0a 20 20 20 20 20 20 20 20 73  y, 6);.        s
20118 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
20119 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
2011a 2c 20 31 2c 20 36 29 3b 0a 20 20 20 20 20 20 7d  , 1, 6);.      }
2011b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a  .    }.  }else..
2011c 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
2011d 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 69 6e 64  ICmp(zLeft, "ind
2011e 65 78 5f 69 6e 66 6f 22 29 3d 3d 30 20 26 26 20  ex_info")==0 && 
2011f 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 49 6e  zRight ){.    In
20120 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 54  dex *pIdx;.    T
20121 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20  able *pTab;.    
20122 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53  if( sqlite3ReadS
20123 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 20  chema(pParse) ) 
20124 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b  goto pragma_out;
20125 0a 20 20 20 20 70 49 64 78 20 3d 20 73 71 6c 69  .    pIdx = sqli
20126 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c  te3FindIndex(db,
20127 20 7a 52 69 67 68 74 2c 20 7a 44 62 29 3b 0a 20   zRight, zDb);. 
20128 20 20 20 69 66 28 20 70 49 64 78 20 29 7b 0a 20     if( pIdx ){. 
20129 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20       int i;.    
2012a 20 20 70 54 61 62 20 3d 20 70 49 64 78 2d 3e 70    pTab = pIdx->p
2012b 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 73 71 6c  Table;.      sql
2012c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
2012d 6c 73 28 76 2c 20 33 29 3b 0a 20 20 20 20 20 20  ls(v, 3);.      
2012e 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 33  pParse->nMem = 3
2012f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
20130 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
20131 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
20132 2c 20 22 73 65 71 6e 6f 22 2c 20 50 34 5f 53 54  , "seqno", P4_ST
20133 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c  ATIC);.      sql
20134 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
20135 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45  me(v, 1, COLNAME
20136 5f 4e 41 4d 45 2c 20 22 63 69 64 22 2c 20 50 34  _NAME, "cid", P4
20137 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
20138 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
20139 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e  lName(v, 2, COLN
2013a 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22  AME_NAME, "name"
2013b 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
2013c 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
2013d 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  Idx->nColumn; i+
2013e 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  +){.        int 
2013f 63 6e 75 6d 20 3d 20 70 49 64 78 2d 3e 61 69 43  cnum = pIdx->aiC
20140 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20  olumn[i];.      
20141 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
20142 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
20143 72 2c 20 69 2c 20 31 29 3b 0a 20 20 20 20 20 20  r, i, 1);.      
20144 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
20145 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
20146 72 2c 20 63 6e 75 6d 2c 20 32 29 3b 0a 20 20 20  r, cnum, 2);.   
20147 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61       assert( pTa
20148 62 2d 3e 6e 43 6f 6c 3e 63 6e 75 6d 20 29 3b 0a  b->nCol>cnum );.
20149 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2014a 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2014b 53 74 72 69 6e 67 38 2c 20 30 2c 20 33 2c 20 30  String8, 0, 3, 0
2014c 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 63 6e 75  , pTab->aCol[cnu
2014d 6d 5d 2e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  m].zName, 0);.  
2014e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2014f 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
20150 73 75 6c 74 52 6f 77 2c 20 31 2c 20 33 29 3b 0a  sultRow, 1, 3);.
20151 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
20152 7d 65 6c 73 65 0a 0a 20 20 69 66 28 20 73 71 6c  }else..  if( sql
20153 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
20154 74 2c 20 22 69 6e 64 65 78 5f 6c 69 73 74 22 29  t, "index_list")
20155 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29 7b  ==0 && zRight ){
20156 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78  .    Index *pIdx
20157 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61  ;.    Table *pTa
20158 62 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  b;.    if( sqlit
20159 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
2015a 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67  rse) ) goto prag
2015b 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70 54 61 62  ma_out;.    pTab
2015c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61   = sqlite3FindTa
2015d 62 6c 65 28 64 62 2c 20 7a 52 69 67 68 74 2c 20  ble(db, zRight, 
2015e 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70 54  zDb);.    if( pT
2015f 61 62 20 29 7b 0a 20 20 20 20 20 20 76 20 3d 20  ab ){.      v = 
20160 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
20161 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 70 49  Parse);.      pI
20162 64 78 20 3d 20 70 54 61 62 2d 3e 70 49 6e 64 65  dx = pTab->pInde
20163 78 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 64  x;.      if( pId
20164 78 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  x ){.        int
20165 20 69 20 3d 20 30 3b 20 0a 20 20 20 20 20 20 20   i = 0; .       
20166 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
20167 75 6d 43 6f 6c 73 28 76 2c 20 33 29 3b 0a 20 20  umCols(v, 3);.  
20168 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d        pParse->nM
20169 65 6d 20 3d 20 33 3b 0a 20 20 20 20 20 20 20 20  em = 3;.        
2016a 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
2016b 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
2016c 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22 2c  AME_NAME, "seq",
2016d 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P4_STATIC);.   
2016e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2016f 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c  SetColName(v, 1,
20170 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
20171 6e 61 6d 65 22 2c 20 50 34 5f 53 54 41 54 49 43  name", P4_STATIC
20172 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
20173 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
20174 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 2, COLNAME_N
20175 41 4d 45 2c 20 22 75 6e 69 71 75 65 22 2c 20 50  AME, "unique", P
20176 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  4_STATIC);.     
20177 20 20 20 77 68 69 6c 65 28 70 49 64 78 29 7b 0a     while(pIdx){.
20178 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
20179 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2017a 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 31 29  P_Integer, i, 1)
2017b 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
2017c 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2017d 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
2017e 32 2c 20 30 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d  2, 0, pIdx->zNam
2017f 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  e, 0);.         
20180 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
20181 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
20182 2c 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21  , pIdx->onError!
20183 3d 4f 45 5f 4e 6f 6e 65 2c 20 33 29 3b 0a 20 20  =OE_None, 3);.  
20184 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
20185 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
20186 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 33 29  ResultRow, 1, 3)
20187 3b 0a 20 20 20 20 20 20 20 20 20 20 2b 2b 69 3b  ;.          ++i;
20188 0a 20 20 20 20 20 20 20 20 20 20 70 49 64 78 20  .          pIdx 
20189 3d 20 70 49 64 78 2d 3e 70 4e 65 78 74 3b 0a 20  = pIdx->pNext;. 
2018a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
2018b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a  .    }.  }else..
2018c 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
2018d 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 64 61 74  ICmp(zLeft, "dat
2018e 61 62 61 73 65 5f 6c 69 73 74 22 29 3d 3d 30 20  abase_list")==0 
2018f 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  ){.    int i;.  
20190 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61    if( sqlite3Rea
20191 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
20192 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75  ) goto pragma_ou
20193 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  t;.    sqlite3Vd
20194 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
20195 33 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  3);.    pParse->
20196 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20 73 71  nMem = 3;.    sq
20197 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
20198 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
20199 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22 2c 20 50  E_NAME, "seq", P
2019a 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73  4_STATIC);.    s
2019b 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
2019c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41  Name(v, 1, COLNA
2019d 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22 2c  ME_NAME, "name",
2019e 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P4_STATIC);.   
2019f 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
201a0 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f 4c  olName(v, 2, COL
201a1 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 66 69 6c 65  NAME_NAME, "file
201a2 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  ", P4_STATIC);. 
201a3 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62     for(i=0; i<db
201a4 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
201a5 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69     if( db->aDb[i
201a6 5d 2e 70 42 74 3d 3d 30 20 29 20 63 6f 6e 74 69  ].pBt==0 ) conti
201a7 6e 75 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72  nue;.      asser
201a8 74 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e  t( db->aDb[i].zN
201a9 61 6d 65 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  ame!=0 );.      
201aa 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
201ab 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
201ac 20 69 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71   i, 1);.      sq
201ad 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
201ae 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
201af 2c 20 32 2c 20 30 2c 20 64 62 2d 3e 61 44 62 5b  , 2, 0, db->aDb[
201b0 69 5d 2e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  i].zName, 0);.  
201b1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
201b2 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
201b3 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c 0a 20 20  ng8, 0, 3, 0,.  
201b4 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
201b5 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65  BtreeGetFilename
201b6 28 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29  (db->aDb[i].pBt)
201b7 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 0);.      sqli
201b8 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
201b9 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31   OP_ResultRow, 1
201ba 2c 20 33 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  , 3);.    }.  }e
201bb 6c 73 65 0a 0a 20 20 69 66 28 20 73 71 6c 69 74  lse..  if( sqlit
201bc 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
201bd 20 22 63 6f 6c 6c 61 74 69 6f 6e 5f 6c 69 73 74   "collation_list
201be 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  ")==0 ){.    int
201bf 20 69 20 3d 20 30 3b 0a 20 20 20 20 48 61 73 68   i = 0;.    Hash
201c0 45 6c 65 6d 20 2a 70 3b 0a 20 20 20 20 73 71 6c  Elem *p;.    sql
201c1 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
201c2 6c 73 28 76 2c 20 32 29 3b 0a 20 20 20 20 70 50  ls(v, 2);.    pP
201c3 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 32 3b 0a  arse->nMem = 2;.
201c4 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
201c5 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
201c6 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73  COLNAME_NAME, "s
201c7 65 71 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b  eq", P4_STATIC);
201c8 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
201c9 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c  SetColName(v, 1,
201ca 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
201cb 6e 61 6d 65 22 2c 20 50 34 5f 53 54 41 54 49 43  name", P4_STATIC
201cc 29 3b 0a 20 20 20 20 66 6f 72 28 70 3d 73 71 6c  );.    for(p=sql
201cd 69 74 65 48 61 73 68 46 69 72 73 74 28 26 64 62  iteHashFirst(&db
201ce 2d 3e 61 43 6f 6c 6c 53 65 71 29 3b 20 70 3b 20  ->aCollSeq); p; 
201cf 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74  p=sqliteHashNext
201d0 28 70 29 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c  (p)){.      Coll
201d1 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 28 43 6f  Seq *pColl = (Co
201d2 6c 6c 53 65 71 20 2a 29 73 71 6c 69 74 65 48 61  llSeq *)sqliteHa
201d3 73 68 44 61 74 61 28 70 29 3b 0a 20 20 20 20 20  shData(p);.     
201d4 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
201d5 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
201d6 2c 20 69 2b 2b 2c 20 31 29 3b 0a 20 20 20 20 20  , i++, 1);.     
201d7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
201d8 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
201d9 2c 20 30 2c 20 32 2c 20 30 2c 20 70 43 6f 6c 6c  , 0, 2, 0, pColl
201da 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  ->zName, 0);.   
201db 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
201dc 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
201dd 74 52 6f 77 2c 20 31 2c 20 32 29 3b 0a 20 20 20  tRow, 1, 2);.   
201de 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69   }.  }else.#endi
201df 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
201e0 5f 53 43 48 45 4d 41 5f 50 52 41 47 4d 41 53 20  _SCHEMA_PRAGMAS 
201e1 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
201e2 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f  TE_OMIT_FOREIGN_
201e3 4b 45 59 0a 20 20 69 66 28 20 73 71 6c 69 74 65  KEY.  if( sqlite
201e4 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
201e5 22 66 6f 72 65 69 67 6e 5f 6b 65 79 5f 6c 69 73  "foreign_key_lis
201e6 74 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74  t")==0 && zRight
201e7 20 29 7b 0a 20 20 20 20 46 4b 65 79 20 2a 70 46   ){.    FKey *pF
201e8 4b 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  K;.    Table *pT
201e9 61 62 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  ab;.    if( sqli
201ea 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
201eb 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61  arse) ) goto pra
201ec 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70 54 61  gma_out;.    pTa
201ed 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54  b = sqlite3FindT
201ee 61 62 6c 65 28 64 62 2c 20 7a 52 69 67 68 74 2c  able(db, zRight,
201ef 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70   zDb);.    if( p
201f0 54 61 62 20 29 7b 0a 20 20 20 20 20 20 76 20 3d  Tab ){.      v =
201f1 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
201f2 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 70  pParse);.      p
201f3 46 4b 20 3d 20 70 54 61 62 2d 3e 70 46 4b 65 79  FK = pTab->pFKey
201f4 3b 0a 20 20 20 20 20 20 69 66 28 20 70 46 4b 20  ;.      if( pFK 
201f5 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  ){.        int i
201f6 20 3d 20 30 3b 20 0a 20 20 20 20 20 20 20 20 73   = 0; .        s
201f7 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
201f8 43 6f 6c 73 28 76 2c 20 35 29 3b 0a 20 20 20 20  Cols(v, 5);.    
201f9 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d      pParse->nMem
201fa 20 3d 20 35 3b 0a 20 20 20 20 20 20 20 20 73 71   = 5;.        sq
201fb 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
201fc 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
201fd 45 5f 4e 41 4d 45 2c 20 22 69 64 22 2c 20 50 34  E_NAME, "id", P4
201fe 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
201ff 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
20200 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f  ColName(v, 1, CO
20201 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71  LNAME_NAME, "seq
20202 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  ", P4_STATIC);. 
20203 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
20204 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
20205 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  2, COLNAME_NAME,
20206 20 22 74 61 62 6c 65 22 2c 20 50 34 5f 53 54 41   "table", P4_STA
20207 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71  TIC);.        sq
20208 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
20209 61 6d 65 28 76 2c 20 33 2c 20 43 4f 4c 4e 41 4d  ame(v, 3, COLNAM
2020a 45 5f 4e 41 4d 45 2c 20 22 66 72 6f 6d 22 2c 20  E_NAME, "from", 
2020b 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P4_STATIC);.    
2020c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
2020d 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 34 2c 20  etColName(v, 4, 
2020e 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 74  COLNAME_NAME, "t
2020f 6f 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a  o", P4_STATIC);.
20210 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 70 46          while(pF
20211 4b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e  K){.          in
20212 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 66  t j;.          f
20213 6f 72 28 6a 3d 30 3b 20 6a 3c 70 46 4b 2d 3e 6e  or(j=0; j<pFK->n
20214 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  Col; j++){.     
20215 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 43 6f         char *zCo
20216 6c 20 3d 20 70 46 4b 2d 3e 61 43 6f 6c 5b 6a 5d  l = pFK->aCol[j]
20217 2e 7a 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20  .zCol;.         
20218 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
20219 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
2021a 65 72 2c 20 69 2c 20 31 29 3b 0a 20 20 20 20 20  er, i, 1);.     
2021b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2021c 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2021d 6e 74 65 67 65 72 2c 20 6a 2c 20 32 29 3b 0a 20  nteger, j, 2);. 
2021e 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2021f 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
20220 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 33  OP_String8, 0, 3
20221 2c 20 30 2c 20 70 46 4b 2d 3e 7a 54 6f 2c 20 30  , 0, pFK->zTo, 0
20222 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  );.            s
20223 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
20224 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
20225 30 2c 20 34 2c 20 30 2c 0a 20 20 20 20 20 20 20  0, 4, 0,.       
20226 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20227 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61 43 6f         pTab->aCo
20228 6c 5b 70 46 4b 2d 3e 61 43 6f 6c 5b 6a 5d 2e 69  l[pFK->aCol[j].i
20229 46 72 6f 6d 5d 2e 7a 4e 61 6d 65 2c 20 30 29 3b  From].zName, 0);
2022a 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
2022b 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2022c 2c 20 7a 43 6f 6c 20 3f 20 4f 50 5f 53 74 72 69  , zCol ? OP_Stri
2022d 6e 67 38 20 3a 20 4f 50 5f 4e 75 6c 6c 2c 20 30  ng8 : OP_Null, 0
2022e 2c 20 35 2c 20 30 2c 20 7a 43 6f 6c 2c 20 30 29  , 5, 0, zCol, 0)
2022f 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ;.            sq
20230 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
20231 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
20232 20 31 2c 20 35 29 3b 0a 20 20 20 20 20 20 20 20   1, 5);.        
20233 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 2b 2b    }.          ++
20234 69 3b 0a 20 20 20 20 20 20 20 20 20 20 70 46 4b  i;.          pFK
20235 20 3d 20 70 46 4b 2d 3e 70 4e 65 78 74 46 72 6f   = pFK->pNextFro
20236 6d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  m;.        }.   
20237 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c     }.    }.  }el
20238 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  se.#endif /* !de
20239 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2023a 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 29 20 2a  T_FOREIGN_KEY) *
2023b 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  /..#ifndef NDEBU
2023c 47 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  G.  if( sqlite3S
2023d 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 70  trICmp(zLeft, "p
2023e 61 72 73 65 72 5f 74 72 61 63 65 22 29 3d 3d 30  arser_trace")==0
2023f 20 29 7b 0a 20 20 20 20 69 66 28 20 7a 52 69 67   ){.    if( zRig
20240 68 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ht ){.      if( 
20241 67 65 74 42 6f 6f 6c 65 61 6e 28 7a 52 69 67 68  getBoolean(zRigh
20242 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  t) ){.        sq
20243 6c 69 74 65 33 50 61 72 73 65 72 54 72 61 63 65  lite3ParserTrace
20244 28 73 74 64 65 72 72 2c 20 22 70 61 72 73 65 72  (stderr, "parser
20245 3a 20 22 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  : ");.      }els
20246 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e{.        sqlit
20247 65 33 50 61 72 73 65 72 54 72 61 63 65 28 30 2c  e3ParserTrace(0,
20248 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   0);.      }.   
20249 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69   }.  }else.#endi
2024a 66 0a 0a 20 20 2f 2a 20 52 65 69 6e 73 74 61 6c  f..  /* Reinstal
2024b 6c 20 74 68 65 20 4c 49 4b 45 20 61 6e 64 20 47  l the LIKE and G
2024c 4c 4f 42 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20  LOB functions.  
2024d 54 68 65 20 76 61 72 69 61 6e 74 20 6f 66 20 4c  The variant of L
2024e 49 4b 45 0a 20 20 2a 2a 20 75 73 65 64 20 77 69  IKE.  ** used wi
2024f 6c 6c 20 62 65 20 63 61 73 65 20 73 65 6e 73 69  ll be case sensi
20250 74 69 76 65 20 6f 72 20 6e 6f 74 20 64 65 70 65  tive or not depe
20251 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 52 48 53  nding on the RHS
20252 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
20253 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
20254 74 2c 20 22 63 61 73 65 5f 73 65 6e 73 69 74 69  t, "case_sensiti
20255 76 65 5f 6c 69 6b 65 22 29 3d 3d 30 20 29 7b 0a  ve_like")==0 ){.
20256 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 20 29      if( zRight )
20257 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  {.      sqlite3R
20258 65 67 69 73 74 65 72 4c 69 6b 65 46 75 6e 63 74  egisterLikeFunct
20259 69 6f 6e 73 28 64 62 2c 20 67 65 74 42 6f 6f 6c  ions(db, getBool
2025a 65 61 6e 28 7a 52 69 67 68 74 29 29 3b 0a 20 20  ean(zRight));.  
2025b 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 23 69 66    }.  }else..#if
2025c 6e 64 65 66 20 53 51 4c 49 54 45 5f 49 4e 54 45  ndef SQLITE_INTE
2025d 47 52 49 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f  GRITY_CHECK_ERRO
2025e 52 5f 4d 41 58 0a 23 20 64 65 66 69 6e 65 20 53  R_MAX.# define S
2025f 51 4c 49 54 45 5f 49 4e 54 45 47 52 49 54 59 5f  QLITE_INTEGRITY_
20260 43 48 45 43 4b 5f 45 52 52 4f 52 5f 4d 41 58 20  CHECK_ERROR_MAX 
20261 31 30 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  100.#endif..#ifn
20262 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
20263 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a  INTEGRITY_CHECK.
20264 20 20 2f 2a 20 50 72 61 67 6d 61 20 22 71 75 69    /* Pragma "qui
20265 63 6b 5f 63 68 65 63 6b 22 20 69 73 20 61 6e 20  ck_check" is an 
20266 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 72 65 64  experimental red
20267 75 63 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20  uced version of 
20268 0a 20 20 2a 2a 20 69 6e 74 65 67 72 69 74 79 5f  .  ** integrity_
20269 63 68 65 63 6b 20 64 65 73 69 67 6e 65 64 20 74  check designed t
2026a 6f 20 64 65 74 65 63 74 20 6d 6f 73 74 20 64 61  o detect most da
2026b 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f  tabase corruptio
2026c 6e 0a 20 20 2a 2a 20 77 69 74 68 6f 75 74 20 6d  n.  ** without m
2026d 6f 73 74 20 6f 66 20 74 68 65 20 6f 76 65 72 68  ost of the overh
2026e 65 61 64 20 6f 66 20 61 20 66 75 6c 6c 20 69 6e  ead of a full in
2026f 74 65 67 72 69 74 79 2d 63 68 65 63 6b 2e 0a 20  tegrity-check.. 
20270 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
20271 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
20272 22 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b  "integrity_check
20273 22 29 3d 3d 30 0a 20 20 20 7c 7c 20 73 71 6c 69  ")==0.   || sqli
20274 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
20275 2c 20 22 71 75 69 63 6b 5f 63 68 65 63 6b 22 29  , "quick_check")
20276 3d 3d 30 20 0a 20 20 29 7b 0a 20 20 20 20 69 6e  ==0 .  ){.    in
20277 74 20 69 2c 20 6a 2c 20 61 64 64 72 2c 20 6d 78  t i, j, addr, mx
20278 45 72 72 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 64  Err;..    /* Cod
20279 65 20 74 68 61 74 20 61 70 70 65 61 72 73 20 61  e that appears a
2027a 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
2027b 20 69 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b   integrity check
2027c 2e 20 20 49 66 20 6e 6f 20 65 72 72 6f 72 0a 20  .  If no error. 
2027d 20 20 20 2a 2a 20 6d 65 73 73 61 67 65 73 20 68     ** messages h
2027e 61 76 65 20 62 65 65 6e 20 67 65 6e 65 72 61 74  ave been generat
2027f 65 64 2c 20 6f 75 74 70 75 74 20 4f 4b 2e 20 20  ed, output OK.  
20280 4f 74 68 65 72 77 69 73 65 20 6f 75 74 70 75 74  Otherwise output
20281 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 72 72 6f   the.    ** erro
20282 72 20 6d 65 73 73 61 67 65 0a 20 20 20 20 2a 2f  r message.    */
20283 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73  .    static cons
20284 74 20 56 64 62 65 4f 70 4c 69 73 74 20 65 6e 64  t VdbeOpList end
20285 43 6f 64 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  Code[] = {.     
20286 20 7b 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 20 20   { OP_AddImm,   
20287 20 20 20 31 2c 20 30 2c 20 20 20 20 20 20 20 20     1, 0,        
20288 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a 2f 0a 20  0},    /* 0 */. 
20289 20 20 20 20 20 7b 20 4f 50 5f 49 66 4e 65 67 2c       { OP_IfNeg,
2028a 20 20 20 20 20 20 20 31 2c 20 30 2c 20 20 20 20         1, 0,    
2028b 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20 31 20      0},    /* 1 
2028c 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53 74  */.      { OP_St
2028d 72 69 6e 67 38 2c 20 20 20 20 20 30 2c 20 33 2c  ring8,     0, 3,
2028e 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20 2f          0},    /
2028f 2a 20 32 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f  * 2 */.      { O
20290 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 20 20 33  P_ResultRow,   3
20291 2c 20 31 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a  , 1,        0},.
20292 20 20 20 20 7d 3b 0a 0a 20 20 20 20 69 6e 74 20      };..    int 
20293 69 73 51 75 69 63 6b 20 3d 20 28 7a 4c 65 66 74  isQuick = (zLeft
20294 5b 30 5d 3d 3d 27 71 27 29 3b 0a 0a 20 20 20 20  [0]=='q');..    
20295 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  /* Initialize th
20296 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 2a  e VDBE program *
20297 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  /.    if( sqlite
20298 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
20299 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d  se) ) goto pragm
2029a 61 5f 6f 75 74 3b 0a 20 20 20 20 70 50 61 72 73  a_out;.    pPars
2029b 65 2d 3e 6e 4d 65 6d 20 3d 20 36 3b 0a 20 20 20  e->nMem = 6;.   
2029c 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
2029d 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20  umCols(v, 1);.  
2029e 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
2029f 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
202a0 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 69 6e 74  LNAME_NAME, "int
202a1 65 67 72 69 74 79 5f 63 68 65 63 6b 22 2c 20 50  egrity_check", P
202a2 34 5f 53 54 41 54 49 43 29 3b 0a 0a 20 20 20 20  4_STATIC);..    
202a3 2f 2a 20 53 65 74 20 74 68 65 20 6d 61 78 69 6d  /* Set the maxim
202a4 75 6d 20 65 72 72 6f 72 20 63 6f 75 6e 74 20 2a  um error count *
202a5 2f 0a 20 20 20 20 6d 78 45 72 72 20 3d 20 53 51  /.    mxErr = SQ
202a6 4c 49 54 45 5f 49 4e 54 45 47 52 49 54 59 5f 43  LITE_INTEGRITY_C
202a7 48 45 43 4b 5f 45 52 52 4f 52 5f 4d 41 58 3b 0a  HECK_ERROR_MAX;.
202a8 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 20 29      if( zRight )
202a9 7b 0a 20 20 20 20 20 20 6d 78 45 72 72 20 3d 20  {.      mxErr = 
202aa 61 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20 20  atoi(zRight);.  
202ab 20 20 20 20 69 66 28 20 6d 78 45 72 72 3c 3d 30      if( mxErr<=0
202ac 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 78 45 72   ){.        mxEr
202ad 72 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47  r = SQLITE_INTEG
202ae 52 49 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f 52  RITY_CHECK_ERROR
202af 5f 4d 41 58 3b 0a 20 20 20 20 20 20 7d 0a 20 20  _MAX;.      }.  
202b0 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
202b1 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
202b2 49 6e 74 65 67 65 72 2c 20 6d 78 45 72 72 2c 20  Integer, mxErr, 
202b3 31 29 3b 20 20 2f 2a 20 72 65 67 5b 31 5d 20 68  1);  /* reg[1] h
202b4 6f 6c 64 73 20 65 72 72 6f 72 73 20 6c 65 66 74  olds errors left
202b5 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 61   */..    /* Do a
202b6 6e 20 69 6e 74 65 67 72 69 74 79 20 63 68 65 63  n integrity chec
202b7 6b 20 6f 6e 20 65 61 63 68 20 64 61 74 61 62 61  k on each databa
202b8 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 66  se file */.    f
202b9 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
202ba 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 48  b; i++){.      H
202bb 61 73 68 45 6c 65 6d 20 2a 78 3b 0a 20 20 20 20  ashElem *x;.    
202bc 20 20 48 61 73 68 20 2a 70 54 62 6c 73 3b 0a 20    Hash *pTbls;. 
202bd 20 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 30       int cnt = 0
202be 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 4f 4d 49  ;..      if( OMI
202bf 54 5f 54 45 4d 50 44 42 20 26 26 20 69 3d 3d 31  T_TEMPDB && i==1
202c0 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20   ) continue;..  
202c1 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56      sqlite3CodeV
202c2 65 72 69 66 79 53 63 68 65 6d 61 28 70 50 61 72  erifySchema(pPar
202c3 73 65 2c 20 69 29 3b 0a 20 20 20 20 20 20 61 64  se, i);.      ad
202c4 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
202c5 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 50  AddOp1(v, OP_IfP
202c6 6f 73 2c 20 31 29 3b 20 2f 2a 20 48 61 6c 74 20  os, 1); /* Halt 
202c7 69 66 20 6f 75 74 20 6f 66 20 65 72 72 6f 72 73  if out of errors
202c8 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
202c9 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
202ca 50 5f 48 61 6c 74 2c 20 30 2c 20 30 29 3b 0a 20  P_Halt, 0, 0);. 
202cb 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
202cc 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
202cd 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 44 6f 20  );..      /* Do 
202ce 61 6e 20 69 6e 74 65 67 72 69 74 79 20 63 68 65  an integrity che
202cf 63 6b 20 6f 66 20 74 68 65 20 42 2d 54 72 65 65  ck of the B-Tree
202d0 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
202d1 2a 2a 20 42 65 67 69 6e 20 62 79 20 66 69 6c 6c  ** Begin by fill
202d2 69 6e 67 20 72 65 67 69 73 74 65 72 73 20 32 2c  ing registers 2,
202d3 20 33 2c 20 2e 2e 2e 20 77 69 74 68 20 74 68 65   3, ... with the
202d4 20 72 6f 6f 74 20 70 61 67 65 73 20 6e 75 6d 62   root pages numb
202d5 65 72 73 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72  ers.      ** for
202d6 20 61 6c 6c 20 74 61 62 6c 65 73 20 61 6e 64 20   all tables and 
202d7 69 6e 64 69 63 65 73 20 69 6e 20 74 68 65 20 64  indices in the d
202d8 61 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 2a  atabase..      *
202d9 2f 0a 20 20 20 20 20 20 70 54 62 6c 73 20 3d 20  /.      pTbls = 
202da 26 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 53 63 68  &db->aDb[i].pSch
202db 65 6d 61 2d 3e 74 62 6c 48 61 73 68 3b 0a 20 20  ema->tblHash;.  
202dc 20 20 20 20 66 6f 72 28 78 3d 73 71 6c 69 74 65      for(x=sqlite
202dd 48 61 73 68 46 69 72 73 74 28 70 54 62 6c 73 29  HashFirst(pTbls)
202de 3b 20 78 3b 20 78 3d 73 71 6c 69 74 65 48 61 73  ; x; x=sqliteHas
202df 68 4e 65 78 74 28 78 29 29 7b 0a 20 20 20 20 20  hNext(x)){.     
202e0 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
202e1 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28   sqliteHashData(
202e2 78 29 3b 0a 20 20 20 20 20 20 20 20 49 6e 64 65  x);.        Inde
202e3 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 20 20 20  x *pIdx;.       
202e4 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
202e5 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
202e6 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 32 2b  , pTab->tnum, 2+
202e7 63 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 63 6e  cnt);.        cn
202e8 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  t++;.        for
202e9 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64  (pIdx=pTab->pInd
202ea 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
202eb 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Idx->pNext){.   
202ec 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
202ed 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
202ee 6e 74 65 67 65 72 2c 20 70 49 64 78 2d 3e 74 6e  nteger, pIdx->tn
202ef 75 6d 2c 20 32 2b 63 6e 74 29 3b 0a 20 20 20 20  um, 2+cnt);.    
202f0 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20        cnt++;.   
202f1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
202f2 20 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20       if( cnt==0 
202f3 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20 20  ) continue;..   
202f4 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20     /* Make sure 
202f5 73 75 66 66 69 63 69 65 6e 74 20 6e 75 6d 62 65  sufficient numbe
202f6 72 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 68  r of registers h
202f7 61 76 65 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74  ave been allocat
202f8 65 64 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  ed */.      if( 
202f9 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3c 20 63  pParse->nMem < c
202fa 6e 74 2b 34 20 29 7b 0a 20 20 20 20 20 20 20 20  nt+4 ){.        
202fb 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 63  pParse->nMem = c
202fc 6e 74 2b 34 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  nt+4;.      }.. 
202fd 20 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 62       /* Do the b
202fe 2d 74 72 65 65 20 69 6e 74 65 67 72 69 74 79 20  -tree integrity 
202ff 63 68 65 63 6b 73 20 2a 2f 0a 20 20 20 20 20 20  checks */.      
20300 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
20301 33 28 76 2c 20 4f 50 5f 49 6e 74 65 67 72 69 74  3(v, OP_Integrit
20302 79 43 6b 2c 20 32 2c 20 63 6e 74 2c 20 31 29 3b  yCk, 2, cnt, 1);
20303 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
20304 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 69 29  beChangeP5(v, i)
20305 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73  ;.      addr = s
20306 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
20307 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 32  (v, OP_IsNull, 2
20308 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
20309 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2030a 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 33 2c 20  _String8, 0, 3, 
2030b 30 2c 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69  0,.         sqli
2030c 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
2030d 2a 2a 2a 20 69 6e 20 64 61 74 61 62 61 73 65 20  *** in database 
2030e 25 73 20 2a 2a 2a 5c 6e 22 2c 20 64 62 2d 3e 61  %s ***\n", db->a
2030f 44 62 5b 69 5d 2e 7a 4e 61 6d 65 29 2c 0a 20 20  Db[i].zName),.  
20310 20 20 20 20 20 20 20 50 34 5f 44 59 4e 41 4d 49         P4_DYNAMI
20311 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  C);.      sqlite
20312 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
20313 50 5f 4d 6f 76 65 2c 20 32 2c 20 34 29 3b 0a 20  P_Move, 2, 4);. 
20314 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
20315 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6e  AddOp3(v, OP_Con
20316 63 61 74 2c 20 34 2c 20 33 2c 20 32 29 3b 0a 20  cat, 4, 3, 2);. 
20317 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
20318 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73  AddOp2(v, OP_Res
20319 75 6c 74 52 6f 77 2c 20 32 2c 20 31 29 3b 0a 20  ultRow, 2, 1);. 
2031a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2031b 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
2031c 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4d 61 6b  );..      /* Mak
2031d 65 20 73 75 72 65 20 61 6c 6c 20 74 68 65 20 69  e sure all the i
2031e 6e 64 69 63 65 73 20 61 72 65 20 63 6f 6e 73 74  ndices are const
2031f 72 75 63 74 65 64 20 63 6f 72 72 65 63 74 6c 79  ructed correctly
20320 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
20321 20 66 6f 72 28 78 3d 73 71 6c 69 74 65 48 61 73   for(x=sqliteHas
20322 68 46 69 72 73 74 28 70 54 62 6c 73 29 3b 20 78  hFirst(pTbls); x
20323 20 26 26 20 21 69 73 51 75 69 63 6b 3b 20 78 3d   && !isQuick; x=
20324 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 78  sqliteHashNext(x
20325 29 29 7b 0a 20 20 20 20 20 20 20 20 54 61 62 6c  )){.        Tabl
20326 65 20 2a 70 54 61 62 20 3d 20 73 71 6c 69 74 65  e *pTab = sqlite
20327 48 61 73 68 44 61 74 61 28 78 29 3b 0a 20 20 20  HashData(x);.   
20328 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78       Index *pIdx
20329 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6c 6f  ;.        int lo
2032a 6f 70 54 6f 70 3b 0a 0a 20 20 20 20 20 20 20 20  opTop;..        
2032b 69 66 28 20 70 54 61 62 2d 3e 70 49 6e 64 65 78  if( pTab->pIndex
2032c 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==0 ) continue;.
2032d 20 20 20 20 20 20 20 20 61 64 64 72 20 3d 20 73          addr = s
2032e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2032f 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 31 29  (v, OP_IfPos, 1)
20330 3b 20 20 2f 2a 20 53 74 6f 70 20 69 66 20 6f 75  ;  /* Stop if ou
20331 74 20 6f 66 20 65 72 72 6f 72 73 20 2a 2f 0a 20  t of errors */. 
20332 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
20333 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 48  beAddOp2(v, OP_H
20334 61 6c 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  alt, 0, 0);.    
20335 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
20336 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29  umpHere(v, addr)
20337 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
20338 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64  3OpenTableAndInd
20339 69 63 65 73 28 70 50 61 72 73 65 2c 20 70 54 61  ices(pParse, pTa
2033a 62 2c 20 31 2c 20 4f 50 5f 4f 70 65 6e 52 65 61  b, 1, OP_OpenRea
2033b 64 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  d);.        sqli
2033c 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2033d 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20   OP_Integer, 0, 
2033e 32 29 3b 20 20 2f 2a 20 72 65 67 28 32 29 20 77  2);  /* reg(2) w
2033f 69 6c 6c 20 63 6f 75 6e 74 20 65 6e 74 72 69 65  ill count entrie
20340 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 6c 6f 6f  s */.        loo
20341 70 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64  pTop = sqlite3Vd
20342 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
20343 65 77 69 6e 64 2c 20 31 2c 20 30 29 3b 0a 20 20  ewind, 1, 0);.  
20344 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
20345 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64  eAddOp2(v, OP_Ad
20346 64 49 6d 6d 2c 20 32 2c 20 31 29 3b 20 20 20 2f  dImm, 2, 1);   /
20347 2a 20 69 6e 63 72 65 6d 65 6e 74 20 65 6e 74 72  * increment entr
20348 79 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 20  y count */.     
20349 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 64 78     for(j=0, pIdx
2034a 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
2034b 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
2034c 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a 20 20 20  pNext, j++){.   
2034d 20 20 20 20 20 20 20 69 6e 74 20 6a 6d 70 32 3b         int jmp2;
2034e 0a 20 20 20 20 20 20 20 20 20 20 73 74 61 74 69  .          stati
2034f 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69  c const VdbeOpLi
20350 73 74 20 69 64 78 45 72 72 5b 5d 20 3d 20 7b 0a  st idxErr[] = {.
20351 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
20352 5f 41 64 64 49 6d 6d 2c 20 20 20 20 20 20 31 2c  _AddImm,      1,
20353 20 2d 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20   -1,  0},.      
20354 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e        { OP_Strin
20355 67 38 2c 20 20 20 20 20 30 2c 20 20 33 2c 20 20  g8,     0,  3,  
20356 30 7d 2c 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20  0},    /* 1 */. 
20357 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
20358 52 6f 77 69 64 2c 20 20 20 20 20 20 20 31 2c 20  Rowid,       1, 
20359 20 34 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20   4,  0},.       
2035a 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67       { OP_String
2035b 38 2c 20 20 20 20 20 30 2c 20 20 35 2c 20 20 30  8,     0,  5,  0
2035c 7d 2c 20 20 20 20 2f 2a 20 33 20 2a 2f 0a 20 20  },    /* 3 */.  
2035d 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53            { OP_S
2035e 74 72 69 6e 67 38 2c 20 20 20 20 20 30 2c 20 20  tring8,     0,  
2035f 36 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 34 20  6,  0},    /* 4 
20360 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b  */.            {
20361 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 20 20 20 20   OP_Concat,     
20362 20 34 2c 20 20 33 2c 20 20 33 7d 2c 0a 20 20 20   4,  3,  3},.   
20363 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f           { OP_Co
20364 6e 63 61 74 2c 20 20 20 20 20 20 35 2c 20 20 33  ncat,      5,  3
20365 2c 20 20 33 7d 2c 0a 20 20 20 20 20 20 20 20 20  ,  3},.         
20366 20 20 20 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c 20     { OP_Concat, 
20367 20 20 20 20 20 36 2c 20 20 33 2c 20 20 33 7d 2c       6,  3,  3},
20368 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
20369 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 20 20 33  P_ResultRow,   3
2036a 2c 20 20 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20  ,  1,  0},.     
2036b 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 66 50 6f         { OP_IfPo
2036c 73 2c 20 20 20 20 20 20 20 31 2c 20 20 30 2c 20  s,       1,  0, 
2036d 20 30 7d 2c 20 20 20 20 2f 2a 20 39 20 2a 2f 0a   0},    /* 9 */.
2036e 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
2036f 5f 48 61 6c 74 2c 20 20 20 20 20 20 20 20 30 2c  _Halt,        0,
20370 20 20 30 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20    0,  0},.      
20371 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 20 20 20      };.         
20372 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
20373 49 6e 64 65 78 4b 65 79 28 70 50 61 72 73 65 2c  IndexKey(pParse,
20374 20 70 49 64 78 2c 20 31 2c 20 33 2c 20 31 29 3b   pIdx, 1, 3, 1);
20375 0a 20 20 20 20 20 20 20 20 20 20 6a 6d 70 32 20  .          jmp2 
20376 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
20377 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c  Op3(v, OP_Found,
20378 20 6a 2b 32 2c 20 30 2c 20 33 29 3b 0a 20 20 20   j+2, 0, 3);.   
20379 20 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71         addr = sq
2037a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69  lite3VdbeAddOpLi
2037b 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28  st(v, ArraySize(
2037c 69 64 78 45 72 72 29 2c 20 69 64 78 45 72 72 29  idxErr), idxErr)
2037d 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
2037e 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
2037f 76 2c 20 61 64 64 72 2b 31 2c 20 22 72 6f 77 69  v, addr+1, "rowi
20380 64 20 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b  d ", P4_STATIC);
20381 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
20382 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
20383 2c 20 61 64 64 72 2b 33 2c 20 22 20 6d 69 73 73  , addr+3, " miss
20384 69 6e 67 20 66 72 6f 6d 20 69 6e 64 65 78 20 22  ing from index "
20385 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
20386 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
20387 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61  dbeChangeP4(v, a
20388 64 64 72 2b 34 2c 20 70 49 64 78 2d 3e 7a 4e 61  ddr+4, pIdx->zNa
20389 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a  me, P4_STATIC);.
2038a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2038b 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2038c 20 61 64 64 72 2b 39 29 3b 0a 20 20 20 20 20 20   addr+9);.      
2038d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
2038e 75 6d 70 48 65 72 65 28 76 2c 20 6a 6d 70 32 29  umpHere(v, jmp2)
2038f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
20390 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
20391 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74  ddOp2(v, OP_Next
20392 2c 20 31 2c 20 6c 6f 6f 70 54 6f 70 2b 31 29 3b  , 1, loopTop+1);
20393 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
20394 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
20395 6c 6f 6f 70 54 6f 70 29 3b 0a 20 20 20 20 20 20  loopTop);.      
20396 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 64 78 3d    for(j=0, pIdx=
20397 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
20398 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  dx; pIdx=pIdx->p
20399 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20  Next, j++){.    
2039a 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e        static con
2039b 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 63 6e  st VdbeOpList cn
2039c 74 49 64 78 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  tIdx[] = {.     
2039d 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 6e 74          { OP_Int
2039e 65 67 65 72 2c 20 20 20 20 20 20 30 2c 20 20 33  eger,      0,  3
2039f 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20  ,  0},.         
203a0 20 20 20 20 7b 20 4f 50 5f 52 65 77 69 6e 64 2c      { OP_Rewind,
203a1 20 20 20 20 20 20 20 30 2c 20 20 30 2c 20 20 30         0,  0,  0
203a2 7d 2c 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20  },  /* 1 */.    
203a3 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 41 64           { OP_Ad
203a4 64 49 6d 6d 2c 20 20 20 20 20 20 20 33 2c 20 20  dImm,       3,  
203a5 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20 20  1,  0},.        
203a6 20 20 20 20 20 7b 20 4f 50 5f 4e 65 78 74 2c 20       { OP_Next, 
203a7 20 20 20 20 20 20 20 20 30 2c 20 20 30 2c 20 20          0,  0,  
203a8 30 7d 2c 20 20 2f 2a 20 33 20 2a 2f 0a 20 20 20  0},  /* 3 */.   
203a9 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 45            { OP_E
203aa 71 2c 20 20 20 20 20 20 20 20 20 20 20 32 2c 20  q,           2, 
203ab 20 30 2c 20 20 33 7d 2c 20 20 2f 2a 20 34 20 2a   0,  3},  /* 4 *
203ac 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7b  /.             {
203ad 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 20 20 20 20   OP_AddImm,     
203ae 20 20 31 2c 20 2d 31 2c 20 20 30 7d 2c 0a 20 20    1, -1,  0},.  
203af 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
203b0 53 74 72 69 6e 67 38 2c 20 20 20 20 20 20 30 2c  String8,      0,
203b1 20 20 32 2c 20 20 30 7d 2c 20 20 2f 2a 20 36 20    2,  0},  /* 6 
203b2 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  */.             
203b3 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20  { OP_String8,   
203b4 20 20 20 30 2c 20 20 33 2c 20 20 30 7d 2c 20 20     0,  3,  0},  
203b5 2f 2a 20 37 20 2a 2f 0a 20 20 20 20 20 20 20 20  /* 7 */.        
203b6 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6e 63 61 74       { OP_Concat
203b7 2c 20 20 20 20 20 20 20 33 2c 20 20 32 2c 20 20  ,       3,  2,  
203b8 32 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  2},.            
203b9 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c   { OP_ResultRow,
203ba 20 20 20 20 32 2c 20 20 31 2c 20 20 30 7d 2c 0a      2,  1,  0},.
203bb 20 20 20 20 20 20 20 20 20 20 7d 3b 0a 20 20 20            };.   
203bc 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d         if( pIdx-
203bd 3e 74 6e 75 6d 3d 3d 30 20 29 20 63 6f 6e 74 69  >tnum==0 ) conti
203be 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 61  nue;.          a
203bf 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
203c0 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
203c1 50 6f 73 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  Pos, 1);.       
203c2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
203c3 64 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c  dOp2(v, OP_Halt,
203c4 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   0, 0);.        
203c5 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
203c6 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a  pHere(v, addr);.
203c7 20 20 20 20 20 20 20 20 20 20 61 64 64 72 20 3d            addr =
203c8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
203c9 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53 69  pList(v, ArraySi
203ca 7a 65 28 63 6e 74 49 64 78 29 2c 20 63 6e 74 49  ze(cntIdx), cntI
203cb 64 78 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  dx);.          s
203cc 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
203cd 50 31 28 76 2c 20 61 64 64 72 2b 31 2c 20 6a 2b  P1(v, addr+1, j+
203ce 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  2);.          sq
203cf 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
203d0 32 28 76 2c 20 61 64 64 72 2b 31 2c 20 61 64 64  2(v, addr+1, add
203d1 72 2b 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20  r+4);.          
203d2 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
203d3 65 50 31 28 76 2c 20 61 64 64 72 2b 33 2c 20 6a  eP1(v, addr+3, j
203d4 2b 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  +2);.          s
203d5 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
203d6 50 32 28 76 2c 20 61 64 64 72 2b 33 2c 20 61 64  P2(v, addr+3, ad
203d7 64 72 2b 32 29 3b 0a 20 20 20 20 20 20 20 20 20  dr+2);.         
203d8 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
203d9 48 65 72 65 28 76 2c 20 61 64 64 72 2b 34 29 3b  Here(v, addr+4);
203da 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
203db 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
203dc 2c 20 61 64 64 72 2b 36 2c 20 0a 20 20 20 20 20  , addr+6, .     
203dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
203de 22 77 72 6f 6e 67 20 23 20 6f 66 20 65 6e 74 72  "wrong # of entr
203df 69 65 73 20 69 6e 20 69 6e 64 65 78 20 22 2c 20  ies in index ", 
203e0 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P4_STATIC);.    
203e1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
203e2 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64  eChangeP4(v, add
203e3 72 2b 37 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65  r+7, pIdx->zName
203e4 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
203e5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20        }.      } 
203e6 0a 20 20 20 20 7d 0a 20 20 20 20 61 64 64 72 20  .    }.    addr 
203e7 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
203e8 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53  OpList(v, ArrayS
203e9 69 7a 65 28 65 6e 64 43 6f 64 65 29 2c 20 65 6e  ize(endCode), en
203ea 64 43 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69  dCode);.    sqli
203eb 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28  te3VdbeChangeP2(
203ec 76 2c 20 61 64 64 72 2c 20 2d 6d 78 45 72 72 29  v, addr, -mxErr)
203ed 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
203ee 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
203ef 72 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  r+1);.    sqlite
203f0 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
203f1 20 61 64 64 72 2b 32 2c 20 22 6f 6b 22 2c 20 50   addr+2, "ok", P
203f2 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 65 6c  4_STATIC);.  }el
203f3 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  se.#endif /* SQL
203f4 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49  ITE_OMIT_INTEGRI
203f5 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66  TY_CHECK */..#if
203f6 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
203f7 5f 55 54 46 31 36 0a 20 20 2f 2a 0a 20 20 2a 2a  _UTF16.  /*.  **
203f8 20 20 20 50 52 41 47 4d 41 20 65 6e 63 6f 64 69     PRAGMA encodi
203f9 6e 67 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41  ng.  **   PRAGMA
203fa 20 65 6e 63 6f 64 69 6e 67 20 3d 20 22 75 74 66   encoding = "utf
203fb 2d 38 22 7c 22 75 74 66 2d 31 36 22 7c 22 75 74  -8"|"utf-16"|"ut
203fc 66 2d 31 36 6c 65 22 7c 22 75 74 66 2d 31 36 62  f-16le"|"utf-16b
203fd 65 22 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20  e".  **.  ** In 
203fe 69 74 73 20 66 69 72 73 74 20 66 6f 72 6d 2c 20  its first form, 
203ff 74 68 69 73 20 70 72 61 67 6d 61 20 72 65 74 75  this pragma retu
20400 72 6e 73 20 74 68 65 20 65 6e 63 6f 64 69 6e 67  rns the encoding
20401 20 6f 66 20 74 68 65 20 6d 61 69 6e 0a 20 20 2a   of the main.  *
20402 2a 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 74  * database. If t
20403 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6e  he database is n
20404 6f 74 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20  ot initialized, 
20405 69 74 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65  it is initialize
20406 64 20 6e 6f 77 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  d now..  **.  **
20407 20 54 68 65 20 73 65 63 6f 6e 64 20 66 6f 72 6d   The second form
20408 20 6f 66 20 74 68 69 73 20 70 72 61 67 6d 61 20   of this pragma 
20409 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68  is a no-op if th
2040a 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
2040b 66 69 6c 65 0a 20 20 2a 2a 20 68 61 73 20 6e 6f  file.  ** has no
2040c 74 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69  t already been i
2040d 6e 69 74 69 61 6c 69 7a 65 64 2e 20 49 6e 20 74  nitialized. In t
2040e 68 69 73 20 63 61 73 65 20 69 74 20 73 65 74 73  his case it sets
2040f 20 74 68 65 20 64 65 66 61 75 6c 74 0a 20 20 2a   the default.  *
20410 2a 20 65 6e 63 6f 64 69 6e 67 20 74 68 61 74 20  * encoding that 
20411 77 69 6c 6c 20 62 65 20 75 73 65 64 20 66 6f 72  will be used for
20412 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
20413 73 65 20 66 69 6c 65 20 69 66 20 61 20 6e 65 77  se file if a new
20414 20 66 69 6c 65 0a 20 20 2a 2a 20 69 73 20 63 72   file.  ** is cr
20415 65 61 74 65 64 2e 20 49 66 20 61 6e 20 65 78 69  eated. If an exi
20416 73 74 69 6e 67 20 6d 61 69 6e 20 64 61 74 61 62  sting main datab
20417 61 73 65 20 66 69 6c 65 20 69 73 20 6f 70 65 6e  ase file is open
20418 65 64 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 2a  ed, then the.  *
20419 2a 20 64 65 66 61 75 6c 74 20 74 65 78 74 20 65  * default text e
2041a 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20  ncoding for the 
2041b 65 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 73  existing databas
2041c 65 20 69 73 20 75 73 65 64 2e 0a 20 20 2a 2a 20  e is used..  ** 
2041d 0a 20 20 2a 2a 20 49 6e 20 61 6c 6c 20 63 61 73  .  ** In all cas
2041e 65 73 20 6e 65 77 20 64 61 74 61 62 61 73 65 73  es new databases
2041f 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 74   created using t
20420 68 65 20 41 54 54 41 43 48 20 63 6f 6d 6d 61 6e  he ATTACH comman
20421 64 20 61 72 65 0a 20 20 2a 2a 20 63 72 65 61 74  d are.  ** creat
20422 65 64 20 74 6f 20 75 73 65 20 74 68 65 20 73 61  ed to use the sa
20423 6d 65 20 64 65 66 61 75 6c 74 20 74 65 78 74 20  me default text 
20424 65 6e 63 6f 64 69 6e 67 20 61 73 20 74 68 65 20  encoding as the 
20425 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20 49  main database. I
20426 66 0a 20 20 2a 2a 20 74 68 65 20 6d 61 69 6e 20  f.  ** the main 
20427 64 61 74 61 62 61 73 65 20 68 61 73 20 6e 6f 74  database has not
20428 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65   been initialize
20429 64 20 61 6e 64 2f 6f 72 20 63 72 65 61 74 65 64  d and/or created
2042a 20 77 68 65 6e 20 41 54 54 41 43 48 0a 20 20 2a   when ATTACH.  *
2042b 2a 20 69 73 20 65 78 65 63 75 74 65 64 2c 20 74  * is executed, t
2042c 68 69 73 20 69 73 20 64 6f 6e 65 20 62 65 66 6f  his is done befo
2042d 72 65 20 74 68 65 20 41 54 54 41 43 48 20 6f 70  re the ATTACH op
2042e 65 72 61 74 69 6f 6e 2e 0a 20 20 2a 2a 0a 20 20  eration..  **.  
2042f 2a 2a 20 49 6e 20 74 68 65 20 73 65 63 6f 6e 64  ** In the second
20430 20 66 6f 72 6d 20 74 68 69 73 20 70 72 61 67 6d   form this pragm
20431 61 20 73 65 74 73 20 74 68 65 20 74 65 78 74 20  a sets the text 
20432 65 6e 63 6f 64 69 6e 67 20 74 6f 20 62 65 20 75  encoding to be u
20433 73 65 64 20 69 6e 0a 20 20 2a 2a 20 6e 65 77 20  sed in.  ** new 
20434 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 63  database files c
20435 72 65 61 74 65 64 20 75 73 69 6e 67 20 74 68 69  reated using thi
20436 73 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  s database handl
20437 65 2e 20 49 74 20 69 73 20 6f 6e 6c 79 0a 20 20  e. It is only.  
20438 2a 2a 20 75 73 65 66 75 6c 20 69 66 20 69 6e 76  ** useful if inv
20439 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79  oked immediately
2043a 20 61 66 74 65 72 20 74 68 65 20 6d 61 69 6e 20   after the main 
2043b 64 61 74 61 62 61 73 65 20 69 0a 20 20 2a 2f 0a  database i.  */.
2043c 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
2043d 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 65 6e 63  ICmp(zLeft, "enc
2043e 6f 64 69 6e 67 22 29 3d 3d 30 20 29 7b 0a 20 20  oding")==0 ){.  
2043f 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73    static const s
20440 74 72 75 63 74 20 45 6e 63 4e 61 6d 65 20 7b 0a  truct EncName {.
20441 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d        char *zNam
20442 65 3b 0a 20 20 20 20 20 20 75 38 20 65 6e 63 3b  e;.      u8 enc;
20443 0a 20 20 20 20 7d 20 65 6e 63 6e 61 6d 65 73 5b  .    } encnames[
20444 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b 20 22 55  ] = {.      { "U
20445 54 46 2d 38 22 2c 20 20 20 20 53 51 4c 49 54 45  TF-8",    SQLITE
20446 5f 55 54 46 38 20 20 20 20 20 20 20 20 7d 2c 0a  _UTF8        },.
20447 20 20 20 20 20 20 7b 20 22 55 54 46 38 22 2c 20        { "UTF8", 
20448 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 20      SQLITE_UTF8 
20449 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 20 20         },.      
2044a 7b 20 22 55 54 46 2d 31 36 6c 65 22 2c 20 53 51  { "UTF-16le", SQ
2044b 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 20 20 20  LITE_UTF16LE    
2044c 20 7d 2c 0a 20 20 20 20 20 20 7b 20 22 55 54 46   },.      { "UTF
2044d 31 36 6c 65 22 2c 20 20 53 51 4c 49 54 45 5f 55  16le",  SQLITE_U
2044e 54 46 31 36 4c 45 20 20 20 20 20 7d 2c 0a 20 20  TF16LE     },.  
2044f 20 20 20 20 7b 20 22 55 54 46 2d 31 36 62 65 22      { "UTF-16be"
20450 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  , SQLITE_UTF16BE
20451 20 20 20 20 20 7d 2c 0a 20 20 20 20 20 20 7b 20       },.      { 
20452 22 55 54 46 31 36 62 65 22 2c 20 20 53 51 4c 49  "UTF16be",  SQLI
20453 54 45 5f 55 54 46 31 36 42 45 20 20 20 20 20 7d  TE_UTF16BE     }
20454 2c 0a 20 20 20 20 20 20 7b 20 22 55 54 46 2d 31  ,.      { "UTF-1
20455 36 22 2c 20 20 20 30 20 20 20 20 20 20 20 20 20  6",   0         
20456 20 20 20 20 20 20 20 20 20 7d 2c 20 2f 2a 20 53           }, /* S
20457 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
20458 45 20 2a 2f 0a 20 20 20 20 20 20 7b 20 22 55 54  E */.      { "UT
20459 46 31 36 22 2c 20 20 20 20 30 20 20 20 20 20 20  F16",    0      
2045a 20 20 20 20 20 20 20 20 20 20 20 20 7d 2c 20 2f              }, /
2045b 2a 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  * SQLITE_UTF16NA
2045c 54 49 56 45 20 2a 2f 0a 20 20 20 20 20 20 7b 20  TIVE */.      { 
2045d 30 2c 20 30 20 7d 0a 20 20 20 20 7d 3b 0a 20 20  0, 0 }.    };.  
2045e 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 45    const struct E
2045f 6e 63 4e 61 6d 65 20 2a 70 45 6e 63 3b 0a 20 20  ncName *pEnc;.  
20460 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b    if( !zRight ){
20461 20 20 20 20 2f 2a 20 22 50 52 41 47 4d 41 20 65      /* "PRAGMA e
20462 6e 63 6f 64 69 6e 67 22 20 2a 2f 0a 20 20 20 20  ncoding" */.    
20463 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61    if( sqlite3Rea
20464 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
20465 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75  ) goto pragma_ou
20466 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  t;.      sqlite3
20467 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
20468 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
20469 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
2046a 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
2046b 4e 41 4d 45 2c 20 22 65 6e 63 6f 64 69 6e 67 22  NAME, "encoding"
2046c 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
2046d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2046e 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp2(v, OP_Stri
2046f 6e 67 38 2c 20 30 2c 20 31 29 3b 0a 20 20 20 20  ng8, 0, 1);.    
20470 20 20 66 6f 72 28 70 45 6e 63 3d 26 65 6e 63 6e    for(pEnc=&encn
20471 61 6d 65 73 5b 30 5d 3b 20 70 45 6e 63 2d 3e 7a  ames[0]; pEnc->z
20472 4e 61 6d 65 3b 20 70 45 6e 63 2b 2b 29 7b 0a 20  Name; pEnc++){. 
20473 20 20 20 20 20 20 20 69 66 28 20 70 45 6e 63 2d         if( pEnc-
20474 3e 65 6e 63 3d 3d 45 4e 43 28 70 50 61 72 73 65  >enc==ENC(pParse
20475 2d 3e 64 62 29 20 29 7b 0a 20 20 20 20 20 20 20  ->db) ){.       
20476 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
20477 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 70 45  angeP4(v, -1, pE
20478 6e 63 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54  nc->zName, P4_ST
20479 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 20  ATIC);.         
2047a 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
2047b 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
2047c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2047d 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f  2(v, OP_ResultRo
2047e 77 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 7d 65  w, 1, 1);.    }e
2047f 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20  lse{            
20480 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 22              /* "
20481 50 52 41 47 4d 41 20 65 6e 63 6f 64 69 6e 67 20  PRAGMA encoding 
20482 3d 20 58 58 58 22 20 2a 2f 0a 20 20 20 20 20 20  = XXX" */.      
20483 2f 2a 20 4f 6e 6c 79 20 63 68 61 6e 67 65 20 74  /* Only change t
20484 68 65 20 76 61 6c 75 65 20 6f 66 20 73 71 6c 69  he value of sqli
20485 74 65 2e 65 6e 63 20 69 66 20 74 68 65 20 64 61  te.enc if the da
20486 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 69 73  tabase handle is
20487 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20 69 6e   not.      ** in
20488 69 74 69 61 6c 69 7a 65 64 2e 20 49 66 20 74 68  itialized. If th
20489 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
2048a 65 78 69 73 74 73 2c 20 74 68 65 20 6e 65 77 20  exists, the new 
2048b 73 71 6c 69 74 65 2e 65 6e 63 20 76 61 6c 75 65  sqlite.enc value
2048c 0a 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62  .      ** will b
2048d 65 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 68  e overwritten wh
2048e 65 6e 20 74 68 65 20 73 63 68 65 6d 61 20 69 73  en the schema is
2048f 20 6e 65 78 74 20 6c 6f 61 64 65 64 2e 20 49 66   next loaded. If
20490 20 69 74 20 64 6f 65 73 20 6e 6f 74 0a 20 20 20   it does not.   
20491 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 65 78     ** already ex
20492 69 73 74 73 2c 20 69 74 20 77 69 6c 6c 20 62 65  ists, it will be
20493 20 63 72 65 61 74 65 64 20 74 6f 20 75 73 65 20   created to use 
20494 74 68 65 20 6e 65 77 20 65 6e 63 6f 64 69 6e 67  the new encoding
20495 20 76 61 6c 75 65 2e 0a 20 20 20 20 20 20 2a 2f   value..      */
20496 0a 20 20 20 20 20 20 69 66 28 20 0a 20 20 20 20  .      if( .    
20497 20 20 20 20 21 28 44 62 48 61 73 50 72 6f 70 65      !(DbHasPrope
20498 72 74 79 28 64 62 2c 20 30 2c 20 44 42 5f 53 63  rty(db, 0, DB_Sc
20499 68 65 6d 61 4c 6f 61 64 65 64 29 29 20 7c 7c 20  hemaLoaded)) || 
2049a 0a 20 20 20 20 20 20 20 20 44 62 48 61 73 50 72  .        DbHasPr
2049b 6f 70 65 72 74 79 28 64 62 2c 20 30 2c 20 44 42  operty(db, 0, DB
2049c 5f 45 6d 70 74 79 29 20 0a 20 20 20 20 20 20 29  _Empty) .      )
2049d 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70 45  {.        for(pE
2049e 6e 63 3d 26 65 6e 63 6e 61 6d 65 73 5b 30 5d 3b  nc=&encnames[0];
2049f 20 70 45 6e 63 2d 3e 7a 4e 61 6d 65 3b 20 70 45   pEnc->zName; pE
204a0 6e 63 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20  nc++){.         
204a1 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53   if( 0==sqlite3S
204a2 74 72 49 43 6d 70 28 7a 52 69 67 68 74 2c 20 70  trICmp(zRight, p
204a3 45 6e 63 2d 3e 7a 4e 61 6d 65 29 20 29 7b 0a 20  Enc->zName) ){. 
204a4 20 20 20 20 20 20 20 20 20 20 20 45 4e 43 28 70             ENC(p
204a5 50 61 72 73 65 2d 3e 64 62 29 20 3d 20 70 45 6e  Parse->db) = pEn
204a6 63 2d 3e 65 6e 63 20 3f 20 70 45 6e 63 2d 3e 65  c->enc ? pEnc->e
204a7 6e 63 20 3a 20 53 51 4c 49 54 45 5f 55 54 46 31  nc : SQLITE_UTF1
204a8 36 4e 41 54 49 56 45 3b 0a 20 20 20 20 20 20 20  6NATIVE;.       
204a9 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
204aa 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
204ab 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70  }.        if( !p
204ac 45 6e 63 2d 3e 7a 4e 61 6d 65 20 29 7b 0a 20 20  Enc->zName ){.  
204ad 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
204ae 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
204af 22 75 6e 73 75 70 70 6f 72 74 65 64 20 65 6e 63  "unsupported enc
204b0 6f 64 69 6e 67 3a 20 25 73 22 2c 20 7a 52 69 67  oding: %s", zRig
204b1 68 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ht);.        }. 
204b2 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
204b3 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53  else.#endif /* S
204b4 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
204b5 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
204b6 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f  ITE_OMIT_SCHEMA_
204b7 56 45 52 53 49 4f 4e 5f 50 52 41 47 4d 41 53 0a  VERSION_PRAGMAS.
204b8 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47    /*.  **   PRAG
204b9 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 73 63  MA [database.]sc
204ba 68 65 6d 61 5f 76 65 72 73 69 6f 6e 0a 20 20 2a  hema_version.  *
204bb 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  *   PRAGMA [data
204bc 62 61 73 65 2e 5d 73 63 68 65 6d 61 5f 76 65 72  base.]schema_ver
204bd 73 69 6f 6e 20 3d 20 3c 69 6e 74 65 67 65 72 3e  sion = <integer>
204be 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 50 52 41  .  **.  **   PRA
204bf 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 75  GMA [database.]u
204c0 73 65 72 5f 76 65 72 73 69 6f 6e 0a 20 20 2a 2a  ser_version.  **
204c1 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62     PRAGMA [datab
204c2 61 73 65 2e 5d 75 73 65 72 5f 76 65 72 73 69 6f  ase.]user_versio
204c3 6e 20 3d 20 3c 69 6e 74 65 67 65 72 3e 0a 20 20  n = <integer>.  
204c4 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 70 72 61 67  **.  ** The prag
204c5 6d 61 27 73 20 73 63 68 65 6d 61 5f 76 65 72 73  ma's schema_vers
204c6 69 6f 6e 20 61 6e 64 20 75 73 65 72 5f 76 65 72  ion and user_ver
204c7 73 69 6f 6e 20 61 72 65 20 75 73 65 64 20 74 6f  sion are used to
204c8 20 73 65 74 20 6f 72 20 67 65 74 0a 20 20 2a 2a   set or get.  **
204c9 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
204ca 65 20 73 63 68 65 6d 61 2d 76 65 72 73 69 6f 6e  e schema-version
204cb 20 61 6e 64 20 75 73 65 72 2d 76 65 72 73 69 6f   and user-versio
204cc 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e  n, respectively.
204cd 20 42 6f 74 68 0a 20 20 2a 2a 20 74 68 65 20 73   Both.  ** the s
204ce 63 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20 61 6e  chema-version an
204cf 64 20 74 68 65 20 75 73 65 72 2d 76 65 72 73 69  d the user-versi
204d0 6f 6e 20 61 72 65 20 33 32 2d 62 69 74 20 73 69  on are 32-bit si
204d1 67 6e 65 64 20 69 6e 74 65 67 65 72 73 0a 20 20  gned integers.  
204d2 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  ** stored in the
204d3 20 64 61 74 61 62 61 73 65 20 68 65 61 64 65 72   database header
204d4 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  **.  ** The 
204d5 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 20 69 73  schema-cookie is
204d6 20 75 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 6d 61   usually only ma
204d7 6e 69 70 75 6c 61 74 65 64 20 69 6e 74 65 72 6e  nipulated intern
204d8 61 6c 6c 79 20 62 79 20 53 51 4c 69 74 65 2e 20  ally by SQLite. 
204d9 49 74 0a 20 20 2a 2a 20 69 73 20 69 6e 63 72 65  It.  ** is incre
204da 6d 65 6e 74 65 64 20 62 79 20 53 51 4c 69 74 65  mented by SQLite
204db 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 64 61   whenever the da
204dc 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 69 73  tabase schema is
204dd 20 6d 6f 64 69 66 69 65 64 20 28 62 79 0a 20 20   modified (by.  
204de 2a 2a 20 63 72 65 61 74 69 6e 67 20 6f 72 20 64  ** creating or d
204df 72 6f 70 70 69 6e 67 20 61 20 74 61 62 6c 65 20  ropping a table 
204e0 6f 72 20 69 6e 64 65 78 29 2e 20 54 68 65 20 73  or index). The s
204e1 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 20 69 73  chema version is
204e2 20 75 73 65 64 20 62 79 0a 20 20 2a 2a 20 53 51   used by.  ** SQ
204e3 4c 69 74 65 20 65 61 63 68 20 74 69 6d 65 20 61  Lite each time a
204e4 20 71 75 65 72 79 20 69 73 20 65 78 65 63 75 74   query is execut
204e5 65 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61  ed to ensure tha
204e6 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 63  t the internal c
204e7 61 63 68 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65  ache.  ** of the
204e8 20 73 63 68 65 6d 61 20 75 73 65 64 20 77 68 65   schema used whe
204e9 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20  n compiling the 
204ea 53 51 4c 20 71 75 65 72 79 20 6d 61 74 63 68 65  SQL query matche
204eb 73 20 74 68 65 20 73 63 68 65 6d 61 20 6f 66 0a  s the schema of.
204ec 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73    ** the databas
204ed 65 20 61 67 61 69 6e 73 74 20 77 68 69 63 68 20  e against which 
204ee 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 71 75 65  the compiled que
204ef 72 79 20 69 73 20 61 63 74 75 61 6c 6c 79 20 65  ry is actually e
204f0 78 65 63 75 74 65 64 2e 0a 20 20 2a 2a 20 53 75  xecuted..  ** Su
204f1 62 76 65 72 74 69 6e 67 20 74 68 69 73 20 6d 65  bverting this me
204f2 63 68 61 6e 69 73 6d 20 62 79 20 75 73 69 6e 67  chanism by using
204f3 20 22 50 52 41 47 4d 41 20 73 63 68 65 6d 61 5f   "PRAGMA schema_
204f4 76 65 72 73 69 6f 6e 22 20 74 6f 20 6d 6f 64 69  version" to modi
204f5 66 79 0a 20 20 2a 2a 20 74 68 65 20 73 63 68 65  fy.  ** the sche
204f6 6d 61 2d 76 65 72 73 69 6f 6e 20 69 73 20 70 6f  ma-version is po
204f7 74 65 6e 74 69 61 6c 6c 79 20 64 61 6e 67 65 72  tentially danger
204f8 6f 75 73 20 61 6e 64 20 6d 61 79 20 6c 65 61 64  ous and may lead
204f9 20 74 6f 20 70 72 6f 67 72 61 6d 0a 20 20 2a 2a   to program.  **
204fa 20 63 72 61 73 68 65 73 20 6f 72 20 64 61 74 61   crashes or data
204fb 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e  base corruption.
204fc 20 55 73 65 20 77 69 74 68 20 63 61 75 74 69 6f   Use with cautio
204fd 6e 21 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  n!.  **.  ** The
204fe 20 75 73 65 72 2d 76 65 72 73 69 6f 6e 20 69 73   user-version is
204ff 20 6e 6f 74 20 75 73 65 64 20 69 6e 74 65 72 6e   not used intern
20500 61 6c 6c 79 20 62 79 20 53 51 4c 69 74 65 2e 20  ally by SQLite. 
20501 49 74 20 6d 61 79 20 62 65 20 75 73 65 64 20 62  It may be used b
20502 79 0a 20 20 2a 2a 20 61 70 70 6c 69 63 61 74 69  y.  ** applicati
20503 6f 6e 73 20 66 6f 72 20 61 6e 79 20 70 75 72 70  ons for any purp
20504 6f 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ose..  */.  if( 
20505 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
20506 4c 65 66 74 2c 20 22 73 63 68 65 6d 61 5f 76 65  Left, "schema_ve
20507 72 73 69 6f 6e 22 29 3d 3d 30 20 0a 20 20 20 7c  rsion")==0 .   |
20508 7c 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  | sqlite3StrICmp
20509 28 7a 4c 65 66 74 2c 20 22 75 73 65 72 5f 76 65  (zLeft, "user_ve
2050a 72 73 69 6f 6e 22 29 3d 3d 30 20 0a 20 20 20 7c  rsion")==0 .   |
2050b 7c 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  | sqlite3StrICmp
2050c 28 7a 4c 65 66 74 2c 20 22 66 72 65 65 6c 69 73  (zLeft, "freelis
2050d 74 5f 63 6f 75 6e 74 22 29 3d 3d 30 20 0a 20 20  t_count")==0 .  
2050e 29 7b 0a 0a 20 20 20 20 69 6e 74 20 69 43 6f 6f  ){..    int iCoo
2050f 6b 69 65 3b 20 20 20 2f 2a 20 43 6f 6f 6b 69 65  kie;   /* Cookie
20510 20 69 6e 64 65 78 2e 20 30 20 66 6f 72 20 73 63   index. 0 for sc
20511 68 65 6d 61 2d 63 6f 6f 6b 69 65 2c 20 36 20 66  hema-cookie, 6 f
20512 6f 72 20 75 73 65 72 2d 63 6f 6f 6b 69 65 2e 20  or user-cookie. 
20513 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  */.    sqlite3Vd
20514 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69  beUsesBtree(v, i
20515 44 62 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28  Db);.    switch(
20516 20 7a 4c 65 66 74 5b 30 5d 20 29 7b 0a 20 20 20   zLeft[0] ){.   
20517 20 20 20 63 61 73 65 20 27 73 27 3a 20 63 61 73     case 's': cas
20518 65 20 27 53 27 3a 0a 20 20 20 20 20 20 20 20 69  e 'S':.        i
20519 43 6f 6f 6b 69 65 20 3d 20 30 3b 0a 20 20 20 20  Cookie = 0;.    
2051a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2051b 20 63 61 73 65 20 27 66 27 3a 20 63 61 73 65 20   case 'f': case 
2051c 27 46 27 3a 0a 20 20 20 20 20 20 20 20 69 43 6f  'F':.        iCo
2051d 6f 6b 69 65 20 3d 20 31 3b 0a 20 20 20 20 20 20  okie = 1;.      
2051e 20 20 69 44 62 20 3d 20 28 2d 31 2a 28 69 44 62    iDb = (-1*(iDb
2051f 2b 31 29 29 3b 0a 20 20 20 20 20 20 20 20 61 73  +1));.        as
20520 73 65 72 74 28 69 44 62 3c 3d 30 29 3b 0a 20 20  sert(iDb<=0);.  
20521 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
20522 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20     default:.    
20523 20 20 20 20 69 43 6f 6f 6b 69 65 20 3d 20 35 3b      iCookie = 5;
20524 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
20525 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 7a      }..    if( z
20526 52 69 67 68 74 20 26 26 20 69 44 62 3e 3d 30 20  Right && iDb>=0 
20527 29 7b 0a 20 20 20 20 20 20 2f 2a 20 57 72 69 74  ){.      /* Writ
20528 65 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  e the specified 
20529 63 6f 6f 6b 69 65 20 76 61 6c 75 65 20 2a 2f 0a  cookie value */.
2052a 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e        static con
2052b 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 73 65  st VdbeOpList se
2052c 74 43 6f 6f 6b 69 65 5b 5d 20 3d 20 7b 0a 20 20  tCookie[] = {.  
2052d 20 20 20 20 20 20 7b 20 4f 50 5f 54 72 61 6e 73        { OP_Trans
2052e 61 63 74 69 6f 6e 2c 20 20 20 20 30 2c 20 20 31  action,    0,  1
2052f 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a  ,  0},    /* 0 *
20530 2f 0a 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49  /.        { OP_I
20531 6e 74 65 67 65 72 2c 20 20 20 20 20 20 20 20 30  nteger,        0
20532 2c 20 20 31 2c 20 20 30 7d 2c 20 20 20 20 2f 2a  ,  1,  0},    /*
20533 20 31 20 2a 2f 0a 20 20 20 20 20 20 20 20 7b 20   1 */.        { 
20534 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 20 20  OP_SetCookie,   
20535 20 20 20 30 2c 20 20 30 2c 20 20 31 7d 2c 20 20     0,  0,  1},  
20536 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 20 20    /* 2 */.      
20537 7d 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64  };.      int add
20538 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
20539 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61  ddOpList(v, Arra
2053a 79 53 69 7a 65 28 73 65 74 43 6f 6f 6b 69 65 29  ySize(setCookie)
2053b 2c 20 73 65 74 43 6f 6f 6b 69 65 29 3b 0a 20 20  , setCookie);.  
2053c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
2053d 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2c  hangeP1(v, addr,
2053e 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c   iDb);.      sql
2053f 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31  ite3VdbeChangeP1
20540 28 76 2c 20 61 64 64 72 2b 31 2c 20 61 74 6f 69  (v, addr+1, atoi
20541 28 7a 52 69 67 68 74 29 29 3b 0a 20 20 20 20 20  (zRight));.     
20542 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
20543 67 65 50 31 28 76 2c 20 61 64 64 72 2b 32 2c 20  geP1(v, addr+2, 
20544 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  iDb);.      sqli
20545 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28  te3VdbeChangeP2(
20546 76 2c 20 61 64 64 72 2b 32 2c 20 69 43 6f 6f 6b  v, addr+2, iCook
20547 69 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ie);.    }else{.
20548 20 20 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68        /* Read th
20549 65 20 73 70 65 63 69 66 69 65 64 20 63 6f 6f 6b  e specified cook
2054a 69 65 20 76 61 6c 75 65 20 2a 2f 0a 20 20 20 20  ie value */.    
2054b 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56    static const V
2054c 64 62 65 4f 70 4c 69 73 74 20 72 65 61 64 43 6f  dbeOpList readCo
2054d 6f 6b 69 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  okie[] = {.     
2054e 20 20 20 7b 20 4f 50 5f 52 65 61 64 43 6f 6f 6b     { OP_ReadCook
2054f 69 65 2c 20 20 20 20 20 20 30 2c 20 20 31 2c 20  ie,      0,  1, 
20550 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a 2f 0a   0},    /* 0 */.
20551 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 73          { OP_Res
20552 75 6c 74 52 6f 77 2c 20 20 20 20 20 20 20 31 2c  ultRow,       1,
20553 20 20 31 2c 20 20 30 7d 0a 20 20 20 20 20 20 7d    1,  0}.      }
20554 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72  ;.      int addr
20555 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
20556 64 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79  dOpList(v, Array
20557 53 69 7a 65 28 72 65 61 64 43 6f 6f 6b 69 65 29  Size(readCookie)
20558 2c 20 72 65 61 64 43 6f 6f 6b 69 65 29 3b 0a 20  , readCookie);. 
20559 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2055a 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72  ChangeP1(v, addr
2055b 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71  , iDb);.      sq
2055c 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
2055d 33 28 76 2c 20 61 64 64 72 2c 20 69 43 6f 6f 6b  3(v, addr, iCook
2055e 69 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ie);.      sqlit
2055f 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
20560 28 76 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71  (v, 1);.      sq
20561 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
20562 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
20563 45 5f 4e 41 4d 45 2c 20 7a 4c 65 66 74 2c 20 50  E_NAME, zLeft, P
20564 34 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  4_TRANSIENT);.  
20565 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64    }.  }else.#end
20566 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
20567 54 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e  T_SCHEMA_VERSION
20568 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66  _PRAGMAS */..#if
20569 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
2056a 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65  DEBUG) || define
2056b 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20  d(SQLITE_TEST). 
2056c 20 2f 2a 0a 20 20 2a 2a 20 52 65 70 6f 72 74 20   /*.  ** Report 
2056d 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74  the current stat
2056e 65 20 6f 66 20 66 69 6c 65 20 6c 6f 67 73 20 66  e of file logs f
2056f 6f 72 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73  or all databases
20570 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
20571 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
20572 2c 20 22 6c 6f 63 6b 5f 73 74 61 74 75 73 22 29  , "lock_status")
20573 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 61 74 69  ==0 ){.    stati
20574 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f  c const char *co
20575 6e 73 74 20 61 7a 4c 6f 63 6b 4e 61 6d 65 5b 5d  nst azLockName[]
20576 20 3d 20 7b 0a 20 20 20 20 20 20 22 75 6e 6c 6f   = {.      "unlo
20577 63 6b 65 64 22 2c 20 22 73 68 61 72 65 64 22 2c  cked", "shared",
20578 20 22 72 65 73 65 72 76 65 64 22 2c 20 22 70 65   "reserved", "pe
20579 6e 64 69 6e 67 22 2c 20 22 65 78 63 6c 75 73 69  nding", "exclusi
2057a 76 65 22 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69  ve".    };.    i
2057b 6e 74 20 69 3b 0a 20 20 20 20 56 64 62 65 20 2a  nt i;.    Vdbe *
2057c 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
2057d 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  be(pParse);.    
2057e 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
2057f 6d 43 6f 6c 73 28 76 2c 20 32 29 3b 0a 20 20 20  mCols(v, 2);.   
20580 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20   pParse->nMem = 
20581 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  2;.    sqlite3Vd
20582 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
20583 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
20584 20 22 64 61 74 61 62 61 73 65 22 2c 20 50 34 5f   "database", P4_
20585 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c  STATIC);.    sql
20586 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
20587 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45  me(v, 1, COLNAME
20588 5f 4e 41 4d 45 2c 20 22 73 74 61 74 75 73 22 2c  _NAME, "status",
20589 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P4_STATIC);.   
2058a 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
2058b 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  nDb; i++){.     
2058c 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 20   Btree *pBt;.   
2058d 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72     Pager *pPager
2058e 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  ;.      const ch
2058f 61 72 20 2a 7a 53 74 61 74 65 20 3d 20 22 75 6e  ar *zState = "un
20590 6b 6e 6f 77 6e 22 3b 0a 20 20 20 20 20 20 69 6e  known";.      in
20591 74 20 6a 3b 0a 20 20 20 20 20 20 69 66 28 20 64  t j;.      if( d
20592 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3d  b->aDb[i].zName=
20593 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  =0 ) continue;. 
20594 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
20595 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
20596 69 6e 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20 64  ing8, 0, 1, 0, d
20597 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 2c  b->aDb[i].zName,
20598 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P4_STATIC);.   
20599 20 20 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 62     pBt = db->aDb
2059a 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69  [i].pBt;.      i
2059b 66 28 20 70 42 74 3d 3d 30 20 7c 7c 20 28 70 50  f( pBt==0 || (pP
2059c 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74  ager = sqlite3Bt
2059d 72 65 65 50 61 67 65 72 28 70 42 74 29 29 3d 3d  reePager(pBt))==
2059e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 53 74  0 ){.        zSt
2059f 61 74 65 20 3d 20 22 63 6c 6f 73 65 64 22 3b 0a  ate = "closed";.
205a0 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
205a1 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e  sqlite3_file_con
205a2 74 72 6f 6c 28 64 62 2c 20 69 20 3f 20 64 62 2d  trol(db, i ? db-
205a3 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 20 3a 20  >aDb[i].zName : 
205a4 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  0, .            
205a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
205a6 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
205a7 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 2c  FCNTL_LOCKSTATE,
205a8 20 26 6a 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20   &j)==SQLITE_OK 
205a9 29 7b 0a 20 20 20 20 20 20 20 20 20 7a 53 74 61  ){.         zSta
205aa 74 65 20 3d 20 61 7a 4c 6f 63 6b 4e 61 6d 65 5b  te = azLockName[
205ab 6a 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  j];.      }.    
205ac 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
205ad 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
205ae 38 2c 20 30 2c 20 32 2c 20 30 2c 20 7a 53 74 61  8, 0, 2, 0, zSta
205af 74 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a  te, P4_STATIC);.
205b0 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
205b1 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
205b2 73 75 6c 74 52 6f 77 2c 20 31 2c 20 32 29 3b 0a  sultRow, 1, 2);.
205b3 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65      }.  }else.#e
205b4 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
205b5 49 54 45 5f 53 53 45 0a 20 20 2f 2a 0a 20 20 2a  ITE_SSE.  /*.  *
205b6 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
205b7 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  f the sqlite_sta
205b8 74 65 6d 65 6e 74 73 20 74 61 62 6c 65 20 65 78  tements table ex
205b9 69 73 74 73 2e 20 20 43 72 65 61 74 65 20 69 74  ists.  Create it
205ba 0a 20 20 2a 2a 20 69 66 20 69 74 20 64 6f 65 73  .  ** if it does
205bb 20 6e 6f 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28   not..  */.  if(
205bc 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
205bd 7a 4c 65 66 74 2c 20 22 63 72 65 61 74 65 5f 73  zLeft, "create_s
205be 71 6c 69 74 65 5f 73 74 61 74 65 6d 65 6e 74 5f  qlite_statement_
205bf 74 61 62 6c 65 22 29 3d 3d 30 20 29 7b 0a 20 20  table")==0 ){.  
205c0 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c    extern int sql
205c1 69 74 65 33 43 72 65 61 74 65 53 74 61 74 65 6d  ite3CreateStatem
205c2 65 6e 74 73 54 61 62 6c 65 28 50 61 72 73 65 2a  entsTable(Parse*
205c3 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 72  );.    sqlite3Cr
205c4 65 61 74 65 53 74 61 74 65 6d 65 6e 74 73 54 61  eateStatementsTa
205c5 62 6c 65 28 70 50 61 72 73 65 29 3b 0a 20 20 7d  ble(pParse);.  }
205c6 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 23 69 66  else.#endif..#if
205c7 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45   SQLITE_HAS_CODE
205c8 43 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  C.  if( sqlite3S
205c9 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 6b  trICmp(zLeft, "k
205ca 65 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73  ey")==0 ){.    s
205cb 71 6c 69 74 65 33 5f 6b 65 79 28 64 62 2c 20 7a  qlite3_key(db, z
205cc 52 69 67 68 74 2c 20 73 74 72 6c 65 6e 28 7a 52  Right, strlen(zR
205cd 69 67 68 74 29 29 3b 0a 20 20 7d 65 6c 73 65 0a  ight));.  }else.
205ce 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
205cf 45 5f 48 41 53 5f 43 4f 44 45 43 20 7c 7c 20 64  E_HAS_CODEC || d
205d0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
205d1 41 42 4c 45 5f 43 45 52 4f 44 29 0a 20 20 69 66  ABLE_CEROD).  if
205d2 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
205d3 28 7a 4c 65 66 74 2c 20 22 61 63 74 69 76 61 74  (zLeft, "activat
205d4 65 5f 65 78 74 65 6e 73 69 6f 6e 73 22 29 3d 3d  e_extensions")==
205d5 30 20 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f  0 ){.#if SQLITE_
205d6 48 41 53 5f 43 4f 44 45 43 0a 20 20 20 20 69 66  HAS_CODEC.    if
205d7 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  ( sqlite3StrNICm
205d8 70 28 7a 52 69 67 68 74 2c 20 22 73 65 65 2d 22  p(zRight, "see-"
205d9 2c 20 34 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  , 4)==0 ){.     
205da 20 65 78 74 65 72 6e 20 76 6f 69 64 20 73 71 6c   extern void sql
205db 69 74 65 33 5f 61 63 74 69 76 61 74 65 5f 73 65  ite3_activate_se
205dc 65 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  e(const char*);.
205dd 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 63        sqlite3_ac
205de 74 69 76 61 74 65 5f 73 65 65 28 26 7a 52 69 67  tivate_see(&zRig
205df 68 74 5b 34 5d 29 3b 0a 20 20 20 20 7d 0a 23 65  ht[4]);.    }.#e
205e0 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
205e1 54 45 5f 45 4e 41 42 4c 45 5f 43 45 52 4f 44 0a  TE_ENABLE_CEROD.
205e2 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
205e3 74 72 4e 49 43 6d 70 28 7a 52 69 67 68 74 2c 20  trNICmp(zRight, 
205e4 22 63 65 72 6f 64 2d 22 2c 20 36 29 3d 3d 30 20  "cerod-", 6)==0 
205e5 29 7b 0a 20 20 20 20 20 20 65 78 74 65 72 6e 20  ){.      extern 
205e6 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 61 63 74  void sqlite3_act
205e7 69 76 61 74 65 5f 63 65 72 6f 64 28 63 6f 6e 73  ivate_cerod(cons
205e8 74 20 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20  t char*);.      
205e9 73 71 6c 69 74 65 33 5f 61 63 74 69 76 61 74 65  sqlite3_activate
205ea 5f 63 65 72 6f 64 28 26 7a 52 69 67 68 74 5b 36  _cerod(&zRight[6
205eb 5d 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  ]);.    }.#endif
205ec 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 7b  .  }.#endif..  {
205ed 7d 0a 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20  }..  if( v ){.  
205ee 20 20 2f 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f    /* Code an OP_
205ef 45 78 70 69 72 65 20 61 74 20 74 68 65 20 65 6e  Expire at the en
205f0 64 20 6f 66 20 65 61 63 68 20 50 52 41 47 4d 41  d of each PRAGMA
205f1 20 70 72 6f 67 72 61 6d 20 74 6f 20 63 61 75 73   program to caus
205f2 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 56 44 42  e.    ** the VDB
205f3 45 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74  E implementing t
205f4 68 65 20 70 72 61 67 6d 61 20 74 6f 20 65 78 70  he pragma to exp
205f5 69 72 65 2e 20 4d 6f 73 74 20 28 61 6c 6c 3f 29  ire. Most (all?)
205f6 20 70 72 61 67 6d 61 73 0a 20 20 20 20 2a 2a 20   pragmas.    ** 
205f7 61 72 65 20 6f 6e 6c 79 20 76 61 6c 69 64 20 66  are only valid f
205f8 6f 72 20 61 20 73 69 6e 67 6c 65 20 65 78 65 63  or a single exec
205f9 75 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20  ution..    */.  
205fa 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
205fb 4f 70 32 28 76 2c 20 4f 50 5f 45 78 70 69 72 65  Op2(v, OP_Expire
205fc 2c 20 31 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a  , 1, 0);..    /*
205fd 0a 20 20 20 20 2a 2a 20 52 65 73 65 74 20 74 68  .    ** Reset th
205fe 65 20 73 61 66 65 74 79 20 6c 65 76 65 6c 2c 20  e safety level, 
205ff 69 6e 20 63 61 73 65 20 74 68 65 20 66 75 6c 6c  in case the full
20600 66 73 79 6e 63 20 66 6c 61 67 20 6f 72 20 73 79  fsync flag or sy
20601 6e 63 68 72 6f 6e 6f 75 73 0a 20 20 20 20 2a 2a  nchronous.    **
20602 20 73 65 74 74 69 6e 67 20 63 68 61 6e 67 65 64   setting changed
20603 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66  ..    */.#ifndef
20604 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47   SQLITE_OMIT_PAG
20605 45 52 5f 50 52 41 47 4d 41 53 0a 20 20 20 20 69  ER_PRAGMAS.    i
20606 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69  f( db->autoCommi
20607 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  t ){.      sqlit
20608 65 33 42 74 72 65 65 53 65 74 53 61 66 65 74 79  e3BtreeSetSafety
20609 4c 65 76 65 6c 28 70 44 62 2d 3e 70 42 74 2c 20  Level(pDb->pBt, 
2060a 70 44 62 2d 3e 73 61 66 65 74 79 5f 6c 65 76 65  pDb->safety_leve
2060b 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  l,.             
2060c 20 20 20 20 28 64 62 2d 3e 66 6c 61 67 73 26 53      (db->flags&S
2060d 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79 6e 63 29  QLITE_FullFSync)
2060e 21 3d 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  !=0);.    }.#end
2060f 69 66 0a 20 20 7d 0a 70 72 61 67 6d 61 5f 6f 75  if.  }.pragma_ou
20610 74 3a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  t:.  sqlite3_fre
20611 65 28 7a 4c 65 66 74 29 3b 0a 20 20 73 71 6c 69  e(zLeft);.  sqli
20612 74 65 33 5f 66 72 65 65 28 7a 52 69 67 68 74 29  te3_free(zRight)
20613 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ;.}..#endif /* S
20614 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 41 47 4d  QLITE_OMIT_PRAGM
20615 41 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4d 49 54  A || SQLITE_OMIT
20616 5f 50 41 52 53 45 52 20 2a 2f 0a 0a 2f 2a 2a 2a  _PARSER */../***
20617 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
20618 6f 66 20 70 72 61 67 6d 61 2e 63 20 2a 2a 2a 2a  of pragma.c ****
20619 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2061a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2061b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
2061c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
2061d 6e 20 66 69 6c 65 20 70 72 65 70 61 72 65 2e 63  n file prepare.c
2061e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2061f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
20621 2a 20 32 30 30 35 20 4d 61 79 20 32 35 0a 2a 2a  * 2005 May 25.**
20622 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
20623 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
20624 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
20625 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
20626 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
20627 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
20628 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
20629 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
2062a 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
2062b 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
2062c 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
2062d 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
2062e 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
2062f 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
20630 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
20631 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
20632 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
20633 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
20634 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20635 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20636 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20637 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
20638 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
20639 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  ns the implement
2063a 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c  ation of the sql
2063b 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 0a 2a  ite3_prepare().*
2063c 2a 20 69 6e 74 65 72 66 61 63 65 2c 20 61 6e 64  * interface, and
2063d 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 63   routines that c
2063e 6f 6e 74 72 69 62 75 74 65 20 74 6f 20 6c 6f 61  ontribute to loa
2063f 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73  ding the databas
20640 65 20 73 63 68 65 6d 61 0a 2a 2a 20 66 72 6f 6d  e schema.** from
20641 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 24 49 64   disk..**.** $Id
20642 3a 20 70 72 65 70 61 72 65 2e 63 2c 76 20 31 2e  : prepare.c,v 1.
20643 38 33 20 32 30 30 38 2f 30 34 2f 30 33 20 31 34  83 2008/04/03 14
20644 3a 33 36 3a 32 36 20 64 61 6e 69 65 6c 6b 31 39  :36:26 danielk19
20645 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a  77 Exp $.*/../*.
20646 2a 2a 20 46 69 6c 6c 20 74 68 65 20 49 6e 69 74  ** Fill the Init
20647 44 61 74 61 20 73 74 72 75 63 74 75 72 65 20 77  Data structure w
20648 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ith an error mes
20649 73 61 67 65 20 74 68 61 74 20 69 6e 64 69 63 61  sage that indica
2064a 74 65 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20  tes.** that the 
2064b 64 61 74 61 62 61 73 65 20 69 73 20 63 6f 72 72  database is corr
2064c 75 70 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  upt..*/.static v
2064d 6f 69 64 20 63 6f 72 72 75 70 74 53 63 68 65 6d  oid corruptSchem
2064e 61 28 0a 20 20 49 6e 69 74 44 61 74 61 20 2a 70  a(.  InitData *p
2064f 44 61 74 61 2c 20 20 20 20 20 2f 2a 20 49 6e 69  Data,     /* Ini
20650 74 69 61 6c 69 7a 61 74 69 6f 6e 20 63 6f 6e 74  tialization cont
20651 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ext */.  const c
20652 68 61 72 20 2a 7a 4f 62 6a 2c 20 20 20 20 2f 2a  har *zObj,    /*
20653 20 4f 62 6a 65 63 74 20 62 65 69 6e 67 20 70 61   Object being pa
20654 72 73 65 64 20 61 74 20 74 68 65 20 70 6f 69 6e  rsed at the poin
20655 74 20 6f 66 20 65 72 72 6f 72 20 2a 2f 0a 20 20  t of error */.  
20656 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 78 74  const char *zExt
20657 72 61 20 20 20 2f 2a 20 45 72 72 6f 72 20 69 6e  ra   /* Error in
20658 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 29 7b 0a  formation */.){.
20659 20 20 69 66 28 20 21 70 44 61 74 61 2d 3e 64 62    if( !pData->db
2065a 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2065b 7b 0a 20 20 20 20 69 66 28 20 7a 4f 62 6a 3d 3d  {.    if( zObj==
2065c 30 20 29 20 7a 4f 62 6a 20 3d 20 22 3f 22 3b 0a  0 ) zObj = "?";.
2065d 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
2065e 72 69 6e 67 28 70 44 61 74 61 2d 3e 70 7a 45 72  ring(pData->pzEr
2065f 72 4d 73 67 2c 20 22 6d 61 6c 66 6f 72 6d 65 64  rMsg, "malformed
20660 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
20661 20 28 22 2c 20 20 7a 4f 62 6a 2c 20 22 29 22 2c   (",  zObj, ")",
20662 0a 20 20 20 20 20 20 20 7a 45 78 74 72 61 21 3d  .       zExtra!=
20663 30 20 26 26 20 7a 45 78 74 72 61 5b 30 5d 21 3d  0 && zExtra[0]!=
20664 30 20 3f 20 22 20 2d 20 22 20 3a 20 28 63 68 61  0 ? " - " : (cha
20665 72 2a 29 30 2c 20 7a 45 78 74 72 61 2c 20 28 63  r*)0, zExtra, (c
20666 68 61 72 2a 29 30 29 3b 0a 20 20 7d 0a 20 20 70  har*)0);.  }.  p
20667 44 61 74 61 2d 3e 72 63 20 3d 20 53 51 4c 49 54  Data->rc = SQLIT
20668 45 5f 43 4f 52 52 55 50 54 3b 0a 7d 0a 0a 2f 2a  E_CORRUPT;.}../*
20669 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
2066a 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65  callback routine
2066b 20 66 6f 72 20 74 68 65 20 63 6f 64 65 20 74 68   for the code th
2066c 61 74 20 69 6e 69 74 69 61 6c 69 7a 65 73 20 74  at initializes t
2066d 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20  he.** database. 
2066e 20 53 65 65 20 73 71 6c 69 74 65 33 49 6e 69 74   See sqlite3Init
2066f 28 29 20 62 65 6c 6f 77 20 66 6f 72 20 61 64 64  () below for add
20670 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
20671 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ion..** This rou
20672 74 69 6e 65 20 69 73 20 61 6c 73 6f 20 63 61 6c  tine is also cal
20673 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 4f 50 5f  led from the OP_
20674 50 61 72 73 65 53 63 68 65 6d 61 20 6f 70 63 6f  ParseSchema opco
20675 64 65 20 6f 66 20 74 68 65 20 56 44 42 45 2e 0a  de of the VDBE..
20676 2a 2a 0a 2a 2a 20 45 61 63 68 20 63 61 6c 6c 62  **.** Each callb
20677 61 63 6b 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ack contains the
20678 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 66 6f 72   following infor
20679 6d 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20  mation:.**.**   
2067a 20 20 61 72 67 76 5b 30 5d 20 3d 20 6e 61 6d 65    argv[0] = name
2067b 20 6f 66 20 74 68 69 6e 67 20 62 65 69 6e 67 20   of thing being 
2067c 63 72 65 61 74 65 64 0a 2a 2a 20 20 20 20 20 61  created.**     a
2067d 72 67 76 5b 31 5d 20 3d 20 72 6f 6f 74 20 70 61  rgv[1] = root pa
2067e 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 61  ge number for ta
2067f 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 20 30 20  ble or index. 0 
20680 66 6f 72 20 74 72 69 67 67 65 72 20 6f 72 20 76  for trigger or v
20681 69 65 77 2e 0a 2a 2a 20 20 20 20 20 61 72 67 76  iew..**     argv
20682 5b 32 5d 20 3d 20 53 51 4c 20 74 65 78 74 20 66  [2] = SQL text f
20683 6f 72 20 74 68 65 20 43 52 45 41 54 45 20 73 74  or the CREATE st
20684 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2f 0a 53  atement..**.*/.S
20685 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
20686 74 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61 6c  t sqlite3InitCal
20687 6c 62 61 63 6b 28 76 6f 69 64 20 2a 70 49 6e 69  lback(void *pIni
20688 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 63 68 61  t, int argc, cha
20689 72 20 2a 2a 61 72 67 76 2c 20 63 68 61 72 20 2a  r **argv, char *
2068a 2a 61 7a 43 6f 6c 4e 61 6d 65 29 7b 0a 20 20 49  *azColName){.  I
2068b 6e 69 74 44 61 74 61 20 2a 70 44 61 74 61 20 3d  nitData *pData =
2068c 20 28 49 6e 69 74 44 61 74 61 2a 29 70 49 6e 69   (InitData*)pIni
2068d 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  t;.  sqlite3 *db
2068e 20 3d 20 70 44 61 74 61 2d 3e 64 62 3b 0a 20 20   = pData->db;.  
2068f 69 6e 74 20 69 44 62 20 3d 20 70 44 61 74 61 2d  int iDb = pData-
20690 3e 69 44 62 3b 0a 0a 20 20 61 73 73 65 72 74 28  >iDb;..  assert(
20691 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
20692 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
20693 3b 0a 20 20 70 44 61 74 61 2d 3e 72 63 20 3d 20  ;.  pData->rc = 
20694 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 44 62 43  SQLITE_OK;.  DbC
20695 6c 65 61 72 50 72 6f 70 65 72 74 79 28 64 62 2c  learProperty(db,
20696 20 69 44 62 2c 20 44 42 5f 45 6d 70 74 79 29 3b   iDb, DB_Empty);
20697 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
20698 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 63  cFailed ){.    c
20699 6f 72 72 75 70 74 53 63 68 65 6d 61 28 70 44 61  orruptSchema(pDa
2069a 74 61 2c 20 61 72 67 76 5b 30 5d 2c 20 30 29 3b  ta, argv[0], 0);
2069b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
2069c 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20  TE_NOMEM;.  }.. 
2069d 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 33   assert( argc==3
2069e 20 29 3b 0a 20 20 69 66 28 20 61 72 67 76 3d 3d   );.  if( argv==
2069f 30 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20  0 ) return 0;   
206a0 2f 2a 20 4d 69 67 68 74 20 68 61 70 70 65 6e 20  /* Might happen 
206a1 69 66 20 45 4d 50 54 59 5f 52 45 53 55 4c 54 5f  if EMPTY_RESULT_
206a2 43 41 4c 4c 42 41 43 4b 53 20 61 72 65 20 6f 6e  CALLBACKS are on
206a3 20 2a 2f 0a 20 20 69 66 28 20 61 72 67 76 5b 31   */.  if( argv[1
206a4 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f 72 72  ]==0 ){.    corr
206a5 75 70 74 53 63 68 65 6d 61 28 70 44 61 74 61 2c  uptSchema(pData,
206a6 20 61 72 67 76 5b 30 5d 2c 20 30 29 3b 0a 20 20   argv[0], 0);.  
206a7 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
206a8 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30    assert( iDb>=0
206a9 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20   && iDb<db->nDb 
206aa 29 3b 0a 20 20 69 66 28 20 61 72 67 76 5b 32 5d  );.  if( argv[2]
206ab 20 26 26 20 61 72 67 76 5b 32 5d 5b 30 5d 20 29   && argv[2][0] )
206ac 7b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74 68  {.    /* Call th
206ad 65 20 70 61 72 73 65 72 20 74 6f 20 70 72 6f 63  e parser to proc
206ae 65 73 73 20 61 20 43 52 45 41 54 45 20 54 41 42  ess a CREATE TAB
206af 4c 45 2c 20 49 4e 44 45 58 20 6f 72 20 56 49 45  LE, INDEX or VIE
206b0 57 2e 0a 20 20 20 20 2a 2a 20 42 75 74 20 62 65  W..    ** But be
206b1 63 61 75 73 65 20 64 62 2d 3e 69 6e 69 74 2e 62  cause db->init.b
206b2 75 73 79 20 69 73 20 73 65 74 20 74 6f 20 31 2c  usy is set to 1,
206b3 20 6e 6f 20 56 44 42 45 20 63 6f 64 65 20 69 73   no VDBE code is
206b4 20 67 65 6e 65 72 61 74 65 64 0a 20 20 20 20 2a   generated.    *
206b5 2a 20 6f 72 20 65 78 65 63 75 74 65 64 2e 20 20  * or executed.  
206b6 41 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20 64  All the parser d
206b7 6f 65 73 20 69 73 20 62 75 69 6c 64 20 74 68 65  oes is build the
206b8 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 0a 20   internal data. 
206b9 20 20 20 2a 2a 20 73 74 72 75 63 74 75 72 65 73     ** structures
206ba 20 74 68 61 74 20 64 65 73 63 72 69 62 65 20 74   that describe t
206bb 68 65 20 74 61 62 6c 65 2c 20 69 6e 64 65 78 2c  he table, index,
206bc 20 6f 72 20 76 69 65 77 2e 0a 20 20 20 20 2a 2f   or view..    */
206bd 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72 3b  .    char *zErr;
206be 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20  .    int rc;.   
206bf 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69   assert( db->ini
206c0 74 2e 62 75 73 79 20 29 3b 0a 20 20 20 20 64 62  t.busy );.    db
206c1 2d 3e 69 6e 69 74 2e 69 44 62 20 3d 20 69 44 62  ->init.iDb = iDb
206c2 3b 0a 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 6e  ;.    db->init.n
206c3 65 77 54 6e 75 6d 20 3d 20 61 74 6f 69 28 61 72  ewTnum = atoi(ar
206c4 67 76 5b 31 5d 29 3b 0a 20 20 20 20 72 63 20 3d  gv[1]);.    rc =
206c5 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62   sqlite3_exec(db
206c6 2c 20 61 72 67 76 5b 32 5d 2c 20 30 2c 20 30 2c  , argv[2], 0, 0,
206c7 20 26 7a 45 72 72 29 3b 0a 20 20 20 20 64 62 2d   &zErr);.    db-
206c8 3e 69 6e 69 74 2e 69 44 62 20 3d 20 30 3b 0a 20  >init.iDb = 0;. 
206c9 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
206ca 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 7a 45 72 72  QLITE_OK || zErr
206cb 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 53  ==0 );.    if( S
206cc 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a  QLITE_OK!=rc ){.
206cd 20 20 20 20 20 20 70 44 61 74 61 2d 3e 72 63 20        pData->rc 
206ce 3d 20 72 63 3b 0a 20 20 20 20 20 20 69 66 28 20  = rc;.      if( 
206cf 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
206d0 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e   ){.        db->
206d1 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
206d2 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
206d3 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4e 54  ( rc!=SQLITE_INT
206d4 45 52 52 55 50 54 20 29 7b 0a 20 20 20 20 20 20  ERRUPT ){.      
206d5 20 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61 28    corruptSchema(
206d6 70 44 61 74 61 2c 20 61 72 67 76 5b 30 5d 2c 20  pData, argv[0], 
206d7 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20  zErr);.      }. 
206d8 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
206d9 65 28 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 72  e(zErr);.      r
206da 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
206db 20 7d 65 6c 73 65 20 69 66 28 20 61 72 67 76 5b   }else if( argv[
206dc 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f 72  0]==0 ){.    cor
206dd 72 75 70 74 53 63 68 65 6d 61 28 70 44 61 74 61  ruptSchema(pData
206de 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65  , 0, 0);.  }else
206df 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  {.    /* If the 
206e0 53 51 4c 20 63 6f 6c 75 6d 6e 20 69 73 20 62 6c  SQL column is bl
206e1 61 6e 6b 20 69 74 20 6d 65 61 6e 73 20 74 68 69  ank it means thi
206e2 73 20 69 73 20 61 6e 20 69 6e 64 65 78 20 74 68  s is an index th
206e3 61 74 0a 20 20 20 20 2a 2a 20 77 61 73 20 63 72  at.    ** was cr
206e4 65 61 74 65 64 20 74 6f 20 62 65 20 74 68 65 20  eated to be the 
206e5 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 74  PRIMARY KEY or t
206e6 6f 20 66 75 6c 66 69 6c 6c 20 61 20 55 4e 49 51  o fulfill a UNIQ
206e7 55 45 0a 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72  UE.    ** constr
206e8 61 69 6e 74 20 66 6f 72 20 61 20 43 52 45 41 54  aint for a CREAT
206e9 45 20 54 41 42 4c 45 2e 20 20 54 68 65 20 69 6e  E TABLE.  The in
206ea 64 65 78 20 73 68 6f 75 6c 64 20 68 61 76 65 20  dex should have 
206eb 61 6c 72 65 61 64 79 0a 20 20 20 20 2a 2a 20 62  already.    ** b
206ec 65 65 6e 20 63 72 65 61 74 65 64 20 77 68 65 6e  een created when
206ed 20 77 65 20 70 72 6f 63 65 73 73 65 64 20 74 68   we processed th
206ee 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 2e 20  e CREATE TABLE. 
206ef 20 41 6c 6c 20 77 65 20 68 61 76 65 0a 20 20 20   All we have.   
206f0 20 2a 2a 20 74 6f 20 64 6f 20 68 65 72 65 20 69   ** to do here i
206f1 73 20 72 65 63 6f 72 64 20 74 68 65 20 72 6f 6f  s record the roo
206f2 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f  t page number fo
206f3 72 20 74 68 61 74 20 69 6e 64 65 78 2e 0a 20 20  r that index..  
206f4 20 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a    */.    Index *
206f5 70 49 6e 64 65 78 3b 0a 20 20 20 20 70 49 6e 64  pIndex;.    pInd
206f6 65 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ex = sqlite3Find
206f7 49 6e 64 65 78 28 64 62 2c 20 61 72 67 76 5b 30  Index(db, argv[0
206f8 5d 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  ], db->aDb[iDb].
206f9 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  zName);.    if( 
206fa 70 49 6e 64 65 78 3d 3d 30 20 7c 7c 20 70 49 6e  pIndex==0 || pIn
206fb 64 65 78 2d 3e 74 6e 75 6d 21 3d 30 20 29 7b 0a  dex->tnum!=0 ){.
206fc 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61        /* This ca
206fd 6e 20 6f 63 63 75 72 20 69 66 20 74 68 65 72 65  n occur if there
206fe 20 65 78 69 73 74 73 20 61 6e 20 69 6e 64 65 78   exists an index
206ff 20 6f 6e 20 61 20 54 45 4d 50 20 74 61 62 6c 65   on a TEMP table
20700 20 77 68 69 63 68 0a 20 20 20 20 20 20 2a 2a 20   which.      ** 
20701 68 61 73 20 74 68 65 20 73 61 6d 65 20 6e 61 6d  has the same nam
20702 65 20 61 73 20 61 6e 6f 74 68 65 72 20 69 6e 64  e as another ind
20703 65 78 20 6f 6e 20 61 20 70 65 72 6d 61 6e 65 6e  ex on a permanen
20704 74 20 69 6e 64 65 78 2e 20 20 53 69 6e 63 65 0a  t index.  Since.
20705 20 20 20 20 20 20 2a 2a 20 74 68 65 20 70 65 72        ** the per
20706 6d 61 6e 65 6e 74 20 74 61 62 6c 65 20 69 73 20  manent table is 
20707 68 69 64 64 65 6e 20 62 79 20 74 68 65 20 54 45  hidden by the TE
20708 4d 50 20 74 61 62 6c 65 2c 20 77 65 20 63 61 6e  MP table, we can
20709 20 61 6c 73 6f 0a 20 20 20 20 20 20 2a 2a 20 73   also.      ** s
2070a 61 66 65 6c 79 20 69 67 6e 6f 72 65 20 74 68 65  afely ignore the
2070b 20 69 6e 64 65 78 20 6f 6e 20 74 68 65 20 70 65   index on the pe
2070c 72 6d 61 6e 65 6e 74 20 74 61 62 6c 65 2e 0a 20  rmanent table.. 
2070d 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 2f 2a       */.      /*
2070e 20 44 6f 20 4e 6f 74 68 69 6e 67 20 2a 2f 3b 0a   Do Nothing */;.
2070f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
20710 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 20 3d 20   pIndex->tnum = 
20711 61 74 6f 69 28 61 72 67 76 5b 31 5d 29 3b 0a 20  atoi(argv[1]);. 
20712 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
20713 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74  n 0;.}../*.** At
20714 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 74 68  tempt to read th
20715 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
20716 61 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65  a and initialize
20717 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 64 61 74   internal.** dat
20718 61 20 73 74 72 75 63 74 75 72 65 73 20 66 6f 72  a structures for
20719 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61   a single databa
2071a 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 69 6e  se file.  The in
2071b 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61  dex of the.** da
2071c 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 67  tabase file is g
2071d 69 76 65 6e 20 62 79 20 69 44 62 2e 20 20 69 44  iven by iDb.  iD
2071e 62 3d 3d 30 20 69 73 20 75 73 65 64 20 66 6f 72  b==0 is used for
2071f 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 64 61 74   the main.** dat
20720 61 62 61 73 65 2e 20 20 69 44 62 3d 3d 31 20 73  abase.  iDb==1 s
20721 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20 75  hould never be u
20722 73 65 64 2e 20 20 69 44 62 3e 3d 32 20 69 73 20  sed.  iDb>=2 is 
20723 75 73 65 64 20 66 6f 72 0a 2a 2a 20 61 75 78 69  used for.** auxi
20724 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 73 2e  liary databases.
20725 20 20 52 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20    Return one of 
20726 74 68 65 20 53 51 4c 49 54 45 5f 20 65 72 72 6f  the SQLITE_ erro
20727 72 20 63 6f 64 65 73 20 74 6f 0a 2a 2a 20 69 6e  r codes to.** in
20728 64 69 63 61 74 65 20 73 75 63 63 65 73 73 20 6f  dicate success o
20729 72 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74  r failure..*/.st
2072a 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33  atic int sqlite3
2072b 49 6e 69 74 4f 6e 65 28 73 71 6c 69 74 65 33 20  InitOne(sqlite3 
2072c 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 68  *db, int iDb, ch
2072d 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 29 7b 0a  ar **pzErrMsg){.
2072e 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 43 75    int rc;.  BtCu
2072f 72 73 6f 72 20 2a 63 75 72 4d 61 69 6e 3b 0a 20  rsor *curMain;. 
20730 20 69 6e 74 20 73 69 7a 65 3b 0a 20 20 54 61 62   int size;.  Tab
20731 6c 65 20 2a 70 54 61 62 3b 0a 20 20 44 62 20 2a  le *pTab;.  Db *
20732 70 44 62 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73  pDb;.  char cons
20733 74 20 2a 61 7a 41 72 67 5b 34 5d 3b 0a 20 20 69  t *azArg[4];.  i
20734 6e 74 20 6d 65 74 61 5b 31 30 5d 3b 0a 20 20 49  nt meta[10];.  I
20735 6e 69 74 44 61 74 61 20 69 6e 69 74 44 61 74 61  nitData initData
20736 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ;.  char const *
20737 7a 4d 61 73 74 65 72 53 63 68 65 6d 61 3b 0a 20  zMasterSchema;. 
20738 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61   char const *zMa
20739 73 74 65 72 4e 61 6d 65 20 3d 20 53 43 48 45 4d  sterName = SCHEM
2073a 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 0a 20  A_TABLE(iDb);.. 
2073b 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6d 61 73   /*.  ** The mas
2073c 74 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62  ter database tab
2073d 6c 65 20 68 61 73 20 61 20 73 74 72 75 63 74 75  le has a structu
2073e 72 65 20 6c 69 6b 65 20 74 68 69 73 0a 20 20 2a  re like this.  *
2073f 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  /.  static const
20740 20 63 68 61 72 20 6d 61 73 74 65 72 5f 73 63 68   char master_sch
20741 65 6d 61 5b 5d 20 3d 20 0a 20 20 20 20 20 22 43  ema[] = .     "C
20742 52 45 41 54 45 20 54 41 42 4c 45 20 73 71 6c 69  REATE TABLE sqli
20743 74 65 5f 6d 61 73 74 65 72 28 5c 6e 22 0a 20 20  te_master(\n".  
20744 20 20 20 22 20 20 74 79 70 65 20 74 65 78 74 2c     "  type text,
20745 5c 6e 22 0a 20 20 20 20 20 22 20 20 6e 61 6d 65  \n".     "  name
20746 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22   text,\n".     "
20747 20 20 74 62 6c 5f 6e 61 6d 65 20 74 65 78 74 2c    tbl_name text,
20748 5c 6e 22 0a 20 20 20 20 20 22 20 20 72 6f 6f 74  \n".     "  root
20749 70 61 67 65 20 69 6e 74 65 67 65 72 2c 5c 6e 22  page integer,\n"
2074a 0a 20 20 20 20 20 22 20 20 73 71 6c 20 74 65 78  .     "  sql tex
2074b 74 5c 6e 22 0a 20 20 20 20 20 22 29 22 0a 20 20  t\n".     ")".  
2074c 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
2074d 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 20 20 73  _OMIT_TEMPDB.  s
2074e 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
2074f 20 74 65 6d 70 5f 6d 61 73 74 65 72 5f 73 63 68   temp_master_sch
20750 65 6d 61 5b 5d 20 3d 20 0a 20 20 20 20 20 22 43  ema[] = .     "C
20751 52 45 41 54 45 20 54 45 4d 50 20 54 41 42 4c 45  REATE TEMP TABLE
20752 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73   sqlite_temp_mas
20753 74 65 72 28 5c 6e 22 0a 20 20 20 20 20 22 20 20  ter(\n".     "  
20754 74 79 70 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20  type text,\n".  
20755 20 20 20 22 20 20 6e 61 6d 65 20 74 65 78 74 2c     "  name text,
20756 5c 6e 22 0a 20 20 20 20 20 22 20 20 74 62 6c 5f  \n".     "  tbl_
20757 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20  name text,\n".  
20758 20 20 20 22 20 20 72 6f 6f 74 70 61 67 65 20 69     "  rootpage i
20759 6e 74 65 67 65 72 2c 5c 6e 22 0a 20 20 20 20 20  nteger,\n".     
2075a 22 20 20 73 71 6c 20 74 65 78 74 5c 6e 22 0a 20  "  sql text\n". 
2075b 20 20 20 20 22 29 22 0a 20 20 3b 0a 23 65 6c 73      ")".  ;.#els
2075c 65 0a 20 20 23 64 65 66 69 6e 65 20 74 65 6d 70  e.  #define temp
2075d 5f 6d 61 73 74 65 72 5f 73 63 68 65 6d 61 20 30  _master_schema 0
2075e 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72  .#endif..  asser
2075f 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62  t( iDb>=0 && iDb
20760 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73  <db->nDb );.  as
20761 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b 69 44  sert( db->aDb[iD
20762 62 5d 2e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20  b].pSchema );.  
20763 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
20764 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d  mutex_held(db->m
20765 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
20766 74 28 20 69 44 62 3d 3d 31 20 7c 7c 20 73 71 6c  t( iDb==1 || sql
20767 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75  ite3BtreeHoldsMu
20768 74 65 78 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d  tex(db->aDb[iDb]
20769 2e 70 42 74 29 20 29 3b 0a 0a 20 20 2f 2a 20 7a  .pBt) );..  /* z
2076a 4d 61 73 74 65 72 53 63 68 65 6d 61 20 61 6e 64  MasterSchema and
2076b 20 7a 49 6e 69 74 53 63 72 69 70 74 20 61 72 65   zInitScript are
2076c 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 61 74   set to point at
2076d 20 74 68 65 20 6d 61 73 74 65 72 20 73 63 68 65   the master sche
2076e 6d 61 0a 20 20 2a 2a 20 61 6e 64 20 69 6e 69 74  ma.  ** and init
2076f 69 61 6c 69 73 61 74 69 6f 6e 20 73 63 72 69 70  ialisation scrip
20770 74 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f  t appropriate fo
20771 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62  r the database b
20772 65 69 6e 67 0a 20 20 2a 2a 20 69 6e 69 74 69 61  eing.  ** initia
20773 6c 69 73 65 64 2e 20 7a 4d 61 73 74 65 72 4e 61  lised. zMasterNa
20774 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  me is the name o
20775 66 20 74 68 65 20 6d 61 73 74 65 72 20 74 61 62  f the master tab
20776 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  le..  */.  if( !
20777 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69  OMIT_TEMPDB && i
20778 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 7a 4d 61  Db==1 ){.    zMa
20779 73 74 65 72 53 63 68 65 6d 61 20 3d 20 74 65 6d  sterSchema = tem
2077a 70 5f 6d 61 73 74 65 72 5f 73 63 68 65 6d 61 3b  p_master_schema;
2077b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 4d  .  }else{.    zM
2077c 61 73 74 65 72 53 63 68 65 6d 61 20 3d 20 6d 61  asterSchema = ma
2077d 73 74 65 72 5f 73 63 68 65 6d 61 3b 0a 20 20 7d  ster_schema;.  }
2077e 0a 20 20 7a 4d 61 73 74 65 72 4e 61 6d 65 20 3d  .  zMasterName =
2077f 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44   SCHEMA_TABLE(iD
20780 62 29 3b 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74 72  b);..  /* Constr
20781 75 63 74 20 74 68 65 20 73 63 68 65 6d 61 20 74  uct the schema t
20782 61 62 6c 65 73 2e 20 20 2a 2f 0a 20 20 61 7a 41  ables.  */.  azA
20783 72 67 5b 30 5d 20 3d 20 7a 4d 61 73 74 65 72 4e  rg[0] = zMasterN
20784 61 6d 65 3b 0a 20 20 61 7a 41 72 67 5b 31 5d 20  ame;.  azArg[1] 
20785 3d 20 22 31 22 3b 0a 20 20 61 7a 41 72 67 5b 32  = "1";.  azArg[2
20786 5d 20 3d 20 7a 4d 61 73 74 65 72 53 63 68 65 6d  ] = zMasterSchem
20787 61 3b 0a 20 20 61 7a 41 72 67 5b 33 5d 20 3d 20  a;.  azArg[3] = 
20788 30 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 64 62  0;.  initData.db
20789 20 3d 20 64 62 3b 0a 20 20 69 6e 69 74 44 61 74   = db;.  initDat
2078a 61 2e 69 44 62 20 3d 20 69 44 62 3b 0a 20 20 69  a.iDb = iDb;.  i
2078b 6e 69 74 44 61 74 61 2e 70 7a 45 72 72 4d 73 67  nitData.pzErrMsg
2078c 20 3d 20 70 7a 45 72 72 4d 73 67 3b 0a 20 20 28   = pzErrMsg;.  (
2078d 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
2078e 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 72 63 20  tyOff(db);.  rc 
2078f 3d 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61 6c  = sqlite3InitCal
20790 6c 62 61 63 6b 28 26 69 6e 69 74 44 61 74 61 2c  lback(&initData,
20791 20 33 2c 20 28 63 68 61 72 20 2a 2a 29 61 7a 41   3, (char **)azA
20792 72 67 2c 20 30 29 3b 0a 20 20 28 76 6f 69 64 29  rg, 0);.  (void)
20793 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
20794 64 62 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b  db);.  if( rc ){
20795 0a 20 20 20 20 72 63 20 3d 20 69 6e 69 74 44 61  .    rc = initDa
20796 74 61 2e 72 63 3b 0a 20 20 20 20 67 6f 74 6f 20  ta.rc;.    goto 
20797 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 20  error_out;.  }. 
20798 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46   pTab = sqlite3F
20799 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 4d 61  indTable(db, zMa
2079a 73 74 65 72 4e 61 6d 65 2c 20 64 62 2d 3e 61 44  sterName, db->aD
2079b 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  b[iDb].zName);. 
2079c 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20   if( pTab ){.   
2079d 20 70 54 61 62 2d 3e 72 65 61 64 4f 6e 6c 79 20   pTab->readOnly 
2079e 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  = 1;.  }..  /* C
2079f 72 65 61 74 65 20 61 20 63 75 72 73 6f 72 20 74  reate a cursor t
207a0 6f 20 68 6f 6c 64 20 74 68 65 20 64 61 74 61 62  o hold the datab
207a1 61 73 65 20 6f 70 65 6e 0a 20 20 2a 2f 0a 20 20  ase open.  */.  
207a2 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69  pDb = &db->aDb[i
207a3 44 62 5d 3b 0a 20 20 69 66 28 20 70 44 62 2d 3e  Db];.  if( pDb->
207a4 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  pBt==0 ){.    if
207a5 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26  ( !OMIT_TEMPDB &
207a6 26 20 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20  & iDb==1 ){.    
207a7 20 20 44 62 53 65 74 50 72 6f 70 65 72 74 79 28    DbSetProperty(
207a8 64 62 2c 20 31 2c 20 44 42 5f 53 63 68 65 6d 61  db, 1, DB_Schema
207a9 4c 6f 61 64 65 64 29 3b 0a 20 20 20 20 7d 0a 20  Loaded);.    }. 
207aa 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
207ab 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 63 75 72 4d 61  _OK;.  }.  curMa
207ac 69 6e 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  in = sqlite3Mall
207ad 6f 63 5a 65 72 6f 28 73 71 6c 69 74 65 33 42 74  ocZero(sqlite3Bt
207ae 72 65 65 43 75 72 73 6f 72 53 69 7a 65 28 29 29  reeCursorSize())
207af 3b 0a 20 20 69 66 28 20 21 63 75 72 4d 61 69 6e  ;.  if( !curMain
207b0 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
207b1 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67  ITE_NOMEM;.    g
207b2 6f 74 6f 20 65 72 72 6f 72 5f 6f 75 74 3b 0a 20  oto error_out;. 
207b3 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65   }.  sqlite3Btre
207b4 65 45 6e 74 65 72 28 70 44 62 2d 3e 70 42 74 29  eEnter(pDb->pBt)
207b5 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
207b6 42 74 72 65 65 43 75 72 73 6f 72 28 70 44 62 2d  BtreeCursor(pDb-
207b7 3e 70 42 74 2c 20 4d 41 53 54 45 52 5f 52 4f 4f  >pBt, MASTER_ROO
207b8 54 2c 20 30 2c 20 30 2c 20 63 75 72 4d 61 69 6e  T, 0, 0, curMain
207b9 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
207ba 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51  ITE_OK && rc!=SQ
207bb 4c 49 54 45 5f 45 4d 50 54 59 20 29 7b 0a 20 20  LITE_EMPTY ){.  
207bc 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
207bd 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 73 71 6c  ng(pzErrMsg, sql
207be 69 74 65 33 45 72 72 53 74 72 28 72 63 29 2c 20  ite3ErrStr(rc), 
207bf 28 63 68 61 72 2a 29 30 29 3b 0a 20 20 20 20 67  (char*)0);.    g
207c0 6f 74 6f 20 6c 65 61 76 65 5f 65 72 72 6f 72 5f  oto leave_error_
207c1 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47  out;.  }..  /* G
207c2 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20  et the database 
207c3 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  meta information
207c4 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d 65 74 61  ..  **.  ** Meta
207c5 20 76 61 6c 75 65 73 20 61 72 65 20 61 73 20 66   values are as f
207c6 6f 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 20 20 20 20  ollows:.  **    
207c7 6d 65 74 61 5b 30 5d 20 20 20 53 63 68 65 6d 61  meta[0]   Schema
207c8 20 63 6f 6f 6b 69 65 2e 20 20 43 68 61 6e 67 65   cookie.  Change
207c9 73 20 77 69 74 68 20 65 61 63 68 20 73 63 68 65  s with each sche
207ca 6d 61 20 63 68 61 6e 67 65 2e 0a 20 20 2a 2a 20  ma change..  ** 
207cb 20 20 20 6d 65 74 61 5b 31 5d 20 20 20 46 69 6c     meta[1]   Fil
207cc 65 20 66 6f 72 6d 61 74 20 6f 66 20 73 63 68 65  e format of sche
207cd 6d 61 20 6c 61 79 65 72 2e 0a 20 20 2a 2a 20 20  ma layer..  **  
207ce 20 20 6d 65 74 61 5b 32 5d 20 20 20 53 69 7a 65    meta[2]   Size
207cf 20 6f 66 20 74 68 65 20 70 61 67 65 20 63 61 63   of the page cac
207d0 68 65 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61  he..  **    meta
207d1 5b 33 5d 20 20 20 55 73 65 20 66 72 65 65 6c 69  [3]   Use freeli
207d2 73 74 20 69 66 20 30 2e 20 20 41 75 74 6f 76 61  st if 0.  Autova
207d3 63 75 75 6d 20 69 66 20 67 72 65 61 74 65 72 20  cuum if greater 
207d4 74 68 61 6e 20 7a 65 72 6f 2e 0a 20 20 2a 2a 20  than zero..  ** 
207d5 20 20 20 6d 65 74 61 5b 34 5d 20 20 20 44 62 20     meta[4]   Db 
207d6 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 2e 20 31  text encoding. 1
207d7 3a 55 54 46 2d 38 20 32 3a 55 54 46 2d 31 36 4c  :UTF-8 2:UTF-16L
207d8 45 20 33 3a 55 54 46 2d 31 36 42 45 0a 20 20 2a  E 3:UTF-16BE.  *
207d9 2a 20 20 20 20 6d 65 74 61 5b 35 5d 20 20 20 54  *    meta[5]   T
207da 68 65 20 75 73 65 72 20 63 6f 6f 6b 69 65 2e 20  he user cookie. 
207db 55 73 65 64 20 62 79 20 74 68 65 20 61 70 70 6c  Used by the appl
207dc 69 63 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 20 20  ication..  **   
207dd 20 6d 65 74 61 5b 36 5d 20 20 20 49 6e 63 72 65   meta[6]   Incre
207de 6d 65 6e 74 61 6c 2d 76 61 63 75 75 6d 20 66 6c  mental-vacuum fl
207df 61 67 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61  ag..  **    meta
207e0 5b 37 5d 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61  [7].  **    meta
207e1 5b 38 5d 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61  [8].  **    meta
207e2 5b 39 5d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f  [9].  **.  ** No
207e3 74 65 3a 20 54 68 65 20 23 64 65 66 69 6e 65 64  te: The #defined
207e4 20 53 51 4c 49 54 45 5f 55 54 46 2a 20 73 79 6d   SQLITE_UTF* sym
207e5 62 6f 6c 73 20 69 6e 20 73 71 6c 69 74 65 49 6e  bols in sqliteIn
207e6 74 2e 68 20 63 6f 72 72 65 73 70 6f 6e 64 20 74  t.h correspond t
207e7 6f 0a 20 20 2a 2a 20 74 68 65 20 70 6f 73 73 69  o.  ** the possi
207e8 62 6c 65 20 76 61 6c 75 65 73 20 6f 66 20 6d 65  ble values of me
207e9 74 61 5b 34 5d 2e 0a 20 20 2a 2f 0a 20 20 69 66  ta[4]..  */.  if
207ea 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
207eb 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  ){.    int i;.  
207ec 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53    for(i=0; rc==S
207ed 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 73 69  QLITE_OK && i<si
207ee 7a 65 6f 66 28 6d 65 74 61 29 2f 73 69 7a 65 6f  zeof(meta)/sizeo
207ef 66 28 6d 65 74 61 5b 30 5d 29 3b 20 69 2b 2b 29  f(meta[0]); i++)
207f0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
207f1 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61  ite3BtreeGetMeta
207f2 28 70 44 62 2d 3e 70 42 74 2c 20 69 2b 31 2c 20  (pDb->pBt, i+1, 
207f3 28 75 33 32 20 2a 29 26 6d 65 74 61 5b 69 5d 29  (u32 *)&meta[i])
207f4 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
207f5 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  rc ){.      sqli
207f6 74 65 33 53 65 74 53 74 72 69 6e 67 28 70 7a 45  te3SetString(pzE
207f7 72 72 4d 73 67 2c 20 73 71 6c 69 74 65 33 45 72  rrMsg, sqlite3Er
207f8 72 53 74 72 28 72 63 29 2c 20 28 63 68 61 72 2a  rStr(rc), (char*
207f9 29 30 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  )0);.      goto 
207fa 6c 65 61 76 65 5f 65 72 72 6f 72 5f 6f 75 74 3b  leave_error_out;
207fb 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
207fc 20 20 20 20 6d 65 6d 73 65 74 28 6d 65 74 61 2c      memset(meta,
207fd 20 30 2c 20 73 69 7a 65 6f 66 28 6d 65 74 61 29   0, sizeof(meta)
207fe 29 3b 0a 20 20 7d 0a 20 20 70 44 62 2d 3e 70 53  );.  }.  pDb->pS
207ff 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f  chema->schema_co
20800 6f 6b 69 65 20 3d 20 6d 65 74 61 5b 30 5d 3b 0a  okie = meta[0];.
20801 0a 20 20 2f 2a 20 49 66 20 6f 70 65 6e 69 6e 67  .  /* If opening
20802 20 61 20 6e 6f 6e 2d 65 6d 70 74 79 20 64 61 74   a non-empty dat
20803 61 62 61 73 65 2c 20 63 68 65 63 6b 20 74 68 65  abase, check the
20804 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 2e 20   text encoding. 
20805 46 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6d 61 69  For the.  ** mai
20806 6e 20 64 61 74 61 62 61 73 65 2c 20 73 65 74 20  n database, set 
20807 73 71 6c 69 74 65 33 2e 65 6e 63 20 74 6f 20 74  sqlite3.enc to t
20808 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74  he encoding of t
20809 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
2080a 2e 0a 20 20 2a 2a 20 46 6f 72 20 61 6e 20 61 74  ..  ** For an at
2080b 74 61 63 68 65 64 20 64 62 2c 20 69 74 20 69 73  tached db, it is
2080c 20 61 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65   an error if the
2080d 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 6e 6f 74   encoding is not
2080e 20 74 68 65 20 73 61 6d 65 0a 20 20 2a 2a 20 61   the same.  ** a
2080f 73 20 73 71 6c 69 74 65 33 2e 65 6e 63 2e 0a 20  s sqlite3.enc.. 
20810 20 2a 2f 0a 20 20 69 66 28 20 6d 65 74 61 5b 34   */.  if( meta[4
20811 5d 20 29 7b 20 20 2f 2a 20 74 65 78 74 20 65 6e  ] ){  /* text en
20812 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 20 20 69 66  coding */.    if
20813 28 20 69 44 62 3d 3d 30 20 29 7b 0a 20 20 20 20  ( iDb==0 ){.    
20814 20 20 2f 2a 20 49 66 20 6f 70 65 6e 69 6e 67 20    /* If opening 
20815 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
20816 65 2c 20 73 65 74 20 45 4e 43 28 64 62 29 2e 20  e, set ENC(db). 
20817 2a 2f 0a 20 20 20 20 20 20 45 4e 43 28 64 62 29  */.      ENC(db)
20818 20 3d 20 28 75 38 29 6d 65 74 61 5b 34 5d 3b 0a   = (u8)meta[4];.
20819 20 20 20 20 20 20 64 62 2d 3e 70 44 66 6c 74 43        db->pDfltC
2081a 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  oll = sqlite3Fin
2081b 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 53 51 4c  dCollSeq(db, SQL
2081c 49 54 45 5f 55 54 46 38 2c 20 22 42 49 4e 41 52  ITE_UTF8, "BINAR
2081d 59 22 2c 20 36 2c 20 30 29 3b 0a 20 20 20 20 7d  Y", 6, 0);.    }
2081e 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49  else{.      /* I
2081f 66 20 6f 70 65 6e 69 6e 67 20 61 6e 20 61 74 74  f opening an att
20820 61 63 68 65 64 20 64 61 74 61 62 61 73 65 2c 20  ached database, 
20821 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6d 75 63  the encoding muc
20822 68 20 6d 61 74 63 68 20 45 4e 43 28 64 62 29 20  h match ENC(db) 
20823 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6d 65 74  */.      if( met
20824 61 5b 34 5d 21 3d 45 4e 43 28 64 62 29 20 29 7b  a[4]!=ENC(db) ){
20825 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
20826 53 65 74 53 74 72 69 6e 67 28 70 7a 45 72 72 4d  SetString(pzErrM
20827 73 67 2c 20 22 61 74 74 61 63 68 65 64 20 64 61  sg, "attached da
20828 74 61 62 61 73 65 73 20 6d 75 73 74 20 75 73 65  tabases must use
20829 20 74 68 65 20 73 61 6d 65 22 0a 20 20 20 20 20   the same".     
2082a 20 20 20 20 20 20 20 22 20 74 65 78 74 20 65 6e         " text en
2082b 63 6f 64 69 6e 67 20 61 73 20 6d 61 69 6e 20 64  coding as main d
2082c 61 74 61 62 61 73 65 22 2c 20 28 63 68 61 72 2a  atabase", (char*
2082d 29 30 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  )0);.        rc 
2082e 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
2082f 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6c 65 61          goto lea
20830 76 65 5f 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20  ve_error_out;.  
20831 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65      }.    }.  }e
20832 6c 73 65 7b 0a 20 20 20 20 44 62 53 65 74 50 72  lse{.    DbSetPr
20833 6f 70 65 72 74 79 28 64 62 2c 20 69 44 62 2c 20  operty(db, iDb, 
20834 44 42 5f 45 6d 70 74 79 29 3b 0a 20 20 7d 0a 20  DB_Empty);.  }. 
20835 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 65   pDb->pSchema->e
20836 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 0a 0a 20  nc = ENC(db);.. 
20837 20 73 69 7a 65 20 3d 20 6d 65 74 61 5b 32 5d 3b   size = meta[2];
20838 0a 20 20 69 66 28 20 73 69 7a 65 3d 3d 30 20 29  .  if( size==0 )
20839 7b 20 73 69 7a 65 20 3d 20 53 51 4c 49 54 45 5f  { size = SQLITE_
2083a 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49  DEFAULT_CACHE_SI
2083b 5a 45 3b 20 7d 0a 20 20 69 66 28 20 73 69 7a 65  ZE; }.  if( size
2083c 3c 30 20 29 20 73 69 7a 65 20 3d 20 2d 73 69 7a  <0 ) size = -siz
2083d 65 3b 0a 20 20 70 44 62 2d 3e 70 53 63 68 65 6d  e;.  pDb->pSchem
2083e 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 20 3d 20  a->cache_size = 
2083f 73 69 7a 65 3b 0a 20 20 73 71 6c 69 74 65 33 42  size;.  sqlite3B
20840 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a 65  treeSetCacheSize
20841 28 70 44 62 2d 3e 70 42 74 2c 20 70 44 62 2d 3e  (pDb->pBt, pDb->
20842 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73  pSchema->cache_s
20843 69 7a 65 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  ize);..  /*.  **
20844 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 31 20   file_format==1 
20845 20 20 20 56 65 72 73 69 6f 6e 20 33 2e 30 2e 30     Version 3.0.0
20846 2e 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f 72 6d  ..  ** file_form
20847 61 74 3d 3d 32 20 20 20 20 56 65 72 73 69 6f 6e  at==2    Version
20848 20 33 2e 31 2e 33 2e 20 20 2f 2f 20 41 4c 54 45   3.1.3.  // ALTE
20849 52 20 54 41 42 4c 45 20 41 44 44 20 43 4f 4c 55  R TABLE ADD COLU
2084a 4d 4e 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f 72  MN.  ** file_for
2084b 6d 61 74 3d 3d 33 20 20 20 20 56 65 72 73 69 6f  mat==3    Versio
2084c 6e 20 33 2e 31 2e 34 2e 20 20 2f 2f 20 64 69 74  n 3.1.4.  // dit
2084d 74 6f 20 62 75 74 20 77 69 74 68 20 6e 6f 6e 2d  to but with non-
2084e 4e 55 4c 4c 20 64 65 66 61 75 6c 74 73 0a 20 20  NULL defaults.  
2084f 2a 2a 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d  ** file_format==
20850 34 20 20 20 20 56 65 72 73 69 6f 6e 20 33 2e 33  4    Version 3.3
20851 2e 30 2e 20 20 2f 2f 20 44 45 53 43 20 69 6e 64  .0.  // DESC ind
20852 69 63 65 73 2e 20 20 42 6f 6f 6c 65 61 6e 20 63  ices.  Boolean c
20853 6f 6e 73 74 61 6e 74 73 0a 20 20 2a 2f 0a 20 20  onstants.  */.  
20854 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69  pDb->pSchema->fi
20855 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 6d 65 74 61  le_format = meta
20856 5b 31 5d 3b 0a 20 20 69 66 28 20 70 44 62 2d 3e  [1];.  if( pDb->
20857 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f  pSchema->file_fo
20858 72 6d 61 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70  rmat==0 ){.    p
20859 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c  Db->pSchema->fil
2085a 65 5f 66 6f 72 6d 61 74 20 3d 20 31 3b 0a 20 20  e_format = 1;.  
2085b 7d 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63  }.  if( pDb->pSc
2085c 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61  hema->file_forma
2085d 74 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c  t>SQLITE_MAX_FIL
2085e 45 5f 46 4f 52 4d 41 54 20 29 7b 0a 20 20 20 20  E_FORMAT ){.    
2085f 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
20860 28 70 7a 45 72 72 4d 73 67 2c 20 22 75 6e 73 75  (pzErrMsg, "unsu
20861 70 70 6f 72 74 65 64 20 66 69 6c 65 20 66 6f 72  pported file for
20862 6d 61 74 22 2c 20 28 63 68 61 72 2a 29 30 29 3b  mat", (char*)0);
20863 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
20864 5f 45 52 52 4f 52 3b 0a 20 20 20 20 67 6f 74 6f  _ERROR;.    goto
20865 20 6c 65 61 76 65 5f 65 72 72 6f 72 5f 6f 75 74   leave_error_out
20866 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 69 63 6b  ;.  }..  /* Tick
20867 65 74 20 23 32 38 30 34 3a 20 20 57 68 65 6e 20  et #2804:  When 
20868 77 65 20 6f 70 65 6e 20 61 20 64 61 74 61 62 61  we open a databa
20869 73 65 20 69 6e 20 74 68 65 20 6e 65 77 65 72 20  se in the newer 
2086a 66 69 6c 65 20 66 6f 72 6d 61 74 2c 0a 20 20 2a  file format,.  *
2086b 2a 20 63 6c 65 61 72 20 74 68 65 20 6c 65 67 61  * clear the lega
2086c 63 79 5f 66 69 6c 65 5f 66 6f 72 6d 61 74 20 70  cy_file_format p
2086d 72 61 67 6d 61 20 66 6c 61 67 20 73 6f 20 74 68  ragma flag so th
2086e 61 74 20 61 20 56 41 43 55 55 4d 20 77 69 6c 6c  at a VACUUM will
2086f 0a 20 20 2a 2a 20 6e 6f 74 20 64 6f 77 6e 67 72  .  ** not downgr
20870 61 64 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ade the database
20871 20 61 6e 64 20 74 68 75 73 20 69 6e 76 61 6c 69   and thus invali
20872 64 61 74 65 20 61 6e 79 20 64 65 73 63 65 6e 64  date any descend
20873 69 6e 67 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73  ing.  ** indices
20874 20 74 68 61 74 20 74 68 65 20 75 73 65 72 20 6d   that the user m
20875 69 67 68 74 20 68 61 76 65 20 63 72 65 61 74 65  ight have create
20876 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 44  d..  */.  if( iD
20877 62 3d 3d 30 20 26 26 20 6d 65 74 61 5b 31 5d 3e  b==0 && meta[1]>
20878 3d 34 20 29 7b 0a 20 20 20 20 64 62 2d 3e 66 6c  =4 ){.    db->fl
20879 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 4c  ags &= ~SQLITE_L
2087a 65 67 61 63 79 46 69 6c 65 46 6d 74 3b 0a 20 20  egacyFileFmt;.  
2087b 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65  }..  /* Read the
2087c 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74   schema informat
2087d 69 6f 6e 20 6f 75 74 20 6f 66 20 74 68 65 20 73  ion out of the s
2087e 63 68 65 6d 61 20 74 61 62 6c 65 73 0a 20 20 2a  chema tables.  *
2087f 2f 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e  /.  assert( db->
20880 69 6e 69 74 2e 62 75 73 79 20 29 3b 0a 20 20 69  init.busy );.  i
20881 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 4d  f( rc==SQLITE_EM
20882 50 54 59 20 29 7b 0a 20 20 20 20 2f 2a 20 46 6f  PTY ){.    /* Fo
20883 72 20 61 6e 20 65 6d 70 74 79 20 64 61 74 61 62  r an empty datab
20884 61 73 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f  ase, there is no
20885 74 68 69 6e 67 20 74 6f 20 72 65 61 64 20 2a 2f  thing to read */
20886 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
20887 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  _OK;.  }else{.  
20888 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20    char *zSql;.  
20889 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33    zSql = sqlite3
2088a 4d 50 72 69 6e 74 66 28 64 62 2c 20 0a 20 20 20  MPrintf(db, .   
2088b 20 20 20 20 20 22 53 45 4c 45 43 54 20 6e 61 6d       "SELECT nam
2088c 65 2c 20 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c  e, rootpage, sql
2088d 20 46 52 4f 4d 20 27 25 71 27 2e 25 73 22 2c 0a   FROM '%q'.%s",.
2088e 20 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b          db->aDb[
2088f 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 4d 61 73  iDb].zName, zMas
20890 74 65 72 4e 61 6d 65 29 3b 0a 20 20 20 20 28 76  terName);.    (v
20891 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
20892 79 4f 66 66 28 64 62 29 3b 0a 23 69 66 6e 64 65  yOff(db);.#ifnde
20893 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
20894 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20  THORIZATION.    
20895 7b 0a 20 20 20 20 20 20 69 6e 74 20 28 2a 78 41  {.      int (*xA
20896 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  uth)(void*,int,c
20897 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
20898 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61   char*,const cha
20899 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  r*,const char*);
2089a 0a 20 20 20 20 20 20 78 41 75 74 68 20 3d 20 64  .      xAuth = d
2089b 62 2d 3e 78 41 75 74 68 3b 0a 20 20 20 20 20 20  b->xAuth;.      
2089c 64 62 2d 3e 78 41 75 74 68 20 3d 20 30 3b 0a 23  db->xAuth = 0;.#
2089d 65 6e 64 69 66 0a 20 20 20 20 20 20 72 63 20 3d  endif.      rc =
2089e 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62   sqlite3_exec(db
2089f 2c 20 7a 53 71 6c 2c 20 73 71 6c 69 74 65 33 49  , zSql, sqlite3I
208a0 6e 69 74 43 61 6c 6c 62 61 63 6b 2c 20 26 69 6e  nitCallback, &in
208a1 69 74 44 61 74 61 2c 20 30 29 3b 0a 23 69 66 6e  itData, 0);.#ifn
208a2 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
208a3 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20  AUTHORIZATION.  
208a4 20 20 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20      db->xAuth = 
208a5 78 41 75 74 68 3b 0a 20 20 20 20 7d 0a 23 65 6e  xAuth;.    }.#en
208a6 64 69 66 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  dif.    if( rc==
208a7 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 29 20 72  SQLITE_ABORT ) r
208a8 63 20 3d 20 69 6e 69 74 44 61 74 61 2e 72 63 3b  c = initData.rc;
208a9 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74  .    (void)sqlit
208aa 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a  e3SafetyOn(db);.
208ab 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
208ac 28 7a 53 71 6c 29 3b 0a 23 69 66 6e 64 65 66 20  (zSql);.#ifndef 
208ad 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c  SQLITE_OMIT_ANAL
208ae 59 5a 45 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  YZE.    if( rc==
208af 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
208b0 20 20 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 73     sqlite3Analys
208b1 69 73 4c 6f 61 64 28 64 62 2c 20 69 44 62 29 3b  isLoad(db, iDb);
208b2 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
208b3 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  }.  if( db->mall
208b4 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
208b5 2f 2a 20 73 71 6c 69 74 65 33 53 65 74 53 74 72  /* sqlite3SetStr
208b6 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 22 6f  ing(pzErrMsg, "o
208b7 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 2c 20 28  ut of memory", (
208b8 63 68 61 72 2a 29 30 29 3b 20 2a 2f 0a 20 20 20  char*)0); */.   
208b9 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
208ba 45 4d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52  EM;.    sqlite3R
208bb 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65  esetInternalSche
208bc 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 20  ma(db, 0);.  }. 
208bd 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
208be 4f 4b 20 7c 7c 20 28 64 62 2d 3e 66 6c 61 67 73  OK || (db->flags
208bf 26 53 51 4c 49 54 45 5f 52 65 63 6f 76 65 72 79  &SQLITE_Recovery
208c0 4d 6f 64 65 29 29 7b 0a 20 20 20 20 2f 2a 20 42  Mode)){.    /* B
208c1 6c 61 63 6b 20 6d 61 67 69 63 3a 20 49 66 20 74  lack magic: If t
208c2 68 65 20 53 51 4c 49 54 45 5f 52 65 63 6f 76 65  he SQLITE_Recove
208c3 72 79 4d 6f 64 65 20 66 6c 61 67 20 69 73 20 73  ryMode flag is s
208c4 65 74 2c 20 74 68 65 6e 20 63 6f 6e 73 69 64 65  et, then conside
208c5 72 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 63 68  r.    ** the sch
208c6 65 6d 61 20 6c 6f 61 64 65 64 2c 20 65 76 65 6e  ema loaded, even
208c7 20 69 66 20 65 72 72 6f 72 73 20 6f 63 63 75 72   if errors occur
208c8 65 64 2e 20 49 6e 20 74 68 69 73 20 73 69 74 75  ed. In this situ
208c9 61 74 69 6f 6e 20 74 68 65 20 0a 20 20 20 20 2a  ation the .    *
208ca 2a 20 63 75 72 72 65 6e 74 20 73 71 6c 69 74 65  * current sqlite
208cb 33 5f 70 72 65 70 61 72 65 28 29 20 6f 70 65 72  3_prepare() oper
208cc 61 74 69 6f 6e 20 77 69 6c 6c 20 66 61 69 6c 2c  ation will fail,
208cd 20 62 75 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69   but the followi
208ce 6e 67 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 77 69  ng one.    ** wi
208cf 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f  ll attempt to co
208d0 6d 70 69 6c 65 20 74 68 65 20 73 75 70 70 6c 69  mpile the suppli
208d1 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61 67 61  ed statement aga
208d2 69 6e 73 74 20 77 68 61 74 65 76 65 72 20 73 75  inst whatever su
208d3 62 73 65 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74  bset.    ** of t
208d4 68 65 20 73 63 68 65 6d 61 20 77 61 73 20 6c 6f  he schema was lo
208d5 61 64 65 64 20 62 65 66 6f 72 65 20 74 68 65 20  aded before the 
208d6 65 72 72 6f 72 20 6f 63 63 75 72 65 64 2e 20 54  error occured. T
208d7 68 65 20 70 72 69 6d 61 72 79 0a 20 20 20 20 2a  he primary.    *
208d8 2a 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69  * purpose of thi
208d9 73 20 69 73 20 74 6f 20 61 6c 6c 6f 77 20 61 63  s is to allow ac
208da 63 65 73 73 20 74 6f 20 74 68 65 20 73 71 6c 69  cess to the sqli
208db 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 0a  te_master table.
208dc 20 20 20 20 2a 2a 20 65 76 65 6e 20 77 68 65 6e      ** even when
208dd 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 20 68 61   its contents ha
208de 76 65 20 62 65 65 6e 20 63 6f 72 72 75 70 74 65  ve been corrupte
208df 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 44 62  d..    */.    Db
208e0 53 65 74 50 72 6f 70 65 72 74 79 28 64 62 2c 20  SetProperty(db, 
208e1 69 44 62 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f  iDb, DB_SchemaLo
208e2 61 64 65 64 29 3b 0a 20 20 20 20 72 63 20 3d 20  aded);.    rc = 
208e3 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a  SQLITE_OK;.  }..
208e4 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 66    /* Jump here f
208e5 6f 72 20 61 6e 20 65 72 72 6f 72 20 74 68 61 74  or an error that
208e6 20 6f 63 63 75 72 73 20 61 66 74 65 72 20 73 75   occurs after su
208e7 63 63 65 73 73 66 75 6c 6c 79 20 61 6c 6c 6f 63  ccessfully alloc
208e8 61 74 69 6e 67 0a 20 20 2a 2a 20 63 75 72 4d 61  ating.  ** curMa
208e9 69 6e 20 61 6e 64 20 63 61 6c 6c 69 6e 67 20 73  in and calling s
208ea 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
208eb 28 29 2e 20 46 6f 72 20 61 6e 20 65 72 72 6f 72  (). For an error
208ec 20 74 68 61 74 20 6f 63 63 75 72 73 0a 20 20 2a   that occurs.  *
208ed 2a 20 62 65 66 6f 72 65 20 74 68 61 74 20 70 6f  * before that po
208ee 69 6e 74 2c 20 6a 75 6d 70 20 74 6f 20 65 72 72  int, jump to err
208ef 6f 72 5f 6f 75 74 2e 0a 20 20 2a 2f 0a 6c 65 61  or_out..  */.lea
208f0 76 65 5f 65 72 72 6f 72 5f 6f 75 74 3a 0a 20 20  ve_error_out:.  
208f1 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73  sqlite3BtreeClos
208f2 65 43 75 72 73 6f 72 28 63 75 72 4d 61 69 6e 29  eCursor(curMain)
208f3 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
208f4 28 63 75 72 4d 61 69 6e 29 3b 0a 20 20 73 71 6c  (curMain);.  sql
208f5 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
208f6 44 62 2d 3e 70 42 74 29 3b 0a 0a 65 72 72 6f 72  Db->pBt);..error
208f7 5f 6f 75 74 3a 0a 20 20 69 66 28 20 72 63 3d 3d  _out:.  if( rc==
208f8 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20  SQLITE_NOMEM || 
208f9 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc==SQLITE_IOERR
208fa 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 64 62  _NOMEM ){.    db
208fb 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
208fc 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   1;.  }.  return
208fd 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e   rc;.}../*.** In
208fe 69 74 69 61 6c 69 7a 65 20 61 6c 6c 20 64 61 74  itialize all dat
208ff 61 62 61 73 65 20 66 69 6c 65 73 20 2d 20 74 68  abase files - th
20900 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
20901 66 69 6c 65 2c 20 74 68 65 20 66 69 6c 65 0a 2a  file, the file.*
20902 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  * used to store 
20903 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73  temporary tables
20904 2c 20 61 6e 64 20 61 6e 79 20 61 64 64 69 74 69  , and any additi
20905 6f 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66 69  onal database fi
20906 6c 65 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 75  les.** created u
20907 73 69 6e 67 20 41 54 54 41 43 48 20 73 74 61 74  sing ATTACH stat
20908 65 6d 65 6e 74 73 2e 20 20 52 65 74 75 72 6e 20  ements.  Return 
20909 61 20 73 75 63 63 65 73 73 20 63 6f 64 65 2e 20  a success code. 
2090a 20 49 66 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20   If an.** error 
2090b 6f 63 63 75 72 73 2c 20 77 72 69 74 65 20 61 6e  occurs, write an
2090c 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
2090d 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 0a 2a  nto *pzErrMsg..*
2090e 2a 0a 2a 2a 20 41 66 74 65 72 20 61 20 64 61 74  *.** After a dat
2090f 61 62 61 73 65 20 69 73 20 69 6e 69 74 69 61 6c  abase is initial
20910 69 7a 65 64 2c 20 74 68 65 20 44 42 5f 53 63 68  ized, the DB_Sch
20911 65 6d 61 4c 6f 61 64 65 64 20 62 69 74 20 69 73  emaLoaded bit is
20912 20 73 65 74 0a 2a 2a 20 62 69 74 20 69 73 20 73   set.** bit is s
20913 65 74 20 69 6e 20 74 68 65 20 66 6c 61 67 73 20  et in the flags 
20914 66 69 65 6c 64 20 6f 66 20 74 68 65 20 44 62 20  field of the Db 
20915 73 74 72 75 63 74 75 72 65 2e 20 49 66 20 74 68  structure. If th
20916 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69  e database.** fi
20917 6c 65 20 77 61 73 20 6f 66 20 7a 65 72 6f 2d 6c  le was of zero-l
20918 65 6e 67 74 68 2c 20 74 68 65 6e 20 74 68 65 20  ength, then the 
20919 44 42 5f 45 6d 70 74 79 20 66 6c 61 67 20 69 73  DB_Empty flag is
2091a 20 61 6c 73 6f 20 73 65 74 2e 0a 2a 2f 0a 53 51   also set..*/.SQ
2091b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2091c 20 73 71 6c 69 74 65 33 49 6e 69 74 28 73 71 6c   sqlite3Init(sql
2091d 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72 20 2a  ite3 *db, char *
2091e 2a 70 7a 45 72 72 4d 73 67 29 7b 0a 20 20 69 6e  *pzErrMsg){.  in
2091f 74 20 69 2c 20 72 63 3b 0a 20 20 69 6e 74 20 63  t i, rc;.  int c
20920 6f 6d 6d 69 74 5f 69 6e 74 65 72 6e 61 6c 20 3d  ommit_internal =
20921 20 21 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c   !(db->flags&SQL
20922 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65  ITE_InternChange
20923 73 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28  s);.  .  assert(
20924 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
20925 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
20926 3b 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74  ;.  if( db->init
20927 2e 62 75 73 79 20 29 20 72 65 74 75 72 6e 20 53  .busy ) return S
20928 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 72 63 20 3d  QLITE_OK;.  rc =
20929 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 64 62   SQLITE_OK;.  db
2092a 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 31 3b  ->init.busy = 1;
2092b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d  .  for(i=0; rc==
2092c 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64  SQLITE_OK && i<d
2092d 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20  b->nDb; i++){.  
2092e 20 20 69 66 28 20 44 62 48 61 73 50 72 6f 70 65    if( DbHasPrope
2092f 72 74 79 28 64 62 2c 20 69 2c 20 44 42 5f 53 63  rty(db, i, DB_Sc
20930 68 65 6d 61 4c 6f 61 64 65 64 29 20 7c 7c 20 69  hemaLoaded) || i
20931 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==1 ) continue;.
20932 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
20933 49 6e 69 74 4f 6e 65 28 64 62 2c 20 69 2c 20 70  InitOne(db, i, p
20934 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66  zErrMsg);.    if
20935 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71  ( rc ){.      sq
20936 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e  lite3ResetIntern
20937 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 69 29 3b  alSchema(db, i);
20938 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
20939 20 4f 6e 63 65 20 61 6c 6c 20 74 68 65 20 6f 74   Once all the ot
2093a 68 65 72 20 64 61 74 61 62 61 73 65 73 20 68 61  her databases ha
2093b 76 65 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69  ve been initiali
2093c 73 65 64 2c 20 6c 6f 61 64 20 74 68 65 20 73 63  sed, load the sc
2093d 68 65 6d 61 0a 20 20 2a 2a 20 66 6f 72 20 74 68  hema.  ** for th
2093e 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 2e  e TEMP database.
2093f 20 54 68 69 73 20 69 73 20 6c 6f 61 64 65 64 20   This is loaded 
20940 6c 61 73 74 2c 20 61 73 20 74 68 65 20 54 45 4d  last, as the TEM
20941 50 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20  P database.  ** 
20942 73 63 68 65 6d 61 20 6d 61 79 20 63 6f 6e 74 61  schema may conta
20943 69 6e 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f  in references to
20944 20 6f 62 6a 65 63 74 73 20 69 6e 20 6f 74 68 65   objects in othe
20945 72 20 64 61 74 61 62 61 73 65 73 2e 0a 20 20 2a  r databases..  *
20946 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
20947 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 20 20 69  _OMIT_TEMPDB.  i
20948 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
20949 20 26 26 20 64 62 2d 3e 6e 44 62 3e 31 20 26 26   && db->nDb>1 &&
2094a 20 21 44 62 48 61 73 50 72 6f 70 65 72 74 79 28   !DbHasProperty(
2094b 64 62 2c 20 31 2c 20 44 42 5f 53 63 68 65 6d 61  db, 1, DB_Schema
2094c 4c 6f 61 64 65 64 29 20 29 7b 0a 20 20 20 20 72  Loaded) ){.    r
2094d 63 20 3d 20 73 71 6c 69 74 65 33 49 6e 69 74 4f  c = sqlite3InitO
2094e 6e 65 28 64 62 2c 20 31 2c 20 70 7a 45 72 72 4d  ne(db, 1, pzErrM
2094f 73 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  sg);.    if( rc 
20950 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
20951 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68  ResetInternalSch
20952 65 6d 61 28 64 62 2c 20 31 29 3b 0a 20 20 20 20  ema(db, 1);.    
20953 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  }.  }.#endif..  
20954 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20  db->init.busy = 
20955 30 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  0;.  if( rc==SQL
20956 49 54 45 5f 4f 4b 20 26 26 20 63 6f 6d 6d 69 74  ITE_OK && commit
20957 5f 69 6e 74 65 72 6e 61 6c 20 29 7b 0a 20 20 20  _internal ){.   
20958 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e   sqlite3CommitIn
20959 74 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 64 62  ternalChanges(db
2095a 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  );.  }..  return
2095b 20 72 63 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54   rc; .}../*.** T
2095c 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
2095d 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 64 61   no-op if the da
2095e 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 69 73  tabase schema is
2095f 20 61 6c 72 65 61 64 79 20 69 6e 69 74 69 61 6c   already initial
20960 69 73 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69  ised..** Otherwi
20961 73 65 2c 20 74 68 65 20 73 63 68 65 6d 61 20 69  se, the schema i
20962 73 20 6c 6f 61 64 65 64 2e 20 41 6e 20 65 72 72  s loaded. An err
20963 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
20964 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ned..*/.SQLITE_P
20965 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
20966 65 33 52 65 61 64 53 63 68 65 6d 61 28 50 61 72  e3ReadSchema(Par
20967 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 69  se *pParse){.  i
20968 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
20969 4b 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  K;.  sqlite3 *db
2096a 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
2096b 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
2096c 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
2096d 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
2096e 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29  !db->init.busy )
2096f 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
20970 65 33 49 6e 69 74 28 64 62 2c 20 26 70 50 61 72  e3Init(db, &pPar
20971 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20  se->zErrMsg);.  
20972 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  }.  if( rc!=SQLI
20973 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61  TE_OK ){.    pPa
20974 72 73 65 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20  rse->rc = rc;.  
20975 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b    pParse->nErr++
20976 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
20977 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65  c;.}.../*.** Che
20978 63 6b 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65  ck schema cookie
20979 73 20 69 6e 20 61 6c 6c 20 64 61 74 61 62 61 73  s in all databas
2097a 65 73 2e 20 20 49 66 20 61 6e 79 20 63 6f 6f 6b  es.  If any cook
2097b 69 65 20 69 73 20 6f 75 74 0a 2a 2a 20 6f 66 20  ie is out.** of 
2097c 64 61 74 65 2c 20 72 65 74 75 72 6e 20 30 2e 20  date, return 0. 
2097d 20 49 66 20 61 6c 6c 20 73 63 68 65 6d 61 20 63   If all schema c
2097e 6f 6f 6b 69 65 73 20 61 72 65 20 63 75 72 72 65  ookies are curre
2097f 6e 74 2c 20 72 65 74 75 72 6e 20 31 2e 0a 2a 2f  nt, return 1..*/
20980 0a 73 74 61 74 69 63 20 69 6e 74 20 73 63 68 65  .static int sche
20981 6d 61 49 73 56 61 6c 69 64 28 73 71 6c 69 74 65  maIsValid(sqlite
20982 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 44  3 *db){.  int iD
20983 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42  b;.  int rc;.  B
20984 74 43 75 72 73 6f 72 20 2a 63 75 72 54 65 6d 70  tCursor *curTemp
20985 3b 0a 20 20 69 6e 74 20 63 6f 6f 6b 69 65 3b 0a  ;.  int cookie;.
20986 20 20 69 6e 74 20 61 6c 6c 4f 6b 20 3d 20 31 3b    int allOk = 1;
20987 0a 0a 20 20 63 75 72 54 65 6d 70 20 3d 20 28 42  ..  curTemp = (B
20988 74 43 75 72 73 6f 72 20 2a 29 73 71 6c 69 74 65  tCursor *)sqlite
20989 33 5f 6d 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33  3_malloc(sqlite3
2098a 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65 28  BtreeCursorSize(
2098b 29 29 3b 0a 20 20 69 66 28 20 63 75 72 54 65 6d  ));.  if( curTem
2098c 70 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  p ){.    assert(
2098d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
2098e 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
2098f 3b 0a 20 20 20 20 66 6f 72 28 69 44 62 3d 30 3b  ;.    for(iDb=0;
20990 20 61 6c 6c 4f 6b 20 26 26 20 69 44 62 3c 64 62   allOk && iDb<db
20991 2d 3e 6e 44 62 3b 20 69 44 62 2b 2b 29 7b 0a 20  ->nDb; iDb++){. 
20992 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 3b       Btree *pBt;
20993 0a 20 20 20 20 20 20 70 42 74 20 3d 20 64 62 2d  .      pBt = db-
20994 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 3b 0a 20  >aDb[iDb].pBt;. 
20995 20 20 20 20 20 69 66 28 20 70 42 74 3d 3d 30 20       if( pBt==0 
20996 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
20997 20 20 6d 65 6d 73 65 74 28 63 75 72 54 65 6d 70    memset(curTemp
20998 2c 20 30 2c 20 73 71 6c 69 74 65 33 42 74 72 65  , 0, sqlite3Btre
20999 65 43 75 72 73 6f 72 53 69 7a 65 28 29 29 3b 0a  eCursorSize());.
2099a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
2099b 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 70 42  e3BtreeCursor(pB
2099c 74 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20  t, MASTER_ROOT, 
2099d 30 2c 20 30 2c 20 63 75 72 54 65 6d 70 29 3b 0a  0, 0, curTemp);.
2099e 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
2099f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
209a0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
209a1 74 72 65 65 47 65 74 4d 65 74 61 28 70 42 74 2c  treeGetMeta(pBt,
209a2 20 31 2c 20 28 75 33 32 20 2a 29 26 63 6f 6f 6b   1, (u32 *)&cook
209a3 69 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ie);.        if(
209a4 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
209a5 26 20 63 6f 6f 6b 69 65 21 3d 64 62 2d 3e 61 44  & cookie!=db->aD
209a6 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e  b[iDb].pSchema->
209a7 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 20 29 7b  schema_cookie ){
209a8 0a 20 20 20 20 20 20 20 20 20 20 61 6c 6c 4f 6b  .          allOk
209a9 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 0;.        }.
209aa 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42          sqlite3B
209ab 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28  treeCloseCursor(
209ac 63 75 72 54 65 6d 70 29 3b 0a 20 20 20 20 20 20  curTemp);.      
209ad 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  }.      if( rc==
209ae 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20  SQLITE_NOMEM || 
209af 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc==SQLITE_IOERR
209b0 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20  _NOMEM ){.      
209b1 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
209b2 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  ed = 1;.      }.
209b3 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
209b4 33 5f 66 72 65 65 28 63 75 72 54 65 6d 70 29 3b  3_free(curTemp);
209b5 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 6c  .  }else{.    al
209b6 6c 4f 6b 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d  lOk = 0;.    db-
209b7 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
209b8 31 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  1;.  }..  return
209b9 20 61 6c 6c 4f 6b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   allOk;.}../*.**
209ba 20 43 6f 6e 76 65 72 74 20 61 20 73 63 68 65 6d   Convert a schem
209bb 61 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 74  a pointer into t
209bc 68 65 20 69 44 62 20 69 6e 64 65 78 20 74 68 61  he iDb index tha
209bd 74 20 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 77  t indicates.** w
209be 68 69 63 68 20 64 61 74 61 62 61 73 65 20 66 69  hich database fi
209bf 6c 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20  le in db->aDb[] 
209c0 74 68 65 20 73 63 68 65 6d 61 20 72 65 66 65 72  the schema refer
209c1 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  s to..**.** If t
209c2 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
209c3 20 69 73 20 61 74 74 61 63 68 65 64 20 6d 6f 72   is attached mor
209c4 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65  e than once, the
209c5 20 66 69 72 73 74 0a 2a 2a 20 61 74 74 61 63 68   first.** attach
209c6 65 64 20 64 61 74 61 62 61 73 65 20 69 73 20 72  ed database is r
209c7 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  eturned..*/.SQLI
209c8 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
209c9 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
209ca 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  dex(sqlite3 *db,
209cb 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61   Schema *pSchema
209cc 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 2d 31 30  ){.  int i = -10
209cd 30 30 30 30 30 3b 0a 0a 20 20 2f 2a 20 49 66 20  00000;..  /* If 
209ce 70 53 63 68 65 6d 61 20 69 73 20 4e 55 4c 4c 2c  pSchema is NULL,
209cf 20 74 68 65 6e 20 72 65 74 75 72 6e 20 2d 31 30   then return -10
209d0 30 30 30 30 30 2e 20 54 68 69 73 20 68 61 70 70  00000. This happ
209d1 65 6e 73 20 77 68 65 6e 20 63 6f 64 65 20 69 6e  ens when code in
209d2 20 0a 20 20 2a 2a 20 65 78 70 72 2e 63 20 69 73   .  ** expr.c is
209d3 20 74 72 79 69 6e 67 20 74 6f 20 72 65 73 6f 6c   trying to resol
209d4 76 65 20 61 20 72 65 66 65 72 65 6e 63 65 20 74  ve a reference t
209d5 6f 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74 61  o a transient ta
209d6 62 6c 65 20 28 69 2e 65 2e 20 6f 6e 65 0a 20 20  ble (i.e. one.  
209d7 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 61 20  ** created by a 
209d8 73 75 62 2d 73 65 6c 65 63 74 29 2e 20 49 6e 20  sub-select). In 
209d9 74 68 69 73 20 63 61 73 65 20 74 68 65 20 72 65  this case the re
209da 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68  turn value of th
209db 69 73 20 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f  is .  ** functio
209dc 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62  n should never b
209dd 65 20 75 73 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a  e used..  **.  *
209de 2a 20 57 65 20 72 65 74 75 72 6e 20 2d 31 30 30  * We return -100
209df 30 30 30 30 20 69 6e 73 74 65 61 64 20 6f 66 20  0000 instead of 
209e0 74 68 65 20 6d 6f 72 65 20 75 73 75 61 6c 20 2d  the more usual -
209e1 31 20 73 69 6d 70 6c 79 20 62 65 63 61 75 73 65  1 simply because
209e2 20 75 73 69 6e 67 0a 20 20 2a 2a 20 2d 31 30 30   using.  ** -100
209e3 30 30 30 30 20 61 73 20 69 6e 63 6f 72 72 65 63  0000 as incorrec
209e4 74 6c 79 20 75 73 69 6e 67 20 2d 31 30 30 30 30  tly using -10000
209e5 30 30 20 69 6e 64 65 78 20 69 6e 74 6f 20 64 62  00 index into db
209e6 2d 3e 61 44 62 5b 5d 20 69 73 20 6d 75 63 68 20  ->aDb[] is much 
209e7 0a 20 20 2a 2a 20 6d 6f 72 65 20 6c 69 6b 65 6c  .  ** more likel
209e8 79 20 74 6f 20 63 61 75 73 65 20 61 20 73 65 67  y to cause a seg
209e9 66 61 75 6c 74 20 74 68 61 6e 20 2d 31 20 28 6f  fault than -1 (o
209ea 66 20 63 6f 75 72 73 65 20 74 68 65 72 65 20 61  f course there a
209eb 72 65 20 61 73 73 65 72 74 28 29 0a 20 20 2a 2a  re assert().  **
209ec 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 6f 2c   statements too,
209ed 20 62 75 74 20 69 74 20 6e 65 76 65 72 20 68 75   but it never hu
209ee 72 74 73 20 74 6f 20 70 6c 61 79 20 74 68 65 20  rts to play the 
209ef 6f 64 64 73 29 2e 0a 20 20 2a 2f 0a 20 20 61 73  odds)..  */.  as
209f0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
209f1 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
209f2 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 53 63  ex) );.  if( pSc
209f3 68 65 6d 61 20 29 7b 0a 20 20 20 20 66 6f 72 28  hema ){.    for(
209f4 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  i=0; i<db->nDb; 
209f5 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
209f6 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 53 63 68 65  db->aDb[i].pSche
209f7 6d 61 3d 3d 70 53 63 68 65 6d 61 20 29 7b 0a 20  ma==pSchema ){. 
209f8 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
209f9 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
209fa 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 69  assert( i>=0 &&i
209fb 3e 3d 30 20 26 26 20 20 69 3c 64 62 2d 3e 6e 44  >=0 &&  i<db->nD
209fc 62 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  b );.  }.  retur
209fd 6e 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  n i;.}../*.** Co
209fe 6d 70 69 6c 65 20 74 68 65 20 55 54 46 2d 38 20  mpile the UTF-8 
209ff 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74  encoded SQL stat
20a00 65 6d 65 6e 74 20 7a 53 71 6c 20 69 6e 74 6f 20  ement zSql into 
20a01 61 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64  a statement hand
20a02 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
20a03 74 20 73 71 6c 69 74 65 33 50 72 65 70 61 72 65  t sqlite3Prepare
20a04 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
20a05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
20a06 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
20a07 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  . */.  const cha
20a08 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20  r *zSql,        
20a09 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65   /* UTF-8 encode
20a0a 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  d SQL statement.
20a0b 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73   */.  int nBytes
20a0c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
20a0d 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71  /* Length of zSq
20a0e 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20  l in bytes. */. 
20a0f 20 69 6e 74 20 73 61 76 65 53 71 6c 46 6c 61 67   int saveSqlFlag
20a10 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  ,          /* Tr
20a11 75 65 20 74 6f 20 63 6f 70 79 20 53 51 4c 20 74  ue to copy SQL t
20a12 65 78 74 20 69 6e 74 6f 20 74 68 65 20 73 71 6c  ext into the sql
20a13 69 74 65 33 5f 73 74 6d 74 20 2a 2f 0a 20 20 73  ite3_stmt */.  s
20a14 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70  qlite3_stmt **pp
20a15 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a  Stmt,    /* OUT:
20a16 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
20a17 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  e prepared state
20a18 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ment */.  const 
20a19 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20  char **pzTail   
20a1a 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20      /* OUT: End 
20a1b 6f 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67  of parsed string
20a1c 20 2a 2f 0a 29 7b 0a 20 20 50 61 72 73 65 20 73   */.){.  Parse s
20a1d 50 61 72 73 65 3b 0a 20 20 63 68 61 72 20 2a 7a  Parse;.  char *z
20a1e 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 6e  ErrMsg = 0;.  in
20a1f 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
20a20 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 61 73  ;.  int i;..  as
20a21 73 65 72 74 28 20 70 70 53 74 6d 74 20 29 3b 0a  sert( ppStmt );.
20a22 20 20 2a 70 70 53 74 6d 74 20 3d 20 30 3b 0a 20    *ppStmt = 0;. 
20a23 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
20a24 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20  tyOn(db) ){.    
20a25 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
20a26 53 55 53 45 3b 0a 20 20 7d 0a 20 20 61 73 73 65  SUSE;.  }.  asse
20a27 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rt( !db->mallocF
20a28 61 69 6c 65 64 20 29 3b 0a 20 20 61 73 73 65 72  ailed );.  asser
20a29 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
20a2a 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
20a2b 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 79   );..  /* If any
20a2c 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
20a2d 73 65 20 73 63 68 65 6d 61 73 20 61 72 65 20 6c  se schemas are l
20a2e 6f 63 6b 65 64 2c 20 64 6f 20 6e 6f 74 20 70 72  ocked, do not pr
20a2f 6f 63 65 65 64 20 77 69 74 68 0a 20 20 2a 2a 20  oceed with.  ** 
20a30 63 6f 6d 70 69 6c 61 74 69 6f 6e 2e 20 49 6e 73  compilation. Ins
20a31 74 65 61 64 20 72 65 74 75 72 6e 20 53 51 4c 49  tead return SQLI
20a32 54 45 5f 4c 4f 43 4b 45 44 20 69 6d 6d 65 64 69  TE_LOCKED immedi
20a33 61 74 65 6c 79 2e 0a 20 20 2a 2f 0a 20 20 66 6f  ately..  */.  fo
20a34 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62  r(i=0; i<db->nDb
20a35 3b 20 69 2b 2b 29 20 7b 0a 20 20 20 20 42 74 72  ; i++) {.    Btr
20a36 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44  ee *pBt = db->aD
20a37 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66  b[i].pBt;.    if
20a38 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 69  ( pBt ){.      i
20a39 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 72 63 20  nt rc;.      rc 
20a3a 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63  = sqlite3BtreeSc
20a3b 68 65 6d 61 4c 6f 63 6b 65 64 28 70 42 74 29 3b  hemaLocked(pBt);
20a3c 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b  .      if( rc ){
20a3d 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63  .        const c
20a3e 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61  har *zDb = db->a
20a3f 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  Db[i].zName;.   
20a40 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
20a41 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4c 4f 43  r(db, SQLITE_LOC
20a42 4b 45 44 2c 20 22 64 61 74 61 62 61 73 65 20 73  KED, "database s
20a43 63 68 65 6d 61 20 69 73 20 6c 6f 63 6b 65 64 3a  chema is locked:
20a44 20 25 73 22 2c 20 7a 44 62 29 3b 0a 20 20 20 20   %s", zDb);.    
20a45 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
20a46 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
20a47 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
20a48 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20  QLITE_LOCKED;.  
20a49 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
20a4a 20 20 0a 20 20 6d 65 6d 73 65 74 28 26 73 50 61    .  memset(&sPa
20a4b 72 73 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  rse, 0, sizeof(s
20a4c 50 61 72 73 65 29 29 3b 0a 20 20 73 50 61 72 73  Parse));.  sPars
20a4d 65 2e 64 62 20 3d 20 64 62 3b 0a 20 20 69 66 28  e.db = db;.  if(
20a4e 20 6e 42 79 74 65 73 3e 3d 30 20 26 26 20 7a 53   nBytes>=0 && zS
20a4f 71 6c 5b 6e 42 79 74 65 73 2d 31 5d 21 3d 30 20  ql[nBytes-1]!=0 
20a50 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 71  ){.    char *zSq
20a51 6c 43 6f 70 79 3b 0a 20 20 20 20 69 6e 74 20 6d  lCopy;.    int m
20a52 78 4c 65 6e 20 3d 20 64 62 2d 3e 61 4c 69 6d 69  xLen = db->aLimi
20a53 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53  t[SQLITE_LIMIT_S
20a54 51 4c 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 20 20  QL_LENGTH];.    
20a55 69 66 28 20 6e 42 79 74 65 73 3e 6d 78 4c 65 6e  if( nBytes>mxLen
20a56 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
20a57 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
20a58 45 5f 54 4f 4f 42 49 47 2c 20 22 73 74 61 74 65  E_TOOBIG, "state
20a59 6d 65 6e 74 20 74 6f 6f 20 6c 6f 6e 67 22 29 3b  ment too long");
20a5a 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c  .      (void)sql
20a5b 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
20a5c 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
20a5d 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20  SQLITE_TOOBIG;. 
20a5e 20 20 20 7d 0a 20 20 20 20 7a 53 71 6c 43 6f 70     }.    zSqlCop
20a5f 79 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  y = sqlite3DbStr
20a60 4e 44 75 70 28 64 62 2c 20 7a 53 71 6c 2c 20 6e  NDup(db, zSql, n
20a61 42 79 74 65 73 29 3b 0a 20 20 20 20 69 66 28 20  Bytes);.    if( 
20a62 7a 53 71 6c 43 6f 70 79 20 29 7b 0a 20 20 20 20  zSqlCopy ){.    
20a63 20 20 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73    sqlite3RunPars
20a64 65 72 28 26 73 50 61 72 73 65 2c 20 7a 53 71 6c  er(&sParse, zSql
20a65 43 6f 70 79 2c 20 26 7a 45 72 72 4d 73 67 29 3b  Copy, &zErrMsg);
20a66 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
20a67 72 65 65 28 7a 53 71 6c 43 6f 70 79 29 3b 0a 20  ree(zSqlCopy);. 
20a68 20 20 20 20 20 73 50 61 72 73 65 2e 7a 54 61 69       sParse.zTai
20a69 6c 20 3d 20 26 7a 53 71 6c 5b 73 50 61 72 73 65  l = &zSql[sParse
20a6a 2e 7a 54 61 69 6c 2d 7a 53 71 6c 43 6f 70 79 5d  .zTail-zSqlCopy]
20a6b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
20a6c 20 20 20 73 50 61 72 73 65 2e 7a 54 61 69 6c 20     sParse.zTail 
20a6d 3d 20 26 7a 53 71 6c 5b 6e 42 79 74 65 73 5d 3b  = &zSql[nBytes];
20a6e 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
20a6f 20 20 20 20 73 71 6c 69 74 65 33 52 75 6e 50 61      sqlite3RunPa
20a70 72 73 65 72 28 26 73 50 61 72 73 65 2c 20 7a 53  rser(&sParse, zS
20a71 71 6c 2c 20 26 7a 45 72 72 4d 73 67 29 3b 0a 20  ql, &zErrMsg);. 
20a72 20 7d 0a 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61   }..  if( db->ma
20a73 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
20a74 20 20 73 50 61 72 73 65 2e 72 63 20 3d 20 53 51    sParse.rc = SQ
20a75 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
20a76 20 20 69 66 28 20 73 50 61 72 73 65 2e 72 63 3d    if( sParse.rc=
20a77 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 73  =SQLITE_DONE ) s
20a78 50 61 72 73 65 2e 72 63 20 3d 20 53 51 4c 49 54  Parse.rc = SQLIT
20a79 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 73 50 61 72  E_OK;.  if( sPar
20a7a 73 65 2e 63 68 65 63 6b 53 63 68 65 6d 61 20 26  se.checkSchema &
20a7b 26 20 21 73 63 68 65 6d 61 49 73 56 61 6c 69 64  & !schemaIsValid
20a7c 28 64 62 29 20 29 7b 0a 20 20 20 20 73 50 61 72  (db) ){.    sPar
20a7d 73 65 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 53  se.rc = SQLITE_S
20a7e 43 48 45 4d 41 3b 0a 20 20 7d 0a 20 20 69 66 28  CHEMA;.  }.  if(
20a7f 20 73 50 61 72 73 65 2e 72 63 3d 3d 53 51 4c 49   sParse.rc==SQLI
20a80 54 45 5f 53 43 48 45 4d 41 20 29 7b 0a 20 20 20  TE_SCHEMA ){.   
20a81 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
20a82 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20  ernalSchema(db, 
20a83 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62  0);.  }.  if( db
20a84 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
20a85 7b 0a 20 20 20 20 73 50 61 72 73 65 2e 72 63 20  {.    sParse.rc 
20a86 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
20a87 20 20 7d 0a 20 20 69 66 28 20 70 7a 54 61 69 6c    }.  if( pzTail
20a88 20 29 7b 0a 20 20 20 20 2a 70 7a 54 61 69 6c 20   ){.    *pzTail 
20a89 3d 20 73 50 61 72 73 65 2e 7a 54 61 69 6c 3b 0a  = sParse.zTail;.
20a8a 20 20 7d 0a 20 20 72 63 20 3d 20 73 50 61 72 73    }.  rc = sPars
20a8b 65 2e 72 63 3b 0a 0a 23 69 66 6e 64 65 66 20 53  e.rc;..#ifndef S
20a8c 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41  QLITE_OMIT_EXPLA
20a8d 49 4e 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  IN.  if( rc==SQL
20a8e 49 54 45 5f 4f 4b 20 26 26 20 73 50 61 72 73 65  ITE_OK && sParse
20a8f 2e 70 56 64 62 65 20 26 26 20 73 50 61 72 73 65  .pVdbe && sParse
20a90 2e 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20  .explain ){.    
20a91 69 66 28 20 73 50 61 72 73 65 2e 65 78 70 6c 61  if( sParse.expla
20a92 69 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20 73  in==2 ){.      s
20a93 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
20a94 43 6f 6c 73 28 73 50 61 72 73 65 2e 70 56 64 62  Cols(sParse.pVdb
20a95 65 2c 20 33 29 3b 0a 20 20 20 20 20 20 73 71 6c  e, 3);.      sql
20a96 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
20a97 6d 65 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c  me(sParse.pVdbe,
20a98 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
20a99 2c 20 22 6f 72 64 65 72 22 2c 20 50 34 5f 53 54  , "order", P4_ST
20a9a 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c  ATIC);.      sql
20a9b 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
20a9c 6d 65 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c  me(sParse.pVdbe,
20a9d 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   1, COLNAME_NAME
20a9e 2c 20 22 66 72 6f 6d 22 2c 20 50 34 5f 53 54 41  , "from", P4_STA
20a9f 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  TIC);.      sqli
20aa0 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
20aa1 65 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20  e(sParse.pVdbe, 
20aa2 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  2, COLNAME_NAME,
20aa3 20 22 64 65 74 61 69 6c 22 2c 20 50 34 5f 53 54   "detail", P4_ST
20aa4 41 54 49 43 29 3b 0a 20 20 20 20 7d 65 6c 73 65  ATIC);.    }else
20aa5 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
20aa6 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 73 50  dbeSetNumCols(sP
20aa7 61 72 73 65 2e 70 56 64 62 65 2c 20 38 29 3b 0a  arse.pVdbe, 8);.
20aa8 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
20aa9 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72  eSetColName(sPar
20aaa 73 65 2e 70 56 64 62 65 2c 20 30 2c 20 43 4f 4c  se.pVdbe, 0, COL
20aab 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 61 64 64 72  NAME_NAME, "addr
20aac 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  ", P4_STATIC);. 
20aad 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
20aae 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72 73  SetColName(sPars
20aaf 65 2e 70 56 64 62 65 2c 20 31 2c 20 43 4f 4c 4e  e.pVdbe, 1, COLN
20ab0 41 4d 45 5f 4e 41 4d 45 2c 20 22 6f 70 63 6f 64  AME_NAME, "opcod
20ab1 65 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a  e", P4_STATIC);.
20ab2 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
20ab3 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72  eSetColName(sPar
20ab4 73 65 2e 70 56 64 62 65 2c 20 32 2c 20 43 4f 4c  se.pVdbe, 2, COL
20ab5 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 31 22 2c  NAME_NAME, "p1",
20ab6 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P4_STATIC);.   
20ab7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
20ab8 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72 73 65 2e  tColName(sParse.
20ab9 70 56 64 62 65 2c 20 33 2c 20 43 4f 4c 4e 41 4d  pVdbe, 3, COLNAM
20aba 45 5f 4e 41 4d 45 2c 20 22 70 32 22 2c 20 50 34  E_NAME, "p2", P4
20abb 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
20abc 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
20abd 6c 4e 61 6d 65 28 73 50 61 72 73 65 2e 70 56 64  lName(sParse.pVd
20abe 62 65 2c 20 34 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  be, 4, COLNAME_N
20abf 41 4d 45 2c 20 22 70 33 22 2c 20 50 34 5f 53 54  AME, "p3", P4_ST
20ac0 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c  ATIC);.      sql
20ac1 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
20ac2 6d 65 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c  me(sParse.pVdbe,
20ac3 20 35 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   5, COLNAME_NAME
20ac4 2c 20 22 70 34 22 2c 20 50 34 5f 53 54 41 54 49  , "p4", P4_STATI
20ac5 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  C);.      sqlite
20ac6 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
20ac7 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20 36 2c  sParse.pVdbe, 6,
20ac8 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
20ac9 70 35 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b  p5", P4_STATIC);
20aca 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
20acb 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61  beSetColName(sPa
20acc 72 73 65 2e 70 56 64 62 65 2c 20 37 2c 20 43 4f  rse.pVdbe, 7, CO
20acd 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 63 6f 6d  LNAME_NAME, "com
20ace 6d 65 6e 74 22 2c 50 34 5f 53 54 41 54 49 43 29  ment",P4_STATIC)
20acf 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
20ad0 69 66 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65  if..  if( sqlite
20ad1 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29  3SafetyOff(db) )
20ad2 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
20ad3 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 0a 20  E_MISUSE;.  }.. 
20ad4 20 69 66 28 20 73 61 76 65 53 71 6c 46 6c 61 67   if( saveSqlFlag
20ad5 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
20ad6 64 62 65 53 65 74 53 71 6c 28 73 50 61 72 73 65  dbeSetSql(sParse
20ad7 2e 70 56 64 62 65 2c 20 7a 53 71 6c 2c 20 73 50  .pVdbe, zSql, sP
20ad8 61 72 73 65 2e 7a 54 61 69 6c 20 2d 20 7a 53 71  arse.zTail - zSq
20ad9 6c 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63  l);.  }.  if( rc
20ada 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 64  !=SQLITE_OK || d
20adb 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
20adc 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
20add 69 6e 61 6c 69 7a 65 28 28 73 71 6c 69 74 65 33  inalize((sqlite3
20ade 5f 73 74 6d 74 2a 29 73 50 61 72 73 65 2e 70 56  _stmt*)sParse.pV
20adf 64 62 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74  dbe);.    assert
20ae0 28 21 28 2a 70 70 53 74 6d 74 29 29 3b 0a 20 20  (!(*ppStmt));.  
20ae1 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 53 74  }else{.    *ppSt
20ae2 6d 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74  mt = (sqlite3_st
20ae3 6d 74 2a 29 73 50 61 72 73 65 2e 70 56 64 62 65  mt*)sParse.pVdbe
20ae4 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 7a 45 72  ;.  }..  if( zEr
20ae5 72 4d 73 67 20 29 7b 0a 20 20 20 20 73 71 6c 69  rMsg ){.    sqli
20ae6 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c  te3Error(db, rc,
20ae7 20 22 25 73 22 2c 20 7a 45 72 72 4d 73 67 29 3b   "%s", zErrMsg);
20ae8 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
20ae9 65 28 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d 65  e(zErrMsg);.  }e
20aea 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
20aeb 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 30 29  Error(db, rc, 0)
20aec 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71  ;.  }..  rc = sq
20aed 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c  lite3ApiExit(db,
20aee 20 72 63 29 3b 0a 20 20 61 73 73 65 72 74 28 20   rc);.  assert( 
20aef 28 72 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29  (rc&db->errMask)
20af0 3d 3d 72 63 20 29 3b 0a 20 20 72 65 74 75 72 6e  ==rc );.  return
20af1 20 72 63 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e   rc;.}.static in
20af2 74 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e 64  t sqlite3LockAnd
20af3 50 72 65 70 61 72 65 28 0a 20 20 73 71 6c 69 74  Prepare(.  sqlit
20af4 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
20af5 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
20af6 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 63 6f   handle. */.  co
20af7 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20  nst char *zSql, 
20af8 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38          /* UTF-8
20af9 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61   encoded SQL sta
20afa 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74  tement. */.  int
20afb 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20   nBytes,        
20afc 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
20afd 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65   of zSql in byte
20afe 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65  s. */.  int save
20aff 53 71 6c 46 6c 61 67 2c 20 20 20 20 20 20 20 20  SqlFlag,        
20b00 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 63 6f 70    /* True to cop
20b01 79 20 53 51 4c 20 74 65 78 74 20 69 6e 74 6f 20  y SQL text into 
20b02 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  the sqlite3_stmt
20b03 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
20b04 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20  mt **ppStmt,    
20b05 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65  /* OUT: A pointe
20b06 72 20 74 6f 20 74 68 65 20 70 72 65 70 61 72 65  r to the prepare
20b07 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  d statement */. 
20b08 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a   const char **pz
20b09 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55  Tail       /* OU
20b0a 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64  T: End of parsed
20b0b 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20   string */.){.  
20b0c 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 21 73  int rc;.  if( !s
20b0d 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63  qlite3SafetyChec
20b0e 6b 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20 20 72  kOk(db) ){.    r
20b0f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
20b10 55 53 45 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  USE;.  }.  sqlit
20b11 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
20b12 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c  b->mutex);.  sql
20b13 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c  ite3BtreeEnterAl
20b14 6c 28 64 62 29 3b 0a 20 20 72 63 20 3d 20 73 71  l(db);.  rc = sq
20b15 6c 69 74 65 33 50 72 65 70 61 72 65 28 64 62 2c  lite3Prepare(db,
20b16 20 7a 53 71 6c 2c 20 6e 42 79 74 65 73 2c 20 73   zSql, nBytes, s
20b17 61 76 65 53 71 6c 46 6c 61 67 2c 20 70 70 53 74  aveSqlFlag, ppSt
20b18 6d 74 2c 20 70 7a 54 61 69 6c 29 3b 0a 20 20 73  mt, pzTail);.  s
20b19 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
20b1a 41 6c 6c 28 64 62 29 3b 0a 20 20 73 71 6c 69 74  All(db);.  sqlit
20b1b 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
20b1c 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
20b1d 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
20b1e 20 52 65 72 75 6e 20 74 68 65 20 63 6f 6d 70 69   Rerun the compi
20b1f 6c 61 74 69 6f 6e 20 6f 66 20 61 20 73 74 61 74  lation of a stat
20b20 65 6d 65 6e 74 20 61 66 74 65 72 20 61 20 73 63  ement after a sc
20b21 68 65 6d 61 20 63 68 61 6e 67 65 2e 0a 2a 2a 20  hema change..** 
20b22 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74  Return true if t
20b23 68 65 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73  he statement was
20b24 20 72 65 63 6f 6d 70 69 6c 65 64 20 73 75 63 63   recompiled succ
20b25 65 73 73 66 75 6c 6c 79 2e 0a 2a 2a 20 52 65 74  essfully..** Ret
20b26 75 72 6e 20 66 61 6c 73 65 20 69 66 20 74 68 65  urn false if the
20b27 72 65 20 69 73 20 61 6e 20 65 72 72 6f 72 20 6f  re is an error o
20b28 66 20 73 6f 6d 65 20 6b 69 6e 64 2e 0a 2a 2f 0a  f some kind..*/.
20b29 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
20b2a 6e 74 20 73 71 6c 69 74 65 33 52 65 70 72 65 70  nt sqlite3Reprep
20b2b 61 72 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  are(Vdbe *p){.  
20b2c 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
20b2d 33 5f 73 74 6d 74 20 2a 70 4e 65 77 3b 0a 20 20  3_stmt *pNew;.  
20b2e 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
20b2f 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  ;.  sqlite3 *db;
20b30 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
20b31 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73  te3_mutex_held(s
20b32 71 6c 69 74 65 33 56 64 62 65 44 62 28 70 29 2d  qlite3VdbeDb(p)-
20b33 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 7a 53 71  >mutex) );.  zSq
20b34 6c 20 3d 20 73 71 6c 69 74 65 33 5f 73 71 6c 28  l = sqlite3_sql(
20b35 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29  (sqlite3_stmt *)
20b36 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 53  p);.  assert( zS
20b37 71 6c 21 3d 30 20 29 3b 20 20 2f 2a 20 52 65 70  ql!=0 );  /* Rep
20b38 72 65 70 61 72 65 20 6f 6e 6c 79 20 63 61 6c 6c  repare only call
20b39 65 64 20 66 6f 72 20 70 72 65 70 61 72 65 5f 76  ed for prepare_v
20b3a 32 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 2a  2() statements *
20b3b 2f 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65 33  /.  db = sqlite3
20b3c 56 64 62 65 44 62 28 70 29 3b 0a 20 20 61 73 73  VdbeDb(p);.  ass
20b3d 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
20b3e 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
20b3f 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  x) );.  rc = sql
20b40 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61  ite3LockAndPrepa
20b41 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c  re(db, zSql, -1,
20b42 20 30 2c 20 26 70 4e 65 77 2c 20 30 29 3b 0a 20   0, &pNew, 0);. 
20b43 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 69   if( rc ){.    i
20b44 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
20b45 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 64 62 2d  MEM ){.      db-
20b46 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
20b47 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  1;.    }.    ass
20b48 65 72 74 28 20 70 4e 65 77 3d 3d 30 20 29 3b 0a  ert( pNew==0 );.
20b49 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
20b4a 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
20b4b 74 28 20 70 4e 65 77 21 3d 30 20 29 3b 0a 20 20  t( pNew!=0 );.  
20b4c 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 53  }.  sqlite3VdbeS
20b4d 77 61 70 28 28 56 64 62 65 2a 29 70 4e 65 77 2c  wap((Vdbe*)pNew,
20b4e 20 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74   p);.  sqlite3_t
20b4f 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67 73  ransfer_bindings
20b50 28 70 4e 65 77 2c 20 28 73 71 6c 69 74 65 33 5f  (pNew, (sqlite3_
20b51 73 74 6d 74 2a 29 70 29 3b 0a 20 20 73 71 6c 69  stmt*)p);.  sqli
20b52 74 65 33 56 64 62 65 52 65 73 65 74 53 74 65 70  te3VdbeResetStep
20b53 52 65 73 75 6c 74 28 28 56 64 62 65 2a 29 70 4e  Result((Vdbe*)pN
20b54 65 77 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ew);.  sqlite3Vd
20b55 62 65 46 69 6e 61 6c 69 7a 65 28 28 56 64 62 65  beFinalize((Vdbe
20b56 2a 29 70 4e 65 77 29 3b 0a 20 20 72 65 74 75 72  *)pNew);.  retur
20b57 6e 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54  n 1;.}.../*.** T
20b58 77 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74  wo versions of t
20b59 68 65 20 6f 66 66 69 63 69 61 6c 20 41 50 49 2e  he official API.
20b5a 20 20 4c 65 67 61 63 79 20 61 6e 64 20 6e 65 77    Legacy and new
20b5b 20 75 73 65 2e 20 20 49 6e 20 74 68 65 20 6c 65   use.  In the le
20b5c 67 61 63 79 0a 2a 2a 20 76 65 72 73 69 6f 6e 2c  gacy.** version,
20b5d 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51   the original SQ
20b5e 4c 20 74 65 78 74 20 69 73 20 6e 6f 74 20 73 61  L text is not sa
20b5f 76 65 64 20 69 6e 20 74 68 65 20 70 72 65 70 61  ved in the prepa
20b60 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a  red statement.**
20b61 20 61 6e 64 20 73 6f 20 69 66 20 61 20 73 63 68   and so if a sch
20b62 65 6d 61 20 63 68 61 6e 67 65 20 6f 63 63 75 72  ema change occur
20b63 73 2c 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  s, SQLITE_SCHEMA
20b64 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 0a   is returned by.
20b65 2a 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  ** sqlite3_step(
20b66 29 2e 20 20 49 6e 20 74 68 65 20 6e 65 77 20 76  ).  In the new v
20b67 65 72 73 69 6f 6e 2c 20 74 68 65 20 6f 72 69 67  ersion, the orig
20b68 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 69 73  inal SQL text is
20b69 20 72 65 74 61 69 6e 65 64 0a 2a 2a 20 61 6e 64   retained.** and
20b6a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69   the statement i
20b6b 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  s automatically 
20b6c 72 65 63 6f 6d 70 69 6c 65 64 20 69 66 20 61 6e  recompiled if an
20b6d 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 0a 2a   schema change.*
20b6e 2a 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c  * occurs..*/.SQL
20b6f 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
20b70 74 65 33 5f 70 72 65 70 61 72 65 28 0a 20 20 73  te3_prepare(.  s
20b71 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
20b72 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
20b73 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a  base handle. */.
20b74 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
20b75 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55  ql,         /* U
20b76 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c  TF-8 encoded SQL
20b77 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20   statement. */. 
20b78 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20   int nBytes,    
20b79 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
20b7a 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20  ngth of zSql in 
20b7b 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69  bytes. */.  sqli
20b7c 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d  te3_stmt **ppStm
20b7d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41 20  t,    /* OUT: A 
20b7e 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70  pointer to the p
20b7f 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
20b80 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  t */.  const cha
20b81 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 20  r **pzTail      
20b82 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66 20   /* OUT: End of 
20b83 70 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a 2f  parsed string */
20b84 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
20b85 72 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 6b  rc = sqlite3Lock
20b86 41 6e 64 50 72 65 70 61 72 65 28 64 62 2c 7a 53  AndPrepare(db,zS
20b87 71 6c 2c 6e 42 79 74 65 73 2c 30 2c 70 70 53 74  ql,nBytes,0,ppSt
20b88 6d 74 2c 70 7a 54 61 69 6c 29 3b 0a 20 20 61 73  mt,pzTail);.  as
20b89 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
20b8a 5f 4f 4b 20 7c 7c 20 70 70 53 74 6d 74 3d 3d 30  _OK || ppStmt==0
20b8b 20 7c 7c 20 2a 70 70 53 74 6d 74 3d 3d 30 20 29   || *ppStmt==0 )
20b8c 3b 20 20 2f 2a 20 56 45 52 49 46 59 3a 20 46 31  ;  /* VERIFY: F1
20b8d 33 30 32 31 20 2a 2f 0a 20 20 72 65 74 75 72 6e  3021 */.  return
20b8e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50   rc;.}.SQLITE_AP
20b8f 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72  I int sqlite3_pr
20b90 65 70 61 72 65 5f 76 32 28 0a 20 20 73 71 6c 69  epare_v2(.  sqli
20b91 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
20b92 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
20b93 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 63  e handle. */.  c
20b94 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c  onst char *zSql,
20b95 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d           /* UTF-
20b96 38 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74  8 encoded SQL st
20b97 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e  atement. */.  in
20b98 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20  t nBytes,       
20b99 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
20b9a 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74  h of zSql in byt
20b9b 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  es. */.  sqlite3
20b9c 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20  _stmt **ppStmt, 
20b9d 20 20 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69     /* OUT: A poi
20b9e 6e 74 65 72 20 74 6f 20 74 68 65 20 70 72 65 70  nter to the prep
20b9f 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 2a  ared statement *
20ba0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
20ba1 2a 70 7a 54 61 69 6c 20 20 20 20 20 20 20 2f 2a  *pzTail       /*
20ba2 20 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70 61 72   OUT: End of par
20ba3 73 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b  sed string */.){
20ba4 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 72 63 20  .  int rc;.  rc 
20ba5 3d 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e 64  = sqlite3LockAnd
20ba6 50 72 65 70 61 72 65 28 64 62 2c 7a 53 71 6c 2c  Prepare(db,zSql,
20ba7 6e 42 79 74 65 73 2c 31 2c 70 70 53 74 6d 74 2c  nBytes,1,ppStmt,
20ba8 70 7a 54 61 69 6c 29 3b 0a 20 20 61 73 73 65 72  pzTail);.  asser
20ba9 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
20baa 20 7c 7c 20 70 70 53 74 6d 74 3d 3d 30 20 7c 7c   || ppStmt==0 ||
20bab 20 2a 70 70 53 74 6d 74 3d 3d 30 20 29 3b 20 20   *ppStmt==0 );  
20bac 2f 2a 20 56 45 52 49 46 59 3a 20 46 31 33 30 32  /* VERIFY: F1302
20bad 31 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 63  1 */.  return rc
20bae 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51  ;.}...#ifndef SQ
20baf 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
20bb0 2f 2a 0a 2a 2a 20 43 6f 6d 70 69 6c 65 20 74 68  /*.** Compile th
20bb1 65 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64  e UTF-16 encoded
20bb2 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 7a   SQL statement z
20bb3 53 71 6c 20 69 6e 74 6f 20 61 20 73 74 61 74 65  Sql into a state
20bb4 6d 65 6e 74 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a  ment handle..*/.
20bb5 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74  static int sqlit
20bb6 65 33 50 72 65 70 61 72 65 31 36 28 0a 20 20 73  e3Prepare16(.  s
20bb7 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
20bb8 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
20bb9 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 20  base handle. */ 
20bba 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
20bbb 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Sql,         /* 
20bbc 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51  UTF-8 encoded SQ
20bbd 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a  L statement. */.
20bbe 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20    int nBytes,   
20bbf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
20bc0 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e  ength of zSql in
20bc1 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 69 6e 74   bytes. */.  int
20bc2 20 73 61 76 65 53 71 6c 46 6c 61 67 2c 20 20 20   saveSqlFlag,   
20bc3 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
20bc4 6f 20 73 61 76 65 20 53 51 4c 20 74 65 78 74 20  o save SQL text 
20bc5 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  into the sqlite3
20bc6 5f 73 74 6d 74 20 2a 2f 0a 20 20 73 71 6c 69 74  _stmt */.  sqlit
20bc7 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74  e3_stmt **ppStmt
20bc8 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41 20 70  ,    /* OUT: A p
20bc9 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 72  ointer to the pr
20bca 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
20bcb 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
20bcc 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 20 20   **pzTail       
20bcd 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70  /* OUT: End of p
20bce 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a  arsed string */.
20bcf 29 7b 0a 20 20 2f 2a 20 54 68 69 73 20 66 75 6e  ){.  /* This fun
20bd0 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20  ction currently 
20bd1 77 6f 72 6b 73 20 62 79 20 66 69 72 73 74 20 74  works by first t
20bd2 72 61 6e 73 66 6f 72 6d 69 6e 67 20 74 68 65 20  ransforming the 
20bd3 55 54 46 2d 31 36 0a 20 20 2a 2a 20 65 6e 63 6f  UTF-16.  ** enco
20bd4 64 65 64 20 73 74 72 69 6e 67 20 74 6f 20 55 54  ded string to UT
20bd5 46 2d 38 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 69  F-8, then invoki
20bd6 6e 67 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  ng sqlite3_prepa
20bd7 72 65 28 29 2e 20 54 68 65 0a 20 20 2a 2a 20 74  re(). The.  ** t
20bd8 72 69 63 6b 79 20 62 69 74 20 69 73 20 66 69 67  ricky bit is fig
20bd9 75 72 69 6e 67 20 6f 75 74 20 74 68 65 20 70 6f  uring out the po
20bda 69 6e 74 65 72 20 74 6f 20 72 65 74 75 72 6e 20  inter to return 
20bdb 69 6e 20 2a 70 7a 54 61 69 6c 2e 0a 20 20 2a 2f  in *pzTail..  */
20bdc 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 38 3b 0a  .  char *zSql8;.
20bdd 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
20bde 61 69 6c 38 20 3d 20 30 3b 0a 20 20 69 6e 74 20  ail8 = 0;.  int 
20bdf 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
20be0 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 53  .  if( !sqlite3S
20be1 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 64 62 29  afetyCheckOk(db)
20be2 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
20be3 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
20be4 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
20be5 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
20be6 78 29 3b 0a 20 20 7a 53 71 6c 38 20 3d 20 73 71  x);.  zSql8 = sq
20be7 6c 69 74 65 33 55 74 66 31 36 74 6f 38 28 64 62  lite3Utf16to8(db
20be8 2c 20 7a 53 71 6c 2c 20 6e 42 79 74 65 73 29 3b  , zSql, nBytes);
20be9 0a 20 20 69 66 28 20 7a 53 71 6c 38 20 29 7b 0a  .  if( zSql8 ){.
20bea 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
20beb 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72 65 28 64  LockAndPrepare(d
20bec 62 2c 20 7a 53 71 6c 38 2c 20 2d 31 2c 20 73 61  b, zSql8, -1, sa
20bed 76 65 53 71 6c 46 6c 61 67 2c 20 70 70 53 74 6d  veSqlFlag, ppStm
20bee 74 2c 20 26 7a 54 61 69 6c 38 29 3b 0a 20 20 7d  t, &zTail8);.  }
20bef 0a 0a 20 20 69 66 28 20 7a 54 61 69 6c 38 20 26  ..  if( zTail8 &
20bf0 26 20 70 7a 54 61 69 6c 20 29 7b 0a 20 20 20 20  & pzTail ){.    
20bf1 2f 2a 20 49 66 20 73 71 6c 69 74 65 33 5f 70 72  /* If sqlite3_pr
20bf2 65 70 61 72 65 20 72 65 74 75 72 6e 73 20 61 20  epare returns a 
20bf3 74 61 69 6c 20 70 6f 69 6e 74 65 72 2c 20 77 65  tail pointer, we
20bf4 20 63 61 6c 63 75 6c 61 74 65 20 74 68 65 0a 20   calculate the. 
20bf5 20 20 20 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74     ** equivalent
20bf6 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 74 68   pointer into th
20bf7 65 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20  e UTF-16 string 
20bf8 62 79 20 63 6f 75 6e 74 69 6e 67 20 74 68 65 20  by counting the 
20bf9 75 6e 69 63 6f 64 65 0a 20 20 20 20 2a 2a 20 63  unicode.    ** c
20bfa 68 61 72 61 63 74 65 72 73 20 62 65 74 77 65 65  haracters betwee
20bfb 6e 20 7a 53 71 6c 38 20 61 6e 64 20 7a 54 61 69  n zSql8 and zTai
20bfc 6c 38 2c 20 61 6e 64 20 74 68 65 6e 20 72 65 74  l8, and then ret
20bfd 75 72 6e 69 6e 67 20 61 20 70 6f 69 6e 74 65 72  urning a pointer
20bfe 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 61 6d 65  .    ** the same
20bff 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61   number of chara
20c00 63 74 65 72 73 20 69 6e 74 6f 20 74 68 65 20 55  cters into the U
20c01 54 46 2d 31 36 20 73 74 72 69 6e 67 2e 0a 20 20  TF-16 string..  
20c02 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63 68 61    */.    int cha
20c03 72 73 5f 70 61 72 73 65 64 20 3d 20 73 71 6c 69  rs_parsed = sqli
20c04 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 7a  te3Utf8CharLen(z
20c05 53 71 6c 38 2c 20 7a 54 61 69 6c 38 2d 7a 53 71  Sql8, zTail8-zSq
20c06 6c 38 29 3b 0a 20 20 20 20 2a 70 7a 54 61 69 6c  l8);.    *pzTail
20c07 20 3d 20 28 75 38 20 2a 29 7a 53 71 6c 20 2b 20   = (u8 *)zSql + 
20c08 73 71 6c 69 74 65 33 55 74 66 31 36 42 79 74 65  sqlite3Utf16Byte
20c09 4c 65 6e 28 7a 53 71 6c 2c 20 63 68 61 72 73 5f  Len(zSql, chars_
20c0a 70 61 72 73 65 64 29 3b 0a 20 20 7d 0a 20 20 73  parsed);.  }.  s
20c0b 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 71 6c  qlite3_free(zSql
20c0c 38 29 3b 20 0a 20 20 72 63 20 3d 20 73 71 6c 69  8); .  rc = sqli
20c0d 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72  te3ApiExit(db, r
20c0e 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  c);.  sqlite3_mu
20c0f 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
20c10 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  tex);.  return r
20c11 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 77 6f 20  c;.}../*.** Two 
20c12 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20  versions of the 
20c13 6f 66 66 69 63 69 61 6c 20 41 50 49 2e 20 20 4c  official API.  L
20c14 65 67 61 63 79 20 61 6e 64 20 6e 65 77 20 75 73  egacy and new us
20c15 65 2e 20 20 49 6e 20 74 68 65 20 6c 65 67 61 63  e.  In the legac
20c16 79 0a 2a 2a 20 76 65 72 73 69 6f 6e 2c 20 74 68  y.** version, th
20c17 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74  e original SQL t
20c18 65 78 74 20 69 73 20 6e 6f 74 20 73 61 76 65 64  ext is not saved
20c19 20 69 6e 20 74 68 65 20 70 72 65 70 61 72 65 64   in the prepared
20c1a 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 61 6e   statement.** an
20c1b 64 20 73 6f 20 69 66 20 61 20 73 63 68 65 6d 61  d so if a schema
20c1c 20 63 68 61 6e 67 65 20 6f 63 63 75 72 73 2c 20   change occurs, 
20c1d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 69 73  SQLITE_SCHEMA is
20c1e 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20   returned by.** 
20c1f 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20  sqlite3_step(). 
20c20 20 49 6e 20 74 68 65 20 6e 65 77 20 76 65 72 73   In the new vers
20c21 69 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69 6e 61  ion, the origina
20c22 6c 20 53 51 4c 20 74 65 78 74 20 69 73 20 72 65  l SQL text is re
20c23 74 61 69 6e 65 64 0a 2a 2a 20 61 6e 64 20 74 68  tained.** and th
20c24 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61  e statement is a
20c25 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 65 63  utomatically rec
20c26 6f 6d 70 69 6c 65 64 20 69 66 20 61 6e 20 73 63  ompiled if an sc
20c27 68 65 6d 61 20 63 68 61 6e 67 65 0a 2a 2a 20 6f  hema change.** o
20c28 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ccurs..*/.SQLITE
20c29 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
20c2a 5f 70 72 65 70 61 72 65 31 36 28 0a 20 20 73 71  _prepare16(.  sq
20c2b 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
20c2c 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
20c2d 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 20 0a  ase handle. */ .
20c2e 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53    const void *zS
20c2f 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55  ql,         /* U
20c30 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c  TF-8 encoded SQL
20c31 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20   statement. */. 
20c32 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20   int nBytes,    
20c33 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
20c34 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20  ngth of zSql in 
20c35 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69  bytes. */.  sqli
20c36 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d  te3_stmt **ppStm
20c37 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41 20  t,    /* OUT: A 
20c38 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70  pointer to the p
20c39 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
20c3a 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69  t */.  const voi
20c3b 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 20  d **pzTail      
20c3c 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66 20   /* OUT: End of 
20c3d 70 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a 2f  parsed string */
20c3e 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
20c3f 72 63 20 3d 20 73 71 6c 69 74 65 33 50 72 65 70  rc = sqlite3Prep
20c40 61 72 65 31 36 28 64 62 2c 7a 53 71 6c 2c 6e 42  are16(db,zSql,nB
20c41 79 74 65 73 2c 30 2c 70 70 53 74 6d 74 2c 70 7a  ytes,0,ppStmt,pz
20c42 54 61 69 6c 29 3b 0a 20 20 61 73 73 65 72 74 28  Tail);.  assert(
20c43 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc==SQLITE_OK |
20c44 7c 20 70 70 53 74 6d 74 3d 3d 30 20 7c 7c 20 2a  | ppStmt==0 || *
20c45 70 70 53 74 6d 74 3d 3d 30 20 29 3b 20 20 2f 2a  ppStmt==0 );  /*
20c46 20 56 45 52 49 46 59 3a 20 46 31 33 30 32 31 20   VERIFY: F13021 
20c47 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  */.  return rc;.
20c48 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
20c49 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
20c4a 31 36 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33  16_v2(.  sqlite3
20c4b 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
20c4c 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68     /* Database h
20c4d 61 6e 64 6c 65 2e 20 2a 2f 20 0a 20 20 63 6f 6e  andle. */ .  con
20c4e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20  st void *zSql,  
20c4f 20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20         /* UTF-8 
20c50 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74  encoded SQL stat
20c51 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20  ement. */.  int 
20c52 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20  nBytes,         
20c53 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
20c54 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73  of zSql in bytes
20c55 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  . */.  sqlite3_s
20c56 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20  tmt **ppStmt,   
20c57 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74   /* OUT: A point
20c58 65 72 20 74 6f 20 74 68 65 20 70 72 65 70 61 72  er to the prepar
20c59 65 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  ed statement */.
20c5a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70    const void **p
20c5b 7a 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f  zTail       /* O
20c5c 55 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65  UT: End of parse
20c5d 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20  d string */.){. 
20c5e 20 69 6e 74 20 72 63 3b 0a 20 20 72 63 20 3d 20   int rc;.  rc = 
20c5f 73 71 6c 69 74 65 33 50 72 65 70 61 72 65 31 36  sqlite3Prepare16
20c60 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 65 73 2c  (db,zSql,nBytes,
20c61 31 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29  1,ppStmt,pzTail)
20c62 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  ;.  assert( rc==
20c63 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 70 53  SQLITE_OK || ppS
20c64 74 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70 53 74 6d  tmt==0 || *ppStm
20c65 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 56 45 52 49  t==0 );  /* VERI
20c66 46 59 3a 20 46 31 33 30 32 31 20 2a 2f 0a 20 20  FY: F13021 */.  
20c67 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65  return rc;.}..#e
20c68 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
20c69 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a  MIT_UTF16 */../*
20c6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
20c6b 64 20 6f 66 20 70 72 65 70 61 72 65 2e 63 20 2a  d of prepare.c *
20c6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20c6d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20c6e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
20c6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
20c70 67 69 6e 20 66 69 6c 65 20 73 65 6c 65 63 74 2e  gin file select.
20c71 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
20c72 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20c73 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
20c74 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
20c75 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
20c76 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
20c77 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
20c78 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
20c79 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
20c7a 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
20c7b 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
20c7c 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
20c7d 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
20c7e 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
20c7f 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
20c80 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
20c81 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
20c82 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
20c83 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
20c84 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
20c85 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
20c86 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
20c87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20c88 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20c89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20c8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20c8b 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
20c8c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64  e contains C cod
20c8d 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20  e routines that 
20c8e 61 72 65 20 63 61 6c 6c 65 64 20 62 79 20 74 68  are called by th
20c8f 65 20 70 61 72 73 65 72 0a 2a 2a 20 74 6f 20 68  e parser.** to h
20c90 61 6e 64 6c 65 20 53 45 4c 45 43 54 20 73 74 61  andle SELECT sta
20c91 74 65 6d 65 6e 74 73 20 69 6e 20 53 51 4c 69 74  tements in SQLit
20c92 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 73 65  e..**.** $Id: se
20c93 6c 65 63 74 2e 63 2c 76 20 31 2e 34 32 39 20 32  lect.c,v 1.429 2
20c94 30 30 38 2f 30 35 2f 30 31 20 31 37 3a 30 33 3a  008/05/01 17:03:
20c95 34 39 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  49 drh Exp $.*/.
20c96 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61  ../*.** Delete a
20c97 6c 6c 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  ll the content o
20c98 66 20 61 20 53 65 6c 65 63 74 20 73 74 72 75 63  f a Select struc
20c99 74 75 72 65 20 62 75 74 20 64 6f 20 6e 6f 74 20  ture but do not 
20c9a 64 65 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 74 68  deallocate.** th
20c9b 65 20 73 65 6c 65 63 74 20 73 74 72 75 63 74 75  e select structu
20c9c 72 65 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a 73 74  re itself..*/.st
20c9d 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 53  atic void clearS
20c9e 65 6c 65 63 74 28 53 65 6c 65 63 74 20 2a 70 29  elect(Select *p)
20c9f 7b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  {.  sqlite3ExprL
20ca0 69 73 74 44 65 6c 65 74 65 28 70 2d 3e 70 45 4c  istDelete(p->pEL
20ca1 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  ist);.  sqlite3S
20ca2 72 63 4c 69 73 74 44 65 6c 65 74 65 28 70 2d 3e  rcListDelete(p->
20ca3 70 53 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33  pSrc);.  sqlite3
20ca4 45 78 70 72 44 65 6c 65 74 65 28 70 2d 3e 70 57  ExprDelete(p->pW
20ca5 68 65 72 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  here);.  sqlite3
20ca6 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70  ExprListDelete(p
20ca7 2d 3e 70 47 72 6f 75 70 42 79 29 3b 0a 20 20 73  ->pGroupBy);.  s
20ca8 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
20ca9 28 70 2d 3e 70 48 61 76 69 6e 67 29 3b 0a 20 20  (p->pHaving);.  
20caa 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
20cab 65 6c 65 74 65 28 70 2d 3e 70 4f 72 64 65 72 42  elete(p->pOrderB
20cac 79 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c  y);.  sqlite3Sel
20cad 65 63 74 44 65 6c 65 74 65 28 70 2d 3e 70 50 72  ectDelete(p->pPr
20cae 69 6f 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ior);.  sqlite3E
20caf 78 70 72 44 65 6c 65 74 65 28 70 2d 3e 70 4c 69  xprDelete(p->pLi
20cb0 6d 69 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  mit);.  sqlite3E
20cb1 78 70 72 44 65 6c 65 74 65 28 70 2d 3e 70 4f 66  xprDelete(p->pOf
20cb2 66 73 65 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  fset);.}../*.** 
20cb3 49 6e 69 74 69 61 6c 69 7a 65 20 61 20 53 65 6c  Initialize a Sel
20cb4 65 63 74 44 65 73 74 20 73 74 72 75 63 74 75 72  ectDest structur
20cb5 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
20cb6 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
20cb7 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28  3SelectDestInit(
20cb8 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73  SelectDest *pDes
20cb9 74 2c 20 69 6e 74 20 65 44 65 73 74 2c 20 69 6e  t, int eDest, in
20cba 74 20 69 50 61 72 6d 29 7b 0a 20 20 70 44 65 73  t iParm){.  pDes
20cbb 74 2d 3e 65 44 65 73 74 20 3d 20 65 44 65 73 74  t->eDest = eDest
20cbc 3b 0a 20 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  ;.  pDest->iParm
20cbd 20 3d 20 69 50 61 72 6d 3b 0a 20 20 70 44 65 73   = iParm;.  pDes
20cbe 74 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 30 3b  t->affinity = 0;
20cbf 0a 20 20 70 44 65 73 74 2d 3e 69 4d 65 6d 20 3d  .  pDest->iMem =
20cc0 20 30 3b 0a 20 20 70 44 65 73 74 2d 3e 6e 4d 65   0;.  pDest->nMe
20cc1 6d 20 3d 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  m = 0;.}.../*.**
20cc2 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   Allocate a new 
20cc3 53 65 6c 65 63 74 20 73 74 72 75 63 74 75 72 65  Select structure
20cc4 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f   and return a po
20cc5 69 6e 74 65 72 20 74 6f 20 74 68 61 74 0a 2a 2a  inter to that.**
20cc6 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53   structure..*/.S
20cc7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 65  QLITE_PRIVATE Se
20cc8 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c  lect *sqlite3Sel
20cc9 65 63 74 4e 65 77 28 0a 20 20 50 61 72 73 65 20  ectNew(.  Parse 
20cca 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
20ccb 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
20ccc 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  xt */.  ExprList
20ccd 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 2f 2a   *pEList,     /*
20cce 20 77 68 69 63 68 20 63 6f 6c 75 6d 6e 73 20 74   which columns t
20ccf 6f 20 69 6e 63 6c 75 64 65 20 69 6e 20 74 68 65  o include in the
20cd0 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 53 72 63   result */.  Src
20cd1 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20  List *pSrc,     
20cd2 20 20 20 2f 2a 20 74 68 65 20 46 52 4f 4d 20 63     /* the FROM c
20cd3 6c 61 75 73 65 20 2d 2d 20 77 68 69 63 68 20 74  lause -- which t
20cd4 61 62 6c 65 73 20 74 6f 20 73 63 61 6e 20 2a 2f  ables to scan */
20cd5 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c  .  Expr *pWhere,
20cd6 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20           /* the 
20cd7 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
20cd8 20 20 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f    ExprList *pGro
20cd9 75 70 42 79 2c 20 20 20 2f 2a 20 74 68 65 20 47  upBy,   /* the G
20cda 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a  ROUP BY clause *
20cdb 2f 0a 20 20 45 78 70 72 20 2a 70 48 61 76 69 6e  /.  Expr *pHavin
20cdc 67 2c 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65  g,        /* the
20cdd 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 20 2a   HAVING clause *
20cde 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f  /.  ExprList *pO
20cdf 72 64 65 72 42 79 2c 20 20 20 2f 2a 20 74 68 65  rderBy,   /* the
20ce0 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
20ce1 20 2a 2f 0a 20 20 69 6e 74 20 69 73 44 69 73 74   */.  int isDist
20ce2 69 6e 63 74 2c 20 20 20 20 20 20 20 2f 2a 20 74  inct,       /* t
20ce3 72 75 65 20 69 66 20 74 68 65 20 44 49 53 54 49  rue if the DISTI
20ce4 4e 43 54 20 6b 65 79 77 6f 72 64 20 69 73 20 70  NCT keyword is p
20ce5 72 65 73 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72  resent */.  Expr
20ce6 20 2a 70 4c 69 6d 69 74 2c 20 20 20 20 20 20 20   *pLimit,       
20ce7 20 20 2f 2a 20 4c 49 4d 49 54 20 76 61 6c 75 65    /* LIMIT value
20ce8 2e 20 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f  .  NULL means no
20ce9 74 20 75 73 65 64 20 2a 2f 0a 20 20 45 78 70 72  t used */.  Expr
20cea 20 2a 70 4f 66 66 73 65 74 20 20 20 20 20 20 20   *pOffset       
20ceb 20 20 2f 2a 20 4f 46 46 53 45 54 20 76 61 6c 75    /* OFFSET valu
20cec 65 2e 20 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e  e.  NULL means n
20ced 6f 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20  o offset */.){. 
20cee 20 53 65 6c 65 63 74 20 2a 70 4e 65 77 3b 0a 20   Select *pNew;. 
20cef 20 53 65 6c 65 63 74 20 73 74 61 6e 64 69 6e 3b   Select standin;
20cf0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
20cf1 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70   pParse->db;.  p
20cf2 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  New = sqlite3DbM
20cf3 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
20cf4 7a 65 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20  zeof(*pNew) );. 
20cf5 20 61 73 73 65 72 74 28 20 21 70 4f 66 66 73 65   assert( !pOffse
20cf6 74 20 7c 7c 20 70 4c 69 6d 69 74 20 29 3b 20 20  t || pLimit );  
20cf7 20 2f 2a 20 43 61 6e 27 74 20 68 61 76 65 20 4f   /* Can't have O
20cf8 46 46 53 45 54 20 77 69 74 68 6f 75 74 20 4c 49  FFSET without LI
20cf9 4d 49 54 2e 20 2a 2f 0a 20 20 69 66 28 20 70 4e  MIT. */.  if( pN
20cfa 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e 65  ew==0 ){.    pNe
20cfb 77 20 3d 20 26 73 74 61 6e 64 69 6e 3b 0a 20 20  w = &standin;.  
20cfc 20 20 6d 65 6d 73 65 74 28 70 4e 65 77 2c 20 30    memset(pNew, 0
20cfd 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29  , sizeof(*pNew))
20cfe 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 45 4c 69  ;.  }.  if( pELi
20cff 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 45 4c  st==0 ){.    pEL
20d00 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ist = sqlite3Exp
20d01 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
20d02 73 65 2c 20 30 2c 20 73 71 6c 69 74 65 33 45 78  se, 0, sqlite3Ex
20d03 70 72 28 64 62 2c 54 4b 5f 41 4c 4c 2c 30 2c 30  pr(db,TK_ALL,0,0
20d04 2c 30 29 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70  ,0), 0);.  }.  p
20d05 4e 65 77 2d 3e 70 45 4c 69 73 74 20 3d 20 70 45  New->pEList = pE
20d06 4c 69 73 74 3b 0a 20 20 70 4e 65 77 2d 3e 70 53  List;.  pNew->pS
20d07 72 63 20 3d 20 70 53 72 63 3b 0a 20 20 70 4e 65  rc = pSrc;.  pNe
20d08 77 2d 3e 70 57 68 65 72 65 20 3d 20 70 57 68 65  w->pWhere = pWhe
20d09 72 65 3b 0a 20 20 70 4e 65 77 2d 3e 70 47 72 6f  re;.  pNew->pGro
20d0a 75 70 42 79 20 3d 20 70 47 72 6f 75 70 42 79 3b  upBy = pGroupBy;
20d0b 0a 20 20 70 4e 65 77 2d 3e 70 48 61 76 69 6e 67  .  pNew->pHaving
20d0c 20 3d 20 70 48 61 76 69 6e 67 3b 0a 20 20 70 4e   = pHaving;.  pN
20d0d 65 77 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 70  ew->pOrderBy = p
20d0e 4f 72 64 65 72 42 79 3b 0a 20 20 70 4e 65 77 2d  OrderBy;.  pNew-
20d0f 3e 69 73 44 69 73 74 69 6e 63 74 20 3d 20 69 73  >isDistinct = is
20d10 44 69 73 74 69 6e 63 74 3b 0a 20 20 70 4e 65 77  Distinct;.  pNew
20d11 2d 3e 6f 70 20 3d 20 54 4b 5f 53 45 4c 45 43 54  ->op = TK_SELECT
20d12 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 66 66  ;.  assert( pOff
20d13 73 65 74 3d 3d 30 20 7c 7c 20 70 4c 69 6d 69 74  set==0 || pLimit
20d14 21 3d 30 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 70  !=0 );.  pNew->p
20d15 4c 69 6d 69 74 20 3d 20 70 4c 69 6d 69 74 3b 0a  Limit = pLimit;.
20d16 20 20 70 4e 65 77 2d 3e 70 4f 66 66 73 65 74 20    pNew->pOffset 
20d17 3d 20 70 4f 66 66 73 65 74 3b 0a 20 20 70 4e 65  = pOffset;.  pNe
20d18 77 2d 3e 69 4c 69 6d 69 74 20 3d 20 2d 31 3b 0a  w->iLimit = -1;.
20d19 20 20 70 4e 65 77 2d 3e 69 4f 66 66 73 65 74 20    pNew->iOffset 
20d1a 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64  = -1;.  pNew->ad
20d1b 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20  drOpenEphm[0] = 
20d1c 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72  -1;.  pNew->addr
20d1d 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d 31  OpenEphm[1] = -1
20d1e 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70  ;.  pNew->addrOp
20d1f 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a  enEphm[2] = -1;.
20d20 20 20 69 66 28 20 70 4e 65 77 3d 3d 26 73 74 61    if( pNew==&sta
20d21 6e 64 69 6e 29 20 7b 0a 20 20 20 20 63 6c 65 61  ndin) {.    clea
20d22 72 53 65 6c 65 63 74 28 70 4e 65 77 29 3b 0a 20  rSelect(pNew);. 
20d23 20 20 20 70 4e 65 77 20 3d 20 30 3b 0a 20 20 7d     pNew = 0;.  }
20d24 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a  .  return pNew;.
20d25 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  }../*.** Delete 
20d26 74 68 65 20 67 69 76 65 6e 20 53 65 6c 65 63 74  the given Select
20d27 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 61   structure and a
20d28 6c 6c 20 6f 66 20 69 74 73 20 73 75 62 73 74 72  ll of its substr
20d29 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 53 51 4c 49  uctures..*/.SQLI
20d2a 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
20d2b 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
20d2c 65 74 65 28 53 65 6c 65 63 74 20 2a 70 29 7b 0a  ete(Select *p){.
20d2d 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 63    if( p ){.    c
20d2e 6c 65 61 72 53 65 6c 65 63 74 28 70 29 3b 0a 20  learSelect(p);. 
20d2f 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
20d30 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  p);.  }.}../*.**
20d31 20 47 69 76 65 6e 20 31 20 74 6f 20 33 20 69 64   Given 1 to 3 id
20d32 65 6e 74 69 66 69 65 72 73 20 70 72 65 63 65 65  entifiers precee
20d33 64 69 6e 67 20 74 68 65 20 4a 4f 49 4e 20 6b 65  ding the JOIN ke
20d34 79 77 6f 72 64 2c 20 64 65 74 65 72 6d 69 6e 65  yword, determine
20d35 20 74 68 65 0a 2a 2a 20 74 79 70 65 20 6f 66 20   the.** type of 
20d36 6a 6f 69 6e 2e 20 20 52 65 74 75 72 6e 20 61 6e  join.  Return an
20d37 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e   integer constan
20d38 74 20 74 68 61 74 20 65 78 70 72 65 73 73 65 73  t that expresses
20d39 20 74 68 61 74 20 74 79 70 65 0a 2a 2a 20 69 6e   that type.** in
20d3a 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 66 6f   terms of the fo
20d3b 6c 6c 6f 77 69 6e 67 20 62 69 74 20 76 61 6c 75  llowing bit valu
20d3c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4a 54  es:.**.**     JT
20d3d 5f 49 4e 4e 45 52 0a 2a 2a 20 20 20 20 20 4a 54  _INNER.**     JT
20d3e 5f 43 52 4f 53 53 0a 2a 2a 20 20 20 20 20 4a 54  _CROSS.**     JT
20d3f 5f 4f 55 54 45 52 0a 2a 2a 20 20 20 20 20 4a 54  _OUTER.**     JT
20d40 5f 4e 41 54 55 52 41 4c 0a 2a 2a 20 20 20 20 20  _NATURAL.**     
20d41 4a 54 5f 4c 45 46 54 0a 2a 2a 20 20 20 20 20 4a  JT_LEFT.**     J
20d42 54 5f 52 49 47 48 54 0a 2a 2a 0a 2a 2a 20 41 20  T_RIGHT.**.** A 
20d43 66 75 6c 6c 20 6f 75 74 65 72 20 6a 6f 69 6e 20  full outer join 
20d44 69 73 20 74 68 65 20 63 6f 6d 62 69 6e 61 74 69  is the combinati
20d45 6f 6e 20 6f 66 20 4a 54 5f 4c 45 46 54 20 61 6e  on of JT_LEFT an
20d46 64 20 4a 54 5f 52 49 47 48 54 2e 0a 2a 2a 0a 2a  d JT_RIGHT..**.*
20d47 2a 20 49 66 20 61 6e 20 69 6c 6c 65 67 61 6c 20  * If an illegal 
20d48 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6a  or unsupported j
20d49 6f 69 6e 20 74 79 70 65 20 69 73 20 73 65 65 6e  oin type is seen
20d4a 2c 20 74 68 65 6e 20 73 74 69 6c 6c 20 72 65 74  , then still ret
20d4b 75 72 6e 0a 2a 2a 20 61 20 6a 6f 69 6e 20 74 79  urn.** a join ty
20d4c 70 65 2c 20 62 75 74 20 70 75 74 20 61 6e 20 65  pe, but put an e
20d4d 72 72 6f 72 20 69 6e 20 74 68 65 20 70 50 61 72  rror in the pPar
20d4e 73 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  se structure..*/
20d4f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20d50 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54  int sqlite3JoinT
20d51 79 70 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ype(Parse *pPars
20d52 65 2c 20 54 6f 6b 65 6e 20 2a 70 41 2c 20 54 6f  e, Token *pA, To
20d53 6b 65 6e 20 2a 70 42 2c 20 54 6f 6b 65 6e 20 2a  ken *pB, Token *
20d54 70 43 29 7b 0a 20 20 69 6e 74 20 6a 6f 69 6e 74  pC){.  int joint
20d55 79 70 65 20 3d 20 30 3b 0a 20 20 54 6f 6b 65 6e  ype = 0;.  Token
20d56 20 2a 61 70 41 6c 6c 5b 33 5d 3b 0a 20 20 54 6f   *apAll[3];.  To
20d57 6b 65 6e 20 2a 70 3b 0a 20 20 73 74 61 74 69 63  ken *p;.  static
20d58 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 7b 0a   const struct {.
20d59 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 7a      const char z
20d5a 4b 65 79 77 6f 72 64 5b 38 5d 3b 0a 20 20 20 20  Keyword[8];.    
20d5b 75 38 20 6e 43 68 61 72 3b 0a 20 20 20 20 75 38  u8 nChar;.    u8
20d5c 20 63 6f 64 65 3b 0a 20 20 7d 20 6b 65 79 77 6f   code;.  } keywo
20d5d 72 64 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20  rds[] = {.    { 
20d5e 22 6e 61 74 75 72 61 6c 22 2c 20 37 2c 20 4a 54  "natural", 7, JT
20d5f 5f 4e 41 54 55 52 41 4c 20 7d 2c 0a 20 20 20 20  _NATURAL },.    
20d60 7b 20 22 6c 65 66 74 22 2c 20 20 20 20 34 2c 20  { "left",    4, 
20d61 4a 54 5f 4c 45 46 54 7c 4a 54 5f 4f 55 54 45 52  JT_LEFT|JT_OUTER
20d62 20 7d 2c 0a 20 20 20 20 7b 20 22 72 69 67 68 74   },.    { "right
20d63 22 2c 20 20 20 35 2c 20 4a 54 5f 52 49 47 48 54  ",   5, JT_RIGHT
20d64 7c 4a 54 5f 4f 55 54 45 52 20 7d 2c 0a 20 20 20  |JT_OUTER },.   
20d65 20 7b 20 22 66 75 6c 6c 22 2c 20 20 20 20 34 2c   { "full",    4,
20d66 20 4a 54 5f 4c 45 46 54 7c 4a 54 5f 52 49 47 48   JT_LEFT|JT_RIGH
20d67 54 7c 4a 54 5f 4f 55 54 45 52 20 7d 2c 0a 20 20  T|JT_OUTER },.  
20d68 20 20 7b 20 22 6f 75 74 65 72 22 2c 20 20 20 35    { "outer",   5
20d69 2c 20 4a 54 5f 4f 55 54 45 52 20 7d 2c 0a 20 20  , JT_OUTER },.  
20d6a 20 20 7b 20 22 69 6e 6e 65 72 22 2c 20 20 20 35    { "inner",   5
20d6b 2c 20 4a 54 5f 49 4e 4e 45 52 20 7d 2c 0a 20 20  , JT_INNER },.  
20d6c 20 20 7b 20 22 63 72 6f 73 73 22 2c 20 20 20 35    { "cross",   5
20d6d 2c 20 4a 54 5f 49 4e 4e 45 52 7c 4a 54 5f 43 52  , JT_INNER|JT_CR
20d6e 4f 53 53 20 7d 2c 0a 20 20 7d 3b 0a 20 20 69 6e  OSS },.  };.  in
20d6f 74 20 69 2c 20 6a 3b 0a 20 20 61 70 41 6c 6c 5b  t i, j;.  apAll[
20d70 30 5d 20 3d 20 70 41 3b 0a 20 20 61 70 41 6c 6c  0] = pA;.  apAll
20d71 5b 31 5d 20 3d 20 70 42 3b 0a 20 20 61 70 41 6c  [1] = pB;.  apAl
20d72 6c 5b 32 5d 20 3d 20 70 43 3b 0a 20 20 66 6f 72  l[2] = pC;.  for
20d73 28 69 3d 30 3b 20 69 3c 33 20 26 26 20 61 70 41  (i=0; i<3 && apA
20d74 6c 6c 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20  ll[i]; i++){.   
20d75 20 70 20 3d 20 61 70 41 6c 6c 5b 69 5d 3b 0a 20   p = apAll[i];. 
20d76 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 73 69     for(j=0; j<si
20d77 7a 65 6f 66 28 6b 65 79 77 6f 72 64 73 29 2f 73  zeof(keywords)/s
20d78 69 7a 65 6f 66 28 6b 65 79 77 6f 72 64 73 5b 30  izeof(keywords[0
20d79 5d 29 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  ]); j++){.      
20d7a 69 66 28 20 70 2d 3e 6e 3d 3d 6b 65 79 77 6f 72  if( p->n==keywor
20d7b 64 73 5b 6a 5d 2e 6e 43 68 61 72 20 0a 20 20 20  ds[j].nChar .   
20d7c 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65         && sqlite
20d7d 33 53 74 72 4e 49 43 6d 70 28 28 63 68 61 72 2a  3StrNICmp((char*
20d7e 29 70 2d 3e 7a 2c 20 6b 65 79 77 6f 72 64 73 5b  )p->z, keywords[
20d7f 6a 5d 2e 7a 4b 65 79 77 6f 72 64 2c 20 70 2d 3e  j].zKeyword, p->
20d80 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  n)==0 ){.       
20d81 20 6a 6f 69 6e 74 79 70 65 20 7c 3d 20 6b 65 79   jointype |= key
20d82 77 6f 72 64 73 5b 6a 5d 2e 63 6f 64 65 3b 0a 20  words[j].code;. 
20d83 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
20d84 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
20d85 69 66 28 20 6a 3e 3d 73 69 7a 65 6f 66 28 6b 65  if( j>=sizeof(ke
20d86 79 77 6f 72 64 73 29 2f 73 69 7a 65 6f 66 28 6b  ywords)/sizeof(k
20d87 65 79 77 6f 72 64 73 5b 30 5d 29 20 29 7b 0a 20  eywords[0]) ){. 
20d88 20 20 20 20 20 6a 6f 69 6e 74 79 70 65 20 7c 3d       jointype |=
20d89 20 4a 54 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20   JT_ERROR;.     
20d8a 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
20d8b 7d 0a 20 20 69 66 28 0a 20 20 20 20 20 28 6a 6f  }.  if(.     (jo
20d8c 69 6e 74 79 70 65 20 26 20 28 4a 54 5f 49 4e 4e  intype & (JT_INN
20d8d 45 52 7c 4a 54 5f 4f 55 54 45 52 29 29 3d 3d 28  ER|JT_OUTER))==(
20d8e 4a 54 5f 49 4e 4e 45 52 7c 4a 54 5f 4f 55 54 45  JT_INNER|JT_OUTE
20d8f 52 29 20 7c 7c 0a 20 20 20 20 20 28 6a 6f 69 6e  R) ||.     (join
20d90 74 79 70 65 20 26 20 4a 54 5f 45 52 52 4f 52 29  type & JT_ERROR)
20d91 21 3d 30 0a 20 20 29 7b 0a 20 20 20 20 63 6f 6e  !=0.  ){.    con
20d92 73 74 20 63 68 61 72 20 2a 7a 53 70 31 20 3d 20  st char *zSp1 = 
20d93 22 20 22 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  " ";.    const c
20d94 68 61 72 20 2a 7a 53 70 32 20 3d 20 22 20 22 3b  har *zSp2 = " ";
20d95 0a 20 20 20 20 69 66 28 20 70 42 3d 3d 30 20 29  .    if( pB==0 )
20d96 7b 20 7a 53 70 31 2b 2b 3b 20 7d 0a 20 20 20 20  { zSp1++; }.    
20d97 69 66 28 20 70 43 3d 3d 30 20 29 7b 20 7a 53 70  if( pC==0 ){ zSp
20d98 32 2b 2b 3b 20 7d 0a 20 20 20 20 73 71 6c 69 74  2++; }.    sqlit
20d99 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
20d9a 65 2c 20 22 75 6e 6b 6e 6f 77 6e 20 6f 72 20 75  e, "unknown or u
20d9b 6e 73 75 70 70 6f 72 74 65 64 20 6a 6f 69 6e 20  nsupported join 
20d9c 74 79 70 65 3a 20 22 0a 20 20 20 20 20 20 20 22  type: ".       "
20d9d 25 54 25 73 25 54 25 73 25 54 22 2c 20 70 41 2c  %T%s%T%s%T", pA,
20d9e 20 7a 53 70 31 2c 20 70 42 2c 20 7a 53 70 32 2c   zSp1, pB, zSp2,
20d9f 20 70 43 29 3b 0a 20 20 20 20 6a 6f 69 6e 74 79   pC);.    jointy
20da0 70 65 20 3d 20 4a 54 5f 49 4e 4e 45 52 3b 0a 20  pe = JT_INNER;. 
20da1 20 7d 65 6c 73 65 20 69 66 28 20 6a 6f 69 6e 74   }else if( joint
20da2 79 70 65 20 26 20 4a 54 5f 52 49 47 48 54 20 29  ype & JT_RIGHT )
20da3 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
20da4 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20  orMsg(pParse, . 
20da5 20 20 20 20 20 22 52 49 47 48 54 20 61 6e 64 20       "RIGHT and 
20da6 46 55 4c 4c 20 4f 55 54 45 52 20 4a 4f 49 4e 73  FULL OUTER JOINs
20da7 20 61 72 65 20 6e 6f 74 20 63 75 72 72 65 6e 74   are not current
20da8 6c 79 20 73 75 70 70 6f 72 74 65 64 22 29 3b 0a  ly supported");.
20da9 20 20 20 20 6a 6f 69 6e 74 79 70 65 20 3d 20 4a      jointype = J
20daa 54 5f 49 4e 4e 45 52 3b 0a 20 20 7d 0a 20 20 72  T_INNER;.  }.  r
20dab 65 74 75 72 6e 20 6a 6f 69 6e 74 79 70 65 3b 0a  eturn jointype;.
20dac 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
20dad 74 68 65 20 69 6e 64 65 78 20 6f 66 20 61 20 63  the index of a c
20dae 6f 6c 75 6d 6e 20 69 6e 20 61 20 74 61 62 6c 65  olumn in a table
20daf 2e 20 20 52 65 74 75 72 6e 20 2d 31 20 69 66 20  .  Return -1 if 
20db0 74 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73  the column.** is
20db1 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 65 64 20 69   not contained i
20db2 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a  n the table..*/.
20db3 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6c 75 6d  static int colum
20db4 6e 49 6e 64 65 78 28 54 61 62 6c 65 20 2a 70 54  nIndex(Table *pT
20db5 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ab, const char *
20db6 7a 43 6f 6c 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  zCol){.  int i;.
20db7 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61    for(i=0; i<pTa
20db8 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20  b->nCol; i++){. 
20db9 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
20dba 72 49 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f 6c  rICmp(pTab->aCol
20dbb 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29  [i].zName, zCol)
20dbc 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 69 3b 0a  ==0 ) return i;.
20dbd 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31 3b    }.  return -1;
20dbe 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
20dbf 65 20 76 61 6c 75 65 20 6f 66 20 61 20 74 6f 6b  e value of a tok
20dc0 65 6e 20 74 6f 20 61 20 27 5c 30 30 30 27 2d 74  en to a '\000'-t
20dc1 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67  erminated string
20dc2 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
20dc3 20 73 65 74 54 6f 6b 65 6e 28 54 6f 6b 65 6e 20   setToken(Token 
20dc4 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  *p, const char *
20dc5 7a 29 7b 0a 20 20 70 2d 3e 7a 20 3d 20 28 75 38  z){.  p->z = (u8
20dc6 2a 29 7a 3b 0a 20 20 70 2d 3e 6e 20 3d 20 7a 20  *)z;.  p->n = z 
20dc7 3f 20 73 74 72 6c 65 6e 28 7a 29 20 3a 20 30 3b  ? strlen(z) : 0;
20dc8 0a 20 20 70 2d 3e 64 79 6e 20 3d 20 30 3b 0a 7d  .  p->dyn = 0;.}
20dc9 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
20dca 74 6f 6b 65 6e 20 74 6f 20 74 68 65 20 64 6f 75  token to the dou
20dcb 62 6c 65 2d 71 75 6f 74 65 64 20 61 6e 64 20 65  ble-quoted and e
20dcc 73 63 61 70 65 64 20 76 65 72 73 69 6f 6e 20 6f  scaped version o
20dcd 66 20 74 68 65 20 73 74 72 69 6e 67 20 70 6f 69  f the string poi
20dce 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 7a 2e  nted.** to by z.
20dcf 20 46 6f 72 20 65 78 61 6d 70 6c 65 3b 0a 2a 2a   For example;.**
20dd0 0a 2a 2a 20 20 20 20 7b 61 22 62 63 7d 20 20 2d  .**    {a"bc}  -
20dd1 3e 20 20 7b 22 61 22 22 62 63 22 7d 0a 2a 2f 0a  >  {"a""bc"}.*/.
20dd2 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 51  static void setQ
20dd3 75 6f 74 65 64 54 6f 6b 65 6e 28 50 61 72 73 65  uotedToken(Parse
20dd4 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20   *pParse, Token 
20dd5 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  *p, const char *
20dd6 7a 29 7b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20  z){..  /* Check 
20dd7 69 66 20 74 68 65 20 73 74 72 69 6e 67 20 63 6f  if the string co
20dd8 6e 74 61 69 6e 73 20 61 6e 79 20 22 20 63 68 61  ntains any " cha
20dd9 72 61 63 74 65 72 73 2e 20 49 66 20 69 74 20 64  racters. If it d
20dda 6f 65 73 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 74  oes, then.  ** t
20ddb 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c  his function wil
20ddc 6c 20 6d 61 6c 6c 6f 63 20 73 70 61 63 65 20 74  l malloc space t
20ddd 6f 20 63 72 65 61 74 65 20 61 20 71 75 6f 74 65  o create a quote
20dde 64 20 76 65 72 73 69 6f 6e 20 6f 66 0a 20 20 2a  d version of.  *
20ddf 2a 20 74 68 65 20 73 74 72 69 6e 67 20 69 6e 2e  * the string in.
20de0 20 4f 74 68 65 72 77 69 73 65 2c 20 73 61 76 65   Otherwise, save
20de1 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74   a call to sqlit
20de2 65 33 4d 50 72 69 6e 74 66 28 29 20 62 79 0a 20  e3MPrintf() by. 
20de3 20 2a 2a 20 6a 75 73 74 20 63 6f 70 79 69 6e 67   ** just copying
20de4 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20   the pointer to 
20de5 74 68 65 20 73 74 72 69 6e 67 2e 0a 20 20 2a 2f  the string..  */
20de6 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
20de7 32 20 3d 20 7a 3b 0a 20 20 77 68 69 6c 65 28 20  2 = z;.  while( 
20de8 2a 7a 32 20 29 7b 0a 20 20 20 20 69 66 28 20 2a  *z2 ){.    if( *
20de9 7a 32 3d 3d 27 22 27 20 29 20 62 72 65 61 6b 3b  z2=='"' ) break;
20dea 0a 20 20 20 20 7a 32 2b 2b 3b 0a 20 20 7d 0a 0a  .    z2++;.  }..
20deb 20 20 69 66 28 20 2a 7a 32 20 29 7b 0a 20 20 20    if( *z2 ){.   
20dec 20 2f 2a 20 53 74 72 69 6e 67 20 63 6f 6e 74 61   /* String conta
20ded 69 6e 73 20 22 20 63 68 61 72 61 63 74 65 72 73  ins " characters
20dee 20 2d 20 63 6f 70 79 20 61 6e 64 20 71 75 6f 74   - copy and quot
20def 65 20 74 68 65 20 73 74 72 69 6e 67 2e 20 2a 2f  e the string. */
20df0 0a 20 20 20 20 70 2d 3e 7a 20 3d 20 28 75 38 20  .    p->z = (u8 
20df1 2a 29 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  *)sqlite3MPrintf
20df2 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 22 5c 22  (pParse->db, "\"
20df3 25 77 5c 22 22 2c 20 7a 29 3b 0a 20 20 20 20 69  %w\"", z);.    i
20df4 66 28 20 70 2d 3e 7a 20 29 7b 0a 20 20 20 20 20  f( p->z ){.     
20df5 20 70 2d 3e 6e 20 3d 20 73 74 72 6c 65 6e 28 28   p->n = strlen((
20df6 63 68 61 72 20 2a 29 70 2d 3e 7a 29 3b 0a 20 20  char *)p->z);.  
20df7 20 20 20 20 70 2d 3e 64 79 6e 20 3d 20 31 3b 0a      p->dyn = 1;.
20df8 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
20df9 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 63 6f 6e     /* String con
20dfa 74 61 69 6e 73 20 6e 6f 20 22 20 63 68 61 72 61  tains no " chara
20dfb 63 74 65 72 73 20 2d 20 63 6f 70 79 20 74 68 65  cters - copy the
20dfc 20 70 6f 69 6e 74 65 72 2e 20 2a 2f 0a 20 20 20   pointer. */.   
20dfd 20 70 2d 3e 7a 20 3d 20 28 75 38 2a 29 7a 3b 0a   p->z = (u8*)z;.
20dfe 20 20 20 20 70 2d 3e 6e 20 3d 20 28 7a 32 20 2d      p->n = (z2 -
20dff 20 7a 29 3b 0a 20 20 20 20 70 2d 3e 64 79 6e 20   z);.    p->dyn 
20e00 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  = 0;.  }.}../*.*
20e01 2a 20 43 72 65 61 74 65 20 61 6e 20 65 78 70 72  * Create an expr
20e02 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20  ession node for 
20e03 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20 77 69  an identifier wi
20e04 74 68 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 7a  th the name of z
20e05 4e 61 6d 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  Name.*/.SQLITE_P
20e06 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c  RIVATE Expr *sql
20e07 69 74 65 33 43 72 65 61 74 65 49 64 45 78 70 72  ite3CreateIdExpr
20e08 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
20e09 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
20e0a 65 29 7b 0a 20 20 54 6f 6b 65 6e 20 64 75 6d 6d  e){.  Token dumm
20e0b 79 3b 0a 20 20 73 65 74 54 6f 6b 65 6e 28 26 64  y;.  setToken(&d
20e0c 75 6d 6d 79 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  ummy, zName);.  
20e0d 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 45  return sqlite3PE
20e0e 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  xpr(pParse, TK_I
20e0f 44 2c 20 30 2c 20 30 2c 20 26 64 75 6d 6d 79 29  D, 0, 0, &dummy)
20e10 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61  ;.}../*.** Add a
20e11 20 74 65 72 6d 20 74 6f 20 74 68 65 20 57 48 45   term to the WHE
20e12 52 45 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  RE expression in
20e13 20 2a 70 70 45 78 70 72 20 74 68 61 74 20 72 65   *ppExpr that re
20e14 71 75 69 72 65 73 20 74 68 65 0a 2a 2a 20 7a 43  quires the.** zC
20e15 6f 6c 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20  ol column to be 
20e16 65 71 75 61 6c 20 69 6e 20 74 68 65 20 74 77 6f  equal in the two
20e17 20 74 61 62 6c 65 73 20 70 54 61 62 31 20 61 6e   tables pTab1 an
20e18 64 20 70 54 61 62 32 2e 0a 2a 2f 0a 73 74 61 74  d pTab2..*/.stat
20e19 69 63 20 76 6f 69 64 20 61 64 64 57 68 65 72 65  ic void addWhere
20e1a 54 65 72 6d 28 0a 20 20 50 61 72 73 65 20 2a 70  Term(.  Parse *p
20e1b 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
20e1c 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
20e1d 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ext */.  const c
20e1e 68 61 72 20 2a 7a 43 6f 6c 2c 20 20 20 20 20 20  har *zCol,      
20e1f 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
20e20 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 6f 6e   column */.  con
20e21 73 74 20 54 61 62 6c 65 20 2a 70 54 61 62 31 2c  st Table *pTab1,
20e22 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 74        /* First t
20e23 61 62 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  able */.  const 
20e24 63 68 61 72 20 2a 7a 41 6c 69 61 73 31 2c 20 20  char *zAlias1,  
20e25 20 20 20 2f 2a 20 41 6c 69 61 73 20 66 6f 72 20     /* Alias for 
20e26 66 69 72 73 74 20 74 61 62 6c 65 2e 20 20 4d 61  first table.  Ma
20e27 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63  y be NULL */.  c
20e28 6f 6e 73 74 20 54 61 62 6c 65 20 2a 70 54 61 62  onst Table *pTab
20e29 32 2c 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e  2,      /* Secon
20e2a 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 6f 6e  d table */.  con
20e2b 73 74 20 63 68 61 72 20 2a 7a 41 6c 69 61 73 32  st char *zAlias2
20e2c 2c 20 20 20 20 20 2f 2a 20 41 6c 69 61 73 20 66  ,     /* Alias f
20e2d 6f 72 20 73 65 63 6f 6e 64 20 74 61 62 6c 65 2e  or second table.
20e2e 20 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f    May be NULL */
20e2f 0a 20 20 69 6e 74 20 69 52 69 67 68 74 4a 6f 69  .  int iRightJoi
20e30 6e 54 61 62 6c 65 2c 20 20 20 20 20 2f 2a 20 56  nTable,     /* V
20e31 44 42 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74  DBE cursor for t
20e32 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 2a  he right table *
20e33 2f 0a 20 20 45 78 70 72 20 2a 2a 70 70 45 78 70  /.  Expr **ppExp
20e34 72 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  r,           /* 
20e35 41 64 64 20 74 68 65 20 65 71 75 61 6c 69 74 79  Add the equality
20e36 20 74 65 72 6d 20 74 6f 20 74 68 69 73 20 65 78   term to this ex
20e37 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e  pression */.  in
20e38 74 20 69 73 4f 75 74 65 72 4a 6f 69 6e 20 20 20  t isOuterJoin   
20e39 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
20e3a 66 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61  f dealing with a
20e3b 6e 20 4f 55 54 45 52 20 6a 6f 69 6e 20 2a 2f 0a  n OUTER join */.
20e3c 29 7b 0a 20 20 45 78 70 72 20 2a 70 45 31 61 2c  ){.  Expr *pE1a,
20e3d 20 2a 70 45 31 62 2c 20 2a 70 45 31 63 3b 0a 20   *pE1b, *pE1c;. 
20e3e 20 45 78 70 72 20 2a 70 45 32 61 2c 20 2a 70 45   Expr *pE2a, *pE
20e3f 32 62 2c 20 2a 70 45 32 63 3b 0a 20 20 45 78 70  2b, *pE2c;.  Exp
20e40 72 20 2a 70 45 3b 0a 0a 20 20 70 45 31 61 20 3d  r *pE;..  pE1a =
20e41 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 64   sqlite3CreateId
20e42 45 78 70 72 28 70 50 61 72 73 65 2c 20 7a 43 6f  Expr(pParse, zCo
20e43 6c 29 3b 0a 20 20 70 45 32 61 20 3d 20 73 71 6c  l);.  pE2a = sql
20e44 69 74 65 33 43 72 65 61 74 65 49 64 45 78 70 72  ite3CreateIdExpr
20e45 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 29 3b 0a  (pParse, zCol);.
20e46 20 20 69 66 28 20 7a 41 6c 69 61 73 31 3d 3d 30    if( zAlias1==0
20e47 20 29 7b 0a 20 20 20 20 7a 41 6c 69 61 73 31 20   ){.    zAlias1 
20e48 3d 20 70 54 61 62 31 2d 3e 7a 4e 61 6d 65 3b 0a  = pTab1->zName;.
20e49 20 20 7d 0a 20 20 70 45 31 62 20 3d 20 73 71 6c    }.  pE1b = sql
20e4a 69 74 65 33 43 72 65 61 74 65 49 64 45 78 70 72  ite3CreateIdExpr
20e4b 28 70 50 61 72 73 65 2c 20 7a 41 6c 69 61 73 31  (pParse, zAlias1
20e4c 29 3b 0a 20 20 69 66 28 20 7a 41 6c 69 61 73 32  );.  if( zAlias2
20e4d 3d 3d 30 20 29 7b 0a 20 20 20 20 7a 41 6c 69 61  ==0 ){.    zAlia
20e4e 73 32 20 3d 20 70 54 61 62 32 2d 3e 7a 4e 61 6d  s2 = pTab2->zNam
20e4f 65 3b 0a 20 20 7d 0a 20 20 70 45 32 62 20 3d 20  e;.  }.  pE2b = 
20e50 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 64 45  sqlite3CreateIdE
20e51 78 70 72 28 70 50 61 72 73 65 2c 20 7a 41 6c 69  xpr(pParse, zAli
20e52 61 73 32 29 3b 0a 20 20 70 45 31 63 20 3d 20 73  as2);.  pE1c = s
20e53 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
20e54 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 70 45 31 62  se, TK_DOT, pE1b
20e55 2c 20 70 45 31 61 2c 20 30 29 3b 0a 20 20 70 45  , pE1a, 0);.  pE
20e56 32 63 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  2c = sqlite3PExp
20e57 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54  r(pParse, TK_DOT
20e58 2c 20 70 45 32 62 2c 20 70 45 32 61 2c 20 30 29  , pE2b, pE2a, 0)
20e59 3b 0a 20 20 70 45 20 3d 20 73 71 6c 69 74 65 33  ;.  pE = sqlite3
20e5a 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
20e5b 5f 45 51 2c 20 70 45 31 63 2c 20 70 45 32 63 2c  _EQ, pE1c, pE2c,
20e5c 20 30 29 3b 0a 20 20 69 66 28 20 70 45 20 26 26   0);.  if( pE &&
20e5d 20 69 73 4f 75 74 65 72 4a 6f 69 6e 20 29 7b 0a   isOuterJoin ){.
20e5e 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65      ExprSetPrope
20e5f 72 74 79 28 70 45 2c 20 45 50 5f 46 72 6f 6d 4a  rty(pE, EP_FromJ
20e60 6f 69 6e 29 3b 0a 20 20 20 20 70 45 2d 3e 69 52  oin);.    pE->iR
20e61 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 3d 20  ightJoinTable = 
20e62 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 3b  iRightJoinTable;
20e63 0a 20 20 7d 0a 20 20 2a 70 70 45 78 70 72 20 3d  .  }.  *ppExpr =
20e64 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28   sqlite3ExprAnd(
20e65 70 50 61 72 73 65 2d 3e 64 62 2c 2a 70 70 45 78  pParse->db,*ppEx
20e66 70 72 2c 20 70 45 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  pr, pE);.}../*.*
20e67 2a 20 53 65 74 20 74 68 65 20 45 50 5f 46 72 6f  * Set the EP_Fro
20e68 6d 4a 6f 69 6e 20 70 72 6f 70 65 72 74 79 20 6f  mJoin property o
20e69 6e 20 61 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74  n all terms of t
20e6a 68 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 73  he given express
20e6b 69 6f 6e 2e 0a 2a 2a 20 41 6e 64 20 73 65 74 20  ion..** And set 
20e6c 74 68 65 20 45 78 70 72 2e 69 52 69 67 68 74 4a  the Expr.iRightJ
20e6d 6f 69 6e 54 61 62 6c 65 20 74 6f 20 69 54 61 62  oinTable to iTab
20e6e 6c 65 20 66 6f 72 20 65 76 65 72 79 20 74 65 72  le for every ter
20e6f 6d 20 69 6e 20 74 68 65 0a 2a 2a 20 65 78 70 72  m in the.** expr
20e70 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  ession..**.** Th
20e71 65 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 20 70 72  e EP_FromJoin pr
20e72 6f 70 65 72 74 79 20 69 73 20 75 73 65 64 20 6f  operty is used o
20e73 6e 20 74 65 72 6d 73 20 6f 66 20 61 6e 20 65 78  n terms of an ex
20e74 70 72 65 73 73 69 6f 6e 20 74 6f 20 74 65 6c 6c  pression to tell
20e75 0a 2a 2a 20 74 68 65 20 4c 45 46 54 20 4f 55 54  .** the LEFT OUT
20e76 45 52 20 4a 4f 49 4e 20 70 72 6f 63 65 73 73 69  ER JOIN processi
20e77 6e 67 20 6c 6f 67 69 63 20 74 68 61 74 20 74 68  ng logic that th
20e78 69 73 20 74 65 72 6d 20 69 73 20 70 61 72 74 20  is term is part 
20e79 6f 66 20 74 68 65 0a 2a 2a 20 6a 6f 69 6e 20 72  of the.** join r
20e7a 65 73 74 72 69 63 74 69 6f 6e 20 73 70 65 63 69  estriction speci
20e7b 66 69 65 64 20 69 6e 20 74 68 65 20 4f 4e 20 6f  fied in the ON o
20e7c 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 61  r USING clause a
20e7d 6e 64 20 6e 6f 74 20 61 20 70 61 72 74 0a 2a 2a  nd not a part.**
20e7e 20 6f 66 20 74 68 65 20 6d 6f 72 65 20 67 65 6e   of the more gen
20e7f 65 72 61 6c 20 57 48 45 52 45 20 63 6c 61 75 73  eral WHERE claus
20e80 65 2e 20 20 54 68 65 73 65 20 74 65 72 6d 73 20  e.  These terms 
20e81 61 72 65 20 6d 6f 76 65 64 20 6f 76 65 72 20 74  are moved over t
20e82 6f 20 74 68 65 0a 2a 2a 20 57 48 45 52 45 20 63  o the.** WHERE c
20e83 6c 61 75 73 65 20 64 75 72 69 6e 67 20 6a 6f 69  lause during joi
20e84 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 62 75 74  n processing but
20e85 20 77 65 20 6e 65 65 64 20 74 6f 20 72 65 6d 65   we need to reme
20e86 6d 62 65 72 20 74 68 61 74 20 74 68 65 79 0a 2a  mber that they.*
20e87 2a 20 6f 72 69 67 69 6e 61 74 65 64 20 69 6e 20  * originated in 
20e88 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20  the ON or USING 
20e89 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  clause..**.** Th
20e8a 65 20 45 78 70 72 2e 69 52 69 67 68 74 4a 6f 69  e Expr.iRightJoi
20e8b 6e 54 61 62 6c 65 20 74 65 6c 6c 73 20 74 68 65  nTable tells the
20e8c 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 70 72   WHERE clause pr
20e8d 6f 63 65 73 73 69 6e 67 20 74 68 61 74 20 74 68  ocessing that th
20e8e 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20  e.** expression 
20e8f 64 65 70 65 6e 64 73 20 6f 6e 20 74 61 62 6c 65  depends on table
20e90 20 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65   iRightJoinTable
20e91 20 65 76 65 6e 20 69 66 20 74 68 61 74 20 74 61   even if that ta
20e92 62 6c 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 65 78  ble is not.** ex
20e93 70 6c 69 63 69 74 6c 79 20 6d 65 6e 74 69 6f 6e  plicitly mention
20e94 65 64 20 69 6e 20 74 68 65 20 65 78 70 72 65 73  ed in the expres
20e95 73 69 6f 6e 2e 20 20 54 68 61 74 20 69 6e 66 6f  sion.  That info
20e96 72 6d 61 74 69 6f 6e 20 69 73 20 6e 65 65 64 65  rmation is neede
20e97 64 0a 2a 2a 20 66 6f 72 20 63 61 73 65 73 20 6c  d.** for cases l
20e98 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
20e99 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d     SELECT * FROM
20e9a 20 74 31 20 4c 45 46 54 20 4a 4f 49 4e 20 74 32   t1 LEFT JOIN t2
20e9b 20 4f 4e 20 74 31 2e 61 3d 74 32 2e 62 20 41 4e   ON t1.a=t2.b AN
20e9c 44 20 74 31 2e 78 3d 35 0a 2a 2a 0a 2a 2a 20 54  D t1.x=5.**.** T
20e9d 68 65 20 77 68 65 72 65 20 63 6c 61 75 73 65 20  he where clause 
20e9e 6e 65 65 64 73 20 74 6f 20 64 65 66 65 72 20 74  needs to defer t
20e9f 68 65 20 68 61 6e 64 6c 69 6e 67 20 6f 66 20 74  he handling of t
20ea0 68 65 20 74 31 2e 78 3d 35 0a 2a 2a 20 74 65 72  he t1.x=5.** ter
20ea1 6d 20 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68  m until after th
20ea2 65 20 74 32 20 6c 6f 6f 70 20 6f 66 20 74 68 65  e t2 loop of the
20ea3 20 6a 6f 69 6e 2e 20 20 49 6e 20 74 68 61 74 20   join.  In that 
20ea4 77 61 79 2c 20 61 0a 2a 2a 20 4e 55 4c 4c 20 74  way, a.** NULL t
20ea5 32 20 72 6f 77 20 77 69 6c 6c 20 62 65 20 69 6e  2 row will be in
20ea6 73 65 72 74 65 64 20 77 68 65 6e 65 76 65 72 20  serted whenever 
20ea7 74 31 2e 78 21 3d 35 2e 20 20 49 66 20 77 65 20  t1.x!=5.  If we 
20ea8 64 6f 20 6e 6f 74 0a 2a 2a 20 64 65 66 65 72 20  do not.** defer 
20ea9 74 68 65 20 68 61 6e 64 6c 69 6e 67 20 6f 66 20  the handling of 
20eaa 74 31 2e 78 3d 35 2c 20 69 74 20 77 69 6c 6c 20  t1.x=5, it will 
20eab 62 65 20 70 72 6f 63 65 73 73 65 64 20 69 6d 6d  be processed imm
20eac 65 64 69 61 74 65 6c 79 0a 2a 2a 20 61 66 74 65  ediately.** afte
20ead 72 20 74 68 65 20 74 31 20 6c 6f 6f 70 20 61 6e  r the t1 loop an
20eae 64 20 72 6f 77 73 20 77 69 74 68 20 74 31 2e 78  d rows with t1.x
20eaf 21 3d 35 20 77 69 6c 6c 20 6e 65 76 65 72 20 61  !=5 will never a
20eb0 70 70 65 61 72 20 69 6e 0a 2a 2a 20 74 68 65 20  ppear in.** the 
20eb1 6f 75 74 70 75 74 2c 20 77 68 69 63 68 20 69 73  output, which is
20eb2 20 69 6e 63 6f 72 72 65 63 74 2e 0a 2a 2f 0a 73   incorrect..*/.s
20eb3 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 4a 6f  tatic void setJo
20eb4 69 6e 45 78 70 72 28 45 78 70 72 20 2a 70 2c 20  inExpr(Expr *p, 
20eb5 69 6e 74 20 69 54 61 62 6c 65 29 7b 0a 20 20 77  int iTable){.  w
20eb6 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 45  hile( p ){.    E
20eb7 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 70  xprSetProperty(p
20eb8 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 3b 0a  , EP_FromJoin);.
20eb9 20 20 20 20 70 2d 3e 69 52 69 67 68 74 4a 6f 69      p->iRightJoi
20eba 6e 54 61 62 6c 65 20 3d 20 69 54 61 62 6c 65 3b  nTable = iTable;
20ebb 0a 20 20 20 20 73 65 74 4a 6f 69 6e 45 78 70 72  .    setJoinExpr
20ebc 28 70 2d 3e 70 4c 65 66 74 2c 20 69 54 61 62 6c  (p->pLeft, iTabl
20ebd 65 29 3b 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70  e);.    p = p->p
20ebe 52 69 67 68 74 3b 0a 20 20 7d 20 0a 7d 0a 0a 2f  Right;.  } .}../
20ebf 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
20ec0 65 20 70 72 6f 63 65 73 73 65 73 20 74 68 65 20  e processes the 
20ec1 6a 6f 69 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  join information
20ec2 20 66 6f 72 20 61 20 53 45 4c 45 43 54 20 73 74   for a SELECT st
20ec3 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 4f 4e 20 61  atement..** ON a
20ec4 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73  nd USING clauses
20ec5 20 61 72 65 20 63 6f 6e 76 65 72 74 65 64 20 69   are converted i
20ec6 6e 74 6f 20 65 78 74 72 61 20 74 65 72 6d 73 20  nto extra terms 
20ec7 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  of the WHERE cla
20ec8 75 73 65 2e 0a 2a 2a 20 4e 41 54 55 52 41 4c 20  use..** NATURAL 
20ec9 6a 6f 69 6e 73 20 61 6c 73 6f 20 63 72 65 61 74  joins also creat
20eca 65 20 65 78 74 72 61 20 57 48 45 52 45 20 63 6c  e extra WHERE cl
20ecb 61 75 73 65 20 74 65 72 6d 73 2e 0a 2a 2a 0a 2a  ause terms..**.*
20ecc 2a 20 54 68 65 20 74 65 72 6d 73 20 6f 66 20 61  * The terms of a
20ecd 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 72 65   FROM clause are
20ece 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68   contained in th
20ecf 65 20 53 65 6c 65 63 74 2e 70 53 72 63 20 73 74  e Select.pSrc st
20ed0 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20  ructure..** The 
20ed1 6c 65 66 74 20 6d 6f 73 74 20 74 61 62 6c 65 20  left most table 
20ed2 69 73 20 74 68 65 20 66 69 72 73 74 20 65 6e 74  is the first ent
20ed3 72 79 20 69 6e 20 53 65 6c 65 63 74 2e 70 53 72  ry in Select.pSr
20ed4 63 2e 20 20 54 68 65 20 72 69 67 68 74 2d 6d 6f  c.  The right-mo
20ed5 73 74 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20 74  st.** table is t
20ed6 68 65 20 6c 61 73 74 20 65 6e 74 72 79 2e 20 20  he last entry.  
20ed7 54 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f  The join operato
20ed8 72 20 69 73 20 68 65 6c 64 20 69 6e 20 74 68 65  r is held in the
20ed9 20 65 6e 74 72 79 20 74 6f 0a 2a 2a 20 74 68 65   entry to.** the
20eda 20 6c 65 66 74 2e 20 20 54 68 75 73 20 65 6e 74   left.  Thus ent
20edb 72 79 20 30 20 63 6f 6e 74 61 69 6e 73 20 74 68  ry 0 contains th
20edc 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 20  e join operator 
20edd 66 6f 72 20 74 68 65 20 6a 6f 69 6e 20 62 65 74  for the join bet
20ede 77 65 65 6e 0a 2a 2a 20 65 6e 74 72 69 65 73 20  ween.** entries 
20edf 30 20 61 6e 64 20 31 2e 20 20 41 6e 79 20 4f 4e  0 and 1.  Any ON
20ee0 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65   or USING clause
20ee1 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
20ee2 68 20 74 68 65 20 6a 6f 69 6e 20 61 72 65 0a 2a  h the join are.*
20ee3 2a 20 61 6c 73 6f 20 61 74 74 61 63 68 65 64 20  * also attached 
20ee4 74 6f 20 74 68 65 20 6c 65 66 74 20 65 6e 74 72  to the left entr
20ee5 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  y..**.** This ro
20ee6 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68  utine returns th
20ee7 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f  e number of erro
20ee8 72 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a  rs encountered..
20ee9 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  */.static int sq
20eea 6c 69 74 65 50 72 6f 63 65 73 73 4a 6f 69 6e 28  liteProcessJoin(
20eeb 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53  Parse *pParse, S
20eec 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 53 72 63  elect *p){.  Src
20eed 4c 69 73 74 20 2a 70 53 72 63 3b 20 20 20 20 20  List *pSrc;     
20eee 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
20eef 41 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68  All tables in th
20ef0 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f  e FROM clause */
20ef1 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20  .  int i, j;    
20ef2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20ef3 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
20ef4 65 72 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ers */.  struct 
20ef5 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c  SrcList_item *pL
20ef6 65 66 74 3b 20 20 20 20 20 2f 2a 20 4c 65 66 74  eft;     /* Left
20ef7 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6a 6f 69   table being joi
20ef8 6e 65 64 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ned */.  struct 
20ef9 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 52  SrcList_item *pR
20efa 69 67 68 74 3b 20 20 20 20 2f 2a 20 52 69 67 68  ight;    /* Righ
20efb 74 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6a 6f  t table being jo
20efc 69 6e 65 64 20 2a 2f 0a 0a 20 20 70 53 72 63 20  ined */..  pSrc 
20efd 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 70 4c 65  = p->pSrc;.  pLe
20efe 66 74 20 3d 20 26 70 53 72 63 2d 3e 61 5b 30 5d  ft = &pSrc->a[0]
20eff 3b 0a 20 20 70 52 69 67 68 74 20 3d 20 26 70 4c  ;.  pRight = &pL
20f00 65 66 74 5b 31 5d 3b 0a 20 20 66 6f 72 28 69 3d  eft[1];.  for(i=
20f01 30 3b 20 69 3c 70 53 72 63 2d 3e 6e 53 72 63 2d  0; i<pSrc->nSrc-
20f02 31 3b 20 69 2b 2b 2c 20 70 52 69 67 68 74 2b 2b  1; i++, pRight++
20f03 2c 20 70 4c 65 66 74 2b 2b 29 7b 0a 20 20 20 20  , pLeft++){.    
20f04 54 61 62 6c 65 20 2a 70 4c 65 66 74 54 61 62 20  Table *pLeftTab 
20f05 3d 20 70 4c 65 66 74 2d 3e 70 54 61 62 3b 0a 20  = pLeft->pTab;. 
20f06 20 20 20 54 61 62 6c 65 20 2a 70 52 69 67 68 74     Table *pRight
20f07 54 61 62 20 3d 20 70 52 69 67 68 74 2d 3e 70 54  Tab = pRight->pT
20f08 61 62 3b 0a 20 20 20 20 69 6e 74 20 69 73 4f 75  ab;.    int isOu
20f09 74 65 72 3b 0a 0a 20 20 20 20 69 66 28 20 70 4c  ter;..    if( pL
20f0a 65 66 74 54 61 62 3d 3d 30 20 7c 7c 20 70 52 69  eftTab==0 || pRi
20f0b 67 68 74 54 61 62 3d 3d 30 20 29 20 63 6f 6e 74  ghtTab==0 ) cont
20f0c 69 6e 75 65 3b 0a 20 20 20 20 69 73 4f 75 74 65  inue;.    isOute
20f0d 72 20 3d 20 28 70 52 69 67 68 74 2d 3e 6a 6f 69  r = (pRight->joi
20f0e 6e 74 79 70 65 20 26 20 4a 54 5f 4f 55 54 45 52  ntype & JT_OUTER
20f0f 29 21 3d 30 3b 0a 0a 20 20 20 20 2f 2a 20 57 68  )!=0;..    /* Wh
20f10 65 6e 20 74 68 65 20 4e 41 54 55 52 41 4c 20 6b  en the NATURAL k
20f11 65 79 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e  eyword is presen
20f12 74 2c 20 61 64 64 20 57 48 45 52 45 20 63 6c 61  t, add WHERE cla
20f13 75 73 65 20 74 65 72 6d 73 20 66 6f 72 0a 20 20  use terms for.  
20f14 20 20 2a 2a 20 65 76 65 72 79 20 63 6f 6c 75 6d    ** every colum
20f15 6e 20 74 68 61 74 20 74 68 65 20 74 77 6f 20 74  n that the two t
20f16 61 62 6c 65 73 20 68 61 76 65 20 69 6e 20 63 6f  ables have in co
20f17 6d 6d 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  mmon..    */.   
20f18 20 69 66 28 20 70 52 69 67 68 74 2d 3e 6a 6f 69   if( pRight->joi
20f19 6e 74 79 70 65 20 26 20 4a 54 5f 4e 41 54 55 52  ntype & JT_NATUR
20f1a 41 4c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  AL ){.      if( 
20f1b 70 52 69 67 68 74 2d 3e 70 4f 6e 20 7c 7c 20 70  pRight->pOn || p
20f1c 52 69 67 68 74 2d 3e 70 55 73 69 6e 67 20 29 7b  Right->pUsing ){
20f1d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
20f1e 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
20f1f 20 22 61 20 4e 41 54 55 52 41 4c 20 6a 6f 69 6e   "a NATURAL join
20f20 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 22 0a   may not have ".
20f21 20 20 20 20 20 20 20 20 20 20 20 22 61 6e 20 4f             "an O
20f22 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73  N or USING claus
20f23 65 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  e", 0);.        
20f24 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20  return 1;.      
20f25 7d 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b  }.      for(j=0;
20f26 20 6a 3c 70 4c 65 66 74 54 61 62 2d 3e 6e 43 6f   j<pLeftTab->nCo
20f27 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  l; j++){.       
20f28 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70   char *zName = p
20f29 4c 65 66 74 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d  LeftTab->aCol[j]
20f2a 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20  .zName;.        
20f2b 69 66 28 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28  if( columnIndex(
20f2c 70 52 69 67 68 74 54 61 62 2c 20 7a 4e 61 6d 65  pRightTab, zName
20f2d 29 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )>=0 ){.        
20f2e 20 20 61 64 64 57 68 65 72 65 54 65 72 6d 28 70    addWhereTerm(p
20f2f 50 61 72 73 65 2c 20 7a 4e 61 6d 65 2c 20 70 4c  Parse, zName, pL
20f30 65 66 74 54 61 62 2c 20 70 4c 65 66 74 2d 3e 7a  eftTab, pLeft->z
20f31 41 6c 69 61 73 2c 20 0a 20 20 20 20 20 20 20 20  Alias, .        
20f32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20f33 20 20 20 20 20 20 70 52 69 67 68 74 54 61 62 2c        pRightTab,
20f34 20 70 52 69 67 68 74 2d 3e 7a 41 6c 69 61 73 2c   pRight->zAlias,
20f35 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
20f36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
20f37 52 69 67 68 74 2d 3e 69 43 75 72 73 6f 72 2c 20  Right->iCursor, 
20f38 26 70 2d 3e 70 57 68 65 72 65 2c 20 69 73 4f 75  &p->pWhere, isOu
20f39 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ter);.          
20f3a 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
20f3b 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a   }.    }..    /*
20f3c 20 44 69 73 61 6c 6c 6f 77 20 62 6f 74 68 20 4f   Disallow both O
20f3d 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75  N and USING clau
20f3e 73 65 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20  ses in the same 
20f3f 6a 6f 69 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  join.    */.    
20f40 69 66 28 20 70 52 69 67 68 74 2d 3e 70 4f 6e 20  if( pRight->pOn 
20f41 26 26 20 70 52 69 67 68 74 2d 3e 70 55 73 69 6e  && pRight->pUsin
20f42 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  g ){.      sqlit
20f43 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
20f44 65 2c 20 22 63 61 6e 6e 6f 74 20 68 61 76 65 20  e, "cannot have 
20f45 62 6f 74 68 20 4f 4e 20 61 6e 64 20 55 53 49 4e  both ON and USIN
20f46 47 20 22 0a 20 20 20 20 20 20 20 20 22 63 6c 61  G ".        "cla
20f47 75 73 65 73 20 69 6e 20 74 68 65 20 73 61 6d 65  uses in the same
20f48 20 6a 6f 69 6e 22 29 3b 0a 20 20 20 20 20 20 72   join");.      r
20f49 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 0a  eturn 1;.    }..
20f4a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 4f      /* Add the O
20f4b 4e 20 63 6c 61 75 73 65 20 74 6f 20 74 68 65 20  N clause to the 
20f4c 65 6e 64 20 6f 66 20 74 68 65 20 57 48 45 52 45  end of the WHERE
20f4d 20 63 6c 61 75 73 65 2c 20 63 6f 6e 6e 65 63 74   clause, connect
20f4e 65 64 20 62 79 0a 20 20 20 20 2a 2a 20 61 6e 20  ed by.    ** an 
20f4f 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20  AND operator..  
20f50 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 52 69    */.    if( pRi
20f51 67 68 74 2d 3e 70 4f 6e 20 29 7b 0a 20 20 20 20  ght->pOn ){.    
20f52 20 20 69 66 28 20 69 73 4f 75 74 65 72 20 29 20    if( isOuter ) 
20f53 73 65 74 4a 6f 69 6e 45 78 70 72 28 70 52 69 67  setJoinExpr(pRig
20f54 68 74 2d 3e 70 4f 6e 2c 20 70 52 69 67 68 74 2d  ht->pOn, pRight-
20f55 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20  >iCursor);.     
20f56 20 70 2d 3e 70 57 68 65 72 65 20 3d 20 73 71 6c   p->pWhere = sql
20f57 69 74 65 33 45 78 70 72 41 6e 64 28 70 50 61 72  ite3ExprAnd(pPar
20f58 73 65 2d 3e 64 62 2c 20 70 2d 3e 70 57 68 65 72  se->db, p->pWher
20f59 65 2c 20 70 52 69 67 68 74 2d 3e 70 4f 6e 29 3b  e, pRight->pOn);
20f5a 0a 20 20 20 20 20 20 70 52 69 67 68 74 2d 3e 70  .      pRight->p
20f5b 4f 6e 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20  On = 0;.    }.. 
20f5c 20 20 20 2f 2a 20 43 72 65 61 74 65 20 65 78 74     /* Create ext
20f5d 72 61 20 74 65 72 6d 73 20 6f 6e 20 74 68 65 20  ra terms on the 
20f5e 57 48 45 52 45 20 63 6c 61 75 73 65 20 66 6f 72  WHERE clause for
20f5f 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6e 61 6d   each column nam
20f60 65 64 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65  ed.    ** in the
20f61 20 55 53 49 4e 47 20 63 6c 61 75 73 65 2e 20 20   USING clause.  
20f62 45 78 61 6d 70 6c 65 3a 20 49 66 20 74 68 65 20  Example: If the 
20f63 74 77 6f 20 74 61 62 6c 65 73 20 74 6f 20 62 65  two tables to be
20f64 20 6a 6f 69 6e 65 64 20 61 72 65 20 0a 20 20 20   joined are .   
20f65 20 2a 2a 20 41 20 61 6e 64 20 42 20 61 6e 64 20   ** A and B and 
20f66 74 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65  the USING clause
20f67 20 6e 61 6d 65 73 20 58 2c 20 59 2c 20 61 6e 64   names X, Y, and
20f68 20 5a 2c 20 74 68 65 6e 20 61 64 64 20 74 68 69   Z, then add thi
20f69 73 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20  s.    ** to the 
20f6a 57 48 45 52 45 20 63 6c 61 75 73 65 3a 20 20 20  WHERE clause:   
20f6b 20 41 2e 58 3d 42 2e 58 20 41 4e 44 20 41 2e 59   A.X=B.X AND A.Y
20f6c 3d 42 2e 59 20 41 4e 44 20 41 2e 5a 3d 42 2e 5a  =B.Y AND A.Z=B.Z
20f6d 0a 20 20 20 20 2a 2a 20 52 65 70 6f 72 74 20 61  .    ** Report a
20f6e 6e 20 65 72 72 6f 72 20 69 66 20 61 6e 79 20 63  n error if any c
20f6f 6f 6c 75 6d 6e 20 6d 65 6e 74 69 6f 6e 65 64 20  olumn mentioned 
20f70 69 6e 20 74 68 65 20 55 53 49 4e 47 20 63 6c 61  in the USING cla
20f71 75 73 65 20 69 73 0a 20 20 20 20 2a 2a 20 6e 6f  use is.    ** no
20f72 74 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 62  t contained in b
20f73 6f 74 68 20 74 61 62 6c 65 73 20 74 6f 20 62 65  oth tables to be
20f74 20 6a 6f 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a   joined..    */.
20f75 20 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e      if( pRight->
20f76 70 55 73 69 6e 67 20 29 7b 0a 20 20 20 20 20 20  pUsing ){.      
20f77 49 64 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20  IdList *pList = 
20f78 70 52 69 67 68 74 2d 3e 70 55 73 69 6e 67 3b 0a  pRight->pUsing;.
20f79 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
20f7a 3c 70 4c 69 73 74 2d 3e 6e 49 64 3b 20 6a 2b 2b  <pList->nId; j++
20f7b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  ){.        char 
20f7c 2a 7a 4e 61 6d 65 20 3d 20 70 4c 69 73 74 2d 3e  *zName = pList->
20f7d 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  a[j].zName;.    
20f7e 20 20 20 20 69 66 28 20 63 6f 6c 75 6d 6e 49 6e      if( columnIn
20f7f 64 65 78 28 70 4c 65 66 74 54 61 62 2c 20 7a 4e  dex(pLeftTab, zN
20f80 61 6d 65 29 3c 30 20 7c 7c 20 63 6f 6c 75 6d 6e  ame)<0 || column
20f81 49 6e 64 65 78 28 70 52 69 67 68 74 54 61 62 2c  Index(pRightTab,
20f82 20 7a 4e 61 6d 65 29 3c 30 20 29 7b 0a 20 20 20   zName)<0 ){.   
20f83 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
20f84 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
20f85 63 61 6e 6e 6f 74 20 6a 6f 69 6e 20 75 73 69 6e  cannot join usin
20f86 67 20 63 6f 6c 75 6d 6e 20 25 73 20 2d 20 63 6f  g column %s - co
20f87 6c 75 6d 6e 20 22 0a 20 20 20 20 20 20 20 20 20  lumn ".         
20f88 20 20 20 22 6e 6f 74 20 70 72 65 73 65 6e 74 20     "not present 
20f89 69 6e 20 62 6f 74 68 20 74 61 62 6c 65 73 22 2c  in both tables",
20f8a 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20   zName);.       
20f8b 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
20f8c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61       }.        a
20f8d 64 64 57 68 65 72 65 54 65 72 6d 28 70 50 61 72  ddWhereTerm(pPar
20f8e 73 65 2c 20 7a 4e 61 6d 65 2c 20 70 4c 65 66 74  se, zName, pLeft
20f8f 54 61 62 2c 20 70 4c 65 66 74 2d 3e 7a 41 6c 69  Tab, pLeft->zAli
20f90 61 73 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  as, .           
20f91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20f92 20 70 52 69 67 68 74 54 61 62 2c 20 70 52 69 67   pRightTab, pRig
20f93 68 74 2d 3e 7a 41 6c 69 61 73 2c 0a 20 20 20 20  ht->zAlias,.    
20f94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20f95 20 20 20 20 20 20 20 20 70 52 69 67 68 74 2d 3e          pRight->
20f96 69 43 75 72 73 6f 72 2c 20 26 70 2d 3e 70 57 68  iCursor, &p->pWh
20f97 65 72 65 2c 20 69 73 4f 75 74 65 72 29 3b 0a 20  ere, isOuter);. 
20f98 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
20f99 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
20f9a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 63 6f 64  /*.** Insert cod
20f9b 65 20 69 6e 74 6f 20 22 76 22 20 74 68 61 74 20  e into "v" that 
20f9c 77 69 6c 6c 20 70 75 73 68 20 74 68 65 20 72 65  will push the re
20f9d 63 6f 72 64 20 6f 6e 20 74 68 65 20 74 6f 70 20  cord on the top 
20f9e 6f 66 20 74 68 65 0a 2a 2a 20 73 74 61 63 6b 20  of the.** stack 
20f9f 69 6e 74 6f 20 74 68 65 20 73 6f 72 74 65 72 2e  into the sorter.
20fa0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
20fa1 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 0a  pushOntoSorter(.
20fa2 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
20fa3 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
20fa4 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
20fa5 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
20fa6 42 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 4f 52  By,    /* The OR
20fa7 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  DER BY clause */
20fa8 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  .  Select *pSele
20fa9 63 74 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ct,       /* The
20faa 20 77 68 6f 6c 65 20 53 45 4c 45 43 54 20 73 74   whole SELECT st
20fab 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  atement */.  int
20fac 20 72 65 67 44 61 74 61 20 20 20 20 20 20 20 20   regData        
20fad 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20      /* Register 
20fae 68 6f 6c 64 69 6e 67 20 64 61 74 61 20 74 6f 20  holding data to 
20faf 62 65 20 73 6f 72 74 65 64 20 2a 2f 0a 29 7b 0a  be sorted */.){.
20fb0 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
20fb1 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74  se->pVdbe;.  int
20fb2 20 6e 45 78 70 72 20 3d 20 70 4f 72 64 65 72 42   nExpr = pOrderB
20fb3 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 69 6e 74 20  y->nExpr;.  int 
20fb4 72 65 67 42 61 73 65 20 3d 20 73 71 6c 69 74 65  regBase = sqlite
20fb5 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50  3GetTempRange(pP
20fb6 61 72 73 65 2c 20 6e 45 78 70 72 2b 32 29 3b 0a  arse, nExpr+2);.
20fb7 20 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64 20    int regRecord 
20fb8 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
20fb9 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 73  Reg(pParse);.  s
20fba 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45 78  qlite3ExprCodeEx
20fbb 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70  prList(pParse, p
20fbc 4f 72 64 65 72 42 79 2c 20 72 65 67 42 61 73 65  OrderBy, regBase
20fbd 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  , 0);.  sqlite3V
20fbe 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
20fbf 53 65 71 75 65 6e 63 65 2c 20 70 4f 72 64 65 72  Sequence, pOrder
20fc0 42 79 2d 3e 69 45 43 75 72 73 6f 72 2c 20 72 65  By->iECursor, re
20fc1 67 42 61 73 65 2b 6e 45 78 70 72 29 3b 0a 20 20  gBase+nExpr);.  
20fc2 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d  sqlite3ExprCodeM
20fc3 6f 76 65 28 70 50 61 72 73 65 2c 20 72 65 67 44  ove(pParse, regD
20fc4 61 74 61 2c 20 72 65 67 42 61 73 65 2b 6e 45 78  ata, regBase+nEx
20fc5 70 72 2b 31 29 3b 0a 20 20 73 71 6c 69 74 65 33  pr+1);.  sqlite3
20fc6 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
20fc7 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67  _MakeRecord, reg
20fc8 42 61 73 65 2c 20 6e 45 78 70 72 20 2b 20 32 2c  Base, nExpr + 2,
20fc9 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 73   regRecord);.  s
20fca 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
20fcb 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74  (v, OP_IdxInsert
20fcc 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75  , pOrderBy->iECu
20fcd 72 73 6f 72 2c 20 72 65 67 52 65 63 6f 72 64 29  rsor, regRecord)
20fce 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61  ;.  sqlite3Relea
20fcf 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
20fd0 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20  , regRecord);.  
20fd1 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
20fd2 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
20fd3 72 65 67 42 61 73 65 2c 20 6e 45 78 70 72 2b 32  regBase, nExpr+2
20fd4 29 3b 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74  );.  if( pSelect
20fd5 2d 3e 69 4c 69 6d 69 74 3e 3d 30 20 29 7b 0a 20  ->iLimit>=0 ){. 
20fd6 20 20 20 69 6e 74 20 61 64 64 72 31 2c 20 61 64     int addr1, ad
20fd7 64 72 32 3b 0a 20 20 20 20 69 6e 74 20 69 4c 69  dr2;.    int iLi
20fd8 6d 69 74 3b 0a 20 20 20 20 69 66 28 20 70 53 65  mit;.    if( pSe
20fd9 6c 65 63 74 2d 3e 70 4f 66 66 73 65 74 20 29 7b  lect->pOffset ){
20fda 0a 20 20 20 20 20 20 69 4c 69 6d 69 74 20 3d 20  .      iLimit = 
20fdb 70 53 65 6c 65 63 74 2d 3e 69 4f 66 66 73 65 74  pSelect->iOffset
20fdc 2b 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  +1;.    }else{. 
20fdd 20 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 70 53       iLimit = pS
20fde 65 6c 65 63 74 2d 3e 69 4c 69 6d 69 74 3b 0a 20  elect->iLimit;. 
20fdf 20 20 20 7d 0a 20 20 20 20 61 64 64 72 31 20 3d     }.    addr1 =
20fe0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
20fe1 70 31 28 76 2c 20 4f 50 5f 49 66 5a 65 72 6f 2c  p1(v, OP_IfZero,
20fe2 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 73 71   iLimit);.    sq
20fe3 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
20fe4 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 69 4c  v, OP_AddImm, iL
20fe5 69 6d 69 74 2c 20 2d 31 29 3b 0a 20 20 20 20 61  imit, -1);.    a
20fe6 64 64 72 32 20 3d 20 73 71 6c 69 74 65 33 56 64  ddr2 = sqlite3Vd
20fe7 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47  beAddOp0(v, OP_G
20fe8 6f 74 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  oto);.    sqlite
20fe9 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
20fea 20 61 64 64 72 31 29 3b 0a 20 20 20 20 73 71 6c   addr1);.    sql
20feb 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
20fec 2c 20 4f 50 5f 4c 61 73 74 2c 20 70 4f 72 64 65  , OP_Last, pOrde
20fed 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 29 3b 0a  rBy->iECursor);.
20fee 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
20fef 64 64 4f 70 31 28 76 2c 20 4f 50 5f 44 65 6c 65  ddOp1(v, OP_Dele
20ff0 74 65 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69 45  te, pOrderBy->iE
20ff1 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 73 71 6c  Cursor);.    sql
20ff2 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
20ff3 28 76 2c 20 61 64 64 72 32 29 3b 0a 20 20 20 20  (v, addr2);.    
20ff4 70 53 65 6c 65 63 74 2d 3e 69 4c 69 6d 69 74 20  pSelect->iLimit 
20ff5 3d 20 2d 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  = -1;.  }.}../*.
20ff6 2a 2a 20 41 64 64 20 63 6f 64 65 20 74 6f 20 69  ** Add code to i
20ff7 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 4f 46 46  mplement the OFF
20ff8 53 45 54 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  SET.*/.static vo
20ff9 69 64 20 63 6f 64 65 4f 66 66 73 65 74 28 0a 20  id codeOffset(. 
20ffa 20 56 64 62 65 20 2a 76 2c 20 20 20 20 20 20 20   Vdbe *v,       
20ffb 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63     /* Generate c
20ffc 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20 56 4d  ode into this VM
20ffd 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c   */.  Select *p,
20ffe 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53          /* The S
20fff 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
21000 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20  being coded */. 
21001 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65 20 20   int iContinue  
21002 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20     /* Jump here 
21003 74 6f 20 73 6b 69 70 20 74 68 65 20 63 75 72 72  to skip the curr
21004 65 6e 74 20 72 65 63 6f 72 64 20 2a 2f 0a 29 7b  ent record */.){
21005 0a 20 20 69 66 28 20 70 2d 3e 69 4f 66 66 73 65  .  if( p->iOffse
21006 74 3e 3d 30 20 26 26 20 69 43 6f 6e 74 69 6e 75  t>=0 && iContinu
21007 65 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  e!=0 ){.    int 
21008 61 64 64 72 3b 0a 20 20 20 20 73 71 6c 69 74 65  addr;.    sqlite
21009 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2100a 50 5f 41 64 64 49 6d 6d 2c 20 70 2d 3e 69 4f 66  P_AddImm, p->iOf
2100b 66 73 65 74 2c 20 2d 31 29 3b 0a 20 20 20 20 61  fset, -1);.    a
2100c 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
2100d 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
2100e 4e 65 67 2c 20 70 2d 3e 69 4f 66 66 73 65 74 29  Neg, p->iOffset)
2100f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
21010 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
21011 74 6f 2c 20 30 2c 20 69 43 6f 6e 74 69 6e 75 65  to, 0, iContinue
21012 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  );.    VdbeComme
21013 6e 74 28 28 76 2c 20 22 73 6b 69 70 20 4f 46 46  nt((v, "skip OFF
21014 53 45 54 20 72 65 63 6f 72 64 73 22 29 29 3b 0a  SET records"));.
21015 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
21016 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29  umpHere(v, addr)
21017 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ;.  }.}../*.** A
21018 64 64 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c  dd code that wil
21019 6c 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20  l check to make 
2101a 73 75 72 65 20 74 68 65 20 4e 20 72 65 67 69 73  sure the N regis
2101b 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 61 74  ters starting at
2101c 20 69 4d 65 6d 0a 2a 2a 20 66 6f 72 6d 20 61 20   iMem.** form a 
2101d 64 69 73 74 69 6e 63 74 20 65 6e 74 72 79 2e 20  distinct entry. 
2101e 20 69 54 61 62 20 69 73 20 61 20 73 6f 72 74 69   iTab is a sorti
2101f 6e 67 20 69 6e 64 65 78 20 74 68 61 74 20 68 6f  ng index that ho
21020 6c 64 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a  lds previously.*
21021 2a 20 73 65 65 6e 20 63 6f 6d 62 69 6e 61 74 69  * seen combinati
21022 6f 6e 73 20 6f 66 20 74 68 65 20 4e 20 76 61 6c  ons of the N val
21023 75 65 73 2e 20 20 41 20 6e 65 77 20 65 6e 74 72  ues.  A new entr
21024 79 20 69 73 20 6d 61 64 65 20 69 6e 20 69 54 61  y is made in iTa
21025 62 0a 2a 2a 20 69 66 20 74 68 65 20 63 75 72 72  b.** if the curr
21026 65 6e 74 20 4e 20 76 61 6c 75 65 73 20 61 72 65  ent N values are
21027 20 6e 65 77 2e 0a 2a 2a 0a 2a 2a 20 41 20 6a 75   new..**.** A ju
21028 6d 70 20 74 6f 20 61 64 64 72 52 65 70 65 61 74  mp to addrRepeat
21029 20 69 73 20 6d 61 64 65 20 61 6e 64 20 74 68 65   is made and the
2102a 20 4e 2b 31 20 76 61 6c 75 65 73 20 61 72 65 20   N+1 values are 
2102b 70 6f 70 70 65 64 20 66 72 6f 6d 20 74 68 65 0a  popped from the.
2102c 2a 2a 20 73 74 61 63 6b 20 69 66 20 74 68 65 20  ** stack if the 
2102d 74 6f 70 20 4e 20 65 6c 65 6d 65 6e 74 73 20 61  top N elements a
2102e 72 65 20 6e 6f 74 20 64 69 73 74 69 6e 63 74 2e  re not distinct.
2102f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
21030 63 6f 64 65 44 69 73 74 69 6e 63 74 28 0a 20 20  codeDistinct(.  
21031 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
21032 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e     /* Parsing an
21033 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e  d code generatin
21034 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69  g context */.  i
21035 6e 74 20 69 54 61 62 2c 20 20 20 20 20 20 20 20  nt iTab,        
21036 20 20 2f 2a 20 41 20 73 6f 72 74 69 6e 67 20 69    /* A sorting i
21037 6e 64 65 78 20 75 73 65 64 20 74 6f 20 74 65 73  ndex used to tes
21038 74 20 66 6f 72 20 64 69 73 74 69 6e 63 74 6e 65  t for distinctne
21039 73 73 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  ss */.  int addr
2103a 52 65 70 65 61 74 2c 20 20 20 20 2f 2a 20 4a 75  Repeat,    /* Ju
2103b 6d 70 20 74 6f 20 68 65 72 65 20 69 66 20 6e 6f  mp to here if no
2103c 74 20 64 69 73 74 69 6e 63 74 20 2a 2f 0a 20 20  t distinct */.  
2103d 69 6e 74 20 4e 2c 20 20 20 20 20 20 20 20 20 20  int N,          
2103e 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2103f 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e  elements */.  in
21040 74 20 69 4d 65 6d 20 20 20 20 20 20 20 20 20 20  t iMem          
21041 20 2f 2a 20 46 69 72 73 74 20 65 6c 65 6d 65 6e   /* First elemen
21042 74 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a  t */.){.  Vdbe *
21043 76 3b 0a 20 20 69 6e 74 20 72 31 3b 0a 0a 20 20  v;.  int r1;..  
21044 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
21045 65 3b 0a 20 20 72 31 20 3d 20 73 71 6c 69 74 65  e;.  r1 = sqlite
21046 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
21047 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  se);.  sqlite3Vd
21048 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
21049 61 6b 65 52 65 63 6f 72 64 2c 20 69 4d 65 6d 2c  akeRecord, iMem,
2104a 20 4e 2c 20 72 31 29 3b 0a 20 20 73 71 6c 69 74   N, r1);.  sqlit
2104b 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2104c 4f 50 5f 46 6f 75 6e 64 2c 20 69 54 61 62 2c 20  OP_Found, iTab, 
2104d 61 64 64 72 52 65 70 65 61 74 2c 20 72 31 29 3b  addrRepeat, r1);
2104e 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2104f 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e  dOp2(v, OP_IdxIn
21050 73 65 72 74 2c 20 69 54 61 62 2c 20 72 31 29 3b  sert, iTab, r1);
21051 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
21052 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
21053 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47   r1);.}../*.** G
21054 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72  enerate an error
21055 20 6d 65 73 73 61 67 65 20 77 68 65 6e 20 61 20   message when a 
21056 53 45 4c 45 43 54 20 69 73 20 75 73 65 64 20 77  SELECT is used w
21057 69 74 68 69 6e 20 61 20 73 75 62 65 78 70 72 65  ithin a subexpre
21058 73 73 69 6f 6e 0a 2a 2a 20 28 65 78 61 6d 70 6c  ssion.** (exampl
21059 65 3a 20 20 22 61 20 49 4e 20 28 53 45 4c 45 43  e:  "a IN (SELEC
2105a 54 20 2a 20 46 52 4f 4d 20 74 61 62 6c 65 29 22  T * FROM table)"
2105b 29 20 62 75 74 20 69 74 20 68 61 73 20 6d 6f 72  ) but it has mor
2105c 65 20 74 68 61 6e 20 31 20 72 65 73 75 6c 74 0a  e than 1 result.
2105d 2a 2a 20 63 6f 6c 75 6d 6e 2e 20 20 57 65 20 64  ** column.  We d
2105e 6f 20 74 68 69 73 20 69 6e 20 61 20 73 75 62 72  o this in a subr
2105f 6f 75 74 69 6e 65 20 62 65 63 61 75 73 65 20 74  outine because t
21060 68 65 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  he error occurs 
21061 69 6e 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 70  in multiple.** p
21062 6c 61 63 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  laces..*/.static
21063 20 69 6e 74 20 63 68 65 63 6b 46 6f 72 4d 75 6c   int checkForMul
21064 74 69 43 6f 6c 75 6d 6e 53 65 6c 65 63 74 45 72  tiColumnSelectEr
21065 72 6f 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ror(.  Parse *pP
21066 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50  arse,       /* P
21067 61 72 73 65 20 63 6f 6e 74 65 78 74 2e 20 2a 2f  arse context. */
21068 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70  .  SelectDest *p
21069 44 65 73 74 2c 20 20 20 2f 2a 20 44 65 73 74 69  Dest,   /* Desti
2106a 6e 61 74 69 6f 6e 20 6f 66 20 53 45 4c 45 43 54  nation of SELECT
2106b 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 69 6e   results */.  in
2106c 74 20 6e 45 78 70 72 20 20 20 20 20 20 20 20 20  t nExpr         
2106d 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2106e 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 72  result columns r
2106f 65 74 75 72 6e 65 64 20 62 79 20 53 45 4c 45 43  eturned by SELEC
21070 54 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 65 44  T */.){.  int eD
21071 65 73 74 20 3d 20 70 44 65 73 74 2d 3e 65 44 65  est = pDest->eDe
21072 73 74 3b 0a 20 20 69 66 28 20 6e 45 78 70 72 3e  st;.  if( nExpr>
21073 31 20 26 26 20 28 65 44 65 73 74 3d 3d 53 52 54  1 && (eDest==SRT
21074 5f 4d 65 6d 20 7c 7c 20 65 44 65 73 74 3d 3d 53  _Mem || eDest==S
21075 52 54 5f 53 65 74 29 20 29 7b 0a 20 20 20 20 73  RT_Set) ){.    s
21076 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
21077 50 61 72 73 65 2c 20 22 6f 6e 6c 79 20 61 20 73  Parse, "only a s
21078 69 6e 67 6c 65 20 72 65 73 75 6c 74 20 61 6c 6c  ingle result all
21079 6f 77 65 64 20 66 6f 72 20 22 0a 20 20 20 20 20  owed for ".     
2107a 20 20 22 61 20 53 45 4c 45 43 54 20 74 68 61 74    "a SELECT that
2107b 20 69 73 20 70 61 72 74 20 6f 66 20 61 6e 20 65   is part of an e
2107c 78 70 72 65 73 73 69 6f 6e 22 29 3b 0a 20 20 20  xpression");.   
2107d 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c   return 1;.  }el
2107e 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  se{.    return 0
2107f 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
21080 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65  his routine gene
21081 72 61 74 65 73 20 74 68 65 20 63 6f 64 65 20 66  rates the code f
21082 6f 72 20 74 68 65 20 69 6e 73 69 64 65 20 6f 66  or the inside of
21083 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f 6f 70 0a   the inner loop.
21084 2a 2a 20 6f 66 20 61 20 53 45 4c 45 43 54 2e 0a  ** of a SELECT..
21085 2a 2a 0a 2a 2a 20 49 66 20 73 72 63 54 61 62 20  **.** If srcTab 
21086 61 6e 64 20 6e 43 6f 6c 75 6d 6e 20 61 72 65 20  and nColumn are 
21087 62 6f 74 68 20 7a 65 72 6f 2c 20 74 68 65 6e 20  both zero, then 
21088 74 68 65 20 70 45 4c 69 73 74 20 65 78 70 72 65  the pEList expre
21089 73 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 65 76  ssions.** are ev
2108a 61 6c 75 61 74 65 64 20 69 6e 20 6f 72 64 65 72  aluated in order
2108b 20 74 6f 20 67 65 74 20 74 68 65 20 64 61 74 61   to get the data
2108c 20 66 6f 72 20 74 68 69 73 20 72 6f 77 2e 20 20   for this row.  
2108d 49 66 20 6e 43 6f 6c 75 6d 6e 3e 30 0a 2a 2a 20  If nColumn>0.** 
2108e 74 68 65 6e 20 64 61 74 61 20 69 73 20 70 75 6c  then data is pul
2108f 6c 65 64 20 66 72 6f 6d 20 73 72 63 54 61 62 20  led from srcTab 
21090 61 6e 64 20 70 45 4c 69 73 74 20 69 73 20 75 73  and pEList is us
21091 65 64 20 6f 6e 6c 79 20 74 6f 20 67 65 74 20 74  ed only to get t
21092 68 65 0a 2a 2a 20 64 61 74 61 74 79 70 65 73 20  he.** datatypes 
21093 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 2e  for each column.
21094 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
21095 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28  selectInnerLoop(
21096 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
21097 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ,          /* Th
21098 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
21099 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c   */.  Select *p,
2109a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2109b 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 73 65   The complete se
2109c 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 62  lect statement b
2109d 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20  eing coded */.  
2109e 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
2109f 2c 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20  ,       /* List 
210a0 6f 66 20 76 61 6c 75 65 73 20 62 65 69 6e 67 20  of values being 
210a1 65 78 74 72 61 63 74 65 64 20 2a 2f 0a 20 20 69  extracted */.  i
210a2 6e 74 20 73 72 63 54 61 62 2c 20 20 20 20 20 20  nt srcTab,      
210a3 20 20 20 20 20 20 20 2f 2a 20 50 75 6c 6c 20 64         /* Pull d
210a4 61 74 61 20 66 72 6f 6d 20 74 68 69 73 20 74 61  ata from this ta
210a5 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  ble */.  int nCo
210a6 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20 20  lumn,           
210a7 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f   /* Number of co
210a8 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 73 6f 75  lumns in the sou
210a9 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 45  rce table */.  E
210aa 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42  xprList *pOrderB
210ab 79 2c 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 74  y,     /* If not
210ac 20 4e 55 4c 4c 2c 20 73 6f 72 74 20 72 65 73 75   NULL, sort resu
210ad 6c 74 73 20 75 73 69 6e 67 20 74 68 69 73 20 6b  lts using this k
210ae 65 79 20 2a 2f 0a 20 20 69 6e 74 20 64 69 73 74  ey */.  int dist
210af 69 6e 63 74 2c 20 20 20 20 20 20 20 20 20 20 20  inct,           
210b0 2f 2a 20 49 66 20 3e 3d 30 2c 20 6d 61 6b 65 20  /* If >=0, make 
210b1 73 75 72 65 20 72 65 73 75 6c 74 73 20 61 72 65  sure results are
210b2 20 64 69 73 74 69 6e 63 74 20 2a 2f 0a 20 20 53   distinct */.  S
210b3 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73 74  electDest *pDest
210b4 2c 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f  ,      /* How to
210b5 20 64 69 73 70 6f 73 65 20 6f 66 20 74 68 65 20   dispose of the 
210b6 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 69 6e 74  results */.  int
210b7 20 69 43 6f 6e 74 69 6e 75 65 2c 20 20 20 20 20   iContinue,     
210b8 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72       /* Jump her
210b9 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69  e to continue wi
210ba 74 68 20 6e 65 78 74 20 72 6f 77 20 2a 2f 0a 20  th next row */. 
210bb 20 69 6e 74 20 69 42 72 65 61 6b 2c 20 20 20 20   int iBreak,    
210bc 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70           /* Jump
210bd 20 68 65 72 65 20 74 6f 20 62 72 65 61 6b 20 6f   here to break o
210be 75 74 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 20  ut of the inner 
210bf 6c 6f 6f 70 20 2a 2f 0a 20 20 63 68 61 72 20 2a  loop */.  char *
210c0 61 66 66 20 20 20 20 20 20 20 20 20 20 20 20 20  aff             
210c1 20 20 2f 2a 20 61 66 66 69 6e 69 74 79 20 73 74    /* affinity st
210c2 72 69 6e 67 20 69 66 20 65 44 65 73 74 20 69 73  ring if eDest is
210c3 20 53 52 54 5f 55 6e 69 6f 6e 20 2a 2f 0a 29 7b   SRT_Union */.){
210c4 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61  .  Vdbe *v = pPa
210c5 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e  rse->pVdbe;.  in
210c6 74 20 69 3b 0a 20 20 69 6e 74 20 68 61 73 44 69  t i;.  int hasDi
210c7 73 74 69 6e 63 74 3b 20 20 20 20 20 20 20 20 2f  stinct;        /
210c8 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 44 49  * True if the DI
210c9 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 69  STINCT keyword i
210ca 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69  s present */.  i
210cb 6e 74 20 72 65 67 52 65 73 75 6c 74 3b 20 20 20  nt regResult;   
210cc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74             /* St
210cd 61 72 74 20 6f 66 20 6d 65 6d 6f 72 79 20 68 6f  art of memory ho
210ce 6c 64 69 6e 67 20 72 65 73 75 6c 74 20 73 65 74  lding result set
210cf 20 2a 2f 0a 20 20 69 6e 74 20 65 44 65 73 74 20   */.  int eDest 
210d0 3d 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3b 20  = pDest->eDest; 
210d1 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69 73 70    /* How to disp
210d2 6f 73 65 20 6f 66 20 72 65 73 75 6c 74 73 20 2a  ose of results *
210d3 2f 0a 20 20 69 6e 74 20 69 50 61 72 6d 20 3d 20  /.  int iParm = 
210d4 70 44 65 73 74 2d 3e 69 50 61 72 6d 3b 20 20 20  pDest->iParm;   
210d5 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e  /* First argumen
210d6 74 20 74 6f 20 64 69 73 70 6f 73 61 6c 20 6d 65  t to disposal me
210d7 74 68 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  thod */.  int nR
210d8 65 73 75 6c 74 43 6f 6c 3b 20 20 20 20 20 20 20  esultCol;       
210d9 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
210da 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  of result column
210db 73 20 2a 2f 0a 0a 20 20 69 66 28 20 76 3d 3d 30  s */..  if( v==0
210dc 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73   ) return;.  ass
210dd 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29  ert( pEList!=0 )
210de 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  ;..  /* If there
210df 20 77 61 73 20 61 20 4c 49 4d 49 54 20 63 6c 61   was a LIMIT cla
210e0 75 73 65 20 6f 6e 20 74 68 65 20 53 45 4c 45 43  use on the SELEC
210e1 54 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65  T statement, the
210e2 6e 20 64 6f 20 74 68 65 20 63 68 65 63 6b 0a 20  n do the check. 
210e3 20 2a 2a 20 74 6f 20 73 65 65 20 69 66 20 74 68   ** to see if th
210e4 69 73 20 72 6f 77 20 73 68 6f 75 6c 64 20 62 65  is row should be
210e5 20 6f 75 74 70 75 74 2e 0a 20 20 2a 2f 0a 20 20   output..  */.  
210e6 68 61 73 44 69 73 74 69 6e 63 74 20 3d 20 64 69  hasDistinct = di
210e7 73 74 69 6e 63 74 3e 3d 30 20 26 26 20 70 45 4c  stinct>=0 && pEL
210e8 69 73 74 2d 3e 6e 45 78 70 72 3e 30 3b 0a 20 20  ist->nExpr>0;.  
210e9 69 66 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20  if( pOrderBy==0 
210ea 26 26 20 21 68 61 73 44 69 73 74 69 6e 63 74 20  && !hasDistinct 
210eb 29 7b 0a 20 20 20 20 63 6f 64 65 4f 66 66 73 65  ){.    codeOffse
210ec 74 28 76 2c 20 70 2c 20 69 43 6f 6e 74 69 6e 75  t(v, p, iContinu
210ed 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50 75  e);.  }..  /* Pu
210ee 6c 6c 20 74 68 65 20 72 65 71 75 65 73 74 65 64  ll the requested
210ef 20 63 6f 6c 75 6d 6e 73 2e 0a 20 20 2a 2f 0a 20   columns..  */. 
210f0 20 69 66 28 20 6e 43 6f 6c 75 6d 6e 3e 30 20 29   if( nColumn>0 )
210f1 7b 0a 20 20 20 20 6e 52 65 73 75 6c 74 43 6f 6c  {.    nResultCol
210f2 20 3d 20 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 7d 65   = nColumn;.  }e
210f3 6c 73 65 7b 0a 20 20 20 20 6e 52 65 73 75 6c 74  lse{.    nResult
210f4 43 6f 6c 20 3d 20 70 45 4c 69 73 74 2d 3e 6e 45  Col = pEList->nE
210f5 78 70 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  xpr;.  }.  if( p
210f6 44 65 73 74 2d 3e 69 4d 65 6d 3d 3d 30 20 29 7b  Dest->iMem==0 ){
210f7 0a 20 20 20 20 70 44 65 73 74 2d 3e 69 4d 65 6d  .    pDest->iMem
210f8 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
210f9 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e  pRange(pParse, n
210fa 52 65 73 75 6c 74 43 6f 6c 29 3b 0a 20 20 20 20  ResultCol);.    
210fb 70 44 65 73 74 2d 3e 6e 4d 65 6d 20 3d 20 6e 52  pDest->nMem = nR
210fc 65 73 75 6c 74 43 6f 6c 3b 0a 20 20 7d 65 6c 73  esultCol;.  }els
210fd 65 20 69 66 28 20 70 44 65 73 74 2d 3e 6e 4d 65  e if( pDest->nMe
210fe 6d 21 3d 6e 52 65 73 75 6c 74 43 6f 6c 20 29 7b  m!=nResultCol ){
210ff 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70  .    /* This hap
21100 70 65 6e 73 20 77 68 65 6e 20 74 77 6f 20 53 45  pens when two SE
21101 4c 45 43 54 73 20 6f 66 20 61 20 63 6f 6d 70 6f  LECTs of a compo
21102 75 6e 64 20 53 45 4c 45 43 54 20 68 61 76 65 20  und SELECT have 
21103 64 69 66 66 65 72 69 6e 67 0a 20 20 20 20 2a 2a  differing.    **
21104 20 6e 75 6d 62 65 72 73 20 6f 66 20 72 65 73 75   numbers of resu
21105 6c 74 20 63 6f 6c 75 6d 6e 73 2e 20 20 54 68 65  lt columns.  The
21106 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77   error message w
21107 69 6c 6c 20 62 65 20 67 65 6e 65 72 61 74 65 64  ill be generated
21108 20 62 79 0a 20 20 20 20 2a 2a 20 61 20 68 69 67   by.    ** a hig
21109 68 65 72 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e  her-level routin
2110a 65 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  e. */.    return
2110b 3b 0a 20 20 7d 0a 20 20 72 65 67 52 65 73 75 6c  ;.  }.  regResul
2110c 74 20 3d 20 70 44 65 73 74 2d 3e 69 4d 65 6d 3b  t = pDest->iMem;
2110d 0a 20 20 69 66 28 20 6e 43 6f 6c 75 6d 6e 3e 30  .  if( nColumn>0
2110e 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
2110f 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29   i<nColumn; i++)
21110 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
21111 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
21112 43 6f 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20  Column, srcTab, 
21113 69 2c 20 72 65 67 52 65 73 75 6c 74 2b 69 29 3b  i, regResult+i);
21114 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
21115 66 28 20 65 44 65 73 74 21 3d 53 52 54 5f 45 78  f( eDest!=SRT_Ex
21116 69 73 74 73 20 29 7b 0a 20 20 20 20 2f 2a 20 49  ists ){.    /* I
21117 66 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  f the destinatio
21118 6e 20 69 73 20 61 6e 20 45 58 49 53 54 53 28 2e  n is an EXISTS(.
21119 2e 2e 29 20 65 78 70 72 65 73 73 69 6f 6e 2c 20  ..) expression, 
2111a 74 68 65 20 61 63 74 75 61 6c 0a 20 20 20 20 2a  the actual.    *
2111b 2a 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65  * values returne
2111c 64 20 62 79 20 74 68 65 20 53 45 4c 45 43 54 20  d by the SELECT 
2111d 61 72 65 20 6e 6f 74 20 72 65 71 75 69 72 65 64  are not required
2111e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  ..    */.    sql
2111f 69 74 65 33 45 78 70 72 43 6f 64 65 45 78 70 72  ite3ExprCodeExpr
21120 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 45 4c  List(pParse, pEL
21121 69 73 74 2c 20 72 65 67 52 65 73 75 6c 74 2c 20  ist, regResult, 
21122 65 44 65 73 74 3d 3d 53 52 54 5f 43 61 6c 6c 62  eDest==SRT_Callb
21123 61 63 6b 29 3b 0a 20 20 7d 0a 20 20 6e 43 6f 6c  ack);.  }.  nCol
21124 75 6d 6e 20 3d 20 6e 52 65 73 75 6c 74 43 6f 6c  umn = nResultCol
21125 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 44  ;..  /* If the D
21126 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20  ISTINCT keyword 
21127 77 61 73 20 70 72 65 73 65 6e 74 20 6f 6e 20 74  was present on t
21128 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
21129 65 6e 74 0a 20 20 2a 2a 20 61 6e 64 20 74 68 69  ent.  ** and thi
2112a 73 20 72 6f 77 20 68 61 73 20 62 65 65 6e 20 73  s row has been s
2112b 65 65 6e 20 62 65 66 6f 72 65 2c 20 74 68 65 6e  een before, then
2112c 20 64 6f 20 6e 6f 74 20 6d 61 6b 65 20 74 68 69   do not make thi
2112d 73 20 72 6f 77 0a 20 20 2a 2a 20 70 61 72 74 20  s row.  ** part 
2112e 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 20  of the result.. 
2112f 20 2a 2f 0a 20 20 69 66 28 20 68 61 73 44 69 73   */.  if( hasDis
21130 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 61 73 73  tinct ){.    ass
21131 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29  ert( pEList!=0 )
21132 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45  ;.    assert( pE
21133 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6e 43 6f  List->nExpr==nCo
21134 6c 75 6d 6e 20 29 3b 0a 20 20 20 20 63 6f 64 65  lumn );.    code
21135 44 69 73 74 69 6e 63 74 28 70 50 61 72 73 65 2c  Distinct(pParse,
21136 20 64 69 73 74 69 6e 63 74 2c 20 69 43 6f 6e 74   distinct, iCont
21137 69 6e 75 65 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72  inue, nColumn, r
21138 65 67 52 65 73 75 6c 74 29 3b 0a 20 20 20 20 69  egResult);.    i
21139 66 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29  f( pOrderBy==0 )
2113a 7b 0a 20 20 20 20 20 20 63 6f 64 65 4f 66 66 73  {.      codeOffs
2113b 65 74 28 76 2c 20 70 2c 20 69 43 6f 6e 74 69 6e  et(v, p, iContin
2113c 75 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ue);.    }.  }..
2113d 20 20 69 66 28 20 63 68 65 63 6b 46 6f 72 4d 75    if( checkForMu
2113e 6c 74 69 43 6f 6c 75 6d 6e 53 65 6c 65 63 74 45  ltiColumnSelectE
2113f 72 72 6f 72 28 70 50 61 72 73 65 2c 20 70 44 65  rror(pParse, pDe
21140 73 74 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70  st, pEList->nExp
21141 72 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  r) ){.    return
21142 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 74 63 68 28  ;.  }..  switch(
21143 20 65 44 65 73 74 20 29 7b 0a 20 20 20 20 2f 2a   eDest ){.    /*
21144 20 49 6e 20 74 68 69 73 20 6d 6f 64 65 2c 20 77   In this mode, w
21145 72 69 74 65 20 65 61 63 68 20 71 75 65 72 79 20  rite each query 
21146 72 65 73 75 6c 74 20 74 6f 20 74 68 65 20 6b 65  result to the ke
21147 79 20 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 61  y of the tempora
21148 72 79 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65 20  ry.    ** table 
21149 69 50 61 72 6d 2e 0a 20 20 20 20 2a 2f 0a 23 69  iParm..    */.#i
2114a 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2114b 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43  T_COMPOUND_SELEC
2114c 54 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 55  T.    case SRT_U
2114d 6e 69 6f 6e 3a 20 7b 0a 20 20 20 20 20 20 69 6e  nion: {.      in
2114e 74 20 72 31 3b 0a 20 20 20 20 20 20 72 31 20 3d  t r1;.      r1 =
2114f 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
21150 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
21151 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21152 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op3(v, OP_MakeRe
21153 63 6f 72 64 2c 20 72 65 67 52 65 73 75 6c 74 2c  cord, regResult,
21154 20 6e 43 6f 6c 75 6d 6e 2c 20 72 31 29 3b 0a 20   nColumn, r1);. 
21155 20 20 20 20 20 69 66 28 20 61 66 66 20 29 7b 0a       if( aff ){.
21156 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
21157 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d  dbeChangeP4(v, -
21158 31 2c 20 61 66 66 2c 20 50 34 5f 53 54 41 54 49  1, aff, P4_STATI
21159 43 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  C);.      }.    
2115a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2115b 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73  Op2(v, OP_IdxIns
2115c 65 72 74 2c 20 69 50 61 72 6d 2c 20 72 31 29 3b  ert, iParm, r1);
2115d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
2115e 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
2115f 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20  rse, r1);.      
21160 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
21161 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20 61    /* Construct a
21162 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 74 68 65   record from the
21163 20 71 75 65 72 79 20 72 65 73 75 6c 74 2c 20 62   query result, b
21164 75 74 20 69 6e 73 74 65 61 64 20 6f 66 0a 20 20  ut instead of.  
21165 20 20 2a 2a 20 73 61 76 69 6e 67 20 74 68 61 74    ** saving that
21166 20 72 65 63 6f 72 64 2c 20 75 73 65 20 69 74 20   record, use it 
21167 61 73 20 61 20 6b 65 79 20 74 6f 20 64 65 6c 65  as a key to dele
21168 74 65 20 65 6c 65 6d 65 6e 74 73 20 66 72 6f 6d  te elements from
21169 0a 20 20 20 20 2a 2a 20 74 68 65 20 74 65 6d 70  .    ** the temp
2116a 6f 72 61 72 79 20 74 61 62 6c 65 20 69 50 61 72  orary table iPar
2116b 6d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  m..    */.    ca
2116c 73 65 20 53 52 54 5f 45 78 63 65 70 74 3a 20 7b  se SRT_Except: {
2116d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2116e 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49  beAddOp3(v, OP_I
2116f 64 78 44 65 6c 65 74 65 2c 20 69 50 61 72 6d 2c  dxDelete, iParm,
21170 20 72 65 67 52 65 73 75 6c 74 2c 20 6e 43 6f 6c   regResult, nCol
21171 75 6d 6e 29 3b 0a 20 20 20 20 20 20 62 72 65 61  umn);.      brea
21172 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  k;.    }.#endif.
21173 0a 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68  .    /* Store th
21174 65 20 72 65 73 75 6c 74 20 61 73 20 64 61 74 61  e result as data
21175 20 75 73 69 6e 67 20 61 20 75 6e 69 71 75 65 20   using a unique 
21176 6b 65 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  key..    */.    
21177 63 61 73 65 20 53 52 54 5f 54 61 62 6c 65 3a 0a  case SRT_Table:.
21178 20 20 20 20 63 61 73 65 20 53 52 54 5f 45 70 68      case SRT_Eph
21179 65 6d 54 61 62 3a 20 7b 0a 20 20 20 20 20 20 69  emTab: {.      i
2117a 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47  nt r1 = sqlite3G
2117b 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
2117c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2117d 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2117e 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67  _MakeRecord, reg
2117f 52 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 2c  Result, nColumn,
21180 20 72 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20   r1);.      if( 
21181 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
21182 20 20 20 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74      pushOntoSort
21183 65 72 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65  er(pParse, pOrde
21184 72 42 79 2c 20 70 2c 20 72 31 29 3b 0a 20 20 20  rBy, p, r1);.   
21185 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
21186 20 20 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74    int r2 = sqlit
21187 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
21188 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71  rse);.        sq
21189 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2118a 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20  v, OP_NewRowid, 
2118b 69 50 61 72 6d 2c 20 72 32 29 3b 0a 20 20 20 20  iParm, r2);.    
2118c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2118d 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65  ddOp3(v, OP_Inse
2118e 72 74 2c 20 69 50 61 72 6d 2c 20 72 31 2c 20 72  rt, iParm, r1, r
2118f 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  2);.        sqli
21190 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
21191 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44  v, OPFLAG_APPEND
21192 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
21193 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
21194 28 70 50 61 72 73 65 2c 20 72 32 29 3b 0a 20 20  (pParse, r2);.  
21195 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
21196 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
21197 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20  g(pParse, r1);. 
21198 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
21199 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
2119a 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a  E_OMIT_SUBQUERY.
2119b 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65      /* If we are
2119c 20 63 72 65 61 74 69 6e 67 20 61 20 73 65 74 20   creating a set 
2119d 66 6f 72 20 61 6e 20 22 65 78 70 72 20 49 4e 20  for an "expr IN 
2119e 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 20 63 6f  (SELECT ...)" co
2119f 6e 73 74 72 75 63 74 2c 0a 20 20 20 20 2a 2a 20  nstruct,.    ** 
211a0 74 68 65 6e 20 74 68 65 72 65 20 73 68 6f 75 6c  then there shoul
211a1 64 20 62 65 20 61 20 73 69 6e 67 6c 65 20 69 74  d be a single it
211a2 65 6d 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e  em on the stack.
211a3 20 20 57 72 69 74 65 20 74 68 69 73 0a 20 20 20    Write this.   
211a4 20 2a 2a 20 69 74 65 6d 20 69 6e 74 6f 20 74 68   ** item into th
211a5 65 20 73 65 74 20 74 61 62 6c 65 20 77 69 74 68  e set table with
211a6 20 62 6f 67 75 73 20 64 61 74 61 2e 0a 20 20 20   bogus data..   
211a7 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54   */.    case SRT
211a8 5f 53 65 74 3a 20 7b 0a 20 20 20 20 20 20 69 6e  _Set: {.      in
211a9 74 20 61 64 64 72 32 3b 0a 0a 20 20 20 20 20 20  t addr2;..      
211aa 61 73 73 65 72 74 28 20 6e 43 6f 6c 75 6d 6e 3d  assert( nColumn=
211ab 3d 31 20 29 3b 0a 20 20 20 20 20 20 61 64 64 72  =1 );.      addr
211ac 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  2 = sqlite3VdbeA
211ad 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 73 4e 75  ddOp1(v, OP_IsNu
211ae 6c 6c 2c 20 72 65 67 52 65 73 75 6c 74 29 3b 0a  ll, regResult);.
211af 20 20 20 20 20 20 70 2d 3e 61 66 66 69 6e 69 74        p->affinit
211b0 79 20 3d 20 73 71 6c 69 74 65 33 43 6f 6d 70 61  y = sqlite3Compa
211b1 72 65 41 66 66 69 6e 69 74 79 28 70 45 4c 69 73  reAffinity(pELis
211b2 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c 20 70  t->a[0].pExpr, p
211b3 44 65 73 74 2d 3e 61 66 66 69 6e 69 74 79 29 3b  Dest->affinity);
211b4 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72 64 65  .      if( pOrde
211b5 72 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  rBy ){.        /
211b6 2a 20 41 74 20 66 69 72 73 74 20 67 6c 61 6e 63  * At first glanc
211b7 65 20 79 6f 75 20 77 6f 75 6c 64 20 74 68 69 6e  e you would thin
211b8 6b 20 77 65 20 63 6f 75 6c 64 20 6f 70 74 69 6d  k we could optim
211b9 69 7a 65 20 6f 75 74 20 74 68 65 0a 20 20 20 20  ize out the.    
211ba 20 20 20 20 2a 2a 20 4f 52 44 45 52 20 42 59 20      ** ORDER BY 
211bb 69 6e 20 74 68 69 73 20 63 61 73 65 20 73 69 6e  in this case sin
211bc 63 65 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20  ce the order of 
211bd 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 73  entries in the s
211be 65 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 6f  et.        ** do
211bf 65 73 20 6e 6f 74 20 6d 61 74 74 65 72 2e 20 20  es not matter.  
211c0 42 75 74 20 74 68 65 72 65 20 6d 69 67 68 74 20  But there might 
211c1 62 65 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 73  be a LIMIT claus
211c2 65 2c 20 69 6e 20 77 68 69 63 68 0a 20 20 20 20  e, in which.    
211c3 20 20 20 20 2a 2a 20 63 61 73 65 20 74 68 65 20      ** case the 
211c4 6f 72 64 65 72 20 64 6f 65 73 20 6d 61 74 74 65  order does matte
211c5 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 75 73  r */.        pus
211c6 68 4f 6e 74 6f 53 6f 72 74 65 72 28 70 50 61 72  hOntoSorter(pPar
211c7 73 65 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 2c  se, pOrderBy, p,
211c8 20 72 65 67 52 65 73 75 6c 74 29 3b 0a 20 20 20   regResult);.   
211c9 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
211ca 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74    int r1 = sqlit
211cb 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
211cc 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71  rse);.        sq
211cd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
211ce 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  v, OP_MakeRecord
211cf 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 31 2c 20  , regResult, 1, 
211d0 72 31 2c 20 26 70 2d 3e 61 66 66 69 6e 69 74 79  r1, &p->affinity
211d1 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 1);.        sq
211d2 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66  lite3ExprCacheAf
211d3 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61  finityChange(pPa
211d4 72 73 65 2c 20 72 65 67 52 65 73 75 6c 74 2c 20  rse, regResult, 
211d5 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  1);.        sqli
211d6 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
211d7 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69   OP_IdxInsert, i
211d8 50 61 72 6d 2c 20 72 31 29 3b 0a 20 20 20 20 20  Parm, r1);.     
211d9 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
211da 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
211db 20 72 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20   r1);.      }.  
211dc 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
211dd 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 32  umpHere(v, addr2
211de 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
211df 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66      }..    /* If
211e0 20 61 6e 79 20 72 6f 77 20 65 78 69 73 74 20 69   any row exist i
211e1 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
211e2 2c 20 72 65 63 6f 72 64 20 74 68 61 74 20 66 61  , record that fa
211e3 63 74 20 61 6e 64 20 61 62 6f 72 74 2e 0a 20 20  ct and abort..  
211e4 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52    */.    case SR
211e5 54 5f 45 78 69 73 74 73 3a 20 7b 0a 20 20 20 20  T_Exists: {.    
211e6 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
211e7 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
211e8 72 2c 20 31 2c 20 69 50 61 72 6d 29 3b 0a 20 20  r, 1, iParm);.  
211e9 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54      /* The LIMIT
211ea 20 63 6c 61 75 73 65 20 77 69 6c 6c 20 74 65 72   clause will ter
211eb 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 20  minate the loop 
211ec 66 6f 72 20 75 73 20 2a 2f 0a 20 20 20 20 20 20  for us */.      
211ed 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
211ee 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
211ef 61 20 73 63 61 6c 61 72 20 73 65 6c 65 63 74 20  a scalar select 
211f0 74 68 61 74 20 69 73 20 70 61 72 74 20 6f 66 20  that is part of 
211f1 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 74  an expression, t
211f2 68 65 6e 0a 20 20 20 20 2a 2a 20 73 74 6f 72 65  hen.    ** store
211f3 20 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20   the results in 
211f4 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
211f5 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e 64 20  memory cell and 
211f6 62 72 65 61 6b 20 6f 75 74 0a 20 20 20 20 2a 2a  break out.    **
211f7 20 6f 66 20 74 68 65 20 73 63 61 6e 20 6c 6f 6f   of the scan loo
211f8 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  p..    */.    ca
211f9 73 65 20 53 52 54 5f 4d 65 6d 3a 20 7b 0a 20 20  se SRT_Mem: {.  
211fa 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 6f 6c      assert( nCol
211fb 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20  umn==1 );.      
211fc 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a  if( pOrderBy ){.
211fd 20 20 20 20 20 20 20 20 70 75 73 68 4f 6e 74 6f          pushOnto
211fe 53 6f 72 74 65 72 28 70 50 61 72 73 65 2c 20 70  Sorter(pParse, p
211ff 4f 72 64 65 72 42 79 2c 20 70 2c 20 72 65 67 52  OrderBy, p, regR
21200 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 7d 65  esult);.      }e
21201 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
21202 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65  ite3ExprCodeMove
21203 28 70 50 61 72 73 65 2c 20 72 65 67 52 65 73 75  (pParse, regResu
21204 6c 74 2c 20 69 50 61 72 6d 29 3b 0a 20 20 20 20  lt, iParm);.    
21205 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54      /* The LIMIT
21206 20 63 6c 61 75 73 65 20 77 69 6c 6c 20 6a 75 6d   clause will jum
21207 70 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f 6f  p out of the loo
21208 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20 20 20  p for us */.    
21209 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
2120a 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  .    }.#endif /*
2120b 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f   #ifndef SQLITE_
2120c 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f  OMIT_SUBQUERY */
2120d 0a 0a 20 20 20 20 2f 2a 20 53 65 6e 64 20 74 68  ..    /* Send th
2120e 65 20 64 61 74 61 20 74 6f 20 74 68 65 20 63 61  e data to the ca
2120f 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
21210 6f 72 20 74 6f 20 61 20 73 75 62 72 6f 75 74 69  or to a subrouti
21211 6e 65 2e 20 20 49 6e 20 74 68 65 0a 20 20 20 20  ne.  In the.    
21212 2a 2a 20 63 61 73 65 20 6f 66 20 61 20 73 75 62  ** case of a sub
21213 72 6f 75 74 69 6e 65 2c 20 74 68 65 20 73 75 62  routine, the sub
21214 72 6f 75 74 69 6e 65 20 69 74 73 65 6c 66 20 69  routine itself i
21215 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f  s responsible fo
21216 72 0a 20 20 20 20 2a 2a 20 70 6f 70 70 69 6e 67  r.    ** popping
21217 20 74 68 65 20 64 61 74 61 20 66 72 6f 6d 20 74   the data from t
21218 68 65 20 73 74 61 63 6b 2e 0a 20 20 20 20 2a 2f  he stack..    */
21219 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 53 75  .    case SRT_Su
2121a 62 72 6f 75 74 69 6e 65 3a 0a 20 20 20 20 63 61  broutine:.    ca
2121b 73 65 20 53 52 54 5f 43 61 6c 6c 62 61 63 6b 3a  se SRT_Callback:
2121c 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72   {.      if( pOr
2121d 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20 20  derBy ){.       
2121e 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65   int r1 = sqlite
2121f 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
21220 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  se);.        sql
21221 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
21222 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c  , OP_MakeRecord,
21223 20 72 65 67 52 65 73 75 6c 74 2c 20 6e 43 6f 6c   regResult, nCol
21224 75 6d 6e 2c 20 72 31 29 3b 0a 20 20 20 20 20 20  umn, r1);.      
21225 20 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65 72    pushOntoSorter
21226 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42  (pParse, pOrderB
21227 79 2c 20 70 2c 20 72 31 29 3b 0a 20 20 20 20 20  y, p, r1);.     
21228 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
21229 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2122a 20 72 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73   r1);.      }els
2122b 65 20 69 66 28 20 65 44 65 73 74 3d 3d 53 52 54  e if( eDest==SRT
2122c 5f 53 75 62 72 6f 75 74 69 6e 65 20 29 7b 0a 20  _Subroutine ){. 
2122d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2122e 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
2122f 6f 73 75 62 2c 20 30 2c 20 69 50 61 72 6d 29 3b  osub, 0, iParm);
21230 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
21231 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21232 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
21233 73 75 6c 74 52 6f 77 2c 20 72 65 67 52 65 73 75  sultRow, regResu
21234 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20  lt, nColumn);.  
21235 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
21236 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68  rCacheAffinityCh
21237 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67  ange(pParse, reg
21238 52 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 29  Result, nColumn)
21239 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2123a 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69  break;.    }..#i
2123b 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
2123c 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a  E_OMIT_TRIGGER).
2123d 20 20 20 20 2f 2a 20 44 69 73 63 61 72 64 20 74      /* Discard t
2123e 68 65 20 72 65 73 75 6c 74 73 2e 20 20 54 68 69  he results.  Thi
2123f 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 53 45  s is used for SE
21240 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20  LECT statements 
21241 69 6e 73 69 64 65 0a 20 20 20 20 2a 2a 20 74 68  inside.    ** th
21242 65 20 62 6f 64 79 20 6f 66 20 61 20 54 52 49 47  e body of a TRIG
21243 47 45 52 2e 20 20 54 68 65 20 70 75 72 70 6f 73  GER.  The purpos
21244 65 20 6f 66 20 73 75 63 68 20 73 65 6c 65 63 74  e of such select
21245 73 20 69 73 20 74 6f 20 63 61 6c 6c 0a 20 20 20  s is to call.   
21246 20 2a 2a 20 75 73 65 72 2d 64 65 66 69 6e 65 64   ** user-defined
21247 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20   functions that 
21248 68 61 76 65 20 73 69 64 65 20 65 66 66 65 63 74  have side effect
21249 73 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 63 61  s.  We do not ca
2124a 72 65 0a 20 20 20 20 2a 2a 20 61 62 6f 75 74 20  re.    ** about 
2124b 74 68 65 20 61 63 74 75 61 6c 20 72 65 73 75 6c  the actual resul
2124c 74 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74  ts of the select
2124d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 65 66  ..    */.    def
2124e 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73  ault: {.      as
2124f 73 65 72 74 28 20 65 44 65 73 74 3d 3d 53 52 54  sert( eDest==SRT
21250 5f 44 69 73 63 61 72 64 20 29 3b 0a 20 20 20 20  _Discard );.    
21251 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
21252 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20  endif.  }..  /* 
21253 4a 75 6d 70 20 74 6f 20 74 68 65 20 65 6e 64 20  Jump to the end 
21254 6f 66 20 74 68 65 20 6c 6f 6f 70 20 69 66 20 74  of the loop if t
21255 68 65 20 4c 49 4d 49 54 20 69 73 20 72 65 61 63  he LIMIT is reac
21256 68 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  hed..  */.  if( 
21257 70 2d 3e 69 4c 69 6d 69 74 3e 3d 30 20 26 26 20  p->iLimit>=0 && 
21258 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 7b 0a 20  pOrderBy==0 ){. 
21259 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2125a 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d  dOp2(v, OP_AddIm
2125b 6d 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c 20 2d 31  m, p->iLimit, -1
2125c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2125d 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2125e 66 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d 69 74  fZero, p->iLimit
2125f 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 7d 0a 7d  , iBreak);.  }.}
21260 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 6e  ../*.** Given an
21261 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
21262 2c 20 67 65 6e 65 72 61 74 65 20 61 20 4b 65 79  , generate a Key
21263 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74  Info structure t
21264 68 61 74 20 72 65 63 6f 72 64 73 0a 2a 2a 20 74  hat records.** t
21265 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  he collating seq
21266 75 65 6e 63 65 20 66 6f 72 20 65 61 63 68 20 65  uence for each e
21267 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 61  xpression in tha
21268 74 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  t expression lis
21269 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  t..**.** If the 
2126a 45 78 70 72 4c 69 73 74 20 69 73 20 61 6e 20 4f  ExprList is an O
2126b 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50  RDER BY or GROUP
2126c 20 42 59 20 63 6c 61 75 73 65 20 74 68 65 6e 20   BY clause then 
2126d 74 68 65 20 72 65 73 75 6c 74 69 6e 67 0a 2a 2a  the resulting.**
2126e 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75   KeyInfo structu
2126f 72 65 20 69 73 20 61 70 70 72 6f 70 72 69 61 74  re is appropriat
21270 65 20 66 6f 72 20 69 6e 69 74 69 61 6c 69 7a 69  e for initializi
21271 6e 67 20 61 20 76 69 72 74 75 61 6c 20 69 6e 64  ng a virtual ind
21272 65 78 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65  ex to.** impleme
21273 6e 74 20 74 68 61 74 20 63 6c 61 75 73 65 2e 20  nt that clause. 
21274 20 49 66 20 74 68 65 20 45 78 70 72 4c 69 73 74   If the ExprList
21275 20 69 73 20 74 68 65 20 72 65 73 75 6c 74 20 73   is the result s
21276 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 0a 2a  et of a SELECT.*
21277 2a 20 74 68 65 6e 20 74 68 65 20 4b 65 79 49 6e  * then the KeyIn
21278 66 6f 20 73 74 72 75 63 74 75 72 65 20 69 73 20  fo structure is 
21279 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20  appropriate for 
2127a 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 61 20 76  initializing a v
2127b 69 72 74 75 61 6c 0a 2a 2a 20 69 6e 64 65 78 20  irtual.** index 
2127c 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 44  to implement a D
2127d 49 53 54 49 4e 43 54 20 74 65 73 74 2e 0a 2a 2a  ISTINCT test..**
2127e 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c  .** Space to hol
2127f 64 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74  d the KeyInfo st
21280 72 75 63 74 75 72 65 20 69 73 20 6f 62 74 61 69  ructure is obtai
21281 6e 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 20 20  n from malloc.  
21282 54 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66  The calling.** f
21283 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 73 70 6f  unction is respo
21284 6e 73 69 62 6c 65 20 66 6f 72 20 73 65 65 69 6e  nsible for seein
21285 67 20 74 68 61 74 20 74 68 69 73 20 73 74 72 75  g that this stru
21286 63 74 75 72 65 20 69 73 20 65 76 65 6e 74 75 61  cture is eventua
21287 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 2e 20 20 41  lly.** freed.  A
21288 64 64 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73  dd the KeyInfo s
21289 74 72 75 63 74 75 72 65 20 74 6f 20 74 68 65 20  tructure to the 
2128a 50 34 20 66 69 65 6c 64 20 6f 66 20 61 6e 20 6f  P4 field of an o
2128b 70 63 6f 64 65 20 75 73 69 6e 67 0a 2a 2a 20 50  pcode using.** P
2128c 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46  4_KEYINFO_HANDOF
2128d 46 20 69 73 20 74 68 65 20 75 73 75 61 6c 20 77  F is the usual w
2128e 61 79 20 6f 66 20 64 65 61 6c 69 6e 67 20 77 69  ay of dealing wi
2128f 74 68 20 74 68 69 73 2e 0a 2a 2f 0a 73 74 61 74  th this..*/.stat
21290 69 63 20 4b 65 79 49 6e 66 6f 20 2a 6b 65 79 49  ic KeyInfo *keyI
21291 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28  nfoFromExprList(
21292 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
21293 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b  xprList *pList){
21294 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
21295 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
21296 6e 74 20 6e 45 78 70 72 3b 0a 20 20 4b 65 79 49  nt nExpr;.  KeyI
21297 6e 66 6f 20 2a 70 49 6e 66 6f 3b 0a 20 20 73 74  nfo *pInfo;.  st
21298 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
21299 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74  em *pItem;.  int
2129a 20 69 3b 0a 0a 20 20 6e 45 78 70 72 20 3d 20 70   i;..  nExpr = p
2129b 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70  List->nExpr;.  p
2129c 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62  Info = sqlite3Db
2129d 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
2129e 69 7a 65 6f 66 28 2a 70 49 6e 66 6f 29 20 2b 20  izeof(*pInfo) + 
2129f 6e 45 78 70 72 2a 28 73 69 7a 65 6f 66 28 43 6f  nExpr*(sizeof(Co
212a0 6c 6c 53 65 71 2a 29 2b 31 29 20 29 3b 0a 20 20  llSeq*)+1) );.  
212a1 69 66 28 20 70 49 6e 66 6f 20 29 7b 0a 20 20 20  if( pInfo ){.   
212a2 20 70 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64   pInfo->aSortOrd
212a3 65 72 20 3d 20 28 75 38 2a 29 26 70 49 6e 66 6f  er = (u8*)&pInfo
212a4 2d 3e 61 43 6f 6c 6c 5b 6e 45 78 70 72 5d 3b 0a  ->aColl[nExpr];.
212a5 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 46 69 65 6c      pInfo->nFiel
212a6 64 20 3d 20 6e 45 78 70 72 3b 0a 20 20 20 20 70  d = nExpr;.    p
212a7 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28  Info->enc = ENC(
212a8 64 62 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  db);.    for(i=0
212a9 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61  , pItem=pList->a
212aa 3b 20 69 3c 6e 45 78 70 72 3b 20 69 2b 2b 2c 20  ; i<nExpr; i++, 
212ab 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pItem++){.      
212ac 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a  CollSeq *pColl;.
212ad 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71        pColl = sq
212ae 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71  lite3ExprCollSeq
212af 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e  (pParse, pItem->
212b0 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 69 66  pExpr);.      if
212b1 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  ( !pColl ){.    
212b2 20 20 20 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e      pColl = db->
212b3 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 20 20 20  pDfltColl;.     
212b4 20 7d 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e   }.      pInfo->
212b5 61 43 6f 6c 6c 5b 69 5d 20 3d 20 70 43 6f 6c 6c  aColl[i] = pColl
212b6 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61  ;.      pInfo->a
212b7 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 70  SortOrder[i] = p
212b8 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3b  Item->sortOrder;
212b9 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
212ba 75 72 6e 20 70 49 6e 66 6f 3b 0a 7d 0a 0a 0a 2f  urn pInfo;.}.../
212bb 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 6e 65  *.** If the inne
212bc 72 20 6c 6f 6f 70 20 77 61 73 20 67 65 6e 65 72  r loop was gener
212bd 61 74 65 64 20 75 73 69 6e 67 20 61 20 6e 6f 6e  ated using a non
212be 2d 6e 75 6c 6c 20 70 4f 72 64 65 72 42 79 20 61  -null pOrderBy a
212bf 72 67 75 6d 65 6e 74 2c 0a 2a 2a 20 74 68 65 6e  rgument,.** then
212c0 20 74 68 65 20 72 65 73 75 6c 74 73 20 77 65 72   the results wer
212c1 65 20 70 6c 61 63 65 64 20 69 6e 20 61 20 73 6f  e placed in a so
212c2 72 74 65 72 2e 20 20 41 66 74 65 72 20 74 68 65  rter.  After the
212c3 20 6c 6f 6f 70 20 69 73 20 74 65 72 6d 69 6e 61   loop is termina
212c4 74 65 64 0a 2a 2a 20 77 65 20 6e 65 65 64 20 74  ted.** we need t
212c5 6f 20 72 75 6e 20 74 68 65 20 73 6f 72 74 65 72  o run the sorter
212c6 20 61 6e 64 20 6f 75 74 70 75 74 20 74 68 65 20   and output the 
212c7 72 65 73 75 6c 74 73 2e 20 20 54 68 65 20 66 6f  results.  The fo
212c8 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 72 6f 75 74 69  llowing.** routi
212c9 6e 65 20 67 65 6e 65 72 61 74 65 73 20 74 68 65  ne generates the
212ca 20 63 6f 64 65 20 6e 65 65 64 65 64 20 74 6f 20   code needed to 
212cb 64 6f 20 74 68 61 74 2e 0a 2a 2f 0a 73 74 61 74  do that..*/.stat
212cc 69 63 20 76 6f 69 64 20 67 65 6e 65 72 61 74 65  ic void generate
212cd 53 6f 72 74 54 61 69 6c 28 0a 20 20 50 61 72 73  SortTail(.  Pars
212ce 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a  e *pParse,    /*
212cf 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
212d0 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c   */.  Select *p,
212d1 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53          /* The S
212d2 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
212d3 2a 2f 0a 20 20 56 64 62 65 20 2a 76 2c 20 20 20  */.  Vdbe *v,   
212d4 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61         /* Genera
212d5 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 69  te code into thi
212d6 73 20 56 44 42 45 20 2a 2f 0a 20 20 69 6e 74 20  s VDBE */.  int 
212d7 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 2f 2a  nColumn,      /*
212d8 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   Number of colum
212d9 6e 73 20 6f 66 20 64 61 74 61 20 2a 2f 0a 20 20  ns of data */.  
212da 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73  SelectDest *pDes
212db 74 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 73  t /* Write the s
212dc 6f 72 74 65 64 20 72 65 73 75 6c 74 73 20 68 65  orted results he
212dd 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 62  re */.){.  int b
212de 72 6b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rk = sqlite3Vdbe
212df 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20  MakeLabel(v);.  
212e0 69 6e 74 20 63 6f 6e 74 20 3d 20 73 71 6c 69 74  int cont = sqlit
212e1 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
212e2 76 29 3b 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a  v);.  int addr;.
212e3 20 20 69 6e 74 20 69 54 61 62 3b 0a 20 20 69 6e    int iTab;.  in
212e4 74 20 70 73 65 75 64 6f 54 61 62 20 3d 20 30 3b  t pseudoTab = 0;
212e5 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72  .  ExprList *pOr
212e6 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65  derBy = p->pOrde
212e7 72 42 79 3b 0a 0a 20 20 69 6e 74 20 65 44 65 73  rBy;..  int eDes
212e8 74 20 3d 20 70 44 65 73 74 2d 3e 65 44 65 73 74  t = pDest->eDest
212e9 3b 0a 20 20 69 6e 74 20 69 50 61 72 6d 20 3d 20  ;.  int iParm = 
212ea 70 44 65 73 74 2d 3e 69 50 61 72 6d 3b 0a 0a 20  pDest->iParm;.. 
212eb 20 69 6e 74 20 72 65 67 52 6f 77 3b 0a 20 20 69   int regRow;.  i
212ec 6e 74 20 72 65 67 52 6f 77 69 64 3b 0a 0a 20 20  nt regRowid;..  
212ed 69 54 61 62 20 3d 20 70 4f 72 64 65 72 42 79 2d  iTab = pOrderBy-
212ee 3e 69 45 43 75 72 73 6f 72 3b 0a 20 20 69 66 28  >iECursor;.  if(
212ef 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 61 6c 6c   eDest==SRT_Call
212f0 62 61 63 6b 20 7c 7c 20 65 44 65 73 74 3d 3d 53  back || eDest==S
212f1 52 54 5f 53 75 62 72 6f 75 74 69 6e 65 20 29 7b  RT_Subroutine ){
212f2 0a 20 20 20 20 70 73 65 75 64 6f 54 61 62 20 3d  .    pseudoTab =
212f3 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
212f4 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
212f5 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 74  AddOp2(v, OP_Set
212f6 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 6e  NumColumns, 0, n
212f7 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 73 71 6c  Column);.    sql
212f8 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
212f9 2c 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 2c  , OP_OpenPseudo,
212fa 20 70 73 65 75 64 6f 54 61 62 2c 20 65 44 65 73   pseudoTab, eDes
212fb 74 3d 3d 53 52 54 5f 43 61 6c 6c 62 61 63 6b 29  t==SRT_Callback)
212fc 3b 0a 20 20 7d 0a 20 20 61 64 64 72 20 3d 20 31  ;.  }.  addr = 1
212fd 20 2b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   + sqlite3VdbeAd
212fe 64 4f 70 32 28 76 2c 20 4f 50 5f 53 6f 72 74 2c  dOp2(v, OP_Sort,
212ff 20 69 54 61 62 2c 20 62 72 6b 29 3b 0a 20 20 63   iTab, brk);.  c
21300 6f 64 65 4f 66 66 73 65 74 28 76 2c 20 70 2c 20  odeOffset(v, p, 
21301 63 6f 6e 74 29 3b 0a 20 20 72 65 67 52 6f 77 20  cont);.  regRow 
21302 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
21303 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 72  Reg(pParse);.  r
21304 65 67 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65  egRowid = sqlite
21305 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
21306 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  se);.  sqlite3Vd
21307 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
21308 6f 6c 75 6d 6e 2c 20 69 54 61 62 2c 20 70 4f 72  olumn, iTab, pOr
21309 64 65 72 42 79 2d 3e 6e 45 78 70 72 20 2b 20 31  derBy->nExpr + 1
2130a 2c 20 72 65 67 52 6f 77 29 3b 0a 20 20 73 77 69  , regRow);.  swi
2130b 74 63 68 28 20 65 44 65 73 74 20 29 7b 0a 20 20  tch( eDest ){.  
2130c 20 20 63 61 73 65 20 53 52 54 5f 54 61 62 6c 65    case SRT_Table
2130d 3a 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 45  :.    case SRT_E
2130e 70 68 65 6d 54 61 62 3a 20 7b 0a 20 20 20 20 20  phemTab: {.     
2130f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21310 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69  p2(v, OP_NewRowi
21311 64 2c 20 69 50 61 72 6d 2c 20 72 65 67 52 6f 77  d, iParm, regRow
21312 69 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  id);.      sqlit
21313 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
21314 4f 50 5f 49 6e 73 65 72 74 2c 20 69 50 61 72 6d  OP_Insert, iParm
21315 2c 20 72 65 67 52 6f 77 2c 20 72 65 67 52 6f 77  , regRow, regRow
21316 69 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  id);.      sqlit
21317 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
21318 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29  , OPFLAG_APPEND)
21319 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
2131a 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
2131b 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
2131c 59 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 53  Y.    case SRT_S
2131d 65 74 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  et: {.      int 
2131e 6a 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  j1;.      assert
2131f 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a  ( nColumn==1 );.
21320 20 20 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74        j1 = sqlit
21321 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
21322 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 52 6f  OP_IsNull, regRo
21323 77 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  w);.      sqlite
21324 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
21325 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65  P_MakeRecord, re
21326 67 52 6f 77 2c 20 31 2c 20 72 65 67 52 6f 77 69  gRow, 1, regRowi
21327 64 2c 20 26 70 2d 3e 61 66 66 69 6e 69 74 79 2c  d, &p->affinity,
21328 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   1);.      sqlit
21329 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e  e3ExprCacheAffin
2132a 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65  ityChange(pParse
2132b 2c 20 72 65 67 52 6f 77 2c 20 31 29 3b 0a 20 20  , regRow, 1);.  
2132c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2132d 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49  ddOp2(v, OP_IdxI
2132e 6e 73 65 72 74 2c 20 69 50 61 72 6d 2c 20 72 65  nsert, iParm, re
2132f 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 73  gRowid);.      s
21330 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
21331 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 20  re(v, j1);.     
21332 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
21333 20 20 63 61 73 65 20 53 52 54 5f 4d 65 6d 3a 20    case SRT_Mem: 
21334 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
21335 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20  nColumn==1 );.  
21336 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
21337 6f 64 65 4d 6f 76 65 28 70 50 61 72 73 65 2c 20  odeMove(pParse, 
21338 72 65 67 52 6f 77 2c 20 69 50 61 72 6d 29 3b 0a  regRow, iParm);.
21339 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d        /* The LIM
2133a 49 54 20 63 6c 61 75 73 65 20 77 69 6c 6c 20 74  IT clause will t
2133b 65 72 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f  erminate the loo
2133c 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20 20 20  p for us */.    
2133d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
2133e 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 53  endif.    case S
2133f 52 54 5f 43 61 6c 6c 62 61 63 6b 3a 0a 20 20 20  RT_Callback:.   
21340 20 63 61 73 65 20 53 52 54 5f 53 75 62 72 6f 75   case SRT_Subrou
21341 74 69 6e 65 3a 20 7b 0a 20 20 20 20 20 20 69 6e  tine: {.      in
21342 74 20 69 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  t i;.      sqlit
21343 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
21344 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 72  OP_Integer, 1, r
21345 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20  egRowid);.      
21346 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21347 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20  3(v, OP_Insert, 
21348 70 73 65 75 64 6f 54 61 62 2c 20 72 65 67 52 6f  pseudoTab, regRo
21349 77 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  w, regRowid);.  
2134a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
2134b 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
2134c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 65        assert( re
2134d 67 52 6f 77 21 3d 70 44 65 73 74 2d 3e 69 4d 65  gRow!=pDest->iMe
2134e 6d 2b 69 20 29 3b 0a 20 20 20 20 20 20 20 20 73  m+i );.        s
2134f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
21350 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 70  (v, OP_Column, p
21351 73 65 75 64 6f 54 61 62 2c 20 69 2c 20 70 44 65  seudoTab, i, pDe
21352 73 74 2d 3e 69 4d 65 6d 2b 69 29 3b 0a 20 20 20  st->iMem+i);.   
21353 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 65     }.      if( e
21354 44 65 73 74 3d 3d 53 52 54 5f 43 61 6c 6c 62 61  Dest==SRT_Callba
21355 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ck ){.        sq
21356 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
21357 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
21358 20 70 44 65 73 74 2d 3e 69 4d 65 6d 2c 20 6e 43   pDest->iMem, nC
21359 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 20 20  olumn);.        
2135a 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
2135b 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70  AffinityChange(p
2135c 50 61 72 73 65 2c 20 70 44 65 73 74 2d 3e 69 4d  Parse, pDest->iM
2135d 65 6d 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20  em, nColumn);.  
2135e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2135f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21360 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62  dOp2(v, OP_Gosub
21361 2c 20 30 2c 20 69 50 61 72 6d 29 3b 0a 20 20 20  , 0, iParm);.   
21362 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
21363 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
21364 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  ult: {.      /* 
21365 44 6f 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  Do nothing */.  
21366 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
21367 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65  .  }.  sqlite3Re
21368 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
21369 72 73 65 2c 20 72 65 67 52 6f 77 29 3b 0a 20 20  rse, regRow);.  
2136a 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2136b 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65  mpReg(pParse, re
2136c 67 52 6f 77 69 64 29 3b 0a 0a 20 20 2f 2a 20 4a  gRowid);..  /* J
2136d 75 6d 70 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  ump to the end o
2136e 66 20 74 68 65 20 6c 6f 6f 70 20 77 68 65 6e 20  f the loop when 
2136f 74 68 65 20 4c 49 4d 49 54 20 69 73 20 72 65 61  the LIMIT is rea
21370 63 68 65 64 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ched.  */.  if( 
21371 70 2d 3e 69 4c 69 6d 69 74 3e 3d 30 20 29 7b 0a  p->iLimit>=0 ){.
21372 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21373 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
21374 6d 6d 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c 20 2d  mm, p->iLimit, -
21375 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
21376 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
21377 49 66 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d 69  IfZero, p->iLimi
21378 74 2c 20 62 72 6b 29 3b 0a 20 20 7d 0a 0a 20 20  t, brk);.  }..  
21379 2f 2a 20 54 68 65 20 62 6f 74 74 6f 6d 20 6f 66  /* The bottom of
2137a 20 74 68 65 20 6c 6f 6f 70 0a 20 20 2a 2f 0a 20   the loop.  */. 
2137b 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
2137c 6c 76 65 4c 61 62 65 6c 28 76 2c 20 63 6f 6e 74  lveLabel(v, cont
2137d 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
2137e 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78  AddOp2(v, OP_Nex
2137f 74 2c 20 69 54 61 62 2c 20 61 64 64 72 29 3b 0a  t, iTab, addr);.
21380 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
21381 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 62 72 6b  olveLabel(v, brk
21382 29 3b 0a 20 20 69 66 28 20 65 44 65 73 74 3d 3d  );.  if( eDest==
21383 53 52 54 5f 43 61 6c 6c 62 61 63 6b 20 7c 7c 20  SRT_Callback || 
21384 65 44 65 73 74 3d 3d 53 52 54 5f 53 75 62 72 6f  eDest==SRT_Subro
21385 75 74 69 6e 65 20 29 7b 0a 20 20 20 20 73 71 6c  utine ){.    sql
21386 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
21387 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 70 73 65 75  , OP_Close, pseu
21388 64 6f 54 61 62 2c 20 30 29 3b 0a 20 20 7d 0a 0a  doTab, 0);.  }..
21389 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
2138a 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73  a pointer to a s
2138b 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67  tring containing
2138c 20 74 68 65 20 27 64 65 63 6c 61 72 61 74 69 6f   the 'declaratio
2138d 6e 20 74 79 70 65 27 20 6f 66 20 74 68 65 0a 2a  n type' of the.*
2138e 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78  * expression pEx
2138f 70 72 2e 20 54 68 65 20 73 74 72 69 6e 67 20 6d  pr. The string m
21390 61 79 20 62 65 20 74 72 65 61 74 65 64 20 61 73  ay be treated as
21391 20 73 74 61 74 69 63 20 62 79 20 74 68 65 20 63   static by the c
21392 61 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  aller..**.** The
21393 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70   declaration typ
21394 65 20 69 73 20 74 68 65 20 65 78 61 63 74 20 64  e is the exact d
21395 61 74 61 74 79 70 65 20 64 65 66 69 6e 69 74 69  atatype definiti
21396 6f 6e 20 65 78 74 72 61 63 74 65 64 20 66 72 6f  on extracted fro
21397 6d 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61  m the.** origina
21398 6c 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  l CREATE TABLE s
21399 74 61 74 65 6d 65 6e 74 20 69 66 20 74 68 65 20  tatement if the 
2139a 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20  expression is a 
2139b 63 6f 6c 75 6d 6e 2e 20 54 68 65 0a 2a 2a 20 64  column. The.** d
2139c 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20  eclaration type 
2139d 66 6f 72 20 61 20 52 4f 57 49 44 20 66 69 65 6c  for a ROWID fiel
2139e 64 20 69 73 20 49 4e 54 45 47 45 52 2e 20 45 78  d is INTEGER. Ex
2139f 61 63 74 6c 79 20 77 68 65 6e 20 61 6e 20 65 78  actly when an ex
213a0 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 63  pression.** is c
213a1 6f 6e 73 69 64 65 72 65 64 20 61 20 63 6f 6c 75  onsidered a colu
213a2 6d 6e 20 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65  mn can be comple
213a3 78 20 69 6e 20 74 68 65 20 70 72 65 73 65 6e 63  x in the presenc
213a4 65 20 6f 66 20 73 75 62 71 75 65 72 69 65 73 2e  e of subqueries.
213a5 20 54 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2d 73   The.** result-s
213a6 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  et expression in
213a7 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c   all of the foll
213a8 6f 77 69 6e 67 20 53 45 4c 45 43 54 20 73 74 61  owing SELECT sta
213a9 74 65 6d 65 6e 74 73 20 69 73 20 0a 2a 2a 20 63  tements is .** c
213aa 6f 6e 73 69 64 65 72 65 64 20 61 20 63 6f 6c 75  onsidered a colu
213ab 6d 6e 20 62 79 20 74 68 69 73 20 66 75 6e 63 74  mn by this funct
213ac 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c  ion..**.**   SEL
213ad 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62 6c  ECT col FROM tbl
213ae 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 28 53  ;.**   SELECT (S
213af 45 4c 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74  ELECT col FROM t
213b0 62 6c 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20  bl;.**   SELECT 
213b1 28 53 45 4c 45 43 54 20 63 6f 6c 20 46 52 4f 4d  (SELECT col FROM
213b2 20 74 62 6c 29 3b 0a 2a 2a 20 20 20 53 45 4c 45   tbl);.**   SELE
213b3 43 54 20 61 62 63 20 46 52 4f 4d 20 28 53 45 4c  CT abc FROM (SEL
213b4 45 43 54 20 63 6f 6c 20 41 53 20 61 62 63 20 46  ECT col AS abc F
213b5 52 4f 4d 20 74 62 6c 29 3b 0a 2a 2a 20 0a 2a 2a  ROM tbl);.** .**
213b6 20 54 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e   The declaration
213b7 20 74 79 70 65 20 66 6f 72 20 61 6e 79 20 65 78   type for any ex
213b8 70 72 65 73 73 69 6f 6e 20 6f 74 68 65 72 20 74  pression other t
213b9 68 61 6e 20 61 20 63 6f 6c 75 6d 6e 20 69 73 20  han a column is 
213ba 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  NULL..*/.static 
213bb 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6c 75  const char *colu
213bc 6d 6e 54 79 70 65 28 0a 20 20 4e 61 6d 65 43 6f  mnType(.  NameCo
213bd 6e 74 65 78 74 20 2a 70 4e 43 2c 20 0a 20 20 45  ntext *pNC, .  E
213be 78 70 72 20 2a 70 45 78 70 72 2c 0a 20 20 63 6f  xpr *pExpr,.  co
213bf 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 4f 72 69  nst char **pzOri
213c0 67 69 6e 44 62 2c 0a 20 20 63 6f 6e 73 74 20 63  ginDb,.  const c
213c1 68 61 72 20 2a 2a 70 7a 4f 72 69 67 69 6e 54 61  har **pzOriginTa
213c2 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  b,.  const char 
213c3 2a 2a 70 7a 4f 72 69 67 69 6e 43 6f 6c 0a 29 7b  **pzOriginCol.){
213c4 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a  .  char const *z
213c5 54 79 70 65 20 3d 20 30 3b 0a 20 20 63 68 61 72  Type = 0;.  char
213c6 20 63 6f 6e 73 74 20 2a 7a 4f 72 69 67 69 6e 44   const *zOriginD
213c7 62 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 6f  b = 0;.  char co
213c8 6e 73 74 20 2a 7a 4f 72 69 67 69 6e 54 61 62 20  nst *zOriginTab 
213c9 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73  = 0;.  char cons
213ca 74 20 2a 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20  t *zOriginCol = 
213cb 30 3b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 69 66  0;.  int j;.  if
213cc 28 20 70 45 78 70 72 3d 3d 30 20 7c 7c 20 70 4e  ( pExpr==0 || pN
213cd 43 2d 3e 70 53 72 63 4c 69 73 74 3d 3d 30 20 29  C->pSrcList==0 )
213ce 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 73 77   return 0;..  sw
213cf 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20  itch( pExpr->op 
213d0 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41  ){.    case TK_A
213d1 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63  GG_COLUMN:.    c
213d2 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b  ase TK_COLUMN: {
213d3 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78  .      /* The ex
213d4 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 63 6f  pression is a co
213d5 6c 75 6d 6e 2e 20 4c 6f 63 61 74 65 20 74 68 65  lumn. Locate the
213d6 20 74 61 62 6c 65 20 74 68 65 20 63 6f 6c 75 6d   table the colum
213d7 6e 20 69 73 20 62 65 69 6e 67 0a 20 20 20 20 20  n is being.     
213d8 20 2a 2a 20 65 78 74 72 61 63 74 65 64 20 66 72   ** extracted fr
213d9 6f 6d 20 69 6e 20 4e 61 6d 65 43 6f 6e 74 65 78  om in NameContex
213da 74 2e 70 53 72 63 4c 69 73 74 2e 20 54 68 69 73  t.pSrcList. This
213db 20 74 61 62 6c 65 20 6d 61 79 20 62 65 20 72 65   table may be re
213dc 61 6c 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61  al.      ** data
213dd 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 61 20  base table or a 
213de 73 75 62 71 75 65 72 79 2e 0a 20 20 20 20 20 20  subquery..      
213df 2a 2f 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a  */.      Table *
213e0 70 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 20  pTab = 0;       
213e1 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 73 74       /* Table st
213e2 72 75 63 74 75 72 65 20 63 6f 6c 75 6d 6e 20 69  ructure column i
213e3 73 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d  s extracted from
213e4 20 2a 2f 0a 20 20 20 20 20 20 53 65 6c 65 63 74   */.      Select
213e5 20 2a 70 53 20 3d 20 30 3b 20 20 20 20 20 20 20   *pS = 0;       
213e6 20 20 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 20        /* Select 
213e7 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 65 78  the column is ex
213e8 74 72 61 63 74 65 64 20 66 72 6f 6d 20 2a 2f 0a  tracted from */.
213e9 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d        int iCol =
213ea 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b   pExpr->iColumn;
213eb 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f    /* Index of co
213ec 6c 75 6d 6e 20 69 6e 20 70 54 61 62 20 2a 2f 0a  lumn in pTab */.
213ed 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 4e 43        while( pNC
213ee 20 26 26 20 21 70 54 61 62 20 29 7b 0a 20 20 20   && !pTab ){.   
213ef 20 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 54       SrcList *pT
213f0 61 62 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53  abList = pNC->pS
213f1 72 63 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20  rcList;.        
213f2 66 6f 72 28 6a 3d 30 3b 6a 3c 70 54 61 62 4c 69  for(j=0;j<pTabLi
213f3 73 74 2d 3e 6e 53 72 63 20 26 26 20 70 54 61 62  st->nSrc && pTab
213f4 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 69 43 75 72 73  List->a[j].iCurs
213f5 6f 72 21 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c  or!=pExpr->iTabl
213f6 65 3b 6a 2b 2b 29 3b 0a 20 20 20 20 20 20 20 20  e;j++);.        
213f7 69 66 28 20 6a 3c 70 54 61 62 4c 69 73 74 2d 3e  if( j<pTabList->
213f8 6e 53 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20  nSrc ){.        
213f9 20 20 70 54 61 62 20 3d 20 70 54 61 62 4c 69 73    pTab = pTabLis
213fa 74 2d 3e 61 5b 6a 5d 2e 70 54 61 62 3b 0a 20 20  t->a[j].pTab;.  
213fb 20 20 20 20 20 20 20 20 70 53 20 3d 20 70 54 61          pS = pTa
213fc 62 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 53 65 6c  bList->a[j].pSel
213fd 65 63 74 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  ect;.        }el
213fe 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4e  se{.          pN
213ff 43 20 3d 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a  C = pNC->pNext;.
21400 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
21401 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 70 54 61  }..      if( pTa
21402 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  b==0 ){.        
21403 2f 2a 20 46 49 58 20 4d 45 3a 0a 20 20 20 20 20  /* FIX ME:.     
21404 20 20 20 2a 2a 20 54 68 69 73 20 63 61 6e 20 6f     ** This can o
21405 63 63 75 72 73 20 69 66 20 79 6f 75 20 68 61 76  ccurs if you hav
21406 65 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69 6b 65  e something like
21407 20 22 53 45 4c 45 43 54 20 6e 65 77 2e 78 3b 22   "SELECT new.x;"
21408 20 69 6e 73 69 64 65 0a 20 20 20 20 20 20 20 20   inside.        
21409 2a 2a 20 61 20 74 72 69 67 67 65 72 2e 20 20 49  ** a trigger.  I
2140a 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69  n other words, i
2140b 66 20 79 6f 75 20 72 65 66 65 72 65 6e 63 65 20  f you reference 
2140c 74 68 65 20 73 70 65 63 69 61 6c 20 22 6e 65 77  the special "new
2140d 22 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62  ".        ** tab
2140e 6c 65 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  le in the result
2140f 20 73 65 74 20 6f 66 20 61 20 73 65 6c 65 63 74   set of a select
21410 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 68 61 76  .  We do not hav
21411 65 20 61 20 67 6f 6f 64 20 77 61 79 0a 20 20 20  e a good way.   
21412 20 20 20 20 20 2a 2a 20 74 6f 20 66 69 6e 64 20       ** to find 
21413 74 68 65 20 61 63 74 75 61 6c 20 74 61 62 6c 65  the actual table
21414 20 74 79 70 65 2c 20 73 6f 20 63 61 6c 6c 20 69   type, so call i
21415 74 20 22 54 45 58 54 22 2e 20 20 54 68 69 73 20  t "TEXT".  This 
21416 69 73 20 72 65 61 6c 6c 79 0a 20 20 20 20 20 20  is really.      
21417 20 20 2a 2a 20 73 6f 6d 65 74 68 69 6e 67 20 6f    ** something o
21418 66 20 61 20 62 75 67 2c 20 62 75 74 20 49 20 64  f a bug, but I d
21419 6f 20 6e 6f 74 20 6b 6e 6f 77 20 68 6f 77 20 74  o not know how t
2141a 6f 20 66 69 78 20 69 74 2e 0a 20 20 20 20 20 20  o fix it..      
2141b 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
2141c 54 68 69 73 20 63 6f 64 65 20 64 6f 65 73 20 6e  This code does n
2141d 6f 74 20 70 72 6f 64 75 63 65 20 74 68 65 20 63  ot produce the c
2141e 6f 72 72 65 63 74 20 61 6e 73 77 65 72 20 2d 20  orrect answer - 
2141f 69 74 20 6a 75 73 74 20 70 72 65 76 65 6e 74 73  it just prevents
21420 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 73 65  .        ** a se
21421 67 66 61 75 6c 74 2e 20 20 53 65 65 20 74 69 63  gfault.  See tic
21422 6b 65 74 20 23 31 32 32 39 2e 0a 20 20 20 20 20  ket #1229..     
21423 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 7a 54     */.        zT
21424 79 70 65 20 3d 20 22 54 45 58 54 22 3b 0a 20 20  ype = "TEXT";.  
21425 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21426 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65     }..      asse
21427 72 74 28 20 70 54 61 62 20 29 3b 0a 20 20 20 20  rt( pTab );.    
21428 20 20 69 66 28 20 70 53 20 29 7b 0a 20 20 20 20    if( pS ){.    
21429 20 20 20 20 2f 2a 20 54 68 65 20 22 74 61 62 6c      /* The "tabl
2142a 65 22 20 69 73 20 61 63 74 75 61 6c 6c 79 20 61  e" is actually a
2142b 20 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 61   sub-select or a
2142c 20 76 69 65 77 20 69 6e 20 74 68 65 20 46 52 4f   view in the FRO
2142d 4d 20 63 6c 61 75 73 65 0a 20 20 20 20 20 20 20  M clause.       
2142e 20 2a 2a 20 6f 66 20 74 68 65 20 53 45 4c 45 43   ** of the SELEC
2142f 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 52 65 74  T statement. Ret
21430 75 72 6e 20 74 68 65 20 64 65 63 6c 61 72 61 74  urn the declarat
21431 69 6f 6e 20 74 79 70 65 20 61 6e 64 20 6f 72 69  ion type and ori
21432 67 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 64  gin.        ** d
21433 61 74 61 20 66 6f 72 20 74 68 65 20 72 65 73 75  ata for the resu
21434 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 20 6f 66  lt-set column of
21435 20 74 68 65 20 73 75 62 2d 73 65 6c 65 63 74 2e   the sub-select.
21436 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
21437 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20      if( iCol>=0 
21438 26 26 20 69 43 6f 6c 3c 70 53 2d 3e 70 45 4c 69  && iCol<pS->pELi
21439 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20  st->nExpr ){.   
2143a 20 20 20 20 20 20 20 2f 2a 20 49 66 20 69 43 6f         /* If iCo
2143b 6c 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a  l is less than z
2143c 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 65 78  ero, then the ex
2143d 70 72 65 73 73 69 6f 6e 20 72 65 71 75 65 73 74  pression request
2143e 73 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20  s the.          
2143f 2a 2a 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20  ** rowid of the 
21440 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 76 69  sub-select or vi
21441 65 77 2e 20 54 68 69 73 20 65 78 70 72 65 73 73  ew. This express
21442 69 6f 6e 20 69 73 20 6c 65 67 61 6c 20 28 73 65  ion is legal (se
21443 65 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  e .          ** 
21444 74 65 73 74 20 63 61 73 65 20 6d 69 73 63 32 2e  test case misc2.
21445 32 2e 32 29 20 2d 20 69 74 20 61 6c 77 61 79 73  2.2) - it always
21446 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20 4e 55   evaluates to NU
21447 4c 4c 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f  LL..          */
21448 0a 20 20 20 20 20 20 20 20 20 20 4e 61 6d 65 43  .          NameC
21449 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 20 20  ontext sNC;.    
2144a 20 20 20 20 20 20 45 78 70 72 20 2a 70 20 3d 20        Expr *p = 
2144b 70 53 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 69 43  pS->pEList->a[iC
2144c 6f 6c 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  ol].pExpr;.     
2144d 20 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69 73       sNC.pSrcLis
2144e 74 20 3d 20 70 53 2d 3e 70 53 72 63 3b 0a 20 20  t = pS->pSrc;.  
2144f 20 20 20 20 20 20 20 20 73 4e 43 2e 70 4e 65 78          sNC.pNex
21450 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  t = 0;.         
21451 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 4e   sNC.pParse = pN
21452 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 20 20  C->pParse;.     
21453 20 20 20 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c       zType = col
21454 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c  umnType(&sNC, p,
21455 20 26 7a 4f 72 69 67 69 6e 44 62 2c 20 26 7a 4f   &zOriginDb, &zO
21456 72 69 67 69 6e 54 61 62 2c 20 26 7a 4f 72 69 67  riginTab, &zOrig
21457 69 6e 43 6f 6c 29 3b 20 0a 20 20 20 20 20 20 20  inCol); .       
21458 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69   }.      }else i
21459 66 28 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  f( pTab->pSchema
2145a 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41   ){.        /* A
2145b 20 72 65 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20   real table */. 
2145c 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21         assert( !
2145d 70 53 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66  pS );.        if
2145e 28 20 69 43 6f 6c 3c 30 20 29 20 69 43 6f 6c 20  ( iCol<0 ) iCol 
2145f 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20  = pTab->iPKey;. 
21460 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69         assert( i
21461 43 6f 6c 3d 3d 2d 31 20 7c 7c 20 28 69 43 6f 6c  Col==-1 || (iCol
21462 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 54 61 62  >=0 && iCol<pTab
21463 2d 3e 6e 43 6f 6c 29 20 29 3b 0a 20 20 20 20 20  ->nCol) );.     
21464 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b     if( iCol<0 ){
21465 0a 20 20 20 20 20 20 20 20 20 20 7a 54 79 70 65  .          zType
21466 20 3d 20 22 49 4e 54 45 47 45 52 22 3b 0a 20 20   = "INTEGER";.  
21467 20 20 20 20 20 20 20 20 7a 4f 72 69 67 69 6e 43          zOriginC
21468 6f 6c 20 3d 20 22 72 6f 77 69 64 22 3b 0a 20 20  ol = "rowid";.  
21469 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2146a 20 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 70         zType = p
2146b 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e  Tab->aCol[iCol].
2146c 7a 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 20  zType;.         
2146d 20 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 70 54   zOriginCol = pT
2146e 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a  ab->aCol[iCol].z
2146f 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Name;.        }.
21470 20 20 20 20 20 20 20 20 7a 4f 72 69 67 69 6e 54          zOriginT
21471 61 62 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ab = pTab->zName
21472 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4e  ;.        if( pN
21473 43 2d 3e 70 50 61 72 73 65 20 29 7b 0a 20 20 20  C->pParse ){.   
21474 20 20 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d         int iDb =
21475 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
21476 49 6e 64 65 78 28 70 4e 43 2d 3e 70 50 61 72 73  Index(pNC->pPars
21477 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  e->db, pTab->pSc
21478 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 20 20 20  hema);.         
21479 20 7a 4f 72 69 67 69 6e 44 62 20 3d 20 70 4e 43   zOriginDb = pNC
2147a 2d 3e 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44  ->pParse->db->aD
2147b 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  b[iDb].zName;.  
2147c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2147d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2147e 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
2147f 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a  E_OMIT_SUBQUERY.
21480 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45      case TK_SELE
21481 43 54 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54  CT: {.      /* T
21482 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
21483 20 61 20 73 75 62 2d 73 65 6c 65 63 74 2e 20 52   a sub-select. R
21484 65 74 75 72 6e 20 74 68 65 20 64 65 63 6c 61 72  eturn the declar
21485 61 74 69 6f 6e 20 74 79 70 65 20 61 6e 64 0a 20  ation type and. 
21486 20 20 20 20 20 2a 2a 20 6f 72 69 67 69 6e 20 69       ** origin i
21487 6e 66 6f 20 66 6f 72 20 74 68 65 20 73 69 6e 67  nfo for the sing
21488 6c 65 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65  le column in the
21489 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74   result set of t
2148a 68 65 20 53 45 4c 45 43 54 0a 20 20 20 20 20 20  he SELECT.      
2148b 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20  ** statement..  
2148c 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 4e 61 6d      */.      Nam
2148d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20  eContext sNC;.  
2148e 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 20 3d      Select *pS =
2148f 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 3b   pExpr->pSelect;
21490 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 20 3d  .      Expr *p =
21491 20 70 53 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30   pS->pEList->a[0
21492 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 73  ].pExpr;.      s
21493 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 53  NC.pSrcList = pS
21494 2d 3e 70 53 72 63 3b 0a 20 20 20 20 20 20 73 4e  ->pSrc;.      sN
21495 43 2e 70 4e 65 78 74 20 3d 20 70 4e 43 3b 0a 20  C.pNext = pNC;. 
21496 20 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20       sNC.pParse 
21497 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20  = pNC->pParse;. 
21498 20 20 20 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c       zType = col
21499 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c  umnType(&sNC, p,
2149a 20 26 7a 4f 72 69 67 69 6e 44 62 2c 20 26 7a 4f   &zOriginDb, &zO
2149b 72 69 67 69 6e 54 61 62 2c 20 26 7a 4f 72 69 67  riginTab, &zOrig
2149c 69 6e 43 6f 6c 29 3b 20 0a 20 20 20 20 20 20 62  inCol); .      b
2149d 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  reak;.    }.#end
2149e 69 66 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28 20  if.  }.  .  if( 
2149f 70 7a 4f 72 69 67 69 6e 44 62 20 29 7b 0a 20 20  pzOriginDb ){.  
214a0 20 20 61 73 73 65 72 74 28 20 70 7a 4f 72 69 67    assert( pzOrig
214a1 69 6e 54 61 62 20 26 26 20 70 7a 4f 72 69 67 69  inTab && pzOrigi
214a2 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 2a 70 7a 4f  nCol );.    *pzO
214a3 72 69 67 69 6e 44 62 20 3d 20 7a 4f 72 69 67 69  riginDb = zOrigi
214a4 6e 44 62 3b 0a 20 20 20 20 2a 70 7a 4f 72 69 67  nDb;.    *pzOrig
214a5 69 6e 54 61 62 20 3d 20 7a 4f 72 69 67 69 6e 54  inTab = zOriginT
214a6 61 62 3b 0a 20 20 20 20 2a 70 7a 4f 72 69 67 69  ab;.    *pzOrigi
214a7 6e 43 6f 6c 20 3d 20 7a 4f 72 69 67 69 6e 43 6f  nCol = zOriginCo
214a8 6c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  l;.  }.  return 
214a9 7a 54 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  zType;.}../*.** 
214aa 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  Generate code th
214ab 61 74 20 77 69 6c 6c 20 74 65 6c 6c 20 74 68 65  at will tell the
214ac 20 56 44 42 45 20 74 68 65 20 64 65 63 6c 61 72   VDBE the declar
214ad 61 74 69 6f 6e 20 74 79 70 65 73 20 6f 66 20 63  ation types of c
214ae 6f 6c 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74 68 65  olumns.** in the
214af 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a   result set..*/.
214b0 73 74 61 74 69 63 20 76 6f 69 64 20 67 65 6e 65  static void gene
214b1 72 61 74 65 43 6f 6c 75 6d 6e 54 79 70 65 73 28  rateColumnTypes(
214b2 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
214b3 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72  ,      /* Parser
214b4 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72   context */.  Sr
214b5 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c  cList *pTabList,
214b6 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61 62    /* List of tab
214b7 6c 65 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  les */.  ExprLis
214b8 74 20 2a 70 45 4c 69 73 74 20 20 20 20 2f 2a 20  t *pEList    /* 
214b9 45 78 70 72 65 73 73 69 6f 6e 73 20 64 65 66 69  Expressions defi
214ba 6e 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 20  ning the result 
214bb 73 65 74 20 2a 2f 0a 29 7b 0a 23 69 66 6e 64 65  set */.){.#ifnde
214bc 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  f SQLITE_OMIT_DE
214bd 43 4c 54 59 50 45 0a 20 20 56 64 62 65 20 2a 76  CLTYPE.  Vdbe *v
214be 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
214bf 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4e 61 6d  ;.  int i;.  Nam
214c0 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20  eContext sNC;.  
214c1 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70  sNC.pSrcList = p
214c2 54 61 62 4c 69 73 74 3b 0a 20 20 73 4e 43 2e 70  TabList;.  sNC.p
214c3 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a  Parse = pParse;.
214c4 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c    for(i=0; i<pEL
214c5 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  ist->nExpr; i++)
214c6 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 20 3d 20  {.    Expr *p = 
214c7 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78  pEList->a[i].pEx
214c8 70 72 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  pr;.    const ch
214c9 61 72 20 2a 7a 54 79 70 65 3b 0a 23 69 66 64 65  ar *zType;.#ifde
214ca 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
214cb 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a  COLUMN_METADATA.
214cc 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
214cd 7a 4f 72 69 67 44 62 20 3d 20 30 3b 0a 20 20 20  zOrigDb = 0;.   
214ce 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 72   const char *zOr
214cf 69 67 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 63  igTab = 0;.    c
214d0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 72 69 67  onst char *zOrig
214d1 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 7a 54 79  Col = 0;.    zTy
214d2 70 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28  pe = columnType(
214d3 26 73 4e 43 2c 20 70 2c 20 26 7a 4f 72 69 67 44  &sNC, p, &zOrigD
214d4 62 2c 20 26 7a 4f 72 69 67 54 61 62 2c 20 26 7a  b, &zOrigTab, &z
214d5 4f 72 69 67 43 6f 6c 29 3b 0a 0a 20 20 20 20 2f  OrigCol);..    /
214d6 2a 20 54 68 65 20 76 64 62 65 20 6d 75 73 74 20  * The vdbe must 
214d7 6d 61 6b 65 20 69 74 73 20 6f 77 6e 20 63 6f 70  make its own cop
214d8 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2d  y of the column-
214d9 74 79 70 65 20 61 6e 64 20 6f 74 68 65 72 20 0a  type and other .
214da 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 73 70      ** column sp
214db 65 63 69 66 69 63 20 73 74 72 69 6e 67 73 2c 20  ecific strings, 
214dc 69 6e 20 63 61 73 65 20 74 68 65 20 73 63 68 65  in case the sche
214dd 6d 61 20 69 73 20 72 65 73 65 74 20 62 65 66 6f  ma is reset befo
214de 72 65 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 76  re this.    ** v
214df 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 69  irtual machine i
214e0 73 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a  s deleted..    *
214e1 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
214e2 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69  eSetColName(v, i
214e3 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41  , COLNAME_DATABA
214e4 53 45 2c 20 7a 4f 72 69 67 44 62 2c 20 50 34 5f  SE, zOrigDb, P4_
214e5 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20  TRANSIENT);.    
214e6 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
214e7 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e  lName(v, i, COLN
214e8 41 4d 45 5f 54 41 42 4c 45 2c 20 7a 4f 72 69 67  AME_TABLE, zOrig
214e9 54 61 62 2c 20 50 34 5f 54 52 41 4e 53 49 45 4e  Tab, P4_TRANSIEN
214ea 54 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  T);.    sqlite3V
214eb 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
214ec 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55   i, COLNAME_COLU
214ed 4d 4e 2c 20 7a 4f 72 69 67 43 6f 6c 2c 20 50 34  MN, zOrigCol, P4
214ee 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 23 65 6c  _TRANSIENT);.#el
214ef 73 65 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 63  se.    zType = c
214f0 6f 6c 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20  olumnType(&sNC, 
214f1 70 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 23 65 6e  p, 0, 0, 0);.#en
214f2 64 69 66 0a 20 20 20 20 73 71 6c 69 74 65 33 56  dif.    sqlite3V
214f3 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
214f4 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c   i, COLNAME_DECL
214f5 54 59 50 45 2c 20 7a 54 79 70 65 2c 20 50 34 5f  TYPE, zType, P4_
214f6 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a  TRANSIENT);.  }.
214f7 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
214f8 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 20 2a  _OMIT_DECLTYPE *
214f9 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  /.}../*.** Gener
214fa 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69  ate code that wi
214fb 6c 6c 20 74 65 6c 6c 20 74 68 65 20 56 44 42 45  ll tell the VDBE
214fc 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 63 6f   the names of co
214fd 6c 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20  lumns.** in the 
214fe 72 65 73 75 6c 74 20 73 65 74 2e 20 20 54 68 69  result set.  Thi
214ff 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73  s information is
21500 20 75 73 65 64 20 74 6f 20 70 72 6f 76 69 64 65   used to provide
21501 20 74 68 65 0a 2a 2a 20 61 7a 43 6f 6c 5b 5d 20   the.** azCol[] 
21502 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 63 61  values in the ca
21503 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69  llback..*/.stati
21504 63 20 76 6f 69 64 20 67 65 6e 65 72 61 74 65 43  c void generateC
21505 6f 6c 75 6d 6e 4e 61 6d 65 73 28 0a 20 20 50 61  olumnNames(.  Pa
21506 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
21507 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74    /* Parser cont
21508 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ext */.  SrcList
21509 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 2f 2a 20   *pTabList,  /* 
2150a 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20 2a  List of tables *
2150b 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
2150c 4c 69 73 74 20 20 20 20 2f 2a 20 45 78 70 72 65  List    /* Expre
2150d 73 73 69 6f 6e 73 20 64 65 66 69 6e 69 6e 67 20  ssions defining 
2150e 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a  the result set *
2150f 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  /.){.  Vdbe *v =
21510 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
21511 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 71    int i, j;.  sq
21512 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
21513 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 66 75  se->db;.  int fu
21514 6c 6c 4e 61 6d 65 73 2c 20 73 68 6f 72 74 4e 61  llNames, shortNa
21515 6d 65 73 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  mes;..#ifndef SQ
21516 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49  LITE_OMIT_EXPLAI
21517 4e 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69  N.  /* If this i
21518 73 20 61 6e 20 45 58 50 4c 41 49 4e 2c 20 73 6b  s an EXPLAIN, sk
21519 69 70 20 74 68 69 73 20 73 74 65 70 20 2a 2f 0a  ip this step */.
2151a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 65 78    if( pParse->ex
2151b 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 72 65 74  plain ){.    ret
2151c 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  urn;.  }.#endif.
2151d 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20  .  assert( v!=0 
2151e 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  );.  if( pParse-
2151f 3e 63 6f 6c 4e 61 6d 65 73 53 65 74 20 7c 7c 20  >colNamesSet || 
21520 76 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  v==0 || db->mall
21521 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72  ocFailed ) retur
21522 6e 3b 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f 6c  n;.  pParse->col
21523 4e 61 6d 65 73 53 65 74 20 3d 20 31 3b 0a 20 20  NamesSet = 1;.  
21524 66 75 6c 6c 4e 61 6d 65 73 20 3d 20 28 64 62 2d  fullNames = (db-
21525 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
21526 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 29 21 3d 30  FullColNames)!=0
21527 3b 0a 20 20 73 68 6f 72 74 4e 61 6d 65 73 20 3d  ;.  shortNames =
21528 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51   (db->flags & SQ
21529 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d  LITE_ShortColNam
2152a 65 73 29 21 3d 30 3b 0a 20 20 73 71 6c 69 74 65  es)!=0;.  sqlite
2152b 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
2152c 76 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  v, pEList->nExpr
2152d 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
2152e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69  pEList->nExpr; i
2152f 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70  ++){.    Expr *p
21530 3b 0a 20 20 20 20 70 20 3d 20 70 45 4c 69 73 74  ;.    p = pEList
21531 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[i].pExpr;.  
21532 20 20 69 66 28 20 70 3d 3d 30 20 29 20 63 6f 6e    if( p==0 ) con
21533 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 70  tinue;.    if( p
21534 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  EList->a[i].zNam
21535 65 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20  e ){.      char 
21536 2a 7a 4e 61 6d 65 20 3d 20 70 45 4c 69 73 74 2d  *zName = pEList-
21537 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  >a[i].zName;.   
21538 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
21539 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43  tColName(v, i, C
2153a 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4e 61  OLNAME_NAME, zNa
2153b 6d 65 2c 20 73 74 72 6c 65 6e 28 7a 4e 61 6d 65  me, strlen(zName
2153c 29 29 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e  ));.      contin
2153d 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ue;.    }.    if
2153e 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55  ( p->op==TK_COLU
2153f 4d 4e 20 26 26 20 70 54 61 62 4c 69 73 74 20 29  MN && pTabList )
21540 7b 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70  {.      Table *p
21541 54 61 62 3b 0a 20 20 20 20 20 20 63 68 61 72 20  Tab;.      char 
21542 2a 7a 43 6f 6c 3b 0a 20 20 20 20 20 20 69 6e 74  *zCol;.      int
21543 20 69 43 6f 6c 20 3d 20 70 2d 3e 69 43 6f 6c 75   iCol = p->iColu
21544 6d 6e 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d  mn;.      for(j=
21545 30 3b 20 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e  0; j<pTabList->n
21546 53 72 63 20 26 26 20 70 54 61 62 4c 69 73 74 2d  Src && pTabList-
21547 3e 61 5b 6a 5d 2e 69 43 75 72 73 6f 72 21 3d 70  >a[j].iCursor!=p
21548 2d 3e 69 54 61 62 6c 65 3b 20 6a 2b 2b 29 7b 7d  ->iTable; j++){}
21549 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6a  .      assert( j
2154a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 20  <pTabList->nSrc 
2154b 29 3b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20  );.      pTab = 
2154c 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70  pTabList->a[j].p
2154d 54 61 62 3b 0a 20 20 20 20 20 20 69 66 28 20 69  Tab;.      if( i
2154e 43 6f 6c 3c 30 20 29 20 69 43 6f 6c 20 3d 20 70  Col<0 ) iCol = p
2154f 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20 20 20 20  Tab->iPKey;.    
21550 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3d 3d    assert( iCol==
21551 2d 31 20 7c 7c 20 28 69 43 6f 6c 3e 3d 30 20 26  -1 || (iCol>=0 &
21552 26 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f  & iCol<pTab->nCo
21553 6c 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  l) );.      if( 
21554 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20  iCol<0 ){.      
21555 20 20 7a 43 6f 6c 20 3d 20 22 72 6f 77 69 64 22    zCol = "rowid"
21556 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
21557 20 20 20 20 20 20 20 7a 43 6f 6c 20 3d 20 70 54         zCol = pT
21558 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a  ab->aCol[iCol].z
21559 4e 61 6d 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Name;.      }.  
2155a 20 20 20 20 69 66 28 20 21 73 68 6f 72 74 4e 61      if( !shortNa
2155b 6d 65 73 20 26 26 20 21 66 75 6c 6c 4e 61 6d 65  mes && !fullName
2155c 73 20 26 26 20 70 2d 3e 73 70 61 6e 2e 7a 20 26  s && p->span.z &
2155d 26 20 70 2d 3e 73 70 61 6e 2e 7a 5b 30 5d 20 29  & p->span.z[0] )
2155e 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2155f 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
21560 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, i, COLNAME_NA
21561 4d 45 2c 20 28 63 68 61 72 2a 29 70 2d 3e 73 70  ME, (char*)p->sp
21562 61 6e 2e 7a 2c 20 70 2d 3e 73 70 61 6e 2e 6e 29  an.z, p->span.n)
21563 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
21564 28 20 66 75 6c 6c 4e 61 6d 65 73 20 7c 7c 20 28  ( fullNames || (
21565 21 73 68 6f 72 74 4e 61 6d 65 73 20 26 26 20 70  !shortNames && p
21566 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3e 31 29  TabList->nSrc>1)
21567 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72   ){.        char
21568 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20   *zName = 0;.   
21569 20 20 20 20 20 63 68 61 72 20 2a 7a 54 61 62 3b       char *zTab;
2156a 0a 20 0a 20 20 20 20 20 20 20 20 7a 54 61 62 20  . .        zTab 
2156b 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d  = pTabList->a[j]
2156c 2e 7a 41 6c 69 61 73 3b 0a 20 20 20 20 20 20 20  .zAlias;.       
2156d 20 69 66 28 20 66 75 6c 6c 4e 61 6d 65 73 20 7c   if( fullNames |
2156e 7c 20 7a 54 61 62 3d 3d 30 20 29 20 7a 54 61 62  | zTab==0 ) zTab
2156f 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a   = pTab->zName;.
21570 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
21571 65 74 53 74 72 69 6e 67 28 26 7a 4e 61 6d 65 2c  etString(&zName,
21572 20 7a 54 61 62 2c 20 22 2e 22 2c 20 7a 43 6f 6c   zTab, ".", zCol
21573 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a 20 20 20  , (char*)0);.   
21574 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21575 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c  SetColName(v, i,
21576 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a   COLNAME_NAME, z
21577 4e 61 6d 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43  Name, P4_DYNAMIC
21578 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
21579 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2157a 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
2157b 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   i, COLNAME_NAME
2157c 2c 20 7a 43 6f 6c 2c 20 73 74 72 6c 65 6e 28 7a  , zCol, strlen(z
2157d 43 6f 6c 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Col));.      }. 
2157e 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e     }else if( p->
2157f 73 70 61 6e 2e 7a 20 26 26 20 70 2d 3e 73 70 61  span.z && p->spa
21580 6e 2e 7a 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20  n.z[0] ){.      
21581 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
21582 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e  lName(v, i, COLN
21583 41 4d 45 5f 4e 41 4d 45 2c 20 28 63 68 61 72 2a  AME_NAME, (char*
21584 29 70 2d 3e 73 70 61 6e 2e 7a 2c 20 70 2d 3e 73  )p->span.z, p->s
21585 70 61 6e 2e 6e 29 3b 0a 20 20 20 20 20 20 2f 2a  pan.n);.      /*
21586 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 6d 70   sqlite3VdbeComp
21587 72 65 73 73 53 70 61 63 65 28 76 2c 20 61 64 64  ressSpace(v, add
21588 72 29 3b 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65  r); */.    }else
21589 7b 0a 20 20 20 20 20 20 63 68 61 72 20 7a 4e 61  {.      char zNa
2158a 6d 65 5b 33 30 5d 3b 0a 20 20 20 20 20 20 61 73  me[30];.      as
2158b 73 65 72 74 28 20 70 2d 3e 6f 70 21 3d 54 4b 5f  sert( p->op!=TK_
2158c 43 4f 4c 55 4d 4e 20 7c 7c 20 70 54 61 62 4c 69  COLUMN || pTabLi
2158d 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73  st==0 );.      s
2158e 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
2158f 73 69 7a 65 6f 66 28 7a 4e 61 6d 65 29 2c 20 7a  sizeof(zName), z
21590 4e 61 6d 65 2c 20 22 63 6f 6c 75 6d 6e 25 64 22  Name, "column%d"
21591 2c 20 69 2b 31 29 3b 0a 20 20 20 20 20 20 73 71  , i+1);.      sq
21592 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
21593 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d  ame(v, i, COLNAM
21594 45 5f 4e 41 4d 45 2c 20 7a 4e 61 6d 65 2c 20 30  E_NAME, zName, 0
21595 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 67  );.    }.  }.  g
21596 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 54 79 70  enerateColumnTyp
21597 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c  es(pParse, pTabL
21598 69 73 74 2c 20 70 45 4c 69 73 74 29 3b 0a 7d 0a  ist, pEList);.}.
21599 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2159a 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45  OMIT_COMPOUND_SE
2159b 4c 45 43 54 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20  LECT./*.** Name 
2159c 6f 66 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f  of the connectio
2159d 6e 20 6f 70 65 72 61 74 6f 72 2c 20 75 73 65 64  n operator, used
2159e 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61   for error messa
2159f 67 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ges..*/.static c
215a0 6f 6e 73 74 20 63 68 61 72 20 2a 73 65 6c 65 63  onst char *selec
215a1 74 4f 70 4e 61 6d 65 28 69 6e 74 20 69 64 29 7b  tOpName(int id){
215a2 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 73 77  .  char *z;.  sw
215a3 69 74 63 68 28 20 69 64 20 29 7b 0a 20 20 20 20  itch( id ){.    
215a4 63 61 73 65 20 54 4b 5f 41 4c 4c 3a 20 20 20 20  case TK_ALL:    
215a5 20 20 20 7a 20 3d 20 22 55 4e 49 4f 4e 20 41 4c     z = "UNION AL
215a6 4c 22 3b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  L";   break;.   
215a7 20 63 61 73 65 20 54 4b 5f 49 4e 54 45 52 53 45   case TK_INTERSE
215a8 43 54 3a 20 7a 20 3d 20 22 49 4e 54 45 52 53 45  CT: z = "INTERSE
215a9 43 54 22 3b 20 20 20 62 72 65 61 6b 3b 0a 20 20  CT";   break;.  
215aa 20 20 63 61 73 65 20 54 4b 5f 45 58 43 45 50 54    case TK_EXCEPT
215ab 3a 20 20 20 20 7a 20 3d 20 22 45 58 43 45 50 54  :    z = "EXCEPT
215ac 22 3b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ";      break;. 
215ad 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 20     default:     
215ae 20 20 20 20 20 20 7a 20 3d 20 22 55 4e 49 4f 4e        z = "UNION
215af 22 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ";       break;.
215b0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a    }.  return z;.
215b1 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
215b2 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44  TE_OMIT_COMPOUND
215b3 5f 53 45 4c 45 43 54 20 2a 2f 0a 0a 2f 2a 0a 2a  _SELECT */../*.*
215b4 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72  * Forward declar
215b5 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20  ation.*/.static 
215b6 69 6e 74 20 70 72 65 70 53 65 6c 65 63 74 53 74  int prepSelectSt
215b7 6d 74 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63  mt(Parse*, Selec
215b8 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65  t*);../*.** Give
215b9 6e 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65  n a SELECT state
215ba 6d 65 6e 74 2c 20 67 65 6e 65 72 61 74 65 20 61  ment, generate a
215bb 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65   Table structure
215bc 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 0a   that describes.
215bd 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  ** the result se
215be 74 20 6f 66 20 74 68 61 74 20 53 45 4c 45 43 54  t of that SELECT
215bf 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
215c0 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74  ATE Table *sqlit
215c1 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c  e3ResultSetOfSel
215c2 65 63 74 28 50 61 72 73 65 20 2a 70 50 61 72 73  ect(Parse *pPars
215c3 65 2c 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d  e, char *zTabNam
215c4 65 2c 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  e, Select *pSele
215c5 63 74 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54  ct){.  Table *pT
215c6 61 62 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a  ab;.  int i, j;.
215c7 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
215c8 73 74 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61 43  st;.  Column *aC
215c9 6f 6c 2c 20 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c  ol, *pCol;.  sql
215ca 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
215cb 65 2d 3e 64 62 3b 0a 0a 20 20 77 68 69 6c 65 28  e->db;..  while(
215cc 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72   pSelect->pPrior
215cd 20 29 20 70 53 65 6c 65 63 74 20 3d 20 70 53 65   ) pSelect = pSe
215ce 6c 65 63 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20  lect->pPrior;.  
215cf 69 66 28 20 70 72 65 70 53 65 6c 65 63 74 53 74  if( prepSelectSt
215d0 6d 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65  mt(pParse, pSele
215d1 63 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ct) ){.    retur
215d2 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  n 0;.  }.  if( s
215d3 71 6c 69 74 65 33 53 65 6c 65 63 74 52 65 73 6f  qlite3SelectReso
215d4 6c 76 65 28 70 50 61 72 73 65 2c 20 70 53 65 6c  lve(pParse, pSel
215d5 65 63 74 2c 20 30 29 20 29 7b 0a 20 20 20 20 72  ect, 0) ){.    r
215d6 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70  eturn 0;.  }.  p
215d7 54 61 62 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  Tab = sqlite3DbM
215d8 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
215d9 7a 65 6f 66 28 54 61 62 6c 65 29 20 29 3b 0a 20  zeof(Table) );. 
215da 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a   if( pTab==0 ){.
215db 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
215dc 7d 0a 20 20 70 54 61 62 2d 3e 6e 52 65 66 20 3d  }.  pTab->nRef =
215dd 20 31 3b 0a 20 20 70 54 61 62 2d 3e 7a 4e 61 6d   1;.  pTab->zNam
215de 65 20 3d 20 7a 54 61 62 4e 61 6d 65 20 3f 20 73  e = zTabName ? s
215df 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
215e0 62 2c 20 7a 54 61 62 4e 61 6d 65 29 20 3a 20 30  b, zTabName) : 0
215e1 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 53 65  ;.  pEList = pSe
215e2 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 20  lect->pEList;.  
215e3 70 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 70 45 4c  pTab->nCol = pEL
215e4 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 61 73  ist->nExpr;.  as
215e5 73 65 72 74 28 20 70 54 61 62 2d 3e 6e 43 6f 6c  sert( pTab->nCol
215e6 3e 30 20 29 3b 0a 20 20 70 54 61 62 2d 3e 61 43  >0 );.  pTab->aC
215e7 6f 6c 20 3d 20 61 43 6f 6c 20 3d 20 73 71 6c 69  ol = aCol = sqli
215e8 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
215e9 64 62 2c 20 73 69 7a 65 6f 66 28 70 54 61 62 2d  db, sizeof(pTab-
215ea 3e 61 43 6f 6c 5b 30 5d 29 2a 70 54 61 62 2d 3e  >aCol[0])*pTab->
215eb 6e 43 6f 6c 29 3b 0a 20 20 66 6f 72 28 69 3d 30  nCol);.  for(i=0
215ec 2c 20 70 43 6f 6c 3d 61 43 6f 6c 3b 20 69 3c 70  , pCol=aCol; i<p
215ed 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20  Tab->nCol; i++, 
215ee 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 45 78 70  pCol++){.    Exp
215ef 72 20 2a 70 2c 20 2a 70 52 3b 0a 20 20 20 20 63  r *p, *pR;.    c
215f0 68 61 72 20 2a 7a 54 79 70 65 3b 0a 20 20 20 20  har *zType;.    
215f1 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 20  char *zName;.   
215f2 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 20 20   int nName;.    
215f3 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a  CollSeq *pColl;.
215f4 20 20 20 20 69 6e 74 20 63 6e 74 3b 0a 20 20 20      int cnt;.   
215f5 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43   NameContext sNC
215f6 3b 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20 47 65  ;.    .    /* Ge
215f7 74 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65  t an appropriate
215f8 20 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 63 6f   name for the co
215f9 6c 75 6d 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  lumn.    */.    
215fa 70 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d  p = pEList->a[i]
215fb 2e 70 45 78 70 72 3b 0a 20 20 20 20 61 73 73 65  .pExpr;.    asse
215fc 72 74 28 20 70 2d 3e 70 52 69 67 68 74 3d 3d 30  rt( p->pRight==0
215fd 20 7c 7c 20 70 2d 3e 70 52 69 67 68 74 2d 3e 74   || p->pRight->t
215fe 6f 6b 65 6e 2e 7a 3d 3d 30 20 7c 7c 20 70 2d 3e  oken.z==0 || p->
215ff 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 2e 7a 5b  pRight->token.z[
21600 30 5d 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  0]!=0 );.    if(
21601 20 28 7a 4e 61 6d 65 20 3d 20 70 45 4c 69 73 74   (zName = pEList
21602 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 21 3d 30  ->a[i].zName)!=0
21603 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20   ){.      /* If 
21604 74 68 65 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61  the column conta
21605 69 6e 73 20 61 6e 20 22 41 53 20 3c 6e 61 6d 65  ins an "AS <name
21606 3e 22 20 70 68 72 61 73 65 2c 20 75 73 65 20 3c  >" phrase, use <
21607 6e 61 6d 65 3e 20 61 73 20 74 68 65 20 6e 61 6d  name> as the nam
21608 65 20 2a 2f 0a 20 20 20 20 20 20 7a 4e 61 6d 65  e */.      zName
21609 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
2160a 75 70 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20  up(db, zName);. 
2160b 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e     }else if( p->
2160c 6f 70 3d 3d 54 4b 5f 44 4f 54 20 0a 20 20 20 20  op==TK_DOT .    
2160d 20 20 20 20 20 20 20 20 20 20 26 26 20 28 70 52            && (pR
2160e 3d 70 2d 3e 70 52 69 67 68 74 29 21 3d 30 20 26  =p->pRight)!=0 &
2160f 26 20 70 52 2d 3e 74 6f 6b 65 6e 2e 7a 20 26 26  & pR->token.z &&
21610 20 70 52 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 20   pR->token.z[0] 
21611 29 7b 0a 20 20 20 20 20 20 2f 2a 20 46 6f 72 20  ){.      /* For 
21612 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 66  columns of the f
21613 72 6f 6d 20 41 2e 42 20 75 73 65 20 42 20 61 73  rom A.B use B as
21614 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20   the name */.   
21615 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74     zName = sqlit
21616 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25  e3MPrintf(db, "%
21617 54 22 2c 20 26 70 52 2d 3e 74 6f 6b 65 6e 29 3b  T", &pR->token);
21618 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70  .    }else if( p
21619 2d 3e 73 70 61 6e 2e 7a 20 26 26 20 70 2d 3e 73  ->span.z && p->s
2161a 70 61 6e 2e 7a 5b 30 5d 20 29 7b 0a 20 20 20 20  pan.z[0] ){.    
2161b 20 20 2f 2a 20 55 73 65 20 74 68 65 20 6f 72 69    /* Use the ori
2161c 67 69 6e 61 6c 20 74 65 78 74 20 6f 66 20 74 68  ginal text of th
2161d 65 20 63 6f 6c 75 6d 6e 20 65 78 70 72 65 73 73  e column express
2161e 69 6f 6e 20 61 73 20 69 74 73 20 6e 61 6d 65 20  ion as its name 
2161f 2a 2f 0a 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d  */.      zName =
21620 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
21621 64 62 2c 20 22 25 54 22 2c 20 26 70 2d 3e 73 70  db, "%T", &p->sp
21622 61 6e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  an);.    }else{.
21623 20 20 20 20 20 20 2f 2a 20 49 66 20 61 6c 6c 20        /* If all 
21624 65 6c 73 65 20 66 61 69 6c 73 2c 20 6d 61 6b 65  else fails, make
21625 20 75 70 20 61 20 6e 61 6d 65 20 2a 2f 0a 20 20   up a name */.  
21626 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69      zName = sqli
21627 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
21628 63 6f 6c 75 6d 6e 25 64 22 2c 20 69 2b 31 29 3b  column%d", i+1);
21629 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21  .    }.    if( !
2162a 7a 4e 61 6d 65 20 7c 7c 20 64 62 2d 3e 6d 61 6c  zName || db->mal
2162b 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
2162c 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
2162d 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 73  led = 1;.      s
2162e 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4e 61 6d  qlite3_free(zNam
2162f 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e);.      sqlite
21630 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 54 61  3DeleteTable(pTa
21631 62 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  b);.      return
21632 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71   0;.    }.    sq
21633 6c 69 74 65 33 44 65 71 75 6f 74 65 28 7a 4e 61  lite3Dequote(zNa
21634 6d 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b  me);..    /* Mak
21635 65 20 73 75 72 65 20 74 68 65 20 63 6f 6c 75 6d  e sure the colum
21636 6e 20 6e 61 6d 65 20 69 73 20 75 6e 69 71 75 65  n name is unique
21637 2e 20 20 49 66 20 74 68 65 20 6e 61 6d 65 20 69  .  If the name i
21638 73 20 6e 6f 74 20 75 6e 69 71 75 65 2c 0a 20 20  s not unique,.  
21639 20 20 2a 2a 20 61 70 70 65 6e 64 20 61 20 69 6e    ** append a in
2163a 74 65 67 65 72 20 74 6f 20 74 68 65 20 6e 61 6d  teger to the nam
2163b 65 20 73 6f 20 74 68 61 74 20 69 74 20 62 65 63  e so that it bec
2163c 6f 6d 65 73 20 75 6e 69 71 75 65 2e 0a 20 20 20  omes unique..   
2163d 20 2a 2f 0a 20 20 20 20 6e 4e 61 6d 65 20 3d 20   */.    nName = 
2163e 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 3b 0a 20  strlen(zName);. 
2163f 20 20 20 66 6f 72 28 6a 3d 63 6e 74 3d 30 3b 20     for(j=cnt=0; 
21640 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  j<i; j++){.     
21641 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
21642 43 6d 70 28 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d  Cmp(aCol[j].zNam
21643 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a  e, zName)==0 ){.
21644 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 5b 6e 4e          zName[nN
21645 61 6d 65 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20  ame] = 0;.      
21646 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    zName = sqlite
21647 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 7a  3MPrintf(db, "%z
21648 3a 25 64 22 2c 20 7a 4e 61 6d 65 2c 20 2b 2b 63  :%d", zName, ++c
21649 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 6a 20 3d  nt);.        j =
2164a 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28   -1;.        if(
2164b 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 62 72 65 61   zName==0 ) brea
2164c 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
2164d 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65  .    pCol->zName
2164e 20 3d 20 7a 4e 61 6d 65 3b 0a 0a 20 20 20 20 2f   = zName;..    /
2164f 2a 20 47 65 74 20 74 68 65 20 74 79 70 65 6e 61  * Get the typena
21650 6d 65 2c 20 74 79 70 65 20 61 66 66 69 6e 69 74  me, type affinit
21651 79 2c 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6e 67  y, and collating
21652 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68   sequence for th
21653 65 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 2e  e.    ** column.
21654 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6d 65 6d 73  .    */.    mems
21655 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65  et(&sNC, 0, size
21656 6f 66 28 73 4e 43 29 29 3b 0a 20 20 20 20 73 4e  of(sNC));.    sN
21657 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 53 65  C.pSrcList = pSe
21658 6c 65 63 74 2d 3e 70 53 72 63 3b 0a 20 20 20 20  lect->pSrc;.    
21659 7a 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 44  zType = sqlite3D
2165a 62 53 74 72 44 75 70 28 64 62 2c 20 63 6f 6c 75  bStrDup(db, colu
2165b 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20  mnType(&sNC, p, 
2165c 30 2c 20 30 2c 20 30 29 29 3b 0a 20 20 20 20 70  0, 0, 0));.    p
2165d 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20 7a 54 79  Col->zType = zTy
2165e 70 65 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 61 66  pe;.    pCol->af
2165f 66 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33  finity = sqlite3
21660 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 29 3b  ExprAffinity(p);
21661 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c  .    pColl = sql
21662 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28  ite3ExprCollSeq(
21663 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 20 20  pParse, p);.    
21664 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20  if( pColl ){.   
21665 20 20 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 20 3d     pCol->zColl =
21666 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
21667 28 64 62 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d  (db, pColl->zNam
21668 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e);.    }.  }.  
21669 70 54 61 62 2d 3e 69 50 4b 65 79 20 3d 20 2d 31  pTab->iPKey = -1
2166a 3b 0a 20 20 72 65 74 75 72 6e 20 70 54 61 62 3b  ;.  return pTab;
2166b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72  .}../*.** Prepar
2166c 65 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65  e a SELECT state
2166d 6d 65 6e 74 20 66 6f 72 20 70 72 6f 63 65 73 73  ment for process
2166e 69 6e 67 20 62 79 20 64 6f 69 6e 67 20 74 68 65  ing by doing the
2166f 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 74 68   following.** th
21670 69 6e 67 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28  ings:.**.**    (
21671 31 29 20 20 4d 61 6b 65 20 73 75 72 65 20 56 44  1)  Make sure VD
21672 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  BE cursor number
21673 73 20 68 61 76 65 20 62 65 65 6e 20 61 73 73 69  s have been assi
21674 67 6e 65 64 20 74 6f 20 65 76 65 72 79 0a 2a 2a  gned to every.**
21675 20 20 20 20 20 20 20 20 20 65 6c 65 6d 65 6e 74           element
21676 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   of the FROM cla
21677 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32  use..**.**    (2
21678 29 20 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 70  )  Fill in the p
21679 54 61 62 4c 69 73 74 2d 3e 61 5b 5d 2e 70 54 61  TabList->a[].pTa
2167a 62 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20  b fields in the 
2167b 53 72 63 4c 69 73 74 20 74 68 61 74 20 0a 2a 2a  SrcList that .**
2167c 20 20 20 20 20 20 20 20 20 64 65 66 69 6e 65 73           defines
2167d 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 57   FROM clause.  W
2167e 68 65 6e 20 76 69 65 77 73 20 61 70 70 65 61 72  hen views appear
2167f 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
21680 75 73 65 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20  use,.**         
21681 66 69 6c 6c 20 70 54 61 62 4c 69 73 74 2d 3e 61  fill pTabList->a
21682 5b 5d 2e 70 53 65 6c 65 63 74 20 77 69 74 68 20  [].pSelect with 
21683 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 53 45  a copy of the SE
21684 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 2a  LECT statement.*
21685 2a 20 20 20 20 20 20 20 20 20 74 68 61 74 20 69  *         that i
21686 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 76 69  mplements the vi
21687 65 77 2e 20 20 41 20 63 6f 70 79 20 69 73 20 6d  ew.  A copy is m
21688 61 64 65 20 6f 66 20 74 68 65 20 76 69 65 77 27  ade of the view'
21689 73 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20  s SELECT.**     
2168a 20 20 20 20 73 74 61 74 65 6d 65 6e 74 20 73 6f      statement so
2168b 20 74 68 61 74 20 77 65 20 63 61 6e 20 66 72 65   that we can fre
2168c 65 6c 79 20 6d 6f 64 69 66 79 20 6f 72 20 64 65  ely modify or de
2168d 6c 65 74 65 20 74 68 61 74 20 73 74 61 74 65 6d  lete that statem
2168e 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 77  ent.**         w
2168f 69 74 68 6f 75 74 20 77 6f 72 72 79 69 6e 67 20  ithout worrying 
21690 61 62 6f 75 74 20 6d 65 73 73 69 6e 67 20 75 70  about messing up
21691 20 74 68 65 20 70 72 65 73 69 73 74 65 6e 74 20   the presistent 
21692 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a  representation.*
21693 2a 20 20 20 20 20 20 20 20 20 6f 66 20 74 68 65  *         of the
21694 20 76 69 65 77 2e 0a 2a 2a 0a 2a 2a 20 20 20 20   view..**.**    
21695 28 33 29 20 20 41 64 64 20 74 65 72 6d 73 20 74  (3)  Add terms t
21696 6f 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  o the WHERE clau
21697 73 65 20 74 6f 20 61 63 63 6f 6d 6f 64 61 74 65  se to accomodate
21698 20 74 68 65 20 4e 41 54 55 52 41 4c 20 6b 65 79   the NATURAL key
21699 77 6f 72 64 0a 2a 2a 20 20 20 20 20 20 20 20 20  word.**         
2169a 6f 6e 20 6a 6f 69 6e 73 20 61 6e 64 20 74 68 65  on joins and the
2169b 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c   ON and USING cl
2169c 61 75 73 65 20 6f 66 20 6a 6f 69 6e 73 2e 0a 2a  ause of joins..*
2169d 2a 0a 2a 2a 20 20 20 20 28 34 29 20 20 53 63 61  *.**    (4)  Sca
2169e 6e 20 74 68 65 20 6c 69 73 74 20 6f 66 20 63 6f  n the list of co
2169f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73  lumns in the res
216a0 75 6c 74 20 73 65 74 20 28 70 45 4c 69 73 74 29  ult set (pEList)
216a1 20 6c 6f 6f 6b 69 6e 67 0a 2a 2a 20 20 20 20 20   looking.**     
216a2 20 20 20 20 66 6f 72 20 69 6e 73 74 61 6e 63 65      for instance
216a3 73 20 6f 66 20 74 68 65 20 22 2a 22 20 6f 70 65  s of the "*" ope
216a4 72 61 74 6f 72 20 6f 72 20 74 68 65 20 54 41 42  rator or the TAB
216a5 4c 45 2e 2a 20 6f 70 65 72 61 74 6f 72 2e 0a 2a  LE.* operator..*
216a6 2a 20 20 20 20 20 20 20 20 20 49 66 20 66 6f 75  *         If fou
216a7 6e 64 2c 20 65 78 70 61 6e 64 20 65 61 63 68 20  nd, expand each 
216a8 22 2a 22 20 74 6f 20 62 65 20 65 76 65 72 79 20  "*" to be every 
216a9 63 6f 6c 75 6d 6e 20 69 6e 20 65 76 65 72 79 20  column in every 
216aa 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20  table.**        
216ab 20 61 6e 64 20 54 41 42 4c 45 2e 2a 20 74 6f 20   and TABLE.* to 
216ac 62 65 20 65 76 65 72 79 20 63 6f 6c 75 6d 6e 20  be every column 
216ad 69 6e 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20  in TABLE..**.** 
216ae 52 65 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63  Return 0 on succ
216af 65 73 73 2e 20 20 49 66 20 74 68 65 72 65 20 61  ess.  If there a
216b0 72 65 20 70 72 6f 62 6c 65 6d 73 2c 20 6c 65 61  re problems, lea
216b1 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  ve an error mess
216b2 61 67 65 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65  age.** in pParse
216b3 20 61 6e 64 20 72 65 74 75 72 6e 20 6e 6f 6e 2d   and return non-
216b4 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  zero..*/.static 
216b5 69 6e 74 20 70 72 65 70 53 65 6c 65 63 74 53 74  int prepSelectSt
216b6 6d 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  mt(Parse *pParse
216b7 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20  , Select *p){.  
216b8 69 6e 74 20 69 2c 20 6a 2c 20 6b 2c 20 72 63 3b  int i, j, k, rc;
216b9 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62  .  SrcList *pTab
216ba 4c 69 73 74 3b 0a 20 20 45 78 70 72 4c 69 73 74  List;.  ExprList
216bb 20 2a 70 45 4c 69 73 74 3b 0a 20 20 73 74 72 75   *pEList;.  stru
216bc 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
216bd 2a 70 46 72 6f 6d 3b 0a 20 20 73 71 6c 69 74 65  *pFrom;.  sqlite
216be 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
216bf 64 62 3b 0a 0a 20 20 69 66 28 20 70 3d 3d 30 20  db;..  if( p==0 
216c0 7c 7c 20 70 2d 3e 70 53 72 63 3d 3d 30 20 7c 7c  || p->pSrc==0 ||
216c1 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
216c2 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  d ){.    return 
216c3 31 3b 0a 20 20 7d 0a 20 20 70 54 61 62 4c 69 73  1;.  }.  pTabLis
216c4 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 70  t = p->pSrc;.  p
216c5 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73  EList = p->pELis
216c6 74 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75  t;..  /* Make su
216c7 72 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  re cursor number
216c8 73 20 68 61 76 65 20 62 65 65 6e 20 61 73 73 69  s have been assi
216c9 67 6e 65 64 20 74 6f 20 61 6c 6c 20 65 6e 74 72  gned to all entr
216ca 69 65 73 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20  ies in.  ** the 
216cb 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74  FROM clause of t
216cc 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
216cd 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  ent..  */.  sqli
216ce 74 65 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e  te3SrcListAssign
216cf 43 75 72 73 6f 72 73 28 70 50 61 72 73 65 2c 20  Cursors(pParse, 
216d0 70 2d 3e 70 53 72 63 29 3b 0a 0a 20 20 2f 2a 20  p->pSrc);..  /* 
216d1 4c 6f 6f 6b 20 75 70 20 65 76 65 72 79 20 74 61  Look up every ta
216d2 62 6c 65 20 6e 61 6d 65 64 20 69 6e 20 74 68 65  ble named in the
216d3 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
216d4 74 68 65 20 73 65 6c 65 63 74 2e 20 20 49 66 0a  the select.  If.
216d5 20 20 2a 2a 20 61 6e 20 65 6e 74 72 79 20 6f 66    ** an entry of
216d6 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
216d7 20 69 73 20 61 20 73 75 62 71 75 65 72 79 20 69   is a subquery i
216d8 6e 73 74 65 61 64 20 6f 66 20 61 20 74 61 62 6c  nstead of a tabl
216d9 65 20 6f 72 20 76 69 65 77 2c 0a 20 20 2a 2a 20  e or view,.  ** 
216da 74 68 65 6e 20 63 72 65 61 74 65 20 61 20 74 72  then create a tr
216db 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 20 73 74  ansient table st
216dc 72 75 63 74 75 72 65 20 74 6f 20 64 65 73 63 72  ructure to descr
216dd 69 62 65 20 74 68 65 20 73 75 62 71 75 65 72 79  ibe the subquery
216de 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  ..  */.  for(i=0
216df 2c 20 70 46 72 6f 6d 3d 70 54 61 62 4c 69 73 74  , pFrom=pTabList
216e0 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d  ->a; i<pTabList-
216e1 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 46 72 6f  >nSrc; i++, pFro
216e2 6d 2b 2b 29 7b 0a 20 20 20 20 54 61 62 6c 65 20  m++){.    Table 
216e3 2a 70 54 61 62 3b 0a 20 20 20 20 69 66 28 20 70  *pTab;.    if( p
216e4 46 72 6f 6d 2d 3e 70 54 61 62 21 3d 30 20 29 7b  From->pTab!=0 ){
216e5 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 73  .      /* This s
216e6 74 61 74 65 6d 65 6e 74 20 68 61 73 20 61 6c 72  tatement has alr
216e7 65 61 64 79 20 62 65 65 6e 20 70 72 65 70 61 72  eady been prepar
216e8 65 64 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f  ed.  There is no
216e9 20 6e 65 65 64 0a 20 20 20 20 20 20 2a 2a 20 74   need.      ** t
216ea 6f 20 67 6f 20 66 75 72 74 68 65 72 2e 20 2a 2f  o go further. */
216eb 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
216ec 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 72 65 74  ==0 );.      ret
216ed 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 0;.    }.   
216ee 20 69 66 28 20 70 46 72 6f 6d 2d 3e 7a 4e 61 6d   if( pFrom->zNam
216ef 65 3d 3d 30 20 29 7b 0a 23 69 66 6e 64 65 66 20  e==0 ){.#ifndef 
216f0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
216f1 55 45 52 59 0a 20 20 20 20 20 20 2f 2a 20 41 20  UERY.      /* A 
216f2 73 75 62 2d 71 75 65 72 79 20 69 6e 20 74 68 65  sub-query in the
216f3 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
216f4 61 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 20 20  a SELECT */.    
216f5 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d    assert( pFrom-
216f6 3e 70 53 65 6c 65 63 74 21 3d 30 20 29 3b 0a 20  >pSelect!=0 );. 
216f7 20 20 20 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e       if( pFrom->
216f8 7a 41 6c 69 61 73 3d 3d 30 20 29 7b 0a 20 20 20  zAlias==0 ){.   
216f9 20 20 20 20 20 70 46 72 6f 6d 2d 3e 7a 41 6c 69       pFrom->zAli
216fa 61 73 20 3d 0a 20 20 20 20 20 20 20 20 20 20 73  as =.          s
216fb 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
216fc 2c 20 22 73 71 6c 69 74 65 5f 73 75 62 71 75 65  , "sqlite_subque
216fd 72 79 5f 25 70 5f 22 2c 20 28 76 6f 69 64 2a 29  ry_%p_", (void*)
216fe 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 29 3b  pFrom->pSelect);
216ff 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61  .      }.      a
21700 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 70 54  ssert( pFrom->pT
21701 61 62 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  ab==0 );.      p
21702 46 72 6f 6d 2d 3e 70 54 61 62 20 3d 20 70 54 61  From->pTab = pTa
21703 62 20 3d 20 0a 20 20 20 20 20 20 20 20 73 71 6c  b = .        sql
21704 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53  ite3ResultSetOfS
21705 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 46  elect(pParse, pF
21706 72 6f 6d 2d 3e 7a 41 6c 69 61 73 2c 20 70 46 72  rom->zAlias, pFr
21707 6f 6d 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20  om->pSelect);.  
21708 20 20 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20      if( pTab==0 
21709 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
2170a 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 1;.      }.   
2170b 20 20 20 2f 2a 20 54 68 65 20 69 73 45 70 68 65     /* The isEphe
2170c 6d 20 66 6c 61 67 20 69 6e 64 69 63 61 74 65 73  m flag indicates
2170d 20 74 68 61 74 20 74 68 65 20 54 61 62 6c 65 20   that the Table 
2170e 73 74 72 75 63 74 75 72 65 20 68 61 73 20 62 65  structure has be
2170f 65 6e 0a 20 20 20 20 20 20 2a 2a 20 64 79 6e 61  en.      ** dyna
21710 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65  mically allocate
21711 64 20 61 6e 64 20 6d 61 79 20 62 65 20 66 72 65  d and may be fre
21712 65 64 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 20  ed at any time. 
21713 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
21714 0a 20 20 20 20 20 20 2a 2a 20 70 54 61 62 20 69  .      ** pTab i
21715 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 74  s not pointing t
21716 6f 20 61 20 70 65 72 73 69 73 74 65 6e 74 20 74  o a persistent t
21717 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 74  able structure t
21718 68 61 74 20 64 65 66 69 6e 65 73 0a 20 20 20 20  hat defines.    
21719 20 20 2a 2a 20 70 61 72 74 20 6f 66 20 74 68 65    ** part of the
2171a 20 73 63 68 65 6d 61 2e 20 2a 2f 0a 20 20 20 20   schema. */.    
2171b 20 20 70 54 61 62 2d 3e 69 73 45 70 68 65 6d 20    pTab->isEphem 
2171c 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  = 1;.#endif.    
2171d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20  }else{.      /* 
2171e 41 6e 20 6f 72 64 69 6e 61 72 79 20 74 61 62 6c  An ordinary tabl
2171f 65 20 6f 72 20 76 69 65 77 20 6e 61 6d 65 20 69  e or view name i
21720 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
21721 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72  e */.      asser
21722 74 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3d 3d  t( pFrom->pTab==
21723 30 20 29 3b 0a 20 20 20 20 20 20 70 46 72 6f 6d  0 );.      pFrom
21724 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 20 3d 20  ->pTab = pTab = 
21725 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
21726 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72  LocateTable(pPar
21727 73 65 2c 30 2c 70 46 72 6f 6d 2d 3e 7a 4e 61 6d  se,0,pFrom->zNam
21728 65 2c 70 46 72 6f 6d 2d 3e 7a 44 61 74 61 62 61  e,pFrom->zDataba
21729 73 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  se);.      if( p
2172a 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Tab==0 ){.      
2172b 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
2172c 20 20 7d 0a 20 20 20 20 20 20 70 54 61 62 2d 3e    }.      pTab->
2172d 6e 52 65 66 2b 2b 3b 0a 23 69 66 20 21 64 65 66  nRef++;.#if !def
2172e 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
2172f 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e  _VIEW) || !defin
21730 65 64 20 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ed (SQLITE_OMIT_
21731 56 49 52 54 55 41 4c 54 41 42 4c 45 29 0a 20 20  VIRTUALTABLE).  
21732 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53      if( pTab->pS
21733 65 6c 65 63 74 20 7c 7c 20 49 73 56 69 72 74 75  elect || IsVirtu
21734 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
21735 20 20 20 20 2f 2a 20 57 65 20 72 65 61 63 68 20      /* We reach 
21736 68 65 72 65 20 69 66 20 74 68 65 20 6e 61 6d 65  here if the name
21737 64 20 74 61 62 6c 65 20 69 73 20 61 20 72 65 61  d table is a rea
21738 6c 6c 79 20 61 20 76 69 65 77 20 2a 2f 0a 20 20  lly a view */.  
21739 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
2173a 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61  3ViewGetColumnNa
2173b 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62  mes(pParse, pTab
2173c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  ) ){.          r
2173d 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20  eturn 1;.       
2173e 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66   }.        /* If
2173f 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 21   pFrom->pSelect!
21740 3d 30 20 69 74 20 6d 65 61 6e 73 20 77 65 20 61  =0 it means we a
21741 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20  re dealing with 
21742 61 0a 20 20 20 20 20 20 20 20 2a 2a 20 76 69 65  a.        ** vie
21743 77 20 77 69 74 68 69 6e 20 61 20 76 69 65 77 2e  w within a view.
21744 20 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 72    The SELECT str
21745 75 63 74 75 72 65 20 68 61 73 20 61 6c 72 65 61  ucture has alrea
21746 64 79 20 62 65 65 6e 0a 20 20 20 20 20 20 20 20  dy been.        
21747 2a 2a 20 63 6f 70 69 65 64 20 62 79 20 74 68 65  ** copied by the
21748 20 6f 75 74 65 72 20 76 69 65 77 20 73 6f 20 77   outer view so w
21749 65 20 63 61 6e 20 73 6b 69 70 20 74 68 65 20 63  e can skip the c
2174a 6f 70 79 20 73 74 65 70 20 68 65 72 65 0a 20 20  opy step here.  
2174b 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20        ** in the 
2174c 69 6e 6e 65 72 20 76 69 65 77 2e 0a 20 20 20 20  inner view..    
2174d 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69      */.        i
2174e 66 28 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63  f( pFrom->pSelec
2174f 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  t==0 ){.        
21750 20 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74    pFrom->pSelect
21751 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
21752 44 75 70 28 64 62 2c 20 70 54 61 62 2d 3e 70 53  Dup(db, pTab->pS
21753 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20  elect);.        
21754 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  }.      }.#endif
21755 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
21756 20 50 72 6f 63 65 73 73 20 4e 41 54 55 52 41 4c   Process NATURAL
21757 20 6b 65 79 77 6f 72 64 73 2c 20 61 6e 64 20 4f   keywords, and O
21758 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75  N and USING clau
21759 73 65 73 20 6f 66 20 6a 6f 69 6e 73 2e 0a 20 20  ses of joins..  
2175a 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 50  */.  if( sqliteP
2175b 72 6f 63 65 73 73 4a 6f 69 6e 28 70 50 61 72 73  rocessJoin(pPars
2175c 65 2c 20 70 29 20 29 20 72 65 74 75 72 6e 20 31  e, p) ) return 1
2175d 3b 0a 0a 20 20 2f 2a 20 46 6f 72 20 65 76 65 72  ;..  /* For ever
2175e 79 20 22 2a 22 20 74 68 61 74 20 6f 63 63 75 72  y "*" that occur
2175f 73 20 69 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20  s in the column 
21760 6c 69 73 74 2c 20 69 6e 73 65 72 74 20 74 68 65  list, insert the
21761 20 6e 61 6d 65 73 20 6f 66 0a 20 20 2a 2a 20 61   names of.  ** a
21762 6c 6c 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6c  ll columns in al
21763 6c 20 74 61 62 6c 65 73 2e 20 20 41 6e 64 20 66  l tables.  And f
21764 6f 72 20 65 76 65 72 79 20 54 41 42 4c 45 2e 2a  or every TABLE.*
21765 20 69 6e 73 65 72 74 20 74 68 65 20 6e 61 6d 65   insert the name
21766 73 0a 20 20 2a 2a 20 6f 66 20 61 6c 6c 20 63 6f  s.  ** of all co
21767 6c 75 6d 6e 73 20 69 6e 20 54 41 42 4c 45 2e 20  lumns in TABLE. 
21768 20 54 68 65 20 70 61 72 73 65 72 20 69 6e 73 65   The parser inse
21769 72 74 65 64 20 61 20 73 70 65 63 69 61 6c 20 65  rted a special e
2176a 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a 20 77  xpression.  ** w
2176b 69 74 68 20 74 68 65 20 54 4b 5f 41 4c 4c 20 6f  ith the TK_ALL o
2176c 70 65 72 61 74 6f 72 20 66 6f 72 20 65 61 63 68  perator for each
2176d 20 22 2a 22 20 74 68 61 74 20 69 74 20 66 6f 75   "*" that it fou
2176e 6e 64 20 69 6e 20 74 68 65 20 63 6f 6c 75 6d 6e  nd in the column
2176f 20 6c 69 73 74 2e 0a 20 20 2a 2a 20 54 68 65 20   list..  ** The 
21770 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 6a  following code j
21771 75 73 74 20 68 61 73 20 74 6f 20 6c 6f 63 61 74  ust has to locat
21772 65 20 74 68 65 20 54 4b 5f 41 4c 4c 20 65 78 70  e the TK_ALL exp
21773 72 65 73 73 69 6f 6e 73 20 61 6e 64 20 65 78 70  ressions and exp
21774 61 6e 64 0a 20 20 2a 2a 20 65 61 63 68 20 6f 6e  and.  ** each on
21775 65 20 74 6f 20 74 68 65 20 6c 69 73 74 20 6f 66  e to the list of
21776 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 69 6e 20   all columns in 
21777 61 6c 6c 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2a  all tables..  **
21778 0a 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20  .  ** The first 
21779 6c 6f 6f 70 20 6a 75 73 74 20 63 68 65 63 6b 73  loop just checks
2177a 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 72 65   to see if there
2177b 20 61 72 65 20 61 6e 79 20 22 2a 22 20 6f 70 65   are any "*" ope
2177c 72 61 74 6f 72 73 0a 20 20 2a 2a 20 74 68 61 74  rators.  ** that
2177d 20 6e 65 65 64 20 65 78 70 61 6e 64 69 6e 67 2e   need expanding.
2177e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 6b 3d 30 3b  .  */.  for(k=0;
2177f 20 6b 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72   k<pEList->nExpr
21780 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72  ; k++){.    Expr
21781 20 2a 70 45 20 3d 20 70 45 4c 69 73 74 2d 3e 61   *pE = pEList->a
21782 5b 6b 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 69  [k].pExpr;.    i
21783 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 41 4c  f( pE->op==TK_AL
21784 4c 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69  L ) break;.    i
21785 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f  f( pE->op==TK_DO
21786 54 20 26 26 20 70 45 2d 3e 70 52 69 67 68 74 20  T && pE->pRight 
21787 26 26 20 70 45 2d 3e 70 52 69 67 68 74 2d 3e 6f  && pE->pRight->o
21788 70 3d 3d 54 4b 5f 41 4c 4c 0a 20 20 20 20 20 20  p==TK_ALL.      
21789 20 20 20 26 26 20 70 45 2d 3e 70 4c 65 66 74 20     && pE->pLeft 
2178a 26 26 20 70 45 2d 3e 70 4c 65 66 74 2d 3e 6f 70  && pE->pLeft->op
2178b 3d 3d 54 4b 5f 49 44 20 29 20 62 72 65 61 6b 3b  ==TK_ID ) break;
2178c 0a 20 20 7d 0a 20 20 72 63 20 3d 20 30 3b 0a 20  .  }.  rc = 0;. 
2178d 20 69 66 28 20 6b 3c 70 45 4c 69 73 74 2d 3e 6e   if( k<pEList->n
2178e 45 78 70 72 20 29 7b 0a 20 20 20 20 2f 2a 0a 20  Expr ){.    /*. 
2178f 20 20 20 2a 2a 20 49 66 20 77 65 20 67 65 74 20     ** If we get 
21790 68 65 72 65 20 69 74 20 6d 65 61 6e 73 20 74 68  here it means th
21791 65 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6e  e result set con
21792 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72  tains one or mor
21793 65 20 22 2a 22 0a 20 20 20 20 2a 2a 20 6f 70 65  e "*".    ** ope
21794 72 61 74 6f 72 73 20 74 68 61 74 20 6e 65 65 64  rators that need
21795 20 74 6f 20 62 65 20 65 78 70 61 6e 64 65 64 2e   to be expanded.
21796 20 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 65    Loop through e
21797 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 0a 20  ach expression. 
21798 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 65 73     ** in the res
21799 75 6c 74 20 73 65 74 20 61 6e 64 20 65 78 70 61  ult set and expa
2179a 6e 64 20 74 68 65 6d 20 6f 6e 65 20 62 79 20 6f  nd them one by o
2179b 6e 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  ne..    */.    s
2179c 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
2179d 74 65 6d 20 2a 61 20 3d 20 70 45 4c 69 73 74 2d  tem *a = pEList-
2179e 3e 61 3b 0a 20 20 20 20 45 78 70 72 4c 69 73 74  >a;.    ExprList
2179f 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 20 20   *pNew = 0;.    
217a0 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 50 61 72  int flags = pPar
217a1 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 3b 0a 20  se->db->flags;. 
217a2 20 20 20 69 6e 74 20 6c 6f 6e 67 4e 61 6d 65 73     int longNames
217a3 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49   = (flags & SQLI
217a4 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 29  TE_FullColNames)
217a5 21 3d 30 20 26 26 0a 20 20 20 20 20 20 20 20 20  !=0 &&.         
217a6 20 20 20 20 20 20 20 20 20 20 20 20 20 28 66 6c               (fl
217a7 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 68 6f  ags & SQLITE_Sho
217a8 72 74 43 6f 6c 4e 61 6d 65 73 29 3d 3d 30 3b 0a  rtColNames)==0;.
217a9 0a 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c  .    for(k=0; k<
217aa 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 6b  pEList->nExpr; k
217ab 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20  ++){.      Expr 
217ac 2a 70 45 20 3d 20 61 5b 6b 5d 2e 70 45 78 70 72  *pE = a[k].pExpr
217ad 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 2d 3e  ;.      if( pE->
217ae 6f 70 21 3d 54 4b 5f 41 4c 4c 20 26 26 0a 20 20  op!=TK_ALL &&.  
217af 20 20 20 20 20 20 20 20 20 28 70 45 2d 3e 6f 70           (pE->op
217b0 21 3d 54 4b 5f 44 4f 54 20 7c 7c 20 70 45 2d 3e  !=TK_DOT || pE->
217b1 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 45 2d  pRight==0 || pE-
217b2 3e 70 52 69 67 68 74 2d 3e 6f 70 21 3d 54 4b 5f  >pRight->op!=TK_
217b3 41 4c 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ALL) ){.        
217b4 2f 2a 20 54 68 69 73 20 70 61 72 74 69 63 75 6c  /* This particul
217b5 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 64 6f  ar expression do
217b6 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62  es not need to b
217b7 65 20 65 78 70 61 6e 64 65 64 2e 0a 20 20 20 20  e expanded..    
217b8 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70      */.        p
217b9 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70  New = sqlite3Exp
217ba 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
217bb 73 65 2c 20 70 4e 65 77 2c 20 61 5b 6b 5d 2e 70  se, pNew, a[k].p
217bc 45 78 70 72 2c 20 30 29 3b 0a 20 20 20 20 20 20  Expr, 0);.      
217bd 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20    if( pNew ){.  
217be 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 61 5b          pNew->a[
217bf 70 4e 65 77 2d 3e 6e 45 78 70 72 2d 31 5d 2e 7a  pNew->nExpr-1].z
217c0 4e 61 6d 65 20 3d 20 61 5b 6b 5d 2e 7a 4e 61 6d  Name = a[k].zNam
217c1 65 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  e;.        }else
217c2 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
217c3 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   1;.        }.  
217c4 20 20 20 20 20 20 61 5b 6b 5d 2e 70 45 78 70 72        a[k].pExpr
217c5 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 61 5b   = 0;.        a[
217c6 6b 5d 2e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20  k].zName = 0;.  
217c7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
217c8 20 20 20 2f 2a 20 54 68 69 73 20 65 78 70 72 65     /* This expre
217c9 73 73 69 6f 6e 20 69 73 20 61 20 22 2a 22 20 6f  ssion is a "*" o
217ca 72 20 61 20 22 54 41 42 4c 45 2e 2a 22 20 61 6e  r a "TABLE.*" an
217cb 64 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 20 20  d needs to be.  
217cc 20 20 20 20 20 20 2a 2a 20 65 78 70 61 6e 64 65        ** expande
217cd 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e  d. */.        in
217ce 74 20 74 61 62 6c 65 53 65 65 6e 20 3d 20 30 3b  t tableSeen = 0;
217cf 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20        /* Set to 
217d0 31 20 77 68 65 6e 20 54 41 42 4c 45 20 6d 61 74  1 when TABLE mat
217d1 63 68 65 73 20 2a 2f 0a 20 20 20 20 20 20 20 20  ches */.        
217d2 63 68 61 72 20 2a 7a 54 4e 61 6d 65 3b 20 20 20  char *zTName;   
217d3 20 20 20 20 20 20 20 20 20 2f 2a 20 74 65 78 74           /* text
217d4 20 6f 66 20 6e 61 6d 65 20 6f 66 20 54 41 42 4c   of name of TABL
217d5 45 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28  E */.        if(
217d6 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20   pE->op==TK_DOT 
217d7 26 26 20 70 45 2d 3e 70 4c 65 66 74 20 29 7b 0a  && pE->pLeft ){.
217d8 20 20 20 20 20 20 20 20 20 20 7a 54 4e 61 6d 65            zTName
217d9 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
217da 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 26 70 45 2d  omToken(db, &pE-
217db 3e 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 29 3b 0a  >pLeft->token);.
217dc 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
217dd 20 20 20 20 20 20 20 20 20 7a 54 4e 61 6d 65 20           zTName 
217de 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 0;.        }. 
217df 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20         for(i=0, 
217e0 70 46 72 6f 6d 3d 70 54 61 62 4c 69 73 74 2d 3e  pFrom=pTabList->
217e1 61 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e  a; i<pTabList->n
217e2 53 72 63 3b 20 69 2b 2b 2c 20 70 46 72 6f 6d 2b  Src; i++, pFrom+
217e3 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 54 61  +){.          Ta
217e4 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 46 72 6f  ble *pTab = pFro
217e5 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 20  m->pTab;.       
217e6 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d     char *zTabNam
217e7 65 20 3d 20 70 46 72 6f 6d 2d 3e 7a 41 6c 69 61  e = pFrom->zAlia
217e8 73 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  s;.          if(
217e9 20 7a 54 61 62 4e 61 6d 65 3d 3d 30 20 7c 7c 20   zTabName==0 || 
217ea 7a 54 61 62 4e 61 6d 65 5b 30 5d 3d 3d 30 20 29  zTabName[0]==0 )
217eb 7b 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a  { .            z
217ec 54 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e  TabName = pTab->
217ed 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20  zName;.         
217ee 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28   }.          if(
217ef 20 7a 54 4e 61 6d 65 20 26 26 20 28 7a 54 61 62   zTName && (zTab
217f0 4e 61 6d 65 3d 3d 30 20 7c 7c 20 7a 54 61 62 4e  Name==0 || zTabN
217f1 61 6d 65 5b 30 5d 3d 3d 30 20 7c 7c 20 0a 20 20  ame[0]==0 || .  
217f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
217f3 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54  qlite3StrICmp(zT
217f4 4e 61 6d 65 2c 20 7a 54 61 62 4e 61 6d 65 29 21  Name, zTabName)!
217f5 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  =0) ){.         
217f6 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
217f7 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
217f8 20 20 20 74 61 62 6c 65 53 65 65 6e 20 3d 20 31     tableSeen = 1
217f9 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28  ;.          for(
217fa 6a 3d 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f  j=0; j<pTab->nCo
217fb 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  l; j++){.       
217fc 20 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72       Expr *pExpr
217fd 2c 20 2a 70 52 69 67 68 74 3b 0a 20 20 20 20 20  , *pRight;.     
217fe 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61         char *zNa
217ff 6d 65 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  me = pTab->aCol[
21800 6a 5d 2e 7a 4e 61 6d 65 3b 0a 0a 20 20 20 20 20  j].zName;..     
21801 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 20 63         /* If a c
21802 6f 6c 75 6d 6e 20 69 73 20 6d 61 72 6b 65 64 20  olumn is marked 
21803 61 73 20 27 68 69 64 64 65 6e 27 20 28 63 75 72  as 'hidden' (cur
21804 72 65 6e 74 6c 79 20 6f 6e 6c 79 20 70 6f 73 73  rently only poss
21805 69 62 6c 65 0a 20 20 20 20 20 20 20 20 20 20 20  ible.           
21806 20 2a 2a 20 66 6f 72 20 76 69 72 74 75 61 6c 20   ** for virtual 
21807 74 61 62 6c 65 73 29 2c 20 64 6f 20 6e 6f 74 20  tables), do not 
21808 69 6e 63 6c 75 64 65 20 69 74 20 69 6e 20 74 68  include it in th
21809 65 20 65 78 70 61 6e 64 65 64 0a 20 20 20 20 20  e expanded.     
2180a 20 20 20 20 20 20 20 2a 2a 20 72 65 73 75 6c 74         ** result
2180b 2d 73 65 74 20 6c 69 73 74 2e 0a 20 20 20 20 20  -set list..     
2180c 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
2180d 20 20 20 20 20 20 69 66 28 20 49 73 48 69 64 64        if( IsHidd
2180e 65 6e 43 6f 6c 75 6d 6e 28 26 70 54 61 62 2d 3e  enColumn(&pTab->
2180f 61 43 6f 6c 5b 6a 5d 29 20 29 7b 0a 20 20 20 20  aCol[j]) ){.    
21810 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
21811 28 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29  (IsVirtual(pTab)
21812 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
21813 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
21814 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
21815 20 20 20 20 20 20 69 66 28 20 69 3e 30 20 29 7b        if( i>0 ){
21816 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  .              s
21817 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
21818 65 6d 20 2a 70 4c 65 66 74 20 3d 20 26 70 54 61  em *pLeft = &pTa
21819 62 4c 69 73 74 2d 3e 61 5b 69 2d 31 5d 3b 0a 20  bList->a[i-1];. 
2181a 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
2181b 20 28 70 4c 65 66 74 5b 31 5d 2e 6a 6f 69 6e 74   (pLeft[1].joint
2181c 79 70 65 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c  ype & JT_NATURAL
2181d 29 21 3d 30 20 26 26 0a 20 20 20 20 20 20 20 20  )!=0 &&.        
2181e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2181f 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 70 4c 65 66  columnIndex(pLef
21820 74 2d 3e 70 54 61 62 2c 20 7a 4e 61 6d 65 29 3e  t->pTab, zName)>
21821 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
21822 20 20 20 20 20 20 2f 2a 20 49 6e 20 61 20 4e 41        /* In a NA
21823 54 55 52 41 4c 20 6a 6f 69 6e 2c 20 6f 6d 69 74  TURAL join, omit
21824 20 74 68 65 20 6a 6f 69 6e 20 63 6f 6c 75 6d 6e   the join column
21825 73 20 66 72 6f 6d 20 74 68 65 20 0a 20 20 20 20  s from the .    
21826 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74              ** t
21827 61 62 6c 65 20 6f 6e 20 74 68 65 20 72 69 67 68  able on the righ
21828 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  t */.           
21829 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
2182a 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
2182b 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
2182c 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 49 6e   sqlite3IdListIn
2182d 64 65 78 28 70 4c 65 66 74 5b 31 5d 2e 70 55 73  dex(pLeft[1].pUs
2182e 69 6e 67 2c 20 7a 4e 61 6d 65 29 3e 3d 30 20 29  ing, zName)>=0 )
2182f 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
21830 20 20 2f 2a 20 49 6e 20 61 20 6a 6f 69 6e 20 77    /* In a join w
21831 69 74 68 20 61 20 55 53 49 4e 47 20 63 6c 61 75  ith a USING clau
21832 73 65 2c 20 6f 6d 69 74 20 63 6f 6c 75 6d 6e 73  se, omit columns
21833 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20   in the.        
21834 20 20 20 20 20 20 20 20 2a 2a 20 75 73 69 6e 67          ** using
21835 20 63 6c 61 75 73 65 20 66 72 6f 6d 20 74 68 65   clause from the
21836 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 20 72 69   table on the ri
21837 67 68 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ght. */.        
21838 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65          continue
21839 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
2183a 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  }.            }.
2183b 20 20 20 20 20 20 20 20 20 20 20 20 70 52 69 67              pRig
2183c 68 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  ht = sqlite3PExp
2183d 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c  r(pParse, TK_ID,
2183e 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20   0, 0, 0);.     
2183f 20 20 20 20 20 20 20 69 66 28 20 70 52 69 67 68         if( pRigh
21840 74 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20  t==0 ) break;.  
21841 20 20 20 20 20 20 20 20 20 20 73 65 74 51 75 6f            setQuo
21842 74 65 64 54 6f 6b 65 6e 28 70 50 61 72 73 65 2c  tedToken(pParse,
21843 20 26 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 2c   &pRight->token,
21844 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20   zName);.       
21845 20 20 20 20 20 69 66 28 20 7a 54 61 62 4e 61 6d       if( zTabNam
21846 65 20 26 26 20 28 6c 6f 6e 67 4e 61 6d 65 73 20  e && (longNames 
21847 7c 7c 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72  || pTabList->nSr
21848 63 3e 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20  c>1) ){.        
21849 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66        Expr *pLef
2184a 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  t = sqlite3PExpr
2184b 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20  (pParse, TK_ID, 
2184c 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  0, 0, 0);.      
2184d 20 20 20 20 20 20 20 20 70 45 78 70 72 20 3d 20          pExpr = 
2184e 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2184f 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 70 4c 65  rse, TK_DOT, pLe
21850 66 74 2c 20 70 52 69 67 68 74 2c 20 30 29 3b 0a  ft, pRight, 0);.
21851 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
21852 28 20 70 45 78 70 72 3d 3d 30 20 29 20 62 72 65  ( pExpr==0 ) bre
21853 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ak;.            
21854 20 20 73 65 74 51 75 6f 74 65 64 54 6f 6b 65 6e    setQuotedToken
21855 28 70 50 61 72 73 65 2c 20 26 70 4c 65 66 74 2d  (pParse, &pLeft-
21856 3e 74 6f 6b 65 6e 2c 20 7a 54 61 62 4e 61 6d 65  >token, zTabName
21857 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
21858 20 73 65 74 54 6f 6b 65 6e 28 26 70 45 78 70 72   setToken(&pExpr
21859 2d 3e 73 70 61 6e 2c 20 0a 20 20 20 20 20 20 20  ->span, .       
2185a 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2185b 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25  e3MPrintf(db, "%
2185c 73 2e 25 73 22 2c 20 7a 54 61 62 4e 61 6d 65 2c  s.%s", zTabName,
2185d 20 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 20 20   zName));.      
2185e 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 73          pExpr->s
2185f 70 61 6e 2e 64 79 6e 20 3d 20 31 3b 0a 20 20 20  pan.dyn = 1;.   
21860 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
21861 2d 3e 74 6f 6b 65 6e 2e 7a 20 3d 20 30 3b 0a 20  ->token.z = 0;. 
21862 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78               pEx
21863 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 20 3d 20 30 3b  pr->token.n = 0;
21864 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
21865 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 64 79 6e 20  Expr->token.dyn 
21866 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = 0;.           
21867 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
21868 20 20 20 20 20 20 70 45 78 70 72 20 3d 20 70 52        pExpr = pR
21869 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20 20 20  ight;.          
2186a 20 20 20 20 70 45 78 70 72 2d 3e 73 70 61 6e 20      pExpr->span 
2186b 3d 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 3b 0a  = pExpr->token;.
2186c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
2186d 78 70 72 2d 3e 73 70 61 6e 2e 64 79 6e 20 3d 20  xpr->span.dyn = 
2186e 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  0;.            }
2186f 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
21870 20 6c 6f 6e 67 4e 61 6d 65 73 20 29 7b 0a 20 20   longNames ){.  
21871 20 20 20 20 20 20 20 20 20 20 20 20 70 4e 65 77              pNew
21872 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
21873 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
21874 20 70 4e 65 77 2c 20 70 45 78 70 72 2c 20 26 70   pNew, pExpr, &p
21875 45 78 70 72 2d 3e 73 70 61 6e 29 3b 0a 20 20 20  Expr->span);.   
21876 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
21877 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4e                pN
21878 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ew = sqlite3Expr
21879 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
2187a 65 2c 20 70 4e 65 77 2c 20 70 45 78 70 72 2c 20  e, pNew, pExpr, 
2187b 26 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 29 3b  &pRight->token);
2187c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
2187d 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2187e 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
2187f 20 21 74 61 62 6c 65 53 65 65 6e 20 29 7b 0a 20   !tableSeen ){. 
21880 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 54 4e           if( zTN
21881 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ame ){.         
21882 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
21883 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73  sg(pParse, "no s
21884 75 63 68 20 74 61 62 6c 65 3a 20 25 73 22 2c 20  uch table: %s", 
21885 7a 54 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20  zTName);.       
21886 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
21887 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
21888 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
21889 6f 20 74 61 62 6c 65 73 20 73 70 65 63 69 66 69  o tables specifi
2188a 65 64 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ed");.          
2188b 7d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  }.          rc =
2188c 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   1;.        }.  
2188d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
2188e 65 65 28 7a 54 4e 61 6d 65 29 3b 0a 20 20 20 20  ee(zTName);.    
2188f 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71    }.    }.    sq
21890 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
21891 65 74 65 28 70 45 4c 69 73 74 29 3b 0a 20 20 20  ete(pEList);.   
21892 20 70 2d 3e 70 45 4c 69 73 74 20 3d 20 70 4e 65   p->pEList = pNe
21893 77 3b 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49 54  w;.  }.#if SQLIT
21894 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69  E_MAX_COLUMN.  i
21895 66 28 20 70 2d 3e 70 45 4c 69 73 74 20 26 26 20  f( p->pEList && 
21896 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  p->pEList->nExpr
21897 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  >db->aLimit[SQLI
21898 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d  TE_LIMIT_COLUMN]
21899 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
2189a 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2189b 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e  "too many column
2189c 73 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74 22  s in result set"
2189d 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
2189e 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 23 65  TE_ERROR;.  }.#e
2189f 6e 64 69 66 0a 20 20 69 66 28 20 64 62 2d 3e 6d  ndif.  if( db->m
218a0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
218a1 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
218a2 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 74 75  OMEM;.  }.  retu
218a3 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
218a4 70 45 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  pE is a pointer 
218a5 74 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  to an expression
218a6 20 77 68 69 63 68 20 69 73 20 61 20 73 69 6e 67   which is a sing
218a7 6c 65 20 74 65 72 6d 20 69 6e 0a 2a 2a 20 4f 52  le term in.** OR
218a8 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20  DER BY or GROUP 
218a9 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a  BY clause..**.**
218aa 20 49 66 20 70 45 20 65 76 61 6c 75 61 74 65 73   If pE evaluates
218ab 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 63   to an integer c
218ac 6f 6e 73 74 61 6e 74 20 69 2c 20 74 68 65 6e 20  onstant i, then 
218ad 72 65 74 75 72 6e 20 69 2e 0a 2a 2a 20 54 68 69  return i..** Thi
218ae 73 20 69 73 20 61 6e 20 69 6e 64 69 63 61 74 69  s is an indicati
218af 6f 6e 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72  on to the caller
218b0 20 74 68 61 74 20 69 74 20 73 68 6f 75 6c 64 20   that it should 
218b1 73 6f 72 74 0a 2a 2a 20 62 79 20 74 68 65 20 69  sort.** by the i
218b2 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  -th column of th
218b3 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2a  e result set..**
218b4 0a 2a 2a 20 49 66 20 70 45 20 69 73 20 61 20 77  .** If pE is a w
218b5 65 6c 6c 2d 66 6f 72 6d 65 64 20 65 78 70 72 65  ell-formed expre
218b6 73 73 69 6f 6e 20 61 6e 64 20 74 68 65 20 53 45  ssion and the SE
218b7 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 2a  LECT statement.*
218b8 2a 20 69 73 20 6e 6f 74 20 63 6f 6d 70 6f 75 6e  * is not compoun
218b9 64 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 30  d, then return 0
218ba 2e 20 20 54 68 69 73 20 69 6e 64 69 63 61 74 65  .  This indicate
218bb 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 61 6c 6c  s to the.** call
218bc 65 72 20 74 68 61 74 20 69 74 20 73 68 6f 75 6c  er that it shoul
218bd 64 20 73 6f 72 74 20 62 79 20 74 68 65 20 76 61  d sort by the va
218be 6c 75 65 20 6f 66 20 74 68 65 20 4f 52 44 45 52  lue of the ORDER
218bf 20 42 59 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f   BY.** expressio
218c0 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  n..**.** If the 
218c1 53 45 4c 45 43 54 20 69 73 20 63 6f 6d 70 6f 75  SELECT is compou
218c2 6e 64 2c 20 74 68 65 6e 20 61 74 74 65 6d 70 74  nd, then attempt
218c3 20 74 6f 20 6d 61 74 63 68 20 70 45 20 61 67 61   to match pE aga
218c4 69 6e 73 74 0a 2a 2a 20 72 65 73 75 6c 74 20 73  inst.** result s
218c5 65 74 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  et columns in th
218c6 65 20 6c 65 66 74 2d 6d 6f 73 74 20 53 45 4c 45  e left-most SELE
218c7 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 52  CT statement.  R
218c8 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 69 6e 64  eturn.** the ind
218c9 65 78 20 69 20 6f 66 20 74 68 65 20 6d 61 74 63  ex i of the matc
218ca 68 69 6e 67 20 63 6f 6c 75 6d 6e 2c 20 61 73 20  hing column, as 
218cb 61 6e 20 69 6e 64 69 63 61 74 69 6f 6e 20 74 6f  an indication to
218cc 20 74 68 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 20   the .** caller 
218cd 74 68 61 74 20 69 74 20 73 68 6f 75 6c 64 20 73  that it should s
218ce 6f 72 74 20 62 79 20 74 68 65 20 69 2d 74 68 20  ort by the i-th 
218cf 63 6f 6c 75 6d 6e 2e 20 20 49 66 20 74 68 65 72  column.  If ther
218d0 65 20 69 73 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68  e is.** no match
218d1 2c 20 72 65 74 75 72 6e 20 2d 31 20 61 6e 64 20  , return -1 and 
218d2 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d  leave an error m
218d3 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65  essage in pParse
218d4 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
218d5 6d 61 74 63 68 4f 72 64 65 72 42 79 54 65 72 6d  matchOrderByTerm
218d6 54 6f 45 78 70 72 4c 69 73 74 28 0a 20 20 50 61  ToExprList(.  Pa
218d7 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
218d8 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
218d9 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d 65  ext for error me
218da 73 73 61 67 65 73 20 2a 2f 0a 20 20 53 65 6c 65  ssages */.  Sele
218db 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 2f  ct *pSelect,   /
218dc 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61  * The SELECT sta
218dd 74 65 6d 65 6e 74 20 77 69 74 68 20 74 68 65 20  tement with the 
218de 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
218df 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 2c 20 20  */.  Expr *pE,  
218e0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73          /* The s
218e1 70 65 63 69 66 69 63 20 4f 52 44 45 52 20 42 59  pecific ORDER BY
218e2 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 69   term */.  int i
218e3 64 78 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  dx,           /*
218e4 20 57 68 65 6e 20 4f 52 44 45 52 20 42 59 20 74   When ORDER BY t
218e5 65 72 6d 20 69 73 20 74 68 69 73 20 2a 2f 0a 20  erm is this */. 
218e6 20 69 6e 74 20 69 73 43 6f 6d 70 6f 75 6e 64 2c   int isCompound,
218e7 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
218e8 68 69 73 20 69 73 20 61 20 63 6f 6d 70 6f 75 6e  his is a compoun
218e9 64 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 75 38  d SELECT */.  u8
218ea 20 2a 70 48 61 73 41 67 67 20 20 20 20 20 20 20   *pHasAgg       
218eb 20 2f 2a 20 54 72 75 65 20 69 66 20 65 78 70 72   /* True if expr
218ec 65 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20  ession contains 
218ed 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
218ee 6f 6e 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ons */.){.  int 
218ef 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  i;             /
218f0 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
218f1 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
218f2 4c 69 73 74 3b 20 20 2f 2a 20 54 68 65 20 63 6f  List;  /* The co
218f3 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 72 65 73  lumns of the res
218f4 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 4e 61 6d  ult set */.  Nam
218f5 65 43 6f 6e 74 65 78 74 20 6e 63 3b 20 20 20 20  eContext nc;    
218f6 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20  /* Name context 
218f7 66 6f 72 20 72 65 73 6f 6c 76 69 6e 67 20 70 45  for resolving pE
218f8 20 2a 2f 0a 0a 0a 20 20 2f 2a 20 49 66 20 74 68   */...  /* If th
218f9 65 20 74 65 72 6d 20 69 73 20 61 6e 20 69 6e 74  e term is an int
218fa 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 2c 20 72  eger constant, r
218fb 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20  eturn the value 
218fc 6f 66 20 74 68 61 74 0a 20 20 2a 2a 20 63 6f 6e  of that.  ** con
218fd 73 74 61 6e 74 20 2a 2f 0a 20 20 70 45 4c 69 73  stant */.  pELis
218fe 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c  t = pSelect->pEL
218ff 69 73 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  ist;.  if( sqlit
21900 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28  e3ExprIsInteger(
21901 70 45 2c 20 26 69 29 20 29 7b 0a 20 20 20 20 69  pE, &i) ){.    i
21902 66 28 20 69 3c 3d 30 20 29 7b 0a 20 20 20 20 20  f( i<=0 ){.     
21903 20 2f 2a 20 49 66 20 69 20 69 73 20 74 6f 6f 20   /* If i is too 
21904 73 6d 61 6c 6c 2c 20 6d 61 6b 65 20 69 74 20 74  small, make it t
21905 6f 6f 20 62 69 67 2e 20 20 54 68 61 74 20 77 61  oo big.  That wa
21906 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 20 20  y the calling.  
21907 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20      ** function 
21908 73 74 69 6c 6c 20 73 65 65 73 20 61 20 76 61 6c  still sees a val
21909 75 65 20 74 68 61 74 20 69 73 20 6f 75 74 20 6f  ue that is out o
2190a 66 20 72 61 6e 67 65 2c 20 62 75 74 20 64 6f 65  f range, but doe
2190b 73 0a 20 20 20 20 20 20 2a 2a 20 6e 6f 74 20 63  s.      ** not c
2190c 6f 6e 66 75 73 65 20 74 68 65 20 63 6f 6c 75 6d  onfuse the colum
2190d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 20 30 20  n number with 0 
2190e 6f 72 20 2d 31 20 72 65 73 75 6c 74 20 63 6f 64  or -1 result cod
2190f 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  e..      */.    
21910 20 20 69 20 3d 20 70 45 4c 69 73 74 2d 3e 6e 45    i = pEList->nE
21911 78 70 72 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 20  xpr+1;.    }.   
21912 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 7d 0a 0a   return i;.  }..
21913 20 20 2f 2a 20 49 66 20 74 68 65 20 74 65 72 6d    /* If the term
21914 20 69 73 20 61 20 73 69 6d 70 6c 65 20 69 64 65   is a simple ide
21915 6e 74 69 66 69 65 72 20 74 68 61 74 20 74 72 79  ntifier that try
21916 20 74 6f 20 6d 61 74 63 68 20 74 68 61 74 20 69   to match that i
21917 64 65 6e 74 69 66 69 65 72 0a 20 20 2a 2a 20 61  dentifier.  ** a
21918 67 61 69 6e 73 74 20 61 20 63 6f 6c 75 6d 6e 20  gainst a column 
21919 6e 61 6d 65 20 69 6e 20 74 68 65 20 72 65 73 75  name in the resu
2191a 6c 74 20 73 65 74 2e 0a 20 20 2a 2f 0a 20 20 69  lt set..  */.  i
2191b 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 49 44  f( pE->op==TK_ID
2191c 20 7c 7c 20 28 70 45 2d 3e 6f 70 3d 3d 54 4b 5f   || (pE->op==TK_
2191d 53 54 52 49 4e 47 20 26 26 20 70 45 2d 3e 74 6f  STRING && pE->to
2191e 6b 65 6e 2e 7a 5b 30 5d 21 3d 27 5c 27 27 29 20  ken.z[0]!='\'') 
2191f 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a  ){.    sqlite3 *
21920 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
21921 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20  .    char *zCol 
21922 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
21923 6d 54 6f 6b 65 6e 28 64 62 2c 20 26 70 45 2d 3e  mToken(db, &pE->
21924 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 69 66 28 20  token);.    if( 
21925 7a 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20  zCol==0 ){.     
21926 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20   return -1;.    
21927 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  }.    for(i=0; i
21928 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  <pEList->nExpr; 
21929 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72  i++){.      char
2192a 20 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74 2d 3e   *zAs = pEList->
2192b 61 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  a[i].zName;.    
2192c 20 20 69 66 28 20 7a 41 73 21 3d 30 20 26 26 20    if( zAs!=0 && 
2192d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
2192e 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a  As, zCol)==0 ){.
2192f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
21930 66 72 65 65 28 7a 43 6f 6c 29 3b 0a 20 20 20 20  free(zCol);.    
21931 20 20 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a      return i+1;.
21932 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
21933 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
21934 43 6f 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  Col);.  }..  /* 
21935 52 65 73 6f 6c 76 65 20 61 6c 6c 20 6e 61 6d 65  Resolve all name
21936 73 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42  s in the ORDER B
21937 59 20 74 65 72 6d 20 65 78 70 72 65 73 73 69 6f  Y term expressio
21938 6e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28  n.  */.  memset(
21939 26 6e 63 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6e  &nc, 0, sizeof(n
2193a 63 29 29 3b 0a 20 20 6e 63 2e 70 50 61 72 73 65  c));.  nc.pParse
2193b 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 6e 63 2e   = pParse;.  nc.
2193c 70 53 72 63 4c 69 73 74 20 3d 20 70 53 65 6c 65  pSrcList = pSele
2193d 63 74 2d 3e 70 53 72 63 3b 0a 20 20 6e 63 2e 70  ct->pSrc;.  nc.p
2193e 45 4c 69 73 74 20 3d 20 70 45 4c 69 73 74 3b 0a  EList = pEList;.
2193f 20 20 6e 63 2e 61 6c 6c 6f 77 41 67 67 20 3d 20    nc.allowAgg = 
21940 31 3b 0a 20 20 6e 63 2e 6e 45 72 72 20 3d 20 30  1;.  nc.nErr = 0
21941 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 45  ;.  if( sqlite3E
21942 78 70 72 52 65 73 6f 6c 76 65 4e 61 6d 65 73 28  xprResolveNames(
21943 26 6e 63 2c 20 70 45 29 20 29 7b 0a 20 20 20 20  &nc, pE) ){.    
21944 69 66 28 20 69 73 43 6f 6d 70 6f 75 6e 64 20 29  if( isCompound )
21945 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
21946 72 72 6f 72 43 6c 65 61 72 28 70 50 61 72 73 65  rrorClear(pParse
21947 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
21948 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  0;.    }else{.  
21949 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20      return -1;. 
2194a 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6e     }.  }.  if( n
2194b 63 2e 68 61 73 41 67 67 20 26 26 20 70 48 61 73  c.hasAgg && pHas
2194c 41 67 67 20 29 7b 0a 20 20 20 20 2a 70 48 61 73  Agg ){.    *pHas
2194d 41 67 67 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20  Agg = 1;.  }..  
2194e 2f 2a 20 46 6f 72 20 61 20 63 6f 6d 70 6f 75 6e  /* For a compoun
2194f 64 20 53 45 4c 45 43 54 2c 20 77 65 20 6e 65 65  d SELECT, we nee
21950 64 20 74 6f 20 74 72 79 20 74 6f 20 6d 61 74 63  d to try to matc
21951 68 20 74 68 65 20 4f 52 44 45 52 20 42 59 0a 20  h the ORDER BY. 
21952 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 61   ** expression a
21953 67 61 69 6e 73 74 20 61 6e 20 65 78 70 72 65 73  gainst an expres
21954 73 69 6f 6e 20 69 6e 20 74 68 65 20 72 65 73 75  sion in the resu
21955 6c 74 20 73 65 74 0a 20 20 2a 2f 0a 20 20 69 66  lt set.  */.  if
21956 28 20 69 73 43 6f 6d 70 6f 75 6e 64 20 29 7b 0a  ( isCompound ){.
21957 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
21958 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b  EList->nExpr; i+
21959 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71  +){.      if( sq
2195a 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65  lite3ExprCompare
2195b 28 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45  (pEList->a[i].pE
2195c 78 70 72 2c 20 70 45 29 20 29 7b 0a 20 20 20 20  xpr, pE) ){.    
2195d 20 20 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a      return i+1;.
2195e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2195f 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
21960 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 7a 65 20  ../*.** Analyze 
21961 61 6e 64 20 4f 52 44 45 52 20 42 59 20 6f 72 20  and ORDER BY or 
21962 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20  GROUP BY clause 
21963 69 6e 20 61 20 73 69 6d 70 6c 65 20 53 45 4c 45  in a simple SELE
21964 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  CT statement..**
21965 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
21966 65 72 20 6f 66 20 65 72 72 6f 72 73 20 73 65 65  er of errors see
21967 6e 2e 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 74  n..**.** Every t
21968 65 72 6d 20 6f 66 20 74 68 65 20 4f 52 44 45 52  erm of the ORDER
21969 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20   BY or GROUP BY 
2196a 63 6c 61 75 73 65 20 6e 65 65 64 73 20 74 6f 20  clause needs to 
2196b 62 65 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73  be an.** express
2196c 69 6f 6e 2e 20 20 49 66 20 61 6e 79 20 65 78 70  ion.  If any exp
2196d 72 65 73 73 69 6f 6e 20 69 73 20 61 6e 20 69 6e  ression is an in
2196e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 2c 20  teger constant, 
2196f 74 68 65 6e 0a 2a 2a 20 74 68 61 74 20 65 78 70  then.** that exp
21970 72 65 73 73 69 6f 6e 20 69 73 20 72 65 70 6c 61  ression is repla
21971 63 65 64 20 62 79 20 74 68 65 20 63 6f 72 72 65  ced by the corre
21972 73 70 6f 6e 64 69 6e 67 20 0a 2a 2a 20 65 78 70  sponding .** exp
21973 72 65 73 73 69 6f 6e 20 66 72 6f 6d 20 74 68 65  ression from the
21974 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a   result set..*/.
21975 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 63 65  static int proce
21976 73 73 4f 72 64 65 72 47 72 6f 75 70 42 79 28 0a  ssOrderGroupBy(.
21977 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
21978 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69          /* Parsi
21979 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20 4c 65 61  ng context.  Lea
2197a 76 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  ve error message
2197b 73 20 68 65 72 65 20 2a 2f 0a 20 20 53 65 6c 65  s here */.  Sele
2197c 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20  ct *pSelect,    
2197d 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20    /* The SELECT 
2197e 73 74 61 74 65 6d 65 6e 74 20 63 6f 6e 74 61 69  statement contai
2197f 6e 69 6e 67 20 74 68 65 20 63 6c 61 75 73 65 20  ning the clause 
21980 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
21981 4f 72 64 65 72 42 79 2c 20 20 20 2f 2a 20 54 68  OrderBy,   /* Th
21982 65 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52  e ORDER BY or GR
21983 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 74 6f  OUP BY clause to
21984 20 62 65 20 70 72 6f 63 65 73 73 65 64 20 2a 2f   be processed */
21985 0a 20 20 69 6e 74 20 69 73 4f 72 64 65 72 2c 20  .  int isOrder, 
21986 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66 6f           /* 1 fo
21987 72 20 4f 52 44 45 52 20 42 59 2e 20 20 30 20 66  r ORDER BY.  0 f
21988 6f 72 20 47 52 4f 55 50 20 42 59 20 2a 2f 0a 20  or GROUP BY */. 
21989 20 75 38 20 2a 70 48 61 73 41 67 67 20 20 20 20   u8 *pHasAgg    
2198a 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 6f         /* Set to
2198b 20 54 52 55 45 20 69 66 20 61 6e 79 20 74 65 72   TRUE if any ter
2198c 6d 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 61 67  m contains an ag
2198d 67 72 65 67 61 74 65 20 2a 2f 0a 29 7b 0a 20 20  gregate */.){.  
2198e 69 6e 74 20 69 3b 0a 20 20 73 71 6c 69 74 65 33  int i;.  sqlite3
2198f 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
21990 62 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  b;.  ExprList *p
21991 45 4c 69 73 74 3b 0a 0a 20 20 69 66 28 20 70 4f  EList;..  if( pO
21992 72 64 65 72 42 79 3d 3d 30 20 7c 7c 20 70 50 61  rderBy==0 || pPa
21993 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rse->db->mallocF
21994 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 20 30  ailed ) return 0
21995 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  ;.#if SQLITE_MAX
21996 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 70 4f  _COLUMN.  if( pO
21997 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3e 64 62  rderBy->nExpr>db
21998 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
21999 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b  LIMIT_COLUMN] ){
2199a 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
2199b 2a 7a 54 79 70 65 20 3d 20 69 73 4f 72 64 65 72  *zType = isOrder
2199c 20 3f 20 22 4f 52 44 45 52 22 20 3a 20 22 47 52   ? "ORDER" : "GR
2199d 4f 55 50 22 3b 0a 20 20 20 20 73 71 6c 69 74 65  OUP";.    sqlite
2199e 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2199f 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d  , "too many term
219a0 73 20 69 6e 20 25 73 20 42 59 20 63 6c 61 75 73  s in %s BY claus
219a1 65 22 2c 20 7a 54 79 70 65 29 3b 0a 20 20 20 20  e", zType);.    
219a2 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65  return 1;.  }.#e
219a3 6e 64 69 66 0a 20 20 70 45 4c 69 73 74 20 3d 20  ndif.  pEList = 
219a4 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b  pSelect->pEList;
219a5 0a 20 20 69 66 28 20 70 45 4c 69 73 74 3d 3d 30  .  if( pEList==0
219a6 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
219a7 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b  ;.  }.  for(i=0;
219a8 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78   i<pOrderBy->nEx
219a9 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e  pr; i++){.    in
219aa 74 20 69 43 6f 6c 3b 0a 20 20 20 20 45 78 70 72  t iCol;.    Expr
219ab 20 2a 70 45 20 3d 20 70 4f 72 64 65 72 42 79 2d   *pE = pOrderBy-
219ac 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[i].pExpr;.   
219ad 20 69 43 6f 6c 20 3d 20 6d 61 74 63 68 4f 72 64   iCol = matchOrd
219ae 65 72 42 79 54 65 72 6d 54 6f 45 78 70 72 4c 69  erByTermToExprLi
219af 73 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65  st(pParse, pSele
219b0 63 74 2c 20 70 45 2c 20 69 2b 31 2c 20 30 2c 20  ct, pE, i+1, 0, 
219b1 70 48 61 73 41 67 67 29 3b 0a 20 20 20 20 69 66  pHasAgg);.    if
219b2 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20  ( iCol<0 ){.    
219b3 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
219b4 7d 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 70  }.    if( iCol>p
219b5 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a  EList->nExpr ){.
219b6 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
219b7 20 2a 7a 54 79 70 65 20 3d 20 69 73 4f 72 64 65   *zType = isOrde
219b8 72 20 3f 20 22 4f 52 44 45 52 22 20 3a 20 22 47  r ? "ORDER" : "G
219b9 52 4f 55 50 22 3b 0a 20 20 20 20 20 20 73 71 6c  ROUP";.      sql
219ba 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
219bb 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 22  rse, .         "
219bc 25 72 20 25 73 20 42 59 20 74 65 72 6d 20 6f 75  %r %s BY term ou
219bd 74 20 6f 66 20 72 61 6e 67 65 20 2d 20 73 68 6f  t of range - sho
219be 75 6c 64 20 62 65 20 22 0a 20 20 20 20 20 20 20  uld be ".       
219bf 20 20 22 62 65 74 77 65 65 6e 20 31 20 61 6e 64    "between 1 and
219c0 20 25 64 22 2c 20 69 2b 31 2c 20 7a 54 79 70 65   %d", i+1, zType
219c1 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29  , pEList->nExpr)
219c2 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  ;.      return 1
219c3 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
219c4 69 43 6f 6c 3e 30 20 29 7b 0a 20 20 20 20 20 20  iCol>0 ){.      
219c5 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d  CollSeq *pColl =
219c6 20 70 45 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20   pE->pColl;.    
219c7 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 45    int flags = pE
219c8 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70  ->flags & EP_Exp
219c9 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20 73  Collate;.      s
219ca 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
219cb 28 70 45 29 3b 0a 20 20 20 20 20 20 70 45 20 3d  (pE);.      pE =
219cc 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
219cd 64 62 2c 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43  db, pEList->a[iC
219ce 6f 6c 2d 31 5d 2e 70 45 78 70 72 29 3b 0a 20 20  ol-1].pExpr);.  
219cf 20 20 20 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b      pOrderBy->a[
219d0 69 5d 2e 70 45 78 70 72 20 3d 20 70 45 3b 0a 20  i].pExpr = pE;. 
219d1 20 20 20 20 20 69 66 28 20 70 45 20 26 26 20 70       if( pE && p
219d2 43 6f 6c 6c 20 26 26 20 66 6c 61 67 73 20 29 7b  Coll && flags ){
219d3 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 70 43 6f  .        pE->pCo
219d4 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20  ll = pColl;.    
219d5 20 20 20 20 70 45 2d 3e 66 6c 61 67 73 20 7c 3d      pE->flags |=
219d6 20 66 6c 61 67 73 3b 0a 20 20 20 20 20 20 7d 0a   flags;.      }.
219d7 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
219d8 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  rn 0;.}../*.** A
219d9 6e 61 6c 79 7a 65 20 61 6e 64 20 4f 52 44 45 52  nalyze and ORDER
219da 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20   BY or GROUP BY 
219db 63 6c 61 75 73 65 20 69 6e 20 61 20 53 45 4c 45  clause in a SELE
219dc 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 52  CT statement.  R
219dd 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d  eturn.** the num
219de 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 73 65  ber of errors se
219df 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72  en..**.** The pr
219e0 6f 63 65 73 73 69 6e 67 20 64 65 70 65 6e 64 73  ocessing depends
219e1 20 6f 6e 20 77 68 65 74 68 65 72 20 74 68 65 20   on whether the 
219e2 53 45 4c 45 43 54 20 69 73 20 73 69 6d 70 6c 65  SELECT is simple
219e3 20 6f 72 20 63 6f 6d 70 6f 75 6e 64 2e 0a 2a 2a   or compound..**
219e4 20 46 6f 72 20 61 20 73 69 6d 70 6c 65 20 53 45   For a simple SE
219e5 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2c 20  LECT statement, 
219e6 65 76 72 79 20 74 65 72 6d 20 6f 66 20 74 68 65  evry term of the
219e7 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f   ORDER BY or GRO
219e8 55 50 20 42 59 0a 2a 2a 20 63 6c 61 75 73 65 20  UP BY.** clause 
219e9 6e 65 65 64 73 20 74 6f 20 62 65 20 61 6e 20 65  needs to be an e
219ea 78 70 72 65 73 73 69 6f 6e 2e 20 20 49 66 20 61  xpression.  If a
219eb 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  ny expression is
219ec 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 63   an integer.** c
219ed 6f 6e 73 74 61 6e 74 2c 20 74 68 65 6e 20 74 68  onstant, then th
219ee 61 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  at expression is
219ef 20 72 65 70 6c 61 63 65 64 20 62 79 20 74 68 65   replaced by the
219f0 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 0a   corresponding .
219f1 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 66 72  ** expression fr
219f2 6f 6d 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  om the result se
219f3 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 63 6f 6d  t..**.** For com
219f4 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 73 74 61  pound SELECT sta
219f5 74 65 6d 65 6e 74 73 2c 20 65 76 65 72 79 20 65  tements, every e
219f6 78 70 72 65 73 73 69 6f 6e 20 6e 65 65 64 73 20  xpression needs 
219f7 74 6f 20 62 65 20 6f 66 0a 2a 2a 20 74 79 70 65  to be of.** type
219f8 20 54 4b 5f 43 4f 4c 55 4d 4e 20 77 69 74 68 20   TK_COLUMN with 
219f9 61 20 69 54 61 62 6c 65 20 76 61 6c 75 65 20 61  a iTable value a
219fa 73 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 34  s given in the 4
219fb 74 68 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a  th parameter..**
219fc 20 49 66 20 61 6e 79 20 65 78 70 72 65 73 73 69   If any expressi
219fd 6f 6e 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72  on is an integer
219fe 2c 20 74 68 61 74 20 62 65 63 6f 6d 65 73 20 74  , that becomes t
219ff 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  he column number
21a00 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20  ..** Otherwise, 
21a01 6d 61 74 63 68 20 74 68 65 20 65 78 70 72 65 73  match the expres
21a02 73 69 6f 6e 20 61 67 61 69 6e 73 74 20 72 65 73  sion against res
21a03 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20  ult set columns 
21a04 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 6c 65 66 74  from.** the left
21a05 2d 6d 6f 73 74 20 53 45 4c 45 43 54 2e 0a 2a 2f  -most SELECT..*/
21a06 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 63  .static int proc
21a07 65 73 73 43 6f 6d 70 6f 75 6e 64 4f 72 64 65 72  essCompoundOrder
21a08 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  By(.  Parse *pPa
21a09 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  rse,        /* P
21a0a 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20  arsing context. 
21a0b 20 4c 65 61 76 65 20 65 72 72 6f 72 20 6d 65 73   Leave error mes
21a0c 73 61 67 65 73 20 68 65 72 65 20 2a 2f 0a 20 20  sages here */.  
21a0d 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c  Select *pSelect,
21a0e 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c        /* The SEL
21a0f 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 63 6f  ECT statement co
21a10 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 4f 52 44  ntaining the ORD
21a11 45 52 20 42 59 20 2a 2f 0a 20 20 69 6e 74 20 69  ER BY */.  int i
21a12 54 61 62 6c 65 20 20 20 20 20 20 20 20 20 20 20  Table           
21a13 20 2f 2a 20 4f 75 74 70 75 74 20 74 61 62 6c 65   /* Output table
21a14 20 66 6f 72 20 63 6f 6d 70 6f 75 6e 64 20 53 45   for compound SE
21a15 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20  LECT statements 
21a16 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  */.){.  int i;. 
21a17 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
21a18 72 42 79 3b 0a 20 20 45 78 70 72 4c 69 73 74 20  rBy;.  ExprList 
21a19 2a 70 45 4c 69 73 74 3b 0a 20 20 73 71 6c 69 74  *pEList;.  sqlit
21a1a 65 33 20 2a 64 62 3b 0a 20 20 69 6e 74 20 6d 6f  e3 *db;.  int mo
21a1b 72 65 54 6f 44 6f 20 3d 20 31 3b 0a 0a 20 20 70  reToDo = 1;..  p
21a1c 4f 72 64 65 72 42 79 20 3d 20 70 53 65 6c 65 63  OrderBy = pSelec
21a1d 74 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 69  t->pOrderBy;.  i
21a1e 66 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29  f( pOrderBy==0 )
21a1f 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 64 62 20   return 0;.  db 
21a20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23 69  = pParse->db;.#i
21a21 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c  f SQLITE_MAX_COL
21a22 55 4d 4e 0a 20 20 69 66 28 20 70 4f 72 64 65 72  UMN.  if( pOrder
21a23 42 79 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 4c  By->nExpr>db->aL
21a24 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
21a25 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20  T_COLUMN] ){.   
21a26 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
21a27 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61  (pParse, "too ma
21a28 6e 79 20 74 65 72 6d 73 20 69 6e 20 4f 52 44 45  ny terms in ORDE
21a29 52 20 42 59 20 63 6c 61 75 73 65 22 29 3b 0a 20  R BY clause");. 
21a2a 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
21a2b 0a 23 65 6e 64 69 66 0a 20 20 66 6f 72 28 69 3d  .#endif.  for(i=
21a2c 30 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e  0; i<pOrderBy->n
21a2d 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
21a2e 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64  pOrderBy->a[i].d
21a2f 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 77  one = 0;.  }.  w
21a30 68 69 6c 65 28 20 70 53 65 6c 65 63 74 2d 3e 70  hile( pSelect->p
21a31 50 72 69 6f 72 20 29 7b 0a 20 20 20 20 70 53 65  Prior ){.    pSe
21a32 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e  lect = pSelect->
21a33 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 77 68  pPrior;.  }.  wh
21a34 69 6c 65 28 20 70 53 65 6c 65 63 74 20 26 26 20  ile( pSelect && 
21a35 6d 6f 72 65 54 6f 44 6f 20 29 7b 0a 20 20 20 20  moreToDo ){.    
21a36 6d 6f 72 65 54 6f 44 6f 20 3d 20 30 3b 0a 20 20  moreToDo = 0;.  
21a37 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72    for(i=0; i<pOr
21a38 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b  derBy->nExpr; i+
21a39 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43  +){.      int iC
21a3a 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 45  ol = -1;.      E
21a3b 78 70 72 20 2a 70 45 2c 20 2a 70 44 75 70 3b 0a  xpr *pE, *pDup;.
21a3c 20 20 20 20 20 20 69 66 28 20 70 4f 72 64 65 72        if( pOrder
21a3d 42 79 2d 3e 61 5b 69 5d 2e 64 6f 6e 65 20 29 20  By->a[i].done ) 
21a3e 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
21a3f 70 45 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61  pE = pOrderBy->a
21a40 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  [i].pExpr;.     
21a41 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45   pDup = sqlite3E
21a42 78 70 72 44 75 70 28 64 62 2c 20 70 45 29 3b 0a  xprDup(db, pE);.
21a43 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d        if( !db->m
21a44 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
21a45 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70 44         assert(pD
21a46 75 70 29 3b 0a 20 20 20 20 20 20 20 20 69 43 6f  up);.        iCo
21a47 6c 20 3d 20 6d 61 74 63 68 4f 72 64 65 72 42 79  l = matchOrderBy
21a48 54 65 72 6d 54 6f 45 78 70 72 4c 69 73 74 28 70  TermToExprList(p
21a49 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20  Parse, pSelect, 
21a4a 70 44 75 70 2c 20 69 2b 31 2c 20 31 2c 20 30 29  pDup, i+1, 1, 0)
21a4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
21a4c 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
21a4d 65 28 70 44 75 70 29 3b 0a 20 20 20 20 20 20 69  e(pDup);.      i
21a4e 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20  f( iCol<0 ){.   
21a4f 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
21a50 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 45 4c       }.      pEL
21a51 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70  ist = pSelect->p
21a52 45 4c 69 73 74 3b 0a 20 20 20 20 20 20 69 66 28  EList;.      if(
21a53 20 70 45 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20   pEList==0 ){.  
21a54 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
21a55 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
21a56 28 20 69 43 6f 6c 3e 70 45 4c 69 73 74 2d 3e 6e  ( iCol>pEList->n
21a57 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20  Expr ){.        
21a58 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
21a59 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20  pParse, .       
21a5a 20 20 20 20 22 25 72 20 4f 52 44 45 52 20 42 59      "%r ORDER BY
21a5b 20 74 65 72 6d 20 6f 75 74 20 6f 66 20 72 61 6e   term out of ran
21a5c 67 65 20 2d 20 73 68 6f 75 6c 64 20 62 65 20 22  ge - should be "
21a5d 0a 20 20 20 20 20 20 20 20 20 20 20 22 62 65 74  .           "bet
21a5e 77 65 65 6e 20 31 20 61 6e 64 20 25 64 22 2c 20  ween 1 and %d", 
21a5f 69 2b 31 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78  i+1, pEList->nEx
21a60 70 72 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  pr);.        ret
21a61 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 1;.      }. 
21a62 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 30 20       if( iCol>0 
21a63 29 7b 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 6f  ){.        pE->o
21a64 70 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20  p = TK_COLUMN;. 
21a65 20 20 20 20 20 20 20 70 45 2d 3e 69 54 61 62 6c         pE->iTabl
21a66 65 20 3d 20 69 54 61 62 6c 65 3b 0a 20 20 20 20  e = iTable;.    
21a67 20 20 20 20 70 45 2d 3e 69 41 67 67 20 3d 20 2d      pE->iAgg = -
21a68 31 3b 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 69  1;.        pE->i
21a69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 2d 31 3b  Column = iCol-1;
21a6a 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 70 54 61  .        pE->pTa
21a6b 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70  b = 0;.        p
21a6c 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f  OrderBy->a[i].do
21a6d 6e 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65  ne = 1;.      }e
21a6e 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6d 6f 72  lse{.        mor
21a6f 65 54 6f 44 6f 20 3d 20 31 3b 0a 20 20 20 20 20  eToDo = 1;.     
21a70 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 65   }.    }.    pSe
21a71 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e  lect = pSelect->
21a72 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 66 6f 72  pNext;.  }.  for
21a73 28 69 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42 79  (i=0; i<pOrderBy
21a74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20  ->nExpr; i++){. 
21a75 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 2d     if( pOrderBy-
21a76 3e 61 5b 69 5d 2e 64 6f 6e 65 3d 3d 30 20 29 7b  >a[i].done==0 ){
21a77 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
21a78 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
21a79 25 72 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d  %r ORDER BY term
21a7a 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20   does not match 
21a7b 61 6e 79 20 22 0a 20 20 20 20 20 20 20 20 20 20  any ".          
21a7c 20 20 22 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65    "column in the
21a7d 20 72 65 73 75 6c 74 20 73 65 74 22 2c 20 69 2b   result set", i+
21a7e 31 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  1);.      return
21a7f 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
21a80 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
21a81 2a 2a 20 47 65 74 20 61 20 56 44 42 45 20 66 6f  ** Get a VDBE fo
21a82 72 20 74 68 65 20 67 69 76 65 6e 20 70 61 72 73  r the given pars
21a83 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 43 72 65  er context.  Cre
21a84 61 74 65 20 61 20 6e 65 77 20 6f 6e 65 20 69 66  ate a new one if
21a85 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 49   necessary..** I
21a86 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
21a87 73 2c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 20 61  s, return NULL a
21a88 6e 64 20 6c 65 61 76 65 20 61 20 6d 65 73 73 61  nd leave a messa
21a89 67 65 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2f  ge in pParse..*/
21a8a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
21a8b 56 64 62 65 20 2a 73 71 6c 69 74 65 33 47 65 74  Vdbe *sqlite3Get
21a8c 56 64 62 65 28 50 61 72 73 65 20 2a 70 50 61 72  Vdbe(Parse *pPar
21a8d 73 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  se){.  Vdbe *v =
21a8e 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
21a8f 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20    if( v==0 ){.  
21a90 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56    v = pParse->pV
21a91 64 62 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62  dbe = sqlite3Vdb
21a92 65 43 72 65 61 74 65 28 70 50 61 72 73 65 2d 3e  eCreate(pParse->
21a93 64 62 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  db);.#ifndef SQL
21a94 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20  ITE_OMIT_TRACE. 
21a95 20 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20     if( v ){.    
21a96 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21a97 4f 70 30 28 76 2c 20 4f 50 5f 54 72 61 63 65 29  Op0(v, OP_Trace)
21a98 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
21a99 20 7d 0a 20 20 72 65 74 75 72 6e 20 76 3b 0a 7d   }.  return v;.}
21a9a 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65  .../*.** Compute
21a9b 20 74 68 65 20 69 4c 69 6d 69 74 20 61 6e 64 20   the iLimit and 
21a9c 69 4f 66 66 73 65 74 20 66 69 65 6c 64 73 20 6f  iOffset fields o
21a9d 66 20 74 68 65 20 53 45 4c 45 43 54 20 62 61 73  f the SELECT bas
21a9e 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 70 4c 69  ed on the.** pLi
21a9f 6d 69 74 20 61 6e 64 20 70 4f 66 66 73 65 74 20  mit and pOffset 
21aa0 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 20 70 4c  expressions.  pL
21aa1 69 6d 69 74 20 61 6e 64 20 70 4f 66 66 73 65 74  imit and pOffset
21aa2 20 68 6f 6c 64 20 74 68 65 20 65 78 70 72 65 73   hold the expres
21aa3 73 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 61 70  sions.** that ap
21aa4 70 65 61 72 20 69 6e 20 74 68 65 20 6f 72 69 67  pear in the orig
21aa5 69 6e 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65  inal SQL stateme
21aa6 6e 74 20 61 66 74 65 72 20 74 68 65 20 4c 49 4d  nt after the LIM
21aa7 49 54 20 61 6e 64 20 4f 46 46 53 45 54 0a 2a 2a  IT and OFFSET.**
21aa8 20 6b 65 79 77 6f 72 64 73 2e 20 20 4f 72 20 4e   keywords.  Or N
21aa9 55 4c 4c 20 69 66 20 74 68 6f 73 65 20 6b 65 79  ULL if those key
21aaa 77 6f 72 64 73 20 61 72 65 20 6f 6d 69 74 74 65  words are omitte
21aab 64 2e 20 69 4c 69 6d 69 74 20 61 6e 64 20 69 4f  d. iLimit and iO
21aac 66 66 73 65 74 20 0a 2a 2a 20 61 72 65 20 74 68  ffset .** are th
21aad 65 20 69 6e 74 65 67 65 72 20 6d 65 6d 6f 72 79  e integer memory
21aae 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72   register number
21aaf 73 20 66 6f 72 20 63 6f 75 6e 74 65 72 73 20 75  s for counters u
21ab0 73 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 0a  sed to compute .
21ab1 2a 2a 20 74 68 65 20 6c 69 6d 69 74 20 61 6e 64  ** the limit and
21ab2 20 6f 66 66 73 65 74 2e 20 20 49 66 20 74 68 65   offset.  If the
21ab3 72 65 20 69 73 20 6e 6f 20 6c 69 6d 69 74 20 61  re is no limit a
21ab4 6e 64 2f 6f 72 20 6f 66 66 73 65 74 2c 20 74 68  nd/or offset, th
21ab5 65 6e 20 0a 2a 2a 20 69 4c 69 6d 69 74 20 61 6e  en .** iLimit an
21ab6 64 20 69 4f 66 66 73 65 74 20 61 72 65 20 6e 65  d iOffset are ne
21ab7 67 61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  gative..**.** Th
21ab8 69 73 20 72 6f 75 74 69 6e 65 20 63 68 61 6e 67  is routine chang
21ab9 65 73 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66  es the values of
21aba 20 69 4c 69 6d 69 74 20 61 6e 64 20 69 4f 66 66   iLimit and iOff
21abb 73 65 74 20 6f 6e 6c 79 20 69 66 0a 2a 2a 20 61  set only if.** a
21abc 20 6c 69 6d 69 74 20 6f 72 20 6f 66 66 73 65 74   limit or offset
21abd 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 70   is defined by p
21abe 4c 69 6d 69 74 20 61 6e 64 20 70 4f 66 66 73 65  Limit and pOffse
21abf 74 2e 20 20 69 4c 69 6d 69 74 20 61 6e 64 0a 2a  t.  iLimit and.*
21ac0 2a 20 69 4f 66 66 73 65 74 20 73 68 6f 75 6c 64  * iOffset should
21ac1 20 68 61 76 65 20 62 65 65 6e 20 70 72 65 73 65   have been prese
21ac2 74 20 74 6f 20 61 70 70 72 6f 70 72 69 61 74 65  t to appropriate
21ac3 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 73 0a   default values.
21ac4 2a 2a 20 28 75 73 75 61 6c 6c 79 20 62 75 74 20  ** (usually but 
21ac5 6e 6f 74 20 61 6c 77 61 79 73 20 2d 31 29 20 70  not always -1) p
21ac6 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20  rior to calling 
21ac7 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a  this routine..**
21ac8 20 4f 6e 6c 79 20 69 66 20 70 4c 69 6d 69 74 21   Only if pLimit!
21ac9 3d 30 20 6f 72 20 70 4f 66 66 73 65 74 21 3d 30  =0 or pOffset!=0
21aca 20 64 6f 20 74 68 65 20 6c 69 6d 69 74 20 72 65   do the limit re
21acb 67 69 73 74 65 72 73 20 67 65 74 0a 2a 2a 20 72  gisters get.** r
21acc 65 64 65 66 69 6e 65 64 2e 20 20 54 68 65 20 55  edefined.  The U
21acd 4e 49 4f 4e 20 41 4c 4c 20 6f 70 65 72 61 74 6f  NION ALL operato
21ace 72 20 75 73 65 73 20 74 68 69 73 20 70 72 6f 70  r uses this prop
21acf 65 72 74 79 20 74 6f 20 66 6f 72 63 65 0a 2a 2a  erty to force.**
21ad0 20 74 68 65 20 72 65 75 73 65 20 6f 66 20 74 68   the reuse of th
21ad1 65 20 73 61 6d 65 20 6c 69 6d 69 74 20 61 6e 64  e same limit and
21ad2 20 6f 66 66 73 65 74 20 72 65 67 69 73 74 65 72   offset register
21ad3 73 20 61 63 72 6f 73 73 20 6d 75 6c 74 69 70 6c  s across multipl
21ad4 65 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74  e.** SELECT stat
21ad5 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69  ements..*/.stati
21ad6 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 4c 69  c void computeLi
21ad7 6d 69 74 52 65 67 69 73 74 65 72 73 28 50 61 72  mitRegisters(Par
21ad8 73 65 20 2a 70 50 61 72 73 65 2c 20 53 65 6c 65  se *pParse, Sele
21ad9 63 74 20 2a 70 2c 20 69 6e 74 20 69 42 72 65 61  ct *p, int iBrea
21ada 6b 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  k){.  Vdbe *v = 
21adb 30 3b 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 20  0;.  int iLimit 
21adc 3d 20 30 3b 0a 20 20 69 6e 74 20 69 4f 66 66 73  = 0;.  int iOffs
21add 65 74 3b 0a 20 20 69 6e 74 20 61 64 64 72 31 3b  et;.  int addr1;
21ade 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 22 4c 49  ..  /* .  ** "LI
21adf 4d 49 54 20 2d 31 22 20 61 6c 77 61 79 73 20 73  MIT -1" always s
21ae0 68 6f 77 73 20 61 6c 6c 20 72 6f 77 73 2e 20 20  hows all rows.  
21ae1 54 68 65 72 65 20 69 73 20 73 6f 6d 65 0a 20 20  There is some.  
21ae2 2a 2a 20 63 6f 6e 74 72 61 76 65 72 73 79 20 61  ** contraversy a
21ae3 62 6f 75 74 20 77 68 61 74 20 74 68 65 20 63 6f  bout what the co
21ae4 72 72 65 63 74 20 62 65 68 61 76 69 6f 72 20 73  rrect behavior s
21ae5 68 6f 75 6c 64 20 62 65 2e 0a 20 20 2a 2a 20 54  hould be..  ** T
21ae6 68 65 20 63 75 72 72 65 6e 74 20 69 6d 70 6c 65  he current imple
21ae7 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 74 65 72 70  mentation interp
21ae8 72 65 74 73 20 22 4c 49 4d 49 54 20 30 22 20 74  rets "LIMIT 0" t
21ae9 6f 20 6d 65 61 6e 0a 20 20 2a 2a 20 6e 6f 20 72  o mean.  ** no r
21aea 6f 77 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ows..  */.  if( 
21aeb 70 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20  p->pLimit ){.   
21aec 20 70 2d 3e 69 4c 69 6d 69 74 20 3d 20 69 4c 69   p->iLimit = iLi
21aed 6d 69 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  mit = ++pParse->
21aee 6e 4d 65 6d 3b 0a 20 20 20 20 76 20 3d 20 73 71  nMem;.    v = sq
21aef 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
21af0 72 73 65 29 3b 0a 20 20 20 20 69 66 28 20 76 3d  rse);.    if( v=
21af1 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20  =0 ) return;.   
21af2 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
21af3 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 4c 69 6d  (pParse, p->pLim
21af4 69 74 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20  it, iLimit);.   
21af5 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21af6 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49  p1(v, OP_MustBeI
21af7 6e 74 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20  nt, iLimit);.   
21af8 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
21af9 20 22 4c 49 4d 49 54 20 63 6f 75 6e 74 65 72 22   "LIMIT counter"
21afa 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ));.    sqlite3V
21afb 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
21afc 49 66 5a 65 72 6f 2c 20 69 4c 69 6d 69 74 2c 20  IfZero, iLimit, 
21afd 69 42 72 65 61 6b 29 3b 0a 20 20 7d 0a 20 20 69  iBreak);.  }.  i
21afe 66 28 20 70 2d 3e 70 4f 66 66 73 65 74 20 29 7b  f( p->pOffset ){
21aff 0a 20 20 20 20 70 2d 3e 69 4f 66 66 73 65 74 20  .    p->iOffset 
21b00 3d 20 69 4f 66 66 73 65 74 20 3d 20 2b 2b 70 50  = iOffset = ++pP
21b01 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
21b02 69 66 28 20 70 2d 3e 70 4c 69 6d 69 74 20 29 7b  if( p->pLimit ){
21b03 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  .      pParse->n
21b04 4d 65 6d 2b 2b 3b 20 20 20 2f 2a 20 41 6c 6c 6f  Mem++;   /* Allo
21b05 63 61 74 65 20 61 6e 20 65 78 74 72 61 20 72 65  cate an extra re
21b06 67 69 73 74 65 72 20 66 6f 72 20 6c 69 6d 69 74  gister for limit
21b07 2b 6f 66 66 73 65 74 20 2a 2f 0a 20 20 20 20 7d  +offset */.    }
21b08 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33  .    v = sqlite3
21b09 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
21b0a 0a 20 20 20 20 69 66 28 20 76 3d 3d 30 20 29 20  .    if( v==0 ) 
21b0b 72 65 74 75 72 6e 3b 0a 20 20 20 20 73 71 6c 69  return;.    sqli
21b0c 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
21b0d 73 65 2c 20 70 2d 3e 70 4f 66 66 73 65 74 2c 20  se, p->pOffset, 
21b0e 69 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 73 71  iOffset);.    sq
21b0f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
21b10 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c  v, OP_MustBeInt,
21b11 20 69 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 56   iOffset);.    V
21b12 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
21b13 4f 46 46 53 45 54 20 63 6f 75 6e 74 65 72 22 29  OFFSET counter")
21b14 29 3b 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73  );.    addr1 = s
21b15 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
21b16 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 69 4f  (v, OP_IfPos, iO
21b17 66 66 73 65 74 29 3b 0a 20 20 20 20 73 71 6c 69  ffset);.    sqli
21b18 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
21b19 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20   OP_Integer, 0, 
21b1a 69 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 73 71  iOffset);.    sq
21b1b 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
21b1c 65 28 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 20  e(v, addr1);.   
21b1d 20 69 66 28 20 70 2d 3e 70 4c 69 6d 69 74 20 29   if( p->pLimit )
21b1e 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
21b1f 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
21b20 41 64 64 2c 20 69 4c 69 6d 69 74 2c 20 69 4f 66  Add, iLimit, iOf
21b21 66 73 65 74 2c 20 69 4f 66 66 73 65 74 2b 31 29  fset, iOffset+1)
21b22 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d  ;.      VdbeComm
21b23 65 6e 74 28 28 76 2c 20 22 4c 49 4d 49 54 2b 4f  ent((v, "LIMIT+O
21b24 46 46 53 45 54 22 29 29 3b 0a 20 20 20 20 20 20  FFSET"));.      
21b25 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56  addr1 = sqlite3V
21b26 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
21b27 49 66 50 6f 73 2c 20 69 4c 69 6d 69 74 29 3b 0a  IfPos, iLimit);.
21b28 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21b29 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
21b2a 74 65 67 65 72 2c 20 2d 31 2c 20 69 4f 66 66 73  teger, -1, iOffs
21b2b 65 74 2b 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  et+1);.      sql
21b2c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
21b2d 28 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 20 20  (v, addr1);.    
21b2e 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  }.  }.}../*.** A
21b2f 6c 6c 6f 63 61 74 65 20 61 20 76 69 72 74 75 61  llocate a virtua
21b30 6c 20 69 6e 64 65 78 20 74 6f 20 75 73 65 20 66  l index to use f
21b31 6f 72 20 73 6f 72 74 69 6e 67 2e 0a 2a 2f 0a 73  or sorting..*/.s
21b32 74 61 74 69 63 20 76 6f 69 64 20 63 72 65 61 74  tatic void creat
21b33 65 53 6f 72 74 69 6e 67 49 6e 64 65 78 28 50 61  eSortingIndex(Pa
21b34 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 65 6c  rse *pParse, Sel
21b35 65 63 74 20 2a 70 2c 20 45 78 70 72 4c 69 73 74  ect *p, ExprList
21b36 20 2a 70 4f 72 64 65 72 42 79 29 7b 0a 20 20 69   *pOrderBy){.  i
21b37 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  f( pOrderBy ){. 
21b38 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20     int addr;.   
21b39 20 61 73 73 65 72 74 28 20 70 4f 72 64 65 72 42   assert( pOrderB
21b3a 79 2d 3e 69 45 43 75 72 73 6f 72 3d 3d 30 20 29  y->iECursor==0 )
21b3b 3b 0a 20 20 20 20 70 4f 72 64 65 72 42 79 2d 3e  ;.    pOrderBy->
21b3c 69 45 43 75 72 73 6f 72 20 3d 20 70 50 61 72 73  iECursor = pPars
21b3d 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 61  e->nTab++;.    a
21b3e 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
21b3f 65 41 64 64 4f 70 32 28 70 50 61 72 73 65 2d 3e  eAddOp2(pParse->
21b40 70 56 64 62 65 2c 20 4f 50 5f 4f 70 65 6e 45 70  pVdbe, OP_OpenEp
21b41 68 65 6d 65 72 61 6c 2c 0a 20 20 20 20 20 20 20  hemeral,.       
21b42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21b43 20 20 20 20 20 70 4f 72 64 65 72 42 79 2d 3e 69       pOrderBy->i
21b44 45 43 75 72 73 6f 72 2c 20 70 4f 72 64 65 72 42  ECursor, pOrderB
21b45 79 2d 3e 6e 45 78 70 72 2b 31 29 3b 0a 20 20 20  y->nExpr+1);.   
21b46 20 61 73 73 65 72 74 28 20 70 2d 3e 61 64 64 72   assert( p->addr
21b47 4f 70 65 6e 45 70 68 6d 5b 32 5d 20 3d 3d 20 2d  OpenEphm[2] == -
21b48 31 20 29 3b 0a 20 20 20 20 70 2d 3e 61 64 64 72  1 );.    p->addr
21b49 4f 70 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 61 64  OpenEphm[2] = ad
21b4a 64 72 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64  dr;.  }.}..#ifnd
21b4b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
21b4c 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 2f  OMPOUND_SELECT./
21b4d 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
21b4e 61 70 70 72 6f 70 72 69 61 74 65 20 63 6f 6c 6c  appropriate coll
21b4f 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66  ating sequence f
21b50 6f 72 20 74 68 65 20 69 43 6f 6c 2d 74 68 20 63  or the iCol-th c
21b51 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a 20 74 68 65 20  olumn of.** the 
21b52 72 65 73 75 6c 74 20 73 65 74 20 66 6f 72 20 74  result set for t
21b53 68 65 20 63 6f 6d 70 6f 75 6e 64 2d 73 65 6c 65  he compound-sele
21b54 63 74 20 73 74 61 74 65 6d 65 6e 74 20 22 70 22  ct statement "p"
21b55 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69  .  Return NULL i
21b56 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20  f.** the column 
21b57 68 61 73 20 6e 6f 20 64 65 66 61 75 6c 74 20 63  has no default c
21b58 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
21b59 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c  e..**.** The col
21b5a 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
21b5b 66 6f 72 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64  for the compound
21b5c 20 73 65 6c 65 63 74 20 69 73 20 74 61 6b 65 6e   select is taken
21b5d 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 6c 65 66   from the.** lef
21b5e 74 2d 6d 6f 73 74 20 74 65 72 6d 20 6f 66 20 74  t-most term of t
21b5f 68 65 20 73 65 6c 65 63 74 20 74 68 61 74 20 68  he select that h
21b60 61 73 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73  as a collating s
21b61 65 71 75 65 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74  equence..*/.stat
21b62 69 63 20 43 6f 6c 6c 53 65 71 20 2a 6d 75 6c 74  ic CollSeq *mult
21b63 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28 50  iSelectCollSeq(P
21b64 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 65  arse *pParse, Se
21b65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 69 43 6f  lect *p, int iCo
21b66 6c 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70  l){.  CollSeq *p
21b67 52 65 74 3b 0a 20 20 69 66 28 20 70 2d 3e 70 50  Ret;.  if( p->pP
21b68 72 69 6f 72 20 29 7b 0a 20 20 20 20 70 52 65 74  rior ){.    pRet
21b69 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63 74 43 6f   = multiSelectCo
21b6a 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 2d  llSeq(pParse, p-
21b6b 3e 70 50 72 69 6f 72 2c 20 69 43 6f 6c 29 3b 0a  >pPrior, iCol);.
21b6c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 52 65    }else{.    pRe
21b6d 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28  t = 0;.  }.  if(
21b6e 20 70 52 65 74 3d 3d 30 20 29 7b 0a 20 20 20 20   pRet==0 ){.    
21b6f 70 52 65 74 20 3d 20 73 71 6c 69 74 65 33 45 78  pRet = sqlite3Ex
21b70 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  prCollSeq(pParse
21b71 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 69  , p->pEList->a[i
21b72 43 6f 6c 5d 2e 70 45 78 70 72 29 3b 0a 20 20 7d  Col].pExpr);.  }
21b73 0a 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a  .  return pRet;.
21b74 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
21b75 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44  TE_OMIT_COMPOUND
21b76 5f 53 45 4c 45 43 54 20 2a 2f 0a 0a 23 69 66 6e  _SELECT */..#ifn
21b77 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
21b78 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a  COMPOUND_SELECT.
21b79 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
21b7a 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  ne is called to 
21b7b 70 72 6f 63 65 73 73 20 61 20 71 75 65 72 79 20  process a query 
21b7c 74 68 61 74 20 69 73 20 72 65 61 6c 6c 79 20 74  that is really t
21b7d 68 65 20 75 6e 69 6f 6e 0a 2a 2a 20 6f 72 20 69  he union.** or i
21b7e 6e 74 65 72 73 65 63 74 69 6f 6e 20 6f 66 20 74  ntersection of t
21b7f 77 6f 20 6f 72 20 6d 6f 72 65 20 73 65 70 61 72  wo or more separ
21b80 61 74 65 20 71 75 65 72 69 65 73 2e 0a 2a 2a 0a  ate queries..**.
21b81 2a 2a 20 22 70 22 20 70 6f 69 6e 74 73 20 74 6f  ** "p" points to
21b82 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20   the right-most 
21b83 6f 66 20 74 68 65 20 74 77 6f 20 71 75 65 72 69  of the two queri
21b84 65 73 2e 20 20 74 68 65 20 71 75 65 72 79 20 6f  es.  the query o
21b85 6e 20 74 68 65 0a 2a 2a 20 6c 65 66 74 20 69 73  n the.** left is
21b86 20 70 2d 3e 70 50 72 69 6f 72 2e 20 20 54 68 65   p->pPrior.  The
21b87 20 6c 65 66 74 20 71 75 65 72 79 20 63 6f 75 6c   left query coul
21b88 64 20 61 6c 73 6f 20 62 65 20 61 20 63 6f 6d 70  d also be a comp
21b89 6f 75 6e 64 20 71 75 65 72 79 0a 2a 2a 20 69 6e  ound query.** in
21b8a 20 77 68 69 63 68 20 63 61 73 65 20 74 68 69 73   which case this
21b8b 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 62 65   routine will be
21b8c 20 63 61 6c 6c 65 64 20 72 65 63 75 72 73 69 76   called recursiv
21b8d 65 6c 79 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ely. .**.** The 
21b8e 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 74  results of the t
21b8f 6f 74 61 6c 20 71 75 65 72 79 20 61 72 65 20 74  otal query are t
21b90 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74  o be written int
21b91 6f 20 61 20 64 65 73 74 69 6e 61 74 69 6f 6e 0a  o a destination.
21b92 2a 2a 20 6f 66 20 74 79 70 65 20 65 44 65 73 74  ** of type eDest
21b93 20 77 69 74 68 20 70 61 72 61 6d 65 74 65 72 20   with parameter 
21b94 69 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20 45 78 61  iParm..**.** Exa
21b95 6d 70 6c 65 20 31 3a 20 20 43 6f 6e 73 69 64 65  mple 1:  Conside
21b96 72 20 61 20 74 68 72 65 65 2d 77 61 79 20 63 6f  r a three-way co
21b97 6d 70 6f 75 6e 64 20 53 51 4c 20 73 74 61 74 65  mpound SQL state
21b98 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ment..**.**     
21b99 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 74 31  SELECT a FROM t1
21b9a 20 55 4e 49 4f 4e 20 53 45 4c 45 43 54 20 62 20   UNION SELECT b 
21b9b 46 52 4f 4d 20 74 32 20 55 4e 49 4f 4e 20 53 45  FROM t2 UNION SE
21b9c 4c 45 43 54 20 63 20 46 52 4f 4d 20 74 33 0a 2a  LECT c FROM t3.*
21b9d 2a 0a 2a 2a 20 54 68 69 73 20 73 74 61 74 65 6d  *.** This statem
21b9e 65 6e 74 20 69 73 20 70 61 72 73 65 64 20 75 70  ent is parsed up
21b9f 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
21ba0 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 63 20  **     SELECT c 
21ba1 46 52 4f 4d 20 74 33 0a 2a 2a 20 20 20 20 20 20  FROM t3.**      
21ba2 7c 0a 2a 2a 20 20 20 20 20 20 60 2d 2d 2d 2d 2d  |.**      `-----
21ba3 3e 20 20 53 45 4c 45 43 54 20 62 20 46 52 4f 4d  >  SELECT b FROM
21ba4 20 74 32 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   t2.**          
21ba5 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20        |.**      
21ba6 20 20 20 20 20 20 20 20 20 20 60 2d 2d 2d 2d 2d            `-----
21ba7 2d 3e 20 20 53 45 4c 45 43 54 20 61 20 46 52 4f  ->  SELECT a FRO
21ba8 4d 20 74 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  M t1.**.** The a
21ba9 72 72 6f 77 73 20 69 6e 20 74 68 65 20 64 69 61  rrows in the dia
21baa 67 72 61 6d 20 61 62 6f 76 65 20 72 65 70 72 65  gram above repre
21bab 73 65 6e 74 20 74 68 65 20 53 65 6c 65 63 74 2e  sent the Select.
21bac 70 50 72 69 6f 72 20 70 6f 69 6e 74 65 72 2e 0a  pPrior pointer..
21bad 2a 2a 20 53 6f 20 69 66 20 74 68 69 73 20 72 6f  ** So if this ro
21bae 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
21baf 77 69 74 68 20 70 20 65 71 75 61 6c 20 74 6f 20  with p equal to 
21bb0 74 68 65 20 74 33 20 71 75 65 72 79 2c 20 74 68  the t3 query, th
21bb1 65 6e 0a 2a 2a 20 70 50 72 69 6f 72 20 77 69 6c  en.** pPrior wil
21bb2 6c 20 62 65 20 74 68 65 20 74 32 20 71 75 65 72  l be the t2 quer
21bb3 79 2e 20 20 70 2d 3e 6f 70 20 77 69 6c 6c 20 62  y.  p->op will b
21bb4 65 20 54 4b 5f 55 4e 49 4f 4e 20 69 6e 20 74 68  e TK_UNION in th
21bb5 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 4e  is case..**.** N
21bb6 6f 74 69 63 65 20 74 68 61 74 20 62 65 63 61 75  otice that becau
21bb7 73 65 20 6f 66 20 74 68 65 20 77 61 79 20 53 51  se of the way SQ
21bb8 4c 69 74 65 20 70 61 72 73 65 73 20 63 6f 6d 70  Lite parses comp
21bb9 6f 75 6e 64 20 53 45 4c 45 43 54 73 2c 20 74 68  ound SELECTs, th
21bba 65 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20  e.** individual 
21bbb 73 65 6c 65 63 74 73 20 61 6c 77 61 79 73 20 67  selects always g
21bbc 72 6f 75 70 20 66 72 6f 6d 20 6c 65 66 74 20 74  roup from left t
21bbd 6f 20 72 69 67 68 74 2e 0a 2a 2f 0a 73 74 61 74  o right..*/.stat
21bbe 69 63 20 69 6e 74 20 6d 75 6c 74 69 53 65 6c 65  ic int multiSele
21bbf 63 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ct(.  Parse *pPa
21bc0 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  rse,        /* P
21bc1 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
21bc2 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20  /.  Select *p,  
21bc3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
21bc4 20 72 69 67 68 74 2d 6d 6f 73 74 20 6f 66 20 53   right-most of S
21bc5 45 4c 45 43 54 73 20 74 6f 20 62 65 20 63 6f 64  ELECTs to be cod
21bc6 65 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65  ed */.  SelectDe
21bc7 73 74 20 2a 70 44 65 73 74 2c 20 20 20 20 2f 2a  st *pDest,    /*
21bc8 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68   What to do with
21bc9 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 20 2a   query results *
21bca 2f 0a 20 20 63 68 61 72 20 2a 61 66 66 20 20 20  /.  char *aff   
21bcb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20            /* If 
21bcc 65 44 65 73 74 20 69 73 20 53 52 54 5f 55 6e 69  eDest is SRT_Uni
21bcd 6f 6e 2c 20 74 68 65 20 61 66 66 69 6e 69 74 79  on, the affinity
21bce 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20   string */.){.  
21bcf 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
21bd0 4f 4b 3b 20 20 20 2f 2a 20 53 75 63 63 65 73 73  OK;   /* Success
21bd1 20 63 6f 64 65 20 66 72 6f 6d 20 61 20 73 75 62   code from a sub
21bd2 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 53 65 6c  routine */.  Sel
21bd3 65 63 74 20 2a 70 50 72 69 6f 72 3b 20 20 20 20  ect *pPrior;    
21bd4 20 20 20 2f 2a 20 41 6e 6f 74 68 65 72 20 53 45     /* Another SE
21bd5 4c 45 43 54 20 69 6d 6d 65 64 69 61 74 65 6c 79  LECT immediately
21bd6 20 74 6f 20 6f 75 72 20 6c 65 66 74 20 2a 2f 0a   to our left */.
21bd7 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20    Vdbe *v;      
21bd8 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72          /* Gener
21bd9 61 74 65 20 63 6f 64 65 20 74 6f 20 74 68 69 73  ate code to this
21bda 20 56 44 42 45 20 2a 2f 0a 20 20 69 6e 74 20 6e   VDBE */.  int n
21bdb 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Col;            
21bdc 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f   /* Number of co
21bdd 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73  lumns in the res
21bde 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 45 78 70  ult set */.  Exp
21bdf 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 3b  rList *pOrderBy;
21be0 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20     /* The ORDER 
21be1 42 59 20 63 6c 61 75 73 65 20 6f 6e 20 70 20 2a  BY clause on p *
21be2 2f 0a 20 20 69 6e 74 20 61 53 65 74 50 32 5b 32  /.  int aSetP2[2
21be3 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74  ];        /* Set
21be4 20 50 32 20 76 61 6c 75 65 20 6f 66 20 74 68 65   P2 value of the
21be5 73 65 20 6f 70 20 74 6f 20 6e 75 6d 62 65 72 20  se op to number 
21be6 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20  of columns */.  
21be7 69 6e 74 20 6e 53 65 74 50 32 20 3d 20 30 3b 20  int nSetP2 = 0; 
21be8 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
21be9 6f 66 20 73 6c 6f 74 73 20 69 6e 20 61 53 65 74  of slots in aSet
21bea 50 32 5b 5d 20 75 73 65 64 20 2a 2f 0a 20 20 53  P2[] used */.  S
21beb 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b 20  electDest dest; 
21bec 20 20 20 20 20 2f 2a 20 41 6c 74 65 72 6e 61 74       /* Alternat
21bed 69 76 65 20 64 61 74 61 20 64 65 73 74 69 6e 61  ive data destina
21bee 74 69 6f 6e 20 2a 2f 0a 0a 20 20 64 65 73 74 20  tion */..  dest 
21bef 3d 20 2a 70 44 65 73 74 3b 0a 0a 20 20 2f 2a 20  = *pDest;..  /* 
21bf0 4d 61 6b 65 20 73 75 72 65 20 74 68 65 72 65 20  Make sure there 
21bf1 69 73 20 6e 6f 20 4f 52 44 45 52 20 42 59 20 6f  is no ORDER BY o
21bf2 72 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 6f  r LIMIT clause o
21bf3 6e 20 70 72 69 6f 72 20 53 45 4c 45 43 54 73 2e  n prior SELECTs.
21bf4 20 20 4f 6e 6c 79 0a 20 20 2a 2a 20 74 68 65 20    Only.  ** the 
21bf5 6c 61 73 74 20 28 72 69 67 68 74 2d 6d 6f 73 74  last (right-most
21bf6 29 20 53 45 4c 45 43 54 20 69 6e 20 74 68 65 20  ) SELECT in the 
21bf7 73 65 72 69 65 73 20 6d 61 79 20 68 61 76 65 20  series may have 
21bf8 61 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20 4c  an ORDER BY or L
21bf9 49 4d 49 54 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  IMIT..  */.  if(
21bfa 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 69   p==0 || p->pPri
21bfb 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20  or==0 ){.    rc 
21bfc 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6d 75  = 1;.    goto mu
21bfd 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  lti_select_end;.
21bfe 20 20 7d 0a 20 20 70 50 72 69 6f 72 20 3d 20 70    }.  pPrior = p
21bff 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 61 73 73 65  ->pPrior;.  asse
21c00 72 74 28 20 70 50 72 69 6f 72 2d 3e 70 52 69 67  rt( pPrior->pRig
21c01 68 74 6d 6f 73 74 21 3d 70 50 72 69 6f 72 20 29  htmost!=pPrior )
21c02 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 69  ;.  assert( pPri
21c03 6f 72 2d 3e 70 52 69 67 68 74 6d 6f 73 74 3d 3d  or->pRightmost==
21c04 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 29 3b  p->pRightmost );
21c05 0a 20 20 69 66 28 20 70 50 72 69 6f 72 2d 3e 70  .  if( pPrior->p
21c06 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 73  OrderBy ){.    s
21c07 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
21c08 50 61 72 73 65 2c 22 4f 52 44 45 52 20 42 59 20  Parse,"ORDER BY 
21c09 63 6c 61 75 73 65 20 73 68 6f 75 6c 64 20 63 6f  clause should co
21c0a 6d 65 20 61 66 74 65 72 20 25 73 20 6e 6f 74 20  me after %s not 
21c0b 62 65 66 6f 72 65 22 2c 0a 20 20 20 20 20 20 73  before",.      s
21c0c 65 6c 65 63 74 4f 70 4e 61 6d 65 28 70 2d 3e 6f  electOpName(p->o
21c0d 70 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 31 3b  p));.    rc = 1;
21c0e 0a 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f  .    goto multi_
21c0f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a  select_end;.  }.
21c10 20 20 69 66 28 20 70 50 72 69 6f 72 2d 3e 70 4c    if( pPrior->pL
21c11 69 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  imit ){.    sqli
21c12 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
21c13 73 65 2c 22 4c 49 4d 49 54 20 63 6c 61 75 73 65  se,"LIMIT clause
21c14 20 73 68 6f 75 6c 64 20 63 6f 6d 65 20 61 66 74   should come aft
21c15 65 72 20 25 73 20 6e 6f 74 20 62 65 66 6f 72 65  er %s not before
21c16 22 2c 0a 20 20 20 20 20 20 73 65 6c 65 63 74 4f  ",.      selectO
21c17 70 4e 61 6d 65 28 70 2d 3e 6f 70 29 29 3b 0a 20  pName(p->op));. 
21c18 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 67     rc = 1;.    g
21c19 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74  oto multi_select
21c1a 5f 65 6e 64 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  _end;.  }..  /* 
21c1b 4d 61 6b 65 20 73 75 72 65 20 77 65 20 68 61 76  Make sure we hav
21c1c 65 20 61 20 76 61 6c 69 64 20 71 75 65 72 79 20  e a valid query 
21c1d 65 6e 67 69 6e 65 2e 20 20 49 66 20 6e 6f 74 2c  engine.  If not,
21c1e 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 6f 6e   create a new on
21c1f 65 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71  e..  */.  v = sq
21c20 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
21c21 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30  rse);.  if( v==0
21c22 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a   ){.    rc = 1;.
21c23 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73      goto multi_s
21c24 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 0a  elect_end;.  }..
21c25 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20    /* Create the 
21c26 64 65 73 74 69 6e 61 74 69 6f 6e 20 74 65 6d 70  destination temp
21c27 6f 72 61 72 79 20 74 61 62 6c 65 20 69 66 20 6e  orary table if n
21c28 65 63 65 73 73 61 72 79 0a 20 20 2a 2f 0a 20 20  ecessary.  */.  
21c29 69 66 28 20 64 65 73 74 2e 65 44 65 73 74 3d 3d  if( dest.eDest==
21c2a 53 52 54 5f 45 70 68 65 6d 54 61 62 20 29 7b 0a  SRT_EphemTab ){.
21c2b 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
21c2c 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 61 73 73  EList );.    ass
21c2d 65 72 74 28 20 6e 53 65 74 50 32 3c 73 69 7a 65  ert( nSetP2<size
21c2e 6f 66 28 61 53 65 74 50 32 29 2f 73 69 7a 65 6f  of(aSetP2)/sizeo
21c2f 66 28 61 53 65 74 50 32 5b 30 5d 29 20 29 3b 0a  f(aSetP2[0]) );.
21c30 20 20 20 20 61 53 65 74 50 32 5b 6e 53 65 74 50      aSetP2[nSetP
21c31 32 2b 2b 5d 20 3d 20 73 71 6c 69 74 65 33 56 64  2++] = sqlite3Vd
21c32 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f  beAddOp2(v, OP_O
21c33 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 64 65  penEphemeral, de
21c34 73 74 2e 69 50 61 72 6d 2c 20 30 29 3b 0a 20 20  st.iParm, 0);.  
21c35 20 20 64 65 73 74 2e 65 44 65 73 74 20 3d 20 53    dest.eDest = S
21c36 52 54 5f 54 61 62 6c 65 3b 0a 20 20 7d 0a 0a 20  RT_Table;.  }.. 
21c37 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
21c38 65 20 66 6f 72 20 74 68 65 20 6c 65 66 74 20 61  e for the left a
21c39 6e 64 20 72 69 67 68 74 20 53 45 4c 45 43 54 20  nd right SELECT 
21c3a 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 2a 2f  statements..  */
21c3b 0a 20 20 70 4f 72 64 65 72 42 79 20 3d 20 70 2d  .  pOrderBy = p-
21c3c 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 73 77 69  >pOrderBy;.  swi
21c3d 74 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 20 20  tch( p->op ){.  
21c3e 20 20 63 61 73 65 20 54 4b 5f 41 4c 4c 3a 20 7b    case TK_ALL: {
21c3f 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72 64 65  .      if( pOrde
21c40 72 42 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  rBy==0 ){.      
21c41 20 20 69 6e 74 20 61 64 64 72 20 3d 20 30 3b 0a    int addr = 0;.
21c42 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
21c43 21 70 50 72 69 6f 72 2d 3e 70 4c 69 6d 69 74 20  !pPrior->pLimit 
21c44 29 3b 0a 20 20 20 20 20 20 20 20 70 50 72 69 6f  );.        pPrio
21c45 72 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 2d 3e 70  r->pLimit = p->p
21c46 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 20 20 70  Limit;.        p
21c47 50 72 69 6f 72 2d 3e 70 4f 66 66 73 65 74 20 3d  Prior->pOffset =
21c48 20 70 2d 3e 70 4f 66 66 73 65 74 3b 0a 20 20 20   p->pOffset;.   
21c49 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
21c4a 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  3Select(pParse, 
21c4b 70 50 72 69 6f 72 2c 20 26 64 65 73 74 2c 20 30  pPrior, &dest, 0
21c4c 2c 20 30 2c 20 30 2c 20 61 66 66 29 3b 0a 20 20  , 0, 0, aff);.  
21c4d 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20        p->pLimit 
21c4e 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  = 0;.        p->
21c4f 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20  pOffset = 0;.   
21c50 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20       if( rc ){. 
21c51 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75           goto mu
21c52 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  lti_select_end;.
21c53 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
21c54 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b    p->pPrior = 0;
21c55 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4c 69 6d  .        p->iLim
21c56 69 74 20 3d 20 70 50 72 69 6f 72 2d 3e 69 4c 69  it = pPrior->iLi
21c57 6d 69 74 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  mit;.        p->
21c58 69 4f 66 66 73 65 74 20 3d 20 70 50 72 69 6f 72  iOffset = pPrior
21c59 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 20  ->iOffset;.     
21c5a 20 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74     if( p->iLimit
21c5b 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  >=0 ){.         
21c5c 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
21c5d 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
21c5e 49 66 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d 69  IfZero, p->iLimi
21c5f 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 56 64  t);.          Vd
21c60 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4a  beComment((v, "J
21c61 75 6d 70 20 61 68 65 61 64 20 69 66 20 4c 49 4d  ump ahead if LIM
21c62 49 54 20 72 65 61 63 68 65 64 22 29 29 3b 0a 20  IT reached"));. 
21c63 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
21c64 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 65 6c   rc = sqlite3Sel
21c65 65 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20 26  ect(pParse, p, &
21c66 64 65 73 74 2c 20 30 2c 20 30 2c 20 30 2c 20 61  dest, 0, 0, 0, a
21c67 66 66 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  ff);.        p->
21c68 70 50 72 69 6f 72 20 3d 20 70 50 72 69 6f 72 3b  pPrior = pPrior;
21c69 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20  .        if( rc 
21c6a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  ){.          got
21c6b 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65  o multi_select_e
21c6c 6e 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  nd;.        }.  
21c6d 20 20 20 20 20 20 69 66 28 20 61 64 64 72 20 29        if( addr )
21c6e 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
21c6f 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
21c70 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20  v, addr);.      
21c71 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61    }.        brea
21c72 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
21c73 20 2f 2a 20 46 6f 72 20 55 4e 49 4f 4e 20 41 4c   /* For UNION AL
21c74 4c 20 2e 2e 2e 20 4f 52 44 45 52 20 42 59 20 66  L ... ORDER BY f
21c75 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74  all through to t
21c76 68 65 20 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a  he next case */.
21c77 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
21c78 4b 5f 45 58 43 45 50 54 3a 0a 20 20 20 20 63 61  K_EXCEPT:.    ca
21c79 73 65 20 54 4b 5f 55 4e 49 4f 4e 3a 20 7b 0a 20  se TK_UNION: {. 
21c7a 20 20 20 20 20 69 6e 74 20 75 6e 69 6f 6e 54 61       int unionTa
21c7b 62 3b 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20  b;    /* Cursor 
21c7c 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 74 65  number of the te
21c7d 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 68 6f  mporary table ho
21c7e 6c 64 69 6e 67 20 72 65 73 75 6c 74 20 2a 2f 0a  lding result */.
21c7f 20 20 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 30        int op = 0
21c80 3b 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66  ;      /* One of
21c81 20 74 68 65 20 53 52 54 5f 20 6f 70 65 72 61 74   the SRT_ operat
21c82 69 6f 6e 73 20 74 6f 20 61 70 70 6c 79 20 74 6f  ions to apply to
21c83 20 73 65 6c 66 20 2a 2f 0a 20 20 20 20 20 20 69   self */.      i
21c84 6e 74 20 70 72 69 6f 72 4f 70 3b 20 20 20 20 20  nt priorOp;     
21c85 2f 2a 20 54 68 65 20 53 52 54 5f 20 6f 70 65 72  /* The SRT_ oper
21c86 61 74 69 6f 6e 20 74 6f 20 61 70 70 6c 79 20 74  ation to apply t
21c87 6f 20 70 72 69 6f 72 20 73 65 6c 65 63 74 73 20  o prior selects 
21c88 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  */.      Expr *p
21c89 4c 69 6d 69 74 2c 20 2a 70 4f 66 66 73 65 74 3b  Limit, *pOffset;
21c8a 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65 73   /* Saved values
21c8b 20 6f 66 20 70 2d 3e 6e 4c 69 6d 69 74 20 61 6e   of p->nLimit an
21c8c 64 20 70 2d 3e 6e 4f 66 66 73 65 74 20 2a 2f 0a  d p->nOffset */.
21c8d 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a        int addr;.
21c8e 20 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 74        SelectDest
21c8f 20 75 6e 69 6f 6e 64 65 73 74 3b 0a 0a 20 20 20   uniondest;..   
21c90 20 20 20 70 72 69 6f 72 4f 70 20 3d 20 70 2d 3e     priorOp = p->
21c91 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 3f 20 53 52 54  op==TK_ALL ? SRT
21c92 5f 54 61 62 6c 65 20 3a 20 53 52 54 5f 55 6e 69  _Table : SRT_Uni
21c93 6f 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 64 65  on;.      if( de
21c94 73 74 2e 65 44 65 73 74 3d 3d 70 72 69 6f 72 4f  st.eDest==priorO
21c95 70 20 26 26 20 70 4f 72 64 65 72 42 79 3d 3d 30  p && pOrderBy==0
21c96 20 26 26 20 21 70 2d 3e 70 4c 69 6d 69 74 20 26   && !p->pLimit &
21c97 26 20 21 70 2d 3e 70 4f 66 66 73 65 74 20 29 7b  & !p->pOffset ){
21c98 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 63  .        /* We c
21c99 61 6e 20 72 65 75 73 65 20 61 20 74 65 6d 70 6f  an reuse a tempo
21c9a 72 61 72 79 20 74 61 62 6c 65 20 67 65 6e 65 72  rary table gener
21c9b 61 74 65 64 20 62 79 20 61 20 53 45 4c 45 43 54  ated by a SELECT
21c9c 20 74 6f 20 6f 75 72 0a 20 20 20 20 20 20 20 20   to our.        
21c9d 2a 2a 20 72 69 67 68 74 2e 0a 20 20 20 20 20 20  ** right..      
21c9e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 6e 69    */.        uni
21c9f 6f 6e 54 61 62 20 3d 20 64 65 73 74 2e 69 50 61  onTab = dest.iPa
21ca0 72 6d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  rm;.      }else{
21ca1 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 77  .        /* We w
21ca2 69 6c 6c 20 6e 65 65 64 20 74 6f 20 63 72 65 61  ill need to crea
21ca3 74 65 20 6f 75 72 20 6f 77 6e 20 74 65 6d 70 6f  te our own tempo
21ca4 72 61 72 79 20 74 61 62 6c 65 20 74 6f 20 68 6f  rary table to ho
21ca5 6c 64 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  ld the.        *
21ca6 2a 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 72  * intermediate r
21ca7 65 73 75 6c 74 73 2e 0a 20 20 20 20 20 20 20 20  esults..        
21ca8 2a 2f 0a 20 20 20 20 20 20 20 20 75 6e 69 6f 6e  */.        union
21ca9 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  Tab = pParse->nT
21caa 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20 69 66  ab++;.        if
21cab 28 20 70 72 6f 63 65 73 73 43 6f 6d 70 6f 75 6e  ( processCompoun
21cac 64 4f 72 64 65 72 42 79 28 70 50 61 72 73 65 2c  dOrderBy(pParse,
21cad 20 70 2c 20 75 6e 69 6f 6e 54 61 62 29 20 29 7b   p, unionTab) ){
21cae 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
21caf 31 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  1;.          got
21cb0 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65  o multi_select_e
21cb1 6e 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  nd;.        }.  
21cb2 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
21cb3 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
21cb4 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  , OP_OpenEphemer
21cb5 61 6c 2c 20 75 6e 69 6f 6e 54 61 62 2c 20 30 29  al, unionTab, 0)
21cb6 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72  ;.        if( pr
21cb7 69 6f 72 4f 70 3d 3d 53 52 54 5f 54 61 62 6c 65  iorOp==SRT_Table
21cb8 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73   ){.          as
21cb9 73 65 72 74 28 20 6e 53 65 74 50 32 3c 73 69 7a  sert( nSetP2<siz
21cba 65 6f 66 28 61 53 65 74 50 32 29 2f 73 69 7a 65  eof(aSetP2)/size
21cbb 6f 66 28 61 53 65 74 50 32 5b 30 5d 29 20 29 3b  of(aSetP2[0]) );
21cbc 0a 20 20 20 20 20 20 20 20 20 20 61 53 65 74 50  .          aSetP
21cbd 32 5b 6e 53 65 74 50 32 2b 2b 5d 20 3d 20 61 64  2[nSetP2++] = ad
21cbe 64 72 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  dr;.        }els
21cbf 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  e{.          ass
21cc0 65 72 74 28 20 70 2d 3e 61 64 64 72 4f 70 65 6e  ert( p->addrOpen
21cc1 45 70 68 6d 5b 30 5d 20 3d 3d 20 2d 31 20 29 3b  Ephm[0] == -1 );
21cc2 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61 64  .          p->ad
21cc3 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20  drOpenEphm[0] = 
21cc4 61 64 64 72 3b 0a 20 20 20 20 20 20 20 20 20 20  addr;.          
21cc5 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 2d 3e 75  p->pRightmost->u
21cc6 73 65 73 45 70 68 6d 20 3d 20 31 3b 0a 20 20 20  sesEphm = 1;.   
21cc7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63       }.        c
21cc8 72 65 61 74 65 53 6f 72 74 69 6e 67 49 6e 64 65  reateSortingInde
21cc9 78 28 70 50 61 72 73 65 2c 20 70 2c 20 70 4f 72  x(pParse, p, pOr
21cca 64 65 72 42 79 29 3b 0a 20 20 20 20 20 20 20 20  derBy);.        
21ccb 61 73 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73  assert( p->pELis
21ccc 74 20 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  t );.      }..  
21ccd 20 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20      /* Code the 
21cce 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
21ccf 73 20 74 6f 20 6f 75 72 20 6c 65 66 74 0a 20 20  s to our left.  
21cd0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73      */.      ass
21cd1 65 72 74 28 20 21 70 50 72 69 6f 72 2d 3e 70 4f  ert( !pPrior->pO
21cd2 72 64 65 72 42 79 20 29 3b 0a 20 20 20 20 20 20  rderBy );.      
21cd3 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73  sqlite3SelectDes
21cd4 74 49 6e 69 74 28 26 75 6e 69 6f 6e 64 65 73 74  tInit(&uniondest
21cd5 2c 20 70 72 69 6f 72 4f 70 2c 20 75 6e 69 6f 6e  , priorOp, union
21cd6 54 61 62 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  Tab);.      rc =
21cd7 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70   sqlite3Select(p
21cd8 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c 20 26  Parse, pPrior, &
21cd9 75 6e 69 6f 6e 64 65 73 74 2c 20 30 2c 20 30 2c  uniondest, 0, 0,
21cda 20 30 2c 20 61 66 66 29 3b 0a 20 20 20 20 20 20   0, aff);.      
21cdb 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
21cdc 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c    goto multi_sel
21cdd 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d  ect_end;.      }
21cde 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64 65 20  ..      /* Code 
21cdf 74 68 65 20 63 75 72 72 65 6e 74 20 53 45 4c 45  the current SELE
21ce0 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20  CT statement.   
21ce1 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 77 69 74     */.      swit
21ce2 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 20 20 20  ch( p->op ){.   
21ce3 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58        case TK_EX
21ce4 43 45 50 54 3a 20 20 6f 70 20 3d 20 53 52 54 5f  CEPT:  op = SRT_
21ce5 45 78 63 65 70 74 3b 20 20 20 62 72 65 61 6b 3b  Except;   break;
21ce6 0a 20 20 20 20 20 20 20 20 20 63 61 73 65 20 54  .         case T
21ce7 4b 5f 55 4e 49 4f 4e 3a 20 20 20 6f 70 20 3d 20  K_UNION:   op = 
21ce8 53 52 54 5f 55 6e 69 6f 6e 3b 20 20 20 20 62 72  SRT_Union;    br
21ce9 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 63 61  eak;.         ca
21cea 73 65 20 54 4b 5f 41 4c 4c 3a 20 20 20 20 20 6f  se TK_ALL:     o
21ceb 70 20 3d 20 53 52 54 5f 54 61 62 6c 65 3b 20 20  p = SRT_Table;  
21cec 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
21ced 0a 20 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72  .      p->pPrior
21cee 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 70   = 0;.      p->p
21cef 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 20  OrderBy = 0;.   
21cf0 20 20 20 70 2d 3e 64 69 73 61 6c 6c 6f 77 4f 72     p->disallowOr
21cf1 64 65 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79  derBy = pOrderBy
21cf2 21 3d 30 3b 0a 20 20 20 20 20 20 70 4c 69 6d 69  !=0;.      pLimi
21cf3 74 20 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a 20  t = p->pLimit;. 
21cf4 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d       p->pLimit =
21cf5 20 30 3b 0a 20 20 20 20 20 20 70 4f 66 66 73 65   0;.      pOffse
21cf6 74 20 3d 20 70 2d 3e 70 4f 66 66 73 65 74 3b 0a  t = p->pOffset;.
21cf7 20 20 20 20 20 20 70 2d 3e 70 4f 66 66 73 65 74        p->pOffset
21cf8 20 3d 20 30 3b 0a 20 20 20 20 20 20 75 6e 69 6f   = 0;.      unio
21cf9 6e 64 65 73 74 2e 65 44 65 73 74 20 3d 20 6f 70  ndest.eDest = op
21cfa 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
21cfb 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73  ite3Select(pPars
21cfc 65 2c 20 70 2c 20 26 75 6e 69 6f 6e 64 65 73 74  e, p, &uniondest
21cfd 2c 20 30 2c 20 30 2c 20 30 2c 20 61 66 66 29 3b  , 0, 0, 0, aff);
21cfe 0a 20 20 20 20 20 20 2f 2a 20 51 75 65 72 79 20  .      /* Query 
21cff 66 6c 61 74 74 65 6e 69 6e 67 20 69 6e 20 73 71  flattening in sq
21d00 6c 69 74 65 33 53 65 6c 65 63 74 28 29 20 6d 69  lite3Select() mi
21d01 67 68 74 20 72 65 66 69 6c 6c 20 70 2d 3e 70 4f  ght refill p->pO
21d02 72 64 65 72 42 79 2e 0a 20 20 20 20 20 20 2a 2a  rderBy..      **
21d03 20 42 65 20 73 75 72 65 20 74 6f 20 64 65 6c 65   Be sure to dele
21d04 74 65 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20  te p->pOrderBy, 
21d05 74 68 65 72 65 66 6f 72 65 2c 20 74 6f 20 61 76  therefore, to av
21d06 6f 69 64 20 61 20 6d 65 6d 6f 72 79 20 6c 65 61  oid a memory lea
21d07 6b 2e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  k. */.      sqli
21d08 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
21d09 65 28 70 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a  e(p->pOrderBy);.
21d0a 20 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20        p->pPrior 
21d0b 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20  = pPrior;.      
21d0c 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 70 4f  p->pOrderBy = pO
21d0d 72 64 65 72 42 79 3b 0a 20 20 20 20 20 20 73 71  rderBy;.      sq
21d0e 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
21d0f 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 20 20  p->pLimit);.    
21d10 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 4c    p->pLimit = pL
21d11 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70  imit;.      p->p
21d12 4f 66 66 73 65 74 20 3d 20 70 4f 66 66 73 65 74  Offset = pOffset
21d13 3b 0a 20 20 20 20 20 20 70 2d 3e 69 4c 69 6d 69  ;.      p->iLimi
21d14 74 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 70 2d  t = -1;.      p-
21d15 3e 69 4f 66 66 73 65 74 20 3d 20 2d 31 3b 0a 20  >iOffset = -1;. 
21d16 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20       if( rc ){. 
21d17 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74         goto mult
21d18 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  i_select_end;.  
21d19 20 20 20 20 7d 0a 0a 0a 20 20 20 20 20 20 2f 2a      }...      /*
21d1a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 64 61 74   Convert the dat
21d1b 61 20 69 6e 20 74 68 65 20 74 65 6d 70 6f 72 61  a in the tempora
21d1c 72 79 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68  ry table into wh
21d1d 61 74 65 76 65 72 20 66 6f 72 6d 0a 20 20 20 20  atever form.    
21d1e 20 20 2a 2a 20 69 74 20 69 73 20 74 68 61 74 20    ** it is that 
21d1f 77 65 20 63 75 72 72 65 6e 74 6c 79 20 6e 65 65  we currently nee
21d20 64 2e 0a 20 20 20 20 20 20 2a 2f 20 20 20 20 20  d..      */     
21d21 20 0a 20 20 20 20 20 20 69 66 28 20 64 65 73 74   .      if( dest
21d22 2e 65 44 65 73 74 21 3d 70 72 69 6f 72 4f 70 20  .eDest!=priorOp 
21d23 7c 7c 20 75 6e 69 6f 6e 54 61 62 21 3d 64 65 73  || unionTab!=des
21d24 74 2e 69 50 61 72 6d 20 29 7b 0a 20 20 20 20 20  t.iParm ){.     
21d25 20 20 20 69 6e 74 20 69 43 6f 6e 74 2c 20 69 42     int iCont, iB
21d26 72 65 61 6b 2c 20 69 53 74 61 72 74 3b 0a 20 20  reak, iStart;.  
21d27 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
21d28 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 20  >pEList );.     
21d29 20 20 20 69 66 28 20 64 65 73 74 2e 65 44 65 73     if( dest.eDes
21d2a 74 3d 3d 53 52 54 5f 43 61 6c 6c 62 61 63 6b 20  t==SRT_Callback 
21d2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 53 65 6c  ){.          Sel
21d2c 65 63 74 20 2a 70 46 69 72 73 74 20 3d 20 70 3b  ect *pFirst = p;
21d2d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65  .          while
21d2e 28 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72  ( pFirst->pPrior
21d2f 20 29 20 70 46 69 72 73 74 20 3d 20 70 46 69 72   ) pFirst = pFir
21d30 73 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20  st->pPrior;.    
21d31 20 20 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f        generateCo
21d32 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65  lumnNames(pParse
21d33 2c 20 30 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c  , 0, pFirst->pEL
21d34 69 73 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ist);.        }.
21d35 20 20 20 20 20 20 20 20 69 42 72 65 61 6b 20 3d          iBreak =
21d36 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
21d37 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20  Label(v);.      
21d38 20 20 69 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65    iCont = sqlite
21d39 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
21d3a 29 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75  );.        compu
21d3b 74 65 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73  teLimitRegisters
21d3c 28 70 50 61 72 73 65 2c 20 70 2c 20 69 42 72 65  (pParse, p, iBre
21d3d 61 6b 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ak);.        sql
21d3e 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
21d3f 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 75 6e 69  , OP_Rewind, uni
21d40 6f 6e 54 61 62 2c 20 69 42 72 65 61 6b 29 3b 0a  onTab, iBreak);.
21d41 20 20 20 20 20 20 20 20 69 53 74 61 72 74 20 3d          iStart =
21d42 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
21d43 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20  entAddr(v);.    
21d44 20 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c      selectInnerL
21d45 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70  oop(pParse, p, p
21d46 2d 3e 70 45 4c 69 73 74 2c 20 75 6e 69 6f 6e 54  ->pEList, unionT
21d47 61 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e  ab, p->pEList->n
21d48 45 78 70 72 2c 0a 20 20 20 20 20 20 20 20 20 20  Expr,.          
21d49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4f                pO
21d4a 72 64 65 72 42 79 2c 20 2d 31 2c 20 26 64 65 73  rderBy, -1, &des
21d4b 74 2c 20 69 43 6f 6e 74 2c 20 69 42 72 65 61 6b  t, iCont, iBreak
21d4c 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 0);.        sq
21d4d 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
21d4e 4c 61 62 65 6c 28 76 2c 20 69 43 6f 6e 74 29 3b  Label(v, iCont);
21d4f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
21d50 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
21d51 5f 4e 65 78 74 2c 20 75 6e 69 6f 6e 54 61 62 2c  _Next, unionTab,
21d52 20 69 53 74 61 72 74 29 3b 0a 20 20 20 20 20 20   iStart);.      
21d53 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
21d54 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 42 72  olveLabel(v, iBr
21d55 65 61 6b 29 3b 0a 20 20 20 20 20 20 20 20 73 71  eak);.        sq
21d56 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
21d57 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 75 6e 69  v, OP_Close, uni
21d58 6f 6e 54 61 62 2c 20 30 29 3b 0a 20 20 20 20 20  onTab, 0);.     
21d59 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
21d5a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
21d5b 4b 5f 49 4e 54 45 52 53 45 43 54 3a 20 7b 0a 20  K_INTERSECT: {. 
21d5c 20 20 20 20 20 69 6e 74 20 74 61 62 31 2c 20 74       int tab1, t
21d5d 61 62 32 3b 0a 20 20 20 20 20 20 69 6e 74 20 69  ab2;.      int i
21d5e 43 6f 6e 74 2c 20 69 42 72 65 61 6b 2c 20 69 53  Cont, iBreak, iS
21d5f 74 61 72 74 3b 0a 20 20 20 20 20 20 45 78 70 72  tart;.      Expr
21d60 20 2a 70 4c 69 6d 69 74 2c 20 2a 70 4f 66 66 73   *pLimit, *pOffs
21d61 65 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 64  et;.      int ad
21d62 64 72 3b 0a 20 20 20 20 20 20 53 65 6c 65 63 74  dr;.      Select
21d63 44 65 73 74 20 69 6e 74 65 72 73 65 63 74 64 65  Dest intersectde
21d64 73 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 31  st;.      int r1
21d65 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 4e 54 45  ;..      /* INTE
21d66 52 53 45 43 54 20 69 73 20 64 69 66 66 65 72 65  RSECT is differe
21d67 6e 74 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65  nt from the othe
21d68 72 73 20 73 69 6e 63 65 20 69 74 20 72 65 71 75  rs since it requ
21d69 69 72 65 73 0a 20 20 20 20 20 20 2a 2a 20 74 77  ires.      ** tw
21d6a 6f 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  o temporary tabl
21d6b 65 73 2e 20 20 48 65 6e 63 65 20 69 74 20 68 61  es.  Hence it ha
21d6c 73 20 69 74 73 20 6f 77 6e 20 63 61 73 65 2e 20  s its own case. 
21d6d 20 42 65 67 69 6e 0a 20 20 20 20 20 20 2a 2a 20   Begin.      ** 
21d6e 62 79 20 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68  by allocating th
21d6f 65 20 74 61 62 6c 65 73 20 77 65 20 77 69 6c 6c  e tables we will
21d70 20 6e 65 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a   need..      */.
21d71 20 20 20 20 20 20 74 61 62 31 20 3d 20 70 50 61        tab1 = pPa
21d72 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20  rse->nTab++;.   
21d73 20 20 20 74 61 62 32 20 3d 20 70 50 61 72 73 65     tab2 = pParse
21d74 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20  ->nTab++;.      
21d75 69 66 28 20 70 72 6f 63 65 73 73 43 6f 6d 70 6f  if( processCompo
21d76 75 6e 64 4f 72 64 65 72 42 79 28 70 50 61 72 73  undOrderBy(pPars
21d77 65 2c 20 70 2c 20 74 61 62 31 29 20 29 7b 0a 20  e, p, tab1) ){. 
21d78 20 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20         rc = 1;. 
21d79 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74         goto mult
21d7a 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  i_select_end;.  
21d7b 20 20 20 20 7d 0a 20 20 20 20 20 20 63 72 65 61      }.      crea
21d7c 74 65 53 6f 72 74 69 6e 67 49 6e 64 65 78 28 70  teSortingIndex(p
21d7d 50 61 72 73 65 2c 20 70 2c 20 70 4f 72 64 65 72  Parse, p, pOrder
21d7e 42 79 29 3b 0a 0a 20 20 20 20 20 20 61 64 64 72  By);..      addr
21d7f 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
21d80 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45  dOp2(v, OP_OpenE
21d81 70 68 65 6d 65 72 61 6c 2c 20 74 61 62 31 2c 20  phemeral, tab1, 
21d82 30 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  0);.      assert
21d83 28 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68  ( p->addrOpenEph
21d84 6d 5b 30 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20 20  m[0] == -1 );.  
21d85 20 20 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45      p->addrOpenE
21d86 70 68 6d 5b 30 5d 20 3d 20 61 64 64 72 3b 0a 20  phm[0] = addr;. 
21d87 20 20 20 20 20 70 2d 3e 70 52 69 67 68 74 6d 6f       p->pRightmo
21d88 73 74 2d 3e 75 73 65 73 45 70 68 6d 20 3d 20 31  st->usesEphm = 1
21d89 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
21d8a 70 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 0a 20 20  p->pEList );..  
21d8b 20 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20      /* Code the 
21d8c 53 45 4c 45 43 54 73 20 74 6f 20 6f 75 72 20 6c  SELECTs to our l
21d8d 65 66 74 20 69 6e 74 6f 20 74 65 6d 70 6f 72 61  eft into tempora
21d8e 72 79 20 74 61 62 6c 65 20 22 74 61 62 31 22 2e  ry table "tab1".
21d8f 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
21d90 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73  sqlite3SelectDes
21d91 74 49 6e 69 74 28 26 69 6e 74 65 72 73 65 63 74  tInit(&intersect
21d92 64 65 73 74 2c 20 53 52 54 5f 55 6e 69 6f 6e 2c  dest, SRT_Union,
21d93 20 74 61 62 31 29 3b 0a 20 20 20 20 20 20 72 63   tab1);.      rc
21d94 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
21d95 28 70 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c  (pParse, pPrior,
21d96 20 26 69 6e 74 65 72 73 65 63 74 64 65 73 74 2c   &intersectdest,
21d97 20 30 2c 20 30 2c 20 30 2c 20 61 66 66 29 3b 0a   0, 0, 0, aff);.
21d98 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
21d99 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c          goto mul
21d9a 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20  ti_select_end;. 
21d9b 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
21d9c 20 43 6f 64 65 20 74 68 65 20 63 75 72 72 65 6e   Code the curren
21d9d 74 20 53 45 4c 45 43 54 20 69 6e 74 6f 20 74 65  t SELECT into te
21d9e 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 22 74  mporary table "t
21d9f 61 62 32 22 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ab2".      */.  
21da0 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
21da1 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
21da2 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
21da3 2c 20 74 61 62 32 2c 20 30 29 3b 0a 20 20 20 20  , tab2, 0);.    
21da4 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 64 64    assert( p->add
21da5 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 3d 20  rOpenEphm[1] == 
21da6 2d 31 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61  -1 );.      p->a
21da7 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d  ddrOpenEphm[1] =
21da8 20 61 64 64 72 3b 0a 20 20 20 20 20 20 70 2d 3e   addr;.      p->
21da9 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20  pPrior = 0;.    
21daa 20 20 70 4c 69 6d 69 74 20 3d 20 70 2d 3e 70 4c    pLimit = p->pL
21dab 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70  imit;.      p->p
21dac 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20  Limit = 0;.     
21dad 20 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e 70 4f   pOffset = p->pO
21dae 66 66 73 65 74 3b 0a 20 20 20 20 20 20 70 2d 3e  ffset;.      p->
21daf 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20  pOffset = 0;.   
21db0 20 20 20 69 6e 74 65 72 73 65 63 74 64 65 73 74     intersectdest
21db1 2e 69 50 61 72 6d 20 3d 20 74 61 62 32 3b 0a 20  .iParm = tab2;. 
21db2 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
21db3 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  3Select(pParse, 
21db4 70 2c 20 26 69 6e 74 65 72 73 65 63 74 64 65 73  p, &intersectdes
21db5 74 2c 20 30 2c 20 30 2c 20 30 2c 20 61 66 66 29  t, 0, 0, 0, aff)
21db6 3b 0a 20 20 20 20 20 20 70 2d 3e 70 50 72 69 6f  ;.      p->pPrio
21db7 72 20 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20 20  r = pPrior;.    
21db8 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
21db9 65 74 65 28 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a  ete(p->pLimit);.
21dba 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20        p->pLimit 
21dbb 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20  = pLimit;.      
21dbc 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20 70 4f 66  p->pOffset = pOf
21dbd 66 73 65 74 3b 0a 20 20 20 20 20 20 69 66 28 20  fset;.      if( 
21dbe 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  rc ){.        go
21dbf 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f  to multi_select_
21dc0 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  end;.      }..  
21dc1 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20      /* Generate 
21dc2 63 6f 64 65 20 74 6f 20 74 61 6b 65 20 74 68 65  code to take the
21dc3 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 6f 66   intersection of
21dc4 20 74 68 65 20 74 77 6f 20 74 65 6d 70 6f 72 61   the two tempora
21dc5 72 79 0a 20 20 20 20 20 20 2a 2a 20 74 61 62 6c  ry.      ** tabl
21dc6 65 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  es..      */.   
21dc7 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45     assert( p->pE
21dc8 4c 69 73 74 20 29 3b 0a 20 20 20 20 20 20 69 66  List );.      if
21dc9 28 20 64 65 73 74 2e 65 44 65 73 74 3d 3d 53 52  ( dest.eDest==SR
21dca 54 5f 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20  T_Callback ){.  
21dcb 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70 46        Select *pF
21dcc 69 72 73 74 20 3d 20 70 3b 0a 20 20 20 20 20 20  irst = p;.      
21dcd 20 20 77 68 69 6c 65 28 20 70 46 69 72 73 74 2d    while( pFirst-
21dce 3e 70 50 72 69 6f 72 20 29 20 70 46 69 72 73 74  >pPrior ) pFirst
21dcf 20 3d 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f   = pFirst->pPrio
21dd0 72 3b 0a 20 20 20 20 20 20 20 20 67 65 6e 65 72  r;.        gener
21dd1 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70  ateColumnNames(p
21dd2 50 61 72 73 65 2c 20 30 2c 20 70 46 69 72 73 74  Parse, 0, pFirst
21dd3 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 20  ->pEList);.     
21dd4 20 7d 0a 20 20 20 20 20 20 69 42 72 65 61 6b 20   }.      iBreak 
21dd5 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
21dd6 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20  eLabel(v);.     
21dd7 20 69 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33   iCont = sqlite3
21dd8 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
21dd9 3b 0a 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4c  ;.      computeL
21dda 69 6d 69 74 52 65 67 69 73 74 65 72 73 28 70 50  imitRegisters(pP
21ddb 61 72 73 65 2c 20 70 2c 20 69 42 72 65 61 6b 29  arse, p, iBreak)
21ddc 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
21ddd 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
21dde 52 65 77 69 6e 64 2c 20 74 61 62 31 2c 20 69 42  Rewind, tab1, iB
21ddf 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 72 31 20  reak);.      r1 
21de0 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
21de1 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
21de2 20 20 20 69 53 74 61 72 74 20 3d 20 73 71 6c 69     iStart = sqli
21de3 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
21de4 20 4f 50 5f 52 6f 77 4b 65 79 2c 20 74 61 62 31   OP_RowKey, tab1
21de5 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  , r1);.      sql
21de6 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
21de7 2c 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 2c 20 74  , OP_NotFound, t
21de8 61 62 32 2c 20 69 43 6f 6e 74 2c 20 72 31 29 3b  ab2, iCont, r1);
21de9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
21dea 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
21deb 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20  rse, r1);.      
21dec 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28  selectInnerLoop(
21ded 70 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 45  pParse, p, p->pE
21dee 4c 69 73 74 2c 20 74 61 62 31 2c 20 70 2d 3e 70  List, tab1, p->p
21def 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 0a 20 20  EList->nExpr,.  
21df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21df1 20 20 20 20 70 4f 72 64 65 72 42 79 2c 20 2d 31      pOrderBy, -1
21df2 2c 20 26 64 65 73 74 2c 20 69 43 6f 6e 74 2c 20  , &dest, iCont, 
21df3 69 42 72 65 61 6b 2c 20 30 29 3b 0a 20 20 20 20  iBreak, 0);.    
21df4 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
21df5 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 43 6f  olveLabel(v, iCo
21df6 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  nt);.      sqlit
21df7 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
21df8 4f 50 5f 4e 65 78 74 2c 20 74 61 62 31 2c 20 69  OP_Next, tab1, i
21df9 53 74 61 72 74 29 3b 0a 20 20 20 20 20 20 73 71  Start);.      sq
21dfa 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
21dfb 4c 61 62 65 6c 28 76 2c 20 69 42 72 65 61 6b 29  Label(v, iBreak)
21dfc 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
21dfd 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
21dfe 43 6c 6f 73 65 2c 20 74 61 62 32 2c 20 30 29 3b  Close, tab2, 0);
21dff 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
21e00 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
21e01 6c 6f 73 65 2c 20 74 61 62 31 2c 20 30 29 3b 0a  lose, tab1, 0);.
21e02 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21e03 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b   }.  }..  /* Mak
21e04 65 20 73 75 72 65 20 61 6c 6c 20 53 45 4c 45 43  e sure all SELEC
21e05 54 73 20 69 6e 20 74 68 65 20 73 74 61 74 65 6d  Ts in the statem
21e06 65 6e 74 20 68 61 76 65 20 74 68 65 20 73 61 6d  ent have the sam
21e07 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d  e number of elem
21e08 65 6e 74 73 0a 20 20 2a 2a 20 69 6e 20 74 68 65  ents.  ** in the
21e09 69 72 20 72 65 73 75 6c 74 20 73 65 74 73 2e 0a  ir result sets..
21e0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
21e0b 2d 3e 70 45 4c 69 73 74 20 26 26 20 70 50 72 69  ->pEList && pPri
21e0c 6f 72 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20  or->pEList );.  
21e0d 69 66 28 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e  if( p->pEList->n
21e0e 45 78 70 72 21 3d 70 50 72 69 6f 72 2d 3e 70 45  Expr!=pPrior->pE
21e0f 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20  List->nExpr ){. 
21e10 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
21e11 73 67 28 70 50 61 72 73 65 2c 20 22 53 45 4c 45  sg(pParse, "SELE
21e12 43 54 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20  CTs to the left 
21e13 61 6e 64 20 72 69 67 68 74 20 6f 66 20 25 73 22  and right of %s"
21e14 0a 20 20 20 20 20 20 22 20 64 6f 20 6e 6f 74 20  .      " do not 
21e15 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 6e 75  have the same nu
21e16 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63  mber of result c
21e17 6f 6c 75 6d 6e 73 22 2c 20 73 65 6c 65 63 74 4f  olumns", selectO
21e18 70 4e 61 6d 65 28 70 2d 3e 6f 70 29 29 3b 0a 20  pName(p->op));. 
21e19 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 67     rc = 1;.    g
21e1a 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74  oto multi_select
21e1b 5f 65 6e 64 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  _end;.  }..  /* 
21e1c 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  Set the number o
21e1d 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 65 6d  f columns in tem
21e1e 70 6f 72 61 72 79 20 74 61 62 6c 65 73 0a 20 20  porary tables.  
21e1f 2a 2f 0a 20 20 6e 43 6f 6c 20 3d 20 70 2d 3e 70  */.  nCol = p->p
21e20 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20  EList->nExpr;.  
21e21 77 68 69 6c 65 28 20 6e 53 65 74 50 32 20 29 7b  while( nSetP2 ){
21e22 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
21e23 43 68 61 6e 67 65 50 32 28 76 2c 20 61 53 65 74  ChangeP2(v, aSet
21e24 50 32 5b 2d 2d 6e 53 65 74 50 32 5d 2c 20 6e 43  P2[--nSetP2], nC
21e25 6f 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  ol);.  }..  /* C
21e26 6f 6d 70 75 74 65 20 63 6f 6c 6c 61 74 69 6e 67  ompute collating
21e27 20 73 65 71 75 65 6e 63 65 73 20 75 73 65 64 20   sequences used 
21e28 62 79 20 65 69 74 68 65 72 20 74 68 65 20 4f 52  by either the OR
21e29 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 6f 72  DER BY clause or
21e2a 0a 20 20 2a 2a 20 62 79 20 61 6e 79 20 74 65 6d  .  ** by any tem
21e2b 70 6f 72 61 72 79 20 74 61 62 6c 65 73 20 6e 65  porary tables ne
21e2c 65 64 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  eded to implemen
21e2d 74 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73  t the compound s
21e2e 65 6c 65 63 74 2e 0a 20 20 2a 2a 20 41 74 74 61  elect..  ** Atta
21e2f 63 68 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73  ch the KeyInfo s
21e30 74 72 75 63 74 75 72 65 20 74 6f 20 61 6c 6c 20  tructure to all 
21e31 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73  temporary tables
21e32 2e 20 20 49 6e 76 6f 6b 65 20 74 68 65 0a 20 20  .  Invoke the.  
21e33 2a 2a 20 4f 52 44 45 52 20 42 59 20 70 72 6f 63  ** ORDER BY proc
21e34 65 73 73 69 6e 67 20 69 66 20 74 68 65 72 65 20  essing if there 
21e35 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63  is an ORDER BY c
21e36 6c 61 75 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  lause..  **.  **
21e37 20 54 68 69 73 20 73 65 63 74 69 6f 6e 20 69 73   This section is
21e38 20 72 75 6e 20 62 79 20 74 68 65 20 72 69 67 68   run by the righ
21e39 74 2d 6d 6f 73 74 20 53 45 4c 45 43 54 20 73 74  t-most SELECT st
21e3a 61 74 65 6d 65 6e 74 20 6f 6e 6c 79 2e 0a 20 20  atement only..  
21e3b 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  ** SELECT statem
21e3c 65 6e 74 73 20 74 6f 20 74 68 65 20 6c 65 66 74  ents to the left
21e3d 20 61 6c 77 61 79 73 20 73 6b 69 70 20 74 68 69   always skip thi
21e3e 73 20 70 61 72 74 2e 20 20 54 68 65 20 72 69 67  s part.  The rig
21e3f 68 74 2d 6d 6f 73 74 0a 20 20 2a 2a 20 53 45 4c  ht-most.  ** SEL
21e40 45 43 54 20 6d 69 67 68 74 20 61 6c 73 6f 20 73  ECT might also s
21e41 6b 69 70 20 74 68 69 73 20 70 61 72 74 20 69 66  kip this part if
21e42 20 69 74 20 68 61 73 20 6e 6f 20 4f 52 44 45 52   it has no ORDER
21e43 20 42 59 20 63 6c 61 75 73 65 20 61 6e 64 0a 20   BY clause and. 
21e44 20 2a 2a 20 6e 6f 20 74 65 6d 70 20 74 61 62 6c   ** no temp tabl
21e45 65 73 20 61 72 65 20 72 65 71 75 69 72 65 64 2e  es are required.
21e46 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4f 72 64  .  */.  if( pOrd
21e47 65 72 42 79 20 7c 7c 20 70 2d 3e 75 73 65 73 45  erBy || p->usesE
21e48 70 68 6d 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  phm ){.    int i
21e49 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
21e4a 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
21e4b 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20   counter */.    
21e4c 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66  KeyInfo *pKeyInf
21e4d 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  o;            /*
21e4e 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   Collating seque
21e4f 6e 63 65 20 66 6f 72 20 74 68 65 20 72 65 73 75  nce for the resu
21e50 6c 74 20 73 65 74 20 2a 2f 0a 20 20 20 20 53 65  lt set */.    Se
21e51 6c 65 63 74 20 2a 70 4c 6f 6f 70 3b 20 20 20 20  lect *pLoop;    
21e52 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
21e53 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f 75  or looping throu
21e54 67 68 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  gh SELECT statem
21e55 65 6e 74 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ents */.    int 
21e56 6e 4b 65 79 43 6f 6c 3b 20 20 20 20 20 20 20 20  nKeyCol;        
21e57 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
21e58 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
21e59 6e 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c  n pKeyInfo->aCol
21e5a 5b 5d 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53 65  [] */.    CollSe
21e5b 71 20 2a 2a 61 70 43 6f 6c 6c 3b 20 20 20 20 20  q **apColl;     
21e5c 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c          /* For l
21e5d 6f 6f 70 69 6e 67 20 74 68 72 6f 75 67 68 20 70  ooping through p
21e5e 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 5d  KeyInfo->aColl[]
21e5f 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20   */.    CollSeq 
21e60 2a 2a 61 43 6f 70 79 3b 20 20 20 20 20 20 20 20  **aCopy;        
21e61 20 20 20 20 20 20 2f 2a 20 41 20 63 6f 70 79 20        /* A copy 
21e62 6f 66 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f  of pKeyInfo->aCo
21e63 6c 6c 5b 5d 20 2a 2f 0a 0a 20 20 20 20 61 73 73  ll[] */..    ass
21e64 65 72 74 28 20 70 2d 3e 70 52 69 67 68 74 6d 6f  ert( p->pRightmo
21e65 73 74 3d 3d 70 20 29 3b 0a 20 20 20 20 6e 4b 65  st==p );.    nKe
21e66 79 43 6f 6c 20 3d 20 6e 43 6f 6c 20 2b 20 28 70  yCol = nCol + (p
21e67 4f 72 64 65 72 42 79 20 3f 20 70 4f 72 64 65 72  OrderBy ? pOrder
21e68 42 79 2d 3e 6e 45 78 70 72 20 3a 20 30 29 3b 0a  By->nExpr : 0);.
21e69 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 73      pKeyInfo = s
21e6a 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
21e6b 72 6f 28 70 50 61 72 73 65 2d 3e 64 62 2c 0a 20  ro(pParse->db,. 
21e6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21e6d 20 20 20 20 20 20 73 69 7a 65 6f 66 28 2a 70 4b        sizeof(*pK
21e6e 65 79 49 6e 66 6f 29 2b 6e 4b 65 79 43 6f 6c 2a  eyInfo)+nKeyCol*
21e6f 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a  (sizeof(CollSeq*
21e70 29 20 2b 20 31 29 29 3b 0a 20 20 20 20 69 66 28  ) + 1));.    if(
21e71 20 21 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 20   !pKeyInfo ){.  
21e72 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
21e73 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74  NOMEM;.      got
21e74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65  o multi_select_e
21e75 6e 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70  nd;.    }..    p
21e76 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45  KeyInfo->enc = E
21e77 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29 3b 0a  NC(pParse->db);.
21e78 20 20 20 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46      pKeyInfo->nF
21e79 69 65 6c 64 20 3d 20 6e 43 6f 6c 3b 0a 0a 20 20  ield = nCol;..  
21e7a 20 20 66 6f 72 28 69 3d 30 2c 20 61 70 43 6f 6c    for(i=0, apCol
21e7b 6c 3d 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c  l=pKeyInfo->aCol
21e7c 6c 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 2c 20  l; i<nCol; i++, 
21e7d 61 70 43 6f 6c 6c 2b 2b 29 7b 0a 20 20 20 20 20  apColl++){.     
21e7e 20 2a 61 70 43 6f 6c 6c 20 3d 20 6d 75 6c 74 69   *apColl = multi
21e7f 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28 70 50  SelectCollSeq(pP
21e80 61 72 73 65 2c 20 70 2c 20 69 29 3b 0a 20 20 20  arse, p, i);.   
21e81 20 20 20 69 66 28 20 30 3d 3d 2a 61 70 43 6f 6c     if( 0==*apCol
21e82 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 61 70  l ){.        *ap
21e83 43 6f 6c 6c 20 3d 20 70 50 61 72 73 65 2d 3e 64  Coll = pParse->d
21e84 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20  b->pDfltColl;.  
21e85 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
21e86 20 66 6f 72 28 70 4c 6f 6f 70 3d 70 3b 20 70 4c   for(pLoop=p; pL
21e87 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70 4c 6f 6f 70  oop; pLoop=pLoop
21e88 2d 3e 70 50 72 69 6f 72 29 7b 0a 20 20 20 20 20  ->pPrior){.     
21e89 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 3b 20 69   for(i=0; i<2; i
21e8a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  ++){.        int
21e8b 20 61 64 64 72 20 3d 20 70 4c 6f 6f 70 2d 3e 61   addr = pLoop->a
21e8c 64 64 72 4f 70 65 6e 45 70 68 6d 5b 69 5d 3b 0a  ddrOpenEphm[i];.
21e8d 20 20 20 20 20 20 20 20 69 66 28 20 61 64 64 72          if( addr
21e8e 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  <0 ){.          
21e8f 2f 2a 20 49 66 20 5b 30 5d 20 69 73 20 75 6e 75  /* If [0] is unu
21e90 73 65 64 20 74 68 65 6e 20 5b 31 5d 20 69 73 20  sed then [1] is 
21e91 61 6c 73 6f 20 75 6e 75 73 65 64 2e 20 20 53 6f  also unused.  So
21e92 20 77 65 20 63 61 6e 0a 20 20 20 20 20 20 20 20   we can.        
21e93 20 20 2a 2a 20 61 6c 77 61 79 73 20 73 61 66 65    ** always safe
21e94 6c 79 20 61 62 6f 72 74 20 61 73 20 73 6f 6f 6e  ly abort as soon
21e95 20 61 73 20 74 68 65 20 66 69 72 73 74 20 75 6e   as the first un
21e96 75 73 65 64 20 73 6c 6f 74 20 69 73 20 66 6f 75  used slot is fou
21e97 6e 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  nd */.          
21e98 61 73 73 65 72 74 28 20 70 4c 6f 6f 70 2d 3e 61  assert( pLoop->a
21e99 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 3c 30  ddrOpenEphm[1]<0
21e9a 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72   );.          br
21e9b 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
21e9c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
21e9d 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 61 64  beChangeP2(v, ad
21e9e 64 72 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20 20 20  dr, nCol);.     
21e9f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
21ea0 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2c 20  angeP4(v, addr, 
21ea1 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c  (char*)pKeyInfo,
21ea2 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20 20   P4_KEYINFO);.  
21ea3 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e 61 64 64        pLoop->add
21ea4 72 4f 70 65 6e 45 70 68 6d 5b 69 5d 20 3d 20 2d  rOpenEphm[i] = -
21ea5 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
21ea6 0a 0a 20 20 20 20 69 66 28 20 70 4f 72 64 65 72  ..    if( pOrder
21ea7 42 79 20 29 7b 0a 20 20 20 20 20 20 73 74 72 75  By ){.      stru
21ea8 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d  ct ExprList_item
21ea9 20 2a 70 4f 54 65 72 6d 20 3d 20 70 4f 72 64 65   *pOTerm = pOrde
21eaa 72 42 79 2d 3e 61 3b 0a 20 20 20 20 20 20 69 6e  rBy->a;.      in
21eab 74 20 6e 4f 72 64 65 72 42 79 45 78 70 72 20 3d  t nOrderByExpr =
21eac 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72   pOrderBy->nExpr
21ead 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72  ;.      int addr
21eae 3b 0a 20 20 20 20 20 20 75 38 20 2a 70 53 6f 72  ;.      u8 *pSor
21eaf 74 4f 72 64 65 72 3b 0a 0a 20 20 20 20 20 20 2f  tOrder;..      /
21eb0 2a 20 52 65 75 73 65 20 74 68 65 20 73 61 6d 65  * Reuse the same
21eb1 20 70 4b 65 79 49 6e 66 6f 20 66 6f 72 20 74 68   pKeyInfo for th
21eb2 65 20 4f 52 44 45 52 20 42 59 20 61 73 20 77 61  e ORDER BY as wa
21eb3 73 20 75 73 65 64 20 61 62 6f 76 65 20 66 6f 72  s used above for
21eb4 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63 6f  .      ** the co
21eb5 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 73 74  mpound select st
21eb6 61 74 65 6d 65 6e 74 73 2e 20 20 45 78 63 65 70  atements.  Excep
21eb7 74 20 77 65 20 68 61 76 65 20 74 6f 20 63 68 61  t we have to cha
21eb8 6e 67 65 20 6f 75 74 20 74 68 65 0a 20 20 20 20  nge out the.    
21eb9 20 20 2a 2a 20 70 4b 65 79 49 6e 66 6f 2d 3e 61    ** pKeyInfo->a
21eba 43 6f 6c 6c 5b 5d 20 76 61 6c 75 65 73 2e 20 20  Coll[] values.  
21ebb 53 6f 6d 65 20 6f 66 20 74 68 65 20 61 43 6f 6c  Some of the aCol
21ebc 6c 5b 5d 20 76 61 6c 75 65 73 20 77 69 6c 6c 20  l[] values will 
21ebd 62 65 0a 20 20 20 20 20 20 2a 2a 20 72 65 75 73  be.      ** reus
21ebe 65 64 20 77 68 65 6e 20 63 6f 6e 73 74 72 75 63  ed when construc
21ebf 74 69 6e 67 20 74 68 65 20 70 4b 65 79 49 6e 66  ting the pKeyInf
21ec0 6f 20 66 6f 72 20 74 68 65 20 4f 52 44 45 52 20  o for the ORDER 
21ec1 42 59 2c 20 73 6f 20 6d 61 6b 65 0a 20 20 20 20  BY, so make.    
21ec2 20 20 2a 2a 20 61 20 63 6f 70 79 2e 20 20 53 75    ** a copy.  Su
21ec3 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 20 74  fficient space t
21ec4 6f 20 68 6f 6c 64 20 62 6f 74 68 20 74 68 65 20  o hold both the 
21ec5 6e 43 6f 6c 20 65 6e 74 72 69 65 73 20 66 6f 72  nCol entries for
21ec6 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63 6f  .      ** the co
21ec7 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 61 6e  mpound select an
21ec8 64 20 74 68 65 20 6e 4f 72 64 65 72 62 79 45 78  d the nOrderbyEx
21ec9 70 72 20 65 6e 74 72 69 65 73 20 66 6f 72 20 74  pr entries for t
21eca 68 65 20 4f 52 44 45 52 20 42 59 0a 20 20 20 20  he ORDER BY.    
21ecb 20 20 2a 2a 20 77 61 73 20 61 6c 6c 6f 63 61 74    ** was allocat
21ecc 65 64 20 61 62 6f 76 65 2e 20 20 42 75 74 20 77  ed above.  But w
21ecd 65 20 6e 65 65 64 20 74 6f 20 6d 6f 76 65 20 74  e need to move t
21ece 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65  he compound sele
21ecf 63 74 0a 20 20 20 20 20 20 2a 2a 20 65 6e 74 72  ct.      ** entr
21ed0 69 65 73 20 6f 75 74 20 6f 66 20 74 68 65 20 77  ies out of the w
21ed1 61 79 20 62 65 66 6f 72 65 20 63 6f 6e 73 74 72  ay before constr
21ed2 75 63 74 69 6e 67 20 74 68 65 20 4f 52 44 45 52  ucting the ORDER
21ed3 20 42 59 20 65 6e 74 72 69 65 73 2e 0a 20 20 20   BY entries..   
21ed4 20 20 20 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63     ** Move the c
21ed5 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 65  ompound select e
21ed6 6e 74 72 69 65 73 20 69 6e 74 6f 20 61 43 6f 70  ntries into aCop
21ed7 79 5b 5d 20 77 68 65 72 65 20 74 68 65 79 20 63  y[] where they c
21ed8 61 6e 20 62 65 0a 20 20 20 20 20 20 2a 2a 20 61  an be.      ** a
21ed9 63 63 65 73 73 65 64 20 61 6e 64 20 72 65 75 73  ccessed and reus
21eda 65 64 20 77 68 65 6e 20 63 6f 6e 73 74 72 75 63  ed when construc
21edb 74 69 6e 67 20 74 68 65 20 4f 52 44 45 52 20 42  ting the ORDER B
21edc 59 20 65 6e 74 72 69 65 73 2e 0a 20 20 20 20 20  Y entries..     
21edd 20 2a 2a 20 42 65 63 61 75 73 65 20 6e 43 6f 6c   ** Because nCol
21ede 20 6d 69 67 68 74 20 62 65 20 67 72 65 61 74 65   might be greate
21edf 72 20 74 68 61 6e 20 6f 72 20 6c 65 73 73 20 74  r than or less t
21ee0 68 61 6e 20 6e 4f 72 64 65 72 42 79 45 78 70 72  han nOrderByExpr
21ee1 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 68 61 76  .      ** we hav
21ee2 65 20 74 6f 20 75 73 65 20 6d 65 6d 6d 6f 76 65  e to use memmove
21ee3 28 29 20 77 68 65 6e 20 64 6f 69 6e 67 20 74 68  () when doing th
21ee4 65 20 63 6f 70 79 2e 0a 20 20 20 20 20 20 2a 2f  e copy..      */
21ee5 0a 20 20 20 20 20 20 61 43 6f 70 79 20 3d 20 26  .      aCopy = &
21ee6 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b  pKeyInfo->aColl[
21ee7 6e 4f 72 64 65 72 42 79 45 78 70 72 5d 3b 0a 20  nOrderByExpr];. 
21ee8 20 20 20 20 20 70 53 6f 72 74 4f 72 64 65 72 20       pSortOrder 
21ee9 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72  = pKeyInfo->aSor
21eea 74 4f 72 64 65 72 20 3d 20 28 75 38 2a 29 26 61  tOrder = (u8*)&a
21eeb 43 6f 70 79 5b 6e 43 6f 6c 5d 3b 0a 20 20 20 20  Copy[nCol];.    
21eec 20 20 6d 65 6d 6d 6f 76 65 28 61 43 6f 70 79 2c    memmove(aCopy,
21eed 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c   pKeyInfo->aColl
21eee 2c 20 6e 43 6f 6c 2a 73 69 7a 65 6f 66 28 43 6f  , nCol*sizeof(Co
21eef 6c 6c 53 65 71 2a 29 29 3b 0a 0a 20 20 20 20 20  llSeq*));..     
21ef0 20 61 70 43 6f 6c 6c 20 3d 20 70 4b 65 79 49 6e   apColl = pKeyIn
21ef1 66 6f 2d 3e 61 43 6f 6c 6c 3b 0a 20 20 20 20 20  fo->aColl;.     
21ef2 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 72 64   for(i=0; i<nOrd
21ef3 65 72 42 79 45 78 70 72 3b 20 69 2b 2b 2c 20 70  erByExpr; i++, p
21ef4 4f 54 65 72 6d 2b 2b 2c 20 61 70 43 6f 6c 6c 2b  OTerm++, apColl+
21ef5 2b 2c 20 70 53 6f 72 74 4f 72 64 65 72 2b 2b 29  +, pSortOrder++)
21ef6 7b 0a 20 20 20 20 20 20 20 20 45 78 70 72 20 2a  {.        Expr *
21ef7 70 45 78 70 72 20 3d 20 70 4f 54 65 72 6d 2d 3e  pExpr = pOTerm->
21ef8 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 69  pExpr;.        i
21ef9 66 28 20 28 70 45 78 70 72 2d 3e 66 6c 61 67 73  f( (pExpr->flags
21efa 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65   & EP_ExpCollate
21efb 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  ) ){.          a
21efc 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 43  ssert( pExpr->pC
21efd 6f 6c 6c 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  oll!=0 );.      
21efe 20 20 20 20 2a 61 70 43 6f 6c 6c 20 3d 20 70 45      *apColl = pE
21eff 78 70 72 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20  xpr->pColl;.    
21f00 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
21f01 20 20 20 20 20 2a 61 70 43 6f 6c 6c 20 3d 20 61       *apColl = a
21f02 43 6f 70 79 5b 70 45 78 70 72 2d 3e 69 43 6f 6c  Copy[pExpr->iCol
21f03 75 6d 6e 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a  umn];.        }.
21f04 20 20 20 20 20 20 20 20 2a 70 53 6f 72 74 4f 72          *pSortOr
21f05 64 65 72 20 3d 20 70 4f 54 65 72 6d 2d 3e 73 6f  der = pOTerm->so
21f06 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 20 20 7d  rtOrder;.      }
21f07 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
21f08 2d 3e 70 52 69 67 68 74 6d 6f 73 74 3d 3d 70 20  ->pRightmost==p 
21f09 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
21f0a 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d   p->addrOpenEphm
21f0b 5b 32 5d 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20  [2]>=0 );.      
21f0c 61 64 64 72 20 3d 20 70 2d 3e 61 64 64 72 4f 70  addr = p->addrOp
21f0d 65 6e 45 70 68 6d 5b 32 5d 3b 0a 20 20 20 20 20  enEphm[2];.     
21f0e 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
21f0f 67 65 50 32 28 76 2c 20 61 64 64 72 2c 20 70 2d  geP2(v, addr, p-
21f10 3e 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72  >pOrderBy->nExpr
21f11 2b 32 29 3b 0a 20 20 20 20 20 20 70 4b 65 79 49  +2);.      pKeyI
21f12 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 3d 20 6e 4f  nfo->nField = nO
21f13 72 64 65 72 42 79 45 78 70 72 3b 0a 20 20 20 20  rderByExpr;.    
21f14 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
21f15 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2c 20 28  ngeP4(v, addr, (
21f16 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20  char*)pKeyInfo, 
21f17 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
21f18 46 46 29 3b 0a 20 20 20 20 20 20 70 4b 65 79 49  FF);.      pKeyI
21f19 6e 66 6f 20 3d 20 30 3b 0a 20 20 20 20 20 20 67  nfo = 0;.      g
21f1a 65 6e 65 72 61 74 65 53 6f 72 74 54 61 69 6c 28  enerateSortTail(
21f1b 70 50 61 72 73 65 2c 20 70 2c 20 76 2c 20 70 2d  pParse, p, v, p-
21f1c 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20  >pEList->nExpr, 
21f1d 26 64 65 73 74 29 3b 0a 20 20 20 20 7d 0a 0a 20  &dest);.    }.. 
21f1e 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
21f1f 70 4b 65 79 49 6e 66 6f 29 3b 0a 20 20 7d 0a 0a  pKeyInfo);.  }..
21f20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64  multi_select_end
21f21 3a 0a 20 20 70 44 65 73 74 2d 3e 69 4d 65 6d 20  :.  pDest->iMem 
21f22 3d 20 64 65 73 74 2e 69 4d 65 6d 3b 0a 20 20 70  = dest.iMem;.  p
21f23 44 65 73 74 2d 3e 6e 4d 65 6d 20 3d 20 64 65 73  Dest->nMem = des
21f24 74 2e 6e 4d 65 6d 3b 0a 20 20 72 65 74 75 72 6e  t.nMem;.  return
21f25 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a   rc;.}.#endif /*
21f26 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d   SQLITE_OMIT_COM
21f27 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 2a 2f 0a  POUND_SELECT */.
21f28 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
21f29 4f 4d 49 54 5f 56 49 45 57 0a 2f 2a 20 46 6f 72  OMIT_VIEW./* For
21f2a 77 61 72 64 20 44 65 63 6c 61 72 61 74 69 6f 6e  ward Declaration
21f2b 73 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  s */.static void
21f2c 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 73   substExprList(s
21f2d 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 4c 69 73  qlite3*, ExprLis
21f2e 74 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73  t*, int, ExprLis
21f2f 74 2a 29 3b 0a 73 74 61 74 69 63 20 76 6f 69 64  t*);.static void
21f30 20 73 75 62 73 74 53 65 6c 65 63 74 28 73 71 6c   substSelect(sql
21f31 69 74 65 33 2a 2c 20 53 65 6c 65 63 74 20 2a 2c  ite3*, Select *,
21f32 20 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 20 2a   int, ExprList *
21f33 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 63 61 6e 20 74  );../*.** Scan t
21f34 68 72 6f 75 67 68 20 74 68 65 20 65 78 70 72 65  hrough the expre
21f35 73 73 69 6f 6e 20 70 45 78 70 72 2e 20 20 52 65  ssion pExpr.  Re
21f36 70 6c 61 63 65 20 65 76 65 72 79 20 72 65 66 65  place every refe
21f37 72 65 6e 63 65 20 74 6f 0a 2a 2a 20 61 20 63 6f  rence to.** a co
21f38 6c 75 6d 6e 20 69 6e 20 74 61 62 6c 65 20 6e 75  lumn in table nu
21f39 6d 62 65 72 20 69 54 61 62 6c 65 20 77 69 74 68  mber iTable with
21f3a 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 69   a copy of the i
21f3b 43 6f 6c 75 6d 6e 2d 74 68 0a 2a 2a 20 65 6e 74  Column-th.** ent
21f3c 72 79 20 69 6e 20 70 45 4c 69 73 74 2e 20 20 28  ry in pEList.  (
21f3d 42 75 74 20 6c 65 61 76 65 20 72 65 66 65 72 65  But leave refere
21f3e 6e 63 65 73 20 74 6f 20 74 68 65 20 52 4f 57 49  nces to the ROWI
21f3f 44 20 63 6f 6c 75 6d 6e 20 0a 2a 2a 20 75 6e 63  D column .** unc
21f40 68 61 6e 67 65 64 2e 29 0a 2a 2a 0a 2a 2a 20 54  hanged.).**.** T
21f41 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 70  his routine is p
21f42 61 72 74 20 6f 66 20 74 68 65 20 66 6c 61 74 74  art of the flatt
21f43 65 6e 69 6e 67 20 70 72 6f 63 65 64 75 72 65 2e  ening procedure.
21f44 20 20 41 20 73 75 62 71 75 65 72 79 0a 2a 2a 20    A subquery.** 
21f45 77 68 6f 73 65 20 72 65 73 75 6c 74 20 73 65 74  whose result set
21f46 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 70   is defined by p
21f47 45 4c 69 73 74 20 61 70 70 65 61 72 73 20 61 73  EList appears as
21f48 20 65 6e 74 72 79 20 69 6e 20 74 68 65 0a 2a 2a   entry in the.**
21f49 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
21f4a 61 20 53 45 4c 45 43 54 20 73 75 63 68 20 74 68  a SELECT such th
21f4b 61 74 20 74 68 65 20 56 44 42 45 20 63 75 72 73  at the VDBE curs
21f4c 6f 72 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74  or assigned to t
21f4d 68 61 74 0a 2a 2a 20 46 4f 52 4d 20 63 6c 61 75  hat.** FORM clau
21f4e 73 65 20 65 6e 74 72 79 20 69 73 20 69 54 61 62  se entry is iTab
21f4f 6c 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  le.  This routin
21f50 65 20 6d 61 6b 65 20 74 68 65 20 6e 65 63 65 73  e make the neces
21f51 73 61 72 79 20 0a 2a 2a 20 63 68 61 6e 67 65 73  sary .** changes
21f52 20 74 6f 20 70 45 78 70 72 20 73 6f 20 74 68 61   to pExpr so tha
21f53 74 20 69 74 20 72 65 66 65 72 73 20 64 69 72 65  t it refers dire
21f54 63 74 6c 79 20 74 6f 20 74 68 65 20 73 6f 75 72  ctly to the sour
21f55 63 65 20 74 61 62 6c 65 0a 2a 2a 20 6f 66 20 74  ce table.** of t
21f56 68 65 20 73 75 62 71 75 65 72 79 20 72 61 74 68  he subquery rath
21f57 65 72 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  er the result se
21f58 74 20 6f 66 20 74 68 65 20 73 75 62 71 75 65 72  t of the subquer
21f59 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  y..*/.static voi
21f5a 64 20 73 75 62 73 74 45 78 70 72 28 0a 20 20 73  d substExpr(.  s
21f5b 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
21f5c 20 20 20 2f 2a 20 52 65 70 6f 72 74 20 6d 61 6c     /* Report mal
21f5d 6c 6f 63 20 65 72 72 6f 72 73 20 74 6f 20 74 68  loc errors to th
21f5e 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  is connection */
21f5f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20  .  Expr *pExpr, 
21f60 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 20 69         /* Expr i
21f61 6e 20 77 68 69 63 68 20 73 75 62 73 74 69 74 75  n which substitu
21f62 74 69 6f 6e 20 6f 63 63 75 72 73 20 2a 2f 0a 20  tion occurs */. 
21f63 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20   int iTable,    
21f64 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f       /* Table to
21f65 20 62 65 20 73 75 62 73 74 69 74 75 74 65 64 20   be substituted 
21f66 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
21f67 45 4c 69 73 74 20 20 20 20 2f 2a 20 53 75 62 73  EList    /* Subs
21f68 74 69 74 75 74 65 20 65 78 70 72 65 73 73 69 6f  titute expressio
21f69 6e 73 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 70  ns */.){.  if( p
21f6a 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e  Expr==0 ) return
21f6b 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f  ;.  if( pExpr->o
21f6c 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20  p==TK_COLUMN && 
21f6d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 69  pExpr->iTable==i
21f6e 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 69 66 28  Table ){.    if(
21f6f 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c   pExpr->iColumn<
21f70 30 20 29 7b 0a 20 20 20 20 20 20 70 45 78 70 72  0 ){.      pExpr
21f71 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a  ->op = TK_NULL;.
21f72 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
21f73 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 20   Expr *pNew;.   
21f74 20 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73     assert( pELis
21f75 74 21 3d 30 20 26 26 20 70 45 78 70 72 2d 3e 69  t!=0 && pExpr->i
21f76 43 6f 6c 75 6d 6e 3c 70 45 4c 69 73 74 2d 3e 6e  Column<pEList->n
21f77 45 78 70 72 20 29 3b 0a 20 20 20 20 20 20 61 73  Expr );.      as
21f78 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 4c 65  sert( pExpr->pLe
21f79 66 74 3d 3d 30 20 26 26 20 70 45 78 70 72 2d 3e  ft==0 && pExpr->
21f7a 70 52 69 67 68 74 3d 3d 30 20 26 26 20 70 45 78  pRight==0 && pEx
21f7b 70 72 2d 3e 70 4c 69 73 74 3d 3d 30 20 29 3b 0a  pr->pList==0 );.
21f7c 20 20 20 20 20 20 70 4e 65 77 20 3d 20 70 45 4c        pNew = pEL
21f7d 69 73 74 2d 3e 61 5b 70 45 78 70 72 2d 3e 69 43  ist->a[pExpr->iC
21f7e 6f 6c 75 6d 6e 5d 2e 70 45 78 70 72 3b 0a 20 20  olumn].pExpr;.  
21f7f 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 77      assert( pNew
21f80 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 45 78  !=0 );.      pEx
21f81 70 72 2d 3e 6f 70 20 3d 20 70 4e 65 77 2d 3e 6f  pr->op = pNew->o
21f82 70 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  p;.      assert(
21f83 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3d 3d 30   pExpr->pLeft==0
21f84 20 29 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d   );.      pExpr-
21f85 3e 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33  >pLeft = sqlite3
21f86 45 78 70 72 44 75 70 28 64 62 2c 20 70 4e 65 77  ExprDup(db, pNew
21f87 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20  ->pLeft);.      
21f88 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70  assert( pExpr->p
21f89 52 69 67 68 74 3d 3d 30 20 29 3b 0a 20 20 20 20  Right==0 );.    
21f8a 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20    pExpr->pRight 
21f8b 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
21f8c 28 64 62 2c 20 70 4e 65 77 2d 3e 70 52 69 67 68  (db, pNew->pRigh
21f8d 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  t);.      assert
21f8e 28 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3d 3d  ( pExpr->pList==
21f8f 30 20 29 3b 0a 20 20 20 20 20 20 70 45 78 70 72  0 );.      pExpr
21f90 2d 3e 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65  ->pList = sqlite
21f91 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c  3ExprListDup(db,
21f92 20 70 4e 65 77 2d 3e 70 4c 69 73 74 29 3b 0a 20   pNew->pList);. 
21f93 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62       pExpr->iTab
21f94 6c 65 20 3d 20 70 4e 65 77 2d 3e 69 54 61 62 6c  le = pNew->iTabl
21f95 65 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e  e;.      pExpr->
21f96 70 54 61 62 20 3d 20 70 4e 65 77 2d 3e 70 54 61  pTab = pNew->pTa
21f97 62 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e  b;.      pExpr->
21f98 69 43 6f 6c 75 6d 6e 20 3d 20 70 4e 65 77 2d 3e  iColumn = pNew->
21f99 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 70  iColumn;.      p
21f9a 45 78 70 72 2d 3e 69 41 67 67 20 3d 20 70 4e 65  Expr->iAgg = pNe
21f9b 77 2d 3e 69 41 67 67 3b 0a 20 20 20 20 20 20 73  w->iAgg;.      s
21f9c 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70 79 28  qlite3TokenCopy(
21f9d 64 62 2c 20 26 70 45 78 70 72 2d 3e 74 6f 6b 65  db, &pExpr->toke
21f9e 6e 2c 20 26 70 4e 65 77 2d 3e 74 6f 6b 65 6e 29  n, &pNew->token)
21f9f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 54  ;.      sqlite3T
21fa0 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26 70 45  okenCopy(db, &pE
21fa1 78 70 72 2d 3e 73 70 61 6e 2c 20 26 70 4e 65 77  xpr->span, &pNew
21fa2 2d 3e 73 70 61 6e 29 3b 0a 20 20 20 20 20 20 70  ->span);.      p
21fa3 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 20 3d 20  Expr->pSelect = 
21fa4 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70  sqlite3SelectDup
21fa5 28 64 62 2c 20 70 4e 65 77 2d 3e 70 53 65 6c 65  (db, pNew->pSele
21fa6 63 74 29 3b 0a 20 20 20 20 20 20 70 45 78 70 72  ct);.      pExpr
21fa7 2d 3e 66 6c 61 67 73 20 3d 20 70 4e 65 77 2d 3e  ->flags = pNew->
21fa8 66 6c 61 67 73 3b 0a 20 20 20 20 7d 0a 20 20 7d  flags;.    }.  }
21fa9 65 6c 73 65 7b 0a 20 20 20 20 73 75 62 73 74 45  else{.    substE
21faa 78 70 72 28 64 62 2c 20 70 45 78 70 72 2d 3e 70  xpr(db, pExpr->p
21fab 4c 65 66 74 2c 20 69 54 61 62 6c 65 2c 20 70 45  Left, iTable, pE
21fac 4c 69 73 74 29 3b 0a 20 20 20 20 73 75 62 73 74  List);.    subst
21fad 45 78 70 72 28 64 62 2c 20 70 45 78 70 72 2d 3e  Expr(db, pExpr->
21fae 70 52 69 67 68 74 2c 20 69 54 61 62 6c 65 2c 20  pRight, iTable, 
21faf 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 73 75 62  pEList);.    sub
21fb0 73 74 53 65 6c 65 63 74 28 64 62 2c 20 70 45 78  stSelect(db, pEx
21fb1 70 72 2d 3e 70 53 65 6c 65 63 74 2c 20 69 54 61  pr->pSelect, iTa
21fb2 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  ble, pEList);.  
21fb3 20 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28    substExprList(
21fb4 64 62 2c 20 70 45 78 70 72 2d 3e 70 4c 69 73 74  db, pExpr->pList
21fb5 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74  , iTable, pEList
21fb6 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20  );.  }.}.static 
21fb7 76 6f 69 64 20 73 75 62 73 74 45 78 70 72 4c 69  void substExprLi
21fb8 73 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  st(.  sqlite3 *d
21fb9 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  b,         /* Re
21fba 70 6f 72 74 20 6d 61 6c 6c 6f 63 20 65 72 72 6f  port malloc erro
21fbb 72 73 20 68 65 72 65 20 2a 2f 0a 20 20 45 78 70  rs here */.  Exp
21fbc 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20  rList *pList,   
21fbd 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 73 63 61    /* List to sca
21fbe 6e 20 61 6e 64 20 69 6e 20 77 68 69 63 68 20 74  n and in which t
21fbf 6f 20 6d 61 6b 65 20 73 75 62 73 74 69 74 75 74  o make substitut
21fc0 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62  es */.  int iTab
21fc1 6c 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  le,          /* 
21fc2 54 61 62 6c 65 20 74 6f 20 62 65 20 73 75 62 73  Table to be subs
21fc3 74 69 74 75 74 65 64 20 2a 2f 0a 20 20 45 78 70  tituted */.  Exp
21fc4 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 20 20  rList *pEList   
21fc5 20 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20    /* Substitute 
21fc6 76 61 6c 75 65 73 20 2a 2f 0a 29 7b 0a 20 20 69  values */.){.  i
21fc7 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73  nt i;.  if( pLis
21fc8 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  t==0 ) return;. 
21fc9 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73   for(i=0; i<pLis
21fca 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  t->nExpr; i++){.
21fcb 20 20 20 20 73 75 62 73 74 45 78 70 72 28 64 62      substExpr(db
21fcc 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45  , pList->a[i].pE
21fcd 78 70 72 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c  xpr, iTable, pEL
21fce 69 73 74 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74  ist);.  }.}.stat
21fcf 69 63 20 76 6f 69 64 20 73 75 62 73 74 53 65 6c  ic void substSel
21fd0 65 63 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ect(.  sqlite3 *
21fd1 64 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52  db,         /* R
21fd2 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20 65 72 72  eport malloc err
21fd3 6f 72 73 20 68 65 72 65 20 2a 2f 0a 20 20 53 65  ors here */.  Se
21fd4 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20  lect *p,        
21fd5 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 73 74 61     /* SELECT sta
21fd6 74 65 6d 65 6e 74 20 69 6e 20 77 68 69 63 68 20  tement in which 
21fd7 74 6f 20 6d 61 6b 65 20 73 75 62 73 74 69 74 75  to make substitu
21fd8 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 69  tions */.  int i
21fd9 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20  Table,          
21fda 2f 2a 20 54 61 62 6c 65 20 74 6f 20 62 65 20 72  /* Table to be r
21fdb 65 70 6c 61 63 65 64 20 2a 2f 0a 20 20 45 78 70  eplaced */.  Exp
21fdc 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 20 20  rList *pEList   
21fdd 20 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20    /* Substitute 
21fde 76 61 6c 75 65 73 20 2a 2f 0a 29 7b 0a 20 20 69  values */.){.  i
21fdf 66 28 20 21 70 20 29 20 72 65 74 75 72 6e 3b 0a  f( !p ) return;.
21fe0 20 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28    substExprList(
21fe1 64 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 69  db, p->pEList, i
21fe2 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a  Table, pEList);.
21fe3 20 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28    substExprList(
21fe4 64 62 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 2c  db, p->pGroupBy,
21fe5 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29   iTable, pEList)
21fe6 3b 0a 20 20 73 75 62 73 74 45 78 70 72 4c 69 73  ;.  substExprLis
21fe7 74 28 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42  t(db, p->pOrderB
21fe8 79 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73  y, iTable, pELis
21fe9 74 29 3b 0a 20 20 73 75 62 73 74 45 78 70 72 28  t);.  substExpr(
21fea 64 62 2c 20 70 2d 3e 70 48 61 76 69 6e 67 2c 20  db, p->pHaving, 
21feb 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b  iTable, pEList);
21fec 0a 20 20 73 75 62 73 74 45 78 70 72 28 64 62 2c  .  substExpr(db,
21fed 20 70 2d 3e 70 57 68 65 72 65 2c 20 69 54 61 62   p->pWhere, iTab
21fee 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73  le, pEList);.  s
21fef 75 62 73 74 53 65 6c 65 63 74 28 64 62 2c 20 70  ubstSelect(db, p
21ff0 2d 3e 70 50 72 69 6f 72 2c 20 69 54 61 62 6c 65  ->pPrior, iTable
21ff1 2c 20 70 45 4c 69 73 74 29 3b 0a 7d 0a 23 65 6e  , pEList);.}.#en
21ff2 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
21ff3 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
21ff4 29 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  ) */..#ifndef SQ
21ff5 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 2f  LITE_OMIT_VIEW./
21ff6 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
21ff7 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 66 6c  e attempts to fl
21ff8 61 74 74 65 6e 20 73 75 62 71 75 65 72 69 65 73  atten subqueries
21ff9 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 70 65   in order to spe
21ffa 65 64 0a 2a 2a 20 65 78 65 63 75 74 69 6f 6e 2e  ed.** execution.
21ffb 20 20 49 74 20 72 65 74 75 72 6e 73 20 31 20 69    It returns 1 i
21ffc 66 20 69 74 20 6d 61 6b 65 73 20 63 68 61 6e 67  f it makes chang
21ffd 65 73 20 61 6e 64 20 30 20 69 66 20 6e 6f 20 66  es and 0 if no f
21ffe 6c 61 74 74 65 6e 69 6e 67 0a 2a 2a 20 6f 63 63  lattening.** occ
21fff 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 75 6e  urs..**.** To un
22000 64 65 72 73 74 61 6e 64 20 74 68 65 20 63 6f 6e  derstand the con
22001 63 65 70 74 20 6f 66 20 66 6c 61 74 74 65 6e 69  cept of flatteni
22002 6e 67 2c 20 63 6f 6e 73 69 64 65 72 20 74 68 65  ng, consider the
22003 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 71 75   following.** qu
22004 65 72 79 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53  ery:.**.**     S
22005 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 28 53 45  ELECT a FROM (SE
22006 4c 45 43 54 20 78 2b 79 20 41 53 20 61 20 46 52  LECT x+y AS a FR
22007 4f 4d 20 74 31 20 57 48 45 52 45 20 7a 3c 31 30  OM t1 WHERE z<10
22008 30 29 20 57 48 45 52 45 20 61 3e 35 0a 2a 2a 0a  0) WHERE a>5.**.
22009 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 77  ** The default w
2200a 61 79 20 6f 66 20 69 6d 70 6c 65 6d 65 6e 74 69  ay of implementi
2200b 6e 67 20 74 68 69 73 20 71 75 65 72 79 20 69 73  ng this query is
2200c 20 74 6f 20 65 78 65 63 75 74 65 20 74 68 65 0a   to execute the.
2200d 2a 2a 20 73 75 62 71 75 65 72 79 20 66 69 72 73  ** subquery firs
2200e 74 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20  t and store the 
2200f 72 65 73 75 6c 74 73 20 69 6e 20 61 20 74 65 6d  results in a tem
22010 70 6f 72 61 72 79 20 74 61 62 6c 65 2c 20 74 68  porary table, th
22011 65 6e 0a 2a 2a 20 72 75 6e 20 74 68 65 20 6f 75  en.** run the ou
22012 74 65 72 20 71 75 65 72 79 20 6f 6e 20 74 68 61  ter query on tha
22013 74 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  t temporary tabl
22014 65 2e 20 20 54 68 69 73 20 72 65 71 75 69 72 65  e.  This require
22015 73 20 74 77 6f 0a 2a 2a 20 70 61 73 73 65 73 20  s two.** passes 
22016 6f 76 65 72 20 74 68 65 20 64 61 74 61 2e 20 20  over the data.  
22017 46 75 72 74 68 65 72 6d 6f 72 65 2c 20 62 65 63  Furthermore, bec
22018 61 75 73 65 20 74 68 65 20 74 65 6d 70 6f 72 61  ause the tempora
22019 72 79 20 74 61 62 6c 65 0a 2a 2a 20 68 61 73 20  ry table.** has 
2201a 6e 6f 20 69 6e 64 69 63 65 73 2c 20 74 68 65 20  no indices, the 
2201b 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f 6e 20  WHERE clause on 
2201c 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
2201d 63 61 6e 6e 6f 74 20 62 65 0a 2a 2a 20 6f 70 74  cannot be.** opt
2201e 69 6d 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  imized..**.** Th
2201f 69 73 20 72 6f 75 74 69 6e 65 20 61 74 74 65 6d  is routine attem
22020 70 74 73 20 74 6f 20 72 65 77 72 69 74 65 20 71  pts to rewrite q
22021 75 65 72 69 65 73 20 73 75 63 68 20 61 73 20 74  ueries such as t
22022 68 65 20 61 62 6f 76 65 20 69 6e 74 6f 0a 2a 2a  he above into.**
22023 20 61 20 73 69 6e 67 6c 65 20 66 6c 61 74 20 73   a single flat s
22024 65 6c 65 63 74 2c 20 6c 69 6b 65 20 74 68 69 73  elect, like this
22025 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45  :.**.**     SELE
22026 43 54 20 78 2b 79 20 41 53 20 61 20 46 52 4f 4d  CT x+y AS a FROM
22027 20 74 31 20 57 48 45 52 45 20 7a 3c 31 30 30 20   t1 WHERE z<100 
22028 41 4e 44 20 61 3e 35 0a 2a 2a 0a 2a 2a 20 54 68  AND a>5.**.** Th
22029 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64  e code generated
2202a 20 66 6f 72 20 74 68 69 73 20 73 69 6d 70 69 66   for this simpif
2202b 69 63 61 74 69 6f 6e 20 67 69 76 65 73 20 74 68  ication gives th
2202c 65 20 73 61 6d 65 20 72 65 73 75 6c 74 0a 2a 2a  e same result.**
2202d 20 62 75 74 20 6f 6e 6c 79 20 68 61 73 20 74 6f   but only has to
2202e 20 73 63 61 6e 20 74 68 65 20 64 61 74 61 20 6f   scan the data o
2202f 6e 63 65 2e 20 20 41 6e 64 20 62 65 63 61 75 73  nce.  And becaus
22030 65 20 69 6e 64 69 63 65 73 20 6d 69 67 68 74 20  e indices might 
22031 0a 2a 2a 20 65 78 69 73 74 20 6f 6e 20 74 68 65  .** exist on the
22032 20 74 61 62 6c 65 20 74 31 2c 20 61 20 63 6f 6d   table t1, a com
22033 70 6c 65 74 65 20 73 63 61 6e 20 6f 66 20 74 68  plete scan of th
22034 65 20 64 61 74 61 20 6d 69 67 68 74 20 62 65 0a  e data might be.
22035 2a 2a 20 61 76 6f 69 64 65 64 2e 0a 2a 2a 0a 2a  ** avoided..**.*
22036 2a 20 46 6c 61 74 74 65 6e 69 6e 67 20 69 73 20  * Flattening is 
22037 6f 6e 6c 79 20 61 74 74 65 6d 70 74 65 64 20 69  only attempted i
22038 66 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c  f all of the fol
22039 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65 3a  lowing are true:
2203a 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 20 54 68  .**.**   (1)  Th
2203b 65 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 74  e subquery and t
2203c 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 64  he outer query d
2203d 6f 20 6e 6f 74 20 62 6f 74 68 20 75 73 65 20 61  o not both use a
2203e 67 67 72 65 67 61 74 65 73 2e 0a 2a 2a 0a 2a 2a  ggregates..**.**
2203f 20 20 20 28 32 29 20 20 54 68 65 20 73 75 62 71     (2)  The subq
22040 75 65 72 79 20 69 73 20 6e 6f 74 20 61 6e 20 61  uery is not an a
22041 67 67 72 65 67 61 74 65 20 6f 72 20 74 68 65 20  ggregate or the 
22042 6f 75 74 65 72 20 71 75 65 72 79 20 69 73 20 6e  outer query is n
22043 6f 74 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a  ot a join..**.**
22044 20 20 20 28 33 29 20 20 54 68 65 20 73 75 62 71     (3)  The subq
22045 75 65 72 79 20 69 73 20 6e 6f 74 20 74 68 65 20  uery is not the 
22046 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 6f 66  right operand of
22047 20 61 20 6c 65 66 74 20 6f 75 74 65 72 20 6a 6f   a left outer jo
22048 69 6e 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20  in, or.**       
22049 20 74 68 65 20 73 75 62 71 75 65 72 79 20 69 73   the subquery is
2204a 20 6e 6f 74 20 69 74 73 65 6c 66 20 61 20 6a 6f   not itself a jo
2204b 69 6e 2e 20 20 28 54 69 63 6b 65 74 20 23 33 30  in.  (Ticket #30
2204c 36 29 0a 2a 2a 0a 2a 2a 20 20 20 28 34 29 20 20  6).**.**   (4)  
2204d 54 68 65 20 73 75 62 71 75 65 72 79 20 69 73 20  The subquery is 
2204e 6e 6f 74 20 44 49 53 54 49 4e 43 54 20 6f 72 20  not DISTINCT or 
2204f 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
22050 69 73 20 6e 6f 74 20 61 20 6a 6f 69 6e 2e 0a 2a  is not a join..*
22051 2a 0a 2a 2a 20 20 20 28 35 29 20 20 54 68 65 20  *.**   (5)  The 
22052 73 75 62 71 75 65 72 79 20 69 73 20 6e 6f 74 20  subquery is not 
22053 44 49 53 54 49 4e 43 54 20 6f 72 20 74 68 65 20  DISTINCT or the 
22054 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f 65 73  outer query does
22055 20 6e 6f 74 20 75 73 65 0a 2a 2a 20 20 20 20 20   not use.**     
22056 20 20 20 61 67 67 72 65 67 61 74 65 73 2e 0a 2a     aggregates..*
22057 2a 0a 2a 2a 20 20 20 28 36 29 20 20 54 68 65 20  *.**   (6)  The 
22058 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f  subquery does no
22059 74 20 75 73 65 20 61 67 67 72 65 67 61 74 65 73  t use aggregates
2205a 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75   or the outer qu
2205b 65 72 79 20 69 73 20 6e 6f 74 0a 2a 2a 20 20 20  ery is not.**   
2205c 20 20 20 20 20 44 49 53 54 49 4e 43 54 2e 0a 2a       DISTINCT..*
2205d 2a 0a 2a 2a 20 20 20 28 37 29 20 20 54 68 65 20  *.**   (7)  The 
2205e 73 75 62 71 75 65 72 79 20 68 61 73 20 61 20 46  subquery has a F
2205f 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a  ROM clause..**.*
22060 2a 20 20 20 28 38 29 20 20 54 68 65 20 73 75 62  *   (8)  The sub
22061 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75  query does not u
22062 73 65 20 4c 49 4d 49 54 20 6f 72 20 74 68 65 20  se LIMIT or the 
22063 6f 75 74 65 72 20 71 75 65 72 79 20 69 73 20 6e  outer query is n
22064 6f 74 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a  ot a join..**.**
22065 20 20 20 28 39 29 20 20 54 68 65 20 73 75 62 71     (9)  The subq
22066 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73  uery does not us
22067 65 20 4c 49 4d 49 54 20 6f 72 20 74 68 65 20 6f  e LIMIT or the o
22068 75 74 65 72 20 71 75 65 72 79 20 64 6f 65 73 20  uter query does 
22069 6e 6f 74 20 75 73 65 0a 2a 2a 20 20 20 20 20 20  not use.**      
2206a 20 20 61 67 67 72 65 67 61 74 65 73 2e 0a 2a 2a    aggregates..**
2206b 0a 2a 2a 20 20 28 31 30 29 20 20 54 68 65 20 73  .**  (10)  The s
2206c 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74  ubquery does not
2206d 20 75 73 65 20 61 67 67 72 65 67 61 74 65 73 20   use aggregates 
2206e 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  or the outer que
2206f 72 79 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 20  ry does not.**  
22070 20 20 20 20 20 20 75 73 65 20 4c 49 4d 49 54 2e        use LIMIT.
22071 0a 2a 2a 0a 2a 2a 20 20 28 31 31 29 20 20 54 68  .**.**  (11)  Th
22072 65 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 74  e subquery and t
22073 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 64  he outer query d
22074 6f 20 6e 6f 74 20 62 6f 74 68 20 68 61 76 65 20  o not both have 
22075 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 73  ORDER BY clauses
22076 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 32 29 20 20 54  ..**.**  (12)  T
22077 68 65 20 73 75 62 71 75 65 72 79 20 69 73 20 6e  he subquery is n
22078 6f 74 20 74 68 65 20 72 69 67 68 74 20 74 65 72  ot the right ter
22079 6d 20 6f 66 20 61 20 4c 45 46 54 20 4f 55 54 45  m of a LEFT OUTE
2207a 52 20 4a 4f 49 4e 20 6f 72 20 74 68 65 0a 2a 2a  R JOIN or the.**
2207b 20 20 20 20 20 20 20 20 73 75 62 71 75 65 72 79          subquery
2207c 20 68 61 73 20 6e 6f 20 57 48 45 52 45 20 63 6c   has no WHERE cl
2207d 61 75 73 65 2e 20 20 28 61 64 64 65 64 20 62 79  ause.  (added by
2207e 20 74 69 63 6b 65 74 20 23 33 35 30 29 0a 2a 2a   ticket #350).**
2207f 0a 2a 2a 20 20 28 31 33 29 20 20 54 68 65 20 73  .**  (13)  The s
22080 75 62 71 75 65 72 79 20 61 6e 64 20 6f 75 74 65  ubquery and oute
22081 72 20 71 75 65 72 79 20 64 6f 20 6e 6f 74 20 62  r query do not b
22082 6f 74 68 20 75 73 65 20 4c 49 4d 49 54 0a 2a 2a  oth use LIMIT.**
22083 0a 2a 2a 20 20 28 31 34 29 20 20 54 68 65 20 73  .**  (14)  The s
22084 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74  ubquery does not
22085 20 75 73 65 20 4f 46 46 53 45 54 0a 2a 2a 0a 2a   use OFFSET.**.*
22086 2a 20 20 28 31 35 29 20 20 54 68 65 20 6f 75 74  *  (15)  The out
22087 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f 74 20  er query is not 
22088 70 61 72 74 20 6f 66 20 61 20 63 6f 6d 70 6f 75  part of a compou
22089 6e 64 20 73 65 6c 65 63 74 20 6f 72 20 74 68 65  nd select or the
2208a 0a 2a 2a 20 20 20 20 20 20 20 20 73 75 62 71 75  .**        subqu
2208b 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 68 61 76  ery does not hav
2208c 65 20 62 6f 74 68 20 61 6e 20 4f 52 44 45 52 20  e both an ORDER 
2208d 42 59 20 61 6e 64 20 61 20 4c 49 4d 49 54 20 63  BY and a LIMIT c
2208e 6c 61 75 73 65 2e 0a 2a 2a 20 20 20 20 20 20 20  lause..**       
2208f 20 28 53 65 65 20 74 69 63 6b 65 74 20 23 32 33   (See ticket #23
22090 33 39 29 0a 2a 2a 0a 2a 2a 20 20 28 31 36 29 20  39).**.**  (16) 
22091 20 54 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   The outer query
22092 20 69 73 20 6e 6f 74 20 61 6e 20 61 67 67 72 65   is not an aggre
22093 67 61 74 65 20 6f 72 20 74 68 65 20 73 75 62 71  gate or the subq
22094 75 65 72 79 20 64 6f 65 73 0a 2a 2a 20 20 20 20  uery does.**    
22095 20 20 20 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20      not contain 
22096 4f 52 44 45 52 20 42 59 2e 20 20 28 54 69 63 6b  ORDER BY.  (Tick
22097 65 74 20 23 32 39 34 32 29 20 20 54 68 69 73 20  et #2942)  This 
22098 75 73 65 64 20 74 6f 20 6e 6f 74 20 6d 61 74 74  used to not matt
22099 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 75 6e 74  er.**        unt
2209a 69 6c 20 77 65 20 69 6e 74 72 6f 64 75 63 65 64  il we introduced
2209b 20 74 68 65 20 67 72 6f 75 70 5f 63 6f 6e 63 61   the group_conca
2209c 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 0a  t() function.  .
2209d 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 72 6f  **.** In this ro
2209e 75 74 69 6e 65 2c 20 74 68 65 20 22 70 22 20 70  utine, the "p" p
2209f 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 70 6f  arameter is a po
220a0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6f 75 74  inter to the out
220a1 65 72 20 71 75 65 72 79 2e 0a 2a 2a 20 54 68 65  er query..** The
220a2 20 73 75 62 71 75 65 72 79 20 69 73 20 70 2d 3e   subquery is p->
220a3 70 53 72 63 2d 3e 61 5b 69 46 72 6f 6d 5d 2e 20  pSrc->a[iFrom]. 
220a4 20 69 73 41 67 67 20 69 73 20 74 72 75 65 20 69   isAgg is true i
220a5 66 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  f the outer quer
220a6 79 0a 2a 2a 20 75 73 65 73 20 61 67 67 72 65 67  y.** uses aggreg
220a7 61 74 65 73 20 61 6e 64 20 73 75 62 71 75 65 72  ates and subquer
220a8 79 49 73 41 67 67 20 69 73 20 74 72 75 65 20 69  yIsAgg is true i
220a9 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20 75  f the subquery u
220aa 73 65 73 20 61 67 67 72 65 67 61 74 65 73 2e 0a  ses aggregates..
220ab 2a 2a 0a 2a 2a 20 49 66 20 66 6c 61 74 74 65 6e  **.** If flatten
220ac 69 6e 67 20 69 73 20 6e 6f 74 20 61 74 74 65 6d  ing is not attem
220ad 70 74 65 64 2c 20 74 68 69 73 20 72 6f 75 74 69  pted, this routi
220ae 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 61 6e  ne is a no-op an
220af 64 20 72 65 74 75 72 6e 73 20 30 2e 0a 2a 2a 20  d returns 0..** 
220b0 49 66 20 66 6c 61 74 74 65 6e 69 6e 67 20 69 73  If flattening is
220b1 20 61 74 74 65 6d 70 74 65 64 20 74 68 69 73 20   attempted this 
220b2 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
220b3 31 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20  1..**.** All of 
220b4 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61  the expression a
220b5 6e 61 6c 79 73 69 73 20 6d 75 73 74 20 6f 63 63  nalysis must occ
220b6 75 72 20 6f 6e 20 62 6f 74 68 20 74 68 65 20 6f  ur on both the o
220b7 75 74 65 72 20 71 75 65 72 79 20 61 6e 64 0a 2a  uter query and.*
220b8 2a 20 74 68 65 20 73 75 62 71 75 65 72 79 20 62  * the subquery b
220b9 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75 74 69  efore this routi
220ba 6e 65 20 72 75 6e 73 2e 0a 2a 2f 0a 73 74 61 74  ne runs..*/.stat
220bb 69 63 20 69 6e 74 20 66 6c 61 74 74 65 6e 53 75  ic int flattenSu
220bc 62 71 75 65 72 79 28 0a 20 20 73 71 6c 69 74 65  bquery(.  sqlite
220bd 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 2f  3 *db,         /
220be 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
220bf 63 74 69 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65 63  ction */.  Selec
220c0 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  t *p,           
220c1 2f 2a 20 54 68 65 20 70 61 72 65 6e 74 20 6f 72  /* The parent or
220c2 20 6f 75 74 65 72 20 53 45 4c 45 43 54 20 73 74   outer SELECT st
220c3 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  atement */.  int
220c4 20 69 46 72 6f 6d 2c 20 20 20 20 20 20 20 20 20   iFrom,         
220c5 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 70 2d    /* Index in p-
220c6 3e 70 53 72 63 2d 3e 61 5b 5d 20 6f 66 20 74 68  >pSrc->a[] of th
220c7 65 20 69 6e 6e 65 72 20 73 75 62 71 75 65 72 79  e inner subquery
220c8 20 2a 2f 0a 20 20 69 6e 74 20 69 73 41 67 67 2c   */.  int isAgg,
220c9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
220ca 75 65 20 69 66 20 6f 75 74 65 72 20 53 45 4c 45  ue if outer SELE
220cb 43 54 20 75 73 65 73 20 61 67 67 72 65 67 61 74  CT uses aggregat
220cc 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20  e functions */. 
220cd 20 69 6e 74 20 73 75 62 71 75 65 72 79 49 73 41   int subqueryIsA
220ce 67 67 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  gg    /* True if
220cf 20 74 68 65 20 73 75 62 71 75 65 72 79 20 75 73   the subquery us
220d0 65 73 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  es aggregate fun
220d1 63 74 69 6f 6e 73 20 2a 2f 0a 29 7b 0a 20 20 53  ctions */.){.  S
220d2 65 6c 65 63 74 20 2a 70 53 75 62 3b 20 20 20 20  elect *pSub;    
220d3 20 20 20 2f 2a 20 54 68 65 20 69 6e 6e 65 72 20     /* The inner 
220d4 71 75 65 72 79 20 6f 72 20 22 73 75 62 71 75 65  query or "subque
220d5 72 79 22 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ry" */.  SrcList
220d6 20 2a 70 53 72 63 3b 20 20 20 20 20 20 2f 2a 20   *pSrc;      /* 
220d7 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  The FROM clause 
220d8 6f 66 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  of the outer que
220d9 72 79 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  ry */.  SrcList 
220da 2a 70 53 75 62 53 72 63 3b 20 20 20 2f 2a 20 54  *pSubSrc;   /* T
220db 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  he FROM clause o
220dc 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20 2a  f the subquery *
220dd 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c  /.  ExprList *pL
220de 69 73 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 72  ist;    /* The r
220df 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65  esult set of the
220e0 20 6f 75 74 65 72 20 71 75 65 72 79 20 2a 2f 0a   outer query */.
220e1 20 20 69 6e 74 20 69 50 61 72 65 6e 74 3b 20 20    int iParent;  
220e2 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 63 75        /* VDBE cu
220e3 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74  rsor number of t
220e4 68 65 20 70 53 75 62 20 72 65 73 75 6c 74 20 73  he pSub result s
220e5 65 74 20 74 65 6d 70 20 74 61 62 6c 65 20 2a 2f  et temp table */
220e6 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
220e7 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
220e8 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78 70 72  ounter */.  Expr
220e9 20 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20 20   *pWhere;       
220ea 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
220eb 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  The WHERE clause
220ec 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63   */.  struct Src
220ed 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 75 62 69  List_item *pSubi
220ee 74 65 6d 3b 20 20 20 2f 2a 20 54 68 65 20 73 75  tem;   /* The su
220ef 62 71 75 65 72 79 20 2a 2f 0a 0a 20 20 2f 2a 20  bquery */..  /* 
220f0 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
220f1 66 6c 61 74 74 65 6e 69 6e 67 20 69 73 20 70 65  flattening is pe
220f2 72 6d 69 74 74 65 64 2e 20 20 52 65 74 75 72 6e  rmitted.  Return
220f3 20 30 20 69 66 20 6e 6f 74 2e 0a 20 20 2a 2f 0a   0 if not..  */.
220f4 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74    if( p==0 ) ret
220f5 75 72 6e 20 30 3b 0a 20 20 70 53 72 63 20 3d 20  urn 0;.  pSrc = 
220f6 70 2d 3e 70 53 72 63 3b 0a 20 20 61 73 73 65 72  p->pSrc;.  asser
220f7 74 28 20 70 53 72 63 20 26 26 20 69 46 72 6f 6d  t( pSrc && iFrom
220f8 3e 3d 30 20 26 26 20 69 46 72 6f 6d 3c 70 53 72  >=0 && iFrom<pSr
220f9 63 2d 3e 6e 53 72 63 20 29 3b 0a 20 20 70 53 75  c->nSrc );.  pSu
220fa 62 69 74 65 6d 20 3d 20 26 70 53 72 63 2d 3e 61  bitem = &pSrc->a
220fb 5b 69 46 72 6f 6d 5d 3b 0a 20 20 70 53 75 62 20  [iFrom];.  pSub 
220fc 3d 20 70 53 75 62 69 74 65 6d 2d 3e 70 53 65 6c  = pSubitem->pSel
220fd 65 63 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ect;.  assert( p
220fe 53 75 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  Sub!=0 );.  if( 
220ff 69 73 41 67 67 20 26 26 20 73 75 62 71 75 65 72  isAgg && subquer
22100 79 49 73 41 67 67 20 29 20 72 65 74 75 72 6e 20  yIsAgg ) return 
22101 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
22102 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f     /* Restrictio
22103 6e 20 28 31 29 20 20 2a 2f 0a 20 20 69 66 28 20  n (1)  */.  if( 
22104 73 75 62 71 75 65 72 79 49 73 41 67 67 20 26 26  subqueryIsAgg &&
22105 20 70 53 72 63 2d 3e 6e 53 72 63 3e 31 20 29 20   pSrc->nSrc>1 ) 
22106 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20  return 0;       
22107 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f     /* Restrictio
22108 6e 20 28 32 29 20 20 2a 2f 0a 20 20 70 53 75 62  n (2)  */.  pSub
22109 53 72 63 20 3d 20 70 53 75 62 2d 3e 70 53 72 63  Src = pSub->pSrc
2210a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 75 62  ;.  assert( pSub
2210b 53 72 63 20 29 3b 0a 20 20 2f 2a 20 50 72 69 6f  Src );.  /* Prio
2210c 72 20 74 6f 20 76 65 72 73 69 6f 6e 20 33 2e 31  r to version 3.1
2210d 2e 32 2c 20 77 68 65 6e 20 4c 49 4d 49 54 20 61  .2, when LIMIT a
2210e 6e 64 20 4f 46 46 53 45 54 20 68 61 64 20 74 6f  nd OFFSET had to
2210f 20 62 65 20 73 69 6d 70 6c 65 20 63 6f 6e 73 74   be simple const
22110 61 6e 74 73 2c 0a 20 20 2a 2a 20 6e 6f 74 20 61  ants,.  ** not a
22111 72 62 69 74 72 61 72 79 20 65 78 70 72 65 73 73  rbitrary express
22112 73 69 6f 6e 73 2c 20 77 65 20 61 6c 6c 6f 77 65  sions, we allowe
22113 64 20 73 6f 6d 65 20 63 6f 6d 62 69 6e 69 6e 67  d some combining
22114 20 6f 66 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46   of LIMIT and OF
22115 46 53 45 54 0a 20 20 2a 2a 20 62 65 63 61 75 73  FSET.  ** becaus
22116 65 20 74 68 65 79 20 63 6f 75 6c 64 20 62 65 20  e they could be 
22117 63 6f 6d 70 75 74 65 64 20 61 74 20 63 6f 6d 70  computed at comp
22118 69 6c 65 2d 74 69 6d 65 2e 20 20 42 75 74 20 77  ile-time.  But w
22119 68 65 6e 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46  hen LIMIT and OF
2211a 46 53 45 54 0a 20 20 2a 2a 20 62 65 63 61 6d 65  FSET.  ** became
2211b 20 61 72 62 69 74 72 61 72 79 20 65 78 70 72 65   arbitrary expre
2211c 73 73 69 6f 6e 73 2c 20 77 65 20 77 65 72 65 20  ssions, we were 
2211d 66 6f 72 63 65 64 20 74 6f 20 61 64 64 20 72 65  forced to add re
2211e 73 74 72 69 63 74 69 6f 6e 73 20 28 31 33 29 0a  strictions (13).
2211f 20 20 2a 2a 20 61 6e 64 20 28 31 34 29 2e 20 2a    ** and (14). *
22120 2f 0a 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4c  /.  if( pSub->pL
22121 69 6d 69 74 20 26 26 20 70 2d 3e 70 4c 69 6d 69  imit && p->pLimi
22122 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20  t ) return 0;   
22123 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
22124 73 74 72 69 63 74 69 6f 6e 20 28 31 33 29 20 2a  striction (13) *
22125 2f 0a 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4f  /.  if( pSub->pO
22126 66 66 73 65 74 20 29 20 72 65 74 75 72 6e 20 30  ffset ) return 0
22127 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
22128 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
22129 73 74 72 69 63 74 69 6f 6e 20 28 31 34 29 20 2a  striction (14) *
2212a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 52 69 67 68  /.  if( p->pRigh
2212b 74 6d 6f 73 74 20 26 26 20 70 53 75 62 2d 3e 70  tmost && pSub->p
2212c 4c 69 6d 69 74 20 26 26 20 70 53 75 62 2d 3e 70  Limit && pSub->p
2212d 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 72  OrderBy ){.    r
2212e 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20  eturn 0;        
2212f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22131 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69      /* Restricti
22132 6f 6e 20 28 31 35 29 20 2a 2f 0a 20 20 7d 0a 20  on (15) */.  }. 
22133 20 69 66 28 20 70 53 75 62 53 72 63 2d 3e 6e 53   if( pSubSrc->nS
22134 72 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  rc==0 ) return 0
22135 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
22136 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72          /* Restr
22137 69 63 74 69 6f 6e 20 28 37 29 20 20 2a 2f 0a 20  iction (7)  */. 
22138 20 69 66 28 20 28 70 53 75 62 2d 3e 69 73 44 69   if( (pSub->isDi
22139 73 74 69 6e 63 74 20 7c 7c 20 70 53 75 62 2d 3e  stinct || pSub->
2213a 70 4c 69 6d 69 74 29 20 0a 20 20 20 20 20 20 20  pLimit) .       
2213b 20 20 26 26 20 28 70 53 72 63 2d 3e 6e 53 72 63    && (pSrc->nSrc
2213c 3e 31 20 7c 7c 20 69 73 41 67 67 29 20 29 7b 20  >1 || isAgg) ){ 
2213d 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74           /* Rest
2213e 72 69 63 74 69 6f 6e 73 20 28 34 29 28 35 29 28  rictions (4)(5)(
2213f 38 29 28 39 29 20 2a 2f 0a 20 20 20 20 20 72 65  8)(9) */.     re
22140 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 0a 20  turn 0;       . 
22141 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 73 44 69   }.  if( p->isDi
22142 73 74 69 6e 63 74 20 26 26 20 73 75 62 71 75 65  stinct && subque
22143 72 79 49 73 41 67 67 20 29 20 72 65 74 75 72 6e  ryIsAgg ) return
22144 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52   0;         /* R
22145 65 73 74 72 69 63 74 69 6f 6e 20 28 36 29 20 20  estriction (6)  
22146 2a 2f 0a 20 20 69 66 28 20 28 70 2d 3e 64 69 73  */.  if( (p->dis
22147 61 6c 6c 6f 77 4f 72 64 65 72 42 79 20 7c 7c 20  allowOrderBy || 
22148 70 2d 3e 70 4f 72 64 65 72 42 79 29 20 26 26 20  p->pOrderBy) && 
22149 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 79 20 29  pSub->pOrderBy )
2214a 7b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  {.     return 0;
2214b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2214c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2214d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
2214e 73 74 72 69 63 74 69 6f 6e 20 28 31 31 29 20 2a  striction (11) *
2214f 2f 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 41 67  /.  }.  if( isAg
22150 67 20 26 26 20 70 53 75 62 2d 3e 70 4f 72 64 65  g && pSub->pOrde
22151 72 42 79 20 29 20 72 65 74 75 72 6e 20 30 3b 20  rBy ) return 0; 
22152 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
22153 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31  * Restriction (1
22154 36 29 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 73 74  6) */..  /* Rest
22155 72 69 63 74 69 6f 6e 20 33 3a 20 20 49 66 20 74  riction 3:  If t
22156 68 65 20 73 75 62 71 75 65 72 79 20 69 73 20 61  he subquery is a
22157 20 6a 6f 69 6e 2c 20 6d 61 6b 65 20 73 75 72 65   join, make sure
22158 20 74 68 65 20 73 75 62 71 75 65 72 79 20 69 73   the subquery is
22159 20 0a 20 20 2a 2a 20 6e 6f 74 20 75 73 65 64 20   .  ** not used 
2215a 61 73 20 74 68 65 20 72 69 67 68 74 20 6f 70 65  as the right ope
2215b 72 61 6e 64 20 6f 66 20 61 6e 20 6f 75 74 65 72  rand of an outer
2215c 20 6a 6f 69 6e 2e 20 20 45 78 61 6d 70 6c 65 73   join.  Examples
2215d 20 6f 66 20 77 68 79 20 74 68 69 73 0a 20 20 2a   of why this.  *
2215e 2a 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64  * is not allowed
2215f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  :.  **.  **     
22160 20 20 20 20 74 31 20 4c 45 46 54 20 4f 55 54 45      t1 LEFT OUTE
22161 52 20 4a 4f 49 4e 20 28 74 32 20 4a 4f 49 4e 20  R JOIN (t2 JOIN 
22162 74 33 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66  t3).  **.  ** If
22163 20 77 65 20 66 6c 61 74 74 65 6e 20 74 68 65 20   we flatten the 
22164 61 62 6f 76 65 2c 20 77 65 20 77 6f 75 6c 64 20  above, we would 
22165 67 65 74 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  get.  **.  **   
22166 20 20 20 20 20 20 28 74 31 20 4c 45 46 54 20 4f        (t1 LEFT O
22167 55 54 45 52 20 4a 4f 49 4e 20 74 32 29 20 4a 4f  UTER JOIN t2) JO
22168 49 4e 20 74 33 0a 20 20 2a 2a 0a 20 20 2a 2a 20  IN t3.  **.  ** 
22169 77 68 69 63 68 20 69 73 20 6e 6f 74 20 61 74 20  which is not at 
2216a 61 6c 6c 20 74 68 65 20 73 61 6d 65 20 74 68 69  all the same thi
2216b 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ng..  */.  if( p
2216c 53 75 62 53 72 63 2d 3e 6e 53 72 63 3e 31 20 26  SubSrc->nSrc>1 &
2216d 26 20 28 70 53 75 62 69 74 65 6d 2d 3e 6a 6f 69  & (pSubitem->joi
2216e 6e 74 79 70 65 20 26 20 4a 54 5f 4f 55 54 45 52  ntype & JT_OUTER
2216f 29 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  )!=0 ){.    retu
22170 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rn 0;.  }..  /* 
22171 52 65 73 74 72 69 63 74 69 6f 6e 20 31 32 3a 20  Restriction 12: 
22172 20 49 66 20 74 68 65 20 73 75 62 71 75 65 72 79   If the subquery
22173 20 69 73 20 74 68 65 20 72 69 67 68 74 20 6f 70   is the right op
22174 65 72 61 6e 64 20 6f 66 20 61 20 6c 65 66 74 20  erand of a left 
22175 6f 75 74 65 72 0a 20 20 2a 2a 20 6a 6f 69 6e 2c  outer.  ** join,
22176 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 73   make sure the s
22177 75 62 71 75 65 72 79 20 68 61 73 20 6e 6f 20 57  ubquery has no W
22178 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 20 20 2a  HERE clause..  *
22179 2a 20 41 6e 20 65 78 61 6d 70 6c 65 73 20 6f 66  * An examples of
2217a 20 77 68 79 20 74 68 69 73 20 69 73 20 6e 6f 74   why this is not
2217b 20 61 6c 6c 6f 77 65 64 3a 0a 20 20 2a 2a 0a 20   allowed:.  **. 
2217c 20 2a 2a 20 20 20 20 20 20 20 20 20 74 31 20 4c   **         t1 L
2217d 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 28  EFT OUTER JOIN (
2217e 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 32  SELECT * FROM t2
2217f 20 57 48 45 52 45 20 74 32 2e 78 3e 30 29 0a 20   WHERE t2.x>0). 
22180 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 77 65 20 66   **.  ** If we f
22181 6c 61 74 74 65 6e 20 74 68 65 20 61 62 6f 76 65  latten the above
22182 2c 20 77 65 20 77 6f 75 6c 64 20 67 65 74 0a 20  , we would get. 
22183 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20   **.  **        
22184 20 28 74 31 20 4c 45 46 54 20 4f 55 54 45 52 20   (t1 LEFT OUTER 
22185 4a 4f 49 4e 20 74 32 29 20 57 48 45 52 45 20 74  JOIN t2) WHERE t
22186 32 2e 78 3e 30 0a 20 20 2a 2a 0a 20 20 2a 2a 20  2.x>0.  **.  ** 
22187 42 75 74 20 74 68 65 20 74 32 2e 78 3e 30 20 74  But the t2.x>0 t
22188 65 73 74 20 77 69 6c 6c 20 61 6c 77 61 79 73 20  est will always 
22189 66 61 69 6c 20 6f 6e 20 61 20 4e 55 4c 4c 20 72  fail on a NULL r
2218a 6f 77 20 6f 66 20 74 32 2c 20 77 68 69 63 68 0a  ow of t2, which.
2218b 20 20 2a 2a 20 65 66 66 65 63 74 69 76 65 6c 79    ** effectively
2218c 20 63 6f 6e 76 65 72 74 73 20 74 68 65 20 4f 55   converts the OU
2218d 54 45 52 20 4a 4f 49 4e 20 69 6e 74 6f 20 61 6e  TER JOIN into an
2218e 20 49 4e 4e 45 52 20 4a 4f 49 4e 2e 0a 20 20 2a   INNER JOIN..  *
2218f 2f 0a 20 20 69 66 28 20 28 70 53 75 62 69 74 65  /.  if( (pSubite
22190 6d 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54  m->jointype & JT
22191 5f 4f 55 54 45 52 29 21 3d 30 20 26 26 20 70 53  _OUTER)!=0 && pS
22192 75 62 2d 3e 70 57 68 65 72 65 21 3d 30 20 29 7b  ub->pWhere!=0 ){
22193 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
22194 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 72   }..  /* If we r
22195 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c  each this point,
22196 20 69 74 20 6d 65 61 6e 73 20 66 6c 61 74 74 65   it means flatte
22197 6e 69 6e 67 20 69 73 20 70 65 72 6d 69 74 74 65  ning is permitte
22198 64 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 69  d for the.  ** i
22199 46 72 6f 6d 2d 74 68 20 65 6e 74 72 79 20 6f 66  From-th entry of
2219a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2219b 20 69 6e 20 74 68 65 20 6f 75 74 65 72 20 71 75   in the outer qu
2219c 65 72 79 2e 0a 20 20 2a 2f 0a 0a 20 20 2f 2a 20  ery..  */..  /* 
2219d 4d 6f 76 65 20 61 6c 6c 20 6f 66 20 74 68 65 20  Move all of the 
2219e 46 52 4f 4d 20 65 6c 65 6d 65 6e 74 73 20 6f 66  FROM elements of
2219f 20 74 68 65 20 73 75 62 71 75 65 72 79 20 69 6e   the subquery in
221a0 74 6f 20 74 68 65 0a 20 20 2a 2a 20 74 68 65 20  to the.  ** the 
221a1 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74  FROM clause of t
221a2 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 2e 20  he outer query. 
221a3 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 74 68   Before doing th
221a4 69 73 2c 20 72 65 6d 65 6d 62 65 72 0a 20 20 2a  is, remember.  *
221a5 2a 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d  * the cursor num
221a6 62 65 72 20 66 6f 72 20 74 68 65 20 6f 72 69 67  ber for the orig
221a7 69 6e 61 6c 20 6f 75 74 65 72 20 71 75 65 72 79  inal outer query
221a8 20 46 52 4f 4d 20 65 6c 65 6d 65 6e 74 20 69 6e   FROM element in
221a9 0a 20 20 2a 2a 20 69 50 61 72 65 6e 74 2e 20 20  .  ** iParent.  
221aa 54 68 65 20 69 50 61 72 65 6e 74 20 63 75 72 73  The iParent curs
221ab 6f 72 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65  or will never be
221ac 20 75 73 65 64 2e 20 20 53 75 62 73 65 71 75 65   used.  Subseque
221ad 6e 74 20 63 6f 64 65 0a 20 20 2a 2a 20 77 69 6c  nt code.  ** wil
221ae 6c 20 73 63 61 6e 20 65 78 70 72 65 73 73 69 6f  l scan expressio
221af 6e 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 69  ns looking for i
221b0 50 61 72 65 6e 74 20 72 65 66 65 72 65 6e 63 65  Parent reference
221b1 73 20 61 6e 64 20 72 65 70 6c 61 63 65 0a 20 20  s and replace.  
221b2 2a 2a 20 74 68 6f 73 65 20 72 65 66 65 72 65 6e  ** those referen
221b3 63 65 73 20 77 69 74 68 20 65 78 70 72 65 73 73  ces with express
221b4 69 6f 6e 73 20 74 68 61 74 20 72 65 73 6f 6c 76  ions that resolv
221b5 65 20 74 6f 20 74 68 65 20 73 75 62 71 75 65 72  e to the subquer
221b6 79 20 46 52 4f 4d 0a 20 20 2a 2a 20 65 6c 65 6d  y FROM.  ** elem
221b7 65 6e 74 73 20 77 65 20 61 72 65 20 6e 6f 77 20  ents we are now 
221b8 63 6f 70 79 69 6e 67 20 69 6e 2e 0a 20 20 2a 2f  copying in..  */
221b9 0a 20 20 69 50 61 72 65 6e 74 20 3d 20 70 53 75  .  iParent = pSu
221ba 62 69 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a  bitem->iCursor;.
221bb 20 20 7b 0a 20 20 20 20 69 6e 74 20 6e 53 75 62    {.    int nSub
221bc 53 72 63 20 3d 20 70 53 75 62 53 72 63 2d 3e 6e  Src = pSubSrc->n
221bd 53 72 63 3b 0a 20 20 20 20 69 6e 74 20 6a 6f 69  Src;.    int joi
221be 6e 74 79 70 65 20 3d 20 70 53 75 62 69 74 65 6d  ntype = pSubitem
221bf 2d 3e 6a 6f 69 6e 74 79 70 65 3b 0a 0a 20 20 20  ->jointype;..   
221c0 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61   sqlite3DeleteTa
221c1 62 6c 65 28 70 53 75 62 69 74 65 6d 2d 3e 70 54  ble(pSubitem->pT
221c2 61 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ab);.    sqlite3
221c3 5f 66 72 65 65 28 70 53 75 62 69 74 65 6d 2d 3e  _free(pSubitem->
221c4 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20  zDatabase);.    
221c5 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 53 75  sqlite3_free(pSu
221c6 62 69 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  bitem->zName);. 
221c7 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
221c8 70 53 75 62 69 74 65 6d 2d 3e 7a 41 6c 69 61 73  pSubitem->zAlias
221c9 29 3b 0a 20 20 20 20 70 53 75 62 69 74 65 6d 2d  );.    pSubitem-
221ca 3e 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 70  >pTab = 0;.    p
221cb 53 75 62 69 74 65 6d 2d 3e 7a 44 61 74 61 62 61  Subitem->zDataba
221cc 73 65 20 3d 20 30 3b 0a 20 20 20 20 70 53 75 62  se = 0;.    pSub
221cd 69 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 30 3b  item->zName = 0;
221ce 0a 20 20 20 20 70 53 75 62 69 74 65 6d 2d 3e 7a  .    pSubitem->z
221cf 41 6c 69 61 73 20 3d 20 30 3b 0a 20 20 20 20 69  Alias = 0;.    i
221d0 66 28 20 6e 53 75 62 53 72 63 3e 31 20 29 7b 0a  f( nSubSrc>1 ){.
221d1 20 20 20 20 20 20 69 6e 74 20 65 78 74 72 61 20        int extra 
221d2 3d 20 6e 53 75 62 53 72 63 20 2d 20 31 3b 0a 20  = nSubSrc - 1;. 
221d3 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c       for(i=1; i<
221d4 6e 53 75 62 53 72 63 3b 20 69 2b 2b 29 7b 0a 20  nSubSrc; i++){. 
221d5 20 20 20 20 20 20 20 70 53 72 63 20 3d 20 73 71         pSrc = sq
221d6 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65  lite3SrcListAppe
221d7 6e 64 28 64 62 2c 20 70 53 72 63 2c 20 30 2c 20  nd(db, pSrc, 0, 
221d8 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  0);.        if( 
221d9 70 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20  pSrc==0 ){.     
221da 20 20 20 20 20 70 2d 3e 70 53 72 63 20 3d 20 30       p->pSrc = 0
221db 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  ;.          retu
221dc 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a  rn 1;.        }.
221dd 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d        }.      p-
221de 3e 70 53 72 63 20 3d 20 70 53 72 63 3b 0a 20 20  >pSrc = pSrc;.  
221df 20 20 20 20 66 6f 72 28 69 3d 70 53 72 63 2d 3e      for(i=pSrc->
221e0 6e 53 72 63 2d 31 3b 20 69 2d 65 78 74 72 61 3e  nSrc-1; i-extra>
221e1 3d 69 46 72 6f 6d 3b 20 69 2d 2d 29 7b 0a 20 20  =iFrom; i--){.  
221e2 20 20 20 20 20 20 70 53 72 63 2d 3e 61 5b 69 5d        pSrc->a[i]
221e3 20 3d 20 70 53 72 63 2d 3e 61 5b 69 2d 65 78 74   = pSrc->a[i-ext
221e4 72 61 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ra];.      }.   
221e5 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20   }.    for(i=0; 
221e6 69 3c 6e 53 75 62 53 72 63 3b 20 69 2b 2b 29 7b  i<nSubSrc; i++){
221e7 0a 20 20 20 20 20 20 70 53 72 63 2d 3e 61 5b 69  .      pSrc->a[i
221e8 2b 69 46 72 6f 6d 5d 20 3d 20 70 53 75 62 53 72  +iFrom] = pSubSr
221e9 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 20 6d  c->a[i];.      m
221ea 65 6d 73 65 74 28 26 70 53 75 62 53 72 63 2d 3e  emset(&pSubSrc->
221eb 61 5b 69 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28  a[i], 0, sizeof(
221ec 70 53 75 62 53 72 63 2d 3e 61 5b 69 5d 29 29 3b  pSubSrc->a[i]));
221ed 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 72 63 2d  .    }.    pSrc-
221ee 3e 61 5b 69 46 72 6f 6d 5d 2e 6a 6f 69 6e 74 79  >a[iFrom].jointy
221ef 70 65 20 3d 20 6a 6f 69 6e 74 79 70 65 3b 0a 20  pe = jointype;. 
221f0 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 62 65 67   }..  /* Now beg
221f1 69 6e 20 73 75 62 73 74 69 74 75 74 69 6e 67 20  in substituting 
221f2 73 75 62 71 75 65 72 79 20 72 65 73 75 6c 74 20  subquery result 
221f3 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20  set expressions 
221f4 66 6f 72 20 0a 20 20 2a 2a 20 72 65 66 65 72 65  for .  ** refere
221f5 6e 63 65 73 20 74 6f 20 74 68 65 20 69 50 61 72  nces to the iPar
221f6 65 6e 74 20 69 6e 20 74 68 65 20 6f 75 74 65 72  ent in the outer
221f7 20 71 75 65 72 79 2e 0a 20 20 2a 2a 20 0a 20 20   query..  ** .  
221f8 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 20 20 2a 2a  ** Example:.  **
221f9 0a 20 20 2a 2a 20 20 20 53 45 4c 45 43 54 20 61  .  **   SELECT a
221fa 2b 35 2c 20 62 2a 31 30 20 46 52 4f 4d 20 28 53  +5, b*10 FROM (S
221fb 45 4c 45 43 54 20 78 2a 33 20 41 53 20 61 2c 20  ELECT x*3 AS a, 
221fc 79 2b 31 30 20 41 53 20 62 20 46 52 4f 4d 20 74  y+10 AS b FROM t
221fd 31 29 20 57 48 45 52 45 20 61 3e 62 3b 0a 20 20  1) WHERE a>b;.  
221fe 2a 2a 20 20 20 5c 20 20 20 20 20 20 20 20 20 20  **   \          
221ff 20 20 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f             \____
22200 5f 5f 5f 5f 5f 5f 5f 5f 5f 20 73 75 62 71 75 65  _________ subque
22201 72 79 20 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20  ry __________/  
22202 20 20 20 20 20 20 20 20 2f 0a 20 20 2a 2a 20 20          /.  **  
22203 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f    \_____________
22204 5f 5f 5f 5f 5f 5f 5f 5f 20 6f 75 74 65 72 20 71  ________ outer q
22205 75 65 72 79 20 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  uery ___________
22206 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
22207 5f 5f 5f 2f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57  ___/.  **.  ** W
22208 65 20 6c 6f 6f 6b 20 61 74 20 65 76 65 72 79 20  e look at every 
22209 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68  expression in th
2220a 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 61 6e  e outer query an
2220b 64 20 65 76 65 72 79 20 70 6c 61 63 65 20 77 65  d every place we
2220c 20 73 65 65 0a 20 20 2a 2a 20 22 61 22 20 77 65   see.  ** "a" we
2220d 20 73 75 62 73 74 69 74 75 74 65 20 22 78 2a 33   substitute "x*3
2220e 22 20 61 6e 64 20 65 76 65 72 79 20 70 6c 61 63  " and every plac
2220f 65 20 77 65 20 73 65 65 20 22 62 22 20 77 65 20  e we see "b" we 
22210 73 75 62 73 74 69 74 75 74 65 20 22 79 2b 31 30  substitute "y+10
22211 22 2e 0a 20 20 2a 2f 0a 20 20 70 4c 69 73 74 20  "..  */.  pList 
22212 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 66  = p->pEList;.  f
22213 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d  or(i=0; i<pList-
22214 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20  >nExpr; i++){.  
22215 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 0a 20    Expr *pExpr;. 
22216 20 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 61 5b     if( pList->a[
22217 69 5d 2e 7a 4e 61 6d 65 3d 3d 30 20 26 26 20 28  i].zName==0 && (
22218 70 45 78 70 72 20 3d 20 70 4c 69 73 74 2d 3e 61  pExpr = pList->a
22219 5b 69 5d 2e 70 45 78 70 72 29 2d 3e 73 70 61 6e  [i].pExpr)->span
2221a 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  .z!=0 ){.      p
2221b 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  List->a[i].zName
2221c 20 3d 20 0a 20 20 20 20 20 20 20 20 20 20 20 20   = .            
2221d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75   sqlite3DbStrNDu
2221e 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70 45 78  p(db, (char*)pEx
2221f 70 72 2d 3e 73 70 61 6e 2e 7a 2c 20 70 45 78 70  pr->span.z, pExp
22220 72 2d 3e 73 70 61 6e 2e 6e 29 3b 0a 20 20 20 20  r->span.n);.    
22221 7d 0a 20 20 7d 0a 20 20 73 75 62 73 74 45 78 70  }.  }.  substExp
22222 72 4c 69 73 74 28 64 62 2c 20 70 2d 3e 70 45 4c  rList(db, p->pEL
22223 69 73 74 2c 20 69 50 61 72 65 6e 74 2c 20 70 53  ist, iParent, pS
22224 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 69  ub->pEList);.  i
22225 66 28 20 69 73 41 67 67 20 29 7b 0a 20 20 20 20  f( isAgg ){.    
22226 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 64 62  substExprList(db
22227 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 69  , p->pGroupBy, i
22228 50 61 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45  Parent, pSub->pE
22229 4c 69 73 74 29 3b 0a 20 20 20 20 73 75 62 73 74  List);.    subst
2222a 45 78 70 72 28 64 62 2c 20 70 2d 3e 70 48 61 76  Expr(db, p->pHav
2222b 69 6e 67 2c 20 69 50 61 72 65 6e 74 2c 20 70 53  ing, iParent, pS
2222c 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 7d  ub->pEList);.  }
2222d 0a 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4f 72  .  if( pSub->pOr
2222e 64 65 72 42 79 20 29 7b 0a 20 20 20 20 61 73 73  derBy ){.    ass
2222f 65 72 74 28 20 70 2d 3e 70 4f 72 64 65 72 42 79  ert( p->pOrderBy
22230 3d 3d 30 20 29 3b 0a 20 20 20 20 70 2d 3e 70 4f  ==0 );.    p->pO
22231 72 64 65 72 42 79 20 3d 20 70 53 75 62 2d 3e 70  rderBy = pSub->p
22232 4f 72 64 65 72 42 79 3b 0a 20 20 20 20 70 53 75  OrderBy;.    pSu
22233 62 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 30 3b  b->pOrderBy = 0;
22234 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e  .  }else if( p->
22235 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
22236 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 64 62  substExprList(db
22237 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 69  , p->pOrderBy, i
22238 50 61 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45  Parent, pSub->pE
22239 4c 69 73 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28  List);.  }.  if(
2223a 20 70 53 75 62 2d 3e 70 57 68 65 72 65 20 29 7b   pSub->pWhere ){
2223b 0a 20 20 20 20 70 57 68 65 72 65 20 3d 20 73 71  .    pWhere = sq
2223c 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
2223d 20 70 53 75 62 2d 3e 70 57 68 65 72 65 29 3b 0a   pSub->pWhere);.
2223e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 57 68    }else{.    pWh
2223f 65 72 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69  ere = 0;.  }.  i
22240 66 28 20 73 75 62 71 75 65 72 79 49 73 41 67 67  f( subqueryIsAgg
22241 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
22242 70 2d 3e 70 48 61 76 69 6e 67 3d 3d 30 20 29 3b  p->pHaving==0 );
22243 0a 20 20 20 20 70 2d 3e 70 48 61 76 69 6e 67 20  .    p->pHaving 
22244 3d 20 70 2d 3e 70 57 68 65 72 65 3b 0a 20 20 20  = p->pWhere;.   
22245 20 70 2d 3e 70 57 68 65 72 65 20 3d 20 70 57 68   p->pWhere = pWh
22246 65 72 65 3b 0a 20 20 20 20 73 75 62 73 74 45 78  ere;.    substEx
22247 70 72 28 64 62 2c 20 70 2d 3e 70 48 61 76 69 6e  pr(db, p->pHavin
22248 67 2c 20 69 50 61 72 65 6e 74 2c 20 70 53 75 62  g, iParent, pSub
22249 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 70  ->pEList);.    p
2224a 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 71 6c 69  ->pHaving = sqli
2224b 74 65 33 45 78 70 72 41 6e 64 28 64 62 2c 20 70  te3ExprAnd(db, p
2224c 2d 3e 70 48 61 76 69 6e 67 2c 20 0a 20 20 20 20  ->pHaving, .    
2224d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2224e 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2224f 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
22250 53 75 62 2d 3e 70 48 61 76 69 6e 67 29 29 3b 0a  Sub->pHaving));.
22251 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
22252 47 72 6f 75 70 42 79 3d 3d 30 20 29 3b 0a 20 20  GroupBy==0 );.  
22253 20 20 70 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20    p->pGroupBy = 
22254 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
22255 75 70 28 64 62 2c 20 70 53 75 62 2d 3e 70 47 72  up(db, pSub->pGr
22256 6f 75 70 42 79 29 3b 0a 20 20 7d 65 6c 73 65 7b  oupBy);.  }else{
22257 0a 20 20 20 20 73 75 62 73 74 45 78 70 72 28 64  .    substExpr(d
22258 62 2c 20 70 2d 3e 70 57 68 65 72 65 2c 20 69 50  b, p->pWhere, iP
22259 61 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c  arent, pSub->pEL
2225a 69 73 74 29 3b 0a 20 20 20 20 70 2d 3e 70 57 68  ist);.    p->pWh
2225b 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ere = sqlite3Exp
2225c 72 41 6e 64 28 64 62 2c 20 70 2d 3e 70 57 68 65  rAnd(db, p->pWhe
2225d 72 65 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 7d  re, pWhere);.  }
2225e 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6c 61 74 74  ..  /* The flatt
2225f 65 6e 65 64 20 71 75 65 72 79 20 69 73 20 64 69  ened query is di
22260 73 74 69 6e 63 74 20 69 66 20 65 69 74 68 65 72  stinct if either
22261 20 74 68 65 20 69 6e 6e 65 72 20 6f 72 20 74 68   the inner or th
22262 65 0a 20 20 2a 2a 20 6f 75 74 65 72 20 71 75 65  e.  ** outer que
22263 72 79 20 69 73 20 64 69 73 74 69 6e 63 74 2e 20  ry is distinct. 
22264 0a 20 20 2a 2f 0a 20 20 70 2d 3e 69 73 44 69 73  .  */.  p->isDis
22265 74 69 6e 63 74 20 3d 20 70 2d 3e 69 73 44 69 73  tinct = p->isDis
22266 74 69 6e 63 74 20 7c 7c 20 70 53 75 62 2d 3e 69  tinct || pSub->i
22267 73 44 69 73 74 69 6e 63 74 3b 0a 0a 20 20 2f 2a  sDistinct;..  /*
22268 0a 20 20 2a 2a 20 53 45 4c 45 43 54 20 2e 2e 2e  .  ** SELECT ...
22269 20 46 52 4f 4d 20 28 53 45 4c 45 43 54 20 2e 2e   FROM (SELECT ..
2226a 2e 20 4c 49 4d 49 54 20 61 20 4f 46 46 53 45 54  . LIMIT a OFFSET
2226b 20 62 29 20 4c 49 4d 49 54 20 78 20 4f 46 46 53   b) LIMIT x OFFS
2226c 45 54 20 79 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ET y;.  **.  ** 
2226d 4f 6e 65 20 69 73 20 74 65 6d 70 74 65 64 20 74  One is tempted t
2226e 6f 20 74 72 79 20 74 6f 20 61 64 64 20 61 20 61  o try to add a a
2226f 6e 64 20 62 20 74 6f 20 63 6f 6d 62 69 6e 65 20  nd b to combine 
22270 74 68 65 20 6c 69 6d 69 74 73 2e 20 20 42 75 74  the limits.  But
22271 20 74 68 69 73 0a 20 20 2a 2a 20 64 6f 65 73 20   this.  ** does 
22272 6e 6f 74 20 77 6f 72 6b 20 69 66 20 65 69 74 68  not work if eith
22273 65 72 20 6c 69 6d 69 74 20 69 73 20 6e 65 67 61  er limit is nega
22274 74 69 76 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  tive..  */.  if(
22275 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20 29 7b   pSub->pLimit ){
22276 0a 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d  .    p->pLimit =
22277 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 3b 0a 20   pSub->pLimit;. 
22278 20 20 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20     pSub->pLimit 
22279 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46  = 0;.  }..  /* F
2227a 69 6e 69 61 6c 6c 79 2c 20 64 65 6c 65 74 65 20  inially, delete 
2227b 77 68 61 74 20 69 73 20 6c 65 66 74 20 6f 66 20  what is left of 
2227c 74 68 65 20 73 75 62 71 75 65 72 79 20 61 6e 64  the subquery and
2227d 20 72 65 74 75 72 6e 0a 20 20 2a 2a 20 73 75 63   return.  ** suc
2227e 63 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  cess..  */.  sql
2227f 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
22280 28 70 53 75 62 29 3b 0a 20 20 72 65 74 75 72 6e  (pSub);.  return
22281 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20   1;.}.#endif /* 
22282 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
22283 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79   */../*.** Analy
22284 7a 65 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  ze the SELECT st
22285 61 74 65 6d 65 6e 74 20 70 61 73 73 65 64 20 61  atement passed a
22286 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f  s an argument to
22287 20 73 65 65 20 69 66 20 69 74 0a 2a 2a 20 69 73   see if it.** is
22288 20 61 20 6d 69 6e 28 29 20 6f 72 20 6d 61 78 28   a min() or max(
22289 29 20 71 75 65 72 79 2e 20 52 65 74 75 72 6e 20  ) query. Return 
2228a 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49  WHERE_ORDERBY_MI
2228b 4e 20 6f 72 20 57 48 45 52 45 5f 4f 52 44 45 52  N or WHERE_ORDER
2228c 42 59 5f 4d 41 58 20 69 66 20 0a 2a 2a 20 69 74  BY_MAX if .** it
2228d 20 69 73 2c 20 6f 72 20 30 20 6f 74 68 65 72 77   is, or 0 otherw
2228e 69 73 65 2e 20 41 74 20 70 72 65 73 65 6e 74 2c  ise. At present,
2228f 20 61 20 71 75 65 72 79 20 69 73 20 63 6f 6e 73   a query is cons
22290 69 64 65 72 65 64 20 74 6f 20 62 65 0a 2a 2a 20  idered to be.** 
22291 61 20 6d 69 6e 28 29 2f 6d 61 78 28 29 20 71 75  a min()/max() qu
22292 65 72 79 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20  ery if:.**.**   
22293 31 2e 20 54 68 65 72 65 20 69 73 20 61 20 73 69  1. There is a si
22294 6e 67 6c 65 20 6f 62 6a 65 63 74 20 69 6e 20 74  ngle object in t
22295 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a  he FROM clause..
22296 2a 2a 0a 2a 2a 20 20 20 32 2e 20 54 68 65 72 65  **.**   2. There
22297 20 69 73 20 61 20 73 69 6e 67 6c 65 20 65 78 70   is a single exp
22298 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20 72  ression in the r
22299 65 73 75 6c 74 20 73 65 74 2c 20 61 6e 64 20 69  esult set, and i
2229a 74 20 69 73 0a 2a 2a 20 20 20 20 20 20 65 69 74  t is.**      eit
2229b 68 65 72 20 6d 69 6e 28 78 29 20 6f 72 20 6d 61  her min(x) or ma
2229c 78 28 78 29 2c 20 77 68 65 72 65 20 78 20 69 73  x(x), where x is
2229d 20 61 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65   a column refere
2229e 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  nce..*/.static i
2229f 6e 74 20 6d 69 6e 4d 61 78 51 75 65 72 79 28 50  nt minMaxQuery(P
222a0 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 65  arse *pParse, Se
222a1 6c 65 63 74 20 2a 70 29 7b 0a 20 20 45 78 70 72  lect *p){.  Expr
222a2 20 2a 70 45 78 70 72 3b 0a 20 20 45 78 70 72 4c   *pExpr;.  ExprL
222a3 69 73 74 20 2a 70 45 4c 69 73 74 20 3d 20 70 2d  ist *pEList = p-
222a4 3e 70 45 4c 69 73 74 3b 0a 0a 20 20 69 66 28 20  >pEList;..  if( 
222a5 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31  pEList->nExpr!=1
222a6 20 29 20 72 65 74 75 72 6e 20 57 48 45 52 45 5f   ) return WHERE_
222a7 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b 0a  ORDERBY_NORMAL;.
222a8 20 20 70 45 78 70 72 20 3d 20 70 45 4c 69 73 74    pExpr = pEList
222a9 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[0].pExpr;.  
222aa 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e  pEList = pExpr->
222ab 70 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 45 78  pList;.  if( pEx
222ac 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 47 47 5f 46  pr->op!=TK_AGG_F
222ad 55 4e 43 54 49 4f 4e 20 7c 7c 20 70 45 4c 69 73  UNCTION || pELis
222ae 74 3d 3d 30 20 7c 7c 20 70 45 4c 69 73 74 2d 3e  t==0 || pEList->
222af 6e 45 78 70 72 21 3d 31 20 29 20 72 65 74 75 72  nExpr!=1 ) retur
222b0 6e 20 30 3b 0a 20 20 69 66 28 20 70 45 4c 69 73  n 0;.  if( pELis
222b1 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f  t->a[0].pExpr->o
222b2 70 21 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  p!=TK_AGG_COLUMN
222b3 20 29 20 72 65 74 75 72 6e 20 57 48 45 52 45 5f   ) return WHERE_
222b4 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b 0a  ORDERBY_NORMAL;.
222b5 20 20 69 66 28 20 70 45 78 70 72 2d 3e 74 6f 6b    if( pExpr->tok
222b6 65 6e 2e 6e 21 3d 33 20 29 20 72 65 74 75 72 6e  en.n!=3 ) return
222b7 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4e   WHERE_ORDERBY_N
222b8 4f 52 4d 41 4c 3b 0a 20 20 69 66 28 20 73 71 6c  ORMAL;.  if( sql
222b9 69 74 65 33 53 74 72 4e 49 43 6d 70 28 28 63 68  ite3StrNICmp((ch
222ba 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  ar*)pExpr->token
222bb 2e 7a 2c 22 6d 69 6e 22 2c 33 29 3d 3d 30 20 29  .z,"min",3)==0 )
222bc 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 48 45  {.    return WHE
222bd 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 3b 0a  RE_ORDERBY_MIN;.
222be 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69    }else if( sqli
222bf 74 65 33 53 74 72 4e 49 43 6d 70 28 28 63 68 61  te3StrNICmp((cha
222c0 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e  r*)pExpr->token.
222c1 7a 2c 22 6d 61 78 22 2c 33 29 3d 3d 30 20 29 7b  z,"max",3)==0 ){
222c2 0a 20 20 20 20 72 65 74 75 72 6e 20 57 48 45 52  .    return WHER
222c3 45 5f 4f 52 44 45 52 42 59 5f 4d 41 58 3b 0a 20  E_ORDERBY_MAX;. 
222c4 20 7d 0a 20 20 72 65 74 75 72 6e 20 57 48 45 52   }.  return WHER
222c5 45 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c  E_ORDERBY_NORMAL
222c6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
222c7 72 6f 75 74 69 6e 65 20 72 65 73 6f 6c 76 65 73  routine resolves
222c8 20 61 6e 79 20 6e 61 6d 65 73 20 75 73 65 64 20   any names used 
222c9 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
222ca 74 20 6f 66 20 74 68 65 0a 2a 2a 20 73 75 70 70  t of the.** supp
222cb 6c 69 65 64 20 53 45 4c 45 43 54 20 73 74 61 74  lied SELECT stat
222cc 65 6d 65 6e 74 2e 20 49 66 20 74 68 65 20 53 45  ement. If the SE
222cd 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 62  LECT statement b
222ce 65 69 6e 67 20 72 65 73 6f 6c 76 65 64 0a 2a 2a  eing resolved.**
222cf 20 69 73 20 61 20 73 75 62 2d 73 65 6c 65 63 74   is a sub-select
222d0 2c 20 74 68 65 6e 20 70 4f 75 74 65 72 4e 43 20  , then pOuterNC 
222d1 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
222d2 74 68 65 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  the NameContext 
222d3 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 72 65 6e  .** of the paren
222d4 74 20 53 45 4c 45 43 54 2e 0a 2a 2f 0a 53 51 4c  t SELECT..*/.SQL
222d5 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
222d6 73 71 6c 69 74 65 33 53 65 6c 65 63 74 52 65 73  sqlite3SelectRes
222d7 6f 6c 76 65 28 0a 20 20 50 61 72 73 65 20 2a 70  olve(.  Parse *p
222d8 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f  Parse,         /
222d9 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e  * The parser con
222da 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74  text */.  Select
222db 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
222dc 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73   /* The SELECT s
222dd 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 63  tatement being c
222de 6f 64 65 64 2e 20 2a 2f 0a 20 20 4e 61 6d 65 43  oded. */.  NameC
222df 6f 6e 74 65 78 74 20 2a 70 4f 75 74 65 72 4e 43  ontext *pOuterNC
222e0 20 20 2f 2a 20 54 68 65 20 6f 75 74 65 72 20 6e    /* The outer n
222e1 61 6d 65 20 63 6f 6e 74 65 78 74 2e 20 4d 61 79  ame context. May
222e2 20 62 65 20 4e 55 4c 4c 2e 20 2a 2f 0a 29 7b 0a   be NULL. */.){.
222e3 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
222e4 73 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  st;          /* 
222e5 52 65 73 75 6c 74 20 73 65 74 2e 20 2a 2f 0a 20  Result set. */. 
222e6 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
222e7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
222e8 6f 72 2d 6c 6f 6f 70 20 76 61 72 69 61 62 6c 65  or-loop variable
222e9 20 75 73 65 64 20 69 6e 20 6d 75 6c 74 69 70 6c   used in multipl
222ea 65 20 70 6c 61 63 65 73 20 2a 2f 0a 20 20 4e 61  e places */.  Na
222eb 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20  meContext sNC;  
222ec 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61           /* Loca
222ed 6c 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 2a  l name-context *
222ee 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 47  /.  ExprList *pG
222ef 72 6f 75 70 42 79 3b 20 20 20 20 20 20 20 20 2f  roupBy;        /
222f0 2a 20 54 68 65 20 67 72 6f 75 70 20 62 79 20 63  * The group by c
222f1 6c 61 75 73 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49  lause */..  /* I
222f2 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 68  f this routine h
222f3 61 73 20 72 75 6e 20 62 65 66 6f 72 65 2c 20 72  as run before, r
222f4 65 74 75 72 6e 20 69 6d 6d 65 64 69 61 74 65 6c  eturn immediatel
222f5 79 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69  y. */.  if( p->i
222f6 73 52 65 73 6f 6c 76 65 64 20 29 7b 0a 20 20 20  sResolved ){.   
222f7 20 61 73 73 65 72 74 28 20 21 70 4f 75 74 65 72   assert( !pOuter
222f8 4e 43 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  NC );.    return
222f9 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
222fa 20 20 70 2d 3e 69 73 52 65 73 6f 6c 76 65 64 20    p->isResolved 
222fb 3d 20 31 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  = 1;..  /* If th
222fc 65 72 65 20 68 61 76 65 20 61 6c 72 65 61 64 79  ere have already
222fd 20 62 65 65 6e 20 65 72 72 6f 72 73 2c 20 64 6f   been errors, do
222fe 20 6e 6f 74 68 69 6e 67 2e 20 2a 2f 0a 20 20 69   nothing. */.  i
222ff 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3e  f( pParse->nErr>
22300 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
22301 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
22302 7d 0a 0a 20 20 2f 2a 20 50 72 65 70 61 72 65 20  }..  /* Prepare 
22303 74 68 65 20 73 65 6c 65 63 74 20 73 74 61 74 65  the select state
22304 6d 65 6e 74 2e 20 54 68 69 73 20 63 61 6c 6c 20  ment. This call 
22305 77 69 6c 6c 20 61 6c 6c 6f 63 61 74 65 20 61 6c  will allocate al
22306 6c 20 63 75 72 73 6f 72 73 0a 20 20 2a 2a 20 72  l cursors.  ** r
22307 65 71 75 69 72 65 64 20 74 6f 20 68 61 6e 64 6c  equired to handl
22308 65 20 74 68 65 20 74 61 62 6c 65 73 20 61 6e 64  e the tables and
22309 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 74   subqueries in t
2230a 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a  he FROM clause..
2230b 20 20 2a 2f 0a 20 20 69 66 28 20 70 72 65 70 53    */.  if( prepS
2230c 65 6c 65 63 74 53 74 6d 74 28 70 50 61 72 73 65  electStmt(pParse
2230d 2c 20 70 29 20 29 7b 0a 20 20 20 20 72 65 74 75  , p) ){.    retu
2230e 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
2230f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c  .  }..  /* Resol
22310 76 65 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  ve the expressio
22311 6e 73 20 69 6e 20 74 68 65 20 4c 49 4d 49 54 20  ns in the LIMIT 
22312 61 6e 64 20 4f 46 46 53 45 54 20 63 6c 61 75 73  and OFFSET claus
22313 65 73 2e 20 54 68 65 73 65 0a 20 20 2a 2a 20 61  es. These.  ** a
22314 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74  re not allowed t
22315 6f 20 72 65 66 65 72 20 74 6f 20 61 6e 79 20 6e  o refer to any n
22316 61 6d 65 73 2c 20 73 6f 20 70 61 73 73 20 61 6e  ames, so pass an
22317 20 65 6d 70 74 79 20 4e 61 6d 65 43 6f 6e 74 65   empty NameConte
22318 78 74 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65  xt..  */.  memse
22319 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f  t(&sNC, 0, sizeo
2231a 66 28 73 4e 43 29 29 3b 0a 20 20 73 4e 43 2e 70  f(sNC));.  sNC.p
2231b 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a  Parse = pParse;.
2231c 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70    if( sqlite3Exp
2231d 72 52 65 73 6f 6c 76 65 4e 61 6d 65 73 28 26 73  rResolveNames(&s
2231e 4e 43 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 20 7c  NC, p->pLimit) |
2231f 7c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  |.      sqlite3E
22320 78 70 72 52 65 73 6f 6c 76 65 4e 61 6d 65 73 28  xprResolveNames(
22321 26 73 4e 43 2c 20 70 2d 3e 70 4f 66 66 73 65 74  &sNC, p->pOffset
22322 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
22323 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
22324 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 75 70 20 74  }..  /* Set up t
22325 68 65 20 6c 6f 63 61 6c 20 6e 61 6d 65 2d 63 6f  he local name-co
22326 6e 74 65 78 74 20 74 6f 20 70 61 73 73 20 74 6f  ntext to pass to
22327 20 45 78 70 72 52 65 73 6f 6c 76 65 4e 61 6d 65   ExprResolveName
22328 73 28 29 20 74 6f 0a 20 20 2a 2a 20 72 65 73 6f  s() to.  ** reso
22329 6c 76 65 20 74 68 65 20 65 78 70 72 65 73 73 69  lve the expressi
2232a 6f 6e 2d 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20  on-list..  */.  
2232b 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31  sNC.allowAgg = 1
2232c 3b 0a 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74  ;.  sNC.pSrcList
2232d 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 73 4e   = p->pSrc;.  sN
2232e 43 2e 70 4e 65 78 74 20 3d 20 70 4f 75 74 65 72  C.pNext = pOuter
2232f 4e 43 3b 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76  NC;..  /* Resolv
22330 65 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 72  e names in the r
22331 65 73 75 6c 74 20 73 65 74 2e 20 2a 2f 0a 20 20  esult set. */.  
22332 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69  pEList = p->pELi
22333 73 74 3b 0a 20 20 69 66 28 20 21 70 45 4c 69 73  st;.  if( !pELis
22334 74 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  t ) return SQLIT
22335 45 5f 45 52 52 4f 52 3b 0a 20 20 66 6f 72 28 69  E_ERROR;.  for(i
22336 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45  =0; i<pEList->nE
22337 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45  xpr; i++){.    E
22338 78 70 72 20 2a 70 58 20 3d 20 70 45 4c 69 73 74  xpr *pX = pEList
22339 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[i].pExpr;.  
2233a 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70    if( sqlite3Exp
2233b 72 52 65 73 6f 6c 76 65 4e 61 6d 65 73 28 26 73  rResolveNames(&s
2233c 4e 43 2c 20 70 58 29 20 29 7b 0a 20 20 20 20 20  NC, pX) ){.     
2233d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
2233e 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  RROR;.    }.  }.
2233f 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61  .  /* If there a
22340 72 65 20 6e 6f 20 61 67 67 72 65 67 61 74 65 20  re no aggregate 
22341 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 65  functions in the
22342 20 72 65 73 75 6c 74 2d 73 65 74 2c 20 61 6e 64   result-set, and
22343 20 6e 6f 20 47 52 4f 55 50 20 42 59 20 0a 20 20   no GROUP BY .  
22344 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 64  ** expression, d
22345 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 67 67 72  o not allow aggr
22346 65 67 61 74 65 73 20 69 6e 20 61 6e 79 20 6f 66  egates in any of
22347 20 74 68 65 20 6f 74 68 65 72 20 65 78 70 72 65   the other expre
22348 73 73 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 61  ssions..  */.  a
22349 73 73 65 72 74 28 20 21 70 2d 3e 69 73 41 67 67  ssert( !p->isAgg
2234a 20 29 3b 0a 20 20 70 47 72 6f 75 70 42 79 20 3d   );.  pGroupBy =
2234b 20 70 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20   p->pGroupBy;.  
2234c 69 66 28 20 70 47 72 6f 75 70 42 79 20 7c 7c 20  if( pGroupBy || 
2234d 73 4e 43 2e 68 61 73 41 67 67 20 29 7b 0a 20 20  sNC.hasAgg ){.  
2234e 20 20 70 2d 3e 69 73 41 67 67 20 3d 20 31 3b 0a    p->isAgg = 1;.
2234f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 4e 43    }else{.    sNC
22350 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 30 3b 0a 20  .allowAgg = 0;. 
22351 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 48 41   }..  /* If a HA
22352 56 49 4e 47 20 63 6c 61 75 73 65 20 69 73 20 70  VING clause is p
22353 72 65 73 65 6e 74 2c 20 74 68 65 6e 20 74 68 65  resent, then the
22354 72 65 20 6d 75 73 74 20 62 65 20 61 20 47 52 4f  re must be a GRO
22355 55 50 20 42 59 20 63 6c 61 75 73 65 2e 0a 20 20  UP BY clause..  
22356 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 48 61 76  */.  if( p->pHav
22357 69 6e 67 20 26 26 20 21 70 47 72 6f 75 70 42 79  ing && !pGroupBy
22358 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
22359 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2235a 22 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75  "a GROUP BY clau
2235b 73 65 20 69 73 20 72 65 71 75 69 72 65 64 20 62  se is required b
2235c 65 66 6f 72 65 20 48 41 56 49 4e 47 22 29 3b 0a  efore HAVING");.
2235d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2235e 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20  E_ERROR;.  }..  
2235f 2f 2a 20 41 64 64 20 74 68 65 20 65 78 70 72 65  /* Add the expre
22360 73 73 69 6f 6e 20 6c 69 73 74 20 74 6f 20 74 68  ssion list to th
22361 65 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 62  e name-context b
22362 65 66 6f 72 65 20 70 61 72 73 69 6e 67 20 74 68  efore parsing th
22363 65 0a 20 20 2a 2a 20 6f 74 68 65 72 20 65 78 70  e.  ** other exp
22364 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20  ressions in the 
22365 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
22366 2e 20 54 68 69 73 20 69 73 20 73 6f 20 74 68 61  . This is so tha
22367 74 0a 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f  t.  ** expressio
22368 6e 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 20  ns in the WHERE 
22369 63 6c 61 75 73 65 20 28 65 74 63 2e 29 20 63 61  clause (etc.) ca
2236a 6e 20 72 65 66 65 72 20 74 6f 20 65 78 70 72 65  n refer to expre
2236b 73 73 69 6f 6e 73 20 62 79 0a 20 20 2a 2a 20 61  ssions by.  ** a
2236c 6c 69 61 73 65 73 20 69 6e 20 74 68 65 20 72 65  liases in the re
2236d 73 75 6c 74 20 73 65 74 2e 0a 20 20 2a 2a 0a 20  sult set..  **. 
2236e 20 2a 2a 20 4d 69 6e 6f 72 20 70 6f 69 6e 74 3a   ** Minor point:
2236f 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
22370 63 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 65  case, then the e
22371 78 70 72 65 73 73 69 6f 6e 20 77 69 6c 6c 20 62  xpression will b
22372 65 0a 20 20 2a 2a 20 72 65 2d 65 76 61 6c 75 61  e.  ** re-evalua
22373 74 65 64 20 66 6f 72 20 65 61 63 68 20 72 65 66  ted for each ref
22374 65 72 65 6e 63 65 20 74 6f 20 69 74 2e 0a 20 20  erence to it..  
22375 2a 2f 0a 20 20 73 4e 43 2e 70 45 4c 69 73 74 20  */.  sNC.pEList 
22376 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 69  = p->pEList;.  i
22377 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 52 65  f( sqlite3ExprRe
22378 73 6f 6c 76 65 4e 61 6d 65 73 28 26 73 4e 43 2c  solveNames(&sNC,
22379 20 70 2d 3e 70 57 68 65 72 65 29 20 7c 7c 0a 20   p->pWhere) ||. 
2237a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 52      sqlite3ExprR
2237b 65 73 6f 6c 76 65 4e 61 6d 65 73 28 26 73 4e 43  esolveNames(&sNC
2237c 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 20 29 7b  , p->pHaving) ){
2237d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
2237e 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
2237f 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 3d 3d 30  if( p->pPrior==0
22380 20 29 7b 0a 20 20 20 20 69 66 28 20 70 72 6f 63   ){.    if( proc
22381 65 73 73 4f 72 64 65 72 47 72 6f 75 70 42 79 28  essOrderGroupBy(
22382 70 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 4f  pParse, p, p->pO
22383 72 64 65 72 42 79 2c 20 31 2c 20 26 73 4e 43 2e  rderBy, 1, &sNC.
22384 68 61 73 41 67 67 29 20 29 7b 0a 20 20 20 20 20  hasAgg) ){.     
22385 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
22386 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  RROR;.    }.  }.
22387 20 20 69 66 28 20 70 72 6f 63 65 73 73 4f 72 64    if( processOrd
22388 65 72 47 72 6f 75 70 42 79 28 70 50 61 72 73 65  erGroupBy(pParse
22389 2c 20 70 2c 20 70 47 72 6f 75 70 42 79 2c 20 30  , p, pGroupBy, 0
2238a 2c 20 26 73 4e 43 2e 68 61 73 41 67 67 29 20 29  , &sNC.hasAgg) )
2238b 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
2238c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a  ITE_ERROR;.  }..
2238d 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62    if( pParse->db
2238e 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2238f 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
22390 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a  ITE_NOMEM;.  }..
22391 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74    /* Make sure t
22392 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75  he GROUP BY clau
22393 73 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74  se does not cont
22394 61 69 6e 20 61 67 67 72 65 67 61 74 65 20 66 75  ain aggregate fu
22395 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20  nctions..  */.  
22396 69 66 28 20 70 47 72 6f 75 70 42 79 20 29 7b 0a  if( pGroupBy ){.
22397 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
22398 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
22399 0a 20 20 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c  .  .    for(i=0,
2239a 20 70 49 74 65 6d 3d 70 47 72 6f 75 70 42 79 2d   pItem=pGroupBy-
2239b 3e 61 3b 20 69 3c 70 47 72 6f 75 70 42 79 2d 3e  >a; i<pGroupBy->
2239c 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65  nExpr; i++, pIte
2239d 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  m++){.      if( 
2239e 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
2239f 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 45 50  pItem->pExpr, EP
223a0 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20 20 20  _Agg) ){.       
223a1 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
223a2 28 70 50 61 72 73 65 2c 20 22 61 67 67 72 65 67  (pParse, "aggreg
223a3 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72  ate functions ar
223a4 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 69 6e  e not allowed in
223a5 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22   ".            "
223a6 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61  the GROUP BY cla
223a7 75 73 65 22 29 3b 0a 20 20 20 20 20 20 20 20 72  use");.        r
223a8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
223a9 4f 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  OR;.      }.    
223aa 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  }.  }..  /* If t
223ab 68 69 73 20 69 73 20 6f 6e 65 20 53 45 4c 45 43  his is one SELEC
223ac 54 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 2c  T of a compound,
223ad 20 62 65 20 73 75 72 65 20 74 6f 20 72 65 73 6f   be sure to reso
223ae 6c 76 65 20 6e 61 6d 65 73 0a 20 20 2a 2a 20 69  lve names.  ** i
223af 6e 20 74 68 65 20 6f 74 68 65 72 20 53 45 4c 45  n the other SELE
223b0 43 54 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  CTs..  */.  if( 
223b1 70 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20  p->pPrior ){.   
223b2 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 53   return sqlite3S
223b3 65 6c 65 63 74 52 65 73 6f 6c 76 65 28 70 50 61  electResolve(pPa
223b4 72 73 65 2c 20 70 2d 3e 70 50 72 69 6f 72 2c 20  rse, p->pPrior, 
223b5 70 4f 75 74 65 72 4e 43 29 3b 0a 20 20 7d 65 6c  pOuterNC);.  }el
223b6 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  se{.    return S
223b7 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a  QLITE_OK;.  }.}.
223b8 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 74 68 65  ./*.** Reset the
223b9 20 61 67 67 72 65 67 61 74 65 20 61 63 63 75 6d   aggregate accum
223ba 75 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  ulator..**.** Th
223bb 65 20 61 67 67 72 65 67 61 74 65 20 61 63 63 75  e aggregate accu
223bc 6d 75 6c 61 74 6f 72 20 69 73 20 61 20 73 65 74  mulator is a set
223bd 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73   of memory cells
223be 20 74 68 61 74 20 68 6f 6c 64 0a 2a 2a 20 69 6e   that hold.** in
223bf 74 65 72 6d 65 64 69 61 74 65 20 72 65 73 75 6c  termediate resul
223c0 74 73 20 77 68 69 6c 65 20 63 61 6c 63 75 6c 61  ts while calcula
223c1 74 69 6e 67 20 61 6e 20 61 67 67 72 65 67 61 74  ting an aggregat
223c2 65 2e 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74  e.  This.** rout
223c3 69 6e 65 20 73 69 6d 70 6c 79 20 73 74 6f 72 65  ine simply store
223c4 73 20 4e 55 4c 4c 73 20 69 6e 20 61 6c 6c 20 6f  s NULLs in all o
223c5 66 20 74 68 6f 73 65 20 6d 65 6d 6f 72 79 20 63  f those memory c
223c6 65 6c 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ells..*/.static 
223c7 76 6f 69 64 20 72 65 73 65 74 41 63 63 75 6d 75  void resetAccumu
223c8 6c 61 74 6f 72 28 50 61 72 73 65 20 2a 70 50 61  lator(Parse *pPa
223c9 72 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41  rse, AggInfo *pA
223ca 67 67 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65 20  ggInfo){.  Vdbe 
223cb 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
223cc 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73  be;.  int i;.  s
223cd 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75  truct AggInfo_fu
223ce 6e 63 20 2a 70 46 75 6e 63 3b 0a 20 20 69 66 28  nc *pFunc;.  if(
223cf 20 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63   pAggInfo->nFunc
223d0 2b 70 41 67 67 49 6e 66 6f 2d 3e 6e 43 6f 6c 75  +pAggInfo->nColu
223d1 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  mn==0 ){.    ret
223d2 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  urn;.  }.  for(i
223d3 3d 30 3b 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e  =0; i<pAggInfo->
223d4 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20  nColumn; i++){. 
223d5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
223d6 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
223d7 20 30 2c 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43   0, pAggInfo->aC
223d8 6f 6c 5b 69 5d 2e 69 4d 65 6d 29 3b 0a 20 20 7d  ol[i].iMem);.  }
223d9 0a 20 20 66 6f 72 28 70 46 75 6e 63 3d 70 41 67  .  for(pFunc=pAg
223da 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 2c 20 69 3d  gInfo->aFunc, i=
223db 30 3b 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e  0; i<pAggInfo->n
223dc 46 75 6e 63 3b 20 69 2b 2b 2c 20 70 46 75 6e 63  Func; i++, pFunc
223dd 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ++){.    sqlite3
223de 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
223df 5f 4e 75 6c 6c 2c 20 30 2c 20 70 46 75 6e 63 2d  _Null, 0, pFunc-
223e0 3e 69 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20  >iMem);.    if( 
223e1 70 46 75 6e 63 2d 3e 69 44 69 73 74 69 6e 63 74  pFunc->iDistinct
223e2 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 45 78 70  >=0 ){.      Exp
223e3 72 20 2a 70 45 20 3d 20 70 46 75 6e 63 2d 3e 70  r *pE = pFunc->p
223e4 45 78 70 72 3b 0a 20 20 20 20 20 20 69 66 28 20  Expr;.      if( 
223e5 70 45 2d 3e 70 4c 69 73 74 3d 3d 30 20 7c 7c 20  pE->pList==0 || 
223e6 70 45 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72  pE->pList->nExpr
223e7 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 73  !=1 ){.        s
223e8 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
223e9 50 61 72 73 65 2c 20 22 44 49 53 54 49 4e 43 54  Parse, "DISTINCT
223ea 20 69 6e 20 61 67 67 72 65 67 61 74 65 20 6d 75   in aggregate mu
223eb 73 74 20 62 65 20 66 6f 6c 6c 6f 77 65 64 20 22  st be followed "
223ec 0a 20 20 20 20 20 20 20 20 20 20 20 22 62 79 20  .           "by 
223ed 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 22 29 3b  an expression");
223ee 0a 20 20 20 20 20 20 20 20 70 46 75 6e 63 2d 3e  .        pFunc->
223ef 69 44 69 73 74 69 6e 63 74 20 3d 20 2d 31 3b 0a  iDistinct = -1;.
223f0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
223f1 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b       KeyInfo *pK
223f2 65 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66 6f  eyInfo = keyInfo
223f3 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61  FromExprList(pPa
223f4 72 73 65 2c 20 70 45 2d 3e 70 4c 69 73 74 29 3b  rse, pE->pList);
223f5 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
223f6 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
223f7 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20  _OpenEphemeral, 
223f8 70 46 75 6e 63 2d 3e 69 44 69 73 74 69 6e 63 74  pFunc->iDistinct
223f9 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  , 0, 0,.        
223fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
223fb 20 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66    (char*)pKeyInf
223fc 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41  o, P4_KEYINFO_HA
223fd 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20 20 7d 0a  NDOFF);.      }.
223fe 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
223ff 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 4f 50  ** Invoke the OP
22400 5f 41 67 67 46 69 6e 61 6c 69 7a 65 20 6f 70 63  _AggFinalize opc
22401 6f 64 65 20 66 6f 72 20 65 76 65 72 79 20 61 67  ode for every ag
22402 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
22403 0a 2a 2a 20 69 6e 20 74 68 65 20 41 67 67 49 6e  .** in the AggIn
22404 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  fo structure..*/
22405 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6e  .static void fin
22406 61 6c 69 7a 65 41 67 67 46 75 6e 63 74 69 6f 6e  alizeAggFunction
22407 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  s(Parse *pParse,
22408 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e   AggInfo *pAggIn
22409 66 6f 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  fo){.  Vdbe *v =
2240a 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
2240b 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63    int i;.  struc
2240c 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 2a  t AggInfo_func *
2240d 70 46 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70  pF;.  for(i=0, p
2240e 46 3d 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e  F=pAggInfo->aFun
2240f 63 3b 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e  c; i<pAggInfo->n
22410 46 75 6e 63 3b 20 69 2b 2b 2c 20 70 46 2b 2b 29  Func; i++, pF++)
22411 7b 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a  {.    ExprList *
22412 70 4c 69 73 74 20 3d 20 70 46 2d 3e 70 45 78 70  pList = pF->pExp
22413 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 20 20 73 71  r->pList;.    sq
22414 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
22415 76 2c 20 4f 50 5f 41 67 67 46 69 6e 61 6c 2c 20  v, OP_AggFinal, 
22416 70 46 2d 3e 69 4d 65 6d 2c 20 70 4c 69 73 74 20  pF->iMem, pList 
22417 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a  ? pList->nExpr :
22418 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20   0, 0,.         
22419 20 20 20 20 20 20 20 20 20 20 20 20 20 28 76 6f               (vo
2241a 69 64 2a 29 70 46 2d 3e 70 46 75 6e 63 2c 20 50  id*)pF->pFunc, P
2241b 34 5f 46 55 4e 43 44 45 46 29 3b 0a 20 20 7d 0a  4_FUNCDEF);.  }.
2241c 7d 0a 0a 2f 2a 0a 2a 2a 20 55 70 64 61 74 65 20  }../*.** Update 
2241d 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20  the accumulator 
2241e 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66 6f 72  memory cells for
2241f 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 62 61   an aggregate ba
22420 73 65 64 20 6f 6e 0a 2a 2a 20 74 68 65 20 63 75  sed on.** the cu
22421 72 72 65 6e 74 20 63 75 72 73 6f 72 20 70 6f 73  rrent cursor pos
22422 69 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ition..*/.static
22423 20 76 6f 69 64 20 75 70 64 61 74 65 41 63 63 75   void updateAccu
22424 6d 75 6c 61 74 6f 72 28 50 61 72 73 65 20 2a 70  mulator(Parse *p
22425 50 61 72 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a  Parse, AggInfo *
22426 70 41 67 67 49 6e 66 6f 29 7b 0a 20 20 56 64 62  pAggInfo){.  Vdb
22427 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
22428 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  Vdbe;.  int i;. 
22429 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f   struct AggInfo_
2242a 66 75 6e 63 20 2a 70 46 3b 0a 20 20 73 74 72 75  func *pF;.  stru
2242b 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a  ct AggInfo_col *
2242c 70 43 3b 0a 0a 20 20 70 41 67 67 49 6e 66 6f 2d  pC;..  pAggInfo-
2242d 3e 64 69 72 65 63 74 4d 6f 64 65 20 3d 20 31 3b  >directMode = 1;
2242e 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 46 3d 70  .  for(i=0, pF=p
2242f 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b 20  AggInfo->aFunc; 
22430 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e  i<pAggInfo->nFun
22431 63 3b 20 69 2b 2b 2c 20 70 46 2b 2b 29 7b 0a 20  c; i++, pF++){. 
22432 20 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 20     int nArg;.   
22433 20 69 6e 74 20 61 64 64 72 4e 65 78 74 20 3d 20   int addrNext = 
22434 30 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 41 67  0;.    int regAg
22435 67 3b 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20  g;.    ExprList 
22436 2a 70 4c 69 73 74 20 3d 20 70 46 2d 3e 70 45 78  *pList = pF->pEx
22437 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 20 20 69  pr->pList;.    i
22438 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20  f( pList ){.    
22439 20 20 6e 41 72 67 20 3d 20 70 4c 69 73 74 2d 3e    nArg = pList->
2243a 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 72 65 67  nExpr;.      reg
2243b 41 67 67 20 3d 20 73 71 6c 69 74 65 33 47 65 74  Agg = sqlite3Get
2243c 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65  TempRange(pParse
2243d 2c 20 6e 41 72 67 29 3b 0a 20 20 20 20 20 20 73  , nArg);.      s
2243e 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45 78  qlite3ExprCodeEx
2243f 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70  prList(pParse, p
22440 4c 69 73 74 2c 20 72 65 67 41 67 67 2c 20 30 29  List, regAgg, 0)
22441 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
22442 20 20 20 6e 41 72 67 20 3d 20 30 3b 0a 20 20 20     nArg = 0;.   
22443 20 20 20 72 65 67 41 67 67 20 3d 20 30 3b 0a 20     regAgg = 0;. 
22444 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 46 2d     }.    if( pF-
22445 3e 69 44 69 73 74 69 6e 63 74 3e 3d 30 20 29 7b  >iDistinct>=0 ){
22446 0a 20 20 20 20 20 20 61 64 64 72 4e 65 78 74 20  .      addrNext 
22447 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
22448 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20  eLabel(v);.     
22449 20 61 73 73 65 72 74 28 20 6e 41 72 67 3d 3d 31   assert( nArg==1
2244a 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 44 69   );.      codeDi
2244b 73 74 69 6e 63 74 28 70 50 61 72 73 65 2c 20 70  stinct(pParse, p
2244c 46 2d 3e 69 44 69 73 74 69 6e 63 74 2c 20 61 64  F->iDistinct, ad
2244d 64 72 4e 65 78 74 2c 20 31 2c 20 72 65 67 41 67  drNext, 1, regAg
2244e 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  g);.    }.    if
2244f 28 20 70 46 2d 3e 70 46 75 6e 63 2d 3e 6e 65 65  ( pF->pFunc->nee
22450 64 43 6f 6c 6c 53 65 71 20 29 7b 0a 20 20 20 20  dCollSeq ){.    
22451 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
22452 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 74 72 75   = 0;.      stru
22453 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d  ct ExprList_item
22454 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 69   *pItem;.      i
22455 6e 74 20 6a 3b 0a 20 20 20 20 20 20 61 73 73 65  nt j;.      asse
22456 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29 3b 20  rt( pList!=0 ); 
22457 20 2f 2a 20 70 4c 69 73 74 21 3d 30 20 69 66 20   /* pList!=0 if 
22458 70 46 2d 3e 70 46 75 6e 63 2d 3e 6e 65 65 64 43  pF->pFunc->needC
22459 6f 6c 6c 53 65 71 20 69 73 20 74 72 75 65 20 2a  ollSeq is true *
2245a 2f 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c  /.      for(j=0,
2245b 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b   pItem=pList->a;
2245c 20 21 70 43 6f 6c 6c 20 26 26 20 6a 3c 6e 41 72   !pColl && j<nAr
2245d 67 3b 20 6a 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29  g; j++, pItem++)
2245e 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20  {.        pColl 
2245f 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c  = sqlite3ExprCol
22460 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 49 74  lSeq(pParse, pIt
22461 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20  em->pExpr);.    
22462 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 70    }.      if( !p
22463 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  Coll ){.        
22464 70 43 6f 6c 6c 20 3d 20 70 50 61 72 73 65 2d 3e  pColl = pParse->
22465 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20  db->pDfltColl;. 
22466 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
22467 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
22468 2c 20 4f 50 5f 43 6f 6c 6c 53 65 71 2c 20 30 2c  , OP_CollSeq, 0,
22469 20 30 2c 20 30 2c 20 28 63 68 61 72 20 2a 29 70   0, 0, (char *)p
2246a 43 6f 6c 6c 2c 20 50 34 5f 43 4f 4c 4c 53 45 51  Coll, P4_COLLSEQ
2246b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
2246c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2246d 2c 20 4f 50 5f 41 67 67 53 74 65 70 2c 20 30 2c  , OP_AggStep, 0,
2246e 20 72 65 67 41 67 67 2c 20 70 46 2d 3e 69 4d 65   regAgg, pF->iMe
2246f 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  m,.             
22470 20 20 20 20 20 20 20 20 20 28 76 6f 69 64 2a 29           (void*)
22471 70 46 2d 3e 70 46 75 6e 63 2c 20 50 34 5f 46 55  pF->pFunc, P4_FU
22472 4e 43 44 45 46 29 3b 0a 20 20 20 20 73 71 6c 69  NCDEF);.    sqli
22473 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
22474 76 2c 20 6e 41 72 67 29 3b 0a 20 20 20 20 73 71  v, nArg);.    sq
22475 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
22476 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65  Range(pParse, re
22477 67 41 67 67 2c 20 6e 41 72 67 29 3b 0a 20 20 20  gAgg, nArg);.   
22478 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
22479 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28  eAffinityChange(
2247a 70 50 61 72 73 65 2c 20 72 65 67 41 67 67 2c 20  pParse, regAgg, 
2247b 6e 41 72 67 29 3b 0a 20 20 20 20 69 66 28 20 61  nArg);.    if( a
2247c 64 64 72 4e 65 78 74 20 29 7b 0a 20 20 20 20 20  ddrNext ){.     
2247d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
2247e 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72  lveLabel(v, addr
2247f 4e 65 78 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Next);.    }.  }
22480 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 43 3d 70  .  for(i=0, pC=p
22481 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 3b 20 69  AggInfo->aCol; i
22482 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 41 63 63 75  <pAggInfo->nAccu
22483 6d 75 6c 61 74 6f 72 3b 20 69 2b 2b 2c 20 70 43  mulator; i++, pC
22484 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ++){.    sqlite3
22485 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
22486 20 70 43 2d 3e 70 45 78 70 72 2c 20 70 43 2d 3e   pC->pExpr, pC->
22487 69 4d 65 6d 29 3b 0a 20 20 7d 0a 20 20 70 41 67  iMem);.  }.  pAg
22488 67 49 6e 66 6f 2d 3e 64 69 72 65 63 74 4d 6f 64  gInfo->directMod
22489 65 20 3d 20 30 3b 0a 7d 0a 0a 23 69 66 20 30 0a  e = 0;.}..#if 0.
2248a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
2248b 69 6f 6e 20 69 73 20 75 73 65 64 20 77 68 65 6e  ion is used when
2248c 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
2248d 65 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20 63  ent is used to c
2248e 72 65 61 74 65 20 61 0a 2a 2a 20 74 65 6d 70 6f  reate a.** tempo
2248f 72 61 72 79 20 74 61 62 6c 65 20 66 6f 72 20 69  rary table for i
22490 74 65 72 61 74 69 6e 67 20 74 68 72 6f 75 67 68  terating through
22491 20 77 68 65 6e 20 72 75 6e 6e 69 6e 67 20 61 6e   when running an
22492 20 49 4e 53 54 45 41 44 20 4f 46 0a 2a 2a 20 55   INSTEAD OF.** U
22493 50 44 41 54 45 20 6f 72 20 49 4e 53 54 45 41 44  PDATE or INSTEAD
22494 20 4f 46 20 44 45 4c 45 54 45 20 74 72 69 67 67   OF DELETE trigg
22495 65 72 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 70 6f  er. .**.** If po
22496 73 73 69 62 6c 65 2c 20 74 68 65 20 53 45 4c 45  ssible, the SELE
22497 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  CT statement is 
22498 6d 6f 64 69 66 69 65 64 20 73 6f 20 74 68 61 74  modified so that
22499 20 4e 55 4c 4c 20 76 61 6c 75 65 73 0a 2a 2a 20   NULL values.** 
2249a 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68  are stored in th
2249b 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  e temporary tabl
2249c 65 20 66 6f 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e  e for all column
2249d 73 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20  s for which the 
2249e 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  .** correspondin
2249f 67 20 62 69 74 20 69 6e 20 61 72 67 75 6d 65 6e  g bit in argumen
224a0 74 20 6d 61 73 6b 20 69 73 20 6e 6f 74 20 73 65  t mask is not se
224a1 74 2e 20 49 66 20 6d 61 73 6b 20 74 61 6b 65 73  t. If mask takes
224a2 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 61 6c 20   the.** special 
224a3 76 61 6c 75 65 20 30 78 66 66 66 66 66 66 66 66  value 0xffffffff
224a4 2c 20 74 68 65 6e 20 61 6c 6c 20 63 6f 6c 75 6d  , then all colum
224a5 6e 73 20 61 72 65 20 70 6f 70 75 6c 61 74 65 64  ns are populated
224a6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
224a7 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
224a8 53 65 6c 65 63 74 4d 61 73 6b 28 50 61 72 73 65  SelectMask(Parse
224a9 20 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63 74   *pParse, Select
224aa 20 2a 70 2c 20 75 33 32 20 6d 61 73 6b 29 7b 0a   *p, u32 mask){.
224ab 20 20 69 66 28 20 70 20 26 26 20 21 70 2d 3e 70    if( p && !p->p
224ac 50 72 69 6f 72 20 26 26 20 21 70 2d 3e 69 73 44  Prior && !p->isD
224ad 69 73 74 69 6e 63 74 20 26 26 20 6d 61 73 6b 21  istinct && mask!
224ae 3d 30 78 66 66 66 66 66 66 66 66 20 29 7b 0a 20  =0xffffffff ){. 
224af 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c     ExprList *pEL
224b0 69 73 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  ist;.    int i;.
224b1 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
224b2 74 52 65 73 6f 6c 76 65 28 70 50 61 72 73 65 2c  tResolve(pParse,
224b3 20 70 2c 20 30 29 3b 0a 20 20 20 20 70 45 4c 69   p, 0);.    pELi
224b4 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a  st = p->pEList;.
224b5 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 70 45 4c      for(i=0; pEL
224b6 69 73 74 20 26 26 20 69 3c 70 45 4c 69 73 74 2d  ist && i<pEList-
224b7 3e 6e 45 78 70 72 20 26 26 20 69 3c 33 32 3b 20  >nExpr && i<32; 
224b8 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
224b9 21 28 6d 61 73 6b 26 28 28 75 33 32 29 31 3c 3c  !(mask&((u32)1<<
224ba 69 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73  i)) ){.        s
224bb 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
224bc 28 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45  (pEList->a[i].pE
224bd 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 70 45  xpr);.        pE
224be 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72  List->a[i].pExpr
224bf 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 70   = sqlite3Expr(p
224c0 50 61 72 73 65 2d 3e 64 62 2c 20 54 4b 5f 4e 55  Parse->db, TK_NU
224c1 4c 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20  LL, 0, 0, 0);.  
224c2 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
224c3 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
224c4 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f  Generate code fo
224c5 72 20 74 68 65 20 67 69 76 65 6e 20 53 45 4c 45  r the given SELE
224c6 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  CT statement..**
224c7 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20  .** The results 
224c8 61 72 65 20 64 69 73 74 72 69 62 75 74 65 64 20  are distributed 
224c9 69 6e 20 76 61 72 69 6f 75 73 20 77 61 79 73 20  in various ways 
224ca 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65  depending on the
224cb 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  .** contents of 
224cc 74 68 65 20 53 65 6c 65 63 74 44 65 73 74 20 73  the SelectDest s
224cd 74 72 75 63 74 75 72 65 20 70 6f 69 6e 74 65 64  tructure pointed
224ce 20 74 6f 20 62 79 20 61 72 67 75 6d 65 6e 74 20   to by argument 
224cf 70 44 65 73 74 0a 2a 2a 20 61 73 20 66 6f 6c 6c  pDest.** as foll
224d0 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 70  ows:.**.**     p
224d1 44 65 73 74 2d 3e 65 44 65 73 74 20 20 20 20 52  Dest->eDest    R
224d2 65 73 75 6c 74 0a 2a 2a 20 20 20 20 20 2d 2d 2d  esult.**     ---
224d3 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 2d 2d 2d  ---------    ---
224d4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
224d5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
224d6 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20  --------.**     
224d7 53 52 54 5f 43 61 6c 6c 62 61 63 6b 20 20 20 20  SRT_Callback    
224d8 49 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62  Invoke the callb
224d9 61 63 6b 20 66 6f 72 20 65 61 63 68 20 72 6f 77  ack for each row
224da 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2e 0a   of the result..
224db 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 4d 65  **.**     SRT_Me
224dc 6d 20 20 20 20 20 20 20 20 20 53 74 6f 72 65 20  m         Store 
224dd 66 69 72 73 74 20 72 65 73 75 6c 74 20 69 6e 20  first result in 
224de 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 70 44 65 73  memory cell pDes
224df 74 2d 3e 69 50 61 72 6d 0a 2a 2a 0a 2a 2a 20 20  t->iParm.**.**  
224e0 20 20 20 53 52 54 5f 53 65 74 20 20 20 20 20 20     SRT_Set      
224e1 20 20 20 53 74 6f 72 65 20 6e 6f 6e 2d 6e 75 6c     Store non-nul
224e2 6c 20 72 65 73 75 6c 74 73 20 61 73 20 6b 65 79  l results as key
224e3 73 20 6f 66 20 74 61 62 6c 65 20 70 44 65 73 74  s of table pDest
224e4 2d 3e 69 50 61 72 6d 2e 20 0a 2a 2a 20 20 20 20  ->iParm. .**    
224e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
224e6 20 41 70 70 6c 79 20 74 68 65 20 61 66 66 69 6e   Apply the affin
224e7 69 74 79 20 70 44 65 73 74 2d 3e 61 66 66 69 6e  ity pDest->affin
224e8 69 74 79 20 62 65 66 6f 72 65 20 73 74 6f 72 69  ity before stori
224e9 6e 67 20 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20 20  ng them..**.**  
224ea 20 20 20 53 52 54 5f 55 6e 69 6f 6e 20 20 20 20     SRT_Union    
224eb 20 20 20 53 74 6f 72 65 20 72 65 73 75 6c 74 73     Store results
224ec 20 61 73 20 61 20 6b 65 79 20 69 6e 20 61 20 74   as a key in a t
224ed 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 70  emporary table p
224ee 44 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a 0a  Dest->iParm..**.
224ef 2a 2a 20 20 20 20 20 53 52 54 5f 45 78 63 65 70  **     SRT_Excep
224f0 74 20 20 20 20 20 20 52 65 6d 6f 76 65 20 72 65  t      Remove re
224f1 73 75 6c 74 73 20 66 72 6f 6d 20 74 68 65 20 74  sults from the t
224f2 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 70  emporary table p
224f3 44 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a 0a  Dest->iParm..**.
224f4 2a 2a 20 20 20 20 20 53 52 54 5f 54 61 62 6c 65  **     SRT_Table
224f5 20 20 20 20 20 20 20 53 74 6f 72 65 20 72 65 73         Store res
224f6 75 6c 74 73 20 69 6e 20 74 65 6d 70 6f 72 61 72  ults in temporar
224f7 79 20 74 61 62 6c 65 20 70 44 65 73 74 2d 3e 69  y table pDest->i
224f8 50 61 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53  Parm.**.**     S
224f9 52 54 5f 45 70 68 65 6d 54 61 62 20 20 20 20 43  RT_EphemTab    C
224fa 72 65 61 74 65 20 61 6e 20 74 65 6d 70 6f 72 61  reate an tempora
224fb 72 79 20 74 61 62 6c 65 20 70 44 65 73 74 2d 3e  ry table pDest->
224fc 69 50 61 72 6d 20 61 6e 64 20 73 74 6f 72 65 0a  iParm and store.
224fd 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
224fe 20 20 20 20 20 20 20 74 68 65 20 72 65 73 75 6c         the resul
224ff 74 20 74 68 65 72 65 2e 20 54 68 65 20 63 75 72  t there. The cur
22500 73 6f 72 20 69 73 20 6c 65 66 74 20 6f 70 65 6e  sor is left open
22501 20 61 66 74 65 72 0a 2a 2a 20 20 20 20 20 20 20   after.**       
22502 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65                re
22503 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20  turning..**.**  
22504 20 20 20 53 52 54 5f 53 75 62 72 6f 75 74 69 6e     SRT_Subroutin
22505 65 20 20 46 6f 72 20 65 61 63 68 20 72 6f 77 20  e  For each row 
22506 72 65 74 75 72 6e 65 64 2c 20 70 75 73 68 20 74  returned, push t
22507 68 65 20 72 65 73 75 6c 74 73 20 6f 6e 74 6f 20  he results onto 
22508 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
22509 20 20 20 20 20 20 20 20 20 20 20 76 64 62 65 20             vdbe 
2250a 73 74 61 63 6b 20 61 6e 64 20 63 61 6c 6c 20 74  stack and call t
2250b 68 65 20 73 75 62 72 6f 75 74 69 6e 65 20 28 76  he subroutine (v
2250c 69 61 20 4f 50 5f 47 6f 73 75 62 29 0a 2a 2a 20  ia OP_Gosub).** 
2250d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2250e 20 20 20 20 61 74 20 61 64 64 72 65 73 73 20 70      at address p
2250f 44 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a 0a  Dest->iParm..**.
22510 2a 2a 20 20 20 20 20 53 52 54 5f 45 78 69 73 74  **     SRT_Exist
22511 73 20 20 20 20 20 20 53 74 6f 72 65 20 61 20 31  s      Store a 1
22512 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20   in memory cell 
22513 70 44 65 73 74 2d 3e 69 50 61 72 6d 20 69 66 20  pDest->iParm if 
22514 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 20 20  the result.**   
22515 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22516 20 20 73 65 74 20 69 73 20 6e 6f 74 20 65 6d 70    set is not emp
22517 74 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52  ty..**.**     SR
22518 54 5f 44 69 73 63 61 72 64 20 20 20 20 20 54 68  T_Discard     Th
22519 72 6f 77 20 74 68 65 20 72 65 73 75 6c 74 73 20  row the results 
2251a 61 77 61 79 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  away..**.** See 
2251b 74 68 65 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c  the selectInnerL
2251c 6f 6f 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 66  oop() function f
2251d 6f 72 20 61 20 63 61 6e 6f 6e 69 63 61 6c 20 6c  or a canonical l
2251e 69 73 74 69 6e 67 20 6f 66 20 74 68 65 20 0a 2a  isting of the .*
2251f 2a 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73  * allowed values
22520 20 6f 66 20 65 44 65 73 74 20 61 6e 64 20 74 68   of eDest and th
22521 65 69 72 20 6d 65 61 6e 69 6e 67 73 2e 0a 2a 2a  eir meanings..**
22522 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
22523 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
22524 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 20 20  ber of errors.  
22525 49 66 20 61 6e 79 20 65 72 72 6f 72 73 20 61 72  If any errors ar
22526 65 0a 2a 2a 20 65 6e 63 6f 75 6e 74 65 72 65 64  e.** encountered
22527 2c 20 74 68 65 6e 20 61 6e 20 61 70 70 72 6f 70  , then an approp
22528 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65 73 73  riate error mess
22529 61 67 65 20 69 73 20 6c 65 66 74 20 69 6e 0a 2a  age is left in.*
2252a 2a 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73  * pParse->zErrMs
2252b 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  g..**.** This ro
2252c 75 74 69 6e 65 20 64 6f 65 73 20 4e 4f 54 20 66  utine does NOT f
2252d 72 65 65 20 74 68 65 20 53 65 6c 65 63 74 20 73  ree the Select s
2252e 74 72 75 63 74 75 72 65 20 70 61 73 73 65 64 20  tructure passed 
2252f 69 6e 2e 20 20 54 68 65 0a 2a 2a 20 63 61 6c 6c  in.  The.** call
22530 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6e 65 65  ing function nee
22531 64 73 20 74 6f 20 64 6f 20 74 68 61 74 2e 0a 2a  ds to do that..*
22532 2a 0a 2a 2a 20 54 68 65 20 70 50 61 72 65 6e 74  *.** The pParent
22533 2c 20 70 61 72 65 6e 74 54 61 62 2c 20 61 6e 64  , parentTab, and
22534 20 2a 70 50 61 72 65 6e 74 41 67 67 20 66 69 65   *pParentAgg fie
22535 6c 64 73 20 61 72 65 20 66 69 6c 6c 65 64 20 69  lds are filled i
22536 6e 20 69 66 20 74 68 69 73 0a 2a 2a 20 53 45 4c  n if this.** SEL
22537 45 43 54 20 69 73 20 61 20 73 75 62 71 75 65 72  ECT is a subquer
22538 79 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  y.  This routine
22539 20 6d 61 79 20 74 72 79 20 74 6f 20 63 6f 6d 62   may try to comb
2253a 69 6e 65 20 74 68 69 73 20 53 45 4c 45 43 54 0a  ine this SELECT.
2253b 2a 2a 20 77 69 74 68 20 69 74 73 20 70 61 72 65  ** with its pare
2253c 6e 74 20 74 6f 20 66 6f 72 6d 20 61 20 73 69 6e  nt to form a sin
2253d 67 6c 65 20 66 6c 61 74 20 71 75 65 72 79 2e 20  gle flat query. 
2253e 20 49 6e 20 73 6f 20 64 6f 69 6e 67 2c 20 69 74   In so doing, it
2253f 20 6d 69 67 68 74 0a 2a 2a 20 63 68 61 6e 67 65   might.** change
22540 20 74 68 65 20 70 61 72 65 6e 74 20 71 75 65 72   the parent quer
22541 79 20 66 72 6f 6d 20 61 20 6e 6f 6e 2d 61 67 67  y from a non-agg
22542 72 65 67 61 74 65 20 74 6f 20 61 6e 20 61 67 67  regate to an agg
22543 72 65 67 61 74 65 20 71 75 65 72 79 2e 0a 2a 2a  regate query..**
22544 20 46 6f 72 20 74 68 61 74 20 72 65 61 73 6f 6e   For that reason
22545 2c 20 74 68 65 20 70 50 61 72 65 6e 74 41 67 67  , the pParentAgg
22546 20 66 6c 61 67 20 69 73 20 70 61 73 73 65 64 20   flag is passed 
22547 61 73 20 61 20 70 6f 69 6e 74 65 72 2c 20 73 6f  as a pointer, so
22548 20 69 74 0a 2a 2a 20 63 61 6e 20 62 65 20 63 68   it.** can be ch
22549 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 45 78 61  anged..**.** Exa
2254a 6d 70 6c 65 20 31 3a 20 20 20 54 68 65 20 6d 65  mple 1:   The me
2254b 61 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 50 61  aning of the pPa
2254c 72 65 6e 74 20 70 61 72 61 6d 65 74 65 72 2e 0a  rent parameter..
2254d 2a 2a 0a 2a 2a 20 20 20 20 53 45 4c 45 43 54 20  **.**    SELECT 
2254e 2a 20 46 52 4f 4d 20 74 31 20 4a 4f 49 4e 20 28  * FROM t1 JOIN (
2254f 53 45 4c 45 43 54 20 78 2c 20 63 6f 75 6e 74 28  SELECT x, count(
22550 2a 29 20 46 52 4f 4d 20 74 32 29 20 4a 4f 49 4e  *) FROM t2) JOIN
22551 20 74 33 3b 0a 2a 2a 20 20 20 20 5c 20 20 20 20   t3;.**    \    
22552 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22553 20 20 5c 5f 5f 5f 5f 5f 5f 5f 20 73 75 62 71 75    \_______ subqu
22554 65 72 79 20 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20  ery _______/    
22555 20 20 20 20 2f 0a 2a 2a 20 20 20 20 20 5c 20 20      /.**     \  
22556 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22557 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22558 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22559 20 20 20 20 2f 0a 2a 2a 20 20 20 20 20 20 5c 5f      /.**      \_
2255a 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
2255b 5f 5f 5f 20 6f 75 74 65 72 20 71 75 65 72 79 20  ___ outer query 
2255c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
2255d 5f 5f 5f 2f 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ___/.**.** This 
2255e 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
2255f 64 20 66 6f 72 20 74 68 65 20 6f 75 74 65 72 20  d for the outer 
22560 71 75 65 72 79 20 66 69 72 73 74 2e 20 20 20 46  query first.   F
22561 6f 72 20 74 68 61 74 20 63 61 6c 6c 2c 0a 2a 2a  or that call,.**
22562 20 70 50 61 72 65 6e 74 20 77 69 6c 6c 20 62 65   pParent will be
22563 20 4e 55 4c 4c 2e 20 20 44 75 72 69 6e 67 20 74   NULL.  During t
22564 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 66  he processing of
22565 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
22566 2c 20 74 68 69 73 20 0a 2a 2a 20 72 6f 75 74 69  , this .** routi
22567 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 72 65 63  ne is called rec
22568 75 72 73 69 76 65 6c 79 20 74 6f 20 68 61 6e 64  ursively to hand
22569 6c 65 20 74 68 65 20 73 75 62 71 75 65 72 79 2e  le the subquery.
2256a 20 20 46 6f 72 20 74 68 65 20 72 65 63 75 72 73    For the recurs
2256b 69 76 65 0a 2a 2a 20 63 61 6c 6c 2c 20 70 50 61  ive.** call, pPa
2256c 72 65 6e 74 20 77 69 6c 6c 20 70 6f 69 6e 74 20  rent will point 
2256d 74 6f 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  to the outer que
2256e 72 79 2e 20 20 42 65 63 61 75 73 65 20 74 68 65  ry.  Because the
2256f 20 73 75 62 71 75 65 72 79 20 69 73 0a 2a 2a 20   subquery is.** 
22570 74 68 65 20 73 65 63 6f 6e 64 20 65 6c 65 6d 65  the second eleme
22571 6e 74 20 69 6e 20 61 20 74 68 72 65 65 2d 77 61  nt in a three-wa
22572 79 20 6a 6f 69 6e 2c 20 74 68 65 20 70 61 72 65  y join, the pare
22573 6e 74 54 61 62 20 70 61 72 61 6d 65 74 65 72 20  ntTab parameter 
22574 77 69 6c 6c 0a 2a 2a 20 62 65 20 31 20 28 74 68  will.** be 1 (th
22575 65 20 32 6e 64 20 76 61 6c 75 65 20 6f 66 20 61  e 2nd value of a
22576 20 30 2d 69 6e 64 65 78 65 64 20 61 72 72 61 79   0-indexed array
22577 2e 29 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  .).*/.SQLITE_PRI
22578 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
22579 53 65 6c 65 63 74 28 0a 20 20 50 61 72 73 65 20  Select(.  Parse 
2257a 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2257b 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
2257c 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65  ontext */.  Sele
2257d 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  ct *p,          
2257e 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54     /* The SELECT
2257f 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67   statement being
22580 20 63 6f 64 65 64 2e 20 2a 2f 0a 20 20 53 65 6c   coded. */.  Sel
22581 65 63 74 44 65 73 74 20 2a 70 44 65 73 74 2c 20  ectDest *pDest, 
22582 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64      /* What to d
22583 6f 20 77 69 74 68 20 74 68 65 20 71 75 65 72 79  o with the query
22584 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 53 65   results */.  Se
22585 6c 65 63 74 20 2a 70 50 61 72 65 6e 74 2c 20 20  lect *pParent,  
22586 20 20 20 20 20 2f 2a 20 41 6e 6f 74 68 65 72 20       /* Another 
22587 53 45 4c 45 43 54 20 66 6f 72 20 77 68 69 63 68  SELECT for which
22588 20 74 68 69 73 20 69 73 20 61 20 73 75 62 2d 71   this is a sub-q
22589 75 65 72 79 20 2a 2f 0a 20 20 69 6e 74 20 70 61  uery */.  int pa
2258a 72 65 6e 74 54 61 62 2c 20 20 20 20 20 20 20 20  rentTab,        
2258b 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 70 50 61   /* Index in pPa
2258c 72 65 6e 74 2d 3e 70 53 72 63 20 6f 66 20 74 68  rent->pSrc of th
2258d 69 73 20 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e  is query */.  in
2258e 74 20 2a 70 50 61 72 65 6e 74 41 67 67 2c 20 20  t *pParentAgg,  
2258f 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
22590 70 50 61 72 65 6e 74 20 75 73 65 73 20 61 67 67  pParent uses agg
22591 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  regate functions
22592 20 2a 2f 0a 20 20 63 68 61 72 20 2a 61 66 66 20   */.  char *aff 
22593 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
22594 49 66 20 65 44 65 73 74 20 69 73 20 53 52 54 5f  If eDest is SRT_
22595 55 6e 69 6f 6e 2c 20 74 68 65 20 61 66 66 69 6e  Union, the affin
22596 69 74 79 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b  ity string */.){
22597 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20  .  int i, j;    
22598 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
22599 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20  p counters */.  
2259a 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66  WhereInfo *pWInf
2259b 6f 3b 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e  o;     /* Return
2259c 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 57 68 65   from sqlite3Whe
2259d 72 65 42 65 67 69 6e 28 29 20 2a 2f 0a 20 20 56  reBegin() */.  V
2259e 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20  dbe *v;         
2259f 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72        /* The vir
225a0 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 75 6e 64  tual machine und
225a1 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20  er construction 
225a2 2a 2f 0a 20 20 69 6e 74 20 69 73 41 67 67 3b 20  */.  int isAgg; 
225a3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
225a4 72 75 65 20 66 6f 72 20 73 65 6c 65 63 74 20 6c  rue for select l
225a5 69 73 74 73 20 6c 69 6b 65 20 22 63 6f 75 6e 74  ists like "count
225a6 28 2a 29 22 20 2a 2f 0a 20 20 45 78 70 72 4c 69  (*)" */.  ExprLi
225a7 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20  st *pEList;     
225a8 20 2f 2a 20 4c 69 73 74 20 6f 66 20 63 6f 6c 75   /* List of colu
225a9 6d 6e 73 20 74 6f 20 65 78 74 72 61 63 74 2e 20  mns to extract. 
225aa 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54  */.  SrcList *pT
225ab 61 62 4c 69 73 74 3b 20 20 20 20 20 2f 2a 20 4c  abList;     /* L
225ac 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20 74 6f  ist of tables to
225ad 20 73 65 6c 65 63 74 20 66 72 6f 6d 20 2a 2f 0a   select from */.
225ae 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20    Expr *pWhere; 
225af 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
225b0 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 4d  WHERE clause.  M
225b1 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20  ay be NULL */.  
225b2 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
225b3 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 4f 52  By;    /* The OR
225b4 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e 20 20  DER BY clause.  
225b5 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20  May be NULL */. 
225b6 20 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f 75   ExprList *pGrou
225b7 70 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 47  pBy;    /* The G
225b8 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e 20  ROUP BY clause. 
225b9 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a   May be NULL */.
225ba 20 20 45 78 70 72 20 2a 70 48 61 76 69 6e 67 3b    Expr *pHaving;
225bb 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
225bc 48 41 56 49 4e 47 20 63 6c 61 75 73 65 2e 20 20  HAVING clause.  
225bd 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20  May be NULL */. 
225be 20 69 6e 74 20 69 73 44 69 73 74 69 6e 63 74 3b   int isDistinct;
225bf 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
225c0 69 66 20 74 68 65 20 44 49 53 54 49 4e 43 54 20  if the DISTINCT 
225c1 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73 65  keyword is prese
225c2 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 64 69 73 74  nt */.  int dist
225c3 69 6e 63 74 3b 20 20 20 20 20 20 20 20 20 20 2f  inct;          /
225c4 2a 20 54 61 62 6c 65 20 74 6f 20 75 73 65 20 66  * Table to use f
225c5 6f 72 20 74 68 65 20 64 69 73 74 69 6e 63 74 20  or the distinct 
225c6 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20  set */.  int rc 
225c7 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 1;            
225c8 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75  /* Value to retu
225c9 72 6e 20 66 72 6f 6d 20 74 68 69 73 20 66 75 6e  rn from this fun
225ca 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 61  ction */.  int a
225cb 64 64 72 53 6f 72 74 49 6e 64 65 78 3b 20 20 20  ddrSortIndex;   
225cc 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
225cd 61 6e 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  an OP_OpenEpheme
225ce 72 61 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ral instruction 
225cf 2a 2f 0a 20 20 41 67 67 49 6e 66 6f 20 73 41 67  */.  AggInfo sAg
225d0 67 49 6e 66 6f 3b 20 20 20 20 20 20 2f 2a 20 49  gInfo;      /* I
225d1 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20  nformation used 
225d2 62 79 20 61 67 67 72 65 67 61 74 65 20 71 75 65  by aggregate que
225d3 72 69 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 45  ries */.  int iE
225d4 6e 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nd;             
225d5 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74   /* Address of t
225d6 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 71 75  he end of the qu
225d7 65 72 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ery */.  sqlite3
225d8 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20   *db;           
225d9 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
225da 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20  connection */.. 
225db 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62   db = pParse->db
225dc 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20  ;.  if( p==0 || 
225dd 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
225de 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72   || pParse->nErr
225df 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31   ){.    return 1
225e0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69  ;.  }.  if( sqli
225e1 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
225e2 72 73 65 2c 20 53 51 4c 49 54 45 5f 53 45 4c 45  rse, SQLITE_SELE
225e3 43 54 2c 20 30 2c 20 30 2c 20 30 29 20 29 20 72  CT, 0, 0, 0) ) r
225e4 65 74 75 72 6e 20 31 3b 0a 20 20 6d 65 6d 73 65  eturn 1;.  memse
225e5 74 28 26 73 41 67 67 49 6e 66 6f 2c 20 30 2c 20  t(&sAggInfo, 0, 
225e6 73 69 7a 65 6f 66 28 73 41 67 67 49 6e 66 6f 29  sizeof(sAggInfo)
225e7 29 3b 0a 0a 20 20 70 4f 72 64 65 72 42 79 20 3d  );..  pOrderBy =
225e8 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20   p->pOrderBy;.  
225e9 69 66 28 20 49 67 6e 6f 72 61 62 6c 65 4f 72 64  if( IgnorableOrd
225ea 65 72 62 79 28 70 44 65 73 74 29 20 29 7b 0a 20  erby(pDest) ){. 
225eb 20 20 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d     p->pOrderBy =
225ec 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 20 74   0;..    /* In t
225ed 68 65 73 65 20 63 61 73 65 73 20 74 68 65 20 44  hese cases the D
225ee 49 53 54 49 4e 43 54 20 6f 70 65 72 61 74 6f 72  ISTINCT operator
225ef 20 6d 61 6b 65 73 20 6e 6f 20 64 69 66 66 65 72   makes no differ
225f0 65 6e 63 65 20 74 6f 20 74 68 65 0a 20 20 20 20  ence to the.    
225f1 2a 2a 20 72 65 73 75 6c 74 73 2c 20 73 6f 20 72  ** results, so r
225f2 65 6d 6f 76 65 20 69 74 20 69 66 20 69 74 20 77  emove it if it w
225f3 65 72 65 20 73 70 65 63 69 66 69 65 64 2e 0a 20  ere specified.. 
225f4 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
225f5 28 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53  (pDest->eDest==S
225f6 52 54 5f 45 78 69 73 74 73 20 7c 7c 20 70 44 65  RT_Exists || pDe
225f7 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 55  st->eDest==SRT_U
225f8 6e 69 6f 6e 20 7c 7c 20 0a 20 20 20 20 20 20 20  nion || .       
225f9 20 20 20 20 70 44 65 73 74 2d 3e 65 44 65 73 74      pDest->eDest
225fa 3d 3d 53 52 54 5f 45 78 63 65 70 74 20 7c 7c 20  ==SRT_Except || 
225fb 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52  pDest->eDest==SR
225fc 54 5f 44 69 73 63 61 72 64 29 3b 0a 20 20 20 20  T_Discard);.    
225fd 70 2d 3e 69 73 44 69 73 74 69 6e 63 74 20 3d 20  p->isDistinct = 
225fe 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c  0;.  }.  if( sql
225ff 69 74 65 33 53 65 6c 65 63 74 52 65 73 6f 6c 76  ite3SelectResolv
22600 65 28 70 50 61 72 73 65 2c 20 70 2c 20 30 29 20  e(pParse, p, 0) 
22601 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 65 6c 65  ){.    goto sele
22602 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20 70 2d  ct_end;.  }.  p-
22603 3e 70 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64  >pOrderBy = pOrd
22604 65 72 42 79 3b 0a 0a 23 69 66 6e 64 65 66 20 53  erBy;..#ifndef S
22605 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f  QLITE_OMIT_COMPO
22606 55 4e 44 5f 53 45 4c 45 43 54 0a 20 20 2f 2a 20  UND_SELECT.  /* 
22607 49 66 20 74 68 65 72 65 20 69 73 20 61 72 65 20  If there is are 
22608 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20 71 75  a sequence of qu
22609 65 72 69 65 73 2c 20 64 6f 20 74 68 65 20 65 61  eries, do the ea
2260a 72 6c 69 65 72 20 6f 6e 65 73 20 66 69 72 73 74  rlier ones first
2260b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  ..  */.  if( p->
2260c 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20 69 66  pPrior ){.    if
2260d 28 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 3d  ( p->pRightmost=
2260e 3d 30 20 29 7b 0a 20 20 20 20 20 20 53 65 6c 65  =0 ){.      Sele
2260f 63 74 20 2a 70 4c 6f 6f 70 2c 20 2a 70 52 69 67  ct *pLoop, *pRig
22610 68 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e  ht = 0;.      in
22611 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20  t cnt = 0;.     
22612 20 69 6e 74 20 6d 78 53 65 6c 65 63 74 3b 0a 20   int mxSelect;. 
22613 20 20 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d 70       for(pLoop=p
22614 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70  ; pLoop; pLoop=p
22615 4c 6f 6f 70 2d 3e 70 50 72 69 6f 72 2c 20 63 6e  Loop->pPrior, cn
22616 74 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 4c  t++){.        pL
22617 6f 6f 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20  oop->pRightmost 
22618 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 70 4c 6f  = p;.        pLo
22619 6f 70 2d 3e 70 4e 65 78 74 20 3d 20 70 52 69 67  op->pNext = pRig
2261a 68 74 3b 0a 20 20 20 20 20 20 20 20 70 52 69 67  ht;.        pRig
2261b 68 74 20 3d 20 70 4c 6f 6f 70 3b 0a 20 20 20 20  ht = pLoop;.    
2261c 20 20 7d 0a 20 20 20 20 20 20 6d 78 53 65 6c 65    }.      mxSele
2261d 63 74 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74 5b  ct = db->aLimit[
2261e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d  SQLITE_LIMIT_COM
2261f 50 4f 55 4e 44 5f 53 45 4c 45 43 54 5d 3b 0a 20  POUND_SELECT];. 
22620 20 20 20 20 20 69 66 28 20 6d 78 53 65 6c 65 63       if( mxSelec
22621 74 20 26 26 20 63 6e 74 3e 6d 78 53 65 6c 65 63  t && cnt>mxSelec
22622 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  t ){.        sql
22623 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
22624 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 74  rse, "too many t
22625 65 72 6d 73 20 69 6e 20 63 6f 6d 70 6f 75 6e 64  erms in compound
22626 20 53 45 4c 45 43 54 22 29 3b 0a 20 20 20 20 20   SELECT");.     
22627 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
22628 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 72     }.    }.    r
22629 65 74 75 72 6e 20 6d 75 6c 74 69 53 65 6c 65 63  eturn multiSelec
2262a 74 28 70 50 61 72 73 65 2c 20 70 2c 20 70 44 65  t(pParse, p, pDe
2262b 73 74 2c 20 61 66 66 29 3b 0a 20 20 7d 0a 23 65  st, aff);.  }.#e
2262c 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20  ndif..  /* Make 
2262d 6c 6f 63 61 6c 20 63 6f 70 69 65 73 20 6f 66 20  local copies of 
2262e 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 66  the parameters f
2262f 6f 72 20 74 68 69 73 20 71 75 65 72 79 2e 0a 20  or this query.. 
22630 20 2a 2f 0a 20 20 70 54 61 62 4c 69 73 74 20 3d   */.  pTabList =
22631 20 70 2d 3e 70 53 72 63 3b 0a 20 20 70 57 68 65   p->pSrc;.  pWhe
22632 72 65 20 3d 20 70 2d 3e 70 57 68 65 72 65 3b 0a  re = p->pWhere;.
22633 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70 2d 3e    pGroupBy = p->
22634 70 47 72 6f 75 70 42 79 3b 0a 20 20 70 48 61 76  pGroupBy;.  pHav
22635 69 6e 67 20 3d 20 70 2d 3e 70 48 61 76 69 6e 67  ing = p->pHaving
22636 3b 0a 20 20 69 73 41 67 67 20 3d 20 70 2d 3e 69  ;.  isAgg = p->i
22637 73 41 67 67 3b 0a 20 20 69 73 44 69 73 74 69 6e  sAgg;.  isDistin
22638 63 74 20 3d 20 70 2d 3e 69 73 44 69 73 74 69 6e  ct = p->isDistin
22639 63 74 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 70  ct;.  pEList = p
2263a 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 69 66 28 20  ->pEList;.  if( 
2263b 70 45 4c 69 73 74 3d 3d 30 20 29 20 67 6f 74 6f  pEList==0 ) goto
2263c 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20 20   select_end;..  
2263d 2f 2a 20 0a 20 20 2a 2a 20 44 6f 20 6e 6f 74 20  /* .  ** Do not 
2263e 65 76 65 6e 20 61 74 74 65 6d 70 74 20 74 6f 20  even attempt to 
2263f 67 65 6e 65 72 61 74 65 20 61 6e 79 20 63 6f 64  generate any cod
22640 65 20 69 66 20 77 65 20 68 61 76 65 20 61 6c 72  e if we have alr
22641 65 61 64 79 20 73 65 65 6e 0a 20 20 2a 2a 20 65  eady seen.  ** e
22642 72 72 6f 72 73 20 62 65 66 6f 72 65 20 74 68 69  rrors before thi
22643 73 20 72 6f 75 74 69 6e 65 20 73 74 61 72 74 73  s routine starts
22644 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61  ..  */.  if( pPa
22645 72 73 65 2d 3e 6e 45 72 72 3e 30 20 29 20 67 6f  rse->nErr>0 ) go
22646 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a  to select_end;..
22647 20 20 2f 2a 20 49 66 20 77 72 69 74 69 6e 67 20    /* If writing 
22648 74 6f 20 6d 65 6d 6f 72 79 20 6f 72 20 67 65 6e  to memory or gen
22649 65 72 61 74 69 6e 67 20 61 20 73 65 74 0a 20 20  erating a set.  
2264a 2a 2a 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65  ** only a single
2264b 20 63 6f 6c 75 6d 6e 20 6d 61 79 20 62 65 20 6f   column may be o
2264c 75 74 70 75 74 2e 0a 20 20 2a 2f 0a 23 69 66 6e  utput..  */.#ifn
2264d 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2264e 53 55 42 51 55 45 52 59 0a 20 20 69 66 28 20 63  SUBQUERY.  if( c
2264f 68 65 63 6b 46 6f 72 4d 75 6c 74 69 43 6f 6c 75  heckForMultiColu
22650 6d 6e 53 65 6c 65 63 74 45 72 72 6f 72 28 70 50  mnSelectError(pP
22651 61 72 73 65 2c 20 70 44 65 73 74 2c 20 70 45 4c  arse, pDest, pEL
22652 69 73 74 2d 3e 6e 45 78 70 72 29 20 29 7b 0a 20  ist->nExpr) ){. 
22653 20 20 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65     goto select_e
22654 6e 64 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  nd;.  }.#endif..
22655 20 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 69 73    /* ORDER BY is
22656 20 69 67 6e 6f 72 65 64 20 66 6f 72 20 73 6f 6d   ignored for som
22657 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 73 2e 0a  e destinations..
22658 20 20 2a 2f 0a 20 20 69 66 28 20 49 67 6e 6f 72    */.  if( Ignor
22659 61 62 6c 65 4f 72 64 65 72 62 79 28 70 44 65 73  ableOrderby(pDes
2265a 74 29 20 29 7b 0a 20 20 20 20 70 4f 72 64 65 72  t) ){.    pOrder
2265b 42 79 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  By = 0;.  }..  /
2265c 2a 20 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69  * Begin generati
2265d 6e 67 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20  ng code..  */.  
2265e 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
2265f 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
22660 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20 73 65  ( v==0 ) goto se
22661 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20 20 2f 2a 20  lect_end;..  /* 
22662 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f  Generate code fo
22663 72 20 61 6c 6c 20 73 75 62 2d 71 75 65 72 69 65  r all sub-querie
22664 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  s in the FROM cl
22665 61 75 73 65 0a 20 20 2a 2f 0a 23 69 66 20 21 64  ause.  */.#if !d
22666 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
22667 49 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c 20  IT_SUBQUERY) || 
22668 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
22669 4f 4d 49 54 5f 56 49 45 57 29 0a 20 20 66 6f 72  OMIT_VIEW).  for
2266a 28 69 3d 30 3b 20 69 3c 70 54 61 62 4c 69 73 74  (i=0; i<pTabList
2266b 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20  ->nSrc; i++){.  
2266c 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
2266d 61 76 65 64 41 75 74 68 43 6f 6e 74 65 78 74 20  avedAuthContext 
2266e 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 6e 65 65  = 0;.    int nee
2266f 64 52 65 73 74 6f 72 65 43 6f 6e 74 65 78 74 3b  dRestoreContext;
22670 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c  .    struct SrcL
22671 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20  ist_item *pItem 
22672 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69  = &pTabList->a[i
22673 5d 3b 0a 20 20 20 20 53 65 6c 65 63 74 44 65 73  ];.    SelectDes
22674 74 20 64 65 73 74 3b 0a 0a 20 20 20 20 69 66 28  t dest;..    if(
22675 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 3d   pItem->pSelect=
22676 3d 30 20 7c 7c 20 70 49 74 65 6d 2d 3e 69 73 50  =0 || pItem->isP
22677 6f 70 75 6c 61 74 65 64 20 29 20 63 6f 6e 74 69  opulated ) conti
22678 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 70 49 74  nue;.    if( pIt
22679 65 6d 2d 3e 7a 4e 61 6d 65 21 3d 30 20 29 7b 0a  em->zName!=0 ){.
2267a 20 20 20 20 20 20 7a 53 61 76 65 64 41 75 74 68        zSavedAuth
2267b 43 6f 6e 74 65 78 74 20 3d 20 70 50 61 72 73 65  Context = pParse
2267c 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a  ->zAuthContext;.
2267d 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 41        pParse->zA
2267e 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 49 74  uthContext = pIt
2267f 65 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  em->zName;.     
22680 20 6e 65 65 64 52 65 73 74 6f 72 65 43 6f 6e 74   needRestoreCont
22681 65 78 74 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c  ext = 1;.    }el
22682 73 65 7b 0a 20 20 20 20 20 20 6e 65 65 64 52 65  se{.      needRe
22683 73 74 6f 72 65 43 6f 6e 74 65 78 74 20 3d 20 30  storeContext = 0
22684 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 49  ;.    }.    /* I
22685 6e 63 72 65 6d 65 6e 74 20 50 61 72 73 65 2e 6e  ncrement Parse.n
22686 48 65 69 67 68 74 20 62 79 20 74 68 65 20 68 65  Height by the he
22687 69 67 68 74 20 6f 66 20 74 68 65 20 6c 61 72 67  ight of the larg
22688 65 73 74 20 65 78 70 72 65 73 73 69 6f 6e 0a 20  est expression. 
22689 20 20 20 2a 2a 20 74 72 65 65 20 72 65 66 65 72     ** tree refer
2268a 65 64 20 74 6f 20 62 79 20 74 68 69 73 2c 20 74  ed to by this, t
2268b 68 65 20 70 61 72 65 6e 74 20 73 65 6c 65 63 74  he parent select
2268c 2e 20 54 68 65 20 63 68 69 6c 64 20 73 65 6c 65  . The child sele
2268d 63 74 0a 20 20 20 20 2a 2a 20 6d 61 79 20 63 6f  ct.    ** may co
2268e 6e 74 61 69 6e 20 65 78 70 72 65 73 73 69 6f 6e  ntain expression
2268f 20 74 72 65 65 73 20 6f 66 20 61 74 20 6d 6f 73   trees of at mos
22690 74 0a 20 20 20 20 2a 2a 20 28 53 51 4c 49 54 45  t.    ** (SQLITE
22691 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 2d  _MAX_EXPR_DEPTH-
22692 50 61 72 73 65 2e 6e 48 65 69 67 68 74 29 20 68  Parse.nHeight) h
22693 65 69 67 68 74 2e 20 54 68 69 73 20 69 73 20 61  eight. This is a
22694 20 62 69 74 0a 20 20 20 20 2a 2a 20 6d 6f 72 65   bit.    ** more
22695 20 63 6f 6e 73 65 72 76 61 74 69 76 65 20 74 68   conservative th
22696 61 6e 20 6e 65 63 65 73 73 61 72 79 2c 20 62 75  an necessary, bu
22697 74 20 6d 75 63 68 20 65 61 73 69 65 72 20 74 68  t much easier th
22698 61 6e 20 65 6e 66 6f 72 63 69 6e 67 0a 20 20 20  an enforcing.   
22699 20 2a 2a 20 61 6e 20 65 78 61 63 74 20 6c 69 6d   ** an exact lim
2269a 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  it..    */.    p
2269b 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2b  Parse->nHeight +
2269c 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45  = sqlite3SelectE
2269d 78 70 72 48 65 69 67 68 74 28 70 29 3b 0a 20 20  xprHeight(p);.  
2269e 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
2269f 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53  estInit(&dest, S
226a0 52 54 5f 45 70 68 65 6d 54 61 62 2c 20 70 49 74  RT_EphemTab, pIt
226a1 65 6d 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20  em->iCursor);.  
226a2 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28    sqlite3Select(
226a3 70 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70  pParse, pItem->p
226a4 53 65 6c 65 63 74 2c 20 26 64 65 73 74 2c 20 70  Select, &dest, p
226a5 2c 20 69 2c 20 26 69 73 41 67 67 2c 20 30 29 3b  , i, &isAgg, 0);
226a6 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  .    if( db->mal
226a7 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
226a8 20 20 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65     goto select_e
226a9 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50  nd;.    }.    pP
226aa 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2d 3d  arse->nHeight -=
226ab 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78   sqlite3SelectEx
226ac 70 72 48 65 69 67 68 74 28 70 29 3b 0a 20 20 20  prHeight(p);.   
226ad 20 69 66 28 20 6e 65 65 64 52 65 73 74 6f 72 65   if( needRestore
226ae 43 6f 6e 74 65 78 74 20 29 7b 0a 20 20 20 20 20  Context ){.     
226af 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f   pParse->zAuthCo
226b0 6e 74 65 78 74 20 3d 20 7a 53 61 76 65 64 41 75  ntext = zSavedAu
226b1 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 7d  thContext;.    }
226b2 0a 20 20 20 20 70 54 61 62 4c 69 73 74 20 3d 20  .    pTabList = 
226b3 70 2d 3e 70 53 72 63 3b 0a 20 20 20 20 70 57 68  p->pSrc;.    pWh
226b4 65 72 65 20 3d 20 70 2d 3e 70 57 68 65 72 65 3b  ere = p->pWhere;
226b5 0a 20 20 20 20 69 66 28 20 21 49 67 6e 6f 72 61  .    if( !Ignora
226b6 62 6c 65 4f 72 64 65 72 62 79 28 70 44 65 73 74  bleOrderby(pDest
226b7 29 20 29 7b 0a 20 20 20 20 20 20 70 4f 72 64 65  ) ){.      pOrde
226b8 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72 42  rBy = p->pOrderB
226b9 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 47 72  y;.    }.    pGr
226ba 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75  oupBy = p->pGrou
226bb 70 42 79 3b 0a 20 20 20 20 70 48 61 76 69 6e 67  pBy;.    pHaving
226bc 20 3d 20 70 2d 3e 70 48 61 76 69 6e 67 3b 0a 20   = p->pHaving;. 
226bd 20 20 20 69 73 44 69 73 74 69 6e 63 74 20 3d 20     isDistinct = 
226be 70 2d 3e 69 73 44 69 73 74 69 6e 63 74 3b 0a 20  p->isDistinct;. 
226bf 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
226c0 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
226c1 74 68 69 73 20 69 73 20 61 20 73 75 62 71 75 65  this is a subque
226c2 72 79 20 74 68 61 74 20 63 61 6e 20 62 65 20 22  ry that can be "
226c3 66 6c 61 74 74 65 6e 65 64 22 20 69 6e 74 6f 20  flattened" into 
226c4 69 74 73 20 70 61 72 65 6e 74 2e 0a 20 20 2a 2a  its parent..  **
226c5 20 49 66 20 66 6c 61 74 74 65 6e 69 6e 67 20 69   If flattening i
226c6 73 20 61 20 70 6f 73 73 69 62 6c 69 74 79 2c 20  s a possiblity, 
226c7 64 6f 20 73 6f 20 61 6e 64 20 72 65 74 75 72 6e  do so and return
226c8 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 20 0a   immediately.  .
226c9 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c    */.#ifndef SQL
226ca 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20  ITE_OMIT_VIEW.  
226cb 69 66 28 20 70 50 61 72 65 6e 74 20 26 26 20 70  if( pParent && p
226cc 50 61 72 65 6e 74 41 67 67 20 26 26 0a 20 20 20  ParentAgg &&.   
226cd 20 20 20 66 6c 61 74 74 65 6e 53 75 62 71 75 65     flattenSubque
226ce 72 79 28 64 62 2c 20 70 50 61 72 65 6e 74 2c 20  ry(db, pParent, 
226cf 70 61 72 65 6e 74 54 61 62 2c 20 2a 70 50 61 72  parentTab, *pPar
226d0 65 6e 74 41 67 67 2c 20 69 73 41 67 67 29 20 29  entAgg, isAgg) )
226d1 7b 0a 20 20 20 20 69 66 28 20 69 73 41 67 67 20  {.    if( isAgg 
226d2 29 20 2a 70 50 61 72 65 6e 74 41 67 67 20 3d 20  ) *pParentAgg = 
226d3 31 3b 0a 20 20 20 20 67 6f 74 6f 20 73 65 6c 65  1;.    goto sele
226d4 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 23 65 6e 64  ct_end;.  }.#end
226d5 69 66 0a 0a 20 20 2f 2a 20 49 66 20 70 6f 73 73  if..  /* If poss
226d6 69 62 6c 65 2c 20 72 65 77 72 69 74 65 20 74 68  ible, rewrite th
226d7 65 20 71 75 65 72 79 20 74 6f 20 75 73 65 20 47  e query to use G
226d8 52 4f 55 50 20 42 59 20 69 6e 73 74 65 61 64 20  ROUP BY instead 
226d9 6f 66 20 44 49 53 54 49 4e 43 54 2e 0a 20 20 2a  of DISTINCT..  *
226da 2a 20 47 52 4f 55 50 20 42 59 20 6d 61 79 20 75  * GROUP BY may u
226db 73 65 20 61 6e 20 69 6e 64 65 78 2c 20 44 49 53  se an index, DIS
226dc 54 49 4e 43 54 20 6e 65 76 65 72 20 64 6f 65 73  TINCT never does
226dd 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  ..  */.  if( p->
226de 69 73 44 69 73 74 69 6e 63 74 20 26 26 20 21 70  isDistinct && !p
226df 2d 3e 69 73 41 67 67 20 26 26 20 21 70 2d 3e 70  ->isAgg && !p->p
226e0 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 70  GroupBy ){.    p
226e1 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20 73 71 6c  ->pGroupBy = sql
226e2 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
226e3 64 62 2c 20 70 2d 3e 70 45 4c 69 73 74 29 3b 0a  db, p->pEList);.
226e4 20 20 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70      pGroupBy = p
226e5 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20 20 20  ->pGroupBy;.    
226e6 70 2d 3e 69 73 44 69 73 74 69 6e 63 74 20 3d 20  p->isDistinct = 
226e7 30 3b 0a 20 20 20 20 69 73 44 69 73 74 69 6e 63  0;.    isDistinc
226e8 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  t = 0;.  }..  /*
226e9 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6e 20   If there is an 
226ea 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c  ORDER BY clause,
226eb 20 74 68 65 6e 20 74 68 69 73 20 73 6f 72 74 69   then this sorti
226ec 6e 67 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6d 69  ng.  ** index mi
226ed 67 68 74 20 65 6e 64 20 75 70 20 62 65 69 6e 67  ght end up being
226ee 20 75 6e 75 73 65 64 20 69 66 20 74 68 65 20 64   unused if the d
226ef 61 74 61 20 63 61 6e 20 62 65 20 0a 20 20 2a 2a  ata can be .  **
226f0 20 65 78 74 72 61 63 74 65 64 20 69 6e 20 70 72   extracted in pr
226f1 65 2d 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 20  e-sorted order. 
226f2 20 49 66 20 74 68 61 74 20 69 73 20 74 68 65 20   If that is the 
226f3 63 61 73 65 2c 20 74 68 65 6e 20 74 68 65 0a 20  case, then the. 
226f4 20 2a 2a 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d   ** OP_OpenEphem
226f5 65 72 61 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e  eral instruction
226f6 20 77 69 6c 6c 20 62 65 20 63 68 61 6e 67 65 64   will be changed
226f7 20 74 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70 20 6f   to an OP_Noop o
226f8 6e 63 65 0a 20 20 2a 2a 20 77 65 20 66 69 67 75  nce.  ** we figu
226f9 72 65 20 6f 75 74 20 74 68 61 74 20 74 68 65 20  re out that the 
226fa 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 69 73  sorting index is
226fb 20 6e 6f 74 20 6e 65 65 64 65 64 2e 20 20 54 68   not needed.  Th
226fc 65 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78 0a  e addrSortIndex.
226fd 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 20 69 73    ** variable is
226fe 20 75 73 65 64 20 74 6f 20 66 61 63 69 6c 69 74   used to facilit
226ff 61 74 65 20 74 68 61 74 20 63 68 61 6e 67 65 2e  ate that change.
22700 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4f 72 64  .  */.  if( pOrd
22701 65 72 42 79 20 29 7b 0a 20 20 20 20 4b 65 79 49  erBy ){.    KeyI
22702 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20  nfo *pKeyInfo;. 
22703 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 6b 65     pKeyInfo = ke
22704 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73  yInfoFromExprLis
22705 74 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72  t(pParse, pOrder
22706 42 79 29 3b 0a 20 20 20 20 70 4f 72 64 65 72 42  By);.    pOrderB
22707 79 2d 3e 69 45 43 75 72 73 6f 72 20 3d 20 70 50  y->iECursor = pP
22708 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
22709 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68    p->addrOpenEph
2270a 6d 5b 32 5d 20 3d 20 61 64 64 72 53 6f 72 74 49  m[2] = addrSortI
2270b 6e 64 65 78 20 3d 0a 20 20 20 20 20 20 73 71 6c  ndex =.      sql
2270c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2270d 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  , OP_OpenEphemer
2270e 61 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  al,.            
2270f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
22710 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f  OrderBy->iECurso
22711 72 2c 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78  r, pOrderBy->nEx
22712 70 72 2b 32 2c 20 30 2c 0a 20 20 20 20 20 20 20  pr+2, 0,.       
22713 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22714 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 49      (char*)pKeyI
22715 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f  nfo, P4_KEYINFO_
22716 48 41 4e 44 4f 46 46 29 3b 0a 20 20 7d 65 6c 73  HANDOFF);.  }els
22717 65 7b 0a 20 20 20 20 61 64 64 72 53 6f 72 74 49  e{.    addrSortI
22718 6e 64 65 78 20 3d 20 2d 31 3b 0a 20 20 7d 0a 0a  ndex = -1;.  }..
22719 20 20 2f 2a 20 49 66 20 74 68 65 20 6f 75 74 70    /* If the outp
2271a 75 74 20 69 73 20 64 65 73 74 69 6e 65 64 20 66  ut is destined f
2271b 6f 72 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74  or a temporary t
2271c 61 62 6c 65 2c 20 6f 70 65 6e 20 74 68 61 74 20  able, open that 
2271d 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  table..  */.  if
2271e 28 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d  ( pDest->eDest==
2271f 53 52 54 5f 45 70 68 65 6d 54 61 62 20 29 7b 0a  SRT_EphemTab ){.
22720 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22721 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp2(v, OP_Open
22722 45 70 68 65 6d 65 72 61 6c 2c 20 70 44 65 73 74  Ephemeral, pDest
22723 2d 3e 69 50 61 72 6d 2c 20 70 45 4c 69 73 74 2d  ->iParm, pEList-
22724 3e 6e 45 78 70 72 29 3b 0a 20 20 7d 0a 0a 20 20  >nExpr);.  }..  
22725 2f 2a 20 53 65 74 20 74 68 65 20 6c 69 6d 69 74  /* Set the limit
22726 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 45 6e 64 20  er..  */.  iEnd 
22727 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
22728 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 63 6f 6d  eLabel(v);.  com
22729 70 75 74 65 4c 69 6d 69 74 52 65 67 69 73 74 65  puteLimitRegiste
2272a 72 73 28 70 50 61 72 73 65 2c 20 70 2c 20 69 45  rs(pParse, p, iE
2272b 6e 64 29 3b 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20  nd);..  /* Open 
2272c 61 20 76 69 72 74 75 61 6c 20 69 6e 64 65 78 20  a virtual index 
2272d 74 6f 20 75 73 65 20 66 6f 72 20 74 68 65 20 64  to use for the d
2272e 69 73 74 69 6e 63 74 20 73 65 74 2e 0a 20 20 2a  istinct set..  *
2272f 2f 0a 20 20 69 66 28 20 69 73 44 69 73 74 69 6e  /.  if( isDistin
22730 63 74 20 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66  ct ){.    KeyInf
22731 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20  o *pKeyInfo;.   
22732 20 61 73 73 65 72 74 28 20 69 73 41 67 67 20 7c   assert( isAgg |
22733 7c 20 70 47 72 6f 75 70 42 79 20 29 3b 0a 20 20  | pGroupBy );.  
22734 20 20 64 69 73 74 69 6e 63 74 20 3d 20 70 50 61    distinct = pPa
22735 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20  rse->nTab++;.   
22736 20 70 4b 65 79 49 6e 66 6f 20 3d 20 6b 65 79 49   pKeyInfo = keyI
22737 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28  nfoFromExprList(
22738 70 50 61 72 73 65 2c 20 70 2d 3e 70 45 4c 69 73  pParse, p->pELis
22739 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  t);.    sqlite3V
2273a 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2273b 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 64  OpenEphemeral, d
2273c 69 73 74 69 6e 63 74 2c 20 30 2c 20 30 2c 0a 20  istinct, 0, 0,. 
2273d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2273e 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b         (char*)pK
2273f 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e  eyInfo, P4_KEYIN
22740 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 7d  FO_HANDOFF);.  }
22741 65 6c 73 65 7b 0a 20 20 20 20 64 69 73 74 69 6e  else{.    distin
22742 63 74 20 3d 20 2d 31 3b 0a 20 20 7d 0a 0a 20 20  ct = -1;.  }..  
22743 2f 2a 20 41 67 67 72 65 67 61 74 65 20 61 6e 64  /* Aggregate and
22744 20 6e 6f 6e 2d 61 67 67 72 65 67 61 74 65 20 71   non-aggregate q
22745 75 65 72 69 65 73 20 61 72 65 20 68 61 6e 64 6c  ueries are handl
22746 65 64 20 64 69 66 66 65 72 65 6e 74 6c 79 20 2a  ed differently *
22747 2f 0a 20 20 69 66 28 20 21 69 73 41 67 67 20 26  /.  if( !isAgg &
22748 26 20 70 47 72 6f 75 70 42 79 3d 3d 30 20 29 7b  & pGroupBy==0 ){
22749 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 73  .    /* This cas
2274a 65 20 69 73 20 66 6f 72 20 6e 6f 6e 2d 61 67 67  e is for non-agg
2274b 72 65 67 61 74 65 20 71 75 65 72 69 65 73 0a 20  regate queries. 
2274c 20 20 20 2a 2a 20 42 65 67 69 6e 20 74 68 65 20     ** Begin the 
2274d 64 61 74 61 62 61 73 65 20 73 63 61 6e 0a 20 20  database scan.  
2274e 20 20 2a 2f 0a 20 20 20 20 70 57 49 6e 66 6f 20    */.    pWInfo 
2274f 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  = sqlite3WhereBe
22750 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62  gin(pParse, pTab
22751 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 20 26 70  List, pWhere, &p
22752 4f 72 64 65 72 42 79 2c 20 30 29 3b 0a 20 20 20  OrderBy, 0);.   
22753 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20 29   if( pWInfo==0 )
22754 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64   goto select_end
22755 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 73 6f 72  ;..    /* If sor
22756 74 69 6e 67 20 69 6e 64 65 78 20 74 68 61 74 20  ting index that 
22757 77 61 73 20 63 72 65 61 74 65 64 20 62 79 20 61  was created by a
22758 20 70 72 69 6f 72 20 4f 50 5f 4f 70 65 6e 45 70   prior OP_OpenEp
22759 68 65 6d 65 72 61 6c 20 0a 20 20 20 20 2a 2a 20  hemeral .    ** 
2275a 69 6e 73 74 72 75 63 74 69 6f 6e 20 65 6e 64 65  instruction ende
2275b 64 20 75 70 20 6e 6f 74 20 62 65 69 6e 67 20 6e  d up not being n
2275c 65 65 64 65 64 2c 20 74 68 65 6e 20 63 68 61 6e  eeded, then chan
2275d 67 65 20 74 68 65 20 4f 50 5f 4f 70 65 6e 45 70  ge the OP_OpenEp
2275e 68 65 6d 65 72 61 6c 0a 20 20 20 20 2a 2a 20 69  hemeral.    ** i
2275f 6e 74 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70 2e 0a  nto an OP_Noop..
22760 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 61      */.    if( a
22761 64 64 72 53 6f 72 74 49 6e 64 65 78 3e 3d 30 20  ddrSortIndex>=0 
22762 26 26 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29  && pOrderBy==0 )
22763 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
22764 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28  dbeChangeToNoop(
22765 76 2c 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78  v, addrSortIndex
22766 2c 20 31 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61  , 1);.      p->a
22767 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d 20 3d  ddrOpenEphm[2] =
22768 20 2d 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20   -1;.    }..    
22769 2f 2a 20 55 73 65 20 74 68 65 20 73 74 61 6e 64  /* Use the stand
2276a 61 72 64 20 69 6e 6e 65 72 20 6c 6f 6f 70 0a 20  ard inner loop. 
2276b 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
2276c 28 21 69 73 44 69 73 74 69 6e 63 74 29 3b 0a 20  (!isDistinct);. 
2276d 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f     selectInnerLo
2276e 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70 45  op(pParse, p, pE
2276f 4c 69 73 74 2c 20 30 2c 20 30 2c 20 70 4f 72 64  List, 0, 0, pOrd
22770 65 72 42 79 2c 20 2d 31 2c 20 70 44 65 73 74 2c  erBy, -1, pDest,
22771 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
22772 20 20 20 20 20 70 57 49 6e 66 6f 2d 3e 69 43 6f       pWInfo->iCo
22773 6e 74 69 6e 75 65 2c 20 70 57 49 6e 66 6f 2d 3e  ntinue, pWInfo->
22774 69 42 72 65 61 6b 2c 20 61 66 66 29 3b 0a 0a 20  iBreak, aff);.. 
22775 20 20 20 2f 2a 20 45 6e 64 20 74 68 65 20 64 61     /* End the da
22776 74 61 62 61 73 65 20 73 63 61 6e 20 6c 6f 6f 70  tabase scan loop
22777 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  ..    */.    sql
22778 69 74 65 33 57 68 65 72 65 45 6e 64 28 70 57 49  ite3WhereEnd(pWI
22779 6e 66 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  nfo);.  }else{. 
2277a 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68     /* This is th
2277b 65 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72  e processing for
2277c 20 61 67 67 72 65 67 61 74 65 20 71 75 65 72 69   aggregate queri
2277d 65 73 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f  es */.    NameCo
2277e 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20 2f 2a  ntext sNC;    /*
2277f 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f   Name context fo
22780 72 20 70 72 6f 63 65 73 73 69 6e 67 20 61 67 67  r processing agg
22781 72 65 67 61 74 65 20 69 6e 66 6f 72 6d 61 74 69  regate informati
22782 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 41  on */.    int iA
22783 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Mem;          /*
22784 20 46 69 72 73 74 20 4d 65 6d 20 61 64 64 72 65   First Mem addre
22785 73 73 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 63  ss for storing c
22786 75 72 72 65 6e 74 20 47 52 4f 55 50 20 42 59 20  urrent GROUP BY 
22787 2a 2f 0a 20 20 20 20 69 6e 74 20 69 42 4d 65 6d  */.    int iBMem
22788 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69  ;          /* Fi
22789 72 73 74 20 4d 65 6d 20 61 64 64 72 65 73 73 20  rst Mem address 
2278a 66 6f 72 20 70 72 65 76 69 6f 75 73 20 47 52 4f  for previous GRO
2278b 55 50 20 42 59 20 2a 2f 0a 20 20 20 20 69 6e 74  UP BY */.    int
2278c 20 69 55 73 65 46 6c 61 67 3b 20 20 20 20 20 20   iUseFlag;      
2278d 20 2f 2a 20 4d 65 6d 20 61 64 64 72 65 73 73 20   /* Mem address 
2278e 68 6f 6c 64 69 6e 67 20 66 6c 61 67 20 69 6e 64  holding flag ind
2278f 69 63 61 74 69 6e 67 20 74 68 61 74 20 61 74 20  icating that at 
22790 6c 65 61 73 74 0a 20 20 20 20 20 20 20 20 20 20  least.          
22791 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
22792 20 6f 6e 65 20 72 6f 77 20 6f 66 20 74 68 65 20   one row of the 
22793 69 6e 70 75 74 20 74 6f 20 74 68 65 20 61 67 67  input to the agg
22794 72 65 67 61 74 6f 72 20 68 61 73 20 62 65 65 6e  regator has been
22795 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
22796 20 20 20 20 20 20 20 20 20 2a 2a 20 70 72 6f 63           ** proc
22797 65 73 73 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74  essed */.    int
22798 20 69 41 62 6f 72 74 46 6c 61 67 3b 20 20 20 20   iAbortFlag;    
22799 20 2f 2a 20 4d 65 6d 20 61 64 64 72 65 73 73 20   /* Mem address 
2279a 77 68 69 63 68 20 63 61 75 73 65 73 20 71 75 65  which causes que
2279b 72 79 20 61 62 6f 72 74 20 69 66 20 70 6f 73 69  ry abort if posi
2279c 74 69 76 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20  tive */.    int 
2279d 67 72 6f 75 70 42 79 53 6f 72 74 3b 20 20 20 20  groupBySort;    
2279e 2f 2a 20 52 6f 77 73 20 63 6f 6d 65 20 66 72 6f  /* Rows come fro
2279f 6d 20 73 6f 75 72 63 65 20 69 6e 20 47 52 4f 55  m source in GROU
227a0 50 20 42 59 20 6f 72 64 65 72 20 2a 2f 0a 0a 0a  P BY order */...
227a1 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f      /* The follo
227a2 77 69 6e 67 20 76 61 72 69 61 62 6c 65 73 20 68  wing variables h
227a3 6f 6c 64 20 61 64 64 72 65 73 73 65 73 20 6f 72  old addresses or
227a4 20 6c 61 62 65 6c 73 20 66 6f 72 20 70 61 72 74   labels for part
227a5 73 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20  s of the.    ** 
227a6 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20  virtual machine 
227a7 70 72 6f 67 72 61 6d 20 77 65 20 61 72 65 20 70  program we are p
227a8 75 74 74 69 6e 67 20 74 6f 67 65 74 68 65 72 20  utting together 
227a9 2a 2f 0a 20 20 20 20 69 6e 74 20 61 64 64 72 4f  */.    int addrO
227aa 75 74 70 75 74 52 6f 77 3b 20 20 20 20 20 20 2f  utputRow;      /
227ab 2a 20 53 74 61 72 74 20 6f 66 20 73 75 62 72 6f  * Start of subro
227ac 75 74 69 6e 65 20 74 68 61 74 20 6f 75 74 70 75  utine that outpu
227ad 74 73 20 61 20 72 65 73 75 6c 74 20 72 6f 77 20  ts a result row 
227ae 2a 2f 0a 20 20 20 20 69 6e 74 20 61 64 64 72 53  */.    int addrS
227af 65 74 41 62 6f 72 74 3b 20 20 20 20 20 20 20 2f  etAbort;       /
227b0 2a 20 53 65 74 20 74 68 65 20 61 62 6f 72 74 20  * Set the abort 
227b1 66 6c 61 67 20 61 6e 64 20 72 65 74 75 72 6e 20  flag and return 
227b2 2a 2f 0a 20 20 20 20 69 6e 74 20 61 64 64 72 49  */.    int addrI
227b3 6e 69 74 69 61 6c 69 7a 65 4c 6f 6f 70 3b 20 2f  nitializeLoop; /
227b4 2a 20 53 74 61 72 74 20 6f 66 20 63 6f 64 65 20  * Start of code 
227b5 74 68 61 74 20 69 6e 69 74 69 61 6c 69 7a 65 73  that initializes
227b6 20 74 68 65 20 69 6e 70 75 74 20 6c 6f 6f 70 20   the input loop 
227b7 2a 2f 0a 20 20 20 20 69 6e 74 20 61 64 64 72 54  */.    int addrT
227b8 6f 70 4f 66 4c 6f 6f 70 3b 20 20 20 20 20 20 2f  opOfLoop;      /
227b9 2a 20 54 6f 70 20 6f 66 20 74 68 65 20 69 6e 70  * Top of the inp
227ba 75 74 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20 69  ut loop */.    i
227bb 6e 74 20 61 64 64 72 47 72 6f 75 70 42 79 43 68  nt addrGroupByCh
227bc 61 6e 67 65 3b 20 20 2f 2a 20 43 6f 64 65 20 74  ange;  /* Code t
227bd 68 61 74 20 72 75 6e 73 20 77 68 65 6e 20 61 6e  hat runs when an
227be 79 20 47 52 4f 55 50 20 42 59 20 74 65 72 6d 20  y GROUP BY term 
227bf 63 68 61 6e 67 65 73 20 2a 2f 0a 20 20 20 20 69  changes */.    i
227c0 6e 74 20 61 64 64 72 50 72 6f 63 65 73 73 52 6f  nt addrProcessRo
227c1 77 3b 20 20 20 20 20 2f 2a 20 43 6f 64 65 20 74  w;     /* Code t
227c2 6f 20 70 72 6f 63 65 73 73 20 61 20 73 69 6e 67  o process a sing
227c3 6c 65 20 69 6e 70 75 74 20 72 6f 77 20 2a 2f 0a  le input row */.
227c4 20 20 20 20 69 6e 74 20 61 64 64 72 45 6e 64 3b      int addrEnd;
227c5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
227c6 6e 64 20 6f 66 20 61 6c 6c 20 70 72 6f 63 65 73  nd of all proces
227c7 73 69 6e 67 20 2a 2f 0a 20 20 20 20 69 6e 74 20  sing */.    int 
227c8 61 64 64 72 53 6f 72 74 69 6e 67 49 64 78 3b 20  addrSortingIdx; 
227c9 20 20 20 20 2f 2a 20 54 68 65 20 4f 50 5f 4f 70      /* The OP_Op
227ca 65 6e 45 70 68 65 6d 65 72 61 6c 20 66 6f 72 20  enEphemeral for 
227cb 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65  the sorting inde
227cc 78 20 2a 2f 0a 20 20 20 20 69 6e 74 20 61 64 64  x */.    int add
227cd 72 52 65 73 65 74 3b 20 20 20 20 20 20 20 20 20  rReset;         
227ce 20 2f 2a 20 53 75 62 72 6f 75 74 69 6e 65 20 66   /* Subroutine f
227cf 6f 72 20 72 65 73 65 74 74 69 6e 67 20 74 68 65  or resetting the
227d0 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a   accumulator */.
227d1 0a 20 20 20 20 61 64 64 72 45 6e 64 20 3d 20 73  .    addrEnd = s
227d2 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
227d3 62 65 6c 28 76 29 3b 0a 0a 20 20 20 20 2f 2a 20  bel(v);..    /* 
227d4 43 6f 6e 76 65 72 74 20 54 4b 5f 43 4f 4c 55 4d  Convert TK_COLUM
227d5 4e 20 6e 6f 64 65 73 20 69 6e 74 6f 20 54 4b 5f  N nodes into TK_
227d6 41 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 6d  AGG_COLUMN and m
227d7 61 6b 65 20 65 6e 74 72 69 65 73 20 69 6e 0a 20  ake entries in. 
227d8 20 20 20 2a 2a 20 73 41 67 67 49 6e 66 6f 20 66     ** sAggInfo f
227d9 6f 72 20 61 6c 6c 20 54 4b 5f 41 47 47 5f 46 55  or all TK_AGG_FU
227da 4e 43 54 49 4f 4e 20 6e 6f 64 65 73 20 69 6e 20  NCTION nodes in 
227db 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74  expressions of t
227dc 68 65 0a 20 20 20 20 2a 2a 20 53 45 4c 45 43 54  he.    ** SELECT
227dd 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20   statement..    
227de 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73  */.    memset(&s
227df 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e  NC, 0, sizeof(sN
227e0 43 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61  C));.    sNC.pPa
227e1 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20  rse = pParse;.  
227e2 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d    sNC.pSrcList =
227e3 20 70 54 61 62 4c 69 73 74 3b 0a 20 20 20 20 73   pTabList;.    s
227e4 4e 43 2e 70 41 67 67 49 6e 66 6f 20 3d 20 26 73  NC.pAggInfo = &s
227e5 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 73 41 67  AggInfo;.    sAg
227e6 67 49 6e 66 6f 2e 6e 53 6f 72 74 69 6e 67 43 6f  gInfo.nSortingCo
227e7 6c 75 6d 6e 20 3d 20 70 47 72 6f 75 70 42 79 20  lumn = pGroupBy 
227e8 3f 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70  ? pGroupBy->nExp
227e9 72 2b 31 20 3a 20 30 3b 0a 20 20 20 20 73 41 67  r+1 : 0;.    sAg
227ea 67 49 6e 66 6f 2e 70 47 72 6f 75 70 42 79 20 3d  gInfo.pGroupBy =
227eb 20 70 47 72 6f 75 70 42 79 3b 0a 20 20 20 20 73   pGroupBy;.    s
227ec 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a  qlite3ExprAnalyz
227ed 65 41 67 67 4c 69 73 74 28 26 73 4e 43 2c 20 70  eAggList(&sNC, p
227ee 45 4c 69 73 74 29 3b 0a 20 20 20 20 73 71 6c 69  EList);.    sqli
227ef 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67  te3ExprAnalyzeAg
227f0 67 4c 69 73 74 28 26 73 4e 43 2c 20 70 4f 72 64  gList(&sNC, pOrd
227f1 65 72 42 79 29 3b 0a 20 20 20 20 69 66 28 20 70  erBy);.    if( p
227f2 48 61 76 69 6e 67 20 29 7b 0a 20 20 20 20 20 20  Having ){.      
227f3 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79  sqlite3ExprAnaly
227f4 7a 65 41 67 67 72 65 67 61 74 65 73 28 26 73 4e  zeAggregates(&sN
227f5 43 2c 20 70 48 61 76 69 6e 67 29 3b 0a 20 20 20  C, pHaving);.   
227f6 20 7d 0a 20 20 20 20 73 41 67 67 49 6e 66 6f 2e   }.    sAggInfo.
227f7 6e 41 63 63 75 6d 75 6c 61 74 6f 72 20 3d 20 73  nAccumulator = s
227f8 41 67 67 49 6e 66 6f 2e 6e 43 6f 6c 75 6d 6e 3b  AggInfo.nColumn;
227f9 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
227fa 73 41 67 67 49 6e 66 6f 2e 6e 46 75 6e 63 3b 20  sAggInfo.nFunc; 
227fb 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  i++){.      sqli
227fc 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67  te3ExprAnalyzeAg
227fd 67 4c 69 73 74 28 26 73 4e 43 2c 20 73 41 67 67  gList(&sNC, sAgg
227fe 49 6e 66 6f 2e 61 46 75 6e 63 5b 69 5d 2e 70 45  Info.aFunc[i].pE
227ff 78 70 72 2d 3e 70 4c 69 73 74 29 3b 0a 20 20 20  xpr->pList);.   
22800 20 7d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d   }.    if( db->m
22801 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f  allocFailed ) go
22802 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a  to select_end;..
22803 20 20 20 20 2f 2a 20 50 72 6f 63 65 73 73 69 6e      /* Processin
22804 67 20 66 6f 72 20 61 67 67 72 65 67 61 74 65 73  g for aggregates
22805 20 77 69 74 68 20 47 52 4f 55 50 20 42 59 20 69   with GROUP BY i
22806 73 20 76 65 72 79 20 64 69 66 66 65 72 65 6e 74  s very different
22807 20 61 6e 64 0a 20 20 20 20 2a 2a 20 6d 75 63 68   and.    ** much
22808 20 6d 6f 72 65 20 63 6f 6d 70 6c 65 78 20 74 68   more complex th
22809 61 6e 20 61 67 67 72 65 67 61 74 65 73 20 77 69  an aggregates wi
2280a 74 68 6f 75 74 20 61 20 47 52 4f 55 50 20 42 59  thout a GROUP BY
2280b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
2280c 20 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20   pGroupBy ){.   
2280d 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79     KeyInfo *pKey
2280e 49 6e 66 6f 3b 20 20 2f 2a 20 4b 65 79 69 6e 67  Info;  /* Keying
2280f 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72   information for
22810 20 74 68 65 20 67 72 6f 75 70 20 62 79 20 63 6c   the group by cl
22811 61 75 73 65 20 2a 2f 0a 0a 20 20 20 20 20 20 2f  ause */..      /
22812 2a 20 43 72 65 61 74 65 20 6c 61 62 65 6c 73 20  * Create labels 
22813 74 68 61 74 20 77 65 20 77 69 6c 6c 20 62 65 20  that we will be 
22814 6e 65 65 64 69 6e 67 0a 20 20 20 20 20 20 2a 2f  needing.      */
22815 0a 20 20 20 20 20 0a 20 20 20 20 20 20 61 64 64  .     .      add
22816 72 49 6e 69 74 69 61 6c 69 7a 65 4c 6f 6f 70 20  rInitializeLoop 
22817 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
22818 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20  eLabel(v);.     
22819 20 61 64 64 72 47 72 6f 75 70 42 79 43 68 61 6e   addrGroupByChan
2281a 67 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  ge = sqlite3Vdbe
2281b 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20  MakeLabel(v);.  
2281c 20 20 20 20 61 64 64 72 50 72 6f 63 65 73 73 52      addrProcessR
2281d 6f 77 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  ow = sqlite3Vdbe
2281e 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 20  MakeLabel(v);.. 
2281f 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65       /* If there
22820 20 69 73 20 61 20 47 52 4f 55 50 20 42 59 20 63   is a GROUP BY c
22821 6c 61 75 73 65 20 77 65 20 6d 69 67 68 74 20 6e  lause we might n
22822 65 65 64 20 61 20 73 6f 72 74 69 6e 67 20 69 6e  eed a sorting in
22823 64 65 78 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20  dex to.      ** 
22824 69 6d 70 6c 65 6d 65 6e 74 20 69 74 2e 20 20 41  implement it.  A
22825 6c 6c 6f 63 61 74 65 20 74 68 61 74 20 73 6f 72  llocate that sor
22826 74 69 6e 67 20 69 6e 64 65 78 20 6e 6f 77 2e 20  ting index now. 
22827 20 49 66 20 69 74 20 74 75 72 6e 73 20 6f 75 74   If it turns out
22828 0a 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 77  .      ** that w
22829 65 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 69 74  e do not need it
2282a 20 61 66 74 65 72 20 61 6c 6c 2c 20 74 68 65 20   after all, the 
2282b 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e  OpenEphemeral in
2282c 73 74 72 75 63 74 69 6f 6e 0a 20 20 20 20 20 20  struction.      
2282d 2a 2a 20 77 69 6c 6c 20 62 65 20 63 6f 6e 76 65  ** will be conve
2282e 72 74 65 64 20 69 6e 74 6f 20 61 20 4e 6f 6f 70  rted into a Noop
2282f 2e 20 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  .  .      */.   
22830 20 20 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74     sAggInfo.sort
22831 69 6e 67 49 64 78 20 3d 20 70 50 61 72 73 65 2d  ingIdx = pParse-
22832 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 70  >nTab++;.      p
22833 4b 65 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66  KeyInfo = keyInf
22834 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50  oFromExprList(pP
22835 61 72 73 65 2c 20 70 47 72 6f 75 70 42 79 29 3b  arse, pGroupBy);
22836 0a 20 20 20 20 20 20 61 64 64 72 53 6f 72 74 69  .      addrSorti
22837 6e 67 49 64 78 20 3d 20 73 71 6c 69 74 65 33 56  ngIdx = sqlite3V
22838 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
22839 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 0a  OpenEphemeral, .
2283a 20 20 20 20 20 20 20 20 20 20 73 41 67 67 49 6e            sAggIn
2283b 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20 73  fo.sortingIdx, s
2283c 41 67 67 49 6e 66 6f 2e 6e 53 6f 72 74 69 6e 67  AggInfo.nSorting
2283d 43 6f 6c 75 6d 6e 2c 20 0a 20 20 20 20 20 20 20  Column, .       
2283e 20 20 20 30 2c 20 28 63 68 61 72 2a 29 70 4b 65     0, (char*)pKe
2283f 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46  yInfo, P4_KEYINF
22840 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 0a 20 20 20  O_HANDOFF);..   
22841 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65     /* Initialize
22842 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e   memory location
22843 73 20 75 73 65 64 20 62 79 20 47 52 4f 55 50 20  s used by GROUP 
22844 42 59 20 61 67 67 72 65 67 61 74 65 20 70 72 6f  BY aggregate pro
22845 63 65 73 73 69 6e 67 0a 20 20 20 20 20 20 2a 2f  cessing.      */
22846 0a 20 20 20 20 20 20 69 55 73 65 46 6c 61 67 20  .      iUseFlag 
22847 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
22848 3b 0a 20 20 20 20 20 20 69 41 62 6f 72 74 46 6c  ;.      iAbortFl
22849 61 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  ag = ++pParse->n
2284a 4d 65 6d 3b 0a 20 20 20 20 20 20 69 41 4d 65 6d  Mem;.      iAMem
2284b 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20   = pParse->nMem 
2284c 2b 20 31 3b 0a 20 20 20 20 20 20 70 50 61 72 73  + 1;.      pPars
2284d 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 47 72 6f 75  e->nMem += pGrou
2284e 70 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20  pBy->nExpr;.    
2284f 20 20 69 42 4d 65 6d 20 3d 20 70 50 61 72 73 65    iBMem = pParse
22850 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20 20 20  ->nMem + 1;.    
22851 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b    pParse->nMem +
22852 3d 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70  = pGroupBy->nExp
22853 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  r;.      sqlite3
22854 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
22855 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 69 41 62  _Integer, 0, iAb
22856 6f 72 74 46 6c 61 67 29 3b 0a 20 20 20 20 20 20  ortFlag);.      
22857 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
22858 22 63 6c 65 61 72 20 61 62 6f 72 74 20 66 6c 61  "clear abort fla
22859 67 22 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  g"));.      sqli
2285a 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2285b 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20   OP_Integer, 0, 
2285c 69 55 73 65 46 6c 61 67 29 3b 0a 20 20 20 20 20  iUseFlag);.     
2285d 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
2285e 20 22 69 6e 64 69 63 61 74 65 20 61 63 63 75 6d   "indicate accum
2285f 75 6c 61 74 6f 72 20 65 6d 70 74 79 22 29 29 3b  ulator empty"));
22860 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
22861 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
22862 6f 74 6f 2c 20 30 2c 20 61 64 64 72 49 6e 69 74  oto, 0, addrInit
22863 69 61 6c 69 7a 65 4c 6f 6f 70 29 3b 0a 0a 20 20  ializeLoop);..  
22864 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20      /* Generate 
22865 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61  a subroutine tha
22866 74 20 6f 75 74 70 75 74 73 20 61 20 73 69 6e 67  t outputs a sing
22867 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 20 72 65  le row of the re
22868 73 75 6c 74 0a 20 20 20 20 20 20 2a 2a 20 73 65  sult.      ** se
22869 74 2e 20 20 54 68 69 73 20 73 75 62 72 6f 75 74  t.  This subrout
2286a 69 6e 65 20 66 69 72 73 74 20 6c 6f 6f 6b 73 20  ine first looks 
2286b 61 74 20 74 68 65 20 69 55 73 65 46 6c 61 67 2e  at the iUseFlag.
2286c 20 20 49 66 20 69 55 73 65 46 6c 61 67 0a 20 20    If iUseFlag.  
2286d 20 20 20 20 2a 2a 20 69 73 20 6c 65 73 73 20 74      ** is less t
2286e 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20  han or equal to 
2286f 7a 65 72 6f 2c 20 74 68 65 20 73 75 62 72 6f 75  zero, the subrou
22870 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tine is a no-op.
22871 20 20 49 66 0a 20 20 20 20 20 20 2a 2a 20 74 68    If.      ** th
22872 65 20 70 72 6f 63 65 73 73 69 6e 67 20 63 61 6c  e processing cal
22873 6c 73 20 66 6f 72 20 74 68 65 20 71 75 65 72 79  ls for the query
22874 20 74 6f 20 61 62 6f 72 74 2c 20 74 68 69 73 20   to abort, this 
22875 73 75 62 72 6f 75 74 69 6e 65 0a 20 20 20 20 20  subroutine.     
22876 20 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 73 20 74   ** increments t
22877 68 65 20 69 41 62 6f 72 74 46 6c 61 67 20 6d 65  he iAbortFlag me
22878 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 62 65  mory location be
22879 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20 69  fore returning i
2287a 6e 0a 20 20 20 20 20 20 2a 2a 20 6f 72 64 65 72  n.      ** order
2287b 20 74 6f 20 73 69 67 6e 61 6c 20 74 68 65 20 63   to signal the c
2287c 61 6c 6c 65 72 20 74 6f 20 61 62 6f 72 74 2e 0a  aller to abort..
2287d 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61        */.      a
2287e 64 64 72 53 65 74 41 62 6f 72 74 20 3d 20 73 71  ddrSetAbort = sq
2287f 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
22880 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 73  Addr(v);.      s
22881 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
22882 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
22883 31 2c 20 69 41 62 6f 72 74 46 6c 61 67 29 3b 0a  1, iAbortFlag);.
22884 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
22885 74 28 28 76 2c 20 22 73 65 74 20 61 62 6f 72 74  t((v, "set abort
22886 20 66 6c 61 67 22 29 29 3b 0a 20 20 20 20 20 20   flag"));.      
22887 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22888 32 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c 20  2(v, OP_Return, 
22889 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 61 64 64  0, 0);.      add
2288a 72 4f 75 74 70 75 74 52 6f 77 20 3d 20 73 71 6c  rOutputRow = sql
2288b 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
2288c 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 73 71  ddr(v);.      sq
2288d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2288e 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 69 55 73  v, OP_IfPos, iUs
2288f 65 46 6c 61 67 2c 20 61 64 64 72 4f 75 74 70 75  eFlag, addrOutpu
22890 74 52 6f 77 2b 32 29 3b 0a 20 20 20 20 20 20 56  tRow+2);.      V
22891 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
22892 47 72 6f 75 70 62 79 20 72 65 73 75 6c 74 20 67  Groupby result g
22893 65 6e 65 72 61 74 6f 72 20 65 6e 74 72 79 20 70  enerator entry p
22894 6f 69 6e 74 22 29 29 3b 0a 20 20 20 20 20 20 73  oint"));.      s
22895 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
22896 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c 20 30  (v, OP_Return, 0
22897 2c 20 30 29 3b 0a 20 20 20 20 20 20 66 69 6e 61  , 0);.      fina
22898 6c 69 7a 65 41 67 67 46 75 6e 63 74 69 6f 6e 73  lizeAggFunctions
22899 28 70 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e  (pParse, &sAggIn
2289a 66 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  fo);.      if( p
2289b 48 61 76 69 6e 67 20 29 7b 0a 20 20 20 20 20 20  Having ){.      
2289c 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46    sqlite3ExprIfF
2289d 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 48 61  alse(pParse, pHa
2289e 76 69 6e 67 2c 20 61 64 64 72 4f 75 74 70 75 74  ving, addrOutput
2289f 52 6f 77 2b 31 2c 20 53 51 4c 49 54 45 5f 4a 55  Row+1, SQLITE_JU
228a0 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20  MPIFNULL);.     
228a1 20 7d 0a 20 20 20 20 20 20 73 65 6c 65 63 74 49   }.      selectI
228a2 6e 6e 65 72 4c 6f 6f 70 28 70 50 61 72 73 65 2c  nnerLoop(pParse,
228a3 20 70 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 30   p, p->pEList, 0
228a4 2c 20 30 2c 20 70 4f 72 64 65 72 42 79 2c 0a 20  , 0, pOrderBy,. 
228a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
228a6 20 20 20 20 20 64 69 73 74 69 6e 63 74 2c 20 70       distinct, p
228a7 44 65 73 74 2c 0a 20 20 20 20 20 20 20 20 20 20  Dest,.          
228a8 20 20 20 20 20 20 20 20 20 20 20 20 61 64 64 72              addr
228a9 4f 75 74 70 75 74 52 6f 77 2b 31 2c 20 61 64 64  OutputRow+1, add
228aa 72 53 65 74 41 62 6f 72 74 2c 20 61 66 66 29 3b  rSetAbort, aff);
228ab 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
228ac 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
228ad 65 74 75 72 6e 2c 20 30 2c 20 30 29 3b 0a 20 20  eturn, 0, 0);.  
228ae 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
228af 28 76 2c 20 22 65 6e 64 20 67 72 6f 75 70 62 79  (v, "end groupby
228b0 20 72 65 73 75 6c 74 20 67 65 6e 65 72 61 74 6f   result generato
228b1 72 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  r"));..      /* 
228b2 47 65 6e 65 72 61 74 65 20 61 20 73 75 62 72 6f  Generate a subro
228b3 75 74 69 6e 65 20 74 68 61 74 20 77 69 6c 6c 20  utine that will 
228b4 72 65 73 65 74 20 74 68 65 20 67 72 6f 75 70 2d  reset the group-
228b5 62 79 20 61 63 63 75 6d 75 6c 61 74 6f 72 0a 20  by accumulator. 
228b6 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 64       */.      ad
228b7 64 72 52 65 73 65 74 20 3d 20 73 71 6c 69 74 65  drReset = sqlite
228b8 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
228b9 28 76 29 3b 0a 20 20 20 20 20 20 72 65 73 65 74  (v);.      reset
228ba 41 63 63 75 6d 75 6c 61 74 6f 72 28 70 50 61 72  Accumulator(pPar
228bb 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a  se, &sAggInfo);.
228bc 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
228bd 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
228be 74 75 72 6e 2c 20 30 2c 20 30 29 3b 0a 0a 20 20  turn, 0, 0);..  
228bf 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 61 20 6c      /* Begin a l
228c0 6f 6f 70 20 74 68 61 74 20 77 69 6c 6c 20 65 78  oop that will ex
228c1 74 72 61 63 74 20 61 6c 6c 20 73 6f 75 72 63 65  tract all source
228c2 20 72 6f 77 73 20 69 6e 20 47 52 4f 55 50 20 42   rows in GROUP B
228c3 59 20 6f 72 64 65 72 2e 0a 20 20 20 20 20 20 2a  Y order..      *
228c4 2a 20 54 68 69 73 20 6d 69 67 68 74 20 69 6e 76  * This might inv
228c5 6f 6c 76 65 20 74 77 6f 20 73 65 70 61 72 61 74  olve two separat
228c6 65 20 6c 6f 6f 70 73 20 77 69 74 68 20 61 6e 20  e loops with an 
228c7 4f 50 5f 53 6f 72 74 20 69 6e 20 62 65 74 77 65  OP_Sort in betwe
228c8 65 6e 2c 20 6f 72 0a 20 20 20 20 20 20 2a 2a 20  en, or.      ** 
228c9 69 74 20 6d 69 67 68 74 20 62 65 20 61 20 73 69  it might be a si
228ca 6e 67 6c 65 20 6c 6f 6f 70 20 74 68 61 74 20 75  ngle loop that u
228cb 73 65 73 20 61 6e 20 69 6e 64 65 78 20 74 6f 20  ses an index to 
228cc 65 78 74 72 61 63 74 20 69 6e 66 6f 72 6d 61 74  extract informat
228cd 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 69 6e 20  ion.      ** in 
228ce 74 68 65 20 72 69 67 68 74 20 6f 72 64 65 72 20  the right order 
228cf 74 6f 20 62 65 67 69 6e 20 77 69 74 68 2e 0a 20  to begin with.. 
228d0 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
228d1 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
228d2 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 49 6e 69  Label(v, addrIni
228d3 74 69 61 6c 69 7a 65 4c 6f 6f 70 29 3b 0a 20 20  tializeLoop);.  
228d4 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
228d5 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75  ddOp2(v, OP_Gosu
228d6 62 2c 20 30 2c 20 61 64 64 72 52 65 73 65 74 29  b, 0, addrReset)
228d7 3b 0a 20 20 20 20 20 20 70 57 49 6e 66 6f 20 3d  ;.      pWInfo =
228d8 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
228d9 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c  in(pParse, pTabL
228da 69 73 74 2c 20 70 57 68 65 72 65 2c 20 26 70 47  ist, pWhere, &pG
228db 72 6f 75 70 42 79 2c 20 30 29 3b 0a 20 20 20 20  roupBy, 0);.    
228dc 20 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20    if( pWInfo==0 
228dd 29 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e  ) goto select_en
228de 64 3b 0a 20 20 20 20 20 20 69 66 28 20 70 47 72  d;.      if( pGr
228df 6f 75 70 42 79 3d 3d 30 20 29 7b 0a 20 20 20 20  oupBy==0 ){.    
228e0 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 74 69 6d      /* The optim
228e1 69 7a 65 72 20 69 73 20 61 62 6c 65 20 74 6f 20  izer is able to 
228e2 64 65 6c 69 76 65 72 20 72 6f 77 73 20 69 6e 20  deliver rows in 
228e3 67 72 6f 75 70 20 62 79 20 6f 72 64 65 72 20 73  group by order s
228e4 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 65 20  o.        ** we 
228e5 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 73  do not have to s
228e6 6f 72 74 2e 20 20 54 68 65 20 4f 50 5f 4f 70 65  ort.  The OP_Ope
228e7 6e 45 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65  nEphemeral table
228e8 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 20 20 20   will be.       
228e9 20 2a 2a 20 63 61 6e 63 65 6c 6c 65 64 20 6c 61   ** cancelled la
228ea 74 65 72 20 62 65 63 61 75 73 65 20 77 65 20 73  ter because we s
228eb 74 69 6c 6c 20 6e 65 65 64 20 74 6f 20 75 73 65  till need to use
228ec 20 74 68 65 20 70 4b 65 79 49 6e 66 6f 0a 20 20   the pKeyInfo.  
228ed 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
228ee 20 70 47 72 6f 75 70 42 79 20 3d 20 70 2d 3e 70   pGroupBy = p->p
228ef 47 72 6f 75 70 42 79 3b 0a 20 20 20 20 20 20 20  GroupBy;.       
228f0 20 67 72 6f 75 70 42 79 53 6f 72 74 20 3d 20 30   groupBySort = 0
228f1 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
228f2 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 73 20 61         /* Rows a
228f3 72 65 20 63 6f 6d 69 6e 67 20 6f 75 74 20 69 6e  re coming out in
228f4 20 75 6e 64 65 74 65 72 6d 69 6e 65 64 20 6f 72   undetermined or
228f5 64 65 72 2e 20 20 57 65 20 68 61 76 65 20 74 6f  der.  We have to
228f6 20 70 75 73 68 0a 20 20 20 20 20 20 20 20 2a 2a   push.        **
228f7 20 65 61 63 68 20 72 6f 77 20 69 6e 74 6f 20 61   each row into a
228f8 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 2c 20   sorting index, 
228f9 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 66 69  terminate the fi
228fa 72 73 74 20 6c 6f 6f 70 2c 0a 20 20 20 20 20 20  rst loop,.      
228fb 20 20 2a 2a 20 74 68 65 6e 20 6c 6f 6f 70 20 6f    ** then loop o
228fc 76 65 72 20 74 68 65 20 73 6f 72 74 69 6e 67 20  ver the sorting 
228fd 69 6e 64 65 78 20 69 6e 20 6f 72 64 65 72 20 74  index in order t
228fe 6f 20 67 65 74 20 74 68 65 20 6f 75 74 70 75 74  o get the output
228ff 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 73  .        ** in s
22900 6f 72 74 65 64 20 6f 72 64 65 72 0a 20 20 20 20  orted order.    
22901 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69      */.        i
22902 6e 74 20 72 65 67 42 61 73 65 3b 0a 20 20 20 20  nt regBase;.    
22903 20 20 20 20 69 6e 74 20 72 65 67 52 65 63 6f 72      int regRecor
22904 64 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e  d;.        int n
22905 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  Col;.        int
22906 20 6e 47 72 6f 75 70 42 79 3b 0a 0a 20 20 20 20   nGroupBy;..    
22907 20 20 20 20 67 72 6f 75 70 42 79 53 6f 72 74 20      groupBySort 
22908 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 6e 47 72  = 1;.        nGr
22909 6f 75 70 42 79 20 3d 20 70 47 72 6f 75 70 42 79  oupBy = pGroupBy
2290a 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 20  ->nExpr;.       
2290b 20 6e 43 6f 6c 20 3d 20 6e 47 72 6f 75 70 42 79   nCol = nGroupBy
2290c 20 2b 20 31 3b 0a 20 20 20 20 20 20 20 20 6a 20   + 1;.        j 
2290d 3d 20 6e 47 72 6f 75 70 42 79 2b 31 3b 0a 20 20  = nGroupBy+1;.  
2290e 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
2290f 3c 73 41 67 67 49 6e 66 6f 2e 6e 43 6f 6c 75 6d  <sAggInfo.nColum
22910 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  n; i++){.       
22911 20 20 20 69 66 28 20 73 41 67 67 49 6e 66 6f 2e     if( sAggInfo.
22912 61 43 6f 6c 5b 69 5d 2e 69 53 6f 72 74 65 72 43  aCol[i].iSorterC
22913 6f 6c 75 6d 6e 3e 3d 6a 20 29 7b 0a 20 20 20 20  olumn>=j ){.    
22914 20 20 20 20 20 20 20 20 6e 43 6f 6c 2b 2b 3b 0a          nCol++;.
22915 20 20 20 20 20 20 20 20 20 20 20 20 6a 2b 2b 3b              j++;
22916 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
22917 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72       }.        r
22918 65 67 42 61 73 65 20 3d 20 73 71 6c 69 74 65 33  egBase = sqlite3
22919 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61  GetTempRange(pPa
2291a 72 73 65 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20 20  rse, nCol);.    
2291b 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2291c 6f 64 65 45 78 70 72 4c 69 73 74 28 70 50 61 72  odeExprList(pPar
2291d 73 65 2c 20 70 47 72 6f 75 70 42 79 2c 20 72 65  se, pGroupBy, re
2291e 67 42 61 73 65 2c 20 30 29 3b 0a 20 20 20 20 20  gBase, 0);.     
2291f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22920 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 71 75 65  dOp2(v, OP_Seque
22921 6e 63 65 2c 20 73 41 67 67 49 6e 66 6f 2e 73 6f  nce, sAggInfo.so
22922 72 74 69 6e 67 49 64 78 2c 72 65 67 42 61 73 65  rtingIdx,regBase
22923 2b 6e 47 72 6f 75 70 42 79 29 3b 0a 20 20 20 20  +nGroupBy);.    
22924 20 20 20 20 6a 20 3d 20 6e 47 72 6f 75 70 42 79      j = nGroupBy
22925 2b 31 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  +1;.        for(
22926 69 3d 30 3b 20 69 3c 73 41 67 67 49 6e 66 6f 2e  i=0; i<sAggInfo.
22927 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20  nColumn; i++){. 
22928 20 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20           struct 
22929 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f  AggInfo_col *pCo
2292a 6c 20 3d 20 26 73 41 67 67 49 6e 66 6f 2e 61 43  l = &sAggInfo.aC
2292b 6f 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20  ol[i];.         
2292c 20 69 66 28 20 70 43 6f 6c 2d 3e 69 53 6f 72 74   if( pCol->iSort
2292d 65 72 43 6f 6c 75 6d 6e 3e 3d 6a 20 29 7b 0a 20  erColumn>=j ){. 
2292e 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 72             int r
2292f 31 20 3d 20 6a 20 2b 20 72 65 67 42 61 73 65 3b  1 = j + regBase;
22930 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74  .            int
22931 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70   r2 = sqlite3Exp
22932 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 70  rCodeGetColumn(p
22933 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20  Parse, .        
22934 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22935 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70 54 61         pCol->pTa
22936 62 2c 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e  b, pCol->iColumn
22937 2c 20 70 43 6f 6c 2d 3e 69 54 61 62 6c 65 2c 20  , pCol->iTable, 
22938 72 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  r1, 0);.        
22939 20 20 20 20 69 66 28 20 72 31 21 3d 72 32 20 29      if( r1!=r2 )
2293a 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
2293b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2293c 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72  2(v, OP_SCopy, r
2293d 32 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20  2, r1);.        
2293e 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
2293f 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20    j++;.         
22940 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
22941 20 20 20 20 20 72 65 67 52 65 63 6f 72 64 20 3d       regRecord =
22942 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
22943 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
22944 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22945 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65  ddOp3(v, OP_Make
22946 52 65 63 6f 72 64 2c 20 72 65 67 42 61 73 65 2c  Record, regBase,
22947 20 6e 43 6f 6c 2c 20 72 65 67 52 65 63 6f 72 64   nCol, regRecord
22948 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
22949 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2294a 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 73 41  OP_IdxInsert, sA
2294b 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64  ggInfo.sortingId
2294c 78 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20  x, regRecord);. 
2294d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65         sqlite3Re
2294e 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
2294f 72 73 65 2c 20 72 65 67 52 65 63 6f 72 64 29 3b  rse, regRecord);
22950 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
22951 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65  ReleaseTempRange
22952 28 70 50 61 72 73 65 2c 20 72 65 67 42 61 73 65  (pParse, regBase
22953 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20 20  , nCol);.       
22954 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64   sqlite3WhereEnd
22955 28 70 57 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20  (pWInfo);.      
22956 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22957 4f 70 32 28 76 2c 20 4f 50 5f 53 6f 72 74 2c 20  Op2(v, OP_Sort, 
22958 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67  sAggInfo.sorting
22959 49 64 78 2c 20 61 64 64 72 45 6e 64 29 3b 0a 20  Idx, addrEnd);. 
2295a 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65         VdbeComme
2295b 6e 74 28 28 76 2c 20 22 47 52 4f 55 50 20 42 59  nt((v, "GROUP BY
2295c 20 73 6f 72 74 22 29 29 3b 0a 20 20 20 20 20 20   sort"));.      
2295d 20 20 73 41 67 67 49 6e 66 6f 2e 75 73 65 53 6f    sAggInfo.useSo
2295e 72 74 69 6e 67 49 64 78 20 3d 20 31 3b 0a 20 20  rtingIdx = 1;.  
2295f 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
22960 45 76 61 6c 75 61 74 65 20 74 68 65 20 63 75 72  Evaluate the cur
22961 72 65 6e 74 20 47 52 4f 55 50 20 42 59 20 74 65  rent GROUP BY te
22962 72 6d 73 20 61 6e 64 20 73 74 6f 72 65 20 69 6e  rms and store in
22963 20 62 30 2c 20 62 31 2c 20 62 32 2e 2e 2e 0a 20   b0, b1, b2.... 
22964 20 20 20 20 20 2a 2a 20 28 62 30 20 69 73 20 6d       ** (b0 is m
22965 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 69  emory location i
22966 42 4d 65 6d 2b 30 2c 20 62 31 20 69 73 20 69 42  BMem+0, b1 is iB
22967 4d 65 6d 2b 31 2c 20 61 6e 64 20 73 6f 20 66 6f  Mem+1, and so fo
22968 72 74 68 29 0a 20 20 20 20 20 20 2a 2a 20 54 68  rth).      ** Th
22969 65 6e 20 63 6f 6d 70 61 72 65 20 74 68 65 20 63  en compare the c
2296a 75 72 72 65 6e 74 20 47 52 4f 55 50 20 42 59 20  urrent GROUP BY 
2296b 74 65 72 6d 73 20 61 67 61 69 6e 73 74 20 74 68  terms against th
2296c 65 20 47 52 4f 55 50 20 42 59 20 74 65 72 6d 73  e GROUP BY terms
2296d 0a 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74  .      ** from t
2296e 68 65 20 70 72 65 76 69 6f 75 73 20 72 6f 77 20  he previous row 
2296f 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64  currently stored
22970 20 69 6e 20 61 30 2c 20 61 31 2c 20 61 32 2e 2e   in a0, a1, a2..
22971 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
22972 20 61 64 64 72 54 6f 70 4f 66 4c 6f 6f 70 20 3d   addrTopOfLoop =
22973 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
22974 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20  entAddr(v);.    
22975 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 47 72    for(j=0; j<pGr
22976 6f 75 70 42 79 2d 3e 6e 45 78 70 72 3b 20 6a 2b  oupBy->nExpr; j+
22977 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
22978 67 72 6f 75 70 42 79 53 6f 72 74 20 29 7b 0a 20  groupBySort ){. 
22979 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2297a 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2297b 5f 43 6f 6c 75 6d 6e 2c 20 73 41 67 67 49 6e 66  _Column, sAggInf
2297c 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20 6a 2c  o.sortingIdx, j,
2297d 20 69 42 4d 65 6d 2b 6a 29 3b 0a 20 20 20 20 20   iBMem+j);.     
2297e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2297f 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 64 69 72      sAggInfo.dir
22980 65 63 74 4d 6f 64 65 20 3d 20 31 3b 0a 20 20 20  ectMode = 1;.   
22981 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
22982 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
22983 47 72 6f 75 70 42 79 2d 3e 61 5b 6a 5d 2e 70 45  GroupBy->a[j].pE
22984 78 70 72 2c 20 69 42 4d 65 6d 2b 6a 29 3b 0a 20  xpr, iBMem+j);. 
22985 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
22986 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 70 47 72  .      for(j=pGr
22987 6f 75 70 42 79 2d 3e 6e 45 78 70 72 2d 31 3b 20  oupBy->nExpr-1; 
22988 6a 3e 3d 30 3b 20 6a 2d 2d 29 7b 0a 20 20 20 20  j>=0; j--){.    
22989 20 20 20 20 69 66 28 20 6a 3d 3d 30 20 29 7b 0a      if( j==0 ){.
2298a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2298b 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2298c 50 5f 45 71 2c 20 69 41 4d 65 6d 2b 6a 2c 20 61  P_Eq, iAMem+j, a
2298d 64 64 72 50 72 6f 63 65 73 73 52 6f 77 2c 20 69  ddrProcessRow, i
2298e 42 4d 65 6d 2b 6a 29 3b 0a 20 20 20 20 20 20 20  BMem+j);.       
2298f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
22990 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22991 4f 70 33 28 76 2c 20 4f 50 5f 4e 65 2c 20 69 41  Op3(v, OP_Ne, iA
22992 4d 65 6d 2b 6a 2c 20 61 64 64 72 47 72 6f 75 70  Mem+j, addrGroup
22993 42 79 43 68 61 6e 67 65 2c 20 69 42 4d 65 6d 2b  ByChange, iBMem+
22994 6a 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  j);.        }.  
22995 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
22996 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c  eChangeP4(v, -1,
22997 20 28 76 6f 69 64 2a 29 70 4b 65 79 49 6e 66 6f   (void*)pKeyInfo
22998 2d 3e 61 43 6f 6c 6c 5b 6a 5d 2c 20 50 34 5f 43  ->aColl[j], P4_C
22999 4f 4c 4c 53 45 51 29 3b 0a 20 20 20 20 20 20 20  OLLSEQ);.       
2299a 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
2299b 67 65 50 35 28 76 2c 20 53 51 4c 49 54 45 5f 4e  geP5(v, SQLITE_N
2299c 55 4c 4c 45 51 55 41 4c 29 3b 0a 20 20 20 20 20  ULLEQUAL);.     
2299d 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 47 65 6e   }..      /* Gen
2299e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20  erate code that 
2299f 72 75 6e 73 20 77 68 65 6e 65 76 65 72 20 74 68  runs whenever th
229a0 65 20 47 52 4f 55 50 20 42 59 20 63 68 61 6e 67  e GROUP BY chang
229a1 65 73 2e 0a 20 20 20 20 20 20 2a 2a 20 43 68 61  es..      ** Cha
229a2 6e 67 65 20 69 6e 20 74 68 65 20 47 52 4f 55 50  nge in the GROUP
229a3 20 42 59 20 61 72 65 20 64 65 74 65 63 74 65 64   BY are detected
229a4 20 62 79 20 74 68 65 20 70 72 65 76 69 6f 75 73   by the previous
229a5 20 63 6f 64 65 0a 20 20 20 20 20 20 2a 2a 20 62   code.      ** b
229a6 6c 6f 63 6b 2e 20 20 49 66 20 74 68 65 72 65 20  lock.  If there 
229a7 77 65 72 65 20 6e 6f 20 63 68 61 6e 67 65 73 2c  were no changes,
229a8 20 74 68 69 73 20 62 6c 6f 63 6b 20 69 73 20 73   this block is s
229a9 6b 69 70 70 65 64 2e 0a 20 20 20 20 20 20 2a 2a  kipped..      **
229aa 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 63  .      ** This c
229ab 6f 64 65 20 63 6f 70 69 65 73 20 63 75 72 72 65  ode copies curre
229ac 6e 74 20 67 72 6f 75 70 20 62 79 20 74 65 72 6d  nt group by term
229ad 73 20 69 6e 20 62 30 2c 62 31 2c 62 32 2c 2e 2e  s in b0,b1,b2,..
229ae 2e 0a 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 20  ..      ** over 
229af 74 6f 20 61 30 2c 61 31 2c 61 32 2e 20 20 49 74  to a0,a1,a2.  It
229b0 20 74 68 65 6e 20 63 61 6c 6c 73 20 74 68 65 20   then calls the 
229b1 6f 75 74 70 75 74 20 73 75 62 72 6f 75 74 69 6e  output subroutin
229b2 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 72  e.      ** and r
229b3 65 73 65 74 73 20 74 68 65 20 61 67 67 72 65 67  esets the aggreg
229b4 61 74 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20  ate accumulator 
229b5 72 65 67 69 73 74 65 72 73 20 69 6e 20 70 72 65  registers in pre
229b6 70 61 72 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a  paration.      *
229b7 2a 20 66 6f 72 20 74 68 65 20 6e 65 78 74 20 47  * for the next G
229b8 52 4f 55 50 20 42 59 20 62 61 74 63 68 2e 0a 20  ROUP BY batch.. 
229b9 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
229ba 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
229bb 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 47 72 6f  Label(v, addrGro
229bc 75 70 42 79 43 68 61 6e 67 65 29 3b 0a 20 20 20  upByChange);.   
229bd 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 47     for(j=0; j<pG
229be 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 3b 20 6a  roupBy->nExpr; j
229bf 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  ++){.        sql
229c0 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65  ite3ExprCodeMove
229c1 28 70 50 61 72 73 65 2c 20 69 42 4d 65 6d 2b 6a  (pParse, iBMem+j
229c2 2c 20 69 41 4d 65 6d 2b 6a 29 3b 0a 20 20 20 20  , iAMem+j);.    
229c3 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
229c4 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
229c5 50 5f 47 6f 73 75 62 2c 20 30 2c 20 61 64 64 72  P_Gosub, 0, addr
229c6 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20  OutputRow);.    
229c7 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
229c8 2c 20 22 6f 75 74 70 75 74 20 6f 6e 65 20 72 6f  , "output one ro
229c9 77 22 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  w"));.      sqli
229ca 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
229cb 20 4f 50 5f 49 66 50 6f 73 2c 20 69 41 62 6f 72   OP_IfPos, iAbor
229cc 74 46 6c 61 67 2c 20 61 64 64 72 45 6e 64 29 3b  tFlag, addrEnd);
229cd 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
229ce 6e 74 28 28 76 2c 20 22 63 68 65 63 6b 20 61 62  nt((v, "check ab
229cf 6f 72 74 20 66 6c 61 67 22 29 29 3b 0a 20 20 20  ort flag"));.   
229d0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
229d1 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62  dOp2(v, OP_Gosub
229d2 2c 20 30 2c 20 61 64 64 72 52 65 73 65 74 29 3b  , 0, addrReset);
229d3 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
229d4 6e 74 28 28 76 2c 20 22 72 65 73 65 74 20 61 63  nt((v, "reset ac
229d5 63 75 6d 75 6c 61 74 6f 72 22 29 29 3b 0a 0a 20  cumulator"));.. 
229d6 20 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74       /* Update t
229d7 68 65 20 61 67 67 72 65 67 61 74 65 20 61 63 63  he aggregate acc
229d8 75 6d 75 6c 61 74 6f 72 73 20 62 61 73 65 64 20  umulators based 
229d9 6f 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  on the content o
229da 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63  f.      ** the c
229db 75 72 72 65 6e 74 20 72 6f 77 0a 20 20 20 20 20  urrent row.     
229dc 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
229dd 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
229de 6c 28 76 2c 20 61 64 64 72 50 72 6f 63 65 73 73  l(v, addrProcess
229df 52 6f 77 29 3b 0a 20 20 20 20 20 20 75 70 64 61  Row);.      upda
229e0 74 65 41 63 63 75 6d 75 6c 61 74 6f 72 28 70 50  teAccumulator(pP
229e1 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29  arse, &sAggInfo)
229e2 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
229e3 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
229e4 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 55 73 65  Integer, 1, iUse
229e5 46 6c 61 67 29 3b 0a 20 20 20 20 20 20 56 64 62  Flag);.      Vdb
229e6 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 69 6e  eComment((v, "in
229e7 64 69 63 61 74 65 20 64 61 74 61 20 69 6e 20 61  dicate data in a
229e8 63 63 75 6d 75 6c 61 74 6f 72 22 29 29 3b 0a 0a  ccumulator"));..
229e9 20 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20        /* End of 
229ea 74 68 65 20 6c 6f 6f 70 0a 20 20 20 20 20 20 2a  the loop.      *
229eb 2f 0a 20 20 20 20 20 20 69 66 28 20 67 72 6f 75  /.      if( grou
229ec 70 42 79 53 6f 72 74 20 29 7b 0a 20 20 20 20 20  pBySort ){.     
229ed 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
229ee 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c  dOp2(v, OP_Next,
229ef 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e   sAggInfo.sortin
229f0 67 49 64 78 2c 20 61 64 64 72 54 6f 70 4f 66 4c  gIdx, addrTopOfL
229f1 6f 6f 70 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  oop);.      }els
229f2 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e{.        sqlit
229f3 65 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66  e3WhereEnd(pWInf
229f4 6f 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  o);.        sqli
229f5 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e  te3VdbeChangeToN
229f6 6f 6f 70 28 76 2c 20 61 64 64 72 53 6f 72 74 69  oop(v, addrSorti
229f7 6e 67 49 64 78 2c 20 31 29 3b 0a 20 20 20 20 20  ngIdx, 1);.     
229f8 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 75 74   }..      /* Out
229f9 70 75 74 20 74 68 65 20 66 69 6e 61 6c 20 72 6f  put the final ro
229fa 77 20 6f 66 20 72 65 73 75 6c 74 0a 20 20 20 20  w of result.    
229fb 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74    */.      sqlit
229fc 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
229fd 4f 50 5f 47 6f 73 75 62 2c 20 30 2c 20 61 64 64  OP_Gosub, 0, add
229fe 72 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20  rOutputRow);.   
229ff 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
22a00 76 2c 20 22 6f 75 74 70 75 74 20 66 69 6e 61 6c  v, "output final
22a01 20 72 6f 77 22 29 29 3b 0a 20 20 20 20 20 20 0a   row"));.      .
22a02 20 20 20 20 7d 20 2f 2a 20 65 6e 64 69 66 20 70      } /* endif p
22a03 47 72 6f 75 70 42 79 20 2a 2f 0a 20 20 20 20 65  GroupBy */.    e
22a04 6c 73 65 20 7b 0a 20 20 20 20 20 20 45 78 70 72  lse {.      Expr
22a05 4c 69 73 74 20 2a 70 4d 69 6e 4d 61 78 20 3d 20  List *pMinMax = 
22a06 30 3b 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73  0;.      ExprLis
22a07 74 20 2a 70 44 65 6c 20 3d 20 30 3b 0a 20 20 20  t *pDel = 0;.   
22a08 20 20 20 75 38 20 66 6c 61 67 3b 0a 0a 20 20 20     u8 flag;..   
22a09 20 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 74     /* Check if t
22a0a 68 65 20 71 75 65 72 79 20 69 73 20 6f 66 20 6f  he query is of o
22a0b 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
22a0c 69 6e 67 20 66 6f 72 6d 73 3a 0a 20 20 20 20 20  ing forms:.     
22a0d 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 53   **.      **   S
22a0e 45 4c 45 43 54 20 6d 69 6e 28 78 29 20 46 52 4f  ELECT min(x) FRO
22a0f 4d 20 2e 2e 2e 0a 20 20 20 20 20 20 2a 2a 20 20  M ....      **  
22a10 20 53 45 4c 45 43 54 20 6d 61 78 28 78 29 20 46   SELECT max(x) F
22a11 52 4f 4d 20 2e 2e 2e 0a 20 20 20 20 20 20 2a 2a  ROM ....      **
22a12 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 69 74 20  .      ** If it 
22a13 69 73 2c 20 74 68 65 6e 20 61 73 6b 20 74 68 65  is, then ask the
22a14 20 63 6f 64 65 20 69 6e 20 77 68 65 72 65 2e 63   code in where.c
22a15 20 74 6f 20 61 74 74 65 6d 70 74 20 74 6f 20 73   to attempt to s
22a16 6f 72 74 20 72 65 73 75 6c 74 73 0a 20 20 20 20  ort results.    
22a17 20 20 2a 2a 20 61 73 20 69 66 20 74 68 65 72 65    ** as if there
22a18 20 77 61 73 20 61 6e 20 22 4f 52 44 45 52 20 4f   was an "ORDER O
22a19 4e 20 78 22 20 6f 72 20 22 4f 52 44 45 52 20 4f  N x" or "ORDER O
22a1a 4e 20 78 20 44 45 53 43 22 20 63 6c 61 75 73 65  N x DESC" clause
22a1b 2e 20 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 77  . .      ** If w
22a1c 68 65 72 65 2e 63 20 69 73 20 61 62 6c 65 20 74  here.c is able t
22a1d 6f 20 70 72 6f 64 75 63 65 20 72 65 73 75 6c 74  o produce result
22a1e 73 20 73 6f 72 74 65 64 20 69 6e 20 74 68 69 73  s sorted in this
22a1f 20 6f 72 64 65 72 2c 20 74 68 65 6e 0a 20 20 20   order, then.   
22a20 20 20 20 2a 2a 20 61 64 64 20 76 64 62 65 20 63     ** add vdbe c
22a21 6f 64 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74  ode to break out
22a22 20 6f 66 20 74 68 65 20 70 72 6f 63 65 73 73 69   of the processi
22a23 6e 67 20 6c 6f 6f 70 20 61 66 74 65 72 20 74 68  ng loop after th
22a24 65 20 0a 20 20 20 20 20 20 2a 2a 20 66 69 72 73  e .      ** firs
22a25 74 20 69 74 65 72 61 74 69 6f 6e 20 28 73 69 6e  t iteration (sin
22a26 63 65 20 74 68 65 20 66 69 72 73 74 20 69 74 65  ce the first ite
22a27 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 6f  ration of the lo
22a28 6f 70 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20  op is .      ** 
22a29 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 6f 70  guaranteed to op
22a2a 65 72 61 74 65 20 6f 6e 20 74 68 65 20 72 6f 77  erate on the row
22a2b 20 77 69 74 68 20 74 68 65 20 6d 69 6e 69 6d 75   with the minimu
22a2c 6d 20 6f 72 20 6d 61 78 69 6d 75 6d 20 0a 20 20  m or maximum .  
22a2d 20 20 20 20 2a 2a 20 76 61 6c 75 65 20 6f 66 20      ** value of 
22a2e 78 2c 20 74 68 65 20 6f 6e 6c 79 20 72 6f 77 20  x, the only row 
22a2f 72 65 71 75 69 72 65 64 29 2e 0a 20 20 20 20 20  required)..     
22a30 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41 20 73   **.      ** A s
22a31 70 65 63 69 61 6c 20 66 6c 61 67 20 6d 75 73 74  pecial flag must
22a32 20 62 65 20 70 61 73 73 65 64 20 74 6f 20 73 71   be passed to sq
22a33 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28  lite3WhereBegin(
22a34 29 20 74 6f 20 73 6c 69 67 68 74 6c 79 0a 20 20  ) to slightly.  
22a35 20 20 20 20 2a 2a 20 6d 6f 64 69 66 79 20 62 65      ** modify be
22a36 68 61 76 69 6f 75 72 20 61 73 20 66 6f 6c 6c 6f  haviour as follo
22a37 77 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  ws:.      **.   
22a38 20 20 20 2a 2a 20 20 20 2b 20 49 66 20 74 68 65     **   + If the
22a39 20 71 75 65 72 79 20 69 73 20 61 20 22 53 45 4c   query is a "SEL
22a3a 45 43 54 20 6d 69 6e 28 78 29 22 2c 20 74 68 65  ECT min(x)", the
22a3b 6e 20 74 68 65 20 6c 6f 6f 70 20 63 6f 64 65 64  n the loop coded
22a3c 20 62 79 0a 20 20 20 20 20 20 2a 2a 20 20 20 20   by.      **    
22a3d 20 77 68 65 72 65 2e 63 20 73 68 6f 75 6c 64 20   where.c should 
22a3e 6e 6f 74 20 69 74 65 72 61 74 65 20 6f 76 65 72  not iterate over
22a3f 20 61 6e 79 20 76 61 6c 75 65 73 20 77 69 74 68   any values with
22a40 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 0a 20 20   a NULL value.  
22a41 20 20 20 20 2a 2a 20 20 20 20 20 66 6f 72 20 78      **     for x
22a42 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  ..      **.     
22a43 20 2a 2a 20 20 20 2b 20 54 68 65 20 6f 70 74 69   **   + The opti
22a44 6d 69 7a 65 72 20 63 6f 64 65 20 69 6e 20 77 68  mizer code in wh
22a45 65 72 65 2e 63 20 28 74 68 65 20 74 68 69 6e 67  ere.c (the thing
22a46 20 74 68 61 74 20 64 65 63 69 64 65 73 20 77 68   that decides wh
22a47 69 63 68 0a 20 20 20 20 20 20 2a 2a 20 20 20 20  ich.      **    
22a48 20 69 6e 64 65 78 20 6f 72 20 69 6e 64 69 63 65   index or indice
22a49 73 20 74 6f 20 75 73 65 29 20 73 68 6f 75 6c 64  s to use) should
22a4a 20 70 6c 61 63 65 20 61 20 64 69 66 66 65 72 65   place a differe
22a4b 6e 74 20 70 72 69 6f 72 69 74 79 20 6f 6e 20 0a  nt priority on .
22a4c 20 20 20 20 20 20 2a 2a 20 20 20 20 20 73 61 74        **     sat
22a4d 69 73 66 79 69 6e 67 20 74 68 65 20 27 4f 52 44  isfying the 'ORD
22a4e 45 52 20 42 59 27 20 63 6c 61 75 73 65 20 74 68  ER BY' clause th
22a4f 61 6e 20 69 74 20 64 6f 65 73 20 69 6e 20 6f 74  an it does in ot
22a50 68 65 72 20 63 61 73 65 73 2e 0a 20 20 20 20 20  her cases..     
22a51 20 2a 2a 20 20 20 20 20 52 65 66 65 72 20 74 6f   **     Refer to
22a52 20 63 6f 64 65 20 61 6e 64 20 63 6f 6d 6d 65 6e   code and commen
22a53 74 73 20 69 6e 20 77 68 65 72 65 2e 63 20 66 6f  ts in where.c fo
22a54 72 20 64 65 74 61 69 6c 73 2e 0a 20 20 20 20 20  r details..     
22a55 20 2a 2f 0a 20 20 20 20 20 20 66 6c 61 67 20 3d   */.      flag =
22a56 20 6d 69 6e 4d 61 78 51 75 65 72 79 28 70 50 61   minMaxQuery(pPa
22a57 72 73 65 2c 20 70 29 3b 0a 20 20 20 20 20 20 69  rse, p);.      i
22a58 66 28 20 66 6c 61 67 20 29 7b 0a 20 20 20 20 20  f( flag ){.     
22a59 20 20 20 70 44 65 6c 20 3d 20 70 4d 69 6e 4d 61     pDel = pMinMa
22a5a 78 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  x = sqlite3ExprL
22a5b 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 45  istDup(db, p->pE
22a5c 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72  List->a[0].pExpr
22a5d 2d 3e 70 4c 69 73 74 29 3b 0a 20 20 20 20 20 20  ->pList);.      
22a5e 20 20 69 66 28 20 70 4d 69 6e 4d 61 78 20 26 26    if( pMinMax &&
22a5f 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
22a60 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ed ){.          
22a61 70 4d 69 6e 4d 61 78 2d 3e 61 5b 30 5d 2e 73 6f  pMinMax->a[0].so
22a62 72 74 4f 72 64 65 72 20 3d 20 28 28 66 6c 61 67  rtOrder = ((flag
22a63 3d 3d 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f  ==WHERE_ORDERBY_
22a64 4d 49 4e 29 3f 30 3a 31 29 3b 0a 20 20 20 20 20  MIN)?0:1);.     
22a65 20 20 20 20 20 70 4d 69 6e 4d 61 78 2d 3e 61 5b       pMinMax->a[
22a66 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54  0].pExpr->op = T
22a67 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20 20 20 20 20 20  K_COLUMN;.      
22a68 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20    }.      }..   
22a69 20 20 20 2f 2a 20 54 68 69 73 20 63 61 73 65 20     /* This case 
22a6a 72 75 6e 73 20 69 66 20 74 68 65 20 61 67 67 72  runs if the aggr
22a6b 65 67 61 74 65 20 68 61 73 20 6e 6f 20 47 52 4f  egate has no GRO
22a6c 55 50 20 42 59 20 63 6c 61 75 73 65 2e 20 20 54  UP BY clause.  T
22a6d 68 65 0a 20 20 20 20 20 20 2a 2a 20 70 72 6f 63  he.      ** proc
22a6e 65 73 73 69 6e 67 20 69 73 20 6d 75 63 68 20 73  essing is much s
22a6f 69 6d 70 6c 65 72 20 73 69 6e 63 65 20 74 68 65  impler since the
22a70 72 65 20 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e  re is only a sin
22a71 67 6c 65 20 72 6f 77 0a 20 20 20 20 20 20 2a 2a  gle row.      **
22a72 20 6f 66 20 6f 75 74 70 75 74 2e 0a 20 20 20 20   of output..    
22a73 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 73 65 74    */.      reset
22a74 41 63 63 75 6d 75 6c 61 74 6f 72 28 70 50 61 72  Accumulator(pPar
22a75 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a  se, &sAggInfo);.
22a76 20 20 20 20 20 20 70 57 49 6e 66 6f 20 3d 20 73        pWInfo = s
22a77 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
22a78 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73  (pParse, pTabLis
22a79 74 2c 20 70 57 68 65 72 65 2c 20 26 70 4d 69 6e  t, pWhere, &pMin
22a7a 4d 61 78 2c 20 66 6c 61 67 29 3b 0a 20 20 20 20  Max, flag);.    
22a7b 20 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20    if( pWInfo==0 
22a7c 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
22a7d 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
22a7e 28 70 44 65 6c 29 3b 0a 20 20 20 20 20 20 20 20  (pDel);.        
22a7f 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b  goto select_end;
22a80 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 75  .      }.      u
22a81 70 64 61 74 65 41 63 63 75 6d 75 6c 61 74 6f 72  pdateAccumulator
22a82 28 70 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e  (pParse, &sAggIn
22a83 66 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  fo);.      if( !
22a84 70 4d 69 6e 4d 61 78 20 26 26 20 66 6c 61 67 20  pMinMax && flag 
22a85 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
22a86 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
22a87 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 57 49 6e  OP_Goto, 0, pWIn
22a88 66 6f 2d 3e 69 42 72 65 61 6b 29 3b 0a 20 20 20  fo->iBreak);.   
22a89 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
22a8a 28 28 76 2c 20 22 25 73 28 29 20 62 79 20 69 6e  ((v, "%s() by in
22a8b 64 65 78 22 2c 20 28 66 6c 61 67 3d 3d 57 48 45  dex", (flag==WHE
22a8c 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 3f 22  RE_ORDERBY_MIN?"
22a8d 6d 69 6e 22 3a 22 6d 61 78 22 29 29 29 3b 0a 20  min":"max")));. 
22a8e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
22a8f 69 74 65 33 57 68 65 72 65 45 6e 64 28 70 57 49  ite3WhereEnd(pWI
22a90 6e 66 6f 29 3b 0a 20 20 20 20 20 20 66 69 6e 61  nfo);.      fina
22a91 6c 69 7a 65 41 67 67 46 75 6e 63 74 69 6f 6e 73  lizeAggFunctions
22a92 28 70 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e  (pParse, &sAggIn
22a93 66 6f 29 3b 0a 20 20 20 20 20 20 70 4f 72 64 65  fo);.      pOrde
22a94 72 42 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  rBy = 0;.      i
22a95 66 28 20 70 48 61 76 69 6e 67 20 29 7b 0a 20 20  f( pHaving ){.  
22a96 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
22a97 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c  rIfFalse(pParse,
22a98 20 70 48 61 76 69 6e 67 2c 20 61 64 64 72 45 6e   pHaving, addrEn
22a99 64 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46  d, SQLITE_JUMPIF
22a9a 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 7d 0a 20  NULL);.      }. 
22a9b 20 20 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72       selectInner
22a9c 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20  Loop(pParse, p, 
22a9d 70 2d 3e 70 45 4c 69 73 74 2c 20 30 2c 20 30 2c  p->pEList, 0, 0,
22a9e 20 30 2c 20 2d 31 2c 20 0a 20 20 20 20 20 20 20   0, -1, .       
22a9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
22aa0 44 65 73 74 2c 20 61 64 64 72 45 6e 64 2c 20 61  Dest, addrEnd, a
22aa1 64 64 72 45 6e 64 2c 20 61 66 66 29 3b 0a 0a 20  ddrEnd, aff);.. 
22aa2 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
22aa3 4c 69 73 74 44 65 6c 65 74 65 28 70 44 65 6c 29  ListDelete(pDel)
22aa4 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
22aa5 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
22aa6 62 65 6c 28 76 2c 20 61 64 64 72 45 6e 64 29 3b  bel(v, addrEnd);
22aa7 0a 20 20 20 20 0a 20 20 7d 20 2f 2a 20 65 6e 64  .    .  } /* end
22aa8 69 66 20 61 67 67 72 65 67 61 74 65 20 71 75 65  if aggregate que
22aa9 72 79 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74  ry */..  /* If t
22aaa 68 65 72 65 20 69 73 20 61 6e 20 4f 52 44 45 52  here is an ORDER
22aab 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68 65 6e   BY clause, then
22aac 20 77 65 20 6e 65 65 64 20 74 6f 20 73 6f 72 74   we need to sort
22aad 20 74 68 65 20 72 65 73 75 6c 74 73 0a 20 20 2a   the results.  *
22aae 2a 20 61 6e 64 20 73 65 6e 64 20 74 68 65 6d 20  * and send them 
22aaf 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  to the callback 
22ab0 6f 6e 65 20 62 79 20 6f 6e 65 2e 0a 20 20 2a 2f  one by one..  */
22ab1 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20  .  if( pOrderBy 
22ab2 29 7b 0a 20 20 20 20 67 65 6e 65 72 61 74 65 53  ){.    generateS
22ab3 6f 72 74 54 61 69 6c 28 70 50 61 72 73 65 2c 20  ortTail(pParse, 
22ab4 70 2c 20 76 2c 20 70 45 4c 69 73 74 2d 3e 6e 45  p, v, pEList->nE
22ab5 78 70 72 2c 20 70 44 65 73 74 29 3b 0a 20 20 7d  xpr, pDest);.  }
22ab6 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
22ab7 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20  _OMIT_SUBQUERY. 
22ab8 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61 73 20   /* If this was 
22ab9 61 20 73 75 62 71 75 65 72 79 2c 20 77 65 20 68  a subquery, we h
22aba 61 76 65 20 6e 6f 77 20 63 6f 6e 76 65 72 74 65  ave now converte
22abb 64 20 74 68 65 20 73 75 62 71 75 65 72 79 20 69  d the subquery i
22abc 6e 74 6f 20 61 0a 20 20 2a 2a 20 74 65 6d 70 6f  nto a.  ** tempo
22abd 72 61 72 79 20 74 61 62 6c 65 2e 20 20 53 6f 20  rary table.  So 
22abe 73 65 74 20 74 68 65 20 53 72 63 4c 69 73 74 5f  set the SrcList_
22abf 69 74 65 6d 2e 69 73 50 6f 70 75 6c 61 74 65 64  item.isPopulated
22ac0 20 66 6c 61 67 20 74 6f 20 70 72 65 76 65 6e 74   flag to prevent
22ac1 0a 20 20 2a 2a 20 74 68 69 73 20 73 75 62 71 75  .  ** this subqu
22ac2 65 72 79 20 66 72 6f 6d 20 62 65 69 6e 67 20 65  ery from being e
22ac3 76 61 6c 75 61 74 65 64 20 61 67 61 69 6e 20 61  valuated again a
22ac4 6e 64 20 74 6f 20 66 6f 72 63 65 20 74 68 65 20  nd to force the 
22ac5 75 73 65 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20  use of.  ** the 
22ac6 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2e  temporary table.
22ac7 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 72  .  */.  if( pPar
22ac8 65 6e 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ent ){.    asser
22ac9 74 28 20 70 50 61 72 65 6e 74 2d 3e 70 53 72 63  t( pParent->pSrc
22aca 2d 3e 6e 53 72 63 3e 70 61 72 65 6e 74 54 61 62  ->nSrc>parentTab
22acb 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
22acc 70 50 61 72 65 6e 74 2d 3e 70 53 72 63 2d 3e 61  pParent->pSrc->a
22acd 5b 70 61 72 65 6e 74 54 61 62 5d 2e 70 53 65 6c  [parentTab].pSel
22ace 65 63 74 3d 3d 70 20 29 3b 0a 20 20 20 20 70 50  ect==p );.    pP
22acf 61 72 65 6e 74 2d 3e 70 53 72 63 2d 3e 61 5b 70  arent->pSrc->a[p
22ad0 61 72 65 6e 74 54 61 62 5d 2e 69 73 50 6f 70 75  arentTab].isPopu
22ad1 6c 61 74 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 23  lated = 1;.  }.#
22ad2 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 4a 75 6d 70  endif..  /* Jump
22ad3 20 68 65 72 65 20 74 6f 20 73 6b 69 70 20 74 68   here to skip th
22ad4 69 73 20 71 75 65 72 79 0a 20 20 2a 2f 0a 20 20  is query.  */.  
22ad5 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
22ad6 76 65 4c 61 62 65 6c 28 76 2c 20 69 45 6e 64 29  veLabel(v, iEnd)
22ad7 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 53 45 4c 45  ;..  /* The SELE
22ad8 43 54 20 77 61 73 20 73 75 63 63 65 73 73 66 75  CT was successfu
22ad9 6c 6c 79 20 63 6f 64 65 64 2e 20 20 20 53 65 74  lly coded.   Set
22ada 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65   the return code
22adb 20 74 6f 20 30 0a 20 20 2a 2a 20 74 6f 20 69 6e   to 0.  ** to in
22adc 64 69 63 61 74 65 20 6e 6f 20 65 72 72 6f 72 73  dicate no errors
22add 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 30 3b  ..  */.  rc = 0;
22ade 0a 0a 20 20 2f 2a 20 43 6f 6e 74 72 6f 6c 20 6a  ..  /* Control j
22adf 75 6d 70 73 20 74 6f 20 68 65 72 65 20 69 66 20  umps to here if 
22ae0 61 6e 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f  an error is enco
22ae1 75 6e 74 65 72 65 64 20 61 62 6f 76 65 2c 20 6f  untered above, o
22ae2 72 20 75 70 6f 6e 0a 20 20 2a 2a 20 73 75 63 63  r upon.  ** succ
22ae3 65 73 73 66 75 6c 20 63 6f 64 69 6e 67 20 6f 66  essful coding of
22ae4 20 74 68 65 20 53 45 4c 45 43 54 2e 0a 20 20 2a   the SELECT..  *
22ae5 2f 0a 73 65 6c 65 63 74 5f 65 6e 64 3a 0a 0a 20  /.select_end:.. 
22ae6 20 2f 2a 20 49 64 65 6e 74 69 66 79 20 63 6f 6c   /* Identify col
22ae7 75 6d 6e 20 6e 61 6d 65 73 20 69 66 20 77 65 20  umn names if we 
22ae8 77 69 6c 6c 20 62 65 20 75 73 69 6e 67 20 74 68  will be using th
22ae9 65 6d 20 69 6e 20 61 20 63 61 6c 6c 62 61 63 6b  em in a callback
22aea 2e 20 20 54 68 69 73 0a 20 20 2a 2a 20 73 74 65  .  This.  ** ste
22aeb 70 20 69 73 20 73 6b 69 70 70 65 64 20 69 66 20  p is skipped if 
22aec 74 68 65 20 6f 75 74 70 75 74 20 69 73 20 67 6f  the output is go
22aed 69 6e 67 20 74 6f 20 73 6f 6d 65 20 6f 74 68 65  ing to some othe
22aee 72 20 64 65 73 74 69 6e 61 74 69 6f 6e 2e 0a 20  r destination.. 
22aef 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   */.  if( rc==SQ
22af0 4c 49 54 45 5f 4f 4b 20 26 26 20 70 44 65 73 74  LITE_OK && pDest
22af1 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 43 61 6c  ->eDest==SRT_Cal
22af2 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 67 65 6e  lback ){.    gen
22af3 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73  erateColumnNames
22af4 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73  (pParse, pTabLis
22af5 74 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 7d 0a  t, pEList);.  }.
22af6 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
22af7 73 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 29 3b 0a  sAggInfo.aCol);.
22af8 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 73    sqlite3_free(s
22af9 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 29 3b 0a  AggInfo.aFunc);.
22afa 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
22afb 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
22afc 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 2a  TE_DEBUG)./*.***
22afd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22afe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22aff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22b01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
22b02 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  The following co
22b03 64 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 74  de is used for t
22b04 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67  esting and debug
22b05 67 69 6e 67 20 6f 6e 6c 79 2e 20 20 54 68 65 20  ging only.  The 
22b06 63 6f 64 65 0a 2a 2a 20 74 68 61 74 20 66 6f 6c  code.** that fol
22b07 6c 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20 61 70  lows does not ap
22b08 70 65 61 72 20 69 6e 20 6e 6f 72 6d 61 6c 20 62  pear in normal b
22b09 75 69 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  uilds..**.** The
22b0a 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  se routines are 
22b0b 75 73 65 64 20 74 6f 20 70 72 69 6e 74 20 6f 75  used to print ou
22b0c 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  t the content of
22b0d 20 61 6c 6c 20 6f 72 20 70 61 72 74 20 6f 66 20   all or part of 
22b0e 61 20 0a 2a 2a 20 70 61 72 73 65 20 73 74 72 75  a .** parse stru
22b0f 63 74 75 72 65 73 20 73 75 63 68 20 61 73 20 53  ctures such as S
22b10 65 6c 65 63 74 20 6f 72 20 45 78 70 72 2e 20 20  elect or Expr.  
22b11 53 75 63 68 20 70 72 69 6e 74 6f 75 74 73 20 61  Such printouts a
22b12 72 65 20 75 73 65 66 75 6c 0a 2a 2a 20 66 6f 72  re useful.** for
22b13 20 68 65 6c 70 69 6e 67 20 74 6f 20 75 6e 64 65   helping to unde
22b14 72 73 74 61 6e 64 20 77 68 61 74 20 69 73 20 68  rstand what is h
22b15 61 70 70 65 6e 69 6e 67 20 69 6e 73 69 64 65 20  appening inside 
22b16 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74  the code generat
22b17 6f 72 0a 2a 2a 20 64 75 72 69 6e 67 20 74 68 65  or.** during the
22b18 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 63 6f   execution of co
22b19 6d 70 6c 65 78 20 53 45 4c 45 43 54 20 73 74 61  mplex SELECT sta
22b1a 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54  tements..**.** T
22b1b 68 65 73 65 20 72 6f 75 74 69 6e 65 20 61 72 65  hese routine are
22b1c 20 6e 6f 74 20 63 61 6c 6c 65 64 20 61 6e 79 77   not called anyw
22b1d 68 65 72 65 20 66 72 6f 6d 20 77 69 74 68 69 6e  here from within
22b1e 20 74 68 65 20 6e 6f 72 6d 61 6c 0a 2a 2a 20 63   the normal.** c
22b1f 6f 64 65 20 62 61 73 65 2e 20 20 54 68 65 6e 20  ode base.  Then 
22b20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f 20  are intended to 
22b21 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77  be called from w
22b22 69 74 68 69 6e 20 74 68 65 20 64 65 62 75 67 67  ithin the debugg
22b23 65 72 0a 2a 2a 20 6f 72 20 66 72 6f 6d 20 74 65  er.** or from te
22b24 6d 70 6f 72 61 72 79 20 22 70 72 69 6e 74 66 22  mporary "printf"
22b25 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 73 65   statements inse
22b26 72 74 65 64 20 66 6f 72 20 64 65 62 75 67 67 69  rted for debuggi
22b27 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ng..*/.SQLITE_PR
22b28 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
22b29 65 33 50 72 69 6e 74 45 78 70 72 28 45 78 70 72  e3PrintExpr(Expr
22b2a 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 74   *p){.  if( p->t
22b2b 6f 6b 65 6e 2e 7a 20 26 26 20 70 2d 3e 74 6f 6b  oken.z && p->tok
22b2c 65 6e 2e 6e 3e 30 20 29 7b 0a 20 20 20 20 73 71  en.n>0 ){.    sq
22b2d 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
22b2e 28 22 28 25 2e 2a 73 22 2c 20 70 2d 3e 74 6f 6b  ("(%.*s", p->tok
22b2f 65 6e 2e 6e 2c 20 70 2d 3e 74 6f 6b 65 6e 2e 7a  en.n, p->token.z
22b30 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
22b31 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
22b32 74 66 28 22 28 25 64 22 2c 20 70 2d 3e 6f 70 29  tf("(%d", p->op)
22b33 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70  ;.  }.  if( p->p
22b34 4c 65 66 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  Left ){.    sqli
22b35 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
22b36 20 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   ");.    sqlite3
22b37 50 72 69 6e 74 45 78 70 72 28 70 2d 3e 70 4c 65  PrintExpr(p->pLe
22b38 66 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  ft);.  }.  if( p
22b39 2d 3e 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20  ->pRight ){.    
22b3a 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
22b3b 74 66 28 22 20 22 29 3b 0a 20 20 20 20 73 71 6c  tf(" ");.    sql
22b3c 69 74 65 33 50 72 69 6e 74 45 78 70 72 28 70 2d  ite3PrintExpr(p-
22b3d 3e 70 52 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20  >pRight);.  }.  
22b3e 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
22b3f 74 66 28 22 29 22 29 3b 0a 7d 0a 53 51 4c 49 54  tf(")");.}.SQLIT
22b40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
22b41 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72 4c  qlite3PrintExprL
22b42 69 73 74 28 45 78 70 72 4c 69 73 74 20 2a 70 4c  ist(ExprList *pL
22b43 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  ist){.  int i;. 
22b44 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73   for(i=0; i<pLis
22b45 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  t->nExpr; i++){.
22b46 20 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74      sqlite3Print
22b47 45 78 70 72 28 70 4c 69 73 74 2d 3e 61 5b 69 5d  Expr(pList->a[i]
22b48 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 69 66 28  .pExpr);.    if(
22b49 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2d   i<pList->nExpr-
22b4a 31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  1 ){.      sqlit
22b4b 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 2c  e3DebugPrintf(",
22b4c 20 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d   ");.    }.  }.}
22b4d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
22b4e 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 69 6e  void sqlite3Prin
22b4f 74 53 65 6c 65 63 74 28 53 65 6c 65 63 74 20 2a  tSelect(Select *
22b50 70 2c 20 69 6e 74 20 69 6e 64 65 6e 74 29 7b 0a  p, int indent){.
22b51 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
22b52 69 6e 74 66 28 22 25 2a 73 53 45 4c 45 43 54 28  intf("%*sSELECT(
22b53 25 70 29 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22  %p) ", indent, "
22b54 22 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  ", p);.  sqlite3
22b55 50 72 69 6e 74 45 78 70 72 4c 69 73 74 28 70 2d  PrintExprList(p-
22b56 3e 70 45 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69  >pEList);.  sqli
22b57 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
22b58 5c 6e 22 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70  \n");.  if( p->p
22b59 53 72 63 20 29 7b 0a 20 20 20 20 63 68 61 72 20  Src ){.    char 
22b5a 2a 7a 50 72 65 66 69 78 3b 0a 20 20 20 20 69 6e  *zPrefix;.    in
22b5b 74 20 69 3b 0a 20 20 20 20 7a 50 72 65 66 69 78  t i;.    zPrefix
22b5c 20 3d 20 22 46 52 4f 4d 22 3b 0a 20 20 20 20 66   = "FROM";.    f
22b5d 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 70 53 72  or(i=0; i<p->pSr
22b5e 63 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20  c->nSrc; i++){. 
22b5f 20 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c       struct SrcL
22b60 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20  ist_item *pItem 
22b61 3d 20 26 70 2d 3e 70 53 72 63 2d 3e 61 5b 69 5d  = &p->pSrc->a[i]
22b62 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ;.      sqlite3D
22b63 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73 20  ebugPrintf("%*s 
22b64 22 2c 20 69 6e 64 65 6e 74 2b 36 2c 20 7a 50 72  ", indent+6, zPr
22b65 65 66 69 78 29 3b 0a 20 20 20 20 20 20 7a 50 72  efix);.      zPr
22b66 65 66 69 78 20 3d 20 22 22 3b 0a 20 20 20 20 20  efix = "";.     
22b67 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c   if( pItem->pSel
22b68 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ect ){.        s
22b69 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
22b6a 66 28 22 28 5c 6e 22 29 3b 0a 20 20 20 20 20 20  f("(\n");.      
22b6b 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74 53 65    sqlite3PrintSe
22b6c 6c 65 63 74 28 70 49 74 65 6d 2d 3e 70 53 65 6c  lect(pItem->pSel
22b6d 65 63 74 2c 20 69 6e 64 65 6e 74 2b 31 30 29 3b  ect, indent+10);
22b6e 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
22b6f 44 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73  DebugPrintf("%*s
22b70 29 22 2c 20 69 6e 64 65 6e 74 2b 38 2c 20 22 22  )", indent+8, ""
22b71 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  );.      }else i
22b72 66 28 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20  f( pItem->zName 
22b73 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
22b74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 25  e3DebugPrintf("%
22b75 73 22 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65  s", pItem->zName
22b76 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
22b77 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 54 61 62   if( pItem->pTab
22b78 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
22b79 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
22b7a 28 74 61 62 6c 65 3a 20 25 73 29 22 2c 20 70 49  (table: %s)", pI
22b7b 74 65 6d 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65  tem->pTab->zName
22b7c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
22b7d 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 41 6c 69   if( pItem->zAli
22b7e 61 73 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  as ){.        sq
22b7f 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
22b80 28 22 20 41 53 20 25 73 22 2c 20 70 49 74 65 6d  (" AS %s", pItem
22b81 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 20 20 20  ->zAlias);.     
22b82 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 3c 70   }.      if( i<p
22b83 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 2d 31 20 29  ->pSrc->nSrc-1 )
22b84 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
22b85 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 2c 22  3DebugPrintf(","
22b86 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
22b87 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
22b88 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20 20 20 7d  ntf("\n");.    }
22b89 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 57  .  }.  if( p->pW
22b8a 68 65 72 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  here ){.    sqli
22b8b 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
22b8c 25 2a 73 20 57 48 45 52 45 20 22 2c 20 69 6e 64  %*s WHERE ", ind
22b8d 65 6e 74 2c 20 22 22 29 3b 0a 20 20 20 20 73 71  ent, "");.    sq
22b8e 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72 28 70  lite3PrintExpr(p
22b8f 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 20 20 73  ->pWhere);.    s
22b90 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
22b91 66 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a 20 20 69  f("\n");.  }.  i
22b92 66 28 20 70 2d 3e 70 47 72 6f 75 70 42 79 20 29  f( p->pGroupBy )
22b93 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62  {.    sqlite3Deb
22b94 75 67 50 72 69 6e 74 66 28 22 25 2a 73 20 47 52  ugPrintf("%*s GR
22b95 4f 55 50 20 42 59 20 22 2c 20 69 6e 64 65 6e 74  OUP BY ", indent
22b96 2c 20 22 22 29 3b 0a 20 20 20 20 73 71 6c 69 74  , "");.    sqlit
22b97 65 33 50 72 69 6e 74 45 78 70 72 4c 69 73 74 28  e3PrintExprList(
22b98 70 2d 3e 70 47 72 6f 75 70 42 79 29 3b 0a 20 20  p->pGroupBy);.  
22b99 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
22b9a 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a  intf("\n");.  }.
22b9b 20 20 69 66 28 20 70 2d 3e 70 48 61 76 69 6e 67    if( p->pHaving
22b9c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   ){.    sqlite3D
22b9d 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73 20  ebugPrintf("%*s 
22b9e 48 41 56 49 4e 47 20 22 2c 20 69 6e 64 65 6e 74  HAVING ", indent
22b9f 2c 20 22 22 29 3b 0a 20 20 20 20 73 71 6c 69 74  , "");.    sqlit
22ba0 65 33 50 72 69 6e 74 45 78 70 72 28 70 2d 3e 70  e3PrintExpr(p->p
22ba1 48 61 76 69 6e 67 29 3b 0a 20 20 20 20 73 71 6c  Having);.    sql
22ba2 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
22ba3 22 5c 6e 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28  "\n");.  }.  if(
22ba4 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a   p->pOrderBy ){.
22ba5 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
22ba6 50 72 69 6e 74 66 28 22 25 2a 73 20 4f 52 44 45  Printf("%*s ORDE
22ba7 52 20 42 59 20 22 2c 20 69 6e 64 65 6e 74 2c 20  R BY ", indent, 
22ba8 22 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  "");.    sqlite3
22ba9 50 72 69 6e 74 45 78 70 72 4c 69 73 74 28 70 2d  PrintExprList(p-
22baa 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 20 20  >pOrderBy);.    
22bab 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
22bac 74 66 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a 7d 0a  tf("\n");.  }.}.
22bad 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 73 74  /* End of the st
22bae 72 75 63 74 75 72 65 20 64 65 62 75 67 20 70 72  ructure debug pr
22baf 69 6e 74 69 6e 67 20 63 6f 64 65 0a 2a 2a 2a 2a  inting code.****
22bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22bb1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22bb2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22bb3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22bb4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69  *********/.#endi
22bb5 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c  f /* defined(SQL
22bb6 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 64 65 66  ITE_TEST) || def
22bb7 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
22bb8 47 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  G) */../********
22bb9 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 65  ****** End of se
22bba 6c 65 63 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  lect.c *********
22bbb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22bbc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22bbd 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
22bbe 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
22bbf 65 20 74 61 62 6c 65 2e 63 20 2a 2a 2a 2a 2a 2a  e table.c ******
22bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22bc1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22bc2 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
22bc3 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a  1 September 15.*
22bc4 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
22bc5 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
22bc6 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
22bc7 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
22bc8 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
22bc9 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
22bca 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
22bcb 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
22bcc 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
22bcd 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
22bce 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
22bcf 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
22bd0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
22bd1 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
22bd2 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
22bd3 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
22bd4 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
22bd5 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
22bd6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22bd7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22bd8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22bd9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
22bda 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
22bdb 69 6e 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ins the sqlite3_
22bdc 67 65 74 5f 74 61 62 6c 65 28 29 20 61 6e 64 20  get_table() and 
22bdd 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62  sqlite3_free_tab
22bde 6c 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63  le().** interfac
22bdf 65 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 68 65  e routines.  The
22be0 73 65 20 61 72 65 20 6a 75 73 74 20 77 72 61 70  se are just wrap
22be1 70 65 72 73 20 61 72 6f 75 6e 64 20 74 68 65 20  pers around the 
22be2 6d 61 69 6e 0a 2a 2a 20 69 6e 74 65 72 66 61 63  main.** interfac
22be3 65 20 72 6f 75 74 69 6e 65 20 6f 66 20 73 71 6c  e routine of sql
22be4 69 74 65 33 5f 65 78 65 63 28 29 2e 0a 2a 2a 0a  ite3_exec()..**.
22be5 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
22be6 73 20 61 72 65 20 69 6e 20 61 20 73 65 70 61 72  s are in a separ
22be7 61 74 65 20 66 69 6c 65 73 20 73 6f 20 74 68 61  ate files so tha
22be8 74 20 74 68 65 79 20 77 69 6c 6c 20 6e 6f 74 20  t they will not 
22be9 62 65 20 6c 69 6e 6b 65 64 0a 2a 2a 20 69 66 20  be linked.** if 
22bea 74 68 65 79 20 61 72 65 20 6e 6f 74 20 75 73 65  they are not use
22beb 64 2e 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  d..*/..#ifndef S
22bec 51 4c 49 54 45 5f 4f 4d 49 54 5f 47 45 54 5f 54  QLITE_OMIT_GET_T
22bed 41 42 4c 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ABLE../*.** This
22bee 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73   structure is us
22bef 65 64 20 74 6f 20 70 61 73 73 20 64 61 74 61 20  ed to pass data 
22bf0 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 67 65 74  from sqlite3_get
22bf1 5f 74 61 62 6c 65 28 29 20 74 68 72 6f 75 67 68  _table() through
22bf2 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62  .** to the callb
22bf3 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  ack function is 
22bf4 75 73 65 73 20 74 6f 20 62 75 69 6c 64 20 74 68  uses to build th
22bf5 65 20 72 65 73 75 6c 74 2e 0a 2a 2f 0a 74 79 70  e result..*/.typ
22bf6 65 64 65 66 20 73 74 72 75 63 74 20 54 61 62 52  edef struct TabR
22bf7 65 73 75 6c 74 20 7b 0a 20 20 63 68 61 72 20 2a  esult {.  char *
22bf8 2a 61 7a 52 65 73 75 6c 74 3b 0a 20 20 63 68 61  *azResult;.  cha
22bf9 72 20 2a 7a 45 72 72 4d 73 67 3b 0a 20 20 69 6e  r *zErrMsg;.  in
22bfa 74 20 6e 52 65 73 75 6c 74 3b 0a 20 20 69 6e 74  t nResult;.  int
22bfb 20 6e 41 6c 6c 6f 63 3b 0a 20 20 69 6e 74 20 6e   nAlloc;.  int n
22bfc 52 6f 77 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 75  Row;.  int nColu
22bfd 6d 6e 3b 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b  mn;.  int nData;
22bfe 0a 20 20 69 6e 74 20 72 63 3b 0a 7d 20 54 61 62  .  int rc;.} Tab
22bff 52 65 73 75 6c 74 3b 0a 0a 2f 2a 0a 2a 2a 20 54  Result;../*.** T
22c00 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
22c01 61 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 20 65  alled once for e
22c02 61 63 68 20 72 6f 77 20 69 6e 20 74 68 65 20 72  ach row in the r
22c03 65 73 75 6c 74 20 74 61 62 6c 65 2e 20 20 49 74  esult table.  It
22c04 73 20 6a 6f 62 0a 2a 2a 20 69 73 20 74 6f 20 66  s job.** is to f
22c05 69 6c 6c 20 69 6e 20 74 68 65 20 54 61 62 52 65  ill in the TabRe
22c06 73 75 6c 74 20 73 74 72 75 63 74 75 72 65 20 61  sult structure a
22c07 70 70 72 6f 70 72 69 61 74 65 6c 79 2c 20 61 6c  ppropriately, al
22c08 6c 6f 63 61 74 69 6e 67 20 6e 65 77 0a 2a 2a 20  locating new.** 
22c09 6d 65 6d 6f 72 79 20 61 73 20 6e 65 63 65 73 73  memory as necess
22c0a 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ary..*/.static i
22c0b 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  nt sqlite3_get_t
22c0c 61 62 6c 65 5f 63 62 28 76 6f 69 64 20 2a 70 41  able_cb(void *pA
22c0d 72 67 2c 20 69 6e 74 20 6e 43 6f 6c 2c 20 63 68  rg, int nCol, ch
22c0e 61 72 20 2a 2a 61 72 67 76 2c 20 63 68 61 72 20  ar **argv, char 
22c0f 2a 2a 63 6f 6c 76 29 7b 0a 20 20 54 61 62 52 65  **colv){.  TabRe
22c10 73 75 6c 74 20 2a 70 20 3d 20 28 54 61 62 52 65  sult *p = (TabRe
22c11 73 75 6c 74 2a 29 70 41 72 67 3b 0a 20 20 69 6e  sult*)pArg;.  in
22c12 74 20 6e 65 65 64 3b 0a 20 20 69 6e 74 20 69 3b  t need;.  int i;
22c13 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 2f  .  char *z;..  /
22c14 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 72  * Make sure ther
22c15 65 20 69 73 20 65 6e 6f 75 67 68 20 73 70 61 63  e is enough spac
22c16 65 20 69 6e 20 70 2d 3e 61 7a 52 65 73 75 6c 74  e in p->azResult
22c17 20 74 6f 20 68 6f 6c 64 20 65 76 65 72 79 74 68   to hold everyth
22c18 69 6e 67 0a 20 20 2a 2a 20 77 65 20 6e 65 65 64  ing.  ** we need
22c19 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 66 72 6f   to remember fro
22c1a 6d 20 74 68 69 73 20 69 6e 76 6f 63 61 74 69 6f  m this invocatio
22c1b 6e 20 6f 66 20 74 68 65 20 63 61 6c 6c 62 61 63  n of the callbac
22c1c 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d  k..  */.  if( p-
22c1d 3e 6e 52 6f 77 3d 3d 30 20 26 26 20 61 72 67 76  >nRow==0 && argv
22c1e 21 3d 30 20 29 7b 0a 20 20 20 20 6e 65 65 64 20  !=0 ){.    need 
22c1f 3d 20 6e 43 6f 6c 2a 32 3b 0a 20 20 7d 65 6c 73  = nCol*2;.  }els
22c20 65 7b 0a 20 20 20 20 6e 65 65 64 20 3d 20 6e 43  e{.    need = nC
22c21 6f 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d  ol;.  }.  if( p-
22c22 3e 6e 44 61 74 61 20 2b 20 6e 65 65 64 20 3e 3d  >nData + need >=
22c23 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20   p->nAlloc ){.  
22c24 20 20 63 68 61 72 20 2a 2a 61 7a 4e 65 77 3b 0a    char **azNew;.
22c25 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20      p->nAlloc = 
22c26 70 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b 20 6e 65  p->nAlloc*2 + ne
22c27 65 64 20 2b 20 31 3b 0a 20 20 20 20 61 7a 4e 65  ed + 1;.    azNe
22c28 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  w = sqlite3_real
22c29 6c 6f 63 28 20 70 2d 3e 61 7a 52 65 73 75 6c 74  loc( p->azResult
22c2a 2c 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a  , sizeof(char*)*
22c2b 70 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 20  p->nAlloc );.   
22c2c 20 69 66 28 20 61 7a 4e 65 77 3d 3d 30 20 29 20   if( azNew==0 ) 
22c2d 67 6f 74 6f 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c  goto malloc_fail
22c2e 65 64 3b 0a 20 20 20 20 70 2d 3e 61 7a 52 65 73  ed;.    p->azRes
22c2f 75 6c 74 20 3d 20 61 7a 4e 65 77 3b 0a 20 20 7d  ult = azNew;.  }
22c30 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69  ..  /* If this i
22c31 73 20 74 68 65 20 66 69 72 73 74 20 72 6f 77 2c  s the first row,
22c32 20 74 68 65 6e 20 67 65 6e 65 72 61 74 65 20 61   then generate a
22c33 6e 20 65 78 74 72 61 20 72 6f 77 20 63 6f 6e 74  n extra row cont
22c34 61 69 6e 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20  aining.  ** the 
22c35 6e 61 6d 65 73 20 6f 66 20 61 6c 6c 20 63 6f 6c  names of all col
22c36 75 6d 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  umns..  */.  if(
22c37 20 70 2d 3e 6e 52 6f 77 3d 3d 30 20 29 7b 0a 20   p->nRow==0 ){. 
22c38 20 20 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20     p->nColumn = 
22c39 6e 43 6f 6c 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nCol;.    for(i=
22c3a 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  0; i<nCol; i++){
22c3b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74  .      z = sqlit
22c3c 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c  e3_mprintf("%s",
22c3d 20 63 6f 6c 76 5b 69 5d 29 3b 0a 20 20 20 20 20   colv[i]);.     
22c3e 20 69 66 28 20 7a 3d 3d 30 20 29 20 67 6f 74 6f   if( z==0 ) goto
22c3f 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a   malloc_failed;.
22c40 20 20 20 20 20 20 70 2d 3e 61 7a 52 65 73 75 6c        p->azResul
22c41 74 5b 70 2d 3e 6e 44 61 74 61 2b 2b 5d 20 3d 20  t[p->nData++] = 
22c42 7a 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  z;.    }.  }else
22c43 20 69 66 28 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 21   if( p->nColumn!
22c44 3d 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 73 71 6c  =nCol ){.    sql
22c45 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 45 72  ite3_free(p->zEr
22c46 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45  rMsg);.    p->zE
22c47 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 5f  rrMsg = sqlite3_
22c48 6d 70 72 69 6e 74 66 28 0a 20 20 20 20 20 20 20  mprintf(.       
22c49 22 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62  "sqlite3_get_tab
22c4a 6c 65 28 29 20 63 61 6c 6c 65 64 20 77 69 74 68  le() called with
22c4b 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 69 6e 63   two or more inc
22c4c 6f 6d 70 61 74 69 62 6c 65 20 71 75 65 72 69 65  ompatible querie
22c4d 73 22 0a 20 20 20 20 29 3b 0a 20 20 20 20 70 2d  s".    );.    p-
22c4e 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52  >rc = SQLITE_ERR
22c4f 4f 52 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  OR;.    return 1
22c50 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79  ;.  }..  /* Copy
22c51 20 6f 76 65 72 20 74 68 65 20 72 6f 77 20 64 61   over the row da
22c52 74 61 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61 72  ta.  */.  if( ar
22c53 67 76 21 3d 30 20 29 7b 0a 20 20 20 20 66 6f 72  gv!=0 ){.    for
22c54 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b  (i=0; i<nCol; i+
22c55 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 72  +){.      if( ar
22c56 67 76 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20  gv[i]==0 ){.    
22c57 20 20 20 20 7a 20 3d 20 30 3b 0a 20 20 20 20 20      z = 0;.     
22c58 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
22c59 69 6e 74 20 6e 20 3d 20 73 74 72 6c 65 6e 28 61  int n = strlen(a
22c5a 72 67 76 5b 69 5d 29 2b 31 3b 0a 20 20 20 20 20  rgv[i])+1;.     
22c5b 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 6d     z = sqlite3_m
22c5c 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 20 20  alloc( n );.    
22c5d 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 67      if( z==0 ) g
22c5e 6f 74 6f 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65  oto malloc_faile
22c5f 64 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  d;.        memcp
22c60 79 28 7a 2c 20 61 72 67 76 5b 69 5d 2c 20 6e 29  y(z, argv[i], n)
22c61 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
22c62 70 2d 3e 61 7a 52 65 73 75 6c 74 5b 70 2d 3e 6e  p->azResult[p->n
22c63 44 61 74 61 2b 2b 5d 20 3d 20 7a 3b 0a 20 20 20  Data++] = z;.   
22c64 20 7d 0a 20 20 20 20 70 2d 3e 6e 52 6f 77 2b 2b   }.    p->nRow++
22c65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
22c66 3b 0a 0a 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64  ;..malloc_failed
22c67 3a 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49  :.  p->rc = SQLI
22c68 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 72 65 74 75  TE_NOMEM;.  retu
22c69 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51  rn 1;.}../*.** Q
22c6a 75 65 72 79 20 74 68 65 20 64 61 74 61 62 61 73  uery the databas
22c6b 65 2e 20 20 42 75 74 20 69 6e 73 74 65 61 64 20  e.  But instead 
22c6c 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 61 20 63 61  of invoking a ca
22c6d 6c 6c 62 61 63 6b 20 66 6f 72 20 65 61 63 68 20  llback for each 
22c6e 72 6f 77 2c 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29  row,.** malloc()
22c6f 20 66 6f 72 20 73 70 61 63 65 20 74 6f 20 68 6f   for space to ho
22c70 6c 64 20 74 68 65 20 72 65 73 75 6c 74 20 61 6e  ld the result an
22c71 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 6e 74  d return the ent
22c72 69 72 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 61  ire results.** a
22c73 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e  t the conclusion
22c74 20 6f 66 20 74 68 65 20 63 61 6c 6c 2e 0a 2a 2a   of the call..**
22c75 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 74  .** The result t
22c76 68 61 74 20 69 73 20 77 72 69 74 74 65 6e 20 74  hat is written t
22c77 6f 20 2a 2a 2a 70 61 7a 52 65 73 75 6c 74 20 69  o ***pazResult i
22c78 73 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79  s held in memory
22c79 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f   obtained.** fro
22c7a 6d 20 6d 61 6c 6c 6f 63 28 29 2e 20 20 42 75 74  m malloc().  But
22c7b 20 74 68 65 20 63 61 6c 6c 65 72 20 63 61 6e 6e   the caller cann
22c7c 6f 74 20 66 72 65 65 20 74 68 69 73 20 6d 65 6d  ot free this mem
22c7d 6f 72 79 20 64 69 72 65 63 74 6c 79 2e 20 20 0a  ory directly.  .
22c7e 2a 2a 20 49 6e 73 74 65 61 64 2c 20 74 68 65 20  ** Instead, the 
22c7f 65 6e 74 69 72 65 20 74 61 62 6c 65 20 73 68 6f  entire table sho
22c80 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 74 6f  uld be passed to
22c81 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61   sqlite3_free_ta
22c82 62 6c 65 28 29 20 77 68 65 6e 0a 2a 2a 20 74 68  ble() when.** th
22c83 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 64  e calling proced
22c84 75 72 65 20 69 73 20 66 69 6e 69 73 68 65 64 20  ure is finished 
22c85 75 73 69 6e 67 20 69 74 2e 0a 2a 2f 0a 53 51 4c  using it..*/.SQL
22c86 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
22c87 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 0a 20  te3_get_table(. 
22c88 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
22c89 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
22c8a 54 68 65 20 64 61 74 61 62 61 73 65 20 6f 6e 20  The database on 
22c8b 77 68 69 63 68 20 74 68 65 20 53 51 4c 20 65 78  which the SQL ex
22c8c 65 63 75 74 65 73 20 2a 2f 0a 20 20 63 6f 6e 73  ecutes */.  cons
22c8d 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20  t char *zSql,   
22c8e 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53          /* The S
22c8f 51 4c 20 74 6f 20 62 65 20 65 78 65 63 75 74 65  QL to be execute
22c90 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 2a 70  d */.  char ***p
22c91 61 7a 52 65 73 75 6c 74 2c 20 20 20 20 20 20 20  azResult,       
22c92 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
22c93 72 65 73 75 6c 74 20 74 61 62 6c 65 20 68 65 72  result table her
22c94 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52 6f  e */.  int *pnRo
22c95 77 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  w,              
22c96 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
22c97 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69  number of rows i
22c98 6e 20 74 68 65 20 72 65 73 75 6c 74 20 68 65 72  n the result her
22c99 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f  e */.  int *pnCo
22c9a 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20 20  lumn,           
22c9b 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
22c9c 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
22c9d 73 20 6f 66 20 72 65 73 75 6c 74 20 68 65 72 65  s of result here
22c9e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45   */.  char **pzE
22c9f 72 72 4d 73 67 20 20 20 20 20 20 20 20 20 20 20  rrMsg           
22ca0 20 20 2f 2a 20 57 72 69 74 65 20 65 72 72 6f 72    /* Write error
22ca1 20 6d 65 73 73 61 67 65 73 20 68 65 72 65 20 2a   messages here *
22ca2 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  /.){.  int rc;. 
22ca3 20 54 61 62 52 65 73 75 6c 74 20 72 65 73 3b 0a   TabResult res;.
22ca4 0a 20 20 2a 70 61 7a 52 65 73 75 6c 74 20 3d 20  .  *pazResult = 
22ca5 30 3b 0a 20 20 69 66 28 20 70 6e 43 6f 6c 75 6d  0;.  if( pnColum
22ca6 6e 20 29 20 2a 70 6e 43 6f 6c 75 6d 6e 20 3d 20  n ) *pnColumn = 
22ca7 30 3b 0a 20 20 69 66 28 20 70 6e 52 6f 77 20 29  0;.  if( pnRow )
22ca8 20 2a 70 6e 52 6f 77 20 3d 20 30 3b 0a 20 20 72   *pnRow = 0;.  r
22ca9 65 73 2e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a  es.zErrMsg = 0;.
22caa 20 20 72 65 73 2e 6e 52 65 73 75 6c 74 20 3d 20    res.nResult = 
22cab 30 3b 0a 20 20 72 65 73 2e 6e 52 6f 77 20 3d 20  0;.  res.nRow = 
22cac 30 3b 0a 20 20 72 65 73 2e 6e 43 6f 6c 75 6d 6e  0;.  res.nColumn
22cad 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e 44 61 74   = 0;.  res.nDat
22cae 61 20 3d 20 31 3b 0a 20 20 72 65 73 2e 6e 41 6c  a = 1;.  res.nAl
22caf 6c 6f 63 20 3d 20 32 30 3b 0a 20 20 72 65 73 2e  loc = 20;.  res.
22cb0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
22cb1 20 20 72 65 73 2e 61 7a 52 65 73 75 6c 74 20 3d    res.azResult =
22cb2 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
22cb3 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a 72 65  sizeof(char*)*re
22cb4 73 2e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 69 66  s.nAlloc );.  if
22cb5 28 20 72 65 73 2e 61 7a 52 65 73 75 6c 74 3d 3d  ( res.azResult==
22cb6 30 20 29 7b 0a 20 20 20 20 20 64 62 2d 3e 65 72  0 ){.     db->er
22cb7 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4e  rCode = SQLITE_N
22cb8 4f 4d 45 4d 3b 0a 20 20 20 20 20 72 65 74 75 72  OMEM;.     retur
22cb9 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
22cba 20 20 7d 0a 20 20 72 65 73 2e 61 7a 52 65 73 75    }.  res.azResu
22cbb 6c 74 5b 30 5d 20 3d 20 30 3b 0a 20 20 72 63 20  lt[0] = 0;.  rc 
22cbc 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64  = sqlite3_exec(d
22cbd 62 2c 20 7a 53 71 6c 2c 20 73 71 6c 69 74 65 33  b, zSql, sqlite3
22cbe 5f 67 65 74 5f 74 61 62 6c 65 5f 63 62 2c 20 26  _get_table_cb, &
22cbf 72 65 73 2c 20 70 7a 45 72 72 4d 73 67 29 3b 0a  res, pzErrMsg);.
22cc0 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
22cc1 28 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 30 5d  (res.azResult[0]
22cc2 29 3e 3d 20 73 69 7a 65 6f 66 28 72 65 73 2e 6e  )>= sizeof(res.n
22cc3 44 61 74 61 29 20 29 3b 0a 20 20 72 65 73 2e 61  Data) );.  res.a
22cc4 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20 28 63 68  zResult[0] = (ch
22cc5 61 72 2a 29 72 65 73 2e 6e 44 61 74 61 3b 0a 20  ar*)res.nData;. 
22cc6 20 69 66 28 20 28 72 63 26 30 78 66 66 29 3d 3d   if( (rc&0xff)==
22cc7 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 29 7b 0a  SQLITE_ABORT ){.
22cc8 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
22cc9 5f 74 61 62 6c 65 28 26 72 65 73 2e 61 7a 52 65  _table(&res.azRe
22cca 73 75 6c 74 5b 31 5d 29 3b 0a 20 20 20 20 69 66  sult[1]);.    if
22ccb 28 20 72 65 73 2e 7a 45 72 72 4d 73 67 20 29 7b  ( res.zErrMsg ){
22ccc 0a 20 20 20 20 20 20 69 66 28 20 70 7a 45 72 72  .      if( pzErr
22ccd 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73  Msg ){.        s
22cce 71 6c 69 74 65 33 5f 66 72 65 65 28 2a 70 7a 45  qlite3_free(*pzE
22ccf 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 20 20  rrMsg);.        
22cd0 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69  *pzErrMsg = sqli
22cd1 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22  te3_mprintf("%s"
22cd2 2c 72 65 73 2e 7a 45 72 72 4d 73 67 29 3b 0a 20  ,res.zErrMsg);. 
22cd3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
22cd4 69 74 65 33 5f 66 72 65 65 28 72 65 73 2e 7a 45  ite3_free(res.zE
22cd5 72 72 4d 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20  rrMsg);.    }.  
22cd6 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20    db->errCode = 
22cd7 72 65 73 2e 72 63 3b 20 20 2f 2a 20 41 73 73 75  res.rc;  /* Assu
22cd8 6d 65 20 33 32 2d 62 69 74 20 61 73 73 69 67 6e  me 32-bit assign
22cd9 6d 65 6e 74 20 69 73 20 61 74 6f 6d 69 63 20 2a  ment is atomic *
22cda 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 72 65 73  /.    return res
22cdb 2e 72 63 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  .rc;.  }.  sqlit
22cdc 65 33 5f 66 72 65 65 28 72 65 73 2e 7a 45 72 72  e3_free(res.zErr
22cdd 4d 73 67 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  Msg);.  if( rc!=
22cde 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
22cdf 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61   sqlite3_free_ta
22ce0 62 6c 65 28 26 72 65 73 2e 61 7a 52 65 73 75 6c  ble(&res.azResul
22ce1 74 5b 31 5d 29 3b 0a 20 20 20 20 72 65 74 75 72  t[1]);.    retur
22ce2 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20  n rc;.  }.  if( 
22ce3 72 65 73 2e 6e 41 6c 6c 6f 63 3e 72 65 73 2e 6e  res.nAlloc>res.n
22ce4 44 61 74 61 20 29 7b 0a 20 20 20 20 63 68 61 72  Data ){.    char
22ce5 20 2a 2a 61 7a 4e 65 77 3b 0a 20 20 20 20 61 7a   **azNew;.    az
22ce6 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65  New = sqlite3_re
22ce7 61 6c 6c 6f 63 28 20 72 65 73 2e 61 7a 52 65 73  alloc( res.azRes
22ce8 75 6c 74 2c 20 73 69 7a 65 6f 66 28 63 68 61 72  ult, sizeof(char
22ce9 2a 29 2a 28 72 65 73 2e 6e 44 61 74 61 2b 31 29  *)*(res.nData+1)
22cea 20 29 3b 0a 20 20 20 20 69 66 28 20 61 7a 4e 65   );.    if( azNe
22ceb 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  w==0 ){.      sq
22cec 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65  lite3_free_table
22ced 28 26 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31  (&res.azResult[1
22cee 5d 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 65 72  ]);.      db->er
22cef 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4e  rCode = SQLITE_N
22cf0 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 72 65 74 75  OMEM;.      retu
22cf1 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
22cf2 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 73 2e 6e  .    }.    res.n
22cf3 41 6c 6c 6f 63 20 3d 20 72 65 73 2e 6e 44 61 74  Alloc = res.nDat
22cf4 61 2b 31 3b 0a 20 20 20 20 72 65 73 2e 61 7a 52  a+1;.    res.azR
22cf5 65 73 75 6c 74 20 3d 20 61 7a 4e 65 77 3b 0a 20  esult = azNew;. 
22cf6 20 7d 0a 20 20 2a 70 61 7a 52 65 73 75 6c 74 20   }.  *pazResult 
22cf7 3d 20 26 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b  = &res.azResult[
22cf8 31 5d 3b 0a 20 20 69 66 28 20 70 6e 43 6f 6c 75  1];.  if( pnColu
22cf9 6d 6e 20 29 20 2a 70 6e 43 6f 6c 75 6d 6e 20 3d  mn ) *pnColumn =
22cfa 20 72 65 73 2e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20   res.nColumn;.  
22cfb 69 66 28 20 70 6e 52 6f 77 20 29 20 2a 70 6e 52  if( pnRow ) *pnR
22cfc 6f 77 20 3d 20 72 65 73 2e 6e 52 6f 77 3b 0a 20  ow = res.nRow;. 
22cfd 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
22cfe 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
22cff 65 20 66 72 65 65 73 20 74 68 65 20 73 70 61 63  e frees the spac
22d00 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f 67 65  e the sqlite3_ge
22d01 74 5f 74 61 62 6c 65 28 29 20 6d 61 6c 6c 6f 63  t_table() malloc
22d02 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ed..*/.SQLITE_AP
22d03 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 66  I void sqlite3_f
22d04 72 65 65 5f 74 61 62 6c 65 28 0a 20 20 63 68 61  ree_table(.  cha
22d05 72 20 2a 2a 61 7a 52 65 73 75 6c 74 20 20 20 20  r **azResult    
22d06 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c          /* Resul
22d07 74 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20  t returned from 
22d08 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 67 65 74  from sqlite3_get
22d09 5f 74 61 62 6c 65 28 29 20 2a 2f 0a 29 7b 0a 20  _table() */.){. 
22d0a 20 69 66 28 20 61 7a 52 65 73 75 6c 74 20 29 7b   if( azResult ){
22d0b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20  .    int i, n;. 
22d0c 20 20 20 61 7a 52 65 73 75 6c 74 2d 2d 3b 0a 20     azResult--;. 
22d0d 20 20 20 61 73 73 65 72 74 28 20 61 7a 52 65 73     assert( azRes
22d0e 75 6c 74 21 3d 30 20 29 3b 0a 20 20 20 20 6e 20  ult!=0 );.    n 
22d0f 3d 20 28 69 6e 74 29 61 7a 52 65 73 75 6c 74 5b  = (int)azResult[
22d10 30 5d 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b  0];.    for(i=1;
22d11 20 69 3c 6e 3b 20 69 2b 2b 29 7b 20 69 66 28 20   i<n; i++){ if( 
22d12 61 7a 52 65 73 75 6c 74 5b 69 5d 20 29 20 73 71  azResult[i] ) sq
22d13 6c 69 74 65 33 5f 66 72 65 65 28 61 7a 52 65 73  lite3_free(azRes
22d14 75 6c 74 5b 69 5d 29 3b 20 7d 0a 20 20 20 20 73  ult[i]); }.    s
22d15 71 6c 69 74 65 33 5f 66 72 65 65 28 61 7a 52 65  qlite3_free(azRe
22d16 73 75 6c 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 65  sult);.  }.}..#e
22d17 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
22d18 4d 49 54 5f 47 45 54 5f 54 41 42 4c 45 20 2a 2f  MIT_GET_TABLE */
22d19 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
22d1a 2a 20 45 6e 64 20 6f 66 20 74 61 62 6c 65 2e 63  * End of table.c
22d1b 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
22d1c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d1e 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
22d1f 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 74 72 69  * Begin file tri
22d20 67 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  gger.c *********
22d21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d22 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d23 2f 0a 2f 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  /./*.**.** The a
22d24 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
22d25 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
22d26 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
22d27 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
22d28 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
22d29 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
22d2a 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
22d2b 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
22d2c 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
22d2d 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
22d2e 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
22d2f 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
22d30 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
22d31 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
22d32 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
22d33 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
22d34 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
22d35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d38 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d39 2a 2a 2a 0a 2a 0a 2a 2f 0a 0a 23 69 66 6e 64 65  ***.*.*/..#ifnde
22d3a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
22d3b 49 47 47 45 52 0a 2f 2a 0a 2a 2a 20 44 65 6c 65  IGGER./*.** Dele
22d3c 74 65 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74  te a linked list
22d3d 20 6f 66 20 54 72 69 67 67 65 72 53 74 65 70 20   of TriggerStep 
22d3e 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 53  structures..*/.S
22d3f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
22d40 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  id sqlite3Delete
22d41 54 72 69 67 67 65 72 53 74 65 70 28 54 72 69 67  TriggerStep(Trig
22d42 67 65 72 53 74 65 70 20 2a 70 54 72 69 67 67 65  gerStep *pTrigge
22d43 72 53 74 65 70 29 7b 0a 20 20 77 68 69 6c 65 28  rStep){.  while(
22d44 20 70 54 72 69 67 67 65 72 53 74 65 70 20 29 7b   pTriggerStep ){
22d45 0a 20 20 20 20 54 72 69 67 67 65 72 53 74 65 70  .    TriggerStep
22d46 20 2a 20 70 54 6d 70 20 3d 20 70 54 72 69 67 67   * pTmp = pTrigg
22d47 65 72 53 74 65 70 3b 0a 20 20 20 20 70 54 72 69  erStep;.    pTri
22d48 67 67 65 72 53 74 65 70 20 3d 20 70 54 72 69 67  ggerStep = pTrig
22d49 67 65 72 53 74 65 70 2d 3e 70 4e 65 78 74 3b 0a  gerStep->pNext;.
22d4a 0a 20 20 20 20 69 66 28 20 70 54 6d 70 2d 3e 74  .    if( pTmp->t
22d4b 61 72 67 65 74 2e 64 79 6e 20 29 20 73 71 6c 69  arget.dyn ) sqli
22d4c 74 65 33 5f 66 72 65 65 28 28 63 68 61 72 2a 29  te3_free((char*)
22d4d 70 54 6d 70 2d 3e 74 61 72 67 65 74 2e 7a 29 3b  pTmp->target.z);
22d4e 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
22d4f 44 65 6c 65 74 65 28 70 54 6d 70 2d 3e 70 57 68  Delete(pTmp->pWh
22d50 65 72 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ere);.    sqlite
22d51 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
22d52 70 54 6d 70 2d 3e 70 45 78 70 72 4c 69 73 74 29  pTmp->pExprList)
22d53 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c  ;.    sqlite3Sel
22d54 65 63 74 44 65 6c 65 74 65 28 70 54 6d 70 2d 3e  ectDelete(pTmp->
22d55 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 73 71  pSelect);.    sq
22d56 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74  lite3IdListDelet
22d57 65 28 70 54 6d 70 2d 3e 70 49 64 4c 69 73 74 29  e(pTmp->pIdList)
22d58 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ;..    sqlite3_f
22d59 72 65 65 28 70 54 6d 70 29 3b 0a 20 20 7d 0a 7d  ree(pTmp);.  }.}
22d5a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ../*.** This is 
22d5b 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61  called by the pa
22d5c 72 73 65 72 20 77 68 65 6e 20 69 74 20 73 65 65  rser when it see
22d5d 73 20 61 20 43 52 45 41 54 45 20 54 52 49 47 47  s a CREATE TRIGG
22d5e 45 52 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20  ER statement.** 
22d5f 75 70 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 20  up to the point 
22d60 6f 66 20 74 68 65 20 42 45 47 49 4e 20 62 65 66  of the BEGIN bef
22d61 6f 72 65 20 74 68 65 20 74 72 69 67 67 65 72 20  ore the trigger 
22d62 61 63 74 69 6f 6e 73 2e 20 20 41 20 54 72 69 67  actions.  A Trig
22d63 67 65 72 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  ger.** structure
22d64 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 61   is generated ba
22d65 73 65 64 20 6f 6e 20 74 68 65 20 69 6e 66 6f 72  sed on the infor
22d66 6d 61 74 69 6f 6e 20 61 76 61 69 6c 61 62 6c 65  mation available
22d67 20 61 6e 64 20 73 74 6f 72 65 64 0a 2a 2a 20 69   and stored.** i
22d68 6e 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72  n pParse->pNewTr
22d69 69 67 67 65 72 2e 20 20 41 66 74 65 72 20 74 68  igger.  After th
22d6a 65 20 74 72 69 67 67 65 72 20 61 63 74 69 6f 6e  e trigger action
22d6b 73 20 68 61 76 65 20 62 65 65 6e 20 70 61 72 73  s have been pars
22d6c 65 64 2c 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74  ed, the.** sqlit
22d6d 65 33 46 69 6e 69 73 68 54 72 69 67 67 65 72 28  e3FinishTrigger(
22d6e 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  ) function is ca
22d6f 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65  lled to complete
22d70 20 74 68 65 20 74 72 69 67 67 65 72 0a 2a 2a 20   the trigger.** 
22d71 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 70 72 6f  construction pro
22d72 63 65 73 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  cess..*/.SQLITE_
22d73 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
22d74 69 74 65 33 42 65 67 69 6e 54 72 69 67 67 65 72  ite3BeginTrigger
22d75 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
22d76 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70  e,      /* The p
22d77 61 72 73 65 20 63 6f 6e 74 65 78 74 20 6f 66 20  arse context of 
22d78 74 68 65 20 43 52 45 41 54 45 20 54 52 49 47 47  the CREATE TRIGG
22d79 45 52 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  ER statement */.
22d7a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c    Token *pName1,
22d7b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d        /* The nam
22d7c 65 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72  e of the trigger
22d7d 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
22d7e 6d 65 32 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  me2,      /* The
22d7f 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 72 69   name of the tri
22d80 67 67 65 72 20 2a 2f 0a 20 20 69 6e 74 20 74 72  gger */.  int tr
22d81 5f 74 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  _tm,          /*
22d82 20 4f 6e 65 20 6f 66 20 54 4b 5f 42 45 46 4f 52   One of TK_BEFOR
22d83 45 2c 20 54 4b 5f 41 46 54 45 52 2c 20 54 4b 5f  E, TK_AFTER, TK_
22d84 49 4e 53 54 45 41 44 20 2a 2f 0a 20 20 69 6e 74  INSTEAD */.  int
22d85 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   op,            
22d86 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 49 4e   /* One of TK_IN
22d87 53 45 52 54 2c 20 54 4b 5f 55 50 44 41 54 45 2c  SERT, TK_UPDATE,
22d88 20 54 4b 5f 44 45 4c 45 54 45 20 2a 2f 0a 20 20   TK_DELETE */.  
22d89 49 64 4c 69 73 74 20 2a 70 43 6f 6c 75 6d 6e 73  IdList *pColumns
22d8a 2c 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6c 69  ,   /* column li
22d8b 73 74 20 69 66 20 74 68 69 73 20 69 73 20 61 6e  st if this is an
22d8c 20 55 50 44 41 54 45 20 4f 46 20 74 72 69 67 67   UPDATE OF trigg
22d8d 65 72 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  er */.  SrcList 
22d8e 2a 70 54 61 62 6c 65 4e 61 6d 65 2c 2f 2a 20 54  *pTableName,/* T
22d8f 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
22d90 61 62 6c 65 2f 76 69 65 77 20 74 68 65 20 74 72  able/view the tr
22d91 69 67 67 65 72 20 61 70 70 6c 69 65 73 20 74 6f  igger applies to
22d92 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65   */.  Expr *pWhe
22d93 6e 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 48 45  n,        /* WHE
22d94 4e 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e  N clause */.  in
22d95 74 20 69 73 54 65 6d 70 2c 20 20 20 20 20 20 20  t isTemp,       
22d96 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
22d97 20 54 45 4d 50 4f 52 41 52 59 20 6b 65 79 77 6f   TEMPORARY keywo
22d98 72 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f  rd is present */
22d99 0a 20 20 69 6e 74 20 6e 6f 45 72 72 20 20 20 20  .  int noErr    
22d9a 20 20 20 20 20 20 20 2f 2a 20 53 75 70 70 72 65         /* Suppre
22d9b 73 73 20 65 72 72 6f 72 73 20 69 66 20 74 68 65  ss errors if the
22d9c 20 74 72 69 67 67 65 72 20 61 6c 72 65 61 64 79   trigger already
22d9d 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 20   exists */.){.  
22d9e 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
22d9f 72 20 3d 20 30 3b 0a 20 20 54 61 62 6c 65 20 2a  r = 0;.  Table *
22da0 70 54 61 62 3b 0a 20 20 63 68 61 72 20 2a 7a 4e  pTab;.  char *zN
22da1 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ame = 0;        
22da2 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74  /* Name of the t
22da3 72 69 67 67 65 72 20 2a 2f 0a 20 20 73 71 6c 69  rigger */.  sqli
22da4 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
22da5 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b  ->db;.  int iDb;
22da6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22da7 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
22da8 74 6f 20 73 74 6f 72 65 20 74 68 65 20 74 72 69  to store the tri
22da9 67 67 65 72 20 69 6e 20 2a 2f 0a 20 20 54 6f 6b  gger in */.  Tok
22daa 65 6e 20 2a 70 4e 61 6d 65 3b 20 20 20 20 20 20  en *pName;      
22dab 20 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 71 75       /* The unqu
22dac 61 6c 69 66 69 65 64 20 64 62 20 6e 61 6d 65 20  alified db name 
22dad 2a 2f 0a 20 20 44 62 46 69 78 65 72 20 73 46 69  */.  DbFixer sFi
22dae 78 3b 0a 20 20 69 6e 74 20 69 54 61 62 44 62 3b  x;.  int iTabDb;
22daf 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d  ..  assert( pNam
22db0 65 31 21 3d 30 20 29 3b 20 20 20 2f 2a 20 70 4e  e1!=0 );   /* pN
22db1 61 6d 65 31 2d 3e 7a 20 6d 69 67 68 74 20 62 65  ame1->z might be
22db2 20 4e 55 4c 4c 2c 20 62 75 74 20 6e 6f 74 20 70   NULL, but not p
22db3 4e 61 6d 65 31 20 69 74 73 65 6c 66 20 2a 2f 0a  Name1 itself */.
22db4 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 32    assert( pName2
22db5 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 69 73 54  !=0 );.  if( isT
22db6 65 6d 70 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  emp ){.    /* If
22db7 20 54 45 4d 50 20 77 61 73 20 73 70 65 63 69 66   TEMP was specif
22db8 69 65 64 2c 20 74 68 65 6e 20 74 68 65 20 74 72  ied, then the tr
22db9 69 67 67 65 72 20 6e 61 6d 65 20 6d 61 79 20 6e  igger name may n
22dba 6f 74 20 62 65 20 71 75 61 6c 69 66 69 65 64 2e  ot be qualified.
22dbb 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4e 61 6d   */.    if( pNam
22dbc 65 32 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 20  e2->n>0 ){.     
22dbd 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
22dbe 28 70 50 61 72 73 65 2c 20 22 74 65 6d 70 6f 72  (pParse, "tempor
22dbf 61 72 79 20 74 72 69 67 67 65 72 20 6d 61 79 20  ary trigger may 
22dc0 6e 6f 74 20 68 61 76 65 20 71 75 61 6c 69 66 69  not have qualifi
22dc1 65 64 20 6e 61 6d 65 22 29 3b 0a 20 20 20 20 20  ed name");.     
22dc2 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c   goto trigger_cl
22dc3 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20  eanup;.    }.   
22dc4 20 69 44 62 20 3d 20 31 3b 0a 20 20 20 20 70 4e   iDb = 1;.    pN
22dc5 61 6d 65 20 3d 20 70 4e 61 6d 65 31 3b 0a 20 20  ame = pName1;.  
22dc6 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 46 69  }else{.    /* Fi
22dc7 67 75 72 65 20 6f 75 74 20 74 68 65 20 64 62 20  gure out the db 
22dc8 74 68 61 74 20 74 68 65 20 74 68 65 20 74 72 69  that the the tri
22dc9 67 67 65 72 20 77 69 6c 6c 20 62 65 20 63 72 65  gger will be cre
22dca 61 74 65 64 20 69 6e 20 2a 2f 0a 20 20 20 20 69  ated in */.    i
22dcb 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50  Db = sqlite3TwoP
22dcc 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20  artName(pParse, 
22dcd 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20  pName1, pName2, 
22dce 26 70 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28  &pName);.    if(
22dcf 20 69 44 62 3c 30 20 29 7b 0a 20 20 20 20 20 20   iDb<0 ){.      
22dd0 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65  goto trigger_cle
22dd1 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  anup;.    }.  }.
22dd2 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72 69  .  /* If the tri
22dd3 67 67 65 72 20 6e 61 6d 65 20 77 61 73 20 75 6e  gger name was un
22dd4 71 75 61 6c 69 66 69 65 64 2c 20 61 6e 64 20 74  qualified, and t
22dd5 68 65 20 74 61 62 6c 65 20 69 73 20 61 20 74 65  he table is a te
22dd6 6d 70 20 74 61 62 6c 65 2c 0a 20 20 2a 2a 20 74  mp table,.  ** t
22dd7 68 65 6e 20 73 65 74 20 69 44 62 20 74 6f 20 31  hen set iDb to 1
22dd8 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 74   to create the t
22dd9 72 69 67 67 65 72 20 69 6e 20 74 68 65 20 74 65  rigger in the te
22dda 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65  mporary database
22ddb 2e 0a 20 20 2a 2a 20 49 66 20 73 71 6c 69 74 65  ..  ** If sqlite
22ddc 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 29  3SrcListLookup()
22ddd 20 72 65 74 75 72 6e 73 20 30 2c 20 69 6e 64 69   returns 0, indi
22dde 63 61 74 69 6e 67 20 74 68 65 20 74 61 62 6c 65  cating the table
22ddf 20 64 6f 65 73 20 6e 6f 74 0a 20 20 2a 2a 20 65   does not.  ** e
22de0 78 69 73 74 2c 20 74 68 65 20 65 72 72 6f 72 20  xist, the error 
22de1 69 73 20 63 61 75 67 68 74 20 62 79 20 74 68 65  is caught by the
22de2 20 62 6c 6f 63 6b 20 62 65 6c 6f 77 2e 0a 20 20   block below..  
22de3 2a 2f 0a 20 20 69 66 28 20 21 70 54 61 62 6c 65  */.  if( !pTable
22de4 4e 61 6d 65 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  Name || db->mall
22de5 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
22de6 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65  goto trigger_cle
22de7 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 70 54 61 62  anup;.  }.  pTab
22de8 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
22de9 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20  tLookup(pParse, 
22dea 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 69  pTableName);.  i
22deb 66 28 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 20  f( pName2->n==0 
22dec 26 26 20 70 54 61 62 20 26 26 20 70 54 61 62 2d  && pTab && pTab-
22ded 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 44  >pSchema==db->aD
22dee 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 7b 0a  b[1].pSchema ){.
22def 20 20 20 20 69 44 62 20 3d 20 31 3b 0a 20 20 7d      iDb = 1;.  }
22df0 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 74 68  ..  /* Ensure th
22df1 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 6d 61 74  e table name mat
22df2 63 68 65 73 20 64 61 74 61 62 61 73 65 20 6e 61  ches database na
22df3 6d 65 20 61 6e 64 20 74 68 61 74 20 74 68 65 20  me and that the 
22df4 74 61 62 6c 65 20 65 78 69 73 74 73 20 2a 2f 0a  table exists */.
22df5 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
22df6 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 74 72  Failed ) goto tr
22df7 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20  igger_cleanup;. 
22df8 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 4e   assert( pTableN
22df9 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a  ame->nSrc==1 );.
22dfa 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78    if( sqlite3Fix
22dfb 49 6e 69 74 28 26 73 46 69 78 2c 20 70 50 61 72  Init(&sFix, pPar
22dfc 73 65 2c 20 69 44 62 2c 20 22 74 72 69 67 67 65  se, iDb, "trigge
22dfd 72 22 2c 20 70 4e 61 6d 65 29 20 26 26 20 0a 20  r", pName) && . 
22dfe 20 20 20 20 20 73 71 6c 69 74 65 33 46 69 78 53       sqlite3FixS
22dff 72 63 4c 69 73 74 28 26 73 46 69 78 2c 20 70 54  rcList(&sFix, pT
22e00 61 62 6c 65 4e 61 6d 65 29 20 29 7b 0a 20 20 20  ableName) ){.   
22e01 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c   goto trigger_cl
22e02 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 70 54 61  eanup;.  }.  pTa
22e03 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69  b = sqlite3SrcLi
22e04 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c  stLookup(pParse,
22e05 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20   pTableName);.  
22e06 69 66 28 20 21 70 54 61 62 20 29 7b 0a 20 20 20  if( !pTab ){.   
22e07 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 64 6f   /* The table do
22e08 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 2a 2f  es not exist. */
22e09 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65  .    goto trigge
22e0a 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20  r_cleanup;.  }. 
22e0b 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70   if( IsVirtual(p
22e0c 54 61 62 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  Tab) ){.    sqli
22e0d 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
22e0e 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63 72 65 61  se, "cannot crea
22e0f 74 65 20 74 72 69 67 67 65 72 73 20 6f 6e 20 76  te triggers on v
22e10 69 72 74 75 61 6c 20 74 61 62 6c 65 73 22 29 3b  irtual tables");
22e11 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65  .    goto trigge
22e12 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a  r_cleanup;.  }..
22e13 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20    /* Check that 
22e14 74 68 65 20 74 72 69 67 67 65 72 20 6e 61 6d 65  the trigger name
22e15 20 69 73 20 6e 6f 74 20 72 65 73 65 72 76 65 64   is not reserved
22e16 20 61 6e 64 20 74 68 61 74 20 6e 6f 20 74 72 69   and that no tri
22e17 67 67 65 72 20 6f 66 20 74 68 65 0a 20 20 2a 2a  gger of the.  **
22e18 20 73 70 65 63 69 66 69 65 64 20 6e 61 6d 65 20   specified name 
22e19 65 78 69 73 74 73 20 2a 2f 0a 20 20 7a 4e 61 6d  exists */.  zNam
22e1a 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  e = sqlite3NameF
22e1b 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61  romToken(db, pNa
22e1c 6d 65 29 3b 0a 20 20 69 66 28 20 21 7a 4e 61 6d  me);.  if( !zNam
22e1d 65 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d  e || SQLITE_OK!=
22e1e 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65  sqlite3CheckObje
22e1f 63 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a  ctName(pParse, z
22e20 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f 74  Name) ){.    got
22e21 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  o trigger_cleanu
22e22 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c  p;.  }.  if( sql
22e23 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 28 64  ite3HashFind(&(d
22e24 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68  b->aDb[iDb].pSch
22e25 65 6d 61 2d 3e 74 72 69 67 48 61 73 68 29 2c 20  ema->trigHash), 
22e26 7a 4e 61 6d 65 2c 73 74 72 6c 65 6e 28 7a 4e 61  zName,strlen(zNa
22e27 6d 65 29 29 20 29 7b 0a 20 20 20 20 69 66 28 20  me)) ){.    if( 
22e28 21 6e 6f 45 72 72 20 29 7b 0a 20 20 20 20 20 20  !noErr ){.      
22e29 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
22e2a 70 50 61 72 73 65 2c 20 22 74 72 69 67 67 65 72  pParse, "trigger
22e2b 20 25 54 20 61 6c 72 65 61 64 79 20 65 78 69 73   %T already exis
22e2c 74 73 22 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20  ts", pName);.   
22e2d 20 7d 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67   }.    goto trig
22e2e 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ger_cleanup;.  }
22e2f 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 72  ..  /* Do not cr
22e30 65 61 74 65 20 61 20 74 72 69 67 67 65 72 20 6f  eate a trigger o
22e31 6e 20 61 20 73 79 73 74 65 6d 20 74 61 62 6c 65  n a system table
22e32 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
22e33 33 53 74 72 4e 49 43 6d 70 28 70 54 61 62 2d 3e  3StrNICmp(pTab->
22e34 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22  zName, "sqlite_"
22e35 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73  , 7)==0 ){.    s
22e36 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
22e37 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63  Parse, "cannot c
22e38 72 65 61 74 65 20 74 72 69 67 67 65 72 20 6f 6e  reate trigger on
22e39 20 73 79 73 74 65 6d 20 74 61 62 6c 65 22 29 3b   system table");
22e3a 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72  .    pParse->nEr
22e3b 72 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72  r++;.    goto tr
22e3c 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20  igger_cleanup;. 
22e3d 20 7d 0a 0a 20 20 2f 2a 20 49 4e 53 54 45 41 44   }..  /* INSTEAD
22e3e 20 6f 66 20 74 72 69 67 67 65 72 73 20 61 72 65   of triggers are
22e3f 20 6f 6e 6c 79 20 66 6f 72 20 76 69 65 77 73 20   only for views 
22e40 61 6e 64 20 76 69 65 77 73 20 6f 6e 6c 79 20 73  and views only s
22e41 75 70 70 6f 72 74 20 49 4e 53 54 45 41 44 0a 20  upport INSTEAD. 
22e42 20 2a 2a 20 6f 66 20 74 72 69 67 67 65 72 73 2e   ** of triggers.
22e43 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62  .  */.  if( pTab
22e44 2d 3e 70 53 65 6c 65 63 74 20 26 26 20 74 72 5f  ->pSelect && tr_
22e45 74 6d 21 3d 54 4b 5f 49 4e 53 54 45 41 44 20 29  tm!=TK_INSTEAD )
22e46 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
22e47 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63  orMsg(pParse, "c
22e48 61 6e 6e 6f 74 20 63 72 65 61 74 65 20 25 73 20  annot create %s 
22e49 74 72 69 67 67 65 72 20 6f 6e 20 76 69 65 77 3a  trigger on view:
22e4a 20 25 53 22 2c 20 0a 20 20 20 20 20 20 20 20 28   %S", .        (
22e4b 74 72 5f 74 6d 20 3d 3d 20 54 4b 5f 42 45 46 4f  tr_tm == TK_BEFO
22e4c 52 45 29 3f 22 42 45 46 4f 52 45 22 3a 22 41 46  RE)?"BEFORE":"AF
22e4d 54 45 52 22 2c 20 70 54 61 62 6c 65 4e 61 6d 65  TER", pTableName
22e4e 2c 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20 74  , 0);.    goto t
22e4f 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a  rigger_cleanup;.
22e50 20 20 7d 0a 20 20 69 66 28 20 21 70 54 61 62 2d    }.  if( !pTab-
22e51 3e 70 53 65 6c 65 63 74 20 26 26 20 74 72 5f 74  >pSelect && tr_t
22e52 6d 3d 3d 54 4b 5f 49 4e 53 54 45 41 44 20 29 7b  m==TK_INSTEAD ){
22e53 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
22e54 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63 61  rMsg(pParse, "ca
22e55 6e 6e 6f 74 20 63 72 65 61 74 65 20 49 4e 53 54  nnot create INST
22e56 45 41 44 20 4f 46 22 0a 20 20 20 20 20 20 20 20  EAD OF".        
22e57 22 20 74 72 69 67 67 65 72 20 6f 6e 20 74 61 62  " trigger on tab
22e58 6c 65 3a 20 25 53 22 2c 20 70 54 61 62 6c 65 4e  le: %S", pTableN
22e59 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 67 6f 74  ame, 0);.    got
22e5a 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  o trigger_cleanu
22e5b 70 3b 0a 20 20 7d 0a 20 20 69 54 61 62 44 62 20  p;.  }.  iTabDb 
22e5c 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
22e5d 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d  oIndex(db, pTab-
22e5e 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 23 69 66 6e  >pSchema);..#ifn
22e5f 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
22e60 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20  AUTHORIZATION.  
22e61 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d  {.    int code =
22e62 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54   SQLITE_CREATE_T
22e63 52 49 47 47 45 52 3b 0a 20 20 20 20 63 6f 6e 73  RIGGER;.    cons
22e64 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62  t char *zDb = db
22e65 2d 3e 61 44 62 5b 69 54 61 62 44 62 5d 2e 7a 4e  ->aDb[iTabDb].zN
22e66 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ame;.    const c
22e67 68 61 72 20 2a 7a 44 62 54 72 69 67 20 3d 20 69  har *zDbTrig = i
22e68 73 54 65 6d 70 20 3f 20 64 62 2d 3e 61 44 62 5b  sTemp ? db->aDb[
22e69 31 5d 2e 7a 4e 61 6d 65 20 3a 20 7a 44 62 3b 0a  1].zName : zDb;.
22e6a 20 20 20 20 69 66 28 20 69 54 61 62 44 62 3d 3d      if( iTabDb==
22e6b 31 20 7c 7c 20 69 73 54 65 6d 70 20 29 20 63 6f  1 || isTemp ) co
22e6c 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41  de = SQLITE_CREA
22e6d 54 45 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 3b  TE_TEMP_TRIGGER;
22e6e 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
22e6f 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
22e70 2c 20 63 6f 64 65 2c 20 7a 4e 61 6d 65 2c 20 70  , code, zName, p
22e71 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 54  Tab->zName, zDbT
22e72 72 69 67 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  rig) ){.      go
22e73 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  to trigger_clean
22e74 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  up;.    }.    if
22e75 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  ( sqlite3AuthChe
22e76 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54  ck(pParse, SQLIT
22e77 45 5f 49 4e 53 45 52 54 2c 20 53 43 48 45 4d 41  E_INSERT, SCHEMA
22e78 5f 54 41 42 4c 45 28 69 54 61 62 44 62 29 2c 30  _TABLE(iTabDb),0
22e79 2c 7a 44 62 29 29 7b 0a 20 20 20 20 20 20 67 6f  ,zDb)){.      go
22e7a 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  to trigger_clean
22e7b 75 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  up;.    }.  }.#e
22e7c 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 4e 53 54 45  ndif..  /* INSTE
22e7d 41 44 20 4f 46 20 74 72 69 67 67 65 72 73 20 63  AD OF triggers c
22e7e 61 6e 20 6f 6e 6c 79 20 61 70 70 65 61 72 20 6f  an only appear o
22e7f 6e 20 76 69 65 77 73 20 61 6e 64 20 42 45 46 4f  n views and BEFO
22e80 52 45 20 74 72 69 67 67 65 72 73 0a 20 20 2a 2a  RE triggers.  **
22e81 20 63 61 6e 6e 6f 74 20 61 70 70 65 61 72 20 6f   cannot appear o
22e82 6e 20 76 69 65 77 73 2e 20 20 53 6f 20 77 65 20  n views.  So we 
22e83 6d 69 67 68 74 20 61 73 20 77 65 6c 6c 20 74 72  might as well tr
22e84 61 6e 73 6c 61 74 65 20 65 76 65 72 79 0a 20 20  anslate every.  
22e85 2a 2a 20 49 4e 53 54 45 41 44 20 4f 46 20 74 72  ** INSTEAD OF tr
22e86 69 67 67 65 72 20 69 6e 74 6f 20 61 20 42 45 46  igger into a BEF
22e87 4f 52 45 20 74 72 69 67 67 65 72 2e 20 20 49 74  ORE trigger.  It
22e88 20 73 69 6d 70 6c 69 66 69 65 73 20 63 6f 64 65   simplifies code
22e89 0a 20 20 2a 2a 20 65 6c 73 65 77 68 65 72 65 2e  .  ** elsewhere.
22e8a 0a 20 20 2a 2f 0a 20 20 69 66 20 28 74 72 5f 74  .  */.  if (tr_t
22e8b 6d 20 3d 3d 20 54 4b 5f 49 4e 53 54 45 41 44 29  m == TK_INSTEAD)
22e8c 7b 0a 20 20 20 20 74 72 5f 74 6d 20 3d 20 54 4b  {.    tr_tm = TK
22e8d 5f 42 45 46 4f 52 45 3b 0a 20 20 7d 0a 0a 20 20  _BEFORE;.  }..  
22e8e 2f 2a 20 42 75 69 6c 64 20 74 68 65 20 54 72 69  /* Build the Tri
22e8f 67 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20  gger object */. 
22e90 20 70 54 72 69 67 67 65 72 20 3d 20 28 54 72 69   pTrigger = (Tri
22e91 67 67 65 72 2a 29 73 71 6c 69 74 65 33 44 62 4d  gger*)sqlite3DbM
22e92 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
22e93 7a 65 6f 66 28 54 72 69 67 67 65 72 29 29 3b 0a  zeof(Trigger));.
22e94 20 20 69 66 28 20 70 54 72 69 67 67 65 72 3d 3d    if( pTrigger==
22e95 30 20 29 20 67 6f 74 6f 20 74 72 69 67 67 65 72  0 ) goto trigger
22e96 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 70 54 72 69  _cleanup;.  pTri
22e97 67 67 65 72 2d 3e 6e 61 6d 65 20 3d 20 7a 4e 61  gger->name = zNa
22e98 6d 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 30 3b  me;.  zName = 0;
22e99 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 74 61 62  .  pTrigger->tab
22e9a 6c 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  le = sqlite3DbSt
22e9b 72 44 75 70 28 64 62 2c 20 70 54 61 62 6c 65 4e  rDup(db, pTableN
22e9c 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 29  ame->a[0].zName)
22e9d 3b 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 70 53  ;.  pTrigger->pS
22e9e 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b  chema = db->aDb[
22e9f 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20  iDb].pSchema;.  
22ea0 70 54 72 69 67 67 65 72 2d 3e 70 54 61 62 53 63  pTrigger->pTabSc
22ea1 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63  hema = pTab->pSc
22ea2 68 65 6d 61 3b 0a 20 20 70 54 72 69 67 67 65 72  hema;.  pTrigger
22ea3 2d 3e 6f 70 20 3d 20 6f 70 3b 0a 20 20 70 54 72  ->op = op;.  pTr
22ea4 69 67 67 65 72 2d 3e 74 72 5f 74 6d 20 3d 20 74  igger->tr_tm = t
22ea5 72 5f 74 6d 3d 3d 54 4b 5f 42 45 46 4f 52 45 20  r_tm==TK_BEFORE 
22ea6 3f 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45  ? TRIGGER_BEFORE
22ea7 20 3a 20 54 52 49 47 47 45 52 5f 41 46 54 45 52   : TRIGGER_AFTER
22ea8 3b 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 70 57  ;.  pTrigger->pW
22ea9 68 65 6e 20 3d 20 73 71 6c 69 74 65 33 45 78 70  hen = sqlite3Exp
22eaa 72 44 75 70 28 64 62 2c 20 70 57 68 65 6e 29 3b  rDup(db, pWhen);
22eab 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 70 43 6f  .  pTrigger->pCo
22eac 6c 75 6d 6e 73 20 3d 20 73 71 6c 69 74 65 33 49  lumns = sqlite3I
22ead 64 4c 69 73 74 44 75 70 28 64 62 2c 20 70 43 6f  dListDup(db, pCo
22eae 6c 75 6d 6e 73 29 3b 0a 20 20 73 71 6c 69 74 65  lumns);.  sqlite
22eaf 33 54 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26  3TokenCopy(db, &
22eb0 70 54 72 69 67 67 65 72 2d 3e 6e 61 6d 65 54 6f  pTrigger->nameTo
22eb1 6b 65 6e 2c 70 4e 61 6d 65 29 3b 0a 20 20 61 73  ken,pName);.  as
22eb2 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 4e  sert( pParse->pN
22eb3 65 77 54 72 69 67 67 65 72 3d 3d 30 20 29 3b 0a  ewTrigger==0 );.
22eb4 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72    pParse->pNewTr
22eb5 69 67 67 65 72 20 3d 20 70 54 72 69 67 67 65 72  igger = pTrigger
22eb6 3b 0a 0a 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  ;..trigger_clean
22eb7 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  up:.  sqlite3_fr
22eb8 65 65 28 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c  ee(zName);.  sql
22eb9 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
22eba 65 28 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20  e(pTableName);. 
22ebb 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65   sqlite3IdListDe
22ebc 6c 65 74 65 28 70 43 6f 6c 75 6d 6e 73 29 3b 0a  lete(pColumns);.
22ebd 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
22ebe 65 74 65 28 70 57 68 65 6e 29 3b 0a 20 20 69 66  ete(pWhen);.  if
22ebf 28 20 21 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  ( !pParse->pNewT
22ec0 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 73 71  rigger ){.    sq
22ec1 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67  lite3DeleteTrigg
22ec2 65 72 28 70 54 72 69 67 67 65 72 29 3b 0a 20 20  er(pTrigger);.  
22ec3 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
22ec4 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  t( pParse->pNewT
22ec5 72 69 67 67 65 72 3d 3d 70 54 72 69 67 67 65 72  rigger==pTrigger
22ec6 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   );.  }.}../*.**
22ec7 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
22ec8 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61 6c   called after al
22ec9 6c 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72  l of the trigger
22eca 20 61 63 74 69 6f 6e 73 20 68 61 76 65 20 62 65   actions have be
22ecb 65 6e 20 70 61 72 73 65 64 0a 2a 2a 20 69 6e 20  en parsed.** in 
22ecc 6f 72 64 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74  order to complet
22ecd 65 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f 66  e the process of
22ece 20 62 75 69 6c 64 69 6e 67 20 74 68 65 20 74 72   building the tr
22ecf 69 67 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  igger..*/.SQLITE
22ed0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
22ed1 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67 67  lite3FinishTrigg
22ed2 65 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  er(.  Parse *pPa
22ed3 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  rse,          /*
22ed4 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   Parser context 
22ed5 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65 70  */.  TriggerStep
22ed6 20 2a 70 53 74 65 70 4c 69 73 74 2c 20 2f 2a 20   *pStepList, /* 
22ed7 54 68 65 20 74 72 69 67 67 65 72 65 64 20 70 72  The triggered pr
22ed8 6f 67 72 61 6d 20 2a 2f 0a 20 20 54 6f 6b 65 6e  ogram */.  Token
22ed9 20 2a 70 41 6c 6c 20 20 20 20 20 20 20 20 20 20   *pAll          
22eda 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 74 68 61 74     /* Token that
22edb 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 63   describes the c
22edc 6f 6d 70 6c 65 74 65 20 43 52 45 41 54 45 20 54  omplete CREATE T
22edd 52 49 47 47 45 52 20 2a 2f 0a 29 7b 0a 20 20 54  RIGGER */.){.  T
22ede 72 69 67 67 65 72 20 2a 70 54 72 69 67 20 3d 20  rigger *pTrig = 
22edf 30 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72  0;     /* The tr
22ee0 69 67 67 65 72 20 77 68 6f 73 65 20 63 6f 6e 73  igger whose cons
22ee1 74 72 75 63 74 69 6f 6e 20 69 73 20 66 69 6e 69  truction is fini
22ee2 73 68 69 6e 67 20 75 70 20 2a 2f 0a 20 20 73 71  shing up */.  sq
22ee3 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
22ee4 73 65 2d 3e 64 62 3b 20 20 2f 2a 20 54 68 65 20  se->db;  /* The 
22ee5 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 44 62  database */.  Db
22ee6 46 69 78 65 72 20 73 46 69 78 3b 0a 20 20 69 6e  Fixer sFix;.  in
22ee7 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20  t iDb;          
22ee8 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
22ee9 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  base containing 
22eea 74 68 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 0a  the trigger */..
22eeb 20 20 70 54 72 69 67 20 3d 20 70 50 61 72 73 65    pTrig = pParse
22eec 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 3b 0a 20  ->pNewTrigger;. 
22eed 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69   pParse->pNewTri
22eee 67 67 65 72 20 3d 20 30 3b 0a 20 20 69 66 28 20  gger = 0;.  if( 
22eef 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20  pParse->nErr || 
22ef0 21 70 54 72 69 67 20 29 20 67 6f 74 6f 20 74 72  !pTrig ) goto tr
22ef1 69 67 67 65 72 66 69 6e 69 73 68 5f 63 6c 65 61  iggerfinish_clea
22ef2 6e 75 70 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c  nup;.  iDb = sql
22ef3 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
22ef4 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  x(pParse->db, pT
22ef5 72 69 67 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  rig->pSchema);. 
22ef6 20 70 54 72 69 67 2d 3e 73 74 65 70 5f 6c 69 73   pTrig->step_lis
22ef7 74 20 3d 20 70 53 74 65 70 4c 69 73 74 3b 0a 20  t = pStepList;. 
22ef8 20 77 68 69 6c 65 28 20 70 53 74 65 70 4c 69 73   while( pStepLis
22ef9 74 20 29 7b 0a 20 20 20 20 70 53 74 65 70 4c 69  t ){.    pStepLi
22efa 73 74 2d 3e 70 54 72 69 67 20 3d 20 70 54 72 69  st->pTrig = pTri
22efb 67 3b 0a 20 20 20 20 70 53 74 65 70 4c 69 73 74  g;.    pStepList
22efc 20 3d 20 70 53 74 65 70 4c 69 73 74 2d 3e 70 4e   = pStepList->pN
22efd 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  ext;.  }.  if( s
22efe 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28 26 73  qlite3FixInit(&s
22eff 46 69 78 2c 20 70 50 61 72 73 65 2c 20 69 44 62  Fix, pParse, iDb
22f00 2c 20 22 74 72 69 67 67 65 72 22 2c 20 26 70 54  , "trigger", &pT
22f01 72 69 67 2d 3e 6e 61 6d 65 54 6f 6b 65 6e 29 20  rig->nameToken) 
22f02 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 73 71  .          && sq
22f03 6c 69 74 65 33 46 69 78 54 72 69 67 67 65 72 53  lite3FixTriggerS
22f04 74 65 70 28 26 73 46 69 78 2c 20 70 54 72 69 67  tep(&sFix, pTrig
22f05 2d 3e 73 74 65 70 5f 6c 69 73 74 29 20 29 7b 0a  ->step_list) ){.
22f06 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72      goto trigger
22f07 66 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70 3b 0a  finish_cleanup;.
22f08 20 20 7d 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20    }..  /* if we 
22f09 61 72 65 20 6e 6f 74 20 69 6e 69 74 69 61 6c 69  are not initiali
22f0a 7a 69 6e 67 2c 20 61 6e 64 20 74 68 69 73 20 74  zing, and this t
22f0b 72 69 67 67 65 72 20 69 73 20 6e 6f 74 20 6f 6e  rigger is not on
22f0c 20 61 20 54 45 4d 50 20 74 61 62 6c 65 2c 20 0a   a TEMP table, .
22f0d 20 20 2a 2a 20 62 75 69 6c 64 20 74 68 65 20 73    ** build the s
22f0e 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 65 6e 74  qlite_master ent
22f0f 72 79 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 64  ry.  */.  if( !d
22f10 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a  b->init.busy ){.
22f11 20 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20      Vdbe *v;.   
22f12 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 20 20 2f   char *z;..    /
22f13 2a 20 4d 61 6b 65 20 61 6e 20 65 6e 74 72 79 20  * Make an entry 
22f14 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  in the sqlite_ma
22f15 73 74 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20  ster table */.  
22f16 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
22f17 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
22f18 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67 6f 74    if( v==0 ) got
22f19 6f 20 74 72 69 67 67 65 72 66 69 6e 69 73 68 5f  o triggerfinish_
22f1a 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 73 71 6c  cleanup;.    sql
22f1b 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
22f1c 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
22f1d 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 7a 20 3d  0, iDb);.    z =
22f1e 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75   sqlite3DbStrNDu
22f1f 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70 41 6c  p(db, (char*)pAl
22f20 6c 2d 3e 7a 2c 20 70 41 6c 6c 2d 3e 6e 29 3b 0a  l->z, pAll->n);.
22f21 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65      sqlite3Neste
22f22 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20  dParse(pParse,. 
22f23 20 20 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e        "INSERT IN
22f24 54 4f 20 25 51 2e 25 73 20 56 41 4c 55 45 53 28  TO %Q.%s VALUES(
22f25 27 74 72 69 67 67 65 72 27 2c 25 51 2c 25 51 2c  'trigger',%Q,%Q,
22f26 30 2c 27 43 52 45 41 54 45 20 54 52 49 47 47 45  0,'CREATE TRIGGE
22f27 52 20 25 71 27 29 22 2c 0a 20 20 20 20 20 20 20  R %q')",.       
22f28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
22f29 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45  me, SCHEMA_TABLE
22f2a 28 69 44 62 29 2c 20 70 54 72 69 67 2d 3e 6e 61  (iDb), pTrig->na
22f2b 6d 65 2c 0a 20 20 20 20 20 20 20 70 54 72 69 67  me,.       pTrig
22f2c 2d 3e 74 61 62 6c 65 2c 20 7a 29 3b 0a 20 20 20  ->table, z);.   
22f2d 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 29   sqlite3_free(z)
22f2e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61  ;.    sqlite3Cha
22f2f 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65  ngeCookie(pParse
22f30 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69  , iDb);.    sqli
22f31 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
22f32 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 2c   OP_ParseSchema,
22f33 20 69 44 62 2c 20 30 2c 20 30 2c 20 73 71 6c 69   iDb, 0, 0, sqli
22f34 74 65 33 4d 50 72 69 6e 74 66 28 0a 20 20 20 20  te3MPrintf(.    
22f35 20 20 20 20 64 62 2c 20 22 74 79 70 65 3d 27 74      db, "type='t
22f36 72 69 67 67 65 72 27 20 41 4e 44 20 6e 61 6d 65  rigger' AND name
22f37 3d 27 25 71 27 22 2c 20 70 54 72 69 67 2d 3e 6e  ='%q'", pTrig->n
22f38 61 6d 65 29 2c 20 50 34 5f 44 59 4e 41 4d 49 43  ame), P4_DYNAMIC
22f39 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 69  .    );.  }..  i
22f3a 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  f( db->init.busy
22f3b 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20   ){.    int n;. 
22f3c 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a     Table *pTab;.
22f3d 20 20 20 20 54 72 69 67 67 65 72 20 2a 70 44 65      Trigger *pDe
22f3e 6c 3b 0a 20 20 20 20 70 44 65 6c 20 3d 20 73 71  l;.    pDel = sq
22f3f 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
22f40 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53  &db->aDb[iDb].pS
22f41 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73 68 2c  chema->trigHash,
22f42 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
22f43 20 20 20 20 20 20 20 70 54 72 69 67 2d 3e 6e 61         pTrig->na
22f44 6d 65 2c 20 73 74 72 6c 65 6e 28 70 54 72 69 67  me, strlen(pTrig
22f45 2d 3e 6e 61 6d 65 29 2c 20 70 54 72 69 67 29 3b  ->name), pTrig);
22f46 0a 20 20 20 20 69 66 28 20 70 44 65 6c 20 29 7b  .    if( pDel ){
22f47 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
22f48 44 65 6c 3d 3d 70 54 72 69 67 20 29 3b 0a 20 20  Del==pTrig );.  
22f49 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
22f4a 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
22f4b 67 6f 74 6f 20 74 72 69 67 67 65 72 66 69 6e 69  goto triggerfini
22f4c 73 68 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  sh_cleanup;.    
22f4d 7d 0a 20 20 20 20 6e 20 3d 20 73 74 72 6c 65 6e  }.    n = strlen
22f4e 28 70 54 72 69 67 2d 3e 74 61 62 6c 65 29 20 2b  (pTrig->table) +
22f4f 20 31 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 73   1;.    pTab = s
22f50 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26  qlite3HashFind(&
22f51 70 54 72 69 67 2d 3e 70 54 61 62 53 63 68 65 6d  pTrig->pTabSchem
22f52 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70 54 72 69  a->tblHash, pTri
22f53 67 2d 3e 74 61 62 6c 65 2c 20 6e 29 3b 0a 20 20  g->table, n);.  
22f54 20 20 61 73 73 65 72 74 28 20 70 54 61 62 21 3d    assert( pTab!=
22f55 30 20 29 3b 0a 20 20 20 20 70 54 72 69 67 2d 3e  0 );.    pTrig->
22f56 70 4e 65 78 74 20 3d 20 70 54 61 62 2d 3e 70 54  pNext = pTab->pT
22f57 72 69 67 67 65 72 3b 0a 20 20 20 20 70 54 61 62  rigger;.    pTab
22f58 2d 3e 70 54 72 69 67 67 65 72 20 3d 20 70 54 72  ->pTrigger = pTr
22f59 69 67 3b 0a 20 20 20 20 70 54 72 69 67 20 3d 20  ig;.    pTrig = 
22f5a 30 3b 0a 20 20 7d 0a 0a 74 72 69 67 67 65 72 66  0;.  }..triggerf
22f5b 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70 3a 0a 20  inish_cleanup:. 
22f5c 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72   sqlite3DeleteTr
22f5d 69 67 67 65 72 28 70 54 72 69 67 29 3b 0a 20 20  igger(pTrig);.  
22f5e 61 73 73 65 72 74 28 20 21 70 50 61 72 73 65 2d  assert( !pParse-
22f5f 3e 70 4e 65 77 54 72 69 67 67 65 72 20 29 3b 0a  >pNewTrigger );.
22f60 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54    sqlite3DeleteT
22f61 72 69 67 67 65 72 53 74 65 70 28 70 53 74 65 70  riggerStep(pStep
22f62 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  List);.}../*.** 
22f63 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 61  Make a copy of a
22f64 6c 6c 20 63 6f 6d 70 6f 6e 65 6e 74 73 20 6f 66  ll components of
22f65 20 74 68 65 20 67 69 76 65 6e 20 74 72 69 67 67   the given trigg
22f66 65 72 20 73 74 65 70 2e 20 20 54 68 69 73 20 68  er step.  This h
22f67 61 73 0a 2a 2a 20 74 68 65 20 65 66 66 65 63 74  as.** the effect
22f68 20 6f 66 20 63 6f 70 79 69 6e 67 20 61 6c 6c 20   of copying all 
22f69 45 78 70 72 2e 74 6f 6b 65 6e 2e 7a 20 76 61 6c  Expr.token.z val
22f6a 75 65 73 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20  ues into memory 
22f6b 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d  obtained.** from
22f6c 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
22f6d 29 2e 20 20 41 73 20 69 6e 69 74 69 61 6c 6c 79  ).  As initially
22f6e 20 63 72 65 61 74 65 64 2c 20 74 68 65 20 45 78   created, the Ex
22f6f 70 72 2e 74 6f 6b 65 6e 2e 7a 20 76 61 6c 75 65  pr.token.z value
22f70 73 0a 2a 2a 20 61 6c 6c 20 70 6f 69 6e 74 20 74  s.** all point t
22f71 6f 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69  o the input stri
22f72 6e 67 20 74 68 61 74 20 77 61 73 20 66 65 64 20  ng that was fed 
22f73 74 6f 20 74 68 65 20 70 61 72 73 65 72 2e 20 20  to the parser.  
22f74 42 75 74 20 74 68 61 74 0a 2a 2a 20 73 74 72 69  But that.** stri
22f75 6e 67 20 69 73 20 65 70 68 65 6d 65 72 61 6c 20  ng is ephemeral 
22f76 2d 20 69 74 20 77 69 6c 6c 20 67 6f 20 61 77 61  - it will go awa
22f77 79 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 65  y as soon as the
22f78 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 0a   sqlite3_exec().
22f79 2a 2a 20 63 61 6c 6c 20 74 68 61 74 20 73 74 61  ** call that sta
22f7a 72 74 65 64 20 74 68 65 20 70 61 72 73 65 72 20  rted the parser 
22f7b 65 78 69 74 73 2e 20 20 54 68 69 73 20 72 6f 75  exits.  This rou
22f7c 74 69 6e 65 20 6d 61 6b 65 73 20 61 20 70 65 72  tine makes a per
22f7d 73 69 73 74 65 6e 74 0a 2a 2a 20 63 6f 70 79 20  sistent.** copy 
22f7e 6f 66 20 61 6c 6c 20 74 68 65 20 45 78 70 72 2e  of all the Expr.
22f7f 74 6f 6b 65 6e 2e 7a 20 73 74 72 69 6e 67 73 20  token.z strings 
22f80 73 6f 20 74 68 61 74 20 74 68 65 20 54 72 69 67  so that the Trig
22f81 67 65 72 53 74 65 70 20 73 74 72 75 63 74 75 72  gerStep structur
22f82 65 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 76 61 6c  e.** will be val
22f83 69 64 20 65 76 65 6e 20 61 66 74 65 72 20 74 68  id even after th
22f84 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29  e sqlite3_exec()
22f85 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e 0a 2a   call returns..*
22f86 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  /.static void sq
22f87 6c 69 74 65 50 65 72 73 69 73 74 54 72 69 67 67  litePersistTrigg
22f88 65 72 53 74 65 70 28 73 71 6c 69 74 65 33 20 2a  erStep(sqlite3 *
22f89 64 62 2c 20 54 72 69 67 67 65 72 53 74 65 70 20  db, TriggerStep 
22f8a 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 74 61  *p){.  if( p->ta
22f8b 72 67 65 74 2e 7a 20 29 7b 0a 20 20 20 20 70 2d  rget.z ){.    p-
22f8c 3e 74 61 72 67 65 74 2e 7a 20 3d 20 28 75 38 2a  >target.z = (u8*
22f8d 29 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75  )sqlite3DbStrNDu
22f8e 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70 2d 3e  p(db, (char*)p->
22f8f 74 61 72 67 65 74 2e 7a 2c 20 70 2d 3e 74 61 72  target.z, p->tar
22f90 67 65 74 2e 6e 29 3b 0a 20 20 20 20 70 2d 3e 74  get.n);.    p->t
22f91 61 72 67 65 74 2e 64 79 6e 20 3d 20 31 3b 0a 20  arget.dyn = 1;. 
22f92 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 53 65 6c   }.  if( p->pSel
22f93 65 63 74 20 29 7b 0a 20 20 20 20 53 65 6c 65 63  ect ){.    Selec
22f94 74 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  t *pNew = sqlite
22f95 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70  3SelectDup(db, p
22f96 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20  ->pSelect);.    
22f97 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
22f98 65 74 65 28 70 2d 3e 70 53 65 6c 65 63 74 29 3b  ete(p->pSelect);
22f99 0a 20 20 20 20 70 2d 3e 70 53 65 6c 65 63 74 20  .    p->pSelect 
22f9a 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 69 66  = pNew;.  }.  if
22f9b 28 20 70 2d 3e 70 57 68 65 72 65 20 29 7b 0a 20  ( p->pWhere ){. 
22f9c 20 20 20 45 78 70 72 20 2a 70 4e 65 77 20 3d 20     Expr *pNew = 
22f9d 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
22f9e 62 2c 20 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20  b, p->pWhere);. 
22f9f 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
22fa0 6c 65 74 65 28 70 2d 3e 70 57 68 65 72 65 29 3b  lete(p->pWhere);
22fa1 0a 20 20 20 20 70 2d 3e 70 57 68 65 72 65 20 3d  .    p->pWhere =
22fa2 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 69 66 28   pNew;.  }.  if(
22fa3 20 70 2d 3e 70 45 78 70 72 4c 69 73 74 20 29 7b   p->pExprList ){
22fa4 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70  .    ExprList *p
22fa5 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70  New = sqlite3Exp
22fa6 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e  rListDup(db, p->
22fa7 70 45 78 70 72 4c 69 73 74 29 3b 0a 20 20 20 20  pExprList);.    
22fa8 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
22fa9 65 6c 65 74 65 28 70 2d 3e 70 45 78 70 72 4c 69  elete(p->pExprLi
22faa 73 74 29 3b 0a 20 20 20 20 70 2d 3e 70 45 78 70  st);.    p->pExp
22fab 72 4c 69 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20  rList = pNew;.  
22fac 7d 0a 20 20 69 66 28 20 70 2d 3e 70 49 64 4c 69  }.  if( p->pIdLi
22fad 73 74 20 29 7b 0a 20 20 20 20 49 64 4c 69 73 74  st ){.    IdList
22fae 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33   *pNew = sqlite3
22faf 49 64 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d  IdListDup(db, p-
22fb0 3e 70 49 64 4c 69 73 74 29 3b 0a 20 20 20 20 73  >pIdList);.    s
22fb1 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65  qlite3IdListDele
22fb2 74 65 28 70 2d 3e 70 49 64 4c 69 73 74 29 3b 0a  te(p->pIdList);.
22fb3 20 20 20 20 70 2d 3e 70 49 64 4c 69 73 74 20 3d      p->pIdList =
22fb4 20 70 4e 65 77 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a   pNew;.  }.}../*
22fb5 0a 2a 2a 20 54 75 72 6e 20 61 20 53 45 4c 45 43  .** Turn a SELEC
22fb6 54 20 73 74 61 74 65 6d 65 6e 74 20 28 74 68 61  T statement (tha
22fb7 74 20 74 68 65 20 70 53 65 6c 65 63 74 20 70 61  t the pSelect pa
22fb8 72 61 6d 65 74 65 72 20 70 6f 69 6e 74 73 20 74  rameter points t
22fb9 6f 29 20 69 6e 74 6f 0a 2a 2a 20 61 20 74 72 69  o) into.** a tri
22fba 67 67 65 72 20 73 74 65 70 2e 20 20 52 65 74 75  gger step.  Retu
22fbb 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
22fbc 61 20 54 72 69 67 67 65 72 53 74 65 70 20 73 74  a TriggerStep st
22fbd 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54  ructure..**.** T
22fbe 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73 20  he parser calls 
22fbf 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65  this routine whe
22fc0 6e 20 69 74 20 66 69 6e 64 73 20 61 20 53 45 4c  n it finds a SEL
22fc1 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69 6e  ECT statement in
22fc2 0a 2a 2a 20 62 6f 64 79 20 6f 66 20 61 20 54 52  .** body of a TR
22fc3 49 47 47 45 52 2e 20 20 0a 2a 2f 0a 53 51 4c 49  IGGER.  .*/.SQLI
22fc4 54 45 5f 50 52 49 56 41 54 45 20 54 72 69 67 67  TE_PRIVATE Trigg
22fc5 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54  erStep *sqlite3T
22fc6 72 69 67 67 65 72 53 65 6c 65 63 74 53 74 65 70  riggerSelectStep
22fc7 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 65  (sqlite3 *db, Se
22fc8 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a  lect *pSelect){.
22fc9 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70    TriggerStep *p
22fca 54 72 69 67 67 65 72 53 74 65 70 20 3d 20 73 71  TriggerStep = sq
22fcb 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
22fcc 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 72 69  o(db, sizeof(Tri
22fcd 67 67 65 72 53 74 65 70 29 29 3b 0a 20 20 69 66  ggerStep));.  if
22fce 28 20 70 54 72 69 67 67 65 72 53 74 65 70 3d 3d  ( pTriggerStep==
22fcf 30 20 29 20 7b 0a 20 20 20 20 73 71 6c 69 74 65  0 ) {.    sqlite
22fd0 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 53  3SelectDelete(pS
22fd1 65 6c 65 63 74 29 3b 0a 20 20 20 20 72 65 74 75  elect);.    retu
22fd2 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 70 54 72  rn 0;.  }..  pTr
22fd3 69 67 67 65 72 53 74 65 70 2d 3e 6f 70 20 3d 20  iggerStep->op = 
22fd4 54 4b 5f 53 45 4c 45 43 54 3b 0a 20 20 70 54 72  TK_SELECT;.  pTr
22fd5 69 67 67 65 72 53 74 65 70 2d 3e 70 53 65 6c 65  iggerStep->pSele
22fd6 63 74 20 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20  ct = pSelect;.  
22fd7 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 72  pTriggerStep->or
22fd8 63 6f 6e 66 20 3d 20 4f 45 5f 44 65 66 61 75 6c  conf = OE_Defaul
22fd9 74 3b 0a 20 20 73 71 6c 69 74 65 50 65 72 73 69  t;.  sqlitePersi
22fda 73 74 54 72 69 67 67 65 72 53 74 65 70 28 64 62  stTriggerStep(db
22fdb 2c 20 70 54 72 69 67 67 65 72 53 74 65 70 29 3b  , pTriggerStep);
22fdc 0a 0a 20 20 72 65 74 75 72 6e 20 70 54 72 69 67  ..  return pTrig
22fdd 67 65 72 53 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  gerStep;.}../*.*
22fde 2a 20 42 75 69 6c 64 20 61 20 74 72 69 67 67 65  * Build a trigge
22fdf 72 20 73 74 65 70 20 6f 75 74 20 6f 66 20 61 6e  r step out of an
22fe0 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e   INSERT statemen
22fe1 74 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69  t.  Return a poi
22fe2 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 6e  nter.** to the n
22fe3 65 77 20 74 72 69 67 67 65 72 20 73 74 65 70 2e  ew trigger step.
22fe4 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65  .**.** The parse
22fe5 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  r calls this rou
22fe6 74 69 6e 65 20 77 68 65 6e 20 69 74 20 73 65 65  tine when it see
22fe7 73 20 61 6e 20 49 4e 53 45 52 54 20 69 6e 73 69  s an INSERT insi
22fe8 64 65 20 74 68 65 0a 2a 2a 20 62 6f 64 79 20 6f  de the.** body o
22fe9 66 20 61 20 74 72 69 67 67 65 72 2e 0a 2a 2f 0a  f a trigger..*/.
22fea 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54  SQLITE_PRIVATE T
22feb 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69  riggerStep *sqli
22fec 74 65 33 54 72 69 67 67 65 72 49 6e 73 65 72 74  te3TriggerInsert
22fed 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65 33 20  Step(.  sqlite3 
22fee 2a 64 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 54  *db,        /* T
22fef 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
22ff0 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65  ection */.  Toke
22ff1 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c 20 20  n *pTableName,  
22ff2 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74  /* Name of the t
22ff3 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20  able into which 
22ff4 77 65 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20 49  we insert */.  I
22ff5 64 4c 69 73 74 20 2a 70 43 6f 6c 75 6d 6e 2c 20  dList *pColumn, 
22ff6 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 63 6f     /* List of co
22ff7 6c 75 6d 6e 73 20 69 6e 20 70 54 61 62 6c 65 4e  lumns in pTableN
22ff8 61 6d 65 20 74 6f 20 69 6e 73 65 72 74 20 69 6e  ame to insert in
22ff9 74 6f 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  to */.  ExprList
22ffa 20 2a 70 45 4c 69 73 74 2c 20 20 20 2f 2a 20 54   *pEList,   /* T
22ffb 68 65 20 56 41 4c 55 45 20 63 6c 61 75 73 65 3a  he VALUE clause:
22ffc 20 61 20 6c 69 73 74 20 6f 66 20 76 61 6c 75 65   a list of value
22ffd 73 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64  s to be inserted
22ffe 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53   */.  Select *pS
22fff 65 6c 65 63 74 2c 20 20 20 20 2f 2a 20 41 20 53  elect,    /* A S
23000 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
23001 74 68 61 74 20 73 75 70 70 6c 69 65 73 20 76 61  that supplies va
23002 6c 75 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 72  lues */.  int or
23003 63 6f 6e 66 20 20 20 20 20 20 20 20 20 20 2f 2a  conf          /*
23004 20 54 68 65 20 63 6f 6e 66 6c 69 63 74 20 61 6c   The conflict al
23005 67 6f 72 69 74 68 6d 20 28 4f 45 5f 41 62 6f 72  gorithm (OE_Abor
23006 74 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20 65  t, OE_Replace, e
23007 74 63 2e 29 20 2a 2f 0a 29 7b 0a 20 20 54 72 69  tc.) */.){.  Tri
23008 67 67 65 72 53 74 65 70 20 2a 70 54 72 69 67 67  ggerStep *pTrigg
23009 65 72 53 74 65 70 3b 0a 0a 20 20 61 73 73 65 72  erStep;..  asser
2300a 74 28 70 45 4c 69 73 74 20 3d 3d 20 30 20 7c 7c  t(pEList == 0 ||
2300b 20 70 53 65 6c 65 63 74 20 3d 3d 20 30 29 3b 0a   pSelect == 0);.
2300c 20 20 61 73 73 65 72 74 28 70 45 4c 69 73 74 20    assert(pEList 
2300d 21 3d 20 30 20 7c 7c 20 70 53 65 6c 65 63 74 20  != 0 || pSelect 
2300e 21 3d 20 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  != 0 || db->mall
2300f 6f 63 46 61 69 6c 65 64 29 3b 0a 0a 20 20 70 54  ocFailed);..  pT
23010 72 69 67 67 65 72 53 74 65 70 20 3d 20 73 71 6c  riggerStep = sql
23011 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
23012 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 72 69 67  (db, sizeof(Trig
23013 67 65 72 53 74 65 70 29 29 3b 0a 20 20 69 66 28  gerStep));.  if(
23014 20 70 54 72 69 67 67 65 72 53 74 65 70 20 29 7b   pTriggerStep ){
23015 0a 20 20 20 20 70 54 72 69 67 67 65 72 53 74 65  .    pTriggerSte
23016 70 2d 3e 6f 70 20 3d 20 54 4b 5f 49 4e 53 45 52  p->op = TK_INSER
23017 54 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72 53  T;.    pTriggerS
23018 74 65 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20 70  tep->pSelect = p
23019 53 65 6c 65 63 74 3b 0a 20 20 20 20 70 54 72 69  Select;.    pTri
2301a 67 67 65 72 53 74 65 70 2d 3e 74 61 72 67 65 74  ggerStep->target
2301b 20 20 3d 20 2a 70 54 61 62 6c 65 4e 61 6d 65 3b    = *pTableName;
2301c 0a 20 20 20 20 70 54 72 69 67 67 65 72 53 74 65  .    pTriggerSte
2301d 70 2d 3e 70 49 64 4c 69 73 74 20 3d 20 70 43 6f  p->pIdList = pCo
2301e 6c 75 6d 6e 3b 0a 20 20 20 20 70 54 72 69 67 67  lumn;.    pTrigg
2301f 65 72 53 74 65 70 2d 3e 70 45 78 70 72 4c 69 73  erStep->pExprLis
23020 74 20 3d 20 70 45 4c 69 73 74 3b 0a 20 20 20 20  t = pEList;.    
23021 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 72  pTriggerStep->or
23022 63 6f 6e 66 20 3d 20 6f 72 63 6f 6e 66 3b 0a 20  conf = orconf;. 
23023 20 20 20 73 71 6c 69 74 65 50 65 72 73 69 73 74     sqlitePersist
23024 54 72 69 67 67 65 72 53 74 65 70 28 64 62 2c 20  TriggerStep(db, 
23025 70 54 72 69 67 67 65 72 53 74 65 70 29 3b 0a 20  pTriggerStep);. 
23026 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
23027 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28  te3IdListDelete(
23028 70 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 73 71  pColumn);.    sq
23029 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
2302a 65 74 65 28 70 45 4c 69 73 74 29 3b 0a 20 20 20  ete(pEList);.   
2302b 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
2302c 6c 65 74 65 28 70 53 65 6c 65 63 74 29 3b 0a 20  lete(pSelect);. 
2302d 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 54 72   }..  return pTr
2302e 69 67 67 65 72 53 74 65 70 3b 0a 7d 0a 0a 2f 2a  iggerStep;.}../*
2302f 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 20  .** Construct a 
23030 74 72 69 67 67 65 72 20 73 74 65 70 20 74 68 61  trigger step tha
23031 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20  t implements an 
23032 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74  UPDATE statement
23033 20 61 6e 64 20 72 65 74 75 72 6e 0a 2a 2a 20 61   and return.** a
23034 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74   pointer to that
23035 20 74 72 69 67 67 65 72 20 73 74 65 70 2e 20 20   trigger step.  
23036 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73  The parser calls
23037 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68   this routine wh
23038 65 6e 20 69 74 0a 2a 2a 20 73 65 65 73 20 61 6e  en it.** sees an
23039 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e   UPDATE statemen
2303a 74 20 69 6e 73 69 64 65 20 74 68 65 20 62 6f 64  t inside the bod
2303b 79 20 6f 66 20 61 20 43 52 45 41 54 45 20 54 52  y of a CREATE TR
2303c 49 47 47 45 52 2e 0a 2a 2f 0a 53 51 4c 49 54 45  IGGER..*/.SQLITE
2303d 5f 50 52 49 56 41 54 45 20 54 72 69 67 67 65 72  _PRIVATE Trigger
2303e 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69  Step *sqlite3Tri
2303f 67 67 65 72 55 70 64 61 74 65 53 74 65 70 28 0a  ggerUpdateStep(.
23040 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
23041 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
23042 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
23043 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54  n */.  Token *pT
23044 61 62 6c 65 4e 61 6d 65 2c 20 20 20 2f 2a 20 4e  ableName,   /* N
23045 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
23046 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 2a   to be updated *
23047 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
23048 4c 69 73 74 2c 20 20 20 20 2f 2a 20 54 68 65 20  List,    /* The 
23049 53 45 54 20 63 6c 61 75 73 65 3a 20 6c 69 73 74  SET clause: list
2304a 20 6f 66 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 6e   of column and n
2304b 65 77 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 45  ew values */.  E
2304c 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20  xpr *pWhere,    
2304d 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45      /* The WHERE
2304e 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74   clause */.  int
2304f 20 6f 72 63 6f 6e 66 20 20 20 20 20 20 20 20 20   orconf         
23050 20 20 2f 2a 20 54 68 65 20 63 6f 6e 66 6c 69 63    /* The conflic
23051 74 20 61 6c 67 6f 72 69 74 68 6d 2e 20 28 4f 45  t algorithm. (OE
23052 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72  _Abort, OE_Ignor
23053 65 2c 20 65 74 63 29 20 2a 2f 0a 29 7b 0a 20 20  e, etc) */.){.  
23054 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 54 72  TriggerStep *pTr
23055 69 67 67 65 72 53 74 65 70 20 3d 20 73 71 6c 69  iggerStep = sqli
23056 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
23057 64 62 2c 20 73 69 7a 65 6f 66 28 54 72 69 67 67  db, sizeof(Trigg
23058 65 72 53 74 65 70 29 29 3b 0a 20 20 69 66 28 20  erStep));.  if( 
23059 70 54 72 69 67 67 65 72 53 74 65 70 3d 3d 30 20  pTriggerStep==0 
2305a 29 7b 0a 20 20 20 20 20 73 71 6c 69 74 65 33 45  ){.     sqlite3E
2305b 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 45  xprListDelete(pE
2305c 4c 69 73 74 29 3b 0a 20 20 20 20 20 73 71 6c 69  List);.     sqli
2305d 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 57  te3ExprDelete(pW
2305e 68 65 72 65 29 3b 0a 20 20 20 20 20 72 65 74 75  here);.     retu
2305f 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 70 54 72  rn 0;.  }..  pTr
23060 69 67 67 65 72 53 74 65 70 2d 3e 6f 70 20 3d 20  iggerStep->op = 
23061 54 4b 5f 55 50 44 41 54 45 3b 0a 20 20 70 54 72  TK_UPDATE;.  pTr
23062 69 67 67 65 72 53 74 65 70 2d 3e 74 61 72 67 65  iggerStep->targe
23063 74 20 20 3d 20 2a 70 54 61 62 6c 65 4e 61 6d 65  t  = *pTableName
23064 3b 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70  ;.  pTriggerStep
23065 2d 3e 70 45 78 70 72 4c 69 73 74 20 3d 20 70 45  ->pExprList = pE
23066 4c 69 73 74 3b 0a 20 20 70 54 72 69 67 67 65 72  List;.  pTrigger
23067 53 74 65 70 2d 3e 70 57 68 65 72 65 20 3d 20 70  Step->pWhere = p
23068 57 68 65 72 65 3b 0a 20 20 70 54 72 69 67 67 65  Where;.  pTrigge
23069 72 53 74 65 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20  rStep->orconf = 
2306a 6f 72 63 6f 6e 66 3b 0a 20 20 73 71 6c 69 74 65  orconf;.  sqlite
2306b 50 65 72 73 69 73 74 54 72 69 67 67 65 72 53 74  PersistTriggerSt
2306c 65 70 28 64 62 2c 20 70 54 72 69 67 67 65 72 53  ep(db, pTriggerS
2306d 74 65 70 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  tep);..  return 
2306e 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a 7d 0a  pTriggerStep;.}.
2306f 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74  ./*.** Construct
23070 20 61 20 74 72 69 67 67 65 72 20 73 74 65 70 20   a trigger step 
23071 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20  that implements 
23072 61 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65  a DELETE stateme
23073 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 0a 2a 2a  nt and return.**
23074 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
23075 61 74 20 74 72 69 67 67 65 72 20 73 74 65 70 2e  at trigger step.
23076 20 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c    The parser cal
23077 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ls this routine 
23078 77 68 65 6e 20 69 74 0a 2a 2a 20 73 65 65 73 20  when it.** sees 
23079 61 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65  a DELETE stateme
2307a 6e 74 20 69 6e 73 69 64 65 20 74 68 65 20 62 6f  nt inside the bo
2307b 64 79 20 6f 66 20 61 20 43 52 45 41 54 45 20 54  dy of a CREATE T
2307c 52 49 47 47 45 52 2e 0a 2a 2f 0a 53 51 4c 49 54  RIGGER..*/.SQLIT
2307d 45 5f 50 52 49 56 41 54 45 20 54 72 69 67 67 65  E_PRIVATE Trigge
2307e 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72  rStep *sqlite3Tr
2307f 69 67 67 65 72 44 65 6c 65 74 65 53 74 65 70 28  iggerDeleteStep(
23080 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
23081 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
23082 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
23083 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54  n */.  Token *pT
23084 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20 20 20 2f  ableName,      /
23085 2a 20 54 68 65 20 74 61 62 6c 65 20 66 72 6f 6d  * The table from
23086 20 77 68 69 63 68 20 72 6f 77 73 20 61 72 65 20   which rows are 
23087 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 45 78 70  deleted */.  Exp
23088 72 20 2a 70 57 68 65 72 65 20 20 20 20 20 20 20  r *pWhere       
23089 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52       /* The WHER
2308a 45 20 63 6c 61 75 73 65 20 2a 2f 0a 29 7b 0a 20  E clause */.){. 
2308b 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 54   TriggerStep *pT
2308c 72 69 67 67 65 72 53 74 65 70 20 3d 20 73 71 6c  riggerStep = sql
2308d 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
2308e 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 72 69 67  (db, sizeof(Trig
2308f 67 65 72 53 74 65 70 29 29 3b 0a 20 20 69 66 28  gerStep));.  if(
23090 20 70 54 72 69 67 67 65 72 53 74 65 70 3d 3d 30   pTriggerStep==0
23091 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
23092 78 70 72 44 65 6c 65 74 65 28 70 57 68 65 72 65  xprDelete(pWhere
23093 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
23094 0a 20 20 7d 0a 0a 20 20 70 54 72 69 67 67 65 72  .  }..  pTrigger
23095 53 74 65 70 2d 3e 6f 70 20 3d 20 54 4b 5f 44 45  Step->op = TK_DE
23096 4c 45 54 45 3b 0a 20 20 70 54 72 69 67 67 65 72  LETE;.  pTrigger
23097 53 74 65 70 2d 3e 74 61 72 67 65 74 20 20 3d 20  Step->target  = 
23098 2a 70 54 61 62 6c 65 4e 61 6d 65 3b 0a 20 20 70  *pTableName;.  p
23099 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 57 68  TriggerStep->pWh
2309a 65 72 65 20 3d 20 70 57 68 65 72 65 3b 0a 20 20  ere = pWhere;.  
2309b 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 72  pTriggerStep->or
2309c 63 6f 6e 66 20 3d 20 4f 45 5f 44 65 66 61 75 6c  conf = OE_Defaul
2309d 74 3b 0a 20 20 73 71 6c 69 74 65 50 65 72 73 69  t;.  sqlitePersi
2309e 73 74 54 72 69 67 67 65 72 53 74 65 70 28 64 62  stTriggerStep(db
2309f 2c 20 70 54 72 69 67 67 65 72 53 74 65 70 29 3b  , pTriggerStep);
230a0 0a 0a 20 20 72 65 74 75 72 6e 20 70 54 72 69 67  ..  return pTrig
230a1 67 65 72 53 74 65 70 3b 0a 7d 0a 0a 2f 2a 20 0a  gerStep;.}../* .
230a2 2a 2a 20 52 65 63 75 72 73 69 76 65 6c 79 20 64  ** Recursively d
230a3 65 6c 65 74 65 20 61 20 54 72 69 67 67 65 72 20  elete a Trigger 
230a4 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a 53 51 4c  structure.*/.SQL
230a5 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
230a6 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72   sqlite3DeleteTr
230a7 69 67 67 65 72 28 54 72 69 67 67 65 72 20 2a 70  igger(Trigger *p
230a8 54 72 69 67 67 65 72 29 7b 0a 20 20 69 66 28 20  Trigger){.  if( 
230a9 70 54 72 69 67 67 65 72 3d 3d 30 20 29 20 72 65  pTrigger==0 ) re
230aa 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 44  turn;.  sqlite3D
230ab 65 6c 65 74 65 54 72 69 67 67 65 72 53 74 65 70  eleteTriggerStep
230ac 28 70 54 72 69 67 67 65 72 2d 3e 73 74 65 70 5f  (pTrigger->step_
230ad 6c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  list);.  sqlite3
230ae 5f 66 72 65 65 28 70 54 72 69 67 67 65 72 2d 3e  _free(pTrigger->
230af 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  name);.  sqlite3
230b0 5f 66 72 65 65 28 70 54 72 69 67 67 65 72 2d 3e  _free(pTrigger->
230b1 74 61 62 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65  table);.  sqlite
230b2 33 45 78 70 72 44 65 6c 65 74 65 28 70 54 72 69  3ExprDelete(pTri
230b3 67 67 65 72 2d 3e 70 57 68 65 6e 29 3b 0a 20 20  gger->pWhen);.  
230b4 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c  sqlite3IdListDel
230b5 65 74 65 28 70 54 72 69 67 67 65 72 2d 3e 70 43  ete(pTrigger->pC
230b6 6f 6c 75 6d 6e 73 29 3b 0a 20 20 69 66 28 20 70  olumns);.  if( p
230b7 54 72 69 67 67 65 72 2d 3e 6e 61 6d 65 54 6f 6b  Trigger->nameTok
230b8 65 6e 2e 64 79 6e 20 29 20 73 71 6c 69 74 65 33  en.dyn ) sqlite3
230b9 5f 66 72 65 65 28 28 63 68 61 72 2a 29 70 54 72  _free((char*)pTr
230ba 69 67 67 65 72 2d 3e 6e 61 6d 65 54 6f 6b 65 6e  igger->nameToken
230bb 2e 7a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  .z);.  sqlite3_f
230bc 72 65 65 28 70 54 72 69 67 67 65 72 29 3b 0a 7d  ree(pTrigger);.}
230bd 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
230be 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
230bf 74 6f 20 64 72 6f 70 20 61 20 74 72 69 67 67 65  to drop a trigge
230c0 72 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62  r from the datab
230c1 61 73 65 20 73 63 68 65 6d 61 2e 20 0a 2a 2a 0a  ase schema. .**.
230c2 2a 2a 20 54 68 69 73 20 6d 61 79 20 62 65 20 63  ** This may be c
230c3 61 6c 6c 65 64 20 64 69 72 65 63 74 6c 79 20 66  alled directly f
230c4 72 6f 6d 20 74 68 65 20 70 61 72 73 65 72 20 61  rom the parser a
230c5 6e 64 20 74 68 65 72 65 66 6f 72 65 20 69 64 65  nd therefore ide
230c6 6e 74 69 66 69 65 73 0a 2a 2a 20 74 68 65 20 74  ntifies.** the t
230c7 72 69 67 67 65 72 20 62 79 20 6e 61 6d 65 2e 20  rigger by name. 
230c8 20 54 68 65 20 73 71 6c 69 74 65 33 44 72 6f 70   The sqlite3Drop
230c9 54 72 69 67 67 65 72 50 74 72 28 29 20 72 6f 75  TriggerPtr() rou
230ca 74 69 6e 65 20 64 6f 65 73 20 74 68 65 0a 2a 2a  tine does the.**
230cb 20 73 61 6d 65 20 6a 6f 62 20 61 73 20 74 68 69   same job as thi
230cc 73 20 72 6f 75 74 69 6e 65 20 65 78 63 65 70 74  s routine except
230cd 20 69 74 20 74 61 6b 65 73 20 61 20 70 6f 69 6e   it takes a poin
230ce 74 65 72 20 74 6f 20 74 68 65 20 74 72 69 67 67  ter to the trigg
230cf 65 72 0a 2a 2a 20 69 6e 73 74 65 61 64 20 6f 66  er.** instead of
230d0 20 74 68 65 20 74 72 69 67 67 65 72 20 6e 61 6d   the trigger nam
230d1 65 2e 0a 2a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  e..**/.SQLITE_PR
230d2 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
230d3 65 33 44 72 6f 70 54 72 69 67 67 65 72 28 50 61  e3DropTrigger(Pa
230d4 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63  rse *pParse, Src
230d5 4c 69 73 74 20 2a 70 4e 61 6d 65 2c 20 69 6e 74  List *pName, int
230d6 20 6e 6f 45 72 72 29 7b 0a 20 20 54 72 69 67 67   noErr){.  Trigg
230d7 65 72 20 2a 70 54 72 69 67 67 65 72 20 3d 20 30  er *pTrigger = 0
230d8 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e  ;.  int i;.  con
230d9 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 0a 20 20  st char *zDb;.  
230da 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
230db 65 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a  e;.  int nName;.
230dc 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
230dd 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 69  pParse->db;..  i
230de 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
230df 6c 65 64 20 29 20 67 6f 74 6f 20 64 72 6f 70 5f  led ) goto drop_
230e0 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
230e1 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
230e2 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  !=sqlite3ReadSch
230e3 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20  ema(pParse) ){. 
230e4 20 20 20 67 6f 74 6f 20 64 72 6f 70 5f 74 72 69     goto drop_tri
230e5 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  gger_cleanup;.  
230e6 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4e 61  }..  assert( pNa
230e7 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20  me->nSrc==1 );. 
230e8 20 7a 44 62 20 3d 20 70 4e 61 6d 65 2d 3e 61 5b   zDb = pName->a[
230e9 30 5d 2e 7a 44 61 74 61 62 61 73 65 3b 0a 20 20  0].zDatabase;.  
230ea 7a 4e 61 6d 65 20 3d 20 70 4e 61 6d 65 2d 3e 61  zName = pName->a
230eb 5b 30 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 6e 4e 61  [0].zName;.  nNa
230ec 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a 4e 61 6d  me = strlen(zNam
230ed 65 29 3b 0a 20 20 66 6f 72 28 69 3d 4f 4d 49 54  e);.  for(i=OMIT
230ee 5f 54 45 4d 50 44 42 3b 20 69 3c 64 62 2d 3e 6e  _TEMPDB; i<db->n
230ef 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e  Db; i++){.    in
230f0 74 20 6a 20 3d 20 28 69 3c 32 29 20 3f 20 69 5e  t j = (i<2) ? i^
230f1 31 20 3a 20 69 3b 20 20 2f 2a 20 53 65 61 72 63  1 : i;  /* Searc
230f2 68 20 54 45 4d 50 20 62 65 66 6f 72 65 20 4d 41  h TEMP before MA
230f3 49 4e 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a 44  IN */.    if( zD
230f4 62 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49  b && sqlite3StrI
230f5 43 6d 70 28 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a  Cmp(db->aDb[j].z
230f6 4e 61 6d 65 2c 20 7a 44 62 29 20 29 20 63 6f 6e  Name, zDb) ) con
230f7 74 69 6e 75 65 3b 0a 20 20 20 20 70 54 72 69 67  tinue;.    pTrig
230f8 67 65 72 20 3d 20 73 71 6c 69 74 65 33 48 61 73  ger = sqlite3Has
230f9 68 46 69 6e 64 28 26 28 64 62 2d 3e 61 44 62 5b  hFind(&(db->aDb[
230fa 6a 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72 69 67  j].pSchema->trig
230fb 48 61 73 68 29 2c 20 7a 4e 61 6d 65 2c 20 6e 4e  Hash), zName, nN
230fc 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70 54  ame);.    if( pT
230fd 72 69 67 67 65 72 20 29 20 62 72 65 61 6b 3b 0a  rigger ) break;.
230fe 20 20 7d 0a 20 20 69 66 28 20 21 70 54 72 69 67    }.  if( !pTrig
230ff 67 65 72 20 29 7b 0a 20 20 20 20 69 66 28 20 21  ger ){.    if( !
23100 6e 6f 45 72 72 20 29 7b 0a 20 20 20 20 20 20 73  noErr ){.      s
23101 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
23102 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20  Parse, "no such 
23103 74 72 69 67 67 65 72 3a 20 25 53 22 2c 20 70 4e  trigger: %S", pN
23104 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20  ame, 0);.    }. 
23105 20 20 20 67 6f 74 6f 20 64 72 6f 70 5f 74 72 69     goto drop_tri
23106 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  gger_cleanup;.  
23107 7d 0a 20 20 73 71 6c 69 74 65 33 44 72 6f 70 54  }.  sqlite3DropT
23108 72 69 67 67 65 72 50 74 72 28 70 50 61 72 73 65  riggerPtr(pParse
23109 2c 20 70 54 72 69 67 67 65 72 29 3b 0a 0a 64 72  , pTrigger);..dr
2310a 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  op_trigger_clean
2310b 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63  up:.  sqlite3Src
2310c 4c 69 73 74 44 65 6c 65 74 65 28 70 4e 61 6d 65  ListDelete(pName
2310d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
2310e 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
2310f 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74  the Table struct
23110 75 72 65 20 66 6f 72 20 74 68 65 20 74 61 62 6c  ure for the tabl
23111 65 20 74 68 61 74 20 61 20 74 72 69 67 67 65 72  e that a trigger
23112 0a 2a 2a 20 69 73 20 73 65 74 20 6f 6e 2e 0a 2a  .** is set on..*
23113 2f 0a 73 74 61 74 69 63 20 54 61 62 6c 65 20 2a  /.static Table *
23114 74 61 62 6c 65 4f 66 54 72 69 67 67 65 72 28 54  tableOfTrigger(T
23115 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72  rigger *pTrigger
23116 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 73 74 72  ){.  int n = str
23117 6c 65 6e 28 70 54 72 69 67 67 65 72 2d 3e 74 61  len(pTrigger->ta
23118 62 6c 65 29 20 2b 20 31 3b 0a 20 20 72 65 74 75  ble) + 1;.  retu
23119 72 6e 20 73 71 6c 69 74 65 33 48 61 73 68 46 69  rn sqlite3HashFi
2311a 6e 64 28 26 70 54 72 69 67 67 65 72 2d 3e 70 54  nd(&pTrigger->pT
2311b 61 62 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73  abSchema->tblHas
2311c 68 2c 20 70 54 72 69 67 67 65 72 2d 3e 74 61 62  h, pTrigger->tab
2311d 6c 65 2c 20 6e 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  le, n);.}.../*.*
2311e 2a 20 44 72 6f 70 20 61 20 74 72 69 67 67 65 72  * Drop a trigger
2311f 20 67 69 76 65 6e 20 61 20 70 6f 69 6e 74 65 72   given a pointer
23120 20 74 6f 20 74 68 61 74 20 74 72 69 67 67 65 72   to that trigger
23121 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  . .*/.SQLITE_PRI
23122 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
23123 33 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28  3DropTriggerPtr(
23124 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
23125 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72  rigger *pTrigger
23126 29 7b 0a 20 20 54 61 62 6c 65 20 20 20 2a 70 54  ){.  Table   *pT
23127 61 62 6c 65 3b 0a 20 20 56 64 62 65 20 2a 76 3b  able;.  Vdbe *v;
23128 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
23129 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
2312a 6e 74 20 69 44 62 3b 0a 0a 20 20 69 44 62 20 3d  nt iDb;..  iDb =
2312b 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
2312c 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62  Index(pParse->db
2312d 2c 20 70 54 72 69 67 67 65 72 2d 3e 70 53 63 68  , pTrigger->pSch
2312e 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ema);.  assert( 
2312f 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62  iDb>=0 && iDb<db
23130 2d 3e 6e 44 62 20 29 3b 0a 20 20 70 54 61 62 6c  ->nDb );.  pTabl
23131 65 20 3d 20 74 61 62 6c 65 4f 66 54 72 69 67 67  e = tableOfTrigg
23132 65 72 28 70 54 72 69 67 67 65 72 29 3b 0a 20 20  er(pTrigger);.  
23133 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 20 29  assert( pTable )
23134 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  ;.  assert( pTab
23135 6c 65 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 72  le->pSchema==pTr
23136 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 20 7c  igger->pSchema |
23137 7c 20 69 44 62 3d 3d 31 20 29 3b 0a 23 69 66 6e  | iDb==1 );.#ifn
23138 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
23139 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20  AUTHORIZATION.  
2313a 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d  {.    int code =
2313b 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49   SQLITE_DROP_TRI
2313c 47 47 45 52 3b 0a 20 20 20 20 63 6f 6e 73 74 20  GGER;.    const 
2313d 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e  char *zDb = db->
2313e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a  aDb[iDb].zName;.
2313f 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
23140 7a 54 61 62 20 3d 20 53 43 48 45 4d 41 5f 54 41  zTab = SCHEMA_TA
23141 42 4c 45 28 69 44 62 29 3b 0a 20 20 20 20 69 66  BLE(iDb);.    if
23142 28 20 69 44 62 3d 3d 31 20 29 20 63 6f 64 65 20  ( iDb==1 ) code 
23143 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45  = SQLITE_DROP_TE
23144 4d 50 5f 54 52 49 47 47 45 52 3b 0a 20 20 20 20  MP_TRIGGER;.    
23145 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43  if( sqlite3AuthC
23146 68 65 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64  heck(pParse, cod
23147 65 2c 20 70 54 72 69 67 67 65 72 2d 3e 6e 61 6d  e, pTrigger->nam
23148 65 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65  e, pTable->zName
23149 2c 20 7a 44 62 29 20 7c 7c 0a 20 20 20 20 20 20  , zDb) ||.      
2314a 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
2314b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
2314c 44 45 4c 45 54 45 2c 20 7a 54 61 62 2c 20 30 2c  DELETE, zTab, 0,
2314d 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 72   zDb) ){.      r
2314e 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d  eturn;.    }.  }
2314f 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47 65  .#endif..  /* Ge
23150 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 64  nerate code to d
23151 65 73 74 72 6f 79 20 74 68 65 20 64 61 74 61 62  estroy the datab
23152 61 73 65 20 72 65 63 6f 72 64 20 6f 66 20 74 68  ase record of th
23153 65 20 74 72 69 67 67 65 72 2e 0a 20 20 2a 2f 0a  e trigger..  */.
23154 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65    assert( pTable
23155 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 76 20  !=0 );.  if( (v 
23156 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
23157 28 70 50 61 72 73 65 29 29 21 3d 30 20 29 7b 0a  (pParse))!=0 ){.
23158 20 20 20 20 69 6e 74 20 62 61 73 65 3b 0a 20 20      int base;.  
23159 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56    static const V
2315a 64 62 65 4f 70 4c 69 73 74 20 64 72 6f 70 54 72  dbeOpList dropTr
2315b 69 67 67 65 72 5b 5d 20 3d 20 7b 0a 20 20 20 20  igger[] = {.    
2315c 20 20 7b 20 4f 50 5f 52 65 77 69 6e 64 2c 20 20    { OP_Rewind,  
2315d 20 20 20 30 2c 20 41 44 44 52 28 39 29 2c 20 20     0, ADDR(9),  
2315e 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53  0},.      { OP_S
2315f 74 72 69 6e 67 38 2c 20 20 20 20 30 2c 20 31 2c  tring8,    0, 1,
23160 20 20 20 20 20 20 20 20 30 7d 2c 20 2f 2a 20 31          0}, /* 1
23161 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 43   */.      { OP_C
23162 6f 6c 75 6d 6e 2c 20 20 20 20 20 30 2c 20 31 2c  olumn,     0, 1,
23163 20 20 20 20 20 20 20 20 32 7d 2c 0a 20 20 20 20          2},.    
23164 20 20 7b 20 4f 50 5f 4e 65 2c 20 20 20 20 20 20    { OP_Ne,      
23165 20 20 20 32 2c 20 41 44 44 52 28 38 29 2c 20 20     2, ADDR(8),  
23166 31 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53  1},.      { OP_S
23167 74 72 69 6e 67 38 2c 20 20 20 20 30 2c 20 31 2c  tring8,    0, 1,
23168 20 20 20 20 20 20 20 20 30 7d 2c 20 2f 2a 20 34          0}, /* 4
23169 3a 20 22 74 72 69 67 67 65 72 22 20 2a 2f 0a 20  : "trigger" */. 
2316a 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6c 75 6d 6e       { OP_Column
2316b 2c 20 20 20 20 20 30 2c 20 30 2c 20 20 20 20 20  ,     0, 0,     
2316c 20 20 20 32 7d 2c 0a 20 20 20 20 20 20 7b 20 4f     2},.      { O
2316d 50 5f 4e 65 2c 20 20 20 20 20 20 20 20 20 32 2c  P_Ne,         2,
2316e 20 41 44 44 52 28 38 29 2c 20 20 31 7d 2c 0a 20   ADDR(8),  1},. 
2316f 20 20 20 20 20 7b 20 4f 50 5f 44 65 6c 65 74 65       { OP_Delete
23170 2c 20 20 20 20 20 30 2c 20 30 2c 20 20 20 20 20  ,     0, 0,     
23171 20 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f     0},.      { O
23172 50 5f 4e 65 78 74 2c 20 20 20 20 20 20 20 30 2c  P_Next,       0,
23173 20 41 44 44 52 28 31 29 2c 20 20 30 7d 2c 20 2f   ADDR(1),  0}, /
23174 2a 20 38 20 2a 2f 0a 20 20 20 20 7d 3b 0a 0a 20  * 8 */.    };.. 
23175 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57     sqlite3BeginW
23176 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50  riteOperation(pP
23177 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20  arse, 0, iDb);. 
23178 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61     sqlite3OpenMa
23179 73 74 65 72 54 61 62 6c 65 28 70 50 61 72 73 65  sterTable(pParse
2317a 2c 20 69 44 62 29 3b 0a 20 20 20 20 62 61 73 65  , iDb);.    base
2317b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2317c 64 4f 70 4c 69 73 74 28 76 2c 20 20 41 72 72 61  dOpList(v,  Arra
2317d 79 53 69 7a 65 28 64 72 6f 70 54 72 69 67 67 65  ySize(dropTrigge
2317e 72 29 2c 20 64 72 6f 70 54 72 69 67 67 65 72 29  r), dropTrigger)
2317f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
23180 65 43 68 61 6e 67 65 50 34 28 76 2c 20 62 61 73  eChangeP4(v, bas
23181 65 2b 31 2c 20 70 54 72 69 67 67 65 72 2d 3e 6e  e+1, pTrigger->n
23182 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  ame, 0);.    sql
23183 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
23184 28 76 2c 20 62 61 73 65 2b 34 2c 20 22 74 72 69  (v, base+4, "tri
23185 67 67 65 72 22 2c 20 50 34 5f 53 54 41 54 49 43  gger", P4_STATIC
23186 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68  );.    sqlite3Ch
23187 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73  angeCookie(pPars
23188 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c  e, iDb);.    sql
23189 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2318a 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 30 2c 20 30  , OP_Close, 0, 0
2318b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2318c 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44  beAddOp4(v, OP_D
2318d 72 6f 70 54 72 69 67 67 65 72 2c 20 69 44 62 2c  ropTrigger, iDb,
2318e 20 30 2c 20 30 2c 20 70 54 72 69 67 67 65 72 2d   0, 0, pTrigger-
2318f 3e 6e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 7d  >name, 0);.  }.}
23190 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 61  ../*.** Remove a
23191 20 74 72 69 67 67 65 72 20 66 72 6f 6d 20 74 68   trigger from th
23192 65 20 68 61 73 68 20 74 61 62 6c 65 73 20 6f 66  e hash tables of
23193 20 74 68 65 20 73 71 6c 69 74 65 2a 20 70 6f 69   the sqlite* poi
23194 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  nter..*/.SQLITE_
23195 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
23196 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c  ite3UnlinkAndDel
23197 65 74 65 54 72 69 67 67 65 72 28 73 71 6c 69 74  eteTrigger(sqlit
23198 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c  e3 *db, int iDb,
23199 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
2319a 6d 65 29 7b 0a 20 20 54 72 69 67 67 65 72 20 2a  me){.  Trigger *
2319b 70 54 72 69 67 67 65 72 3b 0a 20 20 69 6e 74 20  pTrigger;.  int 
2319c 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a  nName = strlen(z
2319d 4e 61 6d 65 29 3b 0a 20 20 70 54 72 69 67 67 65  Name);.  pTrigge
2319e 72 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49  r = sqlite3HashI
2319f 6e 73 65 72 74 28 26 28 64 62 2d 3e 61 44 62 5b  nsert(&(db->aDb[
231a0 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72  iDb].pSchema->tr
231a1 69 67 48 61 73 68 29 2c 0a 20 20 20 20 20 20 20  igHash),.       
231a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
231a3 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 2c 20 6e          zName, n
231a4 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 69 66 28 20  Name, 0);.  if( 
231a5 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20  pTrigger ){.    
231a6 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 20 3d 20  Table *pTable = 
231a7 74 61 62 6c 65 4f 66 54 72 69 67 67 65 72 28 70  tableOfTrigger(p
231a8 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 61 73  Trigger);.    as
231a9 73 65 72 74 28 20 70 54 61 62 6c 65 21 3d 30 20  sert( pTable!=0 
231aa 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 6c  );.    if( pTabl
231ab 65 2d 3e 70 54 72 69 67 67 65 72 20 3d 3d 20 70  e->pTrigger == p
231ac 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 20  Trigger ){.     
231ad 20 70 54 61 62 6c 65 2d 3e 70 54 72 69 67 67 65   pTable->pTrigge
231ae 72 20 3d 20 70 54 72 69 67 67 65 72 2d 3e 70 4e  r = pTrigger->pN
231af 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ext;.    }else{.
231b0 20 20 20 20 20 20 54 72 69 67 67 65 72 20 2a 63        Trigger *c
231b1 63 20 3d 20 70 54 61 62 6c 65 2d 3e 70 54 72 69  c = pTable->pTri
231b2 67 67 65 72 3b 0a 20 20 20 20 20 20 77 68 69 6c  gger;.      whil
231b3 65 28 20 63 63 20 29 7b 20 0a 20 20 20 20 20 20  e( cc ){ .      
231b4 20 20 69 66 28 20 63 63 2d 3e 70 4e 65 78 74 20    if( cc->pNext 
231b5 3d 3d 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20  == pTrigger ){. 
231b6 20 20 20 20 20 20 20 20 20 63 63 2d 3e 70 4e 65           cc->pNe
231b7 78 74 20 3d 20 63 63 2d 3e 70 4e 65 78 74 2d 3e  xt = cc->pNext->
231b8 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 20  pNext;.         
231b9 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
231ba 7d 0a 20 20 20 20 20 20 20 20 63 63 20 3d 20 63  }.        cc = c
231bb 63 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  c->pNext;.      
231bc 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 63  }.      assert(c
231bd 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  c);.    }.    sq
231be 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67  lite3DeleteTrigg
231bf 65 72 28 70 54 72 69 67 67 65 72 29 3b 0a 20 20  er(pTrigger);.  
231c0 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53    db->flags |= S
231c1 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e  QLITE_InternChan
231c2 67 65 73 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ges;.  }.}../*.*
231c3 2a 20 70 45 4c 69 73 74 20 69 73 20 74 68 65 20  * pEList is the 
231c4 53 45 54 20 63 6c 61 75 73 65 20 6f 66 20 61 6e  SET clause of an
231c5 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e   UPDATE statemen
231c6 74 2e 20 20 45 61 63 68 20 65 6e 74 72 79 0a 2a  t.  Each entry.*
231c7 2a 20 69 6e 20 70 45 4c 69 73 74 20 69 73 20 6f  * in pEList is o
231c8 66 20 74 68 65 20 66 6f 72 6d 61 74 20 3c 69 64  f the format <id
231c9 3e 3d 3c 65 78 70 72 3e 2e 20 20 49 66 20 61 6e  >=<expr>.  If an
231ca 79 20 6f 66 20 74 68 65 20 65 6e 74 72 69 65 73  y of the entries
231cb 0a 2a 2a 20 69 6e 20 70 45 4c 69 73 74 20 68 61  .** in pEList ha
231cc 76 65 20 61 6e 20 3c 69 64 3e 20 77 68 69 63 68  ve an <id> which
231cd 20 6d 61 74 63 68 65 73 20 61 6e 20 69 64 65 6e   matches an iden
231ce 74 69 66 69 65 72 20 69 6e 20 70 49 64 4c 69 73  tifier in pIdLis
231cf 74 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 74 75 72  t,.** then retur
231d0 6e 20 54 52 55 45 2e 20 20 49 66 20 70 49 64 4c  n TRUE.  If pIdL
231d1 69 73 74 3d 3d 4e 55 4c 4c 2c 20 74 68 65 6e 20  ist==NULL, then 
231d2 69 74 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  it is considered
231d3 20 61 0a 2a 2a 20 77 69 6c 64 63 61 72 64 20 74   a.** wildcard t
231d4 68 61 74 20 6d 61 74 63 68 65 73 20 61 6e 79 74  hat matches anyt
231d5 68 69 6e 67 2e 20 20 4c 69 6b 65 77 69 73 65 20  hing.  Likewise 
231d6 69 66 20 70 45 4c 69 73 74 3d 3d 4e 55 4c 4c 20  if pEList==NULL 
231d7 74 68 65 6e 0a 2a 2a 20 69 74 20 6d 61 74 63 68  then.** it match
231d8 65 73 20 61 6e 79 74 68 69 6e 67 20 73 6f 20 61  es anything so a
231d9 6c 77 61 79 73 20 72 65 74 75 72 6e 20 74 72 75  lways return tru
231da 65 2e 20 20 52 65 74 75 72 6e 20 66 61 6c 73 65  e.  Return false
231db 20 6f 6e 6c 79 0a 2a 2a 20 69 66 20 74 68 65 72   only.** if ther
231dc 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a  e is no match..*
231dd 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65  /.static int che
231de 63 6b 43 6f 6c 75 6d 6e 4f 76 65 72 4c 61 70 28  ckColumnOverLap(
231df 49 64 4c 69 73 74 20 2a 70 49 64 4c 69 73 74 2c  IdList *pIdList,
231e0 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
231e1 74 29 7b 0a 20 20 69 6e 74 20 65 3b 0a 20 20 69  t){.  int e;.  i
231e2 66 28 20 21 70 49 64 4c 69 73 74 20 7c 7c 20 21  f( !pIdList || !
231e3 70 45 4c 69 73 74 20 29 20 72 65 74 75 72 6e 20  pEList ) return 
231e4 31 3b 0a 20 20 66 6f 72 28 65 3d 30 3b 20 65 3c  1;.  for(e=0; e<
231e5 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 65  pEList->nExpr; e
231e6 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  ++){.    if( sql
231e7 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28  ite3IdListIndex(
231e8 70 49 64 4c 69 73 74 2c 20 70 45 4c 69 73 74 2d  pIdList, pEList-
231e9 3e 61 5b 65 5d 2e 7a 4e 61 6d 65 29 3e 3d 30 20  >a[e].zName)>=0 
231ea 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a  ) return 1;.  }.
231eb 20 20 72 65 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a    return 0; .}..
231ec 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 62  /*.** Return a b
231ed 69 74 20 76 65 63 74 6f 72 20 74 6f 20 69 6e 64  it vector to ind
231ee 69 63 61 74 65 20 77 68 61 74 20 6b 69 6e 64 20  icate what kind 
231ef 6f 66 20 74 72 69 67 67 65 72 73 20 65 78 69 73  of triggers exis
231f0 74 20 66 6f 72 20 6f 70 65 72 61 74 69 6f 6e 0a  t for operation.
231f1 2a 2a 20 22 6f 70 22 20 6f 6e 20 74 61 62 6c 65  ** "op" on table
231f2 20 70 54 61 62 2e 20 20 49 66 20 70 43 68 61 6e   pTab.  If pChan
231f3 67 65 73 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20  ges is not NULL 
231f4 74 68 65 6e 20 69 74 20 69 73 20 61 20 6c 69 73  then it is a lis
231f5 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20  t of columns.** 
231f6 74 68 61 74 20 61 72 65 20 62 65 69 6e 67 20 75  that are being u
231f7 70 64 61 74 65 64 2e 20 20 54 72 69 67 67 65 72  pdated.  Trigger
231f8 73 20 6f 6e 6c 79 20 6d 61 74 63 68 20 69 66 20  s only match if 
231f9 74 68 65 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66  the ON clause of
231fa 20 74 68 65 0a 2a 2a 20 74 72 69 67 67 65 72 20   the.** trigger 
231fb 64 65 66 69 6e 69 74 69 6f 6e 20 6f 76 65 72 6c  definition overl
231fc 61 70 73 20 74 68 65 20 73 65 74 20 6f 66 20 63  aps the set of c
231fd 6f 6c 75 6d 6e 73 20 62 65 69 6e 67 20 75 70 64  olumns being upd
231fe 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ated..**.** The 
231ff 72 65 74 75 72 6e 65 64 20 62 69 74 20 76 65 63  returned bit vec
23200 74 6f 72 20 69 73 20 73 6f 6d 65 20 63 6f 6d 62  tor is some comb
23201 69 6e 61 74 69 6f 6e 20 6f 66 20 54 52 49 47 47  ination of TRIGG
23202 45 52 5f 42 45 46 4f 52 45 20 61 6e 64 0a 2a 2a  ER_BEFORE and.**
23203 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 2e 0a   TRIGGER_AFTER..
23204 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
23205 45 20 69 6e 74 20 73 71 6c 69 74 65 33 54 72 69  E int sqlite3Tri
23206 67 67 65 72 73 45 78 69 73 74 28 0a 20 20 50 61  ggersExist(.  Pa
23207 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
23208 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f        /* Used to
23209 20 63 68 65 63 6b 20 66 6f 72 20 72 65 63 75 72   check for recur
2320a 73 69 76 65 20 74 72 69 67 67 65 72 73 20 2a 2f  sive triggers */
2320b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20  .  Table *pTab, 
2320c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2320d 65 20 74 61 62 6c 65 20 74 68 65 20 63 6f 6e 74  e table the cont
2320e 61 69 6e 73 20 74 68 65 20 74 72 69 67 67 65 72  ains the trigger
2320f 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20  s */.  int op,  
23210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
23211 2a 20 6f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45  * one of TK_DELE
23212 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54  TE, TK_INSERT, T
23213 4b 5f 55 50 44 41 54 45 20 2a 2f 0a 20 20 45 78  K_UPDATE */.  Ex
23214 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65 73  prList *pChanges
23215 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73        /* Columns
23216 20 74 68 61 74 20 63 68 61 6e 67 65 20 69 6e 20   that change in 
23217 61 6e 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  an UPDATE statem
23218 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67  ent */.){.  Trig
23219 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 0a 20  ger *pTrigger;. 
2321a 20 69 6e 74 20 6d 61 73 6b 20 3d 20 30 3b 0a 0a   int mask = 0;..
2321b 20 20 70 54 72 69 67 67 65 72 20 3d 20 49 73 56    pTrigger = IsV
2321c 69 72 74 75 61 6c 28 70 54 61 62 29 20 3f 20 30  irtual(pTab) ? 0
2321d 20 3a 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65   : pTab->pTrigge
2321e 72 3b 0a 20 20 77 68 69 6c 65 28 20 70 54 72 69  r;.  while( pTri
2321f 67 67 65 72 20 29 7b 0a 20 20 20 20 69 66 28 20  gger ){.    if( 
23220 70 54 72 69 67 67 65 72 2d 3e 6f 70 3d 3d 6f 70  pTrigger->op==op
23221 20 26 26 20 63 68 65 63 6b 43 6f 6c 75 6d 6e 4f   && checkColumnO
23222 76 65 72 4c 61 70 28 70 54 72 69 67 67 65 72 2d  verLap(pTrigger-
23223 3e 70 43 6f 6c 75 6d 6e 73 2c 20 70 43 68 61 6e  >pColumns, pChan
23224 67 65 73 29 20 29 7b 0a 20 20 20 20 20 20 6d 61  ges) ){.      ma
23225 73 6b 20 7c 3d 20 70 54 72 69 67 67 65 72 2d 3e  sk |= pTrigger->
23226 74 72 5f 74 6d 3b 0a 20 20 20 20 7d 0a 20 20 20  tr_tm;.    }.   
23227 20 70 54 72 69 67 67 65 72 20 3d 20 70 54 72 69   pTrigger = pTri
23228 67 67 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d  gger->pNext;.  }
23229 0a 20 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a  .  return mask;.
2322a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  }../*.** Convert
2322b 20 74 68 65 20 70 53 74 65 70 2d 3e 74 61 72 67   the pStep->targ
2322c 65 74 20 74 6f 6b 65 6e 20 69 6e 74 6f 20 61 20  et token into a 
2322d 53 72 63 4c 69 73 74 20 61 6e 64 20 72 65 74 75  SrcList and retu
2322e 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20  rn a pointer.** 
2322f 74 6f 20 74 68 61 74 20 53 72 63 4c 69 73 74 2e  to that SrcList.
23230 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
23231 69 6e 65 20 61 64 64 73 20 61 20 73 70 65 63 69  ine adds a speci
23232 66 69 63 20 64 61 74 61 62 61 73 65 20 6e 61 6d  fic database nam
23233 65 2c 20 69 66 20 6e 65 65 64 65 64 2c 20 74 6f  e, if needed, to
23234 20 74 68 65 20 74 61 72 67 65 74 20 77 68 65 6e   the target when
23235 0a 2a 2a 20 66 6f 72 6d 69 6e 67 20 74 68 65 20  .** forming the 
23236 53 72 63 4c 69 73 74 2e 20 20 54 68 69 73 20 70  SrcList.  This p
23237 72 65 76 65 6e 74 73 20 61 20 74 72 69 67 67 65  revents a trigge
23238 72 20 69 6e 20 6f 6e 65 20 64 61 74 61 62 61 73  r in one databas
23239 65 20 66 72 6f 6d 0a 2a 2a 20 72 65 66 65 72 72  e from.** referr
2323a 69 6e 67 20 74 6f 20 61 20 74 61 72 67 65 74 20  ing to a target 
2323b 69 6e 20 61 6e 6f 74 68 65 72 20 64 61 74 61 62  in another datab
2323c 61 73 65 2e 20 20 41 6e 20 65 78 63 65 70 74 69  ase.  An excepti
2323d 6f 6e 20 69 73 20 77 68 65 6e 20 74 68 65 0a 2a  on is when the.*
2323e 2a 20 74 72 69 67 67 65 72 20 69 73 20 69 6e 20  * trigger is in 
2323f 54 45 4d 50 20 69 6e 20 77 68 69 63 68 20 63 61  TEMP in which ca
23240 73 65 20 69 74 20 63 61 6e 20 72 65 66 65 72 20  se it can refer 
23241 74 6f 20 61 6e 79 20 6f 74 68 65 72 20 64 61 74  to any other dat
23242 61 62 61 73 65 20 69 74 0a 2a 2a 20 77 61 6e 74  abase it.** want
23243 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 72 63  s..*/.static Src
23244 4c 69 73 74 20 2a 74 61 72 67 65 74 53 72 63 4c  List *targetSrcL
23245 69 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ist(.  Parse *pP
23246 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 54  arse,       /* T
23247 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65  he parsing conte
23248 78 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53  xt */.  TriggerS
23249 74 65 70 20 2a 70 53 74 65 70 20 20 20 2f 2a 20  tep *pStep   /* 
2324a 54 68 65 20 74 72 69 67 67 65 72 20 63 6f 6e 74  The trigger cont
2324b 61 69 6e 69 6e 67 20 74 68 65 20 74 61 72 67 65  aining the targe
2324c 74 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20  t token */.){.  
2324d 54 6f 6b 65 6e 20 73 44 62 3b 20 20 20 20 20 20  Token sDb;      
2324e 20 20 20 20 20 2f 2a 20 44 75 6d 6d 79 20 64 61       /* Dummy da
2324f 74 61 62 61 73 65 20 6e 61 6d 65 20 74 6f 6b 65  tabase name toke
23250 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  n */.  int iDb; 
23251 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
23252 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
23253 62 61 73 65 20 74 6f 20 75 73 65 20 2a 2f 0a 20  base to use */. 
23254 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 20   SrcList *pSrc; 
23255 20 20 20 20 20 20 2f 2a 20 53 72 63 4c 69 73 74        /* SrcList
23256 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20   to be returned 
23257 2a 2f 0a 0a 20 20 69 44 62 20 3d 20 73 71 6c 69  */..  iDb = sqli
23258 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
23259 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 74  (pParse->db, pSt
2325a 65 70 2d 3e 70 54 72 69 67 2d 3e 70 53 63 68 65  ep->pTrig->pSche
2325b 6d 61 29 3b 0a 20 20 69 66 28 20 69 44 62 3d 3d  ma);.  if( iDb==
2325c 30 20 7c 7c 20 69 44 62 3e 3d 32 20 29 7b 0a 20  0 || iDb>=2 ){. 
2325d 20 20 20 61 73 73 65 72 74 28 20 69 44 62 3c 70     assert( iDb<p
2325e 50 61 72 73 65 2d 3e 64 62 2d 3e 6e 44 62 20 29  Parse->db->nDb )
2325f 3b 0a 20 20 20 20 73 44 62 2e 7a 20 3d 20 28 75  ;.    sDb.z = (u
23260 38 2a 29 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61  8*)pParse->db->a
23261 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20  Db[iDb].zName;. 
23262 20 20 20 73 44 62 2e 6e 20 3d 20 73 74 72 6c 65     sDb.n = strle
23263 6e 28 28 63 68 61 72 2a 29 73 44 62 2e 7a 29 3b  n((char*)sDb.z);
23264 0a 20 20 20 20 70 53 72 63 20 3d 20 73 71 6c 69  .    pSrc = sqli
23265 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
23266 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 30 2c 20  (pParse->db, 0, 
23267 26 73 44 62 2c 20 26 70 53 74 65 70 2d 3e 74 61  &sDb, &pStep->ta
23268 72 67 65 74 29 3b 0a 20 20 7d 20 65 6c 73 65 20  rget);.  } else 
23269 7b 0a 20 20 20 20 70 53 72 63 20 3d 20 73 71 6c  {.    pSrc = sql
2326a 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
2326b 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 30 2c  d(pParse->db, 0,
2326c 20 26 70 53 74 65 70 2d 3e 74 61 72 67 65 74 2c   &pStep->target,
2326d 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   0);.  }.  retur
2326e 6e 20 70 53 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pSrc;.}../*.**
2326f 20 47 65 6e 65 72 61 74 65 20 56 44 42 45 20 63   Generate VDBE c
23270 6f 64 65 20 66 6f 72 20 7a 65 72 6f 20 6f 72 20  ode for zero or 
23271 6d 6f 72 65 20 73 74 61 74 65 6d 65 6e 74 73 20  more statements 
23272 69 6e 73 69 64 65 20 74 68 65 20 62 6f 64 79 20  inside the body 
23273 6f 66 20 61 0a 2a 2a 20 74 72 69 67 67 65 72 2e  of a.** trigger.
23274 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74    .*/.static int
23275 20 63 6f 64 65 54 72 69 67 67 65 72 50 72 6f 67   codeTriggerProg
23276 72 61 6d 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ram(.  Parse *pP
23277 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  arse,           
23278 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
23279 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72 69 67  ontext */.  Trig
2327a 67 65 72 53 74 65 70 20 2a 70 53 74 65 70 4c 69  gerStep *pStepLi
2327b 73 74 2c 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66  st,   /* List of
2327c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 73 69   statements insi
2327d 64 65 20 74 68 65 20 74 72 69 67 67 65 72 20 62  de the trigger b
2327e 6f 64 79 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63  ody */.  int orc
2327f 6f 6e 66 69 6e 20 20 20 20 20 20 20 20 20 20 20  onfin           
23280 20 20 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 20 61     /* Conflict a
23281 6c 67 6f 72 69 74 68 6d 2e 20 28 4f 45 5f 41 62  lgorithm. (OE_Ab
23282 6f 72 74 2c 20 65 74 63 29 20 2a 2f 20 20 0a 29  ort, etc) */  .)
23283 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20  {.  TriggerStep 
23284 2a 20 70 54 72 69 67 67 65 72 53 74 65 70 20 3d  * pTriggerStep =
23285 20 70 53 74 65 70 4c 69 73 74 3b 0a 20 20 69 6e   pStepList;.  in
23286 74 20 6f 72 63 6f 6e 66 3b 0a 20 20 56 64 62 65  t orconf;.  Vdbe
23287 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
23288 64 62 65 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  dbe;.  sqlite3 *
23289 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2328a 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54 72 69  ..  assert( pTri
2328b 67 67 65 72 53 74 65 70 21 3d 30 20 29 3b 0a 20  ggerStep!=0 );. 
2328c 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b   assert( v!=0 );
2328d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2328e 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 6e 74 65  dOp2(v, OP_Conte
2328f 78 74 50 75 73 68 2c 20 30 2c 20 30 29 3b 0a 20  xtPush, 0, 0);. 
23290 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
23291 20 22 62 65 67 69 6e 20 74 72 69 67 67 65 72 20   "begin trigger 
23292 25 73 22 2c 20 70 53 74 65 70 4c 69 73 74 2d 3e  %s", pStepList->
23293 70 54 72 69 67 2d 3e 6e 61 6d 65 29 29 3b 0a 20  pTrig->name));. 
23294 20 77 68 69 6c 65 28 20 70 54 72 69 67 67 65 72   while( pTrigger
23295 53 74 65 70 20 29 7b 0a 20 20 20 20 6f 72 63 6f  Step ){.    orco
23296 6e 66 20 3d 20 28 6f 72 63 6f 6e 66 69 6e 20 3d  nf = (orconfin =
23297 3d 20 4f 45 5f 44 65 66 61 75 6c 74 29 3f 70 54  = OE_Default)?pT
23298 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 72 63 6f  riggerStep->orco
23299 6e 66 3a 6f 72 63 6f 6e 66 69 6e 3b 0a 20 20 20  nf:orconfin;.   
2329a 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61   pParse->trigSta
2329b 63 6b 2d 3e 6f 72 63 6f 6e 66 20 3d 20 6f 72 63  ck->orconf = orc
2329c 6f 6e 66 3b 0a 20 20 20 20 73 77 69 74 63 68 28  onf;.    switch(
2329d 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f   pTriggerStep->o
2329e 70 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20  p ){.      case 
2329f 54 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a 20 20 20  TK_SELECT: {.   
232a0 20 20 20 20 20 53 65 6c 65 63 74 20 2a 73 73 20       Select *ss 
232a1 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  = sqlite3SelectD
232a2 75 70 28 64 62 2c 20 70 54 72 69 67 67 65 72 53  up(db, pTriggerS
232a3 74 65 70 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20  tep->pSelect);. 
232a4 20 20 20 20 20 20 20 69 66 28 20 73 73 20 29 7b         if( ss ){
232a5 0a 20 20 20 20 20 20 20 20 20 20 53 65 6c 65 63  .          Selec
232a6 74 44 65 73 74 20 64 65 73 74 3b 0a 0a 20 20 20  tDest dest;..   
232a7 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65         sqlite3Se
232a8 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65  lectDestInit(&de
232a9 73 74 2c 20 53 52 54 5f 44 69 73 63 61 72 64 2c  st, SRT_Discard,
232aa 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73   0);.          s
232ab 71 6c 69 74 65 33 53 65 6c 65 63 74 52 65 73 6f  qlite3SelectReso
232ac 6c 76 65 28 70 50 61 72 73 65 2c 20 73 73 2c 20  lve(pParse, ss, 
232ad 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  0);.          sq
232ae 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72  lite3Select(pPar
232af 73 65 2c 20 73 73 2c 20 26 64 65 73 74 2c 20 30  se, ss, &dest, 0
232b0 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  , 0, 0, 0);.    
232b1 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c        sqlite3Sel
232b2 65 63 74 44 65 6c 65 74 65 28 73 73 29 3b 0a 20  ectDelete(ss);. 
232b3 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
232b4 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
232b5 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f 55 50        case TK_UP
232b6 44 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 20 20  DATE: {.        
232b7 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20  SrcList *pSrc;. 
232b8 20 20 20 20 20 20 20 70 53 72 63 20 3d 20 74 61         pSrc = ta
232b9 72 67 65 74 53 72 63 4c 69 73 74 28 70 50 61 72  rgetSrcList(pPar
232ba 73 65 2c 20 70 54 72 69 67 67 65 72 53 74 65 70  se, pTriggerStep
232bb 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
232bc 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
232bd 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 2c 20 30  OP_ResetCount, 0
232be 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 0);.        sq
232bf 6c 69 74 65 33 55 70 64 61 74 65 28 70 50 61 72  lite3Update(pPar
232c0 73 65 2c 20 70 53 72 63 2c 0a 20 20 20 20 20 20  se, pSrc,.      
232c1 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
232c2 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c  3ExprListDup(db,
232c3 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70   pTriggerStep->p
232c4 45 78 70 72 4c 69 73 74 29 2c 20 0a 20 20 20 20  ExprList), .    
232c5 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
232c6 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
232c7 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 57 68  TriggerStep->pWh
232c8 65 72 65 29 2c 20 6f 72 63 6f 6e 66 29 3b 0a 20  ere), orconf);. 
232c9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
232ca 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
232cb 65 73 65 74 43 6f 75 6e 74 2c 20 31 2c 20 30 29  esetCount, 1, 0)
232cc 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
232cd 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
232ce 61 73 65 20 54 4b 5f 49 4e 53 45 52 54 3a 20 7b  ase TK_INSERT: {
232cf 0a 20 20 20 20 20 20 20 20 53 72 63 4c 69 73 74  .        SrcList
232d0 20 2a 70 53 72 63 3b 0a 20 20 20 20 20 20 20 20   *pSrc;.        
232d1 70 53 72 63 20 3d 20 74 61 72 67 65 74 53 72 63  pSrc = targetSrc
232d2 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 72  List(pParse, pTr
232d3 69 67 67 65 72 53 74 65 70 29 3b 0a 20 20 20 20  iggerStep);.    
232d4 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
232d5 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 65  ddOp2(v, OP_Rese
232d6 74 43 6f 75 6e 74 2c 20 30 2c 20 30 29 3b 0a 20  tCount, 0, 0);. 
232d7 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 49 6e         sqlite3In
232d8 73 65 72 74 28 70 50 61 72 73 65 2c 20 70 53 72  sert(pParse, pSr
232d9 63 2c 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  c,.          sql
232da 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
232db 64 62 2c 20 70 54 72 69 67 67 65 72 53 74 65 70  db, pTriggerStep
232dc 2d 3e 70 45 78 70 72 4c 69 73 74 29 2c 20 0a 20  ->pExprList), . 
232dd 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
232de 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 54  SelectDup(db, pT
232df 72 69 67 67 65 72 53 74 65 70 2d 3e 70 53 65 6c  riggerStep->pSel
232e0 65 63 74 29 2c 20 0a 20 20 20 20 20 20 20 20 20  ect), .         
232e1 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75   sqlite3IdListDu
232e2 70 28 64 62 2c 20 70 54 72 69 67 67 65 72 53 74  p(db, pTriggerSt
232e3 65 70 2d 3e 70 49 64 4c 69 73 74 29 2c 20 6f 72  ep->pIdList), or
232e4 63 6f 6e 66 29 3b 0a 20 20 20 20 20 20 20 20 73  conf);.        s
232e5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
232e6 28 76 2c 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e  (v, OP_ResetCoun
232e7 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20  t, 1, 0);.      
232e8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
232e9 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f 44  .      case TK_D
232ea 45 4c 45 54 45 3a 20 7b 0a 20 20 20 20 20 20 20  ELETE: {.       
232eb 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a   SrcList *pSrc;.
232ec 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
232ed 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
232ee 52 65 73 65 74 43 6f 75 6e 74 2c 20 30 2c 20 30  ResetCount, 0, 0
232ef 29 3b 0a 20 20 20 20 20 20 20 20 70 53 72 63 20  );.        pSrc 
232f0 3d 20 74 61 72 67 65 74 53 72 63 4c 69 73 74 28  = targetSrcList(
232f1 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72  pParse, pTrigger
232f2 53 74 65 70 29 3b 0a 20 20 20 20 20 20 20 20 73  Step);.        s
232f3 71 6c 69 74 65 33 44 65 6c 65 74 65 46 72 6f 6d  qlite3DeleteFrom
232f4 28 70 50 61 72 73 65 2c 20 70 53 72 63 2c 20 0a  (pParse, pSrc, .
232f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
232f6 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
232f7 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 54 72  3ExprDup(db, pTr
232f8 69 67 67 65 72 53 74 65 70 2d 3e 70 57 68 65 72  iggerStep->pWher
232f9 65 29 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  e));.        sql
232fa 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
232fb 2c 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 2c  , OP_ResetCount,
232fc 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   1, 0);.        
232fd 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
232fe 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20       default:.  
232ff 20 20 20 20 20 20 61 73 73 65 72 74 28 30 29 3b        assert(0);
23300 0a 20 20 20 20 7d 20 0a 20 20 20 20 70 54 72 69  .    } .    pTri
23301 67 67 65 72 53 74 65 70 20 3d 20 70 54 72 69 67  ggerStep = pTrig
23302 67 65 72 53 74 65 70 2d 3e 70 4e 65 78 74 3b 0a  gerStep->pNext;.
23303 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
23304 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f  eAddOp2(v, OP_Co
23305 6e 74 65 78 74 50 6f 70 2c 20 30 2c 20 30 29 3b  ntextPop, 0, 0);
23306 0a 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28  .  VdbeComment((
23307 76 2c 20 22 65 6e 64 20 74 72 69 67 67 65 72 20  v, "end trigger 
23308 25 73 22 2c 20 70 53 74 65 70 4c 69 73 74 2d 3e  %s", pStepList->
23309 70 54 72 69 67 2d 3e 6e 61 6d 65 29 29 3b 0a 0a  pTrig->name));..
2330a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
2330b 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 63 61 6c  *.** This is cal
2330c 6c 65 64 20 74 6f 20 63 6f 64 65 20 46 4f 52 20  led to code FOR 
2330d 45 41 43 48 20 52 4f 57 20 74 72 69 67 67 65 72  EACH ROW trigger
2330e 73 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68  s..**.** When th
2330f 65 20 63 6f 64 65 20 74 68 61 74 20 74 68 69 73  e code that this
23310 20 66 75 6e 63 74 69 6f 6e 20 67 65 6e 65 72 61   function genera
23311 74 65 73 20 69 73 20 65 78 65 63 75 74 65 64 2c  tes is executed,
23312 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a   the following .
23313 2a 2a 20 6d 75 73 74 20 62 65 20 74 72 75 65 3a  ** must be true:
23314 0a 2a 2a 0a 2a 2a 20 31 2e 20 4e 6f 20 63 75 72  .**.** 1. No cur
23315 73 6f 72 73 20 6d 61 79 20 62 65 20 6f 70 65 6e  sors may be open
23316 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74   in the main dat
23317 61 62 61 73 65 2e 20 20 28 42 75 74 20 6e 65 77  abase.  (But new
23318 49 64 78 20 61 6e 64 20 6f 6c 64 49 64 78 0a 2a  Idx and oldIdx.*
23319 2a 20 20 20 20 63 61 6e 20 62 65 20 69 6e 64 69  *    can be indi
2331a 63 65 73 20 6f 66 20 63 75 72 73 6f 72 73 20 69  ces of cursors i
2331b 6e 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  n temporary tabl
2331c 65 73 2e 20 20 53 65 65 20 62 65 6c 6f 77 2e 29  es.  See below.)
2331d 0a 2a 2a 0a 2a 2a 20 32 2e 20 49 66 20 74 68 65  .**.** 2. If the
2331e 20 74 72 69 67 67 65 72 73 20 62 65 69 6e 67 20   triggers being 
2331f 63 6f 64 65 64 20 61 72 65 20 4f 4e 20 49 4e 53  coded are ON INS
23320 45 52 54 20 6f 72 20 4f 4e 20 55 50 44 41 54 45  ERT or ON UPDATE
23321 20 74 72 69 67 67 65 72 73 2c 20 74 68 65 6e 0a   triggers, then.
23322 2a 2a 20 20 20 20 61 20 74 65 6d 70 6f 72 61 72  **    a temporar
23323 79 20 76 64 62 65 20 63 75 72 73 6f 72 20 28 69  y vdbe cursor (i
23324 6e 64 65 78 20 6e 65 77 49 64 78 29 20 6d 75 73  ndex newIdx) mus
23325 74 20 62 65 20 6f 70 65 6e 20 61 6e 64 20 70 6f  t be open and po
23326 69 6e 74 69 6e 67 20 61 74 0a 2a 2a 20 20 20 20  inting at.**    
23327 61 20 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67  a row containing
23328 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 73 75   values to be su
23329 62 73 74 69 74 75 74 65 64 20 66 6f 72 20 6e 65  bstituted for ne
2332a 77 2e 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20  w.* expressions 
2332b 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20 74 72 69  in the.**    tri
2332c 67 67 65 72 20 70 72 6f 67 72 61 6d 28 73 29 2e  gger program(s).
2332d 0a 2a 2a 0a 2a 2a 20 33 2e 20 49 66 20 74 68 65  .**.** 3. If the
2332e 20 74 72 69 67 67 65 72 73 20 62 65 69 6e 67 20   triggers being 
2332f 63 6f 64 65 64 20 61 72 65 20 4f 4e 20 44 45 4c  coded are ON DEL
23330 45 54 45 20 6f 72 20 4f 4e 20 55 50 44 41 54 45  ETE or ON UPDATE
23331 20 74 72 69 67 67 65 72 73 2c 20 74 68 65 6e 0a   triggers, then.
23332 2a 2a 20 20 20 20 61 20 74 65 6d 70 6f 72 61 72  **    a temporar
23333 79 20 76 64 62 65 20 63 75 72 73 6f 72 20 28 69  y vdbe cursor (i
23334 6e 64 65 78 20 6f 6c 64 49 64 78 29 20 6d 75 73  ndex oldIdx) mus
23335 74 20 62 65 20 6f 70 65 6e 20 61 6e 64 20 70 6f  t be open and po
23336 69 6e 74 69 6e 67 20 61 74 0a 2a 2a 20 20 20 20  inting at.**    
23337 61 20 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67  a row containing
23338 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 73 75   values to be su
23339 62 73 74 69 74 75 74 65 64 20 66 6f 72 20 6f 6c  bstituted for ol
2333a 64 2e 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20  d.* expressions 
2333b 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20 74 72 69  in the.**    tri
2333c 67 67 65 72 20 70 72 6f 67 72 61 6d 28 73 29 2e  gger program(s).
2333d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 79 20 61  .**.** If they a
2333e 72 65 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65  re not NULL, the
2333f 20 70 69 4f 6c 64 43 6f 6c 4d 61 73 6b 20 61 6e   piOldColMask an
23340 64 20 70 69 4e 65 77 43 6f 6c 4d 61 73 6b 20 6f  d piNewColMask o
23341 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 73 0a  utput variables.
23342 2a 2a 20 61 72 65 20 73 65 74 20 74 6f 20 76 61  ** are set to va
23343 6c 75 65 73 20 74 68 61 74 20 64 65 73 63 72 69  lues that descri
23344 62 65 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 75  be the columns u
23345 73 65 64 20 62 79 20 74 68 65 20 74 72 69 67 67  sed by the trigg
23346 65 72 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 69 6e  er program.** in
23347 20 74 68 65 20 4f 4c 44 2e 2a 20 61 6e 64 20 4e   the OLD.* and N
23348 45 57 2e 2a 20 74 61 62 6c 65 73 20 72 65 73 70  EW.* tables resp
23349 65 63 74 69 76 65 6c 79 2e 20 49 66 20 63 6f 6c  ectively. If col
2334a 75 6d 6e 20 4e 20 6f 66 20 74 68 65 20 0a 2a 2a  umn N of the .**
2334b 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 69 73   pseudo-table is
2334c 20 72 65 61 64 20 61 74 20 6c 65 61 73 74 20 6f   read at least o
2334d 6e 63 65 2c 20 74 68 65 20 63 6f 72 72 65 73 70  nce, the corresp
2334e 6f 6e 64 69 6e 67 20 62 69 74 20 6f 66 20 74 68  onding bit of th
2334f 65 20 6f 75 74 70 75 74 0a 2a 2a 20 6d 61 73 6b  e output.** mask
23350 20 69 73 20 73 65 74 2e 20 49 66 20 61 20 63 6f   is set. If a co
23351 6c 75 6d 6e 20 77 69 74 68 20 61 6e 20 69 6e 64  lumn with an ind
23352 65 78 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  ex greater than 
23353 33 32 20 69 73 20 72 65 61 64 2c 20 74 68 65 0a  32 is read, the.
23354 2a 2a 20 6f 75 74 70 75 74 20 6d 61 73 6b 20 69  ** output mask i
23355 73 20 73 65 74 20 74 6f 20 74 68 65 20 73 70 65  s set to the spe
23356 63 69 61 6c 20 76 61 6c 75 65 20 30 78 66 66 66  cial value 0xfff
23357 66 66 66 66 66 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c  fffff..**.*/.SQL
23358 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
23359 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72  sqlite3CodeRowTr
2335a 69 67 67 65 72 28 0a 20 20 50 61 72 73 65 20 2a  igger(.  Parse *
2335b 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a  pParse,       /*
2335c 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a   Parse context *
2335d 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20  /.  int op,     
2335e 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
2335f 6f 66 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b  of TK_UPDATE, TK
23360 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 44 45 4c 45  _INSERT, TK_DELE
23361 54 45 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  TE */.  ExprList
23362 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 2f 2a 20   *pChanges,  /* 
23363 43 68 61 6e 67 65 73 20 6c 69 73 74 20 66 6f 72  Changes list for
23364 20 61 6e 79 20 55 50 44 41 54 45 20 4f 46 20 74   any UPDATE OF t
23365 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 69 6e 74  riggers */.  int
23366 20 74 72 5f 74 6d 2c 20 20 20 20 20 20 20 20 20   tr_tm,         
23367 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 52 49 47    /* One of TRIG
23368 47 45 52 5f 42 45 46 4f 52 45 2c 20 54 52 49 47  GER_BEFORE, TRIG
23369 47 45 52 5f 41 46 54 45 52 20 2a 2f 0a 20 20 54  GER_AFTER */.  T
2336a 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20  able *pTab,     
2336b 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
2336c 20 74 6f 20 63 6f 64 65 20 74 72 69 67 67 65 72   to code trigger
2336d 73 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20  s from */.  int 
2336e 6e 65 77 49 64 78 2c 20 20 20 20 20 20 20 20 20  newIdx,         
2336f 20 2f 2a 20 54 68 65 20 69 6e 64 69 63 65 20 6f   /* The indice o
23370 66 20 74 68 65 20 22 6e 65 77 22 20 72 6f 77 20  f the "new" row 
23371 74 6f 20 61 63 63 65 73 73 20 2a 2f 0a 20 20 69  to access */.  i
23372 6e 74 20 6f 6c 64 49 64 78 2c 20 20 20 20 20 20  nt oldIdx,      
23373 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 69 63      /* The indic
23374 65 20 6f 66 20 74 68 65 20 22 6f 6c 64 22 20 72  e of the "old" r
23375 6f 77 20 74 6f 20 61 63 63 65 73 73 20 2a 2f 0a  ow to access */.
23376 20 20 69 6e 74 20 6f 72 63 6f 6e 66 2c 20 20 20    int orconf,   
23377 20 20 20 20 20 20 20 2f 2a 20 4f 4e 20 43 4f 4e         /* ON CON
23378 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 2a 2f 0a  FLICT policy */.
23379 20 20 69 6e 74 20 69 67 6e 6f 72 65 4a 75 6d 70    int ignoreJump
2337a 2c 20 20 20 20 20 20 2f 2a 20 49 6e 73 74 72 75  ,      /* Instru
2337b 63 74 69 6f 6e 20 74 6f 20 6a 75 6d 70 20 74 6f  ction to jump to
2337c 20 66 6f 72 20 52 41 49 53 45 28 49 47 4e 4f 52   for RAISE(IGNOR
2337d 45 29 20 2a 2f 0a 20 20 75 33 32 20 2a 70 69 4f  E) */.  u32 *piO
2337e 6c 64 43 6f 6c 4d 61 73 6b 2c 20 20 20 2f 2a 20  ldColMask,   /* 
2337f 4f 55 54 3a 20 4d 61 73 6b 20 6f 66 20 63 6f 6c  OUT: Mask of col
23380 75 6d 6e 73 20 75 73 65 64 20 66 72 6f 6d 20 74  umns used from t
23381 68 65 20 4f 4c 44 2e 2a 20 74 61 62 6c 65 20 2a  he OLD.* table *
23382 2f 0a 20 20 75 33 32 20 2a 70 69 4e 65 77 43 6f  /.  u32 *piNewCo
23383 6c 4d 61 73 6b 20 20 20 20 2f 2a 20 4f 55 54 3a  lMask    /* OUT:
23384 20 4d 61 73 6b 20 6f 66 20 63 6f 6c 75 6d 6e 73   Mask of columns
23385 20 75 73 65 64 20 66 72 6f 6d 20 74 68 65 20 4e   used from the N
23386 45 57 2e 2a 20 74 61 62 6c 65 20 2a 2f 0a 29 7b  EW.* table */.){
23387 0a 20 20 54 72 69 67 67 65 72 20 2a 70 3b 0a 20  .  Trigger *p;. 
23388 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
23389 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 54 72 69  Parse->db;.  Tri
2338a 67 67 65 72 53 74 61 63 6b 20 74 72 69 67 53 74  ggerStack trigSt
2338b 61 63 6b 45 6e 74 72 79 3b 0a 0a 20 20 74 72 69  ackEntry;..  tri
2338c 67 53 74 61 63 6b 45 6e 74 72 79 2e 6f 6c 64 43  gStackEntry.oldC
2338d 6f 6c 4d 61 73 6b 20 3d 20 30 3b 0a 20 20 74 72  olMask = 0;.  tr
2338e 69 67 53 74 61 63 6b 45 6e 74 72 79 2e 6e 65 77  igStackEntry.new
2338f 43 6f 6c 4d 61 73 6b 20 3d 20 30 3b 0a 0a 20 20  ColMask = 0;..  
23390 61 73 73 65 72 74 28 6f 70 20 3d 3d 20 54 4b 5f  assert(op == TK_
23391 55 50 44 41 54 45 20 7c 7c 20 6f 70 20 3d 3d 20  UPDATE || op == 
23392 54 4b 5f 49 4e 53 45 52 54 20 7c 7c 20 6f 70 20  TK_INSERT || op 
23393 3d 3d 20 54 4b 5f 44 45 4c 45 54 45 29 3b 0a 20  == TK_DELETE);. 
23394 20 61 73 73 65 72 74 28 74 72 5f 74 6d 20 3d 3d   assert(tr_tm ==
23395 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20   TRIGGER_BEFORE 
23396 7c 7c 20 74 72 5f 74 6d 20 3d 3d 20 54 52 49 47  || tr_tm == TRIG
23397 47 45 52 5f 41 46 54 45 52 20 29 3b 0a 0a 20 20  GER_AFTER );..  
23398 61 73 73 65 72 74 28 6e 65 77 49 64 78 20 21 3d  assert(newIdx !=
23399 20 2d 31 20 7c 7c 20 6f 6c 64 49 64 78 20 21 3d   -1 || oldIdx !=
2339a 20 2d 31 29 3b 0a 0a 20 20 66 6f 72 28 70 3d 70   -1);..  for(p=p
2339b 54 61 62 2d 3e 70 54 72 69 67 67 65 72 3b 20 70  Tab->pTrigger; p
2339c 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20  ; p=p->pNext){. 
2339d 20 20 20 69 6e 74 20 66 69 72 65 5f 74 68 69 73     int fire_this
2339e 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 44 65   = 0;..    /* De
2339f 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20  termine whether 
233a0 77 65 20 73 68 6f 75 6c 64 20 63 6f 64 65 20 74  we should code t
233a1 68 69 73 20 74 72 69 67 67 65 72 20 2a 2f 0a 20  his trigger */. 
233a2 20 20 20 69 66 28 20 0a 20 20 20 20 20 20 70 2d     if( .      p-
233a3 3e 6f 70 3d 3d 6f 70 20 26 26 20 0a 20 20 20 20  >op==op && .    
233a4 20 20 70 2d 3e 74 72 5f 74 6d 3d 3d 74 72 5f 74    p->tr_tm==tr_t
233a5 6d 20 26 26 20 0a 20 20 20 20 20 20 28 70 2d 3e  m && .      (p->
233a6 70 53 63 68 65 6d 61 3d 3d 70 2d 3e 70 54 61 62  pSchema==p->pTab
233a7 53 63 68 65 6d 61 20 7c 7c 20 70 2d 3e 70 53 63  Schema || p->pSc
233a8 68 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b 31 5d  hema==db->aDb[1]
233a9 2e 70 53 63 68 65 6d 61 29 20 26 26 0a 20 20 20  .pSchema) &&.   
233aa 20 20 20 28 6f 70 21 3d 54 4b 5f 55 50 44 41 54     (op!=TK_UPDAT
233ab 45 7c 7c 21 70 2d 3e 70 43 6f 6c 75 6d 6e 73 7c  E||!p->pColumns|
233ac 7c 63 68 65 63 6b 43 6f 6c 75 6d 6e 4f 76 65 72  |checkColumnOver
233ad 4c 61 70 28 70 2d 3e 70 43 6f 6c 75 6d 6e 73 2c  Lap(p->pColumns,
233ae 70 43 68 61 6e 67 65 73 29 29 0a 20 20 20 20 29  pChanges)).    )
233af 7b 0a 20 20 20 20 20 20 54 72 69 67 67 65 72 53  {.      TriggerS
233b0 74 61 63 6b 20 2a 70 53 3b 20 20 20 20 20 20 2f  tack *pS;      /
233b1 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 72 69  * Pointer to tri
233b2 67 67 65 72 2d 73 74 61 63 6b 20 65 6e 74 72 79  gger-stack entry
233b3 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 70 53   */.      for(pS
233b4 3d 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61  =pParse->trigSta
233b5 63 6b 3b 20 70 53 20 26 26 20 70 21 3d 70 53 2d  ck; pS && p!=pS-
233b6 3e 70 54 72 69 67 67 65 72 3b 20 70 53 3d 70 53  >pTrigger; pS=pS
233b7 2d 3e 70 4e 65 78 74 29 7b 7d 0a 20 20 20 20 20  ->pNext){}.     
233b8 20 69 66 28 20 21 70 53 20 29 7b 0a 20 20 20 20   if( !pS ){.    
233b9 20 20 20 20 66 69 72 65 5f 74 68 69 73 20 3d 20      fire_this = 
233ba 31 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 20 30  1;.      }.#if 0
233bb 20 20 20 20 2f 2a 20 47 69 76 65 20 6e 6f 20 77      /* Give no w
233bc 61 72 6e 69 6e 67 20 66 6f 72 20 72 65 63 75 72  arning for recur
233bd 73 69 76 65 20 74 72 69 67 67 65 72 73 2e 20 20  sive triggers.  
233be 4a 75 73 74 20 64 6f 20 6e 6f 74 20 64 6f 20 74  Just do not do t
233bf 68 65 6d 20 2a 2f 0a 20 20 20 20 20 20 65 6c 73  hem */.      els
233c0 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e{.        sqlit
233c1 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
233c2 65 2c 20 22 72 65 63 75 72 73 69 76 65 20 74 72  e, "recursive tr
233c3 69 67 67 65 72 73 20 6e 6f 74 20 73 75 70 70 6f  iggers not suppo
233c4 72 74 65 64 20 28 25 73 29 22 2c 0a 20 20 20 20  rted (%s)",.    
233c5 20 20 20 20 20 20 20 20 70 2d 3e 6e 61 6d 65 29          p->name)
233c6 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
233c7 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
233c8 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
233c9 20 20 7d 0a 20 0a 20 20 20 20 69 66 28 20 66 69    }. .    if( fi
233ca 72 65 5f 74 68 69 73 20 29 7b 0a 20 20 20 20 20  re_this ){.     
233cb 20 69 6e 74 20 65 6e 64 54 72 69 67 67 65 72 3b   int endTrigger;
233cc 0a 20 20 20 20 20 20 45 78 70 72 20 2a 20 77 68  .      Expr * wh
233cd 65 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 41 75  enExpr;.      Au
233ce 74 68 43 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65  thContext sConte
233cf 78 74 3b 0a 20 20 20 20 20 20 4e 61 6d 65 43 6f  xt;.      NameCo
233d0 6e 74 65 78 74 20 73 4e 43 3b 0a 0a 23 69 66 6e  ntext sNC;..#ifn
233d1 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
233d2 54 52 41 43 45 0a 20 20 20 20 20 20 73 71 6c 69  TRACE.      sqli
233d3 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 70 50  te3VdbeAddOp4(pP
233d4 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f  arse->pVdbe, OP_
233d5 54 72 61 63 65 2c 20 30 2c 20 30 2c 20 30 2c 0a  Trace, 0, 0, 0,.
233d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
233d7 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d          sqlite3M
233d8 50 72 69 6e 74 66 28 64 62 2c 20 22 2d 2d 20 54  Printf(db, "-- T
233d9 52 49 47 47 45 52 20 25 73 22 2c 20 70 2d 3e 6e  RIGGER %s", p->n
233da 61 6d 65 29 2c 0a 20 20 20 20 20 20 20 20 20 20  ame),.          
233db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 34                P4
233dc 5f 44 59 4e 41 4d 49 43 29 3b 0a 23 65 6e 64 69  _DYNAMIC);.#endi
233dd 66 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26  f.      memset(&
233de 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  sNC, 0, sizeof(s
233df 4e 43 29 29 3b 0a 20 20 20 20 20 20 73 4e 43 2e  NC));.      sNC.
233e0 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b  pParse = pParse;
233e1 0a 0a 20 20 20 20 20 20 2f 2a 20 50 75 73 68 20  ..      /* Push 
233e2 61 6e 20 65 6e 74 72 79 20 6f 6e 20 74 6f 20 74  an entry on to t
233e3 68 65 20 74 72 69 67 67 65 72 20 73 74 61 63 6b  he trigger stack
233e4 20 2a 2f 0a 20 20 20 20 20 20 74 72 69 67 53 74   */.      trigSt
233e5 61 63 6b 45 6e 74 72 79 2e 70 54 72 69 67 67 65  ackEntry.pTrigge
233e6 72 20 3d 20 70 3b 0a 20 20 20 20 20 20 74 72 69  r = p;.      tri
233e7 67 53 74 61 63 6b 45 6e 74 72 79 2e 6e 65 77 49  gStackEntry.newI
233e8 64 78 20 3d 20 6e 65 77 49 64 78 3b 0a 20 20 20  dx = newIdx;.   
233e9 20 20 20 74 72 69 67 53 74 61 63 6b 45 6e 74 72     trigStackEntr
233ea 79 2e 6f 6c 64 49 64 78 20 3d 20 6f 6c 64 49 64  y.oldIdx = oldId
233eb 78 3b 0a 20 20 20 20 20 20 74 72 69 67 53 74 61  x;.      trigSta
233ec 63 6b 45 6e 74 72 79 2e 70 54 61 62 20 3d 20 70  ckEntry.pTab = p
233ed 54 61 62 3b 0a 20 20 20 20 20 20 74 72 69 67 53  Tab;.      trigS
233ee 74 61 63 6b 45 6e 74 72 79 2e 70 4e 65 78 74 20  tackEntry.pNext 
233ef 3d 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74  = pParse->trigSt
233f0 61 63 6b 3b 0a 20 20 20 20 20 20 74 72 69 67 53  ack;.      trigS
233f1 74 61 63 6b 45 6e 74 72 79 2e 69 67 6e 6f 72 65  tackEntry.ignore
233f2 4a 75 6d 70 20 3d 20 69 67 6e 6f 72 65 4a 75 6d  Jump = ignoreJum
233f3 70 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  p;.      pParse-
233f4 3e 74 72 69 67 53 74 61 63 6b 20 3d 20 26 74 72  >trigStack = &tr
233f5 69 67 53 74 61 63 6b 45 6e 74 72 79 3b 0a 20 20  igStackEntry;.  
233f6 20 20 20 20 73 71 6c 69 74 65 33 41 75 74 68 43      sqlite3AuthC
233f7 6f 6e 74 65 78 74 50 75 73 68 28 70 50 61 72 73  ontextPush(pPars
233f8 65 2c 20 26 73 43 6f 6e 74 65 78 74 2c 20 70 2d  e, &sContext, p-
233f9 3e 6e 61 6d 65 29 3b 0a 0a 20 20 20 20 20 20 2f  >name);..      /
233fa 2a 20 63 6f 64 65 20 74 68 65 20 57 48 45 4e 20  * code the WHEN 
233fb 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20  clause */.      
233fc 65 6e 64 54 72 69 67 67 65 72 20 3d 20 73 71 6c  endTrigger = sql
233fd 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
233fe 6c 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 29  l(pParse->pVdbe)
233ff 3b 0a 20 20 20 20 20 20 77 68 65 6e 45 78 70 72  ;.      whenExpr
23400 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
23401 70 28 64 62 2c 20 70 2d 3e 70 57 68 65 6e 29 3b  p(db, p->pWhen);
23402 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6d  .      if( db->m
23403 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 73  allocFailed || s
23404 71 6c 69 74 65 33 45 78 70 72 52 65 73 6f 6c 76  qlite3ExprResolv
23405 65 4e 61 6d 65 73 28 26 73 4e 43 2c 20 77 68 65  eNames(&sNC, whe
23406 6e 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20  nExpr) ){.      
23407 20 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74    pParse->trigSt
23408 61 63 6b 20 3d 20 74 72 69 67 53 74 61 63 6b 45  ack = trigStackE
23409 6e 74 72 79 2e 70 4e 65 78 74 3b 0a 20 20 20 20  ntry.pNext;.    
2340a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
2340b 65 6c 65 74 65 28 77 68 65 6e 45 78 70 72 29 3b  elete(whenExpr);
2340c 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
2340d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
2340e 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61   sqlite3ExprIfFa
2340f 6c 73 65 28 70 50 61 72 73 65 2c 20 77 68 65 6e  lse(pParse, when
23410 45 78 70 72 2c 20 65 6e 64 54 72 69 67 67 65 72  Expr, endTrigger
23411 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  , SQLITE_JUMPIFN
23412 55 4c 4c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ULL);.      sqli
23413 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 77 68  te3ExprDelete(wh
23414 65 6e 45 78 70 72 29 3b 0a 0a 20 20 20 20 20 20  enExpr);..      
23415 63 6f 64 65 54 72 69 67 67 65 72 50 72 6f 67 72  codeTriggerProgr
23416 61 6d 28 70 50 61 72 73 65 2c 20 70 2d 3e 73 74  am(pParse, p->st
23417 65 70 5f 6c 69 73 74 2c 20 6f 72 63 6f 6e 66 29  ep_list, orconf)
23418 3b 20 0a 0a 20 20 20 20 20 20 2f 2a 20 50 6f 70  ; ..      /* Pop
23419 20 74 68 65 20 65 6e 74 72 79 20 6f 66 66 20 74   the entry off t
2341a 68 65 20 74 72 69 67 67 65 72 20 73 74 61 63 6b  he trigger stack
2341b 20 2a 2f 0a 20 20 20 20 20 20 70 50 61 72 73 65   */.      pParse
2341c 2d 3e 74 72 69 67 53 74 61 63 6b 20 3d 20 74 72  ->trigStack = tr
2341d 69 67 53 74 61 63 6b 45 6e 74 72 79 2e 70 4e 65  igStackEntry.pNe
2341e 78 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  xt;.      sqlite
2341f 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 28  3AuthContextPop(
23420 26 73 43 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 20  &sContext);..   
23421 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
23422 73 6f 6c 76 65 4c 61 62 65 6c 28 70 50 61 72 73  solveLabel(pPars
23423 65 2d 3e 70 56 64 62 65 2c 20 65 6e 64 54 72 69  e->pVdbe, endTri
23424 67 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  gger);.    }.  }
23425 0a 20 20 69 66 28 20 70 69 4f 6c 64 43 6f 6c 4d  .  if( piOldColM
23426 61 73 6b 20 29 20 2a 70 69 4f 6c 64 43 6f 6c 4d  ask ) *piOldColM
23427 61 73 6b 20 7c 3d 20 74 72 69 67 53 74 61 63 6b  ask |= trigStack
23428 45 6e 74 72 79 2e 6f 6c 64 43 6f 6c 4d 61 73 6b  Entry.oldColMask
23429 3b 0a 20 20 69 66 28 20 70 69 4e 65 77 43 6f 6c  ;.  if( piNewCol
2342a 4d 61 73 6b 20 29 20 2a 70 69 4e 65 77 43 6f 6c  Mask ) *piNewCol
2342b 4d 61 73 6b 20 7c 3d 20 74 72 69 67 53 74 61 63  Mask |= trigStac
2342c 6b 45 6e 74 72 79 2e 6e 65 77 43 6f 6c 4d 61 73  kEntry.newColMas
2342d 6b 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  k;.  return 0;.}
2342e 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
2342f 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
23430 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a 2f 2a 2a  TRIGGER) */../**
23431 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
23432 20 6f 66 20 74 72 69 67 67 65 72 2e 63 20 2a 2a   of trigger.c **
23433 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23434 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23435 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
23436 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
23437 69 6e 20 66 69 6c 65 20 75 70 64 61 74 65 2e 63  in file update.c
23438 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
23439 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2343a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
2343b 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
2343c 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
2343d 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
2343e 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
2343f 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
23440 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
23441 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
23442 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
23443 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
23444 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
23445 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
23446 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
23447 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
23448 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
23449 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
2344a 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
2344b 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
2344c 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
2344d 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
2344e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2344f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23451 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23452 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
23453 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65   contains C code
23454 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61   routines that a
23455 72 65 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65  re called by the
23456 20 70 61 72 73 65 72 0a 2a 2a 20 74 6f 20 68 61   parser.** to ha
23457 6e 64 6c 65 20 55 50 44 41 54 45 20 73 74 61 74  ndle UPDATE stat
23458 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 24 49  ements..**.** $I
23459 64 3a 20 75 70 64 61 74 65 2e 63 2c 76 20 31 2e  d: update.c,v 1.
2345a 31 37 38 20 32 30 30 38 2f 30 34 2f 32 38 20 31  178 2008/04/28 1
2345b 38 3a 34 36 3a 34 33 20 64 72 68 20 45 78 70 20  8:46:43 drh Exp 
2345c 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  $.*/..#ifndef SQ
2345d 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2345e 4c 54 41 42 4c 45 0a 2f 2a 20 46 6f 72 77 61 72  LTABLE./* Forwar
2345f 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f  d declaration */
23460 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 70 64  .static void upd
23461 61 74 65 56 69 72 74 75 61 6c 54 61 62 6c 65 28  ateVirtualTable(
23462 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
23463 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70  ,       /* The p
23464 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
23465 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  /.  SrcList *pSr
23466 63 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  c,       /* The 
23467 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 74 6f  virtual table to
23468 20 62 65 20 6d 6f 64 69 66 69 65 64 20 2a 2f 0a   be modified */.
23469 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20    Table *pTab,  
2346a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69         /* The vi
2346b 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20  rtual table */. 
2346c 20 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e   ExprList *pChan
2346d 67 65 73 2c 20 20 2f 2a 20 54 68 65 20 63 6f 6c  ges,  /* The col
2346e 75 6d 6e 73 20 74 6f 20 63 68 61 6e 67 65 20 69  umns to change i
2346f 6e 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61  n the UPDATE sta
23470 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72  tement */.  Expr
23471 20 2a 70 52 6f 77 69 64 45 78 70 72 2c 20 20 20   *pRowidExpr,   
23472 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 75   /* Expression u
23473 73 65 64 20 74 6f 20 72 65 63 6f 6d 70 75 74 65  sed to recompute
23474 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20   the rowid */.  
23475 69 6e 74 20 2a 61 58 52 65 66 2c 20 20 20 20 20  int *aXRef,     
23476 20 20 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20       /* Mapping 
23477 66 72 6f 6d 20 63 6f 6c 75 6d 6e 73 20 6f 66 20  from columns of 
23478 70 54 61 62 20 74 6f 20 65 6e 74 72 69 65 73 20  pTab to entries 
23479 69 6e 20 70 43 68 61 6e 67 65 73 20 2a 2f 0a 20  in pChanges */. 
2347a 20 45 78 70 72 20 2a 70 57 68 65 72 65 20 20 20   Expr *pWhere   
2347b 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63        /* WHERE c
2347c 6c 61 75 73 65 20 6f 66 20 74 68 65 20 55 50 44  lause of the UPD
2347d 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  ATE statement */
2347e 0a 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .);.#endif /* SQ
2347f 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
23480 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  LTABLE */../*.**
23481 20 54 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   The most recent
23482 6c 79 20 63 6f 64 65 64 20 69 6e 73 74 72 75 63  ly coded instruc
23483 74 69 6f 6e 20 77 61 73 20 61 6e 20 4f 50 5f 43  tion was an OP_C
23484 6f 6c 75 6d 6e 20 74 6f 20 72 65 74 72 69 65 76  olumn to retriev
23485 65 20 74 68 65 0a 2a 2a 20 69 2d 74 68 20 63 6f  e the.** i-th co
23486 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 70 54  lumn of table pT
23487 61 62 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65  ab. This routine
23488 20 73 65 74 73 20 74 68 65 20 50 34 20 70 61 72   sets the P4 par
23489 61 6d 65 74 65 72 20 6f 66 20 74 68 65 20 0a 2a  ameter of the .*
2348a 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 74  * OP_Column to t
2348b 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
2348c 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20  , if any..**.** 
2348d 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75  The default valu
2348e 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69 73  e of a column is
2348f 20 73 70 65 63 69 66 69 65 64 20 62 79 20 61 20   specified by a 
23490 44 45 46 41 55 4c 54 20 63 6c 61 75 73 65 20 69  DEFAULT clause i
23491 6e 20 74 68 65 20 0a 2a 2a 20 63 6f 6c 75 6d 6e  n the .** column
23492 20 64 65 66 69 6e 69 74 69 6f 6e 2e 20 54 68 69   definition. Thi
23493 73 20 77 61 73 20 65 69 74 68 65 72 20 73 75 70  s was either sup
23494 70 6c 69 65 64 20 62 79 20 74 68 65 20 75 73 65  plied by the use
23495 72 20 77 68 65 6e 20 74 68 65 20 74 61 62 6c 65  r when the table
23496 0a 2a 2a 20 77 61 73 20 63 72 65 61 74 65 64 2c  .** was created,
23497 20 6f 72 20 61 64 64 65 64 20 6c 61 74 65 72 20   or added later 
23498 74 6f 20 74 68 65 20 74 61 62 6c 65 20 64 65 66  to the table def
23499 69 6e 69 74 69 6f 6e 20 62 79 20 61 6e 20 41 4c  inition by an AL
2349a 54 45 52 20 54 41 42 4c 45 0a 2a 2a 20 63 6f 6d  TER TABLE.** com
2349b 6d 61 6e 64 2e 20 49 66 20 74 68 65 20 6c 61 74  mand. If the lat
2349c 74 65 72 2c 20 74 68 65 6e 20 74 68 65 20 72 6f  ter, then the ro
2349d 77 2d 72 65 63 6f 72 64 73 20 69 6e 20 74 68 65  w-records in the
2349e 20 74 61 62 6c 65 20 62 74 72 65 65 20 6f 6e 20   table btree on 
2349f 64 69 73 6b 0a 2a 2a 20 6d 61 79 20 6e 6f 74 20  disk.** may not 
234a0 63 6f 6e 74 61 69 6e 20 61 20 76 61 6c 75 65 20  contain a value 
234a1 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61  for the column a
234a2 6e 64 20 74 68 65 20 64 65 66 61 75 6c 74 20 76  nd the default v
234a3 61 6c 75 65 2c 20 74 61 6b 65 6e 0a 2a 2a 20 66  alue, taken.** f
234a4 72 6f 6d 20 74 68 65 20 50 34 20 70 61 72 61 6d  rom the P4 param
234a5 65 74 65 72 20 6f 66 20 74 68 65 20 4f 50 5f 43  eter of the OP_C
234a6 6f 6c 75 6d 6e 20 69 6e 73 74 72 75 63 74 69 6f  olumn instructio
234a7 6e 2c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  n, is returned i
234a8 6e 73 74 65 61 64 2e 0a 2a 2a 20 49 66 20 74 68  nstead..** If th
234a9 65 20 66 6f 72 6d 65 72 2c 20 74 68 65 6e 20 61  e former, then a
234aa 6c 6c 20 72 6f 77 2d 72 65 63 6f 72 64 73 20 61  ll row-records a
234ab 72 65 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  re guaranteed to
234ac 20 69 6e 63 6c 75 64 65 20 61 20 76 61 6c 75 65   include a value
234ad 0a 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f 6c 75  .** for the colu
234ae 6d 6e 20 61 6e 64 20 74 68 65 20 50 34 20 76 61  mn and the P4 va
234af 6c 75 65 20 69 73 20 6e 6f 74 20 72 65 71 75 69  lue is not requi
234b0 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6c 75 6d  red..**.** Colum
234b1 6e 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 63 72  n definitions cr
234b2 65 61 74 65 64 20 62 79 20 61 6e 20 41 4c 54 45  eated by an ALTE
234b3 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 20  R TABLE command 
234b4 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 0a 2a  may only have .*
234b5 2a 20 6c 69 74 65 72 61 6c 20 64 65 66 61 75 6c  * literal defaul
234b6 74 20 76 61 6c 75 65 73 20 73 70 65 63 69 66 69  t values specifi
234b7 65 64 3a 20 61 20 6e 75 6d 62 65 72 2c 20 6e 75  ed: a number, nu
234b8 6c 6c 20 6f 72 20 61 20 73 74 72 69 6e 67 2e 20  ll or a string. 
234b9 28 49 66 20 61 20 6d 6f 72 65 0a 2a 2a 20 63 6f  (If a more.** co
234ba 6d 70 6c 69 63 61 74 65 64 20 64 65 66 61 75 6c  mplicated defaul
234bb 74 20 65 78 70 72 65 73 73 69 6f 6e 20 76 61 6c  t expression val
234bc 75 65 20 77 61 73 20 70 72 6f 76 69 64 65 64 2c  ue was provided,
234bd 20 69 74 20 69 73 20 65 76 61 6c 75 61 74 65 64   it is evaluated
234be 20 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 41 4c   .** when the AL
234bf 54 45 52 20 54 41 42 4c 45 20 69 73 20 65 78 65  TER TABLE is exe
234c0 63 75 74 65 64 20 61 6e 64 20 6f 6e 65 20 6f 66  cuted and one of
234c1 20 74 68 65 20 6c 69 74 65 72 61 6c 20 76 61 6c   the literal val
234c2 75 65 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69  ues written.** i
234c3 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 5f 6d  nto the sqlite_m
234c4 61 73 74 65 72 20 74 61 62 6c 65 2e 29 0a 2a 2a  aster table.).**
234c5 0a 2a 2a 20 54 68 65 72 65 66 6f 72 65 2c 20 74  .** Therefore, t
234c6 68 65 20 50 34 20 70 61 72 61 6d 65 74 65 72 20  he P4 parameter 
234c7 69 73 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64  is only required
234c8 20 69 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   if the default 
234c9 76 61 6c 75 65 20 66 6f 72 0a 2a 2a 20 74 68 65  value for.** the
234ca 20 63 6f 6c 75 6d 6e 20 69 73 20 61 20 6c 69 74   column is a lit
234cb 65 72 61 6c 20 6e 75 6d 62 65 72 2c 20 73 74 72  eral number, str
234cc 69 6e 67 20 6f 72 20 6e 75 6c 6c 2e 20 54 68 65  ing or null. The
234cd 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f   sqlite3ValueFro
234ce 6d 45 78 70 72 28 29 0a 2a 2a 20 66 75 6e 63 74  mExpr().** funct
234cf 69 6f 6e 20 69 73 20 63 61 70 61 62 6c 65 20 6f  ion is capable o
234d0 66 20 74 72 61 6e 73 66 6f 72 6d 69 6e 67 20 74  f transforming t
234d1 68 65 73 65 20 74 79 70 65 73 20 6f 66 20 65 78  hese types of ex
234d2 70 72 65 73 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a  pressions into.*
234d3 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  * sqlite3_value 
234d4 6f 62 6a 65 63 74 73 2e 0a 2a 2f 0a 53 51 4c 49  objects..*/.SQLI
234d5 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
234d6 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66  sqlite3ColumnDef
234d7 61 75 6c 74 28 56 64 62 65 20 2a 76 2c 20 54 61  ault(Vdbe *v, Ta
234d8 62 6c 65 20 2a 70 54 61 62 2c 20 69 6e 74 20 69  ble *pTab, int i
234d9 29 7b 0a 20 20 69 66 28 20 70 54 61 62 20 26 26  ){.  if( pTab &&
234da 20 21 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20   !pTab->pSelect 
234db 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  ){.    sqlite3_v
234dc 61 6c 75 65 20 2a 70 56 61 6c 75 65 3b 0a 20 20  alue *pValue;.  
234dd 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 73    u8 enc = ENC(s
234de 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 29  qlite3VdbeDb(v))
234df 3b 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43  ;.    Column *pC
234e0 6f 6c 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c  ol = &pTab->aCol
234e1 5b 69 5d 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d  [i];.    VdbeCom
234e2 6d 65 6e 74 28 28 76 2c 20 22 25 73 2e 25 73 22  ment((v, "%s.%s"
234e3 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 70  , pTab->zName, p
234e4 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20  Col->zName));.  
234e5 20 20 61 73 73 65 72 74 28 20 69 3c 70 54 61 62    assert( i<pTab
234e6 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 73 71  ->nCol );.    sq
234e7 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78  lite3ValueFromEx
234e8 70 72 28 73 71 6c 69 74 65 33 56 64 62 65 44 62  pr(sqlite3VdbeDb
234e9 28 76 29 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c 74  (v), pCol->pDflt
234ea 2c 20 65 6e 63 2c 20 0a 20 20 20 20 20 20 20 20  , enc, .        
234eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
234ec 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 2c   pCol->affinity,
234ed 20 26 70 56 61 6c 75 65 29 3b 0a 20 20 20 20 69   &pValue);.    i
234ee 66 28 20 70 56 61 6c 75 65 20 29 7b 0a 20 20 20  f( pValue ){.   
234ef 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
234f0 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63  angeP4(v, -1, (c
234f1 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 56 61 6c  onst char *)pVal
234f2 75 65 2c 20 50 34 5f 4d 45 4d 29 3b 0a 20 20 20  ue, P4_MEM);.   
234f3 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
234f4 50 72 6f 63 65 73 73 20 61 6e 20 55 50 44 41 54  Process an UPDAT
234f5 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a  E statement..**.
234f6 2a 2a 20 20 20 55 50 44 41 54 45 20 4f 52 20 49  **   UPDATE OR I
234f7 47 4e 4f 52 45 20 74 61 62 6c 65 5f 77 78 79 7a  GNORE table_wxyz
234f8 20 53 45 54 20 61 3d 62 2c 20 63 3d 64 20 57 48   SET a=b, c=d WH
234f9 45 52 45 20 65 3c 35 20 41 4e 44 20 66 20 4e 4f  ERE e<5 AND f NO
234fa 54 20 4e 55 4c 4c 3b 0a 2a 2a 20 20 20 20 20 20  T NULL;.**      
234fb 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 2f 20 5c 5f      \_______/ \_
234fc 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 5c 5f 5f  _______/     \__
234fd 5f 5f 5f 5f 2f 20 20 20 20 20 20 20 5c 5f 5f 5f  ____/       \___
234fe 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a  _____________/.*
234ff 20 20 20 20 20 20 20 20 20 20 20 20 6f 6e 45 72              onEr
23500 72 6f 72 20 20 20 70 54 61 62 4c 69 73 74 20 20  ror   pTabList  
23501 20 20 20 20 70 43 68 61 6e 67 65 73 20 20 20 20      pChanges    
23502 20 20 20 20 20 20 20 20 20 70 57 68 65 72 65 0a           pWhere.
23503 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
23504 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 70  E void sqlite3Up
23505 64 61 74 65 28 0a 20 20 50 61 72 73 65 20 2a 70  date(.  Parse *p
23506 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f  Parse,         /
23507 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e  * The parser con
23508 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73  text */.  SrcLis
23509 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 20 20  t *pTabList,    
2350a 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 69 6e   /* The table in
2350b 20 77 68 69 63 68 20 77 65 20 73 68 6f 75 6c 64   which we should
2350c 20 63 68 61 6e 67 65 20 74 68 69 6e 67 73 20 2a   change things *
2350d 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 43  /.  ExprList *pC
2350e 68 61 6e 67 65 73 2c 20 20 20 20 2f 2a 20 54 68  hanges,    /* Th
2350f 69 6e 67 73 20 74 6f 20 62 65 20 63 68 61 6e 67  ings to be chang
23510 65 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57  ed */.  Expr *pW
23511 68 65 72 65 2c 20 20 20 20 20 20 20 20 20 20 2f  here,          /
23512 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75  * The WHERE clau
23513 73 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c  se.  May be null
23514 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f   */.  int onErro
23515 72 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  r            /* 
23516 48 6f 77 20 74 6f 20 68 61 6e 64 6c 65 20 63 6f  How to handle co
23517 6e 73 74 72 61 69 6e 74 20 65 72 72 6f 72 73 20  nstraint errors 
23518 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a  */.){.  int i, j
23519 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2351a 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20  * Loop counters 
2351b 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
2351c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
2351d 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 75  he table to be u
2351e 70 64 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20  pdated */.  int 
2351f 61 64 64 72 20 3d 20 30 3b 20 20 20 20 20 20 20  addr = 0;       
23520 20 20 20 2f 2a 20 56 44 42 45 20 69 6e 73 74 72     /* VDBE instr
23521 75 63 74 69 6f 6e 20 61 64 64 72 65 73 73 20 6f  uction address o
23522 66 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  f the start of t
23523 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 57 68 65  he loop */.  Whe
23524 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20  reInfo *pWInfo; 
23525 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69      /* Informati
23526 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 57 48 45  on about the WHE
23527 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 56  RE clause */.  V
23528 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20  dbe *v;         
23529 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72        /* The vir
2352a 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e  tual database en
2352b 67 69 6e 65 20 2a 2f 0a 20 20 49 6e 64 65 78 20  gine */.  Index 
2352c 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20 20  *pIdx;          
2352d 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20   /* For looping 
2352e 6f 76 65 72 20 69 6e 64 69 63 65 73 20 2a 2f 0a  over indices */.
2352f 20 20 69 6e 74 20 6e 49 64 78 3b 20 20 20 20 20    int nIdx;     
23530 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
23531 65 72 20 6f 66 20 69 6e 64 69 63 65 73 20 74 68  er of indices th
23532 61 74 20 6e 65 65 64 20 75 70 64 61 74 69 6e 67  at need updating
23533 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b 20   */.  int iCur; 
23534 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
23535 56 44 42 45 20 43 75 72 73 6f 72 20 6e 75 6d 62  VDBE Cursor numb
23536 65 72 20 6f 66 20 70 54 61 62 20 2a 2f 0a 20 20  er of pTab */.  
23537 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
23538 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
23539 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65  tabase structure
2353a 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67 49   */.  int *aRegI
2353b 64 78 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20  dx = 0;      /* 
2353c 4f 6e 65 20 72 65 67 69 73 74 65 72 20 61 73 73  One register ass
2353d 69 67 6e 65 64 20 74 6f 20 65 61 63 68 20 69 6e  igned to each in
2353e 64 65 78 20 74 6f 20 62 65 20 75 70 64 61 74 65  dex to be update
2353f 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 58 52 65  d */.  int *aXRe
23540 66 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a  f = 0;        /*
23541 20 61 58 52 65 66 5b 69 5d 20 69 73 20 74 68 65   aXRef[i] is the
23542 20 69 6e 64 65 78 20 69 6e 20 70 43 68 61 6e 67   index in pChang
23543 65 73 2d 3e 61 5b 5d 20 6f 66 20 74 68 65 0a 20  es->a[] of the. 
23544 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23545 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 20 65 78          ** an ex
23546 70 72 65 73 73 69 6f 6e 20 66 6f 72 20 74 68 65  pression for the
23547 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20   i-th column of 
23548 74 68 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 20  the table..     
23549 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2354a 20 20 20 20 2a 2a 20 61 58 52 65 66 5b 69 5d 3d      ** aXRef[i]=
2354b 3d 2d 31 20 69 66 20 74 68 65 20 69 2d 74 68 20  =-1 if the i-th 
2354c 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 63 68  column is not ch
2354d 61 6e 67 65 64 2e 20 2a 2f 0a 20 20 69 6e 74 20  anged. */.  int 
2354e 63 68 6e 67 52 6f 77 69 64 3b 20 20 20 20 20 20  chngRowid;      
2354f 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
23550 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  e record number 
23551 69 73 20 62 65 69 6e 67 20 63 68 61 6e 67 65 64  is being changed
23552 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 6f 77   */.  Expr *pRow
23553 69 64 45 78 70 72 20 3d 20 30 3b 20 20 2f 2a 20  idExpr = 0;  /* 
23554 45 78 70 72 65 73 73 69 6f 6e 20 64 65 66 69 6e  Expression defin
23555 69 6e 67 20 74 68 65 20 6e 65 77 20 72 65 63 6f  ing the new reco
23556 72 64 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69  rd number */.  i
23557 6e 74 20 6f 70 65 6e 41 6c 6c 20 3d 20 30 3b 20  nt openAll = 0; 
23558 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
23559 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6e 65 65   all indices nee
2355a 64 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 2a  d to be opened *
2355b 2f 0a 20 20 41 75 74 68 43 6f 6e 74 65 78 74 20  /.  AuthContext 
2355c 73 43 6f 6e 74 65 78 74 3b 20 20 2f 2a 20 54 68  sContext;  /* Th
2355d 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  e authorization 
2355e 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 4e 61 6d  context */.  Nam
2355f 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20  eContext sNC;   
23560 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 2d      /* The name-
23561 63 6f 6e 74 65 78 74 20 74 6f 20 72 65 73 6f 6c  context to resol
23562 76 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69  ve expressions i
23563 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  n */.  int iDb; 
23564 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
23565 20 44 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   Database contai
23566 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 62  ning the table b
23567 65 69 6e 67 20 75 70 64 61 74 65 64 20 2a 2f 0a  eing updated */.
23568 20 20 69 6e 74 20 6a 31 3b 20 20 20 20 20 20 20    int j1;       
23569 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
2356a 65 73 73 65 73 20 6f 66 20 6a 75 6d 70 20 69 6e  esses of jump in
2356b 73 74 72 75 63 74 69 6f 6e 73 20 2a 2f 0a 20 20  structions */.  
2356c 69 6e 74 20 6f 6b 4f 6e 65 50 61 73 73 3b 20 20  int okOnePass;  
2356d 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66         /* True f
2356e 6f 72 20 6f 6e 65 2d 70 61 73 73 20 61 6c 67 6f  or one-pass algo
2356f 72 69 74 68 6d 20 77 69 74 68 6f 75 74 20 74 68  rithm without th
23570 65 20 46 49 46 4f 20 2a 2f 0a 0a 23 69 66 6e 64  e FIFO */..#ifnd
23571 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
23572 52 49 47 47 45 52 0a 20 20 69 6e 74 20 69 73 56  RIGGER.  int isV
23573 69 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20  iew;            
23574 20 20 20 20 20 20 2f 2a 20 54 72 79 69 6e 67 20        /* Trying 
23575 74 6f 20 75 70 64 61 74 65 20 61 20 76 69 65 77  to update a view
23576 20 2a 2f 0a 20 20 69 6e 74 20 74 72 69 67 67 65   */.  int trigge
23577 72 73 5f 65 78 69 73 74 20 3d 20 30 3b 20 20 20  rs_exist = 0;   
23578 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e     /* True if an
23579 79 20 72 6f 77 20 74 72 69 67 67 65 72 73 20 65  y row triggers e
2357a 78 69 73 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 20  xist */.#endif. 
2357b 20 69 6e 74 20 69 42 65 67 69 6e 41 66 74 65 72   int iBeginAfter
2357c 54 72 69 67 67 65 72 3b 20 20 20 20 20 20 2f 2a  Trigger;      /*
2357d 20 41 64 64 72 65 73 73 20 6f 66 20 61 66 74 65   Address of afte
2357e 72 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61  r trigger progra
2357f 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 45 6e 64 41  m */.  int iEndA
23580 66 74 65 72 54 72 69 67 67 65 72 3b 20 20 20 20  fterTrigger;    
23581 20 20 20 20 2f 2a 20 45 78 69 74 20 6f 66 20 61      /* Exit of a
23582 66 74 65 72 20 74 72 69 67 67 65 72 20 70 72 6f  fter trigger pro
23583 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 42  gram */.  int iB
23584 65 67 69 6e 42 65 66 6f 72 65 54 72 69 67 67 65  eginBeforeTrigge
23585 72 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73  r;     /* Addres
23586 73 20 6f 66 20 62 65 66 6f 72 65 20 74 72 69 67  s of before trig
23587 67 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20  ger program */. 
23588 20 69 6e 74 20 69 45 6e 64 42 65 66 6f 72 65 54   int iEndBeforeT
23589 72 69 67 67 65 72 3b 20 20 20 20 20 20 20 2f 2a  rigger;       /*
2358a 20 45 78 69 74 20 6f 66 20 62 65 66 6f 72 65 20   Exit of before 
2358b 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20  trigger program 
2358c 2a 2f 0a 20 20 75 33 32 20 6f 6c 64 5f 63 6f 6c  */.  u32 old_col
2358d 5f 6d 61 73 6b 20 3d 20 30 3b 20 20 20 20 20 20  _mask = 0;      
2358e 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 4f 4c 44    /* Mask of OLD
2358f 2e 2a 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 75 73  .* columns in us
23590 65 20 2a 2f 0a 20 20 75 33 32 20 6e 65 77 5f 63  e */.  u32 new_c
23591 6f 6c 5f 6d 61 73 6b 20 3d 20 30 3b 20 20 20 20  ol_mask = 0;    
23592 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 4e      /* Mask of N
23593 45 57 2e 2a 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  EW.* columns in 
23594 75 73 65 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 65  use */..  int ne
23595 77 49 64 78 20 20 20 20 20 20 3d 20 2d 31 3b 20  wIdx      = -1; 
23596 20 2f 2a 20 69 6e 64 65 78 20 6f 66 20 74 72 69   /* index of tri
23597 67 67 65 72 20 22 6e 65 77 22 20 74 65 6d 70 20  gger "new" temp 
23598 74 61 62 6c 65 20 20 20 20 20 20 20 2a 2f 0a 20  table       */. 
23599 20 69 6e 74 20 6f 6c 64 49 64 78 20 20 20 20 20   int oldIdx     
2359a 20 3d 20 2d 31 3b 20 20 2f 2a 20 69 6e 64 65 78   = -1;  /* index
2359b 20 6f 66 20 74 72 69 67 67 65 72 20 22 6f 6c 64   of trigger "old
2359c 22 20 74 65 6d 70 20 74 61 62 6c 65 20 20 20 20  " temp table    
2359d 20 20 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 67 69     */..  /* Regi
2359e 73 74 65 72 20 41 6c 6c 6f 63 61 74 69 6f 6e 73  ster Allocations
2359f 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 77   */.  int regRow
235a0 43 6f 75 6e 74 20 3d 20 30 3b 20 20 20 2f 2a 20  Count = 0;   /* 
235a1 41 20 63 6f 75 6e 74 20 6f 66 20 72 6f 77 73 20  A count of rows 
235a2 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 69 6e 74  changed */.  int
235a3 20 72 65 67 4f 6c 64 52 6f 77 69 64 3b 20 20 20   regOldRowid;   
235a4 20 20 20 20 2f 2a 20 54 68 65 20 6f 6c 64 20 72      /* The old r
235a5 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65  owid */.  int re
235a6 67 4e 65 77 52 6f 77 69 64 3b 20 20 20 20 20 20  gNewRowid;      
235a7 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f 77 69   /* The new rowi
235a8 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61  d */.  int regDa
235a9 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ta;           /*
235aa 20 4e 65 77 20 64 61 74 61 20 66 6f 72 20 74 68   New data for th
235ab 65 20 72 6f 77 20 2a 2f 0a 0a 20 20 73 43 6f 6e  e row */..  sCon
235ac 74 65 78 74 2e 70 50 61 72 73 65 20 3d 20 30 3b  text.pParse = 0;
235ad 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  .  db = pParse->
235ae 64 62 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65  db;.  if( pParse
235af 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61  ->nErr || db->ma
235b0 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
235b1 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c    goto update_cl
235b2 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 61 73 73  eanup;.  }.  ass
235b3 65 72 74 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e  ert( pTabList->n
235b4 53 72 63 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20  Src==1 );..  /* 
235b5 4c 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c 65  Locate the table
235b6 20 77 68 69 63 68 20 77 65 20 77 61 6e 74 20 74   which we want t
235b7 6f 20 75 70 64 61 74 65 2e 20 0a 20 20 2a 2f 0a  o update. .  */.
235b8 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
235b9 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50  SrcListLookup(pP
235ba 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 29 3b  arse, pTabList);
235bb 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29  .  if( pTab==0 )
235bc 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65   goto update_cle
235bd 61 6e 75 70 3b 0a 20 20 69 44 62 20 3d 20 73 71  anup;.  iDb = sq
235be 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
235bf 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  ex(pParse->db, p
235c0 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a  Tab->pSchema);..
235c1 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20    /* Figure out 
235c2 69 66 20 77 65 20 68 61 76 65 20 61 6e 79 20 74  if we have any t
235c3 72 69 67 67 65 72 73 20 61 6e 64 20 69 66 20 74  riggers and if t
235c4 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 0a 20  he table being. 
235c5 20 2a 2a 20 75 70 64 61 74 65 64 20 69 73 20 61   ** updated is a
235c6 20 76 69 65 77 0a 20 20 2a 2f 0a 23 69 66 6e 64   view.  */.#ifnd
235c7 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
235c8 52 49 47 47 45 52 0a 20 20 74 72 69 67 67 65 72  RIGGER.  trigger
235c9 73 5f 65 78 69 73 74 20 3d 20 73 71 6c 69 74 65  s_exist = sqlite
235ca 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28 70  3TriggersExist(p
235cb 50 61 72 73 65 2c 20 70 54 61 62 2c 20 54 4b 5f  Parse, pTab, TK_
235cc 55 50 44 41 54 45 2c 20 70 43 68 61 6e 67 65 73  UPDATE, pChanges
235cd 29 3b 0a 20 20 69 73 56 69 65 77 20 3d 20 70 54  );.  isView = pT
235ce 61 62 2d 3e 70 53 65 6c 65 63 74 21 3d 30 3b 0a  ab->pSelect!=0;.
235cf 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 74  #else.# define t
235d0 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 30 0a  riggers_exist 0.
235d1 23 20 64 65 66 69 6e 65 20 69 73 56 69 65 77 20  # define isView 
235d2 30 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  0.#endif.#ifdef 
235d3 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
235d4 0a 23 20 75 6e 64 65 66 20 69 73 56 69 65 77 0a  .# undef isView.
235d5 23 20 64 65 66 69 6e 65 20 69 73 56 69 65 77 20  # define isView 
235d6 30 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20  0.#endif..  if( 
235d7 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c  sqlite3IsReadOnl
235d8 79 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  y(pParse, pTab, 
235d9 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 29 20  triggers_exist) 
235da 29 7b 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 61  ){.    goto upda
235db 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  te_cleanup;.  }.
235dc 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 69 65    if( sqlite3Vie
235dd 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  wGetColumnNames(
235de 70 50 61 72 73 65 2c 20 70 54 61 62 29 20 29 7b  pParse, pTab) ){
235df 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65  .    goto update
235e0 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20  _cleanup;.  }.  
235e1 61 58 52 65 66 20 3d 20 73 71 6c 69 74 65 33 44  aXRef = sqlite3D
235e2 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73  bMallocRaw(db, s
235e3 69 7a 65 6f 66 28 69 6e 74 29 20 2a 20 70 54 61  izeof(int) * pTa
235e4 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 69 66 28  b->nCol );.  if(
235e5 20 61 58 52 65 66 3d 3d 30 20 29 20 67 6f 74 6f   aXRef==0 ) goto
235e6 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b   update_cleanup;
235e7 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54  .  for(i=0; i<pT
235e8 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 20 61  ab->nCol; i++) a
235e9 58 52 65 66 5b 69 5d 20 3d 20 2d 31 3b 0a 0a 20  XRef[i] = -1;.. 
235ea 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65   /* If there are
235eb 20 46 4f 52 20 45 41 43 48 20 52 4f 57 20 74 72   FOR EACH ROW tr
235ec 69 67 67 65 72 73 2c 20 61 6c 6c 6f 63 61 74 65  iggers, allocate
235ed 20 63 75 72 73 6f 72 73 20 66 6f 72 20 74 68 65   cursors for the
235ee 0a 20 20 2a 2a 20 73 70 65 63 69 61 6c 20 4f 4c  .  ** special OL
235ef 44 20 61 6e 64 20 4e 45 57 20 74 61 62 6c 65 73  D and NEW tables
235f0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 74 72 69 67  .  */.  if( trig
235f1 67 65 72 73 5f 65 78 69 73 74 20 29 7b 0a 20 20  gers_exist ){.  
235f2 20 20 6e 65 77 49 64 78 20 3d 20 70 50 61 72 73    newIdx = pPars
235f3 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 6f  e->nTab++;.    o
235f4 6c 64 49 64 78 20 3d 20 70 50 61 72 73 65 2d 3e  ldIdx = pParse->
235f5 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f  nTab++;.  }..  /
235f6 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 63 75 72  * Allocate a cur
235f7 73 6f 72 73 20 66 6f 72 20 74 68 65 20 6d 61 69  sors for the mai
235f8 6e 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  n database table
235f9 20 61 6e 64 20 66 6f 72 20 61 6c 6c 20 69 6e 64   and for all ind
235fa 69 63 65 73 2e 0a 20 20 2a 2a 20 54 68 65 20 69  ices..  ** The i
235fb 6e 64 65 78 20 63 75 72 73 6f 72 73 20 6d 69 67  ndex cursors mig
235fc 68 74 20 6e 6f 74 20 62 65 20 75 73 65 64 2c 20  ht not be used, 
235fd 62 75 74 20 69 66 20 74 68 65 79 20 61 72 65 20  but if they are 
235fe 75 73 65 64 20 74 68 65 79 0a 20 20 2a 2a 20 6e  used they.  ** n
235ff 65 65 64 20 74 6f 20 6f 63 63 75 72 20 72 69 67  eed to occur rig
23600 68 74 20 61 66 74 65 72 20 74 68 65 20 64 61 74  ht after the dat
23601 61 62 61 73 65 20 63 75 72 73 6f 72 2e 20 20 53  abase cursor.  S
23602 6f 20 67 6f 20 61 68 65 61 64 20 61 6e 64 0a 20  o go ahead and. 
23603 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 65 6e 6f   ** allocate eno
23604 75 67 68 20 73 70 61 63 65 2c 20 6a 75 73 74 20  ugh space, just 
23605 69 6e 20 63 61 73 65 2e 0a 20 20 2a 2f 0a 20 20  in case..  */.  
23606 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69  pTabList->a[0].i
23607 43 75 72 73 6f 72 20 3d 20 69 43 75 72 20 3d 20  Cursor = iCur = 
23608 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
23609 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d    for(pIdx=pTab-
2360a 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
2360b 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
2360c 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 54  {.    pParse->nT
2360d 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ab++;.  }..  /* 
2360e 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6e  Initialize the n
2360f 61 6d 65 2d 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ame-context */. 
23610 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c   memset(&sNC, 0,
23611 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20   sizeof(sNC));. 
23612 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50   sNC.pParse = pP
23613 61 72 73 65 3b 0a 20 20 73 4e 43 2e 70 53 72 63  arse;.  sNC.pSrc
23614 4c 69 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b  List = pTabList;
23615 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74  ..  /* Resolve t
23616 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  he column names 
23617 69 6e 20 61 6c 6c 20 74 68 65 20 65 78 70 72 65  in all the expre
23618 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 0a 20 20  ssions of the.  
23619 2a 2a 20 6f 66 20 74 68 65 20 55 50 44 41 54 45  ** of the UPDATE
2361a 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41 6c 73   statement.  Als
2361b 6f 20 66 69 6e 64 20 74 68 65 20 63 6f 6c 75 6d  o find the colum
2361c 6e 20 69 6e 64 65 78 0a 20 20 2a 2a 20 66 6f 72  n index.  ** for
2361d 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 74 6f 20   each column to 
2361e 62 65 20 75 70 64 61 74 65 64 20 69 6e 20 74 68  be updated in th
2361f 65 20 70 43 68 61 6e 67 65 73 20 61 72 72 61 79  e pChanges array
23620 2e 20 20 46 6f 72 20 65 61 63 68 0a 20 20 2a 2a  .  For each.  **
23621 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 75 70   column to be up
23622 64 61 74 65 64 2c 20 6d 61 6b 65 20 73 75 72 65  dated, make sure
23623 20 77 65 20 68 61 76 65 20 61 75 74 68 6f 72 69   we have authori
23624 7a 61 74 69 6f 6e 20 74 6f 20 63 68 61 6e 67 65  zation to change
23625 0a 20 20 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d  .  ** that colum
23626 6e 2e 0a 20 20 2a 2f 0a 20 20 63 68 6e 67 52 6f  n..  */.  chngRo
23627 77 69 64 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69  wid = 0;.  for(i
23628 3d 30 3b 20 69 3c 70 43 68 61 6e 67 65 73 2d 3e  =0; i<pChanges->
23629 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nExpr; i++){.   
2362a 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72   if( sqlite3Expr
2362b 52 65 73 6f 6c 76 65 4e 61 6d 65 73 28 26 73 4e  ResolveNames(&sN
2362c 43 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69  C, pChanges->a[i
2362d 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20  ].pExpr) ){.    
2362e 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c    goto update_cl
2362f 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20  eanup;.    }.   
23630 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 54 61 62   for(j=0; j<pTab
23631 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20  ->nCol; j++){.  
23632 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
23633 74 72 49 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f  trICmp(pTab->aCo
23634 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 70 43 68 61  l[j].zName, pCha
23635 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  nges->a[i].zName
23636 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
23637 69 66 28 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b  if( j==pTab->iPK
23638 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ey ){.          
23639 63 68 6e 67 52 6f 77 69 64 20 3d 20 31 3b 0a 20  chngRowid = 1;. 
2363a 20 20 20 20 20 20 20 20 20 70 52 6f 77 69 64 45           pRowidE
2363b 78 70 72 20 3d 20 70 43 68 61 6e 67 65 73 2d 3e  xpr = pChanges->
2363c 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[i].pExpr;.    
2363d 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 58      }.        aX
2363e 52 65 66 5b 6a 5d 20 3d 20 69 3b 0a 20 20 20 20  Ref[j] = i;.    
2363f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
23640 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
23641 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29   j>=pTab->nCol )
23642 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  {.      if( sqli
23643 74 65 33 49 73 52 6f 77 69 64 28 70 43 68 61 6e  te3IsRowid(pChan
23644 67 65 73 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29  ges->a[i].zName)
23645 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 6e 67   ){.        chng
23646 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20 20  Rowid = 1;.     
23647 20 20 20 70 52 6f 77 69 64 45 78 70 72 20 3d 20     pRowidExpr = 
23648 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 70  pChanges->a[i].p
23649 45 78 70 72 3b 0a 20 20 20 20 20 20 7d 65 6c 73  Expr;.      }els
2364a 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e{.        sqlit
2364b 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2364c 65 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 75  e, "no such colu
2364d 6d 6e 3a 20 25 73 22 2c 20 70 43 68 61 6e 67 65  mn: %s", pChange
2364e 73 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a  s->a[i].zName);.
2364f 20 20 20 20 20 20 20 20 67 6f 74 6f 20 75 70 64          goto upd
23650 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  ate_cleanup;.   
23651 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 6e 64     }.    }.#ifnd
23652 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
23653 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20  UTHORIZATION.   
23654 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b   {.      int rc;
23655 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
23656 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
23657 72 73 65 2c 20 53 51 4c 49 54 45 5f 55 50 44 41  rse, SQLITE_UPDA
23658 54 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  TE, pTab->zName,
23659 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2365a 20 20 20 20 20 20 20 20 20 20 20 20 70 54 61 62              pTab
2365b 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c  ->aCol[j].zName,
2365c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
2365d 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ame);.      if( 
2365e 72 63 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20  rc==SQLITE_DENY 
2365f 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  ){.        goto 
23660 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
23661 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
23662 72 63 3d 3d 53 51 4c 49 54 45 5f 49 47 4e 4f 52  rc==SQLITE_IGNOR
23663 45 20 29 7b 0a 20 20 20 20 20 20 20 20 61 58 52  E ){.        aXR
23664 65 66 5b 6a 5d 20 3d 20 2d 31 3b 0a 20 20 20 20  ef[j] = -1;.    
23665 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66    }.    }.#endif
23666 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  .  }..  /* Alloc
23667 61 74 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74  ate memory for t
23668 68 65 20 61 72 72 61 79 20 61 52 65 67 49 64 78  he array aRegIdx
23669 5b 5d 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e  [].  There is on
2366a 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65 0a 20  e entry in the. 
2366b 20 2a 2a 20 61 72 72 61 79 20 66 6f 72 20 65 61   ** array for ea
2366c 63 68 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61  ch index associa
2366d 74 65 64 20 77 69 74 68 20 74 61 62 6c 65 20 62  ted with table b
2366e 65 69 6e 67 20 75 70 64 61 74 65 64 2e 20 20 46  eing updated.  F
2366f 69 6c 6c 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20  ill in.  ** the 
23670 76 61 6c 75 65 20 77 69 74 68 20 61 20 72 65 67  value with a reg
23671 69 73 74 65 72 20 6e 75 6d 62 65 72 20 66 6f 72  ister number for
23672 20 69 6e 64 69 63 65 73 20 74 68 61 74 20 61 72   indices that ar
23673 65 20 74 6f 20 62 65 20 75 73 65 64 0a 20 20 2a  e to be used.  *
23674 2a 20 61 6e 64 20 77 69 74 68 20 7a 65 72 6f 20  * and with zero 
23675 66 6f 72 20 75 6e 75 73 65 64 20 69 6e 64 69 63  for unused indic
23676 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 6e  es..  */.  for(n
23677 49 64 78 3d 30 2c 20 70 49 64 78 3d 70 54 61 62  Idx=0, pIdx=pTab
23678 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
23679 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
2367a 2c 20 6e 49 64 78 2b 2b 29 7b 7d 0a 20 20 69 66  , nIdx++){}.  if
2367b 28 20 6e 49 64 78 3e 30 20 29 7b 0a 20 20 20 20  ( nIdx>0 ){.    
2367c 61 52 65 67 49 64 78 20 3d 20 73 71 6c 69 74 65  aRegIdx = sqlite
2367d 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
2367e 20 73 69 7a 65 6f 66 28 49 6e 64 65 78 2a 29 20   sizeof(Index*) 
2367f 2a 20 6e 49 64 78 20 29 3b 0a 20 20 20 20 69 66  * nIdx );.    if
23680 28 20 61 52 65 67 49 64 78 3d 3d 30 20 29 20 67  ( aRegIdx==0 ) g
23681 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e  oto update_clean
23682 75 70 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 6a 3d  up;.  }.  for(j=
23683 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  0, pIdx=pTab->pI
23684 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
23685 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 6a 2b  =pIdx->pNext, j+
23686 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 67 3b  +){.    int reg;
23687 0a 20 20 20 20 69 66 28 20 63 68 6e 67 52 6f 77  .    if( chngRow
23688 69 64 20 29 7b 0a 20 20 20 20 20 20 72 65 67 20  id ){.      reg 
23689 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
2368a 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2368b 20 20 20 72 65 67 20 3d 20 30 3b 0a 20 20 20 20     reg = 0;.    
2368c 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64    for(i=0; i<pId
2368d 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29  x->nColumn; i++)
2368e 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 58  {.        if( aX
2368f 52 65 66 5b 70 49 64 78 2d 3e 61 69 43 6f 6c 75  Ref[pIdx->aiColu
23690 6d 6e 5b 69 5d 5d 3e 3d 30 20 29 7b 0a 20 20 20  mn[i]]>=0 ){.   
23691 20 20 20 20 20 20 20 72 65 67 20 3d 20 2b 2b 70         reg = ++p
23692 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
23693 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
23694 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
23695 20 20 20 20 7d 0a 20 20 20 20 61 52 65 67 49 64      }.    aRegId
23696 78 5b 6a 5d 20 3d 20 72 65 67 3b 0a 20 20 7d 0a  x[j] = reg;.  }.
23697 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61  .  /* Allocate a
23698 20 62 6c 6f 63 6b 20 6f 66 20 72 65 67 69 73 74   block of regist
23699 65 72 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  er used to store
2369a 20 74 68 65 20 63 68 61 6e 67 65 20 72 65 63 6f   the change reco
2369b 72 64 0a 20 20 2a 2a 20 73 65 6e 74 20 74 6f 20  rd.  ** sent to 
2369c 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43  sqlite3GenerateC
2369d 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73 28  onstraintChecks(
2369e 29 2e 20 20 54 68 65 72 65 20 61 72 65 20 65 69  ).  There are ei
2369f 74 68 65 72 0a 20 20 2a 2a 20 6f 6e 65 20 6f 72  ther.  ** one or
236a0 20 74 77 6f 20 72 65 67 69 73 74 65 72 73 20 66   two registers f
236a1 6f 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 72  or holding the r
236a2 6f 77 69 64 2e 20 20 4f 6e 65 20 72 6f 77 69 64  owid.  One rowid
236a3 20 72 65 67 69 73 74 65 72 0a 20 20 2a 2a 20 69   register.  ** i
236a4 73 20 75 73 65 64 20 69 66 20 63 68 6e 67 52 6f  s used if chngRo
236a5 77 69 64 20 69 73 20 66 61 6c 73 65 20 61 6e 64  wid is false and
236a6 20 74 77 6f 20 61 72 65 20 75 73 65 64 20 69 66   two are used if
236a7 20 63 68 6e 67 52 6f 77 69 64 20 69 73 0a 20 20   chngRowid is.  
236a8 2a 2a 20 74 72 75 65 2e 20 20 46 6f 6c 6c 6f 77  ** true.  Follow
236a9 69 6e 67 20 74 68 65 73 65 20 61 72 65 20 70 54  ing these are pT
236aa 61 62 2d 3e 6e 43 6f 6c 20 72 65 67 69 73 74 65  ab->nCol registe
236ab 72 20 68 6f 6c 64 69 6e 67 20 63 6f 6c 75 6d 6e  r holding column
236ac 0a 20 20 2a 2a 20 64 61 74 61 2e 0a 20 20 2a 2f  .  ** data..  */
236ad 0a 20 20 72 65 67 4f 6c 64 52 6f 77 69 64 20 3d  .  regOldRowid =
236ae 20 72 65 67 4e 65 77 52 6f 77 69 64 20 3d 20 70   regNewRowid = p
236af 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b  Parse->nMem + 1;
236b0 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20  .  pParse->nMem 
236b1 2b 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 2b 20  += pTab->nCol + 
236b2 31 3b 0a 20 20 69 66 28 20 63 68 6e 67 52 6f 77  1;.  if( chngRow
236b3 69 64 20 29 7b 0a 20 20 20 20 72 65 67 4e 65 77  id ){.    regNew
236b4 52 6f 77 69 64 2b 2b 3b 0a 20 20 20 20 70 50 61  Rowid++;.    pPa
236b5 72 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20 7d  rse->nMem++;.  }
236b6 0a 20 20 72 65 67 44 61 74 61 20 3d 20 72 65 67  .  regData = reg
236b7 4e 65 77 52 6f 77 69 64 2b 31 3b 0a 20 0a 0a 20  NewRowid+1;. .. 
236b8 20 2f 2a 20 42 65 67 69 6e 20 67 65 6e 65 72 61   /* Begin genera
236b9 74 69 6e 67 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a  ting code..  */.
236ba 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
236bb 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
236bc 69 66 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20  if( v==0 ) goto 
236bd 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
236be 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65    if( pParse->ne
236bf 73 74 65 64 3d 3d 30 20 29 20 73 71 6c 69 74 65  sted==0 ) sqlite
236c0 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65  3VdbeCountChange
236c1 73 28 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  s(v);.  sqlite3B
236c2 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
236c3 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44  on(pParse, 1, iD
236c4 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  b);..#ifndef SQL
236c5 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
236c6 54 41 42 4c 45 0a 20 20 2f 2a 20 56 69 72 74 75  TABLE.  /* Virtu
236c7 61 6c 20 74 61 62 6c 65 73 20 6d 75 73 74 20 62  al tables must b
236c8 65 20 68 61 6e 64 6c 65 64 20 73 65 70 61 72 61  e handled separa
236c9 74 65 6c 79 20 2a 2f 0a 20 20 69 66 28 20 49 73  tely */.  if( Is
236ca 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b  Virtual(pTab) ){
236cb 0a 20 20 20 20 75 70 64 61 74 65 56 69 72 74 75  .    updateVirtu
236cc 61 6c 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  alTable(pParse, 
236cd 70 54 61 62 4c 69 73 74 2c 20 70 54 61 62 2c 20  pTabList, pTab, 
236ce 70 43 68 61 6e 67 65 73 2c 20 70 52 6f 77 69 64  pChanges, pRowid
236cf 45 78 70 72 2c 20 61 58 52 65 66 2c 0a 20 20 20  Expr, aXRef,.   
236d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
236d1 20 20 20 20 70 57 68 65 72 65 29 3b 0a 20 20 20      pWhere);.   
236d2 20 70 57 68 65 72 65 20 3d 20 30 3b 0a 20 20 20   pWhere = 0;.   
236d3 20 70 54 61 62 4c 69 73 74 20 3d 20 30 3b 0a 20   pTabList = 0;. 
236d4 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63     goto update_c
236d5 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 23 65 6e 64  leanup;.  }.#end
236d6 69 66 0a 0a 20 20 2f 2a 20 53 74 61 72 74 20 74  if..  /* Start t
236d7 68 65 20 76 69 65 77 20 63 6f 6e 74 65 78 74 0a  he view context.
236d8 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 56 69 65    */.  if( isVie
236d9 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  w ){.    sqlite3
236da 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28  AuthContextPush(
236db 70 50 61 72 73 65 2c 20 26 73 43 6f 6e 74 65 78  pParse, &sContex
236dc 74 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  t, pTab->zName);
236dd 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  .  }..  /* Gener
236de 61 74 65 20 74 68 65 20 63 6f 64 65 20 66 6f 72  ate the code for
236df 20 74 72 69 67 67 65 72 73 2e 0a 20 20 2a 2f 0a   triggers..  */.
236e0 20 20 69 66 28 20 74 72 69 67 67 65 72 73 5f 65    if( triggers_e
236e1 78 69 73 74 20 29 7b 0a 20 20 20 20 69 6e 74 20  xist ){.    int 
236e2 69 47 6f 74 6f 3b 0a 0a 20 20 20 20 2f 2a 20 43  iGoto;..    /* C
236e3 72 65 61 74 65 20 70 73 65 75 64 6f 2d 74 61 62  reate pseudo-tab
236e4 6c 65 73 20 66 6f 72 20 4e 45 57 20 61 6e 64 20  les for NEW and 
236e5 4f 4c 44 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  OLD.    */.    s
236e6 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
236e7 28 76 2c 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c  (v, OP_SetNumCol
236e8 75 6d 6e 73 2c 20 30 2c 20 70 54 61 62 2d 3e 6e  umns, 0, pTab->n
236e9 43 6f 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Col);.    sqlite
236ea 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
236eb 50 5f 4f 70 65 6e 50 73 65 75 64 6f 2c 20 6f 6c  P_OpenPseudo, ol
236ec 64 49 64 78 2c 20 30 29 3b 0a 20 20 20 20 73 71  dIdx, 0);.    sq
236ed 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
236ee 76 2c 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75  v, OP_SetNumColu
236ef 6d 6e 73 2c 20 30 2c 20 70 54 61 62 2d 3e 6e 43  mns, 0, pTab->nC
236f0 6f 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ol);.    sqlite3
236f1 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
236f2 5f 4f 70 65 6e 50 73 65 75 64 6f 2c 20 6e 65 77  _OpenPseudo, new
236f3 49 64 78 2c 20 30 29 3b 0a 0a 20 20 20 20 69 47  Idx, 0);..    iG
236f4 6f 74 6f 20 3d 20 73 71 6c 69 74 65 33 56 64 62  oto = sqlite3Vdb
236f5 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
236f6 74 6f 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 61  to, 0, 0);.    a
236f7 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
236f8 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
236f9 20 20 20 69 42 65 67 69 6e 42 65 66 6f 72 65 54     iBeginBeforeT
236fa 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33  rigger = sqlite3
236fb 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
236fc 76 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  v);.    if( sqli
236fd 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65  te3CodeRowTrigge
236fe 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 55 50 44  r(pParse, TK_UPD
236ff 41 54 45 2c 20 70 43 68 61 6e 67 65 73 2c 20 54  ATE, pChanges, T
23700 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c 20 70  RIGGER_BEFORE, p
23701 54 61 62 2c 0a 20 20 20 20 20 20 20 20 20 20 6e  Tab,.          n
23702 65 77 49 64 78 2c 20 6f 6c 64 49 64 78 2c 20 6f  ewIdx, oldIdx, o
23703 6e 45 72 72 6f 72 2c 20 61 64 64 72 2c 20 26 6f  nError, addr, &o
23704 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 2c 20 26 6e 65  ld_col_mask, &ne
23705 77 5f 63 6f 6c 5f 6d 61 73 6b 29 20 29 7b 0a 20  w_col_mask) ){. 
23706 20 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65       goto update
23707 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a  _cleanup;.    }.
23708 20 20 20 20 69 45 6e 64 42 65 66 6f 72 65 54 72      iEndBeforeTr
23709 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 56  igger = sqlite3V
2370a 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2370b 47 6f 74 6f 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Goto, 0, 0);.   
2370c 20 69 42 65 67 69 6e 41 66 74 65 72 54 72 69 67   iBeginAfterTrig
2370d 67 65 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ger = sqlite3Vdb
2370e 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b  eCurrentAddr(v);
2370f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
23710 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 70  CodeRowTrigger(p
23711 50 61 72 73 65 2c 20 54 4b 5f 55 50 44 41 54 45  Parse, TK_UPDATE
23712 2c 20 70 43 68 61 6e 67 65 73 2c 20 54 52 49 47  , pChanges, TRIG
23713 47 45 52 5f 41 46 54 45 52 2c 20 70 54 61 62 2c  GER_AFTER, pTab,
23714 20 0a 20 20 20 20 20 20 20 20 20 20 6e 65 77 49   .          newI
23715 64 78 2c 20 6f 6c 64 49 64 78 2c 20 6f 6e 45 72  dx, oldIdx, onEr
23716 72 6f 72 2c 20 61 64 64 72 2c 20 26 6f 6c 64 5f  ror, addr, &old_
23717 63 6f 6c 5f 6d 61 73 6b 2c 20 26 6e 65 77 5f 63  col_mask, &new_c
23718 6f 6c 5f 6d 61 73 6b 29 20 29 7b 0a 20 20 20 20  ol_mask) ){.    
23719 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c    goto update_cl
2371a 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20  eanup;.    }.   
2371b 20 69 45 6e 64 41 66 74 65 72 54 72 69 67 67 65   iEndAfterTrigge
2371c 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
2371d 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
2371e 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
2371f 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
23720 28 76 2c 20 69 47 6f 74 6f 29 3b 0a 20 20 7d 0a  (v, iGoto);.  }.
23721 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20  .  /* If we are 
23722 74 72 79 69 6e 67 20 74 6f 20 75 70 64 61 74 65  trying to update
23723 20 61 20 76 69 65 77 2c 20 72 65 61 6c 69 7a 65   a view, realize
23724 20 74 68 61 74 20 76 69 65 77 20 69 6e 74 6f 0a   that view into.
23725 20 20 2a 2a 20 61 20 65 70 68 65 6d 65 72 61 6c    ** a ephemeral
23726 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69   table..  */.  i
23727 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20  f( isView ){.   
23728 20 73 71 6c 69 74 65 33 4d 61 74 65 72 69 61 6c   sqlite3Material
23729 69 7a 65 56 69 65 77 28 70 50 61 72 73 65 2c 20  izeView(pParse, 
2372a 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 2c 20 70  pTab->pSelect, p
2372b 57 68 65 72 65 2c 20 69 43 75 72 29 3b 0a 20 20  Where, iCur);.  
2372c 7d 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20  }..  /* Resolve 
2372d 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  the column names
2372e 20 69 6e 20 61 6c 6c 20 74 68 65 20 65 78 70 72   in all the expr
2372f 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 0a 20  essions in the. 
23730 20 2a 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65   ** WHERE clause
23731 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
23732 69 74 65 33 45 78 70 72 52 65 73 6f 6c 76 65 4e  ite3ExprResolveN
23733 61 6d 65 73 28 26 73 4e 43 2c 20 70 57 68 65 72  ames(&sNC, pWher
23734 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 75  e) ){.    goto u
23735 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  pdate_cleanup;. 
23736 20 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 74   }..  /* Begin t
23737 68 65 20 64 61 74 61 62 61 73 65 20 73 63 61 6e  he database scan
23738 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  .  */.  sqlite3V
23739 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2373a 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 4f 6c 64 52  Null, 0, regOldR
2373b 6f 77 69 64 29 3b 0a 20 20 70 57 49 6e 66 6f 20  owid);.  pWInfo 
2373c 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  = sqlite3WhereBe
2373d 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62  gin(pParse, pTab
2373e 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 20 30 2c  List, pWhere, 0,
2373f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
23740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 57 48                WH
23741 45 52 45 5f 4f 4e 45 50 41 53 53 5f 44 45 53 49  ERE_ONEPASS_DESI
23742 52 45 44 29 3b 0a 20 20 69 66 28 20 70 57 49 6e  RED);.  if( pWIn
23743 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 75 70 64  fo==0 ) goto upd
23744 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 6f  ate_cleanup;.  o
23745 6b 4f 6e 65 50 61 73 73 20 3d 20 70 57 49 6e 66  kOnePass = pWInf
23746 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73 3b 0a 0a 20  o->okOnePass;.. 
23747 20 2f 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 65   /* Remember the
23748 20 72 6f 77 69 64 20 6f 66 20 65 76 65 72 79 20   rowid of every 
23749 69 74 65 6d 20 74 6f 20 62 65 20 75 70 64 61 74  item to be updat
2374a 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ed..  */.  sqlit
2374b 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2374c 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 3f  IsVirtual(pTab)?
2374d 4f 50 5f 56 52 6f 77 69 64 3a 4f 50 5f 52 6f 77  OP_VRowid:OP_Row
2374e 69 64 2c 20 69 43 75 72 2c 20 72 65 67 4f 6c 64  id, iCur, regOld
2374f 52 6f 77 69 64 29 3b 0a 20 20 69 66 28 20 21 6f  Rowid);.  if( !o
23750 6b 4f 6e 65 50 61 73 73 20 29 20 73 71 6c 69 74  kOnePass ) sqlit
23751 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
23752 4f 50 5f 46 69 66 6f 57 72 69 74 65 2c 20 72 65  OP_FifoWrite, re
23753 67 4f 6c 64 52 6f 77 69 64 2c 20 30 29 3b 0a 0a  gOldRowid, 0);..
23754 20 20 2f 2a 20 45 6e 64 20 74 68 65 20 64 61 74    /* End the dat
23755 61 62 61 73 65 20 73 63 61 6e 20 6c 6f 6f 70 2e  abase scan loop.
23756 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 57  .  */.  sqlite3W
23757 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b  hereEnd(pWInfo);
23758 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a  ..  /* Initializ
23759 65 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 75  e the count of u
2375a 70 64 61 74 65 64 20 72 6f 77 73 0a 20 20 2a 2f  pdated rows.  */
2375b 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73  .  if( db->flags
2375c 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52   & SQLITE_CountR
2375d 6f 77 73 20 26 26 20 21 70 50 61 72 73 65 2d 3e  ows && !pParse->
2375e 74 72 69 67 53 74 61 63 6b 20 29 7b 0a 20 20 20  trigStack ){.   
2375f 20 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20 2b   regRowCount = +
23760 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
23761 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
23762 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
23763 65 72 2c 20 30 2c 20 72 65 67 52 6f 77 43 6f 75  er, 0, regRowCou
23764 6e 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  nt);.  }..  if( 
23765 21 69 73 56 69 65 77 20 26 26 20 21 49 73 56 69  !isView && !IsVi
23766 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20  rtual(pTab) ){. 
23767 20 20 20 2f 2a 20 0a 20 20 20 20 2a 2a 20 4f 70     /* .    ** Op
23768 65 6e 20 65 76 65 72 79 20 69 6e 64 65 78 20 74  en every index t
23769 68 61 74 20 6e 65 65 64 73 20 75 70 64 61 74 69  hat needs updati
2376a 6e 67 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 69  ng.  Note that i
2376b 66 20 61 6e 79 0a 20 20 20 20 2a 2a 20 69 6e 64  f any.    ** ind
2376c 65 78 20 63 6f 75 6c 64 20 70 6f 74 65 6e 74 69  ex could potenti
2376d 61 6c 6c 79 20 69 6e 76 6f 6b 65 20 61 20 52 45  ally invoke a RE
2376e 50 4c 41 43 45 20 63 6f 6e 66 6c 69 63 74 20 72  PLACE conflict r
2376f 65 73 6f 6c 75 74 69 6f 6e 20 0a 20 20 20 20 2a  esolution .    *
23770 2a 20 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 77  * action, then w
23771 65 20 6e 65 65 64 20 74 6f 20 6f 70 65 6e 20 61  e need to open a
23772 6c 6c 20 69 6e 64 69 63 65 73 20 62 65 63 61 75  ll indices becau
23773 73 65 20 77 65 20 6d 69 67 68 74 20 6e 65 65 64  se we might need
23774 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 64 65  .    ** to be de
23775 6c 65 74 69 6e 67 20 73 6f 6d 65 20 72 65 63 6f  leting some reco
23776 72 64 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  rds..    */.    
23777 69 66 28 20 21 6f 6b 4f 6e 65 50 61 73 73 20 29  if( !okOnePass )
23778 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c   sqlite3OpenTabl
23779 65 28 70 50 61 72 73 65 2c 20 69 43 75 72 2c 20  e(pParse, iCur, 
2377a 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f 70  iDb, pTab, OP_Op
2377b 65 6e 57 72 69 74 65 29 3b 20 0a 20 20 20 20 69  enWrite); .    i
2377c 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52  f( onError==OE_R
2377d 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 20 20  eplace ){.      
2377e 6f 70 65 6e 41 6c 6c 20 3d 20 31 3b 0a 20 20 20  openAll = 1;.   
2377f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6f 70   }else{.      op
23780 65 6e 41 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 20  enAll = 0;.     
23781 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e   for(pIdx=pTab->
23782 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
23783 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b  dx=pIdx->pNext){
23784 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64  .        if( pId
23785 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52  x->onError==OE_R
23786 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 20 20  eplace ){.      
23787 20 20 20 20 6f 70 65 6e 41 6c 6c 20 3d 20 31 3b      openAll = 1;
23788 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
23789 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2378a 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f    }.    }.    fo
2378b 72 28 69 3d 30 2c 20 70 49 64 78 3d 70 54 61 62  r(i=0, pIdx=pTab
2378c 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
2378d 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
2378e 2c 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  , i++){.      if
2378f 28 20 6f 70 65 6e 41 6c 6c 20 7c 7c 20 61 52 65  ( openAll || aRe
23790 67 49 64 78 5b 69 5d 3e 30 20 29 7b 0a 20 20 20  gIdx[i]>0 ){.   
23791 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b       KeyInfo *pK
23792 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65  ey = sqlite3Inde
23793 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c  xKeyinfo(pParse,
23794 20 70 49 64 78 29 3b 0a 20 20 20 20 20 20 20 20   pIdx);.        
23795 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23796 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74  4(v, OP_OpenWrit
23797 65 2c 20 69 43 75 72 2b 69 2b 31 2c 20 70 49 64  e, iCur+i+1, pId
23798 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a 20 20  x->tnum, iDb,.  
23799 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2379a 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79       (char*)pKey
2379b 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e  , P4_KEYINFO_HAN
2379c 44 4f 46 46 29 3b 0a 20 20 20 20 20 20 20 20 61  DOFF);.        a
2379d 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e  ssert( pParse->n
2379e 54 61 62 3e 69 43 75 72 2b 69 2b 31 20 29 3b 0a  Tab>iCur+i+1 );.
2379f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
237a0 7d 0a 20 20 0a 20 20 2f 2a 20 4a 75 6d 70 20 62  }.  .  /* Jump b
237a1 61 63 6b 20 74 6f 20 74 68 69 73 20 70 6f 69 6e  ack to this poin
237a2 74 20 69 66 20 61 20 74 72 69 67 67 65 72 20 65  t if a trigger e
237a3 6e 63 6f 75 6e 74 65 72 73 20 61 6e 20 49 47 4e  ncounters an IGN
237a4 4f 52 45 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20  ORE constraint. 
237a5 2a 2f 0a 20 20 69 66 28 20 74 72 69 67 67 65 72  */.  if( trigger
237a6 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20 73  s_exist ){.    s
237a7 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
237a8 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 29 3b  eLabel(v, addr);
237a9 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f 70 20 6f  .  }..  /* Top o
237aa 66 20 74 68 65 20 75 70 64 61 74 65 20 6c 6f 6f  f the update loo
237ab 70 20 2a 2f 0a 20 20 69 66 28 20 6f 6b 4f 6e 65  p */.  if( okOne
237ac 50 61 73 73 20 29 7b 0a 20 20 20 20 69 6e 74 20  Pass ){.    int 
237ad 61 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  a1 = sqlite3Vdbe
237ae 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74  AddOp1(v, OP_Not
237af 4e 75 6c 6c 2c 20 72 65 67 4f 6c 64 52 6f 77 69  Null, regOldRowi
237b0 64 29 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 73  d);.    addr = s
237b1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30  qlite3VdbeAddOp0
237b2 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20 20  (v, OP_Goto);.  
237b3 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
237b4 70 48 65 72 65 28 76 2c 20 61 31 29 3b 0a 20 20  pHere(v, a1);.  
237b5 7d 65 6c 73 65 7b 0a 20 20 20 20 61 64 64 72 20  }else{.    addr 
237b6 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
237b7 4f 70 32 28 76 2c 20 4f 50 5f 46 69 66 6f 52 65  Op2(v, OP_FifoRe
237b8 61 64 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 2c  ad, regOldRowid,
237b9 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20   0);.  }..  if( 
237ba 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 29  triggers_exist )
237bb 7b 0a 20 20 20 20 69 6e 74 20 72 65 67 52 6f 77  {.    int regRow
237bc 69 64 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 52  id;.    int regR
237bd 6f 77 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 43  ow;.    int regC
237be 6f 6c 73 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b  ols;..    /* Mak
237bf 65 20 63 75 72 73 6f 72 20 69 43 75 72 20 70 6f  e cursor iCur po
237c0 69 6e 74 20 74 6f 20 74 68 65 20 72 65 63 6f 72  int to the recor
237c1 64 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 20  d that is being 
237c2 75 70 64 61 74 65 64 2e 0a 20 20 20 20 2a 2f 0a  updated..    */.
237c3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
237c4 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45  ddOp3(v, OP_NotE
237c5 78 69 73 74 73 2c 20 69 43 75 72 2c 20 61 64 64  xists, iCur, add
237c6 72 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b  r, regOldRowid);
237c7 0a 0a 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74  ..    /* Generat
237c8 65 20 74 68 65 20 4f 4c 44 20 74 61 62 6c 65 0a  e the OLD table.
237c9 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 67 52 6f      */.    regRo
237ca 77 69 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74  wid = sqlite3Get
237cb 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
237cc 0a 20 20 20 20 72 65 67 52 6f 77 20 3d 20 73 71  .    regRow = sq
237cd 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
237ce 70 50 61 72 73 65 29 3b 0a 20 20 20 20 73 71 6c  pParse);.    sql
237cf 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
237d0 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43 75 72  , OP_Rowid, iCur
237d1 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
237d2 20 69 66 28 20 21 6f 6c 64 5f 63 6f 6c 5f 6d 61   if( !old_col_ma
237d3 73 6b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  sk ){.      sqli
237d4 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
237d5 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67   OP_Null, 0, reg
237d6 52 6f 77 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  Row);.    }else{
237d7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
237d8 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
237d9 6f 77 44 61 74 61 2c 20 69 43 75 72 2c 20 72 65  owData, iCur, re
237da 67 52 6f 77 29 3b 0a 20 20 20 20 7d 0a 20 20 20  gRow);.    }.   
237db 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
237dc 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c  p3(v, OP_Insert,
237dd 20 6f 6c 64 49 64 78 2c 20 72 65 67 52 6f 77 2c   oldIdx, regRow,
237de 20 72 65 67 52 6f 77 69 64 29 3b 0a 0a 20 20 20   regRowid);..   
237df 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65   /* Generate the
237e0 20 4e 45 57 20 74 61 62 6c 65 0a 20 20 20 20 2a   NEW table.    *
237e1 2f 0a 20 20 20 20 69 66 28 20 63 68 6e 67 52 6f  /.    if( chngRo
237e2 77 69 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  wid ){.      sql
237e3 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e 64 43  ite3ExprCodeAndC
237e4 61 63 68 65 28 70 50 61 72 73 65 2c 20 70 52 6f  ache(pParse, pRo
237e5 77 69 64 45 78 70 72 2c 20 72 65 67 52 6f 77 69  widExpr, regRowi
237e6 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  d);.    }else{. 
237e7 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
237e8 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77  AddOp2(v, OP_Row
237e9 69 64 2c 20 69 43 75 72 2c 20 72 65 67 52 6f 77  id, iCur, regRow
237ea 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  id);.    }.    r
237eb 65 67 43 6f 6c 73 20 3d 20 73 71 6c 69 74 65 33  egCols = sqlite3
237ec 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61  GetTempRange(pPa
237ed 72 73 65 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29  rse, pTab->nCol)
237ee 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
237ef 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  <pTab->nCol; i++
237f0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d  ){.      if( i==
237f1 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20  pTab->iPKey ){. 
237f2 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
237f3 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
237f4 75 6c 6c 2c 20 30 2c 20 72 65 67 43 6f 6c 73 2b  ull, 0, regCols+
237f5 69 29 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74  i);.        cont
237f6 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  inue;.      }.  
237f7 20 20 20 20 6a 20 3d 20 61 58 52 65 66 5b 69 5d      j = aXRef[i]
237f8 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 77 5f  ;.      if( new_
237f9 63 6f 6c 5f 6d 61 73 6b 26 28 28 75 33 32 29 31  col_mask&((u32)1
237fa 3c 3c 69 29 20 7c 7c 20 6e 65 77 5f 63 6f 6c 5f  <<i) || new_col_
237fb 6d 61 73 6b 3d 3d 30 78 66 66 66 66 66 66 66 66  mask==0xffffffff
237fc 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
237fd 6a 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  j<0 ){.         
237fe 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
237ff 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
23800 20 69 43 75 72 2c 20 69 2c 20 72 65 67 43 6f 6c   iCur, i, regCol
23801 73 2b 69 29 3b 0a 20 20 20 20 20 20 20 20 20 20  s+i);.          
23802 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66  sqlite3ColumnDef
23803 61 75 6c 74 28 76 2c 20 70 54 61 62 2c 20 69 29  ault(v, pTab, i)
23804 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
23805 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
23806 65 33 45 78 70 72 43 6f 64 65 41 6e 64 43 61 63  e3ExprCodeAndCac
23807 68 65 28 70 50 61 72 73 65 2c 20 70 43 68 61 6e  he(pParse, pChan
23808 67 65 73 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 2c  ges->a[j].pExpr,
23809 20 72 65 67 43 6f 6c 73 2b 69 29 3b 0a 20 20 20   regCols+i);.   
2380a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
2380b 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
2380c 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2380d 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67   OP_Null, 0, reg
2380e 43 6f 6c 73 2b 69 29 3b 0a 20 20 20 20 20 20 7d  Cols+i);.      }
2380f 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
23810 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
23811 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72  OP_MakeRecord, r
23812 65 67 43 6f 6c 73 2c 20 70 54 61 62 2d 3e 6e 43  egCols, pTab->nC
23813 6f 6c 2c 20 72 65 67 52 6f 77 29 3b 0a 20 20 20  ol, regRow);.   
23814 20 69 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a   if( !isView ){.
23815 20 20 20 20 20 20 73 71 6c 69 74 65 33 54 61 62        sqlite3Tab
23816 6c 65 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c  leAffinityStr(v,
23817 20 70 54 61 62 29 3b 0a 20 20 20 20 20 20 73 71   pTab);.      sq
23818 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66  lite3ExprCacheAf
23819 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61  finityChange(pPa
2381a 72 73 65 2c 20 72 65 67 43 6f 6c 73 2c 20 70 54  rse, regCols, pT
2381b 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 7d  ab->nCol);.    }
2381c 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65  .    sqlite3Rele
2381d 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61  aseTempRange(pPa
2381e 72 73 65 2c 20 72 65 67 43 6f 6c 73 2c 20 70 54  rse, regCols, pT
2381f 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 69  ab->nCol);.    i
23820 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20  f( pParse->nErr 
23821 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c  ) goto update_cl
23822 65 61 6e 75 70 3b 0a 20 20 20 20 73 71 6c 69 74  eanup;.    sqlit
23823 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
23824 4f 50 5f 49 6e 73 65 72 74 2c 20 6e 65 77 49 64  OP_Insert, newId
23825 78 2c 20 72 65 67 52 6f 77 2c 20 72 65 67 52 6f  x, regRow, regRo
23826 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  wid);.    sqlite
23827 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
23828 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77 69 64  pParse, regRowid
23829 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65  );.    sqlite3Re
2382a 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
2382b 72 73 65 2c 20 72 65 67 52 6f 77 29 3b 0a 0a 20  rse, regRow);.. 
2382c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2382d 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
2382e 20 30 2c 20 69 42 65 67 69 6e 42 65 66 6f 72 65   0, iBeginBefore
2382f 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 73 71  Trigger);.    sq
23830 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
23831 65 28 76 2c 20 69 45 6e 64 42 65 66 6f 72 65 54  e(v, iEndBeforeT
23832 72 69 67 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20  rigger);.  }..  
23833 69 66 28 20 21 69 73 56 69 65 77 20 26 26 20 21  if( !isView && !
23834 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
23835 29 7b 0a 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 6f  ){.    /* Loop o
23836 76 65 72 20 65 76 65 72 79 20 72 65 63 6f 72 64  ver every record
23837 20 74 68 61 74 20 6e 65 65 64 73 20 75 70 64 61   that needs upda
23838 74 69 6e 67 2e 20 20 57 65 20 68 61 76 65 20 74  ting.  We have t
23839 6f 20 6c 6f 61 64 0a 20 20 20 20 2a 2a 20 74 68  o load.    ** th
2383a 65 20 6f 6c 64 20 64 61 74 61 20 66 6f 72 20 65  e old data for e
2383b 61 63 68 20 72 65 63 6f 72 64 20 74 6f 20 62 65  ach record to be
2383c 20 75 70 64 61 74 65 64 20 62 65 63 61 75 73 65   updated because
2383d 20 73 6f 6d 65 20 63 6f 6c 75 6d 6e 73 0a 20 20   some columns.  
2383e 20 20 2a 2a 20 6d 69 67 68 74 20 6e 6f 74 20 63    ** might not c
2383f 68 61 6e 67 65 20 61 6e 64 20 77 65 20 77 69 6c  hange and we wil
23840 6c 20 6e 65 65 64 20 74 6f 20 63 6f 70 79 20 74  l need to copy t
23841 68 65 20 6f 6c 64 20 76 61 6c 75 65 2e 0a 20 20  he old value..  
23842 20 20 2a 2a 20 41 6c 73 6f 2c 20 74 68 65 20 6f    ** Also, the o
23843 6c 64 20 64 61 74 61 20 69 73 20 6e 65 65 64 65  ld data is neede
23844 64 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20  d to delete the 
23845 6f 6c 64 20 69 6e 64 65 78 20 65 6e 74 72 69 65  old index entrie
23846 73 2e 0a 20 20 20 20 2a 2a 20 53 6f 20 6d 61 6b  s..    ** So mak
23847 65 20 74 68 65 20 63 75 72 73 6f 72 20 70 6f 69  e the cursor poi
23848 6e 74 20 61 74 20 74 68 65 20 6f 6c 64 20 72 65  nt at the old re
23849 63 6f 72 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  cord..    */.   
2384a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2384b 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73  p3(v, OP_NotExis
2384c 74 73 2c 20 69 43 75 72 2c 20 61 64 64 72 2c 20  ts, iCur, addr, 
2384d 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 0a 20  regOldRowid);.. 
2384e 20 20 20 2f 2a 20 49 66 20 74 68 65 20 72 65 63     /* If the rec
2384f 6f 72 64 20 6e 75 6d 62 65 72 20 77 69 6c 6c 20  ord number will 
23850 63 68 61 6e 67 65 2c 20 70 75 73 68 20 74 68 65  change, push the
23851 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 61   record number a
23852 73 20 69 74 0a 20 20 20 20 2a 2a 20 77 69 6c 6c  s it.    ** will
23853 20 62 65 20 61 66 74 65 72 20 74 68 65 20 75 70   be after the up
23854 64 61 74 65 2e 20 28 54 68 65 20 6f 6c 64 20 72  date. (The old r
23855 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20  ecord number is 
23856 63 75 72 72 65 6e 74 6c 79 0a 20 20 20 20 2a 2a  currently.    **
23857 20 6f 6e 20 74 6f 70 20 6f 66 20 74 68 65 20 73   on top of the s
23858 74 61 63 6b 2e 29 0a 20 20 20 20 2a 2f 0a 20 20  tack.).    */.  
23859 20 20 69 66 28 20 63 68 6e 67 52 6f 77 69 64 20    if( chngRowid 
2385a 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2385b 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
2385c 20 70 52 6f 77 69 64 45 78 70 72 2c 20 72 65 67   pRowidExpr, reg
2385d 4e 65 77 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  NewRowid);.     
2385e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2385f 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49  p1(v, OP_MustBeI
23860 6e 74 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 29  nt, regNewRowid)
23861 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
23862 43 6f 6d 70 75 74 65 20 6e 65 77 20 64 61 74 61  Compute new data
23863 20 66 6f 72 20 74 68 69 73 20 72 65 63 6f 72 64   for this record
23864 2e 20 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66  .  .    */.    f
23865 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e  or(i=0; i<pTab->
23866 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
23867 20 20 69 66 28 20 69 3d 3d 70 54 61 62 2d 3e 69    if( i==pTab->i
23868 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20  PKey ){.        
23869 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2386a 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
2386b 20 72 65 67 44 61 74 61 2b 69 29 3b 0a 20 20 20   regData+i);.   
2386c 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
2386d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6a 20 3d       }.      j =
2386e 20 61 58 52 65 66 5b 69 5d 3b 0a 20 20 20 20 20   aXRef[i];.     
2386f 20 69 66 28 20 6a 3c 30 20 29 7b 0a 20 20 20 20   if( j<0 ){.    
23870 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
23871 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
23872 6d 6e 2c 20 69 43 75 72 2c 20 69 2c 20 72 65 67  mn, iCur, i, reg
23873 44 61 74 61 2b 69 29 3b 0a 20 20 20 20 20 20 20  Data+i);.       
23874 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65   sqlite3ColumnDe
23875 66 61 75 6c 74 28 76 2c 20 70 54 61 62 2c 20 69  fault(v, pTab, i
23876 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
23877 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
23878 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20  xprCode(pParse, 
23879 70 43 68 61 6e 67 65 73 2d 3e 61 5b 6a 5d 2e 70  pChanges->a[j].p
2387a 45 78 70 72 2c 20 72 65 67 44 61 74 61 2b 69 29  Expr, regData+i)
2387b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2387c 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f 6e 73 74  .    /* Do const
2387d 72 61 69 6e 74 20 63 68 65 63 6b 73 0a 20 20 20  raint checks.   
2387e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 47   */.    sqlite3G
2387f 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e  enerateConstrain
23880 74 43 68 65 63 6b 73 28 70 50 61 72 73 65 2c 20  tChecks(pParse, 
23881 70 54 61 62 2c 20 69 43 75 72 2c 20 72 65 67 4e  pTab, iCur, regN
23882 65 77 52 6f 77 69 64 2c 0a 20 20 20 20 20 20 20  ewRowid,.       
23883 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23884 20 20 20 20 20 20 20 20 20 20 20 20 20 61 52 65               aRe
23885 67 49 64 78 2c 20 63 68 6e 67 52 6f 77 69 64 2c  gIdx, chngRowid,
23886 20 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   1,.            
23887 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23888 20 20 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 2c          onError,
23889 20 61 64 64 72 29 3b 0a 0a 20 20 20 20 2f 2a 20   addr);..    /* 
2388a 44 65 6c 65 74 65 20 74 68 65 20 6f 6c 64 20 69  Delete the old i
2388b 6e 64 69 63 65 73 20 66 6f 72 20 74 68 65 20 63  ndices for the c
2388c 75 72 72 65 6e 74 20 72 65 63 6f 72 64 2e 0a 20  urrent record.. 
2388d 20 20 20 2a 2f 0a 20 20 20 20 6a 31 20 3d 20 73     */.    j1 = s
2388e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2388f 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73  (v, OP_NotExists
23890 2c 20 69 43 75 72 2c 20 30 2c 20 72 65 67 4f 6c  , iCur, 0, regOl
23891 64 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c  dRowid);.    sql
23892 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 49  ite3GenerateRowI
23893 6e 64 65 78 44 65 6c 65 74 65 28 70 50 61 72 73  ndexDelete(pPars
23894 65 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20 61  e, pTab, iCur, a
23895 52 65 67 49 64 78 29 3b 0a 0a 20 20 20 20 2f 2a  RegIdx);..    /*
23896 20 49 66 20 63 68 61 6e 67 69 6e 67 20 74 68 65   If changing the
23897 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 2c 20   record number, 
23898 64 65 6c 65 74 65 20 74 68 65 20 6f 6c 64 20 72  delete the old r
23899 65 63 6f 72 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  ecord..    */.  
2389a 20 20 69 66 28 20 63 68 6e 67 52 6f 77 69 64 20    if( chngRowid 
2389b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2389c 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2389d 5f 44 65 6c 65 74 65 2c 20 69 43 75 72 2c 20 30  _Delete, iCur, 0
2389e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
2389f 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
238a0 28 76 2c 20 6a 31 29 3b 0a 0a 20 20 20 20 2f 2a  (v, j1);..    /*
238a1 20 43 72 65 61 74 65 20 74 68 65 20 6e 65 77 20   Create the new 
238a2 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20 61 6e  index entries an
238a3 64 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64  d the new record
238a4 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  ..    */.    sql
238a5 69 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73 65  ite3CompleteInse
238a6 72 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 54  rtion(pParse, pT
238a7 61 62 2c 20 69 43 75 72 2c 20 72 65 67 4e 65 77  ab, iCur, regNew
238a8 52 6f 77 69 64 2c 20 0a 20 20 20 20 20 20 20 20  Rowid, .        
238a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
238aa 20 20 20 20 20 61 52 65 67 49 64 78 2c 20 63 68       aRegIdx, ch
238ab 6e 67 52 6f 77 69 64 2c 20 31 2c 20 2d 31 2c 20  ngRowid, 1, -1, 
238ac 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e  0);.  }..  /* In
238ad 63 72 65 6d 65 6e 74 20 74 68 65 20 72 6f 77 20  crement the row 
238ae 63 6f 75 6e 74 65 72 20 0a 20 20 2a 2f 0a 20 20  counter .  */.  
238af 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20  if( db->flags & 
238b0 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73  SQLITE_CountRows
238b1 20 26 26 20 21 70 50 61 72 73 65 2d 3e 74 72 69   && !pParse->tri
238b2 67 53 74 61 63 6b 29 7b 0a 20 20 20 20 73 71 6c  gStack){.    sql
238b3 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
238b4 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67  , OP_AddImm, reg
238b5 52 6f 77 43 6f 75 6e 74 2c 20 31 29 3b 0a 20 20  RowCount, 1);.  
238b6 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  }..  /* If there
238b7 20 61 72 65 20 74 72 69 67 67 65 72 73 2c 20 63   are triggers, c
238b8 6c 6f 73 65 20 61 6c 6c 20 74 68 65 20 63 75 72  lose all the cur
238b9 73 6f 72 73 20 61 66 74 65 72 20 65 61 63 68 20  sors after each 
238ba 69 74 65 72 61 74 69 6f 6e 0a 20 20 2a 2a 20 74  iteration.  ** t
238bb 68 72 6f 75 67 68 20 74 68 65 20 6c 6f 6f 70 2e  hrough the loop.
238bc 20 20 54 68 65 20 66 69 72 65 20 74 68 65 20 61    The fire the a
238bd 66 74 65 72 20 74 72 69 67 67 65 72 73 2e 0a 20  fter triggers.. 
238be 20 2a 2f 0a 20 20 69 66 28 20 74 72 69 67 67 65   */.  if( trigge
238bf 72 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20  rs_exist ){.    
238c0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
238c1 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
238c2 20 69 42 65 67 69 6e 41 66 74 65 72 54 72 69 67   iBeginAfterTrig
238c3 67 65 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ger);.    sqlite
238c4 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
238c5 20 69 45 6e 64 41 66 74 65 72 54 72 69 67 67 65   iEndAfterTrigge
238c6 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65  r);.  }..  /* Re
238c7 70 65 61 74 20 74 68 65 20 61 62 6f 76 65 20 77  peat the above w
238c8 69 74 68 20 74 68 65 20 6e 65 78 74 20 72 65 63  ith the next rec
238c9 6f 72 64 20 74 6f 20 62 65 20 75 70 64 61 74 65  ord to be update
238ca 64 2c 20 75 6e 74 69 6c 0a 20 20 2a 2a 20 61 6c  d, until.  ** al
238cb 6c 20 72 65 63 6f 72 64 20 73 65 6c 65 63 74 65  l record selecte
238cc 64 20 62 79 20 74 68 65 20 57 48 45 52 45 20 63  d by the WHERE c
238cd 6c 61 75 73 65 20 68 61 76 65 20 62 65 65 6e 20  lause have been 
238ce 75 70 64 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 20  updated..  */.  
238cf 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
238d0 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
238d1 20 61 64 64 72 29 3b 0a 20 20 73 71 6c 69 74 65   addr);.  sqlite
238d2 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
238d3 20 61 64 64 72 29 3b 0a 0a 20 20 2f 2a 20 43 6c   addr);..  /* Cl
238d4 6f 73 65 20 61 6c 6c 20 74 61 62 6c 65 73 20 2a  ose all tables *
238d5 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 64  /.  for(i=0, pId
238d6 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
238d7 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
238d8 3e 70 4e 65 78 74 2c 20 69 2b 2b 29 7b 0a 20 20  >pNext, i++){.  
238d9 20 20 69 66 28 20 6f 70 65 6e 41 6c 6c 20 7c 7c    if( openAll ||
238da 20 61 52 65 67 49 64 78 5b 69 5d 3e 30 20 29 7b   aRegIdx[i]>0 ){
238db 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
238dc 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
238dd 6c 6f 73 65 2c 20 69 43 75 72 2b 69 2b 31 2c 20  lose, iCur+i+1, 
238de 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  0);.    }.  }.  
238df 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
238e0 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69  2(v, OP_Close, i
238e1 43 75 72 2c 20 30 29 3b 0a 20 20 69 66 28 20 74  Cur, 0);.  if( t
238e2 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 29 7b  riggers_exist ){
238e3 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
238e4 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp2(v, OP_Clo
238e5 73 65 2c 20 6e 65 77 49 64 78 2c 20 30 29 3b 0a  se, newIdx, 0);.
238e6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
238e7 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp2(v, OP_Clos
238e8 65 2c 20 6f 6c 64 49 64 78 2c 20 30 29 3b 0a 20  e, oldIdx, 0);. 
238e9 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65   }..  /*.  ** Re
238ea 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
238eb 6f 66 20 72 6f 77 73 20 74 68 61 74 20 77 65 72  of rows that wer
238ec 65 20 63 68 61 6e 67 65 64 2e 20 49 66 20 74 68  e changed. If th
238ed 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 0a 20  is routine is . 
238ee 20 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67 20 63   ** generating c
238ef 6f 64 65 20 62 65 63 61 75 73 65 20 6f 66 20 61  ode because of a
238f0 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
238f1 4e 65 73 74 65 64 50 61 72 73 65 28 29 2c 20 64  NestedParse(), d
238f2 6f 20 6e 6f 74 0a 20 20 2a 2a 20 69 6e 76 6f 6b  o not.  ** invok
238f3 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66  e the callback f
238f4 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20  unction..  */.  
238f5 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20  if( db->flags & 
238f6 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73  SQLITE_CountRows
238f7 20 26 26 20 21 70 50 61 72 73 65 2d 3e 74 72 69   && !pParse->tri
238f8 67 53 74 61 63 6b 20 26 26 20 70 50 61 72 73 65  gStack && pParse
238f9 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 7b 0a 20  ->nested==0 ){. 
238fa 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
238fb 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
238fc 74 52 6f 77 2c 20 72 65 67 52 6f 77 43 6f 75 6e  tRow, regRowCoun
238fd 74 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  t, 1);.    sqlit
238fe 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
238ff 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  (v, 1);.    sqli
23900 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
23901 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
23902 4e 41 4d 45 2c 20 22 72 6f 77 73 20 75 70 64 61  NAME, "rows upda
23903 74 65 64 22 2c 20 50 34 5f 53 54 41 54 49 43 29  ted", P4_STATIC)
23904 3b 0a 20 20 7d 0a 0a 75 70 64 61 74 65 5f 63 6c  ;.  }..update_cl
23905 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33  eanup:.  sqlite3
23906 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 28 26  AuthContextPop(&
23907 73 43 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c  sContext);.  sql
23908 69 74 65 33 5f 66 72 65 65 28 61 52 65 67 49 64  ite3_free(aRegId
23909 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  x);.  sqlite3_fr
2390a 65 65 28 61 58 52 65 66 29 3b 0a 20 20 73 71 6c  ee(aXRef);.  sql
2390b 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
2390c 65 28 70 54 61 62 4c 69 73 74 29 3b 0a 20 20 73  e(pTabList);.  s
2390d 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
2390e 6c 65 74 65 28 70 43 68 61 6e 67 65 73 29 3b 0a  lete(pChanges);.
2390f 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
23910 65 74 65 28 70 57 68 65 72 65 29 3b 0a 20 20 72  ete(pWhere);.  r
23911 65 74 75 72 6e 3b 0a 7d 0a 0a 23 69 66 6e 64 65  eturn;.}..#ifnde
23912 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
23913 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 0a 2a 2a  RTUALTABLE./*.**
23914 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66   Generate code f
23915 6f 72 20 61 6e 20 55 50 44 41 54 45 20 6f 66 20  or an UPDATE of 
23916 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  a virtual table.
23917 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 72 61 74  .**.** The strat
23918 65 67 79 20 69 73 20 74 68 61 74 20 77 65 20 63  egy is that we c
23919 72 65 61 74 65 20 61 6e 20 65 70 68 65 6d 65 72  reate an ephemer
2391a 69 61 6c 20 74 61 62 6c 65 20 74 68 61 74 20 63  ial table that c
2391b 6f 6e 74 61 69 6e 73 0a 2a 2a 20 66 6f 72 20 65  ontains.** for e
2391c 61 63 68 20 72 6f 77 20 74 6f 20 62 65 20 63 68  ach row to be ch
2391d 61 6e 67 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 28  anged:.**.**   (
2391e 41 29 20 20 54 68 65 20 6f 72 69 67 69 6e 61 6c  A)  The original
2391f 20 72 6f 77 69 64 20 6f 66 20 74 68 61 74 20 72   rowid of that r
23920 6f 77 2e 0a 2a 2a 20 20 20 28 42 29 20 20 54 68  ow..**   (B)  Th
23921 65 20 72 65 76 69 73 65 64 20 72 6f 77 69 64 20  e revised rowid 
23922 66 6f 72 20 74 68 65 20 72 6f 77 2e 20 28 6e 6f  for the row. (no
23923 74 65 31 29 0a 2a 2a 20 20 20 28 43 29 20 20 54  te1).**   (C)  T
23924 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 65 76  he content of ev
23925 65 72 79 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68  ery column in th
23926 65 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  e row..**.** The
23927 6e 20 77 65 20 6c 6f 6f 70 20 6f 76 65 72 20 74  n we loop over t
23928 68 69 73 20 65 70 68 65 6d 65 72 61 6c 20 74 61  his ephemeral ta
23929 62 6c 65 20 61 6e 64 20 66 6f 72 20 65 61 63 68  ble and for each
2392a 20 72 6f 77 20 69 6e 0a 2a 2a 20 74 68 65 20 65   row in.** the e
2392b 70 68 65 72 6d 65 72 61 6c 20 74 61 62 6c 65 20  phermeral table 
2392c 63 61 6c 6c 20 56 55 70 64 61 74 65 2e 0a 2a 2a  call VUpdate..**
2392d 0a 2a 2a 20 57 68 65 6e 20 66 69 6e 69 73 68 65  .** When finishe
2392e 64 2c 20 64 72 6f 70 20 74 68 65 20 65 70 68 65  d, drop the ephe
2392f 6d 65 72 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a  meral table..**.
23930 2a 2a 20 28 6e 6f 74 65 31 29 20 41 63 74 75 61  ** (note1) Actua
23931 6c 6c 79 2c 20 69 66 20 77 65 20 6b 6e 6f 77 20  lly, if we know 
23932 69 6e 20 61 64 76 61 6e 63 65 20 74 68 61 74 20  in advance that 
23933 28 41 29 20 69 73 20 61 6c 77 61 79 73 20 74 68  (A) is always th
23934 65 20 73 61 6d 65 0a 2a 2a 20 61 73 20 28 42 29  e same.** as (B)
23935 20 77 65 20 6f 6e 6c 79 20 73 74 6f 72 65 20 28   we only store (
23936 41 29 2c 20 74 68 65 6e 20 64 75 70 6c 69 63 61  A), then duplica
23937 74 65 20 28 41 29 20 77 68 65 6e 20 70 75 6c 6c  te (A) when pull
23938 69 6e 67 0a 2a 2a 20 69 74 20 6f 75 74 20 6f 66  ing.** it out of
23939 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74   the ephemeral t
2393a 61 62 6c 65 20 62 65 66 6f 72 65 20 63 61 6c 6c  able before call
2393b 69 6e 67 20 56 55 70 64 61 74 65 2e 0a 2a 2f 0a  ing VUpdate..*/.
2393c 73 74 61 74 69 63 20 76 6f 69 64 20 75 70 64 61  static void upda
2393d 74 65 56 69 72 74 75 61 6c 54 61 62 6c 65 28 0a  teVirtualTable(.
2393e 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2393f 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
23940 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
23941 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63  .  SrcList *pSrc
23942 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76  ,       /* The v
23943 69 72 74 75 61 6c 20 74 61 62 6c 65 20 74 6f 20  irtual table to 
23944 62 65 20 6d 6f 64 69 66 69 65 64 20 2a 2f 0a 20  be modified */. 
23945 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
23946 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72        /* The vir
23947 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tual table */.  
23948 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67  ExprList *pChang
23949 65 73 2c 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75  es,  /* The colu
2394a 6d 6e 73 20 74 6f 20 63 68 61 6e 67 65 20 69 6e  mns to change in
2394b 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61 74   the UPDATE stat
2394c 65 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20  ement */.  Expr 
2394d 2a 70 52 6f 77 69 64 2c 20 20 20 20 20 20 20 20  *pRowid,        
2394e 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 75 73  /* Expression us
2394f 65 64 20 74 6f 20 72 65 63 6f 6d 70 75 74 65 20  ed to recompute 
23950 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69  the rowid */.  i
23951 6e 74 20 2a 61 58 52 65 66 2c 20 20 20 20 20 20  nt *aXRef,      
23952 20 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66      /* Mapping f
23953 72 6f 6d 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 70  rom columns of p
23954 54 61 62 20 74 6f 20 65 6e 74 72 69 65 73 20 69  Tab to entries i
23955 6e 20 70 43 68 61 6e 67 65 73 20 2a 2f 0a 20 20  n pChanges */.  
23956 45 78 70 72 20 2a 70 57 68 65 72 65 20 20 20 20  Expr *pWhere    
23957 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c       /* WHERE cl
23958 61 75 73 65 20 6f 66 20 74 68 65 20 55 50 44 41  ause of the UPDA
23959 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  TE statement */.
2395a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  ){.  Vdbe *v = p
2395b 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 2f  Parse->pVdbe;  /
2395c 2a 20 56 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  * Virtual machin
2395d 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63  e under construc
2395e 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 4c 69  tion */.  ExprLi
2395f 73 74 20 2a 70 45 4c 69 73 74 20 3d 20 30 3b 20  st *pEList = 0; 
23960 20 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75 6c      /* The resul
23961 74 20 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c  t set of the SEL
23962 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  ECT statement */
23963 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  .  Select *pSele
23964 63 74 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20  ct = 0;      /* 
23965 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  The SELECT state
23966 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a  ment */.  Expr *
23967 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20  pExpr;          
23968 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79      /* Temporary
23969 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20   expression */. 
2396a 20 69 6e 74 20 65 70 68 65 6d 54 61 62 3b 20 20   int ephemTab;  
2396b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61             /* Ta
2396c 62 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68 65 20  ble holding the 
2396d 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 53 45  result of the SE
2396e 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  LECT */.  int i;
2396f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23970 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
23971 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  ter */.  int add
23972 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
23973 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
23974 20 74 6f 70 20 6f 66 20 6c 6f 6f 70 20 2a 2f 0a   top of loop */.
23975 20 20 69 6e 74 20 69 52 65 67 3b 20 20 20 20 20    int iReg;     
23976 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
23977 69 72 73 74 20 72 65 67 69 73 74 65 72 20 69 6e  irst register in
23978 20 73 65 74 20 70 61 73 73 65 64 20 74 6f 20 4f   set passed to O
23979 50 5f 56 55 70 64 61 74 65 20 2a 2f 0a 20 20 73  P_VUpdate */.  s
2397a 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2397b 72 73 65 2d 3e 64 62 3b 20 2f 2a 20 44 61 74 61  rse->db; /* Data
2397c 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
2397d 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
2397e 2a 70 56 74 61 62 20 3d 20 28 63 6f 6e 73 74 20  *pVtab = (const 
2397f 63 68 61 72 2a 29 70 54 61 62 2d 3e 70 56 74 61  char*)pTab->pVta
23980 62 3b 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20  b;.  SelectDest 
23981 64 65 73 74 3b 0a 0a 20 20 2f 2a 20 43 6f 6e 73  dest;..  /* Cons
23982 74 72 75 63 74 20 74 68 65 20 53 45 4c 45 43 54  truct the SELECT
23983 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
23984 77 69 6c 6c 20 66 69 6e 64 20 74 68 65 20 6e 65  will find the ne
23985 77 20 76 61 6c 75 65 73 20 66 6f 72 0a 20 20 2a  w values for.  *
23986 2a 20 61 6c 6c 20 75 70 64 61 74 65 64 20 72 6f  * all updated ro
23987 77 73 2e 20 0a 20 20 2a 2f 0a 20 20 70 45 4c 69  ws. .  */.  pELi
23988 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
23989 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
2398a 65 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20  e, 0, .         
2398b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2398c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43          sqlite3C
2398d 72 65 61 74 65 49 64 45 78 70 72 28 70 50 61 72  reateIdExpr(pPar
2398e 73 65 2c 20 22 5f 72 6f 77 69 64 5f 22 29 2c 20  se, "_rowid_"), 
2398f 30 29 3b 0a 20 20 69 66 28 20 70 52 6f 77 69 64  0);.  if( pRowid
23990 20 29 7b 0a 20 20 20 20 70 45 4c 69 73 74 20 3d   ){.    pEList =
23991 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
23992 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70  Append(pParse, p
23993 45 4c 69 73 74 2c 0a 20 20 20 20 20 20 20 20 20  EList,.         
23994 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23995 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
23996 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 52 6f  3ExprDup(db, pRo
23997 77 69 64 29 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  wid), 0);.  }.  
23998 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e 69 50  assert( pTab->iP
23999 4b 65 79 3c 30 20 29 3b 0a 20 20 66 6f 72 28 69  Key<0 );.  for(i
2399a 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c  =0; i<pTab->nCol
2399b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
2399c 61 58 52 65 66 5b 69 5d 3e 3d 30 20 29 7b 0a 20  aXRef[i]>=0 ){. 
2399d 20 20 20 20 20 70 45 78 70 72 20 3d 20 73 71 6c       pExpr = sql
2399e 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
2399f 70 43 68 61 6e 67 65 73 2d 3e 61 5b 61 58 52 65  pChanges->a[aXRe
239a0 66 5b 69 5d 5d 2e 70 45 78 70 72 29 3b 0a 20 20  f[i]].pExpr);.  
239a1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
239a2 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 43 72  Expr = sqlite3Cr
239a3 65 61 74 65 49 64 45 78 70 72 28 70 50 61 72 73  eateIdExpr(pPars
239a4 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d  e, pTab->aCol[i]
239a5 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20  .zName);.    }. 
239a6 20 20 20 70 45 4c 69 73 74 20 3d 20 73 71 6c 69     pEList = sqli
239a7 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
239a8 64 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74  d(pParse, pEList
239a9 2c 20 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 7d  , pExpr, 0);.  }
239aa 0a 20 20 70 53 65 6c 65 63 74 20 3d 20 73 71 6c  .  pSelect = sql
239ab 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50  ite3SelectNew(pP
239ac 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 70 53  arse, pEList, pS
239ad 72 63 2c 20 70 57 68 65 72 65 2c 20 30 2c 20 30  rc, pWhere, 0, 0
239ae 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  , 0, 0, 0, 0);. 
239af 20 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68   .  /* Create th
239b0 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c  e ephemeral tabl
239b1 65 20 69 6e 74 6f 20 77 68 69 63 68 20 74 68 65  e into which the
239b2 20 75 70 64 61 74 65 20 72 65 73 75 6c 74 73 20   update results 
239b3 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 73 74 6f  will.  ** be sto
239b4 72 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  red..  */.  asse
239b5 72 74 28 20 76 20 29 3b 0a 20 20 65 70 68 65 6d  rt( v );.  ephem
239b6 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  Tab = pParse->nT
239b7 61 62 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 56  ab++;.  sqlite3V
239b8 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
239b9 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 65  OpenEphemeral, e
239ba 70 68 65 6d 54 61 62 2c 20 70 54 61 62 2d 3e 6e  phemTab, pTab->n
239bb 43 6f 6c 2b 31 2b 28 70 52 6f 77 69 64 21 3d 30  Col+1+(pRowid!=0
239bc 29 29 3b 0a 0a 20 20 2f 2a 20 66 69 6c 6c 20 74  ));..  /* fill t
239bd 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62  he ephemeral tab
239be 6c 65 20 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  le .  */.  sqlit
239bf 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74  e3SelectDestInit
239c0 28 26 64 65 73 74 2c 20 53 52 54 5f 54 61 62 6c  (&dest, SRT_Tabl
239c1 65 2c 20 65 70 68 65 6d 54 61 62 29 3b 0a 20 20  e, ephemTab);.  
239c2 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50  sqlite3Select(pP
239c3 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 26  arse, pSelect, &
239c4 64 65 73 74 2c 20 30 2c 20 30 2c 20 30 2c 20 30  dest, 0, 0, 0, 0
239c5 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74  );..  /* Generat
239c6 65 20 63 6f 64 65 20 74 6f 20 73 63 61 6e 20 74  e code to scan t
239c7 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62  he ephemeral tab
239c8 6c 65 20 61 6e 64 20 63 61 6c 6c 20 56 55 70 64  le and call VUpd
239c9 61 74 65 2e 20 2a 2f 0a 20 20 69 52 65 67 20 3d  ate. */.  iReg =
239ca 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
239cb 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20  .  pParse->nMem 
239cc 2b 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 3b  += pTab->nCol+1;
239cd 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
239ce 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e  dOp2(v, OP_Rewin
239cf 64 2c 20 65 70 68 65 6d 54 61 62 2c 20 30 29 3b  d, ephemTab, 0);
239d0 0a 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65  .  addr = sqlite
239d1 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
239d2 28 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  (v);.  sqlite3Vd
239d3 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
239d4 6f 6c 75 6d 6e 2c 20 20 65 70 68 65 6d 54 61 62  olumn,  ephemTab
239d5 2c 20 30 2c 20 69 52 65 67 29 3b 0a 20 20 73 71  , 0, iReg);.  sq
239d6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
239d7 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 65 70  v, OP_Column, ep
239d8 68 65 6d 54 61 62 2c 20 28 70 52 6f 77 69 64 3f  hemTab, (pRowid?
239d9 31 3a 30 29 2c 20 69 52 65 67 2b 31 29 3b 0a 20  1:0), iReg+1);. 
239da 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62   for(i=0; i<pTab
239db 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  ->nCol; i++){.  
239dc 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
239dd 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
239de 2c 20 65 70 68 65 6d 54 61 62 2c 20 69 2b 31 2b  , ephemTab, i+1+
239df 28 70 52 6f 77 69 64 21 3d 30 29 2c 20 69 52 65  (pRowid!=0), iRe
239e0 67 2b 32 2b 69 29 3b 0a 20 20 7d 0a 20 20 73 71  g+2+i);.  }.  sq
239e1 6c 69 74 65 33 56 74 61 62 4d 61 6b 65 57 72 69  lite3VtabMakeWri
239e2 74 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54  table(pParse, pT
239e3 61 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ab);.  sqlite3Vd
239e4 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56  beAddOp4(v, OP_V
239e5 55 70 64 61 74 65 2c 20 30 2c 20 70 54 61 62 2d  Update, 0, pTab-
239e6 3e 6e 43 6f 6c 2b 32 2c 20 69 52 65 67 2c 20 70  >nCol+2, iReg, p
239e7 56 74 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a  Vtab, P4_VTAB);.
239e8 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
239e9 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20  Op2(v, OP_Next, 
239ea 65 70 68 65 6d 54 61 62 2c 20 61 64 64 72 29 3b  ephemTab, addr);
239eb 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75  .  sqlite3VdbeJu
239ec 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 2d 31  mpHere(v, addr-1
239ed 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
239ee 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp2(v, OP_Clo
239ef 73 65 2c 20 65 70 68 65 6d 54 61 62 2c 20 30 29  se, ephemTab, 0)
239f0 3b 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 75 70 20  ;..  /* Cleanup 
239f1 2a 2f 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65  */.  sqlite3Sele
239f2 63 74 44 65 6c 65 74 65 28 70 53 65 6c 65 63 74  ctDelete(pSelect
239f3 29 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  );  .}.#endif /*
239f4 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
239f5 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a  TUALTABLE */../*
239f6 20 4d 61 6b 65 20 73 75 72 65 20 22 69 73 56 69   Make sure "isVi
239f7 65 77 22 20 67 65 74 73 20 75 6e 64 65 66 69 6e  ew" gets undefin
239f8 65 64 20 69 6e 20 63 61 73 65 20 74 68 69 73 20  ed in case this 
239f9 66 69 6c 65 20 62 65 63 6f 6d 65 73 20 70 61 72  file becomes par
239fa 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61 6d 61 6c  t of.** the amal
239fb 67 61 6d 61 74 69 6f 6e 20 2d 20 73 6f 20 74 68  gamation - so th
239fc 61 74 20 73 75 62 73 65 71 75 65 6e 74 20 66 69  at subsequent fi
239fd 6c 65 73 20 64 6f 20 6e 6f 74 20 73 65 65 20 69  les do not see i
239fe 73 56 69 65 77 20 61 73 20 61 0a 2a 2a 20 6d 61  sView as a.** ma
239ff 63 72 6f 2e 20 2a 2f 0a 23 75 6e 64 65 66 20 69  cro. */.#undef i
23a00 73 56 69 65 77 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  sView../********
23a01 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 75 70  ****** End of up
23a02 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  date.c *********
23a03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23a04 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23a05 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
23a06 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
23a07 65 20 76 61 63 75 75 6d 2e 63 20 2a 2a 2a 2a 2a  e vacuum.c *****
23a08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23a09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23a0a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
23a0b 33 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20  3 April 6.**.** 
23a0c 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
23a0d 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
23a0e 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
23a0f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
23a10 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
23a11 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
23a12 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
23a13 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
23a14 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
23a15 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
23a16 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
23a17 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
23a18 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
23a19 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
23a1a 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
23a1b 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
23a1c 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
23a1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23a1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23a1f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23a21 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
23a22 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
23a23 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c  ode used to impl
23a24 65 6d 65 6e 74 20 74 68 65 20 56 41 43 55 55 4d  ement the VACUUM
23a25 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20   command..**.** 
23a26 4d 6f 73 74 20 6f 66 20 74 68 65 20 63 6f 64 65  Most of the code
23a27 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 6d 61   in this file ma
23a28 79 20 62 65 20 6f 6d 69 74 74 65 64 20 62 79 20  y be omitted by 
23a29 64 65 66 69 6e 69 6e 67 20 74 68 65 0a 2a 2a 20  defining the.** 
23a2a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55  SQLITE_OMIT_VACU
23a2b 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20  UM macro..**.** 
23a2c 24 49 64 3a 20 76 61 63 75 75 6d 2e 63 2c 76 20  $Id: vacuum.c,v 
23a2d 31 2e 37 38 20 32 30 30 38 2f 30 34 2f 33 30 20  1.78 2008/04/30 
23a2e 31 36 3a 33 38 3a 32 33 20 64 72 68 20 45 78 70  16:38:23 drh Exp
23a2f 20 24 0a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69   $.*/..#if !defi
23a30 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
23a31 56 41 43 55 55 4d 29 20 26 26 20 21 64 65 66 69  VACUUM) && !defi
23a32 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
23a33 41 54 54 41 43 48 29 0a 2f 2a 0a 2a 2a 20 45 78  ATTACH)./*.** Ex
23a34 65 63 75 74 65 20 7a 53 71 6c 20 6f 6e 20 64 61  ecute zSql on da
23a35 74 61 62 61 73 65 20 64 62 2e 20 52 65 74 75 72  tabase db. Retur
23a36 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e  n an error code.
23a37 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65  .*/.static int e
23a38 78 65 63 53 71 6c 28 73 71 6c 69 74 65 33 20 2a  xecSql(sqlite3 *
23a39 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
23a3a 7a 53 71 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33  zSql){.  sqlite3
23a3b 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20  _stmt *pStmt;.  
23a3c 69 66 28 20 21 7a 53 71 6c 20 29 7b 0a 20 20 20  if( !zSql ){.   
23a3d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
23a3e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20  OMEM;.  }.  if( 
23a3f 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74  SQLITE_OK!=sqlit
23a40 65 33 5f 70 72 65 70 61 72 65 28 64 62 2c 20 7a  e3_prepare(db, z
23a41 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c  Sql, -1, &pStmt,
23a42 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72   0) ){.    retur
23a43 6e 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64  n sqlite3_errcod
23a44 65 28 64 62 29 3b 0a 20 20 7d 0a 20 20 77 68 69  e(db);.  }.  whi
23a45 6c 65 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d  le( SQLITE_ROW==
23a46 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74  sqlite3_step(pSt
23a47 6d 74 29 20 29 7b 7d 0a 20 20 72 65 74 75 72 6e  mt) ){}.  return
23a48 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
23a49 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a 0a  e(pStmt);.}../*.
23a4a 2a 2a 20 45 78 65 63 75 74 65 20 7a 53 71 6c 20  ** Execute zSql 
23a4b 6f 6e 20 64 61 74 61 62 61 73 65 20 64 62 2e 20  on database db. 
23a4c 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20 72 65  The statement re
23a4d 74 75 72 6e 73 20 65 78 61 63 74 6c 79 0a 2a 2a  turns exactly.**
23a4e 20 6f 6e 65 20 63 6f 6c 75 6d 6e 2e 20 45 78 65   one column. Exe
23a4f 63 75 74 65 20 74 68 69 73 20 61 73 20 53 51 4c  cute this as SQL
23a50 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74   on the same dat
23a51 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  abase..*/.static
23a52 20 69 6e 74 20 65 78 65 63 45 78 65 63 53 71 6c   int execExecSql
23a53 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
23a54 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 29 7b  nst char *zSql){
23a55 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
23a56 2a 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 72 63  *pStmt;.  int rc
23a57 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
23a58 33 5f 70 72 65 70 61 72 65 28 64 62 2c 20 7a 53  3_prepare(db, zS
23a59 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20  ql, -1, &pStmt, 
23a5a 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  0);.  if( rc!=SQ
23a5b 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
23a5c 20 72 63 3b 0a 0a 20 20 77 68 69 6c 65 28 20 53   rc;..  while( S
23a5d 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74  QLITE_ROW==sqlit
23a5e 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29  e3_step(pStmt) )
23a5f 7b 0a 20 20 20 20 72 63 20 3d 20 65 78 65 63 53  {.    rc = execS
23a60 71 6c 28 64 62 2c 20 28 63 68 61 72 2a 29 73 71  ql(db, (char*)sq
23a61 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
23a62 74 28 70 53 74 6d 74 2c 20 30 29 29 3b 0a 20 20  t(pStmt, 0));.  
23a63 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
23a64 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
23a65 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53  ite3_finalize(pS
23a66 74 6d 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75  tmt);.      retu
23a67 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d  rn rc;.    }.  }
23a68 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ..  return sqlit
23a69 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d  e3_finalize(pStm
23a6a 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  t);.}../*.** The
23a6b 20 6e 6f 6e 2d 73 74 61 6e 64 61 72 64 20 56 41   non-standard VA
23a6c 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 20 69 73 20  CUUM command is 
23a6d 75 73 65 64 20 74 6f 20 63 6c 65 61 6e 20 75 70  used to clean up
23a6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 0a 2a   the database,.*
23a6f 2a 20 63 6f 6c 6c 61 70 73 65 20 66 72 65 65 20  * collapse free 
23a70 73 70 61 63 65 2c 20 65 74 63 2e 20 20 49 74 20  space, etc.  It 
23a71 69 73 20 6d 6f 64 65 6c 6c 65 64 20 61 66 74 65  is modelled afte
23a72 72 20 74 68 65 20 56 41 43 55 55 4d 20 63 6f 6d  r the VACUUM com
23a73 6d 61 6e 64 0a 2a 2a 20 69 6e 20 50 6f 73 74 67  mand.** in Postg
23a74 72 65 53 51 4c 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  reSQL..**.** In 
23a75 76 65 72 73 69 6f 6e 20 31 2e 30 2e 78 20 6f 66  version 1.0.x of
23a76 20 53 51 4c 69 74 65 2c 20 74 68 65 20 56 41 43   SQLite, the VAC
23a77 55 55 4d 20 63 6f 6d 6d 61 6e 64 20 77 6f 75 6c  UUM command woul
23a78 64 20 63 61 6c 6c 0a 2a 2a 20 67 64 62 6d 5f 72  d call.** gdbm_r
23a79 65 6f 72 67 61 6e 69 7a 65 28 29 20 6f 6e 20 61  eorganize() on a
23a7a 6c 6c 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ll the database 
23a7b 74 61 62 6c 65 73 2e 20 20 42 75 74 20 62 65 67  tables.  But beg
23a7c 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69 74 68 20 32  inning.** with 2
23a7d 2e 30 2e 30 2c 20 53 51 4c 69 74 65 20 6e 6f 20  .0.0, SQLite no 
23a7e 6c 6f 6e 67 65 72 20 75 73 65 73 20 47 44 42 4d  longer uses GDBM
23a7f 20 73 6f 20 74 68 69 73 20 63 6f 6d 6d 61 6e 64   so this command
23a80 20 68 61 73 0a 2a 2a 20 62 65 63 6f 6d 65 20 61   has.** become a
23a81 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54   no-op..*/.SQLIT
23a82 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
23a83 71 6c 69 74 65 33 56 61 63 75 75 6d 28 50 61 72  qlite3Vacuum(Par
23a84 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 56  se *pParse){.  V
23a85 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33  dbe *v = sqlite3
23a86 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
23a87 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20  .  if( v ){.    
23a88 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23a89 32 28 76 2c 20 4f 50 5f 56 61 63 75 75 6d 2c 20  2(v, OP_Vacuum, 
23a8a 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74  0, 0);.  }.  ret
23a8b 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  urn;.}../*.** Th
23a8c 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65  is routine imple
23a8d 6d 65 6e 74 73 20 74 68 65 20 4f 50 5f 56 61 63  ments the OP_Vac
23a8e 75 75 6d 20 6f 70 63 6f 64 65 20 6f 66 20 74 68  uum opcode of th
23a8f 65 20 56 44 42 45 2e 0a 2a 2f 0a 53 51 4c 49 54  e VDBE..*/.SQLIT
23a90 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
23a91 6c 69 74 65 33 52 75 6e 56 61 63 75 75 6d 28 63  lite3RunVacuum(c
23a92 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c 20  har **pzErrMsg, 
23a93 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
23a94 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
23a95 4f 4b 3b 20 20 20 20 20 2f 2a 20 52 65 74 75 72  OK;     /* Retur
23a96 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 65 72 76  n code from serv
23a97 69 63 65 20 72 6f 75 74 69 6e 65 73 20 2a 2f 0a  ice routines */.
23a98 20 20 42 74 72 65 65 20 2a 70 4d 61 69 6e 3b 20    Btree *pMain; 
23a99 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
23a9a 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 20   database being 
23a9b 76 61 63 75 75 6d 65 64 20 2a 2f 0a 20 20 42 74  vacuumed */.  Bt
23a9c 72 65 65 20 2a 70 54 65 6d 70 3b 20 20 20 20 20  ree *pTemp;     
23a9d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 65 6d        /* The tem
23a9e 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 20  porary database 
23a9f 77 65 20 76 61 63 75 75 6d 20 69 6e 74 6f 20 2a  we vacuum into *
23aa0 2f 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d  /.  char *zSql =
23aa1 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53   0;         /* S
23aa2 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f  QL statements */
23aa3 0a 20 20 69 6e 74 20 73 61 76 65 64 5f 66 6c 61  .  int saved_fla
23aa4 67 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 61  gs;        /* Sa
23aa5 76 65 64 20 76 61 6c 75 65 20 6f 66 20 74 68 65  ved value of the
23aa6 20 64 62 2d 3e 66 6c 61 67 73 20 2a 2f 0a 20 20   db->flags */.  
23aa7 44 62 20 2a 70 44 62 20 3d 20 30 3b 20 20 20 20  Db *pDb = 0;    
23aa8 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
23aa9 61 73 65 20 74 6f 20 64 65 74 61 63 68 20 61 74  ase to detach at
23aaa 20 65 6e 64 20 6f 66 20 76 61 63 75 75 6d 20 2a   end of vacuum *
23aab 2f 0a 20 20 69 6e 74 20 6e 52 65 73 3b 0a 0a 20  /.  int nRes;.. 
23aac 20 2f 2a 20 53 61 76 65 20 74 68 65 20 63 75 72   /* Save the cur
23aad 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68  rent value of th
23aae 65 20 77 72 69 74 65 2d 73 63 68 65 6d 61 20 66  e write-schema f
23aaf 6c 61 67 20 62 65 66 6f 72 65 20 73 65 74 74 69  lag before setti
23ab0 6e 67 20 69 74 2e 20 2a 2f 0a 20 20 73 61 76 65  ng it. */.  save
23ab1 64 5f 66 6c 61 67 73 20 3d 20 64 62 2d 3e 66 6c  d_flags = db->fl
23ab2 61 67 73 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73  ags;.  db->flags
23ab3 20 7c 3d 20 53 51 4c 49 54 45 5f 57 72 69 74 65   |= SQLITE_Write
23ab4 53 63 68 65 6d 61 20 7c 20 53 51 4c 49 54 45 5f  Schema | SQLITE_
23ab5 49 67 6e 6f 72 65 43 68 65 63 6b 73 3b 0a 0a 20  IgnoreChecks;.. 
23ab6 20 69 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f   if( !db->autoCo
23ab7 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  mmit ){.    sqli
23ab8 74 65 33 53 65 74 53 74 72 69 6e 67 28 70 7a 45  te3SetString(pzE
23ab9 72 72 4d 73 67 2c 20 22 63 61 6e 6e 6f 74 20 56  rrMsg, "cannot V
23aba 41 43 55 55 4d 20 66 72 6f 6d 20 77 69 74 68 69  ACUUM from withi
23abb 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 22  n a transaction"
23abc 2c 20 0a 20 20 20 20 20 20 20 28 63 68 61 72 2a  , .       (char*
23abd 29 30 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  )0);.    rc = SQ
23abe 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
23abf 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75  goto end_of_vacu
23ac0 75 6d 3b 0a 20 20 7d 0a 20 20 70 4d 61 69 6e 20  um;.  }.  pMain 
23ac1 3d 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74  = db->aDb[0].pBt
23ac2 3b 0a 0a 20 20 2f 2a 20 41 74 74 61 63 68 20 74  ;..  /* Attach t
23ac3 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74  he temporary dat
23ac4 61 62 61 73 65 20 61 73 20 27 76 61 63 75 75 6d  abase as 'vacuum
23ac5 5f 64 62 27 2e 20 54 68 65 20 73 79 6e 63 68 72  _db'. The synchr
23ac6 6f 6e 6f 75 73 20 70 72 61 67 6d 61 0a 20 20 2a  onous pragma.  *
23ac7 2a 20 63 61 6e 20 62 65 20 73 65 74 20 74 6f 20  * can be set to 
23ac8 27 6f 66 66 27 20 66 6f 72 20 74 68 69 73 20 66  'off' for this f
23ac9 69 6c 65 2c 20 61 73 20 69 74 20 69 73 20 6e 6f  ile, as it is no
23aca 74 20 72 65 63 6f 76 65 72 65 64 20 69 66 20 61  t recovered if a
23acb 20 63 72 61 73 68 0a 20 20 2a 2a 20 6f 63 63 75   crash.  ** occu
23acc 72 73 20 61 6e 79 77 61 79 2e 20 54 68 65 20 69  rs anyway. The i
23acd 6e 74 65 67 72 69 74 79 20 6f 66 20 74 68 65 20  ntegrity of the 
23ace 64 61 74 61 62 61 73 65 20 69 73 20 6d 61 69 6e  database is main
23acf 74 61 69 6e 65 64 20 62 79 20 61 0a 20 20 2a 2a  tained by a.  **
23ad0 20 28 70 6f 73 73 69 62 6c 79 20 73 79 6e 63 68   (possibly synch
23ad1 72 6f 6e 6f 75 73 29 20 74 72 61 6e 73 61 63 74  ronous) transact
23ad2 69 6f 6e 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68  ion opened on th
23ad3 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
23ad4 62 65 66 6f 72 65 0a 20 20 2a 2a 20 73 71 6c 69  before.  ** sqli
23ad5 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c 65  te3BtreeCopyFile
23ad6 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20  () is called..  
23ad7 2a 2a 0a 20 20 2a 2a 20 41 6e 20 6f 70 74 69 6d  **.  ** An optim
23ad8 69 73 61 74 69 6f 6e 20 77 6f 75 6c 64 20 62 65  isation would be
23ad9 20 74 6f 20 75 73 65 20 61 20 6e 6f 6e 2d 6a 6f   to use a non-jo
23ada 75 72 6e 61 6c 65 64 20 70 61 67 65 72 2e 0a 20  urnaled pager.. 
23adb 20 2a 2a 20 28 4c 61 74 65 72 3a 29 20 49 20 74   ** (Later:) I t
23adc 72 69 65 64 20 73 65 74 74 69 6e 67 20 22 50 52  ried setting "PR
23add 41 47 4d 41 20 76 61 63 75 75 6d 5f 64 62 2e 6a  AGMA vacuum_db.j
23ade 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4f 46 46 22  ournal_mode=OFF"
23adf 20 62 75 74 0a 20 20 2a 2a 20 74 68 61 74 20 61   but.  ** that a
23ae0 63 74 75 61 6c 6c 79 20 6d 61 64 65 20 74 68 65  ctually made the
23ae1 20 56 41 43 55 55 4d 20 72 75 6e 20 73 6c 6f 77   VACUUM run slow
23ae2 65 72 2e 20 20 56 65 72 79 20 6c 69 74 74 6c 65  er.  Very little
23ae3 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 0a 20 20 2a   journalling.  *
23ae4 2a 20 61 63 74 75 61 6c 6c 79 20 6f 63 63 75 72  * actually occur
23ae5 73 20 77 68 65 6e 20 64 6f 69 6e 67 20 61 20 76  s when doing a v
23ae6 61 63 75 75 6d 20 73 69 6e 63 65 20 74 68 65 20  acuum since the 
23ae7 76 61 63 75 75 6d 5f 64 62 20 69 73 20 69 6e 69  vacuum_db is ini
23ae8 74 69 61 6c 6c 79 0a 20 20 2a 2a 20 65 6d 70 74  tially.  ** empt
23ae9 79 2e 20 20 4f 6e 6c 79 20 74 68 65 20 6a 6f 75  y.  Only the jou
23aea 72 6e 61 6c 20 68 65 61 64 65 72 20 69 73 20 77  rnal header is w
23aeb 72 69 74 74 65 6e 2e 20 20 41 70 70 61 72 65 6e  ritten.  Apparen
23aec 74 6c 79 20 69 74 20 74 61 6b 65 73 20 6d 6f 72  tly it takes mor
23aed 65 0a 20 20 2a 2a 20 74 69 6d 65 20 74 6f 20 70  e.  ** time to p
23aee 61 72 73 65 20 61 6e 64 20 72 75 6e 20 74 68 65  arse and run the
23aef 20 50 52 41 47 4d 41 20 74 6f 20 74 75 72 6e 20   PRAGMA to turn 
23af0 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 6f 66 66 20  journalling off 
23af1 74 68 61 6e 20 69 74 20 64 6f 65 73 0a 20 20 2a  than it does.  *
23af2 2a 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 6a  * to write the j
23af3 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 69  ournal header fi
23af4 6c 65 2e 0a 20 20 2a 2f 0a 20 20 7a 53 71 6c 20  le..  */.  zSql 
23af5 3d 20 22 41 54 54 41 43 48 20 27 27 20 41 53 20  = "ATTACH '' AS 
23af6 76 61 63 75 75 6d 5f 64 62 3b 22 3b 0a 20 20 72  vacuum_db;";.  r
23af7 63 20 3d 20 65 78 65 63 53 71 6c 28 64 62 2c 20  c = execSql(db, 
23af8 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20 72 63 21  zSql);.  if( rc!
23af9 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
23afa 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b  o end_of_vacuum;
23afb 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44  .  pDb = &db->aD
23afc 62 5b 64 62 2d 3e 6e 44 62 2d 31 5d 3b 0a 20 20  b[db->nDb-1];.  
23afd 61 73 73 65 72 74 28 20 73 74 72 63 6d 70 28 64  assert( strcmp(d
23afe 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44 62 2d 31  b->aDb[db->nDb-1
23aff 5d 2e 7a 4e 61 6d 65 2c 22 76 61 63 75 75 6d 5f  ].zName,"vacuum_
23b00 64 62 22 29 3d 3d 30 20 29 3b 0a 20 20 70 54 65  db")==0 );.  pTe
23b01 6d 70 20 3d 20 64 62 2d 3e 61 44 62 5b 64 62 2d  mp = db->aDb[db-
23b02 3e 6e 44 62 2d 31 5d 2e 70 42 74 3b 0a 0a 20 20  >nDb-1].pBt;..  
23b03 6e 52 65 73 20 3d 20 73 71 6c 69 74 65 33 42 74  nRes = sqlite3Bt
23b04 72 65 65 47 65 74 52 65 73 65 72 76 65 28 70 4d  reeGetReserve(pM
23b05 61 69 6e 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  ain);.  if( sqli
23b06 74 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53  te3BtreeSetPageS
23b07 69 7a 65 28 70 54 65 6d 70 2c 20 73 71 6c 69 74  ize(pTemp, sqlit
23b08 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69  e3BtreeGetPageSi
23b09 7a 65 28 70 4d 61 69 6e 29 2c 20 6e 52 65 73 29  ze(pMain), nRes)
23b0a 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 42 74  .   || sqlite3Bt
23b0b 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 70  reeSetPageSize(p
23b0c 54 65 6d 70 2c 20 64 62 2d 3e 6e 65 78 74 50 61  Temp, db->nextPa
23b0d 67 65 73 69 7a 65 2c 20 6e 52 65 73 29 0a 20 20  gesize, nRes).  
23b0e 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
23b0f 69 6c 65 64 20 0a 20 20 29 7b 0a 20 20 20 20 72  iled .  ){.    r
23b10 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
23b11 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6f  ;.    goto end_o
23b12 66 5f 76 61 63 75 75 6d 3b 0a 20 20 7d 0a 20 20  f_vacuum;.  }.  
23b13 72 63 20 3d 20 65 78 65 63 53 71 6c 28 64 62 2c  rc = execSql(db,
23b14 20 22 50 52 41 47 4d 41 20 76 61 63 75 75 6d 5f   "PRAGMA vacuum_
23b15 64 62 2e 73 79 6e 63 68 72 6f 6e 6f 75 73 3d 4f  db.synchronous=O
23b16 46 46 22 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  FF");.  if( rc!=
23b17 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
23b18 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63   goto end_of_vac
23b19 75 75 6d 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65  uum;.  }..#ifnde
23b1a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
23b1b 54 4f 56 41 43 55 55 4d 0a 20 20 73 71 6c 69 74  TOVACUUM.  sqlit
23b1c 65 33 42 74 72 65 65 53 65 74 41 75 74 6f 56 61  e3BtreeSetAutoVa
23b1d 63 75 75 6d 28 70 54 65 6d 70 2c 20 64 62 2d 3e  cuum(pTemp, db->
23b1e 6e 65 78 74 41 75 74 6f 76 61 63 3e 3d 30 20 3f  nextAutovac>=0 ?
23b1f 20 64 62 2d 3e 6e 65 78 74 41 75 74 6f 76 61 63   db->nextAutovac
23b20 20 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   :.             
23b21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23b22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
23b23 6c 69 74 65 33 42 74 72 65 65 47 65 74 41 75 74  lite3BtreeGetAut
23b24 6f 56 61 63 75 75 6d 28 70 4d 61 69 6e 29 29 3b  oVacuum(pMain));
23b25 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65  .#endif..  /* Be
23b26 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f  gin a transactio
23b27 6e 20 2a 2f 0a 20 20 72 63 20 3d 20 65 78 65 63  n */.  rc = exec
23b28 53 71 6c 28 64 62 2c 20 22 42 45 47 49 4e 20 45  Sql(db, "BEGIN E
23b29 58 43 4c 55 53 49 56 45 3b 22 29 3b 0a 20 20 69  XCLUSIVE;");.  i
23b2a 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
23b2b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76   ) goto end_of_v
23b2c 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20 51 75 65  acuum;..  /* Que
23b2d 72 79 20 74 68 65 20 73 63 68 65 6d 61 20 6f 66  ry the schema of
23b2e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
23b2f 73 65 2e 20 43 72 65 61 74 65 20 61 20 6d 69 72  se. Create a mir
23b30 72 6f 72 20 73 63 68 65 6d 61 0a 20 20 2a 2a 20  ror schema.  ** 
23b31 69 6e 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  in the temporary
23b32 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a   database..  */.
23b33 20 20 72 63 20 3d 20 65 78 65 63 45 78 65 63 53    rc = execExecS
23b34 71 6c 28 64 62 2c 20 0a 20 20 20 20 20 20 22 53  ql(db, .      "S
23b35 45 4c 45 43 54 20 27 43 52 45 41 54 45 20 54 41  ELECT 'CREATE TA
23b36 42 4c 45 20 76 61 63 75 75 6d 5f 64 62 2e 27 20  BLE vacuum_db.' 
23b37 7c 7c 20 73 75 62 73 74 72 28 73 71 6c 2c 31 34  || substr(sql,14
23b38 29 20 22 0a 20 20 20 20 20 20 22 20 20 46 52 4f  ) ".      "  FRO
23b39 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  M sqlite_master 
23b3a 57 48 45 52 45 20 74 79 70 65 3d 27 74 61 62 6c  WHERE type='tabl
23b3b 65 27 20 41 4e 44 20 6e 61 6d 65 21 3d 27 73 71  e' AND name!='sq
23b3c 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 27 22 0a  lite_sequence'".
23b3d 20 20 20 20 20 20 22 20 20 20 41 4e 44 20 72 6f        "   AND ro
23b3e 6f 74 70 61 67 65 3e 30 22 0a 20 20 29 3b 0a 20  otpage>0".  );. 
23b3f 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
23b40 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
23b41 5f 76 61 63 75 75 6d 3b 0a 20 20 72 63 20 3d 20  _vacuum;.  rc = 
23b42 65 78 65 63 45 78 65 63 53 71 6c 28 64 62 2c 20  execExecSql(db, 
23b43 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 27  .      "SELECT '
23b44 43 52 45 41 54 45 20 49 4e 44 45 58 20 76 61 63  CREATE INDEX vac
23b45 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 73 75 62 73  uum_db.' || subs
23b46 74 72 28 73 71 6c 2c 31 34 29 22 0a 20 20 20 20  tr(sql,14)".    
23b47 20 20 22 20 20 46 52 4f 4d 20 73 71 6c 69 74 65    "  FROM sqlite
23b48 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20 73 71  _master WHERE sq
23b49 6c 20 4c 49 4b 45 20 27 43 52 45 41 54 45 20 49  l LIKE 'CREATE I
23b4a 4e 44 45 58 20 25 27 20 22 29 3b 0a 20 20 69 66  NDEX %' ");.  if
23b4b 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
23b4c 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61  ) goto end_of_va
23b4d 63 75 75 6d 3b 0a 20 20 72 63 20 3d 20 65 78 65  cuum;.  rc = exe
23b4e 63 45 78 65 63 53 71 6c 28 64 62 2c 20 0a 20 20  cExecSql(db, .  
23b4f 20 20 20 20 22 53 45 4c 45 43 54 20 27 43 52 45      "SELECT 'CRE
23b50 41 54 45 20 55 4e 49 51 55 45 20 49 4e 44 45 58  ATE UNIQUE INDEX
23b51 20 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20   vacuum_db.' || 
23b52 73 75 62 73 74 72 28 73 71 6c 2c 32 31 29 20 22  substr(sql,21) "
23b53 0a 20 20 20 20 20 20 22 20 20 46 52 4f 4d 20 73  .      "  FROM s
23b54 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45  qlite_master WHE
23b55 52 45 20 73 71 6c 20 4c 49 4b 45 20 27 43 52 45  RE sql LIKE 'CRE
23b56 41 54 45 20 55 4e 49 51 55 45 20 49 4e 44 45 58  ATE UNIQUE INDEX
23b57 20 25 27 22 29 3b 0a 20 20 69 66 28 20 72 63 21   %'");.  if( rc!
23b58 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
23b59 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b  o end_of_vacuum;
23b5a 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f  ..  /* Loop thro
23b5b 75 67 68 20 74 68 65 20 74 61 62 6c 65 73 20 69  ugh the tables i
23b5c 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  n the main datab
23b5d 61 73 65 2e 20 46 6f 72 20 65 61 63 68 2c 20 64  ase. For each, d
23b5e 6f 0a 20 20 2a 2a 20 61 6e 20 22 49 4e 53 45 52  o.  ** an "INSER
23b5f 54 20 49 4e 54 4f 20 76 61 63 75 75 6d 5f 64 62  T INTO vacuum_db
23b60 2e 78 78 78 20 53 45 4c 45 43 54 20 2a 20 46 52  .xxx SELECT * FR
23b61 4f 4d 20 78 78 78 3b 22 20 74 6f 20 63 6f 70 79  OM xxx;" to copy
23b62 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e  .  ** the conten
23b63 74 73 20 74 6f 20 74 68 65 20 74 65 6d 70 6f 72  ts to the tempor
23b64 61 72 79 20 64 61 74 61 62 61 73 65 2e 0a 20 20  ary database..  
23b65 2a 2f 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78  */.  rc = execEx
23b66 65 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20  ecSql(db, .     
23b67 20 22 53 45 4c 45 43 54 20 27 49 4e 53 45 52 54   "SELECT 'INSERT
23b68 20 49 4e 54 4f 20 76 61 63 75 75 6d 5f 64 62 2e   INTO vacuum_db.
23b69 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29  ' || quote(name)
23b6a 20 22 0a 20 20 20 20 20 20 22 7c 7c 20 27 20 53   ".      "|| ' S
23b6b 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 27 20 7c  ELECT * FROM ' |
23b6c 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29 20 7c 7c  | quote(name) ||
23b6d 20 27 3b 27 22 0a 20 20 20 20 20 20 22 46 52 4f   ';'".      "FRO
23b6e 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  M sqlite_master 
23b6f 22 0a 20 20 20 20 20 20 22 57 48 45 52 45 20 74  ".      "WHERE t
23b70 79 70 65 20 3d 20 27 74 61 62 6c 65 27 20 41 4e  ype = 'table' AN
23b71 44 20 6e 61 6d 65 21 3d 27 73 71 6c 69 74 65 5f  D name!='sqlite_
23b72 73 65 71 75 65 6e 63 65 27 20 22 0a 20 20 20 20  sequence' ".    
23b73 20 20 22 20 20 41 4e 44 20 72 6f 6f 74 70 61 67    "  AND rootpag
23b74 65 3e 30 22 0a 0a 20 20 29 3b 0a 20 20 69 66 28  e>0"..  );.  if(
23b75 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
23b76 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63   goto end_of_vac
23b77 75 75 6d 3b 0a 0a 20 20 2f 2a 20 43 6f 70 79 20  uum;..  /* Copy 
23b78 6f 76 65 72 20 74 68 65 20 73 65 71 75 65 6e 63  over the sequenc
23b79 65 20 74 61 62 6c 65 0a 20 20 2a 2f 0a 20 20 72  e table.  */.  r
23b7a 63 20 3d 20 65 78 65 63 45 78 65 63 53 71 6c 28  c = execExecSql(
23b7b 64 62 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45  db, .      "SELE
23b7c 43 54 20 27 44 45 4c 45 54 45 20 46 52 4f 4d 20  CT 'DELETE FROM 
23b7d 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 71  vacuum_db.' || q
23b7e 75 6f 74 65 28 6e 61 6d 65 29 20 7c 7c 20 27 3b  uote(name) || ';
23b7f 27 20 22 0a 20 20 20 20 20 20 22 46 52 4f 4d 20  ' ".      "FROM 
23b80 76 61 63 75 75 6d 5f 64 62 2e 73 71 6c 69 74 65  vacuum_db.sqlite
23b81 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20 6e 61  _master WHERE na
23b82 6d 65 3d 27 73 71 6c 69 74 65 5f 73 65 71 75 65  me='sqlite_seque
23b83 6e 63 65 27 20 22 0a 20 20 29 3b 0a 20 20 69 66  nce' ".  );.  if
23b84 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
23b85 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61  ) goto end_of_va
23b86 63 75 75 6d 3b 0a 20 20 72 63 20 3d 20 65 78 65  cuum;.  rc = exe
23b87 63 45 78 65 63 53 71 6c 28 64 62 2c 20 0a 20 20  cExecSql(db, .  
23b88 20 20 20 20 22 53 45 4c 45 43 54 20 27 49 4e 53      "SELECT 'INS
23b89 45 52 54 20 49 4e 54 4f 20 76 61 63 75 75 6d 5f  ERT INTO vacuum_
23b8a 64 62 2e 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61  db.' || quote(na
23b8b 6d 65 29 20 22 0a 20 20 20 20 20 20 22 7c 7c 20  me) ".      "|| 
23b8c 27 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  ' SELECT * FROM 
23b8d 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29  ' || quote(name)
23b8e 20 7c 7c 20 27 3b 27 20 22 0a 20 20 20 20 20 20   || ';' ".      
23b8f 22 46 52 4f 4d 20 76 61 63 75 75 6d 5f 64 62 2e  "FROM vacuum_db.
23b90 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48  sqlite_master WH
23b91 45 52 45 20 6e 61 6d 65 3d 3d 27 73 71 6c 69 74  ERE name=='sqlit
23b92 65 5f 73 65 71 75 65 6e 63 65 27 3b 22 0a 20 20  e_sequence';".  
23b93 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
23b94 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e  ITE_OK ) goto en
23b95 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 0a 20  d_of_vacuum;... 
23b96 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 74 72 69   /* Copy the tri
23b97 67 67 65 72 73 2c 20 76 69 65 77 73 2c 20 61 6e  ggers, views, an
23b98 64 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73  d virtual tables
23b99 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 64   from the main d
23b9a 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 6f 76 65  atabase.  ** ove
23b9b 72 20 74 6f 20 74 68 65 20 74 65 6d 70 6f 72 61  r to the tempora
23b9c 72 79 20 64 61 74 61 62 61 73 65 2e 20 20 4e 6f  ry database.  No
23b9d 6e 65 20 6f 66 20 74 68 65 73 65 20 6f 62 6a 65  ne of these obje
23b9e 63 74 73 20 68 61 73 20 61 6e 79 0a 20 20 2a 2a  cts has any.  **
23b9f 20 61 73 73 6f 63 69 61 74 65 64 20 73 74 6f 72   associated stor
23ba0 61 67 65 2c 20 73 6f 20 61 6c 6c 20 77 65 20 68  age, so all we h
23ba1 61 76 65 20 74 6f 20 64 6f 20 69 73 20 63 6f 70  ave to do is cop
23ba2 79 20 74 68 65 69 72 20 65 6e 74 72 69 65 73 0a  y their entries.
23ba3 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 53 51    ** from the SQ
23ba4 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c  LITE_MASTER tabl
23ba5 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 65  e..  */.  rc = e
23ba6 78 65 63 53 71 6c 28 64 62 2c 0a 20 20 20 20 20  xecSql(db,.     
23ba7 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 76 61   "INSERT INTO va
23ba8 63 75 75 6d 5f 64 62 2e 73 71 6c 69 74 65 5f 6d  cuum_db.sqlite_m
23ba9 61 73 74 65 72 20 22 0a 20 20 20 20 20 20 22 20  aster ".      " 
23baa 20 53 45 4c 45 43 54 20 74 79 70 65 2c 20 6e 61   SELECT type, na
23bab 6d 65 2c 20 74 62 6c 5f 6e 61 6d 65 2c 20 72 6f  me, tbl_name, ro
23bac 6f 74 70 61 67 65 2c 20 73 71 6c 22 0a 20 20 20  otpage, sql".   
23bad 20 20 20 22 20 20 20 20 46 52 4f 4d 20 73 71 6c     "    FROM sql
23bae 69 74 65 5f 6d 61 73 74 65 72 22 0a 20 20 20 20  ite_master".    
23baf 20 20 22 20 20 20 57 48 45 52 45 20 74 79 70 65    "   WHERE type
23bb0 3d 27 76 69 65 77 27 20 4f 52 20 74 79 70 65 3d  ='view' OR type=
23bb1 27 74 72 69 67 67 65 72 27 22 0a 20 20 20 20 20  'trigger'".     
23bb2 20 22 20 20 20 20 20 20 4f 52 20 28 74 79 70 65   "      OR (type
23bb3 3d 27 74 61 62 6c 65 27 20 41 4e 44 20 72 6f 6f  ='table' AND roo
23bb4 74 70 61 67 65 3d 30 29 22 0a 20 20 29 3b 0a 20  tpage=0)".  );. 
23bb5 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65   if( rc ) goto e
23bb6 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 20  nd_of_vacuum;.. 
23bb7 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e   /* At this poin
23bb8 74 2c 20 75 6e 6c 65 73 73 20 74 68 65 20 6d 61  t, unless the ma
23bb9 69 6e 20 64 62 20 77 61 73 20 63 6f 6d 70 6c 65  in db was comple
23bba 74 65 6c 79 20 65 6d 70 74 79 2c 20 74 68 65 72  tely empty, ther
23bbb 65 20 69 73 20 6e 6f 77 20 61 0a 20 20 2a 2a 20  e is now a.  ** 
23bbc 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e  transaction open
23bbd 20 6f 6e 20 74 68 65 20 76 61 63 75 75 6d 20 64   on the vacuum d
23bbe 61 74 61 62 61 73 65 2c 20 62 75 74 20 6e 6f 74  atabase, but not
23bbf 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74   on the main dat
23bc0 61 62 61 73 65 2e 0a 20 20 2a 2a 20 4f 70 65 6e  abase..  ** Open
23bc1 20 61 20 62 74 72 65 65 20 6c 65 76 65 6c 20 74   a btree level t
23bc2 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68  ransaction on th
23bc3 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e  e main database.
23bc4 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 61 0a 20   This allows a. 
23bc5 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69   ** call to sqli
23bc6 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c 65  te3BtreeCopyFile
23bc7 28 29 2e 20 54 68 65 20 6d 61 69 6e 20 64 61 74  (). The main dat
23bc8 61 62 61 73 65 20 62 74 72 65 65 20 6c 65 76 65  abase btree leve
23bc9 6c 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69  l.  ** transacti
23bca 6f 6e 20 69 73 20 74 68 65 6e 20 63 6f 6d 6d 69  on is then commi
23bcb 74 74 65 64 2c 20 73 6f 20 74 68 65 20 53 51 4c  tted, so the SQL
23bcc 20 6c 65 76 65 6c 20 6e 65 76 65 72 20 6b 6e 6f   level never kno
23bcd 77 73 20 69 74 20 77 61 73 0a 20 20 2a 2a 20 6f  ws it was.  ** o
23bce 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69 6e  pened for writin
23bcf 67 2e 20 54 68 69 73 20 77 61 79 2c 20 74 68 65  g. This way, the
23bd0 20 53 51 4c 20 74 72 61 6e 73 61 63 74 69 6f 6e   SQL transaction
23bd1 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20   used to create 
23bd2 74 68 65 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61  the.  ** tempora
23bd3 72 79 20 64 61 74 61 62 61 73 65 20 6e 65 76 65  ry database neve
23bd4 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20 63 6f  r needs to be co
23bd5 6d 6d 69 74 74 65 64 2e 0a 20 20 2a 2f 0a 20 20  mmitted..  */.  
23bd6 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
23bd7 4b 20 29 7b 0a 20 20 20 20 75 33 32 20 6d 65 74  K ){.    u32 met
23bd8 61 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 0a 20  a;.    int i;.. 
23bd9 20 20 20 2f 2a 20 54 68 69 73 20 61 72 72 61 79     /* This array
23bda 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69 63   determines whic
23bdb 68 20 6d 65 74 61 20 6d 65 74 61 20 76 61 6c 75  h meta meta valu
23bdc 65 73 20 61 72 65 20 70 72 65 73 65 72 76 65 64  es are preserved
23bdd 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 76   in the.    ** v
23bde 61 63 75 75 6d 2e 20 20 45 76 65 6e 20 65 6e 74  acuum.  Even ent
23bdf 72 69 65 73 20 61 72 65 20 74 68 65 20 6d 65 74  ries are the met
23be0 61 20 76 61 6c 75 65 20 6e 75 6d 62 65 72 20 61  a value number a
23be1 6e 64 20 6f 64 64 20 65 6e 74 72 69 65 73 0a 20  nd odd entries. 
23be2 20 20 20 2a 2a 20 61 72 65 20 61 6e 20 69 6e 63     ** are an inc
23be3 72 65 6d 65 6e 74 20 74 6f 20 61 70 70 6c 79 20  rement to apply 
23be4 74 6f 20 74 68 65 20 6d 65 74 61 20 76 61 6c 75  to the meta valu
23be5 65 20 61 66 74 65 72 20 74 68 65 20 76 61 63 75  e after the vacu
23be6 75 6d 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 69  um..    ** The i
23be7 6e 63 72 65 6d 65 6e 74 20 69 73 20 75 73 65 64  ncrement is used
23be8 20 74 6f 20 69 6e 63 72 65 61 73 65 20 74 68 65   to increase the
23be9 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 73   schema cookie s
23bea 6f 20 74 68 61 74 20 6f 74 68 65 72 0a 20 20 20  o that other.   
23beb 20 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20   ** connections 
23bec 74 6f 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  to the same data
23bed 62 61 73 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 74  base will know t
23bee 6f 20 72 65 72 65 61 64 20 74 68 65 20 73 63 68  o reread the sch
23bef 65 6d 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ema..    */.    
23bf0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
23bf1 69 67 6e 65 64 20 63 68 61 72 20 61 43 6f 70 79  igned char aCopy
23bf2 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 31 2c  [] = {.       1,
23bf3 20 31 2c 20 20 20 20 2f 2a 20 41 64 64 20 6f 6e   1,    /* Add on
23bf4 65 20 74 6f 20 74 68 65 20 6f 6c 64 20 73 63 68  e to the old sch
23bf5 65 6d 61 20 63 6f 6f 6b 69 65 20 2a 2f 0a 20 20  ema cookie */.  
23bf6 20 20 20 20 20 33 2c 20 30 2c 20 20 20 20 2f 2a       3, 0,    /*
23bf7 20 50 72 65 73 65 72 76 65 20 74 68 65 20 64 65   Preserve the de
23bf8 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 65  fault page cache
23bf9 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 20 20 20   size */.       
23bfa 35 2c 20 30 2c 20 20 20 20 2f 2a 20 50 72 65 73  5, 0,    /* Pres
23bfb 65 72 76 65 20 74 68 65 20 64 65 66 61 75 6c 74  erve the default
23bfc 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a   text encoding *
23bfd 2f 0a 20 20 20 20 20 20 20 36 2c 20 30 2c 20 20  /.       6, 0,  
23bfe 20 20 2f 2a 20 50 72 65 73 65 72 76 65 20 74 68    /* Preserve th
23bff 65 20 75 73 65 72 20 76 65 72 73 69 6f 6e 20 2a  e user version *
23c00 2f 0a 20 20 20 20 7d 3b 0a 0a 20 20 20 20 61 73  /.    };..    as
23c01 73 65 72 74 28 20 31 3d 3d 73 71 6c 69 74 65 33  sert( 1==sqlite3
23c02 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70  BtreeIsInTrans(p
23c03 54 65 6d 70 29 20 29 3b 0a 20 20 20 20 61 73 73  Temp) );.    ass
23c04 65 72 74 28 20 31 3d 3d 73 71 6c 69 74 65 33 42  ert( 1==sqlite3B
23c05 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 4d  treeIsInTrans(pM
23c06 61 69 6e 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  ain) );..    /* 
23c07 43 6f 70 79 20 42 74 72 65 65 20 6d 65 74 61 20  Copy Btree meta 
23c08 76 61 6c 75 65 73 20 2a 2f 0a 20 20 20 20 66 6f  values */.    fo
23c09 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28  r(i=0; i<sizeof(
23c0a 61 43 6f 70 79 29 2f 73 69 7a 65 6f 66 28 61 43  aCopy)/sizeof(aC
23c0b 6f 70 79 5b 30 5d 29 3b 20 69 2b 3d 32 29 7b 0a  opy[0]); i+=2){.
23c0c 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
23c0d 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 70  e3BtreeGetMeta(p
23c0e 4d 61 69 6e 2c 20 61 43 6f 70 79 5b 69 5d 2c 20  Main, aCopy[i], 
23c0f 26 6d 65 74 61 29 3b 0a 20 20 20 20 20 20 69 66  &meta);.      if
23c10 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
23c11 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61  ) goto end_of_va
23c12 63 75 75 6d 3b 0a 20 20 20 20 20 20 72 63 20 3d  cuum;.      rc =
23c13 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64   sqlite3BtreeUpd
23c14 61 74 65 4d 65 74 61 28 70 54 65 6d 70 2c 20 61  ateMeta(pTemp, a
23c15 43 6f 70 79 5b 69 5d 2c 20 6d 65 74 61 2b 61 43  Copy[i], meta+aC
23c16 6f 70 79 5b 69 2b 31 5d 29 3b 0a 20 20 20 20 20  opy[i+1]);.     
23c17 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
23c18 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
23c19 5f 76 61 63 75 75 6d 3b 0a 20 20 20 20 7d 0a 0a  _vacuum;.    }..
23c1a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
23c1b 42 74 72 65 65 43 6f 70 79 46 69 6c 65 28 70 4d  BtreeCopyFile(pM
23c1c 61 69 6e 2c 20 70 54 65 6d 70 29 3b 0a 20 20 20  ain, pTemp);.   
23c1d 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
23c1e 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
23c1f 5f 76 61 63 75 75 6d 3b 0a 20 20 20 20 72 63 20  _vacuum;.    rc 
23c20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  = sqlite3BtreeCo
23c21 6d 6d 69 74 28 70 54 65 6d 70 29 3b 0a 20 20 20  mmit(pTemp);.   
23c22 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
23c23 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
23c24 5f 76 61 63 75 75 6d 3b 0a 20 20 20 20 72 63 20  _vacuum;.    rc 
23c25 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  = sqlite3BtreeCo
23c26 6d 6d 69 74 28 70 4d 61 69 6e 29 3b 0a 20 20 7d  mmit(pMain);.  }
23c27 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
23c28 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20  TE_OK ){.    rc 
23c29 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  = sqlite3BtreeSe
23c2a 74 50 61 67 65 53 69 7a 65 28 70 4d 61 69 6e 2c  tPageSize(pMain,
23c2b 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
23c2c 50 61 67 65 53 69 7a 65 28 70 54 65 6d 70 29 2c  PageSize(pTemp),
23c2d 20 6e 52 65 73 29 3b 0a 20 20 7d 0a 0a 65 6e 64   nRes);.  }..end
23c2e 5f 6f 66 5f 76 61 63 75 75 6d 3a 0a 20 20 2f 2a  _of_vacuum:.  /*
23c2f 20 52 65 73 74 6f 72 65 20 74 68 65 20 6f 72 69   Restore the ori
23c30 67 69 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20 64  ginal value of d
23c31 62 2d 3e 66 6c 61 67 73 20 2a 2f 0a 20 20 64 62  b->flags */.  db
23c32 2d 3e 66 6c 61 67 73 20 3d 20 73 61 76 65 64 5f  ->flags = saved_
23c33 66 6c 61 67 73 3b 0a 0a 20 20 2f 2a 20 43 75 72  flags;..  /* Cur
23c34 72 65 6e 74 6c 79 20 74 68 65 72 65 20 69 73 20  rently there is 
23c35 61 6e 20 53 51 4c 20 6c 65 76 65 6c 20 74 72 61  an SQL level tra
23c36 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 20 6f 6e  nsaction open on
23c37 20 74 68 65 20 76 61 63 75 75 6d 0a 20 20 2a 2a   the vacuum.  **
23c38 20 64 61 74 61 62 61 73 65 2e 20 4e 6f 20 6c 6f   database. No lo
23c39 63 6b 73 20 61 72 65 20 68 65 6c 64 20 6f 6e 20  cks are held on 
23c3a 61 6e 79 20 6f 74 68 65 72 20 66 69 6c 65 73 20  any other files 
23c3b 28 73 69 6e 63 65 20 74 68 65 20 6d 61 69 6e 20  (since the main 
23c3c 66 69 6c 65 0a 20 20 2a 2a 20 77 61 73 20 63 6f  file.  ** was co
23c3d 6d 6d 69 74 74 65 64 20 61 74 20 74 68 65 20 62  mmitted at the b
23c3e 74 72 65 65 20 6c 65 76 65 6c 29 2e 20 53 6f 20  tree level). So 
23c3f 69 74 20 73 61 66 65 20 74 6f 20 65 6e 64 20 74  it safe to end t
23c40 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20  he transaction. 
23c41 20 2a 2a 20 62 79 20 6d 61 6e 75 61 6c 6c 79 20   ** by manually 
23c42 73 65 74 74 69 6e 67 20 74 68 65 20 61 75 74 6f  setting the auto
23c43 43 6f 6d 6d 69 74 20 66 6c 61 67 20 74 6f 20 74  Commit flag to t
23c44 72 75 65 20 61 6e 64 20 64 65 74 61 63 68 69 6e  rue and detachin
23c45 67 20 74 68 65 0a 20 20 2a 2a 20 76 61 63 75 75  g the.  ** vacuu
23c46 6d 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20  m database. The 
23c47 76 61 63 75 75 6d 5f 64 62 20 6a 6f 75 72 6e 61  vacuum_db journa
23c48 6c 20 66 69 6c 65 20 69 73 20 64 65 6c 65 74 65  l file is delete
23c49 64 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72  d when the pager
23c4a 0a 20 20 2a 2a 20 69 73 20 63 6c 6f 73 65 64 20  .  ** is closed 
23c4b 62 79 20 74 68 65 20 44 45 54 41 43 48 2e 0a 20  by the DETACH.. 
23c4c 20 2a 2f 0a 20 20 64 62 2d 3e 61 75 74 6f 43 6f   */.  db->autoCo
23c4d 6d 6d 69 74 20 3d 20 31 3b 0a 0a 20 20 69 66 28  mmit = 1;..  if(
23c4e 20 70 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69   pDb ){.    sqli
23c4f 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 70 44  te3BtreeClose(pD
23c50 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 70 44 62  b->pBt);.    pDb
23c51 2d 3e 70 42 74 20 3d 20 30 3b 0a 20 20 20 20 70  ->pBt = 0;.    p
23c52 44 62 2d 3e 70 53 63 68 65 6d 61 20 3d 20 30 3b  Db->pSchema = 0;
23c53 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 52  .  }..  sqlite3R
23c54 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65  esetInternalSche
23c55 6d 61 28 64 62 2c 20 30 29 3b 0a 0a 20 20 72 65  ma(db, 0);..  re
23c56 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69  turn rc;.}.#endi
23c57 66 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  f  /* SQLITE_OMI
23c58 54 5f 56 41 43 55 55 4d 20 26 26 20 53 51 4c 49  T_VACUUM && SQLI
23c59 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48 20 2a  TE_OMIT_ATTACH *
23c5a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
23c5b 2a 2a 20 45 6e 64 20 6f 66 20 76 61 63 75 75 6d  ** End of vacuum
23c5c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
23c5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23c5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23c5f 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
23c60 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 74  ** Begin file vt
23c61 61 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ab.c ***********
23c62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23c63 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23c64 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4a 75  */./*.** 2006 Ju
23c65 6e 65 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ne 10.**.** The 
23c66 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
23c67 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
23c68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
23c69 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
23c6a 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
23c6b 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
23c6c 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
23c6d 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
23c6e 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
23c6f 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
23c70 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
23c71 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
23c72 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
23c73 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
23c74 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
23c75 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
23c76 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
23c77 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23c78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23c79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23c7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23c7b 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
23c7c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
23c7d 75 73 65 64 20 74 6f 20 68 65 6c 70 20 69 6d 70  used to help imp
23c7e 6c 65 6d 65 6e 74 20 76 69 72 74 75 61 6c 20 74  lement virtual t
23c7f 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  ables..**.** $Id
23c80 3a 20 76 74 61 62 2e 63 2c 76 20 31 2e 36 39 20  : vtab.c,v 1.69 
23c81 32 30 30 38 2f 30 35 2f 30 35 20 31 33 3a 32 33  2008/05/05 13:23
23c82 3a 30 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :04 drh Exp $.*/
23c83 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
23c84 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
23c85 45 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 63 72  E..static int cr
23c86 65 61 74 65 4d 6f 64 75 6c 65 28 0a 20 20 73 71  eateModule(.  sq
23c87 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
23c88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
23c89 20 44 61 74 61 62 61 73 65 20 69 6e 20 77 68 69   Database in whi
23c8a 63 68 20 6d 6f 64 75 6c 65 20 69 73 20 72 65 67  ch module is reg
23c8b 69 73 74 65 72 65 64 20 2a 2f 0a 20 20 63 6f 6e  istered */.  con
23c8c 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
23c8d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
23c8e 4e 61 6d 65 20 61 73 73 69 67 6e 65 64 20 74 6f  Name assigned to
23c8f 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 2a 2f 0a   this module */.
23c90 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
23c91 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 2c  module *pModule,
23c92 20 20 2f 2a 20 54 68 65 20 64 65 66 69 6e 69 74    /* The definit
23c93 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c  ion of the modul
23c94 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75  e */.  void *pAu
23c95 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x,              
23c96 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78         /* Contex
23c97 74 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 78 43  t pointer for xC
23c98 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a  reate/xConnect *
23c99 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 73 74  /.  void (*xDest
23c9a 72 6f 79 29 28 76 6f 69 64 20 2a 29 20 20 20 20  roy)(void *)    
23c9b 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65      /* Module de
23c9c 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f  structor functio
23c9d 6e 20 2a 2f 0a 29 20 7b 0a 20 20 69 6e 74 20 72  n */.) {.  int r
23c9e 63 2c 20 6e 4e 61 6d 65 3b 0a 20 20 4d 6f 64 75  c, nName;.  Modu
23c9f 6c 65 20 2a 70 4d 6f 64 3b 0a 0a 20 20 73 71 6c  le *pMod;..  sql
23ca0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
23ca1 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 6e  (db->mutex);.  n
23ca2 4e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a 4e  Name = strlen(zN
23ca3 61 6d 65 29 3b 0a 20 20 70 4d 6f 64 20 3d 20 28  ame);.  pMod = (
23ca4 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65 33  Module *)sqlite3
23ca5 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
23ca6 73 69 7a 65 6f 66 28 4d 6f 64 75 6c 65 29 20 2b  sizeof(Module) +
23ca7 20 6e 4e 61 6d 65 20 2b 20 31 29 3b 0a 20 20 69   nName + 1);.  i
23ca8 66 28 20 70 4d 6f 64 20 29 7b 0a 20 20 20 20 63  f( pMod ){.    c
23ca9 68 61 72 20 2a 7a 43 6f 70 79 20 3d 20 28 63 68  har *zCopy = (ch
23caa 61 72 20 2a 29 28 26 70 4d 6f 64 5b 31 5d 29 3b  ar *)(&pMod[1]);
23cab 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 43 6f 70  .    memcpy(zCop
23cac 79 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b  y, zName, nName+
23cad 31 29 3b 0a 20 20 20 20 70 4d 6f 64 2d 3e 7a 4e  1);.    pMod->zN
23cae 61 6d 65 20 3d 20 7a 43 6f 70 79 3b 0a 20 20 20  ame = zCopy;.   
23caf 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 20 3d   pMod->pModule =
23cb0 20 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 70 4d   pModule;.    pM
23cb1 6f 64 2d 3e 70 41 75 78 20 3d 20 70 41 75 78 3b  od->pAux = pAux;
23cb2 0a 20 20 20 20 70 4d 6f 64 2d 3e 78 44 65 73 74  .    pMod->xDest
23cb3 72 6f 79 20 3d 20 78 44 65 73 74 72 6f 79 3b 0a  roy = xDestroy;.
23cb4 20 20 20 20 70 4d 6f 64 20 3d 20 28 4d 6f 64 75      pMod = (Modu
23cb5 6c 65 20 2a 29 73 71 6c 69 74 65 33 48 61 73 68  le *)sqlite3Hash
23cb6 49 6e 73 65 72 74 28 26 64 62 2d 3e 61 4d 6f 64  Insert(&db->aMod
23cb7 75 6c 65 2c 20 7a 43 6f 70 79 2c 20 6e 4e 61 6d  ule, zCopy, nNam
23cb8 65 2c 20 28 76 6f 69 64 2a 29 70 4d 6f 64 29 3b  e, (void*)pMod);
23cb9 0a 20 20 20 20 69 66 28 20 70 4d 6f 64 20 26 26  .    if( pMod &&
23cba 20 70 4d 6f 64 2d 3e 78 44 65 73 74 72 6f 79 20   pMod->xDestroy 
23cbb 29 7b 0a 20 20 20 20 20 20 70 4d 6f 64 2d 3e 78  ){.      pMod->x
23cbc 44 65 73 74 72 6f 79 28 70 4d 6f 64 2d 3e 70 41  Destroy(pMod->pA
23cbd 75 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ux);.    }.    s
23cbe 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4d 6f 64  qlite3_free(pMod
23cbf 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65  );.    sqlite3Re
23cc0 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d  setInternalSchem
23cc1 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  a(db, 0);.  }.  
23cc2 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45  rc = sqlite3ApiE
23cc3 78 69 74 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f  xit(db, SQLITE_O
23cc4 4b 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  K);.  sqlite3_mu
23cc5 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
23cc6 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  tex);.  return r
23cc7 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 78 74  c;.}.../*.** Ext
23cc8 65 72 6e 61 6c 20 41 50 49 20 66 75 6e 63 74 69  ernal API functi
23cc9 6f 6e 20 75 73 65 64 20 74 6f 20 63 72 65 61 74  on used to creat
23cca 65 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 2d  e a new virtual-
23ccb 74 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f  table module..*/
23ccc 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
23ccd 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
23cce 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 33  odule(.  sqlite3
23ccf 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
23cd0 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
23cd1 62 61 73 65 20 69 6e 20 77 68 69 63 68 20 6d 6f  base in which mo
23cd2 64 75 6c 65 20 69 73 20 72 65 67 69 73 74 65 72  dule is register
23cd3 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ed */.  const ch
23cd4 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20  ar *zName,      
23cd5 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
23cd6 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68 69 73  assigned to this
23cd7 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e   module */.  con
23cd8 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
23cd9 65 20 2a 70 4d 6f 64 75 6c 65 2c 20 20 2f 2a 20  e *pModule,  /* 
23cda 54 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f  The definition o
23cdb 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a  f the module */.
23cdc 20 20 76 6f 69 64 20 2a 70 41 75 78 20 20 20 20    void *pAux    
23cdd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23cde 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69    /* Context poi
23cdf 6e 74 65 72 20 66 6f 72 20 78 43 72 65 61 74 65  nter for xCreate
23ce0 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 29 7b 0a  /xConnect */.){.
23ce1 20 20 72 65 74 75 72 6e 20 63 72 65 61 74 65 4d    return createM
23ce2 6f 64 75 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c  odule(db, zName,
23ce3 20 70 4d 6f 64 75 6c 65 2c 20 70 41 75 78 2c 20   pModule, pAux, 
23ce4 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74  0);.}../*.** Ext
23ce5 65 72 6e 61 6c 20 41 50 49 20 66 75 6e 63 74 69  ernal API functi
23ce6 6f 6e 20 75 73 65 64 20 74 6f 20 63 72 65 61 74  on used to creat
23ce7 65 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 2d  e a new virtual-
23ce8 74 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f  table module..*/
23ce9 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
23cea 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
23ceb 6f 64 75 6c 65 5f 76 32 28 0a 20 20 73 71 6c 69  odule_v2(.  sqli
23cec 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
23ced 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
23cee 61 74 61 62 61 73 65 20 69 6e 20 77 68 69 63 68  atabase in which
23cef 20 6d 6f 64 75 6c 65 20 69 73 20 72 65 67 69 73   module is regis
23cf0 74 65 72 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74  tered */.  const
23cf1 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20   char *zName,   
23cf2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
23cf3 6d 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74  me assigned to t
23cf4 68 69 73 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20  his module */.  
23cf5 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
23cf6 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 2c 20 20  dule *pModule,  
23cf7 2f 2a 20 54 68 65 20 64 65 66 69 6e 69 74 69 6f  /* The definitio
23cf8 6e 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20  n of the module 
23cf9 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c  */.  void *pAux,
23cfa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23cfb 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20       /* Context 
23cfc 70 6f 69 6e 74 65 72 20 66 6f 72 20 78 43 72 65  pointer for xCre
23cfd 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a  ate/xConnect */.
23cfe 20 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f    void (*xDestro
23cff 79 29 28 76 6f 69 64 20 2a 29 20 20 20 20 20 20  y)(void *)      
23d00 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74    /* Module dest
23d01 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20  ructor function 
23d02 2a 2f 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 63  */.){.  return c
23d03 72 65 61 74 65 4d 6f 64 75 6c 65 28 64 62 2c 20  reateModule(db, 
23d04 7a 4e 61 6d 65 2c 20 70 4d 6f 64 75 6c 65 2c 20  zName, pModule, 
23d05 70 41 75 78 2c 20 78 44 65 73 74 72 6f 79 29 3b  pAux, xDestroy);
23d06 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74  .}../*.** Lock t
23d07 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
23d08 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 6e   so that it cann
23d09 6f 74 20 62 65 20 64 69 73 63 6f 6e 6e 65 63 74  ot be disconnect
23d0a 65 64 2e 0a 2a 2a 20 4c 6f 63 6b 73 20 6e 65 73  ed..** Locks nes
23d0b 74 2e 20 20 45 76 65 72 79 20 6c 6f 63 6b 20 73  t.  Every lock s
23d0c 68 6f 75 6c 64 20 68 61 76 65 20 61 20 63 6f 72  hould have a cor
23d0d 72 65 73 70 6f 6e 64 69 6e 67 20 75 6e 6c 6f 63  responding unloc
23d0e 6b 2e 0a 2a 2a 20 49 66 20 61 6e 20 75 6e 6c 6f  k..** If an unlo
23d0f 63 6b 20 69 73 20 6f 6d 69 74 74 65 64 2c 20 72  ck is omitted, r
23d10 65 73 6f 75 72 63 65 73 20 6c 65 61 6b 73 20 77  esources leaks w
23d11 69 6c 6c 20 6f 63 63 75 72 2e 20 20 0a 2a 2a 0a  ill occur.  .**.
23d12 2a 2a 20 49 66 20 61 20 64 69 73 63 6f 6e 6e 65  ** If a disconne
23d13 63 74 20 69 73 20 61 74 74 65 6d 70 74 65 64 20  ct is attempted 
23d14 77 68 69 6c 65 20 61 20 76 69 72 74 75 61 6c 20  while a virtual 
23d15 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 2c  table is locked,
23d16 0a 2a 2a 20 74 68 65 20 64 69 73 63 6f 6e 6e 65  .** the disconne
23d17 63 74 20 69 73 20 64 65 66 65 72 72 65 64 20 75  ct is deferred u
23d18 6e 74 69 6c 20 61 6c 6c 20 6c 6f 63 6b 73 20 68  ntil all locks h
23d19 61 76 65 20 62 65 65 6e 20 72 65 6d 6f 76 65 64  ave been removed
23d1a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
23d1b 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
23d1c 56 74 61 62 4c 6f 63 6b 28 73 71 6c 69 74 65 33  VtabLock(sqlite3
23d1d 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20  _vtab *pVtab){. 
23d1e 20 70 56 74 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a   pVtab->nRef++;.
23d1f 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b 20  }../*.** Unlock 
23d20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  a virtual table.
23d21 20 20 57 68 65 6e 20 74 68 65 20 6c 61 73 74 20    When the last 
23d22 6c 6f 63 6b 20 69 73 20 72 65 6d 6f 76 65 64 2c  lock is removed,
23d23 0a 2a 2a 20 64 69 73 63 6f 6e 6e 65 63 74 20 74  .** disconnect t
23d24 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
23d25 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
23d26 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
23d27 56 74 61 62 55 6e 6c 6f 63 6b 28 73 71 6c 69 74  VtabUnlock(sqlit
23d28 65 33 20 2a 64 62 2c 20 73 71 6c 69 74 65 33 5f  e3 *db, sqlite3_
23d29 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20  vtab *pVtab){.  
23d2a 70 56 74 61 62 2d 3e 6e 52 65 66 2d 2d 3b 0a 20  pVtab->nRef--;. 
23d2b 20 61 73 73 65 72 74 28 64 62 29 3b 0a 20 20 61   assert(db);.  a
23d2c 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53 61  ssert( sqlite3Sa
23d2d 66 65 74 79 43 68 65 63 6b 4f 6b 28 64 62 29 20  fetyCheckOk(db) 
23d2e 29 3b 0a 20 20 69 66 28 20 70 56 74 61 62 2d 3e  );.  if( pVtab->
23d2f 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 69  nRef==0 ){.    i
23d30 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51  f( db->magic==SQ
23d31 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20  LITE_MAGIC_BUSY 
23d32 29 7b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73  ){.      (void)s
23d33 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
23d34 64 62 29 3b 0a 20 20 20 20 20 20 70 56 74 61 62  db);.      pVtab
23d35 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44 69 73 63  ->pModule->xDisc
23d36 6f 6e 6e 65 63 74 28 70 56 74 61 62 29 3b 0a 20  onnect(pVtab);. 
23d37 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74       (void)sqlit
23d38 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a  e3SafetyOn(db);.
23d39 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20      } else {.   
23d3a 20 20 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c     pVtab->pModul
23d3b 65 2d 3e 78 44 69 73 63 6f 6e 6e 65 63 74 28 70  e->xDisconnect(p
23d3c 56 74 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Vtab);.    }.  }
23d3d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  .}../*.** Clear 
23d3e 61 6e 79 20 61 6e 64 20 61 6c 6c 20 76 69 72 74  any and all virt
23d3f 75 61 6c 2d 74 61 62 6c 65 20 69 6e 66 6f 72 6d  ual-table inform
23d40 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 54  ation from the T
23d41 61 62 6c 65 20 72 65 63 6f 72 64 2e 0a 2a 2a 20  able record..** 
23d42 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
23d43 63 61 6c 6c 65 64 2c 20 66 6f 72 20 65 78 61 6d  called, for exam
23d44 70 6c 65 2c 20 6a 75 73 74 20 62 65 66 6f 72 65  ple, just before
23d45 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 54 61   deleting the Ta
23d46 62 6c 65 0a 2a 2a 20 72 65 63 6f 72 64 2e 0a 2a  ble.** record..*
23d47 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
23d48 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61   void sqlite3Vta
23d49 62 43 6c 65 61 72 28 54 61 62 6c 65 20 2a 70 29  bClear(Table *p)
23d4a 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  {.  sqlite3_vtab
23d4b 20 2a 70 56 74 61 62 20 3d 20 70 2d 3e 70 56 74   *pVtab = p->pVt
23d4c 61 62 3b 0a 20 20 69 66 28 20 70 56 74 61 62 20  ab;.  if( pVtab 
23d4d 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
23d4e 2d 3e 70 4d 6f 64 20 26 26 20 70 2d 3e 70 4d 6f  ->pMod && p->pMo
23d4f 64 2d 3e 70 4d 6f 64 75 6c 65 20 29 3b 0a 20 20  d->pModule );.  
23d50 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c    sqlite3VtabUnl
23d51 6f 63 6b 28 70 2d 3e 70 53 63 68 65 6d 61 2d 3e  ock(p->pSchema->
23d52 64 62 2c 20 70 56 74 61 62 29 3b 0a 20 20 20 20  db, pVtab);.    
23d53 70 2d 3e 70 56 74 61 62 20 3d 20 30 3b 0a 20 20  p->pVtab = 0;.  
23d54 7d 0a 20 20 69 66 28 20 70 2d 3e 61 7a 4d 6f 64  }.  if( p->azMod
23d55 75 6c 65 41 72 67 20 29 7b 0a 20 20 20 20 69 6e  uleArg ){.    in
23d56 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  t i;.    for(i=0
23d57 3b 20 69 3c 70 2d 3e 6e 4d 6f 64 75 6c 65 41 72  ; i<p->nModuleAr
23d58 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  g; i++){.      s
23d59 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61  qlite3_free(p->a
23d5a 7a 4d 6f 64 75 6c 65 41 72 67 5b 69 5d 29 3b 0a  zModuleArg[i]);.
23d5b 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
23d5c 33 5f 66 72 65 65 28 70 2d 3e 61 7a 4d 6f 64 75  3_free(p->azModu
23d5d 6c 65 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  leArg);.  }.}../
23d5e 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 6d  *.** Add a new m
23d5f 6f 64 75 6c 65 20 61 72 67 75 6d 65 6e 74 20 74  odule argument t
23d60 6f 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75  o pTable->azModu
23d61 6c 65 41 72 67 5b 5d 2e 0a 2a 2a 20 54 68 65 20  leArg[]..** The 
23d62 73 74 72 69 6e 67 20 69 73 20 6e 6f 74 20 63 6f  string is not co
23d63 70 69 65 64 20 2d 20 74 68 65 20 70 6f 69 6e 74  pied - the point
23d64 65 72 20 69 73 20 73 74 6f 72 65 64 2e 20 20 54  er is stored.  T
23d65 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20 77 69 6c  he.** string wil
23d66 6c 20 62 65 20 66 72 65 65 64 20 61 75 74 6f 6d  l be freed autom
23d67 61 74 69 63 61 6c 6c 79 20 77 68 65 6e 20 74 68  atically when th
23d68 65 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 64 65  e table is.** de
23d69 6c 65 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  leted..*/.static
23d6a 20 76 6f 69 64 20 61 64 64 4d 6f 64 75 6c 65 41   void addModuleA
23d6b 72 67 75 6d 65 6e 74 28 73 71 6c 69 74 65 33 20  rgument(sqlite3 
23d6c 2a 64 62 2c 20 54 61 62 6c 65 20 2a 70 54 61 62  *db, Table *pTab
23d6d 6c 65 2c 20 63 68 61 72 20 2a 7a 41 72 67 29 7b  le, char *zArg){
23d6e 0a 20 20 69 6e 74 20 69 20 3d 20 70 54 61 62 6c  .  int i = pTabl
23d6f 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 2b 2b 3b  e->nModuleArg++;
23d70 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 20 3d 20  .  int nBytes = 
23d71 73 69 7a 65 6f 66 28 63 68 61 72 20 2a 29 2a 28  sizeof(char *)*(
23d72 31 2b 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c  1+pTable->nModul
23d73 65 41 72 67 29 3b 0a 20 20 63 68 61 72 20 2a 2a  eArg);.  char **
23d74 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 0a 20 20 61  azModuleArg;.  a
23d75 7a 4d 6f 64 75 6c 65 41 72 67 20 3d 20 73 71 6c  zModuleArg = sql
23d76 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62  ite3DbRealloc(db
23d77 2c 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75  , pTable->azModu
23d78 6c 65 41 72 67 2c 20 6e 42 79 74 65 73 29 3b 0a  leArg, nBytes);.
23d79 20 20 69 66 28 20 61 7a 4d 6f 64 75 6c 65 41 72    if( azModuleAr
23d7a 67 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  g==0 ){.    int 
23d7b 6a 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  j;.    for(j=0; 
23d7c 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  j<i; j++){.     
23d7d 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54   sqlite3_free(pT
23d7e 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72  able->azModuleAr
23d7f 67 5b 6a 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20  g[j]);.    }.   
23d80 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 41   sqlite3_free(zA
23d81 72 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  rg);.    sqlite3
23d82 5f 66 72 65 65 28 70 54 61 62 6c 65 2d 3e 61 7a  _free(pTable->az
23d83 4d 6f 64 75 6c 65 41 72 67 29 3b 0a 20 20 20 20  ModuleArg);.    
23d84 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c 65 41  pTable->nModuleA
23d85 72 67 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  rg = 0;.  }else{
23d86 0a 20 20 20 20 61 7a 4d 6f 64 75 6c 65 41 72 67  .    azModuleArg
23d87 5b 69 5d 20 3d 20 7a 41 72 67 3b 0a 20 20 20 20  [i] = zArg;.    
23d88 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 69 2b 31 5d  azModuleArg[i+1]
23d89 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 54 61 62   = 0;.  }.  pTab
23d8a 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 20  le->azModuleArg 
23d8b 3d 20 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 0a 7d  = azModuleArg;.}
23d8c 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73  ../*.** The pars
23d8d 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f  er calls this ro
23d8e 75 74 69 6e 65 20 77 68 65 6e 20 69 74 20 66 69  utine when it fi
23d8f 72 73 74 20 73 65 65 73 20 61 20 43 52 45 41 54  rst sees a CREAT
23d90 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 0a  E VIRTUAL TABLE.
23d91 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54  ** statement.  T
23d92 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 68  he module name h
23d93 61 73 20 62 65 65 6e 20 70 61 72 73 65 64 2c 20  as been parsed, 
23d94 62 75 74 20 74 68 65 20 6f 70 74 69 6f 6e 61 6c  but the optional
23d95 20 6c 69 73 74 0a 2a 2a 20 6f 66 20 70 61 72 61   list.** of para
23d96 6d 65 74 65 72 73 20 74 68 61 74 20 66 6f 6c 6c  meters that foll
23d97 6f 77 20 74 68 65 20 6d 6f 64 75 6c 65 20 6e 61  ow the module na
23d98 6d 65 20 61 72 65 20 73 74 69 6c 6c 20 70 65 6e  me are still pen
23d99 64 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ding..*/.SQLITE_
23d9a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
23d9b 69 74 65 33 56 74 61 62 42 65 67 69 6e 50 61 72  ite3VtabBeginPar
23d9c 73 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  se(.  Parse *pPa
23d9d 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  rse,        /* P
23d9e 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
23d9f 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  /.  Token *pName
23da0 31 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  1,        /* Nam
23da1 65 20 6f 66 20 6e 65 77 20 74 61 62 6c 65 2c 20  e of new table, 
23da2 6f 72 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  or database name
23da3 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
23da4 6d 65 32 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e  me2,        /* N
23da5 61 6d 65 20 6f 66 20 6e 65 77 20 74 61 62 6c 65  ame of new table
23da6 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f   or NULL */.  To
23da7 6b 65 6e 20 2a 70 4d 6f 64 75 6c 65 4e 61 6d 65  ken *pModuleName
23da8 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
23da9 68 65 20 6d 6f 64 75 6c 65 20 66 6f 72 20 74 68  he module for th
23daa 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
23dab 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b  */.){.  int iDb;
23dac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
23dad 20 54 68 65 20 64 61 74 61 62 61 73 65 20 74 68   The database th
23dae 65 20 74 61 62 6c 65 20 69 73 20 62 65 69 6e 67  e table is being
23daf 20 63 72 65 61 74 65 64 20 69 6e 20 2a 2f 0a 20   created in */. 
23db0 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b 20   Table *pTable; 
23db1 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65         /* The ne
23db2 77 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  w virtual table 
23db3 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
23db4 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61  ;          /* Da
23db5 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
23db6 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70 50 61 72  n */..  if( pPar
23db7 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20  se->db->flags & 
23db8 53 51 4c 49 54 45 5f 53 68 61 72 65 64 43 61 63  SQLITE_SharedCac
23db9 68 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  he ){.    sqlite
23dba 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
23dbb 2c 20 22 43 61 6e 6e 6f 74 20 75 73 65 20 76 69  , "Cannot use vi
23dbc 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 20  rtual tables in 
23dbd 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64  shared-cache mod
23dbe 65 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  e");.    return;
23dbf 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 53  .  }..  sqlite3S
23dc0 74 61 72 74 54 61 62 6c 65 28 70 50 61 72 73 65  tartTable(pParse
23dc1 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32  , pName1, pName2
23dc2 2c 20 30 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20  , 0, 0, 1, 0);. 
23dc3 20 70 54 61 62 6c 65 20 3d 20 70 50 61 72 73 65   pTable = pParse
23dc4 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69  ->pNewTable;.  i
23dc5 66 28 20 70 54 61 62 6c 65 3d 3d 30 20 7c 7c 20  f( pTable==0 || 
23dc6 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 20 72  pParse->nErr ) r
23dc7 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28  eturn;.  assert(
23dc8 20 30 3d 3d 70 54 61 62 6c 65 2d 3e 70 49 6e 64   0==pTable->pInd
23dc9 65 78 20 29 3b 0a 0a 20 20 64 62 20 3d 20 70 50  ex );..  db = pP
23dca 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 44 62 20  arse->db;.  iDb 
23dcb 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
23dcc 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 6c  oIndex(db, pTabl
23dcd 65 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61  e->pSchema);.  a
23dce 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 29 3b  ssert( iDb>=0 );
23dcf 0a 0a 20 20 70 54 61 62 6c 65 2d 3e 69 73 56 69  ..  pTable->isVi
23dd0 72 74 75 61 6c 20 3d 20 31 3b 0a 20 20 70 54 61  rtual = 1;.  pTa
23dd1 62 6c 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 20  ble->nModuleArg 
23dd2 3d 20 30 3b 0a 20 20 61 64 64 4d 6f 64 75 6c 65  = 0;.  addModule
23dd3 41 72 67 75 6d 65 6e 74 28 64 62 2c 20 70 54 61  Argument(db, pTa
23dd4 62 6c 65 2c 20 73 71 6c 69 74 65 33 4e 61 6d 65  ble, sqlite3Name
23dd5 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4d  FromToken(db, pM
23dd6 6f 64 75 6c 65 4e 61 6d 65 29 29 3b 0a 20 20 61  oduleName));.  a
23dd7 64 64 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74  ddModuleArgument
23dd8 28 64 62 2c 20 70 54 61 62 6c 65 2c 20 73 71 6c  (db, pTable, sql
23dd9 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
23dda 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
23ddb 61 6d 65 29 29 3b 0a 20 20 61 64 64 4d 6f 64 75  ame));.  addModu
23ddc 6c 65 41 72 67 75 6d 65 6e 74 28 64 62 2c 20 70  leArgument(db, p
23ddd 54 61 62 6c 65 2c 20 73 71 6c 69 74 65 33 44 62  Table, sqlite3Db
23dde 53 74 72 44 75 70 28 64 62 2c 20 70 54 61 62 6c  StrDup(db, pTabl
23ddf 65 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 70 50  e->zName));.  pP
23de0 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e  arse->sNameToken
23de1 2e 6e 20 3d 20 70 4d 6f 64 75 6c 65 4e 61 6d 65  .n = pModuleName
23de2 2d 3e 7a 20 2b 20 70 4d 6f 64 75 6c 65 4e 61 6d  ->z + pModuleNam
23de3 65 2d 3e 6e 20 2d 20 70 4e 61 6d 65 31 2d 3e 7a  e->n - pName1->z
23de4 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
23de5 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41  E_OMIT_AUTHORIZA
23de6 54 49 4f 4e 0a 20 20 2f 2a 20 43 72 65 61 74 69  TION.  /* Creati
23de7 6e 67 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  ng a virtual tab
23de8 6c 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 61  le invokes the a
23de9 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c  uthorization cal
23dea 6c 62 61 63 6b 20 74 77 69 63 65 2e 0a 20 20 2a  lback twice..  *
23deb 2a 20 54 68 65 20 66 69 72 73 74 20 69 6e 76 6f  * The first invo
23dec 63 61 74 69 6f 6e 2c 20 74 6f 20 6f 62 74 61 69  cation, to obtai
23ded 6e 20 70 65 72 6d 69 73 73 69 6f 6e 20 74 6f 20  n permission to 
23dee 49 4e 53 45 52 54 20 61 20 72 6f 77 20 69 6e 74  INSERT a row int
23def 6f 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74  o the.  ** sqlit
23df0 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2c 20  e_master table, 
23df1 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
23df2 20 6d 61 64 65 20 62 79 20 73 71 6c 69 74 65 33   made by sqlite3
23df3 53 74 61 72 74 54 61 62 6c 65 28 29 2e 0a 20 20  StartTable()..  
23df4 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 63 61  ** The second ca
23df5 6c 6c 2c 20 74 6f 20 6f 62 74 61 69 6e 20 70 65  ll, to obtain pe
23df6 72 6d 69 73 73 69 6f 6e 20 74 6f 20 63 72 65 61  rmission to crea
23df7 74 65 20 74 68 65 20 74 61 62 6c 65 2c 20 69 73  te the table, is
23df8 20 6d 61 64 65 20 6e 6f 77 2e 0a 20 20 2a 2f 0a   made now..  */.
23df9 20 20 69 66 28 20 70 54 61 62 6c 65 2d 3e 61 7a    if( pTable->az
23dfa 4d 6f 64 75 6c 65 41 72 67 20 29 7b 0a 20 20 20  ModuleArg ){.   
23dfb 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
23dfc 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
23dfd 5f 43 52 45 41 54 45 5f 56 54 41 42 4c 45 2c 20  _CREATE_VTABLE, 
23dfe 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 2c 20 0a  pTable->zName, .
23dff 20 20 20 20 20 20 20 20 20 20 20 20 70 54 61 62              pTab
23e00 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b  le->azModuleArg[
23e01 30 5d 2c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  0], pParse->db->
23e02 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b  aDb[iDb].zName);
23e03 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  .  }.#endif.}../
23e04 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
23e05 65 20 74 61 6b 65 73 20 74 68 65 20 6d 6f 64 75  e takes the modu
23e06 6c 65 20 61 72 67 75 6d 65 6e 74 20 74 68 61 74  le argument that
23e07 20 68 61 73 20 62 65 65 6e 20 61 63 63 75 6d 75   has been accumu
23e08 6c 61 74 69 6e 67 0a 2a 2a 20 69 6e 20 70 50 61  lating.** in pPa
23e09 72 73 65 2d 3e 7a 41 72 67 5b 5d 20 61 6e 64 20  rse->zArg[] and 
23e0a 61 70 70 65 6e 64 73 20 69 74 20 74 6f 20 74 68  appends it to th
23e0b 65 20 6c 69 73 74 20 6f 66 20 61 72 67 75 6d 65  e list of argume
23e0c 6e 74 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 76 69  nts on the.** vi
23e0d 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75 72 72  rtual table curr
23e0e 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73  ently under cons
23e0f 74 72 75 63 74 69 6f 6e 20 69 6e 20 70 50 61 72  truction in pPar
23e10 73 65 2d 3e 70 54 61 62 6c 65 2e 0a 2a 2f 0a 73  se->pTable..*/.s
23e11 74 61 74 69 63 20 76 6f 69 64 20 61 64 64 41 72  tatic void addAr
23e12 67 75 6d 65 6e 74 54 6f 56 74 61 62 28 50 61 72  gumentToVtab(Par
23e13 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 69  se *pParse){.  i
23e14 66 28 20 70 50 61 72 73 65 2d 3e 73 41 72 67 2e  f( pParse->sArg.
23e15 7a 20 26 26 20 70 50 61 72 73 65 2d 3e 70 4e 65  z && pParse->pNe
23e16 77 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 63 6f  wTable ){.    co
23e17 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 28 63  nst char *z = (c
23e18 6f 6e 73 74 20 63 68 61 72 2a 29 70 50 61 72 73  onst char*)pPars
23e19 65 2d 3e 73 41 72 67 2e 7a 3b 0a 20 20 20 20 69  e->sArg.z;.    i
23e1a 6e 74 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 73  nt n = pParse->s
23e1b 41 72 67 2e 6e 3b 0a 20 20 20 20 73 71 6c 69 74  Arg.n;.    sqlit
23e1c 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
23e1d 3e 64 62 3b 0a 20 20 20 20 61 64 64 4d 6f 64 75  >db;.    addModu
23e1e 6c 65 41 72 67 75 6d 65 6e 74 28 64 62 2c 20 70  leArgument(db, p
23e1f 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
23e20 2c 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44  , sqlite3DbStrND
23e21 75 70 28 64 62 2c 20 7a 2c 20 6e 29 29 3b 0a 20  up(db, z, n));. 
23e22 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20   }.}../*.** The 
23e23 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69  parser calls thi
23e24 73 20 72 6f 75 74 69 6e 65 20 61 66 74 65 72 20  s routine after 
23e25 74 68 65 20 43 52 45 41 54 45 20 56 49 52 54 55  the CREATE VIRTU
23e26 41 4c 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  AL TABLE stateme
23e27 6e 74 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 63  nt.** has been c
23e28 6f 6d 70 6c 65 74 65 6c 79 20 70 61 72 73 65 64  ompletely parsed
23e29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
23e2a 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
23e2b 56 74 61 62 46 69 6e 69 73 68 50 61 72 73 65 28  VtabFinishParse(
23e2c 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
23e2d 6f 6b 65 6e 20 2a 70 45 6e 64 29 7b 0a 20 20 54  oken *pEnd){.  T
23e2e 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20  able *pTab;     
23e2f 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
23e30 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65  being constructe
23e31 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  d */.  sqlite3 *
23e32 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68  db;        /* Th
23e33 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
23e34 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72 20  ction */.  char 
23e35 2a 7a 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20 2f  *zModule;      /
23e36 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d  * The module nam
23e37 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 3a 20  e of the table: 
23e38 55 53 49 4e 47 20 6d 6f 64 75 6c 65 6e 61 6d 65  USING modulename
23e39 20 2a 2f 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d   */.  Module *pM
23e3a 6f 64 20 3d 20 30 3b 0a 0a 20 20 61 64 64 41 72  od = 0;..  addAr
23e3b 67 75 6d 65 6e 74 54 6f 56 74 61 62 28 70 50 61  gumentToVtab(pPa
23e3c 72 73 65 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  rse);.  pParse->
23e3d 73 41 72 67 2e 7a 20 3d 20 30 3b 0a 0a 20 20 2f  sArg.z = 0;..  /
23e3e 2a 20 4c 6f 6f 6b 75 70 20 74 68 65 20 6d 6f 64  * Lookup the mod
23e3f 75 6c 65 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 70  ule name. */.  p
23e40 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e  Tab = pParse->pN
23e41 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 70  ewTable;.  if( p
23e42 54 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  Tab==0 ) return;
23e43 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  .  db = pParse->
23e44 64 62 3b 0a 20 20 69 66 28 20 70 54 61 62 2d 3e  db;.  if( pTab->
23e45 6e 4d 6f 64 75 6c 65 41 72 67 3c 31 20 29 20 72  nModuleArg<1 ) r
23e46 65 74 75 72 6e 3b 0a 20 20 7a 4d 6f 64 75 6c 65  eturn;.  zModule
23e47 20 3d 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c   = pTab->azModul
23e48 65 41 72 67 5b 30 5d 3b 0a 20 20 70 4d 6f 64 20  eArg[0];.  pMod 
23e49 3d 20 28 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69  = (Module *)sqli
23e4a 74 65 33 48 61 73 68 46 69 6e 64 28 26 64 62 2d  te3HashFind(&db-
23e4b 3e 61 4d 6f 64 75 6c 65 2c 20 7a 4d 6f 64 75 6c  >aModule, zModul
23e4c 65 2c 20 73 74 72 6c 65 6e 28 7a 4d 6f 64 75 6c  e, strlen(zModul
23e4d 65 29 29 3b 0a 20 20 70 54 61 62 2d 3e 70 4d 6f  e));.  pTab->pMo
23e4e 64 20 3d 20 70 4d 6f 64 3b 0a 20 20 0a 20 20 2f  d = pMod;.  .  /
23e4f 2a 20 49 66 20 74 68 65 20 43 52 45 41 54 45 20  * If the CREATE 
23e50 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74  VIRTUAL TABLE st
23e51 61 74 65 6d 65 6e 74 20 69 73 20 62 65 69 6e 67  atement is being
23e52 20 65 6e 74 65 72 65 64 20 66 6f 72 20 74 68 65   entered for the
23e53 0a 20 20 2a 2a 20 66 69 72 73 74 20 74 69 6d 65  .  ** first time
23e54 20 28 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73   (in other words
23e55 20 69 66 20 74 68 65 20 76 69 72 74 75 61 6c 20   if the virtual 
23e56 74 61 62 6c 65 20 69 73 20 61 63 74 75 61 6c 6c  table is actuall
23e57 79 20 62 65 69 6e 67 0a 20 20 2a 2a 20 63 72 65  y being.  ** cre
23e58 61 74 65 64 20 6e 6f 77 20 69 6e 73 74 65 61 64  ated now instead
23e59 20 6f 66 20 6a 75 73 74 20 62 65 69 6e 67 20 72   of just being r
23e5a 65 61 64 20 6f 75 74 20 6f 66 20 73 71 6c 69 74  ead out of sqlit
23e5b 65 5f 6d 61 73 74 65 72 29 20 74 68 65 6e 0a 20  e_master) then. 
23e5c 20 2a 2a 20 64 6f 20 61 64 64 69 74 69 6f 6e 61   ** do additiona
23e5d 6c 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  l initialization
23e5e 20 77 6f 72 6b 20 61 6e 64 20 73 74 6f 72 65 20   work and store 
23e5f 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 65  the statement te
23e60 78 74 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 73  xt.  ** in the s
23e61 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
23e62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  le..  */.  if( !
23e63 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b  db->init.busy ){
23e64 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 74 6d 74  .    char *zStmt
23e65 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 57 68 65  ;.    char *zWhe
23e66 72 65 3b 0a 20 20 20 20 69 6e 74 20 69 44 62 3b  re;.    int iDb;
23e67 0a 20 20 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20  .    Vdbe *v;.. 
23e68 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68     /* Compute th
23e69 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20  e complete text 
23e6a 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 56 49  of the CREATE VI
23e6b 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74  RTUAL TABLE stat
23e6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 69 66 28  ement */.    if(
23e6d 20 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 70   pEnd ){.      p
23e6e 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65  Parse->sNameToke
23e6f 6e 2e 6e 20 3d 20 70 45 6e 64 2d 3e 7a 20 2d 20  n.n = pEnd->z - 
23e70 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b  pParse->sNameTok
23e71 65 6e 2e 7a 20 2b 20 70 45 6e 64 2d 3e 6e 3b 0a  en.z + pEnd->n;.
23e72 20 20 20 20 7d 0a 20 20 20 20 7a 53 74 6d 74 20      }.    zStmt 
23e73 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
23e74 28 64 62 2c 20 22 43 52 45 41 54 45 20 56 49 52  (db, "CREATE VIR
23e75 54 55 41 4c 20 54 41 42 4c 45 20 25 54 22 2c 20  TUAL TABLE %T", 
23e76 26 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f  &pParse->sNameTo
23e77 6b 65 6e 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 20  ken);..    /* A 
23e78 73 6c 6f 74 20 66 6f 72 20 74 68 65 20 72 65 63  slot for the rec
23e79 6f 72 64 20 68 61 73 20 61 6c 72 65 61 64 79 20  ord has already 
23e7a 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 69  been allocated i
23e7b 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 53 51  n the .    ** SQ
23e7c 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c  LITE_MASTER tabl
23e7d 65 2e 20 20 57 65 20 6a 75 73 74 20 6e 65 65 64  e.  We just need
23e7e 20 74 6f 20 75 70 64 61 74 65 20 74 68 61 74 20   to update that 
23e7f 73 6c 6f 74 20 77 69 74 68 20 61 6c 6c 0a 20 20  slot with all.  
23e80 20 20 2a 2a 20 74 68 65 20 69 6e 66 6f 72 6d 61    ** the informa
23e81 74 69 6f 6e 20 77 65 27 76 65 20 63 6f 6c 6c 65  tion we've colle
23e82 63 74 65 64 2e 20 20 0a 20 20 20 20 2a 2a 0a 20  cted.  .    **. 
23e83 20 20 20 2a 2a 20 54 68 65 20 56 4d 20 72 65 67     ** The VM reg
23e84 69 73 74 65 72 20 6e 75 6d 62 65 72 20 70 50 61  ister number pPa
23e85 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 20 68 6f  rse->regRowid ho
23e86 6c 64 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66  lds the rowid of
23e87 20 61 6e 0a 20 20 20 20 2a 2a 20 65 6e 74 72 79   an.    ** entry
23e88 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d   in the sqlite_m
23e89 61 73 74 65 72 20 74 61 62 6c 65 20 74 68 74 20  aster table tht 
23e8a 77 61 73 20 63 72 65 61 74 65 64 20 66 6f 72 20  was created for 
23e8b 74 68 69 73 20 76 74 61 62 0a 20 20 20 20 2a 2a  this vtab.    **
23e8c 20 62 79 20 73 71 6c 69 74 65 33 53 74 61 72 74   by sqlite3Start
23e8d 54 61 62 6c 65 28 29 2e 0a 20 20 20 20 2a 2f 0a  Table()..    */.
23e8e 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65      iDb = sqlite
23e8f 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
23e90 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
23e91 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65  );.    sqlite3Ne
23e92 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
23e93 2c 0a 20 20 20 20 20 20 22 55 50 44 41 54 45 20  ,.      "UPDATE 
23e94 25 51 2e 25 73 20 22 0a 20 20 20 20 20 20 20 20  %Q.%s ".        
23e95 20 22 53 45 54 20 74 79 70 65 3d 27 74 61 62 6c   "SET type='tabl
23e96 65 27 2c 20 6e 61 6d 65 3d 25 51 2c 20 74 62 6c  e', name=%Q, tbl
23e97 5f 6e 61 6d 65 3d 25 51 2c 20 72 6f 6f 74 70 61  _name=%Q, rootpa
23e98 67 65 3d 30 2c 20 73 71 6c 3d 25 51 20 22 0a 20  ge=0, sql=%Q ". 
23e99 20 20 20 20 20 20 22 57 48 45 52 45 20 72 6f 77        "WHERE row
23e9a 69 64 3d 23 25 64 22 2c 0a 20 20 20 20 20 20 64  id=#%d",.      d
23e9b 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
23e9c 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28  e, SCHEMA_TABLE(
23e9d 69 44 62 29 2c 0a 20 20 20 20 20 20 70 54 61 62  iDb),.      pTab
23e9e 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 70  ->zName,.      p
23e9f 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20  Tab->zName,.    
23ea0 20 20 7a 53 74 6d 74 2c 0a 20 20 20 20 20 20 70    zStmt,.      p
23ea1 50 61 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 0a  Parse->regRowid.
23ea2 20 20 20 20 29 3b 0a 20 20 20 20 73 71 6c 69 74      );.    sqlit
23ea3 65 33 5f 66 72 65 65 28 7a 53 74 6d 74 29 3b 0a  e3_free(zStmt);.
23ea4 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47      v = sqlite3G
23ea5 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
23ea6 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67      sqlite3Chang
23ea7 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20  eCookie(pParse, 
23ea8 69 44 62 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74  iDb);..    sqlit
23ea9 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
23eaa 4f 50 5f 45 78 70 69 72 65 2c 20 30 2c 20 30 29  OP_Expire, 0, 0)
23eab 3b 0a 20 20 20 20 7a 57 68 65 72 65 20 3d 20 73  ;.    zWhere = s
23eac 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
23ead 2c 20 22 6e 61 6d 65 3d 27 25 71 27 22 2c 20 70  , "name='%q'", p
23eae 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Tab->zName);.   
23eaf 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
23eb0 70 34 28 76 2c 20 4f 50 5f 50 61 72 73 65 53 63  p4(v, OP_ParseSc
23eb1 68 65 6d 61 2c 20 69 44 62 2c 20 31 2c 20 30 2c  hema, iDb, 1, 0,
23eb2 20 7a 57 68 65 72 65 2c 20 50 34 5f 44 59 4e 41   zWhere, P4_DYNA
23eb3 4d 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  MIC);.    sqlite
23eb4 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
23eb5 50 5f 56 43 72 65 61 74 65 2c 20 69 44 62 2c 20  P_VCreate, iDb, 
23eb6 30 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20  0, 0, .         
23eb7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23eb8 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 73 74 72  pTab->zName, str
23eb9 6c 65 6e 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  len(pTab->zName)
23eba 20 2b 20 31 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a   + 1);.  }..  /*
23ebb 20 49 66 20 77 65 20 61 72 65 20 72 65 72 65 61   If we are rerea
23ebc 64 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 5f  ding the sqlite_
23ebd 6d 61 73 74 65 72 20 74 61 62 6c 65 20 63 72 65  master table cre
23ebe 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72  ate the in-memor
23ebf 79 0a 20 20 2a 2a 20 72 65 63 6f 72 64 20 6f 66  y.  ** record of
23ec0 20 74 68 65 20 74 61 62 6c 65 2e 20 49 66 20 74   the table. If t
23ec1 68 65 20 6d 6f 64 75 6c 65 20 68 61 73 20 61 6c  he module has al
23ec2 72 65 61 64 79 20 62 65 65 6e 20 72 65 67 69 73  ready been regis
23ec3 74 65 72 65 64 2c 0a 20 20 2a 2a 20 61 6c 73 6f  tered,.  ** also
23ec4 20 63 61 6c 6c 20 74 68 65 20 78 43 6f 6e 6e 65   call the xConne
23ec5 63 74 20 6d 65 74 68 6f 64 20 68 65 72 65 2e 0a  ct method here..
23ec6 20 20 2a 2f 0a 20 20 65 6c 73 65 20 7b 0a 20 20    */.  else {.  
23ec7 20 20 54 61 62 6c 65 20 2a 70 4f 6c 64 3b 0a 20    Table *pOld;. 
23ec8 20 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65     Schema *pSche
23ec9 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65  ma = pTab->pSche
23eca 6d 61 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  ma;.    const ch
23ecb 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 54 61 62  ar *zName = pTab
23ecc 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 69 6e 74  ->zName;.    int
23ecd 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28   nName = strlen(
23ece 7a 4e 61 6d 65 29 20 2b 20 31 3b 0a 20 20 20 20  zName) + 1;.    
23ecf 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33 48 61  pOld = sqlite3Ha
23ed0 73 68 49 6e 73 65 72 74 28 26 70 53 63 68 65 6d  shInsert(&pSchem
23ed1 61 2d 3e 74 62 6c 48 61 73 68 2c 20 7a 4e 61 6d  a->tblHash, zNam
23ed2 65 2c 20 6e 4e 61 6d 65 2c 20 70 54 61 62 29 3b  e, nName, pTab);
23ed3 0a 20 20 20 20 69 66 28 20 70 4f 6c 64 20 29 7b  .    if( pOld ){
23ed4 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  .      db->mallo
23ed5 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
23ed6 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 3d     assert( pTab=
23ed7 3d 70 4f 6c 64 20 29 3b 20 20 2f 2a 20 4d 61 6c  =pOld );  /* Mal
23ed8 6c 6f 63 20 6d 75 73 74 20 68 61 76 65 20 66 61  loc must have fa
23ed9 69 6c 65 64 20 69 6e 73 69 64 65 20 48 61 73 68  iled inside Hash
23eda 49 6e 73 65 72 74 28 29 20 2a 2f 0a 20 20 20 20  Insert() */.    
23edb 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
23edc 20 20 20 20 70 53 63 68 65 6d 61 2d 3e 64 62 20      pSchema->db 
23edd 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
23ede 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61    pParse->pNewTa
23edf 62 6c 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a  ble = 0;.  }.}..
23ee0 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72  /*.** The parser
23ee1 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74   calls this rout
23ee2 69 6e 65 20 77 68 65 6e 20 69 74 20 73 65 65 73  ine when it sees
23ee3 20 74 68 65 20 66 69 72 73 74 20 74 6f 6b 65 6e   the first token
23ee4 0a 2a 2a 20 6f 66 20 61 6e 20 61 72 67 75 6d 65  .** of an argume
23ee5 6e 74 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65  nt to the module
23ee6 20 6e 61 6d 65 20 69 6e 20 61 20 43 52 45 41 54   name in a CREAT
23ee7 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20  E VIRTUAL TABLE 
23ee8 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51  statement..*/.SQ
23ee9 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
23eea 64 20 73 71 6c 69 74 65 33 56 74 61 62 41 72 67  d sqlite3VtabArg
23eeb 49 6e 69 74 28 50 61 72 73 65 20 2a 70 50 61 72  Init(Parse *pPar
23eec 73 65 29 7b 0a 20 20 61 64 64 41 72 67 75 6d 65  se){.  addArgume
23eed 6e 74 54 6f 56 74 61 62 28 70 50 61 72 73 65 29  ntToVtab(pParse)
23eee 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73 41 72 67  ;.  pParse->sArg
23eef 2e 7a 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65  .z = 0;.  pParse
23ef0 2d 3e 73 41 72 67 2e 6e 20 3d 20 30 3b 0a 7d 0a  ->sArg.n = 0;.}.
23ef1 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65  ./*.** The parse
23ef2 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  r calls this rou
23ef3 74 69 6e 65 20 66 6f 72 20 65 61 63 68 20 74 6f  tine for each to
23ef4 6b 65 6e 20 61 66 74 65 72 20 74 68 65 20 66 69  ken after the fi
23ef5 72 73 74 20 74 6f 6b 65 6e 0a 2a 2a 20 69 6e 20  rst token.** in 
23ef6 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  an argument to t
23ef7 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 69  he module name i
23ef8 6e 20 61 20 43 52 45 41 54 45 20 56 49 52 54 55  n a CREATE VIRTU
23ef9 41 4c 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  AL TABLE stateme
23efa 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nt..*/.SQLITE_PR
23efb 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
23efc 65 33 56 74 61 62 41 72 67 45 78 74 65 6e 64 28  e3VtabArgExtend(
23efd 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
23efe 6f 6b 65 6e 20 2a 70 29 7b 0a 20 20 54 6f 6b 65  oken *p){.  Toke
23eff 6e 20 2a 70 41 72 67 20 3d 20 26 70 50 61 72 73  n *pArg = &pPars
23f00 65 2d 3e 73 41 72 67 3b 0a 20 20 69 66 28 20 70  e->sArg;.  if( p
23f01 41 72 67 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20  Arg->z==0 ){.   
23f02 20 70 41 72 67 2d 3e 7a 20 3d 20 70 2d 3e 7a 3b   pArg->z = p->z;
23f03 0a 20 20 20 20 70 41 72 67 2d 3e 6e 20 3d 20 70  .    pArg->n = p
23f04 2d 3e 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ->n;.  }else{.  
23f05 20 20 61 73 73 65 72 74 28 70 41 72 67 2d 3e 7a    assert(pArg->z
23f06 20 3c 20 70 2d 3e 7a 29 3b 0a 20 20 20 20 70 41   < p->z);.    pA
23f07 72 67 2d 3e 6e 20 3d 20 28 70 2d 3e 7a 20 2b 20  rg->n = (p->z + 
23f08 70 2d 3e 6e 20 2d 20 70 41 72 67 2d 3e 7a 29 3b  p->n - pArg->z);
23f09 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  .  }.}../*.** In
23f0a 76 6f 6b 65 20 61 20 76 69 72 74 75 61 6c 20 74  voke a virtual t
23f0b 61 62 6c 65 20 63 6f 6e 73 74 72 75 63 74 6f 72  able constructor
23f0c 20 28 65 69 74 68 65 72 20 78 43 72 65 61 74 65   (either xCreate
23f0d 20 6f 72 20 78 43 6f 6e 6e 65 63 74 29 2e 20 54   or xConnect). T
23f0e 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f  he.** pointer to
23f0f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f   the function to
23f10 20 69 6e 76 6f 6b 65 20 69 73 20 70 61 73 73 65   invoke is passe
23f11 64 20 61 73 20 74 68 65 20 66 6f 75 72 74 68 20  d as the fourth 
23f12 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74 6f 20  parameter.** to 
23f13 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 2e 0a  this procedure..
23f14 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 74  */.static int vt
23f15 61 62 43 61 6c 6c 43 6f 6e 73 74 72 75 63 74 6f  abCallConstructo
23f16 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  r(.  sqlite3 *db
23f17 2c 20 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  , .  Table *pTab
23f18 2c 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64  ,.  Module *pMod
23f19 2c 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e 73 74  ,.  int (*xConst
23f1a 72 75 63 74 29 28 73 71 6c 69 74 65 33 2a 2c 76  ruct)(sqlite3*,v
23f1b 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63  oid*,int,const c
23f1c 68 61 72 2a 63 6f 6e 73 74 2a 2c 73 71 6c 69 74  har*const*,sqlit
23f1d 65 33 5f 76 74 61 62 2a 2a 2c 63 68 61 72 2a 2a  e3_vtab**,char**
23f1e 29 2c 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72  ),.  char **pzEr
23f1f 72 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  r.){.  int rc;. 
23f20 20 69 6e 74 20 72 63 32 3b 0a 20 20 73 71 6c 69   int rc2;.  sqli
23f21 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 20  te3_vtab *pVtab 
23f22 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  = 0;.  const cha
23f23 72 20 2a 63 6f 6e 73 74 2a 61 7a 41 72 67 20 3d  r *const*azArg =
23f24 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f   (const char *co
23f25 6e 73 74 2a 29 70 54 61 62 2d 3e 61 7a 4d 6f 64  nst*)pTab->azMod
23f26 75 6c 65 41 72 67 3b 0a 20 20 69 6e 74 20 6e 41  uleArg;.  int nA
23f27 72 67 20 3d 20 70 54 61 62 2d 3e 6e 4d 6f 64 75  rg = pTab->nModu
23f28 6c 65 41 72 67 3b 0a 20 20 63 68 61 72 20 2a 7a  leArg;.  char *z
23f29 45 72 72 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  Err = 0;.  char 
23f2a 2a 7a 4d 6f 64 75 6c 65 4e 61 6d 65 20 3d 20 73  *zModuleName = s
23f2b 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
23f2c 2c 20 22 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e  , "%s", pTab->zN
23f2d 61 6d 65 29 3b 0a 0a 20 20 69 66 28 20 21 7a 4d  ame);..  if( !zM
23f2e 6f 64 75 6c 65 4e 61 6d 65 20 29 7b 0a 20 20 20  oduleName ){.   
23f2f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
23f30 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  OMEM;.  }..  ass
23f31 65 72 74 28 20 21 64 62 2d 3e 70 56 54 61 62 20  ert( !db->pVTab 
23f32 29 3b 0a 20 20 61 73 73 65 72 74 28 20 78 43 6f  );.  assert( xCo
23f33 6e 73 74 72 75 63 74 20 29 3b 0a 0a 20 20 64 62  nstruct );..  db
23f34 2d 3e 70 56 54 61 62 20 3d 20 70 54 61 62 3b 0a  ->pVTab = pTab;.
23f35 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 61    rc = sqlite3Sa
23f36 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 61  fetyOff(db);.  a
23f37 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
23f38 45 5f 4f 4b 20 29 3b 0a 20 20 72 63 20 3d 20 78  E_OK );.  rc = x
23f39 43 6f 6e 73 74 72 75 63 74 28 64 62 2c 20 70 4d  Construct(db, pM
23f3a 6f 64 2d 3e 70 41 75 78 2c 20 6e 41 72 67 2c 20  od->pAux, nArg, 
23f3b 61 7a 41 72 67 2c 20 26 70 56 74 61 62 2c 20 26  azArg, &pVtab, &
23f3c 7a 45 72 72 29 3b 0a 20 20 72 63 32 20 3d 20 73  zErr);.  rc2 = s
23f3d 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64  qlite3SafetyOn(d
23f3e 62 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  b);.  if( rc==SQ
23f3f 4c 49 54 45 5f 4f 4b 20 26 26 20 70 56 74 61 62  LITE_OK && pVtab
23f40 20 29 7b 0a 20 20 20 20 70 56 74 61 62 2d 3e 70   ){.    pVtab->p
23f41 4d 6f 64 75 6c 65 20 3d 20 70 4d 6f 64 2d 3e 70  Module = pMod->p
23f42 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 70 56 74 61  Module;.    pVta
23f43 62 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20  b->nRef = 1;.   
23f44 20 70 54 61 62 2d 3e 70 56 74 61 62 20 3d 20 70   pTab->pVtab = p
23f45 56 74 61 62 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  Vtab;.  }..  if(
23f46 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 29   SQLITE_OK!=rc )
23f47 7b 0a 20 20 20 20 69 66 28 20 7a 45 72 72 3d 3d  {.    if( zErr==
23f48 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72  0 ){.      *pzEr
23f49 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  r = sqlite3MPrin
23f4a 74 66 28 64 62 2c 20 22 76 74 61 62 6c 65 20 63  tf(db, "vtable c
23f4b 6f 6e 73 74 72 75 63 74 6f 72 20 66 61 69 6c 65  onstructor faile
23f4c 64 3a 20 25 73 22 2c 20 7a 4d 6f 64 75 6c 65 4e  d: %s", zModuleN
23f4d 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  ame);.    }else 
23f4e 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 20 3d  {.      *pzErr =
23f4f 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
23f50 64 62 2c 20 22 25 73 22 2c 20 7a 45 72 72 29 3b  db, "%s", zErr);
23f51 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
23f52 72 65 65 28 7a 45 72 72 29 3b 0a 20 20 20 20 7d  ree(zErr);.    }
23f53 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 64 62 2d  .  }else if( db-
23f54 3e 70 56 54 61 62 20 29 7b 0a 20 20 20 20 63 6f  >pVTab ){.    co
23f55 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61  nst char *zForma
23f56 74 20 3d 20 22 76 74 61 62 6c 65 20 63 6f 6e 73  t = "vtable cons
23f57 74 72 75 63 74 6f 72 20 64 69 64 20 6e 6f 74 20  tructor did not 
23f58 64 65 63 6c 61 72 65 20 73 63 68 65 6d 61 3a 20  declare schema: 
23f59 25 73 22 3b 0a 20 20 20 20 2a 70 7a 45 72 72 20  %s";.    *pzErr 
23f5a 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
23f5b 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 70 54  (db, zFormat, pT
23f5c 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ab->zName);.    
23f5d 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
23f5e 52 3b 0a 20 20 7d 20 0a 20 20 69 66 28 20 72 63  R;.  } .  if( rc
23f5f 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
23f60 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 7d     rc = rc2;.  }
23f61 0a 20 20 64 62 2d 3e 70 56 54 61 62 20 3d 20 30  .  db->pVTab = 0
23f62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
23f63 28 7a 4d 6f 64 75 6c 65 4e 61 6d 65 29 3b 0a 0a  (zModuleName);..
23f64 20 20 2f 2a 20 49 66 20 65 76 65 72 79 74 68 69    /* If everythi
23f65 6e 67 20 77 65 6e 74 20 61 63 63 6f 72 64 69 6e  ng went accordin
23f66 67 20 74 6f 20 70 6c 61 6e 2c 20 6c 6f 6f 70 20  g to plan, loop 
23f67 74 68 72 6f 75 67 68 20 74 68 65 20 63 6f 6c 75  through the colu
23f68 6d 6e 73 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  mns.  ** of the 
23f69 74 61 62 6c 65 20 74 6f 20 73 65 65 20 69 66 20  table to see if 
23f6a 61 6e 79 20 6f 66 20 74 68 65 6d 20 63 6f 6e 74  any of them cont
23f6b 61 69 6e 20 74 68 65 20 74 6f 6b 65 6e 20 22 68  ain the token "h
23f6c 69 64 64 65 6e 22 2e 0a 20 20 2a 2a 20 49 66 20  idden"..  ** If 
23f6d 73 6f 2c 20 73 65 74 20 74 68 65 20 43 6f 6c 75  so, set the Colu
23f6e 6d 6e 2e 69 73 48 69 64 64 65 6e 20 66 6c 61 67  mn.isHidden flag
23f6f 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 65 20   and remove the 
23f70 74 6f 6b 65 6e 20 66 72 6f 6d 0a 20 20 2a 2a 20  token from.  ** 
23f71 74 68 65 20 74 79 70 65 20 73 74 72 69 6e 67 2e  the type string.
23f72 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d  .  */.  if( rc==
23f73 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
23f74 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 20 20 66   int iCol;.    f
23f75 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c  or(iCol=0; iCol<
23f76 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c  pTab->nCol; iCol
23f77 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20  ++){.      char 
23f78 2a 7a 54 79 70 65 20 3d 20 70 54 61 62 2d 3e 61  *zType = pTab->a
23f79 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 54 79 70 65 3b  Col[iCol].zType;
23f7a 0a 20 20 20 20 20 20 69 6e 74 20 6e 54 79 70 65  .      int nType
23f7b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 20 3d 20  ;.      int i = 
23f7c 30 3b 0a 20 20 20 20 20 20 69 66 28 20 21 7a 54  0;.      if( !zT
23f7d 79 70 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ype ) continue;.
23f7e 20 20 20 20 20 20 6e 54 79 70 65 20 3d 20 73 74        nType = st
23f7f 72 6c 65 6e 28 7a 54 79 70 65 29 3b 0a 20 20 20  rlen(zType);.   
23f80 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
23f81 72 4e 49 43 6d 70 28 22 68 69 64 64 65 6e 22 2c  rNICmp("hidden",
23f82 20 7a 54 79 70 65 2c 20 36 29 20 7c 7c 20 28 7a   zType, 6) || (z
23f83 54 79 70 65 5b 36 5d 20 26 26 20 7a 54 79 70 65  Type[6] && zType
23f84 5b 36 5d 21 3d 27 20 27 29 20 29 7b 0a 20 20 20  [6]!=' ') ){.   
23f85 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
23f86 6e 54 79 70 65 3b 20 69 2b 2b 29 7b 0a 20 20 20  nType; i++){.   
23f87 20 20 20 20 20 20 20 69 66 28 20 28 30 3d 3d 73         if( (0==s
23f88 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 22  qlite3StrNICmp("
23f89 20 68 69 64 64 65 6e 22 2c 20 26 7a 54 79 70 65   hidden", &zType
23f8a 5b 69 5d 2c 20 37 29 29 0a 20 20 20 20 20 20 20  [i], 7)).       
23f8b 20 20 20 20 26 26 20 28 7a 54 79 70 65 5b 69 2b      && (zType[i+
23f8c 37 5d 3d 3d 27 5c 30 27 20 7c 7c 20 7a 54 79 70  7]=='\0' || zTyp
23f8d 65 5b 69 2b 37 5d 3d 3d 27 20 27 29 0a 20 20 20  e[i+7]==' ').   
23f8e 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20         ){.      
23f8f 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20        i++;.     
23f90 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
23f91 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
23f92 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
23f93 20 20 69 66 28 20 69 3c 6e 54 79 70 65 20 29 7b    if( i<nType ){
23f94 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a  .        int j;.
23f95 20 20 20 20 20 20 20 20 69 6e 74 20 6e 44 65 6c          int nDel
23f96 20 3d 20 36 20 2b 20 28 7a 54 79 70 65 5b 69 2b   = 6 + (zType[i+
23f97 36 5d 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20  6] ? 1 : 0);.   
23f98 20 20 20 20 20 66 6f 72 28 6a 3d 69 3b 20 28 6a       for(j=i; (j
23f99 2b 6e 44 65 6c 29 3c 3d 6e 54 79 70 65 3b 20 6a  +nDel)<=nType; j
23f9a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a  ++){.          z
23f9b 54 79 70 65 5b 6a 5d 20 3d 20 7a 54 79 70 65 5b  Type[j] = zType[
23f9c 6a 2b 6e 44 65 6c 5d 3b 0a 20 20 20 20 20 20 20  j+nDel];.       
23f9d 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a   }.        if( z
23f9e 54 79 70 65 5b 69 5d 3d 3d 27 5c 30 27 20 26 26  Type[i]=='\0' &&
23f9f 20 69 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20   i>0 ){.        
23fa0 20 20 61 73 73 65 72 74 28 7a 54 79 70 65 5b 69    assert(zType[i
23fa1 2d 31 5d 3d 3d 27 20 27 29 3b 0a 20 20 20 20 20  -1]==' ');.     
23fa2 20 20 20 20 20 7a 54 79 70 65 5b 69 2d 31 5d 20       zType[i-1] 
23fa3 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20  = '\0';.        
23fa4 7d 0a 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e  }.        pTab->
23fa5 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73 48 69 64  aCol[iCol].isHid
23fa6 64 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  den = 1;.      }
23fa7 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
23fa8 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
23fa9 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
23faa 73 20 69 6e 76 6f 6b 65 64 20 62 79 20 74 68 65  s invoked by the
23fab 20 70 61 72 73 65 72 20 74 6f 20 63 61 6c 6c 20   parser to call 
23fac 74 68 65 20 78 43 6f 6e 6e 65 63 74 28 29 20 6d  the xConnect() m
23fad 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20  ethod.** of the 
23fae 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 70 54  virtual table pT
23faf 61 62 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  ab. If an error 
23fb0 6f 63 63 75 72 73 2c 20 61 6e 20 65 72 72 6f 72  occurs, an error
23fb1 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
23fb2 64 20 0a 2a 2a 20 61 6e 64 20 61 6e 20 65 72 72  d .** and an err
23fb3 6f 72 20 6c 65 66 74 20 69 6e 20 70 50 61 72 73  or left in pPars
23fb4 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 61  e..**.** This ca
23fb5 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66  ll is a no-op if
23fb6 20 74 61 62 6c 65 20 70 54 61 62 20 69 73 20 6e   table pTab is n
23fb7 6f 74 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  ot a virtual tab
23fb8 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
23fb9 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
23fba 33 56 74 61 62 43 61 6c 6c 43 6f 6e 6e 65 63 74  3VtabCallConnect
23fbb 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
23fbc 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20  Table *pTab){.  
23fbd 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 0a 20 20  Module *pMod;.  
23fbe 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
23fbf 4f 4b 3b 0a 0a 20 20 69 66 28 20 21 70 54 61 62  OK;..  if( !pTab
23fc0 20 7c 7c 20 21 70 54 61 62 2d 3e 69 73 56 69 72   || !pTab->isVir
23fc1 74 75 61 6c 20 7c 7c 20 70 54 61 62 2d 3e 70 56  tual || pTab->pV
23fc2 74 61 62 20 29 7b 0a 20 20 20 20 72 65 74 75 72  tab ){.    retur
23fc3 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
23fc4 0a 0a 20 20 70 4d 6f 64 20 3d 20 70 54 61 62 2d  ..  pMod = pTab-
23fc5 3e 70 4d 6f 64 3b 0a 20 20 69 66 28 20 21 70 4d  >pMod;.  if( !pM
23fc6 6f 64 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  od ){.    const 
23fc7 63 68 61 72 20 2a 7a 4d 6f 64 75 6c 65 20 3d 20  char *zModule = 
23fc8 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72  pTab->azModuleAr
23fc9 67 5b 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65  g[0];.    sqlite
23fca 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
23fcb 2c 20 22 6e 6f 20 73 75 63 68 20 6d 6f 64 75 6c  , "no such modul
23fcc 65 3a 20 25 73 22 2c 20 7a 4d 6f 64 75 6c 65 29  e: %s", zModule)
23fcd 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
23fce 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 20 65 6c 73  E_ERROR;.  } els
23fcf 65 20 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 45  e {.    char *zE
23fd0 72 72 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69  rr = 0;.    sqli
23fd1 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
23fd2 2d 3e 64 62 3b 0a 20 20 20 20 72 63 20 3d 20 76  ->db;.    rc = v
23fd3 74 61 62 43 61 6c 6c 43 6f 6e 73 74 72 75 63 74  tabCallConstruct
23fd4 6f 72 28 64 62 2c 20 70 54 61 62 2c 20 70 4d 6f  or(db, pTab, pMo
23fd5 64 2c 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65  d, pMod->pModule
23fd6 2d 3e 78 43 6f 6e 6e 65 63 74 2c 20 26 7a 45 72  ->xConnect, &zEr
23fd7 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  r);.    if( rc!=
23fd8 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
23fd9 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
23fda 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 22 2c  sg(pParse, "%s",
23fdb 20 7a 45 72 72 29 3b 0a 20 20 20 20 7d 0a 20 20   zErr);.    }.  
23fdc 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
23fdd 45 72 72 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  Err);.  }..  ret
23fde 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
23fdf 20 41 64 64 20 74 68 65 20 76 69 72 74 75 61 6c   Add the virtual
23fe0 20 74 61 62 6c 65 20 70 56 74 61 62 20 74 6f 20   table pVtab to 
23fe1 74 68 65 20 61 72 72 61 79 20 73 71 6c 69 74 65  the array sqlite
23fe2 33 2e 61 56 54 72 61 6e 73 5b 5d 2e 0a 2a 2f 0a  3.aVTrans[]..*/.
23fe3 73 74 61 74 69 63 20 69 6e 74 20 61 64 64 54 6f  static int addTo
23fe4 56 54 72 61 6e 73 28 73 71 6c 69 74 65 33 20 2a  VTrans(sqlite3 *
23fe5 64 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62  db, sqlite3_vtab
23fe6 20 2a 70 56 74 61 62 29 7b 0a 20 20 63 6f 6e 73   *pVtab){.  cons
23fe7 74 20 69 6e 74 20 41 52 52 41 59 5f 49 4e 43 52  t int ARRAY_INCR
23fe8 20 3d 20 35 3b 0a 0a 20 20 2f 2a 20 47 72 6f 77   = 5;..  /* Grow
23fe9 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 56 54   the sqlite3.aVT
23fea 72 61 6e 73 20 61 72 72 61 79 20 69 66 20 72 65  rans array if re
23feb 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 66 28 20  quired */.  if( 
23fec 28 64 62 2d 3e 6e 56 54 72 61 6e 73 25 41 52 52  (db->nVTrans%ARR
23fed 41 59 5f 49 4e 43 52 29 3d 3d 30 20 29 7b 0a 20  AY_INCR)==0 ){. 
23fee 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20     sqlite3_vtab 
23fef 2a 2a 61 56 54 72 61 6e 73 3b 0a 20 20 20 20 69  **aVTrans;.    i
23ff0 6e 74 20 6e 42 79 74 65 73 20 3d 20 73 69 7a 65  nt nBytes = size
23ff1 6f 66 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  of(sqlite3_vtab 
23ff2 2a 29 20 2a 20 28 64 62 2d 3e 6e 56 54 72 61 6e  *) * (db->nVTran
23ff3 73 20 2b 20 41 52 52 41 59 5f 49 4e 43 52 29 3b  s + ARRAY_INCR);
23ff4 0a 20 20 20 20 61 56 54 72 61 6e 73 20 3d 20 73  .    aVTrans = s
23ff5 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28  qlite3DbRealloc(
23ff6 64 62 2c 20 28 76 6f 69 64 20 2a 29 64 62 2d 3e  db, (void *)db->
23ff7 61 56 54 72 61 6e 73 2c 20 6e 42 79 74 65 73 29  aVTrans, nBytes)
23ff8 3b 0a 20 20 20 20 69 66 28 20 21 61 56 54 72 61  ;.    if( !aVTra
23ff9 6e 73 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  ns ){.      retu
23ffa 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
23ffb 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65  .    }.    memse
23ffc 74 28 26 61 56 54 72 61 6e 73 5b 64 62 2d 3e 6e  t(&aVTrans[db->n
23ffd 56 54 72 61 6e 73 5d 2c 20 30 2c 20 73 69 7a 65  VTrans], 0, size
23ffe 6f 66 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  of(sqlite3_vtab 
23fff 2a 29 2a 41 52 52 41 59 5f 49 4e 43 52 29 3b 0a  *)*ARRAY_INCR);.
24000 20 20 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 20      db->aVTrans 
24001 3d 20 61 56 54 72 61 6e 73 3b 0a 20 20 7d 0a 0a  = aVTrans;.  }..
24002 20 20 2f 2a 20 41 64 64 20 70 56 74 61 62 20 74    /* Add pVtab t
24003 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 73 71 6c  o the end of sql
24004 69 74 65 33 2e 61 56 54 72 61 6e 73 20 2a 2f 0a  ite3.aVTrans */.
24005 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 64 62    db->aVTrans[db
24006 2d 3e 6e 56 54 72 61 6e 73 2b 2b 5d 20 3d 20 70  ->nVTrans++] = p
24007 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 56  Vtab;.  sqlite3V
24008 74 61 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b 0a  tabLock(pVtab);.
24009 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2400a 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  OK;.}../*.** Thi
2400b 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e  s function is in
2400c 76 6f 6b 65 64 20 62 79 20 74 68 65 20 76 64 62  voked by the vdb
2400d 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 78 43  e to call the xC
2400e 72 65 61 74 65 20 6d 65 74 68 6f 64 0a 2a 2a 20  reate method.** 
2400f 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  of the virtual t
24010 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 61 62 20  able named zTab 
24011 69 6e 20 64 61 74 61 62 61 73 65 20 69 44 62 2e  in database iDb.
24012 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72   .**.** If an er
24013 72 6f 72 20 6f 63 63 75 72 73 2c 20 2a 70 7a 45  ror occurs, *pzE
24014 72 72 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69  rr is set to poi
24015 6e 74 20 61 6e 20 61 6e 20 45 6e 67 6c 69 73 68  nt an an English
24016 20 6c 61 6e 67 75 61 67 65 0a 2a 2a 20 64 65 73   language.** des
24017 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20  cription of the 
24018 65 72 72 6f 72 20 61 6e 64 20 61 6e 20 53 51 4c  error and an SQL
24019 49 54 45 5f 58 58 58 20 65 72 72 6f 72 20 63 6f  ITE_XXX error co
2401a 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  de is returned..
2401b 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  ** In this case 
2401c 74 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20  the caller must 
2401d 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 66 72 65  call sqlite3_fre
2401e 65 28 29 20 6f 6e 20 2a 70 7a 45 72 72 2e 0a 2a  e() on *pzErr..*
2401f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
24020 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62   int sqlite3Vtab
24021 43 61 6c 6c 43 72 65 61 74 65 28 73 71 6c 69 74  CallCreate(sqlit
24022 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c  e3 *db, int iDb,
24023 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61   const char *zTa
24024 62 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 29  b, char **pzErr)
24025 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
24026 49 54 45 5f 4f 4b 3b 0a 20 20 54 61 62 6c 65 20  ITE_OK;.  Table 
24027 2a 70 54 61 62 3b 0a 20 20 4d 6f 64 75 6c 65 20  *pTab;.  Module 
24028 2a 70 4d 6f 64 3b 0a 20 20 63 6f 6e 73 74 20 63  *pMod;.  const c
24029 68 61 72 20 2a 7a 4d 6f 64 75 6c 65 3b 0a 0a 20  har *zModule;.. 
2402a 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46   pTab = sqlite3F
2402b 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 54 61  indTable(db, zTa
2402c 62 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  b, db->aDb[iDb].
2402d 7a 4e 61 6d 65 29 3b 0a 20 20 61 73 73 65 72 74  zName);.  assert
2402e 28 70 54 61 62 20 26 26 20 70 54 61 62 2d 3e 69  (pTab && pTab->i
2402f 73 56 69 72 74 75 61 6c 20 26 26 20 21 70 54 61  sVirtual && !pTa
24030 62 2d 3e 70 56 74 61 62 29 3b 0a 20 20 70 4d 6f  b->pVtab);.  pMo
24031 64 20 3d 20 70 54 61 62 2d 3e 70 4d 6f 64 3b 0a  d = pTab->pMod;.
24032 20 20 7a 4d 6f 64 75 6c 65 20 3d 20 70 54 61 62    zModule = pTab
24033 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d  ->azModuleArg[0]
24034 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6d  ;..  /* If the m
24035 6f 64 75 6c 65 20 68 61 73 20 62 65 65 6e 20 72  odule has been r
24036 65 67 69 73 74 65 72 65 64 20 61 6e 64 20 69 6e  egistered and in
24037 63 6c 75 64 65 73 20 61 20 43 72 65 61 74 65 20  cludes a Create 
24038 6d 65 74 68 6f 64 2c 20 0a 20 20 2a 2a 20 69 6e  method, .  ** in
24039 76 6f 6b 65 20 69 74 20 6e 6f 77 2e 20 49 66 20  voke it now. If 
2403a 74 68 65 20 6d 6f 64 75 6c 65 20 68 61 73 20 6e  the module has n
2403b 6f 74 20 62 65 65 6e 20 72 65 67 69 73 74 65 72  ot been register
2403c 65 64 2c 20 72 65 74 75 72 6e 20 61 6e 20 0a 20  ed, return an . 
2403d 20 2a 2a 20 65 72 72 6f 72 2e 20 4f 74 68 65 72   ** error. Other
2403e 77 69 73 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67  wise, do nothing
2403f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 4d  ..  */.  if( !pM
24040 6f 64 20 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72  od ){.    *pzErr
24041 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
24042 66 28 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 6d  f(db, "no such m
24043 6f 64 75 6c 65 3a 20 25 73 22 2c 20 7a 4d 6f 64  odule: %s", zMod
24044 75 6c 65 29 3b 0a 20 20 20 20 72 63 20 3d 20 53  ule);.    rc = S
24045 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
24046 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 76  else{.    rc = v
24047 74 61 62 43 61 6c 6c 43 6f 6e 73 74 72 75 63 74  tabCallConstruct
24048 6f 72 28 64 62 2c 20 70 54 61 62 2c 20 70 4d 6f  or(db, pTab, pMo
24049 64 2c 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65  d, pMod->pModule
2404a 2d 3e 78 43 72 65 61 74 65 2c 20 70 7a 45 72 72  ->xCreate, pzErr
2404b 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63  );.  }..  if( rc
2404c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
2404d 54 61 62 2d 3e 70 56 74 61 62 20 29 7b 0a 20 20  Tab->pVtab ){.  
2404e 20 20 20 20 72 63 20 3d 20 61 64 64 54 6f 56 54      rc = addToVT
2404f 72 61 6e 73 28 64 62 2c 20 70 54 61 62 2d 3e 70  rans(db, pTab->p
24050 56 74 61 62 29 3b 0a 20 20 7d 0a 0a 20 20 72 65  Vtab);.  }..  re
24051 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
24052 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
24053 69 73 20 75 73 65 64 20 74 6f 20 73 65 74 20 74  is used to set t
24054 68 65 20 73 63 68 65 6d 61 20 6f 66 20 61 20 76  he schema of a v
24055 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 49  irtual table.  I
24056 74 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 76 61 6c  t is only.** val
24057 69 64 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20  id to call this 
24058 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 20 77 69  function from wi
24059 74 68 69 6e 20 74 68 65 20 78 43 72 65 61 74 65  thin the xCreate
2405a 28 29 20 6f 72 20 78 43 6f 6e 6e 65 63 74 28 29  () or xConnect()
2405b 20 6f 66 20 61 0a 2a 2a 20 76 69 72 74 75 61 6c   of a.** virtual
2405c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 0a 2a   table module..*
2405d 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2405e 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65   sqlite3_declare
2405f 5f 76 74 61 62 28 73 71 6c 69 74 65 33 20 2a 64  _vtab(sqlite3 *d
24060 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
24061 43 72 65 61 74 65 54 61 62 6c 65 29 7b 0a 20 20  CreateTable){.  
24062 50 61 72 73 65 20 73 50 61 72 73 65 3b 0a 0a 20  Parse sParse;.. 
24063 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
24064 5f 4f 4b 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54  _OK;.  Table *pT
24065 61 62 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72  ab;.  char *zErr
24066 20 3d 20 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33   = 0;..  sqlite3
24067 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
24068 3e 6d 75 74 65 78 29 3b 0a 20 20 70 54 61 62 20  >mutex);.  pTab 
24069 3d 20 64 62 2d 3e 70 56 54 61 62 3b 0a 20 20 69  = db->pVTab;.  i
2406a 66 28 20 21 70 54 61 62 20 29 7b 0a 20 20 20 20  f( !pTab ){.    
2406b 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
2406c 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20   SQLITE_MISUSE, 
2406d 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
2406e 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
2406f 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 65 74 75  mutex);.    retu
24070 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  rn SQLITE_MISUSE
24071 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 70  ;.  }.  assert(p
24072 54 61 62 2d 3e 69 73 56 69 72 74 75 61 6c 20 26  Tab->isVirtual &
24073 26 20 70 54 61 62 2d 3e 6e 43 6f 6c 3d 3d 30 20  & pTab->nCol==0 
24074 26 26 20 70 54 61 62 2d 3e 61 43 6f 6c 3d 3d 30  && pTab->aCol==0
24075 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 26 73 50  );..  memset(&sP
24076 61 72 73 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28  arse, 0, sizeof(
24077 50 61 72 73 65 29 29 3b 0a 20 20 73 50 61 72 73  Parse));.  sPars
24078 65 2e 64 65 63 6c 61 72 65 56 74 61 62 20 3d 20  e.declareVtab = 
24079 31 3b 0a 20 20 73 50 61 72 73 65 2e 64 62 20 3d  1;.  sParse.db =
2407a 20 64 62 3b 0a 0a 20 20 69 66 28 20 0a 20 20 20   db;..  if( .   
2407b 20 20 20 53 51 4c 49 54 45 5f 4f 4b 20 3d 3d 20     SQLITE_OK == 
2407c 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72  sqlite3RunParser
2407d 28 26 73 50 61 72 73 65 2c 20 7a 43 72 65 61 74  (&sParse, zCreat
2407e 65 54 61 62 6c 65 2c 20 26 7a 45 72 72 29 20 26  eTable, &zErr) &
2407f 26 20 0a 20 20 20 20 20 20 73 50 61 72 73 65 2e  & .      sParse.
24080 70 4e 65 77 54 61 62 6c 65 20 26 26 20 0a 20 20  pNewTable && .  
24081 20 20 20 20 21 73 50 61 72 73 65 2e 70 4e 65 77      !sParse.pNew
24082 54 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74 20 26  Table->pSelect &
24083 26 20 0a 20 20 20 20 20 20 21 73 50 61 72 73 65  & .      !sParse
24084 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 69 73 56 69  .pNewTable->isVi
24085 72 74 75 61 6c 20 0a 20 20 29 7b 0a 20 20 20 20  rtual .  ){.    
24086 70 54 61 62 2d 3e 61 43 6f 6c 20 3d 20 73 50 61  pTab->aCol = sPa
24087 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 61  rse.pNewTable->a
24088 43 6f 6c 3b 0a 20 20 20 20 70 54 61 62 2d 3e 6e  Col;.    pTab->n
24089 43 6f 6c 20 3d 20 73 50 61 72 73 65 2e 70 4e 65  Col = sParse.pNe
2408a 77 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3b 0a 20 20  wTable->nCol;.  
2408b 20 20 73 50 61 72 73 65 2e 70 4e 65 77 54 61 62    sParse.pNewTab
2408c 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20  le->nCol = 0;.  
2408d 20 20 73 50 61 72 73 65 2e 70 4e 65 77 54 61 62    sParse.pNewTab
2408e 6c 65 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20  le->aCol = 0;.  
2408f 20 20 64 62 2d 3e 70 56 54 61 62 20 3d 20 30 3b    db->pVTab = 0;
24090 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20  .  } else {.    
24091 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
24092 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 7a   SQLITE_ERROR, z
24093 45 72 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Err);.    sqlite
24094 33 5f 66 72 65 65 28 7a 45 72 72 29 3b 0a 20 20  3_free(zErr);.  
24095 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
24096 52 4f 52 3b 0a 20 20 7d 0a 20 20 73 50 61 72 73  ROR;.  }.  sPars
24097 65 2e 64 65 63 6c 61 72 65 56 74 61 62 20 3d 20  e.declareVtab = 
24098 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 69  0;..  sqlite3_fi
24099 6e 61 6c 69 7a 65 28 28 73 71 6c 69 74 65 33 5f  nalize((sqlite3_
2409a 73 74 6d 74 2a 29 73 50 61 72 73 65 2e 70 56 64  stmt*)sParse.pVd
2409b 62 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65  be);.  sqlite3De
2409c 6c 65 74 65 54 61 62 6c 65 28 73 50 61 72 73 65  leteTable(sParse
2409d 2e 70 4e 65 77 54 61 62 6c 65 29 3b 0a 20 20 73  .pNewTable);.  s
2409e 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 20  Parse.pNewTable 
2409f 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  = 0;..  assert( 
240a0 28 72 63 26 30 78 66 66 29 3d 3d 72 63 20 29 3b  (rc&0xff)==rc );
240a1 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41  .  rc = sqlite3A
240a2 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a  piExit(db, rc);.
240a3 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
240a4 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
240a5 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
240a6 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
240a7 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64  ction is invoked
240a8 20 62 79 20 74 68 65 20 76 64 62 65 20 74 6f 20   by the vdbe to 
240a9 63 61 6c 6c 20 74 68 65 20 78 44 65 73 74 72 6f  call the xDestro
240aa 79 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74  y method.** of t
240ab 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
240ac 20 6e 61 6d 65 64 20 7a 54 61 62 20 69 6e 20 64   named zTab in d
240ad 61 74 61 62 61 73 65 20 69 44 62 2e 20 54 68 69  atabase iDb. Thi
240ae 73 20 6f 63 63 75 72 73 0a 2a 2a 20 77 68 65 6e  s occurs.** when
240af 20 61 20 44 52 4f 50 20 54 41 42 4c 45 20 69 73   a DROP TABLE is
240b0 20 6d 65 6e 74 69 6f 6e 65 64 2e 0a 2a 2a 0a 2a   mentioned..**.*
240b1 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73 20 61  * This call is a
240b2 20 6e 6f 2d 6f 70 20 69 66 20 7a 54 61 62 20 69   no-op if zTab i
240b3 73 20 6e 6f 74 20 61 20 76 69 72 74 75 61 6c 20  s not a virtual 
240b4 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  table..*/.SQLITE
240b5 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
240b6 69 74 65 33 56 74 61 62 43 61 6c 6c 44 65 73 74  ite3VtabCallDest
240b7 72 6f 79 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  roy(sqlite3 *db,
240b8 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 20   int iDb, const 
240b9 63 68 61 72 20 2a 7a 54 61 62 29 0a 7b 0a 20 20  char *zTab).{.  
240ba 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
240bb 4f 4b 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  OK;.  Table *pTa
240bc 62 3b 0a 0a 20 20 70 54 61 62 20 3d 20 73 71 6c  b;..  pTab = sql
240bd 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62  ite3FindTable(db
240be 2c 20 7a 54 61 62 2c 20 64 62 2d 3e 61 44 62 5b  , zTab, db->aDb[
240bf 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 61  iDb].zName);.  a
240c0 73 73 65 72 74 28 70 54 61 62 29 3b 0a 20 20 69  ssert(pTab);.  i
240c1 66 28 20 70 54 61 62 2d 3e 70 56 74 61 62 20 29  f( pTab->pVtab )
240c2 7b 0a 20 20 20 20 69 6e 74 20 28 2a 78 44 65 73  {.    int (*xDes
240c3 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f 76 74  troy)(sqlite3_vt
240c4 61 62 20 2a 70 56 54 61 62 29 20 3d 20 70 54 61  ab *pVTab) = pTa
240c5 62 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65  b->pMod->pModule
240c6 2d 3e 78 44 65 73 74 72 6f 79 3b 0a 20 20 20 20  ->xDestroy;.    
240c7 72 63 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65  rc = sqlite3Safe
240c8 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 61  tyOff(db);.    a
240c9 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
240ca 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69 66 28 20  E_OK );.    if( 
240cb 78 44 65 73 74 72 6f 79 20 29 7b 0a 20 20 20 20  xDestroy ){.    
240cc 20 20 72 63 20 3d 20 78 44 65 73 74 72 6f 79 28    rc = xDestroy(
240cd 70 54 61 62 2d 3e 70 56 74 61 62 29 3b 0a 20 20  pTab->pVtab);.  
240ce 20 20 7d 0a 20 20 20 20 28 76 6f 69 64 29 73 71    }.    (void)sq
240cf 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
240d0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
240d1 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
240d2 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 66    int i;.      f
240d3 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 56  or(i=0; i<db->nV
240d4 54 72 61 6e 73 3b 20 69 2b 2b 29 7b 0a 20 20 20  Trans; i++){.   
240d5 20 20 20 20 20 69 66 28 20 64 62 2d 3e 61 56 54       if( db->aVT
240d6 72 61 6e 73 5b 69 5d 3d 3d 70 54 61 62 2d 3e 70  rans[i]==pTab->p
240d7 56 74 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20  Vtab ){.        
240d8 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 69 5d    db->aVTrans[i]
240d9 20 3d 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 2d   = db->aVTrans[-
240da 2d 64 62 2d 3e 6e 56 54 72 61 6e 73 5d 3b 0a 20  -db->nVTrans];. 
240db 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
240dc 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
240dd 7d 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 70 56  }.      pTab->pV
240de 74 61 62 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  tab = 0;.    }. 
240df 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
240e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
240e1 75 6e 63 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 20  unction invokes 
240e2 65 69 74 68 65 72 20 74 68 65 20 78 52 6f 6c 6c  either the xRoll
240e3 62 61 63 6b 20 6f 72 20 78 43 6f 6d 6d 69 74 20  back or xCommit 
240e4 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 65 61 63  method.** of eac
240e5 68 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c  h of the virtual
240e6 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 73   tables in the s
240e7 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 20 61  qlite3.aVTrans a
240e8 72 72 61 79 2e 20 54 68 65 20 6d 65 74 68 6f 64  rray. The method
240e9 0a 2a 2a 20 63 61 6c 6c 65 64 20 69 73 20 69 64  .** called is id
240ea 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20  entified by the 
240eb 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c  second argument,
240ec 20 22 6f 66 66 73 65 74 22 2c 20 77 68 69 63 68   "offset", which
240ed 20 69 73 0a 2a 2a 20 74 68 65 20 6f 66 66 73 65   is.** the offse
240ee 74 20 6f 66 20 74 68 65 20 6d 65 74 68 6f 64 20  t of the method 
240ef 74 6f 20 63 61 6c 6c 20 69 6e 20 74 68 65 20 73  to call in the s
240f0 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 73 74  qlite3_module st
240f1 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54  ructure..**.** T
240f2 68 65 20 61 72 72 61 79 20 69 73 20 63 6c 65 61  he array is clea
240f3 72 65 64 20 61 66 74 65 72 20 69 6e 76 6f 6b 69  red after invoki
240f4 6e 67 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 73  ng the callbacks
240f5 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  . .*/.static voi
240f6 64 20 63 61 6c 6c 46 69 6e 61 6c 69 73 65 72 28  d callFinaliser(
240f7 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
240f8 20 6f 66 66 73 65 74 29 7b 0a 20 20 69 6e 74 20   offset){.  int 
240f9 69 3b 0a 20 20 69 66 28 20 64 62 2d 3e 61 56 54  i;.  if( db->aVT
240fa 72 61 6e 73 20 29 7b 0a 20 20 20 20 66 6f 72 28  rans ){.    for(
240fb 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 56 54 72 61  i=0; i<db->nVTra
240fc 6e 73 20 26 26 20 64 62 2d 3e 61 56 54 72 61 6e  ns && db->aVTran
240fd 73 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  s[i]; i++){.    
240fe 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
240ff 70 56 74 61 62 20 3d 20 64 62 2d 3e 61 56 54 72  pVtab = db->aVTr
24100 61 6e 73 5b 69 5d 3b 0a 20 20 20 20 20 20 69 6e  ans[i];.      in
24101 74 20 28 2a 78 29 28 73 71 6c 69 74 65 33 5f 76  t (*x)(sqlite3_v
24102 74 61 62 20 2a 29 3b 0a 20 20 20 20 20 20 78 20  tab *);.      x 
24103 3d 20 2a 28 69 6e 74 20 28 2a 2a 29 28 73 71 6c  = *(int (**)(sql
24104 69 74 65 33 5f 76 74 61 62 20 2a 29 29 28 28 63  ite3_vtab *))((c
24105 68 61 72 20 2a 29 70 56 74 61 62 2d 3e 70 4d 6f  har *)pVtab->pMo
24106 64 75 6c 65 20 2b 20 6f 66 66 73 65 74 29 3b 0a  dule + offset);.
24107 20 20 20 20 20 20 69 66 28 20 78 20 29 20 78 28        if( x ) x(
24108 70 56 74 61 62 29 3b 0a 20 20 20 20 20 20 73 71  pVtab);.      sq
24109 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28  lite3VtabUnlock(
2410a 64 62 2c 20 70 56 74 61 62 29 3b 0a 20 20 20 20  db, pVtab);.    
2410b 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  }.    sqlite3_fr
2410c 65 65 28 64 62 2d 3e 61 56 54 72 61 6e 73 29 3b  ee(db->aVTrans);
2410d 0a 20 20 20 20 64 62 2d 3e 6e 56 54 72 61 6e 73  .    db->nVTrans
2410e 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 61 56   = 0;.    db->aV
2410f 54 72 61 6e 73 20 3d 20 30 3b 0a 20 20 7d 0a 7d  Trans = 0;.  }.}
24110 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 61 72 67 75 6d  ../*.** If argum
24111 65 6e 74 20 72 63 32 20 69 73 20 6e 6f 74 20 53  ent rc2 is not S
24112 51 4c 49 54 45 5f 4f 4b 2c 20 74 68 65 6e 20 72  QLITE_OK, then r
24113 65 74 75 72 6e 20 69 74 20 61 6e 64 20 64 6f 20  eturn it and do 
24114 6e 6f 74 68 69 6e 67 2e 20 0a 2a 2a 20 4f 74 68  nothing. .** Oth
24115 65 72 77 69 73 65 2c 20 69 6e 76 6f 6b 65 20 74  erwise, invoke t
24116 68 65 20 78 53 79 6e 63 20 6d 65 74 68 6f 64 20  he xSync method 
24117 6f 66 20 61 6c 6c 20 76 69 72 74 75 61 6c 20 74  of all virtual t
24118 61 62 6c 65 73 20 69 6e 20 74 68 65 20 0a 2a 2a  ables in the .**
24119 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73   sqlite3.aVTrans
2411a 20 61 72 72 61 79 2e 20 52 65 74 75 72 6e 20 74   array. Return t
2411b 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 66 6f  he error code fo
2411c 72 20 74 68 65 20 66 69 72 73 74 20 65 72 72 6f  r the first erro
2411d 72 20 0a 2a 2a 20 74 68 61 74 20 6f 63 63 75 72  r .** that occur
2411e 73 2c 20 6f 72 20 53 51 4c 49 54 45 5f 4f 4b 20  s, or SQLITE_OK 
2411f 69 66 20 61 6c 6c 20 78 53 79 6e 63 20 6f 70 65  if all xSync ope
24120 72 61 74 69 6f 6e 73 20 61 72 65 20 73 75 63 63  rations are succ
24121 65 73 73 66 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 54  essful..*/.SQLIT
24122 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
24123 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 73 71  lite3VtabSync(sq
24124 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 72  lite3 *db, int r
24125 63 32 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  c2){.  int i;.  
24126 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
24127 4f 4b 3b 0a 20 20 69 6e 74 20 72 63 73 61 66 65  OK;.  int rcsafe
24128 74 79 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74  ty;.  sqlite3_vt
24129 61 62 20 2a 2a 61 56 54 72 61 6e 73 20 3d 20 64  ab **aVTrans = d
2412a 62 2d 3e 61 56 54 72 61 6e 73 3b 0a 20 20 69 66  b->aVTrans;.  if
2412b 28 20 72 63 32 21 3d 53 51 4c 49 54 45 5f 4f 4b  ( rc2!=SQLITE_OK
2412c 20 29 20 72 65 74 75 72 6e 20 72 63 32 3b 0a 0a   ) return rc2;..
2412d 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 61    rc = sqlite3Sa
2412e 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 64  fetyOff(db);.  d
2412f 62 2d 3e 61 56 54 72 61 6e 73 20 3d 20 30 3b 0a  b->aVTrans = 0;.
24130 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53    for(i=0; rc==S
24131 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62  QLITE_OK && i<db
24132 2d 3e 6e 56 54 72 61 6e 73 20 26 26 20 61 56 54  ->nVTrans && aVT
24133 72 61 6e 73 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20  rans[i]; i++){. 
24134 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20     sqlite3_vtab 
24135 2a 70 56 74 61 62 20 3d 20 61 56 54 72 61 6e 73  *pVtab = aVTrans
24136 5b 69 5d 3b 0a 20 20 20 20 69 6e 74 20 28 2a 78  [i];.    int (*x
24137 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  )(sqlite3_vtab *
24138 29 3b 0a 20 20 20 20 78 20 3d 20 70 56 74 61 62  );.    x = pVtab
24139 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 53 79 6e 63  ->pModule->xSync
2413a 3b 0a 20 20 20 20 69 66 28 20 78 20 29 7b 0a 20  ;.    if( x ){. 
2413b 20 20 20 20 20 72 63 20 3d 20 78 28 70 56 74 61       rc = x(pVta
2413c 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  b);.    }.  }.  
2413d 64 62 2d 3e 61 56 54 72 61 6e 73 20 3d 20 61 56  db->aVTrans = aV
2413e 54 72 61 6e 73 3b 0a 20 20 72 63 73 61 66 65 74  Trans;.  rcsafet
2413f 79 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65 74  y = sqlite3Safet
24140 79 4f 6e 28 64 62 29 3b 0a 0a 20 20 69 66 28 20  yOn(db);..  if( 
24141 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
24142 0a 20 20 20 20 72 63 20 3d 20 72 63 73 61 66 65  .    rc = rcsafe
24143 74 79 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ty;.  }.  return
24144 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e   rc;.}../*.** In
24145 76 6f 6b 65 20 74 68 65 20 78 52 6f 6c 6c 62 61  voke the xRollba
24146 63 6b 20 6d 65 74 68 6f 64 20 6f 66 20 61 6c 6c  ck method of all
24147 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20   virtual tables 
24148 69 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74  in the .** sqlit
24149 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61 79  e3.aVTrans array
2414a 2e 20 54 68 65 6e 20 63 6c 65 61 72 20 74 68 65  . Then clear the
2414b 20 61 72 72 61 79 20 69 74 73 65 6c 66 2e 0a 2a   array itself..*
2414c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2414d 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62   int sqlite3Vtab
2414e 52 6f 6c 6c 62 61 63 6b 28 73 71 6c 69 74 65 33  Rollback(sqlite3
2414f 20 2a 64 62 29 7b 0a 20 20 63 61 6c 6c 46 69 6e   *db){.  callFin
24150 61 6c 69 73 65 72 28 64 62 2c 20 6f 66 66 73 65  aliser(db, offse
24151 74 6f 66 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75  tof(sqlite3_modu
24152 6c 65 2c 78 52 6f 6c 6c 62 61 63 6b 29 29 3b 0a  le,xRollback));.
24153 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
24154 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76  OK;.}../*.** Inv
24155 6f 6b 65 20 74 68 65 20 78 43 6f 6d 6d 69 74 20  oke the xCommit 
24156 6d 65 74 68 6f 64 20 6f 66 20 61 6c 6c 20 76 69  method of all vi
24157 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 20  rtual tables in 
24158 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65 33 2e  the .** sqlite3.
24159 61 56 54 72 61 6e 73 20 61 72 72 61 79 2e 20 54  aVTrans array. T
2415a 68 65 6e 20 63 6c 65 61 72 20 74 68 65 20 61 72  hen clear the ar
2415b 72 61 79 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a 53  ray itself..*/.S
2415c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2415d 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f 6d  t sqlite3VtabCom
2415e 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29  mit(sqlite3 *db)
2415f 7b 0a 20 20 63 61 6c 6c 46 69 6e 61 6c 69 73 65  {.  callFinalise
24160 72 28 64 62 2c 20 6f 66 66 73 65 74 6f 66 28 73  r(db, offsetof(s
24161 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2c 78 43  qlite3_module,xC
24162 6f 6d 6d 69 74 29 29 3b 0a 20 20 72 65 74 75 72  ommit));.  retur
24163 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
24164 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76 69 72  /*.** If the vir
24165 74 75 61 6c 20 74 61 62 6c 65 20 70 56 74 61 62  tual table pVtab
24166 20 73 75 70 70 6f 72 74 73 20 74 68 65 20 74 72   supports the tr
24167 61 6e 73 61 63 74 69 6f 6e 20 69 6e 74 65 72 66  ansaction interf
24168 61 63 65 0a 2a 2a 20 28 78 42 65 67 69 6e 2f 78  ace.** (xBegin/x
24169 52 6f 6c 6c 62 61 63 6b 2f 78 43 6f 6d 6d 69 74  Rollback/xCommit
2416a 20 61 6e 64 20 6f 70 74 69 6f 6e 61 6c 6c 79 20   and optionally 
2416b 78 53 79 6e 63 29 20 61 6e 64 20 61 20 74 72 61  xSync) and a tra
2416c 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 6e  nsaction is.** n
2416d 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65  ot currently ope
2416e 6e 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 78 42  n, invoke the xB
2416f 65 67 69 6e 20 6d 65 74 68 6f 64 20 6e 6f 77 2e  egin method now.
24170 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 78 42  .**.** If the xB
24171 65 67 69 6e 20 63 61 6c 6c 20 69 73 20 73 75 63  egin call is suc
24172 63 65 73 73 66 75 6c 2c 20 70 6c 61 63 65 20 74  cessful, place t
24173 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  he sqlite3_vtab 
24174 70 6f 69 6e 74 65 72 0a 2a 2a 20 69 6e 20 74 68  pointer.** in th
24175 65 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61 6e  e sqlite3.aVTran
24176 73 20 61 72 72 61 79 2e 0a 2a 2f 0a 53 51 4c 49  s array..*/.SQLI
24177 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
24178 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 28  qlite3VtabBegin(
24179 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 73 71 6c  sqlite3 *db, sql
2417a 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
2417b 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
2417c 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 6f 6e 73 74  LITE_OK;.  const
2417d 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
2417e 2a 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 2f 2a 20  *pModule;..  /* 
2417f 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20 49 66  Special case: If
24180 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 69 73 20   db->aVTrans is 
24181 4e 55 4c 4c 20 61 6e 64 20 64 62 2d 3e 6e 56 54  NULL and db->nVT
24182 72 61 6e 73 20 69 73 20 67 72 65 61 74 65 72 0a  rans is greater.
24183 20 20 2a 2a 20 74 68 61 6e 20 7a 65 72 6f 2c 20    ** than zero, 
24184 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  then this functi
24185 6f 6e 20 69 73 20 62 65 69 6e 67 20 63 61 6c 6c  on is being call
24186 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61  ed from within a
24187 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f  .  ** virtual mo
24188 64 75 6c 65 20 78 53 79 6e 63 28 29 20 63 61 6c  dule xSync() cal
24189 6c 62 61 63 6b 2e 20 49 74 20 69 73 20 69 6c 6c  lback. It is ill
2418a 65 67 61 6c 20 74 6f 20 77 72 69 74 65 20 74 6f  egal to write to
2418b 20 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d   .  ** virtual m
2418c 6f 64 75 6c 65 20 74 61 62 6c 65 73 20 69 6e 20  odule tables in 
2418d 74 68 69 73 20 63 61 73 65 2c 20 73 6f 20 72 65  this case, so re
2418e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  turn SQLITE_LOCK
2418f 45 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 30  ED..  */.  if( 0
24190 3d 3d 64 62 2d 3e 61 56 54 72 61 6e 73 20 26 26  ==db->aVTrans &&
24191 20 64 62 2d 3e 6e 56 54 72 61 6e 73 3e 30 20 29   db->nVTrans>0 )
24192 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
24193 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 7d 0a  ITE_LOCKED;.  }.
24194 20 20 69 66 28 20 21 70 56 74 61 62 20 29 7b 0a    if( !pVtab ){.
24195 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
24196 45 5f 4f 4b 3b 0a 20 20 7d 20 0a 20 20 70 4d 6f  E_OK;.  } .  pMo
24197 64 75 6c 65 20 3d 20 70 56 74 61 62 2d 3e 70 4d  dule = pVtab->pM
24198 6f 64 75 6c 65 3b 0a 0a 20 20 69 66 28 20 70 4d  odule;..  if( pM
24199 6f 64 75 6c 65 2d 3e 78 42 65 67 69 6e 20 29 7b  odule->xBegin ){
2419a 0a 20 20 20 20 69 6e 74 20 69 3b 0a 0a 0a 20 20  .    int i;...  
2419b 20 20 2f 2a 20 49 66 20 70 56 74 61 62 20 69 73    /* If pVtab is
2419c 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
2419d 61 56 54 72 61 6e 73 20 61 72 72 61 79 2c 20 72  aVTrans array, r
2419e 65 74 75 72 6e 20 65 61 72 6c 79 20 2a 2f 0a 20  eturn early */. 
2419f 20 20 20 66 6f 72 28 69 3d 30 3b 20 28 69 3c 64     for(i=0; (i<d
241a0 62 2d 3e 6e 56 54 72 61 6e 73 29 20 26 26 20 30  b->nVTrans) && 0
241a1 21 3d 64 62 2d 3e 61 56 54 72 61 6e 73 5b 69 5d  !=db->aVTrans[i]
241a2 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
241a3 28 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 69 5d  ( db->aVTrans[i]
241a4 3d 3d 70 56 74 61 62 20 29 7b 0a 20 20 20 20 20  ==pVtab ){.     
241a5 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
241a6 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  _OK;.      }.   
241a7 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 6e 76 6f 6b   }..    /* Invok
241a8 65 20 74 68 65 20 78 42 65 67 69 6e 20 6d 65 74  e the xBegin met
241a9 68 6f 64 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  hod */.    rc = 
241aa 70 4d 6f 64 75 6c 65 2d 3e 78 42 65 67 69 6e 28  pModule->xBegin(
241ab 70 56 74 61 62 29 3b 0a 20 20 20 20 69 66 28 20  pVtab);.    if( 
241ac 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
241ad 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
241ae 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 20  ;.    }..    rc 
241af 3d 20 61 64 64 54 6f 56 54 72 61 6e 73 28 64 62  = addToVTrans(db
241b0 2c 20 70 56 74 61 62 29 3b 0a 20 20 7d 0a 20 20  , pVtab);.  }.  
241b1 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
241b2 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61  .** The first pa
241b3 72 61 6d 65 74 65 72 20 28 70 44 65 66 29 20 69  rameter (pDef) i
241b4 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  s a function imp
241b5 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68  lementation.  Th
241b6 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61  e.** second para
241b7 6d 65 74 65 72 20 28 70 45 78 70 72 29 20 69 73  meter (pExpr) is
241b8 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
241b9 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63  ent to this func
241ba 74 69 6f 6e 2e 0a 2a 2a 20 49 66 20 70 45 78 70  tion..** If pExp
241bb 72 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 69 6e  r is a column in
241bc 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
241bd 2c 20 74 68 65 6e 20 6c 65 74 20 74 68 65 20 76  , then let the v
241be 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20  irtual.** table 
241bf 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 68  implementation h
241c0 61 76 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69  ave an opportuni
241c1 74 79 20 74 6f 20 6f 76 65 72 6c 6f 61 64 20 74  ty to overload t
241c2 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a  he function..**.
241c3 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
241c4 69 73 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 77  is used to allow
241c5 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69   virtual table i
241c6 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 74  mplementations t
241c7 6f 0a 2a 2a 20 6f 76 65 72 6c 6f 61 64 20 4d 41  o.** overload MA
241c8 54 43 48 2c 20 4c 49 4b 45 2c 20 47 4c 4f 42 2c  TCH, LIKE, GLOB,
241c9 20 61 6e 64 20 52 45 47 45 58 50 20 6f 70 65 72   and REGEXP oper
241ca 61 74 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  ators..**.** Ret
241cb 75 72 6e 20 65 69 74 68 65 72 20 74 68 65 20 70  urn either the p
241cc 44 65 66 20 61 72 67 75 6d 65 6e 74 20 28 69 6e  Def argument (in
241cd 64 69 63 61 74 69 6e 67 20 6e 6f 20 63 68 61 6e  dicating no chan
241ce 67 65 29 20 6f 72 20 61 20 0a 2a 2a 20 6e 65 77  ge) or a .** new
241cf 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75   FuncDef structu
241d0 72 65 20 74 68 61 74 20 69 73 20 6d 61 72 6b 65  re that is marke
241d1 64 20 61 73 20 65 70 68 65 6d 65 72 61 6c 20 75  d as ephemeral u
241d2 73 69 6e 67 20 74 68 65 0a 2a 2a 20 53 51 4c 49  sing the.** SQLI
241d3 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d 20 66 6c  TE_FUNC_EPHEM fl
241d4 61 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ag..*/.SQLITE_PR
241d5 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a 73  IVATE FuncDef *s
241d6 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c 6f  qlite3VtabOverlo
241d7 61 64 46 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71  adFunction(.  sq
241d8 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 2f 2a  lite3 *db,    /*
241d9 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
241da 74 69 6f 6e 20 66 6f 72 20 72 65 70 6f 72 74 69  tion for reporti
241db 6e 67 20 6d 61 6c 6c 6f 63 20 70 72 6f 62 6c 65  ng malloc proble
241dc 6d 73 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20  ms */.  FuncDef 
241dd 2a 70 44 65 66 2c 20 20 2f 2a 20 46 75 6e 63 74  *pDef,  /* Funct
241de 69 6f 6e 20 74 6f 20 70 6f 73 73 69 62 6c 79 20  ion to possibly 
241df 6f 76 65 72 6c 6f 61 64 20 2a 2f 0a 20 20 69 6e  overload */.  in
241e0 74 20 6e 41 72 67 2c 20 20 20 20 20 20 20 2f 2a  t nArg,       /*
241e1 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   Number of argum
241e2 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75 6e 63  ents to the func
241e3 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a  tion */.  Expr *
241e4 70 45 78 70 72 20 20 20 20 20 2f 2a 20 46 69 72  pExpr     /* Fir
241e5 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  st argument to t
241e6 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29  he function */.)
241e7 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  {.  Table *pTab;
241e8 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
241e9 2a 70 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 65  *pVtab;.  sqlite
241ea 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 0a  3_module *pMod;.
241eb 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28    void (*xFunc)(
241ec 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
241ed 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
241ee 75 65 2a 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 70  ue**);.  void *p
241ef 41 72 67 3b 0a 20 20 46 75 6e 63 44 65 66 20 2a  Arg;.  FuncDef *
241f0 70 4e 65 77 3b 0a 20 20 69 6e 74 20 72 63 20 3d  pNew;.  int rc =
241f1 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 77   0;.  char *zLow
241f2 65 72 4e 61 6d 65 3b 0a 20 20 75 6e 73 69 67 6e  erName;.  unsign
241f3 65 64 20 63 68 61 72 20 2a 7a 3b 0a 0a 0a 20 20  ed char *z;...  
241f4 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  /* Check to see 
241f5 74 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64  the left operand
241f6 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20   is a column in 
241f7 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
241f8 2a 2f 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d  */.  if( pExpr==
241f9 30 20 29 20 72 65 74 75 72 6e 20 70 44 65 66 3b  0 ) return pDef;
241fa 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70  .  if( pExpr->op
241fb 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20 72 65  !=TK_COLUMN ) re
241fc 74 75 72 6e 20 70 44 65 66 3b 0a 20 20 70 54 61  turn pDef;.  pTa
241fd 62 20 3d 20 70 45 78 70 72 2d 3e 70 54 61 62 3b  b = pExpr->pTab;
241fe 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29  .  if( pTab==0 )
241ff 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 20   return pDef;.  
24200 69 66 28 20 21 70 54 61 62 2d 3e 69 73 56 69 72  if( !pTab->isVir
24201 74 75 61 6c 20 29 20 72 65 74 75 72 6e 20 70 44  tual ) return pD
24202 65 66 3b 0a 20 20 70 56 74 61 62 20 3d 20 70 54  ef;.  pVtab = pT
24203 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 61 73 73  ab->pVtab;.  ass
24204 65 72 74 28 20 70 56 74 61 62 21 3d 30 20 29 3b  ert( pVtab!=0 );
24205 0a 20 20 61 73 73 65 72 74 28 20 70 56 74 61 62  .  assert( pVtab
24206 2d 3e 70 4d 6f 64 75 6c 65 21 3d 30 20 29 3b 0a  ->pModule!=0 );.
24207 20 20 70 4d 6f 64 20 3d 20 28 73 71 6c 69 74 65    pMod = (sqlite
24208 33 5f 6d 6f 64 75 6c 65 20 2a 29 70 56 74 61 62  3_module *)pVtab
24209 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 66 28  ->pModule;.  if(
2420a 20 70 4d 6f 64 2d 3e 78 46 69 6e 64 46 75 6e 63   pMod->xFindFunc
2420b 74 69 6f 6e 3d 3d 30 20 29 20 72 65 74 75 72 6e  tion==0 ) return
2420c 20 70 44 65 66 3b 0a 20 0a 20 20 2f 2a 20 43 61   pDef;. .  /* Ca
2420d 6c 6c 20 74 68 65 20 78 46 69 6e 64 46 75 6e 63  ll the xFindFunc
2420e 74 69 6f 6e 20 6d 65 74 68 6f 64 20 6f 6e 20 74  tion method on t
2420f 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
24210 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a   implementation.
24211 20 20 2a 2a 20 74 6f 20 73 65 65 20 69 66 20 74    ** to see if t
24212 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
24213 6e 20 77 61 6e 74 73 20 74 6f 20 6f 76 65 72 6c  n wants to overl
24214 6f 61 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f  oad this functio
24215 6e 20 0a 20 20 2a 2f 0a 20 20 7a 4c 6f 77 65 72  n .  */.  zLower
24216 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62  Name = sqlite3Db
24217 53 74 72 44 75 70 28 64 62 2c 20 70 44 65 66 2d  StrDup(db, pDef-
24218 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a  >zName);.  if( z
24219 4c 6f 77 65 72 4e 61 6d 65 20 29 7b 0a 20 20 20  LowerName ){.   
2421a 20 66 6f 72 28 7a 3d 28 75 6e 73 69 67 6e 65 64   for(z=(unsigned
2421b 20 63 68 61 72 2a 29 7a 4c 6f 77 65 72 4e 61 6d   char*)zLowerNam
2421c 65 3b 20 2a 7a 3b 20 7a 2b 2b 29 7b 0a 20 20 20  e; *z; z++){.   
2421d 20 20 20 2a 7a 20 3d 20 73 71 6c 69 74 65 33 55     *z = sqlite3U
2421e 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 7a 5d 3b  pperToLower[*z];
2421f 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
24220 70 4d 6f 64 2d 3e 78 46 69 6e 64 46 75 6e 63 74  pMod->xFindFunct
24221 69 6f 6e 28 70 56 74 61 62 2c 20 6e 41 72 67 2c  ion(pVtab, nArg,
24222 20 7a 4c 6f 77 65 72 4e 61 6d 65 2c 20 26 78 46   zLowerName, &xF
24223 75 6e 63 2c 20 26 70 41 72 67 29 3b 0a 20 20 20  unc, &pArg);.   
24224 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4c   sqlite3_free(zL
24225 6f 77 65 72 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20  owerName);.  }. 
24226 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20   if( rc==0 ){.  
24227 20 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20    return pDef;. 
24228 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 20   }..  /* Create 
24229 61 20 6e 65 77 20 65 70 68 65 6d 65 72 61 6c 20  a new ephemeral 
2422a 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74  function definit
2422b 69 6f 6e 20 66 6f 72 20 74 68 65 20 6f 76 65 72  ion for the over
2422c 6c 6f 61 64 65 64 0a 20 20 2a 2a 20 66 75 6e 63  loaded.  ** func
2422d 74 69 6f 6e 20 2a 2f 0a 20 20 70 4e 65 77 20 3d  tion */.  pNew =
2422e 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
2422f 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
24230 2a 70 4e 65 77 29 20 2b 20 73 74 72 6c 65 6e 28  *pNew) + strlen(
24231 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 20 29 3b 0a  pDef->zName) );.
24232 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b    if( pNew==0 ){
24233 0a 20 20 20 20 72 65 74 75 72 6e 20 70 44 65 66  .    return pDef
24234 3b 0a 20 20 7d 0a 20 20 2a 70 4e 65 77 20 3d 20  ;.  }.  *pNew = 
24235 2a 70 44 65 66 3b 0a 20 20 6d 65 6d 63 70 79 28  *pDef;.  memcpy(
24236 70 4e 65 77 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65  pNew->zName, pDe
24237 66 2d 3e 7a 4e 61 6d 65 2c 20 73 74 72 6c 65 6e  f->zName, strlen
24238 28 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 2b 31 29  (pDef->zName)+1)
24239 3b 0a 20 20 70 4e 65 77 2d 3e 78 46 75 6e 63 20  ;.  pNew->xFunc 
2423a 3d 20 78 46 75 6e 63 3b 0a 20 20 70 4e 65 77 2d  = xFunc;.  pNew-
2423b 3e 70 55 73 65 72 44 61 74 61 20 3d 20 70 41 72  >pUserData = pAr
2423c 67 3b 0a 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73  g;.  pNew->flags
2423d 20 7c 3d 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f   |= SQLITE_FUNC_
2423e 45 50 48 45 4d 3b 0a 20 20 72 65 74 75 72 6e 20  EPHEM;.  return 
2423f 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  pNew;.}../*.** M
24240 61 6b 65 20 73 75 72 65 20 76 69 72 74 75 61 6c  ake sure virtual
24241 20 74 61 62 6c 65 20 70 54 61 62 20 69 73 20 63   table pTab is c
24242 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20  ontained in the 
24243 70 50 61 72 73 65 2d 3e 61 70 56 69 72 74 75 61  pParse->apVirtua
24244 6c 4c 6f 63 6b 5b 5d 0a 2a 2a 20 61 72 72 61 79  lLock[].** array
24245 20 73 6f 20 74 68 61 74 20 61 6e 20 4f 50 5f 56   so that an OP_V
24246 42 65 67 69 6e 20 77 69 6c 6c 20 67 65 74 20 67  Begin will get g
24247 65 6e 65 72 61 74 65 64 20 66 6f 72 20 69 74 2e  enerated for it.
24248 20 20 41 64 64 20 70 54 61 62 20 74 6f 20 74 68    Add pTab to th
24249 65 0a 2a 2a 20 61 72 72 61 79 20 69 66 20 69 74  e.** array if it
2424a 20 69 73 20 6d 69 73 73 69 6e 67 2e 20 20 49 66   is missing.  If
2424b 20 70 54 61 62 20 69 73 20 61 6c 72 65 61 64 79   pTab is already
2424c 20 69 6e 20 74 68 65 20 61 72 72 61 79 2c 20 74   in the array, t
2424d 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69  his routine.** i
2424e 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51  s a no-op..*/.SQ
2424f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
24250 64 20 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b  d sqlite3VtabMak
24251 65 57 72 69 74 61 62 6c 65 28 50 61 72 73 65 20  eWritable(Parse 
24252 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a  *pParse, Table *
24253 70 54 61 62 29 7b 0a 20 20 69 6e 74 20 69 2c 20  pTab){.  int i, 
24254 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 49 73 56  n;.  assert( IsV
24255 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 3b 0a  irtual(pTab) );.
24256 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61    for(i=0; i<pPa
24257 72 73 65 2d 3e 6e 56 74 61 62 4c 6f 63 6b 3b 20  rse->nVtabLock; 
24258 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 54  i++){.    if( pT
24259 61 62 3d 3d 70 50 61 72 73 65 2d 3e 61 70 56 74  ab==pParse->apVt
2425a 61 62 4c 6f 63 6b 5b 69 5d 20 29 20 72 65 74 75  abLock[i] ) retu
2425b 72 6e 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20 28 70  rn;.  }.  n = (p
2425c 50 61 72 73 65 2d 3e 6e 56 74 61 62 4c 6f 63 6b  Parse->nVtabLock
2425d 2b 31 29 2a 73 69 7a 65 6f 66 28 70 50 61 72 73  +1)*sizeof(pPars
2425e 65 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 5b 30 5d  e->apVtabLock[0]
2425f 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 61 70 56  );.  pParse->apV
24260 74 61 62 4c 6f 63 6b 20 3d 20 73 71 6c 69 74 65  tabLock = sqlite
24261 33 5f 72 65 61 6c 6c 6f 63 28 70 50 61 72 73 65  3_realloc(pParse
24262 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 2c 20 6e 29  ->apVtabLock, n)
24263 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
24264 61 70 56 74 61 62 4c 6f 63 6b 20 29 7b 0a 20 20  apVtabLock ){.  
24265 20 20 70 50 61 72 73 65 2d 3e 61 70 56 74 61 62    pParse->apVtab
24266 4c 6f 63 6b 5b 70 50 61 72 73 65 2d 3e 6e 56 74  Lock[pParse->nVt
24267 61 62 4c 6f 63 6b 2b 2b 5d 20 3d 20 70 54 61 62  abLock++] = pTab
24268 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
24269 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
2426a 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d  cFailed = 1;.  }
2426b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
2426c 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2426d 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  LTABLE */../****
2426e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
2426f 66 20 76 74 61 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f vtab.c *******
24270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24271 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24272 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
24273 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
24274 20 66 69 6c 65 20 77 68 65 72 65 2e 63 20 2a 2a   file where.c **
24275 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24276 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24277 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
24278 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
24279 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
2427a 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
2427b 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
2427c 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
2427d 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
2427e 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
2427f 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
24280 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
24281 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
24282 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
24283 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
24284 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
24285 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
24286 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
24287 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
24288 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
24289 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
2428a 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
2428b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2428c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2428d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2428e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2428f 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75 6c 65  *.** This module
24290 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65   contains C code
24291 20 74 68 61 74 20 67 65 6e 65 72 61 74 65 73 20   that generates 
24292 56 44 42 45 20 63 6f 64 65 20 75 73 65 64 20 74  VDBE code used t
24293 6f 20 70 72 6f 63 65 73 73 0a 2a 2a 20 74 68 65  o process.** the
24294 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66   WHERE clause of
24295 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e   SQL statements.
24296 20 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 69 73    This module is
24297 20 72 65 70 6f 6e 73 69 62 6c 65 20 66 6f 72 0a   reponsible for.
24298 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67 20 74 68  ** generating th
24299 65 20 63 6f 64 65 20 74 68 61 74 20 6c 6f 6f 70  e code that loop
2429a 73 20 74 68 72 6f 75 67 68 20 61 20 74 61 62 6c  s through a tabl
2429b 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 70  e looking for ap
2429c 70 6c 69 63 61 62 6c 65 0a 2a 2a 20 72 6f 77 73  plicable.** rows
2429d 2e 20 20 49 6e 64 69 63 65 73 20 61 72 65 20 73  .  Indices are s
2429e 65 6c 65 63 74 65 64 20 61 6e 64 20 75 73 65 64  elected and used
2429f 20 74 6f 20 73 70 65 65 64 20 74 68 65 20 73 65   to speed the se
242a0 61 72 63 68 20 77 68 65 6e 20 64 6f 69 6e 67 0a  arch when doing.
242a1 2a 2a 20 73 6f 20 69 73 20 61 70 70 6c 69 63 61  ** so is applica
242a2 62 6c 65 2e 20 20 42 65 63 61 75 73 65 20 74 68  ble.  Because th
242a3 69 73 20 6d 6f 64 75 6c 65 20 69 73 20 72 65 73  is module is res
242a4 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 73 65 6c  ponsible for sel
242a5 65 63 74 69 6e 67 0a 2a 2a 20 69 6e 64 69 63 65  ecting.** indice
242a6 73 2c 20 79 6f 75 20 6d 69 67 68 74 20 61 6c 73  s, you might als
242a7 6f 20 74 68 69 6e 6b 20 6f 66 20 74 68 69 73 20  o think of this 
242a8 6d 6f 64 75 6c 65 20 61 73 20 74 68 65 20 22 71  module as the "q
242a9 75 65 72 79 20 6f 70 74 69 6d 69 7a 65 72 22 2e  uery optimizer".
242aa 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 77 68 65 72  .**.** $Id: wher
242ab 65 2e 63 2c 76 20 31 2e 33 30 32 20 32 30 30 38  e.c,v 1.302 2008
242ac 2f 30 34 2f 31 39 20 31 34 3a 34 30 3a 34 34 20  /04/19 14:40:44 
242ad 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  drh Exp $.*/../*
242ae 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f  .** The number o
242af 66 20 62 69 74 73 20 69 6e 20 61 20 42 69 74 6d  f bits in a Bitm
242b0 61 73 6b 2e 20 20 22 42 4d 53 22 20 6d 65 61 6e  ask.  "BMS" mean
242b1 73 20 22 42 69 74 4d 61 73 6b 20 53 69 7a 65 22  s "BitMask Size"
242b2 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 4d 53  ..*/.#define BMS
242b3 20 20 28 73 69 7a 65 6f 66 28 42 69 74 6d 61 73    (sizeof(Bitmas
242b4 6b 29 2a 38 29 0a 0a 2f 2a 0a 2a 2a 20 54 72 61  k)*8)../*.** Tra
242b5 63 65 20 6f 75 74 70 75 74 20 6d 61 63 72 6f 73  ce output macros
242b6 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .*/.#if defined(
242b7 53 51 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c 20  SQLITE_TEST) || 
242b8 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
242b9 45 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49  EBUG).SQLITE_PRI
242ba 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
242bb 57 68 65 72 65 54 72 61 63 65 20 3d 20 30 3b 0a  WhereTrace = 0;.
242bc 23 20 64 65 66 69 6e 65 20 57 48 45 52 45 54 52  # define WHERETR
242bd 41 43 45 28 58 29 20 20 69 66 28 73 71 6c 69 74  ACE(X)  if(sqlit
242be 65 33 57 68 65 72 65 54 72 61 63 65 29 20 73 71  e3WhereTrace) sq
242bf 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
242c0 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e   X.#else.# defin
242c1 65 20 57 48 45 52 45 54 52 41 43 45 28 58 29 0a  e WHERETRACE(X).
242c2 23 65 6e 64 69 66 0a 0a 2f 2a 20 46 6f 72 77 61  #endif../* Forwa
242c3 72 64 20 72 65 66 65 72 65 6e 63 65 0a 2a 2f 0a  rd reference.*/.
242c4 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57  typedef struct W
242c5 68 65 72 65 43 6c 61 75 73 65 20 57 68 65 72 65  hereClause Where
242c6 43 6c 61 75 73 65 3b 0a 74 79 70 65 64 65 66 20  Clause;.typedef 
242c7 73 74 72 75 63 74 20 45 78 70 72 4d 61 73 6b 53  struct ExprMaskS
242c8 65 74 20 45 78 70 72 4d 61 73 6b 53 65 74 3b 0a  et ExprMaskSet;.
242c9 0a 2f 2a 0a 2a 2a 20 54 68 65 20 71 75 65 72 79  ./*.** The query
242ca 20 67 65 6e 65 72 61 74 6f 72 20 75 73 65 73 20   generator uses 
242cb 61 6e 20 61 72 72 61 79 20 6f 66 20 69 6e 73 74  an array of inst
242cc 61 6e 63 65 73 20 6f 66 20 74 68 69 73 20 73 74  ances of this st
242cd 72 75 63 74 75 72 65 20 74 6f 0a 2a 2a 20 68 65  ructure to.** he
242ce 6c 70 20 69 74 20 61 6e 61 6c 79 7a 65 20 74 68  lp it analyze th
242cf 65 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73  e subexpressions
242d0 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c   of the WHERE cl
242d1 61 75 73 65 2e 20 20 45 61 63 68 20 57 48 45 52  ause.  Each WHER
242d2 45 0a 2a 2a 20 63 6c 61 75 73 65 20 73 75 62 65  E.** clause sube
242d3 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73 65 70  xpression is sep
242d4 61 72 61 74 65 64 20 66 72 6f 6d 20 74 68 65 20  arated from the 
242d5 6f 74 68 65 72 73 20 62 79 20 61 6e 20 41 4e 44  others by an AND
242d6 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a   operator..**.**
242d7 20 41 6c 6c 20 57 68 65 72 65 54 65 72 6d 73 20   All WhereTerms 
242d8 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e  are collected in
242d9 74 6f 20 61 20 73 69 6e 67 6c 65 20 57 68 65 72  to a single Wher
242da 65 43 6c 61 75 73 65 20 73 74 72 75 63 74 75 72  eClause structur
242db 65 2e 20 20 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  e.  .** The foll
242dc 6f 77 69 6e 67 20 69 64 65 6e 74 69 74 79 20 68  owing identity h
242dd 6f 6c 64 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  olds:.**.**     
242de 20 20 20 57 68 65 72 65 54 65 72 6d 2e 70 57 43     WhereTerm.pWC
242df 2d 3e 61 5b 57 68 65 72 65 54 65 72 6d 2e 69 64  ->a[WhereTerm.id
242e0 78 5d 20 3d 3d 20 57 68 65 72 65 54 65 72 6d 0a  x] == WhereTerm.
242e1 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 74 65 72  **.** When a ter
242e2 6d 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  m is of the form
242e3 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  :.**.**         
242e4 20 20 20 20 20 58 20 3c 6f 70 3e 20 3c 65 78 70       X <op> <exp
242e5 72 3e 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 58  r>.**.** where X
242e6 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d   is a column nam
242e7 65 20 61 6e 64 20 3c 6f 70 3e 20 69 73 20 6f 6e  e and <op> is on
242e8 65 20 6f 66 20 63 65 72 74 61 69 6e 20 6f 70 65  e of certain ope
242e9 72 61 74 6f 72 73 2c 0a 2a 2a 20 74 68 65 6e 20  rators,.** then 
242ea 57 68 65 72 65 54 65 72 6d 2e 6c 65 66 74 43 75  WhereTerm.leftCu
242eb 72 73 6f 72 20 61 6e 64 20 57 68 65 72 65 54 65  rsor and WhereTe
242ec 72 6d 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20 72 65  rm.leftColumn re
242ed 63 6f 72 64 20 74 68 65 0a 2a 2a 20 63 75 72 73  cord the.** curs
242ee 6f 72 20 6e 75 6d 62 65 72 20 61 6e 64 20 63 6f  or number and co
242ef 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20  lumn number for 
242f0 58 2e 20 20 57 68 65 72 65 54 65 72 6d 2e 6f 70  X.  WhereTerm.op
242f1 65 72 61 74 6f 72 20 72 65 63 6f 72 64 73 0a 2a  erator records.*
242f2 2a 20 74 68 65 20 3c 6f 70 3e 20 75 73 69 6e 67  * the <op> using
242f3 20 61 20 62 69 74 6d 61 73 6b 20 65 6e 63 6f 64   a bitmask encod
242f4 69 6e 67 20 64 65 66 69 6e 65 64 20 62 79 20 57  ing defined by W
242f5 4f 5f 78 78 78 20 62 65 6c 6f 77 2e 20 20 54 68  O_xxx below.  Th
242f6 65 0a 2a 2a 20 75 73 65 20 6f 66 20 61 20 62 69  e.** use of a bi
242f7 74 6d 61 73 6b 20 65 6e 63 6f 64 69 6e 67 20 66  tmask encoding f
242f8 6f 72 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20  or the operator 
242f9 61 6c 6c 6f 77 73 20 75 73 20 74 6f 20 73 65 61  allows us to sea
242fa 72 63 68 0a 2a 2a 20 71 75 69 63 6b 6c 79 20 66  rch.** quickly f
242fb 6f 72 20 74 65 72 6d 73 20 74 68 61 74 20 6d 61  or terms that ma
242fc 74 63 68 20 61 6e 79 20 6f 66 20 73 65 76 65 72  tch any of sever
242fd 61 6c 20 64 69 66 66 65 72 65 6e 74 20 6f 70 65  al different ope
242fe 72 61 74 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 70 72  rators..**.** pr
242ff 65 72 65 71 52 69 67 68 74 20 61 6e 64 20 70 72  ereqRight and pr
24300 65 72 65 71 41 6c 6c 20 72 65 63 6f 72 64 20 73  ereqAll record s
24301 65 74 73 20 6f 66 20 63 75 72 73 6f 72 20 6e 75  ets of cursor nu
24302 6d 62 65 72 73 2c 0a 2a 2a 20 62 75 74 20 74 68  mbers,.** but th
24303 65 79 20 64 6f 20 73 6f 20 69 6e 64 69 72 65 63  ey do so indirec
24304 74 6c 79 2e 20 20 41 20 73 69 6e 67 6c 65 20 45  tly.  A single E
24305 78 70 72 4d 61 73 6b 53 65 74 20 73 74 72 75 63  xprMaskSet struc
24306 74 75 72 65 20 74 72 61 6e 73 6c 61 74 65 73 0a  ture translates.
24307 2a 2a 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  ** cursor number
24308 20 69 6e 74 6f 20 62 69 74 73 20 61 6e 64 20 74   into bits and t
24309 68 65 20 74 72 61 6e 73 6c 61 74 65 64 20 62 69  he translated bi
2430a 74 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74  t is stored in t
2430b 68 65 20 70 72 65 72 65 71 0a 2a 2a 20 66 69 65  he prereq.** fie
2430c 6c 64 73 2e 20 20 54 68 65 20 74 72 61 6e 73 6c  lds.  The transl
2430d 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e  ation is used in
2430e 20 6f 72 64 65 72 20 74 6f 20 6d 61 78 69 6d 69   order to maximi
2430f 7a 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ze the number of
24310 0a 2a 2a 20 62 69 74 73 20 74 68 61 74 20 77 69  .** bits that wi
24311 6c 6c 20 66 69 74 20 69 6e 20 61 20 42 69 74 6d  ll fit in a Bitm
24312 61 73 6b 2e 20 20 54 68 65 20 56 44 42 45 20 63  ask.  The VDBE c
24313 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 6d 69  ursor numbers mi
24314 67 68 74 20 62 65 0a 2a 2a 20 73 70 72 65 61 64  ght be.** spread
24315 20 6f 75 74 20 6f 76 65 72 20 74 68 65 20 6e 6f   out over the no
24316 6e 2d 6e 65 67 61 74 69 76 65 20 69 6e 74 65 67  n-negative integ
24317 65 72 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c  ers.  For exampl
24318 65 2c 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a  e, the cursor.**
24319 20 6e 75 6d 62 65 72 73 20 6d 69 67 68 74 20 62   numbers might b
2431a 65 20 33 2c 20 38 2c 20 39 2c 20 31 30 2c 20 32  e 3, 8, 9, 10, 2
2431b 30 2c 20 32 33 2c 20 34 31 2c 20 61 6e 64 20 34  0, 23, 41, and 4
2431c 35 2e 20 20 54 68 65 20 45 78 70 72 4d 61 73 6b  5.  The ExprMask
2431d 53 65 74 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 65  Set.** translate
2431e 73 20 74 68 65 73 65 20 73 70 61 72 73 65 20 63  s these sparse c
2431f 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e  ursor numbers in
24320 74 6f 20 63 6f 6e 73 65 63 75 74 69 76 65 20 69  to consecutive i
24321 6e 74 65 67 65 72 73 0a 2a 2a 20 62 65 67 69 6e  ntegers.** begin
24322 6e 69 6e 67 20 77 69 74 68 20 30 20 69 6e 20 6f  ning with 0 in o
24323 72 64 65 72 20 74 6f 20 6d 61 6b 65 20 74 68 65  rder to make the
24324 20 62 65 73 74 20 70 6f 73 73 69 62 6c 65 20 75   best possible u
24325 73 65 20 6f 66 20 74 68 65 20 61 76 61 69 6c 61  se of the availa
24326 62 6c 65 0a 2a 2a 20 62 69 74 73 20 69 6e 20 74  ble.** bits in t
24327 68 65 20 42 69 74 6d 61 73 6b 2e 20 20 53 6f 2c  he Bitmask.  So,
24328 20 69 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 20   in the example 
24329 61 62 6f 76 65 2c 20 74 68 65 20 63 75 72 73 6f  above, the curso
2432a 72 20 6e 75 6d 62 65 72 73 0a 2a 2a 20 77 6f 75  r numbers.** wou
2432b 6c 64 20 62 65 20 6d 61 70 70 65 64 20 69 6e 74  ld be mapped int
2432c 6f 20 69 6e 74 65 67 65 72 73 20 30 20 74 68 72  o integers 0 thr
2432d 6f 75 67 68 20 37 2e 0a 2a 2f 0a 74 79 70 65 64  ough 7..*/.typed
2432e 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65 54  ef struct WhereT
2432f 65 72 6d 20 57 68 65 72 65 54 65 72 6d 3b 0a 73  erm WhereTerm;.s
24330 74 72 75 63 74 20 57 68 65 72 65 54 65 72 6d 20  truct WhereTerm 
24331 7b 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b  {.  Expr *pExpr;
24332 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
24333 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73 75  ointer to the su
24334 62 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20  bexpression */. 
24335 20 69 31 36 20 69 50 61 72 65 6e 74 3b 20 20 20   i16 iParent;   
24336 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 73 61           /* Disa
24337 62 6c 65 20 70 57 43 2d 3e 61 5b 69 50 61 72 65  ble pWC->a[iPare
24338 6e 74 5d 20 77 68 65 6e 20 74 68 69 73 20 74 65  nt] when this te
24339 72 6d 20 64 69 73 61 62 6c 65 64 20 2a 2f 0a 20  rm disabled */. 
2433a 20 69 31 36 20 6c 65 66 74 43 75 72 73 6f 72 3b   i16 leftCursor;
2433b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73           /* Curs
2433c 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 58 20 69  or number of X i
2433d 6e 20 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e  n "X <op> <expr>
2433e 22 20 2a 2f 0a 20 20 69 31 36 20 6c 65 66 74 43  " */.  i16 leftC
2433f 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f  olumn;         /
24340 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20  * Column number 
24341 6f 66 20 58 20 69 6e 20 22 58 20 3c 6f 70 3e 20  of X in "X <op> 
24342 3c 65 78 70 72 3e 22 20 2a 2f 0a 20 20 75 31 36  <expr>" */.  u16
24343 20 65 4f 70 65 72 61 74 6f 72 3b 20 20 20 20 20   eOperator;     
24344 20 20 20 20 20 2f 2a 20 41 20 57 4f 5f 78 78 20       /* A WO_xx 
24345 76 61 6c 75 65 20 64 65 73 63 72 69 62 69 6e 67  value describing
24346 20 3c 6f 70 3e 20 2a 2f 0a 20 20 75 38 20 66 6c   <op> */.  u8 fl
24347 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ags;            
24348 20 20 20 2f 2a 20 42 69 74 20 66 6c 61 67 73 2e     /* Bit flags.
24349 20 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0a 20    See below */. 
2434a 20 75 38 20 6e 43 68 69 6c 64 3b 20 20 20 20 20   u8 nChild;     
2434b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
2434c 65 72 20 6f 66 20 63 68 69 6c 64 72 65 6e 20 74  er of children t
2434d 68 61 74 20 6d 75 73 74 20 64 69 73 61 62 6c 65  hat must disable
2434e 20 75 73 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c   us */.  WhereCl
2434f 61 75 73 65 20 2a 70 57 43 3b 20 20 20 20 20 20  ause *pWC;      
24350 20 2f 2a 20 54 68 65 20 63 6c 61 75 73 65 20 74   /* The clause t
24351 68 69 73 20 74 65 72 6d 20 69 73 20 70 61 72 74  his term is part
24352 20 6f 66 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b   of */.  Bitmask
24353 20 70 72 65 72 65 71 52 69 67 68 74 3b 20 20 20   prereqRight;   
24354 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 74   /* Bitmask of t
24355 61 62 6c 65 73 20 75 73 65 64 20 62 79 20 70 52  ables used by pR
24356 69 67 68 74 20 2a 2f 0a 20 20 42 69 74 6d 61 73  ight */.  Bitmas
24357 6b 20 70 72 65 72 65 71 41 6c 6c 3b 20 20 20 20  k prereqAll;    
24358 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20    /* Bitmask of 
24359 74 61 62 6c 65 73 20 72 65 66 65 72 65 6e 63 65  tables reference
2435a 64 20 62 79 20 70 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  d by p */.};../*
2435b 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75  .** Allowed valu
2435c 65 73 20 6f 66 20 57 68 65 72 65 54 65 72 6d 2e  es of WhereTerm.
2435d 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65  flags.*/.#define
2435e 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43 20 20 20   TERM_DYNAMIC   
2435f 20 30 78 30 31 20 20 20 2f 2a 20 4e 65 65 64 20   0x01   /* Need 
24360 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 45  to call sqlite3E
24361 78 70 72 44 65 6c 65 74 65 28 70 45 78 70 72 29  xprDelete(pExpr)
24362 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52 4d   */.#define TERM
24363 5f 56 49 52 54 55 41 4c 20 20 20 20 30 78 30 32  _VIRTUAL    0x02
24364 20 20 20 2f 2a 20 41 64 64 65 64 20 62 79 20 74     /* Added by t
24365 68 65 20 6f 70 74 69 6d 69 7a 65 72 2e 20 20 44  he optimizer.  D
24366 6f 20 6e 6f 74 20 63 6f 64 65 20 2a 2f 0a 23 64  o not code */.#d
24367 65 66 69 6e 65 20 54 45 52 4d 5f 43 4f 44 45 44  efine TERM_CODED
24368 20 20 20 20 20 20 30 78 30 34 20 20 20 2f 2a 20        0x04   /* 
24369 54 68 69 73 20 74 65 72 6d 20 69 73 20 61 6c 72  This term is alr
2436a 65 61 64 79 20 63 6f 64 65 64 20 2a 2f 0a 23 64  eady coded */.#d
2436b 65 66 69 6e 65 20 54 45 52 4d 5f 43 4f 50 49 45  efine TERM_COPIE
2436c 44 20 20 20 20 20 30 78 30 38 20 20 20 2f 2a 20  D     0x08   /* 
2436d 48 61 73 20 61 20 63 68 69 6c 64 20 2a 2f 0a 23  Has a child */.#
2436e 64 65 66 69 6e 65 20 54 45 52 4d 5f 4f 52 5f 4f  define TERM_OR_O
2436f 4b 20 20 20 20 20 20 30 78 31 30 20 20 20 2f 2a  K      0x10   /*
24370 20 55 73 65 64 20 64 75 72 69 6e 67 20 4f 52 2d   Used during OR-
24371 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e  clause processin
24372 67 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  g */../*.** An i
24373 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
24374 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
24375 72 65 20 68 6f 6c 64 73 20 61 6c 6c 20 69 6e 66  re holds all inf
24376 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61  ormation about a
24377 0a 2a 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65  .** WHERE clause
24378 2e 20 20 4d 6f 73 74 6c 79 20 74 68 69 73 20 69  .  Mostly this i
24379 73 20 61 20 63 6f 6e 74 61 69 6e 65 72 20 66 6f  s a container fo
2437a 72 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 57 68  r one or more Wh
2437b 65 72 65 54 65 72 6d 73 2e 0a 2a 2f 0a 73 74 72  ereTerms..*/.str
2437c 75 63 74 20 57 68 65 72 65 43 6c 61 75 73 65 20  uct WhereClause 
2437d 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  {.  Parse *pPars
2437e 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e;           /* 
2437f 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65  The parser conte
24380 78 74 20 2a 2f 0a 20 20 45 78 70 72 4d 61 73 6b  xt */.  ExprMask
24381 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 3b 20 20  Set *pMaskSet;  
24382 20 2f 2a 20 4d 61 70 70 69 6e 67 20 6f 66 20 74   /* Mapping of t
24383 61 62 6c 65 20 69 6e 64 69 63 65 73 20 74 6f 20  able indices to 
24384 62 69 74 6d 61 73 6b 73 20 2a 2f 0a 20 20 69 6e  bitmasks */.  in
24385 74 20 6e 54 65 72 6d 3b 20 20 20 20 20 20 20 20  t nTerm;        
24386 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
24387 20 6f 66 20 74 65 72 6d 73 20 2a 2f 0a 20 20 69   of terms */.  i
24388 6e 74 20 6e 53 6c 6f 74 3b 20 20 20 20 20 20 20  nt nSlot;       
24389 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
2438a 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
2438b 61 5b 5d 20 2a 2f 0a 20 20 57 68 65 72 65 54 65  a[] */.  WhereTe
2438c 72 6d 20 2a 61 3b 20 20 20 20 20 20 20 20 20 20  rm *a;          
2438d 20 20 2f 2a 20 45 61 63 68 20 61 5b 5d 20 64 65    /* Each a[] de
2438e 73 63 72 69 62 65 73 20 61 20 74 65 72 6d 20 6f  scribes a term o
2438f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 75 61  f the WHERE clua
24390 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72  se */.  WhereTer
24391 6d 20 61 53 74 61 74 69 63 5b 31 30 5d 3b 20 20  m aStatic[10];  
24392 20 2f 2a 20 49 6e 69 74 69 61 6c 20 73 74 61 74   /* Initial stat
24393 69 63 20 73 70 61 63 65 20 66 6f 72 20 61 5b 5d  ic space for a[]
24394 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   */.};../*.** An
24395 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
24396 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
24397 74 75 72 65 20 6b 65 65 70 73 20 74 72 61 63 6b  ture keeps track
24398 20 6f 66 20 61 20 6d 61 70 70 69 6e 67 0a 2a 2a   of a mapping.**
24399 20 62 65 74 77 65 65 6e 20 56 44 42 45 20 63 75   between VDBE cu
2439a 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 61 6e 64  rsor numbers and
2439b 20 62 69 74 73 20 6f 66 20 74 68 65 20 62 69 74   bits of the bit
2439c 6d 61 73 6b 73 20 69 6e 20 57 68 65 72 65 54 65  masks in WhereTe
2439d 72 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 44  rm..**.** The VD
2439e 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  BE cursor number
2439f 73 20 61 72 65 20 73 6d 61 6c 6c 20 69 6e 74 65  s are small inte
243a0 67 65 72 73 20 63 6f 6e 74 61 69 6e 65 64 20 69  gers contained i
243a1 6e 20 0a 2a 2a 20 53 72 63 4c 69 73 74 5f 69 74  n .** SrcList_it
243a2 65 6d 2e 69 43 75 72 73 6f 72 20 61 6e 64 20 45  em.iCursor and E
243a3 78 70 72 2e 69 54 61 62 6c 65 20 66 69 65 6c 64  xpr.iTable field
243a4 73 2e 20 20 46 6f 72 20 61 6e 79 20 67 69 76 65  s.  For any give
243a5 6e 20 57 48 45 52 45 20 0a 2a 2a 20 63 6c 61 75  n WHERE .** clau
243a6 73 65 2c 20 74 68 65 20 63 75 72 73 6f 72 20 6e  se, the cursor n
243a7 75 6d 62 65 72 73 20 6d 69 67 68 74 20 6e 6f 74  umbers might not
243a8 20 62 65 67 69 6e 20 77 69 74 68 20 30 20 61 6e   begin with 0 an
243a9 64 20 74 68 65 79 20 6d 69 67 68 74 0a 2a 2a 20  d they might.** 
243aa 63 6f 6e 74 61 69 6e 20 67 61 70 73 20 69 6e 20  contain gaps in 
243ab 74 68 65 20 6e 75 6d 62 65 72 69 6e 67 20 73 65  the numbering se
243ac 71 75 65 6e 63 65 2e 20 20 42 75 74 20 77 65 20  quence.  But we 
243ad 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 6d 61 78  want to make max
243ae 69 6d 75 6d 0a 2a 2a 20 75 73 65 20 6f 66 20 74  imum.** use of t
243af 68 65 20 62 69 74 73 20 69 6e 20 6f 75 72 20 62  he bits in our b
243b0 69 74 6d 61 73 6b 73 2e 20 20 54 68 69 73 20 73  itmasks.  This s
243b1 74 72 75 63 74 75 72 65 20 70 72 6f 76 69 64 65  tructure provide
243b2 73 20 61 20 6d 61 70 70 69 6e 67 0a 2a 2a 20 66  s a mapping.** f
243b3 72 6f 6d 20 74 68 65 20 73 70 61 72 73 65 20 63  rom the sparse c
243b4 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e  ursor numbers in
243b5 74 6f 20 63 6f 6e 73 65 63 75 74 69 76 65 20 69  to consecutive i
243b6 6e 74 65 67 65 72 73 20 62 65 67 69 6e 6e 69 6e  ntegers beginnin
243b7 67 0a 2a 2a 20 77 69 74 68 20 30 2e 0a 2a 2a 0a  g.** with 0..**.
243b8 2a 2a 20 49 66 20 45 78 70 72 4d 61 73 6b 53 65  ** If ExprMaskSe
243b9 74 2e 69 78 5b 41 5d 3d 3d 42 20 69 74 20 6d 65  t.ix[A]==B it me
243ba 61 6e 73 20 74 68 61 74 20 54 68 65 20 41 2d 74  ans that The A-t
243bb 68 20 62 69 74 20 6f 66 20 61 20 42 69 74 6d 61  h bit of a Bitma
243bc 73 6b 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64  sk.** correspond
243bd 73 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75  s VDBE cursor nu
243be 6d 62 65 72 20 42 2e 20 20 54 68 65 20 41 2d 74  mber B.  The A-t
243bf 68 20 62 69 74 20 6f 66 20 61 20 62 69 74 6d 61  h bit of a bitma
243c0 73 6b 20 69 73 20 31 3c 3c 41 2e 0a 2a 2a 0a 2a  sk is 1<<A..**.*
243c1 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69  * For example, i
243c2 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  f the WHERE clau
243c3 73 65 20 65 78 70 72 65 73 73 69 6f 6e 20 75 73  se expression us
243c4 65 64 20 74 68 65 73 65 20 56 44 42 45 0a 2a 2a  ed these VDBE.**
243c5 20 63 75 72 73 6f 72 73 3a 20 20 34 2c 20 35 2c   cursors:  4, 5,
243c6 20 38 2c 20 32 39 2c 20 35 37 2c 20 37 33 2e 20   8, 29, 57, 73. 
243c7 20 54 68 65 6e 20 74 68 65 20 20 45 78 70 72 4d   Then the  ExprM
243c8 61 73 6b 53 65 74 20 73 74 72 75 63 74 75 72 65  askSet structure
243c9 0a 2a 2a 20 77 6f 75 6c 64 20 6d 61 70 20 74 68  .** would map th
243ca 6f 73 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  ose cursor numbe
243cb 72 73 20 69 6e 74 6f 20 62 69 74 73 20 30 20 74  rs into bits 0 t
243cc 68 72 6f 75 67 68 20 35 2e 0a 2a 2a 0a 2a 2a 20  hrough 5..**.** 
243cd 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 6d 61  Note that the ma
243ce 70 70 69 6e 67 20 69 73 20 6e 6f 74 20 6e 65 63  pping is not nec
243cf 65 73 73 61 72 69 6c 79 20 6f 72 64 65 72 65 64  essarily ordered
243d0 2e 20 20 49 6e 20 74 68 65 20 65 78 61 6d 70 6c  .  In the exampl
243d1 65 0a 2a 2a 20 61 62 6f 76 65 2c 20 74 68 65 20  e.** above, the 
243d2 6d 61 70 70 69 6e 67 20 6d 69 67 68 74 20 67 6f  mapping might go
243d3 20 6c 69 6b 65 20 74 68 69 73 3a 20 20 34 2d 3e   like this:  4->
243d4 33 2c 20 35 2d 3e 31 2c 20 38 2d 3e 32 2c 20 32  3, 5->1, 8->2, 2
243d5 39 2d 3e 30 2c 0a 2a 2a 20 35 37 2d 3e 35 2c 20  9->0,.** 57->5, 
243d6 37 33 2d 3e 34 2e 20 20 4f 72 20 6f 6e 65 20 6f  73->4.  Or one o
243d7 66 20 37 31 39 20 6f 74 68 65 72 20 63 6f 6d 62  f 719 other comb
243d8 69 6e 61 74 69 6f 6e 73 20 6d 69 67 68 74 20 62  inations might b
243d9 65 20 75 73 65 64 2e 20 49 74 0a 2a 2a 20 64 6f  e used. It.** do
243da 65 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6d 61  es not really ma
243db 74 74 65 72 2e 20 20 57 68 61 74 20 69 73 20 69  tter.  What is i
243dc 6d 70 6f 72 74 61 6e 74 20 69 73 20 74 68 61 74  mportant is that
243dd 20 73 70 61 72 73 65 20 63 75 72 73 6f 72 0a 2a   sparse cursor.*
243de 2a 20 6e 75 6d 62 65 72 73 20 61 6c 6c 20 67 65  * numbers all ge
243df 74 20 6d 61 70 70 65 64 20 69 6e 74 6f 20 62 69  t mapped into bi
243e0 74 20 6e 75 6d 62 65 72 73 20 74 68 61 74 20 62  t numbers that b
243e1 65 67 69 6e 20 77 69 74 68 20 30 20 61 6e 64 20  egin with 0 and 
243e2 63 6f 6e 74 61 69 6e 0a 2a 2a 20 6e 6f 20 67 61  contain.** no ga
243e3 70 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78  ps..*/.struct Ex
243e4 70 72 4d 61 73 6b 53 65 74 20 7b 0a 20 20 69 6e  prMaskSet {.  in
243e5 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  t n;            
243e6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
243e7 75 6d 62 65 72 20 6f 66 20 61 73 73 69 67 6e 65  umber of assigne
243e8 64 20 63 75 72 73 6f 72 20 76 61 6c 75 65 73 20  d cursor values 
243e9 2a 2f 0a 20 20 69 6e 74 20 69 78 5b 73 69 7a 65  */.  int ix[size
243ea 6f 66 28 42 69 74 6d 61 73 6b 29 2a 38 5d 3b 20  of(Bitmask)*8]; 
243eb 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 61 73 73     /* Cursor ass
243ec 69 67 6e 65 64 20 74 6f 20 65 61 63 68 20 62 69  igned to each bi
243ed 74 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20  t */.};.../*.** 
243ee 42 69 74 6d 61 73 6b 73 20 66 6f 72 20 74 68 65  Bitmasks for the
243ef 20 6f 70 65 72 61 74 6f 72 73 20 74 68 61 74 20   operators that 
243f0 69 6e 64 69 63 65 73 20 61 72 65 20 61 62 6c 65  indices are able
243f1 20 74 6f 20 65 78 70 6c 6f 69 74 2e 20 20 41 6e   to exploit.  An
243f2 0a 2a 2a 20 4f 52 2d 65 64 20 63 6f 6d 62 69 6e  .** OR-ed combin
243f3 61 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 76  ation of these v
243f4 61 6c 75 65 73 20 63 61 6e 20 62 65 20 75 73 65  alues can be use
243f5 64 20 77 68 65 6e 20 73 65 61 72 63 68 69 6e 67  d when searching
243f6 20 66 6f 72 0a 2a 2a 20 74 65 72 6d 73 20 69 6e   for.** terms in
243f7 20 74 68 65 20 77 68 65 72 65 20 63 6c 61 75 73   the where claus
243f8 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 4f  e..*/.#define WO
243f9 5f 49 4e 20 20 20 20 20 31 0a 23 64 65 66 69 6e  _IN     1.#defin
243fa 65 20 57 4f 5f 45 51 20 20 20 20 20 32 0a 23 64  e WO_EQ     2.#d
243fb 65 66 69 6e 65 20 57 4f 5f 4c 54 20 20 20 20 20  efine WO_LT     
243fc 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f 4c 54 2d 54  (WO_EQ<<(TK_LT-T
243fd 4b 5f 45 51 29 29 0a 23 64 65 66 69 6e 65 20 57  K_EQ)).#define W
243fe 4f 5f 4c 45 20 20 20 20 20 28 57 4f 5f 45 51 3c  O_LE     (WO_EQ<
243ff 3c 28 54 4b 5f 4c 45 2d 54 4b 5f 45 51 29 29 0a  <(TK_LE-TK_EQ)).
24400 23 64 65 66 69 6e 65 20 57 4f 5f 47 54 20 20 20  #define WO_GT   
24401 20 20 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f 47 54    (WO_EQ<<(TK_GT
24402 2d 54 4b 5f 45 51 29 29 0a 23 64 65 66 69 6e 65  -TK_EQ)).#define
24403 20 57 4f 5f 47 45 20 20 20 20 20 28 57 4f 5f 45   WO_GE     (WO_E
24404 51 3c 3c 28 54 4b 5f 47 45 2d 54 4b 5f 45 51 29  Q<<(TK_GE-TK_EQ)
24405 29 0a 23 64 65 66 69 6e 65 20 57 4f 5f 4d 41 54  ).#define WO_MAT
24406 43 48 20 20 36 34 0a 23 64 65 66 69 6e 65 20 57  CH  64.#define W
24407 4f 5f 49 53 4e 55 4c 4c 20 31 32 38 0a 0a 2f 2a  O_ISNULL 128../*
24408 0a 2a 2a 20 56 61 6c 75 65 20 66 6f 72 20 66 6c  .** Value for fl
24409 61 67 73 20 72 65 74 75 72 6e 65 64 20 62 79 20  ags returned by 
2440a 62 65 73 74 49 6e 64 65 78 28 29 2e 20 20 0a 2a  bestIndex().  .*
2440b 2a 0a 2a 2a 20 54 68 65 20 6c 65 61 73 74 20 73  *.** The least s
2440c 69 67 6e 69 66 69 63 61 6e 74 20 62 79 74 65 20  ignificant byte 
2440d 69 73 20 72 65 73 65 72 76 65 64 20 61 73 20 61  is reserved as a
2440e 20 6d 61 73 6b 20 66 6f 72 20 57 4f 5f 20 76 61   mask for WO_ va
2440f 6c 75 65 73 20 61 62 6f 76 65 2e 0a 2a 2a 20 54  lues above..** T
24410 68 65 20 57 68 65 72 65 4c 65 76 65 6c 2e 66 6c  he WhereLevel.fl
24411 61 67 73 20 66 69 65 6c 64 20 69 73 20 75 73 75  ags field is usu
24412 61 6c 6c 79 20 73 65 74 20 74 6f 20 57 4f 5f 49  ally set to WO_I
24413 4e 7c 57 4f 5f 45 51 7c 57 4f 5f 49 53 4e 55 4c  N|WO_EQ|WO_ISNUL
24414 4c 2e 0a 2a 2a 20 42 75 74 20 69 66 20 74 68 65  L..** But if the
24415 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 72 69   table is the ri
24416 67 68 74 20 74 61 62 6c 65 20 6f 66 20 61 20 6c  ght table of a l
24417 65 66 74 20 6a 6f 69 6e 2c 20 57 68 65 72 65 4c  eft join, WhereL
24418 65 76 65 6c 2e 66 6c 61 67 73 0a 2a 2a 20 69 73  evel.flags.** is
24419 20 73 65 74 20 74 6f 20 57 4f 5f 49 4e 7c 57 4f   set to WO_IN|WO
2441a 5f 45 51 2e 20 20 54 68 65 20 57 68 65 72 65 4c  _EQ.  The WhereL
2441b 65 76 65 6c 2e 66 6c 61 67 73 20 66 69 65 6c 64  evel.flags field
2441c 20 63 61 6e 20 74 68 65 6e 20 62 65 20 75 73 65   can then be use
2441d 64 20 61 73 0a 2a 2a 20 74 68 65 20 22 6f 70 22  d as.** the "op"
2441e 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 66 69   parameter to fi
2441f 6e 64 54 65 72 6d 20 77 68 65 6e 20 77 65 20 61  ndTerm when we a
24420 72 65 20 72 65 73 6f 6c 76 69 6e 67 20 65 71 75  re resolving equ
24421 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
24422 73 2e 0a 2a 2a 20 49 53 4e 55 4c 4c 20 63 6f 6e  s..** ISNULL con
24423 73 74 72 61 69 6e 74 73 20 77 69 6c 6c 20 74 68  straints will th
24424 65 6e 20 6e 6f 74 20 62 65 20 75 73 65 64 20 6f  en not be used o
24425 6e 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c  n the right tabl
24426 65 20 6f 66 20 61 20 6c 65 66 74 0a 2a 2a 20 6a  e of a left.** j
24427 6f 69 6e 2e 20 20 54 69 63 6b 65 74 73 20 23 32  oin.  Tickets #2
24428 31 37 37 20 61 6e 64 20 23 32 31 38 39 2e 0a 2a  177 and #2189..*
24429 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
2442a 52 4f 57 49 44 5f 45 51 20 20 20 20 20 30 78 30  ROWID_EQ     0x0
2442b 30 30 31 30 30 20 20 20 2f 2a 20 72 6f 77 69 64  00100   /* rowid
2442c 3d 45 58 50 52 20 6f 72 20 72 6f 77 69 64 20 49  =EXPR or rowid I
2442d 4e 20 28 2e 2e 2e 29 20 2a 2f 0a 23 64 65 66 69  N (...) */.#defi
2442e 6e 65 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 52  ne WHERE_ROWID_R
2442f 41 4e 47 45 20 20 30 78 30 30 30 32 30 30 20 20  ANGE  0x000200  
24430 20 2f 2a 20 72 6f 77 69 64 3c 45 58 50 52 20 61   /* rowid<EXPR a
24431 6e 64 2f 6f 72 20 72 6f 77 69 64 3e 45 58 50 52  nd/or rowid>EXPR
24432 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52   */.#define WHER
24433 45 5f 43 4f 4c 55 4d 4e 5f 45 51 20 20 20 20 30  E_COLUMN_EQ    0
24434 78 30 30 31 30 30 30 20 20 20 2f 2a 20 78 3d 45  x001000   /* x=E
24435 58 50 52 20 6f 72 20 78 20 49 4e 20 28 2e 2e 2e  XPR or x IN (...
24436 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  ) */.#define WHE
24437 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45 20  RE_COLUMN_RANGE 
24438 30 78 30 30 32 30 30 30 20 20 20 2f 2a 20 78 3c  0x002000   /* x<
24439 45 58 50 52 20 61 6e 64 2f 6f 72 20 78 3e 45 58  EXPR and/or x>EX
2443a 50 52 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48  PR */.#define WH
2443b 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 20 20 20  ERE_COLUMN_IN   
2443c 20 30 78 30 30 34 30 30 30 20 20 20 2f 2a 20 78   0x004000   /* x
2443d 20 49 4e 20 28 2e 2e 2e 29 20 2a 2f 0a 23 64 65   IN (...) */.#de
2443e 66 69 6e 65 20 57 48 45 52 45 5f 54 4f 50 5f 4c  fine WHERE_TOP_L
2443f 49 4d 49 54 20 20 20 20 30 78 30 31 30 30 30 30  IMIT    0x010000
24440 20 20 20 2f 2a 20 78 3c 45 58 50 52 20 6f 72 20     /* x<EXPR or 
24441 78 3c 3d 45 58 50 52 20 63 6f 6e 73 74 72 61 69  x<=EXPR constrai
24442 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48  nt */.#define WH
24443 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 20 20 20  ERE_BTM_LIMIT   
24444 20 30 78 30 32 30 30 30 30 20 20 20 2f 2a 20 78   0x020000   /* x
24445 3e 45 58 50 52 20 6f 72 20 78 3e 3d 45 58 50 52  >EXPR or x>=EXPR
24446 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 23   constraint */.#
24447 64 65 66 69 6e 65 20 57 48 45 52 45 5f 49 44 58  define WHERE_IDX
24448 5f 4f 4e 4c 59 20 20 20 20 20 30 78 30 38 30 30  _ONLY     0x0800
24449 30 30 20 20 20 2f 2a 20 55 73 65 20 69 6e 64 65  00   /* Use inde
2444a 78 20 6f 6e 6c 79 20 2d 20 6f 6d 69 74 20 74 61  x only - omit ta
2444b 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57  ble */.#define W
2444c 48 45 52 45 5f 4f 52 44 45 52 42 59 20 20 20 20  HERE_ORDERBY    
2444d 20 20 30 78 31 30 30 30 30 30 20 20 20 2f 2a 20    0x100000   /* 
2444e 4f 75 74 70 75 74 20 77 69 6c 6c 20 61 70 70 65  Output will appe
2444f 61 72 20 69 6e 20 63 6f 72 72 65 63 74 20 6f 72  ar in correct or
24450 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57  der */.#define W
24451 48 45 52 45 5f 52 45 56 45 52 53 45 20 20 20 20  HERE_REVERSE    
24452 20 20 30 78 32 30 30 30 30 30 20 20 20 2f 2a 20    0x200000   /* 
24453 53 63 61 6e 20 69 6e 20 72 65 76 65 72 73 65 20  Scan in reverse 
24454 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65  order */.#define
24455 20 57 48 45 52 45 5f 55 4e 49 51 55 45 20 20 20   WHERE_UNIQUE   
24456 20 20 20 20 30 78 34 30 30 30 30 30 20 20 20 2f      0x400000   /
24457 2a 20 53 65 6c 65 63 74 73 20 6e 6f 20 6d 6f 72  * Selects no mor
24458 65 20 74 68 61 6e 20 6f 6e 65 20 72 6f 77 20 2a  e than one row *
24459 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
2445a 56 49 52 54 55 41 4c 54 41 42 4c 45 20 30 78 38  VIRTUALTABLE 0x8
2445b 30 30 30 30 30 20 20 20 2f 2a 20 55 73 65 20 76  00000   /* Use v
2445c 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 70 72 6f  irtual-table pro
2445d 63 65 73 73 69 6e 67 20 2a 2f 0a 0a 2f 2a 0a 2a  cessing */../*.*
2445e 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 20 70  * Initialize a p
2445f 72 65 61 6c 6c 6f 63 61 74 65 64 20 57 68 65 72  reallocated Wher
24460 65 43 6c 61 75 73 65 20 73 74 72 75 63 74 75 72  eClause structur
24461 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
24462 64 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 69  d whereClauseIni
24463 74 28 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65  t(.  WhereClause
24464 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20 2f 2a   *pWC,        /*
24465 20 54 68 65 20 57 68 65 72 65 43 6c 61 75 73 65   The WhereClause
24466 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c 69 7a   to be initializ
24467 65 64 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70  ed */.  Parse *p
24468 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
24469 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20   /* The parsing 
2446a 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70  context */.  Exp
2446b 72 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53  rMaskSet *pMaskS
2446c 65 74 20 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67  et    /* Mapping
2446d 20 66 72 6f 6d 20 74 61 62 6c 65 20 69 6e 64 69   from table indi
2446e 63 65 73 20 74 6f 20 62 69 74 6d 61 73 6b 73 20  ces to bitmasks 
2446f 2a 2f 0a 29 7b 0a 20 20 70 57 43 2d 3e 70 50 61  */.){.  pWC->pPa
24470 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20  rse = pParse;.  
24471 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 20 3d 20  pWC->pMaskSet = 
24472 70 4d 61 73 6b 53 65 74 3b 0a 20 20 70 57 43 2d  pMaskSet;.  pWC-
24473 3e 6e 54 65 72 6d 20 3d 20 30 3b 0a 20 20 70 57  >nTerm = 0;.  pW
24474 43 2d 3e 6e 53 6c 6f 74 20 3d 20 41 72 72 61 79  C->nSlot = Array
24475 53 69 7a 65 28 70 57 43 2d 3e 61 53 74 61 74 69  Size(pWC->aStati
24476 63 29 3b 0a 20 20 70 57 43 2d 3e 61 20 3d 20 70  c);.  pWC->a = p
24477 57 43 2d 3e 61 53 74 61 74 69 63 3b 0a 7d 0a 0a  WC->aStatic;.}..
24478 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65  /*.** Deallocate
24479 20 61 20 57 68 65 72 65 43 6c 61 75 73 65 20 73   a WhereClause s
2447a 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 57  tructure.  The W
2447b 68 65 72 65 43 6c 61 75 73 65 20 73 74 72 75 63  hereClause struc
2447c 74 75 72 65 0a 2a 2a 20 69 74 73 65 6c 66 20 69  ture.** itself i
2447d 73 20 6e 6f 74 20 66 72 65 65 64 2e 20 20 54 68  s not freed.  Th
2447e 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
2447f 65 20 69 6e 76 65 72 73 65 20 6f 66 20 77 68 65  e inverse of whe
24480 72 65 43 6c 61 75 73 65 49 6e 69 74 28 29 2e 0a  reClauseInit()..
24481 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  */.static void w
24482 68 65 72 65 43 6c 61 75 73 65 43 6c 65 61 72 28  hereClauseClear(
24483 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43  WhereClause *pWC
24484 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 57 68  ){.  int i;.  Wh
24485 65 72 65 54 65 72 6d 20 2a 61 3b 0a 20 20 66 6f  ereTerm *a;.  fo
24486 72 28 69 3d 70 57 43 2d 3e 6e 54 65 72 6d 2d 31  r(i=pWC->nTerm-1
24487 2c 20 61 3d 70 57 43 2d 3e 61 3b 20 69 3e 3d 30  , a=pWC->a; i>=0
24488 3b 20 69 2d 2d 2c 20 61 2b 2b 29 7b 0a 20 20 20  ; i--, a++){.   
24489 20 69 66 28 20 61 2d 3e 66 6c 61 67 73 20 26 20   if( a->flags & 
2448a 54 45 52 4d 5f 44 59 4e 41 4d 49 43 20 29 7b 0a  TERM_DYNAMIC ){.
2448b 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2448c 72 44 65 6c 65 74 65 28 61 2d 3e 70 45 78 70 72  rDelete(a->pExpr
2448d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  );.    }.  }.  i
2448e 66 28 20 70 57 43 2d 3e 61 21 3d 70 57 43 2d 3e  f( pWC->a!=pWC->
2448f 61 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 73  aStatic ){.    s
24490 71 6c 69 74 65 33 5f 66 72 65 65 28 70 57 43 2d  qlite3_free(pWC-
24491 3e 61 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  >a);.  }.}../*.*
24492 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6e 74 72  * Add a new entr
24493 69 65 73 20 74 6f 20 74 68 65 20 57 68 65 72 65  ies to the Where
24494 43 6c 61 75 73 65 20 73 74 72 75 63 74 75 72 65  Clause structure
24495 2e 20 20 49 6e 63 72 65 61 73 65 20 74 68 65 20  .  Increase the 
24496 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 73 70 61  allocated.** spa
24497 63 65 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e  ce as necessary.
24498 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6c  .**.** If the fl
24499 61 67 73 20 61 72 67 75 6d 65 6e 74 20 69 6e 63  ags argument inc
2449a 6c 75 64 65 73 20 54 45 52 4d 5f 44 59 4e 41 4d  ludes TERM_DYNAM
2449b 49 43 2c 20 74 68 65 6e 20 72 65 73 70 6f 6e 73  IC, then respons
2449c 69 62 69 6c 69 74 79 0a 2a 2a 20 66 6f 72 20 66  ibility.** for f
2449d 72 65 65 69 6e 67 20 74 68 65 20 65 78 70 72 65  reeing the expre
2449e 73 73 69 6f 6e 20 70 20 69 73 20 61 73 73 75 6d  ssion p is assum
2449f 65 64 20 62 79 20 74 68 65 20 57 68 65 72 65 43  ed by the WhereC
244a0 6c 61 75 73 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a  lause object..**
244a1 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a 20 20 54 68  .** WARNING:  Th
244a2 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74  is routine might
244a3 20 72 65 61 6c 6c 6f 63 61 74 65 20 74 68 65 20   reallocate the 
244a4 73 70 61 63 65 20 75 73 65 64 20 74 6f 20 73 74  space used to st
244a5 6f 72 65 0a 2a 2a 20 57 68 65 72 65 54 65 72 6d  ore.** WhereTerm
244a6 73 2e 20 20 41 6c 6c 20 70 6f 69 6e 74 65 72 73  s.  All pointers
244a7 20 74 6f 20 57 68 65 72 65 54 65 72 6d 73 20 73   to WhereTerms s
244a8 68 6f 75 6c 64 20 62 65 20 69 6e 76 61 6c 69 64  hould be invalid
244a9 65 64 20 61 66 74 65 72 0a 2a 2a 20 63 61 6c 6c  ed after.** call
244aa 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ing this routine
244ab 2e 20 20 53 75 63 68 20 70 6f 69 6e 74 65 72 73  .  Such pointers
244ac 20 6d 61 79 20 62 65 20 72 65 69 6e 69 74 69 61   may be reinitia
244ad 6c 69 7a 65 64 20 62 79 20 72 65 66 65 72 65 6e  lized by referen
244ae 63 69 6e 67 0a 2a 2a 20 74 68 65 20 70 57 43 2d  cing.** the pWC-
244af 3e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a 2f 0a 73  >a[] array..*/.s
244b0 74 61 74 69 63 20 69 6e 74 20 77 68 65 72 65 43  tatic int whereC
244b1 6c 61 75 73 65 49 6e 73 65 72 74 28 57 68 65 72  lauseInsert(Wher
244b2 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 45 78  eClause *pWC, Ex
244b3 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73  pr *p, int flags
244b4 29 7b 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a  ){.  WhereTerm *
244b5 70 54 65 72 6d 3b 0a 20 20 69 6e 74 20 69 64 78  pTerm;.  int idx
244b6 3b 0a 20 20 69 66 28 20 70 57 43 2d 3e 6e 54 65  ;.  if( pWC->nTe
244b7 72 6d 3e 3d 70 57 43 2d 3e 6e 53 6c 6f 74 20 29  rm>=pWC->nSlot )
244b8 7b 0a 20 20 20 20 57 68 65 72 65 54 65 72 6d 20  {.    WhereTerm 
244b9 2a 70 4f 6c 64 20 3d 20 70 57 43 2d 3e 61 3b 0a  *pOld = pWC->a;.
244ba 20 20 20 20 70 57 43 2d 3e 61 20 3d 20 73 71 6c      pWC->a = sql
244bb 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a  ite3_malloc( siz
244bc 65 6f 66 28 70 57 43 2d 3e 61 5b 30 5d 29 2a 70  eof(pWC->a[0])*p
244bd 57 43 2d 3e 6e 53 6c 6f 74 2a 32 20 29 3b 0a 20  WC->nSlot*2 );. 
244be 20 20 20 69 66 28 20 70 57 43 2d 3e 61 3d 3d 30     if( pWC->a==0
244bf 20 29 7b 0a 20 20 20 20 20 20 70 57 43 2d 3e 70   ){.      pWC->p
244c0 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
244c1 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
244c2 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 54     if( flags & T
244c3 45 52 4d 5f 44 59 4e 41 4d 49 43 20 29 7b 0a 20  ERM_DYNAMIC ){. 
244c4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
244c5 70 72 44 65 6c 65 74 65 28 70 29 3b 0a 20 20 20  prDelete(p);.   
244c6 20 20 20 7d 0a 20 20 20 20 20 20 70 57 43 2d 3e     }.      pWC->
244c7 61 20 3d 20 70 4f 6c 64 3b 0a 20 20 20 20 20 20  a = pOld;.      
244c8 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a  return 0;.    }.
244c9 20 20 20 20 6d 65 6d 63 70 79 28 70 57 43 2d 3e      memcpy(pWC->
244ca 61 2c 20 70 4f 6c 64 2c 20 73 69 7a 65 6f 66 28  a, pOld, sizeof(
244cb 70 57 43 2d 3e 61 5b 30 5d 29 2a 70 57 43 2d 3e  pWC->a[0])*pWC->
244cc 6e 54 65 72 6d 29 3b 0a 20 20 20 20 69 66 28 20  nTerm);.    if( 
244cd 70 4f 6c 64 21 3d 70 57 43 2d 3e 61 53 74 61 74  pOld!=pWC->aStat
244ce 69 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ic ){.      sqli
244cf 74 65 33 5f 66 72 65 65 28 70 4f 6c 64 29 3b 0a  te3_free(pOld);.
244d0 20 20 20 20 7d 0a 20 20 20 20 70 57 43 2d 3e 6e      }.    pWC->n
244d1 53 6c 6f 74 20 2a 3d 20 32 3b 0a 20 20 7d 0a 20  Slot *= 2;.  }. 
244d2 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61   pTerm = &pWC->a
244d3 5b 69 64 78 20 3d 20 70 57 43 2d 3e 6e 54 65 72  [idx = pWC->nTer
244d4 6d 5d 3b 0a 20 20 70 57 43 2d 3e 6e 54 65 72 6d  m];.  pWC->nTerm
244d5 2b 2b 3b 0a 20 20 70 54 65 72 6d 2d 3e 70 45 78  ++;.  pTerm->pEx
244d6 70 72 20 3d 20 70 3b 0a 20 20 70 54 65 72 6d 2d  pr = p;.  pTerm-
244d7 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a  >flags = flags;.
244d8 20 20 70 54 65 72 6d 2d 3e 70 57 43 20 3d 20 70    pTerm->pWC = p
244d9 57 43 3b 0a 20 20 70 54 65 72 6d 2d 3e 69 50 61  WC;.  pTerm->iPa
244da 72 65 6e 74 20 3d 20 2d 31 3b 0a 20 20 72 65 74  rent = -1;.  ret
244db 75 72 6e 20 69 64 78 3b 0a 7d 0a 0a 2f 2a 0a 2a  urn idx;.}../*.*
244dc 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
244dd 64 65 6e 74 69 66 69 65 73 20 73 75 62 65 78 70  dentifies subexp
244de 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20  ressions in the 
244df 57 48 45 52 45 20 63 6c 61 75 73 65 20 77 68 65  WHERE clause whe
244e0 72 65 0a 2a 2a 20 65 61 63 68 20 73 75 62 65 78  re.** each subex
244e1 70 72 65 73 73 69 6f 6e 20 69 73 20 73 65 70 61  pression is sepa
244e2 72 61 74 65 64 20 62 79 20 74 68 65 20 41 4e 44  rated by the AND
244e3 20 6f 70 65 72 61 74 6f 72 20 6f 72 20 73 6f 6d   operator or som
244e4 65 20 6f 74 68 65 72 0a 2a 2a 20 6f 70 65 72 61  e other.** opera
244e5 74 6f 72 20 73 70 65 63 69 66 69 65 64 20 69 6e  tor specified in
244e6 20 74 68 65 20 6f 70 20 70 61 72 61 6d 65 74 65   the op paramete
244e7 72 2e 20 20 54 68 65 20 57 68 65 72 65 43 6c 61  r.  The WhereCla
244e8 75 73 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a  use structure.**
244e9 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20   is filled with 
244ea 70 6f 69 6e 74 65 72 73 20 74 6f 20 73 75 62 65  pointers to sube
244eb 78 70 72 65 73 73 69 6f 6e 73 2e 20 20 46 6f 72  xpressions.  For
244ec 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20   example:.**.** 
244ed 20 20 20 57 48 45 52 45 20 20 61 3d 3d 27 68 65     WHERE  a=='he
244ee 6c 6c 6f 27 20 41 4e 44 20 63 6f 61 6c 65 73 63  llo' AND coalesc
244ef 65 28 62 2c 31 31 29 3c 31 30 20 41 4e 44 20 28  e(b,11)<10 AND (
244f0 63 2b 31 32 21 3d 64 20 4f 52 20 63 3d 3d 32 32  c+12!=d OR c==22
244f1 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5c  ).**           \
244f2 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 5c 5f  ________/     \_
244f3 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 20  ______________/ 
244f4 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f      \___________
244f5 5f 5f 5f 5f 5f 2f 0a 2a 2a 20 20 20 20 20 20 20  _____/.**       
244f6 20 20 20 20 20 73 6c 6f 74 5b 30 5d 20 20 20 20       slot[0]    
244f7 20 20 20 20 20 20 20 20 73 6c 6f 74 5b 31 5d 20          slot[1] 
244f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 6c                sl
244f9 6f 74 5b 32 5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ot[2].**.** The 
244fa 6f 72 69 67 69 6e 61 6c 20 57 48 45 52 45 20 63  original WHERE c
244fb 6c 61 75 73 65 20 69 6e 20 70 45 78 70 72 20 69  lause in pExpr i
244fc 73 20 75 6e 61 6c 74 65 72 65 64 2e 20 20 41 6c  s unaltered.  Al
244fd 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  l this routine.*
244fe 2a 20 64 6f 65 73 20 69 73 20 6d 61 6b 65 20 73  * does is make s
244ff 6c 6f 74 5b 5d 20 65 6e 74 72 69 65 73 20 70 6f  lot[] entries po
24500 69 6e 74 20 74 6f 20 73 75 62 73 74 72 75 63 74  int to substruct
24501 75 72 65 20 77 69 74 68 69 6e 20 70 45 78 70 72  ure within pExpr
24502 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 70  ..**.** In the p
24503 72 65 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65  revious sentence
24504 20 61 6e 64 20 69 6e 20 74 68 65 20 64 69 61 67   and in the diag
24505 72 61 6d 2c 20 22 73 6c 6f 74 5b 5d 22 20 72 65  ram, "slot[]" re
24506 66 65 72 73 20 74 6f 0a 2a 2a 20 74 68 65 20 57  fers to.** the W
24507 68 65 72 65 43 6c 61 75 73 65 2e 61 5b 5d 20 61  hereClause.a[] a
24508 72 72 61 79 2e 20 20 54 68 69 73 20 61 72 72 61  rray.  This arra
24509 79 20 67 72 6f 77 73 20 61 73 20 6e 65 65 64 65  y grows as neede
2450a 64 20 74 6f 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20  d to contain.** 
2450b 61 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74 68 65  all terms of the
2450c 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 2a   WHERE clause..*
2450d 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 68  /.static void wh
2450e 65 72 65 53 70 6c 69 74 28 57 68 65 72 65 43 6c  ereSplit(WhereCl
2450f 61 75 73 65 20 2a 70 57 43 2c 20 45 78 70 72 20  ause *pWC, Expr 
24510 2a 70 45 78 70 72 2c 20 69 6e 74 20 6f 70 29 7b  *pExpr, int op){
24511 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20  .  if( pExpr==0 
24512 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
24513 70 45 78 70 72 2d 3e 6f 70 21 3d 6f 70 20 29 7b  pExpr->op!=op ){
24514 0a 20 20 20 20 77 68 65 72 65 43 6c 61 75 73 65  .    whereClause
24515 49 6e 73 65 72 74 28 70 57 43 2c 20 70 45 78 70  Insert(pWC, pExp
24516 72 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  r, 0);.  }else{.
24517 20 20 20 20 77 68 65 72 65 53 70 6c 69 74 28 70      whereSplit(p
24518 57 43 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  WC, pExpr->pLeft
24519 2c 20 6f 70 29 3b 0a 20 20 20 20 77 68 65 72 65  , op);.    where
2451a 53 70 6c 69 74 28 70 57 43 2c 20 70 45 78 70 72  Split(pWC, pExpr
2451b 2d 3e 70 52 69 67 68 74 2c 20 6f 70 29 3b 0a 20  ->pRight, op);. 
2451c 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74   }.}../*.** Init
2451d 69 61 6c 69 7a 65 20 61 6e 20 65 78 70 72 65 73  ialize an expres
2451e 73 69 6f 6e 20 6d 61 73 6b 20 73 65 74 0a 2a 2f  sion mask set.*/
2451f 0a 23 64 65 66 69 6e 65 20 69 6e 69 74 4d 61 73  .#define initMas
24520 6b 53 65 74 28 50 29 20 20 6d 65 6d 73 65 74 28  kSet(P)  memset(
24521 50 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 50 29  P, 0, sizeof(*P)
24522 29 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  )../*.** Return 
24523 74 68 65 20 62 69 74 6d 61 73 6b 20 66 6f 72 20  the bitmask for 
24524 74 68 65 20 67 69 76 65 6e 20 63 75 72 73 6f 72  the given cursor
24525 20 6e 75 6d 62 65 72 2e 20 20 52 65 74 75 72 6e   number.  Return
24526 20 30 20 69 66 0a 2a 2a 20 69 43 75 72 73 6f 72   0 if.** iCursor
24527 20 69 73 20 6e 6f 74 20 69 6e 20 74 68 65 20 73   is not in the s
24528 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 42 69  et..*/.static Bi
24529 74 6d 61 73 6b 20 67 65 74 4d 61 73 6b 28 45 78  tmask getMask(Ex
2452a 70 72 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b  prMaskSet *pMask
2452b 53 65 74 2c 20 69 6e 74 20 69 43 75 72 73 6f 72  Set, int iCursor
2452c 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f  ){.  int i;.  fo
2452d 72 28 69 3d 30 3b 20 69 3c 70 4d 61 73 6b 53 65  r(i=0; i<pMaskSe
2452e 74 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  t->n; i++){.    
2452f 69 66 28 20 70 4d 61 73 6b 53 65 74 2d 3e 69 78  if( pMaskSet->ix
24530 5b 69 5d 3d 3d 69 43 75 72 73 6f 72 20 29 7b 0a  [i]==iCursor ){.
24531 20 20 20 20 20 20 72 65 74 75 72 6e 20 28 28 42        return ((B
24532 69 74 6d 61 73 6b 29 31 29 3c 3c 69 3b 0a 20 20  itmask)1)<<i;.  
24533 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
24534 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65   0;.}../*.** Cre
24535 61 74 65 20 61 20 6e 65 77 20 6d 61 73 6b 20 66  ate a new mask f
24536 6f 72 20 63 75 72 73 6f 72 20 69 43 75 72 73 6f  or cursor iCurso
24537 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69  r..**.** There i
24538 73 20 6f 6e 65 20 63 75 72 73 6f 72 20 70 65 72  s one cursor per
24539 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52   table in the FR
2453a 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54 68 65 20  OM clause.  The 
2453b 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 61 62  number of.** tab
2453c 6c 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  les in the FROM 
2453d 63 6c 61 75 73 65 20 69 73 20 6c 69 6d 69 74 65  clause is limite
2453e 64 20 62 79 20 61 20 74 65 73 74 20 65 61 72 6c  d by a test earl
2453f 79 20 69 6e 20 74 68 65 0a 2a 2a 20 73 71 6c 69  y in the.** sqli
24540 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 20  te3WhereBegin() 
24541 72 6f 75 74 69 6e 65 2e 20 20 53 6f 20 77 65 20  routine.  So we 
24542 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 70 4d  know that the pM
24543 61 73 6b 53 65 74 2d 3e 69 78 5b 5d 0a 2a 2a 20  askSet->ix[].** 
24544 61 72 72 61 79 20 77 69 6c 6c 20 6e 65 76 65 72  array will never
24545 20 6f 76 65 72 66 6c 6f 77 2e 0a 2a 2f 0a 73 74   overflow..*/.st
24546 61 74 69 63 20 76 6f 69 64 20 63 72 65 61 74 65  atic void create
24547 4d 61 73 6b 28 45 78 70 72 4d 61 73 6b 53 65 74  Mask(ExprMaskSet
24548 20 2a 70 4d 61 73 6b 53 65 74 2c 20 69 6e 74 20   *pMaskSet, int 
24549 69 43 75 72 73 6f 72 29 7b 0a 20 20 61 73 73 65  iCursor){.  asse
2454a 72 74 28 20 70 4d 61 73 6b 53 65 74 2d 3e 6e 20  rt( pMaskSet->n 
2454b 3c 20 41 72 72 61 79 53 69 7a 65 28 70 4d 61 73  < ArraySize(pMas
2454c 6b 53 65 74 2d 3e 69 78 29 20 29 3b 0a 20 20 70  kSet->ix) );.  p
2454d 4d 61 73 6b 53 65 74 2d 3e 69 78 5b 70 4d 61 73  MaskSet->ix[pMas
2454e 6b 53 65 74 2d 3e 6e 2b 2b 5d 20 3d 20 69 43 75  kSet->n++] = iCu
2454f 72 73 6f 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  rsor;.}../*.** T
24550 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 6c 6b  his routine walk
24551 73 20 28 72 65 63 75 72 73 69 76 65 6c 79 29 20  s (recursively) 
24552 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72  an expression tr
24553 65 65 20 61 6e 64 20 67 65 6e 65 72 61 74 65 73  ee and generates
24554 0a 2a 2a 20 61 20 62 69 74 6d 61 73 6b 20 69 6e  .** a bitmask in
24555 64 69 63 61 74 69 6e 67 20 77 68 69 63 68 20 74  dicating which t
24556 61 62 6c 65 73 20 61 72 65 20 75 73 65 64 20 69  ables are used i
24557 6e 20 74 68 61 74 20 65 78 70 72 65 73 73 69 6f  n that expressio
24558 6e 0a 2a 2a 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a  n.** tree..**.**
24559 20 49 6e 20 6f 72 64 65 72 20 66 6f 72 20 74 68   In order for th
2455a 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 77 6f  is routine to wo
2455b 72 6b 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67 20  rk, the calling 
2455c 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 68 61  function must ha
2455d 76 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79  ve.** previously
2455e 20 69 6e 76 6f 6b 65 64 20 73 71 6c 69 74 65 33   invoked sqlite3
2455f 45 78 70 72 52 65 73 6f 6c 76 65 4e 61 6d 65 73  ExprResolveNames
24560 28 29 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73  () on the expres
24561 73 69 6f 6e 2e 20 20 53 65 65 0a 2a 2a 20 74 68  sion.  See.** th
24562 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74  e header comment
24563 20 6f 6e 20 74 68 61 74 20 72 6f 75 74 69 6e 65   on that routine
24564 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
24565 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20  information..** 
24566 54 68 65 20 73 71 6c 69 74 65 33 45 78 70 72 52  The sqlite3ExprR
24567 65 73 6f 6c 76 65 4e 61 6d 65 73 28 29 20 72 6f  esolveNames() ro
24568 75 74 69 6e 65 73 20 6c 6f 6f 6b 73 20 66 6f 72  utines looks for
24569 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 6e   column names an
2456a 64 0a 2a 2a 20 73 65 74 73 20 74 68 65 69 72 20  d.** sets their 
2456b 6f 70 63 6f 64 65 73 20 74 6f 20 54 4b 5f 43 4f  opcodes to TK_CO
2456c 4c 55 4d 4e 20 61 6e 64 20 74 68 65 69 72 20 45  LUMN and their E
2456d 78 70 72 2e 69 54 61 62 6c 65 20 66 69 65 6c 64  xpr.iTable field
2456e 73 20 74 6f 0a 2a 2a 20 74 68 65 20 56 44 42 45  s to.** the VDBE
2456f 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f   cursor number o
24570 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20 54 68  f the table.  Th
24571 69 73 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 20  is routine just 
24572 68 61 73 20 74 6f 0a 2a 2a 20 74 72 61 6e 73 6c  has to.** transl
24573 61 74 65 20 74 68 65 20 63 75 72 73 6f 72 20 6e  ate the cursor n
24574 75 6d 62 65 72 73 20 69 6e 74 6f 20 62 69 74 6d  umbers into bitm
24575 61 73 6b 20 76 61 6c 75 65 73 20 61 6e 64 20 4f  ask values and O
24576 52 20 61 6c 6c 0a 2a 2a 20 74 68 65 20 62 69 74  R all.** the bit
24577 6d 61 73 6b 73 20 74 6f 67 65 74 68 65 72 2e 0a  masks together..
24578 2a 2f 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73  */.static Bitmas
24579 6b 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55  k exprListTableU
2457a 73 61 67 65 28 45 78 70 72 4d 61 73 6b 53 65 74  sage(ExprMaskSet
2457b 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 73  *, ExprList*);.s
2457c 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20 65 78  tatic Bitmask ex
2457d 70 72 53 65 6c 65 63 74 54 61 62 6c 65 55 73 61  prSelectTableUsa
2457e 67 65 28 45 78 70 72 4d 61 73 6b 53 65 74 2a 2c  ge(ExprMaskSet*,
2457f 20 53 65 6c 65 63 74 2a 29 3b 0a 73 74 61 74 69   Select*);.stati
24580 63 20 42 69 74 6d 61 73 6b 20 65 78 70 72 54 61  c Bitmask exprTa
24581 62 6c 65 55 73 61 67 65 28 45 78 70 72 4d 61 73  bleUsage(ExprMas
24582 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20  kSet *pMaskSet, 
24583 45 78 70 72 20 2a 70 29 7b 0a 20 20 42 69 74 6d  Expr *p){.  Bitm
24584 61 73 6b 20 6d 61 73 6b 20 3d 20 30 3b 0a 20 20  ask mask = 0;.  
24585 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
24586 6e 20 30 3b 0a 20 20 69 66 28 20 70 2d 3e 6f 70  n 0;.  if( p->op
24587 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20  ==TK_COLUMN ){. 
24588 20 20 20 6d 61 73 6b 20 3d 20 67 65 74 4d 61 73     mask = getMas
24589 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 69  k(pMaskSet, p->i
2458a 54 61 62 6c 65 29 3b 0a 20 20 20 20 72 65 74 75  Table);.    retu
2458b 72 6e 20 6d 61 73 6b 3b 0a 20 20 7d 0a 20 20 6d  rn mask;.  }.  m
2458c 61 73 6b 20 3d 20 65 78 70 72 54 61 62 6c 65 55  ask = exprTableU
2458d 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70  sage(pMaskSet, p
2458e 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 6d 61 73  ->pRight);.  mas
2458f 6b 20 7c 3d 20 65 78 70 72 54 61 62 6c 65 55 73  k |= exprTableUs
24590 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d  age(pMaskSet, p-
24591 3e 70 4c 65 66 74 29 3b 0a 20 20 6d 61 73 6b 20  >pLeft);.  mask 
24592 7c 3d 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65  |= exprListTable
24593 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
24594 70 2d 3e 70 4c 69 73 74 29 3b 0a 20 20 6d 61 73  p->pList);.  mas
24595 6b 20 7c 3d 20 65 78 70 72 53 65 6c 65 63 74 54  k |= exprSelectT
24596 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
24597 65 74 2c 20 70 2d 3e 70 53 65 6c 65 63 74 29 3b  et, p->pSelect);
24598 0a 20 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a  .  return mask;.
24599 7d 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b  }.static Bitmask
2459a 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73   exprListTableUs
2459b 61 67 65 28 45 78 70 72 4d 61 73 6b 53 65 74 20  age(ExprMaskSet 
2459c 2a 70 4d 61 73 6b 53 65 74 2c 20 45 78 70 72 4c  *pMaskSet, ExprL
2459d 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69  ist *pList){.  i
2459e 6e 74 20 69 3b 0a 20 20 42 69 74 6d 61 73 6b 20  nt i;.  Bitmask 
2459f 6d 61 73 6b 20 3d 20 30 3b 0a 20 20 69 66 28 20  mask = 0;.  if( 
245a0 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 66 6f 72  pList ){.    for
245a1 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  (i=0; i<pList->n
245a2 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
245a3 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 54 61    mask |= exprTa
245a4 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65  bleUsage(pMaskSe
245a5 74 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70  t, pList->a[i].p
245a6 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Expr);.    }.  }
245a7 0a 20 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a  .  return mask;.
245a8 7d 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b  }.static Bitmask
245a9 20 65 78 70 72 53 65 6c 65 63 74 54 61 62 6c 65   exprSelectTable
245aa 55 73 61 67 65 28 45 78 70 72 4d 61 73 6b 53 65  Usage(ExprMaskSe
245ab 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 53 65 6c  t *pMaskSet, Sel
245ac 65 63 74 20 2a 70 53 29 7b 0a 20 20 42 69 74 6d  ect *pS){.  Bitm
245ad 61 73 6b 20 6d 61 73 6b 20 3d 20 30 3b 0a 20 20  ask mask = 0;.  
245ae 77 68 69 6c 65 28 20 70 53 20 29 7b 0a 20 20 20  while( pS ){.   
245af 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 4c 69 73   mask |= exprLis
245b0 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73  tTableUsage(pMas
245b1 6b 53 65 74 2c 20 70 53 2d 3e 70 45 4c 69 73 74  kSet, pS->pEList
245b2 29 3b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65  );.    mask |= e
245b3 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67  xprListTableUsag
245b4 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e  e(pMaskSet, pS->
245b5 70 47 72 6f 75 70 42 79 29 3b 0a 20 20 20 20 6d  pGroupBy);.    m
245b6 61 73 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74 54  ask |= exprListT
245b7 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
245b8 65 74 2c 20 70 53 2d 3e 70 4f 72 64 65 72 42 79  et, pS->pOrderBy
245b9 29 3b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65  );.    mask |= e
245ba 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d  xprTableUsage(pM
245bb 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70 57 68 65  askSet, pS->pWhe
245bc 72 65 29 3b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d  re);.    mask |=
245bd 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28   exprTableUsage(
245be 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70 48  pMaskSet, pS->pH
245bf 61 76 69 6e 67 29 3b 0a 20 20 20 20 70 53 20 3d  aving);.    pS =
245c0 20 70 53 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d   pS->pPrior;.  }
245c1 0a 20 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a  .  return mask;.
245c2 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
245c3 54 52 55 45 20 69 66 20 74 68 65 20 67 69 76 65  TRUE if the give
245c4 6e 20 6f 70 65 72 61 74 6f 72 20 69 73 20 6f 6e  n operator is on
245c5 65 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74 6f  e of the operato
245c6 72 73 20 74 68 61 74 20 69 73 0a 2a 2a 20 61 6c  rs that is.** al
245c7 6c 6f 77 65 64 20 66 6f 72 20 61 6e 20 69 6e 64  lowed for an ind
245c8 65 78 61 62 6c 65 20 57 48 45 52 45 20 63 6c 61  exable WHERE cla
245c9 75 73 65 20 74 65 72 6d 2e 20 20 54 68 65 20 61  use term.  The a
245ca 6c 6c 6f 77 65 64 20 6f 70 65 72 61 74 6f 72 73  llowed operators
245cb 20 61 72 65 0a 2a 2a 20 22 3d 22 2c 20 22 3c 22   are.** "=", "<"
245cc 2c 20 22 3e 22 2c 20 22 3c 3d 22 2c 20 22 3e 3d  , ">", "<=", ">=
245cd 22 2c 20 61 6e 64 20 22 49 4e 22 2e 0a 2a 2f 0a  ", and "IN"..*/.
245ce 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 77  static int allow
245cf 65 64 4f 70 28 69 6e 74 20 6f 70 29 7b 0a 20 20  edOp(int op){.  
245d0 61 73 73 65 72 74 28 20 54 4b 5f 47 54 3e 54 4b  assert( TK_GT>TK
245d1 5f 45 51 20 26 26 20 54 4b 5f 47 54 3c 54 4b 5f  _EQ && TK_GT<TK_
245d2 47 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  GE );.  assert( 
245d3 54 4b 5f 4c 54 3e 54 4b 5f 45 51 20 26 26 20 54  TK_LT>TK_EQ && T
245d4 4b 5f 4c 54 3c 54 4b 5f 47 45 20 29 3b 0a 20 20  K_LT<TK_GE );.  
245d5 61 73 73 65 72 74 28 20 54 4b 5f 4c 45 3e 54 4b  assert( TK_LE>TK
245d6 5f 45 51 20 26 26 20 54 4b 5f 4c 45 3c 54 4b 5f  _EQ && TK_LE<TK_
245d7 47 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  GE );.  assert( 
245d8 54 4b 5f 47 45 3d 3d 54 4b 5f 45 51 2b 34 20 29  TK_GE==TK_EQ+4 )
245d9 3b 0a 20 20 72 65 74 75 72 6e 20 6f 70 3d 3d 54  ;.  return op==T
245da 4b 5f 49 4e 20 7c 7c 20 28 6f 70 3e 3d 54 4b 5f  K_IN || (op>=TK_
245db 45 51 20 26 26 20 6f 70 3c 3d 54 4b 5f 47 45 29  EQ && op<=TK_GE)
245dc 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c   || op==TK_ISNUL
245dd 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 77 61 70  L;.}../*.** Swap
245de 20 74 77 6f 20 6f 62 6a 65 63 74 73 20 6f 66 20   two objects of 
245df 74 79 70 65 20 54 2e 0a 2a 2f 0a 23 64 65 66 69  type T..*/.#defi
245e0 6e 65 20 53 57 41 50 28 54 59 50 45 2c 41 2c 42  ne SWAP(TYPE,A,B
245e1 29 20 7b 54 59 50 45 20 74 3d 41 3b 20 41 3d 42  ) {TYPE t=A; A=B
245e2 3b 20 42 3d 74 3b 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ; B=t;}../*.** C
245e3 6f 6d 6d 75 74 65 20 61 20 63 6f 6d 70 61 72 69  ommute a compari
245e4 73 69 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20  sion operator.  
245e5 45 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74  Expressions of t
245e6 68 65 20 66 6f 72 6d 20 22 58 20 6f 70 20 59 22  he form "X op Y"
245e7 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65 72 74 65  .** are converte
245e8 64 20 69 6e 74 6f 20 22 59 20 6f 70 20 58 22 2e  d into "Y op X".
245e9 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 63 6f 6c 6c  .**.** If a coll
245ea 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69  ation sequence i
245eb 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
245ec 68 20 65 69 74 68 65 72 20 74 68 65 20 6c 65 66  h either the lef
245ed 74 20 6f 72 20 72 69 67 68 74 0a 2a 2a 20 73 69  t or right.** si
245ee 64 65 20 6f 66 20 74 68 65 20 63 6f 6d 70 61 72  de of the compar
245ef 69 73 6f 6e 2c 20 69 74 20 72 65 6d 61 69 6e 73  ison, it remains
245f0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
245f1 20 74 68 65 20 73 61 6d 65 20 73 69 64 65 20 61   the same side a
245f2 66 74 65 72 0a 2a 2a 20 74 68 65 20 63 6f 6d 6d  fter.** the comm
245f3 75 74 61 74 69 6f 6e 2e 20 53 6f 20 22 59 20 63  utation. So "Y c
245f4 6f 6c 6c 61 74 65 20 4e 4f 43 41 53 45 20 6f 70  ollate NOCASE op
245f5 20 58 22 20 62 65 63 6f 6d 65 73 20 0a 2a 2a 20   X" becomes .** 
245f6 22 58 20 63 6f 6c 6c 61 74 65 20 4e 4f 43 41 53  "X collate NOCAS
245f7 45 20 6f 70 20 59 22 2e 20 54 68 69 73 20 69 73  E op Y". This is
245f8 20 62 65 63 61 75 73 65 20 61 6e 79 20 63 6f 6c   because any col
245f9 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
245fa 6f 6e 0a 2a 2a 20 74 68 65 20 6c 65 66 74 20 68  on.** the left h
245fb 61 6e 64 20 73 69 64 65 20 6f 66 20 61 20 63 6f  and side of a co
245fc 6d 70 61 72 69 73 6f 6e 20 6f 76 65 72 72 69 64  mparison overrid
245fd 65 73 20 61 6e 79 20 63 6f 6c 6c 61 74 69 6f 6e  es any collation
245fe 20 73 65 71 75 65 6e 63 65 20 0a 2a 2a 20 61 74   sequence .** at
245ff 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 72 69  tached to the ri
24600 67 68 74 2e 20 46 6f 72 20 74 68 65 20 73 61 6d  ght. For the sam
24601 65 20 72 65 61 73 6f 6e 20 74 68 65 20 45 50 5f  e reason the EP_
24602 45 78 70 43 6f 6c 6c 61 74 65 20 66 6c 61 67 0a  ExpCollate flag.
24603 2a 2a 20 69 73 20 6e 6f 74 20 63 6f 6d 6d 75 74  ** is not commut
24604 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
24605 69 64 20 65 78 70 72 43 6f 6d 6d 75 74 65 28 45  id exprCommute(E
24606 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 75  xpr *pExpr){.  u
24607 31 36 20 65 78 70 52 69 67 68 74 20 3d 20 28 70  16 expRight = (p
24608 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 66 6c  Expr->pRight->fl
24609 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c  ags & EP_ExpColl
2460a 61 74 65 29 3b 0a 20 20 75 31 36 20 65 78 70 4c  ate);.  u16 expL
2460b 65 66 74 20 3d 20 28 70 45 78 70 72 2d 3e 70 4c  eft = (pExpr->pL
2460c 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  eft->flags & EP_
2460d 45 78 70 43 6f 6c 6c 61 74 65 29 3b 0a 20 20 61  ExpCollate);.  a
2460e 73 73 65 72 74 28 20 61 6c 6c 6f 77 65 64 4f 70  ssert( allowedOp
2460f 28 70 45 78 70 72 2d 3e 6f 70 29 20 26 26 20 70  (pExpr->op) && p
24610 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 49 4e 20  Expr->op!=TK_IN 
24611 29 3b 0a 20 20 53 57 41 50 28 43 6f 6c 6c 53 65  );.  SWAP(CollSe
24612 71 2a 2c 70 45 78 70 72 2d 3e 70 52 69 67 68 74  q*,pExpr->pRight
24613 2d 3e 70 43 6f 6c 6c 2c 70 45 78 70 72 2d 3e 70  ->pColl,pExpr->p
24614 4c 65 66 74 2d 3e 70 43 6f 6c 6c 29 3b 0a 20 20  Left->pColl);.  
24615 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 66  pExpr->pRight->f
24616 6c 61 67 73 20 3d 20 28 70 45 78 70 72 2d 3e 70  lags = (pExpr->p
24617 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26 20 7e  Right->flags & ~
24618 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 29 20 7c  EP_ExpCollate) |
24619 20 65 78 70 4c 65 66 74 3b 0a 20 20 70 45 78 70   expLeft;.  pExp
2461a 72 2d 3e 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20  r->pLeft->flags 
2461b 3d 20 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d  = (pExpr->pLeft-
2461c 3e 66 6c 61 67 73 20 26 20 7e 45 50 5f 45 78 70  >flags & ~EP_Exp
2461d 43 6f 6c 6c 61 74 65 29 20 7c 20 65 78 70 52 69  Collate) | expRi
2461e 67 68 74 3b 0a 20 20 53 57 41 50 28 45 78 70 72  ght;.  SWAP(Expr
2461f 2a 2c 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  *,pExpr->pRight,
24620 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20  pExpr->pLeft);. 
24621 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3e 3d   if( pExpr->op>=
24622 54 4b 5f 47 54 20 29 7b 0a 20 20 20 20 61 73 73  TK_GT ){.    ass
24623 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 54 4b 5f 47  ert( TK_LT==TK_G
24624 54 2b 32 20 29 3b 0a 20 20 20 20 61 73 73 65 72  T+2 );.    asser
24625 74 28 20 54 4b 5f 47 45 3d 3d 54 4b 5f 4c 45 2b  t( TK_GE==TK_LE+
24626 32 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  2 );.    assert(
24627 20 54 4b 5f 47 54 3e 54 4b 5f 45 51 20 29 3b 0a   TK_GT>TK_EQ );.
24628 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47      assert( TK_G
24629 54 3c 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 61  T<TK_LE );.    a
2462a 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70  ssert( pExpr->op
2462b 3e 3d 54 4b 5f 47 54 20 26 26 20 70 45 78 70 72  >=TK_GT && pExpr
2462c 2d 3e 6f 70 3c 3d 54 4b 5f 47 45 20 29 3b 0a 20  ->op<=TK_GE );. 
2462d 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 28     pExpr->op = (
2462e 28 70 45 78 70 72 2d 3e 6f 70 2d 54 4b 5f 47 54  (pExpr->op-TK_GT
2462f 29 5e 32 29 2b 54 4b 5f 47 54 3b 0a 20 20 7d 0a  )^2)+TK_GT;.  }.
24630 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61  }../*.** Transla
24631 74 65 20 66 72 6f 6d 20 54 4b 5f 78 78 20 6f 70  te from TK_xx op
24632 65 72 61 74 6f 72 20 74 6f 20 57 4f 5f 78 78 20  erator to WO_xx 
24633 62 69 74 6d 61 73 6b 2e 0a 2a 2f 0a 73 74 61 74  bitmask..*/.stat
24634 69 63 20 69 6e 74 20 6f 70 65 72 61 74 6f 72 4d  ic int operatorM
24635 61 73 6b 28 69 6e 74 20 6f 70 29 7b 0a 20 20 69  ask(int op){.  i
24636 6e 74 20 63 3b 0a 20 20 61 73 73 65 72 74 28 20  nt c;.  assert( 
24637 61 6c 6c 6f 77 65 64 4f 70 28 6f 70 29 20 29 3b  allowedOp(op) );
24638 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 4e  .  if( op==TK_IN
24639 20 29 7b 0a 20 20 20 20 63 20 3d 20 57 4f 5f 49   ){.    c = WO_I
2463a 4e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f  N;.  }else if( o
2463b 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 7b 0a  p==TK_ISNULL ){.
2463c 20 20 20 20 63 20 3d 20 57 4f 5f 49 53 4e 55 4c      c = WO_ISNUL
2463d 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  L;.  }else{.    
2463e 63 20 3d 20 57 4f 5f 45 51 3c 3c 28 6f 70 2d 54  c = WO_EQ<<(op-T
2463f 4b 5f 45 51 29 3b 0a 20 20 7d 0a 20 20 61 73 73  K_EQ);.  }.  ass
24640 65 72 74 28 20 6f 70 21 3d 54 4b 5f 49 53 4e 55  ert( op!=TK_ISNU
24641 4c 4c 20 7c 7c 20 63 3d 3d 57 4f 5f 49 53 4e 55  LL || c==WO_ISNU
24642 4c 4c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  LL );.  assert( 
24643 6f 70 21 3d 54 4b 5f 49 4e 20 7c 7c 20 63 3d 3d  op!=TK_IN || c==
24644 57 4f 5f 49 4e 20 29 3b 0a 20 20 61 73 73 65 72  WO_IN );.  asser
24645 74 28 20 6f 70 21 3d 54 4b 5f 45 51 20 7c 7c 20  t( op!=TK_EQ || 
24646 63 3d 3d 57 4f 5f 45 51 20 29 3b 0a 20 20 61 73  c==WO_EQ );.  as
24647 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 4c 54 20  sert( op!=TK_LT 
24648 7c 7c 20 63 3d 3d 57 4f 5f 4c 54 20 29 3b 0a 20  || c==WO_LT );. 
24649 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f   assert( op!=TK_
2464a 4c 45 20 7c 7c 20 63 3d 3d 57 4f 5f 4c 45 20 29  LE || c==WO_LE )
2464b 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d  ;.  assert( op!=
2464c 54 4b 5f 47 54 20 7c 7c 20 63 3d 3d 57 4f 5f 47  TK_GT || c==WO_G
2464d 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f  T );.  assert( o
2464e 70 21 3d 54 4b 5f 47 45 20 7c 7c 20 63 3d 3d 57  p!=TK_GE || c==W
2464f 4f 5f 47 45 20 29 3b 0a 20 20 72 65 74 75 72 6e  O_GE );.  return
24650 20 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 61   c;.}../*.** Sea
24651 72 63 68 20 66 6f 72 20 61 20 74 65 72 6d 20 69  rch for a term i
24652 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  n the WHERE clau
24653 73 65 20 74 68 61 74 20 69 73 20 6f 66 20 74 68  se that is of th
24654 65 20 66 6f 72 6d 20 22 58 20 3c 6f 70 3e 20 3c  e form "X <op> <
24655 65 78 70 72 3e 22 0a 2a 2a 20 77 68 65 72 65 20  expr>".** where 
24656 58 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 65  X is a reference
24657 20 74 6f 20 74 68 65 20 69 43 6f 6c 75 6d 6e 20   to the iColumn 
24658 6f 66 20 74 61 62 6c 65 20 69 43 75 72 20 61 6e  of table iCur an
24659 64 20 3c 6f 70 3e 20 69 73 20 6f 6e 65 20 6f 66  d <op> is one of
2465a 0a 2a 2a 20 74 68 65 20 57 4f 5f 78 78 20 6f 70  .** the WO_xx op
2465b 65 72 61 74 6f 72 20 63 6f 64 65 73 20 73 70 65  erator codes spe
2465c 63 69 66 69 65 64 20 62 79 20 74 68 65 20 6f 70  cified by the op
2465d 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 52   parameter..** R
2465e 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
2465f 74 6f 20 74 68 65 20 74 65 72 6d 2e 20 20 52 65  to the term.  Re
24660 74 75 72 6e 20 30 20 69 66 20 6e 6f 74 20 66 6f  turn 0 if not fo
24661 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57  und..*/.static W
24662 68 65 72 65 54 65 72 6d 20 2a 66 69 6e 64 54 65  hereTerm *findTe
24663 72 6d 28 0a 20 20 57 68 65 72 65 43 6c 61 75 73  rm(.  WhereClaus
24664 65 20 2a 70 57 43 2c 20 20 20 20 20 2f 2a 20 54  e *pWC,     /* T
24665 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
24666 74 6f 20 62 65 20 73 65 61 72 63 68 65 64 20 2a  to be searched *
24667 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20  /.  int iCur,   
24668 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
24669 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 4c 48  sor number of LH
2466a 53 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75  S */.  int iColu
2466b 6d 6e 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  mn,          /* 
2466c 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 6f 66  Column number of
2466d 20 4c 48 53 20 2a 2f 0a 20 20 42 69 74 6d 61 73   LHS */.  Bitmas
2466e 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20 20  k notReady,     
2466f 2f 2a 20 52 48 53 20 6d 75 73 74 20 6e 6f 74 20  /* RHS must not 
24670 6f 76 65 72 6c 61 70 20 77 69 74 68 20 74 68 69  overlap with thi
24671 73 20 6d 61 73 6b 20 2a 2f 0a 20 20 75 31 36 20  s mask */.  u16 
24672 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  op,             
24673 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 57 4f 5f    /* Mask of WO_
24674 78 78 20 76 61 6c 75 65 73 20 64 65 73 63 72 69  xx values descri
24675 62 69 6e 67 20 6f 70 65 72 61 74 6f 72 20 2a 2f  bing operator */
24676 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20 20  .  Index *pIdx  
24677 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 73 74           /* Must
24678 20 62 65 20 63 6f 6d 70 61 74 69 62 6c 65 20 77   be compatible w
24679 69 74 68 20 74 68 69 73 20 69 6e 64 65 78 2c 20  ith this index, 
2467a 69 66 20 6e 6f 74 20 4e 55 4c 4c 20 2a 2f 0a 29  if not NULL */.)
2467b 7b 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70  {.  WhereTerm *p
2467c 54 65 72 6d 3b 0a 20 20 69 6e 74 20 6b 3b 0a 20  Term;.  int k;. 
2467d 20 66 6f 72 28 70 54 65 72 6d 3d 70 57 43 2d 3e   for(pTerm=pWC->
2467e 61 2c 20 6b 3d 70 57 43 2d 3e 6e 54 65 72 6d 3b  a, k=pWC->nTerm;
2467f 20 6b 3b 20 6b 2d 2d 2c 20 70 54 65 72 6d 2b 2b   k; k--, pTerm++
24680 29 7b 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d  ){.    if( pTerm
24681 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43  ->leftCursor==iC
24682 75 72 0a 20 20 20 20 20 20 20 26 26 20 28 70 54  ur.       && (pT
24683 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74  erm->prereqRight
24684 20 26 20 6e 6f 74 52 65 61 64 79 29 3d 3d 30 0a   & notReady)==0.
24685 20 20 20 20 20 20 20 26 26 20 70 54 65 72 6d 2d         && pTerm-
24686 3e 6c 65 66 74 43 6f 6c 75 6d 6e 3d 3d 69 43 6f  >leftColumn==iCo
24687 6c 75 6d 6e 0a 20 20 20 20 20 20 20 26 26 20 28  lumn.       && (
24688 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
24689 20 26 20 6f 70 29 21 3d 30 0a 20 20 20 20 29 7b   & op)!=0.    ){
2468a 0a 20 20 20 20 20 20 69 66 28 20 69 43 75 72 3e  .      if( iCur>
2468b 3d 30 20 26 26 20 70 49 64 78 20 26 26 20 70 54  =0 && pIdx && pT
2468c 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 21 3d  erm->eOperator!=
2468d 57 4f 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20  WO_ISNULL ){.   
2468e 20 20 20 20 20 45 78 70 72 20 2a 70 58 20 3d 20       Expr *pX = 
2468f 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20  pTerm->pExpr;.  
24690 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70        CollSeq *p
24691 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 63 68  Coll;.        ch
24692 61 72 20 69 64 78 61 66 66 3b 0a 20 20 20 20 20  ar idxaff;.     
24693 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
24694 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 20    Parse *pParse 
24695 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 3b 0a 0a  = pWC->pParse;..
24696 20 20 20 20 20 20 20 20 69 64 78 61 66 66 20 3d          idxaff =
24697 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 61   pIdx->pTable->a
24698 43 6f 6c 5b 69 43 6f 6c 75 6d 6e 5d 2e 61 66 66  Col[iColumn].aff
24699 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 20 20 69  inity;.        i
2469a 66 28 20 21 73 71 6c 69 74 65 33 49 6e 64 65 78  f( !sqlite3Index
2469b 41 66 66 69 6e 69 74 79 4f 6b 28 70 58 2c 20 69  AffinityOk(pX, i
2469c 64 78 61 66 66 29 20 29 20 63 6f 6e 74 69 6e 75  dxaff) ) continu
2469d 65 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 46  e;..        /* F
2469e 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 63 6f  igure out the co
2469f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
246a0 20 72 65 71 75 69 72 65 64 20 66 72 6f 6d 20 61   required from a
246a1 6e 20 69 6e 64 65 78 20 66 6f 72 0a 20 20 20 20  n index for.    
246a2 20 20 20 20 2a 2a 20 69 74 20 74 6f 20 62 65 20      ** it to be 
246a3 75 73 65 66 75 6c 20 66 6f 72 20 6f 70 74 69 6d  useful for optim
246a4 69 73 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e  ising expression
246a5 20 70 58 2e 20 53 74 6f 72 65 20 74 68 69 73 0a   pX. Store this.
246a6 20 20 20 20 20 20 20 20 2a 2a 20 76 61 6c 75 65          ** value
246a7 20 69 6e 20 76 61 72 69 61 62 6c 65 20 70 43 6f   in variable pCo
246a8 6c 6c 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ll..        */. 
246a9 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70 58         assert(pX
246aa 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20  ->pLeft);.      
246ab 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
246ac 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f  3BinaryCompareCo
246ad 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 58  llSeq(pParse, pX
246ae 2d 3e 70 4c 65 66 74 2c 20 70 58 2d 3e 70 52 69  ->pLeft, pX->pRi
246af 67 68 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ght);.        if
246b0 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  ( !pColl ){.    
246b1 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 50        pColl = pP
246b2 61 72 73 65 2d 3e 64 62 2d 3e 70 44 66 6c 74 43  arse->db->pDfltC
246b3 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a  oll;.        }..
246b4 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b          for(j=0;
246b5 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e   j<pIdx->nColumn
246b6 20 26 26 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75   && pIdx->aiColu
246b7 6d 6e 5b 6a 5d 21 3d 69 43 6f 6c 75 6d 6e 3b 20  mn[j]!=iColumn; 
246b8 6a 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 20 20 61  j++){}.        a
246b9 73 73 65 72 74 28 20 6a 3c 70 49 64 78 2d 3e 6e  ssert( j<pIdx->n
246ba 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 20 20 20 20  Column );.      
246bb 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
246bc 49 43 6d 70 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d  ICmp(pColl->zNam
246bd 65 2c 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b  e, pIdx->azColl[
246be 6a 5d 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  j]) ) continue;.
246bf 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
246c0 74 75 72 6e 20 70 54 65 72 6d 3b 0a 20 20 20 20  turn pTerm;.    
246c1 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  }.  }.  return 0
246c2 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20  ;.}../* Forward 
246c3 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73 74 61  reference */.sta
246c4 74 69 63 20 76 6f 69 64 20 65 78 70 72 41 6e 61  tic void exprAna
246c5 6c 79 7a 65 28 53 72 63 4c 69 73 74 2a 2c 20 57  lyze(SrcList*, W
246c6 68 65 72 65 43 6c 61 75 73 65 2a 2c 20 69 6e 74  hereClause*, int
246c7 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 65  );../*.** Call e
246c8 78 70 72 41 6e 61 6c 79 7a 65 20 6f 6e 20 61 6c  xprAnalyze on al
246c9 6c 20 74 65 72 6d 73 20 69 6e 20 61 20 57 48 45  l terms in a WHE
246ca 52 45 20 63 6c 61 75 73 65 2e 20 20 0a 2a 2a 0a  RE clause.  .**.
246cb 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  **.*/.static voi
246cc 64 20 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c 6c  d exprAnalyzeAll
246cd 28 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61  (.  SrcList *pTa
246ce 62 4c 69 73 74 2c 20 20 20 20 20 20 20 2f 2a 20  bList,       /* 
246cf 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
246d0 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65  */.  WhereClause
246d1 20 2a 70 57 43 20 20 20 20 20 20 20 20 20 2f 2a   *pWC         /*
246d2 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
246d3 65 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64  e to be analyzed
246d4 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   */.){.  int i;.
246d5 20 20 66 6f 72 28 69 3d 70 57 43 2d 3e 6e 54 65    for(i=pWC->nTe
246d6 72 6d 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29  rm-1; i>=0; i--)
246d7 7b 0a 20 20 20 20 65 78 70 72 41 6e 61 6c 79 7a  {.    exprAnalyz
246d8 65 28 70 54 61 62 4c 69 73 74 2c 20 70 57 43 2c  e(pTabList, pWC,
246d9 20 69 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e   i);.  }.}..#ifn
246da 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
246db 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  LIKE_OPTIMIZATIO
246dc 4e 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f  N./*.** Check to
246dd 20 73 65 65 20 69 66 20 74 68 65 20 67 69 76 65   see if the give
246de 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  n expression is 
246df 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 6f  a LIKE or GLOB o
246e0 70 65 72 61 74 6f 72 20 74 68 61 74 0a 2a 2a 20  perator that.** 
246e1 63 61 6e 20 62 65 20 6f 70 74 69 6d 69 7a 65 64  can be optimized
246e2 20 75 73 69 6e 67 20 69 6e 65 71 75 61 6c 69 74   using inequalit
246e3 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20  y constraints.  
246e4 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 69  Return TRUE if i
246e5 74 20 69 73 0a 2a 2a 20 73 6f 20 61 6e 64 20 66  t is.** so and f
246e6 61 6c 73 65 20 69 66 20 6e 6f 74 2e 0a 2a 2a 0a  alse if not..**.
246e7 2a 2a 20 49 6e 20 6f 72 64 65 72 20 66 6f 72 20  ** In order for 
246e8 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74 6f 20  the operator to 
246e9 62 65 20 6f 70 74 69 6d 69 7a 69 62 6c 65 2c 20  be optimizible, 
246ea 74 68 65 20 52 48 53 20 6d 75 73 74 20 62 65 20  the RHS must be 
246eb 61 20 73 74 72 69 6e 67 0a 2a 2a 20 6c 69 74 65  a string.** lite
246ec 72 61 6c 20 74 68 61 74 20 64 6f 65 73 20 6e 6f  ral that does no
246ed 74 20 62 65 67 69 6e 20 77 69 74 68 20 61 20 77  t begin with a w
246ee 69 6c 64 63 61 72 64 2e 20 20 0a 2a 2f 0a 73 74  ildcard.  .*/.st
246ef 61 74 69 63 20 69 6e 74 20 69 73 4c 69 6b 65 4f  atic int isLikeO
246f0 72 47 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33  rGlob(.  sqlite3
246f1 20 2a 64 62 2c 20 20 20 20 20 20 2f 2a 20 54 68   *db,      /* Th
246f2 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  e database */.  
246f3 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20  Expr *pExpr,    
246f4 20 20 2f 2a 20 54 65 73 74 20 74 68 69 73 20 65    /* Test this e
246f5 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69  xpression */.  i
246f6 6e 74 20 2a 70 6e 50 61 74 74 65 72 6e 2c 20 20  nt *pnPattern,  
246f7 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f   /* Number of no
246f8 6e 2d 77 69 6c 64 63 61 72 64 20 70 72 65 66 69  n-wildcard prefi
246f9 78 20 63 68 61 72 61 63 74 65 72 73 20 2a 2f 0a  x characters */.
246fa 20 20 69 6e 74 20 2a 70 69 73 43 6f 6d 70 6c 65    int *pisComple
246fb 74 65 2c 20 2f 2a 20 54 72 75 65 20 69 66 20 74  te, /* True if t
246fc 68 65 20 6f 6e 6c 79 20 77 69 6c 64 63 61 72 64  he only wildcard
246fd 20 69 73 20 25 20 69 6e 20 74 68 65 20 6c 61 73   is % in the las
246fe 74 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20  t character */. 
246ff 20 69 6e 74 20 2a 70 6e 6f 43 61 73 65 20 20 20   int *pnoCase   
24700 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 75 70     /* True if up
24701 70 65 72 63 61 73 65 20 69 73 20 65 71 75 69 76  percase is equiv
24702 61 6c 65 6e 74 20 74 6f 20 6c 6f 77 65 72 63 61  alent to lowerca
24703 73 65 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74  se */.){.  const
24704 20 63 68 61 72 20 2a 7a 3b 0a 20 20 45 78 70 72   char *z;.  Expr
24705 20 2a 70 52 69 67 68 74 2c 20 2a 70 4c 65 66 74   *pRight, *pLeft
24706 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c  ;.  ExprList *pL
24707 69 73 74 3b 0a 20 20 69 6e 74 20 63 2c 20 63 6e  ist;.  int c, cn
24708 74 3b 0a 20 20 63 68 61 72 20 77 63 5b 33 5d 3b  t;.  char wc[3];
24709 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  .  CollSeq *pCol
2470a 6c 3b 0a 0a 20 20 69 66 28 20 21 73 71 6c 69 74  l;..  if( !sqlit
2470b 65 33 49 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e  e3IsLikeFunction
2470c 28 64 62 2c 20 70 45 78 70 72 2c 20 70 6e 6f 43  (db, pExpr, pnoC
2470d 61 73 65 2c 20 77 63 29 20 29 7b 0a 20 20 20 20  ase, wc) ){.    
2470e 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 23 69  return 0;.  }.#i
2470f 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44  fdef SQLITE_EBCD
24710 49 43 0a 20 20 69 66 28 20 2a 70 6e 6f 43 61 73  IC.  if( *pnoCas
24711 65 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65  e ) return 0;.#e
24712 6e 64 69 66 0a 20 20 70 4c 69 73 74 20 3d 20 70  ndif.  pList = p
24713 45 78 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 70  Expr->pList;.  p
24714 52 69 67 68 74 20 3d 20 70 4c 69 73 74 2d 3e 61  Right = pList->a
24715 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 69 66 28  [0].pExpr;.  if(
24716 20 70 52 69 67 68 74 2d 3e 6f 70 21 3d 54 4b 5f   pRight->op!=TK_
24717 53 54 52 49 4e 47 0a 20 20 20 26 26 20 28 70 52  STRING.   && (pR
24718 69 67 68 74 2d 3e 6f 70 21 3d 54 4b 5f 52 45 47  ight->op!=TK_REG
24719 49 53 54 45 52 20 7c 7c 20 70 52 69 67 68 74 2d  ISTER || pRight-
2471a 3e 69 43 6f 6c 75 6d 6e 21 3d 54 4b 5f 53 54 52  >iColumn!=TK_STR
2471b 49 4e 47 29 20 29 7b 0a 20 20 20 20 72 65 74 75  ING) ){.    retu
2471c 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4c 65 66  rn 0;.  }.  pLef
2471d 74 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e  t = pList->a[1].
2471e 70 45 78 70 72 3b 0a 20 20 69 66 28 20 70 4c 65  pExpr;.  if( pLe
2471f 66 74 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d  ft->op!=TK_COLUM
24720 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  N ){.    return 
24721 30 3b 0a 20 20 7d 0a 20 20 70 43 6f 6c 6c 20 3d  0;.  }.  pColl =
24722 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20   pLeft->pColl;. 
24723 20 61 73 73 65 72 74 28 20 70 43 6f 6c 6c 21 3d   assert( pColl!=
24724 30 20 7c 7c 20 70 4c 65 66 74 2d 3e 69 43 6f 6c  0 || pLeft->iCol
24725 75 6d 6e 3d 3d 2d 31 20 29 3b 0a 20 20 69 66 28  umn==-1 );.  if(
24726 20 70 43 6f 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20   pColl==0 ){.   
24727 20 2f 2a 20 4e 6f 20 63 6f 6c 6c 61 74 69 6f 6e   /* No collation
24728 20 69 73 20 64 65 66 69 6e 65 64 20 66 6f 72 20   is defined for 
24729 74 68 65 20 52 4f 57 49 44 2e 20 20 55 73 65 20  the ROWID.  Use 
2472a 74 68 65 20 64 65 66 61 75 6c 74 2e 20 2a 2f 0a  the default. */.
2472b 20 20 20 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e      pColl = db->
2472c 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 7d 0a 20  pDfltColl;.  }. 
2472d 20 69 66 28 20 28 70 43 6f 6c 6c 2d 3e 74 79 70   if( (pColl->typ
2472e 65 21 3d 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42  e!=SQLITE_COLL_B
2472f 49 4e 41 52 59 20 7c 7c 20 2a 70 6e 6f 43 61 73  INARY || *pnoCas
24730 65 29 20 26 26 0a 20 20 20 20 20 20 28 70 43 6f  e) &&.      (pCo
24731 6c 6c 2d 3e 74 79 70 65 21 3d 53 51 4c 49 54 45  ll->type!=SQLITE
24732 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45 20 7c 7c 20  _COLL_NOCASE || 
24733 21 2a 70 6e 6f 43 61 73 65 29 20 29 7b 0a 20 20  !*pnoCase) ){.  
24734 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
24735 20 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65    sqlite3Dequote
24736 45 78 70 72 28 64 62 2c 20 70 52 69 67 68 74 29  Expr(db, pRight)
24737 3b 0a 20 20 7a 20 3d 20 28 63 68 61 72 20 2a 29  ;.  z = (char *)
24738 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 2e 7a 3b  pRight->token.z;
24739 0a 20 20 63 6e 74 20 3d 20 30 3b 0a 20 20 69 66  .  cnt = 0;.  if
2473a 28 20 7a 20 29 7b 0a 20 20 20 20 77 68 69 6c 65  ( z ){.    while
2473b 28 20 28 63 3d 7a 5b 63 6e 74 5d 29 21 3d 30 20  ( (c=z[cnt])!=0 
2473c 26 26 20 63 21 3d 77 63 5b 30 5d 20 26 26 20 63  && c!=wc[0] && c
2473d 21 3d 77 63 5b 31 5d 20 26 26 20 63 21 3d 77 63  !=wc[1] && c!=wc
2473e 5b 32 5d 20 29 7b 20 63 6e 74 2b 2b 3b 20 7d 0a  [2] ){ cnt++; }.
2473f 20 20 7d 0a 20 20 69 66 28 20 63 6e 74 3d 3d 30    }.  if( cnt==0
24740 20 7c 7c 20 32 35 35 3d 3d 28 75 38 29 7a 5b 63   || 255==(u8)z[c
24741 6e 74 5d 20 29 7b 0a 20 20 20 20 72 65 74 75 72  nt] ){.    retur
24742 6e 20 30 3b 0a 20 20 7d 0a 20 20 2a 70 69 73 43  n 0;.  }.  *pisC
24743 6f 6d 70 6c 65 74 65 20 3d 20 7a 5b 63 6e 74 5d  omplete = z[cnt]
24744 3d 3d 77 63 5b 30 5d 20 26 26 20 7a 5b 63 6e 74  ==wc[0] && z[cnt
24745 2b 31 5d 3d 3d 30 3b 0a 20 20 2a 70 6e 50 61 74  +1]==0;.  *pnPat
24746 74 65 72 6e 20 3d 20 63 6e 74 3b 0a 20 20 72 65  tern = cnt;.  re
24747 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66  turn 1;.}.#endif
24748 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
24749 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  LIKE_OPTIMIZATIO
2474a 4e 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53  N */...#ifndef S
2474b 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
2474c 41 4c 54 41 42 4c 45 0a 2f 2a 0a 2a 2a 20 43 68  ALTABLE./*.** Ch
2474d 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68  eck to see if th
2474e 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 73 69  e given expressi
2474f 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72  on is of the for
24750 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  m.**.**         
24751 63 6f 6c 75 6d 6e 20 4d 41 54 43 48 20 65 78 70  column MATCH exp
24752 72 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 69 73  r.**.** If it is
24753 20 74 68 65 6e 20 72 65 74 75 72 6e 20 54 52 55   then return TRU
24754 45 2e 20 20 49 66 20 6e 6f 74 2c 20 72 65 74 75  E.  If not, retu
24755 72 6e 20 46 41 4c 53 45 2e 0a 2a 2f 0a 73 74 61  rn FALSE..*/.sta
24756 74 69 63 20 69 6e 74 20 69 73 4d 61 74 63 68 4f  tic int isMatchO
24757 66 43 6f 6c 75 6d 6e 28 0a 20 20 45 78 70 72 20  fColumn(.  Expr 
24758 2a 70 45 78 70 72 20 20 20 20 20 20 2f 2a 20 54  *pExpr      /* T
24759 65 73 74 20 74 68 69 73 20 65 78 70 72 65 73 73  est this express
2475a 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72  ion */.){.  Expr
2475b 4c 69 73 74 20 2a 70 4c 69 73 74 3b 0a 0a 20 20  List *pList;..  
2475c 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  if( pExpr->op!=T
2475d 4b 5f 46 55 4e 43 54 49 4f 4e 20 29 7b 0a 20 20  K_FUNCTION ){.  
2475e 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
2475f 20 20 69 66 28 20 70 45 78 70 72 2d 3e 74 6f 6b    if( pExpr->tok
24760 65 6e 2e 6e 21 3d 35 20 7c 7c 0a 20 20 20 20 20  en.n!=5 ||.     
24761 20 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d    sqlite3StrNICm
24762 70 28 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70  p((const char*)p
24763 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 22 6d  Expr->token.z,"m
24764 61 74 63 68 22 2c 35 29 21 3d 30 20 29 7b 0a 20  atch",5)!=0 ){. 
24765 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
24766 0a 20 20 70 4c 69 73 74 20 3d 20 70 45 78 70 72  .  pList = pExpr
24767 2d 3e 70 4c 69 73 74 3b 0a 20 20 69 66 28 20 70  ->pList;.  if( p
24768 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 32 20 29  List->nExpr!=2 )
24769 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
2476a 20 20 7d 0a 20 20 69 66 28 20 70 4c 69 73 74 2d    }.  if( pList-
2476b 3e 61 5b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70 20  >a[1].pExpr->op 
2476c 21 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a  != TK_COLUMN ){.
2476d 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
2476e 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  }.  return 1;.}.
2476f 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
24770 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
24771 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  LE */../*.** If 
24772 74 68 65 20 70 42 61 73 65 20 65 78 70 72 65 73  the pBase expres
24773 73 69 6f 6e 20 6f 72 69 67 69 6e 61 74 65 64 20  sion originated 
24774 69 6e 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49  in the ON or USI
24775 4e 47 20 63 6c 61 75 73 65 20 6f 66 0a 2a 2a 20  NG clause of.** 
24776 61 20 6a 6f 69 6e 2c 20 74 68 65 6e 20 74 72 61  a join, then tra
24777 6e 73 66 65 72 20 74 68 65 20 61 70 70 72 6f 70  nsfer the approp
24778 72 69 61 74 65 20 6d 61 72 6b 69 6e 67 73 20 6f  riate markings o
24779 76 65 72 20 74 6f 20 64 65 72 69 76 65 64 2e 0a  ver to derived..
2477a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74  */.static void t
2477b 72 61 6e 73 66 65 72 4a 6f 69 6e 4d 61 72 6b 69  ransferJoinMarki
2477c 6e 67 73 28 45 78 70 72 20 2a 70 44 65 72 69 76  ngs(Expr *pDeriv
2477d 65 64 2c 20 45 78 70 72 20 2a 70 42 61 73 65 29  ed, Expr *pBase)
2477e 7b 0a 20 20 70 44 65 72 69 76 65 64 2d 3e 66 6c  {.  pDerived->fl
2477f 61 67 73 20 7c 3d 20 70 42 61 73 65 2d 3e 66 6c  ags |= pBase->fl
24780 61 67 73 20 26 20 45 50 5f 46 72 6f 6d 4a 6f 69  ags & EP_FromJoi
24781 6e 3b 0a 20 20 70 44 65 72 69 76 65 64 2d 3e 69  n;.  pDerived->i
24782 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 3d  RightJoinTable =
24783 20 70 42 61 73 65 2d 3e 69 52 69 67 68 74 4a 6f   pBase->iRightJo
24784 69 6e 54 61 62 6c 65 3b 0a 7d 0a 0a 23 69 66 20  inTable;.}..#if 
24785 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
24786 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41  OMIT_OR_OPTIMIZA
24787 54 49 4f 4e 29 20 26 26 20 21 64 65 66 69 6e 65  TION) && !define
24788 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  d(SQLITE_OMIT_SU
24789 42 51 55 45 52 59 29 0a 2f 2a 0a 2a 2a 20 52 65  BQUERY)./*.** Re
2478a 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65  turn TRUE if the
2478b 20 67 69 76 65 6e 20 74 65 72 6d 20 6f 66 20 61   given term of a
2478c 6e 20 4f 52 20 63 6c 61 75 73 65 20 63 61 6e 20  n OR clause can 
2478d 62 65 20 63 6f 6e 76 65 72 74 65 64 0a 2a 2a 20  be converted.** 
2478e 69 6e 74 6f 20 61 6e 20 49 4e 20 63 6c 61 75 73  into an IN claus
2478f 65 2e 20 20 54 68 65 20 69 43 75 72 73 6f 72 20  e.  The iCursor 
24790 61 6e 64 20 69 43 6f 6c 75 6d 6e 20 64 65 66 69  and iColumn defi
24791 6e 65 20 74 68 65 20 6c 65 66 74 2d 68 61 6e 64  ne the left-hand
24792 0a 2a 2a 20 73 69 64 65 20 6f 66 20 74 68 65 20  .** side of the 
24793 49 4e 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a  IN clause..**.**
24794 20 54 68 65 20 63 6f 6e 74 65 78 74 20 69 73 20   The context is 
24795 74 68 61 74 20 77 65 20 68 61 76 65 20 6d 75 6c  that we have mul
24796 74 69 70 6c 65 20 4f 52 2d 63 6f 6e 6e 65 63 74  tiple OR-connect
24797 65 64 20 65 71 75 61 6c 69 74 79 20 74 65 72 6d  ed equality term
24798 73 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a  s.** like this:.
24799 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  **.**           
2479a 61 3d 3c 65 78 70 72 31 3e 20 4f 52 20 20 61 3d  a=<expr1> OR  a=
2479b 3c 65 78 70 72 32 3e 20 4f 52 20 62 3d 3c 65 78  <expr2> OR b=<ex
2479c 70 72 33 3e 20 20 4f 52 20 2e 2e 2e 0a 2a 2a 0a  pr3>  OR ....**.
2479d 2a 2a 20 54 68 65 20 70 4f 72 54 65 72 6d 20 69  ** The pOrTerm i
2479e 6e 70 75 74 20 74 6f 20 74 68 69 73 20 72 6f 75  nput to this rou
2479f 74 69 6e 65 20 63 6f 72 72 65 73 70 6f 6e 64 73  tine corresponds
247a0 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 74 65 72   to a single ter
247a1 6d 20 6f 66 0a 2a 2a 20 74 68 69 73 20 4f 52 20  m of.** this OR 
247a2 63 6c 61 75 73 65 2e 20 20 49 6e 20 6f 72 64 65  clause.  In orde
247a3 72 20 66 6f 72 20 74 68 65 20 74 65 72 6d 20 74  r for the term t
247a4 6f 20 62 65 20 61 20 63 6f 6e 64 69 64 61 74 65  o be a condidate
247a5 20 66 6f 72 0a 2a 2a 20 63 6f 6e 76 65 72 73 69   for.** conversi
247a6 6f 6e 20 74 6f 20 61 6e 20 49 4e 20 6f 70 65 72  on to an IN oper
247a7 61 74 6f 72 2c 20 74 68 65 20 66 6f 6c 6c 6f 77  ator, the follow
247a8 69 6e 67 20 6d 75 73 74 20 62 65 20 74 72 75 65  ing must be true
247a9 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 20 54  :.**.**     *  T
247aa 68 65 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64  he left-hand sid
247ab 65 20 6f 66 20 74 68 65 20 74 65 72 6d 20 6d 75  e of the term mu
247ac 73 74 20 62 65 20 74 68 65 20 63 6f 6c 75 6d 6e  st be the column
247ad 20 77 68 69 63 68 0a 2a 2a 20 20 20 20 20 20 20   which.**       
247ae 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62   is identified b
247af 79 20 69 43 75 72 73 6f 72 20 61 6e 64 20 69 43  y iCursor and iC
247b0 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  olumn..**.**    
247b1 20 2a 20 20 49 66 20 74 68 65 20 72 69 67 68 74   *  If the right
247b2 2d 68 61 6e 64 20 73 69 64 65 20 69 73 20 61 6c  -hand side is al
247b3 73 6f 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65  so a column, the
247b4 6e 20 74 68 65 20 61 66 66 69 6e 69 74 69 65 73  n the affinities
247b5 0a 2a 2a 20 20 20 20 20 20 20 20 6f 66 20 62 6f  .**        of bo
247b6 74 68 20 72 69 67 68 74 20 61 6e 64 20 6c 65 66  th right and lef
247b7 74 20 73 69 64 65 73 20 6d 75 73 74 20 62 65 20  t sides must be 
247b8 73 75 63 68 20 74 68 61 74 20 6e 6f 20 74 79 70  such that no typ
247b9 65 0a 2a 2a 20 20 20 20 20 20 20 20 63 6f 6e 76  e.**        conv
247ba 65 72 73 69 6f 6e 73 20 61 72 65 20 72 65 71 75  ersions are requ
247bb 69 72 65 64 20 6f 6e 20 74 68 65 20 72 69 67 68  ired on the righ
247bc 74 2e 20 20 28 54 69 63 6b 65 74 20 23 32 32 34  t.  (Ticket #224
247bd 39 29 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f 74 68  9).**.** If both
247be 20 6f 66 20 74 68 65 73 65 20 63 6f 6e 64 69 74   of these condit
247bf 69 6f 6e 73 20 61 72 65 20 74 72 75 65 2c 20 74  ions are true, t
247c0 68 65 6e 20 72 65 74 75 72 6e 20 74 72 75 65 2e  hen return true.
247c1 20 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a 20 72    Otherwise.** r
247c2 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2f 0a  eturn false..*/.
247c3 73 74 61 74 69 63 20 69 6e 74 20 6f 72 54 65 72  static int orTer
247c4 6d 49 73 4f 70 74 43 61 6e 64 69 64 61 74 65 28  mIsOptCandidate(
247c5 57 68 65 72 65 54 65 72 6d 20 2a 70 4f 72 54 65  WhereTerm *pOrTe
247c6 72 6d 2c 20 69 6e 74 20 69 43 75 72 73 6f 72 2c  rm, int iCursor,
247c7 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 29 7b 0a 20   int iColumn){. 
247c8 20 69 6e 74 20 61 66 66 4c 65 66 74 2c 20 61 66   int affLeft, af
247c9 66 52 69 67 68 74 3b 0a 20 20 61 73 73 65 72 74  fRight;.  assert
247ca 28 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72  ( pOrTerm->eOper
247cb 61 74 6f 72 3d 3d 57 4f 5f 45 51 20 29 3b 0a 20  ator==WO_EQ );. 
247cc 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65   if( pOrTerm->le
247cd 66 74 43 75 72 73 6f 72 21 3d 69 43 75 72 73 6f  ftCursor!=iCurso
247ce 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  r ){.    return 
247cf 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 72  0;.  }.  if( pOr
247d0 54 65 72 6d 2d 3e 6c 65 66 74 43 6f 6c 75 6d 6e  Term->leftColumn
247d1 21 3d 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  !=iColumn ){.   
247d2 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
247d3 20 61 66 66 52 69 67 68 74 20 3d 20 73 71 6c 69   affRight = sqli
247d4 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28  te3ExprAffinity(
247d5 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e  pOrTerm->pExpr->
247d6 70 52 69 67 68 74 29 3b 0a 20 20 69 66 28 20 61  pRight);.  if( a
247d7 66 66 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20  ffRight==0 ){.  
247d8 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
247d9 20 20 61 66 66 4c 65 66 74 20 3d 20 73 71 6c 69    affLeft = sqli
247da 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28  te3ExprAffinity(
247db 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e  pOrTerm->pExpr->
247dc 70 4c 65 66 74 29 3b 0a 20 20 69 66 28 20 61 66  pLeft);.  if( af
247dd 66 52 69 67 68 74 21 3d 61 66 66 4c 65 66 74 20  fRight!=affLeft 
247de 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
247df 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b  .  }.  return 1;
247e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
247e1 20 74 72 75 65 20 69 66 20 74 68 65 20 67 69 76   true if the giv
247e2 65 6e 20 74 65 72 6d 20 6f 66 20 61 6e 20 4f 52  en term of an OR
247e3 20 63 6c 61 75 73 65 20 63 61 6e 20 62 65 20 69   clause can be i
247e4 67 6e 6f 72 65 64 20 64 75 72 69 6e 67 0a 2a 2a  gnored during.**
247e5 20 61 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b 65   a check to make
247e6 20 73 75 72 65 20 61 6c 6c 20 4f 52 20 74 65 72   sure all OR ter
247e7 6d 73 20 61 72 65 20 63 61 6e 64 69 64 61 74 65  ms are candidate
247e8 73 20 66 6f 72 20 6f 70 74 69 6d 69 7a 61 74 69  s for optimizati
247e9 6f 6e 2e 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20  on..** In other 
247ea 77 6f 72 64 73 2c 20 72 65 74 75 72 6e 20 74 72  words, return tr
247eb 75 65 20 69 66 20 61 20 63 61 6c 6c 20 74 6f 20  ue if a call to 
247ec 74 68 65 20 6f 72 54 65 72 6d 49 73 4f 70 74 43  the orTermIsOptC
247ed 61 6e 64 69 64 61 74 65 28 29 0a 2a 2a 20 61 62  andidate().** ab
247ee 6f 76 65 20 72 65 74 75 72 6e 65 64 20 66 61 6c  ove returned fal
247ef 73 65 20 62 75 74 20 69 74 20 69 73 20 6e 6f 74  se but it is not
247f0 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 64 69   necessary to di
247f1 73 71 75 61 6c 69 66 79 20 74 68 65 0a 2a 2a 20  squalify the.** 
247f2 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 0a 2a 2a  optimization..**
247f3 0a 2a 2a 20 53 75 70 70 6f 73 65 20 74 68 65 20  .** Suppose the 
247f4 6f 72 69 67 69 6e 61 6c 20 4f 52 20 70 68 72 61  original OR phra
247f5 73 65 20 77 61 73 20 74 68 69 73 3a 0a 2a 2a 0a  se was this:.**.
247f6 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 3d 34  **           a=4
247f7 20 20 4f 52 20 20 61 3d 31 31 20 20 4f 52 20 20    OR  a=11  OR  
247f8 61 3d 62 0a 2a 2a 0a 2a 2a 20 44 75 72 69 6e 67  a=b.**.** During
247f9 20 61 6e 61 6c 79 73 69 73 2c 20 74 68 65 20 74   analysis, the t
247fa 68 69 72 64 20 74 65 72 6d 20 67 65 74 73 20 66  hird term gets f
247fb 6c 69 70 70 65 64 20 61 72 6f 75 6e 64 20 61 6e  lipped around an
247fc 64 20 64 75 70 6c 69 63 61 74 65 0a 2a 2a 20 73  d duplicate.** s
247fd 6f 20 74 68 61 74 20 77 65 20 61 72 65 20 6c 65  o that we are le
247fe 66 74 20 77 69 74 68 20 74 68 69 73 3a 0a 2a 2a  ft with this:.**
247ff 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 3d  .**           a=
24800 34 20 20 4f 52 20 20 61 3d 31 31 20 20 4f 52 20  4  OR  a=11  OR 
24801 20 61 3d 62 20 20 4f 52 20 20 62 3d 61 0a 2a 2a   a=b  OR  b=a.**
24802 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 6c 61  .** Since the la
24803 73 74 20 74 77 6f 20 74 65 72 6d 73 20 61 72 65  st two terms are
24804 20 64 75 70 6c 69 63 61 74 65 73 2c 20 6f 6e 6c   duplicates, onl
24805 79 20 6f 6e 65 20 6f 66 20 74 68 65 6d 0a 2a 2a  y one of them.**
24806 20 68 61 73 20 74 6f 20 71 75 61 6c 69 66 79 20   has to qualify 
24807 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 74 68 65  in order for the
24808 20 77 68 6f 6c 65 20 70 68 72 61 73 65 20 74 6f   whole phrase to
24809 20 71 75 61 6c 69 66 79 2e 20 20 57 68 65 6e 0a   qualify.  When.
2480a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ** this routine 
2480b 69 73 20 63 61 6c 6c 65 64 2c 20 77 65 20 6b 6e  is called, we kn
2480c 6f 77 20 74 68 61 74 20 70 4f 72 54 65 72 6d 20  ow that pOrTerm 
2480d 64 69 64 20 6e 6f 74 20 71 75 61 6c 69 66 79 2e  did not qualify.
2480e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2480f 20 6d 65 72 65 6c 79 20 63 68 65 63 6b 73 20 74   merely checks t
24810 6f 20 73 65 65 20 69 66 20 70 4f 72 54 65 72 6d  o see if pOrTerm
24811 20 68 61 73 20 61 20 64 75 70 6c 69 63 61 74 65   has a duplicate
24812 20 74 68 61 74 0a 2a 2a 20 6d 69 67 68 74 20 71   that.** might q
24813 75 61 6c 69 66 79 2e 20 20 49 66 20 74 68 65 72  ualify.  If ther
24814 65 20 69 73 20 61 20 64 75 70 6c 69 63 61 74 65  e is a duplicate
24815 20 74 68 61 74 20 68 61 73 20 6e 6f 74 20 79 65   that has not ye
24816 74 20 62 65 65 6e 0a 2a 2a 20 64 69 73 71 75 61  t been.** disqua
24817 6c 69 66 69 65 64 2c 20 74 68 65 6e 20 72 65 74  lified, then ret
24818 75 72 6e 20 74 72 75 65 2e 20 20 49 66 20 74 68  urn true.  If th
24819 65 72 65 20 61 72 65 20 6e 6f 20 64 75 70 6c 69  ere are no dupli
2481a 63 61 74 65 73 2c 20 6f 72 0a 2a 2a 20 74 68 65  cates, or.** the
2481b 20 64 75 70 6c 69 63 61 74 65 20 68 61 73 20 61   duplicate has a
2481c 6c 73 6f 20 62 65 65 6e 20 64 69 73 71 75 61 6c  lso been disqual
2481d 69 66 65 64 2c 20 72 65 74 75 72 6e 20 66 61 6c  ifed, return fal
2481e 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  se..*/.static in
2481f 74 20 6f 72 54 65 72 6d 48 61 73 4f 6b 44 75 70  t orTermHasOkDup
24820 6c 69 63 61 74 65 28 57 68 65 72 65 43 6c 61 75  licate(WhereClau
24821 73 65 20 2a 70 4f 72 2c 20 57 68 65 72 65 54 65  se *pOr, WhereTe
24822 72 6d 20 2a 70 4f 72 54 65 72 6d 29 7b 0a 20 20  rm *pOrTerm){.  
24823 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 66 6c 61  if( pOrTerm->fla
24824 67 73 20 26 20 54 45 52 4d 5f 43 4f 50 49 45 44  gs & TERM_COPIED
24825 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20   ){.    /* This 
24826 69 73 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  is the original 
24827 74 65 72 6d 2e 20 20 54 68 65 20 64 75 70 6c 69  term.  The dupli
24828 63 61 74 65 20 69 73 20 74 6f 20 74 68 65 20 6c  cate is to the l
24829 65 66 74 20 68 61 64 0a 20 20 20 20 2a 2a 20 68  eft had.    ** h
2482a 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20  as not yet been 
2482b 61 6e 61 6c 79 7a 65 64 20 61 6e 64 20 74 68 75  analyzed and thu
2482c 73 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65  s has not yet be
2482d 65 6e 20 64 69 73 71 75 61 6c 69 66 69 65 64 2e  en disqualified.
2482e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31   */.    return 1
2482f 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 70 4f 72  ;.  }.  if( (pOr
24830 54 65 72 6d 2d 3e 66 6c 61 67 73 20 26 20 54 45  Term->flags & TE
24831 52 4d 5f 56 49 52 54 55 41 4c 29 21 3d 30 0a 20  RM_VIRTUAL)!=0. 
24832 20 20 20 20 26 26 20 28 70 4f 72 2d 3e 61 5b 70      && (pOr->a[p
24833 4f 72 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 5d  OrTerm->iParent]
24834 2e 66 6c 61 67 73 20 26 20 54 45 52 4d 5f 4f 52  .flags & TERM_OR
24835 5f 4f 4b 29 21 3d 30 20 29 7b 0a 20 20 20 20 2f  _OK)!=0 ){.    /
24836 2a 20 54 68 69 73 20 69 73 20 61 20 64 75 70 6c  * This is a dupl
24837 69 63 61 74 65 20 74 65 72 6d 2e 20 20 54 68 65  icate term.  The
24838 20 6f 72 69 67 69 6e 61 6c 20 71 75 61 6c 69 66   original qualif
24839 69 65 64 20 73 6f 20 74 68 69 73 20 6f 6e 65 0a  ied so this one.
2483a 20 20 20 20 2a 2a 20 64 6f 65 73 20 6e 6f 74 20      ** does not 
2483b 68 61 76 65 20 74 6f 2e 20 2a 2f 0a 20 20 20 20  have to. */.    
2483c 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20  return 1;.  }.  
2483d 2f 2a 20 54 68 69 73 20 69 73 20 65 69 74 68 65  /* This is eithe
2483e 72 20 61 20 73 69 6e 67 6c 65 74 6f 6e 20 74 65  r a singleton te
2483f 72 6d 20 6f 72 20 65 6c 73 65 20 69 74 20 69 73  rm or else it is
24840 20 61 20 64 75 70 6c 69 63 61 74 65 20 66 6f 72   a duplicate for
24841 0a 20 20 2a 2a 20 77 68 69 63 68 20 74 68 65 20  .  ** which the 
24842 6f 72 69 67 69 6e 61 6c 20 64 69 64 20 6e 6f 74  original did not
24843 20 71 75 61 6c 69 66 79 2e 20 20 45 69 74 68 65   qualify.  Eithe
24844 72 20 77 61 79 20 77 65 20 61 72 65 20 64 6f 6e  r way we are don
24845 65 20 66 6f 72 2e 20 2a 2f 0a 20 20 72 65 74 75  e for. */.  retu
24846 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  rn 0;.}.#endif /
24847 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f  * !SQLITE_OMIT_O
24848 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 26  R_OPTIMIZATION &
24849 26 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  & !SQLITE_OMIT_S
2484a 55 42 51 55 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a  UBQUERY */../*.*
2484b 2a 20 54 68 65 20 69 6e 70 75 74 20 74 6f 20 74  * The input to t
2484c 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
2484d 6e 20 57 68 65 72 65 54 65 72 6d 20 73 74 72 75  n WhereTerm stru
2484e 63 74 75 72 65 20 77 69 74 68 20 6f 6e 6c 79 20  cture with only 
2484f 74 68 65 0a 2a 2a 20 22 70 45 78 70 72 22 20 66  the.** "pExpr" f
24850 69 65 6c 64 20 66 69 6c 6c 65 64 20 69 6e 2e 20  ield filled in. 
24851 20 54 68 65 20 6a 6f 62 20 6f 66 20 74 68 69 73   The job of this
24852 20 72 6f 75 74 69 6e 65 20 69 73 20 74 6f 20 61   routine is to a
24853 6e 61 6c 79 7a 65 20 74 68 65 0a 2a 2a 20 73 75  nalyze the.** su
24854 62 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20  bexpression and 
24855 70 6f 70 75 6c 61 74 65 20 61 6c 6c 20 74 68 65  populate all the
24856 20 6f 74 68 65 72 20 66 69 65 6c 64 73 20 6f 66   other fields of
24857 20 74 68 65 20 57 68 65 72 65 54 65 72 6d 0a 2a   the WhereTerm.*
24858 2a 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  * structure..**.
24859 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73  ** If the expres
2485a 73 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66  sion is of the f
2485b 6f 72 6d 20 22 3c 65 78 70 72 3e 20 3c 6f 70 3e  orm "<expr> <op>
2485c 20 58 22 20 69 74 20 67 65 74 73 20 63 6f 6d 6d   X" it gets comm
2485d 75 74 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 73  uted.** to the s
2485e 74 61 6e 64 61 72 64 20 66 6f 72 6d 20 6f 66 20  tandard form of 
2485f 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 2e  "X <op> <expr>".
24860 20 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73    If the express
24861 69 6f 6e 20 69 73 20 6f 66 0a 2a 2a 20 74 68 65  ion is of.** the
24862 20 66 6f 72 6d 20 22 58 20 3c 6f 70 3e 20 59 22   form "X <op> Y"
24863 20 77 68 65 72 65 20 62 6f 74 68 20 58 20 61 6e   where both X an
24864 64 20 59 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2c  d Y are columns,
24865 20 74 68 65 6e 20 74 68 65 20 6f 72 69 67 69 6e   then the origin
24866 61 6c 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  al.** expression
24867 20 69 73 20 75 6e 63 68 61 6e 67 65 64 20 61 6e   is unchanged an
24868 64 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20  d a new virtual 
24869 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 74 68  expression of th
2486a 65 20 66 6f 72 6d 0a 2a 2a 20 22 59 20 3c 6f 70  e form.** "Y <op
2486b 3e 20 58 22 20 69 73 20 61 64 64 65 64 20 74 6f  > X" is added to
2486c 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2486d 65 20 61 6e 64 20 61 6e 61 6c 79 7a 65 64 20 73  e and analyzed s
2486e 65 70 61 72 61 74 65 6c 79 2e 0a 2a 2f 0a 73 74  eparately..*/.st
2486f 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 41 6e  atic void exprAn
24870 61 6c 79 7a 65 28 0a 20 20 53 72 63 4c 69 73 74  alyze(.  SrcList
24871 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20 20 20   *pSrc,         
24872 20 20 20 2f 2a 20 74 68 65 20 46 52 4f 4d 20 63     /* the FROM c
24873 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 72 65  lause */.  Where
24874 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20  Clause *pWC,    
24875 20 20 20 20 20 2f 2a 20 74 68 65 20 57 48 45 52       /* the WHER
24876 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e  E clause */.  in
24877 74 20 69 64 78 54 65 72 6d 20 20 20 20 20 20 20  t idxTerm       
24878 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
24879 20 6f 66 20 74 68 65 20 74 65 72 6d 20 74 6f 20   of the term to 
2487a 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 29  be analyzed */.)
2487b 7b 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70  {.  WhereTerm *p
2487c 54 65 72 6d 3b 0a 20 20 45 78 70 72 4d 61 73 6b  Term;.  ExprMask
2487d 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 3b 0a 20  Set *pMaskSet;. 
2487e 20 45 78 70 72 20 2a 70 45 78 70 72 3b 0a 20 20   Expr *pExpr;.  
2487f 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71 4c 65  Bitmask prereqLe
24880 66 74 3b 0a 20 20 42 69 74 6d 61 73 6b 20 70 72  ft;.  Bitmask pr
24881 65 72 65 71 41 6c 6c 3b 0a 20 20 42 69 74 6d 61  ereqAll;.  Bitma
24882 73 6b 20 65 78 74 72 61 52 69 67 68 74 20 3d 20  sk extraRight = 
24883 30 3b 0a 20 20 69 6e 74 20 6e 50 61 74 74 65 72  0;.  int nPatter
24884 6e 3b 0a 20 20 69 6e 74 20 69 73 43 6f 6d 70 6c  n;.  int isCompl
24885 65 74 65 3b 0a 20 20 69 6e 74 20 6e 6f 43 61 73  ete;.  int noCas
24886 65 3b 0a 20 20 69 6e 74 20 6f 70 3b 0a 20 20 50  e;.  int op;.  P
24887 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70  arse *pParse = p
24888 57 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 73 71  WC->pParse;.  sq
24889 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2488a 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 64  se->db;..  if( d
2488b 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2488c 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
2488d 20 7d 0a 20 20 70 54 65 72 6d 20 3d 20 26 70 57   }.  pTerm = &pW
2488e 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20  C->a[idxTerm];. 
2488f 20 70 4d 61 73 6b 53 65 74 20 3d 20 70 57 43 2d   pMaskSet = pWC-
24890 3e 70 4d 61 73 6b 53 65 74 3b 0a 20 20 70 45 78  >pMaskSet;.  pEx
24891 70 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70  pr = pTerm->pExp
24892 72 3b 0a 20 20 70 72 65 72 65 71 4c 65 66 74 20  r;.  prereqLeft 
24893 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65  = exprTableUsage
24894 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72  (pMaskSet, pExpr
24895 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 6f 70 20 3d  ->pLeft);.  op =
24896 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 69 66   pExpr->op;.  if
24897 28 20 6f 70 3d 3d 54 4b 5f 49 4e 20 29 7b 0a 20  ( op==TK_IN ){. 
24898 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
24899 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a 20  ->pRight==0 );. 
2489a 20 20 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71     pTerm->prereq
2489b 52 69 67 68 74 20 3d 20 65 78 70 72 4c 69 73 74  Right = exprList
2489c 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b  TableUsage(pMask
2489d 53 65 74 2c 20 70 45 78 70 72 2d 3e 70 4c 69 73  Set, pExpr->pLis
2489e 74 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t).             
2489f 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 65               | e
248a0 78 70 72 53 65 6c 65 63 74 54 61 62 6c 65 55 73  xprSelectTableUs
248a1 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 45  age(pMaskSet, pE
248a2 78 70 72 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20  xpr->pSelect);. 
248a3 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54   }else if( op==T
248a4 4b 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  K_ISNULL ){.    
248a5 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67  pTerm->prereqRig
248a6 68 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  ht = 0;.  }else{
248a7 0a 20 20 20 20 70 54 65 72 6d 2d 3e 70 72 65 72  .    pTerm->prer
248a8 65 71 52 69 67 68 74 20 3d 20 65 78 70 72 54 61  eqRight = exprTa
248a9 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65  bleUsage(pMaskSe
248aa 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  t, pExpr->pRight
248ab 29 3b 0a 20 20 7d 0a 20 20 70 72 65 72 65 71 41  );.  }.  prereqA
248ac 6c 6c 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73  ll = exprTableUs
248ad 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 45  age(pMaskSet, pE
248ae 78 70 72 29 3b 0a 20 20 69 66 28 20 45 78 70 72  xpr);.  if( Expr
248af 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
248b0 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20  r, EP_FromJoin) 
248b1 29 7b 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 78  ){.    Bitmask x
248b2 20 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b   = getMask(pMask
248b3 53 65 74 2c 20 70 45 78 70 72 2d 3e 69 52 69 67  Set, pExpr->iRig
248b4 68 74 4a 6f 69 6e 54 61 62 6c 65 29 3b 0a 20 20  htJoinTable);.  
248b5 20 20 70 72 65 72 65 71 41 6c 6c 20 7c 3d 20 78    prereqAll |= x
248b6 3b 0a 20 20 20 20 65 78 74 72 61 52 69 67 68 74  ;.    extraRight
248b7 20 3d 20 78 2d 31 3b 20 20 2f 2a 20 4f 4e 20 63   = x-1;  /* ON c
248b8 6c 61 75 73 65 20 74 65 72 6d 73 20 6d 61 79 20  lause terms may 
248b9 6e 6f 74 20 62 65 20 75 73 65 64 20 77 69 74 68  not be used with
248ba 20 61 6e 20 69 6e 64 65 78 0a 20 20 20 20 20 20   an index.      
248bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
248bc 20 2a 2a 20 6f 6e 20 6c 65 66 74 20 74 61 62 6c   ** on left tabl
248bd 65 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e  e of a LEFT JOIN
248be 2e 20 20 54 69 63 6b 65 74 20 23 33 30 31 35 20  .  Ticket #3015 
248bf 2a 2f 0a 20 20 7d 0a 20 20 70 54 65 72 6d 2d 3e  */.  }.  pTerm->
248c0 70 72 65 72 65 71 41 6c 6c 20 3d 20 70 72 65 72  prereqAll = prer
248c1 65 71 41 6c 6c 3b 0a 20 20 70 54 65 72 6d 2d 3e  eqAll;.  pTerm->
248c2 6c 65 66 74 43 75 72 73 6f 72 20 3d 20 2d 31 3b  leftCursor = -1;
248c3 0a 20 20 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e  .  pTerm->iParen
248c4 74 20 3d 20 2d 31 3b 0a 20 20 70 54 65 72 6d 2d  t = -1;.  pTerm-
248c5 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 30 3b 0a  >eOperator = 0;.
248c6 20 20 69 66 28 20 61 6c 6c 6f 77 65 64 4f 70 28    if( allowedOp(
248c7 6f 70 29 20 26 26 20 28 70 54 65 72 6d 2d 3e 70  op) && (pTerm->p
248c8 72 65 72 65 71 52 69 67 68 74 20 26 20 70 72 65  rereqRight & pre
248c9 72 65 71 4c 65 66 74 29 3d 3d 30 20 29 7b 0a 20  reqLeft)==0 ){. 
248ca 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d     Expr *pLeft =
248cb 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20   pExpr->pLeft;. 
248cc 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 20     Expr *pRight 
248cd 3d 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b  = pExpr->pRight;
248ce 0a 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e  .    if( pLeft->
248cf 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b  op==TK_COLUMN ){
248d0 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6c 65  .      pTerm->le
248d1 66 74 43 75 72 73 6f 72 20 3d 20 70 4c 65 66 74  ftCursor = pLeft
248d2 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20  ->iTable;.      
248d3 70 54 65 72 6d 2d 3e 6c 65 66 74 43 6f 6c 75 6d  pTerm->leftColum
248d4 6e 20 3d 20 70 4c 65 66 74 2d 3e 69 43 6f 6c 75  n = pLeft->iColu
248d5 6d 6e 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d  mn;.      pTerm-
248d6 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 6f 70 65  >eOperator = ope
248d7 72 61 74 6f 72 4d 61 73 6b 28 6f 70 29 3b 0a 20  ratorMask(op);. 
248d8 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 52 69     }.    if( pRi
248d9 67 68 74 20 26 26 20 70 52 69 67 68 74 2d 3e 6f  ght && pRight->o
248da 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a  p==TK_COLUMN ){.
248db 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20        WhereTerm 
248dc 2a 70 4e 65 77 3b 0a 20 20 20 20 20 20 45 78 70  *pNew;.      Exp
248dd 72 20 2a 70 44 75 70 3b 0a 20 20 20 20 20 20 69  r *pDup;.      i
248de 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75  f( pTerm->leftCu
248df 72 73 6f 72 3e 3d 30 20 29 7b 0a 20 20 20 20 20  rsor>=0 ){.     
248e0 20 20 20 69 6e 74 20 69 64 78 4e 65 77 3b 0a 20     int idxNew;. 
248e1 20 20 20 20 20 20 20 70 44 75 70 20 3d 20 73 71         pDup = sq
248e2 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
248e3 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20   pExpr);.       
248e4 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
248e5 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20  ailed ){.       
248e6 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
248e7 6c 65 74 65 28 70 44 75 70 29 3b 0a 20 20 20 20  lete(pDup);.    
248e8 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
248e9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
248ea 69 64 78 4e 65 77 20 3d 20 77 68 65 72 65 43 6c  idxNew = whereCl
248eb 61 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20  auseInsert(pWC, 
248ec 70 44 75 70 2c 20 54 45 52 4d 5f 56 49 52 54 55  pDup, TERM_VIRTU
248ed 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29  AL|TERM_DYNAMIC)
248ee 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 64  ;.        if( id
248ef 78 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e  xNew==0 ) return
248f0 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d  ;.        pNew =
248f1 20 26 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 5d   &pWC->a[idxNew]
248f2 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e  ;.        pNew->
248f3 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72  iParent = idxTer
248f4 6d 3b 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d  m;.        pTerm
248f5 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65   = &pWC->a[idxTe
248f6 72 6d 5d 3b 0a 20 20 20 20 20 20 20 20 70 54 65  rm];.        pTe
248f7 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 31 3b 0a  rm->nChild = 1;.
248f8 20 20 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 66          pTerm->f
248f9 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f 50  lags |= TERM_COP
248fa 49 45 44 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  IED;.      }else
248fb 7b 0a 20 20 20 20 20 20 20 20 70 44 75 70 20 3d  {.        pDup =
248fc 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20   pExpr;.        
248fd 70 4e 65 77 20 3d 20 70 54 65 72 6d 3b 0a 20 20  pNew = pTerm;.  
248fe 20 20 20 20 7d 0a 20 20 20 20 20 20 65 78 70 72      }.      expr
248ff 43 6f 6d 6d 75 74 65 28 70 44 75 70 29 3b 0a 20  Commute(pDup);. 
24900 20 20 20 20 20 70 4c 65 66 74 20 3d 20 70 44 75       pLeft = pDu
24901 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20  p->pLeft;.      
24902 70 4e 65 77 2d 3e 6c 65 66 74 43 75 72 73 6f 72  pNew->leftCursor
24903 20 3d 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c 65   = pLeft->iTable
24904 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 6c 65  ;.      pNew->le
24905 66 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65 66 74  ftColumn = pLeft
24906 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20  ->iColumn;.     
24907 20 70 4e 65 77 2d 3e 70 72 65 72 65 71 52 69 67   pNew->prereqRig
24908 68 74 20 3d 20 70 72 65 72 65 71 4c 65 66 74 3b  ht = prereqLeft;
24909 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 72 65  .      pNew->pre
2490a 72 65 71 41 6c 6c 20 3d 20 70 72 65 72 65 71 41  reqAll = prereqA
2490b 6c 6c 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e  ll;.      pNew->
2490c 65 4f 70 65 72 61 74 6f 72 20 3d 20 6f 70 65 72  eOperator = oper
2490d 61 74 6f 72 4d 61 73 6b 28 70 44 75 70 2d 3e 6f  atorMask(pDup->o
2490e 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 23  p);.    }.  }..#
2490f 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
24910 49 54 5f 42 45 54 57 45 45 4e 5f 4f 50 54 49 4d  IT_BETWEEN_OPTIM
24911 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 49 66 20  IZATION.  /* If 
24912 61 20 74 65 72 6d 20 69 73 20 74 68 65 20 42 45  a term is the BE
24913 54 57 45 45 4e 20 6f 70 65 72 61 74 6f 72 2c 20  TWEEN operator, 
24914 63 72 65 61 74 65 20 74 77 6f 20 6e 65 77 20 76  create two new v
24915 69 72 74 75 61 6c 20 74 65 72 6d 73 0a 20 20 2a  irtual terms.  *
24916 2a 20 74 68 61 74 20 64 65 66 69 6e 65 20 74 68  * that define th
24917 65 20 72 61 6e 67 65 20 74 68 61 74 20 74 68 65  e range that the
24918 20 42 45 54 57 45 45 4e 20 69 6d 70 6c 65 6d 65   BETWEEN impleme
24919 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65  nts..  */.  else
2491a 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d   if( pExpr->op==
2491b 54 4b 5f 42 45 54 57 45 45 4e 20 29 7b 0a 20 20  TK_BETWEEN ){.  
2491c 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
2491d 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74  t = pExpr->pList
2491e 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  ;.    int i;.   
2491f 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38   static const u8
24920 20 6f 70 73 5b 5d 20 3d 20 7b 54 4b 5f 47 45 2c   ops[] = {TK_GE,
24921 20 54 4b 5f 4c 45 7d 3b 0a 20 20 20 20 61 73 73   TK_LE};.    ass
24922 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29 3b  ert( pList!=0 );
24923 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 69  .    assert( pLi
24924 73 74 2d 3e 6e 45 78 70 72 3d 3d 32 20 29 3b 0a  st->nExpr==2 );.
24925 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32      for(i=0; i<2
24926 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78  ; i++){.      Ex
24927 70 72 20 2a 70 4e 65 77 45 78 70 72 3b 0a 20 20  pr *pNewExpr;.  
24928 20 20 20 20 69 6e 74 20 69 64 78 4e 65 77 3b 0a      int idxNew;.
24929 20 20 20 20 20 20 70 4e 65 77 45 78 70 72 20 3d        pNewExpr =
2492a 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c   sqlite3Expr(db,
2492b 20 6f 70 73 5b 69 5d 2c 20 73 71 6c 69 74 65 33   ops[i], sqlite3
2492c 45 78 70 72 44 75 70 28 64 62 2c 20 70 45 78 70  ExprDup(db, pExp
2492d 72 2d 3e 70 4c 65 66 74 29 2c 0a 20 20 20 20 20  r->pLeft),.     
2492e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2492f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
24930 78 70 72 44 75 70 28 64 62 2c 20 70 4c 69 73 74  xprDup(db, pList
24931 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 2c 20 30  ->a[i].pExpr), 0
24932 29 3b 0a 20 20 20 20 20 20 69 64 78 4e 65 77 20  );.      idxNew 
24933 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73  = whereClauseIns
24934 65 72 74 28 70 57 43 2c 20 70 4e 65 77 45 78 70  ert(pWC, pNewExp
24935 72 2c 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c  r, TERM_VIRTUAL|
24936 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  TERM_DYNAMIC);. 
24937 20 20 20 20 20 65 78 70 72 41 6e 61 6c 79 7a 65       exprAnalyze
24938 28 70 53 72 63 2c 20 70 57 43 2c 20 69 64 78 4e  (pSrc, pWC, idxN
24939 65 77 29 3b 0a 20 20 20 20 20 20 70 54 65 72 6d  ew);.      pTerm
2493a 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65   = &pWC->a[idxTe
2493b 72 6d 5d 3b 0a 20 20 20 20 20 20 70 57 43 2d 3e  rm];.      pWC->
2493c 61 5b 69 64 78 4e 65 77 5d 2e 69 50 61 72 65 6e  a[idxNew].iParen
2493d 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20  t = idxTerm;.   
2493e 20 7d 0a 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43   }.    pTerm->nC
2493f 68 69 6c 64 20 3d 20 32 3b 0a 20 20 7d 0a 23 65  hild = 2;.  }.#e
24940 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
24941 4d 49 54 5f 42 45 54 57 45 45 4e 5f 4f 50 54 49  MIT_BETWEEN_OPTI
24942 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 23 69 66  MIZATION */..#if
24943 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
24944 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a  _OMIT_OR_OPTIMIZ
24945 41 54 49 4f 4e 29 20 26 26 20 21 64 65 66 69 6e  ATION) && !defin
24946 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ed(SQLITE_OMIT_S
24947 55 42 51 55 45 52 59 29 0a 20 20 2f 2a 20 41 74  UBQUERY).  /* At
24948 74 65 6d 70 74 20 74 6f 20 63 6f 6e 76 65 72 74  tempt to convert
24949 20 4f 52 2d 63 6f 6e 6e 65 63 74 65 64 20 74 65   OR-connected te
2494a 72 6d 73 20 69 6e 74 6f 20 61 6e 20 49 4e 20 6f  rms into an IN o
2494b 70 65 72 61 74 6f 72 20 73 6f 20 74 68 61 74 0a  perator so that.
2494c 20 20 2a 2a 20 74 68 65 79 20 63 61 6e 20 6d 61    ** they can ma
2494d 6b 65 20 75 73 65 20 6f 66 20 69 6e 64 69 63 65  ke use of indice
2494e 73 2e 20 20 45 78 61 6d 70 6c 65 3a 0a 20 20 2a  s.  Example:.  *
2494f 2a 0a 20 20 2a 2a 20 20 20 20 20 20 78 20 3d 20  *.  **      x = 
24950 65 78 70 72 31 20 20 4f 52 20 20 65 78 70 72 32  expr1  OR  expr2
24951 20 3d 20 78 20 20 4f 52 20 20 78 20 3d 20 65 78   = x  OR  x = ex
24952 70 72 33 0a 20 20 2a 2a 0a 20 20 2a 2a 20 69 73  pr3.  **.  ** is
24953 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 0a   converted into.
24954 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 78    **.  **      x
24955 20 49 4e 20 28 65 78 70 72 31 2c 65 78 70 72 32   IN (expr1,expr2
24956 2c 65 78 70 72 33 29 0a 20 20 2a 2a 0a 20 20 2a  ,expr3).  **.  *
24957 2a 20 54 68 69 73 20 6f 70 74 69 6d 69 7a 61 74  * This optimizat
24958 69 6f 6e 20 6d 75 73 74 20 62 65 20 6f 6d 69 74  ion must be omit
24959 74 65 64 20 69 66 20 4f 4d 49 54 5f 53 55 42 51  ted if OMIT_SUBQ
2495a 55 45 52 59 20 69 73 20 64 65 66 69 6e 65 64 20  UERY is defined 
2495b 62 65 63 61 75 73 65 0a 20 20 2a 2a 20 74 68 65  because.  ** the
2495c 20 63 6f 6d 70 69 6c 65 72 20 66 6f 72 20 74 68   compiler for th
2495d 65 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f  e the IN operato
2495e 72 20 69 73 20 70 61 72 74 20 6f 66 20 73 75 62  r is part of sub
2495f 2d 71 75 65 72 69 65 73 2e 0a 20 20 2a 2f 0a 20  -queries..  */. 
24960 20 65 6c 73 65 20 69 66 28 20 70 45 78 70 72 2d   else if( pExpr-
24961 3e 6f 70 3d 3d 54 4b 5f 4f 52 20 29 7b 0a 20 20  >op==TK_OR ){.  
24962 20 20 69 6e 74 20 6f 6b 3b 0a 20 20 20 20 69 6e    int ok;.    in
24963 74 20 69 2c 20 6a 3b 0a 20 20 20 20 69 6e 74 20  t i, j;.    int 
24964 69 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 73 6f 72  iColumn, iCursor
24965 3b 0a 20 20 20 20 57 68 65 72 65 43 6c 61 75 73  ;.    WhereClaus
24966 65 20 73 4f 72 3b 0a 20 20 20 20 57 68 65 72 65  e sOr;.    Where
24967 54 65 72 6d 20 2a 70 4f 72 54 65 72 6d 3b 0a 0a  Term *pOrTerm;..
24968 20 20 20 20 61 73 73 65 72 74 28 20 28 70 54 65      assert( (pTe
24969 72 6d 2d 3e 66 6c 61 67 73 20 26 20 54 45 52 4d  rm->flags & TERM
2496a 5f 44 59 4e 41 4d 49 43 29 3d 3d 30 20 29 3b 0a  _DYNAMIC)==0 );.
2496b 20 20 20 20 77 68 65 72 65 43 6c 61 75 73 65 49      whereClauseI
2496c 6e 69 74 28 26 73 4f 72 2c 20 70 57 43 2d 3e 70  nit(&sOr, pWC->p
2496d 50 61 72 73 65 2c 20 70 4d 61 73 6b 53 65 74 29  Parse, pMaskSet)
2496e 3b 0a 20 20 20 20 77 68 65 72 65 53 70 6c 69 74  ;.    whereSplit
2496f 28 26 73 4f 72 2c 20 70 45 78 70 72 2c 20 54 4b  (&sOr, pExpr, TK
24970 5f 4f 52 29 3b 0a 20 20 20 20 65 78 70 72 41 6e  _OR);.    exprAn
24971 61 6c 79 7a 65 41 6c 6c 28 70 53 72 63 2c 20 26  alyzeAll(pSrc, &
24972 73 4f 72 29 3b 0a 20 20 20 20 61 73 73 65 72 74  sOr);.    assert
24973 28 20 73 4f 72 2e 6e 54 65 72 6d 3e 3d 32 20 29  ( sOr.nTerm>=2 )
24974 3b 0a 20 20 20 20 6a 20 3d 20 30 3b 0a 20 20 20  ;.    j = 0;.   
24975 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
24976 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 6f 72 5f  ailed ) goto or_
24977 6e 6f 74 5f 70 6f 73 73 69 62 6c 65 3b 0a 20 20  not_possible;.  
24978 20 20 64 6f 7b 0a 20 20 20 20 20 20 61 73 73 65    do{.      asse
24979 72 74 28 20 6a 3c 73 4f 72 2e 6e 54 65 72 6d 20  rt( j<sOr.nTerm 
2497a 29 3b 0a 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e  );.      iColumn
2497b 20 3d 20 73 4f 72 2e 61 5b 6a 5d 2e 6c 65 66 74   = sOr.a[j].left
2497c 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 69 43  Column;.      iC
2497d 75 72 73 6f 72 20 3d 20 73 4f 72 2e 61 5b 6a 5d  ursor = sOr.a[j]
2497e 2e 6c 65 66 74 43 75 72 73 6f 72 3b 0a 20 20 20  .leftCursor;.   
2497f 20 20 20 6f 6b 20 3d 20 69 43 75 72 73 6f 72 3e     ok = iCursor>
24980 3d 30 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  =0;.      for(i=
24981 73 4f 72 2e 6e 54 65 72 6d 2d 31 2c 20 70 4f 72  sOr.nTerm-1, pOr
24982 54 65 72 6d 3d 73 4f 72 2e 61 3b 20 69 3e 3d 30  Term=sOr.a; i>=0
24983 20 26 26 20 6f 6b 3b 20 69 2d 2d 2c 20 70 4f 72   && ok; i--, pOr
24984 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Term++){.       
24985 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 65 4f   if( pOrTerm->eO
24986 70 65 72 61 74 6f 72 21 3d 57 4f 5f 45 51 20 29  perator!=WO_EQ )
24987 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  {.          goto
24988 20 6f 72 5f 6e 6f 74 5f 70 6f 73 73 69 62 6c 65   or_not_possible
24989 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2498a 20 20 20 20 69 66 28 20 6f 72 54 65 72 6d 49 73      if( orTermIs
2498b 4f 70 74 43 61 6e 64 69 64 61 74 65 28 70 4f 72  OptCandidate(pOr
2498c 54 65 72 6d 2c 20 69 43 75 72 73 6f 72 2c 20 69  Term, iCursor, i
2498d 43 6f 6c 75 6d 6e 29 20 29 7b 0a 20 20 20 20 20  Column) ){.     
2498e 20 20 20 20 20 70 4f 72 54 65 72 6d 2d 3e 66 6c       pOrTerm->fl
2498f 61 67 73 20 7c 3d 20 54 45 52 4d 5f 4f 52 5f 4f  ags |= TERM_OR_O
24990 4b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  K;.        }else
24991 20 69 66 28 20 6f 72 54 65 72 6d 48 61 73 4f 6b   if( orTermHasOk
24992 44 75 70 6c 69 63 61 74 65 28 26 73 4f 72 2c 20  Duplicate(&sOr, 
24993 70 4f 72 54 65 72 6d 29 20 29 7b 0a 20 20 20 20  pOrTerm) ){.    
24994 20 20 20 20 20 20 70 4f 72 54 65 72 6d 2d 3e 66        pOrTerm->f
24995 6c 61 67 73 20 26 3d 20 7e 54 45 52 4d 5f 4f 52  lags &= ~TERM_OR
24996 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  _OK;.        }el
24997 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 6f 6b  se{.          ok
24998 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 0;.        }.
24999 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 77 68 69        }.    }whi
2499a 6c 65 28 20 21 6f 6b 20 26 26 20 28 73 4f 72 2e  le( !ok && (sOr.
2499b 61 5b 6a 2b 2b 5d 2e 66 6c 61 67 73 20 26 20 54  a[j++].flags & T
2499c 45 52 4d 5f 43 4f 50 49 45 44 29 21 3d 30 20 26  ERM_COPIED)!=0 &
2499d 26 20 6a 3c 32 20 29 3b 0a 20 20 20 20 69 66 28  & j<2 );.    if(
2499e 20 6f 6b 20 29 7b 0a 20 20 20 20 20 20 45 78 70   ok ){.      Exp
2499f 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 30  rList *pList = 0
249a0 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e  ;.      Expr *pN
249a1 65 77 2c 20 2a 70 44 75 70 3b 0a 20 20 20 20 20  ew, *pDup;.     
249a2 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 30   Expr *pLeft = 0
249a3 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 73 4f  ;.      for(i=sO
249a4 72 2e 6e 54 65 72 6d 2d 31 2c 20 70 4f 72 54 65  r.nTerm-1, pOrTe
249a5 72 6d 3d 73 4f 72 2e 61 3b 20 69 3e 3d 30 20 26  rm=sOr.a; i>=0 &
249a6 26 20 6f 6b 3b 20 69 2d 2d 2c 20 70 4f 72 54 65  & ok; i--, pOrTe
249a7 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69  rm++){.        i
249a8 66 28 20 28 70 4f 72 54 65 72 6d 2d 3e 66 6c 61  f( (pOrTerm->fla
249a9 67 73 20 26 20 54 45 52 4d 5f 4f 52 5f 4f 4b 29  gs & TERM_OR_OK)
249aa 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==0 ) continue;.
249ab 20 20 20 20 20 20 20 20 70 44 75 70 20 3d 20 73          pDup = s
249ac 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
249ad 2c 20 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72  , pOrTerm->pExpr
249ae 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20 20  ->pRight);.     
249af 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74     pList = sqlit
249b0 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
249b1 28 70 57 43 2d 3e 70 50 61 72 73 65 2c 20 70 4c  (pWC->pParse, pL
249b2 69 73 74 2c 20 70 44 75 70 2c 20 30 29 3b 0a 20  ist, pDup, 0);. 
249b3 20 20 20 20 20 20 20 70 4c 65 66 74 20 3d 20 70         pLeft = p
249b4 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 70  OrTerm->pExpr->p
249b5 4c 65 66 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Left;.      }.  
249b6 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 65 66      assert( pLef
249b7 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 44  t!=0 );.      pD
249b8 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  up = sqlite3Expr
249b9 44 75 70 28 64 62 2c 20 70 4c 65 66 74 29 3b 0a  Dup(db, pLeft);.
249ba 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c        pNew = sql
249bb 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f  ite3Expr(db, TK_
249bc 49 4e 2c 20 70 44 75 70 2c 20 30 2c 20 30 29 3b  IN, pDup, 0, 0);
249bd 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20  .      if( pNew 
249be 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  ){.        int i
249bf 64 78 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 74  dxNew;.        t
249c0 72 61 6e 73 66 65 72 4a 6f 69 6e 4d 61 72 6b 69  ransferJoinMarki
249c1 6e 67 73 28 70 4e 65 77 2c 20 70 45 78 70 72 29  ngs(pNew, pExpr)
249c2 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e  ;.        pNew->
249c3 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a 20  pList = pList;. 
249c4 20 20 20 20 20 20 20 69 64 78 4e 65 77 20 3d 20         idxNew = 
249c5 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72  whereClauseInser
249c6 74 28 70 57 43 2c 20 70 4e 65 77 2c 20 54 45 52  t(pWC, pNew, TER
249c7 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44  M_VIRTUAL|TERM_D
249c8 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 20  YNAMIC);.       
249c9 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 70 53 72   exprAnalyze(pSr
249ca 63 2c 20 70 57 43 2c 20 69 64 78 4e 65 77 29 3b  c, pWC, idxNew);
249cb 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d 20 3d  .        pTerm =
249cc 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d   &pWC->a[idxTerm
249cd 5d 3b 0a 20 20 20 20 20 20 20 20 70 57 43 2d 3e  ];.        pWC->
249ce 61 5b 69 64 78 4e 65 77 5d 2e 69 50 61 72 65 6e  a[idxNew].iParen
249cf 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20  t = idxTerm;.   
249d0 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68 69       pTerm->nChi
249d1 6c 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65  ld = 1;.      }e
249d2 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
249d3 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
249d4 74 65 28 70 4c 69 73 74 29 3b 0a 20 20 20 20 20  te(pList);.     
249d5 20 7d 0a 20 20 20 20 7d 0a 6f 72 5f 6e 6f 74 5f   }.    }.or_not_
249d6 70 6f 73 73 69 62 6c 65 3a 0a 20 20 20 20 77 68  possible:.    wh
249d7 65 72 65 43 6c 61 75 73 65 43 6c 65 61 72 28 26  ereClauseClear(&
249d8 73 4f 72 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  sOr);.  }.#endif
249d9 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
249da 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20  OR_OPTIMIZATION 
249db 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
249dc 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54  TE_OMIT_LIKE_OPT
249dd 49 4d 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 41  IMIZATION.  /* A
249de 64 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 74  dd constraints t
249df 6f 20 72 65 64 75 63 65 20 74 68 65 20 73 65 61  o reduce the sea
249e0 72 63 68 20 73 70 61 63 65 20 6f 6e 20 61 20 4c  rch space on a L
249e1 49 4b 45 20 6f 72 20 47 4c 4f 42 0a 20 20 2a 2a  IKE or GLOB.  **
249e2 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20 2a 2a 0a   operator..  **.
249e3 20 20 2a 2a 20 41 20 6c 69 6b 65 20 70 61 74 74    ** A like patt
249e4 65 72 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ern of the form 
249e5 22 78 20 4c 49 4b 45 20 27 61 62 63 25 27 22 20  "x LIKE 'abc%'" 
249e6 69 73 20 63 68 61 6e 67 65 64 20 69 6e 74 6f 20  is changed into 
249e7 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a 2a  constraints.  **
249e8 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 78  .  **          x
249e9 3e 3d 27 61 62 63 27 20 41 4e 44 20 78 3c 27 61  >='abc' AND x<'a
249ea 62 64 27 20 41 4e 44 20 78 20 4c 49 4b 45 20 27  bd' AND x LIKE '
249eb 61 62 63 25 27 0a 20 20 2a 2a 0a 20 20 2a 2a 20  abc%'.  **.  ** 
249ec 54 68 65 20 6c 61 73 74 20 63 68 61 72 61 63 74  The last charact
249ed 65 72 20 6f 66 20 74 68 65 20 70 72 65 66 69 78  er of the prefix
249ee 20 22 61 62 63 22 20 69 73 20 69 6e 63 72 65 6d   "abc" is increm
249ef 65 6e 74 65 64 20 74 6f 20 66 6f 72 6d 20 74 68  ented to form th
249f0 65 0a 20 20 2a 2a 20 74 65 72 6d 69 6e 61 74 69  e.  ** terminati
249f1 6f 6e 20 63 6f 6e 64 69 64 74 69 6f 6e 20 22 61  on condidtion "a
249f2 62 64 22 2e 20 20 54 68 69 73 20 74 72 69 63 6b  bd".  This trick
249f3 20 6f 66 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67   of incrementing
249f4 20 74 68 65 20 6c 61 73 74 0a 20 20 2a 2a 20 69   the last.  ** i
249f5 73 20 6e 6f 74 20 32 35 35 20 61 6e 64 20 69 66  s not 255 and if
249f6 20 74 68 65 20 63 68 61 72 61 63 74 65 72 20 73   the character s
249f7 65 74 20 69 73 20 6e 6f 74 20 45 42 43 44 49 43  et is not EBCDIC
249f8 2e 20 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69  .  .  */.  if( i
249f9 73 4c 69 6b 65 4f 72 47 6c 6f 62 28 64 62 2c 20  sLikeOrGlob(db, 
249fa 70 45 78 70 72 2c 20 26 6e 50 61 74 74 65 72 6e  pExpr, &nPattern
249fb 2c 20 26 69 73 43 6f 6d 70 6c 65 74 65 2c 20 26  , &isComplete, &
249fc 6e 6f 43 61 73 65 29 20 29 7b 0a 20 20 20 20 45  noCase) ){.    E
249fd 78 70 72 20 2a 70 4c 65 66 74 2c 20 2a 70 52 69  xpr *pLeft, *pRi
249fe 67 68 74 3b 0a 20 20 20 20 45 78 70 72 20 2a 70  ght;.    Expr *p
249ff 53 74 72 31 2c 20 2a 70 53 74 72 32 3b 0a 20 20  Str1, *pStr2;.  
24a00 20 20 45 78 70 72 20 2a 70 4e 65 77 45 78 70 72    Expr *pNewExpr
24a01 31 2c 20 2a 70 4e 65 77 45 78 70 72 32 3b 0a 20  1, *pNewExpr2;. 
24a02 20 20 20 69 6e 74 20 69 64 78 4e 65 77 31 2c 20     int idxNew1, 
24a03 69 64 78 4e 65 77 32 3b 0a 0a 20 20 20 20 70 4c  idxNew2;..    pL
24a04 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69  eft = pExpr->pLi
24a05 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a  st->a[1].pExpr;.
24a06 20 20 20 20 70 52 69 67 68 74 20 3d 20 70 45 78      pRight = pEx
24a07 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e  pr->pList->a[0].
24a08 70 45 78 70 72 3b 0a 20 20 20 20 70 53 74 72 31  pExpr;.    pStr1
24a09 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
24a0a 70 50 61 72 73 65 2c 20 54 4b 5f 53 54 52 49 4e  pParse, TK_STRIN
24a0b 47 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20  G, 0, 0, 0);.   
24a0c 20 69 66 28 20 70 53 74 72 31 20 29 7b 0a 20 20   if( pStr1 ){.  
24a0d 20 20 20 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e      sqlite3Token
24a0e 43 6f 70 79 28 64 62 2c 20 26 70 53 74 72 31 2d  Copy(db, &pStr1-
24a0f 3e 74 6f 6b 65 6e 2c 20 26 70 52 69 67 68 74 2d  >token, &pRight-
24a10 3e 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 70  >token);.      p
24a11 53 74 72 31 2d 3e 74 6f 6b 65 6e 2e 6e 20 3d 20  Str1->token.n = 
24a12 6e 50 61 74 74 65 72 6e 3b 0a 20 20 20 20 20 20  nPattern;.      
24a13 70 53 74 72 31 2d 3e 66 6c 61 67 73 20 3d 20 45  pStr1->flags = E
24a14 50 5f 44 65 71 75 6f 74 65 64 3b 0a 20 20 20 20  P_Dequoted;.    
24a15 7d 0a 20 20 20 20 70 53 74 72 32 20 3d 20 73 71  }.    pStr2 = sq
24a16 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
24a17 20 70 53 74 72 31 29 3b 0a 20 20 20 20 69 66 28   pStr1);.    if(
24a18 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
24a19 65 64 20 29 7b 0a 20 20 20 20 20 20 75 38 20 63  ed ){.      u8 c
24a1a 2c 20 2a 70 43 3b 0a 20 20 20 20 20 20 61 73 73  , *pC;.      ass
24a1b 65 72 74 28 20 70 53 74 72 32 2d 3e 74 6f 6b 65  ert( pStr2->toke
24a1c 6e 2e 64 79 6e 20 29 3b 0a 20 20 20 20 20 20 70  n.dyn );.      p
24a1d 43 20 3d 20 28 75 38 2a 29 26 70 53 74 72 32 2d  C = (u8*)&pStr2-
24a1e 3e 74 6f 6b 65 6e 2e 7a 5b 6e 50 61 74 74 65 72  >token.z[nPatter
24a1f 6e 2d 31 5d 3b 0a 20 20 20 20 20 20 63 20 3d 20  n-1];.      c = 
24a20 2a 70 43 3b 0a 20 20 20 20 20 20 69 66 28 20 6e  *pC;.      if( n
24a21 6f 43 61 73 65 20 29 20 63 20 3d 20 73 71 6c 69  oCase ) c = sqli
24a22 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  te3UpperToLower[
24a23 63 5d 3b 0a 20 20 20 20 20 20 2a 70 43 20 3d 20  c];.      *pC = 
24a24 63 20 2b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  c + 1;.    }.   
24a25 20 70 4e 65 77 45 78 70 72 31 20 3d 20 73 71 6c   pNewExpr1 = sql
24a26 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
24a27 2c 20 54 4b 5f 47 45 2c 20 73 71 6c 69 74 65 33  , TK_GE, sqlite3
24a28 45 78 70 72 44 75 70 28 64 62 2c 70 4c 65 66 74  ExprDup(db,pLeft
24a29 29 2c 20 70 53 74 72 31 2c 20 30 29 3b 0a 20 20  ), pStr1, 0);.  
24a2a 20 20 69 64 78 4e 65 77 31 20 3d 20 77 68 65 72    idxNew1 = wher
24a2b 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57  eClauseInsert(pW
24a2c 43 2c 20 70 4e 65 77 45 78 70 72 31 2c 20 54 45  C, pNewExpr1, TE
24a2d 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f  RM_VIRTUAL|TERM_
24a2e 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 65 78  DYNAMIC);.    ex
24a2f 70 72 41 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20  prAnalyze(pSrc, 
24a30 70 57 43 2c 20 69 64 78 4e 65 77 31 29 3b 0a 20  pWC, idxNew1);. 
24a31 20 20 20 70 4e 65 77 45 78 70 72 32 20 3d 20 73     pNewExpr2 = s
24a32 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
24a33 73 65 2c 20 54 4b 5f 4c 54 2c 20 73 71 6c 69 74  se, TK_LT, sqlit
24a34 65 33 45 78 70 72 44 75 70 28 64 62 2c 70 4c 65  e3ExprDup(db,pLe
24a35 66 74 29 2c 20 70 53 74 72 32 2c 20 30 29 3b 0a  ft), pStr2, 0);.
24a36 20 20 20 20 69 64 78 4e 65 77 32 20 3d 20 77 68      idxNew2 = wh
24a37 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28  ereClauseInsert(
24a38 70 57 43 2c 20 70 4e 65 77 45 78 70 72 32 2c 20  pWC, pNewExpr2, 
24a39 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52  TERM_VIRTUAL|TER
24a3a 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20  M_DYNAMIC);.    
24a3b 65 78 70 72 41 6e 61 6c 79 7a 65 28 70 53 72 63  exprAnalyze(pSrc
24a3c 2c 20 70 57 43 2c 20 69 64 78 4e 65 77 32 29 3b  , pWC, idxNew2);
24a3d 0a 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57  .    pTerm = &pW
24a3e 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20  C->a[idxTerm];. 
24a3f 20 20 20 69 66 28 20 69 73 43 6f 6d 70 6c 65 74     if( isComplet
24a40 65 20 29 7b 0a 20 20 20 20 20 20 70 57 43 2d 3e  e ){.      pWC->
24a41 61 5b 69 64 78 4e 65 77 31 5d 2e 69 50 61 72 65  a[idxNew1].iPare
24a42 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20  nt = idxTerm;.  
24a43 20 20 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65      pWC->a[idxNe
24a44 77 32 5d 2e 69 50 61 72 65 6e 74 20 3d 20 69 64  w2].iParent = id
24a45 78 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 54 65  xTerm;.      pTe
24a46 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 32 3b 0a  rm->nChild = 2;.
24a47 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
24a48 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
24a49 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  LIKE_OPTIMIZATIO
24a4a 4e 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  N */..#ifndef SQ
24a4b 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
24a4c 4c 54 41 42 4c 45 0a 20 20 2f 2a 20 41 64 64 20  LTABLE.  /* Add 
24a4d 61 20 57 4f 5f 4d 41 54 43 48 20 61 75 78 69 6c  a WO_MATCH auxil
24a4e 69 61 72 79 20 74 65 72 6d 20 74 6f 20 74 68 65  iary term to the
24a4f 20 63 6f 6e 73 74 72 61 69 6e 74 20 73 65 74 20   constraint set 
24a50 69 66 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 72  if the.  ** curr
24a51 65 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69  ent expression i
24a52 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 20 20  s of the form:  
24a53 63 6f 6c 75 6d 6e 20 4d 41 54 43 48 20 65 78 70  column MATCH exp
24a54 72 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 6e 66  r..  ** This inf
24a55 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75 73 65 64  ormation is used
24a56 20 62 79 20 74 68 65 20 78 42 65 73 74 49 6e 64   by the xBestInd
24a57 65 78 20 6d 65 74 68 6f 64 73 20 6f 66 0a 20 20  ex methods of.  
24a58 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ** virtual table
24a59 73 2e 20 20 54 68 65 20 6e 61 74 69 76 65 20 71  s.  The native q
24a5a 75 65 72 79 20 6f 70 74 69 6d 69 7a 65 72 20 64  uery optimizer d
24a5b 6f 65 73 20 6e 6f 74 20 61 74 74 65 6d 70 74 0a  oes not attempt.
24a5c 20 20 2a 2a 20 74 6f 20 64 6f 20 61 6e 79 74 68    ** to do anyth
24a5d 69 6e 67 20 77 69 74 68 20 4d 41 54 43 48 20 66  ing with MATCH f
24a5e 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20  unctions..  */. 
24a5f 20 69 66 28 20 69 73 4d 61 74 63 68 4f 66 43 6f   if( isMatchOfCo
24a60 6c 75 6d 6e 28 70 45 78 70 72 29 20 29 7b 0a 20  lumn(pExpr) ){. 
24a61 20 20 20 69 6e 74 20 69 64 78 4e 65 77 3b 0a 20     int idxNew;. 
24a62 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c     Expr *pRight,
24a63 20 2a 70 4c 65 66 74 3b 0a 20 20 20 20 57 68 65   *pLeft;.    Whe
24a64 72 65 54 65 72 6d 20 2a 70 4e 65 77 54 65 72 6d  reTerm *pNewTerm
24a65 3b 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 70 72  ;.    Bitmask pr
24a66 65 72 65 71 43 6f 6c 75 6d 6e 2c 20 70 72 65 72  ereqColumn, prer
24a67 65 71 45 78 70 72 3b 0a 0a 20 20 20 20 70 52 69  eqExpr;..    pRi
24a68 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69  ght = pExpr->pLi
24a69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a  st->a[0].pExpr;.
24a6a 20 20 20 20 70 4c 65 66 74 20 3d 20 70 45 78 70      pLeft = pExp
24a6b 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70  r->pList->a[1].p
24a6c 45 78 70 72 3b 0a 20 20 20 20 70 72 65 72 65 71  Expr;.    prereq
24a6d 45 78 70 72 20 3d 20 65 78 70 72 54 61 62 6c 65  Expr = exprTable
24a6e 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
24a6f 70 52 69 67 68 74 29 3b 0a 20 20 20 20 70 72 65  pRight);.    pre
24a70 72 65 71 43 6f 6c 75 6d 6e 20 3d 20 65 78 70 72  reqColumn = expr
24a71 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b  TableUsage(pMask
24a72 53 65 74 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20  Set, pLeft);.   
24a73 20 69 66 28 20 28 70 72 65 72 65 71 45 78 70 72   if( (prereqExpr
24a74 20 26 20 70 72 65 72 65 71 43 6f 6c 75 6d 6e 29   & prereqColumn)
24a75 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 45 78 70  ==0 ){.      Exp
24a76 72 20 2a 70 4e 65 77 45 78 70 72 3b 0a 20 20 20  r *pNewExpr;.   
24a77 20 20 20 70 4e 65 77 45 78 70 72 20 3d 20 73 71     pNewExpr = sq
24a78 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b  lite3Expr(db, TK
24a79 5f 4d 41 54 43 48 2c 20 30 2c 20 73 71 6c 69 74  _MATCH, 0, sqlit
24a7a 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 52  e3ExprDup(db, pR
24a7b 69 67 68 74 29 2c 20 30 29 3b 0a 20 20 20 20 20  ight), 0);.     
24a7c 20 69 64 78 4e 65 77 20 3d 20 77 68 65 72 65 43   idxNew = whereC
24a7d 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c  lauseInsert(pWC,
24a7e 20 70 4e 65 77 45 78 70 72 2c 20 54 45 52 4d 5f   pNewExpr, TERM_
24a7f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e  VIRTUAL|TERM_DYN
24a80 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 70 4e 65  AMIC);.      pNe
24a81 77 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b  wTerm = &pWC->a[
24a82 69 64 78 4e 65 77 5d 3b 0a 20 20 20 20 20 20 70  idxNew];.      p
24a83 4e 65 77 54 65 72 6d 2d 3e 70 72 65 72 65 71 52  NewTerm->prereqR
24a84 69 67 68 74 20 3d 20 70 72 65 72 65 71 45 78 70  ight = prereqExp
24a85 72 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72  r;.      pNewTer
24a86 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 3d 20  m->leftCursor = 
24a87 70 4c 65 66 74 2d 3e 69 54 61 62 6c 65 3b 0a 20  pLeft->iTable;. 
24a88 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 6c       pNewTerm->l
24a89 65 66 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65 66  eftColumn = pLef
24a8a 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  t->iColumn;.    
24a8b 20 20 70 4e 65 77 54 65 72 6d 2d 3e 65 4f 70 65    pNewTerm->eOpe
24a8c 72 61 74 6f 72 20 3d 20 57 4f 5f 4d 41 54 43 48  rator = WO_MATCH
24a8d 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d  ;.      pNewTerm
24a8e 2d 3e 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54  ->iParent = idxT
24a8f 65 72 6d 3b 0a 20 20 20 20 20 20 70 54 65 72 6d  erm;.      pTerm
24a90 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65   = &pWC->a[idxTe
24a91 72 6d 5d 3b 0a 20 20 20 20 20 20 70 54 65 72 6d  rm];.      pTerm
24a92 2d 3e 6e 43 68 69 6c 64 20 3d 20 31 3b 0a 20 20  ->nChild = 1;.  
24a93 20 20 20 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73      pTerm->flags
24a94 20 7c 3d 20 54 45 52 4d 5f 43 4f 50 49 45 44 3b   |= TERM_COPIED;
24a95 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d  .      pNewTerm-
24a96 3e 70 72 65 72 65 71 41 6c 6c 20 3d 20 70 54 65  >prereqAll = pTe
24a97 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 3b 0a 20  rm->prereqAll;. 
24a98 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20     }.  }.#endif 
24a99 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
24a9a 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a  IRTUALTABLE */..
24a9b 20 20 2f 2a 20 50 72 65 76 65 6e 74 20 4f 4e 20    /* Prevent ON 
24a9c 63 6c 61 75 73 65 20 74 65 72 6d 73 20 6f 66 20  clause terms of 
24a9d 61 20 4c 45 46 54 20 4a 4f 49 4e 20 66 72 6f 6d  a LEFT JOIN from
24a9e 20 62 65 69 6e 67 20 75 73 65 64 20 74 6f 20 64   being used to d
24a9f 72 69 76 65 0a 20 20 2a 2a 20 61 6e 20 69 6e 64  rive.  ** an ind
24aa0 65 78 20 66 6f 72 20 74 61 62 6c 65 73 20 74 6f  ex for tables to
24aa1 20 74 68 65 20 6c 65 66 74 20 6f 66 20 74 68 65   the left of the
24aa2 20 6a 6f 69 6e 2e 0a 20 20 2a 2f 0a 20 20 70 54   join..  */.  pT
24aa3 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74  erm->prereqRight
24aa4 20 7c 3d 20 65 78 74 72 61 52 69 67 68 74 3b 0a   |= extraRight;.
24aa5 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
24aa6 54 52 55 45 20 69 66 20 61 6e 79 20 6f 66 20 74  TRUE if any of t
24aa7 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69  he expressions i
24aa8 6e 20 70 4c 69 73 74 2d 3e 61 5b 69 46 69 72 73  n pList->a[iFirs
24aa9 74 2e 2e 2e 5d 20 63 6f 6e 74 61 69 6e 0a 2a 2a  t...] contain.**
24aaa 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20   a reference to 
24aab 61 6e 79 20 74 61 62 6c 65 20 6f 74 68 65 72 20  any table other 
24aac 74 68 61 6e 20 74 68 65 20 69 42 61 73 65 20 74  than the iBase t
24aad 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  able..*/.static 
24aae 69 6e 74 20 72 65 66 65 72 65 6e 63 65 73 4f 74  int referencesOt
24aaf 68 65 72 54 61 62 6c 65 73 28 0a 20 20 45 78 70  herTables(.  Exp
24ab0 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20  rList *pList,   
24ab1 20 20 20 20 20 20 20 2f 2a 20 53 65 61 72 63 68         /* Search
24ab2 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20   expressions in 
24ab3 74 68 73 20 6c 69 73 74 20 2a 2f 0a 20 20 45 78  ths list */.  Ex
24ab4 70 72 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b  prMaskSet *pMask
24ab5 53 65 74 2c 20 20 20 20 2f 2a 20 4d 61 70 70 69  Set,    /* Mappi
24ab6 6e 67 20 66 72 6f 6d 20 74 61 62 6c 65 73 20 74  ng from tables t
24ab7 6f 20 62 69 74 6d 61 70 73 20 2a 2f 0a 20 20 69  o bitmaps */.  i
24ab8 6e 74 20 69 46 69 72 73 74 2c 20 20 20 20 20 20  nt iFirst,      
24ab9 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65 20 73           /* Be s
24aba 65 61 72 63 68 69 6e 67 20 77 69 74 68 20 74 68  earching with th
24abb 65 20 69 46 69 72 73 74 2d 74 68 20 65 78 70 72  e iFirst-th expr
24abc 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ession */.  int 
24abd 69 42 61 73 65 20 20 20 20 20 20 20 20 20 20 20  iBase           
24abe 20 20 20 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20        /* Ignore 
24abf 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68  references to th
24ac0 69 73 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20  is table */.){. 
24ac1 20 42 69 74 6d 61 73 6b 20 61 6c 6c 6f 77 65 64   Bitmask allowed
24ac2 20 3d 20 7e 67 65 74 4d 61 73 6b 28 70 4d 61 73   = ~getMask(pMas
24ac3 6b 53 65 74 2c 20 69 42 61 73 65 29 3b 0a 20 20  kSet, iBase);.  
24ac4 77 68 69 6c 65 28 20 69 46 69 72 73 74 3c 70 4c  while( iFirst<pL
24ac5 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20  ist->nExpr ){.  
24ac6 20 20 69 66 28 20 28 65 78 70 72 54 61 62 6c 65    if( (exprTable
24ac7 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
24ac8 70 4c 69 73 74 2d 3e 61 5b 69 46 69 72 73 74 2b  pList->a[iFirst+
24ac9 2b 5d 2e 70 45 78 70 72 29 26 61 6c 6c 6f 77 65  +].pExpr)&allowe
24aca 64 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  d)!=0 ){.      r
24acb 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
24acc 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
24acd 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  .../*.** This ro
24ace 75 74 69 6e 65 20 64 65 63 69 64 65 73 20 69 66  utine decides if
24acf 20 70 49 64 78 20 63 61 6e 20 62 65 20 75 73 65   pIdx can be use
24ad0 64 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65  d to satisfy the
24ad1 20 4f 52 44 45 52 20 42 59 0a 2a 2a 20 63 6c 61   ORDER BY.** cla
24ad2 75 73 65 2e 20 20 49 66 20 69 74 20 63 61 6e 2c  use.  If it can,
24ad3 20 69 74 20 72 65 74 75 72 6e 73 20 31 2e 20 20   it returns 1.  
24ad4 49 66 20 70 49 64 78 20 63 61 6e 6e 6f 74 20 73  If pIdx cannot s
24ad5 61 74 69 73 66 79 20 74 68 65 0a 2a 2a 20 4f 52  atisfy the.** OR
24ad6 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20 74  DER BY clause, t
24ad7 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
24ad8 72 6e 73 20 30 2e 0a 2a 2a 0a 2a 2a 20 70 4f 72  rns 0..**.** pOr
24ad9 64 65 72 42 79 20 69 73 20 61 6e 20 4f 52 44 45  derBy is an ORDE
24ada 52 20 42 59 20 63 6c 61 75 73 65 20 66 72 6f 6d  R BY clause from
24adb 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
24adc 65 6e 74 2e 20 20 70 54 61 62 20 69 73 20 74 68  ent.  pTab is th
24add 65 0a 2a 2a 20 6c 65 66 74 2d 6d 6f 73 74 20 74  e.** left-most t
24ade 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d  able in the FROM
24adf 20 63 6c 61 75 73 65 20 6f 66 20 74 68 61 74 20   clause of that 
24ae0 73 61 6d 65 20 53 45 4c 45 43 54 20 73 74 61 74  same SELECT stat
24ae1 65 6d 65 6e 74 20 61 6e 64 0a 2a 2a 20 74 68 65  ement and.** the
24ae2 20 74 61 62 6c 65 20 68 61 73 20 61 20 63 75 72   table has a cur
24ae3 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 22 62  sor number of "b
24ae4 61 73 65 22 2e 20 20 70 49 64 78 20 69 73 20 61  ase".  pIdx is a
24ae5 6e 20 69 6e 64 65 78 20 6f 6e 20 70 54 61 62 2e  n index on pTab.
24ae6 0a 2a 2a 0a 2a 2a 20 6e 45 71 43 6f 6c 20 69 73  .**.** nEqCol is
24ae7 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
24ae8 6f 6c 75 6d 6e 73 20 6f 66 20 70 49 64 78 20 74  olumns of pIdx t
24ae9 68 61 74 20 61 72 65 20 75 73 65 64 20 61 73 20  hat are used as 
24aea 65 71 75 61 6c 69 74 79 0a 2a 2a 20 63 6f 6e 73  equality.** cons
24aeb 74 72 61 69 6e 74 73 2e 20 20 41 6e 79 20 6f 66  traints.  Any of
24aec 20 74 68 65 73 65 20 63 6f 6c 75 6d 6e 73 20 6d   these columns m
24aed 61 79 20 62 65 20 6d 69 73 73 69 6e 67 20 66 72  ay be missing fr
24aee 6f 6d 20 74 68 65 20 4f 52 44 45 52 20 42 59 0a  om the ORDER BY.
24aef 2a 2a 20 63 6c 61 75 73 65 20 61 6e 64 20 74 68  ** clause and th
24af0 65 20 6d 61 74 63 68 20 63 61 6e 20 73 74 69 6c  e match can stil
24af1 6c 20 62 65 20 61 20 73 75 63 63 65 73 73 2e 0a  l be a success..
24af2 2a 2a 0a 2a 2a 20 41 6c 6c 20 74 65 72 6d 73 20  **.** All terms 
24af3 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  of the ORDER BY 
24af4 74 68 61 74 20 6d 61 74 63 68 20 61 67 61 69 6e  that match again
24af5 73 74 20 74 68 65 20 69 6e 64 65 78 20 6d 75 73  st the index mus
24af6 74 20 62 65 20 65 69 74 68 65 72 0a 2a 2a 20 41  t be either.** A
24af7 53 43 20 6f 72 20 44 45 53 43 2e 20 20 28 54 65  SC or DESC.  (Te
24af8 72 6d 73 20 6f 66 20 74 68 65 20 4f 52 44 45 52  rms of the ORDER
24af9 20 42 59 20 63 6c 61 75 73 65 20 70 61 73 74 20   BY clause past 
24afa 74 68 65 20 65 6e 64 20 6f 66 20 61 20 55 4e 49  the end of a UNI
24afb 51 55 45 0a 2a 2a 20 69 6e 64 65 78 20 64 6f 20  QUE.** index do 
24afc 6e 6f 74 20 6e 65 65 64 20 74 6f 20 73 61 74 69  not need to sati
24afd 73 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72 61  sfy this constra
24afe 69 6e 74 2e 29 20 20 54 68 65 20 2a 70 62 52 65  int.)  The *pbRe
24aff 76 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 73 65  v value is.** se
24b00 74 20 74 6f 20 31 20 69 66 20 74 68 65 20 4f 52  t to 1 if the OR
24b01 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 73  DER BY clause is
24b02 20 61 6c 6c 20 44 45 53 43 20 61 6e 64 20 69 74   all DESC and it
24b03 20 69 73 20 73 65 74 20 74 6f 20 30 20 69 66 0a   is set to 0 if.
24b04 2a 2a 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  ** the ORDER BY 
24b05 63 6c 61 75 73 65 20 69 73 20 61 6c 6c 20 41 53  clause is all AS
24b06 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  C..*/.static int
24b07 20 69 73 53 6f 72 74 69 6e 67 49 6e 64 65 78 28   isSortingIndex(
24b08 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
24b09 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61  ,          /* Pa
24b0a 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
24b0b 0a 20 20 45 78 70 72 4d 61 73 6b 53 65 74 20 2a  .  ExprMaskSet *
24b0c 70 4d 61 73 6b 53 65 74 2c 20 20 2f 2a 20 4d 61  pMaskSet,  /* Ma
24b0d 70 70 69 6e 67 20 66 72 6f 6d 20 74 61 62 6c 65  pping from table
24b0e 20 69 6e 64 69 63 65 73 20 74 6f 20 62 69 74 6d   indices to bitm
24b0f 61 70 73 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a  aps */.  Index *
24b10 70 49 64 78 2c 20 20 20 20 20 20 20 20 20 20 20  pIdx,           
24b11 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 77 65   /* The index we
24b12 20 61 72 65 20 74 65 73 74 69 6e 67 20 2a 2f 0a   are testing */.
24b13 20 20 69 6e 74 20 62 61 73 65 2c 20 20 20 20 20    int base,     
24b14 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
24b15 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  sor number for t
24b16 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 73  he table to be s
24b17 6f 72 74 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c  orted */.  ExprL
24b18 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20  ist *pOrderBy,  
24b19 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20     /* The ORDER 
24b1a 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69  BY clause */.  i
24b1b 6e 74 20 6e 45 71 43 6f 6c 2c 20 20 20 20 20 20  nt nEqCol,      
24b1c 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
24b1d 20 6f 66 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e   of index column
24b1e 73 20 77 69 74 68 20 3d 3d 20 63 6f 6e 73 74 72  s with == constr
24b1f 61 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 2a  aints */.  int *
24b20 70 62 52 65 76 20 20 20 20 20 20 20 20 20 20 20  pbRev           
24b21 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 31 20 69     /* Set to 1 i
24b22 66 20 4f 52 44 45 52 20 42 59 20 69 73 20 44 45  f ORDER BY is DE
24b23 53 43 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  SC */.){.  int i
24b24 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20  , j;            
24b25 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
24b26 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20  op counters */. 
24b27 20 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 20 3d   int sortOrder =
24b28 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
24b29 20 2f 2a 20 58 4f 52 20 6f 66 20 69 6e 64 65 78   /* XOR of index
24b2a 20 61 6e 64 20 4f 52 44 45 52 20 42 59 20 73 6f   and ORDER BY so
24b2b 72 74 20 64 69 72 65 63 74 69 6f 6e 20 2a 2f 0a  rt direction */.
24b2c 20 20 69 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20    int nTerm;    
24b2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24b2e 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 4f    /* Number of O
24b2f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20 2a 2f  RDER BY terms */
24b30 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69  .  struct ExprLi
24b31 73 74 5f 69 74 65 6d 20 2a 70 54 65 72 6d 3b 20  st_item *pTerm; 
24b32 20 20 20 2f 2a 20 41 20 74 65 72 6d 20 6f 66 20     /* A term of 
24b33 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
24b34 75 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  use */.  sqlite3
24b35 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
24b36 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f  b;..  assert( pO
24b37 72 64 65 72 42 79 21 3d 30 20 29 3b 0a 20 20 6e  rderBy!=0 );.  n
24b38 54 65 72 6d 20 3d 20 70 4f 72 64 65 72 42 79 2d  Term = pOrderBy-
24b39 3e 6e 45 78 70 72 3b 0a 20 20 61 73 73 65 72 74  >nExpr;.  assert
24b3a 28 20 6e 54 65 72 6d 3e 30 20 29 3b 0a 0a 20 20  ( nTerm>0 );..  
24b3b 2f 2a 20 4d 61 74 63 68 20 74 65 72 6d 73 20 6f  /* Match terms o
24b3c 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  f the ORDER BY c
24b3d 6c 61 75 73 65 20 61 67 61 69 6e 73 74 20 63 6f  lause against co
24b3e 6c 75 6d 6e 73 20 6f 66 0a 20 20 2a 2a 20 74 68  lumns of.  ** th
24b3f 65 20 69 6e 64 65 78 2e 0a 20 20 2a 2a 0a 20 20  e index..  **.  
24b40 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 6e 64  ** Note that ind
24b41 69 63 65 73 20 68 61 76 65 20 70 49 64 78 2d 3e  ices have pIdx->
24b42 6e 43 6f 6c 75 6d 6e 20 72 65 67 75 6c 61 72 20  nColumn regular 
24b43 63 6f 6c 75 6d 6e 73 20 70 6c 75 73 0a 20 20 2a  columns plus.  *
24b44 2a 20 6f 6e 65 20 61 64 64 69 74 69 6f 6e 61 6c  * one additional
24b45 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 69   column containi
24b46 6e 67 20 74 68 65 20 72 6f 77 69 64 2e 20 20 54  ng the rowid.  T
24b47 68 65 20 72 6f 77 69 64 20 63 6f 6c 75 6d 6e 0a  he rowid column.
24b48 20 20 2a 2a 20 6f 66 20 74 68 65 20 69 6e 64 65    ** of the inde
24b49 78 20 69 73 20 61 6c 73 6f 20 61 6c 6c 6f 77 65  x is also allowe
24b4a 64 20 74 6f 20 6d 61 74 63 68 20 61 67 61 69 6e  d to match again
24b4b 73 74 20 74 68 65 20 4f 52 44 45 52 20 42 59 0a  st the ORDER BY.
24b4c 20 20 2a 2a 20 63 6c 61 75 73 65 2e 0a 20 20 2a    ** clause..  *
24b4d 2f 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 2c 20 70  /.  for(i=j=0, p
24b4e 54 65 72 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61  Term=pOrderBy->a
24b4f 3b 20 6a 3c 6e 54 65 72 6d 20 26 26 20 69 3c 3d  ; j<nTerm && i<=
24b50 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69  pIdx->nColumn; i
24b51 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70  ++){.    Expr *p
24b52 45 78 70 72 3b 20 20 20 20 20 20 20 2f 2a 20 54  Expr;       /* T
24b53 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66  he expression of
24b54 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 70 54   the ORDER BY pT
24b55 65 72 6d 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53  erm */.    CollS
24b56 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a  eq *pColl;    /*
24b57 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73   The collating s
24b58 65 71 75 65 6e 63 65 20 6f 66 20 70 45 78 70 72  equence of pExpr
24b59 20 2a 2f 0a 20 20 20 20 69 6e 74 20 74 65 72 6d   */.    int term
24b5a 53 6f 72 74 4f 72 64 65 72 3b 20 2f 2a 20 53 6f  SortOrder; /* So
24b5b 72 74 20 6f 72 64 65 72 20 66 6f 72 20 74 68 69  rt order for thi
24b5c 73 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 69 6e  s term */.    in
24b5d 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20  t iColumn;      
24b5e 20 2f 2a 20 54 68 65 20 69 2d 74 68 20 63 6f 6c   /* The i-th col
24b5f 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 78  umn of the index
24b60 2e 20 20 2d 31 20 66 6f 72 20 72 6f 77 69 64 20  .  -1 for rowid 
24b61 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53 6f 72 74  */.    int iSort
24b62 4f 72 64 65 72 3b 20 20 20 20 2f 2a 20 31 20 66  Order;    /* 1 f
24b63 6f 72 20 44 45 53 43 2c 20 30 20 66 6f 72 20 41  or DESC, 0 for A
24b64 53 43 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 69  SC on the i-th i
24b65 6e 64 65 78 20 74 65 72 6d 20 2a 2f 0a 20 20 20  ndex term */.   
24b66 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f   const char *zCo
24b67 6c 6c 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74  ll; /* Name of t
24b68 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  he collating seq
24b69 75 65 6e 63 65 20 66 6f 72 20 69 2d 74 68 20 69  uence for i-th i
24b6a 6e 64 65 78 20 74 65 72 6d 20 2a 2f 0a 0a 20 20  ndex term */..  
24b6b 20 20 70 45 78 70 72 20 3d 20 70 54 65 72 6d 2d    pExpr = pTerm-
24b6c 3e 70 45 78 70 72 3b 0a 20 20 20 20 69 66 28 20  >pExpr;.    if( 
24b6d 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f  pExpr->op!=TK_CO
24b6e 4c 55 4d 4e 20 7c 7c 20 70 45 78 70 72 2d 3e 69  LUMN || pExpr->i
24b6f 54 61 62 6c 65 21 3d 62 61 73 65 20 29 7b 0a 20  Table!=base ){. 
24b70 20 20 20 20 20 2f 2a 20 43 61 6e 20 6e 6f 74 20       /* Can not 
24b71 75 73 65 20 61 6e 20 69 6e 64 65 78 20 73 6f 72  use an index sor
24b72 74 20 6f 6e 20 61 6e 79 74 68 69 6e 67 20 74 68  t on anything th
24b73 61 74 20 69 73 20 6e 6f 74 20 61 20 63 6f 6c 75  at is not a colu
24b74 6d 6e 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20  mn in the.      
24b75 2a 2a 20 6c 65 66 74 2d 6d 6f 73 74 20 74 61 62  ** left-most tab
24b76 6c 65 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63  le of the FROM c
24b77 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20 62  lause */.      b
24b78 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
24b79 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45  pColl = sqlite3E
24b7a 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  xprCollSeq(pPars
24b7b 65 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 69  e, pExpr);.    i
24b7c 66 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20  f( !pColl ){.   
24b7d 20 20 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70     pColl = db->p
24b7e 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 20 20 7d 0a  DfltColl;.    }.
24b7f 20 20 20 20 69 66 28 20 69 3c 70 49 64 78 2d 3e      if( i<pIdx->
24b80 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20  nColumn ){.     
24b81 20 69 43 6f 6c 75 6d 6e 20 3d 20 70 49 64 78 2d   iColumn = pIdx-
24b82 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20  >aiColumn[i];.  
24b83 20 20 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3d      if( iColumn=
24b84 3d 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 69  =pIdx->pTable->i
24b85 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20  PKey ){.        
24b86 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20  iColumn = -1;.  
24b87 20 20 20 20 7d 0a 20 20 20 20 20 20 69 53 6f 72      }.      iSor
24b88 74 4f 72 64 65 72 20 3d 20 70 49 64 78 2d 3e 61  tOrder = pIdx->a
24b89 53 6f 72 74 4f 72 64 65 72 5b 69 5d 3b 0a 20 20  SortOrder[i];.  
24b8a 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 49 64 78      zColl = pIdx
24b8b 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20  ->azColl[i];.   
24b8c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 43   }else{.      iC
24b8d 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20  olumn = -1;.    
24b8e 20 20 69 53 6f 72 74 4f 72 64 65 72 20 3d 20 30    iSortOrder = 0
24b8f 3b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20  ;.      zColl = 
24b90 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20  pColl->zName;.  
24b91 20 20 7d 0a 20 20 20 20 69 66 28 20 70 45 78 70    }.    if( pExp
24b92 72 2d 3e 69 43 6f 6c 75 6d 6e 21 3d 69 43 6f 6c  r->iColumn!=iCol
24b93 75 6d 6e 20 7c 7c 20 73 71 6c 69 74 65 33 53 74  umn || sqlite3St
24b94 72 49 43 6d 70 28 70 43 6f 6c 6c 2d 3e 7a 4e 61  rICmp(pColl->zNa
24b95 6d 65 2c 20 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20  me, zColl) ){.  
24b96 20 20 20 20 2f 2a 20 54 65 72 6d 20 6a 20 6f 66      /* Term j of
24b97 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
24b98 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61  ause does not ma
24b99 74 63 68 20 63 6f 6c 75 6d 6e 20 69 20 6f 66 20  tch column i of 
24b9a 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20  the index */.   
24b9b 20 20 20 69 66 28 20 69 3c 6e 45 71 43 6f 6c 20     if( i<nEqCol 
24b9c 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66  ){.        /* If
24b9d 20 61 6e 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e   an index column
24b9e 20 74 68 61 74 20 69 73 20 63 6f 6e 73 74 72 61   that is constra
24b9f 69 6e 65 64 20 62 79 20 3d 3d 20 66 61 69 6c 73  ined by == fails
24ba0 20 74 6f 20 6d 61 74 63 68 20 61 6e 0a 20 20 20   to match an.   
24ba1 20 20 20 20 20 2a 2a 20 4f 52 44 45 52 20 42 59       ** ORDER BY
24ba2 20 74 65 72 6d 2c 20 74 68 61 74 20 69 73 20 4f   term, that is O
24ba3 4b 2e 20 20 4a 75 73 74 20 69 67 6e 6f 72 65 20  K.  Just ignore 
24ba4 74 68 61 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  that column of t
24ba5 68 65 20 69 6e 64 65 78 0a 20 20 20 20 20 20 20  he index.       
24ba6 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e 74   */.        cont
24ba7 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73  inue;.      }els
24ba8 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66  e{.        /* If
24ba9 20 61 6e 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e   an index column
24baa 20 66 61 69 6c 73 20 74 6f 20 6d 61 74 63 68 20   fails to match 
24bab 61 6e 64 20 69 73 20 6e 6f 74 20 63 6f 6e 73 74  and is not const
24bac 72 61 69 6e 65 64 20 62 79 20 3d 3d 0a 20 20 20  rained by ==.   
24bad 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65       ** then the
24bae 20 69 6e 64 65 78 20 63 61 6e 6e 6f 74 20 73 61   index cannot sa
24baf 74 69 73 66 79 20 74 68 65 20 4f 52 44 45 52 20  tisfy the ORDER 
24bb0 42 59 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 20  BY constraint.. 
24bb1 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
24bb2 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
24bb3 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73    }.    }.    as
24bb4 73 65 72 74 28 20 70 49 64 78 2d 3e 61 53 6f 72  sert( pIdx->aSor
24bb5 74 4f 72 64 65 72 21 3d 30 20 29 3b 0a 20 20 20  tOrder!=0 );.   
24bb6 20 61 73 73 65 72 74 28 20 70 54 65 72 6d 2d 3e   assert( pTerm->
24bb7 73 6f 72 74 4f 72 64 65 72 3d 3d 30 20 7c 7c 20  sortOrder==0 || 
24bb8 70 54 65 72 6d 2d 3e 73 6f 72 74 4f 72 64 65 72  pTerm->sortOrder
24bb9 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==1 );.    asser
24bba 74 28 20 69 53 6f 72 74 4f 72 64 65 72 3d 3d 30  t( iSortOrder==0
24bbb 20 7c 7c 20 69 53 6f 72 74 4f 72 64 65 72 3d 3d   || iSortOrder==
24bbc 31 20 29 3b 0a 20 20 20 20 74 65 72 6d 53 6f 72  1 );.    termSor
24bbd 74 4f 72 64 65 72 20 3d 20 69 53 6f 72 74 4f 72  tOrder = iSortOr
24bbe 64 65 72 20 5e 20 70 54 65 72 6d 2d 3e 73 6f 72  der ^ pTerm->sor
24bbf 74 4f 72 64 65 72 3b 0a 20 20 20 20 69 66 28 20  tOrder;.    if( 
24bc0 69 3e 6e 45 71 43 6f 6c 20 29 7b 0a 20 20 20 20  i>nEqCol ){.    
24bc1 20 20 69 66 28 20 74 65 72 6d 53 6f 72 74 4f 72    if( termSortOr
24bc2 64 65 72 21 3d 73 6f 72 74 4f 72 64 65 72 20 29  der!=sortOrder )
24bc3 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64  {.        /* Ind
24bc4 69 63 65 73 20 63 61 6e 20 6f 6e 6c 79 20 62 65  ices can only be
24bc5 20 75 73 65 64 20 69 66 20 61 6c 6c 20 4f 52 44   used if all ORD
24bc6 45 52 20 42 59 20 74 65 72 6d 73 20 70 61 73 74  ER BY terms past
24bc7 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20   the.        ** 
24bc8 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
24bc9 69 6e 74 73 20 61 72 65 20 61 6c 6c 20 65 69 74  ints are all eit
24bca 68 65 72 20 44 45 53 43 20 6f 72 20 41 53 43 2e  her DESC or ASC.
24bcb 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75   */.        retu
24bcc 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 0;.      }.  
24bcd 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
24bce 6f 72 74 4f 72 64 65 72 20 3d 20 74 65 72 6d 53  ortOrder = termS
24bcf 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 7d 0a  ortOrder;.    }.
24bd0 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 70 54 65      j++;.    pTe
24bd1 72 6d 2b 2b 3b 0a 20 20 20 20 69 66 28 20 69 43  rm++;.    if( iC
24bd2 6f 6c 75 6d 6e 3c 30 20 26 26 20 21 72 65 66 65  olumn<0 && !refe
24bd3 72 65 6e 63 65 73 4f 74 68 65 72 54 61 62 6c 65  rencesOtherTable
24bd4 73 28 70 4f 72 64 65 72 42 79 2c 20 70 4d 61 73  s(pOrderBy, pMas
24bd5 6b 53 65 74 2c 20 6a 2c 20 62 61 73 65 29 20 29  kSet, j, base) )
24bd6 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  {.      /* If th
24bd7 65 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e  e indexed column
24bd8 20 69 73 20 74 68 65 20 70 72 69 6d 61 72 79 20   is the primary 
24bd9 6b 65 79 20 61 6e 64 20 65 76 65 72 79 74 68 69  key and everythi
24bda 6e 67 20 6d 61 74 63 68 65 73 0a 20 20 20 20 20  ng matches.     
24bdb 20 2a 2a 20 73 6f 20 66 61 72 20 61 6e 64 20 6e   ** so far and n
24bdc 6f 6e 65 20 6f 66 20 74 68 65 20 4f 52 44 45 52  one of the ORDER
24bdd 20 42 59 20 74 65 72 6d 73 20 74 6f 20 74 68 65   BY terms to the
24bde 20 72 69 67 68 74 20 72 65 66 65 72 65 6e 63 65   right reference
24bdf 20 6f 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20   other.      ** 
24be0 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6a 6f  tables in the jo
24be1 69 6e 2c 20 74 68 65 6e 20 77 65 20 61 72 65 20  in, then we are 
24be2 61 73 73 75 72 65 64 20 74 68 61 74 20 74 68 65  assured that the
24be3 20 69 6e 64 65 78 20 63 61 6e 20 62 65 20 75 73   index can be us
24be4 65 64 20 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20  ed .      ** to 
24be5 73 6f 72 74 20 62 65 63 61 75 73 65 20 74 68 65  sort because the
24be6 20 70 72 69 6d 61 72 79 20 6b 65 79 20 69 73 20   primary key is 
24be7 75 6e 69 71 75 65 20 61 6e 64 20 73 6f 20 6e 6f  unique and so no
24be8 6e 65 20 6f 66 20 74 68 65 20 6f 74 68 65 72 0a  ne of the other.
24be9 20 20 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 73        ** columns
24bea 20 77 69 6c 6c 20 6d 61 6b 65 20 61 6e 79 20 64   will make any d
24beb 69 66 66 65 72 65 6e 63 65 0a 20 20 20 20 20 20  ifference.      
24bec 2a 2f 0a 20 20 20 20 20 20 6a 20 3d 20 6e 54 65  */.      j = nTe
24bed 72 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  rm;.    }.  }.. 
24bee 20 2a 70 62 52 65 76 20 3d 20 73 6f 72 74 4f 72   *pbRev = sortOr
24bef 64 65 72 21 3d 30 3b 0a 20 20 69 66 28 20 6a 3e  der!=0;.  if( j>
24bf0 3d 6e 54 65 72 6d 20 29 7b 0a 20 20 20 20 2f 2a  =nTerm ){.    /*
24bf1 20 41 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74 68   All terms of th
24bf2 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
24bf3 65 20 61 72 65 20 63 6f 76 65 72 65 64 20 62 79  e are covered by
24bf4 20 74 68 69 73 20 69 6e 64 65 78 20 73 6f 0a 20   this index so. 
24bf5 20 20 20 2a 2a 20 74 68 69 73 20 69 6e 64 65 78     ** this index
24bf6 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72   can be used for
24bf7 20 73 6f 72 74 69 6e 67 2e 20 2a 2f 0a 20 20 20   sorting. */.   
24bf8 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20   return 1;.  }. 
24bf9 20 69 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72   if( pIdx->onErr
24bfa 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 26 26 20 69  or!=OE_None && i
24bfb 3d 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 0a  ==pIdx->nColumn.
24bfc 20 20 20 20 20 20 26 26 20 21 72 65 66 65 72 65        && !refere
24bfd 6e 63 65 73 4f 74 68 65 72 54 61 62 6c 65 73 28  ncesOtherTables(
24bfe 70 4f 72 64 65 72 42 79 2c 20 70 4d 61 73 6b 53  pOrderBy, pMaskS
24bff 65 74 2c 20 6a 2c 20 62 61 73 65 29 20 29 7b 0a  et, j, base) ){.
24c00 20 20 20 20 2f 2a 20 41 6c 6c 20 74 65 72 6d 73      /* All terms
24c01 20 6f 66 20 74 68 69 73 20 69 6e 64 65 78 20 6d   of this index m
24c02 61 74 63 68 20 73 6f 6d 65 20 70 72 65 66 69 78  atch some prefix
24c03 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59   of the ORDER BY
24c04 20 63 6c 61 75 73 65 0a 20 20 20 20 2a 2a 20 61   clause.    ** a
24c05 6e 64 20 74 68 65 20 69 6e 64 65 78 20 69 73 20  nd the index is 
24c06 55 4e 49 51 55 45 20 61 6e 64 20 6e 6f 20 74 65  UNIQUE and no te
24c07 72 6d 73 20 6f 6e 20 74 68 65 20 74 61 69 6c 20  rms on the tail 
24c08 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 0a  of the ORDER BY.
24c09 20 20 20 20 2a 2a 20 63 6c 61 75 73 65 20 72 65      ** clause re
24c0a 66 65 72 65 6e 63 65 20 6f 74 68 65 72 20 74 61  ference other ta
24c0b 62 6c 65 73 20 69 6e 20 61 20 6a 6f 69 6e 2e 20  bles in a join. 
24c0c 20 49 66 20 74 68 69 73 20 69 73 20 61 6c 6c 20   If this is all 
24c0d 74 72 75 65 20 74 68 65 6e 0a 20 20 20 20 2a 2a  true then.    **
24c0e 20 74 68 65 20 6f 72 64 65 72 20 62 79 20 63 6c   the order by cl
24c0f 61 75 73 65 20 69 73 20 73 75 70 65 72 66 6c 75  ause is superflu
24c10 6f 75 73 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75  ous. */.    retu
24c11 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rn 1;.  }.  retu
24c12 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  rn 0;.}../*.** C
24c13 68 65 63 6b 20 74 61 62 6c 65 20 74 6f 20 73 65  heck table to se
24c14 65 20 69 66 20 74 68 65 20 4f 52 44 45 52 20 42  e if the ORDER B
24c15 59 20 63 6c 61 75 73 65 20 69 6e 20 70 4f 72 64  Y clause in pOrd
24c16 65 72 42 79 20 63 61 6e 20 62 65 20 73 61 74 69  erBy can be sati
24c17 73 66 69 65 64 0a 2a 2a 20 62 79 20 73 6f 72 74  sfied.** by sort
24c18 69 6e 67 20 69 6e 20 6f 72 64 65 72 20 6f 66 20  ing in order of 
24c19 52 4f 57 49 44 2e 20 20 52 65 74 75 72 6e 20 74  ROWID.  Return t
24c1a 72 75 65 20 69 66 20 73 6f 20 61 6e 64 20 73 65  rue if so and se
24c1b 74 20 2a 70 62 52 65 76 20 74 6f 20 62 65 0a 2a  t *pbRev to be.*
24c1c 2a 20 74 72 75 65 20 66 6f 72 20 72 65 76 65 72  * true for rever
24c1d 73 65 20 52 4f 57 49 44 20 61 6e 64 20 66 61 6c  se ROWID and fal
24c1e 73 65 20 66 6f 72 20 66 6f 72 77 61 72 64 20 52  se for forward R
24c1f 4f 57 49 44 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73  OWID order..*/.s
24c20 74 61 74 69 63 20 69 6e 74 20 73 6f 72 74 61 62  tatic int sortab
24c21 6c 65 42 79 52 6f 77 69 64 28 0a 20 20 69 6e 74  leByRowid(.  int
24c22 20 62 61 73 65 2c 20 20 20 20 20 20 20 20 20 20   base,          
24c23 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e       /* Cursor n
24c24 75 6d 62 65 72 20 66 6f 72 20 74 61 62 6c 65 20  umber for table 
24c25 74 6f 20 62 65 20 73 6f 72 74 65 64 20 2a 2f 0a  to be sorted */.
24c26 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64    ExprList *pOrd
24c27 65 72 42 79 2c 20 20 20 20 20 2f 2a 20 54 68 65  erBy,     /* The
24c28 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
24c29 20 2a 2f 0a 20 20 45 78 70 72 4d 61 73 6b 53 65   */.  ExprMaskSe
24c2a 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 20 2f 2a  t *pMaskSet,  /*
24c2b 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 74 61   Mapping from ta
24c2c 62 6c 65 73 20 74 6f 20 62 69 74 6d 61 70 73 20  bles to bitmaps 
24c2d 2a 2f 0a 20 20 69 6e 74 20 2a 70 62 52 65 76 20  */.  int *pbRev 
24c2e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
24c2f 53 65 74 20 74 6f 20 31 20 69 66 20 4f 52 44 45  Set to 1 if ORDE
24c30 52 20 42 59 20 69 73 20 44 45 53 43 20 2a 2f 0a  R BY is DESC */.
24c31 29 7b 0a 20 20 45 78 70 72 20 2a 70 3b 0a 0a 20  ){.  Expr *p;.. 
24c32 20 61 73 73 65 72 74 28 20 70 4f 72 64 65 72 42   assert( pOrderB
24c33 79 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  y!=0 );.  assert
24c34 28 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  ( pOrderBy->nExp
24c35 72 3e 30 20 29 3b 0a 20 20 70 20 3d 20 70 4f 72  r>0 );.  p = pOr
24c36 64 65 72 42 79 2d 3e 61 5b 30 5d 2e 70 45 78 70  derBy->a[0].pExp
24c37 72 3b 0a 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d  r;.  if( p->op==
24c38 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 2d 3e  TK_COLUMN && p->
24c39 69 54 61 62 6c 65 3d 3d 62 61 73 65 20 26 26 20  iTable==base && 
24c3a 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 2d 31 0a 20  p->iColumn==-1. 
24c3b 20 20 20 26 26 20 21 72 65 66 65 72 65 6e 63 65     && !reference
24c3c 73 4f 74 68 65 72 54 61 62 6c 65 73 28 70 4f 72  sOtherTables(pOr
24c3d 64 65 72 42 79 2c 20 70 4d 61 73 6b 53 65 74 2c  derBy, pMaskSet,
24c3e 20 31 2c 20 62 61 73 65 29 20 29 7b 0a 20 20 20   1, base) ){.   
24c3f 20 2a 70 62 52 65 76 20 3d 20 70 4f 72 64 65 72   *pbRev = pOrder
24c40 42 79 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 64  By->a[0].sortOrd
24c41 65 72 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  er;.    return 1
24c42 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
24c43 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61  ;.}../*.** Prepa
24c44 72 65 20 61 20 63 72 75 64 65 20 65 73 74 69 6d  re a crude estim
24c45 61 74 65 20 6f 66 20 74 68 65 20 6c 6f 67 61 72  ate of the logar
24c46 69 74 68 6d 20 6f 66 20 74 68 65 20 69 6e 70 75  ithm of the inpu
24c47 74 20 76 61 6c 75 65 2e 0a 2a 2a 20 54 68 65 20  t value..** The 
24c48 72 65 73 75 6c 74 73 20 6e 65 65 64 20 6e 6f 74  results need not
24c49 20 62 65 20 65 78 61 63 74 2e 20 20 54 68 69 73   be exact.  This
24c4a 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f   is only used fo
24c4b 72 20 65 73 74 69 6d 61 74 69 6e 67 0a 2a 2a 20  r estimating.** 
24c4c 74 68 65 20 74 6f 74 61 6c 20 63 6f 73 74 20 6f  the total cost o
24c4d 66 20 70 65 72 66 6f 72 6d 69 6e 67 20 6f 70 65  f performing ope
24c4e 72 61 74 69 6e 67 73 20 77 69 74 68 20 4f 28 6c  ratings with O(l
24c4f 6f 67 4e 29 20 6f 72 20 4f 28 4e 6c 6f 67 4e 29  ogN) or O(NlogN)
24c50 0a 2a 2a 20 63 6f 6d 70 6c 65 78 69 74 79 2e 20  .** complexity. 
24c51 20 42 65 63 61 75 73 65 20 4e 20 69 73 20 6a 75   Because N is ju
24c52 73 74 20 61 20 67 75 65 73 73 2c 20 69 74 20 69  st a guess, it i
24c53 73 20 6e 6f 20 67 72 65 61 74 20 74 72 61 67 65  s no great trage
24c54 64 79 20 69 66 0a 2a 2a 20 6c 6f 67 4e 20 69 73  dy if.** logN is
24c55 20 61 20 6c 69 74 74 6c 65 20 6f 66 66 2e 0a 2a   a little off..*
24c56 2f 0a 73 74 61 74 69 63 20 64 6f 75 62 6c 65 20  /.static double 
24c57 65 73 74 4c 6f 67 28 64 6f 75 62 6c 65 20 4e 29  estLog(double N)
24c58 7b 0a 20 20 64 6f 75 62 6c 65 20 6c 6f 67 4e 20  {.  double logN 
24c59 3d 20 31 3b 0a 20 20 64 6f 75 62 6c 65 20 78 20  = 1;.  double x 
24c5a 3d 20 31 30 3b 0a 20 20 77 68 69 6c 65 28 20 4e  = 10;.  while( N
24c5b 3e 78 20 29 7b 0a 20 20 20 20 6c 6f 67 4e 20 2b  >x ){.    logN +
24c5c 3d 20 31 3b 0a 20 20 20 20 78 20 2a 3d 20 31 30  = 1;.    x *= 10
24c5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6c  ;.  }.  return l
24c5e 6f 67 4e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 77  ogN;.}../*.** Tw
24c5f 6f 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 70  o routines for p
24c60 72 69 6e 74 69 6e 67 20 74 68 65 20 63 6f 6e 74  rinting the cont
24c61 65 6e 74 20 6f 66 20 61 6e 20 73 71 6c 69 74 65  ent of an sqlite
24c62 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a 2a 20  3_index_info.** 
24c63 73 74 72 75 63 74 75 72 65 2e 20 20 55 73 65 64  structure.  Used
24c64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64   for testing and
24c65 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e   debugging only.
24c66 20 20 49 66 20 6e 65 69 74 68 65 72 0a 2a 2a 20    If neither.** 
24c67 53 51 4c 49 54 45 5f 54 45 53 54 20 6f 72 20 53  SQLITE_TEST or S
24c68 51 4c 49 54 45 5f 44 45 42 55 47 20 61 72 65 20  QLITE_DEBUG are 
24c69 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68  defined, then th
24c6a 65 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20  ese routines.** 
24c6b 61 72 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23  are no-ops..*/.#
24c6c 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
24c6d 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
24c6e 41 42 4c 45 29 20 26 26 20 64 65 66 69 6e 65 64  ABLE) && defined
24c6f 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 73  (SQLITE_DEBUG).s
24c70 74 61 74 69 63 20 76 6f 69 64 20 54 52 41 43 45  tatic void TRACE
24c71 5f 49 44 58 5f 49 4e 50 55 54 53 28 73 71 6c 69  _IDX_INPUTS(sqli
24c72 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
24c73 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  p){.  int i;.  i
24c74 66 28 20 21 73 71 6c 69 74 65 33 57 68 65 72 65  f( !sqlite3Where
24c75 54 72 61 63 65 20 29 20 72 65 74 75 72 6e 3b 0a  Trace ) return;.
24c76 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
24c77 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b  nConstraint; i++
24c78 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  ){.    sqlite3De
24c79 62 75 67 50 72 69 6e 74 66 28 22 20 20 63 6f 6e  bugPrintf("  con
24c7a 73 74 72 61 69 6e 74 5b 25 64 5d 3a 20 63 6f 6c  straint[%d]: col
24c7b 3d 25 64 20 74 65 72 6d 69 64 3d 25 64 20 6f 70  =%d termid=%d op
24c7c 3d 25 64 20 75 73 61 62 6c 65 64 3d 25 64 5c 6e  =%d usabled=%d\n
24c7d 22 2c 0a 20 20 20 20 20 20 20 69 2c 0a 20 20 20  ",.       i,.   
24c7e 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69      p->aConstrai
24c7f 6e 74 5b 69 5d 2e 69 43 6f 6c 75 6d 6e 2c 0a 20  nt[i].iColumn,. 
24c80 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72        p->aConstr
24c81 61 69 6e 74 5b 69 5d 2e 69 54 65 72 6d 4f 66 66  aint[i].iTermOff
24c82 73 65 74 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61  set,.       p->a
24c83 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 6f 70  Constraint[i].op
24c84 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e  ,.       p->aCon
24c85 73 74 72 61 69 6e 74 5b 69 5d 2e 75 73 61 62 6c  straint[i].usabl
24c86 65 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  e);.  }.  for(i=
24c87 30 3b 20 69 3c 70 2d 3e 6e 4f 72 64 65 72 42 79  0; i<p->nOrderBy
24c88 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69  ; i++){.    sqli
24c89 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
24c8a 20 20 6f 72 64 65 72 62 79 5b 25 64 5d 3a 20 63    orderby[%d]: c
24c8b 6f 6c 3d 25 64 20 64 65 73 63 3d 25 64 5c 6e 22  ol=%d desc=%d\n"
24c8c 2c 0a 20 20 20 20 20 20 20 69 2c 0a 20 20 20 20  ,.       i,.    
24c8d 20 20 20 70 2d 3e 61 4f 72 64 65 72 42 79 5b 69     p->aOrderBy[i
24c8e 5d 2e 69 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20  ].iColumn,.     
24c8f 20 20 70 2d 3e 61 4f 72 64 65 72 42 79 5b 69 5d    p->aOrderBy[i]
24c90 2e 64 65 73 63 29 3b 0a 20 20 7d 0a 7d 0a 73 74  .desc);.  }.}.st
24c91 61 74 69 63 20 76 6f 69 64 20 54 52 41 43 45 5f  atic void TRACE_
24c92 49 44 58 5f 4f 55 54 50 55 54 53 28 73 71 6c 69  IDX_OUTPUTS(sqli
24c93 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
24c94 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  p){.  int i;.  i
24c95 66 28 20 21 73 71 6c 69 74 65 33 57 68 65 72 65  f( !sqlite3Where
24c96 54 72 61 63 65 20 29 20 72 65 74 75 72 6e 3b 0a  Trace ) return;.
24c97 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
24c98 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b  nConstraint; i++
24c99 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  ){.    sqlite3De
24c9a 62 75 67 50 72 69 6e 74 66 28 22 20 20 75 73 61  bugPrintf("  usa
24c9b 67 65 5b 25 64 5d 3a 20 61 72 67 76 49 64 78 3d  ge[%d]: argvIdx=
24c9c 25 64 20 6f 6d 69 74 3d 25 64 5c 6e 22 2c 0a 20  %d omit=%d\n",. 
24c9d 20 20 20 20 20 20 69 2c 0a 20 20 20 20 20 20 20        i,.       
24c9e 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73  p->aConstraintUs
24c9f 61 67 65 5b 69 5d 2e 61 72 67 76 49 6e 64 65 78  age[i].argvIndex
24ca0 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e  ,.       p->aCon
24ca1 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69 5d 2e  straintUsage[i].
24ca2 6f 6d 69 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  omit);.  }.  sql
24ca3 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
24ca4 22 20 20 69 64 78 4e 75 6d 3d 25 64 5c 6e 22 2c  "  idxNum=%d\n",
24ca5 20 70 2d 3e 69 64 78 4e 75 6d 29 3b 0a 20 20 73   p->idxNum);.  s
24ca6 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
24ca7 66 28 22 20 20 69 64 78 53 74 72 3d 25 73 5c 6e  f("  idxStr=%s\n
24ca8 22 2c 20 70 2d 3e 69 64 78 53 74 72 29 3b 0a 20  ", p->idxStr);. 
24ca9 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
24caa 6e 74 66 28 22 20 20 6f 72 64 65 72 42 79 43 6f  ntf("  orderByCo
24cab 6e 73 75 6d 65 64 3d 25 64 5c 6e 22 2c 20 70 2d  nsumed=%d\n", p-
24cac 3e 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64  >orderByConsumed
24cad 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75  );.  sqlite3Debu
24cae 67 50 72 69 6e 74 66 28 22 20 20 65 73 74 69 6d  gPrintf("  estim
24caf 61 74 65 64 43 6f 73 74 3d 25 67 5c 6e 22 2c 20  atedCost=%g\n", 
24cb0 70 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74  p->estimatedCost
24cb1 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69  );.}.#else.#defi
24cb2 6e 65 20 54 52 41 43 45 5f 49 44 58 5f 49 4e 50  ne TRACE_IDX_INP
24cb3 55 54 53 28 41 29 0a 23 64 65 66 69 6e 65 20 54  UTS(A).#define T
24cb4 52 41 43 45 5f 49 44 58 5f 4f 55 54 50 55 54 53  RACE_IDX_OUTPUTS
24cb5 28 41 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  (A).#endif..#ifn
24cb6 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
24cb7 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 0a  VIRTUALTABLE./*.
24cb8 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 62  ** Compute the b
24cb9 65 73 74 20 69 6e 64 65 78 20 66 6f 72 20 61 20  est index for a 
24cba 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a  virtual table..*
24cbb 2a 0a 2a 2a 20 54 68 65 20 62 65 73 74 20 69 6e  *.** The best in
24cbc 64 65 78 20 69 73 20 63 6f 6d 70 75 74 65 64 20  dex is computed 
24cbd 62 79 20 74 68 65 20 78 42 65 73 74 49 6e 64 65  by the xBestInde
24cbe 78 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20  x method of the 
24cbf 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65  virtual.** table
24cc0 20 6d 6f 64 75 6c 65 2e 20 20 54 68 69 73 20 72   module.  This r
24cc1 6f 75 74 69 6e 65 20 69 73 20 72 65 61 6c 6c 79  outine is really
24cc2 20 6a 75 73 74 20 61 20 77 72 61 70 70 65 72 20   just a wrapper 
24cc3 74 68 61 74 20 73 65 74 73 20 75 70 0a 2a 2a 20  that sets up.** 
24cc4 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  the sqlite3_inde
24cc5 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65  x_info structure
24cc6 20 74 68 61 74 20 69 73 20 75 73 65 64 20 74 6f   that is used to
24cc7 20 63 6f 6d 6d 75 6e 69 63 61 74 65 20 77 69 74   communicate wit
24cc8 68 0a 2a 2a 20 78 42 65 73 74 49 6e 64 65 78 2e  h.** xBestIndex.
24cc9 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 20 6a 6f 69 6e  .**.** In a join
24cca 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6d  , this routine m
24ccb 69 67 68 74 20 62 65 20 63 61 6c 6c 65 64 20 6d  ight be called m
24ccc 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 66 6f  ultiple times fo
24ccd 72 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 76 69  r the.** same vi
24cce 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68  rtual table.  Th
24ccf 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  e sqlite3_index_
24cd0 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 69  info structure i
24cd1 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 61 6e 64  s created.** and
24cd2 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 6f 6e 20   initialized on 
24cd3 74 68 65 20 66 69 72 73 74 20 69 6e 76 6f 63 61  the first invoca
24cd4 74 69 6f 6e 20 61 6e 64 20 72 65 75 73 65 64 20  tion and reused 
24cd5 6f 6e 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e  on all subsequen
24cd6 74 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73  t.** invocations
24cd7 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69  .  The sqlite3_i
24cd8 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74  ndex_info struct
24cd9 75 72 65 20 69 73 20 61 6c 73 6f 20 75 73 65 64  ure is also used
24cda 20 77 68 65 6e 0a 2a 2a 20 63 6f 64 65 20 69 73   when.** code is
24cdb 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 61 63   generated to ac
24cdc 63 65 73 73 20 74 68 65 20 76 69 72 74 75 61 6c  cess the virtual
24cdd 20 74 61 62 6c 65 2e 20 20 54 68 65 20 77 68 65   table.  The whe
24cde 72 65 49 6e 66 6f 44 65 6c 65 74 65 28 29 20 0a  reInfoDelete() .
24cdf 2a 2a 20 72 6f 75 74 69 6e 65 20 74 61 6b 65 73  ** routine takes
24ce0 20 63 61 72 65 20 6f 66 20 66 72 65 65 69 6e 67   care of freeing
24ce1 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64   the sqlite3_ind
24ce2 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72  ex_info structur
24ce3 65 20 61 66 74 65 72 0a 2a 2a 20 65 76 65 72 79  e after.** every
24ce4 62 6f 64 79 20 68 61 73 20 66 69 6e 69 73 68 65  body has finishe
24ce5 64 20 77 69 74 68 20 69 74 2e 0a 2a 2f 0a 73 74  d with it..*/.st
24ce6 61 74 69 63 20 64 6f 75 62 6c 65 20 62 65 73 74  atic double best
24ce7 56 69 72 74 75 61 6c 49 6e 64 65 78 28 0a 20 20  VirtualIndex(.  
24ce8 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
24ce9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
24cea 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f  * The parsing co
24ceb 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65  ntext */.  Where
24cec 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20  Clause *pWC,    
24ced 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
24cee 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
24cef 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
24cf0 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c 20 20 20  t_item *pSrc,   
24cf1 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c    /* The FROM cl
24cf2 61 75 73 65 20 74 65 72 6d 20 74 6f 20 73 65 61  ause term to sea
24cf3 72 63 68 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b  rch */.  Bitmask
24cf4 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20 20 20   notReady,      
24cf5 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20          /* Mask 
24cf6 6f 66 20 63 75 72 73 6f 72 73 20 74 68 61 74 20  of cursors that 
24cf7 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c  are not availabl
24cf8 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  e */.  ExprList 
24cf9 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 20  *pOrderBy,      
24cfa 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 64        /* The ord
24cfb 65 72 20 62 79 20 63 6c 61 75 73 65 20 2a 2f 0a  er by clause */.
24cfc 20 20 69 6e 74 20 6f 72 64 65 72 42 79 55 73 61    int orderByUsa
24cfd 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ble,            
24cfe 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 63   /* True if we c
24cff 61 6e 20 70 6f 74 65 6e 74 69 61 6c 20 73 6f 72  an potential sor
24d00 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69  t */.  sqlite3_i
24d01 6e 64 65 78 5f 69 6e 66 6f 20 2a 2a 70 70 49 64  ndex_info **ppId
24d02 78 49 6e 66 6f 20 2f 2a 20 49 6e 64 65 78 20 69  xInfo /* Index i
24d03 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73 73 65  nformation passe
24d04 64 20 74 6f 20 78 42 65 73 74 49 6e 64 65 78 20  d to xBestIndex 
24d05 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  */.){.  Table *p
24d06 54 61 62 20 3d 20 70 53 72 63 2d 3e 70 54 61 62  Tab = pSrc->pTab
24d07 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  ;.  sqlite3_inde
24d08 78 5f 69 6e 66 6f 20 2a 70 49 64 78 49 6e 66 6f  x_info *pIdxInfo
24d09 3b 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74  ;.  struct sqlit
24d0a 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61  e3_index_constra
24d0b 69 6e 74 20 2a 70 49 64 78 43 6f 6e 73 3b 0a 20  int *pIdxCons;. 
24d0c 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
24d0d 69 6e 64 65 78 5f 6f 72 64 65 72 62 79 20 2a 70  index_orderby *p
24d0e 49 64 78 4f 72 64 65 72 42 79 3b 0a 20 20 73 74  IdxOrderBy;.  st
24d0f 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
24d10 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73  ex_constraint_us
24d11 61 67 65 20 2a 70 55 73 61 67 65 3b 0a 20 20 57  age *pUsage;.  W
24d12 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b  hereTerm *pTerm;
24d13 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 69  .  int i, j;.  i
24d14 6e 74 20 6e 4f 72 64 65 72 42 79 3b 0a 20 20 69  nt nOrderBy;.  i
24d15 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 49 66 20  nt rc;..  /* If 
24d16 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  the sqlite3_inde
24d17 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65  x_info structure
24d18 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 70 72   has not been pr
24d19 65 76 69 6f 75 73 6c 79 0a 20 20 2a 2a 20 61 6c  eviously.  ** al
24d1a 6c 6f 63 61 74 65 64 20 61 6e 64 20 69 6e 69 74  located and init
24d1b 69 61 6c 69 7a 65 64 20 66 6f 72 20 74 68 69 73  ialized for this
24d1c 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2c 20   virtual table, 
24d1d 74 68 65 6e 20 61 6c 6c 6f 63 61 74 65 0a 20 20  then allocate.  
24d1e 2a 2a 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a  ** and initializ
24d1f 65 20 69 74 20 6e 6f 77 0a 20 20 2a 2f 0a 20 20  e it now.  */.  
24d20 70 49 64 78 49 6e 66 6f 20 3d 20 2a 70 70 49 64  pIdxInfo = *ppId
24d21 78 49 6e 66 6f 3b 0a 20 20 69 66 28 20 70 49 64  xInfo;.  if( pId
24d22 78 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20  xInfo==0 ){.    
24d23 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d  WhereTerm *pTerm
24d24 3b 0a 20 20 20 20 69 6e 74 20 6e 54 65 72 6d 3b  ;.    int nTerm;
24d25 0a 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28  .    WHERETRACE(
24d26 28 22 52 65 63 6f 6d 70 75 74 69 6e 67 20 69 6e  ("Recomputing in
24d27 64 65 78 20 69 6e 66 6f 20 66 6f 72 20 25 73 2e  dex info for %s.
24d28 2e 2e 5c 6e 22 2c 20 70 54 61 62 2d 3e 7a 4e 61  ..\n", pTab->zNa
24d29 6d 65 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f  me));..    /* Co
24d2a 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  unt the number o
24d2b 66 20 70 6f 73 73 69 62 6c 65 20 57 48 45 52 45  f possible WHERE
24d2c 20 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69   clause constrai
24d2d 6e 74 73 20 72 65 66 65 72 72 69 6e 67 0a 20 20  nts referring.  
24d2e 20 20 2a 2a 20 74 6f 20 74 68 69 73 20 76 69 72    ** to this vir
24d2f 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tual table */.  
24d30 20 20 66 6f 72 28 69 3d 6e 54 65 72 6d 3d 30 2c    for(i=nTerm=0,
24d31 20 70 54 65 72 6d 3d 70 57 43 2d 3e 61 3b 20 69   pTerm=pWC->a; i
24d32 3c 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 69 2b 2b  <pWC->nTerm; i++
24d33 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20  , pTerm++){.    
24d34 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66    if( pTerm->lef
24d35 74 43 75 72 73 6f 72 20 21 3d 20 70 53 72 63 2d  tCursor != pSrc-
24d36 3e 69 43 75 72 73 6f 72 20 29 20 63 6f 6e 74 69  >iCursor ) conti
24d37 6e 75 65 3b 0a 20 20 20 20 20 20 69 66 28 20 28  nue;.      if( (
24d38 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
24d39 26 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  &(pTerm->eOperat
24d3a 6f 72 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20 20  or-1))==0 );.   
24d3b 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65     testcase( pTe
24d3c 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57  rm->eOperator==W
24d3d 4f 5f 49 4e 20 29 3b 0a 20 20 20 20 20 20 74 65  O_IN );.      te
24d3e 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65  stcase( pTerm->e
24d3f 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49 53 4e  Operator==WO_ISN
24d40 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 69 66 28  ULL );.      if(
24d41 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
24d42 72 20 26 20 28 57 4f 5f 49 4e 7c 57 4f 5f 49 53  r & (WO_IN|WO_IS
24d43 4e 55 4c 4c 29 20 29 20 63 6f 6e 74 69 6e 75 65  NULL) ) continue
24d44 3b 0a 20 20 20 20 20 20 6e 54 65 72 6d 2b 2b 3b  ;.      nTerm++;
24d45 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49  .    }..    /* I
24d46 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  f the ORDER BY c
24d47 6c 61 75 73 65 20 63 6f 6e 74 61 69 6e 73 20 6f  lause contains o
24d48 6e 6c 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74  nly columns in t
24d49 68 65 20 63 75 72 72 65 6e 74 20 0a 20 20 20 20  he current .    
24d4a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ** virtual table
24d4b 20 74 68 65 6e 20 61 6c 6c 6f 63 61 74 65 20 73   then allocate s
24d4c 70 61 63 65 20 66 6f 72 20 74 68 65 20 61 4f 72  pace for the aOr
24d4d 64 65 72 42 79 20 70 61 72 74 20 6f 66 0a 20 20  derBy part of.  
24d4e 20 20 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33    ** the sqlite3
24d4f 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75  _index_info stru
24d50 63 74 75 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  cture..    */.  
24d51 20 20 6e 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a    nOrderBy = 0;.
24d52 20 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79      if( pOrderBy
24d53 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d   ){.      for(i=
24d54 30 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e  0; i<pOrderBy->n
24d55 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
24d56 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 20      Expr *pExpr 
24d57 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d  = pOrderBy->a[i]
24d58 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20  .pExpr;.        
24d59 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  if( pExpr->op!=T
24d5a 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 45 78 70  K_COLUMN || pExp
24d5b 72 2d 3e 69 54 61 62 6c 65 21 3d 70 53 72 63 2d  r->iTable!=pSrc-
24d5c 3e 69 43 75 72 73 6f 72 20 29 20 62 72 65 61 6b  >iCursor ) break
24d5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
24d5e 69 66 28 20 69 3d 3d 70 4f 72 64 65 72 42 79 2d  if( i==pOrderBy-
24d5f 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20  >nExpr ){.      
24d60 20 20 6e 4f 72 64 65 72 42 79 20 3d 20 70 4f 72    nOrderBy = pOr
24d61 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20  derBy->nExpr;.  
24d62 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
24d63 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65   /* Allocate the
24d64 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
24d65 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a 20 20  nfo structure.  
24d66 20 20 2a 2f 0a 20 20 20 20 70 49 64 78 49 6e 66    */.    pIdxInf
24d67 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  o = sqlite3DbMal
24d68 6c 6f 63 5a 65 72 6f 28 70 50 61 72 73 65 2d 3e  locZero(pParse->
24d69 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 49 64 78  db, sizeof(*pIdx
24d6a 49 6e 66 6f 29 0a 20 20 20 20 20 20 20 20 20 20  Info).          
24d6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24d6c 20 20 20 2b 20 28 73 69 7a 65 6f 66 28 2a 70 49     + (sizeof(*pI
24d6d 64 78 43 6f 6e 73 29 20 2b 20 73 69 7a 65 6f 66  dxCons) + sizeof
24d6e 28 2a 70 55 73 61 67 65 29 29 2a 6e 54 65 72 6d  (*pUsage))*nTerm
24d6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
24d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2b 20                + 
24d71 73 69 7a 65 6f 66 28 2a 70 49 64 78 4f 72 64 65  sizeof(*pIdxOrde
24d72 72 42 79 29 2a 6e 4f 72 64 65 72 42 79 20 29 3b  rBy)*nOrderBy );
24d73 0a 20 20 20 20 69 66 28 20 70 49 64 78 49 6e 66  .    if( pIdxInf
24d74 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  o==0 ){.      sq
24d75 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
24d76 61 72 73 65 2c 20 22 6f 75 74 20 6f 66 20 6d 65  arse, "out of me
24d77 6d 6f 72 79 22 29 3b 0a 20 20 20 20 20 20 72 65  mory");.      re
24d78 74 75 72 6e 20 30 2e 30 3b 0a 20 20 20 20 7d 0a  turn 0.0;.    }.
24d79 20 20 20 20 2a 70 70 49 64 78 49 6e 66 6f 20 3d      *ppIdxInfo =
24d7a 20 70 49 64 78 49 6e 66 6f 3b 0a 0a 20 20 20 20   pIdxInfo;..    
24d7b 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  /* Initialize th
24d7c 65 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68  e structure.  Th
24d7d 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  e sqlite3_index_
24d7e 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 63  info structure c
24d7f 6f 6e 74 61 69 6e 73 0a 20 20 20 20 2a 2a 20 6d  ontains.    ** m
24d80 61 6e 79 20 66 69 65 6c 64 73 20 74 68 61 74 20  any fields that 
24d81 61 72 65 20 64 65 63 6c 61 72 65 64 20 22 63 6f  are declared "co
24d82 6e 73 74 22 20 74 6f 20 70 72 65 76 65 6e 74 20  nst" to prevent 
24d83 78 42 65 73 74 49 6e 64 65 78 20 66 72 6f 6d 0a  xBestIndex from.
24d84 20 20 20 20 2a 2a 20 63 68 61 6e 67 69 6e 67 20      ** changing 
24d85 74 68 65 6d 2e 20 20 57 65 20 68 61 76 65 20 74  them.  We have t
24d86 6f 20 64 6f 20 73 6f 6d 65 20 66 75 6e 6b 79 20  o do some funky 
24d87 63 61 73 74 69 6e 67 20 69 6e 20 6f 72 64 65 72  casting in order
24d88 20 74 6f 0a 20 20 20 20 2a 2a 20 69 6e 69 74 69   to.    ** initi
24d89 61 6c 69 7a 65 20 74 68 6f 73 65 20 66 69 65 6c  alize those fiel
24d8a 64 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  ds..    */.    p
24d8b 49 64 78 43 6f 6e 73 20 3d 20 28 73 74 72 75 63  IdxCons = (struc
24d8c 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
24d8d 63 6f 6e 73 74 72 61 69 6e 74 2a 29 26 70 49 64  constraint*)&pId
24d8e 78 49 6e 66 6f 5b 31 5d 3b 0a 20 20 20 20 70 49  xInfo[1];.    pI
24d8f 64 78 4f 72 64 65 72 42 79 20 3d 20 28 73 74 72  dxOrderBy = (str
24d90 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
24d91 78 5f 6f 72 64 65 72 62 79 2a 29 26 70 49 64 78  x_orderby*)&pIdx
24d92 43 6f 6e 73 5b 6e 54 65 72 6d 5d 3b 0a 20 20 20  Cons[nTerm];.   
24d93 20 70 55 73 61 67 65 20 3d 20 28 73 74 72 75 63   pUsage = (struc
24d94 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
24d95 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65  constraint_usage
24d96 2a 29 26 70 49 64 78 4f 72 64 65 72 42 79 5b 6e  *)&pIdxOrderBy[n
24d97 4f 72 64 65 72 42 79 5d 3b 0a 20 20 20 20 2a 28  OrderBy];.    *(
24d98 69 6e 74 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e  int*)&pIdxInfo->
24d99 6e 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 6e 54  nConstraint = nT
24d9a 65 72 6d 3b 0a 20 20 20 20 2a 28 69 6e 74 2a 29  erm;.    *(int*)
24d9b 26 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65  &pIdxInfo->nOrde
24d9c 72 42 79 20 3d 20 6e 4f 72 64 65 72 42 79 3b 0a  rBy = nOrderBy;.
24d9d 20 20 20 20 2a 28 73 74 72 75 63 74 20 73 71 6c      *(struct sql
24d9e 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74  ite3_index_const
24d9f 72 61 69 6e 74 2a 2a 29 26 70 49 64 78 49 6e 66  raint**)&pIdxInf
24da0 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 20 3d  o->aConstraint =
24da1 20 70 49 64 78 43 6f 6e 73 3b 0a 20 20 20 20 2a   pIdxCons;.    *
24da2 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  (struct sqlite3_
24da3 69 6e 64 65 78 5f 6f 72 64 65 72 62 79 2a 2a 29  index_orderby**)
24da4 26 70 49 64 78 49 6e 66 6f 2d 3e 61 4f 72 64 65  &pIdxInfo->aOrde
24da5 72 42 79 20 3d 20 70 49 64 78 4f 72 64 65 72 42  rBy = pIdxOrderB
24da6 79 3b 0a 20 20 20 20 2a 28 73 74 72 75 63 74 20  y;.    *(struct 
24da7 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f  sqlite3_index_co
24da8 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65 2a 2a  nstraint_usage**
24da9 29 26 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e  )&pIdxInfo->aCon
24daa 73 74 72 61 69 6e 74 55 73 61 67 65 20 3d 0a 20  straintUsage =. 
24dab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24dac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24dad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24dae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24daf 20 20 20 20 70 55 73 61 67 65 3b 0a 0a 20 20 20      pUsage;..   
24db0 20 66 6f 72 28 69 3d 6a 3d 30 2c 20 70 54 65 72   for(i=j=0, pTer
24db1 6d 3d 70 57 43 2d 3e 61 3b 20 69 3c 70 57 43 2d  m=pWC->a; i<pWC-
24db2 3e 6e 54 65 72 6d 3b 20 69 2b 2b 2c 20 70 54 65  >nTerm; i++, pTe
24db3 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28  rm++){.      if(
24db4 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73   pTerm->leftCurs
24db5 6f 72 20 21 3d 20 70 53 72 63 2d 3e 69 43 75 72  or != pSrc->iCur
24db6 73 6f 72 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  sor ) continue;.
24db7 20 20 20 20 20 20 69 66 28 20 28 70 54 65 72 6d        if( (pTerm
24db8 2d 3e 65 4f 70 65 72 61 74 6f 72 26 28 70 54 65  ->eOperator&(pTe
24db9 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 2d 31 29  rm->eOperator-1)
24dba 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65  )==0 );.      te
24dbb 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65  stcase( pTerm->e
24dbc 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49 4e 20  Operator==WO_IN 
24dbd 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
24dbe 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  e( pTerm->eOpera
24dbf 74 6f 72 3d 3d 57 4f 5f 49 53 4e 55 4c 4c 20 29  tor==WO_ISNULL )
24dc0 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 65 72  ;.      if( pTer
24dc1 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28  m->eOperator & (
24dc2 57 4f 5f 49 4e 7c 57 4f 5f 49 53 4e 55 4c 4c 29  WO_IN|WO_ISNULL)
24dc3 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
24dc4 20 20 20 70 49 64 78 43 6f 6e 73 5b 6a 5d 2e 69     pIdxCons[j].i
24dc5 43 6f 6c 75 6d 6e 20 3d 20 70 54 65 72 6d 2d 3e  Column = pTerm->
24dc6 6c 65 66 74 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  leftColumn;.    
24dc7 20 20 70 49 64 78 43 6f 6e 73 5b 6a 5d 2e 69 54    pIdxCons[j].iT
24dc8 65 72 6d 4f 66 66 73 65 74 20 3d 20 69 3b 0a 20  ermOffset = i;. 
24dc9 20 20 20 20 20 70 49 64 78 43 6f 6e 73 5b 6a 5d       pIdxCons[j]
24dca 2e 6f 70 20 3d 20 70 54 65 72 6d 2d 3e 65 4f 70  .op = pTerm->eOp
24dcb 65 72 61 74 6f 72 3b 0a 20 20 20 20 20 20 2f 2a  erator;.      /*
24dcc 20 54 68 65 20 64 69 72 65 63 74 20 61 73 73 69   The direct assi
24dcd 67 6e 6d 65 6e 74 20 69 6e 20 74 68 65 20 70 72  gnment in the pr
24dce 65 76 69 6f 75 73 20 6c 69 6e 65 20 69 73 20 70  evious line is p
24dcf 6f 73 73 69 62 6c 65 20 6f 6e 6c 79 20 62 65 63  ossible only bec
24dd0 61 75 73 65 0a 20 20 20 20 20 20 2a 2a 20 74 68  ause.      ** th
24dd1 65 20 57 4f 5f 20 61 6e 64 20 53 51 4c 49 54 45  e WO_ and SQLITE
24dd2 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e  _INDEX_CONSTRAIN
24dd3 54 5f 20 63 6f 64 65 73 20 61 72 65 20 69 64 65  T_ codes are ide
24dd4 6e 74 69 63 61 6c 2e 20 20 54 68 65 0a 20 20 20  ntical.  The.   
24dd5 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20     ** following 
24dd6 61 73 73 65 72 74 73 20 76 65 72 69 66 79 20 74  asserts verify t
24dd7 68 69 73 20 66 61 63 74 2e 20 2a 2f 0a 20 20 20  his fact. */.   
24dd8 20 20 20 61 73 73 65 72 74 28 20 57 4f 5f 45 51     assert( WO_EQ
24dd9 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  ==SQLITE_INDEX_C
24dda 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20 29 3b 0a  ONSTRAINT_EQ );.
24ddb 20 20 20 20 20 20 61 73 73 65 72 74 28 20 57 4f        assert( WO
24ddc 5f 4c 54 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45  _LT==SQLITE_INDE
24ddd 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54 20  X_CONSTRAINT_LT 
24dde 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
24ddf 20 57 4f 5f 4c 45 3d 3d 53 51 4c 49 54 45 5f 49   WO_LE==SQLITE_I
24de0 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
24de1 4c 45 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  LE );.      asse
24de2 72 74 28 20 57 4f 5f 47 54 3d 3d 53 51 4c 49 54  rt( WO_GT==SQLIT
24de3 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
24de4 4e 54 5f 47 54 20 29 3b 0a 20 20 20 20 20 20 61  NT_GT );.      a
24de5 73 73 65 72 74 28 20 57 4f 5f 47 45 3d 3d 53 51  ssert( WO_GE==SQ
24de6 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
24de7 52 41 49 4e 54 5f 47 45 20 29 3b 0a 20 20 20 20  RAINT_GE );.    
24de8 20 20 61 73 73 65 72 74 28 20 57 4f 5f 4d 41 54    assert( WO_MAT
24de9 43 48 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58  CH==SQLITE_INDEX
24dea 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4d 41 54 43  _CONSTRAINT_MATC
24deb 48 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  H );.      asser
24dec 74 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  t( pTerm->eOpera
24ded 74 6f 72 20 26 20 28 57 4f 5f 45 51 7c 57 4f 5f  tor & (WO_EQ|WO_
24dee 4c 54 7c 57 4f 5f 4c 45 7c 57 4f 5f 47 54 7c 57  LT|WO_LE|WO_GT|W
24def 4f 5f 47 45 7c 57 4f 5f 4d 41 54 43 48 29 20 29  O_GE|WO_MATCH) )
24df0 3b 0a 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20  ;.      j++;.   
24df1 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20   }.    for(i=0; 
24df2 69 3c 6e 4f 72 64 65 72 42 79 3b 20 69 2b 2b 29  i<nOrderBy; i++)
24df3 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 45  {.      Expr *pE
24df4 78 70 72 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e  xpr = pOrderBy->
24df5 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[i].pExpr;.    
24df6 20 20 70 49 64 78 4f 72 64 65 72 42 79 5b 69 5d    pIdxOrderBy[i]
24df7 2e 69 43 6f 6c 75 6d 6e 20 3d 20 70 45 78 70 72  .iColumn = pExpr
24df8 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20  ->iColumn;.     
24df9 20 70 49 64 78 4f 72 64 65 72 42 79 5b 69 5d 2e   pIdxOrderBy[i].
24dfa 64 65 73 63 20 3d 20 70 4f 72 64 65 72 42 79 2d  desc = pOrderBy-
24dfb 3e 61 5b 69 5d 2e 73 6f 72 74 4f 72 64 65 72 3b  >a[i].sortOrder;
24dfc 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
24dfd 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20   At this point, 
24dfe 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  the sqlite3_inde
24dff 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65  x_info structure
24e00 20 74 68 61 74 20 70 49 64 78 49 6e 66 6f 20 70   that pIdxInfo p
24e01 6f 69 6e 74 73 0a 20 20 2a 2a 20 74 6f 20 77 69  oints.  ** to wi
24e02 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 69 6e 69  ll have been ini
24e03 74 69 61 6c 69 7a 65 64 2c 20 65 69 74 68 65 72  tialized, either
24e04 20 64 75 72 69 6e 67 20 74 68 65 20 63 75 72 72   during the curr
24e05 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  ent invocation o
24e06 72 0a 20 20 2a 2a 20 64 75 72 69 6e 67 20 73 6f  r.  ** during so
24e07 6d 65 20 70 72 69 6f 72 20 69 6e 76 6f 63 61 74  me prior invocat
24e08 69 6f 6e 2e 20 20 4e 6f 77 20 77 65 20 6a 75 73  ion.  Now we jus
24e09 74 20 68 61 76 65 20 74 6f 20 63 75 73 74 6f 6d  t have to custom
24e0a 69 7a 65 20 74 68 65 0a 20 20 2a 2a 20 64 65 74  ize the.  ** det
24e0b 61 69 6c 73 20 6f 66 20 70 49 64 78 49 6e 66 6f  ails of pIdxInfo
24e0c 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74   for the current
24e0d 20 69 6e 76 6f 63 61 74 69 6f 6e 20 61 6e 64 20   invocation and 
24e0e 70 61 73 73 20 69 74 20 74 6f 0a 20 20 2a 2a 20  pass it to.  ** 
24e0f 78 42 65 73 74 49 6e 64 65 78 2e 0a 20 20 2a 2f  xBestIndex..  */
24e10 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c  ..  /* The modul
24e11 65 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 64  e name must be d
24e12 65 66 69 6e 65 64 2e 20 41 6c 73 6f 2c 20 62 79  efined. Also, by
24e13 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 72   this point ther
24e14 65 20 6d 75 73 74 0a 20 20 2a 2a 20 62 65 20 61  e must.  ** be a
24e15 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73   pointer to an s
24e16 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75  qlite3_vtab stru
24e17 63 74 75 72 65 2e 20 4f 74 68 65 72 77 69 73 65  cture. Otherwise
24e18 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 69 65  .  ** sqlite3Vie
24e19 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  wGetColumnNames(
24e1a 29 20 77 6f 75 6c 64 20 68 61 76 65 20 70 69 63  ) would have pic
24e1b 6b 65 64 20 75 70 20 74 68 65 20 65 72 72 6f 72  ked up the error
24e1c 2e 20 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  . .  */.  assert
24e1d 28 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65  ( pTab->azModule
24e1e 41 72 67 20 26 26 20 70 54 61 62 2d 3e 61 7a 4d  Arg && pTab->azM
24e1f 6f 64 75 6c 65 41 72 67 5b 30 5d 20 29 3b 0a 20  oduleArg[0] );. 
24e20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e 70   assert( pTab->p
24e21 56 74 61 62 20 29 3b 0a 23 69 66 20 30 0a 20 20  Vtab );.#if 0.  
24e22 69 66 28 20 70 54 61 62 2d 3e 70 56 74 61 62 3d  if( pTab->pVtab=
24e23 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
24e24 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
24e25 2c 20 22 75 6e 64 65 66 69 6e 65 64 20 6d 6f 64  , "undefined mod
24e26 75 6c 65 20 25 73 20 66 6f 72 20 74 61 62 6c 65  ule %s for table
24e27 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 70 54   %s",.        pT
24e28 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b  ab->azModuleArg[
24e29 30 5d 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  0], pTab->zName)
24e2a 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 2e 30  ;.    return 0.0
24e2b 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
24e2c 2f 2a 20 53 65 74 20 74 68 65 20 61 43 6f 6e 73  /* Set the aCons
24e2d 74 72 61 69 6e 74 5b 5d 2e 75 73 61 62 6c 65 20  traint[].usable 
24e2e 66 69 65 6c 64 73 20 61 6e 64 20 69 6e 69 74 69  fields and initi
24e2f 61 6c 69 7a 65 20 61 6c 6c 20 0a 20 20 2a 2a 20  alize all .  ** 
24e30 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 73  output variables
24e31 20 74 6f 20 7a 65 72 6f 2e 0a 20 20 2a 2a 0a 20   to zero..  **. 
24e32 20 2a 2a 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b   ** aConstraint[
24e33 5d 2e 75 73 61 62 6c 65 20 69 73 20 74 72 75 65  ].usable is true
24e34 20 66 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74 73   for constraints
24e35 20 77 68 65 72 65 20 74 68 65 20 72 69 67 68 74   where the right
24e36 2d 68 61 6e 64 0a 20 20 2a 2a 20 73 69 64 65 20  -hand.  ** side 
24e37 63 6f 6e 74 61 69 6e 73 20 6f 6e 6c 79 20 72 65  contains only re
24e38 66 65 72 65 6e 63 65 73 20 74 6f 20 74 61 62 6c  ferences to tabl
24e39 65 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f  es to the left o
24e3a 66 20 74 68 65 20 63 75 72 72 65 6e 74 0a 20 20  f the current.  
24e3b 2a 2a 20 74 61 62 6c 65 2e 20 20 49 6e 20 6f 74  ** table.  In ot
24e3c 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 74 68  her words, if th
24e3d 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20  e constraint is 
24e3e 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 20 20 2a  of the form:.  *
24e3f 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20  *.  **          
24e40 20 63 6f 6c 75 6d 6e 20 3d 20 65 78 70 72 0a 20   column = expr. 
24e41 20 2a 2a 0a 20 20 2a 2a 20 61 6e 64 20 77 65 20   **.  ** and we 
24e42 61 72 65 20 65 76 61 6c 75 61 74 69 6e 67 20 61  are evaluating a
24e43 20 6a 6f 69 6e 2c 20 74 68 65 6e 20 74 68 65 20   join, then the 
24e44 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20 63 6f  constraint on co
24e45 6c 75 6d 6e 20 69 73 20 0a 20 20 2a 2a 20 6f 6e  lumn is .  ** on
24e46 6c 79 20 76 61 6c 69 64 20 69 66 20 61 6c 6c 20  ly valid if all 
24e47 74 61 62 6c 65 73 20 72 65 66 65 72 65 6e 63 65  tables reference
24e48 64 20 69 6e 20 65 78 70 72 20 6f 63 63 75 72 20  d in expr occur 
24e49 74 6f 20 74 68 65 20 6c 65 66 74 0a 20 20 2a 2a  to the left.  **
24e4a 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f   of the table co
24e4b 6e 74 61 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2e  ntaining column.
24e4c 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 61  .  **.  ** The a
24e4d 43 6f 6e 73 74 72 61 69 6e 74 73 5b 5d 20 61 72  Constraints[] ar
24e4e 72 61 79 20 63 6f 6e 74 61 69 6e 73 20 65 6e 74  ray contains ent
24e4f 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 63 6f 6e  ries for all con
24e50 73 74 72 61 69 6e 74 73 0a 20 20 2a 2a 20 6f 6e  straints.  ** on
24e51 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 61 62   the current tab
24e52 6c 65 2e 20 20 54 68 61 74 20 77 61 79 20 77 65  le.  That way we
24e53 20 6f 6e 6c 79 20 68 61 76 65 20 74 6f 20 63 6f   only have to co
24e54 6d 70 75 74 65 20 69 74 20 6f 6e 63 65 0a 20 20  mpute it once.  
24e55 2a 2a 20 65 76 65 6e 20 74 68 6f 75 67 68 20 77  ** even though w
24e56 65 20 6d 69 67 68 74 20 74 72 79 20 74 6f 20 70  e might try to p
24e57 69 63 6b 20 74 68 65 20 62 65 73 74 20 69 6e 64  ick the best ind
24e58 65 78 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65  ex multiple time
24e59 73 2e 0a 20 20 2a 2a 20 46 6f 72 20 65 61 63 68  s..  ** For each
24e5a 20 61 74 74 65 6d 70 74 20 61 74 20 70 69 63 6b   attempt at pick
24e5b 69 6e 67 20 61 6e 20 69 6e 64 65 78 2c 20 74 68  ing an index, th
24e5c 65 20 6f 72 64 65 72 20 6f 66 20 74 61 62 6c 65  e order of table
24e5d 73 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 6a 6f  s in the.  ** jo
24e5e 69 6e 20 6d 69 67 68 74 20 62 65 20 64 69 66 66  in might be diff
24e5f 65 72 65 6e 74 20 73 6f 20 77 65 20 68 61 76 65  erent so we have
24e60 20 74 6f 20 72 65 63 6f 6d 70 75 74 65 20 74 68   to recompute th
24e61 65 20 75 73 61 62 6c 65 20 66 6c 61 67 0a 20 20  e usable flag.  
24e62 2a 2a 20 65 61 63 68 20 74 69 6d 65 2e 0a 20 20  ** each time..  
24e63 2a 2f 0a 20 20 70 49 64 78 43 6f 6e 73 20 3d 20  */.  pIdxCons = 
24e64 2a 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  *(struct sqlite3
24e65 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e  _index_constrain
24e66 74 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 61  t**)&pIdxInfo->a
24e67 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20 70 55  Constraint;.  pU
24e68 73 61 67 65 20 3d 20 70 49 64 78 49 6e 66 6f 2d  sage = pIdxInfo-
24e69 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67  >aConstraintUsag
24e6a 65 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  e;.  for(i=0; i<
24e6b 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74  pIdxInfo->nConst
24e6c 72 61 69 6e 74 3b 20 69 2b 2b 2c 20 70 49 64 78  raint; i++, pIdx
24e6d 43 6f 6e 73 2b 2b 29 7b 0a 20 20 20 20 6a 20 3d  Cons++){.    j =
24e6e 20 70 49 64 78 43 6f 6e 73 2d 3e 69 54 65 72 6d   pIdxCons->iTerm
24e6f 4f 66 66 73 65 74 3b 0a 20 20 20 20 70 54 65 72  Offset;.    pTer
24e70 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 6a 5d 3b 0a  m = &pWC->a[j];.
24e71 20 20 20 20 70 49 64 78 43 6f 6e 73 2d 3e 75 73      pIdxCons->us
24e72 61 62 6c 65 20 3d 20 20 28 70 54 65 72 6d 2d 3e  able =  (pTerm->
24e73 70 72 65 72 65 71 52 69 67 68 74 20 26 20 6e 6f  prereqRight & no
24e74 74 52 65 61 64 79 29 3d 3d 30 3b 0a 20 20 7d 0a  tReady)==0;.  }.
24e75 20 20 6d 65 6d 73 65 74 28 70 55 73 61 67 65 2c    memset(pUsage,
24e76 20 30 2c 20 73 69 7a 65 6f 66 28 70 55 73 61 67   0, sizeof(pUsag
24e77 65 5b 30 5d 29 2a 70 49 64 78 49 6e 66 6f 2d 3e  e[0])*pIdxInfo->
24e78 6e 43 6f 6e 73 74 72 61 69 6e 74 29 3b 0a 20 20  nConstraint);.  
24e79 69 66 28 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 65  if( pIdxInfo->ne
24e7a 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 20 29  edToFreeIdxStr )
24e7b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
24e7c 65 65 28 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78  ee(pIdxInfo->idx
24e7d 53 74 72 29 3b 0a 20 20 7d 0a 20 20 70 49 64 78  Str);.  }.  pIdx
24e7e 49 6e 66 6f 2d 3e 69 64 78 53 74 72 20 3d 20 30  Info->idxStr = 0
24e7f 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64  ;.  pIdxInfo->id
24e80 78 4e 75 6d 20 3d 20 30 3b 0a 20 20 70 49 64 78  xNum = 0;.  pIdx
24e81 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65  Info->needToFree
24e82 49 64 78 53 74 72 20 3d 20 30 3b 0a 20 20 70 49  IdxStr = 0;.  pI
24e83 64 78 49 6e 66 6f 2d 3e 6f 72 64 65 72 42 79 43  dxInfo->orderByC
24e84 6f 6e 73 75 6d 65 64 20 3d 20 30 3b 0a 20 20 70  onsumed = 0;.  p
24e85 49 64 78 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74  IdxInfo->estimat
24e86 65 64 43 6f 73 74 20 3d 20 53 51 4c 49 54 45 5f  edCost = SQLITE_
24e87 42 49 47 5f 44 42 4c 20 2f 20 32 2e 30 3b 0a 20  BIG_DBL / 2.0;. 
24e88 20 6e 4f 72 64 65 72 42 79 20 3d 20 70 49 64 78   nOrderBy = pIdx
24e89 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79 3b 0a  Info->nOrderBy;.
24e8a 20 20 69 66 28 20 70 49 64 78 49 6e 66 6f 2d 3e    if( pIdxInfo->
24e8b 6e 4f 72 64 65 72 42 79 20 26 26 20 21 6f 72 64  nOrderBy && !ord
24e8c 65 72 42 79 55 73 61 62 6c 65 20 29 7b 0a 20 20  erByUsable ){.  
24e8d 20 20 2a 28 69 6e 74 2a 29 26 70 49 64 78 49 6e    *(int*)&pIdxIn
24e8e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79 20 3d 20 30  fo->nOrderBy = 0
24e8f 3b 0a 20 20 7d 0a 0a 20 20 28 76 6f 69 64 29 73  ;.  }..  (void)s
24e90 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
24e91 70 50 61 72 73 65 2d 3e 64 62 29 3b 0a 20 20 57  pParse->db);.  W
24e92 48 45 52 45 54 52 41 43 45 28 28 22 78 42 65 73  HERETRACE(("xBes
24e93 74 49 6e 64 65 78 20 66 6f 72 20 25 73 5c 6e 22  tIndex for %s\n"
24e94 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b  , pTab->zName));
24e95 0a 20 20 54 52 41 43 45 5f 49 44 58 5f 49 4e 50  .  TRACE_IDX_INP
24e96 55 54 53 28 70 49 64 78 49 6e 66 6f 29 3b 0a 20  UTS(pIdxInfo);. 
24e97 20 72 63 20 3d 20 70 54 61 62 2d 3e 70 56 74 61   rc = pTab->pVta
24e98 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 42 65 73  b->pModule->xBes
24e99 74 49 6e 64 65 78 28 70 54 61 62 2d 3e 70 56 74  tIndex(pTab->pVt
24e9a 61 62 2c 20 70 49 64 78 49 6e 66 6f 29 3b 0a 20  ab, pIdxInfo);. 
24e9b 20 54 52 41 43 45 5f 49 44 58 5f 4f 55 54 50 55   TRACE_IDX_OUTPU
24e9c 54 53 28 70 49 64 78 49 6e 66 6f 29 3b 0a 20 20  TS(pIdxInfo);.  
24e9d 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
24e9e 65 74 79 4f 6e 28 70 50 61 72 73 65 2d 3e 64 62  etyOn(pParse->db
24e9f 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  );..  for(i=0; i
24ea0 3c 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73  <pIdxInfo->nCons
24ea1 74 72 61 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20  traint; i++){.  
24ea2 20 20 69 66 28 20 21 70 49 64 78 49 6e 66 6f 2d    if( !pIdxInfo-
24ea3 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e  >aConstraint[i].
24ea4 75 73 61 62 6c 65 20 26 26 20 70 55 73 61 67 65  usable && pUsage
24ea5 5b 69 5d 2e 61 72 67 76 49 6e 64 65 78 3e 30 20  [i].argvIndex>0 
24ea6 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
24ea7 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
24ea8 20 0a 20 20 20 20 20 20 20 20 20 20 22 74 61 62   .          "tab
24ea9 6c 65 20 25 73 3a 20 78 42 65 73 74 49 6e 64 65  le %s: xBestInde
24eaa 78 20 72 65 74 75 72 6e 65 64 20 61 6e 20 69 6e  x returned an in
24eab 76 61 6c 69 64 20 70 6c 61 6e 22 2c 20 70 54 61  valid plan", pTa
24eac 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  b->zName);.     
24ead 20 72 65 74 75 72 6e 20 30 2e 30 3b 0a 20 20 20   return 0.0;.   
24eae 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63   }.  }..  if( rc
24eaf 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
24eb0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
24eb1 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20  E_NOMEM ){.     
24eb2 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c   pParse->db->mal
24eb3 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
24eb4 20 20 20 7d 65 6c 73 65 20 7b 0a 20 20 20 20 20     }else {.     
24eb5 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
24eb6 28 70 50 61 72 73 65 2c 20 22 25 73 22 2c 20 73  (pParse, "%s", s
24eb7 71 6c 69 74 65 33 45 72 72 53 74 72 28 72 63 29  qlite3ErrStr(rc)
24eb8 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a  );.    }.  }.  *
24eb9 28 69 6e 74 2a 29 26 70 49 64 78 49 6e 66 6f 2d  (int*)&pIdxInfo-
24eba 3e 6e 4f 72 64 65 72 42 79 20 3d 20 6e 4f 72 64  >nOrderBy = nOrd
24ebb 65 72 42 79 3b 0a 0a 20 20 72 65 74 75 72 6e 20  erBy;..  return 
24ebc 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74 69 6d 61  pIdxInfo->estima
24ebd 74 65 64 43 6f 73 74 3b 0a 7d 0a 23 65 6e 64 69  tedCost;.}.#endi
24ebe 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
24ebf 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
24ec0 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65  ../*.** Find the
24ec1 20 62 65 73 74 20 69 6e 64 65 78 20 66 6f 72 20   best index for 
24ec2 61 63 63 65 73 73 69 6e 67 20 61 20 70 61 72 74  accessing a part
24ec3 69 63 75 6c 61 72 20 74 61 62 6c 65 2e 20 20 52  icular table.  R
24ec4 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a  eturn a pointer.
24ec5 2a 2a 20 74 6f 20 74 68 65 20 69 6e 64 65 78 2c  ** to the index,
24ec6 20 66 6c 61 67 73 20 74 68 61 74 20 64 65 73 63   flags that desc
24ec7 72 69 62 65 20 68 6f 77 20 74 68 65 20 69 6e 64  ribe how the ind
24ec8 65 78 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65  ex should be use
24ec9 64 2c 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72  d, the.** number
24eca 20 6f 66 20 65 71 75 61 6c 69 74 79 20 63 6f 6e   of equality con
24ecb 73 74 72 61 69 6e 74 73 2c 20 61 6e 64 20 74 68  straints, and th
24ecc 65 20 22 63 6f 73 74 22 20 66 6f 72 20 74 68 69  e "cost" for thi
24ecd 73 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 54  s index..**.** T
24ece 68 65 20 6c 6f 77 65 73 74 20 63 6f 73 74 20 69  he lowest cost i
24ecf 6e 64 65 78 20 77 69 6e 73 2e 20 20 54 68 65 20  ndex wins.  The 
24ed0 63 6f 73 74 20 69 73 20 61 6e 20 65 73 74 69 6d  cost is an estim
24ed1 61 74 65 20 6f 66 20 74 68 65 20 61 6d 6f 75 6e  ate of the amoun
24ed2 74 20 6f 66 0a 2a 2a 20 43 50 55 20 61 6e 64 20  t of.** CPU and 
24ed3 64 69 73 6b 20 49 2f 4f 20 6e 65 65 64 20 74 6f  disk I/O need to
24ed4 20 70 72 6f 63 65 73 73 20 74 68 65 20 72 65 71   process the req
24ed5 75 65 73 74 20 75 73 69 6e 67 20 74 68 65 20 73  uest using the s
24ed6 65 6c 65 63 74 65 64 20 69 6e 64 65 78 2e 0a 2a  elected index..*
24ed7 2a 20 46 61 63 74 6f 72 73 20 74 68 61 74 20 69  * Factors that i
24ed8 6e 66 6c 75 65 6e 63 65 20 63 6f 73 74 20 69 6e  nfluence cost in
24ed9 63 6c 75 64 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  clude:.**.**    
24eda 2a 20 20 54 68 65 20 65 73 74 69 6d 61 74 65 64  *  The estimated
24edb 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   number of rows 
24edc 74 68 61 74 20 77 69 6c 6c 20 62 65 20 72 65 74  that will be ret
24edd 72 69 65 76 65 64 2e 20 20 28 54 68 65 0a 2a 2a  rieved.  (The.**
24ede 20 20 20 20 20 20 20 66 65 77 65 72 20 74 68 65         fewer the
24edf 20 62 65 74 74 65 72 2e 29 0a 2a 2a 0a 2a 2a 20   better.).**.** 
24ee0 20 20 20 2a 20 20 57 68 65 74 68 65 72 20 6f 72     *  Whether or
24ee1 20 6e 6f 74 20 73 6f 72 74 69 6e 67 20 6d 75 73   not sorting mus
24ee2 74 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a 2a 20 20  t occur..**.**  
24ee3 20 20 2a 20 20 57 68 65 74 68 65 72 20 6f 72 20    *  Whether or 
24ee4 6e 6f 74 20 74 68 65 72 65 20 6d 75 73 74 20 62  not there must b
24ee5 65 20 73 65 70 61 72 61 74 65 20 6c 6f 6f 6b 75  e separate looku
24ee6 70 73 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20  ps in the.**    
24ee7 20 20 20 69 6e 64 65 78 20 61 6e 64 20 69 6e 20     index and in 
24ee8 74 68 65 20 6d 61 69 6e 20 74 61 62 6c 65 2e 0a  the main table..
24ee9 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 64 6f 75  **.*/.static dou
24eea 62 6c 65 20 62 65 73 74 49 6e 64 65 78 28 0a 20  ble bestIndex(. 
24eeb 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
24eec 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
24eed 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74  The parsing cont
24eee 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c  ext */.  WhereCl
24eef 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20  ause *pWC,      
24ef0 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52       /* The WHER
24ef1 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74  E clause */.  st
24ef2 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
24ef3 6d 20 2a 70 53 72 63 2c 20 20 2f 2a 20 54 68 65  m *pSrc,  /* The
24ef4 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72   FROM clause ter
24ef5 6d 20 74 6f 20 73 65 61 72 63 68 20 2a 2f 0a 20  m to search */. 
24ef6 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64   Bitmask notRead
24ef7 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  y,           /* 
24ef8 4d 61 73 6b 20 6f 66 20 63 75 72 73 6f 72 73 20  Mask of cursors 
24ef9 74 68 61 74 20 61 72 65 20 6e 6f 74 20 61 76 61  that are not ava
24efa 69 6c 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72  ilable */.  Expr
24efb 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20  List *pOrderBy, 
24efc 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f          /* The o
24efd 72 64 65 72 20 62 79 20 63 6c 61 75 73 65 20 2a  rder by clause *
24efe 2f 0a 20 20 49 6e 64 65 78 20 2a 2a 70 70 49 6e  /.  Index **ppIn
24eff 64 65 78 2c 20 20 20 20 20 20 20 20 20 20 20 20  dex,            
24f00 2f 2a 20 4d 61 6b 65 20 2a 70 70 49 6e 64 65 78  /* Make *ppIndex
24f01 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 62 65   point to the be
24f02 73 74 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e  st index */.  in
24f03 74 20 2a 70 46 6c 61 67 73 2c 20 20 20 20 20 20  t *pFlags,      
24f04 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 75 74            /* Put
24f05 20 66 6c 61 67 73 20 64 65 73 63 72 69 62 69 6e   flags describin
24f06 67 20 74 68 69 73 20 63 68 6f 69 63 65 20 69 6e  g this choice in
24f07 20 2a 70 46 6c 61 67 73 20 2a 2f 0a 20 20 69 6e   *pFlags */.  in
24f08 74 20 2a 70 6e 45 71 20 20 20 20 20 20 20 20 20  t *pnEq         
24f09 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 75 74            /* Put
24f0a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 3d   the number of =
24f0b 3d 20 6f 72 20 49 4e 20 63 6f 6e 73 74 72 61 69  = or IN constrai
24f0c 6e 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  nts here */.){. 
24f0d 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72   WhereTerm *pTer
24f0e 6d 3b 0a 20 20 49 6e 64 65 78 20 2a 62 65 73 74  m;.  Index *best
24f0f 49 64 78 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Idx = 0;        
24f10 20 2f 2a 20 49 6e 64 65 78 20 74 68 61 74 20 67   /* Index that g
24f11 69 76 65 73 20 74 68 65 20 6c 6f 77 65 73 74 20  ives the lowest 
24f12 63 6f 73 74 20 2a 2f 0a 20 20 64 6f 75 62 6c 65  cost */.  double
24f13 20 6c 6f 77 65 73 74 43 6f 73 74 3b 20 20 20 20   lowestCost;    
24f14 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 73        /* The cos
24f15 74 20 6f 66 20 75 73 69 6e 67 20 62 65 73 74 49  t of using bestI
24f16 64 78 20 2a 2f 0a 20 20 69 6e 74 20 62 65 73 74  dx */.  int best
24f17 46 6c 61 67 73 20 3d 20 30 3b 20 20 20 20 20 20  Flags = 0;      
24f18 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 61 73 73      /* Flags ass
24f19 6f 63 69 61 74 65 64 20 77 69 74 68 20 62 65 73  ociated with bes
24f1a 74 49 64 78 20 2a 2f 0a 20 20 69 6e 74 20 62 65  tIdx */.  int be
24f1b 73 74 4e 45 71 20 3d 20 30 3b 20 20 20 20 20 20  stNEq = 0;      
24f1c 20 20 20 20 20 20 2f 2a 20 42 65 73 74 20 76 61        /* Best va
24f1d 6c 75 65 20 66 6f 72 20 6e 45 71 20 2a 2f 0a 20  lue for nEq */. 
24f1e 20 69 6e 74 20 69 43 75 72 20 3d 20 70 53 72 63   int iCur = pSrc
24f1f 2d 3e 69 43 75 72 73 6f 72 3b 20 20 20 2f 2a 20  ->iCursor;   /* 
24f20 54 68 65 20 63 75 72 73 6f 72 20 6f 66 20 74 68  The cursor of th
24f21 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 61 63  e table to be ac
24f22 63 65 73 73 65 64 20 2a 2f 0a 20 20 49 6e 64 65  cessed */.  Inde
24f23 78 20 2a 70 50 72 6f 62 65 3b 20 20 20 20 20 20  x *pProbe;      
24f24 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e          /* An in
24f25 64 65 78 20 77 65 20 61 72 65 20 65 76 61 6c 75  dex we are evalu
24f26 61 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 72  ating */.  int r
24f27 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ev;             
24f28 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
24f29 6f 20 73 63 61 6e 20 69 6e 20 72 65 76 65 72 73  o scan in revers
24f2a 65 20 6f 72 64 65 72 20 2a 2f 0a 20 20 69 6e 74  e order */.  int
24f2b 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
24f2c 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67           /* Flag
24f2d 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
24f2e 68 20 70 50 72 6f 62 65 20 2a 2f 0a 20 20 69 6e  h pProbe */.  in
24f2f 74 20 6e 45 71 3b 20 20 20 20 20 20 20 20 20 20  t nEq;          
24f30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
24f31 62 65 72 20 6f 66 20 3d 3d 20 6f 72 20 49 4e 20  ber of == or IN 
24f32 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20  constraints */. 
24f33 20 69 6e 74 20 65 71 54 65 72 6d 4d 61 73 6b 3b   int eqTermMask;
24f34 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
24f35 4d 61 73 6b 20 6f 66 20 76 61 6c 69 64 20 65 71  Mask of valid eq
24f36 75 61 6c 69 74 79 20 6f 70 65 72 61 74 6f 72 73  uality operators
24f37 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 63 6f 73   */.  double cos
24f38 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
24f39 20 20 2f 2a 20 43 6f 73 74 20 6f 66 20 75 73 69    /* Cost of usi
24f3a 6e 67 20 70 50 72 6f 62 65 20 2a 2f 0a 0a 20 20  ng pProbe */..  
24f3b 57 48 45 52 45 54 52 41 43 45 28 28 22 62 65 73  WHERETRACE(("bes
24f3c 74 49 6e 64 65 78 3a 20 74 62 6c 3d 25 73 20 6e  tIndex: tbl=%s n
24f3d 6f 74 52 65 61 64 79 3d 25 78 5c 6e 22 2c 20 70  otReady=%x\n", p
24f3e 53 72 63 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65  Src->pTab->zName
24f3f 2c 20 6e 6f 74 52 65 61 64 79 29 29 3b 0a 20 20  , notReady));.  
24f40 6c 6f 77 65 73 74 43 6f 73 74 20 3d 20 53 51 4c  lowestCost = SQL
24f41 49 54 45 5f 42 49 47 5f 44 42 4c 3b 0a 20 20 70  ITE_BIG_DBL;.  p
24f42 50 72 6f 62 65 20 3d 20 70 53 72 63 2d 3e 70 54  Probe = pSrc->pT
24f43 61 62 2d 3e 70 49 6e 64 65 78 3b 0a 0a 20 20 2f  ab->pIndex;..  /
24f44 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 68  * If the table h
24f45 61 73 20 6e 6f 20 69 6e 64 69 63 65 73 20 61 6e  as no indices an
24f46 64 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 74  d there are no t
24f47 65 72 6d 73 20 69 6e 20 74 68 65 20 77 68 65 72  erms in the wher
24f48 65 0a 20 20 2a 2a 20 63 6c 61 75 73 65 20 74 68  e.  ** clause th
24f49 61 74 20 72 65 66 65 72 20 74 6f 20 74 68 65 20  at refer to the 
24f4a 52 4f 57 49 44 2c 20 74 68 65 6e 20 77 65 20 77  ROWID, then we w
24f4b 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 61 62 6c  ill never be abl
24f4c 65 20 74 6f 20 64 6f 0a 20 20 2a 2a 20 61 6e 79  e to do.  ** any
24f4d 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e  thing other than
24f4e 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 73 63   a full table sc
24f4f 61 6e 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65  an on this table
24f50 2e 20 20 57 65 20 6d 69 67 68 74 20 61 73 0a 20  .  We might as. 
24f51 20 2a 2a 20 77 65 6c 6c 20 70 75 74 20 69 74 20   ** well put it 
24f52 66 69 72 73 74 20 69 6e 20 74 68 65 20 6a 6f 69  first in the joi
24f53 6e 20 6f 72 64 65 72 2e 20 20 54 68 61 74 20 77  n order.  That w
24f54 61 79 2c 20 70 65 72 68 61 70 73 20 69 74 20 63  ay, perhaps it c
24f55 61 6e 20 62 65 0a 20 20 2a 2a 20 72 65 66 65 72  an be.  ** refer
24f56 65 6e 63 65 64 20 62 79 20 6f 74 68 65 72 20 74  enced by other t
24f57 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6a 6f 69  ables in the joi
24f58 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50  n..  */.  if( pP
24f59 72 6f 62 65 3d 3d 30 20 26 26 0a 20 20 20 20 20  robe==0 &&.     
24f5a 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43  findTerm(pWC, iC
24f5b 75 72 2c 20 2d 31 2c 20 30 2c 20 57 4f 5f 45 51  ur, -1, 0, WO_EQ
24f5c 7c 57 4f 5f 49 4e 7c 57 4f 5f 4c 54 7c 57 4f 5f  |WO_IN|WO_LT|WO_
24f5d 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47 45 2c 30  LE|WO_GT|WO_GE,0
24f5e 29 3d 3d 30 20 26 26 0a 20 20 20 20 20 28 70 4f  )==0 &&.     (pO
24f5f 72 64 65 72 42 79 3d 3d 30 20 7c 7c 20 21 73 6f  rderBy==0 || !so
24f60 72 74 61 62 6c 65 42 79 52 6f 77 69 64 28 69 43  rtableByRowid(iC
24f61 75 72 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 57  ur, pOrderBy, pW
24f62 43 2d 3e 70 4d 61 73 6b 53 65 74 2c 20 26 72 65  C->pMaskSet, &re
24f63 76 29 29 20 29 7b 0a 20 20 20 20 2a 70 46 6c 61  v)) ){.    *pFla
24f64 67 73 20 3d 20 30 3b 0a 20 20 20 20 2a 70 70 49  gs = 0;.    *ppI
24f65 6e 64 65 78 20 3d 20 30 3b 0a 20 20 20 20 2a 70  ndex = 0;.    *p
24f66 6e 45 71 20 3d 20 30 3b 0a 20 20 20 20 72 65 74  nEq = 0;.    ret
24f67 75 72 6e 20 30 2e 30 3b 0a 20 20 7d 0a 0a 20 20  urn 0.0;.  }..  
24f68 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 61 20 72  /* Check for a r
24f69 6f 77 69 64 3d 45 58 50 52 20 6f 72 20 72 6f 77  owid=EXPR or row
24f6a 69 64 20 49 4e 20 28 2e 2e 2e 29 20 63 6f 6e 73  id IN (...) cons
24f6b 74 72 61 69 6e 74 73 0a 20 20 2a 2f 0a 20 20 70  traints.  */.  p
24f6c 54 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28  Term = findTerm(
24f6d 70 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e  pWC, iCur, -1, n
24f6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 45 51 7c 57  otReady, WO_EQ|W
24f6f 4f 5f 49 4e 2c 20 30 29 3b 0a 20 20 69 66 28 20  O_IN, 0);.  if( 
24f70 70 54 65 72 6d 20 29 7b 0a 20 20 20 20 45 78 70  pTerm ){.    Exp
24f71 72 20 2a 70 45 78 70 72 3b 0a 20 20 20 20 2a 70  r *pExpr;.    *p
24f72 70 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 20 20  pIndex = 0;.    
24f73 62 65 73 74 46 6c 61 67 73 20 3d 20 57 48 45 52  bestFlags = WHER
24f74 45 5f 52 4f 57 49 44 5f 45 51 3b 0a 20 20 20 20  E_ROWID_EQ;.    
24f75 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72  if( pTerm->eOper
24f76 61 74 6f 72 20 26 20 57 4f 5f 45 51 20 29 7b 0a  ator & WO_EQ ){.
24f77 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 3d 3d        /* Rowid==
24f78 20 69 73 20 61 6c 77 61 79 73 20 74 68 65 20 62   is always the b
24f79 65 73 74 20 70 69 63 6b 2e 20 20 4c 6f 6f 6b 20  est pick.  Look 
24f7a 6e 6f 20 66 75 72 74 68 65 72 2e 20 20 42 65 63  no further.  Bec
24f7b 61 75 73 65 20 6f 6e 6c 79 0a 20 20 20 20 20 20  ause only.      
24f7c 2a 2a 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20  ** a single row 
24f7d 69 73 20 67 65 6e 65 72 61 74 65 64 2c 20 6f 75  is generated, ou
24f7e 74 70 75 74 20 69 73 20 61 6c 77 61 79 73 20 69  tput is always i
24f7f 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 20 2a  n sorted order *
24f80 2f 0a 20 20 20 20 20 20 2a 70 46 6c 61 67 73 20  /.      *pFlags 
24f81 3d 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51  = WHERE_ROWID_EQ
24f82 20 7c 20 57 48 45 52 45 5f 55 4e 49 51 55 45 3b   | WHERE_UNIQUE;
24f83 0a 20 20 20 20 20 20 2a 70 6e 45 71 20 3d 20 31  .      *pnEq = 1
24f84 3b 0a 20 20 20 20 20 20 57 48 45 52 45 54 52 41  ;.      WHERETRA
24f85 43 45 28 28 22 2e 2e 2e 20 62 65 73 74 20 69 73  CE(("... best is
24f86 20 72 6f 77 69 64 5c 6e 22 29 29 3b 0a 20 20 20   rowid\n"));.   
24f87 20 20 20 72 65 74 75 72 6e 20 30 2e 30 3b 0a 20     return 0.0;. 
24f88 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 45     }else if( (pE
24f89 78 70 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78  xpr = pTerm->pEx
24f8a 70 72 29 2d 3e 70 4c 69 73 74 21 3d 30 20 29 7b  pr)->pList!=0 ){
24f8b 0a 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20  .      /* Rowid 
24f8c 49 4e 20 28 4c 49 53 54 29 3a 20 63 6f 73 74 20  IN (LIST): cost 
24f8d 69 73 20 4e 6c 6f 67 4e 20 77 68 65 72 65 20 4e  is NlogN where N
24f8e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
24f8f 66 20 6c 69 73 74 0a 20 20 20 20 20 20 2a 2a 20  f list.      ** 
24f90 65 6c 65 6d 65 6e 74 73 2e 20 20 2a 2f 0a 20 20  elements.  */.  
24f91 20 20 20 20 6c 6f 77 65 73 74 43 6f 73 74 20 3d      lowestCost =
24f92 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 6e   pExpr->pList->n
24f93 45 78 70 72 3b 0a 20 20 20 20 20 20 6c 6f 77 65  Expr;.      lowe
24f94 73 74 43 6f 73 74 20 2a 3d 20 65 73 74 4c 6f 67  stCost *= estLog
24f95 28 6c 6f 77 65 73 74 43 6f 73 74 29 3b 0a 20 20  (lowestCost);.  
24f96 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f    }else{.      /
24f97 2a 20 52 6f 77 69 64 20 49 4e 20 28 53 45 4c 45  * Rowid IN (SELE
24f98 43 54 29 3a 20 63 6f 73 74 20 69 73 20 4e 6c 6f  CT): cost is Nlo
24f99 67 4e 20 77 68 65 72 65 20 4e 20 69 73 20 74 68  gN where N is th
24f9a 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
24f9b 0a 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 68 65  .      ** in the
24f9c 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 69   result of the i
24f9d 6e 6e 65 72 20 73 65 6c 65 63 74 2e 20 20 57 65  nner select.  We
24f9e 20 68 61 76 65 20 6e 6f 20 77 61 79 20 74 6f 20   have no way to 
24f9f 65 73 74 69 6d 61 74 65 0a 20 20 20 20 20 20 2a  estimate.      *
24fa0 2a 20 74 68 61 74 20 76 61 6c 75 65 20 73 6f 20  * that value so 
24fa1 6d 61 6b 65 20 61 20 77 69 6c 64 20 67 75 65 73  make a wild gues
24fa2 73 2e 20 2a 2f 0a 20 20 20 20 20 20 6c 6f 77 65  s. */.      lowe
24fa3 73 74 43 6f 73 74 20 3d 20 32 30 30 3b 0a 20 20  stCost = 200;.  
24fa4 20 20 7d 0a 20 20 20 20 57 48 45 52 45 54 52 41    }.    WHERETRA
24fa5 43 45 28 28 22 2e 2e 2e 20 72 6f 77 69 64 20 49  CE(("... rowid I
24fa6 4e 20 63 6f 73 74 3a 20 25 2e 39 67 5c 6e 22 2c  N cost: %.9g\n",
24fa7 20 6c 6f 77 65 73 74 43 6f 73 74 29 29 3b 0a 20   lowestCost));. 
24fa8 20 7d 0a 0a 20 20 2f 2a 20 45 73 74 69 6d 61 74   }..  /* Estimat
24fa9 65 20 74 68 65 20 63 6f 73 74 20 6f 66 20 61 20  e the cost of a 
24faa 74 61 62 6c 65 20 73 63 61 6e 2e 20 20 49 66 20  table scan.  If 
24fab 77 65 20 64 6f 20 6e 6f 74 20 6b 6e 6f 77 20 68  we do not know h
24fac 6f 77 20 6d 61 6e 79 0a 20 20 2a 2a 20 65 6e 74  ow many.  ** ent
24fad 72 69 65 73 20 61 72 65 20 69 6e 20 74 68 65 20  ries are in the 
24fae 74 61 62 6c 65 2c 20 75 73 65 20 31 20 6d 69 6c  table, use 1 mil
24faf 6c 69 6f 6e 20 61 73 20 61 20 67 75 65 73 73 2e  lion as a guess.
24fb0 0a 20 20 2a 2f 0a 20 20 63 6f 73 74 20 3d 20 70  .  */.  cost = p
24fb1 50 72 6f 62 65 20 3f 20 70 50 72 6f 62 65 2d 3e  Probe ? pProbe->
24fb2 61 69 52 6f 77 45 73 74 5b 30 5d 20 3a 20 31 30  aiRowEst[0] : 10
24fb3 30 30 30 30 30 3b 0a 20 20 57 48 45 52 45 54 52  00000;.  WHERETR
24fb4 41 43 45 28 28 22 2e 2e 2e 20 74 61 62 6c 65 20  ACE(("... table 
24fb5 73 63 61 6e 20 62 61 73 65 20 63 6f 73 74 3a 20  scan base cost: 
24fb6 25 2e 39 67 5c 6e 22 2c 20 63 6f 73 74 29 29 3b  %.9g\n", cost));
24fb7 0a 20 20 66 6c 61 67 73 20 3d 20 57 48 45 52 45  .  flags = WHERE
24fb8 5f 52 4f 57 49 44 5f 52 41 4e 47 45 3b 0a 0a 20  _ROWID_RANGE;.. 
24fb9 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 63 6f   /* Check for co
24fba 6e 73 74 72 61 69 6e 74 73 20 6f 6e 20 61 20 72  nstraints on a r
24fbb 61 6e 67 65 20 6f 66 20 72 6f 77 69 64 73 20 69  ange of rowids i
24fbc 6e 20 61 20 74 61 62 6c 65 20 73 63 61 6e 2e 0a  n a table scan..
24fbd 20 20 2a 2f 0a 20 20 70 54 65 72 6d 20 3d 20 66    */.  pTerm = f
24fbe 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75  indTerm(pWC, iCu
24fbf 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64 79 2c  r, -1, notReady,
24fc0 20 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 7c 57 4f 5f   WO_LT|WO_LE|WO_
24fc1 47 54 7c 57 4f 5f 47 45 2c 20 30 29 3b 0a 20 20  GT|WO_GE, 0);.  
24fc2 69 66 28 20 70 54 65 72 6d 20 29 7b 0a 20 20 20  if( pTerm ){.   
24fc3 20 69 66 28 20 66 69 6e 64 54 65 72 6d 28 70 57   if( findTerm(pW
24fc4 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74  C, iCur, -1, not
24fc5 52 65 61 64 79 2c 20 57 4f 5f 4c 54 7c 57 4f 5f  Ready, WO_LT|WO_
24fc6 4c 45 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20  LE, 0) ){.      
24fc7 66 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 54  flags |= WHERE_T
24fc8 4f 50 5f 4c 49 4d 49 54 3b 0a 20 20 20 20 20 20  OP_LIMIT;.      
24fc9 63 6f 73 74 20 2f 3d 20 33 3b 20 20 2f 2a 20 47  cost /= 3;  /* G
24fca 75 65 73 73 20 74 68 61 74 20 72 6f 77 69 64 3c  uess that rowid<
24fcb 45 58 50 52 20 65 6c 69 6d 69 6e 61 74 65 73 20  EXPR eliminates 
24fcc 74 77 6f 2d 74 68 69 72 64 73 20 6f 72 20 72 6f  two-thirds or ro
24fcd 77 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20  ws */.    }.    
24fce 69 66 28 20 66 69 6e 64 54 65 72 6d 28 70 57 43  if( findTerm(pWC
24fcf 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52  , iCur, -1, notR
24fd0 65 61 64 79 2c 20 57 4f 5f 47 54 7c 57 4f 5f 47  eady, WO_GT|WO_G
24fd1 45 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20 66  E, 0) ){.      f
24fd2 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 42 54  lags |= WHERE_BT
24fd3 4d 5f 4c 49 4d 49 54 3b 0a 20 20 20 20 20 20 63  M_LIMIT;.      c
24fd4 6f 73 74 20 2f 3d 20 33 3b 20 20 2f 2a 20 47 75  ost /= 3;  /* Gu
24fd5 65 73 73 20 74 68 61 74 20 72 6f 77 69 64 3e 45  ess that rowid>E
24fd6 58 50 52 20 65 6c 69 6d 69 6e 61 74 65 73 20 74  XPR eliminates t
24fd7 77 6f 2d 74 68 69 72 64 73 20 6f 66 20 72 6f 77  wo-thirds of row
24fd8 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 57  s */.    }.    W
24fd9 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 20  HERETRACE(("... 
24fda 72 6f 77 69 64 20 72 61 6e 67 65 20 72 65 64 75  rowid range redu
24fdb 63 65 73 20 63 6f 73 74 20 74 6f 20 25 2e 39 67  ces cost to %.9g
24fdc 5c 6e 22 2c 20 63 6f 73 74 29 29 3b 0a 20 20 7d  \n", cost));.  }
24fdd 65 6c 73 65 7b 0a 20 20 20 20 66 6c 61 67 73 20  else{.    flags 
24fde 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  = 0;.  }..  /* I
24fdf 66 20 74 68 65 20 74 61 62 6c 65 20 73 63 61 6e  f the table scan
24fe0 20 64 6f 65 73 20 6e 6f 74 20 73 61 74 69 73 66   does not satisf
24fe1 79 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  y the ORDER BY c
24fe2 6c 61 75 73 65 2c 20 69 6e 63 72 65 61 73 65 0a  lause, increase.
24fe3 20 20 2a 2a 20 74 68 65 20 63 6f 73 74 20 62 79    ** the cost by
24fe4 20 4e 6c 6f 67 4e 20 74 6f 20 63 6f 76 65 72 20   NlogN to cover 
24fe5 74 68 65 20 65 78 70 65 6e 73 65 20 6f 66 20 73  the expense of s
24fe6 6f 72 74 69 6e 67 2e 20 2a 2f 0a 20 20 69 66 28  orting. */.  if(
24fe7 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20   pOrderBy ){.   
24fe8 20 69 66 28 20 73 6f 72 74 61 62 6c 65 42 79 52   if( sortableByR
24fe9 6f 77 69 64 28 69 43 75 72 2c 20 70 4f 72 64 65  owid(iCur, pOrde
24fea 72 42 79 2c 20 70 57 43 2d 3e 70 4d 61 73 6b 53  rBy, pWC->pMaskS
24feb 65 74 2c 20 26 72 65 76 29 20 29 7b 0a 20 20 20  et, &rev) ){.   
24fec 20 20 20 66 6c 61 67 73 20 7c 3d 20 57 48 45 52     flags |= WHER
24fed 45 5f 4f 52 44 45 52 42 59 7c 57 48 45 52 45 5f  E_ORDERBY|WHERE_
24fee 52 4f 57 49 44 5f 52 41 4e 47 45 3b 0a 20 20 20  ROWID_RANGE;.   
24fef 20 20 20 69 66 28 20 72 65 76 20 29 7b 0a 20 20     if( rev ){.  
24ff0 20 20 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 57        flags |= W
24ff1 48 45 52 45 5f 52 45 56 45 52 53 45 3b 0a 20 20  HERE_REVERSE;.  
24ff2 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
24ff3 0a 20 20 20 20 20 20 63 6f 73 74 20 2b 3d 20 63  .      cost += c
24ff4 6f 73 74 2a 65 73 74 4c 6f 67 28 63 6f 73 74 29  ost*estLog(cost)
24ff5 3b 0a 20 20 20 20 20 20 57 48 45 52 45 54 52 41  ;.      WHERETRA
24ff6 43 45 28 28 22 2e 2e 2e 20 73 6f 72 74 69 6e 67  CE(("... sorting
24ff7 20 69 6e 63 72 65 61 73 65 73 20 63 6f 73 74 20   increases cost 
24ff8 74 6f 20 25 2e 39 67 5c 6e 22 2c 20 63 6f 73 74  to %.9g\n", cost
24ff9 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ));.    }.  }.  
24ffa 69 66 28 20 63 6f 73 74 3c 6c 6f 77 65 73 74 43  if( cost<lowestC
24ffb 6f 73 74 20 29 7b 0a 20 20 20 20 6c 6f 77 65 73  ost ){.    lowes
24ffc 74 43 6f 73 74 20 3d 20 63 6f 73 74 3b 0a 20 20  tCost = cost;.  
24ffd 20 20 62 65 73 74 46 6c 61 67 73 20 3d 20 66 6c    bestFlags = fl
24ffe 61 67 73 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  ags;.  }..  /* I
24fff 66 20 74 68 65 20 70 53 72 63 20 74 61 62 6c 65  f the pSrc table
25000 20 69 73 20 74 68 65 20 72 69 67 68 74 20 74 61   is the right ta
25001 62 6c 65 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f  ble of a LEFT JO
25002 49 4e 20 74 68 65 6e 20 77 65 20 6d 61 79 20 6e  IN then we may n
25003 6f 74 0a 20 20 2a 2a 20 75 73 65 20 61 6e 20 69  ot.  ** use an i
25004 6e 64 65 78 20 74 6f 20 73 61 74 69 73 66 79 20  ndex to satisfy 
25005 49 53 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69  IS NULL constrai
25006 6e 74 73 20 6f 6e 20 74 68 61 74 20 74 61 62 6c  nts on that tabl
25007 65 2e 20 20 54 68 69 73 20 69 73 0a 20 20 2a 2a  e.  This is.  **
25008 20 62 65 63 61 75 73 65 20 63 6f 6c 75 6d 6e 73   because columns
25009 20 6d 69 67 68 74 20 65 6e 64 20 75 70 20 62 65   might end up be
2500a 69 6e 67 20 4e 55 4c 4c 20 69 66 20 74 68 65 20  ing NULL if the 
2500b 74 61 62 6c 65 20 64 6f 65 73 20 6e 6f 74 20 6d  table does not m
2500c 61 74 63 68 20 2d 0a 20 20 2a 2a 20 61 20 63 69  atch -.  ** a ci
2500d 72 63 75 6d 73 74 61 6e 63 65 20 77 68 69 63 68  rcumstance which
2500e 20 74 68 65 20 69 6e 64 65 78 20 63 61 6e 6e 6f   the index canno
2500f 74 20 68 65 6c 70 20 75 73 20 64 69 73 63 6f 76  t help us discov
25010 65 72 2e 20 20 54 69 63 6b 65 74 20 23 32 31 37  er.  Ticket #217
25011 37 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70  7..  */.  if( (p
25012 53 72 63 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20  Src->jointype & 
25013 4a 54 5f 4c 45 46 54 29 21 3d 30 20 29 7b 0a 20  JT_LEFT)!=0 ){. 
25014 20 20 20 65 71 54 65 72 6d 4d 61 73 6b 20 3d 20     eqTermMask = 
25015 57 4f 5f 45 51 7c 57 4f 5f 49 4e 3b 0a 20 20 7d  WO_EQ|WO_IN;.  }
25016 65 6c 73 65 7b 0a 20 20 20 20 65 71 54 65 72 6d  else{.    eqTerm
25017 4d 61 73 6b 20 3d 20 57 4f 5f 45 51 7c 57 4f 5f  Mask = WO_EQ|WO_
25018 49 4e 7c 57 4f 5f 49 53 4e 55 4c 4c 3b 0a 20 20  IN|WO_ISNULL;.  
25019 7d 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b 20 61 74 20  }..  /* Look at 
2501a 65 61 63 68 20 69 6e 64 65 78 2e 0a 20 20 2a 2f  each index..  */
2501b 0a 20 20 66 6f 72 28 3b 20 70 50 72 6f 62 65 3b  .  for(; pProbe;
2501c 20 70 50 72 6f 62 65 3d 70 50 72 6f 62 65 2d 3e   pProbe=pProbe->
2501d 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 74 20  pNext){.    int 
2501e 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
2501f 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
25020 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20   counter */.    
25021 64 6f 75 62 6c 65 20 69 6e 4d 75 6c 74 69 70 6c  double inMultipl
25022 69 65 72 20 3d 20 31 3b 0a 0a 20 20 20 20 57 48  ier = 1;..    WH
25023 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 20 69  ERETRACE(("... i
25024 6e 64 65 78 20 25 73 3a 5c 6e 22 2c 20 70 50 72  ndex %s:\n", pPr
25025 6f 62 65 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 0a 20  obe->zName));.. 
25026 20 20 20 2f 2a 20 43 6f 75 6e 74 20 74 68 65 20     /* Count the 
25027 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
25028 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78 20 74  s in the index t
25029 68 61 74 20 61 72 65 20 73 61 74 69 73 66 69 65  hat are satisfie
2502a 64 0a 20 20 20 20 2a 2a 20 62 79 20 78 3d 45 58  d.    ** by x=EX
2502b 50 52 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f  PR constraints o
2502c 72 20 78 20 49 4e 20 28 2e 2e 2e 29 20 63 6f 6e  r x IN (...) con
2502d 73 74 72 61 69 6e 74 73 2e 0a 20 20 20 20 2a 2f  straints..    */
2502e 0a 20 20 20 20 66 6c 61 67 73 20 3d 20 30 3b 0a  .    flags = 0;.
2502f 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
25030 50 72 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20  Probe->nColumn; 
25031 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  i++){.      int 
25032 6a 20 3d 20 70 50 72 6f 62 65 2d 3e 61 69 43 6f  j = pProbe->aiCo
25033 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 70  lumn[i];.      p
25034 54 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28  Term = findTerm(
25035 70 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20 6e 6f  pWC, iCur, j, no
25036 74 52 65 61 64 79 2c 20 65 71 54 65 72 6d 4d 61  tReady, eqTermMa
25037 73 6b 2c 20 70 50 72 6f 62 65 29 3b 0a 20 20 20  sk, pProbe);.   
25038 20 20 20 69 66 28 20 70 54 65 72 6d 3d 3d 30 20     if( pTerm==0 
25039 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 66  ) break;.      f
2503a 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 43 4f  lags |= WHERE_CO
2503b 4c 55 4d 4e 5f 45 51 3b 0a 20 20 20 20 20 20 69  LUMN_EQ;.      i
2503c 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  f( pTerm->eOpera
2503d 74 6f 72 20 26 20 57 4f 5f 49 4e 20 29 7b 0a 20  tor & WO_IN ){. 
2503e 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 78         Expr *pEx
2503f 70 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70  pr = pTerm->pExp
25040 72 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 73  r;.        flags
25041 20 7c 3d 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e   |= WHERE_COLUMN
25042 5f 49 4e 3b 0a 20 20 20 20 20 20 20 20 69 66 28  _IN;.        if(
25043 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 21   pExpr->pSelect!
25044 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
25045 69 6e 4d 75 6c 74 69 70 6c 69 65 72 20 2a 3d 20  inMultiplier *= 
25046 32 35 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  25;.        }els
25047 65 20 69 66 28 20 70 45 78 70 72 2d 3e 70 4c 69  e if( pExpr->pLi
25048 73 74 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  st!=0 ){.       
25049 20 20 20 69 6e 4d 75 6c 74 69 70 6c 69 65 72 20     inMultiplier 
2504a 2a 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d  *= pExpr->pList-
2504b 3e 6e 45 78 70 72 20 2b 20 31 3b 0a 20 20 20 20  >nExpr + 1;.    
2504c 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
2504d 20 20 7d 0a 20 20 20 20 63 6f 73 74 20 3d 20 70    }.    cost = p
2504e 50 72 6f 62 65 2d 3e 61 69 52 6f 77 45 73 74 5b  Probe->aiRowEst[
2504f 69 5d 20 2a 20 69 6e 4d 75 6c 74 69 70 6c 69 65  i] * inMultiplie
25050 72 20 2a 20 65 73 74 4c 6f 67 28 69 6e 4d 75 6c  r * estLog(inMul
25051 74 69 70 6c 69 65 72 29 3b 0a 20 20 20 20 6e 45  tiplier);.    nE
25052 71 20 3d 20 69 3b 0a 20 20 20 20 69 66 28 20 70  q = i;.    if( p
25053 50 72 6f 62 65 2d 3e 6f 6e 45 72 72 6f 72 21 3d  Probe->onError!=
25054 4f 45 5f 4e 6f 6e 65 20 26 26 20 28 66 6c 61 67  OE_None && (flag
25055 73 20 26 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e  s & WHERE_COLUMN
25056 5f 49 4e 29 3d 3d 30 0a 20 20 20 20 20 20 20 20  _IN)==0.        
25057 20 26 26 20 6e 45 71 3d 3d 70 50 72 6f 62 65 2d   && nEq==pProbe-
25058 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20  >nColumn ){.    
25059 20 20 66 6c 61 67 73 20 7c 3d 20 57 48 45 52 45    flags |= WHERE
2505a 5f 55 4e 49 51 55 45 3b 0a 20 20 20 20 7d 0a 20  _UNIQUE;.    }. 
2505b 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22     WHERETRACE(("
2505c 2e 2e 2e 2e 2e 2e 20 6e 45 71 3d 25 64 20 69 6e  ...... nEq=%d in
2505d 4d 75 6c 74 3d 25 2e 39 67 20 63 6f 73 74 3d 25  Mult=%.9g cost=%
2505e 2e 39 67 5c 6e 22 2c 6e 45 71 2c 69 6e 4d 75 6c  .9g\n",nEq,inMul
2505f 74 69 70 6c 69 65 72 2c 63 6f 73 74 29 29 3b 0a  tiplier,cost));.
25060 0a 20 20 20 20 2f 2a 20 4c 6f 6f 6b 20 66 6f 72  .    /* Look for
25061 20 72 61 6e 67 65 20 63 6f 6e 73 74 72 61 69 6e   range constrain
25062 74 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  ts.    */.    if
25063 28 20 6e 45 71 3c 70 50 72 6f 62 65 2d 3e 6e 43  ( nEq<pProbe->nC
25064 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 69  olumn ){.      i
25065 6e 74 20 6a 20 3d 20 70 50 72 6f 62 65 2d 3e 61  nt j = pProbe->a
25066 69 43 6f 6c 75 6d 6e 5b 6e 45 71 5d 3b 0a 20 20  iColumn[nEq];.  
25067 20 20 20 20 70 54 65 72 6d 20 3d 20 66 69 6e 64      pTerm = find
25068 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20  Term(pWC, iCur, 
25069 6a 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f  j, notReady, WO_
2506a 4c 54 7c 57 4f 5f 4c 45 7c 57 4f 5f 47 54 7c 57  LT|WO_LE|WO_GT|W
2506b 4f 5f 47 45 2c 20 70 50 72 6f 62 65 29 3b 0a 20  O_GE, pProbe);. 
2506c 20 20 20 20 20 69 66 28 20 70 54 65 72 6d 20 29       if( pTerm )
2506d 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 73 20  {.        flags 
2506e 7c 3d 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  |= WHERE_COLUMN_
2506f 52 41 4e 47 45 3b 0a 20 20 20 20 20 20 20 20 69  RANGE;.        i
25070 66 28 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c  f( findTerm(pWC,
25071 20 69 43 75 72 2c 20 6a 2c 20 6e 6f 74 52 65 61   iCur, j, notRea
25072 64 79 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 2c  dy, WO_LT|WO_LE,
25073 20 70 50 72 6f 62 65 29 20 29 7b 0a 20 20 20 20   pProbe) ){.    
25074 20 20 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 57        flags |= W
25075 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 3b 0a  HERE_TOP_LIMIT;.
25076 20 20 20 20 20 20 20 20 20 20 63 6f 73 74 20 2f            cost /
25077 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 3;.        }. 
25078 20 20 20 20 20 20 20 69 66 28 20 66 69 6e 64 54         if( findT
25079 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6a  erm(pWC, iCur, j
2507a 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 47  , notReady, WO_G
2507b 54 7c 57 4f 5f 47 45 2c 20 70 50 72 6f 62 65 29  T|WO_GE, pProbe)
2507c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c   ){.          fl
2507d 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 42 54 4d  ags |= WHERE_BTM
2507e 5f 4c 49 4d 49 54 3b 0a 20 20 20 20 20 20 20 20  _LIMIT;.        
2507f 20 20 63 6f 73 74 20 2f 3d 20 33 3b 0a 20 20 20    cost /= 3;.   
25080 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 57       }.        W
25081 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 2e  HERETRACE(("....
25082 2e 2e 20 72 61 6e 67 65 20 72 65 64 75 63 65 73  .. range reduces
25083 20 63 6f 73 74 20 74 6f 20 25 2e 39 67 5c 6e 22   cost to %.9g\n"
25084 2c 20 63 6f 73 74 29 29 3b 0a 20 20 20 20 20 20  , cost));.      
25085 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
25086 41 64 64 20 74 68 65 20 61 64 64 69 74 69 6f 6e  Add the addition
25087 61 6c 20 63 6f 73 74 20 6f 66 20 73 6f 72 74 69  al cost of sorti
25088 6e 67 20 69 66 20 74 68 61 74 20 69 73 20 61 20  ng if that is a 
25089 66 61 63 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20  factor..    */. 
2508a 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20     if( pOrderBy 
2508b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 66 6c  ){.      if( (fl
2508c 61 67 73 20 26 20 57 48 45 52 45 5f 43 4f 4c 55  ags & WHERE_COLU
2508d 4d 4e 5f 49 4e 29 3d 3d 30 20 26 26 0a 20 20 20  MN_IN)==0 &&.   
2508e 20 20 20 20 20 20 20 20 69 73 53 6f 72 74 69 6e          isSortin
2508f 67 49 6e 64 65 78 28 70 50 61 72 73 65 2c 70 57  gIndex(pParse,pW
25090 43 2d 3e 70 4d 61 73 6b 53 65 74 2c 70 50 72 6f  C->pMaskSet,pPro
25091 62 65 2c 69 43 75 72 2c 70 4f 72 64 65 72 42 79  be,iCur,pOrderBy
25092 2c 6e 45 71 2c 26 72 65 76 29 20 29 7b 0a 20 20  ,nEq,&rev) ){.  
25093 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 73 3d        if( flags=
25094 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
25095 66 6c 61 67 73 20 3d 20 57 48 45 52 45 5f 43 4f  flags = WHERE_CO
25096 4c 55 4d 4e 5f 52 41 4e 47 45 3b 0a 20 20 20 20  LUMN_RANGE;.    
25097 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 66 6c      }.        fl
25098 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 4f 52 44  ags |= WHERE_ORD
25099 45 52 42 59 3b 0a 20 20 20 20 20 20 20 20 69 66  ERBY;.        if
2509a 28 20 72 65 76 20 29 7b 0a 20 20 20 20 20 20 20  ( rev ){.       
2509b 20 20 20 66 6c 61 67 73 20 7c 3d 20 57 48 45 52     flags |= WHER
2509c 45 5f 52 45 56 45 52 53 45 3b 0a 20 20 20 20 20  E_REVERSE;.     
2509d 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
2509e 7b 0a 20 20 20 20 20 20 20 20 63 6f 73 74 20 2b  {.        cost +
2509f 3d 20 63 6f 73 74 2a 65 73 74 4c 6f 67 28 63 6f  = cost*estLog(co
250a0 73 74 29 3b 0a 20 20 20 20 20 20 20 20 57 48 45  st);.        WHE
250a1 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 2e 2e 2e  RETRACE(("......
250a2 20 6f 72 64 65 72 62 79 20 69 6e 63 72 65 61 73   orderby increas
250a3 65 73 20 63 6f 73 74 20 74 6f 20 25 2e 39 67 5c  es cost to %.9g\
250a4 6e 22 2c 20 63 6f 73 74 29 29 3b 0a 20 20 20 20  n", cost));.    
250a5 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
250a6 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
250a7 66 20 77 65 20 63 61 6e 20 67 65 74 20 61 77 61  f we can get awa
250a8 79 20 77 69 74 68 20 75 73 69 6e 67 20 6a 75 73  y with using jus
250a9 74 20 74 68 65 20 69 6e 64 65 78 20 77 69 74 68  t the index with
250aa 6f 75 74 0a 20 20 20 20 2a 2a 20 65 76 65 72 20  out.    ** ever 
250ab 72 65 61 64 69 6e 67 20 74 68 65 20 74 61 62 6c  reading the tabl
250ac 65 2e 20 20 49 66 20 74 68 61 74 20 69 73 20 74  e.  If that is t
250ad 68 65 20 63 61 73 65 2c 20 74 68 65 6e 20 68 61  he case, then ha
250ae 6c 76 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 63  lve the.    ** c
250af 6f 73 74 20 6f 66 20 74 68 69 73 20 69 6e 64 65  ost of this inde
250b0 78 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  x..    */.    if
250b1 28 20 66 6c 61 67 73 20 26 26 20 70 53 72 63 2d  ( flags && pSrc-
250b2 3e 63 6f 6c 55 73 65 64 20 3c 20 28 28 28 42 69  >colUsed < (((Bi
250b3 74 6d 61 73 6b 29 31 29 3c 3c 28 42 4d 53 2d 31  tmask)1)<<(BMS-1
250b4 29 29 20 29 7b 0a 20 20 20 20 20 20 42 69 74 6d  )) ){.      Bitm
250b5 61 73 6b 20 6d 20 3d 20 70 53 72 63 2d 3e 63 6f  ask m = pSrc->co
250b6 6c 55 73 65 64 3b 0a 20 20 20 20 20 20 69 6e 74  lUsed;.      int
250b7 20 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d   j;.      for(j=
250b8 30 3b 20 6a 3c 70 50 72 6f 62 65 2d 3e 6e 43 6f  0; j<pProbe->nCo
250b9 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  lumn; j++){.    
250ba 20 20 20 20 69 6e 74 20 78 20 3d 20 70 50 72 6f      int x = pPro
250bb 62 65 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b  be->aiColumn[j];
250bc 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 3c 42  .        if( x<B
250bd 4d 53 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  MS-1 ){.        
250be 20 20 6d 20 26 3d 20 7e 28 28 28 42 69 74 6d 61    m &= ~(((Bitma
250bf 73 6b 29 31 29 3c 3c 78 29 3b 0a 20 20 20 20 20  sk)1)<<x);.     
250c0 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
250c1 20 20 20 69 66 28 20 6d 3d 3d 30 20 29 7b 0a 20     if( m==0 ){. 
250c2 20 20 20 20 20 20 20 66 6c 61 67 73 20 7c 3d 20         flags |= 
250c3 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 3b 0a  WHERE_IDX_ONLY;.
250c4 20 20 20 20 20 20 20 20 63 6f 73 74 20 2f 3d 20          cost /= 
250c5 32 3b 0a 20 20 20 20 20 20 20 20 57 48 45 52 45  2;.        WHERE
250c6 54 52 41 43 45 28 28 22 2e 2e 2e 2e 2e 2e 20 69  TRACE(("...... i
250c7 64 78 2d 6f 6e 6c 79 20 72 65 64 75 63 65 73 20  dx-only reduces 
250c8 63 6f 73 74 20 74 6f 20 25 2e 39 67 5c 6e 22 2c  cost to %.9g\n",
250c9 20 63 6f 73 74 29 29 3b 0a 20 20 20 20 20 20 7d   cost));.      }
250ca 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49  .    }..    /* I
250cb 66 20 74 68 69 73 20 69 6e 64 65 78 20 68 61 73  f this index has
250cc 20 61 63 68 69 65 76 65 64 20 74 68 65 20 6c 6f   achieved the lo
250cd 77 65 73 74 20 63 6f 73 74 20 73 6f 20 66 61 72  west cost so far
250ce 2c 20 74 68 65 6e 20 75 73 65 20 69 74 2e 0a 20  , then use it.. 
250cf 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 66 6c     */.    if( fl
250d0 61 67 73 20 26 26 20 63 6f 73 74 20 3c 20 6c 6f  ags && cost < lo
250d1 77 65 73 74 43 6f 73 74 20 29 7b 0a 20 20 20 20  westCost ){.    
250d2 20 20 62 65 73 74 49 64 78 20 3d 20 70 50 72 6f    bestIdx = pPro
250d3 62 65 3b 0a 20 20 20 20 20 20 6c 6f 77 65 73 74  be;.      lowest
250d4 43 6f 73 74 20 3d 20 63 6f 73 74 3b 0a 20 20 20  Cost = cost;.   
250d5 20 20 20 62 65 73 74 46 6c 61 67 73 20 3d 20 66     bestFlags = f
250d6 6c 61 67 73 3b 0a 20 20 20 20 20 20 62 65 73 74  lags;.      best
250d7 4e 45 71 20 3d 20 6e 45 71 3b 0a 20 20 20 20 7d  NEq = nEq;.    }
250d8 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 70 6f 72  .  }..  /* Repor
250d9 74 20 74 68 65 20 62 65 73 74 20 72 65 73 75 6c  t the best resul
250da 74 0a 20 20 2a 2f 0a 20 20 2a 70 70 49 6e 64 65  t.  */.  *ppInde
250db 78 20 3d 20 62 65 73 74 49 64 78 3b 0a 20 20 57  x = bestIdx;.  W
250dc 48 45 52 45 54 52 41 43 45 28 28 22 62 65 73 74  HERETRACE(("best
250dd 20 69 6e 64 65 78 20 69 73 20 25 73 2c 20 63 6f   index is %s, co
250de 73 74 3d 25 2e 39 67 2c 20 66 6c 61 67 73 3d 25  st=%.9g, flags=%
250df 78 2c 20 6e 45 71 3d 25 64 5c 6e 22 2c 0a 20 20  x, nEq=%d\n",.  
250e0 20 20 20 20 20 20 62 65 73 74 49 64 78 20 3f 20        bestIdx ? 
250e1 62 65 73 74 49 64 78 2d 3e 7a 4e 61 6d 65 20 3a  bestIdx->zName :
250e2 20 22 28 6e 6f 6e 65 29 22 2c 20 6c 6f 77 65 73   "(none)", lowes
250e3 74 43 6f 73 74 2c 20 62 65 73 74 46 6c 61 67 73  tCost, bestFlags
250e4 2c 20 62 65 73 74 4e 45 71 29 29 3b 0a 20 20 2a  , bestNEq));.  *
250e5 70 46 6c 61 67 73 20 3d 20 62 65 73 74 46 6c 61  pFlags = bestFla
250e6 67 73 20 7c 20 65 71 54 65 72 6d 4d 61 73 6b 3b  gs | eqTermMask;
250e7 0a 20 20 2a 70 6e 45 71 20 3d 20 62 65 73 74 4e  .  *pnEq = bestN
250e8 45 71 3b 0a 20 20 72 65 74 75 72 6e 20 6c 6f 77  Eq;.  return low
250e9 65 73 74 43 6f 73 74 3b 0a 7d 0a 0a 0a 2f 2a 0a  estCost;.}.../*.
250ea 2a 2a 20 44 69 73 61 62 6c 65 20 61 20 74 65 72  ** Disable a ter
250eb 6d 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63  m in the WHERE c
250ec 6c 61 75 73 65 2e 20 20 45 78 63 65 70 74 2c 20  lause.  Except, 
250ed 64 6f 20 6e 6f 74 20 64 69 73 61 62 6c 65 20 74  do not disable t
250ee 68 65 20 74 65 72 6d 0a 2a 2a 20 69 66 20 69 74  he term.** if it
250ef 20 63 6f 6e 74 72 6f 6c 73 20 61 20 4c 45 46 54   controls a LEFT
250f0 20 4f 55 54 45 52 20 4a 4f 49 4e 20 61 6e 64 20   OUTER JOIN and 
250f1 69 74 20 64 69 64 20 6e 6f 74 20 6f 72 69 67 69  it did not origi
250f2 6e 61 74 65 20 69 6e 20 74 68 65 20 4f 4e 0a 2a  nate in the ON.*
250f3 2a 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73  * or USING claus
250f4 65 20 6f 66 20 74 68 61 74 20 6a 6f 69 6e 2e 0a  e of that join..
250f5 2a 2a 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 20 74  **.** Consider t
250f6 68 65 20 74 65 72 6d 20 74 32 2e 7a 3d 27 6f 6b  he term t2.z='ok
250f7 27 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69  ' in the followi
250f8 6e 67 20 71 75 65 72 69 65 73 3a 0a 2a 2a 0a 2a  ng queries:.**.*
250f9 2a 20 20 20 28 31 29 20 20 53 45 4c 45 43 54 20  *   (1)  SELECT 
250fa 2a 20 46 52 4f 4d 20 74 31 20 4c 45 46 54 20 4a  * FROM t1 LEFT J
250fb 4f 49 4e 20 74 32 20 4f 4e 20 74 31 2e 61 3d 74  OIN t2 ON t1.a=t
250fc 32 2e 78 20 57 48 45 52 45 20 74 32 2e 7a 3d 27  2.x WHERE t2.z='
250fd 6f 6b 27 0a 2a 2a 20 20 20 28 32 29 20 20 53 45  ok'.**   (2)  SE
250fe 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 4c  LECT * FROM t1 L
250ff 45 46 54 20 4a 4f 49 4e 20 74 32 20 4f 4e 20 74  EFT JOIN t2 ON t
25100 31 2e 61 3d 74 32 2e 78 20 41 4e 44 20 74 32 2e  1.a=t2.x AND t2.
25101 7a 3d 27 6f 6b 27 0a 2a 2a 20 20 20 28 33 29 20  z='ok'.**   (3) 
25102 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74   SELECT * FROM t
25103 31 2c 20 74 32 20 57 48 45 52 45 20 74 31 2e 61  1, t2 WHERE t1.a
25104 3d 74 32 2e 78 20 41 4e 44 20 74 32 2e 7a 3d 27  =t2.x AND t2.z='
25105 6f 6b 27 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 32  ok'.**.** The t2
25106 2e 7a 3d 27 6f 6b 27 20 69 73 20 64 69 73 61 62  .z='ok' is disab
25107 6c 65 64 20 69 6e 20 74 68 65 20 69 6e 20 28 32  led in the in (2
25108 29 20 62 65 63 61 75 73 65 20 69 74 20 6f 72 69  ) because it ori
25109 67 69 6e 61 74 65 73 0a 2a 2a 20 69 6e 20 74 68  ginates.** in th
2510a 65 20 4f 4e 20 63 6c 61 75 73 65 2e 20 20 54 68  e ON clause.  Th
2510b 65 20 74 65 72 6d 20 69 73 20 64 69 73 61 62 6c  e term is disabl
2510c 65 64 20 69 6e 20 28 33 29 20 62 65 63 61 75 73  ed in (3) becaus
2510d 65 20 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74  e it is not part
2510e 0a 2a 2a 20 6f 66 20 61 20 4c 45 46 54 20 4f 55  .** of a LEFT OU
2510f 54 45 52 20 4a 4f 49 4e 2e 20 20 49 6e 20 28 31  TER JOIN.  In (1
25110 29 2c 20 74 68 65 20 74 65 72 6d 20 69 73 20 6e  ), the term is n
25111 6f 74 20 64 69 73 61 62 6c 65 64 2e 0a 2a 2a 0a  ot disabled..**.
25112 2a 2a 20 44 69 73 61 62 6c 69 6e 67 20 61 20 74  ** Disabling a t
25113 65 72 6d 20 63 61 75 73 65 73 20 74 68 61 74 20  erm causes that 
25114 74 65 72 6d 20 74 6f 20 6e 6f 74 20 62 65 20 74  term to not be t
25115 65 73 74 65 64 20 69 6e 20 74 68 65 20 69 6e 6e  ested in the inn
25116 65 72 20 6c 6f 6f 70 0a 2a 2a 20 6f 66 20 74 68  er loop.** of th
25117 65 20 6a 6f 69 6e 2e 20 20 44 69 73 61 62 6c 69  e join.  Disabli
25118 6e 67 20 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a  ng is an optimiz
25119 61 74 69 6f 6e 2e 20 20 57 68 65 6e 20 74 65 72  ation.  When ter
2511a 6d 73 20 61 72 65 20 73 61 74 69 73 66 69 65 64  ms are satisfied
2511b 0a 2a 2a 20 62 79 20 69 6e 64 69 63 65 73 2c 20  .** by indices, 
2511c 77 65 20 64 69 73 61 62 6c 65 20 74 68 65 6d 20  we disable them 
2511d 74 6f 20 70 72 65 76 65 6e 74 20 72 65 64 75 6e  to prevent redun
2511e 64 61 6e 74 20 74 65 73 74 73 20 69 6e 20 74 68  dant tests in th
2511f 65 20 69 6e 6e 65 72 0a 2a 2a 20 6c 6f 6f 70 2e  e inner.** loop.
25120 20 20 57 65 20 77 6f 75 6c 64 20 67 65 74 20 74    We would get t
25121 68 65 20 63 6f 72 72 65 63 74 20 72 65 73 75 6c  he correct resul
25122 74 73 20 69 66 20 6e 6f 74 68 69 6e 67 20 77 65  ts if nothing we
25123 72 65 20 65 76 65 72 20 64 69 73 61 62 6c 65 64  re ever disabled
25124 2c 0a 2a 2a 20 62 75 74 20 6a 6f 69 6e 73 20 6d  ,.** but joins m
25125 69 67 68 74 20 72 75 6e 20 61 20 6c 69 74 74 6c  ight run a littl
25126 65 20 73 6c 6f 77 65 72 2e 20 20 54 68 65 20 74  e slower.  The t
25127 72 69 63 6b 20 69 73 20 74 6f 20 64 69 73 61 62  rick is to disab
25128 6c 65 20 61 73 20 6d 75 63 68 0a 2a 2a 20 61 73  le as much.** as
25129 20 77 65 20 63 61 6e 20 77 69 74 68 6f 75 74 20   we can without 
2512a 64 69 73 61 62 6c 69 6e 67 20 74 6f 6f 20 6d 75  disabling too mu
2512b 63 68 2e 20 20 49 66 20 77 65 20 64 69 73 61 62  ch.  If we disab
2512c 6c 65 64 20 69 6e 20 28 31 29 2c 20 77 65 27 64  led in (1), we'd
2512d 20 67 65 74 0a 2a 2a 20 74 68 65 20 77 72 6f 6e   get.** the wron
2512e 67 20 61 6e 73 77 65 72 2e 20 20 53 65 65 20 74  g answer.  See t
2512f 69 63 6b 65 74 20 23 38 31 33 2e 0a 2a 2f 0a 73  icket #813..*/.s
25130 74 61 74 69 63 20 76 6f 69 64 20 64 69 73 61 62  tatic void disab
25131 6c 65 54 65 72 6d 28 57 68 65 72 65 4c 65 76 65  leTerm(WhereLeve
25132 6c 20 2a 70 4c 65 76 65 6c 2c 20 57 68 65 72 65  l *pLevel, Where
25133 54 65 72 6d 20 2a 70 54 65 72 6d 29 7b 0a 20 20  Term *pTerm){.  
25134 69 66 28 20 70 54 65 72 6d 0a 20 20 20 20 20 20  if( pTerm.      
25135 26 26 20 28 70 54 65 72 6d 2d 3e 66 6c 61 67 73  && (pTerm->flags
25136 20 26 20 54 45 52 4d 5f 43 4f 44 45 44 29 3d 3d   & TERM_CODED)==
25137 30 0a 20 20 20 20 20 20 26 26 20 28 70 4c 65 76  0.      && (pLev
25138 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 3d 3d 30  el->iLeftJoin==0
25139 20 7c 7c 20 45 78 70 72 48 61 73 50 72 6f 70 65   || ExprHasPrope
2513a 72 74 79 28 70 54 65 72 6d 2d 3e 70 45 78 70 72  rty(pTerm->pExpr
2513b 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 29 0a  , EP_FromJoin)).
2513c 20 20 29 7b 0a 20 20 20 20 70 54 65 72 6d 2d 3e    ){.    pTerm->
2513d 66 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f  flags |= TERM_CO
2513e 44 45 44 3b 0a 20 20 20 20 69 66 28 20 70 54 65  DED;.    if( pTe
2513f 72 6d 2d 3e 69 50 61 72 65 6e 74 3e 3d 30 20 29  rm->iParent>=0 )
25140 7b 0a 20 20 20 20 20 20 57 68 65 72 65 54 65 72  {.      WhereTer
25141 6d 20 2a 70 4f 74 68 65 72 20 3d 20 26 70 54 65  m *pOther = &pTe
25142 72 6d 2d 3e 70 57 43 2d 3e 61 5b 70 54 65 72 6d  rm->pWC->a[pTerm
25143 2d 3e 69 50 61 72 65 6e 74 5d 3b 0a 20 20 20 20  ->iParent];.    
25144 20 20 69 66 28 20 28 2d 2d 70 4f 74 68 65 72 2d    if( (--pOther-
25145 3e 6e 43 68 69 6c 64 29 3d 3d 30 20 29 7b 0a 20  >nChild)==0 ){. 
25146 20 20 20 20 20 20 20 64 69 73 61 62 6c 65 54 65         disableTe
25147 72 6d 28 70 4c 65 76 65 6c 2c 20 70 4f 74 68 65  rm(pLevel, pOthe
25148 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  r);.      }.    
25149 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  }.  }.}../*.** A
2514a 70 70 6c 79 20 74 68 65 20 61 66 66 69 6e 69 74  pply the affinit
2514b 69 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ies associated w
2514c 69 74 68 20 74 68 65 20 66 69 72 73 74 20 6e 20  ith the first n 
2514d 63 6f 6c 75 6d 6e 73 20 6f 66 20 69 6e 64 65 78  columns of index
2514e 0a 2a 2a 20 70 49 64 78 20 74 6f 20 74 68 65 20  .** pIdx to the 
2514f 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 6e 20  values in the n 
25150 72 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69  registers starti
25151 6e 67 20 61 74 20 62 61 73 65 2e 0a 2a 2f 0a 73  ng at base..*/.s
25152 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 41  tatic void codeA
25153 70 70 6c 79 41 66 66 69 6e 69 74 79 28 50 61 72  pplyAffinity(Par
25154 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
25155 62 61 73 65 2c 20 69 6e 74 20 6e 2c 20 49 6e 64  base, int n, Ind
25156 65 78 20 2a 70 49 64 78 29 7b 0a 20 20 69 66 28  ex *pIdx){.  if(
25157 20 6e 3e 30 20 29 7b 0a 20 20 20 20 56 64 62 65   n>0 ){.    Vdbe
25158 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
25159 64 62 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28  dbe;.    assert(
2515a 20 76 21 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c   v!=0 );.    sql
2515b 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2515c 2c 20 4f 50 5f 41 66 66 69 6e 69 74 79 2c 20 62  , OP_Affinity, b
2515d 61 73 65 2c 20 6e 29 3b 0a 20 20 20 20 73 71 6c  ase, n);.    sql
2515e 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74  ite3IndexAffinit
2515f 79 53 74 72 28 76 2c 20 70 49 64 78 29 3b 0a 20  yStr(v, pIdx);. 
25160 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
25161 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67  cheAffinityChang
25162 65 28 70 50 61 72 73 65 2c 20 62 61 73 65 2c 20  e(pParse, base, 
25163 6e 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a  n);.  }.}.../*.*
25164 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
25165 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 65 71 75  for a single equ
25166 61 6c 69 74 79 20 74 65 72 6d 20 6f 66 20 74 68  ality term of th
25167 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20  e WHERE clause. 
25168 20 41 6e 20 65 71 75 61 6c 69 74 79 0a 2a 2a 20   An equality.** 
25169 74 65 72 6d 20 63 61 6e 20 62 65 20 65 69 74 68  term can be eith
2516a 65 72 20 58 3d 65 78 70 72 20 6f 72 20 58 20 49  er X=expr or X I
2516b 4e 20 28 2e 2e 2e 29 2e 20 20 20 70 54 65 72 6d  N (...).   pTerm
2516c 20 69 73 20 74 68 65 20 74 65 72 6d 20 74 6f 20   is the term to 
2516d 62 65 20 0a 2a 2a 20 63 6f 64 65 64 2e 0a 2a 2a  be .** coded..**
2516e 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20  .** The current 
2516f 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 63 6f  value for the co
25170 6e 73 74 72 61 69 6e 74 20 69 73 20 6c 65 66 74  nstraint is left
25171 20 69 6e 20 72 65 67 69 73 74 65 72 20 69 52 65   in register iRe
25172 67 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20 63  g..**.** For a c
25173 6f 6e 73 74 72 61 69 6e 74 20 6f 66 20 74 68 65  onstraint of the
25174 20 66 6f 72 6d 20 58 3d 65 78 70 72 2c 20 74 68   form X=expr, th
25175 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
25176 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 69 74  evaluated and it
25177 73 0a 2a 2a 20 72 65 73 75 6c 74 20 69 73 20 6c  s.** result is l
25178 65 66 74 20 6f 6e 20 74 68 65 20 73 74 61 63 6b  eft on the stack
25179 2e 20 20 46 6f 72 20 63 6f 6e 73 74 72 61 69 6e  .  For constrain
2517a 74 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 58  ts of the form X
2517b 20 49 4e 20 28 2e 2e 2e 29 0a 2a 2a 20 74 68 69   IN (...).** thi
2517c 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 75  s routine sets u
2517d 70 20 61 20 6c 6f 6f 70 20 74 68 61 74 20 77 69  p a loop that wi
2517e 6c 6c 20 69 74 65 72 61 74 65 20 6f 76 65 72 20  ll iterate over 
2517f 61 6c 6c 20 76 61 6c 75 65 73 20 6f 66 20 58 2e  all values of X.
25180 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
25181 6f 64 65 45 71 75 61 6c 69 74 79 54 65 72 6d 28  odeEqualityTerm(
25182 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
25183 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61  ,      /* The pa
25184 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
25185 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54  .  WhereTerm *pT
25186 65 72 6d 2c 20 20 20 2f 2a 20 54 68 65 20 74 65  erm,   /* The te
25187 72 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45 20  rm of the WHERE 
25188 63 6c 61 75 73 65 20 74 6f 20 62 65 20 63 6f 64  clause to be cod
25189 65 64 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76  ed */.  WhereLev
2518a 65 6c 20 2a 70 4c 65 76 65 6c 2c 20 2f 2a 20 57  el *pLevel, /* W
2518b 68 65 6e 20 6c 65 76 65 6c 20 6f 66 20 74 68 65  hen level of the
2518c 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 77 65 20   FROM clause we 
2518d 61 72 65 20 77 6f 72 6b 69 6e 67 20 6f 6e 20 2a  are working on *
2518e 2f 0a 20 20 69 6e 74 20 69 54 61 72 67 65 74 20  /.  int iTarget 
2518f 20 20 20 20 20 20 20 20 2f 2a 20 41 74 74 65 6d          /* Attem
25190 70 74 20 74 6f 20 6c 65 61 76 65 20 72 65 73 75  pt to leave resu
25191 6c 74 73 20 69 6e 20 74 68 69 73 20 72 65 67 69  lts in this regi
25192 73 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 45 78 70  ster */.){.  Exp
25193 72 20 2a 70 58 20 3d 20 70 54 65 72 6d 2d 3e 70  r *pX = pTerm->p
25194 45 78 70 72 3b 0a 20 20 56 64 62 65 20 2a 76 20  Expr;.  Vdbe *v 
25195 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
25196 0a 20 20 69 6e 74 20 69 52 65 67 3b 20 20 20 20  .  int iReg;    
25197 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
25198 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   Register holdin
25199 67 20 72 65 73 75 6c 74 73 20 2a 2f 0a 0a 20 20  g results */..  
2519a 69 66 28 20 69 54 61 72 67 65 74 3c 3d 30 20 29  if( iTarget<=0 )
2519b 7b 0a 20 20 20 20 69 52 65 67 20 3d 20 69 54 61  {.    iReg = iTa
2519c 72 67 65 74 20 3d 20 73 71 6c 69 74 65 33 47 65  rget = sqlite3Ge
2519d 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
2519e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 58 2d 3e  ;.  }.  if( pX->
2519f 6f 70 3d 3d 54 4b 5f 45 51 20 29 7b 0a 20 20 20  op==TK_EQ ){.   
251a0 20 69 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45   iReg = sqlite3E
251a1 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50  xprCodeTarget(pP
251a2 61 72 73 65 2c 20 70 58 2d 3e 70 52 69 67 68 74  arse, pX->pRight
251a3 2c 20 69 54 61 72 67 65 74 29 3b 0a 20 20 7d 65  , iTarget);.  }e
251a4 6c 73 65 20 69 66 28 20 70 58 2d 3e 6f 70 3d 3d  lse if( pX->op==
251a5 54 4b 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20  TK_ISNULL ){.   
251a6 20 69 52 65 67 20 3d 20 69 54 61 72 67 65 74 3b   iReg = iTarget;
251a7 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
251a8 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
251a9 6c 2c 20 30 2c 20 69 52 65 67 29 3b 0a 23 69 66  l, 0, iReg);.#if
251aa 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
251ab 5f 53 55 42 51 55 45 52 59 0a 20 20 7d 65 6c 73  _SUBQUERY.  }els
251ac 65 7b 0a 20 20 20 20 69 6e 74 20 65 54 79 70 65  e{.    int eType
251ad 3b 0a 20 20 20 20 69 6e 74 20 69 54 61 62 3b 0a  ;.    int iTab;.
251ae 20 20 20 20 73 74 72 75 63 74 20 49 6e 4c 6f 6f      struct InLoo
251af 70 20 2a 70 49 6e 3b 0a 0a 20 20 20 20 61 73 73  p *pIn;..    ass
251b0 65 72 74 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f  ert( pX->op==TK_
251b1 49 4e 20 29 3b 0a 20 20 20 20 69 52 65 67 20 3d  IN );.    iReg =
251b2 20 69 54 61 72 67 65 74 3b 0a 20 20 20 20 65 54   iTarget;.    eT
251b3 79 70 65 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  ype = sqlite3Fin
251b4 64 49 6e 49 6e 64 65 78 28 70 50 61 72 73 65 2c  dInIndex(pParse,
251b5 20 70 58 2c 20 31 29 3b 0a 20 20 20 20 69 54 61   pX, 1);.    iTa
251b6 62 20 3d 20 70 58 2d 3e 69 54 61 62 6c 65 3b 0a  b = pX->iTable;.
251b7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
251b8 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69  ddOp2(v, OP_Rewi
251b9 6e 64 2c 20 69 54 61 62 2c 20 30 29 3b 0a 20 20  nd, iTab, 0);.  
251ba 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
251bb 2c 20 22 25 2e 2a 73 22 2c 20 70 58 2d 3e 73 70  , "%.*s", pX->sp
251bc 61 6e 2e 6e 2c 20 70 58 2d 3e 73 70 61 6e 2e 7a  an.n, pX->span.z
251bd 29 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76  ));.    if( pLev
251be 65 6c 2d 3e 6e 49 6e 3d 3d 30 20 29 7b 0a 20 20  el->nIn==0 ){.  
251bf 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6e 78 74 20      pLevel->nxt 
251c0 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
251c1 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 7d  eLabel(v);.    }
251c2 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6e 49 6e  .    pLevel->nIn
251c3 2b 2b 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e  ++;.    pLevel->
251c4 61 49 6e 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65  aInLoop = sqlite
251c5 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65  3DbReallocOrFree
251c6 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4c 65  (pParse->db, pLe
251c7 76 65 6c 2d 3e 61 49 6e 4c 6f 6f 70 2c 0a 20 20  vel->aInLoop,.  
251c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
251c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
251ca 20 20 73 69 7a 65 6f 66 28 70 4c 65 76 65 6c 2d    sizeof(pLevel-
251cb 3e 61 49 6e 4c 6f 6f 70 5b 30 5d 29 2a 70 4c 65  >aInLoop[0])*pLe
251cc 76 65 6c 2d 3e 6e 49 6e 29 3b 0a 20 20 20 20 70  vel->nIn);.    p
251cd 49 6e 20 3d 20 70 4c 65 76 65 6c 2d 3e 61 49 6e  In = pLevel->aIn
251ce 4c 6f 6f 70 3b 0a 20 20 20 20 69 66 28 20 70 49  Loop;.    if( pI
251cf 6e 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 20 2b  n ){.      pIn +
251d0 3d 20 70 4c 65 76 65 6c 2d 3e 6e 49 6e 20 2d 20  = pLevel->nIn - 
251d1 31 3b 0a 20 20 20 20 20 20 70 49 6e 2d 3e 69 43  1;.      pIn->iC
251d2 75 72 20 3d 20 69 54 61 62 3b 0a 20 20 20 20 20  ur = iTab;.     
251d3 20 69 66 28 20 65 54 79 70 65 3d 3d 49 4e 5f 49   if( eType==IN_I
251d4 4e 44 45 58 5f 52 4f 57 49 44 20 29 7b 0a 20 20  NDEX_ROWID ){.  
251d5 20 20 20 20 20 20 70 49 6e 2d 3e 74 6f 70 41 64        pIn->topAd
251d6 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
251d7 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77  AddOp2(v, OP_Row
251d8 69 64 2c 20 69 54 61 62 2c 20 69 52 65 67 29 3b  id, iTab, iReg);
251d9 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
251da 20 20 20 20 20 20 70 49 6e 2d 3e 74 6f 70 41 64        pIn->topAd
251db 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
251dc 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
251dd 75 6d 6e 2c 20 69 54 61 62 2c 20 30 2c 20 69 52  umn, iTab, 0, iR
251de 65 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eg);.      }.   
251df 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
251e0 64 4f 70 31 28 76 2c 20 4f 50 5f 49 73 4e 75 6c  dOp1(v, OP_IsNul
251e1 6c 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 7d 65  l, iReg);.    }e
251e2 6c 73 65 7b 0a 20 20 20 20 20 20 70 4c 65 76 65  lse{.      pLeve
251e3 6c 2d 3e 6e 49 6e 20 3d 20 30 3b 0a 20 20 20 20  l->nIn = 0;.    
251e4 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 64  }.#endif.  }.  d
251e5 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65  isableTerm(pLeve
251e6 6c 2c 20 70 54 65 72 6d 29 3b 0a 20 20 72 65 74  l, pTerm);.  ret
251e7 75 72 6e 20 69 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a  urn iReg;.}../*.
251e8 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
251e9 20 74 68 61 74 20 77 69 6c 6c 20 65 76 61 6c 75   that will evalu
251ea 61 74 65 20 61 6c 6c 20 3d 3d 20 61 6e 64 20 49  ate all == and I
251eb 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 66 6f  N constraints fo
251ec 72 20 61 6e 0a 2a 2a 20 69 6e 64 65 78 2e 20 20  r an.** index.  
251ed 54 68 65 20 76 61 6c 75 65 73 20 66 6f 72 20 61  The values for a
251ee 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61  ll constraints a
251ef 72 65 20 6c 65 66 74 20 6f 6e 20 74 68 65 20 73  re left on the s
251f0 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  tack..**.** For 
251f1 65 78 61 6d 70 6c 65 2c 20 63 6f 6e 73 69 64 65  example, conside
251f2 72 20 74 61 62 6c 65 20 74 31 28 61 2c 62 2c 63  r table t1(a,b,c
251f3 2c 64 2c 65 2c 66 29 20 77 69 74 68 20 69 6e 64  ,d,e,f) with ind
251f4 65 78 20 69 31 28 61 2c 62 2c 63 29 2e 0a 2a 2a  ex i1(a,b,c)..**
251f5 20 53 75 70 70 6f 73 65 20 74 68 65 20 57 48 45   Suppose the WHE
251f6 52 45 20 63 6c 61 75 73 65 20 69 73 20 74 68 69  RE clause is thi
251f7 73 3a 20 20 61 3d 3d 35 20 41 4e 44 20 62 20 49  s:  a==5 AND b I
251f8 4e 20 28 31 2c 32 2c 33 29 20 41 4e 44 20 63 3e  N (1,2,3) AND c>
251f9 35 20 41 4e 44 20 63 3c 31 30 0a 2a 2a 20 54 68  5 AND c<10.** Th
251fa 65 20 69 6e 64 65 78 20 68 61 73 20 61 73 20 6d  e index has as m
251fb 61 6e 79 20 61 73 20 74 68 72 65 65 20 65 71 75  any as three equ
251fc 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
251fd 73 2c 20 62 75 74 20 69 6e 20 74 68 69 73 0a 2a  s, but in this.*
251fe 2a 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 74  * example, the t
251ff 68 69 72 64 20 22 63 22 20 76 61 6c 75 65 20 69  hird "c" value i
25200 73 20 61 6e 20 69 6e 65 71 75 61 6c 69 74 79 2e  s an inequality.
25201 20 20 53 6f 20 6f 6e 6c 79 20 74 77 6f 20 0a 2a    So only two .*
25202 2a 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72  * constraints ar
25203 65 20 63 6f 64 65 64 2e 20 20 54 68 69 73 20 72  e coded.  This r
25204 6f 75 74 69 6e 65 20 77 69 6c 6c 20 67 65 6e 65  outine will gene
25205 72 61 74 65 20 63 6f 64 65 20 74 6f 20 65 76 61  rate code to eva
25206 6c 75 61 74 65 0a 2a 2a 20 61 3d 3d 35 20 61 6e  luate.** a==5 an
25207 64 20 62 20 49 4e 20 28 31 2c 32 2c 33 29 2e 20  d b IN (1,2,3). 
25208 20 54 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c   The current val
25209 75 65 73 20 66 6f 72 20 61 20 61 6e 64 20 62 20  ues for a and b 
2520a 77 69 6c 6c 20 62 65 20 6c 65 66 74 0a 2a 2a 20  will be left.** 
2520b 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 2d 20 61  on the stack - a
2520c 20 69 73 20 74 68 65 20 64 65 65 70 65 73 74 20   is the deepest 
2520d 61 6e 64 20 62 20 74 68 65 20 73 68 61 6c 6c 6f  and b the shallo
2520e 77 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74  west..**.** In t
2520f 68 65 20 65 78 61 6d 70 6c 65 20 61 62 6f 76 65  he example above
25210 20 6e 45 71 3d 3d 32 2e 20 20 42 75 74 20 74 68   nEq==2.  But th
25211 69 73 20 73 75 62 72 6f 75 74 69 6e 65 20 77 6f  is subroutine wo
25212 72 6b 73 20 66 6f 72 20 61 6e 79 20 76 61 6c 75  rks for any valu
25213 65 0a 2a 2a 20 6f 66 20 6e 45 71 20 69 6e 63 6c  e.** of nEq incl
25214 75 64 69 6e 67 20 30 2e 20 20 49 66 20 6e 45 71  uding 0.  If nEq
25215 3d 3d 30 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  ==0, this routin
25216 65 20 69 73 20 6e 65 61 72 6c 79 20 61 20 6e 6f  e is nearly a no
25217 2d 6f 70 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79  -op..** The only
25218 20 74 68 69 6e 67 20 69 74 20 64 6f 65 73 20 69   thing it does i
25219 73 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 70  s allocate the p
2521a 4c 65 76 65 6c 2d 3e 69 4d 65 6d 20 6d 65 6d 6f  Level->iMem memo
2521b 72 79 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54  ry cell..**.** T
2521c 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 77 61  his routine alwa
2521d 79 73 20 61 6c 6c 6f 63 61 74 65 73 20 61 74 20  ys allocates at 
2521e 6c 65 61 73 74 20 6f 6e 65 20 6d 65 6d 6f 72 79  least one memory
2521f 20 63 65 6c 6c 20 61 6e 64 20 70 75 74 73 0a 2a   cell and puts.*
25220 2a 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66  * the address of
25221 20 74 68 61 74 20 6d 65 6d 6f 72 79 20 63 65 6c   that memory cel
25222 6c 20 69 6e 20 70 4c 65 76 65 6c 2d 3e 69 4d 65  l in pLevel->iMe
25223 6d 2e 20 20 54 68 65 20 63 6f 64 65 20 74 68 61  m.  The code tha
25224 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 68 69 73 20  t.** calls this 
25225 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 75 73 65  routine will use
25226 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d 20 74 6f   pLevel->iMem to
25227 20 73 74 6f 72 65 20 74 68 65 20 74 65 72 6d 69   store the termi
25228 6e 61 74 69 6f 6e 0a 2a 2a 20 6b 65 79 20 76 61  nation.** key va
25229 6c 75 65 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e  lue of the loop.
2522a 20 20 49 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65    If one or more
2522b 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 20 61 70   IN operators ap
2522c 70 65 61 72 2c 20 74 68 65 6e 0a 2a 2a 20 74 68  pear, then.** th
2522d 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63  is routine alloc
2522e 61 74 65 73 20 61 6e 20 61 64 64 69 74 69 6f 6e  ates an addition
2522f 61 6c 20 6e 45 71 20 6d 65 6d 6f 72 79 20 63 65  al nEq memory ce
25230 6c 6c 73 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c  lls for internal
25231 0a 2a 2a 20 75 73 65 2e 0a 2a 2f 0a 73 74 61 74  .** use..*/.stat
25232 69 63 20 69 6e 74 20 63 6f 64 65 41 6c 6c 45 71  ic int codeAllEq
25233 75 61 6c 69 74 79 54 65 72 6d 73 28 0a 20 20 50  ualityTerms(.  P
25234 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
25235 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
25236 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65  context */.  Whe
25237 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c 2c  reLevel *pLevel,
25238 20 20 20 2f 2a 20 57 68 69 63 68 20 6e 65 73 74     /* Which nest
25239 65 64 20 6c 6f 6f 70 20 6f 66 20 74 68 65 20 46  ed loop of the F
2523a 52 4f 4d 20 77 65 20 61 72 65 20 63 6f 64 69 6e  ROM we are codin
2523b 67 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75  g */.  WhereClau
2523c 73 65 20 2a 70 57 43 2c 20 20 20 20 20 2f 2a 20  se *pWC,     /* 
2523d 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  The WHERE clause
2523e 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f   */.  Bitmask no
2523f 74 52 65 61 64 79 2c 20 20 20 20 20 2f 2a 20 57  tReady,     /* W
25240 68 69 63 68 20 70 61 72 74 73 20 6f 66 20 46 52  hich parts of FR
25241 4f 4d 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20  OM have not yet 
25242 62 65 65 6e 20 63 6f 64 65 64 20 2a 2f 0a 20 20  been coded */.  
25243 69 6e 74 20 6e 45 78 74 72 61 52 65 67 20 20 20  int nExtraReg   
25244 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
25245 6f 66 20 65 78 74 72 61 20 72 65 67 69 73 74 65  of extra registe
25246 72 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a  rs to allocate *
25247 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 45 71 20 3d  /.){.  int nEq =
25248 20 70 4c 65 76 65 6c 2d 3e 6e 45 71 3b 20 20 20   pLevel->nEq;   
25249 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62       /* The numb
2524a 65 72 20 6f 66 20 3d 3d 20 6f 72 20 49 4e 20 63  er of == or IN c
2524b 6f 6e 73 74 72 61 69 6e 74 73 20 74 6f 20 63 6f  onstraints to co
2524c 64 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20  de */.  Vdbe *v 
2524d 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
2524e 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72        /* The vir
2524f 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 75 6e 64  tual machine und
25250 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20  er construction 
25251 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  */.  Index *pIdx
25252 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 49 64 78 3b   = pLevel->pIdx;
25253 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20     /* The index 
25254 62 65 69 6e 67 20 75 73 65 64 20 66 6f 72 20 74  being used for t
25255 68 69 73 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e  his loop */.  in
25256 74 20 69 43 75 72 20 3d 20 70 4c 65 76 65 6c 2d  t iCur = pLevel-
25257 3e 69 54 61 62 43 75 72 3b 20 20 20 2f 2a 20 54  >iTabCur;   /* T
25258 68 65 20 63 75 72 73 6f 72 20 6f 66 20 74 68 65  he cursor of the
25259 20 74 61 62 6c 65 20 2a 2f 0a 20 20 57 68 65 72   table */.  Wher
2525a 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20  eTerm *pTerm;   
2525b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73            /* A s
2525c 69 6e 67 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74  ingle constraint
2525d 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 6a   term */.  int j
2525e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2525f 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
25260 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
25261 74 20 72 65 67 42 61 73 65 3b 20 20 20 20 20 20  t regBase;      
25262 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
25263 61 73 65 20 72 65 67 69 73 74 65 72 20 2a 2f 0a  ase register */.
25264 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74  .  /* Figure out
25265 20 68 6f 77 20 6d 61 6e 79 20 6d 65 6d 6f 72 79   how many memory
25266 20 63 65 6c 6c 73 20 77 65 20 77 69 6c 6c 20 6e   cells we will n
25267 65 65 64 20 74 68 65 6e 20 61 6c 6c 6f 63 61 74  eed then allocat
25268 65 20 74 68 65 6d 2e 0a 20 20 2a 2a 20 57 65 20  e them..  ** We 
25269 61 6c 77 61 79 73 20 6e 65 65 64 20 61 74 20 6c  always need at l
2526a 65 61 73 74 20 6f 6e 65 20 75 73 65 64 20 74 6f  east one used to
2526b 20 73 74 6f 72 65 20 74 68 65 20 6c 6f 6f 70 20   store the loop 
2526c 74 65 72 6d 69 6e 61 74 6f 72 0a 20 20 2a 2a 20  terminator.  ** 
2526d 76 61 6c 75 65 2e 20 20 49 66 20 74 68 65 72 65  value.  If there
2526e 20 61 72 65 20 49 4e 20 6f 70 65 72 61 74 6f 72   are IN operator
2526f 73 20 77 65 27 6c 6c 20 6e 65 65 64 20 6f 6e 65  s we'll need one
25270 20 66 6f 72 20 65 61 63 68 20 3d 3d 20 6f 72 0a   for each == or.
25271 20 20 2a 2a 20 49 4e 20 63 6f 6e 73 74 72 61 69    ** IN constrai
25272 6e 74 2e 0a 20 20 2a 2f 0a 20 20 70 4c 65 76 65  nt..  */.  pLeve
25273 6c 2d 3e 69 4d 65 6d 20 3d 20 70 50 61 72 73 65  l->iMem = pParse
25274 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20 72 65  ->nMem + 1;.  re
25275 67 42 61 73 65 20 3d 20 70 50 61 72 73 65 2d 3e  gBase = pParse->
25276 6e 4d 65 6d 20 2b 20 32 3b 0a 20 20 70 50 61 72  nMem + 2;.  pPar
25277 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 4c 65 76  se->nMem += pLev
25278 65 6c 2d 3e 6e 45 71 20 2b 20 32 20 2b 20 6e 45  el->nEq + 2 + nE
25279 78 74 72 61 52 65 67 3b 0a 0a 20 20 2f 2a 20 45  xtraReg;..  /* E
2527a 76 61 6c 75 61 74 65 20 74 68 65 20 65 71 75 61  valuate the equa
2527b 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73  lity constraints
2527c 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
2527d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 3d 6e  pIdx->nColumn>=n
2527e 45 71 20 29 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b  Eq );.  for(j=0;
2527f 20 6a 3c 6e 45 71 3b 20 6a 2b 2b 29 7b 0a 20 20   j<nEq; j++){.  
25280 20 20 69 6e 74 20 72 31 3b 0a 20 20 20 20 69 6e    int r1;.    in
25281 74 20 6b 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f  t k = pIdx->aiCo
25282 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20 20 70 54 65  lumn[j];.    pTe
25283 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57  rm = findTerm(pW
25284 43 2c 20 69 43 75 72 2c 20 6b 2c 20 6e 6f 74 52  C, iCur, k, notR
25285 65 61 64 79 2c 20 70 4c 65 76 65 6c 2d 3e 66 6c  eady, pLevel->fl
25286 61 67 73 2c 20 70 49 64 78 29 3b 0a 20 20 20 20  ags, pIdx);.    
25287 69 66 28 20 70 54 65 72 6d 3d 3d 30 20 29 20 62  if( pTerm==0 ) b
25288 72 65 61 6b 3b 0a 20 20 20 20 61 73 73 65 72 74  reak;.    assert
25289 28 20 28 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20  ( (pTerm->flags 
2528a 26 20 54 45 52 4d 5f 43 4f 44 45 44 29 3d 3d 30  & TERM_CODED)==0
2528b 20 29 3b 0a 20 20 20 20 72 31 20 3d 20 63 6f 64   );.    r1 = cod
2528c 65 45 71 75 61 6c 69 74 79 54 65 72 6d 28 70 50  eEqualityTerm(pP
2528d 61 72 73 65 2c 20 70 54 65 72 6d 2c 20 70 4c 65  arse, pTerm, pLe
2528e 76 65 6c 2c 20 72 65 67 42 61 73 65 2b 6a 29 3b  vel, regBase+j);
2528f 0a 20 20 20 20 69 66 28 20 72 31 21 3d 72 65 67  .    if( r1!=reg
25290 42 61 73 65 2b 6a 20 29 7b 0a 20 20 20 20 20 20  Base+j ){.      
25291 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25292 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72  2(v, OP_SCopy, r
25293 31 2c 20 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20  1, regBase+j);. 
25294 20 20 20 7d 0a 20 20 20 20 74 65 73 74 63 61 73     }.    testcas
25295 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  e( pTerm->eOpera
25296 74 6f 72 20 26 20 57 4f 5f 49 53 4e 55 4c 4c 20  tor & WO_ISNULL 
25297 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
25298 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
25299 72 20 26 20 57 4f 5f 49 4e 20 29 3b 0a 20 20 20  r & WO_IN );.   
2529a 20 69 66 28 20 28 70 54 65 72 6d 2d 3e 65 4f 70   if( (pTerm->eOp
2529b 65 72 61 74 6f 72 20 26 20 28 57 4f 5f 49 53 4e  erator & (WO_ISN
2529c 55 4c 4c 7c 57 4f 5f 49 4e 29 29 3d 3d 30 20 29  ULL|WO_IN))==0 )
2529d 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
2529e 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2529f 49 73 4e 75 6c 6c 2c 20 72 65 67 42 61 73 65 2b  IsNull, regBase+
252a0 6a 2c 20 70 4c 65 76 65 6c 2d 3e 62 72 6b 29 3b  j, pLevel->brk);
252a1 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
252a2 75 72 6e 20 72 65 67 42 61 73 65 3b 0a 7d 0a 0a  urn regBase;.}..
252a3 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
252a4 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 54  TE_TEST)./*.** T
252a5 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72  he following var
252a6 69 61 62 6c 65 20 68 6f 6c 64 73 20 61 20 74 65  iable holds a te
252a7 78 74 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f  xt description o
252a8 66 20 71 75 65 72 79 20 70 6c 61 6e 20 67 65 6e  f query plan gen
252a9 65 72 61 74 65 64 0a 2a 2a 20 62 79 20 74 68 65  erated.** by the
252aa 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c   most recent cal
252ab 6c 20 74 6f 20 73 71 6c 69 74 65 33 57 68 65 72  l to sqlite3Wher
252ac 65 42 65 67 69 6e 28 29 2e 20 20 45 61 63 68 20  eBegin().  Each 
252ad 63 61 6c 6c 20 74 6f 20 57 68 65 72 65 42 65 67  call to WhereBeg
252ae 69 6e 0a 2a 2a 20 6f 76 65 72 77 72 69 74 65 73  in.** overwrites
252af 20 74 68 65 20 70 72 65 76 69 6f 75 73 2e 20 20   the previous.  
252b0 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  This information
252b1 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73   is used for tes
252b2 74 69 6e 67 20 61 6e 64 0a 2a 2a 20 61 6e 61 6c  ting and.** anal
252b3 79 73 69 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51  ysis only..*/.SQ
252b4 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 73 71  LITE_API char sq
252b5 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e  lite3_query_plan
252b6 5b 42 4d 53 2a 32 2a 34 30 5d 3b 20 20 2f 2a 20  [BMS*2*40];  /* 
252b7 54 65 78 74 20 6f 66 20 74 68 65 20 6a 6f 69 6e  Text of the join
252b8 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e   */.static int n
252b9 51 50 6c 61 6e 20 3d 20 30 3b 20 20 20 20 20 20  QPlan = 0;      
252ba 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20          /* Next 
252bb 66 72 65 65 20 73 6c 6f 77 20 69 6e 20 5f 71 75  free slow in _qu
252bc 65 72 79 5f 70 6c 61 6e 5b 5d 20 2a 2f 0a 0a 23  ery_plan[] */..#
252bd 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
252be 54 45 53 54 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20  TEST */.../*.** 
252bf 46 72 65 65 20 61 20 57 68 65 72 65 49 6e 66 6f  Free a WhereInfo
252c0 20 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a 73 74   structure.*/.st
252c1 61 74 69 63 20 76 6f 69 64 20 77 68 65 72 65 49  atic void whereI
252c2 6e 66 6f 46 72 65 65 28 57 68 65 72 65 49 6e 66  nfoFree(WhereInf
252c3 6f 20 2a 70 57 49 6e 66 6f 29 7b 0a 20 20 69 66  o *pWInfo){.  if
252c4 28 20 70 57 49 6e 66 6f 20 29 7b 0a 20 20 20 20  ( pWInfo ){.    
252c5 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69  int i;.    for(i
252c6 3d 30 3b 20 69 3c 70 57 49 6e 66 6f 2d 3e 6e 4c  =0; i<pWInfo->nL
252c7 65 76 65 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  evel; i++){.    
252c8 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f    sqlite3_index_
252c9 69 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 70 57  info *pInfo = pW
252ca 49 6e 66 6f 2d 3e 61 5b 69 5d 2e 70 49 64 78 49  Info->a[i].pIdxI
252cb 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 70  nfo;.      if( p
252cc 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 20 20  Info ){.        
252cd 61 73 73 65 72 74 28 20 70 49 6e 66 6f 2d 3e 6e  assert( pInfo->n
252ce 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 3d  eedToFreeIdxStr=
252cf 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71  =0 );.        sq
252d0 6c 69 74 65 33 5f 66 72 65 65 28 70 49 6e 66 6f  lite3_free(pInfo
252d1 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
252d2 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
252d3 65 28 70 57 49 6e 66 6f 29 3b 0a 20 20 7d 0a 7d  e(pWInfo);.  }.}
252d4 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  .../*.** Generat
252d5 65 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  e the beginning 
252d6 6f 66 20 74 68 65 20 6c 6f 6f 70 20 75 73 65 64  of the loop used
252d7 20 66 6f 72 20 57 48 45 52 45 20 63 6c 61 75 73   for WHERE claus
252d8 65 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a  e processing..**
252d9 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
252da 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  e is a pointer t
252db 6f 20 61 6e 20 6f 70 61 71 75 65 20 73 74 72 75  o an opaque stru
252dc 63 74 75 72 65 20 74 68 61 74 20 63 6f 6e 74 61  cture that conta
252dd 69 6e 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  ins.** informati
252de 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 74 65 72  on needed to ter
252df 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 2e  minate the loop.
252e0 20 20 4c 61 74 65 72 2c 20 74 68 65 20 63 61 6c    Later, the cal
252e1 6c 69 6e 67 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  ling routine.** 
252e2 73 68 6f 75 6c 64 20 69 6e 76 6f 6b 65 20 73 71  should invoke sq
252e3 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 29 20  lite3WhereEnd() 
252e4 77 69 74 68 20 74 68 65 20 72 65 74 75 72 6e 20  with the return 
252e5 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75  value of this fu
252e6 6e 63 74 69 6f 6e 0a 2a 2a 20 69 6e 20 6f 72 64  nction.** in ord
252e7 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74  er to complete t
252e8 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
252e9 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a 0a 2a  processing..**.*
252ea 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  * If an error oc
252eb 63 75 72 73 2c 20 74 68 69 73 20 72 6f 75 74 69  curs, this routi
252ec 6e 65 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e  ne returns NULL.
252ed 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 73 69 63  .**.** The basic
252ee 20 69 64 65 61 20 69 73 20 74 6f 20 64 6f 20 61   idea is to do a
252ef 20 6e 65 73 74 65 64 20 6c 6f 6f 70 2c 20 6f 6e   nested loop, on
252f0 65 20 6c 6f 6f 70 20 66 6f 72 20 65 61 63 68 20  e loop for each 
252f1 74 61 62 6c 65 20 69 6e 0a 2a 2a 20 74 68 65 20  table in.** the 
252f2 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61  FROM clause of a
252f3 20 73 65 6c 65 63 74 2e 20 20 28 49 4e 53 45 52   select.  (INSER
252f4 54 20 61 6e 64 20 55 50 44 41 54 45 20 73 74 61  T and UPDATE sta
252f5 74 65 6d 65 6e 74 73 20 61 72 65 20 74 68 65 0a  tements are the.
252f6 2a 2a 20 73 61 6d 65 20 61 73 20 61 20 53 45 4c  ** same as a SEL
252f7 45 43 54 20 77 69 74 68 20 6f 6e 6c 79 20 61 20  ECT with only a 
252f8 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e 20  single table in 
252f9 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e  the FROM clause.
252fa 29 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c  )  For.** exampl
252fb 65 2c 20 69 66 20 74 68 65 20 53 51 4c 20 69 73  e, if the SQL is
252fc 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   this:.**.**    
252fd 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d     SELECT * FROM
252fe 20 74 31 2c 20 74 32 2c 20 74 33 20 57 48 45 52   t1, t2, t3 WHER
252ff 45 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20 54 68 65  E ...;.**.** The
25300 6e 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72  n the code gener
25301 61 74 65 64 20 69 73 20 63 6f 6e 63 65 70 74 75  ated is conceptu
25302 61 6c 6c 79 20 6c 69 6b 65 20 74 68 65 20 66 6f  ally like the fo
25303 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  llowing:.**.**  
25304 20 20 20 20 66 6f 72 65 61 63 68 20 72 6f 77 31      foreach row1
25305 20 69 6e 20 74 31 20 64 6f 20 20 20 20 20 20 20   in t1 do       
25306 5c 20 20 20 20 43 6f 64 65 20 67 65 6e 65 72 61  \    Code genera
25307 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 66 6f  ted.**        fo
25308 72 65 61 63 68 20 72 6f 77 32 20 69 6e 20 74 32  reach row2 in t2
25309 20 64 6f 20 20 20 20 20 20 7c 2d 2d 20 62 79 20   do      |-- by 
2530a 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69  sqlite3WhereBegi
2530b 6e 28 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  n().**          
2530c 66 6f 72 65 61 63 68 20 72 6f 77 33 20 69 6e 20  foreach row3 in 
2530d 74 33 20 64 6f 20 20 20 2f 0a 2a 2a 20 20 20 20  t3 do   /.**    
2530e 20 20 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20          ....**  
2530f 20 20 20 20 20 20 20 20 65 6e 64 20 20 20 20 20          end     
25310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25311 5c 20 20 20 20 43 6f 64 65 20 67 65 6e 65 72 61  \    Code genera
25312 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 65 6e  ted.**        en
25313 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
25314 20 20 20 20 20 20 20 20 20 7c 2d 2d 20 62 79 20           |-- by 
25315 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28  sqlite3WhereEnd(
25316 29 0a 2a 2a 20 20 20 20 20 20 65 6e 64 20 20 20  ).**      end   
25317 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25318 20 20 20 20 20 20 2f 0a 2a 2a 0a 2a 2a 20 4e 6f        /.**.** No
25319 74 65 20 74 68 61 74 20 74 68 65 20 6c 6f 6f 70  te that the loop
2531a 73 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 6e  s might not be n
2531b 65 73 74 65 64 20 69 6e 20 74 68 65 20 6f 72 64  ested in the ord
2531c 65 72 20 69 6e 20 77 68 69 63 68 20 74 68 65 79  er in which they
2531d 0a 2a 2a 20 61 70 70 65 61 72 20 69 6e 20 74 68  .** appear in th
2531e 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 66  e FROM clause if
2531f 20 61 20 64 69 66 66 65 72 65 6e 74 20 6f 72 64   a different ord
25320 65 72 20 69 73 20 62 65 74 74 65 72 20 61 62 6c  er is better abl
25321 65 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 75 73 65  e to make.** use
25322 20 6f 66 20 69 6e 64 69 63 65 73 2e 20 20 4e 6f   of indices.  No
25323 74 65 20 61 6c 73 6f 20 74 68 61 74 20 77 68 65  te also that whe
25324 6e 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f  n the IN operato
25325 72 20 61 70 70 65 61 72 73 20 69 6e 0a 2a 2a 20  r appears in.** 
25326 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
25327 2c 20 69 74 20 6d 69 67 68 74 20 72 65 73 75 6c  , it might resul
25328 74 20 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20  t in additional 
25329 6e 65 73 74 65 64 20 6c 6f 6f 70 73 20 66 6f 72  nested loops for
2532a 0a 2a 2a 20 73 63 61 6e 6e 69 6e 67 20 74 68 72  .** scanning thr
2532b 6f 75 67 68 20 61 6c 6c 20 76 61 6c 75 65 73 20  ough all values 
2532c 6f 6e 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e  on the right-han
2532d 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 49 4e  d side of the IN
2532e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72  ..**.** There ar
2532f 65 20 42 74 72 65 65 20 63 75 72 73 6f 72 73 20  e Btree cursors 
25330 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
25331 65 61 63 68 20 74 61 62 6c 65 2e 20 20 74 31 20  each table.  t1 
25332 75 73 65 73 20 63 75 72 73 6f 72 0a 2a 2a 20 6e  uses cursor.** n
25333 75 6d 62 65 72 20 70 54 61 62 4c 69 73 74 2d 3e  umber pTabList->
25334 61 5b 30 5d 2e 69 43 75 72 73 6f 72 2e 20 20 74  a[0].iCursor.  t
25335 32 20 75 73 65 73 20 74 68 65 20 63 75 72 73 6f  2 uses the curso
25336 72 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 31 5d  r pTabList->a[1]
25337 2e 69 43 75 72 73 6f 72 2e 0a 2a 2a 20 41 6e 64  .iCursor..** And
25338 20 73 6f 20 66 6f 72 74 68 2e 20 20 54 68 69 73   so forth.  This
25339 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74   routine generat
2533a 65 73 20 63 6f 64 65 20 74 6f 20 6f 70 65 6e 20  es code to open 
2533b 74 68 6f 73 65 20 56 44 42 45 20 63 75 72 73 6f  those VDBE curso
2533c 72 73 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65  rs.** and sqlite
2533d 33 57 68 65 72 65 45 6e 64 28 29 20 67 65 6e 65  3WhereEnd() gene
2533e 72 61 74 65 73 20 74 68 65 20 63 6f 64 65 20 74  rates the code t
2533f 6f 20 63 6c 6f 73 65 20 74 68 65 6d 2e 0a 2a 2a  o close them..**
25340 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 68 61  .** The code tha
25341 74 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  t sqlite3WhereBe
25342 67 69 6e 28 29 20 67 65 6e 65 72 61 74 65 73 20  gin() generates 
25343 6c 65 61 76 65 73 20 74 68 65 20 63 75 72 73 6f  leaves the curso
25344 72 73 20 6e 61 6d 65 64 0a 2a 2a 20 69 6e 20 70  rs named.** in p
25345 54 61 62 4c 69 73 74 20 70 6f 69 6e 74 69 6e 67  TabList pointing
25346 20 61 74 20 74 68 65 69 72 20 61 70 70 72 6f 70   at their approp
25347 72 69 61 74 65 20 65 6e 74 72 69 65 73 2e 20 20  riate entries.  
25348 54 68 65 20 5b 2e 2e 2e 5d 20 63 6f 64 65 0a 2a  The [...] code.*
25349 2a 20 63 61 6e 20 75 73 65 20 4f 50 5f 43 6f 6c  * can use OP_Col
2534a 75 6d 6e 20 61 6e 64 20 4f 50 5f 52 6f 77 69 64  umn and OP_Rowid
2534b 20 6f 70 63 6f 64 65 73 20 6f 6e 20 74 68 65 73   opcodes on thes
2534c 65 20 63 75 72 73 6f 72 73 20 74 6f 20 65 78 74  e cursors to ext
2534d 72 61 63 74 0a 2a 2a 20 64 61 74 61 20 66 72 6f  ract.** data fro
2534e 6d 20 74 68 65 20 76 61 72 69 6f 75 73 20 74 61  m the various ta
2534f 62 6c 65 73 20 6f 66 20 74 68 65 20 6c 6f 6f 70  bles of the loop
25350 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 57  ..**.** If the W
25351 48 45 52 45 20 63 6c 61 75 73 65 20 69 73 20 65  HERE clause is e
25352 6d 70 74 79 2c 20 74 68 65 20 66 6f 72 65 61 63  mpty, the foreac
25353 68 20 6c 6f 6f 70 73 20 6d 75 73 74 20 65 61 63  h loops must eac
25354 68 20 73 63 61 6e 20 74 68 65 69 72 0a 2a 2a 20  h scan their.** 
25355 65 6e 74 69 72 65 20 74 61 62 6c 65 73 2e 20 20  entire tables.  
25356 54 68 75 73 20 61 20 74 68 72 65 65 2d 77 61 79  Thus a three-way
25357 20 6a 6f 69 6e 20 69 73 20 61 6e 20 4f 28 4e 5e   join is an O(N^
25358 33 29 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42  3) operation.  B
25359 75 74 20 69 66 0a 2a 2a 20 74 68 65 20 74 61 62  ut if.** the tab
2535a 6c 65 73 20 68 61 76 65 20 69 6e 64 69 63 65 73  les have indices
2535b 20 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 74   and there are t
2535c 65 72 6d 73 20 69 6e 20 74 68 65 20 57 48 45 52  erms in the WHER
2535d 45 20 63 6c 61 75 73 65 20 74 68 61 74 0a 2a 2a  E clause that.**
2535e 20 72 65 66 65 72 20 74 6f 20 74 68 6f 73 65 20   refer to those 
2535f 69 6e 64 69 63 65 73 2c 20 61 20 63 6f 6d 70 6c  indices, a compl
25360 65 74 65 20 74 61 62 6c 65 20 73 63 61 6e 20 63  ete table scan c
25361 61 6e 20 62 65 20 61 76 6f 69 64 65 64 20 61 6e  an be avoided an
25362 64 20 74 68 65 0a 2a 2a 20 63 6f 64 65 20 77 69  d the.** code wi
25363 6c 6c 20 72 75 6e 20 6d 75 63 68 20 66 61 73 74  ll run much fast
25364 65 72 2e 20 20 4d 6f 73 74 20 6f 66 20 74 68 65  er.  Most of the
25365 20 77 6f 72 6b 20 6f 66 20 74 68 69 73 20 72 6f   work of this ro
25366 75 74 69 6e 65 20 69 73 20 63 68 65 63 6b 69 6e  utine is checkin
25367 67 0a 2a 2a 20 74 6f 20 73 65 65 20 69 66 20 74  g.** to see if t
25368 68 65 72 65 20 61 72 65 20 69 6e 64 69 63 65 73  here are indices
25369 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65   that can be use
2536a 64 20 74 6f 20 73 70 65 65 64 20 75 70 20 74 68  d to speed up th
2536b 65 20 6c 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 65  e loop..**.** Te
2536c 72 6d 73 20 6f 66 20 74 68 65 20 57 48 45 52 45  rms of the WHERE
2536d 20 63 6c 61 75 73 65 20 61 72 65 20 61 6c 73 6f   clause are also
2536e 20 75 73 65 64 20 74 6f 20 6c 69 6d 69 74 20 77   used to limit w
2536f 68 69 63 68 20 72 6f 77 73 20 61 63 74 75 61 6c  hich rows actual
25370 6c 79 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 74 6f  ly.** make it to
25371 20 74 68 65 20 22 2e 2e 2e 22 20 69 6e 20 74 68   the "..." in th
25372 65 20 6d 69 64 64 6c 65 20 6f 66 20 74 68 65 20  e middle of the 
25373 6c 6f 6f 70 2e 20 20 41 66 74 65 72 20 65 61 63  loop.  After eac
25374 68 20 22 66 6f 72 65 61 63 68 22 2c 0a 2a 2a 20  h "foreach",.** 
25375 74 65 72 6d 73 20 6f 66 20 74 68 65 20 57 48 45  terms of the WHE
25376 52 45 20 63 6c 61 75 73 65 20 74 68 61 74 20 75  RE clause that u
25377 73 65 20 6f 6e 6c 79 20 74 65 72 6d 73 20 69 6e  se only terms in
25378 20 74 68 61 74 20 6c 6f 6f 70 20 61 6e 64 20 6f   that loop and o
25379 75 74 65 72 0a 2a 2a 20 6c 6f 6f 70 73 20 61 72  uter.** loops ar
2537a 65 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20  e evaluated and 
2537b 69 66 20 66 61 6c 73 65 20 61 20 6a 75 6d 70 20  if false a jump 
2537c 69 73 20 6d 61 64 65 20 61 72 6f 75 6e 64 20 61  is made around a
2537d 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a  ll subsequent.**
2537e 20 69 6e 6e 65 72 20 6c 6f 6f 70 73 20 28 6f 72   inner loops (or
2537f 20 61 72 6f 75 6e 64 20 74 68 65 20 22 2e 2e 2e   around the "...
25380 22 20 69 66 20 74 68 65 20 74 65 73 74 20 6f 63  " if the test oc
25381 63 75 72 73 20 77 69 74 68 69 6e 20 74 68 65 20  curs within the 
25382 69 6e 6e 65 72 2d 0a 2a 2a 20 6d 6f 73 74 20 6c  inner-.** most l
25383 6f 6f 70 29 0a 2a 2a 0a 2a 2a 20 4f 55 54 45 52  oop).**.** OUTER
25384 20 4a 4f 49 4e 53 0a 2a 2a 0a 2a 2a 20 41 6e 20   JOINS.**.** An 
25385 6f 75 74 65 72 20 6a 6f 69 6e 20 6f 66 20 74 61  outer join of ta
25386 62 6c 65 73 20 74 31 20 61 6e 64 20 74 32 20 69  bles t1 and t2 i
25387 73 20 63 6f 6e 63 65 70 74 61 6c 6c 79 20 63 6f  s conceptally co
25388 64 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  ded as follows:.
25389 2a 2a 0a 2a 2a 20 20 20 20 66 6f 72 65 61 63 68  **.**    foreach
2538a 20 72 6f 77 31 20 69 6e 20 74 31 20 64 6f 0a 2a   row1 in t1 do.*
2538b 2a 20 20 20 20 20 20 66 6c 61 67 20 3d 20 30 0a  *      flag = 0.
2538c 2a 2a 20 20 20 20 20 20 66 6f 72 65 61 63 68 20  **      foreach 
2538d 72 6f 77 32 20 69 6e 20 74 32 20 64 6f 0a 2a 2a  row2 in t2 do.**
2538e 20 20 20 20 20 20 20 20 73 74 61 72 74 3a 0a 2a          start:.*
2538f 2a 20 20 20 20 20 20 20 20 20 20 2e 2e 2e 0a 2a  *          ....*
25390 2a 20 20 20 20 20 20 20 20 20 20 66 6c 61 67 20  *          flag 
25391 3d 20 31 0a 2a 2a 20 20 20 20 20 20 65 6e 64 0a  = 1.**      end.
25392 2a 2a 20 20 20 20 20 20 69 66 20 66 6c 61 67 3d  **      if flag=
25393 3d 30 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20  =0 then.**      
25394 20 20 6d 6f 76 65 20 74 68 65 20 72 6f 77 32 20    move the row2 
25395 63 75 72 73 6f 72 20 74 6f 20 61 20 6e 75 6c 6c  cursor to a null
25396 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 67   row.**        g
25397 6f 74 6f 20 73 74 61 72 74 0a 2a 2a 20 20 20 20  oto start.**    
25398 20 20 66 69 0a 2a 2a 20 20 20 20 65 6e 64 0a 2a    fi.**    end.*
25399 2a 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 43 4c  *.** ORDER BY CL
2539a 41 55 53 45 20 50 52 4f 43 45 53 53 49 4e 47 0a  AUSE PROCESSING.
2539b 2a 2a 0a 2a 2a 20 2a 70 70 4f 72 64 65 72 42 79  **.** *ppOrderBy
2539c 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
2539d 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
2539e 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54  ause of a SELECT
2539f 20 73 74 61 74 65 6d 65 6e 74 2c 0a 2a 2a 20 69   statement,.** i
253a0 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 2e 20  f there is one. 
253a1 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
253a2 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
253a3 6f 72 20 69 66 20 74 68 69 73 20 72 6f 75 74 69  or if this routi
253a4 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20  ne.** is called 
253a5 66 72 6f 6d 20 61 6e 20 55 50 44 41 54 45 20 6f  from an UPDATE o
253a6 72 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65  r DELETE stateme
253a7 6e 74 2c 20 74 68 65 6e 20 70 70 4f 72 64 65 72  nt, then ppOrder
253a8 42 79 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  By is NULL..**.*
253a9 2a 20 49 66 20 61 6e 20 69 6e 64 65 78 20 63 61  * If an index ca
253aa 6e 20 62 65 20 75 73 65 64 20 73 6f 20 74 68 61  n be used so tha
253ab 74 20 74 68 65 20 6e 61 74 75 72 61 6c 20 6f 75  t the natural ou
253ac 74 70 75 74 20 6f 72 64 65 72 20 6f 66 20 74 68  tput order of th
253ad 65 20 74 61 62 6c 65 0a 2a 2a 20 73 63 61 6e 20  e table.** scan 
253ae 69 73 20 63 6f 72 72 65 63 74 20 66 6f 72 20 74  is correct for t
253af 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
253b0 73 65 2c 20 74 68 65 6e 20 74 68 61 74 20 69 6e  se, then that in
253b1 64 65 78 20 69 73 20 75 73 65 64 20 61 6e 64 0a  dex is used and.
253b2 2a 2a 20 2a 70 70 4f 72 64 65 72 42 79 20 69 73  ** *ppOrderBy is
253b3 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 20 54   set to NULL.  T
253b4 68 69 73 20 69 73 20 61 6e 20 6f 70 74 69 6d 69  his is an optimi
253b5 7a 61 74 69 6f 6e 20 74 68 61 74 20 70 72 65 76  zation that prev
253b6 65 6e 74 73 20 61 6e 0a 2a 2a 20 75 6e 6e 65 63  ents an.** unnec
253b7 65 73 73 61 72 79 20 73 6f 72 74 20 6f 66 20 74  essary sort of t
253b8 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 69 66  he result set if
253b9 20 61 6e 20 69 6e 64 65 78 20 61 70 70 72 6f 70   an index approp
253ba 72 69 61 74 65 20 66 6f 72 20 74 68 65 0a 2a 2a  riate for the.**
253bb 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
253bc 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e   already exists.
253bd 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 77 68  .**.** If the wh
253be 65 72 65 20 63 6c 61 75 73 65 20 6c 6f 6f 70 73  ere clause loops
253bf 20 63 61 6e 6e 6f 74 20 62 65 20 61 72 72 61 6e   cannot be arran
253c0 67 65 64 20 74 6f 20 70 72 6f 76 69 64 65 20 74  ged to provide t
253c1 68 65 20 63 6f 72 72 65 63 74 0a 2a 2a 20 6f 75  he correct.** ou
253c2 74 70 75 74 20 6f 72 64 65 72 2c 20 74 68 65 6e  tput order, then
253c3 20 74 68 65 20 2a 70 70 4f 72 64 65 72 42 79 20   the *ppOrderBy 
253c4 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f  is unchanged..*/
253c5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
253c6 57 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69 74  WhereInfo *sqlit
253c7 65 33 57 68 65 72 65 42 65 67 69 6e 28 0a 20 20  e3WhereBegin(.  
253c8 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
253c9 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
253ca 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
253cb 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69   SrcList *pTabLi
253cc 73 74 2c 20 20 20 20 2f 2a 20 41 20 6c 69 73 74  st,    /* A list
253cd 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 74   of all tables t
253ce 6f 20 62 65 20 73 63 61 6e 6e 65 64 20 2a 2f 0a  o be scanned */.
253cf 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c 20    Expr *pWhere, 
253d0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57          /* The W
253d1 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
253d2 20 45 78 70 72 4c 69 73 74 20 2a 2a 70 70 4f 72   ExprList **ppOr
253d3 64 65 72 42 79 2c 20 2f 2a 20 41 6e 20 4f 52 44  derBy, /* An ORD
253d4 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20 6f 72  ER BY clause, or
253d5 20 4e 55 4c 4c 20 2a 2f 0a 20 20 75 38 20 77 66   NULL */.  u8 wf
253d6 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20  lags            
253d7 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 57   /* One of the W
253d8 48 45 52 45 5f 2a 20 66 6c 61 67 73 20 64 65 66  HERE_* flags def
253d9 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 49 6e  ined in sqliteIn
253da 74 2e 68 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  t.h */.){.  int 
253db 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
253dc 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
253dd 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 57 68 65 72  ounter */.  Wher
253de 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20  eInfo *pWInfo;  
253df 20 20 20 20 20 20 20 2f 2a 20 57 69 6c 6c 20 62         /* Will b
253e0 65 63 6f 6d 65 20 74 68 65 20 72 65 74 75 72 6e  ecome the return
253e1 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 66   value of this f
253e2 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 56 64 62  unction */.  Vdb
253e3 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
253e4 56 64 62 65 3b 20 20 20 2f 2a 20 54 68 65 20 76  Vdbe;   /* The v
253e5 69 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20  irtual database 
253e6 65 6e 67 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20  engine */.  int 
253e7 62 72 6b 2c 20 63 6f 6e 74 20 3d 20 30 3b 20 20  brk, cont = 0;  
253e8 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
253e9 73 65 73 20 75 73 65 64 20 64 75 72 69 6e 67 20  ses used during 
253ea 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6f 6e 20  code generation 
253eb 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74  */.  Bitmask not
253ec 52 65 61 64 79 3b 20 20 20 20 20 20 20 20 20 20  Ready;          
253ed 2f 2a 20 43 75 72 73 6f 72 73 20 74 68 61 74 20  /* Cursors that 
253ee 61 72 65 20 6e 6f 74 20 79 65 74 20 70 6f 73 69  are not yet posi
253ef 74 69 6f 6e 65 64 20 2a 2f 0a 20 20 57 68 65 72  tioned */.  Wher
253f0 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20  eTerm *pTerm;   
253f1 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67         /* A sing
253f2 6c 65 20 74 65 72 6d 20 69 6e 20 74 68 65 20 57  le term in the W
253f3 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
253f4 20 45 78 70 72 4d 61 73 6b 53 65 74 20 6d 61 73   ExprMaskSet mas
253f5 6b 53 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 54  kSet;       /* T
253f6 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 61  he expression ma
253f7 73 6b 20 73 65 74 20 2a 2f 0a 20 20 57 68 65 72  sk set */.  Wher
253f8 65 43 6c 61 75 73 65 20 77 63 3b 20 20 20 20 20  eClause wc;     
253f9 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48         /* The WH
253fa 45 52 45 20 63 6c 61 75 73 65 20 69 73 20 64 69  ERE clause is di
253fb 76 69 64 65 64 20 69 6e 74 6f 20 74 68 65 73 65  vided into these
253fc 20 74 65 72 6d 73 20 2a 2f 0a 20 20 73 74 72 75   terms */.  stru
253fd 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
253fe 2a 70 54 61 62 49 74 65 6d 3b 20 20 2f 2a 20 41  *pTabItem;  /* A
253ff 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 66 72   single entry fr
25400 6f 6d 20 70 54 61 62 4c 69 73 74 20 2a 2f 0a 20  om pTabList */. 
25401 20 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65   WhereLevel *pLe
25402 76 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  vel;            
25403 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 6c 65 76   /* A single lev
25404 65 6c 20 69 6e 20 74 68 65 20 70 57 49 6e 66 6f  el in the pWInfo
25405 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69   list */.  int i
25406 46 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20  From;           
25407 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
25408 72 73 74 20 75 6e 75 73 65 64 20 46 52 4f 4d 20  rst unused FROM 
25409 63 6c 61 75 73 65 20 65 6c 65 6d 65 6e 74 20 2a  clause element *
2540a 2f 0a 20 20 69 6e 74 20 61 6e 64 46 6c 61 67 73  /.  int andFlags
2540b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2540c 2a 20 41 4e 44 2d 65 64 20 63 6f 6d 62 69 6e 61  * AND-ed combina
2540d 74 69 6f 6e 20 6f 66 20 61 6c 6c 20 77 63 2e 61  tion of all wc.a
2540e 5b 5d 2e 66 6c 61 67 73 20 2a 2f 0a 20 20 73 71  [].flags */.  sq
2540f 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
25410 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
25411 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
25412 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
25413 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 0a 20 20  OrderBy = 0;..  
25414 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66  /* The number of
25415 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 46   tables in the F
25416 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 6c 69  ROM clause is li
25417 6d 69 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d  mited by the num
25418 62 65 72 20 6f 66 0a 20 20 2a 2a 20 62 69 74 73  ber of.  ** bits
25419 20 69 6e 20 61 20 42 69 74 6d 61 73 6b 20 0a 20   in a Bitmask . 
2541a 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 4c 69   */.  if( pTabLi
2541b 73 74 2d 3e 6e 53 72 63 3e 42 4d 53 20 29 7b 0a  st->nSrc>BMS ){.
2541c 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2541d 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 74 20  Msg(pParse, "at 
2541e 6d 6f 73 74 20 25 64 20 74 61 62 6c 65 73 20 69  most %d tables i
2541f 6e 20 61 20 6a 6f 69 6e 22 2c 20 42 4d 53 29 3b  n a join", BMS);
25420 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
25421 20 7d 0a 0a 20 20 69 66 28 20 70 70 4f 72 64 65   }..  if( ppOrde
25422 72 42 79 20 29 7b 0a 20 20 20 20 70 4f 72 64 65  rBy ){.    pOrde
25423 72 42 79 20 3d 20 2a 70 70 4f 72 64 65 72 42 79  rBy = *ppOrderBy
25424 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 70 6c 69  ;.  }..  /* Spli
25425 74 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  t the WHERE clau
25426 73 65 20 69 6e 74 6f 20 73 65 70 61 72 61 74 65  se into separate
25427 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20   subexpressions 
25428 77 68 65 72 65 20 65 61 63 68 0a 20 20 2a 2a 20  where each.  ** 
25429 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 69 73  subexpression is
2542a 20 73 65 70 61 72 61 74 65 64 20 62 79 20 61 6e   separated by an
2542b 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 0a 20   AND operator.. 
2542c 20 2a 2f 0a 20 20 69 6e 69 74 4d 61 73 6b 53 65   */.  initMaskSe
2542d 74 28 26 6d 61 73 6b 53 65 74 29 3b 0a 20 20 77  t(&maskSet);.  w
2542e 68 65 72 65 43 6c 61 75 73 65 49 6e 69 74 28 26  hereClauseInit(&
2542f 77 63 2c 20 70 50 61 72 73 65 2c 20 26 6d 61 73  wc, pParse, &mas
25430 6b 53 65 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  kSet);.  sqlite3
25431 45 78 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74  ExprCodeConstant
25432 73 28 70 50 61 72 73 65 2c 20 70 57 68 65 72 65  s(pParse, pWhere
25433 29 3b 0a 20 20 77 68 65 72 65 53 70 6c 69 74 28  );.  whereSplit(
25434 26 77 63 2c 20 70 57 68 65 72 65 2c 20 54 4b 5f  &wc, pWhere, TK_
25435 41 4e 44 29 3b 0a 20 20 20 20 0a 20 20 2f 2a 20  AND);.    .  /* 
25436 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 69 6e 69  Allocate and ini
25437 74 69 61 6c 69 7a 65 20 74 68 65 20 57 68 65 72  tialize the Wher
25438 65 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  eInfo structure 
25439 74 68 61 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65  that will become
2543a 20 74 68 65 0a 20 20 2a 2a 20 72 65 74 75 72 6e   the.  ** return
2543b 20 76 61 6c 75 65 2e 0a 20 20 2a 2f 0a 20 20 64   value..  */.  d
2543c 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2543d 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74    pWInfo = sqlit
2543e 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
2543f 62 2c 20 20 0a 20 20 20 20 20 20 20 20 20 20 20  b,  .           
25440 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f             sizeo
25441 66 28 57 68 65 72 65 49 6e 66 6f 29 20 2b 20 70  f(WhereInfo) + p
25442 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 2a 73 69  TabList->nSrc*si
25443 7a 65 6f 66 28 57 68 65 72 65 4c 65 76 65 6c 29  zeof(WhereLevel)
25444 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  );.  if( db->mal
25445 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
25446 20 67 6f 74 6f 20 77 68 65 72 65 42 65 67 69 6e   goto whereBegin
25447 4e 6f 4d 65 6d 3b 0a 20 20 7d 0a 20 20 70 57 49  NoMem;.  }.  pWI
25448 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 20 3d 20 70 54  nfo->nLevel = pT
25449 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 0a 20 20  abList->nSrc;.  
2544a 70 57 49 6e 66 6f 2d 3e 70 50 61 72 73 65 20 3d  pWInfo->pParse =
2544b 20 70 50 61 72 73 65 3b 0a 20 20 70 57 49 6e 66   pParse;.  pWInf
2544c 6f 2d 3e 70 54 61 62 4c 69 73 74 20 3d 20 70 54  o->pTabList = pT
2544d 61 62 4c 69 73 74 3b 0a 20 20 70 57 49 6e 66 6f  abList;.  pWInfo
2544e 2d 3e 69 42 72 65 61 6b 20 3d 20 73 71 6c 69 74  ->iBreak = sqlit
2544f 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
25450 76 29 3b 0a 0a 20 20 2f 2a 20 53 70 65 63 69 61  v);..  /* Specia
25451 6c 20 63 61 73 65 3a 20 61 20 57 48 45 52 45 20  l case: a WHERE 
25452 63 6c 61 75 73 65 20 74 68 61 74 20 69 73 20 63  clause that is c
25453 6f 6e 73 74 61 6e 74 2e 20 20 45 76 61 6c 75 61  onstant.  Evalua
25454 74 65 20 74 68 65 0a 20 20 2a 2a 20 65 78 70 72  te the.  ** expr
25455 65 73 73 69 6f 6e 20 61 6e 64 20 65 69 74 68 65  ession and eithe
25456 72 20 6a 75 6d 70 20 6f 76 65 72 20 61 6c 6c 20  r jump over all 
25457 6f 66 20 74 68 65 20 63 6f 64 65 20 6f 72 20 66  of the code or f
25458 61 6c 6c 20 74 68 72 75 2e 0a 20 20 2a 2f 0a 20  all thru..  */. 
25459 20 69 66 28 20 70 57 68 65 72 65 20 26 26 20 28   if( pWhere && (
2545a 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d  pTabList->nSrc==
2545b 30 20 7c 7c 20 73 71 6c 69 74 65 33 45 78 70 72  0 || sqlite3Expr
2545c 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69  IsConstantNotJoi
2545d 6e 28 70 57 68 65 72 65 29 29 20 29 7b 0a 20 20  n(pWhere)) ){.  
2545e 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46    sqlite3ExprIfF
2545f 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 57 68  alse(pParse, pWh
25460 65 72 65 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72  ere, pWInfo->iBr
25461 65 61 6b 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50  eak, SQLITE_JUMP
25462 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 70 57 68  IFNULL);.    pWh
25463 65 72 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  ere = 0;.  }..  
25464 2f 2a 20 41 73 73 69 67 6e 20 61 20 62 69 74 20  /* Assign a bit 
25465 66 72 6f 6d 20 74 68 65 20 62 69 74 6d 61 73 6b  from the bitmask
25466 20 74 6f 20 65 76 65 72 79 20 74 65 72 6d 20 69   to every term i
25467 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
25468 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65  e..  **.  ** Whe
25469 6e 20 61 73 73 69 67 6e 69 6e 67 20 62 69 74 6d  n assigning bitm
2546a 61 73 6b 20 76 61 6c 75 65 73 20 74 6f 20 46 52  ask values to FR
2546b 4f 4d 20 63 6c 61 75 73 65 20 63 75 72 73 6f 72  OM clause cursor
2546c 73 2c 20 69 74 20 6d 75 73 74 20 62 65 0a 20 20  s, it must be.  
2546d 2a 2a 20 74 68 65 20 63 61 73 65 20 74 68 61 74  ** the case that
2546e 20 69 66 20 58 20 69 73 20 74 68 65 20 62 69 74   if X is the bit
2546f 6d 61 73 6b 20 66 6f 72 20 74 68 65 20 4e 2d 74  mask for the N-t
25470 68 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65  h FROM clause te
25471 72 6d 20 74 68 65 6e 0a 20 20 2a 2a 20 74 68 65  rm then.  ** the
25472 20 62 69 74 6d 61 73 6b 20 66 6f 72 20 61 6c 6c   bitmask for all
25473 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72   FROM clause ter
25474 6d 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f  ms to the left o
25475 66 20 74 68 65 20 4e 2d 74 68 20 74 65 72 6d 0a  f the N-th term.
25476 20 20 2a 2a 20 69 73 20 28 58 2d 31 29 2e 20 20    ** is (X-1).  
25477 20 41 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 66   An expression f
25478 72 6f 6d 20 74 68 65 20 4f 4e 20 63 6c 61 75 73  rom the ON claus
25479 65 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e  e of a LEFT JOIN
2547a 20 63 61 6e 20 75 73 65 0a 20 20 2a 2a 20 69 74   can use.  ** it
2547b 73 20 45 78 70 72 2e 69 52 69 67 68 74 4a 6f 69  s Expr.iRightJoi
2547c 6e 54 61 62 6c 65 20 76 61 6c 75 65 20 74 6f 20  nTable value to 
2547d 66 69 6e 64 20 74 68 65 20 62 69 74 6d 61 73 6b  find the bitmask
2547e 20 6f 66 20 74 68 65 20 72 69 67 68 74 20 74 61   of the right ta
2547f 62 6c 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  ble.  ** of the 
25480 6a 6f 69 6e 2e 20 20 53 75 62 74 72 61 63 74 69  join.  Subtracti
25481 6e 67 20 6f 6e 65 20 66 72 6f 6d 20 74 68 65 20  ng one from the 
25482 72 69 67 68 74 20 74 61 62 6c 65 20 62 69 74 6d  right table bitm
25483 61 73 6b 20 67 69 76 65 73 20 61 0a 20 20 2a 2a  ask gives a.  **
25484 20 62 69 74 6d 61 73 6b 20 66 6f 72 20 61 6c 6c   bitmask for all
25485 20 74 61 62 6c 65 73 20 74 6f 20 74 68 65 20 6c   tables to the l
25486 65 66 74 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e  eft of the join.
25487 20 20 4b 6e 6f 77 69 6e 67 20 74 68 65 20 62 69    Knowing the bi
25488 74 6d 61 73 6b 0a 20 20 2a 2a 20 66 6f 72 20 61  tmask.  ** for a
25489 6c 6c 20 74 61 62 6c 65 73 20 74 6f 20 74 68 65  ll tables to the
2548a 20 6c 65 66 74 20 6f 66 20 61 20 6c 65 66 74 20   left of a left 
2548b 6a 6f 69 6e 20 69 73 20 69 6d 70 6f 72 74 61 6e  join is importan
2548c 74 2e 20 20 54 69 63 6b 65 74 20 23 33 30 31 35  t.  Ticket #3015
2548d 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  ..  */.  for(i=0
2548e 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  ; i<pTabList->nS
2548f 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 72  rc; i++){.    cr
25490 65 61 74 65 4d 61 73 6b 28 26 6d 61 73 6b 53 65  eateMask(&maskSe
25491 74 2c 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69  t, pTabList->a[i
25492 5d 2e 69 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a  ].iCursor);.  }.
25493 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
25494 20 7b 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 74   {.    Bitmask t
25495 6f 54 68 65 4c 65 66 74 20 3d 20 30 3b 0a 20 20  oTheLeft = 0;.  
25496 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61    for(i=0; i<pTa
25497 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  bList->nSrc; i++
25498 29 7b 0a 20 20 20 20 20 20 42 69 74 6d 61 73 6b  ){.      Bitmask
25499 20 6d 20 3d 20 67 65 74 4d 61 73 6b 28 26 6d 61   m = getMask(&ma
2549a 73 6b 53 65 74 2c 20 70 54 61 62 4c 69 73 74 2d  skSet, pTabList-
2549b 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 29 3b 0a  >a[i].iCursor);.
2549c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 6d        assert( (m
2549d 2d 31 29 3d 3d 74 6f 54 68 65 4c 65 66 74 20 29  -1)==toTheLeft )
2549e 3b 0a 20 20 20 20 20 20 74 6f 54 68 65 4c 65 66  ;.      toTheLef
2549f 74 20 7c 3d 20 6d 3b 0a 20 20 20 20 7d 0a 20 20  t |= m;.    }.  
254a0 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41  }.#endif..  /* A
254a1 6e 61 6c 79 7a 65 20 61 6c 6c 20 6f 66 20 74 68  nalyze all of th
254a2 65 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73  e subexpressions
254a3 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 65 78 70  .  Note that exp
254a4 72 41 6e 61 6c 79 7a 65 28 29 20 6d 69 67 68 74  rAnalyze() might
254a5 0a 20 20 2a 2a 20 61 64 64 20 6e 65 77 20 76 69  .  ** add new vi
254a6 72 74 75 61 6c 20 74 65 72 6d 73 20 6f 6e 74 6f  rtual terms onto
254a7 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
254a8 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 57  WHERE clause.  W
254a9 65 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 77 61  e do not.  ** wa
254aa 6e 74 20 74 6f 20 61 6e 61 6c 79 7a 65 20 74 68  nt to analyze th
254ab 65 73 65 20 76 69 72 74 75 61 6c 20 74 65 72 6d  ese virtual term
254ac 73 2c 20 73 6f 20 73 74 61 72 74 20 61 6e 61 6c  s, so start anal
254ad 79 7a 69 6e 67 20 61 74 20 74 68 65 20 65 6e 64  yzing at the end
254ae 0a 20 20 2a 2a 20 61 6e 64 20 77 6f 72 6b 20 66  .  ** and work f
254af 6f 72 77 61 72 64 20 73 6f 20 74 68 61 74 20 74  orward so that t
254b0 68 65 20 61 64 64 65 64 20 76 69 72 74 75 61 6c  he added virtual
254b1 20 74 65 72 6d 73 20 61 72 65 20 6e 65 76 65 72   terms are never
254b2 20 70 72 6f 63 65 73 73 65 64 2e 0a 20 20 2a 2f   processed..  */
254b3 0a 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c  .  exprAnalyzeAl
254b4 6c 28 70 54 61 62 4c 69 73 74 2c 20 26 77 63 29  l(pTabList, &wc)
254b5 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  ;.  if( db->mall
254b6 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
254b7 67 6f 74 6f 20 77 68 65 72 65 42 65 67 69 6e 4e  goto whereBeginN
254b8 6f 4d 65 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  oMem;.  }..  /* 
254b9 43 68 6f 73 65 20 74 68 65 20 62 65 73 74 20 69  Chose the best i
254ba 6e 64 65 78 20 74 6f 20 75 73 65 20 66 6f 72 20  ndex to use for 
254bb 65 61 63 68 20 74 61 62 6c 65 20 69 6e 20 74 68  each table in th
254bc 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 20  e FROM clause.. 
254bd 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 6c 6f   **.  ** This lo
254be 6f 70 20 66 69 6c 6c 73 20 69 6e 20 74 68 65 20  op fills in the 
254bf 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 73  following fields
254c0 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 70 57  :.  **.  **   pW
254c1 49 6e 66 6f 2d 3e 61 5b 5d 2e 70 49 64 78 20 20  Info->a[].pIdx  
254c2 20 20 20 20 54 68 65 20 69 6e 64 65 78 20 74 6f      The index to
254c3 20 75 73 65 20 66 6f 72 20 74 68 69 73 20 6c 65   use for this le
254c4 76 65 6c 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e  vel of the loop.
254c5 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e  .  **   pWInfo->
254c6 61 5b 5d 2e 66 6c 61 67 73 20 20 20 20 20 57 48  a[].flags     WH
254c7 45 52 45 5f 78 78 78 20 66 6c 61 67 73 20 61 73  ERE_xxx flags as
254c8 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 49  sociated with pI
254c9 64 78 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f  dx.  **   pWInfo
254ca 2d 3e 61 5b 5d 2e 6e 45 71 20 20 20 20 20 20 20  ->a[].nEq       
254cb 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 3d 3d  The number of ==
254cc 20 61 6e 64 20 49 4e 20 63 6f 6e 73 74 72 61 69   and IN constrai
254cd 6e 74 73 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66  nts.  **   pWInf
254ce 6f 2d 3e 61 5b 5d 2e 69 46 72 6f 6d 20 20 20 20  o->a[].iFrom    
254cf 20 57 68 65 6e 20 74 65 72 6d 20 6f 66 20 74 68   When term of th
254d0 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73  e FROM clause is
254d1 20 62 65 69 6e 67 20 63 6f 64 65 64 0a 20 20 2a   being coded.  *
254d2 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e  *   pWInfo->a[].
254d3 69 54 61 62 43 75 72 20 20 20 54 68 65 20 56 44  iTabCur   The VD
254d4 42 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68  BE cursor for th
254d5 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  e database table
254d6 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e  .  **   pWInfo->
254d7 61 5b 5d 2e 69 49 64 78 43 75 72 20 20 20 54 68  a[].iIdxCur   Th
254d8 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 66 6f  e VDBE cursor fo
254d9 72 20 74 68 65 20 69 6e 64 65 78 0a 20 20 2a 2a  r the index.  **
254da 0a 20 20 2a 2a 20 54 68 69 73 20 6c 6f 6f 70 20  .  ** This loop 
254db 61 6c 73 6f 20 66 69 67 75 72 65 73 20 6f 75 74  also figures out
254dc 20 74 68 65 20 6e 65 73 74 69 6e 67 20 6f 72 64   the nesting ord
254dd 65 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20  er of tables in 
254de 74 68 65 20 46 52 4f 4d 0a 20 20 2a 2a 20 63 6c  the FROM.  ** cl
254df 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 6e 6f 74  ause..  */.  not
254e0 52 65 61 64 79 20 3d 20 7e 28 42 69 74 6d 61 73  Ready = ~(Bitmas
254e1 6b 29 30 3b 0a 20 20 70 54 61 62 49 74 65 6d 20  k)0;.  pTabItem 
254e2 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 3b 0a 20  = pTabList->a;. 
254e3 20 70 4c 65 76 65 6c 20 3d 20 70 57 49 6e 66 6f   pLevel = pWInfo
254e4 2d 3e 61 3b 0a 20 20 61 6e 64 46 6c 61 67 73 20  ->a;.  andFlags 
254e5 3d 20 7e 30 3b 0a 20 20 57 48 45 52 45 54 52 41  = ~0;.  WHERETRA
254e6 43 45 28 28 22 2a 2a 2a 20 4f 70 74 69 6d 69 7a  CE(("*** Optimiz
254e7 65 72 20 53 74 61 72 74 20 2a 2a 2a 5c 6e 22 29  er Start ***\n")
254e8 29 3b 0a 20 20 66 6f 72 28 69 3d 69 46 72 6f 6d  );.  for(i=iFrom
254e9 3d 30 2c 20 70 4c 65 76 65 6c 3d 70 57 49 6e 66  =0, pLevel=pWInf
254ea 6f 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73 74  o->a; i<pTabList
254eb 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 4c 65  ->nSrc; i++, pLe
254ec 76 65 6c 2b 2b 29 7b 0a 20 20 20 20 49 6e 64 65  vel++){.    Inde
254ed 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20  x *pIdx;        
254ee 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
254ef 20 66 6f 72 20 46 52 4f 4d 20 74 61 62 6c 65 20   for FROM table 
254f0 61 74 20 70 54 61 62 49 74 65 6d 20 2a 2f 0a 20  at pTabItem */. 
254f1 20 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20     int flags;   
254f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
254f3 2a 20 46 6c 61 67 73 20 61 73 73 73 6f 63 69 61  * Flags asssocia
254f4 74 65 64 20 77 69 74 68 20 70 49 64 78 20 2a 2f  ted with pIdx */
254f5 0a 20 20 20 20 69 6e 74 20 6e 45 71 3b 20 20 20  .    int nEq;   
254f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
254f7 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 3d 3d   /* Number of ==
254f8 20 6f 72 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e   or IN constrain
254f9 74 73 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65  ts */.    double
254fa 20 63 6f 73 74 3b 20 20 20 20 20 20 20 20 20 20   cost;          
254fb 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 73        /* The cos
254fc 74 20 66 6f 72 20 70 49 64 78 20 2a 2f 0a 20 20  t for pIdx */.  
254fd 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 20    int j;        
254fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
254ff 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65   For looping ove
25500 72 20 46 52 4f 4d 20 74 61 62 6c 65 73 20 2a 2f  r FROM tables */
25501 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 42 65 73  .    Index *pBes
25502 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  t = 0;          
25503 20 2f 2a 20 54 68 65 20 62 65 73 74 20 69 6e 64   /* The best ind
25504 65 78 20 73 65 65 6e 20 73 6f 20 66 61 72 20 2a  ex seen so far *
25505 2f 0a 20 20 20 20 69 6e 74 20 62 65 73 74 46 6c  /.    int bestFl
25506 61 67 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ags = 0;        
25507 20 20 2f 2a 20 46 6c 61 67 73 20 61 73 73 6f 63    /* Flags assoc
25508 69 61 74 65 64 20 77 69 74 68 20 70 42 65 73 74  iated with pBest
25509 20 2a 2f 0a 20 20 20 20 69 6e 74 20 62 65 73 74   */.    int best
2550a 4e 45 71 20 3d 20 30 3b 20 20 20 20 20 20 20 20  NEq = 0;        
2550b 20 20 20 20 2f 2a 20 6e 45 71 20 61 73 73 6f 63      /* nEq assoc
2550c 69 61 74 65 64 20 77 69 74 68 20 70 42 65 73 74  iated with pBest
2550d 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 6c   */.    double l
2550e 6f 77 65 73 74 43 6f 73 74 3b 20 20 20 20 20 20  owestCost;      
2550f 20 20 20 20 2f 2a 20 43 6f 73 74 20 6f 66 20 74      /* Cost of t
25510 68 65 20 70 42 65 73 74 20 2a 2f 0a 20 20 20 20  he pBest */.    
25511 69 6e 74 20 62 65 73 74 4a 20 3d 20 30 3b 20 20  int bestJ = 0;  
25512 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
25513 68 65 20 76 61 6c 75 65 20 6f 66 20 6a 20 2a 2f  he value of j */
25514 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 6d 3b 20  .    Bitmask m; 
25515 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25516 20 2f 2a 20 42 69 74 6d 61 73 6b 20 76 61 6c 75   /* Bitmask valu
25517 65 20 66 6f 72 20 6a 20 6f 72 20 62 65 73 74 4a  e for j or bestJ
25518 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6f 6e 63 65   */.    int once
25519 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
2551a 20 20 20 20 2f 2a 20 54 72 75 65 20 77 68 65 6e      /* True when
2551b 20 66 69 72 73 74 20 74 61 62 6c 65 20 69 73 20   first table is 
2551c 73 65 65 6e 20 2a 2f 0a 20 20 20 20 73 71 6c 69  seen */.    sqli
2551d 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
2551e 70 49 6e 64 65 78 3b 20 2f 2a 20 43 75 72 72 65  pIndex; /* Curre
2551f 6e 74 20 76 69 72 74 75 61 6c 20 69 6e 64 65 78  nt virtual index
25520 20 2a 2f 0a 0a 20 20 20 20 6c 6f 77 65 73 74 43   */..    lowestC
25521 6f 73 74 20 3d 20 53 51 4c 49 54 45 5f 42 49 47  ost = SQLITE_BIG
25522 5f 44 42 4c 3b 0a 20 20 20 20 66 6f 72 28 6a 3d  _DBL;.    for(j=
25523 69 46 72 6f 6d 2c 20 70 54 61 62 49 74 65 6d 3d  iFrom, pTabItem=
25524 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d 3b  &pTabList->a[j];
25525 20 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72   j<pTabList->nSr
25526 63 3b 20 6a 2b 2b 2c 20 70 54 61 62 49 74 65 6d  c; j++, pTabItem
25527 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 64  ++){.      int d
25528 6f 4e 6f 74 52 65 6f 72 64 65 72 3b 20 20 2f 2a  oNotReorder;  /*
25529 20 54 72 75 65 20 69 66 20 74 68 69 73 20 74 61   True if this ta
2552a 62 6c 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  ble should not b
2552b 65 20 72 65 6f 72 64 65 72 65 64 20 2a 2f 0a 0a  e reordered */..
2552c 20 20 20 20 20 20 64 6f 4e 6f 74 52 65 6f 72 64        doNotReord
2552d 65 72 20 3d 20 20 28 70 54 61 62 49 74 65 6d 2d  er =  (pTabItem-
2552e 3e 6a 6f 69 6e 74 79 70 65 20 26 20 28 4a 54 5f  >jointype & (JT_
2552f 4c 45 46 54 7c 4a 54 5f 43 52 4f 53 53 29 29 21  LEFT|JT_CROSS))!
25530 3d 30 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 6e  =0;.      if( on
25531 63 65 20 26 26 20 64 6f 4e 6f 74 52 65 6f 72 64  ce && doNotReord
25532 65 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  er ) break;.    
25533 20 20 6d 20 3d 20 67 65 74 4d 61 73 6b 28 26 6d    m = getMask(&m
25534 61 73 6b 53 65 74 2c 20 70 54 61 62 49 74 65 6d  askSet, pTabItem
25535 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  ->iCursor);.    
25536 20 20 69 66 28 20 28 6d 20 26 20 6e 6f 74 52 65    if( (m & notRe
25537 61 64 79 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ady)==0 ){.     
25538 20 20 20 69 66 28 20 6a 3d 3d 69 46 72 6f 6d 20     if( j==iFrom 
25539 29 20 69 46 72 6f 6d 2b 2b 3b 0a 20 20 20 20 20  ) iFrom++;.     
2553a 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
2553b 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72     }.      asser
2553c 74 28 20 70 54 61 62 49 74 65 6d 2d 3e 70 54 61  t( pTabItem->pTa
2553d 62 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  b );.#ifndef SQL
2553e 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2553f 54 41 42 4c 45 0a 20 20 20 20 20 20 69 66 28 20  TABLE.      if( 
25540 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 49 74  IsVirtual(pTabIt
25541 65 6d 2d 3e 70 54 61 62 29 20 29 7b 0a 20 20 20  em->pTab) ){.   
25542 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64       sqlite3_ind
25543 65 78 5f 69 6e 66 6f 20 2a 2a 70 70 49 64 78 49  ex_info **ppIdxI
25544 6e 66 6f 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 61  nfo = &pWInfo->a
25545 5b 6a 5d 2e 70 49 64 78 49 6e 66 6f 3b 0a 20 20  [j].pIdxInfo;.  
25546 20 20 20 20 20 20 63 6f 73 74 20 3d 20 62 65 73        cost = bes
25547 74 56 69 72 74 75 61 6c 49 6e 64 65 78 28 70 50  tVirtualIndex(pP
25548 61 72 73 65 2c 20 26 77 63 2c 20 70 54 61 62 49  arse, &wc, pTabI
25549 74 65 6d 2c 20 6e 6f 74 52 65 61 64 79 2c 0a 20  tem, notReady,. 
2554a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2554b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
2554c 70 4f 72 64 65 72 42 79 20 3f 20 2a 70 70 4f 72  pOrderBy ? *ppOr
2554d 64 65 72 42 79 20 3a 20 30 2c 20 69 3d 3d 30 2c  derBy : 0, i==0,
2554e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2554f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25550 20 70 70 49 64 78 49 6e 66 6f 29 3b 0a 20 20 20   ppIdxInfo);.   
25551 20 20 20 20 20 66 6c 61 67 73 20 3d 20 57 48 45       flags = WHE
25552 52 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 3b  RE_VIRTUALTABLE;
25553 0a 20 20 20 20 20 20 20 20 70 49 6e 64 65 78 20  .        pIndex 
25554 3d 20 2a 70 70 49 64 78 49 6e 66 6f 3b 0a 20 20  = *ppIdxInfo;.  
25555 20 20 20 20 20 20 69 66 28 20 70 49 6e 64 65 78        if( pIndex
25556 20 26 26 20 70 49 6e 64 65 78 2d 3e 6f 72 64 65   && pIndex->orde
25557 72 42 79 43 6f 6e 73 75 6d 65 64 20 29 7b 0a 20  rByConsumed ){. 
25558 20 20 20 20 20 20 20 20 20 66 6c 61 67 73 20 3d           flags =
25559 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54 41   WHERE_VIRTUALTA
2555a 42 4c 45 20 7c 20 57 48 45 52 45 5f 4f 52 44 45  BLE | WHERE_ORDE
2555b 52 42 59 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  RBY;.        }. 
2555c 20 20 20 20 20 20 20 70 49 64 78 20 3d 20 30 3b         pIdx = 0;
2555d 0a 20 20 20 20 20 20 20 20 6e 45 71 20 3d 20 30  .        nEq = 0
2555e 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 53  ;.        if( (S
2555f 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 2f 32 2e  QLITE_BIG_DBL/2.
25560 30 29 3c 63 6f 73 74 20 29 7b 0a 20 20 20 20 20  0)<cost ){.     
25561 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 73 74       /* The cost
25562 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20   is not allowed 
25563 74 6f 20 62 65 20 6c 61 72 67 65 72 20 74 68 61  to be larger tha
25564 6e 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c  n SQLITE_BIG_DBL
25565 20 28 74 68 65 0a 20 20 20 20 20 20 20 20 20 20   (the.          
25566 2a 2a 20 69 6e 69 74 61 6c 20 76 61 6c 75 65 20  ** inital value 
25567 6f 66 20 6c 6f 77 65 73 74 43 6f 73 74 20 69 6e  of lowestCost in
25568 20 74 68 69 73 20 6c 6f 6f 70 2e 20 49 66 20 69   this loop. If i
25569 74 20 69 73 2c 20 74 68 65 6e 0a 20 20 20 20 20  t is, then.     
2556a 20 20 20 20 20 2a 2a 20 74 68 65 20 28 63 6f 73       ** the (cos
2556b 74 3c 6c 6f 77 65 73 74 43 6f 73 74 29 20 74 65  t<lowestCost) te
2556c 73 74 20 62 65 6c 6f 77 20 77 69 6c 6c 20 6e 65  st below will ne
2556d 76 65 72 20 62 65 20 74 72 75 65 20 61 6e 64 0a  ver be true and.
2556e 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 4c 65            ** pLe
2556f 76 65 6c 2d 3e 70 42 65 73 74 49 64 78 20 6e 65  vel->pBestIdx ne
25570 76 65 72 20 73 65 74 2e 0a 20 20 20 20 20 20 20  ver set..       
25571 20 20 20 2a 2f 20 0a 20 20 20 20 20 20 20 20 20     */ .         
25572 20 63 6f 73 74 20 3d 20 28 53 51 4c 49 54 45 5f   cost = (SQLITE_
25573 42 49 47 5f 44 42 4c 2f 32 2e 30 29 3b 0a 20 20  BIG_DBL/2.0);.  
25574 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
25575 6c 73 65 20 0a 23 65 6e 64 69 66 0a 20 20 20 20  lse .#endif.    
25576 20 20 7b 0a 20 20 20 20 20 20 20 20 63 6f 73 74    {.        cost
25577 20 3d 20 62 65 73 74 49 6e 64 65 78 28 70 50 61   = bestIndex(pPa
25578 72 73 65 2c 20 26 77 63 2c 20 70 54 61 62 49 74  rse, &wc, pTabIt
25579 65 6d 2c 20 6e 6f 74 52 65 61 64 79 2c 0a 20 20  em, notReady,.  
2557a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2557b 20 20 20 20 20 20 20 28 69 3d 3d 30 20 26 26 20         (i==0 && 
2557c 70 70 4f 72 64 65 72 42 79 29 20 3f 20 2a 70 70  ppOrderBy) ? *pp
2557d 4f 72 64 65 72 42 79 20 3a 20 30 2c 0a 20 20 20  OrderBy : 0,.   
2557e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2557f 20 20 20 20 20 20 26 70 49 64 78 2c 20 26 66 6c        &pIdx, &fl
25580 61 67 73 2c 20 26 6e 45 71 29 3b 0a 20 20 20 20  ags, &nEq);.    
25581 20 20 20 20 70 49 6e 64 65 78 20 3d 20 30 3b 0a      pIndex = 0;.
25582 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
25583 28 20 63 6f 73 74 3c 6c 6f 77 65 73 74 43 6f 73  ( cost<lowestCos
25584 74 20 29 7b 0a 20 20 20 20 20 20 20 20 6f 6e 63  t ){.        onc
25585 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 6c  e = 1;.        l
25586 6f 77 65 73 74 43 6f 73 74 20 3d 20 63 6f 73 74  owestCost = cost
25587 3b 0a 20 20 20 20 20 20 20 20 70 42 65 73 74 20  ;.        pBest 
25588 3d 20 70 49 64 78 3b 0a 20 20 20 20 20 20 20 20  = pIdx;.        
25589 62 65 73 74 46 6c 61 67 73 20 3d 20 66 6c 61 67  bestFlags = flag
2558a 73 3b 0a 20 20 20 20 20 20 20 20 62 65 73 74 4e  s;.        bestN
2558b 45 71 20 3d 20 6e 45 71 3b 0a 20 20 20 20 20 20  Eq = nEq;.      
2558c 20 20 62 65 73 74 4a 20 3d 20 6a 3b 0a 20 20 20    bestJ = j;.   
2558d 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 42 65       pLevel->pBe
2558e 73 74 49 64 78 20 3d 20 70 49 6e 64 65 78 3b 0a  stIdx = pIndex;.
2558f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
25590 28 20 64 6f 4e 6f 74 52 65 6f 72 64 65 72 20 29  ( doNotReorder )
25591 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
25592 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22 2a    WHERETRACE(("*
25593 2a 2a 20 4f 70 74 69 6d 69 7a 65 72 20 63 68 6f  ** Optimizer cho
25594 6f 73 65 20 74 61 62 6c 65 20 25 64 20 66 6f 72  ose table %d for
25595 20 6c 6f 6f 70 20 25 64 5c 6e 22 2c 20 62 65 73   loop %d\n", bes
25596 74 4a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70  tJ,.           p
25597 4c 65 76 65 6c 2d 70 57 49 6e 66 6f 2d 3e 61 29  Level-pWInfo->a)
25598 29 3b 0a 20 20 20 20 69 66 28 20 28 62 65 73 74  );.    if( (best
25599 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 4f 52  Flags & WHERE_OR
2559a 44 45 52 42 59 29 21 3d 30 20 29 7b 0a 20 20 20  DERBY)!=0 ){.   
2559b 20 20 20 2a 70 70 4f 72 64 65 72 42 79 20 3d 20     *ppOrderBy = 
2559c 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 6e 64  0;.    }.    and
2559d 46 6c 61 67 73 20 26 3d 20 62 65 73 74 46 6c 61  Flags &= bestFla
2559e 67 73 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e  gs;.    pLevel->
2559f 66 6c 61 67 73 20 3d 20 62 65 73 74 46 6c 61 67  flags = bestFlag
255a0 73 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70  s;.    pLevel->p
255a1 49 64 78 20 3d 20 70 42 65 73 74 3b 0a 20 20 20  Idx = pBest;.   
255a2 20 70 4c 65 76 65 6c 2d 3e 6e 45 71 20 3d 20 62   pLevel->nEq = b
255a3 65 73 74 4e 45 71 3b 0a 20 20 20 20 70 4c 65 76  estNEq;.    pLev
255a4 65 6c 2d 3e 61 49 6e 4c 6f 6f 70 20 3d 20 30 3b  el->aInLoop = 0;
255a5 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6e 49 6e  .    pLevel->nIn
255a6 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 42   = 0;.    if( pB
255a7 65 73 74 20 29 7b 0a 20 20 20 20 20 20 70 4c 65  est ){.      pLe
255a8 76 65 6c 2d 3e 69 49 64 78 43 75 72 20 3d 20 70  vel->iIdxCur = p
255a9 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
255aa 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
255ab 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72 20  pLevel->iIdxCur 
255ac 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  = -1;.    }.    
255ad 6e 6f 74 52 65 61 64 79 20 26 3d 20 7e 67 65 74  notReady &= ~get
255ae 4d 61 73 6b 28 26 6d 61 73 6b 53 65 74 2c 20 70  Mask(&maskSet, p
255af 54 61 62 4c 69 73 74 2d 3e 61 5b 62 65 73 74 4a  TabList->a[bestJ
255b0 5d 2e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  ].iCursor);.    
255b1 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 20 3d 20  pLevel->iFrom = 
255b2 62 65 73 74 4a 3b 0a 20 20 7d 0a 20 20 57 48 45  bestJ;.  }.  WHE
255b3 52 45 54 52 41 43 45 28 28 22 2a 2a 2a 20 4f 70  RETRACE(("*** Op
255b4 74 69 6d 69 7a 65 72 20 46 69 6e 69 73 68 65 64  timizer Finished
255b5 20 2a 2a 2a 5c 6e 22 29 29 3b 0a 0a 20 20 2f 2a   ***\n"));..  /*
255b6 20 49 66 20 74 68 65 20 74 6f 74 61 6c 20 71 75   If the total qu
255b7 65 72 79 20 6f 6e 6c 79 20 73 65 6c 65 63 74 73  ery only selects
255b8 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 2c 20 74   a single row, t
255b9 68 65 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59  hen the ORDER BY
255ba 0a 20 20 2a 2a 20 63 6c 61 75 73 65 20 69 73 20  .  ** clause is 
255bb 69 72 72 65 6c 65 76 61 6e 74 2e 0a 20 20 2a 2f  irrelevant..  */
255bc 0a 20 20 69 66 28 20 28 61 6e 64 46 6c 61 67 73  .  if( (andFlags
255bd 20 26 20 57 48 45 52 45 5f 55 4e 49 51 55 45 29   & WHERE_UNIQUE)
255be 21 3d 30 20 26 26 20 70 70 4f 72 64 65 72 42 79  !=0 && ppOrderBy
255bf 20 29 7b 0a 20 20 20 20 2a 70 70 4f 72 64 65 72   ){.    *ppOrder
255c0 42 79 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  By = 0;.  }..  /
255c1 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20  * If the caller 
255c2 69 73 20 61 6e 20 55 50 44 41 54 45 20 6f 72 20  is an UPDATE or 
255c3 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74  DELETE statement
255c4 20 74 68 61 74 20 69 73 20 72 65 71 75 65 73 74   that is request
255c5 69 6e 67 0a 20 20 2a 2a 20 74 6f 20 75 73 65 20  ing.  ** to use 
255c6 61 20 6f 6e 65 2d 70 61 73 73 20 61 6c 67 6f 72  a one-pass algor
255c7 69 74 68 6d 2c 20 64 65 74 65 72 6d 69 6e 65 20  ithm, determine 
255c8 69 66 20 74 68 69 73 20 69 73 20 61 70 70 72 6f  if this is appro
255c9 70 72 69 61 74 65 2e 0a 20 20 2a 2a 20 54 68 65  priate..  ** The
255ca 20 6f 6e 65 2d 70 61 73 73 20 61 6c 67 6f 72 69   one-pass algori
255cb 74 68 6d 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 69  thm only works i
255cc 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  f the WHERE clau
255cd 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20  se constraints. 
255ce 20 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e   ** the statemen
255cf 74 20 74 6f 20 75 70 64 61 74 65 20 61 20 73 69  t to update a si
255d0 6e 67 6c 65 20 72 6f 77 2e 0a 20 20 2a 2f 0a 20  ngle row..  */. 
255d1 20 61 73 73 65 72 74 28 20 28 77 66 6c 61 67 73   assert( (wflags
255d2 20 26 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 53   & WHERE_ONEPASS
255d3 5f 44 45 53 49 52 45 44 29 3d 3d 30 20 7c 7c 20  _DESIRED)==0 || 
255d4 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 3d 3d  pWInfo->nLevel==
255d5 31 20 29 3b 0a 20 20 69 66 28 20 28 77 66 6c 61  1 );.  if( (wfla
255d6 67 73 20 26 20 57 48 45 52 45 5f 4f 4e 45 50 41  gs & WHERE_ONEPA
255d7 53 53 5f 44 45 53 49 52 45 44 29 21 3d 30 20 26  SS_DESIRED)!=0 &
255d8 26 20 28 61 6e 64 46 6c 61 67 73 20 26 20 57 48  & (andFlags & WH
255d9 45 52 45 5f 55 4e 49 51 55 45 29 21 3d 30 20 29  ERE_UNIQUE)!=0 )
255da 7b 0a 20 20 20 20 70 57 49 6e 66 6f 2d 3e 6f 6b  {.    pWInfo->ok
255db 4f 6e 65 50 61 73 73 20 3d 20 31 3b 0a 20 20 20  OnePass = 1;.   
255dc 20 70 57 49 6e 66 6f 2d 3e 61 5b 30 5d 2e 66 6c   pWInfo->a[0].fl
255dd 61 67 73 20 26 3d 20 7e 57 48 45 52 45 5f 49 44  ags &= ~WHERE_ID
255de 58 5f 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 2f  X_ONLY;.  }..  /
255df 2a 20 4f 70 65 6e 20 61 6c 6c 20 74 61 62 6c 65  * Open all table
255e0 73 20 69 6e 20 74 68 65 20 70 54 61 62 4c 69 73  s in the pTabLis
255e1 74 20 61 6e 64 20 61 6e 79 20 69 6e 64 69 63 65  t and any indice
255e2 73 20 73 65 6c 65 63 74 65 64 20 66 6f 72 0a 20  s selected for. 
255e3 20 2a 2a 20 73 65 61 72 63 68 69 6e 67 20 74 68   ** searching th
255e4 6f 73 65 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f  ose tables..  */
255e5 0a 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65  .  sqlite3CodeVe
255e6 72 69 66 79 53 63 68 65 6d 61 28 70 50 61 72 73  rifySchema(pPars
255e7 65 2c 20 2d 31 29 3b 20 2f 2a 20 49 6e 73 65 72  e, -1); /* Inser
255e8 74 20 74 68 65 20 63 6f 6f 6b 69 65 20 76 65 72  t the cookie ver
255e9 69 66 69 65 72 20 47 6f 74 6f 20 2a 2f 0a 20 20  ifier Goto */.  
255ea 66 6f 72 28 69 3d 30 2c 20 70 4c 65 76 65 6c 3d  for(i=0, pLevel=
255eb 70 57 49 6e 66 6f 2d 3e 61 3b 20 69 3c 70 54 61  pWInfo->a; i<pTa
255ec 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  bList->nSrc; i++
255ed 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20 20 20  , pLevel++){.   
255ee 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20   Table *pTab;   
255ef 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 6f 70    /* Table to op
255f0 65 6e 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20  en */.    Index 
255f1 2a 70 49 78 3b 20 20 20 20 20 20 2f 2a 20 49 6e  *pIx;      /* In
255f2 64 65 78 20 75 73 65 64 20 74 6f 20 61 63 63 65  dex used to acce
255f3 73 73 20 70 54 61 62 20 28 69 66 20 61 6e 79 29  ss pTab (if any)
255f4 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 44 62 3b   */.    int iDb;
255f5 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
255f6 78 20 6f 66 20 64 61 74 61 62 61 73 65 20 63 6f  x of database co
255f7 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 2f 69  ntaining table/i
255f8 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ndex */.    int 
255f9 69 49 64 78 43 75 72 20 3d 20 70 4c 65 76 65 6c  iIdxCur = pLevel
255fa 2d 3e 69 49 64 78 43 75 72 3b 0a 0a 23 69 66 6e  ->iIdxCur;..#ifn
255fb 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
255fc 45 58 50 4c 41 49 4e 0a 20 20 20 20 69 66 28 20  EXPLAIN.    if( 
255fd 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e 3d  pParse->explain=
255fe 3d 32 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72  =2 ){.      char
255ff 20 2a 7a 4d 73 67 3b 0a 20 20 20 20 20 20 73 74   *zMsg;.      st
25600 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
25601 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 54 61 62  m *pItem = &pTab
25602 4c 69 73 74 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e  List->a[pLevel->
25603 69 46 72 6f 6d 5d 3b 0a 20 20 20 20 20 20 7a 4d  iFrom];.      zM
25604 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  sg = sqlite3MPri
25605 6e 74 66 28 64 62 2c 20 22 54 41 42 4c 45 20 25  ntf(db, "TABLE %
25606 73 22 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65  s", pItem->zName
25607 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 74  );.      if( pIt
25608 65 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a 20 20  em->zAlias ){.  
25609 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c        zMsg = sql
2560a 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
2560b 22 25 7a 20 41 53 20 25 73 22 2c 20 7a 4d 73 67  "%z AS %s", zMsg
2560c 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29  , pItem->zAlias)
2560d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2560e 69 66 28 20 28 70 49 78 20 3d 20 70 4c 65 76 65  if( (pIx = pLeve
2560f 6c 2d 3e 70 49 64 78 29 21 3d 30 20 29 7b 0a 20  l->pIdx)!=0 ){. 
25610 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71         zMsg = sq
25611 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
25612 20 22 25 7a 20 57 49 54 48 20 49 4e 44 45 58 20   "%z WITH INDEX 
25613 25 73 22 2c 20 7a 4d 73 67 2c 20 70 49 78 2d 3e  %s", zMsg, pIx->
25614 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 65  zName);.      }e
25615 6c 73 65 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e  lse if( pLevel->
25616 66 6c 61 67 73 20 26 20 28 57 48 45 52 45 5f 52  flags & (WHERE_R
25617 4f 57 49 44 5f 45 51 7c 57 48 45 52 45 5f 52 4f  OWID_EQ|WHERE_RO
25618 57 49 44 5f 52 41 4e 47 45 29 20 29 7b 0a 20 20  WID_RANGE) ){.  
25619 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c        zMsg = sql
2561a 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
2561b 22 25 7a 20 55 53 49 4e 47 20 50 52 49 4d 41 52  "%z USING PRIMAR
2561c 59 20 4b 45 59 22 2c 20 7a 4d 73 67 29 3b 0a 20  Y KEY", zMsg);. 
2561d 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53       }.#ifndef S
2561e 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
2561f 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 65 6c  ALTABLE.      el
25620 73 65 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70  se if( pLevel->p
25621 42 65 73 74 49 64 78 20 29 7b 0a 20 20 20 20 20  BestIdx ){.     
25622 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78     sqlite3_index
25623 5f 69 6e 66 6f 20 2a 70 42 65 73 74 49 64 78 20  _info *pBestIdx 
25624 3d 20 70 4c 65 76 65 6c 2d 3e 70 42 65 73 74 49  = pLevel->pBestI
25625 64 78 3b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67  dx;.        zMsg
25626 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
25627 66 28 64 62 2c 20 22 25 7a 20 56 49 52 54 55 41  f(db, "%z VIRTUA
25628 4c 20 54 41 42 4c 45 20 49 4e 44 45 58 20 25 64  L TABLE INDEX %d
25629 3a 25 73 22 2c 20 7a 4d 73 67 2c 0a 20 20 20 20  :%s", zMsg,.    
2562a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2562b 70 42 65 73 74 49 64 78 2d 3e 69 64 78 4e 75 6d  pBestIdx->idxNum
2562c 2c 20 70 42 65 73 74 49 64 78 2d 3e 69 64 78 53  , pBestIdx->idxS
2562d 74 72 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  tr);.      }.#en
2562e 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 70 4c  dif.      if( pL
2562f 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26 20 57 48  evel->flags & WH
25630 45 52 45 5f 4f 52 44 45 52 42 59 20 29 7b 0a 20  ERE_ORDERBY ){. 
25631 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71         zMsg = sq
25632 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
25633 20 22 25 7a 20 4f 52 44 45 52 20 42 59 22 2c 20   "%z ORDER BY", 
25634 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20  zMsg);.      }. 
25635 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
25636 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 45 78 70  AddOp4(v, OP_Exp
25637 6c 61 69 6e 2c 20 69 2c 20 70 4c 65 76 65 6c 2d  lain, i, pLevel-
25638 3e 69 46 72 6f 6d 2c 20 30 2c 20 7a 4d 73 67 2c  >iFrom, 0, zMsg,
25639 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20   P4_DYNAMIC);.  
2563a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51    }.#endif /* SQ
2563b 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49  LITE_OMIT_EXPLAI
2563c 4e 20 2a 2f 0a 20 20 20 20 70 54 61 62 49 74 65  N */.    pTabIte
2563d 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61  m = &pTabList->a
2563e 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b  [pLevel->iFrom];
2563f 0a 20 20 20 20 70 54 61 62 20 3d 20 70 54 61 62  .    pTab = pTab
25640 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20  Item->pTab;.    
25641 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
25642 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73  emaToIndex(pPars
25643 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  e->db, pTab->pSc
25644 68 65 6d 61 29 3b 0a 20 20 20 20 69 66 28 20 70  hema);.    if( p
25645 54 61 62 2d 3e 69 73 45 70 68 65 6d 20 7c 7c 20  Tab->isEphem || 
25646 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 20  pTab->pSelect ) 
25647 63 6f 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65  continue;.#ifnde
25648 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
25649 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69  RTUALTABLE.    i
2564a 66 28 20 70 4c 65 76 65 6c 2d 3e 70 42 65 73 74  f( pLevel->pBest
2564b 49 64 78 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  Idx ){.      int
2564c 20 69 43 75 72 20 3d 20 70 54 61 62 49 74 65 6d   iCur = pTabItem
2564d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 20  ->iCursor;.     
2564e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2564f 70 34 28 76 2c 20 4f 50 5f 56 4f 70 65 6e 2c 20  p4(v, OP_VOpen, 
25650 69 43 75 72 2c 20 30 2c 20 30 2c 0a 20 20 20 20  iCur, 0, 0,.    
25651 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25652 20 20 20 20 28 63 6f 6e 73 74 20 63 68 61 72 2a      (const char*
25653 29 70 54 61 62 2d 3e 70 56 74 61 62 2c 20 50 34  )pTab->pVtab, P4
25654 5f 56 54 41 42 29 3b 0a 20 20 20 20 7d 65 6c 73  _VTAB);.    }els
25655 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28  e.#endif.    if(
25656 20 28 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20   (pLevel->flags 
25657 26 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59  & WHERE_IDX_ONLY
25658 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e  )==0 ){.      in
25659 74 20 6f 70 20 3d 20 70 57 49 6e 66 6f 2d 3e 6f  t op = pWInfo->o
2565a 6b 4f 6e 65 50 61 73 73 20 3f 20 4f 50 5f 4f 70  kOnePass ? OP_Op
2565b 65 6e 57 72 69 74 65 20 3a 20 4f 50 5f 4f 70 65  enWrite : OP_Ope
2565c 6e 52 65 61 64 3b 0a 20 20 20 20 20 20 73 71 6c  nRead;.      sql
2565d 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50  ite3OpenTable(pP
2565e 61 72 73 65 2c 20 70 54 61 62 49 74 65 6d 2d 3e  arse, pTabItem->
2565f 69 43 75 72 73 6f 72 2c 20 69 44 62 2c 20 70 54  iCursor, iDb, pT
25660 61 62 2c 20 6f 70 29 3b 0a 20 20 20 20 20 20 69  ab, op);.      i
25661 66 28 20 21 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e  f( !pWInfo->okOn
25662 65 50 61 73 73 20 26 26 20 70 54 61 62 2d 3e 6e  ePass && pTab->n
25663 43 6f 6c 3c 28 73 69 7a 65 6f 66 28 42 69 74 6d  Col<(sizeof(Bitm
25664 61 73 6b 29 2a 38 29 20 29 7b 0a 20 20 20 20 20  ask)*8) ){.     
25665 20 20 20 42 69 74 6d 61 73 6b 20 62 20 3d 20 70     Bitmask b = p
25666 54 61 62 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64  TabItem->colUsed
25667 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 20  ;.        int n 
25668 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  = 0;.        for
25669 28 3b 20 62 3b 20 62 3d 62 3e 3e 31 2c 20 6e 2b  (; b; b=b>>1, n+
2566a 2b 29 7b 7d 0a 20 20 20 20 20 20 20 20 73 71 6c  +){}.        sql
2566b 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32  ite3VdbeChangeP2
2566c 28 76 2c 20 73 71 6c 69 74 65 33 56 64 62 65 43  (v, sqlite3VdbeC
2566d 75 72 72 65 6e 74 41 64 64 72 28 76 29 2d 32 2c  urrentAddr(v)-2,
2566e 20 6e 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73   n);.        ass
2566f 65 72 74 28 20 6e 3c 3d 70 54 61 62 2d 3e 6e 43  ert( n<=pTab->nC
25670 6f 6c 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ol );.      }.  
25671 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
25672 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28  qlite3TableLock(
25673 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54 61  pParse, iDb, pTa
25674 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 54 61 62  b->tnum, 0, pTab
25675 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a  ->zName);.    }.
25676 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69 54 61 62      pLevel->iTab
25677 43 75 72 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e  Cur = pTabItem->
25678 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 69 66 28  iCursor;.    if(
25679 20 28 70 49 78 20 3d 20 70 4c 65 76 65 6c 2d 3e   (pIx = pLevel->
2567a 70 49 64 78 29 21 3d 30 20 29 7b 0a 20 20 20 20  pIdx)!=0 ){.    
2567b 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20    KeyInfo *pKey 
2567c 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65  = sqlite3IndexKe
2567d 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49  yinfo(pParse, pI
2567e 78 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  x);.      assert
2567f 28 20 70 49 78 2d 3e 70 53 63 68 65 6d 61 3d 3d  ( pIx->pSchema==
25680 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b  pTab->pSchema );
25681 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
25682 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
25683 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c  etNumColumns, 0,
25684 20 70 49 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29   pIx->nColumn+1)
25685 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
25686 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
25687 4f 70 65 6e 52 65 61 64 2c 20 69 49 64 78 43 75  OpenRead, iIdxCu
25688 72 2c 20 70 49 78 2d 3e 74 6e 75 6d 2c 20 69 44  r, pIx->tnum, iD
25689 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  b,.             
2568a 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72             (char
2568b 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e  *)pKey, P4_KEYIN
2568c 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20  FO_HANDOFF);.   
2568d 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
2568e 76 2c 20 22 25 73 22 2c 20 70 49 78 2d 3e 7a 4e  v, "%s", pIx->zN
2568f 61 6d 65 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20  ame));.    }.   
25690 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69   sqlite3CodeVeri
25691 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c  fySchema(pParse,
25692 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20 70 57 49   iDb);.  }.  pWI
25693 6e 66 6f 2d 3e 69 54 6f 70 20 3d 20 73 71 6c 69  nfo->iTop = sqli
25694 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
25695 64 72 28 76 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e  dr(v);..  /* Gen
25696 65 72 61 74 65 20 74 68 65 20 63 6f 64 65 20 74  erate the code t
25697 6f 20 64 6f 20 74 68 65 20 73 65 61 72 63 68 2e  o do the search.
25698 20 20 45 61 63 68 20 69 74 65 72 61 74 69 6f 6e    Each iteration
25699 20 6f 66 20 74 68 65 20 66 6f 72 0a 20 20 2a 2a   of the for.  **
2569a 20 6c 6f 6f 70 20 62 65 6c 6f 77 20 67 65 6e 65   loop below gene
2569b 72 61 74 65 73 20 63 6f 64 65 20 66 6f 72 20 61  rates code for a
2569c 20 73 69 6e 67 6c 65 20 6e 65 73 74 65 64 20 6c   single nested l
2569d 6f 6f 70 20 6f 66 20 74 68 65 20 56 4d 0a 20 20  oop of the VM.  
2569e 2a 2a 20 70 72 6f 67 72 61 6d 2e 0a 20 20 2a 2f  ** program..  */
2569f 0a 20 20 6e 6f 74 52 65 61 64 79 20 3d 20 7e 28  .  notReady = ~(
256a0 42 69 74 6d 61 73 6b 29 30 3b 0a 20 20 66 6f 72  Bitmask)0;.  for
256a1 28 69 3d 30 2c 20 70 4c 65 76 65 6c 3d 70 57 49  (i=0, pLevel=pWI
256a2 6e 66 6f 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69  nfo->a; i<pTabLi
256a3 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70  st->nSrc; i++, p
256a4 4c 65 76 65 6c 2b 2b 29 7b 0a 20 20 20 20 69 6e  Level++){.    in
256a5 74 20 6a 3b 0a 20 20 20 20 69 6e 74 20 69 43 75  t j;.    int iCu
256a6 72 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 69 43  r = pTabItem->iC
256a7 75 72 73 6f 72 3b 20 20 2f 2a 20 54 68 65 20 56  ursor;  /* The V
256a8 44 42 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74  DBE cursor for t
256a9 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  he table */.    
256aa 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20  Index *pIdx;    
256ab 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20     /* The index 
256ac 77 65 20 77 69 6c 6c 20 62 65 20 75 73 69 6e 67  we will be using
256ad 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 78 74 3b   */.    int nxt;
256ae 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68             /* Wh
256af 65 72 65 20 74 6f 20 6a 75 6d 70 20 74 6f 20 63  ere to jump to c
256b0 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 74 68 65  ontinue with the
256b1 20 6e 65 78 74 20 49 4e 20 63 61 73 65 20 2a 2f   next IN case */
256b2 0a 20 20 20 20 69 6e 74 20 69 49 64 78 43 75 72  .    int iIdxCur
256b3 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56  ;       /* The V
256b4 44 42 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74  DBE cursor for t
256b5 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20  he index */.    
256b6 69 6e 74 20 6f 6d 69 74 54 61 62 6c 65 3b 20 20  int omitTable;  
256b7 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65     /* True if we
256b8 20 75 73 65 20 74 68 65 20 69 6e 64 65 78 20 6f   use the index o
256b9 6e 6c 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 62  nly */.    int b
256ba 52 65 76 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Rev;          /*
256bb 20 54 72 75 65 20 69 66 20 77 65 20 6e 65 65 64   True if we need
256bc 20 74 6f 20 73 63 61 6e 20 69 6e 20 72 65 76 65   to scan in reve
256bd 72 73 65 20 6f 72 64 65 72 20 2a 2f 0a 0a 20 20  rse order */..  
256be 20 20 70 54 61 62 49 74 65 6d 20 3d 20 26 70 54    pTabItem = &pT
256bf 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76 65 6c  abList->a[pLevel
256c0 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20 20 69 43  ->iFrom];.    iC
256c1 75 72 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 69  ur = pTabItem->i
256c2 43 75 72 73 6f 72 3b 0a 20 20 20 20 70 49 64 78  Cursor;.    pIdx
256c3 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 49 64 78 3b   = pLevel->pIdx;
256c4 0a 20 20 20 20 69 49 64 78 43 75 72 20 3d 20 70  .    iIdxCur = p
256c5 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72 3b 0a  Level->iIdxCur;.
256c6 20 20 20 20 62 52 65 76 20 3d 20 28 70 4c 65 76      bRev = (pLev
256c7 65 6c 2d 3e 66 6c 61 67 73 20 26 20 57 48 45 52  el->flags & WHER
256c8 45 5f 52 45 56 45 52 53 45 29 21 3d 30 3b 0a 20  E_REVERSE)!=0;. 
256c9 20 20 20 6f 6d 69 74 54 61 62 6c 65 20 3d 20 28     omitTable = (
256ca 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26 20  pLevel->flags & 
256cb 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29 21  WHERE_IDX_ONLY)!
256cc 3d 30 3b 0a 0a 20 20 20 20 2f 2a 20 43 72 65 61  =0;..    /* Crea
256cd 74 65 20 6c 61 62 65 6c 73 20 66 6f 72 20 74 68  te labels for th
256ce 65 20 22 62 72 65 61 6b 22 20 61 6e 64 20 22 63  e "break" and "c
256cf 6f 6e 74 69 6e 75 65 22 20 69 6e 73 74 72 75 63  ontinue" instruc
256d0 74 69 6f 6e 73 0a 20 20 20 20 2a 2a 20 66 6f 72  tions.    ** for
256d1 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 6f   the current loo
256d2 70 2e 20 20 4a 75 6d 70 20 74 6f 20 62 72 6b 20  p.  Jump to brk 
256d3 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20  to break out of 
256d4 61 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2a 20 4a  a loop..    ** J
256d5 75 6d 70 20 74 6f 20 63 6f 6e 74 20 74 6f 20 67  ump to cont to g
256d6 6f 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f  o immediately to
256d7 20 74 68 65 20 6e 65 78 74 20 69 74 65 72 61 74   the next iterat
256d8 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20 20 20 2a  ion of the.    *
256d9 2a 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2a 0a 20  * loop..    **. 
256da 20 20 20 2a 2a 20 57 68 65 6e 20 74 68 65 72 65     ** When there
256db 20 69 73 20 61 6e 20 49 4e 20 6f 70 65 72 61 74   is an IN operat
256dc 6f 72 2c 20 77 65 20 61 6c 73 6f 20 68 61 76 65  or, we also have
256dd 20 61 20 22 6e 78 74 22 20 6c 61 62 65 6c 20 74   a "nxt" label t
256de 68 61 74 0a 20 20 20 20 2a 2a 20 6d 65 61 6e 73  hat.    ** means
256df 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74   to continue wit
256e0 68 20 74 68 65 20 6e 65 78 74 20 49 4e 20 76 61  h the next IN va
256e1 6c 75 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 2e  lue combination.
256e2 20 20 57 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68    When.    ** th
256e3 65 72 65 20 61 72 65 20 6e 6f 20 49 4e 20 6f 70  ere are no IN op
256e4 65 72 61 74 6f 72 73 20 69 6e 20 74 68 65 20 63  erators in the c
256e5 6f 6e 73 74 72 61 69 6e 74 73 2c 20 74 68 65 20  onstraints, the 
256e6 22 6e 78 74 22 20 6c 61 62 65 6c 0a 20 20 20 20  "nxt" label.    
256e7 2a 2a 20 69 73 20 74 68 65 20 73 61 6d 65 20 61  ** is the same a
256e8 73 20 22 62 72 6b 22 2e 0a 20 20 20 20 2a 2f 0a  s "brk"..    */.
256e9 20 20 20 20 62 72 6b 20 3d 20 70 4c 65 76 65 6c      brk = pLevel
256ea 2d 3e 62 72 6b 20 3d 20 70 4c 65 76 65 6c 2d 3e  ->brk = pLevel->
256eb 6e 78 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62  nxt = sqlite3Vdb
256ec 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
256ed 20 20 20 63 6f 6e 74 20 3d 20 70 4c 65 76 65 6c     cont = pLevel
256ee 2d 3e 63 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33  ->cont = sqlite3
256ef 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
256f0 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69  ;..    /* If thi
256f1 73 20 69 73 20 74 68 65 20 72 69 67 68 74 20 74  s is the right t
256f2 61 62 6c 65 20 6f 66 20 61 20 4c 45 46 54 20 4f  able of a LEFT O
256f3 55 54 45 52 20 4a 4f 49 4e 2c 20 61 6c 6c 6f 63  UTER JOIN, alloc
256f4 61 74 65 20 61 6e 64 0a 20 20 20 20 2a 2a 20 69  ate and.    ** i
256f5 6e 69 74 69 61 6c 69 7a 65 20 61 20 6d 65 6d 6f  nitialize a memo
256f6 72 79 20 63 65 6c 6c 20 74 68 61 74 20 72 65 63  ry cell that rec
256f7 6f 72 64 73 20 69 66 20 74 68 69 73 20 74 61 62  ords if this tab
256f8 6c 65 20 6d 61 74 63 68 65 73 20 61 6e 79 0a 20  le matches any. 
256f9 20 20 20 2a 2a 20 72 6f 77 20 6f 66 20 74 68 65     ** row of the
256fa 20 6c 65 66 74 20 74 61 62 6c 65 20 6f 66 20 74   left table of t
256fb 68 65 20 6a 6f 69 6e 2e 0a 20 20 20 20 2a 2f 0a  he join..    */.
256fc 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e      if( pLevel->
256fd 69 46 72 6f 6d 3e 30 20 26 26 20 28 70 54 61 62  iFrom>0 && (pTab
256fe 49 74 65 6d 5b 30 5d 2e 6a 6f 69 6e 74 79 70 65  Item[0].jointype
256ff 20 26 20 4a 54 5f 4c 45 46 54 29 21 3d 30 20 29   & JT_LEFT)!=0 )
25700 7b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e  {.      pLevel->
25701 69 4c 65 66 74 4a 6f 69 6e 20 3d 20 2b 2b 70 50  iLeftJoin = ++pP
25702 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
25703 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
25704 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
25705 72 2c 20 30 2c 20 70 4c 65 76 65 6c 2d 3e 69 4c  r, 0, pLevel->iL
25706 65 66 74 4a 6f 69 6e 29 3b 0a 20 20 20 20 20 20  eftJoin);.      
25707 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
25708 22 69 6e 69 74 20 4c 45 46 54 20 4a 4f 49 4e 20  "init LEFT JOIN 
25709 6e 6f 2d 6d 61 74 63 68 20 66 6c 61 67 22 29 29  no-match flag"))
2570a 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66  ;.    }..#ifndef
2570b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
2570c 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 66  TUALTABLE.    if
2570d 28 20 70 4c 65 76 65 6c 2d 3e 70 42 65 73 74 49  ( pLevel->pBestI
2570e 64 78 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43  dx ){.      /* C
2570f 61 73 65 20 30 3a 20 20 54 68 65 20 74 61 62 6c  ase 0:  The tabl
25710 65 20 69 73 20 61 20 76 69 72 74 75 61 6c 2d 74  e is a virtual-t
25711 61 62 6c 65 2e 20 20 55 73 65 20 74 68 65 20 56  able.  Use the V
25712 46 69 6c 74 65 72 20 61 6e 64 20 56 4e 65 78 74  Filter and VNext
25713 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20  .      **       
25714 20 20 20 74 6f 20 61 63 63 65 73 73 20 74 68 65     to access the
25715 20 64 61 74 61 2e 0a 20 20 20 20 20 20 2a 2f 0a   data..      */.
25716 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20        int j;.   
25717 20 20 20 69 6e 74 20 69 52 65 67 3b 20 20 20 2f     int iReg;   /
25718 2a 20 50 33 20 56 61 6c 75 65 20 66 6f 72 20 4f  * P3 Value for O
25719 50 5f 56 46 69 6c 74 65 72 20 2a 2f 0a 20 20 20  P_VFilter */.   
2571a 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78     sqlite3_index
2571b 5f 69 6e 66 6f 20 2a 70 42 65 73 74 49 64 78 20  _info *pBestIdx 
2571c 3d 20 70 4c 65 76 65 6c 2d 3e 70 42 65 73 74 49  = pLevel->pBestI
2571d 64 78 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 43  dx;.      int nC
2571e 6f 6e 73 74 72 61 69 6e 74 20 3d 20 70 42 65 73  onstraint = pBes
2571f 74 49 64 78 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e  tIdx->nConstrain
25720 74 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20  t;.      struct 
25721 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f  sqlite3_index_co
25722 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65 20 2a  nstraint_usage *
25723 61 55 73 61 67 65 20 3d 0a 20 20 20 20 20 20 20  aUsage =.       
25724 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25725 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25726 20 20 20 20 20 20 20 20 20 20 20 70 42 65 73 74             pBest
25727 49 64 78 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  Idx->aConstraint
25728 55 73 61 67 65 3b 0a 20 20 20 20 20 20 63 6f 6e  Usage;.      con
25729 73 74 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  st struct sqlite
2572a 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69  3_index_constrai
2572b 6e 74 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 20  nt *aConstraint 
2572c 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  =.              
2572d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2572e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2572f 20 20 20 20 70 42 65 73 74 49 64 78 2d 3e 61 43      pBestIdx->aC
25730 6f 6e 73 74 72 61 69 6e 74 3b 0a 0a 20 20 20 20  onstraint;..    
25731 20 20 69 52 65 67 20 3d 20 73 71 6c 69 74 65 33    iReg = sqlite3
25732 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61  GetTempRange(pPa
25733 72 73 65 2c 20 6e 43 6f 6e 73 74 72 61 69 6e 74  rse, nConstraint
25734 2b 32 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a  +2);.      for(j
25735 3d 31 3b 20 6a 3c 3d 6e 43 6f 6e 73 74 72 61 69  =1; j<=nConstrai
25736 6e 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  nt; j++){.      
25737 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20 20 20 20    int k;.       
25738 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 6e 43 6f 6e   for(k=0; k<nCon
25739 73 74 72 61 69 6e 74 3b 20 6b 2b 2b 29 7b 0a 20  straint; k++){. 
2573a 20 20 20 20 20 20 20 20 20 69 66 28 20 61 55 73           if( aUs
2573b 61 67 65 5b 6b 5d 2e 61 72 67 76 49 6e 64 65 78  age[k].argvIndex
2573c 3d 3d 6a 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==j ){.         
2573d 20 20 20 69 6e 74 20 69 54 65 72 6d 20 3d 20 61     int iTerm = a
2573e 43 6f 6e 73 74 72 61 69 6e 74 5b 6b 5d 2e 69 54  Constraint[k].iT
2573f 65 72 6d 4f 66 66 73 65 74 3b 0a 20 20 20 20 20  ermOffset;.     
25740 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
25741 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 77  prCode(pParse, w
25742 63 2e 61 5b 69 54 65 72 6d 5d 2e 70 45 78 70 72  c.a[iTerm].pExpr
25743 2d 3e 70 52 69 67 68 74 2c 20 69 52 65 67 2b 6a  ->pRight, iReg+j
25744 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  +1);.           
25745 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
25746 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
25747 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 6e 43 6f        if( k==nCo
25748 6e 73 74 72 61 69 6e 74 20 29 20 62 72 65 61 6b  nstraint ) break
25749 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2574a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2574b 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2574c 20 70 42 65 73 74 49 64 78 2d 3e 69 64 78 4e 75   pBestIdx->idxNu
2574d 6d 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 20 20  m, iReg);.      
2574e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2574f 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
25750 20 6a 2d 31 2c 20 69 52 65 67 2b 31 29 3b 0a 20   j-1, iReg+1);. 
25751 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
25752 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 46 69  AddOp4(v, OP_VFi
25753 6c 74 65 72 2c 20 69 43 75 72 2c 20 62 72 6b 2c  lter, iCur, brk,
25754 20 69 52 65 67 2c 20 70 42 65 73 74 49 64 78 2d   iReg, pBestIdx-
25755 3e 69 64 78 53 74 72 2c 0a 20 20 20 20 20 20 20  >idxStr,.       
25756 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25757 20 70 42 65 73 74 49 64 78 2d 3e 6e 65 65 64 54   pBestIdx->needT
25758 6f 46 72 65 65 49 64 78 53 74 72 20 3f 20 50 34  oFreeIdxStr ? P4
25759 5f 4d 50 52 49 4e 54 46 20 3a 20 50 34 5f 53 54  _MPRINTF : P4_ST
2575a 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c  ATIC);.      sql
2575b 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
2575c 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69 52 65  ange(pParse, iRe
2575d 67 2c 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 32  g, nConstraint+2
2575e 29 3b 0a 20 20 20 20 20 20 70 42 65 73 74 49 64  );.      pBestId
2575f 78 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78  x->needToFreeIdx
25760 53 74 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 66  Str = 0;.      f
25761 6f 72 28 6a 3d 30 3b 20 6a 3c 70 42 65 73 74 49  or(j=0; j<pBestI
25762 64 78 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  dx->nConstraint;
25763 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69   j++){.        i
25764 66 28 20 61 55 73 61 67 65 5b 6a 5d 2e 6f 6d 69  f( aUsage[j].omi
25765 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  t ){.          i
25766 6e 74 20 69 54 65 72 6d 20 3d 20 61 43 6f 6e 73  nt iTerm = aCons
25767 74 72 61 69 6e 74 5b 6a 5d 2e 69 54 65 72 6d 4f  traint[j].iTermO
25768 66 66 73 65 74 3b 0a 20 20 20 20 20 20 20 20 20  ffset;.         
25769 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65   disableTerm(pLe
2576a 76 65 6c 2c 20 26 77 63 2e 61 5b 69 54 65 72 6d  vel, &wc.a[iTerm
2576b 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ]);.        }.  
2576c 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4c 65 76      }.      pLev
2576d 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 56 4e 65 78  el->op = OP_VNex
2576e 74 3b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d  t;.      pLevel-
2576f 3e 70 31 20 3d 20 69 43 75 72 3b 0a 20 20 20 20  >p1 = iCur;.    
25770 20 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 73    pLevel->p2 = s
25771 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
25772 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 7d 65  tAddr(v);.    }e
25773 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  lse.#endif /* SQ
25774 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
25775 4c 54 41 42 4c 45 20 2a 2f 0a 0a 20 20 20 20 69  LTABLE */..    i
25776 66 28 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73  f( pLevel->flags
25777 20 26 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 45   & WHERE_ROWID_E
25778 51 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61  Q ){.      /* Ca
25779 73 65 20 31 3a 20 20 57 65 20 63 61 6e 20 64 69  se 1:  We can di
2577a 72 65 63 74 6c 79 20 72 65 66 65 72 65 6e 63 65  rectly reference
2577b 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 75 73   a single row us
2577c 69 6e 67 20 61 6e 0a 20 20 20 20 20 20 2a 2a 20  ing an.      ** 
2577d 20 20 20 20 20 20 20 20 20 65 71 75 61 6c 69 74           equalit
2577e 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 67 61  y comparison aga
2577f 69 6e 73 74 20 74 68 65 20 52 4f 57 49 44 20 66  inst the ROWID f
25780 69 65 6c 64 2e 20 20 4f 72 0a 20 20 20 20 20 20  ield.  Or.      
25781 2a 2a 20 20 20 20 20 20 20 20 20 20 77 65 20 72  **          we r
25782 65 66 65 72 65 6e 63 65 20 6d 75 6c 74 69 70 6c  eference multipl
25783 65 20 72 6f 77 73 20 75 73 69 6e 67 20 61 20 22  e rows using a "
25784 72 6f 77 69 64 20 49 4e 20 28 2e 2e 2e 29 22 0a  rowid IN (...)".
25785 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20        **        
25786 20 20 63 6f 6e 73 74 72 75 63 74 2e 0a 20 20 20    construct..   
25787 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20     */.      int 
25788 72 31 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 20  r1;.      pTerm 
25789 3d 20 66 69 6e 64 54 65 72 6d 28 26 77 63 2c 20  = findTerm(&wc, 
2578a 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61  iCur, -1, notRea
2578b 64 79 2c 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 2c  dy, WO_EQ|WO_IN,
2578c 20 30 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72   0);.      asser
2578d 74 28 20 70 54 65 72 6d 21 3d 30 20 29 3b 0a 20  t( pTerm!=0 );. 
2578e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65       assert( pTe
2578f 72 6d 2d 3e 70 45 78 70 72 21 3d 30 20 29 3b 0a  rm->pExpr!=0 );.
25790 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54        assert( pT
25791 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d  erm->leftCursor=
25792 3d 69 43 75 72 20 29 3b 0a 20 20 20 20 20 20 61  =iCur );.      a
25793 73 73 65 72 74 28 20 6f 6d 69 74 54 61 62 6c 65  ssert( omitTable
25794 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 72 31 20  ==0 );.      r1 
25795 3d 20 63 6f 64 65 45 71 75 61 6c 69 74 79 54 65  = codeEqualityTe
25796 72 6d 28 70 50 61 72 73 65 2c 20 70 54 65 72 6d  rm(pParse, pTerm
25797 2c 20 70 4c 65 76 65 6c 2c 20 30 29 3b 0a 20 20  , pLevel, 0);.  
25798 20 20 20 20 6e 78 74 20 3d 20 70 4c 65 76 65 6c      nxt = pLevel
25799 2d 3e 6e 78 74 3b 0a 20 20 20 20 20 20 73 71 6c  ->nxt;.      sql
2579a 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2579b 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20  , OP_MustBeInt, 
2579c 72 31 2c 20 6e 78 74 29 3b 0a 20 20 20 20 20 20  r1, nxt);.      
2579d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2579e 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74  3(v, OP_NotExist
2579f 73 2c 20 69 43 75 72 2c 20 6e 78 74 2c 20 72 31  s, iCur, nxt, r1
257a0 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
257a1 6d 65 6e 74 28 28 76 2c 20 22 70 6b 22 29 29 3b  ment((v, "pk"));
257a2 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f  .      pLevel->o
257a3 70 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20  p = OP_Noop;.   
257a4 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 65 76 65   }else if( pLeve
257a5 6c 2d 3e 66 6c 61 67 73 20 26 20 57 48 45 52 45  l->flags & WHERE
257a6 5f 52 4f 57 49 44 5f 52 41 4e 47 45 20 29 7b 0a  _ROWID_RANGE ){.
257a7 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20 32 3a        /* Case 2:
257a8 20 20 57 65 20 68 61 76 65 20 61 6e 20 69 6e 65    We have an ine
257a9 71 75 61 6c 69 74 79 20 63 6f 6d 70 61 72 69 73  quality comparis
257aa 6f 6e 20 61 67 61 69 6e 73 74 20 74 68 65 20 52  on against the R
257ab 4f 57 49 44 20 66 69 65 6c 64 2e 0a 20 20 20 20  OWID field..    
257ac 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 74    */.      int t
257ad 65 73 74 4f 70 20 3d 20 4f 50 5f 4e 6f 6f 70 3b  estOp = OP_Noop;
257ae 0a 20 20 20 20 20 20 69 6e 74 20 73 74 61 72 74  .      int start
257af 3b 0a 20 20 20 20 20 20 57 68 65 72 65 54 65 72  ;.      WhereTer
257b0 6d 20 2a 70 53 74 61 72 74 2c 20 2a 70 45 6e 64  m *pStart, *pEnd
257b1 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
257b2 20 6f 6d 69 74 54 61 62 6c 65 3d 3d 30 20 29 3b   omitTable==0 );
257b3 0a 20 20 20 20 20 20 70 53 74 61 72 74 20 3d 20  .      pStart = 
257b4 66 69 6e 64 54 65 72 6d 28 26 77 63 2c 20 69 43  findTerm(&wc, iC
257b5 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64 79  ur, -1, notReady
257b6 2c 20 57 4f 5f 47 54 7c 57 4f 5f 47 45 2c 20 30  , WO_GT|WO_GE, 0
257b7 29 3b 0a 20 20 20 20 20 20 70 45 6e 64 20 3d 20  );.      pEnd = 
257b8 66 69 6e 64 54 65 72 6d 28 26 77 63 2c 20 69 43  findTerm(&wc, iC
257b9 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64 79  ur, -1, notReady
257ba 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 2c 20 30  , WO_LT|WO_LE, 0
257bb 29 3b 0a 20 20 20 20 20 20 69 66 28 20 62 52 65  );.      if( bRe
257bc 76 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 65  v ){.        pTe
257bd 72 6d 20 3d 20 70 53 74 61 72 74 3b 0a 20 20 20  rm = pStart;.   
257be 20 20 20 20 20 70 53 74 61 72 74 20 3d 20 70 45       pStart = pE
257bf 6e 64 3b 0a 20 20 20 20 20 20 20 20 70 45 6e 64  nd;.        pEnd
257c0 20 3d 20 70 54 65 72 6d 3b 0a 20 20 20 20 20 20   = pTerm;.      
257c1 7d 0a 20 20 20 20 20 20 69 66 28 20 70 53 74 61  }.      if( pSta
257c2 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 45 78  rt ){.        Ex
257c3 70 72 20 2a 70 58 3b 0a 20 20 20 20 20 20 20 20  pr *pX;.        
257c4 69 6e 74 20 72 31 2c 20 72 65 67 46 72 65 65 31  int r1, regFree1
257c5 3b 0a 20 20 20 20 20 20 20 20 70 58 20 3d 20 70  ;.        pX = p
257c6 53 74 61 72 74 2d 3e 70 45 78 70 72 3b 0a 20 20  Start->pExpr;.  
257c7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 58        assert( pX
257c8 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61  !=0 );.        a
257c9 73 73 65 72 74 28 20 70 53 74 61 72 74 2d 3e 6c  ssert( pStart->l
257ca 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20  eftCursor==iCur 
257cb 29 3b 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20  );.        r1 = 
257cc 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
257cd 65 6d 70 28 70 50 61 72 73 65 2c 20 70 58 2d 3e  emp(pParse, pX->
257ce 70 52 69 67 68 74 2c 20 26 72 65 67 46 72 65 65  pRight, &regFree
257cf 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  1);.        sqli
257d0 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
257d1 20 4f 50 5f 46 6f 72 63 65 49 6e 74 2c 20 72 31   OP_ForceInt, r1
257d2 2c 20 62 72 6b 2c 20 0a 20 20 20 20 20 20 20 20  , brk, .        
257d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
257d4 20 20 20 20 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f       pX->op==TK_
257d5 4c 45 20 7c 7c 20 70 58 2d 3e 6f 70 3d 3d 54 4b  LE || pX->op==TK
257d6 5f 47 54 29 3b 0a 20 20 20 20 20 20 20 20 73 71  _GT);.        sq
257d7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
257d8 76 2c 20 62 52 65 76 20 3f 20 4f 50 5f 4d 6f 76  v, bRev ? OP_Mov
257d9 65 4c 74 20 3a 20 4f 50 5f 4d 6f 76 65 47 65 2c  eLt : OP_MoveGe,
257da 20 69 43 75 72 2c 20 62 72 6b 2c 20 72 31 29 3b   iCur, brk, r1);
257db 0a 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d  .        VdbeCom
257dc 6d 65 6e 74 28 28 76 2c 20 22 70 6b 22 29 29 3b  ment((v, "pk"));
257dd 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
257de 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
257df 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 31 29  Parse, regFree1)
257e0 3b 0a 20 20 20 20 20 20 20 20 64 69 73 61 62 6c  ;.        disabl
257e1 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 53  eTerm(pLevel, pS
257e2 74 61 72 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c  tart);.      }el
257e3 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
257e4 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
257e5 20 62 52 65 76 20 3f 20 4f 50 5f 4c 61 73 74 20   bRev ? OP_Last 
257e6 3a 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 43 75  : OP_Rewind, iCu
257e7 72 2c 20 62 72 6b 29 3b 0a 20 20 20 20 20 20 7d  r, brk);.      }
257e8 0a 20 20 20 20 20 20 69 66 28 20 70 45 6e 64 20  .      if( pEnd 
257e9 29 7b 0a 20 20 20 20 20 20 20 20 45 78 70 72 20  ){.        Expr 
257ea 2a 70 58 3b 0a 20 20 20 20 20 20 20 20 70 58 20  *pX;.        pX 
257eb 3d 20 70 45 6e 64 2d 3e 70 45 78 70 72 3b 0a 20  = pEnd->pExpr;. 
257ec 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
257ed 58 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  X!=0 );.        
257ee 61 73 73 65 72 74 28 20 70 45 6e 64 2d 3e 6c 65  assert( pEnd->le
257ef 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 29  ftCursor==iCur )
257f0 3b 0a 20 20 20 20 20 20 20 20 70 4c 65 76 65 6c  ;.        pLevel
257f1 2d 3e 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73  ->iMem = ++pPars
257f2 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20  e->nMem;.       
257f3 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
257f4 28 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 52 69  (pParse, pX->pRi
257f5 67 68 74 2c 20 70 4c 65 76 65 6c 2d 3e 69 4d 65  ght, pLevel->iMe
257f6 6d 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  m);.        if( 
257f7 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 4c 54 20 7c 7c  pX->op==TK_LT ||
257f8 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 47 54 20 29   pX->op==TK_GT )
257f9 7b 0a 20 20 20 20 20 20 20 20 20 20 74 65 73 74  {.          test
257fa 4f 70 20 3d 20 62 52 65 76 20 3f 20 4f 50 5f 4c  Op = bRev ? OP_L
257fb 65 20 3a 20 4f 50 5f 47 65 3b 0a 20 20 20 20 20  e : OP_Ge;.     
257fc 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
257fd 20 20 20 20 74 65 73 74 4f 70 20 3d 20 62 52 65      testOp = bRe
257fe 76 20 3f 20 4f 50 5f 4c 74 20 3a 20 4f 50 5f 47  v ? OP_Lt : OP_G
257ff 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  t;.        }.   
25800 20 20 20 20 20 64 69 73 61 62 6c 65 54 65 72 6d       disableTerm
25801 28 70 4c 65 76 65 6c 2c 20 70 45 6e 64 29 3b 0a  (pLevel, pEnd);.
25802 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 74        }.      st
25803 61 72 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62  art = sqlite3Vdb
25804 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b  eCurrentAddr(v);
25805 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f  .      pLevel->o
25806 70 20 3d 20 62 52 65 76 20 3f 20 4f 50 5f 50 72  p = bRev ? OP_Pr
25807 65 76 20 3a 20 4f 50 5f 4e 65 78 74 3b 0a 20 20  ev : OP_Next;.  
25808 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d      pLevel->p1 =
25809 20 69 43 75 72 3b 0a 20 20 20 20 20 20 70 4c 65   iCur;.      pLe
2580a 76 65 6c 2d 3e 70 32 20 3d 20 73 74 61 72 74 3b  vel->p2 = start;
2580b 0a 20 20 20 20 20 20 69 66 28 20 74 65 73 74 4f  .      if( testO
2580c 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20 20  p!=OP_Noop ){.  
2580d 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d 20 73        int r1 = s
2580e 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
2580f 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
25810 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
25811 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c  Op2(v, OP_Rowid,
25812 20 69 43 75 72 2c 20 72 31 29 3b 0a 20 20 20 20   iCur, r1);.    
25813 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 56 64      /* sqlite3Vd
25814 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
25815 43 6f 70 79 2c 20 70 4c 65 76 65 6c 2d 3e 69 4d  Copy, pLevel->iM
25816 65 6d 2c 20 30 29 3b 20 2a 2f 0a 20 20 20 20 20  em, 0); */.     
25817 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
25818 64 4f 70 33 28 76 2c 20 74 65 73 74 4f 70 2c 20  dOp3(v, testOp, 
25819 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d 2c 20 62 72  pLevel->iMem, br
2581a 6b 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20  k, r1);.        
2581b 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2581c 65 50 35 28 76 2c 20 53 51 4c 49 54 45 5f 41 46  eP5(v, SQLITE_AF
2581d 46 5f 4e 55 4d 45 52 49 43 20 7c 20 53 51 4c 49  F_NUMERIC | SQLI
2581e 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a  TE_JUMPIFNULL);.
2581f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52          sqlite3R
25820 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
25821 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20  arse, r1);.     
25822 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28   }.    }else if(
25823 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26   pLevel->flags &
25824 20 28 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52   (WHERE_COLUMN_R
25825 41 4e 47 45 7c 57 48 45 52 45 5f 43 4f 4c 55 4d  ANGE|WHERE_COLUM
25826 4e 5f 45 51 29 20 29 7b 0a 20 20 20 20 20 20 2f  N_EQ) ){.      /
25827 2a 20 43 61 73 65 20 33 3a 20 41 20 73 63 61 6e  * Case 3: A scan
25828 20 75 73 69 6e 67 20 61 6e 20 69 6e 64 65 78 2e   using an index.
25829 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
2582a 2a 2a 20 20 20 20 20 20 20 20 20 54 68 65 20 57  **         The W
2582b 48 45 52 45 20 63 6c 61 75 73 65 20 6d 61 79 20  HERE clause may 
2582c 63 6f 6e 74 61 69 6e 20 7a 65 72 6f 20 6f 72 20  contain zero or 
2582d 6d 6f 72 65 20 65 71 75 61 6c 69 74 79 20 0a 20  more equality . 
2582e 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20       **         
2582f 74 65 72 6d 73 20 28 22 3d 3d 22 20 6f 72 20 22  terms ("==" or "
25830 49 4e 22 20 6f 70 65 72 61 74 6f 72 73 29 20 74  IN" operators) t
25831 68 61 74 20 72 65 66 65 72 20 74 6f 20 74 68 65  hat refer to the
25832 20 4e 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20   N.      **     
25833 20 20 20 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f      left-most co
25834 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 69 6e 64  lumns of the ind
25835 65 78 2e 20 49 74 20 6d 61 79 20 61 6c 73 6f 20  ex. It may also 
25836 63 6f 6e 74 61 69 6e 0a 20 20 20 20 20 20 2a 2a  contain.      **
25837 20 20 20 20 20 20 20 20 20 69 6e 65 71 75 61 6c           inequal
25838 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  ity constraints 
25839 28 3e 2c 20 3c 2c 20 3e 3d 20 6f 72 20 3c 3d 29  (>, <, >= or <=)
2583a 20 6f 6e 20 74 68 65 20 69 6e 64 65 78 65 64 0a   on the indexed.
2583b 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20        **        
2583c 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 6d 6d   column that imm
2583d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 73  ediately follows
2583e 20 74 68 65 20 4e 20 65 71 75 61 6c 69 74 69 65   the N equalitie
2583f 73 2e 20 4f 6e 6c 79 20 0a 20 20 20 20 20 20 2a  s. Only .      *
25840 2a 20 20 20 20 20 20 20 20 20 74 68 65 20 72 69  *         the ri
25841 67 68 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20  ght-most column 
25842 63 61 6e 20 62 65 20 61 6e 20 69 6e 65 71 75 61  can be an inequa
25843 6c 69 74 79 20 2d 20 74 68 65 20 72 65 73 74 20  lity - the rest 
25844 6d 75 73 74 0a 20 20 20 20 20 20 2a 2a 20 20 20  must.      **   
25845 20 20 20 20 20 20 75 73 65 20 74 68 65 20 22 3d        use the "=
25846 3d 22 20 61 6e 64 20 22 49 4e 22 20 6f 70 65 72  =" and "IN" oper
25847 61 74 6f 72 73 2e 20 46 6f 72 20 65 78 61 6d 70  ators. For examp
25848 6c 65 2c 20 69 66 20 74 68 65 20 0a 20 20 20 20  le, if the .    
25849 20 20 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 64    **         ind
2584a 65 78 20 69 73 20 6f 6e 20 28 78 2c 79 2c 7a 29  ex is on (x,y,z)
2584b 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f  , then the follo
2584c 77 69 6e 67 20 63 6c 61 75 73 65 73 20 61 72 65  wing clauses are
2584d 20 61 6c 6c 20 0a 20 20 20 20 20 20 2a 2a 20 20   all .      **  
2584e 20 20 20 20 20 20 20 6f 70 74 69 6d 69 7a 65 64         optimized
2584f 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  :.      **.     
25850 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 78   **            x
25851 3d 35 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20  =5.      **     
25852 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20 79         x=5 AND y
25853 3d 31 30 0a 20 20 20 20 20 20 2a 2a 20 20 20 20  =10.      **    
25854 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20          x=5 AND 
25855 79 3c 31 30 0a 20 20 20 20 20 20 2a 2a 20 20 20  y<10.      **   
25856 20 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44           x=5 AND
25857 20 79 3e 35 20 41 4e 44 20 79 3c 31 30 0a 20 20   y>5 AND y<10.  
25858 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
25859 20 20 78 3d 35 20 41 4e 44 20 79 3d 35 20 41 4e    x=5 AND y=5 AN
2585a 44 20 7a 3c 3d 31 30 0a 20 20 20 20 20 20 2a 2a  D z<=10.      **
2585b 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20  .      **       
2585c 20 20 54 68 65 20 7a 3c 31 30 20 74 65 72 6d 20    The z<10 term 
2585d 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
2585e 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 2c   cannot be used,
2585f 20 6f 6e 6c 79 0a 20 20 20 20 20 20 2a 2a 20 20   only.      **  
25860 20 20 20 20 20 20 20 74 68 65 20 78 3d 35 20 74         the x=5 t
25861 65 72 6d 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20  erm:.      **.  
25862 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
25863 20 20 78 3d 35 20 41 4e 44 20 7a 3c 31 30 0a 20    x=5 AND z<10. 
25864 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
25865 20 20 20 20 20 20 20 20 20 4e 20 6d 61 79 20 62           N may b
25866 65 20 7a 65 72 6f 20 69 66 20 74 68 65 72 65 20  e zero if there 
25867 61 72 65 20 69 6e 65 71 75 61 6c 69 74 79 20 63  are inequality c
25868 6f 6e 73 74 72 61 69 6e 74 73 2e 0a 20 20 20 20  onstraints..    
25869 20 20 2a 2a 20 20 20 20 20 20 20 20 20 49 66 20    **         If 
2586a 74 68 65 72 65 20 61 72 65 20 6e 6f 20 69 6e 65  there are no ine
2586b 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69  quality constrai
2586c 6e 74 73 2c 20 74 68 65 6e 20 4e 20 69 73 20 61  nts, then N is a
2586d 74 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20  t.      **      
2586e 20 20 20 6c 65 61 73 74 20 6f 6e 65 2e 0a 20 20     least one..  
2586f 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
25870 20 20 20 20 20 20 20 20 54 68 69 73 20 63 61 73          This cas
25871 65 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20 77  e is also used w
25872 68 65 6e 20 74 68 65 72 65 20 61 72 65 20 6e 6f  hen there are no
25873 20 57 48 45 52 45 20 63 6c 61 75 73 65 0a 20 20   WHERE clause.  
25874 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 63      **         c
25875 6f 6e 73 74 72 61 69 6e 74 73 20 62 75 74 20 61  onstraints but a
25876 6e 20 69 6e 64 65 78 20 69 73 20 73 65 6c 65 63  n index is selec
25877 74 65 64 20 61 6e 79 77 61 79 2c 20 69 6e 20 6f  ted anyway, in o
25878 72 64 65 72 0a 20 20 20 20 20 20 2a 2a 20 20 20  rder.      **   
25879 20 20 20 20 20 20 74 6f 20 66 6f 72 63 65 20 74        to force t
2587a 68 65 20 6f 75 74 70 75 74 20 6f 72 64 65 72 20  he output order 
2587b 74 6f 20 63 6f 6e 66 6f 72 6d 20 74 6f 20 61 6e  to conform to an
2587c 20 4f 52 44 45 52 20 42 59 2e 0a 20 20 20 20 20   ORDER BY..     
2587d 20 2a 2f 20 20 0a 20 20 20 20 20 20 69 6e 74 20   */  .      int 
2587e 61 53 74 61 72 74 4f 70 5b 5d 20 3d 20 7b 0a 20  aStartOp[] = {. 
2587f 20 20 20 20 20 20 20 30 2c 0a 20 20 20 20 20 20         0,.      
25880 20 20 30 2c 0a 20 20 20 20 20 20 20 20 4f 50 5f    0,.        OP_
25881 52 65 77 69 6e 64 2c 20 20 20 20 20 20 20 20 20  Rewind,         
25882 20 20 2f 2a 20 32 3a 20 28 21 73 74 61 72 74 5f    /* 2: (!start_
25883 63 6f 6e 73 74 72 61 69 6e 74 73 20 26 26 20 73  constraints && s
25884 74 61 72 74 45 71 20 26 26 20 20 21 62 52 65 76  tartEq &&  !bRev
25885 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 4f 50 5f  ) */.        OP_
25886 4c 61 73 74 2c 20 20 20 20 20 20 20 20 20 20 20  Last,           
25887 20 20 2f 2a 20 33 3a 20 28 21 73 74 61 72 74 5f    /* 3: (!start_
25888 63 6f 6e 73 74 72 61 69 6e 74 73 20 26 26 20 73  constraints && s
25889 74 61 72 74 45 71 20 26 26 20 20 20 62 52 65 76  tartEq &&   bRev
2588a 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 4f 50 5f  ) */.        OP_
2588b 4d 6f 76 65 47 74 2c 20 20 20 20 20 20 20 20 20  MoveGt,         
2588c 20 20 2f 2a 20 34 3a 20 28 73 74 61 72 74 5f 63    /* 4: (start_c
2588d 6f 6e 73 74 72 61 69 6e 74 73 20 20 26 26 20 21  onstraints  && !
2588e 73 74 61 72 74 45 71 20 26 26 20 21 62 52 65 76  startEq && !bRev
2588f 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 4f 50 5f  ) */.        OP_
25890 4d 6f 76 65 4c 74 2c 20 20 20 20 20 20 20 20 20  MoveLt,         
25891 20 20 2f 2a 20 35 3a 20 28 73 74 61 72 74 5f 63    /* 5: (start_c
25892 6f 6e 73 74 72 61 69 6e 74 73 20 20 26 26 20 21  onstraints  && !
25893 73 74 61 72 74 45 71 20 26 26 20 20 62 52 65 76  startEq &&  bRev
25894 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 4f 50 5f  ) */.        OP_
25895 4d 6f 76 65 47 65 2c 20 20 20 20 20 20 20 20 20  MoveGe,         
25896 20 20 2f 2a 20 36 3a 20 28 73 74 61 72 74 5f 63    /* 6: (start_c
25897 6f 6e 73 74 72 61 69 6e 74 73 20 20 26 26 20 20  onstraints  &&  
25898 73 74 61 72 74 45 71 20 26 26 20 21 62 52 65 76  startEq && !bRev
25899 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 4f 50 5f  ) */.        OP_
2589a 4d 6f 76 65 4c 65 20 20 20 20 20 20 20 20 20 20  MoveLe          
2589b 20 20 2f 2a 20 37 3a 20 28 73 74 61 72 74 5f 63    /* 7: (start_c
2589c 6f 6e 73 74 72 61 69 6e 74 73 20 20 26 26 20 20  onstraints  &&  
2589d 73 74 61 72 74 45 71 20 26 26 20 20 62 52 65 76  startEq &&  bRev
2589e 29 20 2a 2f 0a 20 20 20 20 20 20 7d 3b 0a 20 20  ) */.      };.  
2589f 20 20 20 20 69 6e 74 20 61 45 6e 64 4f 70 5b 5d      int aEndOp[]
258a0 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 4f 50 5f   = {.        OP_
258a1 4e 6f 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20  Noop,           
258a2 20 20 2f 2a 20 30 3a 20 28 21 65 6e 64 5f 63 6f    /* 0: (!end_co
258a3 6e 73 74 72 61 69 6e 74 73 29 20 2a 2f 0a 20 20  nstraints) */.  
258a4 20 20 20 20 20 20 4f 50 5f 49 64 78 47 45 2c 20        OP_IdxGE, 
258a5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 3a             /* 1:
258a6 20 28 65 6e 64 5f 63 6f 6e 73 74 72 61 69 6e 74   (end_constraint
258a7 73 20 26 26 20 21 62 52 65 76 29 20 2a 2f 0a 20  s && !bRev) */. 
258a8 20 20 20 20 20 20 20 4f 50 5f 49 64 78 4c 54 20         OP_IdxLT 
258a9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32              /* 2
258aa 3a 20 28 65 6e 64 5f 63 6f 6e 73 74 72 61 69 6e  : (end_constrain
258ab 74 73 20 26 26 20 62 52 65 76 29 20 2a 2f 0a 20  ts && bRev) */. 
258ac 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 69 6e       };.      in
258ad 74 20 6e 45 71 20 3d 20 70 4c 65 76 65 6c 2d 3e  t nEq = pLevel->
258ae 6e 45 71 3b 0a 20 20 20 20 20 20 69 6e 74 20 69  nEq;.      int i
258af 73 4d 69 6e 51 75 65 72 79 20 3d 20 30 3b 20 20  sMinQuery = 0;  
258b0 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68          /* If th
258b1 69 73 20 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a  is is an optimiz
258b2 65 64 20 53 45 4c 45 43 54 20 6d 69 6e 28 78 29  ed SELECT min(x)
258b3 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  .. */.      int 
258b4 72 65 67 42 61 73 65 3b 20 20 20 20 20 20 20 20  regBase;        
258b5 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65           /* Base
258b6 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   register holdin
258b7 67 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 61 6c  g constraint val
258b8 75 65 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ues */.      int
258b9 20 72 31 3b 20 20 20 20 20 20 20 20 20 20 20 20   r1;            
258ba 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d            /* Tem
258bb 70 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20  p register */.  
258bc 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70      WhereTerm *p
258bd 52 61 6e 67 65 53 74 61 72 74 20 3d 20 30 3b 20  RangeStart = 0; 
258be 20 2f 2a 20 49 6e 65 71 75 61 6c 69 74 79 20 63   /* Inequality c
258bf 6f 6e 73 74 72 61 69 6e 74 20 61 74 20 72 61 6e  onstraint at ran
258c0 67 65 20 73 74 61 72 74 20 2a 2f 0a 20 20 20 20  ge start */.    
258c1 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 52 61    WhereTerm *pRa
258c2 6e 67 65 45 6e 64 20 3d 20 30 3b 20 20 20 20 2f  ngeEnd = 0;    /
258c3 2a 20 49 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e  * Inequality con
258c4 73 74 72 61 69 6e 74 20 61 74 20 72 61 6e 67 65  straint at range
258c5 20 65 6e 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e   end */.      in
258c6 74 20 73 74 61 72 74 45 71 3b 20 20 20 20 20 20  t startEq;      
258c7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
258c8 75 65 20 69 66 20 72 61 6e 67 65 20 73 74 61 72  ue if range star
258c9 74 20 75 73 65 73 20 3d 3d 2c 20 3e 3d 20 6f 72  t uses ==, >= or
258ca 20 3c 3d 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74   <= */.      int
258cb 20 65 6e 64 45 71 3b 20 20 20 20 20 20 20 20 20   endEq;         
258cc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
258cd 65 20 69 66 20 72 61 6e 67 65 20 65 6e 64 20 75  e if range end u
258ce 73 65 73 20 3d 3d 2c 20 3e 3d 20 6f 72 20 3c 3d  ses ==, >= or <=
258cf 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 73 74   */.      int st
258d0 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 3b  art_constraints;
258d1 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 20         /* Start 
258d2 6f 66 20 72 61 6e 67 65 20 69 73 20 63 6f 6e 73  of range is cons
258d3 74 72 61 69 6e 65 64 20 2a 2f 0a 20 20 20 20 20  trained */.     
258d4 20 69 6e 74 20 6b 20 3d 20 70 49 64 78 2d 3e 61   int k = pIdx->a
258d5 69 43 6f 6c 75 6d 6e 5b 6e 45 71 5d 3b 20 2f 2a  iColumn[nEq]; /*
258d6 20 43 6f 6c 75 6d 6e 20 66 6f 72 20 69 6e 65 71   Column for ineq
258d7 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e  uality constrain
258d8 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  ts */.      int 
258d9 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20  nConstraint;    
258da 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
258db 65 72 20 6f 66 20 63 6f 6e 73 74 72 61 69 6e 74  er of constraint
258dc 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20 20 20   terms */.      
258dd 69 6e 74 20 6f 70 3b 0a 0a 20 20 20 20 20 20 2f  int op;..      /
258de 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
258df 74 6f 20 65 76 61 6c 75 61 74 65 20 61 6c 6c 20  to evaluate all 
258e0 63 6f 6e 73 74 72 61 69 6e 74 20 74 65 72 6d 73  constraint terms
258e1 20 75 73 69 6e 67 20 3d 3d 20 6f 72 20 49 4e 0a   using == or IN.
258e2 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 73 74 6f        ** and sto
258e3 72 65 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66  re the values of
258e4 20 74 68 6f 73 65 20 74 65 72 6d 73 20 69 6e 20   those terms in 
258e5 61 6e 20 61 72 72 61 79 20 6f 66 20 72 65 67 69  an array of regi
258e6 73 74 65 72 73 0a 20 20 20 20 20 20 2a 2a 20 73  sters.      ** s
258e7 74 61 72 74 69 6e 67 20 61 74 20 72 65 67 42 61  tarting at regBa
258e8 73 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  se..      */.   
258e9 20 20 20 72 65 67 42 61 73 65 20 3d 20 63 6f 64     regBase = cod
258ea 65 41 6c 6c 45 71 75 61 6c 69 74 79 54 65 72 6d  eAllEqualityTerm
258eb 73 28 70 50 61 72 73 65 2c 20 70 4c 65 76 65 6c  s(pParse, pLevel
258ec 2c 20 26 77 63 2c 20 6e 6f 74 52 65 61 64 79 2c  , &wc, notReady,
258ed 20 32 29 3b 0a 20 20 20 20 20 20 6e 78 74 20 3d   2);.      nxt =
258ee 20 70 4c 65 76 65 6c 2d 3e 6e 78 74 3b 0a 0a 20   pLevel->nxt;.. 
258ef 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20       /* If this 
258f0 6c 6f 6f 70 20 73 61 74 69 73 66 69 65 73 20 61  loop satisfies a
258f1 20 73 6f 72 74 20 6f 72 64 65 72 20 28 70 4f 72   sort order (pOr
258f2 64 65 72 42 79 29 20 72 65 71 75 65 73 74 20 74  derBy) request t
258f3 68 61 74 20 0a 20 20 20 20 20 20 2a 2a 20 77 61  hat .      ** wa
258f4 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73  s passed to this
258f5 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6d 70   function to imp
258f6 6c 65 6d 65 6e 74 20 61 20 22 53 45 4c 45 43 54  lement a "SELECT
258f7 20 6d 69 6e 28 78 29 20 2e 2e 2e 22 20 0a 20 20   min(x) ..." .  
258f8 20 20 20 20 2a 2a 20 71 75 65 72 79 2c 20 74 68      ** query, th
258f9 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 77 69  en the caller wi
258fa 6c 6c 20 6f 6e 6c 79 20 61 6c 6c 6f 77 20 74 68  ll only allow th
258fb 65 20 6c 6f 6f 70 20 74 6f 20 72 75 6e 20 66 6f  e loop to run fo
258fc 72 0a 20 20 20 20 20 20 2a 2a 20 61 20 73 69 6e  r.      ** a sin
258fd 67 6c 65 20 69 74 65 72 61 74 69 6f 6e 2e 20 54  gle iteration. T
258fe 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74  his means that t
258ff 68 65 20 66 69 72 73 74 20 72 6f 77 20 72 65 74  he first row ret
25900 75 72 6e 65 64 0a 20 20 20 20 20 20 2a 2a 20 73  urned.      ** s
25901 68 6f 75 6c 64 20 6e 6f 74 20 68 61 76 65 20 61  hould not have a
25902 20 4e 55 4c 4c 20 76 61 6c 75 65 20 73 74 6f 72   NULL value stor
25903 65 64 20 69 6e 20 27 78 27 2e 20 49 66 20 63 6f  ed in 'x'. If co
25904 6c 75 6d 6e 20 27 78 27 20 69 73 0a 20 20 20 20  lumn 'x' is.    
25905 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 6f    ** the first o
25906 6e 65 20 61 66 74 65 72 20 74 68 65 20 6e 45 71  ne after the nEq
25907 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72   equality constr
25908 61 69 6e 74 73 20 69 6e 20 74 68 65 20 69 6e 64  aints in the ind
25909 65 78 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 69  ex,.      ** thi
2590a 73 20 72 65 71 75 69 72 65 73 20 73 6f 6d 65 20  s requires some 
2590b 73 70 65 63 69 61 6c 20 68 61 6e 64 6c 69 6e 67  special handling
2590c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
2590d 20 69 66 28 20 28 77 66 6c 61 67 73 26 57 48 45   if( (wflags&WHE
2590e 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 29 21  RE_ORDERBY_MIN)!
2590f 3d 30 0a 20 20 20 20 20 20 20 26 26 20 28 70 4c  =0.       && (pL
25910 65 76 65 6c 2d 3e 66 6c 61 67 73 26 57 48 45 52  evel->flags&WHER
25911 45 5f 4f 52 44 45 52 42 59 29 0a 20 20 20 20 20  E_ORDERBY).     
25912 20 20 26 26 20 28 70 49 64 78 2d 3e 6e 43 6f 6c    && (pIdx->nCol
25913 75 6d 6e 3e 6e 45 71 29 0a 20 20 20 20 20 20 20  umn>nEq).       
25914 26 26 20 28 70 4f 72 64 65 72 42 79 2d 3e 61 5b  && (pOrderBy->a[
25915 30 5d 2e 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d  0].pExpr->iColum
25916 6e 3d 3d 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d  n==pIdx->aiColum
25917 6e 5b 6e 45 71 5d 29 0a 20 20 20 20 20 20 29 7b  n[nEq]).      ){
25918 0a 20 20 20 20 20 20 20 20 69 73 4d 69 6e 51 75  .        isMinQu
25919 65 72 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  ery = 1;.      }
2591a 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 20  ..      /* Find 
2591b 61 6e 79 20 69 6e 65 71 75 61 6c 69 74 79 20 63  any inequality c
2591c 6f 6e 73 74 72 61 69 6e 74 20 74 65 72 6d 73 20  onstraint terms 
2591d 66 6f 72 20 74 68 65 20 73 74 61 72 74 20 61 6e  for the start an
2591e 64 20 65 6e 64 20 0a 20 20 20 20 20 20 2a 2a 20  d end .      ** 
2591f 6f 66 20 74 68 65 20 72 61 6e 67 65 2e 20 0a 20  of the range. . 
25920 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
25921 28 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20  ( pLevel->flags 
25922 26 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49  & WHERE_TOP_LIMI
25923 54 20 29 7b 0a 20 20 20 20 20 20 20 20 70 52 61  T ){.        pRa
25924 6e 67 65 45 6e 64 20 3d 20 66 69 6e 64 54 65 72  ngeEnd = findTer
25925 6d 28 26 77 63 2c 20 69 43 75 72 2c 20 6b 2c 20  m(&wc, iCur, k, 
25926 6e 6f 74 52 65 61 64 79 2c 20 28 57 4f 5f 4c 54  notReady, (WO_LT
25927 7c 57 4f 5f 4c 45 29 2c 20 70 49 64 78 29 3b 0a  |WO_LE), pIdx);.
25928 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
25929 28 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20  ( pLevel->flags 
2592a 26 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49  & WHERE_BTM_LIMI
2592b 54 20 29 7b 0a 20 20 20 20 20 20 20 20 70 52 61  T ){.        pRa
2592c 6e 67 65 53 74 61 72 74 20 3d 20 66 69 6e 64 54  ngeStart = findT
2592d 65 72 6d 28 26 77 63 2c 20 69 43 75 72 2c 20 6b  erm(&wc, iCur, k
2592e 2c 20 6e 6f 74 52 65 61 64 79 2c 20 28 57 4f 5f  , notReady, (WO_
2592f 47 54 7c 57 4f 5f 47 45 29 2c 20 70 49 64 78 29  GT|WO_GE), pIdx)
25930 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
25931 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 64 6f   /* If we are do
25932 69 6e 67 20 61 20 72 65 76 65 72 73 65 20 6f 72  ing a reverse or
25933 64 65 72 20 73 63 61 6e 20 6f 6e 20 61 6e 20 61  der scan on an a
25934 73 63 65 6e 64 69 6e 67 20 69 6e 64 65 78 2c 20  scending index, 
25935 6f 72 0a 20 20 20 20 20 20 2a 2a 20 61 20 66 6f  or.      ** a fo
25936 72 77 61 72 64 20 6f 72 64 65 72 20 73 63 61 6e  rward order scan
25937 20 6f 6e 20 61 20 64 65 73 63 65 6e 64 69 6e 67   on a descending
25938 20 69 6e 64 65 78 2c 20 69 6e 74 65 72 63 68 61   index, intercha
25939 6e 67 65 20 74 68 65 20 0a 20 20 20 20 20 20 2a  nge the .      *
2593a 2a 20 73 74 61 72 74 20 61 6e 64 20 65 6e 64 20  * start and end 
2593b 74 65 72 6d 73 20 28 70 52 61 6e 67 65 53 74 61  terms (pRangeSta
2593c 72 74 20 61 6e 64 20 70 52 61 6e 67 65 45 6e 64  rt and pRangeEnd
2593d 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  )..      */.    
2593e 20 20 69 66 28 20 62 52 65 76 3d 3d 28 70 49 64    if( bRev==(pId
2593f 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6e 45  x->aSortOrder[nE
25940 71 5d 3d 3d 53 51 4c 49 54 45 5f 53 4f 5f 41 53  q]==SQLITE_SO_AS
25941 43 29 20 29 7b 0a 20 20 20 20 20 20 20 20 53 57  C) ){.        SW
25942 41 50 28 57 68 65 72 65 54 65 72 6d 20 2a 2c 20  AP(WhereTerm *, 
25943 70 52 61 6e 67 65 45 6e 64 2c 20 70 52 61 6e 67  pRangeEnd, pRang
25944 65 53 74 61 72 74 29 3b 0a 20 20 20 20 20 20 7d  eStart);.      }
25945 0a 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
25946 28 20 70 52 61 6e 67 65 53 74 61 72 74 20 26 26  ( pRangeStart &&
25947 20 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 65 4f   pRangeStart->eO
25948 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 4c 45 20  perator & WO_LE 
25949 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
2594a 65 28 20 70 52 61 6e 67 65 53 74 61 72 74 20 26  e( pRangeStart &
2594b 26 20 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 65  & pRangeStart->e
2594c 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 47 45  Operator & WO_GE
2594d 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
2594e 73 65 28 20 70 52 61 6e 67 65 45 6e 64 20 26 26  se( pRangeEnd &&
2594f 20 70 52 61 6e 67 65 45 6e 64 2d 3e 65 4f 70 65   pRangeEnd->eOpe
25950 72 61 74 6f 72 20 26 20 57 4f 5f 4c 45 20 29 3b  rator & WO_LE );
25951 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
25952 20 70 52 61 6e 67 65 45 6e 64 20 26 26 20 70 52   pRangeEnd && pR
25953 61 6e 67 65 45 6e 64 2d 3e 65 4f 70 65 72 61 74  angeEnd->eOperat
25954 6f 72 20 26 20 57 4f 5f 47 45 20 29 3b 0a 20 20  or & WO_GE );.  
25955 20 20 20 20 73 74 61 72 74 45 71 20 3d 20 21 70      startEq = !p
25956 52 61 6e 67 65 53 74 61 72 74 20 7c 7c 20 70 52  RangeStart || pR
25957 61 6e 67 65 53 74 61 72 74 2d 3e 65 4f 70 65 72  angeStart->eOper
25958 61 74 6f 72 20 26 20 28 57 4f 5f 4c 45 7c 57 4f  ator & (WO_LE|WO
25959 5f 47 45 29 3b 0a 20 20 20 20 20 20 65 6e 64 45  _GE);.      endE
2595a 71 20 3d 20 20 20 21 70 52 61 6e 67 65 45 6e 64  q =   !pRangeEnd
2595b 20 7c 7c 20 70 52 61 6e 67 65 45 6e 64 2d 3e 65   || pRangeEnd->e
2595c 4f 70 65 72 61 74 6f 72 20 26 20 28 57 4f 5f 4c  Operator & (WO_L
2595d 45 7c 57 4f 5f 47 45 29 3b 0a 20 20 20 20 20 20  E|WO_GE);.      
2595e 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74  start_constraint
2595f 73 20 3d 20 70 52 61 6e 67 65 53 74 61 72 74 20  s = pRangeStart 
25960 7c 7c 20 6e 45 71 3e 30 3b 0a 0a 20 20 20 20 20  || nEq>0;..     
25961 20 2f 2a 20 53 65 65 6b 20 74 68 65 20 69 6e 64   /* Seek the ind
25962 65 78 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65  ex cursor to the
25963 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 72 61   start of the ra
25964 6e 67 65 2e 20 2a 2f 0a 20 20 20 20 20 20 6e 43  nge. */.      nC
25965 6f 6e 73 74 72 61 69 6e 74 20 3d 20 6e 45 71 3b  onstraint = nEq;
25966 0a 20 20 20 20 20 20 69 66 28 20 70 52 61 6e 67  .      if( pRang
25967 65 53 74 61 72 74 20 29 7b 0a 20 20 20 20 20 20  eStart ){.      
25968 20 20 69 6e 74 20 64 63 63 20 3d 20 70 50 61 72    int dcc = pPar
25969 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61  se->disableColCa
2596a 63 68 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28  che;.        if(
2596b 20 70 52 61 6e 67 65 45 6e 64 20 29 7b 0a 20 20   pRangeEnd ){.  
2596c 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e          pParse->
2596d 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 20  disableColCache 
2596e 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 1;.        }. 
2596f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
25970 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
25971 52 61 6e 67 65 53 74 61 72 74 2d 3e 70 45 78 70  RangeStart->pExp
25972 72 2d 3e 70 52 69 67 68 74 2c 20 72 65 67 42 61  r->pRight, regBa
25973 73 65 2b 6e 45 71 29 3b 0a 20 20 20 20 20 20 20  se+nEq);.       
25974 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65   pParse->disable
25975 43 6f 6c 43 61 63 68 65 20 3d 20 64 63 63 3b 0a  ColCache = dcc;.
25976 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
25977 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
25978 49 73 4e 75 6c 6c 2c 20 72 65 67 42 61 73 65 2b  IsNull, regBase+
25979 6e 45 71 2c 20 6e 78 74 29 3b 0a 20 20 20 20 20  nEq, nxt);.     
2597a 20 20 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 2b     nConstraint++
2597b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
2597c 28 20 69 73 4d 69 6e 51 75 65 72 79 20 29 7b 0a  ( isMinQuery ){.
2597d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2597e 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2597f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 42 61 73 65  Null, 0, regBase
25980 2b 6e 45 71 29 3b 0a 20 20 20 20 20 20 20 20 6e  +nEq);.        n
25981 43 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b 0a 20 20  Constraint++;.  
25982 20 20 20 20 20 20 73 74 61 72 74 45 71 20 3d 20        startEq = 
25983 30 3b 0a 20 20 20 20 20 20 20 20 73 74 61 72 74  0;.        start
25984 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 3d 20 31  _constraints = 1
25985 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
25986 63 6f 64 65 41 70 70 6c 79 41 66 66 69 6e 69 74  codeApplyAffinit
25987 79 28 70 50 61 72 73 65 2c 20 72 65 67 42 61 73  y(pParse, regBas
25988 65 2c 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2c 20  e, nConstraint, 
25989 70 49 64 78 29 3b 0a 20 20 20 20 20 20 6f 70 20  pIdx);.      op 
2598a 3d 20 61 53 74 61 72 74 4f 70 5b 28 73 74 61 72  = aStartOp[(star
2598b 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 3c 3c 32  t_constraints<<2
2598c 29 20 2b 20 28 73 74 61 72 74 45 71 3c 3c 31 29  ) + (startEq<<1)
2598d 20 2b 20 62 52 65 76 5d 3b 0a 20 20 20 20 20 20   + bRev];.      
2598e 61 73 73 65 72 74 28 20 6f 70 21 3d 30 20 29 3b  assert( op!=0 );
2598f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
25990 20 6f 70 3d 3d 4f 50 5f 52 65 77 69 6e 64 20 29   op==OP_Rewind )
25991 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
25992 28 20 6f 70 3d 3d 4f 50 5f 4c 61 73 74 20 29 3b  ( op==OP_Last );
25993 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
25994 20 6f 70 3d 3d 4f 50 5f 4d 6f 76 65 47 74 20 29   op==OP_MoveGt )
25995 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
25996 28 20 6f 70 3d 3d 4f 50 5f 4d 6f 76 65 47 65 20  ( op==OP_MoveGe 
25997 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
25998 65 28 20 6f 70 3d 3d 4f 50 5f 4d 6f 76 65 4c 65  e( op==OP_MoveLe
25999 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
2599a 73 65 28 20 6f 70 3d 3d 4f 50 5f 4d 6f 76 65 4c  se( op==OP_MoveL
2599b 74 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  t );.      sqlit
2599c 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2599d 6f 70 2c 20 69 49 64 78 43 75 72 2c 20 6e 78 74  op, iIdxCur, nxt
2599e 2c 20 72 65 67 42 61 73 65 2c 20 0a 20 20 20 20  , regBase, .    
2599f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
259a0 20 20 20 20 28 63 68 61 72 2a 29 6e 43 6f 6e 73      (char*)nCons
259a1 74 72 61 69 6e 74 2c 20 50 34 5f 49 4e 54 33 32  traint, P4_INT32
259a2 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4c 6f 61  );..      /* Loa
259a3 64 20 74 68 65 20 76 61 6c 75 65 20 66 6f 72 20  d the value for 
259a4 74 68 65 20 69 6e 65 71 75 61 6c 69 74 79 20 63  the inequality c
259a5 6f 6e 73 74 72 61 69 6e 74 20 61 74 20 74 68 65  onstraint at the
259a6 20 65 6e 64 20 6f 66 20 74 68 65 0a 20 20 20 20   end of the.    
259a7 20 20 2a 2a 20 72 61 6e 67 65 20 28 69 66 20 61    ** range (if a
259a8 6e 79 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ny)..      */.  
259a9 20 20 20 20 6e 43 6f 6e 73 74 72 61 69 6e 74 20      nConstraint 
259aa 3d 20 6e 45 71 3b 0a 20 20 20 20 20 20 69 66 28  = nEq;.      if(
259ab 20 70 52 61 6e 67 65 45 6e 64 20 29 7b 0a 20 20   pRangeEnd ){.  
259ac 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
259ad 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 52  rCode(pParse, pR
259ae 61 6e 67 65 45 6e 64 2d 3e 70 45 78 70 72 2d 3e  angeEnd->pExpr->
259af 70 52 69 67 68 74 2c 20 72 65 67 42 61 73 65 2b  pRight, regBase+
259b0 6e 45 71 29 3b 0a 20 20 20 20 20 20 20 20 73 71  nEq);.        sq
259b1 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
259b2 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65  v, OP_IsNull, re
259b3 67 42 61 73 65 2b 6e 45 71 2c 20 6e 78 74 29 3b  gBase+nEq, nxt);
259b4 0a 20 20 20 20 20 20 20 20 63 6f 64 65 41 70 70  .        codeApp
259b5 6c 79 41 66 66 69 6e 69 74 79 28 70 50 61 72 73  lyAffinity(pPars
259b6 65 2c 20 72 65 67 42 61 73 65 2c 20 6e 45 71 2b  e, regBase, nEq+
259b7 31 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20 20  1, pIdx);.      
259b8 20 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b    nConstraint++;
259b9 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
259ba 2f 2a 20 54 6f 70 20 6f 66 20 74 68 65 20 6c 6f  /* Top of the lo
259bb 6f 70 20 62 6f 64 79 20 2a 2f 0a 20 20 20 20 20  op body */.     
259bc 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 73 71   pLevel->p2 = sq
259bd 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
259be 41 64 64 72 28 76 29 3b 0a 0a 20 20 20 20 20 20  Addr(v);..      
259bf 2f 2a 20 43 68 65 63 6b 20 69 66 20 74 68 65 20  /* Check if the 
259c0 69 6e 64 65 78 20 63 75 72 73 6f 72 20 69 73 20  index cursor is 
259c1 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20  past the end of 
259c2 74 68 65 20 72 61 6e 67 65 2e 20 2a 2f 0a 20 20  the range. */.  
259c3 20 20 20 20 6f 70 20 3d 20 61 45 6e 64 4f 70 5b      op = aEndOp[
259c4 28 70 52 61 6e 67 65 45 6e 64 20 7c 7c 20 6e 45  (pRangeEnd || nE
259c5 71 29 20 2a 20 28 31 20 2b 20 62 52 65 76 29 5d  q) * (1 + bRev)]
259c6 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
259c7 28 20 6f 70 3d 3d 4f 50 5f 4e 6f 6f 70 20 29 3b  ( op==OP_Noop );
259c8 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
259c9 20 6f 70 3d 3d 4f 50 5f 49 64 78 47 45 20 29 3b   op==OP_IdxGE );
259ca 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
259cb 20 6f 70 3d 3d 4f 50 5f 49 64 78 4c 54 20 29 3b   op==OP_IdxLT );
259cc 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
259cd 62 65 41 64 64 4f 70 34 28 76 2c 20 6f 70 2c 20  beAddOp4(v, op, 
259ce 69 49 64 78 43 75 72 2c 20 6e 78 74 2c 20 72 65  iIdxCur, nxt, re
259cf 67 42 61 73 65 2c 0a 20 20 20 20 20 20 20 20 20  gBase,.         
259d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
259d1 63 68 61 72 2a 29 6e 43 6f 6e 73 74 72 61 69 6e  char*)nConstrain
259d2 74 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20  t, P4_INT32);.  
259d3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
259d4 68 61 6e 67 65 50 35 28 76 2c 20 65 6e 64 45 71  hangeP5(v, endEq
259d5 21 3d 62 52 65 76 29 3b 0a 0a 20 20 20 20 20 20  !=bRev);..      
259d6 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  /* If there are 
259d7 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74  inequality const
259d8 72 61 69 6e 74 73 2c 20 63 68 65 63 6b 20 74 68  raints, check th
259d9 61 74 20 74 68 65 20 76 61 6c 75 65 0a 20 20 20  at the value.   
259da 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 61 62     ** of the tab
259db 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74  le column that t
259dc 68 65 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f  he inequality co
259dd 6e 74 72 61 69 6e 73 20 69 73 20 6e 6f 74 20 4e  ntrains is not N
259de 55 4c 4c 2e 0a 20 20 20 20 20 20 2a 2a 20 49 66  ULL..      ** If
259df 20 69 74 20 69 73 2c 20 6a 75 6d 70 20 74 6f 20   it is, jump to 
259e0 74 68 65 20 6e 65 78 74 20 69 74 65 72 61 74 69  the next iterati
259e1 6f 6e 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 0a  on of the loop..
259e2 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72        */.      r
259e3 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  1 = sqlite3GetTe
259e4 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
259e5 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
259e6 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26 20 57  Level->flags & W
259e7 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 20 29  HERE_BTM_LIMIT )
259e8 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
259e9 28 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20  ( pLevel->flags 
259ea 26 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49  & WHERE_TOP_LIMI
259eb 54 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  T );.      if( p
259ec 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26 20 28  Level->flags & (
259ed 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 7c  WHERE_BTM_LIMIT|
259ee 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 29  WHERE_TOP_LIMIT)
259ef 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
259f0 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
259f1 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 49 64 78   OP_Column, iIdx
259f2 43 75 72 2c 20 6e 45 71 2c 20 72 31 29 3b 0a 20  Cur, nEq, r1);. 
259f3 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
259f4 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
259f5 73 4e 75 6c 6c 2c 20 72 31 2c 20 63 6f 6e 74 29  sNull, r1, cont)
259f6 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
259f7 20 2f 2a 20 53 65 65 6b 20 74 68 65 20 74 61 62   /* Seek the tab
259f8 6c 65 20 63 75 72 73 6f 72 2c 20 69 66 20 72 65  le cursor, if re
259f9 71 75 69 72 65 64 20 2a 2f 0a 20 20 20 20 20 20  quired */.      
259fa 69 66 28 20 21 6f 6d 69 74 54 61 62 6c 65 20 29  if( !omitTable )
259fb 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
259fc 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
259fd 50 5f 49 64 78 52 6f 77 69 64 2c 20 69 49 64 78  P_IdxRowid, iIdx
259fe 43 75 72 2c 20 72 31 29 3b 0a 20 20 20 20 20 20  Cur, r1);.      
259ff 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
25a00 4f 70 33 28 76 2c 20 4f 50 5f 4d 6f 76 65 47 65  Op3(v, OP_MoveGe
25a01 2c 20 69 43 75 72 2c 20 30 2c 20 72 31 29 3b 20  , iCur, 0, r1); 
25a02 20 2f 2a 20 44 65 66 65 72 72 65 64 20 73 65 65   /* Deferred see
25a03 6b 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20  k */.      }.   
25a04 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
25a05 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
25a06 20 72 31 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20   r1);..      /* 
25a07 52 65 63 6f 72 64 20 74 68 65 20 69 6e 73 74 72  Record the instr
25a08 75 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 74  uction used to t
25a09 65 72 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f  erminate the loo
25a0a 70 2e 20 44 69 73 61 62 6c 65 20 0a 20 20 20 20  p. Disable .    
25a0b 20 20 2a 2a 20 57 48 45 52 45 20 63 6c 61 75 73    ** WHERE claus
25a0c 65 20 74 65 72 6d 73 20 6d 61 64 65 20 72 65 64  e terms made red
25a0d 75 6e 64 61 6e 74 20 62 79 20 74 68 65 20 69 6e  undant by the in
25a0e 64 65 78 20 72 61 6e 67 65 20 73 63 61 6e 2e 0a  dex range scan..
25a0f 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70        */.      p
25a10 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 62 52 65 76  Level->op = bRev
25a11 20 3f 20 4f 50 5f 50 72 65 76 20 3a 20 4f 50 5f   ? OP_Prev : OP_
25a12 4e 65 78 74 3b 0a 20 20 20 20 20 20 70 4c 65 76  Next;.      pLev
25a13 65 6c 2d 3e 70 31 20 3d 20 69 49 64 78 43 75 72  el->p1 = iIdxCur
25a14 3b 0a 20 20 20 20 20 20 64 69 73 61 62 6c 65 54  ;.      disableT
25a15 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 52 61 6e  erm(pLevel, pRan
25a16 67 65 53 74 61 72 74 29 3b 0a 20 20 20 20 20 20  geStart);.      
25a17 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76  disableTerm(pLev
25a18 65 6c 2c 20 70 52 61 6e 67 65 45 6e 64 29 3b 0a  el, pRangeEnd);.
25a19 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
25a1a 20 2f 2a 20 43 61 73 65 20 34 3a 20 20 54 68 65   /* Case 4:  The
25a1b 72 65 20 69 73 20 6e 6f 20 75 73 61 62 6c 65 20  re is no usable 
25a1c 69 6e 64 65 78 2e 20 20 57 65 20 6d 75 73 74 20  index.  We must 
25a1d 64 6f 20 61 20 63 6f 6d 70 6c 65 74 65 0a 20 20  do a complete.  
25a1e 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
25a1f 73 63 61 6e 20 6f 66 20 74 68 65 20 65 6e 74 69  scan of the enti
25a20 72 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 20 20  re table..      
25a21 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
25a22 20 6f 6d 69 74 54 61 62 6c 65 3d 3d 30 20 29 3b   omitTable==0 );
25a23 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 62  .      assert( b
25a24 52 65 76 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  Rev==0 );.      
25a25 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f  pLevel->op = OP_
25a26 4e 65 78 74 3b 0a 20 20 20 20 20 20 70 4c 65 76  Next;.      pLev
25a27 65 6c 2d 3e 70 31 20 3d 20 69 43 75 72 3b 0a 20  el->p1 = iCur;. 
25a28 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 32 20       pLevel->p2 
25a29 3d 20 31 20 2b 20 73 71 6c 69 74 65 33 56 64 62  = 1 + sqlite3Vdb
25a2a 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
25a2b 77 69 6e 64 2c 20 69 43 75 72 2c 20 62 72 6b 29  wind, iCur, brk)
25a2c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 6f 74 52  ;.    }.    notR
25a2d 65 61 64 79 20 26 3d 20 7e 67 65 74 4d 61 73 6b  eady &= ~getMask
25a2e 28 26 6d 61 73 6b 53 65 74 2c 20 69 43 75 72 29  (&maskSet, iCur)
25a2f 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 73 65 72 74  ;..    /* Insert
25a30 20 63 6f 64 65 20 74 6f 20 74 65 73 74 20 65 76   code to test ev
25a31 65 72 79 20 73 75 62 65 78 70 72 65 73 73 69 6f  ery subexpressio
25a32 6e 20 74 68 61 74 20 63 61 6e 20 62 65 20 63 6f  n that can be co
25a33 6d 70 6c 65 74 65 6c 79 0a 20 20 20 20 2a 2a 20  mpletely.    ** 
25a34 63 6f 6d 70 75 74 65 64 20 75 73 69 6e 67 20 74  computed using t
25a35 68 65 20 63 75 72 72 65 6e 74 20 73 65 74 20 6f  he current set o
25a36 66 20 74 61 62 6c 65 73 2e 0a 20 20 20 20 2a 2f  f tables..    */
25a37 0a 20 20 20 20 66 6f 72 28 70 54 65 72 6d 3d 77  .    for(pTerm=w
25a38 63 2e 61 2c 20 6a 3d 77 63 2e 6e 54 65 72 6d 3b  c.a, j=wc.nTerm;
25a39 20 6a 3e 30 3b 20 6a 2d 2d 2c 20 70 54 65 72 6d   j>0; j--, pTerm
25a3a 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20  ++){.      Expr 
25a3b 2a 70 45 3b 0a 20 20 20 20 20 20 74 65 73 74 63  *pE;.      testc
25a3c 61 73 65 28 20 70 54 65 72 6d 2d 3e 66 6c 61 67  ase( pTerm->flag
25a3d 73 20 26 20 54 45 52 4d 5f 56 49 52 54 55 41 4c  s & TERM_VIRTUAL
25a3e 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
25a3f 73 65 28 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73  se( pTerm->flags
25a40 20 26 20 54 45 52 4d 5f 43 4f 44 45 44 20 29 3b   & TERM_CODED );
25a41 0a 20 20 20 20 20 20 69 66 28 20 70 54 65 72 6d  .      if( pTerm
25a42 2d 3e 66 6c 61 67 73 20 26 20 28 54 45 52 4d 5f  ->flags & (TERM_
25a43 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 43 4f 44  VIRTUAL|TERM_COD
25a44 45 44 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ED) ) continue;.
25a45 20 20 20 20 20 20 69 66 28 20 28 70 54 65 72 6d        if( (pTerm
25a46 2d 3e 70 72 65 72 65 71 41 6c 6c 20 26 20 6e 6f  ->prereqAll & no
25a47 74 52 65 61 64 79 29 21 3d 30 20 29 20 63 6f 6e  tReady)!=0 ) con
25a48 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 70 45 20  tinue;.      pE 
25a49 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a  = pTerm->pExpr;.
25a4a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45        assert( pE
25a4b 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28  !=0 );.      if(
25a4c 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f   pLevel->iLeftJo
25a4d 69 6e 20 26 26 20 21 45 78 70 72 48 61 73 50 72  in && !ExprHasPr
25a4e 6f 70 65 72 74 79 28 70 45 2c 20 45 50 5f 46 72  operty(pE, EP_Fr
25a4f 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 20 20  omJoin) ){.     
25a50 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
25a51 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
25a52 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50  e3ExprIfFalse(pP
25a53 61 72 73 65 2c 20 70 45 2c 20 63 6f 6e 74 2c 20  arse, pE, cont, 
25a54 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c  SQLITE_JUMPIFNUL
25a55 4c 29 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d  L);.      pTerm-
25a56 3e 66 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43  >flags |= TERM_C
25a57 4f 44 45 44 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ODED;.    }..   
25a58 20 2f 2a 20 46 6f 72 20 61 20 4c 45 46 54 20 4f   /* For a LEFT O
25a59 55 54 45 52 20 4a 4f 49 4e 2c 20 67 65 6e 65 72  UTER JOIN, gener
25a5a 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69  ate code that wi
25a5b 6c 6c 20 72 65 63 6f 72 64 20 74 68 65 20 66 61  ll record the fa
25a5c 63 74 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 61  ct that.    ** a
25a5d 74 20 6c 65 61 73 74 20 6f 6e 65 20 72 6f 77 20  t least one row 
25a5e 6f 66 20 74 68 65 20 72 69 67 68 74 20 74 61 62  of the right tab
25a5f 6c 65 20 68 61 73 20 6d 61 74 63 68 65 64 20 74  le has matched t
25a60 68 65 20 6c 65 66 74 20 74 61 62 6c 65 2e 20 20  he left table.  
25a61 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
25a62 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69  pLevel->iLeftJoi
25a63 6e 20 29 7b 0a 20 20 20 20 20 20 70 4c 65 76 65  n ){.      pLeve
25a64 6c 2d 3e 74 6f 70 20 3d 20 73 71 6c 69 74 65 33  l->top = sqlite3
25a65 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
25a66 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  v);.      sqlite
25a67 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
25a68 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 70 4c  P_Integer, 1, pL
25a69 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 29  evel->iLeftJoin)
25a6a 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d  ;.      VdbeComm
25a6b 65 6e 74 28 28 76 2c 20 22 72 65 63 6f 72 64 20  ent((v, "record 
25a6c 4c 45 46 54 20 4a 4f 49 4e 20 68 69 74 22 29 29  LEFT JOIN hit"))
25a6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
25a6e 78 70 72 43 6c 65 61 72 43 6f 6c 75 6d 6e 43 61  xprClearColumnCa
25a6f 63 68 65 28 70 50 61 72 73 65 2c 20 70 4c 65 76  che(pParse, pLev
25a70 65 6c 2d 3e 69 54 61 62 43 75 72 29 3b 0a 20 20  el->iTabCur);.  
25a71 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
25a72 6c 65 61 72 43 6f 6c 75 6d 6e 43 61 63 68 65 28  learColumnCache(
25a73 70 50 61 72 73 65 2c 20 70 4c 65 76 65 6c 2d 3e  pParse, pLevel->
25a74 69 49 64 78 43 75 72 29 3b 0a 20 20 20 20 20 20  iIdxCur);.      
25a75 66 6f 72 28 70 54 65 72 6d 3d 77 63 2e 61 2c 20  for(pTerm=wc.a, 
25a76 6a 3d 30 3b 20 6a 3c 77 63 2e 6e 54 65 72 6d 3b  j=0; j<wc.nTerm;
25a77 20 6a 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a   j++, pTerm++){.
25a78 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
25a79 28 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20 26  ( pTerm->flags &
25a7a 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 29 3b   TERM_VIRTUAL );
25a7b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
25a7c 65 28 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20  e( pTerm->flags 
25a7d 26 20 54 45 52 4d 5f 43 4f 44 45 44 20 29 3b 0a  & TERM_CODED );.
25a7e 20 20 20 20 20 20 20 20 69 66 28 20 70 54 65 72          if( pTer
25a7f 6d 2d 3e 66 6c 61 67 73 20 26 20 28 54 45 52 4d  m->flags & (TERM
25a80 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 43 4f  _VIRTUAL|TERM_CO
25a81 44 45 44 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b  DED) ) continue;
25a82 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 70 54  .        if( (pT
25a83 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 20 26  erm->prereqAll &
25a84 20 6e 6f 74 52 65 61 64 79 29 21 3d 30 20 29 20   notReady)!=0 ) 
25a85 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
25a86 20 20 61 73 73 65 72 74 28 20 70 54 65 72 6d 2d    assert( pTerm-
25a87 3e 70 45 78 70 72 20 29 3b 0a 20 20 20 20 20 20  >pExpr );.      
25a88 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46    sqlite3ExprIfF
25a89 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 54 65  alse(pParse, pTe
25a8a 72 6d 2d 3e 70 45 78 70 72 2c 20 63 6f 6e 74 2c  rm->pExpr, cont,
25a8b 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55   SQLITE_JUMPIFNU
25a8c 4c 4c 29 3b 0a 20 20 20 20 20 20 20 20 70 54 65  LL);.        pTe
25a8d 72 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 54 45 52  rm->flags |= TER
25a8e 4d 5f 43 4f 44 45 44 3b 0a 20 20 20 20 20 20 7d  M_CODED;.      }
25a8f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 23 69 66 64  .    }.  }..#ifd
25a90 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 20 20  ef SQLITE_TEST  
25a91 2f 2a 20 46 6f 72 20 74 65 73 74 69 6e 67 20 61  /* For testing a
25a92 6e 64 20 64 65 62 75 67 67 69 6e 67 20 75 73 65  nd debugging use
25a93 20 6f 6e 6c 79 20 2a 2f 0a 20 20 2f 2a 20 52 65   only */.  /* Re
25a94 63 6f 72 64 20 69 6e 20 74 68 65 20 71 75 65 72  cord in the quer
25a95 79 20 70 6c 61 6e 20 69 6e 66 6f 72 6d 61 74 69  y plan informati
25a96 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 63 75 72  on about the cur
25a97 72 65 6e 74 20 74 61 62 6c 65 0a 20 20 2a 2a 20  rent table.  ** 
25a98 61 6e 64 20 74 68 65 20 69 6e 64 65 78 20 75 73  and the index us
25a99 65 64 20 74 6f 20 61 63 63 65 73 73 20 69 74 20  ed to access it 
25a9a 28 69 66 20 61 6e 79 29 2e 20 20 49 66 20 74 68  (if any).  If th
25a9b 65 20 74 61 62 6c 65 20 69 74 73 65 6c 66 0a 20  e table itself. 
25a9c 20 2a 2a 20 69 73 20 6e 6f 74 20 75 73 65 64 2c   ** is not used,
25a9d 20 69 74 73 20 6e 61 6d 65 20 69 73 20 6a 75 73   its name is jus
25a9e 74 20 27 7b 7d 27 2e 20 20 49 66 20 6e 6f 20 69  t '{}'.  If no i
25a9f 6e 64 65 78 20 69 73 20 75 73 65 64 0a 20 20 2a  ndex is used.  *
25aa0 2a 20 74 68 65 20 69 6e 64 65 78 20 69 73 20 6c  * the index is l
25aa1 69 73 74 65 64 20 61 73 20 22 7b 7d 22 2e 20 20  isted as "{}".  
25aa2 49 66 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b  If the primary k
25aa3 65 79 20 69 73 20 75 73 65 64 20 74 68 65 0a 20  ey is used the. 
25aa4 20 2a 2a 20 69 6e 64 65 78 20 6e 61 6d 65 20 69   ** index name i
25aa5 73 20 27 2a 27 2e 0a 20 20 2a 2f 0a 20 20 66 6f  s '*'..  */.  fo
25aa6 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 4c 69 73  r(i=0; i<pTabLis
25aa7 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20  t->nSrc; i++){. 
25aa8 20 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20     char *z;.    
25aa9 69 6e 74 20 6e 3b 0a 20 20 20 20 70 4c 65 76 65  int n;.    pLeve
25aaa 6c 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 61 5b 69  l = &pWInfo->a[i
25aab 5d 3b 0a 20 20 20 20 70 54 61 62 49 74 65 6d 20  ];.    pTabItem 
25aac 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70  = &pTabList->a[p
25aad 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20  Level->iFrom];. 
25aae 20 20 20 7a 20 3d 20 70 54 61 62 49 74 65 6d 2d     z = pTabItem-
25aaf 3e 7a 41 6c 69 61 73 3b 0a 20 20 20 20 69 66 28  >zAlias;.    if(
25ab0 20 7a 3d 3d 30 20 29 20 7a 20 3d 20 70 54 61 62   z==0 ) z = pTab
25ab1 49 74 65 6d 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d  Item->pTab->zNam
25ab2 65 3b 0a 20 20 20 20 6e 20 3d 20 73 74 72 6c 65  e;.    n = strle
25ab3 6e 28 7a 29 3b 0a 20 20 20 20 69 66 28 20 6e 2b  n(z);.    if( n+
25ab4 6e 51 50 6c 61 6e 20 3c 20 73 69 7a 65 6f 66 28  nQPlan < sizeof(
25ab5 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c  sqlite3_query_pl
25ab6 61 6e 29 2d 31 30 20 29 7b 0a 20 20 20 20 20 20  an)-10 ){.      
25ab7 69 66 28 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67  if( pLevel->flag
25ab8 73 20 26 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e  s & WHERE_IDX_ON
25ab9 4c 59 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65  LY ){.        me
25aba 6d 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71 75  mcpy(&sqlite3_qu
25abb 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d  ery_plan[nQPlan]
25abc 2c 20 22 7b 7d 22 2c 20 32 29 3b 0a 20 20 20 20  , "{}", 2);.    
25abd 20 20 20 20 6e 51 50 6c 61 6e 20 2b 3d 20 32 3b      nQPlan += 2;
25abe 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
25abf 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 73 71        memcpy(&sq
25ac0 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e  lite3_query_plan
25ac1 5b 6e 51 50 6c 61 6e 5d 2c 20 7a 2c 20 6e 29 3b  [nQPlan], z, n);
25ac2 0a 20 20 20 20 20 20 20 20 6e 51 50 6c 61 6e 20  .        nQPlan 
25ac3 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  += n;.      }.  
25ac4 20 20 20 20 73 71 6c 69 74 65 33 5f 71 75 65 72      sqlite3_quer
25ac5 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 2b 2b 5d  y_plan[nQPlan++]
25ac6 20 3d 20 27 20 27 3b 0a 20 20 20 20 7d 0a 20 20   = ' ';.    }.  
25ac7 20 20 74 65 73 74 63 61 73 65 28 20 70 4c 65 76    testcase( pLev
25ac8 65 6c 2d 3e 66 6c 61 67 73 20 26 20 57 48 45 52  el->flags & WHER
25ac9 45 5f 52 4f 57 49 44 5f 45 51 20 29 3b 0a 20 20  E_ROWID_EQ );.  
25aca 20 20 74 65 73 74 63 61 73 65 28 20 70 4c 65 76    testcase( pLev
25acb 65 6c 2d 3e 66 6c 61 67 73 20 26 20 57 48 45 52  el->flags & WHER
25acc 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 20 29 3b  E_ROWID_RANGE );
25acd 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d  .    if( pLevel-
25ace 3e 66 6c 61 67 73 20 26 20 28 57 48 45 52 45 5f  >flags & (WHERE_
25acf 52 4f 57 49 44 5f 45 51 7c 57 48 45 52 45 5f 52  ROWID_EQ|WHERE_R
25ad0 4f 57 49 44 5f 52 41 4e 47 45 29 20 29 7b 0a 20  OWID_RANGE) ){. 
25ad1 20 20 20 20 20 6d 65 6d 63 70 79 28 26 73 71 6c       memcpy(&sql
25ad2 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b  ite3_query_plan[
25ad3 6e 51 50 6c 61 6e 5d 2c 20 22 2a 20 22 2c 20 32  nQPlan], "* ", 2
25ad4 29 3b 0a 20 20 20 20 20 20 6e 51 50 6c 61 6e 20  );.      nQPlan 
25ad5 2b 3d 20 32 3b 0a 20 20 20 20 7d 65 6c 73 65 20  += 2;.    }else 
25ad6 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 49 64 78  if( pLevel->pIdx
25ad7 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  ==0 ){.      mem
25ad8 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71 75 65  cpy(&sqlite3_que
25ad9 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c  ry_plan[nQPlan],
25ada 20 22 7b 7d 20 22 2c 20 33 29 3b 0a 20 20 20 20   "{} ", 3);.    
25adb 20 20 6e 51 50 6c 61 6e 20 2b 3d 20 33 3b 0a 20    nQPlan += 3;. 
25adc 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
25add 6e 20 3d 20 73 74 72 6c 65 6e 28 70 4c 65 76 65  n = strlen(pLeve
25ade 6c 2d 3e 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 3b  l->pIdx->zName);
25adf 0a 20 20 20 20 20 20 69 66 28 20 6e 2b 6e 51 50  .      if( n+nQP
25ae0 6c 61 6e 20 3c 20 73 69 7a 65 6f 66 28 73 71 6c  lan < sizeof(sql
25ae1 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 29  ite3_query_plan)
25ae2 2d 32 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65  -2 ){.        me
25ae3 6d 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71 75  mcpy(&sqlite3_qu
25ae4 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d  ery_plan[nQPlan]
25ae5 2c 20 70 4c 65 76 65 6c 2d 3e 70 49 64 78 2d 3e  , pLevel->pIdx->
25ae6 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20 20 20 20  zName, n);.     
25ae7 20 20 20 6e 51 50 6c 61 6e 20 2b 3d 20 6e 3b 0a     nQPlan += n;.
25ae8 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
25ae9 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61  query_plan[nQPla
25aea 6e 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20  n++] = ' ';.    
25aeb 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
25aec 77 68 69 6c 65 28 20 6e 51 50 6c 61 6e 3e 30 20  while( nQPlan>0 
25aed 26 26 20 73 71 6c 69 74 65 33 5f 71 75 65 72 79  && sqlite3_query
25aee 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 2d 31 5d 3d  _plan[nQPlan-1]=
25aef 3d 27 20 27 20 29 7b 0a 20 20 20 20 73 71 6c 69  =' ' ){.    sqli
25af0 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 2d  te3_query_plan[-
25af1 2d 6e 51 50 6c 61 6e 5d 20 3d 20 30 3b 0a 20 20  -nQPlan] = 0;.  
25af2 7d 0a 20 20 73 71 6c 69 74 65 33 5f 71 75 65 72  }.  sqlite3_quer
25af3 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 20 3d  y_plan[nQPlan] =
25af4 20 30 3b 0a 20 20 6e 51 50 6c 61 6e 20 3d 20 30   0;.  nQPlan = 0
25af5 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ;.#endif /* SQLI
25af6 54 45 5f 54 45 53 54 20 2f 2f 20 54 65 73 74 69  TE_TEST // Testi
25af7 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67  ng and debugging
25af8 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 0a 20 20   use only */..  
25af9 2f 2a 20 52 65 63 6f 72 64 20 74 68 65 20 63 6f  /* Record the co
25afa 6e 74 69 6e 75 61 74 69 6f 6e 20 61 64 64 72 65  ntinuation addre
25afb 73 73 20 69 6e 20 74 68 65 20 57 68 65 72 65 49  ss in the WhereI
25afc 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20 20  nfo structure.  
25afd 54 68 65 6e 0a 20 20 2a 2a 20 63 6c 65 61 6e 20  Then.  ** clean 
25afe 75 70 20 61 6e 64 20 72 65 74 75 72 6e 2e 0a 20  up and return.. 
25aff 20 2a 2f 0a 20 20 70 57 49 6e 66 6f 2d 3e 69 43   */.  pWInfo->iC
25b00 6f 6e 74 69 6e 75 65 20 3d 20 63 6f 6e 74 3b 0a  ontinue = cont;.
25b01 20 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65    whereClauseCle
25b02 61 72 28 26 77 63 29 3b 0a 20 20 72 65 74 75 72  ar(&wc);.  retur
25b03 6e 20 70 57 49 6e 66 6f 3b 0a 0a 20 20 2f 2a 20  n pWInfo;..  /* 
25b04 4a 75 6d 70 20 68 65 72 65 20 69 66 20 6d 61 6c  Jump here if mal
25b05 6c 6f 63 20 66 61 69 6c 73 20 2a 2f 0a 77 68 65  loc fails */.whe
25b06 72 65 42 65 67 69 6e 4e 6f 4d 65 6d 3a 0a 20 20  reBeginNoMem:.  
25b07 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65 61 72  whereClauseClear
25b08 28 26 77 63 29 3b 0a 20 20 77 68 65 72 65 49 6e  (&wc);.  whereIn
25b09 66 6f 46 72 65 65 28 70 57 49 6e 66 6f 29 3b 0a  foFree(pWInfo);.
25b0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
25b0b 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 74 68  *.** Generate th
25b0c 65 20 65 6e 64 20 6f 66 20 74 68 65 20 57 48 45  e end of the WHE
25b0d 52 45 20 6c 6f 6f 70 2e 20 20 53 65 65 20 63 6f  RE loop.  See co
25b0e 6d 6d 65 6e 74 73 20 6f 6e 20 0a 2a 2a 20 73 71  mments on .** sq
25b0f 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28  lite3WhereBegin(
25b10 29 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  ) for additional
25b11 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f   information..*/
25b12 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
25b13 76 6f 69 64 20 73 71 6c 69 74 65 33 57 68 65 72  void sqlite3Wher
25b14 65 45 6e 64 28 57 68 65 72 65 49 6e 66 6f 20 2a  eEnd(WhereInfo *
25b15 70 57 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65 20  pWInfo){.  Vdbe 
25b16 2a 76 20 3d 20 70 57 49 6e 66 6f 2d 3e 70 50 61  *v = pWInfo->pPa
25b17 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e  rse->pVdbe;.  in
25b18 74 20 69 3b 0a 20 20 57 68 65 72 65 4c 65 76 65  t i;.  WhereLeve
25b19 6c 20 2a 70 4c 65 76 65 6c 3b 0a 20 20 53 72 63  l *pLevel;.  Src
25b1a 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 20 3d  List *pTabList =
25b1b 20 70 57 49 6e 66 6f 2d 3e 70 54 61 62 4c 69 73   pWInfo->pTabLis
25b1c 74 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74  t;..  /* Generat
25b1d 65 20 6c 6f 6f 70 20 74 65 72 6d 69 6e 61 74 69  e loop terminati
25b1e 6f 6e 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20  on code..  */.  
25b1f 73 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72  sqlite3ExprClear
25b20 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 57 49 6e  ColumnCache(pWIn
25b21 66 6f 2d 3e 70 50 61 72 73 65 2c 20 2d 31 29 3b  fo->pParse, -1);
25b22 0a 20 20 66 6f 72 28 69 3d 70 54 61 62 4c 69 73  .  for(i=pTabLis
25b23 74 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e 3d 30 3b  t->nSrc-1; i>=0;
25b24 20 69 2d 2d 29 7b 0a 20 20 20 20 70 4c 65 76 65   i--){.    pLeve
25b25 6c 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 61 5b 69  l = &pWInfo->a[i
25b26 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ];.    sqlite3Vd
25b27 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
25b28 2c 20 70 4c 65 76 65 6c 2d 3e 63 6f 6e 74 29 3b  , pLevel->cont);
25b29 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d  .    if( pLevel-
25b2a 3e 6f 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a  >op!=OP_Noop ){.
25b2b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
25b2c 65 41 64 64 4f 70 32 28 76 2c 20 70 4c 65 76 65  eAddOp2(v, pLeve
25b2d 6c 2d 3e 6f 70 2c 20 70 4c 65 76 65 6c 2d 3e 70  l->op, pLevel->p
25b2e 31 2c 20 70 4c 65 76 65 6c 2d 3e 70 32 29 3b 0a  1, pLevel->p2);.
25b2f 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4c      }.    if( pL
25b30 65 76 65 6c 2d 3e 6e 49 6e 20 29 7b 0a 20 20 20  evel->nIn ){.   
25b31 20 20 20 73 74 72 75 63 74 20 49 6e 4c 6f 6f 70     struct InLoop
25b32 20 2a 70 49 6e 3b 0a 20 20 20 20 20 20 69 6e 74   *pIn;.      int
25b33 20 6a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   j;.      sqlite
25b34 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
25b35 6c 28 76 2c 20 70 4c 65 76 65 6c 2d 3e 6e 78 74  l(v, pLevel->nxt
25b36 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 70  );.      for(j=p
25b37 4c 65 76 65 6c 2d 3e 6e 49 6e 2c 20 70 49 6e 3d  Level->nIn, pIn=
25b38 26 70 4c 65 76 65 6c 2d 3e 61 49 6e 4c 6f 6f 70  &pLevel->aInLoop
25b39 5b 6a 2d 31 5d 3b 20 6a 3e 30 3b 20 6a 2d 2d 2c  [j-1]; j>0; j--,
25b3a 20 70 49 6e 2d 2d 29 7b 0a 20 20 20 20 20 20 20   pIn--){.       
25b3b 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
25b3c 48 65 72 65 28 76 2c 20 70 49 6e 2d 3e 74 6f 70  Here(v, pIn->top
25b3d 41 64 64 72 2b 31 29 3b 0a 20 20 20 20 20 20 20  Addr+1);.       
25b3e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
25b3f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 70  p2(v, OP_Next, p
25b40 49 6e 2d 3e 69 43 75 72 2c 20 70 49 6e 2d 3e 74  In->iCur, pIn->t
25b41 6f 70 41 64 64 72 29 3b 0a 20 20 20 20 20 20 20  opAddr);.       
25b42 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
25b43 48 65 72 65 28 76 2c 20 70 49 6e 2d 3e 74 6f 70  Here(v, pIn->top
25b44 41 64 64 72 2d 31 29 3b 0a 20 20 20 20 20 20 7d  Addr-1);.      }
25b45 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
25b46 72 65 65 28 70 4c 65 76 65 6c 2d 3e 61 49 6e 4c  ree(pLevel->aInL
25b47 6f 6f 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  oop);.    }.    
25b48 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
25b49 76 65 4c 61 62 65 6c 28 76 2c 20 70 4c 65 76 65  veLabel(v, pLeve
25b4a 6c 2d 3e 62 72 6b 29 3b 0a 20 20 20 20 69 66 28  l->brk);.    if(
25b4b 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f   pLevel->iLeftJo
25b4c 69 6e 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  in ){.      int 
25b4d 61 64 64 72 3b 0a 20 20 20 20 20 20 61 64 64 72  addr;.      addr
25b4e 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
25b4f 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73  dOp1(v, OP_IfPos
25b50 2c 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a  , pLevel->iLeftJ
25b51 6f 69 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  oin);.      sqli
25b52 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
25b53 20 4f 50 5f 4e 75 6c 6c 52 6f 77 2c 20 70 54 61   OP_NullRow, pTa
25b54 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72  bList->a[i].iCur
25b55 73 6f 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20  sor);.      if( 
25b56 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72 3e  pLevel->iIdxCur>
25b57 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  =0 ){.        sq
25b58 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
25b59 76 2c 20 4f 50 5f 4e 75 6c 6c 52 6f 77 2c 20 70  v, OP_NullRow, p
25b5a 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72 29 3b  Level->iIdxCur);
25b5b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
25b5c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
25b5d 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
25b5e 70 4c 65 76 65 6c 2d 3e 74 6f 70 29 3b 0a 20 20  pLevel->top);.  
25b5f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
25b60 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29  umpHere(v, addr)
25b61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
25b62 2a 20 54 68 65 20 22 62 72 65 61 6b 22 20 70 6f  * The "break" po
25b63 69 6e 74 20 69 73 20 68 65 72 65 2c 20 6a 75 73  int is here, jus
25b64 74 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f  t past the end o
25b65 66 20 74 68 65 20 6f 75 74 65 72 20 6c 6f 6f 70  f the outer loop
25b66 2e 0a 20 20 2a 2a 20 53 65 74 20 69 74 2e 0a 20  ..  ** Set it.. 
25b67 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
25b68 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
25b69 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 29   pWInfo->iBreak)
25b6a 3b 0a 0a 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c  ;..  /* Close al
25b6b 6c 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 73  l of the cursors
25b6c 20 74 68 61 74 20 77 65 72 65 20 6f 70 65 6e 65   that were opene
25b6d 64 20 62 79 20 73 71 6c 69 74 65 33 57 68 65 72  d by sqlite3Wher
25b6e 65 42 65 67 69 6e 2e 0a 20 20 2a 2f 0a 20 20 66  eBegin..  */.  f
25b6f 6f 72 28 69 3d 30 2c 20 70 4c 65 76 65 6c 3d 70  or(i=0, pLevel=p
25b70 57 49 6e 66 6f 2d 3e 61 3b 20 69 3c 70 54 61 62  WInfo->a; i<pTab
25b71 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c  List->nSrc; i++,
25b72 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20 20 20 20   pLevel++){.    
25b73 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
25b74 74 65 6d 20 2a 70 54 61 62 49 74 65 6d 20 3d 20  tem *pTabItem = 
25b75 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65  &pTabList->a[pLe
25b76 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20  vel->iFrom];.   
25b77 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70   Table *pTab = p
25b78 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20  TabItem->pTab;. 
25b79 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 21     assert( pTab!
25b7a 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 54  =0 );.    if( pT
25b7b 61 62 2d 3e 69 73 45 70 68 65 6d 20 7c 7c 20 70  ab->isEphem || p
25b7c 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 20 63  Tab->pSelect ) c
25b7d 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28  ontinue;.    if(
25b7e 20 21 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50   !pWInfo->okOneP
25b7f 61 73 73 20 26 26 20 28 70 4c 65 76 65 6c 2d 3e  ass && (pLevel->
25b80 66 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 44  flags & WHERE_ID
25b81 58 5f 4f 4e 4c 59 29 3d 3d 30 20 29 7b 0a 20 20  X_ONLY)==0 ){.  
25b82 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
25b83 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp1(v, OP_Clos
25b84 65 2c 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75  e, pTabItem->iCu
25b85 72 73 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20  rsor);.    }.   
25b86 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 49 64   if( pLevel->pId
25b87 78 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  x!=0 ){.      sq
25b88 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
25b89 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 70 4c 65  v, OP_Close, pLe
25b8a 76 65 6c 2d 3e 69 49 64 78 43 75 72 29 3b 0a 20  vel->iIdxCur);. 
25b8b 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
25b8c 74 68 69 73 20 73 63 61 6e 20 75 73 65 73 20 61  this scan uses a
25b8d 6e 20 69 6e 64 65 78 2c 20 6d 61 6b 65 20 63 6f  n index, make co
25b8e 64 65 20 73 75 62 73 74 69 74 75 74 69 6f 6e 73  de substitutions
25b8f 20 74 6f 20 72 65 61 64 20 64 61 74 61 0a 20 20   to read data.  
25b90 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 69 6e    ** from the in
25b91 64 65 78 20 69 6e 20 70 72 65 66 65 72 65 6e 63  dex in preferenc
25b92 65 20 74 6f 20 74 68 65 20 74 61 62 6c 65 2e 20  e to the table. 
25b93 53 6f 6d 65 74 69 6d 65 73 2c 20 74 68 69 73 20  Sometimes, this 
25b94 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68 65  means.    ** the
25b95 20 74 61 62 6c 65 20 6e 65 65 64 20 6e 65 76 65   table need neve
25b96 72 20 62 65 20 72 65 61 64 20 66 72 6f 6d 2e 20  r be read from. 
25b97 54 68 69 73 20 69 73 20 61 20 70 65 72 66 6f 72  This is a perfor
25b98 6d 61 6e 63 65 20 62 6f 6f 73 74 2c 0a 20 20 20  mance boost,.   
25b99 20 2a 2a 20 61 73 20 74 68 65 20 76 64 62 65 20   ** as the vdbe 
25b9a 6c 65 76 65 6c 20 77 61 69 74 73 20 75 6e 74 69  level waits unti
25b9b 6c 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 72  l the table is r
25b9c 65 61 64 20 62 65 66 6f 72 65 20 61 63 74 75 61  ead before actua
25b9d 6c 6c 79 0a 20 20 20 20 2a 2a 20 73 65 65 6b 69  lly.    ** seeki
25b9e 6e 67 20 74 68 65 20 74 61 62 6c 65 20 63 75 72  ng the table cur
25b9f 73 6f 72 20 74 6f 20 74 68 65 20 72 65 63 6f 72  sor to the recor
25ba0 64 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  d corresponding 
25ba1 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 0a 20  to the current. 
25ba2 20 20 20 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 69     ** position i
25ba3 6e 20 74 68 65 20 69 6e 64 65 78 2e 0a 20 20 20  n the index..   
25ba4 20 2a 2a 20 0a 20 20 20 20 2a 2a 20 43 61 6c 6c   ** .    ** Call
25ba5 73 20 74 6f 20 74 68 65 20 63 6f 64 65 20 67 65  s to the code ge
25ba6 6e 65 72 61 74 6f 72 20 69 6e 20 62 65 74 77 65  nerator in betwe
25ba7 65 6e 20 73 71 6c 69 74 65 33 57 68 65 72 65 42  en sqlite3WhereB
25ba8 65 67 69 6e 20 61 6e 64 0a 20 20 20 20 2a 2a 20  egin and.    ** 
25ba9 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 20  sqlite3WhereEnd 
25baa 77 69 6c 6c 20 68 61 76 65 20 63 72 65 61 74 65  will have create
25bab 64 20 63 6f 64 65 20 74 68 61 74 20 72 65 66 65  d code that refe
25bac 72 65 6e 63 65 73 20 74 68 65 20 74 61 62 6c 65  rences the table
25bad 0a 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79  .    ** directly
25bae 2e 20 20 54 68 69 73 20 6c 6f 6f 70 20 73 63 61  .  This loop sca
25baf 6e 73 20 61 6c 6c 20 74 68 61 74 20 63 6f 64 65  ns all that code
25bb0 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6f 70 63   looking for opc
25bb1 6f 64 65 73 0a 20 20 20 20 2a 2a 20 74 68 61 74  odes.    ** that
25bb2 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 74   reference the t
25bb3 61 62 6c 65 20 61 6e 64 20 63 6f 6e 76 65 72 74  able and convert
25bb4 73 20 74 68 65 6d 20 69 6e 74 6f 20 6f 70 63 6f  s them into opco
25bb5 64 65 73 20 74 68 61 74 0a 20 20 20 20 2a 2a 20  des that.    ** 
25bb6 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 69 6e  reference the in
25bb7 64 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  dex..    */.    
25bb8 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 49 64 78  if( pLevel->pIdx
25bb9 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6b 2c   ){.      int k,
25bba 20 6a 2c 20 6c 61 73 74 3b 0a 20 20 20 20 20 20   j, last;.      
25bbb 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 20  VdbeOp *pOp;.   
25bbc 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d     Index *pIdx =
25bbd 20 70 4c 65 76 65 6c 2d 3e 70 49 64 78 3b 0a 20   pLevel->pIdx;. 
25bbe 20 20 20 20 20 69 6e 74 20 75 73 65 49 6e 64 65       int useInde
25bbf 78 4f 6e 6c 79 20 3d 20 70 4c 65 76 65 6c 2d 3e  xOnly = pLevel->
25bc0 66 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 44  flags & WHERE_ID
25bc1 58 5f 4f 4e 4c 59 3b 0a 0a 20 20 20 20 20 20 61  X_ONLY;..      a
25bc2 73 73 65 72 74 28 20 70 49 64 78 21 3d 30 20 29  ssert( pIdx!=0 )
25bc3 3b 0a 20 20 20 20 20 20 70 4f 70 20 3d 20 73 71  ;.      pOp = sq
25bc4 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76  lite3VdbeGetOp(v
25bc5 2c 20 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 29 3b  , pWInfo->iTop);
25bc6 0a 20 20 20 20 20 20 6c 61 73 74 20 3d 20 73 71  .      last = sq
25bc7 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
25bc8 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 66  Addr(v);.      f
25bc9 6f 72 28 6b 3d 70 57 49 6e 66 6f 2d 3e 69 54 6f  or(k=pWInfo->iTo
25bca 70 3b 20 6b 3c 6c 61 73 74 3b 20 6b 2b 2b 2c 20  p; k<last; k++, 
25bcb 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  pOp++){.        
25bcc 69 66 28 20 70 4f 70 2d 3e 70 31 21 3d 70 4c 65  if( pOp->p1!=pLe
25bcd 76 65 6c 2d 3e 69 54 61 62 43 75 72 20 29 20 63  vel->iTabCur ) c
25bce 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
25bcf 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
25bd0 3d 3d 4f 50 5f 43 6f 6c 75 6d 6e 20 29 7b 0a 20  ==OP_Column ){. 
25bd1 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30           for(j=0
25bd2 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  ; j<pIdx->nColum
25bd3 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  n; j++){.       
25bd4 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32       if( pOp->p2
25bd5 3d 3d 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  ==pIdx->aiColumn
25bd6 5b 6a 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20  [j] ){.         
25bd7 20 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20 6a       pOp->p2 = j
25bd8 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
25bd9 70 4f 70 2d 3e 70 31 20 3d 20 70 4c 65 76 65 6c  pOp->p1 = pLevel
25bda 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20 20 20 20  ->iIdxCur;.     
25bdb 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
25bdc 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
25bdd 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
25bde 20 20 20 20 61 73 73 65 72 74 28 21 75 73 65 49      assert(!useI
25bdf 6e 64 65 78 4f 6e 6c 79 20 7c 7c 20 6a 3c 70 49  ndexOnly || j<pI
25be0 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20  dx->nColumn);.  
25be1 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
25be2 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
25be3 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20  Rowid ){.       
25be4 20 20 20 70 4f 70 2d 3e 70 31 20 3d 20 70 4c 65     pOp->p1 = pLe
25be5 76 65 6c 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20  vel->iIdxCur;.  
25be6 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63          pOp->opc
25be7 6f 64 65 20 3d 20 4f 50 5f 49 64 78 52 6f 77 69  ode = OP_IdxRowi
25be8 64 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  d;.        }else
25be9 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
25bea 3d 3d 4f 50 5f 4e 75 6c 6c 52 6f 77 20 26 26 20  ==OP_NullRow && 
25beb 75 73 65 49 6e 64 65 78 4f 6e 6c 79 20 29 7b 0a  useIndexOnly ){.
25bec 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f            pOp->o
25bed 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b  pcode = OP_Noop;
25bee 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
25bef 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
25bf0 2f 2a 20 46 69 6e 61 6c 20 63 6c 65 61 6e 75 70  /* Final cleanup
25bf1 0a 20 20 2a 2f 0a 20 20 77 68 65 72 65 49 6e 66  .  */.  whereInf
25bf2 6f 46 72 65 65 28 70 57 49 6e 66 6f 29 3b 0a 20  oFree(pWInfo);. 
25bf3 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 2a 2a   return;.}../***
25bf4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
25bf5 6f 66 20 77 68 65 72 65 2e 63 20 2a 2a 2a 2a 2a  of where.c *****
25bf6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25bf7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25bf8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
25bf9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
25bfa 6e 20 66 69 6c 65 20 70 61 72 73 65 2e 63 20 2a  n file parse.c *
25bfb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25bfc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25bfd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44  **********/./* D
25bfe 72 69 76 65 72 20 74 65 6d 70 6c 61 74 65 20 66  river template f
25bff 6f 72 20 74 68 65 20 4c 45 4d 4f 4e 20 70 61 72  or the LEMON par
25c00 73 65 72 20 67 65 6e 65 72 61 74 6f 72 2e 0a 2a  ser generator..*
25c01 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
25c02 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
25c03 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
25c04 63 6f 64 65 2e 0a 2a 2f 0a 2f 2a 20 46 69 72 73  code..*/./* Firs
25c05 74 20 6f 66 66 2c 20 63 6f 64 65 20 69 73 20 69  t off, code is i
25c06 6e 63 6c 75 64 65 20 77 68 69 63 68 20 66 6f 6c  nclude which fol
25c07 6c 6f 77 73 20 74 68 65 20 22 69 6e 63 6c 75 64  lows the "includ
25c08 65 22 20 64 65 63 6c 61 72 61 74 69 6f 6e 0a 2a  e" declaration.*
25c09 2a 20 69 6e 20 74 68 65 20 69 6e 70 75 74 20 66  * in the input f
25c0a 69 6c 65 2e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20  ile. */.../*.** 
25c0b 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
25c0c 68 69 73 20 73 74 72 75 63 74 75 72 65 20 68 6f  his structure ho
25c0d 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  lds information 
25c0e 61 62 6f 75 74 20 74 68 65 0a 2a 2a 20 4c 49 4d  about the.** LIM
25c0f 49 54 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53  IT clause of a S
25c10 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
25c11 0a 2a 2f 0a 73 74 72 75 63 74 20 4c 69 6d 69 74  .*/.struct Limit
25c12 56 61 6c 20 7b 0a 20 20 45 78 70 72 20 2a 70 4c  Val {.  Expr *pL
25c13 69 6d 69 74 3b 20 20 20 20 2f 2a 20 54 68 65 20  imit;    /* The 
25c14 4c 49 4d 49 54 20 65 78 70 72 65 73 73 69 6f 6e  LIMIT expression
25c15 2e 20 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65  .  NULL if there
25c16 20 69 73 20 6e 6f 20 6c 69 6d 69 74 20 2a 2f 0a   is no limit */.
25c17 20 20 45 78 70 72 20 2a 70 4f 66 66 73 65 74 3b    Expr *pOffset;
25c18 20 20 20 2f 2a 20 54 68 65 20 4f 46 46 53 45 54     /* The OFFSET
25c19 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 4e 55   expression.  NU
25c1a 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73 20 6e  LL if there is n
25c1b 6f 6e 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  one */.};../*.**
25c1c 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
25c1d 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69  this structure i
25c1e 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  s used to store 
25c1f 74 68 65 20 4c 49 4b 45 2c 0a 2a 2a 20 47 4c 4f  the LIKE,.** GLO
25c20 42 2c 20 4e 4f 54 20 4c 49 4b 45 2c 20 61 6e 64  B, NOT LIKE, and
25c21 20 4e 4f 54 20 47 4c 4f 42 20 6f 70 65 72 61 74   NOT GLOB operat
25c22 6f 72 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4c  ors..*/.struct L
25c23 69 6b 65 4f 70 20 7b 0a 20 20 54 6f 6b 65 6e 20  ikeOp {.  Token 
25c24 65 4f 70 65 72 61 74 6f 72 3b 20 20 2f 2a 20 22  eOperator;  /* "
25c25 6c 69 6b 65 22 20 6f 72 20 22 67 6c 6f 62 22 20  like" or "glob" 
25c26 6f 72 20 22 72 65 67 65 78 70 22 20 2a 2f 0a 20  or "regexp" */. 
25c27 20 69 6e 74 20 6e 6f 74 3b 20 20 20 20 20 20 20   int not;       
25c28 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
25c29 20 4e 4f 54 20 6b 65 79 77 6f 72 64 20 69 73 20   NOT keyword is 
25c2a 70 72 65 73 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f  present */.};../
25c2b 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
25c2c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
25c2d 67 20 73 74 72 75 63 74 75 72 65 20 64 65 73 63  g structure desc
25c2e 72 69 62 65 73 20 74 68 65 20 65 76 65 6e 74 20  ribes the event 
25c2f 6f 66 20 61 0a 2a 2a 20 54 52 49 47 47 45 52 2e  of a.** TRIGGER.
25c30 20 20 22 61 22 20 69 73 20 74 68 65 20 65 76 65    "a" is the eve
25c31 6e 74 20 74 79 70 65 2c 20 6f 6e 65 20 6f 66 20  nt type, one of 
25c32 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e  TK_UPDATE, TK_IN
25c33 53 45 52 54 2c 0a 2a 2a 20 54 4b 5f 44 45 4c 45  SERT,.** TK_DELE
25c34 54 45 2c 20 6f 72 20 54 4b 5f 49 4e 53 54 45 41  TE, or TK_INSTEA
25c35 44 2e 20 20 49 66 20 74 68 65 20 65 76 65 6e 74  D.  If the event
25c36 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a   is of the form.
25c37 2a 2a 0a 2a 2a 20 20 20 20 20 20 55 50 44 41 54  **.**      UPDAT
25c38 45 20 4f 4e 20 28 61 2c 62 2c 63 29 0a 2a 2a 0a  E ON (a,b,c).**.
25c39 2a 2a 20 54 68 65 6e 20 74 68 65 20 22 62 22 20  ** Then the "b" 
25c3a 49 64 4c 69 73 74 20 72 65 63 6f 72 64 73 20 74  IdList records t
25c3b 68 65 20 6c 69 73 74 20 22 61 2c 62 2c 63 22 2e  he list "a,b,c".
25c3c 0a 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67 45  .*/.struct TrigE
25c3d 76 65 6e 74 20 7b 20 69 6e 74 20 61 3b 20 49 64  vent { int a; Id
25c3e 4c 69 73 74 20 2a 20 62 3b 20 7d 3b 0a 0a 2f 2a  List * b; };../*
25c3f 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
25c40 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
25c41 65 20 68 6f 6c 64 73 20 74 68 65 20 41 54 54 41  e holds the ATTA
25c42 43 48 20 6b 65 79 20 61 6e 64 20 74 68 65 20 6b  CH key and the k
25c43 65 79 20 74 79 70 65 2e 0a 2a 2f 0a 73 74 72 75  ey type..*/.stru
25c44 63 74 20 41 74 74 61 63 68 4b 65 79 20 7b 20 69  ct AttachKey { i
25c45 6e 74 20 74 79 70 65 3b 20 20 54 6f 6b 65 6e 20  nt type;  Token 
25c46 6b 65 79 3b 20 7d 3b 0a 0a 2f 2a 20 4e 65 78 74  key; };../* Next
25c47 20 69 73 20 61 6c 6c 20 74 6f 6b 65 6e 20 76 61   is all token va
25c48 6c 75 65 73 2c 20 69 6e 20 61 20 66 6f 72 6d 20  lues, in a form 
25c49 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65  suitable for use
25c4a 20 62 79 20 6d 61 6b 65 68 65 61 64 65 72 73 2e   by makeheaders.
25c4b 0a 2a 2a 20 54 68 69 73 20 73 65 63 74 69 6f 6e  .** This section
25c4c 20 77 69 6c 6c 20 62 65 20 6e 75 6c 6c 20 75 6e   will be null un
25c4d 6c 65 73 73 20 6c 65 6d 6f 6e 20 69 73 20 72 75  less lemon is ru
25c4e 6e 20 77 69 74 68 20 74 68 65 20 2d 6d 20 73 77  n with the -m sw
25c4f 69 74 63 68 2e 0a 2a 2f 0a 2f 2a 20 0a 2a 2a 20  itch..*/./* .** 
25c50 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20  These constants 
25c51 28 61 6c 6c 20 67 65 6e 65 72 61 74 65 64 20 61  (all generated a
25c52 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79 20  utomatically by 
25c53 74 68 65 20 70 61 72 73 65 72 20 67 65 6e 65 72  the parser gener
25c54 61 74 6f 72 29 0a 2a 2a 20 73 70 65 63 69 66 79  ator).** specify
25c55 20 74 68 65 20 76 61 72 69 6f 75 73 20 6b 69 6e   the various kin
25c56 64 73 20 6f 66 20 74 6f 6b 65 6e 73 20 28 74 65  ds of tokens (te
25c57 72 6d 69 6e 61 6c 73 29 20 74 68 61 74 20 74 68  rminals) that th
25c58 65 20 70 61 72 73 65 72 0a 2a 2a 20 75 6e 64 65  e parser.** unde
25c59 72 73 74 61 6e 64 73 2e 20 0a 2a 2a 0a 2a 2a 20  rstands. .**.** 
25c5a 45 61 63 68 20 73 79 6d 62 6f 6c 20 68 65 72 65  Each symbol here
25c5b 20 69 73 20 61 20 74 65 72 6d 69 6e 61 6c 20 73   is a terminal s
25c5c 79 6d 62 6f 6c 20 69 6e 20 74 68 65 20 67 72 61  ymbol in the gra
25c5d 6d 6d 61 72 2e 0a 2a 2f 0a 2f 2a 20 4d 61 6b 65  mmar..*/./* Make
25c5e 20 73 75 72 65 20 74 68 65 20 49 4e 54 45 52 46   sure the INTERF
25c5f 41 43 45 20 6d 61 63 72 6f 20 69 73 20 64 65 66  ACE macro is def
25c60 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  ined..*/.#ifndef
25c61 20 49 4e 54 45 52 46 41 43 45 0a 23 20 64 65 66   INTERFACE.# def
25c62 69 6e 65 20 49 4e 54 45 52 46 41 43 45 20 31 0a  ine INTERFACE 1.
25c63 23 65 6e 64 69 66 0a 2f 2a 20 54 68 65 20 6e 65  #endif./* The ne
25c64 78 74 20 74 68 69 6e 67 20 69 6e 63 6c 75 64 65  xt thing include
25c65 64 20 69 73 20 73 65 72 69 65 73 20 6f 66 20 64  d is series of d
25c66 65 66 69 6e 65 73 20 77 68 69 63 68 20 63 6f 6e  efines which con
25c67 74 72 6f 6c 0a 2a 2a 20 76 61 72 69 6f 75 73 20  trol.** various 
25c68 61 73 70 65 63 74 73 20 6f 66 20 74 68 65 20 67  aspects of the g
25c69 65 6e 65 72 61 74 65 64 20 70 61 72 73 65 72 2e  enerated parser.
25c6a 0a 2a 2a 20 20 20 20 59 59 43 4f 44 45 54 59 50  .**    YYCODETYP
25c6b 45 20 20 20 20 20 20 20 20 20 69 73 20 74 68 65  E         is the
25c6c 20 64 61 74 61 20 74 79 70 65 20 75 73 65 64 20   data type used 
25c6d 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 65 72 6d  for storing term
25c6e 69 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20  inal.**         
25c6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6e                an
25c70 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20 6e 75  d nonterminal nu
25c71 6d 62 65 72 73 2e 20 20 22 75 6e 73 69 67 6e 65  mbers.  "unsigne
25c72 64 20 63 68 61 72 22 20 69 73 0a 2a 2a 20 20 20  d char" is.**   
25c73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25c74 20 20 20 20 75 73 65 64 20 69 66 20 74 68 65 72      used if ther
25c75 65 20 61 72 65 20 66 65 77 65 72 20 74 68 61 6e  e are fewer than
25c76 20 32 35 30 20 74 65 72 6d 69 6e 61 6c 73 0a 2a   250 terminals.*
25c77 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
25c78 20 20 20 20 20 20 20 20 61 6e 64 20 6e 6f 6e 74          and nont
25c79 65 72 6d 69 6e 61 6c 73 2e 20 20 22 69 6e 74 22  erminals.  "int"
25c7a 20 69 73 20 75 73 65 64 20 6f 74 68 65 72 77 69   is used otherwi
25c7b 73 65 2e 0a 2a 2a 20 20 20 20 59 59 4e 4f 43 4f  se..**    YYNOCO
25c7c 44 45 20 20 20 20 20 20 20 20 20 20 20 69 73 20  DE           is 
25c7d 61 20 6e 75 6d 62 65 72 20 6f 66 20 74 79 70 65  a number of type
25c7e 20 59 59 43 4f 44 45 54 59 50 45 20 77 68 69 63   YYCODETYPE whic
25c7f 68 20 63 6f 72 72 65 73 70 6f 6e 64 73 0a 2a 2a  h corresponds.**
25c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25c81 20 20 20 20 20 20 20 74 6f 20 6e 6f 20 6c 65 67         to no leg
25c82 61 6c 20 74 65 72 6d 69 6e 61 6c 20 6f 72 20 6e  al terminal or n
25c83 6f 6e 74 65 72 6d 69 6e 61 6c 20 6e 75 6d 62 65  onterminal numbe
25c84 72 2e 20 20 54 68 69 73 0a 2a 2a 20 20 20 20 20  r.  This.**     
25c85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25c86 20 20 6e 75 6d 62 65 72 20 69 73 20 75 73 65 64    number is used
25c87 20 74 6f 20 66 69 6c 6c 20 69 6e 20 65 6d 70 74   to fill in empt
25c88 79 20 73 6c 6f 74 73 20 6f 66 20 74 68 65 20 68  y slots of the h
25c89 61 73 68 20 0a 2a 2a 20 20 20 20 20 20 20 20 20  ash .**         
25c8a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 61                ta
25c8b 62 6c 65 2e 0a 2a 2a 20 20 20 20 59 59 46 41 4c  ble..**    YYFAL
25c8c 4c 42 41 43 4b 20 20 20 20 20 20 20 20 20 49 66  LBACK         If
25c8d 20 64 65 66 69 6e 65 64 2c 20 74 68 69 73 20 69   defined, this i
25c8e 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 6f 6e  ndicates that on
25c8f 65 20 6f 72 20 6d 6f 72 65 20 74 6f 6b 65 6e 73  e or more tokens
25c90 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
25c91 20 20 20 20 20 20 20 20 20 20 68 61 76 65 20 66            have f
25c92 61 6c 6c 2d 62 61 63 6b 20 76 61 6c 75 65 73 20  all-back values 
25c93 77 68 69 63 68 20 73 68 6f 75 6c 64 20 62 65 20  which should be 
25c94 75 73 65 64 20 69 66 20 74 68 65 0a 2a 2a 20 20  used if the.**  
25c95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25c96 20 20 20 20 20 6f 72 69 67 69 6e 61 6c 20 76 61       original va
25c97 6c 75 65 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  lue of the token
25c98 20 77 69 6c 6c 20 6e 6f 74 20 70 61 72 73 65 2e   will not parse.
25c99 0a 2a 2a 20 20 20 20 59 59 41 43 54 49 4f 4e 54  .**    YYACTIONT
25c9a 59 50 45 20 20 20 20 20 20 20 69 73 20 74 68 65  YPE       is the
25c9b 20 64 61 74 61 20 74 79 70 65 20 75 73 65 64 20   data type used 
25c9c 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 65 72 6d  for storing term
25c9d 69 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20  inal.**         
25c9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6e                an
25c9f 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20 6e 75  d nonterminal nu
25ca0 6d 62 65 72 73 2e 20 20 22 75 6e 73 69 67 6e 65  mbers.  "unsigne
25ca1 64 20 63 68 61 72 22 20 69 73 0a 2a 2a 20 20 20  d char" is.**   
25ca2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25ca3 20 20 20 20 75 73 65 64 20 69 66 20 74 68 65 72      used if ther
25ca4 65 20 61 72 65 20 66 65 77 65 72 20 74 68 61 6e  e are fewer than
25ca5 20 32 35 30 20 72 75 6c 65 73 20 61 6e 64 0a 2a   250 rules and.*
25ca6 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
25ca7 20 20 20 20 20 20 20 20 73 74 61 74 65 73 20 63          states c
25ca8 6f 6d 62 69 6e 65 64 2e 20 20 22 69 6e 74 22 20  ombined.  "int" 
25ca9 69 73 20 75 73 65 64 20 6f 74 68 65 72 77 69 73  is used otherwis
25caa 65 2e 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65 33  e..**    sqlite3
25cab 50 61 72 73 65 72 54 4f 4b 45 4e 54 59 50 45 20  ParserTOKENTYPE 
25cac 20 20 20 20 69 73 20 74 68 65 20 64 61 74 61 20      is the data 
25cad 74 79 70 65 20 75 73 65 64 20 66 6f 72 20 6d 69  type used for mi
25cae 6e 6f 72 20 74 6f 6b 65 6e 73 20 67 69 76 65 6e  nor tokens given
25caf 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20   .**            
25cb0 20 20 20 20 20 20 20 20 20 20 20 64 69 72 65 63             direc
25cb1 74 6c 79 20 74 6f 20 74 68 65 20 70 61 72 73 65  tly to the parse
25cb2 72 20 66 72 6f 6d 20 74 68 65 20 74 6f 6b 65 6e  r from the token
25cb3 69 7a 65 72 2e 0a 2a 2a 20 20 20 20 59 59 4d 49  izer..**    YYMI
25cb4 4e 4f 52 54 59 50 45 20 20 20 20 20 20 20 20 69  NORTYPE        i
25cb5 73 20 74 68 65 20 64 61 74 61 20 74 79 70 65 20  s the data type 
25cb6 75 73 65 64 20 66 6f 72 20 61 6c 6c 20 6d 69 6e  used for all min
25cb7 6f 72 20 74 6f 6b 65 6e 73 2e 0a 2a 2a 20 20 20  or tokens..**   
25cb8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25cb9 20 20 20 20 54 68 69 73 20 69 73 20 74 79 70 69      This is typi
25cba 63 61 6c 6c 79 20 61 20 75 6e 69 6f 6e 20 6f 66  cally a union of
25cbb 20 6d 61 6e 79 20 74 79 70 65 73 2c 20 6f 6e 65   many types, one
25cbc 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   of.**          
25cbd 20 20 20 20 20 20 20 20 20 20 20 20 20 77 68 69               whi
25cbe 63 68 20 69 73 20 73 71 6c 69 74 65 33 50 61 72  ch is sqlite3Par
25cbf 73 65 72 54 4f 4b 45 4e 54 59 50 45 2e 20 20 54  serTOKENTYPE.  T
25cc0 68 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  he entry in the 
25cc1 75 6e 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20  union.**        
25cc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66                 f
25cc3 6f 72 20 62 61 73 65 20 74 6f 6b 65 6e 73 20 69  or base tokens i
25cc4 73 20 63 61 6c 6c 65 64 20 22 79 79 30 22 2e 0a  s called "yy0"..
25cc5 2a 2a 20 20 20 20 59 59 53 54 41 43 4b 44 45 50  **    YYSTACKDEP
25cc6 54 48 20 20 20 20 20 20 20 69 73 20 74 68 65 20  TH       is the 
25cc7 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66  maximum depth of
25cc8 20 74 68 65 20 70 61 72 73 65 72 27 73 20 73 74   the parser's st
25cc9 61 63 6b 2e 20 20 49 66 0a 2a 2a 20 20 20 20 20  ack.  If.**     
25cca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25ccb 20 20 7a 65 72 6f 20 74 68 65 20 73 74 61 63 6b    zero the stack
25ccc 20 69 73 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20   is dynamically 
25ccd 73 69 7a 65 64 20 75 73 69 6e 67 20 72 65 61 6c  sized using real
25cce 6c 6f 63 28 29 0a 2a 2a 20 20 20 20 73 71 6c 69  loc().**    sqli
25ccf 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 44 45  te3ParserARG_SDE
25cd0 43 4c 20 20 20 20 20 41 20 73 74 61 74 69 63 20  CL     A static 
25cd1 76 61 72 69 61 62 6c 65 20 64 65 63 6c 61 72 61  variable declara
25cd2 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 25 65 78  tion for the %ex
25cd3 74 72 61 5f 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  tra_argument.** 
25cd4 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72     sqlite3Parser
25cd5 41 52 47 5f 50 44 45 43 4c 20 20 20 20 20 41 20  ARG_PDECL     A 
25cd6 70 61 72 61 6d 65 74 65 72 20 64 65 63 6c 61 72  parameter declar
25cd7 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 25 65  ation for the %e
25cd8 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 0a 2a 2a  xtra_argument.**
25cd9 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65      sqlite3Parse
25cda 72 41 52 47 5f 53 54 4f 52 45 20 20 20 20 20 43  rARG_STORE     C
25cdb 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 25 65 78  ode to store %ex
25cdc 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 69 6e 74  tra_argument int
25cdd 6f 20 79 79 70 50 61 72 73 65 72 0a 2a 2a 20 20  o yypParser.**  
25cde 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41    sqlite3ParserA
25cdf 52 47 5f 46 45 54 43 48 20 20 20 20 20 43 6f 64  RG_FETCH     Cod
25ce0 65 20 74 6f 20 65 78 74 72 61 63 74 20 25 65 78  e to extract %ex
25ce1 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 66 72 6f  tra_argument fro
25ce2 6d 20 79 79 70 50 61 72 73 65 72 0a 2a 2a 20 20  m yypParser.**  
25ce3 20 20 59 59 4e 53 54 41 54 45 20 20 20 20 20 20    YYNSTATE      
25ce4 20 20 20 20 20 74 68 65 20 63 6f 6d 62 69 6e 65       the combine
25ce5 64 20 6e 75 6d 62 65 72 20 6f 66 20 73 74 61 74  d number of stat
25ce6 65 73 2e 0a 2a 2a 20 20 20 20 59 59 4e 52 55 4c  es..**    YYNRUL
25ce7 45 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65  E            the
25ce8 20 6e 75 6d 62 65 72 20 6f 66 20 72 75 6c 65 73   number of rules
25ce9 20 69 6e 20 74 68 65 20 67 72 61 6d 6d 61 72 0a   in the grammar.
25cea 2a 2a 20 20 20 20 59 59 45 52 52 4f 52 53 59 4d  **    YYERRORSYM
25ceb 42 4f 4c 20 20 20 20 20 20 69 73 20 74 68 65 20  BOL      is the 
25cec 63 6f 64 65 20 6e 75 6d 62 65 72 20 6f 66 20 74  code number of t
25ced 68 65 20 65 72 72 6f 72 20 73 79 6d 62 6f 6c 2e  he error symbol.
25cee 20 20 49 66 20 6e 6f 74 0a 2a 2a 20 20 20 20 20    If not.**     
25cef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25cf0 20 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20    defined, then 
25cf1 64 6f 20 6e 6f 20 65 72 72 6f 72 20 70 72 6f 63  do no error proc
25cf2 65 73 73 69 6e 67 2e 0a 2a 2f 0a 23 64 65 66 69  essing..*/.#defi
25cf3 6e 65 20 59 59 43 4f 44 45 54 59 50 45 20 75 6e  ne YYCODETYPE un
25cf4 73 69 67 6e 65 64 20 63 68 61 72 0a 23 64 65 66  signed char.#def
25cf5 69 6e 65 20 59 59 4e 4f 43 4f 44 45 20 32 34 38  ine YYNOCODE 248
25cf6 0a 23 64 65 66 69 6e 65 20 59 59 41 43 54 49 4f  .#define YYACTIO
25cf7 4e 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 73  NTYPE unsigned s
25cf8 68 6f 72 74 20 69 6e 74 0a 23 64 65 66 69 6e 65  hort int.#define
25cf9 20 59 59 57 49 4c 44 43 41 52 44 20 35 39 0a 23   YYWILDCARD 59.#
25cfa 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61  define sqlite3Pa
25cfb 72 73 65 72 54 4f 4b 45 4e 54 59 50 45 20 54 6f  rserTOKENTYPE To
25cfc 6b 65 6e 0a 74 79 70 65 64 65 66 20 75 6e 69 6f  ken.typedef unio
25cfd 6e 20 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 72  n {.  sqlite3Par
25cfe 73 65 72 54 4f 4b 45 4e 54 59 50 45 20 79 79 30  serTOKENTYPE yy0
25cff 3b 0a 20 20 69 6e 74 20 79 79 34 36 3b 0a 20 20  ;.  int yy46;.  
25d00 73 74 72 75 63 74 20 4c 69 6b 65 4f 70 20 79 79  struct LikeOp yy
25d01 37 32 3b 0a 20 20 45 78 70 72 2a 20 79 79 31 37  72;.  Expr* yy17
25d02 32 3b 0a 20 20 45 78 70 72 4c 69 73 74 2a 20 79  2;.  ExprList* y
25d03 79 31 37 34 3b 0a 20 20 53 65 6c 65 63 74 2a 20  y174;.  Select* 
25d04 79 79 32 31 39 3b 0a 20 20 73 74 72 75 63 74 20  yy219;.  struct 
25d05 4c 69 6d 69 74 56 61 6c 20 79 79 32 33 34 3b 0a  LimitVal yy234;.
25d06 20 20 54 72 69 67 67 65 72 53 74 65 70 2a 20 79    TriggerStep* y
25d07 79 32 34 33 3b 0a 20 20 73 74 72 75 63 74 20 54  y243;.  struct T
25d08 72 69 67 45 76 65 6e 74 20 79 79 33 37 30 3b 0a  rigEvent yy370;.
25d09 20 20 53 72 63 4c 69 73 74 2a 20 79 79 33 37 33    SrcList* yy373
25d0a 3b 0a 20 20 73 74 72 75 63 74 20 7b 69 6e 74 20  ;.  struct {int 
25d0b 76 61 6c 75 65 3b 20 69 6e 74 20 6d 61 73 6b 3b  value; int mask;
25d0c 7d 20 79 79 34 30 35 3b 0a 20 20 54 6f 6b 65 6e  } yy405;.  Token
25d0d 20 79 79 34 31 30 3b 0a 20 20 49 64 4c 69 73 74   yy410;.  IdList
25d0e 2a 20 79 79 34 33 32 3b 0a 7d 20 59 59 4d 49 4e  * yy432;.} YYMIN
25d0f 4f 52 54 59 50 45 3b 0a 23 69 66 6e 64 65 66 20  ORTYPE;.#ifndef 
25d10 59 59 53 54 41 43 4b 44 45 50 54 48 0a 23 64 65  YYSTACKDEPTH.#de
25d11 66 69 6e 65 20 59 59 53 54 41 43 4b 44 45 50 54  fine YYSTACKDEPT
25d12 48 20 31 30 30 0a 23 65 6e 64 69 66 0a 23 64 65  H 100.#endif.#de
25d13 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61 72 73  fine sqlite3Pars
25d14 65 72 41 52 47 5f 53 44 45 43 4c 20 50 61 72 73  erARG_SDECL Pars
25d15 65 20 2a 70 50 61 72 73 65 3b 0a 23 64 65 66 69  e *pParse;.#defi
25d16 6e 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  ne sqlite3Parser
25d17 41 52 47 5f 50 44 45 43 4c 20 2c 50 61 72 73 65  ARG_PDECL ,Parse
25d18 20 2a 70 50 61 72 73 65 0a 23 64 65 66 69 6e 65   *pParse.#define
25d19 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
25d1a 47 5f 46 45 54 43 48 20 50 61 72 73 65 20 2a 70  G_FETCH Parse *p
25d1b 50 61 72 73 65 20 3d 20 79 79 70 50 61 72 73 65  Parse = yypParse
25d1c 72 2d 3e 70 50 61 72 73 65 0a 23 64 65 66 69 6e  r->pParse.#defin
25d1d 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41  e sqlite3ParserA
25d1e 52 47 5f 53 54 4f 52 45 20 79 79 70 50 61 72 73  RG_STORE yypPars
25d1f 65 72 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61  er->pParse = pPa
25d20 72 73 65 0a 23 64 65 66 69 6e 65 20 59 59 4e 53  rse.#define YYNS
25d21 54 41 54 45 20 35 38 39 0a 23 64 65 66 69 6e 65  TATE 589.#define
25d22 20 59 59 4e 52 55 4c 45 20 33 31 33 0a 23 64 65   YYNRULE 313.#de
25d23 66 69 6e 65 20 59 59 46 41 4c 4c 42 41 43 4b 20  fine YYFALLBACK 
25d24 31 0a 23 64 65 66 69 6e 65 20 59 59 5f 4e 4f 5f  1.#define YY_NO_
25d25 41 43 54 49 4f 4e 20 20 20 20 20 20 28 59 59 4e  ACTION      (YYN
25d26 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b 32 29  STATE+YYNRULE+2)
25d27 0a 23 64 65 66 69 6e 65 20 59 59 5f 41 43 43 45  .#define YY_ACCE
25d28 50 54 5f 41 43 54 49 4f 4e 20 20 28 59 59 4e 53  PT_ACTION  (YYNS
25d29 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b 31 29 0a  TATE+YYNRULE+1).
25d2a 23 64 65 66 69 6e 65 20 59 59 5f 45 52 52 4f 52  #define YY_ERROR
25d2b 5f 41 43 54 49 4f 4e 20 20 20 28 59 59 4e 53 54  _ACTION   (YYNST
25d2c 41 54 45 2b 59 59 4e 52 55 4c 45 29 0a 0a 2f 2a  ATE+YYNRULE)../*
25d2d 20 54 68 65 20 79 79 7a 65 72 6f 6d 69 6e 6f 72   The yyzerominor
25d2e 20 63 6f 6e 73 74 61 6e 74 20 69 73 20 75 73 65   constant is use
25d2f 64 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20  d to initialize 
25d30 69 6e 73 74 61 6e 63 65 73 20 6f 66 0a 2a 2a 20  instances of.** 
25d31 59 59 4d 49 4e 4f 52 54 59 50 45 20 6f 62 6a 65  YYMINORTYPE obje
25d32 63 74 73 20 74 6f 20 7a 65 72 6f 2e 20 2a 2f 0a  cts to zero. */.
25d33 73 74 61 74 69 63 20 63 6f 6e 73 74 20 59 59 4d  static const YYM
25d34 49 4e 4f 52 54 59 50 45 20 79 79 7a 65 72 6f 6d  INORTYPE yyzerom
25d35 69 6e 6f 72 3b 0a 0a 2f 2a 20 4e 65 78 74 20 61  inor;../* Next a
25d36 72 65 20 74 68 61 74 20 74 61 62 6c 65 73 20 75  re that tables u
25d37 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  sed to determine
25d38 20 77 68 61 74 20 61 63 74 69 6f 6e 20 74 6f 20   what action to 
25d39 74 61 6b 65 20 62 61 73 65 64 20 6f 6e 20 74 68  take based on th
25d3a 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 73 74 61  e.** current sta
25d3b 74 65 20 61 6e 64 20 6c 6f 6f 6b 61 68 65 61 64  te and lookahead
25d3c 20 74 6f 6b 65 6e 2e 20 20 54 68 65 73 65 20 74   token.  These t
25d3d 61 62 6c 65 73 20 61 72 65 20 75 73 65 64 20 74  ables are used t
25d3e 6f 20 69 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 66  o implement.** f
25d3f 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 74 61  unctions that ta
25d40 6b 65 20 61 20 73 74 61 74 65 20 6e 75 6d 62 65  ke a state numbe
25d41 72 20 61 6e 64 20 6c 6f 6f 6b 61 68 65 61 64 20  r and lookahead 
25d42 76 61 6c 75 65 20 61 6e 64 20 72 65 74 75 72 6e  value and return
25d43 20 61 6e 0a 2a 2a 20 61 63 74 69 6f 6e 20 69 6e   an.** action in
25d44 74 65 67 65 72 2e 20 20 0a 2a 2a 0a 2a 2a 20 53  teger.  .**.** S
25d45 75 70 70 6f 73 65 20 74 68 65 20 61 63 74 69 6f  uppose the actio
25d46 6e 20 69 6e 74 65 67 65 72 20 69 73 20 4e 2e 20  n integer is N. 
25d47 20 54 68 65 6e 20 74 68 65 20 61 63 74 69 6f 6e   Then the action
25d48 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 61   is determined a
25d49 73 0a 2a 2a 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 0a  s.** follows.**.
25d4a 2a 2a 20 20 20 30 20 3c 3d 20 4e 20 3c 20 59 59  **   0 <= N < YY
25d4b 4e 53 54 41 54 45 20 20 20 20 20 20 20 20 20 20  NSTATE          
25d4c 20 20 20 20 20 20 20 20 53 68 69 66 74 20 4e 2e          Shift N.
25d4d 20 20 54 68 61 74 20 69 73 2c 20 70 75 73 68 20    That is, push 
25d4e 74 68 65 20 6c 6f 6f 6b 61 68 65 61 64 0a 2a 2a  the lookahead.**
25d4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25d51 20 20 20 20 20 20 74 6f 6b 65 6e 20 6f 6e 74 6f        token onto
25d52 20 74 68 65 20 73 74 61 63 6b 20 61 6e 64 20 67   the stack and g
25d53 6f 74 6f 20 73 74 61 74 65 20 4e 2e 0a 2a 2a 0a  oto state N..**.
25d54 2a 2a 20 20 20 59 59 4e 53 54 41 54 45 20 3c 3d  **   YYNSTATE <=
25d55 20 4e 20 3c 20 59 59 4e 53 54 41 54 45 2b 59 59   N < YYNSTATE+YY
25d56 4e 52 55 4c 45 20 20 20 52 65 64 75 63 65 20 62  NRULE   Reduce b
25d57 79 20 72 75 6c 65 20 4e 2d 59 59 4e 53 54 41 54  y rule N-YYNSTAT
25d58 45 2e 0a 2a 2a 0a 2a 2a 20 20 20 4e 20 3d 3d 20  E..**.**   N == 
25d59 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45  YYNSTATE+YYNRULE
25d5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 41 20                A 
25d5b 73 79 6e 74 61 78 20 65 72 72 6f 72 20 68 61 73  syntax error has
25d5c 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 0a 2a 2a   occurred..**.**
25d5d 20 20 20 4e 20 3d 3d 20 59 59 4e 53 54 41 54 45     N == YYNSTATE
25d5e 2b 59 59 4e 52 55 4c 45 2b 31 20 20 20 20 20 20  +YYNRULE+1      
25d5f 20 20 20 20 20 20 54 68 65 20 70 61 72 73 65 72        The parser
25d60 20 61 63 63 65 70 74 73 20 69 74 73 20 69 6e 70   accepts its inp
25d61 75 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 4e 20 3d 3d  ut..**.**   N ==
25d62 20 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c   YYNSTATE+YYNRUL
25d63 45 2b 32 20 20 20 20 20 20 20 20 20 20 20 20 4e  E+2            N
25d64 6f 20 73 75 63 68 20 61 63 74 69 6f 6e 2e 20 20  o such action.  
25d65 44 65 6e 6f 74 65 73 20 75 6e 75 73 65 64 0a 2a  Denotes unused.*
25d66 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
25d67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25d68 20 20 20 20 20 20 20 73 6c 6f 74 73 20 69 6e 20         slots in 
25d69 74 68 65 20 79 79 5f 61 63 74 69 6f 6e 5b 5d 20  the yy_action[] 
25d6a 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  table..**.** The
25d6b 20 61 63 74 69 6f 6e 20 74 61 62 6c 65 20 69 73   action table is
25d6c 20 63 6f 6e 73 74 72 75 63 74 65 64 20 61 73 20   constructed as 
25d6d 61 20 73 69 6e 67 6c 65 20 6c 61 72 67 65 20 74  a single large t
25d6e 61 62 6c 65 20 6e 61 6d 65 64 20 79 79 5f 61 63  able named yy_ac
25d6f 74 69 6f 6e 5b 5d 2e 0a 2a 2a 20 47 69 76 65 6e  tion[]..** Given
25d70 20 73 74 61 74 65 20 53 20 61 6e 64 20 6c 6f 6f   state S and loo
25d71 6b 61 68 65 61 64 20 58 2c 20 74 68 65 20 61 63  kahead X, the ac
25d72 74 69 6f 6e 20 69 73 20 63 6f 6d 70 75 74 65 64  tion is computed
25d73 20 61 73 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 79   as.**.**      y
25d74 79 5f 61 63 74 69 6f 6e 5b 20 79 79 5f 73 68 69  y_action[ yy_shi
25d75 66 74 5f 6f 66 73 74 5b 53 5d 20 2b 20 58 20 5d  ft_ofst[S] + X ]
25d76 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e  .**.** If the in
25d77 64 65 78 20 76 61 6c 75 65 20 79 79 5f 73 68 69  dex value yy_shi
25d78 66 74 5f 6f 66 73 74 5b 53 5d 2b 58 20 69 73 20  ft_ofst[S]+X is 
25d79 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 6f 72 20  out of range or 
25d7a 69 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20  if the value.** 
25d7b 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 79 79 5f  yy_lookahead[yy_
25d7c 73 68 69 66 74 5f 6f 66 73 74 5b 53 5d 2b 58 5d  shift_ofst[S]+X]
25d7d 20 69 73 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f   is not equal to
25d7e 20 58 20 6f 72 20 69 66 20 79 79 5f 73 68 69 66   X or if yy_shif
25d7f 74 5f 6f 66 73 74 5b 53 5d 0a 2a 2a 20 69 73 20  t_ofst[S].** is 
25d80 65 71 75 61 6c 20 74 6f 20 59 59 5f 53 48 49 46  equal to YY_SHIF
25d81 54 5f 55 53 45 5f 44 46 4c 54 2c 20 69 74 20 6d  T_USE_DFLT, it m
25d82 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 61 63  eans that the ac
25d83 74 69 6f 6e 20 69 73 20 6e 6f 74 20 69 6e 20 74  tion is not in t
25d84 68 65 20 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20  he table.** and 
25d85 74 68 61 74 20 79 79 5f 64 65 66 61 75 6c 74 5b  that yy_default[
25d86 53 5d 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65  S] should be use
25d87 64 20 69 6e 73 74 65 61 64 2e 20 20 0a 2a 2a 0a  d instead.  .**.
25d88 2a 2a 20 54 68 65 20 66 6f 72 6d 75 6c 61 20 61  ** The formula a
25d89 62 6f 76 65 20 69 73 20 66 6f 72 20 63 6f 6d 70  bove is for comp
25d8a 75 74 69 6e 67 20 74 68 65 20 61 63 74 69 6f 6e  uting the action
25d8b 20 77 68 65 6e 20 74 68 65 20 6c 6f 6f 6b 61 68   when the lookah
25d8c 65 61 64 20 69 73 0a 2a 2a 20 61 20 74 65 72 6d  ead is.** a term
25d8d 69 6e 61 6c 20 73 79 6d 62 6f 6c 2e 20 20 49 66  inal symbol.  If
25d8e 20 74 68 65 20 6c 6f 6f 6b 61 68 65 61 64 20 69   the lookahead i
25d8f 73 20 61 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c  s a non-terminal
25d90 20 28 61 73 20 6f 63 63 75 72 73 20 61 66 74 65   (as occurs afte
25d91 72 0a 2a 2a 20 61 20 72 65 64 75 63 65 20 61 63  r.** a reduce ac
25d92 74 69 6f 6e 29 20 74 68 65 6e 20 74 68 65 20 79  tion) then the y
25d93 79 5f 72 65 64 75 63 65 5f 6f 66 73 74 5b 5d 20  y_reduce_ofst[] 
25d94 61 72 72 61 79 20 69 73 20 75 73 65 64 20 69 6e  array is used in
25d95 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 74 68 65   place of.** the
25d96 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 5d   yy_shift_ofst[]
25d97 20 61 72 72 61 79 20 61 6e 64 20 59 59 5f 52 45   array and YY_RE
25d98 44 55 43 45 5f 55 53 45 5f 44 46 4c 54 20 69 73  DUCE_USE_DFLT is
25d99 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20 6f   used in place o
25d9a 66 0a 2a 2a 20 59 59 5f 53 48 49 46 54 5f 55 53  f.** YY_SHIFT_US
25d9b 45 5f 44 46 4c 54 2e 0a 2a 2a 0a 2a 2a 20 54 68  E_DFLT..**.** Th
25d9c 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20  e following are 
25d9d 74 68 65 20 74 61 62 6c 65 73 20 67 65 6e 65 72  the tables gener
25d9e 61 74 65 64 20 69 6e 20 74 68 69 73 20 73 65 63  ated in this sec
25d9f 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 79 79 5f  tion:.**.**  yy_
25da0 61 63 74 69 6f 6e 5b 5d 20 20 20 20 20 20 20 20  action[]        
25da1 41 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 63  A single table c
25da2 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 61 63  ontaining all ac
25da3 74 69 6f 6e 73 2e 0a 2a 2a 20 20 79 79 5f 6c 6f  tions..**  yy_lo
25da4 6f 6b 61 68 65 61 64 5b 5d 20 20 20 20 20 41 20  okahead[]     A 
25da5 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67  table containing
25da6 20 74 68 65 20 6c 6f 6f 6b 61 68 65 61 64 20 66   the lookahead f
25da7 6f 72 20 65 61 63 68 20 65 6e 74 72 79 20 69 6e  or each entry in
25da8 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
25da9 20 20 20 20 20 20 20 20 79 79 5f 61 63 74 69 6f          yy_actio
25daa 6e 2e 20 20 55 73 65 64 20 74 6f 20 64 65 74 65  n.  Used to dete
25dab 63 74 20 68 61 73 68 20 63 6f 6c 6c 69 73 69 6f  ct hash collisio
25dac 6e 73 2e 0a 2a 2a 20 20 79 79 5f 73 68 69 66 74  ns..**  yy_shift
25dad 5f 6f 66 73 74 5b 5d 20 20 20 20 46 6f 72 20 65  _ofst[]    For e
25dae 61 63 68 20 73 74 61 74 65 2c 20 74 68 65 20 6f  ach state, the o
25daf 66 66 73 65 74 20 69 6e 74 6f 20 79 79 5f 61 63  ffset into yy_ac
25db0 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 20 20 20 20  tion for.**     
25db1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25db2 73 68 69 66 74 69 6e 67 20 74 65 72 6d 69 6e 61  shifting termina
25db3 6c 73 2e 0a 2a 2a 20 20 79 79 5f 72 65 64 75 63  ls..**  yy_reduc
25db4 65 5f 6f 66 73 74 5b 5d 20 20 20 46 6f 72 20 65  e_ofst[]   For e
25db5 61 63 68 20 73 74 61 74 65 2c 20 74 68 65 20 6f  ach state, the o
25db6 66 66 73 65 74 20 69 6e 74 6f 20 79 79 5f 61 63  ffset into yy_ac
25db7 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 20 20 20 20  tion for.**     
25db8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25db9 73 68 69 66 74 69 6e 67 20 6e 6f 6e 2d 74 65 72  shifting non-ter
25dba 6d 69 6e 61 6c 73 20 61 66 74 65 72 20 61 20 72  minals after a r
25dbb 65 64 75 63 65 2e 0a 2a 2a 20 20 79 79 5f 64 65  educe..**  yy_de
25dbc 66 61 75 6c 74 5b 5d 20 20 20 20 20 20 20 44 65  fault[]       De
25dbd 66 61 75 6c 74 20 61 63 74 69 6f 6e 20 66 6f 72  fault action for
25dbe 20 65 61 63 68 20 73 74 61 74 65 2e 0a 2a 2f 0a   each state..*/.
25dbf 73 74 61 74 69 63 20 63 6f 6e 73 74 20 59 59 41  static const YYA
25dc0 43 54 49 4f 4e 54 59 50 45 20 79 79 5f 61 63 74  CTIONTYPE yy_act
25dc1 69 6f 6e 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20  ion[] = {. /*   
25dc2 20 20 30 20 2a 2f 20 20 20 32 39 32 2c 20 20 39    0 */   292,  9
25dc3 30 33 2c 20 20 31 32 34 2c 20 20 35 38 38 2c 20  03,  124,  588, 
25dc4 20 34 30 39 2c 20 20 31 37 32 2c 20 20 20 20 32   409,  172,    2
25dc5 2c 20 20 34 31 38 2c 20 20 20 36 31 2c 20 20 20  ,  418,   61,   
25dc6 36 31 2c 0a 20 2f 2a 20 20 20 20 31 30 20 2a 2f  61,. /*    10 */
25dc7 20 20 20 20 36 31 2c 20 20 20 36 31 2c 20 20 35      61,   61,  5
25dc8 31 39 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20  19,   63,   63, 
25dc9 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36 34    63,   63,   64
25dca 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 0a 20 2f  ,   64,   65,. /
25dcb 2a 20 20 20 20 32 30 20 2a 2f 20 20 20 20 36 35  *    20 */    65
25dcc 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 32  ,   65,   66,  2
25dcd 31 30 2c 20 20 34 34 37 2c 20 20 32 31 32 2c 20  10,  447,  212, 
25dce 20 34 32 35 2c 20 20 34 33 31 2c 20 20 20 36 38   425,  431,   68
25dcf 2c 20 20 20 36 33 2c 0a 20 2f 2a 20 20 20 20 33  ,   63,. /*    3
25dd0 30 20 2a 2f 20 20 20 20 36 33 2c 20 20 20 36 33  0 */    63,   63
25dd1 2c 20 20 20 36 33 2c 20 20 20 36 34 2c 20 20 20  ,   63,   64,   
25dd2 36 34 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 20  64,   65,   65, 
25dd3 20 20 36 35 2c 20 20 20 36 36 2c 20 20 32 31 30    65,   66,  210
25dd4 2c 0a 20 2f 2a 20 20 20 20 34 30 20 2a 2f 20 20  ,. /*    40 */  
25dd5 20 33 39 31 2c 20 20 33 38 38 2c 20 20 33 39 36   391,  388,  396
25dd6 2c 20 20 34 35 31 2c 20 20 20 36 30 2c 20 20 20  ,  451,   60,   
25dd7 35 39 2c 20 20 32 39 37 2c 20 20 34 33 35 2c 20  59,  297,  435, 
25dd8 20 34 33 36 2c 20 20 34 33 32 2c 0a 20 2f 2a 20   436,  432,. /* 
25dd9 20 20 20 35 30 20 2a 2f 20 20 20 34 33 32 2c 20     50 */   432, 
25dda 20 20 36 32 2c 20 20 20 36 32 2c 20 20 20 36 31    62,   62,   61
25ddb 2c 20 20 20 36 31 2c 20 20 20 36 31 2c 20 20 20  ,   61,   61,   
25ddc 36 31 2c 20 20 32 36 33 2c 20 20 20 36 33 2c 20  61,  263,   63, 
25ddd 20 20 36 33 2c 0a 20 2f 2a 20 20 20 20 36 30 20    63,. /*    60 
25dde 2a 2f 20 20 20 20 36 33 2c 20 20 20 36 33 2c 20  */    63,   63, 
25ddf 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36 35    64,   64,   65
25de0 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20  ,   65,   65,   
25de1 36 36 2c 20 20 32 31 30 2c 20 20 32 39 32 2c 0a  66,  210,  292,.
25de2 20 2f 2a 20 20 20 20 37 30 20 2a 2f 20 20 20 34   /*    70 */   4
25de3 39 33 2c 20 20 34 39 34 2c 20 20 34 31 38 2c 20  93,  494,  418, 
25de4 20 34 38 39 2c 20 20 32 30 38 2c 20 20 20 38 32   489,  208,   82
25de5 2c 20 20 20 36 37 2c 20 20 34 32 30 2c 20 20 20  ,   67,  420,   
25de6 36 39 2c 20 20 31 35 34 2c 0a 20 2f 2a 20 20 20  69,  154,. /*   
25de7 20 38 30 20 2a 2f 20 20 20 20 36 33 2c 20 20 20   80 */    63,   
25de8 36 33 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20  63,   63,   63, 
25de9 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36 35    64,   64,   65
25dea 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20  ,   65,   65,   
25deb 36 36 2c 0a 20 2f 2a 20 20 20 20 39 30 20 2a 2f  66,. /*    90 */
25dec 20 20 20 32 31 30 2c 20 20 20 36 37 2c 20 20 34     210,   67,  4
25ded 36 32 2c 20 20 20 36 39 2c 20 20 31 35 34 2c 20  62,   69,  154, 
25dee 20 34 32 35 2c 20 20 34 33 31 2c 20 20 35 37 34   425,  431,  574
25def 2c 20 20 32 36 34 2c 20 20 20 35 38 2c 0a 20 2f  ,  264,   58,. /
25df0 2a 20 20 20 31 30 30 20 2a 2f 20 20 20 20 36 34  *   100 */    64
25df1 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20  ,   64,   65,   
25df2 36 35 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20  65,   65,   66, 
25df3 20 32 31 30 2c 20 20 33 39 37 2c 20 20 33 39 38   210,  397,  398
25df4 2c 20 20 34 32 32 2c 0a 20 2f 2a 20 20 20 31 31  ,  422,. /*   11
25df5 30 20 2a 2f 20 20 20 34 32 32 2c 20 20 34 32 32  0 */   422,  422
25df6 2c 20 20 32 39 32 2c 20 20 20 36 30 2c 20 20 20  ,  292,   60,   
25df7 35 39 2c 20 20 32 39 37 2c 20 20 34 33 35 2c 20  59,  297,  435, 
25df8 20 34 33 36 2c 20 20 34 33 32 2c 20 20 34 33 32   436,  432,  432
25df9 2c 0a 20 2f 2a 20 20 20 31 32 30 20 2a 2f 20 20  ,. /*   120 */  
25dfa 20 20 36 32 2c 20 20 20 36 32 2c 20 20 20 36 31    62,   62,   61
25dfb 2c 20 20 20 36 31 2c 20 20 20 36 31 2c 20 20 20  ,   61,   61,   
25dfc 36 31 2c 20 20 33 31 37 2c 20 20 20 36 33 2c 20  61,  317,   63, 
25dfd 20 20 36 33 2c 20 20 20 36 33 2c 0a 20 2f 2a 20    63,   63,. /* 
25dfe 20 20 31 33 30 20 2a 2f 20 20 20 20 36 33 2c 20    130 */    63, 
25dff 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36 35    64,   64,   65
25e00 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20  ,   65,   65,   
25e01 36 36 2c 20 20 32 31 30 2c 20 20 34 32 35 2c 20  66,  210,  425, 
25e02 20 34 33 31 2c 0a 20 2f 2a 20 20 20 31 34 30 20   431,. /*   140 
25e03 2a 2f 20 20 20 20 39 34 2c 20 20 20 36 35 2c 20  */    94,   65, 
25e04 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 36    65,   65,   66
25e05 2c 20 20 32 31 30 2c 20 20 33 39 36 2c 20 20 32  ,  210,  396,  2
25e06 31 30 2c 20 20 34 31 34 2c 20 20 20 33 34 2c 0a  10,  414,   34,.
25e07 20 2f 2a 20 20 20 31 35 30 20 2a 2f 20 20 20 20   /*   150 */    
25e08 35 36 2c 20 20 32 39 38 2c 20 20 34 34 32 2c 20  56,  298,  442, 
25e09 20 34 34 33 2c 20 20 34 31 30 2c 20 20 34 31 38   443,  410,  418
25e0a 2c 20 20 20 36 30 2c 20 20 20 35 39 2c 20 20 32  ,   60,   59,  2
25e0b 39 37 2c 20 20 34 33 35 2c 0a 20 2f 2a 20 20 20  97,  435,. /*   
25e0c 31 36 30 20 2a 2f 20 20 20 34 33 36 2c 20 20 34  160 */   436,  4
25e0d 33 32 2c 20 20 34 33 32 2c 20 20 20 36 32 2c 20  32,  432,   62, 
25e0e 20 20 36 32 2c 20 20 20 36 31 2c 20 20 20 36 31    62,   61,   61
25e0f 2c 20 20 20 36 31 2c 20 20 20 36 31 2c 20 20 32  ,   61,   61,  2
25e10 30 38 2c 0a 20 2f 2a 20 20 20 31 37 30 20 2a 2f  08,. /*   170 */
25e11 20 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20      63,   63,   
25e12 36 33 2c 20 20 20 36 33 2c 20 20 20 36 34 2c 20  63,   63,   64, 
25e13 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20 36 35    64,   65,   65
25e14 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 0a 20 2f  ,   65,   66,. /
25e15 2a 20 20 20 31 38 30 20 2a 2f 20 20 20 32 31 30  *   180 */   210
25e16 2c 20 20 32 39 32 2c 20 20 33 37 32 2c 20 20 35  ,  292,  372,  5
25e17 32 34 2c 20 20 32 39 35 2c 20 20 35 37 32 2c 20  24,  295,  572, 
25e18 20 31 31 33 2c 20 20 34 30 38 2c 20 20 35 32 32   113,  408,  522
25e19 2c 20 20 34 35 31 2c 0a 20 2f 2a 20 20 20 31 39  ,  451,. /*   19
25e1a 30 20 2a 2f 20 20 20 33 33 31 2c 20 20 33 31 37  0 */   331,  317
25e1b 2c 20 20 34 30 37 2c 20 20 20 32 30 2c 20 20 32  ,  407,   20,  2
25e1c 34 34 2c 20 20 33 34 30 2c 20 20 35 31 39 2c 20  44,  340,  519, 
25e1d 20 33 39 36 2c 20 20 34 37 38 2c 20 20 35 33 31   396,  478,  531
25e1e 2c 0a 20 2f 2a 20 20 20 32 30 30 20 2a 2f 20 20  ,. /*   200 */  
25e1f 20 35 30 35 2c 20 20 34 34 37 2c 20 20 32 31 32   505,  447,  212
25e20 2c 20 20 35 37 31 2c 20 20 35 37 30 2c 20 20 32  ,  571,  570,  2
25e21 34 35 2c 20 20 35 33 30 2c 20 20 34 32 35 2c 20  45,  530,  425, 
25e22 20 34 33 31 2c 20 20 31 34 39 2c 0a 20 2f 2a 20   431,  149,. /* 
25e23 20 20 32 31 30 20 2a 2f 20 20 20 31 35 30 2c 20    210 */   150, 
25e24 20 33 39 37 2c 20 20 33 39 38 2c 20 20 34 31 34   397,  398,  414
25e25 2c 20 20 20 34 31 2c 20 20 32 31 31 2c 20 20 31  ,   41,  211,  1
25e26 35 31 2c 20 20 35 33 33 2c 20 20 34 38 38 2c 20  51,  533,  488, 
25e27 20 34 38 39 2c 0a 20 2f 2a 20 20 20 32 32 30 20   489,. /*   220 
25e28 2a 2f 20 20 20 34 31 38 2c 20 20 35 36 38 2c 20  */   418,  568, 
25e29 20 35 36 39 2c 20 20 34 32 30 2c 20 20 32 39 32   569,  420,  292
25e2a 2c 20 20 20 36 30 2c 20 20 20 35 39 2c 20 20 32  ,   60,   59,  2
25e2b 39 37 2c 20 20 34 33 35 2c 20 20 34 33 36 2c 0a  97,  435,  436,.
25e2c 20 2f 2a 20 20 20 32 33 30 20 2a 2f 20 20 20 34   /*   230 */   4
25e2d 33 32 2c 20 20 34 33 32 2c 20 20 20 36 32 2c 20  32,  432,   62, 
25e2e 20 20 36 32 2c 20 20 20 36 31 2c 20 20 20 36 31    62,   61,   61
25e2f 2c 20 20 20 36 31 2c 20 20 20 36 31 2c 20 20 33  ,   61,   61,  3
25e30 31 37 2c 20 20 20 36 33 2c 0a 20 2f 2a 20 20 20  17,   63,. /*   
25e31 32 34 30 20 2a 2f 20 20 20 20 36 33 2c 20 20 20  240 */    63,   
25e32 36 33 2c 20 20 20 36 33 2c 20 20 20 36 34 2c 20  63,   63,   64, 
25e33 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20 36 35    64,   65,   65
25e34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 32  ,   65,   66,  2
25e35 31 30 2c 0a 20 2f 2a 20 20 20 32 35 30 20 2a 2f  10,. /*   250 */
25e36 20 20 20 34 32 35 2c 20 20 34 33 31 2c 20 20 34     425,  431,  4
25e37 34 37 2c 20 20 33 33 33 2c 20 20 32 31 35 2c 20  47,  333,  215, 
25e38 20 34 32 32 2c 20 20 34 32 32 2c 20 20 34 32 32   422,  422,  422
25e39 2c 20 20 33 36 33 2c 20 20 32 39 39 2c 0a 20 2f  ,  363,  299,. /
25e3a 2a 20 20 20 32 36 30 20 2a 2f 20 20 20 34 31 34  *   260 */   414
25e3b 2c 20 20 20 34 31 2c 20 20 33 39 37 2c 20 20 33  ,   41,  397,  3
25e3c 39 38 2c 20 20 33 36 36 2c 20 20 35 36 37 2c 20  98,  366,  567, 
25e3d 20 32 31 31 2c 20 20 32 39 32 2c 20 20 20 36 30   211,  292,   60
25e3e 2c 20 20 20 35 39 2c 0a 20 2f 2a 20 20 20 32 37  ,   59,. /*   27
25e3f 30 20 2a 2f 20 20 20 32 39 37 2c 20 20 34 33 35  0 */   297,  435
25e40 2c 20 20 34 33 36 2c 20 20 34 33 32 2c 20 20 34  ,  436,  432,  4
25e41 33 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20  32,   62,   62, 
25e42 20 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31    61,   61,   61
25e43 2c 0a 20 2f 2a 20 20 20 32 38 30 20 2a 2f 20 20  ,. /*   280 */  
25e44 20 20 36 31 2c 20 20 33 39 36 2c 20 20 20 36 33    61,  396,   63
25e45 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20  ,   63,   63,   
25e46 36 33 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20  63,   64,   64, 
25e47 20 20 36 35 2c 20 20 20 36 35 2c 0a 20 2f 2a 20    65,   65,. /* 
25e48 20 20 32 39 30 20 2a 2f 20 20 20 20 36 35 2c 20    290 */    65, 
25e49 20 20 36 36 2c 20 20 32 31 30 2c 20 20 34 32 35    66,  210,  425
25e4a 2c 20 20 34 33 31 2c 20 20 34 39 31 2c 20 20 33  ,  431,  491,  3
25e4b 30 30 2c 20 20 35 32 34 2c 20 20 34 37 34 2c 20  00,  524,  474, 
25e4c 20 20 36 36 2c 0a 20 2f 2a 20 20 20 33 30 30 20    66,. /*   300 
25e4d 2a 2f 20 20 20 32 31 30 2c 20 20 32 31 34 2c 20  */   210,  214, 
25e4e 20 34 37 34 2c 20 20 32 32 39 2c 20 20 34 31 31   474,  229,  411
25e4f 2c 20 20 32 38 36 2c 20 20 35 33 34 2c 20 20 20  ,  286,  534,   
25e50 32 30 2c 20 20 34 34 39 2c 20 20 35 32 33 2c 0a  20,  449,  523,.
25e51 20 2f 2a 20 20 20 33 31 30 20 2a 2f 20 20 20 31   /*   310 */   1
25e52 36 38 2c 20 20 20 36 30 2c 20 20 20 35 39 2c 20  68,   60,   59, 
25e53 20 32 39 37 2c 20 20 34 33 35 2c 20 20 34 33 36   297,  435,  436
25e54 2c 20 20 34 33 32 2c 20 20 34 33 32 2c 20 20 20  ,  432,  432,   
25e55 36 32 2c 20 20 20 36 32 2c 0a 20 2f 2a 20 20 20  62,   62,. /*   
25e56 33 32 30 20 2a 2f 20 20 20 20 36 31 2c 20 20 20  320 */    61,   
25e57 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31 2c 20  61,   61,   61, 
25e58 20 34 37 34 2c 20 20 20 36 33 2c 20 20 20 36 33   474,   63,   63
25e59 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20  ,   63,   63,   
25e5a 36 34 2c 0a 20 2f 2a 20 20 20 33 33 30 20 2a 2f  64,. /*   330 */
25e5b 20 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20      64,   65,   
25e5c 36 35 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20  65,   65,   66, 
25e5d 20 32 31 30 2c 20 20 32 30 39 2c 20 20 34 38 30   210,  209,  480
25e5e 2c 20 20 33 31 37 2c 20 20 20 37 37 2c 0a 20 2f  ,  317,   77,. /
25e5f 2a 20 20 20 33 34 30 20 2a 2f 20 20 20 32 39 32  *   340 */   292
25e60 2c 20 20 32 33 39 2c 20 20 33 30 30 2c 20 20 20  ,  239,  300,   
25e61 35 35 2c 20 20 34 38 34 2c 20 20 34 39 30 2c 20  55,  484,  490, 
25e62 20 33 39 37 2c 20 20 33 39 38 2c 20 20 31 38 31   397,  398,  181
25e63 2c 20 20 35 34 37 2c 0a 20 2f 2a 20 20 20 33 35  ,  547,. /*   35
25e64 30 20 2a 2f 20 20 20 34 39 34 2c 20 20 33 34 35  0 */   494,  345
25e65 2c 20 20 33 34 38 2c 20 20 33 34 39 2c 20 20 20  ,  348,  349,   
25e66 36 37 2c 20 20 31 35 32 2c 20 20 20 36 39 2c 20  67,  152,   69, 
25e67 20 31 35 34 2c 20 20 33 33 39 2c 20 20 35 32 34   154,  339,  524
25e68 2c 0a 20 2f 2a 20 20 20 33 36 30 20 2a 2f 20 20  ,. /*   360 */  
25e69 20 34 31 34 2c 20 20 20 33 35 2c 20 20 33 35 30   414,   35,  350
25e6a 2c 20 20 32 34 31 2c 20 20 32 32 31 2c 20 20 33  ,  241,  221,  3
25e6b 37 30 2c 20 20 34 32 35 2c 20 20 34 33 31 2c 20  70,  425,  431, 
25e6c 20 35 37 39 2c 20 20 20 32 30 2c 0a 20 2f 2a 20   579,   20,. /* 
25e6d 20 20 33 37 30 20 2a 2f 20 20 20 31 36 34 2c 20    370 */   164, 
25e6e 20 31 31 38 2c 20 20 32 34 33 2c 20 20 33 34 33   118,  243,  343
25e6f 2c 20 20 32 34 38 2c 20 20 33 34 34 2c 20 20 31  ,  248,  344,  1
25e70 37 36 2c 20 20 33 32 32 2c 20 20 34 34 32 2c 20  76,  322,  442, 
25e71 20 34 34 33 2c 0a 20 2f 2a 20 20 20 33 38 30 20   443,. /*   380 
25e72 2a 2f 20 20 20 34 31 34 2c 20 20 20 20 33 2c 20  */   414,    3, 
25e73 20 20 38 30 2c 20 20 32 35 32 2c 20 20 20 36 30    80,  252,   60
25e74 2c 20 20 20 35 39 2c 20 20 32 39 37 2c 20 20 34  ,   59,  297,  4
25e75 33 35 2c 20 20 34 33 36 2c 20 20 34 33 32 2c 0a  35,  436,  432,.
25e76 20 2f 2a 20 20 20 33 39 30 20 2a 2f 20 20 20 34   /*   390 */   4
25e77 33 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20  32,   62,   62, 
25e78 20 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31    61,   61,   61
25e79 2c 20 20 20 36 31 2c 20 20 31 37 34 2c 20 20 20  ,   61,  174,   
25e7a 36 33 2c 20 20 20 36 33 2c 0a 20 2f 2a 20 20 20  63,   63,. /*   
25e7b 34 30 30 20 2a 2f 20 20 20 20 36 33 2c 20 20 20  400 */    63,   
25e7c 36 33 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20  63,   64,   64, 
25e7d 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 35    65,   65,   65
25e7e 2c 20 20 20 36 36 2c 20 20 32 31 30 2c 20 20 32  ,   66,  210,  2
25e7f 39 32 2c 0a 20 2f 2a 20 20 20 34 31 30 20 2a 2f  92,. /*   410 */
25e80 20 20 20 32 32 31 2c 20 20 35 35 30 2c 20 20 32     221,  550,  2
25e81 33 36 2c 20 20 34 38 37 2c 20 20 35 31 30 2c 20  36,  487,  510, 
25e82 20 33 35 33 2c 20 20 33 31 37 2c 20 20 31 31 38   353,  317,  118
25e83 2c 20 20 32 34 33 2c 20 20 33 34 33 2c 0a 20 2f  ,  243,  343,. /
25e84 2a 20 20 20 34 32 30 20 2a 2f 20 20 20 32 34 38  *   420 */   248
25e85 2c 20 20 33 34 34 2c 20 20 31 37 36 2c 20 20 31  ,  344,  176,  1
25e86 38 31 2c 20 20 33 31 37 2c 20 20 35 33 32 2c 20  81,  317,  532, 
25e87 20 33 34 35 2c 20 20 33 34 38 2c 20 20 33 34 39   345,  348,  349
25e88 2c 20 20 32 35 32 2c 0a 20 2f 2a 20 20 20 34 33  ,  252,. /*   43
25e89 30 20 2a 2f 20 20 20 32 32 33 2c 20 20 34 31 35  0 */   223,  415
25e8a 2c 20 20 31 35 35 2c 20 20 34 36 34 2c 20 20 35  ,  155,  464,  5
25e8b 31 31 2c 20 20 34 32 35 2c 20 20 34 33 31 2c 20  11,  425,  431, 
25e8c 20 33 35 30 2c 20 20 34 31 34 2c 20 20 20 33 34   350,  414,   34
25e8d 2c 0a 20 2f 2a 20 20 20 34 34 30 20 2a 2f 20 20  ,. /*   440 */  
25e8e 20 34 36 35 2c 20 20 32 31 31 2c 20 20 31 37 37   465,  211,  177
25e8f 2c 20 20 31 37 35 2c 20 20 31 36 30 2c 20 20 35  ,  175,  160,  5
25e90 32 35 2c 20 20 34 31 34 2c 20 20 20 33 34 2c 20  25,  414,   34, 
25e91 20 33 33 38 2c 20 20 35 34 39 2c 0a 20 2f 2a 20   338,  549,. /* 
25e92 20 20 34 35 30 20 2a 2f 20 20 20 34 34 39 2c 20    450 */   449, 
25e93 20 33 32 33 2c 20 20 31 36 38 2c 20 20 20 36 30   323,  168,   60
25e94 2c 20 20 20 35 39 2c 20 20 32 39 37 2c 20 20 34  ,   59,  297,  4
25e95 33 35 2c 20 20 34 33 36 2c 20 20 34 33 32 2c 20  35,  436,  432, 
25e96 20 34 33 32 2c 0a 20 2f 2a 20 20 20 34 36 30 20   432,. /*   460 
25e97 2a 2f 20 20 20 20 36 32 2c 20 20 20 36 32 2c 20  */    62,   62, 
25e98 20 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31    61,   61,   61
25e99 2c 20 20 20 36 31 2c 20 20 34 31 35 2c 20 20 20  ,   61,  415,   
25e9a 36 33 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 0a  63,   63,   63,.
25e9b 20 2f 2a 20 20 20 34 37 30 20 2a 2f 20 20 20 20   /*   470 */    
25e9c 36 33 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20  63,   64,   64, 
25e9d 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 35    65,   65,   65
25e9e 2c 20 20 20 36 36 2c 20 20 32 31 30 2c 20 20 32  ,   66,  210,  2
25e9f 39 32 2c 20 20 35 34 32 2c 0a 20 2f 2a 20 20 20  92,  542,. /*   
25ea0 34 38 30 20 2a 2f 20 20 20 33 33 35 2c 20 20 35  480 */   335,  5
25ea1 31 37 2c 20 20 35 30 34 2c 20 20 35 34 31 2c 20  17,  504,  541, 
25ea2 20 34 35 36 2c 20 20 35 37 32 2c 20 20 33 30 32   456,  572,  302
25ea3 2c 20 20 20 31 39 2c 20 20 33 33 31 2c 20 20 31  ,   19,  331,  1
25ea4 34 34 2c 0a 20 2f 2a 20 20 20 34 39 30 20 2a 2f  44,. /*   490 */
25ea5 20 20 20 33 31 37 2c 20 20 33 39 30 2c 20 20 33     317,  390,  3
25ea6 31 37 2c 20 20 33 33 30 2c 20 20 20 20 32 2c 20  17,  330,    2, 
25ea7 20 33 36 32 2c 20 20 34 35 37 2c 20 20 32 39 34   362,  457,  294
25ea8 2c 20 20 34 38 33 2c 20 20 33 37 33 2c 0a 20 2f  ,  483,  373,. /
25ea9 2a 20 20 20 35 30 30 20 2a 2f 20 20 20 32 36 39  *   500 */   269
25eaa 2c 20 20 32 36 38 2c 20 20 32 35 32 2c 20 20 35  ,  268,  252,  5
25eab 37 31 2c 20 20 34 32 35 2c 20 20 34 33 31 2c 20  71,  425,  431, 
25eac 20 35 38 39 2c 20 20 33 39 31 2c 20 20 33 38 38   589,  391,  388
25ead 2c 20 20 34 35 38 2c 0a 20 2f 2a 20 20 20 35 31  ,  458,. /*   51
25eae 30 20 2a 2f 20 20 20 32 30 38 2c 20 20 34 39 35  0 */   208,  495
25eaf 2c 20 20 34 31 34 2c 20 20 20 34 39 2c 20 20 34  ,  414,   49,  4
25eb0 31 34 2c 20 20 20 34 39 2c 20 20 33 30 33 2c 20  14,   49,  303, 
25eb1 20 35 38 36 2c 20 20 38 39 34 2c 20 20 32 33 30   586,  894,  230
25eb2 2c 0a 20 2f 2a 20 20 20 35 32 30 20 2a 2f 20 20  ,. /*   520 */  
25eb3 20 38 39 34 2c 20 20 34 39 36 2c 20 20 20 36 30   894,  496,   60
25eb4 2c 20 20 20 35 39 2c 20 20 32 39 37 2c 20 20 34  ,   59,  297,  4
25eb5 33 35 2c 20 20 34 33 36 2c 20 20 34 33 32 2c 20  35,  436,  432, 
25eb6 20 34 33 32 2c 20 20 20 36 32 2c 0a 20 2f 2a 20   432,   62,. /* 
25eb7 20 20 35 33 30 20 2a 2f 20 20 20 20 36 32 2c 20    530 */    62, 
25eb8 20 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31    61,   61,   61
25eb9 2c 20 20 20 36 31 2c 20 20 32 30 31 2c 20 20 20  ,   61,  201,   
25eba 36 33 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20  63,   63,   63, 
25ebb 20 20 36 33 2c 0a 20 2f 2a 20 20 20 35 34 30 20    63,. /*   540 
25ebc 2a 2f 20 20 20 20 36 34 2c 20 20 20 36 34 2c 20  */    64,   64, 
25ebd 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 35    65,   65,   65
25ebe 2c 20 20 20 36 36 2c 20 20 32 31 30 2c 20 20 32  ,   66,  210,  2
25ebf 39 32 2c 20 20 33 31 37 2c 20 20 31 38 31 2c 0a  92,  317,  181,.
25ec0 20 2f 2a 20 20 20 35 35 30 20 2a 2f 20 20 20 34   /*   550 */   4
25ec1 33 39 2c 20 20 32 35 35 2c 20 20 33 34 35 2c 20  39,  255,  345, 
25ec2 20 33 34 38 2c 20 20 33 34 39 2c 20 20 33 37 30   348,  349,  370
25ec3 2c 20 20 31 35 33 2c 20 20 35 38 33 2c 20 20 33  ,  153,  583,  3
25ec4 30 38 2c 20 20 32 35 31 2c 0a 20 2f 2a 20 20 20  08,  251,. /*   
25ec5 35 36 30 20 2a 2f 20 20 20 33 30 39 2c 20 20 34  560 */   309,  4
25ec6 35 32 2c 20 20 20 37 36 2c 20 20 33 35 30 2c 20  52,   76,  350, 
25ec7 20 20 37 38 2c 20 20 33 38 32 2c 20 20 32 31 31    78,  382,  211
25ec8 2c 20 20 34 32 36 2c 20 20 34 32 37 2c 20 20 34  ,  426,  427,  4
25ec9 31 35 2c 0a 20 2f 2a 20 20 20 35 37 30 20 2a 2f  15,. /*   570 */
25eca 20 20 20 34 31 34 2c 20 20 20 32 37 2c 20 20 33     414,   27,  3
25ecb 31 39 2c 20 20 34 32 35 2c 20 20 34 33 31 2c 20  19,  425,  431, 
25ecc 20 34 34 30 2c 20 20 20 20 31 2c 20 20 20 32 32   440,    1,   22
25ecd 2c 20 20 35 38 36 2c 20 20 38 39 33 2c 0a 20 2f  ,  586,  893,. /
25ece 2a 20 20 20 35 38 30 20 2a 2f 20 20 20 33 39 36  *   580 */   396
25ecf 2c 20 20 38 39 33 2c 20 20 35 34 34 2c 20 20 34  ,  893,  544,  4
25ed0 37 38 2c 20 20 33 32 30 2c 20 20 32 36 33 2c 20  78,  320,  263, 
25ed1 20 34 33 38 2c 20 20 34 33 38 2c 20 20 34 32 39   438,  438,  429
25ed2 2c 20 20 34 33 30 2c 0a 20 2f 2a 20 20 20 35 39  ,  430,. /*   59
25ed3 30 20 2a 2f 20 20 20 34 31 35 2c 20 20 20 36 30  0 */   415,   60
25ed4 2c 20 20 20 35 39 2c 20 20 32 39 37 2c 20 20 34  ,   59,  297,  4
25ed5 33 35 2c 20 20 34 33 36 2c 20 20 34 33 32 2c 20  35,  436,  432, 
25ed6 20 34 33 32 2c 20 20 20 36 32 2c 20 20 20 36 32   432,   62,   62
25ed7 2c 0a 20 2f 2a 20 20 20 36 30 30 20 2a 2f 20 20  ,. /*   600 */  
25ed8 20 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31    61,   61,   61
25ed9 2c 20 20 20 36 31 2c 20 20 32 33 37 2c 20 20 20  ,   61,  237,   
25eda 36 33 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20  63,   63,   63, 
25edb 20 20 36 33 2c 20 20 20 36 34 2c 0a 20 2f 2a 20    63,   64,. /* 
25edc 20 20 36 31 30 20 2a 2f 20 20 20 20 36 34 2c 20    610 */    64, 
25edd 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 35    65,   65,   65
25ede 2c 20 20 20 36 36 2c 20 20 32 31 30 2c 20 20 32  ,   66,  210,  2
25edf 39 32 2c 20 20 34 32 38 2c 20 20 35 38 33 2c 20  92,  428,  583, 
25ee0 20 33 37 34 2c 0a 20 2f 2a 20 20 20 36 32 30 20   374,. /*   620 
25ee1 2a 2f 20 20 20 32 32 34 2c 20 20 20 39 33 2c 20  */   224,   93, 
25ee2 20 35 31 37 2c 20 20 20 20 39 2c 20 20 31 35 39   517,    9,  159
25ee3 2c 20 20 33 39 36 2c 20 20 35 35 37 2c 20 20 33  ,  396,  557,  3
25ee4 39 36 2c 20 20 34 35 36 2c 20 20 20 36 37 2c 0a  96,  456,   67,.
25ee5 20 2f 2a 20 20 20 36 33 30 20 2a 2f 20 20 20 33   /*   630 */   3
25ee6 39 36 2c 20 20 20 36 39 2c 20 20 31 35 34 2c 20  96,   69,  154, 
25ee7 20 33 39 39 2c 20 20 34 30 30 2c 20 20 34 30 31   399,  400,  401
25ee8 2c 20 20 33 32 30 2c 20 20 33 32 38 2c 20 20 34  ,  320,  328,  4
25ee9 33 38 2c 20 20 34 33 38 2c 0a 20 2f 2a 20 20 20  38,  438,. /*   
25eea 36 34 30 20 2a 2f 20 20 20 34 35 37 2c 20 20 33  640 */   457,  3
25eeb 33 36 2c 20 20 34 32 35 2c 20 20 34 33 31 2c 20  36,  425,  431, 
25eec 20 33 36 31 2c 20 20 33 39 37 2c 20 20 33 39 38   361,  397,  398
25eed 2c 20 20 33 32 30 2c 20 20 34 33 33 2c 20 20 34  ,  320,  433,  4
25eee 33 38 2c 0a 20 2f 2a 20 20 20 36 35 30 20 2a 2f  38,. /*   650 */
25eef 20 20 20 34 33 38 2c 20 20 35 38 32 2c 20 20 32     438,  582,  2
25ef0 39 31 2c 20 20 34 35 38 2c 20 20 32 33 38 2c 20  91,  458,  238, 
25ef1 20 33 32 37 2c 20 20 33 31 38 2c 20 20 32 32 32   327,  318,  222
25ef2 2c 20 20 35 34 36 2c 20 20 32 39 32 2c 0a 20 2f  ,  546,  292,. /
25ef3 2a 20 20 20 36 36 30 20 2a 2f 20 20 20 20 36 30  *   660 */    60
25ef4 2c 20 20 20 35 39 2c 20 20 32 39 37 2c 20 20 34  ,   59,  297,  4
25ef5 33 35 2c 20 20 34 33 36 2c 20 20 34 33 32 2c 20  35,  436,  432, 
25ef6 20 34 33 32 2c 20 20 20 36 32 2c 20 20 20 36 32   432,   62,   62
25ef7 2c 20 20 20 36 31 2c 0a 20 2f 2a 20 20 20 36 37  ,   61,. /*   67
25ef8 30 20 2a 2f 20 20 20 20 36 31 2c 20 20 20 36 31  0 */    61,   61
25ef9 2c 20 20 20 36 31 2c 20 20 32 32 35 2c 20 20 20  ,   61,  225,   
25efa 36 33 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20  63,   63,   63, 
25efb 20 20 36 33 2c 20 20 20 36 34 2c 20 20 20 36 34    63,   64,   64
25efc 2c 0a 20 2f 2a 20 20 20 36 38 30 20 2a 2f 20 20  ,. /*   680 */  
25efd 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 35    65,   65,   65
25efe 2c 20 20 20 36 36 2c 20 20 32 31 30 2c 20 20 34  ,   66,  210,  4
25eff 32 35 2c 20 20 34 33 31 2c 20 20 34 38 32 2c 20  25,  431,  482, 
25f00 20 33 31 33 2c 20 20 33 39 32 2c 0a 20 2f 2a 20   313,  392,. /* 
25f01 20 20 36 39 30 20 2a 2f 20 20 20 33 39 37 2c 20    690 */   397, 
25f02 20 33 39 38 2c 20 20 33 39 37 2c 20 20 33 39 38   398,  397,  398
25f03 2c 20 20 32 30 37 2c 20 20 33 39 37 2c 20 20 33  ,  207,  397,  3
25f04 39 38 2c 20 20 38 32 35 2c 20 20 32 37 33 2c 20  98,  825,  273, 
25f05 20 35 31 37 2c 0a 20 2f 2a 20 20 20 37 30 30 20   517,. /*   700 
25f06 2a 2f 20 20 20 32 35 31 2c 20 20 32 30 30 2c 20  */   251,  200, 
25f07 20 32 39 32 2c 20 20 20 36 30 2c 20 20 20 35 39   292,   60,   59
25f08 2c 20 20 32 39 37 2c 20 20 34 33 35 2c 20 20 34  ,  297,  435,  4
25f09 33 36 2c 20 20 34 33 32 2c 20 20 34 33 32 2c 0a  36,  432,  432,.
25f0a 20 2f 2a 20 20 20 37 31 30 20 2a 2f 20 20 20 20   /*   710 */    
25f0b 36 32 2c 20 20 20 36 32 2c 20 20 20 36 31 2c 20  62,   62,   61, 
25f0c 20 20 36 31 2c 20 20 20 36 31 2c 20 20 20 36 31    61,   61,   61
25f0d 2c 20 20 34 37 30 2c 20 20 20 36 33 2c 20 20 20  ,  470,   63,   
25f0e 36 33 2c 20 20 20 36 33 2c 0a 20 2f 2a 20 20 20  63,   63,. /*   
25f0f 37 32 30 20 2a 2f 20 20 20 20 36 33 2c 20 20 20  720 */    63,   
25f10 36 34 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20  64,   64,   65, 
25f11 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 36    65,   65,   66
25f12 2c 20 20 32 31 30 2c 20 20 34 32 35 2c 20 20 34  ,  210,  425,  4
25f13 33 31 2c 0a 20 2f 2a 20 20 20 37 33 30 20 2a 2f  31,. /*   730 */
25f14 20 20 20 31 37 31 2c 20 20 31 36 30 2c 20 20 32     171,  160,  2
25f15 36 33 2c 20 20 32 36 33 2c 20 20 33 30 34 2c 20  63,  263,  304, 
25f16 20 34 31 35 2c 20 20 32 37 36 2c 20 20 33 39 35   415,  276,  395
25f17 2c 20 20 32 37 34 2c 20 20 32 36 33 2c 0a 20 2f  ,  274,  263,. /
25f18 2a 20 20 20 37 34 30 20 2a 2f 20 20 20 35 31 37  *   740 */   517
25f19 2c 20 20 35 31 37 2c 20 20 32 36 33 2c 20 20 35  ,  517,  263,  5
25f1a 31 37 2c 20 20 31 39 32 2c 20 20 32 39 32 2c 20  17,  192,  292, 
25f1b 20 20 36 30 2c 20 20 20 37 30 2c 20 20 32 39 37    60,   70,  297
25f1c 2c 20 20 34 33 35 2c 0a 20 2f 2a 20 20 20 37 35  ,  435,. /*   75
25f1d 30 20 2a 2f 20 20 20 34 33 36 2c 20 20 34 33 32  0 */   436,  432
25f1e 2c 20 20 34 33 32 2c 20 20 20 36 32 2c 20 20 20  ,  432,   62,   
25f1f 36 32 2c 20 20 20 36 31 2c 20 20 20 36 31 2c 20  62,   61,   61, 
25f20 20 20 36 31 2c 20 20 20 36 31 2c 20 20 33 37 39    61,   61,  379
25f21 2c 0a 20 2f 2a 20 20 20 37 36 30 20 2a 2f 20 20  ,. /*   760 */  
25f22 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36 33    63,   63,   63
25f23 2c 20 20 20 36 33 2c 20 20 20 36 34 2c 20 20 20  ,   63,   64,   
25f24 36 34 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 20  64,   65,   65, 
25f25 20 20 36 35 2c 20 20 20 36 36 2c 0a 20 2f 2a 20    65,   66,. /* 
25f26 20 20 37 37 30 20 2a 2f 20 20 20 32 31 30 2c 20    770 */   210, 
25f27 20 34 32 35 2c 20 20 34 33 31 2c 20 20 33 38 34   425,  431,  384
25f28 2c 20 20 35 35 39 2c 20 20 33 30 35 2c 20 20 33  ,  559,  305,  3
25f29 30 36 2c 20 20 32 35 31 2c 20 20 34 31 35 2c 20  06,  251,  415, 
25f2a 20 33 32 30 2c 0a 20 2f 2a 20 20 20 37 38 30 20   320,. /*   780 
25f2b 2a 2f 20 20 20 35 36 30 2c 20 20 34 33 38 2c 20  */   560,  438, 
25f2c 20 34 33 38 2c 20 20 35 36 31 2c 20 20 35 34 30   438,  561,  540
25f2d 2c 20 20 33 36 30 2c 20 20 35 34 30 2c 20 20 33  ,  360,  540,  3
25f2e 38 37 2c 20 20 32 39 32 2c 20 20 31 39 36 2c 0a  87,  292,  196,.
25f2f 20 2f 2a 20 20 20 37 39 30 20 2a 2f 20 20 20 20   /*   790 */    
25f30 35 39 2c 20 20 32 39 37 2c 20 20 34 33 35 2c 20  59,  297,  435, 
25f31 20 34 33 36 2c 20 20 34 33 32 2c 20 20 34 33 32   436,  432,  432
25f32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20 20 20  ,   62,   62,   
25f33 36 31 2c 20 20 20 36 31 2c 0a 20 2f 2a 20 20 20  61,   61,. /*   
25f34 38 30 30 20 2a 2f 20 20 20 20 36 31 2c 20 20 20  800 */    61,   
25f35 36 31 2c 20 20 33 37 31 2c 20 20 20 36 33 2c 20  61,  371,   63, 
25f36 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20 36 33    63,   63,   63
25f37 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20  ,   64,   64,   
25f38 36 35 2c 0a 20 2f 2a 20 20 20 38 31 30 20 2a 2f  65,. /*   810 */
25f39 20 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20      65,   65,   
25f3a 36 36 2c 20 20 32 31 30 2c 20 20 34 32 35 2c 20  66,  210,  425, 
25f3b 20 34 33 31 2c 20 20 33 39 36 2c 20 20 32 37 35   431,  396,  275
25f3c 2c 20 20 32 35 31 2c 20 20 32 35 31 2c 0a 20 2f  ,  251,  251,. /
25f3d 2a 20 20 20 38 32 30 20 2a 2f 20 20 20 31 37 32  *   820 */   172
25f3e 2c 20 20 32 35 30 2c 20 20 34 31 38 2c 20 20 34  ,  250,  418,  4
25f3f 31 35 2c 20 20 33 38 36 2c 20 20 33 36 37 2c 20  15,  386,  367, 
25f40 20 31 37 38 2c 20 20 31 37 39 2c 20 20 31 38 30   178,  179,  180
25f41 2c 20 20 34 36 39 2c 0a 20 2f 2a 20 20 20 38 33  ,  469,. /*   83
25f42 30 20 2a 2f 20 20 20 33 31 31 2c 20 20 31 32 33  0 */   311,  123
25f43 2c 20 20 31 35 36 2c 20 20 20 20 35 2c 20 20 32  ,  156,    5,  2
25f44 39 37 2c 20 20 34 33 35 2c 20 20 34 33 36 2c 20  97,  435,  436, 
25f45 20 34 33 32 2c 20 20 34 33 32 2c 20 20 20 36 32   432,  432,   62
25f46 2c 0a 20 2f 2a 20 20 20 38 34 30 20 2a 2f 20 20  ,. /*   840 */  
25f47 20 20 36 32 2c 20 20 20 36 31 2c 20 20 20 36 31    62,   61,   61
25f48 2c 20 20 20 36 31 2c 20 20 20 36 31 2c 20 20 33  ,   61,   61,  3
25f49 31 37 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20  17,   63,   63, 
25f4a 20 20 36 33 2c 20 20 20 36 33 2c 0a 20 2f 2a 20    63,   63,. /* 
25f4b 20 20 38 35 30 20 2a 2f 20 20 20 20 36 34 2c 20    850 */    64, 
25f4c 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20 36 35    64,   65,   65
25f4d 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 32  ,   65,   66,  2
25f4e 31 30 2c 20 20 20 37 32 2c 20 20 33 32 34 2c 20  10,   72,  324, 
25f4f 20 31 39 34 2c 0a 20 2f 2a 20 20 20 38 36 30 20   194,. /*   860 
25f50 2a 2f 20 20 20 20 20 34 2c 20 20 33 31 37 2c 20  */     4,  317, 
25f51 20 32 36 33 2c 20 20 33 31 37 2c 20 20 32 39 36   263,  317,  296
25f52 2c 20 20 32 36 33 2c 20 20 34 31 35 2c 20 20 34  ,  263,  415,  4
25f53 31 34 2c 20 20 20 32 38 2c 20 20 33 31 37 2c 0a  14,   28,  317,.
25f54 20 2f 2a 20 20 20 38 37 30 20 2a 2f 20 20 20 32   /*   870 */   2
25f55 35 37 2c 20 20 33 31 37 2c 20 20 33 32 31 2c 20  57,  317,  321, 
25f56 20 20 37 32 2c 20 20 33 32 34 2c 20 20 33 31 37    72,  324,  317
25f57 2c 20 20 20 20 34 2c 20 20 31 31 39 2c 20 20 31  ,    4,  119,  1
25f58 36 35 2c 20 20 31 37 37 2c 0a 20 2f 2a 20 20 20  65,  177,. /*   
25f59 38 38 30 20 2a 2f 20 20 20 32 39 36 2c 20 20 33  880 */   296,  3
25f5a 39 37 2c 20 20 33 39 38 2c 20 20 34 31 34 2c 20  97,  398,  414, 
25f5b 20 20 32 33 2c 20 20 34 31 34 2c 20 20 20 33 32    23,  414,   32
25f5c 2c 20 20 34 31 38 2c 20 20 33 32 31 2c 20 20 33  ,  418,  321,  3
25f5d 32 36 2c 0a 20 2f 2a 20 20 20 38 39 30 20 2a 2f  26,. /*   890 */
25f5e 20 20 20 34 32 31 2c 20 20 34 31 34 2c 20 20 20     421,  414,   
25f5f 35 33 2c 20 20 34 31 34 2c 20 20 20 35 32 2c 20  53,  414,   52, 
25f60 20 33 31 37 2c 20 20 31 35 38 2c 20 20 34 31 34   317,  158,  414
25f61 2c 20 20 20 39 38 2c 20 20 34 35 31 2c 0a 20 2f  ,   98,  451,. /
25f62 2a 20 20 20 39 30 30 20 2a 2f 20 20 20 33 31 37  *   900 */   317
25f63 2c 20 20 32 36 33 2c 20 20 33 31 37 2c 20 20 32  ,  263,  317,  2
25f64 37 37 2c 20 20 33 31 37 2c 20 20 33 32 36 2c 20  77,  317,  326, 
25f65 20 33 37 38 2c 20 20 34 37 31 2c 20 20 32 36 31   378,  471,  261
25f66 2c 20 20 33 31 37 2c 0a 20 2f 2a 20 20 20 39 31  ,  317,. /*   91
25f67 30 20 2a 2f 20 20 20 32 35 39 2c 20 20 20 31 38  0 */   259,   18
25f68 2c 20 20 34 37 38 2c 20 20 34 34 35 2c 20 20 34  ,  478,  445,  4
25f69 34 35 2c 20 20 34 35 31 2c 20 20 33 31 37 2c 20  45,  451,  317, 
25f6a 20 34 31 34 2c 20 20 20 39 36 2c 20 20 20 37 35   414,   96,   75
25f6b 2c 0a 20 2f 2a 20 20 20 39 32 30 20 2a 2f 20 20  ,. /*   920 */  
25f6c 20 20 37 34 2c 20 20 34 36 39 2c 20 20 34 31 34    74,  469,  414
25f6d 2c 20 20 31 30 31 2c 20 20 34 31 34 2c 20 20 31  ,  101,  414,  1
25f6e 30 32 2c 20 20 34 31 34 2c 20 20 31 31 32 2c 20  02,  414,  112, 
25f6f 20 20 37 33 2c 20 20 33 31 35 2c 0a 20 2f 2a 20    73,  315,. /* 
25f70 20 20 39 33 30 20 2a 2f 20 20 20 33 31 36 2c 20    930 */   316, 
25f71 20 34 31 34 2c 20 20 31 31 34 2c 20 20 34 32 30   414,  114,  420
25f72 2c 20 20 32 39 34 2c 20 20 20 37 35 2c 20 20 20  ,  294,   75,   
25f73 37 34 2c 20 20 34 38 31 2c 20 20 34 31 34 2c 20  74,  481,  414, 
25f74 20 20 31 36 2c 0a 20 2f 2a 20 20 20 39 34 30 20    16,. /*   940 
25f75 2a 2f 20 20 20 33 38 31 2c 20 20 33 31 37 2c 20  */   381,  317, 
25f76 20 32 37 39 2c 20 20 34 36 37 2c 20 20 20 37 33   279,  467,   73
25f77 2c 20 20 33 31 35 2c 20 20 33 31 36 2c 20 20 20  ,  315,  316,   
25f78 37 32 2c 20 20 33 32 34 2c 20 20 34 32 30 2c 0a  72,  324,  420,.
25f79 20 2f 2a 20 20 20 39 35 30 20 2a 2f 20 20 20 20   /*   950 */    
25f7a 20 34 2c 20 20 32 30 38 2c 20 20 33 31 37 2c 20   4,  208,  317, 
25f7b 20 31 38 33 2c 20 20 32 39 36 2c 20 20 33 31 37   183,  296,  317
25f7c 2c 20 20 31 38 36 2c 20 20 31 32 38 2c 20 20 20  ,  186,  128,   
25f7d 38 34 2c 20 20 32 30 38 2c 0a 20 2f 2a 20 20 20  84,  208,. /*   
25f7e 39 36 30 20 2a 2f 20 20 20 20 20 38 2c 20 20 33  960 */     8,  3
25f7f 34 31 2c 20 20 33 32 31 2c 20 20 34 31 34 2c 20  41,  321,  414, 
25f80 20 20 39 39 2c 20 20 34 32 32 2c 20 20 34 32 32    99,  422,  422
25f81 2c 20 20 34 32 32 2c 20 20 34 32 33 2c 20 20 34  ,  422,  423,  4
25f82 32 34 2c 0a 20 2f 2a 20 20 20 39 37 30 20 2a 2f  24,. /*   970 */
25f83 20 20 20 20 31 31 2c 20 20 36 32 33 2c 20 20 33      11,  623,  3
25f84 38 30 2c 20 20 33 30 37 2c 20 20 34 31 34 2c 20  80,  307,  414, 
25f85 20 20 33 33 2c 20 20 34 31 33 2c 20 20 34 31 34    33,  413,  414
25f86 2c 20 20 20 39 37 2c 20 20 33 32 36 2c 0a 20 2f  ,   97,  326,. /
25f87 2a 20 20 20 39 38 30 20 2a 2f 20 20 20 34 31 32  *   980 */   412
25f88 2c 20 20 34 32 32 2c 20 20 34 32 32 2c 20 20 34  ,  422,  422,  4
25f89 32 32 2c 20 20 34 32 33 2c 20 20 34 32 34 2c 20  22,  423,  424, 
25f8a 20 20 31 31 2c 20 20 34 31 35 2c 20 20 34 31 33    11,  415,  413
25f8b 2c 20 20 34 35 31 2c 0a 20 2f 2a 20 20 20 39 39  ,  451,. /*   99
25f8c 30 20 2a 2f 20 20 20 34 31 35 2c 20 20 31 36 32  0 */   415,  162
25f8d 2c 20 20 34 31 32 2c 20 20 33 31 37 2c 20 20 34  ,  412,  317,  4
25f8e 39 39 2c 20 20 35 30 30 2c 20 20 32 32 36 2c 20  99,  500,  226, 
25f8f 20 32 32 37 2c 20 20 32 32 38 2c 20 20 31 30 34   227,  228,  104
25f90 2c 0a 20 2f 2a 20 20 31 30 30 30 20 2a 2f 20 20  ,. /*  1000 */  
25f91 20 34 34 38 2c 20 20 34 37 36 2c 20 20 33 31 37   448,  476,  317
25f92 2c 20 20 31 37 33 2c 20 20 35 30 37 2c 20 20 33  ,  173,  507,  3
25f93 31 37 2c 20 20 35 30 39 2c 20 20 35 30 38 2c 20  17,  509,  508, 
25f94 20 33 31 37 2c 20 20 20 37 35 2c 0a 20 2f 2a 20   317,   75,. /* 
25f95 20 31 30 31 30 20 2a 2f 20 20 20 20 37 34 2c 20   1010 */    74, 
25f96 20 33 32 39 2c 20 20 32 30 35 2c 20 20 20 32 31   329,  205,   21
25f97 2c 20 20 32 38 31 2c 20 20 34 31 34 2c 20 20 20  ,  281,  414,   
25f98 32 34 2c 20 20 34 31 38 2c 20 20 20 37 33 2c 20  24,  418,   73, 
25f99 20 33 31 35 2c 0a 20 2f 2a 20 20 31 30 32 30 20   315,. /*  1020 
25f9a 2a 2f 20 20 20 33 31 36 2c 20 20 32 38 32 2c 20  */   316,  282, 
25f9b 20 33 31 37 2c 20 20 34 32 30 2c 20 20 34 31 34   317,  420,  414
25f9c 2c 20 20 20 35 34 2c 20 20 34 36 30 2c 20 20 34  ,   54,  460,  4
25f9d 31 34 2c 20 20 31 31 35 2c 20 20 33 31 37 2c 0a  14,  115,  317,.
25f9e 20 2f 2a 20 20 31 30 33 30 20 2a 2f 20 20 20 34   /*  1030 */   4
25f9f 31 34 2c 20 20 31 31 36 2c 20 20 35 30 32 2c 20  14,  116,  502, 
25fa0 20 32 30 33 2c 20 20 31 34 37 2c 20 20 35 34 39   203,  147,  549
25fa1 2c 20 20 35 31 34 2c 20 20 34 36 38 2c 20 20 31  ,  514,  468,  1
25fa2 32 38 2c 20 20 32 30 32 2c 0a 20 2f 2a 20 20 31  28,  202,. /*  1
25fa3 30 34 30 20 2a 2f 20 20 20 33 31 37 2c 20 20 34  040 */   317,  4
25fa4 37 33 2c 20 20 32 30 34 2c 20 20 33 31 37 2c 20  73,  204,  317, 
25fa5 20 34 31 34 2c 20 20 31 31 37 2c 20 20 33 31 37   414,  117,  317
25fa6 2c 20 20 34 37 37 2c 20 20 33 31 37 2c 20 20 35  ,  477,  317,  5
25fa7 38 34 2c 0a 20 2f 2a 20 20 31 30 35 30 20 2a 2f  84,. /*  1050 */
25fa8 20 20 20 33 31 37 2c 20 20 34 31 34 2c 20 20 20     317,  414,   
25fa9 32 35 2c 20 20 33 31 37 2c 20 20 32 34 39 2c 20  25,  317,  249, 
25faa 20 34 32 32 2c 20 20 34 32 32 2c 20 20 34 32 32   422,  422,  422
25fab 2c 20 20 34 32 33 2c 20 20 34 32 34 2c 0a 20 2f  ,  423,  424,. /
25fac 2a 20 20 31 30 36 30 20 2a 2f 20 20 20 20 31 31  *  1060 */    11
25fad 2c 20 20 35 30 36 2c 20 20 34 31 34 2c 20 20 20  ,  506,  414,   
25fae 33 36 2c 20 20 35 31 32 2c 20 20 34 31 34 2c 20  36,  512,  414, 
25faf 20 20 33 37 2c 20 20 33 31 37 2c 20 20 34 31 34    37,  317,  414
25fb0 2c 20 20 20 32 36 2c 0a 20 2f 2a 20 20 31 30 37  ,   26,. /*  107
25fb1 30 20 2a 2f 20 20 20 34 31 34 2c 20 20 20 33 38  0 */   414,   38
25fb2 2c 20 20 34 31 34 2c 20 20 20 33 39 2c 20 20 35  ,  414,   39,  5
25fb3 32 36 2c 20 20 34 31 34 2c 20 20 20 34 30 2c 20  26,  414,   40, 
25fb4 20 33 31 37 2c 20 20 32 35 34 2c 20 20 33 31 37   317,  254,  317
25fb5 2c 0a 20 2f 2a 20 20 31 30 38 30 20 2a 2f 20 20  ,. /*  1080 */  
25fb6 20 31 32 38 2c 20 20 33 31 37 2c 20 20 34 31 38   128,  317,  418
25fb7 2c 20 20 33 31 37 2c 20 20 32 35 36 2c 20 20 33  ,  317,  256,  3
25fb8 37 37 2c 20 20 32 37 38 2c 20 20 32 36 38 2c 20  77,  278,  268, 
25fb9 20 35 38 35 2c 20 20 34 31 34 2c 0a 20 2f 2a 20   585,  414,. /* 
25fba 20 31 30 39 30 20 2a 2f 20 20 20 20 34 32 2c 20   1090 */    42, 
25fbb 20 32 39 33 2c 20 20 33 31 37 2c 20 20 33 35 32   293,  317,  352
25fbc 2c 20 20 33 31 37 2c 20 20 31 32 38 2c 20 20 32  ,  317,  128,  2
25fbd 30 38 2c 20 20 35 31 33 2c 20 20 32 35 38 2c 20  08,  513,  258, 
25fbe 20 34 31 34 2c 0a 20 2f 2a 20 20 31 31 30 30 20   414,. /*  1100 
25fbf 2a 2f 20 20 20 20 34 33 2c 20 20 34 31 34 2c 20  */    43,  414, 
25fc0 20 20 34 34 2c 20 20 34 31 34 2c 20 20 20 32 39    44,  414,   29
25fc1 2c 20 20 34 31 34 2c 20 20 20 33 30 2c 20 20 35  ,  414,   30,  5
25fc2 34 35 2c 20 20 32 36 30 2c 20 20 31 32 38 2c 0a  45,  260,  128,.
25fc3 20 2f 2a 20 20 31 31 31 30 20 2a 2f 20 20 20 33   /*  1110 */   3
25fc4 31 37 2c 20 20 35 35 33 2c 20 20 33 31 37 2c 20  17,  553,  317, 
25fc5 20 31 37 33 2c 20 20 34 31 34 2c 20 20 20 34 35   173,  414,   45
25fc6 2c 20 20 34 31 34 2c 20 20 20 34 36 2c 20 20 33  ,  414,   46,  3
25fc7 31 37 2c 20 20 32 36 32 2c 0a 20 2f 2a 20 20 31  17,  262,. /*  1
25fc8 31 32 30 20 2a 2f 20 20 20 33 38 33 2c 20 20 35  120 */   383,  5
25fc9 35 34 2c 20 20 33 31 37 2c 20 20 20 39 31 2c 20  54,  317,   91, 
25fca 20 35 36 34 2c 20 20 33 31 37 2c 20 20 20 39 31   564,  317,   91
25fcb 2c 20 20 33 31 37 2c 20 20 35 38 31 2c 20 20 31  ,  317,  581,  1
25fcc 38 39 2c 0a 20 2f 2a 20 20 31 31 33 30 20 2a 2f  89,. /*  1130 */
25fcd 20 20 20 32 39 30 2c 20 20 33 35 37 2c 20 20 34     290,  357,  4
25fce 31 34 2c 20 20 20 34 37 2c 20 20 34 31 34 2c 20  14,   47,  414, 
25fcf 20 20 34 38 2c 20 20 32 36 37 2c 20 20 33 36 35    48,  267,  365
25fd0 2c 20 20 33 36 38 2c 20 20 33 36 39 2c 0a 20 2f  ,  368,  369,. /
25fd1 2a 20 20 31 31 34 30 20 2a 2f 20 20 20 34 31 34  *  1140 */   414
25fd2 2c 20 20 20 33 31 2c 20 20 32 37 30 2c 20 20 32  ,   31,  270,  2
25fd3 37 31 2c 20 20 34 31 34 2c 20 20 20 31 30 2c 20  71,  414,   10, 
25fd4 20 32 37 32 2c 20 20 34 31 34 2c 20 20 20 35 30   272,  414,   50
25fd5 2c 20 20 34 31 34 2c 0a 20 2f 2a 20 20 31 31 35  ,  414,. /*  115
25fd6 30 20 2a 2f 20 20 20 20 35 31 2c 20 20 35 35 36  0 */    51,  556
25fd7 2c 20 20 35 36 36 2c 20 20 32 38 30 2c 20 20 32  ,  566,  280,  2
25fd8 38 33 2c 20 20 32 38 34 2c 20 20 35 37 38 2c 20  83,  284,  578, 
25fd9 20 31 34 36 2c 20 20 34 31 39 2c 20 20 34 30 35   146,  419,  405
25fda 2c 0a 20 2f 2a 20 20 31 31 36 30 20 2a 2f 20 20  ,. /*  1160 */  
25fdb 20 32 33 31 2c 20 20 35 30 35 2c 20 20 34 34 34   231,  505,  444
25fdc 2c 20 20 33 32 35 2c 20 20 35 31 36 2c 20 20 34  ,  325,  516,  4
25fdd 36 33 2c 20 20 31 36 33 2c 20 20 34 34 36 2c 20  63,  163,  446, 
25fde 20 35 35 32 2c 20 20 33 39 34 2c 0a 20 2f 2a 20   552,  394,. /* 
25fdf 20 31 31 37 30 20 2a 2f 20 20 20 34 36 36 2c 20   1170 */   466, 
25fe0 20 35 36 33 2c 20 20 32 34 36 2c 20 20 35 31 35   563,  246,  515
25fe1 2c 20 20 35 31 38 2c 20 20 35 32 30 2c 20 20 34  ,  518,  520,  4
25fe2 30 32 2c 20 20 34 30 33 2c 20 20 34 30 34 2c 20  02,  403,  404, 
25fe3 20 20 20 37 2c 0a 20 2f 2a 20 20 31 31 38 30 20     7,. /*  1180 
25fe4 2a 2f 20 20 20 33 31 34 2c 20 20 20 38 34 2c 20  */   314,   84, 
25fe5 20 32 33 32 2c 20 20 33 33 34 2c 20 20 33 34 37   232,  334,  347
25fe6 2c 20 20 20 38 33 2c 20 20 33 33 32 2c 20 20 20  ,   83,  332,   
25fe7 35 37 2c 20 20 31 37 30 2c 20 20 20 37 39 2c 0a  57,  170,   79,.
25fe8 20 2f 2a 20 20 31 31 39 30 20 2a 2f 20 20 20 32   /*  1190 */   2
25fe9 31 33 2c 20 20 34 36 31 2c 20 20 31 32 35 2c 20  13,  461,  125, 
25fea 20 20 38 35 2c 20 20 33 33 37 2c 20 20 33 34 32    85,  337,  342
25feb 2c 20 20 34 39 32 2c 20 20 35 30 32 2c 20 20 34  ,  492,  502,  4
25fec 39 37 2c 20 20 33 30 31 2c 0a 20 2f 2a 20 20 31  97,  301,. /*  1
25fed 32 30 30 20 2a 2f 20 20 20 34 39 38 2c 20 20 34  200 */   498,  4
25fee 31 36 2c 20 20 31 30 35 2c 20 20 32 31 39 2c 20  16,  105,  219, 
25fef 20 32 34 37 2c 20 20 32 31 38 2c 20 20 35 30 33   247,  218,  503
25ff0 2c 20 20 35 30 31 2c 20 20 32 33 33 2c 20 20 32  ,  501,  233,  2
25ff1 32 30 2c 0a 20 2f 2a 20 20 31 32 31 30 20 2a 2f  20,. /*  1210 */
25ff2 20 20 20 32 38 37 2c 20 20 32 33 34 2c 20 20 35     287,  234,  5
25ff3 32 37 2c 20 20 35 32 38 2c 20 20 32 33 35 2c 20  27,  528,  235, 
25ff4 20 35 32 39 2c 20 20 34 31 37 2c 20 20 35 32 31   529,  417,  521
25ff5 2c 20 20 33 35 34 2c 20 20 32 38 38 2c 0a 20 2f  ,  354,  288,. /
25ff6 2a 20 20 31 32 32 30 20 2a 2f 20 20 20 31 38 34  *  1220 */   184
25ff7 2c 20 20 31 32 31 2c 20 20 31 38 35 2c 20 20 32  ,  121,  185,  2
25ff8 34 30 2c 20 20 35 33 35 2c 20 20 34 37 35 2c 20  40,  535,  475, 
25ff9 20 32 34 32 2c 20 20 33 35 36 2c 20 20 31 38 37   242,  356,  187
25ffa 2c 20 20 34 37 39 2c 0a 20 2f 2a 20 20 31 32 33  ,  479,. /*  123
25ffb 30 20 2a 2f 20 20 20 31 38 38 2c 20 20 33 35 38  0 */   188,  358
25ffc 2c 20 20 35 33 37 2c 20 20 20 38 38 2c 20 20 31  ,  537,   88,  1
25ffd 39 30 2c 20 20 35 34 38 2c 20 20 33 36 34 2c 20  90,  548,  364, 
25ffe 20 31 39 33 2c 20 20 31 33 32 2c 20 20 33 37 36   193,  132,  376
25fff 2c 0a 20 2f 2a 20 20 31 32 34 30 20 2a 2f 20 20  ,. /*  1240 */  
26000 20 35 35 35 2c 20 20 33 37 35 2c 20 20 31 33 33   555,  375,  133
26001 2c 20 20 31 33 34 2c 20 20 31 33 35 2c 20 20 33  ,  134,  135,  3
26002 31 30 2c 20 20 35 36 32 2c 20 20 31 33 38 2c 20  10,  562,  138, 
26003 20 31 33 36 2c 20 20 35 37 35 2c 0a 20 2f 2a 20   136,  575,. /* 
26004 20 31 32 35 30 20 2a 2f 20 20 20 35 37 36 2c 20   1250 */   576, 
26005 20 35 37 37 2c 20 20 35 38 30 2c 20 20 31 30 30   577,  580,  100
26006 2c 20 20 33 39 33 2c 20 20 34 30 36 2c 20 20 32  ,  393,  406,  2
26007 31 37 2c 20 20 31 34 32 2c 20 20 36 32 34 2c 20  17,  142,  624, 
26008 20 36 32 35 2c 0a 20 2f 2a 20 20 31 32 36 30 20   625,. /*  1260 
26009 2a 2f 20 20 20 31 30 33 2c 20 20 31 34 31 2c 20  */   103,  141, 
2600a 20 32 36 35 2c 20 20 31 36 36 2c 20 20 31 36 37   265,  166,  167
2600b 2c 20 20 34 33 34 2c 20 20 20 37 31 2c 20 20 34  ,  434,   71,  4
2600c 35 33 2c 20 20 34 34 31 2c 20 20 34 33 37 2c 0a  53,  441,  437,.
2600d 20 2f 2a 20 20 31 32 37 30 20 2a 2f 20 20 20 34   /*  1270 */   4
2600e 35 30 2c 20 20 31 34 33 2c 20 20 35 33 38 2c 20  50,  143,  538, 
2600f 20 31 35 37 2c 20 20 31 32 30 2c 20 20 34 35 34   157,  120,  454
26010 2c 20 20 31 36 31 2c 20 20 34 37 32 2c 20 20 34  ,  161,  472,  4
26011 35 35 2c 20 20 31 36 39 2c 0a 20 2f 2a 20 20 31  55,  169,. /*  1
26012 32 38 30 20 2a 2f 20 20 20 34 35 39 2c 20 20 20  280 */   459,   
26013 38 31 2c 20 20 20 20 36 2c 20 20 20 31 32 2c 20  81,    6,   12, 
26014 20 20 31 33 2c 20 20 20 39 32 2c 20 20 20 39 35    13,   92,   95
26015 2c 20 20 31 32 36 2c 20 20 32 31 36 2c 20 20 31  ,  126,  216,  1
26016 32 37 2c 0a 20 2f 2a 20 20 31 32 39 30 20 2a 2f  27,. /*  1290 */
26017 20 20 20 31 31 31 2c 20 20 34 38 35 2c 20 20 34     111,  485,  4
26018 38 36 2c 20 20 20 31 37 2c 20 20 20 38 36 2c 20  86,   17,   86, 
26019 20 33 34 36 2c 20 20 31 30 36 2c 20 20 31 32 32   346,  106,  122
2601a 2c 20 20 32 35 33 2c 20 20 31 30 37 2c 0a 20 2f  ,  253,  107,. /
2601b 2a 20 20 31 33 30 30 20 2a 2f 20 20 20 20 38 37  *  1300 */    87
2601c 2c 20 20 31 30 38 2c 20 20 31 38 32 2c 20 20 32  ,  108,  182,  2
2601d 34 35 2c 20 20 33 35 35 2c 20 20 31 34 35 2c 20  45,  355,  145, 
2601e 20 33 35 31 2c 20 20 35 33 36 2c 20 20 31 32 39   351,  536,  129
2601f 2c 20 20 33 35 39 2c 0a 20 2f 2a 20 20 31 33 31  ,  359,. /*  131
26020 30 20 2a 2f 20 20 20 33 31 32 2c 20 20 31 33 30  0 */   312,  130
26021 2c 20 20 35 34 33 2c 20 20 31 37 33 2c 20 20 35  ,  543,  173,  5
26022 33 39 2c 20 20 32 36 36 2c 20 20 31 39 31 2c 20  39,  266,  191, 
26023 20 31 30 39 2c 20 20 32 38 39 2c 20 20 35 35 31   109,  289,  551
26024 2c 0a 20 2f 2a 20 20 31 33 32 30 20 2a 2f 20 20  ,. /*  1320 */  
26025 20 31 39 35 2c 20 20 20 31 34 2c 20 20 31 33 31   195,   14,  131
26026 2c 20 20 31 39 38 2c 20 20 31 39 37 2c 20 20 35  ,  198,  197,  5
26027 35 38 2c 20 20 31 33 37 2c 20 20 31 39 39 2c 20  58,  137,  199, 
26028 20 31 33 39 2c 20 20 31 34 30 2c 0a 20 2f 2a 20   139,  140,. /* 
26029 20 31 33 33 30 20 2a 2f 20 20 20 20 31 35 2c 20   1330 */    15, 
2602a 20 35 36 35 2c 20 20 20 38 39 2c 20 20 20 39 30   565,   89,   90
2602b 2c 20 20 35 37 33 2c 20 20 31 31 30 2c 20 20 33  ,  573,  110,  3
2602c 38 35 2c 20 20 32 30 36 2c 20 20 31 34 38 2c 20  85,  206,  148, 
2602d 20 33 38 39 2c 0a 20 2f 2a 20 20 31 33 34 30 20   389,. /*  1340 
2602e 2a 2f 20 20 20 32 38 35 2c 20 20 35 38 37 2c 0a  */   285,  587,.
2602f 7d 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  };.static const 
26030 59 59 43 4f 44 45 54 59 50 45 20 79 79 5f 6c 6f  YYCODETYPE yy_lo
26031 6f 6b 61 68 65 61 64 5b 5d 20 3d 20 7b 0a 20 2f  okahead[] = {. /
26032 2a 20 20 20 20 20 30 20 2a 2f 20 20 20 20 31 36  *     0 */    16
26033 2c 20 20 31 33 39 2c 20 20 31 34 30 2c 20 20 31  ,  139,  140,  1
26034 34 31 2c 20 20 31 36 38 2c 20 20 20 32 31 2c 20  41,  168,   21, 
26035 20 31 34 34 2c 20 20 20 32 33 2c 20 20 20 36 39   144,   23,   69
26036 2c 20 20 20 37 30 2c 0a 20 2f 2a 20 20 20 20 31  ,   70,. /*    1
26037 30 20 2a 2f 20 20 20 20 37 31 2c 20 20 20 37 32  0 */    71,   72
26038 2c 20 20 31 37 36 2c 20 20 20 37 34 2c 20 20 20  ,  176,   74,   
26039 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20  75,   76,   77, 
2603a 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30    78,   79,   80
2603b 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20  ,. /*    20 */  
2603c 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33    81,   82,   83
2603d 2c 20 20 20 38 34 2c 20 20 20 37 38 2c 20 20 20  ,   84,   78,   
2603e 37 39 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20  79,   42,   43, 
2603f 20 20 37 33 2c 20 20 20 37 34 2c 0a 20 2f 2a 20    73,   74,. /* 
26040 20 20 20 33 30 20 2a 2f 20 20 20 20 37 35 2c 20     30 */    75, 
26041 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38    76,   77,   78
26042 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 20  ,   79,   80,   
26043 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20  81,   82,   83, 
26044 20 20 38 34 2c 0a 20 2f 2a 20 20 20 20 34 30 20    84,. /*    40 
26045 2a 2f 20 20 20 20 20 31 2c 20 20 20 20 32 2c 20  */     1,    2, 
26046 20 20 32 33 2c 20 20 20 35 38 2c 20 20 20 36 30    23,   58,   60
26047 2c 20 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20  ,   61,   62,   
26048 36 33 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 0a  63,   64,   65,.
26049 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20 20 20   /*    50 */    
2604a 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38 2c 20  66,   67,   68, 
2604b 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31    69,   70,   71
2604c 2c 20 20 20 37 32 2c 20 20 31 34 37 2c 20 20 20  ,   72,  147,   
2604d 37 34 2c 20 20 20 37 35 2c 0a 20 2f 2a 20 20 20  74,   75,. /*   
2604e 20 36 30 20 2a 2f 20 20 20 20 37 36 2c 20 20 20   60 */    76,   
2604f 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20  77,   78,   79, 
26050 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32    80,   81,   82
26051 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20  ,   83,   84,   
26052 31 36 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f  16,. /*    70 */
26053 20 20 20 31 38 35 2c 20 20 31 38 36 2c 20 20 20     185,  186,   
26054 38 38 2c 20 20 20 38 38 2c 20 20 31 31 30 2c 20  88,   88,  110, 
26055 20 20 32 32 2c 20 20 32 31 37 2c 20 20 20 39 32    22,  217,   92
26056 2c 20 20 32 31 39 2c 20 20 32 32 30 2c 0a 20 2f  ,  219,  220,. /
26057 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 20 37 34  *    80 */    74
26058 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20  ,   75,   76,   
26059 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20  77,   78,   79, 
2605a 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32    80,   81,   82
2605b 2c 20 20 20 38 33 2c 0a 20 2f 2a 20 20 20 20 39  ,   83,. /*    9
2605c 30 20 2a 2f 20 20 20 20 38 34 2c 20 20 32 31 37  0 */    84,  217
2605d 2c 20 20 32 31 38 2c 20 20 32 31 39 2c 20 20 32  ,  218,  219,  2
2605e 32 30 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20  20,   42,   43, 
2605f 20 32 33 38 2c 20 20 31 38 38 2c 20 20 20 34 36   238,  188,   46
26060 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20  ,. /*   100 */  
26061 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30    78,   79,   80
26062 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20  ,   81,   82,   
26063 38 33 2c 20 20 20 38 34 2c 20 20 20 38 38 2c 20  83,   84,   88, 
26064 20 20 38 39 2c 20 20 31 32 34 2c 0a 20 2f 2a 20    89,  124,. /* 
26065 20 20 31 31 30 20 2a 2f 20 20 20 31 32 35 2c 20    110 */   125, 
26066 20 31 32 36 2c 20 20 20 31 36 2c 20 20 20 36 30   126,   16,   60
26067 2c 20 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20  ,   61,   62,   
26068 36 33 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20  63,   64,   65, 
26069 20 20 36 36 2c 0a 20 2f 2a 20 20 20 31 32 30 20    66,. /*   120 
2606a 2a 2f 20 20 20 20 36 37 2c 20 20 20 36 38 2c 20  */    67,   68, 
2606b 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31    69,   70,   71
2606c 2c 20 20 20 37 32 2c 20 20 31 34 37 2c 20 20 20  ,   72,  147,   
2606d 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 0a  74,   75,   76,.
2606e 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20 20 20   /*   130 */    
2606f 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20  77,   78,   79, 
26070 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32    80,   81,   82
26071 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20  ,   83,   84,   
26072 34 32 2c 20 20 20 34 33 2c 0a 20 2f 2a 20 20 20  42,   43,. /*   
26073 31 34 30 20 2a 2f 20 20 20 20 34 34 2c 20 20 20  140 */    44,   
26074 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20  80,   81,   82, 
26075 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 32 33    83,   84,   23
26076 2c 20 20 20 38 34 2c 20 20 31 36 39 2c 20 20 31  ,   84,  169,  1
26077 37 30 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f  70,. /*   150 */
26078 20 20 20 20 31 39 2c 20 20 31 36 34 2c 20 20 31      19,  164,  1
26079 36 35 2c 20 20 31 36 36 2c 20 20 20 32 33 2c 20  65,  166,   23, 
2607a 20 20 32 33 2c 20 20 20 36 30 2c 20 20 20 36 31    23,   60,   61
2607b 2c 20 20 20 36 32 2c 20 20 20 36 33 2c 0a 20 2f  ,   62,   63,. /
2607c 2a 20 20 20 31 36 30 20 2a 2f 20 20 20 20 36 34  *   160 */    64
2607d 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 20  ,   65,   66,   
2607e 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20  67,   68,   69, 
2607f 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32    70,   71,   72
26080 2c 20 20 31 31 30 2c 0a 20 2f 2a 20 20 20 31 37  ,  110,. /*   17
26081 30 20 2a 2f 20 20 20 20 37 34 2c 20 20 20 37 35  0 */    74,   75
26082 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20  ,   76,   77,   
26083 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20  78,   79,   80, 
26084 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33    81,   82,   83
26085 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20  ,. /*   180 */  
26086 20 20 38 34 2c 20 20 20 31 36 2c 20 20 31 32 33    84,   16,  123
26087 2c 20 20 31 34 37 2c 20 20 31 35 30 2c 20 20 31  ,  147,  150,  1
26088 34 37 2c 20 20 20 32 31 2c 20 20 31 36 37 2c 20  47,   21,  167, 
26089 20 31 36 38 2c 20 20 20 35 38 2c 0a 20 2f 2a 20   168,   58,. /* 
2608a 20 20 31 39 30 20 2a 2f 20 20 20 32 31 31 2c 20    190 */   211, 
2608b 20 31 34 37 2c 20 20 31 35 36 2c 20 20 31 35 37   147,  156,  157
2608c 2c 20 20 20 39 32 2c 20 20 32 31 36 2c 20 20 31  ,   92,  216,  1
2608d 37 36 2c 20 20 20 32 33 2c 20 20 31 34 37 2c 20  76,   23,  147, 
2608e 20 31 37 36 2c 0a 20 2f 2a 20 20 20 32 30 30 20   176,. /*   200 
2608f 2a 2f 20 20 20 31 37 37 2c 20 20 20 37 38 2c 20  */   177,   78, 
26090 20 20 37 39 2c 20 20 31 36 35 2c 20 20 31 36 36    79,  165,  166
26091 2c 20 20 31 30 33 2c 20 20 31 38 33 2c 20 20 20  ,  103,  183,   
26092 34 32 2c 20 20 20 34 33 2c 20 20 20 37 38 2c 0a  42,   43,   78,.
26093 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20 20 20   /*   210 */    
26094 37 39 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20  79,   88,   89, 
26095 20 31 36 39 2c 20 20 31 37 30 2c 20 20 32 32 38   169,  170,  228
26096 2c 20 20 31 38 30 2c 20 20 31 38 31 2c 20 20 31  ,  180,  181,  1
26097 36 39 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20 20  69,   88,. /*   
26098 32 32 30 20 2a 2f 20 20 20 20 38 38 2c 20 20 20  220 */    88,   
26099 39 38 2c 20 20 20 39 39 2c 20 20 20 39 32 2c 20  98,   99,   92, 
2609a 20 20 31 36 2c 20 20 20 36 30 2c 20 20 20 36 31    16,   60,   61
2609b 2c 20 20 20 36 32 2c 20 20 20 36 33 2c 20 20 20  ,   62,   63,   
2609c 36 34 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f  64,. /*   230 */
2609d 20 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 20      65,   66,   
2609e 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20  67,   68,   69, 
2609f 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32    70,   71,   72
260a0 2c 20 20 31 34 37 2c 20 20 20 37 34 2c 0a 20 2f  ,  147,   74,. /
260a1 2a 20 20 20 32 34 30 20 2a 2f 20 20 20 20 37 35  *   240 */    75
260a2 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20  ,   76,   77,   
260a3 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20  78,   79,   80, 
260a4 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33    81,   82,   83
260a5 2c 20 20 20 38 34 2c 0a 20 2f 2a 20 20 20 32 35  ,   84,. /*   25
260a6 30 20 2a 2f 20 20 20 20 34 32 2c 20 20 20 34 33  0 */    42,   43
260a7 2c 20 20 20 37 38 2c 20 20 32 30 39 2c 20 20 32  ,   78,  209,  2
260a8 31 30 2c 20 20 31 32 34 2c 20 20 31 32 35 2c 20  10,  124,  125, 
260a9 20 31 32 36 2c 20 20 32 32 34 2c 20 20 32 30 38   126,  224,  208
260aa 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20  ,. /*   260 */  
260ab 20 31 36 39 2c 20 20 31 37 30 2c 20 20 20 38 38   169,  170,   88
260ac 2c 20 20 20 38 39 2c 20 20 32 33 30 2c 20 20 32  ,   89,  230,  2
260ad 32 37 2c 20 20 32 32 38 2c 20 20 20 31 36 2c 20  27,  228,   16, 
260ae 20 20 36 30 2c 20 20 20 36 31 2c 0a 20 2f 2a 20    60,   61,. /* 
260af 20 20 32 37 30 20 2a 2f 20 20 20 20 36 32 2c 20    270 */    62, 
260b0 20 20 36 33 2c 20 20 20 36 34 2c 20 20 20 36 35    63,   64,   65
260b1 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20 20  ,   66,   67,   
260b2 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20  68,   69,   70, 
260b3 20 20 37 31 2c 0a 20 2f 2a 20 20 20 32 38 30 20    71,. /*   280 
260b4 2a 2f 20 20 20 20 37 32 2c 20 20 20 32 33 2c 20  */    72,   23, 
260b5 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36    74,   75,   76
260b6 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20  ,   77,   78,   
260b7 37 39 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 0a  79,   80,   81,.
260b8 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20 20 20   /*   290 */    
260b9 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20  82,   83,   84, 
260ba 20 20 34 32 2c 20 20 20 34 33 2c 20 20 31 36 30    42,   43,  160
260bb 2c 20 20 20 31 36 2c 20 20 31 34 37 2c 20 20 31  ,   16,  147,  1
260bc 36 31 2c 20 20 20 38 33 2c 0a 20 2f 2a 20 20 20  61,   83,. /*   
260bd 33 30 30 20 2a 2f 20 20 20 20 38 34 2c 20 20 32  300 */    84,  2
260be 31 30 2c 20 20 31 36 31 2c 20 20 31 35 33 2c 20  10,  161,  153, 
260bf 20 31 36 39 2c 20 20 31 35 38 2c 20 20 31 35 36   169,  158,  156
260c0 2c 20 20 31 35 37 2c 20 20 31 36 31 2c 20 20 31  ,  157,  161,  1
260c1 36 32 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f  62,. /*   310 */
260c2 20 20 20 31 36 33 2c 20 20 20 36 30 2c 20 20 20     163,   60,   
260c3 36 31 2c 20 20 20 36 32 2c 20 20 20 36 33 2c 20  61,   62,   63, 
260c4 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20 36 36    64,   65,   66
260c5 2c 20 20 20 36 37 2c 20 20 20 36 38 2c 0a 20 2f  ,   67,   68,. /
260c6 2a 20 20 20 33 32 30 20 2a 2f 20 20 20 20 36 39  *   320 */    69
260c7 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20  ,   70,   71,   
260c8 37 32 2c 20 20 31 36 31 2c 20 20 20 37 34 2c 20  72,  161,   74, 
260c9 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37    75,   76,   77
260ca 2c 20 20 20 37 38 2c 0a 20 2f 2a 20 20 20 33 33  ,   78,. /*   33
260cb 30 20 2a 2f 20 20 20 20 37 39 2c 20 20 20 38 30  0 */    79,   80
260cc 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20  ,   81,   82,   
260cd 38 33 2c 20 20 20 38 34 2c 20 20 31 39 32 2c 20  83,   84,  192, 
260ce 20 32 30 30 2c 20 20 31 34 37 2c 20 20 31 33 31   200,  147,  131
260cf 2c 0a 20 2f 2a 20 20 20 33 34 30 20 2a 2f 20 20  ,. /*   340 */  
260d0 20 20 31 36 2c 20 20 32 30 30 2c 20 20 20 31 36    16,  200,   16
260d1 2c 20 20 31 39 39 2c 20 20 20 32 30 2c 20 20 31  ,  199,   20,  1
260d2 36 39 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20  69,   88,   89, 
260d3 20 20 39 30 2c 20 20 31 38 35 2c 0a 20 2f 2a 20    90,  185,. /* 
260d4 20 20 33 35 30 20 2a 2f 20 20 20 31 38 36 2c 20    350 */   186, 
260d5 20 20 39 33 2c 20 20 20 39 34 2c 20 20 20 39 35    93,   94,   95
260d6 2c 20 20 32 31 37 2c 20 20 20 32 32 2c 20 20 32  ,  217,   22,  2
260d7 31 39 2c 20 20 32 32 30 2c 20 20 31 34 37 2c 20  19,  220,  147, 
260d8 20 31 34 37 2c 0a 20 2f 2a 20 20 20 33 36 30 20   147,. /*   360 
260d9 2a 2f 20 20 20 31 36 39 2c 20 20 31 37 30 2c 20  */   169,  170, 
260da 20 31 30 34 2c 20 20 32 30 30 2c 20 20 20 38 34   104,  200,   84
260db 2c 20 20 31 34 37 2c 20 20 20 34 32 2c 20 20 20  ,  147,   42,   
260dc 34 33 2c 20 20 31 35 36 2c 20 20 31 35 37 2c 0a  43,  156,  157,.
260dd 20 2f 2a 20 20 20 33 37 30 20 2a 2f 20 20 20 20   /*   370 */    
260de 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20  90,   91,   92, 
260df 20 20 39 33 2c 20 20 20 39 34 2c 20 20 20 39 35    93,   94,   95
260e0 2c 20 20 20 39 36 2c 20 20 31 36 34 2c 20 20 31  ,   96,  164,  1
260e1 36 35 2c 20 20 31 36 36 2c 0a 20 2f 2a 20 20 20  65,  166,. /*   
260e2 33 38 30 20 2a 2f 20 20 20 31 36 39 2c 20 20 31  380 */   169,  1
260e3 37 30 2c 20 20 31 33 31 2c 20 20 31 30 33 2c 20  70,  131,  103, 
260e4 20 20 36 30 2c 20 20 20 36 31 2c 20 20 20 36 32    60,   61,   62
260e5 2c 20 20 20 36 33 2c 20 20 20 36 34 2c 20 20 20  ,   63,   64,   
260e6 36 35 2c 0a 20 2f 2a 20 20 20 33 39 30 20 2a 2f  65,. /*   390 */
260e7 20 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20 20      66,   67,   
260e8 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20  68,   69,   70, 
260e9 20 20 37 31 2c 20 20 20 37 32 2c 20 20 31 35 35    71,   72,  155
260ea 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 0a 20 2f  ,   74,   75,. /
260eb 2a 20 20 20 34 30 30 20 2a 2f 20 20 20 20 37 36  *   400 */    76
260ec 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20  ,   77,   78,   
260ed 37 39 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20  79,   80,   81, 
260ee 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34    82,   83,   84
260ef 2c 20 20 20 31 36 2c 0a 20 2f 2a 20 20 20 34 31  ,   16,. /*   41
260f0 30 20 2a 2f 20 20 20 20 38 34 2c 20 20 20 31 31  0 */    84,   11
260f1 2c 20 20 32 32 31 2c 20 20 20 32 30 2c 20 20 20  ,  221,   20,   
260f2 33 30 2c 20 20 20 31 36 2c 20 20 31 34 37 2c 20  30,   16,  147, 
260f3 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20 39 33    91,   92,   93
260f4 2c 0a 20 2f 2a 20 20 20 34 32 30 20 2a 2f 20 20  ,. /*   420 */  
260f5 20 20 39 34 2c 20 20 20 39 35 2c 20 20 20 39 36    94,   95,   96
260f6 2c 20 20 20 39 30 2c 20 20 31 34 37 2c 20 20 31  ,   90,  147,  1
260f7 38 31 2c 20 20 20 39 33 2c 20 20 20 39 34 2c 20  81,   93,   94, 
260f8 20 20 39 35 2c 20 20 31 30 33 2c 0a 20 2f 2a 20    95,  103,. /* 
260f9 20 20 34 33 30 20 2a 2f 20 20 20 32 31 32 2c 20    430 */   212, 
260fa 20 31 38 39 2c 20 20 31 35 35 2c 20 20 20 32 37   189,  155,   27
260fb 2c 20 20 20 35 30 2c 20 20 20 34 32 2c 20 20 20  ,   50,   42,   
260fc 34 33 2c 20 20 31 30 34 2c 20 20 31 36 39 2c 20  43,  104,  169, 
260fd 20 31 37 30 2c 0a 20 2f 2a 20 20 20 34 34 30 20   170,. /*   440 
260fe 2a 2f 20 20 20 20 33 34 2c 20 20 32 32 38 2c 20  */    34,  228, 
260ff 20 20 34 33 2c 20 20 32 30 31 2c 20 20 32 30 32    43,  201,  202
26100 2c 20 20 31 38 31 2c 20 20 31 36 39 2c 20 20 31  ,  181,  169,  1
26101 37 30 2c 20 20 32 30 36 2c 20 20 20 34 39 2c 0a  70,  206,   49,.
26102 20 2f 2a 20 20 20 34 35 30 20 2a 2f 20 20 20 31   /*   450 */   1
26103 36 31 2c 20 20 31 36 32 2c 20 20 31 36 33 2c 20  61,  162,  163, 
26104 20 20 36 30 2c 20 20 20 36 31 2c 20 20 20 36 32    60,   61,   62
26105 2c 20 20 20 36 33 2c 20 20 20 36 34 2c 20 20 20  ,   63,   64,   
26106 36 35 2c 20 20 20 36 36 2c 0a 20 2f 2a 20 20 20  65,   66,. /*   
26107 34 36 30 20 2a 2f 20 20 20 20 36 37 2c 20 20 20  460 */    67,   
26108 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20  68,   69,   70, 
26109 20 20 37 31 2c 20 20 20 37 32 2c 20 20 31 38 39    71,   72,  189
2610a 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20  ,   74,   75,   
2610b 37 36 2c 0a 20 2f 2a 20 20 20 34 37 30 20 2a 2f  76,. /*   470 */
2610c 20 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20      77,   78,   
2610d 37 39 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20  79,   80,   81, 
2610e 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34    82,   83,   84
2610f 2c 20 20 20 31 36 2c 20 20 20 32 35 2c 0a 20 2f  ,   16,   25,. /
26110 2a 20 20 20 34 38 30 20 2a 2f 20 20 20 32 31 31  *   480 */   211
26111 2c 20 20 31 34 37 2c 20 20 20 32 30 2c 20 20 20  ,  147,   20,   
26112 32 39 2c 20 20 20 31 32 2c 20 20 31 34 37 2c 20  29,   12,  147, 
26113 20 31 30 32 2c 20 20 20 31 39 2c 20 20 32 31 31   102,   19,  211
26114 2c 20 20 20 32 31 2c 0a 20 2f 2a 20 20 20 34 39  ,   21,. /*   49
26115 30 20 2a 2f 20 20 20 31 34 37 2c 20 20 31 34 31  0 */   147,  141
26116 2c 20 20 31 34 37 2c 20 20 32 31 36 2c 20 20 31  ,  147,  216,  1
26117 34 34 2c 20 20 20 34 31 2c 20 20 20 32 34 2c 20  44,   41,   24, 
26118 20 20 39 38 2c 20 20 20 32 30 2c 20 20 20 39 39    98,   20,   99
26119 2c 0a 20 2f 2a 20 20 20 35 30 30 20 2a 2f 20 20  ,. /*   500 */  
2611a 20 31 30 30 2c 20 20 31 30 31 2c 20 20 31 30 33   100,  101,  103
2611b 2c 20 20 31 36 35 2c 20 20 20 34 32 2c 20 20 20  ,  165,   42,   
2611c 34 33 2c 20 20 20 20 30 2c 20 20 20 20 31 2c 20  43,    0,    1, 
2611d 20 20 20 32 2c 20 20 20 33 37 2c 0a 20 2f 2a 20     2,   37,. /* 
2611e 20 20 35 31 30 20 2a 2f 20 20 20 31 31 30 2c 20    510 */   110, 
2611f 20 20 33 39 2c 20 20 31 36 39 2c 20 20 31 37 30    39,  169,  170
26120 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20 31  ,  169,  170,  1
26121 38 32 2c 20 20 20 31 39 2c 20 20 20 32 30 2c 20  82,   19,   20, 
26122 20 31 39 30 2c 0a 20 2f 2a 20 20 20 35 32 30 20   190,. /*   520 
26123 2a 2f 20 20 20 20 32 32 2c 20 20 20 34 39 2c 20  */    22,   49, 
26124 20 20 36 30 2c 20 20 20 36 31 2c 20 20 20 36 32    60,   61,   62
26125 2c 20 20 20 36 33 2c 20 20 20 36 34 2c 20 20 20  ,   63,   64,   
26126 36 35 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 0a  65,   66,   67,.
26127 20 2f 2a 20 20 20 35 33 30 20 2a 2f 20 20 20 20   /*   530 */    
26128 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20  68,   69,   70, 
26129 20 20 37 31 2c 20 20 20 37 32 2c 20 20 31 35 35    71,   72,  155
2612a 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20  ,   74,   75,   
2612b 37 36 2c 20 20 20 37 37 2c 0a 20 2f 2a 20 20 20  76,   77,. /*   
2612c 35 34 30 20 2a 2f 20 20 20 20 37 38 2c 20 20 20  540 */    78,   
2612d 37 39 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20  79,   80,   81, 
2612e 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34    82,   83,   84
2612f 2c 20 20 20 31 36 2c 20 20 31 34 37 2c 20 20 20  ,   16,  147,   
26130 39 30 2c 0a 20 2f 2a 20 20 20 35 35 30 20 2a 2f  90,. /*   550 */
26131 20 20 20 20 32 30 2c 20 20 20 32 30 2c 20 20 20      20,   20,   
26132 39 33 2c 20 20 20 39 34 2c 20 20 20 39 35 2c 20  93,   94,   95, 
26133 20 31 34 37 2c 20 20 31 35 35 2c 20 20 20 35 39   147,  155,   59
26134 2c 20 20 32 31 35 2c 20 20 32 32 35 2c 0a 20 2f  ,  215,  225,. /
26135 2a 20 20 20 35 36 30 20 2a 2f 20 20 20 32 31 35  *   560 */   215
26136 2c 20 20 20 32 30 2c 20 20 31 33 30 2c 20 20 31  ,   20,  130,  1
26137 30 34 2c 20 20 31 33 32 2c 20 20 32 32 37 2c 20  04,  132,  227, 
26138 20 32 32 38 2c 20 20 20 34 32 2c 20 20 20 34 33   228,   42,   43
26139 2c 20 20 31 38 39 2c 0a 20 2f 2a 20 20 20 35 37  ,  189,. /*   57
2613a 30 20 2a 2f 20 20 20 31 36 39 2c 20 20 31 37 30  0 */   169,  170
2613b 2c 20 20 20 31 36 2c 20 20 20 34 32 2c 20 20 20  ,   16,   42,   
2613c 34 33 2c 20 20 20 32 30 2c 20 20 20 31 39 2c 20  43,   20,   19, 
2613d 20 20 32 32 2c 20 20 20 31 39 2c 20 20 20 32 30    22,   19,   20
2613e 2c 0a 20 2f 2a 20 20 20 35 38 30 20 2a 2f 20 20  ,. /*   580 */  
2613f 20 20 32 33 2c 20 20 20 32 32 2c 20 20 20 31 38    23,   22,   18
26140 2c 20 20 31 34 37 2c 20 20 31 30 36 2c 20 20 31  ,  147,  106,  1
26141 34 37 2c 20 20 31 30 38 2c 20 20 31 30 39 2c 20  47,  108,  109, 
26142 20 20 36 33 2c 20 20 20 36 34 2c 0a 20 2f 2a 20    63,   64,. /* 
26143 20 20 35 39 30 20 2a 2f 20 20 20 31 38 39 2c 20    590 */   189, 
26144 20 20 36 30 2c 20 20 20 36 31 2c 20 20 20 36 32    60,   61,   62
26145 2c 20 20 20 36 33 2c 20 20 20 36 34 2c 20 20 20  ,   63,   64,   
26146 36 35 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20  65,   66,   67, 
26147 20 20 36 38 2c 0a 20 2f 2a 20 20 20 36 30 30 20    68,. /*   600 
26148 2a 2f 20 20 20 20 36 39 2c 20 20 20 37 30 2c 20  */    69,   70, 
26149 20 20 37 31 2c 20 20 20 37 32 2c 20 20 31 34 37    71,   72,  147
2614a 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20  ,   74,   75,   
2614b 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 0a  76,   77,   78,.
2614c 20 2f 2a 20 20 20 36 31 30 20 2a 2f 20 20 20 20   /*   610 */    
2614d 37 39 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20  79,   80,   81, 
2614e 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34    82,   83,   84
2614f 2c 20 20 20 31 36 2c 20 20 20 39 32 2c 20 20 20  ,   16,   92,   
26150 35 39 2c 20 20 20 35 35 2c 0a 20 2f 2a 20 20 20  59,   55,. /*   
26151 36 32 30 20 2a 2f 20 20 20 32 31 32 2c 20 20 20  620 */   212,   
26152 32 31 2c 20 20 31 34 37 2c 20 20 20 31 39 2c 20  21,  147,   19, 
26153 20 31 34 37 2c 20 20 20 32 33 2c 20 20 31 38 38   147,   23,  188
26154 2c 20 20 20 32 33 2c 20 20 20 31 32 2c 20 20 32  ,   23,   12,  2
26155 31 37 2c 0a 20 2f 2a 20 20 20 36 33 30 20 2a 2f  17,. /*   630 */
26156 20 20 20 20 32 33 2c 20 20 32 31 39 2c 20 20 32      23,  219,  2
26157 32 30 2c 20 20 20 20 37 2c 20 20 20 20 38 2c 20  20,    7,    8, 
26158 20 20 20 39 2c 20 20 31 30 36 2c 20 20 31 38 36     9,  106,  186
26159 2c 20 20 31 30 38 2c 20 20 31 30 39 2c 0a 20 2f  ,  108,  109,. /
2615a 2a 20 20 20 36 34 30 20 2a 2f 20 20 20 20 32 34  *   640 */    24
2615b 2c 20 20 31 34 37 2c 20 20 20 34 32 2c 20 20 20  ,  147,   42,   
2615c 34 33 2c 20 20 32 30 38 2c 20 20 20 38 38 2c 20  43,  208,   88, 
2615d 20 20 38 39 2c 20 20 31 30 36 2c 20 20 20 39 32    89,  106,   92
2615e 2c 20 20 31 30 38 2c 0a 20 2f 2a 20 20 20 36 35  ,  108,. /*   65
2615f 30 20 2a 2f 20 20 20 31 30 39 2c 20 20 32 34 34  0 */   109,  244
26160 2c 20 20 32 34 35 2c 20 20 20 33 37 2c 20 20 31  ,  245,   37,  1
26161 34 37 2c 20 20 20 33 39 2c 20 20 31 34 37 2c 20  47,   39,  147, 
26162 20 31 38 32 2c 20 20 20 39 34 2c 20 20 20 31 36   182,   94,   16
26163 2c 0a 20 2f 2a 20 20 20 36 36 30 20 2a 2f 20 20  ,. /*   660 */  
26164 20 20 36 30 2c 20 20 20 36 31 2c 20 20 20 36 32    60,   61,   62
26165 2c 20 20 20 36 33 2c 20 20 20 36 34 2c 20 20 20  ,   63,   64,   
26166 36 35 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20  65,   66,   67, 
26167 20 20 36 38 2c 20 20 20 36 39 2c 0a 20 2f 2a 20    68,   69,. /* 
26168 20 20 36 37 30 20 2a 2f 20 20 20 20 37 30 2c 20    670 */    70, 
26169 20 20 37 31 2c 20 20 20 37 32 2c 20 20 31 34 35    71,   72,  145
2616a 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20  ,   74,   75,   
2616b 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20  76,   77,   78, 
2616c 20 20 37 39 2c 0a 20 2f 2a 20 20 20 36 38 30 20    79,. /*   680 
2616d 2a 2f 20 20 20 20 38 30 2c 20 20 20 38 31 2c 20  */    80,   81, 
2616e 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34    82,   83,   84
2616f 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20 20  ,   42,   43,   
26170 38 30 2c 20 20 31 34 32 2c 20 20 31 34 33 2c 0a  80,  142,  143,.
26171 20 2f 2a 20 20 20 36 39 30 20 2a 2f 20 20 20 20   /*   690 */    
26172 38 38 2c 20 20 20 38 39 2c 20 20 20 38 38 2c 20  88,   89,   88, 
26173 20 20 38 39 2c 20 20 31 34 38 2c 20 20 20 38 38    89,  148,   88
26174 2c 20 20 20 38 39 2c 20 20 31 33 33 2c 20 20 20  ,   89,  133,   
26175 31 34 2c 20 20 31 34 37 2c 0a 20 2f 2a 20 20 20  14,  147,. /*   
26176 37 30 30 20 2a 2f 20 20 20 32 32 35 2c 20 20 31  700 */   225,  1
26177 35 35 2c 20 20 20 31 36 2c 20 20 20 36 30 2c 20  55,   16,   60, 
26178 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20 36 33    61,   62,   63
26179 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20  ,   64,   65,   
2617a 36 36 2c 0a 20 2f 2a 20 20 20 37 31 30 20 2a 2f  66,. /*   710 */
2617b 20 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20 20      67,   68,   
2617c 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20  69,   70,   71, 
2617d 20 20 37 32 2c 20 20 31 31 34 2c 20 20 20 37 34    72,  114,   74
2617e 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 0a 20 2f  ,   75,   76,. /
2617f 2a 20 20 20 37 32 30 20 2a 2f 20 20 20 20 37 37  *   720 */    77
26180 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20  ,   78,   79,   
26181 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20  80,   81,   82, 
26182 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 34 32    83,   84,   42
26183 2c 20 20 20 34 33 2c 0a 20 2f 2a 20 20 20 37 33  ,   43,. /*   73
26184 30 20 2a 2f 20 20 20 32 30 31 2c 20 20 32 30 32  0 */   201,  202
26185 2c 20 20 31 34 37 2c 20 20 31 34 37 2c 20 20 31  ,  147,  147,  1
26186 38 32 2c 20 20 31 38 39 2c 20 20 20 35 32 2c 20  82,  189,   52, 
26187 20 31 34 37 2c 20 20 20 35 34 2c 20 20 31 34 37   147,   54,  147
26188 2c 0a 20 2f 2a 20 20 20 37 34 30 20 2a 2f 20 20  ,. /*   740 */  
26189 20 31 34 37 2c 20 20 31 34 37 2c 20 20 31 34 37   147,  147,  147
2618a 2c 20 20 31 34 37 2c 20 20 31 35 35 2c 20 20 20  ,  147,  155,   
2618b 31 36 2c 20 20 20 36 30 2c 20 20 20 36 31 2c 20  16,   60,   61, 
2618c 20 20 36 32 2c 20 20 20 36 33 2c 0a 20 2f 2a 20    62,   63,. /* 
2618d 20 20 37 35 30 20 2a 2f 20 20 20 20 36 34 2c 20    750 */    64, 
2618e 20 20 36 35 2c 20 20 20 36 36 2c 20 20 20 36 37    65,   66,   67
2618f 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20  ,   68,   69,   
26190 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20  70,   71,   72, 
26191 20 32 31 33 2c 0a 20 2f 2a 20 20 20 37 36 30 20   213,. /*   760 
26192 2a 2f 20 20 20 20 37 34 2c 20 20 20 37 35 2c 20  */    74,   75, 
26193 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38    76,   77,   78
26194 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 20  ,   79,   80,   
26195 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 0a  81,   82,   83,.
26196 20 2f 2a 20 20 20 37 37 30 20 2a 2f 20 20 20 20   /*   770 */    
26197 38 34 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20  84,   42,   43, 
26198 20 31 38 38 2c 20 20 31 38 38 2c 20 20 31 38 32   188,  188,  182
26199 2c 20 20 31 38 32 2c 20 20 32 32 35 2c 20 20 31  ,  182,  225,  1
2619a 38 39 2c 20 20 31 30 36 2c 0a 20 2f 2a 20 20 20  89,  106,. /*   
2619b 37 38 30 20 2a 2f 20 20 20 31 38 38 2c 20 20 31  780 */   188,  1
2619c 30 38 2c 20 20 31 30 39 2c 20 20 31 38 38 2c 20  08,  109,  188, 
2619d 20 20 39 39 2c 20 20 31 30 30 2c 20 20 31 30 31    99,  100,  101
2619e 2c 20 20 32 34 31 2c 20 20 20 31 36 2c 20 20 31  ,  241,   16,  1
2619f 35 35 2c 0a 20 2f 2a 20 20 20 37 39 30 20 2a 2f  55,. /*   790 */
261a0 20 20 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20      61,   62,   
261a1 36 33 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20  63,   64,   65, 
261a2 20 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38    66,   67,   68
261a3 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 0a 20 2f  ,   69,   70,. /
261a4 2a 20 20 20 38 30 30 20 2a 2f 20 20 20 20 37 31  *   800 */    71
261a5 2c 20 20 20 37 32 2c 20 20 32 31 33 2c 20 20 20  ,   72,  213,   
261a6 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20  74,   75,   76, 
261a7 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39    77,   78,   79
261a8 2c 20 20 20 38 30 2c 0a 20 2f 2a 20 20 20 38 31  ,   80,. /*   81
261a9 30 20 2a 2f 20 20 20 20 38 31 2c 20 20 20 38 32  0 */    81,   82
261aa 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20  ,   83,   84,   
261ab 34 32 2c 20 20 20 34 33 2c 20 20 20 32 33 2c 20  42,   43,   23, 
261ac 20 31 33 33 2c 20 20 32 32 35 2c 20 20 32 32 35   133,  225,  225
261ad 2c 0a 20 2f 2a 20 20 20 38 32 30 20 2a 2f 20 20  ,. /*   820 */  
261ae 20 20 32 31 2c 20 20 32 32 35 2c 20 20 20 32 33    21,  225,   23
261af 2c 20 20 31 38 39 2c 20 20 32 33 39 2c 20 20 32  ,  189,  239,  2
261b0 33 36 2c 20 20 20 39 39 2c 20 20 31 30 30 2c 20  36,   99,  100, 
261b1 20 31 30 31 2c 20 20 20 32 32 2c 0a 20 2f 2a 20   101,   22,. /* 
261b2 20 20 38 33 30 20 2a 2f 20 20 20 32 34 32 2c 20    830 */   242, 
261b3 20 32 34 33 2c 20 20 31 35 35 2c 20 20 31 39 31   243,  155,  191
261b4 2c 20 20 20 36 32 2c 20 20 20 36 33 2c 20 20 20  ,   62,   63,   
261b5 36 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20  64,   65,   66, 
261b6 20 20 36 37 2c 0a 20 2f 2a 20 20 20 38 34 30 20    67,. /*   840 
261b7 2a 2f 20 20 20 20 36 38 2c 20 20 20 36 39 2c 20  */    68,   69, 
261b8 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32    70,   71,   72
261b9 2c 20 20 31 34 37 2c 20 20 20 37 34 2c 20 20 20  ,  147,   74,   
261ba 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 0a  75,   76,   77,.
261bb 20 2f 2a 20 20 20 38 35 30 20 2a 2f 20 20 20 20   /*   850 */    
261bc 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20  78,   79,   80, 
261bd 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33    81,   82,   83
261be 2c 20 20 20 38 34 2c 20 20 20 31 36 2c 20 20 20  ,   84,   16,   
261bf 31 37 2c 20 20 20 32 32 2c 0a 20 2f 2a 20 20 20  17,   22,. /*   
261c0 38 36 30 20 2a 2f 20 20 20 20 31 39 2c 20 20 31  860 */    19,  1
261c1 34 37 2c 20 20 31 34 37 2c 20 20 31 34 37 2c 20  47,  147,  147, 
261c2 20 20 32 33 2c 20 20 31 34 37 2c 20 20 31 38 39    23,  147,  189
261c3 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20 31  ,  169,  170,  1
261c4 34 37 2c 0a 20 2f 2a 20 20 20 38 37 30 20 2a 2f  47,. /*   870 */
261c5 20 20 20 20 31 34 2c 20 20 31 34 37 2c 20 20 20      14,  147,   
261c6 33 31 2c 20 20 20 31 36 2c 20 20 20 31 37 2c 20  31,   16,   17, 
261c7 20 31 34 37 2c 20 20 20 31 39 2c 20 20 31 34 37   147,   19,  147
261c8 2c 20 20 20 31 39 2c 20 20 20 34 33 2c 0a 20 2f  ,   19,   43,. /
261c9 2a 20 20 20 38 38 30 20 2a 2f 20 20 20 20 32 33  *   880 */    23
261ca 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 31  ,   88,   89,  1
261cb 36 39 2c 20 20 31 37 30 2c 20 20 31 36 39 2c 20  69,  170,  169, 
261cc 20 31 37 30 2c 20 20 20 38 38 2c 20 20 20 33 31   170,   88,   31
261cd 2c 20 20 20 34 38 2c 0a 20 2f 2a 20 20 20 38 39  ,   48,. /*   89
261ce 30 20 2a 2f 20 20 20 31 34 37 2c 20 20 31 36 39  0 */   147,  169
261cf 2c 20 20 31 37 30 2c 20 20 31 36 39 2c 20 20 31  ,  170,  169,  1
261d0 37 30 2c 20 20 31 34 37 2c 20 20 20 38 39 2c 20  70,  147,   89, 
261d1 20 31 36 39 2c 20 20 31 37 30 2c 20 20 20 35 38   169,  170,   58
261d2 2c 0a 20 2f 2a 20 20 20 39 30 30 20 2a 2f 20 20  ,. /*   900 */  
261d3 20 31 34 37 2c 20 20 31 34 37 2c 20 20 31 34 37   147,  147,  147
261d4 2c 20 20 31 38 38 2c 20 20 31 34 37 2c 20 20 20  ,  188,  147,   
261d5 34 38 2c 20 20 31 38 38 2c 20 20 31 31 34 2c 20  48,  188,  114, 
261d6 20 20 35 32 2c 20 20 31 34 37 2c 0a 20 2f 2a 20    52,  147,. /* 
261d7 20 20 39 31 30 20 2a 2f 20 20 20 20 35 34 2c 20    910 */    54, 
261d8 20 20 31 39 2c 20 20 31 34 37 2c 20 20 31 32 34    19,  147,  124
261d9 2c 20 20 31 32 35 2c 20 20 20 35 38 2c 20 20 31  ,  125,   58,  1
261da 34 37 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 20  47,  169,  170, 
261db 20 20 37 38 2c 0a 20 2f 2a 20 20 20 39 32 30 20    78,. /*   920 
261dc 2a 2f 20 20 20 20 37 39 2c 20 20 31 31 34 2c 20  */    79,  114, 
261dd 20 31 36 39 2c 20 20 31 37 30 2c 20 20 31 36 39   169,  170,  169
261de 2c 20 20 31 37 30 2c 20 20 31 36 39 2c 20 20 31  ,  170,  169,  1
261df 37 30 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 0a  70,   87,   88,.
261e0 20 2f 2a 20 20 20 39 33 30 20 2a 2f 20 20 20 20   /*   930 */    
261e1 38 39 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 20  89,  169,  170, 
261e2 20 20 39 32 2c 20 20 20 39 38 2c 20 20 20 37 38    92,   98,   78
261e3 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 31  ,   79,   80,  1
261e4 36 39 2c 20 20 31 37 30 2c 0a 20 2f 2a 20 20 20  69,  170,. /*   
261e5 39 34 30 20 2a 2f 20 20 20 20 39 31 2c 20 20 31  940 */    91,  1
261e6 34 37 2c 20 20 31 38 38 2c 20 20 20 32 32 2c 20  47,  188,   22, 
261e7 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39    87,   88,   89
261e8 2c 20 20 20 31 36 2c 20 20 20 31 37 2c 20 20 20  ,   16,   17,   
261e9 39 32 2c 0a 20 2f 2a 20 20 20 39 35 30 20 2a 2f  92,. /*   950 */
261ea 20 20 20 20 31 39 2c 20 20 31 31 30 2c 20 20 31      19,  110,  1
261eb 34 37 2c 20 20 31 35 35 2c 20 20 20 32 33 2c 20  47,  155,   23, 
261ec 20 31 34 37 2c 20 20 31 35 35 2c 20 20 20 32 32   147,  155,   22
261ed 2c 20 20 31 32 31 2c 20 20 31 31 30 2c 0a 20 2f  ,  121,  110,. /
261ee 2a 20 20 20 39 36 30 20 2a 2f 20 20 20 20 36 38  *   960 */    68
261ef 2c 20 20 20 38 30 2c 20 20 20 33 31 2c 20 20 31  ,   80,   31,  1
261f0 36 39 2c 20 20 31 37 30 2c 20 20 31 32 34 2c 20  69,  170,  124, 
261f1 20 31 32 35 2c 20 20 31 32 36 2c 20 20 31 32 37   125,  126,  127
261f2 2c 20 20 31 32 38 2c 0a 20 2f 2a 20 20 20 39 37  ,  128,. /*   97
261f3 30 20 2a 2f 20 20 20 31 32 39 2c 20 20 31 31 32  0 */   129,  112
261f4 2c 20 20 31 32 33 2c 20 20 32 30 38 2c 20 20 31  ,  123,  208,  1
261f5 36 39 2c 20 20 31 37 30 2c 20 20 31 30 37 2c 20  69,  170,  107, 
261f6 20 31 36 39 2c 20 20 31 37 30 2c 20 20 20 34 38   169,  170,   48
261f7 2c 0a 20 2f 2a 20 20 20 39 38 30 20 2a 2f 20 20  ,. /*   980 */  
261f8 20 31 31 31 2c 20 20 31 32 34 2c 20 20 31 32 35   111,  124,  125
261f9 2c 20 20 31 32 36 2c 20 20 31 32 37 2c 20 20 31  ,  126,  127,  1
261fa 32 38 2c 20 20 31 32 39 2c 20 20 31 38 39 2c 20  28,  129,  189, 
261fb 20 31 30 37 2c 20 20 20 35 38 2c 0a 20 2f 2a 20   107,   58,. /* 
261fc 20 20 39 39 30 20 2a 2f 20 20 20 31 38 39 2c 20    990 */   189, 
261fd 20 20 20 35 2c 20 20 31 31 31 2c 20 20 31 34 37     5,  111,  147
261fe 2c 20 20 20 20 37 2c 20 20 20 20 38 2c 20 20 20  ,    7,    8,   
261ff 31 30 2c 20 20 20 31 31 2c 20 20 20 31 32 2c 20  10,   11,   12, 
26200 20 20 31 33 2c 0a 20 2f 2a 20 20 31 30 30 30 20    13,. /*  1000 
26201 2a 2f 20 20 20 31 36 31 2c 20 20 20 32 30 2c 20  */   161,   20, 
26202 20 31 34 37 2c 20 20 20 32 32 2c 20 20 31 37 38   147,   22,  178
26203 2c 20 20 31 34 37 2c 20 20 20 39 31 2c 20 20 20  ,  147,   91,   
26204 39 32 2c 20 20 31 34 37 2c 20 20 20 37 38 2c 0a  92,  147,   78,.
26205 20 2f 2a 20 20 31 30 31 30 20 2a 2f 20 20 20 20   /*  1010 */    
26206 37 39 2c 20 20 31 34 37 2c 20 20 20 32 36 2c 20  79,  147,   26, 
26207 20 20 31 39 2c 20 20 20 32 38 2c 20 20 31 36 39    19,   28,  169
26208 2c 20 20 31 37 30 2c 20 20 20 32 33 2c 20 20 20  ,  170,   23,   
26209 38 37 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20 31  87,   88,. /*  1
2620a 30 32 30 20 2a 2f 20 20 20 20 38 39 2c 20 20 20  020 */    89,   
2620b 33 35 2c 20 20 31 34 37 2c 20 20 20 39 32 2c 20  35,  147,   92, 
2620c 20 31 36 39 2c 20 20 31 37 30 2c 20 20 31 34 37   169,  170,  147
2620d 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20 31  ,  169,  170,  1
2620e 34 37 2c 0a 20 2f 2a 20 20 31 30 33 30 20 2a 2f  47,. /*  1030 */
2620f 20 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20 20     169,  170,   
26210 39 37 2c 20 20 20 34 37 2c 20 20 31 31 33 2c 20  97,   47,  113, 
26211 20 20 34 39 2c 20 20 20 32 30 2c 20 20 32 30 33    49,   20,  203
26212 2c 20 20 20 32 32 2c 20 20 20 35 33 2c 0a 20 2f  ,   22,   53,. /
26213 2a 20 20 31 30 34 30 20 2a 2f 20 20 20 31 34 37  *  1040 */   147
26214 2c 20 20 31 34 37 2c 20 20 20 35 36 2c 20 20 31  ,  147,   56,  1
26215 34 37 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 20  47,  169,  170, 
26216 20 31 34 37 2c 20 20 31 34 37 2c 20 20 31 34 37   147,  147,  147
26217 2c 20 20 20 32 30 2c 0a 20 2f 2a 20 20 31 30 35  ,   20,. /*  105
26218 30 20 2a 2f 20 20 20 31 34 37 2c 20 20 31 36 39  0 */   147,  169
26219 2c 20 20 31 37 30 2c 20 20 31 34 37 2c 20 20 31  ,  170,  147,  1
2621a 34 37 2c 20 20 31 32 34 2c 20 20 31 32 35 2c 20  47,  124,  125, 
2621b 20 31 32 36 2c 20 20 31 32 37 2c 20 20 31 32 38   126,  127,  128
2621c 2c 0a 20 2f 2a 20 20 31 30 36 30 20 2a 2f 20 20  ,. /*  1060 */  
2621d 20 31 32 39 2c 20 20 31 34 37 2c 20 20 31 36 39   129,  147,  169
2621e 2c 20 20 31 37 30 2c 20 20 31 37 38 2c 20 20 31  ,  170,  178,  1
2621f 36 39 2c 20 20 31 37 30 2c 20 20 31 34 37 2c 20  69,  170,  147, 
26220 20 31 36 39 2c 20 20 31 37 30 2c 0a 20 2f 2a 20   169,  170,. /* 
26221 20 31 30 37 30 20 2a 2f 20 20 20 31 36 39 2c 20   1070 */   169, 
26222 20 31 37 30 2c 20 20 31 36 39 2c 20 20 31 37 30   170,  169,  170
26223 2c 20 20 31 34 37 2c 20 20 31 36 39 2c 20 20 31  ,  147,  169,  1
26224 37 30 2c 20 20 31 34 37 2c 20 20 20 32 30 2c 20  70,  147,   20, 
26225 20 31 34 37 2c 0a 20 2f 2a 20 20 31 30 38 30 20   147,. /*  1080 
26226 2a 2f 20 20 20 20 32 32 2c 20 20 31 34 37 2c 20  */    22,  147, 
26227 20 20 38 38 2c 20 20 31 34 37 2c 20 20 31 34 37    88,  147,  147
26228 2c 20 20 20 39 39 2c 20 20 31 30 30 2c 20 20 31  ,   99,  100,  1
26229 30 31 2c 20 20 20 35 39 2c 20 20 31 36 39 2c 0a  01,   59,  169,.
2622a 20 2f 2a 20 20 31 30 39 30 20 2a 2f 20 20 20 31   /*  1090 */   1
2622b 37 30 2c 20 20 31 30 35 2c 20 20 31 34 37 2c 20  70,  105,  147, 
2622c 20 20 32 30 2c 20 20 31 34 37 2c 20 20 20 32 32    20,  147,   22
2622d 2c 20 20 31 31 30 2c 20 20 31 37 38 2c 20 20 31  ,  110,  178,  1
2622e 34 37 2c 20 20 31 36 39 2c 0a 20 2f 2a 20 20 31  47,  169,. /*  1
2622f 31 30 30 20 2a 2f 20 20 20 31 37 30 2c 20 20 31  100 */   170,  1
26230 36 39 2c 20 20 31 37 30 2c 20 20 31 36 39 2c 20  69,  170,  169, 
26231 20 31 37 30 2c 20 20 31 36 39 2c 20 20 31 37 30   170,  169,  170
26232 2c 20 20 20 32 30 2c 20 20 31 34 37 2c 20 20 20  ,   20,  147,   
26233 32 32 2c 0a 20 2f 2a 20 20 31 31 31 30 20 2a 2f  22,. /*  1110 */
26234 20 20 20 31 34 37 2c 20 20 20 32 30 2c 20 20 31     147,   20,  1
26235 34 37 2c 20 20 20 32 32 2c 20 20 31 36 39 2c 20  47,   22,  169, 
26236 20 31 37 30 2c 20 20 31 36 39 2c 20 20 31 37 30   170,  169,  170
26237 2c 20 20 31 34 37 2c 20 20 31 34 37 2c 0a 20 2f  ,  147,  147,. /
26238 2a 20 20 31 31 32 30 20 2a 2f 20 20 20 31 33 34  *  1120 */   134
26239 2c 20 20 20 32 30 2c 20 20 31 34 37 2c 20 20 20  ,   20,  147,   
2623a 32 32 2c 20 20 20 32 30 2c 20 20 31 34 37 2c 20  22,   20,  147, 
2623b 20 20 32 32 2c 20 20 31 34 37 2c 20 20 20 32 30    22,  147,   20
2623c 2c 20 20 32 33 32 2c 0a 20 2f 2a 20 20 31 31 33  ,  232,. /*  113
2623d 30 20 2a 2f 20 20 20 20 32 32 2c 20 20 32 33 33  0 */    22,  233
2623e 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20 31  ,  169,  170,  1
2623f 36 39 2c 20 20 31 37 30 2c 20 20 31 34 37 2c 20  69,  170,  147, 
26240 20 31 34 37 2c 20 20 31 34 37 2c 20 20 31 34 37   147,  147,  147
26241 2c 0a 20 2f 2a 20 20 31 31 34 30 20 2a 2f 20 20  ,. /*  1140 */  
26242 20 31 36 39 2c 20 20 31 37 30 2c 20 20 31 34 37   169,  170,  147
26243 2c 20 20 31 34 37 2c 20 20 31 36 39 2c 20 20 31  ,  147,  169,  1
26244 37 30 2c 20 20 31 34 37 2c 20 20 31 36 39 2c 20  70,  147,  169, 
26245 20 31 37 30 2c 20 20 31 36 39 2c 0a 20 2f 2a 20   170,  169,. /* 
26246 20 31 31 35 30 20 2a 2f 20 20 20 31 37 30 2c 20   1150 */   170, 
26247 20 31 34 37 2c 20 20 31 34 37 2c 20 20 31 34 37   147,  147,  147
26248 2c 20 20 31 34 37 2c 20 20 31 34 37 2c 20 20 31  ,  147,  147,  1
26249 34 37 2c 20 20 31 39 31 2c 20 20 31 36 31 2c 20  47,  191,  161, 
2624a 20 31 34 39 2c 0a 20 2f 2a 20 20 31 31 36 30 20   149,. /*  1160 
2624b 2a 2f 20 20 20 31 39 33 2c 20 20 31 37 37 2c 20  */   193,  177, 
2624c 20 32 32 39 2c 20 20 32 32 33 2c 20 20 31 36 31   229,  223,  161
2624d 2c 20 20 31 37 32 2c 20 20 20 20 36 2c 20 20 32  ,  172,    6,  2
2624e 32 39 2c 20 20 31 39 34 2c 20 20 31 34 36 2c 0a  29,  194,  146,.
2624f 20 2f 2a 20 20 31 31 37 30 20 2a 2f 20 20 20 31   /*  1170 */   1
26250 37 32 2c 20 20 31 39 34 2c 20 20 31 37 32 2c 20  72,  194,  172, 
26251 20 31 37 32 2c 20 20 31 37 32 2c 20 20 31 36 31   172,  172,  161
26252 2c 20 20 31 34 36 2c 20 20 31 34 36 2c 20 20 31  ,  146,  146,  1
26253 34 36 2c 20 20 20 32 32 2c 0a 20 2f 2a 20 20 31  46,   22,. /*  1
26254 31 38 30 20 2a 2f 20 20 20 31 35 34 2c 20 20 31  180 */   154,  1
26255 32 31 2c 20 20 31 39 34 2c 20 20 31 31 38 2c 20  21,  194,  118, 
26256 20 31 37 33 2c 20 20 31 31 39 2c 20 20 31 31 36   173,  119,  116
26257 2c 20 20 31 32 30 2c 20 20 31 31 32 2c 20 20 31  ,  120,  112,  1
26258 33 30 2c 0a 20 2f 2a 20 20 31 31 39 30 20 2a 2f  30,. /*  1190 */
26259 20 20 20 32 32 32 2c 20 20 31 35 32 2c 20 20 31     222,  152,  1
2625a 35 32 2c 20 20 20 39 38 2c 20 20 31 31 35 2c 20  52,   98,  115, 
2625b 20 20 39 38 2c 20 20 31 37 31 2c 20 20 20 39 37    98,  171,   97
2625c 2c 20 20 31 37 31 2c 20 20 20 34 30 2c 0a 20 2f  ,  171,   40,. /
2625d 2a 20 20 31 32 30 30 20 2a 2f 20 20 20 31 37 39  *  1200 */   179
2625e 2c 20 20 31 38 39 2c 20 20 20 31 39 2c 20 20 20  ,  189,   19,   
2625f 38 34 2c 20 20 31 37 31 2c 20 20 32 32 36 2c 20  84,  171,  226, 
26260 20 31 37 31 2c 20 20 31 37 33 2c 20 20 31 39 35   171,  173,  195
26261 2c 20 20 32 32 36 2c 0a 20 2f 2a 20 20 31 32 31  ,  226,. /*  121
26262 30 20 2a 2f 20 20 20 31 37 34 2c 20 20 31 39 36  0 */   174,  196
26263 2c 20 20 31 37 31 2c 20 20 31 37 31 2c 20 20 31  ,  171,  171,  1
26264 39 37 2c 20 20 31 37 31 2c 20 20 31 39 38 2c 20  97,  171,  198, 
26265 20 31 37 39 2c 20 20 20 31 35 2c 20 20 31 37 34   179,   15,  174
26266 2c 0a 20 2f 2a 20 20 31 32 32 30 20 2a 2f 20 20  ,. /*  1220 */  
26267 20 31 35 31 2c 20 20 20 36 30 2c 20 20 31 35 31   151,   60,  151
26268 2c 20 20 32 30 34 2c 20 20 31 35 32 2c 20 20 32  ,  204,  152,  2
26269 30 35 2c 20 20 32 30 34 2c 20 20 31 35 32 2c 20  05,  204,  152, 
2626a 20 31 35 31 2c 20 20 32 30 35 2c 0a 20 2f 2a 20   151,  205,. /* 
2626b 20 31 32 33 30 20 2a 2f 20 20 20 31 35 32 2c 20   1230 */   152, 
2626c 20 20 33 38 2c 20 20 31 35 32 2c 20 20 31 33 30    38,  152,  130
2626d 2c 20 20 31 35 31 2c 20 20 31 38 34 2c 20 20 31  ,  151,  184,  1
2626e 35 32 2c 20 20 31 38 34 2c 20 20 20 31 39 2c 20  52,  184,   19, 
2626f 20 20 31 35 2c 0a 20 2f 2a 20 20 31 32 34 30 20    15,. /*  1240 
26270 2a 2f 20 20 20 31 39 34 2c 20 20 31 35 32 2c 20  */   194,  152, 
26271 20 31 38 37 2c 20 20 31 38 37 2c 20 20 31 38 37   187,  187,  187
26272 2c 20 20 31 35 32 2c 20 20 31 39 34 2c 20 20 31  ,  152,  194,  1
26273 38 34 2c 20 20 31 38 37 2c 20 20 20 33 33 2c 0a  84,  187,   33,.
26274 20 2f 2a 20 20 31 32 35 30 20 2a 2f 20 20 20 31   /*  1250 */   1
26275 35 32 2c 20 20 31 35 32 2c 20 20 31 33 37 2c 20  52,  152,  137, 
26276 20 31 35 39 2c 20 20 20 20 31 2c 20 20 20 32 30   159,    1,   20
26277 2c 20 20 31 37 35 2c 20 20 32 31 34 2c 20 20 31  ,  175,  214,  1
26278 31 32 2c 20 20 31 31 32 2c 0a 20 2f 2a 20 20 31  12,  112,. /*  1
26279 32 36 30 20 2a 2f 20 20 20 31 37 35 2c 20 20 32  260 */   175,  2
2627a 31 34 2c 20 20 32 33 34 2c 20 20 31 31 32 2c 20  14,  234,  112, 
2627b 20 31 31 32 2c 20 20 20 39 32 2c 20 20 20 31 39   112,   92,   19
2627c 2c 20 20 20 31 31 2c 20 20 20 32 30 2c 20 20 31  ,   11,   20,  1
2627d 30 37 2c 0a 20 2f 2a 20 20 31 32 37 30 20 2a 2f  07,. /*  1270 */
2627e 20 20 20 20 32 30 2c 20 20 20 31 39 2c 20 20 32      20,   19,  2
2627f 33 35 2c 20 20 20 31 39 2c 20 20 20 33 32 2c 20  35,   19,   32, 
26280 20 20 32 30 2c 20 20 31 31 32 2c 20 20 31 31 34    20,  112,  114
26281 2c 20 20 20 32 30 2c 20 20 20 32 32 2c 0a 20 2f  ,   20,   22,. /
26282 2a 20 20 31 32 38 30 20 2a 2f 20 20 20 20 32 30  *  1280 */    20
26283 2c 20 20 20 32 32 2c 20 20 31 31 37 2c 20 20 20  ,   22,  117,   
26284 32 32 2c 20 20 31 31 37 2c 20 20 32 33 37 2c 20  22,  117,  237, 
26285 20 32 33 37 2c 20 20 20 31 39 2c 20 20 20 34 34   237,   19,   44
26286 2c 20 20 20 32 30 2c 0a 20 2f 2a 20 20 31 32 39  ,   20,. /*  129
26287 30 20 2a 2f 20 20 20 32 34 30 2c 20 20 20 32 30  0 */   240,   20
26288 2c 20 20 20 32 30 2c 20 20 32 33 31 2c 20 20 20  ,   20,  231,   
26289 31 39 2c 20 20 20 34 34 2c 20 20 20 31 39 2c 20  19,   44,   19, 
2628a 20 32 34 33 2c 20 20 20 32 30 2c 20 20 20 31 39   243,   20,   19
2628b 2c 0a 20 2f 2a 20 20 31 33 30 30 20 2a 2f 20 20  ,. /*  1300 */  
2628c 20 20 31 39 2c 20 20 20 31 39 2c 20 20 20 39 36    19,   19,   96
2628d 2c 20 20 31 30 33 2c 20 20 20 31 36 2c 20 20 20  ,  103,   16,   
2628e 32 31 2c 20 20 20 34 34 2c 20 20 20 31 37 2c 20  21,   44,   17, 
2628f 20 20 39 38 2c 20 20 20 33 36 2c 0a 20 2f 2a 20    98,   36,. /* 
26290 20 31 33 31 30 20 2a 2f 20 20 20 32 34 36 2c 20   1310 */   246, 
26291 20 20 34 35 2c 20 20 20 34 35 2c 20 20 20 32 32    45,   45,   22
26292 2c 20 20 20 35 31 2c 20 20 31 33 33 2c 20 20 20  ,   51,  133,   
26293 39 38 2c 20 20 20 31 39 2c 20 20 20 20 35 2c 20  98,   19,    5, 
26294 20 20 20 31 2c 0a 20 2f 2a 20 20 31 33 32 30 20     1,. /*  1320 
26295 2a 2f 20 20 20 31 32 32 2c 20 20 20 31 39 2c 20  */   122,   19, 
26296 20 31 30 32 2c 20 20 20 31 34 2c 20 20 31 31 33   102,   14,  113
26297 2c 20 20 20 31 37 2c 20 20 31 31 33 2c 20 20 31  ,   17,  113,  1
26298 31 35 2c 20 20 31 30 32 2c 20 20 31 32 32 2c 0a  15,  102,  122,.
26299 20 2f 2a 20 20 31 33 33 30 20 2a 2f 20 20 20 20   /*  1330 */    
2629a 31 39 2c 20 20 31 32 33 2c 20 20 20 36 38 2c 20  19,  123,   68, 
2629b 20 20 36 38 2c 20 20 20 32 30 2c 20 20 20 31 34    68,   20,   14
2629c 2c 20 20 20 35 37 2c 20 20 31 33 35 2c 20 20 20  ,   57,  135,   
2629d 31 39 2c 20 20 20 20 33 2c 0a 20 2f 2a 20 20 31  19,    3,. /*  1
2629e 33 34 30 20 2a 2f 20 20 20 31 33 36 2c 20 20 20  340 */   136,   
2629f 20 34 2c 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 59   4,.};.#define Y
262a0 59 5f 53 48 49 46 54 5f 55 53 45 5f 44 46 4c 54  Y_SHIFT_USE_DFLT
262a1 20 28 2d 36 32 29 0a 23 64 65 66 69 6e 65 20 59   (-62).#define Y
262a2 59 5f 53 48 49 46 54 5f 4d 41 58 20 33 38 39 0a  Y_SHIFT_MAX 389.
262a3 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 68 6f  static const sho
262a4 72 74 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74  rt yy_shift_ofst
262a5 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30  [] = {. /*     0
262a6 20 2a 2f 20 20 20 20 33 39 2c 20 20 38 34 31 2c   */    39,  841,
262a7 20 20 39 38 36 2c 20 20 2d 31 36 2c 20 20 38 34    986,  -16,  84
262a8 31 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 20 20  1,  931,  931,  
262a9 32 35 38 2c 20 20 31 32 33 2c 20 20 2d 33 36 2c  258,  123,  -36,
262aa 0a 20 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20 20  . /*    10 */   
262ab 20 39 36 2c 20 20 39 33 31 2c 20 20 39 33 31 2c   96,  931,  931,
262ac 20 20 39 33 31 2c 20 20 39 33 31 2c 20 20 39 33    931,  931,  93
262ad 31 2c 20 20 2d 34 35 2c 20 20 34 30 30 2c 20 20  1,  -45,  400,  
262ae 31 37 34 2c 20 20 20 31 39 2c 0a 20 2f 2a 20 20  174,   19,. /*  
262af 20 20 32 30 20 2a 2f 20 20 20 31 33 32 2c 20 20    20 */   132,  
262b0 2d 35 34 2c 20 20 2d 35 34 2c 20 20 20 35 33 2c  -54,  -54,   53,
262b1 20 20 31 36 35 2c 20 20 32 30 38 2c 20 20 32 35    165,  208,  25
262b2 31 2c 20 20 33 32 34 2c 20 20 33 39 33 2c 20 20  1,  324,  393,  
262b3 34 36 32 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a  462,. /*    30 *
262b4 2f 20 20 20 35 33 31 2c 20 20 36 30 30 2c 20 20  /   531,  600,  
262b5 36 34 33 2c 20 20 36 38 36 2c 20 20 36 34 33 2c  643,  686,  643,
262b6 20 20 36 34 33 2c 20 20 36 34 33 2c 20 20 36 34    643,  643,  64
262b7 33 2c 20 20 36 34 33 2c 20 20 36 34 33 2c 0a 20  3,  643,  643,. 
262b8 2f 2a 20 20 20 20 34 30 20 2a 2f 20 20 20 36 34  /*    40 */   64
262b9 33 2c 20 20 36 34 33 2c 20 20 36 34 33 2c 20 20  3,  643,  643,  
262ba 36 34 33 2c 20 20 36 34 33 2c 20 20 36 34 33 2c  643,  643,  643,
262bb 20 20 36 34 33 2c 20 20 36 34 33 2c 20 20 36 34    643,  643,  64
262bc 33 2c 20 20 36 34 33 2c 0a 20 2f 2a 20 20 20 20  3,  643,. /*    
262bd 35 30 20 2a 2f 20 20 20 36 34 33 2c 20 20 36 34  50 */   643,  64
262be 33 2c 20 20 37 32 39 2c 20 20 37 37 32 2c 20 20  3,  729,  772,  
262bf 37 37 32 2c 20 20 38 35 37 2c 20 20 39 33 31 2c  772,  857,  931,
262c0 20 20 39 33 31 2c 20 20 39 33 31 2c 20 20 39 33    931,  931,  93
262c1 31 2c 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20  1,. /*    60 */ 
262c2 20 20 39 33 31 2c 20 20 39 33 31 2c 20 20 39 33    931,  931,  93
262c3 31 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 20 20  1,  931,  931,  
262c4 39 33 31 2c 20 20 39 33 31 2c 20 20 39 33 31 2c  931,  931,  931,
262c5 20 20 39 33 31 2c 20 20 39 33 31 2c 0a 20 2f 2a    931,  931,. /*
262c6 20 20 20 20 37 30 20 2a 2f 20 20 20 39 33 31 2c      70 */   931,
262c7 20 20 39 33 31 2c 20 20 39 33 31 2c 20 20 39 33    931,  931,  93
262c8 31 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 20 20  1,  931,  931,  
262c9 39 33 31 2c 20 20 39 33 31 2c 20 20 39 33 31 2c  931,  931,  931,
262ca 20 20 39 33 31 2c 0a 20 2f 2a 20 20 20 20 38 30    931,. /*    80
262cb 20 2a 2f 20 20 20 39 33 31 2c 20 20 39 33 31 2c   */   931,  931,
262cc 20 20 39 33 31 2c 20 20 39 33 31 2c 20 20 39 33    931,  931,  93
262cd 31 2c 20 20 39 33 31 2c 20 20 39 33 31 2c 20 20  1,  931,  931,  
262ce 39 33 31 2c 20 20 39 33 31 2c 20 20 39 33 31 2c  931,  931,  931,
262cf 0a 20 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20 20  . /*    90 */   
262d0 39 33 31 2c 20 20 39 33 31 2c 20 20 39 33 31 2c  931,  931,  931,
262d1 20 20 39 33 31 2c 20 20 39 33 31 2c 20 20 39 33    931,  931,  93
262d2 31 2c 20 20 2d 36 31 2c 20 20 2d 36 31 2c 20 20  1,  -61,  -61,  
262d3 20 20 36 2c 20 20 20 20 36 2c 0a 20 2f 2a 20 20    6,    6,. /*  
262d4 20 31 30 30 20 2a 2f 20 20 20 32 38 30 2c 20 20   100 */   280,  
262d5 20 32 32 2c 20 20 20 36 31 2c 20 20 33 39 39 2c   22,   61,  399,
262d6 20 20 35 36 34 2c 20 20 20 31 39 2c 20 20 20 31    564,   19,   1
262d7 39 2c 20 20 20 31 39 2c 20 20 20 31 39 2c 20 20  9,   19,   19,  
262d8 20 31 39 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a   19,. /*   110 *
262d9 2f 20 20 20 20 31 39 2c 20 20 20 31 39 2c 20 20  /    19,   19,  
262da 32 31 36 2c 20 20 31 33 32 2c 20 20 20 36 33 2c  216,  132,   63,
262db 20 20 2d 36 32 2c 20 20 2d 36 32 2c 20 20 2d 36    -62,  -62,  -6
262dc 32 2c 20 20 31 33 31 2c 20 20 33 32 36 2c 0a 20  2,  131,  326,. 
262dd 2f 2a 20 20 20 31 32 30 20 2a 2f 20 20 20 34 37  /*   120 */   47
262de 32 2c 20 20 34 37 32 2c 20 20 34 39 38 2c 20 20  2,  472,  498,  
262df 35 35 39 2c 20 20 35 30 36 2c 20 20 37 39 39 2c  559,  506,  799,
262e0 20 20 20 31 39 2c 20 20 37 39 39 2c 20 20 20 31     19,  799,   1
262e1 39 2c 20 20 20 31 39 2c 0a 20 2f 2a 20 20 20 31  9,   19,. /*   1
262e2 33 30 20 2a 2f 20 20 20 20 31 39 2c 20 20 20 31  30 */    19,   1
262e3 39 2c 20 20 20 31 39 2c 20 20 20 31 39 2c 20 20  9,   19,   19,  
262e4 20 31 39 2c 20 20 20 31 39 2c 20 20 20 31 39 2c   19,   19,   19,
262e5 20 20 20 31 39 2c 20 20 20 31 39 2c 20 20 20 31     19,   19,   1
262e6 39 2c 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20  9,. /*   140 */ 
262e7 20 20 20 31 39 2c 20 20 38 34 39 2c 20 20 20 35     19,  849,   5
262e8 39 2c 20 20 2d 33 36 2c 20 20 2d 33 36 2c 20 20  9,  -36,  -36,  
262e9 2d 33 36 2c 20 20 2d 36 32 2c 20 20 2d 36 32 2c  -36,  -62,  -62,
262ea 20 20 2d 36 32 2c 20 20 2d 31 35 2c 0a 20 2f 2a    -62,  -15,. /*
262eb 20 20 20 31 35 30 20 2a 2f 20 20 20 2d 31 35 2c     150 */   -15,
262ec 20 20 33 33 33 2c 20 20 34 35 39 2c 20 20 34 37    333,  459,  47
262ed 38 2c 20 20 35 35 37 2c 20 20 35 33 30 2c 20 20  8,  557,  530,  
262ee 35 34 31 2c 20 20 36 31 36 2c 20 20 36 30 32 2c  541,  616,  602,
262ef 20 20 37 39 33 2c 0a 20 2f 2a 20 20 20 31 36 30    793,. /*   160
262f0 20 2a 2f 20 20 20 36 30 34 2c 20 20 36 30 37 2c   */   604,  607,
262f1 20 20 36 32 36 2c 20 20 20 31 39 2c 20 20 20 31    626,   19,   1
262f2 39 2c 20 20 38 38 31 2c 20 20 20 31 39 2c 20 20  9,  881,   19,  
262f3 20 31 39 2c 20 20 39 39 34 2c 20 20 20 31 39 2c   19,  994,   19,
262f4 0a 20 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20 20  . /*   170 */   
262f5 20 31 39 2c 20 20 38 30 37 2c 20 20 20 31 39 2c   19,  807,   19,
262f6 20 20 20 31 39 2c 20 20 36 37 33 2c 20 20 38 30     19,  673,  80
262f7 37 2c 20 20 20 31 39 2c 20 20 20 31 39 2c 20 20  7,   19,   19,  
262f8 33 38 34 2c 20 20 33 38 34 2c 0a 20 2f 2a 20 20  384,  384,. /*  
262f9 20 31 38 30 20 2a 2f 20 20 20 33 38 34 2c 20 20   180 */   384,  
262fa 20 31 39 2c 20 20 20 31 39 2c 20 20 36 37 33 2c   19,   19,  673,
262fb 20 20 20 31 39 2c 20 20 20 31 39 2c 20 20 36 37     19,   19,  67
262fc 33 2c 20 20 20 31 39 2c 20 20 34 35 34 2c 20 20  3,   19,  454,  
262fd 36 38 35 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a  685,. /*   190 *
262fe 2f 20 20 20 20 31 39 2c 20 20 20 31 39 2c 20 20  /    19,   19,  
262ff 36 37 33 2c 20 20 20 31 39 2c 20 20 20 31 39 2c  673,   19,   19,
26300 20 20 20 31 39 2c 20 20 36 37 33 2c 20 20 20 31     19,  673,   1
26301 39 2c 20 20 20 31 39 2c 20 20 20 31 39 2c 0a 20  9,   19,   19,. 
26302 2f 2a 20 20 20 32 30 30 20 2a 2f 20 20 20 36 37  /*   200 */   67
26303 33 2c 20 20 36 37 33 2c 20 20 20 31 39 2c 20 20  3,  673,   19,  
26304 20 31 39 2c 20 20 20 31 39 2c 20 20 20 31 39 2c   19,   19,   19,
26305 20 20 20 31 39 2c 20 20 34 36 38 2c 20 20 38 36     19,  468,  86
26306 39 2c 20 20 39 32 31 2c 0a 20 2f 2a 20 20 20 32  9,  921,. /*   2
26307 31 30 20 2a 2f 20 20 20 31 33 32 2c 20 20 37 38  10 */   132,  78
26308 39 2c 20 20 37 38 39 2c 20 20 34 33 32 2c 20 20  9,  789,  432,  
26309 34 30 36 2c 20 20 34 30 36 2c 20 20 34 30 36 2c  406,  406,  406,
2630a 20 20 38 33 36 2c 20 20 34 30 36 2c 20 20 31 33    836,  406,  13
2630b 32 2c 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20  2,. /*   220 */ 
2630c 20 20 34 30 36 2c 20 20 31 33 32 2c 20 20 39 33    406,  132,  93
2630d 35 2c 20 20 38 33 37 2c 20 20 38 33 37 2c 20 31  5,  837,  837, 1
2630e 31 36 30 2c 20 31 31 36 30 2c 20 31 31 36 30 2c  160, 1160, 1160,
2630f 20 31 31 36 30 2c 20 31 31 35 37 2c 0a 20 2f 2a   1160, 1157,. /*
26310 20 20 20 32 33 30 20 2a 2f 20 20 20 2d 33 36 2c     230 */   -36,
26311 20 31 30 36 30 2c 20 31 30 36 35 2c 20 31 30 36   1060, 1065, 106
26312 36 2c 20 31 30 37 30 2c 20 31 30 36 37 2c 20 31  6, 1070, 1067, 1
26313 30 35 39 2c 20 31 30 37 36 2c 20 31 30 37 36 2c  059, 1076, 1076,
26314 20 31 30 39 35 2c 0a 20 2f 2a 20 20 20 32 34 30   1095,. /*   240
26315 20 2a 2f 20 20 31 30 37 39 2c 20 31 30 39 35 2c   */  1079, 1095,
26316 20 31 30 37 39 2c 20 31 30 39 37 2c 20 31 30 39   1079, 1097, 109
26317 37 2c 20 31 31 35 39 2c 20 31 30 39 37 2c 20 31  7, 1159, 1097, 1
26318 31 30 30 2c 20 31 30 39 37 2c 20 31 31 38 33 2c  100, 1097, 1183,
26319 0a 20 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20 31  . /*   250 */  1
2631a 31 31 39 2c 20 31 31 31 39 2c 20 31 31 35 39 2c  119, 1119, 1159,
2631b 20 31 30 39 37 2c 20 31 30 39 37 2c 20 31 30 39   1097, 1097, 109
2631c 37 2c 20 31 31 38 33 2c 20 31 32 30 33 2c 20 31  7, 1183, 1203, 1
2631d 30 37 36 2c 20 31 32 30 33 2c 0a 20 2f 2a 20 20  076, 1203,. /*  
2631e 20 32 36 30 20 2a 2f 20 20 31 30 37 36 2c 20 31   260 */  1076, 1
2631f 32 30 33 2c 20 31 30 37 36 2c 20 31 30 37 36 2c  203, 1076, 1076,
26320 20 31 31 39 33 2c 20 31 31 30 33 2c 20 31 32 30   1193, 1103, 120
26321 33 2c 20 31 30 37 36 2c 20 31 31 36 31 2c 20 31  3, 1076, 1161, 1
26322 31 36 31 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a  161,. /*   270 *
26323 2f 20 20 31 32 31 39 2c 20 31 30 36 30 2c 20 31  /  1219, 1060, 1
26324 30 37 36 2c 20 31 32 32 34 2c 20 31 32 32 34 2c  076, 1224, 1224,
26325 20 31 32 32 34 2c 20 31 32 32 34 2c 20 31 30 36   1224, 1224, 106
26326 30 2c 20 31 31 36 31 2c 20 31 32 31 39 2c 0a 20  0, 1161, 1219,. 
26327 2f 2a 20 20 20 32 38 30 20 2a 2f 20 20 31 30 37  /*   280 */  107
26328 36 2c 20 31 32 31 36 2c 20 31 32 31 36 2c 20 31  6, 1216, 1216, 1
26329 30 37 36 2c 20 31 30 37 36 2c 20 31 31 31 35 2c  076, 1076, 1115,
2632a 20 20 2d 36 32 2c 20 20 2d 36 32 2c 20 20 2d 36    -62,  -62,  -6
2632b 32 2c 20 20 2d 36 32 2c 0a 20 2f 2a 20 20 20 32  2,  -62,. /*   2
2632c 39 30 20 2a 2f 20 20 20 2d 36 32 2c 20 20 2d 36  90 */   -62,  -6
2632d 32 2c 20 20 35 32 35 2c 20 20 36 38 34 2c 20 20  2,  525,  684,  
2632e 37 32 37 2c 20 20 38 35 36 2c 20 20 38 35 39 2c  727,  856,  859,
2632f 20 20 35 35 36 2c 20 20 35 35 35 2c 20 20 39 38    556,  555,  98
26330 31 2c 0a 20 2f 2a 20 20 20 33 30 30 20 2a 2f 20  1,. /*   300 */ 
26331 20 20 31 30 32 2c 20 20 39 38 37 2c 20 20 39 31    102,  987,  91
26332 35 2c 20 31 30 31 36 2c 20 31 30 35 38 2c 20 31  5, 1016, 1058, 1
26333 30 37 33 2c 20 31 30 38 37 2c 20 31 30 39 31 2c  073, 1087, 1091,
26334 20 31 31 30 31 2c 20 31 31 30 34 2c 0a 20 2f 2a   1101, 1104,. /*
26335 20 20 20 33 31 30 20 2a 2f 20 20 20 38 39 32 2c     310 */   892,
26336 20 31 31 30 38 2c 20 31 30 32 39 2c 20 31 32 35   1108, 1029, 125
26337 33 2c 20 31 32 33 35 2c 20 31 31 34 36 2c 20 31  3, 1235, 1146, 1
26338 31 34 37 2c 20 31 31 35 31 2c 20 31 31 35 32 2c  147, 1151, 1152,
26339 20 31 31 37 33 2c 0a 20 2f 2a 20 20 20 33 32 30   1173,. /*   320
2633a 20 2a 2f 20 20 31 31 36 32 2c 20 31 32 34 37 2c   */  1162, 1247,
2633b 20 31 32 34 38 2c 20 31 32 35 30 2c 20 31 32 35   1248, 1250, 125
2633c 32 2c 20 31 32 35 36 2c 20 31 32 35 34 2c 20 31  2, 1256, 1254, 1
2633d 32 35 35 2c 20 31 32 35 37 2c 20 31 32 35 38 2c  255, 1257, 1258,
2633e 0a 20 2f 2a 20 20 20 33 33 30 20 2a 2f 20 20 31  . /*   330 */  1
2633f 32 36 30 2c 20 31 32 35 39 2c 20 31 31 36 35 2c  260, 1259, 1165,
26340 20 31 32 36 31 2c 20 31 31 36 37 2c 20 31 32 35   1261, 1167, 125
26341 39 2c 20 31 31 36 33 2c 20 31 32 36 38 2c 20 31  9, 1163, 1268, 1
26342 32 36 39 2c 20 31 31 36 34 2c 0a 20 2f 2a 20 20  269, 1164,. /*  
26343 20 33 34 30 20 2a 2f 20 20 31 32 37 31 2c 20 31   340 */  1271, 1
26344 32 37 32 2c 20 31 32 34 32 2c 20 31 32 34 34 2c  272, 1242, 1244,
26345 20 31 32 37 35 2c 20 31 32 35 31 2c 20 31 32 37   1275, 1251, 127
26346 37 2c 20 31 32 37 38 2c 20 31 32 38 30 2c 20 31  7, 1278, 1280, 1
26347 32 38 31 2c 0a 20 2f 2a 20 20 20 33 35 30 20 2a  281,. /*   350 *
26348 2f 20 20 31 32 36 32 2c 20 31 32 38 32 2c 20 31  /  1262, 1282, 1
26349 32 30 36 2c 20 31 32 30 30 2c 20 31 32 38 38 2c  206, 1200, 1288,
2634a 20 31 32 39 30 2c 20 31 32 38 34 2c 20 31 32 31   1290, 1284, 121
2634b 30 2c 20 31 32 37 33 2c 20 31 32 36 33 2c 0a 20  0, 1273, 1263,. 
2634c 2f 2a 20 20 20 33 36 30 20 2a 2f 20 20 31 32 36  /*   360 */  126
2634d 36 2c 20 31 32 39 31 2c 20 31 32 36 37 2c 20 31  6, 1291, 1267, 1
2634e 31 38 32 2c 20 31 32 31 38 2c 20 31 32 39 38 2c  182, 1218, 1298,
2634f 20 31 33 31 33 2c 20 31 33 31 38 2c 20 31 32 32   1313, 1318, 122
26350 30 2c 20 31 32 36 34 2c 0a 20 2f 2a 20 20 20 33  0, 1264,. /*   3
26351 37 30 20 2a 2f 20 20 31 32 36 35 2c 20 31 31 39  70 */  1265, 119
26352 38 2c 20 31 33 30 32 2c 20 31 32 31 31 2c 20 31  8, 1302, 1211, 1
26353 33 30 39 2c 20 31 32 31 32 2c 20 31 33 30 38 2c  309, 1212, 1308,
26354 20 31 32 31 33 2c 20 31 32 32 36 2c 20 31 32 30   1213, 1226, 120
26355 37 2c 0a 20 2f 2a 20 20 20 33 38 30 20 2a 2f 20  7,. /*   380 */ 
26356 20 31 33 31 31 2c 20 31 32 30 38 2c 20 31 33 31   1311, 1208, 131
26357 34 2c 20 31 33 32 31 2c 20 31 32 37 39 2c 20 31  4, 1321, 1279, 1
26358 32 30 32 2c 20 31 32 30 34 2c 20 31 33 31 39 2c  202, 1204, 1319,
26359 20 31 33 33 36 2c 20 31 33 33 37 2c 0a 7d 3b 0a   1336, 1337,.};.
2635a 23 64 65 66 69 6e 65 20 59 59 5f 52 45 44 55 43  #define YY_REDUC
2635b 45 5f 55 53 45 5f 44 46 4c 54 20 28 2d 31 36 35  E_USE_DFLT (-165
2635c 29 0a 23 64 65 66 69 6e 65 20 59 59 5f 52 45 44  ).#define YY_RED
2635d 55 43 45 5f 4d 41 58 20 32 39 31 0a 73 74 61 74  UCE_MAX 291.stat
2635e 69 63 20 63 6f 6e 73 74 20 73 68 6f 72 74 20 79  ic const short y
2635f 79 5f 72 65 64 75 63 65 5f 6f 66 73 74 5b 5d 20  y_reduce_ofst[] 
26360 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a 2f  = {. /*     0 */
26361 20 20 2d 31 33 38 2c 20 20 32 37 37 2c 20 20 35    -138,  277,  5
26362 34 36 2c 20 20 31 33 37 2c 20 20 34 30 31 2c 20  46,  137,  401, 
26363 20 2d 32 31 2c 20 20 20 34 34 2c 20 20 20 33 36   -21,   44,   36
26364 2c 20 20 20 33 38 2c 20 20 32 34 32 2c 0a 20 2f  ,   38,  242,. /
26365 2a 20 20 20 20 31 30 20 2a 2f 20 20 2d 31 34 31  *    10 */  -141
26366 2c 20 20 31 39 31 2c 20 20 20 39 31 2c 20 20 32  ,  191,   91,  2
26367 36 39 2c 20 20 33 34 33 2c 20 20 33 34 35 2c 20  69,  343,  345, 
26368 2d 31 32 36 2c 20 20 35 38 39 2c 20 20 33 33 38  -126,  589,  338
26369 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20 20 32  ,  150,. /*    2
2636a 30 20 2a 2f 20 20 20 31 34 37 2c 20 20 2d 31 33  0 */   147,  -13
2636b 2c 20 20 32 31 33 2c 20 20 34 31 32 2c 20 20 34  ,  213,  412,  4
2636c 31 32 2c 20 20 34 31 32 2c 20 20 34 31 32 2c 20  12,  412,  412, 
2636d 20 34 31 32 2c 20 20 34 31 32 2c 20 20 34 31 32   412,  412,  412
2636e 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20 20  ,. /*    30 */  
2636f 20 34 31 32 2c 20 20 34 31 32 2c 20 20 34 31 32   412,  412,  412
26370 2c 20 20 34 31 32 2c 20 20 34 31 32 2c 20 20 34  ,  412,  412,  4
26371 31 32 2c 20 20 34 31 32 2c 20 20 34 31 32 2c 20  12,  412,  412, 
26372 20 34 31 32 2c 20 20 34 31 32 2c 0a 20 2f 2a 20   412,  412,. /* 
26373 20 20 20 34 30 20 2a 2f 20 20 20 34 31 32 2c 20     40 */   412, 
26374 20 34 31 32 2c 20 20 34 31 32 2c 20 20 34 31 32   412,  412,  412
26375 2c 20 20 34 31 32 2c 20 20 34 31 32 2c 20 20 34  ,  412,  412,  4
26376 31 32 2c 20 20 34 31 32 2c 20 20 34 31 32 2c 20  12,  412,  412, 
26377 20 34 31 32 2c 0a 20 2f 2a 20 20 20 20 35 30 20   412,. /*    50 
26378 2a 2f 20 20 20 34 31 32 2c 20 20 34 31 32 2c 20  */   412,  412, 
26379 20 34 31 32 2c 20 20 34 31 32 2c 20 20 34 31 32   412,  412,  412
2637a 2c 20 20 32 31 31 2c 20 20 36 39 38 2c 20 20 37  ,  211,  698,  7
2637b 31 34 2c 20 20 37 31 36 2c 20 20 37 32 32 2c 0a  14,  716,  722,.
2637c 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20 37   /*    60 */   7
2637d 32 34 2c 20 20 37 32 38 2c 20 20 37 34 38 2c 20  24,  728,  748, 
2637e 20 37 35 33 2c 20 20 37 35 35 2c 20 20 37 35 37   753,  755,  757
2637f 2c 20 20 37 36 32 2c 20 20 37 36 39 2c 20 20 37  ,  762,  769,  7
26380 39 34 2c 20 20 38 30 35 2c 0a 20 2f 2a 20 20 20  94,  805,. /*   
26381 20 37 30 20 2a 2f 20 20 20 38 30 38 2c 20 20 38   70 */   808,  8
26382 34 36 2c 20 20 38 35 35 2c 20 20 38 35 38 2c 20  46,  855,  858, 
26383 20 38 36 31 2c 20 20 38 37 35 2c 20 20 38 38 32   861,  875,  882
26384 2c 20 20 38 39 33 2c 20 20 38 39 36 2c 20 20 38  ,  893,  896,  8
26385 39 39 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a 2f  99,. /*    80 */
26386 20 20 20 39 30 31 2c 20 20 39 30 33 2c 20 20 39     901,  903,  9
26387 30 36 2c 20 20 39 32 30 2c 20 20 39 33 30 2c 20  06,  920,  930, 
26388 20 39 33 32 2c 20 20 39 33 34 2c 20 20 39 33 36   932,  934,  936
26389 2c 20 20 39 34 35 2c 20 20 39 34 37 2c 0a 20 2f  ,  945,  947,. /
2638a 2a 20 20 20 20 39 30 20 2a 2f 20 20 20 39 36 33  *    90 */   963
2638b 2c 20 20 39 36 35 2c 20 20 39 37 31 2c 20 20 39  ,  965,  971,  9
2638c 37 35 2c 20 20 39 37 38 2c 20 20 39 38 30 2c 20  75,  978,  980, 
2638d 20 34 31 32 2c 20 20 34 31 32 2c 20 20 34 31 32   412,  412,  412
2638e 2c 20 20 34 31 32 2c 0a 20 2f 2a 20 20 20 31 30  ,  412,. /*   10
2638f 30 20 2a 2f 20 20 20 20 32 30 2c 20 20 34 31 32  0 */    20,  412
26390 2c 20 20 34 31 32 2c 20 20 20 32 33 2c 20 20 20  ,  412,   23,   
26391 33 34 2c 20 20 33 33 34 2c 20 20 34 37 35 2c 20  34,  334,  475, 
26392 20 35 35 32 2c 20 20 35 39 33 2c 20 20 35 39 34   552,  593,  594
26393 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20  ,. /*   110 */  
26394 20 35 38 35 2c 20 20 32 31 32 2c 20 20 34 31 32   585,  212,  412
26395 2c 20 20 32 38 39 2c 20 20 34 31 32 2c 20 20 34  ,  289,  412,  4
26396 31 32 2c 20 20 34 31 32 2c 20 20 34 31 32 2c 20  12,  412,  412, 
26397 20 31 33 35 2c 20 2d 31 36 34 2c 0a 20 2f 2a 20   135, -164,. /* 
26398 20 20 31 32 30 20 2a 2f 20 20 2d 31 31 35 2c 20    120 */  -115, 
26399 20 31 36 34 2c 20 20 34 30 37 2c 20 20 34 30 37   164,  407,  407
2639a 2c 20 20 33 35 30 2c 20 20 31 34 31 2c 20 20 20  ,  350,  141,   
2639b 35 31 2c 20 20 31 36 33 2c 20 20 35 39 36 2c 20  51,  163,  596, 
2639c 20 2d 39 30 2c 0a 20 2f 2a 20 20 20 31 33 30 20   -90,. /*   130 
2639d 2a 2f 20 20 20 34 33 36 2c 20 20 32 31 38 2c 20  */   436,  218, 
2639e 20 37 36 35 2c 20 20 34 33 38 2c 20 20 35 38 36   765,  438,  586
2639f 2c 20 20 35 39 32 2c 20 20 35 39 35 2c 20 20 37  ,  592,  595,  7
263a0 31 35 2c 20 20 37 31 38 2c 20 20 34 30 38 2c 0a  15,  718,  408,.
263a1 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20 37   /*   140 */   7
263a2 35 34 2c 20 20 33 38 30 2c 20 20 36 33 34 2c 20  54,  380,  634, 
263a3 20 36 37 37 2c 20 20 37 39 38 2c 20 20 38 30 31   677,  798,  801
263a4 2c 20 20 31 34 34 2c 20 20 35 32 39 2c 20 20 35  ,  144,  529,  5
263a5 38 38 2c 20 20 20 34 39 2c 0a 20 2f 2a 20 20 20  88,   49,. /*   
263a6 31 35 30 20 2a 2f 20 20 20 31 37 36 2c 20 20 32  150 */   176,  2
263a7 34 34 2c 20 20 32 36 34 2c 20 20 33 32 39 2c 20  44,  264,  329, 
263a8 20 34 35 37 2c 20 20 33 32 39 2c 20 20 33 32 39   457,  329,  329
263a9 2c 20 20 34 35 31 2c 20 20 34 37 37 2c 20 20 34  ,  451,  477,  4
263aa 39 34 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a 2f  94,. /*   160 */
263ab 20 20 20 35 30 37 2c 20 20 35 30 39 2c 20 20 35     507,  509,  5
263ac 32 38 2c 20 20 35 39 30 2c 20 20 37 33 30 2c 20  28,  590,  730, 
263ad 20 36 34 32 2c 20 20 35 30 39 2c 20 20 37 34 33   642,  509,  743
263ae 2c 20 20 38 33 39 2c 20 20 38 36 34 2c 0a 20 2f  ,  839,  864,. /
263af 2a 20 20 20 31 37 30 20 2a 2f 20 20 20 38 37 39  *   170 */   879
263b0 2c 20 20 38 33 34 2c 20 20 38 39 34 2c 20 20 39  ,  834,  894,  9
263b1 30 30 2c 20 20 33 32 39 2c 20 20 38 33 34 2c 20  00,  329,  834, 
263b2 20 39 30 37 2c 20 20 39 31 34 2c 20 20 38 32 36   907,  914,  826
263b3 2c 20 20 38 38 36 2c 0a 20 2f 2a 20 20 20 31 38  ,  886,. /*   18
263b4 30 20 2a 2f 20 20 20 39 31 39 2c 20 20 39 32 37  0 */   919,  927
263b5 2c 20 20 39 33 37 2c 20 20 33 32 39 2c 20 20 39  ,  937,  329,  9
263b6 35 31 2c 20 20 39 36 31 2c 20 20 33 32 39 2c 20  51,  961,  329, 
263b7 20 39 37 32 2c 20 20 38 39 37 2c 20 20 38 39 38   972,  897,  898
263b8 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20 20  ,. /*   190 */  
263b9 20 39 38 39 2c 20 20 39 39 30 2c 20 20 33 32 39   989,  990,  329
263ba 2c 20 20 39 39 31 2c 20 20 39 39 32 2c 20 20 39  ,  991,  992,  9
263bb 39 35 2c 20 20 33 32 39 2c 20 20 39 39 36 2c 20  95,  329,  996, 
263bc 20 39 39 39 2c 20 31 30 30 34 2c 0a 20 2f 2a 20   999, 1004,. /* 
263bd 20 20 32 30 30 20 2a 2f 20 20 20 33 32 39 2c 20    200 */   329, 
263be 20 33 32 39 2c 20 31 30 30 35 2c 20 31 30 30 36   329, 1005, 1006
263bf 2c 20 31 30 30 37 2c 20 31 30 30 38 2c 20 31 30  , 1007, 1008, 10
263c0 30 39 2c 20 31 30 31 30 2c 20 20 39 36 36 2c 20  09, 1010,  966, 
263c1 20 39 36 37 2c 0a 20 2f 2a 20 20 20 32 31 30 20   967,. /*   210 
263c2 2a 2f 20 20 20 39 39 37 2c 20 20 39 33 33 2c 20  */   997,  933, 
263c3 20 39 33 38 2c 20 20 39 34 30 2c 20 20 39 39 33   938,  940,  993
263c4 2c 20 20 39 39 38 2c 20 31 30 30 30 2c 20 20 39  ,  998, 1000,  9
263c5 38 34 2c 20 31 30 30 31 2c 20 31 30 30 33 2c 0a  84, 1001, 1003,.
263c6 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 31 30   /*   220 */  10
263c7 30 32 2c 20 31 30 31 34 2c 20 31 30 31 31 2c 20  02, 1014, 1011, 
263c8 20 39 37 34 2c 20 20 39 37 37 2c 20 31 30 32 33   974,  977, 1023
263c9 2c 20 31 30 33 30 2c 20 31 30 33 31 2c 20 31 30  , 1030, 1031, 10
263ca 33 32 2c 20 31 30 32 36 2c 0a 20 2f 2a 20 20 20  32, 1026,. /*   
263cb 32 33 30 20 2a 2f 20 20 31 30 31 32 2c 20 20 39  230 */  1012,  9
263cc 38 38 2c 20 31 30 31 33 2c 20 31 30 31 35 2c 20  88, 1013, 1015, 
263cd 31 30 31 37 2c 20 31 30 31 38 2c 20 20 39 36 38  1017, 1018,  968
263ce 2c 20 31 30 33 39 2c 20 31 30 34 30 2c 20 31 30  , 1039, 1040, 10
263cf 31 39 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a 2f  19,. /*   240 */
263d0 20 20 31 30 32 30 2c 20 31 30 32 32 2c 20 31 30    1020, 1022, 10
263d1 32 34 2c 20 31 30 32 35 2c 20 31 30 32 37 2c 20  24, 1025, 1027, 
263d2 31 30 32 31 2c 20 31 30 33 33 2c 20 31 30 33 34  1021, 1033, 1034
263d3 2c 20 31 30 33 35 2c 20 31 30 33 36 2c 0a 20 2f  , 1035, 1036,. /
263d4 2a 20 20 20 32 35 30 20 2a 2f 20 20 20 39 37 39  *   250 */   979
263d5 2c 20 20 39 38 33 2c 20 31 30 33 38 2c 20 31 30  ,  983, 1038, 10
263d6 34 31 2c 20 31 30 34 32 2c 20 31 30 34 34 2c 20  41, 1042, 1044, 
263d7 31 30 34 35 2c 20 31 30 36 39 2c 20 31 30 37 32  1045, 1069, 1072
263d8 2c 20 31 30 37 31 2c 0a 20 2f 2a 20 20 20 32 36  , 1071,. /*   26
263d9 30 20 2a 2f 20 20 31 30 37 35 2c 20 31 30 37 37  0 */  1075, 1077
263da 2c 20 31 30 37 38 2c 20 31 30 38 30 2c 20 31 30  , 1078, 1080, 10
263db 32 38 2c 20 31 30 33 37 2c 20 31 30 38 33 2c 20  28, 1037, 1083, 
263dc 31 30 38 34 2c 20 31 30 35 31 2c 20 31 30 35 33  1084, 1051, 1053
263dd 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20  ,. /*   270 */  
263de 31 30 34 33 2c 20 31 30 34 36 2c 20 31 30 38 39  1043, 1046, 1089
263df 2c 20 31 30 35 35 2c 20 31 30 35 36 2c 20 31 30  , 1055, 1056, 10
263e0 35 37 2c 20 31 30 36 31 2c 20 31 30 35 32 2c 20  57, 1061, 1052, 
263e1 31 30 36 33 2c 20 31 30 34 37 2c 0a 20 2f 2a 20  1063, 1047,. /* 
263e2 20 20 32 38 30 20 2a 2f 20 20 31 30 39 33 2c 20    280 */  1093, 
263e3 31 30 34 38 2c 20 31 30 34 39 2c 20 31 30 39 38  1048, 1049, 1098
263e4 2c 20 31 30 39 39 2c 20 31 30 35 30 2c 20 31 30  , 1099, 1050, 10
263e5 39 34 2c 20 31 30 38 31 2c 20 31 30 38 35 2c 20  94, 1081, 1085, 
263e6 31 30 36 32 2c 0a 20 2f 2a 20 20 20 32 39 30 20  1062,. /*   290 
263e7 2a 2f 20 20 31 30 35 34 2c 20 31 30 36 34 2c 0a  */  1054, 1064,.
263e8 7d 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  };.static const 
263e9 59 59 41 43 54 49 4f 4e 54 59 50 45 20 79 79 5f  YYACTIONTYPE yy_
263ea 64 65 66 61 75 6c 74 5b 5d 20 3d 20 7b 0a 20 2f  default[] = {. /
263eb 2a 20 20 20 20 20 30 20 2a 2f 20 20 20 35 39 35  *     0 */   595
263ec 2c 20 20 38 32 30 2c 20 20 39 30 32 2c 20 20 37  ,  820,  902,  7
263ed 31 30 2c 20 20 39 30 32 2c 20 20 38 32 30 2c 20  10,  902,  820, 
263ee 20 39 30 32 2c 20 20 39 30 32 2c 20 20 38 34 38   902,  902,  848
263ef 2c 20 20 37 31 34 2c 0a 20 2f 2a 20 20 20 20 31  ,  714,. /*    1
263f0 30 20 2a 2f 20 20 20 38 37 37 2c 20 20 38 31 38  0 */   877,  818
263f1 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39  ,  902,  902,  9
263f2 30 32 2c 20 20 39 30 32 2c 20 20 37 39 32 2c 20  02,  902,  792, 
263f3 20 39 30 32 2c 20 20 38 34 38 2c 20 20 39 30 32   902,  848,  902
263f4 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20  ,. /*    20 */  
263f5 20 36 32 36 2c 20 20 38 34 38 2c 20 20 38 34 38   626,  848,  848
263f6 2c 20 20 37 34 33 2c 20 20 39 30 32 2c 20 20 39  ,  743,  902,  9
263f7 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20  02,  902,  902, 
263f8 20 39 30 32 2c 20 20 39 30 32 2c 0a 20 2f 2a 20   902,  902,. /* 
263f9 20 20 20 33 30 20 2a 2f 20 20 20 39 30 32 2c 20     30 */   902, 
263fa 20 39 30 32 2c 20 20 37 34 34 2c 20 20 39 30 32   902,  744,  902
263fb 2c 20 20 38 32 32 2c 20 20 38 31 37 2c 20 20 38  ,  822,  817,  8
263fc 31 33 2c 20 20 38 31 35 2c 20 20 38 31 34 2c 20  13,  815,  814, 
263fd 20 38 32 31 2c 0a 20 2f 2a 20 20 20 20 34 30 20   821,. /*    40 
263fe 2a 2f 20 20 20 37 34 35 2c 20 20 37 33 34 2c 20  */   745,  734, 
263ff 20 37 34 31 2c 20 20 37 34 38 2c 20 20 37 32 36   741,  748,  726
26400 2c 20 20 38 36 31 2c 20 20 37 35 30 2c 20 20 37  ,  861,  750,  7
26401 35 31 2c 20 20 37 35 37 2c 20 20 37 35 38 2c 0a  51,  757,  758,.
26402 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20 20 38   /*    50 */   8
26403 37 38 2c 20 20 38 37 36 2c 20 20 37 38 30 2c 20  78,  876,  780, 
26404 20 37 37 39 2c 20 20 37 39 38 2c 20 20 39 30 32   779,  798,  902
26405 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39  ,  902,  902,  9
26406 30 32 2c 20 20 39 30 32 2c 0a 20 2f 2a 20 20 20  02,  902,. /*   
26407 20 36 30 20 2a 2f 20 20 20 39 30 32 2c 20 20 39   60 */   902,  9
26408 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20  02,  902,  902, 
26409 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32   902,  902,  902
2640a 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39  ,  902,  902,  9
2640b 30 32 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f  02,. /*    70 */
2640c 20 20 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39     902,  902,  9
2640d 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20  02,  902,  902, 
2640e 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32   902,  902,  902
2640f 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 0a 20 2f  ,  902,  902,. /
26410 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 39 30 32  *    80 */   902
26411 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39  ,  902,  902,  9
26412 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20  02,  902,  902, 
26413 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32   902,  902,  902
26414 2c 20 20 39 30 32 2c 0a 20 2f 2a 20 20 20 20 39  ,  902,. /*    9
26415 30 20 2a 2f 20 20 20 39 30 32 2c 20 20 39 30 32  0 */   902,  902
26416 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39  ,  902,  902,  9
26417 30 32 2c 20 20 39 30 32 2c 20 20 37 38 32 2c 20  02,  902,  782, 
26418 20 38 30 34 2c 20 20 37 38 31 2c 20 20 37 39 31   804,  781,  791
26419 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20  ,. /*   100 */  
2641a 20 36 31 39 2c 20 20 37 38 33 2c 20 20 37 38 34   619,  783,  784
2641b 2c 20 20 36 37 39 2c 20 20 36 31 34 2c 20 20 39  ,  679,  614,  9
2641c 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20  02,  902,  902, 
2641d 20 39 30 32 2c 20 20 39 30 32 2c 0a 20 2f 2a 20   902,  902,. /* 
2641e 20 20 31 31 30 20 2a 2f 20 20 20 39 30 32 2c 20    110 */   902, 
2641f 20 39 30 32 2c 20 20 37 38 35 2c 20 20 39 30 32   902,  785,  902
26420 2c 20 20 37 38 36 2c 20 20 37 39 39 2c 20 20 38  ,  786,  799,  8
26421 30 30 2c 20 20 38 30 31 2c 20 20 39 30 32 2c 20  00,  801,  902, 
26422 20 39 30 32 2c 0a 20 2f 2a 20 20 20 31 32 30 20   902,. /*   120 
26423 2a 2f 20 20 20 39 30 32 2c 20 20 39 30 32 2c 20  */   902,  902, 
26424 20 39 30 32 2c 20 20 39 30 32 2c 20 20 35 39 35   902,  902,  595
26425 2c 20 20 37 31 30 2c 20 20 39 30 32 2c 20 20 37  ,  710,  902,  7
26426 31 30 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 0a  10,  902,  902,.
26427 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20 20 39   /*   130 */   9
26428 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20  02,  902,  902, 
26429 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32   902,  902,  902
2642a 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39  ,  902,  902,  9
2642b 30 32 2c 20 20 39 30 32 2c 0a 20 2f 2a 20 20 20  02,  902,. /*   
2642c 31 34 30 20 2a 2f 20 20 20 39 30 32 2c 20 20 39  140 */   902,  9
2642d 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20  02,  902,  902, 
2642e 20 39 30 32 2c 20 20 39 30 32 2c 20 20 37 30 34   902,  902,  704
2642f 2c 20 20 37 31 34 2c 20 20 38 39 35 2c 20 20 39  ,  714,  895,  9
26430 30 32 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f  02,. /*   150 */
26431 20 20 20 39 30 32 2c 20 20 36 37 30 2c 20 20 39     902,  670,  9
26432 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20  02,  902,  902, 
26433 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32   902,  902,  902
26434 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 0a 20 2f  ,  902,  902,. /
26435 2a 20 20 20 31 36 30 20 2a 2f 20 20 20 39 30 32  *   160 */   902
26436 2c 20 20 39 30 32 2c 20 20 36 30 32 2c 20 20 36  ,  902,  602,  6
26437 30 30 2c 20 20 39 30 32 2c 20 20 37 30 32 2c 20  00,  902,  702, 
26438 20 39 30 32 2c 20 20 39 30 32 2c 20 20 36 32 38   902,  902,  628
26439 2c 20 20 39 30 32 2c 0a 20 2f 2a 20 20 20 31 37  ,  902,. /*   17
2643a 30 20 2a 2f 20 20 20 39 30 32 2c 20 20 37 31 32  0 */   902,  712
2643b 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20 20 37  ,  902,  902,  7
2643c 31 37 2c 20 20 37 31 38 2c 20 20 39 30 32 2c 20  17,  718,  902, 
2643d 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32   902,  902,  902
2643e 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20  ,. /*   180 */  
2643f 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32   902,  902,  902
26440 2c 20 20 36 31 36 2c 20 20 39 30 32 2c 20 20 39  ,  616,  902,  9
26441 30 32 2c 20 20 36 39 31 2c 20 20 39 30 32 2c 20  02,  691,  902, 
26442 20 38 35 34 2c 20 20 39 30 32 2c 0a 20 2f 2a 20   854,  902,. /* 
26443 20 20 31 39 30 20 2a 2f 20 20 20 39 30 32 2c 20    190 */   902, 
26444 20 39 30 32 2c 20 20 38 36 38 2c 20 20 39 30 32   902,  868,  902
26445 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20 20 38  ,  902,  902,  8
26446 36 36 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20  66,  902,  902, 
26447 20 39 30 32 2c 0a 20 2f 2a 20 20 20 32 30 30 20   902,. /*   200 
26448 2a 2f 20 20 20 36 39 33 2c 20 20 37 35 33 2c 20  */   693,  753, 
26449 20 38 33 34 2c 20 20 39 30 32 2c 20 20 38 38 31   834,  902,  881
2644a 2c 20 20 38 38 33 2c 20 20 39 30 32 2c 20 20 39  ,  883,  902,  9
2644b 30 32 2c 20 20 37 30 32 2c 20 20 37 31 31 2c 0a  02,  702,  711,.
2644c 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20 20 39   /*   210 */   9
2644d 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20  02,  902,  902, 
2644e 20 38 31 36 2c 20 20 37 33 37 2c 20 20 37 33 37   816,  737,  737
2644f 2c 20 20 37 33 37 2c 20 20 36 34 39 2c 20 20 37  ,  737,  649,  7
26450 33 37 2c 20 20 39 30 32 2c 0a 20 2f 2a 20 20 20  37,  902,. /*   
26451 32 32 30 20 2a 2f 20 20 20 37 33 37 2c 20 20 39  220 */   737,  9
26452 30 32 2c 20 20 36 35 32 2c 20 20 37 34 37 2c 20  02,  652,  747, 
26453 20 37 34 37 2c 20 20 35 39 39 2c 20 20 35 39 39   747,  599,  599
26454 2c 20 20 35 39 39 2c 20 20 35 39 39 2c 20 20 36  ,  599,  599,  6
26455 36 39 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f  69,. /*   230 */
26456 20 20 20 39 30 32 2c 20 20 37 34 37 2c 20 20 37     902,  747,  7
26457 33 38 2c 20 20 37 34 30 2c 20 20 37 33 30 2c 20  38,  740,  730, 
26458 20 37 34 32 2c 20 20 39 30 32 2c 20 20 37 31 39   742,  902,  719
26459 2c 20 20 37 31 39 2c 20 20 37 32 37 2c 0a 20 2f  ,  719,  727,. /
2645a 2a 20 20 20 32 34 30 20 2a 2f 20 20 20 37 32 39  *   240 */   729
2645b 2c 20 20 37 32 37 2c 20 20 37 32 39 2c 20 20 36  ,  727,  729,  6
2645c 38 31 2c 20 20 36 38 31 2c 20 20 36 36 36 2c 20  81,  681,  666, 
2645d 20 36 38 31 2c 20 20 36 35 32 2c 20 20 36 38 31   681,  652,  681
2645e 2c 20 20 38 32 36 2c 0a 20 2f 2a 20 20 20 32 35  ,  826,. /*   25
2645f 30 20 2a 2f 20 20 20 38 33 31 2c 20 20 38 33 31  0 */   831,  831
26460 2c 20 20 36 36 36 2c 20 20 36 38 31 2c 20 20 36  ,  666,  681,  6
26461 38 31 2c 20 20 36 38 31 2c 20 20 38 32 36 2c 20  81,  681,  826, 
26462 20 36 31 31 2c 20 20 37 31 39 2c 20 20 36 31 31   611,  719,  611
26463 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20  ,. /*   260 */  
26464 20 37 31 39 2c 20 20 36 31 31 2c 20 20 37 31 39   719,  611,  719
26465 2c 20 20 37 31 39 2c 20 20 38 35 38 2c 20 20 38  ,  719,  858,  8
26466 36 30 2c 20 20 36 31 31 2c 20 20 37 31 39 2c 20  60,  611,  719, 
26467 20 36 38 33 2c 20 20 36 38 33 2c 0a 20 2f 2a 20   683,  683,. /* 
26468 20 20 32 37 30 20 2a 2f 20 20 20 37 35 39 2c 20    270 */   759, 
26469 20 37 34 37 2c 20 20 37 31 39 2c 20 20 36 39 30   747,  719,  690
2646a 2c 20 20 36 39 30 2c 20 20 36 39 30 2c 20 20 36  ,  690,  690,  6
2646b 39 30 2c 20 20 37 34 37 2c 20 20 36 38 33 2c 20  90,  747,  683, 
2646c 20 37 35 39 2c 0a 20 2f 2a 20 20 20 32 38 30 20   759,. /*   280 
2646d 2a 2f 20 20 20 37 31 39 2c 20 20 38 38 30 2c 20  */   719,  880, 
2646e 20 38 38 30 2c 20 20 37 31 39 2c 20 20 37 31 39   880,  719,  719
2646f 2c 20 20 38 38 38 2c 20 20 36 33 36 2c 20 20 36  ,  888,  636,  6
26470 35 34 2c 20 20 36 35 34 2c 20 20 38 36 33 2c 0a  54,  654,  863,.
26471 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20 20 38   /*   290 */   8
26472 39 35 2c 20 20 39 30 30 2c 20 20 39 30 32 2c 20  95,  900,  902, 
26473 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32   902,  902,  902
26474 2c 20 20 37 36 36 2c 20 20 39 30 32 2c 20 20 39  ,  766,  902,  9
26475 30 32 2c 20 20 39 30 32 2c 0a 20 2f 2a 20 20 20  02,  902,. /*   
26476 33 30 30 20 2a 2f 20 20 20 39 30 32 2c 20 20 39  300 */   902,  9
26477 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20  02,  902,  902, 
26478 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32   902,  902,  902
26479 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39  ,  902,  902,  9
2647a 30 32 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f  02,. /*   310 */
2647b 20 20 20 38 34 31 2c 20 20 39 30 32 2c 20 20 39     841,  902,  9
2647c 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20  02,  902,  902, 
2647d 20 37 37 31 2c 20 20 37 36 37 2c 20 20 39 30 32   771,  767,  902
2647e 2c 20 20 37 36 38 2c 20 20 39 30 32 2c 0a 20 2f  ,  768,  902,. /
2647f 2a 20 20 20 33 32 30 20 2a 2f 20 20 20 36 39 36  *   320 */   696
26480 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39  ,  902,  902,  9
26481 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20  02,  902,  902, 
26482 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32   902,  902,  902
26483 2c 20 20 39 30 32 2c 0a 20 2f 2a 20 20 20 33 33  ,  902,. /*   33
26484 30 20 2a 2f 20 20 20 39 30 32 2c 20 20 38 31 39  0 */   902,  819
26485 2c 20 20 39 30 32 2c 20 20 37 33 31 2c 20 20 39  ,  902,  731,  9
26486 30 32 2c 20 20 37 33 39 2c 20 20 39 30 32 2c 20  02,  739,  902, 
26487 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32   902,  902,  902
26488 2c 0a 20 2f 2a 20 20 20 33 34 30 20 2a 2f 20 20  ,. /*   340 */  
26489 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32   902,  902,  902
2648a 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39  ,  902,  902,  9
2648b 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20  02,  902,  902, 
2648c 20 39 30 32 2c 20 20 39 30 32 2c 0a 20 2f 2a 20   902,  902,. /* 
2648d 20 20 33 35 30 20 2a 2f 20 20 20 39 30 32 2c 20    350 */   902, 
2648e 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32   902,  902,  902
2648f 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39  ,  902,  902,  9
26490 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20  02,  902,  902, 
26491 20 39 30 32 2c 0a 20 2f 2a 20 20 20 33 36 30 20   902,. /*   360 
26492 2a 2f 20 20 20 38 35 36 2c 20 20 38 35 37 2c 20  */   856,  857, 
26493 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32   902,  902,  902
26494 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39  ,  902,  902,  9
26495 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 0a  02,  902,  902,.
26496 20 2f 2a 20 20 20 33 37 30 20 2a 2f 20 20 20 39   /*   370 */   9
26497 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20  02,  902,  902, 
26498 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32   902,  902,  902
26499 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39  ,  902,  902,  9
2649a 30 32 2c 20 20 39 30 32 2c 0a 20 2f 2a 20 20 20  02,  902,. /*   
2649b 33 38 30 20 2a 2f 20 20 20 39 30 32 2c 20 20 39  380 */   902,  9
2649c 30 32 2c 20 20 39 30 32 2c 20 20 39 30 32 2c 20  02,  902,  902, 
2649d 20 38 38 37 2c 20 20 39 30 32 2c 20 20 39 30 32   887,  902,  902
2649e 2c 20 20 38 39 30 2c 20 20 35 39 36 2c 20 20 39  ,  890,  596,  9
2649f 30 32 2c 0a 20 2f 2a 20 20 20 33 39 30 20 2a 2f  02,. /*   390 */
264a0 20 20 20 35 39 30 2c 20 20 35 39 33 2c 20 20 35     590,  593,  5
264a1 39 32 2c 20 20 35 39 34 2c 20 20 35 39 38 2c 20  92,  594,  598, 
264a2 20 36 30 31 2c 20 20 36 32 33 2c 20 20 36 32 34   601,  623,  624
264a3 2c 20 20 36 32 35 2c 20 20 36 30 33 2c 0a 20 2f  ,  625,  603,. /
264a4 2a 20 20 20 34 30 30 20 2a 2f 20 20 20 36 30 34  *   400 */   604
264a5 2c 20 20 36 30 35 2c 20 20 36 30 36 2c 20 20 36  ,  605,  606,  6
264a6 30 37 2c 20 20 36 30 38 2c 20 20 36 30 39 2c 20  07,  608,  609, 
264a7 20 36 31 35 2c 20 20 36 31 37 2c 20 20 36 33 35   615,  617,  635
264a8 2c 20 20 36 33 37 2c 0a 20 2f 2a 20 20 20 34 31  ,  637,. /*   41
264a9 30 20 2a 2f 20 20 20 36 32 31 2c 20 20 36 33 39  0 */   621,  639
264aa 2c 20 20 37 30 30 2c 20 20 37 30 31 2c 20 20 37  ,  700,  701,  7
264ab 36 33 2c 20 20 36 39 34 2c 20 20 36 39 35 2c 20  63,  694,  695, 
264ac 20 36 39 39 2c 20 20 36 32 32 2c 20 20 37 37 34   699,  622,  774
264ad 2c 0a 20 2f 2a 20 20 20 34 32 30 20 2a 2f 20 20  ,. /*   420 */  
264ae 20 37 36 35 2c 20 20 37 36 39 2c 20 20 37 37 30   765,  769,  770
264af 2c 20 20 37 37 32 2c 20 20 37 37 33 2c 20 20 37  ,  772,  773,  7
264b0 38 37 2c 20 20 37 38 38 2c 20 20 37 39 30 2c 20  87,  788,  790, 
264b1 20 37 39 36 2c 20 20 38 30 33 2c 0a 20 2f 2a 20   796,  803,. /* 
264b2 20 20 34 33 30 20 2a 2f 20 20 20 38 30 36 2c 20    430 */   806, 
264b3 20 37 38 39 2c 20 20 37 39 34 2c 20 20 37 39 35   789,  794,  795
264b4 2c 20 20 37 39 37 2c 20 20 38 30 32 2c 20 20 38  ,  797,  802,  8
264b5 30 35 2c 20 20 36 39 37 2c 20 20 36 39 38 2c 20  05,  697,  698, 
264b6 20 38 30 39 2c 0a 20 2f 2a 20 20 20 34 34 30 20   809,. /*   440 
264b7 2a 2f 20 20 20 36 32 39 2c 20 20 36 33 30 2c 20  */   629,  630, 
264b8 20 36 33 33 2c 20 20 36 33 34 2c 20 20 38 34 34   633,  634,  844
264b9 2c 20 20 38 34 36 2c 20 20 38 34 35 2c 20 20 38  ,  846,  845,  8
264ba 34 37 2c 20 20 36 33 32 2c 20 20 36 33 31 2c 0a  47,  632,  631,.
264bb 20 2f 2a 20 20 20 34 35 30 20 2a 2f 20 20 20 37   /*   450 */   7
264bc 37 35 2c 20 20 37 37 38 2c 20 20 38 31 31 2c 20  75,  778,  811, 
264bd 20 38 31 32 2c 20 20 38 36 39 2c 20 20 38 37 30   812,  869,  870
264be 2c 20 20 38 37 31 2c 20 20 38 37 32 2c 20 20 38  ,  871,  872,  8
264bf 37 33 2c 20 20 38 30 37 2c 0a 20 2f 2a 20 20 20  73,  807,. /*   
264c0 34 36 30 20 2a 2f 20 20 20 37 32 30 2c 20 20 38  460 */   720,  8
264c1 31 30 2c 20 20 37 39 33 2c 20 20 37 33 32 2c 20  10,  793,  732, 
264c2 20 37 33 35 2c 20 20 37 33 36 2c 20 20 37 33 33   735,  736,  733
264c3 2c 20 20 37 30 33 2c 20 20 37 31 33 2c 20 20 37  ,  703,  713,  7
264c4 32 32 2c 0a 20 2f 2a 20 20 20 34 37 30 20 2a 2f  22,. /*   470 */
264c5 20 20 20 37 32 33 2c 20 20 37 32 34 2c 20 20 37     723,  724,  7
264c6 32 35 2c 20 20 37 30 38 2c 20 20 37 30 39 2c 20  25,  708,  709, 
264c7 20 37 31 35 2c 20 20 37 32 38 2c 20 20 37 36 31   715,  728,  761
264c8 2c 20 20 37 36 32 2c 20 20 37 31 36 2c 0a 20 2f  ,  762,  716,. /
264c9 2a 20 20 20 34 38 30 20 2a 2f 20 20 20 37 30 35  *   480 */   705
264ca 2c 20 20 37 30 36 2c 20 20 37 30 37 2c 20 20 38  ,  706,  707,  8
264cb 30 38 2c 20 20 37 36 34 2c 20 20 37 37 36 2c 20  08,  764,  776, 
264cc 20 37 37 37 2c 20 20 36 34 30 2c 20 20 36 34 31   777,  640,  641
264cd 2c 20 20 37 37 31 2c 0a 20 2f 2a 20 20 20 34 39  ,  771,. /*   49
264ce 30 20 2a 2f 20 20 20 36 34 32 2c 20 20 36 34 33  0 */   642,  643
264cf 2c 20 20 36 34 34 2c 20 20 36 38 32 2c 20 20 36  ,  644,  682,  6
264d0 38 35 2c 20 20 36 38 36 2c 20 20 36 38 37 2c 20  85,  686,  687, 
264d1 20 36 34 35 2c 20 20 36 36 34 2c 20 20 36 36 37   645,  664,  667
264d2 2c 0a 20 2f 2a 20 20 20 35 30 30 20 2a 2f 20 20  ,. /*   500 */  
264d3 20 36 36 38 2c 20 20 36 34 36 2c 20 20 36 35 33   668,  646,  653
264d4 2c 20 20 36 34 37 2c 20 20 36 34 38 2c 20 20 36  ,  647,  648,  6
264d5 35 35 2c 20 20 36 35 36 2c 20 20 36 35 37 2c 20  55,  656,  657, 
264d6 20 36 36 30 2c 20 20 36 36 31 2c 0a 20 2f 2a 20   660,  661,. /* 
264d7 20 20 35 31 30 20 2a 2f 20 20 20 36 36 32 2c 20    510 */   662, 
264d8 20 36 36 33 2c 20 20 36 35 38 2c 20 20 36 35 39   663,  658,  659
264d9 2c 20 20 38 32 37 2c 20 20 38 32 38 2c 20 20 38  ,  827,  828,  8
264da 33 32 2c 20 20 38 33 30 2c 20 20 38 32 39 2c 20  32,  830,  829, 
264db 20 36 35 30 2c 0a 20 2f 2a 20 20 20 35 32 30 20   650,. /*   520 
264dc 2a 2f 20 20 20 36 35 31 2c 20 20 36 36 35 2c 20  */   651,  665, 
264dd 20 36 33 38 2c 20 20 36 32 37 2c 20 20 36 32 30   638,  627,  620
264de 2c 20 20 36 37 31 2c 20 20 36 37 34 2c 20 20 36  ,  671,  674,  6
264df 37 35 2c 20 20 36 37 36 2c 20 20 36 37 37 2c 0a  75,  676,  677,.
264e0 20 2f 2a 20 20 20 35 33 30 20 2a 2f 20 20 20 36   /*   530 */   6
264e1 37 38 2c 20 20 36 38 30 2c 20 20 36 37 32 2c 20  78,  680,  672, 
264e2 20 36 37 33 2c 20 20 36 31 38 2c 20 20 36 31 30   673,  618,  610
264e3 2c 20 20 36 31 32 2c 20 20 37 32 31 2c 20 20 38  ,  612,  721,  8
264e4 35 30 2c 20 20 38 35 39 2c 0a 20 2f 2a 20 20 20  50,  859,. /*   
264e5 35 34 30 20 2a 2f 20 20 20 38 35 35 2c 20 20 38  540 */   855,  8
264e6 35 31 2c 20 20 38 35 32 2c 20 20 38 35 33 2c 20  51,  852,  853, 
264e7 20 36 31 33 2c 20 20 38 32 33 2c 20 20 38 32 34   613,  823,  824
264e8 2c 20 20 36 38 34 2c 20 20 37 35 35 2c 20 20 37  ,  684,  755,  7
264e9 35 36 2c 0a 20 2f 2a 20 20 20 35 35 30 20 2a 2f  56,. /*   550 */
264ea 20 20 20 38 34 39 2c 20 20 38 36 32 2c 20 20 38     849,  862,  8
264eb 36 34 2c 20 20 37 36 30 2c 20 20 38 36 35 2c 20  64,  760,  865, 
264ec 20 38 36 37 2c 20 20 38 39 32 2c 20 20 36 38 38   867,  892,  688
264ed 2c 20 20 36 38 39 2c 20 20 36 39 32 2c 0a 20 2f  ,  689,  692,. /
264ee 2a 20 20 20 35 36 30 20 2a 2f 20 20 20 38 33 33  *   560 */   833
264ef 2c 20 20 38 37 34 2c 20 20 37 34 36 2c 20 20 37  ,  874,  746,  7
264f0 34 39 2c 20 20 37 35 32 2c 20 20 37 35 34 2c 20  49,  752,  754, 
264f1 20 38 33 35 2c 20 20 38 33 36 2c 20 20 38 33 37   835,  836,  837
264f2 2c 20 20 38 33 38 2c 0a 20 2f 2a 20 20 20 35 37  ,  838,. /*   57
264f3 30 20 2a 2f 20 20 20 38 33 39 2c 20 20 38 34 32  0 */   839,  842
264f4 2c 20 20 38 34 33 2c 20 20 38 34 30 2c 20 20 38  ,  843,  840,  8
264f5 37 35 2c 20 20 38 37 39 2c 20 20 38 38 32 2c 20  75,  879,  882, 
264f6 20 38 38 34 2c 20 20 38 38 35 2c 20 20 38 38 36   884,  885,  886
264f7 2c 0a 20 2f 2a 20 20 20 35 38 30 20 2a 2f 20 20  ,. /*   580 */  
264f8 20 38 38 39 2c 20 20 38 39 31 2c 20 20 38 39 36   889,  891,  896
264f9 2c 20 20 38 39 37 2c 20 20 38 39 38 2c 20 20 39  ,  897,  898,  9
264fa 30 31 2c 20 20 38 39 39 2c 20 20 35 39 37 2c 20  01,  899,  597, 
264fb 20 35 39 31 2c 0a 7d 3b 0a 23 64 65 66 69 6e 65   591,.};.#define
264fc 20 59 59 5f 53 5a 5f 41 43 54 54 41 42 20 28 69   YY_SZ_ACTTAB (i
264fd 6e 74 29 28 73 69 7a 65 6f 66 28 79 79 5f 61 63  nt)(sizeof(yy_ac
264fe 74 69 6f 6e 29 2f 73 69 7a 65 6f 66 28 79 79 5f  tion)/sizeof(yy_
264ff 61 63 74 69 6f 6e 5b 30 5d 29 29 0a 0a 2f 2a 20  action[0]))../* 
26500 54 68 65 20 6e 65 78 74 20 74 61 62 6c 65 20 6d  The next table m
26501 61 70 73 20 74 6f 6b 65 6e 73 20 69 6e 74 6f 20  aps tokens into 
26502 66 61 6c 6c 62 61 63 6b 20 74 6f 6b 65 6e 73 2e  fallback tokens.
26503 20 20 49 66 20 61 20 63 6f 6e 73 74 72 75 63 74    If a construct
26504 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20 66 6f 6c  .** like the fol
26505 6c 6f 77 69 6e 67 3a 0a 2a 2a 20 0a 2a 2a 20 20  lowing:.** .**  
26506 20 20 20 20 25 66 61 6c 6c 62 61 63 6b 20 49 44      %fallback ID
26507 20 58 20 59 20 5a 2e 0a 2a 2a 0a 2a 2a 20 61 70   X Y Z..**.** ap
26508 70 65 61 72 73 20 69 6e 20 74 68 65 20 67 72 61  pears in the gra
26509 6d 6d 65 72 2c 20 74 68 65 6e 20 49 44 20 62 65  mmer, then ID be
2650a 63 6f 6d 65 73 20 61 20 66 61 6c 6c 62 61 63 6b  comes a fallback
2650b 20 74 6f 6b 65 6e 20 66 6f 72 20 58 2c 20 59 2c   token for X, Y,
2650c 0a 2a 2a 20 61 6e 64 20 5a 2e 20 20 57 68 65 6e  .** and Z.  When
2650d 65 76 65 72 20 6f 6e 65 20 6f 66 20 74 68 65 20  ever one of the 
2650e 74 6f 6b 65 6e 73 20 58 2c 20 59 2c 20 6f 72 20  tokens X, Y, or 
2650f 5a 20 69 73 20 69 6e 70 75 74 20 74 6f 20 74 68  Z is input to th
26510 65 20 70 61 72 73 65 72 0a 2a 2a 20 62 75 74 20  e parser.** but 
26511 69 74 20 64 6f 65 73 20 6e 6f 74 20 70 61 72 73  it does not pars
26512 65 2c 20 74 68 65 20 74 79 70 65 20 6f 66 20 74  e, the type of t
26513 68 65 20 74 6f 6b 65 6e 20 69 73 20 63 68 61 6e  he token is chan
26514 67 65 64 20 74 6f 20 49 44 20 61 6e 64 0a 2a 2a  ged to ID and.**
26515 20 74 68 65 20 70 61 72 73 65 20 69 73 20 72 65   the parse is re
26516 74 72 69 65 64 20 62 65 66 6f 72 65 20 61 6e 20  tried before an 
26517 65 72 72 6f 72 20 69 73 20 74 68 72 6f 77 6e 2e  error is thrown.
26518 0a 2a 2f 0a 23 69 66 64 65 66 20 59 59 46 41 4c  .*/.#ifdef YYFAL
26519 4c 42 41 43 4b 0a 73 74 61 74 69 63 20 63 6f 6e  LBACK.static con
2651a 73 74 20 59 59 43 4f 44 45 54 59 50 45 20 79 79  st YYCODETYPE yy
2651b 46 61 6c 6c 62 61 63 6b 5b 5d 20 3d 20 7b 0a 20  Fallback[] = {. 
2651c 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2651d 20 20 20 24 20 3d 3e 20 6e 6f 74 68 69 6e 67 20     $ => nothing 
2651e 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2651f 20 20 20 20 53 45 4d 49 20 3d 3e 20 6e 6f 74 68      SEMI => noth
26520 69 6e 67 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f  ing */.   23,  /
26521 2a 20 20 20 20 45 58 50 4c 41 49 4e 20 3d 3e 20  *    EXPLAIN => 
26522 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a  ID */.   23,  /*
26523 20 20 20 20 20 20 51 55 45 52 59 20 3d 3e 20 49        QUERY => I
26524 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20  D */.   23,  /* 
26525 20 20 20 20 20 20 50 4c 41 4e 20 3d 3e 20 49 44        PLAN => ID
26526 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20   */.   23,  /*  
26527 20 20 20 20 42 45 47 49 4e 20 3d 3e 20 49 44 20      BEGIN => ID 
26528 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 54 52  */.    0,  /* TR
26529 41 4e 53 41 43 54 49 4f 4e 20 3d 3e 20 6e 6f 74  ANSACTION => not
2652a 68 69 6e 67 20 2a 2f 0a 20 20 20 32 33 2c 20 20  hing */.   23,  
2652b 2f 2a 20 20 20 44 45 46 45 52 52 45 44 20 3d 3e  /*   DEFERRED =>
2652c 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f   ID */.   23,  /
2652d 2a 20 20 49 4d 4d 45 44 49 41 54 45 20 3d 3e 20  *  IMMEDIATE => 
2652e 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a  ID */.   23,  /*
2652f 20 20 45 58 43 4c 55 53 49 56 45 20 3d 3e 20 49    EXCLUSIVE => I
26530 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20  D */.    0,  /* 
26531 20 20 20 20 43 4f 4d 4d 49 54 20 3d 3e 20 6e 6f      COMMIT => no
26532 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32 33 2c 20  thing */.   23, 
26533 20 2f 2a 20 20 20 20 20 20 20 20 45 4e 44 20 3d   /*        END =
26534 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20  > ID */.    0,  
26535 2f 2a 20 20 20 52 4f 4c 4c 42 41 43 4b 20 3d 3e  /*   ROLLBACK =>
26536 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
26537 30 2c 20 20 2f 2a 20 20 20 20 20 43 52 45 41 54  0,  /*     CREAT
26538 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  E => nothing */.
26539 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
2653a 54 41 42 4c 45 20 3d 3e 20 6e 6f 74 68 69 6e 67  TABLE => nothing
2653b 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20   */.   23,  /*  
2653c 20 20 20 20 20 20 20 49 46 20 3d 3e 20 49 44 20         IF => ID 
2653d 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2653e 20 20 20 20 20 4e 4f 54 20 3d 3e 20 6e 6f 74 68       NOT => noth
2653f 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
26540 2a 20 20 20 20 20 45 58 49 53 54 53 20 3d 3e 20  *     EXISTS => 
26541 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32 33  nothing */.   23
26542 2c 20 20 2f 2a 20 20 20 20 20 20 20 54 45 4d 50  ,  /*       TEMP
26543 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c   => ID */.    0,
26544 20 20 2f 2a 20 20 20 20 20 20 20 20 20 4c 50 20    /*         LP 
26545 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
26546 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20    0,  /*        
26547 20 52 50 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a   RP => nothing *
26548 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
26549 20 20 20 20 20 41 53 20 3d 3e 20 6e 6f 74 68 69       AS => nothi
2654a 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
2654b 20 20 20 20 20 20 43 4f 4d 4d 41 20 3d 3e 20 6e        COMMA => n
2654c 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2654d 20 20 2f 2a 20 20 20 20 20 20 20 20 20 49 44 20    /*         ID 
2654e 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2654f 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20 41 42   23,  /*      AB
26550 4f 52 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  ORT => ID */.   
26551 32 33 2c 20 20 2f 2a 20 20 20 20 20 20 41 46 54  23,  /*      AFT
26552 45 52 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  ER => ID */.   2
26553 33 2c 20 20 2f 2a 20 20 20 20 41 4e 41 4c 59 5a  3,  /*    ANALYZ
26554 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33  E => ID */.   23
26555 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 41 53 43  ,  /*        ASC
26556 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c   => ID */.   23,
26557 20 20 2f 2a 20 20 20 20 20 41 54 54 41 43 48 20    /*     ATTACH 
26558 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20  => ID */.   23, 
26559 20 2f 2a 20 20 20 20 20 42 45 46 4f 52 45 20 3d   /*     BEFORE =
2655a 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20  > ID */.   23,  
2655b 2f 2a 20 20 20 20 43 41 53 43 41 44 45 20 3d 3e  /*    CASCADE =>
2655c 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f   ID */.   23,  /
2655d 2a 20 20 20 20 20 20 20 43 41 53 54 20 3d 3e 20  *       CAST => 
2655e 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a  ID */.   23,  /*
2655f 20 20 20 43 4f 4e 46 4c 49 43 54 20 3d 3e 20 49     CONFLICT => I
26560 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20  D */.   23,  /* 
26561 20 20 44 41 54 41 42 41 53 45 20 3d 3e 20 49 44    DATABASE => ID
26562 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20   */.   23,  /*  
26563 20 20 20 20 20 44 45 53 43 20 3d 3e 20 49 44 20       DESC => ID 
26564 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20  */.   23,  /*   
26565 20 20 44 45 54 41 43 48 20 3d 3e 20 49 44 20 2a    DETACH => ID *
26566 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20  /.   23,  /*    
26567 20 20 20 45 41 43 48 20 3d 3e 20 49 44 20 2a 2f     EACH => ID */
26568 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20  .   23,  /*     
26569 20 20 46 41 49 4c 20 3d 3e 20 49 44 20 2a 2f 0a    FAIL => ID */.
2656a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20     23,  /*      
2656b 20 20 46 4f 52 20 3d 3e 20 49 44 20 2a 2f 0a 20    FOR => ID */. 
2656c 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 49 47    23,  /*     IG
2656d 4e 4f 52 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  NORE => ID */.  
2656e 20 32 33 2c 20 20 2f 2a 20 20 49 4e 49 54 49 41   23,  /*  INITIA
2656f 4c 4c 59 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  LLY => ID */.   
26570 32 33 2c 20 20 2f 2a 20 20 20 20 49 4e 53 54 45  23,  /*    INSTE
26571 41 44 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  AD => ID */.   2
26572 33 2c 20 20 2f 2a 20 20 20 20 4c 49 4b 45 5f 4b  3,  /*    LIKE_K
26573 57 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33  W => ID */.   23
26574 2c 20 20 2f 2a 20 20 20 20 20 20 4d 41 54 43 48  ,  /*      MATCH
26575 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c   => ID */.   23,
26576 20 20 2f 2a 20 20 20 20 20 20 20 20 4b 45 59 20    /*        KEY 
26577 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20  => ID */.   23, 
26578 20 2f 2a 20 20 20 20 20 20 20 20 20 4f 46 20 3d   /*         OF =
26579 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20  > ID */.   23,  
2657a 2f 2a 20 20 20 20 20 4f 46 46 53 45 54 20 3d 3e  /*     OFFSET =>
2657b 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f   ID */.   23,  /
2657c 2a 20 20 20 20 20 50 52 41 47 4d 41 20 3d 3e 20  *     PRAGMA => 
2657d 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a  ID */.   23,  /*
2657e 20 20 20 20 20 20 52 41 49 53 45 20 3d 3e 20 49        RAISE => I
2657f 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20  D */.   23,  /* 
26580 20 20 20 52 45 50 4c 41 43 45 20 3d 3e 20 49 44     REPLACE => ID
26581 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20   */.   23,  /*  
26582 20 52 45 53 54 52 49 43 54 20 3d 3e 20 49 44 20   RESTRICT => ID 
26583 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20  */.   23,  /*   
26584 20 20 20 20 20 52 4f 57 20 3d 3e 20 49 44 20 2a       ROW => ID *
26585 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20  /.   23,  /*    
26586 54 52 49 47 47 45 52 20 3d 3e 20 49 44 20 2a 2f  TRIGGER => ID */
26587 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20  .   23,  /*     
26588 56 41 43 55 55 4d 20 3d 3e 20 49 44 20 2a 2f 0a  VACUUM => ID */.
26589 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20     23,  /*      
2658a 20 56 49 45 57 20 3d 3e 20 49 44 20 2a 2f 0a 20   VIEW => ID */. 
2658b 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 56 49 52    23,  /*    VIR
2658c 54 55 41 4c 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  TUAL => ID */.  
2658d 20 32 33 2c 20 20 2f 2a 20 20 20 20 52 45 49 4e   23,  /*    REIN
2658e 44 45 58 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  DEX => ID */.   
2658f 32 33 2c 20 20 2f 2a 20 20 20 20 20 52 45 4e 41  23,  /*     RENA
26590 4d 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  ME => ID */.   2
26591 33 2c 20 20 2f 2a 20 20 20 43 54 49 4d 45 5f 4b  3,  /*   CTIME_K
26592 57 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30  W => ID */.    0
26593 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 41 4e 59  ,  /*        ANY
26594 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
26595 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
26596 20 20 4f 52 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    OR => nothing 
26597 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
26598 20 20 20 20 20 41 4e 44 20 3d 3e 20 6e 6f 74 68       AND => noth
26599 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2659a 2a 20 20 20 20 20 20 20 20 20 49 53 20 3d 3e 20  *         IS => 
2659b 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2659c 2c 20 20 2f 2a 20 20 20 20 42 45 54 57 45 45 4e  ,  /*    BETWEEN
2659d 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2659e 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2659f 20 20 49 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    IN => nothing 
265a0 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
265a1 20 20 49 53 4e 55 4c 4c 20 3d 3e 20 6e 6f 74 68    ISNULL => noth
265a2 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
265a3 2a 20 20 20 20 4e 4f 54 4e 55 4c 4c 20 3d 3e 20  *    NOTNULL => 
265a4 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
265a5 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 4e 45  ,  /*         NE
265a6 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
265a7 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
265a8 20 20 45 51 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    EQ => nothing 
265a9 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
265aa 20 20 20 20 20 20 47 54 20 3d 3e 20 6e 6f 74 68        GT => noth
265ab 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
265ac 2a 20 20 20 20 20 20 20 20 20 4c 45 20 3d 3e 20  *         LE => 
265ad 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
265ae 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 4c 54  ,  /*         LT
265af 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
265b0 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
265b1 20 20 47 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    GE => nothing 
265b2 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
265b3 20 20 45 53 43 41 50 45 20 3d 3e 20 6e 6f 74 68    ESCAPE => noth
265b4 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
265b5 2a 20 20 20 20 20 42 49 54 41 4e 44 20 3d 3e 20  *     BITAND => 
265b6 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
265b7 2c 20 20 2f 2a 20 20 20 20 20 20 42 49 54 4f 52  ,  /*      BITOR
265b8 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
265b9 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 4c 53     0,  /*     LS
265ba 48 49 46 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  HIFT => nothing 
265bb 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
265bc 20 20 52 53 48 49 46 54 20 3d 3e 20 6e 6f 74 68    RSHIFT => noth
265bd 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
265be 2a 20 20 20 20 20 20 20 50 4c 55 53 20 3d 3e 20  *       PLUS => 
265bf 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
265c0 2c 20 20 2f 2a 20 20 20 20 20 20 4d 49 4e 55 53  ,  /*      MINUS
265c1 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
265c2 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
265c3 53 54 41 52 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  STAR => nothing 
265c4 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
265c5 20 20 20 53 4c 41 53 48 20 3d 3e 20 6e 6f 74 68     SLASH => noth
265c6 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
265c7 2a 20 20 20 20 20 20 20 20 52 45 4d 20 3d 3e 20  *        REM => 
265c8 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
265c9 2c 20 20 2f 2a 20 20 20 20 20 43 4f 4e 43 41 54  ,  /*     CONCAT
265ca 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
265cb 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 43 4f 4c     0,  /*    COL
265cc 4c 41 54 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  LATE => nothing 
265cd 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
265ce 20 20 55 4d 49 4e 55 53 20 3d 3e 20 6e 6f 74 68    UMINUS => noth
265cf 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
265d0 2a 20 20 20 20 20 20 55 50 4c 55 53 20 3d 3e 20  *      UPLUS => 
265d1 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
265d2 2c 20 20 2f 2a 20 20 20 20 20 42 49 54 4e 4f 54  ,  /*     BITNOT
265d3 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
265d4 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 53 54     0,  /*     ST
265d5 52 49 4e 47 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  RING => nothing 
265d6 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
265d7 20 4a 4f 49 4e 5f 4b 57 20 3d 3e 20 6e 6f 74 68   JOIN_KW => noth
265d8 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
265d9 2a 20 43 4f 4e 53 54 52 41 49 4e 54 20 3d 3e 20  * CONSTRAINT => 
265da 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
265db 2c 20 20 2f 2a 20 20 20 20 44 45 46 41 55 4c 54  ,  /*    DEFAULT
265dc 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
265dd 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
265de 4e 55 4c 4c 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  NULL => nothing 
265df 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
265e0 20 50 52 49 4d 41 52 59 20 3d 3e 20 6e 6f 74 68   PRIMARY => noth
265e1 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
265e2 2a 20 20 20 20 20 55 4e 49 51 55 45 20 3d 3e 20  *     UNIQUE => 
265e3 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
265e4 2c 20 20 2f 2a 20 20 20 20 20 20 43 48 45 43 4b  ,  /*      CHECK
265e5 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
265e6 20 20 20 30 2c 20 20 2f 2a 20 52 45 46 45 52 45     0,  /* REFERE
265e7 4e 43 45 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  NCES => nothing 
265e8 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
265e9 41 55 54 4f 49 4e 43 52 20 3d 3e 20 6e 6f 74 68  AUTOINCR => noth
265ea 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
265eb 2a 20 20 20 20 20 20 20 20 20 4f 4e 20 3d 3e 20  *         ON => 
265ec 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
265ed 2c 20 20 2f 2a 20 20 20 20 20 44 45 4c 45 54 45  ,  /*     DELETE
265ee 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
265ef 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 55 50     0,  /*     UP
265f0 44 41 54 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  DATE => nothing 
265f1 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
265f2 20 20 49 4e 53 45 52 54 20 3d 3e 20 6e 6f 74 68    INSERT => noth
265f3 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
265f4 2a 20 20 20 20 20 20 20 20 53 45 54 20 3d 3e 20  *        SET => 
265f5 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
265f6 2c 20 20 2f 2a 20 44 45 46 45 52 52 41 42 4c 45  ,  /* DEFERRABLE
265f7 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
265f8 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 46 4f 52     0,  /*    FOR
265f9 45 49 47 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  EIGN => nothing 
265fa 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
265fb 20 20 20 20 44 52 4f 50 20 3d 3e 20 6e 6f 74 68      DROP => noth
265fc 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
265fd 2a 20 20 20 20 20 20 55 4e 49 4f 4e 20 3d 3e 20  *      UNION => 
265fe 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
265ff 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 41 4c 4c  ,  /*        ALL
26600 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
26601 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 45 58     0,  /*     EX
26602 43 45 50 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  CEPT => nothing 
26603 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 49  */.    0,  /*  I
26604 4e 54 45 52 53 45 43 54 20 3d 3e 20 6e 6f 74 68  NTERSECT => noth
26605 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
26606 2a 20 20 20 20 20 53 45 4c 45 43 54 20 3d 3e 20  *     SELECT => 
26607 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
26608 2c 20 20 2f 2a 20 20 20 44 49 53 54 49 4e 43 54  ,  /*   DISTINCT
26609 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2660a 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2660b 20 44 4f 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20   DOT => nothing 
2660c 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2660d 20 20 20 20 46 52 4f 4d 20 3d 3e 20 6e 6f 74 68      FROM => noth
2660e 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2660f 2a 20 20 20 20 20 20 20 4a 4f 49 4e 20 3d 3e 20  *       JOIN => 
26610 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
26611 2c 20 20 2f 2a 20 20 20 20 20 20 55 53 49 4e 47  ,  /*      USING
26612 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
26613 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 4f     0,  /*      O
26614 52 44 45 52 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  RDER => nothing 
26615 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
26616 20 20 20 20 20 20 42 59 20 3d 3e 20 6e 6f 74 68        BY => noth
26617 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
26618 2a 20 20 20 20 20 20 47 52 4f 55 50 20 3d 3e 20  *      GROUP => 
26619 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2661a 2c 20 20 2f 2a 20 20 20 20 20 48 41 56 49 4e 47  ,  /*     HAVING
2661b 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2661c 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 4c     0,  /*      L
2661d 49 4d 49 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  IMIT => nothing 
2661e 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2661f 20 20 20 57 48 45 52 45 20 3d 3e 20 6e 6f 74 68     WHERE => noth
26620 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
26621 2a 20 20 20 20 20 20 20 49 4e 54 4f 20 3d 3e 20  *       INTO => 
26622 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
26623 2c 20 20 2f 2a 20 20 20 20 20 56 41 4c 55 45 53  ,  /*     VALUES
26624 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
26625 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 49 4e 54     0,  /*    INT
26626 45 47 45 52 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  EGER => nothing 
26627 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
26628 20 20 20 46 4c 4f 41 54 20 3d 3e 20 6e 6f 74 68     FLOAT => noth
26629 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2662a 2a 20 20 20 20 20 20 20 42 4c 4f 42 20 3d 3e 20  *       BLOB => 
2662b 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2662c 2c 20 20 2f 2a 20 20 20 52 45 47 49 53 54 45 52  ,  /*   REGISTER
2662d 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2662e 20 20 20 30 2c 20 20 2f 2a 20 20 20 56 41 52 49     0,  /*   VARI
2662f 41 42 4c 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  ABLE => nothing 
26630 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
26631 20 20 20 20 43 41 53 45 20 3d 3e 20 6e 6f 74 68      CASE => noth
26632 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
26633 2a 20 20 20 20 20 20 20 57 48 45 4e 20 3d 3e 20  *       WHEN => 
26634 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
26635 2c 20 20 2f 2a 20 20 20 20 20 20 20 54 48 45 4e  ,  /*       THEN
26636 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
26637 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
26638 45 4c 53 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  ELSE => nothing 
26639 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2663a 20 20 20 49 4e 44 45 58 20 3d 3e 20 6e 6f 74 68     INDEX => noth
2663b 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2663c 2a 20 20 20 20 20 20 41 4c 54 45 52 20 3d 3e 20  *      ALTER => 
2663d 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2663e 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 54 4f  ,  /*         TO
2663f 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
26640 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
26641 20 41 44 44 20 3d 3e 20 6e 6f 74 68 69 6e 67 20   ADD => nothing 
26642 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
26643 43 4f 4c 55 4d 4e 4b 57 20 3d 3e 20 6e 6f 74 68  COLUMNKW => noth
26644 69 6e 67 20 2a 2f 0a 7d 3b 0a 23 65 6e 64 69 66  ing */.};.#endif
26645 20 2f 2a 20 59 59 46 41 4c 4c 42 41 43 4b 20 2a   /* YYFALLBACK *
26646 2f 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /../* The follow
26647 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 72 65  ing structure re
26648 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c  presents a singl
26649 65 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65  e element of the
2664a 0a 2a 2a 20 70 61 72 73 65 72 27 73 20 73 74 61  .** parser's sta
2664b 63 6b 2e 20 20 49 6e 66 6f 72 6d 61 74 69 6f 6e  ck.  Information
2664c 20 73 74 6f 72 65 64 20 69 6e 63 6c 75 64 65 73   stored includes
2664d 3a 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 20 54 68 65  :.**.**   +  The
2664e 20 73 74 61 74 65 20 6e 75 6d 62 65 72 20 66 6f   state number fo
2664f 72 20 74 68 65 20 70 61 72 73 65 72 20 61 74 20  r the parser at 
26650 74 68 69 73 20 6c 65 76 65 6c 20 6f 66 20 74 68  this level of th
26651 65 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20  e stack..**.**  
26652 20 2b 20 20 54 68 65 20 76 61 6c 75 65 20 6f 66   +  The value of
26653 20 74 68 65 20 74 6f 6b 65 6e 20 73 74 6f 72 65   the token store
26654 64 20 61 74 20 74 68 69 73 20 6c 65 76 65 6c 20  d at this level 
26655 6f 66 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a  of the stack..**
26656 20 20 20 20 20 20 28 49 6e 20 6f 74 68 65 72 20        (In other 
26657 77 6f 72 64 73 2c 20 74 68 65 20 22 6d 61 6a 6f  words, the "majo
26658 72 22 20 74 6f 6b 65 6e 2e 29 0a 2a 2a 0a 2a 2a  r" token.).**.**
26659 20 20 20 2b 20 20 54 68 65 20 73 65 6d 61 6e 74     +  The semant
2665a 69 63 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20  ic value stored 
2665b 61 74 20 74 68 69 73 20 6c 65 76 65 6c 20 6f 66  at this level of
2665c 20 74 68 65 20 73 74 61 63 6b 2e 20 20 54 68 69   the stack.  Thi
2665d 73 20 69 73 0a 2a 2a 20 20 20 20 20 20 74 68 65  s is.**      the
2665e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65   information use
2665f 64 20 62 79 20 74 68 65 20 61 63 74 69 6f 6e 20  d by the action 
26660 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 65 20  routines in the 
26661 67 72 61 6d 6d 61 72 2e 0a 2a 2a 20 20 20 20 20  grammar..**     
26662 20 49 74 20 69 73 20 73 6f 6d 65 74 69 6d 65 73   It is sometimes
26663 20 63 61 6c 6c 65 64 20 74 68 65 20 22 6d 69 6e   called the "min
26664 6f 72 22 20 74 6f 6b 65 6e 2e 0a 2a 2f 0a 73 74  or" token..*/.st
26665 72 75 63 74 20 79 79 53 74 61 63 6b 45 6e 74 72  ruct yyStackEntr
26666 79 20 7b 0a 20 20 69 6e 74 20 73 74 61 74 65 6e  y {.  int staten
26667 6f 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  o;       /* The 
26668 73 74 61 74 65 2d 6e 75 6d 62 65 72 20 2a 2f 0a  state-number */.
26669 20 20 69 6e 74 20 6d 61 6a 6f 72 3b 20 20 20 20    int major;    
2666a 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 6a 6f       /* The majo
2666b 72 20 74 6f 6b 65 6e 20 76 61 6c 75 65 2e 20 20  r token value.  
2666c 54 68 69 73 20 69 73 20 74 68 65 20 63 6f 64 65  This is the code
2666d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2666e 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20        ** number 
2666f 66 6f 72 20 74 68 65 20 74 6f 6b 65 6e 20 61 74  for the token at
26670 20 74 68 69 73 20 73 74 61 63 6b 20 6c 65 76 65   this stack leve
26671 6c 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52 54 59  l */.  YYMINORTY
26672 50 45 20 6d 69 6e 6f 72 3b 20 2f 2a 20 54 68 65  PE minor; /* The
26673 20 75 73 65 72 2d 73 75 70 70 6c 69 65 64 20 6d   user-supplied m
26674 69 6e 6f 72 20 74 6f 6b 65 6e 20 76 61 6c 75 65  inor token value
26675 2e 20 20 54 68 69 73 0a 20 20 20 20 20 20 20 20  .  This.        
26676 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
26677 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  is the value of 
26678 74 68 65 20 74 6f 6b 65 6e 20 20 2a 2f 0a 7d 3b  the token  */.};
26679 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
2667a 79 79 53 74 61 63 6b 45 6e 74 72 79 20 79 79 53  yyStackEntry yyS
2667b 74 61 63 6b 45 6e 74 72 79 3b 0a 0a 2f 2a 20 54  tackEntry;../* T
2667c 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20  he state of the 
2667d 70 61 72 73 65 72 20 69 73 20 63 6f 6d 70 6c 65  parser is comple
2667e 74 65 6c 79 20 63 6f 6e 74 61 69 6e 65 64 20 69  tely contained i
2667f 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  n an instance of
26680 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** the followin
26681 67 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 73  g structure */.s
26682 74 72 75 63 74 20 79 79 50 61 72 73 65 72 20 7b  truct yyParser {
26683 0a 20 20 69 6e 74 20 79 79 69 64 78 3b 20 20 20  .  int yyidx;   
26684 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26685 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 6f 70   /* Index of top
26686 20 65 6c 65 6d 65 6e 74 20 69 6e 20 73 74 61 63   element in stac
26687 6b 20 2a 2f 0a 20 20 69 6e 74 20 79 79 65 72 72  k */.  int yyerr
26688 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  cnt;            
26689 20 20 20 20 20 2f 2a 20 53 68 69 66 74 73 20 6c       /* Shifts l
2668a 65 66 74 20 62 65 66 6f 72 65 20 6f 75 74 20 6f  eft before out o
2668b 66 20 74 68 65 20 65 72 72 6f 72 20 2a 2f 0a 20  f the error */. 
2668c 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
2668d 47 5f 53 44 45 43 4c 20 20 20 20 20 20 20 20 20  G_SDECL         
2668e 20 20 20 20 20 20 20 2f 2a 20 41 20 70 6c 61 63         /* A plac
2668f 65 20 74 6f 20 68 6f 6c 64 20 25 65 78 74 72 61  e to hold %extra
26690 5f 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 23 69 66  _argument */.#if
26691 20 59 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30   YYSTACKDEPTH<=0
26692 0a 20 20 69 6e 74 20 79 79 73 74 6b 73 7a 3b 20  .  int yystksz; 
26693 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26694 20 2f 2a 20 43 75 72 72 65 6e 74 20 73 69 64 65   /* Current side
26695 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20 2a 2f   of the stack */
26696 0a 20 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20  .  yyStackEntry 
26697 2a 79 79 73 74 61 63 6b 3b 20 20 20 20 20 20 20  *yystack;       
26698 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 27 73   /* The parser's
26699 20 73 74 61 63 6b 20 2a 2f 0a 23 65 6c 73 65 0a   stack */.#else.
2669a 20 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20 79    yyStackEntry y
2669b 79 73 74 61 63 6b 5b 59 59 53 54 41 43 4b 44 45  ystack[YYSTACKDE
2669c 50 54 48 5d 3b 20 20 2f 2a 20 54 68 65 20 70 61  PTH];  /* The pa
2669d 72 73 65 72 27 73 20 73 74 61 63 6b 20 2a 2f 0a  rser's stack */.
2669e 23 65 6e 64 69 66 0a 7d 3b 0a 74 79 70 65 64 65  #endif.};.typede
2669f 66 20 73 74 72 75 63 74 20 79 79 50 61 72 73 65  f struct yyParse
266a0 72 20 79 79 50 61 72 73 65 72 3b 0a 0a 23 69 66  r yyParser;..#if
266a1 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74  ndef NDEBUG.stat
266a2 69 63 20 46 49 4c 45 20 2a 79 79 54 72 61 63 65  ic FILE *yyTrace
266a3 46 49 4c 45 20 3d 20 30 3b 0a 73 74 61 74 69 63  FILE = 0;.static
266a4 20 63 68 61 72 20 2a 79 79 54 72 61 63 65 50 72   char *yyTracePr
266a5 6f 6d 70 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66  ompt = 0;.#endif
266a6 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 23   /* NDEBUG */..#
266a7 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a  ifndef NDEBUG./*
266a8 20 0a 2a 2a 20 54 75 72 6e 20 70 61 72 73 65 72   .** Turn parser
266a9 20 74 72 61 63 69 6e 67 20 6f 6e 20 62 79 20 67   tracing on by g
266aa 69 76 69 6e 67 20 61 20 73 74 72 65 61 6d 20 74  iving a stream t
266ab 6f 20 77 68 69 63 68 20 74 6f 20 77 72 69 74 65  o which to write
266ac 20 74 68 65 20 74 72 61 63 65 0a 2a 2a 20 61 6e   the trace.** an
266ad 64 20 61 20 70 72 6f 6d 70 74 20 74 6f 20 70 72  d a prompt to pr
266ae 65 66 61 63 65 20 65 61 63 68 20 74 72 61 63 65  eface each trace
266af 20 6d 65 73 73 61 67 65 2e 20 20 54 72 61 63 69   message.  Traci
266b0 6e 67 20 69 73 20 74 75 72 6e 65 64 20 6f 66 66  ng is turned off
266b1 0a 2a 2a 20 62 79 20 6d 61 6b 69 6e 67 20 65 69  .** by making ei
266b2 74 68 65 72 20 61 72 67 75 6d 65 6e 74 20 4e 55  ther argument NU
266b3 4c 4c 20 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73  LL .**.** Inputs
266b4 3a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69  :.** <ul>.** <li
266b5 3e 20 41 20 46 49 4c 45 2a 20 74 6f 20 77 68 69  > A FILE* to whi
266b6 63 68 20 74 72 61 63 65 20 6f 75 74 70 75 74 20  ch trace output 
266b7 73 68 6f 75 6c 64 20 62 65 20 77 72 69 74 74 65  should be writte
266b8 6e 2e 0a 2a 2a 20 20 20 20 20 20 49 66 20 4e 55  n..**      If NU
266b9 4c 4c 2c 20 74 68 65 6e 20 74 72 61 63 69 6e 67  LL, then tracing
266ba 20 69 73 20 74 75 72 6e 65 64 20 6f 66 66 2e 0a   is turned off..
266bb 2a 2a 20 3c 6c 69 3e 20 41 20 70 72 65 66 69 78  ** <li> A prefix
266bc 20 73 74 72 69 6e 67 20 77 72 69 74 74 65 6e 20   string written 
266bd 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
266be 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20 20 20 20   of every.**    
266bf 20 20 6c 69 6e 65 20 6f 66 20 74 72 61 63 65 20    line of trace 
266c0 6f 75 74 70 75 74 2e 20 20 49 66 20 4e 55 4c 4c  output.  If NULL
266c1 2c 20 74 68 65 6e 20 74 72 61 63 69 6e 67 20 69  , then tracing i
266c2 73 0a 2a 2a 20 20 20 20 20 20 74 75 72 6e 65 64  s.**      turned
266c3 20 6f 66 66 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a   off..** </ul>.*
266c4 2a 0a 2a 2a 20 4f 75 74 70 75 74 73 3a 0a 2a 2a  *.** Outputs:.**
266c5 20 4e 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   None..*/.SQLITE
266c6 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
266c7 6c 69 74 65 33 50 61 72 73 65 72 54 72 61 63 65  lite3ParserTrace
266c8 28 46 49 4c 45 20 2a 54 72 61 63 65 46 49 4c 45  (FILE *TraceFILE
266c9 2c 20 63 68 61 72 20 2a 7a 54 72 61 63 65 50 72  , char *zTracePr
266ca 6f 6d 70 74 29 7b 0a 20 20 79 79 54 72 61 63 65  ompt){.  yyTrace
266cb 46 49 4c 45 20 3d 20 54 72 61 63 65 46 49 4c 45  FILE = TraceFILE
266cc 3b 0a 20 20 79 79 54 72 61 63 65 50 72 6f 6d 70  ;.  yyTracePromp
266cd 74 20 3d 20 7a 54 72 61 63 65 50 72 6f 6d 70 74  t = zTracePrompt
266ce 3b 0a 20 20 69 66 28 20 79 79 54 72 61 63 65 46  ;.  if( yyTraceF
266cf 49 4c 45 3d 3d 30 20 29 20 79 79 54 72 61 63 65  ILE==0 ) yyTrace
266d0 50 72 6f 6d 70 74 20 3d 20 30 3b 0a 20 20 65 6c  Prompt = 0;.  el
266d1 73 65 20 69 66 28 20 79 79 54 72 61 63 65 50 72  se if( yyTracePr
266d2 6f 6d 70 74 3d 3d 30 20 29 20 79 79 54 72 61 63  ompt==0 ) yyTrac
266d3 65 46 49 4c 45 20 3d 20 30 3b 0a 7d 0a 23 65 6e  eFILE = 0;.}.#en
266d4 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f  dif /* NDEBUG */
266d5 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
266d6 0a 2f 2a 20 46 6f 72 20 74 72 61 63 69 6e 67 20  ./* For tracing 
266d7 73 68 69 66 74 73 2c 20 74 68 65 20 6e 61 6d 65  shifts, the name
266d8 73 20 6f 66 20 61 6c 6c 20 74 65 72 6d 69 6e 61  s of all termina
266d9 6c 73 20 61 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e  ls and nontermin
266da 61 6c 73 0a 2a 2a 20 61 72 65 20 72 65 71 75 69  als.** are requi
266db 72 65 64 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  red.  The follow
266dc 69 6e 67 20 74 61 62 6c 65 20 73 75 70 70 6c 69  ing table suppli
266dd 65 73 20 74 68 65 73 65 20 6e 61 6d 65 73 20 2a  es these names *
266de 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  /.static const c
266df 68 61 72 20 2a 63 6f 6e 73 74 20 79 79 54 6f 6b  har *const yyTok
266e0 65 6e 4e 61 6d 65 5b 5d 20 3d 20 7b 20 0a 20 20  enName[] = { .  
266e1 22 24 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  "$",            
266e2 20 22 53 45 4d 49 22 2c 20 20 20 20 20 20 20 20   "SEMI",        
266e3 20 20 22 45 58 50 4c 41 49 4e 22 2c 20 20 20 20    "EXPLAIN",    
266e4 20 20 20 22 51 55 45 52 59 22 2c 20 20 20 20 20     "QUERY",     
266e5 20 20 0a 20 20 22 50 4c 41 4e 22 2c 20 20 20 20    .  "PLAN",    
266e6 20 20 20 20 20 20 22 42 45 47 49 4e 22 2c 20 20        "BEGIN",  
266e7 20 20 20 20 20 20 20 22 54 52 41 4e 53 41 43 54         "TRANSACT
266e8 49 4f 4e 22 2c 20 20 20 22 44 45 46 45 52 52 45  ION",   "DEFERRE
266e9 44 22 2c 20 20 20 20 0a 20 20 22 49 4d 4d 45 44  D",    .  "IMMED
266ea 49 41 54 45 22 2c 20 20 20 20 20 22 45 58 43 4c  IATE",     "EXCL
266eb 55 53 49 56 45 22 2c 20 20 20 20 20 22 43 4f 4d  USIVE",     "COM
266ec 4d 49 54 22 2c 20 20 20 20 20 20 20 20 22 45 4e  MIT",        "EN
266ed 44 22 2c 20 20 20 20 20 20 20 20 20 0a 20 20 22  D",         .  "
266ee 52 4f 4c 4c 42 41 43 4b 22 2c 20 20 20 20 20 20  ROLLBACK",      
266ef 22 43 52 45 41 54 45 22 2c 20 20 20 20 20 20 20  "CREATE",       
266f0 20 22 54 41 42 4c 45 22 2c 20 20 20 20 20 20 20   "TABLE",       
266f1 20 20 22 49 46 22 2c 20 20 20 20 20 20 20 20 20    "IF",         
266f2 20 0a 20 20 22 4e 4f 54 22 2c 20 20 20 20 20 20   .  "NOT",      
266f3 20 20 20 20 20 22 45 58 49 53 54 53 22 2c 20 20       "EXISTS",  
266f4 20 20 20 20 20 20 22 54 45 4d 50 22 2c 20 20 20        "TEMP",   
266f5 20 20 20 20 20 20 20 22 4c 50 22 2c 20 20 20 20         "LP",    
266f6 20 20 20 20 20 20 0a 20 20 22 52 50 22 2c 20 20        .  "RP",  
266f7 20 20 20 20 20 20 20 20 20 20 22 41 53 22 2c 20            "AS", 
266f8 20 20 20 20 20 20 20 20 20 20 20 22 43 4f 4d 4d             "COMM
266f9 41 22 2c 20 20 20 20 20 20 20 20 20 22 49 44 22  A",         "ID"
266fa 2c 20 20 20 20 20 20 20 20 20 20 0a 20 20 22 41  ,          .  "A
266fb 42 4f 52 54 22 2c 20 20 20 20 20 20 20 20 20 22  BORT",         "
266fc 41 46 54 45 52 22 2c 20 20 20 20 20 20 20 20 20  AFTER",         
266fd 22 41 4e 41 4c 59 5a 45 22 2c 20 20 20 20 20 20  "ANALYZE",      
266fe 20 22 41 53 43 22 2c 20 20 20 20 20 20 20 20 20   "ASC",         
266ff 0a 20 20 22 41 54 54 41 43 48 22 2c 20 20 20 20  .  "ATTACH",    
26700 20 20 20 20 22 42 45 46 4f 52 45 22 2c 20 20 20      "BEFORE",   
26701 20 20 20 20 20 22 43 41 53 43 41 44 45 22 2c 20       "CASCADE", 
26702 20 20 20 20 20 20 22 43 41 53 54 22 2c 20 20 20        "CAST",   
26703 20 20 20 20 20 0a 20 20 22 43 4f 4e 46 4c 49 43       .  "CONFLIC
26704 54 22 2c 20 20 20 20 20 20 22 44 41 54 41 42 41  T",      "DATABA
26705 53 45 22 2c 20 20 20 20 20 20 22 44 45 53 43 22  SE",      "DESC"
26706 2c 20 20 20 20 20 20 20 20 20 20 22 44 45 54 41  ,          "DETA
26707 43 48 22 2c 20 20 20 20 20 20 0a 20 20 22 45 41  CH",      .  "EA
26708 43 48 22 2c 20 20 20 20 20 20 20 20 20 20 22 46  CH",          "F
26709 41 49 4c 22 2c 20 20 20 20 20 20 20 20 20 20 22  AIL",          "
2670a 46 4f 52 22 2c 20 20 20 20 20 20 20 20 20 20 20  FOR",           
2670b 22 49 47 4e 4f 52 45 22 2c 20 20 20 20 20 20 0a  "IGNORE",      .
2670c 20 20 22 49 4e 49 54 49 41 4c 4c 59 22 2c 20 20    "INITIALLY",  
2670d 20 20 20 22 49 4e 53 54 45 41 44 22 2c 20 20 20     "INSTEAD",   
2670e 20 20 20 20 22 4c 49 4b 45 5f 4b 57 22 2c 20 20      "LIKE_KW",  
2670f 20 20 20 20 20 22 4d 41 54 43 48 22 2c 20 20 20       "MATCH",   
26710 20 20 20 20 0a 20 20 22 4b 45 59 22 2c 20 20 20      .  "KEY",   
26711 20 20 20 20 20 20 20 20 22 4f 46 22 2c 20 20 20          "OF",   
26712 20 20 20 20 20 20 20 20 20 22 4f 46 46 53 45 54           "OFFSET
26713 22 2c 20 20 20 20 20 20 20 20 22 50 52 41 47 4d  ",        "PRAGM
26714 41 22 2c 20 20 20 20 20 20 0a 20 20 22 52 41 49  A",      .  "RAI
26715 53 45 22 2c 20 20 20 20 20 20 20 20 20 22 52 45  SE",         "RE
26716 50 4c 41 43 45 22 2c 20 20 20 20 20 20 20 22 52  PLACE",       "R
26717 45 53 54 52 49 43 54 22 2c 20 20 20 20 20 20 22  ESTRICT",      "
26718 52 4f 57 22 2c 20 20 20 20 20 20 20 20 20 0a 20  ROW",         . 
26719 20 22 54 52 49 47 47 45 52 22 2c 20 20 20 20 20   "TRIGGER",     
2671a 20 20 22 56 41 43 55 55 4d 22 2c 20 20 20 20 20    "VACUUM",     
2671b 20 20 20 22 56 49 45 57 22 2c 20 20 20 20 20 20     "VIEW",      
2671c 20 20 20 20 22 56 49 52 54 55 41 4c 22 2c 20 20      "VIRTUAL",  
2671d 20 20 20 0a 20 20 22 52 45 49 4e 44 45 58 22 2c     .  "REINDEX",
2671e 20 20 20 20 20 20 20 22 52 45 4e 41 4d 45 22 2c         "RENAME",
2671f 20 20 20 20 20 20 20 20 22 43 54 49 4d 45 5f 4b          "CTIME_K
26720 57 22 2c 20 20 20 20 20 20 22 41 4e 59 22 2c 20  W",      "ANY", 
26721 20 20 20 20 20 20 20 20 0a 20 20 22 4f 52 22 2c          .  "OR",
26722 20 20 20 20 20 20 20 20 20 20 20 20 22 41 4e 44              "AND
26723 22 2c 20 20 20 20 20 20 20 20 20 20 20 22 49 53  ",           "IS
26724 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 42  ",            "B
26725 45 54 57 45 45 4e 22 2c 20 20 20 20 20 0a 20 20  ETWEEN",     .  
26726 22 49 4e 22 2c 20 20 20 20 20 20 20 20 20 20 20  "IN",           
26727 20 22 49 53 4e 55 4c 4c 22 2c 20 20 20 20 20 20   "ISNULL",      
26728 20 20 22 4e 4f 54 4e 55 4c 4c 22 2c 20 20 20 20    "NOTNULL",    
26729 20 20 20 22 4e 45 22 2c 20 20 20 20 20 20 20 20     "NE",        
2672a 20 20 0a 20 20 22 45 51 22 2c 20 20 20 20 20 20    .  "EQ",      
2672b 20 20 20 20 20 20 22 47 54 22 2c 20 20 20 20 20        "GT",     
2672c 20 20 20 20 20 20 20 22 4c 45 22 2c 20 20 20 20         "LE",    
2672d 20 20 20 20 20 20 20 20 22 4c 54 22 2c 20 20 20          "LT",   
2672e 20 20 20 20 20 20 20 0a 20 20 22 47 45 22 2c 20         .  "GE", 
2672f 20 20 20 20 20 20 20 20 20 20 20 22 45 53 43 41             "ESCA
26730 50 45 22 2c 20 20 20 20 20 20 20 20 22 42 49 54  PE",        "BIT
26731 41 4e 44 22 2c 20 20 20 20 20 20 20 20 22 42 49  AND",        "BI
26732 54 4f 52 22 2c 20 20 20 20 20 20 20 0a 20 20 22  TOR",       .  "
26733 4c 53 48 49 46 54 22 2c 20 20 20 20 20 20 20 20  LSHIFT",        
26734 22 52 53 48 49 46 54 22 2c 20 20 20 20 20 20 20  "RSHIFT",       
26735 20 22 50 4c 55 53 22 2c 20 20 20 20 20 20 20 20   "PLUS",        
26736 20 20 22 4d 49 4e 55 53 22 2c 20 20 20 20 20 20    "MINUS",      
26737 20 0a 20 20 22 53 54 41 52 22 2c 20 20 20 20 20   .  "STAR",     
26738 20 20 20 20 20 22 53 4c 41 53 48 22 2c 20 20 20       "SLASH",   
26739 20 20 20 20 20 20 22 52 45 4d 22 2c 20 20 20 20        "REM",    
2673a 20 20 20 20 20 20 20 22 43 4f 4e 43 41 54 22 2c         "CONCAT",
2673b 20 20 20 20 20 20 0a 20 20 22 43 4f 4c 4c 41 54        .  "COLLAT
2673c 45 22 2c 20 20 20 20 20 20 20 22 55 4d 49 4e 55  E",       "UMINU
2673d 53 22 2c 20 20 20 20 20 20 20 20 22 55 50 4c 55  S",        "UPLU
2673e 53 22 2c 20 20 20 20 20 20 20 20 20 22 42 49 54  S",         "BIT
2673f 4e 4f 54 22 2c 20 20 20 20 20 20 0a 20 20 22 53  NOT",      .  "S
26740 54 52 49 4e 47 22 2c 20 20 20 20 20 20 20 20 22  TRING",        "
26741 4a 4f 49 4e 5f 4b 57 22 2c 20 20 20 20 20 20 20  JOIN_KW",       
26742 22 43 4f 4e 53 54 52 41 49 4e 54 22 2c 20 20 20  "CONSTRAINT",   
26743 20 22 44 45 46 41 55 4c 54 22 2c 20 20 20 20 20   "DEFAULT",     
26744 0a 20 20 22 4e 55 4c 4c 22 2c 20 20 20 20 20 20  .  "NULL",      
26745 20 20 20 20 22 50 52 49 4d 41 52 59 22 2c 20 20      "PRIMARY",  
26746 20 20 20 20 20 22 55 4e 49 51 55 45 22 2c 20 20       "UNIQUE",  
26747 20 20 20 20 20 20 22 43 48 45 43 4b 22 2c 20 20        "CHECK",  
26748 20 20 20 20 20 0a 20 20 22 52 45 46 45 52 45 4e       .  "REFEREN
26749 43 45 53 22 2c 20 20 20 20 22 41 55 54 4f 49 4e  CES",    "AUTOIN
2674a 43 52 22 2c 20 20 20 20 20 20 22 4f 4e 22 2c 20  CR",      "ON", 
2674b 20 20 20 20 20 20 20 20 20 20 20 22 44 45 4c 45             "DELE
2674c 54 45 22 2c 20 20 20 20 20 20 0a 20 20 22 55 50  TE",      .  "UP
2674d 44 41 54 45 22 2c 20 20 20 20 20 20 20 20 22 49  DATE",        "I
2674e 4e 53 45 52 54 22 2c 20 20 20 20 20 20 20 20 22  NSERT",        "
2674f 53 45 54 22 2c 20 20 20 20 20 20 20 20 20 20 20  SET",           
26750 22 44 45 46 45 52 52 41 42 4c 45 22 2c 20 20 0a  "DEFERRABLE",  .
26751 20 20 22 46 4f 52 45 49 47 4e 22 2c 20 20 20 20    "FOREIGN",    
26752 20 20 20 22 44 52 4f 50 22 2c 20 20 20 20 20 20     "DROP",      
26753 20 20 20 20 22 55 4e 49 4f 4e 22 2c 20 20 20 20      "UNION",    
26754 20 20 20 20 20 22 41 4c 4c 22 2c 20 20 20 20 20       "ALL",     
26755 20 20 20 20 0a 20 20 22 45 58 43 45 50 54 22 2c      .  "EXCEPT",
26756 20 20 20 20 20 20 20 20 22 49 4e 54 45 52 53 45          "INTERSE
26757 43 54 22 2c 20 20 20 20 20 22 53 45 4c 45 43 54  CT",     "SELECT
26758 22 2c 20 20 20 20 20 20 20 20 22 44 49 53 54 49  ",        "DISTI
26759 4e 43 54 22 2c 20 20 20 20 0a 20 20 22 44 4f 54  NCT",    .  "DOT
2675a 22 2c 20 20 20 20 20 20 20 20 20 20 20 22 46 52  ",           "FR
2675b 4f 4d 22 2c 20 20 20 20 20 20 20 20 20 20 22 4a  OM",          "J
2675c 4f 49 4e 22 2c 20 20 20 20 20 20 20 20 20 20 22  OIN",          "
2675d 55 53 49 4e 47 22 2c 20 20 20 20 20 20 20 0a 20  USING",       . 
2675e 20 22 4f 52 44 45 52 22 2c 20 20 20 20 20 20 20   "ORDER",       
2675f 20 20 22 42 59 22 2c 20 20 20 20 20 20 20 20 20    "BY",         
26760 20 20 20 22 47 52 4f 55 50 22 2c 20 20 20 20 20     "GROUP",     
26761 20 20 20 20 22 48 41 56 49 4e 47 22 2c 20 20 20      "HAVING",   
26762 20 20 20 0a 20 20 22 4c 49 4d 49 54 22 2c 20 20     .  "LIMIT",  
26763 20 20 20 20 20 20 20 22 57 48 45 52 45 22 2c 20         "WHERE", 
26764 20 20 20 20 20 20 20 20 22 49 4e 54 4f 22 2c 20          "INTO", 
26765 20 20 20 20 20 20 20 20 20 22 56 41 4c 55 45 53           "VALUES
26766 22 2c 20 20 20 20 20 20 0a 20 20 22 49 4e 54 45  ",      .  "INTE
26767 47 45 52 22 2c 20 20 20 20 20 20 20 22 46 4c 4f  GER",       "FLO
26768 41 54 22 2c 20 20 20 20 20 20 20 20 20 22 42 4c  AT",         "BL
26769 4f 42 22 2c 20 20 20 20 20 20 20 20 20 20 22 52  OB",          "R
2676a 45 47 49 53 54 45 52 22 2c 20 20 20 20 0a 20 20  EGISTER",    .  
2676b 22 56 41 52 49 41 42 4c 45 22 2c 20 20 20 20 20  "VARIABLE",     
2676c 20 22 43 41 53 45 22 2c 20 20 20 20 20 20 20 20   "CASE",        
2676d 20 20 22 57 48 45 4e 22 2c 20 20 20 20 20 20 20    "WHEN",       
2676e 20 20 20 22 54 48 45 4e 22 2c 20 20 20 20 20 20     "THEN",      
2676f 20 20 0a 20 20 22 45 4c 53 45 22 2c 20 20 20 20    .  "ELSE",    
26770 20 20 20 20 20 20 22 49 4e 44 45 58 22 2c 20 20        "INDEX",  
26771 20 20 20 20 20 20 20 22 41 4c 54 45 52 22 2c 20         "ALTER", 
26772 20 20 20 20 20 20 20 20 22 54 4f 22 2c 20 20 20          "TO",   
26773 20 20 20 20 20 20 20 0a 20 20 22 41 44 44 22 2c         .  "ADD",
26774 20 20 20 20 20 20 20 20 20 20 20 22 43 4f 4c 55             "COLU
26775 4d 4e 4b 57 22 2c 20 20 20 20 20 20 22 65 72 72  MNKW",      "err
26776 6f 72 22 2c 20 20 20 20 20 20 20 20 20 22 69 6e  or",         "in
26777 70 75 74 22 2c 20 20 20 20 20 20 20 0a 20 20 22  put",       .  "
26778 63 6d 64 6c 69 73 74 22 2c 20 20 20 20 20 20 20  cmdlist",       
26779 22 65 63 6d 64 22 2c 20 20 20 20 20 20 20 20 20  "ecmd",         
2677a 20 22 63 6d 64 78 22 2c 20 20 20 20 20 20 20 20   "cmdx",        
2677b 20 20 22 63 6d 64 22 2c 20 20 20 20 20 20 20 20    "cmd",        
2677c 20 0a 20 20 22 65 78 70 6c 61 69 6e 22 2c 20 20   .  "explain",  
2677d 20 20 20 20 20 22 74 72 61 6e 73 74 79 70 65 22       "transtype"
2677e 2c 20 20 20 20 20 22 74 72 61 6e 73 5f 6f 70 74  ,     "trans_opt
2677f 22 2c 20 20 20 20 20 22 6e 6d 22 2c 20 20 20 20  ",     "nm",    
26780 20 20 20 20 20 20 0a 20 20 22 63 72 65 61 74 65        .  "create
26781 5f 74 61 62 6c 65 22 2c 20 20 22 63 72 65 61 74  _table",  "creat
26782 65 5f 74 61 62 6c 65 5f 61 72 67 73 22 2c 20 20  e_table_args",  
26783 22 74 65 6d 70 22 2c 20 20 20 20 20 20 20 20 20  "temp",         
26784 20 22 69 66 6e 6f 74 65 78 69 73 74 73 22 2c 20   "ifnotexists", 
26785 0a 20 20 22 64 62 6e 6d 22 2c 20 20 20 20 20 20  .  "dbnm",      
26786 20 20 20 20 22 63 6f 6c 75 6d 6e 6c 69 73 74 22      "columnlist"
26787 2c 20 20 20 20 22 63 6f 6e 73 6c 69 73 74 5f 6f  ,    "conslist_o
26788 70 74 22 2c 20 20 22 73 65 6c 65 63 74 22 2c 20  pt",  "select", 
26789 20 20 20 20 20 0a 20 20 22 63 6f 6c 75 6d 6e 22       .  "column"
2678a 2c 20 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e  ,        "column
2678b 69 64 22 2c 20 20 20 20 20 20 22 74 79 70 65 22  id",      "type"
2678c 2c 20 20 20 20 20 20 20 20 20 20 22 63 61 72 67  ,          "carg
2678d 6c 69 73 74 22 2c 20 20 20 20 0a 20 20 22 69 64  list",    .  "id
2678e 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 69  ",            "i
2678f 64 73 22 2c 20 20 20 20 20 20 20 20 20 20 20 22  ds",           "
26790 74 79 70 65 74 6f 6b 65 6e 22 2c 20 20 20 20 20  typetoken",     
26791 22 74 79 70 65 6e 61 6d 65 22 2c 20 20 20 20 0a  "typename",    .
26792 20 20 22 73 69 67 6e 65 64 22 2c 20 20 20 20 20    "signed",     
26793 20 20 20 22 70 6c 75 73 5f 6e 75 6d 22 2c 20 20     "plus_num",  
26794 20 20 20 20 22 6d 69 6e 75 73 5f 6e 75 6d 22 2c      "minus_num",
26795 20 20 20 20 20 22 63 61 72 67 22 2c 20 20 20 20       "carg",    
26796 20 20 20 20 0a 20 20 22 63 63 6f 6e 73 22 2c 20      .  "ccons", 
26797 20 20 20 20 20 20 20 20 22 74 65 72 6d 22 2c 20          "term", 
26798 20 20 20 20 20 20 20 20 20 22 65 78 70 72 22 2c           "expr",
26799 20 20 20 20 20 20 20 20 20 20 22 6f 6e 63 6f 6e            "oncon
2679a 66 22 2c 20 20 20 20 20 20 0a 20 20 22 73 6f 72  f",      .  "sor
2679b 74 6f 72 64 65 72 22 2c 20 20 20 20 20 22 61 75  torder",     "au
2679c 74 6f 69 6e 63 22 2c 20 20 20 20 20 20 20 22 69  toinc",       "i
2679d 64 78 6c 69 73 74 5f 6f 70 74 22 2c 20 20 20 22  dxlist_opt",   "
2679e 72 65 66 61 72 67 73 22 2c 20 20 20 20 20 0a 20  refargs",     . 
2679f 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73   "defer_subclaus
267a0 65 22 2c 20 20 22 72 65 66 61 72 67 22 2c 20 20  e",  "refarg",  
267a1 20 20 20 20 20 20 22 72 65 66 61 63 74 22 2c 20        "refact", 
267a2 20 20 20 20 20 20 20 22 69 6e 69 74 5f 64 65 66         "init_def
267a3 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 22 2c  erred_pred_opt",
267a4 0a 20 20 22 63 6f 6e 73 6c 69 73 74 22 2c 20 20  .  "conslist",  
267a5 20 20 20 20 22 74 63 6f 6e 73 22 2c 20 20 20 20      "tcons",    
267a6 20 20 20 20 20 22 69 64 78 6c 69 73 74 22 2c 20       "idxlist", 
267a7 20 20 20 20 20 20 22 64 65 66 65 72 5f 73 75 62        "defer_sub
267a8 63 6c 61 75 73 65 5f 6f 70 74 22 2c 0a 20 20 22  clause_opt",.  "
267a9 6f 72 63 6f 6e 66 22 2c 20 20 20 20 20 20 20 20  orconf",        
267aa 22 72 65 73 6f 6c 76 65 74 79 70 65 22 2c 20 20  "resolvetype",  
267ab 20 22 72 61 69 73 65 74 79 70 65 22 2c 20 20 20   "raisetype",   
267ac 20 20 22 69 66 65 78 69 73 74 73 22 2c 20 20 20    "ifexists",   
267ad 20 0a 20 20 22 66 75 6c 6c 6e 61 6d 65 22 2c 20   .  "fullname", 
267ae 20 20 20 20 20 22 6f 6e 65 73 65 6c 65 63 74 22       "oneselect"
267af 2c 20 20 20 20 20 22 6d 75 6c 74 69 73 65 6c 65  ,     "multisele
267b0 63 74 5f 6f 70 22 2c 20 20 22 64 69 73 74 69 6e  ct_op",  "distin
267b1 63 74 22 2c 20 20 20 20 0a 20 20 22 73 65 6c 63  ct",    .  "selc
267b2 6f 6c 6c 69 73 74 22 2c 20 20 20 20 22 66 72 6f  ollist",    "fro
267b3 6d 22 2c 20 20 20 20 20 20 20 20 20 20 22 77 68  m",          "wh
267b4 65 72 65 5f 6f 70 74 22 2c 20 20 20 20 20 22 67  ere_opt",     "g
267b5 72 6f 75 70 62 79 5f 6f 70 74 22 2c 20 0a 20 20  roupby_opt", .  
267b6 22 68 61 76 69 6e 67 5f 6f 70 74 22 2c 20 20 20  "having_opt",   
267b7 20 22 6f 72 64 65 72 62 79 5f 6f 70 74 22 2c 20   "orderby_opt", 
267b8 20 20 22 6c 69 6d 69 74 5f 6f 70 74 22 2c 20 20    "limit_opt",  
267b9 20 20 20 22 73 63 6c 70 22 2c 20 20 20 20 20 20     "sclp",      
267ba 20 20 0a 20 20 22 61 73 22 2c 20 20 20 20 20 20    .  "as",      
267bb 20 20 20 20 20 20 22 73 65 6c 74 61 62 6c 69 73        "seltablis
267bc 74 22 2c 20 20 20 20 22 73 74 6c 5f 70 72 65 66  t",    "stl_pref
267bd 69 78 22 2c 20 20 20 20 22 6a 6f 69 6e 6f 70 22  ix",    "joinop"
267be 2c 20 20 20 20 20 20 0a 20 20 22 6f 6e 5f 6f 70  ,      .  "on_op
267bf 74 22 2c 20 20 20 20 20 20 20 20 22 75 73 69 6e  t",        "usin
267c0 67 5f 6f 70 74 22 2c 20 20 20 20 20 22 73 65 6c  g_opt",     "sel
267c1 74 61 62 6c 69 73 74 5f 70 61 72 65 6e 22 2c 20  tablist_paren", 
267c2 20 22 6a 6f 69 6e 6f 70 32 22 2c 20 20 20 20 20   "joinop2",     
267c3 0a 20 20 22 69 6e 73 63 6f 6c 6c 69 73 74 22 2c  .  "inscollist",
267c4 20 20 20 20 22 73 6f 72 74 6c 69 73 74 22 2c 20      "sortlist", 
267c5 20 20 20 20 20 22 73 6f 72 74 69 74 65 6d 22 2c       "sortitem",
267c6 20 20 20 20 20 20 22 6e 65 78 70 72 6c 69 73 74        "nexprlist
267c7 22 2c 20 20 20 0a 20 20 22 73 65 74 6c 69 73 74  ",   .  "setlist
267c8 22 2c 20 20 20 20 20 20 20 22 69 6e 73 65 72 74  ",       "insert
267c9 5f 63 6d 64 22 2c 20 20 20 20 22 69 6e 73 63 6f  _cmd",    "insco
267ca 6c 6c 69 73 74 5f 6f 70 74 22 2c 20 20 22 69 74  llist_opt",  "it
267cb 65 6d 6c 69 73 74 22 2c 20 20 20 20 0a 20 20 22  emlist",    .  "
267cc 65 78 70 72 6c 69 73 74 22 2c 20 20 20 20 20 20  exprlist",      
267cd 22 6c 69 6b 65 6f 70 22 2c 20 20 20 20 20 20 20  "likeop",       
267ce 20 22 65 73 63 61 70 65 22 2c 20 20 20 20 20 20   "escape",      
267cf 20 20 22 62 65 74 77 65 65 6e 5f 6f 70 22 2c 20    "between_op", 
267d0 20 0a 20 20 22 69 6e 5f 6f 70 22 2c 20 20 20 20   .  "in_op",    
267d1 20 20 20 20 20 22 63 61 73 65 5f 6f 70 65 72 61       "case_opera
267d2 6e 64 22 2c 20 20 22 63 61 73 65 5f 65 78 70 72  nd",  "case_expr
267d3 6c 69 73 74 22 2c 20 20 22 63 61 73 65 5f 65 6c  list",  "case_el
267d4 73 65 22 2c 20 20 20 0a 20 20 22 75 6e 69 71 75  se",   .  "uniqu
267d5 65 66 6c 61 67 22 2c 20 20 20 20 22 69 64 78 69  eflag",    "idxi
267d6 74 65 6d 22 2c 20 20 20 20 20 20 20 22 63 6f 6c  tem",       "col
267d7 6c 61 74 65 22 2c 20 20 20 20 20 20 20 22 6e 6d  late",       "nm
267d8 6e 75 6d 22 2c 20 20 20 20 20 20 20 0a 20 20 22  num",       .  "
267d9 70 6c 75 73 5f 6f 70 74 22 2c 20 20 20 20 20 20  plus_opt",      
267da 22 6e 75 6d 62 65 72 22 2c 20 20 20 20 20 20 20  "number",       
267db 20 22 74 72 69 67 67 65 72 5f 64 65 63 6c 22 2c   "trigger_decl",
267dc 20 20 22 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c    "trigger_cmd_l
267dd 69 73 74 22 2c 0a 20 20 22 74 72 69 67 67 65 72  ist",.  "trigger
267de 5f 74 69 6d 65 22 2c 20 20 22 74 72 69 67 67 65  _time",  "trigge
267df 72 5f 65 76 65 6e 74 22 2c 20 20 22 66 6f 72 65  r_event",  "fore
267e0 61 63 68 5f 63 6c 61 75 73 65 22 2c 20 20 22 77  ach_clause",  "w
267e1 68 65 6e 5f 63 6c 61 75 73 65 22 2c 20 0a 20 20  hen_clause", .  
267e2 22 74 72 69 67 67 65 72 5f 63 6d 64 22 2c 20 20  "trigger_cmd",  
267e3 20 22 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70   "database_kw_op
267e4 74 22 2c 20 20 22 6b 65 79 5f 6f 70 74 22 2c 20  t",  "key_opt", 
267e5 20 20 20 20 20 20 22 61 64 64 5f 63 6f 6c 75 6d        "add_colum
267e6 6e 5f 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 20 22  n_fullname",.  "
267e7 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 22 2c 20 20  kwcolumn_opt",  
267e8 22 63 72 65 61 74 65 5f 76 74 61 62 22 2c 20 20  "create_vtab",  
267e9 20 22 76 74 61 62 61 72 67 6c 69 73 74 22 2c 20   "vtabarglist", 
267ea 20 20 22 76 74 61 62 61 72 67 22 2c 20 20 20 20    "vtabarg",    
267eb 20 0a 20 20 22 76 74 61 62 61 72 67 74 6f 6b 65   .  "vtabargtoke
267ec 6e 22 2c 20 20 22 6c 70 22 2c 20 20 20 20 20 20  n",  "lp",      
267ed 20 20 20 20 20 20 22 61 6e 79 6c 69 73 74 22 2c        "anylist",
267ee 20 20 20 20 20 0a 7d 3b 0a 23 65 6e 64 69 66 20       .};.#endif 
267ef 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 23 69  /* NDEBUG */..#i
267f0 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 20  fndef NDEBUG./* 
267f1 46 6f 72 20 74 72 61 63 69 6e 67 20 72 65 64 75  For tracing redu
267f2 63 65 20 61 63 74 69 6f 6e 73 2c 20 74 68 65 20  ce actions, the 
267f3 6e 61 6d 65 73 20 6f 66 20 61 6c 6c 20 72 75 6c  names of all rul
267f4 65 73 20 61 72 65 20 72 65 71 75 69 72 65 64 2e  es are required.
267f5 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
267f6 20 63 68 61 72 20 2a 63 6f 6e 73 74 20 79 79 52   char *const yyR
267f7 75 6c 65 4e 61 6d 65 5b 5d 20 3d 20 7b 0a 20 2f  uleName[] = {. /
267f8 2a 20 20 20 30 20 2a 2f 20 22 69 6e 70 75 74 20  *   0 */ "input 
267f9 3a 3a 3d 20 63 6d 64 6c 69 73 74 22 2c 0a 20 2f  ::= cmdlist",. /
267fa 2a 20 20 20 31 20 2a 2f 20 22 63 6d 64 6c 69 73  *   1 */ "cmdlis
267fb 74 20 3a 3a 3d 20 63 6d 64 6c 69 73 74 20 65 63  t ::= cmdlist ec
267fc 6d 64 22 2c 0a 20 2f 2a 20 20 20 32 20 2a 2f 20  md",. /*   2 */ 
267fd 22 63 6d 64 6c 69 73 74 20 3a 3a 3d 20 65 63 6d  "cmdlist ::= ecm
267fe 64 22 2c 0a 20 2f 2a 20 20 20 33 20 2a 2f 20 22  d",. /*   3 */ "
267ff 63 6d 64 78 20 3a 3a 3d 20 63 6d 64 22 2c 0a 20  cmdx ::= cmd",. 
26800 2f 2a 20 20 20 34 20 2a 2f 20 22 65 63 6d 64 20  /*   4 */ "ecmd 
26801 3a 3a 3d 20 53 45 4d 49 22 2c 0a 20 2f 2a 20 20  ::= SEMI",. /*  
26802 20 35 20 2a 2f 20 22 65 63 6d 64 20 3a 3a 3d 20   5 */ "ecmd ::= 
26803 65 78 70 6c 61 69 6e 20 63 6d 64 78 20 53 45 4d  explain cmdx SEM
26804 49 22 2c 0a 20 2f 2a 20 20 20 36 20 2a 2f 20 22  I",. /*   6 */ "
26805 65 78 70 6c 61 69 6e 20 3a 3a 3d 22 2c 0a 20 2f  explain ::=",. /
26806 2a 20 20 20 37 20 2a 2f 20 22 65 78 70 6c 61 69  *   7 */ "explai
26807 6e 20 3a 3a 3d 20 45 58 50 4c 41 49 4e 22 2c 0a  n ::= EXPLAIN",.
26808 20 2f 2a 20 20 20 38 20 2a 2f 20 22 65 78 70 6c   /*   8 */ "expl
26809 61 69 6e 20 3a 3a 3d 20 45 58 50 4c 41 49 4e 20  ain ::= EXPLAIN 
2680a 51 55 45 52 59 20 50 4c 41 4e 22 2c 0a 20 2f 2a  QUERY PLAN",. /*
2680b 20 20 20 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d     9 */ "cmd ::=
2680c 20 42 45 47 49 4e 20 74 72 61 6e 73 74 79 70 65   BEGIN transtype
2680d 20 74 72 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a   trans_opt",. /*
2680e 20 20 31 30 20 2a 2f 20 22 74 72 61 6e 73 5f 6f    10 */ "trans_o
2680f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 31 31  pt ::=",. /*  11
26810 20 2a 2f 20 22 74 72 61 6e 73 5f 6f 70 74 20 3a   */ "trans_opt :
26811 3a 3d 20 54 52 41 4e 53 41 43 54 49 4f 4e 22 2c  := TRANSACTION",
26812 0a 20 2f 2a 20 20 31 32 20 2a 2f 20 22 74 72 61  . /*  12 */ "tra
26813 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54 52 41 4e 53  ns_opt ::= TRANS
26814 41 43 54 49 4f 4e 20 6e 6d 22 2c 0a 20 2f 2a 20  ACTION nm",. /* 
26815 20 31 33 20 2a 2f 20 22 74 72 61 6e 73 74 79 70   13 */ "transtyp
26816 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 31 34 20  e ::=",. /*  14 
26817 2a 2f 20 22 74 72 61 6e 73 74 79 70 65 20 3a 3a  */ "transtype ::
26818 3d 20 44 45 46 45 52 52 45 44 22 2c 0a 20 2f 2a  = DEFERRED",. /*
26819 20 20 31 35 20 2a 2f 20 22 74 72 61 6e 73 74 79    15 */ "transty
2681a 70 65 20 3a 3a 3d 20 49 4d 4d 45 44 49 41 54 45  pe ::= IMMEDIATE
2681b 22 2c 0a 20 2f 2a 20 20 31 36 20 2a 2f 20 22 74  ",. /*  16 */ "t
2681c 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 45 58 43  ranstype ::= EXC
2681d 4c 55 53 49 56 45 22 2c 0a 20 2f 2a 20 20 31 37  LUSIVE",. /*  17
2681e 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 43 4f 4d   */ "cmd ::= COM
2681f 4d 49 54 20 74 72 61 6e 73 5f 6f 70 74 22 2c 0a  MIT trans_opt",.
26820 20 2f 2a 20 20 31 38 20 2a 2f 20 22 63 6d 64 20   /*  18 */ "cmd 
26821 3a 3a 3d 20 45 4e 44 20 74 72 61 6e 73 5f 6f 70  ::= END trans_op
26822 74 22 2c 0a 20 2f 2a 20 20 31 39 20 2a 2f 20 22  t",. /*  19 */ "
26823 63 6d 64 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b  cmd ::= ROLLBACK
26824 20 74 72 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a   trans_opt",. /*
26825 20 20 32 30 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d    20 */ "cmd ::=
26826 20 63 72 65 61 74 65 5f 74 61 62 6c 65 20 63 72   create_table cr
26827 65 61 74 65 5f 74 61 62 6c 65 5f 61 72 67 73 22  eate_table_args"
26828 2c 0a 20 2f 2a 20 20 32 31 20 2a 2f 20 22 63 72  ,. /*  21 */ "cr
26829 65 61 74 65 5f 74 61 62 6c 65 20 3a 3a 3d 20 43  eate_table ::= C
2682a 52 45 41 54 45 20 74 65 6d 70 20 54 41 42 4c 45  REATE temp TABLE
2682b 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20   ifnotexists nm 
2682c 64 62 6e 6d 22 2c 0a 20 2f 2a 20 20 32 32 20 2a  dbnm",. /*  22 *
2682d 2f 20 22 69 66 6e 6f 74 65 78 69 73 74 73 20 3a  / "ifnotexists :
2682e 3a 3d 22 2c 0a 20 2f 2a 20 20 32 33 20 2a 2f 20  :=",. /*  23 */ 
2682f 22 69 66 6e 6f 74 65 78 69 73 74 73 20 3a 3a 3d  "ifnotexists ::=
26830 20 49 46 20 4e 4f 54 20 45 58 49 53 54 53 22 2c   IF NOT EXISTS",
26831 0a 20 2f 2a 20 20 32 34 20 2a 2f 20 22 74 65 6d  . /*  24 */ "tem
26832 70 20 3a 3a 3d 20 54 45 4d 50 22 2c 0a 20 2f 2a  p ::= TEMP",. /*
26833 20 20 32 35 20 2a 2f 20 22 74 65 6d 70 20 3a 3a    25 */ "temp ::
26834 3d 22 2c 0a 20 2f 2a 20 20 32 36 20 2a 2f 20 22  =",. /*  26 */ "
26835 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61 72 67  create_table_arg
26836 73 20 3a 3a 3d 20 4c 50 20 63 6f 6c 75 6d 6e 6c  s ::= LP columnl
26837 69 73 74 20 63 6f 6e 73 6c 69 73 74 5f 6f 70 74  ist conslist_opt
26838 20 52 50 22 2c 0a 20 2f 2a 20 20 32 37 20 2a 2f   RP",. /*  27 */
26839 20 22 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61   "create_table_a
2683a 72 67 73 20 3a 3a 3d 20 41 53 20 73 65 6c 65 63  rgs ::= AS selec
2683b 74 22 2c 0a 20 2f 2a 20 20 32 38 20 2a 2f 20 22  t",. /*  28 */ "
2683c 63 6f 6c 75 6d 6e 6c 69 73 74 20 3a 3a 3d 20 63  columnlist ::= c
2683d 6f 6c 75 6d 6e 6c 69 73 74 20 43 4f 4d 4d 41 20  olumnlist COMMA 
2683e 63 6f 6c 75 6d 6e 22 2c 0a 20 2f 2a 20 20 32 39  column",. /*  29
2683f 20 2a 2f 20 22 63 6f 6c 75 6d 6e 6c 69 73 74 20   */ "columnlist 
26840 3a 3a 3d 20 63 6f 6c 75 6d 6e 22 2c 0a 20 2f 2a  ::= column",. /*
26841 20 20 33 30 20 2a 2f 20 22 63 6f 6c 75 6d 6e 20    30 */ "column 
26842 3a 3a 3d 20 63 6f 6c 75 6d 6e 69 64 20 74 79 70  ::= columnid typ
26843 65 20 63 61 72 67 6c 69 73 74 22 2c 0a 20 2f 2a  e carglist",. /*
26844 20 20 33 31 20 2a 2f 20 22 63 6f 6c 75 6d 6e 69    31 */ "columni
26845 64 20 3a 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a 20 20  d ::= nm",. /*  
26846 33 32 20 2a 2f 20 22 69 64 20 3a 3a 3d 20 49 44  32 */ "id ::= ID
26847 22 2c 0a 20 2f 2a 20 20 33 33 20 2a 2f 20 22 69  ",. /*  33 */ "i
26848 64 73 20 3a 3a 3d 20 49 44 7c 53 54 52 49 4e 47  ds ::= ID|STRING
26849 22 2c 0a 20 2f 2a 20 20 33 34 20 2a 2f 20 22 6e  ",. /*  34 */ "n
2684a 6d 20 3a 3a 3d 20 49 44 22 2c 0a 20 2f 2a 20 20  m ::= ID",. /*  
2684b 33 35 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 53 54  35 */ "nm ::= ST
2684c 52 49 4e 47 22 2c 0a 20 2f 2a 20 20 33 36 20 2a  RING",. /*  36 *
2684d 2f 20 22 6e 6d 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b  / "nm ::= JOIN_K
2684e 57 22 2c 0a 20 2f 2a 20 20 33 37 20 2a 2f 20 22  W",. /*  37 */ "
2684f 74 79 70 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20  type ::=",. /*  
26850 33 38 20 2a 2f 20 22 74 79 70 65 20 3a 3a 3d 20  38 */ "type ::= 
26851 74 79 70 65 74 6f 6b 65 6e 22 2c 0a 20 2f 2a 20  typetoken",. /* 
26852 20 33 39 20 2a 2f 20 22 74 79 70 65 74 6f 6b 65   39 */ "typetoke
26853 6e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 22 2c  n ::= typename",
26854 0a 20 2f 2a 20 20 34 30 20 2a 2f 20 22 74 79 70  . /*  40 */ "typ
26855 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e  etoken ::= typen
26856 61 6d 65 20 4c 50 20 73 69 67 6e 65 64 20 52 50  ame LP signed RP
26857 22 2c 0a 20 2f 2a 20 20 34 31 20 2a 2f 20 22 74  ",. /*  41 */ "t
26858 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70  ypetoken ::= typ
26859 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e 65 64 20  ename LP signed 
2685a 43 4f 4d 4d 41 20 73 69 67 6e 65 64 20 52 50 22  COMMA signed RP"
2685b 2c 0a 20 2f 2a 20 20 34 32 20 2a 2f 20 22 74 79  ,. /*  42 */ "ty
2685c 70 65 6e 61 6d 65 20 3a 3a 3d 20 69 64 73 22 2c  pename ::= ids",
2685d 0a 20 2f 2a 20 20 34 33 20 2a 2f 20 22 74 79 70  . /*  43 */ "typ
2685e 65 6e 61 6d 65 20 3a 3a 3d 20 74 79 70 65 6e 61  ename ::= typena
2685f 6d 65 20 69 64 73 22 2c 0a 20 2f 2a 20 20 34 34  me ids",. /*  44
26860 20 2a 2f 20 22 73 69 67 6e 65 64 20 3a 3a 3d 20   */ "signed ::= 
26861 70 6c 75 73 5f 6e 75 6d 22 2c 0a 20 2f 2a 20 20  plus_num",. /*  
26862 34 35 20 2a 2f 20 22 73 69 67 6e 65 64 20 3a 3a  45 */ "signed ::
26863 3d 20 6d 69 6e 75 73 5f 6e 75 6d 22 2c 0a 20 2f  = minus_num",. /
26864 2a 20 20 34 36 20 2a 2f 20 22 63 61 72 67 6c 69  *  46 */ "cargli
26865 73 74 20 3a 3a 3d 20 63 61 72 67 6c 69 73 74 20  st ::= carglist 
26866 63 61 72 67 22 2c 0a 20 2f 2a 20 20 34 37 20 2a  carg",. /*  47 *
26867 2f 20 22 63 61 72 67 6c 69 73 74 20 3a 3a 3d 22  / "carglist ::="
26868 2c 0a 20 2f 2a 20 20 34 38 20 2a 2f 20 22 63 61  ,. /*  48 */ "ca
26869 72 67 20 3a 3a 3d 20 43 4f 4e 53 54 52 41 49 4e  rg ::= CONSTRAIN
2686a 54 20 6e 6d 20 63 63 6f 6e 73 22 2c 0a 20 2f 2a  T nm ccons",. /*
2686b 20 20 34 39 20 2a 2f 20 22 63 61 72 67 20 3a 3a    49 */ "carg ::
2686c 3d 20 63 63 6f 6e 73 22 2c 0a 20 2f 2a 20 20 35  = ccons",. /*  5
2686d 30 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20  0 */ "ccons ::= 
2686e 44 45 46 41 55 4c 54 20 74 65 72 6d 22 2c 0a 20  DEFAULT term",. 
2686f 2f 2a 20 20 35 31 20 2a 2f 20 22 63 63 6f 6e 73  /*  51 */ "ccons
26870 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 4c 50 20   ::= DEFAULT LP 
26871 65 78 70 72 20 52 50 22 2c 0a 20 2f 2a 20 20 35  expr RP",. /*  5
26872 32 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20  2 */ "ccons ::= 
26873 44 45 46 41 55 4c 54 20 50 4c 55 53 20 74 65 72  DEFAULT PLUS ter
26874 6d 22 2c 0a 20 2f 2a 20 20 35 33 20 2a 2f 20 22  m",. /*  53 */ "
26875 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c  ccons ::= DEFAUL
26876 54 20 4d 49 4e 55 53 20 74 65 72 6d 22 2c 0a 20  T MINUS term",. 
26877 2f 2a 20 20 35 34 20 2a 2f 20 22 63 63 6f 6e 73  /*  54 */ "ccons
26878 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 69 64 22   ::= DEFAULT id"
26879 2c 0a 20 2f 2a 20 20 35 35 20 2a 2f 20 22 63 63  ,. /*  55 */ "cc
2687a 6f 6e 73 20 3a 3a 3d 20 4e 55 4c 4c 20 6f 6e 63  ons ::= NULL onc
2687b 6f 6e 66 22 2c 0a 20 2f 2a 20 20 35 36 20 2a 2f  onf",. /*  56 */
2687c 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 4e 4f 54 20   "ccons ::= NOT 
2687d 4e 55 4c 4c 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f  NULL onconf",. /
2687e 2a 20 20 35 37 20 2a 2f 20 22 63 63 6f 6e 73 20  *  57 */ "ccons 
2687f 3a 3a 3d 20 50 52 49 4d 41 52 59 20 4b 45 59 20  ::= PRIMARY KEY 
26880 73 6f 72 74 6f 72 64 65 72 20 6f 6e 63 6f 6e 66  sortorder onconf
26881 20 61 75 74 6f 69 6e 63 22 2c 0a 20 2f 2a 20 20   autoinc",. /*  
26882 35 38 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d  58 */ "ccons ::=
26883 20 55 4e 49 51 55 45 20 6f 6e 63 6f 6e 66 22 2c   UNIQUE onconf",
26884 0a 20 2f 2a 20 20 35 39 20 2a 2f 20 22 63 63 6f  . /*  59 */ "cco
26885 6e 73 20 3a 3a 3d 20 43 48 45 43 4b 20 4c 50 20  ns ::= CHECK LP 
26886 65 78 70 72 20 52 50 22 2c 0a 20 2f 2a 20 20 36  expr RP",. /*  6
26887 30 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20  0 */ "ccons ::= 
26888 52 45 46 45 52 45 4e 43 45 53 20 6e 6d 20 69 64  REFERENCES nm id
26889 78 6c 69 73 74 5f 6f 70 74 20 72 65 66 61 72 67  xlist_opt refarg
2688a 73 22 2c 0a 20 2f 2a 20 20 36 31 20 2a 2f 20 22  s",. /*  61 */ "
2688b 63 63 6f 6e 73 20 3a 3a 3d 20 64 65 66 65 72 5f  ccons ::= defer_
2688c 73 75 62 63 6c 61 75 73 65 22 2c 0a 20 2f 2a 20  subclause",. /* 
2688d 20 36 32 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a   62 */ "ccons ::
2688e 3d 20 43 4f 4c 4c 41 54 45 20 69 64 73 22 2c 0a  = COLLATE ids",.
2688f 20 2f 2a 20 20 36 33 20 2a 2f 20 22 61 75 74 6f   /*  63 */ "auto
26890 69 6e 63 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 36  inc ::=",. /*  6
26891 34 20 2a 2f 20 22 61 75 74 6f 69 6e 63 20 3a 3a  4 */ "autoinc ::
26892 3d 20 41 55 54 4f 49 4e 43 52 22 2c 0a 20 2f 2a  = AUTOINCR",. /*
26893 20 20 36 35 20 2a 2f 20 22 72 65 66 61 72 67 73    65 */ "refargs
26894 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 36 36 20 2a   ::=",. /*  66 *
26895 2f 20 22 72 65 66 61 72 67 73 20 3a 3a 3d 20 72  / "refargs ::= r
26896 65 66 61 72 67 73 20 72 65 66 61 72 67 22 2c 0a  efargs refarg",.
26897 20 2f 2a 20 20 36 37 20 2a 2f 20 22 72 65 66 61   /*  67 */ "refa
26898 72 67 20 3a 3a 3d 20 4d 41 54 43 48 20 6e 6d 22  rg ::= MATCH nm"
26899 2c 0a 20 2f 2a 20 20 36 38 20 2a 2f 20 22 72 65  ,. /*  68 */ "re
2689a 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 44 45 4c 45  farg ::= ON DELE
2689b 54 45 20 72 65 66 61 63 74 22 2c 0a 20 2f 2a 20  TE refact",. /* 
2689c 20 36 39 20 2a 2f 20 22 72 65 66 61 72 67 20 3a   69 */ "refarg :
2689d 3a 3d 20 4f 4e 20 55 50 44 41 54 45 20 72 65 66  := ON UPDATE ref
2689e 61 63 74 22 2c 0a 20 2f 2a 20 20 37 30 20 2a 2f  act",. /*  70 */
2689f 20 22 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20   "refarg ::= ON 
268a0 49 4e 53 45 52 54 20 72 65 66 61 63 74 22 2c 0a  INSERT refact",.
268a1 20 2f 2a 20 20 37 31 20 2a 2f 20 22 72 65 66 61   /*  71 */ "refa
268a2 63 74 20 3a 3a 3d 20 53 45 54 20 4e 55 4c 4c 22  ct ::= SET NULL"
268a3 2c 0a 20 2f 2a 20 20 37 32 20 2a 2f 20 22 72 65  ,. /*  72 */ "re
268a4 66 61 63 74 20 3a 3a 3d 20 53 45 54 20 44 45 46  fact ::= SET DEF
268a5 41 55 4c 54 22 2c 0a 20 2f 2a 20 20 37 33 20 2a  AULT",. /*  73 *
268a6 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d 20 43 41  / "refact ::= CA
268a7 53 43 41 44 45 22 2c 0a 20 2f 2a 20 20 37 34 20  SCADE",. /*  74 
268a8 2a 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d 20 52  */ "refact ::= R
268a9 45 53 54 52 49 43 54 22 2c 0a 20 2f 2a 20 20 37  ESTRICT",. /*  7
268aa 35 20 2a 2f 20 22 64 65 66 65 72 5f 73 75 62 63  5 */ "defer_subc
268ab 6c 61 75 73 65 20 3a 3a 3d 20 4e 4f 54 20 44 45  lause ::= NOT DE
268ac 46 45 52 52 41 42 4c 45 20 69 6e 69 74 5f 64 65  FERRABLE init_de
268ad 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 22  ferred_pred_opt"
268ae 2c 0a 20 2f 2a 20 20 37 36 20 2a 2f 20 22 64 65  ,. /*  76 */ "de
268af 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20 3a 3a  fer_subclause ::
268b0 3d 20 44 45 46 45 52 52 41 42 4c 45 20 69 6e 69  = DEFERRABLE ini
268b1 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f  t_deferred_pred_
268b2 6f 70 74 22 2c 0a 20 2f 2a 20 20 37 37 20 2a 2f  opt",. /*  77 */
268b3 20 22 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f   "init_deferred_
268b4 70 72 65 64 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  pred_opt ::=",. 
268b5 2f 2a 20 20 37 38 20 2a 2f 20 22 69 6e 69 74 5f  /*  78 */ "init_
268b6 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70  deferred_pred_op
268b7 74 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59 20  t ::= INITIALLY 
268b8 44 45 46 45 52 52 45 44 22 2c 0a 20 2f 2a 20 20  DEFERRED",. /*  
268b9 37 39 20 2a 2f 20 22 69 6e 69 74 5f 64 65 66 65  79 */ "init_defe
268ba 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a  rred_pred_opt ::
268bb 3d 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45  = INITIALLY IMME
268bc 44 49 41 54 45 22 2c 0a 20 2f 2a 20 20 38 30 20  DIATE",. /*  80 
268bd 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 5f 6f 70 74  */ "conslist_opt
268be 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 38 31 20 2a   ::=",. /*  81 *
268bf 2f 20 22 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 20  / "conslist_opt 
268c0 3a 3a 3d 20 43 4f 4d 4d 41 20 63 6f 6e 73 6c 69  ::= COMMA consli
268c1 73 74 22 2c 0a 20 2f 2a 20 20 38 32 20 2a 2f 20  st",. /*  82 */ 
268c2 22 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 63 6f  "conslist ::= co
268c3 6e 73 6c 69 73 74 20 43 4f 4d 4d 41 20 74 63 6f  nslist COMMA tco
268c4 6e 73 22 2c 0a 20 2f 2a 20 20 38 33 20 2a 2f 20  ns",. /*  83 */ 
268c5 22 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 63 6f  "conslist ::= co
268c6 6e 73 6c 69 73 74 20 74 63 6f 6e 73 22 2c 0a 20  nslist tcons",. 
268c7 2f 2a 20 20 38 34 20 2a 2f 20 22 63 6f 6e 73 6c  /*  84 */ "consl
268c8 69 73 74 20 3a 3a 3d 20 74 63 6f 6e 73 22 2c 0a  ist ::= tcons",.
268c9 20 2f 2a 20 20 38 35 20 2a 2f 20 22 74 63 6f 6e   /*  85 */ "tcon
268ca 73 20 3a 3a 3d 20 43 4f 4e 53 54 52 41 49 4e 54  s ::= CONSTRAINT
268cb 20 6e 6d 22 2c 0a 20 2f 2a 20 20 38 36 20 2a 2f   nm",. /*  86 */
268cc 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d   "tcons ::= PRIM
268cd 41 52 59 20 4b 45 59 20 4c 50 20 69 64 78 6c 69  ARY KEY LP idxli
268ce 73 74 20 61 75 74 6f 69 6e 63 20 52 50 20 6f 6e  st autoinc RP on
268cf 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 38 37 20 2a  conf",. /*  87 *
268d0 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49  / "tcons ::= UNI
268d1 51 55 45 20 4c 50 20 69 64 78 6c 69 73 74 20 52  QUE LP idxlist R
268d2 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20  P onconf",. /*  
268d3 38 38 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d  88 */ "tcons ::=
268d4 20 43 48 45 43 4b 20 4c 50 20 65 78 70 72 20 52   CHECK LP expr R
268d5 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20  P onconf",. /*  
268d6 38 39 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d  89 */ "tcons ::=
268d7 20 46 4f 52 45 49 47 4e 20 4b 45 59 20 4c 50 20   FOREIGN KEY LP 
268d8 69 64 78 6c 69 73 74 20 52 50 20 52 45 46 45 52  idxlist RP REFER
268d9 45 4e 43 45 53 20 6e 6d 20 69 64 78 6c 69 73 74  ENCES nm idxlist
268da 5f 6f 70 74 20 72 65 66 61 72 67 73 20 64 65 66  _opt refargs def
268db 65 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74  er_subclause_opt
268dc 22 2c 0a 20 2f 2a 20 20 39 30 20 2a 2f 20 22 64  ",. /*  90 */ "d
268dd 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f  efer_subclause_o
268de 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 39 31  pt ::=",. /*  91
268df 20 2a 2f 20 22 64 65 66 65 72 5f 73 75 62 63 6c   */ "defer_subcl
268e0 61 75 73 65 5f 6f 70 74 20 3a 3a 3d 20 64 65 66  ause_opt ::= def
268e1 65 72 5f 73 75 62 63 6c 61 75 73 65 22 2c 0a 20  er_subclause",. 
268e2 2f 2a 20 20 39 32 20 2a 2f 20 22 6f 6e 63 6f 6e  /*  92 */ "oncon
268e3 66 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 39 33 20  f ::=",. /*  93 
268e4 2a 2f 20 22 6f 6e 63 6f 6e 66 20 3a 3a 3d 20 4f  */ "onconf ::= O
268e5 4e 20 43 4f 4e 46 4c 49 43 54 20 72 65 73 6f 6c  N CONFLICT resol
268e6 76 65 74 79 70 65 22 2c 0a 20 2f 2a 20 20 39 34  vetype",. /*  94
268e7 20 2a 2f 20 22 6f 72 63 6f 6e 66 20 3a 3a 3d 22   */ "orconf ::="
268e8 2c 0a 20 2f 2a 20 20 39 35 20 2a 2f 20 22 6f 72  ,. /*  95 */ "or
268e9 63 6f 6e 66 20 3a 3a 3d 20 4f 52 20 72 65 73 6f  conf ::= OR reso
268ea 6c 76 65 74 79 70 65 22 2c 0a 20 2f 2a 20 20 39  lvetype",. /*  9
268eb 36 20 2a 2f 20 22 72 65 73 6f 6c 76 65 74 79 70  6 */ "resolvetyp
268ec 65 20 3a 3a 3d 20 72 61 69 73 65 74 79 70 65 22  e ::= raisetype"
268ed 2c 0a 20 2f 2a 20 20 39 37 20 2a 2f 20 22 72 65  ,. /*  97 */ "re
268ee 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d 20 49 47  solvetype ::= IG
268ef 4e 4f 52 45 22 2c 0a 20 2f 2a 20 20 39 38 20 2a  NORE",. /*  98 *
268f0 2f 20 22 72 65 73 6f 6c 76 65 74 79 70 65 20 3a  / "resolvetype :
268f1 3a 3d 20 52 45 50 4c 41 43 45 22 2c 0a 20 2f 2a  := REPLACE",. /*
268f2 20 20 39 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d    99 */ "cmd ::=
268f3 20 44 52 4f 50 20 54 41 42 4c 45 20 69 66 65 78   DROP TABLE ifex
268f4 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a  ists fullname",.
268f5 20 2f 2a 20 31 30 30 20 2a 2f 20 22 69 66 65 78   /* 100 */ "ifex
268f6 69 73 74 73 20 3a 3a 3d 20 49 46 20 45 58 49 53  ists ::= IF EXIS
268f7 54 53 22 2c 0a 20 2f 2a 20 31 30 31 20 2a 2f 20  TS",. /* 101 */ 
268f8 22 69 66 65 78 69 73 74 73 20 3a 3a 3d 22 2c 0a  "ifexists ::=",.
268f9 20 2f 2a 20 31 30 32 20 2a 2f 20 22 63 6d 64 20   /* 102 */ "cmd 
268fa 3a 3a 3d 20 43 52 45 41 54 45 20 74 65 6d 70 20  ::= CREATE temp 
268fb 56 49 45 57 20 69 66 6e 6f 74 65 78 69 73 74 73  VIEW ifnotexists
268fc 20 6e 6d 20 64 62 6e 6d 20 41 53 20 73 65 6c 65   nm dbnm AS sele
268fd 63 74 22 2c 0a 20 2f 2a 20 31 30 33 20 2a 2f 20  ct",. /* 103 */ 
268fe 22 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 56 49  "cmd ::= DROP VI
268ff 45 57 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c  EW ifexists full
26900 6e 61 6d 65 22 2c 0a 20 2f 2a 20 31 30 34 20 2a  name",. /* 104 *
26901 2f 20 22 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63  / "cmd ::= selec
26902 74 22 2c 0a 20 2f 2a 20 31 30 35 20 2a 2f 20 22  t",. /* 105 */ "
26903 73 65 6c 65 63 74 20 3a 3a 3d 20 6f 6e 65 73 65  select ::= onese
26904 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 30 36 20 2a  lect",. /* 106 *
26905 2f 20 22 73 65 6c 65 63 74 20 3a 3a 3d 20 73 65  / "select ::= se
26906 6c 65 63 74 20 6d 75 6c 74 69 73 65 6c 65 63 74  lect multiselect
26907 5f 6f 70 20 6f 6e 65 73 65 6c 65 63 74 22 2c 0a  _op oneselect",.
26908 20 2f 2a 20 31 30 37 20 2a 2f 20 22 6d 75 6c 74   /* 107 */ "mult
26909 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 55  iselect_op ::= U
2690a 4e 49 4f 4e 22 2c 0a 20 2f 2a 20 31 30 38 20 2a  NION",. /* 108 *
2690b 2f 20 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f  / "multiselect_o
2690c 70 20 3a 3a 3d 20 55 4e 49 4f 4e 20 41 4c 4c 22  p ::= UNION ALL"
2690d 2c 0a 20 2f 2a 20 31 30 39 20 2a 2f 20 22 6d 75  ,. /* 109 */ "mu
2690e 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d  ltiselect_op ::=
2690f 20 45 58 43 45 50 54 7c 49 4e 54 45 52 53 45 43   EXCEPT|INTERSEC
26910 54 22 2c 0a 20 2f 2a 20 31 31 30 20 2a 2f 20 22  T",. /* 110 */ "
26911 6f 6e 65 73 65 6c 65 63 74 20 3a 3a 3d 20 53 45  oneselect ::= SE
26912 4c 45 43 54 20 64 69 73 74 69 6e 63 74 20 73 65  LECT distinct se
26913 6c 63 6f 6c 6c 69 73 74 20 66 72 6f 6d 20 77 68  lcollist from wh
26914 65 72 65 5f 6f 70 74 20 67 72 6f 75 70 62 79 5f  ere_opt groupby_
26915 6f 70 74 20 68 61 76 69 6e 67 5f 6f 70 74 20 6f  opt having_opt o
26916 72 64 65 72 62 79 5f 6f 70 74 20 6c 69 6d 69 74  rderby_opt limit
26917 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 31 31 20 2a  _opt",. /* 111 *
26918 2f 20 22 64 69 73 74 69 6e 63 74 20 3a 3a 3d 20  / "distinct ::= 
26919 44 49 53 54 49 4e 43 54 22 2c 0a 20 2f 2a 20 31  DISTINCT",. /* 1
2691a 31 32 20 2a 2f 20 22 64 69 73 74 69 6e 63 74 20  12 */ "distinct 
2691b 3a 3a 3d 20 41 4c 4c 22 2c 0a 20 2f 2a 20 31 31  ::= ALL",. /* 11
2691c 33 20 2a 2f 20 22 64 69 73 74 69 6e 63 74 20 3a  3 */ "distinct :
2691d 3a 3d 22 2c 0a 20 2f 2a 20 31 31 34 20 2a 2f 20  :=",. /* 114 */ 
2691e 22 73 63 6c 70 20 3a 3a 3d 20 73 65 6c 63 6f 6c  "sclp ::= selcol
2691f 6c 69 73 74 20 43 4f 4d 4d 41 22 2c 0a 20 2f 2a  list COMMA",. /*
26920 20 31 31 35 20 2a 2f 20 22 73 63 6c 70 20 3a 3a   115 */ "sclp ::
26921 3d 22 2c 0a 20 2f 2a 20 31 31 36 20 2a 2f 20 22  =",. /* 116 */ "
26922 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73  selcollist ::= s
26923 63 6c 70 20 65 78 70 72 20 61 73 22 2c 0a 20 2f  clp expr as",. /
26924 2a 20 31 31 37 20 2a 2f 20 22 73 65 6c 63 6f 6c  * 117 */ "selcol
26925 6c 69 73 74 20 3a 3a 3d 20 73 63 6c 70 20 53 54  list ::= sclp ST
26926 41 52 22 2c 0a 20 2f 2a 20 31 31 38 20 2a 2f 20  AR",. /* 118 */ 
26927 22 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20  "selcollist ::= 
26928 73 63 6c 70 20 6e 6d 20 44 4f 54 20 53 54 41 52  sclp nm DOT STAR
26929 22 2c 0a 20 2f 2a 20 31 31 39 20 2a 2f 20 22 61  ",. /* 119 */ "a
2692a 73 20 3a 3a 3d 20 41 53 20 6e 6d 22 2c 0a 20 2f  s ::= AS nm",. /
2692b 2a 20 31 32 30 20 2a 2f 20 22 61 73 20 3a 3a 3d  * 120 */ "as ::=
2692c 20 69 64 73 22 2c 0a 20 2f 2a 20 31 32 31 20 2a   ids",. /* 121 *
2692d 2f 20 22 61 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  / "as ::=",. /* 
2692e 31 32 32 20 2a 2f 20 22 66 72 6f 6d 20 3a 3a 3d  122 */ "from ::=
2692f 22 2c 0a 20 2f 2a 20 31 32 33 20 2a 2f 20 22 66  ",. /* 123 */ "f
26930 72 6f 6d 20 3a 3a 3d 20 46 52 4f 4d 20 73 65 6c  rom ::= FROM sel
26931 74 61 62 6c 69 73 74 22 2c 0a 20 2f 2a 20 31 32  tablist",. /* 12
26932 34 20 2a 2f 20 22 73 74 6c 5f 70 72 65 66 69 78  4 */ "stl_prefix
26933 20 3a 3a 3d 20 73 65 6c 74 61 62 6c 69 73 74 20   ::= seltablist 
26934 6a 6f 69 6e 6f 70 22 2c 0a 20 2f 2a 20 31 32 35  joinop",. /* 125
26935 20 2a 2f 20 22 73 74 6c 5f 70 72 65 66 69 78 20   */ "stl_prefix 
26936 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 32 36 20 2a 2f  ::=",. /* 126 */
26937 20 22 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d   "seltablist ::=
26938 20 73 74 6c 5f 70 72 65 66 69 78 20 6e 6d 20 64   stl_prefix nm d
26939 62 6e 6d 20 61 73 20 6f 6e 5f 6f 70 74 20 75 73  bnm as on_opt us
2693a 69 6e 67 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 32  ing_opt",. /* 12
2693b 37 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69 73 74  7 */ "seltablist
2693c 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20   ::= stl_prefix 
2693d 4c 50 20 73 65 6c 74 61 62 6c 69 73 74 5f 70 61  LP seltablist_pa
2693e 72 65 6e 20 52 50 20 61 73 20 6f 6e 5f 6f 70 74  ren RP as on_opt
2693f 20 75 73 69 6e 67 5f 6f 70 74 22 2c 0a 20 2f 2a   using_opt",. /*
26940 20 31 32 38 20 2a 2f 20 22 73 65 6c 74 61 62 6c   128 */ "seltabl
26941 69 73 74 5f 70 61 72 65 6e 20 3a 3a 3d 20 73 65  ist_paren ::= se
26942 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 32 39 20 2a  lect",. /* 129 *
26943 2f 20 22 73 65 6c 74 61 62 6c 69 73 74 5f 70 61  / "seltablist_pa
26944 72 65 6e 20 3a 3a 3d 20 73 65 6c 74 61 62 6c 69  ren ::= seltabli
26945 73 74 22 2c 0a 20 2f 2a 20 31 33 30 20 2a 2f 20  st",. /* 130 */ 
26946 22 64 62 6e 6d 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  "dbnm ::=",. /* 
26947 31 33 31 20 2a 2f 20 22 64 62 6e 6d 20 3a 3a 3d  131 */ "dbnm ::=
26948 20 44 4f 54 20 6e 6d 22 2c 0a 20 2f 2a 20 31 33   DOT nm",. /* 13
26949 32 20 2a 2f 20 22 66 75 6c 6c 6e 61 6d 65 20 3a  2 */ "fullname :
2694a 3a 3d 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a  := nm dbnm",. /*
2694b 20 31 33 33 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20   133 */ "joinop 
2694c 3a 3a 3d 20 43 4f 4d 4d 41 7c 4a 4f 49 4e 22 2c  ::= COMMA|JOIN",
2694d 0a 20 2f 2a 20 31 33 34 20 2a 2f 20 22 6a 6f 69  . /* 134 */ "joi
2694e 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20  nop ::= JOIN_KW 
2694f 4a 4f 49 4e 22 2c 0a 20 2f 2a 20 31 33 35 20 2a  JOIN",. /* 135 *
26950 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f  / "joinop ::= JO
26951 49 4e 5f 4b 57 20 6e 6d 20 4a 4f 49 4e 22 2c 0a  IN_KW nm JOIN",.
26952 20 2f 2a 20 31 33 36 20 2a 2f 20 22 6a 6f 69 6e   /* 136 */ "join
26953 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 6e  op ::= JOIN_KW n
26954 6d 20 6e 6d 20 4a 4f 49 4e 22 2c 0a 20 2f 2a 20  m nm JOIN",. /* 
26955 31 33 37 20 2a 2f 20 22 6f 6e 5f 6f 70 74 20 3a  137 */ "on_opt :
26956 3a 3d 20 4f 4e 20 65 78 70 72 22 2c 0a 20 2f 2a  := ON expr",. /*
26957 20 31 33 38 20 2a 2f 20 22 6f 6e 5f 6f 70 74 20   138 */ "on_opt 
26958 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 33 39 20 2a 2f  ::=",. /* 139 */
26959 20 22 75 73 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20   "using_opt ::= 
2695a 55 53 49 4e 47 20 4c 50 20 69 6e 73 63 6f 6c 6c  USING LP inscoll
2695b 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 31 34 30  ist RP",. /* 140
2695c 20 2a 2f 20 22 75 73 69 6e 67 5f 6f 70 74 20 3a   */ "using_opt :
2695d 3a 3d 22 2c 0a 20 2f 2a 20 31 34 31 20 2a 2f 20  :=",. /* 141 */ 
2695e 22 6f 72 64 65 72 62 79 5f 6f 70 74 20 3a 3a 3d  "orderby_opt ::=
2695f 22 2c 0a 20 2f 2a 20 31 34 32 20 2a 2f 20 22 6f  ",. /* 142 */ "o
26960 72 64 65 72 62 79 5f 6f 70 74 20 3a 3a 3d 20 4f  rderby_opt ::= O
26961 52 44 45 52 20 42 59 20 73 6f 72 74 6c 69 73 74  RDER BY sortlist
26962 22 2c 0a 20 2f 2a 20 31 34 33 20 2a 2f 20 22 73  ",. /* 143 */ "s
26963 6f 72 74 6c 69 73 74 20 3a 3a 3d 20 73 6f 72 74  ortlist ::= sort
26964 6c 69 73 74 20 43 4f 4d 4d 41 20 73 6f 72 74 69  list COMMA sorti
26965 74 65 6d 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a  tem sortorder",.
26966 20 2f 2a 20 31 34 34 20 2a 2f 20 22 73 6f 72 74   /* 144 */ "sort
26967 6c 69 73 74 20 3a 3a 3d 20 73 6f 72 74 69 74 65  list ::= sortite
26968 6d 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f  m sortorder",. /
26969 2a 20 31 34 35 20 2a 2f 20 22 73 6f 72 74 69 74  * 145 */ "sortit
2696a 65 6d 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20 2f  em ::= expr",. /
2696b 2a 20 31 34 36 20 2a 2f 20 22 73 6f 72 74 6f 72  * 146 */ "sortor
2696c 64 65 72 20 3a 3a 3d 20 41 53 43 22 2c 0a 20 2f  der ::= ASC",. /
2696d 2a 20 31 34 37 20 2a 2f 20 22 73 6f 72 74 6f 72  * 147 */ "sortor
2696e 64 65 72 20 3a 3a 3d 20 44 45 53 43 22 2c 0a 20  der ::= DESC",. 
2696f 2f 2a 20 31 34 38 20 2a 2f 20 22 73 6f 72 74 6f  /* 148 */ "sorto
26970 72 64 65 72 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31  rder ::=",. /* 1
26971 34 39 20 2a 2f 20 22 67 72 6f 75 70 62 79 5f 6f  49 */ "groupby_o
26972 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35 30  pt ::=",. /* 150
26973 20 2a 2f 20 22 67 72 6f 75 70 62 79 5f 6f 70 74   */ "groupby_opt
26974 20 3a 3a 3d 20 47 52 4f 55 50 20 42 59 20 6e 65   ::= GROUP BY ne
26975 78 70 72 6c 69 73 74 22 2c 0a 20 2f 2a 20 31 35  xprlist",. /* 15
26976 31 20 2a 2f 20 22 68 61 76 69 6e 67 5f 6f 70 74  1 */ "having_opt
26977 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35 32 20 2a   ::=",. /* 152 *
26978 2f 20 22 68 61 76 69 6e 67 5f 6f 70 74 20 3a 3a  / "having_opt ::
26979 3d 20 48 41 56 49 4e 47 20 65 78 70 72 22 2c 0a  = HAVING expr",.
2697a 20 2f 2a 20 31 35 33 20 2a 2f 20 22 6c 69 6d 69   /* 153 */ "limi
2697b 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  t_opt ::=",. /* 
2697c 31 35 34 20 2a 2f 20 22 6c 69 6d 69 74 5f 6f 70  154 */ "limit_op
2697d 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72  t ::= LIMIT expr
2697e 22 2c 0a 20 2f 2a 20 31 35 35 20 2a 2f 20 22 6c  ",. /* 155 */ "l
2697f 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d  imit_opt ::= LIM
26980 49 54 20 65 78 70 72 20 4f 46 46 53 45 54 20 65  IT expr OFFSET e
26981 78 70 72 22 2c 0a 20 2f 2a 20 31 35 36 20 2a 2f  xpr",. /* 156 */
26982 20 22 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20   "limit_opt ::= 
26983 4c 49 4d 49 54 20 65 78 70 72 20 43 4f 4d 4d 41  LIMIT expr COMMA
26984 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 35 37 20   expr",. /* 157 
26985 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 45 4c 45  */ "cmd ::= DELE
26986 54 45 20 46 52 4f 4d 20 66 75 6c 6c 6e 61 6d 65  TE FROM fullname
26987 20 77 68 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a   where_opt",. /*
26988 20 31 35 38 20 2a 2f 20 22 77 68 65 72 65 5f 6f   158 */ "where_o
26989 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35 39  pt ::=",. /* 159
2698a 20 2a 2f 20 22 77 68 65 72 65 5f 6f 70 74 20 3a   */ "where_opt :
2698b 3a 3d 20 57 48 45 52 45 20 65 78 70 72 22 2c 0a  := WHERE expr",.
2698c 20 2f 2a 20 31 36 30 20 2a 2f 20 22 63 6d 64 20   /* 160 */ "cmd 
2698d 3a 3a 3d 20 55 50 44 41 54 45 20 6f 72 63 6f 6e  ::= UPDATE orcon
2698e 66 20 66 75 6c 6c 6e 61 6d 65 20 53 45 54 20 73  f fullname SET s
2698f 65 74 6c 69 73 74 20 77 68 65 72 65 5f 6f 70 74  etlist where_opt
26990 22 2c 0a 20 2f 2a 20 31 36 31 20 2a 2f 20 22 73  ",. /* 161 */ "s
26991 65 74 6c 69 73 74 20 3a 3a 3d 20 73 65 74 6c 69  etlist ::= setli
26992 73 74 20 43 4f 4d 4d 41 20 6e 6d 20 45 51 20 65  st COMMA nm EQ e
26993 78 70 72 22 2c 0a 20 2f 2a 20 31 36 32 20 2a 2f  xpr",. /* 162 */
26994 20 22 73 65 74 6c 69 73 74 20 3a 3a 3d 20 6e 6d   "setlist ::= nm
26995 20 45 51 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31   EQ expr",. /* 1
26996 36 33 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 69  63 */ "cmd ::= i
26997 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66  nsert_cmd INTO f
26998 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69  ullname inscolli
26999 73 74 5f 6f 70 74 20 56 41 4c 55 45 53 20 4c 50  st_opt VALUES LP
2699a 20 69 74 65 6d 6c 69 73 74 20 52 50 22 2c 0a 20   itemlist RP",. 
2699b 2f 2a 20 31 36 34 20 2a 2f 20 22 63 6d 64 20 3a  /* 164 */ "cmd :
2699c 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e  := insert_cmd IN
2699d 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63  TO fullname insc
2699e 6f 6c 6c 69 73 74 5f 6f 70 74 20 73 65 6c 65 63  ollist_opt selec
2699f 74 22 2c 0a 20 2f 2a 20 31 36 35 20 2a 2f 20 22  t",. /* 165 */ "
269a0 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63  cmd ::= insert_c
269a1 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65  md INTO fullname
269a2 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20   inscollist_opt 
269a3 44 45 46 41 55 4c 54 20 56 41 4c 55 45 53 22 2c  DEFAULT VALUES",
269a4 0a 20 2f 2a 20 31 36 36 20 2a 2f 20 22 69 6e 73  . /* 166 */ "ins
269a5 65 72 74 5f 63 6d 64 20 3a 3a 3d 20 49 4e 53 45  ert_cmd ::= INSE
269a6 52 54 20 6f 72 63 6f 6e 66 22 2c 0a 20 2f 2a 20  RT orconf",. /* 
269a7 31 36 37 20 2a 2f 20 22 69 6e 73 65 72 74 5f 63  167 */ "insert_c
269a8 6d 64 20 3a 3a 3d 20 52 45 50 4c 41 43 45 22 2c  md ::= REPLACE",
269a9 0a 20 2f 2a 20 31 36 38 20 2a 2f 20 22 69 74 65  . /* 168 */ "ite
269aa 6d 6c 69 73 74 20 3a 3a 3d 20 69 74 65 6d 6c 69  mlist ::= itemli
269ab 73 74 20 43 4f 4d 4d 41 20 65 78 70 72 22 2c 0a  st COMMA expr",.
269ac 20 2f 2a 20 31 36 39 20 2a 2f 20 22 69 74 65 6d   /* 169 */ "item
269ad 6c 69 73 74 20 3a 3a 3d 20 65 78 70 72 22 2c 0a  list ::= expr",.
269ae 20 2f 2a 20 31 37 30 20 2a 2f 20 22 69 6e 73 63   /* 170 */ "insc
269af 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 22 2c  ollist_opt ::=",
269b0 0a 20 2f 2a 20 31 37 31 20 2a 2f 20 22 69 6e 73  . /* 171 */ "ins
269b1 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20  collist_opt ::= 
269b2 4c 50 20 69 6e 73 63 6f 6c 6c 69 73 74 20 52 50  LP inscollist RP
269b3 22 2c 0a 20 2f 2a 20 31 37 32 20 2a 2f 20 22 69  ",. /* 172 */ "i
269b4 6e 73 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 69 6e  nscollist ::= in
269b5 73 63 6f 6c 6c 69 73 74 20 43 4f 4d 4d 41 20 6e  scollist COMMA n
269b6 6d 22 2c 0a 20 2f 2a 20 31 37 33 20 2a 2f 20 22  m",. /* 173 */ "
269b7 69 6e 73 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 6e  inscollist ::= n
269b8 6d 22 2c 0a 20 2f 2a 20 31 37 34 20 2a 2f 20 22  m",. /* 174 */ "
269b9 65 78 70 72 20 3a 3a 3d 20 74 65 72 6d 22 2c 0a  expr ::= term",.
269ba 20 2f 2a 20 31 37 35 20 2a 2f 20 22 65 78 70 72   /* 175 */ "expr
269bb 20 3a 3a 3d 20 4c 50 20 65 78 70 72 20 52 50 22   ::= LP expr RP"
269bc 2c 0a 20 2f 2a 20 31 37 36 20 2a 2f 20 22 74 65  ,. /* 176 */ "te
269bd 72 6d 20 3a 3a 3d 20 4e 55 4c 4c 22 2c 0a 20 2f  rm ::= NULL",. /
269be 2a 20 31 37 37 20 2a 2f 20 22 65 78 70 72 20 3a  * 177 */ "expr :
269bf 3a 3d 20 49 44 22 2c 0a 20 2f 2a 20 31 37 38 20  := ID",. /* 178 
269c0 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 4a 4f 49  */ "expr ::= JOI
269c1 4e 5f 4b 57 22 2c 0a 20 2f 2a 20 31 37 39 20 2a  N_KW",. /* 179 *
269c2 2f 20 22 65 78 70 72 20 3a 3a 3d 20 6e 6d 20 44  / "expr ::= nm D
269c3 4f 54 20 6e 6d 22 2c 0a 20 2f 2a 20 31 38 30 20  OT nm",. /* 180 
269c4 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 6e 6d 20  */ "expr ::= nm 
269c5 44 4f 54 20 6e 6d 20 44 4f 54 20 6e 6d 22 2c 0a  DOT nm DOT nm",.
269c6 20 2f 2a 20 31 38 31 20 2a 2f 20 22 74 65 72 6d   /* 181 */ "term
269c7 20 3a 3a 3d 20 49 4e 54 45 47 45 52 7c 46 4c 4f   ::= INTEGER|FLO
269c8 41 54 7c 42 4c 4f 42 22 2c 0a 20 2f 2a 20 31 38  AT|BLOB",. /* 18
269c9 32 20 2a 2f 20 22 74 65 72 6d 20 3a 3a 3d 20 53  2 */ "term ::= S
269ca 54 52 49 4e 47 22 2c 0a 20 2f 2a 20 31 38 33 20  TRING",. /* 183 
269cb 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 52 45 47  */ "expr ::= REG
269cc 49 53 54 45 52 22 2c 0a 20 2f 2a 20 31 38 34 20  ISTER",. /* 184 
269cd 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 56 41 52  */ "expr ::= VAR
269ce 49 41 42 4c 45 22 2c 0a 20 2f 2a 20 31 38 35 20  IABLE",. /* 185 
269cf 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70  */ "expr ::= exp
269d0 72 20 43 4f 4c 4c 41 54 45 20 69 64 73 22 2c 0a  r COLLATE ids",.
269d1 20 2f 2a 20 31 38 36 20 2a 2f 20 22 65 78 70 72   /* 186 */ "expr
269d2 20 3a 3a 3d 20 43 41 53 54 20 4c 50 20 65 78 70   ::= CAST LP exp
269d3 72 20 41 53 20 74 79 70 65 74 6f 6b 65 6e 20 52  r AS typetoken R
269d4 50 22 2c 0a 20 2f 2a 20 31 38 37 20 2a 2f 20 22  P",. /* 187 */ "
269d5 65 78 70 72 20 3a 3a 3d 20 49 44 20 4c 50 20 64  expr ::= ID LP d
269d6 69 73 74 69 6e 63 74 20 65 78 70 72 6c 69 73 74  istinct exprlist
269d7 20 52 50 22 2c 0a 20 2f 2a 20 31 38 38 20 2a 2f   RP",. /* 188 */
269d8 20 22 65 78 70 72 20 3a 3a 3d 20 49 44 20 4c 50   "expr ::= ID LP
269d9 20 53 54 41 52 20 52 50 22 2c 0a 20 2f 2a 20 31   STAR RP",. /* 1
269da 38 39 20 2a 2f 20 22 74 65 72 6d 20 3a 3a 3d 20  89 */ "term ::= 
269db 43 54 49 4d 45 5f 4b 57 22 2c 0a 20 2f 2a 20 31  CTIME_KW",. /* 1
269dc 39 30 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  90 */ "expr ::= 
269dd 65 78 70 72 20 41 4e 44 20 65 78 70 72 22 2c 0a  expr AND expr",.
269de 20 2f 2a 20 31 39 31 20 2a 2f 20 22 65 78 70 72   /* 191 */ "expr
269df 20 3a 3a 3d 20 65 78 70 72 20 4f 52 20 65 78 70   ::= expr OR exp
269e0 72 22 2c 0a 20 2f 2a 20 31 39 32 20 2a 2f 20 22  r",. /* 192 */ "
269e1 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 4c 54  expr ::= expr LT
269e2 7c 47 54 7c 47 45 7c 4c 45 20 65 78 70 72 22 2c  |GT|GE|LE expr",
269e3 0a 20 2f 2a 20 31 39 33 20 2a 2f 20 22 65 78 70  . /* 193 */ "exp
269e4 72 20 3a 3a 3d 20 65 78 70 72 20 45 51 7c 4e 45  r ::= expr EQ|NE
269e5 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 39 34 20   expr",. /* 194 
269e6 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70  */ "expr ::= exp
269e7 72 20 42 49 54 41 4e 44 7c 42 49 54 4f 52 7c 4c  r BITAND|BITOR|L
269e8 53 48 49 46 54 7c 52 53 48 49 46 54 20 65 78 70  SHIFT|RSHIFT exp
269e9 72 22 2c 0a 20 2f 2a 20 31 39 35 20 2a 2f 20 22  r",. /* 195 */ "
269ea 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 50 4c  expr ::= expr PL
269eb 55 53 7c 4d 49 4e 55 53 20 65 78 70 72 22 2c 0a  US|MINUS expr",.
269ec 20 2f 2a 20 31 39 36 20 2a 2f 20 22 65 78 70 72   /* 196 */ "expr
269ed 20 3a 3a 3d 20 65 78 70 72 20 53 54 41 52 7c 53   ::= expr STAR|S
269ee 4c 41 53 48 7c 52 45 4d 20 65 78 70 72 22 2c 0a  LASH|REM expr",.
269ef 20 2f 2a 20 31 39 37 20 2a 2f 20 22 65 78 70 72   /* 197 */ "expr
269f0 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4e 43 41 54   ::= expr CONCAT
269f1 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 39 38 20   expr",. /* 198 
269f2 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4c  */ "likeop ::= L
269f3 49 4b 45 5f 4b 57 22 2c 0a 20 2f 2a 20 31 39 39  IKE_KW",. /* 199
269f4 20 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20   */ "likeop ::= 
269f5 4e 4f 54 20 4c 49 4b 45 5f 4b 57 22 2c 0a 20 2f  NOT LIKE_KW",. /
269f6 2a 20 32 30 30 20 2a 2f 20 22 6c 69 6b 65 6f 70  * 200 */ "likeop
269f7 20 3a 3a 3d 20 4d 41 54 43 48 22 2c 0a 20 2f 2a   ::= MATCH",. /*
269f8 20 32 30 31 20 2a 2f 20 22 6c 69 6b 65 6f 70 20   201 */ "likeop 
269f9 3a 3a 3d 20 4e 4f 54 20 4d 41 54 43 48 22 2c 0a  ::= NOT MATCH",.
269fa 20 2f 2a 20 32 30 32 20 2a 2f 20 22 65 73 63 61   /* 202 */ "esca
269fb 70 65 20 3a 3a 3d 20 45 53 43 41 50 45 20 65 78  pe ::= ESCAPE ex
269fc 70 72 22 2c 0a 20 2f 2a 20 32 30 33 20 2a 2f 20  pr",. /* 203 */ 
269fd 22 65 73 63 61 70 65 20 3a 3a 3d 22 2c 0a 20 2f  "escape ::=",. /
269fe 2a 20 32 30 34 20 2a 2f 20 22 65 78 70 72 20 3a  * 204 */ "expr :
269ff 3a 3d 20 65 78 70 72 20 6c 69 6b 65 6f 70 20 65  := expr likeop e
26a00 78 70 72 20 65 73 63 61 70 65 22 2c 0a 20 2f 2a  xpr escape",. /*
26a01 20 32 30 35 20 2a 2f 20 22 65 78 70 72 20 3a 3a   205 */ "expr ::
26a02 3d 20 65 78 70 72 20 49 53 4e 55 4c 4c 7c 4e 4f  = expr ISNULL|NO
26a03 54 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 32 30 36 20  TNULL",. /* 206 
26a04 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70  */ "expr ::= exp
26a05 72 20 49 53 20 4e 55 4c 4c 22 2c 0a 20 2f 2a 20  r IS NULL",. /* 
26a06 32 30 37 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  207 */ "expr ::=
26a07 20 65 78 70 72 20 4e 4f 54 20 4e 55 4c 4c 22 2c   expr NOT NULL",
26a08 0a 20 2f 2a 20 32 30 38 20 2a 2f 20 22 65 78 70  . /* 208 */ "exp
26a09 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 20 4e 4f  r ::= expr IS NO
26a0a 54 20 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 32 30 39  T NULL",. /* 209
26a0b 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 4e 4f   */ "expr ::= NO
26a0c 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 31 30  T expr",. /* 210
26a0d 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 42 49   */ "expr ::= BI
26a0e 54 4e 4f 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20  TNOT expr",. /* 
26a0f 32 31 31 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  211 */ "expr ::=
26a10 20 4d 49 4e 55 53 20 65 78 70 72 22 2c 0a 20 2f   MINUS expr",. /
26a11 2a 20 32 31 32 20 2a 2f 20 22 65 78 70 72 20 3a  * 212 */ "expr :
26a12 3a 3d 20 50 4c 55 53 20 65 78 70 72 22 2c 0a 20  := PLUS expr",. 
26a13 2f 2a 20 32 31 33 20 2a 2f 20 22 62 65 74 77 65  /* 213 */ "betwe
26a14 65 6e 5f 6f 70 20 3a 3a 3d 20 42 45 54 57 45 45  en_op ::= BETWEE
26a15 4e 22 2c 0a 20 2f 2a 20 32 31 34 20 2a 2f 20 22  N",. /* 214 */ "
26a16 62 65 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20 4e  between_op ::= N
26a17 4f 54 20 42 45 54 57 45 45 4e 22 2c 0a 20 2f 2a  OT BETWEEN",. /*
26a18 20 32 31 35 20 2a 2f 20 22 65 78 70 72 20 3a 3a   215 */ "expr ::
26a19 3d 20 65 78 70 72 20 62 65 74 77 65 65 6e 5f 6f  = expr between_o
26a1a 70 20 65 78 70 72 20 41 4e 44 20 65 78 70 72 22  p expr AND expr"
26a1b 2c 0a 20 2f 2a 20 32 31 36 20 2a 2f 20 22 69 6e  ,. /* 216 */ "in
26a1c 5f 6f 70 20 3a 3a 3d 20 49 4e 22 2c 0a 20 2f 2a  _op ::= IN",. /*
26a1d 20 32 31 37 20 2a 2f 20 22 69 6e 5f 6f 70 20 3a   217 */ "in_op :
26a1e 3a 3d 20 4e 4f 54 20 49 4e 22 2c 0a 20 2f 2a 20  := NOT IN",. /* 
26a1f 32 31 38 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  218 */ "expr ::=
26a20 20 65 78 70 72 20 69 6e 5f 6f 70 20 4c 50 20 65   expr in_op LP e
26a21 78 70 72 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a  xprlist RP",. /*
26a22 20 32 31 39 20 2a 2f 20 22 65 78 70 72 20 3a 3a   219 */ "expr ::
26a23 3d 20 4c 50 20 73 65 6c 65 63 74 20 52 50 22 2c  = LP select RP",
26a24 0a 20 2f 2a 20 32 32 30 20 2a 2f 20 22 65 78 70  . /* 220 */ "exp
26a25 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70  r ::= expr in_op
26a26 20 4c 50 20 73 65 6c 65 63 74 20 52 50 22 2c 0a   LP select RP",.
26a27 20 2f 2a 20 32 32 31 20 2a 2f 20 22 65 78 70 72   /* 221 */ "expr
26a28 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20   ::= expr in_op 
26a29 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20 32 32  nm dbnm",. /* 22
26a2a 32 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 45  2 */ "expr ::= E
26a2b 58 49 53 54 53 20 4c 50 20 73 65 6c 65 63 74 20  XISTS LP select 
26a2c 52 50 22 2c 0a 20 2f 2a 20 32 32 33 20 2a 2f 20  RP",. /* 223 */ 
26a2d 22 65 78 70 72 20 3a 3a 3d 20 43 41 53 45 20 63  "expr ::= CASE c
26a2e 61 73 65 5f 6f 70 65 72 61 6e 64 20 63 61 73 65  ase_operand case
26a2f 5f 65 78 70 72 6c 69 73 74 20 63 61 73 65 5f 65  _exprlist case_e
26a30 6c 73 65 20 45 4e 44 22 2c 0a 20 2f 2a 20 32 32  lse END",. /* 22
26a31 34 20 2a 2f 20 22 63 61 73 65 5f 65 78 70 72 6c  4 */ "case_exprl
26a32 69 73 74 20 3a 3a 3d 20 63 61 73 65 5f 65 78 70  ist ::= case_exp
26a33 72 6c 69 73 74 20 57 48 45 4e 20 65 78 70 72 20  rlist WHEN expr 
26a34 54 48 45 4e 20 65 78 70 72 22 2c 0a 20 2f 2a 20  THEN expr",. /* 
26a35 32 32 35 20 2a 2f 20 22 63 61 73 65 5f 65 78 70  225 */ "case_exp
26a36 72 6c 69 73 74 20 3a 3a 3d 20 57 48 45 4e 20 65  rlist ::= WHEN e
26a37 78 70 72 20 54 48 45 4e 20 65 78 70 72 22 2c 0a  xpr THEN expr",.
26a38 20 2f 2a 20 32 32 36 20 2a 2f 20 22 63 61 73 65   /* 226 */ "case
26a39 5f 65 6c 73 65 20 3a 3a 3d 20 45 4c 53 45 20 65  _else ::= ELSE e
26a3a 78 70 72 22 2c 0a 20 2f 2a 20 32 32 37 20 2a 2f  xpr",. /* 227 */
26a3b 20 22 63 61 73 65 5f 65 6c 73 65 20 3a 3a 3d 22   "case_else ::="
26a3c 2c 0a 20 2f 2a 20 32 32 38 20 2a 2f 20 22 63 61  ,. /* 228 */ "ca
26a3d 73 65 5f 6f 70 65 72 61 6e 64 20 3a 3a 3d 20 65  se_operand ::= e
26a3e 78 70 72 22 2c 0a 20 2f 2a 20 32 32 39 20 2a 2f  xpr",. /* 229 */
26a3f 20 22 63 61 73 65 5f 6f 70 65 72 61 6e 64 20 3a   "case_operand :
26a40 3a 3d 22 2c 0a 20 2f 2a 20 32 33 30 20 2a 2f 20  :=",. /* 230 */ 
26a41 22 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 6e 65  "exprlist ::= ne
26a42 78 70 72 6c 69 73 74 22 2c 0a 20 2f 2a 20 32 33  xprlist",. /* 23
26a43 31 20 2a 2f 20 22 65 78 70 72 6c 69 73 74 20 3a  1 */ "exprlist :
26a44 3a 3d 22 2c 0a 20 2f 2a 20 32 33 32 20 2a 2f 20  :=",. /* 232 */ 
26a45 22 6e 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 6e  "nexprlist ::= n
26a46 65 78 70 72 6c 69 73 74 20 43 4f 4d 4d 41 20 65  exprlist COMMA e
26a47 78 70 72 22 2c 0a 20 2f 2a 20 32 33 33 20 2a 2f  xpr",. /* 233 */
26a48 20 22 6e 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20   "nexprlist ::= 
26a49 65 78 70 72 22 2c 0a 20 2f 2a 20 32 33 34 20 2a  expr",. /* 234 *
26a4a 2f 20 22 63 6d 64 20 3a 3a 3d 20 43 52 45 41 54  / "cmd ::= CREAT
26a4b 45 20 75 6e 69 71 75 65 66 6c 61 67 20 49 4e 44  E uniqueflag IND
26a4c 45 58 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e  EX ifnotexists n
26a4d 6d 20 64 62 6e 6d 20 4f 4e 20 6e 6d 20 4c 50 20  m dbnm ON nm LP 
26a4e 69 64 78 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a  idxlist RP",. /*
26a4f 20 32 33 35 20 2a 2f 20 22 75 6e 69 71 75 65 66   235 */ "uniquef
26a50 6c 61 67 20 3a 3a 3d 20 55 4e 49 51 55 45 22 2c  lag ::= UNIQUE",
26a51 0a 20 2f 2a 20 32 33 36 20 2a 2f 20 22 75 6e 69  . /* 236 */ "uni
26a52 71 75 65 66 6c 61 67 20 3a 3a 3d 22 2c 0a 20 2f  queflag ::=",. /
26a53 2a 20 32 33 37 20 2a 2f 20 22 69 64 78 6c 69 73  * 237 */ "idxlis
26a54 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  t_opt ::=",. /* 
26a55 32 33 38 20 2a 2f 20 22 69 64 78 6c 69 73 74 5f  238 */ "idxlist_
26a56 6f 70 74 20 3a 3a 3d 20 4c 50 20 69 64 78 6c 69  opt ::= LP idxli
26a57 73 74 20 52 50 22 2c 0a 20 2f 2a 20 32 33 39 20  st RP",. /* 239 
26a58 2a 2f 20 22 69 64 78 6c 69 73 74 20 3a 3a 3d 20  */ "idxlist ::= 
26a59 69 64 78 6c 69 73 74 20 43 4f 4d 4d 41 20 69 64  idxlist COMMA id
26a5a 78 69 74 65 6d 20 63 6f 6c 6c 61 74 65 20 73 6f  xitem collate so
26a5b 72 74 6f 72 64 65 72 22 2c 0a 20 2f 2a 20 32 34  rtorder",. /* 24
26a5c 30 20 2a 2f 20 22 69 64 78 6c 69 73 74 20 3a 3a  0 */ "idxlist ::
26a5d 3d 20 69 64 78 69 74 65 6d 20 63 6f 6c 6c 61 74  = idxitem collat
26a5e 65 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f  e sortorder",. /
26a5f 2a 20 32 34 31 20 2a 2f 20 22 69 64 78 69 74 65  * 241 */ "idxite
26a60 6d 20 3a 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a 20 32  m ::= nm",. /* 2
26a61 34 32 20 2a 2f 20 22 63 6f 6c 6c 61 74 65 20 3a  42 */ "collate :
26a62 3a 3d 22 2c 0a 20 2f 2a 20 32 34 33 20 2a 2f 20  :=",. /* 243 */ 
26a63 22 63 6f 6c 6c 61 74 65 20 3a 3a 3d 20 43 4f 4c  "collate ::= COL
26a64 4c 41 54 45 20 69 64 73 22 2c 0a 20 2f 2a 20 32  LATE ids",. /* 2
26a65 34 34 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44  44 */ "cmd ::= D
26a66 52 4f 50 20 49 4e 44 45 58 20 69 66 65 78 69 73  ROP INDEX ifexis
26a67 74 73 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f  ts fullname",. /
26a68 2a 20 32 34 35 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 245 */ "cmd ::
26a69 3d 20 56 41 43 55 55 4d 22 2c 0a 20 2f 2a 20 32  = VACUUM",. /* 2
26a6a 34 36 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 56  46 */ "cmd ::= V
26a6b 41 43 55 55 4d 20 6e 6d 22 2c 0a 20 2f 2a 20 32  ACUUM nm",. /* 2
26a6c 34 37 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 50  47 */ "cmd ::= P
26a6d 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51  RAGMA nm dbnm EQ
26a6e 20 6e 6d 6e 75 6d 22 2c 0a 20 2f 2a 20 32 34 38   nmnum",. /* 248
26a6f 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 50 52 41   */ "cmd ::= PRA
26a70 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51 20 4f  GMA nm dbnm EQ O
26a71 4e 22 2c 0a 20 2f 2a 20 32 34 39 20 2a 2f 20 22  N",. /* 249 */ "
26a72 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e  cmd ::= PRAGMA n
26a73 6d 20 64 62 6e 6d 20 45 51 20 44 45 4c 45 54 45  m dbnm EQ DELETE
26a74 22 2c 0a 20 2f 2a 20 32 35 30 20 2a 2f 20 22 63  ",. /* 250 */ "c
26a75 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d  md ::= PRAGMA nm
26a76 20 64 62 6e 6d 20 45 51 20 6d 69 6e 75 73 5f 6e   dbnm EQ minus_n
26a77 75 6d 22 2c 0a 20 2f 2a 20 32 35 31 20 2a 2f 20  um",. /* 251 */ 
26a78 22 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20  "cmd ::= PRAGMA 
26a79 6e 6d 20 64 62 6e 6d 20 4c 50 20 6e 6d 6e 75 6d  nm dbnm LP nmnum
26a7a 20 52 50 22 2c 0a 20 2f 2a 20 32 35 32 20 2a 2f   RP",. /* 252 */
26a7b 20 22 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41   "cmd ::= PRAGMA
26a7c 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20 32   nm dbnm",. /* 2
26a7d 35 33 20 2a 2f 20 22 6e 6d 6e 75 6d 20 3a 3a 3d  53 */ "nmnum ::=
26a7e 20 70 6c 75 73 5f 6e 75 6d 22 2c 0a 20 2f 2a 20   plus_num",. /* 
26a7f 32 35 34 20 2a 2f 20 22 6e 6d 6e 75 6d 20 3a 3a  254 */ "nmnum ::
26a80 3d 20 6e 6d 22 2c 0a 20 2f 2a 20 32 35 35 20 2a  = nm",. /* 255 *
26a81 2f 20 22 70 6c 75 73 5f 6e 75 6d 20 3a 3a 3d 20  / "plus_num ::= 
26a82 70 6c 75 73 5f 6f 70 74 20 6e 75 6d 62 65 72 22  plus_opt number"
26a83 2c 0a 20 2f 2a 20 32 35 36 20 2a 2f 20 22 6d 69  ,. /* 256 */ "mi
26a84 6e 75 73 5f 6e 75 6d 20 3a 3a 3d 20 4d 49 4e 55  nus_num ::= MINU
26a85 53 20 6e 75 6d 62 65 72 22 2c 0a 20 2f 2a 20 32  S number",. /* 2
26a86 35 37 20 2a 2f 20 22 6e 75 6d 62 65 72 20 3a 3a  57 */ "number ::
26a87 3d 20 49 4e 54 45 47 45 52 7c 46 4c 4f 41 54 22  = INTEGER|FLOAT"
26a88 2c 0a 20 2f 2a 20 32 35 38 20 2a 2f 20 22 70 6c  ,. /* 258 */ "pl
26a89 75 73 5f 6f 70 74 20 3a 3a 3d 20 50 4c 55 53 22  us_opt ::= PLUS"
26a8a 2c 0a 20 2f 2a 20 32 35 39 20 2a 2f 20 22 70 6c  ,. /* 259 */ "pl
26a8b 75 73 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  us_opt ::=",. /*
26a8c 20 32 36 30 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   260 */ "cmd ::=
26a8d 20 43 52 45 41 54 45 20 74 72 69 67 67 65 72 5f   CREATE trigger_
26a8e 64 65 63 6c 20 42 45 47 49 4e 20 74 72 69 67 67  decl BEGIN trigg
26a8f 65 72 5f 63 6d 64 5f 6c 69 73 74 20 45 4e 44 22  er_cmd_list END"
26a90 2c 0a 20 2f 2a 20 32 36 31 20 2a 2f 20 22 74 72  ,. /* 261 */ "tr
26a91 69 67 67 65 72 5f 64 65 63 6c 20 3a 3a 3d 20 74  igger_decl ::= t
26a92 65 6d 70 20 54 52 49 47 47 45 52 20 69 66 6e 6f  emp TRIGGER ifno
26a93 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20  texists nm dbnm 
26a94 74 72 69 67 67 65 72 5f 74 69 6d 65 20 74 72 69  trigger_time tri
26a95 67 67 65 72 5f 65 76 65 6e 74 20 4f 4e 20 66 75  gger_event ON fu
26a96 6c 6c 6e 61 6d 65 20 66 6f 72 65 61 63 68 5f 63  llname foreach_c
26a97 6c 61 75 73 65 20 77 68 65 6e 5f 63 6c 61 75 73  lause when_claus
26a98 65 22 2c 0a 20 2f 2a 20 32 36 32 20 2a 2f 20 22  e",. /* 262 */ "
26a99 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d  trigger_time ::=
26a9a 20 42 45 46 4f 52 45 22 2c 0a 20 2f 2a 20 32 36   BEFORE",. /* 26
26a9b 33 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 74 69  3 */ "trigger_ti
26a9c 6d 65 20 3a 3a 3d 20 41 46 54 45 52 22 2c 0a 20  me ::= AFTER",. 
26a9d 2f 2a 20 32 36 34 20 2a 2f 20 22 74 72 69 67 67  /* 264 */ "trigg
26a9e 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 49 4e 53 54  er_time ::= INST
26a9f 45 41 44 20 4f 46 22 2c 0a 20 2f 2a 20 32 36 35  EAD OF",. /* 265
26aa0 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 74 69 6d   */ "trigger_tim
26aa1 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 36 36 20  e ::=",. /* 266 
26aa2 2a 2f 20 22 74 72 69 67 67 65 72 5f 65 76 65 6e  */ "trigger_even
26aa3 74 20 3a 3a 3d 20 44 45 4c 45 54 45 7c 49 4e 53  t ::= DELETE|INS
26aa4 45 52 54 22 2c 0a 20 2f 2a 20 32 36 37 20 2a 2f  ERT",. /* 267 */
26aa5 20 22 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20   "trigger_event 
26aa6 3a 3a 3d 20 55 50 44 41 54 45 22 2c 0a 20 2f 2a  ::= UPDATE",. /*
26aa7 20 32 36 38 20 2a 2f 20 22 74 72 69 67 67 65 72   268 */ "trigger
26aa8 5f 65 76 65 6e 74 20 3a 3a 3d 20 55 50 44 41 54  _event ::= UPDAT
26aa9 45 20 4f 46 20 69 6e 73 63 6f 6c 6c 69 73 74 22  E OF inscollist"
26aaa 2c 0a 20 2f 2a 20 32 36 39 20 2a 2f 20 22 66 6f  ,. /* 269 */ "fo
26aab 72 65 61 63 68 5f 63 6c 61 75 73 65 20 3a 3a 3d  reach_clause ::=
26aac 22 2c 0a 20 2f 2a 20 32 37 30 20 2a 2f 20 22 66  ",. /* 270 */ "f
26aad 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 20 3a 3a  oreach_clause ::
26aae 3d 20 46 4f 52 20 45 41 43 48 20 52 4f 57 22 2c  = FOR EACH ROW",
26aaf 0a 20 2f 2a 20 32 37 31 20 2a 2f 20 22 77 68 65  . /* 271 */ "whe
26ab0 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d 22 2c 0a 20  n_clause ::=",. 
26ab1 2f 2a 20 32 37 32 20 2a 2f 20 22 77 68 65 6e 5f  /* 272 */ "when_
26ab2 63 6c 61 75 73 65 20 3a 3a 3d 20 57 48 45 4e 20  clause ::= WHEN 
26ab3 65 78 70 72 22 2c 0a 20 2f 2a 20 32 37 33 20 2a  expr",. /* 273 *
26ab4 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c  / "trigger_cmd_l
26ab5 69 73 74 20 3a 3a 3d 20 74 72 69 67 67 65 72 5f  ist ::= trigger_
26ab6 63 6d 64 5f 6c 69 73 74 20 74 72 69 67 67 65 72  cmd_list trigger
26ab7 5f 63 6d 64 20 53 45 4d 49 22 2c 0a 20 2f 2a 20  _cmd SEMI",. /* 
26ab8 32 37 34 20 2a 2f 20 22 74 72 69 67 67 65 72 5f  274 */ "trigger_
26ab9 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d 22 2c 0a 20  cmd_list ::=",. 
26aba 2f 2a 20 32 37 35 20 2a 2f 20 22 74 72 69 67 67  /* 275 */ "trigg
26abb 65 72 5f 63 6d 64 20 3a 3a 3d 20 55 50 44 41 54  er_cmd ::= UPDAT
26abc 45 20 6f 72 63 6f 6e 66 20 6e 6d 20 53 45 54 20  E orconf nm SET 
26abd 73 65 74 6c 69 73 74 20 77 68 65 72 65 5f 6f 70  setlist where_op
26abe 74 22 2c 0a 20 2f 2a 20 32 37 36 20 2a 2f 20 22  t",. /* 276 */ "
26abf 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20  trigger_cmd ::= 
26ac0 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20  insert_cmd INTO 
26ac1 6e 6d 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70  nm inscollist_op
26ac2 74 20 56 41 4c 55 45 53 20 4c 50 20 69 74 65 6d  t VALUES LP item
26ac3 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 32 37  list RP",. /* 27
26ac4 37 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63 6d  7 */ "trigger_cm
26ac5 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64  d ::= insert_cmd
26ac6 20 49 4e 54 4f 20 6e 6d 20 69 6e 73 63 6f 6c 6c   INTO nm inscoll
26ac7 69 73 74 5f 6f 70 74 20 73 65 6c 65 63 74 22 2c  ist_opt select",
26ac8 0a 20 2f 2a 20 32 37 38 20 2a 2f 20 22 74 72 69  . /* 278 */ "tri
26ac9 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 44 45 4c  gger_cmd ::= DEL
26aca 45 54 45 20 46 52 4f 4d 20 6e 6d 20 77 68 65 72  ETE FROM nm wher
26acb 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 32 37 39 20  e_opt",. /* 279 
26acc 2a 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20  */ "trigger_cmd 
26acd 3a 3a 3d 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a  ::= select",. /*
26ace 20 32 38 30 20 2a 2f 20 22 65 78 70 72 20 3a 3a   280 */ "expr ::
26acf 3d 20 52 41 49 53 45 20 4c 50 20 49 47 4e 4f 52  = RAISE LP IGNOR
26ad0 45 20 52 50 22 2c 0a 20 2f 2a 20 32 38 31 20 2a  E RP",. /* 281 *
26ad1 2f 20 22 65 78 70 72 20 3a 3a 3d 20 52 41 49 53  / "expr ::= RAIS
26ad2 45 20 4c 50 20 72 61 69 73 65 74 79 70 65 20 43  E LP raisetype C
26ad3 4f 4d 4d 41 20 6e 6d 20 52 50 22 2c 0a 20 2f 2a  OMMA nm RP",. /*
26ad4 20 32 38 32 20 2a 2f 20 22 72 61 69 73 65 74 79   282 */ "raisety
26ad5 70 65 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 22  pe ::= ROLLBACK"
26ad6 2c 0a 20 2f 2a 20 32 38 33 20 2a 2f 20 22 72 61  ,. /* 283 */ "ra
26ad7 69 73 65 74 79 70 65 20 3a 3a 3d 20 41 42 4f 52  isetype ::= ABOR
26ad8 54 22 2c 0a 20 2f 2a 20 32 38 34 20 2a 2f 20 22  T",. /* 284 */ "
26ad9 72 61 69 73 65 74 79 70 65 20 3a 3a 3d 20 46 41  raisetype ::= FA
26ada 49 4c 22 2c 0a 20 2f 2a 20 32 38 35 20 2a 2f 20  IL",. /* 285 */ 
26adb 22 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54 52  "cmd ::= DROP TR
26adc 49 47 47 45 52 20 69 66 65 78 69 73 74 73 20 66  IGGER ifexists f
26add 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 32 38  ullname",. /* 28
26ade 36 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 41 54  6 */ "cmd ::= AT
26adf 54 41 43 48 20 64 61 74 61 62 61 73 65 5f 6b 77  TACH database_kw
26ae0 5f 6f 70 74 20 65 78 70 72 20 41 53 20 65 78 70  _opt expr AS exp
26ae1 72 20 6b 65 79 5f 6f 70 74 22 2c 0a 20 2f 2a 20  r key_opt",. /* 
26ae2 32 38 37 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  287 */ "cmd ::= 
26ae3 44 45 54 41 43 48 20 64 61 74 61 62 61 73 65 5f  DETACH database_
26ae4 6b 77 5f 6f 70 74 20 65 78 70 72 22 2c 0a 20 2f  kw_opt expr",. /
26ae5 2a 20 32 38 38 20 2a 2f 20 22 6b 65 79 5f 6f 70  * 288 */ "key_op
26ae6 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 38 39 20  t ::=",. /* 289 
26ae7 2a 2f 20 22 6b 65 79 5f 6f 70 74 20 3a 3a 3d 20  */ "key_opt ::= 
26ae8 4b 45 59 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32  KEY expr",. /* 2
26ae9 39 30 20 2a 2f 20 22 64 61 74 61 62 61 73 65 5f  90 */ "database_
26aea 6b 77 5f 6f 70 74 20 3a 3a 3d 20 44 41 54 41 42  kw_opt ::= DATAB
26aeb 41 53 45 22 2c 0a 20 2f 2a 20 32 39 31 20 2a 2f  ASE",. /* 291 */
26aec 20 22 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70   "database_kw_op
26aed 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 39 32 20  t ::=",. /* 292 
26aee 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 45 49 4e  */ "cmd ::= REIN
26aef 44 45 58 22 2c 0a 20 2f 2a 20 32 39 33 20 2a 2f  DEX",. /* 293 */
26af0 20 22 63 6d 64 20 3a 3a 3d 20 52 45 49 4e 44 45   "cmd ::= REINDE
26af1 58 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20  X nm dbnm",. /* 
26af2 32 39 34 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  294 */ "cmd ::= 
26af3 41 4e 41 4c 59 5a 45 22 2c 0a 20 2f 2a 20 32 39  ANALYZE",. /* 29
26af4 35 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 41 4e  5 */ "cmd ::= AN
26af5 41 4c 59 5a 45 20 6e 6d 20 64 62 6e 6d 22 2c 0a  ALYZE nm dbnm",.
26af6 20 2f 2a 20 32 39 36 20 2a 2f 20 22 63 6d 64 20   /* 296 */ "cmd 
26af7 3a 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c 45 20  ::= ALTER TABLE 
26af8 66 75 6c 6c 6e 61 6d 65 20 52 45 4e 41 4d 45 20  fullname RENAME 
26af9 54 4f 20 6e 6d 22 2c 0a 20 2f 2a 20 32 39 37 20  TO nm",. /* 297 
26afa 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 41 4c 54 45  */ "cmd ::= ALTE
26afb 52 20 54 41 42 4c 45 20 61 64 64 5f 63 6f 6c 75  R TABLE add_colu
26afc 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 20 41 44 44 20  mn_fullname ADD 
26afd 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 63 6f 6c  kwcolumn_opt col
26afe 75 6d 6e 22 2c 0a 20 2f 2a 20 32 39 38 20 2a 2f  umn",. /* 298 */
26aff 20 22 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c   "add_column_ful
26b00 6c 6e 61 6d 65 20 3a 3a 3d 20 66 75 6c 6c 6e 61  lname ::= fullna
26b01 6d 65 22 2c 0a 20 2f 2a 20 32 39 39 20 2a 2f 20  me",. /* 299 */ 
26b02 22 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 3a 3a  "kwcolumn_opt ::
26b03 3d 22 2c 0a 20 2f 2a 20 33 30 30 20 2a 2f 20 22  =",. /* 300 */ "
26b04 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 3a 3a 3d  kwcolumn_opt ::=
26b05 20 43 4f 4c 55 4d 4e 4b 57 22 2c 0a 20 2f 2a 20   COLUMNKW",. /* 
26b06 33 30 31 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  301 */ "cmd ::= 
26b07 63 72 65 61 74 65 5f 76 74 61 62 22 2c 0a 20 2f  create_vtab",. /
26b08 2a 20 33 30 32 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 302 */ "cmd ::
26b09 3d 20 63 72 65 61 74 65 5f 76 74 61 62 20 4c 50  = create_vtab LP
26b0a 20 76 74 61 62 61 72 67 6c 69 73 74 20 52 50 22   vtabarglist RP"
26b0b 2c 0a 20 2f 2a 20 33 30 33 20 2a 2f 20 22 63 72  ,. /* 303 */ "cr
26b0c 65 61 74 65 5f 76 74 61 62 20 3a 3a 3d 20 43 52  eate_vtab ::= CR
26b0d 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42  EATE VIRTUAL TAB
26b0e 4c 45 20 6e 6d 20 64 62 6e 6d 20 55 53 49 4e 47  LE nm dbnm USING
26b0f 20 6e 6d 22 2c 0a 20 2f 2a 20 33 30 34 20 2a 2f   nm",. /* 304 */
26b10 20 22 76 74 61 62 61 72 67 6c 69 73 74 20 3a 3a   "vtabarglist ::
26b11 3d 20 76 74 61 62 61 72 67 22 2c 0a 20 2f 2a 20  = vtabarg",. /* 
26b12 33 30 35 20 2a 2f 20 22 76 74 61 62 61 72 67 6c  305 */ "vtabargl
26b13 69 73 74 20 3a 3a 3d 20 76 74 61 62 61 72 67 6c  ist ::= vtabargl
26b14 69 73 74 20 43 4f 4d 4d 41 20 76 74 61 62 61 72  ist COMMA vtabar
26b15 67 22 2c 0a 20 2f 2a 20 33 30 36 20 2a 2f 20 22  g",. /* 306 */ "
26b16 76 74 61 62 61 72 67 20 3a 3a 3d 22 2c 0a 20 2f  vtabarg ::=",. /
26b17 2a 20 33 30 37 20 2a 2f 20 22 76 74 61 62 61 72  * 307 */ "vtabar
26b18 67 20 3a 3a 3d 20 76 74 61 62 61 72 67 20 76 74  g ::= vtabarg vt
26b19 61 62 61 72 67 74 6f 6b 65 6e 22 2c 0a 20 2f 2a  abargtoken",. /*
26b1a 20 33 30 38 20 2a 2f 20 22 76 74 61 62 61 72 67   308 */ "vtabarg
26b1b 74 6f 6b 65 6e 20 3a 3a 3d 20 41 4e 59 22 2c 0a  token ::= ANY",.
26b1c 20 2f 2a 20 33 30 39 20 2a 2f 20 22 76 74 61 62   /* 309 */ "vtab
26b1d 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d 20 6c 70 20  argtoken ::= lp 
26b1e 61 6e 79 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a  anylist RP",. /*
26b1f 20 33 31 30 20 2a 2f 20 22 6c 70 20 3a 3a 3d 20   310 */ "lp ::= 
26b20 4c 50 22 2c 0a 20 2f 2a 20 33 31 31 20 2a 2f 20  LP",. /* 311 */ 
26b21 22 61 6e 79 6c 69 73 74 20 3a 3a 3d 22 2c 0a 20  "anylist ::=",. 
26b22 2f 2a 20 33 31 32 20 2a 2f 20 22 61 6e 79 6c 69  /* 312 */ "anyli
26b23 73 74 20 3a 3a 3d 20 61 6e 79 6c 69 73 74 20 41  st ::= anylist A
26b24 4e 59 22 2c 0a 7d 3b 0a 23 65 6e 64 69 66 20 2f  NY",.};.#endif /
26b25 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 0a 23 69  * NDEBUG */...#i
26b26 66 20 59 59 53 54 41 43 4b 44 45 50 54 48 3c 3d  f YYSTACKDEPTH<=
26b27 30 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 69  0./*.** Try to i
26b28 6e 63 72 65 61 73 65 20 74 68 65 20 73 69 7a 65  ncrease the size
26b29 20 6f 66 20 74 68 65 20 70 61 72 73 65 72 20 73   of the parser s
26b2a 74 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tack..*/.static 
26b2b 76 6f 69 64 20 79 79 47 72 6f 77 53 74 61 63 6b  void yyGrowStack
26b2c 28 79 79 50 61 72 73 65 72 20 2a 70 29 7b 0a 20  (yyParser *p){. 
26b2d 20 69 6e 74 20 6e 65 77 53 69 7a 65 3b 0a 20 20   int newSize;.  
26b2e 79 79 53 74 61 63 6b 45 6e 74 72 79 20 2a 70 4e  yyStackEntry *pN
26b2f 65 77 3b 0a 0a 20 20 6e 65 77 53 69 7a 65 20 3d  ew;..  newSize =
26b30 20 70 2d 3e 79 79 73 74 6b 73 7a 2a 32 20 2b 20   p->yystksz*2 + 
26b31 31 30 30 3b 0a 20 20 70 4e 65 77 20 3d 20 72 65  100;.  pNew = re
26b32 61 6c 6c 6f 63 28 70 2d 3e 79 79 73 74 61 63 6b  alloc(p->yystack
26b33 2c 20 6e 65 77 53 69 7a 65 2a 73 69 7a 65 6f 66  , newSize*sizeof
26b34 28 70 4e 65 77 5b 30 5d 29 29 3b 0a 20 20 69 66  (pNew[0]));.  if
26b35 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 70 2d  ( pNew ){.    p-
26b36 3e 79 79 73 74 61 63 6b 20 3d 20 70 4e 65 77 3b  >yystack = pNew;
26b37 0a 20 20 20 20 70 2d 3e 79 79 73 74 6b 73 7a 20  .    p->yystksz 
26b38 3d 20 6e 65 77 53 69 7a 65 3b 0a 23 69 66 6e 64  = newSize;.#ifnd
26b39 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 69 66  ef NDEBUG.    if
26b3a 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b  ( yyTraceFILE ){
26b3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 79  .      fprintf(y
26b3c 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73 53 74  yTraceFILE,"%sSt
26b3d 61 63 6b 20 67 72 6f 77 73 20 74 6f 20 25 64 20  ack grows to %d 
26b3e 65 6e 74 72 69 65 73 21 5c 6e 22 2c 0a 20 20 20  entries!\n",.   
26b3f 20 20 20 20 20 20 20 20 20 20 20 79 79 54 72 61             yyTra
26b40 63 65 50 72 6f 6d 70 74 2c 20 70 2d 3e 79 79 73  cePrompt, p->yys
26b41 74 6b 73 7a 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  tksz);.    }.#en
26b42 64 69 66 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66  dif.  }.}.#endif
26b43 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 66 75  ../* .** This fu
26b44 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 73  nction allocates
26b45 20 61 20 6e 65 77 20 70 61 72 73 65 72 2e 0a 2a   a new parser..*
26b46 2a 20 54 68 65 20 6f 6e 6c 79 20 61 72 67 75 6d  * The only argum
26b47 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ent is a pointer
26b48 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 77   to a function w
26b49 68 69 63 68 20 77 6f 72 6b 73 20 6c 69 6b 65 0a  hich works like.
26b4a 2a 2a 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 0a 2a 2a  ** malloc..**.**
26b4b 20 49 6e 70 75 74 73 3a 0a 2a 2a 20 41 20 70 6f   Inputs:.** A po
26b4c 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 75 6e  inter to the fun
26b4d 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 61 6c  ction used to al
26b4e 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 2e 0a 2a  locate memory..*
26b4f 2a 0a 2a 2a 20 4f 75 74 70 75 74 73 3a 0a 2a 2a  *.** Outputs:.**
26b50 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   A pointer to a 
26b51 70 61 72 73 65 72 2e 20 20 54 68 69 73 20 70 6f  parser.  This po
26b52 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 69 6e  inter is used in
26b53 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
26b54 73 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 50  s.** to sqlite3P
26b55 61 72 73 65 72 20 61 6e 64 20 73 71 6c 69 74 65  arser and sqlite
26b56 33 50 61 72 73 65 72 46 72 65 65 2e 0a 2a 2f 0a  3ParserFree..*/.
26b57 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
26b58 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 72 73  oid *sqlite3Pars
26b59 65 72 41 6c 6c 6f 63 28 76 6f 69 64 20 2a 28 2a  erAlloc(void *(*
26b5a 6d 61 6c 6c 6f 63 50 72 6f 63 29 28 73 69 7a 65  mallocProc)(size
26b5b 5f 74 29 29 7b 0a 20 20 79 79 50 61 72 73 65 72  _t)){.  yyParser
26b5c 20 2a 70 50 61 72 73 65 72 3b 0a 20 20 70 50 61   *pParser;.  pPa
26b5d 72 73 65 72 20 3d 20 28 79 79 50 61 72 73 65 72  rser = (yyParser
26b5e 2a 29 28 2a 6d 61 6c 6c 6f 63 50 72 6f 63 29 28  *)(*mallocProc)(
26b5f 20 28 73 69 7a 65 5f 74 29 73 69 7a 65 6f 66 28   (size_t)sizeof(
26b60 79 79 50 61 72 73 65 72 29 20 29 3b 0a 20 20 69  yyParser) );.  i
26b61 66 28 20 70 50 61 72 73 65 72 20 29 7b 0a 20 20  f( pParser ){.  
26b62 20 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78    pParser->yyidx
26b63 20 3d 20 2d 31 3b 0a 23 69 66 20 59 59 53 54 41   = -1;.#if YYSTA
26b64 43 4b 44 45 50 54 48 3c 3d 30 0a 20 20 20 20 79  CKDEPTH<=0.    y
26b65 79 47 72 6f 77 53 74 61 63 6b 28 70 50 61 72 73  yGrowStack(pPars
26b66 65 72 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a  er);.#endif.  }.
26b67 20 20 72 65 74 75 72 6e 20 70 50 61 72 73 65 72    return pParser
26b68 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c  ;.}../* The foll
26b69 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 64  owing function d
26b6a 65 6c 65 74 65 73 20 74 68 65 20 76 61 6c 75 65  eletes the value
26b6b 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
26b6c 20 61 0a 2a 2a 20 73 79 6d 62 6f 6c 2e 20 20 54   a.** symbol.  T
26b6d 68 65 20 73 79 6d 62 6f 6c 20 63 61 6e 20 62 65  he symbol can be
26b6e 20 65 69 74 68 65 72 20 61 20 74 65 72 6d 69 6e   either a termin
26b6f 61 6c 20 6f 72 20 6e 6f 6e 74 65 72 6d 69 6e 61  al or nontermina
26b70 6c 2e 0a 2a 2a 20 22 79 79 6d 61 6a 6f 72 22 20  l..** "yymajor" 
26b71 69 73 20 74 68 65 20 73 79 6d 62 6f 6c 20 63 6f  is the symbol co
26b72 64 65 2c 20 61 6e 64 20 22 79 79 70 6d 69 6e 6f  de, and "yypmino
26b73 72 22 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  r" is a pointer 
26b74 74 6f 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 2e  to.** the value.
26b75 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
26b76 79 79 5f 64 65 73 74 72 75 63 74 6f 72 28 59 59  yy_destructor(YY
26b77 43 4f 44 45 54 59 50 45 20 79 79 6d 61 6a 6f 72  CODETYPE yymajor
26b78 2c 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 2a 79  , YYMINORTYPE *y
26b79 79 70 6d 69 6e 6f 72 29 7b 0a 20 20 73 77 69 74  ypminor){.  swit
26b7a 63 68 28 20 79 79 6d 61 6a 6f 72 20 29 7b 0a 20  ch( yymajor ){. 
26b7b 20 20 20 2f 2a 20 48 65 72 65 20 69 73 20 69 6e     /* Here is in
26b7c 73 65 72 74 65 64 20 74 68 65 20 61 63 74 69 6f  serted the actio
26b7d 6e 73 20 77 68 69 63 68 20 74 61 6b 65 20 70 6c  ns which take pl
26b7e 61 63 65 20 77 68 65 6e 20 61 0a 20 20 20 20 2a  ace when a.    *
26b7f 2a 20 74 65 72 6d 69 6e 61 6c 20 6f 72 20 6e 6f  * terminal or no
26b80 6e 2d 74 65 72 6d 69 6e 61 6c 20 69 73 20 64 65  n-terminal is de
26b81 73 74 72 6f 79 65 64 2e 20 20 54 68 69 73 20 63  stroyed.  This c
26b82 61 6e 20 68 61 70 70 65 6e 0a 20 20 20 20 2a 2a  an happen.    **
26b83 20 77 68 65 6e 20 74 68 65 20 73 79 6d 62 6f 6c   when the symbol
26b84 20 69 73 20 70 6f 70 70 65 64 20 66 72 6f 6d 20   is popped from 
26b85 74 68 65 20 73 74 61 63 6b 20 64 75 72 69 6e 67  the stack during
26b86 20 61 0a 20 20 20 20 2a 2a 20 72 65 64 75 63 65   a.    ** reduce
26b87 20 6f 72 20 64 75 72 69 6e 67 20 65 72 72 6f 72   or during error
26b88 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 72 20 77   processing or w
26b89 68 65 6e 20 61 20 70 61 72 73 65 72 20 69 73 20  hen a parser is 
26b8a 0a 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 64 65  .    ** being de
26b8b 73 74 72 6f 79 65 64 20 62 65 66 6f 72 65 20 69  stroyed before i
26b8c 74 20 69 73 20 66 69 6e 69 73 68 65 64 20 70 61  t is finished pa
26b8d 72 73 69 6e 67 2e 0a 20 20 20 20 2a 2a 0a 20 20  rsing..    **.  
26b8e 20 20 2a 2a 20 4e 6f 74 65 3a 20 64 75 72 69 6e    ** Note: durin
26b8f 67 20 61 20 72 65 64 75 63 65 2c 20 74 68 65 20  g a reduce, the 
26b90 6f 6e 6c 79 20 73 79 6d 62 6f 6c 73 20 64 65 73  only symbols des
26b91 74 72 6f 79 65 64 20 61 72 65 20 74 68 6f 73 65  troyed are those
26b92 0a 20 20 20 20 2a 2a 20 77 68 69 63 68 20 61 70  .    ** which ap
26b93 70 65 61 72 20 6f 6e 20 74 68 65 20 52 48 53 20  pear on the RHS 
26b94 6f 66 20 74 68 65 20 72 75 6c 65 2c 20 62 75 74  of the rule, but
26b95 20 77 68 69 63 68 20 61 72 65 20 6e 6f 74 20 75   which are not u
26b96 73 65 64 0a 20 20 20 20 2a 2a 20 69 6e 73 69 64  sed.    ** insid
26b97 65 20 74 68 65 20 43 20 63 6f 64 65 2e 0a 20 20  e the C code..  
26b98 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 35    */.    case 15
26b99 35 3a 20 2f 2a 20 73 65 6c 65 63 74 20 2a 2f 0a  5: /* select */.
26b9a 7b 0a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  {.sqlite3SelectD
26b9b 65 6c 65 74 65 28 28 79 79 70 6d 69 6e 6f 72 2d  elete((yypminor-
26b9c 3e 79 79 32 31 39 29 29 3b 0a 7d 0a 20 20 20 20  >yy219));.}.    
26b9d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
26b9e 65 20 31 36 39 3a 20 2f 2a 20 74 65 72 6d 20 2a  e 169: /* term *
26b9f 2f 0a 7b 0a 73 71 6c 69 74 65 33 45 78 70 72 44  /.{.sqlite3ExprD
26ba0 65 6c 65 74 65 28 28 79 79 70 6d 69 6e 6f 72 2d  elete((yypminor-
26ba1 3e 79 79 31 37 32 29 29 3b 0a 7d 0a 20 20 20 20  >yy172));.}.    
26ba2 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
26ba3 65 20 31 37 30 3a 20 2f 2a 20 65 78 70 72 20 2a  e 170: /* expr *
26ba4 2f 0a 7b 0a 73 71 6c 69 74 65 33 45 78 70 72 44  /.{.sqlite3ExprD
26ba5 65 6c 65 74 65 28 28 79 79 70 6d 69 6e 6f 72 2d  elete((yypminor-
26ba6 3e 79 79 31 37 32 29 29 3b 0a 7d 0a 20 20 20 20  >yy172));.}.    
26ba7 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
26ba8 65 20 31 37 34 3a 20 2f 2a 20 69 64 78 6c 69 73  e 174: /* idxlis
26ba9 74 5f 6f 70 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74  t_opt */.{.sqlit
26baa 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
26bab 28 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 31 37  ((yypminor->yy17
26bac 34 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65  4));.}.      bre
26bad 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 31 38 32  ak;.    case 182
26bae 3a 20 2f 2a 20 69 64 78 6c 69 73 74 20 2a 2f 0a  : /* idxlist */.
26baf 7b 0a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  {.sqlite3ExprLis
26bb0 74 44 65 6c 65 74 65 28 28 79 79 70 6d 69 6e 6f  tDelete((yypmino
26bb1 72 2d 3e 79 79 31 37 34 29 29 3b 0a 7d 0a 20 20  r->yy174));.}.  
26bb2 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
26bb3 61 73 65 20 31 38 38 3a 20 2f 2a 20 66 75 6c 6c  ase 188: /* full
26bb4 6e 61 6d 65 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65  name */.{.sqlite
26bb5 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 28  3SrcListDelete((
26bb6 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 33 37 33 29  yypminor->yy373)
26bb7 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b  );.}.      break
26bb8 3b 0a 20 20 20 20 63 61 73 65 20 31 38 39 3a 20  ;.    case 189: 
26bb9 2f 2a 20 6f 6e 65 73 65 6c 65 63 74 20 2a 2f 0a  /* oneselect */.
26bba 7b 0a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  {.sqlite3SelectD
26bbb 65 6c 65 74 65 28 28 79 79 70 6d 69 6e 6f 72 2d  elete((yypminor-
26bbc 3e 79 79 32 31 39 29 29 3b 0a 7d 0a 20 20 20 20  >yy219));.}.    
26bbd 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
26bbe 65 20 31 39 32 3a 20 2f 2a 20 73 65 6c 63 6f 6c  e 192: /* selcol
26bbf 6c 69 73 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65  list */.{.sqlite
26bc0 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
26bc1 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 31 37 34  (yypminor->yy174
26bc2 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61  ));.}.      brea
26bc3 6b 3b 0a 20 20 20 20 63 61 73 65 20 31 39 33 3a  k;.    case 193:
26bc4 20 2f 2a 20 66 72 6f 6d 20 2a 2f 0a 7b 0a 73 71   /* from */.{.sq
26bc5 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65  lite3SrcListDele
26bc6 74 65 28 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79  te((yypminor->yy
26bc7 33 37 33 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62  373));.}.      b
26bc8 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 31  reak;.    case 1
26bc9 39 34 3a 20 2f 2a 20 77 68 65 72 65 5f 6f 70 74  94: /* where_opt
26bca 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 45 78 70   */.{.sqlite3Exp
26bcb 72 44 65 6c 65 74 65 28 28 79 79 70 6d 69 6e 6f  rDelete((yypmino
26bcc 72 2d 3e 79 79 31 37 32 29 29 3b 0a 7d 0a 20 20  r->yy172));.}.  
26bcd 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
26bce 61 73 65 20 31 39 35 3a 20 2f 2a 20 67 72 6f 75  ase 195: /* grou
26bcf 70 62 79 5f 6f 70 74 20 2a 2f 0a 7b 0a 73 71 6c  pby_opt */.{.sql
26bd0 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
26bd1 74 65 28 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79  te((yypminor->yy
26bd2 31 37 34 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62  174));.}.      b
26bd3 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 31  reak;.    case 1
26bd4 39 36 3a 20 2f 2a 20 68 61 76 69 6e 67 5f 6f 70  96: /* having_op
26bd5 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 45 78  t */.{.sqlite3Ex
26bd6 70 72 44 65 6c 65 74 65 28 28 79 79 70 6d 69 6e  prDelete((yypmin
26bd7 6f 72 2d 3e 79 79 31 37 32 29 29 3b 0a 7d 0a 20  or->yy172));.}. 
26bd8 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
26bd9 63 61 73 65 20 31 39 37 3a 20 2f 2a 20 6f 72 64  case 197: /* ord
26bda 65 72 62 79 5f 6f 70 74 20 2a 2f 0a 7b 0a 73 71  erby_opt */.{.sq
26bdb 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
26bdc 65 74 65 28 28 79 79 70 6d 69 6e 6f 72 2d 3e 79  ete((yypminor->y
26bdd 79 31 37 34 29 29 3b 0a 7d 0a 20 20 20 20 20 20  y174));.}.      
26bde 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
26bdf 31 39 39 3a 20 2f 2a 20 73 63 6c 70 20 2a 2f 0a  199: /* sclp */.
26be0 7b 0a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  {.sqlite3ExprLis
26be1 74 44 65 6c 65 74 65 28 28 79 79 70 6d 69 6e 6f  tDelete((yypmino
26be2 72 2d 3e 79 79 31 37 34 29 29 3b 0a 7d 0a 20 20  r->yy174));.}.  
26be3 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
26be4 61 73 65 20 32 30 31 3a 20 2f 2a 20 73 65 6c 74  ase 201: /* selt
26be5 61 62 6c 69 73 74 20 2a 2f 0a 7b 0a 73 71 6c 69  ablist */.{.sqli
26be6 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65  te3SrcListDelete
26be7 28 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 33 37  ((yypminor->yy37
26be8 33 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65  3));.}.      bre
26be9 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 32 30 32  ak;.    case 202
26bea 3a 20 2f 2a 20 73 74 6c 5f 70 72 65 66 69 78 20  : /* stl_prefix 
26beb 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 53 72 63 4c  */.{.sqlite3SrcL
26bec 69 73 74 44 65 6c 65 74 65 28 28 79 79 70 6d 69  istDelete((yypmi
26bed 6e 6f 72 2d 3e 79 79 33 37 33 29 29 3b 0a 7d 0a  nor->yy373));.}.
26bee 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
26bef 20 63 61 73 65 20 32 30 34 3a 20 2f 2a 20 6f 6e   case 204: /* on
26bf0 5f 6f 70 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65  _opt */.{.sqlite
26bf1 33 45 78 70 72 44 65 6c 65 74 65 28 28 79 79 70  3ExprDelete((yyp
26bf2 6d 69 6e 6f 72 2d 3e 79 79 31 37 32 29 29 3b 0a  minor->yy172));.
26bf3 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
26bf4 20 20 20 63 61 73 65 20 32 30 35 3a 20 2f 2a 20     case 205: /* 
26bf5 75 73 69 6e 67 5f 6f 70 74 20 2a 2f 0a 7b 0a 73  using_opt */.{.s
26bf6 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65  qlite3IdListDele
26bf7 74 65 28 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79  te((yypminor->yy
26bf8 34 33 32 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62  432));.}.      b
26bf9 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 32  reak;.    case 2
26bfa 30 36 3a 20 2f 2a 20 73 65 6c 74 61 62 6c 69 73  06: /* seltablis
26bfb 74 5f 70 61 72 65 6e 20 2a 2f 0a 7b 0a 73 71 6c  t_paren */.{.sql
26bfc 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
26bfd 28 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 32 31  ((yypminor->yy21
26bfe 39 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65  9));.}.      bre
26bff 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 32 30 38  ak;.    case 208
26c00 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 20  : /* inscollist 
26c01 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 49 64 4c 69  */.{.sqlite3IdLi
26c02 73 74 44 65 6c 65 74 65 28 28 79 79 70 6d 69 6e  stDelete((yypmin
26c03 6f 72 2d 3e 79 79 34 33 32 29 29 3b 0a 7d 0a 20  or->yy432));.}. 
26c04 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
26c05 63 61 73 65 20 32 30 39 3a 20 2f 2a 20 73 6f 72  case 209: /* sor
26c06 74 6c 69 73 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74  tlist */.{.sqlit
26c07 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
26c08 28 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 31 37  ((yypminor->yy17
26c09 34 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65  4));.}.      bre
26c0a 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 32 31 30  ak;.    case 210
26c0b 3a 20 2f 2a 20 73 6f 72 74 69 74 65 6d 20 2a 2f  : /* sortitem */
26c0c 0a 7b 0a 73 71 6c 69 74 65 33 45 78 70 72 44 65  .{.sqlite3ExprDe
26c0d 6c 65 74 65 28 28 79 79 70 6d 69 6e 6f 72 2d 3e  lete((yypminor->
26c0e 79 79 31 37 32 29 29 3b 0a 7d 0a 20 20 20 20 20  yy172));.}.     
26c0f 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
26c10 20 32 31 31 3a 20 2f 2a 20 6e 65 78 70 72 6c 69   211: /* nexprli
26c11 73 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 45  st */.{.sqlite3E
26c12 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 28 79  xprListDelete((y
26c13 79 70 6d 69 6e 6f 72 2d 3e 79 79 31 37 34 29 29  ypminor->yy174))
26c14 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.}.      break;
26c15 0a 20 20 20 20 63 61 73 65 20 32 31 32 3a 20 2f  .    case 212: /
26c16 2a 20 73 65 74 6c 69 73 74 20 2a 2f 0a 7b 0a 73  * setlist */.{.s
26c17 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
26c18 6c 65 74 65 28 28 79 79 70 6d 69 6e 6f 72 2d 3e  lete((yypminor->
26c19 79 79 31 37 34 29 29 3b 0a 7d 0a 20 20 20 20 20  yy174));.}.     
26c1a 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
26c1b 20 32 31 34 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c   214: /* inscoll
26c1c 69 73 74 5f 6f 70 74 20 2a 2f 0a 7b 0a 73 71 6c  ist_opt */.{.sql
26c1d 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65  ite3IdListDelete
26c1e 28 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 34 33  ((yypminor->yy43
26c1f 32 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65  2));.}.      bre
26c20 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 32 31 35  ak;.    case 215
26c21 3a 20 2f 2a 20 69 74 65 6d 6c 69 73 74 20 2a 2f  : /* itemlist */
26c22 0a 7b 0a 73 71 6c 69 74 65 33 45 78 70 72 4c 69  .{.sqlite3ExprLi
26c23 73 74 44 65 6c 65 74 65 28 28 79 79 70 6d 69 6e  stDelete((yypmin
26c24 6f 72 2d 3e 79 79 31 37 34 29 29 3b 0a 7d 0a 20  or->yy174));.}. 
26c25 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
26c26 63 61 73 65 20 32 31 36 3a 20 2f 2a 20 65 78 70  case 216: /* exp
26c27 72 6c 69 73 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74  rlist */.{.sqlit
26c28 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
26c29 28 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 31 37  ((yypminor->yy17
26c2a 34 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65  4));.}.      bre
26c2b 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 32 31 38  ak;.    case 218
26c2c 3a 20 2f 2a 20 65 73 63 61 70 65 20 2a 2f 0a 7b  : /* escape */.{
26c2d 0a 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65  .sqlite3ExprDele
26c2e 74 65 28 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79  te((yypminor->yy
26c2f 31 37 32 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62  172));.}.      b
26c30 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 32  reak;.    case 2
26c31 32 31 3a 20 2f 2a 20 63 61 73 65 5f 6f 70 65 72  21: /* case_oper
26c32 61 6e 64 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33  and */.{.sqlite3
26c33 45 78 70 72 44 65 6c 65 74 65 28 28 79 79 70 6d  ExprDelete((yypm
26c34 69 6e 6f 72 2d 3e 79 79 31 37 32 29 29 3b 0a 7d  inor->yy172));.}
26c35 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
26c36 20 20 63 61 73 65 20 32 32 32 3a 20 2f 2a 20 63    case 222: /* c
26c37 61 73 65 5f 65 78 70 72 6c 69 73 74 20 2a 2f 0a  ase_exprlist */.
26c38 7b 0a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  {.sqlite3ExprLis
26c39 74 44 65 6c 65 74 65 28 28 79 79 70 6d 69 6e 6f  tDelete((yypmino
26c3a 72 2d 3e 79 79 31 37 34 29 29 3b 0a 7d 0a 20 20  r->yy174));.}.  
26c3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
26c3c 61 73 65 20 32 32 33 3a 20 2f 2a 20 63 61 73 65  ase 223: /* case
26c3d 5f 65 6c 73 65 20 2a 2f 0a 7b 0a 73 71 6c 69 74  _else */.{.sqlit
26c3e 65 33 45 78 70 72 44 65 6c 65 74 65 28 28 79 79  e3ExprDelete((yy
26c3f 70 6d 69 6e 6f 72 2d 3e 79 79 31 37 32 29 29 3b  pminor->yy172));
26c40 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .}.      break;.
26c41 20 20 20 20 63 61 73 65 20 32 33 31 3a 20 2f 2a      case 231: /*
26c42 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73   trigger_cmd_lis
26c43 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 44 65  t */.{.sqlite3De
26c44 6c 65 74 65 54 72 69 67 67 65 72 53 74 65 70 28  leteTriggerStep(
26c45 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 32 34 33  (yypminor->yy243
26c46 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61  ));.}.      brea
26c47 6b 3b 0a 20 20 20 20 63 61 73 65 20 32 33 33 3a  k;.    case 233:
26c48 20 2f 2a 20 74 72 69 67 67 65 72 5f 65 76 65 6e   /* trigger_even
26c49 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 49 64  t */.{.sqlite3Id
26c4a 4c 69 73 74 44 65 6c 65 74 65 28 28 79 79 70 6d  ListDelete((yypm
26c4b 69 6e 6f 72 2d 3e 79 79 33 37 30 29 2e 62 29 3b  inor->yy370).b);
26c4c 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .}.      break;.
26c4d 20 20 20 20 63 61 73 65 20 32 33 35 3a 20 2f 2a      case 235: /*
26c4e 20 77 68 65 6e 5f 63 6c 61 75 73 65 20 2a 2f 0a   when_clause */.
26c4f 7b 0a 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c  {.sqlite3ExprDel
26c50 65 74 65 28 28 79 79 70 6d 69 6e 6f 72 2d 3e 79  ete((yypminor->y
26c51 79 31 37 32 29 29 3b 0a 7d 0a 20 20 20 20 20 20  y172));.}.      
26c52 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
26c53 32 33 36 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  236: /* trigger_
26c54 63 6d 64 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33  cmd */.{.sqlite3
26c55 44 65 6c 65 74 65 54 72 69 67 67 65 72 53 74 65  DeleteTriggerSte
26c56 70 28 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 32  p((yypminor->yy2
26c57 34 33 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72  43));.}.      br
26c58 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 32 33  eak;.    case 23
26c59 38 3a 20 2f 2a 20 6b 65 79 5f 6f 70 74 20 2a 2f  8: /* key_opt */
26c5a 0a 7b 0a 73 71 6c 69 74 65 33 45 78 70 72 44 65  .{.sqlite3ExprDe
26c5b 6c 65 74 65 28 28 79 79 70 6d 69 6e 6f 72 2d 3e  lete((yypminor->
26c5c 79 79 31 37 32 29 29 3b 0a 7d 0a 20 20 20 20 20  yy172));.}.     
26c5d 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61   break;.    defa
26c5e 75 6c 74 3a 20 20 62 72 65 61 6b 3b 20 20 20 2f  ult:  break;   /
26c5f 2a 20 49 66 20 6e 6f 20 64 65 73 74 72 75 63 74  * If no destruct
26c60 6f 72 20 61 63 74 69 6f 6e 20 73 70 65 63 69 66  or action specif
26c61 69 65 64 3a 20 64 6f 20 6e 6f 74 68 69 6e 67 20  ied: do nothing 
26c62 2a 2f 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  */.  }.}../*.** 
26c63 50 6f 70 20 74 68 65 20 70 61 72 73 65 72 27 73  Pop the parser's
26c64 20 73 74 61 63 6b 20 6f 6e 63 65 2e 0a 2a 2a 0a   stack once..**.
26c65 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61  ** If there is a
26c66 20 64 65 73 74 72 75 63 74 6f 72 20 72 6f 75 74   destructor rout
26c67 69 6e 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  ine associated w
26c68 69 74 68 20 74 68 65 20 74 6f 6b 65 6e 20 77 68  ith the token wh
26c69 69 63 68 0a 2a 2a 20 69 73 20 70 6f 70 70 65 64  ich.** is popped
26c6a 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63 6b 2c   from the stack,
26c6b 20 74 68 65 6e 20 63 61 6c 6c 20 69 74 2e 0a 2a   then call it..*
26c6c 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
26c6d 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62  major token numb
26c6e 65 72 20 66 6f 72 20 74 68 65 20 73 79 6d 62 6f  er for the symbo
26c6f 6c 20 70 6f 70 70 65 64 2e 0a 2a 2f 0a 73 74 61  l popped..*/.sta
26c70 74 69 63 20 69 6e 74 20 79 79 5f 70 6f 70 5f 70  tic int yy_pop_p
26c71 61 72 73 65 72 5f 73 74 61 63 6b 28 79 79 50 61  arser_stack(yyPa
26c72 72 73 65 72 20 2a 70 50 61 72 73 65 72 29 7b 0a  rser *pParser){.
26c73 20 20 59 59 43 4f 44 45 54 59 50 45 20 79 79 6d    YYCODETYPE yym
26c74 61 6a 6f 72 3b 0a 20 20 79 79 53 74 61 63 6b 45  ajor;.  yyStackE
26c75 6e 74 72 79 20 2a 79 79 74 6f 73 20 3d 20 26 70  ntry *yytos = &p
26c76 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b  Parser->yystack[
26c77 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 3b  pParser->yyidx];
26c78 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 72 2d  ..  if( pParser-
26c79 3e 79 79 69 64 78 3c 30 20 29 20 72 65 74 75 72  >yyidx<0 ) retur
26c7a 6e 20 30 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45  n 0;.#ifndef NDE
26c7b 42 55 47 0a 20 20 69 66 28 20 79 79 54 72 61 63  BUG.  if( yyTrac
26c7c 65 46 49 4c 45 20 26 26 20 70 50 61 72 73 65 72  eFILE && pParser
26c7d 2d 3e 79 79 69 64 78 3e 3d 30 20 29 7b 0a 20 20  ->yyidx>=0 ){.  
26c7e 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63    fprintf(yyTrac
26c7f 65 46 49 4c 45 2c 22 25 73 50 6f 70 70 69 6e 67  eFILE,"%sPopping
26c80 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 79 79   %s\n",.      yy
26c81 54 72 61 63 65 50 72 6f 6d 70 74 2c 0a 20 20 20  TracePrompt,.   
26c82 20 20 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79     yyTokenName[y
26c83 79 74 6f 73 2d 3e 6d 61 6a 6f 72 5d 29 3b 0a 20  ytos->major]);. 
26c84 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79 79 6d 61   }.#endif.  yyma
26c85 6a 6f 72 20 3d 20 79 79 74 6f 73 2d 3e 6d 61 6a  jor = yytos->maj
26c86 6f 72 3b 0a 20 20 79 79 5f 64 65 73 74 72 75 63  or;.  yy_destruc
26c87 74 6f 72 28 20 79 79 6d 61 6a 6f 72 2c 20 26 79  tor( yymajor, &y
26c88 79 74 6f 73 2d 3e 6d 69 6e 6f 72 29 3b 0a 20 20  ytos->minor);.  
26c89 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 2d 2d  pParser->yyidx--
26c8a 3b 0a 20 20 72 65 74 75 72 6e 20 79 79 6d 61 6a  ;.  return yymaj
26c8b 6f 72 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 44 65  or;.}../* .** De
26c8c 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 64 65 73  allocate and des
26c8d 74 72 6f 79 20 61 20 70 61 72 73 65 72 2e 20 20  troy a parser.  
26c8e 44 65 73 74 72 75 63 74 6f 72 73 20 61 72 65 20  Destructors are 
26c8f 61 6c 6c 20 63 61 6c 6c 65 64 20 66 6f 72 0a 2a  all called for.*
26c90 2a 20 61 6c 6c 20 73 74 61 63 6b 20 65 6c 65 6d  * all stack elem
26c91 65 6e 74 73 20 62 65 66 6f 72 65 20 73 68 75 74  ents before shut
26c92 74 69 6e 67 20 74 68 65 20 70 61 72 73 65 72 20  ting the parser 
26c93 64 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70 75  down..**.** Inpu
26c94 74 73 3a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c  ts:.** <ul>.** <
26c95 6c 69 3e 20 20 41 20 70 6f 69 6e 74 65 72 20 74  li>  A pointer t
26c96 6f 20 74 68 65 20 70 61 72 73 65 72 2e 20 20 54  o the parser.  T
26c97 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 61 20  his should be a 
26c98 70 6f 69 6e 74 65 72 0a 2a 2a 20 20 20 20 20 20  pointer.**      
26c99 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
26c9a 71 6c 69 74 65 33 50 61 72 73 65 72 41 6c 6c 6f  qlite3ParserAllo
26c9b 63 2e 0a 2a 2a 20 3c 6c 69 3e 20 20 41 20 70 6f  c..** <li>  A po
26c9c 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74  inter to a funct
26c9d 69 6f 6e 20 75 73 65 64 20 74 6f 20 72 65 63 6c  ion used to recl
26c9e 61 69 6d 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69  aim memory obtai
26c9f 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 66 72 6f  ned.**       fro
26ca0 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 20 3c 2f 75  m malloc..** </u
26ca1 6c 3e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  l>.*/.SQLITE_PRI
26ca2 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
26ca3 33 50 61 72 73 65 72 46 72 65 65 28 0a 20 20 76  3ParserFree(.  v
26ca4 6f 69 64 20 2a 70 2c 20 20 20 20 20 20 20 20 20  oid *p,         
26ca5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
26ca6 65 20 70 61 72 73 65 72 20 74 6f 20 62 65 20 64  e parser to be d
26ca7 65 6c 65 74 65 64 20 2a 2f 0a 20 20 76 6f 69 64  eleted */.  void
26ca8 20 28 2a 66 72 65 65 50 72 6f 63 29 28 76 6f 69   (*freeProc)(voi
26ca9 64 2a 29 20 20 20 20 20 2f 2a 20 46 75 6e 63 74  d*)     /* Funct
26caa 69 6f 6e 20 75 73 65 64 20 74 6f 20 72 65 63 6c  ion used to recl
26cab 61 69 6d 20 6d 65 6d 6f 72 79 20 2a 2f 0a 29 7b  aim memory */.){
26cac 0a 20 20 79 79 50 61 72 73 65 72 20 2a 70 50 61  .  yyParser *pPa
26cad 72 73 65 72 20 3d 20 28 79 79 50 61 72 73 65 72  rser = (yyParser
26cae 2a 29 70 3b 0a 20 20 69 66 28 20 70 50 61 72 73  *)p;.  if( pPars
26caf 65 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  er==0 ) return;.
26cb0 20 20 77 68 69 6c 65 28 20 70 50 61 72 73 65 72    while( pParser
26cb1 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20 79 79 5f  ->yyidx>=0 ) yy_
26cb2 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b  pop_parser_stack
26cb3 28 70 50 61 72 73 65 72 29 3b 0a 23 69 66 20 59  (pParser);.#if Y
26cb4 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a 20  YSTACKDEPTH<=0. 
26cb5 20 66 72 65 65 28 70 50 61 72 73 65 72 2d 3e 79   free(pParser->y
26cb6 79 73 74 61 63 6b 29 3b 0a 23 65 6e 64 69 66 0a  ystack);.#endif.
26cb7 20 20 28 2a 66 72 65 65 50 72 6f 63 29 28 28 76    (*freeProc)((v
26cb8 6f 69 64 2a 29 70 50 61 72 73 65 72 29 3b 0a 7d  oid*)pParser);.}
26cb9 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65  ../*.** Find the
26cba 20 61 70 70 72 6f 70 72 69 61 74 65 20 61 63 74   appropriate act
26cbb 69 6f 6e 20 66 6f 72 20 61 20 70 61 72 73 65 72  ion for a parser
26cbc 20 67 69 76 65 6e 20 74 68 65 20 74 65 72 6d 69   given the termi
26cbd 6e 61 6c 0a 2a 2a 20 6c 6f 6f 6b 2d 61 68 65 61  nal.** look-ahea
26cbe 64 20 74 6f 6b 65 6e 20 69 4c 6f 6f 6b 41 68 65  d token iLookAhe
26cbf 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ad..**.** If the
26cc0 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65   look-ahead toke
26cc1 6e 20 69 73 20 59 59 4e 4f 43 4f 44 45 2c 20 74  n is YYNOCODE, t
26cc2 68 65 6e 20 63 68 65 63 6b 20 74 6f 20 73 65 65  hen check to see
26cc3 20 69 66 20 74 68 65 20 61 63 74 69 6f 6e 20 69   if the action i
26cc4 73 0a 2a 2a 20 69 6e 64 65 70 65 6e 64 65 6e 74  s.** independent
26cc5 20 6f 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68 65   of the look-ahe
26cc6 61 64 2e 20 20 49 66 20 69 74 20 69 73 2c 20 72  ad.  If it is, r
26cc7 65 74 75 72 6e 20 74 68 65 20 61 63 74 69 6f 6e  eturn the action
26cc8 2c 20 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20 72  , otherwise.** r
26cc9 65 74 75 72 6e 20 59 59 5f 4e 4f 5f 41 43 54 49  eturn YY_NO_ACTI
26cca 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ON..*/.static in
26ccb 74 20 79 79 5f 66 69 6e 64 5f 73 68 69 66 74 5f  t yy_find_shift_
26ccc 61 63 74 69 6f 6e 28 0a 20 20 79 79 50 61 72 73  action(.  yyPars
26ccd 65 72 20 2a 70 50 61 72 73 65 72 2c 20 20 20 20  er *pParser,    
26cce 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
26ccf 72 20 2a 2f 0a 20 20 59 59 43 4f 44 45 54 59 50  r */.  YYCODETYP
26cd0 45 20 69 4c 6f 6f 6b 41 68 65 61 64 20 20 20 20  E iLookAhead    
26cd1 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b 2d 61 68 65   /* The look-ahe
26cd2 61 64 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20  ad token */.){. 
26cd3 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 73 74   int i;.  int st
26cd4 61 74 65 6e 6f 20 3d 20 70 50 61 72 73 65 72 2d  ateno = pParser-
26cd5 3e 79 79 73 74 61 63 6b 5b 70 50 61 72 73 65 72  >yystack[pParser
26cd6 2d 3e 79 79 69 64 78 5d 2e 73 74 61 74 65 6e 6f  ->yyidx].stateno
26cd7 3b 0a 20 0a 20 20 69 66 28 20 73 74 61 74 65 6e  ;. .  if( staten
26cd8 6f 3e 59 59 5f 53 48 49 46 54 5f 4d 41 58 20 7c  o>YY_SHIFT_MAX |
26cd9 7c 20 28 69 20 3d 20 79 79 5f 73 68 69 66 74 5f  | (i = yy_shift_
26cda 6f 66 73 74 5b 73 74 61 74 65 6e 6f 5d 29 3d 3d  ofst[stateno])==
26cdb 59 59 5f 53 48 49 46 54 5f 55 53 45 5f 44 46 4c  YY_SHIFT_USE_DFL
26cdc 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  T ){.    return 
26cdd 79 79 5f 64 65 66 61 75 6c 74 5b 73 74 61 74 65  yy_default[state
26cde 6e 6f 5d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  no];.  }.  asser
26cdf 74 28 20 69 4c 6f 6f 6b 41 68 65 61 64 21 3d 59  t( iLookAhead!=Y
26ce0 59 4e 4f 43 4f 44 45 20 29 3b 0a 20 20 69 20 2b  YNOCODE );.  i +
26ce1 3d 20 69 4c 6f 6f 6b 41 68 65 61 64 3b 0a 20 20  = iLookAhead;.  
26ce2 69 66 28 20 69 3c 30 20 7c 7c 20 69 3e 3d 59 59  if( i<0 || i>=YY
26ce3 5f 53 5a 5f 41 43 54 54 41 42 20 7c 7c 20 79 79  _SZ_ACTTAB || yy
26ce4 5f 6c 6f 6f 6b 61 68 65 61 64 5b 69 5d 21 3d 69  _lookahead[i]!=i
26ce5 4c 6f 6f 6b 41 68 65 61 64 20 29 7b 0a 20 20 20  LookAhead ){.   
26ce6 20 69 66 28 20 69 4c 6f 6f 6b 41 68 65 61 64 3e   if( iLookAhead>
26ce7 30 20 29 7b 0a 23 69 66 64 65 66 20 59 59 46 41  0 ){.#ifdef YYFA
26ce8 4c 4c 42 41 43 4b 0a 20 20 20 20 20 20 69 6e 74  LLBACK.      int
26ce9 20 69 46 61 6c 6c 62 61 63 6b 3b 20 20 20 20 20   iFallback;     
26cea 20 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 62 61         /* Fallba
26ceb 63 6b 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20  ck token */.    
26cec 20 20 69 66 28 20 69 4c 6f 6f 6b 41 68 65 61 64    if( iLookAhead
26ced 3c 73 69 7a 65 6f 66 28 79 79 46 61 6c 6c 62 61  <sizeof(yyFallba
26cee 63 6b 29 2f 73 69 7a 65 6f 66 28 79 79 46 61 6c  ck)/sizeof(yyFal
26cef 6c 62 61 63 6b 5b 30 5d 29 0a 20 20 20 20 20 20  lback[0]).      
26cf0 20 20 20 20 20 20 20 26 26 20 28 69 46 61 6c 6c         && (iFall
26cf1 62 61 63 6b 20 3d 20 79 79 46 61 6c 6c 62 61 63  back = yyFallbac
26cf2 6b 5b 69 4c 6f 6f 6b 41 68 65 61 64 5d 29 21 3d  k[iLookAhead])!=
26cf3 30 20 29 7b 0a 23 69 66 6e 64 65 66 20 4e 44 45  0 ){.#ifndef NDE
26cf4 42 55 47 0a 20 20 20 20 20 20 20 20 69 66 28 20  BUG.        if( 
26cf5 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20  yyTraceFILE ){. 
26cf6 20 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66           fprintf
26cf7 28 79 79 54 72 61 63 65 46 49 4c 45 2c 20 22 25  (yyTraceFILE, "%
26cf8 73 46 41 4c 4c 42 41 43 4b 20 25 73 20 3d 3e 20  sFALLBACK %s => 
26cf9 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  %s\n",.         
26cfa 20 20 20 20 79 79 54 72 61 63 65 50 72 6f 6d 70      yyTracePromp
26cfb 74 2c 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 69  t, yyTokenName[i
26cfc 4c 6f 6f 6b 41 68 65 61 64 5d 2c 20 79 79 54 6f  LookAhead], yyTo
26cfd 6b 65 6e 4e 61 6d 65 5b 69 46 61 6c 6c 62 61 63  kenName[iFallbac
26cfe 6b 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23  k]);.        }.#
26cff 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 72 65  endif.        re
26d00 74 75 72 6e 20 79 79 5f 66 69 6e 64 5f 73 68 69  turn yy_find_shi
26d01 66 74 5f 61 63 74 69 6f 6e 28 70 50 61 72 73 65  ft_action(pParse
26d02 72 2c 20 69 46 61 6c 6c 62 61 63 6b 29 3b 0a 20  r, iFallback);. 
26d03 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69       }.#endif.#i
26d04 66 64 65 66 20 59 59 57 49 4c 44 43 41 52 44 0a  fdef YYWILDCARD.
26d05 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20        {.        
26d06 69 6e 74 20 6a 20 3d 20 69 20 2d 20 69 4c 6f 6f  int j = i - iLoo
26d07 6b 41 68 65 61 64 20 2b 20 59 59 57 49 4c 44 43  kAhead + YYWILDC
26d08 41 52 44 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ARD;.        if(
26d09 20 6a 3e 3d 30 20 26 26 20 6a 3c 59 59 5f 53 5a   j>=0 && j<YY_SZ
26d0a 5f 41 43 54 54 41 42 20 26 26 20 79 79 5f 6c 6f  _ACTTAB && yy_lo
26d0b 6f 6b 61 68 65 61 64 5b 6a 5d 3d 3d 59 59 57 49  okahead[j]==YYWI
26d0c 4c 44 43 41 52 44 20 29 7b 0a 23 69 66 6e 64 65  LDCARD ){.#ifnde
26d0d 66 20 4e 44 45 42 55 47 0a 20 20 20 20 20 20 20  f NDEBUG.       
26d0e 20 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49     if( yyTraceFI
26d0f 4c 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  LE ){.          
26d10 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63    fprintf(yyTrac
26d11 65 46 49 4c 45 2c 20 22 25 73 57 49 4c 44 43 41  eFILE, "%sWILDCA
26d12 52 44 20 25 73 20 3d 3e 20 25 73 5c 6e 22 2c 0a  RD %s => %s\n",.
26d13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 79                 y
26d14 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 20 79 79  yTracePrompt, yy
26d15 54 6f 6b 65 6e 4e 61 6d 65 5b 69 4c 6f 6f 6b 41  TokenName[iLookA
26d16 68 65 61 64 5d 2c 20 79 79 54 6f 6b 65 6e 4e 61  head], yyTokenNa
26d17 6d 65 5b 59 59 57 49 4c 44 43 41 52 44 5d 29 3b  me[YYWILDCARD]);
26d18 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 23 65 6e  .          }.#en
26d19 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f  dif /* NDEBUG */
26d1a 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
26d1b 6e 20 79 79 5f 61 63 74 69 6f 6e 5b 6a 5d 3b 0a  n yy_action[j];.
26d1c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
26d1d 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 59 59 57 49  }.#endif /* YYWI
26d1e 4c 44 43 41 52 44 20 2a 2f 0a 20 20 20 20 7d 0a  LDCARD */.    }.
26d1f 20 20 20 20 72 65 74 75 72 6e 20 79 79 5f 64 65      return yy_de
26d20 66 61 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a  fault[stateno];.
26d21 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
26d22 75 72 6e 20 79 79 5f 61 63 74 69 6f 6e 5b 69 5d  urn yy_action[i]
26d23 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  ;.  }.}../*.** F
26d24 69 6e 64 20 74 68 65 20 61 70 70 72 6f 70 72 69  ind the appropri
26d25 61 74 65 20 61 63 74 69 6f 6e 20 66 6f 72 20 61  ate action for a
26d26 20 70 61 72 73 65 72 20 67 69 76 65 6e 20 74 68   parser given th
26d27 65 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c 0a 2a  e non-terminal.*
26d28 2a 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b  * look-ahead tok
26d29 65 6e 20 69 4c 6f 6f 6b 41 68 65 61 64 2e 0a 2a  en iLookAhead..*
26d2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 6f 6b  *.** If the look
26d2b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 69 73 20  -ahead token is 
26d2c 59 59 4e 4f 43 4f 44 45 2c 20 74 68 65 6e 20 63  YYNOCODE, then c
26d2d 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
26d2e 68 65 20 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20  he action is.** 
26d2f 69 6e 64 65 70 65 6e 64 65 6e 74 20 6f 66 20 74  independent of t
26d30 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 2e 20 20  he look-ahead.  
26d31 49 66 20 69 74 20 69 73 2c 20 72 65 74 75 72 6e  If it is, return
26d32 20 74 68 65 20 61 63 74 69 6f 6e 2c 20 6f 74 68   the action, oth
26d33 65 72 77 69 73 65 0a 2a 2a 20 72 65 74 75 72 6e  erwise.** return
26d34 20 59 59 5f 4e 4f 5f 41 43 54 49 4f 4e 2e 0a 2a   YY_NO_ACTION..*
26d35 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 79 79 5f  /.static int yy_
26d36 66 69 6e 64 5f 72 65 64 75 63 65 5f 61 63 74 69  find_reduce_acti
26d37 6f 6e 28 0a 20 20 69 6e 74 20 73 74 61 74 65 6e  on(.  int staten
26d38 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o,              
26d39 2f 2a 20 43 75 72 72 65 6e 74 20 73 74 61 74 65  /* Current state
26d3a 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 59 59 43   number */.  YYC
26d3b 4f 44 45 54 59 50 45 20 69 4c 6f 6f 6b 41 68 65  ODETYPE iLookAhe
26d3c 61 64 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f  ad     /* The lo
26d3d 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 2a  ok-ahead token *
26d3e 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 23 69  /.){.  int i;.#i
26d3f 66 64 65 66 20 59 59 45 52 52 4f 52 53 59 4d 42  fdef YYERRORSYMB
26d40 4f 4c 0a 20 20 69 66 28 20 73 74 61 74 65 6e 6f  OL.  if( stateno
26d41 3e 59 59 5f 52 45 44 55 43 45 5f 4d 41 58 20 29  >YY_REDUCE_MAX )
26d42 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 79 79 5f  {.    return yy_
26d43 64 65 66 61 75 6c 74 5b 73 74 61 74 65 6e 6f 5d  default[stateno]
26d44 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 61 73  ;.  }.#else.  as
26d45 73 65 72 74 28 20 73 74 61 74 65 6e 6f 3c 3d 59  sert( stateno<=Y
26d46 59 5f 52 45 44 55 43 45 5f 4d 41 58 20 29 3b 0a  Y_REDUCE_MAX );.
26d47 23 65 6e 64 69 66 0a 20 20 69 20 3d 20 79 79 5f  #endif.  i = yy_
26d48 72 65 64 75 63 65 5f 6f 66 73 74 5b 73 74 61 74  reduce_ofst[stat
26d49 65 6e 6f 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  eno];.  assert( 
26d4a 69 21 3d 59 59 5f 52 45 44 55 43 45 5f 55 53 45  i!=YY_REDUCE_USE
26d4b 5f 44 46 4c 54 20 29 3b 0a 20 20 61 73 73 65 72  _DFLT );.  asser
26d4c 74 28 20 69 4c 6f 6f 6b 41 68 65 61 64 21 3d 59  t( iLookAhead!=Y
26d4d 59 4e 4f 43 4f 44 45 20 29 3b 0a 20 20 69 20 2b  YNOCODE );.  i +
26d4e 3d 20 69 4c 6f 6f 6b 41 68 65 61 64 3b 0a 23 69  = iLookAhead;.#i
26d4f 66 64 65 66 20 59 59 45 52 52 4f 52 53 59 4d 42  fdef YYERRORSYMB
26d50 4f 4c 0a 20 20 69 66 28 20 69 3c 30 20 7c 7c 20  OL.  if( i<0 || 
26d51 69 3e 3d 59 59 5f 53 5a 5f 41 43 54 54 41 42 20  i>=YY_SZ_ACTTAB 
26d52 7c 7c 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b  || yy_lookahead[
26d53 69 5d 21 3d 69 4c 6f 6f 6b 41 68 65 61 64 20 29  i]!=iLookAhead )
26d54 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 79 79 5f  {.    return yy_
26d55 64 65 66 61 75 6c 74 5b 73 74 61 74 65 6e 6f 5d  default[stateno]
26d56 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 61 73  ;.  }.#else.  as
26d57 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
26d58 59 59 5f 53 5a 5f 41 43 54 54 41 42 20 29 3b 0a  YY_SZ_ACTTAB );.
26d59 20 20 61 73 73 65 72 74 28 20 79 79 5f 6c 6f 6f    assert( yy_loo
26d5a 6b 61 68 65 61 64 5b 69 5d 3d 3d 69 4c 6f 6f 6b  kahead[i]==iLook
26d5b 41 68 65 61 64 20 29 3b 0a 23 65 6e 64 69 66 0a  Ahead );.#endif.
26d5c 20 20 72 65 74 75 72 6e 20 79 79 5f 61 63 74 69    return yy_acti
26d5d 6f 6e 5b 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  on[i];.}../*.** 
26d5e 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f  The following ro
26d5f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
26d60 69 66 20 74 68 65 20 73 74 61 63 6b 20 6f 76 65  if the stack ove
26d61 72 66 6c 6f 77 73 2e 0a 2a 2f 0a 73 74 61 74 69  rflows..*/.stati
26d62 63 20 76 6f 69 64 20 79 79 53 74 61 63 6b 4f 76  c void yyStackOv
26d63 65 72 66 6c 6f 77 28 79 79 50 61 72 73 65 72 20  erflow(yyParser 
26d64 2a 79 79 70 50 61 72 73 65 72 2c 20 59 59 4d 49  *yypParser, YYMI
26d65 4e 4f 52 54 59 50 45 20 2a 79 79 70 4d 69 6e 6f  NORTYPE *yypMino
26d66 72 29 7b 0a 20 20 20 73 71 6c 69 74 65 33 50 61  r){.   sqlite3Pa
26d67 72 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a 20  rserARG_FETCH;. 
26d68 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69    yypParser->yyi
26d69 64 78 2d 2d 3b 0a 23 69 66 6e 64 65 66 20 4e 44  dx--;.#ifndef ND
26d6a 45 42 55 47 0a 20 20 20 69 66 28 20 79 79 54 72  EBUG.   if( yyTr
26d6b 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 20  aceFILE ){.     
26d6c 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46  fprintf(yyTraceF
26d6d 49 4c 45 2c 22 25 73 53 74 61 63 6b 20 4f 76 65  ILE,"%sStack Ove
26d6e 72 66 6c 6f 77 21 5c 6e 22 2c 79 79 54 72 61 63  rflow!\n",yyTrac
26d6f 65 50 72 6f 6d 70 74 29 3b 0a 20 20 20 7d 0a 23  ePrompt);.   }.#
26d70 65 6e 64 69 66 0a 20 20 20 77 68 69 6c 65 28 20  endif.   while( 
26d71 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
26d72 3e 3d 30 20 29 20 79 79 5f 70 6f 70 5f 70 61 72  >=0 ) yy_pop_par
26d73 73 65 72 5f 73 74 61 63 6b 28 79 79 70 50 61 72  ser_stack(yypPar
26d74 73 65 72 29 3b 0a 20 20 20 2f 2a 20 48 65 72 65  ser);.   /* Here
26d75 20 63 6f 64 65 20 69 73 20 69 6e 73 65 72 74 65   code is inserte
26d76 64 20 77 68 69 63 68 20 77 69 6c 6c 20 65 78 65  d which will exe
26d77 63 75 74 65 20 69 66 20 74 68 65 20 70 61 72 73  cute if the pars
26d78 65 72 0a 20 20 20 2a 2a 20 73 74 61 63 6b 20 65  er.   ** stack e
26d79 76 65 72 79 20 6f 76 65 72 66 6c 6f 77 73 20 2a  very overflows *
26d7a 2f 0a 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f  /..  sqlite3Erro
26d7b 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 70 61  rMsg(pParse, "pa
26d7c 72 73 65 72 20 73 74 61 63 6b 20 6f 76 65 72 66  rser stack overf
26d7d 6c 6f 77 22 29 3b 0a 20 20 70 50 61 72 73 65 2d  low");.  pParse-
26d7e 3e 70 61 72 73 65 45 72 72 6f 72 20 3d 20 31 3b  >parseError = 1;
26d7f 0a 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  .   sqlite3Parse
26d80 72 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53  rARG_STORE; /* S
26d81 75 70 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20  uppress warning 
26d82 61 62 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78  about unused %ex
26d83 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72  tra_argument var
26d84 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72   */.}../*.** Per
26d85 66 6f 72 6d 20 61 20 73 68 69 66 74 20 61 63 74  form a shift act
26d86 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
26d87 6f 69 64 20 79 79 5f 73 68 69 66 74 28 0a 20 20  oid yy_shift(.  
26d88 79 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72  yyParser *yypPar
26d89 73 65 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ser,          /*
26d8a 20 54 68 65 20 70 61 72 73 65 72 20 74 6f 20 62   The parser to b
26d8b 65 20 73 68 69 66 74 65 64 20 2a 2f 0a 20 20 69  e shifted */.  i
26d8c 6e 74 20 79 79 4e 65 77 53 74 61 74 65 2c 20 20  nt yyNewState,  
26d8d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26d8e 54 68 65 20 6e 65 77 20 73 74 61 74 65 20 74 6f  The new state to
26d8f 20 73 68 69 66 74 20 69 6e 20 2a 2f 0a 20 20 69   shift in */.  i
26d90 6e 74 20 79 79 4d 61 6a 6f 72 2c 20 20 20 20 20  nt yyMajor,     
26d91 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26d92 54 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20  The major token 
26d93 74 6f 20 73 68 69 66 74 20 69 6e 20 2a 2f 0a 20  to shift in */. 
26d94 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 2a 79 79   YYMINORTYPE *yy
26d95 70 4d 69 6e 6f 72 20 20 20 20 20 20 20 20 20 2f  pMinor         /
26d96 2a 20 50 6f 69 6e 74 65 72 20 6f 74 20 74 68 65  * Pointer ot the
26d97 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 20 74 6f 20   minor token to 
26d98 73 68 69 66 74 20 69 6e 20 2a 2f 0a 29 7b 0a 20  shift in */.){. 
26d99 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20 2a 79   yyStackEntry *y
26d9a 79 74 6f 73 3b 0a 20 20 79 79 70 50 61 72 73 65  ytos;.  yypParse
26d9b 72 2d 3e 79 79 69 64 78 2b 2b 3b 0a 23 69 66 20  r->yyidx++;.#if 
26d9c 59 59 53 54 41 43 4b 44 45 50 54 48 3e 30 20 0a  YYSTACKDEPTH>0 .
26d9d 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d    if( yypParser-
26d9e 3e 79 79 69 64 78 3e 3d 59 59 53 54 41 43 4b 44  >yyidx>=YYSTACKD
26d9f 45 50 54 48 20 29 7b 0a 20 20 20 20 79 79 53 74  EPTH ){.    yySt
26da0 61 63 6b 4f 76 65 72 66 6c 6f 77 28 79 79 70 50  ackOverflow(yypP
26da1 61 72 73 65 72 2c 20 79 79 70 4d 69 6e 6f 72 29  arser, yypMinor)
26da2 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
26da3 7d 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 79 79  }.#else.  if( yy
26da4 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d  pParser->yyidx>=
26da5 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74 6b  yypParser->yystk
26da6 73 7a 20 29 7b 0a 20 20 20 20 79 79 47 72 6f 77  sz ){.    yyGrow
26da7 53 74 61 63 6b 28 79 79 70 50 61 72 73 65 72 29  Stack(yypParser)
26da8 3b 0a 20 20 20 20 69 66 28 20 79 79 70 50 61 72  ;.    if( yypPar
26da9 73 65 72 2d 3e 79 79 69 64 78 3e 3d 79 79 70 50  ser->yyidx>=yypP
26daa 61 72 73 65 72 2d 3e 79 79 73 74 6b 73 7a 20 29  arser->yystksz )
26dab 7b 0a 20 20 20 20 20 20 79 79 53 74 61 63 6b 4f  {.      yyStackO
26dac 76 65 72 66 6c 6f 77 28 79 79 70 50 61 72 73 65  verflow(yypParse
26dad 72 2c 20 79 79 70 4d 69 6e 6f 72 29 3b 0a 20 20  r, yypMinor);.  
26dae 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
26daf 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79  }.  }.#endif.  y
26db0 79 74 6f 73 20 3d 20 26 79 79 70 50 61 72 73 65  ytos = &yypParse
26db1 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79 70 50 61  r->yystack[yypPa
26db2 72 73 65 72 2d 3e 79 79 69 64 78 5d 3b 0a 20 20  rser->yyidx];.  
26db3 79 79 74 6f 73 2d 3e 73 74 61 74 65 6e 6f 20 3d  yytos->stateno =
26db4 20 79 79 4e 65 77 53 74 61 74 65 3b 0a 20 20 79   yyNewState;.  y
26db5 79 74 6f 73 2d 3e 6d 61 6a 6f 72 20 3d 20 79 79  ytos->major = yy
26db6 4d 61 6a 6f 72 3b 0a 20 20 79 79 74 6f 73 2d 3e  Major;.  yytos->
26db7 6d 69 6e 6f 72 20 3d 20 2a 79 79 70 4d 69 6e 6f  minor = *yypMino
26db8 72 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  r;.#ifndef NDEBU
26db9 47 0a 20 20 69 66 28 20 79 79 54 72 61 63 65 46  G.  if( yyTraceF
26dba 49 4c 45 20 26 26 20 79 79 70 50 61 72 73 65 72  ILE && yypParser
26dbb 2d 3e 79 79 69 64 78 3e 30 20 29 7b 0a 20 20 20  ->yyidx>0 ){.   
26dbc 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 70 72 69   int i;.    fpri
26dbd 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
26dbe 22 25 73 53 68 69 66 74 20 25 64 5c 6e 22 2c 79  "%sShift %d\n",y
26dbf 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 79 79 4e  yTracePrompt,yyN
26dc0 65 77 53 74 61 74 65 29 3b 0a 20 20 20 20 66 70  ewState);.    fp
26dc1 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
26dc2 45 2c 22 25 73 53 74 61 63 6b 3a 22 2c 79 79 54  E,"%sStack:",yyT
26dc3 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20 20  racePrompt);.   
26dc4 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 79 79 70   for(i=1; i<=yyp
26dc5 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3b 20 69  Parser->yyidx; i
26dc6 2b 2b 29 0a 20 20 20 20 20 20 66 70 72 69 6e 74  ++).      fprint
26dc7 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 20  f(yyTraceFILE," 
26dc8 25 73 22 2c 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b  %s",yyTokenName[
26dc9 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61  yypParser->yysta
26dca 63 6b 5b 69 5d 2e 6d 61 6a 6f 72 5d 29 3b 0a 20  ck[i].major]);. 
26dcb 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61     fprintf(yyTra
26dcc 63 65 46 49 4c 45 2c 22 5c 6e 22 29 3b 0a 20 20  ceFILE,"\n");.  
26dcd 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 20 54  }.#endif.}../* T
26dce 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62  he following tab
26dcf 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f  le contains info
26dd0 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 76  rmation about ev
26dd1 65 72 79 20 72 75 6c 65 20 74 68 61 74 0a 2a 2a  ery rule that.**
26dd2 20 69 73 20 75 73 65 64 20 64 75 72 69 6e 67 20   is used during 
26dd3 74 68 65 20 72 65 64 75 63 65 2e 0a 2a 2f 0a 73  the reduce..*/.s
26dd4 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75  tatic const stru
26dd5 63 74 20 7b 0a 20 20 59 59 43 4f 44 45 54 59 50  ct {.  YYCODETYP
26dd6 45 20 6c 68 73 3b 20 20 20 20 20 20 20 20 20 2f  E lhs;         /
26dd7 2a 20 53 79 6d 62 6f 6c 20 6f 6e 20 74 68 65 20  * Symbol on the 
26dd8 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f  left-hand side o
26dd9 66 20 74 68 65 20 72 75 6c 65 20 2a 2f 0a 20 20  f the rule */.  
26dda 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6e 72  unsigned char nr
26ddb 68 73 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  hs;     /* Numbe
26ddc 72 20 6f 66 20 72 69 67 68 74 2d 68 61 6e 64 20  r of right-hand 
26ddd 73 69 64 65 20 73 79 6d 62 6f 6c 73 20 69 6e 20  side symbols in 
26dde 74 68 65 20 72 75 6c 65 20 2a 2f 0a 7d 20 79 79  the rule */.} yy
26ddf 52 75 6c 65 49 6e 66 6f 5b 5d 20 3d 20 7b 0a 20  RuleInfo[] = {. 
26de0 20 7b 20 31 33 39 2c 20 31 20 7d 2c 0a 20 20 7b   { 139, 1 },.  {
26de1 20 31 34 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   140, 2 },.  { 1
26de2 34 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 32  40, 1 },.  { 142
26de3 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 31 2c 20  , 1 },.  { 141, 
26de4 31 20 7d 2c 0a 20 20 7b 20 31 34 31 2c 20 33 20  1 },.  { 141, 3 
26de5 7d 2c 0a 20 20 7b 20 31 34 34 2c 20 30 20 7d 2c  },.  { 144, 0 },
26de6 0a 20 20 7b 20 31 34 34 2c 20 31 20 7d 2c 0a 20  .  { 144, 1 },. 
26de7 20 7b 20 31 34 34 2c 20 33 20 7d 2c 0a 20 20 7b   { 144, 3 },.  {
26de8 20 31 34 33 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   143, 3 },.  { 1
26de9 34 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34 36  46, 0 },.  { 146
26dea 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 36 2c 20  , 1 },.  { 146, 
26deb 32 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 30 20  2 },.  { 145, 0 
26dec 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 31 20 7d 2c  },.  { 145, 1 },
26ded 0a 20 20 7b 20 31 34 35 2c 20 31 20 7d 2c 0a 20  .  { 145, 1 },. 
26dee 20 7b 20 31 34 35 2c 20 31 20 7d 2c 0a 20 20 7b   { 145, 1 },.  {
26def 20 31 34 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   143, 2 },.  { 1
26df0 34 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 33  43, 2 },.  { 143
26df1 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20  , 2 },.  { 143, 
26df2 32 20 7d 2c 0a 20 20 7b 20 31 34 38 2c 20 36 20  2 },.  { 148, 6 
26df3 7d 2c 0a 20 20 7b 20 31 35 31 2c 20 30 20 7d 2c  },.  { 151, 0 },
26df4 0a 20 20 7b 20 31 35 31 2c 20 33 20 7d 2c 0a 20  .  { 151, 3 },. 
26df5 20 7b 20 31 35 30 2c 20 31 20 7d 2c 0a 20 20 7b   { 150, 1 },.  {
26df6 20 31 35 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   150, 0 },.  { 1
26df7 34 39 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 34 39  49, 4 },.  { 149
26df8 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 35 33 2c 20  , 2 },.  { 153, 
26df9 33 20 7d 2c 0a 20 20 7b 20 31 35 33 2c 20 31 20  3 },.  { 153, 1 
26dfa 7d 2c 0a 20 20 7b 20 31 35 36 2c 20 33 20 7d 2c  },.  { 156, 3 },
26dfb 0a 20 20 7b 20 31 35 37 2c 20 31 20 7d 2c 0a 20  .  { 157, 1 },. 
26dfc 20 7b 20 31 36 30 2c 20 31 20 7d 2c 0a 20 20 7b   { 160, 1 },.  {
26dfd 20 31 36 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   161, 1 },.  { 1
26dfe 34 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37  47, 1 },.  { 147
26dff 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 1 },.  { 147, 
26e00 31 20 7d 2c 0a 20 20 7b 20 31 35 38 2c 20 30 20  1 },.  { 158, 0 
26e01 7d 2c 0a 20 20 7b 20 31 35 38 2c 20 31 20 7d 2c  },.  { 158, 1 },
26e02 0a 20 20 7b 20 31 36 32 2c 20 31 20 7d 2c 0a 20  .  { 162, 1 },. 
26e03 20 7b 20 31 36 32 2c 20 34 20 7d 2c 0a 20 20 7b   { 162, 4 },.  {
26e04 20 31 36 32 2c 20 36 20 7d 2c 0a 20 20 7b 20 31   162, 6 },.  { 1
26e05 36 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 33  63, 1 },.  { 163
26e06 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 36 34 2c 20  , 2 },.  { 164, 
26e07 31 20 7d 2c 0a 20 20 7b 20 31 36 34 2c 20 31 20  1 },.  { 164, 1 
26e08 7d 2c 0a 20 20 7b 20 31 35 39 2c 20 32 20 7d 2c  },.  { 159, 2 },
26e09 0a 20 20 7b 20 31 35 39 2c 20 30 20 7d 2c 0a 20  .  { 159, 0 },. 
26e0a 20 7b 20 31 36 37 2c 20 33 20 7d 2c 0a 20 20 7b   { 167, 3 },.  {
26e0b 20 31 36 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   167, 1 },.  { 1
26e0c 36 38 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 36 38  68, 2 },.  { 168
26e0d 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 36 38 2c 20  , 4 },.  { 168, 
26e0e 33 20 7d 2c 0a 20 20 7b 20 31 36 38 2c 20 33 20  3 },.  { 168, 3 
26e0f 7d 2c 0a 20 20 7b 20 31 36 38 2c 20 32 20 7d 2c  },.  { 168, 2 },
26e10 0a 20 20 7b 20 31 36 38 2c 20 32 20 7d 2c 0a 20  .  { 168, 2 },. 
26e11 20 7b 20 31 36 38 2c 20 33 20 7d 2c 0a 20 20 7b   { 168, 3 },.  {
26e12 20 31 36 38 2c 20 35 20 7d 2c 0a 20 20 7b 20 31   168, 5 },.  { 1
26e13 36 38 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 36 38  68, 2 },.  { 168
26e14 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 36 38 2c 20  , 4 },.  { 168, 
26e15 34 20 7d 2c 0a 20 20 7b 20 31 36 38 2c 20 31 20  4 },.  { 168, 1 
26e16 7d 2c 0a 20 20 7b 20 31 36 38 2c 20 32 20 7d 2c  },.  { 168, 2 },
26e17 0a 20 20 7b 20 31 37 33 2c 20 30 20 7d 2c 0a 20  .  { 173, 0 },. 
26e18 20 7b 20 31 37 33 2c 20 31 20 7d 2c 0a 20 20 7b   { 173, 1 },.  {
26e19 20 31 37 35 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   175, 0 },.  { 1
26e1a 37 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 37  75, 2 },.  { 177
26e1b 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 37 2c 20  , 2 },.  { 177, 
26e1c 33 20 7d 2c 0a 20 20 7b 20 31 37 37 2c 20 33 20  3 },.  { 177, 3 
26e1d 7d 2c 0a 20 20 7b 20 31 37 37 2c 20 33 20 7d 2c  },.  { 177, 3 },
26e1e 0a 20 20 7b 20 31 37 38 2c 20 32 20 7d 2c 0a 20  .  { 178, 2 },. 
26e1f 20 7b 20 31 37 38 2c 20 32 20 7d 2c 0a 20 20 7b   { 178, 2 },.  {
26e20 20 31 37 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   178, 1 },.  { 1
26e21 37 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 36  78, 1 },.  { 176
26e22 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 36 2c 20  , 3 },.  { 176, 
26e23 32 20 7d 2c 0a 20 20 7b 20 31 37 39 2c 20 30 20  2 },.  { 179, 0 
26e24 7d 2c 0a 20 20 7b 20 31 37 39 2c 20 32 20 7d 2c  },.  { 179, 2 },
26e25 0a 20 20 7b 20 31 37 39 2c 20 32 20 7d 2c 0a 20  .  { 179, 2 },. 
26e26 20 7b 20 31 35 34 2c 20 30 20 7d 2c 0a 20 20 7b   { 154, 0 },.  {
26e27 20 31 35 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   154, 2 },.  { 1
26e28 38 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 38 30  80, 3 },.  { 180
26e29 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 30 2c 20  , 2 },.  { 180, 
26e2a 31 20 7d 2c 0a 20 20 7b 20 31 38 31 2c 20 32 20  1 },.  { 181, 2 
26e2b 7d 2c 0a 20 20 7b 20 31 38 31 2c 20 37 20 7d 2c  },.  { 181, 7 },
26e2c 0a 20 20 7b 20 31 38 31 2c 20 35 20 7d 2c 0a 20  .  { 181, 5 },. 
26e2d 20 7b 20 31 38 31 2c 20 35 20 7d 2c 0a 20 20 7b   { 181, 5 },.  {
26e2e 20 31 38 31 2c 20 31 30 20 7d 2c 0a 20 20 7b 20   181, 10 },.  { 
26e2f 31 38 33 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 38  183, 0 },.  { 18
26e30 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 31 2c  3, 1 },.  { 171,
26e31 20 30 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 33   0 },.  { 171, 3
26e32 20 7d 2c 0a 20 20 7b 20 31 38 34 2c 20 30 20 7d   },.  { 184, 0 }
26e33 2c 0a 20 20 7b 20 31 38 34 2c 20 32 20 7d 2c 0a  ,.  { 184, 2 },.
26e34 20 20 7b 20 31 38 35 2c 20 31 20 7d 2c 0a 20 20    { 185, 1 },.  
26e35 7b 20 31 38 35 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 185, 1 },.  { 
26e36 31 38 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  185, 1 },.  { 14
26e37 33 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 38 37 2c  3, 4 },.  { 187,
26e38 20 32 20 7d 2c 0a 20 20 7b 20 31 38 37 2c 20 30   2 },.  { 187, 0
26e39 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 38 20 7d   },.  { 143, 8 }
26e3a 2c 0a 20 20 7b 20 31 34 33 2c 20 34 20 7d 2c 0a  ,.  { 143, 4 },.
26e3b 20 20 7b 20 31 34 33 2c 20 31 20 7d 2c 0a 20 20    { 143, 1 },.  
26e3c 7b 20 31 35 35 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 155, 1 },.  { 
26e3d 31 35 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 39  155, 3 },.  { 19
26e3e 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 30 2c  0, 1 },.  { 190,
26e3f 20 32 20 7d 2c 0a 20 20 7b 20 31 39 30 2c 20 31   2 },.  { 190, 1
26e40 20 7d 2c 0a 20 20 7b 20 31 38 39 2c 20 39 20 7d   },.  { 189, 9 }
26e41 2c 0a 20 20 7b 20 31 39 31 2c 20 31 20 7d 2c 0a  ,.  { 191, 1 },.
26e42 20 20 7b 20 31 39 31 2c 20 31 20 7d 2c 0a 20 20    { 191, 1 },.  
26e43 7b 20 31 39 31 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 191, 0 },.  { 
26e44 31 39 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 39  199, 2 },.  { 19
26e45 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 32 2c  9, 0 },.  { 192,
26e46 20 33 20 7d 2c 0a 20 20 7b 20 31 39 32 2c 20 32   3 },.  { 192, 2
26e47 20 7d 2c 0a 20 20 7b 20 31 39 32 2c 20 34 20 7d   },.  { 192, 4 }
26e48 2c 0a 20 20 7b 20 32 30 30 2c 20 32 20 7d 2c 0a  ,.  { 200, 2 },.
26e49 20 20 7b 20 32 30 30 2c 20 31 20 7d 2c 0a 20 20    { 200, 1 },.  
26e4a 7b 20 32 30 30 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 200, 0 },.  { 
26e4b 31 39 33 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39  193, 0 },.  { 19
26e4c 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 32 2c  3, 2 },.  { 202,
26e4d 20 32 20 7d 2c 0a 20 20 7b 20 32 30 32 2c 20 30   2 },.  { 202, 0
26e4e 20 7d 2c 0a 20 20 7b 20 32 30 31 2c 20 36 20 7d   },.  { 201, 6 }
26e4f 2c 0a 20 20 7b 20 32 30 31 2c 20 37 20 7d 2c 0a  ,.  { 201, 7 },.
26e50 20 20 7b 20 32 30 36 2c 20 31 20 7d 2c 0a 20 20    { 206, 1 },.  
26e51 7b 20 32 30 36 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 206, 1 },.  { 
26e52 31 35 32 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35  152, 0 },.  { 15
26e53 32 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 38 2c  2, 2 },.  { 188,
26e54 20 32 20 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 31   2 },.  { 203, 1
26e55 20 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 32 20 7d   },.  { 203, 2 }
26e56 2c 0a 20 20 7b 20 32 30 33 2c 20 33 20 7d 2c 0a  ,.  { 203, 3 },.
26e57 20 20 7b 20 32 30 33 2c 20 34 20 7d 2c 0a 20 20    { 203, 4 },.  
26e58 7b 20 32 30 34 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 204, 2 },.  { 
26e59 32 30 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30  204, 0 },.  { 20
26e5a 35 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 30 35 2c  5, 4 },.  { 205,
26e5b 20 30 20 7d 2c 0a 20 20 7b 20 31 39 37 2c 20 30   0 },.  { 197, 0
26e5c 20 7d 2c 0a 20 20 7b 20 31 39 37 2c 20 33 20 7d   },.  { 197, 3 }
26e5d 2c 0a 20 20 7b 20 32 30 39 2c 20 34 20 7d 2c 0a  ,.  { 209, 4 },.
26e5e 20 20 7b 20 32 30 39 2c 20 32 20 7d 2c 0a 20 20    { 209, 2 },.  
26e5f 7b 20 32 31 30 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 210, 1 },.  { 
26e60 31 37 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37  172, 1 },.  { 17
26e61 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 32 2c  2, 1 },.  { 172,
26e62 20 30 20 7d 2c 0a 20 20 7b 20 31 39 35 2c 20 30   0 },.  { 195, 0
26e63 20 7d 2c 0a 20 20 7b 20 31 39 35 2c 20 33 20 7d   },.  { 195, 3 }
26e64 2c 0a 20 20 7b 20 31 39 36 2c 20 30 20 7d 2c 0a  ,.  { 196, 0 },.
26e65 20 20 7b 20 31 39 36 2c 20 32 20 7d 2c 0a 20 20    { 196, 2 },.  
26e66 7b 20 31 39 38 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 198, 0 },.  { 
26e67 31 39 38 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 39  198, 2 },.  { 19
26e68 38 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 39 38 2c  8, 4 },.  { 198,
26e69 20 34 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 34   4 },.  { 143, 4
26e6a 20 7d 2c 0a 20 20 7b 20 31 39 34 2c 20 30 20 7d   },.  { 194, 0 }
26e6b 2c 0a 20 20 7b 20 31 39 34 2c 20 32 20 7d 2c 0a  ,.  { 194, 2 },.
26e6c 20 20 7b 20 31 34 33 2c 20 36 20 7d 2c 0a 20 20    { 143, 6 },.  
26e6d 7b 20 32 31 32 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 212, 5 },.  { 
26e6e 32 31 32 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34  212, 3 },.  { 14
26e6f 33 2c 20 38 20 7d 2c 0a 20 20 7b 20 31 34 33 2c  3, 8 },.  { 143,
26e70 20 35 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 36   5 },.  { 143, 6
26e71 20 7d 2c 0a 20 20 7b 20 32 31 33 2c 20 32 20 7d   },.  { 213, 2 }
26e72 2c 0a 20 20 7b 20 32 31 33 2c 20 31 20 7d 2c 0a  ,.  { 213, 1 },.
26e73 20 20 7b 20 32 31 35 2c 20 33 20 7d 2c 0a 20 20    { 215, 3 },.  
26e74 7b 20 32 31 35 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 215, 1 },.  { 
26e75 32 31 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 31  214, 0 },.  { 21
26e76 34 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 30 38 2c  4, 3 },.  { 208,
26e77 20 33 20 7d 2c 0a 20 20 7b 20 32 30 38 2c 20 31   3 },.  { 208, 1
26e78 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 31 20 7d   },.  { 170, 1 }
26e79 2c 0a 20 20 7b 20 31 37 30 2c 20 33 20 7d 2c 0a  ,.  { 170, 3 },.
26e7a 20 20 7b 20 31 36 39 2c 20 31 20 7d 2c 0a 20 20    { 169, 1 },.  
26e7b 7b 20 31 37 30 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 170, 1 },.  { 
26e7c 31 37 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37  170, 1 },.  { 17
26e7d 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 30 2c  0, 3 },.  { 170,
26e7e 20 35 20 7d 2c 0a 20 20 7b 20 31 36 39 2c 20 31   5 },.  { 169, 1
26e7f 20 7d 2c 0a 20 20 7b 20 31 36 39 2c 20 31 20 7d   },.  { 169, 1 }
26e80 2c 0a 20 20 7b 20 31 37 30 2c 20 31 20 7d 2c 0a  ,.  { 170, 1 },.
26e81 20 20 7b 20 31 37 30 2c 20 31 20 7d 2c 0a 20 20    { 170, 1 },.  
26e82 7b 20 31 37 30 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 170, 3 },.  { 
26e83 31 37 30 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 37  170, 6 },.  { 17
26e84 30 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 37 30 2c  0, 5 },.  { 170,
26e85 20 34 20 7d 2c 0a 20 20 7b 20 31 36 39 2c 20 31   4 },.  { 169, 1
26e86 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 33 20 7d   },.  { 170, 3 }
26e87 2c 0a 20 20 7b 20 31 37 30 2c 20 33 20 7d 2c 0a  ,.  { 170, 3 },.
26e88 20 20 7b 20 31 37 30 2c 20 33 20 7d 2c 0a 20 20    { 170, 3 },.  
26e89 7b 20 31 37 30 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 170, 3 },.  { 
26e8a 31 37 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37  170, 3 },.  { 17
26e8b 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 30 2c  0, 3 },.  { 170,
26e8c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 33   3 },.  { 170, 3
26e8d 20 7d 2c 0a 20 20 7b 20 32 31 37 2c 20 31 20 7d   },.  { 217, 1 }
26e8e 2c 0a 20 20 7b 20 32 31 37 2c 20 32 20 7d 2c 0a  ,.  { 217, 2 },.
26e8f 20 20 7b 20 32 31 37 2c 20 31 20 7d 2c 0a 20 20    { 217, 1 },.  
26e90 7b 20 32 31 37 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 217, 2 },.  { 
26e91 32 31 38 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 31  218, 2 },.  { 21
26e92 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 37 30 2c  8, 0 },.  { 170,
26e93 20 34 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 32   4 },.  { 170, 2
26e94 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 33 20 7d   },.  { 170, 3 }
26e95 2c 0a 20 20 7b 20 31 37 30 2c 20 33 20 7d 2c 0a  ,.  { 170, 3 },.
26e96 20 20 7b 20 31 37 30 2c 20 34 20 7d 2c 0a 20 20    { 170, 4 },.  
26e97 7b 20 31 37 30 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 170, 2 },.  { 
26e98 31 37 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37  170, 2 },.  { 17
26e99 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 30 2c  0, 2 },.  { 170,
26e9a 20 32 20 7d 2c 0a 20 20 7b 20 32 31 39 2c 20 31   2 },.  { 219, 1
26e9b 20 7d 2c 0a 20 20 7b 20 32 31 39 2c 20 32 20 7d   },.  { 219, 2 }
26e9c 2c 0a 20 20 7b 20 31 37 30 2c 20 35 20 7d 2c 0a  ,.  { 170, 5 },.
26e9d 20 20 7b 20 32 32 30 2c 20 31 20 7d 2c 0a 20 20    { 220, 1 },.  
26e9e 7b 20 32 32 30 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 220, 2 },.  { 
26e9f 31 37 30 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 37  170, 5 },.  { 17
26ea0 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 30 2c  0, 3 },.  { 170,
26ea1 20 35 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 34   5 },.  { 170, 4
26ea2 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 34 20 7d   },.  { 170, 4 }
26ea3 2c 0a 20 20 7b 20 31 37 30 2c 20 35 20 7d 2c 0a  ,.  { 170, 5 },.
26ea4 20 20 7b 20 32 32 32 2c 20 35 20 7d 2c 0a 20 20    { 222, 5 },.  
26ea5 7b 20 32 32 32 2c 20 34 20 7d 2c 0a 20 20 7b 20  { 222, 4 },.  { 
26ea6 32 32 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 32  223, 2 },.  { 22
26ea7 33 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 32 31 2c  3, 0 },.  { 221,
26ea8 20 31 20 7d 2c 0a 20 20 7b 20 32 32 31 2c 20 30   1 },.  { 221, 0
26ea9 20 7d 2c 0a 20 20 7b 20 32 31 36 2c 20 31 20 7d   },.  { 216, 1 }
26eaa 2c 0a 20 20 7b 20 32 31 36 2c 20 30 20 7d 2c 0a  ,.  { 216, 0 },.
26eab 20 20 7b 20 32 31 31 2c 20 33 20 7d 2c 0a 20 20    { 211, 3 },.  
26eac 7b 20 32 31 31 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 211, 1 },.  { 
26ead 31 34 33 2c 20 31 31 20 7d 2c 0a 20 20 7b 20 32  143, 11 },.  { 2
26eae 32 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 34  24, 1 },.  { 224
26eaf 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20  , 0 },.  { 174, 
26eb0 30 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 33 20  0 },.  { 174, 3 
26eb1 7d 2c 0a 20 20 7b 20 31 38 32 2c 20 35 20 7d 2c  },.  { 182, 5 },
26eb2 0a 20 20 7b 20 31 38 32 2c 20 33 20 7d 2c 0a 20  .  { 182, 3 },. 
26eb3 20 7b 20 32 32 35 2c 20 31 20 7d 2c 0a 20 20 7b   { 225, 1 },.  {
26eb4 20 32 32 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   226, 0 },.  { 2
26eb5 32 36 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 33  26, 2 },.  { 143
26eb6 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20  , 4 },.  { 143, 
26eb7 31 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 32 20  1 },.  { 143, 2 
26eb8 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 35 20 7d 2c  },.  { 143, 5 },
26eb9 0a 20 20 7b 20 31 34 33 2c 20 35 20 7d 2c 0a 20  .  { 143, 5 },. 
26eba 20 7b 20 31 34 33 2c 20 35 20 7d 2c 0a 20 20 7b   { 143, 5 },.  {
26ebb 20 31 34 33 2c 20 35 20 7d 2c 0a 20 20 7b 20 31   143, 5 },.  { 1
26ebc 34 33 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 34 33  43, 6 },.  { 143
26ebd 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 32 37 2c 20  , 3 },.  { 227, 
26ebe 31 20 7d 2c 0a 20 20 7b 20 32 32 37 2c 20 31 20  1 },.  { 227, 1 
26ebf 7d 2c 0a 20 20 7b 20 31 36 35 2c 20 32 20 7d 2c  },.  { 165, 2 },
26ec0 0a 20 20 7b 20 31 36 36 2c 20 32 20 7d 2c 0a 20  .  { 166, 2 },. 
26ec1 20 7b 20 32 32 39 2c 20 31 20 7d 2c 0a 20 20 7b   { 229, 1 },.  {
26ec2 20 32 32 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   228, 1 },.  { 2
26ec3 32 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34 33  28, 0 },.  { 143
26ec4 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 33 30 2c 20  , 5 },.  { 230, 
26ec5 31 31 20 7d 2c 0a 20 20 7b 20 32 33 32 2c 20 31  11 },.  { 232, 1
26ec6 20 7d 2c 0a 20 20 7b 20 32 33 32 2c 20 31 20 7d   },.  { 232, 1 }
26ec7 2c 0a 20 20 7b 20 32 33 32 2c 20 32 20 7d 2c 0a  ,.  { 232, 2 },.
26ec8 20 20 7b 20 32 33 32 2c 20 30 20 7d 2c 0a 20 20    { 232, 0 },.  
26ec9 7b 20 32 33 33 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 233, 1 },.  { 
26eca 32 33 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33  233, 1 },.  { 23
26ecb 33 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 33 34 2c  3, 3 },.  { 234,
26ecc 20 30 20 7d 2c 0a 20 20 7b 20 32 33 34 2c 20 33   0 },.  { 234, 3
26ecd 20 7d 2c 0a 20 20 7b 20 32 33 35 2c 20 30 20 7d   },.  { 235, 0 }
26ece 2c 0a 20 20 7b 20 32 33 35 2c 20 32 20 7d 2c 0a  ,.  { 235, 2 },.
26ecf 20 20 7b 20 32 33 31 2c 20 33 20 7d 2c 0a 20 20    { 231, 3 },.  
26ed0 7b 20 32 33 31 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 231, 0 },.  { 
26ed1 32 33 36 2c 20 36 20 7d 2c 0a 20 20 7b 20 32 33  236, 6 },.  { 23
26ed2 36 2c 20 38 20 7d 2c 0a 20 20 7b 20 32 33 36 2c  6, 8 },.  { 236,
26ed3 20 35 20 7d 2c 0a 20 20 7b 20 32 33 36 2c 20 34   5 },.  { 236, 4
26ed4 20 7d 2c 0a 20 20 7b 20 32 33 36 2c 20 31 20 7d   },.  { 236, 1 }
26ed5 2c 0a 20 20 7b 20 31 37 30 2c 20 34 20 7d 2c 0a  ,.  { 170, 4 },.
26ed6 20 20 7b 20 31 37 30 2c 20 36 20 7d 2c 0a 20 20    { 170, 6 },.  
26ed7 7b 20 31 38 36 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 186, 1 },.  { 
26ed8 31 38 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 38  186, 1 },.  { 18
26ed9 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 33 2c  6, 1 },.  { 143,
26eda 20 34 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 36   4 },.  { 143, 6
26edb 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 33 20 7d   },.  { 143, 3 }
26edc 2c 0a 20 20 7b 20 32 33 38 2c 20 30 20 7d 2c 0a  ,.  { 238, 0 },.
26edd 20 20 7b 20 32 33 38 2c 20 32 20 7d 2c 0a 20 20    { 238, 2 },.  
26ede 7b 20 32 33 37 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 237, 1 },.  { 
26edf 32 33 37 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34  237, 0 },.  { 14
26ee0 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 33 2c  3, 1 },.  { 143,
26ee1 20 33 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 31   3 },.  { 143, 1
26ee2 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 33 20 7d   },.  { 143, 3 }
26ee3 2c 0a 20 20 7b 20 31 34 33 2c 20 36 20 7d 2c 0a  ,.  { 143, 6 },.
26ee4 20 20 7b 20 31 34 33 2c 20 36 20 7d 2c 0a 20 20    { 143, 6 },.  
26ee5 7b 20 32 33 39 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 239, 1 },.  { 
26ee6 32 34 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 34  240, 0 },.  { 24
26ee7 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 33 2c  0, 1 },.  { 143,
26ee8 20 31 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 34   1 },.  { 143, 4
26ee9 20 7d 2c 0a 20 20 7b 20 32 34 31 2c 20 37 20 7d   },.  { 241, 7 }
26eea 2c 0a 20 20 7b 20 32 34 32 2c 20 31 20 7d 2c 0a  ,.  { 242, 1 },.
26eeb 20 20 7b 20 32 34 32 2c 20 33 20 7d 2c 0a 20 20    { 242, 3 },.  
26eec 7b 20 32 34 33 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 243, 0 },.  { 
26eed 32 34 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 34  243, 2 },.  { 24
26eee 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 34 34 2c  4, 1 },.  { 244,
26eef 20 33 20 7d 2c 0a 20 20 7b 20 32 34 35 2c 20 31   3 },.  { 245, 1
26ef0 20 7d 2c 0a 20 20 7b 20 32 34 36 2c 20 30 20 7d   },.  { 246, 0 }
26ef1 2c 0a 20 20 7b 20 32 34 36 2c 20 32 20 7d 2c 0a  ,.  { 246, 2 },.
26ef2 7d 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  };..static void 
26ef3 79 79 5f 61 63 63 65 70 74 28 79 79 50 61 72 73  yy_accept(yyPars
26ef4 65 72 2a 29 3b 20 20 2f 2a 20 46 6f 72 77 61 72  er*);  /* Forwar
26ef5 64 20 44 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f  d Declaration */
26ef6 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20  ../*.** Perform 
26ef7 61 20 72 65 64 75 63 65 20 61 63 74 69 6f 6e 20  a reduce action 
26ef8 61 6e 64 20 74 68 65 20 73 68 69 66 74 20 74 68  and the shift th
26ef9 61 74 20 6d 75 73 74 20 69 6d 6d 65 64 69 61 74  at must immediat
26efa 65 6c 79 0a 2a 2a 20 66 6f 6c 6c 6f 77 20 74 68  ely.** follow th
26efb 65 20 72 65 64 75 63 65 2e 0a 2a 2f 0a 73 74 61  e reduce..*/.sta
26efc 74 69 63 20 76 6f 69 64 20 79 79 5f 72 65 64 75  tic void yy_redu
26efd 63 65 28 0a 20 20 79 79 50 61 72 73 65 72 20 2a  ce(.  yyParser *
26efe 79 79 70 50 61 72 73 65 72 2c 20 20 20 20 20 20  yypParser,      
26eff 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
26f00 20 2a 2f 0a 20 20 69 6e 74 20 79 79 72 75 6c 65   */.  int yyrule
26f01 6e 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  no              
26f02 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
26f03 74 68 65 20 72 75 6c 65 20 62 79 20 77 68 69 63  the rule by whic
26f04 68 20 74 6f 20 72 65 64 75 63 65 20 2a 2f 0a 29  h to reduce */.)
26f05 7b 0a 20 20 69 6e 74 20 79 79 67 6f 74 6f 3b 20  {.  int yygoto; 
26f06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26f07 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20      /* The next 
26f08 73 74 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 79  state */.  int y
26f09 79 61 63 74 3b 20 20 20 20 20 20 20 20 20 20 20  yact;           
26f0a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
26f0b 65 20 6e 65 78 74 20 61 63 74 69 6f 6e 20 2a 2f  e next action */
26f0c 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 79  .  YYMINORTYPE y
26f0d 79 67 6f 74 6f 6d 69 6e 6f 72 3b 20 20 20 20 20  ygotominor;     
26f0e 20 20 20 2f 2a 20 54 68 65 20 4c 48 53 20 6f 66     /* The LHS of
26f0f 20 74 68 65 20 72 75 6c 65 20 72 65 64 75 63 65   the rule reduce
26f10 64 20 2a 2f 0a 20 20 79 79 53 74 61 63 6b 45 6e  d */.  yyStackEn
26f11 74 72 79 20 2a 79 79 6d 73 70 3b 20 20 20 20 20  try *yymsp;     
26f12 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f         /* The to
26f13 70 20 6f 66 20 74 68 65 20 70 61 72 73 65 72 27  p of the parser'
26f14 73 20 73 74 61 63 6b 20 2a 2f 0a 20 20 69 6e 74  s stack */.  int
26f15 20 79 79 73 69 7a 65 3b 20 20 20 20 20 20 20 20   yysize;        
26f16 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26f17 41 6d 6f 75 6e 74 20 74 6f 20 70 6f 70 20 74 68  Amount to pop th
26f18 65 20 73 74 61 63 6b 20 2a 2f 0a 20 20 73 71 6c  e stack */.  sql
26f19 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 46 45  ite3ParserARG_FE
26f1a 54 43 48 3b 0a 20 20 79 79 6d 73 70 20 3d 20 26  TCH;.  yymsp = &
26f1b 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61  yypParser->yysta
26f1c 63 6b 5b 79 79 70 50 61 72 73 65 72 2d 3e 79 79  ck[yypParser->yy
26f1d 69 64 78 5d 3b 0a 23 69 66 6e 64 65 66 20 4e 44  idx];.#ifndef ND
26f1e 45 42 55 47 0a 20 20 69 66 28 20 79 79 54 72 61  EBUG.  if( yyTra
26f1f 63 65 46 49 4c 45 20 26 26 20 79 79 72 75 6c 65  ceFILE && yyrule
26f20 6e 6f 3e 3d 30 20 0a 20 20 20 20 20 20 20 20 26  no>=0 .        &
26f21 26 20 79 79 72 75 6c 65 6e 6f 3c 28 69 6e 74 29  & yyruleno<(int)
26f22 28 73 69 7a 65 6f 66 28 79 79 52 75 6c 65 4e 61  (sizeof(yyRuleNa
26f23 6d 65 29 2f 73 69 7a 65 6f 66 28 79 79 52 75 6c  me)/sizeof(yyRul
26f24 65 4e 61 6d 65 5b 30 5d 29 29 20 29 7b 0a 20 20  eName[0])) ){.  
26f25 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63    fprintf(yyTrac
26f26 65 46 49 4c 45 2c 20 22 25 73 52 65 64 75 63 65  eFILE, "%sReduce
26f27 20 5b 25 73 5d 2e 5c 6e 22 2c 20 79 79 54 72 61   [%s].\n", yyTra
26f28 63 65 50 72 6f 6d 70 74 2c 0a 20 20 20 20 20 20  cePrompt,.      
26f29 79 79 52 75 6c 65 4e 61 6d 65 5b 79 79 72 75 6c  yyRuleName[yyrul
26f2a 65 6e 6f 5d 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  eno]);.  }.#endi
26f2b 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a  f /* NDEBUG */..
26f2c 20 20 2f 2a 20 53 69 6c 65 6e 63 65 20 63 6f 6d    /* Silence com
26f2d 70 6c 61 69 6e 74 73 20 66 72 6f 6d 20 70 75 72  plaints from pur
26f2e 69 66 79 20 61 62 6f 75 74 20 79 79 67 6f 74 6f  ify about yygoto
26f2f 6d 69 6e 6f 72 20 62 65 69 6e 67 20 75 6e 69 6e  minor being unin
26f30 69 74 69 61 6c 69 7a 65 64 0a 20 20 2a 2a 20 69  itialized.  ** i
26f31 6e 20 73 6f 6d 65 20 63 61 73 65 73 20 77 68 65  n some cases whe
26f32 6e 20 69 74 20 69 73 20 63 6f 70 69 65 64 20 69  n it is copied i
26f33 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 20 61 66  nto the stack af
26f34 74 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ter the followin
26f35 67 0a 20 20 2a 2a 20 73 77 69 74 63 68 2e 20 20  g.  ** switch.  
26f36 79 79 67 6f 74 6f 6d 69 6e 6f 72 20 69 73 20 75  yygotominor is u
26f37 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 77 68 65  ninitialized whe
26f38 6e 20 61 20 72 75 6c 65 20 72 65 64 75 63 65 73  n a rule reduces
26f39 20 74 68 61 74 20 64 6f 65 73 0a 20 20 2a 2a 20   that does.  ** 
26f3a 6e 6f 74 20 73 65 74 20 74 68 65 20 76 61 6c 75  not set the valu
26f3b 65 20 6f 66 20 69 74 73 20 6c 65 66 74 2d 68 61  e of its left-ha
26f3c 6e 64 20 73 69 64 65 20 6e 6f 6e 74 65 72 6d 69  nd side nontermi
26f3d 6e 61 6c 2e 20 20 4c 65 61 76 69 6e 67 20 74 68  nal.  Leaving th
26f3e 65 0a 20 20 2a 2a 20 76 61 6c 75 65 20 6f 66 20  e.  ** value of 
26f3f 74 68 65 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20  the nonterminal 
26f40 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 73  uninitialized is
26f41 20 75 74 74 65 72 6c 79 20 68 61 72 6d 6c 65 73   utterly harmles
26f42 73 20 61 73 20 6c 6f 6e 67 0a 20 20 2a 2a 20 61  s as long.  ** a
26f43 73 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 6e  s the value is n
26f44 65 76 65 72 20 75 73 65 64 2e 20 20 53 6f 20 72  ever used.  So r
26f45 65 61 6c 6c 79 20 74 68 65 20 6f 6e 6c 79 20 74  eally the only t
26f46 68 69 6e 67 20 74 68 69 73 20 63 6f 64 65 0a 20  hing this code. 
26f47 20 2a 2a 20 61 63 63 6f 6d 70 6c 69 73 68 65 73   ** accomplishes
26f48 20 69 73 20 74 6f 20 71 75 69 65 74 65 6e 20 70   is to quieten p
26f49 75 72 69 66 79 2e 20 20 0a 20 20 2a 2a 0a 20 20  urify.  .  **.  
26f4a 2a 2a 20 32 30 30 37 2d 30 31 2d 31 36 3a 20 20  ** 2007-01-16:  
26f4b 54 68 65 20 77 69 72 65 73 68 61 72 6b 20 70 72  The wireshark pr
26f4c 6f 6a 65 63 74 20 28 77 77 77 2e 77 69 72 65 73  oject (www.wires
26f4d 68 61 72 6b 2e 6f 72 67 29 20 72 65 70 6f 72 74  hark.org) report
26f4e 73 20 74 68 61 74 0a 20 20 2a 2a 20 77 69 74 68  s that.  ** with
26f4f 6f 75 74 20 74 68 69 73 20 63 6f 64 65 2c 20 74  out this code, t
26f50 68 65 69 72 20 70 61 72 73 65 72 20 73 65 67 66  heir parser segf
26f51 61 75 6c 74 73 2e 20 20 49 27 6d 20 6e 6f 74 20  aults.  I'm not 
26f52 73 75 72 65 20 77 68 61 74 20 74 68 65 72 65 0a  sure what there.
26f53 20 20 2a 2a 20 70 61 72 73 65 72 20 69 73 20 64    ** parser is d
26f54 6f 69 6e 67 20 74 6f 20 6d 61 6b 65 20 74 68 69  oing to make thi
26f55 73 20 68 61 70 70 65 6e 2e 20 20 54 68 69 73 20  s happen.  This 
26f56 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 62 75  is the second bu
26f57 67 20 72 65 70 6f 72 74 0a 20 20 2a 2a 20 66 72  g report.  ** fr
26f58 6f 6d 20 77 69 72 65 73 68 61 72 6b 20 74 68 69  om wireshark thi
26f59 73 20 77 65 65 6b 2e 20 20 43 6c 65 61 72 6c 79  s week.  Clearly
26f5a 20 74 68 65 79 20 61 72 65 20 73 74 72 65 73 73   they are stress
26f5b 69 6e 67 20 4c 65 6d 6f 6e 20 69 6e 20 77 61 79  ing Lemon in way
26f5c 73 0a 20 20 2a 2a 20 74 68 61 74 20 69 74 20 68  s.  ** that it h
26f5d 61 73 20 6e 6f 74 20 62 65 65 6e 20 70 72 65 76  as not been prev
26f5e 69 6f 75 73 6c 79 20 73 74 72 65 73 73 65 64 2e  iously stressed.
26f5f 2e 2e 20 20 28 53 51 4c 69 74 65 20 74 69 63 6b  ..  (SQLite tick
26f60 65 74 20 23 32 31 37 32 29 0a 20 20 2a 2f 0a 20  et #2172).  */. 
26f61 20 2f 2a 6d 65 6d 73 65 74 28 26 79 79 67 6f 74   /*memset(&yygot
26f62 6f 6d 69 6e 6f 72 2c 20 30 2c 20 73 69 7a 65 6f  ominor, 0, sizeo
26f63 66 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 29 29 3b  f(yygotominor));
26f64 2a 2f 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  */.  yygotominor
26f65 20 3d 20 79 79 7a 65 72 6f 6d 69 6e 6f 72 3b 0a   = yyzerominor;.
26f66 0a 0a 20 20 73 77 69 74 63 68 28 20 79 79 72 75  ..  switch( yyru
26f67 6c 65 6e 6f 20 29 7b 0a 20 20 2f 2a 20 42 65 67  leno ){.  /* Beg
26f68 69 6e 6e 69 6e 67 20 68 65 72 65 20 61 72 65 20  inning here are 
26f69 74 68 65 20 72 65 64 75 63 74 69 6f 6e 20 63 61  the reduction ca
26f6a 73 65 73 2e 20 20 41 20 74 79 70 69 63 61 6c 20  ses.  A typical 
26f6b 65 78 61 6d 70 6c 65 0a 20 20 2a 2a 20 66 6f 6c  example.  ** fol
26f6c 6c 6f 77 73 3a 0a 20 20 2a 2a 20 20 20 63 61 73  lows:.  **   cas
26f6d 65 20 30 3a 0a 20 20 2a 2a 20 20 23 6c 69 6e 65  e 0:.  **  #line
26f6e 20 3c 6c 69 6e 65 6e 6f 3e 20 3c 67 72 61 6d 6d   <lineno> <gramm
26f6f 61 72 66 69 6c 65 3e 0a 20 20 2a 2a 20 20 20 20  arfile>.  **    
26f70 20 7b 20 2e 2e 2e 20 7d 20 20 20 20 20 20 20 20   { ... }        
26f71 20 20 20 2f 2f 20 55 73 65 72 20 73 75 70 70 6c     // User suppl
26f72 69 65 64 20 63 6f 64 65 0a 20 20 2a 2a 20 20 23  ied code.  **  #
26f73 6c 69 6e 65 20 3c 6c 69 6e 65 6e 6f 3e 20 3c 74  line <lineno> <t
26f74 68 69 73 66 69 6c 65 3e 0a 20 20 2a 2a 20 20 20  hisfile>.  **   
26f75 20 20 62 72 65 61 6b 3b 0a 20 20 2a 2f 0a 20 20    break;.  */.  
26f76 20 20 20 20 63 61 73 65 20 30 3a 20 2f 2a 20 69      case 0: /* i
26f77 6e 70 75 74 20 3a 3a 3d 20 63 6d 64 6c 69 73 74  nput ::= cmdlist
26f78 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
26f79 3a 20 2f 2a 20 63 6d 64 6c 69 73 74 20 3a 3a 3d  : /* cmdlist ::=
26f7a 20 63 6d 64 6c 69 73 74 20 65 63 6d 64 20 2a 2f   cmdlist ecmd */
26f7b 0a 20 20 20 20 20 20 63 61 73 65 20 32 3a 20 2f  .      case 2: /
26f7c 2a 20 63 6d 64 6c 69 73 74 20 3a 3a 3d 20 65 63  * cmdlist ::= ec
26f7d 6d 64 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  md */.      case
26f7e 20 34 3a 20 2f 2a 20 65 63 6d 64 20 3a 3a 3d 20   4: /* ecmd ::= 
26f7f 53 45 4d 49 20 2a 2f 0a 20 20 20 20 20 20 63 61  SEMI */.      ca
26f80 73 65 20 35 3a 20 2f 2a 20 65 63 6d 64 20 3a 3a  se 5: /* ecmd ::
26f81 3d 20 65 78 70 6c 61 69 6e 20 63 6d 64 78 20 53  = explain cmdx S
26f82 45 4d 49 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  EMI */.      cas
26f83 65 20 31 30 3a 20 2f 2a 20 74 72 61 6e 73 5f 6f  e 10: /* trans_o
26f84 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20  pt ::= */.      
26f85 63 61 73 65 20 31 31 3a 20 2f 2a 20 74 72 61 6e  case 11: /* tran
26f86 73 5f 6f 70 74 20 3a 3a 3d 20 54 52 41 4e 53 41  s_opt ::= TRANSA
26f87 43 54 49 4f 4e 20 2a 2f 0a 20 20 20 20 20 20 63  CTION */.      c
26f88 61 73 65 20 31 32 3a 20 2f 2a 20 74 72 61 6e 73  ase 12: /* trans
26f89 5f 6f 70 74 20 3a 3a 3d 20 54 52 41 4e 53 41 43  _opt ::= TRANSAC
26f8a 54 49 4f 4e 20 6e 6d 20 2a 2f 0a 20 20 20 20 20  TION nm */.     
26f8b 20 63 61 73 65 20 32 30 3a 20 2f 2a 20 63 6d 64   case 20: /* cmd
26f8c 20 3a 3a 3d 20 63 72 65 61 74 65 5f 74 61 62 6c   ::= create_tabl
26f8d 65 20 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61  e create_table_a
26f8e 72 67 73 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  rgs */.      cas
26f8f 65 20 32 38 3a 20 2f 2a 20 63 6f 6c 75 6d 6e 6c  e 28: /* columnl
26f90 69 73 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 6c 69  ist ::= columnli
26f91 73 74 20 43 4f 4d 4d 41 20 63 6f 6c 75 6d 6e 20  st COMMA column 
26f92 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 39  */.      case 29
26f93 3a 20 2f 2a 20 63 6f 6c 75 6d 6e 6c 69 73 74 20  : /* columnlist 
26f94 3a 3a 3d 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20  ::= column */.  
26f95 20 20 20 20 63 61 73 65 20 33 37 3a 20 2f 2a 20      case 37: /* 
26f96 74 79 70 65 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20  type ::= */.    
26f97 20 20 63 61 73 65 20 34 34 3a 20 2f 2a 20 73 69    case 44: /* si
26f98 67 6e 65 64 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75  gned ::= plus_nu
26f99 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  m */.      case 
26f9a 34 35 3a 20 2f 2a 20 73 69 67 6e 65 64 20 3a 3a  45: /* signed ::
26f9b 3d 20 6d 69 6e 75 73 5f 6e 75 6d 20 2a 2f 0a 20  = minus_num */. 
26f9c 20 20 20 20 20 63 61 73 65 20 34 36 3a 20 2f 2a       case 46: /*
26f9d 20 63 61 72 67 6c 69 73 74 20 3a 3a 3d 20 63 61   carglist ::= ca
26f9e 72 67 6c 69 73 74 20 63 61 72 67 20 2a 2f 0a 20  rglist carg */. 
26f9f 20 20 20 20 20 63 61 73 65 20 34 37 3a 20 2f 2a       case 47: /*
26fa0 20 63 61 72 67 6c 69 73 74 20 3a 3a 3d 20 2a 2f   carglist ::= */
26fa1 0a 20 20 20 20 20 20 63 61 73 65 20 34 38 3a 20  .      case 48: 
26fa2 2f 2a 20 63 61 72 67 20 3a 3a 3d 20 43 4f 4e 53  /* carg ::= CONS
26fa3 54 52 41 49 4e 54 20 6e 6d 20 63 63 6f 6e 73 20  TRAINT nm ccons 
26fa4 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 34 39  */.      case 49
26fa5 3a 20 2f 2a 20 63 61 72 67 20 3a 3a 3d 20 63 63  : /* carg ::= cc
26fa6 6f 6e 73 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ons */.      cas
26fa7 65 20 35 35 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a  e 55: /* ccons :
26fa8 3a 3d 20 4e 55 4c 4c 20 6f 6e 63 6f 6e 66 20 2a  := NULL onconf *
26fa9 2f 0a 20 20 20 20 20 20 63 61 73 65 20 38 32 3a  /.      case 82:
26faa 20 2f 2a 20 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d   /* conslist ::=
26fab 20 63 6f 6e 73 6c 69 73 74 20 43 4f 4d 4d 41 20   conslist COMMA 
26fac 74 63 6f 6e 73 20 2a 2f 0a 20 20 20 20 20 20 63  tcons */.      c
26fad 61 73 65 20 38 33 3a 20 2f 2a 20 63 6f 6e 73 6c  ase 83: /* consl
26fae 69 73 74 20 3a 3a 3d 20 63 6f 6e 73 6c 69 73 74  ist ::= conslist
26faf 20 74 63 6f 6e 73 20 2a 2f 0a 20 20 20 20 20 20   tcons */.      
26fb0 63 61 73 65 20 38 34 3a 20 2f 2a 20 63 6f 6e 73  case 84: /* cons
26fb1 6c 69 73 74 20 3a 3a 3d 20 74 63 6f 6e 73 20 2a  list ::= tcons *
26fb2 2f 0a 20 20 20 20 20 20 63 61 73 65 20 38 35 3a  /.      case 85:
26fb3 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20 43 4f   /* tcons ::= CO
26fb4 4e 53 54 52 41 49 4e 54 20 6e 6d 20 2a 2f 0a 20  NSTRAINT nm */. 
26fb5 20 20 20 20 20 63 61 73 65 20 32 35 38 3a 20 2f       case 258: /
26fb6 2a 20 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d 20 50  * plus_opt ::= P
26fb7 4c 55 53 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  LUS */.      cas
26fb8 65 20 32 35 39 3a 20 2f 2a 20 70 6c 75 73 5f 6f  e 259: /* plus_o
26fb9 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20  pt ::= */.      
26fba 63 61 73 65 20 32 36 39 3a 20 2f 2a 20 66 6f 72  case 269: /* for
26fbb 65 61 63 68 5f 63 6c 61 75 73 65 20 3a 3a 3d 20  each_clause ::= 
26fbc 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 37  */.      case 27
26fbd 30 3a 20 2f 2a 20 66 6f 72 65 61 63 68 5f 63 6c  0: /* foreach_cl
26fbe 61 75 73 65 20 3a 3a 3d 20 46 4f 52 20 45 41 43  ause ::= FOR EAC
26fbf 48 20 52 4f 57 20 2a 2f 0a 20 20 20 20 20 20 63  H ROW */.      c
26fc0 61 73 65 20 32 39 30 3a 20 2f 2a 20 64 61 74 61  ase 290: /* data
26fc1 62 61 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 20  base_kw_opt ::= 
26fc2 44 41 54 41 42 41 53 45 20 2a 2f 0a 20 20 20 20  DATABASE */.    
26fc3 20 20 63 61 73 65 20 32 39 31 3a 20 2f 2a 20 64    case 291: /* d
26fc4 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20 3a  atabase_kw_opt :
26fc5 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  := */.      case
26fc6 20 32 39 39 3a 20 2f 2a 20 6b 77 63 6f 6c 75 6d   299: /* kwcolum
26fc7 6e 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20  n_opt ::= */.   
26fc8 20 20 20 63 61 73 65 20 33 30 30 3a 20 2f 2a 20     case 300: /* 
26fc9 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 3a 3a 3d  kwcolumn_opt ::=
26fca 20 43 4f 4c 55 4d 4e 4b 57 20 2a 2f 0a 20 20 20   COLUMNKW */.   
26fcb 20 20 20 63 61 73 65 20 33 30 34 3a 20 2f 2a 20     case 304: /* 
26fcc 76 74 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20  vtabarglist ::= 
26fcd 76 74 61 62 61 72 67 20 2a 2f 0a 20 20 20 20 20  vtabarg */.     
26fce 20 63 61 73 65 20 33 30 35 3a 20 2f 2a 20 76 74   case 305: /* vt
26fcf 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20 76 74  abarglist ::= vt
26fd0 61 62 61 72 67 6c 69 73 74 20 43 4f 4d 4d 41 20  abarglist COMMA 
26fd1 76 74 61 62 61 72 67 20 2a 2f 0a 20 20 20 20 20  vtabarg */.     
26fd2 20 63 61 73 65 20 33 30 37 3a 20 2f 2a 20 76 74   case 307: /* vt
26fd3 61 62 61 72 67 20 3a 3a 3d 20 76 74 61 62 61 72  abarg ::= vtabar
26fd4 67 20 76 74 61 62 61 72 67 74 6f 6b 65 6e 20 2a  g vtabargtoken *
26fd5 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33 31 31  /.      case 311
26fd6 3a 20 2f 2a 20 61 6e 79 6c 69 73 74 20 3a 3a 3d  : /* anylist ::=
26fd7 20 2a 2f 0a 7b 0a 7d 0a 20 20 20 20 20 20 20 20   */.{.}.        
26fd8 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
26fd9 65 20 33 3a 20 2f 2a 20 63 6d 64 78 20 3a 3a 3d  e 3: /* cmdx ::=
26fda 20 63 6d 64 20 2a 2f 0a 7b 20 73 71 6c 69 74 65   cmd */.{ sqlite
26fdb 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 70 50  3FinishCoding(pP
26fdc 61 72 73 65 29 3b 20 7d 0a 20 20 20 20 20 20 20  arse); }.       
26fdd 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
26fde 73 65 20 36 3a 20 2f 2a 20 65 78 70 6c 61 69 6e  se 6: /* explain
26fdf 20 3a 3a 3d 20 2a 2f 0a 7b 20 73 71 6c 69 74 65   ::= */.{ sqlite
26fe0 33 42 65 67 69 6e 50 61 72 73 65 28 70 50 61 72  3BeginParse(pPar
26fe1 73 65 2c 20 30 29 3b 20 7d 0a 20 20 20 20 20 20  se, 0); }.      
26fe2 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
26fe3 61 73 65 20 37 3a 20 2f 2a 20 65 78 70 6c 61 69  ase 7: /* explai
26fe4 6e 20 3a 3a 3d 20 45 58 50 4c 41 49 4e 20 2a 2f  n ::= EXPLAIN */
26fe5 0a 7b 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50  .{ sqlite3BeginP
26fe6 61 72 73 65 28 70 50 61 72 73 65 2c 20 31 29 3b  arse(pParse, 1);
26fe7 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
26fe8 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38 3a 20  ;.      case 8: 
26fe9 2f 2a 20 65 78 70 6c 61 69 6e 20 3a 3a 3d 20 45  /* explain ::= E
26fea 58 50 4c 41 49 4e 20 51 55 45 52 59 20 50 4c 41  XPLAIN QUERY PLA
26feb 4e 20 2a 2f 0a 7b 20 73 71 6c 69 74 65 33 42 65  N */.{ sqlite3Be
26fec 67 69 6e 50 61 72 73 65 28 70 50 61 72 73 65 2c  ginParse(pParse,
26fed 20 32 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62   2); }.        b
26fee 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
26fef 20 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 42   9: /* cmd ::= B
26ff0 45 47 49 4e 20 74 72 61 6e 73 74 79 70 65 20 74  EGIN transtype t
26ff1 72 61 6e 73 5f 6f 70 74 20 2a 2f 0a 7b 73 71 6c  rans_opt */.{sql
26ff2 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73 61 63  ite3BeginTransac
26ff3 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 79 79 6d  tion(pParse, yym
26ff4 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-1].minor.yy4
26ff5 36 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  6);}.        bre
26ff6 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
26ff7 33 3a 20 2f 2a 20 74 72 61 6e 73 74 79 70 65 20  3: /* transtype 
26ff8 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ::= */.{yygotomi
26ff9 6e 6f 72 2e 79 79 34 36 20 3d 20 54 4b 5f 44 45  nor.yy46 = TK_DE
26ffa 46 45 52 52 45 44 3b 7d 0a 20 20 20 20 20 20 20  FERRED;}.       
26ffb 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
26ffc 73 65 20 31 34 3a 20 2f 2a 20 74 72 61 6e 73 74  se 14: /* transt
26ffd 79 70 65 20 3a 3a 3d 20 44 45 46 45 52 52 45 44  ype ::= DEFERRED
26ffe 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
26fff 35 3a 20 2f 2a 20 74 72 61 6e 73 74 79 70 65 20  5: /* transtype 
27000 3a 3a 3d 20 49 4d 4d 45 44 49 41 54 45 20 2a 2f  ::= IMMEDIATE */
27001 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 3a 20  .      case 16: 
27002 2f 2a 20 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d  /* transtype ::=
27003 20 45 58 43 4c 55 53 49 56 45 20 2a 2f 0a 20 20   EXCLUSIVE */.  
27004 20 20 20 20 63 61 73 65 20 31 30 37 3a 20 2f 2a      case 107: /*
27005 20 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20   multiselect_op 
27006 3a 3a 3d 20 55 4e 49 4f 4e 20 2a 2f 0a 20 20 20  ::= UNION */.   
27007 20 20 20 63 61 73 65 20 31 30 39 3a 20 2f 2a 20     case 109: /* 
27008 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a  multiselect_op :
27009 3a 3d 20 45 58 43 45 50 54 7c 49 4e 54 45 52 53  := EXCEPT|INTERS
2700a 45 43 54 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ECT */.{yygotomi
2700b 6e 6f 72 2e 79 79 34 36 20 3d 20 79 79 6d 73 70  nor.yy46 = yymsp
2700c 5b 30 5d 2e 6d 61 6a 6f 72 3b 7d 0a 20 20 20 20  [0].major;}.    
2700d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2700e 20 63 61 73 65 20 31 37 3a 20 2f 2a 20 63 6d 64   case 17: /* cmd
2700f 20 3a 3a 3d 20 43 4f 4d 4d 49 54 20 74 72 61 6e   ::= COMMIT tran
27010 73 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 20 20 63  s_opt */.      c
27011 61 73 65 20 31 38 3a 20 2f 2a 20 63 6d 64 20 3a  ase 18: /* cmd :
27012 3a 3d 20 45 4e 44 20 74 72 61 6e 73 5f 6f 70 74  := END trans_opt
27013 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 43 6f 6d 6d   */.{sqlite3Comm
27014 69 74 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 50  itTransaction(pP
27015 61 72 73 65 29 3b 7d 0a 20 20 20 20 20 20 20 20  arse);}.        
27016 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
27017 65 20 31 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d  e 19: /* cmd ::=
27018 20 52 4f 4c 4c 42 41 43 4b 20 74 72 61 6e 73 5f   ROLLBACK trans_
27019 6f 70 74 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 52  opt */.{sqlite3R
2701a 6f 6c 6c 62 61 63 6b 54 72 61 6e 73 61 63 74 69  ollbackTransacti
2701b 6f 6e 28 70 50 61 72 73 65 29 3b 7d 0a 20 20 20  on(pParse);}.   
2701c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2701d 20 20 63 61 73 65 20 32 31 3a 20 2f 2a 20 63 72    case 21: /* cr
2701e 65 61 74 65 5f 74 61 62 6c 65 20 3a 3a 3d 20 43  eate_table ::= C
2701f 52 45 41 54 45 20 74 65 6d 70 20 54 41 42 4c 45  REATE temp TABLE
27020 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20   ifnotexists nm 
27021 64 62 6e 6d 20 2a 2f 0a 7b 0a 20 20 20 73 71 6c  dbnm */.{.   sql
27022 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28 70  ite3StartTable(p
27023 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d  Parse,&yymsp[-1]
27024 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 26 79 79  .minor.yy410,&yy
27025 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34  msp[0].minor.yy4
27026 31 30 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  10,yymsp[-4].min
27027 6f 72 2e 79 79 34 36 2c 30 2c 30 2c 79 79 6d 73  or.yy46,0,0,yyms
27028 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36  p[-2].minor.yy46
27029 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2702a 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2702b 32 3a 20 2f 2a 20 69 66 6e 6f 74 65 78 69 73 74  2: /* ifnotexist
2702c 73 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63  s ::= */.      c
2702d 61 73 65 20 32 35 3a 20 2f 2a 20 74 65 6d 70 20  ase 25: /* temp 
2702e 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ::= */.      cas
2702f 65 20 36 33 3a 20 2f 2a 20 61 75 74 6f 69 6e 63  e 63: /* autoinc
27030 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61   ::= */.      ca
27031 73 65 20 37 37 3a 20 2f 2a 20 69 6e 69 74 5f 64  se 77: /* init_d
27032 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74  eferred_pred_opt
27033 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61   ::= */.      ca
27034 73 65 20 37 39 3a 20 2f 2a 20 69 6e 69 74 5f 64  se 79: /* init_d
27035 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74  eferred_pred_opt
27036 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59 20 49   ::= INITIALLY I
27037 4d 4d 45 44 49 41 54 45 20 2a 2f 0a 20 20 20 20  MMEDIATE */.    
27038 20 20 63 61 73 65 20 39 30 3a 20 2f 2a 20 64 65    case 90: /* de
27039 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70  fer_subclause_op
2703a 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63  t ::= */.      c
2703b 61 73 65 20 31 30 31 3a 20 2f 2a 20 69 66 65 78  ase 101: /* ifex
2703c 69 73 74 73 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20  ists ::= */.    
2703d 20 20 63 61 73 65 20 31 31 32 3a 20 2f 2a 20 64    case 112: /* d
2703e 69 73 74 69 6e 63 74 20 3a 3a 3d 20 41 4c 4c 20  istinct ::= ALL 
2703f 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 31  */.      case 11
27040 33 3a 20 2f 2a 20 64 69 73 74 69 6e 63 74 20 3a  3: /* distinct :
27041 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  := */.      case
27042 20 32 31 33 3a 20 2f 2a 20 62 65 74 77 65 65 6e   213: /* between
27043 5f 6f 70 20 3a 3a 3d 20 42 45 54 57 45 45 4e 20  _op ::= BETWEEN 
27044 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 31  */.      case 21
27045 36 3a 20 2f 2a 20 69 6e 5f 6f 70 20 3a 3a 3d 20  6: /* in_op ::= 
27046 49 4e 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  IN */.{yygotomin
27047 6f 72 2e 79 79 34 36 20 3d 20 30 3b 7d 0a 20 20  or.yy46 = 0;}.  
27048 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
27049 20 20 20 63 61 73 65 20 32 33 3a 20 2f 2a 20 69     case 23: /* i
2704a 66 6e 6f 74 65 78 69 73 74 73 20 3a 3a 3d 20 49  fnotexists ::= I
2704b 46 20 4e 4f 54 20 45 58 49 53 54 53 20 2a 2f 0a  F NOT EXISTS */.
2704c 20 20 20 20 20 20 63 61 73 65 20 32 34 3a 20 2f        case 24: /
2704d 2a 20 74 65 6d 70 20 3a 3a 3d 20 54 45 4d 50 20  * temp ::= TEMP 
2704e 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 36 34  */.      case 64
2704f 3a 20 2f 2a 20 61 75 74 6f 69 6e 63 20 3a 3a 3d  : /* autoinc ::=
27050 20 41 55 54 4f 49 4e 43 52 20 2a 2f 0a 20 20 20   AUTOINCR */.   
27051 20 20 20 63 61 73 65 20 37 38 3a 20 2f 2a 20 69     case 78: /* i
27052 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65  nit_deferred_pre
27053 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41  d_opt ::= INITIA
27054 4c 4c 59 20 44 45 46 45 52 52 45 44 20 2a 2f 0a  LLY DEFERRED */.
27055 20 20 20 20 20 20 63 61 73 65 20 31 30 30 3a 20        case 100: 
27056 2f 2a 20 69 66 65 78 69 73 74 73 20 3a 3a 3d 20  /* ifexists ::= 
27057 49 46 20 45 58 49 53 54 53 20 2a 2f 0a 20 20 20  IF EXISTS */.   
27058 20 20 20 63 61 73 65 20 31 31 31 3a 20 2f 2a 20     case 111: /* 
27059 64 69 73 74 69 6e 63 74 20 3a 3a 3d 20 44 49 53  distinct ::= DIS
2705a 54 49 4e 43 54 20 2a 2f 0a 20 20 20 20 20 20 63  TINCT */.      c
2705b 61 73 65 20 32 31 34 3a 20 2f 2a 20 62 65 74 77  ase 214: /* betw
2705c 65 65 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20 42  een_op ::= NOT B
2705d 45 54 57 45 45 4e 20 2a 2f 0a 20 20 20 20 20 20  ETWEEN */.      
2705e 63 61 73 65 20 32 31 37 3a 20 2f 2a 20 69 6e 5f  case 217: /* in_
2705f 6f 70 20 3a 3a 3d 20 4e 4f 54 20 49 4e 20 2a 2f  op ::= NOT IN */
27060 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
27061 34 36 20 3d 20 31 3b 7d 0a 20 20 20 20 20 20 20  46 = 1;}.       
27062 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
27063 73 65 20 32 36 3a 20 2f 2a 20 63 72 65 61 74 65  se 26: /* create
27064 5f 74 61 62 6c 65 5f 61 72 67 73 20 3a 3a 3d 20  _table_args ::= 
27065 4c 50 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 63 6f  LP columnlist co
27066 6e 73 6c 69 73 74 5f 6f 70 74 20 52 50 20 2a 2f  nslist_opt RP */
27067 0a 7b 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 54  .{.  sqlite3EndT
27068 61 62 6c 65 28 70 50 61 72 73 65 2c 26 79 79 6d  able(pParse,&yym
27069 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-1].minor.yy4
2706a 31 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  10,&yymsp[0].min
2706b 6f 72 2e 79 79 30 2c 30 29 3b 0a 7d 0a 20 20 20  or.yy0,0);.}.   
2706c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2706d 20 20 63 61 73 65 20 32 37 3a 20 2f 2a 20 63 72    case 27: /* cr
2706e 65 61 74 65 5f 74 61 62 6c 65 5f 61 72 67 73 20  eate_table_args 
2706f 3a 3a 3d 20 41 53 20 73 65 6c 65 63 74 20 2a 2f  ::= AS select */
27070 0a 7b 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 54  .{.  sqlite3EndT
27071 61 62 6c 65 28 70 50 61 72 73 65 2c 30 2c 30 2c  able(pParse,0,0,
27072 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
27073 79 32 31 39 29 3b 0a 20 20 73 71 6c 69 74 65 33  y219);.  sqlite3
27074 53 65 6c 65 63 74 44 65 6c 65 74 65 28 79 79 6d  SelectDelete(yym
27075 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31  sp[0].minor.yy21
27076 39 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  9);.}.        br
27077 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
27078 33 30 3a 20 2f 2a 20 63 6f 6c 75 6d 6e 20 3a 3a  30: /* column ::
27079 3d 20 63 6f 6c 75 6d 6e 69 64 20 74 79 70 65 20  = columnid type 
2707a 63 61 72 67 6c 69 73 74 20 2a 2f 0a 7b 0a 20 20  carglist */.{.  
2707b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31  yygotominor.yy41
2707c 30 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e  0.z = yymsp[-2].
2707d 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 7a 3b 0a 20  minor.yy410.z;. 
2707e 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
2707f 31 30 2e 6e 20 3d 20 28 70 50 61 72 73 65 2d 3e  10.n = (pParse->
27080 73 4c 61 73 74 54 6f 6b 65 6e 2e 7a 2d 79 79 6d  sLastToken.z-yym
27081 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-2].minor.yy4
27082 31 30 2e 7a 29 20 2b 20 70 50 61 72 73 65 2d 3e  10.z) + pParse->
27083 73 4c 61 73 74 54 6f 6b 65 6e 2e 6e 3b 0a 7d 0a  sLastToken.n;.}.
27084 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
27085 20 20 20 20 20 63 61 73 65 20 33 31 3a 20 2f 2a       case 31: /*
27086 20 63 6f 6c 75 6d 6e 69 64 20 3a 3a 3d 20 6e 6d   columnid ::= nm
27087 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41   */.{.  sqlite3A
27088 64 64 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c  ddColumn(pParse,
27089 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2708a 79 79 34 31 30 29 3b 0a 20 20 79 79 67 6f 74 6f  yy410);.  yygoto
2708b 6d 69 6e 6f 72 2e 79 79 34 31 30 20 3d 20 79 79  minor.yy410 = yy
2708c 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34  msp[0].minor.yy4
2708d 31 30 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  10;.}.        br
2708e 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2708f 33 32 3a 20 2f 2a 20 69 64 20 3a 3a 3d 20 49 44  32: /* id ::= ID
27090 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33   */.      case 3
27091 33 3a 20 2f 2a 20 69 64 73 20 3a 3a 3d 20 49 44  3: /* ids ::= ID
27092 7c 53 54 52 49 4e 47 20 2a 2f 0a 20 20 20 20 20  |STRING */.     
27093 20 63 61 73 65 20 33 34 3a 20 2f 2a 20 6e 6d 20   case 34: /* nm 
27094 3a 3a 3d 20 49 44 20 2a 2f 0a 20 20 20 20 20 20  ::= ID */.      
27095 63 61 73 65 20 33 35 3a 20 2f 2a 20 6e 6d 20 3a  case 35: /* nm :
27096 3a 3d 20 53 54 52 49 4e 47 20 2a 2f 0a 20 20 20  := STRING */.   
27097 20 20 20 63 61 73 65 20 33 36 3a 20 2f 2a 20 6e     case 36: /* n
27098 6d 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 2a 2f  m ::= JOIN_KW */
27099 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 37 3a  .      case 257:
2709a 20 2f 2a 20 6e 75 6d 62 65 72 20 3a 3a 3d 20 49   /* number ::= I
2709b 4e 54 45 47 45 52 7c 46 4c 4f 41 54 20 2a 2f 0a  NTEGER|FLOAT */.
2709c 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34  {yygotominor.yy4
2709d 31 30 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  10 = yymsp[0].mi
2709e 6e 6f 72 2e 79 79 30 3b 7d 0a 20 20 20 20 20 20  nor.yy0;}.      
2709f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
270a0 61 73 65 20 33 38 3a 20 2f 2a 20 74 79 70 65 20  ase 38: /* type 
270a1 3a 3a 3d 20 74 79 70 65 74 6f 6b 65 6e 20 2a 2f  ::= typetoken */
270a2 0a 7b 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75  .{sqlite3AddColu
270a3 6d 6e 54 79 70 65 28 70 50 61 72 73 65 2c 26 79  mnType(pParse,&y
270a4 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
270a5 34 31 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  410);}.        b
270a6 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
270a7 20 33 39 3a 20 2f 2a 20 74 79 70 65 74 6f 6b 65   39: /* typetoke
270a8 6e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 20 2a  n ::= typename *
270a9 2f 0a 20 20 20 20 20 20 63 61 73 65 20 34 32 3a  /.      case 42:
270aa 20 2f 2a 20 74 79 70 65 6e 61 6d 65 20 3a 3a 3d   /* typename ::=
270ab 20 69 64 73 20 2a 2f 0a 20 20 20 20 20 20 63 61   ids */.      ca
270ac 73 65 20 31 31 39 3a 20 2f 2a 20 61 73 20 3a 3a  se 119: /* as ::
270ad 3d 20 41 53 20 6e 6d 20 2a 2f 0a 20 20 20 20 20  = AS nm */.     
270ae 20 63 61 73 65 20 31 32 30 3a 20 2f 2a 20 61 73   case 120: /* as
270af 20 3a 3a 3d 20 69 64 73 20 2a 2f 0a 20 20 20 20   ::= ids */.    
270b0 20 20 63 61 73 65 20 31 33 31 3a 20 2f 2a 20 64    case 131: /* d
270b1 62 6e 6d 20 3a 3a 3d 20 44 4f 54 20 6e 6d 20 2a  bnm ::= DOT nm *
270b2 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 31  /.      case 241
270b3 3a 20 2f 2a 20 69 64 78 69 74 65 6d 20 3a 3a 3d  : /* idxitem ::=
270b4 20 6e 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73   nm */.      cas
270b5 65 20 32 34 33 3a 20 2f 2a 20 63 6f 6c 6c 61 74  e 243: /* collat
270b6 65 20 3a 3a 3d 20 43 4f 4c 4c 41 54 45 20 69 64  e ::= COLLATE id
270b7 73 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  s */.      case 
270b8 32 35 33 3a 20 2f 2a 20 6e 6d 6e 75 6d 20 3a 3a  253: /* nmnum ::
270b9 3d 20 70 6c 75 73 5f 6e 75 6d 20 2a 2f 0a 20 20  = plus_num */.  
270ba 20 20 20 20 63 61 73 65 20 32 35 34 3a 20 2f 2a      case 254: /*
270bb 20 6e 6d 6e 75 6d 20 3a 3a 3d 20 6e 6d 20 2a 2f   nmnum ::= nm */
270bc 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 35 3a  .      case 255:
270bd 20 2f 2a 20 70 6c 75 73 5f 6e 75 6d 20 3a 3a 3d   /* plus_num ::=
270be 20 70 6c 75 73 5f 6f 70 74 20 6e 75 6d 62 65 72   plus_opt number
270bf 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
270c0 35 36 3a 20 2f 2a 20 6d 69 6e 75 73 5f 6e 75 6d  56: /* minus_num
270c1 20 3a 3a 3d 20 4d 49 4e 55 53 20 6e 75 6d 62 65   ::= MINUS numbe
270c2 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  r */.{yygotomino
270c3 72 2e 79 79 34 31 30 20 3d 20 79 79 6d 73 70 5b  r.yy410 = yymsp[
270c4 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 3b 7d  0].minor.yy410;}
270c5 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
270c6 20 20 20 20 20 20 63 61 73 65 20 34 30 3a 20 2f        case 40: /
270c7 2a 20 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20  * typetoken ::= 
270c8 74 79 70 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e  typename LP sign
270c9 65 64 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67  ed RP */.{.  yyg
270ca 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 7a  otominor.yy410.z
270cb 20 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e   = yymsp[-3].min
270cc 6f 72 2e 79 79 34 31 30 2e 7a 3b 0a 20 20 79 79  or.yy410.z;.  yy
270cd 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30 2e  gotominor.yy410.
270ce 6e 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  n = &yymsp[0].mi
270cf 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b  nor.yy0.z[yymsp[
270d0 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 20  0].minor.yy0.n] 
270d1 2d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  - yymsp[-3].mino
270d2 72 2e 79 79 34 31 30 2e 7a 3b 0a 7d 0a 20 20 20  r.yy410.z;.}.   
270d3 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
270d4 20 20 63 61 73 65 20 34 31 3a 20 2f 2a 20 74 79    case 41: /* ty
270d5 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65  petoken ::= type
270d6 6e 61 6d 65 20 4c 50 20 73 69 67 6e 65 64 20 43  name LP signed C
270d7 4f 4d 4d 41 20 73 69 67 6e 65 64 20 52 50 20 2a  OMMA signed RP *
270d8 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
270d9 72 2e 79 79 34 31 30 2e 7a 20 3d 20 79 79 6d 73  r.yy410.z = yyms
270da 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31  p[-5].minor.yy41
270db 30 2e 7a 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  0.z;.  yygotomin
270dc 6f 72 2e 79 79 34 31 30 2e 6e 20 3d 20 26 79 79  or.yy410.n = &yy
270dd 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
270de 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  .z[yymsp[0].mino
270df 72 2e 79 79 30 2e 6e 5d 20 2d 20 79 79 6d 73 70  r.yy0.n] - yymsp
270e0 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30  [-5].minor.yy410
270e1 2e 7a 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  .z;.}.        br
270e2 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
270e3 34 33 3a 20 2f 2a 20 74 79 70 65 6e 61 6d 65 20  43: /* typename 
270e4 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 20 69 64 73  ::= typename ids
270e5 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
270e6 2e 79 79 34 31 30 2e 7a 3d 79 79 6d 73 70 5b 2d  .yy410.z=yymsp[-
270e7 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 7a  1].minor.yy410.z
270e8 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ; yygotominor.yy
270e9 34 31 30 2e 6e 3d 79 79 6d 73 70 5b 30 5d 2e 6d  410.n=yymsp[0].m
270ea 69 6e 6f 72 2e 79 79 34 31 30 2e 6e 2b 28 79 79  inor.yy410.n+(yy
270eb 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34  msp[0].minor.yy4
270ec 31 30 2e 7a 2d 79 79 6d 73 70 5b 2d 31 5d 2e 6d  10.z-yymsp[-1].m
270ed 69 6e 6f 72 2e 79 79 34 31 30 2e 7a 29 3b 7d 0a  inor.yy410.z);}.
270ee 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
270ef 20 20 20 20 20 63 61 73 65 20 35 30 3a 20 2f 2a       case 50: /*
270f0 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55   ccons ::= DEFAU
270f1 4c 54 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 20  LT term */.     
270f2 20 63 61 73 65 20 35 32 3a 20 2f 2a 20 63 63 6f   case 52: /* cco
270f3 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 50  ns ::= DEFAULT P
270f4 4c 55 53 20 74 65 72 6d 20 2a 2f 0a 7b 73 71 6c  LUS term */.{sql
270f5 69 74 65 33 41 64 64 44 65 66 61 75 6c 74 56 61  ite3AddDefaultVa
270f6 6c 75 65 28 70 50 61 72 73 65 2c 79 79 6d 73 70  lue(pParse,yymsp
270f7 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 29  [0].minor.yy172)
270f8 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
270f9 3b 0a 20 20 20 20 20 20 63 61 73 65 20 35 31 3a  ;.      case 51:
270fa 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45   /* ccons ::= DE
270fb 46 41 55 4c 54 20 4c 50 20 65 78 70 72 20 52 50  FAULT LP expr RP
270fc 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 44   */.{sqlite3AddD
270fd 65 66 61 75 6c 74 56 61 6c 75 65 28 70 50 61 72  efaultValue(pPar
270fe 73 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  se,yymsp[-1].min
270ff 6f 72 2e 79 79 31 37 32 29 3b 7d 0a 20 20 20 20  or.yy172);}.    
27100 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
27101 20 63 61 73 65 20 35 33 3a 20 2f 2a 20 63 63 6f   case 53: /* cco
27102 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 4d  ns ::= DEFAULT M
27103 49 4e 55 53 20 74 65 72 6d 20 2a 2f 0a 7b 0a 20  INUS term */.{. 
27104 20 45 78 70 72 20 2a 70 20 3d 20 73 71 6c 69 74   Expr *p = sqlit
27105 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
27106 54 4b 5f 55 4d 49 4e 55 53 2c 20 79 79 6d 73 70  TK_UMINUS, yymsp
27107 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c  [0].minor.yy172,
27108 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65   0, 0);.  sqlite
27109 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65  3AddDefaultValue
2710a 28 70 50 61 72 73 65 2c 70 29 3b 0a 7d 0a 20 20  (pParse,p);.}.  
2710b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2710c 20 20 20 63 61 73 65 20 35 34 3a 20 2f 2a 20 63     case 54: /* c
2710d 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54  cons ::= DEFAULT
2710e 20 69 64 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20   id */.{.  Expr 
2710f 2a 70 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  *p = sqlite3PExp
27110 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 53 54 52  r(pParse, TK_STR
27111 49 4e 47 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73  ING, 0, 0, &yyms
27112 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30  p[0].minor.yy410
27113 29 3b 0a 20 20 73 71 6c 69 74 65 33 41 64 64 44  );.  sqlite3AddD
27114 65 66 61 75 6c 74 56 61 6c 75 65 28 70 50 61 72  efaultValue(pPar
27115 73 65 2c 70 29 3b 0a 7d 0a 20 20 20 20 20 20 20  se,p);.}.       
27116 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
27117 73 65 20 35 36 3a 20 2f 2a 20 63 63 6f 6e 73 20  se 56: /* ccons 
27118 3a 3a 3d 20 4e 4f 54 20 4e 55 4c 4c 20 6f 6e 63  ::= NOT NULL onc
27119 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41  onf */.{sqlite3A
2711a 64 64 4e 6f 74 4e 75 6c 6c 28 70 50 61 72 73 65  ddNotNull(pParse
2711b 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
2711c 2e 79 79 34 36 29 3b 7d 0a 20 20 20 20 20 20 20  .yy46);}.       
2711d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2711e 73 65 20 35 37 3a 20 2f 2a 20 63 63 6f 6e 73 20  se 57: /* ccons 
2711f 3a 3a 3d 20 50 52 49 4d 41 52 59 20 4b 45 59 20  ::= PRIMARY KEY 
27120 73 6f 72 74 6f 72 64 65 72 20 6f 6e 63 6f 6e 66  sortorder onconf
27121 20 61 75 74 6f 69 6e 63 20 2a 2f 0a 7b 73 71 6c   autoinc */.{sql
27122 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65  ite3AddPrimaryKe
27123 79 28 70 50 61 72 73 65 2c 30 2c 79 79 6d 73 70  y(pParse,0,yymsp
27124 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 2c  [-1].minor.yy46,
27125 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
27126 79 34 36 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  y46,yymsp[-2].mi
27127 6e 6f 72 2e 79 79 34 36 29 3b 7d 0a 20 20 20 20  nor.yy46);}.    
27128 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
27129 20 63 61 73 65 20 35 38 3a 20 2f 2a 20 63 63 6f   case 58: /* cco
2712a 6e 73 20 3a 3a 3d 20 55 4e 49 51 55 45 20 6f 6e  ns ::= UNIQUE on
2712b 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  conf */.{sqlite3
2712c 43 72 65 61 74 65 49 6e 64 65 78 28 70 50 61 72  CreateIndex(pPar
2712d 73 65 2c 30 2c 30 2c 30 2c 30 2c 79 79 6d 73 70  se,0,0,0,0,yymsp
2712e 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 2c 30  [0].minor.yy46,0
2712f 2c 30 2c 30 2c 30 29 3b 7d 0a 20 20 20 20 20 20  ,0,0,0);}.      
27130 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
27131 61 73 65 20 35 39 3a 20 2f 2a 20 63 63 6f 6e 73  ase 59: /* ccons
27132 20 3a 3a 3d 20 43 48 45 43 4b 20 4c 50 20 65 78   ::= CHECK LP ex
27133 70 72 20 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65  pr RP */.{sqlite
27134 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74 72 61  3AddCheckConstra
27135 69 6e 74 28 70 50 61 72 73 65 2c 79 79 6d 73 70  int(pParse,yymsp
27136 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32  [-1].minor.yy172
27137 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
27138 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36 30  k;.      case 60
27139 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 52  : /* ccons ::= R
2713a 45 46 45 52 45 4e 43 45 53 20 6e 6d 20 69 64 78  EFERENCES nm idx
2713b 6c 69 73 74 5f 6f 70 74 20 72 65 66 61 72 67 73  list_opt refargs
2713c 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 43 72 65 61   */.{sqlite3Crea
2713d 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 70 50 61  teForeignKey(pPa
2713e 72 73 65 2c 30 2c 26 79 79 6d 73 70 5b 2d 32 5d  rse,0,&yymsp[-2]
2713f 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 79 79 6d  .minor.yy410,yym
27140 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
27141 37 34 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  74,yymsp[0].mino
27142 72 2e 79 79 34 36 29 3b 7d 0a 20 20 20 20 20 20  r.yy46);}.      
27143 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
27144 61 73 65 20 36 31 3a 20 2f 2a 20 63 63 6f 6e 73  ase 61: /* ccons
27145 20 3a 3a 3d 20 64 65 66 65 72 5f 73 75 62 63 6c   ::= defer_subcl
27146 61 75 73 65 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  ause */.{sqlite3
27147 44 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28  DeferForeignKey(
27148 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 30 5d 2e  pParse,yymsp[0].
27149 6d 69 6e 6f 72 2e 79 79 34 36 29 3b 7d 0a 20 20  minor.yy46);}.  
2714a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2714b 20 20 20 63 61 73 65 20 36 32 3a 20 2f 2a 20 63     case 62: /* c
2714c 63 6f 6e 73 20 3a 3a 3d 20 43 4f 4c 4c 41 54 45  cons ::= COLLATE
2714d 20 69 64 73 20 2a 2f 0a 7b 73 71 6c 69 74 65 33   ids */.{sqlite3
2714e 41 64 64 43 6f 6c 6c 61 74 65 54 79 70 65 28 70  AddCollateType(p
2714f 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 30 5d  Parse, &yymsp[0]
27150 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29 3b 7d 0a  .minor.yy410);}.
27151 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
27152 20 20 20 20 20 63 61 73 65 20 36 35 3a 20 2f 2a       case 65: /*
27153 20 72 65 66 61 72 67 73 20 3a 3a 3d 20 2a 2f 0a   refargs ::= */.
27154 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
27155 34 36 20 3d 20 4f 45 5f 52 65 73 74 72 69 63 74  46 = OE_Restrict
27156 20 2a 20 30 78 30 31 30 31 30 31 3b 20 7d 0a 20   * 0x010101; }. 
27157 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
27158 20 20 20 20 63 61 73 65 20 36 36 3a 20 2f 2a 20      case 66: /* 
27159 72 65 66 61 72 67 73 20 3a 3a 3d 20 72 65 66 61  refargs ::= refa
2715a 72 67 73 20 72 65 66 61 72 67 20 2a 2f 0a 7b 20  rgs refarg */.{ 
2715b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 36  yygotominor.yy46
2715c 20 3d 20 28 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69   = (yymsp[-1].mi
2715d 6e 6f 72 2e 79 79 34 36 20 26 20 79 79 6d 73 70  nor.yy46 & yymsp
2715e 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 35 2e  [0].minor.yy405.
2715f 6d 61 73 6b 29 20 7c 20 79 79 6d 73 70 5b 30 5d  mask) | yymsp[0]
27160 2e 6d 69 6e 6f 72 2e 79 79 34 30 35 2e 76 61 6c  .minor.yy405.val
27161 75 65 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  ue; }.        br
27162 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
27163 36 37 3a 20 2f 2a 20 72 65 66 61 72 67 20 3a 3a  67: /* refarg ::
27164 3d 20 4d 41 54 43 48 20 6e 6d 20 2a 2f 0a 7b 20  = MATCH nm */.{ 
27165 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 30  yygotominor.yy40
27166 35 2e 76 61 6c 75 65 20 3d 20 30 3b 20 20 20 20  5.value = 0;    
27167 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
27168 30 35 2e 6d 61 73 6b 20 3d 20 30 78 30 30 30 30  05.mask = 0x0000
27169 30 30 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  00; }.        br
2716a 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2716b 36 38 3a 20 2f 2a 20 72 65 66 61 72 67 20 3a 3a  68: /* refarg ::
2716c 3d 20 4f 4e 20 44 45 4c 45 54 45 20 72 65 66 61  = ON DELETE refa
2716d 63 74 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69  ct */.{ yygotomi
2716e 6e 6f 72 2e 79 79 34 30 35 2e 76 61 6c 75 65 20  nor.yy405.value 
2716f 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
27170 2e 79 79 34 36 3b 20 20 20 20 20 79 79 67 6f 74  .yy46;     yygot
27171 6f 6d 69 6e 6f 72 2e 79 79 34 30 35 2e 6d 61 73  ominor.yy405.mas
27172 6b 20 3d 20 30 78 30 30 30 30 66 66 3b 20 7d 0a  k = 0x0000ff; }.
27173 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
27174 20 20 20 20 20 63 61 73 65 20 36 39 3a 20 2f 2a       case 69: /*
27175 20 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 55   refarg ::= ON U
27176 50 44 41 54 45 20 72 65 66 61 63 74 20 2a 2f 0a  PDATE refact */.
27177 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
27178 34 30 35 2e 76 61 6c 75 65 20 3d 20 79 79 6d 73  405.value = yyms
27179 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 3c  p[0].minor.yy46<
2717a 3c 38 3b 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  <8;  yygotominor
2717b 2e 79 79 34 30 35 2e 6d 61 73 6b 20 3d 20 30 78  .yy405.mask = 0x
2717c 30 30 66 66 30 30 3b 20 7d 0a 20 20 20 20 20 20  00ff00; }.      
2717d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2717e 61 73 65 20 37 30 3a 20 2f 2a 20 72 65 66 61 72  ase 70: /* refar
2717f 67 20 3a 3a 3d 20 4f 4e 20 49 4e 53 45 52 54 20  g ::= ON INSERT 
27180 72 65 66 61 63 74 20 2a 2f 0a 7b 20 79 79 67 6f  refact */.{ yygo
27181 74 6f 6d 69 6e 6f 72 2e 79 79 34 30 35 2e 76 61  tominor.yy405.va
27182 6c 75 65 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  lue = yymsp[0].m
27183 69 6e 6f 72 2e 79 79 34 36 3c 3c 31 36 3b 20 79  inor.yy46<<16; y
27184 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 30 35  ygotominor.yy405
27185 2e 6d 61 73 6b 20 3d 20 30 78 66 66 30 30 30 30  .mask = 0xff0000
27186 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ; }.        brea
27187 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37 31  k;.      case 71
27188 3a 20 2f 2a 20 72 65 66 61 63 74 20 3a 3a 3d 20  : /* refact ::= 
27189 53 45 54 20 4e 55 4c 4c 20 2a 2f 0a 7b 20 79 79  SET NULL */.{ yy
2718a 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 36 20 3d  gotominor.yy46 =
2718b 20 4f 45 5f 53 65 74 4e 75 6c 6c 3b 20 7d 0a 20   OE_SetNull; }. 
2718c 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2718d 20 20 20 20 63 61 73 65 20 37 32 3a 20 2f 2a 20      case 72: /* 
2718e 72 65 66 61 63 74 20 3a 3a 3d 20 53 45 54 20 44  refact ::= SET D
2718f 45 46 41 55 4c 54 20 2a 2f 0a 7b 20 79 79 67 6f  EFAULT */.{ yygo
27190 74 6f 6d 69 6e 6f 72 2e 79 79 34 36 20 3d 20 4f  tominor.yy46 = O
27191 45 5f 53 65 74 44 66 6c 74 3b 20 7d 0a 20 20 20  E_SetDflt; }.   
27192 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
27193 20 20 63 61 73 65 20 37 33 3a 20 2f 2a 20 72 65    case 73: /* re
27194 66 61 63 74 20 3a 3a 3d 20 43 41 53 43 41 44 45  fact ::= CASCADE
27195 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f   */.{ yygotomino
27196 72 2e 79 79 34 36 20 3d 20 4f 45 5f 43 61 73 63  r.yy46 = OE_Casc
27197 61 64 65 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  ade; }.        b
27198 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
27199 20 37 34 3a 20 2f 2a 20 72 65 66 61 63 74 20 3a   74: /* refact :
2719a 3a 3d 20 52 45 53 54 52 49 43 54 20 2a 2f 0a 7b  := RESTRICT */.{
2719b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
2719c 36 20 3d 20 4f 45 5f 52 65 73 74 72 69 63 74 3b  6 = OE_Restrict;
2719d 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
2719e 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37 35 3a  ;.      case 75:
2719f 20 2f 2a 20 64 65 66 65 72 5f 73 75 62 63 6c 61   /* defer_subcla
271a0 75 73 65 20 3a 3a 3d 20 4e 4f 54 20 44 45 46 45  use ::= NOT DEFE
271a1 52 52 41 42 4c 45 20 69 6e 69 74 5f 64 65 66 65  RRABLE init_defe
271a2 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 2a 2f  rred_pred_opt */
271a3 0a 20 20 20 20 20 20 63 61 73 65 20 37 36 3a 20  .      case 76: 
271a4 2f 2a 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75  /* defer_subclau
271a5 73 65 20 3a 3a 3d 20 44 45 46 45 52 52 41 42 4c  se ::= DEFERRABL
271a6 45 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f  E init_deferred_
271a7 70 72 65 64 5f 6f 70 74 20 2a 2f 0a 20 20 20 20  pred_opt */.    
271a8 20 20 63 61 73 65 20 39 31 3a 20 2f 2a 20 64 65    case 91: /* de
271a9 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70  fer_subclause_op
271aa 74 20 3a 3a 3d 20 64 65 66 65 72 5f 73 75 62 63  t ::= defer_subc
271ab 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20 63  lause */.      c
271ac 61 73 65 20 39 33 3a 20 2f 2a 20 6f 6e 63 6f 6e  ase 93: /* oncon
271ad 66 20 3a 3a 3d 20 4f 4e 20 43 4f 4e 46 4c 49 43  f ::= ON CONFLIC
271ae 54 20 72 65 73 6f 6c 76 65 74 79 70 65 20 2a 2f  T resolvetype */
271af 0a 20 20 20 20 20 20 63 61 73 65 20 39 35 3a 20  .      case 95: 
271b0 2f 2a 20 6f 72 63 6f 6e 66 20 3a 3a 3d 20 4f 52  /* orconf ::= OR
271b1 20 72 65 73 6f 6c 76 65 74 79 70 65 20 2a 2f 0a   resolvetype */.
271b2 20 20 20 20 20 20 63 61 73 65 20 39 36 3a 20 2f        case 96: /
271b3 2a 20 72 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a  * resolvetype ::
271b4 3d 20 72 61 69 73 65 74 79 70 65 20 2a 2f 0a 20  = raisetype */. 
271b5 20 20 20 20 20 63 61 73 65 20 31 36 36 3a 20 2f       case 166: /
271b6 2a 20 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a 3d  * insert_cmd ::=
271b7 20 49 4e 53 45 52 54 20 6f 72 63 6f 6e 66 20 2a   INSERT orconf *
271b8 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
271b9 79 34 36 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  y46 = yymsp[0].m
271ba 69 6e 6f 72 2e 79 79 34 36 3b 7d 0a 20 20 20 20  inor.yy46;}.    
271bb 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
271bc 20 63 61 73 65 20 38 30 3a 20 2f 2a 20 63 6f 6e   case 80: /* con
271bd 73 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f  slist_opt ::= */
271be 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
271bf 34 31 30 2e 6e 20 3d 20 30 3b 20 79 79 67 6f 74  410.n = 0; yygot
271c0 6f 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 7a 20 3d  ominor.yy410.z =
271c1 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65   0;}.        bre
271c2 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38  ak;.      case 8
271c3 31 3a 20 2f 2a 20 63 6f 6e 73 6c 69 73 74 5f 6f  1: /* conslist_o
271c4 70 74 20 3a 3a 3d 20 43 4f 4d 4d 41 20 63 6f 6e  pt ::= COMMA con
271c5 73 6c 69 73 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f  slist */.{yygoto
271c6 6d 69 6e 6f 72 2e 79 79 34 31 30 20 3d 20 79 79  minor.yy410 = yy
271c7 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
271c8 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
271c9 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38 36  k;.      case 86
271ca 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20 50  : /* tcons ::= P
271cb 52 49 4d 41 52 59 20 4b 45 59 20 4c 50 20 69 64  RIMARY KEY LP id
271cc 78 6c 69 73 74 20 61 75 74 6f 69 6e 63 20 52 50  xlist autoinc RP
271cd 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69   onconf */.{sqli
271ce 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65 79  te3AddPrimaryKey
271cf 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 33  (pParse,yymsp[-3
271d0 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 2c 79 79  ].minor.yy174,yy
271d1 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34  msp[0].minor.yy4
271d2 36 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  6,yymsp[-2].mino
271d3 72 2e 79 79 34 36 2c 30 29 3b 7d 0a 20 20 20 20  r.yy46,0);}.    
271d4 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
271d5 20 63 61 73 65 20 38 37 3a 20 2f 2a 20 74 63 6f   case 87: /* tco
271d6 6e 73 20 3a 3a 3d 20 55 4e 49 51 55 45 20 4c 50  ns ::= UNIQUE LP
271d7 20 69 64 78 6c 69 73 74 20 52 50 20 6f 6e 63 6f   idxlist RP onco
271d8 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 43 72  nf */.{sqlite3Cr
271d9 65 61 74 65 49 6e 64 65 78 28 70 50 61 72 73 65  eateIndex(pParse
271da 2c 30 2c 30 2c 30 2c 79 79 6d 73 70 5b 2d 32 5d  ,0,0,0,yymsp[-2]
271db 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 2c 79 79 6d  .minor.yy174,yym
271dc 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36  sp[0].minor.yy46
271dd 2c 30 2c 30 2c 30 2c 30 29 3b 7d 0a 20 20 20 20  ,0,0,0,0);}.    
271de 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
271df 20 63 61 73 65 20 38 38 3a 20 2f 2a 20 74 63 6f   case 88: /* tco
271e0 6e 73 20 3a 3a 3d 20 43 48 45 43 4b 20 4c 50 20  ns ::= CHECK LP 
271e1 65 78 70 72 20 52 50 20 6f 6e 63 6f 6e 66 20 2a  expr RP onconf *
271e2 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 43 68 65  /.{sqlite3AddChe
271e3 63 6b 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61  ckConstraint(pPa
271e4 72 73 65 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  rse,yymsp[-2].mi
271e5 6e 6f 72 2e 79 79 31 37 32 29 3b 7d 0a 20 20 20  nor.yy172);}.   
271e6 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
271e7 20 20 63 61 73 65 20 38 39 3a 20 2f 2a 20 74 63    case 89: /* tc
271e8 6f 6e 73 20 3a 3a 3d 20 46 4f 52 45 49 47 4e 20  ons ::= FOREIGN 
271e9 4b 45 59 20 4c 50 20 69 64 78 6c 69 73 74 20 52  KEY LP idxlist R
271ea 50 20 52 45 46 45 52 45 4e 43 45 53 20 6e 6d 20  P REFERENCES nm 
271eb 69 64 78 6c 69 73 74 5f 6f 70 74 20 72 65 66 61  idxlist_opt refa
271ec 72 67 73 20 64 65 66 65 72 5f 73 75 62 63 6c 61  rgs defer_subcla
271ed 75 73 65 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20 20  use_opt */.{.   
271ee 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 6f   sqlite3CreateFo
271ef 72 65 69 67 6e 4b 65 79 28 70 50 61 72 73 65 2c  reignKey(pParse,
271f0 20 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72   yymsp[-6].minor
271f1 2e 79 79 31 37 34 2c 20 26 79 79 6d 73 70 5b 2d  .yy174, &yymsp[-
271f2 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 20  3].minor.yy410, 
271f3 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
271f4 79 79 31 37 34 2c 20 79 79 6d 73 70 5b 2d 31 5d  yy174, yymsp[-1]
271f5 2e 6d 69 6e 6f 72 2e 79 79 34 36 29 3b 0a 20 20  .minor.yy46);.  
271f6 20 20 73 71 6c 69 74 65 33 44 65 66 65 72 46 6f    sqlite3DeferFo
271f7 72 65 69 67 6e 4b 65 79 28 70 50 61 72 73 65 2c  reignKey(pParse,
271f8 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
271f9 79 79 34 36 29 3b 0a 7d 0a 20 20 20 20 20 20 20  yy46);.}.       
271fa 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
271fb 73 65 20 39 32 3a 20 2f 2a 20 6f 6e 63 6f 6e 66  se 92: /* onconf
271fc 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61   ::= */.      ca
271fd 73 65 20 39 34 3a 20 2f 2a 20 6f 72 63 6f 6e 66  se 94: /* orconf
271fe 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d   ::= */.{yygotom
271ff 69 6e 6f 72 2e 79 79 34 36 20 3d 20 4f 45 5f 44  inor.yy46 = OE_D
27200 65 66 61 75 6c 74 3b 7d 0a 20 20 20 20 20 20 20  efault;}.       
27201 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
27202 73 65 20 39 37 3a 20 2f 2a 20 72 65 73 6f 6c 76  se 97: /* resolv
27203 65 74 79 70 65 20 3a 3a 3d 20 49 47 4e 4f 52 45  etype ::= IGNORE
27204 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
27205 2e 79 79 34 36 20 3d 20 4f 45 5f 49 67 6e 6f 72  .yy46 = OE_Ignor
27206 65 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  e;}.        brea
27207 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 39 38  k;.      case 98
27208 3a 20 2f 2a 20 72 65 73 6f 6c 76 65 74 79 70 65  : /* resolvetype
27209 20 3a 3a 3d 20 52 45 50 4c 41 43 45 20 2a 2f 0a   ::= REPLACE */.
2720a 20 20 20 20 20 20 63 61 73 65 20 31 36 37 3a 20        case 167: 
2720b 2f 2a 20 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a  /* insert_cmd ::
2720c 3d 20 52 45 50 4c 41 43 45 20 2a 2f 0a 7b 79 79  = REPLACE */.{yy
2720d 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 36 20 3d  gotominor.yy46 =
2720e 20 4f 45 5f 52 65 70 6c 61 63 65 3b 7d 0a 20 20   OE_Replace;}.  
2720f 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
27210 20 20 20 63 61 73 65 20 39 39 3a 20 2f 2a 20 63     case 99: /* c
27211 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54 41 42 4c  md ::= DROP TABL
27212 45 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e  E ifexists fulln
27213 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74  ame */.{.  sqlit
27214 65 33 44 72 6f 70 54 61 62 6c 65 28 70 50 61 72  e3DropTable(pPar
27215 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  se, yymsp[0].min
27216 6f 72 2e 79 79 33 37 33 2c 20 30 2c 20 79 79 6d  or.yy373, 0, yym
27217 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-1].minor.yy4
27218 36 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  6);.}.        br
27219 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2721a 31 30 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  102: /* cmd ::= 
2721b 43 52 45 41 54 45 20 74 65 6d 70 20 56 49 45 57  CREATE temp VIEW
2721c 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20   ifnotexists nm 
2721d 64 62 6e 6d 20 41 53 20 73 65 6c 65 63 74 20 2a  dbnm AS select *
2721e 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 43 72 65  /.{.  sqlite3Cre
2721f 61 74 65 56 69 65 77 28 70 50 61 72 73 65 2c 20  ateView(pParse, 
27220 26 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72  &yymsp[-7].minor
27221 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 2d 33 5d  .yy0, &yymsp[-3]
27222 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 20 26 79  .minor.yy410, &y
27223 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
27224 79 34 31 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  y410, yymsp[0].m
27225 69 6e 6f 72 2e 79 79 32 31 39 2c 20 79 79 6d 73  inor.yy219, yyms
27226 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36  p[-6].minor.yy46
27227 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  , yymsp[-4].mino
27228 72 2e 79 79 34 36 29 3b 0a 7d 0a 20 20 20 20 20  r.yy46);.}.     
27229 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2722a 63 61 73 65 20 31 30 33 3a 20 2f 2a 20 63 6d 64  case 103: /* cmd
2722b 20 3a 3a 3d 20 44 52 4f 50 20 56 49 45 57 20 69   ::= DROP VIEW i
2722c 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65  fexists fullname
2722d 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 44   */.{.  sqlite3D
2722e 72 6f 70 54 61 62 6c 65 28 70 50 61 72 73 65 2c  ropTable(pParse,
2722f 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
27230 79 79 33 37 33 2c 20 31 2c 20 79 79 6d 73 70 5b  yy373, 1, yymsp[
27231 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 29 3b  -1].minor.yy46);
27232 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
27233 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 34  ;.      case 104
27234 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 73 65 6c  : /* cmd ::= sel
27235 65 63 74 20 2a 2f 0a 7b 0a 20 20 53 65 6c 65 63  ect */.{.  Selec
27236 74 44 65 73 74 20 64 65 73 74 20 3d 20 7b 53 52  tDest dest = {SR
27237 54 5f 43 61 6c 6c 62 61 63 6b 2c 20 30 2c 20 30  T_Callback, 0, 0
27238 2c 20 30 2c 20 30 7d 3b 0a 20 20 73 71 6c 69 74  , 0, 0};.  sqlit
27239 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
2723a 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2723b 79 79 32 31 39 2c 20 26 64 65 73 74 2c 20 30 2c  yy219, &dest, 0,
2723c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c   0, 0, 0);.  sql
2723d 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
2723e 28 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  (yymsp[0].minor.
2723f 79 79 32 31 39 29 3b 0a 7d 0a 20 20 20 20 20 20  yy219);.}.      
27240 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
27241 61 73 65 20 31 30 35 3a 20 2f 2a 20 73 65 6c 65  ase 105: /* sele
27242 63 74 20 3a 3a 3d 20 6f 6e 65 73 65 6c 65 63 74  ct ::= oneselect
27243 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
27244 32 38 3a 20 2f 2a 20 73 65 6c 74 61 62 6c 69 73  28: /* seltablis
27245 74 5f 70 61 72 65 6e 20 3a 3a 3d 20 73 65 6c 65  t_paren ::= sele
27246 63 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  ct */.{yygotomin
27247 6f 72 2e 79 79 32 31 39 20 3d 20 79 79 6d 73 70  or.yy219 = yymsp
27248 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31 39 3b  [0].minor.yy219;
27249 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2724a 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 36 3a  .      case 106:
2724b 20 2f 2a 20 73 65 6c 65 63 74 20 3a 3a 3d 20 73   /* select ::= s
2724c 65 6c 65 63 74 20 6d 75 6c 74 69 73 65 6c 65 63  elect multiselec
2724d 74 5f 6f 70 20 6f 6e 65 73 65 6c 65 63 74 20 2a  t_op oneselect *
2724e 2f 0a 7b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b  /.{.  if( yymsp[
2724f 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31 39 20 29  0].minor.yy219 )
27250 7b 0a 20 20 20 20 79 79 6d 73 70 5b 30 5d 2e 6d  {.    yymsp[0].m
27251 69 6e 6f 72 2e 79 79 32 31 39 2d 3e 6f 70 20 3d  inor.yy219->op =
27252 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
27253 2e 79 79 34 36 3b 0a 20 20 20 20 79 79 6d 73 70  .yy46;.    yymsp
27254 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31 39 2d  [0].minor.yy219-
27255 3e 70 50 72 69 6f 72 20 3d 20 79 79 6d 73 70 5b  >pPrior = yymsp[
27256 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31 39 3b  -2].minor.yy219;
27257 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
27258 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
27259 65 28 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  e(yymsp[-2].mino
2725a 72 2e 79 79 32 31 39 29 3b 0a 20 20 7d 0a 20 20  r.yy219);.  }.  
2725b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 31  yygotominor.yy21
2725c 39 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  9 = yymsp[0].min
2725d 6f 72 2e 79 79 32 31 39 3b 0a 7d 0a 20 20 20 20  or.yy219;.}.    
2725e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2725f 20 63 61 73 65 20 31 30 38 3a 20 2f 2a 20 6d 75   case 108: /* mu
27260 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d  ltiselect_op ::=
27261 20 55 4e 49 4f 4e 20 41 4c 4c 20 2a 2f 0a 7b 79   UNION ALL */.{y
27262 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 36 20  ygotominor.yy46 
27263 3d 20 54 4b 5f 41 4c 4c 3b 7d 0a 20 20 20 20 20  = TK_ALL;}.     
27264 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
27265 63 61 73 65 20 31 31 30 3a 20 2f 2a 20 6f 6e 65  case 110: /* one
27266 73 65 6c 65 63 74 20 3a 3a 3d 20 53 45 4c 45 43  select ::= SELEC
27267 54 20 64 69 73 74 69 6e 63 74 20 73 65 6c 63 6f  T distinct selco
27268 6c 6c 69 73 74 20 66 72 6f 6d 20 77 68 65 72 65  llist from where
27269 5f 6f 70 74 20 67 72 6f 75 70 62 79 5f 6f 70 74  _opt groupby_opt
2726a 20 68 61 76 69 6e 67 5f 6f 70 74 20 6f 72 64 65   having_opt orde
2726b 72 62 79 5f 6f 70 74 20 6c 69 6d 69 74 5f 6f 70  rby_opt limit_op
2726c 74 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d  t */.{.  yygotom
2726d 69 6e 6f 72 2e 79 79 32 31 39 20 3d 20 73 71 6c  inor.yy219 = sql
2726e 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50  ite3SelectNew(pP
2726f 61 72 73 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d  arse,yymsp[-6].m
27270 69 6e 6f 72 2e 79 79 31 37 34 2c 79 79 6d 73 70  inor.yy174,yymsp
27271 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 33 37 33  [-5].minor.yy373
27272 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  ,yymsp[-4].minor
27273 2e 79 79 31 37 32 2c 79 79 6d 73 70 5b 2d 33 5d  .yy172,yymsp[-3]
27274 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 2c 79 79 6d  .minor.yy174,yym
27275 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-2].minor.yy1
27276 37 32 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  72,yymsp[-1].min
27277 6f 72 2e 79 79 31 37 34 2c 79 79 6d 73 70 5b 2d  or.yy174,yymsp[-
27278 37 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 2c 79 79  7].minor.yy46,yy
27279 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32  msp[0].minor.yy2
2727a 33 34 2e 70 4c 69 6d 69 74 2c 79 79 6d 73 70 5b  34.pLimit,yymsp[
2727b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 33 34 2e 70  0].minor.yy234.p
2727c 4f 66 66 73 65 74 29 3b 0a 7d 0a 20 20 20 20 20  Offset);.}.     
2727d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2727e 63 61 73 65 20 31 31 34 3a 20 2f 2a 20 73 63 6c  case 114: /* scl
2727f 70 20 3a 3a 3d 20 73 65 6c 63 6f 6c 6c 69 73 74  p ::= selcollist
27280 20 43 4f 4d 4d 41 20 2a 2f 0a 20 20 20 20 20 20   COMMA */.      
27281 63 61 73 65 20 32 33 38 3a 20 2f 2a 20 69 64 78  case 238: /* idx
27282 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 4c 50 20  list_opt ::= LP 
27283 69 64 78 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 79  idxlist RP */.{y
27284 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34  ygotominor.yy174
27285 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e   = yymsp[-1].min
27286 6f 72 2e 79 79 31 37 34 3b 7d 0a 20 20 20 20 20  or.yy174;}.     
27287 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
27288 63 61 73 65 20 31 31 35 3a 20 2f 2a 20 73 63 6c  case 115: /* scl
27289 70 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63  p ::= */.      c
2728a 61 73 65 20 31 34 31 3a 20 2f 2a 20 6f 72 64 65  ase 141: /* orde
2728b 72 62 79 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20  rby_opt ::= */. 
2728c 20 20 20 20 20 63 61 73 65 20 31 34 39 3a 20 2f       case 149: /
2728d 2a 20 67 72 6f 75 70 62 79 5f 6f 70 74 20 3a 3a  * groupby_opt ::
2728e 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  = */.      case 
2728f 32 33 31 3a 20 2f 2a 20 65 78 70 72 6c 69 73 74  231: /* exprlist
27290 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61   ::= */.      ca
27291 73 65 20 32 33 37 3a 20 2f 2a 20 69 64 78 6c 69  se 237: /* idxli
27292 73 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 79  st_opt ::= */.{y
27293 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34  ygotominor.yy174
27294 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62   = 0;}.        b
27295 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
27296 20 31 31 36 3a 20 2f 2a 20 73 65 6c 63 6f 6c 6c   116: /* selcoll
27297 69 73 74 20 3a 3a 3d 20 73 63 6c 70 20 65 78 70  ist ::= sclp exp
27298 72 20 61 73 20 2a 2f 0a 7b 0a 20 20 20 79 79 67  r as */.{.   yyg
27299 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 20 3d  otominor.yy174 =
2729a 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2729b 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79  Append(pParse,yy
2729c 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2729d 31 37 34 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  174,yymsp[-1].mi
2729e 6e 6f 72 2e 79 79 31 37 32 2c 79 79 6d 73 70 5b  nor.yy172,yymsp[
2729f 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 6e  0].minor.yy410.n
272a0 3f 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ?&yymsp[0].minor
272a1 2e 79 79 34 31 30 3a 30 29 3b 0a 7d 0a 20 20 20  .yy410:0);.}.   
272a2 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
272a3 20 20 63 61 73 65 20 31 31 37 3a 20 2f 2a 20 73    case 117: /* s
272a4 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63  elcollist ::= sc
272a5 6c 70 20 53 54 41 52 20 2a 2f 0a 7b 0a 20 20 45  lp STAR */.{.  E
272a6 78 70 72 20 2a 70 20 3d 20 73 71 6c 69 74 65 33  xpr *p = sqlite3
272a7 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
272a8 5f 41 4c 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  _ALL, 0, 0, 0);.
272a9 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
272aa 31 37 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70  174 = sqlite3Exp
272ab 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
272ac 73 65 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  se, yymsp[-1].mi
272ad 6e 6f 72 2e 79 79 31 37 34 2c 20 70 2c 20 30 29  nor.yy174, p, 0)
272ae 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
272af 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 31  k;.      case 11
272b0 38 3a 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74  8: /* selcollist
272b1 20 3a 3a 3d 20 73 63 6c 70 20 6e 6d 20 44 4f 54   ::= sclp nm DOT
272b2 20 53 54 41 52 20 2a 2f 0a 7b 0a 20 20 45 78 70   STAR */.{.  Exp
272b3 72 20 2a 70 52 69 67 68 74 20 3d 20 73 71 6c 69  r *pRight = sqli
272b4 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
272b5 20 54 4b 5f 41 4c 4c 2c 20 30 2c 20 30 2c 20 30   TK_ALL, 0, 0, 0
272b6 29 3b 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74  );.  Expr *pLeft
272b7 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
272b8 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30  pParse, TK_ID, 0
272b9 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e  , 0, &yymsp[-2].
272ba 6d 69 6e 6f 72 2e 79 79 34 31 30 29 3b 0a 20 20  minor.yy410);.  
272bb 45 78 70 72 20 2a 70 44 6f 74 20 3d 20 73 71 6c  Expr *pDot = sql
272bc 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
272bd 2c 20 54 4b 5f 44 4f 54 2c 20 70 4c 65 66 74 2c  , TK_DOT, pLeft,
272be 20 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 20 79   pRight, 0);.  y
272bf 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34  ygotominor.yy174
272c0 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
272c1 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
272c2 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
272c3 79 79 31 37 34 2c 20 70 44 6f 74 2c 20 30 29 3b  yy174, pDot, 0);
272c4 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
272c5 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 31  ;.      case 121
272c6 3a 20 2f 2a 20 61 73 20 3a 3a 3d 20 2a 2f 0a 7b  : /* as ::= */.{
272c7 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31  yygotominor.yy41
272c8 30 2e 6e 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20  0.n = 0;}.      
272c9 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
272ca 61 73 65 20 31 32 32 3a 20 2f 2a 20 66 72 6f 6d  ase 122: /* from
272cb 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d   ::= */.{yygotom
272cc 69 6e 6f 72 2e 79 79 33 37 33 20 3d 20 73 71 6c  inor.yy373 = sql
272cd 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
272ce 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 73 69 7a  (pParse->db, siz
272cf 65 6f 66 28 2a 79 79 67 6f 74 6f 6d 69 6e 6f 72  eof(*yygotominor
272d0 2e 79 79 33 37 33 29 29 3b 7d 0a 20 20 20 20 20  .yy373));}.     
272d1 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
272d2 63 61 73 65 20 31 32 33 3a 20 2f 2a 20 66 72 6f  case 123: /* fro
272d3 6d 20 3a 3a 3d 20 46 52 4f 4d 20 73 65 6c 74 61  m ::= FROM selta
272d4 62 6c 69 73 74 20 2a 2f 0a 7b 0a 20 20 79 79 67  blist */.{.  yyg
272d5 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 37 33 20 3d  otominor.yy373 =
272d6 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
272d7 79 79 33 37 33 3b 0a 20 20 73 71 6c 69 74 65 33  yy373;.  sqlite3
272d8 53 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e  SrcListShiftJoin
272d9 54 79 70 65 28 79 79 67 6f 74 6f 6d 69 6e 6f 72  Type(yygotominor
272da 2e 79 79 33 37 33 29 3b 0a 7d 0a 20 20 20 20 20  .yy373);.}.     
272db 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
272dc 63 61 73 65 20 31 32 34 3a 20 2f 2a 20 73 74 6c  case 124: /* stl
272dd 5f 70 72 65 66 69 78 20 3a 3a 3d 20 73 65 6c 74  _prefix ::= selt
272de 61 62 6c 69 73 74 20 6a 6f 69 6e 6f 70 20 2a 2f  ablist joinop */
272df 0a 7b 0a 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  .{.   yygotomino
272e0 72 2e 79 79 33 37 33 20 3d 20 79 79 6d 73 70 5b  r.yy373 = yymsp[
272e1 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 37 33 3b  -1].minor.yy373;
272e2 0a 20 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69  .   if( yygotomi
272e3 6e 6f 72 2e 79 79 33 37 33 20 26 26 20 79 79 67  nor.yy373 && yyg
272e4 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 37 33 2d 3e  otominor.yy373->
272e5 6e 53 72 63 3e 30 20 29 20 79 79 67 6f 74 6f 6d  nSrc>0 ) yygotom
272e6 69 6e 6f 72 2e 79 79 33 37 33 2d 3e 61 5b 79 79  inor.yy373->a[yy
272e7 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 37 33 2d  gotominor.yy373-
272e8 3e 6e 53 72 63 2d 31 5d 2e 6a 6f 69 6e 74 79 70  >nSrc-1].jointyp
272e9 65 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  e = yymsp[0].min
272ea 6f 72 2e 79 79 34 36 3b 0a 7d 0a 20 20 20 20 20  or.yy46;.}.     
272eb 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
272ec 63 61 73 65 20 31 32 35 3a 20 2f 2a 20 73 74 6c  case 125: /* stl
272ed 5f 70 72 65 66 69 78 20 3a 3a 3d 20 2a 2f 0a 7b  _prefix ::= */.{
272ee 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 37  yygotominor.yy37
272ef 33 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20  3 = 0;}.        
272f0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
272f1 65 20 31 32 36 3a 20 2f 2a 20 73 65 6c 74 61 62  e 126: /* seltab
272f2 6c 69 73 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65  list ::= stl_pre
272f3 66 69 78 20 6e 6d 20 64 62 6e 6d 20 61 73 20 6f  fix nm dbnm as o
272f4 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 20  n_opt using_opt 
272f5 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  */.{.  yygotomin
272f6 6f 72 2e 79 79 33 37 33 20 3d 20 73 71 6c 69 74  or.yy373 = sqlit
272f7 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 46  e3SrcListAppendF
272f8 72 6f 6d 54 65 72 6d 28 70 50 61 72 73 65 2c 79  romTerm(pParse,y
272f9 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-5].minor.y
272fa 79 33 37 33 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e  y373,&yymsp[-4].
272fb 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 26 79 79 6d  minor.yy410,&yym
272fc 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-3].minor.yy4
272fd 31 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  10,&yymsp[-2].mi
272fe 6e 6f 72 2e 79 79 34 31 30 2c 30 2c 79 79 6d 73  nor.yy410,0,yyms
272ff 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37  p[-1].minor.yy17
27300 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  2,yymsp[0].minor
27301 2e 79 79 34 33 32 29 3b 0a 7d 0a 20 20 20 20 20  .yy432);.}.     
27302 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
27303 63 61 73 65 20 31 32 37 3a 20 2f 2a 20 73 65 6c  case 127: /* sel
27304 74 61 62 6c 69 73 74 20 3a 3a 3d 20 73 74 6c 5f  tablist ::= stl_
27305 70 72 65 66 69 78 20 4c 50 20 73 65 6c 74 61 62  prefix LP seltab
27306 6c 69 73 74 5f 70 61 72 65 6e 20 52 50 20 61 73  list_paren RP as
27307 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70   on_opt using_op
27308 74 20 2a 2f 0a 7b 0a 20 20 20 20 79 79 67 6f 74  t */.{.    yygot
27309 6f 6d 69 6e 6f 72 2e 79 79 33 37 33 20 3d 20 73  ominor.yy373 = s
2730a 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
2730b 65 6e 64 46 72 6f 6d 54 65 72 6d 28 70 50 61 72  endFromTerm(pPar
2730c 73 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e  se,yymsp[-6].min
2730d 6f 72 2e 79 79 33 37 33 2c 30 2c 30 2c 26 79 79  or.yy373,0,0,&yy
2730e 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2730f 34 31 30 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  410,yymsp[-4].mi
27310 6e 6f 72 2e 79 79 32 31 39 2c 79 79 6d 73 70 5b  nor.yy219,yymsp[
27311 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c  -1].minor.yy172,
27312 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
27313 79 34 33 32 29 3b 0a 20 20 7d 0a 20 20 20 20 20  y432);.  }.     
27314 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
27315 63 61 73 65 20 31 32 39 3a 20 2f 2a 20 73 65 6c  case 129: /* sel
27316 74 61 62 6c 69 73 74 5f 70 61 72 65 6e 20 3a 3a  tablist_paren ::
27317 3d 20 73 65 6c 74 61 62 6c 69 73 74 20 2a 2f 0a  = seltablist */.
27318 7b 0a 20 20 20 20 20 73 71 6c 69 74 65 33 53 72  {.     sqlite3Sr
27319 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79  cListShiftJoinTy
2731a 70 65 28 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  pe(yymsp[0].mino
2731b 72 2e 79 79 33 37 33 29 3b 0a 20 20 20 20 20 79  r.yy373);.     y
2731c 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 31 39  ygotominor.yy219
2731d 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
2731e 4e 65 77 28 70 50 61 72 73 65 2c 30 2c 79 79 6d  New(pParse,0,yym
2731f 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 37  sp[0].minor.yy37
27320 33 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 29  3,0,0,0,0,0,0,0)
27321 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72  ;.  }.        br
27322 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
27323 31 33 30 3a 20 2f 2a 20 64 62 6e 6d 20 3a 3a 3d  130: /* dbnm ::=
27324 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
27325 2e 79 79 34 31 30 2e 7a 3d 30 3b 20 79 79 67 6f  .yy410.z=0; yygo
27326 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 6e 3d  tominor.yy410.n=
27327 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
27328 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33  k;.      case 13
27329 32 3a 20 2f 2a 20 66 75 6c 6c 6e 61 6d 65 20 3a  2: /* fullname :
2732a 3a 3d 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 79  := nm dbnm */.{y
2732b 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 37 33  ygotominor.yy373
2732c 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
2732d 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e  tAppend(pParse->
2732e 64 62 2c 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e  db,0,&yymsp[-1].
2732f 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 26 79 79 6d  minor.yy410,&yym
27330 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31  sp[0].minor.yy41
27331 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
27332 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
27333 33 33 3a 20 2f 2a 20 6a 6f 69 6e 6f 70 20 3a 3a  33: /* joinop ::
27334 3d 20 43 4f 4d 4d 41 7c 4a 4f 49 4e 20 2a 2f 0a  = COMMA|JOIN */.
27335 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
27336 34 36 20 3d 20 4a 54 5f 49 4e 4e 45 52 3b 20 7d  46 = JT_INNER; }
27337 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
27338 20 20 20 20 20 20 63 61 73 65 20 31 33 34 3a 20        case 134: 
27339 2f 2a 20 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f  /* joinop ::= JO
2733a 49 4e 5f 4b 57 20 4a 4f 49 4e 20 2a 2f 0a 7b 20  IN_KW JOIN */.{ 
2733b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 36  yygotominor.yy46
2733c 20 3d 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79   = sqlite3JoinTy
2733d 70 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70  pe(pParse,&yymsp
2733e 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30  [-1].minor.yy0,0
2733f 2c 30 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  ,0); }.        b
27340 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
27341 20 31 33 35 3a 20 2f 2a 20 6a 6f 69 6e 6f 70 20   135: /* joinop 
27342 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 6e 6d 20 4a  ::= JOIN_KW nm J
27343 4f 49 4e 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d  OIN */.{ yygotom
27344 69 6e 6f 72 2e 79 79 34 36 20 3d 20 73 71 6c 69  inor.yy46 = sqli
27345 74 65 33 4a 6f 69 6e 54 79 70 65 28 70 50 61 72  te3JoinType(pPar
27346 73 65 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  se,&yymsp[-2].mi
27347 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d  nor.yy0,&yymsp[-
27348 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 30  1].minor.yy410,0
27349 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  ); }.        bre
2734a 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2734b 33 36 3a 20 2f 2a 20 6a 6f 69 6e 6f 70 20 3a 3a  36: /* joinop ::
2734c 3d 20 4a 4f 49 4e 5f 4b 57 20 6e 6d 20 6e 6d 20  = JOIN_KW nm nm 
2734d 4a 4f 49 4e 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f  JOIN */.{ yygoto
2734e 6d 69 6e 6f 72 2e 79 79 34 36 20 3d 20 73 71 6c  minor.yy46 = sql
2734f 69 74 65 33 4a 6f 69 6e 54 79 70 65 28 70 50 61  ite3JoinType(pPa
27350 72 73 65 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d  rse,&yymsp[-3].m
27351 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
27352 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c  -2].minor.yy410,
27353 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
27354 2e 79 79 34 31 30 29 3b 20 7d 0a 20 20 20 20 20  .yy410); }.     
27355 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
27356 63 61 73 65 20 31 33 37 3a 20 2f 2a 20 6f 6e 5f  case 137: /* on_
27357 6f 70 74 20 3a 3a 3d 20 4f 4e 20 65 78 70 72 20  opt ::= ON expr 
27358 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 34  */.      case 14
27359 35 3a 20 2f 2a 20 73 6f 72 74 69 74 65 6d 20 3a  5: /* sortitem :
2735a 3a 3d 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20  := expr */.     
2735b 20 63 61 73 65 20 31 35 32 3a 20 2f 2a 20 68 61   case 152: /* ha
2735c 76 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 48 41 56  ving_opt ::= HAV
2735d 49 4e 47 20 65 78 70 72 20 2a 2f 0a 20 20 20 20  ING expr */.    
2735e 20 20 63 61 73 65 20 31 35 39 3a 20 2f 2a 20 77    case 159: /* w
2735f 68 65 72 65 5f 6f 70 74 20 3a 3a 3d 20 57 48 45  here_opt ::= WHE
27360 52 45 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20  RE expr */.     
27361 20 63 61 73 65 20 31 37 34 3a 20 2f 2a 20 65 78   case 174: /* ex
27362 70 72 20 3a 3a 3d 20 74 65 72 6d 20 2a 2f 0a 20  pr ::= term */. 
27363 20 20 20 20 20 63 61 73 65 20 32 30 32 3a 20 2f       case 202: /
27364 2a 20 65 73 63 61 70 65 20 3a 3a 3d 20 45 53 43  * escape ::= ESC
27365 41 50 45 20 65 78 70 72 20 2a 2f 0a 20 20 20 20  APE expr */.    
27366 20 20 63 61 73 65 20 32 32 36 3a 20 2f 2a 20 63    case 226: /* c
27367 61 73 65 5f 65 6c 73 65 20 3a 3a 3d 20 45 4c 53  ase_else ::= ELS
27368 45 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20  E expr */.      
27369 63 61 73 65 20 32 32 38 3a 20 2f 2a 20 63 61 73  case 228: /* cas
2736a 65 5f 6f 70 65 72 61 6e 64 20 3a 3a 3d 20 65 78  e_operand ::= ex
2736b 70 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  pr */.{yygotomin
2736c 6f 72 2e 79 79 31 37 32 20 3d 20 79 79 6d 73 70  or.yy172 = yymsp
2736d 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 3b  [0].minor.yy172;
2736e 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2736f 0a 20 20 20 20 20 20 63 61 73 65 20 31 33 38 3a  .      case 138:
27370 20 2f 2a 20 6f 6e 5f 6f 70 74 20 3a 3a 3d 20 2a   /* on_opt ::= *
27371 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 31  /.      case 151
27372 3a 20 2f 2a 20 68 61 76 69 6e 67 5f 6f 70 74 20  : /* having_opt 
27373 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ::= */.      cas
27374 65 20 31 35 38 3a 20 2f 2a 20 77 68 65 72 65 5f  e 158: /* where_
27375 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20  opt ::= */.     
27376 20 63 61 73 65 20 32 30 33 3a 20 2f 2a 20 65 73   case 203: /* es
27377 63 61 70 65 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20  cape ::= */.    
27378 20 20 63 61 73 65 20 32 32 37 3a 20 2f 2a 20 63    case 227: /* c
27379 61 73 65 5f 65 6c 73 65 20 3a 3a 3d 20 2a 2f 0a  ase_else ::= */.
2737a 20 20 20 20 20 20 63 61 73 65 20 32 32 39 3a 20        case 229: 
2737b 2f 2a 20 63 61 73 65 5f 6f 70 65 72 61 6e 64 20  /* case_operand 
2737c 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ::= */.{yygotomi
2737d 6e 6f 72 2e 79 79 31 37 32 20 3d 20 30 3b 7d 0a  nor.yy172 = 0;}.
2737e 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2737f 20 20 20 20 20 63 61 73 65 20 31 33 39 3a 20 2f       case 139: /
27380 2a 20 75 73 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20  * using_opt ::= 
27381 55 53 49 4e 47 20 4c 50 20 69 6e 73 63 6f 6c 6c  USING LP inscoll
27382 69 73 74 20 52 50 20 2a 2f 0a 20 20 20 20 20 20  ist RP */.      
27383 63 61 73 65 20 31 37 31 3a 20 2f 2a 20 69 6e 73  case 171: /* ins
27384 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20  collist_opt ::= 
27385 4c 50 20 69 6e 73 63 6f 6c 6c 69 73 74 20 52 50  LP inscollist RP
27386 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
27387 2e 79 79 34 33 32 20 3d 20 79 79 6d 73 70 5b 2d  .yy432 = yymsp[-
27388 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 33 32 3b 7d  1].minor.yy432;}
27389 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2738a 20 20 20 20 20 20 63 61 73 65 20 31 34 30 3a 20        case 140: 
2738b 2f 2a 20 75 73 69 6e 67 5f 6f 70 74 20 3a 3a 3d  /* using_opt ::=
2738c 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
2738d 37 30 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73  70: /* inscollis
2738e 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 79 79  t_opt ::= */.{yy
2738f 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 33 32 20  gotominor.yy432 
27390 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  = 0;}.        br
27391 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
27392 31 34 32 3a 20 2f 2a 20 6f 72 64 65 72 62 79 5f  142: /* orderby_
27393 6f 70 74 20 3a 3a 3d 20 4f 52 44 45 52 20 42 59  opt ::= ORDER BY
27394 20 73 6f 72 74 6c 69 73 74 20 2a 2f 0a 20 20 20   sortlist */.   
27395 20 20 20 63 61 73 65 20 31 35 30 3a 20 2f 2a 20     case 150: /* 
27396 67 72 6f 75 70 62 79 5f 6f 70 74 20 3a 3a 3d 20  groupby_opt ::= 
27397 47 52 4f 55 50 20 42 59 20 6e 65 78 70 72 6c 69  GROUP BY nexprli
27398 73 74 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  st */.      case
27399 20 32 33 30 3a 20 2f 2a 20 65 78 70 72 6c 69 73   230: /* exprlis
2739a 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74 20  t ::= nexprlist 
2739b 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2739c 79 79 31 37 34 20 3d 20 79 79 6d 73 70 5b 30 5d  yy174 = yymsp[0]
2739d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 3b 7d 0a 20  .minor.yy174;}. 
2739e 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2739f 20 20 20 20 63 61 73 65 20 31 34 33 3a 20 2f 2a      case 143: /*
273a0 20 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20 73 6f   sortlist ::= so
273a1 72 74 6c 69 73 74 20 43 4f 4d 4d 41 20 73 6f 72  rtlist COMMA sor
273a2 74 69 74 65 6d 20 73 6f 72 74 6f 72 64 65 72 20  titem sortorder 
273a3 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  */.{.  yygotomin
273a4 6f 72 2e 79 79 31 37 34 20 3d 20 73 71 6c 69 74  or.yy174 = sqlit
273a5 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
273a6 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 33  (pParse,yymsp[-3
273a7 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 2c 79 79  ].minor.yy174,yy
273a8 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
273a9 31 37 32 2c 30 29 3b 0a 20 20 69 66 28 20 79 79  172,0);.  if( yy
273aa 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 20  gotominor.yy174 
273ab 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ) yygotominor.yy
273ac 31 37 34 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e  174->a[yygotomin
273ad 6f 72 2e 79 79 31 37 34 2d 3e 6e 45 78 70 72 2d  or.yy174->nExpr-
273ae 31 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 79  1].sortOrder = y
273af 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
273b0 34 36 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  46;.}.        br
273b1 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
273b2 31 34 34 3a 20 2f 2a 20 73 6f 72 74 6c 69 73 74  144: /* sortlist
273b3 20 3a 3a 3d 20 73 6f 72 74 69 74 65 6d 20 73 6f   ::= sortitem so
273b4 72 74 6f 72 64 65 72 20 2a 2f 0a 7b 0a 20 20 79  rtorder */.{.  y
273b5 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34  ygotominor.yy174
273b6 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
273b7 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
273b8 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  0,yymsp[-1].mino
273b9 72 2e 79 79 31 37 32 2c 30 29 3b 0a 20 20 69 66  r.yy172,0);.  if
273ba 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
273bb 31 37 34 20 26 26 20 79 79 67 6f 74 6f 6d 69 6e  174 && yygotomin
273bc 6f 72 2e 79 79 31 37 34 2d 3e 61 20 29 20 79 79  or.yy174->a ) yy
273bd 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 2d  gotominor.yy174-
273be 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 64 65 72 20  >a[0].sortOrder 
273bf 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
273c0 2e 79 79 34 36 3b 0a 7d 0a 20 20 20 20 20 20 20  .yy46;.}.       
273c1 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
273c2 73 65 20 31 34 36 3a 20 2f 2a 20 73 6f 72 74 6f  se 146: /* sorto
273c3 72 64 65 72 20 3a 3a 3d 20 41 53 43 20 2a 2f 0a  rder ::= ASC */.
273c4 20 20 20 20 20 20 63 61 73 65 20 31 34 38 3a 20        case 148: 
273c5 2f 2a 20 73 6f 72 74 6f 72 64 65 72 20 3a 3a 3d  /* sortorder ::=
273c6 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
273c7 2e 79 79 34 36 20 3d 20 53 51 4c 49 54 45 5f 53  .yy46 = SQLITE_S
273c8 4f 5f 41 53 43 3b 7d 0a 20 20 20 20 20 20 20 20  O_ASC;}.        
273c9 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
273ca 65 20 31 34 37 3a 20 2f 2a 20 73 6f 72 74 6f 72  e 147: /* sortor
273cb 64 65 72 20 3a 3a 3d 20 44 45 53 43 20 2a 2f 0a  der ::= DESC */.
273cc 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34  {yygotominor.yy4
273cd 36 20 3d 20 53 51 4c 49 54 45 5f 53 4f 5f 44 45  6 = SQLITE_SO_DE
273ce 53 43 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  SC;}.        bre
273cf 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
273d0 35 33 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f 70 74  53: /* limit_opt
273d1 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d   ::= */.{yygotom
273d2 69 6e 6f 72 2e 79 79 32 33 34 2e 70 4c 69 6d 69  inor.yy234.pLimi
273d3 74 20 3d 20 30 3b 20 79 79 67 6f 74 6f 6d 69 6e  t = 0; yygotomin
273d4 6f 72 2e 79 79 32 33 34 2e 70 4f 66 66 73 65 74  or.yy234.pOffset
273d5 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62   = 0;}.        b
273d6 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
273d7 20 31 35 34 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f   154: /* limit_o
273d8 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70  pt ::= LIMIT exp
273d9 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  r */.{yygotomino
273da 72 2e 79 79 32 33 34 2e 70 4c 69 6d 69 74 20 3d  r.yy234.pLimit =
273db 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
273dc 79 79 31 37 32 3b 20 79 79 67 6f 74 6f 6d 69 6e  yy172; yygotomin
273dd 6f 72 2e 79 79 32 33 34 2e 70 4f 66 66 73 65 74  or.yy234.pOffset
273de 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62   = 0;}.        b
273df 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
273e0 20 31 35 35 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f   155: /* limit_o
273e1 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70  pt ::= LIMIT exp
273e2 72 20 4f 46 46 53 45 54 20 65 78 70 72 20 2a 2f  r OFFSET expr */
273e3 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
273e4 32 33 34 2e 70 4c 69 6d 69 74 20 3d 20 79 79 6d  234.pLimit = yym
273e5 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-2].minor.yy1
273e6 37 32 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  72; yygotominor.
273e7 79 79 32 33 34 2e 70 4f 66 66 73 65 74 20 3d 20  yy234.pOffset = 
273e8 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
273e9 79 31 37 32 3b 7d 0a 20 20 20 20 20 20 20 20 62  y172;}.        b
273ea 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
273eb 20 31 35 36 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f   156: /* limit_o
273ec 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70  pt ::= LIMIT exp
273ed 72 20 43 4f 4d 4d 41 20 65 78 70 72 20 2a 2f 0a  r COMMA expr */.
273ee 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32  {yygotominor.yy2
273ef 33 34 2e 70 4f 66 66 73 65 74 20 3d 20 79 79 6d  34.pOffset = yym
273f0 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-2].minor.yy1
273f1 37 32 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  72; yygotominor.
273f2 79 79 32 33 34 2e 70 4c 69 6d 69 74 20 3d 20 79  yy234.pLimit = y
273f3 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
273f4 31 37 32 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  172;}.        br
273f5 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
273f6 31 35 37 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  157: /* cmd ::= 
273f7 44 45 4c 45 54 45 20 46 52 4f 4d 20 66 75 6c 6c  DELETE FROM full
273f8 6e 61 6d 65 20 77 68 65 72 65 5f 6f 70 74 20 2a  name where_opt *
273f9 2f 0a 7b 73 71 6c 69 74 65 33 44 65 6c 65 74 65  /.{sqlite3Delete
273fa 46 72 6f 6d 28 70 50 61 72 73 65 2c 79 79 6d 73  From(pParse,yyms
273fb 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 37  p[-1].minor.yy37
273fc 33 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  3,yymsp[0].minor
273fd 2e 79 79 31 37 32 29 3b 7d 0a 20 20 20 20 20 20  .yy172);}.      
273fe 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
273ff 61 73 65 20 31 36 30 3a 20 2f 2a 20 63 6d 64 20  ase 160: /* cmd 
27400 3a 3a 3d 20 55 50 44 41 54 45 20 6f 72 63 6f 6e  ::= UPDATE orcon
27401 66 20 66 75 6c 6c 6e 61 6d 65 20 53 45 54 20 73  f fullname SET s
27402 65 74 6c 69 73 74 20 77 68 65 72 65 5f 6f 70 74  etlist where_opt
27403 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 45   */.{.  sqlite3E
27404 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67  xprListCheckLeng
27405 74 68 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  th(pParse,yymsp[
27406 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 2c  -1].minor.yy174,
27407 22 73 65 74 20 6c 69 73 74 22 29 3b 20 0a 20 20  "set list"); .  
27408 73 71 6c 69 74 65 33 55 70 64 61 74 65 28 70 50  sqlite3Update(pP
27409 61 72 73 65 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d  arse,yymsp[-3].m
2740a 69 6e 6f 72 2e 79 79 33 37 33 2c 79 79 6d 73 70  inor.yy373,yymsp
2740b 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34  [-1].minor.yy174
2740c 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  ,yymsp[0].minor.
2740d 79 79 31 37 32 2c 79 79 6d 73 70 5b 2d 34 5d 2e  yy172,yymsp[-4].
2740e 6d 69 6e 6f 72 2e 79 79 34 36 29 3b 0a 7d 0a 20  minor.yy46);.}. 
2740f 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
27410 20 20 20 20 63 61 73 65 20 31 36 31 3a 20 2f 2a      case 161: /*
27411 20 73 65 74 6c 69 73 74 20 3a 3a 3d 20 73 65 74   setlist ::= set
27412 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 20 45 51  list COMMA nm EQ
27413 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f   expr */.{yygoto
27414 6d 69 6e 6f 72 2e 79 79 31 37 34 20 3d 20 73 71  minor.yy174 = sq
27415 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
27416 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70  end(pParse,yymsp
27417 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34  [-4].minor.yy174
27418 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  ,yymsp[0].minor.
27419 79 79 31 37 32 2c 26 79 79 6d 73 70 5b 2d 32 5d  yy172,&yymsp[-2]
2741a 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29 3b 7d 0a  .minor.yy410);}.
2741b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2741c 20 20 20 20 20 63 61 73 65 20 31 36 32 3a 20 2f       case 162: /
2741d 2a 20 73 65 74 6c 69 73 74 20 3a 3a 3d 20 6e 6d  * setlist ::= nm
2741e 20 45 51 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67   EQ expr */.{yyg
2741f 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 20 3d  otominor.yy174 =
27420 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
27421 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30 2c  Append(pParse,0,
27422 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
27423 79 31 37 32 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e  y172,&yymsp[-2].
27424 6d 69 6e 6f 72 2e 79 79 34 31 30 29 3b 7d 0a 20  minor.yy410);}. 
27425 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
27426 20 20 20 20 63 61 73 65 20 31 36 33 3a 20 2f 2a      case 163: /*
27427 20 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f   cmd ::= insert_
27428 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d  cmd INTO fullnam
27429 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74  e inscollist_opt
2742a 20 56 41 4c 55 45 53 20 4c 50 20 69 74 65 6d 6c   VALUES LP iteml
2742b 69 73 74 20 52 50 20 2a 2f 0a 7b 73 71 6c 69 74  ist RP */.{sqlit
2742c 65 33 49 6e 73 65 72 74 28 70 50 61 72 73 65 2c  e3Insert(pParse,
2742d 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72   yymsp[-5].minor
2742e 2e 79 79 33 37 33 2c 20 79 79 6d 73 70 5b 2d 31  .yy373, yymsp[-1
2742f 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 2c 20 30  ].minor.yy174, 0
27430 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  , yymsp[-4].mino
27431 72 2e 79 79 34 33 32 2c 20 79 79 6d 73 70 5b 2d  r.yy432, yymsp[-
27432 37 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 29 3b 7d  7].minor.yy46);}
27433 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
27434 20 20 20 20 20 20 63 61 73 65 20 31 36 34 3a 20        case 164: 
27435 2f 2a 20 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72  /* cmd ::= inser
27436 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e  t_cmd INTO fulln
27437 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f  ame inscollist_o
27438 70 74 20 73 65 6c 65 63 74 20 2a 2f 0a 7b 73 71  pt select */.{sq
27439 6c 69 74 65 33 49 6e 73 65 72 74 28 70 50 61 72  lite3Insert(pPar
2743a 73 65 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  se, yymsp[-2].mi
2743b 6e 6f 72 2e 79 79 33 37 33 2c 20 30 2c 20 79 79  nor.yy373, 0, yy
2743c 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32  msp[0].minor.yy2
2743d 31 39 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  19, yymsp[-1].mi
2743e 6e 6f 72 2e 79 79 34 33 32 2c 20 79 79 6d 73 70  nor.yy432, yymsp
2743f 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 29  [-4].minor.yy46)
27440 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
27441 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 35  ;.      case 165
27442 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 69 6e 73  : /* cmd ::= ins
27443 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c  ert_cmd INTO ful
27444 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74  lname inscollist
27445 5f 6f 70 74 20 44 45 46 41 55 4c 54 20 56 41 4c  _opt DEFAULT VAL
27446 55 45 53 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 49  UES */.{sqlite3I
27447 6e 73 65 72 74 28 70 50 61 72 73 65 2c 20 79 79  nsert(pParse, yy
27448 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
27449 33 37 33 2c 20 30 2c 20 30 2c 20 79 79 6d 73 70  373, 0, 0, yymsp
2744a 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 33 32  [-2].minor.yy432
2744b 2c 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f  , yymsp[-5].mino
2744c 72 2e 79 79 34 36 29 3b 7d 0a 20 20 20 20 20 20  r.yy46);}.      
2744d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2744e 61 73 65 20 31 36 38 3a 20 2f 2a 20 69 74 65 6d  ase 168: /* item
2744f 6c 69 73 74 20 3a 3a 3d 20 69 74 65 6d 6c 69 73  list ::= itemlis
27450 74 20 43 4f 4d 4d 41 20 65 78 70 72 20 2a 2f 0a  t COMMA expr */.
27451 20 20 20 20 20 20 63 61 73 65 20 32 33 32 3a 20        case 232: 
27452 2f 2a 20 6e 65 78 70 72 6c 69 73 74 20 3a 3a 3d  /* nexprlist ::=
27453 20 6e 65 78 70 72 6c 69 73 74 20 43 4f 4d 4d 41   nexprlist COMMA
27454 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f   expr */.{yygoto
27455 6d 69 6e 6f 72 2e 79 79 31 37 34 20 3d 20 73 71  minor.yy174 = sq
27456 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
27457 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70  end(pParse,yymsp
27458 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34  [-2].minor.yy174
27459 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  ,yymsp[0].minor.
2745a 79 79 31 37 32 2c 30 29 3b 7d 0a 20 20 20 20 20  yy172,0);}.     
2745b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2745c 63 61 73 65 20 31 36 39 3a 20 2f 2a 20 69 74 65  case 169: /* ite
2745d 6d 6c 69 73 74 20 3a 3a 3d 20 65 78 70 72 20 2a  mlist ::= expr *
2745e 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 33  /.      case 233
2745f 3a 20 2f 2a 20 6e 65 78 70 72 6c 69 73 74 20 3a  : /* nexprlist :
27460 3a 3d 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f  := expr */.{yygo
27461 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 20 3d 20  tominor.yy174 = 
27462 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
27463 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30 2c 79  ppend(pParse,0,y
27464 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
27465 31 37 32 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20  172,0);}.       
27466 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
27467 73 65 20 31 37 32 3a 20 2f 2a 20 69 6e 73 63 6f  se 172: /* insco
27468 6c 6c 69 73 74 20 3a 3a 3d 20 69 6e 73 63 6f 6c  llist ::= inscol
27469 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 20 2a 2f  list COMMA nm */
2746a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
2746b 34 33 32 20 3d 20 73 71 6c 69 74 65 33 49 64 4c  432 = sqlite3IdL
2746c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
2746d 2d 3e 64 62 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d  ->db,yymsp[-2].m
2746e 69 6e 6f 72 2e 79 79 34 33 32 2c 26 79 79 6d 73  inor.yy432,&yyms
2746f 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30  p[0].minor.yy410
27470 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
27471 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 37  k;.      case 17
27472 33 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74  3: /* inscollist
27473 20 3a 3a 3d 20 6e 6d 20 2a 2f 0a 7b 79 79 67 6f   ::= nm */.{yygo
27474 74 6f 6d 69 6e 6f 72 2e 79 79 34 33 32 20 3d 20  tominor.yy432 = 
27475 73 71 6c 69 74 65 33 49 64 4c 69 73 74 41 70 70  sqlite3IdListApp
27476 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 30  end(pParse->db,0
27477 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
27478 2e 79 79 34 31 30 29 3b 7d 0a 20 20 20 20 20 20  .yy410);}.      
27479 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2747a 61 73 65 20 31 37 35 3a 20 2f 2a 20 65 78 70 72  ase 175: /* expr
2747b 20 3a 3a 3d 20 4c 50 20 65 78 70 72 20 52 50 20   ::= LP expr RP 
2747c 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2747d 79 79 31 37 32 20 3d 20 79 79 6d 73 70 5b 2d 31  yy172 = yymsp[-1
2747e 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 3b 20 73  ].minor.yy172; s
2747f 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 79  qlite3ExprSpan(y
27480 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32  ygotominor.yy172
27481 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  ,&yymsp[-2].mino
27482 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e  r.yy0,&yymsp[0].
27483 6d 69 6e 6f 72 2e 79 79 30 29 3b 20 7d 0a 20 20  minor.yy0); }.  
27484 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
27485 20 20 20 63 61 73 65 20 31 37 36 3a 20 2f 2a 20     case 176: /* 
27486 74 65 72 6d 20 3a 3a 3d 20 4e 55 4c 4c 20 2a 2f  term ::= NULL */
27487 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 31 3a  .      case 181:
27488 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20 49 4e 54   /* term ::= INT
27489 45 47 45 52 7c 46 4c 4f 41 54 7c 42 4c 4f 42 20  EGER|FLOAT|BLOB 
2748a 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 38  */.      case 18
2748b 32 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20 53  2: /* term ::= S
2748c 54 52 49 4e 47 20 2a 2f 0a 7b 79 79 67 6f 74 6f  TRING */.{yygoto
2748d 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71  minor.yy172 = sq
2748e 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2748f 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f  e, yymsp[0].majo
27490 72 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b  r, 0, 0, &yymsp[
27491 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a  0].minor.yy0);}.
27492 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
27493 20 20 20 20 20 63 61 73 65 20 31 37 37 3a 20 2f       case 177: /
27494 2a 20 65 78 70 72 20 3a 3a 3d 20 49 44 20 2a 2f  * expr ::= ID */
27495 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 38 3a  .      case 178:
27496 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 4a 4f 49   /* expr ::= JOI
27497 4e 5f 4b 57 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  N_KW */.{yygotom
27498 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71 6c  inor.yy172 = sql
27499 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2749a 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26  , TK_ID, 0, 0, &
2749b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2749c 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  y0);}.        br
2749d 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2749e 31 37 39 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  179: /* expr ::=
2749f 20 6e 6d 20 44 4f 54 20 6e 6d 20 2a 2f 0a 7b 0a   nm DOT nm */.{.
274a0 20 20 45 78 70 72 20 2a 74 65 6d 70 31 20 3d 20    Expr *temp1 = 
274a1 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
274a2 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30  rse, TK_ID, 0, 0
274a3 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  , &yymsp[-2].min
274a4 6f 72 2e 79 79 34 31 30 29 3b 0a 20 20 45 78 70  or.yy410);.  Exp
274a5 72 20 2a 74 65 6d 70 32 20 3d 20 73 71 6c 69 74  r *temp2 = sqlit
274a6 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
274a7 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79  TK_ID, 0, 0, &yy
274a8 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34  msp[0].minor.yy4
274a9 31 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  10);.  yygotomin
274aa 6f 72 2e 79 79 31 37 32 20 3d 20 73 71 6c 69 74  or.yy172 = sqlit
274ab 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
274ac 54 4b 5f 44 4f 54 2c 20 74 65 6d 70 31 2c 20 74  TK_DOT, temp1, t
274ad 65 6d 70 32 2c 20 30 29 3b 0a 7d 0a 20 20 20 20  emp2, 0);.}.    
274ae 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
274af 20 63 61 73 65 20 31 38 30 3a 20 2f 2a 20 65 78   case 180: /* ex
274b0 70 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d  pr ::= nm DOT nm
274b1 20 44 4f 54 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 45   DOT nm */.{.  E
274b2 78 70 72 20 2a 74 65 6d 70 31 20 3d 20 73 71 6c  xpr *temp1 = sql
274b3 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
274b4 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26  , TK_ID, 0, 0, &
274b5 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
274b6 79 79 34 31 30 29 3b 0a 20 20 45 78 70 72 20 2a  yy410);.  Expr *
274b7 74 65 6d 70 32 20 3d 20 73 71 6c 69 74 65 33 50  temp2 = sqlite3P
274b8 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
274b9 49 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70  ID, 0, 0, &yymsp
274ba 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30  [-2].minor.yy410
274bb 29 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70 33  );.  Expr *temp3
274bc 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
274bd 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30  pParse, TK_ID, 0
274be 2c 20 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  , 0, &yymsp[0].m
274bf 69 6e 6f 72 2e 79 79 34 31 30 29 3b 0a 20 20 45  inor.yy410);.  E
274c0 78 70 72 20 2a 74 65 6d 70 34 20 3d 20 73 71 6c  xpr *temp4 = sql
274c1 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
274c2 2c 20 54 4b 5f 44 4f 54 2c 20 74 65 6d 70 32 2c  , TK_DOT, temp2,
274c3 20 74 65 6d 70 33 2c 20 30 29 3b 0a 20 20 79 79   temp3, 0);.  yy
274c4 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20  gotominor.yy172 
274c5 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
274c6 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 74  Parse, TK_DOT, t
274c7 65 6d 70 31 2c 20 74 65 6d 70 34 2c 20 30 29 3b  emp1, temp4, 0);
274c8 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
274c9 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 33  ;.      case 183
274ca 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 52 45  : /* expr ::= RE
274cb 47 49 53 54 45 52 20 2a 2f 0a 7b 79 79 67 6f 74  GISTER */.{yygot
274cc 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73  ominor.yy172 = s
274cd 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 45 78  qlite3RegisterEx
274ce 70 72 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73  pr(pParse, &yyms
274cf 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
274d0 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
274d1 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 34 3a  .      case 184:
274d2 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 56 41 52   /* expr ::= VAR
274d3 49 41 42 4c 45 20 2a 2f 0a 7b 0a 20 20 54 6f 6b  IABLE */.{.  Tok
274d4 65 6e 20 2a 70 54 6f 6b 65 6e 20 3d 20 26 79 79  en *pToken = &yy
274d5 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
274d6 3b 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20  ;.  Expr *pExpr 
274d7 3d 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  = yygotominor.yy
274d8 31 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78  172 = sqlite3PEx
274d9 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 56 41  pr(pParse, TK_VA
274da 52 49 41 42 4c 45 2c 20 30 2c 20 30 2c 20 70 54  RIABLE, 0, 0, pT
274db 6f 6b 65 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33  oken);.  sqlite3
274dc 45 78 70 72 41 73 73 69 67 6e 56 61 72 4e 75 6d  ExprAssignVarNum
274dd 62 65 72 28 70 50 61 72 73 65 2c 20 70 45 78 70  ber(pParse, pExp
274de 72 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  r);.}.        br
274df 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
274e0 31 38 35 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  185: /* expr ::=
274e1 20 65 78 70 72 20 43 4f 4c 4c 41 54 45 20 69 64   expr COLLATE id
274e2 73 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d  s */.{.  yygotom
274e3 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71 6c  inor.yy172 = sql
274e4 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28  ite3ExprSetColl(
274e5 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 32  pParse, yymsp[-2
274e6 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 26  ].minor.yy172, &
274e7 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
274e8 79 34 31 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20  y410);.}.       
274e9 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
274ea 73 65 20 31 38 36 3a 20 2f 2a 20 65 78 70 72 20  se 186: /* expr 
274eb 3a 3a 3d 20 43 41 53 54 20 4c 50 20 65 78 70 72  ::= CAST LP expr
274ec 20 41 53 20 74 79 70 65 74 6f 6b 65 6e 20 52 50   AS typetoken RP
274ed 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69   */.{.  yygotomi
274ee 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71 6c 69  nor.yy172 = sqli
274ef 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
274f0 20 54 4b 5f 43 41 53 54 2c 20 79 79 6d 73 70 5b   TK_CAST, yymsp[
274f1 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c  -3].minor.yy172,
274f2 20 30 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d   0, &yymsp[-1].m
274f3 69 6e 6f 72 2e 79 79 34 31 30 29 3b 0a 20 20 73  inor.yy410);.  s
274f4 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 79  qlite3ExprSpan(y
274f5 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32  ygotominor.yy172
274f6 2c 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f  ,&yymsp[-5].mino
274f7 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e  r.yy0,&yymsp[0].
274f8 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20  minor.yy0);.}.  
274f9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
274fa 20 20 20 63 61 73 65 20 31 38 37 3a 20 2f 2a 20     case 187: /* 
274fb 65 78 70 72 20 3a 3a 3d 20 49 44 20 4c 50 20 64  expr ::= ID LP d
274fc 69 73 74 69 6e 63 74 20 65 78 70 72 6c 69 73 74  istinct exprlist
274fd 20 52 50 20 2a 2f 0a 7b 0a 20 20 69 66 28 20 79   RP */.{.  if( y
274fe 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
274ff 79 31 37 34 20 26 26 20 79 79 6d 73 70 5b 2d 31  y174 && yymsp[-1
27500 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 2d 3e 6e  ].minor.yy174->n
27501 45 78 70 72 3e 53 51 4c 49 54 45 5f 4d 41 58 5f  Expr>SQLITE_MAX_
27502 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 29 7b 0a  FUNCTION_ARG ){.
27503 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
27504 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f  Msg(pParse, "too
27505 20 6d 61 6e 79 20 61 72 67 75 6d 65 6e 74 73 20   many arguments 
27506 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 25 54 22 2c  on function %T",
27507 20 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f   &yymsp[-4].mino
27508 72 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20 79 79  r.yy0);.  }.  yy
27509 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20  gotominor.yy172 
2750a 3d 20 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e  = sqlite3ExprFun
2750b 63 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 79 79  ction(pParse, yy
2750c 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2750d 31 37 34 2c 20 26 79 79 6d 73 70 5b 2d 34 5d 2e  174, &yymsp[-4].
2750e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71  minor.yy0);.  sq
2750f 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 79 79  lite3ExprSpan(yy
27510 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2c  gotominor.yy172,
27511 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  &yymsp[-4].minor
27512 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  .yy0,&yymsp[0].m
27513 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 69 66 28  inor.yy0);.  if(
27514 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
27515 2e 79 79 34 36 20 26 26 20 79 79 67 6f 74 6f 6d  .yy46 && yygotom
27516 69 6e 6f 72 2e 79 79 31 37 32 20 29 7b 0a 20 20  inor.yy172 ){.  
27517 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
27518 31 37 32 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50  172->flags |= EP
27519 5f 44 69 73 74 69 6e 63 74 3b 0a 20 20 7d 0a 7d  _Distinct;.  }.}
2751a 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2751b 20 20 20 20 20 20 63 61 73 65 20 31 38 38 3a 20        case 188: 
2751c 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 49 44 20 4c  /* expr ::= ID L
2751d 50 20 53 54 41 52 20 52 50 20 2a 2f 0a 7b 0a 20  P STAR RP */.{. 
2751e 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
2751f 37 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  72 = sqlite3Expr
27520 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2c  Function(pParse,
27521 20 30 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d   0, &yymsp[-3].m
27522 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71 6c  inor.yy0);.  sql
27523 69 74 65 33 45 78 70 72 53 70 61 6e 28 79 79 67  ite3ExprSpan(yyg
27524 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 26  otominor.yy172,&
27525 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
27526 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  yy0,&yymsp[0].mi
27527 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20  nor.yy0);.}.    
27528 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
27529 20 63 61 73 65 20 31 38 39 3a 20 2f 2a 20 74 65   case 189: /* te
2752a 72 6d 20 3a 3a 3d 20 43 54 49 4d 45 5f 4b 57 20  rm ::= CTIME_KW 
2752b 2a 2f 0a 7b 0a 20 20 2f 2a 20 54 68 65 20 43 55  */.{.  /* The CU
2752c 52 52 45 4e 54 5f 54 49 4d 45 2c 20 43 55 52 52  RRENT_TIME, CURR
2752d 45 4e 54 5f 44 41 54 45 2c 20 61 6e 64 20 43 55  ENT_DATE, and CU
2752e 52 52 45 4e 54 5f 54 49 4d 45 53 54 41 4d 50 20  RRENT_TIMESTAMP 
2752f 76 61 6c 75 65 73 20 61 72 65 0a 20 20 2a 2a 20  values are.  ** 
27530 74 72 65 61 74 65 64 20 61 73 20 66 75 6e 63 74  treated as funct
27531 69 6f 6e 73 20 74 68 61 74 20 72 65 74 75 72 6e  ions that return
27532 20 63 6f 6e 73 74 61 6e 74 73 20 2a 2f 0a 20 20   constants */.  
27533 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37  yygotominor.yy17
27534 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 46  2 = sqlite3ExprF
27535 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  unction(pParse, 
27536 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
27537 72 2e 79 79 30 29 3b 0a 20 20 69 66 28 20 79 79  r.yy0);.  if( yy
27538 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20  gotominor.yy172 
27539 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  ){.    yygotomin
2753a 6f 72 2e 79 79 31 37 32 2d 3e 6f 70 20 3d 20 54  or.yy172->op = T
2753b 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3b 20 20 0a  K_CONST_FUNC;  .
2753c 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
2753d 79 79 31 37 32 2d 3e 73 70 61 6e 20 3d 20 79 79  yy172->span = yy
2753e 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
2753f 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20 20 20 20 20  ;.  }.}.        
27540 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
27541 65 20 31 39 30 3a 20 2f 2a 20 65 78 70 72 20 3a  e 190: /* expr :
27542 3a 3d 20 65 78 70 72 20 41 4e 44 20 65 78 70 72  := expr AND expr
27543 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
27544 39 31 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  91: /* expr ::= 
27545 65 78 70 72 20 4f 52 20 65 78 70 72 20 2a 2f 0a  expr OR expr */.
27546 20 20 20 20 20 20 63 61 73 65 20 31 39 32 3a 20        case 192: 
27547 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  /* expr ::= expr
27548 20 4c 54 7c 47 54 7c 47 45 7c 4c 45 20 65 78 70   LT|GT|GE|LE exp
27549 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  r */.      case 
2754a 31 39 33 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  193: /* expr ::=
2754b 20 65 78 70 72 20 45 51 7c 4e 45 20 65 78 70 72   expr EQ|NE expr
2754c 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
2754d 39 34 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  94: /* expr ::= 
2754e 65 78 70 72 20 42 49 54 41 4e 44 7c 42 49 54 4f  expr BITAND|BITO
2754f 52 7c 4c 53 48 49 46 54 7c 52 53 48 49 46 54 20  R|LSHIFT|RSHIFT 
27550 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61  expr */.      ca
27551 73 65 20 31 39 35 3a 20 2f 2a 20 65 78 70 72 20  se 195: /* expr 
27552 3a 3a 3d 20 65 78 70 72 20 50 4c 55 53 7c 4d 49  ::= expr PLUS|MI
27553 4e 55 53 20 65 78 70 72 20 2a 2f 0a 20 20 20 20  NUS expr */.    
27554 20 20 63 61 73 65 20 31 39 36 3a 20 2f 2a 20 65    case 196: /* e
27555 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 53 54 41  xpr ::= expr STA
27556 52 7c 53 4c 41 53 48 7c 52 45 4d 20 65 78 70 72  R|SLASH|REM expr
27557 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
27558 39 37 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  97: /* expr ::= 
27559 65 78 70 72 20 43 4f 4e 43 41 54 20 65 78 70 72  expr CONCAT expr
2755a 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2755b 2e 79 79 31 37 32 20 3d 20 73 71 6c 69 74 65 33  .yy172 = sqlite3
2755c 50 45 78 70 72 28 70 50 61 72 73 65 2c 79 79 6d  PExpr(pParse,yym
2755d 73 70 5b 2d 31 5d 2e 6d 61 6a 6f 72 2c 79 79 6d  sp[-1].major,yym
2755e 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-2].minor.yy1
2755f 37 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  72,yymsp[0].mino
27560 72 2e 79 79 31 37 32 2c 30 29 3b 7d 0a 20 20 20  r.yy172,0);}.   
27561 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
27562 20 20 63 61 73 65 20 31 39 38 3a 20 2f 2a 20 6c    case 198: /* l
27563 69 6b 65 6f 70 20 3a 3a 3d 20 4c 49 4b 45 5f 4b  ikeop ::= LIKE_K
27564 57 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  W */.      case 
27565 32 30 30 3a 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a  200: /* likeop :
27566 3a 3d 20 4d 41 54 43 48 20 2a 2f 0a 7b 79 79 67  := MATCH */.{yyg
27567 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2e 65 4f  otominor.yy72.eO
27568 70 65 72 61 74 6f 72 20 3d 20 79 79 6d 73 70 5b  perator = yymsp[
27569 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 20 79 79  0].minor.yy0; yy
2756a 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2e 6e  gotominor.yy72.n
2756b 6f 74 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20  ot = 0;}.       
2756c 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2756d 73 65 20 31 39 39 3a 20 2f 2a 20 6c 69 6b 65 6f  se 199: /* likeo
2756e 70 20 3a 3a 3d 20 4e 4f 54 20 4c 49 4b 45 5f 4b  p ::= NOT LIKE_K
2756f 57 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  W */.      case 
27570 32 30 31 3a 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a  201: /* likeop :
27571 3a 3d 20 4e 4f 54 20 4d 41 54 43 48 20 2a 2f 0a  := NOT MATCH */.
27572 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37  {yygotominor.yy7
27573 32 2e 65 4f 70 65 72 61 74 6f 72 20 3d 20 79 79  2.eOperator = yy
27574 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
27575 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ; yygotominor.yy
27576 37 32 2e 6e 6f 74 20 3d 20 31 3b 7d 0a 20 20 20  72.not = 1;}.   
27577 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
27578 20 20 63 61 73 65 20 32 30 34 3a 20 2f 2a 20 65    case 204: /* e
27579 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 6c 69 6b  xpr ::= expr lik
2757a 65 6f 70 20 65 78 70 72 20 65 73 63 61 70 65 20  eop expr escape 
2757b 2a 2f 0a 7b 0a 20 20 45 78 70 72 4c 69 73 74 20  */.{.  ExprList 
2757c 2a 70 4c 69 73 74 3b 0a 20 20 70 4c 69 73 74 20  *pList;.  pList 
2757d 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
2757e 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30  tAppend(pParse,0
2757f 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
27580 72 2e 79 79 31 37 32 2c 20 30 29 3b 0a 20 20 70  r.yy172, 0);.  p
27581 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78  List = sqlite3Ex
27582 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
27583 72 73 65 2c 70 4c 69 73 74 2c 20 79 79 6d 73 70  rse,pList, yymsp
27584 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32  [-3].minor.yy172
27585 2c 20 30 29 3b 0a 20 20 69 66 28 20 79 79 6d 73  , 0);.  if( yyms
27586 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32  p[0].minor.yy172
27587 20 29 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20   ){.    pList = 
27588 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
27589 70 70 65 6e 64 28 70 50 61 72 73 65 2c 70 4c 69  ppend(pParse,pLi
2758a 73 74 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  st, yymsp[0].min
2758b 6f 72 2e 79 79 31 37 32 2c 20 30 29 3b 0a 20 20  or.yy172, 0);.  
2758c 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  }.  yygotominor.
2758d 79 79 31 37 32 20 3d 20 73 71 6c 69 74 65 33 45  yy172 = sqlite3E
2758e 78 70 72 46 75 6e 63 74 69 6f 6e 28 70 50 61 72  xprFunction(pPar
2758f 73 65 2c 20 70 4c 69 73 74 2c 20 26 79 79 6d 73  se, pList, &yyms
27590 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32  p[-2].minor.yy72
27591 2e 65 4f 70 65 72 61 74 6f 72 29 3b 0a 20 20 69  .eOperator);.  i
27592 66 28 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  f( yymsp[-2].min
27593 6f 72 2e 79 79 37 32 2e 6e 6f 74 20 29 20 79 79  or.yy72.not ) yy
27594 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20  gotominor.yy172 
27595 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
27596 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79  Parse, TK_NOT, y
27597 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32  ygotominor.yy172
27598 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  , 0, 0);.  sqlit
27599 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74  e3ExprSpan(yygot
2759a 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 26 79  ominor.yy172, &y
2759b 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
2759c 79 31 37 32 2d 3e 73 70 61 6e 2c 20 26 79 79 6d  y172->span, &yym
2759d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
2759e 37 32 2d 3e 73 70 61 6e 29 3b 0a 20 20 69 66 28  72->span);.  if(
2759f 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
275a0 37 32 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  72 ) yygotominor
275a1 2e 79 79 31 37 32 2d 3e 66 6c 61 67 73 20 7c 3d  .yy172->flags |=
275a2 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 3b 0a 7d   EP_InfixFunc;.}
275a3 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
275a4 20 20 20 20 20 20 63 61 73 65 20 32 30 35 3a 20        case 205: 
275a5 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  /* expr ::= expr
275a6 20 49 53 4e 55 4c 4c 7c 4e 4f 54 4e 55 4c 4c 20   ISNULL|NOTNULL 
275a7 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  */.{.  yygotomin
275a8 6f 72 2e 79 79 31 37 32 20 3d 20 73 71 6c 69 74  or.yy172 = sqlit
275a9 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
275aa 79 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72 2c 20  yymsp[0].major, 
275ab 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
275ac 79 79 31 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20  yy172, 0, 0);.  
275ad 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28  sqlite3ExprSpan(
275ae 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37  yygotominor.yy17
275af 32 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  2,&yymsp[-1].min
275b0 6f 72 2e 79 79 31 37 32 2d 3e 73 70 61 6e 2c 26  or.yy172->span,&
275b1 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
275b2 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  y0);.}.        b
275b3 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
275b4 20 32 30 36 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   206: /* expr ::
275b5 3d 20 65 78 70 72 20 49 53 20 4e 55 4c 4c 20 2a  = expr IS NULL *
275b6 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
275b7 72 2e 79 79 31 37 32 20 3d 20 73 71 6c 69 74 65  r.yy172 = sqlite
275b8 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
275b9 4b 5f 49 53 4e 55 4c 4c 2c 20 79 79 6d 73 70 5b  K_ISNULL, yymsp[
275ba 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c  -2].minor.yy172,
275bb 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65   0, 0);.  sqlite
275bc 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f  3ExprSpan(yygoto
275bd 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 26 79 79 6d  minor.yy172,&yym
275be 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-2].minor.yy1
275bf 37 32 2d 3e 73 70 61 6e 2c 26 79 79 6d 73 70 5b  72->span,&yymsp[
275c0 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d  0].minor.yy0);.}
275c1 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
275c2 20 20 20 20 20 20 63 61 73 65 20 32 30 37 3a 20        case 207: 
275c3 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  /* expr ::= expr
275c4 20 4e 4f 54 20 4e 55 4c 4c 20 2a 2f 0a 7b 0a 20   NOT NULL */.{. 
275c5 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
275c6 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  72 = sqlite3PExp
275c7 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54  r(pParse, TK_NOT
275c8 4e 55 4c 4c 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e  NULL, yymsp[-2].
275c9 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 30 2c 20  minor.yy172, 0, 
275ca 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  0);.  sqlite3Exp
275cb 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f  rSpan(yygotomino
275cc 72 2e 79 79 31 37 32 2c 26 79 79 6d 73 70 5b 2d  r.yy172,&yymsp[-
275cd 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2d 3e  2].minor.yy172->
275ce 73 70 61 6e 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  span,&yymsp[0].m
275cf 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20  inor.yy0);.}.   
275d0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
275d1 20 20 63 61 73 65 20 32 30 38 3a 20 2f 2a 20 65    case 208: /* e
275d2 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 20  xpr ::= expr IS 
275d3 4e 4f 54 20 4e 55 4c 4c 20 2a 2f 0a 7b 0a 20 20  NOT NULL */.{.  
275d4 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37  yygotominor.yy17
275d5 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  2 = sqlite3PExpr
275d6 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 4e  (pParse, TK_NOTN
275d7 55 4c 4c 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d  ULL, yymsp[-3].m
275d8 69 6e 6f 72 2e 79 79 31 37 32 2c 20 30 2c 20 30  inor.yy172, 0, 0
275d9 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
275da 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72  Span(yygotominor
275db 2e 79 79 31 37 32 2c 26 79 79 6d 73 70 5b 2d 33  .yy172,&yymsp[-3
275dc 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2d 3e 73  ].minor.yy172->s
275dd 70 61 6e 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  pan,&yymsp[0].mi
275de 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20  nor.yy0);.}.    
275df 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
275e0 20 63 61 73 65 20 32 30 39 3a 20 2f 2a 20 65 78   case 209: /* ex
275e1 70 72 20 3a 3a 3d 20 4e 4f 54 20 65 78 70 72 20  pr ::= NOT expr 
275e2 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 31  */.      case 21
275e3 30 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 42  0: /* expr ::= B
275e4 49 54 4e 4f 54 20 65 78 70 72 20 2a 2f 0a 7b 0a  ITNOT expr */.{.
275e5 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
275e6 31 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78  172 = sqlite3PEx
275e7 70 72 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  pr(pParse, yymsp
275e8 5b 2d 31 5d 2e 6d 61 6a 6f 72 2c 20 79 79 6d 73  [-1].major, yyms
275e9 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32  p[0].minor.yy172
275ea 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  , 0, 0);.  sqlit
275eb 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74  e3ExprSpan(yygot
275ec 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 26 79 79  ominor.yy172,&yy
275ed 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
275ee 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
275ef 72 2e 79 79 31 37 32 2d 3e 73 70 61 6e 29 3b 0a  r.yy172->span);.
275f0 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
275f1 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 31 3a  .      case 211:
275f2 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 4d 49 4e   /* expr ::= MIN
275f3 55 53 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 79  US expr */.{.  y
275f4 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32  ygotominor.yy172
275f5 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
275f6 70 50 61 72 73 65 2c 20 54 4b 5f 55 4d 49 4e 55  pParse, TK_UMINU
275f7 53 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  S, yymsp[0].mino
275f8 72 2e 79 79 31 37 32 2c 20 30 2c 20 30 29 3b 0a  r.yy172, 0, 0);.
275f9 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61    sqlite3ExprSpa
275fa 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  n(yygotominor.yy
275fb 31 37 32 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  172,&yymsp[-1].m
275fc 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
275fd 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2d 3e  0].minor.yy172->
275fe 73 70 61 6e 29 3b 0a 7d 0a 20 20 20 20 20 20 20  span);.}.       
275ff 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
27600 73 65 20 32 31 32 3a 20 2f 2a 20 65 78 70 72 20  se 212: /* expr 
27601 3a 3a 3d 20 50 4c 55 53 20 65 78 70 72 20 2a 2f  ::= PLUS expr */
27602 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .{.  yygotominor
27603 2e 79 79 31 37 32 20 3d 20 73 71 6c 69 74 65 33  .yy172 = sqlite3
27604 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
27605 5f 55 50 4c 55 53 2c 20 79 79 6d 73 70 5b 30 5d  _UPLUS, yymsp[0]
27606 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 30 2c  .minor.yy172, 0,
27607 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78   0);.  sqlite3Ex
27608 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e  prSpan(yygotomin
27609 6f 72 2e 79 79 31 37 32 2c 26 79 79 6d 73 70 5b  or.yy172,&yymsp[
2760a 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -1].minor.yy0,&y
2760b 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2760c 31 37 32 2d 3e 73 70 61 6e 29 3b 0a 7d 0a 20 20  172->span);.}.  
2760d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2760e 20 20 20 63 61 73 65 20 32 31 35 3a 20 2f 2a 20     case 215: /* 
2760f 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 62 65  expr ::= expr be
27610 74 77 65 65 6e 5f 6f 70 20 65 78 70 72 20 41 4e  tween_op expr AN
27611 44 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 45 78  D expr */.{.  Ex
27612 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20  prList *pList = 
27613 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
27614 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30 2c 20  ppend(pParse,0, 
27615 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
27616 79 79 31 37 32 2c 20 30 29 3b 0a 20 20 70 4c 69  yy172, 0);.  pLi
27617 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
27618 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
27619 65 2c 70 4c 69 73 74 2c 20 79 79 6d 73 70 5b 30  e,pList, yymsp[0
2761a 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 30  ].minor.yy172, 0
2761b 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  );.  yygotominor
2761c 2e 79 79 31 37 32 20 3d 20 73 71 6c 69 74 65 33  .yy172 = sqlite3
2761d 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2761e 5f 42 45 54 57 45 45 4e 2c 20 79 79 6d 73 70 5b  _BETWEEN, yymsp[
2761f 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c  -4].minor.yy172,
27620 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 79 79   0, 0);.  if( yy
27621 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20  gotominor.yy172 
27622 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  ){.    yygotomin
27623 6f 72 2e 79 79 31 37 32 2d 3e 70 4c 69 73 74 20  or.yy172->pList 
27624 3d 20 70 4c 69 73 74 3b 0a 20 20 7d 65 6c 73 65  = pList;.  }else
27625 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  {.    sqlite3Exp
27626 72 4c 69 73 74 44 65 6c 65 74 65 28 70 4c 69 73  rListDelete(pLis
27627 74 29 3b 0a 20 20 7d 20 0a 20 20 69 66 28 20 79  t);.  } .  if( y
27628 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
27629 79 34 36 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f  y46 ) yygotomino
2762a 72 2e 79 79 31 37 32 20 3d 20 73 71 6c 69 74 65  r.yy172 = sqlite
2762b 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
2762c 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e  K_NOT, yygotomin
2762d 6f 72 2e 79 79 31 37 32 2c 20 30 2c 20 30 29 3b  or.yy172, 0, 0);
2762e 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70  .  sqlite3ExprSp
2762f 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  an(yygotominor.y
27630 79 31 37 32 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e  y172,&yymsp[-4].
27631 6d 69 6e 6f 72 2e 79 79 31 37 32 2d 3e 73 70 61  minor.yy172->spa
27632 6e 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  n,&yymsp[0].mino
27633 72 2e 79 79 31 37 32 2d 3e 73 70 61 6e 29 3b 0a  r.yy172->span);.
27634 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
27635 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 38 3a  .      case 218:
27636 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70   /* expr ::= exp
27637 72 20 69 6e 5f 6f 70 20 4c 50 20 65 78 70 72 6c  r in_op LP exprl
27638 69 73 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 20 20  ist RP */.{.    
27639 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37  yygotominor.yy17
2763a 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  2 = sqlite3PExpr
2763b 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20  (pParse, TK_IN, 
2763c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
2763d 79 79 31 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20  yy172, 0, 0);.  
2763e 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
2763f 72 2e 79 79 31 37 32 20 29 7b 0a 20 20 20 20 20  r.yy172 ){.     
27640 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
27641 37 32 2d 3e 70 4c 69 73 74 20 3d 20 79 79 6d 73  72->pList = yyms
27642 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37  p[-1].minor.yy17
27643 34 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  4;.      sqlite3
27644 45 78 70 72 53 65 74 48 65 69 67 68 74 28 79 79  ExprSetHeight(yy
27645 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 29  gotominor.yy172)
27646 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
27647 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
27648 73 74 44 65 6c 65 74 65 28 79 79 6d 73 70 5b 2d  stDelete(yymsp[-
27649 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 29 3b  1].minor.yy174);
2764a 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 79  .    }.    if( y
2764b 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
2764c 79 34 36 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f  y46 ) yygotomino
2764d 72 2e 79 79 31 37 32 20 3d 20 73 71 6c 69 74 65  r.yy172 = sqlite
2764e 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
2764f 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e  K_NOT, yygotomin
27650 6f 72 2e 79 79 31 37 32 2c 20 30 2c 20 30 29 3b  or.yy172, 0, 0);
27651 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
27652 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72  Span(yygotominor
27653 2e 79 79 31 37 32 2c 26 79 79 6d 73 70 5b 2d 34  .yy172,&yymsp[-4
27654 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2d 3e 73  ].minor.yy172->s
27655 70 61 6e 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  pan,&yymsp[0].mi
27656 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20  nor.yy0);.  }.  
27657 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
27658 20 20 20 63 61 73 65 20 32 31 39 3a 20 2f 2a 20     case 219: /* 
27659 65 78 70 72 20 3a 3a 3d 20 4c 50 20 73 65 6c 65  expr ::= LP sele
2765a 63 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 20 20 79  ct RP */.{.    y
2765b 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32  ygotominor.yy172
2765c 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
2765d 70 50 61 72 73 65 2c 20 54 4b 5f 53 45 4c 45 43  pParse, TK_SELEC
2765e 54 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20  T, 0, 0, 0);.   
2765f 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72   if( yygotominor
27660 2e 79 79 31 37 32 20 29 7b 0a 20 20 20 20 20 20  .yy172 ){.      
27661 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37  yygotominor.yy17
27662 32 2d 3e 70 53 65 6c 65 63 74 20 3d 20 79 79 6d  2->pSelect = yym
27663 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32  sp[-1].minor.yy2
27664 31 39 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  19;.      sqlite
27665 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28 79  3ExprSetHeight(y
27666 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32  ygotominor.yy172
27667 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
27668 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
27669 74 44 65 6c 65 74 65 28 79 79 6d 73 70 5b 2d 31  tDelete(yymsp[-1
2766a 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31 39 29 3b 0a  ].minor.yy219);.
2766b 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2766c 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f  3ExprSpan(yygoto
2766d 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 26 79 79 6d  minor.yy172,&yym
2766e 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
2766f 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
27670 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20 20 20 20  .yy0);.  }.     
27671 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
27672 63 61 73 65 20 32 32 30 3a 20 2f 2a 20 65 78 70  case 220: /* exp
27673 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70  r ::= expr in_op
27674 20 4c 50 20 73 65 6c 65 63 74 20 52 50 20 2a 2f   LP select RP */
27675 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  .{.    yygotomin
27676 6f 72 2e 79 79 31 37 32 20 3d 20 73 71 6c 69 74  or.yy172 = sqlit
27677 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
27678 54 4b 5f 49 4e 2c 20 79 79 6d 73 70 5b 2d 34 5d  TK_IN, yymsp[-4]
27679 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 30 2c  .minor.yy172, 0,
2767a 20 30 29 3b 0a 20 20 20 20 69 66 28 20 79 79 67   0);.    if( yyg
2767b 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 29  otominor.yy172 )
2767c 7b 0a 20 20 20 20 20 20 79 79 67 6f 74 6f 6d 69  {.      yygotomi
2767d 6e 6f 72 2e 79 79 31 37 32 2d 3e 70 53 65 6c 65  nor.yy172->pSele
2767e 63 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  ct = yymsp[-1].m
2767f 69 6e 6f 72 2e 79 79 32 31 39 3b 0a 20 20 20 20  inor.yy219;.    
27680 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74    sqlite3ExprSet
27681 48 65 69 67 68 74 28 79 79 67 6f 74 6f 6d 69 6e  Height(yygotomin
27682 6f 72 2e 79 79 31 37 32 29 3b 0a 20 20 20 20 7d  or.yy172);.    }
27683 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
27684 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
27685 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
27686 79 79 32 31 39 29 3b 0a 20 20 20 20 7d 0a 20 20  yy219);.    }.  
27687 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 33 5d 2e    if( yymsp[-3].
27688 6d 69 6e 6f 72 2e 79 79 34 36 20 29 20 79 79 67  minor.yy46 ) yyg
27689 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d  otominor.yy172 =
2768a 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2768b 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79  arse, TK_NOT, yy
2768c 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2c  gotominor.yy172,
2768d 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
2768e 74 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f  te3ExprSpan(yygo
2768f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 26 79  tominor.yy172,&y
27690 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
27691 79 31 37 32 2d 3e 73 70 61 6e 2c 26 79 79 6d 73  y172->span,&yyms
27692 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
27693 0a 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  .  }.        bre
27694 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
27695 32 31 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  21: /* expr ::= 
27696 65 78 70 72 20 69 6e 5f 6f 70 20 6e 6d 20 64 62  expr in_op nm db
27697 6e 6d 20 2a 2f 0a 7b 0a 20 20 20 20 53 72 63 4c  nm */.{.    SrcL
27698 69 73 74 20 2a 70 53 72 63 20 3d 20 73 71 6c 69  ist *pSrc = sqli
27699 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
2769a 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 30 2c 26  (pParse->db, 0,&
2769b 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2769c 79 79 34 31 30 2c 26 79 79 6d 73 70 5b 30 5d 2e  yy410,&yymsp[0].
2769d 6d 69 6e 6f 72 2e 79 79 34 31 30 29 3b 0a 20 20  minor.yy410);.  
2769e 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
2769f 31 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78  172 = sqlite3PEx
276a0 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e  pr(pParse, TK_IN
276a1 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  , yymsp[-3].mino
276a2 72 2e 79 79 31 37 32 2c 20 30 2c 20 30 29 3b 0a  r.yy172, 0, 0);.
276a3 20 20 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69      if( yygotomi
276a4 6e 6f 72 2e 79 79 31 37 32 20 29 7b 0a 20 20 20  nor.yy172 ){.   
276a5 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
276a6 79 31 37 32 2d 3e 70 53 65 6c 65 63 74 20 3d 20  y172->pSelect = 
276a7 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77  sqlite3SelectNew
276a8 28 70 50 61 72 73 65 2c 20 30 2c 70 53 72 63 2c  (pParse, 0,pSrc,
276a9 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 29 3b 0a  0,0,0,0,0,0,0);.
276aa 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
276ab 72 53 65 74 48 65 69 67 68 74 28 79 79 67 6f 74  rSetHeight(yygot
276ac 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 29 3b 0a 20  ominor.yy172);. 
276ad 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
276ae 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65  sqlite3SrcListDe
276af 6c 65 74 65 28 70 53 72 63 29 3b 0a 20 20 20 20  lete(pSrc);.    
276b0 7d 0a 20 20 20 20 69 66 28 20 79 79 6d 73 70 5b  }.    if( yymsp[
276b1 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 20 29  -2].minor.yy46 )
276b2 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
276b3 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  72 = sqlite3PExp
276b4 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54  r(pParse, TK_NOT
276b5 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  , yygotominor.yy
276b6 31 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  172, 0, 0);.    
276b7 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28  sqlite3ExprSpan(
276b8 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37  yygotominor.yy17
276b9 32 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  2,&yymsp[-3].min
276ba 6f 72 2e 79 79 31 37 32 2d 3e 73 70 61 6e 2c 79  or.yy172->span,y
276bb 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
276bc 34 31 30 2e 7a 3f 26 79 79 6d 73 70 5b 30 5d 2e  410.z?&yymsp[0].
276bd 6d 69 6e 6f 72 2e 79 79 34 31 30 3a 26 79 79 6d  minor.yy410:&yym
276be 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-1].minor.yy4
276bf 31 30 29 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20  10);.  }.       
276c0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
276c1 73 65 20 32 32 32 3a 20 2f 2a 20 65 78 70 72 20  se 222: /* expr 
276c2 3a 3a 3d 20 45 58 49 53 54 53 20 4c 50 20 73 65  ::= EXISTS LP se
276c3 6c 65 63 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 20  lect RP */.{.   
276c4 20 45 78 70 72 20 2a 70 20 3d 20 79 79 67 6f 74   Expr *p = yygot
276c5 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73  ominor.yy172 = s
276c6 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
276c7 73 65 2c 20 54 4b 5f 45 58 49 53 54 53 2c 20 30  se, TK_EXISTS, 0
276c8 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  , 0, 0);.    if(
276c9 20 70 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70   p ){.      p->p
276ca 53 65 6c 65 63 74 20 3d 20 79 79 6d 73 70 5b 2d  Select = yymsp[-
276cb 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31 39 3b 0a  1].minor.yy219;.
276cc 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
276cd 72 53 70 61 6e 28 70 2c 26 79 79 6d 73 70 5b 2d  rSpan(p,&yymsp[-
276ce 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  3].minor.yy0,&yy
276cf 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
276d0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
276d1 45 78 70 72 53 65 74 48 65 69 67 68 74 28 79 79  ExprSetHeight(yy
276d2 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 29  gotominor.yy172)
276d3 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
276d4 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
276d5 44 65 6c 65 74 65 28 79 79 6d 73 70 5b 2d 31 5d  Delete(yymsp[-1]
276d6 2e 6d 69 6e 6f 72 2e 79 79 32 31 39 29 3b 0a 20  .minor.yy219);. 
276d7 20 20 20 7d 0a 20 20 7d 0a 20 20 20 20 20 20 20     }.  }.       
276d8 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
276d9 73 65 20 32 32 33 3a 20 2f 2a 20 65 78 70 72 20  se 223: /* expr 
276da 3a 3a 3d 20 43 41 53 45 20 63 61 73 65 5f 6f 70  ::= CASE case_op
276db 65 72 61 6e 64 20 63 61 73 65 5f 65 78 70 72 6c  erand case_exprl
276dc 69 73 74 20 63 61 73 65 5f 65 6c 73 65 20 45 4e  ist case_else EN
276dd 44 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d  D */.{.  yygotom
276de 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71 6c  inor.yy172 = sql
276df 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
276e0 2c 20 54 4b 5f 43 41 53 45 2c 20 79 79 6d 73 70  , TK_CASE, yymsp
276e1 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32  [-3].minor.yy172
276e2 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
276e3 72 2e 79 79 31 37 32 2c 20 30 29 3b 0a 20 20 69  r.yy172, 0);.  i
276e4 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  f( yygotominor.y
276e5 79 31 37 32 20 29 7b 0a 20 20 20 20 79 79 67 6f  y172 ){.    yygo
276e6 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2d 3e 70  tominor.yy172->p
276e7 4c 69 73 74 20 3d 20 79 79 6d 73 70 5b 2d 32 5d  List = yymsp[-2]
276e8 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 3b 0a 20 20  .minor.yy174;.  
276e9 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74    sqlite3ExprSet
276ea 48 65 69 67 68 74 28 79 79 67 6f 74 6f 6d 69 6e  Height(yygotomin
276eb 6f 72 2e 79 79 31 37 32 29 3b 0a 20 20 7d 65 6c  or.yy172);.  }el
276ec 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  se{.    sqlite3E
276ed 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 79 79  xprListDelete(yy
276ee 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
276ef 31 37 34 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  174);.  }.  sqli
276f0 74 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f  te3ExprSpan(yygo
276f1 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 26  tominor.yy172, &
276f2 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
276f3 79 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  yy0, &yymsp[0].m
276f4 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20  inor.yy0);.}.   
276f5 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
276f6 20 20 63 61 73 65 20 32 32 34 3a 20 2f 2a 20 63    case 224: /* c
276f7 61 73 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d  ase_exprlist ::=
276f8 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 57   case_exprlist W
276f9 48 45 4e 20 65 78 70 72 20 54 48 45 4e 20 65 78  HEN expr THEN ex
276fa 70 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  pr */.{.  yygoto
276fb 6d 69 6e 6f 72 2e 79 79 31 37 34 20 3d 20 73 71  minor.yy174 = sq
276fc 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
276fd 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70  end(pParse,yymsp
276fe 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34  [-4].minor.yy174
276ff 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  , yymsp[-2].mino
27700 72 2e 79 79 31 37 32 2c 20 30 29 3b 0a 20 20 79  r.yy172, 0);.  y
27701 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34  ygotominor.yy174
27702 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
27703 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
27704 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37  yygotominor.yy17
27705 34 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  4, yymsp[0].mino
27706 72 2e 79 79 31 37 32 2c 20 30 29 3b 0a 7d 0a 20  r.yy172, 0);.}. 
27707 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
27708 20 20 20 20 63 61 73 65 20 32 32 35 3a 20 2f 2a      case 225: /*
27709 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 3a   case_exprlist :
2770a 3a 3d 20 57 48 45 4e 20 65 78 70 72 20 54 48 45  := WHEN expr THE
2770b 4e 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 79 79  N expr */.{.  yy
2770c 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 20  gotominor.yy174 
2770d 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
2770e 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30  tAppend(pParse,0
2770f 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  , yymsp[-2].mino
27710 72 2e 79 79 31 37 32 2c 20 30 29 3b 0a 20 20 79  r.yy172, 0);.  y
27711 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34  ygotominor.yy174
27712 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
27713 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
27714 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37  yygotominor.yy17
27715 34 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  4, yymsp[0].mino
27716 72 2e 79 79 31 37 32 2c 20 30 29 3b 0a 7d 0a 20  r.yy172, 0);.}. 
27717 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
27718 20 20 20 20 63 61 73 65 20 32 33 34 3a 20 2f 2a      case 234: /*
27719 20 63 6d 64 20 3a 3a 3d 20 43 52 45 41 54 45 20   cmd ::= CREATE 
2771a 75 6e 69 71 75 65 66 6c 61 67 20 49 4e 44 45 58  uniqueflag INDEX
2771b 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20   ifnotexists nm 
2771c 64 62 6e 6d 20 4f 4e 20 6e 6d 20 4c 50 20 69 64  dbnm ON nm LP id
2771d 78 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 0a 20 20  xlist RP */.{.  
2771e 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64  sqlite3CreateInd
2771f 65 78 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73  ex(pParse, &yyms
27720 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31  p[-6].minor.yy41
27721 30 2c 20 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69  0, &yymsp[-5].mi
27722 6e 6f 72 2e 79 79 34 31 30 2c 20 0a 20 20 20 20  nor.yy410, .    
27723 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27724 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
27725 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62  ppend(pParse->db
27726 2c 30 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  ,0,&yymsp[-3].mi
27727 6e 6f 72 2e 79 79 34 31 30 2c 30 29 2c 20 79 79  nor.yy410,0), yy
27728 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
27729 31 37 34 2c 20 79 79 6d 73 70 5b 2d 39 5d 2e 6d  174, yymsp[-9].m
2772a 69 6e 6f 72 2e 79 79 34 36 2c 0a 20 20 20 20 20  inor.yy46,.     
2772b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2772c 20 26 79 79 6d 73 70 5b 2d 31 30 5d 2e 6d 69 6e   &yymsp[-10].min
2772d 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 30  or.yy0, &yymsp[0
2772e 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 53 51 4c  ].minor.yy0, SQL
2772f 49 54 45 5f 53 4f 5f 41 53 43 2c 20 79 79 6d 73  ITE_SO_ASC, yyms
27730 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36  p[-7].minor.yy46
27731 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
27732 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
27733 33 35 3a 20 2f 2a 20 75 6e 69 71 75 65 66 6c 61  35: /* uniquefla
27734 67 20 3a 3a 3d 20 55 4e 49 51 55 45 20 2a 2f 0a  g ::= UNIQUE */.
27735 20 20 20 20 20 20 63 61 73 65 20 32 38 33 3a 20        case 283: 
27736 2f 2a 20 72 61 69 73 65 74 79 70 65 20 3a 3a 3d  /* raisetype ::=
27737 20 41 42 4f 52 54 20 2a 2f 0a 7b 79 79 67 6f 74   ABORT */.{yygot
27738 6f 6d 69 6e 6f 72 2e 79 79 34 36 20 3d 20 4f 45  ominor.yy46 = OE
27739 5f 41 62 6f 72 74 3b 7d 0a 20 20 20 20 20 20 20  _Abort;}.       
2773a 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2773b 73 65 20 32 33 36 3a 20 2f 2a 20 75 6e 69 71 75  se 236: /* uniqu
2773c 65 66 6c 61 67 20 3a 3a 3d 20 2a 2f 0a 7b 79 79  eflag ::= */.{yy
2773d 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 36 20 3d  gotominor.yy46 =
2773e 20 4f 45 5f 4e 6f 6e 65 3b 7d 0a 20 20 20 20 20   OE_None;}.     
2773f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
27740 63 61 73 65 20 32 33 39 3a 20 2f 2a 20 69 64 78  case 239: /* idx
27741 6c 69 73 74 20 3a 3a 3d 20 69 64 78 6c 69 73 74  list ::= idxlist
27742 20 43 4f 4d 4d 41 20 69 64 78 69 74 65 6d 20 63   COMMA idxitem c
27743 6f 6c 6c 61 74 65 20 73 6f 72 74 6f 72 64 65 72  ollate sortorder
27744 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 70 20   */.{.  Expr *p 
27745 3d 20 30 3b 0a 20 20 69 66 28 20 79 79 6d 73 70  = 0;.  if( yymsp
27746 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30  [-1].minor.yy410
27747 2e 6e 3e 30 20 29 7b 0a 20 20 20 20 70 20 3d 20  .n>0 ){.    p = 
27748 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
27749 72 73 65 2c 20 54 4b 5f 43 4f 4c 55 4d 4e 2c 20  rse, TK_COLUMN, 
2774a 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  0, 0, 0);.    sq
2774b 6c 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c  lite3ExprSetColl
2774c 28 70 50 61 72 73 65 2c 20 70 2c 20 26 79 79 6d  (pParse, p, &yym
2774d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-1].minor.yy4
2774e 31 30 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74  10);.  }.  yygot
2774f 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 20 3d 20 73  ominor.yy174 = s
27750 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
27751 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73  pend(pParse,yyms
27752 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37  p[-4].minor.yy17
27753 34 2c 20 70 2c 20 26 79 79 6d 73 70 5b 2d 32 5d  4, p, &yymsp[-2]
27754 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29 3b 0a 20  .minor.yy410);. 
27755 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
27756 43 68 65 63 6b 4c 65 6e 67 74 68 28 70 50 61 72  CheckLength(pPar
27757 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  se, yygotominor.
27758 79 79 31 37 34 2c 20 22 69 6e 64 65 78 22 29 3b  yy174, "index");
27759 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e  .  if( yygotomin
2775a 6f 72 2e 79 79 31 37 34 20 29 20 79 79 67 6f 74  or.yy174 ) yygot
2775b 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 2d 3e 61 5b  ominor.yy174->a[
2775c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37  yygotominor.yy17
2775d 34 2d 3e 6e 45 78 70 72 2d 31 5d 2e 73 6f 72 74  4->nExpr-1].sort
2775e 4f 72 64 65 72 20 3d 20 79 79 6d 73 70 5b 30 5d  Order = yymsp[0]
2775f 2e 6d 69 6e 6f 72 2e 79 79 34 36 3b 0a 7d 0a 20  .minor.yy46;.}. 
27760 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
27761 20 20 20 20 63 61 73 65 20 32 34 30 3a 20 2f 2a      case 240: /*
27762 20 69 64 78 6c 69 73 74 20 3a 3a 3d 20 69 64 78   idxlist ::= idx
27763 69 74 65 6d 20 63 6f 6c 6c 61 74 65 20 73 6f 72  item collate sor
27764 74 6f 72 64 65 72 20 2a 2f 0a 7b 0a 20 20 45 78  torder */.{.  Ex
27765 70 72 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28  pr *p = 0;.  if(
27766 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
27767 2e 79 79 34 31 30 2e 6e 3e 30 20 29 7b 0a 20 20  .yy410.n>0 ){.  
27768 20 20 70 20 3d 20 73 71 6c 69 74 65 33 50 45 78    p = sqlite3PEx
27769 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 43 4f  pr(pParse, TK_CO
2776a 4c 55 4d 4e 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  LUMN, 0, 0, 0);.
2776b 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53      sqlite3ExprS
2776c 65 74 43 6f 6c 6c 28 70 50 61 72 73 65 2c 20 70  etColl(pParse, p
2776d 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  , &yymsp[-1].min
2776e 6f 72 2e 79 79 34 31 30 29 3b 0a 20 20 7d 0a 20  or.yy410);.  }. 
2776f 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
27770 37 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  74 = sqlite3Expr
27771 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
27772 65 2c 30 2c 20 70 2c 20 26 79 79 6d 73 70 5b 2d  e,0, p, &yymsp[-
27773 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29 3b  2].minor.yy410);
27774 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69  .  sqlite3ExprLi
27775 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 70 50  stCheckLength(pP
27776 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f  arse, yygotomino
27777 72 2e 79 79 31 37 34 2c 20 22 69 6e 64 65 78 22  r.yy174, "index"
27778 29 3b 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d  );.  if( yygotom
27779 69 6e 6f 72 2e 79 79 31 37 34 20 29 20 79 79 67  inor.yy174 ) yyg
2777a 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 34 2d 3e  otominor.yy174->
2777b 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  a[yygotominor.yy
2777c 31 37 34 2d 3e 6e 45 78 70 72 2d 31 5d 2e 73 6f  174->nExpr-1].so
2777d 72 74 4f 72 64 65 72 20 3d 20 79 79 6d 73 70 5b  rtOrder = yymsp[
2777e 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 3b 0a 7d  0].minor.yy46;.}
2777f 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
27780 20 20 20 20 20 20 63 61 73 65 20 32 34 32 3a 20        case 242: 
27781 2f 2a 20 63 6f 6c 6c 61 74 65 20 3a 3a 3d 20 2a  /* collate ::= *
27782 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
27783 79 34 31 30 2e 7a 20 3d 20 30 3b 20 79 79 67 6f  y410.z = 0; yygo
27784 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 6e 20  tominor.yy410.n 
27785 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  = 0;}.        br
27786 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
27787 32 34 34 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  244: /* cmd ::= 
27788 44 52 4f 50 20 49 4e 44 45 58 20 69 66 65 78 69  DROP INDEX ifexi
27789 73 74 73 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f 0a  sts fullname */.
2778a 7b 73 71 6c 69 74 65 33 44 72 6f 70 49 6e 64 65  {sqlite3DropInde
2778b 78 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  x(pParse, yymsp[
2778c 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 37 33 2c 20  0].minor.yy373, 
2778d 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2778e 79 79 34 36 29 3b 7d 0a 20 20 20 20 20 20 20 20  yy46);}.        
2778f 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
27790 65 20 32 34 35 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 245: /* cmd ::
27791 3d 20 56 41 43 55 55 4d 20 2a 2f 0a 20 20 20 20  = VACUUM */.    
27792 20 20 63 61 73 65 20 32 34 36 3a 20 2f 2a 20 63    case 246: /* c
27793 6d 64 20 3a 3a 3d 20 56 41 43 55 55 4d 20 6e 6d  md ::= VACUUM nm
27794 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 56 61 63 75   */.{sqlite3Vacu
27795 75 6d 28 70 50 61 72 73 65 29 3b 7d 0a 20 20 20  um(pParse);}.   
27796 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
27797 20 20 63 61 73 65 20 32 34 37 3a 20 2f 2a 20 63    case 247: /* c
27798 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d  md ::= PRAGMA nm
27799 20 64 62 6e 6d 20 45 51 20 6e 6d 6e 75 6d 20 2a   dbnm EQ nmnum *
2779a 2f 0a 7b 73 71 6c 69 74 65 33 50 72 61 67 6d 61  /.{sqlite3Pragma
2779b 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d  (pParse,&yymsp[-
2779c 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 26  3].minor.yy410,&
2779d 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2779e 79 79 34 31 30 2c 26 79 79 6d 73 70 5b 30 5d 2e  yy410,&yymsp[0].
2779f 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 30 29 3b 7d  minor.yy410,0);}
277a0 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
277a1 20 20 20 20 20 20 63 61 73 65 20 32 34 38 3a 20        case 248: 
277a2 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d  /* cmd ::= PRAGM
277a3 41 20 6e 6d 20 64 62 6e 6d 20 45 51 20 4f 4e 20  A nm dbnm EQ ON 
277a4 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 34  */.      case 24
277a5 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52  9: /* cmd ::= PR
277a6 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51 20  AGMA nm dbnm EQ 
277a7 44 45 4c 45 54 45 20 2a 2f 0a 7b 73 71 6c 69 74  DELETE */.{sqlit
277a8 65 33 50 72 61 67 6d 61 28 70 50 61 72 73 65 2c  e3Pragma(pParse,
277a9 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  &yymsp[-3].minor
277aa 2e 79 79 34 31 30 2c 26 79 79 6d 73 70 5b 2d 32  .yy410,&yymsp[-2
277ab 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 26 79  ].minor.yy410,&y
277ac 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
277ad 30 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  0,0);}.        b
277ae 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
277af 20 32 35 30 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   250: /* cmd ::=
277b0 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20   PRAGMA nm dbnm 
277b1 45 51 20 6d 69 6e 75 73 5f 6e 75 6d 20 2a 2f 0a  EQ minus_num */.
277b2 7b 0a 20 20 73 71 6c 69 74 65 33 50 72 61 67 6d  {.  sqlite3Pragm
277b3 61 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b  a(pParse,&yymsp[
277b4 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c  -3].minor.yy410,
277b5 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
277b6 2e 79 79 34 31 30 2c 26 79 79 6d 73 70 5b 30 5d  .yy410,&yymsp[0]
277b7 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 31 29 3b  .minor.yy410,1);
277b8 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
277b9 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 31  ;.      case 251
277ba 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41  : /* cmd ::= PRA
277bb 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 4c 50 20 6e  GMA nm dbnm LP n
277bc 6d 6e 75 6d 20 52 50 20 2a 2f 0a 7b 73 71 6c 69  mnum RP */.{sqli
277bd 74 65 33 50 72 61 67 6d 61 28 70 50 61 72 73 65  te3Pragma(pParse
277be 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  ,&yymsp[-4].mino
277bf 72 2e 79 79 34 31 30 2c 26 79 79 6d 73 70 5b 2d  r.yy410,&yymsp[-
277c0 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 26  3].minor.yy410,&
277c1 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
277c2 79 79 34 31 30 2c 30 29 3b 7d 0a 20 20 20 20 20  yy410,0);}.     
277c3 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
277c4 63 61 73 65 20 32 35 32 3a 20 2f 2a 20 63 6d 64  case 252: /* cmd
277c5 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64   ::= PRAGMA nm d
277c6 62 6e 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 50  bnm */.{sqlite3P
277c7 72 61 67 6d 61 28 70 50 61 72 73 65 2c 26 79 79  ragma(pParse,&yy
277c8 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
277c9 34 31 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  410,&yymsp[0].mi
277ca 6e 6f 72 2e 79 79 34 31 30 2c 30 2c 30 29 3b 7d  nor.yy410,0,0);}
277cb 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
277cc 20 20 20 20 20 20 63 61 73 65 20 32 36 30 3a 20        case 260: 
277cd 2f 2a 20 63 6d 64 20 3a 3a 3d 20 43 52 45 41 54  /* cmd ::= CREAT
277ce 45 20 74 72 69 67 67 65 72 5f 64 65 63 6c 20 42  E trigger_decl B
277cf 45 47 49 4e 20 74 72 69 67 67 65 72 5f 63 6d 64  EGIN trigger_cmd
277d0 5f 6c 69 73 74 20 45 4e 44 20 2a 2f 0a 7b 0a 20  _list END */.{. 
277d1 20 54 6f 6b 65 6e 20 61 6c 6c 3b 0a 20 20 61 6c   Token all;.  al
277d2 6c 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e  l.z = yymsp[-3].
277d3 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 7a 3b 0a 20  minor.yy410.z;. 
277d4 20 61 6c 6c 2e 6e 20 3d 20 28 79 79 6d 73 70 5b   all.n = (yymsp[
277d5 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 2d  0].minor.yy0.z -
277d6 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72   yymsp[-3].minor
277d7 2e 79 79 34 31 30 2e 7a 29 20 2b 20 79 79 6d 73  .yy410.z) + yyms
277d8 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e  p[0].minor.yy0.n
277d9 3b 0a 20 20 73 71 6c 69 74 65 33 46 69 6e 69 73  ;.  sqlite3Finis
277da 68 54 72 69 67 67 65 72 28 70 50 61 72 73 65 2c  hTrigger(pParse,
277db 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
277dc 2e 79 79 32 34 33 2c 20 26 61 6c 6c 29 3b 0a 7d  .yy243, &all);.}
277dd 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
277de 20 20 20 20 20 20 63 61 73 65 20 32 36 31 3a 20        case 261: 
277df 2f 2a 20 74 72 69 67 67 65 72 5f 64 65 63 6c 20  /* trigger_decl 
277e0 3a 3a 3d 20 74 65 6d 70 20 54 52 49 47 47 45 52  ::= temp TRIGGER
277e1 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20   ifnotexists nm 
277e2 64 62 6e 6d 20 74 72 69 67 67 65 72 5f 74 69 6d  dbnm trigger_tim
277e3 65 20 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20  e trigger_event 
277e4 4f 4e 20 66 75 6c 6c 6e 61 6d 65 20 66 6f 72 65  ON fullname fore
277e5 61 63 68 5f 63 6c 61 75 73 65 20 77 68 65 6e 5f  ach_clause when_
277e6 63 6c 61 75 73 65 20 2a 2f 0a 7b 0a 20 20 73 71  clause */.{.  sq
277e7 6c 69 74 65 33 42 65 67 69 6e 54 72 69 67 67 65  lite3BeginTrigge
277e8 72 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70  r(pParse, &yymsp
277e9 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30  [-7].minor.yy410
277ea 2c 20 26 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e  , &yymsp[-6].min
277eb 6f 72 2e 79 79 34 31 30 2c 20 79 79 6d 73 70 5b  or.yy410, yymsp[
277ec 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 2c 20  -5].minor.yy46, 
277ed 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
277ee 79 79 33 37 30 2e 61 2c 20 79 79 6d 73 70 5b 2d  yy370.a, yymsp[-
277ef 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 37 30 2e 62  4].minor.yy370.b
277f0 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  , yymsp[-2].mino
277f1 72 2e 79 79 33 37 33 2c 20 79 79 6d 73 70 5b 30  r.yy373, yymsp[0
277f2 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 79  ].minor.yy172, y
277f3 79 6d 73 70 5b 2d 31 30 5d 2e 6d 69 6e 6f 72 2e  ymsp[-10].minor.
277f4 79 79 34 36 2c 20 79 79 6d 73 70 5b 2d 38 5d 2e  yy46, yymsp[-8].
277f5 6d 69 6e 6f 72 2e 79 79 34 36 29 3b 0a 20 20 79  minor.yy46);.  y
277f6 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30  ygotominor.yy410
277f7 20 3d 20 28 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69   = (yymsp[-6].mi
277f8 6e 6f 72 2e 79 79 34 31 30 2e 6e 3d 3d 30 3f 79  nor.yy410.n==0?y
277f9 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-7].minor.y
277fa 79 34 31 30 3a 79 79 6d 73 70 5b 2d 36 5d 2e 6d  y410:yymsp[-6].m
277fb 69 6e 6f 72 2e 79 79 34 31 30 29 3b 0a 7d 0a 20  inor.yy410);.}. 
277fc 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
277fd 20 20 20 20 63 61 73 65 20 32 36 32 3a 20 2f 2a      case 262: /*
277fe 20 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a   trigger_time ::
277ff 3d 20 42 45 46 4f 52 45 20 2a 2f 0a 20 20 20 20  = BEFORE */.    
27800 20 20 63 61 73 65 20 32 36 35 3a 20 2f 2a 20 74    case 265: /* t
27801 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20  rigger_time ::= 
27802 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  */.{ yygotominor
27803 2e 79 79 34 36 20 3d 20 54 4b 5f 42 45 46 4f 52  .yy46 = TK_BEFOR
27804 45 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  E; }.        bre
27805 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
27806 36 33 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 74  63: /* trigger_t
27807 69 6d 65 20 3a 3a 3d 20 41 46 54 45 52 20 2a 2f  ime ::= AFTER */
27808 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
27809 79 34 36 20 3d 20 54 4b 5f 41 46 54 45 52 3b 20  y46 = TK_AFTER; 
2780a 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
2780b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 34  ;.      case 264
2780c 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 74 69 6d  : /* trigger_tim
2780d 65 20 3a 3a 3d 20 49 4e 53 54 45 41 44 20 4f 46  e ::= INSTEAD OF
2780e 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f   */.{ yygotomino
2780f 72 2e 79 79 34 36 20 3d 20 54 4b 5f 49 4e 53 54  r.yy46 = TK_INST
27810 45 41 44 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  EAD;}.        br
27811 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
27812 32 36 36 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  266: /* trigger_
27813 65 76 65 6e 74 20 3a 3a 3d 20 44 45 4c 45 54 45  event ::= DELETE
27814 7c 49 4e 53 45 52 54 20 2a 2f 0a 20 20 20 20 20  |INSERT */.     
27815 20 63 61 73 65 20 32 36 37 3a 20 2f 2a 20 74 72   case 267: /* tr
27816 69 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20  igger_event ::= 
27817 55 50 44 41 54 45 20 2a 2f 0a 7b 79 79 67 6f 74  UPDATE */.{yygot
27818 6f 6d 69 6e 6f 72 2e 79 79 33 37 30 2e 61 20 3d  ominor.yy370.a =
27819 20 79 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72 3b   yymsp[0].major;
2781a 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
2781b 37 30 2e 62 20 3d 20 30 3b 7d 0a 20 20 20 20 20  70.b = 0;}.     
2781c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2781d 63 61 73 65 20 32 36 38 3a 20 2f 2a 20 74 72 69  case 268: /* tri
2781e 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 55  gger_event ::= U
2781f 50 44 41 54 45 20 4f 46 20 69 6e 73 63 6f 6c 6c  PDATE OF inscoll
27820 69 73 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ist */.{yygotomi
27821 6e 6f 72 2e 79 79 33 37 30 2e 61 20 3d 20 54 4b  nor.yy370.a = TK
27822 5f 55 50 44 41 54 45 3b 20 79 79 67 6f 74 6f 6d  _UPDATE; yygotom
27823 69 6e 6f 72 2e 79 79 33 37 30 2e 62 20 3d 20 79  inor.yy370.b = y
27824 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
27825 34 33 32 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  432;}.        br
27826 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
27827 32 37 31 3a 20 2f 2a 20 77 68 65 6e 5f 63 6c 61  271: /* when_cla
27828 75 73 65 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20  use ::= */.     
27829 20 63 61 73 65 20 32 38 38 3a 20 2f 2a 20 6b 65   case 288: /* ke
2782a 79 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 20 79  y_opt ::= */.{ y
2782b 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32  ygotominor.yy172
2782c 20 3d 20 30 3b 20 7d 0a 20 20 20 20 20 20 20 20   = 0; }.        
2782d 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2782e 65 20 32 37 32 3a 20 2f 2a 20 77 68 65 6e 5f 63  e 272: /* when_c
2782f 6c 61 75 73 65 20 3a 3a 3d 20 57 48 45 4e 20 65  lause ::= WHEN e
27830 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  xpr */.      cas
27831 65 20 32 38 39 3a 20 2f 2a 20 6b 65 79 5f 6f 70  e 289: /* key_op
27832 74 20 3a 3a 3d 20 4b 45 59 20 65 78 70 72 20 2a  t ::= KEY expr *
27833 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  /.{ yygotominor.
27834 79 79 31 37 32 20 3d 20 79 79 6d 73 70 5b 30 5d  yy172 = yymsp[0]
27835 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 3b 20 7d 0a  .minor.yy172; }.
27836 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
27837 20 20 20 20 20 63 61 73 65 20 32 37 33 3a 20 2f       case 273: /
27838 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69  * trigger_cmd_li
27839 73 74 20 3a 3a 3d 20 74 72 69 67 67 65 72 5f 63  st ::= trigger_c
2783a 6d 64 5f 6c 69 73 74 20 74 72 69 67 67 65 72 5f  md_list trigger_
2783b 63 6d 64 20 53 45 4d 49 20 2a 2f 0a 7b 0a 20 20  cmd SEMI */.{.  
2783c 69 66 28 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  if( yymsp[-2].mi
2783d 6e 6f 72 2e 79 79 32 34 33 20 29 7b 0a 20 20 20  nor.yy243 ){.   
2783e 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
2783f 2e 79 79 32 34 33 2d 3e 70 4c 61 73 74 2d 3e 70  .yy243->pLast->p
27840 4e 65 78 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d  Next = yymsp[-1]
27841 2e 6d 69 6e 6f 72 2e 79 79 32 34 33 3b 0a 20 20  .minor.yy243;.  
27842 7d 65 6c 73 65 7b 0a 20 20 20 20 79 79 6d 73 70  }else{.    yymsp
27843 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33  [-2].minor.yy243
27844 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e   = yymsp[-1].min
27845 6f 72 2e 79 79 32 34 33 3b 0a 20 20 7d 0a 20 20  or.yy243;.  }.  
27846 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
27847 79 79 32 34 33 2d 3e 70 4c 61 73 74 20 3d 20 79  yy243->pLast = y
27848 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
27849 79 32 34 33 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  y243;.  yygotomi
2784a 6e 6f 72 2e 79 79 32 34 33 20 3d 20 79 79 6d 73  nor.yy243 = yyms
2784b 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34  p[-2].minor.yy24
2784c 33 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  3;.}.        bre
2784d 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2784e 37 34 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63  74: /* trigger_c
2784f 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20 2a 2f 0a 7b  md_list ::= */.{
27850 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32   yygotominor.yy2
27851 34 33 20 3d 20 30 3b 20 7d 0a 20 20 20 20 20 20  43 = 0; }.      
27852 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
27853 61 73 65 20 32 37 35 3a 20 2f 2a 20 74 72 69 67  ase 275: /* trig
27854 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 55 50 44 41  ger_cmd ::= UPDA
27855 54 45 20 6f 72 63 6f 6e 66 20 6e 6d 20 53 45 54  TE orconf nm SET
27856 20 73 65 74 6c 69 73 74 20 77 68 65 72 65 5f 6f   setlist where_o
27857 70 74 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69  pt */.{ yygotomi
27858 6e 6f 72 2e 79 79 32 34 33 20 3d 20 73 71 6c 69  nor.yy243 = sqli
27859 74 65 33 54 72 69 67 67 65 72 55 70 64 61 74 65  te3TriggerUpdate
2785a 53 74 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c  Step(pParse->db,
2785b 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f   &yymsp[-3].mino
2785c 72 2e 79 79 34 31 30 2c 20 79 79 6d 73 70 5b 2d  r.yy410, yymsp[-
2785d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 34 2c 20  1].minor.yy174, 
2785e 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2785f 79 31 37 32 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e  y172, yymsp[-4].
27860 6d 69 6e 6f 72 2e 79 79 34 36 29 3b 20 7d 0a 20  minor.yy46); }. 
27861 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
27862 20 20 20 20 63 61 73 65 20 32 37 36 3a 20 2f 2a      case 276: /*
27863 20 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d   trigger_cmd ::=
27864 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f   insert_cmd INTO
27865 20 6e 6d 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f   nm inscollist_o
27866 70 74 20 56 41 4c 55 45 53 20 4c 50 20 69 74 65  pt VALUES LP ite
27867 6d 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 79 79 67  mlist RP */.{yyg
27868 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 33 20 3d  otominor.yy243 =
27869 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 49   sqlite3TriggerI
2786a 6e 73 65 72 74 53 74 65 70 28 70 50 61 72 73 65  nsertStep(pParse
2786b 2d 3e 64 62 2c 20 26 79 79 6d 73 70 5b 2d 35 5d  ->db, &yymsp[-5]
2786c 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 20 79 79  .minor.yy410, yy
2786d 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2786e 34 33 32 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  432, yymsp[-1].m
2786f 69 6e 6f 72 2e 79 79 31 37 34 2c 20 30 2c 20 79  inor.yy174, 0, y
27870 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-7].minor.y
27871 79 34 36 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  y46);}.        b
27872 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
27873 20 32 37 37 3a 20 2f 2a 20 74 72 69 67 67 65 72   277: /* trigger
27874 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f  _cmd ::= insert_
27875 63 6d 64 20 49 4e 54 4f 20 6e 6d 20 69 6e 73 63  cmd INTO nm insc
27876 6f 6c 6c 69 73 74 5f 6f 70 74 20 73 65 6c 65 63  ollist_opt selec
27877 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  t */.{yygotomino
27878 72 2e 79 79 32 34 33 20 3d 20 73 71 6c 69 74 65  r.yy243 = sqlite
27879 33 54 72 69 67 67 65 72 49 6e 73 65 72 74 53 74  3TriggerInsertSt
2787a 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 26  ep(pParse->db, &
2787b 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2787c 79 79 34 31 30 2c 20 79 79 6d 73 70 5b 2d 31 5d  yy410, yymsp[-1]
2787d 2e 6d 69 6e 6f 72 2e 79 79 34 33 32 2c 20 30 2c  .minor.yy432, 0,
2787e 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2787f 79 79 32 31 39 2c 20 79 79 6d 73 70 5b 2d 34 5d  yy219, yymsp[-4]
27880 2e 6d 69 6e 6f 72 2e 79 79 34 36 29 3b 7d 0a 20  .minor.yy46);}. 
27881 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
27882 20 20 20 20 63 61 73 65 20 32 37 38 3a 20 2f 2a      case 278: /*
27883 20 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d   trigger_cmd ::=
27884 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 6e 6d 20   DELETE FROM nm 
27885 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a 7b 79 79  where_opt */.{yy
27886 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 33 20  gotominor.yy243 
27887 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  = sqlite3Trigger
27888 44 65 6c 65 74 65 53 74 65 70 28 70 50 61 72 73  DeleteStep(pPars
27889 65 2d 3e 64 62 2c 20 26 79 79 6d 73 70 5b 2d 31  e->db, &yymsp[-1
2788a 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 20 79  ].minor.yy410, y
2788b 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2788c 31 37 32 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  172);}.        b
2788d 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2788e 20 32 37 39 3a 20 2f 2a 20 74 72 69 67 67 65 72   279: /* trigger
2788f 5f 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63 74 20  _cmd ::= select 
27890 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
27891 79 79 32 34 33 20 3d 20 73 71 6c 69 74 65 33 54  yy243 = sqlite3T
27892 72 69 67 67 65 72 53 65 6c 65 63 74 53 74 65 70  riggerSelectStep
27893 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d  (pParse->db, yym
27894 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 31  sp[0].minor.yy21
27895 39 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  9); }.        br
27896 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
27897 32 38 30 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  280: /* expr ::=
27898 20 52 41 49 53 45 20 4c 50 20 49 47 4e 4f 52 45   RAISE LP IGNORE
27899 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74   RP */.{.  yygot
2789a 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73  ominor.yy172 = s
2789b 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2789c 73 65 2c 20 54 4b 5f 52 41 49 53 45 2c 20 30 2c  se, TK_RAISE, 0,
2789d 20 30 2c 20 30 29 3b 20 0a 20 20 69 66 28 20 79   0, 0); .  if( y
2789e 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32  ygotominor.yy172
2789f 20 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69   ){.    yygotomi
278a0 6e 6f 72 2e 79 79 31 37 32 2d 3e 69 43 6f 6c 75  nor.yy172->iColu
278a1 6d 6e 20 3d 20 4f 45 5f 49 67 6e 6f 72 65 3b 0a  mn = OE_Ignore;.
278a2 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53      sqlite3ExprS
278a3 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  pan(yygotominor.
278a4 79 79 31 37 32 2c 20 26 79 79 6d 73 70 5b 2d 33  yy172, &yymsp[-3
278a5 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79  ].minor.yy0, &yy
278a6 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
278a7 29 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20 20 20 20  );.  }.}.       
278a8 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
278a9 73 65 20 32 38 31 3a 20 2f 2a 20 65 78 70 72 20  se 281: /* expr 
278aa 3a 3a 3d 20 52 41 49 53 45 20 4c 50 20 72 61 69  ::= RAISE LP rai
278ab 73 65 74 79 70 65 20 43 4f 4d 4d 41 20 6e 6d 20  setype COMMA nm 
278ac 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  RP */.{.  yygoto
278ad 6d 69 6e 6f 72 2e 79 79 31 37 32 20 3d 20 73 71  minor.yy172 = sq
278ae 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
278af 65 2c 20 54 4b 5f 52 41 49 53 45 2c 20 30 2c 20  e, TK_RAISE, 0, 
278b0 30 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  0, &yymsp[-1].mi
278b1 6e 6f 72 2e 79 79 34 31 30 29 3b 20 0a 20 20 69  nor.yy410); .  i
278b2 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  f( yygotominor.y
278b3 79 31 37 32 20 29 20 7b 0a 20 20 20 20 79 79 67  y172 ) {.    yyg
278b4 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 37 32 2d 3e  otominor.yy172->
278b5 69 43 6f 6c 75 6d 6e 20 3d 20 79 79 6d 73 70 5b  iColumn = yymsp[
278b6 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36 3b 0a  -3].minor.yy46;.
278b7 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53      sqlite3ExprS
278b8 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  pan(yygotominor.
278b9 79 79 31 37 32 2c 20 26 79 79 6d 73 70 5b 2d 35  yy172, &yymsp[-5
278ba 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79  ].minor.yy0, &yy
278bb 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
278bc 29 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20 20 20 20  );.  }.}.       
278bd 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
278be 73 65 20 32 38 32 3a 20 2f 2a 20 72 61 69 73 65  se 282: /* raise
278bf 74 79 70 65 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43  type ::= ROLLBAC
278c0 4b 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  K */.{yygotomino
278c1 72 2e 79 79 34 36 20 3d 20 4f 45 5f 52 6f 6c 6c  r.yy46 = OE_Roll
278c2 62 61 63 6b 3b 7d 0a 20 20 20 20 20 20 20 20 62  back;}.        b
278c3 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
278c4 20 32 38 34 3a 20 2f 2a 20 72 61 69 73 65 74 79   284: /* raisety
278c5 70 65 20 3a 3a 3d 20 46 41 49 4c 20 2a 2f 0a 7b  pe ::= FAIL */.{
278c6 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 36  yygotominor.yy46
278c7 20 3d 20 4f 45 5f 46 61 69 6c 3b 7d 0a 20 20 20   = OE_Fail;}.   
278c8 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
278c9 20 20 63 61 73 65 20 32 38 35 3a 20 2f 2a 20 63    case 285: /* c
278ca 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54 52 49 47  md ::= DROP TRIG
278cb 47 45 52 20 69 66 65 78 69 73 74 73 20 66 75 6c  GER ifexists ful
278cc 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c  lname */.{.  sql
278cd 69 74 65 33 44 72 6f 70 54 72 69 67 67 65 72 28  ite3DropTrigger(
278ce 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 30 5d 2e  pParse,yymsp[0].
278cf 6d 69 6e 6f 72 2e 79 79 33 37 33 2c 79 79 6d 73  minor.yy373,yyms
278d0 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 36  p[-1].minor.yy46
278d1 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
278d2 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
278d3 38 36 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41  86: /* cmd ::= A
278d4 54 54 41 43 48 20 64 61 74 61 62 61 73 65 5f 6b  TTACH database_k
278d5 77 5f 6f 70 74 20 65 78 70 72 20 41 53 20 65 78  w_opt expr AS ex
278d6 70 72 20 6b 65 79 5f 6f 70 74 20 2a 2f 0a 7b 0a  pr key_opt */.{.
278d7 20 20 73 71 6c 69 74 65 33 41 74 74 61 63 68 28    sqlite3Attach(
278d8 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 33  pParse, yymsp[-3
278d9 5d 2e 6d 69 6e 6f 72 2e 79 79 31 37 32 2c 20 79  ].minor.yy172, y
278da 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
278db 79 31 37 32 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  y172, yymsp[0].m
278dc 69 6e 6f 72 2e 79 79 31 37 32 29 3b 0a 7d 0a 20  inor.yy172);.}. 
278dd 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
278de 20 20 20 20 63 61 73 65 20 32 38 37 3a 20 2f 2a      case 287: /*
278df 20 63 6d 64 20 3a 3a 3d 20 44 45 54 41 43 48 20   cmd ::= DETACH 
278e0 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20  database_kw_opt 
278e1 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  expr */.{.  sqli
278e2 74 65 33 44 65 74 61 63 68 28 70 50 61 72 73 65  te3Detach(pParse
278e3 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
278e4 2e 79 79 31 37 32 29 3b 0a 7d 0a 20 20 20 20 20  .yy172);.}.     
278e5 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
278e6 63 61 73 65 20 32 39 32 3a 20 2f 2a 20 63 6d 64  case 292: /* cmd
278e7 20 3a 3a 3d 20 52 45 49 4e 44 45 58 20 2a 2f 0a   ::= REINDEX */.
278e8 7b 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28  {sqlite3Reindex(
278e9 70 50 61 72 73 65 2c 20 30 2c 20 30 29 3b 7d 0a  pParse, 0, 0);}.
278ea 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
278eb 20 20 20 20 20 63 61 73 65 20 32 39 33 3a 20 2f       case 293: /
278ec 2a 20 63 6d 64 20 3a 3a 3d 20 52 45 49 4e 44 45  * cmd ::= REINDE
278ed 58 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 73 71  X nm dbnm */.{sq
278ee 6c 69 74 65 33 52 65 69 6e 64 65 78 28 70 50 61  lite3Reindex(pPa
278ef 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e  rse, &yymsp[-1].
278f0 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 20 26 79 79  minor.yy410, &yy
278f1 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34  msp[0].minor.yy4
278f2 31 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  10);}.        br
278f3 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
278f4 32 39 34 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  294: /* cmd ::= 
278f5 41 4e 41 4c 59 5a 45 20 2a 2f 0a 7b 73 71 6c 69  ANALYZE */.{sqli
278f6 74 65 33 41 6e 61 6c 79 7a 65 28 70 50 61 72 73  te3Analyze(pPars
278f7 65 2c 20 30 2c 20 30 29 3b 7d 0a 20 20 20 20 20  e, 0, 0);}.     
278f8 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
278f9 63 61 73 65 20 32 39 35 3a 20 2f 2a 20 63 6d 64  case 295: /* cmd
278fa 20 3a 3a 3d 20 41 4e 41 4c 59 5a 45 20 6e 6d 20   ::= ANALYZE nm 
278fb 64 62 6e 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  dbnm */.{sqlite3
278fc 41 6e 61 6c 79 7a 65 28 70 50 61 72 73 65 2c 20  Analyze(pParse, 
278fd 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
278fe 2e 79 79 34 31 30 2c 20 26 79 79 6d 73 70 5b 30  .yy410, &yymsp[0
278ff 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29 3b 7d  ].minor.yy410);}
27900 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
27901 20 20 20 20 20 20 63 61 73 65 20 32 39 36 3a 20        case 296: 
27902 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41 4c 54 45 52  /* cmd ::= ALTER
27903 20 54 41 42 4c 45 20 66 75 6c 6c 6e 61 6d 65 20   TABLE fullname 
27904 52 45 4e 41 4d 45 20 54 4f 20 6e 6d 20 2a 2f 0a  RENAME TO nm */.
27905 7b 0a 20 20 73 71 6c 69 74 65 33 41 6c 74 65 72  {.  sqlite3Alter
27906 52 65 6e 61 6d 65 54 61 62 6c 65 28 70 50 61 72  RenameTable(pPar
27907 73 65 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  se,yymsp[-3].min
27908 6f 72 2e 79 79 33 37 33 2c 26 79 79 6d 73 70 5b  or.yy373,&yymsp[
27909 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 29 3b  0].minor.yy410);
2790a 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2790b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 37  ;.      case 297
2790c 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41 4c 54  : /* cmd ::= ALT
2790d 45 52 20 54 41 42 4c 45 20 61 64 64 5f 63 6f 6c  ER TABLE add_col
2790e 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 20 41 44 44  umn_fullname ADD
2790f 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 63 6f   kwcolumn_opt co
27910 6c 75 6d 6e 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  lumn */.{.  sqli
27911 74 65 33 41 6c 74 65 72 46 69 6e 69 73 68 41 64  te3AlterFinishAd
27912 64 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20  dColumn(pParse, 
27913 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
27914 79 79 34 31 30 29 3b 0a 7d 0a 20 20 20 20 20 20  yy410);.}.      
27915 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
27916 61 73 65 20 32 39 38 3a 20 2f 2a 20 61 64 64 5f  ase 298: /* add_
27917 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 20  column_fullname 
27918 3a 3a 3d 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f 0a  ::= fullname */.
27919 7b 0a 20 20 73 71 6c 69 74 65 33 41 6c 74 65 72  {.  sqlite3Alter
2791a 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d 6e 28 70  BeginAddColumn(p
2791b 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e  Parse, yymsp[0].
2791c 6d 69 6e 6f 72 2e 79 79 33 37 33 29 3b 0a 7d 0a  minor.yy373);.}.
2791d 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2791e 20 20 20 20 20 63 61 73 65 20 33 30 31 3a 20 2f       case 301: /
2791f 2a 20 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65  * cmd ::= create
27920 5f 76 74 61 62 20 2a 2f 0a 7b 73 71 6c 69 74 65  _vtab */.{sqlite
27921 33 56 74 61 62 46 69 6e 69 73 68 50 61 72 73 65  3VtabFinishParse
27922 28 70 50 61 72 73 65 2c 30 29 3b 7d 0a 20 20 20  (pParse,0);}.   
27923 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
27924 20 20 63 61 73 65 20 33 30 32 3a 20 2f 2a 20 63    case 302: /* c
27925 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 76 74  md ::= create_vt
27926 61 62 20 4c 50 20 76 74 61 62 61 72 67 6c 69 73  ab LP vtabarglis
27927 74 20 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  t RP */.{sqlite3
27928 56 74 61 62 46 69 6e 69 73 68 50 61 72 73 65 28  VtabFinishParse(
27929 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 30 5d  pParse,&yymsp[0]
2792a 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20  .minor.yy0);}.  
2792b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2792c 20 20 20 63 61 73 65 20 33 30 33 3a 20 2f 2a 20     case 303: /* 
2792d 63 72 65 61 74 65 5f 76 74 61 62 20 3a 3a 3d 20  create_vtab ::= 
2792e 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54  CREATE VIRTUAL T
2792f 41 42 4c 45 20 6e 6d 20 64 62 6e 6d 20 55 53 49  ABLE nm dbnm USI
27930 4e 47 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 20 20 73  NG nm */.{.    s
27931 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 50  qlite3VtabBeginP
27932 61 72 73 65 28 70 50 61 72 73 65 2c 20 26 79 79  arse(pParse, &yy
27933 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
27934 34 31 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e  410, &yymsp[-2].
27935 6d 69 6e 6f 72 2e 79 79 34 31 30 2c 20 26 79 79  minor.yy410, &yy
27936 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34  msp[0].minor.yy4
27937 31 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  10);.}.        b
27938 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
27939 20 33 30 36 3a 20 2f 2a 20 76 74 61 62 61 72 67   306: /* vtabarg
2793a 20 3a 3a 3d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33   ::= */.{sqlite3
2793b 56 74 61 62 41 72 67 49 6e 69 74 28 70 50 61 72  VtabArgInit(pPar
2793c 73 65 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  se);}.        br
2793d 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2793e 33 30 38 3a 20 2f 2a 20 76 74 61 62 61 72 67 74  308: /* vtabargt
2793f 6f 6b 65 6e 20 3a 3a 3d 20 41 4e 59 20 2a 2f 0a  oken ::= ANY */.
27940 20 20 20 20 20 20 63 61 73 65 20 33 30 39 3a 20        case 309: 
27941 2f 2a 20 76 74 61 62 61 72 67 74 6f 6b 65 6e 20  /* vtabargtoken 
27942 3a 3a 3d 20 6c 70 20 61 6e 79 6c 69 73 74 20 52  ::= lp anylist R
27943 50 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  P */.      case 
27944 33 31 30 3a 20 2f 2a 20 6c 70 20 3a 3a 3d 20 4c  310: /* lp ::= L
27945 50 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  P */.      case 
27946 33 31 32 3a 20 2f 2a 20 61 6e 79 6c 69 73 74 20  312: /* anylist 
27947 3a 3a 3d 20 61 6e 79 6c 69 73 74 20 41 4e 59 20  ::= anylist ANY 
27948 2a 2f 0a 7b 73 71 6c 69 74 65 33 56 74 61 62 41  */.{sqlite3VtabA
27949 72 67 45 78 74 65 6e 64 28 70 50 61 72 73 65 2c  rgExtend(pParse,
2794a 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2794b 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  yy0);}.        b
2794c 72 65 61 6b 3b 0a 20 20 7d 3b 0a 20 20 79 79 67  reak;.  };.  yyg
2794d 6f 74 6f 20 3d 20 79 79 52 75 6c 65 49 6e 66 6f  oto = yyRuleInfo
2794e 5b 79 79 72 75 6c 65 6e 6f 5d 2e 6c 68 73 3b 0a  [yyruleno].lhs;.
2794f 20 20 79 79 73 69 7a 65 20 3d 20 79 79 52 75 6c    yysize = yyRul
27950 65 49 6e 66 6f 5b 79 79 72 75 6c 65 6e 6f 5d 2e  eInfo[yyruleno].
27951 6e 72 68 73 3b 0a 20 20 79 79 70 50 61 72 73 65  nrhs;.  yypParse
27952 72 2d 3e 79 79 69 64 78 20 2d 3d 20 79 79 73 69  r->yyidx -= yysi
27953 7a 65 3b 0a 20 20 79 79 61 63 74 20 3d 20 79 79  ze;.  yyact = yy
27954 5f 66 69 6e 64 5f 72 65 64 75 63 65 5f 61 63 74  _find_reduce_act
27955 69 6f 6e 28 79 79 6d 73 70 5b 2d 79 79 73 69 7a  ion(yymsp[-yysiz
27956 65 5d 2e 73 74 61 74 65 6e 6f 2c 79 79 67 6f 74  e].stateno,yygot
27957 6f 29 3b 0a 20 20 69 66 28 20 79 79 61 63 74 20  o);.  if( yyact 
27958 3c 20 59 59 4e 53 54 41 54 45 20 29 7b 0a 23 69  < YYNSTATE ){.#i
27959 66 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20  fdef NDEBUG.    
2795a 2f 2a 20 49 66 20 77 65 20 61 72 65 20 6e 6f 74  /* If we are not
2795b 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74   debugging and t
2795c 68 65 20 72 65 64 75 63 65 20 61 63 74 69 6f 6e  he reduce action
2795d 20 70 6f 70 70 65 64 20 61 74 20 6c 65 61 73 74   popped at least
2795e 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 65 6c 65 6d  .    ** one elem
2795f 65 6e 74 20 6f 66 66 20 74 68 65 20 73 74 61 63  ent off the stac
27960 6b 2c 20 74 68 65 6e 20 77 65 20 63 61 6e 20 70  k, then we can p
27961 75 73 68 20 74 68 65 20 6e 65 77 20 65 6c 65 6d  ush the new elem
27962 65 6e 74 20 62 61 63 6b 0a 20 20 20 20 2a 2a 20  ent back.    ** 
27963 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 20 68  onto the stack h
27964 65 72 65 2c 20 61 6e 64 20 73 6b 69 70 20 74 68  ere, and skip th
27965 65 20 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77  e stack overflow
27966 20 74 65 73 74 20 69 6e 20 79 79 5f 73 68 69 66   test in yy_shif
27967 74 28 29 2e 0a 20 20 20 20 2a 2a 20 54 68 61 74  t()..    ** That
27968 20 67 69 76 65 73 20 61 20 73 69 67 6e 69 66 69   gives a signifi
27969 63 61 6e 74 20 73 70 65 65 64 20 69 6d 70 72 6f  cant speed impro
2796a 76 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 69  vement. */.    i
2796b 66 28 20 79 79 73 69 7a 65 20 29 7b 0a 20 20 20  f( yysize ){.   
2796c 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79     yypParser->yy
2796d 69 64 78 2b 2b 3b 0a 20 20 20 20 20 20 79 79 6d  idx++;.      yym
2796e 73 70 20 2d 3d 20 79 79 73 69 7a 65 2d 31 3b 0a  sp -= yysize-1;.
2796f 20 20 20 20 20 20 79 79 6d 73 70 2d 3e 73 74 61        yymsp->sta
27970 74 65 6e 6f 20 3d 20 79 79 61 63 74 3b 0a 20 20  teno = yyact;.  
27971 20 20 20 20 79 79 6d 73 70 2d 3e 6d 61 6a 6f 72      yymsp->major
27972 20 3d 20 79 79 67 6f 74 6f 3b 0a 20 20 20 20 20   = yygoto;.     
27973 20 79 79 6d 73 70 2d 3e 6d 69 6e 6f 72 20 3d 20   yymsp->minor = 
27974 79 79 67 6f 74 6f 6d 69 6e 6f 72 3b 0a 20 20 20  yygotominor;.   
27975 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20   }else.#endif.  
27976 20 20 7b 0a 20 20 20 20 20 20 79 79 5f 73 68 69    {.      yy_shi
27977 66 74 28 79 79 70 50 61 72 73 65 72 2c 79 79 61  ft(yypParser,yya
27978 63 74 2c 79 79 67 6f 74 6f 2c 26 79 79 67 6f 74  ct,yygoto,&yygot
27979 6f 6d 69 6e 6f 72 29 3b 0a 20 20 20 20 7d 0a 20  ominor);.    }. 
2797a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
2797b 72 74 28 20 79 79 61 63 74 20 3d 3d 20 59 59 4e  rt( yyact == YYN
2797c 53 54 41 54 45 20 2b 20 59 59 4e 52 55 4c 45 20  STATE + YYNRULE 
2797d 2b 20 31 20 29 3b 0a 20 20 20 20 79 79 5f 61 63  + 1 );.    yy_ac
2797e 63 65 70 74 28 79 79 70 50 61 72 73 65 72 29 3b  cept(yypParser);
2797f 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
27980 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65  e following code
27981 20 65 78 65 63 75 74 65 73 20 77 68 65 6e 20 74   executes when t
27982 68 65 20 70 61 72 73 65 20 66 61 69 6c 73 0a 2a  he parse fails.*
27983 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79 79  /.static void yy
27984 5f 70 61 72 73 65 5f 66 61 69 6c 65 64 28 0a 20  _parse_failed(. 
27985 20 79 79 50 61 72 73 65 72 20 2a 79 79 70 50 61   yyParser *yypPa
27986 72 73 65 72 20 20 20 20 20 20 20 20 20 20 20 2f  rser           /
27987 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a  * The parser */.
27988 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73  ){.  sqlite3Pars
27989 65 72 41 52 47 5f 46 45 54 43 48 3b 0a 23 69 66  erARG_FETCH;.#if
2798a 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69 66  ndef NDEBUG.  if
2798b 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b  ( yyTraceFILE ){
2798c 0a 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54  .    fprintf(yyT
2798d 72 61 63 65 46 49 4c 45 2c 22 25 73 46 61 69 6c  raceFILE,"%sFail
2798e 21 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d  !\n",yyTraceProm
2798f 70 74 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  pt);.  }.#endif.
27990 20 20 77 68 69 6c 65 28 20 79 79 70 50 61 72 73    while( yypPars
27991 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20 79  er->yyidx>=0 ) y
27992 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61  y_pop_parser_sta
27993 63 6b 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20  ck(yypParser);. 
27994 20 2f 2a 20 48 65 72 65 20 63 6f 64 65 20 69 73   /* Here code is
27995 20 69 6e 73 65 72 74 65 64 20 77 68 69 63 68 20   inserted which 
27996 77 69 6c 6c 20 62 65 20 65 78 65 63 75 74 65 64  will be executed
27997 20 77 68 65 6e 65 76 65 72 20 74 68 65 0a 20 20   whenever the.  
27998 2a 2a 20 70 61 72 73 65 72 20 66 61 69 6c 73 20  ** parser fails 
27999 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73  */.  sqlite3Pars
2799a 65 72 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20  erARG_STORE; /* 
2799b 53 75 70 70 72 65 73 73 20 77 61 72 6e 69 6e 67  Suppress warning
2799c 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20 25 65   about unused %e
2799d 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 76 61  xtra_argument va
2799e 72 69 61 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a 0a  riable */.}../*.
2799f 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
279a0 20 63 6f 64 65 20 65 78 65 63 75 74 65 73 20 77   code executes w
279a1 68 65 6e 20 61 20 73 79 6e 74 61 78 20 65 72 72  hen a syntax err
279a2 6f 72 20 66 69 72 73 74 20 6f 63 63 75 72 73 2e  or first occurs.
279a3 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
279a4 79 79 5f 73 79 6e 74 61 78 5f 65 72 72 6f 72 28  yy_syntax_error(
279a5 0a 20 20 79 79 50 61 72 73 65 72 20 2a 79 79 70  .  yyParser *yyp
279a6 50 61 72 73 65 72 2c 20 20 20 20 20 20 20 20 20  Parser,         
279a7 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
279a8 2a 2f 0a 20 20 69 6e 74 20 79 79 6d 61 6a 6f 72  */.  int yymajor
279a9 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
279aa 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 6a 6f 72      /* The major
279ab 20 74 79 70 65 20 6f 66 20 74 68 65 20 65 72 72   type of the err
279ac 6f 72 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 59 59  or token */.  YY
279ad 4d 49 4e 4f 52 54 59 50 45 20 79 79 6d 69 6e 6f  MINORTYPE yymino
279ae 72 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  r            /* 
279af 54 68 65 20 6d 69 6e 6f 72 20 74 79 70 65 20 6f  The minor type o
279b0 66 20 74 68 65 20 65 72 72 6f 72 20 74 6f 6b 65  f the error toke
279b1 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  n */.){.  sqlite
279b2 33 50 61 72 73 65 72 41 52 47 5f 46 45 54 43 48  3ParserARG_FETCH
279b3 3b 0a 23 64 65 66 69 6e 65 20 54 4f 4b 45 4e 20  ;.#define TOKEN 
279b4 28 79 79 6d 69 6e 6f 72 2e 79 79 30 29 0a 0a 20  (yyminor.yy0).. 
279b5 20 61 73 73 65 72 74 28 20 54 4f 4b 45 4e 2e 7a   assert( TOKEN.z
279b6 5b 30 5d 20 29 3b 20 20 2f 2a 20 54 68 65 20 74  [0] );  /* The t
279b7 6f 6b 65 6e 69 7a 65 72 20 61 6c 77 61 79 73 20  okenizer always 
279b8 67 69 76 65 73 20 75 73 20 61 20 74 6f 6b 65 6e  gives us a token
279b9 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 72 72   */.  sqlite3Err
279ba 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
279bb 65 61 72 20 5c 22 25 54 5c 22 3a 20 73 79 6e 74  ear \"%T\": synt
279bc 61 78 20 65 72 72 6f 72 22 2c 20 26 54 4f 4b 45  ax error", &TOKE
279bd 4e 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 61  N);.  pParse->pa
279be 72 73 65 45 72 72 6f 72 20 3d 20 31 3b 0a 20 20  rseError = 1;.  
279bf 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
279c0 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70 72  _STORE; /* Suppr
279c1 65 73 73 20 77 61 72 6e 69 6e 67 20 61 62 6f 75  ess warning abou
279c2 74 20 75 6e 75 73 65 64 20 25 65 78 74 72 61 5f  t unused %extra_
279c3 61 72 67 75 6d 65 6e 74 20 76 61 72 69 61 62 6c  argument variabl
279c4 65 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  e */.}../*.** Th
279c5 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 65  e following is e
279c6 78 65 63 75 74 65 64 20 77 68 65 6e 20 74 68 65  xecuted when the
279c7 20 70 61 72 73 65 72 20 61 63 63 65 70 74 73 0a   parser accepts.
279c8 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79  */.static void y
279c9 79 5f 61 63 63 65 70 74 28 0a 20 20 79 79 50 61  y_accept(.  yyPa
279ca 72 73 65 72 20 2a 79 79 70 50 61 72 73 65 72 20  rser *yypParser 
279cb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
279cc 20 70 61 72 73 65 72 20 2a 2f 0a 29 7b 0a 20 20   parser */.){.  
279cd 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
279ce 5f 46 45 54 43 48 3b 0a 23 69 66 6e 64 65 66 20  _FETCH;.#ifndef 
279cf 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79 79 54  NDEBUG.  if( yyT
279d0 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20  raceFILE ){.    
279d1 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46  fprintf(yyTraceF
279d2 49 4c 45 2c 22 25 73 41 63 63 65 70 74 21 5c 6e  ILE,"%sAccept!\n
279d3 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74 29  ",yyTracePrompt)
279d4 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 77  ;.  }.#endif.  w
279d5 68 69 6c 65 28 20 79 79 70 50 61 72 73 65 72 2d  hile( yypParser-
279d6 3e 79 79 69 64 78 3e 3d 30 20 29 20 79 79 5f 70  >yyidx>=0 ) yy_p
279d7 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b 28  op_parser_stack(
279d8 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20 2f 2a  yypParser);.  /*
279d9 20 48 65 72 65 20 63 6f 64 65 20 69 73 20 69 6e   Here code is in
279da 73 65 72 74 65 64 20 77 68 69 63 68 20 77 69 6c  serted which wil
279db 6c 20 62 65 20 65 78 65 63 75 74 65 64 20 77 68  l be executed wh
279dc 65 6e 65 76 65 72 20 74 68 65 0a 20 20 2a 2a 20  enever the.  ** 
279dd 70 61 72 73 65 72 20 61 63 63 65 70 74 73 20 2a  parser accepts *
279de 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  /.  sqlite3Parse
279df 72 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53  rARG_STORE; /* S
279e0 75 70 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20  uppress warning 
279e1 61 62 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78  about unused %ex
279e2 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72  tra_argument var
279e3 69 61 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a 20 54  iable */.}../* T
279e4 68 65 20 6d 61 69 6e 20 70 61 72 73 65 72 20 70  he main parser p
279e5 72 6f 67 72 61 6d 2e 0a 2a 2a 20 54 68 65 20 66  rogram..** The f
279e6 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73  irst argument is
279e7 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
279e8 73 74 72 75 63 74 75 72 65 20 6f 62 74 61 69 6e  structure obtain
279e9 65 64 20 66 72 6f 6d 0a 2a 2a 20 22 73 71 6c 69  ed from.** "sqli
279ea 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 22 20  te3ParserAlloc" 
279eb 77 68 69 63 68 20 64 65 73 63 72 69 62 65 73 20  which describes 
279ec 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74  the current stat
279ed 65 20 6f 66 20 74 68 65 20 70 61 72 73 65 72 2e  e of the parser.
279ee 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61  .** The second a
279ef 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6d  rgument is the m
279f0 61 6a 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65  ajor token numbe
279f1 72 2e 20 20 54 68 65 20 74 68 69 72 64 20 69 73  r.  The third is
279f2 0a 2a 2a 20 74 68 65 20 6d 69 6e 6f 72 20 74 6f  .** the minor to
279f3 6b 65 6e 2e 20 20 54 68 65 20 66 6f 75 72 74 68  ken.  The fourth
279f4 20 6f 70 74 69 6f 6e 61 6c 20 61 72 67 75 6d 65   optional argume
279f5 6e 74 20 69 73 20 77 68 61 74 65 76 65 72 20 74  nt is whatever t
279f6 68 65 0a 2a 2a 20 75 73 65 72 20 77 61 6e 74 73  he.** user wants
279f7 20 28 61 6e 64 20 73 70 65 63 69 66 69 65 64 20   (and specified 
279f8 69 6e 20 74 68 65 20 67 72 61 6d 6d 61 72 29 20  in the grammar) 
279f9 61 6e 64 20 69 73 20 61 76 61 69 6c 61 62 6c 65  and is available
279fa 20 66 6f 72 0a 2a 2a 20 75 73 65 20 62 79 20 74   for.** use by t
279fb 68 65 20 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e  he action routin
279fc 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73  es..**.** Inputs
279fd 3a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69  :.** <ul>.** <li
279fe 3e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  > A pointer to t
279ff 68 65 20 70 61 72 73 65 72 20 28 61 6e 20 6f 70  he parser (an op
27a00 61 71 75 65 20 73 74 72 75 63 74 75 72 65 2e 29  aque structure.)
27a01 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 6d 61 6a  .** <li> The maj
27a02 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e  or token number.
27a03 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 6d 69 6e  .** <li> The min
27a04 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e  or token number.
27a05 0a 2a 2a 20 3c 6c 69 3e 20 41 6e 20 6f 70 74 69  .** <li> An opti
27a06 6f 6e 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 61  on argument of a
27a07 20 67 72 61 6d 6d 61 72 2d 73 70 65 63 69 66 69   grammar-specifi
27a08 65 64 20 74 79 70 65 2e 0a 2a 2a 20 3c 2f 75 6c  ed type..** </ul
27a09 3e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75 74 73 3a  >.**.** Outputs:
27a0a 0a 2a 2a 20 4e 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c  .** None..*/.SQL
27a0b 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
27a0c 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 28 0a   sqlite3Parser(.
27a0d 20 20 76 6f 69 64 20 2a 79 79 70 2c 20 20 20 20    void *yyp,    
27a0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
27a0f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a  * The parser */.
27a10 20 20 69 6e 74 20 79 79 6d 61 6a 6f 72 2c 20 20    int yymajor,  
27a11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
27a12 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65  * The major toke
27a13 6e 20 63 6f 64 65 20 6e 75 6d 62 65 72 20 2a 2f  n code number */
27a14 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  .  sqlite3Parser
27a15 54 4f 4b 45 4e 54 59 50 45 20 79 79 6d 69 6e 6f  TOKENTYPE yymino
27a16 72 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76  r       /* The v
27a17 61 6c 75 65 20 66 6f 72 20 74 68 65 20 74 6f 6b  alue for the tok
27a18 65 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50  en */.  sqlite3P
27a19 61 72 73 65 72 41 52 47 5f 50 44 45 43 4c 20 20  arserARG_PDECL  
27a1a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
27a1b 4f 70 74 69 6f 6e 61 6c 20 25 65 78 74 72 61 5f  Optional %extra_
27a1c 61 72 67 75 6d 65 6e 74 20 70 61 72 61 6d 65 74  argument paramet
27a1d 65 72 20 2a 2f 0a 29 7b 0a 20 20 59 59 4d 49 4e  er */.){.  YYMIN
27a1e 4f 52 54 59 50 45 20 79 79 6d 69 6e 6f 72 75 6e  ORTYPE yyminorun
27a1f 69 6f 6e 3b 0a 20 20 69 6e 74 20 79 79 61 63 74  ion;.  int yyact
27a20 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
27a21 54 68 65 20 70 61 72 73 65 72 20 61 63 74 69 6f  The parser actio
27a22 6e 2e 20 2a 2f 0a 20 20 69 6e 74 20 79 79 65 6e  n. */.  int yyen
27a23 64 6f 66 69 6e 70 75 74 3b 20 20 20 20 20 2f 2a  dofinput;     /*
27a24 20 54 72 75 65 20 69 66 20 77 65 20 61 72 65 20   True if we are 
27a25 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 69 6e  at the end of in
27a26 70 75 74 20 2a 2f 0a 23 69 66 64 65 66 20 59 59  put */.#ifdef YY
27a27 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 69 6e  ERRORSYMBOL.  in
27a28 74 20 79 79 65 72 72 6f 72 68 69 74 20 3d 20 30  t yyerrorhit = 0
27a29 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 79  ;   /* True if y
27a2a 79 6d 61 6a 6f 72 20 68 61 73 20 69 6e 76 6f 6b  ymajor has invok
27a2b 65 64 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 23  ed an error */.#
27a2c 65 6e 64 69 66 0a 20 20 79 79 50 61 72 73 65 72  endif.  yyParser
27a2d 20 2a 79 79 70 50 61 72 73 65 72 3b 20 20 2f 2a   *yypParser;  /*
27a2e 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 0a   The parser */..
27a2f 20 20 2f 2a 20 28 72 65 29 69 6e 69 74 69 61 6c    /* (re)initial
27a30 69 7a 65 20 74 68 65 20 70 61 72 73 65 72 2c 20  ize the parser, 
27a31 69 66 20 6e 65 63 65 73 73 61 72 79 20 2a 2f 0a  if necessary */.
27a32 20 20 79 79 70 50 61 72 73 65 72 20 3d 20 28 79    yypParser = (y
27a33 79 50 61 72 73 65 72 2a 29 79 79 70 3b 0a 20 20  yParser*)yyp;.  
27a34 69 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79  if( yypParser->y
27a35 79 69 64 78 3c 30 20 29 7b 0a 23 69 66 20 59 59  yidx<0 ){.#if YY
27a36 53 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a 20 20  STACKDEPTH<=0.  
27a37 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d    if( yypParser-
27a38 3e 79 79 73 74 6b 73 7a 20 3c 3d 30 20 29 7b 0a  >yystksz <=0 ){.
27a39 20 20 20 20 20 20 2f 2a 6d 65 6d 73 65 74 28 26        /*memset(&
27a3a 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 2c 20 30 2c  yyminorunion, 0,
27a3b 20 73 69 7a 65 6f 66 28 79 79 6d 69 6e 6f 72 75   sizeof(yyminoru
27a3c 6e 69 6f 6e 29 29 3b 2a 2f 0a 20 20 20 20 20 20  nion));*/.      
27a3d 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 20 3d 20 79  yyminorunion = y
27a3e 79 7a 65 72 6f 6d 69 6e 6f 72 3b 0a 20 20 20 20  yzerominor;.    
27a3f 20 20 79 79 53 74 61 63 6b 4f 76 65 72 66 6c 6f    yyStackOverflo
27a40 77 28 79 79 70 50 61 72 73 65 72 2c 20 26 79 79  w(yypParser, &yy
27a41 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20  minorunion);.   
27a42 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
27a43 0a 23 65 6e 64 69 66 0a 20 20 20 20 79 79 70 50  .#endif.    yypP
27a44 61 72 73 65 72 2d 3e 79 79 69 64 78 20 3d 20 30  arser->yyidx = 0
27a45 3b 0a 20 20 20 20 79 79 70 50 61 72 73 65 72 2d  ;.    yypParser-
27a46 3e 79 79 65 72 72 63 6e 74 20 3d 20 2d 31 3b 0a  >yyerrcnt = -1;.
27a47 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79      yypParser->y
27a48 79 73 74 61 63 6b 5b 30 5d 2e 73 74 61 74 65 6e  ystack[0].staten
27a49 6f 20 3d 20 30 3b 0a 20 20 20 20 79 79 70 50 61  o = 0;.    yypPa
27a4a 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 30 5d  rser->yystack[0]
27a4b 2e 6d 61 6a 6f 72 20 3d 20 30 3b 0a 20 20 7d 0a  .major = 0;.  }.
27a4c 20 20 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 2e 79    yyminorunion.y
27a4d 79 30 20 3d 20 79 79 6d 69 6e 6f 72 3b 0a 20 20  y0 = yyminor;.  
27a4e 79 79 65 6e 64 6f 66 69 6e 70 75 74 20 3d 20 28  yyendofinput = (
27a4f 79 79 6d 61 6a 6f 72 3d 3d 30 29 3b 0a 20 20 73  yymajor==0);.  s
27a50 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f  qlite3ParserARG_
27a51 53 54 4f 52 45 3b 0a 0a 23 69 66 6e 64 65 66 20  STORE;..#ifndef 
27a52 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79 79 54  NDEBUG.  if( yyT
27a53 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20  raceFILE ){.    
27a54 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46  fprintf(yyTraceF
27a55 49 4c 45 2c 22 25 73 49 6e 70 75 74 20 25 73 5c  ILE,"%sInput %s\
27a56 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74  n",yyTracePrompt
27a57 2c 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79 6d  ,yyTokenName[yym
27a58 61 6a 6f 72 5d 29 3b 0a 20 20 7d 0a 23 65 6e 64  ajor]);.  }.#end
27a59 69 66 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 79 79  if..  do{.    yy
27a5a 61 63 74 20 3d 20 79 79 5f 66 69 6e 64 5f 73 68  act = yy_find_sh
27a5b 69 66 74 5f 61 63 74 69 6f 6e 28 79 79 70 50 61  ift_action(yypPa
27a5c 72 73 65 72 2c 79 79 6d 61 6a 6f 72 29 3b 0a 20  rser,yymajor);. 
27a5d 20 20 20 69 66 28 20 79 79 61 63 74 3c 59 59 4e     if( yyact<YYN
27a5e 53 54 41 54 45 20 29 7b 0a 20 20 20 20 20 20 61  STATE ){.      a
27a5f 73 73 65 72 74 28 20 21 79 79 65 6e 64 6f 66 69  ssert( !yyendofi
27a60 6e 70 75 74 20 29 3b 20 20 2f 2a 20 49 6d 70 6f  nput );  /* Impo
27a61 73 73 69 62 6c 65 20 74 6f 20 73 68 69 66 74 20  ssible to shift 
27a62 74 68 65 20 24 20 74 6f 6b 65 6e 20 2a 2f 0a 20  the $ token */. 
27a63 20 20 20 20 20 79 79 5f 73 68 69 66 74 28 79 79       yy_shift(yy
27a64 70 50 61 72 73 65 72 2c 79 79 61 63 74 2c 79 79  pParser,yyact,yy
27a65 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e  major,&yyminorun
27a66 69 6f 6e 29 3b 0a 20 20 20 20 20 20 79 79 70 50  ion);.      yypP
27a67 61 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74 2d  arser->yyerrcnt-
27a68 2d 3b 0a 20 20 20 20 20 20 79 79 6d 61 6a 6f 72  -;.      yymajor
27a69 20 3d 20 59 59 4e 4f 43 4f 44 45 3b 0a 20 20 20   = YYNOCODE;.   
27a6a 20 7d 65 6c 73 65 20 69 66 28 20 79 79 61 63 74   }else if( yyact
27a6b 20 3c 20 59 59 4e 53 54 41 54 45 20 2b 20 59 59   < YYNSTATE + YY
27a6c 4e 52 55 4c 45 20 29 7b 0a 20 20 20 20 20 20 79  NRULE ){.      y
27a6d 79 5f 72 65 64 75 63 65 28 79 79 70 50 61 72 73  y_reduce(yypPars
27a6e 65 72 2c 79 79 61 63 74 2d 59 59 4e 53 54 41 54  er,yyact-YYNSTAT
27a6f 45 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  E);.    }else{. 
27a70 20 20 20 20 20 61 73 73 65 72 74 28 20 79 79 61       assert( yya
27a71 63 74 20 3d 3d 20 59 59 5f 45 52 52 4f 52 5f 41  ct == YY_ERROR_A
27a72 43 54 49 4f 4e 20 29 3b 0a 23 69 66 64 65 66 20  CTION );.#ifdef 
27a73 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20  YYERRORSYMBOL.  
27a74 20 20 20 20 69 6e 74 20 79 79 6d 78 3b 0a 23 65      int yymx;.#e
27a75 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4e 44 45  ndif.#ifndef NDE
27a76 42 55 47 0a 20 20 20 20 20 20 69 66 28 20 79 79  BUG.      if( yy
27a77 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20  TraceFILE ){.   
27a78 20 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54       fprintf(yyT
27a79 72 61 63 65 46 49 4c 45 2c 22 25 73 53 79 6e 74  raceFILE,"%sSynt
27a7a 61 78 20 45 72 72 6f 72 21 5c 6e 22 2c 79 79 54  ax Error!\n",yyT
27a7b 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20 20  racePrompt);.   
27a7c 20 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 64     }.#endif.#ifd
27a7d 65 66 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c  ef YYERRORSYMBOL
27a7e 0a 20 20 20 20 20 20 2f 2a 20 41 20 73 79 6e 74  .      /* A synt
27a7f 61 78 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63  ax error has occ
27a80 75 72 72 65 64 2e 0a 20 20 20 20 20 20 2a 2a 20  urred..      ** 
27a81 54 68 65 20 72 65 73 70 6f 6e 73 65 20 74 6f 20  The response to 
27a82 61 6e 20 65 72 72 6f 72 20 64 65 70 65 6e 64 73  an error depends
27a83 20 75 70 6f 6e 20 77 68 65 74 68 65 72 20 6f 72   upon whether or
27a84 20 6e 6f 74 20 74 68 65 0a 20 20 20 20 20 20 2a   not the.      *
27a85 2a 20 67 72 61 6d 6d 61 72 20 64 65 66 69 6e 65  * grammar define
27a86 73 20 61 6e 20 65 72 72 6f 72 20 74 6f 6b 65 6e  s an error token
27a87 20 22 45 52 52 4f 52 22 2e 20 20 0a 20 20 20 20   "ERROR".  .    
27a88 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68    **.      ** Th
27a89 69 73 20 69 73 20 77 68 61 74 20 77 65 20 64 6f  is is what we do
27a8a 20 69 66 20 74 68 65 20 67 72 61 6d 6d 61 72 20   if the grammar 
27a8b 64 6f 65 73 20 64 65 66 69 6e 65 20 45 52 52 4f  does define ERRO
27a8c 52 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  R:.      **.    
27a8d 20 20 2a 2a 20 20 2a 20 43 61 6c 6c 20 74 68 65    **  * Call the
27a8e 20 25 73 79 6e 74 61 78 5f 65 72 72 6f 72 20 66   %syntax_error f
27a8f 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a  unction..      *
27a90 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 42 65  *.      **  * Be
27a91 67 69 6e 20 70 6f 70 70 69 6e 67 20 74 68 65 20  gin popping the 
27a92 73 74 61 63 6b 20 75 6e 74 69 6c 20 77 65 20 65  stack until we e
27a93 6e 74 65 72 20 61 20 73 74 61 74 65 20 77 68 65  nter a state whe
27a94 72 65 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 69  re.      **    i
27a95 74 20 69 73 20 6c 65 67 61 6c 20 74 6f 20 73 68  t is legal to sh
27a96 69 66 74 20 74 68 65 20 65 72 72 6f 72 20 73 79  ift the error sy
27a97 6d 62 6f 6c 2c 20 74 68 65 6e 20 73 68 69 66 74  mbol, then shift
27a98 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 74 68 65  .      **    the
27a99 20 65 72 72 6f 72 20 73 79 6d 62 6f 6c 2e 0a 20   error symbol.. 
27a9a 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
27a9b 20 20 2a 20 53 65 74 20 74 68 65 20 65 72 72 6f    * Set the erro
27a9c 72 20 63 6f 75 6e 74 20 74 6f 20 74 68 72 65 65  r count to three
27a9d 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  ..      **.     
27a9e 20 2a 2a 20 20 2a 20 42 65 67 69 6e 20 61 63 63   **  * Begin acc
27a9f 65 70 74 69 6e 67 20 61 6e 64 20 73 68 69 66 74  epting and shift
27aa0 69 6e 67 20 6e 65 77 20 74 6f 6b 65 6e 73 2e 20  ing new tokens. 
27aa1 20 4e 6f 20 6e 65 77 20 65 72 72 6f 72 0a 20 20   No new error.  
27aa2 20 20 20 20 2a 2a 20 20 20 20 70 72 6f 63 65 73      **    proces
27aa3 73 69 6e 67 20 77 69 6c 6c 20 6f 63 63 75 72 20  sing will occur 
27aa4 75 6e 74 69 6c 20 74 68 72 65 65 20 74 6f 6b 65  until three toke
27aa5 6e 73 20 68 61 76 65 20 62 65 65 6e 0a 20 20 20  ns have been.   
27aa6 20 20 20 2a 2a 20 20 20 20 73 68 69 66 74 65 64     **    shifted
27aa7 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 0a 20   successfully.. 
27aa8 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2f       **.      */
27aa9 0a 20 20 20 20 20 20 69 66 28 20 79 79 70 50 61  .      if( yypPa
27aaa 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74 3c 30  rser->yyerrcnt<0
27aab 20 29 7b 0a 20 20 20 20 20 20 20 20 79 79 5f 73   ){.        yy_s
27aac 79 6e 74 61 78 5f 65 72 72 6f 72 28 79 79 70 50  yntax_error(yypP
27aad 61 72 73 65 72 2c 79 79 6d 61 6a 6f 72 2c 79 79  arser,yymajor,yy
27aae 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20  minorunion);.   
27aaf 20 20 20 7d 0a 20 20 20 20 20 20 79 79 6d 78 20     }.      yymx 
27ab0 3d 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73  = yypParser->yys
27ab1 74 61 63 6b 5b 79 79 70 50 61 72 73 65 72 2d 3e  tack[yypParser->
27ab2 79 79 69 64 78 5d 2e 6d 61 6a 6f 72 3b 0a 20 20  yyidx].major;.  
27ab3 20 20 20 20 69 66 28 20 79 79 6d 78 3d 3d 59 59      if( yymx==YY
27ab4 45 52 52 4f 52 53 59 4d 42 4f 4c 20 7c 7c 20 79  ERRORSYMBOL || y
27ab5 79 65 72 72 6f 72 68 69 74 20 29 7b 0a 23 69 66  yerrorhit ){.#if
27ab6 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20  ndef NDEBUG.    
27ab7 20 20 20 20 69 66 28 20 79 79 54 72 61 63 65 46      if( yyTraceF
27ab8 49 4c 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ILE ){.         
27ab9 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65   fprintf(yyTrace
27aba 46 49 4c 45 2c 22 25 73 44 69 73 63 61 72 64 20  FILE,"%sDiscard 
27abb 69 6e 70 75 74 20 74 6f 6b 65 6e 20 25 73 5c 6e  input token %s\n
27abc 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
27abd 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 79 79  yyTracePrompt,yy
27abe 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79 6d 61 6a 6f  TokenName[yymajo
27abf 72 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23  r]);.        }.#
27ac0 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 79 79  endif.        yy
27ac1 5f 64 65 73 74 72 75 63 74 6f 72 28 79 79 6d 61  _destructor(yyma
27ac2 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f  jor,&yyminorunio
27ac3 6e 29 3b 0a 20 20 20 20 20 20 20 20 79 79 6d 61  n);.        yyma
27ac4 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f 44 45 3b 0a  jor = YYNOCODE;.
27ac5 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
27ac6 20 20 20 20 20 20 77 68 69 6c 65 28 0a 20 20 20        while(.   
27ac7 20 20 20 20 20 20 20 79 79 70 50 61 72 73 65 72         yypParser
27ac8 2d 3e 79 79 69 64 78 20 3e 3d 20 30 20 26 26 0a  ->yyidx >= 0 &&.
27ac9 20 20 20 20 20 20 20 20 20 20 79 79 6d 78 20 21            yymx !
27aca 3d 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 20  = YYERRORSYMBOL 
27acb 26 26 0a 20 20 20 20 20 20 20 20 20 20 28 79 79  &&.          (yy
27acc 61 63 74 20 3d 20 79 79 5f 66 69 6e 64 5f 72 65  act = yy_find_re
27acd 64 75 63 65 5f 61 63 74 69 6f 6e 28 0a 20 20 20  duce_action(.   
27ace 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27acf 20 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e       yypParser->
27ad0 79 79 73 74 61 63 6b 5b 79 79 70 50 61 72 73 65  yystack[yypParse
27ad1 72 2d 3e 79 79 69 64 78 5d 2e 73 74 61 74 65 6e  r->yyidx].staten
27ad2 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  o,.             
27ad3 20 20 20 20 20 20 20 20 20 20 20 59 59 45 52 52             YYERR
27ad4 4f 52 53 59 4d 42 4f 4c 29 29 20 3e 3d 20 59 59  ORSYMBOL)) >= YY
27ad5 4e 53 54 41 54 45 0a 20 20 20 20 20 20 20 20 29  NSTATE.        )
27ad6 7b 0a 20 20 20 20 20 20 20 20 20 20 79 79 5f 70  {.          yy_p
27ad7 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b 28  op_parser_stack(
27ad8 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20 20 20  yypParser);.    
27ad9 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
27ada 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69  ( yypParser->yyi
27adb 64 78 20 3c 20 30 20 7c 7c 20 79 79 6d 61 6a 6f  dx < 0 || yymajo
27adc 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  r==0 ){.        
27add 20 20 79 79 5f 64 65 73 74 72 75 63 74 6f 72 28    yy_destructor(
27ade 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72  yymajor,&yyminor
27adf 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 20 20  union);.        
27ae0 20 20 79 79 5f 70 61 72 73 65 5f 66 61 69 6c 65    yy_parse_faile
27ae1 64 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20  d(yypParser);.  
27ae2 20 20 20 20 20 20 20 20 79 79 6d 61 6a 6f 72 20          yymajor 
27ae3 3d 20 59 59 4e 4f 43 4f 44 45 3b 0a 20 20 20 20  = YYNOCODE;.    
27ae4 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 79 79      }else if( yy
27ae5 6d 78 21 3d 59 59 45 52 52 4f 52 53 59 4d 42 4f  mx!=YYERRORSYMBO
27ae6 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 59  L ){.          Y
27ae7 59 4d 49 4e 4f 52 54 59 50 45 20 75 32 3b 0a 20  YMINORTYPE u2;. 
27ae8 20 20 20 20 20 20 20 20 20 75 32 2e 59 59 45 52           u2.YYER
27ae9 52 53 59 4d 44 54 20 3d 20 30 3b 0a 20 20 20 20  RSYMDT = 0;.    
27aea 20 20 20 20 20 20 79 79 5f 73 68 69 66 74 28 79        yy_shift(y
27aeb 79 70 50 61 72 73 65 72 2c 79 79 61 63 74 2c 59  ypParser,yyact,Y
27aec 59 45 52 52 4f 52 53 59 4d 42 4f 4c 2c 26 75 32  YERRORSYMBOL,&u2
27aed 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
27aee 20 20 20 7d 0a 20 20 20 20 20 20 79 79 70 50 61     }.      yypPa
27aef 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74 20 3d  rser->yyerrcnt =
27af0 20 33 3b 0a 20 20 20 20 20 20 79 79 65 72 72 6f   3;.      yyerro
27af1 72 68 69 74 20 3d 20 31 3b 0a 23 65 6c 73 65 20  rhit = 1;.#else 
27af2 20 2f 2a 20 59 59 45 52 52 4f 52 53 59 4d 42 4f   /* YYERRORSYMBO
27af3 4c 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64  L is not defined
27af4 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 54 68 69   */.      /* Thi
27af5 73 20 69 73 20 77 68 61 74 20 77 65 20 64 6f 20  s is what we do 
27af6 69 66 20 74 68 65 20 67 72 61 6d 6d 61 72 20 64  if the grammar d
27af7 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e 65 20 45  oes not define E
27af8 52 52 4f 52 3a 0a 20 20 20 20 20 20 2a 2a 0a 20  RROR:.      **. 
27af9 20 20 20 20 20 2a 2a 20 20 2a 20 52 65 70 6f 72       **  * Repor
27afa 74 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  t an error messa
27afb 67 65 2c 20 61 6e 64 20 74 68 72 6f 77 20 61 77  ge, and throw aw
27afc 61 79 20 74 68 65 20 69 6e 70 75 74 20 74 6f 6b  ay the input tok
27afd 65 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  en..      **.   
27afe 20 20 20 2a 2a 20 20 2a 20 49 66 20 74 68 65 20     **  * If the 
27aff 69 6e 70 75 74 20 74 6f 6b 65 6e 20 69 73 20 24  input token is $
27b00 2c 20 74 68 65 6e 20 66 61 69 6c 20 74 68 65 20  , then fail the 
27b01 70 61 72 73 65 2e 0a 20 20 20 20 20 20 2a 2a 0a  parse..      **.
27b02 20 20 20 20 20 20 2a 2a 20 41 73 20 62 65 66 6f        ** As befo
27b03 72 65 2c 20 73 75 62 73 65 71 75 65 6e 74 20 65  re, subsequent e
27b04 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 61 72  rror messages ar
27b05 65 20 73 75 70 70 72 65 73 73 65 64 20 75 6e 74  e suppressed unt
27b06 69 6c 0a 20 20 20 20 20 20 2a 2a 20 74 68 72 65  il.      ** thre
27b07 65 20 69 6e 70 75 74 20 74 6f 6b 65 6e 73 20 68  e input tokens h
27b08 61 76 65 20 62 65 65 6e 20 73 75 63 63 65 73 73  ave been success
27b09 66 75 6c 6c 79 20 73 68 69 66 74 65 64 2e 0a 20  fully shifted.. 
27b0a 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
27b0b 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 65  ( yypParser->yye
27b0c 72 72 63 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20  rrcnt<=0 ){.    
27b0d 20 20 20 20 79 79 5f 73 79 6e 74 61 78 5f 65 72      yy_syntax_er
27b0e 72 6f 72 28 79 79 70 50 61 72 73 65 72 2c 79 79  ror(yypParser,yy
27b0f 6d 61 6a 6f 72 2c 79 79 6d 69 6e 6f 72 75 6e 69  major,yyminoruni
27b10 6f 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  on);.      }.   
27b11 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79     yypParser->yy
27b12 65 72 72 63 6e 74 20 3d 20 33 3b 0a 20 20 20 20  errcnt = 3;.    
27b13 20 20 79 79 5f 64 65 73 74 72 75 63 74 6f 72 28    yy_destructor(
27b14 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72  yymajor,&yyminor
27b15 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 69 66  union);.      if
27b16 28 20 79 79 65 6e 64 6f 66 69 6e 70 75 74 20 29  ( yyendofinput )
27b17 7b 0a 20 20 20 20 20 20 20 20 79 79 5f 70 61 72  {.        yy_par
27b18 73 65 5f 66 61 69 6c 65 64 28 79 79 70 50 61 72  se_failed(yypPar
27b19 73 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ser);.      }.  
27b1a 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59      yymajor = YY
27b1b 4e 4f 43 4f 44 45 3b 0a 23 65 6e 64 69 66 0a 20  NOCODE;.#endif. 
27b1c 20 20 20 7d 0a 20 20 7d 77 68 69 6c 65 28 20 79     }.  }while( y
27b1d 79 6d 61 6a 6f 72 21 3d 59 59 4e 4f 43 4f 44 45  ymajor!=YYNOCODE
27b1e 20 26 26 20 79 79 70 50 61 72 73 65 72 2d 3e 79   && yypParser->y
27b1f 79 69 64 78 3e 3d 30 20 29 3b 0a 20 20 72 65 74  yidx>=0 );.  ret
27b20 75 72 6e 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  urn;.}../*******
27b21 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70  ******* End of p
27b22 61 72 73 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  arse.c *********
27b23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27b24 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27b25 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
27b26 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
27b27 6c 65 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a  le tokenize.c **
27b28 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27b29 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27b2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
27b2b 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
27b2c 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
27b2d 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
27b2e 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
27b2f 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
27b30 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
27b31 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
27b32 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
27b33 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
27b34 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
27b35 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
27b36 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
27b37 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
27b38 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
27b39 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
27b3a 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
27b3b 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
27b3c 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
27b3d 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
27b3e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27b3f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27b41 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
27b42 2a 20 41 6e 20 74 6f 6b 65 6e 69 7a 65 72 20 66  * An tokenizer f
27b43 6f 72 20 53 51 4c 0a 2a 2a 0a 2a 2a 20 54 68 69  or SQL.**.** Thi
27b44 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
27b45 43 20 63 6f 64 65 20 74 68 61 74 20 73 70 6c 69  C code that spli
27b46 74 73 20 61 6e 20 53 51 4c 20 69 6e 70 75 74 20  ts an SQL input 
27b47 73 74 72 69 6e 67 20 75 70 20 69 6e 74 6f 0a 2a  string up into.*
27b48 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 74 6f 6b  * individual tok
27b49 65 6e 73 20 61 6e 64 20 73 65 6e 64 73 20 74 68  ens and sends th
27b4a 6f 73 65 20 74 6f 6b 65 6e 73 20 6f 6e 65 2d 62  ose tokens one-b
27b4b 79 2d 6f 6e 65 20 6f 76 65 72 20 74 6f 20 74 68  y-one over to th
27b4c 65 0a 2a 2a 20 70 61 72 73 65 72 20 66 6f 72 20  e.** parser for 
27b4d 61 6e 61 6c 79 73 69 73 2e 0a 2a 2a 0a 2a 2a 20  analysis..**.** 
27b4e 24 49 64 3a 20 74 6f 6b 65 6e 69 7a 65 2e 63 2c  $Id: tokenize.c,
27b4f 76 20 31 2e 31 34 32 20 32 30 30 38 2f 30 34 2f  v 1.142 2008/04/
27b50 32 38 20 31 38 3a 34 36 3a 34 33 20 64 72 68 20  28 18:46:43 drh 
27b51 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
27b52 54 68 65 20 63 68 61 72 4d 61 70 28 29 20 6d 61  The charMap() ma
27b53 63 72 6f 20 6d 61 70 73 20 61 6c 70 68 61 62 65  cro maps alphabe
27b54 74 69 63 20 63 68 61 72 61 63 74 65 72 73 20 69  tic characters i
27b55 6e 74 6f 20 74 68 65 69 72 0a 2a 2a 20 6c 6f 77  nto their.** low
27b56 65 72 2d 63 61 73 65 20 41 53 43 49 49 20 65 71  er-case ASCII eq
27b57 75 69 76 61 6c 65 6e 74 2e 20 20 4f 6e 20 41 53  uivalent.  On AS
27b58 43 49 49 20 6d 61 63 68 69 6e 65 73 2c 20 74 68  CII machines, th
27b59 69 73 20 69 73 20 6a 75 73 74 0a 2a 2a 20 61 6e  is is just.** an
27b5a 20 75 70 70 65 72 2d 74 6f 2d 6c 6f 77 65 72 20   upper-to-lower 
27b5b 63 61 73 65 20 6d 61 70 2e 20 20 4f 6e 20 45 42  case map.  On EB
27b5c 43 44 49 43 20 6d 61 63 68 69 6e 65 73 20 77 65  CDIC machines we
27b5d 20 61 6c 73 6f 20 6e 65 65 64 0a 2a 2a 20 74 6f   also need.** to
27b5e 20 61 64 6a 75 73 74 20 74 68 65 20 65 6e 63 6f   adjust the enco
27b5f 64 69 6e 67 2e 20 20 4f 6e 6c 79 20 61 6c 70 68  ding.  Only alph
27b60 61 62 65 74 69 63 20 63 68 61 72 61 63 74 65 72  abetic character
27b61 73 20 61 6e 64 20 75 6e 64 65 72 73 63 6f 72 65  s and underscore
27b62 73 0a 2a 2a 20 6e 65 65 64 20 74 6f 20 62 65 20  s.** need to be 
27b63 74 72 61 6e 73 6c 61 74 65 64 2e 0a 2a 2f 0a 23  translated..*/.#
27b64 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43  ifdef SQLITE_ASC
27b65 49 49 0a 23 20 64 65 66 69 6e 65 20 63 68 61 72  II.# define char
27b66 4d 61 70 28 58 29 20 73 71 6c 69 74 65 33 55 70  Map(X) sqlite3Up
27b67 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69  perToLower[(unsi
27b68 67 6e 65 64 20 63 68 61 72 29 58 5d 0a 23 65 6e  gned char)X].#en
27b69 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
27b6a 45 5f 45 42 43 44 49 43 0a 23 20 64 65 66 69 6e  E_EBCDIC.# defin
27b6b 65 20 63 68 61 72 4d 61 70 28 58 29 20 65 62 63  e charMap(X) ebc
27b6c 64 69 63 54 6f 41 73 63 69 69 5b 28 75 6e 73 69  dicToAscii[(unsi
27b6d 67 6e 65 64 20 63 68 61 72 29 58 5d 0a 63 6f 6e  gned char)X].con
27b6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
27b6f 20 65 62 63 64 69 63 54 6f 41 73 63 69 69 5b 5d   ebcdicToAscii[]
27b70 20 3d 20 7b 0a 2f 2a 20 30 20 20 20 31 20 20 20   = {./* 0   1   
27b71 32 20 20 20 33 20 20 20 34 20 20 20 35 20 20 20  2   3   4   5   
27b72 36 20 20 20 37 20 20 20 38 20 20 20 39 20 20 20  6   7   8   9   
27b73 41 20 20 20 42 20 20 20 43 20 20 20 44 20 20 20  A   B   C   D   
27b74 45 20 20 20 46 20 2a 2f 0a 20 20 20 30 2c 20 20  E   F */.   0,  
27b75 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b76 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b77 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b78 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 30  0,  0,  0,  /* 0
27b79 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20  x */.   0,  0,  
27b7a 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b7b 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b7c 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b7d 30 2c 20 20 30 2c 20 20 2f 2a 20 31 78 20 2a 2f  0,  0,  /* 1x */
27b7e 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  .   0,  0,  0,  
27b7f 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b80 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b81 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b82 30 2c 20 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20  0,  /* 2x */.   
27b83 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b84 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b85 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b86 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b87 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 30 2c 20 20  /* 3x */.   0,  
27b88 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b89 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b8a 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b8b 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 34  0,  0,  0,  /* 4
27b8c 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20  x */.   0,  0,  
27b8d 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b8e 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b8f 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b90 30 2c 20 20 30 2c 20 20 2f 2a 20 35 78 20 2a 2f  0,  0,  /* 5x */
27b91 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  .   0,  0,  0,  
27b92 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b93 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b94 30 2c 20 20 30 2c 20 39 35 2c 20 20 30 2c 20 20  0,  0, 95,  0,  
27b95 30 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20  0,  /* 6x */.   
27b96 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b97 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b98 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b99 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27b9a 2f 2a 20 37 78 20 2a 2f 0a 20 20 20 30 2c 20 39  /* 7x */.   0, 9
27b9b 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30  7, 98, 99,100,10
27b9c 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 30  1,102,103,104,10
27b9d 35 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  5,  0,  0,  0,  
27b9e 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 38  0,  0,  0,  /* 8
27b9f 78 20 2a 2f 0a 20 20 20 30 2c 31 30 36 2c 31 30  x */.   0,106,10
27ba0 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31  7,108,109,110,11
27ba1 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 20 20  1,112,113,114,  
27ba2 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27ba3 30 2c 20 20 30 2c 20 20 2f 2a 20 39 78 20 2a 2f  0,  0,  /* 9x */
27ba4 0a 20 20 20 30 2c 20 20 30 2c 31 31 35 2c 31 31  .   0,  0,115,11
27ba5 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32  6,117,118,119,12
27ba6 30 2c 31 32 31 2c 31 32 32 2c 20 20 30 2c 20 20  0,121,122,  0,  
27ba7 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27ba8 30 2c 20 20 2f 2a 20 41 78 20 2a 2f 0a 20 20 20  0,  /* Ax */.   
27ba9 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27baa 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27bab 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27bac 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27bad 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 30 2c 20 39  /* Bx */.   0, 9
27bae 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30  7, 98, 99,100,10
27baf 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 30  1,102,103,104,10
27bb0 35 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  5,  0,  0,  0,  
27bb1 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 43  0,  0,  0,  /* C
27bb2 78 20 2a 2f 0a 20 20 20 30 2c 31 30 36 2c 31 30  x */.   0,106,10
27bb3 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31  7,108,109,110,11
27bb4 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 20 20  1,112,113,114,  
27bb5 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27bb6 30 2c 20 20 30 2c 20 20 2f 2a 20 44 78 20 2a 2f  0,  0,  /* Dx */
27bb7 0a 20 20 20 30 2c 20 20 30 2c 31 31 35 2c 31 31  .   0,  0,115,11
27bb8 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32  6,117,118,119,12
27bb9 30 2c 31 32 31 2c 31 32 32 2c 20 20 30 2c 20 20  0,121,122,  0,  
27bba 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27bbb 30 2c 20 20 2f 2a 20 45 78 20 2a 2f 0a 20 20 20  0,  /* Ex */.   
27bbc 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27bbd 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27bbe 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27bbf 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
27bc0 2f 2a 20 46 78 20 2a 2f 0a 7d 3b 0a 23 65 6e 64  /* Fx */.};.#end
27bc1 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71  if../*.** The sq
27bc2 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65  lite3KeywordCode
27bc3 20 66 75 6e 63 74 69 6f 6e 20 6c 6f 6f 6b 73 20   function looks 
27bc4 75 70 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72  up an identifier
27bc5 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66   to determine if
27bc6 0a 2a 2a 20 69 74 20 69 73 20 61 20 6b 65 79 77  .** it is a keyw
27bc7 6f 72 64 2e 20 20 49 66 20 69 74 20 69 73 20 61  ord.  If it is a
27bc8 20 6b 65 79 77 6f 72 64 2c 20 74 68 65 20 74 6f   keyword, the to
27bc9 6b 65 6e 20 63 6f 64 65 20 6f 66 20 74 68 61 74  ken code of that
27bca 20 6b 65 79 77 6f 72 64 20 69 73 20 0a 2a 2a 20   keyword is .** 
27bcb 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74 68  returned.  If th
27bcc 65 20 69 6e 70 75 74 20 69 73 20 6e 6f 74 20 61  e input is not a
27bcd 20 6b 65 79 77 6f 72 64 2c 20 54 4b 5f 49 44 20   keyword, TK_ID 
27bce 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
27bcf 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74  ** The implement
27bd0 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 72 6f  ation of this ro
27bd1 75 74 69 6e 65 20 77 61 73 20 67 65 6e 65 72 61  utine was genera
27bd2 74 65 64 20 62 79 20 61 20 70 72 6f 67 72 61 6d  ted by a program
27bd3 2c 0a 2a 2a 20 6d 6b 6b 65 79 77 6f 72 64 68 61  ,.** mkkeywordha
27bd4 73 68 2e 68 2c 20 6c 6f 63 61 74 65 64 20 69 6e  sh.h, located in
27bd5 20 74 68 65 20 74 6f 6f 6c 20 73 75 62 64 69 72   the tool subdir
27bd6 65 63 74 6f 72 79 20 6f 66 20 74 68 65 20 64 69  ectory of the di
27bd7 73 74 72 69 62 75 74 69 6f 6e 2e 0a 2a 2a 20 54  stribution..** T
27bd8 68 65 20 6f 75 74 70 75 74 20 6f 66 20 74 68 65  he output of the
27bd9 20 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68 2e 63   mkkeywordhash.c
27bda 20 70 72 6f 67 72 61 6d 20 69 73 20 77 72 69 74   program is writ
27bdb 74 65 6e 20 69 6e 74 6f 20 61 20 66 69 6c 65 0a  ten into a file.
27bdc 2a 2a 20 6e 61 6d 65 64 20 6b 65 79 77 6f 72 64  ** named keyword
27bdd 68 61 73 68 2e 68 20 61 6e 64 20 74 68 65 6e 20  hash.h and then 
27bde 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68  included into th
27bdf 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 20 62  is source file b
27be0 79 0a 2a 2a 20 74 68 65 20 23 69 6e 63 6c 75 64  y.** the #includ
27be1 65 20 62 65 6c 6f 77 2e 0a 2a 2f 0a 2f 2a 2a 2a  e below..*/./***
27be2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
27be3 75 64 65 20 6b 65 79 77 6f 72 64 68 61 73 68 2e  ude keywordhash.
27be4 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
27be5 6f 66 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a  of tokenize.c **
27be6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
27be7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
27be8 6e 20 66 69 6c 65 20 6b 65 79 77 6f 72 64 68 61  n file keywordha
27be9 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  sh.h ***********
27bea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27beb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
27bec 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
27bed 74 61 69 6e 73 20 61 75 74 6f 6d 61 74 69 63 61  tains automatica
27bee 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20 63 6f  lly generated co
27bef 64 65 20 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  de ******.**.** 
27bf0 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73  The code in this
27bf1 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 61   file has been a
27bf2 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e  utomatically gen
27bf3 65 72 61 74 65 64 20 62 79 0a 2a 2a 0a 2a 2a 20  erated by.**.** 
27bf4 20 20 20 20 24 48 65 61 64 65 72 3a 20 2f 73 71      $Header: /sq
27bf5 6c 69 74 65 2f 73 71 6c 69 74 65 2f 74 6f 6f 6c  lite/sqlite/tool
27bf6 2f 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68 2e 63  /mkkeywordhash.c
27bf7 2c 76 20 31 2e 33 31 20 32 30 30 37 2f 30 37 2f  ,v 1.31 2007/07/
27bf8 33 30 20 31 38 3a 32 36 3a 32 30 20 72 73 65 20  30 18:26:20 rse 
27bf9 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 65 20  Exp $.**.** The 
27bfa 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c  code in this fil
27bfb 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 66  e implements a f
27bfc 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 64 65 74  unction that det
27bfd 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65 72 0a  ermines whether.
27bfe 2a 2a 20 6f 72 20 6e 6f 74 20 61 20 67 69 76 65  ** or not a give
27bff 6e 20 69 64 65 6e 74 69 66 69 65 72 20 69 73 20  n identifier is 
27c00 72 65 61 6c 6c 79 20 61 6e 20 53 51 4c 20 6b 65  really an SQL ke
27c01 79 77 6f 72 64 2e 20 20 54 68 65 20 73 61 6d 65  yword.  The same
27c02 20 74 68 69 6e 67 0a 2a 2a 20 6d 69 67 68 74 20   thing.** might 
27c03 62 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 6d  be implemented m
27c04 6f 72 65 20 64 69 72 65 63 74 6c 79 20 75 73 69  ore directly usi
27c05 6e 67 20 61 20 68 61 6e 64 2d 77 72 69 74 74 65  ng a hand-writte
27c06 6e 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2a  n hash table..**
27c07 20 42 75 74 20 62 79 20 75 73 69 6e 67 20 74 68   But by using th
27c08 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  is automatically
27c09 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 2c   generated code,
27c0a 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
27c0b 20 63 6f 64 65 0a 2a 2a 20 69 73 20 73 75 62 73   code.** is subs
27c0c 74 61 6e 74 69 61 6c 6c 79 20 72 65 64 75 63 65  tantially reduce
27c0d 64 2e 20 20 54 68 69 73 20 69 73 20 69 6d 70 6f  d.  This is impo
27c0e 72 74 61 6e 74 20 66 6f 72 20 65 6d 62 65 64 64  rtant for embedd
27c0f 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a  ed applications.
27c10 2a 2a 20 6f 6e 20 70 6c 61 74 66 6f 72 6d 73 20  ** on platforms 
27c11 77 69 74 68 20 6c 69 6d 69 74 65 64 20 6d 65 6d  with limited mem
27c12 6f 72 79 2e 0a 2a 2f 0a 2f 2a 20 48 61 73 68 20  ory..*/./* Hash 
27c13 73 63 6f 72 65 3a 20 31 36 35 20 2a 2f 0a 73 74  score: 165 */.st
27c14 61 74 69 63 20 69 6e 74 20 6b 65 79 77 6f 72 64  atic int keyword
27c15 43 6f 64 65 28 63 6f 6e 73 74 20 63 68 61 72 20  Code(const char 
27c16 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 2f 2a  *z, int n){.  /*
27c17 20 7a 54 65 78 74 5b 5d 20 65 6e 63 6f 64 65 73   zText[] encodes
27c18 20 37 37 35 20 62 79 74 65 73 20 6f 66 20 6b 65   775 bytes of ke
27c19 79 77 6f 72 64 73 20 69 6e 20 35 32 36 20 62 79  ywords in 526 by
27c1a 74 65 73 20 2a 2f 0a 20 20 73 74 61 74 69 63 20  tes */.  static 
27c1b 63 6f 6e 73 74 20 63 68 61 72 20 7a 54 65 78 74  const char zText
27c1c 5b 35 32 36 5d 20 3d 0a 20 20 20 20 22 42 45 46  [526] =.    "BEF
27c1d 4f 52 45 49 47 4e 4f 52 45 47 45 58 50 4c 41 49  OREIGNOREGEXPLAI
27c1e 4e 53 54 45 41 44 44 45 53 43 41 50 45 41 43 48  NSTEADDESCAPEACH
27c1f 45 43 4b 45 59 43 4f 4e 53 54 52 41 49 4e 54 45  ECKEYCONSTRAINTE
27c20 52 53 45 43 54 41 42 4c 45 46 54 22 0a 20 20 20  RSECTABLEFT".   
27c21 20 22 48 45 4e 44 41 54 41 42 41 53 45 4c 45 43   "HENDATABASELEC
27c22 54 52 41 4e 53 41 43 54 49 4f 4e 41 54 55 52 41  TRANSACTIONATURA
27c23 4c 54 45 52 41 49 53 45 4c 53 45 58 43 45 50 54  LTERAISELSEXCEPT
27c24 52 49 47 47 45 52 45 46 45 52 45 4e 43 45 53 22  RIGGEREFERENCES"
27c25 0a 20 20 20 20 22 55 4e 49 51 55 45 52 59 41 54  .    "UNIQUERYAT
27c26 54 41 43 48 41 56 49 4e 47 52 4f 55 50 44 41 54  TACHAVINGROUPDAT
27c27 45 4d 50 4f 52 41 52 59 42 45 47 49 4e 4e 45 52  EMPORARYBEGINNER
27c28 45 49 4e 44 45 58 43 4c 55 53 49 56 45 58 49 53  EINDEXCLUSIVEXIS
27c29 54 53 42 45 54 57 45 45 4e 22 0a 20 20 20 20 22  TSBETWEEN".    "
27c2a 4f 54 4e 55 4c 4c 49 4b 45 43 41 53 43 41 44 45  OTNULLIKECASCADE
27c2b 46 45 52 52 41 42 4c 45 43 41 53 45 43 4f 4c 4c  FERRABLECASECOLL
27c2c 41 54 45 43 52 45 41 54 45 43 55 52 52 45 4e 54  ATECREATECURRENT
27c2d 5f 44 41 54 45 44 45 4c 45 54 45 44 45 54 41 43  _DATEDELETEDETAC
27c2e 48 22 0a 20 20 20 20 22 49 4d 4d 45 44 49 41 54  H".    "IMMEDIAT
27c2f 45 4a 4f 49 4e 53 45 52 54 4d 41 54 43 48 50 4c  EJOINSERTMATCHPL
27c30 41 4e 41 4c 59 5a 45 50 52 41 47 4d 41 42 4f 52  ANALYZEPRAGMABOR
27c31 54 56 41 4c 55 45 53 56 49 52 54 55 41 4c 49 4d  TVALUESVIRTUALIM
27c32 49 54 57 48 45 4e 22 0a 20 20 20 20 22 57 48 45  ITWHEN".    "WHE
27c33 52 45 4e 41 4d 45 41 46 54 45 52 45 50 4c 41 43  RENAMEAFTEREPLAC
27c34 45 41 4e 44 45 46 41 55 4c 54 41 55 54 4f 49 4e  EANDEFAULTAUTOIN
27c35 43 52 45 4d 45 4e 54 43 41 53 54 43 4f 4c 55 4d  CREMENTCASTCOLUM
27c36 4e 43 4f 4d 4d 49 54 43 4f 4e 46 4c 49 43 54 22  NCOMMITCONFLICT"
27c37 0a 20 20 20 20 22 43 52 4f 53 53 43 55 52 52 45  .    "CROSSCURRE
27c38 4e 54 5f 54 49 4d 45 53 54 41 4d 50 52 49 4d 41  NT_TIMESTAMPRIMA
27c39 52 59 44 45 46 45 52 52 45 44 49 53 54 49 4e 43  RYDEFERREDISTINC
27c3a 54 44 52 4f 50 46 41 49 4c 46 52 4f 4d 46 55 4c  TDROPFAILFROMFUL
27c3b 4c 47 4c 4f 42 22 0a 20 20 20 20 22 59 49 46 49  LGLOB".    "YIFI
27c3c 4e 54 4f 46 46 53 45 54 49 53 4e 55 4c 4c 4f 52  NTOFFSETISNULLOR
27c3d 44 45 52 45 53 54 52 49 43 54 4f 55 54 45 52 49  DERESTRICTOUTERI
27c3e 47 48 54 52 4f 4c 4c 42 41 43 4b 52 4f 57 55 4e  GHTROLLBACKROWUN
27c3f 49 4f 4e 55 53 49 4e 47 56 41 43 55 55 4d 22 0a  IONUSINGVACUUM".
27c40 20 20 20 20 22 56 49 45 57 49 4e 49 54 49 41 4c      "VIEWINITIAL
27c41 4c 59 22 3b 0a 20 20 73 74 61 74 69 63 20 63 6f  LY";.  static co
27c42 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
27c43 72 20 61 48 61 73 68 5b 31 32 37 5d 20 3d 20 7b  r aHash[127] = {
27c44 0a 20 20 20 20 20 20 36 33 2c 20 20 39 32 2c 20  .      63,  92, 
27c45 31 30 39 2c 20 20 36 31 2c 20 20 20 30 2c 20 20  109,  61,   0,  
27c46 33 38 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 36  38,   0,   0,  6
27c47 39 2c 20 20 20 30 2c 20 20 36 34 2c 20 20 20 30  9,   0,  64,   0
27c48 2c 20 20 20 30 2c 0a 20 20 20 20 20 31 30 32 2c  ,   0,.     102,
27c49 20 20 20 34 2c 20 20 36 35 2c 20 20 20 37 2c 20     4,  65,   7, 
27c4a 20 20 30 2c 20 31 30 38 2c 20 20 37 32 2c 20 31    0, 108,  72, 1
27c4b 30 33 2c 20 20 39 39 2c 20 20 20 30 2c 20 20 32  03,  99,   0,  2
27c4c 32 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20  2,   0,   0,.   
27c4d 20 20 31 31 33 2c 20 20 20 30 2c 20 31 31 31 2c    113,   0, 111,
27c4e 20 31 30 36 2c 20 20 20 30 2c 20 20 31 38 2c 20   106,   0,  18, 
27c4f 20 38 30 2c 20 20 20 30 2c 20 20 20 31 2c 20 20   80,   0,   1,  
27c50 20 30 2c 20 20 20 30 2c 20 20 35 36 2c 20 20 35   0,   0,  56,  5
27c51 37 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 35 35  7,.       0,  55
27c52 2c 20 20 31 31 2c 20 20 20 30 2c 20 20 33 33 2c  ,  11,   0,  33,
27c53 20 20 37 37 2c 20 20 38 39 2c 20 20 20 30 2c 20    77,  89,   0, 
27c54 31 31 30 2c 20 20 38 38 2c 20 20 20 30 2c 20 20  110,  88,   0,  
27c55 20 30 2c 20 20 34 35 2c 0a 20 20 20 20 20 20 20   0,  45,.       
27c56 30 2c 20 20 39 30 2c 20 20 35 34 2c 20 20 20 30  0,  90,  54,   0
27c57 2c 20 20 32 30 2c 20 20 20 30 2c 20 31 31 34 2c  ,  20,   0, 114,
27c58 20 20 33 34 2c 20 20 31 39 2c 20 20 20 30 2c 20    34,  19,   0, 
27c59 20 31 30 2c 20 20 39 37 2c 20 20 32 38 2c 0a 20   10,  97,  28,. 
27c5a 20 20 20 20 20 38 33 2c 20 20 20 30 2c 20 20 20       83,   0,   
27c5b 30 2c 20 31 31 36 2c 20 20 39 33 2c 20 20 34 37  0, 116,  93,  47
27c5c 2c 20 31 31 35 2c 20 20 34 31 2c 20 20 31 32 2c  , 115,  41,  12,
27c5d 20 20 34 34 2c 20 20 20 30 2c 20 20 37 38 2c 20    44,   0,  78, 
27c5e 20 20 30 2c 0a 20 20 20 20 20 20 38 37 2c 20 20    0,.      87,  
27c5f 32 39 2c 20 20 20 30 2c 20 20 38 36 2c 20 20 20  29,   0,  86,   
27c60 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 38 32  0,   0,   0,  82
27c61 2c 20 20 37 39 2c 20 20 38 34 2c 20 20 37 35 2c  ,  79,  84,  75,
27c62 20 20 39 36 2c 20 20 20 36 2c 0a 20 20 20 20 20    96,   6,.     
27c63 20 31 34 2c 20 20 39 35 2c 20 20 20 30 2c 20 20   14,  95,   0,  
27c64 36 38 2c 20 20 20 30 2c 20 20 32 31 2c 20 20 37  68,   0,  21,  7
27c65 36 2c 20 20 39 38 2c 20 20 32 37 2c 20 20 20 30  6,  98,  27,   0
27c66 2c 20 31 31 32 2c 20 20 36 37 2c 20 31 30 34 2c  , 112,  67, 104,
27c67 0a 20 20 20 20 20 20 34 39 2c 20 20 34 30 2c 20  .      49,  40, 
27c68 20 37 31 2c 20 20 20 30 2c 20 20 20 30 2c 20 20   71,   0,   0,  
27c69 38 31 2c 20 31 30 30 2c 20 20 20 30 2c 20 31 30  81, 100,   0, 10
27c6a 37 2c 20 20 20 30 2c 20 20 31 35 2c 20 20 20 30  7,   0,  15,   0
27c6b 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 32 34 2c  ,   0,.      24,
27c6c 20 20 20 30 2c 20 20 37 33 2c 20 20 34 32 2c 20     0,  73,  42, 
27c6d 20 35 30 2c 20 20 20 30 2c 20 20 31 36 2c 20 20   50,   0,  16,  
27c6e 34 38 2c 20 20 20 30 2c 20 20 33 37 2c 0a 20 20  48,   0,  37,.  
27c6f 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  };.  static cons
27c70 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
27c71 61 4e 65 78 74 5b 31 31 36 5d 20 3d 20 7b 0a 20  aNext[116] = {. 
27c72 20 20 20 20 20 20 30 2c 20 20 20 30 2c 20 20 20        0,   0,   
27c73 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
27c74 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
27c75 20 20 20 39 2c 20 20 20 30 2c 20 20 20 30 2c 20     9,   0,   0, 
27c76 20 20 30 2c 0a 20 20 20 20 20 20 20 30 2c 20 20    0,.       0,  
27c77 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
27c78 35 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  5,   0,   0,   0
27c79 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
27c7a 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20     0,   0,.     
27c7b 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
27c7c 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
27c7d 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
27c7e 2c 20 20 33 32 2c 20 20 20 30 2c 20 20 20 30 2c  ,  32,   0,   0,
27c7f 0a 20 20 20 20 20 20 31 37 2c 20 20 20 30 2c 20  .      17,   0, 
27c80 20 20 30 2c 20 20 20 30 2c 20 20 33 36 2c 20 20    0,   0,  36,  
27c81 33 39 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 32  39,   0,   0,  2
27c82 35 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 33 31  5,   0,   0,  31
27c83 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 20 30 2c  ,   0,.       0,
27c84 20 20 20 30 2c 20 20 34 33 2c 20 20 35 32 2c 20     0,  43,  52, 
27c85 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
27c86 35 33 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20  53,   0,   0,   
27c87 30 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20  0,   0,   0,.   
27c88 20 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c      0,   0,   0,
27c89 20 20 20 30 2c 20 20 35 31 2c 20 20 20 30 2c 20     0,  51,   0, 
27c8a 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
27c8b 32 36 2c 20 20 20 30 2c 20 20 20 38 2c 20 20 34  26,   0,   8,  4
27c8c 36 2c 0a 20 20 20 20 20 20 20 32 2c 20 20 20 30  6,.       2,   0
27c8d 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
27c8e 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
27c8f 20 20 33 2c 20 20 35 38 2c 20 20 36 36 2c 20 20    3,  58,  66,  
27c90 20 30 2c 20 20 31 33 2c 0a 20 20 20 20 20 20 20   0,  13,.       
27c91 30 2c 20 20 39 31 2c 20 20 38 35 2c 20 20 20 30  0,  91,  85,   0
27c92 2c 20 20 39 34 2c 20 20 20 30 2c 20 20 37 34 2c  ,  94,   0,  74,
27c93 20 20 20 30 2c 20 20 20 30 2c 20 20 36 32 2c 20     0,   0,  62, 
27c94 20 20 30 2c 20 20 33 35 2c 20 31 30 31 2c 0a 20    0,  35, 101,. 
27c95 20 20 20 20 20 20 30 2c 20 20 20 30 2c 20 31 30        0,   0, 10
27c96 35 2c 20 20 32 33 2c 20 20 33 30 2c 20 20 36 30  5,  23,  30,  60
27c97 2c 20 20 37 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,  70,   0,   0,
27c98 20 20 35 39 2c 20 20 20 30 2c 20 20 20 30 2c 0a    59,   0,   0,.
27c99 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f    };.  static co
27c9a 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
27c9b 72 20 61 4c 65 6e 5b 31 31 36 5d 20 3d 20 7b 0a  r aLen[116] = {.
27c9c 20 20 20 20 20 20 20 36 2c 20 20 20 37 2c 20 20         6,   7,  
27c9d 20 33 2c 20 20 20 36 2c 20 20 20 36 2c 20 20 20   3,   6,   6,   
27c9e 37 2c 20 20 20 37 2c 20 20 20 33 2c 20 20 20 34  7,   7,   3,   4
27c9f 2c 20 20 20 36 2c 20 20 20 34 2c 20 20 20 35 2c  ,   6,   4,   5,
27ca0 20 20 20 33 2c 0a 20 20 20 20 20 20 31 30 2c 20     3,.      10, 
27ca1 20 20 39 2c 20 20 20 35 2c 20 20 20 34 2c 20 20    9,   5,   4,  
27ca2 20 34 2c 20 20 20 33 2c 20 20 20 38 2c 20 20 20   4,   3,   8,   
27ca3 32 2c 20 20 20 36 2c 20 20 31 31 2c 20 20 20 32  2,   6,  11,   2
27ca4 2c 20 20 20 37 2c 20 20 20 35 2c 0a 20 20 20 20  ,   7,   5,.    
27ca5 20 20 20 35 2c 20 20 20 34 2c 20 20 20 36 2c 20     5,   4,   6, 
27ca6 20 20 37 2c 20 20 31 30 2c 20 20 20 36 2c 20 20    7,  10,   6,  
27ca7 20 35 2c 20 20 20 36 2c 20 20 20 36 2c 20 20 20   5,   6,   6,   
27ca8 35 2c 20 20 20 36 2c 20 20 20 34 2c 20 20 20 39  5,   6,   4,   9
27ca9 2c 0a 20 20 20 20 20 20 20 32 2c 20 20 20 35 2c  ,.       2,   5,
27caa 20 20 20 35 2c 20 20 20 37 2c 20 20 20 35 2c 20     5,   7,   5, 
27cab 20 20 39 2c 20 20 20 36 2c 20 20 20 37 2c 20 20    9,   6,   7,  
27cac 20 37 2c 20 20 20 33 2c 20 20 20 34 2c 20 20 20   7,   3,   4,   
27cad 34 2c 20 20 20 37 2c 0a 20 20 20 20 20 20 20 33  4,   7,.       3
27cae 2c 20 20 31 30 2c 20 20 20 34 2c 20 20 20 37 2c  ,  10,   4,   7,
27caf 20 20 20 36 2c 20 20 31 32 2c 20 20 20 36 2c 20     6,  12,   6, 
27cb0 20 20 36 2c 20 20 20 39 2c 20 20 20 34 2c 20 20    6,   9,   4,  
27cb1 20 36 2c 20 20 20 35 2c 20 20 20 34 2c 0a 20 20   6,   5,   4,.  
27cb2 20 20 20 20 20 37 2c 20 20 20 36 2c 20 20 20 35       7,   6,   5
27cb3 2c 20 20 20 36 2c 20 20 20 37 2c 20 20 20 35 2c  ,   6,   7,   5,
27cb4 20 20 20 34 2c 20 20 20 35 2c 20 20 20 36 2c 20     4,   5,   6, 
27cb5 20 20 35 2c 20 20 20 37 2c 20 20 20 33 2c 20 20    5,   7,   3,  
27cb6 20 37 2c 0a 20 20 20 20 20 20 31 33 2c 20 20 20   7,.      13,   
27cb7 32 2c 20 20 20 32 2c 20 20 20 34 2c 20 20 20 36  2,   2,   4,   6
27cb8 2c 20 20 20 36 2c 20 20 20 38 2c 20 20 20 35 2c  ,   6,   8,   5,
27cb9 20 20 31 37 2c 20 20 31 32 2c 20 20 20 37 2c 20    17,  12,   7, 
27cba 20 20 38 2c 20 20 20 38 2c 0a 20 20 20 20 20 20    8,   8,.      
27cbb 20 32 2c 20 20 20 34 2c 20 20 20 34 2c 20 20 20   2,   4,   4,   
27cbc 34 2c 20 20 20 34 2c 20 20 20 34 2c 20 20 20 32  4,   4,   4,   2
27cbd 2c 20 20 20 32 2c 20 20 20 34 2c 20 20 20 36 2c  ,   2,   4,   6,
27cbe 20 20 20 32 2c 20 20 20 33 2c 20 20 20 36 2c 0a     2,   3,   6,.
27cbf 20 20 20 20 20 20 20 35 2c 20 20 20 38 2c 20 20         5,   8,  
27cc0 20 35 2c 20 20 20 35 2c 20 20 20 38 2c 20 20 20   5,   5,   8,   
27cc1 33 2c 20 20 20 35 2c 20 20 20 35 2c 20 20 20 36  3,   5,   5,   6
27cc2 2c 20 20 20 34 2c 20 20 20 39 2c 20 20 20 33 2c  ,   4,   9,   3,
27cc3 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63  .  };.  static c
27cc4 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 73 68  onst unsigned sh
27cc5 6f 72 74 20 69 6e 74 20 61 4f 66 66 73 65 74 5b  ort int aOffset[
27cc6 31 31 36 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20  116] = {.       
27cc7 30 2c 20 20 20 32 2c 20 20 20 32 2c 20 20 20 36  0,   2,   2,   6
27cc8 2c 20 20 31 30 2c 20 20 31 33 2c 20 20 31 38 2c  ,  10,  13,  18,
27cc9 20 20 32 33 2c 20 20 32 35 2c 20 20 32 36 2c 20    23,  25,  26, 
27cca 20 33 31 2c 20 20 33 33 2c 20 20 33 37 2c 0a 20   31,  33,  37,. 
27ccb 20 20 20 20 20 34 30 2c 20 20 34 37 2c 20 20 35       40,  47,  5
27ccc 35 2c 20 20 35 38 2c 20 20 36 31 2c 20 20 36 33  5,  58,  61,  63
27ccd 2c 20 20 36 35 2c 20 20 37 30 2c 20 20 37 31 2c  ,  65,  70,  71,
27cce 20 20 37 36 2c 20 20 38 35 2c 20 20 38 36 2c 20    76,  85,  86, 
27ccf 20 39 31 2c 0a 20 20 20 20 20 20 39 35 2c 20 20   91,.      95,  
27cd0 39 39 2c 20 31 30 32 2c 20 31 30 37 2c 20 31 31  99, 102, 107, 11
27cd1 33 2c 20 31 32 33 2c 20 31 32 36 2c 20 31 33 31  3, 123, 126, 131
27cd2 2c 20 31 33 36 2c 20 31 34 31 2c 20 31 34 34 2c  , 136, 141, 144,
27cd3 20 31 34 38 2c 20 31 34 38 2c 0a 20 20 20 20 20   148, 148,.     
27cd4 31 35 32 2c 20 31 35 37 2c 20 31 36 30 2c 20 31  152, 157, 160, 1
27cd5 36 34 2c 20 31 36 36 2c 20 31 36 39 2c 20 31 37  64, 166, 169, 17
27cd6 37 2c 20 31 38 33 2c 20 31 38 39 2c 20 31 38 39  7, 183, 189, 189
27cd7 2c 20 31 39 32 2c 20 31 39 35 2c 20 31 39 39 2c  , 192, 195, 199,
27cd8 0a 20 20 20 20 20 32 30 30 2c 20 32 30 34 2c 20  .     200, 204, 
27cd9 32 31 34 2c 20 32 31 38 2c 20 32 32 35 2c 20 32  214, 218, 225, 2
27cda 33 31 2c 20 32 34 33 2c 20 32 34 39 2c 20 32 35  31, 243, 249, 25
27cdb 35 2c 20 32 36 34 2c 20 32 36 36 2c 20 32 37 32  5, 264, 266, 272
27cdc 2c 20 32 37 37 2c 0a 20 20 20 20 20 32 37 39 2c  , 277,.     279,
27cdd 20 32 38 36 2c 20 32 39 31 2c 20 32 39 36 2c 20   286, 291, 296, 
27cde 33 30 32 2c 20 33 30 38 2c 20 33 31 33 2c 20 33  302, 308, 313, 3
27cdf 31 37 2c 20 33 32 30 2c 20 33 32 36 2c 20 33 33  17, 320, 326, 33
27ce0 30 2c 20 33 33 37 2c 20 33 33 39 2c 0a 20 20 20  0, 337, 339,.   
27ce1 20 20 33 34 36 2c 20 33 34 38 2c 20 33 35 30 2c    346, 348, 350,
27ce2 20 33 35 39 2c 20 33 36 33 2c 20 33 36 39 2c 20   359, 363, 369, 
27ce3 33 37 35 2c 20 33 38 33 2c 20 33 38 38 2c 20 33  375, 383, 388, 3
27ce4 38 38 2c 20 34 30 34 2c 20 34 31 31 2c 20 34 31  88, 404, 411, 41
27ce5 38 2c 0a 20 20 20 20 20 34 31 39 2c 20 34 32 36  8,.     419, 426
27ce6 2c 20 34 33 30 2c 20 34 33 34 2c 20 34 33 38 2c  , 430, 434, 438,
27ce7 20 34 34 32 2c 20 34 34 35 2c 20 34 34 37 2c 20   442, 445, 447, 
27ce8 34 34 39 2c 20 34 35 32 2c 20 34 35 32 2c 20 34  449, 452, 452, 4
27ce9 35 35 2c 20 34 35 38 2c 0a 20 20 20 20 20 34 36  55, 458,.     46
27cea 34 2c 20 34 36 38 2c 20 34 37 36 2c 20 34 38 30  4, 468, 476, 480
27ceb 2c 20 34 38 35 2c 20 34 39 33 2c 20 34 39 36 2c  , 485, 493, 496,
27cec 20 35 30 31 2c 20 35 30 36 2c 20 35 31 32 2c 20   501, 506, 512, 
27ced 35 31 36 2c 20 35 32 31 2c 0a 20 20 7d 3b 0a 20  516, 521,.  };. 
27cee 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
27cef 73 69 67 6e 65 64 20 63 68 61 72 20 61 43 6f 64  signed char aCod
27cf0 65 5b 31 31 36 5d 20 3d 20 7b 0a 20 20 20 20 54  e[116] = {.    T
27cf1 4b 5f 42 45 46 4f 52 45 2c 20 20 20 20 20 54 4b  K_BEFORE,     TK
27cf2 5f 46 4f 52 45 49 47 4e 2c 20 20 20 20 54 4b 5f  _FOREIGN,    TK_
27cf3 46 4f 52 2c 20 20 20 20 20 20 20 20 54 4b 5f 49  FOR,        TK_I
27cf4 47 4e 4f 52 45 2c 20 20 20 20 20 54 4b 5f 4c 49  GNORE,     TK_LI
27cf5 4b 45 5f 4b 57 2c 20 20 20 20 0a 20 20 20 20 54  KE_KW,    .    T
27cf6 4b 5f 45 58 50 4c 41 49 4e 2c 20 20 20 20 54 4b  K_EXPLAIN,    TK
27cf7 5f 49 4e 53 54 45 41 44 2c 20 20 20 20 54 4b 5f  _INSTEAD,    TK_
27cf8 41 44 44 2c 20 20 20 20 20 20 20 20 54 4b 5f 44  ADD,        TK_D
27cf9 45 53 43 2c 20 20 20 20 20 20 20 54 4b 5f 45 53  ESC,       TK_ES
27cfa 43 41 50 45 2c 20 20 20 20 20 0a 20 20 20 20 54  CAPE,     .    T
27cfb 4b 5f 45 41 43 48 2c 20 20 20 20 20 20 20 54 4b  K_EACH,       TK
27cfc 5f 43 48 45 43 4b 2c 20 20 20 20 20 20 54 4b 5f  _CHECK,      TK_
27cfd 4b 45 59 2c 20 20 20 20 20 20 20 20 54 4b 5f 43  KEY,        TK_C
27cfe 4f 4e 53 54 52 41 49 4e 54 2c 20 54 4b 5f 49 4e  ONSTRAINT, TK_IN
27cff 54 45 52 53 45 43 54 2c 20 20 0a 20 20 20 20 54  TERSECT,  .    T
27d00 4b 5f 54 41 42 4c 45 2c 20 20 20 20 20 20 54 4b  K_TABLE,      TK
27d01 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f  _JOIN_KW,    TK_
27d02 54 48 45 4e 2c 20 20 20 20 20 20 20 54 4b 5f 45  THEN,       TK_E
27d03 4e 44 2c 20 20 20 20 20 20 20 20 54 4b 5f 44 41  ND,        TK_DA
27d04 54 41 42 41 53 45 2c 20 20 20 0a 20 20 20 20 54  TABASE,   .    T
27d05 4b 5f 41 53 2c 20 20 20 20 20 20 20 20 20 54 4b  K_AS,         TK
27d06 5f 53 45 4c 45 43 54 2c 20 20 20 20 20 54 4b 5f  _SELECT,     TK_
27d07 54 52 41 4e 53 41 43 54 49 4f 4e 2c 54 4b 5f 4f  TRANSACTION,TK_O
27d08 4e 2c 20 20 20 20 20 20 20 20 20 54 4b 5f 4a 4f  N,         TK_JO
27d09 49 4e 5f 4b 57 2c 20 20 20 20 0a 20 20 20 20 54  IN_KW,    .    T
27d0a 4b 5f 41 4c 54 45 52 2c 20 20 20 20 20 20 54 4b  K_ALTER,      TK
27d0b 5f 52 41 49 53 45 2c 20 20 20 20 20 20 54 4b 5f  _RAISE,      TK_
27d0c 45 4c 53 45 2c 20 20 20 20 20 20 20 54 4b 5f 45  ELSE,       TK_E
27d0d 58 43 45 50 54 2c 20 20 20 20 20 54 4b 5f 54 52  XCEPT,     TK_TR
27d0e 49 47 47 45 52 2c 20 20 20 20 0a 20 20 20 20 54  IGGER,    .    T
27d0f 4b 5f 52 45 46 45 52 45 4e 43 45 53 2c 20 54 4b  K_REFERENCES, TK
27d10 5f 55 4e 49 51 55 45 2c 20 20 20 20 20 54 4b 5f  _UNIQUE,     TK_
27d11 51 55 45 52 59 2c 20 20 20 20 20 20 54 4b 5f 41  QUERY,      TK_A
27d12 54 54 41 43 48 2c 20 20 20 20 20 54 4b 5f 48 41  TTACH,     TK_HA
27d13 56 49 4e 47 2c 20 20 20 20 20 0a 20 20 20 20 54  VING,     .    T
27d14 4b 5f 47 52 4f 55 50 2c 20 20 20 20 20 20 54 4b  K_GROUP,      TK
27d15 5f 55 50 44 41 54 45 2c 20 20 20 20 20 54 4b 5f  _UPDATE,     TK_
27d16 54 45 4d 50 2c 20 20 20 20 20 20 20 54 4b 5f 54  TEMP,       TK_T
27d17 45 4d 50 2c 20 20 20 20 20 20 20 54 4b 5f 4f 52  EMP,       TK_OR
27d18 2c 20 20 20 20 20 20 20 20 20 0a 20 20 20 20 54  ,         .    T
27d19 4b 5f 42 45 47 49 4e 2c 20 20 20 20 20 20 54 4b  K_BEGIN,      TK
27d1a 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f  _JOIN_KW,    TK_
27d1b 52 45 49 4e 44 45 58 2c 20 20 20 20 54 4b 5f 49  REINDEX,    TK_I
27d1c 4e 44 45 58 2c 20 20 20 20 20 20 54 4b 5f 45 58  NDEX,      TK_EX
27d1d 43 4c 55 53 49 56 45 2c 20 20 0a 20 20 20 20 54  CLUSIVE,  .    T
27d1e 4b 5f 45 58 49 53 54 53 2c 20 20 20 20 20 54 4b  K_EXISTS,     TK
27d1f 5f 42 45 54 57 45 45 4e 2c 20 20 20 20 54 4b 5f  _BETWEEN,    TK_
27d20 4e 4f 54 4e 55 4c 4c 2c 20 20 20 20 54 4b 5f 4e  NOTNULL,    TK_N
27d21 4f 54 2c 20 20 20 20 20 20 20 20 54 4b 5f 4e 55  OT,        TK_NU
27d22 4c 4c 2c 20 20 20 20 20 20 20 0a 20 20 20 20 54  LL,       .    T
27d23 4b 5f 4c 49 4b 45 5f 4b 57 2c 20 20 20 20 54 4b  K_LIKE_KW,    TK
27d24 5f 43 41 53 43 41 44 45 2c 20 20 20 20 54 4b 5f  _CASCADE,    TK_
27d25 41 53 43 2c 20 20 20 20 20 20 20 20 54 4b 5f 44  ASC,        TK_D
27d26 45 46 45 52 52 41 42 4c 45 2c 20 54 4b 5f 43 41  EFERRABLE, TK_CA
27d27 53 45 2c 20 20 20 20 20 20 20 0a 20 20 20 20 54  SE,       .    T
27d28 4b 5f 43 4f 4c 4c 41 54 45 2c 20 20 20 20 54 4b  K_COLLATE,    TK
27d29 5f 43 52 45 41 54 45 2c 20 20 20 20 20 54 4b 5f  _CREATE,     TK_
27d2a 43 54 49 4d 45 5f 4b 57 2c 20 20 20 54 4b 5f 44  CTIME_KW,   TK_D
27d2b 45 4c 45 54 45 2c 20 20 20 20 20 54 4b 5f 44 45  ELETE,     TK_DE
27d2c 54 41 43 48 2c 20 20 20 20 20 0a 20 20 20 20 54  TACH,     .    T
27d2d 4b 5f 49 4d 4d 45 44 49 41 54 45 2c 20 20 54 4b  K_IMMEDIATE,  TK
27d2e 5f 4a 4f 49 4e 2c 20 20 20 20 20 20 20 54 4b 5f  _JOIN,       TK_
27d2f 49 4e 53 45 52 54 2c 20 20 20 20 20 54 4b 5f 4d  INSERT,     TK_M
27d30 41 54 43 48 2c 20 20 20 20 20 20 54 4b 5f 50 4c  ATCH,      TK_PL
27d31 41 4e 2c 20 20 20 20 20 20 20 0a 20 20 20 20 54  AN,       .    T
27d32 4b 5f 41 4e 41 4c 59 5a 45 2c 20 20 20 20 54 4b  K_ANALYZE,    TK
27d33 5f 50 52 41 47 4d 41 2c 20 20 20 20 20 54 4b 5f  _PRAGMA,     TK_
27d34 41 42 4f 52 54 2c 20 20 20 20 20 20 54 4b 5f 56  ABORT,      TK_V
27d35 41 4c 55 45 53 2c 20 20 20 20 20 54 4b 5f 56 49  ALUES,     TK_VI
27d36 52 54 55 41 4c 2c 20 20 20 20 0a 20 20 20 20 54  RTUAL,    .    T
27d37 4b 5f 4c 49 4d 49 54 2c 20 20 20 20 20 20 54 4b  K_LIMIT,      TK
27d38 5f 57 48 45 4e 2c 20 20 20 20 20 20 20 54 4b 5f  _WHEN,       TK_
27d39 57 48 45 52 45 2c 20 20 20 20 20 20 54 4b 5f 52  WHERE,      TK_R
27d3a 45 4e 41 4d 45 2c 20 20 20 20 20 54 4b 5f 41 46  ENAME,     TK_AF
27d3b 54 45 52 2c 20 20 20 20 20 20 0a 20 20 20 20 54  TER,      .    T
27d3c 4b 5f 52 45 50 4c 41 43 45 2c 20 20 20 20 54 4b  K_REPLACE,    TK
27d3d 5f 41 4e 44 2c 20 20 20 20 20 20 20 20 54 4b 5f  _AND,        TK_
27d3e 44 45 46 41 55 4c 54 2c 20 20 20 20 54 4b 5f 41  DEFAULT,    TK_A
27d3f 55 54 4f 49 4e 43 52 2c 20 20 20 54 4b 5f 54 4f  UTOINCR,   TK_TO
27d40 2c 20 20 20 20 20 20 20 20 20 0a 20 20 20 20 54  ,         .    T
27d41 4b 5f 49 4e 2c 20 20 20 20 20 20 20 20 20 54 4b  K_IN,         TK
27d42 5f 43 41 53 54 2c 20 20 20 20 20 20 20 54 4b 5f  _CAST,       TK_
27d43 43 4f 4c 55 4d 4e 4b 57 2c 20 20 20 54 4b 5f 43  COLUMNKW,   TK_C
27d44 4f 4d 4d 49 54 2c 20 20 20 20 20 54 4b 5f 43 4f  OMMIT,     TK_CO
27d45 4e 46 4c 49 43 54 2c 20 20 20 0a 20 20 20 20 54  NFLICT,   .    T
27d46 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b  K_JOIN_KW,    TK
27d47 5f 43 54 49 4d 45 5f 4b 57 2c 20 20 20 54 4b 5f  _CTIME_KW,   TK_
27d48 43 54 49 4d 45 5f 4b 57 2c 20 20 20 54 4b 5f 50  CTIME_KW,   TK_P
27d49 52 49 4d 41 52 59 2c 20 20 20 20 54 4b 5f 44 45  RIMARY,    TK_DE
27d4a 46 45 52 52 45 44 2c 20 20 20 0a 20 20 20 20 54  FERRED,   .    T
27d4b 4b 5f 44 49 53 54 49 4e 43 54 2c 20 20 20 54 4b  K_DISTINCT,   TK
27d4c 5f 49 53 2c 20 20 20 20 20 20 20 20 20 54 4b 5f  _IS,         TK_
27d4d 44 52 4f 50 2c 20 20 20 20 20 20 20 54 4b 5f 46  DROP,       TK_F
27d4e 41 49 4c 2c 20 20 20 20 20 20 20 54 4b 5f 46 52  AIL,       TK_FR
27d4f 4f 4d 2c 20 20 20 20 20 20 20 0a 20 20 20 20 54  OM,       .    T
27d50 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b  K_JOIN_KW,    TK
27d51 5f 4c 49 4b 45 5f 4b 57 2c 20 20 20 20 54 4b 5f  _LIKE_KW,    TK_
27d52 42 59 2c 20 20 20 20 20 20 20 20 20 54 4b 5f 49  BY,         TK_I
27d53 46 2c 20 20 20 20 20 20 20 20 20 54 4b 5f 49 4e  F,         TK_IN
27d54 54 4f 2c 20 20 20 20 20 20 20 0a 20 20 20 20 54  TO,       .    T
27d55 4b 5f 4f 46 46 53 45 54 2c 20 20 20 20 20 54 4b  K_OFFSET,     TK
27d56 5f 4f 46 2c 20 20 20 20 20 20 20 20 20 54 4b 5f  _OF,         TK_
27d57 53 45 54 2c 20 20 20 20 20 20 20 20 54 4b 5f 49  SET,        TK_I
27d58 53 4e 55 4c 4c 2c 20 20 20 20 20 54 4b 5f 4f 52  SNULL,     TK_OR
27d59 44 45 52 2c 20 20 20 20 20 20 0a 20 20 20 20 54  DER,      .    T
27d5a 4b 5f 52 45 53 54 52 49 43 54 2c 20 20 20 54 4b  K_RESTRICT,   TK
27d5b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f  _JOIN_KW,    TK_
27d5c 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f 52  JOIN_KW,    TK_R
27d5d 4f 4c 4c 42 41 43 4b 2c 20 20 20 54 4b 5f 52 4f  OLLBACK,   TK_RO
27d5e 57 2c 20 20 20 20 20 20 20 20 0a 20 20 20 20 54  W,        .    T
27d5f 4b 5f 55 4e 49 4f 4e 2c 20 20 20 20 20 20 54 4b  K_UNION,      TK
27d60 5f 55 53 49 4e 47 2c 20 20 20 20 20 20 54 4b 5f  _USING,      TK_
27d61 56 41 43 55 55 4d 2c 20 20 20 20 20 54 4b 5f 56  VACUUM,     TK_V
27d62 49 45 57 2c 20 20 20 20 20 20 20 54 4b 5f 49 4e  IEW,       TK_IN
27d63 49 54 49 41 4c 4c 59 2c 20 20 0a 20 20 20 20 54  ITIALLY,  .    T
27d64 4b 5f 41 4c 4c 2c 20 20 20 20 20 20 20 20 0a 20  K_ALL,        . 
27d65 20 7d 3b 0a 20 20 69 6e 74 20 68 2c 20 69 3b 0a   };.  int h, i;.
27d66 20 20 69 66 28 20 6e 3c 32 20 29 20 72 65 74 75    if( n<2 ) retu
27d67 72 6e 20 54 4b 5f 49 44 3b 0a 20 20 68 20 3d 20  rn TK_ID;.  h = 
27d68 28 28 63 68 61 72 4d 61 70 28 7a 5b 30 5d 29 2a  ((charMap(z[0])*
27d69 34 29 20 5e 0a 20 20 20 20 20 20 28 63 68 61 72  4) ^.      (char
27d6a 4d 61 70 28 7a 5b 6e 2d 31 5d 29 2a 33 29 20 5e  Map(z[n-1])*3) ^
27d6b 0a 20 20 20 20 20 20 6e 29 20 25 20 31 32 37 3b  .      n) % 127;
27d6c 0a 20 20 66 6f 72 28 69 3d 28 28 69 6e 74 29 61  .  for(i=((int)a
27d6d 48 61 73 68 5b 68 5d 29 2d 31 3b 20 69 3e 3d 30  Hash[h])-1; i>=0
27d6e 3b 20 69 3d 28 28 69 6e 74 29 61 4e 65 78 74 5b  ; i=((int)aNext[
27d6f 69 5d 29 2d 31 29 7b 0a 20 20 20 20 69 66 28 20  i])-1){.    if( 
27d70 61 4c 65 6e 5b 69 5d 3d 3d 6e 20 26 26 20 73 71  aLen[i]==n && sq
27d71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 26 7a  lite3StrNICmp(&z
27d72 54 65 78 74 5b 61 4f 66 66 73 65 74 5b 69 5d 5d  Text[aOffset[i]]
27d73 2c 7a 2c 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ,z,n)==0 ){.    
27d74 20 20 72 65 74 75 72 6e 20 61 43 6f 64 65 5b 69    return aCode[i
27d75 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  ];.    }.  }.  r
27d76 65 74 75 72 6e 20 54 4b 5f 49 44 3b 0a 7d 0a 53  eturn TK_ID;.}.S
27d77 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
27d78 74 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64  t sqlite3Keyword
27d79 43 6f 64 65 28 63 6f 6e 73 74 20 75 6e 73 69 67  Code(const unsig
27d7a 6e 65 64 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  ned char *z, int
27d7b 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20 6b 65   n){.  return ke
27d7c 79 77 6f 72 64 43 6f 64 65 28 28 63 68 61 72 2a  ywordCode((char*
27d7d 29 7a 2c 20 6e 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  )z, n);.}../****
27d7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
27d7f 66 20 6b 65 79 77 6f 72 64 68 61 73 68 2e 68 20  f keywordhash.h 
27d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27d81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27d82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
27d83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
27d84 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
27d85 65 66 74 20 6f 66 66 20 69 6e 20 74 6f 6b 65 6e  eft off in token
27d86 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ize.c **********
27d87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 0a  *********/.../*.
27d88 2a 2a 20 49 66 20 58 20 69 73 20 61 20 63 68 61  ** If X is a cha
27d89 72 61 63 74 65 72 20 74 68 61 74 20 63 61 6e 20  racter that can 
27d8a 62 65 20 75 73 65 64 20 69 6e 20 61 6e 20 69 64  be used in an id
27d8b 65 6e 74 69 66 69 65 72 20 74 68 65 6e 0a 2a 2a  entifier then.**
27d8c 20 49 64 43 68 61 72 28 58 29 20 77 69 6c 6c 20   IdChar(X) will 
27d8d 62 65 20 74 72 75 65 2e 20 20 4f 74 68 65 72 77  be true.  Otherw
27d8e 69 73 65 20 69 74 20 69 73 20 66 61 6c 73 65 2e  ise it is false.
27d8f 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 41 53 43 49 49  .**.** For ASCII
27d90 2c 20 61 6e 79 20 63 68 61 72 61 63 74 65 72 20  , any character 
27d91 77 69 74 68 20 74 68 65 20 68 69 67 68 2d 6f 72  with the high-or
27d92 64 65 72 20 62 69 74 20 73 65 74 20 69 73 0a 2a  der bit set is.*
27d93 2a 20 61 6c 6c 6f 77 65 64 20 69 6e 20 61 6e 20  * allowed in an 
27d94 69 64 65 6e 74 69 66 69 65 72 2e 20 20 46 6f 72  identifier.  For
27d95 20 37 2d 62 69 74 20 63 68 61 72 61 63 74 65 72   7-bit character
27d96 73 2c 20 0a 2a 2a 20 73 71 6c 69 74 65 33 49 73  s, .** sqlite3Is
27d97 49 64 43 68 61 72 5b 58 5d 20 6d 75 73 74 20 62  IdChar[X] must b
27d98 65 20 31 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 45  e 1..**.** For E
27d99 42 43 44 49 43 2c 20 74 68 65 20 72 75 6c 65 73  BCDIC, the rules
27d9a 20 61 72 65 20 6d 6f 72 65 20 63 6f 6d 70 6c 65   are more comple
27d9b 78 20 62 75 74 20 68 61 76 65 20 74 68 65 20 73  x but have the s
27d9c 61 6d 65 0a 2a 2a 20 65 6e 64 20 72 65 73 75 6c  ame.** end resul
27d9d 74 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20  t..**.** Ticket 
27d9e 23 31 30 36 36 2e 20 20 74 68 65 20 53 51 4c 20  #1066.  the SQL 
27d9f 73 74 61 6e 64 61 72 64 20 64 6f 65 73 20 6e 6f  standard does no
27da0 74 20 61 6c 6c 6f 77 20 27 24 27 20 69 6e 20 74  t allow '$' in t
27da1 68 65 0a 2a 2a 20 6d 69 64 64 6c 65 20 6f 66 20  he.** middle of 
27da2 69 64 65 6e 74 66 69 65 72 73 2e 20 20 42 75 74  identfiers.  But
27da3 20 6d 61 6e 79 20 53 51 4c 20 69 6d 70 6c 65 6d   many SQL implem
27da4 65 6e 74 61 74 69 6f 6e 73 20 64 6f 2e 20 0a 2a  entations do. .*
27da5 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c  * SQLite will al
27da6 6c 6f 77 20 27 24 27 20 69 6e 20 69 64 65 6e 74  low '$' in ident
27da7 69 66 69 65 72 73 20 66 6f 72 20 63 6f 6d 70 61  ifiers for compa
27da8 74 69 62 69 6c 69 74 79 2e 0a 2a 2a 20 42 75 74  tibility..** But
27da9 20 74 68 65 20 66 65 61 74 75 72 65 20 69 73 20   the feature is 
27daa 75 6e 64 6f 63 75 6d 65 6e 74 65 64 2e 0a 2a 2f  undocumented..*/
27dab 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41  .#ifdef SQLITE_A
27dac 53 43 49 49 0a 53 51 4c 49 54 45 5f 50 52 49 56  SCII.SQLITE_PRIV
27dad 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 73  ATE const char s
27dae 71 6c 69 74 65 33 49 73 41 73 63 69 69 49 64 43  qlite3IsAsciiIdC
27daf 68 61 72 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30 20  har[] = {./* x0 
27db0 78 31 20 78 32 20 78 33 20 78 34 20 78 35 20 78  x1 x2 x3 x4 x5 x
27db1 36 20 78 37 20 78 38 20 78 39 20 78 41 20 78 42  6 x7 x8 x9 xA xB
27db2 20 78 43 20 78 44 20 78 45 20 78 46 20 2a 2f 0a   xC xD xE xF */.
27db3 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20      0, 0, 0, 0, 
27db4 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 0, 0, 0, 0, 0
27db5 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
27db6 20 30 2c 20 20 2f 2a 20 32 78 20 2a 2f 0a 20 20   0,  /* 2x */.  
27db7 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    1, 1, 1, 1, 1,
27db8 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
27db9 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
27dba 2c 20 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 20  ,  /* 3x */.    
27dbb 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
27dbc 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
27dbd 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
27dbe 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20 31 2c   /* 4x */.    1,
27dbf 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
27dc0 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 1, 1, 1, 1, 0
27dc1 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 20 2f  , 0, 0, 0, 1,  /
27dc2 2a 20 35 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31  * 5x */.    0, 1
27dc3 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
27dc4 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
27dc5 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20  1, 1, 1, 1,  /* 
27dc6 36 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20  6x */.    1, 1, 
27dc7 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
27dc8 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c  , 1, 1, 1, 0, 0,
27dc9 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 37 78   0, 0, 0,  /* 7x
27dca 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 49   */.};.#define I
27dcb 64 43 68 61 72 28 43 29 20 20 28 28 28 63 3d 43  dChar(C)  (((c=C
27dcc 29 26 30 78 38 30 29 21 3d 30 20 7c 7c 20 28 63  )&0x80)!=0 || (c
27dcd 3e 30 78 31 66 20 26 26 20 73 71 6c 69 74 65 33  >0x1f && sqlite3
27dce 49 73 41 73 63 69 69 49 64 43 68 61 72 5b 63 2d  IsAsciiIdChar[c-
27dcf 30 78 32 30 5d 29 29 0a 23 65 6e 64 69 66 0a 23  0x20])).#endif.#
27dd0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43  ifdef SQLITE_EBC
27dd1 44 49 43 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  DIC.SQLITE_PRIVA
27dd2 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71  TE const char sq
27dd3 6c 69 74 65 33 49 73 45 62 63 64 69 63 49 64 43  lite3IsEbcdicIdC
27dd4 68 61 72 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30 20  har[] = {./* x0 
27dd5 78 31 20 78 32 20 78 33 20 78 34 20 78 35 20 78  x1 x2 x3 x4 x5 x
27dd6 36 20 78 37 20 78 38 20 78 39 20 78 41 20 78 42  6 x7 x8 x9 xA xB
27dd7 20 78 43 20 78 44 20 78 45 20 78 46 20 2a 2f 0a   xC xD xE xF */.
27dd8 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20      0, 0, 1, 1, 
27dd9 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
27dda 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
27ddb 20 30 2c 20 20 2f 2a 20 34 78 20 2a 2f 0a 20 20   0,  /* 4x */.  
27ddc 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    0, 1, 1, 1, 1,
27ddd 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
27dde 30 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 1, 0, 0, 0, 0
27ddf 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20  ,  /* 5x */.    
27de0 30 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 0, 1, 1, 1, 1
27de1 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
27de2 20 30 2c 20 30 2c 20 31 2c 20 30 2c 20 30 2c 20   0, 0, 1, 0, 0, 
27de3 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 20 30 2c   /* 6x */.    0,
27de4 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
27de5 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30  1, 1, 1, 0, 0, 0
27de6 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f  , 0, 0, 0, 0,  /
27de7 2a 20 37 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31  * 7x */.    0, 1
27de8 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
27de9 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20   1, 1, 1, 0, 0, 
27dea 31 2c 20 31 2c 20 31 2c 20 30 2c 20 20 2f 2a 20  1, 1, 1, 0,  /* 
27deb 38 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20  8x */.    0, 1, 
27dec 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
27ded 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 31 2c  , 1, 1, 0, 0, 1,
27dee 20 30 2c 20 31 2c 20 30 2c 20 20 2f 2a 20 39 78   0, 1, 0,  /* 9x
27def 20 2a 2f 0a 20 20 20 20 31 2c 20 30 2c 20 31 2c   */.    1, 0, 1,
27df0 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
27df1 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31  1, 1, 1, 0, 1, 1
27df2 2c 20 31 2c 20 30 2c 20 20 2f 2a 20 41 78 20 2a  , 1, 0,  /* Ax *
27df3 2f 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30  /.    0, 0, 0, 0
27df4 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
27df5 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
27df6 30 2c 20 30 2c 20 20 2f 2a 20 42 78 20 2a 2f 0a  0, 0,  /* Bx */.
27df7 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20      0, 1, 1, 1, 
27df8 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
27df9 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 0, 1, 1, 1, 1,
27dfa 20 31 2c 20 20 2f 2a 20 43 78 20 2a 2f 0a 20 20   1,  /* Cx */.  
27dfb 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    0, 1, 1, 1, 1,
27dfc 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
27dfd 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
27dfe 2c 20 20 2f 2a 20 44 78 20 2a 2f 0a 20 20 20 20  ,  /* Dx */.    
27dff 30 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 0, 1, 1, 1, 1
27e00 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
27e01 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
27e02 20 2f 2a 20 45 78 20 2a 2f 0a 20 20 20 20 31 2c   /* Ex */.    1,
27e03 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
27e04 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31  1, 1, 1, 1, 0, 1
27e05 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 20 2f  , 1, 1, 1, 0,  /
27e06 2a 20 46 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69  * Fx */.};.#defi
27e07 6e 65 20 49 64 43 68 61 72 28 43 29 20 20 28 28  ne IdChar(C)  ((
27e08 28 63 3d 43 29 3e 3d 30 78 34 32 20 26 26 20 73  (c=C)>=0x42 && s
27e09 71 6c 69 74 65 33 49 73 45 62 63 64 69 63 49 64  qlite3IsEbcdicId
27e0a 43 68 61 72 5b 63 2d 30 78 34 30 5d 29 29 0a 23  Char[c-0x40])).#
27e0b 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65  endif.../*.** Re
27e0c 74 75 72 6e 20 74 68 65 20 6c 65 6e 67 74 68 20  turn the length 
27e0d 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 61  of the token tha
27e0e 74 20 62 65 67 69 6e 73 20 61 74 20 7a 5b 30 5d  t begins at z[0]
27e0f 2e 20 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20  . .** Store the 
27e10 74 6f 6b 65 6e 20 74 79 70 65 20 69 6e 20 2a 74  token type in *t
27e11 6f 6b 65 6e 54 79 70 65 20 62 65 66 6f 72 65 20  okenType before 
27e12 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51  returning..*/.SQ
27e13 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
27e14 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e   sqlite3GetToken
27e15 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20  (const unsigned 
27e16 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 74 6f  char *z, int *to
27e17 6b 65 6e 54 79 70 65 29 7b 0a 20 20 69 6e 74 20  kenType){.  int 
27e18 69 2c 20 63 3b 0a 20 20 73 77 69 74 63 68 28 20  i, c;.  switch( 
27e19 2a 7a 20 29 7b 0a 20 20 20 20 63 61 73 65 20 27  *z ){.    case '
27e1a 20 27 3a 20 63 61 73 65 20 27 5c 74 27 3a 20 63   ': case '\t': c
27e1b 61 73 65 20 27 5c 6e 27 3a 20 63 61 73 65 20 27  ase '\n': case '
27e1c 5c 66 27 3a 20 63 61 73 65 20 27 5c 72 27 3a 20  \f': case '\r': 
27e1d 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b  {.      for(i=1;
27e1e 20 69 73 73 70 61 63 65 28 7a 5b 69 5d 29 3b 20   isspace(z[i]); 
27e1f 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 2a 74 6f  i++){}.      *to
27e20 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53 50 41  kenType = TK_SPA
27e21 43 45 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  CE;.      return
27e22 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   i;.    }.    ca
27e23 73 65 20 27 2d 27 3a 20 7b 0a 20 20 20 20 20 20  se '-': {.      
27e24 69 66 28 20 7a 5b 31 5d 3d 3d 27 2d 27 20 29 7b  if( z[1]=='-' ){
27e25 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 32  .        for(i=2
27e26 3b 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 20 26 26  ; (c=z[i])!=0 &&
27e27 20 63 21 3d 27 5c 6e 27 3b 20 69 2b 2b 29 7b 7d   c!='\n'; i++){}
27e28 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
27e29 79 70 65 20 3d 20 54 4b 5f 43 4f 4d 4d 45 4e 54  ype = TK_COMMENT
27e2a 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
27e2b 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   i;.      }.    
27e2c 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
27e2d 4b 5f 4d 49 4e 55 53 3b 0a 20 20 20 20 20 20 72  K_MINUS;.      r
27e2e 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
27e2f 20 20 20 63 61 73 65 20 27 28 27 3a 20 7b 0a 20     case '(': {. 
27e30 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
27e31 3d 20 54 4b 5f 4c 50 3b 0a 20 20 20 20 20 20 72  = TK_LP;.      r
27e32 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
27e33 20 20 20 63 61 73 65 20 27 29 27 3a 20 7b 0a 20     case ')': {. 
27e34 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
27e35 3d 20 54 4b 5f 52 50 3b 0a 20 20 20 20 20 20 72  = TK_RP;.      r
27e36 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
27e37 20 20 20 63 61 73 65 20 27 3b 27 3a 20 7b 0a 20     case ';': {. 
27e38 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
27e39 3d 20 54 4b 5f 53 45 4d 49 3b 0a 20 20 20 20 20  = TK_SEMI;.     
27e3a 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
27e3b 0a 20 20 20 20 63 61 73 65 20 27 2b 27 3a 20 7b  .    case '+': {
27e3c 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70  .      *tokenTyp
27e3d 65 20 3d 20 54 4b 5f 50 4c 55 53 3b 0a 20 20 20  e = TK_PLUS;.   
27e3e 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
27e3f 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2a 27 3a   }.    case '*':
27e40 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54   {.      *tokenT
27e41 79 70 65 20 3d 20 54 4b 5f 53 54 41 52 3b 0a 20  ype = TK_STAR;. 
27e42 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
27e43 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2f     }.    case '/
27e44 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 7a  ': {.      if( z
27e45 5b 31 5d 21 3d 27 2a 27 20 7c 7c 20 7a 5b 32 5d  [1]!='*' || z[2]
27e46 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a  ==0 ){.        *
27e47 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53  tokenType = TK_S
27e48 4c 41 53 48 3b 0a 20 20 20 20 20 20 20 20 72 65  LASH;.        re
27e49 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a  turn 1;.      }.
27e4a 20 20 20 20 20 20 66 6f 72 28 69 3d 33 2c 20 63        for(i=3, c
27e4b 3d 7a 5b 32 5d 3b 20 28 63 21 3d 27 2a 27 20 7c  =z[2]; (c!='*' |
27e4c 7c 20 7a 5b 69 5d 21 3d 27 2f 27 29 20 26 26 20  | z[i]!='/') && 
27e4d 28 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b  (c=z[i])!=0; i++
27e4e 29 7b 7d 0a 20 20 20 20 20 20 69 66 28 20 63 20  ){}.      if( c 
27e4f 29 20 69 2b 2b 3b 0a 20 20 20 20 20 20 2a 74 6f  ) i++;.      *to
27e50 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 43 4f 4d  kenType = TK_COM
27e51 4d 45 4e 54 3b 0a 20 20 20 20 20 20 72 65 74 75  MENT;.      retu
27e52 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn i;.    }.    
27e53 63 61 73 65 20 27 25 27 3a 20 7b 0a 20 20 20 20  case '%': {.    
27e54 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
27e55 4b 5f 52 45 4d 3b 0a 20 20 20 20 20 20 72 65 74  K_REM;.      ret
27e56 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
27e57 20 63 61 73 65 20 27 3d 27 3a 20 7b 0a 20 20 20   case '=': {.   
27e58 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
27e59 54 4b 5f 45 51 3b 0a 20 20 20 20 20 20 72 65 74  TK_EQ;.      ret
27e5a 75 72 6e 20 31 20 2b 20 28 7a 5b 31 5d 3d 3d 27  urn 1 + (z[1]=='
27e5b 3d 27 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  =');.    }.    c
27e5c 61 73 65 20 27 3c 27 3a 20 7b 0a 20 20 20 20 20  ase '<': {.     
27e5d 20 69 66 28 20 28 63 3d 7a 5b 31 5d 29 3d 3d 27   if( (c=z[1])=='
27e5e 3d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74  =' ){.        *t
27e5f 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4c 45  okenType = TK_LE
27e60 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
27e61 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20   2;.      }else 
27e62 69 66 28 20 63 3d 3d 27 3e 27 20 29 7b 0a 20 20  if( c=='>' ){.  
27e63 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
27e64 20 3d 20 54 4b 5f 4e 45 3b 0a 20 20 20 20 20 20   = TK_NE;.      
27e65 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20    return 2;.    
27e66 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27    }else if( c=='
27e67 3c 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74  <' ){.        *t
27e68 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4c 53  okenType = TK_LS
27e69 48 49 46 54 3b 0a 20 20 20 20 20 20 20 20 72 65  HIFT;.        re
27e6a 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65  turn 2;.      }e
27e6b 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f  lse{.        *to
27e6c 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4c 54 3b  kenType = TK_LT;
27e6d 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
27e6e 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
27e6f 0a 20 20 20 20 63 61 73 65 20 27 3e 27 3a 20 7b  .    case '>': {
27e70 0a 20 20 20 20 20 20 69 66 28 20 28 63 3d 7a 5b  .      if( (c=z[
27e71 31 5d 29 3d 3d 27 3d 27 20 29 7b 0a 20 20 20 20  1])=='=' ){.    
27e72 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
27e73 20 54 4b 5f 47 45 3b 0a 20 20 20 20 20 20 20 20   TK_GE;.        
27e74 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20  return 2;.      
27e75 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 3e 27  }else if( c=='>'
27e76 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b   ){.        *tok
27e77 65 6e 54 79 70 65 20 3d 20 54 4b 5f 52 53 48 49  enType = TK_RSHI
27e78 46 54 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  FT;.        retu
27e79 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73  rn 2;.      }els
27e7a 65 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65  e{.        *toke
27e7b 6e 54 79 70 65 20 3d 20 54 4b 5f 47 54 3b 0a 20  nType = TK_GT;. 
27e7c 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b         return 1;
27e7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
27e7e 20 20 20 63 61 73 65 20 27 21 27 3a 20 7b 0a 20     case '!': {. 
27e7f 20 20 20 20 20 69 66 28 20 7a 5b 31 5d 21 3d 27       if( z[1]!='
27e80 3d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74  =' ){.        *t
27e81 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c  okenType = TK_IL
27e82 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 20 20 72  LEGAL;.        r
27e83 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d  eturn 2;.      }
27e84 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2a 74  else{.        *t
27e85 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4e 45  okenType = TK_NE
27e86 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
27e87 20 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   2;.      }.    
27e88 7d 0a 20 20 20 20 63 61 73 65 20 27 7c 27 3a 20  }.    case '|': 
27e89 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 31 5d  {.      if( z[1]
27e8a 21 3d 27 7c 27 20 29 7b 0a 20 20 20 20 20 20 20  !='|' ){.       
27e8b 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
27e8c 5f 42 49 54 4f 52 3b 0a 20 20 20 20 20 20 20 20  _BITOR;.        
27e8d 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20  return 1;.      
27e8e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2a  }else{.        *
27e8f 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 43  tokenType = TK_C
27e90 4f 4e 43 41 54 3b 0a 20 20 20 20 20 20 20 20 72  ONCAT;.        r
27e91 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d  eturn 2;.      }
27e92 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
27e93 27 2c 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f  ',': {.      *to
27e94 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 43 4f 4d  kenType = TK_COM
27e95 4d 41 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  MA;.      return
27e96 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   1;.    }.    ca
27e97 73 65 20 27 26 27 3a 20 7b 0a 20 20 20 20 20 20  se '&': {.      
27e98 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
27e99 42 49 54 41 4e 44 3b 0a 20 20 20 20 20 20 72 65  BITAND;.      re
27e9a 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
27e9b 20 20 63 61 73 65 20 27 7e 27 3a 20 7b 0a 20 20    case '~': {.  
27e9c 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
27e9d 20 54 4b 5f 42 49 54 4e 4f 54 3b 0a 20 20 20 20   TK_BITNOT;.    
27e9e 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
27e9f 7d 0a 20 20 20 20 63 61 73 65 20 27 60 27 3a 0a  }.    case '`':.
27ea0 20 20 20 20 63 61 73 65 20 27 5c 27 27 3a 0a 20      case '\'':. 
27ea1 20 20 20 63 61 73 65 20 27 22 27 3a 20 7b 0a 20     case '"': {. 
27ea2 20 20 20 20 20 69 6e 74 20 64 65 6c 69 6d 20 3d       int delim =
27ea3 20 7a 5b 30 5d 3b 0a 20 20 20 20 20 20 66 6f 72   z[0];.      for
27ea4 28 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d 29 21 3d  (i=1; (c=z[i])!=
27ea5 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  0; i++){.       
27ea6 20 69 66 28 20 63 3d 3d 64 65 6c 69 6d 20 29 7b   if( c==delim ){
27ea7 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a  .          if( z
27ea8 5b 69 2b 31 5d 3d 3d 64 65 6c 69 6d 20 29 7b 0a  [i+1]==delim ){.
27ea9 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b              i++;
27eaa 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
27eab 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72  {.            br
27eac 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  eak;.          }
27ead 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
27eae 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 20 29   }.      if( c )
27eaf 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
27eb0 54 79 70 65 20 3d 20 54 4b 5f 53 54 52 49 4e 47  Type = TK_STRING
27eb1 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
27eb2 20 69 2b 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73   i+1;.      }els
27eb3 65 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65  e{.        *toke
27eb4 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47  nType = TK_ILLEG
27eb5 41 4c 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  AL;.        retu
27eb6 72 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn i;.      }.  
27eb7 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2e 27    }.    case '.'
27eb8 3a 20 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  : {.#ifndef SQLI
27eb9 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
27eba 5f 50 4f 49 4e 54 0a 20 20 20 20 20 20 69 66 28  _POINT.      if(
27ebb 20 21 69 73 64 69 67 69 74 28 7a 5b 31 5d 29 20   !isdigit(z[1]) 
27ebc 29 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b  ).#endif.      {
27ebd 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
27ebe 79 70 65 20 3d 20 54 4b 5f 44 4f 54 3b 0a 20 20  ype = TK_DOT;.  
27ebf 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
27ec0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a        }.      /*
27ec1 20 49 66 20 74 68 65 20 6e 65 78 74 20 63 68 61   If the next cha
27ec2 72 61 63 74 65 72 20 69 73 20 61 20 64 69 67 69  racter is a digi
27ec3 74 2c 20 74 68 69 73 20 69 73 20 61 20 66 6c 6f  t, this is a flo
27ec4 61 74 69 6e 67 20 70 6f 69 6e 74 0a 20 20 20 20  ating point.    
27ec5 20 20 2a 2a 20 6e 75 6d 62 65 72 20 74 68 61 74    ** number that
27ec6 20 62 65 67 69 6e 73 20 77 69 74 68 20 22 2e 22   begins with "."
27ec7 2e 20 20 46 61 6c 6c 20 74 68 72 75 20 69 6e 74  .  Fall thru int
27ec8 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73 65 20  o the next case 
27ec9 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  */.    }.    cas
27eca 65 20 27 30 27 3a 20 63 61 73 65 20 27 31 27 3a  e '0': case '1':
27ecb 20 63 61 73 65 20 27 32 27 3a 20 63 61 73 65 20   case '2': case 
27ecc 27 33 27 3a 20 63 61 73 65 20 27 34 27 3a 0a 20  '3': case '4':. 
27ecd 20 20 20 63 61 73 65 20 27 35 27 3a 20 63 61 73     case '5': cas
27ece 65 20 27 36 27 3a 20 63 61 73 65 20 27 37 27 3a  e '6': case '7':
27ecf 20 63 61 73 65 20 27 38 27 3a 20 63 61 73 65 20   case '8': case 
27ed0 27 39 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f  '9': {.      *to
27ed1 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4e 54  kenType = TK_INT
27ed2 45 47 45 52 3b 0a 20 20 20 20 20 20 66 6f 72 28  EGER;.      for(
27ed3 69 3d 30 3b 20 69 73 64 69 67 69 74 28 7a 5b 69  i=0; isdigit(z[i
27ed4 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 23 69 66 6e 64  ]); i++){}.#ifnd
27ed5 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
27ed6 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20  LOATING_POINT.  
27ed7 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 27 2e      if( z[i]=='.
27ed8 27 20 29 7b 0a 20 20 20 20 20 20 20 20 69 2b 2b  ' ){.        i++
27ed9 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  ;.        while(
27eda 20 69 73 64 69 67 69 74 28 7a 5b 69 5d 29 20 29   isdigit(z[i]) )
27edb 7b 20 69 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20  { i++; }.       
27edc 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
27edd 5f 46 4c 4f 41 54 3b 0a 20 20 20 20 20 20 7d 0a  _FLOAT;.      }.
27ede 20 20 20 20 20 20 69 66 28 20 28 7a 5b 69 5d 3d        if( (z[i]=
27edf 3d 27 65 27 20 7c 7c 20 7a 5b 69 5d 3d 3d 27 45  ='e' || z[i]=='E
27ee0 27 29 20 26 26 0a 20 20 20 20 20 20 20 20 20 20  ') &&.          
27ee1 20 28 20 69 73 64 69 67 69 74 28 7a 5b 69 2b 31   ( isdigit(z[i+1
27ee2 5d 29 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  ]) .            
27ee3 7c 7c 20 28 28 7a 5b 69 2b 31 5d 3d 3d 27 2b 27  || ((z[i+1]=='+'
27ee4 20 7c 7c 20 7a 5b 69 2b 31 5d 3d 3d 27 2d 27 29   || z[i+1]=='-')
27ee5 20 26 26 20 69 73 64 69 67 69 74 28 7a 5b 69 2b   && isdigit(z[i+
27ee6 32 5d 29 29 0a 20 20 20 20 20 20 20 20 20 20 20  2])).           
27ee7 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20  ).      ){.     
27ee8 20 20 20 69 20 2b 3d 20 32 3b 0a 20 20 20 20 20     i += 2;.     
27ee9 20 20 20 77 68 69 6c 65 28 20 69 73 64 69 67 69     while( isdigi
27eea 74 28 7a 5b 69 5d 29 20 29 7b 20 69 2b 2b 3b 20  t(z[i]) ){ i++; 
27eeb 7d 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  }.        *token
27eec 54 79 70 65 20 3d 20 54 4b 5f 46 4c 4f 41 54 3b  Type = TK_FLOAT;
27eed 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
27eee 20 20 20 20 20 20 77 68 69 6c 65 28 20 49 64 43        while( IdC
27eef 68 61 72 28 7a 5b 69 5d 29 20 29 7b 0a 20 20 20  har(z[i]) ){.   
27ef0 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
27ef1 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20  = TK_ILLEGAL;.  
27ef2 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20        i++;.     
27ef3 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20   }.      return 
27ef4 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  i;.    }.    cas
27ef5 65 20 27 5b 27 3a 20 7b 0a 20 20 20 20 20 20 66  e '[': {.      f
27ef6 6f 72 28 69 3d 31 2c 20 63 3d 7a 5b 30 5d 3b 20  or(i=1, c=z[0]; 
27ef7 63 21 3d 27 5d 27 20 26 26 20 28 63 3d 7a 5b 69  c!=']' && (c=z[i
27ef8 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 7d 0a 20 20  ])!=0; i++){}.  
27ef9 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
27efa 20 63 3d 3d 27 5d 27 20 3f 20 54 4b 5f 49 44 20   c==']' ? TK_ID 
27efb 3a 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20  : TK_ILLEGAL;.  
27efc 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20      return i;.  
27efd 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 3f 27    }.    case '?'
27efe 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e  : {.      *token
27eff 54 79 70 65 20 3d 20 54 4b 5f 56 41 52 49 41 42  Type = TK_VARIAB
27f00 4c 45 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  LE;.      for(i=
27f01 31 3b 20 69 73 64 69 67 69 74 28 7a 5b 69 5d 29  1; isdigit(z[i])
27f02 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 72  ; i++){}.      r
27f03 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20  eturn i;.    }. 
27f04 20 20 20 63 61 73 65 20 27 23 27 3a 20 7b 0a 20     case '#': {. 
27f05 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 73       for(i=1; is
27f06 64 69 67 69 74 28 7a 5b 69 5d 29 3b 20 69 2b 2b  digit(z[i]); i++
27f07 29 7b 7d 0a 20 20 20 20 20 20 69 66 28 20 69 3e  ){}.      if( i>
27f08 31 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  1 ){.        /* 
27f09 50 61 72 61 6d 65 74 65 72 73 20 6f 66 20 74 68  Parameters of th
27f0a 65 20 66 6f 72 6d 20 23 4e 4e 4e 20 28 77 68 65  e form #NNN (whe
27f0b 72 65 20 4e 4e 4e 20 69 73 20 61 20 6e 75 6d 62  re NNN is a numb
27f0c 65 72 29 20 61 72 65 20 75 73 65 64 0a 20 20 20  er) are used.   
27f0d 20 20 20 20 20 2a 2a 20 69 6e 74 65 72 6e 61 6c       ** internal
27f0e 6c 79 20 62 79 20 73 71 6c 69 74 65 33 4e 65 73  ly by sqlite3Nes
27f0f 74 65 64 50 61 72 73 65 2e 20 20 2a 2f 0a 20 20  tedParse.  */.  
27f10 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
27f11 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a   = TK_REGISTER;.
27f12 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69          return i
27f13 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
27f14 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20  /* Fall through 
27f15 69 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61  into the next ca
27f16 73 65 20 69 66 20 74 68 65 20 27 23 27 20 69 73  se if the '#' is
27f17 20 6e 6f 74 20 66 6f 6c 6c 6f 77 65 64 20 62 79   not followed by
27f18 0a 20 20 20 20 20 20 2a 2a 20 61 20 64 69 67 69  .      ** a digi
27f19 74 2e 20 54 72 79 20 74 6f 20 6d 61 74 63 68 20  t. Try to match 
27f1a 23 41 41 41 41 20 77 68 65 72 65 20 41 41 41 41  #AAAA where AAAA
27f1b 20 69 73 20 61 20 70 61 72 61 6d 65 74 65 72 20   is a parameter 
27f1c 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 7d 0a 23  name. */.    }.#
27f1d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
27f1e 49 54 5f 54 43 4c 5f 56 41 52 49 41 42 4c 45 0a  IT_TCL_VARIABLE.
27f1f 20 20 20 20 63 61 73 65 20 27 24 27 3a 0a 23 65      case '$':.#e
27f20 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 27 40  ndif.    case '@
27f21 27 3a 20 20 2f 2a 20 46 6f 72 20 63 6f 6d 70 61  ':  /* For compa
27f22 74 69 62 69 6c 69 74 79 20 77 69 74 68 20 4d 53  tibility with MS
27f23 20 53 51 4c 20 53 65 72 76 65 72 20 2a 2f 0a 20   SQL Server */. 
27f24 20 20 20 63 61 73 65 20 27 3a 27 3a 20 7b 0a 20     case ':': {. 
27f25 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a       int n = 0;.
27f26 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
27f27 20 3d 20 54 4b 5f 56 41 52 49 41 42 4c 45 3b 0a   = TK_VARIABLE;.
27f28 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 28        for(i=1; (
27f29 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29  c=z[i])!=0; i++)
27f2a 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 64  {.        if( Id
27f2b 43 68 61 72 28 63 29 20 29 7b 0a 20 20 20 20 20  Char(c) ){.     
27f2c 20 20 20 20 20 6e 2b 2b 3b 0a 23 69 66 6e 64 65       n++;.#ifnde
27f2d 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 43  f SQLITE_OMIT_TC
27f2e 4c 5f 56 41 52 49 41 42 4c 45 0a 20 20 20 20 20  L_VARIABLE.     
27f2f 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d     }else if( c==
27f30 27 28 27 20 26 26 20 6e 3e 30 20 29 7b 0a 20 20  '(' && n>0 ){.  
27f31 20 20 20 20 20 20 20 20 64 6f 7b 0a 20 20 20 20          do{.    
27f32 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20          i++;.   
27f33 20 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 28         }while( (
27f34 63 3d 7a 5b 69 5d 29 21 3d 30 20 26 26 20 21 69  c=z[i])!=0 && !i
27f35 73 73 70 61 63 65 28 63 29 20 26 26 20 63 21 3d  sspace(c) && c!=
27f36 27 29 27 20 29 3b 0a 20 20 20 20 20 20 20 20 20  ')' );.         
27f37 20 69 66 28 20 63 3d 3d 27 29 27 20 29 7b 0a 20   if( c==')' ){. 
27f38 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a             i++;.
27f39 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
27f3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 74 6f  .            *to
27f3b 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c  kenType = TK_ILL
27f3c 45 47 41 4c 3b 0a 20 20 20 20 20 20 20 20 20 20  EGAL;.          
27f3d 7d 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  }.          brea
27f3e 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  k;.        }else
27f3f 20 69 66 28 20 63 3d 3d 27 3a 27 20 26 26 20 7a   if( c==':' && z
27f40 5b 69 2b 31 5d 3d 3d 27 3a 27 20 29 7b 0a 20 20  [i+1]==':' ){.  
27f41 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 23 65 6e          i++;.#en
27f42 64 69 66 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  dif.        }els
27f43 65 7b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  e{.          bre
27f44 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
27f45 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
27f46 6e 3d 3d 30 20 29 20 2a 74 6f 6b 65 6e 54 79 70  n==0 ) *tokenTyp
27f47 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a  e = TK_ILLEGAL;.
27f48 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a        return i;.
27f49 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
27f4a 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c  LITE_OMIT_BLOB_L
27f4b 49 54 45 52 41 4c 0a 20 20 20 20 63 61 73 65 20  ITERAL.    case 
27f4c 27 78 27 3a 20 63 61 73 65 20 27 58 27 3a 20 7b  'x': case 'X': {
27f4d 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 31 5d 3d  .      if( z[1]=
27f4e 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20 20 20 20  ='\'' ){.       
27f4f 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
27f50 5f 42 4c 4f 42 3b 0a 20 20 20 20 20 20 20 20 66  _BLOB;.        f
27f51 6f 72 28 69 3d 32 3b 20 28 63 3d 7a 5b 69 5d 29  or(i=2; (c=z[i])
27f52 21 3d 30 20 26 26 20 63 21 3d 27 5c 27 27 3b 20  !=0 && c!='\''; 
27f53 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
27f54 69 66 28 20 21 69 73 78 64 69 67 69 74 28 63 29  if( !isxdigit(c)
27f55 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
27f56 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
27f57 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 20  ILLEGAL;.       
27f58 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
27f59 20 20 20 20 20 20 20 69 66 28 20 69 25 32 20 7c         if( i%2 |
27f5a 7c 20 21 63 20 29 20 2a 74 6f 6b 65 6e 54 79 70  | !c ) *tokenTyp
27f5b 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a  e = TK_ILLEGAL;.
27f5c 20 20 20 20 20 20 20 20 69 66 28 20 63 20 29 20          if( c ) 
27f5d 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 72 65 74  i++;.        ret
27f5e 75 72 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20  urn i;.      }. 
27f5f 20 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 69 73       /* Otherwis
27f60 65 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74  e fall through t
27f61 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73 65 20  o the next case 
27f62 2a 2f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  */.    }.#endif.
27f63 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
27f64 20 20 20 20 20 69 66 28 20 21 49 64 43 68 61 72       if( !IdChar
27f65 28 2a 7a 29 20 29 7b 0a 20 20 20 20 20 20 20 20  (*z) ){.        
27f66 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
27f67 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 49 64       for(i=1; Id
27f68 43 68 61 72 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29  Char(z[i]); i++)
27f69 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  {}.      *tokenT
27f6a 79 70 65 20 3d 20 6b 65 79 77 6f 72 64 43 6f 64  ype = keywordCod
27f6b 65 28 28 63 68 61 72 2a 29 7a 2c 20 69 29 3b 0a  e((char*)z, i);.
27f6c 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a        return i;.
27f6d 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 74 6f 6b      }.  }.  *tok
27f6e 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45  enType = TK_ILLE
27f6f 47 41 4c 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  GAL;.  return 1;
27f70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 75 6e 20 74 68  .}../*.** Run th
27f71 65 20 70 61 72 73 65 72 20 6f 6e 20 74 68 65 20  e parser on the 
27f72 67 69 76 65 6e 20 53 51 4c 20 73 74 72 69 6e 67  given SQL string
27f73 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 73 74  .  The parser st
27f74 72 75 63 74 75 72 65 20 69 73 0a 2a 2a 20 70 61  ructure is.** pa
27f75 73 73 65 64 20 69 6e 2e 20 20 41 6e 20 53 51 4c  ssed in.  An SQL
27f76 49 54 45 5f 20 73 74 61 74 75 73 20 63 6f 64 65  ITE_ status code
27f77 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49   is returned.  I
27f78 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
27f79 73 0a 2a 2a 20 61 6e 64 20 70 7a 45 72 72 4d 73  s.** and pzErrMs
27f7a 67 21 3d 4e 55 4c 4c 20 74 68 65 6e 20 61 6e 20  g!=NULL then an 
27f7b 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 6d 69  error message mi
27f7c 67 68 74 20 62 65 20 77 72 69 74 74 65 6e 20 69  ght be written i
27f7d 6e 74 6f 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6f  nto .** memory o
27f7e 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c  btained from sql
27f7f 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e  ite3_malloc() an
27f80 64 20 2a 70 7a 45 72 72 4d 73 67 20 6d 61 64 65  d *pzErrMsg made
27f81 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 61   to point to tha
27f82 74 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61  t.** error messa
27f83 67 65 2e 20 20 4f 72 20 6d 61 79 62 65 20 6e 6f  ge.  Or maybe no
27f84 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
27f85 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
27f86 52 75 6e 50 61 72 73 65 72 28 50 61 72 73 65 20  RunParser(Parse 
27f87 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63  *pParse, const c
27f88 68 61 72 20 2a 7a 53 71 6c 2c 20 63 68 61 72 20  har *zSql, char 
27f89 2a 2a 70 7a 45 72 72 4d 73 67 29 7b 0a 20 20 69  **pzErrMsg){.  i
27f8a 6e 74 20 6e 45 72 72 20 3d 20 30 3b 0a 20 20 69  nt nErr = 0;.  i
27f8b 6e 74 20 69 3b 0a 20 20 76 6f 69 64 20 2a 70 45  nt i;.  void *pE
27f8c 6e 67 69 6e 65 3b 0a 20 20 69 6e 74 20 74 6f 6b  ngine;.  int tok
27f8d 65 6e 54 79 70 65 3b 0a 20 20 69 6e 74 20 6c 61  enType;.  int la
27f8e 73 74 54 6f 6b 65 6e 50 61 72 73 65 64 20 3d 20  stTokenParsed = 
27f8f 2d 31 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  -1;.  sqlite3 *d
27f90 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
27f91 20 20 69 6e 74 20 6d 78 53 71 6c 4c 65 6e 20 3d    int mxSqlLen =
27f92 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49   db->aLimit[SQLI
27f93 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e  TE_LIMIT_SQL_LEN
27f94 47 54 48 5d 3b 0a 0a 20 20 69 66 28 20 64 62 2d  GTH];..  if( db-
27f95 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3d 3d  >activeVdbeCnt==
27f96 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 75 31 2e  0 ){.    db->u1.
27f97 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20  isInterrupted = 
27f98 30 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d  0;.  }.  pParse-
27f99 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  >rc = SQLITE_OK;
27f9a 0a 20 20 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c  .  pParse->zTail
27f9b 20 3d 20 70 50 61 72 73 65 2d 3e 7a 53 71 6c 20   = pParse->zSql 
27f9c 3d 20 7a 53 71 6c 3b 0a 20 20 69 20 3d 20 30 3b  = zSql;.  i = 0;
27f9d 0a 20 20 70 45 6e 67 69 6e 65 20 3d 20 73 71 6c  .  pEngine = sql
27f9e 69 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 28  ite3ParserAlloc(
27f9f 28 76 6f 69 64 2a 28 2a 29 28 73 69 7a 65 5f 74  (void*(*)(size_t
27fa0 29 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  ))sqlite3_malloc
27fa1 29 3b 0a 20 20 69 66 28 20 70 45 6e 67 69 6e 65  );.  if( pEngine
27fa2 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d  ==0 ){.    db->m
27fa3 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
27fa4 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
27fa5 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
27fa6 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
27fa7 73 4c 61 73 74 54 6f 6b 65 6e 2e 64 79 6e 3d 3d  sLastToken.dyn==
27fa8 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
27fa9 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
27faa 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
27fab 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69   pParse->pNewTri
27fac 67 67 65 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73  gger==0 );.  ass
27fad 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 56 61  ert( pParse->nVa
27fae 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  r==0 );.  assert
27faf 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78  ( pParse->nVarEx
27fb0 70 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  pr==0 );.  asser
27fb1 74 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45  t( pParse->nVarE
27fb2 78 70 72 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20  xprAlloc==0 );. 
27fb3 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
27fb4 3e 61 70 56 61 72 45 78 70 72 3d 3d 30 20 29 3b  >apVarExpr==0 );
27fb5 0a 20 20 77 68 69 6c 65 28 20 21 64 62 2d 3e 6d  .  while( !db->m
27fb6 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 7a  allocFailed && z
27fb7 53 71 6c 5b 69 5d 21 3d 30 20 29 7b 0a 20 20 20  Sql[i]!=0 ){.   
27fb8 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 29 3b   assert( i>=0 );
27fb9 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 73 4c 61  .    pParse->sLa
27fba 73 74 54 6f 6b 65 6e 2e 7a 20 3d 20 28 75 38 2a  stToken.z = (u8*
27fbb 29 26 7a 53 71 6c 5b 69 5d 3b 0a 20 20 20 20 61  )&zSql[i];.    a
27fbc 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 73  ssert( pParse->s
27fbd 4c 61 73 74 54 6f 6b 65 6e 2e 64 79 6e 3d 3d 30  LastToken.dyn==0
27fbe 20 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e   );.    pParse->
27fbf 73 4c 61 73 74 54 6f 6b 65 6e 2e 6e 20 3d 20 73  sLastToken.n = s
27fc0 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 28  qlite3GetToken((
27fc1 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 26  unsigned char*)&
27fc2 7a 53 71 6c 5b 69 5d 2c 26 74 6f 6b 65 6e 54 79  zSql[i],&tokenTy
27fc3 70 65 29 3b 0a 20 20 20 20 69 20 2b 3d 20 70 50  pe);.    i += pP
27fc4 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e  arse->sLastToken
27fc5 2e 6e 3b 0a 20 20 20 20 69 66 28 20 69 3e 6d 78  .n;.    if( i>mx
27fc6 53 71 6c 4c 65 6e 20 29 7b 0a 20 20 20 20 20 20  SqlLen ){.      
27fc7 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c  pParse->rc = SQL
27fc8 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 20 20  ITE_TOOBIG;.    
27fc9 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
27fca 20 20 20 73 77 69 74 63 68 28 20 74 6f 6b 65 6e     switch( token
27fcb 54 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61  Type ){.      ca
27fcc 73 65 20 54 4b 5f 53 50 41 43 45 3a 0a 20 20 20  se TK_SPACE:.   
27fcd 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4d 4d 45     case TK_COMME
27fce 4e 54 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 66  NT: {.        if
27fcf 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72  ( db->u1.isInter
27fd0 72 75 70 74 65 64 20 29 7b 0a 20 20 20 20 20 20  rupted ){.      
27fd1 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d      pParse->rc =
27fd2 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50   SQLITE_INTERRUP
27fd3 54 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  T;.          sql
27fd4 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 70 7a  ite3SetString(pz
27fd5 45 72 72 4d 73 67 2c 20 22 69 6e 74 65 72 72 75  ErrMsg, "interru
27fd6 70 74 22 2c 20 28 63 68 61 72 2a 29 30 29 3b 0a  pt", (char*)0);.
27fd7 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61            goto a
27fd8 62 6f 72 74 5f 70 61 72 73 65 3b 0a 20 20 20 20  bort_parse;.    
27fd9 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72      }.        br
27fda 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
27fdb 20 20 20 63 61 73 65 20 54 4b 5f 49 4c 4c 45 47     case TK_ILLEG
27fdc 41 4c 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 66  AL: {.        if
27fdd 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20  ( pzErrMsg ){.  
27fde 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
27fdf 66 72 65 65 28 2a 70 7a 45 72 72 4d 73 67 29 3b  free(*pzErrMsg);
27fe0 0a 20 20 20 20 20 20 20 20 20 20 2a 70 7a 45 72  .          *pzEr
27fe1 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 50  rMsg = sqlite3MP
27fe2 72 69 6e 74 66 28 64 62 2c 20 22 75 6e 72 65 63  rintf(db, "unrec
27fe3 6f 67 6e 69 7a 65 64 20 74 6f 6b 65 6e 3a 20 5c  ognized token: \
27fe4 22 25 54 5c 22 22 2c 0a 20 20 20 20 20 20 20 20  "%T\"",.        
27fe5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27fe6 20 20 26 70 50 61 72 73 65 2d 3e 73 4c 61 73 74    &pParse->sLast
27fe7 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 20 20  Token);.        
27fe8 7d 0a 20 20 20 20 20 20 20 20 6e 45 72 72 2b 2b  }.        nErr++
27fe9 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61  ;.        goto a
27fea 62 6f 72 74 5f 70 61 72 73 65 3b 0a 20 20 20 20  bort_parse;.    
27feb 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 54    }.      case T
27fec 4b 5f 53 45 4d 49 3a 20 7b 0a 20 20 20 20 20 20  K_SEMI: {.      
27fed 20 20 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 20    pParse->zTail 
27fee 3d 20 26 7a 53 71 6c 5b 69 5d 3b 0a 20 20 20 20  = &zSql[i];.    
27fef 20 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75      /* Fall thru
27ff0 20 69 6e 74 6f 20 74 68 65 20 64 65 66 61 75 6c   into the defaul
27ff1 74 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 20 20  t case */.      
27ff2 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a  }.      default:
27ff3 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   {.        sqlit
27ff4 65 33 50 61 72 73 65 72 28 70 45 6e 67 69 6e 65  e3Parser(pEngine
27ff5 2c 20 74 6f 6b 65 6e 54 79 70 65 2c 20 70 50 61  , tokenType, pPa
27ff6 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2c  rse->sLastToken,
27ff7 20 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20   pParse);.      
27ff8 20 20 6c 61 73 74 54 6f 6b 65 6e 50 61 72 73 65    lastTokenParse
27ff9 64 20 3d 20 74 6f 6b 65 6e 54 79 70 65 3b 0a 20  d = tokenType;. 
27ffa 20 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73         if( pPars
27ffb 65 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  e->rc!=SQLITE_OK
27ffc 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f   ){.          go
27ffd 74 6f 20 61 62 6f 72 74 5f 70 61 72 73 65 3b 0a  to abort_parse;.
27ffe 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
27fff 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
28000 0a 20 20 20 20 7d 0a 20 20 7d 0a 61 62 6f 72 74  .    }.  }.abort
28001 5f 70 61 72 73 65 3a 0a 20 20 69 66 28 20 7a 53  _parse:.  if( zS
28002 71 6c 5b 69 5d 3d 3d 30 20 26 26 20 6e 45 72 72  ql[i]==0 && nErr
28003 3d 3d 30 20 26 26 20 70 50 61 72 73 65 2d 3e 72  ==0 && pParse->r
28004 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
28005 20 20 20 20 69 66 28 20 6c 61 73 74 54 6f 6b 65      if( lastToke
28006 6e 50 61 72 73 65 64 21 3d 54 4b 5f 53 45 4d 49  nParsed!=TK_SEMI
28007 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
28008 33 50 61 72 73 65 72 28 70 45 6e 67 69 6e 65 2c  3Parser(pEngine,
28009 20 54 4b 5f 53 45 4d 49 2c 20 70 50 61 72 73 65   TK_SEMI, pParse
2800a 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2c 20 70 50  ->sLastToken, pP
2800b 61 72 73 65 29 3b 0a 20 20 20 20 20 20 70 50 61  arse);.      pPa
2800c 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a 53  rse->zTail = &zS
2800d 71 6c 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20  ql[i];.    }.   
2800e 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 28 70   sqlite3Parser(p
2800f 45 6e 67 69 6e 65 2c 20 30 2c 20 70 50 61 72 73  Engine, 0, pPars
28010 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2c 20 70  e->sLastToken, p
28011 50 61 72 73 65 29 3b 0a 20 20 7d 0a 20 20 73 71  Parse);.  }.  sq
28012 6c 69 74 65 33 50 61 72 73 65 72 46 72 65 65 28  lite3ParserFree(
28013 70 45 6e 67 69 6e 65 2c 20 73 71 6c 69 74 65 33  pEngine, sqlite3
28014 5f 66 72 65 65 29 3b 0a 20 20 69 66 28 20 64 62  _free);.  if( db
28015 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
28016 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63  {.    pParse->rc
28017 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
28018 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73  .  }.  if( pPars
28019 65 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  e->rc!=SQLITE_OK
2801a 20 26 26 20 70 50 61 72 73 65 2d 3e 72 63 21 3d   && pParse->rc!=
2801b 53 51 4c 49 54 45 5f 44 4f 4e 45 20 26 26 20 70  SQLITE_DONE && p
2801c 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 3d 3d  Parse->zErrMsg==
2801d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
2801e 53 65 74 53 74 72 69 6e 67 28 26 70 50 61 72 73  SetString(&pPars
2801f 65 2d 3e 7a 45 72 72 4d 73 67 2c 20 73 71 6c 69  e->zErrMsg, sqli
28020 74 65 33 45 72 72 53 74 72 28 70 50 61 72 73 65  te3ErrStr(pParse
28021 2d 3e 72 63 29 2c 20 28 63 68 61 72 2a 29 30 29  ->rc), (char*)0)
28022 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72  ;.  }.  if( pPar
28023 73 65 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20  se->zErrMsg ){. 
28024 20 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20     if( pzErrMsg 
28025 26 26 20 2a 70 7a 45 72 72 4d 73 67 3d 3d 30 20  && *pzErrMsg==0 
28026 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 4d  ){.      *pzErrM
28027 73 67 20 3d 20 70 50 61 72 73 65 2d 3e 7a 45 72  sg = pParse->zEr
28028 72 4d 73 67 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  rMsg;.    }else{
28029 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
2802a 72 65 65 28 70 50 61 72 73 65 2d 3e 7a 45 72 72  ree(pParse->zErr
2802b 4d 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Msg);.    }.    
2802c 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20  pParse->zErrMsg 
2802d 3d 20 30 3b 0a 20 20 20 20 6e 45 72 72 2b 2b 3b  = 0;.    nErr++;
2802e 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73  .  }.  if( pPars
2802f 65 2d 3e 70 56 64 62 65 20 26 26 20 70 50 61 72  e->pVdbe && pPar
28030 73 65 2d 3e 6e 45 72 72 3e 30 20 26 26 20 70 50  se->nErr>0 && pP
28031 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20  arse->nested==0 
28032 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
28033 62 65 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  beDelete(pParse-
28034 3e 70 56 64 62 65 29 3b 0a 20 20 20 20 70 50 61  >pVdbe);.    pPa
28035 72 73 65 2d 3e 70 56 64 62 65 20 3d 20 30 3b 0a  rse->pVdbe = 0;.
28036 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
28037 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
28038 41 43 48 45 0a 20 20 69 66 28 20 70 50 61 72 73  ACHE.  if( pPars
28039 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 7b 0a  e->nested==0 ){.
2803a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
2803b 28 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c  (pParse->aTableL
2803c 6f 63 6b 29 3b 0a 20 20 20 20 70 50 61 72 73 65  ock);.    pParse
2803d 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 20 3d 20 30  ->aTableLock = 0
2803e 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 54  ;.    pParse->nT
2803f 61 62 6c 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20  ableLock = 0;.  
28040 7d 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  }.#endif.#ifndef
28041 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
28042 54 55 41 4c 54 41 42 4c 45 0a 20 20 73 71 6c 69  TUALTABLE.  sqli
28043 74 65 33 5f 66 72 65 65 28 70 50 61 72 73 65 2d  te3_free(pParse-
28044 3e 61 70 56 74 61 62 4c 6f 63 6b 29 3b 0a 23 65  >apVtabLock);.#e
28045 6e 64 69 66 0a 0a 20 20 69 66 28 20 21 49 4e 5f  ndif..  if( !IN_
28046 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29 7b 0a  DECLARE_VTAB ){.
28047 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70 50      /* If the pP
28048 61 72 73 65 2d 3e 64 65 63 6c 61 72 65 56 74 61  arse->declareVta
28049 62 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 64  b flag is set, d
2804a 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 61 6e 79  o not delete any
2804b 20 74 61 62 6c 65 20 0a 20 20 20 20 2a 2a 20 73   table .    ** s
2804c 74 72 75 63 74 75 72 65 20 62 75 69 6c 74 20 75  tructure built u
2804d 70 20 69 6e 20 70 50 61 72 73 65 2d 3e 70 4e 65  p in pParse->pNe
2804e 77 54 61 62 6c 65 2e 20 54 68 65 20 63 61 6c 6c  wTable. The call
2804f 69 6e 67 20 63 6f 64 65 20 28 73 65 65 20 76 74  ing code (see vt
28050 61 62 2e 63 29 0a 20 20 20 20 2a 2a 20 77 69 6c  ab.c).    ** wil
28051 6c 20 74 61 6b 65 20 72 65 73 70 6f 6e 73 69 62  l take responsib
28052 69 6c 69 74 79 20 66 6f 72 20 66 72 65 65 69 6e  ility for freein
28053 67 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75  g the Table stru
28054 63 74 75 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  cture..    */.  
28055 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54    sqlite3DeleteT
28056 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 70 4e 65  able(pParse->pNe
28057 77 54 61 62 6c 65 29 3b 0a 20 20 7d 0a 0a 20 20  wTable);.  }..  
28058 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
28059 67 67 65 72 28 70 50 61 72 73 65 2d 3e 70 4e 65  gger(pParse->pNe
2805a 77 54 72 69 67 67 65 72 29 3b 0a 20 20 73 71 6c  wTrigger);.  sql
2805b 69 74 65 33 5f 66 72 65 65 28 70 50 61 72 73 65  ite3_free(pParse
2805c 2d 3e 61 70 56 61 72 45 78 70 72 29 3b 0a 20 20  ->apVarExpr);.  
2805d 69 66 28 20 6e 45 72 72 3e 30 20 26 26 20 28 70  if( nErr>0 && (p
2805e 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54  Parse->rc==SQLIT
2805f 45 5f 4f 4b 20 7c 7c 20 70 50 61 72 73 65 2d 3e  E_OK || pParse->
28060 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 29  rc==SQLITE_DONE)
28061 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e   ){.    pParse->
28062 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
28063 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  R;.  }.  return 
28064 6e 45 72 72 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  nErr;.}../******
28065 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
28066 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a  tokenize.c *****
28067 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28068 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28069 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
2806a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
2806b 69 6c 65 20 63 6f 6d 70 6c 65 74 65 2e 63 20 2a  ile complete.c *
2806c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2806d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2806e 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
2806f 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
28070 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
28071 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
28072 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
28073 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
28074 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
28075 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
28076 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
28077 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
28078 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
28079 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
2807a 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
2807b 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
2807c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
2807d 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
2807e 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
2807f 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
28080 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
28081 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
28082 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28083 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28084 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28085 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
28086 2a 2a 20 41 6e 20 74 6f 6b 65 6e 69 7a 65 72 20  ** An tokenizer 
28087 66 6f 72 20 53 51 4c 0a 2a 2a 0a 2a 2a 20 54 68  for SQL.**.** Th
28088 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
28089 20 43 20 63 6f 64 65 20 74 68 61 74 20 69 6d 70   C code that imp
2808a 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 71 6c 69  lements the sqli
2808b 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29 20 41  te3_complete() A
2808c 50 49 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65  PI..** This code
2808d 20 75 73 65 64 20 74 6f 20 62 65 20 70 61 72 74   used to be part
2808e 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65   of the tokenize
2808f 72 2e 63 20 73 6f 75 72 63 65 20 66 69 6c 65 2e  r.c source file.
28090 20 20 42 75 74 20 62 79 0a 2a 2a 20 73 65 70 61    But by.** sepa
28091 72 61 74 69 6e 67 20 69 74 20 6f 75 74 2c 20 74  rating it out, t
28092 68 65 20 63 6f 64 65 20 77 69 6c 6c 20 62 65 20  he code will be 
28093 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 6f 6d  automatically om
28094 69 74 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 74  itted from.** st
28095 61 74 69 63 20 6c 69 6e 6b 73 20 74 68 61 74 20  atic links that 
28096 64 6f 20 6e 6f 74 20 75 73 65 20 69 74 2e 0a 2a  do not use it..*
28097 2a 0a 2a 2a 20 24 49 64 3a 20 63 6f 6d 70 6c 65  *.** $Id: comple
28098 74 65 2e 63 2c 76 20 31 2e 36 20 32 30 30 37 2f  te.c,v 1.6 2007/
28099 30 38 2f 32 37 20 32 33 3a 32 36 3a 35 39 20 64  08/27 23:26:59 d
2809a 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e  rh Exp $.*/.#ifn
2809b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2809c 43 4f 4d 50 4c 45 54 45 0a 0a 2f 2a 0a 2a 2a 20  COMPLETE../*.** 
2809d 54 68 69 73 20 69 73 20 64 65 66 69 6e 65 64 20  This is defined 
2809e 69 6e 20 74 6f 6b 65 6e 69 7a 65 2e 63 2e 20 20  in tokenize.c.  
2809f 57 65 20 6a 75 73 74 20 68 61 76 65 20 74 6f 20  We just have to 
280a0 69 6d 70 6f 72 74 20 74 68 65 20 64 65 66 69 6e  import the defin
280a1 69 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65  ition..*/.#ifnde
280a2 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d  f SQLITE_AMALGAM
280a3 41 54 49 4f 4e 0a 23 69 66 64 65 66 20 53 51 4c  ATION.#ifdef SQL
280a4 49 54 45 5f 41 53 43 49 49 0a 53 51 4c 49 54 45  ITE_ASCII.SQLITE
280a5 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
280a6 68 61 72 20 73 71 6c 69 74 65 33 49 73 41 73 63  har sqlite3IsAsc
280a7 69 69 49 64 43 68 61 72 5b 5d 3b 0a 23 64 65 66  iiIdChar[];.#def
280a8 69 6e 65 20 49 64 43 68 61 72 28 43 29 20 20 28  ine IdChar(C)  (
280a9 28 28 63 3d 43 29 26 30 78 38 30 29 21 3d 30 20  ((c=C)&0x80)!=0 
280aa 7c 7c 20 28 63 3e 30 78 31 66 20 26 26 20 73 71  || (c>0x1f && sq
280ab 6c 69 74 65 33 49 73 41 73 63 69 69 49 64 43 68  lite3IsAsciiIdCh
280ac 61 72 5b 63 2d 30 78 32 30 5d 29 29 0a 23 65 6e  ar[c-0x20])).#en
280ad 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
280ae 45 5f 45 42 43 44 49 43 0a 53 51 4c 49 54 45 5f  E_EBCDIC.SQLITE_
280af 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
280b0 61 72 20 73 71 6c 69 74 65 33 49 73 45 62 63 64  ar sqlite3IsEbcd
280b1 69 63 49 64 43 68 61 72 5b 5d 3b 0a 23 64 65 66  icIdChar[];.#def
280b2 69 6e 65 20 49 64 43 68 61 72 28 43 29 20 20 28  ine IdChar(C)  (
280b3 28 28 63 3d 43 29 3e 3d 30 78 34 32 20 26 26 20  ((c=C)>=0x42 && 
280b4 73 71 6c 69 74 65 33 49 73 45 62 63 64 69 63 49  sqlite3IsEbcdicI
280b5 64 43 68 61 72 5b 63 2d 30 78 34 30 5d 29 29 0a  dChar[c-0x40])).
280b6 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a  #endif.#endif /*
280b7 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41   SQLITE_AMALGAMA
280b8 54 49 4f 4e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20  TION */.../*.** 
280b9 54 6f 6b 65 6e 20 74 79 70 65 73 20 75 73 65 64  Token types used
280ba 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f   by the sqlite3_
280bb 63 6f 6d 70 6c 65 74 65 28 29 20 72 6f 75 74 69  complete() routi
280bc 6e 65 2e 20 20 53 65 65 20 74 68 65 20 68 65 61  ne.  See the hea
280bd 64 65 72 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20  der.** comments 
280be 6f 6e 20 74 68 61 74 20 70 72 6f 63 65 64 75 72  on that procedur
280bf 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  e for additional
280c0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f   information..*/
280c1 0a 23 64 65 66 69 6e 65 20 74 6b 53 45 4d 49 20  .#define tkSEMI 
280c2 20 20 20 30 0a 23 64 65 66 69 6e 65 20 74 6b 57     0.#define tkW
280c3 53 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65  S      1.#define
280c4 20 74 6b 4f 54 48 45 52 20 20 20 32 0a 23 64 65   tkOTHER   2.#de
280c5 66 69 6e 65 20 74 6b 45 58 50 4c 41 49 4e 20 33  fine tkEXPLAIN 3
280c6 0a 23 64 65 66 69 6e 65 20 74 6b 43 52 45 41 54  .#define tkCREAT
280c7 45 20 20 34 0a 23 64 65 66 69 6e 65 20 74 6b 54  E  4.#define tkT
280c8 45 4d 50 20 20 20 20 35 0a 23 64 65 66 69 6e 65  EMP    5.#define
280c9 20 74 6b 54 52 49 47 47 45 52 20 36 0a 23 64 65   tkTRIGGER 6.#de
280ca 66 69 6e 65 20 74 6b 45 4e 44 20 20 20 20 20 37  fine tkEND     7
280cb 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ../*.** Return T
280cc 52 55 45 20 69 66 20 74 68 65 20 67 69 76 65 6e  RUE if the given
280cd 20 53 51 4c 20 73 74 72 69 6e 67 20 65 6e 64 73   SQL string ends
280ce 20 69 6e 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 2e   in a semicolon.
280cf 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 61 6c 20 68  .**.** Special h
280d0 61 6e 64 6c 69 6e 67 20 69 73 20 72 65 71 75 69  andling is requi
280d1 72 65 20 66 6f 72 20 43 52 45 41 54 45 20 54 52  re for CREATE TR
280d2 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74 73  IGGER statements
280d3 2e 0a 2a 2a 20 57 68 65 6e 65 76 65 72 20 74 68  ..** Whenever th
280d4 65 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52  e CREATE TRIGGER
280d5 20 6b 65 79 77 6f 72 64 73 20 61 72 65 20 73 65   keywords are se
280d6 65 6e 2c 20 74 68 65 20 73 74 61 74 65 6d 65 6e  en, the statemen
280d7 74 0a 2a 2a 20 6d 75 73 74 20 65 6e 64 20 77 69  t.** must end wi
280d8 74 68 20 22 3b 45 4e 44 3b 22 2e 0a 2a 2a 0a 2a  th ";END;"..**.*
280d9 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74  * This implement
280da 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73 74 61  ation uses a sta
280db 74 65 20 6d 61 63 68 69 6e 65 20 77 69 74 68 20  te machine with 
280dc 37 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20  7 states:.**.** 
280dd 20 20 28 30 29 20 53 54 41 52 54 20 20 20 20 20    (0) START     
280de 41 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  At the beginning
280df 20 6f 72 20 65 6e 64 20 6f 66 20 61 6e 20 53 51   or end of an SQ
280e0 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68  L statement.  Th
280e1 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 20 20  is routine.**   
280e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65                re
280e3 74 75 72 6e 73 20 31 20 69 66 20 69 74 20 65 6e  turns 1 if it en
280e4 64 73 20 69 6e 20 74 68 65 20 53 54 41 52 54 20  ds in the START 
280e5 73 74 61 74 65 20 61 6e 64 20 30 20 69 66 20 69  state and 0 if i
280e6 74 20 65 6e 64 73 0a 2a 2a 20 20 20 20 20 20 20  t ends.**       
280e7 20 20 20 20 20 20 20 20 20 20 69 6e 20 61 6e 79            in any
280e8 20 6f 74 68 65 72 20 73 74 61 74 65 2e 0a 2a 2a   other state..**
280e9 0a 2a 2a 20 20 20 28 31 29 20 4e 4f 52 4d 41 4c  .**   (1) NORMAL
280ea 20 20 20 20 57 65 20 61 72 65 20 69 6e 20 74 68      We are in th
280eb 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 74 61 74  e middle of stat
280ec 65 6d 65 6e 74 20 77 68 69 63 68 20 65 6e 64 73  ement which ends
280ed 20 77 69 74 68 20 61 20 73 69 6e 67 6c 65 0a 2a   with a single.*
280ee 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
280ef 20 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a 0a    semicolon..**.
280f0 2a 2a 20 20 20 28 32 29 20 45 58 50 4c 41 49 4e  **   (2) EXPLAIN
280f1 20 20 20 54 68 65 20 6b 65 79 77 6f 72 64 20 45     The keyword E
280f2 58 50 4c 41 49 4e 20 68 61 73 20 62 65 65 6e 20  XPLAIN has been 
280f3 73 65 65 6e 20 61 74 20 74 68 65 20 62 65 67 69  seen at the begi
280f4 6e 6e 69 6e 67 20 6f 66 20 0a 2a 2a 20 20 20 20  nning of .**    
280f5 20 20 20 20 20 20 20 20 20 20 20 20 20 61 20 73               a s
280f6 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  tatement..**.** 
280f7 20 20 28 33 29 20 43 52 45 41 54 45 20 20 20 20    (3) CREATE    
280f8 54 68 65 20 6b 65 79 77 6f 72 64 20 43 52 45 41  The keyword CREA
280f9 54 45 20 68 61 73 20 62 65 65 6e 20 73 65 65 6e  TE has been seen
280fa 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   at the beginnin
280fb 67 20 6f 66 20 61 0a 2a 2a 20 20 20 20 20 20 20  g of a.**       
280fc 20 20 20 20 20 20 20 20 20 20 73 74 61 74 65 6d            statem
280fd 65 6e 74 2c 20 70 6f 73 73 69 62 6c 79 20 70 72  ent, possibly pr
280fe 65 63 65 65 64 65 64 20 62 79 20 45 58 50 4c 41  eceeded by EXPLA
280ff 49 4e 20 61 6e 64 2f 6f 72 20 66 6f 6c 6c 6f 77  IN and/or follow
28100 65 64 20 62 79 0a 2a 2a 20 20 20 20 20 20 20 20  ed by.**        
28101 20 20 20 20 20 20 20 20 20 54 45 4d 50 20 6f 72           TEMP or
28102 20 54 45 4d 50 4f 52 41 52 59 0a 2a 2a 0a 2a 2a   TEMPORARY.**.**
28103 20 20 20 28 34 29 20 54 52 49 47 47 45 52 20 20     (4) TRIGGER  
28104 20 57 65 20 61 72 65 20 69 6e 20 74 68 65 20 6d   We are in the m
28105 69 64 64 6c 65 20 6f 66 20 61 20 74 72 69 67 67  iddle of a trigg
28106 65 72 20 64 65 66 69 6e 69 74 69 6f 6e 20 74 68  er definition th
28107 61 74 20 6d 75 73 74 20 62 65 0a 2a 2a 20 20 20  at must be.**   
28108 20 20 20 20 20 20 20 20 20 20 20 20 20 20 65 6e                en
28109 64 65 64 20 62 79 20 61 20 73 65 6d 69 63 6f 6c  ded by a semicol
2810a 6f 6e 2c 20 74 68 65 20 6b 65 79 77 6f 72 64 20  on, the keyword 
2810b 45 4e 44 2c 20 61 6e 64 20 61 6e 6f 74 68 65 72  END, and another
2810c 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a   semicolon..**.*
2810d 2a 20 20 20 28 35 29 20 53 45 4d 49 20 20 20 20  *   (5) SEMI    
2810e 20 20 57 65 27 76 65 20 73 65 65 6e 20 74 68 65    We've seen the
2810f 20 66 69 72 73 74 20 73 65 6d 69 63 6f 6c 6f 6e   first semicolon
28110 20 69 6e 20 74 68 65 20 22 3b 45 4e 44 3b 22 20   in the ";END;" 
28111 74 68 61 74 20 6f 63 63 75 72 73 20 61 74 0a 2a  that occurs at.*
28112 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
28113 20 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 74    the end of a t
28114 72 69 67 67 65 72 20 64 65 66 69 6e 69 74 69 6f  rigger definitio
28115 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 36 29 20 45  n..**.**   (6) E
28116 4e 44 20 20 20 20 20 20 20 57 65 27 76 65 20 73  ND       We've s
28117 65 65 6e 20 74 68 65 20 22 3b 45 4e 44 22 20 6f  een the ";END" o
28118 66 20 74 68 65 20 22 3b 45 4e 44 3b 22 20 74 68  f the ";END;" th
28119 61 74 20 6f 63 63 75 72 73 20 61 74 20 74 68 65  at occurs at the
2811a 20 65 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20   end.**         
2811b 20 20 20 20 20 20 20 20 6f 66 20 61 20 74 72 69          of a tri
2811c 67 67 65 72 20 64 69 66 69 6e 69 74 69 6f 6e 2e  gger difinition.
2811d 0a 2a 2a 0a 2a 2a 20 54 72 61 6e 73 69 74 69 6f  .**.** Transitio
2811e 6e 73 20 62 65 74 77 65 65 6e 20 73 74 61 74 65  ns between state
2811f 73 20 61 62 6f 76 65 20 61 72 65 20 64 65 74 65  s above are dete
28120 72 6d 69 6e 65 64 20 62 79 20 74 6f 6b 65 6e 73  rmined by tokens
28121 20 65 78 74 72 61 63 74 65 64 0a 2a 2a 20 66 72   extracted.** fr
28122 6f 6d 20 74 68 65 20 69 6e 70 75 74 2e 20 20 54  om the input.  T
28123 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 6f 6b  he following tok
28124 65 6e 73 20 61 72 65 20 73 69 67 6e 69 66 69 63  ens are signific
28125 61 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 30 29  ant:.**.**   (0)
28126 20 74 6b 53 45 4d 49 20 20 20 20 20 20 41 20 73   tkSEMI      A s
28127 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a 20 20 20 28  emicolon..**   (
28128 31 29 20 74 6b 57 53 20 20 20 20 20 20 20 20 57  1) tkWS        W
28129 68 69 74 65 73 70 61 63 65 0a 2a 2a 20 20 20 28  hitespace.**   (
2812a 32 29 20 74 6b 4f 54 48 45 52 20 20 20 20 20 41  2) tkOTHER     A
2812b 6e 79 20 6f 74 68 65 72 20 53 51 4c 20 74 6f 6b  ny other SQL tok
2812c 65 6e 2e 0a 2a 2a 20 20 20 28 33 29 20 74 6b 45  en..**   (3) tkE
2812d 58 50 4c 41 49 4e 20 20 20 54 68 65 20 22 65 78  XPLAIN   The "ex
2812e 70 6c 61 69 6e 22 20 6b 65 79 77 6f 72 64 2e 0a  plain" keyword..
2812f 2a 2a 20 20 20 28 34 29 20 74 6b 43 52 45 41 54  **   (4) tkCREAT
28130 45 20 20 20 20 54 68 65 20 22 63 72 65 61 74 65  E    The "create
28131 22 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a 20 20 20  " keyword..**   
28132 28 35 29 20 74 6b 54 45 4d 50 20 20 20 20 20 20  (5) tkTEMP      
28133 54 68 65 20 22 74 65 6d 70 22 20 6f 72 20 22 74  The "temp" or "t
28134 65 6d 70 6f 72 61 72 79 22 20 6b 65 79 77 6f 72  emporary" keywor
28135 64 2e 0a 2a 2a 20 20 20 28 36 29 20 74 6b 54 52  d..**   (6) tkTR
28136 49 47 47 45 52 20 20 20 54 68 65 20 22 74 72 69  IGGER   The "tri
28137 67 67 65 72 22 20 6b 65 79 77 6f 72 64 2e 0a 2a  gger" keyword..*
28138 2a 20 20 20 28 37 29 20 74 6b 45 4e 44 20 20 20  *   (7) tkEND   
28139 20 20 20 20 54 68 65 20 22 65 6e 64 22 20 6b 65      The "end" ke
2813a 79 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 69  yword..**.** Whi
2813b 74 65 73 70 61 63 65 20 6e 65 76 65 72 20 63 61  tespace never ca
2813c 75 73 65 73 20 61 20 73 74 61 74 65 20 74 72 61  uses a state tra
2813d 6e 73 69 74 69 6f 6e 20 61 6e 64 20 69 73 20 61  nsition and is a
2813e 6c 77 61 79 73 20 69 67 6e 6f 72 65 64 2e 0a 2a  lways ignored..*
2813f 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 69  *.** If we compi
28140 6c 65 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4f  le with SQLITE_O
28141 4d 49 54 5f 54 52 49 47 47 45 52 2c 20 61 6c 6c  MIT_TRIGGER, all
28142 20 6f 66 20 74 68 65 20 63 6f 6d 70 75 74 61 74   of the computat
28143 69 6f 6e 20 6e 65 65 64 65 64 0a 2a 2a 20 74 6f  ion needed.** to
28144 20 72 65 63 6f 67 6e 69 7a 65 20 74 68 65 20 65   recognize the e
28145 6e 64 20 6f 66 20 61 20 74 72 69 67 67 65 72 20  nd of a trigger 
28146 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 64 2e 20  can be omitted. 
28147 20 41 6c 6c 20 77 65 20 68 61 76 65 20 74 6f 20   All we have to 
28148 64 6f 0a 2a 2a 20 69 73 20 6c 6f 6f 6b 20 66 6f  do.** is look fo
28149 72 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 20 74 68  r a semicolon th
2814a 61 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f  at is not part o
2814b 66 20 61 6e 20 73 74 72 69 6e 67 20 6f 72 20 63  f an string or c
2814c 6f 6d 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  omment..*/.SQLIT
2814d 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2814e 33 5f 63 6f 6d 70 6c 65 74 65 28 63 6f 6e 73 74  3_complete(const
2814f 20 63 68 61 72 20 2a 7a 53 71 6c 29 7b 0a 20 20   char *zSql){.  
28150 75 38 20 73 74 61 74 65 20 3d 20 30 3b 20 20 20  u8 state = 0;   
28151 2f 2a 20 43 75 72 72 65 6e 74 20 73 74 61 74 65  /* Current state
28152 2c 20 75 73 69 6e 67 20 6e 75 6d 62 65 72 73 20  , using numbers 
28153 64 65 66 69 6e 65 64 20 69 6e 20 68 65 61 64 65  defined in heade
28154 72 20 63 6f 6d 6d 65 6e 74 20 2a 2f 0a 20 20 75  r comment */.  u
28155 38 20 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20 2f  8 token;       /
28156 2a 20 56 61 6c 75 65 20 6f 66 20 74 68 65 20 6e  * Value of the n
28157 65 78 74 20 74 6f 6b 65 6e 20 2a 2f 0a 0a 23 69  ext token */..#i
28158 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
28159 54 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 41  T_TRIGGER.  /* A
2815a 20 63 6f 6d 70 6c 65 78 20 73 74 61 74 65 6d 65   complex stateme
2815b 6e 74 20 6d 61 63 68 69 6e 65 20 75 73 65 64 20  nt machine used 
2815c 74 6f 20 64 65 74 65 63 74 20 74 68 65 20 65 6e  to detect the en
2815d 64 20 6f 66 20 61 20 43 52 45 41 54 45 20 54 52  d of a CREATE TR
2815e 49 47 47 45 52 0a 20 20 2a 2a 20 73 74 61 74 65  IGGER.  ** state
2815f 6d 65 6e 74 2e 20 20 54 68 69 73 20 69 73 20 74  ment.  This is t
28160 68 65 20 6e 6f 72 6d 61 6c 20 63 61 73 65 2e 0a  he normal case..
28161 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f    */.  static co
28162 6e 73 74 20 75 38 20 74 72 61 6e 73 5b 37 5d 5b  nst u8 trans[7][
28163 38 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20  8] = {.         
28164 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
28165 6f 6b 65 6e 3a 20 20 20 20 20 20 20 20 20 20 20  oken:           
28166 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28167 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28168 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 2f 2a 20       */.     /* 
28169 53 74 61 74 65 3a 20 20 20 20 20 20 20 2a 2a 20  State:       ** 
2816a 20 53 45 4d 49 20 20 57 53 20 20 4f 54 48 45 52   SEMI  WS  OTHER
2816b 20 45 58 50 4c 41 49 4e 20 20 43 52 45 41 54 45   EXPLAIN  CREATE
2816c 20 20 54 45 4d 50 20 20 54 52 49 47 47 45 52 20    TEMP  TRIGGER 
2816d 20 45 4e 44 20 20 2a 2f 0a 20 20 20 20 20 2f 2a   END  */.     /*
2816e 20 30 20 20 20 53 54 41 52 54 3a 20 2a 2f 20 7b   0   START: */ {
2816f 20 20 20 20 30 2c 20 20 30 2c 20 20 20 20 20 31      0,  0,     1
28170 2c 20 20 20 20 20 20 32 2c 20 20 20 20 20 20 33  ,      2,      3
28171 2c 20 20 20 20 31 2c 20 20 20 20 20 20 20 31 2c  ,    1,       1,
28172 20 20 20 31 2c 20 20 7d 2c 0a 20 20 20 20 20 2f     1,  },.     /
28173 2a 20 31 20 20 4e 4f 52 4d 41 4c 3a 20 2a 2f 20  * 1  NORMAL: */ 
28174 7b 20 20 20 20 30 2c 20 20 31 2c 20 20 20 20 20  {    0,  1,     
28175 31 2c 20 20 20 20 20 20 31 2c 20 20 20 20 20 20  1,      1,      
28176 31 2c 20 20 20 20 31 2c 20 20 20 20 20 20 20 31  1,    1,       1
28177 2c 20 20 20 31 2c 20 20 7d 2c 0a 20 20 20 20 20  ,   1,  },.     
28178 2f 2a 20 32 20 45 58 50 4c 41 49 4e 3a 20 2a 2f  /* 2 EXPLAIN: */
28179 20 7b 20 20 20 20 30 2c 20 20 32 2c 20 20 20 20   {    0,  2,    
2817a 20 31 2c 20 20 20 20 20 20 31 2c 20 20 20 20 20   1,      1,     
2817b 20 33 2c 20 20 20 20 31 2c 20 20 20 20 20 20 20   3,    1,       
2817c 31 2c 20 20 20 31 2c 20 20 7d 2c 0a 20 20 20 20  1,   1,  },.    
2817d 20 2f 2a 20 33 20 20 43 52 45 41 54 45 3a 20 2a   /* 3  CREATE: *
2817e 2f 20 7b 20 20 20 20 30 2c 20 20 33 2c 20 20 20  / {    0,  3,   
2817f 20 20 31 2c 20 20 20 20 20 20 31 2c 20 20 20 20    1,      1,    
28180 20 20 31 2c 20 20 20 20 33 2c 20 20 20 20 20 20    1,    3,      
28181 20 34 2c 20 20 20 31 2c 20 20 7d 2c 0a 20 20 20   4,   1,  },.   
28182 20 20 2f 2a 20 34 20 54 52 49 47 47 45 52 3a 20    /* 4 TRIGGER: 
28183 2a 2f 20 7b 20 20 20 20 35 2c 20 20 34 2c 20 20  */ {    5,  4,  
28184 20 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20 20     4,      4,   
28185 20 20 20 34 2c 20 20 20 20 34 2c 20 20 20 20 20     4,    4,     
28186 20 20 34 2c 20 20 20 34 2c 20 20 7d 2c 0a 20 20    4,   4,  },.  
28187 20 20 20 2f 2a 20 35 20 20 20 20 53 45 4d 49 3a     /* 5    SEMI:
28188 20 2a 2f 20 7b 20 20 20 20 35 2c 20 20 35 2c 20   */ {    5,  5, 
28189 20 20 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20      4,      4,  
2818a 20 20 20 20 34 2c 20 20 20 20 34 2c 20 20 20 20      4,    4,    
2818b 20 20 20 34 2c 20 20 20 36 2c 20 20 7d 2c 0a 20     4,   6,  },. 
2818c 20 20 20 20 2f 2a 20 36 20 20 20 20 20 45 4e 44      /* 6     END
2818d 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 36 2c  : */ {    0,  6,
2818e 20 20 20 20 20 34 2c 20 20 20 20 20 20 34 2c 20       4,      4, 
2818f 20 20 20 20 20 34 2c 20 20 20 20 34 2c 20 20 20       4,    4,   
28190 20 20 20 20 34 2c 20 20 20 34 2c 20 20 7d 2c 0a      4,   4,  },.
28191 20 20 7d 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20    };.#else.  /* 
28192 49 66 20 74 72 69 67 67 65 72 73 20 61 72 65 20  If triggers are 
28193 6e 6f 74 20 73 75 70 70 6f 72 65 64 20 62 79 20  not suppored by 
28194 74 68 69 73 20 63 6f 6d 70 69 6c 65 20 74 68 65  this compile the
28195 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  n the statement 
28196 6d 61 63 68 69 6e 65 0a 20 20 2a 2a 20 75 73 65  machine.  ** use
28197 64 20 74 6f 20 64 65 74 65 63 74 20 74 68 65 20  d to detect the 
28198 65 6e 64 20 6f 66 20 61 20 73 74 61 74 65 6d 65  end of a stateme
28199 6e 74 20 69 73 20 6d 75 63 68 20 73 69 6d 70 6c  nt is much simpl
2819a 69 65 72 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69  ier.  */.  stati
2819b 63 20 63 6f 6e 73 74 20 75 38 20 74 72 61 6e 73  c const u8 trans
2819c 5b 32 5d 5b 33 5d 20 3d 20 7b 0a 20 20 20 20 20  [2][3] = {.     
2819d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2819e 2f 2a 20 54 6f 6b 65 6e 3a 20 20 20 20 20 20 20  /* Token:       
2819f 20 20 20 20 2a 2f 0a 20 20 20 20 20 2f 2a 20 53      */.     /* S
281a0 74 61 74 65 3a 20 20 20 20 20 20 20 2a 2a 20 20  tate:       **  
281a1 53 45 4d 49 20 20 57 53 20 20 4f 54 48 45 52 20  SEMI  WS  OTHER 
281a2 2a 2f 0a 20 20 20 20 20 2f 2a 20 30 20 20 20 53  */.     /* 0   S
281a3 54 41 52 54 3a 20 2a 2f 20 7b 20 20 20 20 30 2c  TART: */ {    0,
281a4 20 20 30 2c 20 20 20 20 20 31 2c 20 7d 2c 0a 20    0,     1, },. 
281a5 20 20 20 20 2f 2a 20 31 20 20 4e 4f 52 4d 41 4c      /* 1  NORMAL
281a6 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 31 2c  : */ {    0,  1,
281a7 20 20 20 20 20 31 2c 20 7d 2c 0a 20 20 7d 3b 0a       1, },.  };.
281a8 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
281a9 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f  _OMIT_TRIGGER */
281aa 0a 0a 20 20 77 68 69 6c 65 28 20 2a 7a 53 71 6c  ..  while( *zSql
281ab 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68 28 20   ){.    switch( 
281ac 2a 7a 53 71 6c 20 29 7b 0a 20 20 20 20 20 20 63  *zSql ){.      c
281ad 61 73 65 20 27 3b 27 3a 20 7b 20 20 2f 2a 20 41  ase ';': {  /* A
281ae 20 73 65 6d 69 63 6f 6c 6f 6e 20 2a 2f 0a 20 20   semicolon */.  
281af 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
281b0 53 45 4d 49 3b 0a 20 20 20 20 20 20 20 20 62 72  SEMI;.        br
281b1 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
281b2 20 20 20 63 61 73 65 20 27 20 27 3a 0a 20 20 20     case ' ':.   
281b3 20 20 20 63 61 73 65 20 27 5c 72 27 3a 0a 20 20     case '\r':.  
281b4 20 20 20 20 63 61 73 65 20 27 5c 74 27 3a 0a 20      case '\t':. 
281b5 20 20 20 20 20 63 61 73 65 20 27 5c 6e 27 3a 0a       case '\n':.
281b6 20 20 20 20 20 20 63 61 73 65 20 27 5c 66 27 3a        case '\f':
281b7 20 7b 20 20 2f 2a 20 57 68 69 74 65 20 73 70 61   {  /* White spa
281b8 63 65 20 69 73 20 69 67 6e 6f 72 65 64 20 2a 2f  ce is ignored */
281b9 0a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d  .        token =
281ba 20 74 6b 57 53 3b 0a 20 20 20 20 20 20 20 20 62   tkWS;.        b
281bb 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
281bc 20 20 20 20 63 61 73 65 20 27 2f 27 3a 20 7b 20      case '/': { 
281bd 20 20 2f 2a 20 43 2d 73 74 79 6c 65 20 63 6f 6d    /* C-style com
281be 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 20  ments */.       
281bf 20 69 66 28 20 7a 53 71 6c 5b 31 5d 21 3d 27 2a   if( zSql[1]!='*
281c0 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 74  ' ){.          t
281c1 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a  oken = tkOTHER;.
281c2 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
281c3 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
281c4 20 20 20 7a 53 71 6c 20 2b 3d 20 32 3b 0a 20 20     zSql += 2;.  
281c5 20 20 20 20 20 20 77 68 69 6c 65 28 20 7a 53 71        while( zSq
281c6 6c 5b 30 5d 20 26 26 20 28 7a 53 71 6c 5b 30 5d  l[0] && (zSql[0]
281c7 21 3d 27 2a 27 20 7c 7c 20 7a 53 71 6c 5b 31 5d  !='*' || zSql[1]
281c8 21 3d 27 2f 27 29 20 29 7b 20 7a 53 71 6c 2b 2b  !='/') ){ zSql++
281c9 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ; }.        if( 
281ca 7a 53 71 6c 5b 30 5d 3d 3d 30 20 29 20 72 65 74  zSql[0]==0 ) ret
281cb 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 7a  urn 0;.        z
281cc 53 71 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 74  Sql++;.        t
281cd 6f 6b 65 6e 20 3d 20 74 6b 57 53 3b 0a 20 20 20  oken = tkWS;.   
281ce 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
281cf 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 27    }.      case '
281d0 2d 27 3a 20 7b 20 20 20 2f 2a 20 53 51 4c 2d 73  -': {   /* SQL-s
281d1 74 79 6c 65 20 63 6f 6d 6d 65 6e 74 73 20 66 72  tyle comments fr
281d2 6f 6d 20 22 2d 2d 22 20 74 6f 20 65 6e 64 20 6f  om "--" to end o
281d3 66 20 6c 69 6e 65 20 2a 2f 0a 20 20 20 20 20 20  f line */.      
281d4 20 20 69 66 28 20 7a 53 71 6c 5b 31 5d 21 3d 27    if( zSql[1]!='
281d5 2d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  -' ){.          
281d6 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b  token = tkOTHER;
281d7 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
281d8 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
281d9 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 53 71 6c      while( *zSql
281da 20 26 26 20 2a 7a 53 71 6c 21 3d 27 5c 6e 27 20   && *zSql!='\n' 
281db 29 7b 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20  ){ zSql++; }.   
281dc 20 20 20 20 20 69 66 28 20 2a 7a 53 71 6c 3d 3d       if( *zSql==
281dd 30 20 29 20 72 65 74 75 72 6e 20 73 74 61 74 65  0 ) return state
281de 3d 3d 30 3b 0a 20 20 20 20 20 20 20 20 74 6f 6b  ==0;.        tok
281df 65 6e 20 3d 20 74 6b 57 53 3b 0a 20 20 20 20 20  en = tkWS;.     
281e0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
281e1 7d 0a 20 20 20 20 20 20 63 61 73 65 20 27 5b 27  }.      case '['
281e2 3a 20 7b 20 20 20 2f 2a 20 4d 69 63 72 6f 73 6f  : {   /* Microso
281e3 66 74 2d 73 74 79 6c 65 20 69 64 65 6e 74 69 66  ft-style identif
281e4 69 65 72 73 20 69 6e 20 5b 2e 2e 2e 5d 20 2a 2f  iers in [...] */
281e5 0a 20 20 20 20 20 20 20 20 7a 53 71 6c 2b 2b 3b  .        zSql++;
281e6 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
281e7 2a 7a 53 71 6c 20 26 26 20 2a 7a 53 71 6c 21 3d  *zSql && *zSql!=
281e8 27 5d 27 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20 7d  ']' ){ zSql++; }
281e9 0a 20 20 20 20 20 20 20 20 69 66 28 20 2a 7a 53  .        if( *zS
281ea 71 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  ql==0 ) return 0
281eb 3b 0a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20  ;.        token 
281ec 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20  = tkOTHER;.     
281ed 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
281ee 7d 0a 20 20 20 20 20 20 63 61 73 65 20 27 60 27  }.      case '`'
281ef 3a 20 20 20 20 20 2f 2a 20 47 72 61 76 65 2d 61  :     /* Grave-a
281f0 63 63 65 6e 74 20 71 75 6f 74 65 64 20 73 79 6d  ccent quoted sym
281f1 62 6f 6c 73 20 75 73 65 64 20 62 79 20 4d 79 53  bols used by MyS
281f2 51 4c 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  QL */.      case
281f3 20 27 22 27 3a 20 20 20 20 20 2f 2a 20 73 69 6e   '"':     /* sin
281f4 67 6c 65 2d 20 61 6e 64 20 64 6f 75 62 6c 65 2d  gle- and double-
281f5 71 75 6f 74 65 64 20 73 74 72 69 6e 67 73 20 2a  quoted strings *
281f6 2f 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c 27  /.      case '\'
281f7 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  ': {.        int
281f8 20 63 20 3d 20 2a 7a 53 71 6c 3b 0a 20 20 20 20   c = *zSql;.    
281f9 20 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20 20 20      zSql++;.    
281fa 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 53 71 6c      while( *zSql
281fb 20 26 26 20 2a 7a 53 71 6c 21 3d 63 20 29 7b 20   && *zSql!=c ){ 
281fc 7a 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20 20 20 20  zSql++; }.      
281fd 20 20 69 66 28 20 2a 7a 53 71 6c 3d 3d 30 20 29    if( *zSql==0 )
281fe 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
281ff 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48     token = tkOTH
28200 45 52 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  ER;.        brea
28201 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
28202 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
28203 20 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 20      int c;.     
28204 20 20 20 69 66 28 20 49 64 43 68 61 72 28 28 75     if( IdChar((u
28205 38 29 2a 7a 53 71 6c 29 20 29 7b 0a 20 20 20 20  8)*zSql) ){.    
28206 20 20 20 20 20 20 2f 2a 20 4b 65 79 77 6f 72 64        /* Keyword
28207 73 20 61 6e 64 20 75 6e 71 75 6f 74 65 64 20 69  s and unquoted i
28208 64 65 6e 74 69 66 69 65 72 73 20 2a 2f 0a 20 20  dentifiers */.  
28209 20 20 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b          int nId;
2820a 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6e  .          for(n
2820b 49 64 3d 31 3b 20 49 64 43 68 61 72 28 7a 53 71  Id=1; IdChar(zSq
2820c 6c 5b 6e 49 64 5d 29 3b 20 6e 49 64 2b 2b 29 7b  l[nId]); nId++){
2820d 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  }.#ifdef SQLITE_
2820e 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20  OMIT_TRIGGER.   
2820f 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
28210 6b 4f 54 48 45 52 3b 0a 23 65 6c 73 65 0a 20 20  kOTHER;.#else.  
28211 20 20 20 20 20 20 20 20 73 77 69 74 63 68 28 20          switch( 
28212 2a 7a 53 71 6c 20 29 7b 0a 20 20 20 20 20 20 20  *zSql ){.       
28213 20 20 20 20 20 63 61 73 65 20 27 63 27 3a 20 63       case 'c': c
28214 61 73 65 20 27 43 27 3a 20 7b 0a 20 20 20 20 20  ase 'C': {.     
28215 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 49 64           if( nId
28216 3d 3d 36 20 26 26 20 73 71 6c 69 74 65 33 53 74  ==6 && sqlite3St
28217 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 63 72  rNICmp(zSql, "cr
28218 65 61 74 65 22 2c 20 36 29 3d 3d 30 20 29 7b 0a  eate", 6)==0 ){.
28219 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2821a 74 6f 6b 65 6e 20 3d 20 74 6b 43 52 45 41 54 45  token = tkCREATE
2821b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
2821c 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
2821d 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
2821e 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20  kOTHER;.        
2821f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
28220 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
28221 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
28222 20 20 20 20 20 20 20 63 61 73 65 20 27 74 27 3a         case 't':
28223 20 63 61 73 65 20 27 54 27 3a 20 7b 0a 20 20 20   case 'T': {.   
28224 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6e             if( n
28225 49 64 3d 3d 37 20 26 26 20 73 71 6c 69 74 65 33  Id==7 && sqlite3
28226 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22  StrNICmp(zSql, "
28227 74 72 69 67 67 65 72 22 2c 20 37 29 3d 3d 30 20  trigger", 7)==0 
28228 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
28229 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 54 52 49     token = tkTRI
2822a 47 47 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20  GGER;.          
2822b 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 49      }else if( nI
2822c 64 3d 3d 34 20 26 26 20 73 71 6c 69 74 65 33 53  d==4 && sqlite3S
2822d 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 74  trNICmp(zSql, "t
2822e 65 6d 70 22 2c 20 34 29 3d 3d 30 20 29 7b 0a 20  emp", 4)==0 ){. 
2822f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
28230 6f 6b 65 6e 20 3d 20 74 6b 54 45 4d 50 3b 0a 20  oken = tkTEMP;. 
28231 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c               }el
28232 73 65 20 69 66 28 20 6e 49 64 3d 3d 39 20 26 26  se if( nId==9 &&
28233 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
28234 28 7a 53 71 6c 2c 20 22 74 65 6d 70 6f 72 61 72  (zSql, "temporar
28235 79 22 2c 20 39 29 3d 3d 30 20 29 7b 0a 20 20 20  y", 9)==0 ){.   
28236 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b               tok
28237 65 6e 20 3d 20 74 6b 54 45 4d 50 3b 0a 20 20 20  en = tkTEMP;.   
28238 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
28239 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
2823a 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45    token = tkOTHE
2823b 52 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  R;.             
2823c 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   }.             
2823d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
2823e 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
2823f 20 20 63 61 73 65 20 27 65 27 3a 20 20 63 61 73    case 'e':  cas
28240 65 20 27 45 27 3a 20 7b 0a 20 20 20 20 20 20 20  e 'E': {.       
28241 20 20 20 20 20 20 20 69 66 28 20 6e 49 64 3d 3d         if( nId==
28242 33 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e  3 && sqlite3StrN
28243 49 43 6d 70 28 7a 53 71 6c 2c 20 22 65 6e 64 22  ICmp(zSql, "end"
28244 2c 20 33 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  , 3)==0 ){.     
28245 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e             token
28246 20 3d 20 74 6b 45 4e 44 3b 0a 20 20 20 20 20 20   = tkEND;.      
28247 20 20 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 69          }else.#i
28248 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
28249 54 5f 45 58 50 4c 41 49 4e 0a 20 20 20 20 20 20  T_EXPLAIN.      
2824a 20 20 20 20 20 20 20 20 69 66 28 20 6e 49 64 3d          if( nId=
2824b 3d 37 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  =7 && sqlite3Str
2824c 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 65 78 70  NICmp(zSql, "exp
2824d 6c 61 69 6e 22 2c 20 37 29 3d 3d 30 20 29 7b 0a  lain", 7)==0 ){.
2824e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2824f 74 6f 6b 65 6e 20 3d 20 74 6b 45 58 50 4c 41 49  token = tkEXPLAI
28250 4e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  N;.             
28251 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20   }else.#endif.  
28252 20 20 20 20 20 20 20 20 20 20 20 20 7b 0a 20 20              {.  
28253 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f                to
28254 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20  ken = tkOTHER;. 
28255 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
28256 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65               bre
28257 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ak;.            
28258 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 65  }.            de
28259 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20  fault: {.       
2825a 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
2825b 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20  kOTHER;.        
2825c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2825d 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2825e 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a       }.#endif /*
2825f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
28260 47 47 45 52 20 2a 2f 0a 20 20 20 20 20 20 20 20  GGER */.        
28261 20 20 7a 53 71 6c 20 2b 3d 20 6e 49 64 2d 31 3b    zSql += nId-1;
28262 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
28263 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65            /* Ope
28264 72 61 74 6f 72 73 20 61 6e 64 20 73 70 65 63 69  rators and speci
28265 61 6c 20 73 79 6d 62 6f 6c 73 20 2a 2f 0a 20 20  al symbols */.  
28266 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
28267 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20  tkOTHER;.       
28268 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
28269 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2826a 20 20 20 20 73 74 61 74 65 20 3d 20 74 72 61 6e      state = tran
2826b 73 5b 73 74 61 74 65 5d 5b 74 6f 6b 65 6e 5d 3b  s[state][token];
2826c 0a 20 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20 7d  .    zSql++;.  }
2826d 0a 20 20 72 65 74 75 72 6e 20 73 74 61 74 65 3d  .  return state=
2826e 3d 30 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53  =0;.}..#ifndef S
2826f 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
28270 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
28271 69 6e 65 20 69 73 20 74 68 65 20 73 61 6d 65 20  ine is the same 
28272 61 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63  as the sqlite3_c
28273 6f 6d 70 6c 65 74 65 28 29 20 72 6f 75 74 69 6e  omplete() routin
28274 65 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 61  e described.** a
28275 62 6f 76 65 2c 20 65 78 63 65 70 74 20 74 68 61  bove, except tha
28276 74 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20  t the parameter 
28277 69 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 62  is required to b
28278 65 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64  e UTF-16 encoded
28279 2c 20 6e 6f 74 0a 2a 2a 20 55 54 46 2d 38 2e 0a  , not.** UTF-8..
2827a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
2827b 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65  t sqlite3_comple
2827c 74 65 31 36 28 63 6f 6e 73 74 20 76 6f 69 64 20  te16(const void 
2827d 2a 7a 53 71 6c 29 7b 0a 20 20 73 71 6c 69 74 65  *zSql){.  sqlite
2827e 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20  3_value *pVal;. 
2827f 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 53 71   char const *zSq
28280 6c 38 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  l8;.  int rc = S
28281 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a 20 20  QLITE_NOMEM;..  
28282 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61  pVal = sqlite3Va
28283 6c 75 65 4e 65 77 28 30 29 3b 0a 20 20 73 71 6c  lueNew(0);.  sql
28284 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28  ite3ValueSetStr(
28285 70 56 61 6c 2c 20 2d 31 2c 20 7a 53 71 6c 2c 20  pVal, -1, zSql, 
28286 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49  SQLITE_UTF16NATI
28287 56 45 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  VE, SQLITE_STATI
28288 43 29 3b 0a 20 20 7a 53 71 6c 38 20 3d 20 73 71  C);.  zSql8 = sq
28289 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70  lite3ValueText(p
2828a 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  Val, SQLITE_UTF8
2828b 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 38 20 29  );.  if( zSql8 )
2828c 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
2828d 65 33 5f 63 6f 6d 70 6c 65 74 65 28 7a 53 71 6c  e3_complete(zSql
2828e 38 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  8);.  }.  sqlite
2828f 33 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29  3ValueFree(pVal)
28290 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ;.  return sqlit
28291 65 33 41 70 69 45 78 69 74 28 30 2c 20 72 63 29  e3ApiExit(0, rc)
28292 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
28293 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
28294 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  */.#endif /* SQL
28295 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4c 45 54  ITE_OMIT_COMPLET
28296 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  E */../*********
28297 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 63 6f 6d  ***** End of com
28298 70 6c 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  plete.c ********
28299 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2829a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2829b 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
2829c 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
2829d 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   main.c ********
2829e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2829f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
282a0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
282a1 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
282a2 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
282a3 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
282a4 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
282a5 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
282a6 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
282a7 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
282a8 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
282a9 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
282aa 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
282ab 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
282ac 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
282ad 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
282ae 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
282af 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
282b0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
282b1 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
282b2 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
282b3 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
282b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
282b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
282b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
282b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
282b8 4d 61 69 6e 20 66 69 6c 65 20 66 6f 72 20 74 68  Main file for th
282b9 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79  e SQLite library
282ba 2e 20 20 54 68 65 20 72 6f 75 74 69 6e 65 73 20  .  The routines 
282bb 69 6e 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20  in this file.** 
282bc 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 70 72  implement the pr
282bd 6f 67 72 61 6d 6d 65 72 20 69 6e 74 65 72 66 61  ogrammer interfa
282be 63 65 20 74 6f 20 74 68 65 20 6c 69 62 72 61 72  ce to the librar
282bf 79 2e 20 20 52 6f 75 74 69 6e 65 73 20 69 6e 0a  y.  Routines in.
282c0 2a 2a 20 6f 74 68 65 72 20 66 69 6c 65 73 20 61  ** other files a
282c1 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20  re for internal 
282c2 75 73 65 20 62 79 20 53 51 4c 69 74 65 20 61 6e  use by SQLite an
282c3 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a  d should not be.
282c4 2a 2a 20 61 63 63 65 73 73 65 64 20 62 79 20 75  ** accessed by u
282c5 73 65 72 73 20 6f 66 20 74 68 65 20 6c 69 62 72  sers of the libr
282c6 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  ary..**.** $Id: 
282c7 6d 61 69 6e 2e 63 2c 76 20 31 2e 34 33 39 20 32  main.c,v 1.439 2
282c8 30 30 38 2f 30 35 2f 31 33 20 31 33 3a 32 37 3a  008/05/13 13:27:
282c9 33 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  34 drh Exp $.*/.
282ca 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
282cb 41 42 4c 45 5f 46 54 53 33 0a 2f 2a 2a 2a 2a 2a  ABLE_FTS3./*****
282cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
282cd 65 20 66 74 73 33 2e 68 20 69 6e 20 74 68 65 20  e fts3.h in the 
282ce 6d 69 64 64 6c 65 20 6f 66 20 6d 61 69 6e 2e 63  middle of main.c
282cf 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
282d0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
282d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
282d2 66 69 6c 65 20 66 74 73 33 2e 68 20 2a 2a 2a 2a  file fts3.h ****
282d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
282d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
282d5 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
282d6 32 30 30 36 20 4f 63 74 20 31 30 0a 2a 2a 0a 2a  2006 Oct 10.**.*
282d7 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
282d8 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
282d9 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
282da 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
282db 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
282dc 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
282dd 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
282de 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
282df 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
282e0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
282e1 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
282e2 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
282e3 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
282e4 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
282e5 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
282e6 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
282e7 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
282e8 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
282e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
282ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
282eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
282ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
282ed 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65  **.** This heade
282ee 72 20 66 69 6c 65 20 69 73 20 75 73 65 64 20 62  r file is used b
282ef 79 20 70 72 6f 67 72 61 6d 73 20 74 68 61 74 20  y programs that 
282f0 77 61 6e 74 20 74 6f 20 6c 69 6e 6b 20 61 67 61  want to link aga
282f1 69 6e 73 74 20 74 68 65 0a 2a 2a 20 46 54 53 33  inst the.** FTS3
282f2 20 6c 69 62 72 61 72 79 2e 20 20 41 6c 6c 20 69   library.  All i
282f3 74 20 64 6f 65 73 20 69 73 20 64 65 63 6c 61 72  t does is declar
282f4 65 20 74 68 65 20 73 71 6c 69 74 65 33 46 74 73  e the sqlite3Fts
282f5 33 49 6e 69 74 28 29 20 69 6e 74 65 72 66 61 63  3Init() interfac
282f6 65 2e 0a 2a 2f 0a 0a 23 69 66 20 30 0a 65 78 74  e..*/..#if 0.ext
282f7 65 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69 66  ern "C" {.#endif
282f8 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73    /* __cplusplus
282f9 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56   */..SQLITE_PRIV
282fa 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
282fb 74 73 33 49 6e 69 74 28 73 71 6c 69 74 65 33 20  ts3Init(sqlite3 
282fc 2a 64 62 29 3b 0a 0a 23 69 66 20 30 0a 7d 20 20  *db);..#if 0.}  
282fd 2f 2a 20 65 78 74 65 72 6e 20 22 43 22 20 2a 2f  /* extern "C" */
282fe 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70  .#endif  /* __cp
282ff 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a 2f 2a 2a 2a  lusplus */../***
28300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
28301 6f 66 20 66 74 73 33 2e 68 20 2a 2a 2a 2a 2a 2a  of fts3.h ******
28302 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28303 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28304 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
28305 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
28306 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
28307 6c 65 66 74 20 6f 66 66 20 69 6e 20 6d 61 69 6e  left off in main
28308 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
28309 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64  **********/.#end
2830a 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76 65  if../*.** The ve
2830b 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62  rsion of the lib
2830c 72 61 72 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  rary.*/.SQLITE_A
2830d 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71  PI const char sq
2830e 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20  lite3_version[] 
2830f 3d 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e  = SQLITE_VERSION
28310 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  ;.SQLITE_API con
28311 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
28312 5f 6c 69 62 76 65 72 73 69 6f 6e 28 76 6f 69 64  _libversion(void
28313 29 7b 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  ){ return sqlite
28314 33 5f 76 65 72 73 69 6f 6e 3b 20 7d 0a 53 51 4c  3_version; }.SQL
28315 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
28316 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e  te3_libversion_n
28317 75 6d 62 65 72 28 76 6f 69 64 29 7b 20 72 65 74  umber(void){ ret
28318 75 72 6e 20 53 51 4c 49 54 45 5f 56 45 52 53 49  urn SQLITE_VERSI
28319 4f 4e 5f 4e 55 4d 42 45 52 3b 20 7d 0a 53 51 4c  ON_NUMBER; }.SQL
2831a 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2831b 74 65 33 5f 74 68 72 65 61 64 73 61 66 65 28 76  te3_threadsafe(v
2831c 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c  oid){ return SQL
2831d 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3b 20  ITE_THREADSAFE; 
2831e 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  }..#if !defined(
2831f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
28320 45 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51  E) && defined(SQ
28321 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52  LITE_ENABLE_IOTR
28322 41 43 45 29 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  ACE)./*.** If th
28323 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63  e following func
28324 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20 69 73 20  tion pointer is 
28325 6e 6f 74 20 4e 55 4c 4c 20 61 6e 64 20 69 66 0a  not NULL and if.
28326 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ** SQLITE_ENABLE
28327 5f 49 4f 54 52 41 43 45 20 69 73 20 65 6e 61 62  _IOTRACE is enab
28328 6c 65 64 2c 20 74 68 65 6e 20 6d 65 73 73 61 67  led, then messag
28329 65 73 20 64 65 73 63 72 69 62 69 6e 67 0a 2a 2a  es describing.**
2832a 20 49 2f 4f 20 61 63 74 69 76 65 20 61 72 65 20   I/O active are 
2832b 77 72 69 74 74 65 6e 20 75 73 69 6e 67 20 74 68  written using th
2832c 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  is function.  Th
2832d 65 73 65 20 6d 65 73 73 61 67 65 73 0a 2a 2a 20  ese messages.** 
2832e 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72  are intended for
2832f 20 64 65 62 75 67 67 69 6e 67 20 61 63 74 69 76   debugging activ
28330 69 74 79 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c  ity only..*/.SQL
28331 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
28332 20 28 2a 73 71 6c 69 74 65 33 49 6f 54 72 61 63   (*sqlite3IoTrac
28333 65 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  e)(const char*, 
28334 2e 2e 2e 29 20 3d 20 30 3b 0a 23 65 6e 64 69 66  ...) = 0;.#endif
28335 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66  ../*.** If the f
28336 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20  ollowing global 
28337 76 61 72 69 61 62 6c 65 20 70 6f 69 6e 74 73 20  variable points 
28338 74 6f 20 61 20 73 74 72 69 6e 67 20 77 68 69 63  to a string whic
28339 68 20 69 73 20 74 68 65 0a 2a 2a 20 6e 61 6d 65  h is the.** name
2833a 20 6f 66 20 61 20 64 69 72 65 63 74 6f 72 79 2c   of a directory,
2833b 20 74 68 65 6e 20 74 68 61 74 20 64 69 72 65 63   then that direc
2833c 74 6f 72 79 20 77 69 6c 6c 20 62 65 20 75 73 65  tory will be use
2833d 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 65  d to store.** te
2833e 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 0a 2a  mporary files..*
2833f 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68  *.** See also th
28340 65 20 22 50 52 41 47 4d 41 20 74 65 6d 70 5f 73  e "PRAGMA temp_s
28341 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 22 20  tore_directory" 
28342 53 51 4c 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a  SQL command..*/.
28343 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20  SQLITE_API char 
28344 2a 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69  *sqlite3_temp_di
28345 72 65 63 74 6f 72 79 20 3d 20 30 3b 0a 0a 2f 2a  rectory = 0;../*
28346 0a 2a 2a 20 52 6f 75 74 69 6e 65 20 6e 65 65 64  .** Routine need
28347 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68  ed to support th
28348 65 20 74 65 73 74 63 61 73 65 28 29 20 6d 61 63  e testcase() mac
28349 72 6f 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ro..*/.#ifdef SQ
2834a 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 5f 54 45  LITE_COVERAGE_TE
2834b 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ST.SQLITE_PRIVAT
2834c 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  E void sqlite3Co
2834d 76 65 72 61 67 65 28 69 6e 74 20 78 29 7b 0a 20  verage(int x){. 
2834e 20 73 74 61 74 69 63 20 69 6e 74 20 64 75 6d 6d   static int dumm
2834f 79 20 3d 20 30 3b 0a 20 20 64 75 6d 6d 79 20 2b  y = 0;.  dummy +
28350 3d 20 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a  = x;.}.#endif...
28351 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  /*.** Return tru
28352 65 20 69 66 20 74 68 65 20 62 75 66 66 65 72 20  e if the buffer 
28353 7a 5b 30 2e 2e 6e 2d 31 5d 20 63 6f 6e 74 61 69  z[0..n-1] contai
28354 6e 73 20 61 6c 6c 20 73 70 61 63 65 73 2e 0a 2a  ns all spaces..*
28355 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c  /.static int all
28356 53 70 61 63 65 73 28 63 6f 6e 73 74 20 63 68 61  Spaces(const cha
28357 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20  r *z, int n){.  
28358 77 68 69 6c 65 28 20 6e 3e 30 20 26 26 20 7a 5b  while( n>0 && z[
28359 6e 2d 31 5d 3d 3d 27 20 27 20 29 7b 20 6e 2d 2d  n-1]==' ' ){ n--
2835a 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3d 3d  ; }.  return n==
2835b 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  0;.}../*.** This
2835c 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 20   is the default 
2835d 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69  collating functi
2835e 6f 6e 20 6e 61 6d 65 64 20 22 42 49 4e 41 52 59  on named "BINARY
2835f 22 20 77 68 69 63 68 20 69 73 20 61 6c 77 61 79  " which is alway
28360 73 0a 2a 2a 20 61 76 61 69 6c 61 62 6c 65 2e 0a  s.** available..
28361 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 64  **.** If the pad
28362 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 20 69 73  Flag argument is
28363 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 73   not NULL then s
28364 70 61 63 65 20 70 61 64 64 69 6e 67 20 61 74 20  pace padding at 
28365 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20 73 74  the end.** of st
28366 72 69 6e 67 73 20 69 73 20 69 67 6e 6f 72 65 64  rings is ignored
28367 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e  .  This implemen
28368 74 73 20 74 68 65 20 52 54 52 49 4d 20 63 6f 6c  ts the RTRIM col
28369 6c 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  lation..*/.stati
2836a 63 20 69 6e 74 20 62 69 6e 43 6f 6c 6c 46 75 6e  c int binCollFun
2836b 63 28 0a 20 20 76 6f 69 64 20 2a 70 61 64 46 6c  c(.  void *padFl
2836c 61 67 2c 0a 20 20 69 6e 74 20 6e 4b 65 79 31 2c  ag,.  int nKey1,
2836d 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
2836e 79 31 2c 0a 20 20 69 6e 74 20 6e 4b 65 79 32 2c  y1,.  int nKey2,
2836f 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
28370 79 32 0a 29 7b 0a 20 20 69 6e 74 20 72 63 2c 20  y2.){.  int rc, 
28371 6e 3b 0a 20 20 6e 20 3d 20 6e 4b 65 79 31 3c 6e  n;.  n = nKey1<n
28372 4b 65 79 32 20 3f 20 6e 4b 65 79 31 20 3a 20 6e  Key2 ? nKey1 : n
28373 4b 65 79 32 3b 0a 20 20 72 63 20 3d 20 6d 65 6d  Key2;.  rc = mem
28374 63 6d 70 28 70 4b 65 79 31 2c 20 70 4b 65 79 32  cmp(pKey1, pKey2
28375 2c 20 6e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  , n);.  if( rc==
28376 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 61 64  0 ){.    if( pad
28377 46 6c 61 67 0a 20 20 20 20 20 26 26 20 61 6c 6c  Flag.     && all
28378 53 70 61 63 65 73 28 28 28 63 68 61 72 2a 29 70  Spaces(((char*)p
28379 4b 65 79 31 29 2b 6e 2c 20 6e 4b 65 79 31 2d 6e  Key1)+n, nKey1-n
2837a 29 0a 20 20 20 20 20 26 26 20 61 6c 6c 53 70 61  ).     && allSpa
2837b 63 65 73 28 28 28 63 68 61 72 2a 29 70 4b 65 79  ces(((char*)pKey
2837c 32 29 2b 6e 2c 20 6e 4b 65 79 32 2d 6e 29 0a 20  2)+n, nKey2-n). 
2837d 20 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4c     ){.      /* L
2837e 65 61 76 65 20 72 63 20 75 6e 63 68 61 6e 67 65  eave rc unchange
2837f 64 20 61 74 20 30 20 2a 2f 0a 20 20 20 20 7d 65  d at 0 */.    }e
28380 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
28381 6e 4b 65 79 31 20 2d 20 6e 4b 65 79 32 3b 0a 20  nKey1 - nKey2;. 
28382 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
28383 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  n rc;.}../*.** A
28384 6e 6f 74 68 65 72 20 62 75 69 6c 74 2d 69 6e 20  nother built-in 
28385 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
28386 63 65 3a 20 4e 4f 43 41 53 45 2e 20 0a 2a 2a 0a  ce: NOCASE. .**.
28387 2a 2a 20 54 68 69 73 20 63 6f 6c 6c 61 74 69 6e  ** This collatin
28388 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 69 6e  g sequence is in
28389 74 65 6e 64 65 64 20 74 6f 20 62 65 20 75 73 65  tended to be use
2838a 64 20 66 6f 72 20 22 63 61 73 65 20 69 6e 64 65  d for "case inde
2838b 70 65 6e 64 61 6e 74 0a 2a 2a 20 63 6f 6d 70 61  pendant.** compa
2838c 72 69 73 6f 6e 22 2e 20 53 51 4c 69 74 65 27 73  rison". SQLite's
2838d 20 6b 6e 6f 77 6c 65 64 67 65 20 6f 66 20 75 70   knowledge of up
2838e 70 65 72 20 61 6e 64 20 6c 6f 77 65 72 20 63 61  per and lower ca
2838f 73 65 20 65 71 75 69 76 61 6c 65 6e 74 73 0a 2a  se equivalents.*
28390 2a 20 65 78 74 65 6e 64 73 20 6f 6e 6c 79 20 74  * extends only t
28391 6f 20 74 68 65 20 32 36 20 63 68 61 72 61 63 74  o the 26 charact
28392 65 72 73 20 75 73 65 64 20 69 6e 20 74 68 65 20  ers used in the 
28393 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65  English language
28394 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74 68 65 20 6d  ..**.** At the m
28395 6f 6d 65 6e 74 20 74 68 65 72 65 20 69 73 20 6f  oment there is o
28396 6e 6c 79 20 61 20 55 54 46 2d 38 20 69 6d 70 6c  nly a UTF-8 impl
28397 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 73  ementation..*/.s
28398 74 61 74 69 63 20 69 6e 74 20 6e 6f 63 61 73 65  tatic int nocase
28399 43 6f 6c 6c 61 74 69 6e 67 46 75 6e 63 28 0a 20  CollatingFunc(. 
2839a 20 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 2c 0a   void *NotUsed,.
2839b 20 20 69 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e    int nKey1, con
2839c 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 0a  st void *pKey1,.
2839d 20 20 69 6e 74 20 6e 4b 65 79 32 2c 20 63 6f 6e    int nKey2, con
2839e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32 0a 29  st void *pKey2.)
2839f 7b 0a 20 20 69 6e 74 20 72 20 3d 20 73 71 6c 69  {.  int r = sqli
283a0 74 65 33 53 74 72 4e 49 43 6d 70 28 0a 20 20 20  te3StrNICmp(.   
283a1 20 20 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a     (const char *
283a2 29 70 4b 65 79 31 2c 20 28 63 6f 6e 73 74 20 63  )pKey1, (const c
283a3 68 61 72 20 2a 29 70 4b 65 79 32 2c 20 28 6e 4b  har *)pKey2, (nK
283a4 65 79 31 3c 6e 4b 65 79 32 29 3f 6e 4b 65 79 31  ey1<nKey2)?nKey1
283a5 3a 6e 4b 65 79 32 29 3b 0a 20 20 69 66 28 20 30  :nKey2);.  if( 0
283a6 3d 3d 72 20 29 7b 0a 20 20 20 20 72 20 3d 20 6e  ==r ){.    r = n
283a7 4b 65 79 31 2d 6e 4b 65 79 32 3b 0a 20 20 7d 0a  Key1-nKey2;.  }.
283a8 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a 0a 2f    return r;.}../
283a9 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
283aa 52 4f 57 49 44 20 6f 66 20 74 68 65 20 6d 6f 73  ROWID of the mos
283ab 74 20 72 65 63 65 6e 74 20 69 6e 73 65 72 74 0a  t recent insert.
283ac 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  */.SQLITE_API sq
283ad 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74  lite_int64 sqlit
283ae 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  e3_last_insert_r
283af 6f 77 69 64 28 73 71 6c 69 74 65 33 20 2a 64 62  owid(sqlite3 *db
283b0 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e  ){.  return db->
283b1 6c 61 73 74 52 6f 77 69 64 3b 0a 7d 0a 0a 2f 2a  lastRowid;.}../*
283b2 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
283b3 75 6d 62 65 72 20 6f 66 20 63 68 61 6e 67 65 73  umber of changes
283b4 20 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63   in the most rec
283b5 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  ent call to sqli
283b6 74 65 33 5f 65 78 65 63 28 29 2e 0a 2a 2f 0a 53  te3_exec()..*/.S
283b7 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
283b8 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 73 71  lite3_changes(sq
283b9 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 72 65  lite3 *db){.  re
283ba 74 75 72 6e 20 64 62 2d 3e 6e 43 68 61 6e 67 65  turn db->nChange
283bb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
283bc 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
283bd 63 68 61 6e 67 65 73 20 73 69 6e 63 65 20 74 68  changes since th
283be 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
283bf 65 20 77 61 73 20 6f 70 65 6e 65 64 2e 0a 2a 2f  e was opened..*/
283c0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
283c1 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68  sqlite3_total_ch
283c2 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20 2a 64  anges(sqlite3 *d
283c3 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 2d  b){.  return db-
283c4 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 3b 0a 7d  >nTotalChange;.}
283c5 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e  ../*.** Close an
283c6 20 65 78 69 73 74 69 6e 67 20 53 51 4c 69 74 65   existing SQLite
283c7 20 64 61 74 61 62 61 73 65 0a 2a 2f 0a 53 51 4c   database.*/.SQL
283c8 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
283c9 74 65 33 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65  te3_close(sqlite
283ca 33 20 2a 64 62 29 7b 0a 20 20 48 61 73 68 45 6c  3 *db){.  HashEl
283cb 65 6d 20 2a 69 3b 0a 20 20 69 6e 74 20 6a 3b 0a  em *i;.  int j;.
283cc 0a 20 20 69 66 28 20 21 64 62 20 29 7b 0a 20 20  .  if( !db ){.  
283cd 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
283ce 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 73  OK;.  }.  if( !s
283cf 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63  qlite3SafetyChec
283d0 6b 53 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29 7b  kSickOrOk(db) ){
283d1 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
283d2 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20  TE_MISUSE;.  }. 
283d3 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
283d4 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
283d5 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
283d6 53 53 45 0a 20 20 7b 0a 20 20 20 20 65 78 74 65  SSE.  {.    exte
283d7 72 6e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  rn void sqlite3S
283d8 73 65 43 6c 65 61 6e 75 70 28 73 71 6c 69 74 65  seCleanup(sqlite
283d9 33 2a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  3*);.    sqlite3
283da 53 73 65 43 6c 65 61 6e 75 70 28 64 62 29 3b 0a  SseCleanup(db);.
283db 20 20 7d 0a 23 65 6e 64 69 66 20 0a 0a 20 20 73    }.#endif ..  s
283dc 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
283dd 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29  nalSchema(db, 0)
283de 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 20 74 72 61  ;..  /* If a tra
283df 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e  nsaction is open
283e0 2c 20 74 68 65 20 52 65 73 65 74 49 6e 74 65 72  , the ResetInter
283e1 6e 61 6c 53 63 68 65 6d 61 28 29 20 63 61 6c 6c  nalSchema() call
283e2 20 61 62 6f 76 65 0a 20 20 2a 2a 20 77 69 6c 6c   above.  ** will
283e3 20 6e 6f 74 20 68 61 76 65 20 63 61 6c 6c 65 64   not have called
283e4 20 74 68 65 20 78 44 69 73 63 6f 6e 6e 65 63 74   the xDisconnect
283e5 28 29 20 6d 65 74 68 6f 64 20 6f 6e 20 61 6e 79  () method on any
283e6 20 76 69 72 74 75 61 6c 0a 20 20 2a 2a 20 74 61   virtual.  ** ta
283e7 62 6c 65 73 20 69 6e 20 74 68 65 20 64 62 2d 3e  bles in the db->
283e8 61 56 54 72 61 6e 73 5b 5d 20 61 72 72 61 79 2e  aVTrans[] array.
283e9 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   The following s
283ea 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61  qlite3VtabRollba
283eb 63 6b 28 29 0a 20 20 2a 2a 20 63 61 6c 6c 20 77  ck().  ** call w
283ec 69 6c 6c 20 64 6f 20 73 6f 2e 20 57 65 20 6e 65  ill do so. We ne
283ed 65 64 20 74 6f 20 64 6f 20 74 68 69 73 20 62 65  ed to do this be
283ee 66 6f 72 65 20 74 68 65 20 63 68 65 63 6b 20 66  fore the check f
283ef 6f 72 20 61 63 74 69 76 65 0a 20 20 2a 2a 20 53  or active.  ** S
283f0 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 62 65  QL statements be
283f1 6c 6f 77 2c 20 61 73 20 74 68 65 20 76 2d 74 61  low, as the v-ta
283f2 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ble implementati
283f3 6f 6e 20 6d 61 79 20 62 65 20 73 74 6f 72 69 6e  on may be storin
283f4 67 0a 20 20 2a 2a 20 73 6f 6d 65 20 70 72 65 70  g.  ** some prep
283f5 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20  ared statements 
283f6 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 2a 2f  internally..  */
283f7 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f  .  sqlite3VtabRo
283f8 6c 6c 62 61 63 6b 28 64 62 29 3b 0a 0a 20 20 2f  llback(db);..  /
283f9 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61  * If there are a
283fa 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 56  ny outstanding V
283fb 4d 73 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  Ms, return SQLIT
283fc 45 5f 42 55 53 59 2e 20 2a 2f 0a 20 20 69 66 28  E_BUSY. */.  if(
283fd 20 64 62 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20   db->pVdbe ){.  
283fe 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
283ff 62 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 2c 20  b, SQLITE_BUSY, 
28400 0a 20 20 20 20 20 20 20 20 22 55 6e 61 62 6c 65  .        "Unable
28401 20 74 6f 20 63 6c 6f 73 65 20 64 75 65 20 74 6f   to close due to
28402 20 75 6e 66 69 6e 61 6c 69 73 65 64 20 73 74 61   unfinalised sta
28403 74 65 6d 65 6e 74 73 22 29 3b 0a 20 20 20 20 73  tements");.    s
28404 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
28405 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
28406 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
28407 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 61 73 73  _BUSY;.  }.  ass
28408 65 72 74 28 20 73 71 6c 69 74 65 33 53 61 66 65  ert( sqlite3Safe
28409 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28  tyCheckSickOrOk(
2840a 64 62 29 20 29 3b 0a 0a 20 20 66 6f 72 28 6a 3d  db) );..  for(j=
2840b 30 3b 20 6a 3c 64 62 2d 3e 6e 44 62 3b 20 6a 2b  0; j<db->nDb; j+
2840c 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 44  +){.    struct D
2840d 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44  b *pDb = &db->aD
2840e 62 5b 6a 5d 3b 0a 20 20 20 20 69 66 28 20 70 44  b[j];.    if( pD
2840f 62 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 20  b->pBt ){.      
28410 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73  sqlite3BtreeClos
28411 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20  e(pDb->pBt);.   
28412 20 20 20 70 44 62 2d 3e 70 42 74 20 3d 20 30 3b     pDb->pBt = 0;
28413 0a 20 20 20 20 20 20 69 66 28 20 6a 21 3d 31 20  .      if( j!=1 
28414 29 7b 0a 20 20 20 20 20 20 20 20 70 44 62 2d 3e  ){.        pDb->
28415 70 53 63 68 65 6d 61 20 3d 20 30 3b 0a 20 20 20  pSchema = 0;.   
28416 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
28417 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
28418 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20  ernalSchema(db, 
28419 30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62  0);.  assert( db
2841a 2d 3e 6e 44 62 3c 3d 32 20 29 3b 0a 20 20 61 73  ->nDb<=2 );.  as
2841b 73 65 72 74 28 20 64 62 2d 3e 61 44 62 3d 3d 64  sert( db->aDb==d
2841c 62 2d 3e 61 44 62 53 74 61 74 69 63 20 29 3b 0a  b->aDbStatic );.
2841d 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61    for(i=sqliteHa
2841e 73 68 46 69 72 73 74 28 26 64 62 2d 3e 61 46 75  shFirst(&db->aFu
2841f 6e 63 29 3b 20 69 3b 20 69 3d 73 71 6c 69 74 65  nc); i; i=sqlite
28420 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20 20  HashNext(i)){.  
28421 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63    FuncDef *pFunc
28422 2c 20 2a 70 4e 65 78 74 3b 0a 20 20 20 20 66 6f  , *pNext;.    fo
28423 72 28 70 46 75 6e 63 20 3d 20 28 46 75 6e 63 44  r(pFunc = (FuncD
28424 65 66 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61  ef*)sqliteHashDa
28425 74 61 28 69 29 3b 20 70 46 75 6e 63 3b 20 70 46  ta(i); pFunc; pF
28426 75 6e 63 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20  unc=pNext){.    
28427 20 20 70 4e 65 78 74 20 3d 20 70 46 75 6e 63 2d    pNext = pFunc-
28428 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 73 71  >pNext;.      sq
28429 6c 69 74 65 33 5f 66 72 65 65 28 70 46 75 6e 63  lite3_free(pFunc
2842a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
2842b 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68  for(i=sqliteHash
2842c 46 69 72 73 74 28 26 64 62 2d 3e 61 43 6f 6c 6c  First(&db->aColl
2842d 53 65 71 29 3b 20 69 3b 20 69 3d 73 71 6c 69 74  Seq); i; i=sqlit
2842e 65 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20  eHashNext(i)){. 
2842f 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c     CollSeq *pCol
28430 6c 20 3d 20 28 43 6f 6c 6c 53 65 71 20 2a 29 73  l = (CollSeq *)s
28431 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 69 29  qliteHashData(i)
28432 3b 0a 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 20  ;.    /* Invoke 
28433 61 6e 79 20 64 65 73 74 72 75 63 74 6f 72 73 20  any destructors 
28434 72 65 67 69 73 74 65 72 65 64 20 66 6f 72 20 63  registered for c
28435 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
28436 65 20 75 73 65 72 20 64 61 74 61 2e 20 2a 2f 0a  e user data. */.
28437 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 33      for(j=0; j<3
28438 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; j++){.      if
28439 28 20 70 43 6f 6c 6c 5b 6a 5d 2e 78 44 65 6c 20  ( pColl[j].xDel 
2843a 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c  ){.        pColl
2843b 5b 6a 5d 2e 78 44 65 6c 28 70 43 6f 6c 6c 5b 6a  [j].xDel(pColl[j
2843c 5d 2e 70 55 73 65 72 29 3b 0a 20 20 20 20 20 20  ].pUser);.      
2843d 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
2843e 74 65 33 5f 66 72 65 65 28 70 43 6f 6c 6c 29 3b  te3_free(pColl);
2843f 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 48 61  .  }.  sqlite3Ha
28440 73 68 43 6c 65 61 72 28 26 64 62 2d 3e 61 43 6f  shClear(&db->aCo
28441 6c 6c 53 65 71 29 3b 0a 23 69 66 6e 64 65 66 20  llSeq);.#ifndef 
28442 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
28443 55 41 4c 54 41 42 4c 45 0a 20 20 66 6f 72 28 69  UALTABLE.  for(i
28444 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74  =sqliteHashFirst
28445 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65 29 3b 20  (&db->aModule); 
28446 69 3b 20 69 3d 73 71 6c 69 74 65 48 61 73 68 4e  i; i=sqliteHashN
28447 65 78 74 28 69 29 29 7b 0a 20 20 20 20 4d 6f 64  ext(i)){.    Mod
28448 75 6c 65 20 2a 70 4d 6f 64 20 3d 20 28 4d 6f 64  ule *pMod = (Mod
28449 75 6c 65 20 2a 29 73 71 6c 69 74 65 48 61 73 68  ule *)sqliteHash
2844a 44 61 74 61 28 69 29 3b 0a 20 20 20 20 69 66 28  Data(i);.    if(
2844b 20 70 4d 6f 64 2d 3e 78 44 65 73 74 72 6f 79 20   pMod->xDestroy 
2844c 29 7b 0a 20 20 20 20 20 20 70 4d 6f 64 2d 3e 78  ){.      pMod->x
2844d 44 65 73 74 72 6f 79 28 70 4d 6f 64 2d 3e 70 41  Destroy(pMod->pA
2844e 75 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ux);.    }.    s
2844f 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4d 6f 64  qlite3_free(pMod
28450 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
28451 48 61 73 68 43 6c 65 61 72 28 26 64 62 2d 3e 61  HashClear(&db->a
28452 4d 6f 64 75 6c 65 29 3b 0a 23 65 6e 64 69 66 0a  Module);.#endif.
28453 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c  .  sqlite3HashCl
28454 65 61 72 28 26 64 62 2d 3e 61 46 75 6e 63 29 3b  ear(&db->aFunc);
28455 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28  .  sqlite3Error(
28456 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30  db, SQLITE_OK, 0
28457 29 3b 20 2f 2a 20 44 65 61 6c 6c 6f 63 61 74 65  ); /* Deallocate
28458 73 20 61 6e 79 20 63 61 63 68 65 64 20 65 72 72  s any cached err
28459 6f 72 20 73 74 72 69 6e 67 73 2e 20 2a 2f 0a 20  or strings. */. 
2845a 20 69 66 28 20 64 62 2d 3e 70 45 72 72 20 29 7b   if( db->pErr ){
2845b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75  .    sqlite3Valu
2845c 65 46 72 65 65 28 64 62 2d 3e 70 45 72 72 29 3b  eFree(db->pErr);
2845d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 43 6c  .  }.  sqlite3Cl
2845e 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 64 62  oseExtensions(db
2845f 29 3b 0a 0a 20 20 64 62 2d 3e 6d 61 67 69 63 20  );..  db->magic 
28460 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45  = SQLITE_MAGIC_E
28461 52 52 4f 52 3b 0a 0a 20 20 2f 2a 20 54 68 65 20  RROR;..  /* The 
28462 74 65 6d 70 2d 64 61 74 61 62 61 73 65 20 73 63  temp-database sc
28463 68 65 6d 61 20 69 73 20 61 6c 6c 6f 63 61 74 65  hema is allocate
28464 64 20 64 69 66 66 65 72 65 6e 74 6c 79 20 66 72  d differently fr
28465 6f 6d 20 74 68 65 20 6f 74 68 65 72 20 73 63 68  om the other sch
28466 65 6d 61 0a 20 20 2a 2a 20 6f 62 6a 65 63 74 73  ema.  ** objects
28467 20 28 75 73 69 6e 67 20 73 71 6c 69 74 65 4d 61   (using sqliteMa
28468 6c 6c 6f 63 28 29 20 64 69 72 65 63 74 6c 79 2c  lloc() directly,
28469 20 69 6e 73 74 65 61 64 20 6f 66 20 73 71 6c 69   instead of sqli
2846a 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 28 29  te3BtreeSchema()
2846b 29 2e 0a 20 20 2a 2a 20 53 6f 20 69 74 20 6e 65  )..  ** So it ne
2846c 65 64 73 20 74 6f 20 62 65 20 66 72 65 65 64 20  eds to be freed 
2846d 68 65 72 65 2e 20 54 6f 64 6f 3a 20 57 68 79 20  here. Todo: Why 
2846e 6e 6f 74 20 72 6f 6c 6c 20 74 68 65 20 74 65 6d  not roll the tem
2846f 70 20 73 63 68 65 6d 61 20 69 6e 74 6f 0a 20 20  p schema into.  
28470 2a 2a 20 74 68 65 20 73 61 6d 65 20 73 71 6c 69  ** the same sqli
28471 74 65 4d 61 6c 6c 6f 63 28 29 20 61 73 20 74 68  teMalloc() as th
28472 65 20 6f 6e 65 20 74 68 61 74 20 61 6c 6c 6f 63  e one that alloc
28473 61 74 65 73 20 74 68 65 20 64 61 74 61 62 61 73  ates the databas
28474 65 20 0a 20 20 2a 2a 20 73 74 72 75 63 74 75 72  e .  ** structur
28475 65 3f 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  e?.  */.  sqlite
28476 33 5f 66 72 65 65 28 64 62 2d 3e 61 44 62 5b 31  3_free(db->aDb[1
28477 5d 2e 70 53 63 68 65 6d 61 29 3b 0a 20 20 73 71  ].pSchema);.  sq
28478 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
28479 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
2847a 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49  db->magic = SQLI
2847b 54 45 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 44 3b  TE_MAGIC_CLOSED;
2847c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2847d 5f 66 72 65 65 28 64 62 2d 3e 6d 75 74 65 78 29  _free(db->mutex)
2847e 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
2847f 28 64 62 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  (db);.  return S
28480 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
28481 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20  ** Rollback all 
28482 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 2e 0a  database files..
28483 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
28484 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f  E void sqlite3Ro
28485 6c 6c 62 61 63 6b 41 6c 6c 28 73 71 6c 69 74 65  llbackAll(sqlite
28486 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b  3 *db){.  int i;
28487 0a 20 20 69 6e 74 20 69 6e 54 72 61 6e 73 20 3d  .  int inTrans =
28488 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   0;.  assert( sq
28489 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
2848a 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
2848b 20 73 71 6c 69 74 65 33 46 61 75 6c 74 42 65 67   sqlite3FaultBeg
2848c 69 6e 42 65 6e 69 67 6e 28 53 51 4c 49 54 45 5f  inBenign(SQLITE_
2848d 46 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 4d 41  FAULTINJECTOR_MA
2848e 4c 4c 4f 43 29 3b 0a 20 20 66 6f 72 28 69 3d 30  LLOC);.  for(i=0
2848f 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
28490 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61  ){.    if( db->a
28491 44 62 5b 69 5d 2e 70 42 74 20 29 7b 0a 20 20 20  Db[i].pBt ){.   
28492 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74     if( sqlite3Bt
28493 72 65 65 49 73 49 6e 54 72 61 6e 73 28 64 62 2d  reeIsInTrans(db-
28494 3e 61 44 62 5b 69 5d 2e 70 42 74 29 20 29 7b 0a  >aDb[i].pBt) ){.
28495 20 20 20 20 20 20 20 20 69 6e 54 72 61 6e 73 20          inTrans 
28496 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
28497 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 52     sqlite3BtreeR
28498 6f 6c 6c 62 61 63 6b 28 64 62 2d 3e 61 44 62 5b  ollback(db->aDb[
28499 69 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20 20 64  i].pBt);.      d
2849a 62 2d 3e 61 44 62 5b 69 5d 2e 69 6e 54 72 61 6e  b->aDb[i].inTran
2849b 73 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  s = 0;.    }.  }
2849c 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f  .  sqlite3VtabRo
2849d 6c 6c 62 61 63 6b 28 64 62 29 3b 0a 20 20 73 71  llback(db);.  sq
2849e 6c 69 74 65 33 46 61 75 6c 74 45 6e 64 42 65 6e  lite3FaultEndBen
2849f 69 67 6e 28 53 51 4c 49 54 45 5f 46 41 55 4c 54  ign(SQLITE_FAULT
284a0 49 4e 4a 45 43 54 4f 52 5f 4d 41 4c 4c 4f 43 29  INJECTOR_MALLOC)
284a1 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61  ;..  if( db->fla
284a2 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e  gs&SQLITE_Intern
284a3 43 68 61 6e 67 65 73 20 29 7b 0a 20 20 20 20 73  Changes ){.    s
284a4 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70  qlite3ExpirePrep
284a5 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64  aredStatements(d
284a6 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52  b);.    sqlite3R
284a7 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65  esetInternalSche
284a8 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 0a  ma(db, 0);.  }..
284a9 20 20 2f 2a 20 49 66 20 6f 6e 65 20 68 61 73 20    /* If one has 
284aa 62 65 65 6e 20 63 6f 6e 66 69 67 75 72 65 64 2c  been configured,
284ab 20 69 6e 76 6f 6b 65 20 74 68 65 20 72 6f 6c 6c   invoke the roll
284ac 62 61 63 6b 2d 68 6f 6f 6b 20 63 61 6c 6c 62 61  back-hook callba
284ad 63 6b 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  ck */.  if( db->
284ae 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63  xRollbackCallbac
284af 6b 20 26 26 20 28 69 6e 54 72 61 6e 73 20 7c 7c  k && (inTrans ||
284b0 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74   !db->autoCommit
284b1 29 20 29 7b 0a 20 20 20 20 64 62 2d 3e 78 52 6f  ) ){.    db->xRo
284b2 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 28 64  llbackCallback(d
284b3 62 2d 3e 70 52 6f 6c 6c 62 61 63 6b 41 72 67 29  b->pRollbackArg)
284b4 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ;.  }.}../*.** R
284b5 65 74 75 72 6e 20 61 20 73 74 61 74 69 63 20 73  eturn a static s
284b6 74 72 69 6e 67 20 74 68 61 74 20 64 65 73 63 72  tring that descr
284b7 69 62 65 73 20 74 68 65 20 6b 69 6e 64 20 6f 66  ibes the kind of
284b8 20 65 72 72 6f 72 20 73 70 65 63 69 66 69 65 64   error specified
284b9 20 69 6e 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d   in the.** argum
284ba 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ent..*/.SQLITE_P
284bb 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61  RIVATE const cha
284bc 72 20 2a 73 71 6c 69 74 65 33 45 72 72 53 74 72  r *sqlite3ErrStr
284bd 28 69 6e 74 20 72 63 29 7b 0a 20 20 63 6f 6e 73  (int rc){.  cons
284be 74 20 63 68 61 72 20 2a 7a 3b 0a 20 20 73 77 69  t char *z;.  swi
284bf 74 63 68 28 20 72 63 20 26 20 30 78 66 66 20 29  tch( rc & 0xff )
284c0 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  {.    case SQLIT
284c1 45 5f 52 4f 57 3a 0a 20 20 20 20 63 61 73 65 20  E_ROW:.    case 
284c2 53 51 4c 49 54 45 5f 44 4f 4e 45 3a 0a 20 20 20  SQLITE_DONE:.   
284c3 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4f 4b 3a   case SQLITE_OK:
284c4 20 20 20 20 20 20 20 20 20 7a 20 3d 20 22 6e 6f           z = "no
284c5 74 20 61 6e 20 65 72 72 6f 72 22 3b 20 20 20 20  t an error";    
284c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
284c7 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
284c8 20 63 61 73 65 20 53 51 4c 49 54 45 5f 45 52 52   case SQLITE_ERR
284c9 4f 52 3a 20 20 20 20 20 20 7a 20 3d 20 22 53 51  OR:      z = "SQ
284ca 4c 20 6c 6f 67 69 63 20 65 72 72 6f 72 20 6f 72  L logic error or
284cb 20 6d 69 73 73 69 6e 67 20 64 61 74 61 62 61 73   missing databas
284cc 65 22 3b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  e";   break;.   
284cd 20 63 61 73 65 20 53 51 4c 49 54 45 5f 50 45 52   case SQLITE_PER
284ce 4d 3a 20 20 20 20 20 20 20 7a 20 3d 20 22 61 63  M:       z = "ac
284cf 63 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 20  cess permission 
284d0 64 65 6e 69 65 64 22 3b 20 20 20 20 20 20 20 20  denied";        
284d1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
284d2 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 42 4f   case SQLITE_ABO
284d3 52 54 3a 20 20 20 20 20 20 7a 20 3d 20 22 63 61  RT:      z = "ca
284d4 6c 6c 62 61 63 6b 20 72 65 71 75 65 73 74 65 64  llback requested
284d5 20 71 75 65 72 79 20 61 62 6f 72 74 22 3b 20 20   query abort";  
284d6 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
284d7 20 63 61 73 65 20 53 51 4c 49 54 45 5f 42 55 53   case SQLITE_BUS
284d8 59 3a 20 20 20 20 20 20 20 7a 20 3d 20 22 64 61  Y:       z = "da
284d9 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b 65 64  tabase is locked
284da 22 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ";              
284db 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
284dc 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4c 4f 43   case SQLITE_LOC
284dd 4b 45 44 3a 20 20 20 20 20 7a 20 3d 20 22 64 61  KED:     z = "da
284de 74 61 62 61 73 65 20 74 61 62 6c 65 20 69 73 20  tabase table is 
284df 6c 6f 63 6b 65 64 22 3b 20 20 20 20 20 20 20 20  locked";        
284e0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
284e1 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 4f 4d   case SQLITE_NOM
284e2 45 4d 3a 20 20 20 20 20 20 7a 20 3d 20 22 6f 75  EM:      z = "ou
284e3 74 20 6f 66 20 6d 65 6d 6f 72 79 22 3b 20 20 20  t of memory";   
284e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
284e5 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
284e6 20 63 61 73 65 20 53 51 4c 49 54 45 5f 52 45 41   case SQLITE_REA
284e7 44 4f 4e 4c 59 3a 20 20 20 7a 20 3d 20 22 61 74  DONLY:   z = "at
284e8 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20 61  tempt to write a
284e9 20 72 65 61 64 6f 6e 6c 79 20 64 61 74 61 62 61   readonly databa
284ea 73 65 22 3b 20 20 62 72 65 61 6b 3b 0a 20 20 20  se";  break;.   
284eb 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54   case SQLITE_INT
284ec 45 52 52 55 50 54 3a 20 20 7a 20 3d 20 22 69 6e  ERRUPT:  z = "in
284ed 74 65 72 72 75 70 74 65 64 22 3b 20 20 20 20 20  terrupted";     
284ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
284ef 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
284f0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4f 45   case SQLITE_IOE
284f1 52 52 3a 20 20 20 20 20 20 7a 20 3d 20 22 64 69  RR:      z = "di
284f2 73 6b 20 49 2f 4f 20 65 72 72 6f 72 22 3b 20 20  sk I/O error";  
284f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
284f4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
284f5 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 52   case SQLITE_COR
284f6 52 55 50 54 3a 20 20 20 20 7a 20 3d 20 22 64 61  RUPT:    z = "da
284f7 74 61 62 61 73 65 20 64 69 73 6b 20 69 6d 61 67  tabase disk imag
284f8 65 20 69 73 20 6d 61 6c 66 6f 72 6d 65 64 22 3b  e is malformed";
284f9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
284fa 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 55 4c   case SQLITE_FUL
284fb 4c 3a 20 20 20 20 20 20 20 7a 20 3d 20 22 64 61  L:       z = "da
284fc 74 61 62 61 73 65 20 6f 72 20 64 69 73 6b 20 69  tabase or disk i
284fd 73 20 66 75 6c 6c 22 3b 20 20 20 20 20 20 20 20  s full";        
284fe 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
284ff 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 41 4e   case SQLITE_CAN
28500 54 4f 50 45 4e 3a 20 20 20 7a 20 3d 20 22 75 6e  TOPEN:   z = "un
28501 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 64 61 74  able to open dat
28502 61 62 61 73 65 20 66 69 6c 65 22 3b 20 20 20 20  abase file";    
28503 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
28504 20 63 61 73 65 20 53 51 4c 49 54 45 5f 45 4d 50   case SQLITE_EMP
28505 54 59 3a 20 20 20 20 20 20 7a 20 3d 20 22 74 61  TY:      z = "ta
28506 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20  ble contains no 
28507 64 61 74 61 22 3b 20 20 20 20 20 20 20 20 20 20  data";          
28508 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
28509 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53 43 48   case SQLITE_SCH
2850a 45 4d 41 3a 20 20 20 20 20 7a 20 3d 20 22 64 61  EMA:     z = "da
2850b 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 68 61  tabase schema ha
2850c 73 20 63 68 61 6e 67 65 64 22 3b 20 20 20 20 20  s changed";     
2850d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2850e 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 4f 4f   case SQLITE_TOO
2850f 42 49 47 3a 20 20 20 20 20 7a 20 3d 20 22 53 74  BIG:     z = "St
28510 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 65 78 63  ring or BLOB exc
28511 65 65 64 65 64 20 73 69 7a 65 20 6c 69 6d 69 74  eeded size limit
28512 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  ";    break;.   
28513 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e   case SQLITE_CON
28514 53 54 52 41 49 4e 54 3a 20 7a 20 3d 20 22 63 6f  STRAINT: z = "co
28515 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 22  nstraint failed"
28516 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
28517 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
28518 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 49 53   case SQLITE_MIS
28519 4d 41 54 43 48 3a 20 20 20 7a 20 3d 20 22 64 61  MATCH:   z = "da
2851a 74 61 74 79 70 65 20 6d 69 73 6d 61 74 63 68 22  tatype mismatch"
2851b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2851c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2851d 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 49 53   case SQLITE_MIS
2851e 55 53 45 3a 20 20 20 20 20 7a 20 3d 20 22 6c 69  USE:     z = "li
2851f 62 72 61 72 79 20 72 6f 75 74 69 6e 65 20 63 61  brary routine ca
28520 6c 6c 65 64 20 6f 75 74 20 6f 66 20 73 65 71 75  lled out of sequ
28521 65 6e 63 65 22 3b 62 72 65 61 6b 3b 0a 20 20 20  ence";break;.   
28522 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 4f 4c   case SQLITE_NOL
28523 46 53 3a 20 20 20 20 20 20 7a 20 3d 20 22 6c 61  FS:      z = "la
28524 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74  rge file support
28525 20 69 73 20 64 69 73 61 62 6c 65 64 22 3b 20 20   is disabled";  
28526 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
28527 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 55 54   case SQLITE_AUT
28528 48 3a 20 20 20 20 20 20 20 7a 20 3d 20 22 61 75  H:       z = "au
28529 74 68 6f 72 69 7a 61 74 69 6f 6e 20 64 65 6e 69  thorization deni
2852a 65 64 22 3b 20 20 20 20 20 20 20 20 20 20 20 20  ed";            
2852b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2852c 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4f 52   case SQLITE_FOR
2852d 4d 41 54 3a 20 20 20 20 20 7a 20 3d 20 22 61 75  MAT:     z = "au
2852e 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65  xiliary database
2852f 20 66 6f 72 6d 61 74 20 65 72 72 6f 72 22 3b 20   format error"; 
28530 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
28531 20 63 61 73 65 20 53 51 4c 49 54 45 5f 52 41 4e   case SQLITE_RAN
28532 47 45 3a 20 20 20 20 20 20 7a 20 3d 20 22 62 69  GE:      z = "bi
28533 6e 64 20 6f 72 20 63 6f 6c 75 6d 6e 20 69 6e 64  nd or column ind
28534 65 78 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 22  ex out of range"
28535 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  ;     break;.   
28536 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 4f 54   case SQLITE_NOT
28537 41 44 42 3a 20 20 20 20 20 7a 20 3d 20 22 66 69  ADB:     z = "fi
28538 6c 65 20 69 73 20 65 6e 63 72 79 70 74 65 64 20  le is encrypted 
28539 6f 72 20 69 73 20 6e 6f 74 20 61 20 64 61 74 61  or is not a data
2853a 62 61 73 65 22 3b 62 72 65 61 6b 3b 0a 20 20 20  base";break;.   
2853b 20 64 65 66 61 75 6c 74 3a 20 20 20 20 20 20 20   default:       
2853c 20 20 20 20 20 20 20 20 20 7a 20 3d 20 22 75 6e           z = "un
2853d 6b 6e 6f 77 6e 20 65 72 72 6f 72 22 3b 20 20 20  known error";   
2853e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2853f 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d        break;.  }
28540 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a  .  return z;.}..
28541 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
28542 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20  ne implements a 
28543 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 74 68  busy callback th
28544 61 74 20 73 6c 65 65 70 73 20 61 6e 64 20 74 72  at sleeps and tr
28545 69 65 73 0a 2a 2a 20 61 67 61 69 6e 20 75 6e 74  ies.** again unt
28546 69 6c 20 61 20 74 69 6d 65 6f 75 74 20 76 61 6c  il a timeout val
28547 75 65 20 69 73 20 72 65 61 63 68 65 64 2e 20 20  ue is reached.  
28548 54 68 65 20 74 69 6d 65 6f 75 74 20 76 61 6c 75  The timeout valu
28549 65 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 74 65 67  e is.** an integ
2854a 65 72 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c  er number of mil
2854b 6c 69 73 65 63 6f 6e 64 73 20 70 61 73 73 65 64  liseconds passed
2854c 20 69 6e 20 61 73 20 74 68 65 20 66 69 72 73 74   in as the first
2854d 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f  .** argument..*/
2854e 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
2854f 74 65 44 65 66 61 75 6c 74 42 75 73 79 43 61 6c  teDefaultBusyCal
28550 6c 62 61 63 6b 28 0a 20 76 6f 69 64 20 2a 70 74  lback(. void *pt
28551 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
28552 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
28553 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 69 6e 74 20  nection */. int 
28554 63 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20 20  count           
28555 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
28556 66 20 74 69 6d 65 73 20 74 61 62 6c 65 20 68 61  f times table ha
28557 73 20 62 65 65 6e 20 62 75 73 79 20 2a 2f 0a 29  s been busy */.)
28558 7b 0a 23 69 66 20 4f 53 5f 57 49 4e 20 7c 7c 20  {.#if OS_WIN || 
28559 28 64 65 66 69 6e 65 64 28 48 41 56 45 5f 55 53  (defined(HAVE_US
2855a 4c 45 45 50 29 20 26 26 20 48 41 56 45 5f 55 53  LEEP) && HAVE_US
2855b 4c 45 45 50 29 0a 20 20 73 74 61 74 69 63 20 63  LEEP).  static c
2855c 6f 6e 73 74 20 75 38 20 64 65 6c 61 79 73 5b 5d  onst u8 delays[]
2855d 20 3d 0a 20 20 20 20 20 7b 20 31 2c 20 32 2c 20   =.     { 1, 2, 
2855e 35 2c 20 31 30 2c 20 31 35 2c 20 32 30 2c 20 32  5, 10, 15, 20, 2
2855f 35 2c 20 32 35 2c 20 20 32 35 2c 20 20 35 30 2c  5, 25,  25,  50,
28560 20 20 35 30 2c 20 31 30 30 20 7d 3b 0a 20 20 73    50, 100 };.  s
28561 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 74  tatic const u8 t
28562 6f 74 61 6c 73 5b 5d 20 3d 0a 20 20 20 20 20 7b  otals[] =.     {
28563 20 30 2c 20 31 2c 20 33 2c 20 20 38 2c 20 31 38   0, 1, 3,  8, 18
28564 2c 20 33 33 2c 20 35 33 2c 20 37 38 2c 20 31 30  , 33, 53, 78, 10
28565 33 2c 20 31 32 38 2c 20 31 37 38 2c 20 32 32 38  3, 128, 178, 228
28566 20 7d 3b 0a 23 20 64 65 66 69 6e 65 20 4e 44 45   };.# define NDE
28567 4c 41 59 20 28 73 69 7a 65 6f 66 28 64 65 6c 61  LAY (sizeof(dela
28568 79 73 29 2f 73 69 7a 65 6f 66 28 64 65 6c 61 79  ys)/sizeof(delay
28569 73 5b 30 5d 29 29 0a 20 20 73 71 6c 69 74 65 33  s[0])).  sqlite3
2856a 20 2a 64 62 20 3d 20 28 73 71 6c 69 74 65 33 20   *db = (sqlite3 
2856b 2a 29 70 74 72 3b 0a 20 20 69 6e 74 20 74 69 6d  *)ptr;.  int tim
2856c 65 6f 75 74 20 3d 20 64 62 2d 3e 62 75 73 79 54  eout = db->busyT
2856d 69 6d 65 6f 75 74 3b 0a 20 20 69 6e 74 20 64 65  imeout;.  int de
2856e 6c 61 79 2c 20 70 72 69 6f 72 3b 0a 0a 20 20 61  lay, prior;..  a
2856f 73 73 65 72 74 28 20 63 6f 75 6e 74 3e 3d 30 20  ssert( count>=0 
28570 29 3b 0a 20 20 69 66 28 20 63 6f 75 6e 74 20 3c  );.  if( count <
28571 20 4e 44 45 4c 41 59 20 29 7b 0a 20 20 20 20 64   NDELAY ){.    d
28572 65 6c 61 79 20 3d 20 64 65 6c 61 79 73 5b 63 6f  elay = delays[co
28573 75 6e 74 5d 3b 0a 20 20 20 20 70 72 69 6f 72 20  unt];.    prior 
28574 3d 20 74 6f 74 61 6c 73 5b 63 6f 75 6e 74 5d 3b  = totals[count];
28575 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 65  .  }else{.    de
28576 6c 61 79 20 3d 20 64 65 6c 61 79 73 5b 4e 44 45  lay = delays[NDE
28577 4c 41 59 2d 31 5d 3b 0a 20 20 20 20 70 72 69 6f  LAY-1];.    prio
28578 72 20 3d 20 74 6f 74 61 6c 73 5b 4e 44 45 4c 41  r = totals[NDELA
28579 59 2d 31 5d 20 2b 20 64 65 6c 61 79 2a 28 63 6f  Y-1] + delay*(co
2857a 75 6e 74 2d 28 4e 44 45 4c 41 59 2d 31 29 29 3b  unt-(NDELAY-1));
2857b 0a 20 20 7d 0a 20 20 69 66 28 20 70 72 69 6f 72  .  }.  if( prior
2857c 20 2b 20 64 65 6c 61 79 20 3e 20 74 69 6d 65 6f   + delay > timeo
2857d 75 74 20 29 7b 0a 20 20 20 20 64 65 6c 61 79 20  ut ){.    delay 
2857e 3d 20 74 69 6d 65 6f 75 74 20 2d 20 70 72 69 6f  = timeout - prio
2857f 72 3b 0a 20 20 20 20 69 66 28 20 64 65 6c 61 79  r;.    if( delay
28580 3c 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  <=0 ) return 0;.
28581 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 4f 73 53    }.  sqlite3OsS
28582 6c 65 65 70 28 64 62 2d 3e 70 56 66 73 2c 20 64  leep(db->pVfs, d
28583 65 6c 61 79 2a 31 30 30 30 29 3b 0a 20 20 72 65  elay*1000);.  re
28584 74 75 72 6e 20 31 3b 0a 23 65 6c 73 65 0a 20 20  turn 1;.#else.  
28585 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 28 73  sqlite3 *db = (s
28586 71 6c 69 74 65 33 20 2a 29 70 74 72 3b 0a 20 20  qlite3 *)ptr;.  
28587 69 6e 74 20 74 69 6d 65 6f 75 74 20 3d 20 28 28  int timeout = ((
28588 73 71 6c 69 74 65 33 20 2a 29 70 74 72 29 2d 3e  sqlite3 *)ptr)->
28589 62 75 73 79 54 69 6d 65 6f 75 74 3b 0a 20 20 69  busyTimeout;.  i
2858a 66 28 20 28 63 6f 75 6e 74 2b 31 29 2a 31 30 30  f( (count+1)*100
2858b 30 20 3e 20 74 69 6d 65 6f 75 74 20 29 7b 0a 20  0 > timeout ){. 
2858c 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
2858d 0a 20 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65  .  sqlite3OsSlee
2858e 70 28 64 62 2d 3e 70 56 66 73 2c 20 31 30 30 30  p(db->pVfs, 1000
2858f 30 30 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 31  000);.  return 1
28590 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  ;.#endif.}../*.*
28591 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 67 69 76  * Invoke the giv
28592 65 6e 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e  en busy handler.
28593 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
28594 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68  ine is called wh
28595 65 6e 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 20  en an operation 
28596 66 61 69 6c 65 64 20 77 69 74 68 20 61 20 6c 6f  failed with a lo
28597 63 6b 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20 72  ck..** If this r
28598 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 6e  outine returns n
28599 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 6c 6f 63  on-zero, the loc
2859a 6b 20 69 73 20 72 65 74 72 69 65 64 2e 20 20 49  k is retried.  I
2859b 66 20 69 74 0a 2a 2a 20 72 65 74 75 72 6e 73 20  f it.** returns 
2859c 30 2c 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  0, the operation
2859d 20 61 62 6f 72 74 73 20 77 69 74 68 20 61 6e 20   aborts with an 
2859e 53 51 4c 49 54 45 5f 42 55 53 59 20 65 72 72 6f  SQLITE_BUSY erro
2859f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
285a0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
285a1 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65  InvokeBusyHandle
285a2 72 28 42 75 73 79 48 61 6e 64 6c 65 72 20 2a 70  r(BusyHandler *p
285a3 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69  ){.  int rc;.  i
285a4 66 28 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 78 46  f( p==0 || p->xF
285a5 75 6e 63 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 42 75  unc==0 || p->nBu
285a6 73 79 3c 30 20 29 20 72 65 74 75 72 6e 20 30 3b  sy<0 ) return 0;
285a7 0a 20 20 72 63 20 3d 20 70 2d 3e 78 46 75 6e 63  .  rc = p->xFunc
285a8 28 70 2d 3e 70 41 72 67 2c 20 70 2d 3e 6e 42 75  (p->pArg, p->nBu
285a9 73 79 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 30  sy);.  if( rc==0
285aa 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 42 75 73 79   ){.    p->nBusy
285ab 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a   = -1;.  }else{.
285ac 20 20 20 20 70 2d 3e 6e 42 75 73 79 2b 2b 3b 0a      p->nBusy++;.
285ad 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
285ae 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20   .}../*.** This 
285af 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65  routine sets the
285b0 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 66   busy callback f
285b1 6f 72 20 61 6e 20 53 71 6c 69 74 65 20 64 61 74  or an Sqlite dat
285b2 61 62 61 73 65 20 74 6f 20 74 68 65 0a 2a 2a 20  abase to the.** 
285b3 67 69 76 65 6e 20 63 61 6c 6c 62 61 63 6b 20 66  given callback f
285b4 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65  unction with the
285b5 20 67 69 76 65 6e 20 61 72 67 75 6d 65 6e 74 2e   given argument.
285b6 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
285b7 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f  nt sqlite3_busy_
285b8 68 61 6e 64 6c 65 72 28 0a 20 20 73 71 6c 69 74  handler(.  sqlit
285b9 65 33 20 2a 64 62 2c 0a 20 20 69 6e 74 20 28 2a  e3 *db,.  int (*
285ba 78 42 75 73 79 29 28 76 6f 69 64 2a 2c 69 6e 74  xBusy)(void*,int
285bb 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 0a  ),.  void *pArg.
285bc 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ){.  sqlite3_mut
285bd 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
285be 65 78 29 3b 0a 20 20 64 62 2d 3e 62 75 73 79 48  ex);.  db->busyH
285bf 61 6e 64 6c 65 72 2e 78 46 75 6e 63 20 3d 20 78  andler.xFunc = x
285c0 42 75 73 79 3b 0a 20 20 64 62 2d 3e 62 75 73 79  Busy;.  db->busy
285c1 48 61 6e 64 6c 65 72 2e 70 41 72 67 20 3d 20 70  Handler.pArg = p
285c2 41 72 67 3b 0a 20 20 64 62 2d 3e 62 75 73 79 48  Arg;.  db->busyH
285c3 61 6e 64 6c 65 72 2e 6e 42 75 73 79 20 3d 20 30  andler.nBusy = 0
285c4 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
285c5 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
285c6 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  x);.  return SQL
285c7 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64  ITE_OK;.}..#ifnd
285c8 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  ef SQLITE_OMIT_P
285c9 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b  ROGRESS_CALLBACK
285ca 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
285cb 69 6e 65 20 73 65 74 73 20 74 68 65 20 70 72 6f  ine sets the pro
285cc 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 66  gress callback f
285cd 6f 72 20 61 6e 20 53 71 6c 69 74 65 20 64 61 74  or an Sqlite dat
285ce 61 62 61 73 65 20 74 6f 20 74 68 65 0a 2a 2a 20  abase to the.** 
285cf 67 69 76 65 6e 20 63 61 6c 6c 62 61 63 6b 20 66  given callback f
285d0 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65  unction with the
285d1 20 67 69 76 65 6e 20 61 72 67 75 6d 65 6e 74 2e   given argument.
285d2 20 54 68 65 20 70 72 6f 67 72 65 73 73 20 63 61   The progress ca
285d3 6c 6c 62 61 63 6b 20 77 69 6c 6c 0a 2a 2a 20 62  llback will.** b
285d4 65 20 69 6e 76 6f 6b 65 64 20 65 76 65 72 79 20  e invoked every 
285d5 6e 4f 70 73 20 6f 70 63 6f 64 65 73 2e 0a 2a 2f  nOps opcodes..*/
285d6 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
285d7 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73   sqlite3_progres
285d8 73 5f 68 61 6e 64 6c 65 72 28 0a 20 20 73 71 6c  s_handler(.  sql
285d9 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 69 6e 74  ite3 *db, .  int
285da 20 6e 4f 70 73 2c 0a 20 20 69 6e 74 20 28 2a 78   nOps,.  int (*x
285db 50 72 6f 67 72 65 73 73 29 28 76 6f 69 64 2a 29  Progress)(void*)
285dc 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 0a  , .  void *pArg.
285dd 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ){.  if( sqlite3
285de 53 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 64 62  SafetyCheckOk(db
285df 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
285e0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
285e1 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 69 66 28  >mutex);.    if(
285e2 20 6e 4f 70 73 3e 30 20 29 7b 0a 20 20 20 20 20   nOps>0 ){.     
285e3 20 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 20 3d   db->xProgress =
285e4 20 78 50 72 6f 67 72 65 73 73 3b 0a 20 20 20 20   xProgress;.    
285e5 20 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73 4f    db->nProgressO
285e6 70 73 20 3d 20 6e 4f 70 73 3b 0a 20 20 20 20 20  ps = nOps;.     
285e7 20 64 62 2d 3e 70 50 72 6f 67 72 65 73 73 41 72   db->pProgressAr
285e8 67 20 3d 20 70 41 72 67 3b 0a 20 20 20 20 7d 65  g = pArg;.    }e
285e9 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e 78  lse{.      db->x
285ea 50 72 6f 67 72 65 73 73 20 3d 20 30 3b 0a 20 20  Progress = 0;.  
285eb 20 20 20 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73      db->nProgres
285ec 73 4f 70 73 20 3d 20 30 3b 0a 20 20 20 20 20 20  sOps = 0;.      
285ed 64 62 2d 3e 70 50 72 6f 67 72 65 73 73 41 72 67  db->pProgressArg
285ee 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
285ef 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
285f0 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
285f1 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f    }.}.#endif.../
285f2 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
285f3 65 20 69 6e 73 74 61 6c 6c 73 20 61 20 64 65 66  e installs a def
285f4 61 75 6c 74 20 62 75 73 79 20 68 61 6e 64 6c 65  ault busy handle
285f5 72 20 74 68 61 74 20 77 61 69 74 73 20 66 6f 72  r that waits for
285f6 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 65   the.** specifie
285f7 64 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c  d number of mill
285f8 69 73 65 63 6f 6e 64 73 20 62 65 66 6f 72 65 20  iseconds before 
285f9 72 65 74 75 72 6e 69 6e 67 20 30 2e 0a 2a 2f 0a  returning 0..*/.
285fa 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
285fb 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65  qlite3_busy_time
285fc 6f 75 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  out(sqlite3 *db,
285fd 20 69 6e 74 20 6d 73 29 7b 0a 20 20 69 66 28 20   int ms){.  if( 
285fe 6d 73 3e 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e  ms>0 ){.    db->
285ff 62 75 73 79 54 69 6d 65 6f 75 74 20 3d 20 6d 73  busyTimeout = ms
28600 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62 75  ;.    sqlite3_bu
28601 73 79 5f 68 61 6e 64 6c 65 72 28 64 62 2c 20 73  sy_handler(db, s
28602 71 6c 69 74 65 44 65 66 61 75 6c 74 42 75 73 79  qliteDefaultBusy
28603 43 61 6c 6c 62 61 63 6b 2c 20 28 76 6f 69 64 2a  Callback, (void*
28604 29 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  )db);.  }else{. 
28605 20 20 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f     sqlite3_busy_
28606 68 61 6e 64 6c 65 72 28 64 62 2c 20 30 2c 20 30  handler(db, 0, 0
28607 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
28608 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
28609 0a 2a 2a 20 43 61 75 73 65 20 61 6e 79 20 70 65  .** Cause any pe
2860a 6e 64 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20  nding operation 
2860b 74 6f 20 73 74 6f 70 20 61 74 20 69 74 73 20 65  to stop at its e
2860c 61 72 6c 69 65 73 74 20 6f 70 70 6f 72 74 75 6e  arliest opportun
2860d 69 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ity..*/.SQLITE_A
2860e 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
2860f 69 6e 74 65 72 72 75 70 74 28 73 71 6c 69 74 65  interrupt(sqlite
28610 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 73 71  3 *db){.  if( sq
28611 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b  lite3SafetyCheck
28612 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20 20 64 62  Ok(db) ){.    db
28613 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74  ->u1.isInterrupt
28614 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 0a  ed = 1;.  }.}...
28615 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
28616 69 6f 6e 20 69 73 20 65 78 61 63 74 6c 79 20 74  ion is exactly t
28617 68 65 20 73 61 6d 65 20 61 73 20 73 71 6c 69 74  he same as sqlit
28618 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
28619 6f 6e 28 29 2c 20 65 78 63 65 70 74 0a 2a 2a 20  on(), except.** 
2861a 74 68 61 74 20 69 74 20 69 73 20 64 65 73 69 67  that it is desig
2861b 6e 65 64 20 74 6f 20 62 65 20 63 61 6c 6c 65 64  ned to be called
2861c 20 62 79 20 69 6e 74 65 72 6e 61 6c 20 63 6f 64   by internal cod
2861d 65 2e 20 54 68 65 20 64 69 66 66 65 72 65 6e 63  e. The differenc
2861e 65 20 69 73 0a 2a 2a 20 74 68 61 74 20 69 66 20  e is.** that if 
2861f 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73  a malloc() fails
28620 20 69 6e 20 73 71 6c 69 74 65 33 5f 63 72 65 61   in sqlite3_crea
28621 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 2c 20 61  te_function(), a
28622 6e 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20  n error code.** 
28623 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
28624 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  the mallocFailed
28625 20 66 6c 61 67 20 63 6c 65 61 72 65 64 2e 20 0a   flag cleared. .
28626 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
28627 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 72 65  E int sqlite3Cre
28628 61 74 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  ateFunc(.  sqlit
28629 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20  e3 *db,.  const 
2862a 63 68 61 72 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e  char *zFunctionN
2862b 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c  ame,.  int nArg,
2862c 0a 20 20 69 6e 74 20 65 6e 63 2c 0a 20 20 76 6f  .  int enc,.  vo
2862d 69 64 20 2a 70 55 73 65 72 44 61 74 61 2c 0a 20  id *pUserData,. 
2862e 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73   void (*xFunc)(s
2862f 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
28630 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
28631 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a  e **),.  void (*
28632 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63  xStep)(sqlite3_c
28633 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
28634 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20  te3_value **),. 
28635 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28   void (*xFinal)(
28636 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
28637 29 0a 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a  ).){.  FuncDef *
28638 70 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a  p;.  int nName;.
28639 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
2863a 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
2863b 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66  ->mutex) );.  if
2863c 28 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 3d  ( zFunctionName=
2863d 3d 30 20 7c 7c 0a 20 20 20 20 20 20 28 78 46 75  =0 ||.      (xFu
2863e 6e 63 20 26 26 20 28 78 46 69 6e 61 6c 20 7c 7c  nc && (xFinal ||
2863f 20 78 53 74 65 70 29 29 20 7c 7c 20 0a 20 20 20   xStep)) || .   
28640 20 20 20 28 21 78 46 75 6e 63 20 26 26 20 28 78     (!xFunc && (x
28641 46 69 6e 61 6c 20 26 26 20 21 78 53 74 65 70 29  Final && !xStep)
28642 29 20 7c 7c 0a 20 20 20 20 20 20 28 21 78 46 75  ) ||.      (!xFu
28643 6e 63 20 26 26 20 28 21 78 46 69 6e 61 6c 20 26  nc && (!xFinal &
28644 26 20 78 53 74 65 70 29 29 20 7c 7c 0a 20 20 20  & xStep)) ||.   
28645 20 20 20 28 6e 41 72 67 3c 2d 31 20 7c 7c 20 6e     (nArg<-1 || n
28646 41 72 67 3e 31 32 37 29 20 7c 7c 0a 20 20 20 20  Arg>127) ||.    
28647 20 20 28 32 35 35 3c 28 6e 4e 61 6d 65 20 3d 20    (255<(nName = 
28648 73 74 72 6c 65 6e 28 7a 46 75 6e 63 74 69 6f 6e  strlen(zFunction
28649 4e 61 6d 65 29 29 29 20 29 7b 0a 20 20 20 20 73  Name))) ){.    s
2864a 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
2864b 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 22 62  SQLITE_ERROR, "b
2864c 61 64 20 70 61 72 61 6d 65 74 65 72 73 22 29 3b  ad parameters");
2864d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
2864e 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
2864f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
28650 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 2f 2a 20  OMIT_UTF16.  /* 
28651 49 66 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20  If SQLITE_UTF16 
28652 69 73 20 73 70 65 63 69 66 69 65 64 20 61 73 20  is specified as 
28653 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 74 79 70  the encoding typ
28654 65 2c 20 74 72 61 6e 73 66 6f 72 6d 20 74 68 69  e, transform thi
28655 73 0a 20 20 2a 2a 20 74 6f 20 6f 6e 65 20 6f 66  s.  ** to one of
28656 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20   SQLITE_UTF16LE 
28657 6f 72 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42  or SQLITE_UTF16B
28658 45 20 75 73 69 6e 67 20 74 68 65 0a 20 20 2a 2a  E using the.  **
28659 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
2865a 49 56 45 20 6d 61 63 72 6f 2e 20 53 51 4c 49 54  IVE macro. SQLIT
2865b 45 5f 55 54 46 31 36 20 69 73 20 6e 6f 74 20 75  E_UTF16 is not u
2865c 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a  sed internally..
2865d 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 53 51 4c    **.  ** If SQL
2865e 49 54 45 5f 41 4e 59 20 69 73 20 73 70 65 63 69  ITE_ANY is speci
2865f 66 69 65 64 2c 20 61 64 64 20 74 68 72 65 65 20  fied, add three 
28660 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20  versions of the 
28661 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 74 6f  function.  ** to
28662 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e   the hash table.
28663 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 6e 63 3d  .  */.  if( enc=
28664 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 20 29 7b  =SQLITE_UTF16 ){
28665 0a 20 20 20 20 65 6e 63 20 3d 20 53 51 4c 49 54  .    enc = SQLIT
28666 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3b 0a 20  E_UTF16NATIVE;. 
28667 20 7d 65 6c 73 65 20 69 66 28 20 65 6e 63 3d 3d   }else if( enc==
28668 53 51 4c 49 54 45 5f 41 4e 59 20 29 7b 0a 20 20  SQLITE_ANY ){.  
28669 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 72 63    int rc;.    rc
2866a 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74 65   = sqlite3Create
2866b 46 75 6e 63 28 64 62 2c 20 7a 46 75 6e 63 74 69  Func(db, zFuncti
2866c 6f 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 53 51  onName, nArg, SQ
2866d 4c 49 54 45 5f 55 54 46 38 2c 0a 20 20 20 20 20  LITE_UTF8,.     
2866e 20 20 20 20 70 55 73 65 72 44 61 74 61 2c 20 78      pUserData, x
2866f 46 75 6e 63 2c 20 78 53 74 65 70 2c 20 78 46 69  Func, xStep, xFi
28670 6e 61 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63  nal);.    if( rc
28671 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
28672 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
28673 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20  3CreateFunc(db, 
28674 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e  zFunctionName, n
28675 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  Arg, SQLITE_UTF1
28676 36 4c 45 2c 0a 20 20 20 20 20 20 20 20 20 20 70  6LE,.          p
28677 55 73 65 72 44 61 74 61 2c 20 78 46 75 6e 63 2c  UserData, xFunc,
28678 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 3b   xStep, xFinal);
28679 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
2867a 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
2867b 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
2867c 0a 20 20 20 20 7d 0a 20 20 20 20 65 6e 63 20 3d  .    }.    enc =
2867d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3b   SQLITE_UTF16BE;
2867e 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 65 6e 63  .  }.#else.  enc
2867f 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a   = SQLITE_UTF8;.
28680 23 65 6e 64 69 66 0a 20 20 0a 20 20 2f 2a 20 43  #endif.  .  /* C
28681 68 65 63 6b 20 69 66 20 61 6e 20 65 78 69 73 74  heck if an exist
28682 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  ing function is 
28683 62 65 69 6e 67 20 6f 76 65 72 72 69 64 64 65 6e  being overridden
28684 20 6f 72 20 64 65 6c 65 74 65 64 2e 20 49 66 20   or deleted. If 
28685 73 6f 2c 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65  so,.  ** and the
28686 72 65 20 61 72 65 20 61 63 74 69 76 65 20 56 4d  re are active VM
28687 73 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 53  s, then return S
28688 51 4c 49 54 45 5f 42 55 53 59 2e 20 49 66 20 61  QLITE_BUSY. If a
28689 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 69   function.  ** i
2868a 73 20 62 65 69 6e 67 20 6f 76 65 72 72 69 64 64  s being overridd
2868b 65 6e 2f 64 65 6c 65 74 65 64 20 62 75 74 20 74  en/deleted but t
2868c 68 65 72 65 20 61 72 65 20 6e 6f 20 61 63 74 69  here are no acti
2868d 76 65 20 56 4d 73 2c 20 61 6c 6c 6f 77 20 74 68  ve VMs, allow th
2868e 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f 6e  e.  ** operation
2868f 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 62 75 74   to continue but
28690 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20   invalidate all 
28691 70 72 65 63 6f 6d 70 69 6c 65 64 20 73 74 61 74  precompiled stat
28692 65 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 70  ements..  */.  p
28693 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75   = sqlite3FindFu
28694 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 46 75 6e 63  nction(db, zFunc
28695 74 69 6f 6e 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c  tionName, nName,
28696 20 6e 41 72 67 2c 20 65 6e 63 2c 20 30 29 3b 0a   nArg, enc, 0);.
28697 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 69 50    if( p && p->iP
28698 72 65 66 45 6e 63 3d 3d 65 6e 63 20 26 26 20 70  refEnc==enc && p
28699 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 20 29 7b 0a  ->nArg==nArg ){.
2869a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 63 74 69      if( db->acti
2869b 76 65 56 64 62 65 43 6e 74 20 29 7b 0a 20 20 20  veVdbeCnt ){.   
2869c 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
2869d 64 62 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 2c  db, SQLITE_BUSY,
2869e 20 0a 20 20 20 20 20 20 20 20 22 55 6e 61 62 6c   .        "Unabl
2869f 65 20 74 6f 20 64 65 6c 65 74 65 2f 6d 6f 64 69  e to delete/modi
286a0 66 79 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e  fy user-function
286a1 20 64 75 65 20 74 6f 20 61 63 74 69 76 65 20 73   due to active s
286a2 74 61 74 65 6d 65 6e 74 73 22 29 3b 0a 20 20 20  tatements");.   
286a3 20 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e     assert( !db->
286a4 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
286a5 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
286a6 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65  ITE_BUSY;.    }e
286a7 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
286a8 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64  e3ExpirePrepared
286a9 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a  Statements(db);.
286aa 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 20 3d      }.  }..  p =
286ab 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63   sqlite3FindFunc
286ac 74 69 6f 6e 28 64 62 2c 20 7a 46 75 6e 63 74 69  tion(db, zFuncti
286ad 6f 6e 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e  onName, nName, n
286ae 41 72 67 2c 20 65 6e 63 2c 20 31 29 3b 0a 20 20  Arg, enc, 1);.  
286af 61 73 73 65 72 74 28 70 20 7c 7c 20 64 62 2d 3e  assert(p || db->
286b0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 3b 0a 20  mallocFailed);. 
286b1 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72   if( !p ){.    r
286b2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
286b3 45 4d 3b 0a 20 20 7d 0a 20 20 70 2d 3e 66 6c 61  EM;.  }.  p->fla
286b4 67 73 20 3d 20 30 3b 0a 20 20 70 2d 3e 78 46 75  gs = 0;.  p->xFu
286b5 6e 63 20 3d 20 78 46 75 6e 63 3b 0a 20 20 70 2d  nc = xFunc;.  p-
286b6 3e 78 53 74 65 70 20 3d 20 78 53 74 65 70 3b 0a  >xStep = xStep;.
286b7 20 20 70 2d 3e 78 46 69 6e 61 6c 69 7a 65 20 3d    p->xFinalize =
286b8 20 78 46 69 6e 61 6c 3b 0a 20 20 70 2d 3e 70 55   xFinal;.  p->pU
286b9 73 65 72 44 61 74 61 20 3d 20 70 55 73 65 72 44  serData = pUserD
286ba 61 74 61 3b 0a 20 20 70 2d 3e 6e 41 72 67 20 3d  ata;.  p->nArg =
286bb 20 6e 41 72 67 3b 0a 20 20 72 65 74 75 72 6e 20   nArg;.  return 
286bc 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
286bd 0a 2a 2a 20 43 72 65 61 74 65 20 6e 65 77 20 75  .** Create new u
286be 73 65 72 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a  ser functions..*
286bf 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
286c0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
286c1 66 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69  function(.  sqli
286c2 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74  te3 *db,.  const
286c3 20 63 68 61 72 20 2a 7a 46 75 6e 63 74 69 6f 6e   char *zFunction
286c4 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67  Name,.  int nArg
286c5 2c 0a 20 20 69 6e 74 20 65 6e 63 2c 0a 20 20 76  ,.  int enc,.  v
286c6 6f 69 64 20 2a 70 2c 0a 20 20 76 6f 69 64 20 28  oid *p,.  void (
286c7 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f  *xFunc)(sqlite3_
286c8 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
286c9 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a  ite3_value **),.
286ca 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28    void (*xStep)(
286cb 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
286cc 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
286cd 75 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28  ue **),.  void (
286ce 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33  *xFinal)(sqlite3
286cf 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 7b 0a 20 20  _context*).){.  
286d0 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
286d1 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
286d2 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 63 20 3d  ->mutex);.  rc =
286d3 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75   sqlite3CreateFu
286d4 6e 63 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e  nc(db, zFunction
286d5 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 65 6e 63 2c  Name, nArg, enc,
286d6 20 70 2c 20 78 46 75 6e 63 2c 20 78 53 74 65 70   p, xFunc, xStep
286d7 2c 20 78 46 69 6e 61 6c 29 3b 0a 20 20 72 63 20  , xFinal);.  rc 
286d8 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
286d9 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69  (db, rc);.  sqli
286da 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
286db 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
286dc 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e  turn rc;.}..#ifn
286dd 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
286de 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49  UTF16.SQLITE_API
286df 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65   int sqlite3_cre
286e0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 0a  ate_function16(.
286e1 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20    sqlite3 *db,. 
286e2 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46 75   const void *zFu
286e3 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e  nctionName,.  in
286e4 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 54  t nArg,.  int eT
286e5 65 78 74 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a  extRep,.  void *
286e6 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e  p,.  void (*xFun
286e7 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  c)(sqlite3_conte
286e8 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
286e9 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64  value**),.  void
286ea 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65   (*xStep)(sqlite
286eb 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
286ec 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c  qlite3_value**),
286ed 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c  .  void (*xFinal
286ee 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
286ef 74 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b  t*).){.  int rc;
286f0 0a 20 20 63 68 61 72 20 2a 7a 46 75 6e 63 38 3b  .  char *zFunc8;
286f1 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
286f2 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
286f3 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64 62  );.  assert( !db
286f4 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
286f5 3b 0a 20 20 7a 46 75 6e 63 38 20 3d 20 73 71 6c  ;.  zFunc8 = sql
286f6 69 74 65 33 55 74 66 31 36 74 6f 38 28 64 62 2c  ite3Utf16to8(db,
286f7 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20   zFunctionName, 
286f8 2d 31 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  -1);.  rc = sqli
286f9 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62  te3CreateFunc(db
286fa 2c 20 7a 46 75 6e 63 38 2c 20 6e 41 72 67 2c 20  , zFunc8, nArg, 
286fb 65 54 65 78 74 52 65 70 2c 20 70 2c 20 78 46 75  eTextRep, p, xFu
286fc 6e 63 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61  nc, xStep, xFina
286fd 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  l);.  sqlite3_fr
286fe 65 65 28 7a 46 75 6e 63 38 29 3b 0a 20 20 72 63  ee(zFunc8);.  rc
286ff 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
28700 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c  t(db, rc);.  sql
28701 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
28702 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
28703 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64  eturn rc;.}.#end
28704 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6c 61  if.../*.** Decla
28705 72 65 20 74 68 61 74 20 61 20 66 75 6e 63 74 69  re that a functi
28706 6f 6e 20 68 61 73 20 62 65 65 6e 20 6f 76 65 72  on has been over
28707 6c 6f 61 64 65 64 20 62 79 20 61 20 76 69 72 74  loaded by a virt
28708 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  ual table..**.**
28709 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   If the function
2870a 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20   already exists 
2870b 61 73 20 61 20 72 65 67 75 6c 61 72 20 67 6c 6f  as a regular glo
2870c 62 61 6c 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68  bal function, th
2870d 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69  en.** this routi
2870e 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20  ne is a no-op.  
2870f 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  If the function 
28710 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20  does not exist, 
28711 74 68 65 6e 20 63 72 65 61 74 65 0a 2a 2a 20 61  then create.** a
28712 20 6e 65 77 20 6f 6e 65 20 74 68 61 74 20 61 6c   new one that al
28713 77 61 79 73 20 74 68 72 6f 77 73 20 61 20 72 75  ways throws a ru
28714 6e 2d 74 69 6d 65 20 65 72 72 6f 72 2e 20 20 0a  n-time error.  .
28715 2a 2a 0a 2a 2a 20 57 68 65 6e 20 76 69 72 74 75  **.** When virtu
28716 61 6c 20 74 61 62 6c 65 73 20 69 6e 74 65 6e 64  al tables intend
28717 20 74 6f 20 70 72 6f 76 69 64 65 20 61 6e 20 6f   to provide an o
28718 76 65 72 6c 6f 61 64 65 64 20 66 75 6e 63 74 69  verloaded functi
28719 6f 6e 2c 20 74 68 65 79 0a 2a 2a 20 73 68 6f 75  on, they.** shou
2871a 6c 64 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75  ld call this rou
2871b 74 69 6e 65 20 74 6f 20 6d 61 6b 65 20 73 75 72  tine to make sur
2871c 65 20 74 68 65 20 67 6c 6f 62 61 6c 20 66 75 6e  e the global fun
2871d 63 74 69 6f 6e 20 65 78 69 73 74 73 2e 0a 2a 2a  ction exists..**
2871e 20 41 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69   A global functi
2871f 6f 6e 20 6d 75 73 74 20 65 78 69 73 74 20 69 6e  on must exist in
28720 20 6f 72 64 65 72 20 66 6f 72 20 6e 61 6d 65 20   order for name 
28721 72 65 73 6f 6c 75 74 69 6f 6e 20 74 6f 20 77 6f  resolution to wo
28722 72 6b 0a 2a 2a 20 70 72 6f 70 65 72 6c 79 2e 0a  rk.** properly..
28723 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
28724 74 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f  t sqlite3_overlo
28725 61 64 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 73  ad_function(.  s
28726 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f  qlite3 *db,.  co
28727 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
28728 0a 20 20 69 6e 74 20 6e 41 72 67 0a 29 7b 0a 20  .  int nArg.){. 
28729 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 74 72   int nName = str
2872a 6c 65 6e 28 7a 4e 61 6d 65 29 3b 0a 20 20 69 6e  len(zName);.  in
2872b 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f  t rc;.  sqlite3_
2872c 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
2872d 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 73 71  mutex);.  if( sq
2872e 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f  lite3FindFunctio
2872f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  n(db, zName, nNa
28730 6d 65 2c 20 6e 41 72 67 2c 20 53 51 4c 49 54 45  me, nArg, SQLITE
28731 5f 55 54 46 38 2c 20 30 29 3d 3d 30 20 29 7b 0a  _UTF8, 0)==0 ){.
28732 20 20 20 20 73 71 6c 69 74 65 33 43 72 65 61 74      sqlite3Creat
28733 65 46 75 6e 63 28 64 62 2c 20 7a 4e 61 6d 65 2c  eFunc(db, zName,
28734 20 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54   nArg, SQLITE_UT
28735 46 38 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  F8,.            
28736 20 20 20 20 20 20 20 20 20 20 30 2c 20 73 71 6c            0, sql
28737 69 74 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74  ite3InvalidFunct
28738 69 6f 6e 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a  ion, 0, 0);.  }.
28739 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70    rc = sqlite3Ap
2873a 69 45 78 69 74 28 64 62 2c 20 53 51 4c 49 54 45  iExit(db, SQLITE
2873b 5f 4f 4b 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  _OK);.  sqlite3_
2873c 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
2873d 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
2873e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20   rc;.}..#ifndef 
2873f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
28740 45 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  E./*.** Register
28741 20 61 20 74 72 61 63 65 20 66 75 6e 63 74 69 6f   a trace functio
28742 6e 2e 20 20 54 68 65 20 70 41 72 67 20 66 72 6f  n.  The pArg fro
28743 6d 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c 79  m the previously
28744 20 72 65 67 69 73 74 65 72 65 64 20 74 72 61 63   registered trac
28745 65 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64  e.** is returned
28746 2e 20 20 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c  .  .**.** A NULL
28747 20 74 72 61 63 65 20 66 75 6e 63 74 69 6f 6e 20   trace function 
28748 6d 65 61 6e 73 20 74 68 61 74 20 6e 6f 20 74 72  means that no tr
28749 61 63 69 6e 67 20 69 73 20 65 78 65 63 75 74 65  acing is execute
2874a 73 2e 20 20 41 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a  s.  A non-NULL.*
2874b 2a 20 74 72 61 63 65 20 69 73 20 61 20 70 6f 69  * trace is a poi
2874c 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69  nter to a functi
2874d 6f 6e 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b  on that is invok
2874e 65 64 20 61 74 20 74 68 65 20 73 74 61 72 74 20  ed at the start 
2874f 6f 66 20 65 61 63 68 0a 2a 2a 20 53 51 4c 20 73  of each.** SQL s
28750 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c  tatement..*/.SQL
28751 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71  ITE_API void *sq
28752 6c 69 74 65 33 5f 74 72 61 63 65 28 73 71 6c 69  lite3_trace(sqli
28753 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 28 2a  te3 *db, void (*
28754 78 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f  xTrace)(void*,co
28755 6e 73 74 20 63 68 61 72 2a 29 2c 20 76 6f 69 64  nst char*), void
28756 20 2a 70 41 72 67 29 7b 0a 20 20 76 6f 69 64 20   *pArg){.  void 
28757 2a 70 4f 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33  *pOld;.  sqlite3
28758 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
28759 3e 6d 75 74 65 78 29 3b 0a 20 20 70 4f 6c 64 20  >mutex);.  pOld 
2875a 3d 20 64 62 2d 3e 70 54 72 61 63 65 41 72 67 3b  = db->pTraceArg;
2875b 0a 20 20 64 62 2d 3e 78 54 72 61 63 65 20 3d 20  .  db->xTrace = 
2875c 78 54 72 61 63 65 3b 0a 20 20 64 62 2d 3e 70 54  xTrace;.  db->pT
2875d 72 61 63 65 41 72 67 20 3d 20 70 41 72 67 3b 0a  raceArg = pArg;.
2875e 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2875f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
28760 3b 0a 20 20 72 65 74 75 72 6e 20 70 4f 6c 64 3b  ;.  return pOld;
28761 0a 7d 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65  .}./*.** Registe
28762 72 20 61 20 70 72 6f 66 69 6c 65 20 66 75 6e 63  r a profile func
28763 74 69 6f 6e 2e 20 20 54 68 65 20 70 41 72 67 20  tion.  The pArg 
28764 66 72 6f 6d 20 74 68 65 20 70 72 65 76 69 6f 75  from the previou
28765 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 20 0a  sly registered .
28766 2a 2a 20 70 72 6f 66 69 6c 65 20 66 75 6e 63 74  ** profile funct
28767 69 6f 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ion is returned.
28768 20 20 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20    .**.** A NULL 
28769 70 72 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e  profile function
2876a 20 6d 65 61 6e 73 20 74 68 61 74 20 6e 6f 20 70   means that no p
2876b 72 6f 66 69 6c 69 6e 67 20 69 73 20 65 78 65 63  rofiling is exec
2876c 75 74 65 73 2e 20 20 41 20 6e 6f 6e 2d 4e 55 4c  utes.  A non-NUL
2876d 4c 0a 2a 2a 20 70 72 6f 66 69 6c 65 20 69 73 20  L.** profile is 
2876e 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66  a pointer to a f
2876f 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 69 73 20  unction that is 
28770 69 6e 76 6f 6b 65 64 20 61 74 20 74 68 65 20 63  invoked at the c
28771 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 0a 2a 2a 20  onclusion of.** 
28772 65 61 63 68 20 53 51 4c 20 73 74 61 74 65 6d 65  each SQL stateme
28773 6e 74 20 74 68 61 74 20 69 73 20 72 75 6e 2e 0a  nt that is run..
28774 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
28775 69 64 20 2a 73 71 6c 69 74 65 33 5f 70 72 6f 66  id *sqlite3_prof
28776 69 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ile(.  sqlite3 *
28777 64 62 2c 0a 20 20 76 6f 69 64 20 28 2a 78 50 72  db,.  void (*xPr
28778 6f 66 69 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e  ofile)(void*,con
28779 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 5f  st char*,sqlite_
2877a 75 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69 64 20  uint64),.  void 
2877b 2a 70 41 72 67 0a 29 7b 0a 20 20 76 6f 69 64 20  *pArg.){.  void 
2877c 2a 70 4f 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33  *pOld;.  sqlite3
2877d 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
2877e 3e 6d 75 74 65 78 29 3b 0a 20 20 70 4f 6c 64 20  >mutex);.  pOld 
2877f 3d 20 64 62 2d 3e 70 50 72 6f 66 69 6c 65 41 72  = db->pProfileAr
28780 67 3b 0a 20 20 64 62 2d 3e 78 50 72 6f 66 69 6c  g;.  db->xProfil
28781 65 20 3d 20 78 50 72 6f 66 69 6c 65 3b 0a 20 20  e = xProfile;.  
28782 64 62 2d 3e 70 50 72 6f 66 69 6c 65 41 72 67 20  db->pProfileArg 
28783 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 65  = pArg;.  sqlite
28784 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
28785 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
28786 72 6e 20 70 4f 6c 64 3b 0a 7d 0a 23 65 6e 64 69  rn pOld;.}.#endi
28787 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
28788 5f 54 52 41 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a 20  _TRACE */../*** 
28789 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 2a 2a 2a  EXPERIMENTAL ***
2878a 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  .**.** Register 
2878b 61 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65  a function to be
2878c 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 61 20   invoked when a 
2878d 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6d 6d  transaction comm
2878e 65 6e 74 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20  ents..** If the 
2878f 69 6e 76 6f 6b 65 64 20 66 75 6e 63 74 69 6f 6e  invoked function
28790 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72   returns non-zer
28791 6f 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6d 6d  o, then the comm
28792 69 74 20 62 65 63 6f 6d 65 73 20 61 0a 2a 2a 20  it becomes a.** 
28793 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 53 51 4c  rollback..*/.SQL
28794 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71  ITE_API void *sq
28795 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f  lite3_commit_hoo
28796 6b 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  k(.  sqlite3 *db
28797 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
28798 2a 20 41 74 74 61 63 68 20 74 68 65 20 68 6f 6f  * Attach the hoo
28799 6b 20 74 6f 20 74 68 69 73 20 64 61 74 61 62 61  k to this databa
2879a 73 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43  se */.  int (*xC
2879b 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 29 2c  allback)(void*),
2879c 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f    /* Function to
2879d 20 69 6e 76 6f 6b 65 20 6f 6e 20 65 61 63 68 20   invoke on each 
2879e 63 6f 6d 6d 69 74 20 2a 2f 0a 20 20 76 6f 69 64  commit */.  void
2879f 20 2a 70 41 72 67 20 20 20 20 20 20 20 20 20 20   *pArg          
287a0 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e        /* Argumen
287a1 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f  t to the functio
287a2 6e 20 2a 2f 0a 29 7b 0a 20 20 76 6f 69 64 20 2a  n */.){.  void *
287a3 70 4f 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f  pOld;.  sqlite3_
287a4 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
287a5 6d 75 74 65 78 29 3b 0a 20 20 70 4f 6c 64 20 3d  mutex);.  pOld =
287a6 20 64 62 2d 3e 70 43 6f 6d 6d 69 74 41 72 67 3b   db->pCommitArg;
287a7 0a 20 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61  .  db->xCommitCa
287a8 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61  llback = xCallba
287a9 63 6b 3b 0a 20 20 64 62 2d 3e 70 43 6f 6d 6d 69  ck;.  db->pCommi
287aa 74 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73  tArg = pArg;.  s
287ab 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
287ac 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
287ad 20 72 65 74 75 72 6e 20 70 4f 6c 64 3b 0a 7d 0a   return pOld;.}.
287ae 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
287af 61 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65  a callback to be
287b0 20 69 6e 76 6f 6b 65 64 20 65 61 63 68 20 74 69   invoked each ti
287b1 6d 65 20 61 20 72 6f 77 20 69 73 20 75 70 64 61  me a row is upda
287b2 74 65 64 2c 0a 2a 2a 20 69 6e 73 65 72 74 65 64  ted,.** inserted
287b3 20 6f 72 20 64 65 6c 65 74 65 64 20 75 73 69 6e   or deleted usin
287b4 67 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20  g this database 
287b5 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53  connection..*/.S
287b6 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
287b7 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68  sqlite3_update_h
287b8 6f 6f 6b 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ook(.  sqlite3 *
287b9 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
287ba 20 2f 2a 20 41 74 74 61 63 68 20 74 68 65 20 68   /* Attach the h
287bb 6f 6f 6b 20 74 6f 20 74 68 69 73 20 64 61 74 61  ook to this data
287bc 62 61 73 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28  base */.  void (
287bd 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64  *xCallback)(void
287be 2a 2c 69 6e 74 2c 63 68 61 72 20 63 6f 6e 73 74  *,int,char const
287bf 20 2a 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c   *,char const *,
287c0 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 2c 0a 20  sqlite_int64),. 
287c1 20 76 6f 69 64 20 2a 70 41 72 67 20 20 20 20 20   void *pArg     
287c2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
287c3 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 75  gument to the fu
287c4 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 76  nction */.){.  v
287c5 6f 69 64 20 2a 70 52 65 74 3b 0a 20 20 73 71 6c  oid *pRet;.  sql
287c6 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
287c7 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70  (db->mutex);.  p
287c8 52 65 74 20 3d 20 64 62 2d 3e 70 55 70 64 61 74  Ret = db->pUpdat
287c9 65 41 72 67 3b 0a 20 20 64 62 2d 3e 78 55 70 64  eArg;.  db->xUpd
287ca 61 74 65 43 61 6c 6c 62 61 63 6b 20 3d 20 78 43  ateCallback = xC
287cb 61 6c 6c 62 61 63 6b 3b 0a 20 20 64 62 2d 3e 70  allback;.  db->p
287cc 55 70 64 61 74 65 41 72 67 20 3d 20 70 41 72 67  UpdateArg = pArg
287cd 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
287ce 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
287cf 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65  x);.  return pRe
287d0 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69  t;.}../*.** Regi
287d1 73 74 65 72 20 61 20 63 61 6c 6c 62 61 63 6b 20  ster a callback 
287d2 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 65 61  to be invoked ea
287d3 63 68 20 74 69 6d 65 20 61 20 74 72 61 6e 73 61  ch time a transa
287d4 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 0a  ction is rolled.
287d5 2a 2a 20 62 61 63 6b 20 62 79 20 74 68 69 73 20  ** back by this 
287d6 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
287d7 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ion..*/.SQLITE_A
287d8 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  PI void *sqlite3
287d9 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 0a  _rollback_hook(.
287da 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
287db 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
287dc 74 74 61 63 68 20 74 68 65 20 68 6f 6f 6b 20 74  ttach the hook t
287dd 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20  o this database 
287de 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43 61 6c  */.  void (*xCal
287df 6c 62 61 63 6b 29 28 76 6f 69 64 2a 29 2c 20 2f  lback)(void*), /
287e0 2a 20 43 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  * Callback funct
287e1 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ion */.  void *p
287e2 41 72 67 20 20 20 20 20 20 20 20 20 20 20 20 20  Arg             
287e3 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74     /* Argument t
287e4 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a  o the function *
287e5 2f 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 52 65  /.){.  void *pRe
287e6 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  t;.  sqlite3_mut
287e7 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
287e8 65 78 29 3b 0a 20 20 70 52 65 74 20 3d 20 64 62  ex);.  pRet = db
287e9 2d 3e 70 52 6f 6c 6c 62 61 63 6b 41 72 67 3b 0a  ->pRollbackArg;.
287ea 20 20 64 62 2d 3e 78 52 6f 6c 6c 62 61 63 6b 43    db->xRollbackC
287eb 61 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62  allback = xCallb
287ec 61 63 6b 3b 0a 20 20 64 62 2d 3e 70 52 6f 6c 6c  ack;.  db->pRoll
287ed 62 61 63 6b 41 72 67 20 3d 20 70 41 72 67 3b 0a  backArg = pArg;.
287ee 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
287ef 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
287f0 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b  ;.  return pRet;
287f1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
287f2 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
287f3 20 74 6f 20 63 72 65 61 74 65 20 61 20 63 6f 6e   to create a con
287f4 6e 65 63 74 69 6f 6e 20 74 6f 20 61 20 64 61 74  nection to a dat
287f5 61 62 61 73 65 20 42 54 72 65 65 0a 2a 2a 20 64  abase BTree.** d
287f6 72 69 76 65 72 2e 20 20 49 66 20 7a 46 69 6c 65  river.  If zFile
287f7 6e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65  name is the name
287f8 20 6f 66 20 61 20 66 69 6c 65 2c 20 74 68 65 6e   of a file, then
287f9 20 74 68 61 74 20 66 69 6c 65 20 69 73 0a 2a 2a   that file is.**
287fa 20 6f 70 65 6e 65 64 20 61 6e 64 20 75 73 65 64   opened and used
287fb 2e 20 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20  .  If zFilename 
287fc 69 73 20 74 68 65 20 6d 61 67 69 63 20 6e 61 6d  is the magic nam
287fd 65 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 65  e ":memory:" the
287fe 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73  n.** the databas
287ff 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 6d  e is stored in m
28800 65 6d 6f 72 79 20 28 61 6e 64 20 69 73 20 74 68  emory (and is th
28801 75 73 20 66 6f 72 67 6f 74 74 65 6e 20 61 73 20  us forgotten as 
28802 73 6f 6f 6e 20 61 73 0a 2a 2a 20 74 68 65 20 63  soon as.** the c
28803 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f  onnection is clo
28804 73 65 64 2e 29 20 20 49 66 20 7a 46 69 6c 65 6e  sed.)  If zFilen
28805 61 6d 65 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e  ame is NULL then
28806 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a   the database.**
28807 20 69 73 20 61 20 22 76 69 72 74 75 61 6c 22 20   is a "virtual" 
28808 64 61 74 61 62 61 73 65 20 66 6f 72 20 74 72 61  database for tra
28809 6e 73 69 65 6e 74 20 75 73 65 20 6f 6e 6c 79 20  nsient use only 
2880a 61 6e 64 20 69 73 20 64 65 6c 65 74 65 64 20 61  and is deleted a
2880b 73 0a 2a 2a 20 73 6f 6f 6e 20 61 73 20 74 68 65  s.** soon as the
2880c 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63   connection is c
2880d 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 76  losed..**.** A v
2880e 69 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20  irtual database 
2880f 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 20  can be either a 
28810 64 69 73 6b 20 66 69 6c 65 20 28 74 68 61 74 20  disk file (that 
28811 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  is automatically
28812 0a 2a 2a 20 64 65 6c 65 74 65 64 20 77 68 65 6e  .** deleted when
28813 20 74 68 65 20 66 69 6c 65 20 69 73 20 63 6c 6f   the file is clo
28814 73 65 64 29 20 6f 72 20 69 74 20 61 6e 20 62 65  sed) or it an be
28815 20 68 65 6c 64 20 65 6e 74 69 72 65 6c 79 20 69   held entirely i
28816 6e 20 6d 65 6d 6f 72 79 2c 0a 2a 2a 20 64 65 70  n memory,.** dep
28817 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 76 61  ending on the va
28818 6c 75 65 73 20 6f 66 20 74 68 65 20 54 45 4d 50  lues of the TEMP
28819 5f 53 54 4f 52 45 20 63 6f 6d 70 69 6c 65 2d 74  _STORE compile-t
2881a 69 6d 65 20 6d 61 63 72 6f 20 61 6e 64 20 74 68  ime macro and th
2881b 65 0a 2a 2a 20 64 62 2d 3e 74 65 6d 70 5f 73 74  e.** db->temp_st
2881c 6f 72 65 20 76 61 72 69 61 62 6c 65 2c 20 61 63  ore variable, ac
2881d 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 66  cording to the f
2881e 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 3a 0a  ollowing chart:.
2881f 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 54 45 4d 50  **.**       TEMP
28820 5f 53 54 4f 52 45 20 20 20 20 20 64 62 2d 3e 74  _STORE     db->t
28821 65 6d 70 5f 73 74 6f 72 65 20 20 20 20 20 4c 6f  emp_store     Lo
28822 63 61 74 69 6f 6e 20 6f 66 20 74 65 6d 70 6f 72  cation of tempor
28823 61 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  ary database.** 
28824 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d        ----------
28825 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d       -----------
28826 2d 2d 2d 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d  ---     --------
28827 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
28828 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 20  ------.**       
28829 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20      0           
2882a 20 20 20 20 61 6e 79 20 20 20 20 20 20 20 20 20      any         
2882b 20 20 20 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20      file.**     
2882c 20 20 20 20 20 20 31 20 20 20 20 20 20 20 20 20        1         
2882d 20 20 20 20 20 20 20 31 20 20 20 20 20 20 20 20         1        
2882e 20 20 20 20 20 20 66 69 6c 65 0a 2a 2a 20 20 20        file.**   
2882f 20 20 20 20 20 20 20 20 31 20 20 20 20 20 20 20          1       
28830 20 20 20 20 20 20 20 20 20 32 20 20 20 20 20 20           2      
28831 20 20 20 20 20 20 20 20 6d 65 6d 6f 72 79 0a 2a          memory.*
28832 2a 20 20 20 20 20 20 20 20 20 20 20 31 20 20 20  *           1   
28833 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20               0  
28834 20 20 20 20 20 20 20 20 20 20 20 20 66 69 6c 65              file
28835 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 32 20  .**           2 
28836 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
28837 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69                fi
28838 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  le.**           
28839 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2               
2883a 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20   2              
2883b 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20 20  memory.**       
2883c 20 20 20 20 32 20 20 20 20 20 20 20 20 20 20 20      2           
2883d 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20       0          
2883e 20 20 20 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20      memory.**   
2883f 20 20 20 20 20 20 20 20 33 20 20 20 20 20 20 20          3       
28840 20 20 20 20 20 20 20 20 61 6e 79 20 20 20 20 20          any     
28841 20 20 20 20 20 20 20 20 6d 65 6d 6f 72 79 0a 2a          memory.*
28842 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
28843 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
28844 65 46 61 63 74 6f 72 79 28 0a 20 20 63 6f 6e 73  eFactory(.  cons
28845 74 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20  t sqlite3 *db,  
28846 20 20 20 20 20 20 2f 2a 20 4d 61 69 6e 20 64 61        /* Main da
28847 74 61 62 61 73 65 20 77 68 65 6e 20 6f 70 65 6e  tabase when open
28848 69 6e 67 20 61 75 78 20 6f 74 68 65 72 77 69 73  ing aux otherwis
28849 65 20 30 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  e 0 */.  const c
2884a 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20  har *zFilename, 
2884b 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
2884c 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e  e file containin
2884d 67 20 74 68 65 20 42 54 72 65 65 20 64 61 74 61  g the BTree data
2884e 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6d  base */.  int om
2884f 69 74 4a 6f 75 72 6e 61 6c 2c 20 20 20 20 20 20  itJournal,      
28850 20 20 20 20 2f 2a 20 69 66 20 54 52 55 45 20 74      /* if TRUE t
28851 68 65 6e 20 64 6f 20 6e 6f 74 20 6a 6f 75 72 6e  hen do not journ
28852 61 6c 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a  al this file */.
28853 20 20 69 6e 74 20 6e 43 61 63 68 65 2c 20 20 20    int nCache,   
28854 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48              /* H
28855 6f 77 20 6d 61 6e 79 20 70 61 67 65 73 20 69 6e  ow many pages in
28856 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 20   the page cache 
28857 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67  */.  int vfsFlag
28858 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  s,             /
28859 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74  * Flags passed t
2885a 68 72 6f 75 67 68 20 74 6f 20 76 66 73 4f 70 65  hrough to vfsOpe
2885b 6e 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70  n */.  Btree **p
2885c 70 42 74 72 65 65 20 20 20 20 20 20 20 20 20 20  pBtree          
2885d 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e   /* Pointer to n
2885e 65 77 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20  ew Btree object 
2885f 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a  written here */.
28860 29 7b 0a 20 20 69 6e 74 20 62 74 46 6c 61 67 73  ){.  int btFlags
28861 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a   = 0;.  int rc;.
28862 20 20 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c    .  assert( sql
28863 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
28864 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
28865 61 73 73 65 72 74 28 20 70 70 42 74 72 65 65 20  assert( ppBtree 
28866 21 3d 20 30 29 3b 0a 20 20 69 66 28 20 6f 6d 69  != 0);.  if( omi
28867 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20  tJournal ){.    
28868 62 74 46 6c 61 67 73 20 7c 3d 20 42 54 52 45 45  btFlags |= BTREE
28869 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 3b 0a 20  _OMIT_JOURNAL;. 
2886a 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61   }.  if( db->fla
2886b 67 73 20 26 20 53 51 4c 49 54 45 5f 4e 6f 52 65  gs & SQLITE_NoRe
2886c 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 62 74  adlock ){.    bt
2886d 46 6c 61 67 73 20 7c 3d 20 42 54 52 45 45 5f 4e  Flags |= BTREE_N
2886e 4f 5f 52 45 41 44 4c 4f 43 4b 3b 0a 20 20 7d 0a  O_READLOCK;.  }.
2886f 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d    if( zFilename=
28870 3d 30 20 29 7b 0a 23 69 66 20 54 45 4d 50 5f 53  =0 ){.#if TEMP_S
28871 54 4f 52 45 3d 3d 30 0a 20 20 20 20 2f 2a 20 44  TORE==0.    /* D
28872 6f 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 23 65 6e  o nothing */.#en
28873 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  dif.#ifndef SQLI
28874 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42  TE_OMIT_MEMORYDB
28875 0a 23 69 66 20 54 45 4d 50 5f 53 54 4f 52 45 3d  .#if TEMP_STORE=
28876 3d 31 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 74  =1.    if( db->t
28877 65 6d 70 5f 73 74 6f 72 65 3d 3d 32 20 29 20 7a  emp_store==2 ) z
28878 46 69 6c 65 6e 61 6d 65 20 3d 20 22 3a 6d 65 6d  Filename = ":mem
28879 6f 72 79 3a 22 3b 0a 23 65 6e 64 69 66 0a 23 69  ory:";.#endif.#i
2887a 66 20 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 32 0a  f TEMP_STORE==2.
2887b 20 20 20 20 69 66 28 20 64 62 2d 3e 74 65 6d 70      if( db->temp
2887c 5f 73 74 6f 72 65 21 3d 31 20 29 20 7a 46 69 6c  _store!=1 ) zFil
2887d 65 6e 61 6d 65 20 3d 20 22 3a 6d 65 6d 6f 72 79  ename = ":memory
2887e 3a 22 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 54  :";.#endif.#if T
2887f 45 4d 50 5f 53 54 4f 52 45 3d 3d 33 0a 20 20 20  EMP_STORE==3.   
28880 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 22 3a 6d   zFilename = ":m
28881 65 6d 6f 72 79 3a 22 3b 0a 23 65 6e 64 69 66 0a  emory:";.#endif.
28882 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
28883 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 2a  _OMIT_MEMORYDB *
28884 2f 0a 20 20 7d 0a 0a 20 20 69 66 28 20 28 76 66  /.  }..  if( (vf
28885 73 46 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  sFlags & SQLITE_
28886 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 21 3d 30  OPEN_MAIN_DB)!=0
28887 20 26 26 20 28 7a 46 69 6c 65 6e 61 6d 65 3d 3d   && (zFilename==
28888 30 20 7c 7c 20 2a 7a 46 69 6c 65 6e 61 6d 65 3d  0 || *zFilename=
28889 3d 30 29 20 29 7b 0a 20 20 20 20 76 66 73 46 6c  =0) ){.    vfsFl
2888a 61 67 73 20 3d 20 28 76 66 73 46 6c 61 67 73 20  ags = (vfsFlags 
2888b 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  & ~SQLITE_OPEN_M
2888c 41 49 4e 5f 44 42 29 20 7c 20 53 51 4c 49 54 45  AIN_DB) | SQLITE
2888d 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 3b 0a 20  _OPEN_TEMP_DB;. 
2888e 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65   }.  rc = sqlite
2888f 33 42 74 72 65 65 4f 70 65 6e 28 7a 46 69 6c 65  3BtreeOpen(zFile
28890 6e 61 6d 65 2c 20 28 73 71 6c 69 74 65 33 20 2a  name, (sqlite3 *
28891 29 64 62 2c 20 70 70 42 74 72 65 65 2c 20 62 74  )db, ppBtree, bt
28892 46 6c 61 67 73 2c 20 76 66 73 46 6c 61 67 73 29  Flags, vfsFlags)
28893 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
28894 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c  TE_OK ){.    sql
28895 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68  ite3BtreeSetCach
28896 65 53 69 7a 65 28 2a 70 70 42 74 72 65 65 2c 20  eSize(*ppBtree, 
28897 6e 43 61 63 68 65 29 3b 0a 20 20 7d 0a 20 20 72  nCache);.  }.  r
28898 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
28899 2a 2a 20 52 65 74 75 72 6e 20 55 54 46 2d 38 20  ** Return UTF-8 
2889a 65 6e 63 6f 64 65 64 20 45 6e 67 6c 69 73 68 20  encoded English 
2889b 6c 61 6e 67 75 61 67 65 20 65 78 70 6c 61 6e 61  language explana
2889c 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 73 74  tion of the most
2889d 20 72 65 63 65 6e 74 0a 2a 2a 20 65 72 72 6f 72   recent.** error
2889e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2889f 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
288a0 74 65 33 5f 65 72 72 6d 73 67 28 73 71 6c 69 74  te3_errmsg(sqlit
288a1 65 33 20 2a 64 62 29 7b 0a 20 20 63 6f 6e 73 74  e3 *db){.  const
288a2 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 66 28 20   char *z;.  if( 
288a3 21 64 62 20 29 7b 0a 20 20 20 20 72 65 74 75 72  !db ){.    retur
288a4 6e 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28  n sqlite3ErrStr(
288a5 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20  SQLITE_NOMEM);. 
288a6 20 7d 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65   }.  if( !sqlite
288a7 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b  3SafetyCheckSick
288a8 4f 72 4f 6b 28 64 62 29 20 7c 7c 20 64 62 2d 3e  OrOk(db) || db->
288a9 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f  errCode==SQLITE_
288aa 4d 49 53 55 53 45 20 29 7b 0a 20 20 20 20 72 65  MISUSE ){.    re
288ab 74 75 72 6e 20 73 71 6c 69 74 65 33 45 72 72 53  turn sqlite3ErrS
288ac 74 72 28 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  tr(SQLITE_MISUSE
288ad 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
288ae 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
288af 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72  >mutex);.  asser
288b0 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  t( !db->mallocFa
288b1 69 6c 65 64 20 29 3b 0a 20 20 7a 20 3d 20 28 63  iled );.  z = (c
288b2 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  har*)sqlite3_val
288b3 75 65 5f 74 65 78 74 28 64 62 2d 3e 70 45 72 72  ue_text(db->pErr
288b4 29 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b  );.  if( z==0 ){
288b5 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33  .    z = sqlite3
288b6 45 72 72 53 74 72 28 64 62 2d 3e 65 72 72 43 6f  ErrStr(db->errCo
288b7 64 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  de);.  }.  sqlit
288b8 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
288b9 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
288ba 75 72 6e 20 7a 3b 0a 7d 0a 0a 23 69 66 6e 64 65  urn z;.}..#ifnde
288bb 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
288bc 46 31 36 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  F16./*.** Return
288bd 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20   UTF-16 encoded 
288be 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65  English language
288bf 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20 6f 66 20   explanation of 
288c0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 0a  the most recent.
288c1 2a 2a 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c  ** error..*/.SQL
288c2 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
288c3 69 64 20 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d  id *sqlite3_errm
288c4 73 67 31 36 28 73 71 6c 69 74 65 33 20 2a 64 62  sg16(sqlite3 *db
288c5 29 7b 0a 20 20 2f 2a 20 42 65 63 61 75 73 65 20  ){.  /* Because 
288c6 61 6c 6c 20 74 68 65 20 63 68 61 72 61 63 74 65  all the characte
288c7 72 73 20 69 6e 20 74 68 65 20 73 74 72 69 6e 67  rs in the string
288c8 20 61 72 65 20 69 6e 20 74 68 65 20 75 6e 69 63   are in the unic
288c9 6f 64 65 0a 20 20 2a 2a 20 72 61 6e 67 65 20 30  ode.  ** range 0
288ca 78 30 30 2d 30 78 46 46 2c 20 69 66 20 77 65 20  x00-0xFF, if we 
288cb 70 61 64 20 74 68 65 20 62 69 67 2d 65 6e 64 69  pad the big-endi
288cc 61 6e 20 73 74 72 69 6e 67 20 77 69 74 68 20 61  an string with a
288cd 20 0a 20 20 2a 2a 20 7a 65 72 6f 20 62 79 74 65   .  ** zero byte
288ce 2c 20 77 65 20 63 61 6e 20 6f 62 74 61 69 6e 20  , we can obtain 
288cf 74 68 65 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61  the little-endia
288d0 6e 20 73 74 72 69 6e 67 20 77 69 74 68 0a 20 20  n string with.  
288d1 2a 2a 20 26 62 69 67 5f 65 6e 64 69 61 6e 5b 31  ** &big_endian[1
288d2 5d 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63  ]..  */.  static
288d3 20 63 6f 6e 73 74 20 63 68 61 72 20 6f 75 74 4f   const char outO
288d4 66 4d 65 6d 42 65 5b 5d 20 3d 20 7b 0a 20 20 20  fMemBe[] = {.   
288d5 20 30 2c 20 27 6f 27 2c 20 30 2c 20 27 75 27 2c   0, 'o', 0, 'u',
288d6 20 30 2c 20 27 74 27 2c 20 30 2c 20 27 20 27 2c   0, 't', 0, ' ',
288d7 20 0a 20 20 20 20 30 2c 20 27 6f 27 2c 20 30 2c   .    0, 'o', 0,
288d8 20 27 66 27 2c 20 30 2c 20 27 20 27 2c 20 0a 20   'f', 0, ' ', . 
288d9 20 20 20 30 2c 20 27 6d 27 2c 20 30 2c 20 27 65     0, 'm', 0, 'e
288da 27 2c 20 30 2c 20 27 6d 27 2c 20 30 2c 20 27 6f  ', 0, 'm', 0, 'o
288db 27 2c 20 30 2c 20 27 72 27 2c 20 30 2c 20 27 79  ', 0, 'r', 0, 'y
288dc 27 2c 20 30 2c 20 30 2c 20 30 0a 20 20 7d 3b 0a  ', 0, 0, 0.  };.
288dd 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
288de 68 61 72 20 6d 69 73 75 73 65 42 65 20 5b 5d 20  har misuseBe [] 
288df 3d 20 7b 0a 20 20 20 20 30 2c 20 27 6c 27 2c 20  = {.    0, 'l', 
288e0 30 2c 20 27 69 27 2c 20 30 2c 20 27 62 27 2c 20  0, 'i', 0, 'b', 
288e1 30 2c 20 27 72 27 2c 20 30 2c 20 27 61 27 2c 20  0, 'r', 0, 'a', 
288e2 30 2c 20 27 72 27 2c 20 30 2c 20 27 79 27 2c 20  0, 'r', 0, 'y', 
288e3 30 2c 20 27 20 27 2c 20 0a 20 20 20 20 30 2c 20  0, ' ', .    0, 
288e4 27 72 27 2c 20 30 2c 20 27 6f 27 2c 20 30 2c 20  'r', 0, 'o', 0, 
288e5 27 75 27 2c 20 30 2c 20 27 74 27 2c 20 30 2c 20  'u', 0, 't', 0, 
288e6 27 69 27 2c 20 30 2c 20 27 6e 27 2c 20 30 2c 20  'i', 0, 'n', 0, 
288e7 27 65 27 2c 20 30 2c 20 27 20 27 2c 20 0a 20 20  'e', 0, ' ', .  
288e8 20 20 30 2c 20 27 63 27 2c 20 30 2c 20 27 61 27    0, 'c', 0, 'a'
288e9 2c 20 30 2c 20 27 6c 27 2c 20 30 2c 20 27 6c 27  , 0, 'l', 0, 'l'
288ea 2c 20 30 2c 20 27 65 27 2c 20 30 2c 20 27 64 27  , 0, 'e', 0, 'd'
288eb 2c 20 30 2c 20 27 20 27 2c 20 0a 20 20 20 20 30  , 0, ' ', .    0
288ec 2c 20 27 6f 27 2c 20 30 2c 20 27 75 27 2c 20 30  , 'o', 0, 'u', 0
288ed 2c 20 27 74 27 2c 20 30 2c 20 27 20 27 2c 20 0a  , 't', 0, ' ', .
288ee 20 20 20 20 30 2c 20 27 6f 27 2c 20 30 2c 20 27      0, 'o', 0, '
288ef 66 27 2c 20 30 2c 20 27 20 27 2c 20 0a 20 20 20  f', 0, ' ', .   
288f0 20 30 2c 20 27 73 27 2c 20 30 2c 20 27 65 27 2c   0, 's', 0, 'e',
288f1 20 30 2c 20 27 71 27 2c 20 30 2c 20 27 75 27 2c   0, 'q', 0, 'u',
288f2 20 30 2c 20 27 65 27 2c 20 30 2c 20 27 6e 27 2c   0, 'e', 0, 'n',
288f3 20 30 2c 20 27 63 27 2c 20 30 2c 20 27 65 27 2c   0, 'c', 0, 'e',
288f4 20 30 2c 20 30 2c 20 30 0a 20 20 7d 3b 0a 0a 20   0, 0, 0.  };.. 
288f5 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 3b 0a   const void *z;.
288f6 20 20 69 66 28 20 21 64 62 20 29 7b 0a 20 20 20    if( !db ){.   
288f7 20 72 65 74 75 72 6e 20 28 76 6f 69 64 20 2a 29   return (void *)
288f8 28 26 6f 75 74 4f 66 4d 65 6d 42 65 5b 53 51 4c  (&outOfMemBe[SQL
288f9 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3d  ITE_UTF16NATIVE=
288fa 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 3f  =SQLITE_UTF16LE?
288fb 31 3a 30 5d 29 3b 0a 20 20 7d 0a 20 20 69 66 28  1:0]);.  }.  if(
288fc 20 21 73 71 6c 69 74 65 33 53 61 66 65 74 79 43   !sqlite3SafetyC
288fd 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62 29  heckSickOrOk(db)
288fe 20 7c 7c 20 64 62 2d 3e 65 72 72 43 6f 64 65 3d   || db->errCode=
288ff 3d 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 29  =SQLITE_MISUSE )
28900 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 76 6f  {.    return (vo
28901 69 64 20 2a 29 28 26 6d 69 73 75 73 65 42 65 5b  id *)(&misuseBe[
28902 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49  SQLITE_UTF16NATI
28903 56 45 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  VE==SQLITE_UTF16
28904 4c 45 3f 31 3a 30 5d 29 3b 0a 20 20 7d 0a 20 20  LE?1:0]);.  }.  
28905 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
28906 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
28907 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d    assert( !db->m
28908 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
28909 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c   z = sqlite3_val
2890a 75 65 5f 74 65 78 74 31 36 28 64 62 2d 3e 70 45  ue_text16(db->pE
2890b 72 72 29 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20  rr);.  if( z==0 
2890c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61  ){.    sqlite3Va
2890d 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45  lueSetStr(db->pE
2890e 72 72 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 45  rr, -1, sqlite3E
2890f 72 72 53 74 72 28 64 62 2d 3e 65 72 72 43 6f 64  rrStr(db->errCod
28910 65 29 2c 0a 20 20 20 20 20 20 20 20 20 53 51 4c  e),.         SQL
28911 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45  ITE_UTF8, SQLITE
28912 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7a 20  _STATIC);.    z 
28913 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
28914 74 65 78 74 31 36 28 64 62 2d 3e 70 45 72 72 29  text16(db->pErr)
28915 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 41  ;.  }.  sqlite3A
28916 70 69 45 78 69 74 28 30 2c 20 30 29 3b 0a 20 20  piExit(0, 0);.  
28917 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
28918 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
28919 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 23 65    return z;.}.#e
2891a 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2891b 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a  MIT_UTF16 */../*
2891c 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6d  .** Return the m
2891d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72  ost recent error
2891e 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20   code generated 
2891f 62 79 20 61 6e 20 53 51 4c 69 74 65 20 72 6f 75  by an SQLite rou
28920 74 69 6e 65 2e 20 49 66 20 4e 55 4c 4c 20 69 73  tine. If NULL is
28921 0a 2a 2a 20 70 61 73 73 65 64 20 74 6f 20 74 68  .** passed to th
28922 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 77 65 20  is function, we 
28923 61 73 73 75 6d 65 20 61 20 6d 61 6c 6c 6f 63 28  assume a malloc(
28924 29 20 66 61 69 6c 65 64 20 64 75 72 69 6e 67 20  ) failed during 
28925 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2e 0a  sqlite3_open()..
28926 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
28927 74 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64  t sqlite3_errcod
28928 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  e(sqlite3 *db){.
28929 20 20 69 66 28 20 64 62 20 26 26 20 21 73 71 6c    if( db && !sql
2892a 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 53  ite3SafetyCheckS
2892b 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20  ickOrOk(db) ){. 
2892c 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2892d 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69  _MISUSE;.  }.  i
2892e 66 28 20 21 64 62 20 7c 7c 20 64 62 2d 3e 6d 61  f( !db || db->ma
2892f 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
28930 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
28931 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 74  NOMEM;.  }.  ret
28932 75 72 6e 20 64 62 2d 3e 65 72 72 43 6f 64 65 20  urn db->errCode 
28933 26 20 64 62 2d 3e 65 72 72 4d 61 73 6b 3b 0a 7d  & db->errMask;.}
28934 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
28935 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6e 67 20 66   new collating f
28936 75 6e 63 74 69 6f 6e 20 66 6f 72 20 64 61 74 61  unction for data
28937 62 61 73 65 20 22 64 62 22 2e 20 20 54 68 65 20  base "db".  The 
28938 6e 61 6d 65 20 69 73 20 7a 4e 61 6d 65 0a 2a 2a  name is zName.**
28939 20 61 6e 64 20 74 68 65 20 65 6e 63 6f 64 69 6e   and the encodin
2893a 67 20 69 73 20 65 6e 63 2e 0a 2a 2f 0a 73 74 61  g is enc..*/.sta
2893b 74 69 63 20 69 6e 74 20 63 72 65 61 74 65 43 6f  tic int createCo
2893c 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74  llation(.  sqlit
2893d 65 33 2a 20 64 62 2c 20 0a 20 20 63 6f 6e 73 74  e3* db, .  const
2893e 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20   char *zName, . 
2893f 20 69 6e 74 20 65 6e 63 2c 20 0a 20 20 76 6f 69   int enc, .  voi
28940 64 2a 20 70 43 74 78 2c 0a 20 20 69 6e 74 28 2a  d* pCtx,.  int(*
28941 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c  xCompare)(void*,
28942 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
28943 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29  int,const void*)
28944 2c 0a 20 20 76 6f 69 64 28 2a 78 44 65 6c 29 28  ,.  void(*xDel)(
28945 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 43 6f 6c 6c  void*).){.  Coll
28946 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 6e  Seq *pColl;.  in
28947 74 20 65 6e 63 32 3b 0a 20 20 0a 20 20 61 73 73  t enc2;.  .  ass
28948 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
28949 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
2894a 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 53  x) );..  /* If S
2894b 51 4c 49 54 45 5f 55 54 46 31 36 20 69 73 20 73  QLITE_UTF16 is s
2894c 70 65 63 69 66 69 65 64 20 61 73 20 74 68 65 20  pecified as the 
2894d 65 6e 63 6f 64 69 6e 67 20 74 79 70 65 2c 20 74  encoding type, t
2894e 72 61 6e 73 66 6f 72 6d 20 74 68 69 73 0a 20 20  ransform this.  
2894f 2a 2a 20 74 6f 20 6f 6e 65 20 6f 66 20 53 51 4c  ** to one of SQL
28950 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 20 53  ITE_UTF16LE or S
28951 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 75 73  QLITE_UTF16BE us
28952 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 53 51 4c  ing the.  ** SQL
28953 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20  ITE_UTF16NATIVE 
28954 6d 61 63 72 6f 2e 20 53 51 4c 49 54 45 5f 55 54  macro. SQLITE_UT
28955 46 31 36 20 69 73 20 6e 6f 74 20 75 73 65 64 20  F16 is not used 
28956 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 2a 2f  internally..  */
28957 0a 20 20 65 6e 63 32 20 3d 20 65 6e 63 20 26 20  .  enc2 = enc & 
28958 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c  ~SQLITE_UTF16_AL
28959 49 47 4e 45 44 3b 0a 20 20 69 66 28 20 65 6e 63  IGNED;.  if( enc
2895a 32 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 20  2==SQLITE_UTF16 
2895b 29 7b 0a 20 20 20 20 65 6e 63 32 20 3d 20 53 51  ){.    enc2 = SQ
2895c 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
2895d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 28 65 6e  ;.  }..  if( (en
2895e 63 32 26 7e 33 29 21 3d 30 20 29 7b 0a 20 20 20  c2&~3)!=0 ){.   
2895f 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
28960 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20  , SQLITE_ERROR, 
28961 22 75 6e 6b 6e 6f 77 6e 20 65 6e 63 6f 64 69 6e  "unknown encodin
28962 67 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  g");.    return 
28963 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
28964 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66  }..  /* Check if
28965 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 72 65   this call is re
28966 6d 6f 76 69 6e 67 20 6f 72 20 72 65 70 6c 61 63  moving or replac
28967 69 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67 20  ing an existing 
28968 63 6f 6c 6c 61 74 69 6f 6e 20 0a 20 20 2a 2a 20  collation .  ** 
28969 73 65 71 75 65 6e 63 65 2e 20 49 66 20 73 6f 2c  sequence. If so,
2896a 20 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 61   and there are a
2896b 63 74 69 76 65 20 56 4d 73 2c 20 72 65 74 75 72  ctive VMs, retur
2896c 6e 20 62 75 73 79 2e 20 49 66 20 74 68 65 72 65  n busy. If there
2896d 0a 20 20 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74  .  ** are no act
2896e 69 76 65 20 56 4d 73 2c 20 69 6e 76 61 6c 69 64  ive VMs, invalid
2896f 61 74 65 20 61 6e 79 20 70 72 65 2d 63 6f 6d 70  ate any pre-comp
28970 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e  iled statements.
28971 0a 20 20 2a 2f 0a 20 20 70 43 6f 6c 6c 20 3d 20  .  */.  pColl = 
28972 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53  sqlite3FindCollS
28973 65 71 28 64 62 2c 20 28 75 38 29 65 6e 63 32 2c  eq(db, (u8)enc2,
28974 20 7a 4e 61 6d 65 2c 20 73 74 72 6c 65 6e 28 7a   zName, strlen(z
28975 4e 61 6d 65 29 2c 20 30 29 3b 0a 20 20 69 66 28  Name), 0);.  if(
28976 20 70 43 6f 6c 6c 20 26 26 20 70 43 6f 6c 6c 2d   pColl && pColl-
28977 3e 78 43 6d 70 20 29 7b 0a 20 20 20 20 69 66 28  >xCmp ){.    if(
28978 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43   db->activeVdbeC
28979 6e 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  nt ){.      sqli
2897a 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c  te3Error(db, SQL
2897b 49 54 45 5f 42 55 53 59 2c 20 0a 20 20 20 20 20  ITE_BUSY, .     
2897c 20 20 20 22 55 6e 61 62 6c 65 20 74 6f 20 64 65     "Unable to de
2897d 6c 65 74 65 2f 6d 6f 64 69 66 79 20 63 6f 6c 6c  lete/modify coll
2897e 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 64  ation sequence d
2897f 75 65 20 74 6f 20 61 63 74 69 76 65 20 73 74 61  ue to active sta
28980 74 65 6d 65 6e 74 73 22 29 3b 0a 20 20 20 20 20  tements");.     
28981 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
28982 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  USY;.    }.    s
28983 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70  qlite3ExpirePrep
28984 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64  aredStatements(d
28985 62 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 63  b);..    /* If c
28986 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
28987 65 20 70 43 6f 6c 6c 20 77 61 73 20 63 72 65 61  e pColl was crea
28988 74 65 64 20 64 69 72 65 63 74 6c 79 20 62 79 20  ted directly by 
28989 61 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a  a call to.    **
2898a 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2898b 63 6f 6c 6c 61 74 69 6f 6e 2c 20 61 6e 64 20 6e  collation, and n
2898c 6f 74 20 67 65 6e 65 72 61 74 65 64 20 62 79 20  ot generated by 
2898d 73 79 6e 74 68 43 6f 6c 6c 53 65 71 28 29 2c 0a  synthCollSeq(),.
2898e 20 20 20 20 2a 2a 20 74 68 65 6e 20 61 6e 79 20      ** then any 
2898f 63 6f 70 69 65 73 20 6d 61 64 65 20 62 79 20 73  copies made by s
28990 79 6e 74 68 43 6f 6c 6c 53 65 71 28 29 20 6e 65  ynthCollSeq() ne
28991 65 64 20 74 6f 20 62 65 20 69 6e 76 61 6c 69 64  ed to be invalid
28992 61 74 65 64 2e 0a 20 20 20 20 2a 2a 20 41 6c 73  ated..    ** Als
28993 6f 2c 20 63 6f 6c 6c 61 74 69 6f 6e 20 64 65 73  o, collation des
28994 74 72 75 63 74 6f 72 20 2d 20 43 6f 6c 6c 53 65  tructor - CollSe
28995 71 2e 78 44 65 6c 28 29 20 2d 20 66 75 6e 63 74  q.xDel() - funct
28996 69 6f 6e 20 6d 61 79 20 6e 65 65 64 0a 20 20 20  ion may need.   
28997 20 2a 2a 20 74 6f 20 62 65 20 63 61 6c 6c 65 64   ** to be called
28998 2e 0a 20 20 20 20 2a 2f 20 0a 20 20 20 20 69 66  ..    */ .    if
28999 28 20 28 70 43 6f 6c 6c 2d 3e 65 6e 63 20 26 20  ( (pColl->enc & 
2899a 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c  ~SQLITE_UTF16_AL
2899b 49 47 4e 45 44 29 3d 3d 65 6e 63 32 20 29 7b 0a  IGNED)==enc2 ){.
2899c 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 61        CollSeq *a
2899d 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 48 61  Coll = sqlite3Ha
2899e 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 43 6f 6c  shFind(&db->aCol
2899f 6c 53 65 71 2c 20 7a 4e 61 6d 65 2c 20 73 74 72  lSeq, zName, str
289a0 6c 65 6e 28 7a 4e 61 6d 65 29 29 3b 0a 20 20 20  len(zName));.   
289a1 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
289a2 66 6f 72 28 6a 3d 30 3b 20 6a 3c 33 3b 20 6a 2b  for(j=0; j<3; j+
289a3 2b 29 7b 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c  +){.        Coll
289a4 53 65 71 20 2a 70 20 3d 20 26 61 43 6f 6c 6c 5b  Seq *p = &aColl[
289a5 6a 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  j];.        if( 
289a6 70 2d 3e 65 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e 65  p->enc==pColl->e
289a7 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  nc ){.          
289a8 69 66 28 20 70 2d 3e 78 44 65 6c 20 29 7b 0a 20  if( p->xDel ){. 
289a9 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 78 44             p->xD
289aa 65 6c 28 70 2d 3e 70 55 73 65 72 29 3b 0a 20 20  el(p->pUser);.  
289ab 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
289ac 20 20 20 20 70 2d 3e 78 43 6d 70 20 3d 20 30 3b      p->xCmp = 0;
289ad 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
289ae 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
289af 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46  pColl = sqlite3F
289b0 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 28  indCollSeq(db, (
289b1 75 38 29 65 6e 63 32 2c 20 7a 4e 61 6d 65 2c 20  u8)enc2, zName, 
289b2 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 2c 20 31  strlen(zName), 1
289b3 29 3b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 20 29  );.  if( pColl )
289b4 7b 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e 78 43 6d  {.    pColl->xCm
289b5 70 20 3d 20 78 43 6f 6d 70 61 72 65 3b 0a 20 20  p = xCompare;.  
289b6 20 20 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 20 3d    pColl->pUser =
289b7 20 70 43 74 78 3b 0a 20 20 20 20 70 43 6f 6c 6c   pCtx;.    pColl
289b8 2d 3e 78 44 65 6c 20 3d 20 78 44 65 6c 3b 0a 20  ->xDel = xDel;. 
289b9 20 20 20 70 43 6f 6c 6c 2d 3e 65 6e 63 20 3d 20     pColl->enc = 
289ba 65 6e 63 32 20 7c 20 28 65 6e 63 20 26 20 53 51  enc2 | (enc & SQ
289bb 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e  LITE_UTF16_ALIGN
289bc 45 44 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ED);.  }.  sqlit
289bd 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49  e3Error(db, SQLI
289be 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 72 65 74  TE_OK, 0);.  ret
289bf 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
289c0 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 61 72  .../*.** This ar
289c1 72 61 79 20 64 65 66 69 6e 65 73 20 68 61 72 64  ray defines hard
289c2 20 75 70 70 65 72 20 62 6f 75 6e 64 73 20 6f 6e   upper bounds on
289c3 20 6c 69 6d 69 74 20 76 61 6c 75 65 73 2e 20 20   limit values.  
289c4 54 68 65 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a  The.** initializ
289c5 65 72 20 6d 75 73 74 20 62 65 20 6b 65 70 74 20  er must be kept 
289c6 69 6e 20 73 79 6e 63 20 77 69 74 68 20 74 68 65  in sync with the
289c7 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 2a 0a   SQLITE_LIMIT_*.
289c8 2a 2a 20 23 64 65 66 69 6e 65 73 20 69 6e 20 73  ** #defines in s
289c9 71 6c 69 74 65 33 2e 68 2e 0a 2a 2f 0a 73 74 61  qlite3.h..*/.sta
289ca 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 61 48  tic const int aH
289cb 61 72 64 4c 69 6d 69 74 5b 5d 20 3d 20 7b 0a 20  ardLimit[] = {. 
289cc 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47   SQLITE_MAX_LENG
289cd 54 48 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58  TH,.  SQLITE_MAX
289ce 5f 53 51 4c 5f 4c 45 4e 47 54 48 2c 0a 20 20 53  _SQL_LENGTH,.  S
289cf 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e  QLITE_MAX_COLUMN
289d0 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45  ,.  SQLITE_MAX_E
289d1 58 50 52 5f 44 45 50 54 48 2c 0a 20 20 53 51 4c  XPR_DEPTH,.  SQL
289d2 49 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44  ITE_MAX_COMPOUND
289d3 5f 53 45 4c 45 43 54 2c 0a 20 20 53 51 4c 49 54  _SELECT,.  SQLIT
289d4 45 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 2c 0a 20  E_MAX_VDBE_OP,. 
289d5 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43   SQLITE_MAX_FUNC
289d6 54 49 4f 4e 5f 41 52 47 2c 0a 20 20 53 51 4c 49  TION_ARG,.  SQLI
289d7 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 2c  TE_MAX_ATTACHED,
289d8 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49  .  SQLITE_MAX_LI
289d9 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54  KE_PATTERN_LENGT
289da 48 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f  H,.  SQLITE_MAX_
289db 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 2c  VARIABLE_NUMBER,
289dc 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20  .};../*.** Make 
289dd 73 75 72 65 20 74 68 65 20 68 61 72 64 20 6c 69  sure the hard li
289de 6d 69 74 73 20 61 72 65 20 73 65 74 20 74 6f 20  mits are set to 
289df 72 65 61 73 6f 6e 61 62 6c 65 20 76 61 6c 75 65  reasonable value
289e0 73 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  s.*/.#if SQLITE_
289e1 4d 41 58 5f 4c 45 4e 47 54 48 3c 31 30 30 0a 23  MAX_LENGTH<100.#
289e2 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41   error SQLITE_MA
289e3 58 5f 4c 45 4e 47 54 48 20 6d 75 73 74 20 62 65  X_LENGTH must be
289e4 20 61 74 20 6c 65 61 73 74 20 31 30 30 0a 23 65   at least 100.#e
289e5 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
289e6 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 3c 31  MAX_SQL_LENGTH<1
289e7 30 30 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54  00.# error SQLIT
289e8 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48  E_MAX_SQL_LENGTH
289e9 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 73   must be at leas
289ea 74 20 31 30 30 0a 23 65 6e 64 69 66 0a 23 69 66  t 100.#endif.#if
289eb 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f   SQLITE_MAX_SQL_
289ec 4c 45 4e 47 54 48 3e 53 51 4c 49 54 45 5f 4d 41  LENGTH>SQLITE_MA
289ed 58 5f 4c 45 4e 47 54 48 0a 23 20 65 72 72 6f 72  X_LENGTH.# error
289ee 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f   SQLITE_MAX_SQL_
289ef 4c 45 4e 47 54 48 20 6d 75 73 74 20 6e 6f 74 20  LENGTH must not 
289f0 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  be greater than 
289f1 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54  SQLITE_MAX_LENGT
289f2 48 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c  H.#endif.#if SQL
289f3 49 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44  ITE_MAX_COMPOUND
289f4 5f 53 45 4c 45 43 54 3c 32 0a 23 20 65 72 72 6f  _SELECT<2.# erro
289f5 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d  r SQLITE_MAX_COM
289f6 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 6d 75 73  POUND_SELECT mus
289f7 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 32 0a  t be at least 2.
289f8 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
289f9 45 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 3c 34 30  E_MAX_VDBE_OP<40
289fa 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f  .# error SQLITE_
289fb 4d 41 58 5f 56 44 42 45 5f 4f 50 20 6d 75 73 74  MAX_VDBE_OP must
289fc 20 62 65 20 61 74 20 6c 65 61 73 74 20 34 30 0a   be at least 40.
289fd 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
289fe 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41  E_MAX_FUNCTION_A
289ff 52 47 3c 30 20 7c 7c 20 53 51 4c 49 54 45 5f 4d  RG<0 || SQLITE_M
28a00 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 3e  AX_FUNCTION_ARG>
28a01 32 35 35 0a 23 20 65 72 72 6f 72 20 53 51 4c 49  255.# error SQLI
28a02 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f  TE_MAX_FUNCTION_
28a03 41 52 47 20 6d 75 73 74 20 62 65 20 62 65 74 77  ARG must be betw
28a04 65 65 6e 20 30 20 61 6e 64 20 32 35 35 0a 23 65  een 0 and 255.#e
28a05 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
28a06 4d 41 58 5f 41 54 54 41 43 48 3c 30 20 7c 7c 20  MAX_ATTACH<0 || 
28a07 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43  SQLITE_MAX_ATTAC
28a08 48 3e 33 30 0a 23 20 65 72 72 6f 72 20 53 51 4c  H>30.# error SQL
28a09 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 20 6d  ITE_MAX_ATTACH m
28a0a 75 73 74 20 62 65 20 62 65 74 77 65 65 6e 20 30  ust be between 0
28a0b 20 61 6e 64 20 33 30 0a 23 65 6e 64 69 66 0a 23   and 30.#endif.#
28a0c 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49  if SQLITE_MAX_LI
28a0d 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54  KE_PATTERN_LENGT
28a0e 48 3c 31 0a 23 20 65 72 72 6f 72 20 53 51 4c 49  H<1.# error SQLI
28a0f 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54  TE_MAX_LIKE_PATT
28a10 45 52 4e 5f 4c 45 4e 47 54 48 20 6d 75 73 74 20  ERN_LENGTH must 
28a11 62 65 20 61 74 20 6c 65 61 73 74 20 31 0a 23 65  be at least 1.#e
28a12 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
28a13 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d  MAX_VARIABLE_NUM
28a14 42 45 52 3c 31 0a 23 20 65 72 72 6f 72 20 53 51  BER<1.# error SQ
28a15 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c  LITE_MAX_VARIABL
28a16 45 5f 4e 55 4d 42 45 52 20 6d 75 73 74 20 62 65  E_NUMBER must be
28a17 20 61 74 20 6c 65 61 73 74 20 31 0a 23 65 6e 64   at least 1.#end
28a18 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  if.../*.** Chang
28a19 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61  e the value of a
28a1a 20 6c 69 6d 69 74 2e 20 20 52 65 70 6f 72 74 20   limit.  Report 
28a1b 74 68 65 20 6f 6c 64 20 76 61 6c 75 65 2e 0a 2a  the old value..*
28a1c 2a 20 49 66 20 61 6e 20 69 6e 76 61 6c 69 64 20  * If an invalid 
28a1d 6c 69 6d 69 74 20 69 6e 64 65 78 20 69 73 20 73  limit index is s
28a1e 75 70 70 6c 69 65 64 2c 20 72 65 70 6f 72 74 20  upplied, report 
28a1f 2d 31 2e 0a 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63  -1..** Make no c
28a20 68 61 6e 67 65 73 20 62 75 74 20 73 74 69 6c 6c  hanges but still
28a21 20 72 65 70 6f 72 74 20 74 68 65 20 6f 6c 64 20   report the old 
28a22 76 61 6c 75 65 20 69 66 20 74 68 65 0a 2a 2a 20  value if the.** 
28a23 6e 65 77 20 6c 69 6d 69 74 20 69 73 20 6e 65 67  new limit is neg
28a24 61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e  ative..**.** A n
28a25 65 77 20 6c 6f 77 65 72 20 6c 69 6d 69 74 20 64  ew lower limit d
28a26 6f 65 73 20 6e 6f 74 20 73 68 72 69 6e 6b 20 65  oes not shrink e
28a27 78 69 73 74 69 6e 67 20 63 6f 6e 73 74 72 75 63  xisting construc
28a28 74 73 2e 0a 2a 2a 20 49 74 20 6d 65 72 65 6c 79  ts..** It merely
28a29 20 70 72 65 76 65 6e 74 73 20 6e 65 77 20 63 6f   prevents new co
28a2a 6e 73 74 72 75 63 74 73 20 74 68 61 74 20 65 78  nstructs that ex
28a2b 63 65 65 64 20 74 68 65 20 6c 69 6d 69 74 0a 2a  ceed the limit.*
28a2c 2a 20 66 72 6f 6d 20 66 6f 72 6d 69 6e 67 2e 0a  * from forming..
28a2d 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
28a2e 74 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28  t sqlite3_limit(
28a2f 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
28a30 20 6c 69 6d 69 74 49 64 2c 20 69 6e 74 20 6e 65   limitId, int ne
28a31 77 4c 69 6d 69 74 29 7b 0a 20 20 69 6e 74 20 6f  wLimit){.  int o
28a32 6c 64 4c 69 6d 69 74 3b 0a 20 20 69 66 28 20 6c  ldLimit;.  if( l
28a33 69 6d 69 74 49 64 3c 30 20 7c 7c 20 6c 69 6d 69  imitId<0 || limi
28a34 74 49 64 3e 3d 53 51 4c 49 54 45 5f 4e 5f 4c 49  tId>=SQLITE_N_LI
28a35 4d 49 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72  MIT ){.    retur
28a36 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 6f 6c 64 4c  n -1;.  }.  oldL
28a37 69 6d 69 74 20 3d 20 64 62 2d 3e 61 4c 69 6d 69  imit = db->aLimi
28a38 74 5b 6c 69 6d 69 74 49 64 5d 3b 0a 20 20 69 66  t[limitId];.  if
28a39 28 20 6e 65 77 4c 69 6d 69 74 3e 3d 30 20 29 7b  ( newLimit>=0 ){
28a3a 0a 20 20 20 20 69 66 28 20 6e 65 77 4c 69 6d 69  .    if( newLimi
28a3b 74 3e 61 48 61 72 64 4c 69 6d 69 74 5b 6c 69 6d  t>aHardLimit[lim
28a3c 69 74 49 64 5d 20 29 7b 0a 20 20 20 20 20 20 6e  itId] ){.      n
28a3d 65 77 4c 69 6d 69 74 20 3d 20 61 48 61 72 64 4c  ewLimit = aHardL
28a3e 69 6d 69 74 5b 6c 69 6d 69 74 49 64 5d 3b 0a 20  imit[limitId];. 
28a3f 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 61 4c 69     }.    db->aLi
28a40 6d 69 74 5b 6c 69 6d 69 74 49 64 5d 20 3d 20 6e  mit[limitId] = n
28a41 65 77 4c 69 6d 69 74 3b 0a 20 20 7d 0a 20 20 72  ewLimit;.  }.  r
28a42 65 74 75 72 6e 20 6f 6c 64 4c 69 6d 69 74 3b 0a  eturn oldLimit;.
28a43 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
28a44 75 74 69 6e 65 20 64 6f 65 73 20 74 68 65 20 77  utine does the w
28a45 6f 72 6b 20 6f 66 20 6f 70 65 6e 69 6e 67 20 61  ork of opening a
28a46 20 64 61 74 61 62 61 73 65 20 6f 6e 20 62 65 68   database on beh
28a47 61 6c 66 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65  alf of.** sqlite
28a48 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c  3_open() and sql
28a49 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 2e 20 54  ite3_open16(). T
28a4a 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
28a4b 6e 61 6d 65 20 22 7a 46 69 6c 65 6e 61 6d 65 22  name "zFilename"
28a4c 20 20 0a 2a 2a 20 69 73 20 55 54 46 2d 38 20 65    .** is UTF-8 e
28a4d 6e 63 6f 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  ncoded..*/.stati
28a4e 63 20 69 6e 74 20 6f 70 65 6e 44 61 74 61 62 61  c int openDataba
28a4f 73 65 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  se(.  const char
28a50 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 2f 2a 20   *zFilename, /* 
28a51 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d  Database filenam
28a52 65 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20  e UTF-8 encoded 
28a53 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70  */.  sqlite3 **p
28a54 70 44 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f  pDb,        /* O
28a55 55 54 3a 20 52 65 74 75 72 6e 65 64 20 64 61 74  UT: Returned dat
28a56 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  abase handle */.
28a57 20 20 75 6e 73 69 67 6e 65 64 20 66 6c 61 67 73    unsigned flags
28a58 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 72  ,        /* Oper
28a59 61 74 69 6f 6e 61 6c 20 66 6c 61 67 73 20 2a 2f  ational flags */
28a5a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
28a5b 56 66 73 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  Vfs       /* Nam
28a5c 65 20 6f 66 20 74 68 65 20 56 46 53 20 74 6f 20  e of the VFS to 
28a5d 75 73 65 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  use */.){.  sqli
28a5e 74 65 33 20 2a 64 62 3b 0a 20 20 69 6e 74 20 72  te3 *db;.  int r
28a5f 63 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43  c;.  CollSeq *pC
28a60 6f 6c 6c 3b 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76  oll;..  /* Remov
28a61 65 20 68 61 72 6d 66 75 6c 20 62 69 74 73 20 66  e harmful bits f
28a62 72 6f 6d 20 74 68 65 20 66 6c 61 67 73 20 70 61  rom the flags pa
28a63 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 66 6c 61  rameter */.  fla
28a64 67 73 20 26 3d 20 20 7e 28 20 53 51 4c 49 54 45  gs &=  ~( SQLITE
28a65 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c  _OPEN_DELETEONCL
28a66 4f 53 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20  OSE |.          
28a67 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
28a68 5f 4d 41 49 4e 5f 44 42 20 7c 0a 20 20 20 20 20  _MAIN_DB |.     
28a69 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
28a6a 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 20 7c 20  _OPEN_TEMP_DB | 
28a6b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
28a6c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e  SQLITE_OPEN_TRAN
28a6d 53 49 45 4e 54 5f 44 42 20 7c 20 0a 20 20 20 20  SIENT_DB | .    
28a6e 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
28a6f 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52  E_OPEN_MAIN_JOUR
28a70 4e 41 4c 20 7c 20 0a 20 20 20 20 20 20 20 20 20  NAL | .         
28a71 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
28a72 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 7c  N_TEMP_JOURNAL |
28a73 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
28a74 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42   SQLITE_OPEN_SUB
28a75 4a 4f 55 52 4e 41 4c 20 7c 20 0a 20 20 20 20 20  JOURNAL | .     
28a76 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
28a77 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55  _OPEN_MASTER_JOU
28a78 52 4e 41 4c 0a 20 20 20 20 20 20 20 20 20 20 20  RNAL.           
28a79 20 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63    );..  /* Alloc
28a7a 61 74 65 20 74 68 65 20 73 71 6c 69 74 65 20 64  ate the sqlite d
28a7b 61 74 61 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ata structure */
28a7c 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65 33 4d  .  db = sqlite3M
28a7d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f  allocZero( sizeo
28a7e 66 28 73 71 6c 69 74 65 33 29 20 29 3b 0a 20 20  f(sqlite3) );.  
28a7f 69 66 28 20 64 62 3d 3d 30 20 29 20 67 6f 74 6f  if( db==0 ) goto
28a80 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 64   opendb_out;.  d
28a81 62 2d 3e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74  b->mutex = sqlit
28a82 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53  e3_mutex_alloc(S
28a83 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
28a84 52 53 49 56 45 29 3b 0a 20 20 69 66 28 20 64 62  RSIVE);.  if( db
28a85 2d 3e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20  ->mutex==0 ){.  
28a86 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 64    sqlite3_free(d
28a87 62 29 3b 0a 20 20 20 20 64 62 20 3d 20 30 3b 0a  b);.    db = 0;.
28a88 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f      goto opendb_
28a89 6f 75 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  out;.  }.  sqlit
28a8a 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
28a8b 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d  b->mutex);.  db-
28a8c 3e 65 72 72 4d 61 73 6b 20 3d 20 30 78 66 66 3b  >errMask = 0xff;
28a8d 0a 20 20 64 62 2d 3e 70 72 69 6f 72 4e 65 77 52  .  db->priorNewR
28a8e 6f 77 69 64 20 3d 20 30 3b 0a 20 20 64 62 2d 3e  owid = 0;.  db->
28a8f 6e 44 62 20 3d 20 32 3b 0a 20 20 64 62 2d 3e 6d  nDb = 2;.  db->m
28a90 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41  agic = SQLITE_MA
28a91 47 49 43 5f 42 55 53 59 3b 0a 20 20 64 62 2d 3e  GIC_BUSY;.  db->
28a92 61 44 62 20 3d 20 64 62 2d 3e 61 44 62 53 74 61  aDb = db->aDbSta
28a93 74 69 63 3b 0a 20 20 61 73 73 65 72 74 28 20 73  tic;.  assert( s
28a94 69 7a 65 6f 66 28 64 62 2d 3e 61 4c 69 6d 69 74  izeof(db->aLimit
28a95 29 3d 3d 73 69 7a 65 6f 66 28 61 48 61 72 64 4c  )==sizeof(aHardL
28a96 69 6d 69 74 29 20 29 3b 0a 20 20 6d 65 6d 63 70  imit) );.  memcp
28a97 79 28 64 62 2d 3e 61 4c 69 6d 69 74 2c 20 61 48  y(db->aLimit, aH
28a98 61 72 64 4c 69 6d 69 74 2c 20 73 69 7a 65 6f 66  ardLimit, sizeof
28a99 28 64 62 2d 3e 61 4c 69 6d 69 74 29 29 3b 0a 20  (db->aLimit));. 
28a9a 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20   db->autoCommit 
28a9b 3d 20 31 3b 0a 20 20 64 62 2d 3e 6e 65 78 74 41  = 1;.  db->nextA
28a9c 75 74 6f 76 61 63 20 3d 20 2d 31 3b 0a 20 20 64  utovac = -1;.  d
28a9d 62 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a 65 20  b->nextPagesize 
28a9e 3d 20 30 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73  = 0;.  db->flags
28a9f 20 7c 3d 20 53 51 4c 49 54 45 5f 53 68 6f 72 74   |= SQLITE_Short
28aa0 43 6f 6c 4e 61 6d 65 73 0a 23 69 66 20 53 51 4c  ColNames.#if SQL
28aa1 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45  ITE_DEFAULT_FILE
28aa2 5f 46 4f 52 4d 41 54 3c 34 0a 20 20 20 20 20 20  _FORMAT<4.      
28aa3 20 20 20 20 20 20 20 20 20 20 20 7c 20 53 51 4c             | SQL
28aa4 49 54 45 5f 4c 65 67 61 63 79 46 69 6c 65 46 6d  ITE_LegacyFileFm
28aa5 74 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  t.#endif.#ifdef 
28aa6 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
28aa7 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 20 20 20  AD_EXTENSION.   
28aa8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20                | 
28aa9 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e  SQLITE_LoadExten
28aaa 73 69 6f 6e 0a 23 65 6e 64 69 66 0a 20 20 20 20  sion.#endif.    
28aab 20 20 3b 0a 20 20 73 71 6c 69 74 65 33 48 61 73    ;.  sqlite3Has
28aac 68 49 6e 69 74 28 26 64 62 2d 3e 61 46 75 6e 63  hInit(&db->aFunc
28aad 2c 20 53 51 4c 49 54 45 5f 48 41 53 48 5f 53 54  , SQLITE_HASH_ST
28aae 52 49 4e 47 2c 20 30 29 3b 0a 20 20 73 71 6c 69  RING, 0);.  sqli
28aaf 74 65 33 48 61 73 68 49 6e 69 74 28 26 64 62 2d  te3HashInit(&db-
28ab0 3e 61 43 6f 6c 6c 53 65 71 2c 20 53 51 4c 49 54  >aCollSeq, SQLIT
28ab1 45 5f 48 41 53 48 5f 53 54 52 49 4e 47 2c 20 30  E_HASH_STRING, 0
28ab2 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
28ab3 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
28ab4 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 48 61 73  BLE.  sqlite3Has
28ab5 68 49 6e 69 74 28 26 64 62 2d 3e 61 4d 6f 64 75  hInit(&db->aModu
28ab6 6c 65 2c 20 53 51 4c 49 54 45 5f 48 41 53 48 5f  le, SQLITE_HASH_
28ab7 53 54 52 49 4e 47 2c 20 30 29 3b 0a 23 65 6e 64  STRING, 0);.#end
28ab8 69 66 0a 0a 20 20 64 62 2d 3e 70 56 66 73 20 3d  if..  db->pVfs =
28ab9 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e   sqlite3_vfs_fin
28aba 64 28 7a 56 66 73 29 3b 0a 20 20 69 66 28 20 21  d(zVfs);.  if( !
28abb 64 62 2d 3e 70 56 66 73 20 29 7b 0a 20 20 20 20  db->pVfs ){.    
28abc 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
28abd 52 3b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63  R;.    db->magic
28abe 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f   = SQLITE_MAGIC_
28abf 53 49 43 4b 3b 0a 20 20 20 20 73 71 6c 69 74 65  SICK;.    sqlite
28ac0 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 22  3Error(db, rc, "
28ac1 6e 6f 20 73 75 63 68 20 76 66 73 3a 20 25 73 22  no such vfs: %s"
28ac2 2c 20 7a 56 66 73 29 3b 0a 20 20 20 20 67 6f 74  , zVfs);.    got
28ac3 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20  o opendb_out;.  
28ac4 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20  }..  /* Add the 
28ac5 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f  default collatio
28ac6 6e 20 73 65 71 75 65 6e 63 65 20 42 49 4e 41 52  n sequence BINAR
28ac7 59 2e 20 42 49 4e 41 52 59 20 77 6f 72 6b 73 20  Y. BINARY works 
28ac8 66 6f 72 20 62 6f 74 68 20 55 54 46 2d 38 0a 20  for both UTF-8. 
28ac9 20 2a 2a 20 61 6e 64 20 55 54 46 2d 31 36 2c 20   ** and UTF-16, 
28aca 73 6f 20 61 64 64 20 61 20 76 65 72 73 69 6f 6e  so add a version
28acb 20 66 6f 72 20 65 61 63 68 20 74 6f 20 61 76 6f   for each to avo
28acc 69 64 20 61 6e 79 20 75 6e 6e 65 63 65 73 73 61  id any unnecessa
28acd 72 79 0a 20 20 2a 2a 20 63 6f 6e 76 65 72 73 69  ry.  ** conversi
28ace 6f 6e 73 2e 20 54 68 65 20 6f 6e 6c 79 20 65 72  ons. The only er
28acf 72 6f 72 20 74 68 61 74 20 63 61 6e 20 6f 63 63  ror that can occ
28ad0 75 72 20 68 65 72 65 20 69 73 20 61 20 6d 61 6c  ur here is a mal
28ad1 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 2e 0a 20  loc() failure.. 
28ad2 20 2a 2f 0a 20 20 63 72 65 61 74 65 43 6f 6c 6c   */.  createColl
28ad3 61 74 69 6f 6e 28 64 62 2c 20 22 42 49 4e 41 52  ation(db, "BINAR
28ad4 59 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  Y", SQLITE_UTF8,
28ad5 20 30 2c 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c   0, binCollFunc,
28ad6 20 30 29 3b 0a 20 20 63 72 65 61 74 65 43 6f 6c   0);.  createCol
28ad7 6c 61 74 69 6f 6e 28 64 62 2c 20 22 42 49 4e 41  lation(db, "BINA
28ad8 52 59 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  RY", SQLITE_UTF1
28ad9 36 42 45 2c 20 30 2c 20 62 69 6e 43 6f 6c 6c 46  6BE, 0, binCollF
28ada 75 6e 63 2c 20 30 29 3b 0a 20 20 63 72 65 61 74  unc, 0);.  creat
28adb 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 22  eCollation(db, "
28adc 42 49 4e 41 52 59 22 2c 20 53 51 4c 49 54 45 5f  BINARY", SQLITE_
28add 55 54 46 31 36 4c 45 2c 20 30 2c 20 62 69 6e 43  UTF16LE, 0, binC
28ade 6f 6c 6c 46 75 6e 63 2c 20 30 29 3b 0a 20 20 63  ollFunc, 0);.  c
28adf 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64  reateCollation(d
28ae0 62 2c 20 22 52 54 52 49 4d 22 2c 20 53 51 4c 49  b, "RTRIM", SQLI
28ae1 54 45 5f 55 54 46 38 2c 20 28 76 6f 69 64 2a 29  TE_UTF8, (void*)
28ae2 31 2c 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c 20  1, binCollFunc, 
28ae3 30 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  0);.  if( db->ma
28ae4 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
28ae5 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51    db->magic = SQ
28ae6 4c 49 54 45 5f 4d 41 47 49 43 5f 53 49 43 4b 3b  LITE_MAGIC_SICK;
28ae7 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62  .    goto opendb
28ae8 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e  _out;.  }.  db->
28ae9 70 44 66 6c 74 43 6f 6c 6c 20 3d 20 73 71 6c 69  pDfltColl = sqli
28aea 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64  te3FindCollSeq(d
28aeb 62 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  b, SQLITE_UTF8, 
28aec 22 42 49 4e 41 52 59 22 2c 20 36 2c 20 30 29 3b  "BINARY", 6, 0);
28aed 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 70  .  assert( db->p
28aee 44 66 6c 74 43 6f 6c 6c 21 3d 30 20 29 3b 0a 0a  DfltColl!=0 );..
28aef 20 20 2f 2a 20 41 6c 73 6f 20 61 64 64 20 61 20    /* Also add a 
28af0 55 54 46 2d 38 20 63 61 73 65 2d 69 6e 73 65 6e  UTF-8 case-insen
28af1 73 69 74 69 76 65 20 63 6f 6c 6c 61 74 69 6f 6e  sitive collation
28af2 20 73 65 71 75 65 6e 63 65 2e 20 2a 2f 0a 20 20   sequence. */.  
28af3 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28  createCollation(
28af4 64 62 2c 20 22 4e 4f 43 41 53 45 22 2c 20 53 51  db, "NOCASE", SQ
28af5 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 6e 6f  LITE_UTF8, 0, no
28af6 63 61 73 65 43 6f 6c 6c 61 74 69 6e 67 46 75 6e  caseCollatingFun
28af7 63 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 53 65 74  c, 0);..  /* Set
28af8 20 66 6c 61 67 73 20 6f 6e 20 74 68 65 20 62 75   flags on the bu
28af9 69 6c 74 2d 69 6e 20 63 6f 6c 6c 61 74 69 6e 67  ilt-in collating
28afa 20 73 65 71 75 65 6e 63 65 73 20 2a 2f 0a 20 20   sequences */.  
28afb 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 2d 3e 74  db->pDfltColl->t
28afc 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4c  ype = SQLITE_COL
28afd 4c 5f 42 49 4e 41 52 59 3b 0a 20 20 70 43 6f 6c  L_BINARY;.  pCol
28afe 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43  l = sqlite3FindC
28aff 6f 6c 6c 53 65 71 28 64 62 2c 20 53 51 4c 49 54  ollSeq(db, SQLIT
28b00 45 5f 55 54 46 38 2c 20 22 4e 4f 43 41 53 45 22  E_UTF8, "NOCASE"
28b01 2c 20 36 2c 20 30 29 3b 0a 20 20 69 66 28 20 70  , 6, 0);.  if( p
28b02 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 70 43 6f 6c  Coll ){.    pCol
28b03 6c 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  l->type = SQLITE
28b04 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45 3b 0a 20 20  _COLL_NOCASE;.  
28b05 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65  }..  /* Open the
28b06 20 62 61 63 6b 65 6e 64 20 64 61 74 61 62 61 73   backend databas
28b07 65 20 64 72 69 76 65 72 20 2a 2f 0a 20 20 64 62  e driver */.  db
28b08 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 3d 20 66 6c  ->openFlags = fl
28b09 61 67 73 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  ags;.  rc = sqli
28b0a 74 65 33 42 74 72 65 65 46 61 63 74 6f 72 79 28  te3BtreeFactory(
28b0b 64 62 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 30  db, zFilename, 0
28b0c 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  , SQLITE_DEFAULT
28b0d 5f 43 41 43 48 45 5f 53 49 5a 45 2c 20 0a 20 20  _CACHE_SIZE, .  
28b0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28b0f 20 20 20 20 20 20 20 20 20 66 6c 61 67 73 20 7c           flags |
28b10 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49   SQLITE_OPEN_MAI
28b11 4e 5f 44 42 2c 0a 20 20 20 20 20 20 20 20 20 20  N_DB,.          
28b12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28b13 20 26 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74   &db->aDb[0].pBt
28b14 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
28b15 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71  ITE_OK ){.    sq
28b16 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72  lite3Error(db, r
28b17 63 2c 20 30 29 3b 0a 20 20 20 20 64 62 2d 3e 6d  c, 0);.    db->m
28b18 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41  agic = SQLITE_MA
28b19 47 49 43 5f 53 49 43 4b 3b 0a 20 20 20 20 67 6f  GIC_SICK;.    go
28b1a 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20  to opendb_out;. 
28b1b 20 7d 0a 20 20 64 62 2d 3e 61 44 62 5b 30 5d 2e   }.  db->aDb[0].
28b1c 70 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 65  pSchema = sqlite
28b1d 33 53 63 68 65 6d 61 47 65 74 28 64 62 2c 20 64  3SchemaGet(db, d
28b1e 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29 3b 0a  b->aDb[0].pBt);.
28b1f 20 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63    db->aDb[1].pSc
28b20 68 65 6d 61 20 3d 20 73 71 6c 69 74 65 33 53 63  hema = sqlite3Sc
28b21 68 65 6d 61 47 65 74 28 64 62 2c 20 30 29 3b 0a  hemaGet(db, 0);.
28b22 0a 0a 20 20 2f 2a 20 54 68 65 20 64 65 66 61 75  ..  /* The defau
28b23 6c 74 20 73 61 66 65 74 79 5f 6c 65 76 65 6c 20  lt safety_level 
28b24 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74  for the main dat
28b25 61 62 61 73 65 20 69 73 20 27 66 75 6c 6c 27 3b  abase is 'full';
28b26 20 66 6f 72 20 74 68 65 20 74 65 6d 70 0a 20 20   for the temp.  
28b27 2a 2a 20 64 61 74 61 62 61 73 65 20 69 74 20 69  ** database it i
28b28 73 20 27 4e 4f 4e 45 27 2e 20 54 68 69 73 20 6d  s 'NONE'. This m
28b29 61 74 63 68 65 73 20 74 68 65 20 70 61 67 65 72  atches the pager
28b2a 20 6c 61 79 65 72 20 64 65 66 61 75 6c 74 73 2e   layer defaults.
28b2b 20 20 0a 20 20 2a 2f 0a 20 20 64 62 2d 3e 61 44    .  */.  db->aD
28b2c 62 5b 30 5d 2e 7a 4e 61 6d 65 20 3d 20 22 6d 61  b[0].zName = "ma
28b2d 69 6e 22 3b 0a 20 20 64 62 2d 3e 61 44 62 5b 30  in";.  db->aDb[0
28b2e 5d 2e 73 61 66 65 74 79 5f 6c 65 76 65 6c 20 3d  ].safety_level =
28b2f 20 33 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   3;.#ifndef SQLI
28b30 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 20  TE_OMIT_TEMPDB. 
28b31 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 7a 4e 61 6d   db->aDb[1].zNam
28b32 65 20 3d 20 22 74 65 6d 70 22 3b 0a 20 20 64 62  e = "temp";.  db
28b33 2d 3e 61 44 62 5b 31 5d 2e 73 61 66 65 74 79 5f  ->aDb[1].safety_
28b34 6c 65 76 65 6c 20 3d 20 31 3b 0a 23 65 6e 64 69  level = 1;.#endi
28b35 66 0a 0a 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d  f..  db->magic =
28b36 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50   SQLITE_MAGIC_OP
28b37 45 4e 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  EN;.  if( db->ma
28b38 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
28b39 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75    goto opendb_ou
28b3a 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 67  t;.  }..  /* Reg
28b3b 69 73 74 65 72 20 61 6c 6c 20 62 75 69 6c 74 2d  ister all built-
28b3c 69 6e 20 66 75 6e 63 74 69 6f 6e 73 2c 20 62 75  in functions, bu
28b3d 74 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74  t do not attempt
28b3e 20 74 6f 20 72 65 61 64 20 74 68 65 0a 20 20 2a   to read the.  *
28b3f 2a 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  * database schem
28b40 61 20 79 65 74 2e 20 54 68 69 73 20 69 73 20 64  a yet. This is d
28b41 65 6c 61 79 65 64 20 75 6e 74 69 6c 20 74 68 65  elayed until the
28b42 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 65 20   first time the 
28b43 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 69 73  database.  ** is
28b44 20 61 63 63 65 73 73 65 64 2e 0a 20 20 2a 2f 0a   accessed..  */.
28b45 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
28b46 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29  b, SQLITE_OK, 0)
28b47 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 67 69 73  ;.  sqlite3Regis
28b48 74 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74 69  terBuiltinFuncti
28b49 6f 6e 73 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 4c  ons(db);..  /* L
28b4a 6f 61 64 20 61 75 74 6f 6d 61 74 69 63 20 65 78  oad automatic ex
28b4b 74 65 6e 73 69 6f 6e 73 20 2d 20 65 78 74 65 6e  tensions - exten
28b4c 73 69 6f 6e 73 20 74 68 61 74 20 68 61 76 65 20  sions that have 
28b4d 62 65 65 6e 20 72 65 67 69 73 74 65 72 65 64 0a  been registered.
28b4e 20 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73    ** using the s
28b4f 71 6c 69 74 65 33 5f 61 75 74 6f 6d 61 74 69 63  qlite3_automatic
28b50 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 41 50 49  _extension() API
28b51 2e 0a 20 20 2a 2f 0a 20 20 28 76 6f 69 64 29 73  ..  */.  (void)s
28b52 71 6c 69 74 65 33 41 75 74 6f 4c 6f 61 64 45 78  qlite3AutoLoadEx
28b53 74 65 6e 73 69 6f 6e 73 28 64 62 29 3b 0a 20 20  tensions(db);.  
28b54 69 66 28 20 73 71 6c 69 74 65 33 5f 65 72 72 63  if( sqlite3_errc
28b55 6f 64 65 28 64 62 29 21 3d 53 51 4c 49 54 45 5f  ode(db)!=SQLITE_
28b56 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6f  OK ){.    goto o
28b57 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  pendb_out;.  }..
28b58 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
28b59 41 42 4c 45 5f 46 54 53 31 0a 20 20 69 66 28 20  ABLE_FTS1.  if( 
28b5a 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
28b5b 64 20 29 7b 0a 20 20 20 20 65 78 74 65 72 6e 20  d ){.    extern 
28b5c 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 31 49  int sqlite3Fts1I
28b5d 6e 69 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20  nit(sqlite3*);. 
28b5e 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46     rc = sqlite3F
28b5f 74 73 31 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d  ts1Init(db);.  }
28b60 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
28b61 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
28b62 53 32 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d 61  S2.  if( !db->ma
28b63 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 72 63  llocFailed && rc
28b64 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
28b65 20 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71     extern int sq
28b66 6c 69 74 65 33 46 74 73 32 49 6e 69 74 28 73 71  lite3Fts2Init(sq
28b67 6c 69 74 65 33 2a 29 3b 0a 20 20 20 20 72 63 20  lite3*);.    rc 
28b68 3d 20 73 71 6c 69 74 65 33 46 74 73 32 49 6e 69  = sqlite3Fts2Ini
28b69 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  t(db);.  }.#endi
28b6a 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
28b6b 5f 45 4e 41 42 4c 45 5f 46 54 53 33 0a 20 20 69  _ENABLE_FTS3.  i
28b6c 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  f( !db->mallocFa
28b6d 69 6c 65 64 20 26 26 20 72 63 3d 3d 53 51 4c 49  iled && rc==SQLI
28b6e 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20  TE_OK ){.    rc 
28b6f 3d 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69  = sqlite3Fts3Ini
28b70 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  t(db);.  }.#endi
28b71 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
28b72 5f 45 4e 41 42 4c 45 5f 49 43 55 0a 20 20 69 66  _ENABLE_ICU.  if
28b73 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
28b74 6c 65 64 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  led && rc==SQLIT
28b75 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 65 78 74 65  E_OK ){.    exte
28b76 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 49 63  rn int sqlite3Ic
28b77 75 49 6e 69 74 28 73 71 6c 69 74 65 33 2a 29 3b  uInit(sqlite3*);
28b78 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
28b79 33 49 63 75 49 6e 69 74 28 64 62 29 3b 0a 20 20  3IcuInit(db);.  
28b7a 7d 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74  }.#endif.  sqlit
28b7b 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20  e3Error(db, rc, 
28b7c 30 29 3b 0a 0a 20 20 2f 2a 20 2d 44 53 51 4c 49  0);..  /* -DSQLI
28b7d 54 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49  TE_DEFAULT_LOCKI
28b7e 4e 47 5f 4d 4f 44 45 3d 31 20 6d 61 6b 65 73 20  NG_MODE=1 makes 
28b7f 45 58 43 4c 55 53 49 56 45 20 74 68 65 20 64 65  EXCLUSIVE the de
28b80 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 0a 20 20  fault locking.  
28b81 2a 2a 20 6d 6f 64 65 2e 20 20 2d 44 53 51 4c 49  ** mode.  -DSQLI
28b82 54 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49  TE_DEFAULT_LOCKI
28b83 4e 47 5f 4d 4f 44 45 3d 30 20 6d 61 6b 65 20 4e  NG_MODE=0 make N
28b84 4f 52 4d 41 4c 20 74 68 65 20 64 65 66 61 75 6c  ORMAL the defaul
28b85 74 20 6c 6f 63 6b 69 6e 67 0a 20 20 2a 2a 20 6d  t locking.  ** m
28b86 6f 64 65 2e 20 20 44 6f 69 6e 67 20 6e 6f 74 68  ode.  Doing noth
28b87 69 6e 67 20 61 74 20 61 6c 6c 20 61 6c 73 6f 20  ing at all also 
28b88 6d 61 6b 65 73 20 4e 4f 52 4d 41 4c 20 74 68 65  makes NORMAL the
28b89 20 64 65 66 61 75 6c 74 2e 0a 20 20 2a 2f 0a 23   default..  */.#
28b8a 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46  ifdef SQLITE_DEF
28b8b 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44  AULT_LOCKING_MOD
28b8c 45 0a 20 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b  E.  db->dfltLock
28b8d 4d 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 45  Mode = SQLITE_DE
28b8e 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f  FAULT_LOCKING_MO
28b8f 44 45 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67  DE;.  sqlite3Pag
28b90 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 73 71  erLockingMode(sq
28b91 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
28b92 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29 2c  db->aDb[0].pBt),
28b93 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
28b94 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
28b95 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e  E_DEFAULT_LOCKIN
28b96 47 5f 4d 4f 44 45 29 3b 0a 23 65 6e 64 69 66 0a  G_MODE);.#endif.
28b97 0a 6f 70 65 6e 64 62 5f 6f 75 74 3a 0a 20 20 69  .opendb_out:.  i
28b98 66 28 20 64 62 20 29 7b 0a 20 20 20 20 61 73 73  f( db ){.    ass
28b99 65 72 74 28 20 64 62 2d 3e 6d 75 74 65 78 21 3d  ert( db->mutex!=
28b9a 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 );.    sqlite3
28b9b 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
28b9c 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 69  >mutex);.  }.  i
28b9d 66 28 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3d  f( SQLITE_NOMEM=
28b9e 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65  =(rc = sqlite3_e
28b9f 72 72 63 6f 64 65 28 64 62 29 29 20 29 7b 0a 20  rrcode(db)) ){. 
28ba0 20 20 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65     sqlite3_close
28ba1 28 64 62 29 3b 0a 20 20 20 20 64 62 20 3d 20 30  (db);.    db = 0
28ba2 3b 0a 20 20 7d 0a 20 20 2a 70 70 44 62 20 3d 20  ;.  }.  *ppDb = 
28ba3 64 62 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  db;.  return sql
28ba4 69 74 65 33 41 70 69 45 78 69 74 28 30 2c 20 72  ite3ApiExit(0, r
28ba5 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65  c);.}../*.** Ope
28ba6 6e 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65  n a new database
28ba7 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49   handle..*/.SQLI
28ba8 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
28ba9 65 33 5f 6f 70 65 6e 28 0a 20 20 63 6f 6e 73 74  e3_open(.  const
28baa 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
28bab 2c 20 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70  , .  sqlite3 **p
28bac 70 44 62 20 0a 29 7b 0a 20 20 72 65 74 75 72 6e  pDb .){.  return
28bad 20 6f 70 65 6e 44 61 74 61 62 61 73 65 28 7a 46   openDatabase(zF
28bae 69 6c 65 6e 61 6d 65 2c 20 70 70 44 62 2c 0a 20  ilename, ppDb,. 
28baf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28bb0 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
28bb1 5f 52 45 41 44 57 52 49 54 45 20 7c 20 53 51 4c  _READWRITE | SQL
28bb2 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 2c  ITE_OPEN_CREATE,
28bb3 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50   0);.}.SQLITE_AP
28bb4 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70  I int sqlite3_op
28bb5 65 6e 5f 76 32 28 0a 20 20 63 6f 6e 73 74 20 63  en_v2(.  const c
28bb6 68 61 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20  har *filename,  
28bb7 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c   /* Database fil
28bb8 65 6e 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f  ename (UTF-8) */
28bb9 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44  .  sqlite3 **ppD
28bba 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55  b,         /* OU
28bbb 54 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e  T: SQLite db han
28bbc 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61  dle */.  int fla
28bbd 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  gs,             
28bbe 20 2f 2a 20 46 6c 61 67 73 20 2a 2f 0a 20 20 63   /* Flags */.  c
28bbf 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73 20  onst char *zVfs 
28bc0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
28bc1 66 20 56 46 53 20 6d 6f 64 75 6c 65 20 74 6f 20  f VFS module to 
28bc2 75 73 65 20 2a 2f 0a 29 7b 0a 20 20 72 65 74 75  use */.){.  retu
28bc3 72 6e 20 6f 70 65 6e 44 61 74 61 62 61 73 65 28  rn openDatabase(
28bc4 66 69 6c 65 6e 61 6d 65 2c 20 70 70 44 62 2c 20  filename, ppDb, 
28bc5 66 6c 61 67 73 2c 20 7a 56 66 73 29 3b 0a 7d 0a  flags, zVfs);.}.
28bc6 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
28bc7 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a  OMIT_UTF16./*.**
28bc8 20 4f 70 65 6e 20 61 20 6e 65 77 20 64 61 74 61   Open a new data
28bc9 62 61 73 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a  base handle..*/.
28bca 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
28bcb 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 0a 20  qlite3_open16(. 
28bcc 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46 69   const void *zFi
28bcd 6c 65 6e 61 6d 65 2c 20 0a 20 20 73 71 6c 69 74  lename, .  sqlit
28bce 65 33 20 2a 2a 70 70 44 62 0a 29 7b 0a 20 20 63  e3 **ppDb.){.  c
28bcf 68 61 72 20 63 6f 6e 73 74 20 2a 7a 46 69 6c 65  har const *zFile
28bd0 6e 61 6d 65 38 3b 20 20 20 2f 2a 20 7a 46 69 6c  name8;   /* zFil
28bd1 65 6e 61 6d 65 20 65 6e 63 6f 64 65 64 20 69 6e  ename encoded in
28bd2 20 55 54 46 2d 38 20 69 6e 73 74 65 61 64 20 6f   UTF-8 instead o
28bd3 66 20 55 54 46 2d 31 36 20 2a 2f 0a 20 20 73 71  f UTF-16 */.  sq
28bd4 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
28bd5 6c 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  l;.  int rc = SQ
28bd6 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a 20 20 61  LITE_NOMEM;..  a
28bd7 73 73 65 72 74 28 20 7a 46 69 6c 65 6e 61 6d 65  ssert( zFilename
28bd8 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 70   );.  assert( pp
28bd9 44 62 20 29 3b 0a 20 20 2a 70 70 44 62 20 3d 20  Db );.  *ppDb = 
28bda 30 3b 0a 20 20 70 56 61 6c 20 3d 20 73 71 6c 69  0;.  pVal = sqli
28bdb 74 65 33 56 61 6c 75 65 4e 65 77 28 30 29 3b 0a  te3ValueNew(0);.
28bdc 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65    sqlite3ValueSe
28bdd 74 53 74 72 28 70 56 61 6c 2c 20 2d 31 2c 20 7a  tStr(pVal, -1, z
28bde 46 69 6c 65 6e 61 6d 65 2c 20 53 51 4c 49 54 45  Filename, SQLITE
28bdf 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 53 51  _UTF16NATIVE, SQ
28be0 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
28be1 7a 46 69 6c 65 6e 61 6d 65 38 20 3d 20 73 71 6c  zFilename8 = sql
28be2 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56  ite3ValueText(pV
28be3 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29  al, SQLITE_UTF8)
28be4 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d  ;.  if( zFilenam
28be5 65 38 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6f  e8 ){.    rc = o
28be6 70 65 6e 44 61 74 61 62 61 73 65 28 7a 46 69 6c  penDatabase(zFil
28be7 65 6e 61 6d 65 38 2c 20 70 70 44 62 2c 0a 20 20  ename8, ppDb,.  
28be8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28be9 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
28bea 52 45 41 44 57 52 49 54 45 20 7c 20 53 51 4c 49  READWRITE | SQLI
28beb 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 2c 20  TE_OPEN_CREATE, 
28bec 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  0);.    assert( 
28bed 2a 70 70 44 62 20 7c 7c 20 72 63 3d 3d 53 51 4c  *ppDb || rc==SQL
28bee 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20  ITE_NOMEM );.   
28bef 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
28bf0 4f 4b 20 26 26 20 21 44 62 48 61 73 50 72 6f 70  OK && !DbHasProp
28bf1 65 72 74 79 28 2a 70 70 44 62 2c 20 30 2c 20 44  erty(*ppDb, 0, D
28bf2 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 20  B_SchemaLoaded) 
28bf3 29 7b 0a 20 20 20 20 20 20 45 4e 43 28 2a 70 70  ){.      ENC(*pp
28bf4 44 62 29 20 3d 20 53 51 4c 49 54 45 5f 55 54 46  Db) = SQLITE_UTF
28bf5 31 36 4e 41 54 49 56 45 3b 0a 20 20 20 20 7d 0a  16NATIVE;.    }.
28bf6 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 61 6c    }.  sqlite3Val
28bf7 75 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 0a 20  ueFree(pVal);.. 
28bf8 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41   return sqlite3A
28bf9 70 69 45 78 69 74 28 30 2c 20 72 63 29 3b 0a 7d  piExit(0, rc);.}
28bfa 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
28bfb 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a  E_OMIT_UTF16 */.
28bfc 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
28bfd 61 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e 20  a new collation 
28bfe 73 65 71 75 65 6e 63 65 20 77 69 74 68 20 74 68  sequence with th
28bff 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
28c00 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  e db..*/.SQLITE_
28c01 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
28c02 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
28c03 28 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c  (.  sqlite3* db,
28c04 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   .  const char *
28c05 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 6e  zName, .  int en
28c06 63 2c 20 0a 20 20 76 6f 69 64 2a 20 70 43 74 78  c, .  void* pCtx
28c07 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72  ,.  int(*xCompar
28c08 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  e)(void*,int,con
28c09 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  st void*,int,con
28c0a 73 74 20 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69  st void*).){.  i
28c0b 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33  nt rc;.  sqlite3
28c0c 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
28c0d 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72  >mutex);.  asser
28c0e 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  t( !db->mallocFa
28c0f 69 6c 65 64 20 29 3b 0a 20 20 72 63 20 3d 20 63  iled );.  rc = c
28c10 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64  reateCollation(d
28c11 62 2c 20 7a 4e 61 6d 65 2c 20 65 6e 63 2c 20 70  b, zName, enc, p
28c12 43 74 78 2c 20 78 43 6f 6d 70 61 72 65 2c 20 30  Ctx, xCompare, 0
28c13 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
28c14 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29  3ApiExit(db, rc)
28c15 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
28c16 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
28c17 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  x);.  return rc;
28c18 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74  .}../*.** Regist
28c19 65 72 20 61 20 6e 65 77 20 63 6f 6c 6c 61 74 69  er a new collati
28c1a 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69 74 68  on sequence with
28c1b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
28c1c 6e 64 6c 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49  ndle db..*/.SQLI
28c1d 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
28c1e 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
28c1f 69 6f 6e 5f 76 32 28 0a 20 20 73 71 6c 69 74 65  ion_v2(.  sqlite
28c20 33 2a 20 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20  3* db, .  const 
28c21 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20  char *zName, .  
28c22 69 6e 74 20 65 6e 63 2c 20 0a 20 20 76 6f 69 64  int enc, .  void
28c23 2a 20 70 43 74 78 2c 0a 20 20 69 6e 74 28 2a 78  * pCtx,.  int(*x
28c24 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c 69  Compare)(void*,i
28c25 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  nt,const void*,i
28c26 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c  nt,const void*),
28c27 0a 20 20 76 6f 69 64 28 2a 78 44 65 6c 29 28 76  .  void(*xDel)(v
28c28 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72  oid*).){.  int r
28c29 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  c;.  sqlite3_mut
28c2a 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
28c2b 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  ex);.  assert( !
28c2c 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
28c2d 20 29 3b 0a 20 20 72 63 20 3d 20 63 72 65 61 74   );.  rc = creat
28c2e 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 7a  eCollation(db, z
28c2f 4e 61 6d 65 2c 20 65 6e 63 2c 20 70 43 74 78 2c  Name, enc, pCtx,
28c30 20 78 43 6f 6d 70 61 72 65 2c 20 78 44 65 6c 29   xCompare, xDel)
28c31 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
28c32 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b  ApiExit(db, rc);
28c33 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
28c34 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
28c35 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
28c36 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
28c37 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a  E_OMIT_UTF16./*.
28c38 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 6e 65  ** Register a ne
28c39 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  w collation sequ
28c3a 65 6e 63 65 20 77 69 74 68 20 74 68 65 20 64 61  ence with the da
28c3b 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62  tabase handle db
28c3c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
28c3d 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
28c3e 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a  te_collation16(.
28c3f 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 0a    sqlite3* db, .
28c40 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
28c41 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 6e 63 2c  ame, .  int enc,
28c42 20 0a 20 20 76 6f 69 64 2a 20 70 43 74 78 2c 0a   .  void* pCtx,.
28c43 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29    int(*xCompare)
28c44 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74  (void*,int,const
28c45 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74   void*,int,const
28c46 20 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74   void*).){.  int
28c47 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
28c48 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 38 3b  .  char *zName8;
28c49 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
28c4a 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
28c4b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64 62  );.  assert( !db
28c4c 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
28c4d 3b 0a 20 20 7a 4e 61 6d 65 38 20 3d 20 73 71 6c  ;.  zName8 = sql
28c4e 69 74 65 33 55 74 66 31 36 74 6f 38 28 64 62 2c  ite3Utf16to8(db,
28c4f 20 7a 4e 61 6d 65 2c 20 2d 31 29 3b 0a 20 20 69   zName, -1);.  i
28c50 66 28 20 7a 4e 61 6d 65 38 20 29 7b 0a 20 20 20  f( zName8 ){.   
28c51 20 72 63 20 3d 20 63 72 65 61 74 65 43 6f 6c 6c   rc = createColl
28c52 61 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 38  ation(db, zName8
28c53 2c 20 65 6e 63 2c 20 70 43 74 78 2c 20 78 43 6f  , enc, pCtx, xCo
28c54 6d 70 61 72 65 2c 20 30 29 3b 0a 20 20 20 20 73  mpare, 0);.    s
28c55 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4e 61 6d  qlite3_free(zNam
28c56 65 38 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  e8);.  }.  rc = 
28c57 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
28c58 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65  b, rc);.  sqlite
28c59 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
28c5a 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
28c5b 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20  rn rc;.}.#endif 
28c5c 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  /* SQLITE_OMIT_U
28c5d 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52  TF16 */../*.** R
28c5e 65 67 69 73 74 65 72 20 61 20 63 6f 6c 6c 61 74  egister a collat
28c5f 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 61 63  ion sequence fac
28c60 74 6f 72 79 20 63 61 6c 6c 62 61 63 6b 20 77 69  tory callback wi
28c61 74 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20  th the database 
28c62 68 61 6e 64 6c 65 0a 2a 2a 20 64 62 2e 20 52 65  handle.** db. Re
28c63 70 6c 61 63 65 20 61 6e 79 20 70 72 65 76 69 6f  place any previo
28c64 75 73 6c 79 20 69 6e 73 74 61 6c 6c 65 64 20 63  usly installed c
28c65 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
28c66 65 20 66 61 63 74 6f 72 79 2e 0a 2a 2f 0a 53 51  e factory..*/.SQ
28c67 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
28c68 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
28c69 65 65 64 65 64 28 0a 20 20 73 71 6c 69 74 65 33  eeded(.  sqlite3
28c6a 20 2a 64 62 2c 20 0a 20 20 76 6f 69 64 20 2a 70   *db, .  void *p
28c6b 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 2c 20 0a  CollNeededArg, .
28c6c 20 20 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65    void(*xCollNee
28c6d 64 65 64 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74  ded)(void*,sqlit
28c6e 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70  e3*,int eTextRep
28c6f 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 0a 29 7b  ,const char*).){
28c70 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
28c71 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
28c72 29 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65  );.  db->xCollNe
28c73 65 64 65 64 20 3d 20 78 43 6f 6c 6c 4e 65 65 64  eded = xCollNeed
28c74 65 64 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e  ed;.  db->xCollN
28c75 65 65 64 65 64 31 36 20 3d 20 30 3b 0a 20 20 64  eeded16 = 0;.  d
28c76 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72  b->pCollNeededAr
28c77 67 20 3d 20 70 43 6f 6c 6c 4e 65 65 64 65 64 41  g = pCollNeededA
28c78 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  rg;.  sqlite3_mu
28c79 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
28c7a 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  tex);.  return S
28c7b 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66  QLITE_OK;.}..#if
28c7c 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
28c7d 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 52 65 67  _UTF16./*.** Reg
28c7e 69 73 74 65 72 20 61 20 63 6f 6c 6c 61 74 69 6f  ister a collatio
28c7f 6e 20 73 65 71 75 65 6e 63 65 20 66 61 63 74 6f  n sequence facto
28c80 72 79 20 63 61 6c 6c 62 61 63 6b 20 77 69 74 68  ry callback with
28c81 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
28c82 6e 64 6c 65 0a 2a 2a 20 64 62 2e 20 52 65 70 6c  ndle.** db. Repl
28c83 61 63 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73  ace any previous
28c84 6c 79 20 69 6e 73 74 61 6c 6c 65 64 20 63 6f 6c  ly installed col
28c85 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
28c86 66 61 63 74 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49  factory..*/.SQLI
28c87 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
28c88 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  e3_collation_nee
28c89 64 65 64 31 36 28 0a 20 20 73 71 6c 69 74 65 33  ded16(.  sqlite3
28c8a 20 2a 64 62 2c 20 0a 20 20 76 6f 69 64 20 2a 70   *db, .  void *p
28c8b 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 2c 20 0a  CollNeededArg, .
28c8c 20 20 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65    void(*xCollNee
28c8d 64 65 64 31 36 29 28 76 6f 69 64 2a 2c 73 71 6c  ded16)(void*,sql
28c8e 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52  ite3*,int eTextR
28c8f 65 70 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a  ep,const void*).
28c90 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ){.  sqlite3_mut
28c91 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
28c92 65 78 29 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c  ex);.  db->xColl
28c93 4e 65 65 64 65 64 20 3d 20 30 3b 0a 20 20 64 62  Needed = 0;.  db
28c94 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 20  ->xCollNeeded16 
28c95 3d 20 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 3b  = xCollNeeded16;
28c96 0a 20 20 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64  .  db->pCollNeed
28c97 65 64 41 72 67 20 3d 20 70 43 6f 6c 6c 4e 65 65  edArg = pCollNee
28c98 64 65 64 41 72 67 3b 0a 20 20 73 71 6c 69 74 65  dedArg;.  sqlite
28c99 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
28c9a 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
28c9b 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
28c9c 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
28c9d 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a  _OMIT_UTF16 */..
28c9e 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
28c9f 4d 49 54 5f 47 4c 4f 42 41 4c 52 45 43 4f 56 45  MIT_GLOBALRECOVE
28ca0 52 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  R./*.** This fun
28ca1 63 74 69 6f 6e 20 69 73 20 6e 6f 77 20 61 6e 20  ction is now an 
28ca2 61 6e 61 63 68 72 6f 6e 69 73 6d 2e 20 49 74 20  anachronism. It 
28ca3 75 73 65 64 20 74 6f 20 62 65 20 75 73 65 64 20  used to be used 
28ca4 74 6f 20 72 65 63 6f 76 65 72 20 66 72 6f 6d 20  to recover from 
28ca5 61 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 66 61  a.** malloc() fa
28ca6 69 6c 75 72 65 2c 20 62 75 74 20 53 51 4c 69 74  ilure, but SQLit
28ca7 65 20 6e 6f 77 20 64 6f 65 73 20 74 68 69 73 20  e now does this 
28ca8 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a  automatically..*
28ca9 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
28caa 20 73 71 6c 69 74 65 33 5f 67 6c 6f 62 61 6c 5f   sqlite3_global_
28cab 72 65 63 6f 76 65 72 28 76 6f 69 64 29 7b 0a 20  recover(void){. 
28cac 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
28cad 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  K;.}.#endif../*.
28cae 2a 2a 20 54 65 73 74 20 74 6f 20 73 65 65 20 77  ** Test to see w
28caf 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
28cb0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
28cb1 63 74 69 6f 6e 20 69 73 20 69 6e 20 61 75 74 6f  ction is in auto
28cb2 63 6f 6d 6d 69 74 0a 2a 2a 20 6d 6f 64 65 2e 20  commit.** mode. 
28cb3 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
28cb4 69 74 20 69 73 20 61 6e 64 20 46 41 4c 53 45 20  it is and FALSE 
28cb5 69 66 20 6e 6f 74 2e 20 20 41 75 74 6f 63 6f 6d  if not.  Autocom
28cb6 6d 69 74 20 6d 6f 64 65 20 69 73 20 6f 6e 0a 2a  mit mode is on.*
28cb7 2a 20 62 79 20 64 65 66 61 75 6c 74 2e 20 20 41  * by default.  A
28cb8 75 74 6f 63 6f 6d 6d 69 74 20 69 73 20 64 69 73  utocommit is dis
28cb9 61 62 6c 65 64 20 62 79 20 61 20 42 45 47 49 4e  abled by a BEGIN
28cba 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72   statement and r
28cbb 65 65 6e 61 62 6c 65 64 0a 2a 2a 20 62 79 20 74  eenabled.** by t
28cbc 68 65 20 6e 65 78 74 20 43 4f 4d 4d 49 54 20 6f  he next COMMIT o
28cbd 72 20 52 4f 4c 4c 42 41 43 4b 2e 0a 2a 2a 0a 2a  r ROLLBACK..**.*
28cbe 2a 2a 2a 2a 2a 2a 20 54 48 49 53 20 49 53 20 41  ****** THIS IS A
28cbf 4e 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 41  N EXPERIMENTAL A
28cc0 50 49 20 41 4e 44 20 49 53 20 53 55 42 4a 45 43  PI AND IS SUBJEC
28cc1 54 20 54 4f 20 43 48 41 4e 47 45 20 2a 2a 2a 2a  T TO CHANGE ****
28cc2 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  **.*/.SQLITE_API
28cc3 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 74   int sqlite3_get
28cc4 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 73 71 6c 69  _autocommit(sqli
28cc5 74 65 33 20 2a 64 62 29 7b 0a 20 20 72 65 74 75  te3 *db){.  retu
28cc6 72 6e 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69  rn db->autoCommi
28cc7 74 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c  t;.}..#ifdef SQL
28cc8 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20  ITE_DEBUG./*.** 
28cc9 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f  The following ro
28cca 75 74 69 6e 65 20 69 73 20 73 75 62 74 69 74 75  utine is subtitu
28ccb 74 65 64 20 66 6f 72 20 63 6f 6e 73 74 61 6e 74  ted for constant
28ccc 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20   SQLITE_CORRUPT 
28ccd 69 6e 0a 2a 2a 20 64 65 62 75 67 67 69 6e 67 20  in.** debugging 
28cce 62 75 69 6c 64 73 2e 20 20 54 68 69 73 20 70 72  builds.  This pr
28ccf 6f 76 69 64 65 73 20 61 20 77 61 79 20 74 6f 20  ovides a way to 
28cd0 73 65 74 20 61 20 62 72 65 61 6b 70 6f 69 6e 74  set a breakpoint
28cd1 20 66 6f 72 20 77 68 65 6e 0a 2a 2a 20 63 6f 72   for when.** cor
28cd2 72 75 70 74 69 6f 6e 20 69 73 20 66 69 72 73 74  ruption is first
28cd3 20 64 65 74 65 63 74 65 64 2e 0a 2a 2f 0a 53 51   detected..*/.SQ
28cd4 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
28cd5 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28   sqlite3Corrupt(
28cd6 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20  void){.  return 
28cd7 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a  SQLITE_CORRUPT;.
28cd8 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
28cd9 54 68 69 73 20 69 73 20 61 20 63 6f 6e 76 65 6e  This is a conven
28cda 69 65 6e 63 65 20 72 6f 75 74 69 6e 65 20 74 68  ience routine th
28cdb 61 74 20 6d 61 6b 65 73 20 73 75 72 65 20 74 68  at makes sure th
28cdc 61 74 20 61 6c 6c 20 74 68 72 65 61 64 2d 73 70  at all thread-sp
28cdd 65 63 69 66 69 63 0a 2a 2a 20 64 61 74 61 20 66  ecific.** data f
28cde 6f 72 20 74 68 69 73 20 74 68 72 65 61 64 20 68  or this thread h
28cdf 61 73 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63 61  as been dealloca
28ce0 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  ted..**.** SQLit
28ce1 65 20 6e 6f 20 6c 6f 6e 67 65 72 20 75 73 65 73  e no longer uses
28ce2 20 74 68 72 65 61 64 2d 73 70 65 63 69 66 69 63   thread-specific
28ce3 20 64 61 74 61 20 73 6f 20 74 68 69 73 20 72 6f   data so this ro
28ce4 75 74 69 6e 65 20 69 73 20 6e 6f 77 20 61 0a 2a  utine is now a.*
28ce5 2a 20 6e 6f 2d 6f 70 2e 20 20 49 74 20 69 73 20  * no-op.  It is 
28ce6 72 65 74 61 69 6e 65 64 20 66 6f 72 20 68 69 73  retained for his
28ce7 74 6f 72 69 63 61 6c 20 63 6f 6d 70 61 74 69 62  torical compatib
28ce8 69 6c 69 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ility..*/.SQLITE
28ce9 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
28cea 33 5f 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 70  3_thread_cleanup
28ceb 28 76 6f 69 64 29 7b 0a 7d 0a 0a 2f 2a 0a 2a 2a  (void){.}../*.**
28cec 20 52 65 74 75 72 6e 20 6d 65 74 61 20 69 6e 66   Return meta inf
28ced 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61  ormation about a
28cee 20 73 70 65 63 69 66 69 63 20 63 6f 6c 75 6d 6e   specific column
28cef 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 74   of a database t
28cf0 61 62 6c 65 2e 0a 2a 2a 20 53 65 65 20 63 6f 6d  able..** See com
28cf1 6d 65 6e 74 20 69 6e 20 73 71 6c 69 74 65 33 2e  ment in sqlite3.
28cf2 68 20 28 73 71 6c 69 74 65 2e 68 2e 69 6e 29 20  h (sqlite.h.in) 
28cf3 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a  for details..*/.
28cf4 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
28cf5 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41  ABLE_COLUMN_META
28cf6 44 41 54 41 0a 53 51 4c 49 54 45 5f 41 50 49 20  DATA.SQLITE_API 
28cf7 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 61 62 6c  int sqlite3_tabl
28cf8 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74  e_column_metadat
28cf9 61 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  a(.  sqlite3 *db
28cfa 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
28cfb 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 68   /* Connection h
28cfc 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  andle */.  const
28cfd 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20   char *zDbName, 
28cfe 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
28cff 73 65 20 6e 61 6d 65 20 6f 72 20 4e 55 4c 4c 20  se name or NULL 
28d00 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
28d01 2a 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20  *zTableName,    
28d02 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a   /* Table name *
28d03 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
28d04 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20 20 20  zColumnName,    
28d05 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a  /* Column name *
28d06 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  /.  char const *
28d07 2a 70 7a 44 61 74 61 54 79 70 65 2c 20 20 20 20  *pzDataType,    
28d08 2f 2a 20 4f 55 54 50 55 54 3a 20 44 65 63 6c 61  /* OUTPUT: Decla
28d09 72 65 64 20 64 61 74 61 20 74 79 70 65 20 2a 2f  red data type */
28d0a 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a  .  char const **
28d0b 70 7a 43 6f 6c 6c 53 65 71 2c 20 20 20 20 20 2f  pzCollSeq,     /
28d0c 2a 20 4f 55 54 50 55 54 3a 20 43 6f 6c 6c 61 74  * OUTPUT: Collat
28d0d 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d  ion sequence nam
28d0e 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e 6f 74  e */.  int *pNot
28d0f 4e 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20  Null,           
28d10 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72     /* OUTPUT: Tr
28d11 75 65 20 69 66 20 4e 4f 54 20 4e 55 4c 4c 20 63  ue if NOT NULL c
28d12 6f 6e 73 74 72 61 69 6e 74 20 65 78 69 73 74 73  onstraint exists
28d13 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 50 72 69 6d   */.  int *pPrim
28d14 61 72 79 4b 65 79 2c 20 20 20 20 20 20 20 20 20  aryKey,         
28d15 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75    /* OUTPUT: Tru
28d16 65 20 69 66 20 63 6f 6c 75 6d 6e 20 70 61 72 74  e if column part
28d17 20 6f 66 20 50 4b 20 2a 2f 0a 20 20 69 6e 74 20   of PK */.  int 
28d18 2a 70 41 75 74 6f 69 6e 63 20 20 20 20 20 20 20  *pAutoinc       
28d19 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55          /* OUTPU
28d1a 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d  T: True if colum
28d1b 73 20 69 73 20 61 75 74 6f 2d 69 6e 63 72 65 6d  s is auto-increm
28d1c 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ent */.){.  int 
28d1d 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72  rc;.  char *zErr
28d1e 4d 73 67 20 3d 20 30 3b 0a 20 20 54 61 62 6c 65  Msg = 0;.  Table
28d1f 20 2a 70 54 61 62 20 3d 20 30 3b 0a 20 20 43 6f   *pTab = 0;.  Co
28d20 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 30 3b 0a  lumn *pCol = 0;.
28d21 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 0a 20 20 63    int iCol;..  c
28d22 68 61 72 20 63 6f 6e 73 74 20 2a 7a 44 61 74 61  har const *zData
28d23 54 79 70 65 20 3d 20 30 3b 0a 20 20 63 68 61 72  Type = 0;.  char
28d24 20 63 6f 6e 73 74 20 2a 7a 43 6f 6c 6c 53 65 71   const *zCollSeq
28d25 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 6f 74 6e   = 0;.  int notn
28d26 75 6c 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 70  ull = 0;.  int p
28d27 72 69 6d 61 72 79 6b 65 79 20 3d 20 30 3b 0a 20  rimarykey = 0;. 
28d28 20 69 6e 74 20 61 75 74 6f 69 6e 63 20 3d 20 30   int autoinc = 0
28d29 3b 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 74  ;..  /* Ensure t
28d2a 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
28d2b 6d 61 20 68 61 73 20 62 65 65 6e 20 6c 6f 61 64  ma has been load
28d2c 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ed */.  sqlite3_
28d2d 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
28d2e 6d 75 74 65 78 29 3b 0a 20 20 28 76 6f 69 64 29  mutex);.  (void)
28d2f 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
28d30 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  db);.  sqlite3Bt
28d31 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b  reeEnterAll(db);
28d32 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49  .  rc = sqlite3I
28d33 6e 69 74 28 64 62 2c 20 26 7a 45 72 72 4d 73 67  nit(db, &zErrMsg
28d34 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
28d35 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20  eLeaveAll(db);. 
28d36 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
28d37 72 63 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65  rc ){.    goto e
28d38 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20  rror_out;.  }.. 
28d39 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74   /* Locate the t
28d3a 61 62 6c 65 20 69 6e 20 71 75 65 73 74 69 6f 6e  able in question
28d3b 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 73 71 6c   */.  pTab = sql
28d3c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62  ite3FindTable(db
28d3d 2c 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 7a 44  , zTableName, zD
28d3e 62 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 70  bName);.  if( !p
28d3f 54 61 62 20 7c 7c 20 70 54 61 62 2d 3e 70 53 65  Tab || pTab->pSe
28d40 6c 65 63 74 20 29 7b 0a 20 20 20 20 70 54 61 62  lect ){.    pTab
28d41 20 3d 20 30 3b 0a 20 20 20 20 67 6f 74 6f 20 65   = 0;.    goto e
28d42 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20  rror_out;.  }.. 
28d43 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 63 6f 6c   /* Find the col
28d44 75 6d 6e 20 66 6f 72 20 77 68 69 63 68 20 69 6e  umn for which in
28d45 66 6f 20 69 73 20 72 65 71 75 65 73 74 65 64 20  fo is requested 
28d46 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
28d47 49 73 52 6f 77 69 64 28 7a 43 6f 6c 75 6d 6e 4e  IsRowid(zColumnN
28d48 61 6d 65 29 20 29 7b 0a 20 20 20 20 69 43 6f 6c  ame) ){.    iCol
28d49 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a   = pTab->iPKey;.
28d4a 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20      if( iCol>=0 
28d4b 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 20 3d 20  ){.      pCol = 
28d4c 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c  &pTab->aCol[iCol
28d4d 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  ];.    }.  }else
28d4e 7b 0a 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30  {.    for(iCol=0
28d4f 3b 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f  ; iCol<pTab->nCo
28d50 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20  l; iCol++){.    
28d51 20 20 70 43 6f 6c 20 3d 20 26 70 54 61 62 2d 3e    pCol = &pTab->
28d52 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20  aCol[iCol];.    
28d53 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33    if( 0==sqlite3
28d54 53 74 72 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e  StrICmp(pCol->zN
28d55 61 6d 65 2c 20 7a 43 6f 6c 75 6d 6e 4e 61 6d 65  ame, zColumnName
28d56 29 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65  ) ){.        bre
28d57 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
28d58 7d 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d  }.    if( iCol==
28d59 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20  pTab->nCol ){.  
28d5a 20 20 20 20 70 54 61 62 20 3d 20 30 3b 0a 20 20      pTab = 0;.  
28d5b 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f      goto error_o
28d5c 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ut;.    }.  }.. 
28d5d 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   /* The followin
28d5e 67 20 62 6c 6f 63 6b 20 73 74 6f 72 65 73 20 74  g block stores t
28d5f 68 65 20 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74  he meta informat
28d60 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 62 65  ion that will be
28d61 20 72 65 74 75 72 6e 65 64 0a 20 20 2a 2a 20 74   returned.  ** t
28d62 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 69 6e 20  o the caller in 
28d63 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
28d64 7a 44 61 74 61 54 79 70 65 2c 20 7a 43 6f 6c 6c  zDataType, zColl
28d65 53 65 71 2c 20 6e 6f 74 6e 75 6c 6c 2c 20 70 72  Seq, notnull, pr
28d66 69 6d 61 72 79 6b 65 79 0a 20 20 2a 2a 20 61 6e  imarykey.  ** an
28d67 64 20 61 75 74 6f 69 6e 63 2e 20 41 74 20 74 68  d autoinc. At th
28d68 69 73 20 70 6f 69 6e 74 20 74 68 65 72 65 20 61  is point there a
28d69 72 65 20 74 77 6f 20 70 6f 73 73 69 62 69 6c 69  re two possibili
28d6a 74 69 65 73 3a 0a 20 20 2a 2a 20 0a 20 20 2a 2a  ties:.  ** .  **
28d6b 20 20 20 20 20 31 2e 20 54 68 65 20 73 70 65 63       1. The spec
28d6c 69 66 69 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d  ified column nam
28d6d 65 20 77 61 73 20 72 6f 77 69 64 22 2c 20 22 6f  e was rowid", "o
28d6e 69 64 22 20 6f 72 20 22 5f 72 6f 77 69 64 5f 22  id" or "_rowid_"
28d6f 20 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 61 6e   .  **        an
28d70 64 20 74 68 65 72 65 20 69 73 20 6e 6f 20 65 78  d there is no ex
28d71 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65  plicitly declare
28d72 64 20 49 50 4b 20 63 6f 6c 75 6d 6e 2e 20 0a 20  d IPK column. . 
28d73 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 32 2e 20   **.  **     2. 
28d74 54 68 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74  The table is not
28d75 20 61 20 76 69 65 77 20 61 6e 64 20 74 68 65 20   a view and the 
28d76 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 69 64 65 6e  column name iden
28d77 74 69 66 69 65 64 20 61 6e 20 0a 20 20 2a 2a 20  tified an .  ** 
28d78 20 20 20 20 20 20 20 65 78 70 6c 69 63 69 74 6c         explicitl
28d79 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d  y declared colum
28d7a 6e 2e 20 43 6f 70 79 20 6d 65 74 61 20 69 6e 66  n. Copy meta inf
28d7b 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 2a 70  ormation from *p
28d7c 43 6f 6c 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 28  Col..  */ .  if(
28d7d 20 70 43 6f 6c 20 29 7b 0a 20 20 20 20 7a 44 61   pCol ){.    zDa
28d7e 74 61 54 79 70 65 20 3d 20 70 43 6f 6c 2d 3e 7a  taType = pCol->z
28d7f 54 79 70 65 3b 0a 20 20 20 20 7a 43 6f 6c 6c 53  Type;.    zCollS
28d80 65 71 20 3d 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c  eq = pCol->zColl
28d81 3b 0a 20 20 20 20 6e 6f 74 6e 75 6c 6c 20 3d 20  ;.    notnull = 
28d82 28 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c 3f 31  (pCol->notNull?1
28d83 3a 30 29 3b 0a 20 20 20 20 70 72 69 6d 61 72 79  :0);.    primary
28d84 6b 65 79 20 20 3d 20 28 70 43 6f 6c 2d 3e 69 73  key  = (pCol->is
28d85 50 72 69 6d 4b 65 79 3f 31 3a 30 29 3b 0a 20 20  PrimKey?1:0);.  
28d86 20 20 61 75 74 6f 69 6e 63 20 3d 20 28 28 70 54    autoinc = ((pT
28d87 61 62 2d 3e 69 50 4b 65 79 3d 3d 69 43 6f 6c 20  ab->iPKey==iCol 
28d88 26 26 20 70 54 61 62 2d 3e 61 75 74 6f 49 6e 63  && pTab->autoInc
28d89 29 3f 31 3a 30 29 3b 0a 20 20 7d 65 6c 73 65 7b  )?1:0);.  }else{
28d8a 0a 20 20 20 20 7a 44 61 74 61 54 79 70 65 20 3d  .    zDataType =
28d8b 20 22 49 4e 54 45 47 45 52 22 3b 0a 20 20 20 20   "INTEGER";.    
28d8c 70 72 69 6d 61 72 79 6b 65 79 20 3d 20 31 3b 0a  primarykey = 1;.
28d8d 20 20 7d 0a 20 20 69 66 28 20 21 7a 43 6f 6c 6c    }.  if( !zColl
28d8e 53 65 71 20 29 7b 0a 20 20 20 20 7a 43 6f 6c 6c  Seq ){.    zColl
28d8f 53 65 71 20 3d 20 22 42 49 4e 41 52 59 22 3b 0a  Seq = "BINARY";.
28d90 20 20 7d 0a 0a 65 72 72 6f 72 5f 6f 75 74 3a 0a    }..error_out:.
28d91 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
28d92 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 0a 20  afetyOff(db);.. 
28d93 20 2f 2a 20 57 68 65 74 68 65 72 20 74 68 65 20   /* Whether the 
28d94 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 73 75  function call su
28d95 63 63 65 65 64 65 64 20 6f 72 20 66 61 69 6c 65  cceeded or faile
28d96 64 2c 20 73 65 74 20 74 68 65 20 6f 75 74 70 75  d, set the outpu
28d97 74 20 70 61 72 61 6d 65 74 65 72 73 0a 20 20 2a  t parameters.  *
28d98 2a 20 74 6f 20 77 68 61 74 65 76 65 72 20 74 68  * to whatever th
28d99 65 69 72 20 6c 6f 63 61 6c 20 63 6f 75 6e 74 65  eir local counte
28d9a 72 70 61 72 74 73 20 63 6f 6e 74 61 69 6e 2e 20  rparts contain. 
28d9b 49 66 20 61 6e 20 65 72 72 6f 72 20 64 69 64 20  If an error did 
28d9c 6f 63 63 75 72 2c 0a 20 20 2a 2a 20 74 68 69 73  occur,.  ** this
28d9d 20 68 61 73 20 74 68 65 20 65 66 66 65 63 74 20   has the effect 
28d9e 6f 66 20 7a 65 72 6f 69 6e 67 20 61 6c 6c 20 6f  of zeroing all o
28d9f 75 74 70 75 74 20 70 61 72 61 6d 65 74 65 72 73  utput parameters
28da0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 7a 44  ..  */.  if( pzD
28da1 61 74 61 54 79 70 65 20 29 20 2a 70 7a 44 61 74  ataType ) *pzDat
28da2 61 54 79 70 65 20 3d 20 7a 44 61 74 61 54 79 70  aType = zDataTyp
28da3 65 3b 0a 20 20 69 66 28 20 70 7a 43 6f 6c 6c 53  e;.  if( pzCollS
28da4 65 71 20 29 20 2a 70 7a 43 6f 6c 6c 53 65 71 20  eq ) *pzCollSeq 
28da5 3d 20 7a 43 6f 6c 6c 53 65 71 3b 0a 20 20 69 66  = zCollSeq;.  if
28da6 28 20 70 4e 6f 74 4e 75 6c 6c 20 29 20 2a 70 4e  ( pNotNull ) *pN
28da7 6f 74 4e 75 6c 6c 20 3d 20 6e 6f 74 6e 75 6c 6c  otNull = notnull
28da8 3b 0a 20 20 69 66 28 20 70 50 72 69 6d 61 72 79  ;.  if( pPrimary
28da9 4b 65 79 20 29 20 2a 70 50 72 69 6d 61 72 79 4b  Key ) *pPrimaryK
28daa 65 79 20 3d 20 70 72 69 6d 61 72 79 6b 65 79 3b  ey = primarykey;
28dab 0a 20 20 69 66 28 20 70 41 75 74 6f 69 6e 63 20  .  if( pAutoinc 
28dac 29 20 2a 70 41 75 74 6f 69 6e 63 20 3d 20 61 75  ) *pAutoinc = au
28dad 74 6f 69 6e 63 3b 0a 0a 20 20 69 66 28 20 53 51  toinc;..  if( SQ
28dae 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20 26 26 20 21  LITE_OK==rc && !
28daf 70 54 61 62 20 29 7b 0a 20 20 20 20 73 71 6c 69  pTab ){.    sqli
28db0 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 7a 45  te3SetString(&zE
28db1 72 72 4d 73 67 2c 20 22 6e 6f 20 73 75 63 68 20  rrMsg, "no such 
28db2 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 3a 20 22 2c  table column: ",
28db3 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 22 2e 22   zTableName, "."
28db4 2c 20 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 75  , .        zColu
28db5 6d 6e 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20  mnName, 0);.    
28db6 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
28db7 52 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  R;.  }.  sqlite3
28db8 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 28 7a  Error(db, rc, (z
28db9 45 72 72 4d 73 67 3f 22 25 73 22 3a 30 29 2c 20  ErrMsg?"%s":0), 
28dba 7a 45 72 72 4d 73 67 29 3b 0a 20 20 73 71 6c 69  zErrMsg);.  sqli
28dbb 74 65 33 5f 66 72 65 65 28 7a 45 72 72 4d 73 67  te3_free(zErrMsg
28dbc 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
28dbd 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29  3ApiExit(db, rc)
28dbe 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
28dbf 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
28dc0 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  x);.  return rc;
28dc1 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
28dc2 20 53 6c 65 65 70 20 66 6f 72 20 61 20 6c 69 74   Sleep for a lit
28dc3 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 65 74 75  tle while.  Retu
28dc4 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66  rn the amount of
28dc5 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a   time slept..*/.
28dc6 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
28dc7 71 6c 69 74 65 33 5f 73 6c 65 65 70 28 69 6e 74  qlite3_sleep(int
28dc8 20 6d 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f   ms){.  sqlite3_
28dc9 76 66 73 20 2a 70 56 66 73 3b 0a 20 20 69 6e 74  vfs *pVfs;.  int
28dca 20 72 63 3b 0a 20 20 70 56 66 73 20 3d 20 73 71   rc;.  pVfs = sq
28dcb 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 30  lite3_vfs_find(0
28dcc 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66 75  );..  /* This fu
28dcd 6e 63 74 69 6f 6e 20 77 6f 72 6b 73 20 69 6e 20  nction works in 
28dce 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 2c 20 62 75  milliseconds, bu
28dcf 74 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  t the underlying
28dd0 20 4f 73 53 6c 65 65 70 28 29 20 0a 20 20 2a 2a   OsSleep() .  **
28dd1 20 41 50 49 20 75 73 65 73 20 6d 69 63 72 6f 73   API uses micros
28dd2 65 63 6f 6e 64 73 2e 20 48 65 6e 63 65 20 74 68  econds. Hence th
28dd3 65 20 31 30 30 30 27 73 2e 0a 20 20 2a 2f 0a 20  e 1000's..  */. 
28dd4 20 72 63 20 3d 20 28 73 71 6c 69 74 65 33 4f 73   rc = (sqlite3Os
28dd5 53 6c 65 65 70 28 70 56 66 73 2c 20 31 30 30 30  Sleep(pVfs, 1000
28dd6 2a 6d 73 29 2f 31 30 30 30 29 3b 0a 20 20 72 65  *ms)/1000);.  re
28dd7 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
28dd8 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64 69 73 61  * Enable or disa
28dd9 62 6c 65 20 74 68 65 20 65 78 74 65 6e 64 65 64  ble the extended
28dda 20 72 65 73 75 6c 74 20 63 6f 64 65 73 2e 0a 2a   result codes..*
28ddb 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
28ddc 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65   sqlite3_extende
28ddd 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 73  d_result_codes(s
28dde 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
28ddf 6f 6e 6f 66 66 29 7b 0a 20 20 73 71 6c 69 74 65  onoff){.  sqlite
28de0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
28de1 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e  ->mutex);.  db->
28de2 65 72 72 4d 61 73 6b 20 3d 20 6f 6e 6f 66 66 20  errMask = onoff 
28de3 3f 20 30 78 66 66 66 66 66 66 66 66 20 3a 20 30  ? 0xffffffff : 0
28de4 78 66 66 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  xff;.  sqlite3_m
28de5 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
28de6 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
28de7 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
28de8 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78  .** Invoke the x
28de9 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68  FileControl meth
28dea 6f 64 20 6f 6e 20 61 20 70 61 72 74 69 63 75 6c  od on a particul
28deb 61 72 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a  ar database..*/.
28dec 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
28ded 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74  qlite3_file_cont
28dee 72 6f 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  rol(sqlite3 *db,
28def 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
28df0 4e 61 6d 65 2c 20 69 6e 74 20 6f 70 2c 20 76 6f  Name, int op, vo
28df1 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 69 6e 74  id *pArg){.  int
28df2 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
28df3 4f 52 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20  OR;.  int iDb;. 
28df4 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
28df5 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
28df6 0a 20 20 69 66 28 20 7a 44 62 4e 61 6d 65 3d 3d  .  if( zDbName==
28df7 30 20 29 7b 0a 20 20 20 20 69 44 62 20 3d 20 30  0 ){.    iDb = 0
28df8 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66  ;.  }else{.    f
28df9 6f 72 28 69 44 62 3d 30 3b 20 69 44 62 3c 64 62  or(iDb=0; iDb<db
28dfa 2d 3e 6e 44 62 3b 20 69 44 62 2b 2b 29 7b 0a 20  ->nDb; iDb++){. 
28dfb 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28       if( strcmp(
28dfc 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
28dfd 6d 65 2c 20 7a 44 62 4e 61 6d 65 29 3d 3d 30 20  me, zDbName)==0 
28dfe 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20  ) break;.    }. 
28dff 20 7d 0a 20 20 69 66 28 20 69 44 62 3c 64 62 2d   }.  if( iDb<db-
28e00 3e 6e 44 62 20 29 7b 0a 20 20 20 20 42 74 72 65  >nDb ){.    Btre
28e01 65 20 2a 70 42 74 72 65 65 20 3d 20 64 62 2d 3e  e *pBtree = db->
28e02 61 44 62 5b 69 44 62 5d 2e 70 42 74 3b 0a 20 20  aDb[iDb].pBt;.  
28e03 20 20 69 66 28 20 70 42 74 72 65 65 20 29 7b 0a    if( pBtree ){.
28e04 20 20 20 20 20 20 50 61 67 65 72 20 2a 70 50 61        Pager *pPa
28e05 67 65 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ger;.      sqlit
28e06 65 33 5f 66 69 6c 65 20 2a 66 64 3b 0a 20 20 20  e3_file *fd;.   
28e07 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45     sqlite3BtreeE
28e08 6e 74 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20  nter(pBtree);.  
28e09 20 20 20 20 70 50 61 67 65 72 20 3d 20 73 71 6c      pPager = sql
28e0a 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70  ite3BtreePager(p
28e0b 42 74 72 65 65 29 3b 0a 20 20 20 20 20 20 61 73  Btree);.      as
28e0c 73 65 72 74 28 20 70 50 61 67 65 72 21 3d 30 20  sert( pPager!=0 
28e0d 29 3b 0a 20 20 20 20 20 20 66 64 20 3d 20 73 71  );.      fd = sq
28e0e 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 70  lite3PagerFile(p
28e0f 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 61 73  Pager);.      as
28e10 73 65 72 74 28 20 66 64 21 3d 30 20 29 3b 0a 20  sert( fd!=0 );. 
28e11 20 20 20 20 20 69 66 28 20 66 64 2d 3e 70 4d 65       if( fd->pMe
28e12 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 20 20 20  thods ){.       
28e13 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46   rc = sqlite3OsF
28e14 69 6c 65 43 6f 6e 74 72 6f 6c 28 66 64 2c 20 6f  ileControl(fd, o
28e15 70 2c 20 70 41 72 67 29 3b 0a 20 20 20 20 20 20  p, pArg);.      
28e16 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  }.      sqlite3B
28e17 74 72 65 65 4c 65 61 76 65 28 70 42 74 72 65 65  treeLeave(pBtree
28e18 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  );.    }.  }.  s
28e19 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
28e1a 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
28e1b 20 72 65 74 75 72 6e 20 72 63 3b 20 20 20 0a 7d   return rc;   .}
28e1c 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63  ../*.** Interfac
28e1d 65 20 74 6f 20 74 68 65 20 74 65 73 74 69 6e 67  e to the testing
28e1e 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 53 51 4c 49 54   logic..*/.SQLIT
28e1f 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
28e20 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 69  3_test_control(i
28e21 6e 74 20 6f 70 2c 20 2e 2e 2e 29 7b 0a 20 20 69  nt op, ...){.  i
28e22 6e 74 20 72 63 20 3d 20 30 3b 0a 23 69 66 6e 64  nt rc = 0;.#ifnd
28e23 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  ef SQLITE_OMIT_B
28e24 55 49 4c 54 49 4e 5f 54 45 53 54 0a 20 20 76 61  UILTIN_TEST.  va
28e25 5f 6c 69 73 74 20 61 70 3b 0a 20 20 76 61 5f 73  _list ap;.  va_s
28e26 74 61 72 74 28 61 70 2c 20 6f 70 29 3b 0a 20 20  tart(ap, op);.  
28e27 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20  switch( op ){.  
28e28 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 73 71 6c 69    /*.    ** sqli
28e29 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c  te3_test_control
28e2a 28 46 41 55 4c 54 5f 43 4f 4e 46 49 47 2c 20 66  (FAULT_CONFIG, f
28e2b 61 75 6c 74 5f 69 64 2c 20 6e 44 65 6c 61 79 2c  ault_id, nDelay,
28e2c 20 6e 52 65 70 65 61 74 29 0a 20 20 20 20 2a 2a   nRepeat).    **
28e2d 0a 20 20 20 20 2a 2a 20 43 6f 6e 66 69 67 75 72  .    ** Configur
28e2e 65 20 61 20 66 61 75 6c 74 20 69 6e 6a 65 63 74  e a fault inject
28e2f 6f 72 2e 20 20 54 68 65 20 73 70 65 63 69 66 69  or.  The specifi
28e30 63 20 66 61 75 6c 74 20 69 6e 6a 65 63 74 6f 72  c fault injector
28e31 20 69 73 0a 20 20 20 20 2a 2a 20 69 64 65 6e 74   is.    ** ident
28e32 69 66 69 65 64 20 62 79 20 74 68 65 20 66 61 75  ified by the fau
28e33 6c 74 5f 69 64 20 61 72 67 75 6d 65 6e 74 2e 20  lt_id argument. 
28e34 20 28 65 78 3a 20 53 51 4c 49 54 45 5f 46 41 55   (ex: SQLITE_FAU
28e35 4c 54 49 4e 4a 45 43 54 4f 52 5f 4d 41 4c 4c 4f  LTINJECTOR_MALLO
28e36 43 29 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 61  C).    ** The fa
28e37 75 6c 74 20 77 69 6c 6c 20 6f 63 63 75 72 20 61  ult will occur a
28e38 66 74 65 72 20 61 20 64 65 6c 61 79 20 6f 66 20  fter a delay of 
28e39 6e 44 65 6c 61 79 20 63 61 6c 6c 73 2e 20 20 54  nDelay calls.  T
28e3a 68 65 20 66 61 75 6c 74 0a 20 20 20 20 2a 2a 20  he fault.    ** 
28e3b 77 69 6c 6c 20 72 65 70 65 61 74 20 6e 52 65 70  will repeat nRep
28e3c 65 61 74 20 74 69 6d 65 73 2e 0a 20 20 20 20 2a  eat times..    *
28e3d 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  /.    case SQLIT
28e3e 45 5f 54 45 53 54 43 54 52 4c 5f 46 41 55 4c 54  E_TESTCTRL_FAULT
28e3f 5f 43 4f 4e 46 49 47 3a 20 7b 0a 20 20 20 20 20  _CONFIG: {.     
28e40 20 69 6e 74 20 69 64 20 3d 20 76 61 5f 61 72 67   int id = va_arg
28e41 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20  (ap, int);.     
28e42 20 69 6e 74 20 6e 44 65 6c 61 79 20 3d 20 76 61   int nDelay = va
28e43 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20  _arg(ap, int);. 
28e44 20 20 20 20 20 69 6e 74 20 6e 52 65 70 65 61 74       int nRepeat
28e45 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
28e46 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
28e47 33 46 61 75 6c 74 43 6f 6e 66 69 67 28 69 64 2c  3FaultConfig(id,
28e48 20 6e 44 65 6c 61 79 2c 20 6e 52 65 70 65 61 74   nDelay, nRepeat
28e49 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
28e4a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20      }..    /*.  
28e4b 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 74 65 73    ** sqlite3_tes
28e4c 74 5f 63 6f 6e 74 72 6f 6c 28 46 41 55 4c 54 5f  t_control(FAULT_
28e4d 46 41 49 4c 55 52 45 53 2c 20 66 61 75 6c 74 5f  FAILURES, fault_
28e4e 69 64 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  id).    **.    *
28e4f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
28e50 62 65 72 20 6f 66 20 66 61 75 6c 74 73 20 28 62  ber of faults (b
28e51 6f 74 68 20 68 61 72 64 20 61 6e 64 20 62 65 6e  oth hard and ben
28e52 69 67 6e 20 66 61 75 6c 74 73 29 20 74 68 61 74  ign faults) that
28e53 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 6f 63 63   have.    ** occ
28e54 75 72 72 65 64 20 73 69 6e 63 65 20 74 68 65 20  urred since the 
28e55 69 6e 6a 65 63 74 6f 72 20 69 64 65 6e 74 69 66  injector identif
28e56 69 65 64 20 62 79 20 66 61 75 6c 74 5f 69 64 29  ied by fault_id)
28e57 20 77 61 73 20 6c 61 73 74 20 63 6f 6e 66 69 67   was last config
28e58 75 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ured..    */.   
28e59 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53   case SQLITE_TES
28e5a 54 43 54 52 4c 5f 46 41 55 4c 54 5f 46 41 49 4c  TCTRL_FAULT_FAIL
28e5b 55 52 45 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e  URES: {.      in
28e5c 74 20 69 64 20 3d 20 76 61 5f 61 72 67 28 61 70  t id = va_arg(ap
28e5d 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 72 63  , int);.      rc
28e5e 20 3d 20 73 71 6c 69 74 65 33 46 61 75 6c 74 46   = sqlite3FaultF
28e5f 61 69 6c 75 72 65 73 28 69 64 29 3b 0a 20 20 20  ailures(id);.   
28e60 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
28e61 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 73  .    /*.    ** s
28e62 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74  qlite3_test_cont
28e63 72 6f 6c 28 46 41 55 4c 54 5f 42 45 4e 49 47 4e  rol(FAULT_BENIGN
28e64 5f 46 41 49 4c 55 52 45 53 2c 20 66 61 75 6c 74  _FAILURES, fault
28e65 5f 69 64 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20  _id).    **.    
28e66 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
28e67 6d 62 65 72 20 6f 66 20 62 65 6e 69 67 6e 20 66  mber of benign f
28e68 61 75 6c 74 73 20 74 68 61 74 20 68 61 76 65 20  aults that have 
28e69 6f 63 63 75 72 72 65 64 20 73 69 6e 63 65 20 74  occurred since t
28e6a 68 65 0a 20 20 20 20 2a 2a 20 69 6e 6a 65 63 74  he.    ** inject
28e6b 6f 72 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  or identified by
28e6c 20 66 61 75 6c 74 5f 69 64 20 77 61 73 20 6c 61   fault_id was la
28e6d 73 74 20 63 6f 6e 66 69 67 75 72 65 64 2e 0a 20  st configured.. 
28e6e 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53     */.    case S
28e6f 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 46  QLITE_TESTCTRL_F
28e70 41 55 4c 54 5f 42 45 4e 49 47 4e 5f 46 41 49 4c  AULT_BENIGN_FAIL
28e71 55 52 45 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e  URES: {.      in
28e72 74 20 69 64 20 3d 20 76 61 5f 61 72 67 28 61 70  t id = va_arg(ap
28e73 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 72 63  , int);.      rc
28e74 20 3d 20 73 71 6c 69 74 65 33 46 61 75 6c 74 42   = sqlite3FaultB
28e75 65 6e 69 67 6e 46 61 69 6c 75 72 65 73 28 69 64  enignFailures(id
28e76 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
28e77 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20      }..    /*.  
28e78 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 74 65 73    ** sqlite3_tes
28e79 74 5f 63 6f 6e 74 72 6f 6c 28 46 41 55 4c 54 5f  t_control(FAULT_
28e7a 50 45 4e 44 49 4e 47 2c 20 66 61 75 6c 74 5f 69  PENDING, fault_i
28e7b 64 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  d).    **.    **
28e7c 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
28e7d 65 72 20 6f 66 20 73 75 63 63 65 73 73 65 73 20  er of successes 
28e7e 74 68 61 74 20 77 69 6c 6c 20 6f 63 63 75 72 20  that will occur 
28e7f 62 65 66 6f 72 65 20 74 68 65 20 6e 65 78 74 0a  before the next.
28e80 20 20 20 20 2a 2a 20 73 63 68 65 64 75 6c 65 64      ** scheduled
28e81 20 66 61 69 6c 75 72 65 20 6f 6e 20 66 61 75 6c   failure on faul
28e82 74 20 69 6e 6a 65 63 74 6f 72 20 66 61 75 6c 74  t injector fault
28e83 5f 69 64 2e 0a 20 20 20 20 2a 2a 20 49 66 20 6e  _id..    ** If n
28e84 6f 20 66 61 69 6c 75 72 65 73 20 61 72 65 20 73  o failures are s
28e85 63 68 65 64 75 6c 65 64 2c 20 72 65 74 75 72 6e  cheduled, return
28e86 20 2d 31 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20   -1..    */.    
28e87 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54  case SQLITE_TEST
28e88 43 54 52 4c 5f 46 41 55 4c 54 5f 50 45 4e 44 49  CTRL_FAULT_PENDI
28e89 4e 47 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  NG: {.      int 
28e8a 69 64 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  id = va_arg(ap, 
28e8b 69 6e 74 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  int);.      rc =
28e8c 20 73 71 6c 69 74 65 33 46 61 75 6c 74 50 65 6e   sqlite3FaultPen
28e8d 64 69 6e 67 28 69 64 29 3b 0a 20 20 20 20 20 20  ding(id);.      
28e8e 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
28e8f 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 53 61 76 65    /*.    ** Save
28e90 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61   the current sta
28e91 74 65 20 6f 66 20 74 68 65 20 50 52 4e 47 2e 0a  te of the PRNG..
28e92 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
28e93 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
28e94 50 52 4e 47 5f 53 41 56 45 3a 20 7b 0a 20 20 20  PRNG_SAVE: {.   
28e95 20 20 20 73 71 6c 69 74 65 33 50 72 6e 67 53 61     sqlite3PrngSa
28e96 76 65 53 74 61 74 65 28 29 3b 0a 20 20 20 20 20  veState();.     
28e97 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
28e98 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 52 65 73     /*.    ** Res
28e99 74 6f 72 65 20 74 68 65 20 73 74 61 74 65 20 6f  tore the state o
28e9a 66 20 74 68 65 20 50 52 4e 47 20 74 6f 20 74 68  f the PRNG to th
28e9b 65 20 6c 61 73 74 20 73 74 61 74 65 20 73 61 76  e last state sav
28e9c 65 64 20 75 73 69 6e 67 0a 20 20 20 20 2a 2a 20  ed using.    ** 
28e9d 50 52 4e 47 5f 53 41 56 45 2e 20 20 49 66 20 50  PRNG_SAVE.  If P
28e9e 52 4e 47 5f 53 41 56 45 20 68 61 73 20 6e 65 76  RNG_SAVE has nev
28e9f 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e 20 63  er before been c
28ea0 61 6c 6c 65 64 2c 20 74 68 65 6e 0a 20 20 20 20  alled, then.    
28ea1 2a 2a 20 74 68 69 73 20 76 65 72 62 20 61 63 74  ** this verb act
28ea2 73 20 6c 69 6b 65 20 50 52 4e 47 5f 52 45 53 45  s like PRNG_RESE
28ea3 54 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  T..    */.    ca
28ea4 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54  se SQLITE_TESTCT
28ea5 52 4c 5f 50 52 4e 47 5f 52 45 53 54 4f 52 45 3a  RL_PRNG_RESTORE:
28ea6 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
28ea7 50 72 6e 67 52 65 73 74 6f 72 65 53 74 61 74 65  PrngRestoreState
28ea8 28 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ();.      break;
28ea9 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20  .    }..    /*. 
28eaa 20 20 20 2a 2a 20 52 65 73 65 74 20 74 68 65 20     ** Reset the 
28eab 50 52 4e 47 20 62 61 63 6b 20 74 6f 20 69 74 73  PRNG back to its
28eac 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 73   uninitialized s
28ead 74 61 74 65 2e 20 20 54 68 65 20 6e 65 78 74 20  tate.  The next 
28eae 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 73  call.    ** to s
28eaf 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73  qlite3_randomnes
28eb0 73 28 29 20 77 69 6c 6c 20 72 65 73 65 65 64 20  s() will reseed 
28eb1 74 68 65 20 50 52 4e 47 20 75 73 69 6e 67 20 61  the PRNG using a
28eb2 20 73 69 6e 67 6c 65 20 63 61 6c 6c 0a 20 20 20   single call.   
28eb3 20 2a 2a 20 74 6f 20 74 68 65 20 78 52 61 6e 64   ** to the xRand
28eb4 6f 6d 6e 65 73 73 20 6d 65 74 68 6f 64 20 6f 66  omness method of
28eb5 20 74 68 65 20 64 65 66 61 75 6c 74 20 56 46 53   the default VFS
28eb6 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  ..    */.    cas
28eb7 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52  e SQLITE_TESTCTR
28eb8 4c 5f 50 52 4e 47 5f 52 45 53 45 54 3a 20 7b 0a  L_PRNG_RESET: {.
28eb9 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 72 6e        sqlite3Prn
28eba 67 52 65 73 65 74 53 74 61 74 65 28 29 3b 0a 20  gResetState();. 
28ebb 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
28ebc 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a  }..    /*.    **
28ebd 20 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63    sqlite3_test_c
28ebe 6f 6e 74 72 6f 6c 28 42 49 54 56 45 43 5f 54 45  ontrol(BITVEC_TE
28ebf 53 54 2c 20 73 69 7a 65 2c 20 70 72 6f 67 72 61  ST, size, progra
28ec0 6d 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  m).    **.    **
28ec1 20 52 75 6e 20 61 20 74 65 73 74 20 61 67 61 69   Run a test agai
28ec2 6e 73 74 20 61 20 42 69 74 76 65 63 20 6f 62 6a  nst a Bitvec obj
28ec3 65 63 74 20 6f 66 20 73 69 7a 65 2e 20 20 54 68  ect of size.  Th
28ec4 65 20 70 72 6f 67 72 61 6d 20 61 72 67 75 6d 65  e program argume
28ec5 6e 74 0a 20 20 20 20 2a 2a 20 69 73 20 61 6e 20  nt.    ** is an 
28ec6 61 72 72 61 79 20 6f 66 20 69 6e 74 65 67 65 72  array of integer
28ec7 73 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74  s that defines t
28ec8 68 65 20 74 65 73 74 2e 20 20 52 65 74 75 72 6e  he test.  Return
28ec9 20 2d 31 20 6f 6e 20 61 0a 20 20 20 20 2a 2a 20   -1 on a.    ** 
28eca 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
28ecb 6e 20 65 72 72 6f 72 2c 20 30 20 6f 6e 20 73 75  n error, 0 on su
28ecc 63 63 65 73 73 2c 20 6f 72 20 6e 6f 6e 2d 7a 65  ccess, or non-ze
28ecd 72 6f 20 66 6f 72 20 61 6e 20 65 72 72 6f 72 2e  ro for an error.
28ece 0a 20 20 20 20 2a 2a 20 53 65 65 20 74 68 65 20  .    ** See the 
28ecf 73 71 6c 69 74 65 33 42 69 74 76 65 63 42 75 69  sqlite3BitvecBui
28ed0 6c 74 69 6e 54 65 73 74 28 29 20 66 6f 72 20 61  ltinTest() for a
28ed1 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
28ed2 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20  ation..    */.  
28ed3 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45    case SQLITE_TE
28ed4 53 54 43 54 52 4c 5f 42 49 54 56 45 43 5f 54 45  STCTRL_BITVEC_TE
28ed5 53 54 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  ST: {.      int 
28ed6 73 7a 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  sz = va_arg(ap, 
28ed7 69 6e 74 29 3b 0a 20 20 20 20 20 20 69 6e 74 20  int);.      int 
28ed8 2a 61 50 72 6f 67 20 3d 20 76 61 5f 61 72 67 28  *aProg = va_arg(
28ed9 61 70 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20 20  ap, int*);.     
28eda 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74   rc = sqlite3Bit
28edb 76 65 63 42 75 69 6c 74 69 6e 54 65 73 74 28 73  vecBuiltinTest(s
28edc 7a 2c 20 61 50 72 6f 67 29 3b 0a 20 20 20 20 20  z, aProg);.     
28edd 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
28ede 7d 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a  }.  va_end(ap);.
28edf 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
28ee0 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45  _OMIT_BUILTIN_TE
28ee1 53 54 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72  ST */.  return r
28ee2 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  c;.}../*********
28ee3 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 61 69  ***** End of mai
28ee4 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
28ee5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ee6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ee7 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
28ee8 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
28ee9 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   fts3.c ********
28eea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28eeb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28eec 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36  ****/./*.** 2006
28eed 20 4f 63 74 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68   Oct 10.**.** Th
28eee 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
28eef 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
28ef0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
28ef1 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
28ef2 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
28ef3 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
28ef4 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
28ef5 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
28ef6 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
28ef7 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
28ef8 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
28ef9 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
28efa 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
28efb 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
28efc 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
28efd 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
28efe 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
28eff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28f01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28f02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28f03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
28f04 2a 20 54 68 69 73 20 69 73 20 61 6e 20 53 51 4c  * This is an SQL
28f05 69 74 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65  ite module imple
28f06 6d 65 6e 74 69 6e 67 20 66 75 6c 6c 2d 74 65 78  menting full-tex
28f07 74 20 73 65 61 72 63 68 2e 0a 2a 2f 0a 0a 2f 2a  t search..*/../*
28f08 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20  .** The code in 
28f09 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c  this file is onl
28f0a 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a  y compiled if:.*
28f0b 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46  *.**     * The F
28f0c 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65  TS3 module is be
28f0d 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61 6e 20  ing built as an 
28f0e 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20  extension.**    
28f0f 20 20 20 28 69 6e 20 77 68 69 63 68 20 63 61 73     (in which cas
28f10 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 69 73  e SQLITE_CORE is
28f11 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 6f   not defined), o
28f12 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68  r.**.**     * Th
28f13 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73  e FTS3 module is
28f14 20 62 65 69 6e 67 20 62 75 69 6c 74 20 69 6e 74   being built int
28f15 6f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a  o the core of.**
28f16 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 28 69         SQLite (i
28f17 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 51 4c  n which case SQL
28f18 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20  ITE_ENABLE_FTS3 
28f19 69 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a  is defined)..*/.
28f1a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
28f1b 43 6f 6e 73 69 64 65 72 20 65 78 70 6f 72 74 69  Consider exporti
28f1c 6e 67 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 20  ng this comment 
28f1d 74 6f 20 61 6e 20 48 54 4d 4c 20 66 69 6c 65 20  to an HTML file 
28f1e 6f 72 20 74 68 65 0a 2a 2a 20 77 69 6b 69 2e 0a  or the.** wiki..
28f1f 2a 2f 0a 2f 2a 20 54 68 65 20 66 75 6c 6c 2d 74  */./* The full-t
28f20 65 78 74 20 69 6e 64 65 78 20 69 73 20 73 74 6f  ext index is sto
28f21 72 65 64 20 69 6e 20 61 20 73 65 72 69 65 73 20  red in a series 
28f22 6f 66 20 62 2b 74 72 65 65 20 28 2d 6c 69 6b 65  of b+tree (-like
28f23 29 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20  ).** structures 
28f24 63 61 6c 6c 65 64 20 73 65 67 6d 65 6e 74 73 20  called segments 
28f25 77 68 69 63 68 20 6d 61 70 20 74 65 72 6d 73 20  which map terms 
28f26 74 6f 20 64 6f 63 6c 69 73 74 73 2e 20 20 54 68  to doclists.  Th
28f27 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20  e.** structures 
28f28 61 72 65 20 6c 69 6b 65 20 62 2b 74 72 65 65 73  are like b+trees
28f29 20 69 6e 20 6c 61 79 6f 75 74 2c 20 62 75 74 20   in layout, but 
28f2a 61 72 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20  are constructed 
28f2b 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 62 6f 74 74  from the.** bott
28f2c 6f 6d 20 75 70 20 69 6e 20 6f 70 74 69 6d 61 6c  om up in optimal
28f2d 20 66 61 73 68 69 6f 6e 20 61 6e 64 20 61 72 65   fashion and are
28f2e 20 6e 6f 74 20 75 70 64 61 74 61 62 6c 65 2e 20   not updatable. 
28f2f 20 53 69 6e 63 65 20 74 72 65 65 73 0a 2a 2a 20   Since trees.** 
28f30 61 72 65 20 62 75 69 6c 74 20 66 72 6f 6d 20 74  are built from t
28f31 68 65 20 62 6f 74 74 6f 6d 20 75 70 2c 20 74 68  he bottom up, th
28f32 69 6e 67 73 20 77 69 6c 6c 20 62 65 20 64 65 73  ings will be des
28f33 63 72 69 62 65 64 20 66 72 6f 6d 20 74 68 65 0a  cribed from the.
28f34 2a 2a 20 62 6f 74 74 6f 6d 20 75 70 2e 0a 2a 2a  ** bottom up..**
28f35 0a 2a 2a 0a 2a 2a 2a 2a 20 56 61 72 69 6e 74 73  .**.**** Varints
28f36 20 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 73   ****.** The bas
28f37 69 63 20 75 6e 69 74 20 6f 66 20 65 6e 63 6f 64  ic unit of encod
28f38 69 6e 67 20 69 73 20 61 20 76 61 72 69 61 62 6c  ing is a variabl
28f39 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e-length integer
28f3a 20 63 61 6c 6c 65 64 20 61 0a 2a 2a 20 76 61 72   called a.** var
28f3b 69 6e 74 2e 20 20 57 65 20 65 6e 63 6f 64 65 20  int.  We encode 
28f3c 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20  variable-length 
28f3d 69 6e 74 65 67 65 72 73 20 69 6e 20 6c 69 74 74  integers in litt
28f3e 6c 65 2d 65 6e 64 69 61 6e 20 6f 72 64 65 72 0a  le-endian order.
28f3f 2a 2a 20 75 73 69 6e 67 20 73 65 76 65 6e 20 62  ** using seven b
28f40 69 74 73 20 2a 20 70 65 72 20 62 79 74 65 20 61  its * per byte a
28f41 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
28f42 20 4b 45 59 3a 0a 2a 2a 20 20 20 20 20 20 20 20   KEY:.**        
28f43 20 41 20 3d 20 30 78 78 78 78 78 78 78 20 20 20   A = 0xxxxxxx   
28f44 20 37 20 62 69 74 73 20 6f 66 20 64 61 74 61 20   7 bits of data 
28f45 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74  and one flag bit
28f46 0a 2a 2a 20 20 20 20 20 20 20 20 20 42 20 3d 20  .**         B = 
28f47 31 78 78 78 78 78 78 78 20 20 20 20 37 20 62 69  1xxxxxxx    7 bi
28f48 74 73 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f  ts of data and o
28f49 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a 0a 2a  ne flag bit.**.*
28f4a 2a 20 20 37 20 62 69 74 73 20 2d 20 41 0a 2a 2a  *  7 bits - A.**
28f4b 20 31 34 20 62 69 74 73 20 2d 20 42 41 0a 2a 2a   14 bits - BA.**
28f4c 20 32 31 20 62 69 74 73 20 2d 20 42 42 41 0a 2a   21 bits - BBA.*
28f4d 2a 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a  * and so on..**.
28f4e 2a 2a 20 54 68 69 73 20 69 73 20 69 64 65 6e 74  ** This is ident
28f4f 69 63 61 6c 20 74 6f 20 68 6f 77 20 73 71 6c 69  ical to how sqli
28f50 74 65 20 65 6e 63 6f 64 65 73 20 76 61 72 69 6e  te encodes varin
28f51 74 73 20 28 73 65 65 20 75 74 69 6c 2e 63 29 2e  ts (see util.c).
28f52 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 44 6f 63 75  .**.**.**** Docu
28f53 6d 65 6e 74 20 6c 69 73 74 73 20 2a 2a 2a 2a 0a  ment lists ****.
28f54 2a 2a 20 41 20 64 6f 63 6c 69 73 74 20 28 64 6f  ** A doclist (do
28f55 63 75 6d 65 6e 74 20 6c 69 73 74 29 20 68 6f 6c  cument list) hol
28f56 64 73 20 61 20 64 6f 63 69 64 2d 73 6f 72 74 65  ds a docid-sorte
28f57 64 20 6c 69 73 74 20 6f 66 20 68 69 74 73 20 66  d list of hits f
28f58 6f 72 20 61 0a 2a 2a 20 67 69 76 65 6e 20 74 65  or a.** given te
28f59 72 6d 2e 20 20 44 6f 63 6c 69 73 74 73 20 68 6f  rm.  Doclists ho
28f5a 6c 64 20 64 6f 63 69 64 73 2c 20 61 6e 64 20 63  ld docids, and c
28f5b 61 6e 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 61 73  an optionally as
28f5c 73 6f 63 69 61 74 65 0a 2a 2a 20 74 6f 6b 65 6e  sociate.** token
28f5d 20 70 6f 73 69 74 69 6f 6e 73 20 61 6e 64 20 6f   positions and o
28f5e 66 66 73 65 74 73 20 77 69 74 68 20 64 6f 63 69  ffsets with doci
28f5f 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 44 4c 5f 50  ds..**.** A DL_P
28f60 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53  OSITIONS_OFFSETS
28f61 20 64 6f 63 6c 69 73 74 20 69 73 20 73 74 6f 72   doclist is stor
28f62 65 64 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  ed like this:.**
28f63 0a 2a 2a 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20  .** array {.**  
28f64 20 76 61 72 69 6e 74 20 64 6f 63 69 64 3b 0a 2a   varint docid;.*
28f65 2a 20 20 20 61 72 72 61 79 20 7b 20 20 20 20 20  *   array {     
28f66 20 20 20 20 20 20 20 20 20 20 20 28 70 6f 73 69             (posi
28f67 74 69 6f 6e 20 6c 69 73 74 20 66 6f 72 20 63 6f  tion list for co
28f68 6c 75 6d 6e 20 30 29 0a 2a 2a 20 20 20 20 20 76  lumn 0).**     v
28f69 61 72 69 6e 74 20 70 6f 73 69 74 69 6f 6e 3b 20  arint position; 
28f6a 20 20 20 20 28 64 65 6c 74 61 20 66 72 6f 6d 20      (delta from 
28f6b 70 72 65 76 69 6f 75 73 20 70 6f 73 69 74 69 6f  previous positio
28f6c 6e 20 70 6c 75 73 20 50 4f 53 5f 42 41 53 45 29  n plus POS_BASE)
28f6d 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20 73  .**     varint s
28f6e 74 61 72 74 4f 66 66 73 65 74 3b 20 20 28 64 65  tartOffset;  (de
28f6f 6c 74 61 20 66 72 6f 6d 20 70 72 65 76 69 6f 75  lta from previou
28f70 73 20 73 74 61 72 74 4f 66 66 73 65 74 29 0a 2a  s startOffset).*
28f71 2a 20 20 20 20 20 76 61 72 69 6e 74 20 65 6e 64  *     varint end
28f72 4f 66 66 73 65 74 3b 20 20 20 20 28 64 65 6c 74  Offset;    (delt
28f73 61 20 66 72 6f 6d 20 73 74 61 72 74 4f 66 66 73  a from startOffs
28f74 65 74 29 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 20 20  et).**   }.**   
28f75 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20 20 20 76  array {.**     v
28f76 61 72 69 6e 74 20 50 4f 53 5f 43 4f 4c 55 4d 4e  arint POS_COLUMN
28f77 3b 20 20 20 28 6d 61 72 6b 73 20 73 74 61 72 74  ;   (marks start
28f78 20 6f 66 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73   of position lis
28f79 74 20 66 6f 72 20 6e 65 77 20 63 6f 6c 75 6d 6e  t for new column
28f7a 29 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20  ).**     varint 
28f7b 63 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 28 69  column;       (i
28f7c 6e 64 65 78 20 6f 66 20 6e 65 77 20 63 6f 6c 75  ndex of new colu
28f7d 6d 6e 29 0a 2a 2a 20 20 20 20 20 61 72 72 61 79  mn).**     array
28f7e 20 7b 0a 2a 2a 20 20 20 20 20 20 20 76 61 72 69   {.**       vari
28f7f 6e 74 20 70 6f 73 69 74 69 6f 6e 3b 20 20 20 28  nt position;   (
28f80 64 65 6c 74 61 20 66 72 6f 6d 20 70 72 65 76 69  delta from previ
28f81 6f 75 73 20 70 6f 73 69 74 69 6f 6e 20 70 6c 75  ous position plu
28f82 73 20 50 4f 53 5f 42 41 53 45 29 0a 2a 2a 20 20  s POS_BASE).**  
28f83 20 20 20 20 20 76 61 72 69 6e 74 20 73 74 61 72       varint star
28f84 74 4f 66 66 73 65 74 3b 28 64 65 6c 74 61 20 66  tOffset;(delta f
28f85 72 6f 6d 20 70 72 65 76 69 6f 75 73 20 73 74 61  rom previous sta
28f86 72 74 4f 66 66 73 65 74 29 0a 2a 2a 20 20 20 20  rtOffset).**    
28f87 20 20 20 76 61 72 69 6e 74 20 65 6e 64 4f 66 66     varint endOff
28f88 73 65 74 3b 20 20 28 64 65 6c 74 61 20 66 72 6f  set;  (delta fro
28f89 6d 20 73 74 61 72 74 4f 66 66 73 65 74 29 0a 2a  m startOffset).*
28f8a 2a 20 20 20 20 20 7d 0a 2a 2a 20 20 20 7d 0a 2a  *     }.**   }.*
28f8b 2a 20 20 20 76 61 72 69 6e 74 20 50 4f 53 5f 45  *   varint POS_E
28f8c 4e 44 3b 20 20 20 20 20 20 20 20 28 6d 61 72 6b  ND;        (mark
28f8d 73 20 65 6e 64 20 6f 66 20 70 6f 73 69 74 69 6f  s end of positio
28f8e 6e 73 20 66 6f 72 20 74 68 69 73 20 64 6f 63 75  ns for this docu
28f8f 6d 65 6e 74 2e 0a 2a 2a 20 7d 0a 2a 2a 0a 2a 2a  ment..** }.**.**
28f90 20 48 65 72 65 2c 20 61 72 72 61 79 20 7b 20 58   Here, array { X
28f91 20 7d 20 6d 65 61 6e 73 20 7a 65 72 6f 20 6f 72   } means zero or
28f92 20 6d 6f 72 65 20 6f 63 63 75 72 72 65 6e 63 65   more occurrence
28f93 73 20 6f 66 20 58 2c 20 61 64 6a 61 63 65 6e 74  s of X, adjacent
28f94 20 69 6e 0a 2a 2a 20 6d 65 6d 6f 72 79 2e 20 20   in.** memory.  
28f95 41 20 22 70 6f 73 69 74 69 6f 6e 22 20 69 73 20  A "position" is 
28f96 61 6e 20 69 6e 64 65 78 20 6f 66 20 61 20 74 6f  an index of a to
28f97 6b 65 6e 20 69 6e 20 74 68 65 20 74 6f 6b 65 6e  ken in the token
28f98 20 73 74 72 65 61 6d 0a 2a 2a 20 67 65 6e 65 72   stream.** gener
28f99 61 74 65 64 20 62 79 20 74 68 65 20 74 6f 6b 65  ated by the toke
28f9a 6e 69 7a 65 72 2c 20 77 68 69 6c 65 20 61 6e 20  nizer, while an 
28f9b 22 6f 66 66 73 65 74 22 20 69 73 20 61 20 62 79  "offset" is a by
28f9c 74 65 20 6f 66 66 73 65 74 2c 0a 2a 2a 20 62 6f  te offset,.** bo
28f9d 74 68 20 62 61 73 65 64 20 61 74 20 30 2e 20 20  th based at 0.  
28f9e 4e 6f 74 65 20 74 68 61 74 20 50 4f 53 5f 45 4e  Note that POS_EN
28f9f 44 20 61 6e 64 20 50 4f 53 5f 43 4f 4c 55 4d 4e  D and POS_COLUMN
28fa0 20 6f 63 63 75 72 20 69 6e 20 74 68 65 0a 2a 2a   occur in the.**
28fa1 20 73 61 6d 65 20 6c 6f 67 69 63 61 6c 20 70 6c   same logical pl
28fa2 61 63 65 20 61 73 20 74 68 65 20 70 6f 73 69 74  ace as the posit
28fa3 69 6f 6e 20 65 6c 65 6d 65 6e 74 2c 20 61 6e 64  ion element, and
28fa4 20 61 63 74 20 61 73 20 73 65 6e 74 69 6e 61 6c   act as sentinal
28fa5 73 0a 2a 2a 20 65 6e 64 69 6e 67 20 61 20 70 6f  s.** ending a po
28fa6 73 69 74 69 6f 6e 20 6c 69 73 74 20 61 72 72 61  sition list arra
28fa7 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 44 4c 5f 50 4f  y..**.** A DL_PO
28fa8 53 49 54 49 4f 4e 53 20 64 6f 63 6c 69 73 74 20  SITIONS doclist 
28fa9 6f 6d 69 74 73 20 74 68 65 20 73 74 61 72 74 4f  omits the startO
28faa 66 66 73 65 74 20 61 6e 64 20 65 6e 64 4f 66 66  ffset and endOff
28fab 73 65 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  set.** informati
28fac 6f 6e 2e 20 20 41 20 44 4c 5f 44 4f 43 49 44 53  on.  A DL_DOCIDS
28fad 20 64 6f 63 6c 69 73 74 20 6f 6d 69 74 73 20 62   doclist omits b
28fae 6f 74 68 20 74 68 65 20 70 6f 73 69 74 69 6f 6e  oth the position
28faf 20 61 6e 64 0a 2a 2a 20 6f 66 66 73 65 74 20 69   and.** offset i
28fb0 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 62 65 63 6f  nformation, beco
28fb1 6d 69 6e 67 20 61 6e 20 61 72 72 61 79 20 6f 66  ming an array of
28fb2 20 76 61 72 69 6e 74 2d 65 6e 63 6f 64 65 64 20   varint-encoded 
28fb3 64 6f 63 69 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  docids..**.** On
28fb4 2d 64 69 73 6b 20 64 61 74 61 20 69 73 20 73 74  -disk data is st
28fb5 6f 72 65 64 20 61 73 20 74 79 70 65 20 44 4c 5f  ored as type DL_
28fb6 44 45 46 41 55 4c 54 2c 20 73 6f 20 77 65 20 64  DEFAULT, so we d
28fb7 6f 6e 27 74 20 73 65 72 69 61 6c 69 7a 65 0a 2a  on't serialize.*
28fb8 2a 20 74 68 65 20 74 79 70 65 2e 20 20 44 75 65  * the type.  Due
28fb9 20 74 6f 20 68 6f 77 20 64 65 6c 65 74 69 6f 6e   to how deletion
28fba 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20   is implemented 
28fbb 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74 61 74  in the segmentat
28fbc 69 6f 6e 0a 2a 2a 20 73 79 73 74 65 6d 2c 20 6f  ion.** system, o
28fbd 6e 2d 64 69 73 6b 20 64 6f 63 6c 69 73 74 73 20  n-disk doclists 
28fbe 4d 55 53 54 20 73 74 6f 72 65 20 61 74 20 6c 65  MUST store at le
28fbf 61 73 74 20 70 6f 73 69 74 69 6f 6e 73 2e 0a 2a  ast positions..*
28fc0 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e  *.**.**** Segmen
28fc1 74 20 6c 65 61 66 20 6e 6f 64 65 73 20 2a 2a 2a  t leaf nodes ***
28fc2 2a 0a 2a 2a 20 53 65 67 6d 65 6e 74 20 6c 65 61  *.** Segment lea
28fc3 66 20 6e 6f 64 65 73 20 73 74 6f 72 65 20 74 65  f nodes store te
28fc4 72 6d 73 20 61 6e 64 20 64 6f 63 6c 69 73 74 73  rms and doclists
28fc5 2c 20 6f 72 64 65 72 65 64 20 62 79 20 74 65 72  , ordered by ter
28fc6 6d 2e 20 20 4c 65 61 66 0a 2a 2a 20 6e 6f 64 65  m.  Leaf.** node
28fc7 73 20 61 72 65 20 77 72 69 74 74 65 6e 20 75 73  s are written us
28fc8 69 6e 67 20 4c 65 61 66 57 72 69 74 65 72 2c 20  ing LeafWriter, 
28fc9 61 6e 64 20 72 65 61 64 20 75 73 69 6e 67 20 4c  and read using L
28fca 65 61 66 52 65 61 64 65 72 20 28 74 6f 0a 2a 2a  eafReader (to.**
28fcb 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   iterate through
28fcc 20 61 20 73 69 6e 67 6c 65 20 6c 65 61 66 20 6e   a single leaf n
28fcd 6f 64 65 27 73 20 64 61 74 61 29 20 61 6e 64 20  ode's data) and 
28fce 4c 65 61 76 65 73 52 65 61 64 65 72 20 28 74 6f  LeavesReader (to
28fcf 0a 2a 2a 20 69 74 65 72 61 74 65 20 74 68 72 6f  .** iterate thro
28fd0 75 67 68 20 61 20 73 65 67 6d 65 6e 74 27 73 20  ugh a segment's 
28fd1 65 6e 74 69 72 65 20 6c 65 61 66 20 6c 61 79 65  entire leaf laye
28fd2 72 29 2e 20 20 4c 65 61 66 20 6e 6f 64 65 73 20  r).  Leaf nodes 
28fd3 68 61 76 65 0a 2a 2a 20 74 68 65 20 66 6f 72 6d  have.** the form
28fd4 61 74 3a 0a 2a 2a 0a 2a 2a 20 76 61 72 69 6e 74  at:.**.** varint
28fd5 20 69 48 65 69 67 68 74 3b 20 20 20 20 20 20 20   iHeight;       
28fd6 20 20 20 20 20 20 28 68 65 69 67 68 74 20 66 72        (height fr
28fd7 6f 6d 20 6c 65 61 66 20 6c 65 76 65 6c 2c 20 61  om leaf level, a
28fd8 6c 77 61 79 73 20 30 29 0a 2a 2a 20 76 61 72 69  lways 0).** vari
28fd9 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20 20 20 20  nt nTerm;       
28fda 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20          (length 
28fdb 6f 66 20 66 69 72 73 74 20 74 65 72 6d 29 0a 2a  of first term).*
28fdc 2a 20 63 68 61 72 20 70 54 65 72 6d 5b 6e 54 65  * char pTerm[nTe
28fdd 72 6d 5d 3b 20 20 20 20 20 20 20 20 20 20 28 63  rm];          (c
28fde 6f 6e 74 65 6e 74 20 6f 66 20 66 69 72 73 74 20  ontent of first 
28fdf 74 65 72 6d 29 0a 2a 2a 20 76 61 72 69 6e 74 20  term).** varint 
28fe0 6e 44 6f 63 6c 69 73 74 3b 20 20 20 20 20 20 20  nDoclist;       
28fe1 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20       (length of 
28fe2 74 65 72 6d 27 73 20 61 73 73 6f 63 69 61 74 65  term's associate
28fe3 64 20 64 6f 63 6c 69 73 74 29 0a 2a 2a 20 63 68  d doclist).** ch
28fe4 61 72 20 70 44 6f 63 6c 69 73 74 5b 6e 44 6f 63  ar pDoclist[nDoc
28fe5 6c 69 73 74 5d 3b 20 20 20 20 28 63 6f 6e 74 65  list];    (conte
28fe6 6e 74 20 6f 66 20 64 6f 63 6c 69 73 74 29 0a 2a  nt of doclist).*
28fe7 2a 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20 20  * array {.**    
28fe8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28fe9 20 20 20 20 20 20 20 20 20 28 66 75 72 74 68 65           (furthe
28fea 72 20 74 65 72 6d 73 20 61 72 65 20 64 65 6c 74  r terms are delt
28feb 61 2d 65 6e 63 6f 64 65 64 29 0a 2a 2a 20 20 20  a-encoded).**   
28fec 76 61 72 69 6e 74 20 6e 50 72 65 66 69 78 3b 20  varint nPrefix; 
28fed 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74            (lengt
28fee 68 20 6f 66 20 70 72 65 66 69 78 20 73 68 61 72  h of prefix shar
28fef 65 64 20 77 69 74 68 20 70 72 65 76 69 6f 75 73  ed with previous
28ff0 20 74 65 72 6d 29 0a 2a 2a 20 20 20 76 61 72 69   term).**   vari
28ff1 6e 74 20 6e 53 75 66 66 69 78 3b 20 20 20 20 20  nt nSuffix;     
28ff2 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66        (length of
28ff3 20 75 6e 73 68 61 72 65 64 20 73 75 66 66 69 78   unshared suffix
28ff4 29 0a 2a 2a 20 20 20 63 68 61 72 20 70 54 65 72  ).**   char pTer
28ff5 6d 53 75 66 66 69 78 5b 6e 53 75 66 66 69 78 5d  mSuffix[nSuffix]
28ff6 3b 28 75 6e 73 68 61 72 65 64 20 73 75 66 66 69  ;(unshared suffi
28ff7 78 20 6f 66 20 6e 65 78 74 20 74 65 72 6d 29 0a  x of next term).
28ff8 2a 2a 20 20 20 76 61 72 69 6e 74 20 6e 44 6f 63  **   varint nDoc
28ff9 6c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 28  list;          (
28ffa 6c 65 6e 67 74 68 20 6f 66 20 74 65 72 6d 27 73  length of term's
28ffb 20 61 73 73 6f 63 69 61 74 65 64 20 64 6f 63 6c   associated docl
28ffc 69 73 74 29 0a 2a 2a 20 20 20 63 68 61 72 20 70  ist).**   char p
28ffd 44 6f 63 6c 69 73 74 5b 6e 44 6f 63 6c 69 73 74  Doclist[nDoclist
28ffe 5d 3b 20 20 28 63 6f 6e 74 65 6e 74 20 6f 66 20  ];  (content of 
28fff 64 6f 63 6c 69 73 74 29 0a 2a 2a 20 7d 0a 2a 2a  doclist).** }.**
29000 0a 2a 2a 20 48 65 72 65 2c 20 61 72 72 61 79 20  .** Here, array 
29001 7b 20 58 20 7d 20 6d 65 61 6e 73 20 7a 65 72 6f  { X } means zero
29002 20 6f 72 20 6d 6f 72 65 20 6f 63 63 75 72 72 65   or more occurre
29003 6e 63 65 73 20 6f 66 20 58 2c 20 61 64 6a 61 63  nces of X, adjac
29004 65 6e 74 20 69 6e 0a 2a 2a 20 6d 65 6d 6f 72 79  ent in.** memory
29005 2e 0a 2a 2a 0a 2a 2a 20 4c 65 61 66 20 6e 6f 64  ..**.** Leaf nod
29006 65 73 20 61 72 65 20 62 72 6f 6b 65 6e 20 69 6e  es are broken in
29007 74 6f 20 62 6c 6f 63 6b 73 20 77 68 69 63 68 20  to blocks which 
29008 61 72 65 20 73 74 6f 72 65 64 20 63 6f 6e 74 69  are stored conti
29009 67 75 6f 75 73 6c 79 20 69 6e 0a 2a 2a 20 74 68  guously in.** th
2900a 65 20 25 5f 73 65 67 6d 65 6e 74 73 20 74 61 62  e %_segments tab
2900b 6c 65 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64  le in sorted ord
2900c 65 72 2e 20 20 54 68 69 73 20 6d 65 61 6e 73 20  er.  This means 
2900d 74 68 61 74 20 77 68 65 6e 20 74 68 65 20 65 6e  that when the en
2900e 64 0a 2a 2a 20 6f 66 20 61 20 6e 6f 64 65 20 69  d.** of a node i
2900f 73 20 72 65 61 63 68 65 64 2c 20 74 68 65 20 6e  s reached, the n
29010 65 78 74 20 74 65 72 6d 20 69 73 20 69 6e 20 74  ext term is in t
29011 68 65 20 6e 6f 64 65 20 77 69 74 68 20 74 68 65  he node with the
29012 20 6e 65 78 74 0a 2a 2a 20 67 72 65 61 74 65 72   next.** greater
29013 20 6e 6f 64 65 20 69 64 2e 0a 2a 2a 0a 2a 2a 20   node id..**.** 
29014 4e 65 77 20 64 61 74 61 20 69 73 20 73 70 69 6c  New data is spil
29015 6c 65 64 20 74 6f 20 61 20 6e 65 77 20 6c 65 61  led to a new lea
29016 66 20 6e 6f 64 65 20 77 68 65 6e 20 74 68 65 20  f node when the 
29017 63 75 72 72 65 6e 74 20 6e 6f 64 65 0a 2a 2a 20  current node.** 
29018 65 78 63 65 65 64 73 20 4c 45 41 46 5f 4d 41 58  exceeds LEAF_MAX
29019 20 62 79 74 65 73 20 28 64 65 66 61 75 6c 74 20   bytes (default 
2901a 32 30 34 38 29 2e 20 20 4e 65 77 20 64 61 74 61  2048).  New data
2901b 20 77 68 69 63 68 20 69 74 73 65 6c 66 20 69 73   which itself is
2901c 0a 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20  .** larger than 
2901d 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 28  STANDALONE_MIN (
2901e 64 65 66 61 75 6c 74 20 31 30 32 34 29 20 69 73  default 1024) is
2901f 20 70 6c 61 63 65 64 20 69 6e 20 61 20 73 74 61   placed in a sta
29020 6e 64 61 6c 6f 6e 65 0a 2a 2a 20 6e 6f 64 65 20  ndalone.** node 
29021 28 61 20 6c 65 61 66 20 6e 6f 64 65 20 77 69 74  (a leaf node wit
29022 68 20 61 20 73 69 6e 67 6c 65 20 74 65 72 6d 20  h a single term 
29023 61 6e 64 20 64 6f 63 6c 69 73 74 29 2e 20 20 54  and doclist).  T
29024 68 65 20 67 6f 61 6c 20 6f 66 0a 2a 2a 20 74 68  he goal of.** th
29025 65 73 65 20 73 65 74 74 69 6e 67 73 20 69 73 20  ese settings is 
29026 74 6f 20 70 61 63 6b 20 74 6f 67 65 74 68 65 72  to pack together
29027 20 67 72 6f 75 70 73 20 6f 66 20 73 6d 61 6c 6c   groups of small
29028 20 64 6f 63 6c 69 73 74 73 20 77 68 69 6c 65 0a   doclists while.
29029 2a 2a 20 6d 61 6b 69 6e 67 20 69 74 20 65 66 66  ** making it eff
2902a 69 63 69 65 6e 74 20 74 6f 20 64 69 72 65 63 74  icient to direct
2902b 6c 79 20 61 63 63 65 73 73 20 6c 61 72 67 65 20  ly access large 
2902c 64 6f 63 6c 69 73 74 73 2e 20 20 54 68 65 0a 2a  doclists.  The.*
2902d 2a 20 61 73 73 75 6d 70 74 69 6f 6e 20 69 73 20  * assumption is 
2902e 74 68 61 74 20 6c 61 72 67 65 20 64 6f 63 6c 69  that large docli
2902f 73 74 73 20 72 65 70 72 65 73 65 6e 74 20 74 65  sts represent te
29030 72 6d 73 20 77 68 69 63 68 20 61 72 65 20 6d 6f  rms which are mo
29031 72 65 0a 2a 2a 20 6c 69 6b 65 6c 79 20 74 6f 20  re.** likely to 
29032 62 65 20 71 75 65 72 79 20 74 61 72 67 65 74 73  be query targets
29033 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73 68 65  ..**.** TODO(she
29034 73 73 29 20 49 74 20 6d 61 79 20 62 65 20 75 73  ss) It may be us
29035 65 66 75 6c 20 66 6f 72 20 62 6c 6f 63 6b 69 6e  eful for blockin
29036 67 20 64 65 63 69 73 69 6f 6e 73 20 74 6f 20 62  g decisions to b
29037 65 20 6d 6f 72 65 0a 2a 2a 20 64 79 6e 61 6d 69  e more.** dynami
29038 63 2e 20 20 46 6f 72 20 69 6e 73 74 61 6e 63 65  c.  For instance
29039 2c 20 69 74 20 6d 61 79 20 6d 61 6b 65 20 6d 6f  , it may make mo
2903a 72 65 20 73 65 6e 73 65 20 74 6f 20 68 61 76 65  re sense to have
2903b 20 61 20 32 2e 35 6b 20 6c 65 61 66 0a 2a 2a 20   a 2.5k leaf.** 
2903c 6e 6f 64 65 20 72 61 74 68 65 72 20 74 68 61 6e  node rather than
2903d 20 73 70 6c 69 74 74 69 6e 67 20 69 6e 74 6f 20   splitting into 
2903e 32 6b 20 61 6e 64 20 2e 35 6b 20 6e 6f 64 65 73  2k and .5k nodes
2903f 2e 20 20 4d 79 20 69 6e 74 75 69 74 69 6f 6e 20  .  My intuition 
29040 69 73 0a 2a 2a 20 74 68 61 74 20 74 68 69 73 20  is.** that this 
29041 6d 69 67 68 74 20 65 78 74 65 6e 64 20 74 68 72  might extend thr
29042 6f 75 67 68 20 32 78 20 6f 72 20 34 78 20 74 68  ough 2x or 4x th
29043 65 20 70 61 67 65 73 69 7a 65 2e 0a 2a 2a 0a 2a  e pagesize..**.*
29044 2a 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74 20 69  *.**** Segment i
29045 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 2a 2a  nterior nodes **
29046 2a 2a 0a 2a 2a 20 53 65 67 6d 65 6e 74 20 69 6e  **.** Segment in
29047 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 73 74 6f  terior nodes sto
29048 72 65 20 62 6c 6f 63 6b 69 64 73 20 66 6f 72 20  re blockids for 
29049 73 75 62 74 72 65 65 20 6e 6f 64 65 73 20 61 6e  subtree nodes an
2904a 64 20 74 65 72 6d 73 0a 2a 2a 20 74 6f 20 64 65  d terms.** to de
2904b 73 63 72 69 62 65 20 77 68 61 74 20 64 61 74 61  scribe what data
2904c 20 69 73 20 73 74 6f 72 65 64 20 62 79 20 74 68   is stored by th
2904d 65 20 65 61 63 68 20 73 75 62 74 72 65 65 2e 20  e each subtree. 
2904e 20 49 6e 74 65 72 69 6f 72 0a 2a 2a 20 6e 6f 64   Interior.** nod
2904f 65 73 20 61 72 65 20 77 72 69 74 74 65 6e 20 75  es are written u
29050 73 69 6e 67 20 49 6e 74 65 72 69 6f 72 57 72 69  sing InteriorWri
29051 74 65 72 2c 20 61 6e 64 20 72 65 61 64 20 75 73  ter, and read us
29052 69 6e 67 0a 2a 2a 20 49 6e 74 65 72 69 6f 72 52  ing.** InteriorR
29053 65 61 64 65 72 2e 20 20 49 6e 74 65 72 69 6f 72  eader.  Interior
29054 57 72 69 74 65 72 73 20 61 72 65 20 63 72 65 61  Writers are crea
29055 74 65 64 20 61 73 20 6e 65 65 64 65 64 20 77 68  ted as needed wh
29056 65 6e 0a 2a 2a 20 53 65 67 6d 65 6e 74 57 72 69  en.** SegmentWri
29057 74 65 72 20 63 72 65 61 74 65 73 20 6e 65 77 20  ter creates new 
29058 6c 65 61 66 20 6e 6f 64 65 73 2c 20 6f 72 20 77  leaf nodes, or w
29059 68 65 6e 20 61 6e 20 69 6e 74 65 72 69 6f 72 20  hen an interior 
2905a 6e 6f 64 65 0a 2a 2a 20 69 74 73 65 6c 66 20 67  node.** itself g
2905b 72 6f 77 73 20 74 6f 6f 20 62 69 67 20 61 6e 64  rows too big and
2905c 20 6d 75 73 74 20 62 65 20 73 70 6c 69 74 2e 20   must be split. 
2905d 20 54 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 69   The format of i
2905e 6e 74 65 72 69 6f 72 0a 2a 2a 20 6e 6f 64 65 73  nterior.** nodes
2905f 3a 0a 2a 2a 0a 2a 2a 20 76 61 72 69 6e 74 20 69  :.**.** varint i
29060 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 20  Height;         
29061 20 20 28 68 65 69 67 68 74 20 66 72 6f 6d 20 6c    (height from l
29062 65 61 66 20 6c 65 76 65 6c 2c 20 61 6c 77 61 79  eaf level, alway
29063 73 20 3e 30 29 0a 2a 2a 20 76 61 72 69 6e 74 20  s >0).** varint 
29064 69 42 6c 6f 63 6b 69 64 3b 20 20 20 20 20 20 20  iBlockid;       
29065 20 20 20 28 62 6c 6f 63 6b 20 69 64 20 6f 66 20     (block id of 
29066 6e 6f 64 65 27 73 20 6c 65 66 74 6d 6f 73 74 20  node's leftmost 
29067 73 75 62 74 72 65 65 29 0a 2a 2a 20 6f 70 74 69  subtree).** opti
29068 6f 6e 61 6c 20 7b 0a 2a 2a 20 20 20 76 61 72 69  onal {.**   vari
29069 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20 20 20 20  nt nTerm;       
2906a 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 66      (length of f
2906b 69 72 73 74 20 74 65 72 6d 29 0a 2a 2a 20 20 20  irst term).**   
2906c 63 68 61 72 20 70 54 65 72 6d 5b 6e 54 65 72 6d  char pTerm[nTerm
2906d 5d 3b 20 20 20 20 20 20 28 63 6f 6e 74 65 6e 74  ];      (content
2906e 20 6f 66 20 66 69 72 73 74 20 74 65 72 6d 29 0a   of first term).
2906f 2a 2a 20 20 20 61 72 72 61 79 20 7b 0a 2a 2a 20  **   array {.** 
29070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29071 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
29072 66 75 72 74 68 65 72 20 74 65 72 6d 73 20 61 72  further terms ar
29073 65 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64 29  e delta-encoded)
29074 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20 6e  .**     varint n
29075 50 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 20  Prefix;         
29076 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 73 68     (length of sh
29077 61 72 65 64 20 70 72 65 66 69 78 20 77 69 74 68  ared prefix with
29078 20 70 72 65 76 69 6f 75 73 20 74 65 72 6d 29 0a   previous term).
29079 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20 6e 53  **     varint nS
2907a 75 66 66 69 78 3b 20 20 20 20 20 20 20 20 20 20  uffix;          
2907b 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 75 6e 73    (length of uns
2907c 68 61 72 65 64 20 73 75 66 66 69 78 29 0a 2a 2a  hared suffix).**
2907d 20 20 20 20 20 63 68 61 72 20 70 54 65 72 6d 53       char pTermS
2907e 75 66 66 69 78 5b 6e 53 75 66 66 69 78 5d 3b 20  uffix[nSuffix]; 
2907f 28 75 6e 73 68 61 72 65 64 20 73 75 66 66 69 78  (unshared suffix
29080 20 6f 66 20 6e 65 78 74 20 74 65 72 6d 29 0a 2a   of next term).*
29081 2a 20 20 20 7d 0a 2a 2a 20 7d 0a 2a 2a 0a 2a 2a  *   }.** }.**.**
29082 20 48 65 72 65 2c 20 6f 70 74 69 6f 6e 61 6c 20   Here, optional 
29083 7b 20 58 20 7d 20 6d 65 61 6e 73 20 61 6e 20 6f  { X } means an o
29084 70 74 69 6f 6e 61 6c 20 65 6c 65 6d 65 6e 74 2c  ptional element,
29085 20 77 68 69 6c 65 20 61 72 72 61 79 20 7b 20 58   while array { X
29086 20 7d 0a 2a 2a 20 6d 65 61 6e 73 20 7a 65 72 6f   }.** means zero
29087 20 6f 72 20 6d 6f 72 65 20 6f 63 63 75 72 72 65   or more occurre
29088 6e 63 65 73 20 6f 66 20 58 2c 20 61 64 6a 61 63  nces of X, adjac
29089 65 6e 74 20 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a  ent in memory..*
2908a 2a 0a 2a 2a 20 41 6e 20 69 6e 74 65 72 69 6f 72  *.** An interior
2908b 20 6e 6f 64 65 20 65 6e 63 6f 64 65 73 20 6e 20   node encodes n 
2908c 74 65 72 6d 73 20 73 65 70 61 72 61 74 69 6e 67  terms separating
2908d 20 6e 2b 31 20 73 75 62 74 72 65 65 73 2e 20 20   n+1 subtrees.  
2908e 54 68 65 0a 2a 2a 20 73 75 62 74 72 65 65 20 62  The.** subtree b
2908f 6c 6f 63 6b 73 20 61 72 65 20 63 6f 6e 74 69 67  locks are contig
29090 75 6f 75 73 2c 20 73 6f 20 6f 6e 6c 79 20 74 68  uous, so only th
29091 65 20 66 69 72 73 74 20 73 75 62 74 72 65 65 27  e first subtree'
29092 73 20 62 6c 6f 63 6b 69 64 0a 2a 2a 20 69 73 20  s blockid.** is 
29093 65 6e 63 6f 64 65 64 2e 20 20 54 68 65 20 73 75  encoded.  The su
29094 62 74 72 65 65 20 61 74 20 69 42 6c 6f 63 6b 69  btree at iBlocki
29095 64 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 61  d will contain a
29096 6c 6c 20 74 65 72 6d 73 20 6c 65 73 73 0a 2a 2a  ll terms less.**
29097 20 74 68 61 6e 20 74 68 65 20 66 69 72 73 74 20   than the first 
29098 74 65 72 6d 20 65 6e 63 6f 64 65 64 20 28 6f 72  term encoded (or
29099 20 61 6c 6c 20 74 65 72 6d 73 20 69 66 20 6e 6f   all terms if no
2909a 20 74 65 72 6d 20 69 73 20 65 6e 63 6f 64 65 64   term is encoded
2909b 29 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  )..** Otherwise,
2909c 20 66 6f 72 20 74 65 72 6d 73 20 67 72 65 61 74   for terms great
2909d 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  er than or equal
2909e 20 74 6f 20 70 54 65 72 6d 5b 69 5d 20 62 75 74   to pTerm[i] but
2909f 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 70 54   less.** than pT
290a0 65 72 6d 5b 69 2b 31 5d 2c 20 74 68 65 20 73 75  erm[i+1], the su
290a1 62 74 72 65 65 20 66 6f 72 20 74 68 61 74 20 74  btree for that t
290a2 65 72 6d 20 77 69 6c 6c 20 62 65 20 72 6f 6f 74  erm will be root
290a3 65 64 20 61 74 0a 2a 2a 20 69 42 6c 6f 63 6b 69  ed at.** iBlocki
290a4 64 2b 69 2e 20 20 49 6e 74 65 72 69 6f 72 20 6e  d+i.  Interior n
290a5 6f 64 65 73 20 6f 6e 6c 79 20 73 74 6f 72 65 20  odes only store 
290a6 65 6e 6f 75 67 68 20 74 65 72 6d 20 64 61 74 61  enough term data
290a7 20 74 6f 0a 2a 2a 20 64 69 73 74 69 6e 67 75 69   to.** distingui
290a8 73 68 20 61 64 6a 61 63 65 6e 74 20 63 68 69 6c  sh adjacent chil
290a9 64 72 65 6e 20 28 69 66 20 74 68 65 20 72 69 67  dren (if the rig
290aa 68 74 6d 6f 73 74 20 74 65 72 6d 20 6f 66 20 74  htmost term of t
290ab 68 65 20 6c 65 66 74 0a 2a 2a 20 63 68 69 6c 64  he left.** child
290ac 20 69 73 20 22 73 6f 6d 65 74 68 69 6e 67 22 2c   is "something",
290ad 20 61 6e 64 20 74 68 65 20 6c 65 66 74 6d 6f 73   and the leftmos
290ae 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20 72 69  t term of the ri
290af 67 68 74 20 63 68 69 6c 64 20 69 73 0a 2a 2a 20  ght child is.** 
290b0 22 77 69 63 6b 65 64 22 2c 20 6f 6e 6c 79 20 22  "wicked", only "
290b1 77 22 20 69 73 20 73 74 6f 72 65 64 29 2e 0a 2a  w" is stored)..*
290b2 2a 0a 2a 2a 20 4e 65 77 20 64 61 74 61 20 69 73  *.** New data is
290b3 20 73 70 69 6c 6c 65 64 20 74 6f 20 61 20 6e 65   spilled to a ne
290b4 77 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20  w interior node 
290b5 61 74 20 74 68 65 20 73 61 6d 65 20 68 65 69 67  at the same heig
290b6 68 74 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 63  ht when.** the c
290b7 75 72 72 65 6e 74 20 6e 6f 64 65 20 65 78 63 65  urrent node exce
290b8 65 64 73 20 49 4e 54 45 52 49 4f 52 5f 4d 41 58  eds INTERIOR_MAX
290b9 20 62 79 74 65 73 20 28 64 65 66 61 75 6c 74 20   bytes (default 
290ba 32 30 34 38 29 2e 0a 2a 2a 20 49 4e 54 45 52 49  2048)..** INTERI
290bb 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53 20 28 64 65  OR_MIN_TERMS (de
290bc 66 61 75 6c 74 20 37 29 20 6b 65 65 70 73 20 6c  fault 7) keeps l
290bd 61 72 67 65 20 74 65 72 6d 73 20 66 72 6f 6d 20  arge terms from 
290be 6d 6f 6e 6f 70 6f 6c 69 7a 69 6e 67 0a 2a 2a 20  monopolizing.** 
290bf 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 61  interior nodes a
290c0 6e 64 20 6d 61 6b 69 6e 67 20 74 68 65 20 74 72  nd making the tr
290c1 65 65 20 74 6f 6f 20 73 6b 69 6e 6e 79 2e 20 20  ee too skinny.  
290c2 54 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  The interior nod
290c3 65 73 0a 2a 2a 20 61 74 20 61 20 67 69 76 65 6e  es.** at a given
290c4 20 68 65 69 67 68 74 20 61 72 65 20 6e 61 74 75   height are natu
290c5 72 61 6c 6c 79 20 74 72 61 63 6b 65 64 20 62 79  rally tracked by
290c6 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20   interior nodes 
290c7 61 74 0a 2a 2a 20 68 65 69 67 68 74 2b 31 2c 20  at.** height+1, 
290c8 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a  and so on..**.**
290c9 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74 20 64 69  .**** Segment di
290ca 72 65 63 74 6f 72 79 20 2a 2a 2a 2a 0a 2a 2a 20  rectory ****.** 
290cb 54 68 65 20 73 65 67 6d 65 6e 74 20 64 69 72 65  The segment dire
290cc 63 74 6f 72 79 20 69 6e 20 74 61 62 6c 65 20 25  ctory in table %
290cd 5f 73 65 67 64 69 72 20 73 74 6f 72 65 73 20 6d  _segdir stores m
290ce 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  eta-information 
290cf 66 6f 72 0a 2a 2a 20 6d 65 72 67 69 6e 67 20 61  for.** merging a
290d0 6e 64 20 64 65 6c 65 74 69 6e 67 20 73 65 67 6d  nd deleting segm
290d1 65 6e 74 73 2c 20 61 6e 64 20 61 6c 73 6f 20 74  ents, and also t
290d2 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20  he root node of 
290d3 74 68 65 0a 2a 2a 20 73 65 67 6d 65 6e 74 27 73  the.** segment's
290d4 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   tree..**.** The
290d5 20 72 6f 6f 74 20 6e 6f 64 65 20 69 73 20 74 68   root node is th
290d6 65 20 74 6f 70 20 6e 6f 64 65 20 6f 66 20 74 68  e top node of th
290d7 65 20 73 65 67 6d 65 6e 74 27 73 20 74 72 65 65  e segment's tree
290d8 20 61 66 74 65 72 20 65 6e 63 6f 64 69 6e 67 0a   after encoding.
290d9 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 20 73 65  ** the entire se
290da 67 6d 65 6e 74 2c 20 72 65 73 74 72 69 63 74 65  gment, restricte
290db 64 20 74 6f 20 52 4f 4f 54 5f 4d 41 58 20 62 79  d to ROOT_MAX by
290dc 74 65 73 20 28 64 65 66 61 75 6c 74 20 31 30 32  tes (default 102
290dd 34 29 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 75 6c  4)..** This coul
290de 64 20 62 65 20 65 69 74 68 65 72 20 61 20 6c 65  d be either a le
290df 61 66 20 6e 6f 64 65 20 6f 72 20 61 6e 20 69 6e  af node or an in
290e0 74 65 72 69 6f 72 20 6e 6f 64 65 2e 20 20 49 66  terior node.  If
290e1 20 74 68 65 20 74 6f 70 0a 2a 2a 20 6e 6f 64 65   the top.** node
290e2 20 72 65 71 75 69 72 65 73 20 6d 6f 72 65 20 74   requires more t
290e3 68 61 6e 20 52 4f 4f 54 5f 4d 41 58 20 62 79 74  han ROOT_MAX byt
290e4 65 73 2c 20 69 74 20 69 73 20 66 6c 75 73 68 65  es, it is flushe
290e5 64 20 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 0a  d to %_segments.
290e6 2a 2a 20 61 6e 64 20 61 20 6e 65 77 20 72 6f 6f  ** and a new roo
290e7 74 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20  t interior node 
290e8 69 73 20 67 65 6e 65 72 61 74 65 64 20 28 77 68  is generated (wh
290e9 69 63 68 20 73 68 6f 75 6c 64 20 61 6c 77 61 79  ich should alway
290ea 73 20 66 69 74 0a 2a 2a 20 77 69 74 68 69 6e 20  s fit.** within 
290eb 52 4f 4f 54 5f 4d 41 58 20 62 65 63 61 75 73 65  ROOT_MAX because
290ec 20 69 74 20 6f 6e 6c 79 20 6e 65 65 64 73 20 73   it only needs s
290ed 70 61 63 65 20 66 6f 72 20 32 20 76 61 72 69 6e  pace for 2 varin
290ee 74 73 2c 20 74 68 65 0a 2a 2a 20 68 65 69 67 68  ts, the.** heigh
290ef 74 20 61 6e 64 20 74 68 65 20 62 6c 6f 63 6b 69  t and the blocki
290f0 64 20 6f 66 20 74 68 65 20 70 72 65 76 69 6f 75  d of the previou
290f1 73 20 72 6f 6f 74 29 2e 0a 2a 2a 0a 2a 2a 20 54  s root)..**.** T
290f2 68 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74  he meta-informat
290f3 69 6f 6e 20 69 6e 20 74 68 65 20 73 65 67 6d 65  ion in the segme
290f4 6e 74 20 64 69 72 65 63 74 6f 72 79 20 69 73 3a  nt directory is:
290f5 0a 2a 2a 20 20 20 6c 65 76 65 6c 20 20 20 20 20  .**   level     
290f6 20 20 20 20 20 20 20 20 20 20 2d 20 73 65 67 6d            - segm
290f7 65 6e 74 20 6c 65 76 65 6c 20 28 73 65 65 20 62  ent level (see b
290f8 65 6c 6f 77 29 0a 2a 2a 20 20 20 69 64 78 20 20  elow).**   idx  
290f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d                 -
290fa 20 69 6e 64 65 78 20 77 69 74 68 69 6e 20 6c 65   index within le
290fb 76 65 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  vel.**          
290fc 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 20 28               - (
290fd 6c 65 76 65 6c 2c 69 64 78 20 75 6e 69 71 75 65  level,idx unique
290fe 6c 79 20 69 64 65 6e 74 69 66 79 20 61 20 73 65  ly identify a se
290ff 67 6d 65 6e 74 29 0a 2a 2a 20 20 20 73 74 61 72  gment).**   star
29100 74 5f 62 6c 6f 63 6b 20 20 20 20 20 20 20 20 20  t_block         
29101 2d 20 66 69 72 73 74 20 6c 65 61 66 20 6e 6f 64  - first leaf nod
29102 65 0a 2a 2a 20 20 20 6c 65 61 76 65 73 5f 65 6e  e.**   leaves_en
29103 64 5f 62 6c 6f 63 6b 20 20 20 20 2d 20 6c 61 73  d_block    - las
29104 74 20 6c 65 61 66 20 6e 6f 64 65 0a 2a 2a 20 20  t leaf node.**  
29105 20 65 6e 64 5f 62 6c 6f 63 6b 20 20 20 20 20 20   end_block      
29106 20 20 20 20 20 2d 20 6c 61 73 74 20 62 6c 6f 63       - last bloc
29107 6b 20 28 69 6e 63 6c 75 64 69 6e 67 20 69 6e 74  k (including int
29108 65 72 69 6f 72 20 6e 6f 64 65 73 29 0a 2a 2a 20  erior nodes).** 
29109 20 20 72 6f 6f 74 20 20 20 20 20 20 20 20 20 20    root          
2910a 20 20 20 20 20 20 2d 20 63 6f 6e 74 65 6e 74 73        - contents
2910b 20 6f 66 20 72 6f 6f 74 20 6e 6f 64 65 0a 2a 2a   of root node.**
2910c 0a 2a 2a 20 49 66 20 74 68 65 20 72 6f 6f 74 20  .** If the root 
2910d 6e 6f 64 65 20 69 73 20 61 20 6c 65 61 66 20 6e  node is a leaf n
2910e 6f 64 65 2c 20 74 68 65 6e 20 73 74 61 72 74 5f  ode, then start_
2910f 62 6c 6f 63 6b 2c 0a 2a 2a 20 6c 65 61 76 65 73  block,.** leaves
29110 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 61 6e 64 20  _end_block, and 
29111 65 6e 64 5f 62 6c 6f 63 6b 20 61 72 65 20 61 6c  end_block are al
29112 6c 20 30 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20  l 0..**.**.**** 
29113 53 65 67 6d 65 6e 74 20 6d 65 72 67 69 6e 67 20  Segment merging 
29114 2a 2a 2a 2a 0a 2a 2a 20 54 6f 20 61 6d 6f 72 74  ****.** To amort
29115 69 7a 65 20 75 70 64 61 74 65 20 63 6f 73 74 73  ize update costs
29116 2c 20 73 65 67 6d 65 6e 74 73 20 61 72 65 20 67  , segments are g
29117 72 6f 75 70 73 20 69 6e 74 6f 20 6c 65 76 65 6c  roups into level
29118 73 20 61 6e 64 0a 2a 2a 20 6d 65 72 67 65 64 20  s and.** merged 
29119 69 6e 20 6d 61 74 63 68 65 73 2e 20 20 45 61 63  in matches.  Eac
2911a 68 20 69 6e 63 72 65 61 73 65 20 69 6e 20 6c 65  h increase in le
2911b 76 65 6c 20 72 65 70 72 65 73 65 6e 74 73 20 65  vel represents e
2911c 78 70 6f 6e 65 6e 74 69 61 6c 6c 79 0a 2a 2a 20  xponentially.** 
2911d 6d 6f 72 65 20 64 6f 63 75 6d 65 6e 74 73 2e 0a  more documents..
2911e 2a 2a 0a 2a 2a 20 4e 65 77 20 64 6f 63 75 6d 65  **.** New docume
2911f 6e 74 73 20 28 61 63 74 75 61 6c 6c 79 2c 20 64  nts (actually, d
29120 6f 63 75 6d 65 6e 74 20 75 70 64 61 74 65 73 29  ocument updates)
29121 20 61 72 65 20 74 6f 6b 65 6e 69 7a 65 64 20 61   are tokenized a
29122 6e 64 0a 2a 2a 20 77 72 69 74 74 65 6e 20 69 6e  nd.** written in
29123 64 69 76 69 64 75 61 6c 6c 79 20 28 75 73 69 6e  dividually (usin
29124 67 20 4c 65 61 66 57 72 69 74 65 72 29 20 74 6f  g LeafWriter) to
29125 20 61 20 6c 65 76 65 6c 20 30 20 73 65 67 6d 65   a level 0 segme
29126 6e 74 2c 20 77 69 74 68 0a 2a 2a 20 69 6e 63 72  nt, with.** incr
29127 65 6d 65 6e 74 69 6e 67 20 69 64 78 2e 20 20 57  ementing idx.  W
29128 68 65 6e 20 69 64 78 20 72 65 61 63 68 65 73 20  hen idx reaches 
29129 4d 45 52 47 45 5f 43 4f 55 4e 54 20 28 64 65 66  MERGE_COUNT (def
2912a 61 75 6c 74 20 31 36 29 2c 20 61 6c 6c 0a 2a 2a  ault 16), all.**
2912b 20 6c 65 76 65 6c 20 30 20 73 65 67 6d 65 6e 74   level 0 segment
2912c 73 20 61 72 65 20 6d 65 72 67 65 64 20 69 6e 74  s are merged int
2912d 6f 20 61 20 73 69 6e 67 6c 65 20 6c 65 76 65 6c  o a single level
2912e 20 31 20 73 65 67 6d 65 6e 74 2e 20 20 4c 65 76   1 segment.  Lev
2912f 65 6c 20 31 0a 2a 2a 20 69 73 20 70 6f 70 75 6c  el 1.** is popul
29130 61 74 65 64 20 6c 69 6b 65 20 6c 65 76 65 6c 20  ated like level 
29131 30 2c 20 61 6e 64 20 65 76 65 6e 74 75 61 6c 6c  0, and eventuall
29132 79 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 6c 65  y MERGE_COUNT le
29133 76 65 6c 20 31 0a 2a 2a 20 73 65 67 6d 65 6e 74  vel 1.** segment
29134 73 20 61 72 65 20 6d 65 72 67 65 64 20 74 6f 20  s are merged to 
29135 61 20 73 69 6e 67 6c 65 20 6c 65 76 65 6c 20 32  a single level 2
29136 20 73 65 67 6d 65 6e 74 20 28 72 65 70 72 65 73   segment (repres
29137 65 6e 74 69 6e 67 0a 2a 2a 20 4d 45 52 47 45 5f  enting.** MERGE_
29138 43 4f 55 4e 54 5e 32 20 75 70 64 61 74 65 73 29  COUNT^2 updates)
29139 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a  , and so on..**.
2913a 2a 2a 20 41 20 73 65 67 6d 65 6e 74 20 6d 65 72  ** A segment mer
2913b 67 65 20 74 72 61 76 65 72 73 65 73 20 61 6c 6c  ge traverses all
2913c 20 73 65 67 6d 65 6e 74 73 20 61 74 20 61 20 67   segments at a g
2913d 69 76 65 6e 20 6c 65 76 65 6c 20 69 6e 0a 2a 2a  iven level in.**
2913e 20 70 61 72 61 6c 6c 65 6c 2c 20 70 65 72 66 6f   parallel, perfo
2913f 72 6d 69 6e 67 20 61 20 73 74 72 61 69 67 68 74  rming a straight
29140 66 6f 72 77 61 72 64 20 73 6f 72 74 65 64 20 6d  forward sorted m
29141 65 72 67 65 2e 20 20 53 69 6e 63 65 20 73 65 67  erge.  Since seg
29142 6d 65 6e 74 0a 2a 2a 20 6c 65 61 66 20 6e 6f 64  ment.** leaf nod
29143 65 73 20 61 72 65 20 77 72 69 74 74 65 6e 20 69  es are written i
29144 6e 20 74 6f 20 74 68 65 20 25 5f 73 65 67 6d 65  n to the %_segme
29145 6e 74 73 20 74 61 62 6c 65 20 69 6e 20 6f 72 64  nts table in ord
29146 65 72 2c 20 74 68 69 73 0a 2a 2a 20 6d 65 72 67  er, this.** merg
29147 65 20 74 72 61 76 65 72 73 65 73 20 74 68 65 20  e traverses the 
29148 75 6e 64 65 72 6c 79 69 6e 67 20 73 71 6c 69 74  underlying sqlit
29149 65 20 64 69 73 6b 20 73 74 72 75 63 74 75 72 65  e disk structure
2914a 73 20 65 66 66 69 63 69 65 6e 74 6c 79 2e 0a 2a  s efficiently..*
2914b 2a 20 41 66 74 65 72 20 74 68 65 20 6d 65 72 67  * After the merg
2914c 65 2c 20 61 6c 6c 20 73 65 67 6d 65 6e 74 20 62  e, all segment b
2914d 6c 6f 63 6b 73 20 66 72 6f 6d 20 74 68 65 20 6d  locks from the m
2914e 65 72 67 65 64 20 6c 65 76 65 6c 20 61 72 65 0a  erged level are.
2914f 2a 2a 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a 2a  ** deleted..**.*
29150 2a 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 63 6f  * MERGE_COUNT co
29151 6e 74 72 6f 6c 73 20 68 6f 77 20 6f 66 74 65 6e  ntrols how often
29152 20 77 65 20 6d 65 72 67 65 20 73 65 67 6d 65 6e   we merge segmen
29153 74 73 2e 20 20 31 36 20 73 65 65 6d 73 20 74 6f  ts.  16 seems to
29154 20 62 65 0a 2a 2a 20 73 6f 6d 65 77 68 61 74 20   be.** somewhat 
29155 6f 66 20 61 20 73 77 65 65 74 20 73 70 6f 74 20  of a sweet spot 
29156 66 6f 72 20 69 6e 73 65 72 74 69 6f 6e 20 70 65  for insertion pe
29157 72 66 6f 72 6d 61 6e 63 65 2e 20 20 33 32 20 61  rformance.  32 a
29158 6e 64 20 36 34 20 73 68 6f 77 0a 2a 2a 20 76 65  nd 64 show.** ve
29159 72 79 20 73 69 6d 69 6c 61 72 20 70 65 72 66 6f  ry similar perfo
2915a 72 6d 61 6e 63 65 20 6e 75 6d 62 65 72 73 20 74  rmance numbers t
2915b 6f 20 31 36 20 6f 6e 20 69 6e 73 65 72 74 69 6f  o 16 on insertio
2915c 6e 2c 20 74 68 6f 75 67 68 20 74 68 65 79 27 72  n, though they'r
2915d 65 0a 2a 2a 20 61 20 74 69 6e 79 20 62 69 74 20  e.** a tiny bit 
2915e 73 6c 6f 77 65 72 20 28 70 65 72 68 61 70 73 20  slower (perhaps 
2915f 64 75 65 20 74 6f 20 6d 6f 72 65 20 6f 76 65 72  due to more over
29160 68 65 61 64 20 69 6e 20 6d 65 72 67 65 2d 74 69  head in merge-ti
29161 6d 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 29 2e 20  me.** sorting). 
29162 20 38 20 69 73 20 61 62 6f 75 74 20 32 30 25 20   8 is about 20% 
29163 73 6c 6f 77 65 72 20 74 68 61 6e 20 31 36 2c 20  slower than 16, 
29164 34 20 61 62 6f 75 74 20 35 30 25 20 73 6c 6f 77  4 about 50% slow
29165 65 72 20 74 68 61 6e 0a 2a 2a 20 31 36 2c 20 32  er than.** 16, 2
29166 20 61 62 6f 75 74 20 36 36 25 20 73 6c 6f 77 65   about 66% slowe
29167 72 20 74 68 61 6e 20 31 36 2e 0a 2a 2a 0a 2a 2a  r than 16..**.**
29168 20 41 74 20 71 75 65 72 79 20 74 69 6d 65 2c 20   At query time, 
29169 68 69 67 68 20 4d 45 52 47 45 5f 43 4f 55 4e 54  high MERGE_COUNT
2916a 20 69 6e 63 72 65 61 73 65 73 20 74 68 65 20 6e   increases the n
2916b 75 6d 62 65 72 20 6f 66 20 73 65 67 6d 65 6e 74  umber of segment
2916c 73 0a 2a 2a 20 77 68 69 63 68 20 6e 65 65 64 20  s.** which need 
2916d 74 6f 20 62 65 20 73 63 61 6e 6e 65 64 20 61 6e  to be scanned an
2916e 64 20 6d 65 72 67 65 64 2e 20 20 46 6f 72 20 69  d merged.  For i
2916f 6e 73 74 61 6e 63 65 2c 20 77 69 74 68 20 31 30  nstance, with 10
29170 30 6b 20 64 6f 63 73 0a 2a 2a 20 69 6e 73 65 72  0k docs.** inser
29171 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 45  ted:.**.**    ME
29172 52 47 45 5f 43 4f 55 4e 54 20 20 20 73 65 67 6d  RGE_COUNT   segm
29173 65 6e 74 73 0a 2a 2a 20 20 20 20 20 20 20 31 36  ents.**       16
29174 20 20 20 20 20 20 20 20 20 20 20 32 35 0a 2a 2a             25.**
29175 20 20 20 20 20 20 20 20 38 20 20 20 20 20 20 20          8       
29176 20 20 20 20 31 32 0a 2a 2a 20 20 20 20 20 20 20      12.**       
29177 20 34 20 20 20 20 20 20 20 20 20 20 20 31 30 0a   4           10.
29178 2a 2a 20 20 20 20 20 20 20 20 32 20 20 20 20 20  **        2     
29179 20 20 20 20 20 20 20 36 0a 2a 2a 0a 2a 2a 20 54         6.**.** T
2917a 68 69 73 20 61 70 70 65 61 72 73 20 74 6f 20 68  his appears to h
2917b 61 76 65 20 6f 6e 6c 79 20 61 20 6d 6f 64 65 72  ave only a moder
2917c 61 74 65 20 69 6d 70 61 63 74 20 6f 6e 20 71 75  ate impact on qu
2917d 65 72 69 65 73 20 66 6f 72 20 76 65 72 79 0a 2a  eries for very.*
2917e 2a 20 66 72 65 71 75 65 6e 74 20 74 65 72 6d 73  * frequent terms
2917f 20 28 77 68 69 63 68 20 61 72 65 20 73 6f 6d 65   (which are some
29180 77 68 61 74 20 64 6f 6d 69 6e 61 74 65 64 20 62  what dominated b
29181 79 20 73 65 67 6d 65 6e 74 20 6d 65 72 67 65 0a  y segment merge.
29182 2a 2a 20 63 6f 73 74 73 29 2c 20 61 6e 64 20 69  ** costs), and i
29183 6e 66 72 65 71 75 65 6e 74 20 61 6e 64 20 6e 6f  nfrequent and no
29184 6e 2d 65 78 69 73 74 65 6e 74 20 74 65 72 6d 73  n-existent terms
29185 20 73 74 69 6c 6c 20 73 65 65 6d 20 74 6f 20 62   still seem to b
29186 65 20 66 61 73 74 0a 2a 2a 20 65 76 65 6e 20 77  e fast.** even w
29187 69 74 68 20 6d 61 6e 79 20 73 65 67 6d 65 6e 74  ith many segment
29188 73 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73 68  s..**.** TODO(sh
29189 65 73 73 29 20 54 68 61 74 20 73 61 69 64 2c 20  ess) That said, 
2918a 69 74 20 77 6f 75 6c 64 20 62 65 20 6e 69 63 65  it would be nice
2918b 20 74 6f 20 68 61 76 65 20 61 20 62 65 74 74 65   to have a bette
2918c 72 20 71 75 65 72 79 2d 73 69 64 65 0a 2a 2a 20  r query-side.** 
2918d 61 72 67 75 6d 65 6e 74 20 66 6f 72 20 4d 45 52  argument for MER
2918e 47 45 5f 43 4f 55 4e 54 20 6f 66 20 31 36 2e 20  GE_COUNT of 16. 
2918f 20 41 6c 73 6f 2c 20 69 74 20 69 73 20 70 6f 73   Also, it is pos
29190 73 69 62 6c 65 2f 6c 69 6b 65 6c 79 20 74 68 61  sible/likely tha
29191 74 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f  t.** optimizatio
29192 6e 73 20 74 6f 20 74 68 69 6e 67 73 20 6c 69 6b  ns to things lik
29193 65 20 64 6f 63 6c 69 73 74 20 6d 65 72 67 69 6e  e doclist mergin
29194 67 20 77 69 6c 6c 20 73 77 69 6e 67 20 74 68 65  g will swing the
29195 20 73 77 65 65 74 0a 2a 2a 20 73 70 6f 74 20 61   sweet.** spot a
29196 72 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 0a  round..**.**.**.
29197 2a 2a 2a 2a 20 48 61 6e 64 6c 69 6e 67 20 6f 66  **** Handling of
29198 20 64 65 6c 65 74 69 6f 6e 73 20 61 6e 64 20 75   deletions and u
29199 70 64 61 74 65 73 20 2a 2a 2a 2a 0a 2a 2a 20 53  pdates ****.** S
2919a 69 6e 63 65 20 77 65 27 72 65 20 75 73 69 6e 67  ince we're using
2919b 20 61 20 73 65 67 6d 65 6e 74 65 64 20 73 74 72   a segmented str
2919c 75 63 74 75 72 65 2c 20 77 69 74 68 20 6e 6f 20  ucture, with no 
2919d 64 6f 63 69 64 2d 6f 72 69 65 6e 74 65 64 0a 2a  docid-oriented.*
2919e 2a 20 69 6e 64 65 78 20 69 6e 74 6f 20 74 68 65  * index into the
2919f 20 74 65 72 6d 20 69 6e 64 65 78 2c 20 77 65 20   term index, we 
291a0 63 6c 65 61 72 6c 79 20 63 61 6e 6e 6f 74 20 73  clearly cannot s
291a1 69 6d 70 6c 79 20 75 70 64 61 74 65 20 74 68 65  imply update the
291a2 20 74 65 72 6d 0a 2a 2a 20 69 6e 64 65 78 20 77   term.** index w
291a3 68 65 6e 20 61 20 64 6f 63 75 6d 65 6e 74 20 69  hen a document i
291a4 73 20 64 65 6c 65 74 65 64 20 6f 72 20 75 70 64  s deleted or upd
291a5 61 74 65 64 2e 20 20 46 6f 72 20 64 65 6c 65 74  ated.  For delet
291a6 69 6f 6e 73 2c 20 77 65 0a 2a 2a 20 77 72 69 74  ions, we.** writ
291a7 65 20 61 6e 20 65 6d 70 74 79 20 64 6f 63 6c 69  e an empty docli
291a8 73 74 20 28 76 61 72 69 6e 74 28 64 6f 63 69 64  st (varint(docid
291a9 29 20 76 61 72 69 6e 74 28 50 4f 53 5f 45 4e 44  ) varint(POS_END
291aa 29 29 2c 20 66 6f 72 20 75 70 64 61 74 65 73 0a  )), for updates.
291ab 2a 2a 20 77 65 20 73 69 6d 70 6c 79 20 77 72 69  ** we simply wri
291ac 74 65 20 74 68 65 20 6e 65 77 20 64 6f 63 6c 69  te the new docli
291ad 73 74 2e 20 20 53 65 67 6d 65 6e 74 20 6d 65 72  st.  Segment mer
291ae 67 65 73 20 6f 76 65 72 77 72 69 74 65 20 6f 6c  ges overwrite ol
291af 64 65 72 0a 2a 2a 20 64 61 74 61 20 66 6f 72 20  der.** data for 
291b0 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 6f 63  a particular doc
291b1 69 64 20 77 69 74 68 20 6e 65 77 65 72 20 64 61  id with newer da
291b2 74 61 2c 20 73 6f 20 64 65 6c 65 74 65 73 20 6f  ta, so deletes o
291b3 72 20 75 70 64 61 74 65 73 0a 2a 2a 20 77 69 6c  r updates.** wil
291b4 6c 20 65 76 65 6e 74 75 61 6c 6c 79 20 6f 76 65  l eventually ove
291b5 72 74 61 6b 65 20 74 68 65 20 65 61 72 6c 69 65  rtake the earlie
291b6 72 20 64 61 74 61 20 61 6e 64 20 6b 6e 6f 63 6b  r data and knock
291b7 20 69 74 20 6f 75 74 2e 20 20 54 68 65 0a 2a 2a   it out.  The.**
291b8 20 71 75 65 72 79 20 6c 6f 67 69 63 20 6c 69 6b   query logic lik
291b9 65 77 69 73 65 20 6d 65 72 67 65 73 20 64 6f 63  ewise merges doc
291ba 6c 69 73 74 73 20 73 6f 20 74 68 61 74 20 6e 65  lists so that ne
291bb 77 65 72 20 64 61 74 61 20 6b 6e 6f 63 6b 73 20  wer data knocks 
291bc 6f 75 74 0a 2a 2a 20 6f 6c 64 65 72 20 64 61 74  out.** older dat
291bd 61 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73 68  a..**.** TODO(sh
291be 65 73 73 29 20 50 72 6f 76 69 64 65 20 61 20 56  ess) Provide a V
291bf 41 43 55 55 4d 20 74 79 70 65 20 6f 70 65 72 61  ACUUM type opera
291c0 74 69 6f 6e 20 74 6f 20 63 6c 65 61 72 20 6f 75  tion to clear ou
291c1 74 20 61 6c 6c 0a 2a 2a 20 64 65 6c 65 74 69 6f  t all.** deletio
291c2 6e 73 20 61 6e 64 20 64 75 70 6c 69 63 61 74 69  ns and duplicati
291c3 6f 6e 73 2e 20 20 54 68 69 73 20 77 6f 75 6c 64  ons.  This would
291c4 20 62 61 73 69 63 61 6c 6c 79 20 62 65 20 61 20   basically be a 
291c5 66 6f 72 63 65 64 20 6d 65 72 67 65 0a 2a 2a 20  forced merge.** 
291c6 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 65  into a single se
291c7 67 6d 65 6e 74 2e 0a 2a 2f 0a 0a 23 69 66 20 21  gment..*/..#if !
291c8 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43  defined(SQLITE_C
291c9 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  ORE) || defined(
291ca 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
291cb 53 33 29 0a 0a 23 69 66 20 64 65 66 69 6e 65 64  S3)..#if defined
291cc 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46  (SQLITE_ENABLE_F
291cd 54 53 33 29 20 26 26 20 21 64 65 66 69 6e 65 64  TS3) && !defined
291ce 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 0a 23 20  (SQLITE_CORE).# 
291cf 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
291d0 52 45 20 31 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  RE 1.#endif.../*
291d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
291d2 63 6c 75 64 65 20 66 74 73 33 5f 68 61 73 68 2e  clude fts3_hash.
291d3 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
291d4 6f 66 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a  of fts3.c ******
291d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
291d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
291d7 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 68 61  gin file fts3_ha
291d8 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  sh.h ***********
291d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
291db 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
291dc 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 22.**.** The 
291dd 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
291de 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
291df 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
291e0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
291e1 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
291e2 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
291e3 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
291e4 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
291e5 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
291e6 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
291e7 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
291e8 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
291e9 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
291ea 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
291eb 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
291ec 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
291ed 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
291ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291f2 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ****.** This is 
291f3 74 68 65 20 68 65 61 64 65 72 20 66 69 6c 65 20  the header file 
291f4 66 6f 72 20 74 68 65 20 67 65 6e 65 72 69 63 20  for the generic 
291f5 68 61 73 68 2d 74 61 62 6c 65 20 69 6d 70 6c 65  hash-table imple
291f6 6d 65 6e 61 74 69 6f 6e 0a 2a 2a 20 75 73 65 64  menation.** used
291f7 20 69 6e 20 53 51 4c 69 74 65 2e 20 20 57 65 27   in SQLite.  We'
291f8 76 65 20 6d 6f 64 69 66 69 65 64 20 69 74 20 73  ve modified it s
291f9 6c 69 67 68 74 6c 79 20 74 6f 20 73 65 72 76 65  lightly to serve
291fa 20 61 73 20 61 20 73 74 61 6e 64 61 6c 6f 6e 65   as a standalone
291fb 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20 69  .** hash table i
291fc 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f  mplementation fo
291fd 72 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 20  r the full-text 
291fe 69 6e 64 65 78 69 6e 67 20 6d 6f 64 75 6c 65 2e  indexing module.
291ff 0a 2a 2a 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f  .**.*/.#ifndef _
29200 46 54 53 33 5f 48 41 53 48 5f 48 5f 0a 23 64 65  FTS3_HASH_H_.#de
29201 66 69 6e 65 20 5f 46 54 53 33 5f 48 41 53 48 5f  fine _FTS3_HASH_
29202 48 5f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64  H_../* Forward d
29203 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 73  eclarations of s
29204 74 72 75 63 74 75 72 65 73 2e 20 2a 2f 0a 74 79  tructures. */.ty
29205 70 65 64 65 66 20 73 74 72 75 63 74 20 66 74 73  pedef struct fts
29206 33 48 61 73 68 20 66 74 73 33 48 61 73 68 3b 0a  3Hash fts3Hash;.
29207 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 66  typedef struct f
29208 74 73 33 48 61 73 68 45 6c 65 6d 20 66 74 73 33  ts3HashElem fts3
29209 48 61 73 68 45 6c 65 6d 3b 0a 0a 2f 2a 20 41 20  HashElem;../* A 
2920a 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 74 61  complete hash ta
2920b 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e  ble is an instan
2920c 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
2920d 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ing structure..*
2920e 2a 20 54 68 65 20 69 6e 74 65 72 6e 61 6c 73 20  * The internals 
2920f 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
29210 65 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74  e are intended t
29211 6f 20 62 65 20 6f 70 61 71 75 65 20 2d 2d 20 63  o be opaque -- c
29212 6c 69 65 6e 74 0a 2a 2a 20 63 6f 64 65 20 73 68  lient.** code sh
29213 6f 75 6c 64 20 6e 6f 74 20 61 74 74 65 6d 70 74  ould not attempt
29214 20 74 6f 20 61 63 63 65 73 73 20 6f 72 20 6d 6f   to access or mo
29215 64 69 66 79 20 74 68 65 20 66 69 65 6c 64 73 20  dify the fields 
29216 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
29217 65 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2e 20 20  e.** directly.  
29218 43 68 61 6e 67 65 20 74 68 69 73 20 73 74 72 75  Change this stru
29219 63 74 75 72 65 20 6f 6e 6c 79 20 62 79 20 75 73  cture only by us
2921a 69 6e 67 20 74 68 65 20 72 6f 75 74 69 6e 65 73  ing the routines
2921b 20 62 65 6c 6f 77 2e 0a 2a 2a 20 48 6f 77 65 76   below..** Howev
2921c 65 72 2c 20 6d 61 6e 79 20 6f 66 20 74 68 65 20  er, many of the 
2921d 22 70 72 6f 63 65 64 75 72 65 73 22 20 61 6e 64  "procedures" and
2921e 20 22 66 75 6e 63 74 69 6f 6e 73 22 20 66 6f 72   "functions" for
2921f 20 6d 6f 64 69 66 79 69 6e 67 20 61 6e 64 0a 2a   modifying and.*
29220 2a 20 61 63 63 65 73 73 69 6e 67 20 74 68 69 73  * accessing this
29221 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 72   structure are r
29222 65 61 6c 6c 79 20 6d 61 63 72 6f 73 2c 20 73 6f  eally macros, so
29223 20 77 65 20 63 61 6e 27 74 20 72 65 61 6c 6c 79   we can't really
29224 20 6d 61 6b 65 0a 2a 2a 20 74 68 69 73 20 73 74   make.** this st
29225 72 75 63 74 75 72 65 20 6f 70 61 71 75 65 2e 0a  ructure opaque..
29226 2a 2f 0a 73 74 72 75 63 74 20 66 74 73 33 48 61  */.struct fts3Ha
29227 73 68 20 7b 0a 20 20 63 68 61 72 20 6b 65 79 43  sh {.  char keyC
29228 6c 61 73 73 3b 20 20 20 20 20 20 20 20 20 20 2f  lass;          /
29229 2a 20 48 41 53 48 5f 49 4e 54 2c 20 5f 50 4f 49  * HASH_INT, _POI
2922a 4e 54 45 52 2c 20 5f 53 54 52 49 4e 47 2c 20 5f  NTER, _STRING, _
2922b 42 49 4e 41 52 59 20 2a 2f 0a 20 20 63 68 61 72  BINARY */.  char
2922c 20 63 6f 70 79 4b 65 79 3b 20 20 20 20 20 20 20   copyKey;       
2922d 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63      /* True if c
2922e 6f 70 79 20 6f 66 20 6b 65 79 20 6d 61 64 65 20  opy of key made 
2922f 6f 6e 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20 69  on insert */.  i
29230 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20  nt count;       
29231 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
29232 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74   of entries in t
29233 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 66  his table */.  f
29234 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 66 69 72  ts3HashElem *fir
29235 73 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 66 69  st;    /* The fi
29236 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74  rst element of t
29237 68 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e  he array */.  in
29238 74 20 68 74 73 69 7a 65 3b 20 20 20 20 20 20 20  t htsize;       
29239 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2923a 6f 66 20 62 75 63 6b 65 74 73 20 69 6e 20 74 68  of buckets in th
2923b 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a  e hash table */.
2923c 20 20 73 74 72 75 63 74 20 5f 66 74 73 33 68 74    struct _fts3ht
2923d 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65   {        /* the
2923e 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20   hash table */. 
2923f 20 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20     int count;   
29240 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
29241 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
29242 20 77 69 74 68 20 74 68 69 73 20 68 61 73 68 20   with this hash 
29243 2a 2f 0a 20 20 20 20 66 74 73 33 48 61 73 68 45  */.    fts3HashE
29244 6c 65 6d 20 2a 63 68 61 69 6e 3b 20 20 20 20 20  lem *chain;     
29245 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 69  /* Pointer to fi
29246 72 73 74 20 65 6e 74 72 79 20 77 69 74 68 20 74  rst entry with t
29247 68 69 73 20 68 61 73 68 20 2a 2f 0a 20 20 7d 20  his hash */.  } 
29248 2a 68 74 3b 0a 7d 3b 0a 0a 2f 2a 20 45 61 63 68  *ht;.};../* Each
29249 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 20   element in the 
2924a 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 61 6e  hash table is an
2924b 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
2924c 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 73   following .** s
2924d 74 72 75 63 74 75 72 65 2e 20 20 41 6c 6c 20 65  tructure.  All e
2924e 6c 65 6d 65 6e 74 73 20 61 72 65 20 73 74 6f 72  lements are stor
2924f 65 64 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64  ed on a single d
29250 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73  oubly-linked lis
29251 74 2e 0a 2a 2a 0a 2a 2a 20 41 67 61 69 6e 2c 20  t..**.** Again, 
29252 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69  this structure i
29253 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65  s intended to be
29254 20 6f 70 61 71 75 65 2c 20 62 75 74 20 69 74 20   opaque, but it 
29255 63 61 6e 27 74 20 72 65 61 6c 6c 79 0a 2a 2a 20  can't really.** 
29256 62 65 20 6f 70 61 71 75 65 20 62 65 63 61 75 73  be opaque becaus
29257 65 20 69 74 20 69 73 20 75 73 65 64 20 62 79 20  e it is used by 
29258 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 73 74 72 75 63  macros..*/.struc
29259 74 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 7b  t fts3HashElem {
2925a 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20  .  fts3HashElem 
2925b 2a 6e 65 78 74 2c 20 2a 70 72 65 76 3b 20 2f 2a  *next, *prev; /*
2925c 20 4e 65 78 74 20 61 6e 64 20 70 72 65 76 69 6f   Next and previo
2925d 75 73 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74  us elements in t
2925e 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76 6f  he table */.  vo
2925f 69 64 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20  id *data;       
29260 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
29261 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
29262 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f   this element */
29263 0a 20 20 76 6f 69 64 20 2a 70 4b 65 79 3b 20 69  .  void *pKey; i
29264 6e 74 20 6e 4b 65 79 3b 20 20 20 20 20 20 2f 2a  nt nKey;      /*
29265 20 4b 65 79 20 61 73 73 6f 63 69 61 74 65 64 20   Key associated 
29266 77 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65 6e  with this elemen
29267 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  t */.};../*.** T
29268 68 65 72 65 20 61 72 65 20 32 20 64 69 66 66 65  here are 2 diffe
29269 72 65 6e 74 20 6d 6f 64 65 73 20 6f 66 20 6f 70  rent modes of op
2926a 65 72 61 74 69 6f 6e 20 66 6f 72 20 61 20 68 61  eration for a ha
2926b 73 68 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20  sh table:.**.** 
2926c 20 20 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49    FTS3_HASH_STRI
2926d 4e 47 20 20 20 20 20 20 20 20 70 4b 65 79 20 70  NG        pKey p
2926e 6f 69 6e 74 73 20 74 6f 20 61 20 73 74 72 69 6e  oints to a strin
2926f 67 20 74 68 61 74 20 69 73 20 6e 4b 65 79 20 62  g that is nKey b
29270 79 74 65 73 20 6c 6f 6e 67 0a 2a 2a 20 20 20 20  ytes long.**    
29271 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29272 20 20 20 20 20 20 20 28 69 6e 63 6c 75 64 69 6e         (includin
29273 67 20 74 68 65 20 6e 75 6c 6c 2d 74 65 72 6d 69  g the null-termi
29274 6e 61 74 6f 72 2c 20 69 66 20 61 6e 79 29 2e 20  nator, if any). 
29275 20 43 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20   Case.**        
29276 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29277 20 20 20 69 73 20 72 65 73 70 65 63 74 65 64 20     is respected 
29278 69 6e 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a  in comparisons..
29279 2a 2a 0a 2a 2a 20 20 20 46 54 53 33 5f 48 41 53  **.**   FTS3_HAS
2927a 48 5f 42 49 4e 41 52 59 20 20 20 20 20 20 20 20  H_BINARY        
2927b 70 4b 65 79 20 70 6f 69 6e 74 73 20 74 6f 20 62  pKey points to b
2927c 69 6e 61 72 79 20 64 61 74 61 20 6e 4b 65 79 20  inary data nKey 
2927d 62 79 74 65 73 20 6c 6f 6e 67 2e 20 0a 2a 2a 20  bytes long. .** 
2927e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2927f 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 6d 70            memcmp
29280 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f  () is used to co
29281 6d 70 61 72 65 20 6b 65 79 73 2e 0a 2a 2a 0a 2a  mpare keys..**.*
29282 2a 20 41 20 63 6f 70 79 20 6f 66 20 74 68 65 20  * A copy of the 
29283 6b 65 79 20 69 73 20 6d 61 64 65 20 69 66 20 74  key is made if t
29284 68 65 20 63 6f 70 79 4b 65 79 20 70 61 72 61 6d  he copyKey param
29285 65 74 65 72 20 74 6f 20 66 74 73 33 48 61 73 68  eter to fts3Hash
29286 49 6e 69 74 20 69 73 20 31 2e 20 20 0a 2a 2f 0a  Init is 1.  .*/.
29287 23 64 65 66 69 6e 65 20 46 54 53 33 5f 48 41 53  #define FTS3_HAS
29288 48 5f 53 54 52 49 4e 47 20 20 20 20 31 0a 23 64  H_STRING    1.#d
29289 65 66 69 6e 65 20 46 54 53 33 5f 48 41 53 48 5f  efine FTS3_HASH_
2928a 42 49 4e 41 52 59 20 20 20 20 32 0a 0a 2f 2a 0a  BINARY    2../*.
2928b 2a 2a 20 41 63 63 65 73 73 20 72 6f 75 74 69 6e  ** Access routin
2928c 65 73 2e 20 20 54 6f 20 64 65 6c 65 74 65 2c 20  es.  To delete, 
2928d 69 6e 73 65 72 74 20 61 20 4e 55 4c 4c 20 70 6f  insert a NULL po
2928e 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  inter..*/.SQLITE
2928f 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
29290 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 69  lite3Fts3HashIni
29291 74 28 66 74 73 33 48 61 73 68 2a 2c 20 69 6e 74  t(fts3Hash*, int
29292 20 6b 65 79 74 79 70 65 2c 20 69 6e 74 20 63 6f   keytype, int co
29293 70 79 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f 50  pyKey);.SQLITE_P
29294 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
29295 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 73 65  ite3Fts3HashInse
29296 72 74 28 66 74 73 33 48 61 73 68 2a 2c 20 63 6f  rt(fts3Hash*, co
29297 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
29298 69 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 64 20 2a  int nKey, void *
29299 70 44 61 74 61 29 3b 0a 53 51 4c 49 54 45 5f 50  pData);.SQLITE_P
2929a 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
2929b 69 74 65 33 46 74 73 33 48 61 73 68 46 69 6e 64  ite3Fts3HashFind
2929c 28 63 6f 6e 73 74 20 66 74 73 33 48 61 73 68 2a  (const fts3Hash*
2929d 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b  , const void *pK
2929e 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 3b 0a 53  ey, int nKey);.S
2929f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
292a0 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 48 61  id sqlite3Fts3Ha
292a1 73 68 43 6c 65 61 72 28 66 74 73 33 48 61 73 68  shClear(fts3Hash
292a2 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 68 6f 72 74  *);../*.** Short
292a3 68 61 6e 64 20 66 6f 72 20 74 68 65 20 66 75 6e  hand for the fun
292a4 63 74 69 6f 6e 73 20 61 62 6f 76 65 0a 2a 2f 0a  ctions above.*/.
292a5 23 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68  #define fts3Hash
292a6 49 6e 69 74 20 20 20 73 71 6c 69 74 65 33 46 74  Init   sqlite3Ft
292a7 73 33 48 61 73 68 49 6e 69 74 0a 23 64 65 66 69  s3HashInit.#defi
292a8 6e 65 20 66 74 73 33 48 61 73 68 49 6e 73 65 72  ne fts3HashInser
292a9 74 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73  t sqlite3Fts3Has
292aa 68 49 6e 73 65 72 74 0a 23 64 65 66 69 6e 65 20  hInsert.#define 
292ab 66 74 73 33 48 61 73 68 46 69 6e 64 20 20 20 73  fts3HashFind   s
292ac 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 46 69  qlite3Fts3HashFi
292ad 6e 64 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48  nd.#define fts3H
292ae 61 73 68 43 6c 65 61 72 20 20 73 71 6c 69 74 65  ashClear  sqlite
292af 33 46 74 73 33 48 61 73 68 43 6c 65 61 72 0a 0a  3Fts3HashClear..
292b0 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72  /*.** Macros for
292b1 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c   looping over al
292b2 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 61 20  l elements of a 
292b3 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68 65  hash table.  The
292b4 20 69 64 69 6f 6d 20 69 73 0a 2a 2a 20 6c 69 6b   idiom is.** lik
292b5 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
292b6 66 74 73 33 48 61 73 68 20 68 3b 0a 2a 2a 20 20  fts3Hash h;.**  
292b7 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 70   fts3HashElem *p
292b8 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20  ;.**   ....**   
292b9 66 6f 72 28 70 3d 66 74 73 33 48 61 73 68 46 69  for(p=fts3HashFi
292ba 72 73 74 28 26 68 29 3b 20 70 3b 20 70 3d 66 74  rst(&h); p; p=ft
292bb 73 33 48 61 73 68 4e 65 78 74 28 70 29 29 7b 0a  s3HashNext(p)){.
292bc 2a 2a 20 20 20 20 20 53 6f 6d 65 53 74 72 75 63  **     SomeStruc
292bd 74 75 72 65 20 2a 70 44 61 74 61 20 3d 20 66 74  ture *pData = ft
292be 73 33 48 61 73 68 44 61 74 61 28 70 29 3b 0a 2a  s3HashData(p);.*
292bf 2a 20 20 20 20 20 2f 2f 20 64 6f 20 73 6f 6d 65  *     // do some
292c0 74 68 69 6e 67 20 77 69 74 68 20 70 44 61 74 61  thing with pData
292c1 0a 2a 2a 20 20 20 7d 0a 2a 2f 0a 23 64 65 66 69  .**   }.*/.#defi
292c2 6e 65 20 66 74 73 33 48 61 73 68 46 69 72 73 74  ne fts3HashFirst
292c3 28 48 29 20 20 28 28 48 29 2d 3e 66 69 72 73 74  (H)  ((H)->first
292c4 29 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48 61  ).#define fts3Ha
292c5 73 68 4e 65 78 74 28 45 29 20 20 20 28 28 45 29  shNext(E)   ((E)
292c6 2d 3e 6e 65 78 74 29 0a 23 64 65 66 69 6e 65 20  ->next).#define 
292c7 66 74 73 33 48 61 73 68 44 61 74 61 28 45 29 20  fts3HashData(E) 
292c8 20 20 28 28 45 29 2d 3e 64 61 74 61 29 0a 23 64    ((E)->data).#d
292c9 65 66 69 6e 65 20 66 74 73 33 48 61 73 68 4b 65  efine fts3HashKe
292ca 79 28 45 29 20 20 20 20 28 28 45 29 2d 3e 70 4b  y(E)    ((E)->pK
292cb 65 79 29 0a 23 64 65 66 69 6e 65 20 66 74 73 33  ey).#define fts3
292cc 48 61 73 68 4b 65 79 73 69 7a 65 28 45 29 20 28  HashKeysize(E) (
292cd 28 45 29 2d 3e 6e 4b 65 79 29 0a 0a 2f 2a 0a 2a  (E)->nKey)../*.*
292ce 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
292cf 69 65 73 20 69 6e 20 61 20 68 61 73 68 20 74 61  ies in a hash ta
292d0 62 6c 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66  ble.*/.#define f
292d1 74 73 33 48 61 73 68 43 6f 75 6e 74 28 48 29 20  ts3HashCount(H) 
292d2 20 28 28 48 29 2d 3e 63 6f 75 6e 74 29 0a 0a 23   ((H)->count)..#
292d3 65 6e 64 69 66 20 2f 2a 20 5f 46 54 53 33 5f 48  endif /* _FTS3_H
292d4 41 53 48 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  ASH_H_ */../****
292d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
292d6 66 20 66 74 73 33 5f 68 61 73 68 2e 68 20 2a 2a  f fts3_hash.h **
292d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
292d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
292d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
292da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
292db 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
292dc 65 66 74 20 6f 66 66 20 69 6e 20 66 74 73 33 2e  eft off in fts3.
292dd 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
292de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
292df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
292e0 64 65 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65  de fts3_tokenize
292e1 72 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  r.h in the middl
292e2 65 20 6f 66 20 66 74 73 33 2e 63 20 2a 2a 2a 2a  e of fts3.c ****
292e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
292e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
292e5 20 66 69 6c 65 20 66 74 73 33 5f 74 6f 6b 65 6e   file fts3_token
292e6 69 7a 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  izer.h *********
292e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
292e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
292e9 20 32 30 30 36 20 4a 75 6c 79 20 31 30 0a 2a 2a   2006 July 10.**
292ea 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
292eb 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
292ec 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
292ed 65 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  e code..**.*****
292ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
292ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
292f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
292f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
292f2 2a 2a 2a 2a 0a 2a 2a 20 44 65 66 69 6e 65 73 20  ****.** Defines 
292f3 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f  the interface to
292f4 20 74 6f 6b 65 6e 69 7a 65 72 73 20 75 73 65 64   tokenizers used
292f5 20 62 79 20 66 75 6c 6c 74 65 78 74 2d 73 65 61   by fulltext-sea
292f6 72 63 68 2e 20 20 54 68 65 72 65 0a 2a 2a 20 61  rch.  There.** a
292f7 72 65 20 74 68 72 65 65 20 62 61 73 69 63 20 63  re three basic c
292f8 6f 6d 70 6f 6e 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a  omponents:.**.**
292f9 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
292fa 65 72 5f 6d 6f 64 75 6c 65 20 69 73 20 61 20 73  er_module is a s
292fb 69 6e 67 6c 65 74 6f 6e 20 64 65 66 69 6e 69 6e  ingleton definin
292fc 67 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 0a  g the tokenizer.
292fd 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 66 75 6e  ** interface fun
292fe 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 69 73  ctions.  This is
292ff 20 65 73 73 65 6e 74 69 61 6c 6c 79 20 74 68 65   essentially the
29300 20 63 6c 61 73 73 20 73 74 72 75 63 74 75 72 65   class structure
29301 20 66 6f 72 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 65   for.** tokenize
29302 72 73 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65  rs..**.** sqlite
29303 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 69 73 20 75  3_tokenizer is u
29304 73 65 64 20 74 6f 20 64 65 66 69 6e 65 20 61 20  sed to define a 
29305 70 61 72 74 69 63 75 6c 61 72 20 74 6f 6b 65 6e  particular token
29306 69 7a 65 72 2c 20 70 65 72 68 61 70 73 0a 2a 2a  izer, perhaps.**
29307 20 69 6e 63 6c 75 64 69 6e 67 20 63 75 73 74 6f   including custo
29308 6d 69 7a 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61  mization informa
29309 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 61 74 20  tion defined at 
2930a 63 72 65 61 74 69 6f 6e 20 74 69 6d 65 2e 0a 2a  creation time..*
2930b 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b  *.** sqlite3_tok
2930c 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 69 73  enizer_cursor is
2930d 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 61 20   generated by a 
2930e 74 6f 6b 65 6e 69 7a 65 72 20 74 6f 20 67 65 6e  tokenizer to gen
2930f 65 72 61 74 65 0a 2a 2a 20 74 6f 6b 65 6e 73 20  erate.** tokens 
29310 66 72 6f 6d 20 61 20 70 61 72 74 69 63 75 6c 61  from a particula
29311 72 20 69 6e 70 75 74 2e 0a 2a 2f 0a 23 69 66 6e  r input..*/.#ifn
29312 64 65 66 20 5f 46 54 53 33 5f 54 4f 4b 45 4e 49  def _FTS3_TOKENI
29313 5a 45 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f  ZER_H_.#define _
29314 46 54 53 33 5f 54 4f 4b 45 4e 49 5a 45 52 5f 48  FTS3_TOKENIZER_H
29315 5f 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  _../* TODO(shess
29316 29 20 4f 6e 6c 79 20 75 73 65 64 20 66 6f 72 20  ) Only used for 
29317 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 53 51  SQLITE_OK and SQ
29318 4c 49 54 45 5f 44 4f 4e 45 20 61 74 20 74 68 69  LITE_DONE at thi
29319 73 20 74 69 6d 65 2e 0a 2a 2a 20 49 66 20 74 6f  s time..** If to
2931a 6b 65 6e 69 7a 65 72 73 20 61 72 65 20 74 6f 20  kenizers are to 
2931b 62 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 63 61  be allowed to ca
2931c 6c 6c 20 73 71 6c 69 74 65 33 5f 2a 28 29 20 66  ll sqlite3_*() f
2931d 75 6e 63 74 69 6f 6e 73 2c 20 74 68 65 6e 0a 2a  unctions, then.*
2931e 2a 20 77 65 20 77 69 6c 6c 20 6e 65 65 64 20 61  * we will need a
2931f 20 77 61 79 20 74 6f 20 72 65 67 69 73 74 65 72   way to register
29320 20 74 68 65 20 41 50 49 20 63 6f 6e 73 69 73 74   the API consist
29321 65 6e 74 6c 79 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ently..*/../*.**
29322 20 53 74 72 75 63 74 75 72 65 73 20 75 73 65 64   Structures used
29323 20 62 79 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65   by the tokenize
29324 72 20 69 6e 74 65 72 66 61 63 65 2e 20 57 68 65  r interface. Whe
29325 6e 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a 65  n a new tokenize
29326 72 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  r.** implementat
29327 69 6f 6e 20 69 73 20 72 65 67 69 73 74 65 72 65  ion is registere
29328 64 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 70 72  d, the caller pr
29329 6f 76 69 64 65 73 20 61 20 70 6f 69 6e 74 65 72  ovides a pointer
2932a 20 74 6f 0a 2a 2a 20 61 6e 20 73 71 6c 69 74 65   to.** an sqlite
2932b 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
2932c 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 6f  le containing po
2932d 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 63 61  inters to the ca
2932e 6c 6c 62 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69  llback.** functi
2932f 6f 6e 73 20 74 68 61 74 20 6d 61 6b 65 20 75 70  ons that make up
29330 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69   an implementati
29331 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61  on..**.** When a
29332 6e 20 66 74 73 33 20 74 61 62 6c 65 20 69 73 20  n fts3 table is 
29333 63 72 65 61 74 65 64 2c 20 69 74 20 70 61 73 73  created, it pass
29334 65 73 20 61 6e 79 20 61 72 67 75 6d 65 6e 74 73  es any arguments
29335 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20 74 68   passed to.** th
29336 65 20 74 6f 6b 65 6e 69 7a 65 72 20 63 6c 61 75  e tokenizer clau
29337 73 65 20 6f 66 20 74 68 65 20 43 52 45 41 54 45  se of the CREATE
29338 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73   VIRTUAL TABLE s
29339 74 61 74 65 6d 65 6e 74 20 74 6f 20 74 68 65 0a  tatement to the.
2933a 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  ** sqlite3_token
2933b 69 7a 65 72 5f 6d 6f 64 75 6c 65 2e 78 43 72 65  izer_module.xCre
2933c 61 74 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 6f  ate() function o
2933d 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  f the requested 
2933e 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20 69 6d 70  tokenizer.** imp
2933f 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 54 68 65  lementation. The
29340 20 78 43 72 65 61 74 65 28 29 20 66 75 6e 63 74   xCreate() funct
29341 69 6f 6e 20 69 6e 20 74 75 72 6e 20 72 65 74 75  ion in turn retu
29342 72 6e 73 20 61 6e 20 0a 2a 2a 20 73 71 6c 69 74  rns an .** sqlit
29343 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 73 74 72  e3_tokenizer str
29344 75 63 74 75 72 65 20 72 65 70 72 65 73 65 6e 74  ucture represent
29345 69 6e 67 20 74 68 65 20 73 70 65 63 69 66 69 63  ing the specific
29346 20 74 6f 6b 65 6e 69 7a 65 72 20 74 6f 0a 2a 2a   tokenizer to.**
29347 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65   be used for the
29348 20 66 74 73 33 20 74 61 62 6c 65 20 28 63 75 73   fts3 table (cus
29349 74 6f 6d 69 7a 65 64 20 62 79 20 74 68 65 20 74  tomized by the t
2934a 6f 6b 65 6e 69 7a 65 72 20 63 6c 61 75 73 65 20  okenizer clause 
2934b 61 72 67 75 6d 65 6e 74 73 29 2e 0a 2a 2a 0a 2a  arguments)..**.*
2934c 2a 20 54 6f 20 74 6f 6b 65 6e 69 7a 65 20 61 6e  * To tokenize an
2934d 20 69 6e 70 75 74 20 62 75 66 66 65 72 2c 20 74   input buffer, t
2934e 68 65 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  he sqlite3_token
2934f 69 7a 65 72 5f 6d 6f 64 75 6c 65 2e 78 4f 70 65  izer_module.xOpe
29350 6e 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 69 73  n().** method is
29351 20 63 61 6c 6c 65 64 2e 20 49 74 20 72 65 74 75   called. It retu
29352 72 6e 73 20 61 6e 20 73 71 6c 69 74 65 33 5f 74  rns an sqlite3_t
29353 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
29354 6f 62 6a 65 63 74 0a 2a 2a 20 74 68 61 74 20 6d  object.** that m
29355 61 79 20 62 65 20 75 73 65 64 20 74 6f 20 74 6f  ay be used to to
29356 6b 65 6e 69 7a 65 20 61 20 73 70 65 63 69 66 69  kenize a specifi
29357 63 20 69 6e 70 75 74 20 62 75 66 66 65 72 20 62  c input buffer b
29358 61 73 65 64 20 6f 6e 0a 2a 2a 20 74 68 65 20 74  ased on.** the t
29359 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 72 75 6c 65  okenization rule
2935a 73 20 73 75 70 70 6c 69 65 64 20 62 79 20 61 20  s supplied by a 
2935b 73 70 65 63 69 66 69 63 20 73 71 6c 69 74 65 33  specific sqlite3
2935c 5f 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20 6f 62  _tokenizer.** ob
2935d 6a 65 63 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ject..*/.typedef
2935e 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
2935f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
29360 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
29361 65 72 5f 6d 6f 64 75 6c 65 3b 0a 74 79 70 65 64  er_module;.typed
29362 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
29363 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 73 71 6c 69  3_tokenizer sqli
29364 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 3b 0a 74  te3_tokenizer;.t
29365 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
29366 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
29367 63 75 72 73 6f 72 20 73 71 6c 69 74 65 33 5f 74  cursor sqlite3_t
29368 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 3b  okenizer_cursor;
29369 0a 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  ..struct sqlite3
2936a 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
2936b 65 20 7b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53  e {..  /*.  ** S
2936c 74 72 75 63 74 75 72 65 20 76 65 72 73 69 6f 6e  tructure version
2936d 2e 20 53 68 6f 75 6c 64 20 61 6c 77 61 79 73 20  . Should always 
2936e 62 65 20 73 65 74 20 74 6f 20 30 2e 0a 20 20 2a  be set to 0..  *
2936f 2f 0a 20 20 69 6e 74 20 69 56 65 72 73 69 6f 6e  /.  int iVersion
29370 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 72 65  ;..  /*.  ** Cre
29371 61 74 65 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69  ate a new tokeni
29372 7a 65 72 2e 20 54 68 65 20 76 61 6c 75 65 73 20  zer. The values 
29373 69 6e 20 74 68 65 20 61 72 67 76 5b 5d 20 61 72  in the argv[] ar
29374 72 61 79 20 61 72 65 20 74 68 65 0a 20 20 2a 2a  ray are the.  **
29375 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73 65   arguments passe
29376 64 20 74 6f 20 74 68 65 20 22 74 6f 6b 65 6e 69  d to the "tokeni
29377 7a 65 72 22 20 63 6c 61 75 73 65 20 6f 66 20 74  zer" clause of t
29378 68 65 20 43 52 45 41 54 45 20 56 49 52 54 55 41  he CREATE VIRTUA
29379 4c 0a 20 20 2a 2a 20 54 41 42 4c 45 20 73 74 61  L.  ** TABLE sta
2937a 74 65 6d 65 6e 74 20 74 68 61 74 20 63 72 65 61  tement that crea
2937b 74 65 64 20 74 68 65 20 66 74 73 33 20 74 61 62  ted the fts3 tab
2937c 6c 65 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  le. For example,
2937d 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 66 6f 6c   if.  ** the fol
2937e 6c 6f 77 69 6e 67 20 53 51 4c 20 69 73 20 65 78  lowing SQL is ex
2937f 65 63 75 74 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a  ecuted:.  **.  *
29380 2a 20 20 20 43 52 45 41 54 45 20 2e 2e 20 55 53  *   CREATE .. US
29381 49 4e 47 20 66 74 73 33 28 20 2e 2e 2e 20 2c 20  ING fts3( ... , 
29382 74 6f 6b 65 6e 69 7a 65 72 20 3c 74 6f 6b 65 6e  tokenizer <token
29383 69 7a 65 72 2d 6e 61 6d 65 3e 20 61 72 67 31 20  izer-name> arg1 
29384 61 72 67 32 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20  arg2).  **.  ** 
29385 74 68 65 6e 20 61 72 67 63 20 69 73 20 73 65 74  then argc is set
29386 20 74 6f 20 32 2c 20 61 6e 64 20 74 68 65 20 61   to 2, and the a
29387 72 67 76 5b 5d 20 61 72 72 61 79 20 63 6f 6e 74  rgv[] array cont
29388 61 69 6e 73 20 70 6f 69 6e 74 65 72 73 0a 20 20  ains pointers.  
29389 2a 2a 20 74 6f 20 74 68 65 20 73 74 72 69 6e 67  ** to the string
2938a 73 20 22 61 72 67 31 22 20 61 6e 64 20 22 61 72  s "arg1" and "ar
2938b 67 32 22 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  g2"..  **.  ** T
2938c 68 69 73 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c  his method shoul
2938d 64 20 72 65 74 75 72 6e 20 65 69 74 68 65 72 20  d return either 
2938e 53 51 4c 49 54 45 5f 4f 4b 20 28 30 29 2c 20 6f  SQLITE_OK (0), o
2938f 72 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f  r an SQLite erro
29390 72 20 0a 20 20 2a 2a 20 63 6f 64 65 2e 20 49 66  r .  ** code. If
29391 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
29392 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 2a 70 70  turned, then *pp
29393 54 6f 6b 65 6e 69 7a 65 72 20 73 68 6f 75 6c 64  Tokenizer should
29394 20 62 65 20 73 65 74 0a 20 20 2a 2a 20 74 6f 20   be set.  ** to 
29395 70 6f 69 6e 74 20 61 74 20 74 68 65 20 6e 65 77  point at the new
29396 6c 79 20 63 72 65 61 74 65 64 20 74 6f 6b 65 6e  ly created token
29397 69 7a 65 72 20 73 74 72 75 63 74 75 72 65 2e 20  izer structure. 
29398 54 68 65 20 67 65 6e 65 72 69 63 0a 20 20 2a 2a  The generic.  **
29399 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
2939a 65 72 2e 70 4d 6f 64 75 6c 65 20 76 61 72 69 61  er.pModule varia
2939b 62 6c 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  ble should not b
2939c 65 20 69 6e 69 74 69 61 6c 69 73 65 64 20 62 79  e initialised by
2939d 0a 20 20 2a 2a 20 74 68 69 73 20 63 61 6c 6c 62  .  ** this callb
2939e 61 63 6b 2e 20 54 68 65 20 63 61 6c 6c 65 72 20  ack. The caller 
2939f 77 69 6c 6c 20 64 6f 20 73 6f 2e 0a 20 20 2a 2f  will do so..  */
293a0 0a 20 20 69 6e 74 20 28 2a 78 43 72 65 61 74 65  .  int (*xCreate
293a1 29 28 0a 20 20 20 20 69 6e 74 20 61 72 67 63 2c  )(.    int argc,
293a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
293a3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
293a4 7a 65 20 6f 66 20 61 72 67 76 20 61 72 72 61 79  ze of argv array
293a5 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68   */.    const ch
293a6 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 20  ar *const*argv, 
293a7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
293a8 6f 6b 65 6e 69 7a 65 72 20 61 72 67 75 6d 65 6e  okenizer argumen
293a9 74 20 73 74 72 69 6e 67 73 20 2a 2f 0a 20 20 20  t strings */.   
293aa 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
293ab 65 72 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65 72  er **ppTokenizer
293ac 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 43 72 65       /* OUT: Cre
293ad 61 74 65 64 20 74 6f 6b 65 6e 69 7a 65 72 20 2a  ated tokenizer *
293ae 2f 0a 20 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  /.  );..  /*.  *
293af 2a 20 44 65 73 74 72 6f 79 20 61 6e 20 65 78 69  * Destroy an exi
293b0 73 74 69 6e 67 20 74 6f 6b 65 6e 69 7a 65 72 2e  sting tokenizer.
293b1 20 54 68 65 20 66 74 73 33 20 6d 6f 64 75 6c 65   The fts3 module
293b2 20 63 61 6c 6c 73 20 74 68 69 73 20 6d 65 74 68   calls this meth
293b3 6f 64 0a 20 20 2a 2a 20 65 78 61 63 74 6c 79 20  od.  ** exactly 
293b4 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 73 75  once for each su
293b5 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f  ccessful call to
293b6 20 78 43 72 65 61 74 65 28 29 2e 0a 20 20 2a 2f   xCreate()..  */
293b7 0a 20 20 69 6e 74 20 28 2a 78 44 65 73 74 72 6f  .  int (*xDestro
293b8 79 29 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  y)(sqlite3_token
293b9 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72  izer *pTokenizer
293ba 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 72  );..  /*.  ** Cr
293bb 65 61 74 65 20 61 20 74 6f 6b 65 6e 69 7a 65 72  eate a tokenizer
293bc 20 63 75 72 73 6f 72 20 74 6f 20 74 6f 6b 65 6e   cursor to token
293bd 69 7a 65 20 61 6e 20 69 6e 70 75 74 20 62 75 66  ize an input buf
293be 66 65 72 2e 20 54 68 65 20 63 61 6c 6c 65 72 0a  fer. The caller.
293bf 20 20 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69    ** is responsi
293c0 62 6c 65 20 66 6f 72 20 65 6e 73 75 72 69 6e 67  ble for ensuring
293c1 20 74 68 61 74 20 74 68 65 20 69 6e 70 75 74 20   that the input 
293c2 62 75 66 66 65 72 20 72 65 6d 61 69 6e 73 20 76  buffer remains v
293c3 61 6c 69 64 0a 20 20 2a 2a 20 75 6e 74 69 6c 20  alid.  ** until 
293c4 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 63 6c  the cursor is cl
293c5 6f 73 65 64 20 28 75 73 69 6e 67 20 74 68 65 20  osed (using the 
293c6 78 43 6c 6f 73 65 28 29 20 6d 65 74 68 6f 64 29  xClose() method)
293c7 2e 20 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  . .  */.  int (*
293c8 78 4f 70 65 6e 29 28 0a 20 20 20 20 73 71 6c 69  xOpen)(.    sqli
293c9 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70  te3_tokenizer *p
293ca 54 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20 20  Tokenizer,      
293cb 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 6f 62   /* Tokenizer ob
293cc 6a 65 63 74 20 2a 2f 0a 20 20 20 20 63 6f 6e 73  ject */.    cons
293cd 74 20 63 68 61 72 20 2a 70 49 6e 70 75 74 2c 20  t char *pInput, 
293ce 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20  int nBytes,     
293cf 20 2f 2a 20 49 6e 70 75 74 20 62 75 66 66 65 72   /* Input buffer
293d0 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   */.    sqlite3_
293d1 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
293d2 20 2a 2a 70 70 43 75 72 73 6f 72 20 20 2f 2a 20   **ppCursor  /* 
293d3 4f 55 54 3a 20 43 72 65 61 74 65 64 20 74 6f 6b  OUT: Created tok
293d4 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 20 2a 2f  enizer cursor */
293d5 0a 20 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  .  );..  /*.  **
293d6 20 44 65 73 74 72 6f 79 20 61 6e 20 65 78 69 73   Destroy an exis
293d7 74 69 6e 67 20 74 6f 6b 65 6e 69 7a 65 72 20 63  ting tokenizer c
293d8 75 72 73 6f 72 2e 20 54 68 65 20 66 74 73 33 20  ursor. The fts3 
293d9 6d 6f 64 75 6c 65 20 63 61 6c 6c 73 20 74 68 69  module calls thi
293da 73 20 0a 20 20 2a 2a 20 6d 65 74 68 6f 64 20 65  s .  ** method e
293db 78 61 63 74 6c 79 20 6f 6e 63 65 20 66 6f 72 20  xactly once for 
293dc 65 61 63 68 20 73 75 63 63 65 73 73 66 75 6c 20  each successful 
293dd 63 61 6c 6c 20 74 6f 20 78 4f 70 65 6e 28 29 2e  call to xOpen().
293de 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43  .  */.  int (*xC
293df 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 74 6f  lose)(sqlite3_to
293e0 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
293e1 70 43 75 72 73 6f 72 29 3b 0a 0a 20 20 2f 2a 0a  pCursor);..  /*.
293e2 20 20 2a 2a 20 52 65 74 72 69 65 76 65 20 74 68    ** Retrieve th
293e3 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f  e next token fro
293e4 6d 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20  m the tokenizer 
293e5 63 75 72 73 6f 72 20 70 43 75 72 73 6f 72 2e 20  cursor pCursor. 
293e6 54 68 69 73 0a 20 20 2a 2a 20 6d 65 74 68 6f 64  This.  ** method
293e7 20 73 68 6f 75 6c 64 20 65 69 74 68 65 72 20 72   should either r
293e8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
293e9 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75  and set the valu
293ea 65 73 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 22  es of the.  ** "
293eb 4f 55 54 22 20 76 61 72 69 61 62 6c 65 73 20 69  OUT" variables i
293ec 64 65 6e 74 69 66 69 65 64 20 62 65 6c 6f 77 2c  dentified below,
293ed 20 6f 72 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20   or SQLITE_DONE 
293ee 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74  to indicate that
293ef 0a 20 20 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66  .  ** the end of
293f0 20 74 68 65 20 62 75 66 66 65 72 20 68 61 73 20   the buffer has 
293f1 62 65 65 6e 20 72 65 61 63 68 65 64 2c 20 6f 72  been reached, or
293f2 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
293f3 20 63 6f 64 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a   code..  **.  **
293f4 20 2a 70 70 54 6f 6b 65 6e 20 73 68 6f 75 6c 64   *ppToken should
293f5 20 62 65 20 73 65 74 20 74 6f 20 70 6f 69 6e 74   be set to point
293f6 20 61 74 20 61 20 62 75 66 66 65 72 20 63 6f 6e   at a buffer con
293f7 74 61 69 6e 69 6e 67 20 74 68 65 20 0a 20 20 2a  taining the .  *
293f8 2a 20 6e 6f 72 6d 61 6c 69 7a 65 64 20 76 65 72  * normalized ver
293f9 73 69 6f 6e 20 6f 66 20 74 68 65 20 74 6f 6b 65  sion of the toke
293fa 6e 20 28 69 2e 65 2e 20 61 66 74 65 72 20 61 6e  n (i.e. after an
293fb 79 20 63 61 73 65 2d 66 6f 6c 64 69 6e 67 20 61  y case-folding a
293fc 6e 64 2f 6f 72 0a 20 20 2a 2a 20 73 74 65 6d 6d  nd/or.  ** stemm
293fd 69 6e 67 20 68 61 73 20 62 65 65 6e 20 70 65 72  ing has been per
293fe 66 6f 72 6d 65 64 29 2e 20 2a 70 6e 42 79 74 65  formed). *pnByte
293ff 73 20 73 68 6f 75 6c 64 20 62 65 20 73 65 74 20  s should be set 
29400 74 6f 20 74 68 65 20 6c 65 6e 67 74 68 0a 20 20  to the length.  
29401 2a 2a 20 6f 66 20 74 68 69 73 20 62 75 66 66 65  ** of this buffe
29402 72 20 69 6e 20 62 79 74 65 73 2e 20 54 68 65 20  r in bytes. The 
29403 69 6e 70 75 74 20 74 65 78 74 20 74 68 61 74 20  input text that 
29404 67 65 6e 65 72 61 74 65 64 20 74 68 65 20 74 6f  generated the to
29405 6b 65 6e 20 69 73 0a 20 20 2a 2a 20 69 64 65 6e  ken is.  ** iden
29406 74 69 66 69 65 64 20 62 79 20 74 68 65 20 62 79  tified by the by
29407 74 65 20 6f 66 66 73 65 74 73 20 72 65 74 75 72  te offsets retur
29408 6e 65 64 20 69 6e 20 2a 70 69 53 74 61 72 74 4f  ned in *piStartO
29409 66 66 73 65 74 20 61 6e 64 0a 20 20 2a 2a 20 2a  ffset and.  ** *
2940a 70 69 45 6e 64 4f 66 66 73 65 74 2e 0a 20 20 2a  piEndOffset..  *
2940b 2a 0a 20 20 2a 2a 20 54 68 65 20 62 75 66 66 65  *.  ** The buffe
2940c 72 20 2a 70 70 54 6f 6b 65 6e 20 69 73 20 73 65  r *ppToken is se
2940d 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 69 73  t to point at is
2940e 20 6d 61 6e 61 67 65 64 20 62 79 20 74 68 65 20   managed by the 
2940f 74 6f 6b 65 6e 69 7a 65 72 0a 20 20 2a 2a 20 69  tokenizer.  ** i
29410 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 49  mplementation. I
29411 74 20 69 73 20 6f 6e 6c 79 20 72 65 71 75 69 72  t is only requir
29412 65 64 20 74 6f 20 62 65 20 76 61 6c 69 64 20 75  ed to be valid u
29413 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 20 63 61  ntil the next ca
29414 6c 6c 0a 20 20 2a 2a 20 74 6f 20 78 4e 65 78 74  ll.  ** to xNext
29415 28 29 20 6f 72 20 78 43 6c 6f 73 65 28 29 2e 20  () or xClose(). 
29416 0a 20 20 2a 2f 0a 20 20 2f 2a 20 54 4f 44 4f 28  .  */.  /* TODO(
29417 73 68 65 73 73 29 20 63 75 72 72 65 6e 74 20 69  shess) current i
29418 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 72 65  mplementation re
29419 71 75 69 72 65 73 20 70 49 6e 70 75 74 20 74 6f  quires pInput to
2941a 20 62 65 0a 20 20 2a 2a 20 6e 75 6c 2d 74 65 72   be.  ** nul-ter
2941b 6d 69 6e 61 74 65 64 2e 20 20 54 68 69 73 20 73  minated.  This s
2941c 68 6f 75 6c 64 20 65 69 74 68 65 72 20 62 65 20  hould either be 
2941d 66 69 78 65 64 2c 20 6f 72 20 70 49 6e 70 75 74  fixed, or pInput
2941e 2f 6e 42 79 74 65 73 0a 20 20 2a 2a 20 73 68 6f  /nBytes.  ** sho
2941f 75 6c 64 20 62 65 20 63 6f 6e 76 65 72 74 65 64  uld be converted
29420 20 74 6f 20 7a 49 6e 70 75 74 2e 0a 20 20 2a 2f   to zInput..  */
29421 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78 74 29 28  .  int (*xNext)(
29422 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  .    sqlite3_tok
29423 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70  enizer_cursor *p
29424 43 75 72 73 6f 72 2c 20 20 20 2f 2a 20 54 6f 6b  Cursor,   /* Tok
29425 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 20 2a 2f  enizer cursor */
29426 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
29427 2a 2a 70 70 54 6f 6b 65 6e 2c 20 69 6e 74 20 2a  **ppToken, int *
29428 70 6e 42 79 74 65 73 2c 20 20 2f 2a 20 4f 55 54  pnBytes,  /* OUT
29429 3a 20 4e 6f 72 6d 61 6c 69 7a 65 64 20 74 65 78  : Normalized tex
2942a 74 20 66 6f 72 20 74 6f 6b 65 6e 20 2a 2f 0a 20  t for token */. 
2942b 20 20 20 69 6e 74 20 2a 70 69 53 74 61 72 74 4f     int *piStartO
2942c 66 66 73 65 74 2c 20 20 2f 2a 20 4f 55 54 3a 20  ffset,  /* OUT: 
2942d 42 79 74 65 20 6f 66 66 73 65 74 20 6f 66 20 74  Byte offset of t
2942e 6f 6b 65 6e 20 69 6e 20 69 6e 70 75 74 20 62 75  oken in input bu
2942f 66 66 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ffer */.    int 
29430 2a 70 69 45 6e 64 4f 66 66 73 65 74 2c 20 20 20  *piEndOffset,   
29431 20 2f 2a 20 4f 55 54 3a 20 42 79 74 65 20 6f 66   /* OUT: Byte of
29432 66 73 65 74 20 6f 66 20 65 6e 64 20 6f 66 20 74  fset of end of t
29433 6f 6b 65 6e 20 69 6e 20 69 6e 70 75 74 20 62 75  oken in input bu
29434 66 66 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ffer */.    int 
29435 2a 70 69 50 6f 73 69 74 69 6f 6e 20 20 20 20 20  *piPosition     
29436 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20   /* OUT: Number 
29437 6f 66 20 74 6f 6b 65 6e 73 20 72 65 74 75 72 6e  of tokens return
29438 65 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 6f  ed before this o
29439 6e 65 20 2a 2f 0a 20 20 29 3b 0a 7d 3b 0a 0a 73  ne */.  );.};..s
2943a 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 74 6f  truct sqlite3_to
2943b 6b 65 6e 69 7a 65 72 20 7b 0a 20 20 63 6f 6e 73  kenizer {.  cons
2943c 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  t sqlite3_tokeni
2943d 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  zer_module *pMod
2943e 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20 6d 6f 64  ule;  /* The mod
2943f 75 6c 65 20 66 6f 72 20 74 68 69 73 20 74 6f 6b  ule for this tok
29440 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 2f 2a 20 54  enizer */.  /* T
29441 6f 6b 65 6e 69 7a 65 72 20 69 6d 70 6c 65 6d 65  okenizer impleme
29442 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79  ntations will ty
29443 70 69 63 61 6c 6c 79 20 61 64 64 20 61 64 64 69  pically add addi
29444 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f  tional fields */
29445 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 73 71 6c 69  .};..struct sqli
29446 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
29447 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33  rsor {.  sqlite3
29448 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b  _tokenizer *pTok
29449 65 6e 69 7a 65 72 3b 20 20 20 20 20 20 20 2f 2a  enizer;       /*
2944a 20 54 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 74   Tokenizer for t
2944b 68 69 73 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20  his cursor. */. 
2944c 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 69 6d   /* Tokenizer im
2944d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69  plementations wi
2944e 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20 61 64 64  ll typically add
2944f 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c   additional fiel
29450 64 73 20 2a 2f 0a 7d 3b 0a 0a 23 65 6e 64 69 66  ds */.};..#endif
29451 20 2f 2a 20 5f 46 54 53 33 5f 54 4f 4b 45 4e 49   /* _FTS3_TOKENI
29452 5a 45 52 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  ZER_H_ */../****
29453 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
29454 66 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72  f fts3_tokenizer
29455 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
29456 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29457 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
29458 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
29459 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
2945a 65 66 74 20 6f 66 66 20 69 6e 20 66 74 73 33 2e  eft off in fts3.
2945b 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2945c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 66 6e 64  *********/.#ifnd
2945d 65 66 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 0a  ef SQLITE_CORE .
2945e 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49    SQLITE_EXTENSI
2945f 4f 4e 5f 49 4e 49 54 31 0a 23 65 6e 64 69 66 0a  ON_INIT1.#endif.
29460 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  ../* TODO(shess)
29461 20 4d 41 4e 2c 20 74 68 69 73 20 74 68 69 6e 67   MAN, this thing
29462 20 6e 65 65 64 73 20 73 6f 6d 65 20 72 65 66 61   needs some refa
29463 63 74 6f 72 69 6e 67 2e 20 20 41 74 20 6d 69 6e  ctoring.  At min
29464 69 6d 75 6d 2c 20 69 74 0a 2a 2a 20 77 6f 75 6c  imum, it.** woul
29465 64 20 62 65 20 6e 69 63 65 20 74 6f 20 6f 72 64  d be nice to ord
29466 65 72 20 74 68 65 20 66 69 6c 65 20 62 65 74 74  er the file bett
29467 65 72 2c 20 70 65 72 68 61 70 73 20 73 6f 6d 65  er, perhaps some
29468 74 68 69 6e 67 20 61 6c 6f 6e 67 20 74 68 65 0a  thing along the.
29469 2a 2a 20 6c 69 6e 65 73 20 6f 66 3a 0a 2a 2a 0a  ** lines of:.**.
2946a 2a 2a 20 20 2d 20 75 74 69 6c 69 74 79 20 66 75  **  - utility fu
2946b 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 2d 20 74 61  nctions.**  - ta
2946c 62 6c 65 20 73 65 74 75 70 20 66 75 6e 63 74 69  ble setup functi
2946d 6f 6e 73 0a 2a 2a 20 20 2d 20 74 61 62 6c 65 20  ons.**  - table 
2946e 75 70 64 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  update functions
2946f 0a 2a 2a 20 20 2d 20 74 61 62 6c 65 20 71 75 65  .**  - table que
29470 72 79 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 0a  ry functions.**.
29471 2a 2a 20 50 75 74 20 74 68 65 20 71 75 65 72 79  ** Put the query
29472 20 66 75 6e 63 74 69 6f 6e 73 20 6c 61 73 74 20   functions last 
29473 62 65 63 61 75 73 65 20 74 68 65 79 27 72 65 20  because they're 
29474 6c 69 6b 65 6c 79 20 74 6f 20 72 65 66 65 72 65  likely to refere
29475 6e 63 65 0a 2a 2a 20 74 79 70 65 64 65 66 73 20  nce.** typedefs 
29476 6f 72 20 66 75 6e 63 74 69 6f 6e 73 20 66 72 6f  or functions fro
29477 6d 20 74 68 65 20 74 61 62 6c 65 20 75 70 64 61  m the table upda
29478 74 65 20 73 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 0a  te section..*/..
29479 23 69 66 20 30 0a 23 20 64 65 66 69 6e 65 20 46  #if 0.# define F
2947a 54 53 54 52 41 43 45 28 41 29 20 20 70 72 69 6e  TSTRACE(A)  prin
2947b 74 66 20 41 3b 20 66 66 6c 75 73 68 28 73 74 64  tf A; fflush(std
2947c 6f 75 74 29 0a 23 65 6c 73 65 0a 23 20 64 65 66  out).#else.# def
2947d 69 6e 65 20 46 54 53 54 52 41 43 45 28 41 29 0a  ine FTSTRACE(A).
2947e 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65  #endif../*.** De
2947f 66 61 75 6c 74 20 73 70 61 6e 20 66 6f 72 20 4e  fault span for N
29480 45 41 52 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a  EAR operators..*
29481 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
29482 5f 46 54 53 33 5f 44 45 46 41 55 4c 54 5f 4e 45  _FTS3_DEFAULT_NE
29483 41 52 5f 50 41 52 41 4d 20 31 30 0a 0a 2f 2a 20  AR_PARAM 10../* 
29484 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74  It is not safe t
29485 6f 20 63 61 6c 6c 20 69 73 73 70 61 63 65 28 29  o call isspace()
29486 2c 20 74 6f 6c 6f 77 65 72 28 29 2c 20 6f 72 20  , tolower(), or 
29487 69 73 61 6c 6e 75 6d 28 29 20 6f 6e 0a 2a 2a 20  isalnum() on.** 
29488 68 69 2d 62 69 74 2d 73 65 74 20 63 68 61 72 61  hi-bit-set chara
29489 63 74 65 72 73 2e 20 20 54 68 69 73 20 69 73 20  cters.  This is 
2948a 74 68 65 20 73 61 6d 65 20 73 6f 6c 75 74 69 6f  the same solutio
2948b 6e 20 75 73 65 64 20 69 6e 20 74 68 65 0a 2a 2a  n used in the.**
2948c 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2f 0a 2f   tokenizer..*/./
2948d 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68  * TODO(shess) Th
2948e 65 20 73 6e 69 70 70 65 74 2d 67 65 6e 65 72 61  e snippet-genera
2948f 74 69 6f 6e 20 63 6f 64 65 20 73 68 6f 75 6c 64  tion code should
29490 20 62 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a   be using the.**
29491 20 74 6f 6b 65 6e 69 7a 65 72 2d 67 65 6e 65 72   tokenizer-gener
29492 61 74 65 64 20 74 6f 6b 65 6e 73 20 72 61 74 68  ated tokens rath
29493 65 72 20 74 68 61 6e 20 64 6f 69 6e 67 20 69 74  er than doing it
29494 73 20 6f 77 6e 20 6c 6f 63 61 6c 0a 2a 2a 20 74  s own local.** t
29495 6f 6b 65 6e 69 7a 61 74 69 6f 6e 2e 0a 2a 2f 0a  okenization..*/.
29496 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 49  /* TODO(shess) I
29497 73 20 5f 5f 69 73 61 73 63 69 69 28 29 20 61 20  s __isascii() a 
29498 70 6f 72 74 61 62 6c 65 20 76 65 72 73 69 6f 6e  portable version
29499 20 6f 66 20 28 63 26 30 78 38 30 29 3d 3d 30 3f   of (c&0x80)==0?
2949a 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73   */.static int s
2949b 61 66 65 5f 69 73 73 70 61 63 65 28 63 68 61 72  afe_isspace(char
2949c 20 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 63   c){.  return (c
2949d 26 30 78 38 30 29 3d 3d 30 20 3f 20 69 73 73 70  &0x80)==0 ? issp
2949e 61 63 65 28 63 29 20 3a 20 30 3b 0a 7d 0a 73 74  ace(c) : 0;.}.st
2949f 61 74 69 63 20 69 6e 74 20 73 61 66 65 5f 74 6f  atic int safe_to
294a0 6c 6f 77 65 72 28 63 68 61 72 20 63 29 7b 0a 20  lower(char c){. 
294a1 20 72 65 74 75 72 6e 20 28 63 26 30 78 38 30 29   return (c&0x80)
294a2 3d 3d 30 20 3f 20 74 6f 6c 6f 77 65 72 28 63 29  ==0 ? tolower(c)
294a3 20 3a 20 63 3b 0a 7d 0a 73 74 61 74 69 63 20 69   : c;.}.static i
294a4 6e 74 20 73 61 66 65 5f 69 73 61 6c 6e 75 6d 28  nt safe_isalnum(
294a5 63 68 61 72 20 63 29 7b 0a 20 20 72 65 74 75 72  char c){.  retur
294a6 6e 20 28 63 26 30 78 38 30 29 3d 3d 30 20 3f 20  n (c&0x80)==0 ? 
294a7 69 73 61 6c 6e 75 6d 28 63 29 20 3a 20 30 3b 0a  isalnum(c) : 0;.
294a8 7d 0a 0a 74 79 70 65 64 65 66 20 65 6e 75 6d 20  }..typedef enum 
294a9 44 6f 63 4c 69 73 74 54 79 70 65 20 7b 0a 20 20  DocListType {.  
294aa 44 4c 5f 44 4f 43 49 44 53 2c 20 20 20 20 20 20  DL_DOCIDS,      
294ab 20 20 20 20 20 20 20 20 2f 2a 20 64 6f 63 69 64          /* docid
294ac 73 20 6f 6e 6c 79 20 2a 2f 0a 20 20 44 4c 5f 50  s only */.  DL_P
294ad 4f 53 49 54 49 4f 4e 53 2c 20 20 20 20 20 20 20  OSITIONS,       
294ae 20 20 20 20 2f 2a 20 64 6f 63 69 64 73 20 2b 20      /* docids + 
294af 70 6f 73 69 74 69 6f 6e 73 20 2a 2f 0a 20 20 44  positions */.  D
294b0 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53  L_POSITIONS_OFFS
294b1 45 54 53 20 20 20 20 2f 2a 20 64 6f 63 69 64 73  ETS    /* docids
294b2 20 2b 20 70 6f 73 69 74 69 6f 6e 73 20 2b 20 6f   + positions + o
294b3 66 66 73 65 74 73 20 2a 2f 0a 7d 20 44 6f 63 4c  ffsets */.} DocL
294b4 69 73 74 54 79 70 65 3b 0a 0a 2f 2a 0a 2a 2a 20  istType;../*.** 
294b5 42 79 20 64 65 66 61 75 6c 74 2c 20 6f 6e 6c 79  By default, only
294b6 20 70 6f 73 69 74 69 6f 6e 73 20 61 6e 64 20 6e   positions and n
294b7 6f 74 20 6f 66 66 73 65 74 73 20 61 72 65 20 73  ot offsets are s
294b8 74 6f 72 65 64 20 69 6e 20 74 68 65 20 64 6f 63  tored in the doc
294b9 6c 69 73 74 73 2e 0a 2a 2a 20 54 6f 20 63 68 61  lists..** To cha
294ba 6e 67 65 20 74 68 69 73 20 73 6f 20 74 68 61 74  nge this so that
294bb 20 6f 66 66 73 65 74 73 20 61 72 65 20 73 74 6f   offsets are sto
294bc 72 65 64 20 74 6f 6f 2c 20 63 6f 6d 70 69 6c 65  red too, compile
294bd 20 77 69 74 68 0a 2a 2a 0a 2a 2a 20 20 20 20 20   with.**.**     
294be 20 20 20 20 20 2d 44 44 4c 5f 44 45 46 41 55 4c       -DDL_DEFAUL
294bf 54 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f  T=DL_POSITIONS_O
294c0 46 46 53 45 54 53 0a 2a 2a 0a 2a 2a 20 49 66 20  FFSETS.**.** If 
294c1 44 4c 5f 44 45 46 41 55 4c 54 20 69 73 20 73 65  DL_DEFAULT is se
294c2 74 20 74 6f 20 44 4c 5f 44 4f 43 49 44 53 2c 20  t to DL_DOCIDS, 
294c3 79 6f 75 72 20 74 61 62 6c 65 20 63 61 6e 20 6f  your table can o
294c4 6e 6c 79 20 62 65 20 69 6e 73 65 72 74 65 64 0a  nly be inserted.
294c5 2a 2a 20 69 6e 74 6f 20 28 6e 6f 20 64 65 6c 65  ** into (no dele
294c6 74 65 73 20 6f 72 20 75 70 64 61 74 65 73 29 2e  tes or updates).
294c7 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 44 4c 5f 44  .*/.#ifndef DL_D
294c8 45 46 41 55 4c 54 0a 23 20 64 65 66 69 6e 65 20  EFAULT.# define 
294c9 44 4c 5f 44 45 46 41 55 4c 54 20 44 4c 5f 50 4f  DL_DEFAULT DL_PO
294ca 53 49 54 49 4f 4e 53 0a 23 65 6e 64 69 66 0a 0a  SITIONS.#endif..
294cb 65 6e 75 6d 20 7b 0a 20 20 50 4f 53 5f 45 4e 44  enum {.  POS_END
294cc 20 3d 20 30 2c 20 20 20 20 20 20 20 20 2f 2a 20   = 0,        /* 
294cd 65 6e 64 20 6f 66 20 74 68 69 73 20 70 6f 73 69  end of this posi
294ce 74 69 6f 6e 20 6c 69 73 74 20 2a 2f 0a 20 20 50  tion list */.  P
294cf 4f 53 5f 43 4f 4c 55 4d 4e 2c 20 20 20 20 20 20  OS_COLUMN,      
294d0 20 20 20 2f 2a 20 66 6f 6c 6c 6f 77 65 64 20 62     /* followed b
294d1 79 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 6e 75 6d  y new column num
294d2 62 65 72 20 2a 2f 0a 20 20 50 4f 53 5f 42 41 53  ber */.  POS_BAS
294d3 45 0a 7d 3b 0a 0a 2f 2a 20 4d 45 52 47 45 5f 43  E.};../* MERGE_C
294d4 4f 55 4e 54 20 63 6f 6e 74 72 6f 6c 73 20 68 6f  OUNT controls ho
294d5 77 20 6f 66 74 65 6e 20 77 65 20 6d 65 72 67 65  w often we merge
294d6 20 73 65 67 6d 65 6e 74 73 20 28 73 65 65 20 63   segments (see c
294d7 6f 6d 6d 65 6e 74 20 61 74 0a 2a 2a 20 74 6f 70  omment at.** top
294d8 20 6f 66 20 66 69 6c 65 29 2e 0a 2a 2f 0a 23 64   of file)..*/.#d
294d9 65 66 69 6e 65 20 4d 45 52 47 45 5f 43 4f 55 4e  efine MERGE_COUN
294da 54 20 31 36 0a 0a 2f 2a 20 75 74 69 6c 69 74 79  T 16../* utility
294db 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 0a 2f   functions */../
294dc 2a 20 43 4c 45 41 52 28 29 20 61 6e 64 20 53 43  * CLEAR() and SC
294dd 52 41 4d 42 4c 45 28 29 20 61 62 73 74 72 61 63  RAMBLE() abstrac
294de 74 20 6d 65 6d 73 65 74 28 29 20 6f 6e 20 61 20  t memset() on a 
294df 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 69 6e  pointer to a sin
294e0 67 6c 65 0a 2a 2a 20 72 65 63 6f 72 64 20 74 6f  gle.** record to
294e1 20 70 72 65 76 65 6e 74 20 65 72 72 6f 72 73 20   prevent errors 
294e2 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a  of the form:.**.
294e3 2a 2a 20 6d 79 5f 66 75 6e 63 74 69 6f 6e 28 53  ** my_function(S
294e4 6f 6d 65 54 79 70 65 20 2a 62 29 7b 0a 2a 2a 20  omeType *b){.** 
294e5 20 20 6d 65 6d 73 65 74 28 62 2c 20 27 5c 30 27    memset(b, '\0'
294e6 2c 20 73 69 7a 65 6f 66 28 62 29 29 3b 20 20 2f  , sizeof(b));  /
294e7 2f 20 73 69 7a 65 6f 66 28 62 29 21 3d 73 69 7a  / sizeof(b)!=siz
294e8 65 6f 66 28 2a 62 29 0a 2a 2a 20 7d 0a 2a 2f 0a  eof(*b).** }.*/.
294e9 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 4f  /* TODO(shess) O
294ea 62 76 69 6f 75 73 20 63 61 6e 64 69 64 61 74 65  bvious candidate
294eb 73 20 66 6f 72 20 61 20 68 65 61 64 65 72 20 66  s for a header f
294ec 69 6c 65 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ile. */.#define 
294ed 43 4c 45 41 52 28 62 29 20 6d 65 6d 73 65 74 28  CLEAR(b) memset(
294ee 62 2c 20 27 5c 30 27 2c 20 73 69 7a 65 6f 66 28  b, '\0', sizeof(
294ef 2a 28 62 29 29 29 0a 0a 23 69 66 6e 64 65 66 20  *(b)))..#ifndef 
294f0 4e 44 45 42 55 47 0a 23 20 20 64 65 66 69 6e 65  NDEBUG.#  define
294f1 20 53 43 52 41 4d 42 4c 45 28 62 29 20 6d 65 6d   SCRAMBLE(b) mem
294f2 73 65 74 28 62 2c 20 30 78 35 35 2c 20 73 69 7a  set(b, 0x55, siz
294f3 65 6f 66 28 2a 28 62 29 29 29 0a 23 65 6c 73 65  eof(*(b))).#else
294f4 0a 23 20 20 64 65 66 69 6e 65 20 53 43 52 41 4d  .#  define SCRAM
294f5 42 4c 45 28 62 29 0a 23 65 6e 64 69 66 0a 0a 2f  BLE(b).#endif../
294f6 2a 20 57 65 20 6d 61 79 20 6e 65 65 64 20 75 70  * We may need up
294f7 20 74 6f 20 56 41 52 49 4e 54 5f 4d 41 58 20 62   to VARINT_MAX b
294f8 79 74 65 73 20 74 6f 20 73 74 6f 72 65 20 61 6e  ytes to store an
294f9 20 65 6e 63 6f 64 65 64 20 36 34 2d 62 69 74 20   encoded 64-bit 
294fa 69 6e 74 65 67 65 72 2e 20 2a 2f 0a 23 64 65 66  integer. */.#def
294fb 69 6e 65 20 56 41 52 49 4e 54 5f 4d 41 58 20 31  ine VARINT_MAX 1
294fc 30 0a 0a 2f 2a 20 57 72 69 74 65 20 61 20 36 34  0../* Write a 64
294fd 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65  -bit variable-le
294fe 6e 67 74 68 20 69 6e 74 65 67 65 72 20 74 6f 20  ngth integer to 
294ff 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20  memory starting 
29500 61 74 20 70 5b 30 5d 2e 0a 20 2a 20 54 68 65 20  at p[0].. * The 
29501 6c 65 6e 67 74 68 20 6f 66 20 64 61 74 61 20 77  length of data w
29502 72 69 74 74 65 6e 20 77 69 6c 6c 20 62 65 20 62  ritten will be b
29503 65 74 77 65 65 6e 20 31 20 61 6e 64 20 56 41 52  etween 1 and VAR
29504 49 4e 54 5f 4d 41 58 20 62 79 74 65 73 2e 0a 20  INT_MAX bytes.. 
29505 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * The number of 
29506 62 79 74 65 73 20 77 72 69 74 74 65 6e 20 69 73  bytes written is
29507 20 72 65 74 75 72 6e 65 64 2e 20 2a 2f 0a 73 74   returned. */.st
29508 61 74 69 63 20 69 6e 74 20 66 74 73 33 50 75 74  atic int fts3Put
29509 56 61 72 69 6e 74 28 63 68 61 72 20 2a 70 2c 20  Varint(char *p, 
2950a 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 76 29 7b  sqlite_int64 v){
2950b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
2950c 20 2a 71 20 3d 20 28 75 6e 73 69 67 6e 65 64 20   *q = (unsigned 
2950d 63 68 61 72 20 2a 29 20 70 3b 0a 20 20 73 71 6c  char *) p;.  sql
2950e 69 74 65 5f 75 69 6e 74 36 34 20 76 75 20 3d 20  ite_uint64 vu = 
2950f 76 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 2a 71 2b  v;.  do{.    *q+
29510 2b 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  + = (unsigned ch
29511 61 72 29 20 28 28 76 75 20 26 20 30 78 37 66 29  ar) ((vu & 0x7f)
29512 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 20 76 75   | 0x80);.    vu
29513 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65   >>= 7;.  }while
29514 28 20 76 75 21 3d 30 20 29 3b 0a 20 20 71 5b 2d  ( vu!=0 );.  q[-
29515 31 5d 20 26 3d 20 30 78 37 66 3b 20 20 2f 2a 20  1] &= 0x7f;  /* 
29516 74 75 72 6e 20 6f 66 66 20 68 69 67 68 20 62 69  turn off high bi
29517 74 20 69 6e 20 66 69 6e 61 6c 20 62 79 74 65 20  t in final byte 
29518 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 71 20 2d  */.  assert( q -
29519 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
2951a 2a 29 70 20 3c 3d 20 56 41 52 49 4e 54 5f 4d 41  *)p <= VARINT_MA
2951b 58 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 69  X );.  return (i
2951c 6e 74 29 20 28 71 20 2d 20 28 75 6e 73 69 67 6e  nt) (q - (unsign
2951d 65 64 20 63 68 61 72 20 2a 29 70 29 3b 0a 7d 0a  ed char *)p);.}.
2951e 0a 2f 2a 20 52 65 61 64 20 61 20 36 34 2d 62 69  ./* Read a 64-bi
2951f 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74  t variable-lengt
29520 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d  h integer from m
29521 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61  emory starting a
29522 74 20 70 5b 30 5d 2e 0a 20 2a 20 52 65 74 75 72  t p[0].. * Retur
29523 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
29524 62 79 74 65 73 20 72 65 61 64 2c 20 6f 72 20 30  bytes read, or 0
29525 20 6f 6e 20 65 72 72 6f 72 2e 0a 20 2a 20 54 68   on error.. * Th
29526 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65  e value is store
29527 64 20 69 6e 20 2a 76 2e 20 2a 2f 0a 73 74 61 74  d in *v. */.stat
29528 69 63 20 69 6e 74 20 66 74 73 33 47 65 74 56 61  ic int fts3GetVa
29529 72 69 6e 74 28 63 6f 6e 73 74 20 63 68 61 72 20  rint(const char 
2952a 2a 70 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  *p, sqlite_int64
2952b 20 2a 76 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e   *v){.  const un
2952c 73 69 67 6e 65 64 20 63 68 61 72 20 2a 71 20 3d  signed char *q =
2952d 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64   (const unsigned
2952e 20 63 68 61 72 20 2a 29 20 70 3b 0a 20 20 73 71   char *) p;.  sq
2952f 6c 69 74 65 5f 75 69 6e 74 36 34 20 78 20 3d 20  lite_uint64 x = 
29530 30 2c 20 79 20 3d 20 31 3b 0a 20 20 77 68 69 6c  0, y = 1;.  whil
29531 65 28 20 28 2a 71 20 26 20 30 78 38 30 29 20 3d  e( (*q & 0x80) =
29532 3d 20 30 78 38 30 20 29 7b 0a 20 20 20 20 78 20  = 0x80 ){.    x 
29533 2b 3d 20 79 20 2a 20 28 2a 71 2b 2b 20 26 20 30  += y * (*q++ & 0
29534 78 37 66 29 3b 0a 20 20 20 20 79 20 3c 3c 3d 20  x7f);.    y <<= 
29535 37 3b 0a 20 20 20 20 69 66 28 20 71 20 2d 20 28  7;.    if( q - (
29536 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
29537 70 20 3e 3d 20 56 41 52 49 4e 54 5f 4d 41 58 20  p >= VARINT_MAX 
29538 29 7b 20 20 2f 2a 20 62 61 64 20 64 61 74 61 20  ){  /* bad data 
29539 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
2953a 20 30 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75   0 );.      retu
2953b 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rn 0;.    }.  }.
2953c 20 20 78 20 2b 3d 20 79 20 2a 20 28 2a 71 2b 2b    x += y * (*q++
2953d 29 3b 0a 20 20 2a 76 20 3d 20 28 73 71 6c 69 74  );.  *v = (sqlit
2953e 65 5f 69 6e 74 36 34 29 20 78 3b 0a 20 20 72 65  e_int64) x;.  re
2953f 74 75 72 6e 20 28 69 6e 74 29 20 28 71 20 2d 20  turn (int) (q - 
29540 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
29541 29 70 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69  )p);.}..static i
29542 6e 74 20 66 74 73 33 47 65 74 56 61 72 69 6e 74  nt fts3GetVarint
29543 33 32 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  32(const char *p
29544 2c 20 69 6e 74 20 2a 70 69 29 7b 0a 20 73 71 6c  , int *pi){. sql
29545 69 74 65 5f 69 6e 74 36 34 20 69 3b 0a 20 69 6e  ite_int64 i;. in
29546 74 20 72 65 74 20 3d 20 66 74 73 33 47 65 74 56  t ret = fts3GetV
29547 61 72 69 6e 74 28 70 2c 20 26 69 29 3b 0a 20 2a  arint(p, &i);. *
29548 70 69 20 3d 20 28 69 6e 74 29 20 69 3b 0a 20 61  pi = (int) i;. a
29549 73 73 65 72 74 28 20 2a 70 69 3d 3d 69 20 29 3b  ssert( *pi==i );
2954a 0a 20 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a  . return ret;.}.
2954b 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2954c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2954d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2954e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2954f 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44 61 74 61 42 75  *****/./* DataBu
29550 66 66 65 72 20 69 73 20 75 73 65 64 20 74 6f 20  ffer is used to 
29551 63 6f 6c 6c 65 63 74 20 64 61 74 61 20 69 6e 74  collect data int
29552 6f 20 61 20 62 75 66 66 65 72 20 69 6e 20 70 69  o a buffer in pi
29553 65 63 65 6d 65 61 6c 0a 2a 2a 20 66 61 73 68 69  ecemeal.** fashi
29554 6f 6e 2e 20 20 49 74 20 69 6d 70 6c 65 6d 65 6e  on.  It implemen
29555 74 73 20 74 68 65 20 75 73 75 61 6c 20 64 69 73  ts the usual dis
29556 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65 65 6e  tinction between
29557 20 61 6d 6f 75 6e 74 20 6f 66 0a 2a 2a 20 64 61   amount of.** da
29558 74 61 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f  ta currently sto
29559 72 65 64 20 28 6e 44 61 74 61 29 20 61 6e 64 20  red (nData) and 
2955a 62 75 66 66 65 72 20 63 61 70 61 63 69 74 79 20  buffer capacity 
2955b 28 6e 43 61 70 61 63 69 74 79 29 2e 0a 2a 2a 0a  (nCapacity)..**.
2955c 2a 2a 20 64 61 74 61 42 75 66 66 65 72 49 6e 69  ** dataBufferIni
2955d 74 20 2d 20 63 72 65 61 74 65 20 61 20 62 75 66  t - create a buf
2955e 66 65 72 20 77 69 74 68 20 67 69 76 65 6e 20 69  fer with given i
2955f 6e 69 74 69 61 6c 20 63 61 70 61 63 69 74 79 2e  nitial capacity.
29560 0a 2a 2a 20 64 61 74 61 42 75 66 66 65 72 52 65  .** dataBufferRe
29561 73 65 74 20 2d 20 66 6f 72 67 65 74 20 62 75 66  set - forget buf
29562 66 65 72 27 73 20 64 61 74 61 2c 20 72 65 74 61  fer's data, reta
29563 69 6e 69 6e 67 20 63 61 70 61 63 69 74 79 2e 0a  ining capacity..
29564 2a 2a 20 64 61 74 61 42 75 66 66 65 72 44 65 73  ** dataBufferDes
29565 74 72 6f 79 20 2d 20 66 72 65 65 20 62 75 66 66  troy - free buff
29566 65 72 27 73 20 64 61 74 61 2e 0a 2a 2a 20 64 61  er's data..** da
29567 74 61 42 75 66 66 65 72 53 77 61 70 20 2d 20 73  taBufferSwap - s
29568 77 61 70 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  wap contents of 
29569 74 77 6f 20 62 75 66 66 65 72 73 2e 0a 2a 2a 20  two buffers..** 
2956a 64 61 74 61 42 75 66 66 65 72 45 78 70 61 6e 64  dataBufferExpand
2956b 20 2d 20 65 78 70 61 6e 64 20 63 61 70 61 63 69   - expand capaci
2956c 74 79 20 77 69 74 68 6f 75 74 20 61 64 64 69 6e  ty without addin
2956d 67 20 64 61 74 61 2e 0a 2a 2a 20 64 61 74 61 42  g data..** dataB
2956e 75 66 66 65 72 41 70 70 65 6e 64 20 2d 20 61 70  ufferAppend - ap
2956f 70 65 6e 64 20 64 61 74 61 2e 0a 2a 2a 20 64 61  pend data..** da
29570 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32 20  taBufferAppend2 
29571 2d 20 61 70 70 65 6e 64 20 74 77 6f 20 70 69 65  - append two pie
29572 63 65 73 20 6f 66 20 64 61 74 61 20 61 74 20 6f  ces of data at o
29573 6e 63 65 2e 0a 2a 2a 20 64 61 74 61 42 75 66 66  nce..** dataBuff
29574 65 72 52 65 70 6c 61 63 65 20 2d 20 72 65 70 6c  erReplace - repl
29575 61 63 65 20 62 75 66 66 65 72 27 73 20 64 61 74  ace buffer's dat
29576 61 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  a..*/.typedef st
29577 72 75 63 74 20 44 61 74 61 42 75 66 66 65 72 20  ruct DataBuffer 
29578 7b 0a 20 20 63 68 61 72 20 2a 70 44 61 74 61 3b  {.  char *pData;
29579 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
2957a 6e 74 65 72 20 74 6f 20 6d 61 6c 6c 6f 63 27 65  nter to malloc'e
2957b 64 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20 69  d buffer. */.  i
2957c 6e 74 20 6e 43 61 70 61 63 69 74 79 3b 20 20 20  nt nCapacity;   
2957d 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
2957e 70 44 61 74 61 20 62 75 66 66 65 72 2e 20 2a 2f  pData buffer. */
2957f 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 20 20 20  .  int nData;   
29580 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 64 20           /* End 
29581 6f 66 20 64 61 74 61 20 6c 6f 61 64 65 64 20 69  of data loaded i
29582 6e 74 6f 20 70 44 61 74 61 2e 20 2a 2f 0a 7d 20  nto pData. */.} 
29583 44 61 74 61 42 75 66 66 65 72 3b 0a 0a 73 74 61  DataBuffer;..sta
29584 74 69 63 20 76 6f 69 64 20 64 61 74 61 42 75 66  tic void dataBuf
29585 66 65 72 49 6e 69 74 28 44 61 74 61 42 75 66 66  ferInit(DataBuff
29586 65 72 20 2a 70 42 75 66 66 65 72 2c 20 69 6e 74  er *pBuffer, int
29587 20 6e 43 61 70 61 63 69 74 79 29 7b 0a 20 20 61   nCapacity){.  a
29588 73 73 65 72 74 28 20 6e 43 61 70 61 63 69 74 79  ssert( nCapacity
29589 3e 3d 30 20 29 3b 0a 20 20 70 42 75 66 66 65 72  >=0 );.  pBuffer
2958a 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20 70  ->nData = 0;.  p
2958b 42 75 66 66 65 72 2d 3e 6e 43 61 70 61 63 69 74  Buffer->nCapacit
2958c 79 20 3d 20 6e 43 61 70 61 63 69 74 79 3b 0a 20  y = nCapacity;. 
2958d 20 70 42 75 66 66 65 72 2d 3e 70 44 61 74 61 20   pBuffer->pData 
2958e 3d 20 6e 43 61 70 61 63 69 74 79 3d 3d 30 20 3f  = nCapacity==0 ?
2958f 20 4e 55 4c 4c 20 3a 20 73 71 6c 69 74 65 33 5f   NULL : sqlite3_
29590 6d 61 6c 6c 6f 63 28 6e 43 61 70 61 63 69 74 79  malloc(nCapacity
29591 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  );.}.static void
29592 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74   dataBufferReset
29593 28 44 61 74 61 42 75 66 66 65 72 20 2a 70 42 75  (DataBuffer *pBu
29594 66 66 65 72 29 7b 0a 20 20 70 42 75 66 66 65 72  ffer){.  pBuffer
29595 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a 7d 0a 73  ->nData = 0;.}.s
29596 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 61 42  tatic void dataB
29597 75 66 66 65 72 44 65 73 74 72 6f 79 28 44 61 74  ufferDestroy(Dat
29598 61 42 75 66 66 65 72 20 2a 70 42 75 66 66 65 72  aBuffer *pBuffer
29599 29 7b 0a 20 20 69 66 28 20 70 42 75 66 66 65 72  ){.  if( pBuffer
2959a 2d 3e 70 44 61 74 61 21 3d 4e 55 4c 4c 20 29 20  ->pData!=NULL ) 
2959b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 75  sqlite3_free(pBu
2959c 66 66 65 72 2d 3e 70 44 61 74 61 29 3b 0a 20 20  ffer->pData);.  
2959d 53 43 52 41 4d 42 4c 45 28 70 42 75 66 66 65 72  SCRAMBLE(pBuffer
2959e 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  );.}.static void
2959f 20 64 61 74 61 42 75 66 66 65 72 53 77 61 70 28   dataBufferSwap(
295a0 44 61 74 61 42 75 66 66 65 72 20 2a 70 42 75 66  DataBuffer *pBuf
295a1 66 65 72 31 2c 20 44 61 74 61 42 75 66 66 65 72  fer1, DataBuffer
295a2 20 2a 70 42 75 66 66 65 72 32 29 7b 0a 20 20 44   *pBuffer2){.  D
295a3 61 74 61 42 75 66 66 65 72 20 74 6d 70 20 3d 20  ataBuffer tmp = 
295a4 2a 70 42 75 66 66 65 72 31 3b 0a 20 20 2a 70 42  *pBuffer1;.  *pB
295a5 75 66 66 65 72 31 20 3d 20 2a 70 42 75 66 66 65  uffer1 = *pBuffe
295a6 72 32 3b 0a 20 20 2a 70 42 75 66 66 65 72 32 20  r2;.  *pBuffer2 
295a7 3d 20 74 6d 70 3b 0a 7d 0a 73 74 61 74 69 63 20  = tmp;.}.static 
295a8 76 6f 69 64 20 64 61 74 61 42 75 66 66 65 72 45  void dataBufferE
295a9 78 70 61 6e 64 28 44 61 74 61 42 75 66 66 65 72  xpand(DataBuffer
295aa 20 2a 70 42 75 66 66 65 72 2c 20 69 6e 74 20 6e   *pBuffer, int n
295ab 41 64 64 43 61 70 61 63 69 74 79 29 7b 0a 20 20  AddCapacity){.  
295ac 61 73 73 65 72 74 28 20 6e 41 64 64 43 61 70 61  assert( nAddCapa
295ad 63 69 74 79 3e 30 20 29 3b 0a 20 20 2f 2a 20 54  city>0 );.  /* T
295ae 4f 44 4f 28 73 68 65 73 73 29 20 43 6f 6e 73 69  ODO(shess) Consi
295af 64 65 72 20 65 78 70 61 6e 64 69 6e 67 20 6d 6f  der expanding mo
295b0 72 65 20 61 67 67 72 65 73 73 69 76 65 6c 79 2e  re aggressively.
295b1 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 0a    Note that the.
295b2 20 20 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20    ** underlying 
295b3 6d 61 6c 6c 6f 63 20 69 6d 70 6c 65 6d 65 6e 74  malloc implement
295b4 61 74 69 6f 6e 20 6d 61 79 20 74 61 6b 65 20 63  ation may take c
295b5 61 72 65 20 6f 66 20 73 75 63 68 20 74 68 69 6e  are of such thin
295b6 67 73 20 66 6f 72 0a 20 20 2a 2a 20 75 73 20 61  gs for.  ** us a
295b7 6c 72 65 61 64 79 2e 0a 20 20 2a 2f 0a 20 20 69  lready..  */.  i
295b8 66 28 20 70 42 75 66 66 65 72 2d 3e 6e 44 61 74  f( pBuffer->nDat
295b9 61 2b 6e 41 64 64 43 61 70 61 63 69 74 79 3e 70  a+nAddCapacity>p
295ba 42 75 66 66 65 72 2d 3e 6e 43 61 70 61 63 69 74  Buffer->nCapacit
295bb 79 20 29 7b 0a 20 20 20 20 70 42 75 66 66 65 72  y ){.    pBuffer
295bc 2d 3e 6e 43 61 70 61 63 69 74 79 20 3d 20 70 42  ->nCapacity = pB
295bd 75 66 66 65 72 2d 3e 6e 44 61 74 61 2b 6e 41 64  uffer->nData+nAd
295be 64 43 61 70 61 63 69 74 79 3b 0a 20 20 20 20 70  dCapacity;.    p
295bf 42 75 66 66 65 72 2d 3e 70 44 61 74 61 20 3d 20  Buffer->pData = 
295c0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
295c1 70 42 75 66 66 65 72 2d 3e 70 44 61 74 61 2c 20  pBuffer->pData, 
295c2 70 42 75 66 66 65 72 2d 3e 6e 43 61 70 61 63 69  pBuffer->nCapaci
295c3 74 79 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69  ty);.  }.}.stati
295c4 63 20 76 6f 69 64 20 64 61 74 61 42 75 66 66 65  c void dataBuffe
295c5 72 41 70 70 65 6e 64 28 44 61 74 61 42 75 66 66  rAppend(DataBuff
295c6 65 72 20 2a 70 42 75 66 66 65 72 2c 0a 20 20 20  er *pBuffer,.   
295c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
295c8 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
295c9 63 68 61 72 20 2a 70 53 6f 75 72 63 65 2c 20 69  char *pSource, i
295ca 6e 74 20 6e 53 6f 75 72 63 65 29 7b 0a 20 20 61  nt nSource){.  a
295cb 73 73 65 72 74 28 20 6e 53 6f 75 72 63 65 3e 30  ssert( nSource>0
295cc 20 26 26 20 70 53 6f 75 72 63 65 21 3d 4e 55 4c   && pSource!=NUL
295cd 4c 20 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  L );.  dataBuffe
295ce 72 45 78 70 61 6e 64 28 70 42 75 66 66 65 72 2c  rExpand(pBuffer,
295cf 20 6e 53 6f 75 72 63 65 29 3b 0a 20 20 6d 65 6d   nSource);.  mem
295d0 63 70 79 28 70 42 75 66 66 65 72 2d 3e 70 44 61  cpy(pBuffer->pDa
295d1 74 61 2b 70 42 75 66 66 65 72 2d 3e 6e 44 61 74  ta+pBuffer->nDat
295d2 61 2c 20 70 53 6f 75 72 63 65 2c 20 6e 53 6f 75  a, pSource, nSou
295d3 72 63 65 29 3b 0a 20 20 70 42 75 66 66 65 72 2d  rce);.  pBuffer-
295d4 3e 6e 44 61 74 61 20 2b 3d 20 6e 53 6f 75 72 63  >nData += nSourc
295d5 65 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  e;.}.static void
295d6 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
295d7 64 32 28 44 61 74 61 42 75 66 66 65 72 20 2a 70  d2(DataBuffer *p
295d8 42 75 66 66 65 72 2c 0a 20 20 20 20 20 20 20 20  Buffer,.        
295d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
295da 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
295db 20 2a 70 53 6f 75 72 63 65 31 2c 20 69 6e 74 20   *pSource1, int 
295dc 6e 53 6f 75 72 63 65 31 2c 0a 20 20 20 20 20 20  nSource1,.      
295dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
295de 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
295df 61 72 20 2a 70 53 6f 75 72 63 65 32 2c 20 69 6e  ar *pSource2, in
295e0 74 20 6e 53 6f 75 72 63 65 32 29 7b 0a 20 20 61  t nSource2){.  a
295e1 73 73 65 72 74 28 20 6e 53 6f 75 72 63 65 31 3e  ssert( nSource1>
295e2 30 20 26 26 20 70 53 6f 75 72 63 65 31 21 3d 4e  0 && pSource1!=N
295e3 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ULL );.  assert(
295e4 20 6e 53 6f 75 72 63 65 32 3e 30 20 26 26 20 70   nSource2>0 && p
295e5 53 6f 75 72 63 65 32 21 3d 4e 55 4c 4c 20 29 3b  Source2!=NULL );
295e6 0a 20 20 64 61 74 61 42 75 66 66 65 72 45 78 70  .  dataBufferExp
295e7 61 6e 64 28 70 42 75 66 66 65 72 2c 20 6e 53 6f  and(pBuffer, nSo
295e8 75 72 63 65 31 2b 6e 53 6f 75 72 63 65 32 29 3b  urce1+nSource2);
295e9 0a 20 20 6d 65 6d 63 70 79 28 70 42 75 66 66 65  .  memcpy(pBuffe
295ea 72 2d 3e 70 44 61 74 61 2b 70 42 75 66 66 65 72  r->pData+pBuffer
295eb 2d 3e 6e 44 61 74 61 2c 20 70 53 6f 75 72 63 65  ->nData, pSource
295ec 31 2c 20 6e 53 6f 75 72 63 65 31 29 3b 0a 20 20  1, nSource1);.  
295ed 6d 65 6d 63 70 79 28 70 42 75 66 66 65 72 2d 3e  memcpy(pBuffer->
295ee 70 44 61 74 61 2b 70 42 75 66 66 65 72 2d 3e 6e  pData+pBuffer->n
295ef 44 61 74 61 2b 6e 53 6f 75 72 63 65 31 2c 20 70  Data+nSource1, p
295f0 53 6f 75 72 63 65 32 2c 20 6e 53 6f 75 72 63 65  Source2, nSource
295f1 32 29 3b 0a 20 20 70 42 75 66 66 65 72 2d 3e 6e  2);.  pBuffer->n
295f2 44 61 74 61 20 2b 3d 20 6e 53 6f 75 72 63 65 31  Data += nSource1
295f3 2b 6e 53 6f 75 72 63 65 32 3b 0a 7d 0a 73 74 61  +nSource2;.}.sta
295f4 74 69 63 20 76 6f 69 64 20 64 61 74 61 42 75 66  tic void dataBuf
295f5 66 65 72 52 65 70 6c 61 63 65 28 44 61 74 61 42  ferReplace(DataB
295f6 75 66 66 65 72 20 2a 70 42 75 66 66 65 72 2c 0a  uffer *pBuffer,.
295f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
295f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
295f9 6e 73 74 20 63 68 61 72 20 2a 70 53 6f 75 72 63  nst char *pSourc
295fa 65 2c 20 69 6e 74 20 6e 53 6f 75 72 63 65 29 7b  e, int nSource){
295fb 0a 20 20 64 61 74 61 42 75 66 66 65 72 52 65 73  .  dataBufferRes
295fc 65 74 28 70 42 75 66 66 65 72 29 3b 0a 20 20 64  et(pBuffer);.  d
295fd 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28  ataBufferAppend(
295fe 70 42 75 66 66 65 72 2c 20 70 53 6f 75 72 63 65  pBuffer, pSource
295ff 2c 20 6e 53 6f 75 72 63 65 29 3b 0a 7d 0a 0a 2f  , nSource);.}../
29600 2a 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 69  * StringBuffer i
29601 73 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61  s a null-termina
29602 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 44  ted version of D
29603 61 74 61 42 75 66 66 65 72 2e 20 2a 2f 0a 74 79  ataBuffer. */.ty
29604 70 65 64 65 66 20 73 74 72 75 63 74 20 53 74 72  pedef struct Str
29605 69 6e 67 42 75 66 66 65 72 20 7b 0a 20 20 44 61  ingBuffer {.  Da
29606 74 61 42 75 66 66 65 72 20 62 3b 20 20 20 20 20  taBuffer b;     
29607 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 6c 75 64         /* Includ
29608 65 73 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 74  es null terminat
29609 6f 72 2e 20 2a 2f 0a 7d 20 53 74 72 69 6e 67 42  or. */.} StringB
2960a 75 66 66 65 72 3b 0a 0a 73 74 61 74 69 63 20 76  uffer;..static v
2960b 6f 69 64 20 69 6e 69 74 53 74 72 69 6e 67 42 75  oid initStringBu
2960c 66 66 65 72 28 53 74 72 69 6e 67 42 75 66 66 65  ffer(StringBuffe
2960d 72 20 2a 73 62 29 7b 0a 20 20 64 61 74 61 42 75  r *sb){.  dataBu
2960e 66 66 65 72 49 6e 69 74 28 26 73 62 2d 3e 62 2c  fferInit(&sb->b,
2960f 20 31 30 30 29 3b 0a 20 20 64 61 74 61 42 75 66   100);.  dataBuf
29610 66 65 72 52 65 70 6c 61 63 65 28 26 73 62 2d 3e  ferReplace(&sb->
29611 62 2c 20 22 22 2c 20 31 29 3b 0a 7d 0a 73 74 61  b, "", 1);.}.sta
29612 74 69 63 20 69 6e 74 20 73 74 72 69 6e 67 42 75  tic int stringBu
29613 66 66 65 72 4c 65 6e 67 74 68 28 53 74 72 69 6e  fferLength(Strin
29614 67 42 75 66 66 65 72 20 2a 73 62 29 7b 0a 20 20  gBuffer *sb){.  
29615 72 65 74 75 72 6e 20 73 62 2d 3e 62 2e 6e 44 61  return sb->b.nDa
29616 74 61 2d 31 3b 0a 7d 0a 73 74 61 74 69 63 20 63  ta-1;.}.static c
29617 68 61 72 20 2a 73 74 72 69 6e 67 42 75 66 66 65  har *stringBuffe
29618 72 44 61 74 61 28 53 74 72 69 6e 67 42 75 66 66  rData(StringBuff
29619 65 72 20 2a 73 62 29 7b 0a 20 20 72 65 74 75 72  er *sb){.  retur
2961a 6e 20 73 62 2d 3e 62 2e 70 44 61 74 61 3b 0a 7d  n sb->b.pData;.}
2961b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 74 72  .static void str
2961c 69 6e 67 42 75 66 66 65 72 44 65 73 74 72 6f 79  ingBufferDestroy
2961d 28 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a 73  (StringBuffer *s
2961e 62 29 7b 0a 20 20 64 61 74 61 42 75 66 66 65 72  b){.  dataBuffer
2961f 44 65 73 74 72 6f 79 28 26 73 62 2d 3e 62 29 3b  Destroy(&sb->b);
29620 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .}..static void 
29621 6e 61 70 70 65 6e 64 28 53 74 72 69 6e 67 42 75  nappend(StringBu
29622 66 66 65 72 20 2a 73 62 2c 20 63 6f 6e 73 74 20  ffer *sb, const 
29623 63 68 61 72 20 2a 7a 46 72 6f 6d 2c 20 69 6e 74  char *zFrom, int
29624 20 6e 46 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72   nFrom){.  asser
29625 74 28 20 73 62 2d 3e 62 2e 6e 44 61 74 61 3e 30  t( sb->b.nData>0
29626 20 29 3b 0a 20 20 69 66 28 20 6e 46 72 6f 6d 3e   );.  if( nFrom>
29627 30 20 29 7b 0a 20 20 20 20 73 62 2d 3e 62 2e 6e  0 ){.    sb->b.n
29628 44 61 74 61 2d 2d 3b 0a 20 20 20 20 64 61 74 61  Data--;.    data
29629 42 75 66 66 65 72 41 70 70 65 6e 64 32 28 26 73  BufferAppend2(&s
2962a 62 2d 3e 62 2c 20 7a 46 72 6f 6d 2c 20 6e 46 72  b->b, zFrom, nFr
2962b 6f 6d 2c 20 22 22 2c 20 31 29 3b 0a 20 20 7d 0a  om, "", 1);.  }.
2962c 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 70  }.static void ap
2962d 70 65 6e 64 28 53 74 72 69 6e 67 42 75 66 66 65  pend(StringBuffe
2962e 72 20 2a 73 62 2c 20 63 6f 6e 73 74 20 63 68 61  r *sb, const cha
2962f 72 20 2a 7a 46 72 6f 6d 29 7b 0a 20 20 6e 61 70  r *zFrom){.  nap
29630 70 65 6e 64 28 73 62 2c 20 7a 46 72 6f 6d 2c 20  pend(sb, zFrom, 
29631 73 74 72 6c 65 6e 28 7a 46 72 6f 6d 29 29 3b 0a  strlen(zFrom));.
29632 7d 0a 0a 2f 2a 20 41 70 70 65 6e 64 20 61 20 6c  }../* Append a l
29633 69 73 74 20 6f 66 20 73 74 72 69 6e 67 73 20 73  ist of strings s
29634 65 70 61 72 61 74 65 64 20 62 79 20 63 6f 6d 6d  eparated by comm
29635 61 73 2e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f  as. */.static vo
29636 69 64 20 61 70 70 65 6e 64 4c 69 73 74 28 53 74  id appendList(St
29637 72 69 6e 67 42 75 66 66 65 72 20 2a 73 62 2c 20  ringBuffer *sb, 
29638 69 6e 74 20 6e 53 74 72 69 6e 67 2c 20 63 68 61  int nString, cha
29639 72 20 2a 2a 61 7a 53 74 72 69 6e 67 29 7b 0a 20  r **azString){. 
2963a 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
2963b 30 3b 20 69 3c 6e 53 74 72 69 6e 67 3b 20 2b 2b  0; i<nString; ++
2963c 69 29 7b 0a 20 20 20 20 69 66 28 20 69 3e 30 20  i){.    if( i>0 
2963d 29 20 61 70 70 65 6e 64 28 73 62 2c 20 22 2c 20  ) append(sb, ", 
2963e 22 29 3b 0a 20 20 20 20 61 70 70 65 6e 64 28 73  ");.    append(s
2963f 62 2c 20 61 7a 53 74 72 69 6e 67 5b 69 5d 29 3b  b, azString[i]);
29640 0a 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 69  .  }.}..static i
29641 6e 74 20 65 6e 64 73 49 6e 57 68 69 74 65 53 70  nt endsInWhiteSp
29642 61 63 65 28 53 74 72 69 6e 67 42 75 66 66 65 72  ace(StringBuffer
29643 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73   *p){.  return s
29644 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e 67 74  tringBufferLengt
29645 68 28 70 29 3e 30 20 26 26 0a 20 20 20 20 73 61  h(p)>0 &&.    sa
29646 66 65 5f 69 73 73 70 61 63 65 28 73 74 72 69 6e  fe_isspace(strin
29647 67 42 75 66 66 65 72 44 61 74 61 28 70 29 5b 73  gBufferData(p)[s
29648 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e 67 74  tringBufferLengt
29649 68 28 70 29 2d 31 5d 29 3b 0a 7d 0a 0a 2f 2a 20  h(p)-1]);.}../* 
2964a 49 66 20 74 68 65 20 53 74 72 69 6e 67 42 75 66  If the StringBuf
2964b 66 65 72 20 65 6e 64 73 20 69 6e 20 73 6f 6d 65  fer ends in some
2964c 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e  thing other than
2964d 20 77 68 69 74 65 20 73 70 61 63 65 2c 20 61 64   white space, ad
2964e 64 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 73 70  d a.** single sp
2964f 61 63 65 20 63 68 61 72 61 63 74 65 72 20 74 6f  ace character to
29650 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61   the end..*/.sta
29651 74 69 63 20 76 6f 69 64 20 61 70 70 65 6e 64 57  tic void appendW
29652 68 69 74 65 53 70 61 63 65 28 53 74 72 69 6e 67  hiteSpace(String
29653 42 75 66 66 65 72 20 2a 70 29 7b 0a 20 20 69 66  Buffer *p){.  if
29654 28 20 73 74 72 69 6e 67 42 75 66 66 65 72 4c 65  ( stringBufferLe
29655 6e 67 74 68 28 70 29 3d 3d 30 20 29 20 72 65 74  ngth(p)==0 ) ret
29656 75 72 6e 3b 0a 20 20 69 66 28 20 21 65 6e 64 73  urn;.  if( !ends
29657 49 6e 57 68 69 74 65 53 70 61 63 65 28 70 29 20  InWhiteSpace(p) 
29658 29 20 61 70 70 65 6e 64 28 70 2c 20 22 20 22 29  ) append(p, " ")
29659 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 77  ;.}../* Remove w
2965a 68 69 74 65 20 73 70 61 63 65 20 66 72 6f 6d 20  hite space from 
2965b 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 53  the end of the S
2965c 74 72 69 6e 67 42 75 66 66 65 72 20 2a 2f 0a 73  tringBuffer */.s
2965d 74 61 74 69 63 20 76 6f 69 64 20 74 72 69 6d 57  tatic void trimW
2965e 68 69 74 65 53 70 61 63 65 28 53 74 72 69 6e 67  hiteSpace(String
2965f 42 75 66 66 65 72 20 2a 70 29 7b 0a 20 20 77 68  Buffer *p){.  wh
29660 69 6c 65 28 20 65 6e 64 73 49 6e 57 68 69 74 65  ile( endsInWhite
29661 53 70 61 63 65 28 70 29 20 29 7b 0a 20 20 20 20  Space(p) ){.    
29662 70 2d 3e 62 2e 70 44 61 74 61 5b 2d 2d 70 2d 3e  p->b.pData[--p->
29663 62 2e 6e 44 61 74 61 2d 31 5d 20 3d 20 27 5c 30  b.nData-1] = '\0
29664 27 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  ';.  }.}../*****
29665 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29666 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29667 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29668 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
29669 2f 2a 20 44 4c 52 65 61 64 65 72 20 69 73 20 75  /* DLReader is u
2966a 73 65 64 20 74 6f 20 72 65 61 64 20 64 6f 63 75  sed to read docu
2966b 6d 65 6e 74 20 65 6c 65 6d 65 6e 74 73 20 66 72  ment elements fr
2966c 6f 6d 20 61 20 64 6f 63 6c 69 73 74 2e 20 20 54  om a doclist.  T
2966d 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 64 6f  he.** current do
2966e 63 69 64 20 69 73 20 63 61 63 68 65 64 2c 20 73  cid is cached, s
2966f 6f 20 64 6c 72 44 6f 63 69 64 28 29 20 69 73 20  o dlrDocid() is 
29670 66 61 73 74 2e 20 20 44 4c 52 65 61 64 65 72 20  fast.  DLReader 
29671 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6f 77 6e 20  does not.** own 
29672 74 68 65 20 64 6f 63 6c 69 73 74 20 62 75 66 66  the doclist buff
29673 65 72 2e 0a 2a 2a 0a 2a 2a 20 64 6c 72 41 74 45  er..**.** dlrAtE
29674 6e 64 20 2d 20 74 72 75 65 20 69 66 20 74 68 65  nd - true if the
29675 72 65 27 73 20 6e 6f 20 6d 6f 72 65 20 64 61 74  re's no more dat
29676 61 20 74 6f 20 72 65 61 64 2e 0a 2a 2a 20 64 6c  a to read..** dl
29677 72 44 6f 63 69 64 20 2d 20 64 6f 63 69 64 20 6f  rDocid - docid o
29678 66 20 63 75 72 72 65 6e 74 20 64 6f 63 75 6d 65  f current docume
29679 6e 74 2e 0a 2a 2a 20 64 6c 72 44 6f 63 44 61 74  nt..** dlrDocDat
2967a 61 20 2d 20 64 6f 63 6c 69 73 74 20 64 61 74 61  a - doclist data
2967b 20 66 6f 72 20 63 75 72 72 65 6e 74 20 64 6f 63   for current doc
2967c 75 6d 65 6e 74 20 28 69 6e 63 6c 75 64 69 6e 67  ument (including
2967d 20 64 6f 63 69 64 29 2e 0a 2a 2a 20 64 6c 72 44   docid)..** dlrD
2967e 6f 63 44 61 74 61 42 79 74 65 73 20 2d 20 6c 65  ocDataBytes - le
2967f 6e 67 74 68 20 6f 66 20 73 61 6d 65 2e 0a 2a 2a  ngth of same..**
29680 20 64 6c 72 41 6c 6c 44 61 74 61 42 79 74 65 73   dlrAllDataBytes
29681 20 2d 20 6c 65 6e 67 74 68 20 6f 66 20 61 6c 6c   - length of all
29682 20 72 65 6d 61 69 6e 69 6e 67 20 64 61 74 61 2e   remaining data.
29683 0a 2a 2a 20 64 6c 72 50 6f 73 44 61 74 61 20 2d  .** dlrPosData -
29684 20 70 6f 73 69 74 69 6f 6e 20 64 61 74 61 20 66   position data f
29685 6f 72 20 63 75 72 72 65 6e 74 20 64 6f 63 75 6d  or current docum
29686 65 6e 74 2e 0a 2a 2a 20 64 6c 72 50 6f 73 44 61  ent..** dlrPosDa
29687 74 61 4c 65 6e 20 2d 20 6c 65 6e 67 74 68 20 6f  taLen - length o
29688 66 20 70 6f 73 20 64 61 74 61 20 66 6f 72 20 63  f pos data for c
29689 75 72 72 65 6e 74 20 64 6f 63 75 6d 65 6e 74 20  urrent document 
2968a 28 69 6e 63 6c 20 50 4f 53 5f 45 4e 44 29 2e 0a  (incl POS_END)..
2968b 2a 2a 20 64 6c 72 53 74 65 70 20 2d 20 73 74 65  ** dlrStep - ste
2968c 70 20 74 6f 20 63 75 72 72 65 6e 74 20 64 6f 63  p to current doc
2968d 75 6d 65 6e 74 2e 0a 2a 2a 20 64 6c 72 49 6e 69  ument..** dlrIni
2968e 74 20 2d 20 69 6e 69 74 69 61 6c 20 66 6f 72 20  t - initial for 
2968f 64 6f 63 6c 69 73 74 20 6f 66 20 67 69 76 65 6e  doclist of given
29690 20 74 79 70 65 20 61 67 61 69 6e 73 74 20 67 69   type against gi
29691 76 65 6e 20 64 61 74 61 2e 0a 2a 2a 20 64 6c 72  ven data..** dlr
29692 44 65 73 74 72 6f 79 20 2d 20 63 6c 65 61 6e 20  Destroy - clean 
29693 75 70 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 65 63 74  up..**.** Expect
29694 65 64 20 75 73 61 67 65 20 69 73 20 73 6f 6d 65  ed usage is some
29695 74 68 69 6e 67 20 6c 69 6b 65 3a 0a 2a 2a 0a 2a  thing like:.**.*
29696 2a 20 20 20 44 4c 52 65 61 64 65 72 20 72 65 61  *   DLReader rea
29697 64 65 72 3b 0a 2a 2a 20 20 20 64 6c 72 49 6e 69  der;.**   dlrIni
29698 74 28 26 72 65 61 64 65 72 2c 20 70 44 61 74 61  t(&reader, pData
29699 2c 20 6e 44 61 74 61 29 3b 0a 2a 2a 20 20 20 77  , nData);.**   w
2969a 68 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64 28  hile( !dlrAtEnd(
2969b 26 72 65 61 64 65 72 29 20 29 7b 0a 2a 2a 20 20  &reader) ){.**  
2969c 20 20 20 2f 2f 20 63 61 6c 6c 73 20 74 6f 20 64     // calls to d
2969d 6c 72 44 6f 63 69 64 28 29 20 61 6e 64 20 6b 69  lrDocid() and ki
2969e 6e 2e 0a 2a 2a 20 20 20 20 20 64 6c 72 53 74 65  n..**     dlrSte
2969f 70 28 26 72 65 61 64 65 72 29 3b 0a 2a 2a 20 20  p(&reader);.**  
296a0 20 7d 0a 2a 2a 20 20 20 64 6c 72 44 65 73 74 72   }.**   dlrDestr
296a1 6f 79 28 26 72 65 61 64 65 72 29 3b 0a 2a 2f 0a  oy(&reader);.*/.
296a2 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 44  typedef struct D
296a3 4c 52 65 61 64 65 72 20 7b 0a 20 20 44 6f 63 4c  LReader {.  DocL
296a4 69 73 74 54 79 70 65 20 69 54 79 70 65 3b 0a 20  istType iType;. 
296a5 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61   const char *pDa
296a6 74 61 3b 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b  ta;.  int nData;
296a7 0a 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  ..  sqlite_int64
296a8 20 69 44 6f 63 69 64 3b 0a 20 20 69 6e 74 20 6e   iDocid;.  int n
296a9 45 6c 65 6d 65 6e 74 3b 0a 7d 20 44 4c 52 65 61  Element;.} DLRea
296aa 64 65 72 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74  der;..static int
296ab 20 64 6c 72 41 74 45 6e 64 28 44 4c 52 65 61 64   dlrAtEnd(DLRead
296ac 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
296ad 61 73 73 65 72 74 28 20 70 52 65 61 64 65 72 2d  assert( pReader-
296ae 3e 6e 44 61 74 61 3e 3d 30 20 29 3b 0a 20 20 72  >nData>=0 );.  r
296af 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 6e  eturn pReader->n
296b0 44 61 74 61 3d 3d 30 3b 0a 7d 0a 73 74 61 74 69  Data==0;.}.stati
296b1 63 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 64  c sqlite_int64 d
296b2 6c 72 44 6f 63 69 64 28 44 4c 52 65 61 64 65 72  lrDocid(DLReader
296b3 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73   *pReader){.  as
296b4 73 65 72 74 28 20 21 64 6c 72 41 74 45 6e 64 28  sert( !dlrAtEnd(
296b5 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65  pReader) );.  re
296b6 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 69 44  turn pReader->iD
296b7 6f 63 69 64 3b 0a 7d 0a 73 74 61 74 69 63 20 63  ocid;.}.static c
296b8 6f 6e 73 74 20 63 68 61 72 20 2a 64 6c 72 44 6f  onst char *dlrDo
296b9 63 44 61 74 61 28 44 4c 52 65 61 64 65 72 20 2a  cData(DLReader *
296ba 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65  pReader){.  asse
296bb 72 74 28 20 21 64 6c 72 41 74 45 6e 64 28 70 52  rt( !dlrAtEnd(pR
296bc 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75  eader) );.  retu
296bd 72 6e 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74  rn pReader->pDat
296be 61 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  a;.}.static int 
296bf 64 6c 72 44 6f 63 44 61 74 61 42 79 74 65 73 28  dlrDocDataBytes(
296c0 44 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  DLReader *pReade
296c1 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 64  r){.  assert( !d
296c2 6c 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29  lrAtEnd(pReader)
296c3 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65   );.  return pRe
296c4 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 3b 0a  ader->nElement;.
296c5 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6c 72  }.static int dlr
296c6 41 6c 6c 44 61 74 61 42 79 74 65 73 28 44 4c 52  AllDataBytes(DLR
296c7 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
296c8 0a 20 20 61 73 73 65 72 74 28 20 21 64 6c 72 41  .  assert( !dlrA
296c9 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b  tEnd(pReader) );
296ca 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65  .  return pReade
296cb 72 2d 3e 6e 44 61 74 61 3b 0a 7d 0a 2f 2a 20 54  r->nData;.}./* T
296cc 4f 44 4f 28 73 68 65 73 73 29 20 43 6f 6e 73 69  ODO(shess) Consi
296cd 64 65 72 20 61 64 64 69 6e 67 20 61 20 66 69 65  der adding a fie
296ce 6c 64 20 74 6f 20 74 72 61 63 6b 20 69 44 6f 63  ld to track iDoc
296cf 69 64 20 76 61 72 69 6e 74 20 6c 65 6e 67 74 68  id varint length
296d0 0a 2a 2a 20 74 6f 20 6d 61 6b 65 20 74 68 65 73  .** to make thes
296d1 65 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20  e two functions 
296d2 66 61 73 74 65 72 2e 20 20 54 68 69 73 20 6d 69  faster.  This mi
296d3 67 68 74 20 6d 61 74 74 65 72 20 28 61 20 74 69  ght matter (a ti
296d4 6e 79 20 62 69 74 29 0a 2a 2a 20 66 6f 72 20 71  ny bit).** for q
296d5 75 65 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  ueries..*/.stati
296d6 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 6c  c const char *dl
296d7 72 50 6f 73 44 61 74 61 28 44 4c 52 65 61 64 65  rPosData(DLReade
296d8 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 73  r *pReader){.  s
296d9 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 75 6d  qlite_int64 iDum
296da 6d 79 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 66 74  my;.  int n = ft
296db 73 33 47 65 74 56 61 72 69 6e 74 28 70 52 65 61  s3GetVarint(pRea
296dc 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 69 44 75  der->pData, &iDu
296dd 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20  mmy);.  assert( 
296de 21 64 6c 72 41 74 45 6e 64 28 70 52 65 61 64 65  !dlrAtEnd(pReade
296df 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  r) );.  return p
296e0 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 3b  Reader->pData+n;
296e1 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6c  .}.static int dl
296e2 72 50 6f 73 44 61 74 61 4c 65 6e 28 44 4c 52 65  rPosDataLen(DLRe
296e3 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
296e4 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
296e5 44 75 6d 6d 79 3b 0a 20 20 69 6e 74 20 6e 20 3d  Dummy;.  int n =
296e6 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 28 70   fts3GetVarint(p
296e7 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26  Reader->pData, &
296e8 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72  iDummy);.  asser
296e9 74 28 20 21 64 6c 72 41 74 45 6e 64 28 70 52 65  t( !dlrAtEnd(pRe
296ea 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72  ader) );.  retur
296eb 6e 20 70 52 65 61 64 65 72 2d 3e 6e 45 6c 65 6d  n pReader->nElem
296ec 65 6e 74 2d 6e 3b 0a 7d 0a 73 74 61 74 69 63 20  ent-n;.}.static 
296ed 76 6f 69 64 20 64 6c 72 53 74 65 70 28 44 4c 52  void dlrStep(DLR
296ee 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
296ef 0a 20 20 61 73 73 65 72 74 28 20 21 64 6c 72 41  .  assert( !dlrA
296f0 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b  tEnd(pReader) );
296f1 0a 0a 20 20 2f 2a 20 53 6b 69 70 20 70 61 73 74  ..  /* Skip past
296f2 20 63 75 72 72 65 6e 74 20 64 6f 63 6c 69 73 74   current doclist
296f3 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 61   element. */.  a
296f4 73 73 65 72 74 28 20 70 52 65 61 64 65 72 2d 3e  ssert( pReader->
296f5 6e 45 6c 65 6d 65 6e 74 3c 3d 70 52 65 61 64 65  nElement<=pReade
296f6 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20 70 52  r->nData );.  pR
296f7 65 61 64 65 72 2d 3e 70 44 61 74 61 20 2b 3d 20  eader->pData += 
296f8 70 52 65 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e  pReader->nElemen
296f9 74 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 6e 44  t;.  pReader->nD
296fa 61 74 61 20 2d 3d 20 70 52 65 61 64 65 72 2d 3e  ata -= pReader->
296fb 6e 45 6c 65 6d 65 6e 74 3b 0a 0a 20 20 2f 2a 20  nElement;..  /* 
296fc 49 66 20 74 68 65 72 65 20 69 73 20 6d 6f 72 65  If there is more
296fd 20 64 61 74 61 2c 20 72 65 61 64 20 74 68 65 20   data, read the 
296fe 6e 65 78 74 20 64 6f 63 6c 69 73 74 20 65 6c 65  next doclist ele
296ff 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70  ment. */.  if( p
29700 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 21 3d 30  Reader->nData!=0
29701 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 5f 69   ){.    sqlite_i
29702 6e 74 36 34 20 69 44 6f 63 69 64 44 65 6c 74 61  nt64 iDocidDelta
29703 3b 0a 20 20 20 20 69 6e 74 20 69 44 75 6d 6d 79  ;.    int iDummy
29704 2c 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72  , n = fts3GetVar
29705 69 6e 74 28 70 52 65 61 64 65 72 2d 3e 70 44 61  int(pReader->pDa
29706 74 61 2c 20 26 69 44 6f 63 69 64 44 65 6c 74 61  ta, &iDocidDelta
29707 29 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  );.    pReader->
29708 69 44 6f 63 69 64 20 2b 3d 20 69 44 6f 63 69 64  iDocid += iDocid
29709 44 65 6c 74 61 3b 0a 20 20 20 20 69 66 28 20 70  Delta;.    if( p
2970a 52 65 61 64 65 72 2d 3e 69 54 79 70 65 3e 3d 44  Reader->iType>=D
2970b 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 29 7b 0a 20  L_POSITIONS ){. 
2970c 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c 70       assert( n<p
2970d 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b  Reader->nData );
2970e 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 31 20  .      while( 1 
2970f 29 7b 0a 20 20 20 20 20 20 20 20 6e 20 2b 3d 20  ){.        n += 
29710 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
29711 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e  pReader->pData+n
29712 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20  , &iDummy);.    
29713 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c 3d 70      assert( n<=p
29714 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b  Reader->nData );
29715 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 44 75  .        if( iDu
29716 6d 6d 79 3d 3d 50 4f 53 5f 45 4e 44 20 29 20 62  mmy==POS_END ) b
29717 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 69 66  reak;.        if
29718 28 20 69 44 75 6d 6d 79 3d 3d 50 4f 53 5f 43 4f  ( iDummy==POS_CO
29719 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20 20 20  LUMN ){.        
2971a 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61    n += fts3GetVa
2971b 72 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e  rint32(pReader->
2971c 70 44 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79  pData+n, &iDummy
2971d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  );.          ass
2971e 65 72 74 28 20 6e 3c 70 52 65 61 64 65 72 2d 3e  ert( n<pReader->
2971f 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 20 20 20  nData );.       
29720 20 7d 65 6c 73 65 20 69 66 28 20 70 52 65 61 64   }else if( pRead
29721 65 72 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 50 4f  er->iType==DL_PO
29722 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20  SITIONS_OFFSETS 
29723 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b  ){.          n +
29724 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33  = fts3GetVarint3
29725 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61  2(pReader->pData
29726 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20  +n, &iDummy);.  
29727 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66 74 73          n += fts
29728 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65  3GetVarint32(pRe
29729 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26  ader->pData+n, &
2972a 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20  iDummy);.       
2972b 20 20 20 61 73 73 65 72 74 28 20 6e 3c 70 52 65     assert( n<pRe
2972c 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20  ader->nData );. 
2972d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
2972e 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 65 61 64  .    }.    pRead
2972f 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 20 3d 20 6e  er->nElement = n
29730 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 52  ;.    assert( pR
29731 65 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 3c  eader->nElement<
29732 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20  =pReader->nData 
29733 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20  );.  }.}.static 
29734 76 6f 69 64 20 64 6c 72 49 6e 69 74 28 44 4c 52  void dlrInit(DLR
29735 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 2c 20  eader *pReader, 
29736 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79 70  DocListType iTyp
29737 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e,.             
29738 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
29739 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44  r *pData, int nD
2973a 61 74 61 29 7b 0a 20 20 61 73 73 65 72 74 28 20  ata){.  assert( 
2973b 70 44 61 74 61 21 3d 4e 55 4c 4c 20 26 26 20 6e  pData!=NULL && n
2973c 44 61 74 61 21 3d 30 20 29 3b 0a 20 20 70 52 65  Data!=0 );.  pRe
2973d 61 64 65 72 2d 3e 69 54 79 70 65 20 3d 20 69 54  ader->iType = iT
2973e 79 70 65 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e  ype;.  pReader->
2973f 70 44 61 74 61 20 3d 20 70 44 61 74 61 3b 0a 20  pData = pData;. 
29740 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20   pReader->nData 
29741 3d 20 6e 44 61 74 61 3b 0a 20 20 70 52 65 61 64  = nData;.  pRead
29742 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 20 3d 20 30  er->nElement = 0
29743 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 44 6f  ;.  pReader->iDo
29744 63 69 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 4c  cid = 0;..  /* L
29745 6f 61 64 20 74 68 65 20 66 69 72 73 74 20 65 6c  oad the first el
29746 65 6d 65 6e 74 27 73 20 64 61 74 61 2e 20 20 54  ement's data.  T
29747 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 20 66  here must be a f
29748 69 72 73 74 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f  irst element. */
29749 0a 20 20 64 6c 72 53 74 65 70 28 70 52 65 61 64  .  dlrStep(pRead
2974a 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  er);.}.static vo
2974b 69 64 20 64 6c 72 44 65 73 74 72 6f 79 28 44 4c  id dlrDestroy(DL
2974c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
2974d 7b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 52 65  {.  SCRAMBLE(pRe
2974e 61 64 65 72 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65  ader);.}..#ifnde
2974f 66 20 4e 44 45 42 55 47 0a 2f 2a 20 56 65 72 69  f NDEBUG./* Veri
29750 66 79 20 74 68 61 74 20 74 68 65 20 64 6f 63 6c  fy that the docl
29751 69 73 74 20 63 61 6e 20 62 65 20 76 61 6c 69 64  ist can be valid
29752 6c 79 20 64 65 63 6f 64 65 64 2e 20 20 41 6c 73  ly decoded.  Als
29753 6f 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a  o returns the.**
29754 20 6c 61 73 74 20 64 6f 63 69 64 20 66 6f 75 6e   last docid foun
29755 64 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20  d because it is 
29756 63 6f 6e 76 65 6e 69 65 6e 74 20 69 6e 20 6f 74  convenient in ot
29757 68 65 72 20 61 73 73 65 72 74 69 6f 6e 73 20 66  her assertions f
29758 6f 72 0a 2a 2a 20 44 4c 57 72 69 74 65 72 2e 0a  or.** DLWriter..
29759 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  */.static void d
2975a 6f 63 4c 69 73 74 56 61 6c 69 64 61 74 65 28 44  ocListValidate(D
2975b 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79 70 65  ocListType iType
2975c 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44  , const char *pD
2975d 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a  ata, int nData,.
2975e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2975f 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
29760 74 65 5f 69 6e 74 36 34 20 2a 70 4c 61 73 74 44  te_int64 *pLastD
29761 6f 63 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 5f  ocid){.  sqlite_
29762 69 6e 74 36 34 20 69 50 72 65 76 44 6f 63 69 64  int64 iPrevDocid
29763 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
29764 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 61 73 73  nData>0 );.  ass
29765 65 72 74 28 20 70 44 61 74 61 21 3d 30 20 29 3b  ert( pData!=0 );
29766 0a 20 20 61 73 73 65 72 74 28 20 70 44 61 74 61  .  assert( pData
29767 2b 6e 44 61 74 61 3e 70 44 61 74 61 20 29 3b 0a  +nData>pData );.
29768 20 20 77 68 69 6c 65 28 20 6e 44 61 74 61 21 3d    while( nData!=
29769 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 5f  0 ){.    sqlite_
2976a 69 6e 74 36 34 20 69 44 6f 63 69 64 44 65 6c 74  int64 iDocidDelt
2976b 61 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 66  a;.    int n = f
2976c 74 73 33 47 65 74 56 61 72 69 6e 74 28 70 44 61  ts3GetVarint(pDa
2976d 74 61 2c 20 26 69 44 6f 63 69 64 44 65 6c 74 61  ta, &iDocidDelta
2976e 29 3b 0a 20 20 20 20 69 50 72 65 76 44 6f 63 69  );.    iPrevDoci
2976f 64 20 2b 3d 20 69 44 6f 63 69 64 44 65 6c 74 61  d += iDocidDelta
29770 3b 0a 20 20 20 20 69 66 28 20 69 54 79 70 65 3e  ;.    if( iType>
29771 44 4c 5f 44 4f 43 49 44 53 20 29 7b 0a 20 20 20  DL_DOCIDS ){.   
29772 20 20 20 69 6e 74 20 69 44 75 6d 6d 79 3b 0a 20     int iDummy;. 
29773 20 20 20 20 20 77 68 69 6c 65 28 20 31 20 29 7b       while( 1 ){
29774 0a 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66 74  .        n += ft
29775 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 44  s3GetVarint32(pD
29776 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b  ata+n, &iDummy);
29777 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 44 75  .        if( iDu
29778 6d 6d 79 3d 3d 50 4f 53 5f 45 4e 44 20 29 20 62  mmy==POS_END ) b
29779 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 69 66  reak;.        if
2977a 28 20 69 44 75 6d 6d 79 3d 3d 50 4f 53 5f 43 4f  ( iDummy==POS_CO
2977b 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20 20 20  LUMN ){.        
2977c 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61    n += fts3GetVa
2977d 72 69 6e 74 33 32 28 70 44 61 74 61 2b 6e 2c 20  rint32(pData+n, 
2977e 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20  &iDummy);.      
2977f 20 20 7d 65 6c 73 65 20 69 66 28 20 69 54 79 70    }else if( iTyp
29780 65 3e 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 29  e>DL_POSITIONS )
29781 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d  {.          n +=
29782 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
29783 28 70 44 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d  (pData+n, &iDumm
29784 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 20  y);.          n 
29785 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74  += fts3GetVarint
29786 33 32 28 70 44 61 74 61 2b 6e 2c 20 26 69 44 75  32(pData+n, &iDu
29787 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  mmy);.        }.
29788 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
29789 6e 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20 20 20  n<=nData );.    
2978a 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73    }.    }.    as
2978b 73 65 72 74 28 20 6e 3c 3d 6e 44 61 74 61 20 29  sert( n<=nData )
2978c 3b 0a 20 20 20 20 70 44 61 74 61 20 2b 3d 20 6e  ;.    pData += n
2978d 3b 0a 20 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e  ;.    nData -= n
2978e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4c 61 73  ;.  }.  if( pLas
2978f 74 44 6f 63 69 64 20 29 20 2a 70 4c 61 73 74 44  tDocid ) *pLastD
29790 6f 63 69 64 20 3d 20 69 50 72 65 76 44 6f 63 69  ocid = iPrevDoci
29791 64 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 41 53 53  d;.}.#define ASS
29792 45 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c 49 53  ERT_VALID_DOCLIS
29793 54 28 69 2c 20 70 2c 20 6e 2c 20 6f 29 20 64 6f  T(i, p, n, o) do
29794 63 4c 69 73 74 56 61 6c 69 64 61 74 65 28 69 2c  cListValidate(i,
29795 20 70 2c 20 6e 2c 20 6f 29 0a 23 65 6c 73 65 0a   p, n, o).#else.
29796 23 64 65 66 69 6e 65 20 41 53 53 45 52 54 5f 56  #define ASSERT_V
29797 41 4c 49 44 5f 44 4f 43 4c 49 53 54 28 69 2c 20  ALID_DOCLIST(i, 
29798 70 2c 20 6e 2c 20 6f 29 20 61 73 73 65 72 74 28  p, n, o) assert(
29799 20 31 20 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a   1 ).#endif../**
2979a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2979b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2979c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2979d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2979e 2a 2f 0a 2f 2a 20 44 4c 57 72 69 74 65 72 20 69  */./* DLWriter i
2979f 73 20 75 73 65 64 20 74 6f 20 77 72 69 74 65 20  s used to write 
297a0 64 6f 63 6c 69 73 74 20 64 61 74 61 20 74 6f 20  doclist data to 
297a1 61 20 44 61 74 61 42 75 66 66 65 72 2e 20 20 44  a DataBuffer.  D
297a2 4c 57 72 69 74 65 72 0a 2a 2a 20 61 6c 77 61 79  LWriter.** alway
297a3 73 20 61 70 70 65 6e 64 73 20 74 6f 20 74 68 65  s appends to the
297a4 20 62 75 66 66 65 72 20 61 6e 64 20 64 6f 65 73   buffer and does
297a5 20 6e 6f 74 20 6f 77 6e 20 69 74 2e 0a 2a 2a 0a   not own it..**.
297a6 2a 2a 20 64 6c 77 49 6e 69 74 20 2d 20 69 6e 69  ** dlwInit - ini
297a7 74 69 61 6c 69 7a 65 20 74 6f 20 77 72 69 74 65  tialize to write
297a8 20 61 20 67 69 76 65 6e 20 74 79 70 65 20 64 6f   a given type do
297a9 63 6c 69 73 74 74 6f 20 61 20 62 75 66 66 65 72  clistto a buffer
297aa 2e 0a 2a 2a 20 64 6c 77 44 65 73 74 72 6f 79 20  ..** dlwDestroy 
297ab 2d 20 63 6c 65 61 72 20 74 68 65 20 77 72 69 74  - clear the writ
297ac 65 72 27 73 20 6d 65 6d 6f 72 79 2e 20 20 44 6f  er's memory.  Do
297ad 65 73 20 6e 6f 74 20 66 72 65 65 20 62 75 66 66  es not free buff
297ae 65 72 2e 0a 2a 2a 20 64 6c 77 41 70 70 65 6e 64  er..** dlwAppend
297af 20 2d 20 61 70 70 65 6e 64 20 72 61 77 20 64 6f   - append raw do
297b0 63 6c 69 73 74 20 64 61 74 61 20 74 6f 20 62 75  clist data to bu
297b1 66 66 65 72 2e 0a 2a 2a 20 64 6c 77 43 6f 70 79  ffer..** dlwCopy
297b2 20 2d 20 63 6f 70 79 20 6e 65 78 74 20 64 6f 63   - copy next doc
297b3 6c 69 73 74 20 66 72 6f 6d 20 72 65 61 64 65 72  list from reader
297b4 20 74 6f 20 77 72 69 74 65 72 2e 0a 2a 2a 20 64   to writer..** d
297b5 6c 77 41 64 64 20 2d 20 63 6f 6e 73 74 72 75 63  lwAdd - construc
297b6 74 20 64 6f 63 6c 69 73 74 20 65 6c 65 6d 65 6e  t doclist elemen
297b7 74 20 61 6e 64 20 61 70 70 65 6e 64 20 74 6f 20  t and append to 
297b8 62 75 66 66 65 72 2e 0a 2a 2a 20 20 20 20 4f 6e  buffer..**    On
297b9 6c 79 20 61 70 70 6c 79 20 64 6c 77 41 64 64 28  ly apply dlwAdd(
297ba 29 20 74 6f 20 44 4c 5f 44 4f 43 49 44 53 20 64  ) to DL_DOCIDS d
297bb 6f 63 6c 69 73 74 73 20 28 65 6c 73 65 20 75 73  oclists (else us
297bc 65 20 50 4c 57 72 69 74 65 72 29 2e 0a 2a 2f 0a  e PLWriter)..*/.
297bd 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 44  typedef struct D
297be 4c 57 72 69 74 65 72 20 7b 0a 20 20 44 6f 63 4c  LWriter {.  DocL
297bf 69 73 74 54 79 70 65 20 69 54 79 70 65 3b 0a 20  istType iType;. 
297c0 20 44 61 74 61 42 75 66 66 65 72 20 2a 62 3b 0a   DataBuffer *b;.
297c1 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
297c2 50 72 65 76 44 6f 63 69 64 3b 0a 23 69 66 6e 64  PrevDocid;.#ifnd
297c3 65 66 20 4e 44 45 42 55 47 0a 20 20 69 6e 74 20  ef NDEBUG.  int 
297c4 68 61 73 5f 69 50 72 65 76 44 6f 63 69 64 3b 0a  has_iPrevDocid;.
297c5 23 65 6e 64 69 66 0a 7d 20 44 4c 57 72 69 74 65  #endif.} DLWrite
297c6 72 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  r;..static void 
297c7 64 6c 77 49 6e 69 74 28 44 4c 57 72 69 74 65 72  dlwInit(DLWriter
297c8 20 2a 70 57 72 69 74 65 72 2c 20 44 6f 63 4c 69   *pWriter, DocLi
297c9 73 74 54 79 70 65 20 69 54 79 70 65 2c 20 44 61  stType iType, Da
297ca 74 61 42 75 66 66 65 72 20 2a 62 29 7b 0a 20 20  taBuffer *b){.  
297cb 70 57 72 69 74 65 72 2d 3e 62 20 3d 20 62 3b 0a  pWriter->b = b;.
297cc 20 20 70 57 72 69 74 65 72 2d 3e 69 54 79 70 65    pWriter->iType
297cd 20 3d 20 69 54 79 70 65 3b 0a 20 20 70 57 72 69   = iType;.  pWri
297ce 74 65 72 2d 3e 69 50 72 65 76 44 6f 63 69 64 20  ter->iPrevDocid 
297cf 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45  = 0;.#ifndef NDE
297d0 42 55 47 0a 20 20 70 57 72 69 74 65 72 2d 3e 68  BUG.  pWriter->h
297d1 61 73 5f 69 50 72 65 76 44 6f 63 69 64 20 3d 20  as_iPrevDocid = 
297d2 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 73 74 61 74  0;.#endif.}.stat
297d3 69 63 20 76 6f 69 64 20 64 6c 77 44 65 73 74 72  ic void dlwDestr
297d4 6f 79 28 44 4c 57 72 69 74 65 72 20 2a 70 57 72  oy(DLWriter *pWr
297d5 69 74 65 72 29 7b 0a 20 20 53 43 52 41 4d 42 4c  iter){.  SCRAMBL
297d6 45 28 70 57 72 69 74 65 72 29 3b 0a 7d 0a 2f 2a  E(pWriter);.}./*
297d7 20 69 46 69 72 73 74 44 6f 63 69 64 20 69 73 20   iFirstDocid is 
297d8 74 68 65 20 66 69 72 73 74 20 64 6f 63 69 64 20  the first docid 
297d9 69 6e 20 74 68 65 20 64 6f 63 6c 69 73 74 20 69  in the doclist i
297da 6e 20 70 44 61 74 61 2e 20 20 49 74 20 69 73 0a  n pData.  It is.
297db 2a 2a 20 6e 65 65 64 65 64 20 62 65 63 61 75 73  ** needed becaus
297dc 65 20 70 44 61 74 61 20 6d 61 79 20 70 6f 69 6e  e pData may poin
297dd 74 20 77 69 74 68 69 6e 20 61 20 6c 61 72 67 65  t within a large
297de 72 20 64 6f 63 6c 69 73 74 2c 20 69 6e 20 77 68  r doclist, in wh
297df 69 63 68 0a 2a 2a 20 63 61 73 65 20 74 68 65 20  ich.** case the 
297e0 66 69 72 73 74 20 69 74 65 6d 20 77 6f 75 6c 64  first item would
297e1 20 62 65 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65   be delta-encode
297e2 64 2e 0a 2a 2a 0a 2a 2a 20 69 4c 61 73 74 44 6f  d..**.** iLastDo
297e3 63 69 64 20 69 73 20 74 68 65 20 66 69 6e 61 6c  cid is the final
297e4 20 64 6f 63 69 64 20 69 6e 20 74 68 65 20 64 6f   docid in the do
297e5 63 6c 69 73 74 20 69 6e 20 70 44 61 74 61 2e 20  clist in pData. 
297e6 20 49 74 20 69 73 0a 2a 2a 20 6e 65 65 64 65 64   It is.** needed
297e7 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 6e   to create the n
297e8 65 77 20 69 50 72 65 76 44 6f 63 69 64 20 66 6f  ew iPrevDocid fo
297e9 72 20 66 75 74 75 72 65 20 64 65 6c 74 61 2d 65  r future delta-e
297ea 6e 63 6f 64 69 6e 67 2e 20 20 54 68 65 0a 2a 2a  ncoding.  The.**
297eb 20 63 6f 64 65 20 63 6f 75 6c 64 20 64 65 63 6f   code could deco
297ec 64 65 20 74 68 65 20 70 61 73 73 65 64 20 64 6f  de the passed do
297ed 63 6c 69 73 74 20 74 6f 20 72 65 63 72 65 61 74  clist to recreat
297ee 65 20 69 4c 61 73 74 44 6f 63 69 64 2c 20 62 75  e iLastDocid, bu
297ef 74 0a 2a 2a 20 74 68 65 20 6f 6e 6c 79 20 63 75  t.** the only cu
297f0 72 72 65 6e 74 20 75 73 65 72 20 28 64 6f 63 4c  rrent user (docL
297f1 69 73 74 4d 65 72 67 65 29 20 61 6c 72 65 61 64  istMerge) alread
297f2 79 20 68 61 73 20 64 65 63 6f 64 65 64 20 74 68  y has decoded th
297f3 69 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f  is.** informatio
297f4 6e 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68  n..*/./* TODO(sh
297f5 65 73 73 29 20 54 68 69 73 20 68 61 73 20 62 65  ess) This has be
297f6 63 6f 6d 65 20 6a 75 73 74 20 61 20 68 65 6c 70  come just a help
297f7 65 72 20 66 6f 72 20 64 6f 63 4c 69 73 74 4d 65  er for docListMe
297f8 72 67 65 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72  rge..** Consider
297f9 20 61 20 72 65 66 61 63 74 6f 72 20 74 6f 20 6d   a refactor to m
297fa 61 6b 65 20 74 68 69 73 20 63 6c 65 61 6e 65 72  ake this cleaner
297fb 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
297fc 20 64 6c 77 41 70 70 65 6e 64 28 44 4c 57 72 69   dlwAppend(DLWri
297fd 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20  ter *pWriter,.  
297fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
297ff 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
29800 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61  pData, int nData
29801 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
29802 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69          sqlite_i
29803 6e 74 36 34 20 69 46 69 72 73 74 44 6f 63 69 64  nt64 iFirstDocid
29804 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  , sqlite_int64 i
29805 4c 61 73 74 44 6f 63 69 64 29 7b 0a 20 20 73 71  LastDocid){.  sq
29806 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69  lite_int64 iDoci
29807 64 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 5b  d = 0;.  char c[
29808 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69  VARINT_MAX];.  i
29809 6e 74 20 6e 46 69 72 73 74 4f 6c 64 2c 20 6e 46  nt nFirstOld, nF
2980a 69 72 73 74 4e 65 77 3b 20 20 20 20 20 2f 2a 20  irstNew;     /* 
2980b 4f 6c 64 20 61 6e 64 20 6e 65 77 20 76 61 72 69  Old and new vari
2980c 6e 74 20 6c 65 6e 20 6f 66 20 66 69 72 73 74 20  nt len of first 
2980d 64 6f 63 69 64 2e 20 2a 2f 0a 23 69 66 6e 64 65  docid. */.#ifnde
2980e 66 20 4e 44 45 42 55 47 0a 20 20 73 71 6c 69 74  f NDEBUG.  sqlit
2980f 65 5f 69 6e 74 36 34 20 69 4c 61 73 74 44 6f 63  e_int64 iLastDoc
29810 69 64 44 65 6c 74 61 3b 0a 23 65 6e 64 69 66 0a  idDelta;.#endif.
29811 0a 20 20 2f 2a 20 52 65 63 6f 64 65 20 74 68 65  .  /* Recode the
29812 20 69 6e 69 74 69 61 6c 20 64 6f 63 69 64 20 61   initial docid a
29813 73 20 64 65 6c 74 61 20 66 72 6f 6d 20 69 50 72  s delta from iPr
29814 65 76 44 6f 63 69 64 2e 20 2a 2f 0a 20 20 6e 46  evDocid. */.  nF
29815 69 72 73 74 4f 6c 64 20 3d 20 66 74 73 33 47 65  irstOld = fts3Ge
29816 74 56 61 72 69 6e 74 28 70 44 61 74 61 2c 20 26  tVarint(pData, &
29817 69 44 6f 63 69 64 29 3b 0a 20 20 61 73 73 65 72  iDocid);.  asser
29818 74 28 20 6e 46 69 72 73 74 4f 6c 64 3c 6e 44 61  t( nFirstOld<nDa
29819 74 61 20 7c 7c 20 28 6e 46 69 72 73 74 4f 6c 64  ta || (nFirstOld
2981a 3d 3d 6e 44 61 74 61 20 26 26 20 70 57 72 69 74  ==nData && pWrit
2981b 65 72 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 44 4f  er->iType==DL_DO
2981c 43 49 44 53 29 20 29 3b 0a 20 20 6e 46 69 72 73  CIDS) );.  nFirs
2981d 74 4e 65 77 20 3d 20 66 74 73 33 50 75 74 56 61  tNew = fts3PutVa
2981e 72 69 6e 74 28 63 2c 20 69 46 69 72 73 74 44 6f  rint(c, iFirstDo
2981f 63 69 64 2d 70 57 72 69 74 65 72 2d 3e 69 50 72  cid-pWriter->iPr
29820 65 76 44 6f 63 69 64 29 3b 0a 0a 20 20 2f 2a 20  evDocid);..  /* 
29821 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20  Verify that the 
29822 69 6e 63 6f 6d 69 6e 67 20 64 6f 63 6c 69 73 74  incoming doclist
29823 20 69 73 20 76 61 6c 69 64 20 41 4e 44 20 74 68   is valid AND th
29824 61 74 20 69 74 20 65 6e 64 73 20 77 69 74 68 0a  at it ends with.
29825 20 20 2a 2a 20 74 68 65 20 65 78 70 65 63 74 65    ** the expecte
29826 64 20 64 6f 63 69 64 2e 20 20 54 68 69 73 20 69  d docid.  This i
29827 73 20 65 73 73 65 6e 74 69 61 6c 20 62 65 63 61  s essential beca
29828 75 73 65 20 77 65 27 6c 6c 20 74 72 75 73 74 20  use we'll trust 
29829 74 68 69 73 0a 20 20 2a 2a 20 64 6f 63 69 64 20  this.  ** docid 
2982a 69 6e 20 66 75 74 75 72 65 20 64 65 6c 74 61 2d  in future delta-
2982b 65 6e 63 6f 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20  encoding..  */. 
2982c 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f   ASSERT_VALID_DO
2982d 43 4c 49 53 54 28 70 57 72 69 74 65 72 2d 3e 69  CLIST(pWriter->i
2982e 54 79 70 65 2c 20 70 44 61 74 61 2c 20 6e 44 61  Type, pData, nDa
2982f 74 61 2c 20 26 69 4c 61 73 74 44 6f 63 69 64 44  ta, &iLastDocidD
29830 65 6c 74 61 29 3b 0a 20 20 61 73 73 65 72 74 28  elta);.  assert(
29831 20 69 4c 61 73 74 44 6f 63 69 64 3d 3d 69 46 69   iLastDocid==iFi
29832 72 73 74 44 6f 63 69 64 2d 69 44 6f 63 69 64 2b  rstDocid-iDocid+
29833 69 4c 61 73 74 44 6f 63 69 64 44 65 6c 74 61 20  iLastDocidDelta 
29834 29 3b 0a 0a 20 20 2f 2a 20 41 70 70 65 6e 64 20  );..  /* Append 
29835 72 65 63 6f 64 65 64 20 69 6e 69 74 69 61 6c 20  recoded initial 
29836 64 6f 63 69 64 20 61 6e 64 20 65 76 65 72 79 74  docid and everyt
29837 68 69 6e 67 20 65 6c 73 65 2e 20 20 52 65 73 74  hing else.  Rest
29838 20 6f 66 20 64 6f 63 69 64 73 0a 20 20 2a 2a 20   of docids.  ** 
29839 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e  should have been
2983a 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64 20 66   delta-encoded f
2983b 72 6f 6d 20 70 72 65 76 69 6f 75 73 20 69 6e 69  rom previous ini
2983c 74 69 61 6c 20 64 6f 63 69 64 2e 0a 20 20 2a 2f  tial docid..  */
2983d 0a 20 20 69 66 28 20 6e 46 69 72 73 74 4f 6c 64  .  if( nFirstOld
2983e 3c 6e 44 61 74 61 20 29 7b 0a 20 20 20 20 64 61  <nData ){.    da
2983f 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32 28  taBufferAppend2(
29840 70 57 72 69 74 65 72 2d 3e 62 2c 20 63 2c 20 6e  pWriter->b, c, n
29841 46 69 72 73 74 4e 65 77 2c 0a 20 20 20 20 20 20  FirstNew,.      
29842 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29843 70 44 61 74 61 2b 6e 46 69 72 73 74 4f 6c 64 2c  pData+nFirstOld,
29844 20 6e 44 61 74 61 2d 6e 46 69 72 73 74 4f 6c 64   nData-nFirstOld
29845 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
29846 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
29847 28 70 57 72 69 74 65 72 2d 3e 62 2c 20 63 2c 20  (pWriter->b, c, 
29848 6e 46 69 72 73 74 4e 65 77 29 3b 0a 20 20 7d 0a  nFirstNew);.  }.
29849 20 20 70 57 72 69 74 65 72 2d 3e 69 50 72 65 76    pWriter->iPrev
2984a 44 6f 63 69 64 20 3d 20 69 4c 61 73 74 44 6f 63  Docid = iLastDoc
2984b 69 64 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69  id;.}.static voi
2984c 64 20 64 6c 77 43 6f 70 79 28 44 4c 57 72 69 74  d dlwCopy(DLWrit
2984d 65 72 20 2a 70 57 72 69 74 65 72 2c 20 44 4c 52  er *pWriter, DLR
2984e 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
2984f 0a 20 20 64 6c 77 41 70 70 65 6e 64 28 70 57 72  .  dlwAppend(pWr
29850 69 74 65 72 2c 20 64 6c 72 44 6f 63 44 61 74 61  iter, dlrDocData
29851 28 70 52 65 61 64 65 72 29 2c 20 64 6c 72 44 6f  (pReader), dlrDo
29852 63 44 61 74 61 42 79 74 65 73 28 70 52 65 61 64  cDataBytes(pRead
29853 65 72 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  er),.           
29854 20 64 6c 72 44 6f 63 69 64 28 70 52 65 61 64 65   dlrDocid(pReade
29855 72 29 2c 20 64 6c 72 44 6f 63 69 64 28 70 52 65  r), dlrDocid(pRe
29856 61 64 65 72 29 29 3b 0a 7d 0a 73 74 61 74 69 63  ader));.}.static
29857 20 76 6f 69 64 20 64 6c 77 41 64 64 28 44 4c 57   void dlwAdd(DLW
29858 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c 20  riter *pWriter, 
29859 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f  sqlite_int64 iDo
2985a 63 69 64 29 7b 0a 20 20 63 68 61 72 20 63 5b 56  cid){.  char c[V
2985b 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e  ARINT_MAX];.  in
2985c 74 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72  t n = fts3PutVar
2985d 69 6e 74 28 63 2c 20 69 44 6f 63 69 64 2d 70 57  int(c, iDocid-pW
2985e 72 69 74 65 72 2d 3e 69 50 72 65 76 44 6f 63 69  riter->iPrevDoci
2985f 64 29 3b 0a 0a 20 20 2f 2a 20 44 6f 63 69 64 73  d);..  /* Docids
29860 20 6d 75 73 74 20 61 73 63 65 6e 64 2e 20 2a 2f   must ascend. */
29861 0a 20 20 61 73 73 65 72 74 28 20 21 70 57 72 69  .  assert( !pWri
29862 74 65 72 2d 3e 68 61 73 5f 69 50 72 65 76 44 6f  ter->has_iPrevDo
29863 63 69 64 20 7c 7c 20 69 44 6f 63 69 64 3e 70 57  cid || iDocid>pW
29864 72 69 74 65 72 2d 3e 69 50 72 65 76 44 6f 63 69  riter->iPrevDoci
29865 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  d );.  assert( p
29866 57 72 69 74 65 72 2d 3e 69 54 79 70 65 3d 3d 44  Writer->iType==D
29867 4c 5f 44 4f 43 49 44 53 20 29 3b 0a 0a 20 20 64  L_DOCIDS );..  d
29868 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28  ataBufferAppend(
29869 70 57 72 69 74 65 72 2d 3e 62 2c 20 63 2c 20 6e  pWriter->b, c, n
2986a 29 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 50  );.  pWriter->iP
2986b 72 65 76 44 6f 63 69 64 20 3d 20 69 44 6f 63 69  revDocid = iDoci
2986c 64 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  d;.#ifndef NDEBU
2986d 47 0a 20 20 70 57 72 69 74 65 72 2d 3e 68 61 73  G.  pWriter->has
2986e 5f 69 50 72 65 76 44 6f 63 69 64 20 3d 20 31 3b  _iPrevDocid = 1;
2986f 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 2a 2a 2a  .#endif.}../****
29870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29871 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29872 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29873 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
29874 0a 2f 2a 20 50 4c 52 65 61 64 65 72 20 69 73 20  ./* PLReader is 
29875 75 73 65 64 20 74 6f 20 72 65 61 64 20 64 61 74  used to read dat
29876 61 20 66 72 6f 6d 20 61 20 64 6f 63 75 6d 65 6e  a from a documen
29877 74 27 73 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73  t's position lis
29878 74 2e 20 20 41 73 0a 2a 2a 20 74 68 65 20 63 61  t.  As.** the ca
29879 6c 6c 65 72 20 73 74 65 70 73 20 74 68 72 6f 75  ller steps throu
2987a 67 68 20 74 68 65 20 6c 69 73 74 2c 20 64 61 74  gh the list, dat
2987b 61 20 69 73 20 63 61 63 68 65 64 20 73 6f 20 74  a is cached so t
2987c 68 61 74 20 76 61 72 69 6e 74 73 0a 2a 2a 20 6f  hat varints.** o
2987d 6e 6c 79 20 6e 65 65 64 20 74 6f 20 62 65 20 64  nly need to be d
2987e 65 63 6f 64 65 64 20 6f 6e 63 65 2e 0a 2a 2a 0a  ecoded once..**.
2987f 2a 2a 20 70 6c 72 49 6e 69 74 2c 20 70 6c 72 44  ** plrInit, plrD
29880 65 73 74 72 6f 79 20 2d 20 63 72 65 61 74 65 2f  estroy - create/
29881 64 65 73 74 72 6f 79 20 61 20 72 65 61 64 65 72  destroy a reader
29882 2e 0a 2a 2a 20 70 6c 72 43 6f 6c 75 6d 6e 2c 20  ..** plrColumn, 
29883 70 6c 72 50 6f 73 69 74 69 6f 6e 2c 20 70 6c 72  plrPosition, plr
29884 53 74 61 72 74 4f 66 66 73 65 74 2c 20 70 6c 72  StartOffset, plr
29885 45 6e 64 4f 66 66 73 65 74 20 2d 20 61 63 63 65  EndOffset - acce
29886 73 73 6f 72 73 0a 2a 2a 20 70 6c 72 41 74 45 6e  ssors.** plrAtEn
29887 64 20 2d 20 61 74 20 65 6e 64 20 6f 66 20 73 74  d - at end of st
29888 72 65 61 6d 2c 20 6f 6e 6c 79 20 63 61 6c 6c 20  ream, only call 
29889 70 6c 72 44 65 73 74 72 6f 79 20 6f 6e 63 65 20  plrDestroy once 
2988a 74 72 75 65 2e 0a 2a 2a 20 70 6c 72 53 74 65 70  true..** plrStep
2988b 20 2d 20 73 74 65 70 20 74 6f 20 74 68 65 20 6e   - step to the n
2988c 65 78 74 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a  ext element..*/.
2988d 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50  typedef struct P
2988e 4c 52 65 61 64 65 72 20 7b 0a 20 20 2f 2a 20 54  LReader {.  /* T
2988f 68 65 73 65 20 72 65 66 65 72 20 74 6f 20 74 68  hese refer to th
29890 65 20 6e 65 78 74 20 70 6f 73 69 74 69 6f 6e 27  e next position'
29891 73 20 64 61 74 61 2e 20 20 6e 44 61 74 61 20 77  s data.  nData w
29892 69 6c 6c 20 72 65 61 63 68 20 30 20 77 68 65 6e  ill reach 0 when
29893 0a 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 74 68  .  ** reading th
29894 65 20 6c 61 73 74 20 70 6f 73 69 74 69 6f 6e 2c  e last position,
29895 20 73 6f 20 70 6c 72 53 74 65 70 28 29 20 73 69   so plrStep() si
29896 67 6e 61 6c 73 20 45 4f 46 20 62 79 20 73 65 74  gnals EOF by set
29897 74 69 6e 67 0a 20 20 2a 2a 20 70 44 61 74 61 20  ting.  ** pData 
29898 74 6f 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20  to NULL..  */.  
29899 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
2989a 61 3b 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a  a;.  int nData;.
2989b 0a 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69  .  DocListType i
2989c 54 79 70 65 3b 0a 20 20 69 6e 74 20 69 43 6f 6c  Type;.  int iCol
2989d 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  umn;         /* 
2989e 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20  the last column 
2989f 72 65 61 64 20 2a 2f 0a 20 20 69 6e 74 20 69 50  read */.  int iP
298a0 6f 73 69 74 69 6f 6e 3b 20 20 20 20 20 20 20 2f  osition;       /
298a1 2a 20 74 68 65 20 6c 61 73 74 20 70 6f 73 69 74  * the last posit
298a2 69 6f 6e 20 72 65 61 64 20 2a 2f 0a 20 20 69 6e  ion read */.  in
298a3 74 20 69 53 74 61 72 74 4f 66 66 73 65 74 3b 20  t iStartOffset; 
298a4 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74 20 73     /* the last s
298a5 74 61 72 74 20 6f 66 66 73 65 74 20 72 65 61 64  tart offset read
298a6 20 2a 2f 0a 20 20 69 6e 74 20 69 45 6e 64 4f 66   */.  int iEndOf
298a7 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 74 68  fset;      /* th
298a8 65 20 6c 61 73 74 20 65 6e 64 20 6f 66 66 73 65  e last end offse
298a9 74 20 72 65 61 64 20 2a 2f 0a 7d 20 50 4c 52 65  t read */.} PLRe
298aa 61 64 65 72 3b 0a 0a 73 74 61 74 69 63 20 69 6e  ader;..static in
298ab 74 20 70 6c 72 41 74 45 6e 64 28 50 4c 52 65 61  t plrAtEnd(PLRea
298ac 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
298ad 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d   return pReader-
298ae 3e 70 44 61 74 61 3d 3d 4e 55 4c 4c 3b 0a 7d 0a  >pData==NULL;.}.
298af 73 74 61 74 69 63 20 69 6e 74 20 70 6c 72 43 6f  static int plrCo
298b0 6c 75 6d 6e 28 50 4c 52 65 61 64 65 72 20 2a 70  lumn(PLReader *p
298b1 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72  Reader){.  asser
298b2 74 28 20 21 70 6c 72 41 74 45 6e 64 28 70 52 65  t( !plrAtEnd(pRe
298b3 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72  ader) );.  retur
298b4 6e 20 70 52 65 61 64 65 72 2d 3e 69 43 6f 6c 75  n pReader->iColu
298b5 6d 6e 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  mn;.}.static int
298b6 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 50 4c 52   plrPosition(PLR
298b7 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
298b8 0a 20 20 61 73 73 65 72 74 28 20 21 70 6c 72 41  .  assert( !plrA
298b9 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b  tEnd(pReader) );
298ba 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65  .  return pReade
298bb 72 2d 3e 69 50 6f 73 69 74 69 6f 6e 3b 0a 7d 0a  r->iPosition;.}.
298bc 73 74 61 74 69 63 20 69 6e 74 20 70 6c 72 53 74  static int plrSt
298bd 61 72 74 4f 66 66 73 65 74 28 50 4c 52 65 61 64  artOffset(PLRead
298be 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
298bf 61 73 73 65 72 74 28 20 21 70 6c 72 41 74 45 6e  assert( !plrAtEn
298c0 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20  d(pReader) );.  
298c1 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
298c2 69 53 74 61 72 74 4f 66 66 73 65 74 3b 0a 7d 0a  iStartOffset;.}.
298c3 73 74 61 74 69 63 20 69 6e 74 20 70 6c 72 45 6e  static int plrEn
298c4 64 4f 66 66 73 65 74 28 50 4c 52 65 61 64 65 72  dOffset(PLReader
298c5 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73   *pReader){.  as
298c6 73 65 72 74 28 20 21 70 6c 72 41 74 45 6e 64 28  sert( !plrAtEnd(
298c7 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65  pReader) );.  re
298c8 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 69 45  turn pReader->iE
298c9 6e 64 4f 66 66 73 65 74 3b 0a 7d 0a 73 74 61 74  ndOffset;.}.stat
298ca 69 63 20 76 6f 69 64 20 70 6c 72 53 74 65 70 28  ic void plrStep(
298cb 50 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  PLReader *pReade
298cc 72 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a  r){.  int i, n;.
298cd 0a 20 20 61 73 73 65 72 74 28 20 21 70 6c 72 41  .  assert( !plrA
298ce 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b  tEnd(pReader) );
298cf 0a 0a 20 20 69 66 28 20 70 52 65 61 64 65 72 2d  ..  if( pReader-
298d0 3e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20  >nData==0 ){.   
298d1 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 20   pReader->pData 
298d2 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 72 65 74 75  = NULL;.    retu
298d3 72 6e 3b 0a 20 20 7d 0a 0a 20 20 6e 20 3d 20 66  rn;.  }..  n = f
298d4 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
298d5 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26  Reader->pData, &
298d6 69 29 3b 0a 20 20 69 66 28 20 69 3d 3d 50 4f 53  i);.  if( i==POS
298d7 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 6e  _COLUMN ){.    n
298d8 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e   += fts3GetVarin
298d9 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61  t32(pReader->pDa
298da 74 61 2b 6e 2c 20 26 70 52 65 61 64 65 72 2d 3e  ta+n, &pReader->
298db 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 70 52  iColumn);.    pR
298dc 65 61 64 65 72 2d 3e 69 50 6f 73 69 74 69 6f 6e  eader->iPosition
298dd 20 3d 20 30 3b 0a 20 20 20 20 70 52 65 61 64 65   = 0;.    pReade
298de 72 2d 3e 69 53 74 61 72 74 4f 66 66 73 65 74 20  r->iStartOffset 
298df 3d 20 30 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74  = 0;.    n += ft
298e0 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52  s3GetVarint32(pR
298e1 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20  eader->pData+n, 
298e2 26 69 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 53 68  &i);.  }.  /* Sh
298e3 6f 75 6c 64 20 6e 65 76 65 72 20 73 65 65 20 61  ould never see a
298e4 64 6a 61 63 65 6e 74 20 63 6f 6c 75 6d 6e 20 63  djacent column c
298e5 68 61 6e 67 65 73 2e 20 2a 2f 0a 20 20 61 73 73  hanges. */.  ass
298e6 65 72 74 28 20 69 21 3d 50 4f 53 5f 43 4f 4c 55  ert( i!=POS_COLU
298e7 4d 4e 20 29 3b 0a 0a 20 20 69 66 28 20 69 3d 3d  MN );..  if( i==
298e8 50 4f 53 5f 45 4e 44 20 29 7b 0a 20 20 20 20 70  POS_END ){.    p
298e9 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 3d 20  Reader->nData = 
298ea 30 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  0;.    pReader->
298eb 70 44 61 74 61 20 3d 20 4e 55 4c 4c 3b 0a 20 20  pData = NULL;.  
298ec 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
298ed 20 70 52 65 61 64 65 72 2d 3e 69 50 6f 73 69 74   pReader->iPosit
298ee 69 6f 6e 20 2b 3d 20 69 2d 50 4f 53 5f 42 41 53  ion += i-POS_BAS
298ef 45 3b 0a 20 20 69 66 28 20 70 52 65 61 64 65 72  E;.  if( pReader
298f0 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49  ->iType==DL_POSI
298f1 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29 7b  TIONS_OFFSETS ){
298f2 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65  .    n += fts3Ge
298f3 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65  tVarint32(pReade
298f4 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69 29 3b  r->pData+n, &i);
298f5 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 69 53  .    pReader->iS
298f6 74 61 72 74 4f 66 66 73 65 74 20 2b 3d 20 69 3b  tartOffset += i;
298f7 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65  .    n += fts3Ge
298f8 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65  tVarint32(pReade
298f9 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69 29 3b  r->pData+n, &i);
298fa 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 69 45  .    pReader->iE
298fb 6e 64 4f 66 66 73 65 74 20 3d 20 70 52 65 61 64  ndOffset = pRead
298fc 65 72 2d 3e 69 53 74 61 72 74 4f 66 66 73 65 74  er->iStartOffset
298fd 2b 69 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  +i;.  }.  assert
298fe 28 20 6e 3c 3d 70 52 65 61 64 65 72 2d 3e 6e 44  ( n<=pReader->nD
298ff 61 74 61 20 29 3b 0a 20 20 70 52 65 61 64 65 72  ata );.  pReader
29900 2d 3e 70 44 61 74 61 20 2b 3d 20 6e 3b 0a 20 20  ->pData += n;.  
29901 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 2d  pReader->nData -
29902 3d 20 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  = n;.}..static v
29903 6f 69 64 20 70 6c 72 49 6e 69 74 28 50 4c 52 65  oid plrInit(PLRe
29904 61 64 65 72 20 2a 70 52 65 61 64 65 72 2c 20 44  ader *pReader, D
29905 4c 52 65 61 64 65 72 20 2a 70 44 4c 52 65 61 64  LReader *pDLRead
29906 65 72 29 7b 0a 20 20 70 52 65 61 64 65 72 2d 3e  er){.  pReader->
29907 70 44 61 74 61 20 3d 20 64 6c 72 50 6f 73 44 61  pData = dlrPosDa
29908 74 61 28 70 44 4c 52 65 61 64 65 72 29 3b 0a 20  ta(pDLReader);. 
29909 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20   pReader->nData 
2990a 3d 20 64 6c 72 50 6f 73 44 61 74 61 4c 65 6e 28  = dlrPosDataLen(
2990b 70 44 4c 52 65 61 64 65 72 29 3b 0a 20 20 70 52  pDLReader);.  pR
2990c 65 61 64 65 72 2d 3e 69 54 79 70 65 20 3d 20 70  eader->iType = p
2990d 44 4c 52 65 61 64 65 72 2d 3e 69 54 79 70 65 3b  DLReader->iType;
2990e 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 43 6f 6c  .  pReader->iCol
2990f 75 6d 6e 20 3d 20 30 3b 0a 20 20 70 52 65 61 64  umn = 0;.  pRead
29910 65 72 2d 3e 69 50 6f 73 69 74 69 6f 6e 20 3d 20  er->iPosition = 
29911 30 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 53  0;.  pReader->iS
29912 74 61 72 74 4f 66 66 73 65 74 20 3d 20 30 3b 0a  tartOffset = 0;.
29913 20 20 70 52 65 61 64 65 72 2d 3e 69 45 6e 64 4f    pReader->iEndO
29914 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 70 6c 72  ffset = 0;.  plr
29915 53 74 65 70 28 70 52 65 61 64 65 72 29 3b 0a 7d  Step(pReader);.}
29916 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 6c 72  .static void plr
29917 44 65 73 74 72 6f 79 28 50 4c 52 65 61 64 65 72  Destroy(PLReader
29918 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 53 43   *pReader){.  SC
29919 52 41 4d 42 4c 45 28 70 52 65 61 64 65 72 29 3b  RAMBLE(pReader);
2991a 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
2991b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2991c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2991d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2991e 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 50 4c 57  ********/./* PLW
2991f 72 69 74 65 72 20 69 73 20 75 73 65 64 20 69 6e  riter is used in
29920 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 61 20   constructing a 
29921 64 6f 63 75 6d 65 6e 74 27 73 20 70 6f 73 69 74  document's posit
29922 69 6f 6e 20 6c 69 73 74 2e 20 20 41 73 20 61 0a  ion list.  As a.
29923 2a 2a 20 63 6f 6e 76 65 6e 69 65 6e 63 65 2c 20  ** convenience, 
29924 69 66 20 69 54 79 70 65 20 69 73 20 44 4c 5f 44  if iType is DL_D
29925 4f 43 49 44 53 2c 20 50 4c 57 72 69 74 65 72 20  OCIDS, PLWriter 
29926 62 65 63 6f 6d 65 73 20 61 20 6e 6f 2d 6f 70 2e  becomes a no-op.
29927 0a 2a 2a 20 50 4c 57 72 69 74 65 72 20 77 72 69  .** PLWriter wri
29928 74 65 73 20 74 6f 20 74 68 65 20 61 73 73 6f 63  tes to the assoc
29929 69 61 74 65 64 20 44 4c 57 72 69 74 65 72 27 73  iated DLWriter's
2992a 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 70   buffer..**.** p
2992b 6c 77 49 6e 69 74 20 2d 20 69 6e 69 74 20 66 6f  lwInit - init fo
2992c 72 20 77 72 69 74 69 6e 67 20 61 20 64 6f 63 75  r writing a docu
2992d 6d 65 6e 74 27 73 20 70 6f 73 6c 69 73 74 2e 0a  ment's poslist..
2992e 2a 2a 20 70 6c 77 44 65 73 74 72 6f 79 20 2d 20  ** plwDestroy - 
2992f 63 6c 65 61 72 20 61 20 77 72 69 74 65 72 2e 0a  clear a writer..
29930 2a 2a 20 70 6c 77 41 64 64 20 2d 20 61 70 70 65  ** plwAdd - appe
29931 6e 64 20 70 6f 73 69 74 69 6f 6e 20 61 6e 64 20  nd position and 
29932 6f 66 66 73 65 74 20 69 6e 66 6f 72 6d 61 74 69  offset informati
29933 6f 6e 2e 0a 2a 2a 20 70 6c 77 43 6f 70 79 20 2d  on..** plwCopy -
29934 20 63 6f 70 79 20 6e 65 78 74 20 70 6f 73 69 74   copy next posit
29935 69 6f 6e 27 73 20 64 61 74 61 20 66 72 6f 6d 20  ion's data from 
29936 72 65 61 64 65 72 20 74 6f 20 77 72 69 74 65 72  reader to writer
29937 2e 0a 2a 2a 20 70 6c 77 54 65 72 6d 69 6e 61 74  ..** plwTerminat
29938 65 20 2d 20 61 64 64 20 61 6e 79 20 6e 65 63 65  e - add any nece
29939 73 73 61 72 79 20 64 6f 63 6c 69 73 74 20 74 65  ssary doclist te
2993a 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20  rminator..**.** 
2993b 43 61 6c 6c 69 6e 67 20 70 6c 77 41 64 64 28 29  Calling plwAdd()
2993c 20 61 66 74 65 72 20 70 6c 77 54 65 72 6d 69 6e   after plwTermin
2993d 61 74 65 28 29 20 6d 61 79 20 72 65 73 75 6c 74  ate() may result
2993e 20 69 6e 20 61 20 63 6f 72 72 75 70 74 0a 2a 2a   in a corrupt.**
2993f 20 64 6f 63 6c 69 73 74 2e 0a 2a 2f 0a 2f 2a 20   doclist..*/./* 
29940 54 4f 44 4f 28 73 68 65 73 73 29 20 55 6e 74 69  TODO(shess) Unti
29941 6c 20 77 65 27 76 65 20 77 72 69 74 74 65 6e 20  l we've written 
29942 74 68 65 20 73 65 63 6f 6e 64 20 69 74 65 6d 2c  the second item,
29943 20 77 65 20 63 61 6e 20 63 61 63 68 65 20 74 68   we can cache th
29944 65 0a 2a 2a 20 66 69 72 73 74 20 69 74 65 6d 27  e.** first item'
29945 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20  s information.  
29946 54 68 65 6e 20 77 65 27 64 20 68 61 76 65 20 74  Then we'd have t
29947 68 72 65 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a  hree states:.**.
29948 2a 2a 20 2d 20 69 6e 69 74 69 61 6c 69 7a 65 64  ** - initialized
29949 20 77 69 74 68 20 64 6f 63 69 64 2c 20 6e 6f 20   with docid, no 
2994a 70 6f 73 69 74 69 6f 6e 73 2e 0a 2a 2a 20 2d 20  positions..** - 
2994b 64 6f 63 69 64 20 61 6e 64 20 6f 6e 65 20 70 6f  docid and one po
2994c 73 69 74 69 6f 6e 2e 0a 2a 2a 20 2d 20 64 6f 63  sition..** - doc
2994d 69 64 20 61 6e 64 20 6d 75 6c 74 69 70 6c 65 20  id and multiple 
2994e 70 6f 73 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  positions..**.**
2994f 20 4f 6e 6c 79 20 74 68 65 20 6c 61 73 74 20 73   Only the last s
29950 74 61 74 65 20 6e 65 65 64 73 20 74 6f 20 61 63  tate needs to ac
29951 74 75 61 6c 6c 79 20 77 72 69 74 65 20 74 6f 20  tually write to 
29952 64 6c 77 2d 3e 62 2c 20 77 68 69 63 68 20 77 6f  dlw->b, which wo
29953 75 6c 64 0a 2a 2a 20 62 65 20 61 6e 20 69 6d 70  uld.** be an imp
29954 72 6f 76 65 6d 65 6e 74 20 69 6e 20 74 68 65 20  rovement in the 
29955 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 63 61 73 65  DLCollector case
29956 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
29957 75 63 74 20 50 4c 57 72 69 74 65 72 20 7b 0a 20  uct PLWriter {. 
29958 20 44 4c 57 72 69 74 65 72 20 2a 64 6c 77 3b 0a   DLWriter *dlw;.
29959 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20  .  int iColumn; 
2995a 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74 20 63     /* the last c
2995b 6f 6c 75 6d 6e 20 77 72 69 74 74 65 6e 20 2a 2f  olumn written */
2995c 0a 20 20 69 6e 74 20 69 50 6f 73 3b 20 20 20 20  .  int iPos;    
2995d 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74 20 70     /* the last p
2995e 6f 73 69 74 69 6f 6e 20 77 72 69 74 74 65 6e 20  osition written 
2995f 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74  */.  int iOffset
29960 3b 20 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74  ;    /* the last
29961 20 73 74 61 72 74 20 6f 66 66 73 65 74 20 77 72   start offset wr
29962 69 74 74 65 6e 20 2a 2f 0a 7d 20 50 4c 57 72 69  itten */.} PLWri
29963 74 65 72 3b 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68  ter;../* TODO(sh
29964 65 73 73 29 20 49 6e 20 74 68 65 20 63 61 73 65  ess) In the case
29965 20 77 68 65 72 65 20 74 68 65 20 70 61 72 65 6e   where the paren
29966 74 20 69 73 20 72 65 61 64 69 6e 67 20 74 68 65  t is reading the
29967 73 65 20 76 61 6c 75 65 73 0a 2a 2a 20 66 72 6f  se values.** fro
29968 6d 20 61 20 50 4c 52 65 61 64 65 72 2c 20 77 65  m a PLReader, we
29969 20 63 6f 75 6c 64 20 6f 70 74 69 6d 69 7a 65 20   could optimize 
2996a 74 6f 20 61 20 63 6f 70 79 20 69 66 20 74 68 61  to a copy if tha
2996b 74 20 50 4c 52 65 61 64 65 72 20 68 61 73 0a 2a  t PLReader has.*
2996c 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20  * the same type 
2996d 61 73 20 70 57 72 69 74 65 72 2e 0a 2a 2f 0a 73  as pWriter..*/.s
2996e 74 61 74 69 63 20 76 6f 69 64 20 70 6c 77 41 64  tatic void plwAd
2996f 64 28 50 4c 57 72 69 74 65 72 20 2a 70 57 72 69  d(PLWriter *pWri
29970 74 65 72 2c 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  ter, int iColumn
29971 2c 20 69 6e 74 20 69 50 6f 73 2c 0a 20 20 20 20  , int iPos,.    
29972 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
29973 6e 74 20 69 53 74 61 72 74 4f 66 66 73 65 74 2c  nt iStartOffset,
29974 20 69 6e 74 20 69 45 6e 64 4f 66 66 73 65 74 29   int iEndOffset)
29975 7b 0a 20 20 2f 2a 20 57 6f 72 73 74 2d 63 61 73  {.  /* Worst-cas
29976 65 20 73 70 61 63 65 20 66 6f 72 20 50 4f 53 5f  e space for POS_
29977 43 4f 4c 55 4d 4e 2c 20 69 43 6f 6c 75 6d 6e 2c  COLUMN, iColumn,
29978 20 69 50 6f 73 44 65 6c 74 61 2c 0a 20 20 2a 2a   iPosDelta,.  **
29979 20 69 53 74 61 72 74 4f 66 66 73 65 74 44 65 6c   iStartOffsetDel
2997a 74 61 2c 20 61 6e 64 20 69 45 6e 64 4f 66 66 73  ta, and iEndOffs
2997b 65 74 44 65 6c 74 61 2e 0a 20 20 2a 2f 0a 20 20  etDelta..  */.  
2997c 63 68 61 72 20 63 5b 35 2a 56 41 52 49 4e 54 5f  char c[5*VARINT_
2997d 4d 41 58 5d 3b 0a 20 20 69 6e 74 20 6e 20 3d 20  MAX];.  int n = 
2997e 30 3b 0a 0a 20 20 2f 2a 20 42 61 6e 20 70 6c 77  0;..  /* Ban plw
2997f 41 64 64 28 29 20 61 66 74 65 72 20 70 6c 77 54  Add() after plwT
29980 65 72 6d 69 6e 61 74 65 28 29 2e 20 2a 2f 0a 20  erminate(). */. 
29981 20 61 73 73 65 72 74 28 20 70 57 72 69 74 65 72   assert( pWriter
29982 2d 3e 69 50 6f 73 21 3d 2d 31 20 29 3b 0a 0a 20  ->iPos!=-1 );.. 
29983 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e 64 6c   if( pWriter->dl
29984 77 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 44 4f 43  w->iType==DL_DOC
29985 49 44 53 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20  IDS ) return;.. 
29986 20 69 66 28 20 69 43 6f 6c 75 6d 6e 21 3d 70 57   if( iColumn!=pW
29987 72 69 74 65 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29  riter->iColumn )
29988 7b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 50  {.    n += fts3P
29989 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 50 4f  utVarint(c+n, PO
2998a 53 5f 43 4f 4c 55 4d 4e 29 3b 0a 20 20 20 20 6e  S_COLUMN);.    n
2998b 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e   += fts3PutVarin
2998c 74 28 63 2b 6e 2c 20 69 43 6f 6c 75 6d 6e 29 3b  t(c+n, iColumn);
2998d 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e 69 43  .    pWriter->iC
2998e 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 75 6d 6e 3b  olumn = iColumn;
2998f 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e 69 50  .    pWriter->iP
29990 6f 73 20 3d 20 30 3b 0a 20 20 20 20 70 57 72 69  os = 0;.    pWri
29991 74 65 72 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30  ter->iOffset = 0
29992 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
29993 69 50 6f 73 3e 3d 70 57 72 69 74 65 72 2d 3e 69  iPos>=pWriter->i
29994 50 6f 73 20 29 3b 0a 20 20 6e 20 2b 3d 20 66 74  Pos );.  n += ft
29995 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c  s3PutVarint(c+n,
29996 20 50 4f 53 5f 42 41 53 45 2b 28 69 50 6f 73 2d   POS_BASE+(iPos-
29997 70 57 72 69 74 65 72 2d 3e 69 50 6f 73 29 29 3b  pWriter->iPos));
29998 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 50 6f 73  .  pWriter->iPos
29999 20 3d 20 69 50 6f 73 3b 0a 20 20 69 66 28 20 70   = iPos;.  if( p
2999a 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 69 54 79  Writer->dlw->iTy
2999b 70 65 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53  pe==DL_POSITIONS
2999c 5f 4f 46 46 53 45 54 53 20 29 7b 0a 20 20 20 20  _OFFSETS ){.    
2999d 61 73 73 65 72 74 28 20 69 53 74 61 72 74 4f 66  assert( iStartOf
2999e 66 73 65 74 3e 3d 70 57 72 69 74 65 72 2d 3e 69  fset>=pWriter->i
2999f 4f 66 66 73 65 74 20 29 3b 0a 20 20 20 20 6e 20  Offset );.    n 
299a0 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74  += fts3PutVarint
299a1 28 63 2b 6e 2c 20 69 53 74 61 72 74 4f 66 66 73  (c+n, iStartOffs
299a2 65 74 2d 70 57 72 69 74 65 72 2d 3e 69 4f 66 66  et-pWriter->iOff
299a3 73 65 74 29 3b 0a 20 20 20 20 70 57 72 69 74 65  set);.    pWrite
299a4 72 2d 3e 69 4f 66 66 73 65 74 20 3d 20 69 53 74  r->iOffset = iSt
299a5 61 72 74 4f 66 66 73 65 74 3b 0a 20 20 20 20 61  artOffset;.    a
299a6 73 73 65 72 74 28 20 69 45 6e 64 4f 66 66 73 65  ssert( iEndOffse
299a7 74 3e 3d 69 53 74 61 72 74 4f 66 66 73 65 74 20  t>=iStartOffset 
299a8 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33  );.    n += fts3
299a9 50 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 69  PutVarint(c+n, i
299aa 45 6e 64 4f 66 66 73 65 74 2d 69 53 74 61 72 74  EndOffset-iStart
299ab 4f 66 66 73 65 74 29 3b 0a 20 20 7d 0a 20 20 64  Offset);.  }.  d
299ac 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28  ataBufferAppend(
299ad 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 62 2c  pWriter->dlw->b,
299ae 20 63 2c 20 6e 29 3b 0a 7d 0a 73 74 61 74 69 63   c, n);.}.static
299af 20 76 6f 69 64 20 70 6c 77 43 6f 70 79 28 50 4c   void plwCopy(PL
299b0 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c  Writer *pWriter,
299b1 20 50 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64   PLReader *pRead
299b2 65 72 29 7b 0a 20 20 70 6c 77 41 64 64 28 70 57  er){.  plwAdd(pW
299b3 72 69 74 65 72 2c 20 70 6c 72 43 6f 6c 75 6d 6e  riter, plrColumn
299b4 28 70 52 65 61 64 65 72 29 2c 20 70 6c 72 50 6f  (pReader), plrPo
299b5 73 69 74 69 6f 6e 28 70 52 65 61 64 65 72 29 2c  sition(pReader),
299b6 0a 20 20 20 20 20 20 20 20 20 70 6c 72 53 74 61  .         plrSta
299b7 72 74 4f 66 66 73 65 74 28 70 52 65 61 64 65 72  rtOffset(pReader
299b8 29 2c 20 70 6c 72 45 6e 64 4f 66 66 73 65 74 28  ), plrEndOffset(
299b9 70 52 65 61 64 65 72 29 29 3b 0a 7d 0a 73 74 61  pReader));.}.sta
299ba 74 69 63 20 76 6f 69 64 20 70 6c 77 49 6e 69 74  tic void plwInit
299bb 28 50 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74  (PLWriter *pWrit
299bc 65 72 2c 20 44 4c 57 72 69 74 65 72 20 2a 64 6c  er, DLWriter *dl
299bd 77 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  w, sqlite_int64 
299be 69 44 6f 63 69 64 29 7b 0a 20 20 63 68 61 72 20  iDocid){.  char 
299bf 63 5b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20  c[VARINT_MAX];. 
299c0 20 69 6e 74 20 6e 3b 0a 0a 20 20 70 57 72 69 74   int n;..  pWrit
299c1 65 72 2d 3e 64 6c 77 20 3d 20 64 6c 77 3b 0a 0a  er->dlw = dlw;..
299c2 20 20 2f 2a 20 44 6f 63 69 64 73 20 6d 75 73 74    /* Docids must
299c3 20 61 73 63 65 6e 64 2e 20 2a 2f 0a 20 20 61 73   ascend. */.  as
299c4 73 65 72 74 28 20 21 70 57 72 69 74 65 72 2d 3e  sert( !pWriter->
299c5 64 6c 77 2d 3e 68 61 73 5f 69 50 72 65 76 44 6f  dlw->has_iPrevDo
299c6 63 69 64 20 7c 7c 20 69 44 6f 63 69 64 3e 70 57  cid || iDocid>pW
299c7 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 69 50 72 65  riter->dlw->iPre
299c8 76 44 6f 63 69 64 20 29 3b 0a 20 20 6e 20 3d 20  vDocid );.  n = 
299c9 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c  fts3PutVarint(c,
299ca 20 69 44 6f 63 69 64 2d 70 57 72 69 74 65 72 2d   iDocid-pWriter-
299cb 3e 64 6c 77 2d 3e 69 50 72 65 76 44 6f 63 69 64  >dlw->iPrevDocid
299cc 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 41  );.  dataBufferA
299cd 70 70 65 6e 64 28 70 57 72 69 74 65 72 2d 3e 64  ppend(pWriter->d
299ce 6c 77 2d 3e 62 2c 20 63 2c 20 6e 29 3b 0a 20 20  lw->b, c, n);.  
299cf 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 69 50  pWriter->dlw->iP
299d0 72 65 76 44 6f 63 69 64 20 3d 20 69 44 6f 63 69  revDocid = iDoci
299d1 64 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  d;.#ifndef NDEBU
299d2 47 0a 20 20 70 57 72 69 74 65 72 2d 3e 64 6c 77  G.  pWriter->dlw
299d3 2d 3e 68 61 73 5f 69 50 72 65 76 44 6f 63 69 64  ->has_iPrevDocid
299d4 20 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 20 20   = 1;.#endif..  
299d5 70 57 72 69 74 65 72 2d 3e 69 43 6f 6c 75 6d 6e  pWriter->iColumn
299d6 20 3d 20 30 3b 0a 20 20 70 57 72 69 74 65 72 2d   = 0;.  pWriter-
299d7 3e 69 50 6f 73 20 3d 20 30 3b 0a 20 20 70 57 72  >iPos = 0;.  pWr
299d8 69 74 65 72 2d 3e 69 4f 66 66 73 65 74 20 3d 20  iter->iOffset = 
299d9 30 3b 0a 7d 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  0;.}./* TODO(she
299da 73 73 29 20 53 68 6f 75 6c 64 20 70 6c 77 44 65  ss) Should plwDe
299db 73 74 72 6f 79 28 29 20 61 6c 73 6f 20 74 65 72  stroy() also ter
299dc 6d 69 6e 61 74 65 20 74 68 65 20 64 6f 63 6c 69  minate the docli
299dd 73 74 3f 20 20 42 75 74 0a 2a 2a 20 74 68 65 6e  st?  But.** then
299de 20 70 6c 77 44 65 73 74 72 6f 79 28 29 20 77 6f   plwDestroy() wo
299df 75 6c 64 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65  uld no longer be
299e0 20 6a 75 73 74 20 61 20 64 65 73 74 72 75 63 74   just a destruct
299e1 6f 72 2c 20 69 74 20 77 6f 75 6c 64 0a 2a 2a 20  or, it would.** 
299e2 61 6c 73 6f 20 62 65 20 64 6f 69 6e 67 20 77 6f  also be doing wo
299e3 72 6b 2c 20 77 68 69 63 68 20 69 73 6e 27 74 20  rk, which isn't 
299e4 63 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20  consistent with 
299e5 74 68 65 20 6f 76 65 72 61 6c 6c 20 69 64 69 6f  the overall idio
299e6 6d 2e 0a 2a 2a 20 41 6e 6f 74 68 65 72 20 6f 70  m..** Another op
299e7 74 69 6f 6e 20 77 6f 75 6c 64 20 62 65 20 66 6f  tion would be fo
299e8 72 20 70 6c 77 41 64 64 28 29 20 74 6f 20 61 6c  r plwAdd() to al
299e9 77 61 79 73 20 61 70 70 65 6e 64 20 61 6e 79 20  ways append any 
299ea 6e 65 63 65 73 73 61 72 79 0a 2a 2a 20 74 65 72  necessary.** ter
299eb 6d 69 6e 61 74 6f 72 2c 20 73 6f 20 74 68 61 74  minator, so that
299ec 20 74 68 65 20 6f 75 74 70 75 74 20 69 73 20 61   the output is a
299ed 6c 77 61 79 73 20 63 6f 72 72 65 63 74 2e 20 20  lways correct.  
299ee 42 75 74 20 74 68 61 74 20 77 6f 75 6c 64 0a 2a  But that would.*
299ef 2a 20 61 64 64 20 69 6e 63 72 65 6d 65 6e 74 61  * add incrementa
299f0 6c 20 77 6f 72 6b 20 74 6f 20 74 68 65 20 63 6f  l work to the co
299f1 6d 6d 6f 6e 20 63 61 73 65 20 77 69 74 68 20 74  mmon case with t
299f2 68 65 20 6f 6e 6c 79 20 62 65 6e 65 66 69 74 20  he only benefit 
299f3 62 65 69 6e 67 0a 2a 2a 20 41 50 49 20 65 6c 65  being.** API ele
299f4 67 61 6e 63 65 2e 20 20 50 75 6e 74 20 66 6f 72  gance.  Punt for
299f5 20 6e 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   now..*/.static 
299f6 76 6f 69 64 20 70 6c 77 54 65 72 6d 69 6e 61 74  void plwTerminat
299f7 65 28 50 4c 57 72 69 74 65 72 20 2a 70 57 72 69  e(PLWriter *pWri
299f8 74 65 72 29 7b 0a 20 20 69 66 28 20 70 57 72 69  ter){.  if( pWri
299f9 74 65 72 2d 3e 64 6c 77 2d 3e 69 54 79 70 65 3e  ter->dlw->iType>
299fa 44 4c 5f 44 4f 43 49 44 53 20 29 7b 0a 20 20 20  DL_DOCIDS ){.   
299fb 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d   char c[VARINT_M
299fc 41 58 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d  AX];.    int n =
299fd 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
299fe 2c 20 50 4f 53 5f 45 4e 44 29 3b 0a 20 20 20 20  , POS_END);.    
299ff 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
29a00 28 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 62  (pWriter->dlw->b
29a01 2c 20 63 2c 20 6e 29 3b 0a 20 20 7d 0a 23 69 66  , c, n);.  }.#if
29a02 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a  ndef NDEBUG.  /*
29a03 20 4d 61 72 6b 20 61 73 20 74 65 72 6d 69 6e 61   Mark as termina
29a04 74 65 64 20 66 6f 72 20 61 73 73 65 72 74 20 69  ted for assert i
29a05 6e 20 70 6c 77 41 64 64 28 29 2e 20 2a 2f 0a 20  n plwAdd(). */. 
29a06 20 70 57 72 69 74 65 72 2d 3e 69 50 6f 73 20 3d   pWriter->iPos =
29a07 20 2d 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 73 74   -1;.#endif.}.st
29a08 61 74 69 63 20 76 6f 69 64 20 70 6c 77 44 65 73  atic void plwDes
29a09 74 72 6f 79 28 50 4c 57 72 69 74 65 72 20 2a 70  troy(PLWriter *p
29a0a 57 72 69 74 65 72 29 7b 0a 20 20 53 43 52 41 4d  Writer){.  SCRAM
29a0b 42 4c 45 28 70 57 72 69 74 65 72 29 3b 0a 7d 0a  BLE(pWriter);.}.
29a0c 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
29a0d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29a0e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29a0f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29a10 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44 4c 43 6f 6c 6c  *****/./* DLColl
29a11 65 63 74 6f 72 20 77 72 61 70 73 20 50 4c 57 72  ector wraps PLWr
29a12 69 74 65 72 20 61 6e 64 20 44 4c 57 72 69 74 65  iter and DLWrite
29a13 72 20 74 6f 20 70 72 6f 76 69 64 65 20 61 0a 2a  r to provide a.*
29a14 2a 20 64 79 6e 61 6d 69 63 61 6c 6c 79 2d 61 6c  * dynamically-al
29a15 6c 6f 63 61 74 65 64 20 64 6f 63 6c 69 73 74 20  located doclist 
29a16 61 72 65 61 20 74 6f 20 75 73 65 20 64 75 72 69  area to use duri
29a17 6e 67 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 2e  ng tokenization.
29a18 0a 2a 2a 0a 2a 2a 20 64 6c 63 4e 65 77 20 2d 20  .**.** dlcNew - 
29a19 6d 61 6c 6c 6f 63 20 75 70 20 61 6e 64 20 69 6e  malloc up and in
29a1a 69 74 69 61 6c 69 7a 65 20 61 20 63 6f 6c 6c 65  itialize a colle
29a1b 63 74 6f 72 2e 0a 2a 2a 20 64 6c 63 44 65 6c 65  ctor..** dlcDele
29a1c 74 65 20 2d 20 64 65 73 74 72 6f 79 20 61 20 63  te - destroy a c
29a1d 6f 6c 6c 65 63 74 6f 72 20 61 6e 64 20 61 6c 6c  ollector and all
29a1e 20 63 6f 6e 74 61 69 6e 65 64 20 69 74 65 6d 73   contained items
29a1f 2e 0a 2a 2a 20 64 6c 63 41 64 64 50 6f 73 20 2d  ..** dlcAddPos -
29a20 20 61 70 70 65 6e 64 20 70 6f 73 69 74 69 6f 6e   append position
29a21 20 61 6e 64 20 6f 66 66 73 65 74 20 69 6e 66 6f   and offset info
29a22 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 64 6c 63 41  rmation..** dlcA
29a23 64 64 44 6f 63 6c 69 73 74 20 2d 20 61 64 64 20  ddDoclist - add 
29a24 74 68 65 20 63 6f 6c 6c 65 63 74 65 64 20 64 6f  the collected do
29a25 63 6c 69 73 74 20 74 6f 20 74 68 65 20 67 69 76  clist to the giv
29a26 65 6e 20 62 75 66 66 65 72 2e 0a 2a 2a 20 64 6c  en buffer..** dl
29a27 63 4e 65 78 74 20 2d 20 74 65 72 6d 69 6e 61 74  cNext - terminat
29a28 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 6f  e the current do
29a29 63 75 6d 65 6e 74 20 61 6e 64 20 6f 70 65 6e 20  cument and open 
29a2a 61 6e 6f 74 68 65 72 2e 0a 2a 2f 0a 74 79 70 65  another..*/.type
29a2b 64 65 66 20 73 74 72 75 63 74 20 44 4c 43 6f 6c  def struct DLCol
29a2c 6c 65 63 74 6f 72 20 7b 0a 20 20 44 61 74 61 42  lector {.  DataB
29a2d 75 66 66 65 72 20 62 3b 0a 20 20 44 4c 57 72 69  uffer b;.  DLWri
29a2e 74 65 72 20 64 6c 77 3b 0a 20 20 50 4c 57 72 69  ter dlw;.  PLWri
29a2f 74 65 72 20 70 6c 77 3b 0a 7d 20 44 4c 43 6f 6c  ter plw;.} DLCol
29a30 6c 65 63 74 6f 72 3b 0a 0a 2f 2a 20 54 4f 44 4f  lector;../* TODO
29a31 28 73 68 65 73 73 29 20 54 68 69 73 20 63 6f 75  (shess) This cou
29a32 6c 64 20 61 6c 73 6f 20 62 65 20 64 6f 6e 65 20  ld also be done 
29a33 62 79 20 63 61 6c 6c 69 6e 67 20 70 6c 77 54 65  by calling plwTe
29a34 72 6d 69 6e 61 74 65 28 29 20 61 6e 64 0a 2a 2a  rminate() and.**
29a35 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
29a36 64 28 29 2e 20 20 49 20 74 72 69 65 64 20 74 68  d().  I tried th
29a37 61 74 2c 20 65 78 70 65 63 74 69 6e 67 20 6e 6f  at, expecting no
29a38 6d 69 6e 61 6c 20 70 65 72 66 6f 72 6d 61 6e 63  minal performanc
29a39 65 0a 2a 2a 20 64 69 66 66 65 72 65 6e 63 65 73  e.** differences
29a3a 2c 20 62 75 74 20 69 74 20 73 65 65 6d 65 64 20  , but it seemed 
29a3b 74 6f 20 70 72 65 74 74 79 20 72 65 6c 69 61 62  to pretty reliab
29a3c 6c 79 20 62 65 20 77 6f 72 74 68 20 31 25 20 74  ly be worth 1% t
29a3d 6f 20 63 6f 64 65 0a 2a 2a 20 69 74 20 74 68 69  o code.** it thi
29a3e 73 20 77 61 79 2e 20 20 49 20 73 75 73 70 65 63  s way.  I suspec
29a3f 74 20 69 74 20 69 73 20 74 68 65 20 69 6e 63 72  t it is the incr
29a40 65 6d 65 6e 74 61 6c 20 6d 61 6c 6c 6f 63 20 6f  emental malloc o
29a41 76 65 72 68 65 61 64 20 28 73 6f 6d 65 0a 2a 2a  verhead (some.**
29a42 20 70 65 72 63 65 6e 74 61 67 65 20 6f 66 20 74   percentage of t
29a43 68 65 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 28  he plwTerminate(
29a44 29 20 63 61 6c 6c 73 20 77 69 6c 6c 20 63 61 75  ) calls will cau
29a45 73 65 20 61 20 72 65 61 6c 6c 6f 63 29 2c 20 73  se a realloc), s
29a46 6f 0a 2a 2a 20 74 68 69 73 20 6d 69 67 68 74 20  o.** this might 
29a47 62 65 20 77 6f 72 74 68 20 72 65 76 69 73 69 74  be worth revisit
29a48 69 6e 67 20 69 66 20 74 68 65 20 44 61 74 61 42  ing if the DataB
29a49 75 66 66 65 72 20 69 6d 70 6c 65 6d 65 6e 74 61  uffer implementa
29a4a 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65 73 2e  tion.** changes.
29a4b 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
29a4c 64 6c 63 41 64 64 44 6f 63 6c 69 73 74 28 44 4c  dlcAddDoclist(DL
29a4d 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c 6c  Collector *pColl
29a4e 65 63 74 6f 72 2c 20 44 61 74 61 42 75 66 66 65  ector, DataBuffe
29a4f 72 20 2a 62 29 7b 0a 20 20 69 66 28 20 70 43 6f  r *b){.  if( pCo
29a50 6c 6c 65 63 74 6f 72 2d 3e 64 6c 77 2e 69 54 79  llector->dlw.iTy
29a51 70 65 3e 44 4c 5f 44 4f 43 49 44 53 20 29 7b 0a  pe>DL_DOCIDS ){.
29a52 20 20 20 20 63 68 61 72 20 63 5b 56 41 52 49 4e      char c[VARIN
29a53 54 5f 4d 41 58 5d 3b 0a 20 20 20 20 69 6e 74 20  T_MAX];.    int 
29a54 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e  n = fts3PutVarin
29a55 74 28 63 2c 20 50 4f 53 5f 45 4e 44 29 3b 0a 20  t(c, POS_END);. 
29a56 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70     dataBufferApp
29a57 65 6e 64 32 28 62 2c 20 70 43 6f 6c 6c 65 63 74  end2(b, pCollect
29a58 6f 72 2d 3e 62 2e 70 44 61 74 61 2c 20 70 43 6f  or->b.pData, pCo
29a59 6c 6c 65 63 74 6f 72 2d 3e 62 2e 6e 44 61 74 61  llector->b.nData
29a5a 2c 20 63 2c 20 6e 29 3b 0a 20 20 7d 65 6c 73 65  , c, n);.  }else
29a5b 7b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  {.    dataBuffer
29a5c 41 70 70 65 6e 64 28 62 2c 20 70 43 6f 6c 6c 65  Append(b, pColle
29a5d 63 74 6f 72 2d 3e 62 2e 70 44 61 74 61 2c 20 70  ctor->b.pData, p
29a5e 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62 2e 6e 44 61  Collector->b.nDa
29a5f 74 61 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69  ta);.  }.}.stati
29a60 63 20 76 6f 69 64 20 64 6c 63 4e 65 78 74 28 44  c void dlcNext(D
29a61 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c  LCollector *pCol
29a62 6c 65 63 74 6f 72 2c 20 73 71 6c 69 74 65 5f 69  lector, sqlite_i
29a63 6e 74 36 34 20 69 44 6f 63 69 64 29 7b 0a 20 20  nt64 iDocid){.  
29a64 70 6c 77 54 65 72 6d 69 6e 61 74 65 28 26 70 43  plwTerminate(&pC
29a65 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77 29 3b 0a  ollector->plw);.
29a66 20 20 70 6c 77 44 65 73 74 72 6f 79 28 26 70 43    plwDestroy(&pC
29a67 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77 29 3b 0a  ollector->plw);.
29a68 20 20 70 6c 77 49 6e 69 74 28 26 70 43 6f 6c 6c    plwInit(&pColl
29a69 65 63 74 6f 72 2d 3e 70 6c 77 2c 20 26 70 43 6f  ector->plw, &pCo
29a6a 6c 6c 65 63 74 6f 72 2d 3e 64 6c 77 2c 20 69 44  llector->dlw, iD
29a6b 6f 63 69 64 29 3b 0a 7d 0a 73 74 61 74 69 63 20  ocid);.}.static 
29a6c 76 6f 69 64 20 64 6c 63 41 64 64 50 6f 73 28 44  void dlcAddPos(D
29a6d 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c  LCollector *pCol
29a6e 6c 65 63 74 6f 72 2c 20 69 6e 74 20 69 43 6f 6c  lector, int iCol
29a6f 75 6d 6e 2c 20 69 6e 74 20 69 50 6f 73 2c 0a 20  umn, int iPos,. 
29a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29a71 20 20 20 20 20 69 6e 74 20 69 53 74 61 72 74 4f       int iStartO
29a72 66 66 73 65 74 2c 20 69 6e 74 20 69 45 6e 64 4f  ffset, int iEndO
29a73 66 66 73 65 74 29 7b 0a 20 20 70 6c 77 41 64 64  ffset){.  plwAdd
29a74 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c  (&pCollector->pl
29a75 77 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 50 6f 73  w, iColumn, iPos
29a76 2c 20 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20  , iStartOffset, 
29a77 69 45 6e 64 4f 66 66 73 65 74 29 3b 0a 7d 0a 0a  iEndOffset);.}..
29a78 73 74 61 74 69 63 20 44 4c 43 6f 6c 6c 65 63 74  static DLCollect
29a79 6f 72 20 2a 64 6c 63 4e 65 77 28 73 71 6c 69 74  or *dlcNew(sqlit
29a7a 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 2c 20  e_int64 iDocid, 
29a7b 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79 70  DocListType iTyp
29a7c 65 29 7b 0a 20 20 44 4c 43 6f 6c 6c 65 63 74 6f  e){.  DLCollecto
29a7d 72 20 2a 70 43 6f 6c 6c 65 63 74 6f 72 20 3d 20  r *pCollector = 
29a7e 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
29a7f 69 7a 65 6f 66 28 44 4c 43 6f 6c 6c 65 63 74 6f  izeof(DLCollecto
29a80 72 29 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  r));.  dataBuffe
29a81 72 49 6e 69 74 28 26 70 43 6f 6c 6c 65 63 74 6f  rInit(&pCollecto
29a82 72 2d 3e 62 2c 20 30 29 3b 0a 20 20 64 6c 77 49  r->b, 0);.  dlwI
29a83 6e 69 74 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d  nit(&pCollector-
29a84 3e 64 6c 77 2c 20 69 54 79 70 65 2c 20 26 70 43  >dlw, iType, &pC
29a85 6f 6c 6c 65 63 74 6f 72 2d 3e 62 29 3b 0a 20 20  ollector->b);.  
29a86 70 6c 77 49 6e 69 74 28 26 70 43 6f 6c 6c 65 63  plwInit(&pCollec
29a87 74 6f 72 2d 3e 70 6c 77 2c 20 26 70 43 6f 6c 6c  tor->plw, &pColl
29a88 65 63 74 6f 72 2d 3e 64 6c 77 2c 20 69 44 6f 63  ector->dlw, iDoc
29a89 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 43  id);.  return pC
29a8a 6f 6c 6c 65 63 74 6f 72 3b 0a 7d 0a 73 74 61 74  ollector;.}.stat
29a8b 69 63 20 76 6f 69 64 20 64 6c 63 44 65 6c 65 74  ic void dlcDelet
29a8c 65 28 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70  e(DLCollector *p
29a8d 43 6f 6c 6c 65 63 74 6f 72 29 7b 0a 20 20 70 6c  Collector){.  pl
29a8e 77 44 65 73 74 72 6f 79 28 26 70 43 6f 6c 6c 65  wDestroy(&pColle
29a8f 63 74 6f 72 2d 3e 70 6c 77 29 3b 0a 20 20 64 6c  ctor->plw);.  dl
29a90 77 44 65 73 74 72 6f 79 28 26 70 43 6f 6c 6c 65  wDestroy(&pColle
29a91 63 74 6f 72 2d 3e 64 6c 77 29 3b 0a 20 20 64 61  ctor->dlw);.  da
29a92 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
29a93 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62 29 3b  &pCollector->b);
29a94 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 43 6f 6c  .  SCRAMBLE(pCol
29a95 6c 65 63 74 6f 72 29 3b 0a 20 20 73 71 6c 69 74  lector);.  sqlit
29a96 65 33 5f 66 72 65 65 28 70 43 6f 6c 6c 65 63 74  e3_free(pCollect
29a97 6f 72 29 3b 0a 7d 0a 0a 0a 2f 2a 20 43 6f 70 79  or);.}.../* Copy
29a98 20 74 68 65 20 64 6f 63 6c 69 73 74 20 64 61 74   the doclist dat
29a99 61 20 6f 66 20 69 54 79 70 65 20 69 6e 20 70 44  a of iType in pD
29a9a 61 74 61 2f 6e 44 61 74 61 20 69 6e 74 6f 20 2a  ata/nData into *
29a9b 6f 75 74 2c 20 74 72 69 6d 6d 69 6e 67 0a 2a 2a  out, trimming.**
29a9c 20 75 6e 6e 65 63 65 73 73 61 72 79 20 64 61 74   unnecessary dat
29a9d 61 20 61 73 20 77 65 20 67 6f 2e 20 20 4f 6e 6c  a as we go.  Onl
29a9e 79 20 63 6f 6c 75 6d 6e 73 20 6d 61 74 63 68 69  y columns matchi
29a9f 6e 67 20 69 43 6f 6c 75 6d 6e 20 61 72 65 0a 2a  ng iColumn are.*
29aa0 2a 20 63 6f 70 69 65 64 2c 20 61 6c 6c 20 63 6f  * copied, all co
29aa1 6c 75 6d 6e 73 20 63 6f 70 69 65 64 20 69 66 20  lumns copied if 
29aa2 69 43 6f 6c 75 6d 6e 20 69 73 20 2d 31 2e 20 20  iColumn is -1.  
29aa3 45 6c 65 6d 65 6e 74 73 20 77 69 74 68 20 6e 6f  Elements with no
29aa4 0a 2a 2a 20 6d 61 74 63 68 69 6e 67 20 63 6f 6c  .** matching col
29aa5 75 6d 6e 73 20 61 72 65 20 64 72 6f 70 70 65 64  umns are dropped
29aa6 2e 20 20 54 68 65 20 6f 75 74 70 75 74 20 69 73  .  The output is
29aa7 20 61 6e 20 69 4f 75 74 54 79 70 65 20 64 6f 63   an iOutType doc
29aa8 6c 69 73 74 2e 0a 2a 2f 0a 2f 2a 20 4e 4f 54 45  list..*/./* NOTE
29aa9 28 73 68 65 73 73 29 20 54 68 69 73 20 63 6f 64  (shess) This cod
29aaa 65 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69 64 20  e is only valid 
29aab 61 66 74 65 72 20 61 6c 6c 20 64 6f 63 6c 69 73  after all doclis
29aac 74 73 20 61 72 65 20 6d 65 72 67 65 64 2e 0a 2a  ts are merged..*
29aad 2a 20 49 66 20 74 68 69 73 20 69 73 20 72 75 6e  * If this is run
29aae 20 62 65 66 6f 72 65 20 6d 65 72 67 65 73 2c 20   before merges, 
29aaf 74 68 65 6e 20 64 6f 63 6c 69 73 74 20 69 74 65  then doclist ite
29ab0 6d 73 20 77 68 69 63 68 20 72 65 70 72 65 73 65  ms which represe
29ab1 6e 74 0a 2a 2a 20 64 65 6c 65 74 69 6f 6e 20 77  nt.** deletion w
29ab2 69 6c 6c 20 62 65 20 74 72 69 6d 6d 65 64 2c 20  ill be trimmed, 
29ab3 61 6e 64 20 77 69 6c 6c 20 74 68 75 73 20 6e 6f  and will thus no
29ab4 74 20 65 66 66 65 63 74 20 61 20 64 65 6c 65 74  t effect a delet
29ab5 69 6f 6e 0a 2a 2a 20 64 75 72 69 6e 67 20 74 68  ion.** during th
29ab6 65 20 6d 65 72 67 65 2e 0a 2a 2f 0a 73 74 61 74  e merge..*/.stat
29ab7 69 63 20 76 6f 69 64 20 64 6f 63 4c 69 73 74 54  ic void docListT
29ab8 72 69 6d 28 44 6f 63 4c 69 73 74 54 79 70 65 20  rim(DocListType 
29ab9 69 54 79 70 65 2c 20 63 6f 6e 73 74 20 63 68 61  iType, const cha
29aba 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44  r *pData, int nD
29abb 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
29abc 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74               int
29abd 20 69 43 6f 6c 75 6d 6e 2c 20 44 6f 63 4c 69 73   iColumn, DocLis
29abe 74 54 79 70 65 20 69 4f 75 74 54 79 70 65 2c 20  tType iOutType, 
29abf 44 61 74 61 42 75 66 66 65 72 20 2a 6f 75 74 29  DataBuffer *out)
29ac0 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 64 6c 52  {.  DLReader dlR
29ac1 65 61 64 65 72 3b 0a 20 20 44 4c 57 72 69 74 65  eader;.  DLWrite
29ac2 72 20 64 6c 57 72 69 74 65 72 3b 0a 0a 20 20 61  r dlWriter;..  a
29ac3 73 73 65 72 74 28 20 69 4f 75 74 54 79 70 65 3c  ssert( iOutType<
29ac4 3d 69 54 79 70 65 20 29 3b 0a 0a 20 20 64 6c 72  =iType );..  dlr
29ac5 49 6e 69 74 28 26 64 6c 52 65 61 64 65 72 2c 20  Init(&dlReader, 
29ac6 69 54 79 70 65 2c 20 70 44 61 74 61 2c 20 6e 44  iType, pData, nD
29ac7 61 74 61 29 3b 0a 20 20 64 6c 77 49 6e 69 74 28  ata);.  dlwInit(
29ac8 26 64 6c 57 72 69 74 65 72 2c 20 69 4f 75 74 54  &dlWriter, iOutT
29ac9 79 70 65 2c 20 6f 75 74 29 3b 0a 0a 20 20 77 68  ype, out);..  wh
29aca 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64 28 26  ile( !dlrAtEnd(&
29acb 64 6c 52 65 61 64 65 72 29 20 29 7b 0a 20 20 20  dlReader) ){.   
29acc 20 50 4c 52 65 61 64 65 72 20 70 6c 52 65 61 64   PLReader plRead
29acd 65 72 3b 0a 20 20 20 20 50 4c 57 72 69 74 65 72  er;.    PLWriter
29ace 20 70 6c 57 72 69 74 65 72 3b 0a 20 20 20 20 69   plWriter;.    i
29acf 6e 74 20 6d 61 74 63 68 20 3d 20 30 3b 0a 0a 20  nt match = 0;.. 
29ad0 20 20 20 70 6c 72 49 6e 69 74 28 26 70 6c 52 65     plrInit(&plRe
29ad1 61 64 65 72 2c 20 26 64 6c 52 65 61 64 65 72 29  ader, &dlReader)
29ad2 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 21 70  ;..    while( !p
29ad3 6c 72 41 74 45 6e 64 28 26 70 6c 52 65 61 64 65  lrAtEnd(&plReade
29ad4 72 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  r) ){.      if( 
29ad5 69 43 6f 6c 75 6d 6e 3d 3d 2d 31 20 7c 7c 20 70  iColumn==-1 || p
29ad6 6c 72 43 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64  lrColumn(&plRead
29ad7 65 72 29 3d 3d 69 43 6f 6c 75 6d 6e 20 29 7b 0a  er)==iColumn ){.
29ad8 20 20 20 20 20 20 20 20 69 66 28 20 21 6d 61 74          if( !mat
29ad9 63 68 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ch ){.          
29ada 70 6c 77 49 6e 69 74 28 26 70 6c 57 72 69 74 65  plwInit(&plWrite
29adb 72 2c 20 26 64 6c 57 72 69 74 65 72 2c 20 64 6c  r, &dlWriter, dl
29adc 72 44 6f 63 69 64 28 26 64 6c 52 65 61 64 65 72  rDocid(&dlReader
29add 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6d 61  ));.          ma
29ade 74 63 68 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  tch = 1;.       
29adf 20 7d 0a 20 20 20 20 20 20 20 20 70 6c 77 41 64   }.        plwAd
29ae0 64 28 26 70 6c 57 72 69 74 65 72 2c 20 70 6c 72  d(&plWriter, plr
29ae1 43 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64 65 72  Column(&plReader
29ae2 29 2c 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26  ), plrPosition(&
29ae3 70 6c 52 65 61 64 65 72 29 2c 0a 20 20 20 20 20  plReader),.     
29ae4 20 20 20 20 20 20 20 20 20 20 70 6c 72 53 74 61            plrSta
29ae5 72 74 4f 66 66 73 65 74 28 26 70 6c 52 65 61 64  rtOffset(&plRead
29ae6 65 72 29 2c 20 70 6c 72 45 6e 64 4f 66 66 73 65  er), plrEndOffse
29ae7 74 28 26 70 6c 52 65 61 64 65 72 29 29 3b 0a 20  t(&plReader));. 
29ae8 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 6c 72       }.      plr
29ae9 53 74 65 70 28 26 70 6c 52 65 61 64 65 72 29 3b  Step(&plReader);
29aea 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d  .    }.    if( m
29aeb 61 74 63 68 20 29 7b 0a 20 20 20 20 20 20 70 6c  atch ){.      pl
29aec 77 54 65 72 6d 69 6e 61 74 65 28 26 70 6c 57 72  wTerminate(&plWr
29aed 69 74 65 72 29 3b 0a 20 20 20 20 20 20 70 6c 77  iter);.      plw
29aee 44 65 73 74 72 6f 79 28 26 70 6c 57 72 69 74 65  Destroy(&plWrite
29aef 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70  r);.    }..    p
29af0 6c 72 44 65 73 74 72 6f 79 28 26 70 6c 52 65 61  lrDestroy(&plRea
29af1 64 65 72 29 3b 0a 20 20 20 20 64 6c 72 53 74 65  der);.    dlrSte
29af2 70 28 26 64 6c 52 65 61 64 65 72 29 3b 0a 20 20  p(&dlReader);.  
29af3 7d 0a 20 20 64 6c 77 44 65 73 74 72 6f 79 28 26  }.  dlwDestroy(&
29af4 64 6c 57 72 69 74 65 72 29 3b 0a 20 20 64 6c 72  dlWriter);.  dlr
29af5 44 65 73 74 72 6f 79 28 26 64 6c 52 65 61 64 65  Destroy(&dlReade
29af6 72 29 3b 0a 7d 0a 0a 2f 2a 20 55 73 65 64 20 62  r);.}../* Used b
29af7 79 20 64 6f 63 4c 69 73 74 4d 65 72 67 65 28 29  y docListMerge()
29af8 20 74 6f 20 6b 65 65 70 20 64 6f 63 6c 69 73 74   to keep doclist
29af9 73 20 69 6e 20 74 68 65 20 61 73 63 65 6e 64 69  s in the ascendi
29afa 6e 67 20 6f 72 64 65 72 20 62 79 0a 2a 2a 20 64  ng order by.** d
29afb 6f 63 69 64 2c 20 74 68 65 6e 20 61 73 63 65 6e  ocid, then ascen
29afc 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 61 67  ding order by ag
29afd 65 20 28 73 6f 20 74 68 65 20 6e 65 77 65 73 74  e (so the newest
29afe 20 63 6f 6d 65 73 20 66 69 72 73 74 29 2e 0a 2a   comes first)..*
29aff 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
29b00 20 4f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72   OrderedDLReader
29b01 20 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 2a 70   {.  DLReader *p
29b02 52 65 61 64 65 72 3b 0a 0a 20 20 2f 2a 20 54 4f  Reader;..  /* TO
29b03 44 4f 28 73 68 65 73 73 29 20 49 66 20 77 65 20  DO(shess) If we 
29b04 61 73 73 75 6d 65 20 74 68 61 74 20 64 6f 63 4c  assume that docL
29b05 69 73 74 4d 65 72 67 65 20 70 52 65 61 64 65 72  istMerge pReader
29b06 73 20 69 73 20 6f 72 64 65 72 65 64 20 62 79 0a  s is ordered by.
29b07 20 20 2a 2a 20 61 67 65 20 28 77 68 69 63 68 20    ** age (which 
29b08 77 65 20 64 6f 29 2c 20 74 68 65 6e 20 77 65 20  we do), then we 
29b09 63 6f 75 6c 64 20 75 73 65 20 70 52 65 61 64 65  could use pReade
29b0a 72 20 63 6f 6d 70 61 72 69 73 6f 6e 73 20 74 6f  r comparisons to
29b0b 20 62 72 65 61 6b 0a 20 20 2a 2a 20 74 69 65 73   break.  ** ties
29b0c 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78  ..  */.  int idx
29b0d 3b 0a 7d 20 4f 72 64 65 72 65 64 44 4c 52 65 61  ;.} OrderedDLRea
29b0e 64 65 72 3b 0a 0a 2f 2a 20 4f 72 64 65 72 20 65  der;../* Order e
29b0f 6f 66 20 74 6f 20 65 6e 64 2c 20 74 68 65 6e 20  of to end, then 
29b10 62 79 20 64 6f 63 69 64 20 61 73 63 2c 20 69 64  by docid asc, id
29b11 78 20 64 65 73 63 2e 20 2a 2f 0a 73 74 61 74 69  x desc. */.stati
29b12 63 20 69 6e 74 20 6f 72 64 65 72 65 64 44 4c 52  c int orderedDLR
29b13 65 61 64 65 72 43 6d 70 28 4f 72 64 65 72 65 64  eaderCmp(Ordered
29b14 44 4c 52 65 61 64 65 72 20 2a 72 31 2c 20 4f 72  DLReader *r1, Or
29b15 64 65 72 65 64 44 4c 52 65 61 64 65 72 20 2a 72  deredDLReader *r
29b16 32 29 7b 0a 20 20 69 66 28 20 64 6c 72 41 74 45  2){.  if( dlrAtE
29b17 6e 64 28 72 31 2d 3e 70 52 65 61 64 65 72 29 20  nd(r1->pReader) 
29b18 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72 41 74  ){.    if( dlrAt
29b19 45 6e 64 28 72 32 2d 3e 70 52 65 61 64 65 72 29  End(r2->pReader)
29b1a 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a   ) return 0;  /*
29b1b 20 42 6f 74 68 20 61 74 45 6e 64 28 29 2e 20 2a   Both atEnd(). *
29b1c 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 20  /.    return 1; 
29b1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29b1e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
29b1f 4f 6e 6c 79 20 72 31 20 61 74 45 6e 64 28 29 2e  Only r1 atEnd().
29b20 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 64 6c   */.  }.  if( dl
29b21 72 41 74 45 6e 64 28 72 32 2d 3e 70 52 65 61 64  rAtEnd(r2->pRead
29b22 65 72 29 20 29 20 72 65 74 75 72 6e 20 2d 31 3b  er) ) return -1;
29b23 20 20 20 2f 2a 20 4f 6e 6c 79 20 72 32 20 61 74     /* Only r2 at
29b24 45 6e 64 28 29 2e 20 2a 2f 0a 0a 20 20 69 66 28  End(). */..  if(
29b25 20 64 6c 72 44 6f 63 69 64 28 72 31 2d 3e 70 52   dlrDocid(r1->pR
29b26 65 61 64 65 72 29 3c 64 6c 72 44 6f 63 69 64 28  eader)<dlrDocid(
29b27 72 32 2d 3e 70 52 65 61 64 65 72 29 20 29 20 72  r2->pReader) ) r
29b28 65 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66 28 20  eturn -1;.  if( 
29b29 64 6c 72 44 6f 63 69 64 28 72 31 2d 3e 70 52 65  dlrDocid(r1->pRe
29b2a 61 64 65 72 29 3e 64 6c 72 44 6f 63 69 64 28 72  ader)>dlrDocid(r
29b2b 32 2d 3e 70 52 65 61 64 65 72 29 20 29 20 72 65  2->pReader) ) re
29b2c 74 75 72 6e 20 31 3b 0a 0a 20 20 2f 2a 20 44 65  turn 1;..  /* De
29b2d 73 63 65 6e 64 69 6e 67 20 6f 6e 20 69 64 78 2e  scending on idx.
29b2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 32 2d   */.  return r2-
29b2f 3e 69 64 78 2d 72 31 2d 3e 69 64 78 3b 0a 7d 0a  >idx-r1->idx;.}.
29b30 0a 2f 2a 20 42 75 62 62 6c 65 20 70 5b 30 5d 20  ./* Bubble p[0] 
29b31 74 6f 20 61 70 70 72 6f 70 72 69 61 74 65 20 70  to appropriate p
29b32 6c 61 63 65 20 69 6e 20 70 5b 31 2e 2e 6e 2d 31  lace in p[1..n-1
29b33 5d 2e 20 20 41 73 73 75 6d 65 73 20 74 68 61 74  ].  Assumes that
29b34 0a 2a 2a 20 70 5b 31 2e 2e 6e 2d 31 5d 20 69 73  .** p[1..n-1] is
29b35 20 61 6c 72 65 61 64 79 20 73 6f 72 74 65 64 2e   already sorted.
29b36 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  .*/./* TODO(shes
29b37 73 29 20 49 73 20 74 68 69 73 20 66 72 65 71 75  s) Is this frequ
29b38 65 6e 74 20 65 6e 6f 75 67 68 20 74 6f 20 77 61  ent enough to wa
29b39 72 72 61 6e 74 20 61 20 62 69 6e 61 72 79 20 73  rrant a binary s
29b3a 65 61 72 63 68 3f 0a 2a 2a 20 42 65 66 6f 72 65  earch?.** Before
29b3b 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68   implementing th
29b3c 61 74 2c 20 69 6e 73 74 72 75 6d 65 6e 74 20 74  at, instrument t
29b3d 68 65 20 63 6f 64 65 20 74 6f 20 63 68 65 63 6b  he code to check
29b3e 2e 20 20 49 6e 20 6d 6f 73 74 0a 2a 2a 20 63 75  .  In most.** cu
29b3f 72 72 65 6e 74 20 75 73 61 67 65 2c 20 49 20 65  rrent usage, I e
29b40 78 70 65 63 74 20 74 68 61 74 20 70 5b 30 5d 20  xpect that p[0] 
29b41 77 69 6c 6c 20 62 65 20 6c 65 73 73 20 74 68 61  will be less tha
29b42 6e 20 70 5b 31 5d 20 61 20 76 65 72 79 0a 2a 2a  n p[1] a very.**
29b43 20 68 69 67 68 20 70 72 6f 70 6f 72 74 69 6f 6e   high proportion
29b44 20 6f 66 20 74 68 65 20 74 69 6d 65 2e 0a 2a 2f   of the time..*/
29b45 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 72 64  .static void ord
29b46 65 72 65 64 44 4c 52 65 61 64 65 72 52 65 6f 72  eredDLReaderReor
29b47 64 65 72 28 4f 72 64 65 72 65 64 44 4c 52 65 61  der(OrderedDLRea
29b48 64 65 72 20 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a  der *p, int n){.
29b49 20 20 77 68 69 6c 65 28 20 6e 3e 31 20 26 26 20    while( n>1 && 
29b4a 6f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72 43  orderedDLReaderC
29b4b 6d 70 28 70 2c 20 70 2b 31 29 3e 30 20 29 7b 0a  mp(p, p+1)>0 ){.
29b4c 20 20 20 20 4f 72 64 65 72 65 64 44 4c 52 65 61      OrderedDLRea
29b4d 64 65 72 20 74 6d 70 20 3d 20 70 5b 30 5d 3b 0a  der tmp = p[0];.
29b4e 20 20 20 20 70 5b 30 5d 20 3d 20 70 5b 31 5d 3b      p[0] = p[1];
29b4f 0a 20 20 20 20 70 5b 31 5d 20 3d 20 74 6d 70 3b  .    p[1] = tmp;
29b50 0a 20 20 20 20 6e 2d 2d 3b 0a 20 20 20 20 70 2b  .    n--;.    p+
29b51 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 47 69 76  +;.  }.}../* Giv
29b52 65 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 64  en an array of d
29b53 6f 63 6c 69 73 74 20 72 65 61 64 65 72 73 2c 20  oclist readers, 
29b54 6d 65 72 67 65 20 74 68 65 69 72 20 64 6f 63 6c  merge their docl
29b55 69 73 74 20 65 6c 65 6d 65 6e 74 73 0a 2a 2a 20  ist elements.** 
29b56 69 6e 74 6f 20 6f 75 74 20 69 6e 20 73 6f 72 74  into out in sort
29b57 65 64 20 6f 72 64 65 72 20 28 62 79 20 64 6f 63  ed order (by doc
29b58 69 64 29 2c 20 64 72 6f 70 70 69 6e 67 20 65 6c  id), dropping el
29b59 65 6d 65 6e 74 73 20 66 72 6f 6d 20 6f 6c 64 65  ements from olde
29b5a 72 0a 2a 2a 20 72 65 61 64 65 72 73 20 77 68 65  r.** readers whe
29b5b 6e 20 74 68 65 72 65 20 69 73 20 61 20 64 75 70  n there is a dup
29b5c 6c 69 63 61 74 65 20 64 6f 63 69 64 2e 20 20 70  licate docid.  p
29b5d 52 65 61 64 65 72 73 20 69 73 20 61 73 73 75 6d  Readers is assum
29b5e 65 64 20 74 6f 20 62 65 0a 2a 2a 20 6f 72 64 65  ed to be.** orde
29b5f 72 65 64 20 62 79 20 61 67 65 2c 20 6f 6c 64 65  red by age, olde
29b60 73 74 20 66 69 72 73 74 2e 0a 2a 2f 0a 2f 2a 20  st first..*/./* 
29b61 54 4f 44 4f 28 73 68 65 73 73 29 20 6e 52 65 61  TODO(shess) nRea
29b62 64 65 72 73 20 6d 75 73 74 20 62 65 20 3c 3d 20  ders must be <= 
29b63 4d 45 52 47 45 5f 43 4f 55 4e 54 2e 20 20 54 68  MERGE_COUNT.  Th
29b64 69 73 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 62  is should probab
29b65 6c 79 0a 2a 2a 20 62 65 20 66 69 78 65 64 2e 0a  ly.** be fixed..
29b66 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  */.static void d
29b67 6f 63 4c 69 73 74 4d 65 72 67 65 28 44 61 74 61  ocListMerge(Data
29b68 42 75 66 66 65 72 20 2a 6f 75 74 2c 0a 20 20 20  Buffer *out,.   
29b69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29b6a 20 20 20 20 20 20 44 4c 52 65 61 64 65 72 20 2a        DLReader *
29b6b 70 52 65 61 64 65 72 73 2c 20 69 6e 74 20 6e 52  pReaders, int nR
29b6c 65 61 64 65 72 73 29 7b 0a 20 20 4f 72 64 65 72  eaders){.  Order
29b6d 65 64 44 4c 52 65 61 64 65 72 20 72 65 61 64 65  edDLReader reade
29b6e 72 73 5b 4d 45 52 47 45 5f 43 4f 55 4e 54 5d 3b  rs[MERGE_COUNT];
29b6f 0a 20 20 44 4c 57 72 69 74 65 72 20 77 72 69 74  .  DLWriter writ
29b70 65 72 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a  er;.  int i, n;.
29b71 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 53    const char *pS
29b72 74 61 72 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20  tart = 0;.  int 
29b73 6e 53 74 61 72 74 20 3d 20 30 3b 0a 20 20 73 71  nStart = 0;.  sq
29b74 6c 69 74 65 5f 69 6e 74 36 34 20 69 46 69 72 73  lite_int64 iFirs
29b75 74 44 6f 63 69 64 20 3d 20 30 2c 20 69 4c 61 73  tDocid = 0, iLas
29b76 74 44 6f 63 69 64 20 3d 20 30 3b 0a 0a 20 20 61  tDocid = 0;..  a
29b77 73 73 65 72 74 28 20 6e 52 65 61 64 65 72 73 3e  ssert( nReaders>
29b78 30 20 29 3b 0a 20 20 69 66 28 20 6e 52 65 61 64  0 );.  if( nRead
29b79 65 72 73 3d 3d 31 20 29 7b 0a 20 20 20 20 64 61  ers==1 ){.    da
29b7a 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 6f  taBufferAppend(o
29b7b 75 74 2c 20 64 6c 72 44 6f 63 44 61 74 61 28 70  ut, dlrDocData(p
29b7c 52 65 61 64 65 72 73 29 2c 20 64 6c 72 41 6c 6c  Readers), dlrAll
29b7d 44 61 74 61 42 79 74 65 73 28 70 52 65 61 64 65  DataBytes(pReade
29b7e 72 73 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  rs));.    return
29b7f 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  ;.  }..  assert(
29b80 20 6e 52 65 61 64 65 72 73 3c 3d 4d 45 52 47 45   nReaders<=MERGE
29b81 5f 43 4f 55 4e 54 20 29 3b 0a 20 20 6e 20 3d 20  _COUNT );.  n = 
29b82 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  0;.  for(i=0; i<
29b83 6e 52 65 61 64 65 72 73 3b 20 69 2b 2b 29 7b 0a  nReaders; i++){.
29b84 20 20 20 20 61 73 73 65 72 74 28 20 70 52 65 61      assert( pRea
29b85 64 65 72 73 5b 69 5d 2e 69 54 79 70 65 3d 3d 70  ders[i].iType==p
29b86 52 65 61 64 65 72 73 5b 30 5d 2e 69 54 79 70 65  Readers[0].iType
29b87 20 29 3b 0a 20 20 20 20 72 65 61 64 65 72 73 5b   );.    readers[
29b88 69 5d 2e 70 52 65 61 64 65 72 20 3d 20 70 52 65  i].pReader = pRe
29b89 61 64 65 72 73 2b 69 3b 0a 20 20 20 20 72 65 61  aders+i;.    rea
29b8a 64 65 72 73 5b 69 5d 2e 69 64 78 20 3d 20 69 3b  ders[i].idx = i;
29b8b 0a 20 20 20 20 6e 20 2b 3d 20 64 6c 72 41 6c 6c  .    n += dlrAll
29b8c 44 61 74 61 42 79 74 65 73 28 26 70 52 65 61 64  DataBytes(&pRead
29b8d 65 72 73 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 2f  ers[i]);.  }.  /
29b8e 2a 20 43 6f 6e 73 65 72 76 61 74 69 76 65 6c 79  * Conservatively
29b8f 20 73 69 7a 65 20 6f 75 74 70 75 74 20 74 6f 20   size output to 
29b90 73 75 6d 20 6f 66 20 69 6e 70 75 74 73 2e 20 20  sum of inputs.  
29b91 4f 75 74 70 75 74 20 73 68 6f 75 6c 64 20 65 6e  Output should en
29b92 64 0a 20 20 2a 2a 20 75 70 20 73 74 72 69 63 74  d.  ** up strict
29b93 6c 79 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20  ly smaller than 
29b94 69 6e 70 75 74 2e 0a 20 20 2a 2f 0a 20 20 64 61  input..  */.  da
29b95 74 61 42 75 66 66 65 72 45 78 70 61 6e 64 28 6f  taBufferExpand(o
29b96 75 74 2c 20 6e 29 3b 0a 0a 20 20 2f 2a 20 47 65  ut, n);..  /* Ge
29b97 74 20 74 68 65 20 72 65 61 64 65 72 73 20 69 6e  t the readers in
29b98 74 6f 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e  to sorted order.
29b99 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69 2d 2d   */.  while( i--
29b9a 3e 30 20 29 7b 0a 20 20 20 20 6f 72 64 65 72 65  >0 ){.    ordere
29b9b 64 44 4c 52 65 61 64 65 72 52 65 6f 72 64 65 72  dDLReaderReorder
29b9c 28 72 65 61 64 65 72 73 2b 69 2c 20 6e 52 65 61  (readers+i, nRea
29b9d 64 65 72 73 2d 69 29 3b 0a 20 20 7d 0a 0a 20 20  ders-i);.  }..  
29b9e 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72 2c  dlwInit(&writer,
29b9f 20 70 52 65 61 64 65 72 73 5b 30 5d 2e 69 54 79   pReaders[0].iTy
29ba0 70 65 2c 20 6f 75 74 29 3b 0a 20 20 77 68 69 6c  pe, out);.  whil
29ba1 65 28 20 21 64 6c 72 41 74 45 6e 64 28 72 65 61  e( !dlrAtEnd(rea
29ba2 64 65 72 73 5b 30 5d 2e 70 52 65 61 64 65 72 29  ders[0].pReader)
29ba3 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 5f 69   ){.    sqlite_i
29ba4 6e 74 36 34 20 69 44 6f 63 69 64 20 3d 20 64 6c  nt64 iDocid = dl
29ba5 72 44 6f 63 69 64 28 72 65 61 64 65 72 73 5b 30  rDocid(readers[0
29ba6 5d 2e 70 52 65 61 64 65 72 29 3b 0a 0a 20 20 20  ].pReader);..   
29ba7 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
29ba8 20 63 6f 6e 74 69 6e 75 61 74 69 6f 6e 20 6f 66   continuation of
29ba9 20 74 68 65 20 63 75 72 72 65 6e 74 20 62 75 66   the current buf
29baa 66 65 72 20 74 6f 20 63 6f 70 79 2c 20 65 78 74  fer to copy, ext
29bab 65 6e 64 0a 20 20 20 20 2a 2a 20 74 68 61 74 20  end.    ** that 
29bac 62 75 66 66 65 72 2e 20 20 6d 65 6d 63 70 79 28  buffer.  memcpy(
29bad 29 20 73 65 65 6d 73 20 74 6f 20 62 65 20 6d 6f  ) seems to be mo
29bae 72 65 20 65 66 66 69 63 69 65 6e 74 20 69 66 20  re efficient if 
29baf 69 74 20 68 61 73 20 61 0a 20 20 20 20 2a 2a 20  it has a.    ** 
29bb0 6c 6f 74 73 20 6f 66 20 64 61 74 61 20 74 6f 20  lots of data to 
29bb1 63 6f 70 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  copy..    */.   
29bb2 20 69 66 28 20 64 6c 72 44 6f 63 44 61 74 61 28   if( dlrDocData(
29bb3 72 65 61 64 65 72 73 5b 30 5d 2e 70 52 65 61 64  readers[0].pRead
29bb4 65 72 29 3d 3d 70 53 74 61 72 74 2b 6e 53 74 61  er)==pStart+nSta
29bb5 72 74 20 29 7b 0a 20 20 20 20 20 20 6e 53 74 61  rt ){.      nSta
29bb6 72 74 20 2b 3d 20 64 6c 72 44 6f 63 44 61 74 61  rt += dlrDocData
29bb7 42 79 74 65 73 28 72 65 61 64 65 72 73 5b 30 5d  Bytes(readers[0]
29bb8 2e 70 52 65 61 64 65 72 29 3b 0a 20 20 20 20 7d  .pReader);.    }
29bb9 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20  else{.      if( 
29bba 70 53 74 61 72 74 21 3d 30 20 29 7b 0a 20 20 20  pStart!=0 ){.   
29bbb 20 20 20 20 20 64 6c 77 41 70 70 65 6e 64 28 26       dlwAppend(&
29bbc 77 72 69 74 65 72 2c 20 70 53 74 61 72 74 2c 20  writer, pStart, 
29bbd 6e 53 74 61 72 74 2c 20 69 46 69 72 73 74 44 6f  nStart, iFirstDo
29bbe 63 69 64 2c 20 69 4c 61 73 74 44 6f 63 69 64 29  cid, iLastDocid)
29bbf 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
29bc0 70 53 74 61 72 74 20 3d 20 64 6c 72 44 6f 63 44  pStart = dlrDocD
29bc1 61 74 61 28 72 65 61 64 65 72 73 5b 30 5d 2e 70  ata(readers[0].p
29bc2 52 65 61 64 65 72 29 3b 0a 20 20 20 20 20 20 6e  Reader);.      n
29bc3 53 74 61 72 74 20 3d 20 64 6c 72 44 6f 63 44 61  Start = dlrDocDa
29bc4 74 61 42 79 74 65 73 28 72 65 61 64 65 72 73 5b  taBytes(readers[
29bc5 30 5d 2e 70 52 65 61 64 65 72 29 3b 0a 20 20 20  0].pReader);.   
29bc6 20 20 20 69 46 69 72 73 74 44 6f 63 69 64 20 3d     iFirstDocid =
29bc7 20 69 44 6f 63 69 64 3b 0a 20 20 20 20 7d 0a 20   iDocid;.    }. 
29bc8 20 20 20 69 4c 61 73 74 44 6f 63 69 64 20 3d 20     iLastDocid = 
29bc9 69 44 6f 63 69 64 3b 0a 20 20 20 20 64 6c 72 53  iDocid;.    dlrS
29bca 74 65 70 28 72 65 61 64 65 72 73 5b 30 5d 2e 70  tep(readers[0].p
29bcb 52 65 61 64 65 72 29 3b 0a 0a 20 20 20 20 2f 2a  Reader);..    /*
29bcc 20 44 72 6f 70 20 61 6c 6c 20 6f 66 20 74 68 65   Drop all of the
29bcd 20 6f 6c 64 65 72 20 65 6c 65 6d 65 6e 74 73 20   older elements 
29bce 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 64 6f  with the same do
29bcf 63 69 64 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28  cid. */.    for(
29bd0 69 3d 31 3b 20 69 3c 6e 52 65 61 64 65 72 73 20  i=1; i<nReaders 
29bd1 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  &&.             
29bd2 21 64 6c 72 41 74 45 6e 64 28 72 65 61 64 65 72  !dlrAtEnd(reader
29bd3 73 5b 69 5d 2e 70 52 65 61 64 65 72 29 20 26 26  s[i].pReader) &&
29bd4 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 64 6c  .             dl
29bd5 72 44 6f 63 69 64 28 72 65 61 64 65 72 73 5b 69  rDocid(readers[i
29bd6 5d 2e 70 52 65 61 64 65 72 29 3d 3d 69 44 6f 63  ].pReader)==iDoc
29bd7 69 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  id; i++){.      
29bd8 64 6c 72 53 74 65 70 28 72 65 61 64 65 72 73 5b  dlrStep(readers[
29bd9 69 5d 2e 70 52 65 61 64 65 72 29 3b 0a 20 20 20  i].pReader);.   
29bda 20 7d 0a 0a 20 20 20 20 2f 2a 20 47 65 74 20 74   }..    /* Get t
29bdb 68 65 20 72 65 61 64 65 72 73 20 62 61 63 6b 20  he readers back 
29bdc 69 6e 74 6f 20 6f 72 64 65 72 2e 20 2a 2f 0a 20  into order. */. 
29bdd 20 20 20 77 68 69 6c 65 28 20 69 2d 2d 3e 30 20     while( i-->0 
29bde 29 7b 0a 20 20 20 20 20 20 6f 72 64 65 72 65 64  ){.      ordered
29bdf 44 4c 52 65 61 64 65 72 52 65 6f 72 64 65 72 28  DLReaderReorder(
29be0 72 65 61 64 65 72 73 2b 69 2c 20 6e 52 65 61 64  readers+i, nRead
29be1 65 72 73 2d 69 29 3b 0a 20 20 20 20 7d 0a 20 20  ers-i);.    }.  
29be2 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 6f 76 65  }..  /* Copy ove
29be3 72 20 61 6e 79 20 72 65 6d 61 69 6e 69 6e 67 20  r any remaining 
29be4 65 6c 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20 20 69  elements. */.  i
29be5 66 28 20 6e 53 74 61 72 74 3e 30 20 29 20 64 6c  f( nStart>0 ) dl
29be6 77 41 70 70 65 6e 64 28 26 77 72 69 74 65 72 2c  wAppend(&writer,
29be7 20 70 53 74 61 72 74 2c 20 6e 53 74 61 72 74 2c   pStart, nStart,
29be8 20 69 46 69 72 73 74 44 6f 63 69 64 2c 20 69 4c   iFirstDocid, iL
29be9 61 73 74 44 6f 63 69 64 29 3b 0a 20 20 64 6c 77  astDocid);.  dlw
29bea 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29  Destroy(&writer)
29beb 3b 0a 7d 0a 0a 2f 2a 20 48 65 6c 70 65 72 20 66  ;.}../* Helper f
29bec 75 6e 63 74 69 6f 6e 20 66 6f 72 20 70 6f 73 4c  unction for posL
29bed 69 73 74 55 6e 69 6f 6e 28 29 2e 20 20 43 6f 6d  istUnion().  Com
29bee 70 61 72 65 73 20 74 68 65 20 63 75 72 72 65 6e  pares the curren
29bef 74 20 70 6f 73 69 74 69 6f 6e 0a 2a 2a 20 62 65  t position.** be
29bf0 74 77 65 65 6e 20 6c 65 66 74 20 61 6e 64 20 72  tween left and r
29bf1 69 67 68 74 2c 20 72 65 74 75 72 6e 69 6e 67 20  ight, returning 
29bf2 61 73 20 73 74 61 6e 64 61 72 64 20 43 20 69 64  as standard C id
29bf3 69 6f 6d 20 6f 66 20 3c 30 20 69 66 0a 2a 2a 20  iom of <0 if.** 
29bf4 6c 65 66 74 3c 72 69 67 68 74 2c 20 3e 30 20 69  left<right, >0 i
29bf5 66 20 6c 65 66 74 3e 72 69 67 68 74 2c 20 61 6e  f left>right, an
29bf6 64 20 30 20 69 66 20 6c 65 66 74 3d 3d 72 69 67  d 0 if left==rig
29bf7 68 74 2e 20 20 22 45 6e 64 22 20 61 6c 77 61 79  ht.  "End" alway
29bf8 73 0a 2a 2a 20 63 6f 6d 70 61 72 65 73 20 67 72  s.** compares gr
29bf9 65 61 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  eater..*/.static
29bfa 20 69 6e 74 20 70 6f 73 4c 69 73 74 43 6d 70 28   int posListCmp(
29bfb 50 4c 52 65 61 64 65 72 20 2a 70 4c 65 66 74 2c  PLReader *pLeft,
29bfc 20 50 4c 52 65 61 64 65 72 20 2a 70 52 69 67 68   PLReader *pRigh
29bfd 74 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4c  t){.  assert( pL
29bfe 65 66 74 2d 3e 69 54 79 70 65 3d 3d 70 52 69 67  eft->iType==pRig
29bff 68 74 2d 3e 69 54 79 70 65 20 29 3b 0a 20 20 69  ht->iType );.  i
29c00 66 28 20 70 4c 65 66 74 2d 3e 69 54 79 70 65 3d  f( pLeft->iType=
29c01 3d 44 4c 5f 44 4f 43 49 44 53 20 29 20 72 65 74  =DL_DOCIDS ) ret
29c02 75 72 6e 20 30 3b 0a 0a 20 20 69 66 28 20 70 6c  urn 0;..  if( pl
29c03 72 41 74 45 6e 64 28 70 4c 65 66 74 29 20 29 20  rAtEnd(pLeft) ) 
29c04 72 65 74 75 72 6e 20 70 6c 72 41 74 45 6e 64 28  return plrAtEnd(
29c05 70 52 69 67 68 74 29 20 3f 20 30 20 3a 20 31 3b  pRight) ? 0 : 1;
29c06 0a 20 20 69 66 28 20 70 6c 72 41 74 45 6e 64 28  .  if( plrAtEnd(
29c07 70 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e  pRight) ) return
29c08 20 2d 31 3b 0a 0a 20 20 69 66 28 20 70 6c 72 43   -1;..  if( plrC
29c09 6f 6c 75 6d 6e 28 70 4c 65 66 74 29 3c 70 6c 72  olumn(pLeft)<plr
29c0a 43 6f 6c 75 6d 6e 28 70 52 69 67 68 74 29 20 29  Column(pRight) )
29c0b 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66   return -1;.  if
29c0c 28 20 70 6c 72 43 6f 6c 75 6d 6e 28 70 4c 65 66  ( plrColumn(pLef
29c0d 74 29 3e 70 6c 72 43 6f 6c 75 6d 6e 28 70 52 69  t)>plrColumn(pRi
29c0e 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 31 3b  ght) ) return 1;
29c0f 0a 0a 20 20 69 66 28 20 70 6c 72 50 6f 73 69 74  ..  if( plrPosit
29c10 69 6f 6e 28 70 4c 65 66 74 29 3c 70 6c 72 50 6f  ion(pLeft)<plrPo
29c11 73 69 74 69 6f 6e 28 70 52 69 67 68 74 29 20 29  sition(pRight) )
29c12 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66   return -1;.  if
29c13 28 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70 4c  ( plrPosition(pL
29c14 65 66 74 29 3e 70 6c 72 50 6f 73 69 74 69 6f 6e  eft)>plrPosition
29c15 28 70 52 69 67 68 74 29 20 29 20 72 65 74 75 72  (pRight) ) retur
29c16 6e 20 31 3b 0a 20 20 69 66 28 20 70 4c 65 66 74  n 1;.  if( pLeft
29c17 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49  ->iType==DL_POSI
29c18 54 49 4f 4e 53 20 29 20 72 65 74 75 72 6e 20 30  TIONS ) return 0
29c19 3b 0a 0a 20 20 69 66 28 20 70 6c 72 53 74 61 72  ;..  if( plrStar
29c1a 74 4f 66 66 73 65 74 28 70 4c 65 66 74 29 3c 70  tOffset(pLeft)<p
29c1b 6c 72 53 74 61 72 74 4f 66 66 73 65 74 28 70 52  lrStartOffset(pR
29c1c 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 2d  ight) ) return -
29c1d 31 3b 0a 20 20 69 66 28 20 70 6c 72 53 74 61 72  1;.  if( plrStar
29c1e 74 4f 66 66 73 65 74 28 70 4c 65 66 74 29 3e 70  tOffset(pLeft)>p
29c1f 6c 72 53 74 61 72 74 4f 66 66 73 65 74 28 70 52  lrStartOffset(pR
29c20 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 31  ight) ) return 1
29c21 3b 0a 0a 20 20 69 66 28 20 70 6c 72 45 6e 64 4f  ;..  if( plrEndO
29c22 66 66 73 65 74 28 70 4c 65 66 74 29 3c 70 6c 72  ffset(pLeft)<plr
29c23 45 6e 64 4f 66 66 73 65 74 28 70 52 69 67 68 74  EndOffset(pRight
29c24 29 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20  ) ) return -1;. 
29c25 20 69 66 28 20 70 6c 72 45 6e 64 4f 66 66 73 65   if( plrEndOffse
29c26 74 28 70 4c 65 66 74 29 3e 70 6c 72 45 6e 64 4f  t(pLeft)>plrEndO
29c27 66 66 73 65 74 28 70 52 69 67 68 74 29 20 29 20  ffset(pRight) ) 
29c28 72 65 74 75 72 6e 20 31 3b 0a 0a 20 20 72 65 74  return 1;..  ret
29c29 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 57 72 69  urn 0;.}../* Wri
29c2a 74 65 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20  te the union of 
29c2b 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 73 20 69  position lists i
29c2c 6e 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67  n pLeft and pRig
29c2d 68 74 20 74 6f 20 70 4f 75 74 2e 0a 2a 2a 20 22  ht to pOut..** "
29c2e 55 6e 69 6f 6e 22 20 69 6e 20 74 68 69 73 20 63  Union" in this c
29c2f 61 73 65 20 6d 65 61 6e 69 6e 67 20 22 41 6c 6c  ase meaning "All
29c30 20 75 6e 69 71 75 65 20 70 6f 73 69 74 69 6f 6e   unique position
29c31 20 74 75 70 6c 65 73 22 2e 20 20 53 68 6f 75 6c   tuples".  Shoul
29c32 64 0a 2a 2a 20 77 6f 72 6b 20 77 69 74 68 20 61  d.** work with a
29c33 6e 79 20 64 6f 63 6c 69 73 74 20 74 79 70 65 2c  ny doclist type,
29c34 20 74 68 6f 75 67 68 20 62 6f 74 68 20 69 6e 70   though both inp
29c35 75 74 73 20 61 6e 64 20 74 68 65 20 6f 75 74 70  uts and the outp
29c36 75 74 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20  ut.** should be 
29c37 74 68 65 20 73 61 6d 65 20 74 79 70 65 2e 0a 2a  the same type..*
29c38 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 6f  /.static void po
29c39 73 4c 69 73 74 55 6e 69 6f 6e 28 44 4c 52 65 61  sListUnion(DLRea
29c3a 64 65 72 20 2a 70 4c 65 66 74 2c 20 44 4c 52 65  der *pLeft, DLRe
29c3b 61 64 65 72 20 2a 70 52 69 67 68 74 2c 20 44 4c  ader *pRight, DL
29c3c 57 72 69 74 65 72 20 2a 70 4f 75 74 29 7b 0a 20  Writer *pOut){. 
29c3d 20 50 4c 52 65 61 64 65 72 20 6c 65 66 74 2c 20   PLReader left, 
29c3e 72 69 67 68 74 3b 0a 20 20 50 4c 57 72 69 74 65  right;.  PLWrite
29c3f 72 20 77 72 69 74 65 72 3b 0a 0a 20 20 61 73 73  r writer;..  ass
29c40 65 72 74 28 20 64 6c 72 44 6f 63 69 64 28 70 4c  ert( dlrDocid(pL
29c41 65 66 74 29 3d 3d 64 6c 72 44 6f 63 69 64 28 70  eft)==dlrDocid(p
29c42 52 69 67 68 74 29 20 29 3b 0a 20 20 61 73 73 65  Right) );.  asse
29c43 72 74 28 20 70 4c 65 66 74 2d 3e 69 54 79 70 65  rt( pLeft->iType
29c44 3d 3d 70 52 69 67 68 74 2d 3e 69 54 79 70 65 20  ==pRight->iType 
29c45 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65  );.  assert( pLe
29c46 66 74 2d 3e 69 54 79 70 65 3d 3d 70 4f 75 74 2d  ft->iType==pOut-
29c47 3e 69 54 79 70 65 20 29 3b 0a 0a 20 20 70 6c 72  >iType );..  plr
29c48 49 6e 69 74 28 26 6c 65 66 74 2c 20 70 4c 65 66  Init(&left, pLef
29c49 74 29 3b 0a 20 20 70 6c 72 49 6e 69 74 28 26 72  t);.  plrInit(&r
29c4a 69 67 68 74 2c 20 70 52 69 67 68 74 29 3b 0a 20  ight, pRight);. 
29c4b 20 70 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72   plwInit(&writer
29c4c 2c 20 70 4f 75 74 2c 20 64 6c 72 44 6f 63 69 64  , pOut, dlrDocid
29c4d 28 70 4c 65 66 74 29 29 3b 0a 0a 20 20 77 68 69  (pLeft));..  whi
29c4e 6c 65 28 20 21 70 6c 72 41 74 45 6e 64 28 26 6c  le( !plrAtEnd(&l
29c4f 65 66 74 29 20 7c 7c 20 21 70 6c 72 41 74 45 6e  eft) || !plrAtEn
29c50 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20  d(&right) ){.   
29c51 20 69 6e 74 20 63 20 3d 20 70 6f 73 4c 69 73 74   int c = posList
29c52 43 6d 70 28 26 6c 65 66 74 2c 20 26 72 69 67 68  Cmp(&left, &righ
29c53 74 29 3b 0a 20 20 20 20 69 66 28 20 63 3c 30 20  t);.    if( c<0 
29c54 29 7b 0a 20 20 20 20 20 20 70 6c 77 43 6f 70 79  ){.      plwCopy
29c55 28 26 77 72 69 74 65 72 2c 20 26 6c 65 66 74 29  (&writer, &left)
29c56 3b 0a 20 20 20 20 20 20 70 6c 72 53 74 65 70 28  ;.      plrStep(
29c57 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73  &left);.    }els
29c58 65 20 69 66 28 20 63 3e 30 20 29 7b 0a 20 20 20  e if( c>0 ){.   
29c59 20 20 20 70 6c 77 43 6f 70 79 28 26 77 72 69 74     plwCopy(&writ
29c5a 65 72 2c 20 26 72 69 67 68 74 29 3b 0a 20 20 20  er, &right);.   
29c5b 20 20 20 70 6c 72 53 74 65 70 28 26 72 69 67 68     plrStep(&righ
29c5c 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  t);.    }else{. 
29c5d 20 20 20 20 20 70 6c 77 43 6f 70 79 28 26 77 72       plwCopy(&wr
29c5e 69 74 65 72 2c 20 26 6c 65 66 74 29 3b 0a 20 20  iter, &left);.  
29c5f 20 20 20 20 70 6c 72 53 74 65 70 28 26 6c 65 66      plrStep(&lef
29c60 74 29 3b 0a 20 20 20 20 20 20 70 6c 72 53 74 65  t);.      plrSte
29c61 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d  p(&right);.    }
29c62 0a 20 20 7d 0a 0a 20 20 70 6c 77 54 65 72 6d 69  .  }..  plwTermi
29c63 6e 61 74 65 28 26 77 72 69 74 65 72 29 3b 0a 20  nate(&writer);. 
29c64 20 70 6c 77 44 65 73 74 72 6f 79 28 26 77 72 69   plwDestroy(&wri
29c65 74 65 72 29 3b 0a 20 20 70 6c 72 44 65 73 74 72  ter);.  plrDestr
29c66 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20 70 6c 72  oy(&left);.  plr
29c67 44 65 73 74 72 6f 79 28 26 72 69 67 68 74 29 3b  Destroy(&right);
29c68 0a 7d 0a 0a 2f 2a 20 57 72 69 74 65 20 74 68 65  .}../* Write the
29c69 20 75 6e 69 6f 6e 20 6f 66 20 64 6f 63 6c 69 73   union of doclis
29c6a 74 73 20 69 6e 20 70 4c 65 66 74 20 61 6e 64 20  ts in pLeft and 
29c6b 70 52 69 67 68 74 20 74 6f 20 70 4f 75 74 2e 20  pRight to pOut. 
29c6c 20 46 6f 72 0a 2a 2a 20 64 6f 63 69 64 73 20 69   For.** docids i
29c6d 6e 20 63 6f 6d 6d 6f 6e 20 62 65 74 77 65 65 6e  n common between
29c6e 20 74 68 65 20 69 6e 70 75 74 73 2c 20 74 68 65   the inputs, the
29c6f 20 75 6e 69 6f 6e 20 6f 66 20 74 68 65 20 70 6f   union of the po
29c70 73 69 74 69 6f 6e 0a 2a 2a 20 6c 69 73 74 73 20  sition.** lists 
29c71 69 73 20 77 72 69 74 74 65 6e 2e 20 20 49 6e 70  is written.  Inp
29c72 75 74 73 20 61 6e 64 20 6f 75 74 70 75 74 73 20  uts and outputs 
29c73 61 72 65 20 61 6c 77 61 79 73 20 74 79 70 65 20  are always type 
29c74 44 4c 5f 44 45 46 41 55 4c 54 2e 0a 2a 2f 0a 73  DL_DEFAULT..*/.s
29c75 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63 4c 69  tatic void docLi
29c76 73 74 55 6e 69 6f 6e 28 0a 20 20 63 6f 6e 73 74  stUnion(.  const
29c77 20 63 68 61 72 20 2a 70 4c 65 66 74 2c 20 69 6e   char *pLeft, in
29c78 74 20 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e 73 74  t nLeft,.  const
29c79 20 63 68 61 72 20 2a 70 52 69 67 68 74 2c 20 69   char *pRight, i
29c7a 6e 74 20 6e 52 69 67 68 74 2c 0a 20 20 44 61 74  nt nRight,.  Dat
29c7b 61 42 75 66 66 65 72 20 2a 70 4f 75 74 20 20 20  aBuffer *pOut   
29c7c 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
29c7d 63 6f 6d 62 69 6e 65 64 20 64 6f 63 6c 69 73 74  combined doclist
29c7e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 44 4c   here */.){.  DL
29c7f 52 65 61 64 65 72 20 6c 65 66 74 2c 20 72 69 67  Reader left, rig
29c80 68 74 3b 0a 20 20 44 4c 57 72 69 74 65 72 20 77  ht;.  DLWriter w
29c81 72 69 74 65 72 3b 0a 0a 20 20 69 66 28 20 6e 4c  riter;..  if( nL
29c82 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  eft==0 ){.    if
29c83 28 20 6e 52 69 67 68 74 21 3d 30 29 20 64 61 74  ( nRight!=0) dat
29c84 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 4f  aBufferAppend(pO
29c85 75 74 2c 20 70 52 69 67 68 74 2c 20 6e 52 69 67  ut, pRight, nRig
29c86 68 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ht);.    return;
29c87 0a 20 20 7d 0a 20 20 69 66 28 20 6e 52 69 67 68  .  }.  if( nRigh
29c88 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64 61 74 61  t==0 ){.    data
29c89 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 4f 75  BufferAppend(pOu
29c8a 74 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29  t, pLeft, nLeft)
29c8b 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
29c8c 7d 0a 0a 20 20 64 6c 72 49 6e 69 74 28 26 6c 65  }..  dlrInit(&le
29c8d 66 74 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20  ft, DL_DEFAULT, 
29c8e 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20  pLeft, nLeft);. 
29c8f 20 64 6c 72 49 6e 69 74 28 26 72 69 67 68 74 2c   dlrInit(&right,
29c90 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70 52 69   DL_DEFAULT, pRi
29c91 67 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20  ght, nRight);.  
29c92 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72 2c  dlwInit(&writer,
29c93 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70 4f 75   DL_DEFAULT, pOu
29c94 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 21 64  t);..  while( !d
29c95 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29 20 7c  lrAtEnd(&left) |
29c96 7c 20 21 64 6c 72 41 74 45 6e 64 28 26 72 69 67  | !dlrAtEnd(&rig
29c97 68 74 29 20 29 7b 0a 20 20 20 20 69 66 28 20 64  ht) ){.    if( d
29c98 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29 20  lrAtEnd(&right) 
29c99 29 7b 0a 20 20 20 20 20 20 64 6c 77 43 6f 70 79  ){.      dlwCopy
29c9a 28 26 77 72 69 74 65 72 2c 20 26 6c 65 66 74 29  (&writer, &left)
29c9b 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28  ;.      dlrStep(
29c9c 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73  &left);.    }els
29c9d 65 20 69 66 28 20 64 6c 72 41 74 45 6e 64 28 26  e if( dlrAtEnd(&
29c9e 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20 64  left) ){.      d
29c9f 6c 77 43 6f 70 79 28 26 77 72 69 74 65 72 2c 20  lwCopy(&writer, 
29ca0 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20 20 64  &right);.      d
29ca1 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a  lrStep(&right);.
29ca2 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 64 6c      }else if( dl
29ca3 72 44 6f 63 69 64 28 26 6c 65 66 74 29 3c 64 6c  rDocid(&left)<dl
29ca4 72 44 6f 63 69 64 28 26 72 69 67 68 74 29 20 29  rDocid(&right) )
29ca5 7b 0a 20 20 20 20 20 20 64 6c 77 43 6f 70 79 28  {.      dlwCopy(
29ca6 26 77 72 69 74 65 72 2c 20 26 6c 65 66 74 29 3b  &writer, &left);
29ca7 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26  .      dlrStep(&
29ca8 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  left);.    }else
29ca9 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28 26 6c   if( dlrDocid(&l
29caa 65 66 74 29 3e 64 6c 72 44 6f 63 69 64 28 26 72  eft)>dlrDocid(&r
29cab 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 64  ight) ){.      d
29cac 6c 77 43 6f 70 79 28 26 77 72 69 74 65 72 2c 20  lwCopy(&writer, 
29cad 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20 20 64  &right);.      d
29cae 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a  lrStep(&right);.
29caf 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
29cb0 20 70 6f 73 4c 69 73 74 55 6e 69 6f 6e 28 26 6c   posListUnion(&l
29cb1 65 66 74 2c 20 26 72 69 67 68 74 2c 20 26 77 72  eft, &right, &wr
29cb2 69 74 65 72 29 3b 0a 20 20 20 20 20 20 64 6c 72  iter);.      dlr
29cb3 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20  Step(&left);.   
29cb4 20 20 20 64 6c 72 53 74 65 70 28 26 72 69 67 68     dlrStep(&righ
29cb5 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  t);.    }.  }.. 
29cb6 20 64 6c 72 44 65 73 74 72 6f 79 28 26 6c 65 66   dlrDestroy(&lef
29cb7 74 29 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f 79  t);.  dlrDestroy
29cb8 28 26 72 69 67 68 74 29 3b 0a 20 20 64 6c 77 44  (&right);.  dlwD
29cb9 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b  estroy(&writer);
29cba 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20  .}../* .** This 
29cbb 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
29cbc 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20   as part of the 
29cbd 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
29cbe 66 20 70 68 72 61 73 65 20 61 6e 64 0a 2a 2a 20  f phrase and.** 
29cbf 4e 45 41 52 20 6d 61 74 63 68 69 6e 67 2e 0a 2a  NEAR matching..*
29cc0 2a 0a 2a 2a 20 70 4c 65 66 74 20 61 6e 64 20 70  *.** pLeft and p
29cc1 52 69 67 68 74 20 61 72 65 20 44 4c 52 65 61 64  Right are DLRead
29cc2 65 72 73 20 70 6f 73 69 74 69 6f 6e 65 64 20 74  ers positioned t
29cc3 6f 20 74 68 65 20 73 61 6d 65 20 64 6f 63 69 64  o the same docid
29cc4 20 69 6e 0a 2a 2a 20 6c 69 73 74 73 20 6f 66 20   in.** lists of 
29cc5 74 79 70 65 20 44 4c 5f 50 4f 53 49 54 49 4f 4e  type DL_POSITION
29cc6 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
29cc7 77 72 69 74 65 73 20 61 6e 20 65 6e 74 72 79 20  writes an entry 
29cc8 74 6f 20 74 68 65 0a 2a 2a 20 44 4c 57 72 69 74  to the.** DLWrit
29cc9 65 72 20 70 4f 75 74 20 66 6f 72 20 65 61 63 68  er pOut for each
29cca 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 70 52 69   position in pRi
29ccb 67 68 74 20 74 68 61 74 20 69 73 20 6c 65 73 73  ght that is less
29ccc 20 74 68 61 6e 0a 2a 2a 20 28 6e 4e 65 61 72 2b   than.** (nNear+
29ccd 31 29 20 67 72 65 61 74 65 72 20 28 62 75 74 20  1) greater (but 
29cce 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 6f 72 20  not equal to or 
29ccf 73 6d 61 6c 6c 65 72 29 20 74 68 61 6e 20 61 20  smaller) than a 
29cd0 70 6f 73 69 74 69 6f 6e 20 0a 2a 2a 20 69 6e 20  position .** in 
29cd1 70 4c 65 66 74 2e 20 46 6f 72 20 65 78 61 6d 70  pLeft. For examp
29cd2 6c 65 2c 20 69 66 20 6e 4e 65 61 72 20 69 73 20  le, if nNear is 
29cd3 30 2c 20 61 6e 64 20 74 68 65 20 70 6f 73 69 74  0, and the posit
29cd4 69 6f 6e 73 20 63 6f 6e 74 61 69 6e 65 64 0a 2a  ions contained.*
29cd5 2a 20 62 79 20 70 4c 65 66 74 20 61 6e 64 20 70  * by pLeft and p
29cd6 52 69 67 68 74 20 61 72 65 3a 0a 2a 2a 0a 2a 2a  Right are:.**.**
29cd7 20 20 20 20 70 4c 65 66 74 3a 20 20 35 20 31 30      pLeft:  5 10
29cd8 20 31 35 20 32 30 0a 2a 2a 20 20 20 20 70 52 69   15 20.**    pRi
29cd9 67 68 74 3a 20 36 20 20 39 20 31 37 20 32 31 0a  ght: 6  9 17 21.
29cda 2a 2a 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 64  **.** then the d
29cdb 6f 63 69 64 20 69 73 20 61 64 64 65 64 20 74 6f  ocid is added to
29cdc 20 70 4f 75 74 2e 20 49 66 20 70 4f 75 74 20 69   pOut. If pOut i
29cdd 73 20 6f 66 20 74 79 70 65 20 44 4c 5f 50 4f 53  s of type DL_POS
29cde 49 54 49 4f 4e 53 2c 0a 2a 2a 20 74 68 65 6e 20  ITIONS,.** then 
29cdf 61 20 70 6f 73 69 74 69 6f 6e 69 64 73 20 22 36  a positionids "6
29ce0 22 20 61 6e 64 20 22 32 31 22 20 61 72 65 20 61  " and "21" are a
29ce1 6c 73 6f 20 61 64 64 65 64 20 74 6f 20 70 4f 75  lso added to pOu
29ce2 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f 6f 6c  t..**.** If bool
29ce3 65 61 6e 20 61 72 67 75 6d 65 6e 74 20 69 73 53  ean argument isS
29ce4 61 76 65 4c 65 66 74 20 69 73 20 74 72 75 65 2c  aveLeft is true,
29ce5 20 74 68 65 6e 20 70 6f 73 69 74 69 6f 6e 69 64   then positionid
29ce6 73 20 61 72 65 20 63 6f 70 69 65 64 0a 2a 2a 20  s are copied.** 
29ce7 66 72 6f 6d 20 70 4c 65 66 74 20 69 6e 73 74 65  from pLeft inste
29ce8 61 64 20 6f 66 20 70 52 69 67 68 74 2e 20 49 6e  ad of pRight. In
29ce9 20 74 68 65 20 65 78 61 6d 70 6c 65 20 61 62 6f   the example abo
29cea 76 65 2c 20 74 68 65 20 70 6f 73 69 74 69 6f 6e  ve, the position
29ceb 73 20 22 35 22 0a 2a 2a 20 61 6e 64 20 22 32 30  s "5".** and "20
29cec 22 20 77 6f 75 6c 64 20 62 65 20 61 64 64 65 64  " would be added
29ced 20 69 6e 73 74 65 61 64 20 6f 66 20 22 36 22 20   instead of "6" 
29cee 61 6e 64 20 22 32 31 22 2e 0a 2a 2f 0a 73 74 61  and "21"..*/.sta
29cef 74 69 63 20 76 6f 69 64 20 70 6f 73 4c 69 73 74  tic void posList
29cf0 50 68 72 61 73 65 4d 65 72 67 65 28 0a 20 20 44  PhraseMerge(.  D
29cf1 4c 52 65 61 64 65 72 20 2a 70 4c 65 66 74 2c 20  LReader *pLeft, 
29cf2 0a 20 20 44 4c 52 65 61 64 65 72 20 2a 70 52 69  .  DLReader *pRi
29cf3 67 68 74 2c 0a 20 20 69 6e 74 20 6e 4e 65 61 72  ght,.  int nNear
29cf4 2c 0a 20 20 69 6e 74 20 69 73 53 61 76 65 4c 65  ,.  int isSaveLe
29cf5 66 74 2c 0a 20 20 44 4c 57 72 69 74 65 72 20 2a  ft,.  DLWriter *
29cf6 70 4f 75 74 0a 29 7b 0a 20 20 50 4c 52 65 61 64  pOut.){.  PLRead
29cf7 65 72 20 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a  er left, right;.
29cf8 20 20 50 4c 57 72 69 74 65 72 20 77 72 69 74 65    PLWriter write
29cf9 72 3b 0a 20 20 69 6e 74 20 6d 61 74 63 68 20 3d  r;.  int match =
29cfa 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64   0;..  assert( d
29cfb 6c 72 44 6f 63 69 64 28 70 4c 65 66 74 29 3d 3d  lrDocid(pLeft)==
29cfc 64 6c 72 44 6f 63 69 64 28 70 52 69 67 68 74 29  dlrDocid(pRight)
29cfd 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
29cfe 75 74 2d 3e 69 54 79 70 65 21 3d 44 4c 5f 50 4f  ut->iType!=DL_PO
29cff 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20  SITIONS_OFFSETS 
29d00 29 3b 0a 0a 20 20 70 6c 72 49 6e 69 74 28 26 6c  );..  plrInit(&l
29d01 65 66 74 2c 20 70 4c 65 66 74 29 3b 0a 20 20 70  eft, pLeft);.  p
29d02 6c 72 49 6e 69 74 28 26 72 69 67 68 74 2c 20 70  lrInit(&right, p
29d03 52 69 67 68 74 29 3b 0a 0a 20 20 77 68 69 6c 65  Right);..  while
29d04 28 20 21 70 6c 72 41 74 45 6e 64 28 26 6c 65 66  ( !plrAtEnd(&lef
29d05 74 29 20 26 26 20 21 70 6c 72 41 74 45 6e 64 28  t) && !plrAtEnd(
29d06 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 69  &right) ){.    i
29d07 66 28 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 6c 65  f( plrColumn(&le
29d08 66 74 29 3c 70 6c 72 43 6f 6c 75 6d 6e 28 26 72  ft)<plrColumn(&r
29d09 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 70  ight) ){.      p
29d0a 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20  lrStep(&left);. 
29d0b 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 6c 72     }else if( plr
29d0c 43 6f 6c 75 6d 6e 28 26 6c 65 66 74 29 3e 70 6c  Column(&left)>pl
29d0d 72 43 6f 6c 75 6d 6e 28 26 72 69 67 68 74 29 20  rColumn(&right) 
29d0e 29 7b 0a 20 20 20 20 20 20 70 6c 72 53 74 65 70  ){.      plrStep
29d0f 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 65  (&right);.    }e
29d10 6c 73 65 20 69 66 28 20 70 6c 72 50 6f 73 69 74  lse if( plrPosit
29d11 69 6f 6e 28 26 6c 65 66 74 29 3e 3d 70 6c 72 50  ion(&left)>=plrP
29d12 6f 73 69 74 69 6f 6e 28 26 72 69 67 68 74 29 20  osition(&right) 
29d13 29 7b 0a 20 20 20 20 20 20 70 6c 72 53 74 65 70  ){.      plrStep
29d14 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 65  (&right);.    }e
29d15 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 28  lse{.      if( (
29d16 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26 72 69 67  plrPosition(&rig
29d17 68 74 29 2d 70 6c 72 50 6f 73 69 74 69 6f 6e 28  ht)-plrPosition(
29d18 26 6c 65 66 74 29 29 3c 3d 28 6e 4e 65 61 72 2b  &left))<=(nNear+
29d19 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  1) ){.        if
29d1a 28 20 21 6d 61 74 63 68 20 29 7b 0a 20 20 20 20  ( !match ){.    
29d1b 20 20 20 20 20 20 70 6c 77 49 6e 69 74 28 26 77        plwInit(&w
29d1c 72 69 74 65 72 2c 20 70 4f 75 74 2c 20 64 6c 72  riter, pOut, dlr
29d1d 44 6f 63 69 64 28 70 4c 65 66 74 29 29 3b 0a 20  Docid(pLeft));. 
29d1e 20 20 20 20 20 20 20 20 20 6d 61 74 63 68 20 3d           match =
29d1f 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   1;.        }.  
29d20 20 20 20 20 20 20 69 66 28 20 21 69 73 53 61 76        if( !isSav
29d21 65 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20  eLeft ){.       
29d22 20 20 20 70 6c 77 41 64 64 28 26 77 72 69 74 65     plwAdd(&write
29d23 72 2c 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 72 69  r, plrColumn(&ri
29d24 67 68 74 29 2c 20 70 6c 72 50 6f 73 69 74 69 6f  ght), plrPositio
29d25 6e 28 26 72 69 67 68 74 29 2c 20 30 2c 20 30 29  n(&right), 0, 0)
29d26 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
29d27 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77 41 64  .          plwAd
29d28 64 28 26 77 72 69 74 65 72 2c 20 70 6c 72 43 6f  d(&writer, plrCo
29d29 6c 75 6d 6e 28 26 6c 65 66 74 29 2c 20 70 6c 72  lumn(&left), plr
29d2a 50 6f 73 69 74 69 6f 6e 28 26 6c 65 66 74 29 2c  Position(&left),
29d2b 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   0, 0);.        
29d2c 7d 0a 20 20 20 20 20 20 20 20 70 6c 72 53 74 65  }.        plrSte
29d2d 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20  p(&right);.     
29d2e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
29d2f 70 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a  plrStep(&left);.
29d30 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
29d31 7d 0a 0a 20 20 69 66 28 20 6d 61 74 63 68 20 29  }..  if( match )
29d32 7b 0a 20 20 20 20 70 6c 77 54 65 72 6d 69 6e 61  {.    plwTermina
29d33 74 65 28 26 77 72 69 74 65 72 29 3b 0a 20 20 20  te(&writer);.   
29d34 20 70 6c 77 44 65 73 74 72 6f 79 28 26 77 72 69   plwDestroy(&wri
29d35 74 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 70 6c 72  ter);.  }..  plr
29d36 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a  Destroy(&left);.
29d37 20 20 70 6c 72 44 65 73 74 72 6f 79 28 26 72 69    plrDestroy(&ri
29d38 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ght);.}../*.** C
29d39 6f 6d 70 61 72 65 20 74 68 65 20 76 61 6c 75 65  ompare the value
29d3a 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  s pointed to by 
29d3b 74 68 65 20 50 4c 52 65 61 64 65 72 73 20 70 61  the PLReaders pa
29d3c 73 73 65 64 20 61 73 20 61 72 67 75 6d 65 6e 74  ssed as argument
29d3d 73 2e 20 0a 2a 2a 20 52 65 74 75 72 6e 20 2d 31  s. .** Return -1
29d3e 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 70 6f   if the value po
29d3f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 4c 65 66  inted to by pLef
29d40 74 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  t is considered 
29d41 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 74 68 65  less than.** the
29d42 20 76 61 6c 75 65 20 70 6f 69 6e 74 65 64 20 74   value pointed t
29d43 6f 20 62 79 20 70 52 69 67 68 74 2c 20 2b 31 20  o by pRight, +1 
29d44 69 66 20 69 74 20 69 73 20 63 6f 6e 73 69 64 65  if it is conside
29d45 72 65 64 20 67 72 65 61 74 65 72 0a 2a 2a 20 74  red greater.** t
29d46 68 61 6e 20 69 74 2c 20 6f 72 20 30 20 69 66 20  han it, or 0 if 
29d47 69 74 20 69 73 20 65 71 75 61 6c 2e 20 69 2e 65  it is equal. i.e
29d48 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 2a 70 4c  ..**.**     (*pL
29d49 65 66 74 20 2d 20 2a 70 52 69 67 68 74 29 0a 2a  eft - *pRight).*
29d4a 2a 0a 2a 2a 20 41 20 50 4c 52 65 61 64 65 72 20  *.** A PLReader 
29d4b 74 68 61 74 20 69 73 20 69 6e 20 74 68 65 20 45  that is in the E
29d4c 4f 46 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 20  OF condition is 
29d4d 63 6f 6e 73 69 64 65 72 65 64 20 67 72 65 61 74  considered great
29d4e 65 72 20 74 68 61 6e 0a 2a 2a 20 61 6e 79 20 6f  er than.** any o
29d4f 74 68 65 72 2e 20 49 66 20 6e 65 69 74 68 65 72  ther. If neither
29d50 20 61 72 67 75 6d 65 6e 74 20 69 73 20 69 6e 20   argument is in 
29d51 45 4f 46 20 73 74 61 74 65 2c 20 74 68 65 20 72  EOF state, the r
29d52 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 0a 2a  eturn value of.*
29d53 2a 20 70 6c 72 43 6f 6c 75 6d 6e 28 29 20 69 73  * plrColumn() is
29d54 20 75 73 65 64 2e 20 49 66 20 74 68 65 20 70 6c   used. If the pl
29d55 72 43 6f 6c 75 6d 6e 28 29 20 76 61 6c 75 65 73  rColumn() values
29d56 20 61 72 65 20 65 71 75 61 6c 2c 20 74 68 65 0a   are equal, the.
29d57 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73  ** comparison is
29d58 20 6f 6e 20 74 68 65 20 62 61 73 69 73 20 6f 66   on the basis of
29d59 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 29 2e 0a   plrPosition()..
29d5a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6c  */.static int pl
29d5b 72 43 6f 6d 70 61 72 65 28 50 4c 52 65 61 64 65  rCompare(PLReade
29d5c 72 20 2a 70 4c 65 66 74 2c 20 50 4c 52 65 61 64  r *pLeft, PLRead
29d5d 65 72 20 2a 70 52 69 67 68 74 29 7b 0a 20 20 61  er *pRight){.  a
29d5e 73 73 65 72 74 28 21 70 6c 72 41 74 45 6e 64 28  ssert(!plrAtEnd(
29d5f 70 4c 65 66 74 29 20 7c 7c 20 21 70 6c 72 41 74  pLeft) || !plrAt
29d60 45 6e 64 28 70 52 69 67 68 74 29 29 3b 0a 0a 20  End(pRight));.. 
29d61 20 69 66 28 20 70 6c 72 41 74 45 6e 64 28 70 52   if( plrAtEnd(pR
29d62 69 67 68 74 29 20 7c 7c 20 70 6c 72 41 74 45 6e  ight) || plrAtEn
29d63 64 28 70 4c 65 66 74 29 20 29 7b 0a 20 20 20 20  d(pLeft) ){.    
29d64 72 65 74 75 72 6e 20 28 70 6c 72 41 74 45 6e 64  return (plrAtEnd
29d65 28 70 52 69 67 68 74 29 20 3f 20 2d 31 20 3a 20  (pRight) ? -1 : 
29d66 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 6c  1);.  }.  if( pl
29d67 72 43 6f 6c 75 6d 6e 28 70 4c 65 66 74 29 21 3d  rColumn(pLeft)!=
29d68 70 6c 72 43 6f 6c 75 6d 6e 28 70 52 69 67 68 74  plrColumn(pRight
29d69 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
29d6a 28 28 70 6c 72 43 6f 6c 75 6d 6e 28 70 4c 65 66  ((plrColumn(pLef
29d6b 74 29 3c 70 6c 72 43 6f 6c 75 6d 6e 28 70 52 69  t)<plrColumn(pRi
29d6c 67 68 74 29 29 20 3f 20 2d 31 20 3a 20 31 29 3b  ght)) ? -1 : 1);
29d6d 0a 20 20 7d 0a 20 20 69 66 28 20 70 6c 72 50 6f  .  }.  if( plrPo
29d6e 73 69 74 69 6f 6e 28 70 4c 65 66 74 29 21 3d 70  sition(pLeft)!=p
29d6f 6c 72 50 6f 73 69 74 69 6f 6e 28 70 52 69 67 68  lrPosition(pRigh
29d70 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  t) ){.    return
29d71 20 28 28 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70   ((plrPosition(p
29d72 4c 65 66 74 29 3c 70 6c 72 50 6f 73 69 74 69 6f  Left)<plrPositio
29d73 6e 28 70 52 69 67 68 74 29 29 20 3f 20 2d 31 20  n(pRight)) ? -1 
29d74 3a 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  : 1);.  }.  retu
29d75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 57 65 20 68  rn 0;.}../* We h
29d76 61 76 65 20 74 77 6f 20 64 6f 63 6c 69 73 74 73  ave two doclists
29d77 20 77 69 74 68 20 70 6f 73 69 74 69 6f 6e 73 3a   with positions:
29d78 20 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67    pLeft and pRig
29d79 68 74 2e 20 44 65 70 65 6e 64 69 6e 67 0a 2a 2a  ht. Depending.**
29d7a 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66   on the value of
29d7b 20 74 68 65 20 6e 4e 65 61 72 20 70 61 72 61 6d   the nNear param
29d7c 65 74 65 72 2c 20 70 65 72 66 6f 72 6d 20 65 69  eter, perform ei
29d7d 74 68 65 72 20 61 20 70 68 72 61 73 65 0a 2a 2a  ther a phrase.**
29d7e 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 28 69   intersection (i
29d7f 66 20 6e 4e 65 61 72 3d 3d 30 29 20 6f 72 20 61  f nNear==0) or a
29d80 20 4e 45 41 52 20 69 6e 74 65 72 73 65 63 74 69   NEAR intersecti
29d81 6f 6e 20 28 69 66 20 6e 4e 65 61 72 3e 30 29 0a  on (if nNear>0).
29d82 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 74 68 65  ** and write the
29d83 20 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 70 4f   results into pO
29d84 75 74 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 68 72 61  ut..**.** A phra
29d85 73 65 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20  se intersection 
29d86 6d 65 61 6e 73 20 74 68 61 74 20 74 77 6f 20 64  means that two d
29d87 6f 63 75 6d 65 6e 74 73 20 6f 6e 6c 79 20 6d 61  ocuments only ma
29d88 74 63 68 0a 2a 2a 20 69 66 20 70 4c 65 66 74 2e  tch.** if pLeft.
29d89 69 50 6f 73 2b 31 3d 3d 70 52 69 67 68 74 2e 69  iPos+1==pRight.i
29d8a 50 6f 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 45 41  Pos..**.** A NEA
29d8b 52 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 6d  R intersection m
29d8c 65 61 6e 73 20 74 68 61 74 20 74 77 6f 20 64 6f  eans that two do
29d8d 63 75 6d 65 6e 74 73 20 6f 6e 6c 79 20 6d 61 74  cuments only mat
29d8e 63 68 20 69 66 20 0a 2a 2a 20 28 61 62 73 28 70  ch if .** (abs(p
29d8f 4c 65 66 74 2e 69 50 6f 73 2d 70 52 69 67 68 74  Left.iPos-pRight
29d90 2e 69 50 6f 73 29 3c 6e 4e 65 61 72 29 2e 0a 2a  .iPos)<nNear)..*
29d91 2a 0a 2a 2a 20 49 66 20 61 20 4e 45 41 52 20 69  *.** If a NEAR i
29d92 6e 74 65 72 73 65 63 74 69 6f 6e 20 69 73 20 72  ntersection is r
29d93 65 71 75 65 73 74 65 64 2c 20 74 68 65 6e 20 74  equested, then t
29d94 68 65 20 6e 50 68 72 61 73 65 20 61 72 67 75 6d  he nPhrase argum
29d95 65 6e 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65  ent should.** be
29d96 20 70 61 73 73 65 64 20 74 68 65 20 6e 75 6d 62   passed the numb
29d97 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20  er of tokens in 
29d98 74 68 65 20 74 77 6f 20 6f 70 65 72 61 6e 64 73  the two operands
29d99 20 74 6f 20 74 68 65 20 4e 45 41 52 20 6f 70 65   to the NEAR ope
29d9a 72 61 74 6f 72 0a 2a 2a 20 63 6f 6d 62 69 6e 65  rator.** combine
29d9b 64 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a  d. For example:.
29d9c 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 51 75 65 72  **.**       Quer
29d9d 79 20 73 79 6e 74 61 78 20 20 20 20 20 20 20 20  y syntax        
29d9e 20 20 20 20 20 20 20 6e 50 68 72 61 73 65 0a 2a         nPhrase.*
29d9f 2a 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  *      ---------
29da0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
29da1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20  -----------.**  
29da2 20 20 20 20 20 22 41 20 42 20 43 22 20 4e 45 41       "A B C" NEA
29da3 52 20 22 44 20 45 22 20 20 20 20 20 20 20 20 20  R "D E"         
29da4 35 0a 2a 2a 20 20 20 20 20 20 20 41 20 4e 45 41  5.**       A NEA
29da5 52 20 42 20 20 20 20 20 20 20 20 20 20 20 20 20  R B             
29da6 20 20 20 20 20 20 32 0a 2a 2a 0a 2a 2a 20 69 54        2.**.** iT
29da7 79 70 65 20 63 6f 6e 74 72 6f 6c 73 20 74 68 65  ype controls the
29da8 20 74 79 70 65 20 6f 66 20 64 61 74 61 20 77 72   type of data wr
29da9 69 74 74 65 6e 20 74 6f 20 70 4f 75 74 2e 20 20  itten to pOut.  
29daa 49 66 20 69 54 79 70 65 20 69 73 0a 2a 2a 20 44  If iType is.** D
29dab 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c 20 74 68 65  L_POSITIONS, the
29dac 20 70 6f 73 69 74 69 6f 6e 73 20 61 72 65 20 74   positions are t
29dad 68 6f 73 65 20 66 72 6f 6d 20 70 52 69 67 68 74  hose from pRight
29dae 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
29daf 20 64 6f 63 4c 69 73 74 50 68 72 61 73 65 4d 65   docListPhraseMe
29db0 72 67 65 28 0a 20 20 63 6f 6e 73 74 20 63 68 61  rge(.  const cha
29db1 72 20 2a 70 4c 65 66 74 2c 20 69 6e 74 20 6e 4c  r *pLeft, int nL
29db2 65 66 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  eft,.  const cha
29db3 72 20 2a 70 52 69 67 68 74 2c 20 69 6e 74 20 6e  r *pRight, int n
29db4 52 69 67 68 74 2c 0a 20 20 69 6e 74 20 6e 4e 65  Right,.  int nNe
29db5 61 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  ar,            /
29db6 2a 20 30 20 66 6f 72 20 61 20 70 68 72 61 73 65  * 0 for a phrase
29db7 20 6d 65 72 67 65 2c 20 6e 6f 6e 2d 7a 65 72 6f   merge, non-zero
29db8 20 66 6f 72 20 61 20 4e 45 41 52 20 6d 65 72 67   for a NEAR merg
29db9 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 68 72 61  e */.  int nPhra
29dba 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  se,          /* 
29dbb 4e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73  Number of tokens
29dbc 20 69 6e 20 6c 65 66 74 2b 72 69 67 68 74 20 6f   in left+right o
29dbd 70 65 72 61 6e 64 73 20 74 6f 20 4e 45 41 52 20  perands to NEAR 
29dbe 2a 2f 0a 20 20 44 6f 63 4c 69 73 74 54 79 70 65  */.  DocListType
29dbf 20 69 54 79 70 65 2c 20 20 20 20 2f 2a 20 54 79   iType,    /* Ty
29dc0 70 65 20 6f 66 20 64 6f 63 6c 69 73 74 20 74 6f  pe of doclist to
29dc1 20 77 72 69 74 65 20 74 6f 20 70 4f 75 74 20 2a   write to pOut *
29dc2 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 2a  /.  DataBuffer *
29dc3 70 4f 75 74 20 20 20 20 20 20 2f 2a 20 57 72 69  pOut      /* Wri
29dc4 74 65 20 74 68 65 20 63 6f 6d 62 69 6e 65 64 20  te the combined 
29dc5 64 6f 63 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a  doclist here */.
29dc6 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 6c 65  ){.  DLReader le
29dc7 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 44 4c 57  ft, right;.  DLW
29dc8 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a 0a 20  riter writer;.. 
29dc9 20 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20 7c 7c   if( nLeft==0 ||
29dca 20 6e 52 69 67 68 74 3d 3d 30 20 29 20 72 65 74   nRight==0 ) ret
29dcb 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  urn;..  assert( 
29dcc 69 54 79 70 65 21 3d 44 4c 5f 50 4f 53 49 54 49  iType!=DL_POSITI
29dcd 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29 3b 0a 0a  ONS_OFFSETS );..
29dce 20 20 64 6c 72 49 6e 69 74 28 26 6c 65 66 74 2c    dlrInit(&left,
29dcf 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c 20 70   DL_POSITIONS, p
29dd0 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20  Left, nLeft);.  
29dd1 64 6c 72 49 6e 69 74 28 26 72 69 67 68 74 2c 20  dlrInit(&right, 
29dd2 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c 20 70 52  DL_POSITIONS, pR
29dd3 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20  ight, nRight);. 
29dd4 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72   dlwInit(&writer
29dd5 2c 20 69 54 79 70 65 2c 20 70 4f 75 74 29 3b 0a  , iType, pOut);.
29dd6 0a 20 20 77 68 69 6c 65 28 20 21 64 6c 72 41 74  .  while( !dlrAt
29dd7 45 6e 64 28 26 6c 65 66 74 29 20 26 26 20 21 64  End(&left) && !d
29dd8 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29 20  lrAtEnd(&right) 
29dd9 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72 44 6f  ){.    if( dlrDo
29dda 63 69 64 28 26 6c 65 66 74 29 3c 64 6c 72 44 6f  cid(&left)<dlrDo
29ddb 63 69 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20  cid(&right) ){. 
29ddc 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65       dlrStep(&le
29ddd 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ft);.    }else i
29dde 66 28 20 64 6c 72 44 6f 63 69 64 28 26 72 69 67  f( dlrDocid(&rig
29ddf 68 74 29 3c 64 6c 72 44 6f 63 69 64 28 26 6c 65  ht)<dlrDocid(&le
29de0 66 74 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 72  ft) ){.      dlr
29de1 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20  Step(&right);.  
29de2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
29de3 66 28 20 6e 4e 65 61 72 3d 3d 30 20 29 7b 0a 20  f( nNear==0 ){. 
29de4 20 20 20 20 20 20 20 70 6f 73 4c 69 73 74 50 68         posListPh
29de5 72 61 73 65 4d 65 72 67 65 28 26 6c 65 66 74 2c  raseMerge(&left,
29de6 20 26 72 69 67 68 74 2c 20 30 2c 20 30 2c 20 26   &right, 0, 0, &
29de7 77 72 69 74 65 72 29 3b 0a 20 20 20 20 20 20 7d  writer);.      }
29de8 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a  else{.        /*
29de9 20 54 68 69 73 20 63 61 73 65 20 6f 63 63 75 72   This case occur
29dea 73 20 77 68 65 6e 20 74 77 6f 20 74 65 72 6d 73  s when two terms
29deb 20 28 73 69 6d 70 6c 65 20 74 65 72 6d 73 20 6f   (simple terms o
29dec 72 20 70 68 72 61 73 65 73 29 20 61 72 65 0a 20  r phrases) are. 
29ded 20 20 20 20 20 20 20 20 2a 20 63 6f 6e 6e 65 63          * connec
29dee 74 65 64 20 62 79 20 61 20 4e 45 41 52 20 6f 70  ted by a NEAR op
29def 65 72 61 74 6f 72 2c 20 73 70 61 6e 20 28 6e 4e  erator, span (nN
29df0 65 61 72 2b 31 29 2e 20 69 2e 65 2e 0a 20 20 20  ear+1). i.e..   
29df1 20 20 20 20 20 20 2a 0a 20 20 20 20 20 20 20 20        *.        
29df2 20 2a 20 20 20 20 20 27 22 74 65 72 72 69 62 6c   *     '"terribl
29df3 65 20 63 6f 6d 70 61 6e 79 22 20 4e 45 41 52 20  e company" NEAR 
29df4 77 69 64 67 65 74 27 0a 20 20 20 20 20 20 20 20  widget'.        
29df5 20 2a 2f 0a 20 20 20 20 20 20 20 20 44 61 74 61   */.        Data
29df6 42 75 66 66 65 72 20 6f 6e 65 20 3d 20 7b 30 2c  Buffer one = {0,
29df7 20 30 2c 20 30 7d 3b 0a 20 20 20 20 20 20 20 20   0, 0};.        
29df8 44 61 74 61 42 75 66 66 65 72 20 74 77 6f 20 3d  DataBuffer two =
29df9 20 7b 30 2c 20 30 2c 20 30 7d 3b 0a 0a 20 20 20   {0, 0, 0};..   
29dfa 20 20 20 20 20 44 4c 57 72 69 74 65 72 20 64 6c       DLWriter dl
29dfb 77 72 69 74 65 72 32 3b 0a 20 20 20 20 20 20 20  writer2;.       
29dfc 20 44 4c 52 65 61 64 65 72 20 64 72 31 20 3d 20   DLReader dr1 = 
29dfd 7b 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 7d 3b  {0, 0, 0, 0, 0};
29dfe 20 0a 20 20 20 20 20 20 20 20 44 4c 52 65 61 64   .        DLRead
29dff 65 72 20 64 72 32 20 3d 20 7b 30 2c 20 30 2c 20  er dr2 = {0, 0, 
29e00 30 2c 20 30 2c 20 30 7d 3b 0a 0a 20 20 20 20 20  0, 0, 0};..     
29e01 20 20 20 64 6c 77 49 6e 69 74 28 26 64 6c 77 72     dlwInit(&dlwr
29e02 69 74 65 72 32 2c 20 69 54 79 70 65 2c 20 26 6f  iter2, iType, &o
29e03 6e 65 29 3b 0a 20 20 20 20 20 20 20 20 70 6f 73  ne);.        pos
29e04 4c 69 73 74 50 68 72 61 73 65 4d 65 72 67 65 28  ListPhraseMerge(
29e05 26 72 69 67 68 74 2c 20 26 6c 65 66 74 2c 20 6e  &right, &left, n
29e06 4e 65 61 72 2d 33 2b 6e 50 68 72 61 73 65 2c 20  Near-3+nPhrase, 
29e07 31 2c 20 26 64 6c 77 72 69 74 65 72 32 29 3b 0a  1, &dlwriter2);.
29e08 20 20 20 20 20 20 20 20 64 6c 77 49 6e 69 74 28          dlwInit(
29e09 26 64 6c 77 72 69 74 65 72 32 2c 20 69 54 79 70  &dlwriter2, iTyp
29e0a 65 2c 20 26 74 77 6f 29 3b 0a 20 20 20 20 20 20  e, &two);.      
29e0b 20 20 70 6f 73 4c 69 73 74 50 68 72 61 73 65 4d    posListPhraseM
29e0c 65 72 67 65 28 26 6c 65 66 74 2c 20 26 72 69 67  erge(&left, &rig
29e0d 68 74 2c 20 6e 4e 65 61 72 2d 31 2c 20 30 2c 20  ht, nNear-1, 0, 
29e0e 26 64 6c 77 72 69 74 65 72 32 29 3b 0a 0a 20 20  &dlwriter2);..  
29e0f 20 20 20 20 20 20 69 66 28 20 6f 6e 65 2e 6e 44        if( one.nD
29e10 61 74 61 29 20 64 6c 72 49 6e 69 74 28 26 64 72  ata) dlrInit(&dr
29e11 31 2c 20 69 54 79 70 65 2c 20 6f 6e 65 2e 70 44  1, iType, one.pD
29e12 61 74 61 2c 20 6f 6e 65 2e 6e 44 61 74 61 29 3b  ata, one.nData);
29e13 0a 20 20 20 20 20 20 20 20 69 66 28 20 74 77 6f  .        if( two
29e14 2e 6e 44 61 74 61 29 20 64 6c 72 49 6e 69 74 28  .nData) dlrInit(
29e15 26 64 72 32 2c 20 69 54 79 70 65 2c 20 74 77 6f  &dr2, iType, two
29e16 2e 70 44 61 74 61 2c 20 74 77 6f 2e 6e 44 61 74  .pData, two.nDat
29e17 61 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28  a);..        if(
29e18 20 21 64 6c 72 41 74 45 6e 64 28 26 64 72 31 29   !dlrAtEnd(&dr1)
29e19 20 7c 7c 20 21 64 6c 72 41 74 45 6e 64 28 26 64   || !dlrAtEnd(&d
29e1a 72 32 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  r2) ){.         
29e1b 20 50 4c 52 65 61 64 65 72 20 70 72 31 20 3d 20   PLReader pr1 = 
29e1c 7b 30 7d 3b 0a 20 20 20 20 20 20 20 20 20 20 50  {0};.          P
29e1d 4c 52 65 61 64 65 72 20 70 72 32 20 3d 20 7b 30  LReader pr2 = {0
29e1e 7d 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 50 4c  };..          PL
29e1f 57 72 69 74 65 72 20 70 6c 77 72 69 74 65 72 3b  Writer plwriter;
29e20 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77 49 6e  .          plwIn
29e21 69 74 28 26 70 6c 77 72 69 74 65 72 2c 20 26 77  it(&plwriter, &w
29e22 72 69 74 65 72 2c 20 64 6c 72 44 6f 63 69 64 28  riter, dlrDocid(
29e23 64 6c 72 41 74 45 6e 64 28 26 64 72 31 29 3f 26  dlrAtEnd(&dr1)?&
29e24 64 72 32 3a 26 64 72 31 29 29 3b 0a 0a 20 20 20  dr2:&dr1));..   
29e25 20 20 20 20 20 20 20 69 66 28 20 6f 6e 65 2e 6e         if( one.n
29e26 44 61 74 61 20 29 20 70 6c 72 49 6e 69 74 28 26  Data ) plrInit(&
29e27 70 72 31 2c 20 26 64 72 31 29 3b 0a 20 20 20 20  pr1, &dr1);.    
29e28 20 20 20 20 20 20 69 66 28 20 74 77 6f 2e 6e 44        if( two.nD
29e29 61 74 61 20 29 20 70 6c 72 49 6e 69 74 28 26 70  ata ) plrInit(&p
29e2a 72 32 2c 20 26 64 72 32 29 3b 0a 20 20 20 20 20  r2, &dr2);.     
29e2b 20 20 20 20 20 77 68 69 6c 65 28 20 21 70 6c 72       while( !plr
29e2c 41 74 45 6e 64 28 26 70 72 31 29 20 7c 7c 20 21  AtEnd(&pr1) || !
29e2d 70 6c 72 41 74 45 6e 64 28 26 70 72 32 29 20 29  plrAtEnd(&pr2) )
29e2e 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e  {.            in
29e2f 74 20 69 43 6f 6d 70 61 72 65 20 3d 20 70 6c 72  t iCompare = plr
29e30 43 6f 6d 70 61 72 65 28 26 70 72 31 2c 20 26 70  Compare(&pr1, &p
29e31 72 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  r2);.           
29e32 20 73 77 69 74 63 68 28 20 69 43 6f 6d 70 61 72   switch( iCompar
29e33 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  e ){.           
29e34 20 20 20 63 61 73 65 20 2d 31 3a 0a 20 20 20 20     case -1:.    
29e35 20 20 20 20 20 20 20 20 20 20 20 20 70 6c 77 43              plwC
29e36 6f 70 79 28 26 70 6c 77 72 69 74 65 72 2c 20 26  opy(&plwriter, &
29e37 70 72 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20  pr1);.          
29e38 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 70        plrStep(&p
29e39 72 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  r1);.           
29e3a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
29e3b 20 20 20 20 20 20 20 20 20 20 63 61 73 65 20 31            case 1
29e3c 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  :.              
29e3d 20 20 70 6c 77 43 6f 70 79 28 26 70 6c 77 72 69    plwCopy(&plwri
29e3e 74 65 72 2c 20 26 70 72 32 29 3b 0a 20 20 20 20  ter, &pr2);.    
29e3f 20 20 20 20 20 20 20 20 20 20 20 20 70 6c 72 53              plrS
29e40 74 65 70 28 26 70 72 32 29 3b 0a 20 20 20 20 20  tep(&pr2);.     
29e41 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
29e42 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
29e43 63 61 73 65 20 30 3a 0a 20 20 20 20 20 20 20 20  case 0:.        
29e44 20 20 20 20 20 20 20 20 70 6c 77 43 6f 70 79 28          plwCopy(
29e45 26 70 6c 77 72 69 74 65 72 2c 20 26 70 72 31 29  &plwriter, &pr1)
29e46 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
29e47 20 20 70 6c 72 53 74 65 70 28 26 70 72 31 29 3b    plrStep(&pr1);
29e48 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
29e49 20 70 6c 72 53 74 65 70 28 26 70 72 32 29 3b 0a   plrStep(&pr2);.
29e4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29e4b 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
29e4c 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d     }.          }
29e4d 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77 54 65  .          plwTe
29e4e 72 6d 69 6e 61 74 65 28 26 70 6c 77 72 69 74 65  rminate(&plwrite
29e4f 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  r);.        }.  
29e50 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72        dataBuffer
29e51 44 65 73 74 72 6f 79 28 26 6f 6e 65 29 3b 0a 20  Destroy(&one);. 
29e52 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65         dataBuffe
29e53 72 44 65 73 74 72 6f 79 28 26 74 77 6f 29 3b 0a  rDestroy(&two);.
29e54 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 6c        }.      dl
29e55 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20  rStep(&left);.  
29e56 20 20 20 20 64 6c 72 53 74 65 70 28 26 72 69 67      dlrStep(&rig
29e57 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ht);.    }.  }..
29e58 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 6c 65    dlrDestroy(&le
29e59 66 74 29 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f  ft);.  dlrDestro
29e5a 79 28 26 72 69 67 68 74 29 3b 0a 20 20 64 6c 77  y(&right);.  dlw
29e5b 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29  Destroy(&writer)
29e5c 3b 0a 7d 0a 0a 2f 2a 20 57 65 20 68 61 76 65 20  ;.}../* We have 
29e5d 74 77 6f 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f  two DL_DOCIDS do
29e5e 63 6c 69 73 74 73 3a 20 20 70 4c 65 66 74 20 61  clists:  pLeft a
29e5f 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2a 20 57 72  nd pRight..** Wr
29e60 69 74 65 20 74 68 65 20 69 6e 74 65 72 73 65 63  ite the intersec
29e61 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 74 77  tion of these tw
29e62 6f 20 64 6f 63 6c 69 73 74 73 20 69 6e 74 6f 20  o doclists into 
29e63 70 4f 75 74 20 61 73 20 61 0a 2a 2a 20 44 4c 5f  pOut as a.** DL_
29e64 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74 2e 0a  DOCIDS doclist..
29e65 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  */.static void d
29e66 6f 63 4c 69 73 74 41 6e 64 4d 65 72 67 65 28 0a  ocListAndMerge(.
29e67 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4c    const char *pL
29e68 65 66 74 2c 20 69 6e 74 20 6e 4c 65 66 74 2c 0a  eft, int nLeft,.
29e69 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 52    const char *pR
29e6a 69 67 68 74 2c 20 69 6e 74 20 6e 52 69 67 68 74  ight, int nRight
29e6b 2c 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 2a  ,.  DataBuffer *
29e6c 70 4f 75 74 20 20 20 20 20 20 2f 2a 20 57 72 69  pOut      /* Wri
29e6d 74 65 20 74 68 65 20 63 6f 6d 62 69 6e 65 64 20  te the combined 
29e6e 64 6f 63 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a  doclist here */.
29e6f 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 6c 65  ){.  DLReader le
29e70 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 44 4c 57  ft, right;.  DLW
29e71 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a 0a 20  riter writer;.. 
29e72 20 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20 7c 7c   if( nLeft==0 ||
29e73 20 6e 52 69 67 68 74 3d 3d 30 20 29 20 72 65 74   nRight==0 ) ret
29e74 75 72 6e 3b 0a 0a 20 20 64 6c 72 49 6e 69 74 28  urn;..  dlrInit(
29e75 26 6c 65 66 74 2c 20 44 4c 5f 44 4f 43 49 44 53  &left, DL_DOCIDS
29e76 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b  , pLeft, nLeft);
29e77 0a 20 20 64 6c 72 49 6e 69 74 28 26 72 69 67 68  .  dlrInit(&righ
29e78 74 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 52  t, DL_DOCIDS, pR
29e79 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20  ight, nRight);. 
29e7a 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72   dlwInit(&writer
29e7b 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 4f 75  , DL_DOCIDS, pOu
29e7c 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 21 64  t);..  while( !d
29e7d 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29 20 26  lrAtEnd(&left) &
29e7e 26 20 21 64 6c 72 41 74 45 6e 64 28 26 72 69 67  & !dlrAtEnd(&rig
29e7f 68 74 29 20 29 7b 0a 20 20 20 20 69 66 28 20 64  ht) ){.    if( d
29e80 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29 3c 64  lrDocid(&left)<d
29e81 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74 29 20  lrDocid(&right) 
29e82 29 7b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70  ){.      dlrStep
29e83 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c  (&left);.    }el
29e84 73 65 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28  se if( dlrDocid(
29e85 26 72 69 67 68 74 29 3c 64 6c 72 44 6f 63 69 64  &right)<dlrDocid
29e86 28 26 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 20  (&left) ){.     
29e87 20 64 6c 72 53 74 65 70 28 26 72 69 67 68 74 29   dlrStep(&right)
29e88 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
29e89 20 20 20 64 6c 77 41 64 64 28 26 77 72 69 74 65     dlwAdd(&write
29e8a 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66  r, dlrDocid(&lef
29e8b 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74  t));.      dlrSt
29e8c 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 20  ep(&left);.     
29e8d 20 64 6c 72 53 74 65 70 28 26 72 69 67 68 74 29   dlrStep(&right)
29e8e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 64  ;.    }.  }..  d
29e8f 6c 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29  lrDestroy(&left)
29e90 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26  ;.  dlrDestroy(&
29e91 72 69 67 68 74 29 3b 0a 20 20 64 6c 77 44 65 73  right);.  dlwDes
29e92 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 7d  troy(&writer);.}
29e93 0a 0a 2f 2a 20 57 65 20 68 61 76 65 20 74 77 6f  ../* We have two
29e94 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69   DL_DOCIDS docli
29e95 73 74 73 3a 20 20 70 4c 65 66 74 20 61 6e 64 20  sts:  pLeft and 
29e96 70 52 69 67 68 74 2e 0a 2a 2a 20 57 72 69 74 65  pRight..** Write
29e97 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20 74 68   the union of th
29e98 65 73 65 20 74 77 6f 20 64 6f 63 6c 69 73 74 73  ese two doclists
29e99 20 69 6e 74 6f 20 70 4f 75 74 20 61 73 20 61 0a   into pOut as a.
29e9a 2a 2a 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63  ** DL_DOCIDS doc
29e9b 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  list..*/.static 
29e9c 76 6f 69 64 20 64 6f 63 4c 69 73 74 4f 72 4d 65  void docListOrMe
29e9d 72 67 65 28 0a 20 20 63 6f 6e 73 74 20 63 68 61  rge(.  const cha
29e9e 72 20 2a 70 4c 65 66 74 2c 20 69 6e 74 20 6e 4c  r *pLeft, int nL
29e9f 65 66 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  eft,.  const cha
29ea0 72 20 2a 70 52 69 67 68 74 2c 20 69 6e 74 20 6e  r *pRight, int n
29ea1 52 69 67 68 74 2c 0a 20 20 44 61 74 61 42 75 66  Right,.  DataBuf
29ea2 66 65 72 20 2a 70 4f 75 74 20 20 20 20 20 20 2f  fer *pOut      /
29ea3 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6d 62  * Write the comb
29ea4 69 6e 65 64 20 64 6f 63 6c 69 73 74 20 68 65 72  ined doclist her
29ea5 65 20 2a 2f 0a 29 7b 0a 20 20 44 4c 52 65 61 64  e */.){.  DLRead
29ea6 65 72 20 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a  er left, right;.
29ea7 20 20 44 4c 57 72 69 74 65 72 20 77 72 69 74 65    DLWriter write
29ea8 72 3b 0a 0a 20 20 69 66 28 20 6e 4c 65 66 74 3d  r;..  if( nLeft=
29ea9 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 52  =0 ){.    if( nR
29eaa 69 67 68 74 21 3d 30 20 29 20 64 61 74 61 42 75  ight!=0 ) dataBu
29eab 66 66 65 72 41 70 70 65 6e 64 28 70 4f 75 74 2c  fferAppend(pOut,
29eac 20 70 52 69 67 68 74 2c 20 6e 52 69 67 68 74 29   pRight, nRight)
29ead 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
29eae 7d 0a 20 20 69 66 28 20 6e 52 69 67 68 74 3d 3d  }.  if( nRight==
29eaf 30 20 29 7b 0a 20 20 20 20 64 61 74 61 42 75 66  0 ){.    dataBuf
29eb0 66 65 72 41 70 70 65 6e 64 28 70 4f 75 74 2c 20  ferAppend(pOut, 
29eb1 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20  pLeft, nLeft);. 
29eb2 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
29eb3 20 20 64 6c 72 49 6e 69 74 28 26 6c 65 66 74 2c    dlrInit(&left,
29eb4 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 4c 65 66   DL_DOCIDS, pLef
29eb5 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20 64 6c 72  t, nLeft);.  dlr
29eb6 49 6e 69 74 28 26 72 69 67 68 74 2c 20 44 4c 5f  Init(&right, DL_
29eb7 44 4f 43 49 44 53 2c 20 70 52 69 67 68 74 2c 20  DOCIDS, pRight, 
29eb8 6e 52 69 67 68 74 29 3b 0a 20 20 64 6c 77 49 6e  nRight);.  dlwIn
29eb9 69 74 28 26 77 72 69 74 65 72 2c 20 44 4c 5f 44  it(&writer, DL_D
29eba 4f 43 49 44 53 2c 20 70 4f 75 74 29 3b 0a 0a 20  OCIDS, pOut);.. 
29ebb 20 77 68 69 6c 65 28 20 21 64 6c 72 41 74 45 6e   while( !dlrAtEn
29ebc 64 28 26 6c 65 66 74 29 20 7c 7c 20 21 64 6c 72  d(&left) || !dlr
29ebd 41 74 45 6e 64 28 26 72 69 67 68 74 29 20 29 7b  AtEnd(&right) ){
29ebe 0a 20 20 20 20 69 66 28 20 64 6c 72 41 74 45 6e  .    if( dlrAtEn
29ebf 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20  d(&right) ){.   
29ec0 20 20 20 64 6c 77 41 64 64 28 26 77 72 69 74 65     dlwAdd(&write
29ec1 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66  r, dlrDocid(&lef
29ec2 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74  t));.      dlrSt
29ec3 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d  ep(&left);.    }
29ec4 65 6c 73 65 20 69 66 28 20 64 6c 72 41 74 45 6e  else if( dlrAtEn
29ec5 64 28 26 6c 65 66 74 29 20 29 7b 0a 20 20 20 20  d(&left) ){.    
29ec6 20 20 64 6c 77 41 64 64 28 26 77 72 69 74 65 72    dlwAdd(&writer
29ec7 2c 20 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68  , dlrDocid(&righ
29ec8 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74  t));.      dlrSt
29ec9 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20  ep(&right);.    
29eca 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 44 6f 63  }else if( dlrDoc
29ecb 69 64 28 26 6c 65 66 74 29 3c 64 6c 72 44 6f 63  id(&left)<dlrDoc
29ecc 69 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20  id(&right) ){.  
29ecd 20 20 20 20 64 6c 77 41 64 64 28 26 77 72 69 74      dlwAdd(&writ
29ece 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 6c 65  er, dlrDocid(&le
29ecf 66 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72 53  ft));.      dlrS
29ed0 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20  tep(&left);.    
29ed1 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 44 6f 63  }else if( dlrDoc
29ed2 69 64 28 26 72 69 67 68 74 29 3c 64 6c 72 44 6f  id(&right)<dlrDo
29ed3 63 69 64 28 26 6c 65 66 74 29 20 29 7b 0a 20 20  cid(&left) ){.  
29ed4 20 20 20 20 64 6c 77 41 64 64 28 26 77 72 69 74      dlwAdd(&writ
29ed5 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 72 69  er, dlrDocid(&ri
29ed6 67 68 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72  ght));.      dlr
29ed7 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20  Step(&right);.  
29ed8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64    }else{.      d
29ed9 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c 20 64  lwAdd(&writer, d
29eda 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29 29 3b  lrDocid(&left));
29edb 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26  .      dlrStep(&
29edc 6c 65 66 74 29 3b 0a 20 20 20 20 20 20 64 6c 72  left);.      dlr
29edd 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20  Step(&right);.  
29ede 20 20 7d 0a 20 20 7d 0a 0a 20 20 64 6c 72 44 65    }.  }..  dlrDe
29edf 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20  stroy(&left);.  
29ee0 64 6c 72 44 65 73 74 72 6f 79 28 26 72 69 67 68  dlrDestroy(&righ
29ee1 74 29 3b 0a 20 20 64 6c 77 44 65 73 74 72 6f 79  t);.  dlwDestroy
29ee2 28 26 77 72 69 74 65 72 29 3b 0a 7d 0a 0a 2f 2a  (&writer);.}../*
29ee3 20 57 65 20 68 61 76 65 20 74 77 6f 20 44 4c 5f   We have two DL_
29ee4 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74 73 3a  DOCIDS doclists:
29ee5 20 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67    pLeft and pRig
29ee6 68 74 2e 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74  ht..** Write int
29ee7 6f 20 70 4f 75 74 20 61 73 20 44 4c 5f 44 4f 43  o pOut as DL_DOC
29ee8 49 44 53 20 64 6f 63 6c 69 73 74 20 63 6f 6e 74  IDS doclist cont
29ee9 61 69 6e 69 6e 67 20 61 6c 6c 20 64 6f 63 75 6d  aining all docum
29eea 65 6e 74 73 20 74 68 61 74 0a 2a 2a 20 6f 63 63  ents that.** occ
29eeb 75 72 20 69 6e 20 70 4c 65 66 74 20 62 75 74 20  ur in pLeft but 
29eec 6e 6f 74 20 69 6e 20 70 52 69 67 68 74 2e 0a 2a  not in pRight..*
29eed 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f  /.static void do
29eee 63 4c 69 73 74 45 78 63 65 70 74 4d 65 72 67 65  cListExceptMerge
29eef 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  (.  const char *
29ef0 70 4c 65 66 74 2c 20 69 6e 74 20 6e 4c 65 66 74  pLeft, int nLeft
29ef1 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
29ef2 70 52 69 67 68 74 2c 20 69 6e 74 20 6e 52 69 67  pRight, int nRig
29ef3 68 74 2c 0a 20 20 44 61 74 61 42 75 66 66 65 72  ht,.  DataBuffer
29ef4 20 2a 70 4f 75 74 20 20 20 20 20 20 2f 2a 20 57   *pOut      /* W
29ef5 72 69 74 65 20 74 68 65 20 63 6f 6d 62 69 6e 65  rite the combine
29ef6 64 20 64 6f 63 6c 69 73 74 20 68 65 72 65 20 2a  d doclist here *
29ef7 2f 0a 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20  /.){.  DLReader 
29ef8 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 44  left, right;.  D
29ef9 4c 57 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a  LWriter writer;.
29efa 0a 20 20 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20  .  if( nLeft==0 
29efb 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
29efc 6e 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20  nRight==0 ){.   
29efd 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
29efe 64 28 70 4f 75 74 2c 20 70 4c 65 66 74 2c 20 6e  d(pOut, pLeft, n
29eff 4c 65 66 74 29 3b 0a 20 20 20 20 72 65 74 75 72  Left);.    retur
29f00 6e 3b 0a 20 20 7d 0a 0a 20 20 64 6c 72 49 6e 69  n;.  }..  dlrIni
29f01 74 28 26 6c 65 66 74 2c 20 44 4c 5f 44 4f 43 49  t(&left, DL_DOCI
29f02 44 53 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74  DS, pLeft, nLeft
29f03 29 3b 0a 20 20 64 6c 72 49 6e 69 74 28 26 72 69  );.  dlrInit(&ri
29f04 67 68 74 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20  ght, DL_DOCIDS, 
29f05 70 52 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b  pRight, nRight);
29f06 0a 20 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74  .  dlwInit(&writ
29f07 65 72 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70  er, DL_DOCIDS, p
29f08 4f 75 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20  Out);..  while( 
29f09 21 64 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29  !dlrAtEnd(&left)
29f0a 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 21   ){.    while( !
29f0b 64 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29  dlrAtEnd(&right)
29f0c 20 26 26 20 64 6c 72 44 6f 63 69 64 28 26 72 69   && dlrDocid(&ri
29f0d 67 68 74 29 3c 64 6c 72 44 6f 63 69 64 28 26 6c  ght)<dlrDocid(&l
29f0e 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20 64 6c  eft) ){.      dl
29f0f 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20  rStep(&right);. 
29f10 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64 6c 72     }.    if( dlr
29f11 41 74 45 6e 64 28 26 72 69 67 68 74 29 20 7c 7c  AtEnd(&right) ||
29f12 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29   dlrDocid(&left)
29f13 3c 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74  <dlrDocid(&right
29f14 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 77 41 64  ) ){.      dlwAd
29f15 64 28 26 77 72 69 74 65 72 2c 20 64 6c 72 44 6f  d(&writer, dlrDo
29f16 63 69 64 28 26 6c 65 66 74 29 29 3b 0a 20 20 20  cid(&left));.   
29f17 20 7d 0a 20 20 20 20 64 6c 72 53 74 65 70 28 26   }.    dlrStep(&
29f18 6c 65 66 74 29 3b 0a 20 20 7d 0a 0a 20 20 64 6c  left);.  }..  dl
29f19 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29 3b  rDestroy(&left);
29f1a 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72  .  dlrDestroy(&r
29f1b 69 67 68 74 29 3b 0a 20 20 64 6c 77 44 65 73 74  ight);.  dlwDest
29f1c 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 7d 0a  roy(&writer);.}.
29f1d 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 73 74  .static char *st
29f1e 72 69 6e 67 5f 64 75 70 5f 6e 28 63 6f 6e 73 74  ring_dup_n(const
29f1f 20 63 68 61 72 20 2a 73 2c 20 69 6e 74 20 6e 29   char *s, int n)
29f20 7b 0a 20 20 63 68 61 72 20 2a 73 74 72 20 3d 20  {.  char *str = 
29f21 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
29f22 20 2b 20 31 29 3b 0a 20 20 6d 65 6d 63 70 79 28   + 1);.  memcpy(
29f23 73 74 72 2c 20 73 2c 20 6e 29 3b 0a 20 20 73 74  str, s, n);.  st
29f24 72 5b 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 72  r[n] = '\0';.  r
29f25 65 74 75 72 6e 20 73 74 72 3b 0a 7d 0a 0a 2f 2a  eturn str;.}../*
29f26 20 44 75 70 6c 69 63 61 74 65 20 61 20 73 74 72   Duplicate a str
29f27 69 6e 67 3b 20 74 68 65 20 63 61 6c 6c 65 72 20  ing; the caller 
29f28 6d 75 73 74 20 66 72 65 65 28 29 20 74 68 65 20  must free() the 
29f29 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 2e  returned string.
29f2a 0a 20 2a 20 28 57 65 20 64 6f 6e 27 74 20 75 73  . * (We don't us
29f2b 65 20 73 74 72 64 75 70 28 29 20 73 69 6e 63 65  e strdup() since
29f2c 20 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20   it is not part 
29f2d 6f 66 20 74 68 65 20 73 74 61 6e 64 61 72 64 20  of the standard 
29f2e 43 20 6c 69 62 72 61 72 79 20 61 6e 64 0a 20 2a  C library and. *
29f2f 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 76 61 69   may not be avai
29f30 6c 61 62 6c 65 20 65 76 65 72 79 77 68 65 72 65  lable everywhere
29f31 2e 29 20 2a 2f 0a 73 74 61 74 69 63 20 63 68 61  .) */.static cha
29f32 72 20 2a 73 74 72 69 6e 67 5f 64 75 70 28 63 6f  r *string_dup(co
29f33 6e 73 74 20 63 68 61 72 20 2a 73 29 7b 0a 20 20  nst char *s){.  
29f34 72 65 74 75 72 6e 20 73 74 72 69 6e 67 5f 64 75  return string_du
29f35 70 5f 6e 28 73 2c 20 73 74 72 6c 65 6e 28 73 29  p_n(s, strlen(s)
29f36 29 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 6d 61 74 20  );.}../* Format 
29f37 61 20 73 74 72 69 6e 67 2c 20 72 65 70 6c 61 63  a string, replac
29f38 69 6e 67 20 65 61 63 68 20 6f 63 63 75 72 72 65  ing each occurre
29f39 6e 63 65 20 6f 66 20 74 68 65 20 25 20 63 68 61  nce of the % cha
29f3a 72 61 63 74 65 72 20 77 69 74 68 0a 20 2a 20 7a  racter with. * z
29f3b 44 62 2e 7a 4e 61 6d 65 2e 20 20 54 68 69 73 20  Db.zName.  This 
29f3c 6d 61 79 20 62 65 20 6d 6f 72 65 20 63 6f 6e 76  may be more conv
29f3d 65 6e 69 65 6e 74 20 74 68 61 6e 20 73 71 6c 69  enient than sqli
29f3e 74 65 5f 6d 70 72 69 6e 74 66 28 29 0a 20 2a 20  te_mprintf(). * 
29f3f 77 68 65 6e 20 6f 6e 65 20 73 74 72 69 6e 67 20  when one string 
29f40 69 73 20 75 73 65 64 20 72 65 70 65 61 74 65 64  is used repeated
29f41 6c 79 20 69 6e 20 61 20 66 6f 72 6d 61 74 20 73  ly in a format s
29f42 74 72 69 6e 67 2e 0a 20 2a 20 54 68 65 20 63 61  tring.. * The ca
29f43 6c 6c 65 72 20 6d 75 73 74 20 66 72 65 65 28 29  ller must free()
29f44 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74   the returned st
29f45 72 69 6e 67 2e 20 2a 2f 0a 73 74 61 74 69 63 20  ring. */.static 
29f46 63 68 61 72 20 2a 73 74 72 69 6e 67 5f 66 6f 72  char *string_for
29f47 6d 61 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  mat(const char *
29f48 7a 46 6f 72 6d 61 74 2c 0a 20 20 20 20 20 20 20  zFormat,.       
29f49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29f4a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
29f4b 7a 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  zDb, const char 
29f4c 2a 7a 4e 61 6d 65 29 7b 0a 20 20 63 6f 6e 73 74  *zName){.  const
29f4d 20 63 68 61 72 20 2a 70 3b 0a 20 20 73 69 7a 65   char *p;.  size
29f4e 5f 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 73 69  _t len = 0;.  si
29f4f 7a 65 5f 74 20 6e 44 62 20 3d 20 73 74 72 6c 65  ze_t nDb = strle
29f50 6e 28 7a 44 62 29 3b 0a 20 20 73 69 7a 65 5f 74  n(zDb);.  size_t
29f51 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28   nName = strlen(
29f52 7a 4e 61 6d 65 29 3b 0a 20 20 73 69 7a 65 5f 74  zName);.  size_t
29f53 20 6e 46 75 6c 6c 54 61 62 6c 65 4e 61 6d 65 20   nFullTableName 
29f54 3d 20 6e 44 62 2b 31 2b 6e 4e 61 6d 65 3b 0a 20  = nDb+1+nName;. 
29f55 20 63 68 61 72 20 2a 72 65 73 75 6c 74 3b 0a 20   char *result;. 
29f56 20 63 68 61 72 20 2a 72 3b 0a 0a 20 20 2f 2a 20   char *r;..  /* 
29f57 66 69 72 73 74 20 63 6f 6d 70 75 74 65 20 6c 65  first compute le
29f58 6e 67 74 68 20 6e 65 65 64 65 64 20 2a 2f 0a 20  ngth needed */. 
29f59 20 66 6f 72 28 70 20 3d 20 7a 46 6f 72 6d 61 74   for(p = zFormat
29f5a 20 3b 20 2a 70 20 3b 20 2b 2b 70 29 7b 0a 20 20   ; *p ; ++p){.  
29f5b 20 20 6c 65 6e 20 2b 3d 20 28 2a 70 3d 3d 27 25    len += (*p=='%
29f5c 27 20 3f 20 6e 46 75 6c 6c 54 61 62 6c 65 4e 61  ' ? nFullTableNa
29f5d 6d 65 20 3a 20 31 29 3b 0a 20 20 7d 0a 20 20 6c  me : 1);.  }.  l
29f5e 65 6e 20 2b 3d 20 31 3b 20 20 2f 2a 20 66 6f 72  en += 1;  /* for
29f5f 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 74 6f 72   null terminator
29f60 20 2a 2f 0a 0a 20 20 72 20 3d 20 72 65 73 75 6c   */..  r = resul
29f61 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  t = sqlite3_mall
29f62 6f 63 28 6c 65 6e 29 3b 0a 20 20 66 6f 72 28 70  oc(len);.  for(p
29f63 20 3d 20 7a 46 6f 72 6d 61 74 3b 20 2a 70 3b 20   = zFormat; *p; 
29f64 2b 2b 70 29 7b 0a 20 20 20 20 69 66 28 20 2a 70  ++p){.    if( *p
29f65 3d 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20 6d  =='%' ){.      m
29f66 65 6d 63 70 79 28 72 2c 20 7a 44 62 2c 20 6e 44  emcpy(r, zDb, nD
29f67 62 29 3b 0a 20 20 20 20 20 20 72 20 2b 3d 20 6e  b);.      r += n
29f68 44 62 3b 0a 20 20 20 20 20 20 2a 72 2b 2b 20 3d  Db;.      *r++ =
29f69 20 27 2e 27 3b 0a 20 20 20 20 20 20 6d 65 6d 63   '.';.      memc
29f6a 70 79 28 72 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  py(r, zName, nNa
29f6b 6d 65 29 3b 0a 20 20 20 20 20 20 72 20 2b 3d 20  me);.      r += 
29f6c 6e 4e 61 6d 65 3b 0a 20 20 20 20 7d 20 65 6c 73  nName;.    } els
29f6d 65 20 7b 0a 20 20 20 20 20 20 2a 72 2b 2b 20 3d  e {.      *r++ =
29f6e 20 2a 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20   *p;.    }.  }. 
29f6f 20 2a 72 2b 2b 20 3d 20 27 5c 30 27 3b 0a 20 20   *r++ = '\0';.  
29f70 61 73 73 65 72 74 28 20 72 20 3d 3d 20 72 65 73  assert( r == res
29f71 75 6c 74 20 2b 20 6c 65 6e 20 29 3b 0a 20 20 72  ult + len );.  r
29f72 65 74 75 72 6e 20 72 65 73 75 6c 74 3b 0a 7d 0a  eturn result;.}.
29f73 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 5f  .static int sql_
29f74 65 78 65 63 28 73 71 6c 69 74 65 33 20 2a 64 62  exec(sqlite3 *db
29f75 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44  , const char *zD
29f76 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
29f77 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20  Name,.          
29f78 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
29f79 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 29 7b 0a  char *zFormat){.
29f7a 20 20 63 68 61 72 20 2a 7a 43 6f 6d 6d 61 6e 64    char *zCommand
29f7b 20 3d 20 73 74 72 69 6e 67 5f 66 6f 72 6d 61 74   = string_format
29f7c 28 7a 46 6f 72 6d 61 74 2c 20 7a 44 62 2c 20 7a  (zFormat, zDb, z
29f7d 4e 61 6d 65 29 3b 0a 20 20 69 6e 74 20 72 63 3b  Name);.  int rc;
29f7e 0a 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54  .  FTSTRACE(("FT
29f7f 53 33 20 73 71 6c 3a 20 25 73 5c 6e 22 2c 20 7a  S3 sql: %s\n", z
29f80 43 6f 6d 6d 61 6e 64 29 29 3b 0a 20 20 72 63 20  Command));.  rc 
29f81 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64  = sqlite3_exec(d
29f82 62 2c 20 7a 43 6f 6d 6d 61 6e 64 2c 20 4e 55 4c  b, zCommand, NUL
29f83 4c 2c 20 30 2c 20 4e 55 4c 4c 29 3b 0a 20 20 73  L, 0, NULL);.  s
29f84 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6d  qlite3_free(zCom
29f85 6d 61 6e 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  mand);.  return 
29f86 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  rc;.}..static in
29f87 74 20 73 71 6c 5f 70 72 65 70 61 72 65 28 73 71  t sql_prepare(sq
29f88 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
29f89 20 63 68 61 72 20 2a 7a 44 62 2c 20 63 6f 6e 73   char *zDb, cons
29f8a 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20  t char *zName,. 
29f8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29f8c 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74        sqlite3_st
29f8d 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 63 6f 6e  mt **ppStmt, con
29f8e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74  st char *zFormat
29f8f 29 7b 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6d 6d  ){.  char *zComm
29f90 61 6e 64 20 3d 20 73 74 72 69 6e 67 5f 66 6f 72  and = string_for
29f91 6d 61 74 28 7a 46 6f 72 6d 61 74 2c 20 7a 44 62  mat(zFormat, zDb
29f92 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 6e 74 20  , zName);.  int 
29f93 72 63 3b 0a 20 20 46 54 53 54 52 41 43 45 28 28  rc;.  FTSTRACE((
29f94 22 46 54 53 33 20 70 72 65 70 61 72 65 3a 20 25  "FTS3 prepare: %
29f95 73 5c 6e 22 2c 20 7a 43 6f 6d 6d 61 6e 64 29 29  s\n", zCommand))
29f96 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
29f97 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c 20  _prepare_v2(db, 
29f98 7a 43 6f 6d 6d 61 6e 64 2c 20 2d 31 2c 20 70 70  zCommand, -1, pp
29f99 53 74 6d 74 2c 20 4e 55 4c 4c 29 3b 0a 20 20 73  Stmt, NULL);.  s
29f9a 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6d  qlite3_free(zCom
29f9b 6d 61 6e 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  mand);.  return 
29f9c 72 63 3b 0a 7d 0a 0a 2f 2a 20 65 6e 64 20 75 74  rc;.}../* end ut
29f9d 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e 73 20  ility functions 
29f9e 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72  */../* Forward r
29f9f 65 66 65 72 65 6e 63 65 20 2a 2f 0a 74 79 70 65  eference */.type
29fa0 64 65 66 20 73 74 72 75 63 74 20 66 75 6c 6c 74  def struct fullt
29fa1 65 78 74 5f 76 74 61 62 20 66 75 6c 6c 74 65 78  ext_vtab fulltex
29fa2 74 5f 76 74 61 62 3b 0a 0a 2f 2a 20 41 20 73 69  t_vtab;../* A si
29fa3 6e 67 6c 65 20 74 65 72 6d 20 69 6e 20 61 20 71  ngle term in a q
29fa4 75 65 72 79 20 69 73 20 72 65 70 72 65 73 65 6e  uery is represen
29fa5 74 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e  ted by an instan
29fa6 63 65 73 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f  ces of.** the fo
29fa7 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
29fa8 65 2e 20 45 61 63 68 20 77 6f 72 64 20 77 68 69  e. Each word whi
29fa9 63 68 20 6d 61 79 20 6d 61 74 63 68 20 61 67 61  ch may match aga
29faa 69 6e 73 74 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74  inst.** document
29fab 20 63 6f 6e 74 65 6e 74 20 69 73 20 61 20 74 65   content is a te
29fac 72 6d 2e 20 4f 70 65 72 61 74 6f 72 73 2c 20 6c  rm. Operators, l
29fad 69 6b 65 20 4e 45 41 52 20 6f 72 20 4f 52 2c 20  ike NEAR or OR, 
29fae 61 72 65 0a 2a 2a 20 6e 6f 74 20 74 65 72 6d 73  are.** not terms
29faf 2e 20 51 75 65 72 79 20 74 65 72 6d 73 20 61 72  . Query terms ar
29fb0 65 20 6f 72 67 61 6e 69 7a 65 64 20 61 73 20 61  e organized as a
29fb1 20 66 6c 61 74 20 6c 69 73 74 20 73 74 6f 72 65   flat list store
29fb2 64 0a 2a 2a 20 69 6e 20 74 68 65 20 51 75 65 72  d.** in the Quer
29fb3 79 2e 70 54 65 72 6d 73 20 61 72 72 61 79 2e 0a  y.pTerms array..
29fb4 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 51 75 65  **.** If the Que
29fb5 72 79 54 65 72 6d 2e 6e 50 68 72 61 73 65 20 76  ryTerm.nPhrase v
29fb6 61 72 69 61 62 6c 65 20 69 73 20 6e 6f 6e 2d 7a  ariable is non-z
29fb7 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 51 75  ero, then the Qu
29fb8 65 72 79 54 65 72 6d 0a 2a 2a 20 69 73 20 74 68  eryTerm.** is th
29fb9 65 20 66 69 72 73 74 20 69 6e 20 61 20 63 6f 6e  e first in a con
29fba 74 69 67 75 6f 75 73 20 73 74 72 69 6e 67 20 6f  tiguous string o
29fbb 66 20 74 65 72 6d 73 20 74 68 61 74 20 61 72 65  f terms that are
29fbc 20 65 69 74 68 65 72 20 70 61 72 74 0a 2a 2a 20   either part.** 
29fbd 6f 66 20 74 68 65 20 73 61 6d 65 20 70 68 72 61  of the same phra
29fbe 73 65 2c 20 6f 72 20 63 6f 6e 6e 65 63 74 65 64  se, or connected
29fbf 20 62 79 20 74 68 65 20 4e 45 41 52 20 6f 70 65   by the NEAR ope
29fc0 72 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  rator..**.** If 
29fc1 74 68 65 20 51 75 65 72 79 54 65 72 6d 2e 6e 4e  the QueryTerm.nN
29fc2 65 61 72 20 76 61 72 69 61 62 6c 65 20 69 73 20  ear variable is 
29fc3 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74  non-zero, then t
29fc4 68 65 20 74 6f 6b 65 6e 20 69 73 20 66 6f 6c 6c  he token is foll
29fc5 6f 77 65 64 20 0a 2a 2a 20 62 79 20 61 20 4e 45  owed .** by a NE
29fc6 41 52 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68  AR operator with
29fc7 20 73 70 61 6e 20 73 65 74 20 74 6f 20 28 6e 4e   span set to (nN
29fc8 65 61 72 2d 31 29 2e 20 46 6f 72 20 65 78 61 6d  ear-1). For exam
29fc9 70 6c 65 2c 20 74 68 65 20 0a 2a 2a 20 66 6f 6c  ple, the .** fol
29fca 6c 6f 77 69 6e 67 20 71 75 65 72 79 3a 0a 2a 2a  lowing query:.**
29fcb 0a 2a 2a 20 54 68 65 20 51 75 65 72 79 54 65 72  .** The QueryTer
29fcc 6d 2e 69 50 68 72 61 73 65 20 76 61 72 69 61 62  m.iPhrase variab
29fcd 6c 65 20 73 74 6f 72 65 73 20 74 68 65 20 69 6e  le stores the in
29fce 64 65 78 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  dex of the token
29fcf 20 77 69 74 68 69 6e 0a 2a 2a 20 69 74 73 20 70   within.** its p
29fd0 68 72 61 73 65 2c 20 69 6e 64 65 78 65 64 20 73  hrase, indexed s
29fd1 74 61 72 74 69 6e 67 20 61 74 20 31 2c 20 6f 72  tarting at 1, or
29fd2 20 31 20 69 66 20 74 68 65 20 74 6f 6b 65 6e 20   1 if the token 
29fd3 69 73 20 6e 6f 74 20 70 61 72 74 20 0a 2a 2a 20  is not part .** 
29fd4 6f 66 20 61 6e 79 20 70 68 72 61 73 65 2e 0a 2a  of any phrase..*
29fd5 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65  *.** For example
29fd6 2c 20 74 68 65 20 64 61 74 61 20 73 74 72 75 63  , the data struc
29fd7 74 75 72 65 20 75 73 65 64 20 74 6f 20 72 65 70  ture used to rep
29fd8 72 65 73 65 6e 74 20 74 68 65 20 66 6f 6c 6c 6f  resent the follo
29fd9 77 69 6e 67 20 71 75 65 72 79 3a 0a 2a 2a 0a 2a  wing query:.**.*
29fda 2a 20 20 20 20 20 2e 2e 2e 20 4d 41 54 43 48 20  *     ... MATCH 
29fdb 27 73 71 6c 69 74 65 20 4e 45 41 52 2f 35 20 67  'sqlite NEAR/5 g
29fdc 6f 6f 67 6c 65 20 4e 45 41 52 2f 32 20 22 73 65  oogle NEAR/2 "se
29fdd 61 72 63 68 20 65 6e 67 69 6e 65 22 27 0a 2a 2a  arch engine"'.**
29fde 0a 2a 2a 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  .** is:.**.**   
29fdf 20 20 7b 6e 50 68 72 61 73 65 3d 34 2c 20 69 50    {nPhrase=4, iP
29fe0 68 72 61 73 65 3d 31 2c 20 6e 4e 65 61 72 3d 36  hrase=1, nNear=6
29fe1 2c 20 70 54 65 72 6d 3d 22 73 71 6c 69 74 65 22  , pTerm="sqlite"
29fe2 7d 2c 0a 2a 2a 20 20 20 20 20 7b 6e 50 68 72 61  },.**     {nPhra
29fe3 73 65 3d 30 2c 20 69 50 68 72 61 73 65 3d 31 2c  se=0, iPhrase=1,
29fe4 20 6e 4e 65 61 72 3d 33 2c 20 70 54 65 72 6d 3d   nNear=3, pTerm=
29fe5 22 67 6f 6f 67 6c 65 22 7d 2c 0a 2a 2a 20 20 20  "google"},.**   
29fe6 20 20 7b 6e 50 68 72 61 73 65 3d 30 2c 20 69 50    {nPhrase=0, iP
29fe7 68 72 61 73 65 3d 31 2c 20 6e 4e 65 61 72 3d 30  hrase=1, nNear=0
29fe8 2c 20 70 54 65 72 6d 3d 22 73 65 61 72 63 68 22  , pTerm="search"
29fe9 7d 2c 0a 2a 2a 20 20 20 20 20 7b 6e 50 68 72 61  },.**     {nPhra
29fea 73 65 3d 30 2c 20 69 50 68 72 61 73 65 3d 32 2c  se=0, iPhrase=2,
29feb 20 6e 4e 65 61 72 3d 30 2c 20 70 54 65 72 6d 3d   nNear=0, pTerm=
29fec 22 65 6e 67 69 6e 65 22 7d 2c 0a 2a 2a 0a 2a 2a  "engine"},.**.**
29fed 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 46   compiling the F
29fee 54 53 33 20 73 79 6e 74 61 78 20 74 6f 20 51 75  TS3 syntax to Qu
29fef 65 72 79 20 73 74 72 75 63 74 75 72 65 73 20 69  ery structures i
29ff0 73 20 64 6f 6e 65 20 62 79 20 74 68 65 20 70 61  s done by the pa
29ff1 72 73 65 51 75 65 72 79 28 29 0a 2a 2a 20 66 75  rseQuery().** fu
29ff2 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64  nction..*/.typed
29ff3 65 66 20 73 74 72 75 63 74 20 51 75 65 72 79 54  ef struct QueryT
29ff4 65 72 6d 20 7b 0a 20 20 73 68 6f 72 74 20 69 6e  erm {.  short in
29ff5 74 20 6e 50 68 72 61 73 65 3b 20 2f 2a 20 48 6f  t nPhrase; /* Ho
29ff6 77 20 6d 61 6e 79 20 66 6f 6c 6c 6f 77 69 6e 67  w many following
29ff7 20 74 65 72 6d 73 20 61 72 65 20 70 61 72 74 20   terms are part 
29ff8 6f 66 20 74 68 65 20 73 61 6d 65 20 70 68 72 61  of the same phra
29ff9 73 65 20 2a 2f 0a 20 20 73 68 6f 72 74 20 69 6e  se */.  short in
29ffa 74 20 69 50 68 72 61 73 65 3b 20 2f 2a 20 54 68  t iPhrase; /* Th
29ffb 69 73 20 69 73 20 74 68 65 20 69 2d 74 68 20 74  is is the i-th t
29ffc 65 72 6d 20 6f 66 20 61 20 70 68 72 61 73 65 2e  erm of a phrase.
29ffd 20 2a 2f 0a 20 20 73 68 6f 72 74 20 69 6e 74 20   */.  short int 
29ffe 69 43 6f 6c 75 6d 6e 3b 20 2f 2a 20 43 6f 6c 75  iColumn; /* Colu
29fff 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  mn of the index 
2a000 74 68 61 74 20 6d 75 73 74 20 6d 61 74 63 68 20  that must match 
2a001 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a 20 20 73  this term */.  s
2a002 69 67 6e 65 64 20 63 68 61 72 20 6e 4e 65 61 72  igned char nNear
2a003 3b 20 2f 2a 20 74 65 72 6d 20 66 6f 6c 6c 6f 77  ; /* term follow
2a004 65 64 20 62 79 20 61 20 4e 45 41 52 20 6f 70 65  ed by a NEAR ope
2a005 72 61 74 6f 72 20 77 69 74 68 20 73 70 61 6e 3d  rator with span=
2a006 28 6e 4e 65 61 72 2d 31 29 20 2a 2f 0a 20 20 73  (nNear-1) */.  s
2a007 69 67 6e 65 64 20 63 68 61 72 20 69 73 4f 72 3b  igned char isOr;
2a008 20 20 2f 2a 20 74 68 69 73 20 74 65 72 6d 20 69    /* this term i
2a009 73 20 70 72 65 63 65 64 65 64 20 62 79 20 22 4f  s preceded by "O
2a00a 52 22 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63  R" */.  signed c
2a00b 68 61 72 20 69 73 4e 6f 74 3b 20 2f 2a 20 74 68  har isNot; /* th
2a00c 69 73 20 74 65 72 6d 20 69 73 20 70 72 65 63 65  is term is prece
2a00d 64 65 64 20 62 79 20 22 2d 22 20 2a 2f 0a 20 20  ded by "-" */.  
2a00e 73 69 67 6e 65 64 20 63 68 61 72 20 69 73 50 72  signed char isPr
2a00f 65 66 69 78 3b 20 2f 2a 20 74 68 69 73 20 74 65  efix; /* this te
2a010 72 6d 20 69 73 20 66 6f 6c 6c 6f 77 65 64 20 62  rm is followed b
2a011 79 20 22 2a 22 20 2a 2f 0a 20 20 63 68 61 72 20  y "*" */.  char 
2a012 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20 2f 2a  *pTerm;       /*
2a013 20 74 65 78 74 20 6f 66 20 74 68 65 20 74 65 72   text of the ter
2a014 6d 2e 20 20 27 5c 30 30 30 27 20 74 65 72 6d 69  m.  '\000' termi
2a015 6e 61 74 65 64 2e 20 20 6d 61 6c 6c 6f 63 65 64  nated.  malloced
2a016 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b   */.  int nTerm;
2a017 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
2a018 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 70  er of bytes in p
2a019 54 65 72 6d 5b 5d 20 2a 2f 0a 7d 20 51 75 65 72  Term[] */.} Quer
2a01a 79 54 65 72 6d 3b 0a 0a 0a 2f 2a 20 41 20 71 75  yTerm;.../* A qu
2a01b 65 72 79 20 73 74 72 69 6e 67 20 69 73 20 70 61  ery string is pa
2a01c 72 73 65 64 20 69 6e 74 6f 20 61 20 51 75 65 72  rsed into a Quer
2a01d 79 20 73 74 72 75 63 74 75 72 65 2e 0a 20 2a 0a  y structure.. *.
2a01e 20 2a 20 57 65 20 63 6f 75 6c 64 2c 20 69 6e 20   * We could, in 
2a01f 74 68 65 6f 72 79 2c 20 61 6c 6c 6f 77 20 71 75  theory, allow qu
2a020 65 72 79 20 73 74 72 69 6e 67 73 20 74 6f 20 62  ery strings to b
2a021 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 0a 20 2a  e complicated. *
2a022 20 6e 65 73 74 65 64 20 65 78 70 72 65 73 73 69   nested expressi
2a023 6f 6e 73 20 77 69 74 68 20 70 72 65 63 65 64 65  ons with precede
2a024 6e 63 65 20 64 65 74 65 72 6d 69 6e 65 64 20 62  nce determined b
2a025 79 20 70 61 72 65 6e 74 68 65 73 65 73 2e 0a 20  y parentheses.. 
2a026 2a 20 42 75 74 20 6e 6f 6e 65 20 6f 66 20 74 68  * But none of th
2a027 65 20 6d 61 6a 6f 72 20 73 65 61 72 63 68 20 65  e major search e
2a028 6e 67 69 6e 65 73 20 64 6f 20 74 68 69 73 2e 20  ngines do this. 
2a029 20 28 50 65 72 68 61 70 73 20 74 68 65 0a 20 2a   (Perhaps the. *
2a02a 20 66 65 65 6c 69 6e 67 20 69 73 20 74 68 61 74   feeling is that
2a02b 20 61 6e 20 70 61 72 65 6e 74 68 65 73 69 7a 65   an parenthesize
2a02c 64 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  d expression is 
2a02d 74 77 6f 20 63 6f 6d 70 6c 65 78 20 6f 66 0a 20  two complex of. 
2a02e 2a 20 61 6e 20 69 64 65 61 20 66 6f 72 20 74 68  * an idea for th
2a02f 65 20 61 76 65 72 61 67 65 20 75 73 65 72 20 74  e average user t
2a030 6f 20 67 72 61 73 70 2e 29 20 20 54 61 6b 69 6e  o grasp.)  Takin
2a031 67 20 6f 75 72 20 6c 65 61 64 20 66 72 6f 6d 0a  g our lead from.
2a032 20 2a 20 74 68 65 20 6d 61 6a 6f 72 20 73 65 61   * the major sea
2a033 72 63 68 20 65 6e 67 69 6e 65 73 2c 20 77 65 20  rch engines, we 
2a034 77 69 6c 6c 20 61 6c 6c 6f 77 20 71 75 65 72 69  will allow queri
2a035 65 73 20 74 6f 20 62 65 20 61 20 6c 69 73 74 0a  es to be a list.
2a036 20 2a 20 6f 66 20 74 65 72 6d 73 20 28 77 69 74   * of terms (wit
2a037 68 20 61 6e 20 69 6d 70 6c 69 65 64 20 41 4e 44  h an implied AND
2a038 20 6f 70 65 72 61 74 6f 72 29 20 6f 72 20 70 68   operator) or ph
2a039 72 61 73 65 73 20 69 6e 20 64 6f 75 62 6c 65 2d  rases in double-
2a03a 71 75 6f 74 65 73 2c 0a 20 2a 20 77 69 74 68 20  quotes,. * with 
2a03b 61 20 73 69 6e 67 6c 65 20 6f 70 74 69 6f 6e 61  a single optiona
2a03c 6c 20 22 2d 22 20 62 65 66 6f 72 65 20 65 61 63  l "-" before eac
2a03d 68 20 6e 6f 6e 2d 70 68 72 61 73 65 20 74 65 72  h non-phrase ter
2a03e 6d 20 74 6f 20 64 65 73 69 67 6e 61 74 65 0a 20  m to designate. 
2a03f 2a 20 6e 65 67 61 74 69 6f 6e 20 61 6e 64 20 61  * negation and a
2a040 6e 20 6f 70 74 69 6f 6e 61 6c 20 4f 52 20 63 6f  n optional OR co
2a041 6e 6e 65 63 74 6f 72 2e 0a 20 2a 0a 20 2a 20 4f  nnector.. *. * O
2a042 52 20 62 69 6e 64 73 20 6d 6f 72 65 20 74 69 67  R binds more tig
2a043 68 74 6c 79 20 74 68 61 6e 20 74 68 65 20 69 6d  htly than the im
2a044 70 6c 69 65 64 20 41 4e 44 2c 20 77 68 69 63 68  plied AND, which
2a045 20 69 73 20 77 68 61 74 20 74 68 65 0a 20 2a 20   is what the. * 
2a046 6d 61 6a 6f 72 20 73 65 61 72 63 68 20 65 6e 67  major search eng
2a047 69 6e 65 73 20 73 65 65 6d 20 74 6f 20 64 6f 2e  ines seem to do.
2a048 20 20 53 6f 2c 20 66 6f 72 20 65 78 61 6d 70 6c    So, for exampl
2a049 65 3a 0a 20 2a 20 0a 20 2a 20 20 20 20 5b 6f 6e  e:. * . *    [on
2a04a 65 20 74 77 6f 20 4f 52 20 74 68 72 65 65 5d 20  e two OR three] 
2a04b 20 20 20 20 3d 3d 3e 20 20 20 20 6f 6e 65 20 41      ==>    one A
2a04c 4e 44 20 28 74 77 6f 20 4f 52 20 74 68 72 65 65  ND (two OR three
2a04d 29 0a 20 2a 20 20 20 20 5b 6f 6e 65 20 4f 52 20  ). *    [one OR 
2a04e 74 77 6f 20 74 68 72 65 65 5d 20 20 20 20 20 3d  two three]     =
2a04f 3d 3e 20 20 20 20 28 6f 6e 65 20 4f 52 20 74 77  =>    (one OR tw
2a050 6f 29 20 41 4e 44 20 74 68 72 65 65 0a 20 2a 0a  o) AND three. *.
2a051 20 2a 20 41 20 22 2d 22 20 62 65 66 6f 72 65 20   * A "-" before 
2a052 61 20 74 65 72 6d 20 6d 61 74 63 68 65 73 20 61  a term matches a
2a053 6c 6c 20 65 6e 74 72 69 65 73 20 74 68 61 74 20  ll entries that 
2a054 6c 61 63 6b 20 74 68 61 74 20 74 65 72 6d 2e 0a  lack that term..
2a055 20 2a 20 54 68 65 20 22 2d 22 20 6d 75 73 74 20   * The "-" must 
2a056 6f 63 63 75 72 20 69 6d 6d 65 64 69 61 74 65 6c  occur immediatel
2a057 79 20 62 65 66 6f 72 65 20 74 68 65 20 74 65 72  y before the ter
2a058 6d 20 77 69 74 68 20 69 6e 20 69 6e 74 65 72 76  m with in interv
2a059 65 6e 69 6e 67 0a 20 2a 20 73 70 61 63 65 2e 20  ening. * space. 
2a05a 20 54 68 69 73 20 69 73 20 68 6f 77 20 74 68 65   This is how the
2a05b 20 73 65 61 72 63 68 20 65 6e 67 69 6e 65 73 20   search engines 
2a05c 64 6f 20 69 74 2e 0a 20 2a 0a 20 2a 20 41 20 4e  do it.. *. * A N
2a05d 4f 54 20 74 65 72 6d 20 63 61 6e 6e 6f 74 20 62  OT term cannot b
2a05e 65 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64  e the right-hand
2a05f 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 6e 20 4f   operand of an O
2a060 52 2e 20 20 49 66 20 74 68 69 73 0a 20 2a 20 6f  R.  If this. * o
2a061 63 63 75 72 73 20 69 6e 20 74 68 65 20 71 75 65  ccurs in the que
2a062 72 79 20 73 74 72 69 6e 67 2c 20 74 68 65 20 4e  ry string, the N
2a063 4f 54 20 69 73 20 69 67 6e 6f 72 65 64 3a 0a 20  OT is ignored:. 
2a064 2a 0a 20 2a 20 20 20 20 5b 6f 6e 65 20 4f 52 20  *. *    [one OR 
2a065 2d 74 77 6f 5d 20 20 20 20 20 20 20 20 20 20 3d  -two]          =
2a066 3d 3e 20 20 20 20 6f 6e 65 20 4f 52 20 74 77 6f  =>    one OR two
2a067 0a 20 2a 0a 20 2a 2f 0a 74 79 70 65 64 65 66 20  . *. */.typedef 
2a068 73 74 72 75 63 74 20 51 75 65 72 79 20 7b 0a 20  struct Query {. 
2a069 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a   fulltext_vtab *
2a06a 70 46 74 73 3b 20 20 2f 2a 20 54 68 65 20 66 75  pFts;  /* The fu
2a06b 6c 6c 20 74 65 78 74 20 69 6e 64 65 78 20 2a 2f  ll text index */
2a06c 0a 20 20 69 6e 74 20 6e 54 65 72 6d 73 3b 20 20  .  int nTerms;  
2a06d 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
2a06e 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 74  er of terms in t
2a06f 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 51 75  he query */.  Qu
2a070 65 72 79 54 65 72 6d 20 2a 70 54 65 72 6d 73 3b  eryTerm *pTerms;
2a071 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20      /* Array of 
2a072 74 65 72 6d 73 2e 20 20 53 70 61 63 65 20 6f 62  terms.  Space ob
2a073 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c  tained from mall
2a074 6f 63 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 65  oc() */.  int ne
2a075 78 74 49 73 4f 72 3b 20 20 20 20 20 20 20 20 20  xtIsOr;         
2a076 2f 2a 20 53 65 74 20 74 68 65 20 69 73 4f 72 20  /* Set the isOr 
2a077 66 6c 61 67 20 6f 6e 20 74 68 65 20 6e 65 78 74  flag on the next
2a078 20 69 6e 73 65 72 74 65 64 20 74 65 72 6d 20 2a   inserted term *
2a079 2f 0a 20 20 69 6e 74 20 6e 65 78 74 49 73 4e 65  /.  int nextIsNe
2a07a 61 72 3b 20 20 20 20 20 20 20 2f 2a 20 53 65 74  ar;       /* Set
2a07b 20 74 68 65 20 69 73 4f 72 20 66 6c 61 67 20 6f   the isOr flag o
2a07c 6e 20 74 68 65 20 6e 65 78 74 20 69 6e 73 65 72  n the next inser
2a07d 74 65 64 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e  ted term */.  in
2a07e 74 20 6e 65 78 74 43 6f 6c 75 6d 6e 3b 20 20 20  t nextColumn;   
2a07f 20 20 20 20 2f 2a 20 4e 65 78 74 20 77 6f 72 64      /* Next word
2a080 20 70 61 72 73 65 64 20 6d 75 73 74 20 62 65 20   parsed must be 
2a081 69 6e 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a  in this column *
2a082 2f 0a 20 20 69 6e 74 20 64 66 6c 74 43 6f 6c 75  /.  int dfltColu
2a083 6d 6e 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  mn;       /* The
2a084 20 64 65 66 61 75 6c 74 20 63 6f 6c 75 6d 6e 20   default column 
2a085 2a 2f 0a 7d 20 51 75 65 72 79 3b 0a 0a 0a 2f 2a  */.} Query;.../*
2a086 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
2a087 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
2a088 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 73   structure keeps
2a089 20 74 72 61 63 6b 20 6f 66 20 67 65 6e 65 72 61   track of genera
2a08a 74 65 64 0a 2a 2a 20 6d 61 74 63 68 69 6e 67 2d  ted.** matching-
2a08b 77 6f 72 64 20 6f 66 66 73 65 74 20 69 6e 66 6f  word offset info
2a08c 72 6d 61 74 69 6f 6e 20 61 6e 64 20 73 6e 69 70  rmation and snip
2a08d 70 65 74 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  pets..*/.typedef
2a08e 20 73 74 72 75 63 74 20 53 6e 69 70 70 65 74 20   struct Snippet 
2a08f 7b 0a 20 20 69 6e 74 20 6e 4d 61 74 63 68 3b 20  {.  int nMatch; 
2a090 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d      /* Total num
2a091 62 65 72 20 6f 66 20 6d 61 74 63 68 65 73 20 2a  ber of matches *
2a092 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20  /.  int nAlloc; 
2a093 20 20 20 20 2f 2a 20 53 70 61 63 65 20 61 6c 6c      /* Space all
2a094 6f 63 61 74 65 64 20 66 6f 72 20 61 4d 61 74 63  ocated for aMatc
2a095 68 5b 5d 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  h[] */.  struct 
2a096 73 6e 69 70 70 65 74 4d 61 74 63 68 20 7b 20 2f  snippetMatch { /
2a097 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20  * One entry for 
2a098 65 61 63 68 20 6d 61 74 63 68 69 6e 67 20 74 65  each matching te
2a099 72 6d 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 73  rm */.    char s
2a09a 6e 53 74 61 74 75 73 3b 20 20 20 20 20 20 20 2f  nStatus;       /
2a09b 2a 20 53 74 61 74 75 73 20 66 6c 61 67 20 66 6f  * Status flag fo
2a09c 72 20 75 73 65 20 77 68 69 6c 65 20 63 6f 6e 73  r use while cons
2a09d 74 72 75 63 74 69 6e 67 20 73 6e 69 70 70 65 74  tructing snippet
2a09e 73 20 2a 2f 0a 20 20 20 20 73 68 6f 72 74 20 69  s */.    short i
2a09f 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 2f 2a  nt iCol;      /*
2a0a0 20 54 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74   The column that
2a0a1 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6d 61   contains the ma
2a0a2 74 63 68 20 2a 2f 0a 20 20 20 20 73 68 6f 72 74  tch */.    short
2a0a3 20 69 6e 74 20 69 54 65 72 6d 3b 20 20 20 20 20   int iTerm;     
2a0a4 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 69 6e 20  /* The index in 
2a0a5 51 75 65 72 79 2e 70 54 65 72 6d 73 5b 5d 20 6f  Query.pTerms[] o
2a0a6 66 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 74  f the matching t
2a0a7 65 72 6d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  erm */.    int i
2a0a8 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20  Token;          
2a0a9 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20  /* The index of 
2a0aa 74 68 65 20 6d 61 74 63 68 69 6e 67 20 64 6f 63  the matching doc
2a0ab 75 6d 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20  ument token */. 
2a0ac 20 20 20 73 68 6f 72 74 20 69 6e 74 20 6e 42 79     short int nBy
2a0ad 74 65 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  te;     /* Numbe
2a0ae 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
2a0af 65 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 69 6e  e term */.    in
2a0b0 74 20 69 53 74 61 72 74 3b 20 20 20 20 20 20 20  t iStart;       
2a0b1 20 20 20 2f 2a 20 54 68 65 20 6f 66 66 73 65 74     /* The offset
2a0b2 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 63 68   to the first ch
2a0b3 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 20 74  aracter of the t
2a0b4 65 72 6d 20 2a 2f 0a 20 20 7d 20 2a 61 4d 61 74  erm */.  } *aMat
2a0b5 63 68 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  ch;      /* Poin
2a0b6 74 73 20 74 6f 20 73 70 61 63 65 20 6f 62 74 61  ts to space obta
2a0b7 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63  ined from malloc
2a0b8 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4f 66 66   */.  char *zOff
2a0b9 73 65 74 3b 20 20 2f 2a 20 54 65 78 74 20 72 65  set;  /* Text re
2a0ba 6e 64 65 72 69 6e 67 20 6f 66 20 61 4d 61 74 63  ndering of aMatc
2a0bb 68 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 66  h[] */.  int nOf
2a0bc 66 73 65 74 3b 20 20 20 20 2f 2a 20 73 74 72 6c  fset;    /* strl
2a0bd 65 6e 28 7a 4f 66 66 73 65 74 29 20 2a 2f 0a 20  en(zOffset) */. 
2a0be 20 63 68 61 72 20 2a 7a 53 6e 69 70 70 65 74 3b   char *zSnippet;
2a0bf 20 2f 2a 20 53 6e 69 70 70 65 74 20 74 65 78 74   /* Snippet text
2a0c0 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 6e 69 70 70   */.  int nSnipp
2a0c1 65 74 3b 20 20 20 2f 2a 20 73 74 72 6c 65 6e 28  et;   /* strlen(
2a0c2 7a 53 6e 69 70 70 65 74 29 20 2a 2f 0a 7d 20 53  zSnippet) */.} S
2a0c3 6e 69 70 70 65 74 3b 0a 0a 0a 74 79 70 65 64 65  nippet;...typede
2a0c4 66 20 65 6e 75 6d 20 51 75 65 72 79 54 79 70 65  f enum QueryType
2a0c5 20 7b 0a 20 20 51 55 45 52 59 5f 47 45 4e 45 52   {.  QUERY_GENER
2a0c6 49 43 2c 20 20 20 2f 2a 20 74 61 62 6c 65 20 73  IC,   /* table s
2a0c7 63 61 6e 20 2a 2f 0a 20 20 51 55 45 52 59 5f 44  can */.  QUERY_D
2a0c8 4f 43 49 44 2c 20 20 20 20 20 2f 2a 20 6c 6f 6f  OCID,     /* loo
2a0c9 6b 75 70 20 62 79 20 64 6f 63 69 64 20 2a 2f 0a  kup by docid */.
2a0ca 20 20 51 55 45 52 59 5f 46 55 4c 4c 54 45 58 54    QUERY_FULLTEXT
2a0cb 20 20 20 2f 2a 20 51 55 45 52 59 5f 46 55 4c 4c     /* QUERY_FULL
2a0cc 54 45 58 54 20 2b 20 5b 69 5d 20 69 73 20 61 20  TEXT + [i] is a 
2a0cd 66 75 6c 6c 2d 74 65 78 74 20 73 65 61 72 63 68  full-text search
2a0ce 20 66 6f 72 20 63 6f 6c 75 6d 6e 20 69 2a 2f 0a   for column i*/.
2a0cf 7d 20 51 75 65 72 79 54 79 70 65 3b 0a 0a 74 79  } QueryType;..ty
2a0d0 70 65 64 65 66 20 65 6e 75 6d 20 66 75 6c 6c 74  pedef enum fullt
2a0d1 65 78 74 5f 73 74 61 74 65 6d 65 6e 74 20 7b 0a  ext_statement {.
2a0d2 20 20 43 4f 4e 54 45 4e 54 5f 49 4e 53 45 52 54    CONTENT_INSERT
2a0d3 5f 53 54 4d 54 2c 0a 20 20 43 4f 4e 54 45 4e 54  _STMT,.  CONTENT
2a0d4 5f 53 45 4c 45 43 54 5f 53 54 4d 54 2c 0a 20 20  _SELECT_STMT,.  
2a0d5 43 4f 4e 54 45 4e 54 5f 55 50 44 41 54 45 5f 53  CONTENT_UPDATE_S
2a0d6 54 4d 54 2c 0a 20 20 43 4f 4e 54 45 4e 54 5f 44  TMT,.  CONTENT_D
2a0d7 45 4c 45 54 45 5f 53 54 4d 54 2c 0a 0a 20 20 42  ELETE_STMT,..  B
2a0d8 4c 4f 43 4b 5f 49 4e 53 45 52 54 5f 53 54 4d 54  LOCK_INSERT_STMT
2a0d9 2c 0a 20 20 42 4c 4f 43 4b 5f 53 45 4c 45 43 54  ,.  BLOCK_SELECT
2a0da 5f 53 54 4d 54 2c 0a 20 20 42 4c 4f 43 4b 5f 44  _STMT,.  BLOCK_D
2a0db 45 4c 45 54 45 5f 53 54 4d 54 2c 0a 0a 20 20 53  ELETE_STMT,..  S
2a0dc 45 47 44 49 52 5f 4d 41 58 5f 49 4e 44 45 58 5f  EGDIR_MAX_INDEX_
2a0dd 53 54 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f 53  STMT,.  SEGDIR_S
2a0de 45 54 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44 49  ET_STMT,.  SEGDI
2a0df 52 5f 53 45 4c 45 43 54 5f 53 54 4d 54 2c 0a 20  R_SELECT_STMT,. 
2a0e0 20 53 45 47 44 49 52 5f 53 50 41 4e 5f 53 54 4d   SEGDIR_SPAN_STM
2a0e1 54 2c 0a 20 20 53 45 47 44 49 52 5f 44 45 4c 45  T,.  SEGDIR_DELE
2a0e2 54 45 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44 49  TE_STMT,.  SEGDI
2a0e3 52 5f 53 45 4c 45 43 54 5f 41 4c 4c 5f 53 54 4d  R_SELECT_ALL_STM
2a0e4 54 2c 0a 0a 20 20 4d 41 58 5f 53 54 4d 54 20 20  T,..  MAX_STMT  
2a0e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a0e6 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 61 74 20     /* Always at 
2a0e7 65 6e 64 21 20 2a 2f 0a 7d 20 66 75 6c 6c 74 65  end! */.} fullte
2a0e8 78 74 5f 73 74 61 74 65 6d 65 6e 74 3b 0a 0a 2f  xt_statement;../
2a0e9 2a 20 54 68 65 73 65 20 6d 75 73 74 20 65 78 61  * These must exa
2a0ea 63 74 6c 79 20 6d 61 74 63 68 20 74 68 65 20 65  ctly match the e
2a0eb 6e 75 6d 20 61 62 6f 76 65 2e 20 2a 2f 0a 2f 2a  num above. */./*
2a0ec 20 54 4f 44 4f 28 73 68 65 73 73 29 3a 20 49 73   TODO(shess): Is
2a0ed 20 74 68 65 72 65 20 73 6f 6d 65 20 72 69 73 6b   there some risk
2a0ee 20 74 68 61 74 20 61 20 73 74 61 74 65 6d 65 6e   that a statemen
2a0ef 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 69  t will be used i
2a0f0 6e 20 74 77 6f 0a 2a 2a 20 63 75 72 73 6f 72 73  n two.** cursors
2a0f1 20 61 74 20 6f 6e 63 65 2c 20 65 2e 67 2e 20 20   at once, e.g.  
2a0f2 69 66 20 61 20 71 75 65 72 79 20 6a 6f 69 6e 73  if a query joins
2a0f3 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
2a0f4 20 74 6f 20 69 74 73 65 6c 66 3f 0a 2a 2a 20 49   to itself?.** I
2a0f5 66 20 73 6f 20 70 65 72 68 61 70 73 20 77 65 20  f so perhaps we 
2a0f6 73 68 6f 75 6c 64 20 6d 6f 76 65 20 73 6f 6d 65  should move some
2a0f7 20 6f 66 20 74 68 65 73 65 20 74 6f 20 74 68 65   of these to the
2a0f8 20 63 75 72 73 6f 72 20 6f 62 6a 65 63 74 2e 0a   cursor object..
2a0f9 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
2a0fa 63 68 61 72 20 2a 63 6f 6e 73 74 20 66 75 6c 6c  char *const full
2a0fb 74 65 78 74 5f 7a 53 74 61 74 65 6d 65 6e 74 5b  text_zStatement[
2a0fc 4d 41 58 5f 53 54 4d 54 5d 20 3d 20 7b 0a 20 20  MAX_STMT] = {.  
2a0fd 2f 2a 20 43 4f 4e 54 45 4e 54 5f 49 4e 53 45 52  /* CONTENT_INSER
2a0fe 54 20 2a 2f 20 4e 55 4c 4c 2c 20 20 2f 2a 20 67  T */ NULL,  /* g
2a0ff 65 6e 65 72 61 74 65 64 20 69 6e 20 63 6f 6e 74  enerated in cont
2a100 65 6e 74 49 6e 73 65 72 74 53 74 61 74 65 6d 65  entInsertStateme
2a101 6e 74 28 29 20 2a 2f 0a 20 20 2f 2a 20 43 4f 4e  nt() */.  /* CON
2a102 54 45 4e 54 5f 53 45 4c 45 43 54 20 2a 2f 20 4e  TENT_SELECT */ N
2a103 55 4c 4c 2c 20 20 2f 2a 20 67 65 6e 65 72 61 74  ULL,  /* generat
2a104 65 64 20 69 6e 20 63 6f 6e 74 65 6e 74 53 65 6c  ed in contentSel
2a105 65 63 74 53 74 61 74 65 6d 65 6e 74 28 29 20 2a  ectStatement() *
2a106 2f 0a 20 20 2f 2a 20 43 4f 4e 54 45 4e 54 5f 55  /.  /* CONTENT_U
2a107 50 44 41 54 45 20 2a 2f 20 4e 55 4c 4c 2c 20 20  PDATE */ NULL,  
2a108 2f 2a 20 67 65 6e 65 72 61 74 65 64 20 69 6e 20  /* generated in 
2a109 63 6f 6e 74 65 6e 74 55 70 64 61 74 65 53 74 61  contentUpdateSta
2a10a 74 65 6d 65 6e 74 28 29 20 2a 2f 0a 20 20 2f 2a  tement() */.  /*
2a10b 20 43 4f 4e 54 45 4e 54 5f 44 45 4c 45 54 45 20   CONTENT_DELETE 
2a10c 2a 2f 20 22 64 65 6c 65 74 65 20 66 72 6f 6d 20  */ "delete from 
2a10d 25 5f 63 6f 6e 74 65 6e 74 20 77 68 65 72 65 20  %_content where 
2a10e 64 6f 63 69 64 20 3d 20 3f 22 2c 0a 0a 20 20 2f  docid = ?",..  /
2a10f 2a 20 42 4c 4f 43 4b 5f 49 4e 53 45 52 54 20 2a  * BLOCK_INSERT *
2a110 2f 0a 20 20 22 69 6e 73 65 72 74 20 69 6e 74 6f  /.  "insert into
2a111 20 25 5f 73 65 67 6d 65 6e 74 73 20 28 62 6c 6f   %_segments (blo
2a112 63 6b 69 64 2c 20 62 6c 6f 63 6b 29 20 76 61 6c  ckid, block) val
2a113 75 65 73 20 28 6e 75 6c 6c 2c 20 3f 29 22 2c 0a  ues (null, ?)",.
2a114 20 20 2f 2a 20 42 4c 4f 43 4b 5f 53 45 4c 45 43    /* BLOCK_SELEC
2a115 54 20 2a 2f 20 22 73 65 6c 65 63 74 20 62 6c 6f  T */ "select blo
2a116 63 6b 20 66 72 6f 6d 20 25 5f 73 65 67 6d 65 6e  ck from %_segmen
2a117 74 73 20 77 68 65 72 65 20 62 6c 6f 63 6b 69 64  ts where blockid
2a118 20 3d 20 3f 22 2c 0a 20 20 2f 2a 20 42 4c 4f 43   = ?",.  /* BLOC
2a119 4b 5f 44 45 4c 45 54 45 20 2a 2f 20 22 64 65 6c  K_DELETE */ "del
2a11a 65 74 65 20 66 72 6f 6d 20 25 5f 73 65 67 6d 65  ete from %_segme
2a11b 6e 74 73 20 77 68 65 72 65 20 62 6c 6f 63 6b 69  nts where blocki
2a11c 64 20 62 65 74 77 65 65 6e 20 3f 20 61 6e 64 20  d between ? and 
2a11d 3f 22 2c 0a 0a 20 20 2f 2a 20 53 45 47 44 49 52  ?",..  /* SEGDIR
2a11e 5f 4d 41 58 5f 49 4e 44 45 58 20 2a 2f 20 22 73  _MAX_INDEX */ "s
2a11f 65 6c 65 63 74 20 6d 61 78 28 69 64 78 29 20 66  elect max(idx) f
2a120 72 6f 6d 20 25 5f 73 65 67 64 69 72 20 77 68 65  rom %_segdir whe
2a121 72 65 20 6c 65 76 65 6c 20 3d 20 3f 22 2c 0a 20  re level = ?",. 
2a122 20 2f 2a 20 53 45 47 44 49 52 5f 53 45 54 20 2a   /* SEGDIR_SET *
2a123 2f 20 22 69 6e 73 65 72 74 20 69 6e 74 6f 20 25  / "insert into %
2a124 5f 73 65 67 64 69 72 20 76 61 6c 75 65 73 20 28  _segdir values (
2a125 3f 2c 20 3f 2c 20 3f 2c 20 3f 2c 20 3f 2c 20 3f  ?, ?, ?, ?, ?, ?
2a126 29 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f  )",.  /* SEGDIR_
2a127 53 45 4c 45 43 54 20 2a 2f 0a 20 20 22 73 65 6c  SELECT */.  "sel
2a128 65 63 74 20 73 74 61 72 74 5f 62 6c 6f 63 6b 2c  ect start_block,
2a129 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63   leaves_end_bloc
2a12a 6b 2c 20 72 6f 6f 74 20 66 72 6f 6d 20 25 5f 73  k, root from %_s
2a12b 65 67 64 69 72 20 22 0a 20 20 22 20 77 68 65 72  egdir ".  " wher
2a12c 65 20 6c 65 76 65 6c 20 3d 20 3f 20 6f 72 64 65  e level = ? orde
2a12d 72 20 62 79 20 69 64 78 22 2c 0a 20 20 2f 2a 20  r by idx",.  /* 
2a12e 53 45 47 44 49 52 5f 53 50 41 4e 20 2a 2f 0a 20  SEGDIR_SPAN */. 
2a12f 20 22 73 65 6c 65 63 74 20 6d 69 6e 28 73 74 61   "select min(sta
2a130 72 74 5f 62 6c 6f 63 6b 29 2c 20 6d 61 78 28 65  rt_block), max(e
2a131 6e 64 5f 62 6c 6f 63 6b 29 20 66 72 6f 6d 20 25  nd_block) from %
2a132 5f 73 65 67 64 69 72 20 22 0a 20 20 22 20 77 68  _segdir ".  " wh
2a133 65 72 65 20 6c 65 76 65 6c 20 3d 20 3f 20 61 6e  ere level = ? an
2a134 64 20 73 74 61 72 74 5f 62 6c 6f 63 6b 20 3c 3e  d start_block <>
2a135 20 30 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52   0",.  /* SEGDIR
2a136 5f 44 45 4c 45 54 45 20 2a 2f 20 22 64 65 6c 65  _DELETE */ "dele
2a137 74 65 20 66 72 6f 6d 20 25 5f 73 65 67 64 69 72  te from %_segdir
2a138 20 77 68 65 72 65 20 6c 65 76 65 6c 20 3d 20 3f   where level = ?
2a139 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f 53  ",.  /* SEGDIR_S
2a13a 45 4c 45 43 54 5f 41 4c 4c 20 2a 2f 0a 20 20 22  ELECT_ALL */.  "
2a13b 73 65 6c 65 63 74 20 72 6f 6f 74 2c 20 6c 65 61  select root, lea
2a13c 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 20 66 72  ves_end_block fr
2a13d 6f 6d 20 25 5f 73 65 67 64 69 72 20 6f 72 64 65  om %_segdir orde
2a13e 72 20 62 79 20 6c 65 76 65 6c 20 64 65 73 63 2c  r by level desc,
2a13f 20 69 64 78 22 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a   idx",.};../*.**
2a140 20 41 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f   A connection to
2a141 20 61 20 66 75 6c 6c 74 65 78 74 20 69 6e 64 65   a fulltext inde
2a142 78 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  x is an instance
2a143 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
2a144 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20  g.** structure. 
2a145 20 54 68 65 20 78 43 72 65 61 74 65 20 61 6e 64   The xCreate and
2a146 20 78 43 6f 6e 6e 65 63 74 20 6d 65 74 68 6f 64   xConnect method
2a147 73 20 63 72 65 61 74 65 20 61 6e 20 69 6e 73 74  s create an inst
2a148 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 69 73 20  ance.** of this 
2a149 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 78 44  structure and xD
2a14a 65 73 74 72 6f 79 20 61 6e 64 20 78 44 69 73 63  estroy and xDisc
2a14b 6f 6e 6e 65 63 74 20 66 72 65 65 20 74 68 61 74  onnect free that
2a14c 20 69 6e 73 74 61 6e 63 65 2e 0a 2a 2a 20 41 6c   instance..** Al
2a14d 6c 20 6f 74 68 65 72 20 6d 65 74 68 6f 64 73 20  l other methods 
2a14e 72 65 63 65 69 76 65 20 61 20 70 6f 69 6e 74 65  receive a pointe
2a14f 72 20 74 6f 20 74 68 65 20 73 74 72 75 63 74 75  r to the structu
2a150 72 65 20 61 73 20 6f 6e 65 20 6f 66 20 74 68 65  re as one of the
2a151 69 72 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 2e  ir.** arguments.
2a152 0a 2a 2f 0a 73 74 72 75 63 74 20 66 75 6c 6c 74  .*/.struct fullt
2a153 65 78 74 5f 76 74 61 62 20 7b 0a 20 20 73 71 6c  ext_vtab {.  sql
2a154 69 74 65 33 5f 76 74 61 62 20 62 61 73 65 3b 20  ite3_vtab base; 
2a155 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a156 20 42 61 73 65 20 63 6c 61 73 73 20 75 73 65 64   Base class used
2a157 20 62 79 20 53 51 4c 69 74 65 20 63 6f 72 65 20   by SQLite core 
2a158 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
2a159 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2a15a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
2a15b 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2a15c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
2a15d 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 20 20 20   *zDb;          
2a15e 20 20 20 20 20 20 20 2f 2a 20 6c 6f 67 69 63 61         /* logica
2a15f 6c 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  l database name 
2a160 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
2a161 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  *zName;         
2a162 20 20 20 20 20 20 2f 2a 20 76 69 72 74 75 61 6c        /* virtual
2a163 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20   table name */. 
2a164 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20   int nColumn;   
2a165 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a166 20 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63    /* number of c
2a167 6f 6c 75 6d 6e 73 20 69 6e 20 76 69 72 74 75 61  olumns in virtua
2a168 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61  l table */.  cha
2a169 72 20 2a 2a 61 7a 43 6f 6c 75 6d 6e 3b 20 20 20  r **azColumn;   
2a16a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a16b 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20 20   column names.  
2a16c 6d 61 6c 6c 6f 63 65 64 20 2a 2f 0a 20 20 63 68  malloced */.  ch
2a16d 61 72 20 2a 2a 61 7a 43 6f 6e 74 65 6e 74 43 6f  ar **azContentCo
2a16e 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 2f  lumn;          /
2a16f 2a 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69  * column names i
2a170 6e 20 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 3b  n content table;
2a171 20 6d 61 6c 6c 6f 63 65 64 20 2a 2f 0a 20 20 73   malloced */.  s
2a172 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
2a173 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 3b 20 20 20   *pTokenizer;   
2a174 2f 2a 20 74 6f 6b 65 6e 69 7a 65 72 20 66 6f 72  /* tokenizer for
2a175 20 69 6e 73 65 72 74 73 20 61 6e 64 20 71 75 65   inserts and que
2a176 72 69 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 50 72  ries */..  /* Pr
2a177 65 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d  ecompiled statem
2a178 65 6e 74 73 20 77 68 69 63 68 20 77 65 20 6b 65  ents which we ke
2a179 65 70 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68  ep as long as th
2a17a 65 20 74 61 62 6c 65 20 69 73 0a 20 20 2a 2a 20  e table is.  ** 
2a17b 6f 70 65 6e 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  open..  */.  sql
2a17c 69 74 65 33 5f 73 74 6d 74 20 2a 70 46 75 6c 6c  ite3_stmt *pFull
2a17d 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73 5b 4d  textStatements[M
2a17e 41 58 5f 53 54 4d 54 5d 3b 0a 0a 20 20 2f 2a 20  AX_STMT];..  /* 
2a17f 50 72 65 63 6f 6d 70 69 6c 65 64 20 73 74 61 74  Precompiled stat
2a180 65 6d 65 6e 74 73 20 75 73 65 64 20 66 6f 72 20  ements used for 
2a181 73 65 67 6d 65 6e 74 20 6d 65 72 67 65 73 2e 20  segment merges. 
2a182 20 57 65 20 72 75 6e 20 61 0a 20 20 2a 2a 20 73   We run a.  ** s
2a183 65 70 61 72 61 74 65 20 73 65 6c 65 63 74 20 61  eparate select a
2a184 63 72 6f 73 73 20 74 68 65 20 6c 65 61 66 20 6c  cross the leaf l
2a185 65 76 65 6c 20 6f 66 20 65 61 63 68 20 74 72 65  evel of each tre
2a186 65 20 62 65 69 6e 67 20 6d 65 72 67 65 64 2e 0a  e being merged..
2a187 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73    */.  sqlite3_s
2a188 74 6d 74 20 2a 70 4c 65 61 66 53 65 6c 65 63 74  tmt *pLeafSelect
2a189 53 74 6d 74 73 5b 4d 45 52 47 45 5f 43 4f 55 4e  Stmts[MERGE_COUN
2a18a 54 5d 3b 0a 20 20 2f 2a 20 54 68 65 20 73 74 61  T];.  /* The sta
2a18b 74 65 6d 65 6e 74 20 75 73 65 64 20 74 6f 20 70  tement used to p
2a18c 72 65 70 61 72 65 20 70 4c 65 61 66 53 65 6c 65  repare pLeafSele
2a18d 63 74 53 74 6d 74 73 2e 20 2a 2f 0a 23 64 65 66  ctStmts. */.#def
2a18e 69 6e 65 20 4c 45 41 46 5f 53 45 4c 45 43 54 20  ine LEAF_SELECT 
2a18f 5c 0a 20 20 22 73 65 6c 65 63 74 20 62 6c 6f 63  \.  "select bloc
2a190 6b 20 66 72 6f 6d 20 25 5f 73 65 67 6d 65 6e 74  k from %_segment
2a191 73 20 77 68 65 72 65 20 62 6c 6f 63 6b 69 64 20  s where blockid 
2a192 62 65 74 77 65 65 6e 20 3f 20 61 6e 64 20 3f 20  between ? and ? 
2a193 6f 72 64 65 72 20 62 79 20 62 6c 6f 63 6b 69 64  order by blockid
2a194 22 0a 0a 20 20 2f 2a 20 54 68 65 73 65 20 62 75  "..  /* These bu
2a195 66 66 65 72 20 70 65 6e 64 69 6e 67 20 69 6e 64  ffer pending ind
2a196 65 78 20 75 70 64 61 74 65 73 20 64 75 72 69 6e  ex updates durin
2a197 67 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a  g transactions..
2a198 20 20 2a 2a 20 6e 50 65 6e 64 69 6e 67 44 61 74    ** nPendingDat
2a199 61 20 65 73 74 69 6d 61 74 65 73 20 74 68 65 20  a estimates the 
2a19a 6d 65 6d 6f 72 79 20 73 69 7a 65 20 6f 66 20 74  memory size of t
2a19b 68 65 20 70 65 6e 64 69 6e 67 20 64 61 74 61 2e  he pending data.
2a19c 20 20 49 74 0a 20 20 2a 2a 20 64 6f 65 73 6e 27    It.  ** doesn'
2a19d 74 20 69 6e 63 6c 75 64 65 20 74 68 65 20 68 61  t include the ha
2a19e 73 68 2d 62 75 63 6b 65 74 20 6f 76 65 72 68 65  sh-bucket overhe
2a19f 61 64 2c 20 6e 6f 72 20 61 6e 79 20 6d 61 6c 6c  ad, nor any mall
2a1a0 6f 63 0a 20 20 2a 2a 20 6f 76 65 72 68 65 61 64  oc.  ** overhead
2a1a1 2e 20 20 57 68 65 6e 20 6e 50 65 6e 64 69 6e 67  .  When nPending
2a1a2 44 61 74 61 20 65 78 63 65 65 64 73 20 6b 50 65  Data exceeds kPe
2a1a3 6e 64 69 6e 67 54 68 72 65 73 68 6f 6c 64 2c 20  ndingThreshold, 
2a1a4 74 68 65 0a 20 20 2a 2a 20 62 75 66 66 65 72 20  the.  ** buffer 
2a1a5 69 73 20 66 6c 75 73 68 65 64 20 65 76 65 6e 20  is flushed even 
2a1a6 62 65 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73  before the trans
2a1a7 61 63 74 69 6f 6e 20 63 6c 6f 73 65 73 2e 0a 20  action closes.. 
2a1a8 20 2a 2a 20 70 65 6e 64 69 6e 67 54 65 72 6d 73   ** pendingTerms
2a1a9 20 73 74 6f 72 65 73 20 74 68 65 20 64 61 74 61   stores the data
2a1aa 2c 20 61 6e 64 20 69 73 20 6f 6e 6c 79 20 76 61  , and is only va
2a1ab 6c 69 64 20 77 68 65 6e 20 6e 50 65 6e 64 69 6e  lid when nPendin
2a1ac 67 44 61 74 61 0a 20 20 2a 2a 20 69 73 20 3e 3d  gData.  ** is >=
2a1ad 30 20 28 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c  0 (nPendingData<
2a1ae 30 20 6d 65 61 6e 73 20 70 65 6e 64 69 6e 67 54  0 means pendingT
2a1af 65 72 6d 73 20 68 61 73 20 6e 6f 74 20 62 65 65  erms has not bee
2a1b0 6e 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a  n.  ** initializ
2a1b1 65 64 29 2e 20 20 69 50 72 65 76 44 6f 63 69 64  ed).  iPrevDocid
2a1b2 20 69 73 20 74 68 65 20 6c 61 73 74 20 64 6f 63   is the last doc
2a1b3 69 64 20 77 72 69 74 74 65 6e 2c 20 75 73 65 64  id written, used
2a1b4 20 74 6f 20 6d 61 6b 65 0a 20 20 2a 2a 20 63 65   to make.  ** ce
2a1b5 72 74 61 69 6e 20 77 65 27 72 65 20 69 6e 73 65  rtain we're inse
2a1b6 72 74 69 6e 67 20 69 6e 20 73 6f 72 74 65 64 20  rting in sorted 
2a1b7 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 6e  order..  */.  in
2a1b8 74 20 6e 50 65 6e 64 69 6e 67 44 61 74 61 3b 0a  t nPendingData;.
2a1b9 23 64 65 66 69 6e 65 20 6b 50 65 6e 64 69 6e 67  #define kPending
2a1ba 54 68 72 65 73 68 6f 6c 64 20 28 31 2a 31 30 32  Threshold (1*102
2a1bb 34 2a 31 30 32 34 29 0a 20 20 73 71 6c 69 74 65  4*1024).  sqlite
2a1bc 5f 69 6e 74 36 34 20 69 50 72 65 76 44 6f 63 69  _int64 iPrevDoci
2a1bd 64 3b 0a 20 20 66 74 73 33 48 61 73 68 20 70 65  d;.  fts3Hash pe
2a1be 6e 64 69 6e 67 54 65 72 6d 73 3b 0a 7d 3b 0a 0a  ndingTerms;.};..
2a1bf 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63  /*.** When the c
2a1c0 6f 72 65 20 77 61 6e 74 73 20 74 6f 20 64 6f 20  ore wants to do 
2a1c1 61 20 71 75 65 72 79 2c 20 69 74 20 63 72 65 61  a query, it crea
2a1c2 74 65 20 61 20 63 75 72 73 6f 72 20 75 73 69 6e  te a cursor usin
2a1c3 67 20 61 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 78  g a.** call to x
2a1c4 4f 70 65 6e 2e 20 20 54 68 69 73 20 73 74 72 75  Open.  This stru
2a1c5 63 74 75 72 65 20 69 73 20 61 6e 20 69 6e 73 74  cture is an inst
2a1c6 61 6e 63 65 20 6f 66 20 61 20 63 75 72 73 6f 72  ance of a cursor
2a1c7 2e 20 20 49 74 0a 2a 2a 20 69 73 20 64 65 73 74  .  It.** is dest
2a1c8 72 6f 79 65 64 20 62 79 20 78 43 6c 6f 73 65 2e  royed by xClose.
2a1c9 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
2a1ca 63 74 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  ct fulltext_curs
2a1cb 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76  or {.  sqlite3_v
2a1cc 74 61 62 5f 63 75 72 73 6f 72 20 62 61 73 65 3b  tab_cursor base;
2a1cd 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20          /* Base 
2a1ce 63 6c 61 73 73 20 75 73 65 64 20 62 79 20 53 51  class used by SQ
2a1cf 4c 69 74 65 20 63 6f 72 65 20 2a 2f 0a 20 20 51  Lite core */.  Q
2a1d0 75 65 72 79 54 79 70 65 20 69 43 75 72 73 6f 72  ueryType iCursor
2a1d1 54 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20  Type;           
2a1d2 2f 2a 20 43 6f 70 79 20 6f 66 20 73 71 6c 69 74  /* Copy of sqlit
2a1d3 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 2e 69 64  e3_index_info.id
2a1d4 78 4e 75 6d 20 2a 2f 0a 20 20 73 71 6c 69 74 65  xNum */.  sqlite
2a1d5 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20  3_stmt *pStmt;  
2a1d6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72             /* Pr
2a1d7 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
2a1d8 20 69 6e 20 75 73 65 20 62 79 20 74 68 65 20 63   in use by the c
2a1d9 75 72 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 65  ursor */.  int e
2a1da 6f 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  of;             
2a1db 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2a1dc 72 75 65 20 69 66 20 61 74 20 45 6e 64 20 4f 66  rue if at End Of
2a1dd 20 52 65 73 75 6c 74 73 20 2a 2f 0a 20 20 51 75   Results */.  Qu
2a1de 65 72 79 20 71 3b 20 20 20 20 20 20 20 20 20 20  ery q;          
2a1df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2a1e0 2a 20 50 61 72 73 65 64 20 71 75 65 72 79 20 73  * Parsed query s
2a1e1 74 72 69 6e 67 20 2a 2f 0a 20 20 53 6e 69 70 70  tring */.  Snipp
2a1e2 65 74 20 73 6e 69 70 70 65 74 3b 20 20 20 20 20  et snippet;     
2a1e3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
2a1e4 61 63 68 65 64 20 73 6e 69 70 70 65 74 20 66 6f  ached snippet fo
2a1e5 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f  r the current ro
2a1e6 77 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75  w */.  int iColu
2a1e7 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  mn;             
2a1e8 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d          /* Colum
2a1e9 6e 20 62 65 69 6e 67 20 73 65 61 72 63 68 65 64  n being searched
2a1ea 20 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72   */.  DataBuffer
2a1eb 20 72 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20   result;        
2a1ec 20 20 20 20 20 20 20 2f 2a 20 44 6f 63 6c 69 73         /* Doclis
2a1ed 74 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 66  t results from f
2a1ee 75 6c 6c 74 65 78 74 51 75 65 72 79 20 2a 2f 0a  ulltextQuery */.
2a1ef 20 20 44 4c 52 65 61 64 65 72 20 72 65 61 64 65    DLReader reade
2a1f0 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
2a1f1 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 72 65 61     /* Result rea
2a1f2 64 65 72 20 69 66 20 72 65 73 75 6c 74 20 6e 6f  der if result no
2a1f3 74 20 65 6d 70 74 79 20 2a 2f 0a 7d 20 66 75 6c  t empty */.} ful
2a1f4 6c 74 65 78 74 5f 63 75 72 73 6f 72 3b 0a 0a 73  ltext_cursor;..s
2a1f5 74 61 74 69 63 20 73 74 72 75 63 74 20 66 75 6c  tatic struct ful
2a1f6 6c 74 65 78 74 5f 76 74 61 62 20 2a 63 75 72 73  ltext_vtab *curs
2a1f7 6f 72 5f 76 74 61 62 28 66 75 6c 6c 74 65 78 74  or_vtab(fulltext
2a1f8 5f 63 75 72 73 6f 72 20 2a 63 29 7b 0a 20 20 72  _cursor *c){.  r
2a1f9 65 74 75 72 6e 20 28 66 75 6c 6c 74 65 78 74 5f  eturn (fulltext_
2a1fa 76 74 61 62 20 2a 29 20 63 2d 3e 62 61 73 65 2e  vtab *) c->base.
2a1fb 70 56 74 61 62 3b 0a 7d 0a 0a 73 74 61 74 69 63  pVtab;.}..static
2a1fc 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
2a1fd 6f 64 75 6c 65 20 66 74 73 33 4d 6f 64 75 6c 65  odule fts3Module
2a1fe 3b 20 20 20 2f 2a 20 66 6f 72 77 61 72 64 20 64  ;   /* forward d
2a1ff 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 0a 2f  eclaration */../
2a200 2a 20 52 65 74 75 72 6e 20 61 20 64 79 6e 61 6d  * Return a dynam
2a201 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64  ically generated
2a202 20 73 74 61 74 65 6d 65 6e 74 20 6f 66 20 74 68   statement of th
2a203 65 20 66 6f 72 6d 0a 20 2a 20 20 20 69 6e 73 65  e form. *   inse
2a204 72 74 20 69 6e 74 6f 20 25 5f 63 6f 6e 74 65 6e  rt into %_conten
2a205 74 20 28 64 6f 63 69 64 2c 20 2e 2e 2e 29 20 76  t (docid, ...) v
2a206 61 6c 75 65 73 20 28 3f 2c 20 2e 2e 2e 29 0a 20  alues (?, ...). 
2a207 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
2a208 63 68 61 72 20 2a 63 6f 6e 74 65 6e 74 49 6e 73  char *contentIns
2a209 65 72 74 53 74 61 74 65 6d 65 6e 74 28 66 75 6c  ertStatement(ful
2a20a 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a  ltext_vtab *v){.
2a20b 20 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 73    StringBuffer s
2a20c 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 69  b;.  int i;..  i
2a20d 6e 69 74 53 74 72 69 6e 67 42 75 66 66 65 72 28  nitStringBuffer(
2a20e 26 73 62 29 3b 0a 20 20 61 70 70 65 6e 64 28 26  &sb);.  append(&
2a20f 73 62 2c 20 22 69 6e 73 65 72 74 20 69 6e 74 6f  sb, "insert into
2a210 20 25 5f 63 6f 6e 74 65 6e 74 20 28 64 6f 63 69   %_content (doci
2a211 64 2c 20 22 29 3b 0a 20 20 61 70 70 65 6e 64 4c  d, ");.  appendL
2a212 69 73 74 28 26 73 62 2c 20 76 2d 3e 6e 43 6f 6c  ist(&sb, v->nCol
2a213 75 6d 6e 2c 20 76 2d 3e 61 7a 43 6f 6e 74 65 6e  umn, v->azConten
2a214 74 43 6f 6c 75 6d 6e 29 3b 0a 20 20 61 70 70 65  tColumn);.  appe
2a215 6e 64 28 26 73 62 2c 20 22 29 20 76 61 6c 75 65  nd(&sb, ") value
2a216 73 20 28 3f 22 29 3b 0a 20 20 66 6f 72 28 69 3d  s (?");.  for(i=
2a217 30 3b 20 69 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b  0; i<v->nColumn;
2a218 20 2b 2b 69 29 0a 20 20 20 20 61 70 70 65 6e 64   ++i).    append
2a219 28 26 73 62 2c 20 22 2c 20 3f 22 29 3b 0a 20 20  (&sb, ", ?");.  
2a21a 61 70 70 65 6e 64 28 26 73 62 2c 20 22 29 22 29  append(&sb, ")")
2a21b 3b 0a 20 20 72 65 74 75 72 6e 20 73 74 72 69 6e  ;.  return strin
2a21c 67 42 75 66 66 65 72 44 61 74 61 28 26 73 62 29  gBufferData(&sb)
2a21d 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 61  ;.}../* Return a
2a21e 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 67 65 6e   dynamically gen
2a21f 65 72 61 74 65 64 20 73 74 61 74 65 6d 65 6e 74  erated statement
2a220 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 20 2a 20   of the form. * 
2a221 20 20 73 65 6c 65 63 74 20 3c 63 6f 6e 74 65 6e    select <conten
2a222 74 20 63 6f 6c 75 6d 6e 73 3e 20 66 72 6f 6d 20  t columns> from 
2a223 25 5f 63 6f 6e 74 65 6e 74 20 77 68 65 72 65 20  %_content where 
2a224 64 6f 63 69 64 20 3d 20 3f 0a 20 2a 2f 0a 73 74  docid = ?. */.st
2a225 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
2a226 2a 63 6f 6e 74 65 6e 74 53 65 6c 65 63 74 53 74  *contentSelectSt
2a227 61 74 65 6d 65 6e 74 28 66 75 6c 6c 74 65 78 74  atement(fulltext
2a228 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20 53 74 72  _vtab *v){.  Str
2a229 69 6e 67 42 75 66 66 65 72 20 73 62 3b 0a 20 20  ingBuffer sb;.  
2a22a 69 6e 69 74 53 74 72 69 6e 67 42 75 66 66 65 72  initStringBuffer
2a22b 28 26 73 62 29 3b 0a 20 20 61 70 70 65 6e 64 28  (&sb);.  append(
2a22c 26 73 62 2c 20 22 53 45 4c 45 43 54 20 22 29 3b  &sb, "SELECT ");
2a22d 0a 20 20 61 70 70 65 6e 64 4c 69 73 74 28 26 73  .  appendList(&s
2a22e 62 2c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 76  b, v->nColumn, v
2a22f 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d  ->azContentColum
2a230 6e 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 62  n);.  append(&sb
2a231 2c 20 22 20 46 52 4f 4d 20 25 5f 63 6f 6e 74 65  , " FROM %_conte
2a232 6e 74 20 57 48 45 52 45 20 64 6f 63 69 64 20 3d  nt WHERE docid =
2a233 20 3f 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 73   ?");.  return s
2a234 74 72 69 6e 67 42 75 66 66 65 72 44 61 74 61 28  tringBufferData(
2a235 26 73 62 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75  &sb);.}../* Retu
2a236 72 6e 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79  rn a dynamically
2a237 20 67 65 6e 65 72 61 74 65 64 20 73 74 61 74 65   generated state
2a238 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 6f 72 6d  ment of the form
2a239 0a 20 2a 20 20 20 75 70 64 61 74 65 20 25 5f 63  . *   update %_c
2a23a 6f 6e 74 65 6e 74 20 73 65 74 20 5b 63 6f 6c 5f  ontent set [col_
2a23b 30 5d 20 3d 20 3f 2c 20 5b 63 6f 6c 5f 31 5d 20  0] = ?, [col_1] 
2a23c 3d 20 3f 2c 20 2e 2e 2e 0a 20 2a 20 20 20 20 20  = ?, .... *     
2a23d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77                 w
2a23e 68 65 72 65 20 64 6f 63 69 64 20 3d 20 3f 0a 20  here docid = ?. 
2a23f 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
2a240 63 68 61 72 20 2a 63 6f 6e 74 65 6e 74 55 70 64  char *contentUpd
2a241 61 74 65 53 74 61 74 65 6d 65 6e 74 28 66 75 6c  ateStatement(ful
2a242 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a  ltext_vtab *v){.
2a243 20 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 73    StringBuffer s
2a244 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 69  b;.  int i;..  i
2a245 6e 69 74 53 74 72 69 6e 67 42 75 66 66 65 72 28  nitStringBuffer(
2a246 26 73 62 29 3b 0a 20 20 61 70 70 65 6e 64 28 26  &sb);.  append(&
2a247 73 62 2c 20 22 75 70 64 61 74 65 20 25 5f 63 6f  sb, "update %_co
2a248 6e 74 65 6e 74 20 73 65 74 20 22 29 3b 0a 20 20  ntent set ");.  
2a249 66 6f 72 28 69 3d 30 3b 20 69 3c 76 2d 3e 6e 43  for(i=0; i<v->nC
2a24a 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 20 7b 0a 20 20  olumn; ++i) {.  
2a24b 20 20 69 66 28 20 69 3e 30 20 29 7b 0a 20 20 20    if( i>0 ){.   
2a24c 20 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22     append(&sb, "
2a24d 2c 20 22 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , ");.    }.    
2a24e 61 70 70 65 6e 64 28 26 73 62 2c 20 76 2d 3e 61  append(&sb, v->a
2a24f 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 5b 69  zContentColumn[i
2a250 5d 29 3b 0a 20 20 20 20 61 70 70 65 6e 64 28 26  ]);.    append(&
2a251 73 62 2c 20 22 20 3d 20 3f 22 29 3b 0a 20 20 7d  sb, " = ?");.  }
2a252 0a 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22  .  append(&sb, "
2a253 20 77 68 65 72 65 20 64 6f 63 69 64 20 3d 20 3f   where docid = ?
2a254 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 74 72  ");.  return str
2a255 69 6e 67 42 75 66 66 65 72 44 61 74 61 28 26 73  ingBufferData(&s
2a256 62 29 3b 0a 7d 0a 0a 2f 2a 20 50 75 74 73 20 61  b);.}../* Puts a
2a257 20 66 72 65 73 68 6c 79 2d 70 72 65 70 61 72 65   freshly-prepare
2a258 64 20 73 74 61 74 65 6d 65 6e 74 20 64 65 74 65  d statement dete
2a259 72 6d 69 6e 65 64 20 62 79 20 69 53 74 6d 74 20  rmined by iStmt 
2a25a 69 6e 20 2a 70 70 53 74 6d 74 2e 0a 2a 2a 20 49  in *ppStmt..** I
2a25b 66 20 74 68 65 20 69 6e 64 69 63 61 74 65 64 20  f the indicated 
2a25c 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 6e 65  statement has ne
2a25d 76 65 72 20 62 65 65 6e 20 70 72 65 70 61 72 65  ver been prepare
2a25e 64 2c 20 69 74 20 69 73 20 70 72 65 70 61 72 65  d, it is prepare
2a25f 64 0a 2a 2a 20 61 6e 64 20 63 61 63 68 65 64 2c  d.** and cached,
2a260 20 6f 74 68 65 72 77 69 73 65 20 74 68 65 20 63   otherwise the c
2a261 61 63 68 65 64 20 76 65 72 73 69 6f 6e 20 69 73  ached version is
2a262 20 72 65 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69   reset..*/.stati
2a263 63 20 69 6e 74 20 73 71 6c 5f 67 65 74 5f 73 74  c int sql_get_st
2a264 61 74 65 6d 65 6e 74 28 66 75 6c 6c 74 65 78 74  atement(fulltext
2a265 5f 76 74 61 62 20 2a 76 2c 20 66 75 6c 6c 74 65  _vtab *v, fullte
2a266 78 74 5f 73 74 61 74 65 6d 65 6e 74 20 69 53 74  xt_statement iSt
2a267 6d 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  mt,.            
2a268 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a269 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
2a26a 70 70 53 74 6d 74 29 7b 0a 20 20 61 73 73 65 72  ppStmt){.  asser
2a26b 74 28 20 69 53 74 6d 74 3c 4d 41 58 5f 53 54 4d  t( iStmt<MAX_STM
2a26c 54 20 29 3b 0a 20 20 69 66 28 20 76 2d 3e 70 46  T );.  if( v->pF
2a26d 75 6c 6c 74 65 78 74 53 74 61 74 65 6d 65 6e 74  ulltextStatement
2a26e 73 5b 69 53 74 6d 74 5d 3d 3d 4e 55 4c 4c 20 29  s[iStmt]==NULL )
2a26f 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
2a270 20 2a 7a 53 74 6d 74 3b 0a 20 20 20 20 69 6e 74   *zStmt;.    int
2a271 20 72 63 3b 0a 20 20 20 20 73 77 69 74 63 68 28   rc;.    switch(
2a272 20 69 53 74 6d 74 20 29 7b 0a 20 20 20 20 20 20   iStmt ){.      
2a273 63 61 73 65 20 43 4f 4e 54 45 4e 54 5f 49 4e 53  case CONTENT_INS
2a274 45 52 54 5f 53 54 4d 54 3a 0a 20 20 20 20 20 20  ERT_STMT:.      
2a275 20 20 7a 53 74 6d 74 20 3d 20 63 6f 6e 74 65 6e    zStmt = conten
2a276 74 49 6e 73 65 72 74 53 74 61 74 65 6d 65 6e 74  tInsertStatement
2a277 28 76 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20  (v); break;.    
2a278 20 20 63 61 73 65 20 43 4f 4e 54 45 4e 54 5f 53    case CONTENT_S
2a279 45 4c 45 43 54 5f 53 54 4d 54 3a 0a 20 20 20 20  ELECT_STMT:.    
2a27a 20 20 20 20 7a 53 74 6d 74 20 3d 20 63 6f 6e 74      zStmt = cont
2a27b 65 6e 74 53 65 6c 65 63 74 53 74 61 74 65 6d 65  entSelectStateme
2a27c 6e 74 28 76 29 3b 20 62 72 65 61 6b 3b 0a 20 20  nt(v); break;.  
2a27d 20 20 20 20 63 61 73 65 20 43 4f 4e 54 45 4e 54      case CONTENT
2a27e 5f 55 50 44 41 54 45 5f 53 54 4d 54 3a 0a 20 20  _UPDATE_STMT:.  
2a27f 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 63 6f        zStmt = co
2a280 6e 74 65 6e 74 55 70 64 61 74 65 53 74 61 74 65  ntentUpdateState
2a281 6d 65 6e 74 28 76 29 3b 20 62 72 65 61 6b 3b 0a  ment(v); break;.
2a282 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20        default:. 
2a283 20 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 66         zStmt = f
2a284 75 6c 6c 74 65 78 74 5f 7a 53 74 61 74 65 6d 65  ulltext_zStateme
2a285 6e 74 5b 69 53 74 6d 74 5d 3b 0a 20 20 20 20 7d  nt[iStmt];.    }
2a286 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 5f 70 72  .    rc = sql_pr
2a287 65 70 61 72 65 28 76 2d 3e 64 62 2c 20 76 2d 3e  epare(v->db, v->
2a288 7a 44 62 2c 20 76 2d 3e 7a 4e 61 6d 65 2c 20 26  zDb, v->zName, &
2a289 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74  v->pFulltextStat
2a28a 65 6d 65 6e 74 73 5b 69 53 74 6d 74 5d 2c 0a 20  ements[iStmt],. 
2a28b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a28c 20 20 20 20 20 20 20 20 7a 53 74 6d 74 29 3b 0a          zStmt);.
2a28d 20 20 20 20 69 66 28 20 7a 53 74 6d 74 20 21 3d      if( zStmt !=
2a28e 20 66 75 6c 6c 74 65 78 74 5f 7a 53 74 61 74 65   fulltext_zState
2a28f 6d 65 6e 74 5b 69 53 74 6d 74 5d 29 20 73 71 6c  ment[iStmt]) sql
2a290 69 74 65 33 5f 66 72 65 65 28 28 76 6f 69 64 20  ite3_free((void 
2a291 2a 29 20 7a 53 74 6d 74 29 3b 0a 20 20 20 20 69  *) zStmt);.    i
2a292 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2a293 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
2a294 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 69 6e 74  } else {.    int
2a295 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65   rc = sqlite3_re
2a296 73 65 74 28 76 2d 3e 70 46 75 6c 6c 74 65 78 74  set(v->pFulltext
2a297 53 74 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d 74  Statements[iStmt
2a298 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  ]);.    if( rc!=
2a299 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
2a29a 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a 70  rn rc;.  }..  *p
2a29b 70 53 74 6d 74 20 3d 20 76 2d 3e 70 46 75 6c 6c  pStmt = v->pFull
2a29c 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73 5b 69  textStatements[i
2a29d 53 74 6d 74 5d 3b 0a 20 20 72 65 74 75 72 6e 20  Stmt];.  return 
2a29e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
2a29f 20 4c 69 6b 65 20 73 71 6c 69 74 65 33 5f 73 74   Like sqlite3_st
2a2a0 65 70 28 29 2c 20 62 75 74 20 63 6f 6e 76 65 72  ep(), but conver
2a2a1 74 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 74 6f  t SQLITE_DONE to
2a2a2 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 0a 2a   SQLITE_OK and.*
2a2a3 2a 20 53 51 4c 49 54 45 5f 52 4f 57 20 74 6f 20  * SQLITE_ROW to 
2a2a4 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20 20 55  SQLITE_ERROR.  U
2a2a5 73 65 66 75 6c 20 66 6f 72 20 73 74 61 74 65 6d  seful for statem
2a2a6 65 6e 74 73 20 6c 69 6b 65 20 55 50 44 41 54 45  ents like UPDATE
2a2a7 2c 0a 2a 2a 20 77 68 65 72 65 20 77 65 20 65 78  ,.** where we ex
2a2a8 70 65 63 74 20 6e 6f 20 72 65 73 75 6c 74 73 2e  pect no results.
2a2a9 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
2a2aa 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73  ql_single_step(s
2a2ab 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 29 7b  qlite3_stmt *s){
2a2ac 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69  .  int rc = sqli
2a2ad 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 72  te3_step(s);.  r
2a2ae 65 74 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54  eturn (rc==SQLIT
2a2af 45 5f 44 4f 4e 45 29 20 3f 20 53 51 4c 49 54 45  E_DONE) ? SQLITE
2a2b0 5f 4f 4b 20 3a 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  _OK : rc;.}../* 
2a2b1 4c 69 6b 65 20 73 71 6c 5f 67 65 74 5f 73 74 61  Like sql_get_sta
2a2b2 74 65 6d 65 6e 74 28 29 2c 20 62 75 74 20 66 6f  tement(), but fo
2a2b3 72 20 73 70 65 63 69 61 6c 20 72 65 70 6c 69 63  r special replic
2a2b4 61 74 65 64 20 4c 45 41 46 5f 53 45 4c 45 43 54  ated LEAF_SELECT
2a2b5 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  .** statements..
2a2b6 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  */./* TODO(shess
2a2b7 29 20 57 72 69 74 65 20 76 65 72 73 69 6f 6e 20  ) Write version 
2a2b8 66 6f 72 20 67 65 6e 65 72 69 63 20 73 74 61 74  for generic stat
2a2b9 65 6d 65 6e 74 73 20 61 6e 64 20 74 68 65 6e 20  ements and then 
2a2ba 73 68 61 72 65 0a 2a 2a 20 74 68 61 74 20 62 65  share.** that be
2a2bb 74 77 65 65 6e 20 74 68 65 20 63 61 63 68 65 64  tween the cached
2a2bc 2d 73 74 61 74 65 6d 65 6e 74 20 66 75 6e 63 74  -statement funct
2a2bd 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ions..*/.static 
2a2be 69 6e 74 20 73 71 6c 5f 67 65 74 5f 6c 65 61 66  int sql_get_leaf
2a2bf 5f 73 74 61 74 65 6d 65 6e 74 28 66 75 6c 6c 74  _statement(fullt
2a2c0 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74  ext_vtab *v, int
2a2c1 20 69 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20   idx,.          
2a2c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a2c3 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
2a2c4 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 29 7b 0a  stmt **ppStmt){.
2a2c5 20 20 61 73 73 65 72 74 28 20 69 64 78 3e 3d 30    assert( idx>=0
2a2c6 20 26 26 20 69 64 78 3c 4d 45 52 47 45 5f 43 4f   && idx<MERGE_CO
2a2c7 55 4e 54 20 29 3b 0a 20 20 69 66 28 20 76 2d 3e  UNT );.  if( v->
2a2c8 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d 74 73  pLeafSelectStmts
2a2c9 5b 69 64 78 5d 3d 3d 4e 55 4c 4c 20 29 7b 0a 20  [idx]==NULL ){. 
2a2ca 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f     int rc = sql_
2a2cb 70 72 65 70 61 72 65 28 76 2d 3e 64 62 2c 20 76  prepare(v->db, v
2a2cc 2d 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61 6d 65 2c  ->zDb, v->zName,
2a2cd 20 26 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74   &v->pLeafSelect
2a2ce 53 74 6d 74 73 5b 69 64 78 5d 2c 0a 20 20 20 20  Stmts[idx],.    
2a2cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a2d0 20 20 20 20 20 4c 45 41 46 5f 53 45 4c 45 43 54       LEAF_SELECT
2a2d1 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
2a2d2 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
2a2d3 6e 20 72 63 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  n rc;.  }else{. 
2a2d4 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69     int rc = sqli
2a2d5 74 65 33 5f 72 65 73 65 74 28 76 2d 3e 70 4c 65  te3_reset(v->pLe
2a2d6 61 66 53 65 6c 65 63 74 53 74 6d 74 73 5b 69 64  afSelectStmts[id
2a2d7 78 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  x]);.    if( rc!
2a2d8 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
2a2d9 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a  urn rc;.  }..  *
2a2da 70 70 53 74 6d 74 20 3d 20 76 2d 3e 70 4c 65 61  ppStmt = v->pLea
2a2db 66 53 65 6c 65 63 74 53 74 6d 74 73 5b 69 64 78  fSelectStmts[idx
2a2dc 5d 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ];.  return SQLI
2a2dd 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 69 6e 73  TE_OK;.}../* ins
2a2de 65 72 74 20 69 6e 74 6f 20 25 5f 63 6f 6e 74 65  ert into %_conte
2a2df 6e 74 20 28 64 6f 63 69 64 2c 20 2e 2e 2e 29 20  nt (docid, ...) 
2a2e0 76 61 6c 75 65 73 20 28 5b 64 6f 63 69 64 5d 2c  values ([docid],
2a2e1 20 5b 70 56 61 6c 75 65 73 5d 29 0a 2a 2a 20 49   [pValues]).** I
2a2e2 66 20 74 68 65 20 64 6f 63 69 64 20 63 6f 6e 74  f the docid cont
2a2e3 61 69 6e 73 20 53 51 4c 20 4e 55 4c 4c 2c 20 74  ains SQL NULL, t
2a2e4 68 65 6e 20 61 20 75 6e 69 71 75 65 20 64 6f 63  hen a unique doc
2a2e5 69 64 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 67 65  id will be.** ge
2a2e6 6e 65 72 61 74 65 64 2e 0a 2a 2f 0a 73 74 61 74  nerated..*/.stat
2a2e7 69 63 20 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 69  ic int content_i
2a2e8 6e 73 65 72 74 28 66 75 6c 6c 74 65 78 74 5f 76  nsert(fulltext_v
2a2e9 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 33 5f  tab *v, sqlite3_
2a2ea 76 61 6c 75 65 20 2a 64 6f 63 69 64 2c 0a 20 20  value *docid,.  
2a2eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a2ec 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
2a2ed 76 61 6c 75 65 20 2a 2a 70 56 61 6c 75 65 73 29  value **pValues)
2a2ee 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  {.  sqlite3_stmt
2a2ef 20 2a 73 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20   *s;.  int i;.  
2a2f0 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74  int rc = sql_get
2a2f1 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 43 4f  _statement(v, CO
2a2f2 4e 54 45 4e 54 5f 49 4e 53 45 52 54 5f 53 54 4d  NTENT_INSERT_STM
2a2f3 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63  T, &s);.  if( rc
2a2f4 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
2a2f5 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
2a2f6 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61   sqlite3_bind_va
2a2f7 6c 75 65 28 73 2c 20 31 2c 20 64 6f 63 69 64 29  lue(s, 1, docid)
2a2f8 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
2a2f9 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
2a2fa 63 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  c;..  for(i=0; i
2a2fb 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69  <v->nColumn; ++i
2a2fc 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
2a2fd 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73  te3_bind_value(s
2a2fe 2c 20 32 2b 69 2c 20 70 56 61 6c 75 65 73 5b 69  , 2+i, pValues[i
2a2ff 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  ]);.    if( rc!=
2a300 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
2a301 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 72 65  rn rc;.  }..  re
2a302 74 75 72 6e 20 73 71 6c 5f 73 69 6e 67 6c 65 5f  turn sql_single_
2a303 73 74 65 70 28 73 29 3b 0a 7d 0a 0a 2f 2a 20 75  step(s);.}../* u
2a304 70 64 61 74 65 20 25 5f 63 6f 6e 74 65 6e 74 20  pdate %_content 
2a305 73 65 74 20 63 6f 6c 30 20 3d 20 70 56 61 6c 75  set col0 = pValu
2a306 65 73 5b 30 5d 2c 20 63 6f 6c 31 20 3d 20 70 56  es[0], col1 = pV
2a307 61 6c 75 65 73 5b 31 5d 2c 20 2e 2e 2e 0a 20 2a  alues[1], .... *
2a308 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a309 20 20 77 68 65 72 65 20 64 6f 63 69 64 20 3d 20    where docid = 
2a30a 5b 69 44 6f 63 69 64 5d 20 2a 2f 0a 73 74 61 74  [iDocid] */.stat
2a30b 69 63 20 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 75  ic int content_u
2a30c 70 64 61 74 65 28 66 75 6c 6c 74 65 78 74 5f 76  pdate(fulltext_v
2a30d 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 33 5f  tab *v, sqlite3_
2a30e 76 61 6c 75 65 20 2a 2a 70 56 61 6c 75 65 73 2c  value **pValues,
2a30f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2a310 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2a311 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 29 7b  e_int64 iDocid){
2a312 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
2a313 2a 73 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  *s;.  int i;.  i
2a314 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f  nt rc = sql_get_
2a315 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 43 4f 4e  statement(v, CON
2a316 54 45 4e 54 5f 55 50 44 41 54 45 5f 53 54 4d 54  TENT_UPDATE_STMT
2a317 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21  , &s);.  if( rc!
2a318 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
2a319 75 72 6e 20 72 63 3b 0a 0a 20 20 66 6f 72 28 69  urn rc;..  for(i
2a31a 3d 30 3b 20 69 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e  =0; i<v->nColumn
2a31b 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 72 63 20 3d  ; ++i){.    rc =
2a31c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61   sqlite3_bind_va
2a31d 6c 75 65 28 73 2c 20 31 2b 69 2c 20 70 56 61 6c  lue(s, 1+i, pVal
2a31e 75 65 73 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28  ues[i]);.    if(
2a31f 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2a320 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
2a321 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
2a322 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 31 2b  bind_int64(s, 1+
2a323 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 69 44 6f 63  v->nColumn, iDoc
2a324 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  id);.  if( rc!=S
2a325 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
2a326 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20  n rc;..  return 
2a327 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28  sql_single_step(
2a328 73 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  s);.}..static vo
2a329 69 64 20 66 72 65 65 53 74 72 69 6e 67 41 72 72  id freeStringArr
2a32a 61 79 28 69 6e 74 20 6e 53 74 72 69 6e 67 2c 20  ay(int nString, 
2a32b 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 53 74  const char **pSt
2a32c 72 69 6e 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  ring){.  int i;.
2a32d 0a 20 20 66 6f 72 20 28 69 3d 30 20 3b 20 69 20  .  for (i=0 ; i 
2a32e 3c 20 6e 53 74 72 69 6e 67 20 3b 20 2b 2b 69 29  < nString ; ++i)
2a32f 20 7b 0a 20 20 20 20 69 66 28 20 70 53 74 72 69   {.    if( pStri
2a330 6e 67 5b 69 5d 21 3d 4e 55 4c 4c 20 29 20 73 71  ng[i]!=NULL ) sq
2a331 6c 69 74 65 33 5f 66 72 65 65 28 28 76 6f 69 64  lite3_free((void
2a332 20 2a 29 20 70 53 74 72 69 6e 67 5b 69 5d 29 3b   *) pString[i]);
2a333 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66  .  }.  sqlite3_f
2a334 72 65 65 28 28 76 6f 69 64 20 2a 29 20 70 53 74  ree((void *) pSt
2a335 72 69 6e 67 29 3b 0a 7d 0a 0a 2f 2a 20 73 65 6c  ring);.}../* sel
2a336 65 63 74 20 2a 20 66 72 6f 6d 20 25 5f 63 6f 6e  ect * from %_con
2a337 74 65 6e 74 20 77 68 65 72 65 20 64 6f 63 69 64  tent where docid
2a338 20 3d 20 5b 69 44 6f 63 69 64 5d 0a 20 2a 20 54   = [iDocid]. * T
2a339 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 64  he caller must d
2a33a 65 6c 65 74 65 20 74 68 65 20 72 65 74 75 72 6e  elete the return
2a33b 65 64 20 61 72 72 61 79 20 61 6e 64 20 61 6c 6c  ed array and all
2a33c 20 73 74 72 69 6e 67 73 20 69 6e 20 69 74 2e 0a   strings in it..
2a33d 20 2a 20 6e 75 6c 6c 20 66 69 65 6c 64 73 20 77   * null fields w
2a33e 69 6c 6c 20 62 65 20 4e 55 4c 4c 20 69 6e 20 74  ill be NULL in t
2a33f 68 65 20 72 65 74 75 72 6e 65 64 20 61 72 72 61  he returned arra
2a340 79 2e 0a 20 2a 0a 20 2a 20 54 4f 44 4f 3a 20 50  y.. *. * TODO: P
2a341 65 72 68 61 70 73 20 77 65 20 73 68 6f 75 6c 64  erhaps we should
2a342 20 72 65 74 75 72 6e 20 70 6f 69 6e 74 65 72 2f   return pointer/
2a343 6c 65 6e 67 74 68 20 73 74 72 69 6e 67 73 20 68  length strings h
2a344 65 72 65 20 66 6f 72 20 63 6f 6e 73 69 73 74 65  ere for consiste
2a345 6e 63 79 0a 20 2a 20 77 69 74 68 20 6f 74 68 65  ncy. * with othe
2a346 72 20 63 6f 64 65 20 77 68 69 63 68 20 75 73 65  r code which use
2a347 73 20 70 6f 69 6e 74 65 72 2f 6c 65 6e 67 74 68  s pointer/length
2a348 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  . */.static int 
2a349 63 6f 6e 74 65 6e 74 5f 73 65 6c 65 63 74 28 66  content_select(f
2a34a 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
2a34b 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44   sqlite_int64 iD
2a34c 6f 63 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20  ocid,.          
2a34d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a34e 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 2a 70 56  const char ***pV
2a34f 61 6c 75 65 73 29 7b 0a 20 20 73 71 6c 69 74 65  alues){.  sqlite
2a350 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 63 6f 6e  3_stmt *s;.  con
2a351 73 74 20 63 68 61 72 20 2a 2a 76 61 6c 75 65 73  st char **values
2a352 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  ;.  int i;.  int
2a353 20 72 63 3b 0a 0a 20 20 2a 70 56 61 6c 75 65 73   rc;..  *pValues
2a354 20 3d 20 4e 55 4c 4c 3b 0a 0a 20 20 72 63 20 3d   = NULL;..  rc =
2a355 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65   sql_get_stateme
2a356 6e 74 28 76 2c 20 43 4f 4e 54 45 4e 54 5f 53 45  nt(v, CONTENT_SE
2a357 4c 45 43 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a  LECT_STMT, &s);.
2a358 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2a359 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
2a35a 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
2a35b 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 31  _bind_int64(s, 1
2a35c 2c 20 69 44 6f 63 69 64 29 3b 0a 20 20 69 66 28  , iDocid);.  if(
2a35d 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2a35e 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
2a35f 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
2a360 28 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  (s);.  if( rc!=S
2a361 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75  QLITE_ROW ) retu
2a362 72 6e 20 72 63 3b 0a 0a 20 20 76 61 6c 75 65 73  rn rc;..  values
2a363 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
2a364 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  *) sqlite3_mallo
2a365 63 28 76 2d 3e 6e 43 6f 6c 75 6d 6e 20 2a 20 73  c(v->nColumn * s
2a366 69 7a 65 6f 66 28 63 6f 6e 73 74 20 63 68 61 72  izeof(const char
2a367 20 2a 29 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b   *));.  for(i=0;
2a368 20 69 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b   i<v->nColumn; +
2a369 2b 69 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  +i){.    if( sql
2a36a 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65  ite3_column_type
2a36b 28 73 2c 20 69 29 3d 3d 53 51 4c 49 54 45 5f 4e  (s, i)==SQLITE_N
2a36c 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 76 61 6c  ULL ){.      val
2a36d 75 65 73 5b 69 5d 20 3d 20 4e 55 4c 4c 3b 0a 20  ues[i] = NULL;. 
2a36e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2a36f 76 61 6c 75 65 73 5b 69 5d 20 3d 20 73 74 72 69  values[i] = stri
2a370 6e 67 5f 64 75 70 28 28 63 68 61 72 2a 29 73 71  ng_dup((char*)sq
2a371 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
2a372 74 28 73 2c 20 69 29 29 3b 0a 20 20 20 20 7d 0a  t(s, i));.    }.
2a373 20 20 7d 0a 0a 20 20 2f 2a 20 57 65 20 65 78 70    }..  /* We exp
2a374 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77  ect only one row
2a375 2e 20 20 57 65 20 6d 75 73 74 20 65 78 65 63 75  .  We must execu
2a376 74 65 20 61 6e 6f 74 68 65 72 20 73 71 6c 69 74  te another sqlit
2a377 65 33 5f 73 74 65 70 28 29 0a 20 20 20 2a 20 74  e3_step().   * t
2a378 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 69  o complete the i
2a379 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65 72 77  teration; otherw
2a37a 69 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69  ise the table wi
2a37b 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f 63 6b 65 64  ll remain locked
2a37c 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69  . */.  rc = sqli
2a37d 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 69  te3_step(s);.  i
2a37e 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  f( rc==SQLITE_DO
2a37f 4e 45 20 29 7b 0a 20 20 20 20 2a 70 56 61 6c 75  NE ){.    *pValu
2a380 65 73 20 3d 20 76 61 6c 75 65 73 3b 0a 20 20 20  es = values;.   
2a381 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
2a382 4b 3b 0a 20 20 7d 0a 0a 20 20 66 72 65 65 53 74  K;.  }..  freeSt
2a383 72 69 6e 67 41 72 72 61 79 28 76 2d 3e 6e 43 6f  ringArray(v->nCo
2a384 6c 75 6d 6e 2c 20 76 61 6c 75 65 73 29 3b 0a 20  lumn, values);. 
2a385 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
2a386 2a 20 64 65 6c 65 74 65 20 66 72 6f 6d 20 25 5f  * delete from %_
2a387 63 6f 6e 74 65 6e 74 20 77 68 65 72 65 20 64 6f  content where do
2a388 63 69 64 20 3d 20 5b 69 44 6f 63 69 64 20 5d 20  cid = [iDocid ] 
2a389 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f  */.static int co
2a38a 6e 74 65 6e 74 5f 64 65 6c 65 74 65 28 66 75 6c  ntent_delete(ful
2a38b 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73  ltext_vtab *v, s
2a38c 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63  qlite_int64 iDoc
2a38d 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73  id){.  sqlite3_s
2a38e 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63  tmt *s;.  int rc
2a38f 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65   = sql_get_state
2a390 6d 65 6e 74 28 76 2c 20 43 4f 4e 54 45 4e 54 5f  ment(v, CONTENT_
2a391 44 45 4c 45 54 45 5f 53 54 4d 54 2c 20 26 73 29  DELETE_STMT, &s)
2a392 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
2a393 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
2a394 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  c;..  rc = sqlit
2a395 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c  e3_bind_int64(s,
2a396 20 31 2c 20 69 44 6f 63 69 64 29 3b 0a 20 20 69   1, iDocid);.  i
2a397 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2a398 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
2a399 20 72 65 74 75 72 6e 20 73 71 6c 5f 73 69 6e 67   return sql_sing
2a39a 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d 0a 0a 2f  le_step(s);.}../
2a39b 2a 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 25 5f  * insert into %_
2a39c 73 65 67 6d 65 6e 74 73 20 76 61 6c 75 65 73 20  segments values 
2a39d 28 5b 70 44 61 74 61 5d 29 0a 2a 2a 20 20 20 72  ([pData]).**   r
2a39e 65 74 75 72 6e 73 20 61 73 73 69 67 6e 65 64 20  eturns assigned 
2a39f 62 6c 6f 63 6b 69 64 20 69 6e 20 2a 70 69 42 6c  blockid in *piBl
2a3a0 6f 63 6b 69 64 0a 2a 2f 0a 73 74 61 74 69 63 20  ockid.*/.static 
2a3a1 69 6e 74 20 62 6c 6f 63 6b 5f 69 6e 73 65 72 74  int block_insert
2a3a2 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
2a3a3 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  v, const char *p
2a3a4 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c  Data, int nData,
2a3a5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2a3a6 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f           sqlite_
2a3a7 69 6e 74 36 34 20 2a 70 69 42 6c 6f 63 6b 69 64  int64 *piBlockid
2a3a8 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  ){.  sqlite3_stm
2a3a9 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d  t *s;.  int rc =
2a3aa 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65   sql_get_stateme
2a3ab 6e 74 28 76 2c 20 42 4c 4f 43 4b 5f 49 4e 53 45  nt(v, BLOCK_INSE
2a3ac 52 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20  RT_STMT, &s);.  
2a3ad 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2a3ae 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
2a3af 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
2a3b0 69 6e 64 5f 62 6c 6f 62 28 73 2c 20 31 2c 20 70  ind_blob(s, 1, p
2a3b1 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 53 51 4c  Data, nData, SQL
2a3b2 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 69  ITE_STATIC);.  i
2a3b3 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2a3b4 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
2a3b5 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74   rc = sqlite3_st
2a3b6 65 70 28 73 29 3b 0a 20 20 69 66 28 20 72 63 3d  ep(s);.  if( rc=
2a3b7 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65  =SQLITE_ROW ) re
2a3b8 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
2a3b9 52 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  R;.  if( rc!=SQL
2a3ba 49 54 45 5f 44 4f 4e 45 20 29 20 72 65 74 75 72  ITE_DONE ) retur
2a3bb 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 62 6c 6f 63  n rc;..  /* bloc
2a3bc 6b 69 64 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e  kid column is an
2a3bd 20 61 6c 69 61 73 20 66 6f 72 20 72 6f 77 69 64   alias for rowid
2a3be 2e 20 2a 2f 0a 20 20 2a 70 69 42 6c 6f 63 6b 69  . */.  *piBlocki
2a3bf 64 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61 73 74  d = sqlite3_last
2a3c0 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 76 2d  _insert_rowid(v-
2a3c1 3e 64 62 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  >db);.  return S
2a3c2 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20  QLITE_OK;.}../* 
2a3c3 64 65 6c 65 74 65 20 66 72 6f 6d 20 25 5f 73 65  delete from %_se
2a3c4 67 6d 65 6e 74 73 0a 2a 2a 20 20 20 77 68 65 72  gments.**   wher
2a3c5 65 20 62 6c 6f 63 6b 69 64 20 62 65 74 77 65 65  e blockid betwee
2a3c6 6e 20 5b 69 53 74 61 72 74 42 6c 6f 63 6b 69 64  n [iStartBlockid
2a3c7 5d 20 61 6e 64 20 5b 69 45 6e 64 42 6c 6f 63 6b  ] and [iEndBlock
2a3c8 69 64 5d 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65  id].**.** Delete
2a3c9 73 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20 62  s the range of b
2a3ca 6c 6f 63 6b 73 2c 20 69 6e 63 6c 75 73 69 76 65  locks, inclusive
2a3cb 2c 20 75 73 65 64 20 74 6f 20 64 65 6c 65 74 65  , used to delete
2a3cc 20 74 68 65 20 62 6c 6f 63 6b 73 0a 2a 2a 20 77   the blocks.** w
2a3cd 68 69 63 68 20 66 6f 72 6d 20 61 20 73 65 67 6d  hich form a segm
2a3ce 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ent..*/.static i
2a3cf 6e 74 20 62 6c 6f 63 6b 5f 64 65 6c 65 74 65 28  nt block_delete(
2a3d0 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
2a3d1 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2a3d2 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2a3d3 5f 69 6e 74 36 34 20 69 53 74 61 72 74 42 6c 6f  _int64 iStartBlo
2a3d4 63 6b 69 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74  ckid, sqlite_int
2a3d5 36 34 20 69 45 6e 64 42 6c 6f 63 6b 69 64 29 7b  64 iEndBlockid){
2a3d6 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
2a3d7 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73  *s;.  int rc = s
2a3d8 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
2a3d9 28 76 2c 20 42 4c 4f 43 4b 5f 44 45 4c 45 54 45  (v, BLOCK_DELETE
2a3da 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66  _STMT, &s);.  if
2a3db 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2a3dc 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
2a3dd 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e  rc = sqlite3_bin
2a3de 64 5f 69 6e 74 36 34 28 73 2c 20 31 2c 20 69 53  d_int64(s, 1, iS
2a3df 74 61 72 74 42 6c 6f 63 6b 69 64 29 3b 0a 20 20  tartBlockid);.  
2a3e0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2a3e1 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
2a3e2 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
2a3e3 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 32 2c 20  ind_int64(s, 2, 
2a3e4 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20 20  iEndBlockid);.  
2a3e5 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2a3e6 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
2a3e7 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 73 69 6e    return sql_sin
2a3e8 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d 0a 0a  gle_step(s);.}..
2a3e9 2f 2a 20 52 65 74 75 72 6e 73 20 53 51 4c 49 54  /* Returns SQLIT
2a3ea 45 5f 52 4f 57 20 77 69 74 68 20 2a 70 69 64 78  E_ROW with *pidx
2a3eb 20 73 65 74 20 74 6f 20 74 68 65 20 6d 61 78 69   set to the maxi
2a3ec 6d 75 6d 20 73 65 67 6d 65 6e 74 20 69 64 78 20  mum segment idx 
2a3ed 66 6f 75 6e 64 0a 2a 2a 20 61 74 20 69 4c 65 76  found.** at iLev
2a3ee 65 6c 2e 20 20 52 65 74 75 72 6e 73 20 53 51 4c  el.  Returns SQL
2a3ef 49 54 45 5f 44 4f 4e 45 20 69 66 20 74 68 65 72  ITE_DONE if ther
2a3f0 65 20 61 72 65 20 6e 6f 20 73 65 67 6d 65 6e 74  e are no segment
2a3f1 73 20 61 74 0a 2a 2a 20 69 4c 65 76 65 6c 2e 20  s at.** iLevel. 
2a3f2 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72   Otherwise retur
2a3f3 6e 73 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a  ns an error..*/.
2a3f4 73 74 61 74 69 63 20 69 6e 74 20 73 65 67 64 69  static int segdi
2a3f5 72 5f 6d 61 78 5f 69 6e 64 65 78 28 66 75 6c 6c  r_max_index(full
2a3f6 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e  text_vtab *v, in
2a3f7 74 20 69 4c 65 76 65 6c 2c 20 69 6e 74 20 2a 70  t iLevel, int *p
2a3f8 69 64 78 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  idx){.  sqlite3_
2a3f9 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72  stmt *s;.  int r
2a3fa 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74  c = sql_get_stat
2a3fb 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49 52 5f  ement(v, SEGDIR_
2a3fc 4d 41 58 5f 49 4e 44 45 58 5f 53 54 4d 54 2c 20  MAX_INDEX_STMT, 
2a3fd 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  &s);.  if( rc!=S
2a3fe 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
2a3ff 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
2a400 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73  lite3_bind_int(s
2a401 2c 20 31 2c 20 69 4c 65 76 65 6c 29 3b 0a 20 20  , 1, iLevel);.  
2a402 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2a403 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
2a404 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73    rc = sqlite3_s
2a405 74 65 70 28 73 29 3b 0a 20 20 2f 2a 20 53 68 6f  tep(s);.  /* Sho
2a406 75 6c 64 20 61 6c 77 61 79 73 20 67 65 74 20 61  uld always get a
2a407 74 20 6c 65 61 73 74 20 6f 6e 65 20 72 6f 77 20  t least one row 
2a408 64 75 65 20 74 6f 20 68 6f 77 20 6d 61 78 28 29  due to how max()
2a409 20 77 6f 72 6b 73 2e 20 2a 2f 0a 20 20 69 66 28   works. */.  if(
2a40a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc==SQLITE_DONE
2a40b 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
2a40c 5f 44 4f 4e 45 3b 0a 20 20 69 66 28 20 72 63 21  _DONE;.  if( rc!
2a40d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65  =SQLITE_ROW ) re
2a40e 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 4e  turn rc;..  /* N
2a40f 55 4c 4c 20 6d 65 61 6e 73 20 74 68 61 74 20 74  ULL means that t
2a410 68 65 72 65 20 77 65 72 65 20 6e 6f 20 69 6e 70  here were no inp
2a411 75 74 73 20 74 6f 20 6d 61 78 28 29 2e 20 2a 2f  uts to max(). */
2a412 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 55  .  if( SQLITE_NU
2a413 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 63 6f 6c 75  LL==sqlite3_colu
2a414 6d 6e 5f 74 79 70 65 28 73 2c 20 30 29 20 29 7b  mn_type(s, 0) ){
2a415 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
2a416 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 20 20 69  3_step(s);.    i
2a417 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f  f( rc==SQLITE_RO
2a418 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  W ) return SQLIT
2a419 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 72 65 74  E_ERROR;.    ret
2a41a 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a  urn rc;.  }..  *
2a41b 70 69 64 78 20 3d 20 73 71 6c 69 74 65 33 5f 63  pidx = sqlite3_c
2a41c 6f 6c 75 6d 6e 5f 69 6e 74 28 73 2c 20 30 29 3b  olumn_int(s, 0);
2a41d 0a 0a 20 20 2f 2a 20 57 65 20 65 78 70 65 63 74  ..  /* We expect
2a41e 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77 2e 20 20   only one row.  
2a41f 57 65 20 6d 75 73 74 20 65 78 65 63 75 74 65 20  We must execute 
2a420 61 6e 6f 74 68 65 72 20 73 71 6c 69 74 65 33 5f  another sqlite3_
2a421 73 74 65 70 28 29 0a 20 20 20 2a 20 74 6f 20 63  step().   * to c
2a422 6f 6d 70 6c 65 74 65 20 74 68 65 20 69 74 65 72  omplete the iter
2a423 61 74 69 6f 6e 3b 20 6f 74 68 65 72 77 69 73 65  ation; otherwise
2a424 20 74 68 65 20 74 61 62 6c 65 20 77 69 6c 6c 20   the table will 
2a425 72 65 6d 61 69 6e 20 6c 6f 63 6b 65 64 2e 20 2a  remain locked. *
2a426 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  /.  rc = sqlite3
2a427 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20  _step(s);.  if( 
2a428 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  rc==SQLITE_ROW )
2a429 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
2a42a 52 52 4f 52 3b 0a 20 20 69 66 28 20 72 63 21 3d  RROR;.  if( rc!=
2a42b 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72 65  SQLITE_DONE ) re
2a42c 74 75 72 6e 20 72 63 3b 0a 20 20 72 65 74 75 72  turn rc;.  retur
2a42d 6e 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 7d 0a  n SQLITE_ROW;.}.
2a42e 0a 2f 2a 20 69 6e 73 65 72 74 20 69 6e 74 6f 20  ./* insert into 
2a42f 25 5f 73 65 67 64 69 72 20 76 61 6c 75 65 73 20  %_segdir values 
2a430 28 0a 2a 2a 20 20 20 5b 69 4c 65 76 65 6c 5d 2c  (.**   [iLevel],
2a431 20 5b 69 64 78 5d 2c 0a 2a 2a 20 20 20 5b 69 53   [idx],.**   [iS
2a432 74 61 72 74 42 6c 6f 63 6b 69 64 5d 2c 20 5b 69  tartBlockid], [i
2a433 4c 65 61 76 65 73 45 6e 64 42 6c 6f 63 6b 69 64  LeavesEndBlockid
2a434 5d 2c 20 5b 69 45 6e 64 42 6c 6f 63 6b 69 64 5d  ], [iEndBlockid]
2a435 2c 0a 2a 2a 20 20 20 5b 70 52 6f 6f 74 44 61 74  ,.**   [pRootDat
2a436 61 5d 0a 2a 2a 20 29 0a 2a 2f 0a 73 74 61 74 69  a].** ).*/.stati
2a437 63 20 69 6e 74 20 73 65 67 64 69 72 5f 73 65 74  c int segdir_set
2a438 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
2a439 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 2c 20 69  v, int iLevel, i
2a43a 6e 74 20 69 64 78 2c 0a 20 20 20 20 20 20 20 20  nt idx,.        
2a43b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
2a43c 6c 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61 72  lite_int64 iStar
2a43d 74 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20  tBlockid,.      
2a43e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a43f 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4c 65  sqlite_int64 iLe
2a440 61 76 65 73 45 6e 64 42 6c 6f 63 6b 69 64 2c 0a  avesEndBlockid,.
2a441 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a442 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74        sqlite_int
2a443 36 34 20 69 45 6e 64 42 6c 6f 63 6b 69 64 2c 0a  64 iEndBlockid,.
2a444 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a445 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
2a446 20 2a 70 52 6f 6f 74 44 61 74 61 2c 20 69 6e 74   *pRootData, int
2a447 20 6e 52 6f 6f 74 44 61 74 61 29 7b 0a 20 20 73   nRootData){.  s
2a448 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a  qlite3_stmt *s;.
2a449 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67    int rc = sql_g
2a44a 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
2a44b 53 45 47 44 49 52 5f 53 45 54 5f 53 54 4d 54 2c  SEGDIR_SET_STMT,
2a44c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d   &s);.  if( rc!=
2a44d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
2a44e 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73  rn rc;..  rc = s
2a44f 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28  qlite3_bind_int(
2a450 73 2c 20 31 2c 20 69 4c 65 76 65 6c 29 3b 0a 20  s, 1, iLevel);. 
2a451 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2a452 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
2a453 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
2a454 62 69 6e 64 5f 69 6e 74 28 73 2c 20 32 2c 20 69  bind_int(s, 2, i
2a455 64 78 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  dx);.  if( rc!=S
2a456 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
2a457 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
2a458 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
2a459 28 73 2c 20 33 2c 20 69 53 74 61 72 74 42 6c 6f  (s, 3, iStartBlo
2a45a 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21  ckid);.  if( rc!
2a45b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
2a45c 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
2a45d 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
2a45e 36 34 28 73 2c 20 34 2c 20 69 4c 65 61 76 65 73  64(s, 4, iLeaves
2a45f 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69  EndBlockid);.  i
2a460 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2a461 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
2a462 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
2a463 6e 64 5f 69 6e 74 36 34 28 73 2c 20 35 2c 20 69  nd_int64(s, 5, i
2a464 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69  EndBlockid);.  i
2a465 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2a466 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
2a467 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
2a468 6e 64 5f 62 6c 6f 62 28 73 2c 20 36 2c 20 70 52  nd_blob(s, 6, pR
2a469 6f 6f 74 44 61 74 61 2c 20 6e 52 6f 6f 74 44 61  ootData, nRootDa
2a46a 74 61 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  ta, SQLITE_STATI
2a46b 43 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  C);.  if( rc!=SQ
2a46c 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
2a46d 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 73   rc;..  return s
2a46e 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73  ql_single_step(s
2a46f 29 3b 0a 7d 0a 0a 2f 2a 20 51 75 65 72 69 65 73  );.}../* Queries
2a470 20 25 5f 73 65 67 64 69 72 20 66 6f 72 20 74 68   %_segdir for th
2a471 65 20 62 6c 6f 63 6b 20 73 70 61 6e 20 6f 66 20  e block span of 
2a472 74 68 65 20 73 65 67 6d 65 6e 74 73 20 69 6e 20  the segments in 
2a473 6c 65 76 65 6c 0a 2a 2a 20 69 4c 65 76 65 6c 2e  level.** iLevel.
2a474 20 20 52 65 74 75 72 6e 73 20 53 51 4c 49 54 45    Returns SQLITE
2a475 5f 44 4f 4e 45 20 69 66 20 74 68 65 72 65 20 61  _DONE if there a
2a476 72 65 20 6e 6f 20 62 6c 6f 63 6b 73 20 66 6f 72  re no blocks for
2a477 20 69 4c 65 76 65 6c 2c 0a 2a 2a 20 53 51 4c 49   iLevel,.** SQLI
2a478 54 45 5f 52 4f 57 20 69 66 20 74 68 65 72 65 20  TE_ROW if there 
2a479 61 72 65 20 62 6c 6f 63 6b 73 2c 20 65 6c 73 65  are blocks, else
2a47a 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74   an error..*/.st
2a47b 61 74 69 63 20 69 6e 74 20 73 65 67 64 69 72 5f  atic int segdir_
2a47c 73 70 61 6e 28 66 75 6c 6c 74 65 78 74 5f 76 74  span(fulltext_vt
2a47d 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65  ab *v, int iLeve
2a47e 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  l,.             
2a47f 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2a480 5f 69 6e 74 36 34 20 2a 70 69 53 74 61 72 74 42  _int64 *piStartB
2a481 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20 20  lockid,.        
2a482 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
2a483 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69 45  qlite_int64 *piE
2a484 6e 64 42 6c 6f 63 6b 69 64 29 7b 0a 20 20 73 71  ndBlockid){.  sq
2a485 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20  lite3_stmt *s;. 
2a486 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65   int rc = sql_ge
2a487 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 53  t_statement(v, S
2a488 45 47 44 49 52 5f 53 50 41 4e 5f 53 54 4d 54 2c  EGDIR_SPAN_STMT,
2a489 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d   &s);.  if( rc!=
2a48a 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
2a48b 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73  rn rc;..  rc = s
2a48c 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28  qlite3_bind_int(
2a48d 73 2c 20 31 2c 20 69 4c 65 76 65 6c 29 3b 0a 20  s, 1, iLevel);. 
2a48e 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2a48f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
2a490 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
2a491 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20 72  step(s);.  if( r
2a492 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29  c==SQLITE_DONE )
2a493 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44   return SQLITE_D
2a494 4f 4e 45 3b 20 20 2f 2a 20 53 68 6f 75 6c 64 20  ONE;  /* Should 
2a495 6e 65 76 65 72 20 68 61 70 70 65 6e 20 2a 2f 0a  never happen */.
2a496 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2a497 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20 72 63  _ROW ) return rc
2a498 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 68 61 70  ;..  /* This hap
2a499 70 65 6e 73 20 69 66 20 61 6c 6c 20 73 65 67 6d  pens if all segm
2a49a 65 6e 74 73 20 61 74 20 74 68 69 73 20 6c 65 76  ents at this lev
2a49b 65 6c 20 61 72 65 20 65 6e 74 69 72 65 6c 79 20  el are entirely 
2a49c 69 6e 6c 69 6e 65 2e 20 2a 2f 0a 20 20 69 66 28  inline. */.  if(
2a49d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73 71   SQLITE_NULL==sq
2a49e 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
2a49f 65 28 73 2c 20 30 29 20 29 7b 0a 20 20 20 20 2f  e(s, 0) ){.    /
2a4a0 2a 20 57 65 20 65 78 70 65 63 74 20 6f 6e 6c 79  * We expect only
2a4a1 20 6f 6e 65 20 72 6f 77 2e 20 20 57 65 20 6d 75   one row.  We mu
2a4a2 73 74 20 65 78 65 63 75 74 65 20 61 6e 6f 74 68  st execute anoth
2a4a3 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  er sqlite3_step(
2a4a4 29 0a 20 20 20 20 20 2a 20 74 6f 20 63 6f 6d 70  ).     * to comp
2a4a5 6c 65 74 65 20 74 68 65 20 69 74 65 72 61 74 69  lete the iterati
2a4a6 6f 6e 3b 20 6f 74 68 65 72 77 69 73 65 20 74 68  on; otherwise th
2a4a7 65 20 74 61 62 6c 65 20 77 69 6c 6c 20 72 65 6d  e table will rem
2a4a8 61 69 6e 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20  ain locked. */. 
2a4a9 20 20 20 69 6e 74 20 72 63 32 20 3d 20 73 71 6c     int rc2 = sql
2a4aa 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20  ite3_step(s);.  
2a4ab 20 20 69 66 28 20 72 63 32 3d 3d 53 51 4c 49 54    if( rc2==SQLIT
2a4ac 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20 53  E_ROW ) return S
2a4ad 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
2a4ae 20 72 65 74 75 72 6e 20 72 63 32 3b 0a 20 20 7d   return rc2;.  }
2a4af 0a 0a 20 20 2a 70 69 53 74 61 72 74 42 6c 6f 63  ..  *piStartBloc
2a4b0 6b 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  kid = sqlite3_co
2a4b1 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 30 29  lumn_int64(s, 0)
2a4b2 3b 0a 20 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69  ;.  *piEndBlocki
2a4b3 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  d = sqlite3_colu
2a4b4 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29 3b 0a  mn_int64(s, 1);.
2a4b5 0a 20 20 2f 2a 20 57 65 20 65 78 70 65 63 74 20  .  /* We expect 
2a4b6 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77 2e 20 20 57  only one row.  W
2a4b7 65 20 6d 75 73 74 20 65 78 65 63 75 74 65 20 61  e must execute a
2a4b8 6e 6f 74 68 65 72 20 73 71 6c 69 74 65 33 5f 73  nother sqlite3_s
2a4b9 74 65 70 28 29 0a 20 20 20 2a 20 74 6f 20 63 6f  tep().   * to co
2a4ba 6d 70 6c 65 74 65 20 74 68 65 20 69 74 65 72 61  mplete the itera
2a4bb 74 69 6f 6e 3b 20 6f 74 68 65 72 77 69 73 65 20  tion; otherwise 
2a4bc 74 68 65 20 74 61 62 6c 65 20 77 69 6c 6c 20 72  the table will r
2a4bd 65 6d 61 69 6e 20 6c 6f 63 6b 65 64 2e 20 2a 2f  emain locked. */
2a4be 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
2a4bf 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20 72  step(s);.  if( r
2a4c0 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20  c==SQLITE_ROW ) 
2a4c1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
2a4c2 52 4f 52 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  ROR;.  if( rc!=S
2a4c3 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72 65 74  QLITE_DONE ) ret
2a4c4 75 72 6e 20 72 63 3b 0a 20 20 72 65 74 75 72 6e  urn rc;.  return
2a4c5 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 7d 0a 0a   SQLITE_ROW;.}..
2a4c6 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 73 65  /* Delete the se
2a4c7 67 6d 65 6e 74 20 62 6c 6f 63 6b 73 20 61 6e 64  gment blocks and
2a4c8 20 73 65 67 6d 65 6e 74 20 64 69 72 65 63 74 6f   segment directo
2a4c9 72 79 20 72 65 63 6f 72 64 73 20 66 6f 72 20 61  ry records for a
2a4ca 6c 6c 0a 2a 2a 20 73 65 67 6d 65 6e 74 73 20 61  ll.** segments a
2a4cb 74 20 69 4c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61  t iLevel..*/.sta
2a4cc 74 69 63 20 69 6e 74 20 73 65 67 64 69 72 5f 64  tic int segdir_d
2a4cd 65 6c 65 74 65 28 66 75 6c 6c 74 65 78 74 5f 76  elete(fulltext_v
2a4ce 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76  tab *v, int iLev
2a4cf 65 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73  el){.  sqlite3_s
2a4d0 74 6d 74 20 2a 73 3b 0a 20 20 73 71 6c 69 74 65  tmt *s;.  sqlite
2a4d1 5f 69 6e 74 36 34 20 69 53 74 61 72 74 42 6c 6f  _int64 iStartBlo
2a4d2 63 6b 69 64 2c 20 69 45 6e 64 42 6c 6f 63 6b 69  ckid, iEndBlocki
2a4d3 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 65  d;.  int rc = se
2a4d4 67 64 69 72 5f 73 70 61 6e 28 76 2c 20 69 4c 65  gdir_span(v, iLe
2a4d5 76 65 6c 2c 20 26 69 53 74 61 72 74 42 6c 6f 63  vel, &iStartBloc
2a4d6 6b 69 64 2c 20 26 69 45 6e 64 42 6c 6f 63 6b 69  kid, &iEndBlocki
2a4d7 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  d);.  if( rc!=SQ
2a4d8 4c 49 54 45 5f 52 4f 57 20 26 26 20 72 63 21 3d  LITE_ROW && rc!=
2a4d9 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72 65  SQLITE_DONE ) re
2a4da 74 75 72 6e 20 72 63 3b 0a 0a 20 20 69 66 28 20  turn rc;..  if( 
2a4db 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  rc==SQLITE_ROW )
2a4dc 7b 0a 20 20 20 20 72 63 20 3d 20 62 6c 6f 63 6b  {.    rc = block
2a4dd 5f 64 65 6c 65 74 65 28 76 2c 20 69 53 74 61 72  _delete(v, iStar
2a4de 74 42 6c 6f 63 6b 69 64 2c 20 69 45 6e 64 42 6c  tBlockid, iEndBl
2a4df 6f 63 6b 69 64 29 3b 0a 20 20 20 20 69 66 28 20  ockid);.    if( 
2a4e0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2a4e1 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
2a4e2 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20    /* Delete the 
2a4e3 73 65 67 6d 65 6e 74 20 64 69 72 65 63 74 6f 72  segment director
2a4e4 79 20 69 74 73 65 6c 66 2e 20 2a 2f 0a 20 20 72  y itself. */.  r
2a4e5 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74  c = sql_get_stat
2a4e6 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49 52 5f  ement(v, SEGDIR_
2a4e7 44 45 4c 45 54 45 5f 53 54 4d 54 2c 20 26 73 29  DELETE_STMT, &s)
2a4e8 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
2a4e9 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
2a4ea 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  c;..  rc = sqlit
2a4eb 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c  e3_bind_int64(s,
2a4ec 20 31 2c 20 69 4c 65 76 65 6c 29 3b 0a 20 20 69   1, iLevel);.  i
2a4ed 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2a4ee 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
2a4ef 20 72 65 74 75 72 6e 20 73 71 6c 5f 73 69 6e 67   return sql_sing
2a4f0 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d 0a 0a 2f  le_step(s);.}../
2a4f1 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 63 6c  * TODO(shess) cl
2a4f2 65 61 72 50 65 6e 64 69 6e 67 54 65 72 6d 73 28  earPendingTerms(
2a4f3 29 20 69 73 20 66 61 72 20 64 6f 77 6e 20 74 68  ) is far down th
2a4f4 65 20 66 69 6c 65 20 62 65 63 61 75 73 65 0a 2a  e file because.*
2a4f5 2a 20 77 72 69 74 65 5a 65 72 6f 53 65 67 6d 65  * writeZeroSegme
2a4f6 6e 74 28 29 20 69 73 20 66 61 72 20 64 6f 77 6e  nt() is far down
2a4f7 20 74 68 65 20 66 69 6c 65 20 62 65 63 61 75 73   the file becaus
2a4f8 65 20 4c 65 61 66 57 72 69 74 65 72 20 69 73 20  e LeafWriter is 
2a4f9 66 61 72 0a 2a 2a 20 64 6f 77 6e 20 74 68 65 20  far.** down the 
2a4fa 66 69 6c 65 2e 20 20 43 6f 6e 73 69 64 65 72 20  file.  Consider 
2a4fb 72 65 66 61 63 74 6f 72 69 6e 67 20 74 68 65 20  refactoring the 
2a4fc 63 6f 64 65 20 74 6f 20 6d 6f 76 65 20 74 68 65  code to move the
2a4fd 20 6e 6f 6e 2d 76 74 61 62 0a 2a 2a 20 63 6f 64   non-vtab.** cod
2a4fe 65 20 61 62 6f 76 65 20 74 68 65 20 76 74 61 62  e above the vtab
2a4ff 20 63 6f 64 65 20 73 6f 20 74 68 61 74 20 77 65   code so that we
2a500 20 64 6f 6e 27 74 20 6e 65 65 64 20 74 68 69 73   don't need this
2a501 20 66 6f 72 77 61 72 64 0a 2a 2a 20 72 65 66 65   forward.** refe
2a502 72 65 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  rence..*/.static
2a503 20 69 6e 74 20 63 6c 65 61 72 50 65 6e 64 69 6e   int clearPendin
2a504 67 54 65 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f  gTerms(fulltext_
2a505 76 74 61 62 20 2a 76 29 3b 0a 0a 2f 2a 0a 2a 2a  vtab *v);../*.**
2a506 20 46 72 65 65 20 74 68 65 20 6d 65 6d 6f 72 79   Free the memory
2a507 20 75 73 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e   used to contain
2a508 20 61 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62   a fulltext_vtab
2a509 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73   structure..*/.s
2a50a 74 61 74 69 63 20 76 6f 69 64 20 66 75 6c 6c 74  tatic void fullt
2a50b 65 78 74 5f 76 74 61 62 5f 64 65 73 74 72 6f 79  ext_vtab_destroy
2a50c 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
2a50d 76 29 7b 0a 20 20 69 6e 74 20 69 53 74 6d 74 2c  v){.  int iStmt,
2a50e 20 69 3b 0a 0a 20 20 46 54 53 54 52 41 43 45 28   i;..  FTSTRACE(
2a50f 28 22 46 54 53 33 20 44 65 73 74 72 6f 79 20 25  ("FTS3 Destroy %
2a510 70 5c 6e 22 2c 20 76 29 29 3b 0a 20 20 66 6f 72  p\n", v));.  for
2a511 28 20 69 53 74 6d 74 3d 30 3b 20 69 53 74 6d 74  ( iStmt=0; iStmt
2a512 3c 4d 41 58 5f 53 54 4d 54 3b 20 69 53 74 6d 74  <MAX_STMT; iStmt
2a513 2b 2b 20 29 7b 0a 20 20 20 20 69 66 28 20 76 2d  ++ ){.    if( v-
2a514 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d  >pFulltextStatem
2a515 65 6e 74 73 5b 69 53 74 6d 74 5d 21 3d 4e 55 4c  ents[iStmt]!=NUL
2a516 4c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  L ){.      sqlit
2a517 65 33 5f 66 69 6e 61 6c 69 7a 65 28 76 2d 3e 70  e3_finalize(v->p
2a518 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d 65 6e  FulltextStatemen
2a519 74 73 5b 69 53 74 6d 74 5d 29 3b 0a 20 20 20 20  ts[iStmt]);.    
2a51a 20 20 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74    v->pFulltextSt
2a51b 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d 74 5d 20  atements[iStmt] 
2a51c 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20  = NULL;.    }.  
2a51d 7d 0a 0a 20 20 66 6f 72 28 20 69 3d 30 3b 20 69  }..  for( i=0; i
2a51e 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 3b 20 69 2b  <MERGE_COUNT; i+
2a51f 2b 20 29 7b 0a 20 20 20 20 69 66 28 20 76 2d 3e  + ){.    if( v->
2a520 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d 74 73  pLeafSelectStmts
2a521 5b 69 5d 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20  [i]!=NULL ){.   
2a522 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c     sqlite3_final
2a523 69 7a 65 28 76 2d 3e 70 4c 65 61 66 53 65 6c 65  ize(v->pLeafSele
2a524 63 74 53 74 6d 74 73 5b 69 5d 29 3b 0a 20 20 20  ctStmts[i]);.   
2a525 20 20 20 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63     v->pLeafSelec
2a526 74 53 74 6d 74 73 5b 69 5d 20 3d 20 4e 55 4c 4c  tStmts[i] = NULL
2a527 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69  ;.    }.  }..  i
2a528 66 28 20 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72  f( v->pTokenizer
2a529 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 76 2d  !=NULL ){.    v-
2a52a 3e 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f  >pTokenizer->pMo
2a52b 64 75 6c 65 2d 3e 78 44 65 73 74 72 6f 79 28 76  dule->xDestroy(v
2a52c 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20  ->pTokenizer);. 
2a52d 20 20 20 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72     v->pTokenizer
2a52e 20 3d 20 4e 55 4c 4c 3b 0a 20 20 7d 0a 0a 20 20   = NULL;.  }..  
2a52f 63 6c 65 61 72 50 65 6e 64 69 6e 67 54 65 72 6d  clearPendingTerm
2a530 73 28 76 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33  s(v);..  sqlite3
2a531 5f 66 72 65 65 28 76 2d 3e 61 7a 43 6f 6c 75 6d  _free(v->azColum
2a532 6e 29 3b 0a 20 20 66 6f 72 28 69 20 3d 20 30 3b  n);.  for(i = 0;
2a533 20 69 20 3c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b   i < v->nColumn;
2a534 20 2b 2b 69 29 20 7b 0a 20 20 20 20 73 71 6c 69   ++i) {.    sqli
2a535 74 65 33 5f 66 72 65 65 28 76 2d 3e 61 7a 43 6f  te3_free(v->azCo
2a536 6e 74 65 6e 74 43 6f 6c 75 6d 6e 5b 69 5d 29 3b  ntentColumn[i]);
2a537 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66  .  }.  sqlite3_f
2a538 72 65 65 28 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74  ree(v->azContent
2a539 43 6f 6c 75 6d 6e 29 3b 0a 20 20 73 71 6c 69 74  Column);.  sqlit
2a53a 65 33 5f 66 72 65 65 28 76 29 3b 0a 7d 0a 0a 2f  e3_free(v);.}../
2a53b 2a 0a 2a 2a 20 54 6f 6b 65 6e 20 74 79 70 65 73  *.** Token types
2a53c 20 66 6f 72 20 70 61 72 73 69 6e 67 20 74 68 65   for parsing the
2a53d 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 78 43   arguments to xC
2a53e 6f 6e 6e 65 63 74 20 6f 72 20 78 43 72 65 61 74  onnect or xCreat
2a53f 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4f  e..*/.#define TO
2a540 4b 45 4e 5f 45 4f 46 20 20 20 20 20 20 20 20 20  KEN_EOF         
2a541 30 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 66  0    /* End of f
2a542 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54  ile */.#define T
2a543 4f 4b 45 4e 5f 53 50 41 43 45 20 20 20 20 20 20  OKEN_SPACE      
2a544 20 31 20 20 20 20 2f 2a 20 41 6e 79 20 6b 69 6e   1    /* Any kin
2a545 64 20 6f 66 20 77 68 69 74 65 73 70 61 63 65 20  d of whitespace 
2a546 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4f 4b 45 4e  */.#define TOKEN
2a547 5f 49 44 20 20 20 20 20 20 20 20 20 20 32 20 20  _ID          2  
2a548 20 20 2f 2a 20 41 6e 20 69 64 65 6e 74 69 66 69    /* An identifi
2a549 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4f  er */.#define TO
2a54a 4b 45 4e 5f 53 54 52 49 4e 47 20 20 20 20 20 20  KEN_STRING      
2a54b 33 20 20 20 20 2f 2a 20 41 20 73 74 72 69 6e 67  3    /* A string
2a54c 20 6c 69 74 65 72 61 6c 20 2a 2f 0a 23 64 65 66   literal */.#def
2a54d 69 6e 65 20 54 4f 4b 45 4e 5f 50 55 4e 43 54 20  ine TOKEN_PUNCT 
2a54e 20 20 20 20 20 20 34 20 20 20 20 2f 2a 20 41 20        4    /* A 
2a54f 73 69 6e 67 6c 65 20 70 75 6e 63 74 75 61 74 69  single punctuati
2a550 6f 6e 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a  on character */.
2a551 0a 2f 2a 0a 2a 2a 20 49 66 20 58 20 69 73 20 61  ./*.** If X is a
2a552 20 63 68 61 72 61 63 74 65 72 20 74 68 61 74 20   character that 
2a553 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20 61  can be used in a
2a554 6e 20 69 64 65 6e 74 69 66 69 65 72 20 74 68 65  n identifier the
2a555 6e 0a 2a 2a 20 66 74 73 49 64 43 68 61 72 28 58  n.** ftsIdChar(X
2a556 29 20 77 69 6c 6c 20 62 65 20 74 72 75 65 2e 20  ) will be true. 
2a557 20 4f 74 68 65 72 77 69 73 65 20 69 74 20 69 73   Otherwise it is
2a558 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f   false..**.** Fo
2a559 72 20 41 53 43 49 49 2c 20 61 6e 79 20 63 68 61  r ASCII, any cha
2a55a 72 61 63 74 65 72 20 77 69 74 68 20 74 68 65 20  racter with the 
2a55b 68 69 67 68 2d 6f 72 64 65 72 20 62 69 74 20 73  high-order bit s
2a55c 65 74 20 69 73 0a 2a 2a 20 61 6c 6c 6f 77 65 64  et is.** allowed
2a55d 20 69 6e 20 61 6e 20 69 64 65 6e 74 69 66 69 65   in an identifie
2a55e 72 2e 20 20 46 6f 72 20 37 2d 62 69 74 20 63 68  r.  For 7-bit ch
2a55f 61 72 61 63 74 65 72 73 2c 20 0a 2a 2a 20 69 73  aracters, .** is
2a560 46 74 73 49 64 43 68 61 72 5b 58 5d 20 6d 75 73  FtsIdChar[X] mus
2a561 74 20 62 65 20 31 2e 0a 2a 2a 0a 2a 2a 20 54 69  t be 1..**.** Ti
2a562 63 6b 65 74 20 23 31 30 36 36 2e 20 20 74 68 65  cket #1066.  the
2a563 20 53 51 4c 20 73 74 61 6e 64 61 72 64 20 64 6f   SQL standard do
2a564 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 27 24 27  es not allow '$'
2a565 20 69 6e 20 74 68 65 0a 2a 2a 20 6d 69 64 64 6c   in the.** middl
2a566 65 20 6f 66 20 69 64 65 6e 74 66 69 65 72 73 2e  e of identfiers.
2a567 20 20 42 75 74 20 6d 61 6e 79 20 53 51 4c 20 69    But many SQL i
2a568 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 64  mplementations d
2a569 6f 2e 20 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69  o. .** SQLite wi
2a56a 6c 6c 20 61 6c 6c 6f 77 20 27 24 27 20 69 6e 20  ll allow '$' in 
2a56b 69 64 65 6e 74 69 66 69 65 72 73 20 66 6f 72 20  identifiers for 
2a56c 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a  compatibility..*
2a56d 2a 20 42 75 74 20 74 68 65 20 66 65 61 74 75 72  * But the featur
2a56e 65 20 69 73 20 75 6e 64 6f 63 75 6d 65 6e 74 65  e is undocumente
2a56f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  d..*/.static con
2a570 73 74 20 63 68 61 72 20 69 73 46 74 73 49 64 43  st char isFtsIdC
2a571 68 61 72 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30 20  har[] = {./* x0 
2a572 78 31 20 78 32 20 78 33 20 78 34 20 78 35 20 78  x1 x2 x3 x4 x5 x
2a573 36 20 78 37 20 78 38 20 78 39 20 78 41 20 78 42  6 x7 x8 x9 xA xB
2a574 20 78 43 20 78 44 20 78 45 20 78 46 20 2a 2f 0a   xC xD xE xF */.
2a575 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20      0, 0, 0, 0, 
2a576 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 0, 0, 0, 0, 0
2a577 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
2a578 20 30 2c 20 20 2f 2a 20 32 78 20 2a 2f 0a 20 20   0,  /* 2x */.  
2a579 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    1, 1, 1, 1, 1,
2a57a 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2a57b 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2a57c 2c 20 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 20  ,  /* 3x */.    
2a57d 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
2a57e 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2a57f 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2a580 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20 31 2c   /* 4x */.    1,
2a581 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2a582 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 1, 1, 1, 1, 0
2a583 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 20 2f  , 0, 0, 0, 1,  /
2a584 2a 20 35 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31  * 5x */.    0, 1
2a585 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2a586 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2a587 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20  1, 1, 1, 1,  /* 
2a588 36 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20  6x */.    1, 1, 
2a589 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2a58a 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c  , 1, 1, 1, 0, 0,
2a58b 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 37 78   0, 0, 0,  /* 7x
2a58c 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 66   */.};.#define f
2a58d 74 73 49 64 43 68 61 72 28 43 29 20 20 28 28 28  tsIdChar(C)  (((
2a58e 63 3d 43 29 26 30 78 38 30 29 21 3d 30 20 7c 7c  c=C)&0x80)!=0 ||
2a58f 20 28 63 3e 30 78 31 66 20 26 26 20 69 73 46 74   (c>0x1f && isFt
2a590 73 49 64 43 68 61 72 5b 63 2d 30 78 32 30 5d 29  sIdChar[c-0x20])
2a591 29 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  ).../*.** Return
2a592 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74   the length of t
2a593 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 62 65  he token that be
2a594 67 69 6e 73 20 61 74 20 7a 5b 30 5d 2e 20 0a 2a  gins at z[0]. .*
2a595 2a 20 53 74 6f 72 65 20 74 68 65 20 74 6f 6b 65  * Store the toke
2a596 6e 20 74 79 70 65 20 69 6e 20 2a 74 6f 6b 65 6e  n type in *token
2a597 54 79 70 65 20 62 65 66 6f 72 65 20 72 65 74 75  Type before retu
2a598 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63  rning..*/.static
2a599 20 69 6e 74 20 66 74 73 47 65 74 54 6f 6b 65 6e   int ftsGetToken
2a59a 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20  (const char *z, 
2a59b 69 6e 74 20 2a 74 6f 6b 65 6e 54 79 70 65 29 7b  int *tokenType){
2a59c 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 73  .  int i, c;.  s
2a59d 77 69 74 63 68 28 20 2a 7a 20 29 7b 0a 20 20 20  witch( *z ){.   
2a59e 20 63 61 73 65 20 30 3a 20 7b 0a 20 20 20 20 20   case 0: {.     
2a59f 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4f   *tokenType = TO
2a5a0 4b 45 4e 5f 45 4f 46 3b 0a 20 20 20 20 20 20 72  KEN_EOF;.      r
2a5a1 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20  eturn 0;.    }. 
2a5a2 20 20 20 63 61 73 65 20 27 20 27 3a 20 63 61 73     case ' ': cas
2a5a3 65 20 27 5c 74 27 3a 20 63 61 73 65 20 27 5c 6e  e '\t': case '\n
2a5a4 27 3a 20 63 61 73 65 20 27 5c 66 27 3a 20 63 61  ': case '\f': ca
2a5a5 73 65 20 27 5c 72 27 3a 20 7b 0a 20 20 20 20 20  se '\r': {.     
2a5a6 20 66 6f 72 28 69 3d 31 3b 20 73 61 66 65 5f 69   for(i=1; safe_i
2a5a7 73 73 70 61 63 65 28 7a 5b 69 5d 29 3b 20 69 2b  sspace(z[i]); i+
2a5a8 2b 29 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65  +){}.      *toke
2a5a9 6e 54 79 70 65 20 3d 20 54 4f 4b 45 4e 5f 53 50  nType = TOKEN_SP
2a5aa 41 43 45 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ACE;.      retur
2a5ab 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n i;.    }.    c
2a5ac 61 73 65 20 27 60 27 3a 0a 20 20 20 20 63 61 73  ase '`':.    cas
2a5ad 65 20 27 5c 27 27 3a 0a 20 20 20 20 63 61 73 65  e '\'':.    case
2a5ae 20 27 22 27 3a 20 7b 0a 20 20 20 20 20 20 69 6e   '"': {.      in
2a5af 74 20 64 65 6c 69 6d 20 3d 20 7a 5b 30 5d 3b 0a  t delim = z[0];.
2a5b0 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 28        for(i=1; (
2a5b1 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29  c=z[i])!=0; i++)
2a5b2 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 3d  {.        if( c=
2a5b3 3d 64 65 6c 69 6d 20 29 7b 0a 20 20 20 20 20 20  =delim ){.      
2a5b4 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d      if( z[i+1]==
2a5b5 64 65 6c 69 6d 20 29 7b 0a 20 20 20 20 20 20 20  delim ){.       
2a5b6 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20       i++;.      
2a5b7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2a5b8 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2a5b9 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2a5ba 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
2a5bb 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
2a5bc 4f 4b 45 4e 5f 53 54 52 49 4e 47 3b 0a 20 20 20  OKEN_STRING;.   
2a5bd 20 20 20 72 65 74 75 72 6e 20 69 20 2b 20 28 63     return i + (c
2a5be 21 3d 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  !=0);.    }.    
2a5bf 63 61 73 65 20 27 5b 27 3a 20 7b 0a 20 20 20 20  case '[': {.    
2a5c0 20 20 66 6f 72 28 69 3d 31 2c 20 63 3d 7a 5b 30    for(i=1, c=z[0
2a5c1 5d 3b 20 63 21 3d 27 5d 27 20 26 26 20 28 63 3d  ]; c!=']' && (c=
2a5c2 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 7d  z[i])!=0; i++){}
2a5c3 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70  .      *tokenTyp
2a5c4 65 20 3d 20 54 4f 4b 45 4e 5f 49 44 3b 0a 20 20  e = TOKEN_ID;.  
2a5c5 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20      return i;.  
2a5c6 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a    }.    default:
2a5c7 20 7b 0a 20 20 20 20 20 20 69 66 28 20 21 66 74   {.      if( !ft
2a5c8 73 49 64 43 68 61 72 28 2a 7a 29 20 29 7b 0a 20  sIdChar(*z) ){. 
2a5c9 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2a5ca 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28      }.      for(
2a5cb 69 3d 31 3b 20 66 74 73 49 64 43 68 61 72 28 7a  i=1; ftsIdChar(z
2a5cc 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20  [i]); i++){}.   
2a5cd 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
2a5ce 54 4f 4b 45 4e 5f 49 44 3b 0a 20 20 20 20 20 20  TOKEN_ID;.      
2a5cf 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a  return i;.    }.
2a5d0 20 20 7d 0a 20 20 2a 74 6f 6b 65 6e 54 79 70 65    }.  *tokenType
2a5d1 20 3d 20 54 4f 4b 45 4e 5f 50 55 4e 43 54 3b 0a   = TOKEN_PUNCT;.
2a5d2 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f    return 1;.}../
2a5d3 2a 0a 2a 2a 20 41 20 74 6f 6b 65 6e 20 65 78 74  *.** A token ext
2a5d4 72 61 63 74 65 64 20 66 72 6f 6d 20 61 20 73 74  racted from a st
2a5d5 72 69 6e 67 20 69 73 20 61 6e 20 69 6e 73 74 61  ring is an insta
2a5d6 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
2a5d7 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72  wing.** structur
2a5d8 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  e..*/.typedef st
2a5d9 72 75 63 74 20 46 74 73 54 6f 6b 65 6e 20 7b 0a  ruct FtsToken {.
2a5da 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b    const char *z;
2a5db 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
2a5dc 72 20 74 6f 20 74 6f 6b 65 6e 20 74 65 78 74 2e  r to token text.
2a5dd 20 20 4e 6f 74 20 27 5c 30 30 30 27 20 74 65 72    Not '\000' ter
2a5de 6d 69 6e 61 74 65 64 20 2a 2f 0a 20 20 73 68 6f  minated */.  sho
2a5df 72 74 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20  rt int n;       
2a5e0 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74    /* Length of t
2a5e1 68 65 20 74 6f 6b 65 6e 20 74 65 78 74 20 69 6e  he token text in
2a5e2 20 62 79 74 65 73 2e 20 2a 2f 0a 7d 20 46 74 73   bytes. */.} Fts
2a5e3 54 6f 6b 65 6e 3b 0a 0a 2f 2a 0a 2a 2a 20 47 69  Token;../*.** Gi
2a5e4 76 65 6e 20 61 20 69 6e 70 75 74 20 73 74 72 69  ven a input stri
2a5e5 6e 67 20 28 77 68 69 63 68 20 69 73 20 72 65 61  ng (which is rea
2a5e6 6c 6c 79 20 6f 6e 65 20 6f 66 20 74 68 65 20 61  lly one of the a
2a5e7 72 67 76 5b 5d 20 70 61 72 61 6d 65 74 65 72 73  rgv[] parameters
2a5e8 0a 2a 2a 20 70 61 73 73 65 64 20 69 6e 74 6f 20  .** passed into 
2a5e9 78 43 6f 6e 6e 65 63 74 20 6f 72 20 78 43 72 65  xConnect or xCre
2a5ea 61 74 65 29 20 73 70 6c 69 74 20 74 68 65 20 73  ate) split the s
2a5eb 74 72 69 6e 67 20 75 70 20 69 6e 74 6f 20 74 6f  tring up into to
2a5ec 6b 65 6e 73 2e 0a 2a 2a 20 52 65 74 75 72 6e 20  kens..** Return 
2a5ed 61 6e 20 61 72 72 61 79 20 6f 66 20 70 6f 69 6e  an array of poin
2a5ee 74 65 72 73 20 74 6f 20 27 5c 30 30 30 27 20 74  ters to '\000' t
2a5ef 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67  erminated string
2a5f0 73 2c 20 6f 6e 65 20 73 74 72 69 6e 67 0a 2a 2a  s, one string.**
2a5f1 20 66 6f 72 20 65 61 63 68 20 6e 6f 6e 2d 77 68   for each non-wh
2a5f2 69 74 65 73 70 61 63 65 20 74 6f 6b 65 6e 2e 0a  itespace token..
2a5f3 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  **.** The return
2a5f4 65 64 20 61 72 72 61 79 20 69 73 20 74 65 72 6d  ed array is term
2a5f5 69 6e 61 74 65 64 20 62 79 20 61 20 73 69 6e 67  inated by a sing
2a5f6 6c 65 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e  le NULL pointer.
2a5f7 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20  .**.** Space to 
2a5f8 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65  hold the returne
2a5f9 64 20 61 72 72 61 79 20 69 73 20 6f 62 74 61 69  d array is obtai
2a5fa 6e 65 64 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c  ned from a singl
2a5fb 65 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 61 6e 64 20  e.** malloc and 
2a5fc 73 68 6f 75 6c 64 20 62 65 20 66 72 65 65 64 20  should be freed 
2a5fd 62 79 20 70 61 73 73 69 6e 67 20 74 68 65 20 72  by passing the r
2a5fe 65 74 75 72 6e 20 76 61 6c 75 65 20 74 6f 20 66  eturn value to f
2a5ff 72 65 65 28 29 2e 0a 2a 2a 20 54 68 65 20 69 6e  ree()..** The in
2a600 64 69 76 69 64 75 61 6c 20 73 74 72 69 6e 67 73  dividual strings
2a601 20 77 69 74 68 69 6e 20 74 68 65 20 74 6f 6b 65   within the toke
2a602 6e 20 6c 69 73 74 20 61 72 65 20 61 6c 6c 20 61  n list are all a
2a603 20 70 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20   part of.** the 
2a604 73 69 6e 67 6c 65 20 6d 65 6d 6f 72 79 20 61 6c  single memory al
2a605 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 20 77 69 6c  location and wil
2a606 6c 20 61 6c 6c 20 62 65 20 66 72 65 65 64 20 61  l all be freed a
2a607 74 20 6f 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69  t once..*/.stati
2a608 63 20 63 68 61 72 20 2a 2a 74 6f 6b 65 6e 69 7a  c char **tokeniz
2a609 65 53 74 72 69 6e 67 28 63 6f 6e 73 74 20 63 68  eString(const ch
2a60a 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 70 6e 54 6f  ar *z, int *pnTo
2a60b 6b 65 6e 29 7b 0a 20 20 69 6e 74 20 6e 54 6f 6b  ken){.  int nTok
2a60c 65 6e 20 3d 20 30 3b 0a 20 20 46 74 73 54 6f 6b  en = 0;.  FtsTok
2a60d 65 6e 20 2a 61 54 6f 6b 65 6e 20 3d 20 73 71 6c  en *aToken = sql
2a60e 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 74 72  ite3_malloc( str
2a60f 6c 65 6e 28 7a 29 20 2a 20 73 69 7a 65 6f 66 28  len(z) * sizeof(
2a610 61 54 6f 6b 65 6e 5b 30 5d 29 20 29 3b 0a 20 20  aToken[0]) );.  
2a611 69 6e 74 20 6e 20 3d 20 31 3b 0a 20 20 69 6e 74  int n = 1;.  int
2a612 20 65 2c 20 69 3b 0a 20 20 69 6e 74 20 74 6f 74   e, i;.  int tot
2a613 61 6c 53 69 7a 65 20 3d 20 30 3b 0a 20 20 63 68  alSize = 0;.  ch
2a614 61 72 20 2a 2a 61 7a 54 6f 6b 65 6e 3b 0a 20 20  ar **azToken;.  
2a615 63 68 61 72 20 2a 7a 43 6f 70 79 3b 0a 20 20 77  char *zCopy;.  w
2a616 68 69 6c 65 28 20 6e 3e 30 20 29 7b 0a 20 20 20  hile( n>0 ){.   
2a617 20 6e 20 3d 20 66 74 73 47 65 74 54 6f 6b 65 6e   n = ftsGetToken
2a618 28 7a 2c 20 26 65 29 3b 0a 20 20 20 20 69 66 28  (z, &e);.    if(
2a619 20 65 21 3d 54 4f 4b 45 4e 5f 53 50 41 43 45 20   e!=TOKEN_SPACE 
2a61a 29 7b 0a 20 20 20 20 20 20 61 54 6f 6b 65 6e 5b  ){.      aToken[
2a61b 6e 54 6f 6b 65 6e 5d 2e 7a 20 3d 20 7a 3b 0a 20  nToken].z = z;. 
2a61c 20 20 20 20 20 61 54 6f 6b 65 6e 5b 6e 54 6f 6b       aToken[nTok
2a61d 65 6e 5d 2e 6e 20 3d 20 6e 3b 0a 20 20 20 20 20  en].n = n;.     
2a61e 20 6e 54 6f 6b 65 6e 2b 2b 3b 0a 20 20 20 20 20   nToken++;.     
2a61f 20 74 6f 74 61 6c 53 69 7a 65 20 2b 3d 20 6e 2b   totalSize += n+
2a620 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 20 2b  1;.    }.    z +
2a621 3d 20 6e 3b 0a 20 20 7d 0a 20 20 61 7a 54 6f 6b  = n;.  }.  azTok
2a622 65 6e 20 3d 20 28 63 68 61 72 2a 2a 29 73 71 6c  en = (char**)sql
2a623 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 6e 54 6f  ite3_malloc( nTo
2a624 6b 65 6e 2a 73 69 7a 65 6f 66 28 63 68 61 72 2a  ken*sizeof(char*
2a625 29 20 2b 20 74 6f 74 61 6c 53 69 7a 65 20 29 3b  ) + totalSize );
2a626 0a 20 20 7a 43 6f 70 79 20 3d 20 28 63 68 61 72  .  zCopy = (char
2a627 2a 29 26 61 7a 54 6f 6b 65 6e 5b 6e 54 6f 6b 65  *)&azToken[nToke
2a628 6e 5d 3b 0a 20 20 6e 54 6f 6b 65 6e 2d 2d 3b 0a  n];.  nToken--;.
2a629 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 54 6f    for(i=0; i<nTo
2a62a 6b 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61  ken; i++){.    a
2a62b 7a 54 6f 6b 65 6e 5b 69 5d 20 3d 20 7a 43 6f 70  zToken[i] = zCop
2a62c 79 3b 0a 20 20 20 20 6e 20 3d 20 61 54 6f 6b 65  y;.    n = aToke
2a62d 6e 5b 69 5d 2e 6e 3b 0a 20 20 20 20 6d 65 6d 63  n[i].n;.    memc
2a62e 70 79 28 7a 43 6f 70 79 2c 20 61 54 6f 6b 65 6e  py(zCopy, aToken
2a62f 5b 69 5d 2e 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a  [i].z, n);.    z
2a630 43 6f 70 79 5b 6e 5d 20 3d 20 30 3b 0a 20 20 20  Copy[n] = 0;.   
2a631 20 7a 43 6f 70 79 20 2b 3d 20 6e 2b 31 3b 0a 20   zCopy += n+1;. 
2a632 20 7d 0a 20 20 61 7a 54 6f 6b 65 6e 5b 6e 54 6f   }.  azToken[nTo
2a633 6b 65 6e 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69  ken] = 0;.  sqli
2a634 74 65 33 5f 66 72 65 65 28 61 54 6f 6b 65 6e 29  te3_free(aToken)
2a635 3b 0a 20 20 2a 70 6e 54 6f 6b 65 6e 20 3d 20 6e  ;.  *pnToken = n
2a636 54 6f 6b 65 6e 3b 0a 20 20 72 65 74 75 72 6e 20  Token;.  return 
2a637 61 7a 54 6f 6b 65 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a  azToken;.}../*.*
2a638 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20 53 51 4c  * Convert an SQL
2a639 2d 73 74 79 6c 65 20 71 75 6f 74 65 64 20 73 74  -style quoted st
2a63a 72 69 6e 67 20 69 6e 74 6f 20 61 20 6e 6f 72 6d  ring into a norm
2a63b 61 6c 20 73 74 72 69 6e 67 20 62 79 20 72 65 6d  al string by rem
2a63c 6f 76 69 6e 67 0a 2a 2a 20 74 68 65 20 71 75 6f  oving.** the quo
2a63d 74 65 20 63 68 61 72 61 63 74 65 72 73 2e 20 20  te characters.  
2a63e 54 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69  The conversion i
2a63f 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 2e  s done in-place.
2a640 20 20 49 66 20 74 68 65 0a 2a 2a 20 69 6e 70 75    If the.** inpu
2a641 74 20 64 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e  t does not begin
2a642 20 77 69 74 68 20 61 20 71 75 6f 74 65 20 63 68   with a quote ch
2a643 61 72 61 63 74 65 72 2c 20 74 68 65 6e 20 74 68  aracter, then th
2a644 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73  is routine.** is
2a645 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20   a no-op..**.** 
2a646 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20  Examples:.**.** 
2a647 20 20 20 20 22 61 62 63 22 20 20 20 62 65 63 6f      "abc"   beco
2a648 6d 65 73 20 20 20 61 62 63 0a 2a 2a 20 20 20 20  mes   abc.**    
2a649 20 27 78 79 7a 27 20 20 20 62 65 63 6f 6d 65 73   'xyz'   becomes
2a64a 20 20 20 78 79 7a 0a 2a 2a 20 20 20 20 20 5b 70     xyz.**     [p
2a64b 71 72 5d 20 20 20 62 65 63 6f 6d 65 73 20 20 20  qr]   becomes   
2a64c 70 71 72 0a 2a 2a 20 20 20 20 20 60 6d 6e 6f 60  pqr.**     `mno`
2a64d 20 20 20 62 65 63 6f 6d 65 73 20 20 20 6d 6e 6f     becomes   mno
2a64e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2a64f 64 65 71 75 6f 74 65 53 74 72 69 6e 67 28 63 68  dequoteString(ch
2a650 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 71 75  ar *z){.  int qu
2a651 6f 74 65 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  ote;.  int i, j;
2a652 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65  .  if( z==0 ) re
2a653 74 75 72 6e 3b 0a 20 20 71 75 6f 74 65 20 3d 20  turn;.  quote = 
2a654 7a 5b 30 5d 3b 0a 20 20 73 77 69 74 63 68 28 20  z[0];.  switch( 
2a655 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 63 61 73  quote ){.    cas
2a656 65 20 27 5c 27 27 3a 20 20 62 72 65 61 6b 3b 0a  e '\'':  break;.
2a657 20 20 20 20 63 61 73 65 20 27 22 27 3a 20 20 20      case '"':   
2a658 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
2a659 27 60 27 3a 20 20 20 62 72 65 61 6b 3b 20 20 20  '`':   break;   
2a65a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a65b 46 6f 72 20 4d 79 53 51 4c 20 63 6f 6d 70 61 74  For MySQL compat
2a65c 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 63  ibility */.    c
2a65d 61 73 65 20 27 5b 27 3a 20 20 20 71 75 6f 74 65  ase '[':   quote
2a65e 20 3d 20 27 5d 27 3b 20 20 62 72 65 61 6b 3b 20   = ']';  break; 
2a65f 20 2f 2a 20 46 6f 72 20 4d 53 20 53 71 6c 53 65   /* For MS SqlSe
2a660 72 76 65 72 20 63 6f 6d 70 61 74 69 62 69 6c 69  rver compatibili
2a661 74 79 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c  ty */.    defaul
2a662 74 3a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  t:    return;.  
2a663 7d 0a 20 20 66 6f 72 28 69 3d 31 2c 20 6a 3d 30  }.  for(i=1, j=0
2a664 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20  ; z[i]; i++){.  
2a665 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 71 75 6f 74    if( z[i]==quot
2a666 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a  e ){.      if( z
2a667 5b 69 2b 31 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a  [i+1]==quote ){.
2a668 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d          z[j++] =
2a669 20 71 75 6f 74 65 3b 0a 20 20 20 20 20 20 20 20   quote;.        
2a66a 69 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  i++;.      }else
2a66b 7b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d  {.        z[j++]
2a66c 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72   = 0;.        br
2a66d 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
2a66e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 5b   }else{.      z[
2a66f 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 20  j++] = z[i];.   
2a670 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
2a671 54 68 65 20 69 6e 70 75 74 20 61 7a 49 6e 20 69  The input azIn i
2a672 73 20 61 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e 61  s a NULL-termina
2a673 74 65 64 20 6c 69 73 74 20 6f 66 20 74 6f 6b 65  ted list of toke
2a674 6e 73 2e 20 20 52 65 6d 6f 76 65 20 74 68 65 20  ns.  Remove the 
2a675 66 69 72 73 74 0a 2a 2a 20 74 6f 6b 65 6e 20 61  first.** token a
2a676 6e 64 20 61 6c 6c 20 70 75 6e 63 74 75 61 74 69  nd all punctuati
2a677 6f 6e 20 74 6f 6b 65 6e 73 2e 20 20 52 65 6d 6f  on tokens.  Remo
2a678 76 65 20 74 68 65 20 71 75 6f 74 65 73 20 66 72  ve the quotes fr
2a679 6f 6d 0a 2a 2a 20 61 72 6f 75 6e 64 20 73 74 72  om.** around str
2a67a 69 6e 67 20 6c 69 74 65 72 61 6c 20 74 6f 6b 65  ing literal toke
2a67b 6e 73 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c  ns..**.** Exampl
2a67c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 69 6e 70  e:.**.**     inp
2a67d 75 74 3a 20 20 20 20 20 20 74 6f 6b 65 6e 69 7a  ut:      tokeniz
2a67e 65 20 63 68 69 6e 65 73 65 20 28 20 27 73 69 6d  e chinese ( 'sim
2a67f 70 6c 69 66 65 64 27 20 2c 20 27 6d 69 78 65 64  plifed' , 'mixed
2a680 27 20 29 0a 2a 2a 20 20 20 20 20 6f 75 74 70 75  ' ).**     outpu
2a681 74 3a 20 20 20 20 20 63 68 69 6e 65 73 65 20 73  t:     chinese s
2a682 69 6d 70 6c 69 66 65 64 20 6d 69 78 65 64 0a 2a  implifed mixed.*
2a683 2a 0a 2a 2a 20 41 6e 6f 74 68 65 72 20 65 78 61  *.** Another exa
2a684 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  mple:.**.**     
2a685 69 6e 70 75 74 3a 20 20 20 20 20 20 64 65 6c 69  input:      deli
2a686 6d 69 74 65 72 73 20 28 20 27 5b 27 20 2c 20 27  miters ( '[' , '
2a687 5d 27 20 2c 20 27 2e 2e 2e 27 20 29 0a 2a 2a 20  ]' , '...' ).** 
2a688 20 20 20 20 6f 75 74 70 75 74 3a 20 20 20 20 20      output:     
2a689 5b 20 5d 20 2e 2e 2e 0a 2a 2f 0a 73 74 61 74 69  [ ] ....*/.stati
2a68a 63 20 76 6f 69 64 20 74 6f 6b 65 6e 4c 69 73 74  c void tokenList
2a68b 54 6f 49 64 4c 69 73 74 28 63 68 61 72 20 2a 2a  ToIdList(char **
2a68c 61 7a 49 6e 29 7b 0a 20 20 69 6e 74 20 69 2c 20  azIn){.  int i, 
2a68d 6a 3b 0a 20 20 69 66 28 20 61 7a 49 6e 20 29 7b  j;.  if( azIn ){
2a68e 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d  .    for(i=0, j=
2a68f 2d 31 3b 20 61 7a 49 6e 5b 69 5d 3b 20 69 2b 2b  -1; azIn[i]; i++
2a690 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 61 66  ){.      if( saf
2a691 65 5f 69 73 61 6c 6e 75 6d 28 61 7a 49 6e 5b 69  e_isalnum(azIn[i
2a692 5d 5b 30 5d 29 20 7c 7c 20 61 7a 49 6e 5b 69 5d  ][0]) || azIn[i]
2a693 5b 31 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 64  [1] ){.        d
2a694 65 71 75 6f 74 65 53 74 72 69 6e 67 28 61 7a 49  equoteString(azI
2a695 6e 5b 69 5d 29 3b 0a 20 20 20 20 20 20 20 20 69  n[i]);.        i
2a696 66 28 20 6a 3e 3d 30 20 29 7b 0a 20 20 20 20 20  f( j>=0 ){.     
2a697 20 20 20 20 20 61 7a 49 6e 5b 6a 5d 20 3d 20 61       azIn[j] = a
2a698 7a 49 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20  zIn[i];.        
2a699 7d 0a 20 20 20 20 20 20 20 20 6a 2b 2b 3b 0a 20  }.        j++;. 
2a69a 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
2a69b 20 61 7a 49 6e 5b 6a 5d 20 3d 20 30 3b 0a 20 20   azIn[j] = 0;.  
2a69c 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64  }.}.../*.** Find
2a69d 20 74 68 65 20 66 69 72 73 74 20 61 6c 70 68 61   the first alpha
2a69e 6e 75 6d 65 72 69 63 20 74 6f 6b 65 6e 20 69 6e  numeric token in
2a69f 20 74 68 65 20 73 74 72 69 6e 67 20 7a 49 6e 2e   the string zIn.
2a6a0 20 20 4e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65    Null-terminate
2a6a1 0a 2a 2a 20 74 68 69 73 20 74 6f 6b 65 6e 2e 20  .** this token. 
2a6a2 20 52 65 6d 6f 76 65 20 61 6e 79 20 71 75 6f 74   Remove any quot
2a6a3 61 74 69 6f 6e 20 6d 61 72 6b 73 2e 20 20 41 6e  ation marks.  An
2a6a4 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  d return a point
2a6a5 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 72 65 73  er to.** the res
2a6a6 75 6c 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ult..*/.static c
2a6a7 68 61 72 20 2a 66 69 72 73 74 54 6f 6b 65 6e 28  har *firstToken(
2a6a8 63 68 61 72 20 2a 7a 49 6e 2c 20 63 68 61 72 20  char *zIn, char 
2a6a9 2a 2a 70 7a 54 61 69 6c 29 7b 0a 20 20 69 6e 74  **pzTail){.  int
2a6aa 20 6e 2c 20 74 74 79 70 65 3b 0a 20 20 77 68 69   n, ttype;.  whi
2a6ab 6c 65 28 31 29 7b 0a 20 20 20 20 6e 20 3d 20 66  le(1){.    n = f
2a6ac 74 73 47 65 74 54 6f 6b 65 6e 28 7a 49 6e 2c 20  tsGetToken(zIn, 
2a6ad 26 74 74 79 70 65 29 3b 0a 20 20 20 20 69 66 28  &ttype);.    if(
2a6ae 20 74 74 79 70 65 3d 3d 54 4f 4b 45 4e 5f 53 50   ttype==TOKEN_SP
2a6af 41 43 45 20 29 7b 0a 20 20 20 20 20 20 7a 49 6e  ACE ){.      zIn
2a6b0 20 2b 3d 20 6e 3b 0a 20 20 20 20 7d 65 6c 73 65   += n;.    }else
2a6b1 20 69 66 28 20 74 74 79 70 65 3d 3d 54 4f 4b 45   if( ttype==TOKE
2a6b2 4e 5f 45 4f 46 20 29 7b 0a 20 20 20 20 20 20 2a  N_EOF ){.      *
2a6b3 70 7a 54 61 69 6c 20 3d 20 7a 49 6e 3b 0a 20 20  pzTail = zIn;.  
2a6b4 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
2a6b5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a    }else{.      z
2a6b6 49 6e 5b 6e 5d 20 3d 20 30 3b 0a 20 20 20 20 20  In[n] = 0;.     
2a6b7 20 2a 70 7a 54 61 69 6c 20 3d 20 26 7a 49 6e 5b   *pzTail = &zIn[
2a6b8 31 5d 3b 0a 20 20 20 20 20 20 64 65 71 75 6f 74  1];.      dequot
2a6b9 65 53 74 72 69 6e 67 28 7a 49 6e 29 3b 0a 20 20  eString(zIn);.  
2a6ba 20 20 20 20 72 65 74 75 72 6e 20 7a 49 6e 3b 0a      return zIn;.
2a6bb 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 4e 4f      }.  }.  /*NO
2a6bc 54 52 45 41 43 48 45 44 2a 2f 0a 7d 0a 0a 2f 2a  TREACHED*/.}../*
2a6bd 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 2e   Return true if.
2a6be 2e 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 73 20  ...**.**   *  s 
2a6bf 62 65 67 69 6e 73 20 77 69 74 68 20 74 68 65 20  begins with the 
2a6c0 73 74 72 69 6e 67 20 74 2c 20 69 67 6e 6f 72 69  string t, ignori
2a6c1 6e 67 20 63 61 73 65 0a 2a 2a 20 20 20 2a 20 20  ng case.**   *  
2a6c2 73 20 69 73 20 6c 6f 6e 67 65 72 20 74 68 61 6e  s is longer than
2a6c3 20 74 0a 2a 2a 20 20 20 2a 20 20 54 68 65 20 66   t.**   *  The f
2a6c4 69 72 73 74 20 63 68 61 72 61 63 74 65 72 20 6f  irst character o
2a6c5 66 20 73 20 62 65 79 6f 6e 64 20 74 20 69 73 20  f s beyond t is 
2a6c6 6e 6f 74 20 61 20 61 6c 70 68 61 6e 75 6d 65 72  not a alphanumer
2a6c7 69 63 0a 2a 2a 20 0a 2a 2a 20 49 67 6e 6f 72 65  ic.** .** Ignore
2a6c8 20 6c 65 61 64 69 6e 67 20 73 70 61 63 65 20 69   leading space i
2a6c9 6e 20 2a 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 70  n *s..**.** To p
2a6ca 75 74 20 69 74 20 61 6e 6f 74 68 65 72 20 77 61  ut it another wa
2a6cb 79 2c 20 72 65 74 75 72 6e 20 74 72 75 65 20 69  y, return true i
2a6cc 66 20 74 68 65 20 66 69 72 73 74 20 74 6f 6b 65  f the first toke
2a6cd 6e 20 6f 66 0a 2a 2a 20 73 5b 5d 20 69 73 20 74  n of.** s[] is t
2a6ce 5b 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  []..*/.static in
2a6cf 74 20 73 74 61 72 74 73 57 69 74 68 28 63 6f 6e  t startsWith(con
2a6d0 73 74 20 63 68 61 72 20 2a 73 2c 20 63 6f 6e 73  st char *s, cons
2a6d1 74 20 63 68 61 72 20 2a 74 29 7b 0a 20 20 77 68  t char *t){.  wh
2a6d2 69 6c 65 28 20 73 61 66 65 5f 69 73 73 70 61 63  ile( safe_isspac
2a6d3 65 28 2a 73 29 20 29 7b 20 73 2b 2b 3b 20 7d 0a  e(*s) ){ s++; }.
2a6d4 20 20 77 68 69 6c 65 28 20 2a 74 20 29 7b 0a 20    while( *t ){. 
2a6d5 20 20 20 69 66 28 20 73 61 66 65 5f 74 6f 6c 6f     if( safe_tolo
2a6d6 77 65 72 28 2a 73 2b 2b 29 21 3d 73 61 66 65 5f  wer(*s++)!=safe_
2a6d7 74 6f 6c 6f 77 65 72 28 2a 74 2b 2b 29 20 29 20  tolower(*t++) ) 
2a6d8 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
2a6d9 72 65 74 75 72 6e 20 2a 73 21 3d 27 5f 27 20 26  return *s!='_' &
2a6da 26 20 21 73 61 66 65 5f 69 73 61 6c 6e 75 6d 28  & !safe_isalnum(
2a6db 2a 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e  *s);.}../*.** An
2a6dc 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
2a6dd 73 20 73 74 72 75 63 74 75 72 65 20 64 65 66 69  s structure defi
2a6de 6e 65 73 20 74 68 65 20 22 73 70 65 63 22 20 6f  nes the "spec" o
2a6df 66 20 61 0a 2a 2a 20 66 75 6c 6c 20 74 65 78 74  f a.** full text
2a6e0 20 69 6e 64 65 78 2e 20 20 54 68 69 73 20 73 74   index.  This st
2a6e1 72 75 63 74 75 72 65 20 69 73 20 70 6f 70 75 6c  ructure is popul
2a6e2 61 74 65 64 20 62 79 20 70 61 72 73 65 53 70 65  ated by parseSpe
2a6e3 63 0a 2a 2a 20 61 6e 64 20 75 73 65 20 62 79 20  c.** and use by 
2a6e4 66 75 6c 6c 74 65 78 74 43 6f 6e 6e 65 63 74 20  fulltextConnect 
2a6e5 61 6e 64 20 66 75 6c 6c 74 65 78 74 43 72 65 61  and fulltextCrea
2a6e6 74 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  te..*/.typedef s
2a6e7 74 72 75 63 74 20 54 61 62 6c 65 53 70 65 63 20  truct TableSpec 
2a6e8 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  {.  const char *
2a6e9 7a 44 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  zDb;         /* 
2a6ea 4c 6f 67 69 63 61 6c 20 64 61 74 61 62 61 73 65  Logical database
2a6eb 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74   name */.  const
2a6ec 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
2a6ed 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
2a6ee 68 65 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64  he full-text ind
2a6ef 65 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c  ex */.  int nCol
2a6f0 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  umn;            
2a6f1 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f   /* Number of co
2a6f2 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e 64 65  lumns to be inde
2a6f3 78 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  xed */.  char **
2a6f4 61 7a 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20  azColumn;       
2a6f5 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6e 61    /* Original na
2a6f6 6d 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74  mes of columns t
2a6f7 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f 0a  o be indexed */.
2a6f8 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6e 74 65    char **azConte
2a6f9 6e 74 43 6f 6c 75 6d 6e 3b 20 20 2f 2a 20 43 6f  ntColumn;  /* Co
2a6fa 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 6f 72 20 25  lumn names for %
2a6fb 5f 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 63 68  _content */.  ch
2a6fc 61 72 20 2a 2a 61 7a 54 6f 6b 65 6e 69 7a 65 72  ar **azTokenizer
2a6fd 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  ;      /* Name o
2a6fe 66 20 74 6f 6b 65 6e 69 7a 65 72 20 61 6e 64 20  f tokenizer and 
2a6ff 69 74 73 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f  its arguments */
2a700 0a 7d 20 54 61 62 6c 65 53 70 65 63 3b 0a 0a 2f  .} TableSpec;../
2a701 2a 0a 2a 2a 20 52 65 63 6c 61 69 6d 20 61 6c 6c  *.** Reclaim all
2a702 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 75   of the memory u
2a703 73 65 64 20 62 79 20 61 20 54 61 62 6c 65 53 70  sed by a TableSp
2a704 65 63 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ec.*/.static voi
2a705 64 20 63 6c 65 61 72 54 61 62 6c 65 53 70 65 63  d clearTableSpec
2a706 28 54 61 62 6c 65 53 70 65 63 20 2a 70 29 20 7b  (TableSpec *p) {
2a707 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
2a708 70 2d 3e 61 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 20  p->azColumn);.  
2a709 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e  sqlite3_free(p->
2a70a 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29  azContentColumn)
2a70b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
2a70c 28 70 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72 29  (p->azTokenizer)
2a70d 3b 0a 7d 0a 0a 2f 2a 20 50 61 72 73 65 20 61 20  ;.}../* Parse a 
2a70e 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54  CREATE VIRTUAL T
2a70f 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2c 20  ABLE statement, 
2a710 77 68 69 63 68 20 6c 6f 6f 6b 73 20 6c 69 6b 65  which looks like
2a711 20 74 68 69 73 3a 0a 20 2a 0a 20 2a 20 43 52 45   this:. *. * CRE
2a712 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c  ATE VIRTUAL TABL
2a713 45 20 65 6d 61 69 6c 0a 20 2a 20 20 20 20 20 20  E email. *      
2a714 20 20 55 53 49 4e 47 20 66 74 73 33 28 73 75 62    USING fts3(sub
2a715 6a 65 63 74 2c 20 62 6f 64 79 2c 20 74 6f 6b 65  ject, body, toke
2a716 6e 69 7a 65 20 6d 79 74 6f 6b 65 6e 69 7a 65 72  nize mytokenizer
2a717 28 6d 79 61 72 67 29 29 0a 20 2a 0a 20 2a 20 57  (myarg)). *. * W
2a718 65 20 72 65 74 75 72 6e 20 70 61 72 73 65 64 20  e return parsed 
2a719 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 61  information in a
2a71a 20 54 61 62 6c 65 53 70 65 63 20 73 74 72 75 63   TableSpec struc
2a71b 74 75 72 65 2e 0a 20 2a 20 0a 20 2a 2f 0a 73 74  ture.. * . */.st
2a71c 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 53 70  atic int parseSp
2a71d 65 63 28 54 61 62 6c 65 53 70 65 63 20 2a 70 53  ec(TableSpec *pS
2a71e 70 65 63 2c 20 69 6e 74 20 61 72 67 63 2c 20 63  pec, int argc, c
2a71f 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74  onst char *const
2a720 2a 61 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20  *argv,.         
2a721 20 20 20 20 20 20 20 20 20 20 20 20 63 68 61 72              char
2a722 2a 2a 70 7a 45 72 72 29 7b 0a 20 20 69 6e 74 20  **pzErr){.  int 
2a723 69 2c 20 6e 3b 0a 20 20 63 68 61 72 20 2a 7a 2c  i, n;.  char *z,
2a724 20 2a 7a 44 75 6d 6d 79 3b 0a 20 20 63 68 61 72   *zDummy;.  char
2a725 20 2a 2a 61 7a 41 72 67 3b 0a 20 20 63 6f 6e 73   **azArg;.  cons
2a726 74 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 69 7a  t char *zTokeniz
2a727 65 72 20 3d 20 30 3b 20 20 20 20 2f 2a 20 61 72  er = 0;    /* ar
2a728 67 76 5b 5d 20 65 6e 74 72 79 20 64 65 73 63 72  gv[] entry descr
2a729 69 62 69 6e 67 20 74 68 65 20 74 6f 6b 65 6e 69  ibing the tokeni
2a72a 7a 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  zer */..  assert
2a72b 28 20 61 72 67 63 3e 3d 33 20 29 3b 0a 20 20 2f  ( argc>=3 );.  /
2a72c 2a 20 43 75 72 72 65 6e 74 20 69 6e 74 65 72 66  * Current interf
2a72d 61 63 65 3a 0a 20 20 2a 2a 20 61 72 67 76 5b 30  ace:.  ** argv[0
2a72e 5d 20 2d 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 0a  ] - module name.
2a72f 20 20 2a 2a 20 61 72 67 76 5b 31 5d 20 2d 20 64    ** argv[1] - d
2a730 61 74 61 62 61 73 65 20 6e 61 6d 65 0a 20 20 2a  atabase name.  *
2a731 2a 20 61 72 67 76 5b 32 5d 20 2d 20 74 61 62 6c  * argv[2] - tabl
2a732 65 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 72 67 76  e name.  ** argv
2a733 5b 33 2e 2e 5d 20 2d 20 63 6f 6c 75 6d 6e 73 2c  [3..] - columns,
2a734 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 66 6f 6c 6c   optionally foll
2a735 6f 77 65 64 20 62 79 20 74 6f 6b 65 6e 69 7a 65  owed by tokenize
2a736 72 20 73 70 65 63 69 66 69 63 61 74 69 6f 6e 0a  r specification.
2a737 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20    **            
2a738 20 61 6e 64 20 73 6e 69 70 70 65 74 20 64 65 6c   and snippet del
2a739 69 6d 69 74 65 72 73 20 73 70 65 63 69 66 69 63  imiters specific
2a73a 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 0a 20 20 2f  ation..  */..  /
2a73b 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66  * Make a copy of
2a73c 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 61 72   the complete ar
2a73d 67 76 5b 5d 5b 5d 20 61 72 72 61 79 20 69 6e 20  gv[][] array in 
2a73e 61 20 73 69 6e 67 6c 65 20 61 6c 6c 6f 63 61 74  a single allocat
2a73f 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 65 20 61 72  ion..  ** The ar
2a740 67 76 5b 5d 5b 5d 20 61 72 72 61 79 20 69 73 20  gv[][] array is 
2a741 72 65 61 64 2d 6f 6e 6c 79 20 61 6e 64 20 74 72  read-only and tr
2a742 61 6e 73 69 65 6e 74 2e 20 20 57 65 20 63 61 6e  ansient.  We can
2a743 20 77 72 69 74 65 20 74 6f 20 74 68 65 0a 20 20   write to the.  
2a744 2a 2a 20 63 6f 70 79 20 69 6e 20 6f 72 64 65 72  ** copy in order
2a745 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 69 6e 67   to modify thing
2a746 73 20 61 6e 64 20 74 68 65 20 63 6f 70 79 20 69  s and the copy i
2a747 73 20 70 65 72 73 69 73 74 65 6e 74 2e 0a 20 20  s persistent..  
2a748 2a 2f 0a 20 20 43 4c 45 41 52 28 70 53 70 65 63  */.  CLEAR(pSpec
2a749 29 3b 0a 20 20 66 6f 72 28 69 3d 6e 3d 30 3b 20  );.  for(i=n=0; 
2a74a 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20  i<argc; i++){.  
2a74b 20 20 6e 20 2b 3d 20 73 74 72 6c 65 6e 28 61 72    n += strlen(ar
2a74c 67 76 5b 69 5d 29 20 2b 20 31 3b 0a 20 20 7d 0a  gv[i]) + 1;.  }.
2a74d 20 20 61 7a 41 72 67 20 3d 20 73 71 6c 69 74 65    azArg = sqlite
2a74e 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66  3_malloc( sizeof
2a74f 28 63 68 61 72 2a 29 2a 61 72 67 63 20 2b 20 6e  (char*)*argc + n
2a750 20 29 3b 0a 20 20 69 66 28 20 61 7a 41 72 67 3d   );.  if( azArg=
2a751 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
2a752 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
2a753 20 7d 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a 29   }.  z = (char*)
2a754 26 61 7a 41 72 67 5b 61 72 67 63 5d 3b 0a 20 20  &azArg[argc];.  
2a755 66 6f 72 28 69 3d 30 3b 20 69 3c 61 72 67 63 3b  for(i=0; i<argc;
2a756 20 69 2b 2b 29 7b 0a 20 20 20 20 61 7a 41 72 67   i++){.    azArg
2a757 5b 69 5d 20 3d 20 7a 3b 0a 20 20 20 20 73 74 72  [i] = z;.    str
2a758 63 70 79 28 7a 2c 20 61 72 67 76 5b 69 5d 29 3b  cpy(z, argv[i]);
2a759 0a 20 20 20 20 7a 20 2b 3d 20 73 74 72 6c 65 6e  .    z += strlen
2a75a 28 7a 29 2b 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  (z)+1;.  }..  /*
2a75b 20 49 64 65 6e 74 69 66 79 20 74 68 65 20 63 6f   Identify the co
2a75c 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 6e 64 20 74  lumn names and t
2a75d 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 61 6e 64  he tokenizer and
2a75e 20 64 65 6c 69 6d 69 74 65 72 20 61 72 67 75 6d   delimiter argum
2a75f 65 6e 74 73 0a 20 20 2a 2a 20 69 6e 20 74 68 65  ents.  ** in the
2a760 20 61 72 67 76 5b 5d 5b 5d 20 61 72 72 61 79 2e   argv[][] array.
2a761 0a 20 20 2a 2f 0a 20 20 70 53 70 65 63 2d 3e 7a  .  */.  pSpec->z
2a762 44 62 20 3d 20 61 7a 41 72 67 5b 31 5d 3b 0a 20  Db = azArg[1];. 
2a763 20 70 53 70 65 63 2d 3e 7a 4e 61 6d 65 20 3d 20   pSpec->zName = 
2a764 61 7a 41 72 67 5b 32 5d 3b 0a 20 20 70 53 70 65  azArg[2];.  pSpe
2a765 63 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 30 3b 0a  c->nColumn = 0;.
2a766 20 20 70 53 70 65 63 2d 3e 61 7a 43 6f 6c 75 6d    pSpec->azColum
2a767 6e 20 3d 20 61 7a 41 72 67 3b 0a 20 20 7a 54 6f  n = azArg;.  zTo
2a768 6b 65 6e 69 7a 65 72 20 3d 20 22 74 6f 6b 65 6e  kenizer = "token
2a769 69 7a 65 20 73 69 6d 70 6c 65 22 3b 0a 20 20 66  ize simple";.  f
2a76a 6f 72 28 69 3d 33 3b 20 69 3c 61 72 67 63 3b 20  or(i=3; i<argc; 
2a76b 2b 2b 69 29 7b 0a 20 20 20 20 69 66 28 20 73 74  ++i){.    if( st
2a76c 61 72 74 73 57 69 74 68 28 61 7a 41 72 67 5b 69  artsWith(azArg[i
2a76d 5d 2c 22 74 6f 6b 65 6e 69 7a 65 22 29 20 29 7b  ],"tokenize") ){
2a76e 0a 20 20 20 20 20 20 7a 54 6f 6b 65 6e 69 7a 65  .      zTokenize
2a76f 72 20 3d 20 61 7a 41 72 67 5b 69 5d 3b 0a 20 20  r = azArg[i];.  
2a770 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a    }else{.      z
2a771 20 3d 20 61 7a 41 72 67 5b 70 53 70 65 63 2d 3e   = azArg[pSpec->
2a772 6e 43 6f 6c 75 6d 6e 5d 20 3d 20 66 69 72 73 74  nColumn] = first
2a773 54 6f 6b 65 6e 28 61 7a 41 72 67 5b 69 5d 2c 20  Token(azArg[i], 
2a774 26 7a 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20  &zDummy);.      
2a775 70 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 2b 2b  pSpec->nColumn++
2a776 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
2a777 28 20 70 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e  ( pSpec->nColumn
2a778 3d 3d 30 20 29 7b 0a 20 20 20 20 61 7a 41 72 67  ==0 ){.    azArg
2a779 5b 30 5d 20 3d 20 22 63 6f 6e 74 65 6e 74 22 3b  [0] = "content";
2a77a 0a 20 20 20 20 70 53 70 65 63 2d 3e 6e 43 6f 6c  .    pSpec->nCol
2a77b 75 6d 6e 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20  umn = 1;.  }..  
2a77c 2f 2a 0a 20 20 2a 2a 20 43 6f 6e 73 74 72 75 63  /*.  ** Construc
2a77d 74 20 74 68 65 20 6c 69 73 74 20 6f 66 20 63 6f  t the list of co
2a77e 6e 74 65 6e 74 20 63 6f 6c 75 6d 6e 20 6e 61 6d  ntent column nam
2a77f 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 61  es..  **.  ** Ea
2a780 63 68 20 63 6f 6e 74 65 6e 74 20 63 6f 6c 75 6d  ch content colum
2a781 6e 20 6e 61 6d 65 20 77 69 6c 6c 20 62 65 20 6f  n name will be o
2a782 66 20 74 68 65 20 66 6f 72 6d 20 63 4e 4e 41 41  f the form cNNAA
2a783 41 41 0a 20 20 2a 2a 20 77 68 65 72 65 20 4e 4e  AA.  ** where NN
2a784 20 69 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e   is the column n
2a785 75 6d 62 65 72 20 61 6e 64 20 41 41 41 41 20 69  umber and AAAA i
2a786 73 20 74 68 65 20 73 61 6e 69 74 69 7a 65 64 0a  s the sanitized.
2a787 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65    ** column name
2a788 2e 20 20 22 73 61 6e 69 74 69 7a 65 64 22 20 6d  .  "sanitized" m
2a789 65 61 6e 73 20 74 68 61 74 20 73 70 65 63 69 61  eans that specia
2a78a 6c 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65  l characters are
2a78b 0a 20 20 2a 2a 20 63 6f 6e 76 65 72 74 65 64 20  .  ** converted 
2a78c 74 6f 20 22 5f 22 2e 20 20 54 68 65 20 63 4e 4e  to "_".  The cNN
2a78d 20 70 72 65 66 69 78 20 67 75 61 72 61 6e 74 65   prefix guarante
2a78e 65 73 20 74 68 61 74 20 61 6c 6c 20 63 6f 6c 75  es that all colu
2a78f 6d 6e 0a 20 20 2a 2a 20 6e 61 6d 65 73 20 61 72  mn.  ** names ar
2a790 65 20 75 6e 69 71 75 65 2e 0a 20 20 2a 2a 0a 20  e unique..  **. 
2a791 20 2a 2a 20 54 68 65 20 41 41 41 41 20 73 75 66   ** The AAAA suf
2a792 66 69 78 20 69 73 20 6e 6f 74 20 73 74 72 69 63  fix is not stric
2a793 74 6c 79 20 6e 65 63 65 73 73 61 72 79 2e 20 20  tly necessary.  
2a794 49 74 20 69 73 20 69 6e 63 6c 75 64 65 64 0a 20  It is included. 
2a795 20 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f 6e 76   ** for the conv
2a796 65 6e 69 65 6e 63 65 20 6f 66 20 70 65 6f 70 6c  enience of peopl
2a797 65 20 77 68 6f 20 6d 69 67 68 74 20 65 78 61 6d  e who might exam
2a798 69 6e 65 20 74 68 65 20 67 65 6e 65 72 61 74 65  ine the generate
2a799 64 0a 20 20 2a 2a 20 25 5f 63 6f 6e 74 65 6e 74  d.  ** %_content
2a79a 20 74 61 62 6c 65 20 61 6e 64 20 77 6f 6e 64 65   table and wonde
2a79b 72 20 77 68 61 74 20 74 68 65 20 63 6f 6c 75 6d  r what the colum
2a79c 6e 73 20 61 72 65 20 75 73 65 64 20 66 6f 72 2e  ns are used for.
2a79d 0a 20 20 2a 2f 0a 20 20 70 53 70 65 63 2d 3e 61  .  */.  pSpec->a
2a79e 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 20 3d  zContentColumn =
2a79f 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
2a7a0 20 70 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 20   pSpec->nColumn 
2a7a1 2a 20 73 69 7a 65 6f 66 28 63 68 61 72 20 2a 29  * sizeof(char *)
2a7a2 20 29 3b 0a 20 20 69 66 28 20 70 53 70 65 63 2d   );.  if( pSpec-
2a7a3 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e  >azContentColumn
2a7a4 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6c 65 61 72  ==0 ){.    clear
2a7a5 54 61 62 6c 65 53 70 65 63 28 70 53 70 65 63 29  TableSpec(pSpec)
2a7a6 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
2a7a7 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
2a7a8 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 53 70 65   for(i=0; i<pSpe
2a7a9 63 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29  c->nColumn; i++)
2a7aa 7b 0a 20 20 20 20 63 68 61 72 20 2a 70 3b 0a 20  {.    char *p;. 
2a7ab 20 20 20 70 53 70 65 63 2d 3e 61 7a 43 6f 6e 74     pSpec->azCont
2a7ac 65 6e 74 43 6f 6c 75 6d 6e 5b 69 5d 20 3d 20 73  entColumn[i] = s
2a7ad 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
2a7ae 63 25 64 25 73 22 2c 20 69 2c 20 61 7a 41 72 67  c%d%s", i, azArg
2a7af 5b 69 5d 29 3b 0a 20 20 20 20 66 6f 72 20 28 70  [i]);.    for (p
2a7b0 20 3d 20 70 53 70 65 63 2d 3e 61 7a 43 6f 6e 74   = pSpec->azCont
2a7b1 65 6e 74 43 6f 6c 75 6d 6e 5b 69 5d 3b 20 2a 70  entColumn[i]; *p
2a7b2 20 3b 20 2b 2b 70 29 20 7b 0a 20 20 20 20 20 20   ; ++p) {.      
2a7b3 69 66 28 20 21 73 61 66 65 5f 69 73 61 6c 6e 75  if( !safe_isalnu
2a7b4 6d 28 2a 70 29 20 29 20 2a 70 20 3d 20 27 5f 27  m(*p) ) *p = '_'
2a7b5 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
2a7b6 2a 0a 20 20 2a 2a 20 50 61 72 73 65 20 74 68 65  *.  ** Parse the
2a7b7 20 74 6f 6b 65 6e 69 7a 65 72 20 73 70 65 63 69   tokenizer speci
2a7b8 66 69 63 61 74 69 6f 6e 20 73 74 72 69 6e 67 2e  fication string.
2a7b9 0a 20 20 2a 2f 0a 20 20 70 53 70 65 63 2d 3e 61  .  */.  pSpec->a
2a7ba 7a 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 74 6f 6b  zTokenizer = tok
2a7bb 65 6e 69 7a 65 53 74 72 69 6e 67 28 7a 54 6f 6b  enizeString(zTok
2a7bc 65 6e 69 7a 65 72 2c 20 26 6e 29 3b 0a 20 20 74  enizer, &n);.  t
2a7bd 6f 6b 65 6e 4c 69 73 74 54 6f 49 64 4c 69 73 74  okenListToIdList
2a7be 28 70 53 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69  (pSpec->azTokeni
2a7bf 7a 65 72 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  zer);..  return 
2a7c0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
2a7c1 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 20 43  .** Generate a C
2a7c2 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
2a7c3 65 6d 65 6e 74 20 74 68 61 74 20 64 65 73 63 72  ement that descr
2a7c4 69 62 65 73 20 74 68 65 20 73 63 68 65 6d 61 20  ibes the schema 
2a7c5 6f 66 0a 2a 2a 20 74 68 65 20 76 69 72 74 75 61  of.** the virtua
2a7c6 6c 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e  l table.  Return
2a7c7 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
2a7c8 69 73 20 73 63 68 65 6d 61 20 73 74 72 69 6e 67  is schema string
2a7c9 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 69 73  ..**.** Space is
2a7ca 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
2a7cb 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29  qlite3_mprintf()
2a7cc 20 61 6e 64 20 73 68 6f 75 6c 64 20 62 65 20 66   and should be f
2a7cd 72 65 65 64 0a 2a 2a 20 75 73 69 6e 67 20 73 71  reed.** using sq
2a7ce 6c 69 74 65 33 5f 66 72 65 65 28 29 2e 0a 2a 2f  lite3_free()..*/
2a7cf 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 66 75  .static char *fu
2a7d0 6c 6c 74 65 78 74 53 63 68 65 6d 61 28 0a 20 20  lltextSchema(.  
2a7d1 69 6e 74 20 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20  int nColumn,    
2a7d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a7d3 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   Number of colum
2a7d4 6e 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ns */.  const ch
2a7d5 61 72 20 2a 63 6f 6e 73 74 2a 20 61 7a 43 6f 6c  ar *const* azCol
2a7d6 75 6d 6e 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66  umn,  /* List of
2a7d7 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 63 6f   columns */.  co
2a7d8 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65  nst char *zTable
2a7d9 4e 61 6d 65 20 20 20 20 20 20 20 20 2f 2a 20 4e  Name        /* N
2a7da 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
2a7db 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   */.){.  int i;.
2a7dc 20 20 63 68 61 72 20 2a 7a 53 63 68 65 6d 61 2c    char *zSchema,
2a7dd 20 2a 7a 4e 65 78 74 3b 0a 20 20 63 6f 6e 73 74   *zNext;.  const
2a7de 20 63 68 61 72 20 2a 7a 53 65 70 20 3d 20 22 28   char *zSep = "(
2a7df 22 3b 0a 20 20 7a 53 63 68 65 6d 61 20 3d 20 73  ";.  zSchema = s
2a7e0 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
2a7e1 43 52 45 41 54 45 20 54 41 42 4c 45 20 78 22 29  CREATE TABLE x")
2a7e2 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  ;.  for(i=0; i<n
2a7e3 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
2a7e4 20 20 7a 4e 65 78 74 20 3d 20 73 71 6c 69 74 65    zNext = sqlite
2a7e5 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 25 73 25  3_mprintf("%s%s%
2a7e6 51 22 2c 20 7a 53 63 68 65 6d 61 2c 20 7a 53 65  Q", zSchema, zSe
2a7e7 70 2c 20 61 7a 43 6f 6c 75 6d 6e 5b 69 5d 29 3b  p, azColumn[i]);
2a7e8 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
2a7e9 65 28 7a 53 63 68 65 6d 61 29 3b 0a 20 20 20 20  e(zSchema);.    
2a7ea 7a 53 63 68 65 6d 61 20 3d 20 7a 4e 65 78 74 3b  zSchema = zNext;
2a7eb 0a 20 20 20 20 7a 53 65 70 20 3d 20 22 2c 22 3b  .    zSep = ",";
2a7ec 0a 20 20 7d 0a 20 20 7a 4e 65 78 74 20 3d 20 73  .  }.  zNext = s
2a7ed 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
2a7ee 25 73 2c 25 51 20 48 49 44 44 45 4e 22 2c 20 7a  %s,%Q HIDDEN", z
2a7ef 53 63 68 65 6d 61 2c 20 7a 54 61 62 6c 65 4e 61  Schema, zTableNa
2a7f0 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  me);.  sqlite3_f
2a7f1 72 65 65 28 7a 53 63 68 65 6d 61 29 3b 0a 20 20  ree(zSchema);.  
2a7f2 7a 53 63 68 65 6d 61 20 3d 20 7a 4e 65 78 74 3b  zSchema = zNext;
2a7f3 0a 20 20 7a 4e 65 78 74 20 3d 20 73 71 6c 69 74  .  zNext = sqlit
2a7f4 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 2c 64  e3_mprintf("%s,d
2a7f5 6f 63 69 64 20 48 49 44 44 45 4e 29 22 2c 20 7a  ocid HIDDEN)", z
2a7f6 53 63 68 65 6d 61 29 3b 0a 20 20 73 71 6c 69 74  Schema);.  sqlit
2a7f7 65 33 5f 66 72 65 65 28 7a 53 63 68 65 6d 61 29  e3_free(zSchema)
2a7f8 3b 0a 20 20 72 65 74 75 72 6e 20 7a 4e 65 78 74  ;.  return zNext
2a7f9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 75 69 6c 64  ;.}../*.** Build
2a7fa 20 61 20 6e 65 77 20 73 71 6c 69 74 65 33 5f 76   a new sqlite3_v
2a7fb 74 61 62 20 73 74 72 75 63 74 75 72 65 20 74 68  tab structure th
2a7fc 61 74 20 77 69 6c 6c 20 64 65 73 63 72 69 62 65  at will describe
2a7fd 20 74 68 65 0a 2a 2a 20 66 75 6c 6c 74 65 78 74   the.** fulltext
2a7fe 20 69 6e 64 65 78 20 64 65 66 69 6e 65 64 20 62   index defined b
2a7ff 79 20 73 70 65 63 2e 0a 2a 2f 0a 73 74 61 74 69  y spec..*/.stati
2a800 63 20 69 6e 74 20 63 6f 6e 73 74 72 75 63 74 56  c int constructV
2a801 74 61 62 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  tab(.  sqlite3 *
2a802 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
2a803 20 2f 2a 20 54 68 65 20 53 51 4c 69 74 65 20 64   /* The SQLite d
2a804 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
2a805 6f 6e 20 2a 2f 0a 20 20 66 74 73 33 48 61 73 68  on */.  fts3Hash
2a806 20 2a 70 48 61 73 68 2c 20 20 20 20 20 20 20 20   *pHash,        
2a807 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20    /* Hash table 
2a808 63 6f 6e 74 61 69 6e 69 6e 67 20 74 6f 6b 65 6e  containing token
2a809 69 7a 65 72 73 20 2a 2f 0a 20 20 54 61 62 6c 65  izers */.  Table
2a80a 53 70 65 63 20 2a 73 70 65 63 2c 20 20 20 20 20  Spec *spec,     
2a80b 20 20 20 20 20 2f 2a 20 50 61 72 73 65 64 20 73       /* Parsed s
2a80c 70 65 63 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  pec information 
2a80d 66 72 6f 6d 20 70 61 72 73 65 53 70 65 63 28 29  from parseSpec()
2a80e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74   */.  sqlite3_vt
2a80f 61 62 20 2a 2a 70 70 56 54 61 62 2c 20 20 20 20  ab **ppVTab,    
2a810 2f 2a 20 57 72 69 74 65 20 74 68 65 20 72 65 73  /* Write the res
2a811 75 6c 74 69 6e 67 20 76 74 61 62 20 73 74 72 75  ulting vtab stru
2a812 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20  cture here */.  
2a813 63 68 61 72 20 2a 2a 70 7a 45 72 72 20 20 20 20  char **pzErr    
2a814 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
2a815 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73  te any error mes
2a816 73 61 67 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  sage here */.){.
2a817 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20    int rc;.  int 
2a818 6e 3b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74  n;.  fulltext_vt
2a819 61 62 20 2a 76 20 3d 20 30 3b 0a 20 20 63 6f 6e  ab *v = 0;.  con
2a81a 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  st sqlite3_token
2a81b 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 6d 20 3d  izer_module *m =
2a81c 20 4e 55 4c 4c 3b 0a 20 20 63 68 61 72 20 2a 73   NULL;.  char *s
2a81d 63 68 65 6d 61 3b 0a 0a 20 20 63 68 61 72 20 63  chema;..  char c
2a81e 6f 6e 73 74 20 2a 7a 54 6f 6b 3b 20 20 20 20 20  onst *zTok;     
2a81f 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
2a820 6f 6b 65 6e 69 7a 65 72 20 74 6f 20 75 73 65 20  okenizer to use 
2a821 66 6f 72 20 74 68 69 73 20 66 74 73 20 74 61 62  for this fts tab
2a822 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b  le */.  int nTok
2a823 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2a824 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a    /* Length of z
2a825 54 6f 6b 2c 20 69 6e 63 6c 75 64 69 6e 67 20 6e  Tok, including n
2a826 75 6c 20 74 65 72 6d 69 6e 61 74 6f 72 20 2a 2f  ul terminator */
2a827 0a 0a 20 20 76 20 3d 20 28 66 75 6c 6c 74 65 78  ..  v = (fulltex
2a828 74 5f 76 74 61 62 20 2a 29 20 73 71 6c 69 74 65  t_vtab *) sqlite
2a829 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
2a82a 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 29 29 3b  fulltext_vtab));
2a82b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65  .  if( v==0 ) re
2a82c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
2a82d 4d 3b 0a 20 20 43 4c 45 41 52 28 76 29 3b 0a 20  M;.  CLEAR(v);. 
2a82e 20 2f 2a 20 73 71 6c 69 74 65 20 77 69 6c 6c 20   /* sqlite will 
2a82f 69 6e 69 74 69 61 6c 69 7a 65 20 76 2d 3e 62 61  initialize v->ba
2a830 73 65 20 2a 2f 0a 20 20 76 2d 3e 64 62 20 3d 20  se */.  v->db = 
2a831 64 62 3b 0a 20 20 76 2d 3e 7a 44 62 20 3d 20 73  db;.  v->zDb = s
2a832 70 65 63 2d 3e 7a 44 62 3b 20 20 20 20 20 20 20  pec->zDb;       
2a833 2f 2a 20 46 72 65 65 64 20 77 68 65 6e 20 61 7a  /* Freed when az
2a834 43 6f 6c 75 6d 6e 20 69 73 20 66 72 65 65 64 20  Column is freed 
2a835 2a 2f 0a 20 20 76 2d 3e 7a 4e 61 6d 65 20 3d 20  */.  v->zName = 
2a836 73 70 65 63 2d 3e 7a 4e 61 6d 65 3b 20 20 20 2f  spec->zName;   /
2a837 2a 20 46 72 65 65 64 20 77 68 65 6e 20 61 7a 43  * Freed when azC
2a838 6f 6c 75 6d 6e 20 69 73 20 66 72 65 65 64 20 2a  olumn is freed *
2a839 2f 0a 20 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d  /.  v->nColumn =
2a83a 20 73 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a   spec->nColumn;.
2a83b 20 20 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f    v->azContentCo
2a83c 6c 75 6d 6e 20 3d 20 73 70 65 63 2d 3e 61 7a 43  lumn = spec->azC
2a83d 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 3b 0a 20 20  ontentColumn;.  
2a83e 73 70 65 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43  spec->azContentC
2a83f 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20 76 2d 3e  olumn = 0;.  v->
2a840 61 7a 43 6f 6c 75 6d 6e 20 3d 20 73 70 65 63 2d  azColumn = spec-
2a841 3e 61 7a 43 6f 6c 75 6d 6e 3b 0a 20 20 73 70 65  >azColumn;.  spe
2a842 63 2d 3e 61 7a 43 6f 6c 75 6d 6e 20 3d 20 30 3b  c->azColumn = 0;
2a843 0a 0a 20 20 69 66 28 20 73 70 65 63 2d 3e 61 7a  ..  if( spec->az
2a844 54 6f 6b 65 6e 69 7a 65 72 3d 3d 30 20 29 7b 0a  Tokenizer==0 ){.
2a845 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2a846 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20  E_NOMEM;.  }..  
2a847 7a 54 6f 6b 20 3d 20 73 70 65 63 2d 3e 61 7a 54  zTok = spec->azT
2a848 6f 6b 65 6e 69 7a 65 72 5b 30 5d 3b 20 0a 20 20  okenizer[0]; .  
2a849 69 66 28 20 21 7a 54 6f 6b 20 29 7b 0a 20 20 20  if( !zTok ){.   
2a84a 20 7a 54 6f 6b 20 3d 20 22 73 69 6d 70 6c 65 22   zTok = "simple"
2a84b 3b 0a 20 20 7d 0a 20 20 6e 54 6f 6b 20 3d 20 73  ;.  }.  nTok = s
2a84c 74 72 6c 65 6e 28 7a 54 6f 6b 29 2b 31 3b 0a 0a  trlen(zTok)+1;..
2a84d 20 20 6d 20 3d 20 28 73 71 6c 69 74 65 33 5f 74    m = (sqlite3_t
2a84e 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
2a84f 2a 29 73 71 6c 69 74 65 33 46 74 73 33 48 61 73  *)sqlite3Fts3Has
2a850 68 46 69 6e 64 28 70 48 61 73 68 2c 20 7a 54 6f  hFind(pHash, zTo
2a851 6b 2c 20 6e 54 6f 6b 29 3b 0a 20 20 69 66 28 20  k, nTok);.  if( 
2a852 21 6d 20 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72  !m ){.    *pzErr
2a853 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
2a854 74 66 28 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65  tf("unknown toke
2a855 6e 69 7a 65 72 3a 20 25 73 22 2c 20 73 70 65 63  nizer: %s", spec
2a856 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72 5b 30 5d  ->azTokenizer[0]
2a857 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
2a858 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 67 6f  TE_ERROR;.    go
2a859 74 6f 20 65 72 72 3b 0a 20 20 7d 0a 0a 20 20 66  to err;.  }..  f
2a85a 6f 72 28 6e 3d 30 3b 20 73 70 65 63 2d 3e 61 7a  or(n=0; spec->az
2a85b 54 6f 6b 65 6e 69 7a 65 72 5b 6e 5d 3b 20 6e 2b  Tokenizer[n]; n+
2a85c 2b 29 7b 7d 0a 20 20 69 66 28 20 6e 20 29 7b 0a  +){}.  if( n ){.
2a85d 20 20 20 20 72 63 20 3d 20 6d 2d 3e 78 43 72 65      rc = m->xCre
2a85e 61 74 65 28 6e 2d 31 2c 20 28 63 6f 6e 73 74 20  ate(n-1, (const 
2a85f 63 68 61 72 2a 63 6f 6e 73 74 2a 29 26 73 70 65  char*const*)&spe
2a860 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72 5b 31  c->azTokenizer[1
2a861 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ],.             
2a862 20 20 20 20 20 20 20 26 76 2d 3e 70 54 6f 6b 65         &v->pToke
2a863 6e 69 7a 65 72 29 3b 0a 20 20 7d 65 6c 73 65 7b  nizer);.  }else{
2a864 0a 20 20 20 20 72 63 20 3d 20 6d 2d 3e 78 43 72  .    rc = m->xCr
2a865 65 61 74 65 28 30 2c 20 30 2c 20 26 76 2d 3e 70  eate(0, 0, &v->p
2a866 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20 20 7d 0a  Tokenizer);.  }.
2a867 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2a868 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a  _OK ) goto err;.
2a869 20 20 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 2d    v->pTokenizer-
2a86a 3e 70 4d 6f 64 75 6c 65 20 3d 20 6d 3b 0a 0a 20  >pModule = m;.. 
2a86b 20 2f 2a 20 54 4f 44 4f 3a 20 76 65 72 69 66 79   /* TODO: verify
2a86c 20 74 68 65 20 65 78 69 73 74 65 6e 63 65 20 6f   the existence o
2a86d 66 20 62 61 63 6b 69 6e 67 20 74 61 62 6c 65 73  f backing tables
2a86e 20 66 6f 6f 5f 63 6f 6e 74 65 6e 74 2c 20 66 6f   foo_content, fo
2a86f 6f 5f 74 65 72 6d 20 2a 2f 0a 0a 20 20 73 63 68  o_term */..  sch
2a870 65 6d 61 20 3d 20 66 75 6c 6c 74 65 78 74 53 63  ema = fulltextSc
2a871 68 65 6d 61 28 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c  hema(v->nColumn,
2a872 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 63 6f 6e   (const char*con
2a873 73 74 2a 29 76 2d 3e 61 7a 43 6f 6c 75 6d 6e 2c  st*)v->azColumn,
2a874 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2a875 20 20 20 20 20 20 20 20 20 20 20 73 70 65 63 2d             spec-
2a876 3e 7a 4e 61 6d 65 29 3b 0a 20 20 72 63 20 3d 20  >zName);.  rc = 
2a877 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f  sqlite3_declare_
2a878 76 74 61 62 28 64 62 2c 20 73 63 68 65 6d 61 29  vtab(db, schema)
2a879 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
2a87a 28 73 63 68 65 6d 61 29 3b 0a 20 20 69 66 28 20  (schema);.  if( 
2a87b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2a87c 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20 6d 65 6d  goto err;..  mem
2a87d 73 65 74 28 76 2d 3e 70 46 75 6c 6c 74 65 78 74  set(v->pFulltext
2a87e 53 74 61 74 65 6d 65 6e 74 73 2c 20 30 2c 20 73  Statements, 0, s
2a87f 69 7a 65 6f 66 28 76 2d 3e 70 46 75 6c 6c 74 65  izeof(v->pFullte
2a880 78 74 53 74 61 74 65 6d 65 6e 74 73 29 29 3b 0a  xtStatements));.
2a881 0a 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 20 74  .  /* Indicate t
2a882 68 61 74 20 74 68 65 20 62 75 66 66 65 72 20 69  hat the buffer i
2a883 73 20 6e 6f 74 20 6c 69 76 65 2e 20 2a 2f 0a 20  s not live. */. 
2a884 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61   v->nPendingData
2a885 20 3d 20 2d 31 3b 0a 0a 20 20 2a 70 70 56 54 61   = -1;..  *ppVTa
2a886 62 20 3d 20 26 76 2d 3e 62 61 73 65 3b 0a 20 20  b = &v->base;.  
2a887 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20  FTSTRACE(("FTS3 
2a888 43 6f 6e 6e 65 63 74 20 25 70 5c 6e 22 2c 20 76  Connect %p\n", v
2a889 29 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63  ));..  return rc
2a88a 3b 0a 0a 65 72 72 3a 0a 20 20 66 75 6c 6c 74 65  ;..err:.  fullte
2a88b 78 74 5f 76 74 61 62 5f 64 65 73 74 72 6f 79 28  xt_vtab_destroy(
2a88c 76 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  v);.  return rc;
2a88d 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .}..static int f
2a88e 75 6c 6c 74 65 78 74 43 6f 6e 6e 65 63 74 28 0a  ulltextConnect(.
2a88f 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20    sqlite3 *db,. 
2a890 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 69   void *pAux,.  i
2a891 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63  nt argc, const c
2a892 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c  har *const*argv,
2a893 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
2a894 2a 2a 70 70 56 54 61 62 2c 0a 20 20 63 68 61 72  **ppVTab,.  char
2a895 20 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20 20 54 61   **pzErr.){.  Ta
2a896 62 6c 65 53 70 65 63 20 73 70 65 63 3b 0a 20 20  bleSpec spec;.  
2a897 69 6e 74 20 72 63 20 3d 20 70 61 72 73 65 53 70  int rc = parseSp
2a898 65 63 28 26 73 70 65 63 2c 20 61 72 67 63 2c 20  ec(&spec, argc, 
2a899 61 72 67 76 2c 20 70 7a 45 72 72 29 3b 0a 20 20  argv, pzErr);.  
2a89a 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2a89b 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
2a89c 20 20 72 63 20 3d 20 63 6f 6e 73 74 72 75 63 74    rc = construct
2a89d 56 74 61 62 28 64 62 2c 20 28 66 74 73 33 48 61  Vtab(db, (fts3Ha
2a89e 73 68 20 2a 29 70 41 75 78 2c 20 26 73 70 65 63  sh *)pAux, &spec
2a89f 2c 20 70 70 56 54 61 62 2c 20 70 7a 45 72 72 29  , ppVTab, pzErr)
2a8a0 3b 0a 20 20 63 6c 65 61 72 54 61 62 6c 65 53 70  ;.  clearTableSp
2a8a1 65 63 28 26 73 70 65 63 29 3b 0a 20 20 72 65 74  ec(&spec);.  ret
2a8a2 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 68  urn rc;.}../* Th
2a8a3 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c  e %_content tabl
2a8a4 65 20 68 6f 6c 64 73 20 74 68 65 20 74 65 78 74  e holds the text
2a8a5 20 6f 66 20 65 61 63 68 20 64 6f 63 75 6d 65 6e   of each documen
2a8a6 74 2c 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 64  t, with.** the d
2a8a7 6f 63 69 64 20 63 6f 6c 75 6d 6e 20 65 78 70 6f  ocid column expo
2a8a8 73 65 64 20 61 73 20 74 68 65 20 53 51 4c 69 74  sed as the SQLit
2a8a9 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20  e rowid for the 
2a8aa 74 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44  table..*/./* TOD
2a8ab 4f 28 73 68 65 73 73 29 20 54 68 69 73 20 63 6f  O(shess) This co
2a8ac 6d 6d 65 6e 74 20 6e 65 65 64 73 20 65 6c 61 62  mment needs elab
2a8ad 6f 72 61 74 69 6f 6e 20 74 6f 20 6d 61 74 63 68  oration to match
2a8ae 20 74 68 65 20 75 70 64 61 74 65 64 0a 2a 2a 20   the updated.** 
2a8af 63 6f 64 65 2e 20 20 57 6f 72 6b 20 69 74 20 69  code.  Work it i
2a8b0 6e 74 6f 20 74 68 65 20 74 6f 70 2d 6f 66 2d 66  nto the top-of-f
2a8b1 69 6c 65 20 63 6f 6d 6d 65 6e 74 20 61 74 20 74  ile comment at t
2a8b2 68 61 74 20 74 69 6d 65 2e 0a 2a 2f 0a 73 74 61  hat time..*/.sta
2a8b3 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74  tic int fulltext
2a8b4 43 72 65 61 74 65 28 73 71 6c 69 74 65 33 20 2a  Create(sqlite3 *
2a8b5 64 62 2c 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a  db, void *pAux,.
2a8b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a8b7 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 61 72            int ar
2a8b8 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  gc, const char *
2a8b9 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c 0a 20 20   const *argv,.  
2a8ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a8bb 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
2a8bc 76 74 61 62 20 2a 2a 70 70 56 54 61 62 2c 20 63  vtab **ppVTab, c
2a8bd 68 61 72 20 2a 2a 70 7a 45 72 72 29 7b 0a 20 20  har **pzErr){.  
2a8be 69 6e 74 20 72 63 3b 0a 20 20 54 61 62 6c 65 53  int rc;.  TableS
2a8bf 70 65 63 20 73 70 65 63 3b 0a 20 20 53 74 72 69  pec spec;.  Stri
2a8c0 6e 67 42 75 66 66 65 72 20 73 63 68 65 6d 61 3b  ngBuffer schema;
2a8c1 0a 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54  .  FTSTRACE(("FT
2a8c2 53 33 20 43 72 65 61 74 65 5c 6e 22 29 29 3b 0a  S3 Create\n"));.
2a8c3 0a 20 20 72 63 20 3d 20 70 61 72 73 65 53 70 65  .  rc = parseSpe
2a8c4 63 28 26 73 70 65 63 2c 20 61 72 67 63 2c 20 61  c(&spec, argc, a
2a8c5 72 67 76 2c 20 70 7a 45 72 72 29 3b 0a 20 20 69  rgv, pzErr);.  i
2a8c6 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2a8c7 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
2a8c8 20 69 6e 69 74 53 74 72 69 6e 67 42 75 66 66 65   initStringBuffe
2a8c9 72 28 26 73 63 68 65 6d 61 29 3b 0a 20 20 61 70  r(&schema);.  ap
2a8ca 70 65 6e 64 28 26 73 63 68 65 6d 61 2c 20 22 43  pend(&schema, "C
2a8cb 52 45 41 54 45 20 54 41 42 4c 45 20 25 5f 63 6f  REATE TABLE %_co
2a8cc 6e 74 65 6e 74 28 22 29 3b 0a 20 20 61 70 70 65  ntent(");.  appe
2a8cd 6e 64 28 26 73 63 68 65 6d 61 2c 20 22 20 20 64  nd(&schema, "  d
2a8ce 6f 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 49  ocid INTEGER PRI
2a8cf 4d 41 52 59 20 4b 45 59 2c 22 29 3b 0a 20 20 61  MARY KEY,");.  a
2a8d0 70 70 65 6e 64 4c 69 73 74 28 26 73 63 68 65 6d  ppendList(&schem
2a8d1 61 2c 20 73 70 65 63 2e 6e 43 6f 6c 75 6d 6e 2c  a, spec.nColumn,
2a8d2 20 73 70 65 63 2e 61 7a 43 6f 6e 74 65 6e 74 43   spec.azContentC
2a8d3 6f 6c 75 6d 6e 29 3b 0a 20 20 61 70 70 65 6e 64  olumn);.  append
2a8d4 28 26 73 63 68 65 6d 61 2c 20 22 29 22 29 3b 0a  (&schema, ")");.
2a8d5 20 20 72 63 20 3d 20 73 71 6c 5f 65 78 65 63 28    rc = sql_exec(
2a8d6 64 62 2c 20 73 70 65 63 2e 7a 44 62 2c 20 73 70  db, spec.zDb, sp
2a8d7 65 63 2e 7a 4e 61 6d 65 2c 20 73 74 72 69 6e 67  ec.zName, string
2a8d8 42 75 66 66 65 72 44 61 74 61 28 26 73 63 68 65  BufferData(&sche
2a8d9 6d 61 29 29 3b 0a 20 20 73 74 72 69 6e 67 42 75  ma));.  stringBu
2a8da 66 66 65 72 44 65 73 74 72 6f 79 28 26 73 63 68  fferDestroy(&sch
2a8db 65 6d 61 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  ema);.  if( rc!=
2a8dc 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
2a8dd 20 6f 75 74 3b 0a 0a 20 20 72 63 20 3d 20 73 71   out;..  rc = sq
2a8de 6c 5f 65 78 65 63 28 64 62 2c 20 73 70 65 63 2e  l_exec(db, spec.
2a8df 7a 44 62 2c 20 73 70 65 63 2e 7a 4e 61 6d 65 2c  zDb, spec.zName,
2a8e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2a8e1 20 22 63 72 65 61 74 65 20 74 61 62 6c 65 20 25   "create table %
2a8e2 5f 73 65 67 6d 65 6e 74 73 28 22 0a 20 20 20 20  _segments(".    
2a8e3 20 20 20 20 20 20 20 20 20 20 20 20 22 20 20 62              "  b
2a8e4 6c 6f 63 6b 69 64 20 49 4e 54 45 47 45 52 20 50  lockid INTEGER P
2a8e5 52 49 4d 41 52 59 20 4b 45 59 2c 22 0a 20 20 20  RIMARY KEY,".   
2a8e6 20 20 20 20 20 20 20 20 20 20 20 20 20 22 20 20               "  
2a8e7 62 6c 6f 63 6b 20 62 6c 6f 62 22 0a 20 20 20 20  block blob".    
2a8e8 20 20 20 20 20 20 20 20 20 20 20 20 22 29 3b 22              ");"
2a8e9 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2a8ea 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51   );.  if( rc!=SQ
2a8eb 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 6f  LITE_OK ) goto o
2a8ec 75 74 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 5f  ut;..  rc = sql_
2a8ed 65 78 65 63 28 64 62 2c 20 73 70 65 63 2e 7a 44  exec(db, spec.zD
2a8ee 62 2c 20 73 70 65 63 2e 7a 4e 61 6d 65 2c 0a 20  b, spec.zName,. 
2a8ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
2a8f0 63 72 65 61 74 65 20 74 61 62 6c 65 20 25 5f 73  create table %_s
2a8f1 65 67 64 69 72 28 22 0a 20 20 20 20 20 20 20 20  egdir(".        
2a8f2 20 20 20 20 20 20 20 20 22 20 20 6c 65 76 65 6c          "  level
2a8f3 20 69 6e 74 65 67 65 72 2c 22 0a 20 20 20 20 20   integer,".     
2a8f4 20 20 20 20 20 20 20 20 20 20 20 22 20 20 69 64             "  id
2a8f5 78 20 69 6e 74 65 67 65 72 2c 22 0a 20 20 20 20  x integer,".    
2a8f6 20 20 20 20 20 20 20 20 20 20 20 20 22 20 20 73              "  s
2a8f7 74 61 72 74 5f 62 6c 6f 63 6b 20 69 6e 74 65 67  tart_block integ
2a8f8 65 72 2c 22 0a 20 20 20 20 20 20 20 20 20 20 20  er,".           
2a8f9 20 20 20 20 20 22 20 20 6c 65 61 76 65 73 5f 65       "  leaves_e
2a8fa 6e 64 5f 62 6c 6f 63 6b 20 69 6e 74 65 67 65 72  nd_block integer
2a8fb 2c 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,".             
2a8fc 20 20 20 22 20 20 65 6e 64 5f 62 6c 6f 63 6b 20     "  end_block 
2a8fd 69 6e 74 65 67 65 72 2c 22 0a 20 20 20 20 20 20  integer,".      
2a8fe 20 20 20 20 20 20 20 20 20 20 22 20 20 72 6f 6f            "  roo
2a8ff 74 20 62 6c 6f 62 2c 22 0a 20 20 20 20 20 20 20  t blob,".       
2a900 20 20 20 20 20 20 20 20 20 22 20 20 70 72 69 6d           "  prim
2a901 61 72 79 20 6b 65 79 28 6c 65 76 65 6c 2c 20 69  ary key(level, i
2a902 64 78 29 22 0a 20 20 20 20 20 20 20 20 20 20 20  dx)".           
2a903 20 20 20 20 20 22 29 3b 22 29 3b 0a 20 20 69 66       ");");.  if
2a904 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2a905 29 20 67 6f 74 6f 20 6f 75 74 3b 0a 0a 20 20 72  ) goto out;..  r
2a906 63 20 3d 20 63 6f 6e 73 74 72 75 63 74 56 74 61  c = constructVta
2a907 62 28 64 62 2c 20 28 66 74 73 33 48 61 73 68 20  b(db, (fts3Hash 
2a908 2a 29 70 41 75 78 2c 20 26 73 70 65 63 2c 20 70  *)pAux, &spec, p
2a909 70 56 54 61 62 2c 20 70 7a 45 72 72 29 3b 0a 0a  pVTab, pzErr);..
2a90a 6f 75 74 3a 0a 20 20 63 6c 65 61 72 54 61 62 6c  out:.  clearTabl
2a90b 65 53 70 65 63 28 26 73 70 65 63 29 3b 0a 20 20  eSpec(&spec);.  
2a90c 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
2a90d 20 44 65 63 69 64 65 20 68 6f 77 20 74 6f 20 68   Decide how to h
2a90e 61 6e 64 6c 65 20 61 6e 20 53 51 4c 20 71 75 65  andle an SQL que
2a90f 72 79 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ry. */.static in
2a910 74 20 66 75 6c 6c 74 65 78 74 42 65 73 74 49 6e  t fulltextBestIn
2a911 64 65 78 28 73 71 6c 69 74 65 33 5f 76 74 61 62  dex(sqlite3_vtab
2a912 20 2a 70 56 54 61 62 2c 20 73 71 6c 69 74 65 33   *pVTab, sqlite3
2a913 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49 6e  _index_info *pIn
2a914 66 6f 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f  fo){.  fulltext_
2a915 76 74 61 62 20 2a 76 20 3d 20 28 66 75 6c 6c 74  vtab *v = (fullt
2a916 65 78 74 5f 76 74 61 62 20 2a 29 70 56 54 61 62  ext_vtab *)pVTab
2a917 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 46 54 53  ;.  int i;.  FTS
2a918 54 52 41 43 45 28 28 22 46 54 53 33 20 42 65 73  TRACE(("FTS3 Bes
2a919 74 49 6e 64 65 78 5c 6e 22 29 29 3b 0a 0a 20 20  tIndex\n"));..  
2a91a 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 6e 66 6f  for(i=0; i<pInfo
2a91b 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 2b  ->nConstraint; +
2a91c 2b 69 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 73  +i){.    const s
2a91d 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
2a91e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 2a  dex_constraint *
2a91f 70 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20 20  pConstraint;.   
2a920 20 70 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 26   pConstraint = &
2a921 70 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  pInfo->aConstrai
2a922 6e 74 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70  nt[i];.    if( p
2a923 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 75 73 61 62  Constraint->usab
2a924 6c 65 20 29 20 7b 0a 20 20 20 20 20 20 69 66 28  le ) {.      if(
2a925 20 28 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 69   (pConstraint->i
2a926 43 6f 6c 75 6d 6e 3d 3d 2d 31 20 7c 7c 20 70 43  Column==-1 || pC
2a927 6f 6e 73 74 72 61 69 6e 74 2d 3e 69 43 6f 6c 75  onstraint->iColu
2a928 6d 6e 3d 3d 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31  mn==v->nColumn+1
2a929 29 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 70  ) &&.          p
2a92a 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 6f 70 3d 3d  Constraint->op==
2a92b 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
2a92c 53 54 52 41 49 4e 54 5f 45 51 20 29 7b 0a 20 20  STRAINT_EQ ){.  
2a92d 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 69 64 78        pInfo->idx
2a92e 4e 75 6d 20 3d 20 51 55 45 52 59 5f 44 4f 43 49  Num = QUERY_DOCI
2a92f 44 3b 20 20 20 20 20 20 2f 2a 20 6c 6f 6f 6b 75  D;      /* looku
2a930 70 20 62 79 20 64 6f 63 69 64 20 2a 2f 0a 20 20  p by docid */.  
2a931 20 20 20 20 20 20 46 54 53 54 52 41 43 45 28 28        FTSTRACE((
2a932 22 46 54 53 33 20 51 55 45 52 59 5f 44 4f 43 49  "FTS3 QUERY_DOCI
2a933 44 5c 6e 22 29 29 3b 0a 20 20 20 20 20 20 7d 20  D\n"));.      } 
2a934 65 6c 73 65 20 69 66 28 20 70 43 6f 6e 73 74 72  else if( pConstr
2a935 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30  aint->iColumn>=0
2a936 20 26 26 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d   && pConstraint-
2a937 3e 69 43 6f 6c 75 6d 6e 3c 3d 76 2d 3e 6e 43 6f  >iColumn<=v->nCo
2a938 6c 75 6d 6e 20 26 26 0a 20 20 20 20 20 20 20 20  lumn &&.        
2a939 20 20 20 20 20 20 20 20 20 70 43 6f 6e 73 74 72           pConstr
2a93a 61 69 6e 74 2d 3e 6f 70 3d 3d 53 51 4c 49 54 45  aint->op==SQLITE
2a93b 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e  _INDEX_CONSTRAIN
2a93c 54 5f 4d 41 54 43 48 20 29 7b 0a 20 20 20 20 20  T_MATCH ){.     
2a93d 20 20 20 2f 2a 20 66 75 6c 6c 2d 74 65 78 74 20     /* full-text 
2a93e 73 65 61 72 63 68 20 2a 2f 0a 20 20 20 20 20 20  search */.      
2a93f 20 20 70 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20    pInfo->idxNum 
2a940 3d 20 51 55 45 52 59 5f 46 55 4c 4c 54 45 58 54  = QUERY_FULLTEXT
2a941 20 2b 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e   + pConstraint->
2a942 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 20  iColumn;.       
2a943 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33   FTSTRACE(("FTS3
2a944 20 51 55 45 52 59 5f 46 55 4c 4c 54 45 58 54 20   QUERY_FULLTEXT 
2a945 25 64 5c 6e 22 2c 20 70 43 6f 6e 73 74 72 61 69  %d\n", pConstrai
2a946 6e 74 2d 3e 69 43 6f 6c 75 6d 6e 29 29 3b 0a 20  nt->iColumn));. 
2a947 20 20 20 20 20 7d 20 65 6c 73 65 20 63 6f 6e 74       } else cont
2a948 69 6e 75 65 3b 0a 0a 20 20 20 20 20 20 70 49 6e  inue;..      pIn
2a949 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55  fo->aConstraintU
2a94a 73 61 67 65 5b 69 5d 2e 61 72 67 76 49 6e 64 65  sage[i].argvInde
2a94b 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 49 6e  x = 1;.      pIn
2a94c 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55  fo->aConstraintU
2a94d 73 61 67 65 5b 69 5d 2e 6f 6d 69 74 20 3d 20 31  sage[i].omit = 1
2a94e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 41 6e 20 61  ;..      /* An a
2a94f 72 62 69 74 72 61 72 79 20 76 61 6c 75 65 20 66  rbitrary value f
2a950 6f 72 20 6e 6f 77 2e 0a 20 20 20 20 20 20 20 2a  or now..       *
2a951 20 54 4f 44 4f 3a 20 50 65 72 68 61 70 73 20 64   TODO: Perhaps d
2a952 6f 63 69 64 20 6d 61 74 63 68 65 73 20 73 68 6f  ocid matches sho
2a953 75 6c 64 20 62 65 20 63 6f 6e 73 69 64 65 72 65  uld be considere
2a954 64 20 63 68 65 61 70 65 72 20 74 68 61 6e 0a 20  d cheaper than. 
2a955 20 20 20 20 20 20 2a 20 66 75 6c 6c 2d 74 65 78        * full-tex
2a956 74 20 73 65 61 72 63 68 65 73 2e 20 2a 2f 0a 20  t searches. */. 
2a957 20 20 20 20 20 70 49 6e 66 6f 2d 3e 65 73 74 69       pInfo->esti
2a958 6d 61 74 65 64 43 6f 73 74 20 3d 20 31 2e 30 3b  matedCost = 1.0;
2a959 20 20 20 0a 0a 20 20 20 20 20 20 72 65 74 75 72     ..      retur
2a95a 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
2a95b 20 7d 0a 20 20 7d 0a 20 20 70 49 6e 66 6f 2d 3e   }.  }.  pInfo->
2a95c 69 64 78 4e 75 6d 20 3d 20 51 55 45 52 59 5f 47  idxNum = QUERY_G
2a95d 45 4e 45 52 49 43 3b 0a 20 20 72 65 74 75 72 6e  ENERIC;.  return
2a95e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73   SQLITE_OK;.}..s
2a95f 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65  tatic int fullte
2a960 78 74 44 69 73 63 6f 6e 6e 65 63 74 28 73 71 6c  xtDisconnect(sql
2a961 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62  ite3_vtab *pVTab
2a962 29 7b 0a 20 20 46 54 53 54 52 41 43 45 28 28 22  ){.  FTSTRACE(("
2a963 46 54 53 33 20 44 69 73 63 6f 6e 6e 65 63 74 20  FTS3 Disconnect 
2a964 25 70 5c 6e 22 2c 20 70 56 54 61 62 29 29 3b 0a  %p\n", pVTab));.
2a965 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 5f    fulltext_vtab_
2a966 64 65 73 74 72 6f 79 28 28 66 75 6c 6c 74 65 78  destroy((fulltex
2a967 74 5f 76 74 61 62 20 2a 29 70 56 54 61 62 29 3b  t_vtab *)pVTab);
2a968 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
2a969 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69  _OK;.}..static i
2a96a 6e 74 20 66 75 6c 6c 74 65 78 74 44 65 73 74 72  nt fulltextDestr
2a96b 6f 79 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  oy(sqlite3_vtab 
2a96c 2a 70 56 54 61 62 29 7b 0a 20 20 66 75 6c 6c 74  *pVTab){.  fullt
2a96d 65 78 74 5f 76 74 61 62 20 2a 76 20 3d 20 28 66  ext_vtab *v = (f
2a96e 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29 70  ulltext_vtab *)p
2a96f 56 54 61 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  VTab;.  int rc;.
2a970 0a 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54  .  FTSTRACE(("FT
2a971 53 33 20 44 65 73 74 72 6f 79 20 25 70 5c 6e 22  S3 Destroy %p\n"
2a972 2c 20 70 56 54 61 62 29 29 3b 0a 20 20 72 63 20  , pVTab));.  rc 
2a973 3d 20 73 71 6c 5f 65 78 65 63 28 76 2d 3e 64 62  = sql_exec(v->db
2a974 2c 20 76 2d 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61  , v->zDb, v->zNa
2a975 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  me,.            
2a976 20 20 20 20 22 64 72 6f 70 20 74 61 62 6c 65 20      "drop table 
2a977 69 66 20 65 78 69 73 74 73 20 25 5f 63 6f 6e 74  if exists %_cont
2a978 65 6e 74 3b 22 0a 20 20 20 20 20 20 20 20 20 20  ent;".          
2a979 20 20 20 20 20 20 22 64 72 6f 70 20 74 61 62 6c        "drop tabl
2a97a 65 20 69 66 20 65 78 69 73 74 73 20 25 5f 73 65  e if exists %_se
2a97b 67 6d 65 6e 74 73 3b 22 0a 20 20 20 20 20 20 20  gments;".       
2a97c 20 20 20 20 20 20 20 20 20 22 64 72 6f 70 20 74           "drop t
2a97d 61 62 6c 65 20 69 66 20 65 78 69 73 74 73 20 25  able if exists %
2a97e 5f 73 65 67 64 69 72 3b 22 0a 20 20 20 20 20 20  _segdir;".      
2a97f 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20 69            );.  i
2a980 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2a981 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
2a982 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 5f 64   fulltext_vtab_d
2a983 65 73 74 72 6f 79 28 28 66 75 6c 6c 74 65 78 74  estroy((fulltext
2a984 5f 76 74 61 62 20 2a 29 70 56 54 61 62 29 3b 0a  _vtab *)pVTab);.
2a985 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2a986 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  OK;.}..static in
2a987 74 20 66 75 6c 6c 74 65 78 74 4f 70 65 6e 28 73  t fulltextOpen(s
2a988 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54  qlite3_vtab *pVT
2a989 61 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62  ab, sqlite3_vtab
2a98a 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73  _cursor **ppCurs
2a98b 6f 72 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f  or){.  fulltext_
2a98c 63 75 72 73 6f 72 20 2a 63 3b 0a 0a 20 20 63 20  cursor *c;..  c 
2a98d 3d 20 28 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  = (fulltext_curs
2a98e 6f 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61  or *) sqlite3_ma
2a98f 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 66 75 6c 6c  lloc(sizeof(full
2a990 74 65 78 74 5f 63 75 72 73 6f 72 29 29 3b 0a 20  text_cursor));. 
2a991 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 6d 65   if( c ){.    me
2a992 6d 73 65 74 28 63 2c 20 30 2c 20 73 69 7a 65 6f  mset(c, 0, sizeo
2a993 66 28 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f  f(fulltext_curso
2a994 72 29 29 3b 0a 20 20 20 20 2f 2a 20 73 71 6c 69  r));.    /* sqli
2a995 74 65 20 77 69 6c 6c 20 69 6e 69 74 69 61 6c 69  te will initiali
2a996 7a 65 20 63 2d 3e 62 61 73 65 20 2a 2f 0a 20 20  ze c->base */.  
2a997 20 20 2a 70 70 43 75 72 73 6f 72 20 3d 20 26 63    *ppCursor = &c
2a998 2d 3e 62 61 73 65 3b 0a 20 20 20 20 46 54 53 54  ->base;.    FTST
2a999 52 41 43 45 28 28 22 46 54 53 33 20 4f 70 65 6e  RACE(("FTS3 Open
2a99a 20 25 70 3a 20 25 70 5c 6e 22 2c 20 70 56 54 61   %p: %p\n", pVTa
2a99b 62 2c 20 63 29 29 3b 0a 20 20 20 20 72 65 74 75  b, c));.    retu
2a99c 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
2a99d 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
2a99e 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
2a99f 20 20 7d 0a 7d 0a 0a 0a 2f 2a 20 46 72 65 65 20    }.}.../* Free 
2a9a0 61 6c 6c 20 6f 66 20 74 68 65 20 64 79 6e 61 6d  all of the dynam
2a9a1 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64  ically allocated
2a9a2 20 6d 65 6d 6f 72 79 20 68 65 6c 64 20 62 79 20   memory held by 
2a9a3 2a 71 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  *q.*/.static voi
2a9a4 64 20 71 75 65 72 79 43 6c 65 61 72 28 51 75 65  d queryClear(Que
2a9a5 72 79 20 2a 71 29 7b 0a 20 20 69 6e 74 20 69 3b  ry *q){.  int i;
2a9a6 0a 20 20 66 6f 72 28 69 20 3d 20 30 3b 20 69 20  .  for(i = 0; i 
2a9a7 3c 20 71 2d 3e 6e 54 65 72 6d 73 3b 20 2b 2b 69  < q->nTerms; ++i
2a9a8 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
2a9a9 72 65 65 28 71 2d 3e 70 54 65 72 6d 73 5b 69 5d  ree(q->pTerms[i]
2a9aa 2e 70 54 65 72 6d 29 3b 0a 20 20 7d 0a 20 20 73  .pTerm);.  }.  s
2a9ab 71 6c 69 74 65 33 5f 66 72 65 65 28 71 2d 3e 70  qlite3_free(q->p
2a9ac 54 65 72 6d 73 29 3b 0a 20 20 43 4c 45 41 52 28  Terms);.  CLEAR(
2a9ad 71 29 3b 0a 7d 0a 0a 2f 2a 20 46 72 65 65 20 61  q);.}../* Free a
2a9ae 6c 6c 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69  ll of the dynami
2a9af 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  cally allocated 
2a9b0 6d 65 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 74  memory held by t
2a9b1 68 65 0a 2a 2a 20 53 6e 69 70 70 65 74 0a 2a 2f  he.** Snippet.*/
2a9b2 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 6e 69  .static void sni
2a9b3 70 70 65 74 43 6c 65 61 72 28 53 6e 69 70 70 65  ppetClear(Snippe
2a9b4 74 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33  t *p){.  sqlite3
2a9b5 5f 66 72 65 65 28 70 2d 3e 61 4d 61 74 63 68 29  _free(p->aMatch)
2a9b6 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
2a9b7 28 70 2d 3e 7a 4f 66 66 73 65 74 29 3b 0a 20 20  (p->zOffset);.  
2a9b8 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e  sqlite3_free(p->
2a9b9 7a 53 6e 69 70 70 65 74 29 3b 0a 20 20 43 4c 45  zSnippet);.  CLE
2a9ba 41 52 28 70 29 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 41  AR(p);.}./*.** A
2a9bb 70 70 65 6e 64 20 61 20 73 69 6e 67 6c 65 20 65  ppend a single e
2a9bc 6e 74 72 79 20 74 6f 20 74 68 65 20 70 2d 3e 61  ntry to the p->a
2a9bd 4d 61 74 63 68 5b 5d 20 6c 6f 67 2e 0a 2a 2f 0a  Match[] log..*/.
2a9be 73 74 61 74 69 63 20 76 6f 69 64 20 73 6e 69 70  static void snip
2a9bf 70 65 74 41 70 70 65 6e 64 4d 61 74 63 68 28 0a  petAppendMatch(.
2a9c0 20 20 53 6e 69 70 70 65 74 20 2a 70 2c 20 20 20    Snippet *p,   
2a9c1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
2a9c2 70 70 65 6e 64 20 74 68 65 20 65 6e 74 72 79 20  ppend the entry 
2a9c3 74 6f 20 74 68 69 73 20 73 6e 69 70 70 65 74 20  to this snippet 
2a9c4 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20 69  */.  int iCol, i
2a9c5 6e 74 20 69 54 65 72 6d 2c 20 20 20 20 20 20 2f  nt iTerm,      /
2a9c6 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 61 6e 64  * The column and
2a9c7 20 71 75 65 72 79 20 74 65 72 6d 20 2a 2f 0a 20   query term */. 
2a9c8 20 69 6e 74 20 69 54 6f 6b 65 6e 2c 20 20 20 20   int iToken,    
2a9c9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
2a9ca 74 63 68 69 6e 67 20 74 6f 6b 65 6e 20 69 6e 20  tching token in 
2a9cb 64 6f 63 75 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e  document */.  in
2a9cc 74 20 69 53 74 61 72 74 2c 20 69 6e 74 20 6e 42  t iStart, int nB
2a9cd 79 74 65 20 20 20 20 20 2f 2a 20 4f 66 66 73 65  yte     /* Offse
2a9ce 74 20 61 6e 64 20 73 69 7a 65 20 6f 66 20 74 68  t and size of th
2a9cf 65 20 6d 61 74 63 68 20 2a 2f 0a 29 7b 0a 20 20  e match */.){.  
2a9d0 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
2a9d1 73 6e 69 70 70 65 74 4d 61 74 63 68 20 2a 70 4d  snippetMatch *pM
2a9d2 61 74 63 68 3b 0a 20 20 69 66 28 20 70 2d 3e 6e  atch;.  if( p->n
2a9d3 4d 61 74 63 68 2b 31 3e 3d 70 2d 3e 6e 41 6c 6c  Match+1>=p->nAll
2a9d4 6f 63 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 41 6c  oc ){.    p->nAl
2a9d5 6c 6f 63 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 2a  loc = p->nAlloc*
2a9d6 32 20 2b 20 31 30 3b 0a 20 20 20 20 70 2d 3e 61  2 + 10;.    p->a
2a9d7 4d 61 74 63 68 20 3d 20 73 71 6c 69 74 65 33 5f  Match = sqlite3_
2a9d8 72 65 61 6c 6c 6f 63 28 70 2d 3e 61 4d 61 74 63  realloc(p->aMatc
2a9d9 68 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 2a 73 69 7a  h, p->nAlloc*siz
2a9da 65 6f 66 28 70 2d 3e 61 4d 61 74 63 68 5b 30 5d  eof(p->aMatch[0]
2a9db 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  ) );.    if( p->
2a9dc 61 4d 61 74 63 68 3d 3d 30 20 29 7b 0a 20 20 20  aMatch==0 ){.   
2a9dd 20 20 20 70 2d 3e 6e 4d 61 74 63 68 20 3d 20 30     p->nMatch = 0
2a9de 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f  ;.      p->nAllo
2a9df 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74  c = 0;.      ret
2a9e0 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  urn;.    }.  }. 
2a9e1 20 69 20 3d 20 70 2d 3e 6e 4d 61 74 63 68 2b 2b   i = p->nMatch++
2a9e2 3b 0a 20 20 70 4d 61 74 63 68 20 3d 20 26 70 2d  ;.  pMatch = &p-
2a9e3 3e 61 4d 61 74 63 68 5b 69 5d 3b 0a 20 20 70 4d  >aMatch[i];.  pM
2a9e4 61 74 63 68 2d 3e 69 43 6f 6c 20 3d 20 69 43 6f  atch->iCol = iCo
2a9e5 6c 3b 0a 20 20 70 4d 61 74 63 68 2d 3e 69 54 65  l;.  pMatch->iTe
2a9e6 72 6d 20 3d 20 69 54 65 72 6d 3b 0a 20 20 70 4d  rm = iTerm;.  pM
2a9e7 61 74 63 68 2d 3e 69 54 6f 6b 65 6e 20 3d 20 69  atch->iToken = i
2a9e8 54 6f 6b 65 6e 3b 0a 20 20 70 4d 61 74 63 68 2d  Token;.  pMatch-
2a9e9 3e 69 53 74 61 72 74 20 3d 20 69 53 74 61 72 74  >iStart = iStart
2a9ea 3b 0a 20 20 70 4d 61 74 63 68 2d 3e 6e 42 79 74  ;.  pMatch->nByt
2a9eb 65 20 3d 20 6e 42 79 74 65 3b 0a 7d 0a 0a 2f 2a  e = nByte;.}../*
2a9ec 0a 2a 2a 20 53 69 7a 69 6e 67 20 69 6e 66 6f 72  .** Sizing infor
2a9ed 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 63  mation for the c
2a9ee 69 72 63 75 6c 61 72 20 62 75 66 66 65 72 20 75  ircular buffer u
2a9ef 73 65 64 20 69 6e 20 73 6e 69 70 70 65 74 4f 66  sed in snippetOf
2a9f0 66 73 65 74 73 4f 66 43 6f 6c 75 6d 6e 28 29 0a  fsetsOfColumn().
2a9f1 2a 2f 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f  */.#define FTS3_
2a9f2 52 4f 54 4f 52 5f 53 5a 20 20 20 28 33 32 29 0a  ROTOR_SZ   (32).
2a9f3 23 64 65 66 69 6e 65 20 46 54 53 33 5f 52 4f 54  #define FTS3_ROT
2a9f4 4f 52 5f 4d 41 53 4b 20 28 46 54 53 33 5f 52 4f  OR_MASK (FTS3_RO
2a9f5 54 4f 52 5f 53 5a 2d 31 29 0a 0a 2f 2a 0a 2a 2a  TOR_SZ-1)../*.**
2a9f6 20 41 64 64 20 65 6e 74 72 69 65 73 20 74 6f 20   Add entries to 
2a9f7 70 53 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63 68  pSnippet->aMatch
2a9f8 5b 5d 20 66 6f 72 20 65 76 65 72 79 20 6d 61 74  [] for every mat
2a9f9 63 68 20 74 68 61 74 20 6f 63 63 75 72 73 20 61  ch that occurs a
2a9fa 67 61 69 6e 73 74 0a 2a 2a 20 64 6f 63 75 6d 65  gainst.** docume
2a9fb 6e 74 20 7a 44 6f 63 5b 30 2e 2e 6e 44 6f 63 2d  nt zDoc[0..nDoc-
2a9fc 31 5d 20 77 68 69 63 68 20 69 73 20 73 74 6f 72  1] which is stor
2a9fd 65 64 20 69 6e 20 63 6f 6c 75 6d 6e 20 69 43 6f  ed in column iCo
2a9fe 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lumn..*/.static 
2a9ff 76 6f 69 64 20 73 6e 69 70 70 65 74 4f 66 66 73  void snippetOffs
2aa00 65 74 73 4f 66 43 6f 6c 75 6d 6e 28 0a 20 20 51  etsOfColumn(.  Q
2aa01 75 65 72 79 20 2a 70 51 75 65 72 79 2c 0a 20 20  uery *pQuery,.  
2aa02 53 6e 69 70 70 65 74 20 2a 70 53 6e 69 70 70 65  Snippet *pSnippe
2aa03 74 2c 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  t,.  int iColumn
2aa04 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
2aa05 7a 44 6f 63 2c 0a 20 20 69 6e 74 20 6e 44 6f 63  zDoc,.  int nDoc
2aa06 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69  .){.  const sqli
2aa07 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
2aa08 64 75 6c 65 20 2a 70 54 4d 6f 64 75 6c 65 3b 20  dule *pTModule; 
2aa09 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 69 7a 65   /* The tokenize
2aa0a 72 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 73 71  r module */.  sq
2aa0b 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
2aa0c 2a 70 54 6f 6b 65 6e 69 7a 65 72 3b 20 20 20 20  *pTokenizer;    
2aa0d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2aa0e 73 70 65 63 69 66 69 63 20 74 6f 6b 65 6e 69 7a  specific tokeniz
2aa0f 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  er */.  sqlite3_
2aa10 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
2aa11 20 2a 70 54 43 75 72 73 6f 72 3b 20 20 20 20 20   *pTCursor;     
2aa12 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20     /* Tokenizer 
2aa13 63 75 72 73 6f 72 20 2a 2f 0a 20 20 66 75 6c 6c  cursor */.  full
2aa14 74 65 78 74 5f 76 74 61 62 20 2a 70 56 74 61 62  text_vtab *pVtab
2aa15 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2aa16 20 2f 2a 20 54 68 65 20 66 75 6c 6c 20 74 65 78   /* The full tex
2aa17 74 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74  t index */.  int
2aa18 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20   nColumn;       
2aa19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2aa1a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
2aa1b 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 69 6e  olumns in the in
2aa1c 64 65 78 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 51  dex */.  const Q
2aa1d 75 65 72 79 54 65 72 6d 20 2a 61 54 65 72 6d 3b  ueryTerm *aTerm;
2aa1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2aa1f 20 51 75 65 72 79 20 73 74 72 69 6e 67 20 74 65   Query string te
2aa20 72 6d 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65  rms */.  int nTe
2aa21 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rm;             
2aa22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2aa23 20 4e 75 6d 62 65 72 20 6f 66 20 71 75 65 72 79   Number of query
2aa24 20 73 74 72 69 6e 67 20 74 65 72 6d 73 20 2a 2f   string terms */
2aa25 20 20 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20    .  int i, j;  
2aa26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2aa27 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
2aa28 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20  p counters */.  
2aa29 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
2aa2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2aa2b 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
2aa2c 6f 64 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  ode */.  unsigne
2aa2d 64 20 69 6e 74 20 6d 61 74 63 68 2c 20 70 72 65  d int match, pre
2aa2e 76 4d 61 74 63 68 3b 20 20 20 20 20 20 20 2f 2a  vMatch;       /*
2aa2f 20 50 68 72 61 73 65 20 73 65 61 72 63 68 20 62   Phrase search b
2aa30 69 74 6d 61 73 6b 73 20 2a 2f 0a 20 20 63 6f 6e  itmasks */.  con
2aa31 73 74 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b  st char *zToken;
2aa32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2aa33 20 20 2f 2a 20 4e 65 78 74 20 74 6f 6b 65 6e 20    /* Next token 
2aa34 66 72 6f 6d 20 74 68 65 20 74 6f 6b 65 6e 69 7a  from the tokeniz
2aa35 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b  er */.  int nTok
2aa36 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  en;             
2aa37 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2aa38 53 69 7a 65 20 6f 66 20 7a 54 6f 6b 65 6e 20 2a  Size of zToken *
2aa39 2f 0a 20 20 69 6e 74 20 69 42 65 67 69 6e 2c 20  /.  int iBegin, 
2aa3a 69 45 6e 64 2c 20 69 50 6f 73 3b 20 20 20 20 20  iEnd, iPos;     
2aa3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
2aa3c 65 74 73 20 6f 66 20 62 65 67 69 6e 6e 69 6e 67  ets of beginning
2aa3d 20 61 6e 64 20 65 6e 64 20 2a 2f 0a 0a 20 20 2f   and end */..  /
2aa3e 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
2aa3f 76 61 72 69 61 62 6c 65 73 20 6b 65 65 70 20 61  variables keep a
2aa40 20 63 69 72 63 75 6c 61 72 20 62 75 66 66 65 72   circular buffer
2aa41 20 6f 66 20 74 68 65 20 6c 61 73 74 0a 20 20 2a   of the last.  *
2aa42 2a 20 66 65 77 20 74 6f 6b 65 6e 73 20 2a 2f 0a  * few tokens */.
2aa43 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69    unsigned int i
2aa44 52 6f 74 6f 72 20 3d 20 30 3b 20 20 20 20 20 20  Rotor = 0;      
2aa45 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
2aa46 6f 66 20 63 75 72 72 65 6e 74 20 74 6f 6b 65 6e  of current token
2aa47 20 2a 2f 0a 20 20 69 6e 74 20 69 52 6f 74 6f 72   */.  int iRotor
2aa48 42 65 67 69 6e 5b 46 54 53 33 5f 52 4f 54 4f 52  Begin[FTS3_ROTOR
2aa49 5f 53 5a 5d 3b 20 20 20 20 20 20 2f 2a 20 42 65  _SZ];      /* Be
2aa4a 67 69 6e 6e 69 6e 67 20 6f 66 66 73 65 74 20 6f  ginning offset o
2aa4b 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74  f token */.  int
2aa4c 20 69 52 6f 74 6f 72 4c 65 6e 5b 46 54 53 33 5f   iRotorLen[FTS3_
2aa4d 52 4f 54 4f 52 5f 53 5a 5d 3b 20 20 20 20 20 20  ROTOR_SZ];      
2aa4e 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74    /* Length of t
2aa4f 6f 6b 65 6e 20 2a 2f 0a 0a 20 20 70 56 74 61 62  oken */..  pVtab
2aa50 20 3d 20 70 51 75 65 72 79 2d 3e 70 46 74 73 3b   = pQuery->pFts;
2aa51 0a 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 56 74  .  nColumn = pVt
2aa52 61 62 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 70  ab->nColumn;.  p
2aa53 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 56 74 61  Tokenizer = pVta
2aa54 62 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20  b->pTokenizer;. 
2aa55 20 70 54 4d 6f 64 75 6c 65 20 3d 20 70 54 6f 6b   pTModule = pTok
2aa56 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 3b  enizer->pModule;
2aa57 0a 20 20 72 63 20 3d 20 70 54 4d 6f 64 75 6c 65  .  rc = pTModule
2aa58 2d 3e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69 7a  ->xOpen(pTokeniz
2aa59 65 72 2c 20 7a 44 6f 63 2c 20 6e 44 6f 63 2c 20  er, zDoc, nDoc, 
2aa5a 26 70 54 43 75 72 73 6f 72 29 3b 0a 20 20 69 66  &pTCursor);.  if
2aa5b 28 20 72 63 20 29 20 72 65 74 75 72 6e 3b 0a 20  ( rc ) return;. 
2aa5c 20 70 54 43 75 72 73 6f 72 2d 3e 70 54 6f 6b 65   pTCursor->pToke
2aa5d 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a  nizer = pTokeniz
2aa5e 65 72 3b 0a 20 20 61 54 65 72 6d 20 3d 20 70 51  er;.  aTerm = pQ
2aa5f 75 65 72 79 2d 3e 70 54 65 72 6d 73 3b 0a 20 20  uery->pTerms;.  
2aa60 6e 54 65 72 6d 20 3d 20 70 51 75 65 72 79 2d 3e  nTerm = pQuery->
2aa61 6e 54 65 72 6d 73 3b 0a 20 20 69 66 28 20 6e 54  nTerms;.  if( nT
2aa62 65 72 6d 3e 3d 46 54 53 33 5f 52 4f 54 4f 52 5f  erm>=FTS3_ROTOR_
2aa63 53 5a 20 29 7b 0a 20 20 20 20 6e 54 65 72 6d 20  SZ ){.    nTerm 
2aa64 3d 20 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 20  = FTS3_ROTOR_SZ 
2aa65 2d 20 31 3b 0a 20 20 7d 0a 20 20 70 72 65 76 4d  - 1;.  }.  prevM
2aa66 61 74 63 68 20 3d 20 30 3b 0a 20 20 77 68 69 6c  atch = 0;.  whil
2aa67 65 28 31 29 7b 0a 20 20 20 20 72 63 20 3d 20 70  e(1){.    rc = p
2aa68 54 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70  TModule->xNext(p
2aa69 54 43 75 72 73 6f 72 2c 20 26 7a 54 6f 6b 65 6e  TCursor, &zToken
2aa6a 2c 20 26 6e 54 6f 6b 65 6e 2c 20 26 69 42 65 67  , &nToken, &iBeg
2aa6b 69 6e 2c 20 26 69 45 6e 64 2c 20 26 69 50 6f 73  in, &iEnd, &iPos
2aa6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20  );.    if( rc ) 
2aa6d 62 72 65 61 6b 3b 0a 20 20 20 20 69 52 6f 74 6f  break;.    iRoto
2aa6e 72 42 65 67 69 6e 5b 69 52 6f 74 6f 72 26 46 54  rBegin[iRotor&FT
2aa6f 53 33 5f 52 4f 54 4f 52 5f 4d 41 53 4b 5d 20 3d  S3_ROTOR_MASK] =
2aa70 20 69 42 65 67 69 6e 3b 0a 20 20 20 20 69 52 6f   iBegin;.    iRo
2aa71 74 6f 72 4c 65 6e 5b 69 52 6f 74 6f 72 26 46 54  torLen[iRotor&FT
2aa72 53 33 5f 52 4f 54 4f 52 5f 4d 41 53 4b 5d 20 3d  S3_ROTOR_MASK] =
2aa73 20 69 45 6e 64 2d 69 42 65 67 69 6e 3b 0a 20 20   iEnd-iBegin;.  
2aa74 20 20 6d 61 74 63 68 20 3d 20 30 3b 0a 20 20 20    match = 0;.   
2aa75 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 54 65 72   for(i=0; i<nTer
2aa76 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  m; i++){.      i
2aa77 6e 74 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 69  nt iCol;.      i
2aa78 43 6f 6c 20 3d 20 61 54 65 72 6d 5b 69 5d 2e 69  Col = aTerm[i].i
2aa79 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 69 66  Column;.      if
2aa7a 28 20 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f  ( iCol>=0 && iCo
2aa7b 6c 3c 6e 43 6f 6c 75 6d 6e 20 26 26 20 69 43 6f  l<nColumn && iCo
2aa7c 6c 21 3d 69 43 6f 6c 75 6d 6e 20 29 20 63 6f 6e  l!=iColumn ) con
2aa7d 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 69 66 28  tinue;.      if(
2aa7e 20 61 54 65 72 6d 5b 69 5d 2e 6e 54 65 72 6d 3e   aTerm[i].nTerm>
2aa7f 6e 54 6f 6b 65 6e 20 29 20 63 6f 6e 74 69 6e 75  nToken ) continu
2aa80 65 3b 0a 20 20 20 20 20 20 69 66 28 20 21 61 54  e;.      if( !aT
2aa81 65 72 6d 5b 69 5d 2e 69 73 50 72 65 66 69 78 20  erm[i].isPrefix 
2aa82 26 26 20 61 54 65 72 6d 5b 69 5d 2e 6e 54 65 72  && aTerm[i].nTer
2aa83 6d 3c 6e 54 6f 6b 65 6e 20 29 20 63 6f 6e 74 69  m<nToken ) conti
2aa84 6e 75 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72  nue;.      asser
2aa85 74 28 20 61 54 65 72 6d 5b 69 5d 2e 6e 54 65 72  t( aTerm[i].nTer
2aa86 6d 3c 3d 6e 54 6f 6b 65 6e 20 29 3b 0a 20 20 20  m<=nToken );.   
2aa87 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 61 54     if( memcmp(aT
2aa88 65 72 6d 5b 69 5d 2e 70 54 65 72 6d 2c 20 7a 54  erm[i].pTerm, zT
2aa89 6f 6b 65 6e 2c 20 61 54 65 72 6d 5b 69 5d 2e 6e  oken, aTerm[i].n
2aa8a 54 65 72 6d 29 20 29 20 63 6f 6e 74 69 6e 75 65  Term) ) continue
2aa8b 3b 0a 20 20 20 20 20 20 69 66 28 20 61 54 65 72  ;.      if( aTer
2aa8c 6d 5b 69 5d 2e 69 50 68 72 61 73 65 3e 31 20 26  m[i].iPhrase>1 &
2aa8d 26 20 28 70 72 65 76 4d 61 74 63 68 20 26 20 28  & (prevMatch & (
2aa8e 31 3c 3c 69 29 29 3d 3d 30 20 29 20 63 6f 6e 74  1<<i))==0 ) cont
2aa8f 69 6e 75 65 3b 0a 20 20 20 20 20 20 6d 61 74 63  inue;.      matc
2aa90 68 20 7c 3d 20 31 3c 3c 69 3b 0a 20 20 20 20 20  h |= 1<<i;.     
2aa91 20 69 66 28 20 69 3d 3d 6e 54 65 72 6d 2d 31 20   if( i==nTerm-1 
2aa92 7c 7c 20 61 54 65 72 6d 5b 69 2b 31 5d 2e 69 50  || aTerm[i+1].iP
2aa93 68 72 61 73 65 3d 3d 31 20 29 7b 0a 20 20 20 20  hrase==1 ){.    
2aa94 20 20 20 20 66 6f 72 28 6a 3d 61 54 65 72 6d 5b      for(j=aTerm[
2aa95 69 5d 2e 69 50 68 72 61 73 65 2d 31 3b 20 6a 3e  i].iPhrase-1; j>
2aa96 3d 30 3b 20 6a 2d 2d 29 7b 0a 20 20 20 20 20 20  =0; j--){.      
2aa97 20 20 20 20 69 6e 74 20 6b 20 3d 20 28 69 52 6f      int k = (iRo
2aa98 74 6f 72 2d 6a 29 20 26 20 46 54 53 33 5f 52 4f  tor-j) & FTS3_RO
2aa99 54 4f 52 5f 4d 41 53 4b 3b 0a 20 20 20 20 20 20  TOR_MASK;.      
2aa9a 20 20 20 20 73 6e 69 70 70 65 74 41 70 70 65 6e      snippetAppen
2aa9b 64 4d 61 74 63 68 28 70 53 6e 69 70 70 65 74 2c  dMatch(pSnippet,
2aa9c 20 69 43 6f 6c 75 6d 6e 2c 20 69 2d 6a 2c 20 69   iColumn, i-j, i
2aa9d 50 6f 73 2d 6a 2c 0a 20 20 20 20 20 20 20 20 20  Pos-j,.         
2aa9e 20 20 20 20 20 20 20 69 52 6f 74 6f 72 42 65 67         iRotorBeg
2aa9f 69 6e 5b 6b 5d 2c 20 69 52 6f 74 6f 72 4c 65 6e  in[k], iRotorLen
2aaa0 5b 6b 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  [k]);.        }.
2aaa1 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2aaa2 20 20 70 72 65 76 4d 61 74 63 68 20 3d 20 6d 61    prevMatch = ma
2aaa3 74 63 68 3c 3c 31 3b 0a 20 20 20 20 69 52 6f 74  tch<<1;.    iRot
2aaa4 6f 72 2b 2b 3b 0a 20 20 7d 0a 20 20 70 54 4d 6f  or++;.  }.  pTMo
2aaa5 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 54 43  dule->xClose(pTC
2aaa6 75 72 73 6f 72 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a  ursor);  .}../*.
2aaa7 2a 2a 20 52 65 6d 6f 76 65 20 65 6e 74 72 69 65  ** Remove entrie
2aaa8 73 20 66 72 6f 6d 20 74 68 65 20 70 53 6e 69 70  s from the pSnip
2aaa9 70 65 74 20 73 74 72 75 63 74 75 72 65 20 74 6f  pet structure to
2aaaa 20 61 63 63 6f 75 6e 74 20 66 6f 72 20 74 68 65   account for the
2aaab 20 4e 45 41 52 0a 2a 2a 20 6f 70 65 72 61 74 6f   NEAR.** operato
2aaac 72 2e 20 57 68 65 6e 20 74 68 69 73 20 69 73 20  r. When this is 
2aaad 63 61 6c 6c 65 64 2c 20 70 53 6e 69 70 70 65 74  called, pSnippet
2aaae 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6c 69   contains the li
2aaaf 73 74 20 6f 66 20 74 6f 6b 65 6e 20 0a 2a 2a 20  st of token .** 
2aab0 6f 66 66 73 65 74 73 20 70 72 6f 64 75 63 65 64  offsets produced
2aab1 20 62 79 20 74 72 65 61 74 69 6e 67 20 61 6c 6c   by treating all
2aab2 20 4e 45 41 52 20 6f 70 65 72 61 74 6f 72 73 20   NEAR operators 
2aab3 61 73 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 73  as AND operators
2aab4 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  ..** This functi
2aab5 6f 6e 20 72 65 6d 6f 76 65 73 20 61 6e 79 20 65  on removes any e
2aab6 6e 74 72 69 65 73 20 74 68 61 74 20 73 68 6f 75  ntries that shou
2aab7 6c 64 20 6e 6f 74 20 62 65 20 70 72 65 73 65 6e  ld not be presen
2aab8 74 20 61 66 74 65 72 0a 2a 2a 20 61 63 63 6f 75  t after.** accou
2aab9 6e 74 69 6e 67 20 66 6f 72 20 74 68 65 20 4e 45  nting for the NE
2aaba 41 52 20 72 65 73 74 72 69 63 74 69 6f 6e 2e 20  AR restriction. 
2aabb 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20  For example, if 
2aabc 74 68 65 20 71 75 65 72 69 65 64 0a 2a 2a 20 64  the queried.** d
2aabd 6f 63 75 6d 65 6e 74 20 69 73 3a 0a 2a 2a 0a 2a  ocument is:.**.*
2aabe 2a 20 20 20 20 20 22 41 20 42 20 43 20 44 20 45  *     "A B C D E
2aabf 20 41 22 0a 2a 2a 0a 2a 2a 20 61 6e 64 20 74 68   A".**.** and th
2aac0 65 20 71 75 65 72 79 20 69 73 3a 0a 2a 2a 20 0a  e query is:.** .
2aac1 2a 2a 20 20 20 20 20 41 20 4e 45 41 52 2f 30 20  **     A NEAR/0 
2aac2 45 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 77 68 65  E.**.** then whe
2aac3 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
2aac4 69 73 20 63 61 6c 6c 65 64 20 74 68 65 20 53 6e  is called the Sn
2aac5 69 70 70 65 74 20 63 6f 6e 74 61 69 6e 73 20 74  ippet contains t
2aac6 6f 6b 65 6e 20 6f 66 66 73 65 74 73 0a 2a 2a 20  oken offsets.** 
2aac7 30 2c 20 34 20 61 6e 64 20 35 2e 20 54 68 69 73  0, 4 and 5. This
2aac8 20 66 75 6e 63 74 69 6f 6e 20 72 65 6d 6f 76 65   function remove
2aac9 73 20 74 68 65 20 22 30 22 20 65 6e 74 72 79 20  s the "0" entry 
2aaca 28 62 65 63 61 75 73 65 20 74 68 65 20 66 69 72  (because the fir
2aacb 73 74 20 41 0a 2a 2a 20 69 73 20 6e 6f 74 20 6e  st A.** is not n
2aacc 65 61 72 20 65 6e 6f 75 67 68 20 74 6f 20 61 6e  ear enough to an
2aacd 20 45 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76   E)..*/.static v
2aace 6f 69 64 20 74 72 69 6d 53 6e 69 70 70 65 74 4f  oid trimSnippetO
2aacf 66 66 73 65 74 73 46 6f 72 4e 65 61 72 28 51 75  ffsetsForNear(Qu
2aad0 65 72 79 20 2a 70 51 75 65 72 79 2c 20 53 6e 69  ery *pQuery, Sni
2aad1 70 70 65 74 20 2a 70 53 6e 69 70 70 65 74 29 7b  ppet *pSnippet){
2aad2 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74  .  int ii;.  int
2aad3 20 69 44 69 72 20 3d 20 31 3b 0a 0a 20 20 77 68   iDir = 1;..  wh
2aad4 69 6c 65 28 69 44 69 72 3e 2d 32 29 20 7b 0a 20  ile(iDir>-2) {. 
2aad5 20 20 20 61 73 73 65 72 74 28 20 69 44 69 72 3d     assert( iDir=
2aad6 3d 31 20 7c 7c 20 69 44 69 72 3d 3d 2d 31 20 29  =1 || iDir==-1 )
2aad7 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20  ;.    for(ii=0; 
2aad8 69 69 3c 70 53 6e 69 70 70 65 74 2d 3e 6e 4d 61  ii<pSnippet->nMa
2aad9 74 63 68 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  tch; ii++){.    
2aada 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20    int jj;.      
2aadb 69 6e 74 20 6e 4e 65 61 72 3b 0a 20 20 20 20 20  int nNear;.     
2aadc 20 73 74 72 75 63 74 20 73 6e 69 70 70 65 74 4d   struct snippetM
2aadd 61 74 63 68 20 2a 70 4d 61 74 63 68 20 3d 20 26  atch *pMatch = &
2aade 70 53 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63 68  pSnippet->aMatch
2aadf 5b 69 69 5d 3b 0a 20 20 20 20 20 20 51 75 65 72  [ii];.      Quer
2aae0 79 54 65 72 6d 20 2a 70 51 75 65 72 79 54 65 72  yTerm *pQueryTer
2aae1 6d 20 3d 20 26 70 51 75 65 72 79 2d 3e 70 54 65  m = &pQuery->pTe
2aae2 72 6d 73 5b 70 4d 61 74 63 68 2d 3e 69 54 65 72  rms[pMatch->iTer
2aae3 6d 5d 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 28  m];..      if( (
2aae4 70 4d 61 74 63 68 2d 3e 69 54 65 72 6d 2b 69 44  pMatch->iTerm+iD
2aae5 69 72 29 3c 30 20 0a 20 20 20 20 20 20 20 7c 7c  ir)<0 .       ||
2aae6 20 28 70 4d 61 74 63 68 2d 3e 69 54 65 72 6d 2b   (pMatch->iTerm+
2aae7 69 44 69 72 29 3e 3d 70 51 75 65 72 79 2d 3e 6e  iDir)>=pQuery->n
2aae8 54 65 72 6d 73 0a 20 20 20 20 20 20 29 7b 0a 20  Terms.      ){. 
2aae9 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
2aaea 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 0a 20  .      }.     . 
2aaeb 20 20 20 20 20 6e 4e 65 61 72 20 3d 20 70 51 75       nNear = pQu
2aaec 65 72 79 54 65 72 6d 2d 3e 6e 4e 65 61 72 3b 0a  eryTerm->nNear;.
2aaed 20 20 20 20 20 20 69 66 28 20 69 44 69 72 3c 30        if( iDir<0
2aaee 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 4e 65 61   ){.        nNea
2aaef 72 20 3d 20 70 51 75 65 72 79 54 65 72 6d 5b 2d  r = pQueryTerm[-
2aaf0 31 5d 2e 6e 4e 65 61 72 3b 0a 20 20 20 20 20 20  1].nNear;.      
2aaf1 7d 0a 20 20 0a 20 20 20 20 20 20 69 66 28 20 70  }.  .      if( p
2aaf2 4d 61 74 63 68 2d 3e 69 54 65 72 6d 3e 3d 30 20  Match->iTerm>=0 
2aaf3 26 26 20 6e 4e 65 61 72 20 29 7b 0a 20 20 20 20  && nNear ){.    
2aaf4 20 20 20 20 69 6e 74 20 69 73 4f 6b 20 3d 20 30      int isOk = 0
2aaf5 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 4e  ;.        int iN
2aaf6 65 78 74 54 65 72 6d 20 3d 20 70 4d 61 74 63 68  extTerm = pMatch
2aaf7 2d 3e 69 54 65 72 6d 2b 69 44 69 72 3b 0a 20 20  ->iTerm+iDir;.  
2aaf8 20 20 20 20 20 20 69 6e 74 20 69 50 72 65 76 54        int iPrevT
2aaf9 65 72 6d 20 3d 20 69 4e 65 78 74 54 65 72 6d 3b  erm = iNextTerm;
2aafa 0a 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 45  ..        int iE
2aafb 6e 64 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20  ndToken;.       
2aafc 20 69 6e 74 20 69 53 74 61 72 74 54 6f 6b 65 6e   int iStartToken
2aafd 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  ;..        if( i
2aafe 44 69 72 3c 30 20 29 7b 0a 20 20 20 20 20 20 20  Dir<0 ){.       
2aaff 20 20 20 69 6e 74 20 6e 50 68 72 61 73 65 20 3d     int nPhrase =
2ab00 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 69 53   1;.          iS
2ab01 74 61 72 74 54 6f 6b 65 6e 20 3d 20 70 4d 61 74  tartToken = pMat
2ab02 63 68 2d 3e 69 54 6f 6b 65 6e 3b 0a 20 20 20 20  ch->iToken;.    
2ab03 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 70 4d        while( (pM
2ab04 61 74 63 68 2d 3e 69 54 65 72 6d 2b 6e 50 68 72  atch->iTerm+nPhr
2ab05 61 73 65 29 3c 70 51 75 65 72 79 2d 3e 6e 54 65  ase)<pQuery->nTe
2ab06 72 6d 73 20 0a 20 20 20 20 20 20 20 20 20 20 20  rms .           
2ab07 20 20 20 26 26 20 70 51 75 65 72 79 2d 3e 70 54     && pQuery->pT
2ab08 65 72 6d 73 5b 70 4d 61 74 63 68 2d 3e 69 54 65  erms[pMatch->iTe
2ab09 72 6d 2b 6e 50 68 72 61 73 65 5d 2e 69 50 68 72  rm+nPhrase].iPhr
2ab0a 61 73 65 3e 31 20 0a 20 20 20 20 20 20 20 20 20  ase>1 .         
2ab0b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2ab0c 6e 50 68 72 61 73 65 2b 2b 3b 0a 20 20 20 20 20  nPhrase++;.     
2ab0d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
2ab0e 20 69 45 6e 64 54 6f 6b 65 6e 20 3d 20 69 53 74   iEndToken = iSt
2ab0f 61 72 74 54 6f 6b 65 6e 20 2b 20 6e 50 68 72 61  artToken + nPhra
2ab10 73 65 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20  se - 1;.        
2ab11 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
2ab12 20 69 45 6e 64 54 6f 6b 65 6e 20 20 20 3d 20 70   iEndToken   = p
2ab13 4d 61 74 63 68 2d 3e 69 54 6f 6b 65 6e 3b 0a 20  Match->iToken;. 
2ab14 20 20 20 20 20 20 20 20 20 69 53 74 61 72 74 54           iStartT
2ab15 6f 6b 65 6e 20 3d 20 70 4d 61 74 63 68 2d 3e 69  oken = pMatch->i
2ab16 54 6f 6b 65 6e 2b 31 2d 70 51 75 65 72 79 54 65  Token+1-pQueryTe
2ab17 72 6d 2d 3e 69 50 68 72 61 73 65 3b 0a 20 20 20  rm->iPhrase;.   
2ab18 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20       }..        
2ab19 77 68 69 6c 65 28 20 70 51 75 65 72 79 2d 3e 70  while( pQuery->p
2ab1a 54 65 72 6d 73 5b 69 4e 65 78 74 54 65 72 6d 5d  Terms[iNextTerm]
2ab1b 2e 69 50 68 72 61 73 65 3e 31 20 29 7b 0a 20 20  .iPhrase>1 ){.  
2ab1c 20 20 20 20 20 20 20 20 69 4e 65 78 74 54 65 72          iNextTer
2ab1d 6d 2d 2d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  m--;.        }. 
2ab1e 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 69         while( (i
2ab1f 50 72 65 76 54 65 72 6d 2b 31 29 3c 70 51 75 65  PrevTerm+1)<pQue
2ab20 72 79 2d 3e 6e 54 65 72 6d 73 20 26 26 20 0a 20  ry->nTerms && . 
2ab21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 51                pQ
2ab22 75 65 72 79 2d 3e 70 54 65 72 6d 73 5b 69 50 72  uery->pTerms[iPr
2ab23 65 76 54 65 72 6d 2b 31 5d 2e 69 50 68 72 61 73  evTerm+1].iPhras
2ab24 65 3e 31 20 0a 20 20 20 20 20 20 20 20 29 7b 0a  e>1 .        ){.
2ab25 20 20 20 20 20 20 20 20 20 20 69 50 72 65 76 54            iPrevT
2ab26 65 72 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d  erm++;.        }
2ab27 0a 20 20 0a 20 20 20 20 20 20 20 20 66 6f 72 28  .  .        for(
2ab28 6a 6a 3d 30 3b 20 69 73 4f 6b 3d 3d 30 20 26 26  jj=0; isOk==0 &&
2ab29 20 6a 6a 3c 70 53 6e 69 70 70 65 74 2d 3e 6e 4d   jj<pSnippet->nM
2ab2a 61 74 63 68 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20  atch; jj++){.   
2ab2b 20 20 20 20 20 20 20 73 74 72 75 63 74 20 73 6e         struct sn
2ab2c 69 70 70 65 74 4d 61 74 63 68 20 2a 70 20 3d 20  ippetMatch *p = 
2ab2d 26 70 53 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63  &pSnippet->aMatc
2ab2e 68 5b 6a 6a 5d 3b 0a 20 20 20 20 20 20 20 20 20  h[jj];.         
2ab2f 20 69 66 28 20 70 2d 3e 69 43 6f 6c 3d 3d 70 4d   if( p->iCol==pM
2ab30 61 74 63 68 2d 3e 69 43 6f 6c 20 26 26 20 28 28  atch->iCol && ((
2ab31 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2ab32 70 2d 3e 69 54 65 72 6d 3d 3d 69 4e 65 78 74 54  p->iTerm==iNextT
2ab33 65 72 6d 20 26 26 20 0a 20 20 20 20 20 20 20 20  erm && .        
2ab34 20 20 20 20 20 20 20 70 2d 3e 69 54 6f 6b 65 6e         p->iToken
2ab35 3e 69 45 6e 64 54 6f 6b 65 6e 20 26 26 20 0a 20  >iEndToken && . 
2ab36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 2d                p-
2ab37 3e 69 54 6f 6b 65 6e 3c 3d 69 45 6e 64 54 6f 6b  >iToken<=iEndTok
2ab38 65 6e 2b 6e 4e 65 61 72 0a 20 20 20 20 20 20 20  en+nNear.       
2ab39 20 20 20 29 20 7c 7c 20 28 0a 20 20 20 20 20 20     ) || (.      
2ab3a 20 20 20 20 20 20 20 20 20 70 2d 3e 69 54 65 72           p->iTer
2ab3b 6d 3d 3d 69 50 72 65 76 54 65 72 6d 20 26 26 20  m==iPrevTerm && 
2ab3c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2ab3d 70 2d 3e 69 54 6f 6b 65 6e 3c 69 53 74 61 72 74  p->iToken<iStart
2ab3e 54 6f 6b 65 6e 20 26 26 20 0a 20 20 20 20 20 20  Token && .      
2ab3f 20 20 20 20 20 20 20 20 20 70 2d 3e 69 54 6f 6b           p->iTok
2ab40 65 6e 3e 3d 69 53 74 61 72 74 54 6f 6b 65 6e 2d  en>=iStartToken-
2ab41 6e 4e 65 61 72 0a 20 20 20 20 20 20 20 20 20 20  nNear.          
2ab42 29 29 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ))){.           
2ab43 20 69 73 4f 6b 20 3d 20 31 3b 0a 20 20 20 20 20   isOk = 1;.     
2ab44 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
2ab45 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73  .        if( !is
2ab46 4f 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  Ok ){.          
2ab47 66 6f 72 28 6a 6a 3d 31 2d 70 51 75 65 72 79 54  for(jj=1-pQueryT
2ab48 65 72 6d 2d 3e 69 50 68 72 61 73 65 3b 20 6a 6a  erm->iPhrase; jj
2ab49 3c 3d 30 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20  <=0; jj++){.    
2ab4a 20 20 20 20 20 20 20 20 70 4d 61 74 63 68 5b 6a          pMatch[j
2ab4b 6a 5d 2e 69 54 65 72 6d 20 3d 20 2d 31 3b 0a 20  j].iTerm = -1;. 
2ab4c 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2ab4d 20 20 20 20 20 69 69 20 3d 20 2d 31 3b 0a 20 20       ii = -1;.  
2ab4e 20 20 20 20 20 20 20 20 69 44 69 72 20 3d 20 31          iDir = 1
2ab4f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2ab50 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 44    }.    }.    iD
2ab51 69 72 20 2d 3d 20 32 3b 0a 20 20 7d 0a 7d 0a 0a  ir -= 2;.  }.}..
2ab52 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61 6c  /*.** Compute al
2ab53 6c 20 6f 66 66 73 65 74 73 20 66 6f 72 20 74 68  l offsets for th
2ab54 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66  e current row of
2ab55 20 74 68 65 20 71 75 65 72 79 2e 20 20 0a 2a 2a   the query.  .**
2ab56 20 49 66 20 74 68 65 20 6f 66 66 73 65 74 73 20   If the offsets 
2ab57 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65  have already bee
2ab58 6e 20 63 6f 6d 70 75 74 65 64 2c 20 74 68 69 73  n computed, this
2ab59 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
2ab5a 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  -op..*/.static v
2ab5b 6f 69 64 20 73 6e 69 70 70 65 74 41 6c 6c 4f 66  oid snippetAllOf
2ab5c 66 73 65 74 73 28 66 75 6c 6c 74 65 78 74 5f 63  fsets(fulltext_c
2ab5d 75 72 73 6f 72 20 2a 70 29 7b 0a 20 20 69 6e 74  ursor *p){.  int
2ab5e 20 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69 6e 74 20   nColumn;.  int 
2ab5f 69 43 6f 6c 75 6d 6e 2c 20 69 3b 0a 20 20 69 6e  iColumn, i;.  in
2ab60 74 20 69 46 69 72 73 74 2c 20 69 4c 61 73 74 3b  t iFirst, iLast;
2ab61 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  .  fulltext_vtab
2ab62 20 2a 70 46 74 73 3b 0a 0a 20 20 69 66 28 20 70   *pFts;..  if( p
2ab63 2d 3e 73 6e 69 70 70 65 74 2e 6e 4d 61 74 63 68  ->snippet.nMatch
2ab64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
2ab65 20 70 2d 3e 71 2e 6e 54 65 72 6d 73 3d 3d 30 20   p->q.nTerms==0 
2ab66 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 46 74 73  ) return;.  pFts
2ab67 20 3d 20 70 2d 3e 71 2e 70 46 74 73 3b 0a 20 20   = p->q.pFts;.  
2ab68 6e 43 6f 6c 75 6d 6e 20 3d 20 70 46 74 73 2d 3e  nColumn = pFts->
2ab69 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69 43 6f 6c 75  nColumn;.  iColu
2ab6a 6d 6e 20 3d 20 28 70 2d 3e 69 43 75 72 73 6f 72  mn = (p->iCursor
2ab6b 54 79 70 65 20 2d 20 51 55 45 52 59 5f 46 55 4c  Type - QUERY_FUL
2ab6c 4c 54 45 58 54 29 3b 0a 20 20 69 66 28 20 69 43  LTEXT);.  if( iC
2ab6d 6f 6c 75 6d 6e 3c 30 20 7c 7c 20 69 43 6f 6c 75  olumn<0 || iColu
2ab6e 6d 6e 3e 3d 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20  mn>=nColumn ){. 
2ab6f 20 20 20 69 46 69 72 73 74 20 3d 20 30 3b 0a 20     iFirst = 0;. 
2ab70 20 20 20 69 4c 61 73 74 20 3d 20 6e 43 6f 6c 75     iLast = nColu
2ab71 6d 6e 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  mn-1;.  }else{. 
2ab72 20 20 20 69 46 69 72 73 74 20 3d 20 69 43 6f 6c     iFirst = iCol
2ab73 75 6d 6e 3b 0a 20 20 20 20 69 4c 61 73 74 20 3d  umn;.    iLast =
2ab74 20 69 43 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 20 20   iColumn;.  }.  
2ab75 66 6f 72 28 69 3d 69 46 69 72 73 74 3b 20 69 3c  for(i=iFirst; i<
2ab76 3d 69 4c 61 73 74 3b 20 69 2b 2b 29 7b 0a 20 20  =iLast; i++){.  
2ab77 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
2ab78 6f 63 3b 0a 20 20 20 20 69 6e 74 20 6e 44 6f 63  oc;.    int nDoc
2ab79 3b 0a 20 20 20 20 7a 44 6f 63 20 3d 20 28 63 6f  ;.    zDoc = (co
2ab7a 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65  nst char*)sqlite
2ab7b 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 70 2d  3_column_text(p-
2ab7c 3e 70 53 74 6d 74 2c 20 69 2b 31 29 3b 0a 20 20  >pStmt, i+1);.  
2ab7d 20 20 6e 44 6f 63 20 3d 20 73 71 6c 69 74 65 33    nDoc = sqlite3
2ab7e 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 2d  _column_bytes(p-
2ab7f 3e 70 53 74 6d 74 2c 20 69 2b 31 29 3b 0a 20 20  >pStmt, i+1);.  
2ab80 20 20 73 6e 69 70 70 65 74 4f 66 66 73 65 74 73    snippetOffsets
2ab81 4f 66 43 6f 6c 75 6d 6e 28 26 70 2d 3e 71 2c 20  OfColumn(&p->q, 
2ab82 26 70 2d 3e 73 6e 69 70 70 65 74 2c 20 69 2c 20  &p->snippet, i, 
2ab83 7a 44 6f 63 2c 20 6e 44 6f 63 29 3b 0a 20 20 7d  zDoc, nDoc);.  }
2ab84 0a 0a 20 20 74 72 69 6d 53 6e 69 70 70 65 74 4f  ..  trimSnippetO
2ab85 66 66 73 65 74 73 46 6f 72 4e 65 61 72 28 26 70  ffsetsForNear(&p
2ab86 2d 3e 71 2c 20 26 70 2d 3e 73 6e 69 70 70 65 74  ->q, &p->snippet
2ab87 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  );.}../*.** Conv
2ab88 65 72 74 20 74 68 65 20 69 6e 66 6f 72 6d 61 74  ert the informat
2ab89 69 6f 6e 20 69 6e 20 74 68 65 20 61 4d 61 74 63  ion in the aMatc
2ab8a 68 5b 5d 20 61 72 72 61 79 20 6f 66 20 74 68 65  h[] array of the
2ab8b 20 73 6e 69 70 70 65 74 0a 2a 2a 20 69 6e 74 6f   snippet.** into
2ab8c 20 74 68 65 20 73 74 72 69 6e 67 20 7a 4f 66 66   the string zOff
2ab8d 73 65 74 5b 30 2e 2e 6e 4f 66 66 73 65 74 2d 31  set[0..nOffset-1
2ab8e 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ]..*/.static voi
2ab8f 64 20 73 6e 69 70 70 65 74 4f 66 66 73 65 74 54  d snippetOffsetT
2ab90 65 78 74 28 53 6e 69 70 70 65 74 20 2a 70 29 7b  ext(Snippet *p){
2ab91 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
2ab92 63 6e 74 20 3d 20 30 3b 0a 20 20 53 74 72 69 6e  cnt = 0;.  Strin
2ab93 67 42 75 66 66 65 72 20 73 62 3b 0a 20 20 63 68  gBuffer sb;.  ch
2ab94 61 72 20 7a 42 75 66 5b 32 30 30 5d 3b 0a 20 20  ar zBuf[200];.  
2ab95 69 66 28 20 70 2d 3e 7a 4f 66 66 73 65 74 20 29  if( p->zOffset )
2ab96 20 72 65 74 75 72 6e 3b 0a 20 20 69 6e 69 74 53   return;.  initS
2ab97 74 72 69 6e 67 42 75 66 66 65 72 28 26 73 62 29  tringBuffer(&sb)
2ab98 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
2ab99 2d 3e 6e 4d 61 74 63 68 3b 20 69 2b 2b 29 7b 0a  ->nMatch; i++){.
2ab9a 20 20 20 20 73 74 72 75 63 74 20 73 6e 69 70 70      struct snipp
2ab9b 65 74 4d 61 74 63 68 20 2a 70 4d 61 74 63 68 20  etMatch *pMatch 
2ab9c 3d 20 26 70 2d 3e 61 4d 61 74 63 68 5b 69 5d 3b  = &p->aMatch[i];
2ab9d 0a 20 20 20 20 69 66 28 20 70 4d 61 74 63 68 2d  .    if( pMatch-
2ab9e 3e 69 54 65 72 6d 3e 3d 30 20 29 7b 0a 20 20 20  >iTerm>=0 ){.   
2ab9f 20 20 20 2f 2a 20 49 66 20 73 6e 69 70 70 65 74     /* If snippet
2aba0 4d 61 74 63 68 2e 69 54 65 72 6d 20 69 73 20 6c  Match.iTerm is l
2aba1 65 73 73 20 74 68 61 6e 20 30 2c 20 74 68 65 6e  ess than 0, then
2aba2 20 74 68 65 20 6d 61 74 63 68 20 77 61 73 20 0a   the match was .
2aba3 20 20 20 20 20 20 2a 2a 20 64 69 73 63 61 72 64        ** discard
2aba4 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 70 72  ed as part of pr
2aba5 6f 63 65 73 73 69 6e 67 20 74 68 65 20 4e 45 41  ocessing the NEA
2aba6 52 20 6f 70 65 72 61 74 6f 72 20 28 73 65 65 20  R operator (see 
2aba7 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 74 72  the .      ** tr
2aba8 69 6d 53 6e 69 70 70 65 74 4f 66 66 73 65 74 73  imSnippetOffsets
2aba9 46 6f 72 4e 65 61 72 28 29 20 66 75 6e 63 74 69  ForNear() functi
2abaa 6f 6e 20 66 6f 72 20 64 65 74 61 69 6c 73 29 2e  on for details).
2abab 20 49 67 6e 6f 72 65 20 0a 20 20 20 20 20 20 2a   Ignore .      *
2abac 2a 20 69 74 20 69 6e 20 74 68 69 73 20 63 61 73  * it in this cas
2abad 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  e.      */.     
2abae 20 7a 42 75 66 5b 30 5d 20 3d 20 27 20 27 3b 0a   zBuf[0] = ' ';.
2abaf 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
2abb0 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42  printf(sizeof(zB
2abb1 75 66 29 2d 31 2c 20 26 7a 42 75 66 5b 63 6e 74  uf)-1, &zBuf[cnt
2abb2 3e 30 5d 2c 20 22 25 64 20 25 64 20 25 64 20 25  >0], "%d %d %d %
2abb3 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 4d  d",.          pM
2abb4 61 74 63 68 2d 3e 69 43 6f 6c 2c 20 70 4d 61 74  atch->iCol, pMat
2abb5 63 68 2d 3e 69 54 65 72 6d 2c 20 70 4d 61 74 63  ch->iTerm, pMatc
2abb6 68 2d 3e 69 53 74 61 72 74 2c 20 70 4d 61 74 63  h->iStart, pMatc
2abb7 68 2d 3e 6e 42 79 74 65 29 3b 0a 20 20 20 20 20  h->nByte);.     
2abb8 20 61 70 70 65 6e 64 28 26 73 62 2c 20 7a 42 75   append(&sb, zBu
2abb9 66 29 3b 0a 20 20 20 20 20 20 63 6e 74 2b 2b 3b  f);.      cnt++;
2abba 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 2d 3e  .    }.  }.  p->
2abbb 7a 4f 66 66 73 65 74 20 3d 20 73 74 72 69 6e 67  zOffset = string
2abbc 42 75 66 66 65 72 44 61 74 61 28 26 73 62 29 3b  BufferData(&sb);
2abbd 0a 20 20 70 2d 3e 6e 4f 66 66 73 65 74 20 3d 20  .  p->nOffset = 
2abbe 73 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e 67  stringBufferLeng
2abbf 74 68 28 26 73 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  th(&sb);.}../*.*
2abc0 2a 20 7a 44 6f 63 5b 30 2e 2e 6e 44 6f 63 2d 31  * zDoc[0..nDoc-1
2abc1 5d 20 69 73 20 70 68 72 61 73 65 20 6f 66 20 74  ] is phrase of t
2abc2 65 78 74 2e 20 20 61 4d 61 74 63 68 5b 30 2e 2e  ext.  aMatch[0..
2abc3 6e 4d 61 74 63 68 2d 31 5d 20 61 72 65 20 61 20  nMatch-1] are a 
2abc4 73 65 74 0a 2a 2a 20 6f 66 20 6d 61 74 63 68 69  set.** of matchi
2abc5 6e 67 20 77 6f 72 64 73 20 73 6f 6d 65 20 6f 66  ng words some of
2abc6 20 77 68 69 63 68 20 6d 69 67 68 74 20 62 65 20   which might be 
2abc7 69 6e 20 7a 44 6f 63 2e 20 20 7a 44 6f 63 20 69  in zDoc.  zDoc i
2abc8 73 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6e 75 6d 62  s column.** numb
2abc9 65 72 20 69 43 6f 6c 2e 0a 2a 2a 0a 2a 2a 20 69  er iCol..**.** i
2abca 42 72 65 61 6b 20 69 73 20 73 75 67 67 65 73 74  Break is suggest
2abcb 65 64 20 73 70 6f 74 20 69 6e 20 7a 44 6f 63 20  ed spot in zDoc 
2abcc 77 68 65 72 65 20 77 65 20 63 6f 75 6c 64 20 62  where we could b
2abcd 65 67 69 6e 20 6f 72 20 65 6e 64 20 61 6e 0a 2a  egin or end an.*
2abce 2a 20 65 78 63 65 72 70 74 2e 20 20 52 65 74 75  * excerpt.  Retu
2abcf 72 6e 20 61 20 76 61 6c 75 65 20 73 69 6d 69 6c  rn a value simil
2abd0 61 72 20 74 6f 20 69 42 72 65 61 6b 20 62 75 74  ar to iBreak but
2abd1 20 70 6f 73 73 69 62 6c 79 20 61 64 6a 75 73 74   possibly adjust
2abd2 65 64 0a 2a 2a 20 74 6f 20 62 65 20 61 20 6c 69  ed.** to be a li
2abd3 74 74 6c 65 20 6c 65 66 74 20 6f 72 20 72 69 67  ttle left or rig
2abd4 68 74 20 73 6f 20 74 68 61 74 20 74 68 65 20 62  ht so that the b
2abd5 72 65 61 6b 20 70 6f 69 6e 74 20 69 73 20 62 65  reak point is be
2abd6 74 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tter..*/.static 
2abd7 69 6e 74 20 77 6f 72 64 42 6f 75 6e 64 61 72 79  int wordBoundary
2abd8 28 0a 20 20 69 6e 74 20 69 42 72 65 61 6b 2c 20  (.  int iBreak, 
2abd9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2abda 20 20 2f 2a 20 54 68 65 20 73 75 67 67 65 73 74    /* The suggest
2abdb 65 64 20 62 72 65 61 6b 20 70 6f 69 6e 74 20 2a  ed break point *
2abdc 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
2abdd 7a 44 6f 63 2c 20 20 20 20 20 20 20 20 20 20 20  zDoc,           
2abde 20 20 2f 2a 20 44 6f 63 75 6d 65 6e 74 20 74 65    /* Document te
2abdf 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 6f 63  xt */.  int nDoc
2abe0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2abe1 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2abe2 6f 66 20 62 79 74 65 73 20 69 6e 20 7a 44 6f 63  of bytes in zDoc
2abe3 5b 5d 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73  [] */.  struct s
2abe4 6e 69 70 70 65 74 4d 61 74 63 68 20 2a 61 4d 61  nippetMatch *aMa
2abe5 74 63 68 2c 20 20 2f 2a 20 4d 61 74 63 68 69 6e  tch,  /* Matchin
2abe6 67 20 77 6f 72 64 73 20 2a 2f 0a 20 20 69 6e 74  g words */.  int
2abe7 20 6e 4d 61 74 63 68 2c 20 20 20 20 20 20 20 20   nMatch,        
2abe8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
2abe9 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
2abea 69 6e 20 61 4d 61 74 63 68 5b 5d 20 2a 2f 0a 20  in aMatch[] */. 
2abeb 20 69 6e 74 20 69 43 6f 6c 20 20 20 20 20 20 20   int iCol       
2abec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2abed 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d  * The column num
2abee 62 65 72 20 66 6f 72 20 7a 44 6f 63 5b 5d 20 2a  ber for zDoc[] *
2abef 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  /.){.  int i;.  
2abf0 69 66 28 20 69 42 72 65 61 6b 3c 3d 31 30 20 29  if( iBreak<=10 )
2abf1 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
2abf2 20 20 7d 0a 20 20 69 66 28 20 69 42 72 65 61 6b    }.  if( iBreak
2abf3 3e 3d 6e 44 6f 63 2d 31 30 20 29 7b 0a 20 20 20  >=nDoc-10 ){.   
2abf4 20 72 65 74 75 72 6e 20 6e 44 6f 63 3b 0a 20 20   return nDoc;.  
2abf5 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  }.  for(i=0; i<n
2abf6 4d 61 74 63 68 20 26 26 20 61 4d 61 74 63 68 5b  Match && aMatch[
2abf7 69 5d 2e 69 43 6f 6c 3c 69 43 6f 6c 3b 20 69 2b  i].iCol<iCol; i+
2abf8 2b 29 7b 7d 0a 20 20 77 68 69 6c 65 28 20 69 3c  +){}.  while( i<
2abf9 6e 4d 61 74 63 68 20 26 26 20 61 4d 61 74 63 68  nMatch && aMatch
2abfa 5b 69 5d 2e 69 53 74 61 72 74 2b 61 4d 61 74 63  [i].iStart+aMatc
2abfb 68 5b 69 5d 2e 6e 42 79 74 65 3c 69 42 72 65 61  h[i].nByte<iBrea
2abfc 6b 20 29 7b 20 69 2b 2b 3b 20 7d 0a 20 20 69 66  k ){ i++; }.  if
2abfd 28 20 69 3c 6e 4d 61 74 63 68 20 29 7b 0a 20 20  ( i<nMatch ){.  
2abfe 20 20 69 66 28 20 61 4d 61 74 63 68 5b 69 5d 2e    if( aMatch[i].
2abff 69 53 74 61 72 74 3c 69 42 72 65 61 6b 2b 31 30  iStart<iBreak+10
2ac00 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
2ac01 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61 72   aMatch[i].iStar
2ac02 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  t;.    }.    if(
2ac03 20 69 3e 30 20 26 26 20 61 4d 61 74 63 68 5b 69   i>0 && aMatch[i
2ac04 2d 31 5d 2e 69 53 74 61 72 74 2b 61 4d 61 74 63  -1].iStart+aMatc
2ac05 68 5b 69 2d 31 5d 2e 6e 42 79 74 65 3e 3d 69 42  h[i-1].nByte>=iB
2ac06 72 65 61 6b 20 29 7b 0a 20 20 20 20 20 20 72 65  reak ){.      re
2ac07 74 75 72 6e 20 61 4d 61 74 63 68 5b 69 2d 31 5d  turn aMatch[i-1]
2ac08 2e 69 53 74 61 72 74 3b 0a 20 20 20 20 7d 0a 20  .iStart;.    }. 
2ac09 20 7d 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c   }.  for(i=1; i<
2ac0a 3d 31 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  =10; i++){.    i
2ac0b 66 28 20 73 61 66 65 5f 69 73 73 70 61 63 65 28  f( safe_isspace(
2ac0c 7a 44 6f 63 5b 69 42 72 65 61 6b 2d 69 5d 29 20  zDoc[iBreak-i]) 
2ac0d 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
2ac0e 69 42 72 65 61 6b 20 2d 20 69 20 2b 20 31 3b 0a  iBreak - i + 1;.
2ac0f 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 61      }.    if( sa
2ac10 66 65 5f 69 73 73 70 61 63 65 28 7a 44 6f 63 5b  fe_isspace(zDoc[
2ac11 69 42 72 65 61 6b 2b 69 5d 29 20 29 7b 0a 20 20  iBreak+i]) ){.  
2ac12 20 20 20 20 72 65 74 75 72 6e 20 69 42 72 65 61      return iBrea
2ac13 6b 20 2b 20 69 20 2b 20 31 3b 0a 20 20 20 20 7d  k + i + 1;.    }
2ac14 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 42  .  }.  return iB
2ac15 72 65 61 6b 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a  reak;.}..../*.**
2ac16 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20   Allowed values 
2ac17 66 6f 72 20 53 6e 69 70 70 65 74 2e 61 4d 61 74  for Snippet.aMat
2ac18 63 68 5b 5d 2e 73 6e 53 74 61 74 75 73 0a 2a 2f  ch[].snStatus.*/
2ac19 0a 23 64 65 66 69 6e 65 20 53 4e 49 50 50 45 54  .#define SNIPPET
2ac1a 5f 49 47 4e 4f 52 45 20 20 30 20 20 20 2f 2a 20  _IGNORE  0   /* 
2ac1b 49 74 20 69 73 20 6f 6b 20 74 6f 20 6f 6d 69 74  It is ok to omit
2ac1c 20 74 68 69 73 20 6d 61 74 63 68 20 66 72 6f 6d   this match from
2ac1d 20 74 68 65 20 73 6e 69 70 70 65 74 20 2a 2f 0a   the snippet */.
2ac1e 23 64 65 66 69 6e 65 20 53 4e 49 50 50 45 54 5f  #define SNIPPET_
2ac1f 44 45 53 49 52 45 44 20 31 20 20 20 2f 2a 20 57  DESIRED 1   /* W
2ac20 65 20 77 61 6e 74 20 74 6f 20 69 6e 63 6c 75 64  e want to includ
2ac21 65 20 74 68 69 73 20 6d 61 74 63 68 20 69 6e 20  e this match in 
2ac22 74 68 65 20 73 6e 69 70 70 65 74 20 2a 2f 0a 0a  the snippet */..
2ac23 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 74  /*.** Generate t
2ac24 68 65 20 74 65 78 74 20 6f 66 20 61 20 73 6e 69  he text of a sni
2ac25 70 70 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ppet..*/.static 
2ac26 76 6f 69 64 20 73 6e 69 70 70 65 74 54 65 78 74  void snippetText
2ac27 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72  (.  fulltext_cur
2ac28 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 20 20  sor *pCursor,   
2ac29 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 77 65  /* The cursor we
2ac2a 20 6e 65 65 64 20 74 68 65 20 73 6e 69 70 70 65   need the snippe
2ac2b 74 20 66 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74  t for */.  const
2ac2c 20 63 68 61 72 20 2a 7a 53 74 61 72 74 4d 61 72   char *zStartMar
2ac2d 6b 2c 20 20 20 20 20 2f 2a 20 4d 61 72 6b 75 70  k,     /* Markup
2ac2e 20 74 6f 20 61 70 70 65 61 72 20 62 65 66 6f 72   to appear befor
2ac2f 65 20 65 61 63 68 20 6d 61 74 63 68 20 2a 2f 0a  e each match */.
2ac30 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45    const char *zE
2ac31 6e 64 4d 61 72 6b 2c 20 20 20 20 20 20 20 2f 2a  ndMark,       /*
2ac32 20 4d 61 72 6b 75 70 20 74 6f 20 61 70 70 65 61   Markup to appea
2ac33 72 20 61 66 74 65 72 20 65 61 63 68 20 6d 61 74  r after each mat
2ac34 63 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ch */.  const ch
2ac35 61 72 20 2a 7a 45 6c 6c 69 70 73 69 73 20 20 20  ar *zEllipsis   
2ac36 20 20 20 20 2f 2a 20 45 6c 6c 69 70 73 69 73 20      /* Ellipsis 
2ac37 6d 61 72 6b 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  mark */.){.  int
2ac38 20 69 2c 20 6a 3b 0a 20 20 73 74 72 75 63 74 20   i, j;.  struct 
2ac39 73 6e 69 70 70 65 74 4d 61 74 63 68 20 2a 61 4d  snippetMatch *aM
2ac3a 61 74 63 68 3b 0a 20 20 69 6e 74 20 6e 4d 61 74  atch;.  int nMat
2ac3b 63 68 3b 0a 20 20 69 6e 74 20 6e 44 65 73 69 72  ch;.  int nDesir
2ac3c 65 64 3b 0a 20 20 53 74 72 69 6e 67 42 75 66 66  ed;.  StringBuff
2ac3d 65 72 20 73 62 3b 0a 20 20 69 6e 74 20 74 61 69  er sb;.  int tai
2ac3e 6c 43 6f 6c 3b 0a 20 20 69 6e 74 20 74 61 69 6c  lCol;.  int tail
2ac3f 4f 66 66 73 65 74 3b 0a 20 20 69 6e 74 20 69 43  Offset;.  int iC
2ac40 6f 6c 3b 0a 20 20 69 6e 74 20 6e 44 6f 63 3b 0a  ol;.  int nDoc;.
2ac41 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
2ac42 6f 63 3b 0a 20 20 69 6e 74 20 69 53 74 61 72 74  oc;.  int iStart
2ac43 2c 20 69 45 6e 64 3b 0a 20 20 69 6e 74 20 74 61  , iEnd;.  int ta
2ac44 69 6c 45 6c 6c 69 70 73 69 73 20 3d 20 30 3b 0a  ilEllipsis = 0;.
2ac45 20 20 69 6e 74 20 69 4d 61 74 63 68 3b 0a 20 20    int iMatch;.  
2ac46 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
2ac47 28 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65  (pCursor->snippe
2ac48 74 2e 7a 53 6e 69 70 70 65 74 29 3b 0a 20 20 70  t.zSnippet);.  p
2ac49 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e  Cursor->snippet.
2ac4a 7a 53 6e 69 70 70 65 74 20 3d 20 30 3b 0a 20 20  zSnippet = 0;.  
2ac4b 61 4d 61 74 63 68 20 3d 20 70 43 75 72 73 6f 72  aMatch = pCursor
2ac4c 2d 3e 73 6e 69 70 70 65 74 2e 61 4d 61 74 63 68  ->snippet.aMatch
2ac4d 3b 0a 20 20 6e 4d 61 74 63 68 20 3d 20 70 43 75  ;.  nMatch = pCu
2ac4e 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 6e 4d  rsor->snippet.nM
2ac4f 61 74 63 68 3b 0a 20 20 69 6e 69 74 53 74 72 69  atch;.  initStri
2ac50 6e 67 42 75 66 66 65 72 28 26 73 62 29 3b 0a 0a  ngBuffer(&sb);..
2ac51 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4d 61    for(i=0; i<nMa
2ac52 74 63 68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61  tch; i++){.    a
2ac53 4d 61 74 63 68 5b 69 5d 2e 73 6e 53 74 61 74 75  Match[i].snStatu
2ac54 73 20 3d 20 53 4e 49 50 50 45 54 5f 49 47 4e 4f  s = SNIPPET_IGNO
2ac55 52 45 3b 0a 20 20 7d 0a 20 20 6e 44 65 73 69 72  RE;.  }.  nDesir
2ac56 65 64 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d  ed = 0;.  for(i=
2ac57 30 3b 20 69 3c 70 43 75 72 73 6f 72 2d 3e 71 2e  0; i<pCursor->q.
2ac58 6e 54 65 72 6d 73 3b 20 69 2b 2b 29 7b 0a 20 20  nTerms; i++){.  
2ac59 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 4d 61    for(j=0; j<nMa
2ac5a 74 63 68 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  tch; j++){.     
2ac5b 20 69 66 28 20 61 4d 61 74 63 68 5b 6a 5d 2e 69   if( aMatch[j].i
2ac5c 54 65 72 6d 3d 3d 69 20 29 7b 0a 20 20 20 20 20  Term==i ){.     
2ac5d 20 20 20 61 4d 61 74 63 68 5b 6a 5d 2e 73 6e 53     aMatch[j].snS
2ac5e 74 61 74 75 73 20 3d 20 53 4e 49 50 50 45 54 5f  tatus = SNIPPET_
2ac5f 44 45 53 49 52 45 44 3b 0a 20 20 20 20 20 20 20  DESIRED;.       
2ac60 20 6e 44 65 73 69 72 65 64 2b 2b 3b 0a 20 20 20   nDesired++;.   
2ac61 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2ac62 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
2ac63 20 69 4d 61 74 63 68 20 3d 20 30 3b 0a 20 20 74   iMatch = 0;.  t
2ac64 61 69 6c 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 74  ailCol = -1;.  t
2ac65 61 69 6c 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20  ailOffset = 0;. 
2ac66 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4d 61 74   for(i=0; i<nMat
2ac67 63 68 20 26 26 20 6e 44 65 73 69 72 65 64 3e 30  ch && nDesired>0
2ac68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
2ac69 61 4d 61 74 63 68 5b 69 5d 2e 73 6e 53 74 61 74  aMatch[i].snStat
2ac6a 75 73 21 3d 53 4e 49 50 50 45 54 5f 44 45 53 49  us!=SNIPPET_DESI
2ac6b 52 45 44 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  RED ) continue;.
2ac6c 20 20 20 20 6e 44 65 73 69 72 65 64 2d 2d 3b 0a      nDesired--;.
2ac6d 20 20 20 20 69 43 6f 6c 20 3d 20 61 4d 61 74 63      iCol = aMatc
2ac6e 68 5b 69 5d 2e 69 43 6f 6c 3b 0a 20 20 20 20 7a  h[i].iCol;.    z
2ac6f 44 6f 63 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  Doc = (const cha
2ac70 72 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  r*)sqlite3_colum
2ac71 6e 5f 74 65 78 74 28 70 43 75 72 73 6f 72 2d 3e  n_text(pCursor->
2ac72 70 53 74 6d 74 2c 20 69 43 6f 6c 2b 31 29 3b 0a  pStmt, iCol+1);.
2ac73 20 20 20 20 6e 44 6f 63 20 3d 20 73 71 6c 69 74      nDoc = sqlit
2ac74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
2ac75 70 43 75 72 73 6f 72 2d 3e 70 53 74 6d 74 2c 20  pCursor->pStmt, 
2ac76 69 43 6f 6c 2b 31 29 3b 0a 20 20 20 20 69 53 74  iCol+1);.    iSt
2ac77 61 72 74 20 3d 20 61 4d 61 74 63 68 5b 69 5d 2e  art = aMatch[i].
2ac78 69 53 74 61 72 74 20 2d 20 34 30 3b 0a 20 20 20  iStart - 40;.   
2ac79 20 69 53 74 61 72 74 20 3d 20 77 6f 72 64 42 6f   iStart = wordBo
2ac7a 75 6e 64 61 72 79 28 69 53 74 61 72 74 2c 20 7a  undary(iStart, z
2ac7b 44 6f 63 2c 20 6e 44 6f 63 2c 20 61 4d 61 74 63  Doc, nDoc, aMatc
2ac7c 68 2c 20 6e 4d 61 74 63 68 2c 20 69 43 6f 6c 29  h, nMatch, iCol)
2ac7d 3b 0a 20 20 20 20 69 66 28 20 69 53 74 61 72 74  ;.    if( iStart
2ac7e 3c 3d 31 30 20 29 7b 0a 20 20 20 20 20 20 69 53  <=10 ){.      iS
2ac7f 74 61 72 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  tart = 0;.    }.
2ac80 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 74 61      if( iCol==ta
2ac81 69 6c 43 6f 6c 20 26 26 20 69 53 74 61 72 74 3c  ilCol && iStart<
2ac82 3d 74 61 69 6c 4f 66 66 73 65 74 2b 32 30 20 29  =tailOffset+20 )
2ac83 7b 0a 20 20 20 20 20 20 69 53 74 61 72 74 20 3d  {.      iStart =
2ac84 20 74 61 69 6c 4f 66 66 73 65 74 3b 0a 20 20 20   tailOffset;.   
2ac85 20 7d 0a 20 20 20 20 69 66 28 20 28 69 43 6f 6c   }.    if( (iCol
2ac86 21 3d 74 61 69 6c 43 6f 6c 20 26 26 20 74 61 69  !=tailCol && tai
2ac87 6c 43 6f 6c 3e 3d 30 29 20 7c 7c 20 69 53 74 61  lCol>=0) || iSta
2ac88 72 74 21 3d 74 61 69 6c 4f 66 66 73 65 74 20 29  rt!=tailOffset )
2ac89 7b 0a 20 20 20 20 20 20 74 72 69 6d 57 68 69 74  {.      trimWhit
2ac8a 65 53 70 61 63 65 28 26 73 62 29 3b 0a 20 20 20  eSpace(&sb);.   
2ac8b 20 20 20 61 70 70 65 6e 64 57 68 69 74 65 53 70     appendWhiteSp
2ac8c 61 63 65 28 26 73 62 29 3b 0a 20 20 20 20 20 20  ace(&sb);.      
2ac8d 61 70 70 65 6e 64 28 26 73 62 2c 20 7a 45 6c 6c  append(&sb, zEll
2ac8e 69 70 73 69 73 29 3b 0a 20 20 20 20 20 20 61 70  ipsis);.      ap
2ac8f 70 65 6e 64 57 68 69 74 65 53 70 61 63 65 28 26  pendWhiteSpace(&
2ac90 73 62 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  sb);.    }.    i
2ac91 45 6e 64 20 3d 20 61 4d 61 74 63 68 5b 69 5d 2e  End = aMatch[i].
2ac92 69 53 74 61 72 74 20 2b 20 61 4d 61 74 63 68 5b  iStart + aMatch[
2ac93 69 5d 2e 6e 42 79 74 65 20 2b 20 34 30 3b 0a 20  i].nByte + 40;. 
2ac94 20 20 20 69 45 6e 64 20 3d 20 77 6f 72 64 42 6f     iEnd = wordBo
2ac95 75 6e 64 61 72 79 28 69 45 6e 64 2c 20 7a 44 6f  undary(iEnd, zDo
2ac96 63 2c 20 6e 44 6f 63 2c 20 61 4d 61 74 63 68 2c  c, nDoc, aMatch,
2ac97 20 6e 4d 61 74 63 68 2c 20 69 43 6f 6c 29 3b 0a   nMatch, iCol);.
2ac98 20 20 20 20 69 66 28 20 69 45 6e 64 3e 3d 6e 44      if( iEnd>=nD
2ac99 6f 63 2d 31 30 20 29 7b 0a 20 20 20 20 20 20 69  oc-10 ){.      i
2ac9a 45 6e 64 20 3d 20 6e 44 6f 63 3b 0a 20 20 20 20  End = nDoc;.    
2ac9b 20 20 74 61 69 6c 45 6c 6c 69 70 73 69 73 20 3d    tailEllipsis =
2ac9c 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   0;.    }else{. 
2ac9d 20 20 20 20 20 74 61 69 6c 45 6c 6c 69 70 73 69       tailEllipsi
2ac9e 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  s = 1;.    }.   
2ac9f 20 77 68 69 6c 65 28 20 69 4d 61 74 63 68 3c 6e   while( iMatch<n
2aca0 4d 61 74 63 68 20 26 26 20 61 4d 61 74 63 68 5b  Match && aMatch[
2aca1 69 4d 61 74 63 68 5d 2e 69 43 6f 6c 3c 69 43 6f  iMatch].iCol<iCo
2aca2 6c 20 29 7b 20 69 4d 61 74 63 68 2b 2b 3b 20 7d  l ){ iMatch++; }
2aca3 0a 20 20 20 20 77 68 69 6c 65 28 20 69 53 74 61  .    while( iSta
2aca4 72 74 3c 69 45 6e 64 20 29 7b 0a 20 20 20 20 20  rt<iEnd ){.     
2aca5 20 77 68 69 6c 65 28 20 69 4d 61 74 63 68 3c 6e   while( iMatch<n
2aca6 4d 61 74 63 68 20 26 26 20 61 4d 61 74 63 68 5b  Match && aMatch[
2aca7 69 4d 61 74 63 68 5d 2e 69 53 74 61 72 74 3c 69  iMatch].iStart<i
2aca8 53 74 61 72 74 0a 20 20 20 20 20 20 20 20 20 20  Start.          
2aca9 20 20 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61     && aMatch[iMa
2acaa 74 63 68 5d 2e 69 43 6f 6c 3c 3d 69 43 6f 6c 20  tch].iCol<=iCol 
2acab 29 7b 0a 20 20 20 20 20 20 20 20 69 4d 61 74 63  ){.        iMatc
2acac 68 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  h++;.      }.   
2acad 20 20 20 69 66 28 20 69 4d 61 74 63 68 3c 6e 4d     if( iMatch<nM
2acae 61 74 63 68 20 26 26 20 61 4d 61 74 63 68 5b 69  atch && aMatch[i
2acaf 4d 61 74 63 68 5d 2e 69 53 74 61 72 74 3c 69 45  Match].iStart<iE
2acb0 6e 64 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  nd.             
2acb1 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68  && aMatch[iMatch
2acb2 5d 2e 69 43 6f 6c 3d 3d 69 43 6f 6c 20 29 7b 0a  ].iCol==iCol ){.
2acb3 20 20 20 20 20 20 20 20 6e 61 70 70 65 6e 64 28          nappend(
2acb4 26 73 62 2c 20 26 7a 44 6f 63 5b 69 53 74 61 72  &sb, &zDoc[iStar
2acb5 74 5d 2c 20 61 4d 61 74 63 68 5b 69 4d 61 74 63  t], aMatch[iMatc
2acb6 68 5d 2e 69 53 74 61 72 74 20 2d 20 69 53 74 61  h].iStart - iSta
2acb7 72 74 29 3b 0a 20 20 20 20 20 20 20 20 69 53 74  rt);.        iSt
2acb8 61 72 74 20 3d 20 61 4d 61 74 63 68 5b 69 4d 61  art = aMatch[iMa
2acb9 74 63 68 5d 2e 69 53 74 61 72 74 3b 0a 20 20 20  tch].iStart;.   
2acba 20 20 20 20 20 61 70 70 65 6e 64 28 26 73 62 2c       append(&sb,
2acbb 20 7a 53 74 61 72 74 4d 61 72 6b 29 3b 0a 20 20   zStartMark);.  
2acbc 20 20 20 20 20 20 6e 61 70 70 65 6e 64 28 26 73        nappend(&s
2acbd 62 2c 20 26 7a 44 6f 63 5b 69 53 74 61 72 74 5d  b, &zDoc[iStart]
2acbe 2c 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d  , aMatch[iMatch]
2acbf 2e 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 20  .nByte);.       
2acc0 20 61 70 70 65 6e 64 28 26 73 62 2c 20 7a 45 6e   append(&sb, zEn
2acc1 64 4d 61 72 6b 29 3b 0a 20 20 20 20 20 20 20 20  dMark);.        
2acc2 69 53 74 61 72 74 20 2b 3d 20 61 4d 61 74 63 68  iStart += aMatch
2acc3 5b 69 4d 61 74 63 68 5d 2e 6e 42 79 74 65 3b 0a  [iMatch].nByte;.
2acc4 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 69 4d          for(j=iM
2acc5 61 74 63 68 2b 31 3b 20 6a 3c 6e 4d 61 74 63 68  atch+1; j<nMatch
2acc6 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
2acc7 20 20 69 66 28 20 61 4d 61 74 63 68 5b 6a 5d 2e    if( aMatch[j].
2acc8 69 54 65 72 6d 3d 3d 61 4d 61 74 63 68 5b 69 4d  iTerm==aMatch[iM
2acc9 61 74 63 68 5d 2e 69 54 65 72 6d 0a 20 20 20 20  atch].iTerm.    
2acca 20 20 20 20 20 20 20 20 20 20 26 26 20 61 4d 61            && aMa
2accb 74 63 68 5b 6a 5d 2e 73 6e 53 74 61 74 75 73 3d  tch[j].snStatus=
2accc 3d 53 4e 49 50 50 45 54 5f 44 45 53 49 52 45 44  =SNIPPET_DESIRED
2accd 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2acce 6e 44 65 73 69 72 65 64 2d 2d 3b 0a 20 20 20 20  nDesired--;.    
2accf 20 20 20 20 20 20 20 20 61 4d 61 74 63 68 5b 6a          aMatch[j
2acd0 5d 2e 73 6e 53 74 61 74 75 73 20 3d 20 53 4e 49  ].snStatus = SNI
2acd1 50 50 45 54 5f 49 47 4e 4f 52 45 3b 0a 20 20 20  PPET_IGNORE;.   
2acd2 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2acd3 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a   }.      }else{.
2acd4 20 20 20 20 20 20 20 20 6e 61 70 70 65 6e 64 28          nappend(
2acd5 26 73 62 2c 20 26 7a 44 6f 63 5b 69 53 74 61 72  &sb, &zDoc[iStar
2acd6 74 5d 2c 20 69 45 6e 64 20 2d 20 69 53 74 61 72  t], iEnd - iStar
2acd7 74 29 3b 0a 20 20 20 20 20 20 20 20 69 53 74 61  t);.        iSta
2acd8 72 74 20 3d 20 69 45 6e 64 3b 0a 20 20 20 20 20  rt = iEnd;.     
2acd9 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 74 61 69   }.    }.    tai
2acda 6c 43 6f 6c 20 3d 20 69 43 6f 6c 3b 0a 20 20 20  lCol = iCol;.   
2acdb 20 74 61 69 6c 4f 66 66 73 65 74 20 3d 20 69 45   tailOffset = iE
2acdc 6e 64 3b 0a 20 20 7d 0a 20 20 74 72 69 6d 57 68  nd;.  }.  trimWh
2acdd 69 74 65 53 70 61 63 65 28 26 73 62 29 3b 0a 20  iteSpace(&sb);. 
2acde 20 69 66 28 20 74 61 69 6c 45 6c 6c 69 70 73 69   if( tailEllipsi
2acdf 73 20 29 7b 0a 20 20 20 20 61 70 70 65 6e 64 57  s ){.    appendW
2ace0 68 69 74 65 53 70 61 63 65 28 26 73 62 29 3b 0a  hiteSpace(&sb);.
2ace1 20 20 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20      append(&sb, 
2ace2 7a 45 6c 6c 69 70 73 69 73 29 3b 0a 20 20 7d 0a  zEllipsis);.  }.
2ace3 20 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70    pCursor->snipp
2ace4 65 74 2e 7a 53 6e 69 70 70 65 74 20 3d 20 73 74  et.zSnippet = st
2ace5 72 69 6e 67 42 75 66 66 65 72 44 61 74 61 28 26  ringBufferData(&
2ace6 73 62 29 3b 0a 20 20 70 43 75 72 73 6f 72 2d 3e  sb);.  pCursor->
2ace7 73 6e 69 70 70 65 74 2e 6e 53 6e 69 70 70 65 74  snippet.nSnippet
2ace8 20 3d 20 73 74 72 69 6e 67 42 75 66 66 65 72 4c   = stringBufferL
2ace9 65 6e 67 74 68 28 26 73 62 29 3b 0a 7d 0a 0a 0a  ength(&sb);.}...
2acea 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20  /*.** Close the 
2aceb 63 75 72 73 6f 72 2e 20 20 46 6f 72 20 61 64 64  cursor.  For add
2acec 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
2aced 69 6f 6e 20 73 65 65 20 74 68 65 20 64 6f 63 75  ion see the docu
2acee 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 6e 20  mentation.** on 
2acef 74 68 65 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f  the xClose metho
2acf0 64 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c  d of the virtual
2acf1 20 74 61 62 6c 65 20 69 6e 74 65 72 66 61 63 65   table interface
2acf2 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2acf3 66 75 6c 6c 74 65 78 74 43 6c 6f 73 65 28 73 71  fulltextClose(sq
2acf4 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
2acf5 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 66  r *pCursor){.  f
2acf6 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a  ulltext_cursor *
2acf7 63 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 63 75  c = (fulltext_cu
2acf8 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72 3b  rsor *) pCursor;
2acf9 0a 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54  .  FTSTRACE(("FT
2acfa 53 33 20 43 6c 6f 73 65 20 25 70 5c 6e 22 2c 20  S3 Close %p\n", 
2acfb 63 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  c));.  sqlite3_f
2acfc 69 6e 61 6c 69 7a 65 28 63 2d 3e 70 53 74 6d 74  inalize(c->pStmt
2acfd 29 3b 0a 20 20 71 75 65 72 79 43 6c 65 61 72 28  );.  queryClear(
2acfe 26 63 2d 3e 71 29 3b 0a 20 20 73 6e 69 70 70 65  &c->q);.  snippe
2acff 74 43 6c 65 61 72 28 26 63 2d 3e 73 6e 69 70 70  tClear(&c->snipp
2ad00 65 74 29 3b 0a 20 20 69 66 28 20 63 2d 3e 72 65  et);.  if( c->re
2ad01 73 75 6c 74 2e 6e 44 61 74 61 21 3d 30 20 29 20  sult.nData!=0 ) 
2ad02 64 6c 72 44 65 73 74 72 6f 79 28 26 63 2d 3e 72  dlrDestroy(&c->r
2ad03 65 61 64 65 72 29 3b 0a 20 20 64 61 74 61 42 75  eader);.  dataBu
2ad04 66 66 65 72 44 65 73 74 72 6f 79 28 26 63 2d 3e  fferDestroy(&c->
2ad05 72 65 73 75 6c 74 29 3b 0a 20 20 73 71 6c 69 74  result);.  sqlit
2ad06 65 33 5f 66 72 65 65 28 63 29 3b 0a 20 20 72 65  e3_free(c);.  re
2ad07 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
2ad08 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75  }..static int fu
2ad09 6c 6c 74 65 78 74 4e 65 78 74 28 73 71 6c 69 74  lltextNext(sqlit
2ad0a 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a  e3_vtab_cursor *
2ad0b 70 43 75 72 73 6f 72 29 7b 0a 20 20 66 75 6c 6c  pCursor){.  full
2ad0c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 63 20 3d  text_cursor *c =
2ad0d 20 28 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f   (fulltext_curso
2ad0e 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20  r *) pCursor;.  
2ad0f 69 6e 74 20 72 63 3b 0a 0a 20 20 46 54 53 54 52  int rc;..  FTSTR
2ad10 41 43 45 28 28 22 46 54 53 33 20 4e 65 78 74 20  ACE(("FTS3 Next 
2ad11 25 70 5c 6e 22 2c 20 70 43 75 72 73 6f 72 29 29  %p\n", pCursor))
2ad12 3b 0a 20 20 73 6e 69 70 70 65 74 43 6c 65 61 72  ;.  snippetClear
2ad13 28 26 63 2d 3e 73 6e 69 70 70 65 74 29 3b 0a 20  (&c->snippet);. 
2ad14 20 69 66 28 20 63 2d 3e 69 43 75 72 73 6f 72 54   if( c->iCursorT
2ad15 79 70 65 20 3c 20 51 55 45 52 59 5f 46 55 4c 4c  ype < QUERY_FULL
2ad16 54 45 58 54 20 29 7b 0a 20 20 20 20 2f 2a 20 54  TEXT ){.    /* T
2ad17 4f 44 4f 28 73 68 65 73 73 29 20 48 61 6e 64 6c  ODO(shess) Handl
2ad18 65 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20  e SQLITE_SCHEMA 
2ad19 41 4e 44 20 53 51 4c 49 54 45 5f 42 55 53 59 2e  AND SQLITE_BUSY.
2ad1a 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   */.    rc = sql
2ad1b 69 74 65 33 5f 73 74 65 70 28 63 2d 3e 70 53 74  ite3_step(c->pSt
2ad1c 6d 74 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28  mt);.    switch(
2ad1d 20 72 63 20 29 7b 0a 20 20 20 20 20 20 63 61 73   rc ){.      cas
2ad1e 65 20 53 51 4c 49 54 45 5f 52 4f 57 3a 0a 20 20  e SQLITE_ROW:.  
2ad1f 20 20 20 20 20 20 63 2d 3e 65 6f 66 20 3d 20 30        c->eof = 0
2ad20 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
2ad21 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
2ad22 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 44 4f    case SQLITE_DO
2ad23 4e 45 3a 0a 20 20 20 20 20 20 20 20 63 2d 3e 65  NE:.        c->e
2ad24 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  of = 1;.        
2ad25 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
2ad26 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a  ;.      default:
2ad27 0a 20 20 20 20 20 20 20 20 63 2d 3e 65 6f 66 20  .        c->eof 
2ad28 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72 65 74  = 1;.        ret
2ad29 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
2ad2a 7d 20 65 6c 73 65 20 7b 20 20 2f 2a 20 66 75 6c  } else {  /* ful
2ad2b 6c 2d 74 65 78 74 20 71 75 65 72 79 20 2a 2f 0a  l-text query */.
2ad2c 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
2ad2d 5f 72 65 73 65 74 28 63 2d 3e 70 53 74 6d 74 29  _reset(c->pStmt)
2ad2e 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
2ad2f 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
2ad30 20 72 63 3b 0a 0a 20 20 20 20 69 66 28 20 63 2d   rc;..    if( c-
2ad31 3e 72 65 73 75 6c 74 2e 6e 44 61 74 61 3d 3d 30  >result.nData==0
2ad32 20 7c 7c 20 64 6c 72 41 74 45 6e 64 28 26 63 2d   || dlrAtEnd(&c-
2ad33 3e 72 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20  >reader) ){.    
2ad34 20 20 63 2d 3e 65 6f 66 20 3d 20 31 3b 0a 20 20    c->eof = 1;.  
2ad35 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2ad36 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  E_OK;.    }.    
2ad37 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e  rc = sqlite3_bin
2ad38 64 5f 69 6e 74 36 34 28 63 2d 3e 70 53 74 6d 74  d_int64(c->pStmt
2ad39 2c 20 31 2c 20 64 6c 72 44 6f 63 69 64 28 26 63  , 1, dlrDocid(&c
2ad3a 2d 3e 72 65 61 64 65 72 29 29 3b 0a 20 20 20 20  ->reader));.    
2ad3b 64 6c 72 53 74 65 70 28 26 63 2d 3e 72 65 61 64  dlrStep(&c->read
2ad3c 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  er);.    if( rc!
2ad3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
2ad3e 75 72 6e 20 72 63 3b 0a 20 20 20 20 2f 2a 20 54  urn rc;.    /* T
2ad3f 4f 44 4f 28 73 68 65 73 73 29 20 48 61 6e 64 6c  ODO(shess) Handl
2ad40 65 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20  e SQLITE_SCHEMA 
2ad41 41 4e 44 20 53 51 4c 49 54 45 5f 42 55 53 59 2e  AND SQLITE_BUSY.
2ad42 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   */.    rc = sql
2ad43 69 74 65 33 5f 73 74 65 70 28 63 2d 3e 70 53 74  ite3_step(c->pSt
2ad44 6d 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  mt);.    if( rc=
2ad45 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 20 20  =SQLITE_ROW ){  
2ad46 20 2f 2a 20 74 68 65 20 63 61 73 65 20 77 65 20   /* the case we 
2ad47 65 78 70 65 63 74 20 2a 2f 0a 20 20 20 20 20 20  expect */.      
2ad48 63 2d 3e 65 6f 66 20 3d 20 30 3b 0a 20 20 20 20  c->eof = 0;.    
2ad49 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2ad4a 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a  OK;.    }.    /*
2ad4b 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 72   an error occurr
2ad4c 65 64 3b 20 61 62 6f 72 74 20 2a 2f 0a 20 20 20  ed; abort */.   
2ad4d 20 72 65 74 75 72 6e 20 72 63 3d 3d 53 51 4c 49   return rc==SQLI
2ad4e 54 45 5f 44 4f 4e 45 20 3f 20 53 51 4c 49 54 45  TE_DONE ? SQLITE
2ad4f 5f 45 52 52 4f 52 20 3a 20 72 63 3b 0a 20 20 7d  _ERROR : rc;.  }
2ad50 0a 7d 0a 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  .}.../* TODO(she
2ad51 73 73 29 20 49 66 20 77 65 20 70 75 73 68 65 64  ss) If we pushed
2ad52 20 4c 65 61 66 52 65 61 64 65 72 20 74 6f 20 74   LeafReader to t
2ad53 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 66 69  he top of the fi
2ad54 6c 65 2c 20 6f 72 20 74 6f 0a 2a 2a 20 61 6e 6f  le, or to.** ano
2ad55 74 68 65 72 20 66 69 6c 65 2c 20 74 65 72 6d 5f  ther file, term_
2ad56 73 65 6c 65 63 74 28 29 20 63 6f 75 6c 64 20 62  select() could b
2ad57 65 20 70 75 73 68 65 64 20 61 62 6f 76 65 0a 2a  e pushed above.*
2ad58 2a 20 64 6f 63 4c 69 73 74 4f 66 54 65 72 6d 28  * docListOfTerm(
2ad59 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
2ad5a 20 74 65 72 6d 53 65 6c 65 63 74 28 66 75 6c 6c   termSelect(full
2ad5b 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e  text_vtab *v, in
2ad5c 74 20 69 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20  t iColumn,.     
2ad5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ad5e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65   const char *pTe
2ad5f 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20 69  rm, int nTerm, i
2ad60 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20 20  nt isPrefix,.   
2ad61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ad62 20 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69     DocListType i
2ad63 54 79 70 65 2c 20 44 61 74 61 42 75 66 66 65 72  Type, DataBuffer
2ad64 20 2a 6f 75 74 29 3b 0a 0a 2f 2a 20 52 65 74 75   *out);../* Retu
2ad65 72 6e 20 61 20 44 6f 63 4c 69 73 74 20 63 6f 72  rn a DocList cor
2ad66 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68  responding to th
2ad67 65 20 71 75 65 72 79 20 74 65 72 6d 20 2a 70 54  e query term *pT
2ad68 65 72 6d 2e 20 20 49 66 20 2a 70 54 65 72 6d 0a  erm.  If *pTerm.
2ad69 2a 2a 20 69 73 20 74 68 65 20 66 69 72 73 74 20  ** is the first 
2ad6a 74 65 72 6d 20 6f 66 20 61 20 70 68 72 61 73 65  term of a phrase
2ad6b 20 71 75 65 72 79 2c 20 67 6f 20 61 68 65 61 64   query, go ahead
2ad6c 20 61 6e 64 20 65 76 61 6c 75 61 74 65 20 74 68   and evaluate th
2ad6d 65 20 70 68 72 61 73 65 0a 2a 2a 20 71 75 65 72  e phrase.** quer
2ad6e 79 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65  y and return the
2ad6f 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 74 68 65   doclist for the
2ad70 20 65 6e 74 69 72 65 20 70 68 72 61 73 65 20 71   entire phrase q
2ad71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  uery..**.** The 
2ad72 72 65 73 75 6c 74 69 6e 67 20 44 4c 5f 44 4f 43  resulting DL_DOC
2ad73 49 44 53 20 64 6f 63 6c 69 73 74 20 69 73 20 73  IDS doclist is s
2ad74 74 6f 72 65 64 20 69 6e 20 70 52 65 73 75 6c 74  tored in pResult
2ad75 2c 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 6f 76  , which is.** ov
2ad76 65 72 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a 73 74  erwritten..*/.st
2ad77 61 74 69 63 20 69 6e 74 20 64 6f 63 4c 69 73 74  atic int docList
2ad78 4f 66 54 65 72 6d 28 0a 20 20 66 75 6c 6c 74 65  OfTerm(.  fullte
2ad79 78 74 5f 76 74 61 62 20 2a 76 2c 20 20 20 20 2f  xt_vtab *v,    /
2ad7a 2a 20 54 68 65 20 66 75 6c 6c 20 74 65 78 74 20  * The full text 
2ad7b 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69  index */.  int i
2ad7c 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20  Column,         
2ad7d 2f 2a 20 63 6f 6c 75 6d 6e 20 74 6f 20 72 65 73  /* column to res
2ad7e 74 72 69 63 74 20 74 6f 2e 20 20 4e 6f 20 72 65  trict to.  No re
2ad7f 73 74 72 69 63 74 69 6f 6e 20 69 66 20 3e 3d 6e  striction if >=n
2ad80 43 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 51 75 65 72  Column */.  Quer
2ad81 79 54 65 72 6d 20 2a 70 51 54 65 72 6d 2c 20 20  yTerm *pQTerm,  
2ad82 20 2f 2a 20 54 65 72 6d 20 77 65 20 61 72 65 20   /* Term we are 
2ad83 6c 6f 6f 6b 69 6e 67 20 66 6f 72 2c 20 6f 72 20  looking for, or 
2ad84 31 73 74 20 74 65 72 6d 20 6f 66 20 61 20 70 68  1st term of a ph
2ad85 72 61 73 65 20 2a 2f 0a 20 20 44 61 74 61 42 75  rase */.  DataBu
2ad86 66 66 65 72 20 2a 70 52 65 73 75 6c 74 20 20 2f  ffer *pResult  /
2ad87 2a 20 57 72 69 74 65 20 74 68 65 20 72 65 73 75  * Write the resu
2ad88 6c 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  lt here */.){.  
2ad89 44 61 74 61 42 75 66 66 65 72 20 6c 65 66 74 2c  DataBuffer left,
2ad8a 20 72 69 67 68 74 2c 20 6e 65 77 3b 0a 20 20 69   right, new;.  i
2ad8b 6e 74 20 69 2c 20 72 63 3b 0a 0a 20 20 2f 2a 20  nt i, rc;..  /* 
2ad8c 4e 6f 20 70 68 72 61 73 65 20 73 65 61 72 63 68  No phrase search
2ad8d 20 69 66 20 6e 6f 20 70 6f 73 69 74 69 6f 6e 20   if no position 
2ad8e 69 6e 66 6f 2e 20 2a 2f 0a 20 20 61 73 73 65 72  info. */.  asser
2ad8f 74 28 20 70 51 54 65 72 6d 2d 3e 6e 50 68 72 61  t( pQTerm->nPhra
2ad90 73 65 3d 3d 30 20 7c 7c 20 44 4c 5f 44 45 46 41  se==0 || DL_DEFA
2ad91 55 4c 54 21 3d 44 4c 5f 44 4f 43 49 44 53 20 29  ULT!=DL_DOCIDS )
2ad92 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f 64  ;..  /* This cod
2ad93 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62  e should never b
2ad94 65 20 63 61 6c 6c 65 64 20 77 69 74 68 20 62 75  e called with bu
2ad95 66 66 65 72 65 64 20 75 70 64 61 74 65 73 2e 20  ffered updates. 
2ad96 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 76 2d 3e  */.  assert( v->
2ad97 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c 30 20 29  nPendingData<0 )
2ad98 3b 0a 0a 20 20 64 61 74 61 42 75 66 66 65 72 49  ;..  dataBufferI
2ad99 6e 69 74 28 26 6c 65 66 74 2c 20 30 29 3b 0a 20  nit(&left, 0);. 
2ad9a 20 72 63 20 3d 20 74 65 72 6d 53 65 6c 65 63 74   rc = termSelect
2ad9b 28 76 2c 20 69 43 6f 6c 75 6d 6e 2c 20 70 51 54  (v, iColumn, pQT
2ad9c 65 72 6d 2d 3e 70 54 65 72 6d 2c 20 70 51 54 65  erm->pTerm, pQTe
2ad9d 72 6d 2d 3e 6e 54 65 72 6d 2c 20 70 51 54 65 72  rm->nTerm, pQTer
2ad9e 6d 2d 3e 69 73 50 72 65 66 69 78 2c 0a 20 20 20  m->isPrefix,.   
2ad9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
2ada0 30 3c 70 51 54 65 72 6d 2d 3e 6e 50 68 72 61 73  0<pQTerm->nPhras
2ada1 65 20 3f 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53  e ? DL_POSITIONS
2ada2 20 3a 20 44 4c 5f 44 4f 43 49 44 53 29 2c 20 26   : DL_DOCIDS), &
2ada3 6c 65 66 74 29 3b 0a 20 20 69 66 28 20 72 63 20  left);.  if( rc 
2ada4 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 66  ) return rc;.  f
2ada5 6f 72 28 69 3d 31 3b 20 69 3c 3d 70 51 54 65 72  or(i=1; i<=pQTer
2ada6 6d 2d 3e 6e 50 68 72 61 73 65 20 26 26 20 6c 65  m->nPhrase && le
2ada7 66 74 2e 6e 44 61 74 61 3e 30 3b 20 69 2b 2b 29  ft.nData>0; i++)
2ada8 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73  {.    /* If this
2ada9 20 74 6f 6b 65 6e 20 69 73 20 63 6f 6e 6e 65 63   token is connec
2adaa 74 65 64 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ted to the next 
2adab 62 79 20 61 20 4e 45 41 52 20 6f 70 65 72 61 74  by a NEAR operat
2adac 6f 72 2c 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74  or, and.    ** t
2adad 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 69 73  he next token is
2adae 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 61 20   the start of a 
2adaf 70 68 72 61 73 65 2c 20 74 68 65 6e 20 73 65 74  phrase, then set
2adb0 20 6e 50 68 72 61 73 65 52 69 67 68 74 0a 20 20   nPhraseRight.  
2adb1 20 20 2a 2a 20 74 6f 20 74 68 65 20 6e 75 6d 62    ** to the numb
2adb2 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20  er of tokens in 
2adb3 74 68 65 20 70 68 72 61 73 65 2e 20 4f 74 68 65  the phrase. Othe
2adb4 72 77 69 73 65 20 6c 65 61 76 65 20 69 74 20 61  rwise leave it a
2adb5 74 20 31 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  t 1..    */.    
2adb6 69 6e 74 20 6e 50 68 72 61 73 65 52 69 67 68 74  int nPhraseRight
2adb7 20 3d 20 31 3b 0a 20 20 20 20 77 68 69 6c 65 28   = 1;.    while(
2adb8 20 28 69 2b 6e 50 68 72 61 73 65 52 69 67 68 74   (i+nPhraseRight
2adb9 29 3c 3d 70 51 54 65 72 6d 2d 3e 6e 50 68 72 61  )<=pQTerm->nPhra
2adba 73 65 20 0a 20 20 20 20 20 20 20 20 26 26 20 70  se .        && p
2adbb 51 54 65 72 6d 5b 69 2b 6e 50 68 72 61 73 65 52  QTerm[i+nPhraseR
2adbc 69 67 68 74 5d 2e 6e 4e 65 61 72 3d 3d 30 20 0a  ight].nNear==0 .
2adbd 20 20 20 20 29 7b 0a 20 20 20 20 20 20 6e 50 68      ){.      nPh
2adbe 72 61 73 65 52 69 67 68 74 2b 2b 3b 0a 20 20 20  raseRight++;.   
2adbf 20 7d 0a 0a 20 20 20 20 64 61 74 61 42 75 66 66   }..    dataBuff
2adc0 65 72 49 6e 69 74 28 26 72 69 67 68 74 2c 20 30  erInit(&right, 0
2adc1 29 3b 0a 20 20 20 20 72 63 20 3d 20 74 65 72 6d  );.    rc = term
2adc2 53 65 6c 65 63 74 28 76 2c 20 69 43 6f 6c 75 6d  Select(v, iColum
2adc3 6e 2c 20 70 51 54 65 72 6d 5b 69 5d 2e 70 54 65  n, pQTerm[i].pTe
2adc4 72 6d 2c 20 70 51 54 65 72 6d 5b 69 5d 2e 6e 54  rm, pQTerm[i].nT
2adc5 65 72 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20  erm,.           
2adc6 20 20 20 20 20 20 20 20 20 70 51 54 65 72 6d 5b           pQTerm[
2adc7 69 5d 2e 69 73 50 72 65 66 69 78 2c 20 44 4c 5f  i].isPrefix, DL_
2adc8 50 4f 53 49 54 49 4f 4e 53 2c 20 26 72 69 67 68  POSITIONS, &righ
2adc9 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  t);.    if( rc )
2adca 7b 0a 20 20 20 20 20 20 64 61 74 61 42 75 66 66  {.      dataBuff
2adcb 65 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29  erDestroy(&left)
2adcc 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
2adcd 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 61 74  c;.    }.    dat
2adce 61 42 75 66 66 65 72 49 6e 69 74 28 26 6e 65 77  aBufferInit(&new
2adcf 2c 20 30 29 3b 0a 20 20 20 20 64 6f 63 4c 69 73  , 0);.    docLis
2add0 74 50 68 72 61 73 65 4d 65 72 67 65 28 6c 65 66  tPhraseMerge(lef
2add1 74 2e 70 44 61 74 61 2c 20 6c 65 66 74 2e 6e 44  t.pData, left.nD
2add2 61 74 61 2c 20 72 69 67 68 74 2e 70 44 61 74 61  ata, right.pData
2add3 2c 20 72 69 67 68 74 2e 6e 44 61 74 61 2c 0a 20  , right.nData,. 
2add4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2add5 20 20 20 20 20 20 70 51 54 65 72 6d 5b 69 2d 31        pQTerm[i-1
2add6 5d 2e 6e 4e 65 61 72 2c 20 70 51 54 65 72 6d 5b  ].nNear, pQTerm[
2add7 69 2d 31 5d 2e 69 50 68 72 61 73 65 20 2b 20 6e  i-1].iPhrase + n
2add8 50 68 72 61 73 65 52 69 67 68 74 2c 0a 20 20 20  PhraseRight,.   
2add9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2adda 20 20 20 20 28 28 69 3c 70 51 54 65 72 6d 2d 3e      ((i<pQTerm->
2addb 6e 50 68 72 61 73 65 29 20 3f 20 44 4c 5f 50 4f  nPhrase) ? DL_PO
2addc 53 49 54 49 4f 4e 53 20 3a 20 44 4c 5f 44 4f 43  SITIONS : DL_DOC
2addd 49 44 53 29 2c 0a 20 20 20 20 20 20 20 20 20 20  IDS),.          
2adde 20 20 20 20 20 20 20 20 20 20 20 20 20 26 6e 65               &ne
2addf 77 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66  w);.    dataBuff
2ade0 65 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29  erDestroy(&left)
2ade1 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  ;.    dataBuffer
2ade2 44 65 73 74 72 6f 79 28 26 72 69 67 68 74 29 3b  Destroy(&right);
2ade3 0a 20 20 20 20 6c 65 66 74 20 3d 20 6e 65 77 3b  .    left = new;
2ade4 0a 20 20 7d 0a 20 20 2a 70 52 65 73 75 6c 74 20  .  }.  *pResult 
2ade5 3d 20 6c 65 66 74 3b 0a 20 20 72 65 74 75 72 6e  = left;.  return
2ade6 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
2ade7 2a 20 41 64 64 20 61 20 6e 65 77 20 74 65 72 6d  * Add a new term
2ade8 20 70 54 65 72 6d 5b 30 2e 2e 6e 54 65 72 6d 2d   pTerm[0..nTerm-
2ade9 31 5d 20 74 6f 20 74 68 65 20 71 75 65 72 79 20  1] to the query 
2adea 2a 71 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  *q..*/.static vo
2adeb 69 64 20 71 75 65 72 79 41 64 64 28 51 75 65 72  id queryAdd(Quer
2adec 79 20 2a 71 2c 20 63 6f 6e 73 74 20 63 68 61 72  y *q, const char
2aded 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65   *pTerm, int nTe
2adee 72 6d 29 7b 0a 20 20 51 75 65 72 79 54 65 72 6d  rm){.  QueryTerm
2adef 20 2a 74 3b 0a 20 20 2b 2b 71 2d 3e 6e 54 65 72   *t;.  ++q->nTer
2adf0 6d 73 3b 0a 20 20 71 2d 3e 70 54 65 72 6d 73 20  ms;.  q->pTerms 
2adf1 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  = sqlite3_reallo
2adf2 63 28 71 2d 3e 70 54 65 72 6d 73 2c 20 71 2d 3e  c(q->pTerms, q->
2adf3 6e 54 65 72 6d 73 20 2a 20 73 69 7a 65 6f 66 28  nTerms * sizeof(
2adf4 71 2d 3e 70 54 65 72 6d 73 5b 30 5d 29 29 3b 0a  q->pTerms[0]));.
2adf5 20 20 69 66 28 20 71 2d 3e 70 54 65 72 6d 73 3d    if( q->pTerms=
2adf6 3d 30 20 29 7b 0a 20 20 20 20 71 2d 3e 6e 54 65  =0 ){.    q->nTe
2adf7 72 6d 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 74  rms = 0;.    ret
2adf8 75 72 6e 3b 0a 20 20 7d 0a 20 20 74 20 3d 20 26  urn;.  }.  t = &
2adf9 71 2d 3e 70 54 65 72 6d 73 5b 71 2d 3e 6e 54 65  q->pTerms[q->nTe
2adfa 72 6d 73 20 2d 20 31 5d 3b 0a 20 20 43 4c 45 41  rms - 1];.  CLEA
2adfb 52 28 74 29 3b 0a 20 20 74 2d 3e 70 54 65 72 6d  R(t);.  t->pTerm
2adfc 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
2adfd 63 28 6e 54 65 72 6d 2b 31 29 3b 0a 20 20 6d 65  c(nTerm+1);.  me
2adfe 6d 63 70 79 28 74 2d 3e 70 54 65 72 6d 2c 20 70  mcpy(t->pTerm, p
2adff 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20  Term, nTerm);.  
2ae00 74 2d 3e 70 54 65 72 6d 5b 6e 54 65 72 6d 5d 20  t->pTerm[nTerm] 
2ae01 3d 20 30 3b 0a 20 20 74 2d 3e 6e 54 65 72 6d 20  = 0;.  t->nTerm 
2ae02 3d 20 6e 54 65 72 6d 3b 0a 20 20 74 2d 3e 69 73  = nTerm;.  t->is
2ae03 4f 72 20 3d 20 71 2d 3e 6e 65 78 74 49 73 4f 72  Or = q->nextIsOr
2ae04 3b 0a 20 20 74 2d 3e 69 73 50 72 65 66 69 78 20  ;.  t->isPrefix 
2ae05 3d 20 30 3b 0a 20 20 71 2d 3e 6e 65 78 74 49 73  = 0;.  q->nextIs
2ae06 4f 72 20 3d 20 30 3b 0a 20 20 74 2d 3e 69 43 6f  Or = 0;.  t->iCo
2ae07 6c 75 6d 6e 20 3d 20 71 2d 3e 6e 65 78 74 43 6f  lumn = q->nextCo
2ae08 6c 75 6d 6e 3b 0a 20 20 71 2d 3e 6e 65 78 74 43  lumn;.  q->nextC
2ae09 6f 6c 75 6d 6e 20 3d 20 71 2d 3e 64 66 6c 74 43  olumn = q->dfltC
2ae0a 6f 6c 75 6d 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  olumn;.}../*.** 
2ae0b 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
2ae0c 74 68 65 20 73 74 72 69 6e 67 20 7a 54 6f 6b 65  the string zToke
2ae0d 6e 5b 30 2e 2e 2e 6e 54 6f 6b 65 6e 2d 31 5d 20  n[0...nToken-1] 
2ae0e 6d 61 74 63 68 65 73 20 61 6e 79 0a 2a 2a 20 63  matches any.** c
2ae0f 6f 6c 75 6d 6e 20 6e 61 6d 65 20 69 6e 20 74 68  olumn name in th
2ae10 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
2ae11 20 20 20 49 66 20 69 74 20 64 6f 65 73 2c 0a 2a     If it does,.*
2ae12 2a 20 72 65 74 75 72 6e 20 74 68 65 20 7a 65 72  * return the zer
2ae13 6f 2d 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e  o-indexed column
2ae14 20 6e 75 6d 62 65 72 2e 20 20 49 66 20 6e 6f 74   number.  If not
2ae15 2c 20 72 65 74 75 72 6e 20 2d 31 2e 0a 2a 2f 0a  , return -1..*/.
2ae16 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b  static int check
2ae17 43 6f 6c 75 6d 6e 53 70 65 63 69 66 69 65 72 28  ColumnSpecifier(
2ae18 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  .  fulltext_vtab
2ae19 20 2a 70 56 74 61 62 2c 20 20 20 20 2f 2a 20 54   *pVtab,    /* T
2ae1a 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
2ae1b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
2ae1c 20 2a 7a 54 6f 6b 65 6e 2c 20 20 20 20 20 20 2f   *zToken,      /
2ae1d 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 74 6f  * Text of the to
2ae1e 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f  ken */.  int nTo
2ae1f 6b 65 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  ken             
2ae20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
2ae21 68 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 65  haracters in the
2ae22 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 69   token */.){.  i
2ae23 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nt i;.  for(i=0;
2ae24 20 69 3c 70 56 74 61 62 2d 3e 6e 43 6f 6c 75 6d   i<pVtab->nColum
2ae25 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  n; i++){.    if(
2ae26 20 6d 65 6d 63 6d 70 28 70 56 74 61 62 2d 3e 61   memcmp(pVtab->a
2ae27 7a 43 6f 6c 75 6d 6e 5b 69 5d 2c 20 7a 54 6f 6b  zColumn[i], zTok
2ae28 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3d 3d 30 0a 20  en, nToken)==0. 
2ae29 20 20 20 20 20 20 20 26 26 20 70 56 74 61 62 2d         && pVtab-
2ae2a 3e 61 7a 43 6f 6c 75 6d 6e 5b 69 5d 5b 6e 54 6f  >azColumn[i][nTo
2ae2b 6b 65 6e 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ken]==0 ){.     
2ae2c 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d   return i;.    }
2ae2d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31  .  }.  return -1
2ae2e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65  ;.}../*.** Parse
2ae2f 20 74 68 65 20 74 65 78 74 20 61 74 20 70 53 65   the text at pSe
2ae30 67 6d 65 6e 74 5b 30 2e 2e 6e 53 65 67 6d 65 6e  gment[0..nSegmen
2ae31 74 2d 31 5d 2e 20 20 41 64 64 20 61 64 64 69 74  t-1].  Add addit
2ae32 69 6f 6e 61 6c 20 74 65 72 6d 73 0a 2a 2a 20 74  ional terms.** t
2ae33 6f 20 74 68 65 20 71 75 65 72 79 20 62 65 69 6e  o the query bein
2ae34 67 20 61 73 73 65 6d 62 6c 69 65 64 20 69 6e 20  g assemblied in 
2ae35 70 51 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 69 6e  pQuery..**.** in
2ae36 50 68 72 61 73 65 20 69 73 20 74 72 75 65 20 69  Phrase is true i
2ae37 66 20 70 53 65 67 6d 65 6e 74 5b 30 2e 2e 6e 53  f pSegment[0..nS
2ae38 65 67 65 6d 65 6e 74 2d 31 5d 20 69 73 20 63 6f  egement-1] is co
2ae39 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e 0a 2a  ntained within.*
2ae3a 2a 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 73 2e  * double-quotes.
2ae3b 20 20 49 66 20 69 6e 50 68 72 61 73 65 20 69 73    If inPhrase is
2ae3c 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20   true, then the 
2ae3d 66 69 72 73 74 20 74 65 72 6d 0a 2a 2a 20 69 73  first term.** is
2ae3e 20 6d 61 72 6b 65 64 20 77 69 74 68 20 74 68 65   marked with the
2ae3f 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73   number of terms
2ae40 20 69 6e 20 74 68 65 20 70 68 72 61 73 65 20 6c   in the phrase l
2ae41 65 73 73 20 6f 6e 65 20 61 6e 64 0a 2a 2a 20 4f  ess one and.** O
2ae42 52 20 61 6e 64 20 22 2d 22 20 73 79 6e 74 61 78  R and "-" syntax
2ae43 20 69 73 20 69 67 6e 6f 72 65 64 2e 20 20 49 66   is ignored.  If
2ae44 20 69 6e 50 68 72 61 73 65 20 69 73 20 66 61 6c   inPhrase is fal
2ae45 73 65 2c 20 74 68 65 6e 20 65 76 65 72 79 0a 2a  se, then every.*
2ae46 2a 20 74 65 72 6d 20 66 6f 75 6e 64 20 69 73 20  * term found is 
2ae47 6d 61 72 6b 65 64 20 77 69 74 68 20 6e 50 68 72  marked with nPhr
2ae48 61 73 65 3d 30 20 61 6e 64 20 4f 52 20 61 6e 64  ase=0 and OR and
2ae49 20 22 2d 22 20 73 79 6e 74 61 78 20 69 73 20 73   "-" syntax is s
2ae4a 69 67 6e 69 66 69 63 61 6e 74 2e 0a 2a 2f 0a 73  ignificant..*/.s
2ae4b 74 61 74 69 63 20 69 6e 74 20 74 6f 6b 65 6e 69  tatic int tokeni
2ae4c 7a 65 53 65 67 6d 65 6e 74 28 0a 20 20 73 71 6c  zeSegment(.  sql
2ae4d 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
2ae4e 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20  pTokenizer,     
2ae4f 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65       /* The toke
2ae50 6e 69 7a 65 72 20 74 6f 20 75 73 65 20 2a 2f 0a  nizer to use */.
2ae51 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 53    const char *pS
2ae52 65 67 6d 65 6e 74 2c 20 69 6e 74 20 6e 53 65 67  egment, int nSeg
2ae53 6d 65 6e 74 2c 20 20 20 20 20 2f 2a 20 51 75 65  ment,     /* Que
2ae54 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 62 65  ry expression be
2ae55 69 6e 67 20 70 61 72 73 65 64 20 2a 2f 0a 20 20  ing parsed */.  
2ae56 69 6e 74 20 69 6e 50 68 72 61 73 65 2c 20 20 20  int inPhrase,   
2ae57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ae58 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
2ae59 69 66 20 77 69 74 68 69 6e 20 22 2e 2e 2e 22 20  if within "..." 
2ae5a 2a 2f 0a 20 20 51 75 65 72 79 20 2a 70 51 75 65  */.  Query *pQue
2ae5b 72 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ry              
2ae5c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2ae5d 41 70 70 65 6e 64 20 72 65 73 75 6c 74 73 20 68  Append results h
2ae5e 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73  ere */.){.  cons
2ae5f 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  t sqlite3_tokeni
2ae60 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  zer_module *pMod
2ae61 75 6c 65 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72  ule = pTokenizer
2ae62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 73 71 6c  ->pModule;.  sql
2ae63 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
2ae64 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a  ursor *pCursor;.
2ae65 20 20 69 6e 74 20 66 69 72 73 74 49 6e 64 65 78    int firstIndex
2ae66 20 3d 20 70 51 75 65 72 79 2d 3e 6e 54 65 72 6d   = pQuery->nTerm
2ae67 73 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20  s;.  int iCol;. 
2ae68 20 69 6e 74 20 6e 54 65 72 6d 20 3d 20 31 3b 0a   int nTerm = 1;.
2ae69 20 20 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 4d    .  int rc = pM
2ae6a 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 70 54 6f  odule->xOpen(pTo
2ae6b 6b 65 6e 69 7a 65 72 2c 20 70 53 65 67 6d 65 6e  kenizer, pSegmen
2ae6c 74 2c 20 6e 53 65 67 6d 65 6e 74 2c 20 26 70 43  t, nSegment, &pC
2ae6d 75 72 73 6f 72 29 3b 0a 20 20 69 66 28 20 72 63  ursor);.  if( rc
2ae6e 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
2ae6f 74 75 72 6e 20 72 63 3b 0a 20 20 70 43 75 72 73  turn rc;.  pCurs
2ae70 6f 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d  or->pTokenizer =
2ae71 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 0a 20 20   pTokenizer;..  
2ae72 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20  while( 1 ){.    
2ae73 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 6f 6b  const char *pTok
2ae74 65 6e 3b 0a 20 20 20 20 69 6e 74 20 6e 54 6f 6b  en;.    int nTok
2ae75 65 6e 2c 20 69 42 65 67 69 6e 2c 20 69 45 6e 64  en, iBegin, iEnd
2ae76 2c 20 69 50 6f 73 3b 0a 0a 20 20 20 20 72 63 20  , iPos;..    rc 
2ae77 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74  = pModule->xNext
2ae78 28 70 43 75 72 73 6f 72 2c 0a 20 20 20 20 20 20  (pCursor,.      
2ae79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ae7a 20 20 26 70 54 6f 6b 65 6e 2c 20 26 6e 54 6f 6b    &pToken, &nTok
2ae7b 65 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  en,.            
2ae7c 20 20 20 20 20 20 20 20 20 20 20 20 26 69 42 65              &iBe
2ae7d 67 69 6e 2c 20 26 69 45 6e 64 2c 20 26 69 50 6f  gin, &iEnd, &iPo
2ae7e 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  s);.    if( rc!=
2ae7f 53 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65 61  SQLITE_OK ) brea
2ae80 6b 3b 0a 20 20 20 20 69 66 28 20 21 69 6e 50 68  k;.    if( !inPh
2ae81 72 61 73 65 20 26 26 0a 20 20 20 20 20 20 20 20  rase &&.        
2ae82 70 53 65 67 6d 65 6e 74 5b 69 45 6e 64 5d 3d 3d  pSegment[iEnd]==
2ae83 27 3a 27 20 26 26 0a 20 20 20 20 20 20 20 20 20  ':' &&.         
2ae84 28 69 43 6f 6c 20 3d 20 63 68 65 63 6b 43 6f 6c  (iCol = checkCol
2ae85 75 6d 6e 53 70 65 63 69 66 69 65 72 28 70 51 75  umnSpecifier(pQu
2ae86 65 72 79 2d 3e 70 46 74 73 2c 20 70 54 6f 6b 65  ery->pFts, pToke
2ae87 6e 2c 20 6e 54 6f 6b 65 6e 29 29 3e 3d 30 20 29  n, nToken))>=0 )
2ae88 7b 0a 20 20 20 20 20 20 70 51 75 65 72 79 2d 3e  {.      pQuery->
2ae89 6e 65 78 74 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f  nextColumn = iCo
2ae8a 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75  l;.      continu
2ae8b 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  e;.    }.    if(
2ae8c 20 21 69 6e 50 68 72 61 73 65 20 26 26 20 70 51   !inPhrase && pQ
2ae8d 75 65 72 79 2d 3e 6e 54 65 72 6d 73 3e 30 20 26  uery->nTerms>0 &
2ae8e 26 20 6e 54 6f 6b 65 6e 3d 3d 32 20 0a 20 20 20  & nToken==2 .   
2ae8f 20 20 26 26 20 70 53 65 67 6d 65 6e 74 5b 69 42    && pSegment[iB
2ae90 65 67 69 6e 2b 30 5d 3d 3d 27 4f 27 0a 20 20 20  egin+0]=='O'.   
2ae91 20 20 26 26 20 70 53 65 67 6d 65 6e 74 5b 69 42    && pSegment[iB
2ae92 65 67 69 6e 2b 31 5d 3d 3d 27 52 27 20 0a 20 20  egin+1]=='R' .  
2ae93 20 20 29 7b 0a 20 20 20 20 20 20 70 51 75 65 72    ){.      pQuer
2ae94 79 2d 3e 6e 65 78 74 49 73 4f 72 20 3d 20 31 3b  y->nextIsOr = 1;
2ae95 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b  .      continue;
2ae96 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21  .    }.    if( !
2ae97 69 6e 50 68 72 61 73 65 20 26 26 20 70 51 75 65  inPhrase && pQue
2ae98 72 79 2d 3e 6e 54 65 72 6d 73 3e 30 20 26 26 20  ry->nTerms>0 && 
2ae99 21 70 51 75 65 72 79 2d 3e 6e 65 78 74 49 73 4f  !pQuery->nextIsO
2ae9a 72 20 26 26 20 6e 54 6f 6b 65 6e 3d 3d 34 20 0a  r && nToken==4 .
2ae9b 20 20 20 20 20 20 26 26 20 70 53 65 67 6d 65 6e        && pSegmen
2ae9c 74 5b 69 42 65 67 69 6e 2b 30 5d 3d 3d 27 4e 27  t[iBegin+0]=='N'
2ae9d 20 0a 20 20 20 20 20 20 26 26 20 70 53 65 67 6d   .      && pSegm
2ae9e 65 6e 74 5b 69 42 65 67 69 6e 2b 31 5d 3d 3d 27  ent[iBegin+1]=='
2ae9f 45 27 20 0a 20 20 20 20 20 20 26 26 20 70 53 65  E' .      && pSe
2aea0 67 6d 65 6e 74 5b 69 42 65 67 69 6e 2b 32 5d 3d  gment[iBegin+2]=
2aea1 3d 27 41 27 20 0a 20 20 20 20 20 20 26 26 20 70  ='A' .      && p
2aea2 53 65 67 6d 65 6e 74 5b 69 42 65 67 69 6e 2b 33  Segment[iBegin+3
2aea3 5d 3d 3d 27 52 27 20 0a 20 20 20 20 29 7b 0a 20  ]=='R' .    ){. 
2aea4 20 20 20 20 20 51 75 65 72 79 54 65 72 6d 20 2a       QueryTerm *
2aea5 70 54 65 72 6d 20 3d 20 26 70 51 75 65 72 79 2d  pTerm = &pQuery-
2aea6 3e 70 54 65 72 6d 73 5b 70 51 75 65 72 79 2d 3e  >pTerms[pQuery->
2aea7 6e 54 65 72 6d 73 2d 31 5d 3b 0a 20 20 20 20 20  nTerms-1];.     
2aea8 20 69 66 28 20 28 69 42 65 67 69 6e 2b 36 29 3c   if( (iBegin+6)<
2aea9 6e 53 65 67 6d 65 6e 74 20 0a 20 20 20 20 20 20  nSegment .      
2aeaa 20 26 26 20 70 53 65 67 6d 65 6e 74 5b 69 42 65   && pSegment[iBe
2aeab 67 69 6e 2b 34 5d 20 3d 3d 20 27 2f 27 0a 20 20  gin+4] == '/'.  
2aeac 20 20 20 20 20 26 26 20 70 53 65 67 6d 65 6e 74       && pSegment
2aead 5b 69 42 65 67 69 6e 2b 35 5d 3e 3d 27 30 27 20  [iBegin+5]>='0' 
2aeae 26 26 20 70 53 65 67 6d 65 6e 74 5b 69 42 65 67  && pSegment[iBeg
2aeaf 69 6e 2b 35 5d 3c 3d 27 39 27 0a 20 20 20 20 20  in+5]<='9'.     
2aeb0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 65 72   ){.        pTer
2aeb1 6d 2d 3e 6e 4e 65 61 72 20 3d 20 28 70 53 65 67  m->nNear = (pSeg
2aeb2 6d 65 6e 74 5b 69 42 65 67 69 6e 2b 35 5d 20 2d  ment[iBegin+5] -
2aeb3 20 27 30 27 29 3b 0a 20 20 20 20 20 20 20 20 6e   '0');.        n
2aeb4 54 6f 6b 65 6e 20 2b 3d 20 32 3b 0a 20 20 20 20  Token += 2;.    
2aeb5 20 20 20 20 69 66 28 20 70 53 65 67 6d 65 6e 74      if( pSegment
2aeb6 5b 69 42 65 67 69 6e 2b 36 5d 3e 3d 27 30 27 20  [iBegin+6]>='0' 
2aeb7 26 26 20 70 53 65 67 6d 65 6e 74 5b 69 42 65 67  && pSegment[iBeg
2aeb8 69 6e 2b 36 5d 3c 3d 39 20 29 7b 0a 20 20 20 20  in+6]<=9 ){.    
2aeb9 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e 4e 65        pTerm->nNe
2aeba 61 72 20 3d 20 70 54 65 72 6d 2d 3e 6e 4e 65 61  ar = pTerm->nNea
2aebb 72 20 2a 20 31 30 20 2b 20 28 70 53 65 67 6d 65  r * 10 + (pSegme
2aebc 6e 74 5b 69 42 65 67 69 6e 2b 36 5d 20 2d 20 27  nt[iBegin+6] - '
2aebd 30 27 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  0');.          i
2aebe 45 6e 64 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d  End++;.        }
2aebf 0a 20 20 20 20 20 20 20 20 70 4d 6f 64 75 6c 65  .        pModule
2aec0 2d 3e 78 4e 65 78 74 28 70 43 75 72 73 6f 72 2c  ->xNext(pCursor,
2aec1 20 26 70 54 6f 6b 65 6e 2c 20 26 6e 54 6f 6b 65   &pToken, &nToke
2aec2 6e 2c 20 26 69 42 65 67 69 6e 2c 20 26 69 45 6e  n, &iBegin, &iEn
2aec3 64 2c 20 26 69 50 6f 73 29 3b 0a 20 20 20 20 20  d, &iPos);.     
2aec4 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20   } else {.      
2aec5 20 20 70 54 65 72 6d 2d 3e 6e 4e 65 61 72 20 3d    pTerm->nNear =
2aec6 20 53 51 4c 49 54 45 5f 46 54 53 33 5f 44 45 46   SQLITE_FTS3_DEF
2aec7 41 55 4c 54 5f 4e 45 41 52 5f 50 41 52 41 4d 3b  AULT_NEAR_PARAM;
2aec8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
2aec9 54 65 72 6d 2d 3e 6e 4e 65 61 72 2b 2b 3b 0a 20  Term->nNear++;. 
2aeca 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
2aecb 20 20 20 7d 0a 0a 20 20 20 20 71 75 65 72 79 41     }..    queryA
2aecc 64 64 28 70 51 75 65 72 79 2c 20 70 54 6f 6b 65  dd(pQuery, pToke
2aecd 6e 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20  n, nToken);.    
2aece 69 66 28 20 21 69 6e 50 68 72 61 73 65 20 26 26  if( !inPhrase &&
2aecf 20 69 42 65 67 69 6e 3e 30 20 26 26 20 70 53 65   iBegin>0 && pSe
2aed0 67 6d 65 6e 74 5b 69 42 65 67 69 6e 2d 31 5d 3d  gment[iBegin-1]=
2aed1 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 70 51  ='-' ){.      pQ
2aed2 75 65 72 79 2d 3e 70 54 65 72 6d 73 5b 70 51 75  uery->pTerms[pQu
2aed3 65 72 79 2d 3e 6e 54 65 72 6d 73 2d 31 5d 2e 69  ery->nTerms-1].i
2aed4 73 4e 6f 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  sNot = 1;.    }.
2aed5 20 20 20 20 69 66 28 20 69 45 6e 64 3c 6e 53 65      if( iEnd<nSe
2aed6 67 6d 65 6e 74 20 26 26 20 70 53 65 67 6d 65 6e  gment && pSegmen
2aed7 74 5b 69 45 6e 64 5d 3d 3d 27 2a 27 20 29 7b 0a  t[iEnd]=='*' ){.
2aed8 20 20 20 20 20 20 70 51 75 65 72 79 2d 3e 70 54        pQuery->pT
2aed9 65 72 6d 73 5b 70 51 75 65 72 79 2d 3e 6e 54 65  erms[pQuery->nTe
2aeda 72 6d 73 2d 31 5d 2e 69 73 50 72 65 66 69 78 20  rms-1].isPrefix 
2aedb 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  = 1;.    }.    p
2aedc 51 75 65 72 79 2d 3e 70 54 65 72 6d 73 5b 70 51  Query->pTerms[pQ
2aedd 75 65 72 79 2d 3e 6e 54 65 72 6d 73 2d 31 5d 2e  uery->nTerms-1].
2aede 69 50 68 72 61 73 65 20 3d 20 6e 54 65 72 6d 3b  iPhrase = nTerm;
2aedf 0a 20 20 20 20 69 66 28 20 69 6e 50 68 72 61 73  .    if( inPhras
2aee0 65 20 29 7b 0a 20 20 20 20 20 20 6e 54 65 72 6d  e ){.      nTerm
2aee1 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ++;.    }.  }.. 
2aee2 20 69 66 28 20 69 6e 50 68 72 61 73 65 20 26 26   if( inPhrase &&
2aee3 20 70 51 75 65 72 79 2d 3e 6e 54 65 72 6d 73 3e   pQuery->nTerms>
2aee4 66 69 72 73 74 49 6e 64 65 78 20 29 7b 0a 20 20  firstIndex ){.  
2aee5 20 20 70 51 75 65 72 79 2d 3e 70 54 65 72 6d 73    pQuery->pTerms
2aee6 5b 66 69 72 73 74 49 6e 64 65 78 5d 2e 6e 50 68  [firstIndex].nPh
2aee7 72 61 73 65 20 3d 20 70 51 75 65 72 79 2d 3e 6e  rase = pQuery->n
2aee8 54 65 72 6d 73 20 2d 20 66 69 72 73 74 49 6e 64  Terms - firstInd
2aee9 65 78 20 2d 20 31 3b 0a 20 20 7d 0a 0a 20 20 72  ex - 1;.  }..  r
2aeea 65 74 75 72 6e 20 70 4d 6f 64 75 6c 65 2d 3e 78  eturn pModule->x
2aeeb 43 6c 6f 73 65 28 70 43 75 72 73 6f 72 29 3b 0a  Close(pCursor);.
2aeec 7d 0a 0a 2f 2a 20 50 61 72 73 65 20 61 20 71 75  }../* Parse a qu
2aeed 65 72 79 20 73 74 72 69 6e 67 2c 20 79 69 65 6c  ery string, yiel
2aeee 64 69 6e 67 20 61 20 51 75 65 72 79 20 6f 62 6a  ding a Query obj
2aeef 65 63 74 20 70 51 75 65 72 79 2e 0a 2a 2a 0a 2a  ect pQuery..**.*
2aef0 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75  * The calling fu
2aef1 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 6e 65 65 64  nction will need
2aef2 20 74 6f 20 71 75 65 72 79 43 6c 65 61 72 28 29   to queryClear()
2aef3 20 74 6f 20 63 6c 65 61 6e 20 75 70 0a 2a 2a 20   to clean up.** 
2aef4 74 68 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20  the dynamically 
2aef5 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79  allocated memory
2aef6 20 68 65 6c 64 20 62 79 20 70 51 75 65 72 79 2e   held by pQuery.
2aef7 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
2aef8 61 72 73 65 51 75 65 72 79 28 0a 20 20 66 75 6c  arseQuery(.  ful
2aef9 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 20  ltext_vtab *v,  
2aefa 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6c        /* The ful
2aefb 6c 74 65 78 74 20 69 6e 64 65 78 20 2a 2f 0a 20  ltext index */. 
2aefc 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e   const char *zIn
2aefd 70 75 74 2c 20 20 20 20 20 20 2f 2a 20 49 6e 70  put,      /* Inp
2aefe 75 74 20 74 65 78 74 20 6f 66 20 74 68 65 20 71  ut text of the q
2aeff 75 65 72 79 20 73 74 72 69 6e 67 20 2a 2f 0a 20  uery string */. 
2af00 20 69 6e 74 20 6e 49 6e 70 75 74 2c 20 20 20 20   int nInput,    
2af01 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
2af02 65 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 74  e of the input t
2af03 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 64 66 6c  ext */.  int dfl
2af04 74 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20  tColumn,        
2af05 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 63 6f 6c    /* Default col
2af06 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 78  umn of the index
2af07 20 74 6f 20 6d 61 74 63 68 20 61 67 61 69 6e 73   to match agains
2af08 74 20 2a 2f 0a 20 20 51 75 65 72 79 20 2a 70 51  t */.  Query *pQ
2af09 75 65 72 79 20 20 20 20 20 20 20 20 20 20 20 20  uery            
2af0a 2f 2a 20 57 72 69 74 65 20 74 68 65 20 70 61 72  /* Write the par
2af0b 73 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 2e  se results here.
2af0c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 49 6e   */.){.  int iIn
2af0d 70 75 74 2c 20 69 6e 50 68 72 61 73 65 20 3d 20  put, inPhrase = 
2af0e 30 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 51  0;.  int ii;.  Q
2af0f 75 65 72 79 54 65 72 6d 20 2a 61 54 65 72 6d 3b  ueryTerm *aTerm;
2af10 0a 0a 20 20 69 66 28 20 7a 49 6e 70 75 74 3d 3d  ..  if( zInput==
2af11 30 20 29 20 6e 49 6e 70 75 74 20 3d 20 30 3b 0a  0 ) nInput = 0;.
2af12 20 20 69 66 28 20 6e 49 6e 70 75 74 3c 30 20 29    if( nInput<0 )
2af13 20 6e 49 6e 70 75 74 20 3d 20 73 74 72 6c 65 6e   nInput = strlen
2af14 28 7a 49 6e 70 75 74 29 3b 0a 20 20 70 51 75 65  (zInput);.  pQue
2af15 72 79 2d 3e 6e 54 65 72 6d 73 20 3d 20 30 3b 0a  ry->nTerms = 0;.
2af16 20 20 70 51 75 65 72 79 2d 3e 70 54 65 72 6d 73    pQuery->pTerms
2af17 20 3d 20 4e 55 4c 4c 3b 0a 20 20 70 51 75 65 72   = NULL;.  pQuer
2af18 79 2d 3e 6e 65 78 74 49 73 4f 72 20 3d 20 30 3b  y->nextIsOr = 0;
2af19 0a 20 20 70 51 75 65 72 79 2d 3e 6e 65 78 74 43  .  pQuery->nextC
2af1a 6f 6c 75 6d 6e 20 3d 20 64 66 6c 74 43 6f 6c 75  olumn = dfltColu
2af1b 6d 6e 3b 0a 20 20 70 51 75 65 72 79 2d 3e 64 66  mn;.  pQuery->df
2af1c 6c 74 43 6f 6c 75 6d 6e 20 3d 20 64 66 6c 74 43  ltColumn = dfltC
2af1d 6f 6c 75 6d 6e 3b 0a 20 20 70 51 75 65 72 79 2d  olumn;.  pQuery-
2af1e 3e 70 46 74 73 20 3d 20 76 3b 0a 0a 20 20 66 6f  >pFts = v;..  fo
2af1f 72 28 69 49 6e 70 75 74 3d 30 3b 20 69 49 6e 70  r(iInput=0; iInp
2af20 75 74 3c 6e 49 6e 70 75 74 3b 20 2b 2b 69 49 6e  ut<nInput; ++iIn
2af21 70 75 74 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  put){.    int i;
2af22 0a 20 20 20 20 66 6f 72 28 69 3d 69 49 6e 70 75  .    for(i=iInpu
2af23 74 3b 20 69 3c 6e 49 6e 70 75 74 20 26 26 20 7a  t; i<nInput && z
2af24 49 6e 70 75 74 5b 69 5d 21 3d 27 22 27 3b 20 2b  Input[i]!='"'; +
2af25 2b 69 29 7b 7d 0a 20 20 20 20 69 66 28 20 69 3e  +i){}.    if( i>
2af26 69 49 6e 70 75 74 20 29 7b 0a 20 20 20 20 20 20  iInput ){.      
2af27 74 6f 6b 65 6e 69 7a 65 53 65 67 6d 65 6e 74 28  tokenizeSegment(
2af28 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a  v->pTokenizer, z
2af29 49 6e 70 75 74 2b 69 49 6e 70 75 74 2c 20 69 2d  Input+iInput, i-
2af2a 69 49 6e 70 75 74 2c 20 69 6e 50 68 72 61 73 65  iInput, inPhrase
2af2b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2af2c 20 20 20 20 20 20 20 20 20 70 51 75 65 72 79 29           pQuery)
2af2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 49 6e 70  ;.    }.    iInp
2af2e 75 74 20 3d 20 69 3b 0a 20 20 20 20 69 66 28 20  ut = i;.    if( 
2af2f 69 3c 6e 49 6e 70 75 74 20 29 7b 0a 20 20 20 20  i<nInput ){.    
2af30 20 20 61 73 73 65 72 74 28 20 7a 49 6e 70 75 74    assert( zInput
2af31 5b 69 5d 3d 3d 27 22 27 20 29 3b 0a 20 20 20 20  [i]=='"' );.    
2af32 20 20 69 6e 50 68 72 61 73 65 20 3d 20 21 69 6e    inPhrase = !in
2af33 50 68 72 61 73 65 3b 0a 20 20 20 20 7d 0a 20 20  Phrase;.    }.  
2af34 7d 0a 0a 20 20 69 66 28 20 69 6e 50 68 72 61 73  }..  if( inPhras
2af35 65 20 29 7b 0a 20 20 20 20 2f 2a 20 75 6e 6d 61  e ){.    /* unma
2af36 74 63 68 65 64 20 71 75 6f 74 65 20 2a 2f 0a 20  tched quote */. 
2af37 20 20 20 71 75 65 72 79 43 6c 65 61 72 28 70 51     queryClear(pQ
2af38 75 65 72 79 29 3b 0a 20 20 20 20 72 65 74 75 72  uery);.    retur
2af39 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
2af3a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66 79    }..  /* Modify
2af3b 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 74   the values of t
2af3c 68 65 20 51 75 65 72 79 54 65 72 6d 2e 6e 50 68  he QueryTerm.nPh
2af3d 72 61 73 65 20 76 61 72 69 61 62 6c 65 73 20 74  rase variables t
2af3e 6f 20 61 63 63 6f 75 6e 74 20 66 6f 72 0a 20 20  o account for.  
2af3f 2a 2a 20 74 68 65 20 4e 45 41 52 20 6f 70 65 72  ** the NEAR oper
2af40 61 74 6f 72 2e 20 46 6f 72 20 74 68 65 20 70 75  ator. For the pu
2af41 72 70 6f 73 65 73 20 6f 66 20 51 75 65 72 79 54  rposes of QueryT
2af42 65 72 6d 2e 6e 50 68 72 61 73 65 2c 20 70 68 72  erm.nPhrase, phr
2af43 61 73 65 73 0a 20 20 2a 2a 20 61 6e 64 20 74 6f  ases.  ** and to
2af44 6b 65 6e 73 20 63 6f 6e 6e 65 63 74 65 64 20 62  kens connected b
2af45 79 20 74 68 65 20 4e 45 41 52 20 6f 70 65 72 61  y the NEAR opera
2af46 74 6f 72 20 61 72 65 20 68 61 6e 64 6c 65 64 20  tor are handled 
2af47 61 73 20 61 20 73 69 6e 67 6c 65 0a 20 20 2a 2a  as a single.  **
2af48 20 70 68 72 61 73 65 2e 20 53 65 65 20 63 6f 6d   phrase. See com
2af49 6d 65 6e 74 73 20 61 62 6f 76 65 20 74 68 65 20  ments above the 
2af4a 51 75 65 72 79 54 65 72 6d 20 73 74 72 75 63 74  QueryTerm struct
2af4b 75 72 65 20 66 6f 72 20 64 65 74 61 69 6c 73 2e  ure for details.
2af4c 0a 20 20 2a 2f 0a 20 20 61 54 65 72 6d 20 3d 20  .  */.  aTerm = 
2af4d 70 51 75 65 72 79 2d 3e 70 54 65 72 6d 73 3b 0a  pQuery->pTerms;.
2af4e 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70    for(ii=0; ii<p
2af4f 51 75 65 72 79 2d 3e 6e 54 65 72 6d 73 3b 20 69  Query->nTerms; i
2af50 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 54  i++){.    if( aT
2af51 65 72 6d 5b 69 69 5d 2e 6e 4e 65 61 72 20 7c 7c  erm[ii].nNear ||
2af52 20 61 54 65 72 6d 5b 69 69 5d 2e 6e 50 68 72 61   aTerm[ii].nPhra
2af53 73 65 20 29 7b 0a 20 20 20 20 20 20 77 68 69 6c  se ){.      whil
2af54 65 20 28 61 54 65 72 6d 5b 69 69 2b 61 54 65 72  e (aTerm[ii+aTer
2af55 6d 5b 69 69 5d 2e 6e 50 68 72 61 73 65 5d 2e 6e  m[ii].nPhrase].n
2af56 4e 65 61 72 29 20 7b 0a 20 20 20 20 20 20 20 20  Near) {.        
2af57 61 54 65 72 6d 5b 69 69 5d 2e 6e 50 68 72 61 73  aTerm[ii].nPhras
2af58 65 20 2b 3d 20 28 31 20 2b 20 61 54 65 72 6d 5b  e += (1 + aTerm[
2af59 69 69 2b 61 54 65 72 6d 5b 69 69 5d 2e 6e 50 68  ii+aTerm[ii].nPh
2af5a 72 61 73 65 2b 31 5d 2e 6e 50 68 72 61 73 65 29  rase+1].nPhrase)
2af5b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2af5c 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51    }..  return SQ
2af5d 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54  LITE_OK;.}../* T
2af5e 4f 44 4f 28 73 68 65 73 73 29 20 52 65 66 61 63  ODO(shess) Refac
2af5f 74 6f 72 20 74 68 65 20 63 6f 64 65 20 74 6f 20  tor the code to 
2af60 72 65 6d 6f 76 65 20 74 68 69 73 20 66 6f 72 77  remove this forw
2af61 61 72 64 20 64 65 63 6c 2e 20 2a 2f 0a 73 74 61  ard decl. */.sta
2af62 74 69 63 20 69 6e 74 20 66 6c 75 73 68 50 65 6e  tic int flushPen
2af63 64 69 6e 67 54 65 72 6d 73 28 66 75 6c 6c 74 65  dingTerms(fullte
2af64 78 74 5f 76 74 61 62 20 2a 76 29 3b 0a 0a 2f 2a  xt_vtab *v);../*
2af65 20 50 65 72 66 6f 72 6d 20 61 20 66 75 6c 6c 2d   Perform a full-
2af66 74 65 78 74 20 71 75 65 72 79 20 75 73 69 6e 67  text query using
2af67 20 74 68 65 20 73 65 61 72 63 68 20 65 78 70 72   the search expr
2af68 65 73 73 69 6f 6e 20 69 6e 0a 2a 2a 20 7a 49 6e  ession in.** zIn
2af69 70 75 74 5b 30 2e 2e 6e 49 6e 70 75 74 2d 31 5d  put[0..nInput-1]
2af6a 2e 20 20 52 65 74 75 72 6e 20 61 20 6c 69 73 74  .  Return a list
2af6b 20 6f 66 20 6d 61 74 63 68 69 6e 67 20 64 6f 63   of matching doc
2af6c 75 6d 65 6e 74 73 0a 2a 2a 20 69 6e 20 70 52 65  uments.** in pRe
2af6d 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 51 75 65 72  sult..**.** Quer
2af6e 69 65 73 20 6d 75 73 74 20 6d 61 74 63 68 20 63  ies must match c
2af6f 6f 6c 75 6d 6e 20 69 43 6f 6c 75 6d 6e 2e 20 20  olumn iColumn.  
2af70 4f 72 20 69 66 20 69 43 6f 6c 75 6d 6e 3e 3d 6e  Or if iColumn>=n
2af71 43 6f 6c 75 6d 6e 0a 2a 2a 20 74 68 65 79 20 61  Column.** they a
2af72 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 6d 61  re allowed to ma
2af73 74 63 68 20 61 67 61 69 6e 73 74 20 61 6e 79 20  tch against any 
2af74 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69  column..*/.stati
2af75 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 51 75  c int fulltextQu
2af76 65 72 79 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f  ery(.  fulltext_
2af77 76 74 61 62 20 2a 76 2c 20 20 20 20 20 20 2f 2a  vtab *v,      /*
2af78 20 54 68 65 20 66 75 6c 6c 20 74 65 78 74 20 69   The full text i
2af79 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 43  ndex */.  int iC
2af7a 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20  olumn,          
2af7b 20 2f 2a 20 4d 61 74 63 68 20 61 67 61 69 6e 73   /* Match agains
2af7c 74 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 62 79  t this column by
2af7d 20 64 65 66 61 75 6c 74 20 2a 2f 0a 20 20 63 6f   default */.  co
2af7e 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74  nst char *zInput
2af7f 2c 20 20 20 20 2f 2a 20 54 68 65 20 71 75 65 72  ,    /* The quer
2af80 79 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 69 6e  y string */.  in
2af81 74 20 6e 49 6e 70 75 74 2c 20 20 20 20 20 20 20  t nInput,       
2af82 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2af83 66 20 62 79 74 65 73 20 69 6e 20 7a 49 6e 70 75  f bytes in zInpu
2af84 74 5b 5d 20 2a 2f 0a 20 20 44 61 74 61 42 75 66  t[] */.  DataBuf
2af85 66 65 72 20 2a 70 52 65 73 75 6c 74 2c 20 20 20  fer *pResult,   
2af86 2f 2a 20 57 72 69 74 65 20 74 68 65 20 72 65 73  /* Write the res
2af87 75 6c 74 20 64 6f 63 6c 69 73 74 20 68 65 72 65  ult doclist here
2af88 20 2a 2f 0a 20 20 51 75 65 72 79 20 2a 70 51 75   */.  Query *pQu
2af89 65 72 79 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ery          /* 
2af8a 50 75 74 20 70 61 72 73 65 64 20 71 75 65 72 79  Put parsed query
2af8b 20 73 74 72 69 6e 67 20 68 65 72 65 20 2a 2f 0a   string here */.
2af8c 29 7b 0a 20 20 69 6e 74 20 69 2c 20 69 4e 65 78  ){.  int i, iNex
2af8d 74 2c 20 72 63 3b 0a 20 20 44 61 74 61 42 75 66  t, rc;.  DataBuf
2af8e 66 65 72 20 6c 65 66 74 2c 20 72 69 67 68 74 2c  fer left, right,
2af8f 20 6f 72 2c 20 6e 65 77 3b 0a 20 20 69 6e 74 20   or, new;.  int 
2af90 6e 4e 6f 74 20 3d 20 30 3b 0a 20 20 51 75 65 72  nNot = 0;.  Quer
2af91 79 54 65 72 6d 20 2a 61 54 65 72 6d 3b 0a 0a 20  yTerm *aTerm;.. 
2af92 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20   /* TODO(shess) 
2af93 49 6e 73 74 65 61 64 20 6f 66 20 66 6c 75 73 68  Instead of flush
2af94 69 6e 67 20 70 65 6e 64 69 6e 67 54 65 72 6d 73  ing pendingTerms
2af95 2c 20 77 65 20 63 6f 75 6c 64 20 71 75 65 72 79  , we could query
2af96 20 66 6f 72 0a 20 20 2a 2a 20 74 68 65 20 72 65   for.  ** the re
2af97 6c 65 76 61 6e 74 20 74 65 72 6d 20 61 6e 64 20  levant term and 
2af98 6d 65 72 67 65 20 74 68 65 20 64 6f 63 6c 69 73  merge the doclis
2af99 74 20 69 6e 74 6f 20 77 68 61 74 20 77 65 20 72  t into what we r
2af9a 65 63 65 69 76 65 20 66 72 6f 6d 0a 20 20 2a 2a  eceive from.  **
2af9b 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
2af9c 57 61 69 74 20 61 6e 64 20 73 65 65 20 69 66 20  Wait and see if 
2af9d 74 68 69 73 20 69 73 20 61 20 63 6f 6d 6d 6f 6e  this is a common
2af9e 20 69 73 73 75 65 2c 20 66 69 72 73 74 2e 0a 20   issue, first.. 
2af9f 20 2a 2a 0a 20 20 2a 2a 20 41 20 67 6f 6f 64 20   **.  ** A good 
2afa0 72 65 61 73 6f 6e 20 6e 6f 74 20 74 6f 20 66 6c  reason not to fl
2afa1 75 73 68 20 69 73 20 74 6f 20 6e 6f 74 20 67 65  ush is to not ge
2afa2 6e 65 72 61 74 65 20 75 70 64 61 74 65 2d 72 65  nerate update-re
2afa3 6c 61 74 65 64 0a 20 20 2a 2a 20 65 72 72 6f 72  lated.  ** error
2afa4 20 63 6f 64 65 73 20 66 72 6f 6d 20 68 65 72 65   codes from here
2afa5 2e 0a 20 20 2a 2f 0a 0a 20 20 2f 2a 20 46 6c 75  ..  */..  /* Flu
2afa6 73 68 20 61 6e 79 20 62 75 66 66 65 72 65 64 20  sh any buffered 
2afa7 75 70 64 61 74 65 73 20 62 65 66 6f 72 65 20 65  updates before e
2afa8 78 65 63 75 74 69 6e 67 20 74 68 65 20 71 75 65  xecuting the que
2afa9 72 79 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 66 6c  ry. */.  rc = fl
2afaa 75 73 68 50 65 6e 64 69 6e 67 54 65 72 6d 73 28  ushPendingTerms(
2afab 76 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  v);.  if( rc!=SQ
2afac 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
2afad 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28   rc;..  /* TODO(
2afae 73 68 65 73 73 29 20 49 20 74 68 69 6e 6b 20 74  shess) I think t
2afaf 68 61 74 20 74 68 65 20 71 75 65 72 79 43 6c 65  hat the queryCle
2afb0 61 72 28 29 20 63 61 6c 6c 73 20 62 65 6c 6f 77  ar() calls below
2afb1 20 61 72 65 20 6e 6f 74 0a 20 20 2a 2a 20 6e 65   are not.  ** ne
2afb2 63 65 73 73 61 72 79 2c 20 62 65 63 61 75 73 65  cessary, because
2afb3 20 66 75 6c 6c 74 65 78 74 43 6c 6f 73 65 28 29   fulltextClose()
2afb4 20 61 6c 72 65 61 64 79 20 63 6c 65 61 72 73 20   already clears 
2afb5 74 68 65 20 71 75 65 72 79 2e 0a 20 20 2a 2f 0a  the query..  */.
2afb6 20 20 72 63 20 3d 20 70 61 72 73 65 51 75 65 72    rc = parseQuer
2afb7 79 28 76 2c 20 7a 49 6e 70 75 74 2c 20 6e 49 6e  y(v, zInput, nIn
2afb8 70 75 74 2c 20 69 43 6f 6c 75 6d 6e 2c 20 70 51  put, iColumn, pQ
2afb9 75 65 72 79 29 3b 0a 20 20 69 66 28 20 72 63 21  uery);.  if( rc!
2afba 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
2afbb 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 45 6d  urn rc;..  /* Em
2afbc 70 74 79 20 6f 72 20 4e 55 4c 4c 20 71 75 65 72  pty or NULL quer
2afbd 69 65 73 20 72 65 74 75 72 6e 20 6e 6f 20 72 65  ies return no re
2afbe 73 75 6c 74 73 2e 20 2a 2f 0a 20 20 69 66 28 20  sults. */.  if( 
2afbf 70 51 75 65 72 79 2d 3e 6e 54 65 72 6d 73 3d 3d  pQuery->nTerms==
2afc0 30 20 29 7b 0a 20 20 20 20 64 61 74 61 42 75 66  0 ){.    dataBuf
2afc1 66 65 72 49 6e 69 74 28 70 52 65 73 75 6c 74 2c  ferInit(pResult,
2afc2 20 30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20   0);.    return 
2afc3 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a  SQLITE_OK;.  }..
2afc4 20 20 2f 2a 20 4d 65 72 67 65 20 41 4e 44 20 74    /* Merge AND t
2afc5 65 72 6d 73 2e 20 2a 2f 0a 20 20 2f 2a 20 54 4f  erms. */.  /* TO
2afc6 44 4f 28 73 68 65 73 73 29 20 49 20 74 68 69 6e  DO(shess) I thin
2afc7 6b 20 77 65 20 63 61 6e 20 65 61 72 6c 79 2d 65  k we can early-e
2afc8 78 69 74 20 69 66 28 20 69 3e 6e 4e 6f 74 20 26  xit if( i>nNot &
2afc9 26 20 6c 65 66 74 2e 6e 44 61 74 61 3d 3d 30 20  & left.nData==0 
2afca 29 2e 20 2a 2f 0a 20 20 61 54 65 72 6d 20 3d 20  ). */.  aTerm = 
2afcb 70 51 75 65 72 79 2d 3e 70 54 65 72 6d 73 3b 0a  pQuery->pTerms;.
2afcc 20 20 66 6f 72 28 69 20 3d 20 30 3b 20 69 3c 70    for(i = 0; i<p
2afcd 51 75 65 72 79 2d 3e 6e 54 65 72 6d 73 3b 20 69  Query->nTerms; i
2afce 3d 69 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28  =iNext){.    if(
2afcf 20 61 54 65 72 6d 5b 69 5d 2e 69 73 4e 6f 74 20   aTerm[i].isNot 
2afd0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 48 61 6e 64  ){.      /* Hand
2afd1 6c 65 20 61 6c 6c 20 4e 4f 54 20 74 65 72 6d 73  le all NOT terms
2afd2 20 69 6e 20 61 20 73 65 70 61 72 61 74 65 20 70   in a separate p
2afd3 61 73 73 20 2a 2f 0a 20 20 20 20 20 20 6e 4e 6f  ass */.      nNo
2afd4 74 2b 2b 3b 0a 20 20 20 20 20 20 69 4e 65 78 74  t++;.      iNext
2afd5 20 3d 20 69 20 2b 20 61 54 65 72 6d 5b 69 5d 2e   = i + aTerm[i].
2afd6 6e 50 68 72 61 73 65 2b 31 3b 0a 20 20 20 20 20  nPhrase+1;.     
2afd7 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d   continue;.    }
2afd8 0a 20 20 20 20 69 4e 65 78 74 20 3d 20 69 20 2b  .    iNext = i +
2afd9 20 61 54 65 72 6d 5b 69 5d 2e 6e 50 68 72 61 73   aTerm[i].nPhras
2afda 65 20 2b 20 31 3b 0a 20 20 20 20 72 63 20 3d 20  e + 1;.    rc = 
2afdb 64 6f 63 4c 69 73 74 4f 66 54 65 72 6d 28 76 2c  docListOfTerm(v,
2afdc 20 61 54 65 72 6d 5b 69 5d 2e 69 43 6f 6c 75 6d   aTerm[i].iColum
2afdd 6e 2c 20 26 61 54 65 72 6d 5b 69 5d 2c 20 26 72  n, &aTerm[i], &r
2afde 69 67 68 74 29 3b 0a 20 20 20 20 69 66 28 20 72  ight);.    if( r
2afdf 63 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69  c ){.      if( i
2afe0 21 3d 6e 4e 6f 74 20 29 20 64 61 74 61 42 75 66  !=nNot ) dataBuf
2afe1 66 65 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74  ferDestroy(&left
2afe2 29 3b 0a 20 20 20 20 20 20 71 75 65 72 79 43 6c  );.      queryCl
2afe3 65 61 72 28 70 51 75 65 72 79 29 3b 0a 20 20 20  ear(pQuery);.   
2afe4 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
2afe5 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 69    }.    while( i
2afe6 4e 65 78 74 3c 70 51 75 65 72 79 2d 3e 6e 54 65  Next<pQuery->nTe
2afe7 72 6d 73 20 26 26 20 61 54 65 72 6d 5b 69 4e 65  rms && aTerm[iNe
2afe8 78 74 5d 2e 69 73 4f 72 20 29 7b 0a 20 20 20 20  xt].isOr ){.    
2afe9 20 20 72 63 20 3d 20 64 6f 63 4c 69 73 74 4f 66    rc = docListOf
2afea 54 65 72 6d 28 76 2c 20 61 54 65 72 6d 5b 69 4e  Term(v, aTerm[iN
2afeb 65 78 74 5d 2e 69 43 6f 6c 75 6d 6e 2c 20 26 61  ext].iColumn, &a
2afec 54 65 72 6d 5b 69 4e 65 78 74 5d 2c 20 26 6f 72  Term[iNext], &or
2afed 29 3b 0a 20 20 20 20 20 20 69 4e 65 78 74 20 2b  );.      iNext +
2afee 3d 20 61 54 65 72 6d 5b 69 4e 65 78 74 5d 2e 6e  = aTerm[iNext].n
2afef 50 68 72 61 73 65 20 2b 20 31 3b 0a 20 20 20 20  Phrase + 1;.    
2aff0 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
2aff1 20 20 20 20 69 66 28 20 69 21 3d 6e 4e 6f 74 20      if( i!=nNot 
2aff2 29 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74  ) dataBufferDest
2aff3 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20 20 20  roy(&left);.    
2aff4 20 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65      dataBufferDe
2aff5 73 74 72 6f 79 28 26 72 69 67 68 74 29 3b 0a 20  stroy(&right);. 
2aff6 20 20 20 20 20 20 20 71 75 65 72 79 43 6c 65 61         queryClea
2aff7 72 28 70 51 75 65 72 79 29 3b 0a 20 20 20 20 20  r(pQuery);.     
2aff8 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
2aff9 20 20 20 20 7d 0a 20 20 20 20 20 20 64 61 74 61      }.      data
2affa 42 75 66 66 65 72 49 6e 69 74 28 26 6e 65 77 2c  BufferInit(&new,
2affb 20 30 29 3b 0a 20 20 20 20 20 20 64 6f 63 4c 69   0);.      docLi
2affc 73 74 4f 72 4d 65 72 67 65 28 72 69 67 68 74 2e  stOrMerge(right.
2affd 70 44 61 74 61 2c 20 72 69 67 68 74 2e 6e 44 61  pData, right.nDa
2affe 74 61 2c 20 6f 72 2e 70 44 61 74 61 2c 20 6f 72  ta, or.pData, or
2afff 2e 6e 44 61 74 61 2c 20 26 6e 65 77 29 3b 0a 20  .nData, &new);. 
2b000 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72 44       dataBufferD
2b001 65 73 74 72 6f 79 28 26 72 69 67 68 74 29 3b 0a  estroy(&right);.
2b002 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72        dataBuffer
2b003 44 65 73 74 72 6f 79 28 26 6f 72 29 3b 0a 20 20  Destroy(&or);.  
2b004 20 20 20 20 72 69 67 68 74 20 3d 20 6e 65 77 3b      right = new;
2b005 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69  .    }.    if( i
2b006 3d 3d 6e 4e 6f 74 20 29 7b 20 20 20 20 20 20 20  ==nNot ){       
2b007 20 20 20 20 2f 2a 20 66 69 72 73 74 20 74 65 72      /* first ter
2b008 6d 20 70 72 6f 63 65 73 73 65 64 2e 20 2a 2f 0a  m processed. */.
2b009 20 20 20 20 20 20 6c 65 66 74 20 3d 20 72 69 67        left = rig
2b00a 68 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ht;.    }else{. 
2b00b 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72 49       dataBufferI
2b00c 6e 69 74 28 26 6e 65 77 2c 20 30 29 3b 0a 20 20  nit(&new, 0);.  
2b00d 20 20 20 20 64 6f 63 4c 69 73 74 41 6e 64 4d 65      docListAndMe
2b00e 72 67 65 28 6c 65 66 74 2e 70 44 61 74 61 2c 20  rge(left.pData, 
2b00f 6c 65 66 74 2e 6e 44 61 74 61 2c 20 72 69 67 68  left.nData, righ
2b010 74 2e 70 44 61 74 61 2c 20 72 69 67 68 74 2e 6e  t.pData, right.n
2b011 44 61 74 61 2c 20 26 6e 65 77 29 3b 0a 20 20 20  Data, &new);.   
2b012 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73     dataBufferDes
2b013 74 72 6f 79 28 26 72 69 67 68 74 29 3b 0a 20 20  troy(&right);.  
2b014 20 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65      dataBufferDe
2b015 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20  stroy(&left);.  
2b016 20 20 20 20 6c 65 66 74 20 3d 20 6e 65 77 3b 0a      left = new;.
2b017 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28      }.  }..  if(
2b018 20 6e 4e 6f 74 3d 3d 70 51 75 65 72 79 2d 3e 6e   nNot==pQuery->n
2b019 54 65 72 6d 73 20 29 7b 0a 20 20 20 20 2f 2a 20  Terms ){.    /* 
2b01a 57 65 20 64 6f 20 6e 6f 74 20 79 65 74 20 6b 6e  We do not yet kn
2b01b 6f 77 20 68 6f 77 20 74 6f 20 68 61 6e 64 6c 65  ow how to handle
2b01c 20 61 20 71 75 65 72 79 20 6f 66 20 6f 6e 6c 79   a query of only
2b01d 20 4e 4f 54 20 74 65 72 6d 73 20 2a 2f 0a 20 20   NOT terms */.  
2b01e 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2b01f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  ERROR;.  }..  /*
2b020 20 44 6f 20 74 68 65 20 45 58 43 45 50 54 20 74   Do the EXCEPT t
2b021 65 72 6d 73 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  erms */.  for(i=
2b022 30 3b 20 69 3c 70 51 75 65 72 79 2d 3e 6e 54 65  0; i<pQuery->nTe
2b023 72 6d 73 3b 20 20 69 20 2b 3d 20 61 54 65 72 6d  rms;  i += aTerm
2b024 5b 69 5d 2e 6e 50 68 72 61 73 65 20 2b 20 31 29  [i].nPhrase + 1)
2b025 7b 0a 20 20 20 20 69 66 28 20 21 61 54 65 72 6d  {.    if( !aTerm
2b026 5b 69 5d 2e 69 73 4e 6f 74 20 29 20 63 6f 6e 74  [i].isNot ) cont
2b027 69 6e 75 65 3b 0a 20 20 20 20 72 63 20 3d 20 64  inue;.    rc = d
2b028 6f 63 4c 69 73 74 4f 66 54 65 72 6d 28 76 2c 20  ocListOfTerm(v, 
2b029 61 54 65 72 6d 5b 69 5d 2e 69 43 6f 6c 75 6d 6e  aTerm[i].iColumn
2b02a 2c 20 26 61 54 65 72 6d 5b 69 5d 2c 20 26 72 69  , &aTerm[i], &ri
2b02b 67 68 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ght);.    if( rc
2b02c 20 29 7b 0a 20 20 20 20 20 20 71 75 65 72 79 43   ){.      queryC
2b02d 6c 65 61 72 28 70 51 75 65 72 79 29 3b 0a 20 20  lear(pQuery);.  
2b02e 20 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65      dataBufferDe
2b02f 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20  stroy(&left);.  
2b030 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
2b031 20 20 20 7d 0a 20 20 20 20 64 61 74 61 42 75 66     }.    dataBuf
2b032 66 65 72 49 6e 69 74 28 26 6e 65 77 2c 20 30 29  ferInit(&new, 0)
2b033 3b 0a 20 20 20 20 64 6f 63 4c 69 73 74 45 78 63  ;.    docListExc
2b034 65 70 74 4d 65 72 67 65 28 6c 65 66 74 2e 70 44  eptMerge(left.pD
2b035 61 74 61 2c 20 6c 65 66 74 2e 6e 44 61 74 61 2c  ata, left.nData,
2b036 20 72 69 67 68 74 2e 70 44 61 74 61 2c 20 72 69   right.pData, ri
2b037 67 68 74 2e 6e 44 61 74 61 2c 20 26 6e 65 77 29  ght.nData, &new)
2b038 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  ;.    dataBuffer
2b039 44 65 73 74 72 6f 79 28 26 72 69 67 68 74 29 3b  Destroy(&right);
2b03a 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 44  .    dataBufferD
2b03b 65 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20  estroy(&left);. 
2b03c 20 20 20 6c 65 66 74 20 3d 20 6e 65 77 3b 0a 20     left = new;. 
2b03d 20 7d 0a 0a 20 20 2a 70 52 65 73 75 6c 74 20 3d   }..  *pResult =
2b03e 20 6c 65 66 74 3b 0a 20 20 72 65 74 75 72 6e 20   left;.  return 
2b03f 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rc;.}../*.** Thi
2b040 73 20 69 73 20 74 68 65 20 78 46 69 6c 74 65 72  s is the xFilter
2b041 20 69 6e 74 65 72 66 61 63 65 20 66 6f 72 20 74   interface for t
2b042 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
2b043 2e 20 20 53 65 65 0a 2a 2a 20 74 68 65 20 76 69  .  See.** the vi
2b044 72 74 75 61 6c 20 74 61 62 6c 65 20 78 46 69 6c  rtual table xFil
2b045 74 65 72 20 6d 65 74 68 6f 64 20 64 6f 63 75 6d  ter method docum
2b046 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64  entation for add
2b047 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72  itional.** infor
2b048 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66  mation..**.** If
2b049 20 69 64 78 4e 75 6d 3d 3d 51 55 45 52 59 5f 47   idxNum==QUERY_G
2b04a 45 4e 45 52 49 43 20 74 68 65 6e 20 64 6f 20 61  ENERIC then do a
2b04b 20 66 75 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e   full table scan
2b04c 20 61 67 61 69 6e 73 74 0a 2a 2a 20 74 68 65 20   against.** the 
2b04d 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 2e  %_content table.
2b04e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 64 78 4e 75 6d  .**.** If idxNum
2b04f 3d 3d 51 55 45 52 59 5f 44 4f 43 49 44 20 74 68  ==QUERY_DOCID th
2b050 65 6e 20 64 6f 20 61 20 64 6f 63 69 64 20 6c 6f  en do a docid lo
2b051 6f 6b 75 70 20 66 6f 72 20 61 20 73 69 6e 67 6c  okup for a singl
2b052 65 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 74 68  e entry.** in th
2b053 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c  e %_content tabl
2b054 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 64 78 4e  e..**.** If idxN
2b055 75 6d 3e 3d 51 55 45 52 59 5f 46 55 4c 4c 54 45  um>=QUERY_FULLTE
2b056 58 54 20 74 68 65 6e 20 75 73 65 20 74 68 65 20  XT then use the 
2b057 66 75 6c 6c 20 74 65 78 74 20 69 6e 64 65 78 2e  full text index.
2b058 20 20 54 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20    The.** column 
2b059 6f 6e 20 74 68 65 20 6c 65 66 74 2d 68 61 6e 64  on the left-hand
2b05a 20 73 69 64 65 20 6f 66 20 74 68 65 20 4d 41 54   side of the MAT
2b05b 43 48 20 6f 70 65 72 61 74 6f 72 20 69 73 20 63  CH operator is c
2b05c 6f 6c 75 6d 6e 0a 2a 2a 20 6e 75 6d 62 65 72 20  olumn.** number 
2b05d 69 64 78 4e 75 6d 2d 51 55 45 52 59 5f 46 55 4c  idxNum-QUERY_FUL
2b05e 4c 54 45 58 54 2c 20 30 20 69 6e 64 65 78 65 64  LTEXT, 0 indexed
2b05f 2e 20 20 61 72 67 76 5b 30 5d 20 69 73 20 74 68  .  argv[0] is th
2b060 65 20 72 69 67 68 74 2d 68 61 6e 64 0a 2a 2a 20  e right-hand.** 
2b061 73 69 64 65 20 6f 66 20 74 68 65 20 4d 41 54 43  side of the MATC
2b062 48 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 2f  H operator..*/./
2b063 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 55 70  * TODO(shess) Up
2b064 67 72 61 64 65 20 74 68 65 20 63 75 72 73 6f 72  grade the cursor
2b065 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
2b066 61 6e 64 20 64 65 73 74 72 75 63 74 69 6f 6e 20  and destruction 
2b067 74 6f 0a 2a 2a 20 61 63 63 6f 75 6e 74 20 66 6f  to.** account fo
2b068 72 20 66 75 6c 6c 74 65 78 74 46 69 6c 74 65 72  r fulltextFilter
2b069 28 29 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20  () being called 
2b06a 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 6f  multiple times o
2b06b 6e 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 63 75  n the.** same cu
2b06c 72 73 6f 72 2e 20 20 54 68 65 20 63 75 72 72 65  rsor.  The curre
2b06d 6e 74 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 76  nt solution is v
2b06e 65 72 79 20 66 72 61 67 69 6c 65 2e 20 20 41 70  ery fragile.  Ap
2b06f 70 6c 79 20 66 69 78 20 74 6f 0a 2a 2a 20 66 74  ply fix to.** ft
2b070 73 33 20 61 73 20 61 70 70 72 6f 70 72 69 61 74  s3 as appropriat
2b071 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
2b072 20 66 75 6c 6c 74 65 78 74 46 69 6c 74 65 72 28   fulltextFilter(
2b073 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  .  sqlite3_vtab_
2b074 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c  cursor *pCursor,
2b075 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73       /* The curs
2b076 6f 72 20 75 73 65 64 20 66 6f 72 20 74 68 69 73  or used for this
2b077 20 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e 74 20   query */.  int 
2b078 69 64 78 4e 75 6d 2c 20 63 6f 6e 73 74 20 63 68  idxNum, const ch
2b079 61 72 20 2a 69 64 78 53 74 72 2c 20 20 20 2f 2a  ar *idxStr,   /*
2b07a 20 57 68 69 63 68 20 69 6e 64 65 78 69 6e 67 20   Which indexing 
2b07b 73 63 68 65 6d 65 20 74 6f 20 75 73 65 20 2a 2f  scheme to use */
2b07c 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  .  int argc, sql
2b07d 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
2b07e 76 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74  v    /* Argument
2b07f 73 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 69  s for the indexi
2b080 6e 67 20 73 63 68 65 6d 65 20 2a 2f 0a 29 7b 0a  ng scheme */.){.
2b081 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f    fulltext_curso
2b082 72 20 2a 63 20 3d 20 28 66 75 6c 6c 74 65 78 74  r *c = (fulltext
2b083 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73  _cursor *) pCurs
2b084 6f 72 3b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76  or;.  fulltext_v
2b085 74 61 62 20 2a 76 20 3d 20 63 75 72 73 6f 72 5f  tab *v = cursor_
2b086 76 74 61 62 28 63 29 3b 0a 20 20 69 6e 74 20 72  vtab(c);.  int r
2b087 63 3b 0a 20 20 53 74 72 69 6e 67 42 75 66 66 65  c;.  StringBuffe
2b088 72 20 73 62 3b 0a 0a 20 20 46 54 53 54 52 41 43  r sb;..  FTSTRAC
2b089 45 28 28 22 46 54 53 33 20 46 69 6c 74 65 72 20  E(("FTS3 Filter 
2b08a 25 70 5c 6e 22 2c 70 43 75 72 73 6f 72 29 29 3b  %p\n",pCursor));
2b08b 0a 0a 20 20 69 6e 69 74 53 74 72 69 6e 67 42 75  ..  initStringBu
2b08c 66 66 65 72 28 26 73 62 29 3b 0a 20 20 61 70 70  ffer(&sb);.  app
2b08d 65 6e 64 28 26 73 62 2c 20 22 53 45 4c 45 43 54  end(&sb, "SELECT
2b08e 20 64 6f 63 69 64 2c 20 22 29 3b 0a 20 20 61 70   docid, ");.  ap
2b08f 70 65 6e 64 4c 69 73 74 28 26 73 62 2c 20 76 2d  pendList(&sb, v-
2b090 3e 6e 43 6f 6c 75 6d 6e 2c 20 76 2d 3e 61 7a 43  >nColumn, v->azC
2b091 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20  ontentColumn);. 
2b092 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 20 46   append(&sb, " F
2b093 52 4f 4d 20 25 5f 63 6f 6e 74 65 6e 74 22 29 3b  ROM %_content");
2b094 0a 20 20 69 66 28 20 69 64 78 4e 75 6d 21 3d 51  .  if( idxNum!=Q
2b095 55 45 52 59 5f 47 45 4e 45 52 49 43 20 29 20 61  UERY_GENERIC ) a
2b096 70 70 65 6e 64 28 26 73 62 2c 20 22 20 57 48 45  ppend(&sb, " WHE
2b097 52 45 20 64 6f 63 69 64 20 3d 20 3f 22 29 3b 0a  RE docid = ?");.
2b098 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69    sqlite3_finali
2b099 7a 65 28 63 2d 3e 70 53 74 6d 74 29 3b 0a 20 20  ze(c->pStmt);.  
2b09a 72 63 20 3d 20 73 71 6c 5f 70 72 65 70 61 72 65  rc = sql_prepare
2b09b 28 76 2d 3e 64 62 2c 20 76 2d 3e 7a 44 62 2c 20  (v->db, v->zDb, 
2b09c 76 2d 3e 7a 4e 61 6d 65 2c 20 26 63 2d 3e 70 53  v->zName, &c->pS
2b09d 74 6d 74 2c 20 73 74 72 69 6e 67 42 75 66 66 65  tmt, stringBuffe
2b09e 72 44 61 74 61 28 26 73 62 29 29 3b 0a 20 20 73  rData(&sb));.  s
2b09f 74 72 69 6e 67 42 75 66 66 65 72 44 65 73 74 72  tringBufferDestr
2b0a0 6f 79 28 26 73 62 29 3b 0a 20 20 69 66 28 20 72  oy(&sb);.  if( r
2b0a1 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
2b0a2 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 63 2d 3e  eturn rc;..  c->
2b0a3 69 43 75 72 73 6f 72 54 79 70 65 20 3d 20 69 64  iCursorType = id
2b0a4 78 4e 75 6d 3b 0a 20 20 73 77 69 74 63 68 28 20  xNum;.  switch( 
2b0a5 69 64 78 4e 75 6d 20 29 7b 0a 20 20 20 20 63 61  idxNum ){.    ca
2b0a6 73 65 20 51 55 45 52 59 5f 47 45 4e 45 52 49 43  se QUERY_GENERIC
2b0a7 3a 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a  :.      break;..
2b0a8 20 20 20 20 63 61 73 65 20 51 55 45 52 59 5f 44      case QUERY_D
2b0a9 4f 43 49 44 3a 0a 20 20 20 20 20 20 72 63 20 3d  OCID:.      rc =
2b0aa 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
2b0ab 74 36 34 28 63 2d 3e 70 53 74 6d 74 2c 20 31 2c  t64(c->pStmt, 1,
2b0ac 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
2b0ad 6e 74 36 34 28 61 72 67 76 5b 30 5d 29 29 3b 0a  nt64(argv[0]));.
2b0ae 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
2b0af 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
2b0b0 20 72 63 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   rc;.      break
2b0b1 3b 0a 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  ;..    default: 
2b0b2 20 20 2f 2a 20 66 75 6c 6c 2d 74 65 78 74 20 73    /* full-text s
2b0b3 65 61 72 63 68 20 2a 2f 0a 20 20 20 20 7b 0a 20  earch */.    {. 
2b0b4 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
2b0b5 2a 7a 51 75 65 72 79 20 3d 20 28 63 6f 6e 73 74  *zQuery = (const
2b0b6 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f   char *)sqlite3_
2b0b7 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
2b0b8 30 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  0]);.      asser
2b0b9 74 28 20 69 64 78 4e 75 6d 3c 3d 51 55 45 52 59  t( idxNum<=QUERY
2b0ba 5f 46 55 4c 4c 54 45 58 54 2b 76 2d 3e 6e 43 6f  _FULLTEXT+v->nCo
2b0bb 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 61 73 73  lumn);.      ass
2b0bc 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a  ert( argc==1 );.
2b0bd 20 20 20 20 20 20 71 75 65 72 79 43 6c 65 61 72        queryClear
2b0be 28 26 63 2d 3e 71 29 3b 0a 20 20 20 20 20 20 69  (&c->q);.      i
2b0bf 66 28 20 63 2d 3e 72 65 73 75 6c 74 2e 6e 44 61  f( c->result.nDa
2b0c0 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ta!=0 ){.       
2b0c1 20 2f 2a 20 54 68 69 73 20 63 61 73 65 20 68 61   /* This case ha
2b0c2 70 70 65 6e 73 20 69 66 20 74 68 65 20 73 61 6d  ppens if the sam
2b0c3 65 20 63 75 72 73 6f 72 20 69 73 20 75 73 65 64  e cursor is used
2b0c4 20 72 65 70 65 61 74 65 64 6c 79 2e 20 2a 2f 0a   repeatedly. */.
2b0c5 20 20 20 20 20 20 20 20 64 6c 72 44 65 73 74 72          dlrDestr
2b0c6 6f 79 28 26 63 2d 3e 72 65 61 64 65 72 29 3b 0a  oy(&c->reader);.
2b0c7 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66          dataBuff
2b0c8 65 72 52 65 73 65 74 28 26 63 2d 3e 72 65 73 75  erReset(&c->resu
2b0c9 6c 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  lt);.      }else
2b0ca 7b 0a 20 20 20 20 20 20 20 20 64 61 74 61 42 75  {.        dataBu
2b0cb 66 66 65 72 49 6e 69 74 28 26 63 2d 3e 72 65 73  fferInit(&c->res
2b0cc 75 6c 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d  ult, 0);.      }
2b0cd 0a 20 20 20 20 20 20 72 63 20 3d 20 66 75 6c 6c  .      rc = full
2b0ce 74 65 78 74 51 75 65 72 79 28 76 2c 20 69 64 78  textQuery(v, idx
2b0cf 4e 75 6d 2d 51 55 45 52 59 5f 46 55 4c 4c 54 45  Num-QUERY_FULLTE
2b0d0 58 54 2c 20 7a 51 75 65 72 79 2c 20 2d 31 2c 20  XT, zQuery, -1, 
2b0d1 26 63 2d 3e 72 65 73 75 6c 74 2c 20 26 63 2d 3e  &c->result, &c->
2b0d2 71 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  q);.      if( rc
2b0d3 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
2b0d4 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 69  turn rc;.      i
2b0d5 66 28 20 63 2d 3e 72 65 73 75 6c 74 2e 6e 44 61  f( c->result.nDa
2b0d6 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ta!=0 ){.       
2b0d7 20 64 6c 72 49 6e 69 74 28 26 63 2d 3e 72 65 61   dlrInit(&c->rea
2b0d8 64 65 72 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20  der, DL_DOCIDS, 
2b0d9 63 2d 3e 72 65 73 75 6c 74 2e 70 44 61 74 61 2c  c->result.pData,
2b0da 20 63 2d 3e 72 65 73 75 6c 74 2e 6e 44 61 74 61   c->result.nData
2b0db 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
2b0dc 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
2b0dd 7d 0a 0a 20 20 72 65 74 75 72 6e 20 66 75 6c 6c  }..  return full
2b0de 74 65 78 74 4e 65 78 74 28 70 43 75 72 73 6f 72  textNext(pCursor
2b0df 29 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 69 73  );.}../* This is
2b0e0 20 74 68 65 20 78 45 6f 66 20 6d 65 74 68 6f 64   the xEof method
2b0e1 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20   of the virtual 
2b0e2 74 61 62 6c 65 2e 20 20 54 68 65 20 53 51 4c 69  table.  The SQLi
2b0e3 74 65 20 63 6f 72 65 0a 2a 2a 20 63 61 6c 6c 73  te core.** calls
2b0e4 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f   this routine to
2b0e5 20 66 69 6e 64 20 6f 75 74 20 69 66 20 69 74 20   find out if it 
2b0e6 68 61 73 20 72 65 61 63 68 65 64 20 74 68 65 20  has reached the 
2b0e7 65 6e 64 20 6f 66 0a 2a 2a 20 61 20 71 75 65 72  end of.** a quer
2b0e8 79 27 73 20 72 65 73 75 6c 74 73 20 73 65 74 2e  y's results set.
2b0e9 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
2b0ea 75 6c 6c 74 65 78 74 45 6f 66 28 73 71 6c 69 74  ulltextEof(sqlit
2b0eb 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a  e3_vtab_cursor *
2b0ec 70 43 75 72 73 6f 72 29 7b 0a 20 20 66 75 6c 6c  pCursor){.  full
2b0ed 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 63 20 3d  text_cursor *c =
2b0ee 20 28 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f   (fulltext_curso
2b0ef 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20  r *) pCursor;.  
2b0f0 72 65 74 75 72 6e 20 63 2d 3e 65 6f 66 3b 0a 7d  return c->eof;.}
2b0f1 0a 0a 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65  ../* This is the
2b0f2 20 78 43 6f 6c 75 6d 6e 20 6d 65 74 68 6f 64 20   xColumn method 
2b0f3 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  of the virtual t
2b0f4 61 62 6c 65 2e 20 20 54 68 65 20 53 51 4c 69 74  able.  The SQLit
2b0f5 65 0a 2a 2a 20 63 6f 72 65 20 63 61 6c 6c 73 20  e.** core calls 
2b0f6 74 68 69 73 20 6d 65 74 68 6f 64 20 64 75 72 69  this method duri
2b0f7 6e 67 20 61 20 71 75 65 72 79 20 77 68 65 6e 20  ng a query when 
2b0f8 69 74 20 6e 65 65 64 73 20 74 68 65 20 76 61 6c  it needs the val
2b0f9 75 65 0a 2a 2a 20 6f 66 20 61 20 63 6f 6c 75 6d  ue.** of a colum
2b0fa 6e 20 66 72 6f 6d 20 74 68 65 20 76 69 72 74 75  n from the virtu
2b0fb 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20  al table.  This 
2b0fc 6d 65 74 68 6f 64 20 6e 65 65 64 73 20 74 6f 20  method needs to 
2b0fd 75 73 65 0a 2a 2a 20 6f 6e 65 20 6f 66 20 74 68  use.** one of th
2b0fe 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
2b0ff 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 74 6f  _*() routines to
2b100 20 73 74 6f 72 65 20 74 68 65 20 72 65 71 75 65   store the reque
2b101 73 74 65 64 0a 2a 2a 20 76 61 6c 75 65 20 62 61  sted.** value ba
2b102 63 6b 20 69 6e 20 74 68 65 20 70 43 6f 6e 74 65  ck in the pConte
2b103 78 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  xt..*/.static in
2b104 74 20 66 75 6c 6c 74 65 78 74 43 6f 6c 75 6d 6e  t fulltextColumn
2b105 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75  (sqlite3_vtab_cu
2b106 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 0a 20  rsor *pCursor,. 
2b107 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b108 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2b109 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65  _context *pConte
2b10a 78 74 2c 20 69 6e 74 20 69 64 78 43 6f 6c 29 7b  xt, int idxCol){
2b10b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  .  fulltext_curs
2b10c 6f 72 20 2a 63 20 3d 20 28 66 75 6c 6c 74 65 78  or *c = (fulltex
2b10d 74 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72  t_cursor *) pCur
2b10e 73 6f 72 3b 0a 20 20 66 75 6c 6c 74 65 78 74 5f  sor;.  fulltext_
2b10f 76 74 61 62 20 2a 76 20 3d 20 63 75 72 73 6f 72  vtab *v = cursor
2b110 5f 76 74 61 62 28 63 29 3b 0a 0a 20 20 69 66 28  _vtab(c);..  if(
2b111 20 69 64 78 43 6f 6c 3c 76 2d 3e 6e 43 6f 6c 75   idxCol<v->nColu
2b112 6d 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  mn ){.    sqlite
2b113 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20 3d 20  3_value *pVal = 
2b114 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76  sqlite3_column_v
2b115 61 6c 75 65 28 63 2d 3e 70 53 74 6d 74 2c 20 69  alue(c->pStmt, i
2b116 64 78 43 6f 6c 2b 31 29 3b 0a 20 20 20 20 73 71  dxCol+1);.    sq
2b117 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c  lite3_result_val
2b118 75 65 28 70 43 6f 6e 74 65 78 74 2c 20 70 56 61  ue(pContext, pVa
2b119 6c 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  l);.  }else if( 
2b11a 69 64 78 43 6f 6c 3d 3d 76 2d 3e 6e 43 6f 6c 75  idxCol==v->nColu
2b11b 6d 6e 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  mn ){.    /* The
2b11c 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e 20 77 68   extra column wh
2b11d 6f 73 65 20 6e 61 6d 65 20 69 73 20 74 68 65 20  ose name is the 
2b11e 73 61 6d 65 20 61 73 20 74 68 65 20 74 61 62 6c  same as the tabl
2b11f 65 2e 0a 20 20 20 20 2a 2a 20 52 65 74 75 72 6e  e..    ** Return
2b120 20 61 20 62 6c 6f 62 20 77 68 69 63 68 20 69 73   a blob which is
2b121 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
2b122 65 20 63 75 72 73 6f 72 0a 20 20 20 20 2a 2f 0a  e cursor.    */.
2b123 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
2b124 6c 74 5f 62 6c 6f 62 28 70 43 6f 6e 74 65 78 74  lt_blob(pContext
2b125 2c 20 26 63 2c 20 73 69 7a 65 6f 66 28 63 29 2c  , &c, sizeof(c),
2b126 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
2b127 54 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  T);.  }else if( 
2b128 69 64 78 43 6f 6c 3d 3d 76 2d 3e 6e 43 6f 6c 75  idxCol==v->nColu
2b129 6d 6e 2b 31 20 29 7b 0a 20 20 20 20 2f 2a 20 54  mn+1 ){.    /* T
2b12a 68 65 20 64 6f 63 69 64 20 63 6f 6c 75 6d 6e 2c  he docid column,
2b12b 20 77 68 69 63 68 20 69 73 20 61 6e 20 61 6c 69   which is an ali
2b12c 61 73 20 66 6f 72 20 72 6f 77 69 64 2e 20 2a 2f  as for rowid. */
2b12d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  .    sqlite3_val
2b12e 75 65 20 2a 70 56 61 6c 20 3d 20 73 71 6c 69 74  ue *pVal = sqlit
2b12f 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28  e3_column_value(
2b130 63 2d 3e 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20  c->pStmt, 0);.  
2b131 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
2b132 5f 76 61 6c 75 65 28 70 43 6f 6e 74 65 78 74 2c  _value(pContext,
2b133 20 70 56 61 6c 29 3b 0a 20 20 7d 0a 20 20 72 65   pVal);.  }.  re
2b134 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
2b135 7d 0a 0a 2f 2a 20 54 68 69 73 20 69 73 20 74 68  }../* This is th
2b136 65 20 78 52 6f 77 69 64 20 6d 65 74 68 6f 64 2e  e xRowid method.
2b137 20 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72    The SQLite cor
2b138 65 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  e calls this rou
2b139 74 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 74 72 69  tine to.** retri
2b13a 65 76 65 20 74 68 65 20 72 6f 77 69 64 20 66 6f  eve the rowid fo
2b13b 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f  r the current ro
2b13c 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20  w of the result 
2b13d 73 65 74 2e 20 20 66 74 73 33 0a 2a 2a 20 65 78  set.  fts3.** ex
2b13e 70 6f 73 65 73 20 25 5f 63 6f 6e 74 65 6e 74 2e  poses %_content.
2b13f 64 6f 63 69 64 20 61 73 20 74 68 65 20 72 6f 77  docid as the row
2b140 69 64 20 66 6f 72 20 74 68 65 20 76 69 72 74 75  id for the virtu
2b141 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 0a 2a  al table.  The.*
2b142 2a 20 72 6f 77 69 64 20 73 68 6f 75 6c 64 20 62  * rowid should b
2b143 65 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 52  e written to *pR
2b144 6f 77 69 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  owid..*/.static 
2b145 69 6e 74 20 66 75 6c 6c 74 65 78 74 52 6f 77 69  int fulltextRowi
2b146 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  d(sqlite3_vtab_c
2b147 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20  ursor *pCursor, 
2b148 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 52  sqlite_int64 *pR
2b149 6f 77 69 64 29 7b 0a 20 20 66 75 6c 6c 74 65 78  owid){.  fulltex
2b14a 74 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 66  t_cursor *c = (f
2b14b 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a  ulltext_cursor *
2b14c 29 20 70 43 75 72 73 6f 72 3b 0a 0a 20 20 2a 70  ) pCursor;..  *p
2b14d 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 5f  Rowid = sqlite3_
2b14e 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 63 2d 3e  column_int64(c->
2b14f 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 72 65 74  pStmt, 0);.  ret
2b150 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
2b151 0a 0a 2f 2a 20 41 64 64 20 61 6c 6c 20 74 65 72  ../* Add all ter
2b152 6d 73 20 69 6e 20 5b 7a 54 65 78 74 5d 20 74 6f  ms in [zText] to
2b153 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20 74 61   pendingTerms ta
2b154 62 6c 65 2e 20 20 49 66 20 5b 69 43 6f 6c 75 6d  ble.  If [iColum
2b155 6e 5d 20 3e 20 30 2c 0a 2a 2a 20 77 65 20 61 6c  n] > 0,.** we al
2b156 73 6f 20 73 74 6f 72 65 20 70 6f 73 69 74 69 6f  so store positio
2b157 6e 73 20 61 6e 64 20 6f 66 66 73 65 74 73 20 69  ns and offsets i
2b158 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  n the hash table
2b159 20 75 73 69 6e 67 20 74 68 61 74 0a 2a 2a 20 63   using that.** c
2b15a 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2f  olumn number..*/
2b15b 0a 73 74 61 74 69 63 20 69 6e 74 20 62 75 69 6c  .static int buil
2b15c 64 54 65 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f  dTerms(fulltext_
2b15d 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f  vtab *v, sqlite_
2b15e 69 6e 74 36 34 20 69 44 6f 63 69 64 2c 0a 20 20  int64 iDocid,.  
2b15f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b160 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2b161 7a 54 65 78 74 2c 20 69 6e 74 20 69 43 6f 6c 75  zText, int iColu
2b162 6d 6e 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74  mn){.  sqlite3_t
2b163 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
2b164 69 7a 65 72 20 3d 20 76 2d 3e 70 54 6f 6b 65 6e  izer = v->pToken
2b165 69 7a 65 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f  izer;.  sqlite3_
2b166 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
2b167 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20 63 6f 6e   *pCursor;.  con
2b168 73 74 20 63 68 61 72 20 2a 70 54 6f 6b 65 6e 3b  st char *pToken;
2b169 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 42 79 74  .  int nTokenByt
2b16a 65 73 3b 0a 20 20 69 6e 74 20 69 53 74 61 72 74  es;.  int iStart
2b16b 4f 66 66 73 65 74 2c 20 69 45 6e 64 4f 66 66 73  Offset, iEndOffs
2b16c 65 74 2c 20 69 50 6f 73 69 74 69 6f 6e 3b 0a 20  et, iPosition;. 
2b16d 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d   int rc;..  rc =
2b16e 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f   pTokenizer->pMo
2b16f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 70 54 6f 6b  dule->xOpen(pTok
2b170 65 6e 69 7a 65 72 2c 20 7a 54 65 78 74 2c 20 2d  enizer, zText, -
2b171 31 2c 20 26 70 43 75 72 73 6f 72 29 3b 0a 20 20  1, &pCursor);.  
2b172 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2b173 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
2b174 20 20 70 43 75 72 73 6f 72 2d 3e 70 54 6f 6b 65    pCursor->pToke
2b175 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a  nizer = pTokeniz
2b176 65 72 3b 0a 20 20 77 68 69 6c 65 28 20 53 51 4c  er;.  while( SQL
2b177 49 54 45 5f 4f 4b 3d 3d 28 72 63 3d 70 54 6f 6b  ITE_OK==(rc=pTok
2b178 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 2d  enizer->pModule-
2b179 3e 78 4e 65 78 74 28 70 43 75 72 73 6f 72 2c 0a  >xNext(pCursor,.
2b17a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b17b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b17c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b17d 20 20 20 26 70 54 6f 6b 65 6e 2c 20 26 6e 54 6f     &pToken, &nTo
2b17e 6b 65 6e 42 79 74 65 73 2c 0a 20 20 20 20 20 20  kenBytes,.      
2b17f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b181 20 20 20 20 20 20 20 20 20 20 20 20 20 26 69 53               &iS
2b182 74 61 72 74 4f 66 66 73 65 74 2c 20 26 69 45 6e  tartOffset, &iEn
2b183 64 4f 66 66 73 65 74 2c 0a 20 20 20 20 20 20 20  dOffset,.       
2b184 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b185 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b186 20 20 20 20 20 20 20 20 20 20 20 20 26 69 50 6f              &iPo
2b187 73 69 74 69 6f 6e 29 29 20 29 7b 0a 20 20 20 20  sition)) ){.    
2b188 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 3b 0a  DLCollector *p;.
2b189 20 20 20 20 69 6e 74 20 6e 44 61 74 61 3b 20 20      int nData;  
2b18a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b18b 20 2f 2a 20 53 69 7a 65 20 6f 66 20 64 6f 63 6c   /* Size of docl
2b18c 69 73 74 20 62 65 66 6f 72 65 20 6f 75 72 20 75  ist before our u
2b18d 70 64 61 74 65 2e 20 2a 2f 0a 0a 20 20 20 20 2f  pdate. */..    /
2b18e 2a 20 50 6f 73 69 74 69 6f 6e 73 20 63 61 6e 27  * Positions can'
2b18f 74 20 62 65 20 6e 65 67 61 74 69 76 65 3b 20 77  t be negative; w
2b190 65 20 75 73 65 20 2d 31 20 61 73 20 61 20 74 65  e use -1 as a te
2b191 72 6d 69 6e 61 74 6f 72 0a 20 20 20 20 20 2a 20  rminator.     * 
2b192 69 6e 74 65 72 6e 61 6c 6c 79 2e 20 20 54 6f 6b  internally.  Tok
2b193 65 6e 20 63 61 6e 27 74 20 62 65 20 4e 55 4c 4c  en can't be NULL
2b194 20 6f 72 20 65 6d 70 74 79 2e 20 2a 2f 0a 20 20   or empty. */.  
2b195 20 20 69 66 28 20 69 50 6f 73 69 74 69 6f 6e 3c    if( iPosition<
2b196 30 20 7c 7c 20 70 54 6f 6b 65 6e 20 3d 3d 20 4e  0 || pToken == N
2b197 55 4c 4c 20 7c 7c 20 6e 54 6f 6b 65 6e 42 79 74  ULL || nTokenByt
2b198 65 73 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 20  es == 0 ){.     
2b199 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
2b19a 4f 52 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  OR;.      break;
2b19b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 20 3d 20  .    }..    p = 
2b19c 66 74 73 33 48 61 73 68 46 69 6e 64 28 26 76 2d  fts3HashFind(&v-
2b19d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 2c 20 70  >pendingTerms, p
2b19e 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 42 79 74  Token, nTokenByt
2b19f 65 73 29 3b 0a 20 20 20 20 69 66 28 20 70 3d 3d  es);.    if( p==
2b1a0 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 6e 44  NULL ){.      nD
2b1a1 61 74 61 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  ata = 0;.      p
2b1a2 20 3d 20 64 6c 63 4e 65 77 28 69 44 6f 63 69 64   = dlcNew(iDocid
2b1a3 2c 20 44 4c 5f 44 45 46 41 55 4c 54 29 3b 0a 20  , DL_DEFAULT);. 
2b1a4 20 20 20 20 20 66 74 73 33 48 61 73 68 49 6e 73       fts3HashIns
2b1a5 65 72 74 28 26 76 2d 3e 70 65 6e 64 69 6e 67 54  ert(&v->pendingT
2b1a6 65 72 6d 73 2c 20 70 54 6f 6b 65 6e 2c 20 6e 54  erms, pToken, nT
2b1a7 6f 6b 65 6e 42 79 74 65 73 2c 20 70 29 3b 0a 0a  okenBytes, p);..
2b1a8 20 20 20 20 20 20 2f 2a 20 4f 76 65 72 68 65 61        /* Overhea
2b1a9 64 20 66 6f 72 20 6f 75 72 20 68 61 73 68 20 74  d for our hash t
2b1aa 61 62 6c 65 20 65 6e 74 72 79 2c 20 74 68 65 20  able entry, the 
2b1ab 6b 65 79 2c 20 61 6e 64 20 74 68 65 20 76 61 6c  key, and the val
2b1ac 75 65 2e 20 2a 2f 0a 20 20 20 20 20 20 76 2d 3e  ue. */.      v->
2b1ad 6e 50 65 6e 64 69 6e 67 44 61 74 61 20 2b 3d 20  nPendingData += 
2b1ae 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 66 74  sizeof(struct ft
2b1af 73 33 48 61 73 68 45 6c 65 6d 29 2b 73 69 7a 65  s3HashElem)+size
2b1b0 6f 66 28 2a 70 29 2b 6e 54 6f 6b 65 6e 42 79 74  of(*p)+nTokenByt
2b1b1 65 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  es;.    }else{. 
2b1b2 20 20 20 20 20 6e 44 61 74 61 20 3d 20 70 2d 3e       nData = p->
2b1b3 62 2e 6e 44 61 74 61 3b 0a 20 20 20 20 20 20 69  b.nData;.      i
2b1b4 66 28 20 70 2d 3e 64 6c 77 2e 69 50 72 65 76 44  f( p->dlw.iPrevD
2b1b5 6f 63 69 64 21 3d 69 44 6f 63 69 64 20 29 20 64  ocid!=iDocid ) d
2b1b6 6c 63 4e 65 78 74 28 70 2c 20 69 44 6f 63 69 64  lcNext(p, iDocid
2b1b7 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
2b1b8 20 69 43 6f 6c 75 6d 6e 3e 3d 30 20 29 7b 0a 20   iColumn>=0 ){. 
2b1b9 20 20 20 20 20 64 6c 63 41 64 64 50 6f 73 28 70       dlcAddPos(p
2b1ba 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 50 6f 73 69  , iColumn, iPosi
2b1bb 74 69 6f 6e 2c 20 69 53 74 61 72 74 4f 66 66 73  tion, iStartOffs
2b1bc 65 74 2c 20 69 45 6e 64 4f 66 66 73 65 74 29 3b  et, iEndOffset);
2b1bd 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41  .    }..    /* A
2b1be 63 63 75 6d 75 6c 61 74 65 20 64 61 74 61 20 61  ccumulate data a
2b1bf 64 64 65 64 20 62 79 20 64 6c 63 4e 65 77 20 6f  dded by dlcNew o
2b1c0 72 20 64 6c 63 4e 65 78 74 2c 20 61 6e 64 20 64  r dlcNext, and d
2b1c1 6c 63 41 64 64 50 6f 73 2e 20 2a 2f 0a 20 20 20  lcAddPos. */.   
2b1c2 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61   v->nPendingData
2b1c3 20 2b 3d 20 70 2d 3e 62 2e 6e 44 61 74 61 2d 6e   += p->b.nData-n
2b1c4 44 61 74 61 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  Data;.  }..  /* 
2b1c5 54 4f 44 4f 28 73 68 65 73 73 29 20 43 68 65 63  TODO(shess) Chec
2b1c6 6b 20 72 65 74 75 72 6e 3f 20 20 53 68 6f 75 6c  k return?  Shoul
2b1c7 64 20 74 68 69 73 20 62 65 20 61 62 6c 65 20 74  d this be able t
2b1c8 6f 20 63 61 75 73 65 20 65 72 72 6f 72 73 20 61  o cause errors a
2b1c9 74 0a 20 20 2a 2a 20 74 68 69 73 20 70 6f 69 6e  t.  ** this poin
2b1ca 74 3f 20 20 41 63 74 75 61 6c 6c 79 2c 20 73 61  t?  Actually, sa
2b1cb 6d 65 20 71 75 65 73 74 69 6f 6e 20 61 62 6f 75  me question abou
2b1cc 74 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69  t sqlite3_finali
2b1cd 7a 65 28 29 2c 0a 20 20 2a 2a 20 74 68 6f 75 67  ze(),.  ** thoug
2b1ce 68 20 6f 6e 65 20 63 6f 75 6c 64 20 61 72 67 75  h one could argu
2b1cf 65 20 74 68 61 74 20 66 61 69 6c 75 72 65 20 74  e that failure t
2b1d0 68 65 72 65 20 6d 65 61 6e 73 20 74 68 61 74 20  here means that 
2b1d1 74 68 65 20 64 61 74 61 20 69 73 0a 20 20 2a 2a  the data is.  **
2b1d2 20 6e 6f 74 20 64 75 72 61 62 6c 65 2e 20 20 2a   not durable.  *
2b1d3 70 6f 6e 64 65 72 2a 0a 20 20 2a 2f 0a 20 20 70  ponder*.  */.  p
2b1d4 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75  Tokenizer->pModu
2b1d5 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 43 75 72 73  le->xClose(pCurs
2b1d6 6f 72 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54  or);.  if( SQLIT
2b1d7 45 5f 44 4f 4e 45 20 3d 3d 20 72 63 20 29 20 72  E_DONE == rc ) r
2b1d8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2b1d9 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2b1da 0a 2f 2a 20 41 64 64 20 64 6f 63 6c 69 73 74 73  ./* Add doclists
2b1db 20 66 6f 72 20 61 6c 6c 20 74 65 72 6d 73 20 69   for all terms i
2b1dc 6e 20 5b 70 56 61 6c 75 65 73 5d 20 74 6f 20 70  n [pValues] to p
2b1dd 65 6e 64 69 6e 67 54 65 72 6d 73 20 74 61 62 6c  endingTerms tabl
2b1de 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e. */.static int
2b1df 20 69 6e 73 65 72 74 54 65 72 6d 73 28 66 75 6c   insertTerms(ful
2b1e0 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73  ltext_vtab *v, s
2b1e1 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63  qlite_int64 iDoc
2b1e2 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  id,.            
2b1e3 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2b1e4 65 33 5f 76 61 6c 75 65 20 2a 2a 70 56 61 6c 75  e3_value **pValu
2b1e5 65 73 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  es){.  int i;.  
2b1e6 66 6f 72 28 69 20 3d 20 30 3b 20 69 20 3c 20 76  for(i = 0; i < v
2b1e7 2d 3e 6e 43 6f 6c 75 6d 6e 20 3b 20 2b 2b 69 29  ->nColumn ; ++i)
2b1e8 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 65 78  {.    char *zTex
2b1e9 74 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74  t = (char*)sqlit
2b1ea 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 70 56  e3_value_text(pV
2b1eb 61 6c 75 65 73 5b 69 5d 29 3b 0a 20 20 20 20 69  alues[i]);.    i
2b1ec 6e 74 20 72 63 20 3d 20 62 75 69 6c 64 54 65 72  nt rc = buildTer
2b1ed 6d 73 28 76 2c 20 69 44 6f 63 69 64 2c 20 7a 54  ms(v, iDocid, zT
2b1ee 65 78 74 2c 20 69 29 3b 0a 20 20 20 20 69 66 28  ext, i);.    if(
2b1ef 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2b1f0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
2b1f1 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2b1f2 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20 65 6d  OK;.}../* Add em
2b1f3 70 74 79 20 64 6f 63 6c 69 73 74 73 20 66 6f 72  pty doclists for
2b1f4 20 61 6c 6c 20 74 65 72 6d 73 20 69 6e 20 74 68   all terms in th
2b1f5 65 20 67 69 76 65 6e 20 72 6f 77 27 73 20 63 6f  e given row's co
2b1f6 6e 74 65 6e 74 20 74 6f 0a 2a 2a 20 70 65 6e 64  ntent to.** pend
2b1f7 69 6e 67 54 65 72 6d 73 2e 0a 2a 2f 0a 73 74 61  ingTerms..*/.sta
2b1f8 74 69 63 20 69 6e 74 20 64 65 6c 65 74 65 54 65  tic int deleteTe
2b1f9 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  rms(fulltext_vta
2b1fa 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74  b *v, sqlite_int
2b1fb 36 34 20 69 44 6f 63 69 64 29 7b 0a 20 20 63 6f  64 iDocid){.  co
2b1fc 6e 73 74 20 63 68 61 72 20 2a 2a 70 56 61 6c 75  nst char **pValu
2b1fd 65 73 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63 3b  es;.  int i, rc;
2b1fe 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73  ..  /* TODO(shes
2b1ff 73 29 20 53 68 6f 75 6c 64 20 77 65 20 61 6c 6c  s) Should we all
2b200 6f 77 20 73 75 63 68 20 74 61 62 6c 65 73 20 61  ow such tables a
2b201 74 20 61 6c 6c 3f 20 2a 2f 0a 20 20 69 66 28 20  t all? */.  if( 
2b202 44 4c 5f 44 45 46 41 55 4c 54 3d 3d 44 4c 5f 44  DL_DEFAULT==DL_D
2b203 4f 43 49 44 53 20 29 20 72 65 74 75 72 6e 20 53  OCIDS ) return S
2b204 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 0a 20 20  QLITE_ERROR;..  
2b205 72 63 20 3d 20 63 6f 6e 74 65 6e 74 5f 73 65 6c  rc = content_sel
2b206 65 63 74 28 76 2c 20 69 44 6f 63 69 64 2c 20 26  ect(v, iDocid, &
2b207 70 56 61 6c 75 65 73 29 3b 0a 20 20 69 66 28 20  pValues);.  if( 
2b208 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2b209 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 66 6f  return rc;..  fo
2b20a 72 28 69 20 3d 20 30 20 3b 20 69 20 3c 20 76 2d  r(i = 0 ; i < v-
2b20b 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 20 7b  >nColumn; ++i) {
2b20c 0a 20 20 20 20 72 63 20 3d 20 62 75 69 6c 64 54  .    rc = buildT
2b20d 65 72 6d 73 28 76 2c 20 69 44 6f 63 69 64 2c 20  erms(v, iDocid, 
2b20e 70 56 61 6c 75 65 73 5b 69 5d 2c 20 2d 31 29 3b  pValues[i], -1);
2b20f 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
2b210 49 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b 0a  ITE_OK ) break;.
2b211 20 20 7d 0a 0a 20 20 66 72 65 65 53 74 72 69 6e    }..  freeStrin
2b212 67 41 72 72 61 79 28 76 2d 3e 6e 43 6f 6c 75 6d  gArray(v->nColum
2b213 6e 2c 20 70 56 61 6c 75 65 73 29 3b 0a 20 20 72  n, pValues);.  r
2b214 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2b215 0a 7d 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  .}../* TODO(shes
2b216 73 29 20 52 65 66 61 63 74 6f 72 20 74 68 65 20  s) Refactor the 
2b217 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65 20 74  code to remove t
2b218 68 69 73 20 66 6f 72 77 61 72 64 20 64 65 63 6c  his forward decl
2b219 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  . */.static int 
2b21a 69 6e 69 74 50 65 6e 64 69 6e 67 54 65 72 6d 73  initPendingTerms
2b21b 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
2b21c 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  v, sqlite_int64 
2b21d 69 44 6f 63 69 64 29 3b 0a 0a 2f 2a 20 49 6e 73  iDocid);../* Ins
2b21e 65 72 74 20 61 20 72 6f 77 20 69 6e 74 6f 20 74  ert a row into t
2b21f 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62  he %_content tab
2b220 6c 65 3b 20 73 65 74 20 2a 70 69 44 6f 63 69 64  le; set *piDocid
2b221 20 74 6f 20 62 65 20 74 68 65 20 49 44 20 6f 66   to be the ID of
2b222 20 74 68 65 0a 2a 2a 20 6e 65 77 20 72 6f 77 2e   the.** new row.
2b223 20 20 41 64 64 20 64 6f 63 6c 69 73 74 73 20 66    Add doclists f
2b224 6f 72 20 74 65 72 6d 73 20 74 6f 20 70 65 6e 64  or terms to pend
2b225 69 6e 67 54 65 72 6d 73 2e 0a 2a 2f 0a 73 74 61  ingTerms..*/.sta
2b226 74 69 63 20 69 6e 74 20 69 6e 64 65 78 5f 69 6e  tic int index_in
2b227 73 65 72 74 28 66 75 6c 6c 74 65 78 74 5f 76 74  sert(fulltext_vt
2b228 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 33 5f 76  ab *v, sqlite3_v
2b229 61 6c 75 65 20 2a 70 52 65 71 75 65 73 74 44 6f  alue *pRequestDo
2b22a 63 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20  cid,.           
2b22b 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
2b22c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 56 61  ite3_value **pVa
2b22d 6c 75 65 73 2c 20 73 71 6c 69 74 65 5f 69 6e 74  lues, sqlite_int
2b22e 36 34 20 2a 70 69 44 6f 63 69 64 29 7b 0a 20 20  64 *piDocid){.  
2b22f 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  int rc;..  rc = 
2b230 63 6f 6e 74 65 6e 74 5f 69 6e 73 65 72 74 28 76  content_insert(v
2b231 2c 20 70 52 65 71 75 65 73 74 44 6f 63 69 64 2c  , pRequestDocid,
2b232 20 70 56 61 6c 75 65 73 29 3b 20 20 2f 2a 20 65   pValues);  /* e
2b233 78 65 63 75 74 65 20 61 6e 20 53 51 4c 20 49 4e  xecute an SQL IN
2b234 53 45 52 54 20 2a 2f 0a 20 20 69 66 28 20 72 63  SERT */.  if( rc
2b235 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
2b236 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 64  turn rc;..  /* d
2b237 6f 63 69 64 20 63 6f 6c 75 6d 6e 20 69 73 20 61  ocid column is a
2b238 6e 20 61 6c 69 61 73 20 66 6f 72 20 72 6f 77 69  n alias for rowi
2b239 64 2e 20 2a 2f 0a 20 20 2a 70 69 44 6f 63 69 64  d. */.  *piDocid
2b23a 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f   = sqlite3_last_
2b23b 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 76 2d 3e  insert_rowid(v->
2b23c 64 62 29 3b 0a 20 20 72 63 20 3d 20 69 6e 69 74  db);.  rc = init
2b23d 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 76 2c 20  PendingTerms(v, 
2b23e 2a 70 69 44 6f 63 69 64 29 3b 0a 20 20 69 66 28  *piDocid);.  if(
2b23f 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2b240 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
2b241 65 74 75 72 6e 20 69 6e 73 65 72 74 54 65 72 6d  eturn insertTerm
2b242 73 28 76 2c 20 2a 70 69 44 6f 63 69 64 2c 20 70  s(v, *piDocid, p
2b243 56 61 6c 75 65 73 29 3b 0a 7d 0a 0a 2f 2a 20 44  Values);.}../* D
2b244 65 6c 65 74 65 20 61 20 72 6f 77 20 66 72 6f 6d  elete a row from
2b245 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74   the %_content t
2b246 61 62 6c 65 3b 20 61 64 64 20 65 6d 70 74 79 20  able; add empty 
2b247 64 6f 63 6c 69 73 74 73 20 66 6f 72 20 74 65 72  doclists for ter
2b248 6d 73 0a 2a 2a 20 74 6f 20 70 65 6e 64 69 6e 67  ms.** to pending
2b249 54 65 72 6d 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  Terms..*/.static
2b24a 20 69 6e 74 20 69 6e 64 65 78 5f 64 65 6c 65 74   int index_delet
2b24b 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  e(fulltext_vtab 
2b24c 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  *v, sqlite_int64
2b24d 20 69 52 6f 77 29 7b 0a 20 20 69 6e 74 20 72 63   iRow){.  int rc
2b24e 20 3d 20 69 6e 69 74 50 65 6e 64 69 6e 67 54 65   = initPendingTe
2b24f 72 6d 73 28 76 2c 20 69 52 6f 77 29 3b 0a 20 20  rms(v, iRow);.  
2b250 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2b251 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
2b252 20 20 72 63 20 3d 20 64 65 6c 65 74 65 54 65 72    rc = deleteTer
2b253 6d 73 28 76 2c 20 69 52 6f 77 29 3b 0a 20 20 69  ms(v, iRow);.  i
2b254 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2b255 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
2b256 20 72 65 74 75 72 6e 20 63 6f 6e 74 65 6e 74 5f   return content_
2b257 64 65 6c 65 74 65 28 76 2c 20 69 52 6f 77 29 3b  delete(v, iRow);
2b258 20 20 2f 2a 20 65 78 65 63 75 74 65 20 61 6e 20    /* execute an 
2b259 53 51 4c 20 44 45 4c 45 54 45 20 2a 2f 0a 7d 0a  SQL DELETE */.}.
2b25a 0a 2f 2a 20 55 70 64 61 74 65 20 61 20 72 6f 77  ./* Update a row
2b25b 20 69 6e 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e   in the %_conten
2b25c 74 20 74 61 62 6c 65 3b 20 61 64 64 20 64 65 6c  t table; add del
2b25d 65 74 65 20 64 6f 63 6c 69 73 74 73 20 74 6f 0a  ete doclists to.
2b25e 2a 2a 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20  ** pendingTerms 
2b25f 66 6f 72 20 6f 6c 64 20 74 65 72 6d 73 20 6e 6f  for old terms no
2b260 74 20 69 6e 20 74 68 65 20 6e 65 77 20 64 61 74  t in the new dat
2b261 61 2c 20 61 64 64 20 69 6e 73 65 72 74 20 64 6f  a, add insert do
2b262 63 6c 69 73 74 73 0a 2a 2a 20 74 6f 20 70 65 6e  clists.** to pen
2b263 64 69 6e 67 54 65 72 6d 73 20 66 6f 72 20 74 65  dingTerms for te
2b264 72 6d 73 20 69 6e 20 74 68 65 20 6e 65 77 20 64  rms in the new d
2b265 61 74 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ata..*/.static i
2b266 6e 74 20 69 6e 64 65 78 5f 75 70 64 61 74 65 28  nt index_update(
2b267 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
2b268 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  , sqlite_int64 i
2b269 52 6f 77 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Row,.           
2b26a 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
2b26b 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 56 61  ite3_value **pVa
2b26c 6c 75 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 20  lues){.  int rc 
2b26d 3d 20 69 6e 69 74 50 65 6e 64 69 6e 67 54 65 72  = initPendingTer
2b26e 6d 73 28 76 2c 20 69 52 6f 77 29 3b 0a 20 20 69  ms(v, iRow);.  i
2b26f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2b270 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
2b271 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 20   /* Generate an 
2b272 65 6d 70 74 79 20 64 6f 63 6c 69 73 74 20 66 6f  empty doclist fo
2b273 72 20 65 61 63 68 20 74 65 72 6d 20 74 68 61 74  r each term that
2b274 20 70 72 65 76 69 6f 75 73 6c 79 20 61 70 70 65   previously appe
2b275 61 72 65 64 20 69 6e 20 74 68 69 73 0a 20 20 20  ared in this.   
2b276 2a 20 72 6f 77 2e 20 2a 2f 0a 20 20 72 63 20 3d  * row. */.  rc =
2b277 20 64 65 6c 65 74 65 54 65 72 6d 73 28 76 2c 20   deleteTerms(v, 
2b278 69 52 6f 77 29 3b 0a 20 20 69 66 28 20 72 63 21  iRow);.  if( rc!
2b279 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
2b27a 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
2b27b 63 6f 6e 74 65 6e 74 5f 75 70 64 61 74 65 28 76  content_update(v
2b27c 2c 20 70 56 61 6c 75 65 73 2c 20 69 52 6f 77 29  , pValues, iRow)
2b27d 3b 20 20 2f 2a 20 65 78 65 63 75 74 65 20 61 6e  ;  /* execute an
2b27e 20 53 51 4c 20 55 50 44 41 54 45 20 2a 2f 0a 20   SQL UPDATE */. 
2b27f 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2b280 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
2b281 0a 20 20 2f 2a 20 4e 6f 77 20 61 64 64 20 70 6f  .  /* Now add po
2b282 73 69 74 69 6f 6e 73 20 66 6f 72 20 74 65 72 6d  sitions for term
2b283 73 20 77 68 69 63 68 20 61 70 70 65 61 72 20 69  s which appear i
2b284 6e 20 74 68 65 20 75 70 64 61 74 65 64 20 72 6f  n the updated ro
2b285 77 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 69  w. */.  return i
2b286 6e 73 65 72 74 54 65 72 6d 73 28 76 2c 20 69 52  nsertTerms(v, iR
2b287 6f 77 2c 20 70 56 61 6c 75 65 73 29 3b 0a 7d 0a  ow, pValues);.}.
2b288 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2b289 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b28a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b28b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b28c 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 49 6e 74 65 72 69  *****/./* Interi
2b28d 6f 72 57 72 69 74 65 72 20 69 73 20 75 73 65 64  orWriter is used
2b28e 20 74 6f 20 63 6f 6c 6c 65 63 74 20 74 65 72 6d   to collect term
2b28f 73 20 61 6e 64 20 62 6c 6f 63 6b 20 72 65 66 65  s and block refe
2b290 72 65 6e 63 65 73 20 69 6e 74 6f 0a 2a 2a 20 69  rences into.** i
2b291 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 69 6e  nterior nodes in
2b292 20 25 5f 73 65 67 6d 65 6e 74 73 2e 20 20 53 65   %_segments.  Se
2b293 65 20 63 6f 6d 6d 65 6e 74 61 72 79 20 61 74 20  e commentary at 
2b294 74 6f 70 20 6f 66 20 66 69 6c 65 20 66 6f 72 0a  top of file for.
2b295 2a 2a 20 66 6f 72 6d 61 74 2e 0a 2a 2f 0a 0a 2f  ** format..*/../
2b296 2a 20 48 6f 77 20 6c 61 72 67 65 20 69 6e 74 65  * How large inte
2b297 72 69 6f 72 20 6e 6f 64 65 73 20 63 61 6e 20 67  rior nodes can g
2b298 72 6f 77 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  row. */.#define 
2b299 49 4e 54 45 52 49 4f 52 5f 4d 41 58 20 32 30 34  INTERIOR_MAX 204
2b29a 38 0a 0a 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6e 75  8../* Minimum nu
2b29b 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 70 65  mber of terms pe
2b29c 72 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20  r interior node 
2b29d 28 65 78 63 65 70 74 20 74 68 65 20 72 6f 6f 74  (except the root
2b29e 29 2e 20 54 68 69 73 0a 2a 2a 20 70 72 65 76 65  ). This.** preve
2b29f 6e 74 73 20 6c 61 72 67 65 20 74 65 72 6d 73 20  nts large terms 
2b2a0 66 72 6f 6d 20 6d 61 6b 69 6e 67 20 74 68 65 20  from making the 
2b2a1 74 72 65 65 20 74 6f 6f 20 73 6b 69 6e 6e 79 20  tree too skinny 
2b2a2 2d 20 6d 75 73 74 20 62 65 20 3e 30 0a 2a 2a 20  - must be >0.** 
2b2a3 73 6f 20 74 68 61 74 20 74 68 65 20 74 72 65 65  so that the tree
2b2a4 20 61 6c 77 61 79 73 20 6d 61 6b 65 73 20 70 72   always makes pr
2b2a5 6f 67 72 65 73 73 2e 20 20 4e 6f 74 65 20 74 68  ogress.  Note th
2b2a6 61 74 20 74 68 65 20 6d 69 6e 20 74 72 65 65 0a  at the min tree.
2b2a7 2a 2a 20 66 61 6e 6f 75 74 20 77 69 6c 6c 20 62  ** fanout will b
2b2a8 65 20 49 4e 54 45 52 49 4f 52 5f 4d 49 4e 5f 54  e INTERIOR_MIN_T
2b2a9 45 52 4d 53 2b 31 2e 0a 2a 2f 0a 23 64 65 66 69  ERMS+1..*/.#defi
2b2aa 6e 65 20 49 4e 54 45 52 49 4f 52 5f 4d 49 4e 5f  ne INTERIOR_MIN_
2b2ab 54 45 52 4d 53 20 37 0a 23 69 66 20 49 4e 54 45  TERMS 7.#if INTE
2b2ac 52 49 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53 3c 31  RIOR_MIN_TERMS<1
2b2ad 0a 23 20 65 72 72 6f 72 20 49 4e 54 45 52 49 4f  .# error INTERIO
2b2ae 52 5f 4d 49 4e 5f 54 45 52 4d 53 20 6d 75 73 74  R_MIN_TERMS must
2b2af 20 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e   be greater than
2b2b0 20 30 2e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 52   0..#endif../* R
2b2b1 4f 4f 54 5f 4d 41 58 20 63 6f 6e 74 72 6f 6c 73  OOT_MAX controls
2b2b2 20 68 6f 77 20 6d 75 63 68 20 64 61 74 61 20 69   how much data i
2b2b3 73 20 73 74 6f 72 65 64 20 69 6e 6c 69 6e 65 20  s stored inline 
2b2b4 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74 0a 2a  in the segment.*
2b2b5 2a 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2f 0a  * directory..*/.
2b2b6 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 50  /* TODO(shess) P
2b2b7 75 73 68 20 52 4f 4f 54 5f 4d 41 58 20 64 6f 77  ush ROOT_MAX dow
2b2b8 6e 20 74 6f 20 77 68 6f 65 76 65 72 20 69 73 20  n to whoever is 
2b2b9 77 72 69 74 69 6e 67 20 74 68 69 6e 67 73 2e 20  writing things. 
2b2ba 20 49 74 27 73 0a 2a 2a 20 6f 6e 6c 79 20 68 65   It's.** only he
2b2bb 72 65 20 73 6f 20 74 68 61 74 20 69 6e 74 65 72  re so that inter
2b2bc 69 6f 72 57 72 69 74 65 72 52 6f 6f 74 49 6e 66  iorWriterRootInf
2b2bd 6f 28 29 20 61 6e 64 20 6c 65 61 66 57 72 69 74  o() and leafWrit
2b2be 65 72 52 6f 6f 74 49 6e 66 6f 28 29 0a 2a 2a 20  erRootInfo().** 
2b2bf 63 61 6e 20 62 6f 74 68 20 73 65 65 20 69 74 2c  can both see it,
2b2c0 20 62 75 74 20 69 66 20 74 68 65 20 63 61 6c 6c   but if the call
2b2c1 65 72 20 70 61 73 73 65 64 20 69 74 20 69 6e 2c  er passed it in,
2b2c2 20 77 65 20 77 6f 75 6c 64 6e 27 74 20 65 76 65   we wouldn't eve
2b2c3 6e 0a 2a 2a 20 6e 65 65 64 20 61 20 64 65 66 69  n.** need a defi
2b2c4 6e 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52  ne..*/.#define R
2b2c5 4f 4f 54 5f 4d 41 58 20 31 30 32 34 0a 23 69 66  OOT_MAX 1024.#if
2b2c6 20 52 4f 4f 54 5f 4d 41 58 3c 56 41 52 49 4e 54   ROOT_MAX<VARINT
2b2c7 5f 4d 41 58 2a 32 0a 23 20 65 72 72 6f 72 20 52  _MAX*2.# error R
2b2c8 4f 4f 54 5f 4d 41 58 20 6d 75 73 74 20 68 61 76  OOT_MAX must hav
2b2c9 65 20 65 6e 6f 75 67 68 20 73 70 61 63 65 20 66  e enough space f
2b2ca 6f 72 20 61 20 68 65 61 64 65 72 2e 0a 23 65 6e  or a header..#en
2b2cb 64 69 66 0a 0a 2f 2a 20 49 6e 74 65 72 69 6f 72  dif../* Interior
2b2cc 42 6c 6f 63 6b 20 73 74 6f 72 65 73 20 61 20 6c  Block stores a l
2b2cd 69 6e 6b 65 64 2d 6c 69 73 74 20 6f 66 20 69 6e  inked-list of in
2b2ce 74 65 72 69 6f 72 20 62 6c 6f 63 6b 73 20 77 68  terior blocks wh
2b2cf 69 6c 65 20 61 20 6c 6f 77 65 72 0a 2a 2a 20 6c  ile a lower.** l
2b2d0 61 79 65 72 20 69 73 20 62 65 69 6e 67 20 63 6f  ayer is being co
2b2d1 6e 73 74 72 75 63 74 65 64 2e 0a 2a 2f 0a 74 79  nstructed..*/.ty
2b2d2 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e 74  pedef struct Int
2b2d3 65 72 69 6f 72 42 6c 6f 63 6b 20 7b 0a 20 20 44  eriorBlock {.  D
2b2d4 61 74 61 42 75 66 66 65 72 20 74 65 72 6d 3b 20  ataBuffer term; 
2b2d5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 66            /* Lef
2b2d6 74 6d 6f 73 74 20 74 65 72 6d 20 69 6e 20 62 6c  tmost term in bl
2b2d7 6f 63 6b 27 73 20 73 75 62 74 72 65 65 2e 20 2a  ock's subtree. *
2b2d8 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 64  /.  DataBuffer d
2b2d9 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ata;           /
2b2da 2a 20 41 63 63 75 6d 75 6c 61 74 65 64 20 64 61  * Accumulated da
2b2db 74 61 20 66 6f 72 20 74 68 65 20 62 6c 6f 63 6b  ta for the block
2b2dc 2e 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 49 6e  . */.  struct In
2b2dd 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 6e 65 78  teriorBlock *nex
2b2de 74 3b 0a 7d 20 49 6e 74 65 72 69 6f 72 42 6c 6f  t;.} InteriorBlo
2b2df 63 6b 3b 0a 0a 73 74 61 74 69 63 20 49 6e 74 65  ck;..static Inte
2b2e0 72 69 6f 72 42 6c 6f 63 6b 20 2a 69 6e 74 65 72  riorBlock *inter
2b2e1 69 6f 72 42 6c 6f 63 6b 4e 65 77 28 69 6e 74 20  iorBlockNew(int 
2b2e2 69 48 65 69 67 68 74 2c 20 73 71 6c 69 74 65 5f  iHeight, sqlite_
2b2e3 69 6e 74 36 34 20 69 43 68 69 6c 64 42 6c 6f 63  int64 iChildBloc
2b2e4 6b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  k,.             
2b2e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b2e6 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
2b2e7 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74  char *pTerm, int
2b2e8 20 6e 54 65 72 6d 29 7b 0a 20 20 49 6e 74 65 72   nTerm){.  Inter
2b2e9 69 6f 72 42 6c 6f 63 6b 20 2a 62 6c 6f 63 6b 20  iorBlock *block 
2b2ea 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
2b2eb 28 73 69 7a 65 6f 66 28 49 6e 74 65 72 69 6f 72  (sizeof(Interior
2b2ec 42 6c 6f 63 6b 29 29 3b 0a 20 20 63 68 61 72 20  Block));.  char 
2b2ed 63 5b 56 41 52 49 4e 54 5f 4d 41 58 2b 56 41 52  c[VARINT_MAX+VAR
2b2ee 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20  INT_MAX];.  int 
2b2ef 6e 3b 0a 0a 20 20 69 66 28 20 62 6c 6f 63 6b 20  n;..  if( block 
2b2f0 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 62 6c  ){.    memset(bl
2b2f1 6f 63 6b 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a  ock, 0, sizeof(*
2b2f2 62 6c 6f 63 6b 29 29 3b 0a 20 20 20 20 64 61 74  block));.    dat
2b2f3 61 42 75 66 66 65 72 49 6e 69 74 28 26 62 6c 6f  aBufferInit(&blo
2b2f4 63 6b 2d 3e 74 65 72 6d 2c 20 30 29 3b 0a 20 20  ck->term, 0);.  
2b2f5 20 20 64 61 74 61 42 75 66 66 65 72 52 65 70 6c    dataBufferRepl
2b2f6 61 63 65 28 26 62 6c 6f 63 6b 2d 3e 74 65 72 6d  ace(&block->term
2b2f7 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b  , pTerm, nTerm);
2b2f8 0a 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 50 75  ..    n = fts3Pu
2b2f9 74 56 61 72 69 6e 74 28 63 2c 20 69 48 65 69 67  tVarint(c, iHeig
2b2fa 68 74 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74  ht);.    n += ft
2b2fb 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c  s3PutVarint(c+n,
2b2fc 20 69 43 68 69 6c 64 42 6c 6f 63 6b 29 3b 0a 20   iChildBlock);. 
2b2fd 20 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69     dataBufferIni
2b2fe 74 28 26 62 6c 6f 63 6b 2d 3e 64 61 74 61 2c 20  t(&block->data, 
2b2ff 49 4e 54 45 52 49 4f 52 5f 4d 41 58 29 3b 0a 20  INTERIOR_MAX);. 
2b300 20 20 20 64 61 74 61 42 75 66 66 65 72 52 65 70     dataBufferRep
2b301 6c 61 63 65 28 26 62 6c 6f 63 6b 2d 3e 64 61 74  lace(&block->dat
2b302 61 2c 20 63 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20  a, c, n);.  }.  
2b303 72 65 74 75 72 6e 20 62 6c 6f 63 6b 3b 0a 7d 0a  return block;.}.
2b304 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
2b305 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74  /* Verify that t
2b306 68 65 20 64 61 74 61 20 69 73 20 72 65 61 64 61  he data is reada
2b307 62 6c 65 20 61 73 20 61 6e 20 69 6e 74 65 72 69  ble as an interi
2b308 6f 72 20 6e 6f 64 65 2e 20 2a 2f 0a 73 74 61 74  or node. */.stat
2b309 69 63 20 76 6f 69 64 20 69 6e 74 65 72 69 6f 72  ic void interior
2b30a 42 6c 6f 63 6b 56 61 6c 69 64 61 74 65 28 49 6e  BlockValidate(In
2b30b 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 70 42 6c  teriorBlock *pBl
2b30c 6f 63 6b 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68  ock){.  const ch
2b30d 61 72 20 2a 70 44 61 74 61 20 3d 20 70 42 6c 6f  ar *pData = pBlo
2b30e 63 6b 2d 3e 64 61 74 61 2e 70 44 61 74 61 3b 0a  ck->data.pData;.
2b30f 20 20 69 6e 74 20 6e 44 61 74 61 20 3d 20 70 42    int nData = pB
2b310 6c 6f 63 6b 2d 3e 64 61 74 61 2e 6e 44 61 74 61  lock->data.nData
2b311 3b 0a 20 20 69 6e 74 20 6e 2c 20 69 44 75 6d 6d  ;.  int n, iDumm
2b312 79 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  y;.  sqlite_int6
2b313 34 20 69 42 6c 6f 63 6b 69 64 3b 0a 0a 20 20 61  4 iBlockid;..  a
2b314 73 73 65 72 74 28 20 6e 44 61 74 61 3e 30 20 29  ssert( nData>0 )
2b315 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 61 74  ;.  assert( pDat
2b316 61 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  a!=0 );.  assert
2b317 28 20 70 44 61 74 61 2b 6e 44 61 74 61 3e 70 44  ( pData+nData>pD
2b318 61 74 61 20 29 3b 0a 0a 20 20 2f 2a 20 4d 75 73  ata );..  /* Mus
2b319 74 20 6c 65 61 64 20 77 69 74 68 20 68 65 69 67  t lead with heig
2b31a 68 74 20 6f 66 20 6e 6f 64 65 20 61 73 20 61 20  ht of node as a 
2b31b 76 61 72 69 6e 74 28 6e 29 2c 20 6e 3e 30 20 2a  varint(n), n>0 *
2b31c 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56  /.  n = fts3GetV
2b31d 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c 20 26  arint32(pData, &
2b31e 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72  iDummy);.  asser
2b31f 74 28 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65  t( n>0 );.  asse
2b320 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b 0a  rt( iDummy>0 );.
2b321 20 20 61 73 73 65 72 74 28 20 6e 3c 6e 44 61 74    assert( n<nDat
2b322 61 20 29 3b 0a 20 20 70 44 61 74 61 20 2b 3d 20  a );.  pData += 
2b323 6e 3b 0a 20 20 6e 44 61 74 61 20 2d 3d 20 6e 3b  n;.  nData -= n;
2b324 0a 0a 20 20 2f 2a 20 4d 75 73 74 20 63 6f 6e 74  ..  /* Must cont
2b325 61 69 6e 20 69 42 6c 6f 63 6b 69 64 2e 20 2a 2f  ain iBlockid. */
2b326 0a 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61  .  n = fts3GetVa
2b327 72 69 6e 74 28 70 44 61 74 61 2c 20 26 69 42 6c  rint(pData, &iBl
2b328 6f 63 6b 69 64 29 3b 0a 20 20 61 73 73 65 72 74  ockid);.  assert
2b329 28 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72  ( n>0 );.  asser
2b32a 74 28 20 6e 3c 3d 6e 44 61 74 61 20 29 3b 0a 20  t( n<=nData );. 
2b32b 20 70 44 61 74 61 20 2b 3d 20 6e 3b 0a 20 20 6e   pData += n;.  n
2b32c 44 61 74 61 20 2d 3d 20 6e 3b 0a 0a 20 20 2f 2a  Data -= n;..  /*
2b32d 20 5a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 74 65   Zero or more te
2b32e 72 6d 73 20 6f 66 20 70 6f 73 69 74 69 76 65 20  rms of positive 
2b32f 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 69 66 28 20  length */.  if( 
2b330 6e 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 20 20  nData!=0 ){.    
2b331 2f 2a 20 46 69 72 73 74 20 74 65 72 6d 20 69 73  /* First term is
2b332 20 6e 6f 74 20 64 65 6c 74 61 2d 65 6e 63 6f 64   not delta-encod
2b333 65 64 2e 20 2a 2f 0a 20 20 20 20 6e 20 3d 20 66  ed. */.    n = f
2b334 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
2b335 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a  Data, &iDummy);.
2b336 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20      assert( n>0 
2b337 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  );.    assert( i
2b338 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 20 20 61  Dummy>0 );.    a
2b339 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3e  ssert( n+iDummy>
2b33a 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  0);.    assert( 
2b33b 6e 2b 69 44 75 6d 6d 79 3c 3d 6e 44 61 74 61 20  n+iDummy<=nData 
2b33c 29 3b 0a 20 20 20 20 70 44 61 74 61 20 2b 3d 20  );.    pData += 
2b33d 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20 20 20 6e 44  n+iDummy;.    nD
2b33e 61 74 61 20 2d 3d 20 6e 2b 69 44 75 6d 6d 79 3b  ata -= n+iDummy;
2b33f 0a 0a 20 20 20 20 2f 2a 20 46 6f 6c 6c 6f 77 69  ..    /* Followi
2b340 6e 67 20 74 65 72 6d 73 20 64 65 6c 74 61 2d 65  ng terms delta-e
2b341 6e 63 6f 64 65 64 2e 20 2a 2f 0a 20 20 20 20 77  ncoded. */.    w
2b342 68 69 6c 65 28 20 6e 44 61 74 61 21 3d 30 20 29  hile( nData!=0 )
2b343 7b 0a 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74  {.      /* Lengt
2b344 68 20 6f 66 20 73 68 61 72 65 64 20 70 72 65 66  h of shared pref
2b345 69 78 2e 20 2a 2f 0a 20 20 20 20 20 20 6e 20 3d  ix. */.      n =
2b346 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
2b347 28 70 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29  (pData, &iDummy)
2b348 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2b349 6e 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  n>0 );.      ass
2b34a 65 72 74 28 20 69 44 75 6d 6d 79 3e 3d 30 20 29  ert( iDummy>=0 )
2b34b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2b34c 6e 3c 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 20  n<nData );.     
2b34d 20 70 44 61 74 61 20 2b 3d 20 6e 3b 0a 20 20 20   pData += n;.   
2b34e 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a 0a     nData -= n;..
2b34f 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
2b350 61 6e 64 20 64 61 74 61 20 6f 66 20 64 69 73 74  and data of dist
2b351 69 6e 63 74 20 73 75 66 66 69 78 2e 20 2a 2f 0a  inct suffix. */.
2b352 20 20 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65        n = fts3Ge
2b353 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c  tVarint32(pData,
2b354 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20   &iDummy);.     
2b355 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a   assert( n>0 );.
2b356 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 44        assert( iD
2b357 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 20 20 20 20  ummy>0 );.      
2b358 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79  assert( n+iDummy
2b359 3e 30 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  >0);.      asser
2b35a 74 28 20 6e 2b 69 44 75 6d 6d 79 3c 3d 6e 44 61  t( n+iDummy<=nDa
2b35b 74 61 20 29 3b 0a 20 20 20 20 20 20 70 44 61 74  ta );.      pDat
2b35c 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20  a += n+iDummy;. 
2b35d 20 20 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e 2b       nData -= n+
2b35e 69 44 75 6d 6d 79 3b 0a 20 20 20 20 7d 0a 20 20  iDummy;.    }.  
2b35f 7d 0a 7d 0a 23 64 65 66 69 6e 65 20 41 53 53 45  }.}.#define ASSE
2b360 52 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52 49 4f  RT_VALID_INTERIO
2b361 52 5f 42 4c 4f 43 4b 28 78 29 20 69 6e 74 65 72  R_BLOCK(x) inter
2b362 69 6f 72 42 6c 6f 63 6b 56 61 6c 69 64 61 74 65  iorBlockValidate
2b363 28 78 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e  (x).#else.#defin
2b364 65 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 49  e ASSERT_VALID_I
2b365 4e 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 78 29  NTERIOR_BLOCK(x)
2b366 20 61 73 73 65 72 74 28 20 31 20 29 0a 23 65 6e   assert( 1 ).#en
2b367 64 69 66 0a 0a 74 79 70 65 64 65 66 20 73 74 72  dif..typedef str
2b368 75 63 74 20 49 6e 74 65 72 69 6f 72 57 72 69 74  uct InteriorWrit
2b369 65 72 20 7b 0a 20 20 69 6e 74 20 69 48 65 69 67  er {.  int iHeig
2b36a 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ht;             
2b36b 20 20 20 20 20 20 2f 2a 20 66 72 6f 6d 20 30 20        /* from 0 
2b36c 61 74 20 6c 65 61 76 65 73 2e 20 2a 2f 0a 20 20  at leaves. */.  
2b36d 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 66  InteriorBlock *f
2b36e 69 72 73 74 2c 20 2a 6c 61 73 74 3b 0a 20 20 73  irst, *last;.  s
2b36f 74 72 75 63 74 20 49 6e 74 65 72 69 6f 72 57 72  truct InteriorWr
2b370 69 74 65 72 20 2a 70 61 72 65 6e 74 57 72 69 74  iter *parentWrit
2b371 65 72 3b 0a 0a 20 20 44 61 74 61 42 75 66 66 65  er;..  DataBuffe
2b372 72 20 74 65 72 6d 3b 20 20 20 20 20 20 20 20 20  r term;         
2b373 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 74 65        /* Last te
2b374 72 6d 20 77 72 69 74 74 65 6e 20 74 6f 20 62 6c  rm written to bl
2b375 6f 63 6b 20 22 6c 61 73 74 22 2e 20 2a 2f 0a 20  ock "last". */. 
2b376 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f   sqlite_int64 iO
2b377 70 65 6e 69 6e 67 43 68 69 6c 64 42 6c 6f 63 6b  peningChildBlock
2b378 3b 20 2f 2a 20 46 69 72 73 74 20 63 68 69 6c 64  ; /* First child
2b379 20 62 6c 6f 63 6b 20 69 6e 20 62 6c 6f 63 6b 20   block in block 
2b37a 22 6c 61 73 74 22 2e 20 2a 2f 0a 23 69 66 6e 64  "last". */.#ifnd
2b37b 65 66 20 4e 44 45 42 55 47 0a 20 20 73 71 6c 69  ef NDEBUG.  sqli
2b37c 74 65 5f 69 6e 74 36 34 20 69 4c 61 73 74 43 68  te_int64 iLastCh
2b37d 69 6c 64 42 6c 6f 63 6b 3b 20 20 2f 2a 20 66 6f  ildBlock;  /* fo
2b37e 72 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 63 68  r consistency ch
2b37f 65 63 6b 73 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a  ecks. */.#endif.
2b380 7d 20 49 6e 74 65 72 69 6f 72 57 72 69 74 65 72  } InteriorWriter
2b381 3b 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65  ;../* Initialize
2b382 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64   an interior nod
2b383 65 20 77 68 65 72 65 20 70 54 65 72 6d 5b 6e 54  e where pTerm[nT
2b384 65 72 6d 5d 20 6d 61 72 6b 73 20 74 68 65 20 6c  erm] marks the l
2b385 65 66 74 6d 6f 73 74 0a 2a 2a 20 74 65 72 6d 20  eftmost.** term 
2b386 69 6e 20 74 68 65 20 74 72 65 65 2e 20 20 69 43  in the tree.  iC
2b387 68 69 6c 64 42 6c 6f 63 6b 20 69 73 20 74 68 65  hildBlock is the
2b388 20 6c 65 66 74 6d 6f 73 74 20 63 68 69 6c 64 20   leftmost child 
2b389 62 6c 6f 63 6b 20 61 74 20 74 68 65 0a 2a 2a 20  block at the.** 
2b38a 6e 65 78 74 20 6c 65 76 65 6c 20 64 6f 77 6e 20  next level down 
2b38b 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 61  the tree..*/.sta
2b38c 74 69 63 20 76 6f 69 64 20 69 6e 74 65 72 69 6f  tic void interio
2b38d 72 57 72 69 74 65 72 49 6e 69 74 28 69 6e 74 20  rWriterInit(int 
2b38e 69 48 65 69 67 68 74 2c 20 63 6f 6e 73 74 20 63  iHeight, const c
2b38f 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20  har *pTerm, int 
2b390 6e 54 65 72 6d 2c 0a 20 20 20 20 20 20 20 20 20  nTerm,.         
2b391 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b392 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74        sqlite_int
2b393 36 34 20 69 43 68 69 6c 64 42 6c 6f 63 6b 2c 0a  64 iChildBlock,.
2b394 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b395 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 49                 I
2b396 6e 74 65 72 69 6f 72 57 72 69 74 65 72 20 2a 70  nteriorWriter *p
2b397 57 72 69 74 65 72 29 7b 0a 20 20 49 6e 74 65 72  Writer){.  Inter
2b398 69 6f 72 42 6c 6f 63 6b 20 2a 62 6c 6f 63 6b 3b  iorBlock *block;
2b399 0a 20 20 61 73 73 65 72 74 28 20 69 48 65 69 67  .  assert( iHeig
2b39a 68 74 3e 30 20 29 3b 0a 20 20 43 4c 45 41 52 28  ht>0 );.  CLEAR(
2b39b 70 57 72 69 74 65 72 29 3b 0a 0a 20 20 70 57 72  pWriter);..  pWr
2b39c 69 74 65 72 2d 3e 69 48 65 69 67 68 74 20 3d 20  iter->iHeight = 
2b39d 69 48 65 69 67 68 74 3b 0a 20 20 70 57 72 69 74  iHeight;.  pWrit
2b39e 65 72 2d 3e 69 4f 70 65 6e 69 6e 67 43 68 69 6c  er->iOpeningChil
2b39f 64 42 6c 6f 63 6b 20 3d 20 69 43 68 69 6c 64 42  dBlock = iChildB
2b3a0 6c 6f 63 6b 3b 0a 23 69 66 6e 64 65 66 20 4e 44  lock;.#ifndef ND
2b3a1 45 42 55 47 0a 20 20 70 57 72 69 74 65 72 2d 3e  EBUG.  pWriter->
2b3a2 69 4c 61 73 74 43 68 69 6c 64 42 6c 6f 63 6b 20  iLastChildBlock 
2b3a3 3d 20 69 43 68 69 6c 64 42 6c 6f 63 6b 3b 0a 23  = iChildBlock;.#
2b3a4 65 6e 64 69 66 0a 20 20 62 6c 6f 63 6b 20 3d 20  endif.  block = 
2b3a5 69 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 4e 65 77  interiorBlockNew
2b3a6 28 69 48 65 69 67 68 74 2c 20 69 43 68 69 6c 64  (iHeight, iChild
2b3a7 42 6c 6f 63 6b 2c 20 70 54 65 72 6d 2c 20 6e 54  Block, pTerm, nT
2b3a8 65 72 6d 29 3b 0a 20 20 70 57 72 69 74 65 72 2d  erm);.  pWriter-
2b3a9 3e 6c 61 73 74 20 3d 20 70 57 72 69 74 65 72 2d  >last = pWriter-
2b3aa 3e 66 69 72 73 74 20 3d 20 62 6c 6f 63 6b 3b 0a  >first = block;.
2b3ab 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 49    ASSERT_VALID_I
2b3ac 4e 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 70 57  NTERIOR_BLOCK(pW
2b3ad 72 69 74 65 72 2d 3e 6c 61 73 74 29 3b 0a 20 20  riter->last);.  
2b3ae 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26  dataBufferInit(&
2b3af 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 2c 20 30  pWriter->term, 0
2b3b0 29 3b 0a 7d 0a 0a 2f 2a 20 41 70 70 65 6e 64 20  );.}../* Append 
2b3b1 74 68 65 20 63 68 69 6c 64 20 6e 6f 64 65 20 72  the child node r
2b3b2 6f 6f 74 65 64 20 61 74 20 69 43 68 69 6c 64 42  ooted at iChildB
2b3b3 6c 6f 63 6b 20 74 6f 20 74 68 65 20 69 6e 74 65  lock to the inte
2b3b4 72 69 6f 72 20 6e 6f 64 65 2c 0a 2a 2a 20 77 69  rior node,.** wi
2b3b5 74 68 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d 20  th pTerm[nTerm] 
2b3b6 61 73 20 74 68 65 20 6c 65 66 74 6d 6f 73 74 20  as the leftmost 
2b3b7 74 65 72 6d 20 69 6e 20 69 43 68 69 6c 64 42 6c  term in iChildBl
2b3b8 6f 63 6b 27 73 20 73 75 62 74 72 65 65 2e 0a 2a  ock's subtree..*
2b3b9 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e  /.static void in
2b3ba 74 65 72 69 6f 72 57 72 69 74 65 72 41 70 70 65  teriorWriterAppe
2b3bb 6e 64 28 49 6e 74 65 72 69 6f 72 57 72 69 74 65  nd(InteriorWrite
2b3bc 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20  r *pWriter,.    
2b3bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b3be 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
2b3bf 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20  st char *pTerm, 
2b3c0 69 6e 74 20 6e 54 65 72 6d 2c 0a 20 20 20 20 20  int nTerm,.     
2b3c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b3c2 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2b3c3 74 65 5f 69 6e 74 36 34 20 69 43 68 69 6c 64 42  te_int64 iChildB
2b3c4 6c 6f 63 6b 29 7b 0a 20 20 63 68 61 72 20 63 5b  lock){.  char c[
2b3c5 56 41 52 49 4e 54 5f 4d 41 58 2b 56 41 52 49 4e  VARINT_MAX+VARIN
2b3c6 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20 6e 2c  T_MAX];.  int n,
2b3c7 20 6e 50 72 65 66 69 78 20 3d 20 30 3b 0a 0a 20   nPrefix = 0;.. 
2b3c8 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 49 4e   ASSERT_VALID_IN
2b3c9 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 70 57 72  TERIOR_BLOCK(pWr
2b3ca 69 74 65 72 2d 3e 6c 61 73 74 29 3b 0a 0a 20 20  iter->last);..  
2b3cb 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74 65 72  /* The first ter
2b3cc 6d 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 61  m written into a
2b3cd 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20  n interior node 
2b3ce 69 73 20 61 63 74 75 61 6c 6c 79 0a 20 20 2a 2a  is actually.  **
2b3cf 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
2b3d0 20 74 68 65 20 73 65 63 6f 6e 64 20 63 68 69 6c   the second chil
2b3d1 64 20 61 64 64 65 64 20 28 74 68 65 20 66 69 72  d added (the fir
2b3d2 73 74 20 63 68 69 6c 64 20 77 61 73 20 61 64 64  st child was add
2b3d3 65 64 0a 20 20 2a 2a 20 69 6e 20 69 6e 74 65 72  ed.  ** in inter
2b3d4 69 6f 72 57 72 69 74 65 72 49 6e 69 74 2c 20 6f  iorWriterInit, o
2b3d5 72 20 69 6e 20 74 68 65 20 69 66 20 63 6c 61 75  r in the if clau
2b3d6 73 65 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d  se at the bottom
2b3d7 20 6f 66 20 74 68 69 73 0a 20 20 2a 2a 20 66 75   of this.  ** fu
2b3d8 6e 63 74 69 6f 6e 29 2e 20 20 54 68 61 74 20 74  nction).  That t
2b3d9 65 72 6d 20 67 65 74 73 20 65 6e 63 6f 64 65 64  erm gets encoded
2b3da 20 73 74 72 61 69 67 68 74 20 75 70 2c 20 77 69   straight up, wi
2b3db 74 68 20 6e 50 72 65 66 69 78 20 6c 65 66 74 0a  th nPrefix left.
2b3dc 20 20 2a 2a 20 61 74 20 30 2e 0a 20 20 2a 2f 0a    ** at 0..  */.
2b3dd 20 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e 74    if( pWriter->t
2b3de 65 72 6d 2e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a  erm.nData==0 ){.
2b3df 20 20 20 20 6e 20 3d 20 66 74 73 33 50 75 74 56      n = fts3PutV
2b3e0 61 72 69 6e 74 28 63 2c 20 6e 54 65 72 6d 29 3b  arint(c, nTerm);
2b3e1 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 68  .  }else{.    wh
2b3e2 69 6c 65 28 20 6e 50 72 65 66 69 78 3c 70 57 72  ile( nPrefix<pWr
2b3e3 69 74 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61  iter->term.nData
2b3e4 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 70   &&.           p
2b3e5 54 65 72 6d 5b 6e 50 72 65 66 69 78 5d 3d 3d 70  Term[nPrefix]==p
2b3e6 57 72 69 74 65 72 2d 3e 74 65 72 6d 2e 70 44 61  Writer->term.pDa
2b3e7 74 61 5b 6e 50 72 65 66 69 78 5d 20 29 7b 0a 20  ta[nPrefix] ){. 
2b3e8 20 20 20 20 20 6e 50 72 65 66 69 78 2b 2b 3b 0a       nPrefix++;.
2b3e9 20 20 20 20 7d 0a 0a 20 20 20 20 6e 20 3d 20 66      }..    n = f
2b3ea 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20  ts3PutVarint(c, 
2b3eb 6e 50 72 65 66 69 78 29 3b 0a 20 20 20 20 6e 20  nPrefix);.    n 
2b3ec 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74  += fts3PutVarint
2b3ed 28 63 2b 6e 2c 20 6e 54 65 72 6d 2d 6e 50 72 65  (c+n, nTerm-nPre
2b3ee 66 69 78 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64  fix);.  }..#ifnd
2b3ef 65 66 20 4e 44 45 42 55 47 0a 20 20 70 57 72 69  ef NDEBUG.  pWri
2b3f0 74 65 72 2d 3e 69 4c 61 73 74 43 68 69 6c 64 42  ter->iLastChildB
2b3f1 6c 6f 63 6b 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20  lock++;.#endif. 
2b3f2 20 61 73 73 65 72 74 28 20 70 57 72 69 74 65 72   assert( pWriter
2b3f3 2d 3e 69 4c 61 73 74 43 68 69 6c 64 42 6c 6f 63  ->iLastChildBloc
2b3f4 6b 3d 3d 69 43 68 69 6c 64 42 6c 6f 63 6b 20 29  k==iChildBlock )
2b3f5 3b 0a 0a 20 20 2f 2a 20 4f 76 65 72 66 6c 6f 77  ;..  /* Overflow
2b3f6 20 74 6f 20 61 20 6e 65 77 20 62 6c 6f 63 6b 20   to a new block 
2b3f7 69 66 20 74 68 65 20 6e 65 77 20 74 65 72 6d 20  if the new term 
2b3f8 6d 61 6b 65 73 20 74 68 65 20 63 75 72 72 65 6e  makes the curren
2b3f9 74 20 62 6c 6f 63 6b 0a 20 20 2a 2a 20 74 6f 6f  t block.  ** too
2b3fa 20 62 69 67 2c 20 61 6e 64 20 74 68 65 20 63 75   big, and the cu
2b3fb 72 72 65 6e 74 20 62 6c 6f 63 6b 20 61 6c 72 65  rrent block alre
2b3fc 61 64 79 20 68 61 73 20 65 6e 6f 75 67 68 20 74  ady has enough t
2b3fd 65 72 6d 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  erms..  */.  if(
2b3fe 20 70 57 72 69 74 65 72 2d 3e 6c 61 73 74 2d 3e   pWriter->last->
2b3ff 64 61 74 61 2e 6e 44 61 74 61 2b 6e 2b 6e 54 65  data.nData+n+nTe
2b400 72 6d 2d 6e 50 72 65 66 69 78 3e 49 4e 54 45 52  rm-nPrefix>INTER
2b401 49 4f 52 5f 4d 41 58 20 26 26 0a 20 20 20 20 20  IOR_MAX &&.     
2b402 20 69 43 68 69 6c 64 42 6c 6f 63 6b 2d 70 57 72   iChildBlock-pWr
2b403 69 74 65 72 2d 3e 69 4f 70 65 6e 69 6e 67 43 68  iter->iOpeningCh
2b404 69 6c 64 42 6c 6f 63 6b 3e 49 4e 54 45 52 49 4f  ildBlock>INTERIO
2b405 52 5f 4d 49 4e 5f 54 45 52 4d 53 20 29 7b 0a 20  R_MIN_TERMS ){. 
2b406 20 20 20 70 57 72 69 74 65 72 2d 3e 6c 61 73 74     pWriter->last
2b407 2d 3e 6e 65 78 74 20 3d 20 69 6e 74 65 72 69 6f  ->next = interio
2b408 72 42 6c 6f 63 6b 4e 65 77 28 70 57 72 69 74 65  rBlockNew(pWrite
2b409 72 2d 3e 69 48 65 69 67 68 74 2c 20 69 43 68 69  r->iHeight, iChi
2b40a 6c 64 42 6c 6f 63 6b 2c 0a 20 20 20 20 20 20 20  ldBlock,.       
2b40b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b40c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b40d 20 20 20 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d      pTerm, nTerm
2b40e 29 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  );.    pWriter->
2b40f 6c 61 73 74 20 3d 20 70 57 72 69 74 65 72 2d 3e  last = pWriter->
2b410 6c 61 73 74 2d 3e 6e 65 78 74 3b 0a 20 20 20 20  last->next;.    
2b411 70 57 72 69 74 65 72 2d 3e 69 4f 70 65 6e 69 6e  pWriter->iOpenin
2b412 67 43 68 69 6c 64 42 6c 6f 63 6b 20 3d 20 69 43  gChildBlock = iC
2b413 68 69 6c 64 42 6c 6f 63 6b 3b 0a 20 20 20 20 64  hildBlock;.    d
2b414 61 74 61 42 75 66 66 65 72 52 65 73 65 74 28 26  ataBufferReset(&
2b415 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 29 3b 0a  pWriter->term);.
2b416 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 61 74    }else{.    dat
2b417 61 42 75 66 66 65 72 41 70 70 65 6e 64 32 28 26  aBufferAppend2(&
2b418 70 57 72 69 74 65 72 2d 3e 6c 61 73 74 2d 3e 64  pWriter->last->d
2b419 61 74 61 2c 20 63 2c 20 6e 2c 0a 20 20 20 20 20  ata, c, n,.     
2b41a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b41b 20 70 54 65 72 6d 2b 6e 50 72 65 66 69 78 2c 20   pTerm+nPrefix, 
2b41c 6e 54 65 72 6d 2d 6e 50 72 65 66 69 78 29 3b 0a  nTerm-nPrefix);.
2b41d 20 20 20 20 64 61 74 61 42 75 66 66 65 72 52 65      dataBufferRe
2b41e 70 6c 61 63 65 28 26 70 57 72 69 74 65 72 2d 3e  place(&pWriter->
2b41f 74 65 72 6d 2c 20 70 54 65 72 6d 2c 20 6e 54 65  term, pTerm, nTe
2b420 72 6d 29 3b 0a 20 20 7d 0a 20 20 41 53 53 45 52  rm);.  }.  ASSER
2b421 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52  T_VALID_INTERIOR
2b422 5f 42 4c 4f 43 4b 28 70 57 72 69 74 65 72 2d 3e  _BLOCK(pWriter->
2b423 6c 61 73 74 29 3b 0a 7d 0a 0a 2f 2a 20 46 72 65  last);.}../* Fre
2b424 65 20 74 68 65 20 73 70 61 63 65 20 75 73 65 64  e the space used
2b425 20 62 79 20 70 57 72 69 74 65 72 2c 20 69 6e 63   by pWriter, inc
2b426 6c 75 64 69 6e 67 20 74 68 65 20 6c 69 6e 6b 65  luding the linke
2b427 64 2d 6c 69 73 74 20 6f 66 0a 2a 2a 20 49 6e 74  d-list of.** Int
2b428 65 72 69 6f 72 42 6c 6f 63 6b 73 2c 20 61 6e 64  eriorBlocks, and
2b429 20 70 61 72 65 6e 74 57 72 69 74 65 72 2c 20 69   parentWriter, i
2b42a 66 20 70 72 65 73 65 6e 74 2e 0a 2a 2f 0a 73 74  f present..*/.st
2b42b 61 74 69 63 20 69 6e 74 20 69 6e 74 65 72 69 6f  atic int interio
2b42c 72 57 72 69 74 65 72 44 65 73 74 72 6f 79 28 49  rWriterDestroy(I
2b42d 6e 74 65 72 69 6f 72 57 72 69 74 65 72 20 2a 70  nteriorWriter *p
2b42e 57 72 69 74 65 72 29 7b 0a 20 20 49 6e 74 65 72  Writer){.  Inter
2b42f 69 6f 72 42 6c 6f 63 6b 20 2a 62 6c 6f 63 6b 20  iorBlock *block 
2b430 3d 20 70 57 72 69 74 65 72 2d 3e 66 69 72 73 74  = pWriter->first
2b431 3b 0a 0a 20 20 77 68 69 6c 65 28 20 62 6c 6f 63  ;..  while( bloc
2b432 6b 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 49  k!=NULL ){.    I
2b433 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 62 20  nteriorBlock *b 
2b434 3d 20 62 6c 6f 63 6b 3b 0a 20 20 20 20 62 6c 6f  = block;.    blo
2b435 63 6b 20 3d 20 62 6c 6f 63 6b 2d 3e 6e 65 78 74  ck = block->next
2b436 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  ;.    dataBuffer
2b437 44 65 73 74 72 6f 79 28 26 62 2d 3e 74 65 72 6d  Destroy(&b->term
2b438 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65  );.    dataBuffe
2b439 72 44 65 73 74 72 6f 79 28 26 62 2d 3e 64 61 74  rDestroy(&b->dat
2b43a 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  a);.    sqlite3_
2b43b 66 72 65 65 28 62 29 3b 0a 20 20 7d 0a 20 20 69  free(b);.  }.  i
2b43c 66 28 20 70 57 72 69 74 65 72 2d 3e 70 61 72 65  f( pWriter->pare
2b43d 6e 74 57 72 69 74 65 72 21 3d 4e 55 4c 4c 20 29  ntWriter!=NULL )
2b43e 7b 0a 20 20 20 20 69 6e 74 65 72 69 6f 72 57 72  {.    interiorWr
2b43f 69 74 65 72 44 65 73 74 72 6f 79 28 70 57 72 69  iterDestroy(pWri
2b440 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65  ter->parentWrite
2b441 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  r);.    sqlite3_
2b442 66 72 65 65 28 70 57 72 69 74 65 72 2d 3e 70 61  free(pWriter->pa
2b443 72 65 6e 74 57 72 69 74 65 72 29 3b 0a 20 20 7d  rentWriter);.  }
2b444 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73  .  dataBufferDes
2b445 74 72 6f 79 28 26 70 57 72 69 74 65 72 2d 3e 74  troy(&pWriter->t
2b446 65 72 6d 29 3b 0a 20 20 53 43 52 41 4d 42 4c 45  erm);.  SCRAMBLE
2b447 28 70 57 72 69 74 65 72 29 3b 0a 20 20 72 65 74  (pWriter);.  ret
2b448 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
2b449 0a 0a 2f 2a 20 49 66 20 70 57 72 69 74 65 72 20  ../* If pWriter 
2b44a 63 61 6e 20 66 69 74 20 65 6e 74 69 72 65 6c 79  can fit entirely
2b44b 20 69 6e 20 52 4f 4f 54 5f 4d 41 58 2c 20 72 65   in ROOT_MAX, re
2b44c 74 75 72 6e 20 69 74 20 61 73 20 74 68 65 20 72  turn it as the r
2b44d 6f 6f 74 20 69 6e 66 6f 0a 2a 2a 20 64 69 72 65  oot info.** dire
2b44e 63 74 6c 79 2c 20 6c 65 61 76 69 6e 67 20 2a 70  ctly, leaving *p
2b44f 69 45 6e 64 42 6c 6f 63 6b 69 64 20 75 6e 63 68  iEndBlockid unch
2b450 61 6e 67 65 64 2e 20 20 4f 74 68 65 72 77 69 73  anged.  Otherwis
2b451 65 2c 20 66 6c 75 73 68 0a 2a 2a 20 70 57 72 69  e, flush.** pWri
2b452 74 65 72 20 74 6f 20 25 5f 73 65 67 6d 65 6e 74  ter to %_segment
2b453 73 2c 20 62 75 69 6c 64 69 6e 67 20 61 20 6e 65  s, building a ne
2b454 77 20 6c 61 79 65 72 20 6f 66 20 69 6e 74 65 72  w layer of inter
2b455 69 6f 72 20 6e 6f 64 65 73 2c 20 61 6e 64 0a 2a  ior nodes, and.*
2b456 2a 20 72 65 63 75 72 73 69 76 65 6c 79 20 61 73  * recursively as
2b457 6b 20 66 6f 72 20 74 68 65 69 72 20 72 6f 6f 74  k for their root
2b458 20 69 6e 74 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63   into..*/.static
2b459 20 69 6e 74 20 69 6e 74 65 72 69 6f 72 57 72 69   int interiorWri
2b45a 74 65 72 52 6f 6f 74 49 6e 66 6f 28 66 75 6c 6c  terRootInfo(full
2b45b 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 49 6e  text_vtab *v, In
2b45c 74 65 72 69 6f 72 57 72 69 74 65 72 20 2a 70 57  teriorWriter *pW
2b45d 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20  riter,.         
2b45e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b45f 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 2a           char **
2b460 70 70 52 6f 6f 74 49 6e 66 6f 2c 20 69 6e 74 20  ppRootInfo, int 
2b461 2a 70 6e 52 6f 6f 74 49 6e 66 6f 2c 0a 20 20 20  *pnRootInfo,.   
2b462 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b463 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
2b464 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69 45  qlite_int64 *piE
2b465 6e 64 42 6c 6f 63 6b 69 64 29 7b 0a 20 20 49 6e  ndBlockid){.  In
2b466 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 62 6c 6f  teriorBlock *blo
2b467 63 6b 20 3d 20 70 57 72 69 74 65 72 2d 3e 66 69  ck = pWriter->fi
2b468 72 73 74 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e  rst;.  sqlite_in
2b469 74 36 34 20 69 42 6c 6f 63 6b 69 64 20 3d 20 30  t64 iBlockid = 0
2b46a 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2f  ;.  int rc;..  /
2b46b 2a 20 49 66 20 77 65 20 63 61 6e 20 66 69 74 20  * If we can fit 
2b46c 74 68 65 20 73 65 67 6d 65 6e 74 20 69 6e 6c 69  the segment inli
2b46d 6e 65 20 2a 2f 0a 20 20 69 66 28 20 62 6c 6f 63  ne */.  if( bloc
2b46e 6b 3d 3d 70 57 72 69 74 65 72 2d 3e 6c 61 73 74  k==pWriter->last
2b46f 20 26 26 20 62 6c 6f 63 6b 2d 3e 64 61 74 61 2e   && block->data.
2b470 6e 44 61 74 61 3c 52 4f 4f 54 5f 4d 41 58 20 29  nData<ROOT_MAX )
2b471 7b 0a 20 20 20 20 2a 70 70 52 6f 6f 74 49 6e 66  {.    *ppRootInf
2b472 6f 20 3d 20 62 6c 6f 63 6b 2d 3e 64 61 74 61 2e  o = block->data.
2b473 70 44 61 74 61 3b 0a 20 20 20 20 2a 70 6e 52 6f  pData;.    *pnRo
2b474 6f 74 49 6e 66 6f 20 3d 20 62 6c 6f 63 6b 2d 3e  otInfo = block->
2b475 64 61 74 61 2e 6e 44 61 74 61 3b 0a 20 20 20 20  data.nData;.    
2b476 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
2b477 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6c 75 73  ;.  }..  /* Flus
2b478 68 20 74 68 65 20 66 69 72 73 74 20 62 6c 6f 63  h the first bloc
2b479 6b 20 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 2c  k to %_segments,
2b47a 20 61 6e 64 20 63 72 65 61 74 65 20 61 20 6e 65   and create a ne
2b47b 77 20 6c 65 76 65 6c 20 6f 66 0a 20 20 2a 2a 20  w level of.  ** 
2b47c 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2e 0a 20  interior node.. 
2b47d 20 2a 2f 0a 20 20 41 53 53 45 52 54 5f 56 41 4c   */.  ASSERT_VAL
2b47e 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42 4c 4f 43  ID_INTERIOR_BLOC
2b47f 4b 28 62 6c 6f 63 6b 29 3b 0a 20 20 72 63 20 3d  K(block);.  rc =
2b480 20 62 6c 6f 63 6b 5f 69 6e 73 65 72 74 28 76 2c   block_insert(v,
2b481 20 62 6c 6f 63 6b 2d 3e 64 61 74 61 2e 70 44 61   block->data.pDa
2b482 74 61 2c 20 62 6c 6f 63 6b 2d 3e 64 61 74 61 2e  ta, block->data.
2b483 6e 44 61 74 61 2c 20 26 69 42 6c 6f 63 6b 69 64  nData, &iBlockid
2b484 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
2b485 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
2b486 72 63 3b 0a 20 20 2a 70 69 45 6e 64 42 6c 6f 63  rc;.  *piEndBloc
2b487 6b 69 64 20 3d 20 69 42 6c 6f 63 6b 69 64 3b 0a  kid = iBlockid;.
2b488 0a 20 20 70 57 72 69 74 65 72 2d 3e 70 61 72 65  .  pWriter->pare
2b489 6e 74 57 72 69 74 65 72 20 3d 20 73 71 6c 69 74  ntWriter = sqlit
2b48a 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66  e3_malloc(sizeof
2b48b 28 2a 70 57 72 69 74 65 72 2d 3e 70 61 72 65 6e  (*pWriter->paren
2b48c 74 57 72 69 74 65 72 29 29 3b 0a 20 20 69 6e 74  tWriter));.  int
2b48d 65 72 69 6f 72 57 72 69 74 65 72 49 6e 69 74 28  eriorWriterInit(
2b48e 70 57 72 69 74 65 72 2d 3e 69 48 65 69 67 68 74  pWriter->iHeight
2b48f 2b 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  +1,.            
2b490 20 20 20 20 20 20 20 20 20 62 6c 6f 63 6b 2d 3e           block->
2b491 74 65 72 6d 2e 70 44 61 74 61 2c 20 62 6c 6f 63  term.pData, bloc
2b492 6b 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 2c 0a 20  k->term.nData,. 
2b493 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b494 20 20 20 20 69 42 6c 6f 63 6b 69 64 2c 20 70 57      iBlockid, pW
2b495 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69  riter->parentWri
2b496 74 65 72 29 3b 0a 0a 20 20 2f 2a 20 46 6c 75 73  ter);..  /* Flus
2b497 68 20 61 64 64 69 74 69 6f 6e 61 6c 20 62 6c 6f  h additional blo
2b498 63 6b 73 20 61 6e 64 20 61 70 70 65 6e 64 20 74  cks and append t
2b499 6f 20 74 68 65 20 68 69 67 68 65 72 20 69 6e 74  o the higher int
2b49a 65 72 69 6f 72 0a 20 20 2a 2a 20 6e 6f 64 65 2e  erior.  ** node.
2b49b 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 62 6c 6f 63  .  */.  for(bloc
2b49c 6b 3d 62 6c 6f 63 6b 2d 3e 6e 65 78 74 3b 20 62  k=block->next; b
2b49d 6c 6f 63 6b 21 3d 4e 55 4c 4c 3b 20 62 6c 6f 63  lock!=NULL; bloc
2b49e 6b 3d 62 6c 6f 63 6b 2d 3e 6e 65 78 74 29 7b 0a  k=block->next){.
2b49f 20 20 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44      ASSERT_VALID
2b4a0 5f 49 4e 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28  _INTERIOR_BLOCK(
2b4a1 62 6c 6f 63 6b 29 3b 0a 20 20 20 20 72 63 20 3d  block);.    rc =
2b4a2 20 62 6c 6f 63 6b 5f 69 6e 73 65 72 74 28 76 2c   block_insert(v,
2b4a3 20 62 6c 6f 63 6b 2d 3e 64 61 74 61 2e 70 44 61   block->data.pDa
2b4a4 74 61 2c 20 62 6c 6f 63 6b 2d 3e 64 61 74 61 2e  ta, block->data.
2b4a5 6e 44 61 74 61 2c 20 26 69 42 6c 6f 63 6b 69 64  nData, &iBlockid
2b4a6 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
2b4a7 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
2b4a8 6e 20 72 63 3b 0a 20 20 20 20 2a 70 69 45 6e 64  n rc;.    *piEnd
2b4a9 42 6c 6f 63 6b 69 64 20 3d 20 69 42 6c 6f 63 6b  Blockid = iBlock
2b4aa 69 64 3b 0a 0a 20 20 20 20 69 6e 74 65 72 69 6f  id;..    interio
2b4ab 72 57 72 69 74 65 72 41 70 70 65 6e 64 28 70 57  rWriterAppend(pW
2b4ac 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69  riter->parentWri
2b4ad 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ter,.           
2b4ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 6c                bl
2b4af 6f 63 6b 2d 3e 74 65 72 6d 2e 70 44 61 74 61 2c  ock->term.pData,
2b4b0 20 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2e 6e 44 61   block->term.nDa
2b4b1 74 61 2c 20 69 42 6c 6f 63 6b 69 64 29 3b 0a 20  ta, iBlockid);. 
2b4b2 20 7d 0a 0a 20 20 2f 2a 20 50 61 72 65 6e 74 20   }..  /* Parent 
2b4b3 6e 6f 64 65 20 67 65 74 73 20 74 68 65 20 63 68  node gets the ch
2b4b4 61 6e 63 65 20 74 6f 20 62 65 20 74 68 65 20 72  ance to be the r
2b4b5 6f 6f 74 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e  oot. */.  return
2b4b6 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 52   interiorWriterR
2b4b7 6f 6f 74 49 6e 66 6f 28 76 2c 20 70 57 72 69 74  ootInfo(v, pWrit
2b4b8 65 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72  er->parentWriter
2b4b9 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2b4ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b4bb 20 20 70 70 52 6f 6f 74 49 6e 66 6f 2c 20 70 6e    ppRootInfo, pn
2b4bc 52 6f 6f 74 49 6e 66 6f 2c 20 70 69 45 6e 64 42  RootInfo, piEndB
2b4bd 6c 6f 63 6b 69 64 29 3b 0a 7d 0a 0a 2f 2a 2a 2a  lockid);.}../***
2b4be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b4bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b4c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
2b4c2 2a 20 49 6e 74 65 72 69 6f 72 52 65 61 64 65 72  * InteriorReader
2b4c3 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 61 64   is used to read
2b4c4 20 6f 66 66 20 74 68 65 20 64 61 74 61 20 66 72   off the data fr
2b4c5 6f 6d 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e  om an interior n
2b4c6 6f 64 65 0a 2a 2a 20 28 73 65 65 20 63 6f 6d 6d  ode.** (see comm
2b4c7 65 6e 74 20 61 74 20 74 6f 70 20 6f 66 20 66 69  ent at top of fi
2b4c8 6c 65 20 66 6f 72 20 74 68 65 20 66 6f 72 6d 61  le for the forma
2b4c9 74 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  t)..*/.typedef s
2b4ca 74 72 75 63 74 20 49 6e 74 65 72 69 6f 72 52 65  truct InteriorRe
2b4cb 61 64 65 72 20 7b 0a 20 20 63 6f 6e 73 74 20 63  ader {.  const c
2b4cc 68 61 72 20 2a 70 44 61 74 61 3b 0a 20 20 69 6e  har *pData;.  in
2b4cd 74 20 6e 44 61 74 61 3b 0a 0a 20 20 44 61 74 61  t nData;..  Data
2b4ce 42 75 66 66 65 72 20 74 65 72 6d 3b 20 20 20 20  Buffer term;    
2b4cf 20 20 20 20 20 20 2f 2a 20 70 72 65 76 69 6f 75        /* previou
2b4d0 73 20 74 65 72 6d 2c 20 66 6f 72 20 64 65 63 6f  s term, for deco
2b4d1 64 69 6e 67 20 74 65 72 6d 20 64 65 6c 74 61 2e  ding term delta.
2b4d2 20 2a 2f 0a 0a 20 20 73 71 6c 69 74 65 5f 69 6e   */..  sqlite_in
2b4d3 74 36 34 20 69 42 6c 6f 63 6b 69 64 3b 0a 7d 20  t64 iBlockid;.} 
2b4d4 49 6e 74 65 72 69 6f 72 52 65 61 64 65 72 3b 0a  InteriorReader;.
2b4d5 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 74  .static void int
2b4d6 65 72 69 6f 72 52 65 61 64 65 72 44 65 73 74 72  eriorReaderDestr
2b4d7 6f 79 28 49 6e 74 65 72 69 6f 72 52 65 61 64 65  oy(InteriorReade
2b4d8 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 64  r *pReader){.  d
2b4d9 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
2b4da 28 26 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 29  (&pReader->term)
2b4db 3b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 52 65  ;.  SCRAMBLE(pRe
2b4dc 61 64 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44  ader);.}../* TOD
2b4dd 4f 28 73 68 65 73 73 29 20 54 68 65 20 61 73 73  O(shess) The ass
2b4de 65 72 74 69 6f 6e 73 20 61 72 65 20 67 72 65 61  ertions are grea
2b4df 74 2c 20 62 75 74 20 77 68 61 74 20 69 66 20 77  t, but what if w
2b4e0 65 27 72 65 20 69 6e 20 4e 44 45 42 55 47 0a 2a  e're in NDEBUG.*
2b4e1 2a 20 61 6e 64 20 74 68 65 20 62 6c 6f 62 20 69  * and the blob i
2b4e2 73 20 65 6d 70 74 79 20 6f 72 20 6f 74 68 65 72  s empty or other
2b4e3 77 69 73 65 20 63 6f 6e 74 61 69 6e 73 20 73 75  wise contains su
2b4e4 73 70 65 63 74 20 64 61 74 61 3f 0a 2a 2f 0a 73  spect data?.*/.s
2b4e5 74 61 74 69 63 20 76 6f 69 64 20 69 6e 74 65 72  tatic void inter
2b4e6 69 6f 72 52 65 61 64 65 72 49 6e 69 74 28 63 6f  iorReaderInit(co
2b4e7 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c  nst char *pData,
2b4e8 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20   int nData,.    
2b4e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b4ea 20 20 20 20 20 20 20 20 20 20 20 49 6e 74 65 72             Inter
2b4eb 69 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61 64  iorReader *pRead
2b4ec 65 72 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6e 54  er){.  int n, nT
2b4ed 65 72 6d 3b 0a 0a 20 20 2f 2a 20 52 65 71 75 69  erm;..  /* Requi
2b4ee 72 65 20 61 74 20 6c 65 61 73 74 20 74 68 65 20  re at least the 
2b4ef 6c 65 61 64 69 6e 67 20 66 6c 61 67 20 62 79 74  leading flag byt
2b4f0 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 6e  e */.  assert( n
2b4f1 44 61 74 61 3e 30 20 29 3b 0a 20 20 61 73 73 65  Data>0 );.  asse
2b4f2 72 74 28 20 70 44 61 74 61 5b 30 5d 21 3d 27 5c  rt( pData[0]!='\
2b4f3 30 27 20 29 3b 0a 0a 20 20 43 4c 45 41 52 28 70  0' );..  CLEAR(p
2b4f4 52 65 61 64 65 72 29 3b 0a 0a 20 20 2f 2a 20 44  Reader);..  /* D
2b4f5 65 63 6f 64 65 20 74 68 65 20 62 61 73 65 20 62  ecode the base b
2b4f6 6c 6f 63 6b 69 64 2c 20 61 6e 64 20 73 65 74 20  lockid, and set 
2b4f7 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68  the cursor to th
2b4f8 65 20 66 69 72 73 74 20 74 65 72 6d 2e 20 2a 2f  e first term. */
2b4f9 0a 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61  .  n = fts3GetVa
2b4fa 72 69 6e 74 28 70 44 61 74 61 2b 31 2c 20 26 70  rint(pData+1, &p
2b4fb 52 65 61 64 65 72 2d 3e 69 42 6c 6f 63 6b 69 64  Reader->iBlockid
2b4fc 29 3b 0a 20 20 61 73 73 65 72 74 28 20 31 2b 6e  );.  assert( 1+n
2b4fd 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20 70 52 65  <=nData );.  pRe
2b4fe 61 64 65 72 2d 3e 70 44 61 74 61 20 3d 20 70 44  ader->pData = pD
2b4ff 61 74 61 2b 31 2b 6e 3b 0a 20 20 70 52 65 61 64  ata+1+n;.  pRead
2b500 65 72 2d 3e 6e 44 61 74 61 20 3d 20 6e 44 61 74  er->nData = nDat
2b501 61 2d 28 31 2b 6e 29 3b 0a 0a 20 20 2f 2a 20 41  a-(1+n);..  /* A
2b502 20 73 69 6e 67 6c 65 2d 63 68 69 6c 64 20 69 6e   single-child in
2b503 74 65 72 69 6f 72 20 6e 6f 64 65 20 28 73 75 63  terior node (suc
2b504 68 20 61 73 20 77 68 65 6e 20 61 20 6c 65 61 66  h as when a leaf
2b505 20 6e 6f 64 65 20 77 61 73 20 74 6f 6f 0a 20 20   node was too.  
2b506 2a 2a 20 6c 61 72 67 65 20 66 6f 72 20 74 68 65  ** large for the
2b507 20 73 65 67 6d 65 6e 74 20 64 69 72 65 63 74 6f   segment directo
2b508 72 79 29 20 77 6f 6e 27 74 20 68 61 76 65 20 61  ry) won't have a
2b509 6e 79 20 74 65 72 6d 73 2e 0a 20 20 2a 2a 20 4f  ny terms..  ** O
2b50a 74 68 65 72 77 69 73 65 2c 20 64 65 63 6f 64 65  therwise, decode
2b50b 20 74 68 65 20 66 69 72 73 74 20 74 65 72 6d 2e   the first term.
2b50c 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 52 65 61  .  */.  if( pRea
2b50d 64 65 72 2d 3e 6e 44 61 74 61 3d 3d 30 20 29 7b  der->nData==0 ){
2b50e 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 49  .    dataBufferI
2b50f 6e 69 74 28 26 70 52 65 61 64 65 72 2d 3e 74 65  nit(&pReader->te
2b510 72 6d 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 7b  rm, 0);.  }else{
2b511 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65 74  .    n = fts3Get
2b512 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65 72  Varint32(pReader
2b513 2d 3e 70 44 61 74 61 2c 20 26 6e 54 65 72 6d 29  ->pData, &nTerm)
2b514 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  ;.    dataBuffer
2b515 49 6e 69 74 28 26 70 52 65 61 64 65 72 2d 3e 74  Init(&pReader->t
2b516 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20  erm, nTerm);.   
2b517 20 64 61 74 61 42 75 66 66 65 72 52 65 70 6c 61   dataBufferRepla
2b518 63 65 28 26 70 52 65 61 64 65 72 2d 3e 74 65 72  ce(&pReader->ter
2b519 6d 2c 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74  m, pReader->pDat
2b51a 61 2b 6e 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20  a+n, nTerm);.   
2b51b 20 61 73 73 65 72 74 28 20 6e 2b 6e 54 65 72 6d   assert( n+nTerm
2b51c 3c 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61  <=pReader->nData
2b51d 20 29 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d   );.    pReader-
2b51e 3e 70 44 61 74 61 20 2b 3d 20 6e 2b 6e 54 65 72  >pData += n+nTer
2b51f 6d 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  m;.    pReader->
2b520 6e 44 61 74 61 20 2d 3d 20 6e 2b 6e 54 65 72 6d  nData -= n+nTerm
2b521 3b 0a 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20  ;.  }.}..static 
2b522 69 6e 74 20 69 6e 74 65 72 69 6f 72 52 65 61 64  int interiorRead
2b523 65 72 41 74 45 6e 64 28 49 6e 74 65 72 69 6f 72  erAtEnd(Interior
2b524 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
2b525 7b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64  {.  return pRead
2b526 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 3d 3d  er->term.nData==
2b527 30 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 73 71 6c  0;.}..static sql
2b528 69 74 65 5f 69 6e 74 36 34 20 69 6e 74 65 72 69  ite_int64 interi
2b529 6f 72 52 65 61 64 65 72 43 75 72 72 65 6e 74 42  orReaderCurrentB
2b52a 6c 6f 63 6b 69 64 28 49 6e 74 65 72 69 6f 72 52  lockid(InteriorR
2b52b 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
2b52c 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65  .  return pReade
2b52d 72 2d 3e 69 42 6c 6f 63 6b 69 64 3b 0a 7d 0a 0a  r->iBlockid;.}..
2b52e 73 74 61 74 69 63 20 69 6e 74 20 69 6e 74 65 72  static int inter
2b52f 69 6f 72 52 65 61 64 65 72 54 65 72 6d 42 79 74  iorReaderTermByt
2b530 65 73 28 49 6e 74 65 72 69 6f 72 52 65 61 64 65  es(InteriorReade
2b531 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61  r *pReader){.  a
2b532 73 73 65 72 74 28 20 21 69 6e 74 65 72 69 6f 72  ssert( !interior
2b533 52 65 61 64 65 72 41 74 45 6e 64 28 70 52 65 61  ReaderAtEnd(pRea
2b534 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  der) );.  return
2b535 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e   pReader->term.n
2b536 44 61 74 61 3b 0a 7d 0a 73 74 61 74 69 63 20 63  Data;.}.static c
2b537 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e 74 65 72  onst char *inter
2b538 69 6f 72 52 65 61 64 65 72 54 65 72 6d 28 49 6e  iorReaderTerm(In
2b539 74 65 72 69 6f 72 52 65 61 64 65 72 20 2a 70 52  teriorReader *pR
2b53a 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74  eader){.  assert
2b53b 28 20 21 69 6e 74 65 72 69 6f 72 52 65 61 64 65  ( !interiorReade
2b53c 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20  rAtEnd(pReader) 
2b53d 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61  );.  return pRea
2b53e 64 65 72 2d 3e 74 65 72 6d 2e 70 44 61 74 61 3b  der->term.pData;
2b53f 0a 7d 0a 0a 2f 2a 20 53 74 65 70 20 66 6f 72 77  .}../* Step forw
2b540 61 72 64 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ard to the next 
2b541 74 65 72 6d 20 69 6e 20 74 68 65 20 6e 6f 64 65  term in the node
2b542 2e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  . */.static void
2b543 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 53   interiorReaderS
2b544 74 65 70 28 49 6e 74 65 72 69 6f 72 52 65 61 64  tep(InteriorRead
2b545 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
2b546 61 73 73 65 72 74 28 20 21 69 6e 74 65 72 69 6f  assert( !interio
2b547 72 52 65 61 64 65 72 41 74 45 6e 64 28 70 52 65  rReaderAtEnd(pRe
2b548 61 64 65 72 29 20 29 3b 0a 0a 20 20 2f 2a 20 49  ader) );..  /* I
2b549 66 20 74 68 65 20 6c 61 73 74 20 74 65 72 6d 20  f the last term 
2b54a 68 61 73 20 62 65 65 6e 20 72 65 61 64 2c 20 73  has been read, s
2b54b 69 67 6e 61 6c 20 65 6f 66 2c 20 65 6c 73 65 20  ignal eof, else 
2b54c 63 6f 6e 73 74 72 75 63 74 20 74 68 65 0a 20 20  construct the.  
2b54d 2a 2a 20 6e 65 78 74 20 74 65 72 6d 2e 0a 20 20  ** next term..  
2b54e 2a 2f 0a 20 20 69 66 28 20 70 52 65 61 64 65 72  */.  if( pReader
2b54f 2d 3e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20  ->nData==0 ){.  
2b550 20 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65    dataBufferRese
2b551 74 28 26 70 52 65 61 64 65 72 2d 3e 74 65 72 6d  t(&pReader->term
2b552 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
2b553 69 6e 74 20 6e 2c 20 6e 50 72 65 66 69 78 2c 20  int n, nPrefix, 
2b554 6e 53 75 66 66 69 78 3b 0a 0a 20 20 20 20 6e 20  nSuffix;..    n 
2b555 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33  = fts3GetVarint3
2b556 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61  2(pReader->pData
2b557 2c 20 26 6e 50 72 65 66 69 78 29 3b 0a 20 20 20  , &nPrefix);.   
2b558 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72   n += fts3GetVar
2b559 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70  int32(pReader->p
2b55a 44 61 74 61 2b 6e 2c 20 26 6e 53 75 66 66 69 78  Data+n, &nSuffix
2b55b 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 72 75 6e 63  );..    /* Trunc
2b55c 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ate the current 
2b55d 74 65 72 6d 20 61 6e 64 20 61 70 70 65 6e 64 20  term and append 
2b55e 73 75 66 66 69 78 20 64 61 74 61 2e 20 2a 2f 0a  suffix data. */.
2b55f 20 20 20 20 70 52 65 61 64 65 72 2d 3e 74 65 72      pReader->ter
2b560 6d 2e 6e 44 61 74 61 20 3d 20 6e 50 72 65 66 69  m.nData = nPrefi
2b561 78 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65  x;.    dataBuffe
2b562 72 41 70 70 65 6e 64 28 26 70 52 65 61 64 65 72  rAppend(&pReader
2b563 2d 3e 74 65 72 6d 2c 20 70 52 65 61 64 65 72 2d  ->term, pReader-
2b564 3e 70 44 61 74 61 2b 6e 2c 20 6e 53 75 66 66 69  >pData+n, nSuffi
2b565 78 29 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28  x);..    assert(
2b566 20 6e 2b 6e 53 75 66 66 69 78 3c 3d 70 52 65 61   n+nSuffix<=pRea
2b567 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20  der->nData );.  
2b568 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61    pReader->pData
2b569 20 2b 3d 20 6e 2b 6e 53 75 66 66 69 78 3b 0a 20   += n+nSuffix;. 
2b56a 20 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74     pReader->nDat
2b56b 61 20 2d 3d 20 6e 2b 6e 53 75 66 66 69 78 3b 0a  a -= n+nSuffix;.
2b56c 20 20 7d 0a 20 20 70 52 65 61 64 65 72 2d 3e 69    }.  pReader->i
2b56d 42 6c 6f 63 6b 69 64 2b 2b 3b 0a 7d 0a 0a 2f 2a  Blockid++;.}../*
2b56e 20 43 6f 6d 70 61 72 65 20 74 68 65 20 63 75 72   Compare the cur
2b56f 72 65 6e 74 20 74 65 72 6d 20 74 6f 20 70 54 65  rent term to pTe
2b570 72 6d 5b 6e 54 65 72 6d 5d 2c 20 72 65 74 75 72  rm[nTerm], retur
2b571 6e 69 6e 67 20 73 74 72 63 6d 70 2d 73 74 79 6c  ning strcmp-styl
2b572 65 0a 2a 2a 20 72 65 73 75 6c 74 73 2e 20 20 49  e.** results.  I
2b573 66 20 69 73 50 72 65 66 69 78 2c 20 65 71 75 61  f isPrefix, equa
2b574 6c 69 74 79 20 6d 65 61 6e 73 20 65 71 75 61 6c  lity means equal
2b575 20 74 68 72 6f 75 67 68 20 6e 54 65 72 6d 20 62   through nTerm b
2b576 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ytes..*/.static 
2b577 69 6e 74 20 69 6e 74 65 72 69 6f 72 52 65 61 64  int interiorRead
2b578 65 72 54 65 72 6d 43 6d 70 28 49 6e 74 65 72 69  erTermCmp(Interi
2b579 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  orReader *pReade
2b57a 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
2b57b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b57c 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2b57d 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d  pTerm, int nTerm
2b57e 2c 20 69 6e 74 20 69 73 50 72 65 66 69 78 29 7b  , int isPrefix){
2b57f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  .  const char *p
2b580 52 65 61 64 65 72 54 65 72 6d 20 3d 20 69 6e 74  ReaderTerm = int
2b581 65 72 69 6f 72 52 65 61 64 65 72 54 65 72 6d 28  eriorReaderTerm(
2b582 70 52 65 61 64 65 72 29 3b 0a 20 20 69 6e 74 20  pReader);.  int 
2b583 6e 52 65 61 64 65 72 54 65 72 6d 20 3d 20 69 6e  nReaderTerm = in
2b584 74 65 72 69 6f 72 52 65 61 64 65 72 54 65 72 6d  teriorReaderTerm
2b585 42 79 74 65 73 28 70 52 65 61 64 65 72 29 3b 0a  Bytes(pReader);.
2b586 20 20 69 6e 74 20 63 2c 20 6e 20 3d 20 6e 52 65    int c, n = nRe
2b587 61 64 65 72 54 65 72 6d 3c 6e 54 65 72 6d 20 3f  aderTerm<nTerm ?
2b588 20 6e 52 65 61 64 65 72 54 65 72 6d 20 3a 20 6e   nReaderTerm : n
2b589 54 65 72 6d 3b 0a 0a 20 20 69 66 28 20 6e 3d 3d  Term;..  if( n==
2b58a 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 52 65  0 ){.    if( nRe
2b58b 61 64 65 72 54 65 72 6d 3e 30 20 29 20 72 65 74  aderTerm>0 ) ret
2b58c 75 72 6e 20 2d 31 3b 0a 20 20 20 20 69 66 28 20  urn -1;.    if( 
2b58d 6e 54 65 72 6d 3e 30 20 29 20 72 65 74 75 72 6e  nTerm>0 ) return
2b58e 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   1;.    return 0
2b58f 3b 0a 20 20 7d 0a 0a 20 20 63 20 3d 20 6d 65 6d  ;.  }..  c = mem
2b590 63 6d 70 28 70 52 65 61 64 65 72 54 65 72 6d 2c  cmp(pReaderTerm,
2b591 20 70 54 65 72 6d 2c 20 6e 29 3b 0a 20 20 69 66   pTerm, n);.  if
2b592 28 20 63 21 3d 30 20 29 20 72 65 74 75 72 6e 20  ( c!=0 ) return 
2b593 63 3b 0a 20 20 69 66 28 20 69 73 50 72 65 66 69  c;.  if( isPrefi
2b594 78 20 26 26 20 6e 3d 3d 6e 54 65 72 6d 20 29 20  x && n==nTerm ) 
2b595 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 75  return 0;.  retu
2b596 72 6e 20 6e 52 65 61 64 65 72 54 65 72 6d 20 2d  rn nReaderTerm -
2b597 20 6e 54 65 72 6d 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a   nTerm;.}../****
2b598 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b599 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b59a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b59b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2b59c 20 4c 65 61 66 57 72 69 74 65 72 20 69 73 20 75   LeafWriter is u
2b59d 73 65 64 20 74 6f 20 63 6f 6c 6c 65 63 74 20 74  sed to collect t
2b59e 65 72 6d 73 20 61 6e 64 20 61 73 73 6f 63 69 61  erms and associa
2b59f 74 65 64 20 64 6f 63 6c 69 73 74 20 64 61 74 61  ted doclist data
2b5a0 0a 2a 2a 20 69 6e 74 6f 20 6c 65 61 66 20 62 6c  .** into leaf bl
2b5a1 6f 63 6b 73 20 69 6e 20 25 5f 73 65 67 6d 65 6e  ocks in %_segmen
2b5a2 74 73 20 28 73 65 65 20 74 6f 70 20 6f 66 20 66  ts (see top of f
2b5a3 69 6c 65 20 66 6f 72 20 66 6f 72 6d 61 74 20 69  ile for format i
2b5a4 6e 66 6f 29 2e 0a 2a 2a 20 45 78 70 65 63 74 65  nfo)..** Expecte
2b5a5 64 20 75 73 61 67 65 20 69 73 3a 0a 2a 2a 0a 2a  d usage is:.**.*
2b5a6 2a 20 4c 65 61 66 57 72 69 74 65 72 20 77 72 69  * LeafWriter wri
2b5a7 74 65 72 3b 0a 2a 2a 20 6c 65 61 66 57 72 69 74  ter;.** leafWrit
2b5a8 65 72 49 6e 69 74 28 30 2c 20 30 2c 20 26 77 72  erInit(0, 0, &wr
2b5a9 69 74 65 72 29 3b 0a 2a 2a 20 77 68 69 6c 65 28  iter);.** while(
2b5aa 20 73 6f 72 74 65 64 5f 74 65 72 6d 73 5f 6c 65   sorted_terms_le
2b5ab 66 74 5f 74 6f 5f 70 72 6f 63 65 73 73 20 29 7b  ft_to_process ){
2b5ac 0a 2a 2a 20 20 20 2f 2f 20 64 61 74 61 20 69 73  .**   // data is
2b5ad 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 66 6f   doclist data fo
2b5ae 72 20 74 68 61 74 20 74 65 72 6d 2e 0a 2a 2a 20  r that term..** 
2b5af 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65    rc = leafWrite
2b5b0 72 53 74 65 70 28 76 2c 20 26 77 72 69 74 65 72  rStep(v, &writer
2b5b1 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20  , pTerm, nTerm, 
2b5b2 70 44 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a 2a  pData, nData);.*
2b5b3 2a 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  *   if( rc!=SQLI
2b5b4 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72  TE_OK ) goto err
2b5b5 3b 0a 2a 2a 20 7d 0a 2a 2a 20 72 63 20 3d 20 6c  ;.** }.** rc = l
2b5b6 65 61 66 57 72 69 74 65 72 46 69 6e 61 6c 69 7a  eafWriterFinaliz
2b5b7 65 28 76 2c 20 26 77 72 69 74 65 72 29 3b 0a 2a  e(v, &writer);.*
2b5b8 2a 65 72 72 3a 0a 2a 2a 20 6c 65 61 66 57 72 69  *err:.** leafWri
2b5b9 74 65 72 44 65 73 74 72 6f 79 28 26 77 72 69 74  terDestroy(&writ
2b5ba 65 72 29 3b 0a 2a 2a 20 72 65 74 75 72 6e 20 72  er);.** return r
2b5bb 63 3b 0a 2a 2a 0a 2a 2a 20 6c 65 61 66 57 72 69  c;.**.** leafWri
2b5bc 74 65 72 53 74 65 70 28 29 20 6d 61 79 20 77 72  terStep() may wr
2b5bd 69 74 65 20 61 20 63 6f 6c 6c 65 63 74 65 64 20  ite a collected 
2b5be 6c 65 61 66 20 6f 75 74 20 74 6f 20 25 5f 73 65  leaf out to %_se
2b5bf 67 6d 65 6e 74 73 2e 0a 2a 2a 20 6c 65 61 66 57  gments..** leafW
2b5c0 72 69 74 65 72 46 69 6e 61 6c 69 7a 65 28 29 20  riterFinalize() 
2b5c1 66 69 6e 69 73 68 65 73 20 77 72 69 74 69 6e 67  finishes writing
2b5c2 20 61 6e 79 20 62 75 66 66 65 72 65 64 20 64 61   any buffered da
2b5c3 74 61 20 61 6e 64 20 73 74 6f 72 65 73 0a 2a 2a  ta and stores.**
2b5c4 20 61 20 72 6f 6f 74 20 6e 6f 64 65 20 69 6e 20   a root node in 
2b5c5 25 5f 73 65 67 64 69 72 2e 20 20 6c 65 61 66 57  %_segdir.  leafW
2b5c6 72 69 74 65 72 44 65 73 74 72 6f 79 28 29 20 66  riterDestroy() f
2b5c7 72 65 65 73 20 61 6c 6c 20 62 75 66 66 65 72 73  rees all buffers
2b5c8 20 61 6e 64 0a 2a 2a 20 49 6e 74 65 72 69 6f 72   and.** Interior
2b5c9 57 72 69 74 65 72 73 20 61 6c 6c 6f 63 61 74 65  Writers allocate
2b5ca 64 20 61 73 20 70 61 72 74 20 6f 66 20 77 72 69  d as part of wri
2b5cb 74 69 6e 67 20 74 68 69 73 20 73 65 67 6d 65 6e  ting this segmen
2b5cc 74 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73 68  t..**.** TODO(sh
2b5cd 65 73 73 29 20 44 6f 63 75 6d 65 6e 74 20 6c 65  ess) Document le
2b5ce 61 66 57 72 69 74 65 72 53 74 65 70 4d 65 72 67  afWriterStepMerg
2b5cf 65 28 29 2e 0a 2a 2f 0a 0a 2f 2a 20 50 75 74 20  e()..*/../* Put 
2b5d0 74 65 72 6d 73 20 77 69 74 68 20 64 61 74 61 20  terms with data 
2b5d1 74 68 69 73 20 62 69 67 20 69 6e 20 74 68 65 69  this big in thei
2b5d2 72 20 6f 77 6e 20 62 6c 6f 63 6b 2e 20 2a 2f 0a  r own block. */.
2b5d3 23 64 65 66 69 6e 65 20 53 54 41 4e 44 41 4c 4f  #define STANDALO
2b5d4 4e 45 5f 4d 49 4e 20 31 30 32 34 0a 0a 2f 2a 20  NE_MIN 1024../* 
2b5d5 4b 65 65 70 20 6c 65 61 66 20 62 6c 6f 63 6b 73  Keep leaf blocks
2b5d6 20 62 65 6c 6f 77 20 74 68 69 73 20 73 69 7a 65   below this size
2b5d7 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4c 45 41  . */.#define LEA
2b5d8 46 5f 4d 41 58 20 32 30 34 38 0a 0a 74 79 70 65  F_MAX 2048..type
2b5d9 64 65 66 20 73 74 72 75 63 74 20 4c 65 61 66 57  def struct LeafW
2b5da 72 69 74 65 72 20 7b 0a 20 20 69 6e 74 20 69 4c  riter {.  int iL
2b5db 65 76 65 6c 3b 0a 20 20 69 6e 74 20 69 64 78 3b  evel;.  int idx;
2b5dc 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
2b5dd 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 3b 20 20  iStartBlockid;  
2b5de 20 20 20 2f 2a 20 6e 65 65 64 65 64 20 74 6f 20     /* needed to 
2b5df 63 72 65 61 74 65 20 74 68 65 20 72 6f 6f 74 20  create the root 
2b5e0 69 6e 66 6f 20 2a 2f 0a 20 20 73 71 6c 69 74 65  info */.  sqlite
2b5e1 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c 6f 63 6b  _int64 iEndBlock
2b5e2 69 64 3b 20 20 20 20 20 20 20 2f 2a 20 77 68 65  id;       /* whe
2b5e3 6e 20 77 65 27 72 65 20 64 6f 6e 65 20 77 72 69  n we're done wri
2b5e4 74 69 6e 67 2e 20 2a 2f 0a 0a 20 20 44 61 74 61  ting. */..  Data
2b5e5 42 75 66 66 65 72 20 74 65 72 6d 3b 20 20 20 20  Buffer term;    
2b5e6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70              /* p
2b5e7 72 65 76 69 6f 75 73 20 65 6e 63 6f 64 65 64 20  revious encoded 
2b5e8 74 65 72 6d 20 2a 2f 0a 20 20 44 61 74 61 42 75  term */.  DataBu
2b5e9 66 66 65 72 20 64 61 74 61 3b 20 20 20 20 20 20  ffer data;      
2b5ea 20 20 20 20 20 20 20 20 20 20 2f 2a 20 65 6e 63            /* enc
2b5eb 6f 64 69 6e 67 20 62 75 66 66 65 72 20 2a 2f 0a  oding buffer */.
2b5ec 0a 20 20 2f 2a 20 62 79 74 65 73 20 6f 66 20 66  .  /* bytes of f
2b5ed 69 72 73 74 20 74 65 72 6d 20 69 6e 20 74 68 65  irst term in the
2b5ee 20 63 75 72 72 65 6e 74 20 6e 6f 64 65 20 77 68   current node wh
2b5ef 69 63 68 20 64 69 73 74 69 6e 67 75 69 73 68 65  ich distinguishe
2b5f0 73 20 74 68 61 74 0a 20 20 2a 2a 20 74 65 72 6d  s that.  ** term
2b5f1 20 66 72 6f 6d 20 74 68 65 20 6c 61 73 74 20 74   from the last t
2b5f2 65 72 6d 20 6f 66 20 74 68 65 20 70 72 65 76 69  erm of the previ
2b5f3 6f 75 73 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20  ous node..  */. 
2b5f4 20 69 6e 74 20 6e 54 65 72 6d 44 69 73 74 69 6e   int nTermDistin
2b5f5 63 74 3b 0a 0a 20 20 49 6e 74 65 72 69 6f 72 57  ct;..  InteriorW
2b5f6 72 69 74 65 72 20 70 61 72 65 6e 74 57 72 69 74  riter parentWrit
2b5f7 65 72 3b 20 20 20 20 2f 2a 20 69 66 20 77 65 20  er;    /* if we 
2b5f8 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 69 6e  overflow */.  in
2b5f9 74 20 68 61 73 5f 70 61 72 65 6e 74 3b 0a 7d 20  t has_parent;.} 
2b5fa 4c 65 61 66 57 72 69 74 65 72 3b 0a 0a 73 74 61  LeafWriter;..sta
2b5fb 74 69 63 20 76 6f 69 64 20 6c 65 61 66 57 72 69  tic void leafWri
2b5fc 74 65 72 49 6e 69 74 28 69 6e 74 20 69 4c 65 76  terInit(int iLev
2b5fd 65 6c 2c 20 69 6e 74 20 69 64 78 2c 20 4c 65 61  el, int idx, Lea
2b5fe 66 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  fWriter *pWriter
2b5ff 29 7b 0a 20 20 43 4c 45 41 52 28 70 57 72 69 74  ){.  CLEAR(pWrit
2b600 65 72 29 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e  er);.  pWriter->
2b601 69 4c 65 76 65 6c 20 3d 20 69 4c 65 76 65 6c 3b  iLevel = iLevel;
2b602 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 64 78 20  .  pWriter->idx 
2b603 3d 20 69 64 78 3b 0a 0a 20 20 64 61 74 61 42 75  = idx;..  dataBu
2b604 66 66 65 72 49 6e 69 74 28 26 70 57 72 69 74 65  fferInit(&pWrite
2b605 72 2d 3e 74 65 72 6d 2c 20 33 32 29 3b 0a 0a 20  r->term, 32);.. 
2b606 20 2f 2a 20 53 74 61 72 74 20 6f 75 74 20 77 69   /* Start out wi
2b607 74 68 20 61 20 72 65 61 73 6f 6e 61 62 6c 79 20  th a reasonably 
2b608 73 69 7a 65 64 20 62 6c 6f 63 6b 2c 20 74 68 6f  sized block, tho
2b609 75 67 68 20 69 74 20 63 61 6e 20 67 72 6f 77 2e  ugh it can grow.
2b60a 20 2a 2f 0a 20 20 64 61 74 61 42 75 66 66 65 72   */.  dataBuffer
2b60b 49 6e 69 74 28 26 70 57 72 69 74 65 72 2d 3e 64  Init(&pWriter->d
2b60c 61 74 61 2c 20 4c 45 41 46 5f 4d 41 58 29 3b 0a  ata, LEAF_MAX);.
2b60d 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  }..#ifndef NDEBU
2b60e 47 0a 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74  G./* Verify that
2b60f 20 74 68 65 20 64 61 74 61 20 69 73 20 72 65 61   the data is rea
2b610 64 61 62 6c 65 20 61 73 20 61 20 6c 65 61 66 20  dable as a leaf 
2b611 6e 6f 64 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20  node. */.static 
2b612 76 6f 69 64 20 6c 65 61 66 4e 6f 64 65 56 61 6c  void leafNodeVal
2b613 69 64 61 74 65 28 63 6f 6e 73 74 20 63 68 61 72  idate(const char
2b614 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61   *pData, int nDa
2b615 74 61 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 69 44  ta){.  int n, iD
2b616 75 6d 6d 79 3b 0a 0a 20 20 69 66 28 20 6e 44 61  ummy;..  if( nDa
2b617 74 61 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  ta==0 ) return;.
2b618 20 20 61 73 73 65 72 74 28 20 6e 44 61 74 61 3e    assert( nData>
2b619 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
2b61a 44 61 74 61 21 3d 30 20 29 3b 0a 20 20 61 73 73  Data!=0 );.  ass
2b61b 65 72 74 28 20 70 44 61 74 61 2b 6e 44 61 74 61  ert( pData+nData
2b61c 3e 70 44 61 74 61 20 29 3b 0a 0a 20 20 2f 2a 20  >pData );..  /* 
2b61d 4d 75 73 74 20 6c 65 61 64 20 77 69 74 68 20 61  Must lead with a
2b61e 20 76 61 72 69 6e 74 28 30 29 20 2a 2f 0a 20 20   varint(0) */.  
2b61f 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e  n = fts3GetVarin
2b620 74 33 32 28 70 44 61 74 61 2c 20 26 69 44 75 6d  t32(pData, &iDum
2b621 6d 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  my);.  assert( i
2b622 44 75 6d 6d 79 3d 3d 30 20 29 3b 0a 20 20 61 73  Dummy==0 );.  as
2b623 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 61  sert( n>0 );.  a
2b624 73 73 65 72 74 28 20 6e 3c 6e 44 61 74 61 20 29  ssert( n<nData )
2b625 3b 0a 20 20 70 44 61 74 61 20 2b 3d 20 6e 3b 0a  ;.  pData += n;.
2b626 20 20 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a 0a 20    nData -= n;.. 
2b627 20 2f 2a 20 4c 65 61 64 69 6e 67 20 74 65 72 6d   /* Leading term
2b628 20 6c 65 6e 67 74 68 20 61 6e 64 20 64 61 74 61   length and data
2b629 20 6d 75 73 74 20 66 69 74 20 69 6e 20 62 75 66   must fit in buf
2b62a 66 65 72 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66 74  fer. */.  n = ft
2b62b 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 44  s3GetVarint32(pD
2b62c 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20  ata, &iDummy);. 
2b62d 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a   assert( n>0 );.
2b62e 20 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d 79    assert( iDummy
2b62f 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
2b630 6e 2b 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20  n+iDummy>0 );.  
2b631 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79  assert( n+iDummy
2b632 3c 6e 44 61 74 61 20 29 3b 0a 20 20 70 44 61 74  <nData );.  pDat
2b633 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20  a += n+iDummy;. 
2b634 20 6e 44 61 74 61 20 2d 3d 20 6e 2b 69 44 75 6d   nData -= n+iDum
2b635 6d 79 3b 0a 0a 20 20 2f 2a 20 4c 65 61 64 69 6e  my;..  /* Leadin
2b636 67 20 74 65 72 6d 27 73 20 64 6f 63 6c 69 73 74  g term's doclist
2b637 20 6c 65 6e 67 74 68 20 61 6e 64 20 64 61 74 61   length and data
2b638 20 6d 75 73 74 20 66 69 74 2e 20 2a 2f 0a 20 20   must fit. */.  
2b639 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e  n = fts3GetVarin
2b63a 74 33 32 28 70 44 61 74 61 2c 20 26 69 44 75 6d  t32(pData, &iDum
2b63b 6d 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  my);.  assert( n
2b63c 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
2b63d 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 61 73  iDummy>0 );.  as
2b63e 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3e 30  sert( n+iDummy>0
2b63f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 2b   );.  assert( n+
2b640 69 44 75 6d 6d 79 3c 3d 6e 44 61 74 61 20 29 3b  iDummy<=nData );
2b641 0a 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f  .  ASSERT_VALID_
2b642 44 4f 43 4c 49 53 54 28 44 4c 5f 44 45 46 41 55  DOCLIST(DL_DEFAU
2b643 4c 54 2c 20 70 44 61 74 61 2b 6e 2c 20 69 44 75  LT, pData+n, iDu
2b644 6d 6d 79 2c 20 4e 55 4c 4c 29 3b 0a 20 20 70 44  mmy, NULL);.  pD
2b645 61 74 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b  ata += n+iDummy;
2b646 0a 20 20 6e 44 61 74 61 20 2d 3d 20 6e 2b 69 44  .  nData -= n+iD
2b647 75 6d 6d 79 3b 0a 0a 20 20 2f 2a 20 56 65 72 69  ummy;..  /* Veri
2b648 66 79 20 74 68 61 74 20 74 72 61 69 6c 69 6e 67  fy that trailing
2b649 20 74 65 72 6d 73 20 61 6e 64 20 64 6f 63 6c 69   terms and docli
2b64a 73 74 73 20 61 6c 73 6f 20 61 72 65 20 72 65 61  sts also are rea
2b64b 64 61 62 6c 65 2e 20 2a 2f 0a 20 20 77 68 69 6c  dable. */.  whil
2b64c 65 28 20 6e 44 61 74 61 21 3d 30 20 29 7b 0a 20  e( nData!=0 ){. 
2b64d 20 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61     n = fts3GetVa
2b64e 72 69 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69  rint32(pData, &i
2b64f 44 75 6d 6d 79 29 3b 0a 20 20 20 20 61 73 73 65  Dummy);.    asse
2b650 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 20 20 61  rt( n>0 );.    a
2b651 73 73 65 72 74 28 20 69 44 75 6d 6d 79 3e 3d 30  ssert( iDummy>=0
2b652 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
2b653 6e 3c 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 70  n<nData );.    p
2b654 44 61 74 61 20 2b 3d 20 6e 3b 0a 20 20 20 20 6e  Data += n;.    n
2b655 44 61 74 61 20 2d 3d 20 6e 3b 0a 20 20 20 20 6e  Data -= n;.    n
2b656 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
2b657 33 32 28 70 44 61 74 61 2c 20 26 69 44 75 6d 6d  32(pData, &iDumm
2b658 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  y);.    assert( 
2b659 6e 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  n>0 );.    asser
2b65a 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20  t( iDummy>0 );. 
2b65b 20 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75     assert( n+iDu
2b65c 6d 6d 79 3e 30 20 29 3b 0a 20 20 20 20 61 73 73  mmy>0 );.    ass
2b65d 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c 6e 44  ert( n+iDummy<nD
2b65e 61 74 61 20 29 3b 0a 20 20 20 20 70 44 61 74 61  ata );.    pData
2b65f 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20   += n+iDummy;.  
2b660 20 20 6e 44 61 74 61 20 2d 3d 20 6e 2b 69 44 75    nData -= n+iDu
2b661 6d 6d 79 3b 0a 0a 20 20 20 20 6e 20 3d 20 66 74  mmy;..    n = ft
2b662 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 44  s3GetVarint32(pD
2b663 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20  ata, &iDummy);. 
2b664 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29     assert( n>0 )
2b665 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44  ;.    assert( iD
2b666 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 20 20 61 73  ummy>0 );.    as
2b667 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3e 30  sert( n+iDummy>0
2b668 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
2b669 6e 2b 69 44 75 6d 6d 79 3c 3d 6e 44 61 74 61 20  n+iDummy<=nData 
2b66a 29 3b 0a 20 20 20 20 41 53 53 45 52 54 5f 56 41  );.    ASSERT_VA
2b66b 4c 49 44 5f 44 4f 43 4c 49 53 54 28 44 4c 5f 44  LID_DOCLIST(DL_D
2b66c 45 46 41 55 4c 54 2c 20 70 44 61 74 61 2b 6e 2c  EFAULT, pData+n,
2b66d 20 69 44 75 6d 6d 79 2c 20 4e 55 4c 4c 29 3b 0a   iDummy, NULL);.
2b66e 20 20 20 20 70 44 61 74 61 20 2b 3d 20 6e 2b 69      pData += n+i
2b66f 44 75 6d 6d 79 3b 0a 20 20 20 20 6e 44 61 74 61  Dummy;.    nData
2b670 20 2d 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20   -= n+iDummy;.  
2b671 7d 0a 7d 0a 23 64 65 66 69 6e 65 20 41 53 53 45  }.}.#define ASSE
2b672 52 54 5f 56 41 4c 49 44 5f 4c 45 41 46 5f 4e 4f  RT_VALID_LEAF_NO
2b673 44 45 28 70 2c 20 6e 29 20 6c 65 61 66 4e 6f 64  DE(p, n) leafNod
2b674 65 56 61 6c 69 64 61 74 65 28 70 2c 20 6e 29 0a  eValidate(p, n).
2b675 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 41 53  #else.#define AS
2b676 53 45 52 54 5f 56 41 4c 49 44 5f 4c 45 41 46 5f  SERT_VALID_LEAF_
2b677 4e 4f 44 45 28 70 2c 20 6e 29 20 61 73 73 65 72  NODE(p, n) asser
2b678 74 28 20 31 20 29 0a 23 65 6e 64 69 66 0a 0a 2f  t( 1 ).#endif../
2b679 2a 20 46 6c 75 73 68 20 74 68 65 20 63 75 72 72  * Flush the curr
2b67a 65 6e 74 20 6c 65 61 66 20 6e 6f 64 65 20 74 6f  ent leaf node to
2b67b 20 25 5f 73 65 67 6d 65 6e 74 73 2c 20 61 6e 64   %_segments, and
2b67c 20 61 64 64 69 6e 67 20 74 68 65 20 72 65 73 75   adding the resu
2b67d 6c 74 69 6e 67 0a 2a 2a 20 62 6c 6f 63 6b 69 64  lting.** blockid
2b67e 20 61 6e 64 20 74 68 65 20 73 74 61 72 74 69 6e   and the startin
2b67f 67 20 74 65 72 6d 20 74 6f 20 74 68 65 20 69 6e  g term to the in
2b680 74 65 72 69 6f 72 20 6e 6f 64 65 20 77 68 69 63  terior node whic
2b681 68 20 77 69 6c 6c 0a 2a 2a 20 63 6f 6e 74 61 69  h will.** contai
2b682 6e 20 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  n it..*/.static 
2b683 69 6e 74 20 6c 65 61 66 57 72 69 74 65 72 49 6e  int leafWriterIn
2b684 74 65 72 6e 61 6c 46 6c 75 73 68 28 66 75 6c 6c  ternalFlush(full
2b685 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65  text_vtab *v, Le
2b686 61 66 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  afWriter *pWrite
2b687 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
2b688 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b689 20 20 20 20 20 20 69 6e 74 20 69 44 61 74 61 2c        int iData,
2b68a 20 69 6e 74 20 6e 44 61 74 61 29 7b 0a 20 20 73   int nData){.  s
2b68b 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 42 6c 6f  qlite_int64 iBlo
2b68c 63 6b 69 64 20 3d 20 30 3b 0a 20 20 63 6f 6e 73  ckid = 0;.  cons
2b68d 74 20 63 68 61 72 20 2a 70 53 74 61 72 74 69 6e  t char *pStartin
2b68e 67 54 65 72 6d 3b 0a 20 20 69 6e 74 20 6e 53 74  gTerm;.  int nSt
2b68f 61 72 74 69 6e 67 54 65 72 6d 2c 20 72 63 2c 20  artingTerm, rc, 
2b690 6e 3b 0a 0a 20 20 2f 2a 20 4d 75 73 74 20 68 61  n;..  /* Must ha
2b691 76 65 20 74 68 65 20 6c 65 61 64 69 6e 67 20 76  ve the leading v
2b692 61 72 69 6e 74 28 30 29 20 66 6c 61 67 2c 20 70  arint(0) flag, p
2b693 6c 75 73 20 61 74 20 6c 65 61 73 74 20 73 6f 6d  lus at least som
2b694 65 0a 20 20 2a 2a 20 76 61 6c 69 64 2d 6c 6f 6f  e.  ** valid-loo
2b695 6b 69 6e 67 20 64 61 74 61 2e 0a 20 20 2a 2f 0a  king data..  */.
2b696 20 20 61 73 73 65 72 74 28 20 6e 44 61 74 61 3e    assert( nData>
2b697 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  2 );.  assert( i
2b698 44 61 74 61 3e 3d 30 20 29 3b 0a 20 20 61 73 73  Data>=0 );.  ass
2b699 65 72 74 28 20 69 44 61 74 61 2b 6e 44 61 74 61  ert( iData+nData
2b69a 3c 3d 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e  <=pWriter->data.
2b69b 6e 44 61 74 61 20 29 3b 0a 20 20 41 53 53 45 52  nData );.  ASSER
2b69c 54 5f 56 41 4c 49 44 5f 4c 45 41 46 5f 4e 4f 44  T_VALID_LEAF_NOD
2b69d 45 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e  E(pWriter->data.
2b69e 70 44 61 74 61 2b 69 44 61 74 61 2c 20 6e 44 61  pData+iData, nDa
2b69f 74 61 29 3b 0a 0a 20 20 72 63 20 3d 20 62 6c 6f  ta);..  rc = blo
2b6a0 63 6b 5f 69 6e 73 65 72 74 28 76 2c 20 70 57 72  ck_insert(v, pWr
2b6a1 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61  iter->data.pData
2b6a2 2b 69 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 26  +iData, nData, &
2b6a3 69 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28  iBlockid);.  if(
2b6a4 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2b6a5 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 61 73   return rc;.  as
2b6a6 73 65 72 74 28 20 69 42 6c 6f 63 6b 69 64 21 3d  sert( iBlockid!=
2b6a7 30 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 63 6f 6e  0 );..  /* Recon
2b6a8 73 74 72 75 63 74 20 74 68 65 20 66 69 72 73 74  struct the first
2b6a9 20 74 65 72 6d 20 69 6e 20 74 68 65 20 6c 65 61   term in the lea
2b6aa 66 20 66 6f 72 20 70 75 72 70 6f 73 65 73 20 6f  f for purposes o
2b6ab 66 20 62 75 69 6c 64 69 6e 67 0a 20 20 2a 2a 20  f building.  ** 
2b6ac 74 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  the interior nod
2b6ad 65 2e 0a 20 20 2a 2f 0a 20 20 6e 20 3d 20 66 74  e..  */.  n = ft
2b6ae 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 57  s3GetVarint32(pW
2b6af 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74  riter->data.pDat
2b6b0 61 2b 69 44 61 74 61 2b 31 2c 20 26 6e 53 74 61  a+iData+1, &nSta
2b6b1 72 74 69 6e 67 54 65 72 6d 29 3b 0a 20 20 70 53  rtingTerm);.  pS
2b6b2 74 61 72 74 69 6e 67 54 65 72 6d 20 3d 20 70 57  tartingTerm = pW
2b6b3 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74  riter->data.pDat
2b6b4 61 2b 69 44 61 74 61 2b 31 2b 6e 3b 0a 20 20 61  a+iData+1+n;.  a
2b6b5 73 73 65 72 74 28 20 70 57 72 69 74 65 72 2d 3e  ssert( pWriter->
2b6b6 64 61 74 61 2e 6e 44 61 74 61 3e 69 44 61 74 61  data.nData>iData
2b6b7 2b 31 2b 6e 2b 6e 53 74 61 72 74 69 6e 67 54 65  +1+n+nStartingTe
2b6b8 72 6d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  rm );.  assert( 
2b6b9 70 57 72 69 74 65 72 2d 3e 6e 54 65 72 6d 44 69  pWriter->nTermDi
2b6ba 73 74 69 6e 63 74 3e 30 20 29 3b 0a 20 20 61 73  stinct>0 );.  as
2b6bb 73 65 72 74 28 20 70 57 72 69 74 65 72 2d 3e 6e  sert( pWriter->n
2b6bc 54 65 72 6d 44 69 73 74 69 6e 63 74 3c 3d 6e 53  TermDistinct<=nS
2b6bd 74 61 72 74 69 6e 67 54 65 72 6d 20 29 3b 0a 20  tartingTerm );. 
2b6be 20 6e 53 74 61 72 74 69 6e 67 54 65 72 6d 20 3d   nStartingTerm =
2b6bf 20 70 57 72 69 74 65 72 2d 3e 6e 54 65 72 6d 44   pWriter->nTermD
2b6c0 69 73 74 69 6e 63 74 3b 0a 0a 20 20 69 66 28 20  istinct;..  if( 
2b6c1 70 57 72 69 74 65 72 2d 3e 68 61 73 5f 70 61 72  pWriter->has_par
2b6c2 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 65 72  ent ){.    inter
2b6c3 69 6f 72 57 72 69 74 65 72 41 70 70 65 6e 64 28  iorWriterAppend(
2b6c4 26 70 57 72 69 74 65 72 2d 3e 70 61 72 65 6e 74  &pWriter->parent
2b6c5 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20  Writer,.        
2b6c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b6c7 20 70 53 74 61 72 74 69 6e 67 54 65 72 6d 2c 20   pStartingTerm, 
2b6c8 6e 53 74 61 72 74 69 6e 67 54 65 72 6d 2c 20 69  nStartingTerm, i
2b6c9 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 7d 65 6c 73  Blockid);.  }els
2b6ca 65 7b 0a 20 20 20 20 69 6e 74 65 72 69 6f 72 57  e{.    interiorW
2b6cb 72 69 74 65 72 49 6e 69 74 28 31 2c 20 70 53 74  riterInit(1, pSt
2b6cc 61 72 74 69 6e 67 54 65 72 6d 2c 20 6e 53 74 61  artingTerm, nSta
2b6cd 72 74 69 6e 67 54 65 72 6d 2c 20 69 42 6c 6f 63  rtingTerm, iBloc
2b6ce 6b 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20  kid,.           
2b6cf 20 20 20 20 20 20 20 20 20 20 20 20 26 70 57 72              &pWr
2b6d0 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69 74  iter->parentWrit
2b6d1 65 72 29 3b 0a 20 20 20 20 70 57 72 69 74 65 72  er);.    pWriter
2b6d2 2d 3e 68 61 73 5f 70 61 72 65 6e 74 20 3d 20 31  ->has_parent = 1
2b6d3 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 72 61 63  ;.  }..  /* Trac
2b6d4 6b 20 74 68 65 20 73 70 61 6e 20 6f 66 20 74 68  k the span of th
2b6d5 69 73 20 73 65 67 6d 65 6e 74 27 73 20 6c 65 61  is segment's lea
2b6d6 66 20 6e 6f 64 65 73 2e 20 2a 2f 0a 20 20 69 66  f nodes. */.  if
2b6d7 28 20 70 57 72 69 74 65 72 2d 3e 69 45 6e 64 42  ( pWriter->iEndB
2b6d8 6c 6f 63 6b 69 64 3d 3d 30 20 29 7b 0a 20 20 20  lockid==0 ){.   
2b6d9 20 70 57 72 69 74 65 72 2d 3e 69 45 6e 64 42 6c   pWriter->iEndBl
2b6da 6f 63 6b 69 64 20 3d 20 70 57 72 69 74 65 72 2d  ockid = pWriter-
2b6db 3e 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 20 3d  >iStartBlockid =
2b6dc 20 69 42 6c 6f 63 6b 69 64 3b 0a 20 20 7d 65 6c   iBlockid;.  }el
2b6dd 73 65 7b 0a 20 20 20 20 70 57 72 69 74 65 72 2d  se{.    pWriter-
2b6de 3e 69 45 6e 64 42 6c 6f 63 6b 69 64 2b 2b 3b 0a  >iEndBlockid++;.
2b6df 20 20 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f      assert( iBlo
2b6e0 63 6b 69 64 3d 3d 70 57 72 69 74 65 72 2d 3e 69  ckid==pWriter->i
2b6e1 45 6e 64 42 6c 6f 63 6b 69 64 20 29 3b 0a 20 20  EndBlockid );.  
2b6e2 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  }..  return SQLI
2b6e3 54 45 5f 4f 4b 3b 0a 7d 0a 73 74 61 74 69 63 20  TE_OK;.}.static 
2b6e4 69 6e 74 20 6c 65 61 66 57 72 69 74 65 72 46 6c  int leafWriterFl
2b6e5 75 73 68 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  ush(fulltext_vta
2b6e6 62 20 2a 76 2c 20 4c 65 61 66 57 72 69 74 65 72  b *v, LeafWriter
2b6e7 20 2a 70 57 72 69 74 65 72 29 7b 0a 20 20 69 6e   *pWriter){.  in
2b6e8 74 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65  t rc = leafWrite
2b6e9 72 49 6e 74 65 72 6e 61 6c 46 6c 75 73 68 28 76  rInternalFlush(v
2b6ea 2c 20 70 57 72 69 74 65 72 2c 20 30 2c 20 70 57  , pWriter, 0, pW
2b6eb 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
2b6ec 61 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  a);.  if( rc!=SQ
2b6ed 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
2b6ee 20 72 63 3b 0a 0a 20 20 2f 2a 20 52 65 2d 69 6e   rc;..  /* Re-in
2b6ef 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 75 74  itialize the out
2b6f0 70 75 74 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20  put buffer. */. 
2b6f1 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74   dataBufferReset
2b6f2 28 26 70 57 72 69 74 65 72 2d 3e 64 61 74 61 29  (&pWriter->data)
2b6f3 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ;..  return SQLI
2b6f4 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 46 65 74  TE_OK;.}../* Fet
2b6f5 63 68 20 74 68 65 20 72 6f 6f 74 20 69 6e 66 6f  ch the root info
2b6f6 20 66 6f 72 20 74 68 65 20 73 65 67 6d 65 6e 74   for the segment
2b6f7 2e 20 20 49 66 20 74 68 65 20 65 6e 74 69 72 65  .  If the entire
2b6f8 20 6c 65 61 66 20 66 69 74 73 0a 2a 2a 20 77 69   leaf fits.** wi
2b6f9 74 68 69 6e 20 52 4f 4f 54 5f 4d 41 58 2c 20 74  thin ROOT_MAX, t
2b6fa 68 65 6e 20 69 74 20 77 69 6c 6c 20 62 65 20 72  hen it will be r
2b6fb 65 74 75 72 6e 65 64 20 64 69 72 65 63 74 6c 79  eturned directly
2b6fc 2c 20 6f 74 68 65 72 77 69 73 65 20 69 74 0a 2a  , otherwise it.*
2b6fd 2a 20 77 69 6c 6c 20 62 65 20 66 6c 75 73 68 65  * will be flushe
2b6fe 64 20 61 6e 64 20 74 68 65 20 72 6f 6f 74 20 69  d and the root i
2b6ff 6e 66 6f 20 77 69 6c 6c 20 62 65 20 72 65 74 75  nfo will be retu
2b700 72 6e 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a 2a  rned from the.**
2b701 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2e 20   interior node. 
2b702 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64 20 69   *piEndBlockid i
2b703 73 20 73 65 74 20 74 6f 20 74 68 65 20 62 6c 6f  s set to the blo
2b704 63 6b 69 64 20 6f 66 20 74 68 65 20 6c 61 73 74  ckid of the last
2b705 0a 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6f 72 20  .** interior or 
2b706 6c 65 61 66 20 6e 6f 64 65 20 77 72 69 74 74 65  leaf node writte
2b707 6e 20 74 6f 20 64 69 73 6b 20 28 30 20 69 66 20  n to disk (0 if 
2b708 6e 6f 6e 65 20 61 72 65 20 77 72 69 74 74 65 6e  none are written
2b709 20 61 74 0a 2a 2a 20 61 6c 6c 29 2e 0a 2a 2f 0a   at.** all)..*/.
2b70a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57  static int leafW
2b70b 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 66 75  riterRootInfo(fu
2b70c 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
2b70d 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57 72 69  LeafWriter *pWri
2b70e 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ter,.           
2b70f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b710 20 20 20 63 68 61 72 20 2a 2a 70 70 52 6f 6f 74     char **ppRoot
2b711 49 6e 66 6f 2c 20 69 6e 74 20 2a 70 6e 52 6f 6f  Info, int *pnRoo
2b712 74 49 6e 66 6f 2c 0a 20 20 20 20 20 20 20 20 20  tInfo,.         
2b713 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b714 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
2b715 34 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64 29  4 *piEndBlockid)
2b716 7b 0a 20 20 2f 2a 20 77 65 20 63 61 6e 20 66 69  {.  /* we can fi
2b717 74 20 74 68 65 20 73 65 67 6d 65 6e 74 20 65 6e  t the segment en
2b718 74 69 72 65 6c 79 20 69 6e 6c 69 6e 65 20 2a 2f  tirely inline */
2b719 0a 20 20 69 66 28 20 21 70 57 72 69 74 65 72 2d  .  if( !pWriter-
2b71a 3e 68 61 73 5f 70 61 72 65 6e 74 20 26 26 20 70  >has_parent && p
2b71b 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61  Writer->data.nDa
2b71c 74 61 3c 52 4f 4f 54 5f 4d 41 58 20 29 7b 0a 20  ta<ROOT_MAX ){. 
2b71d 20 20 20 2a 70 70 52 6f 6f 74 49 6e 66 6f 20 3d     *ppRootInfo =
2b71e 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70   pWriter->data.p
2b71f 44 61 74 61 3b 0a 20 20 20 20 2a 70 6e 52 6f 6f  Data;.    *pnRoo
2b720 74 49 6e 66 6f 20 3d 20 70 57 72 69 74 65 72 2d  tInfo = pWriter-
2b721 3e 64 61 74 61 2e 6e 44 61 74 61 3b 0a 20 20 20  >data.nData;.   
2b722 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64 20 3d   *piEndBlockid =
2b723 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   0;.    return S
2b724 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20  QLITE_OK;.  }.. 
2b725 20 2f 2a 20 46 6c 75 73 68 20 72 65 6d 61 69 6e   /* Flush remain
2b726 69 6e 67 20 6c 65 61 66 20 64 61 74 61 2e 20 2a  ing leaf data. *
2b727 2f 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d  /.  if( pWriter-
2b728 3e 64 61 74 61 2e 6e 44 61 74 61 3e 30 20 29 7b  >data.nData>0 ){
2b729 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 6c 65  .    int rc = le
2b72a 61 66 57 72 69 74 65 72 46 6c 75 73 68 28 76 2c  afWriterFlush(v,
2b72b 20 70 57 72 69 74 65 72 29 3b 0a 20 20 20 20 69   pWriter);.    i
2b72c 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2b72d 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
2b72e 7d 0a 0a 20 20 2f 2a 20 57 65 20 6d 75 73 74 20  }..  /* We must 
2b72f 68 61 76 65 20 66 6c 75 73 68 65 64 20 61 20 6c  have flushed a l
2b730 65 61 66 20 61 74 20 73 6f 6d 65 20 70 6f 69 6e  eaf at some poin
2b731 74 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  t. */.  assert( 
2b732 70 57 72 69 74 65 72 2d 3e 68 61 73 5f 70 61 72  pWriter->has_par
2b733 65 6e 74 20 29 3b 0a 0a 20 20 2f 2a 20 54 65 6e  ent );..  /* Ten
2b734 61 74 69 76 65 6c 79 20 73 65 74 20 74 68 65 20  atively set the 
2b735 65 6e 64 20 6c 65 61 66 20 62 6c 6f 63 6b 69 64  end leaf blockid
2b736 20 61 73 20 74 68 65 20 65 6e 64 20 62 6c 6f 63   as the end bloc
2b737 6b 69 64 2e 20 20 49 66 20 74 68 65 0a 20 20 2a  kid.  If the.  *
2b738 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20  * interior node 
2b739 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65 64 20  can be returned 
2b73a 69 6e 6c 69 6e 65 2c 20 74 68 69 73 20 77 69 6c  inline, this wil
2b73b 6c 20 62 65 20 74 68 65 20 66 69 6e 61 6c 0a 20  l be the final. 
2b73c 20 2a 2a 20 62 6c 6f 63 6b 69 64 2c 20 6f 74 68   ** blockid, oth
2b73d 65 72 77 69 73 65 20 69 74 20 77 69 6c 6c 20 62  erwise it will b
2b73e 65 20 6f 76 65 72 77 72 69 74 74 65 6e 20 62 79  e overwritten by
2b73f 0a 20 20 2a 2a 20 69 6e 74 65 72 69 6f 72 57 72  .  ** interiorWr
2b740 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 29 2e 0a  iterRootInfo()..
2b741 20 20 2a 2f 0a 20 20 2a 70 69 45 6e 64 42 6c 6f    */.  *piEndBlo
2b742 63 6b 69 64 20 3d 20 70 57 72 69 74 65 72 2d 3e  ckid = pWriter->
2b743 69 45 6e 64 42 6c 6f 63 6b 69 64 3b 0a 0a 20 20  iEndBlockid;..  
2b744 72 65 74 75 72 6e 20 69 6e 74 65 72 69 6f 72 57  return interiorW
2b745 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 76 2c  riterRootInfo(v,
2b746 20 26 70 57 72 69 74 65 72 2d 3e 70 61 72 65 6e   &pWriter->paren
2b747 74 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20  tWriter,.       
2b748 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b749 20 20 20 20 20 20 20 20 20 70 70 52 6f 6f 74 49           ppRootI
2b74a 6e 66 6f 2c 20 70 6e 52 6f 6f 74 49 6e 66 6f 2c  nfo, pnRootInfo,
2b74b 20 70 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a   piEndBlockid);.
2b74c 7d 0a 0a 2f 2a 20 43 6f 6c 6c 65 63 74 20 74 68  }../* Collect th
2b74d 65 20 72 6f 6f 74 49 6e 66 6f 20 64 61 74 61 20  e rootInfo data 
2b74e 61 6e 64 20 73 74 6f 72 65 20 69 74 20 69 6e 74  and store it int
2b74f 6f 20 74 68 65 20 73 65 67 6d 65 6e 74 20 64 69  o the segment di
2b750 72 65 63 74 6f 72 79 2e 0a 2a 2a 20 54 68 69 73  rectory..** This
2b751 20 68 61 73 20 74 68 65 20 65 66 66 65 63 74 20   has the effect 
2b752 6f 66 20 66 6c 75 73 68 69 6e 67 20 74 68 65 20  of flushing the 
2b753 73 65 67 6d 65 6e 74 27 73 20 6c 65 61 66 20 64  segment's leaf d
2b754 61 74 61 20 74 6f 0a 2a 2a 20 25 5f 73 65 67 6d  ata to.** %_segm
2b755 65 6e 74 73 2c 20 61 6e 64 20 61 6c 73 6f 20 66  ents, and also f
2b756 6c 75 73 68 69 6e 67 20 61 6e 79 20 69 6e 74 65  lushing any inte
2b757 72 69 6f 72 20 6e 6f 64 65 73 20 74 6f 20 25 5f  rior nodes to %_
2b758 73 65 67 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61  segments..*/.sta
2b759 74 69 63 20 69 6e 74 20 6c 65 61 66 57 72 69 74  tic int leafWrit
2b75a 65 72 46 69 6e 61 6c 69 7a 65 28 66 75 6c 6c 74  erFinalize(fullt
2b75b 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61  ext_vtab *v, Lea
2b75c 66 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  fWriter *pWriter
2b75d 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  ){.  sqlite_int6
2b75e 34 20 69 45 6e 64 42 6c 6f 63 6b 69 64 3b 0a 20  4 iEndBlockid;. 
2b75f 20 63 68 61 72 20 2a 70 52 6f 6f 74 49 6e 66 6f   char *pRootInfo
2b760 3b 0a 20 20 69 6e 74 20 72 63 2c 20 6e 52 6f 6f  ;.  int rc, nRoo
2b761 74 49 6e 66 6f 3b 0a 0a 20 20 72 63 20 3d 20 6c  tInfo;..  rc = l
2b762 65 61 66 57 72 69 74 65 72 52 6f 6f 74 49 6e 66  eafWriterRootInf
2b763 6f 28 76 2c 20 70 57 72 69 74 65 72 2c 20 26 70  o(v, pWriter, &p
2b764 52 6f 6f 74 49 6e 66 6f 2c 20 26 6e 52 6f 6f 74  RootInfo, &nRoot
2b765 49 6e 66 6f 2c 20 26 69 45 6e 64 42 6c 6f 63 6b  Info, &iEndBlock
2b766 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  id);.  if( rc!=S
2b767 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
2b768 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f 6e 27  n rc;..  /* Don'
2b769 74 20 62 6f 74 68 65 72 20 73 74 6f 72 69 6e 67  t bother storing
2b76a 20 61 6e 20 65 6e 74 69 72 65 6c 79 20 65 6d 70   an entirely emp
2b76b 74 79 20 73 65 67 6d 65 6e 74 2e 20 2a 2f 0a 20  ty segment. */. 
2b76c 20 69 66 28 20 69 45 6e 64 42 6c 6f 63 6b 69 64   if( iEndBlockid
2b76d 3d 3d 30 20 26 26 20 6e 52 6f 6f 74 49 6e 66 6f  ==0 && nRootInfo
2b76e 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ==0 ) return SQL
2b76f 49 54 45 5f 4f 4b 3b 0a 0a 20 20 72 65 74 75 72  ITE_OK;..  retur
2b770 6e 20 73 65 67 64 69 72 5f 73 65 74 28 76 2c 20  n segdir_set(v, 
2b771 70 57 72 69 74 65 72 2d 3e 69 4c 65 76 65 6c 2c  pWriter->iLevel,
2b772 20 70 57 72 69 74 65 72 2d 3e 69 64 78 2c 0a 20   pWriter->idx,. 
2b773 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b774 20 20 20 70 57 72 69 74 65 72 2d 3e 69 53 74 61     pWriter->iSta
2b775 72 74 42 6c 6f 63 6b 69 64 2c 20 70 57 72 69 74  rtBlockid, pWrit
2b776 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69 64 2c  er->iEndBlockid,
2b777 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2b778 20 20 20 20 20 69 45 6e 64 42 6c 6f 63 6b 69 64       iEndBlockid
2b779 2c 20 70 52 6f 6f 74 49 6e 66 6f 2c 20 6e 52 6f  , pRootInfo, nRo
2b77a 6f 74 49 6e 66 6f 29 3b 0a 7d 0a 0a 73 74 61 74  otInfo);.}..stat
2b77b 69 63 20 76 6f 69 64 20 6c 65 61 66 57 72 69 74  ic void leafWrit
2b77c 65 72 44 65 73 74 72 6f 79 28 4c 65 61 66 57 72  erDestroy(LeafWr
2b77d 69 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a  iter *pWriter){.
2b77e 20 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e 68    if( pWriter->h
2b77f 61 73 5f 70 61 72 65 6e 74 20 29 20 69 6e 74 65  as_parent ) inte
2b780 72 69 6f 72 57 72 69 74 65 72 44 65 73 74 72 6f  riorWriterDestro
2b781 79 28 26 70 57 72 69 74 65 72 2d 3e 70 61 72 65  y(&pWriter->pare
2b782 6e 74 57 72 69 74 65 72 29 3b 0a 20 20 64 61 74  ntWriter);.  dat
2b783 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  aBufferDestroy(&
2b784 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 29 3b 0a  pWriter->term);.
2b785 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74    dataBufferDest
2b786 72 6f 79 28 26 70 57 72 69 74 65 72 2d 3e 64 61  roy(&pWriter->da
2b787 74 61 29 3b 0a 7d 0a 0a 2f 2a 20 45 6e 63 6f 64  ta);.}../* Encod
2b788 65 20 61 20 74 65 72 6d 20 69 6e 74 6f 20 74 68  e a term into th
2b789 65 20 6c 65 61 66 57 72 69 74 65 72 2c 20 64 65  e leafWriter, de
2b78a 6c 74 61 2d 65 6e 63 6f 64 69 6e 67 20 61 73 20  lta-encoding as 
2b78b 61 70 70 72 6f 70 72 69 61 74 65 2e 0a 2a 2a 20  appropriate..** 
2b78c 52 65 74 75 72 6e 73 20 74 68 65 20 6c 65 6e 67  Returns the leng
2b78d 74 68 20 6f 66 20 74 68 65 20 6e 65 77 20 74 65  th of the new te
2b78e 72 6d 20 77 68 69 63 68 20 64 69 73 74 69 6e 67  rm which disting
2b78f 75 69 73 68 65 73 20 69 74 20 66 72 6f 6d 20 74  uishes it from t
2b790 68 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73 20 74  he.** previous t
2b791 65 72 6d 2c 20 77 68 69 63 68 20 63 61 6e 20 62  erm, which can b
2b792 65 20 75 73 65 64 20 74 6f 20 73 65 74 20 6e 54  e used to set nT
2b793 65 72 6d 44 69 73 74 69 6e 63 74 20 77 68 65 6e  ermDistinct when
2b794 20 61 20 6e 6f 64 65 0a 2a 2a 20 62 6f 75 6e 64   a node.** bound
2b795 61 72 79 20 69 73 20 63 72 6f 73 73 65 64 2e 0a  ary is crossed..
2b796 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65  */.static int le
2b797 61 66 57 72 69 74 65 72 45 6e 63 6f 64 65 54 65  afWriterEncodeTe
2b798 72 6d 28 4c 65 61 66 57 72 69 74 65 72 20 2a 70  rm(LeafWriter *p
2b799 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20  Writer,.        
2b79a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b79b 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
2b79c 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e  ar *pTerm, int n
2b79d 54 65 72 6d 29 7b 0a 20 20 63 68 61 72 20 63 5b  Term){.  char c[
2b79e 56 41 52 49 4e 54 5f 4d 41 58 2b 56 41 52 49 4e  VARINT_MAX+VARIN
2b79f 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20 6e 2c  T_MAX];.  int n,
2b7a0 20 6e 50 72 65 66 69 78 20 3d 20 30 3b 0a 0a 20   nPrefix = 0;.. 
2b7a1 20 61 73 73 65 72 74 28 20 6e 54 65 72 6d 3e 30   assert( nTerm>0
2b7a2 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e 50 72   );.  while( nPr
2b7a3 65 66 69 78 3c 70 57 72 69 74 65 72 2d 3e 74 65  efix<pWriter->te
2b7a4 72 6d 2e 6e 44 61 74 61 20 26 26 0a 20 20 20 20  rm.nData &&.    
2b7a5 20 20 20 20 20 70 54 65 72 6d 5b 6e 50 72 65 66       pTerm[nPref
2b7a6 69 78 5d 3d 3d 70 57 72 69 74 65 72 2d 3e 74 65  ix]==pWriter->te
2b7a7 72 6d 2e 70 44 61 74 61 5b 6e 50 72 65 66 69 78  rm.pData[nPrefix
2b7a8 5d 20 29 7b 0a 20 20 20 20 6e 50 72 65 66 69 78  ] ){.    nPrefix
2b7a9 2b 2b 3b 0a 20 20 20 20 2f 2a 20 46 61 69 6c 69  ++;.    /* Faili
2b7aa 6e 67 20 74 68 69 73 20 69 6d 70 6c 69 65 73 20  ng this implies 
2b7ab 74 68 61 74 20 74 68 65 20 74 65 72 6d 73 20 77  that the terms w
2b7ac 65 72 65 6e 27 74 20 69 6e 20 6f 72 64 65 72 2e  eren't in order.
2b7ad 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
2b7ae 6e 50 72 65 66 69 78 3c 6e 54 65 72 6d 20 29 3b  nPrefix<nTerm );
2b7af 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 57 72 69  .  }..  if( pWri
2b7b0 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3d  ter->data.nData=
2b7b1 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 45 6e 63  =0 ){.    /* Enc
2b7b2 6f 64 65 20 74 68 65 20 6e 6f 64 65 20 68 65 61  ode the node hea
2b7b3 64 65 72 20 61 6e 64 20 6c 65 61 64 69 6e 67 20  der and leading 
2b7b4 74 65 72 6d 20 61 73 3a 0a 20 20 20 20 2a 2a 20  term as:.    ** 
2b7b5 20 76 61 72 69 6e 74 28 30 29 0a 20 20 20 20 2a   varint(0).    *
2b7b6 2a 20 20 76 61 72 69 6e 74 28 6e 54 65 72 6d 29  *  varint(nTerm)
2b7b7 0a 20 20 20 20 2a 2a 20 20 63 68 61 72 20 70 54  .    **  char pT
2b7b8 65 72 6d 5b 6e 54 65 72 6d 5d 0a 20 20 20 20 2a  erm[nTerm].    *
2b7b9 2f 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 50 75  /.    n = fts3Pu
2b7ba 74 56 61 72 69 6e 74 28 63 2c 20 27 5c 30 27 29  tVarint(c, '\0')
2b7bb 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 50  ;.    n += fts3P
2b7bc 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 6e 54  utVarint(c+n, nT
2b7bd 65 72 6d 29 3b 0a 20 20 20 20 64 61 74 61 42 75  erm);.    dataBu
2b7be 66 66 65 72 41 70 70 65 6e 64 32 28 26 70 57 72  fferAppend2(&pWr
2b7bf 69 74 65 72 2d 3e 64 61 74 61 2c 20 63 2c 20 6e  iter->data, c, n
2b7c0 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b  , pTerm, nTerm);
2b7c1 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
2b7c2 20 44 65 6c 74 61 2d 65 6e 63 6f 64 65 20 74 68   Delta-encode th
2b7c3 65 20 74 65 72 6d 20 61 73 3a 0a 20 20 20 20 2a  e term as:.    *
2b7c4 2a 20 20 76 61 72 69 6e 74 28 6e 50 72 65 66 69  *  varint(nPrefi
2b7c5 78 29 0a 20 20 20 20 2a 2a 20 20 76 61 72 69 6e  x).    **  varin
2b7c6 74 28 6e 53 75 66 66 69 78 29 0a 20 20 20 20 2a  t(nSuffix).    *
2b7c7 2a 20 20 63 68 61 72 20 70 54 65 72 6d 53 75 66  *  char pTermSuf
2b7c8 66 69 78 5b 6e 53 75 66 66 69 78 5d 0a 20 20 20  fix[nSuffix].   
2b7c9 20 2a 2f 0a 20 20 20 20 6e 20 3d 20 66 74 73 33   */.    n = fts3
2b7ca 50 75 74 56 61 72 69 6e 74 28 63 2c 20 6e 50 72  PutVarint(c, nPr
2b7cb 65 66 69 78 29 3b 0a 20 20 20 20 6e 20 2b 3d 20  efix);.    n += 
2b7cc 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b  fts3PutVarint(c+
2b7cd 6e 2c 20 6e 54 65 72 6d 2d 6e 50 72 65 66 69 78  n, nTerm-nPrefix
2b7ce 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65  );.    dataBuffe
2b7cf 72 41 70 70 65 6e 64 32 28 26 70 57 72 69 74 65  rAppend2(&pWrite
2b7d0 72 2d 3e 64 61 74 61 2c 20 63 2c 20 6e 2c 20 70  r->data, c, n, p
2b7d1 54 65 72 6d 2b 6e 50 72 65 66 69 78 2c 20 6e 54  Term+nPrefix, nT
2b7d2 65 72 6d 2d 6e 50 72 65 66 69 78 29 3b 0a 20 20  erm-nPrefix);.  
2b7d3 7d 0a 20 20 64 61 74 61 42 75 66 66 65 72 52 65  }.  dataBufferRe
2b7d4 70 6c 61 63 65 28 26 70 57 72 69 74 65 72 2d 3e  place(&pWriter->
2b7d5 74 65 72 6d 2c 20 70 54 65 72 6d 2c 20 6e 54 65  term, pTerm, nTe
2b7d6 72 6d 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6e  rm);..  return n
2b7d7 50 72 65 66 69 78 2b 31 3b 0a 7d 0a 0a 2f 2a 20  Prefix+1;.}../* 
2b7d8 55 73 65 64 20 74 6f 20 61 76 6f 69 64 20 61 20  Used to avoid a 
2b7d9 6d 65 6d 6d 6f 76 65 20 77 68 65 6e 20 61 20 6c  memmove when a l
2b7da 61 72 67 65 20 61 6d 6f 75 6e 74 20 6f 66 20 64  arge amount of d
2b7db 6f 63 6c 69 73 74 20 64 61 74 61 20 69 73 20 69  oclist data is i
2b7dc 6e 0a 2a 2a 20 74 68 65 20 62 75 66 66 65 72 2e  n.** the buffer.
2b7dd 20 20 54 68 69 73 20 63 6f 6e 73 74 72 75 63 74    This construct
2b7de 73 20 61 20 6e 6f 64 65 20 61 6e 64 20 74 65 72  s a node and ter
2b7df 6d 20 68 65 61 64 65 72 20 62 65 66 6f 72 65 0a  m header before.
2b7e0 2a 2a 20 69 44 6f 63 6c 69 73 74 44 61 74 61 20  ** iDoclistData 
2b7e1 61 6e 64 20 66 6c 75 73 68 65 73 20 74 68 65 20  and flushes the 
2b7e2 72 65 73 75 6c 74 69 6e 67 20 63 6f 6d 70 6c 65  resulting comple
2b7e3 74 65 20 6e 6f 64 65 20 75 73 69 6e 67 0a 2a 2a  te node using.**
2b7e4 20 6c 65 61 66 57 72 69 74 65 72 49 6e 74 65 72   leafWriterInter
2b7e5 6e 61 6c 46 6c 75 73 68 28 29 2e 0a 2a 2f 0a 73  nalFlush()..*/.s
2b7e6 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57 72  tatic int leafWr
2b7e7 69 74 65 72 49 6e 6c 69 6e 65 46 6c 75 73 68 28  iterInlineFlush(
2b7e8 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
2b7e9 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57  , LeafWriter *pW
2b7ea 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20  riter,.         
2b7eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b7ec 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
2b7ed 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e  ar *pTerm, int n
2b7ee 54 65 72 6d 2c 0a 20 20 20 20 20 20 20 20 20 20  Term,.          
2b7ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b7f0 20 20 20 20 20 20 20 69 6e 74 20 69 44 6f 63 6c         int iDocl
2b7f1 69 73 74 44 61 74 61 29 7b 0a 20 20 63 68 61 72  istData){.  char
2b7f2 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 2b 56 41   c[VARINT_MAX+VA
2b7f3 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74  RINT_MAX];.  int
2b7f4 20 69 44 61 74 61 2c 20 6e 20 3d 20 66 74 73 33   iData, n = fts3
2b7f5 50 75 74 56 61 72 69 6e 74 28 63 2c 20 30 29 3b  PutVarint(c, 0);
2b7f6 0a 20 20 6e 20 2b 3d 20 66 74 73 33 50 75 74 56  .  n += fts3PutV
2b7f7 61 72 69 6e 74 28 63 2b 6e 2c 20 6e 54 65 72 6d  arint(c+n, nTerm
2b7f8 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 72 65 20 73  );..  /* There s
2b7f9 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65 20  hould always be 
2b7fa 72 6f 6f 6d 20 66 6f 72 20 74 68 65 20 68 65 61  room for the hea
2b7fb 64 65 72 2e 20 20 45 76 65 6e 20 69 66 20 70 54  der.  Even if pT
2b7fc 65 72 6d 20 73 68 61 72 65 64 0a 20 20 2a 2a 20  erm shared.  ** 
2b7fd 61 20 73 75 62 73 74 61 6e 74 69 61 6c 20 70 72  a substantial pr
2b7fe 65 66 69 78 20 77 69 74 68 20 74 68 65 20 70 72  efix with the pr
2b7ff 65 76 69 6f 75 73 20 74 65 72 6d 2c 20 74 68 65  evious term, the
2b800 20 65 6e 74 69 72 65 20 70 72 65 66 69 78 0a 20   entire prefix. 
2b801 20 2a 2a 20 63 6f 75 6c 64 20 62 65 20 63 6f 6e   ** could be con
2b802 73 74 72 75 63 74 65 64 20 66 72 6f 6d 20 65 61  structed from ea
2b803 72 6c 69 65 72 20 64 61 74 61 20 69 6e 20 74 68  rlier data in th
2b804 65 20 64 6f 63 6c 69 73 74 2c 20 73 6f 20 74 68  e doclist, so th
2b805 65 72 65 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20  ere.  ** should 
2b806 62 65 20 72 6f 6f 6d 2e 0a 20 20 2a 2f 0a 20 20  be room..  */.  
2b807 61 73 73 65 72 74 28 20 69 44 6f 63 6c 69 73 74  assert( iDoclist
2b808 44 61 74 61 3e 3d 6e 2b 6e 54 65 72 6d 20 29 3b  Data>=n+nTerm );
2b809 0a 0a 20 20 69 44 61 74 61 20 3d 20 69 44 6f 63  ..  iData = iDoc
2b80a 6c 69 73 74 44 61 74 61 2d 28 6e 2b 6e 54 65 72  listData-(n+nTer
2b80b 6d 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 57 72  m);.  memcpy(pWr
2b80c 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61  iter->data.pData
2b80d 2b 69 44 61 74 61 2c 20 63 2c 20 6e 29 3b 0a 20  +iData, c, n);. 
2b80e 20 6d 65 6d 63 70 79 28 70 57 72 69 74 65 72 2d   memcpy(pWriter-
2b80f 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 61 74  >data.pData+iDat
2b810 61 2b 6e 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72  a+n, pTerm, nTer
2b811 6d 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6c 65  m);..  return le
2b812 61 66 57 72 69 74 65 72 49 6e 74 65 72 6e 61 6c  afWriterInternal
2b813 46 6c 75 73 68 28 76 2c 20 70 57 72 69 74 65 72  Flush(v, pWriter
2b814 2c 20 69 44 61 74 61 2c 20 70 57 72 69 74 65 72  , iData, pWriter
2b815 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2d 69 44 61  ->data.nData-iDa
2b816 74 61 29 3b 0a 7d 0a 0a 2f 2a 20 50 75 73 68 20  ta);.}../* Push 
2b817 70 54 65 72 6d 5b 6e 54 65 72 6d 5d 20 61 6c 6f  pTerm[nTerm] alo
2b818 6e 67 20 77 69 74 68 20 74 68 65 20 64 6f 63 6c  ng with the docl
2b819 69 73 74 20 64 61 74 61 20 74 6f 20 74 68 65 20  ist data to the 
2b81a 6c 65 61 66 20 6c 61 79 65 72 20 6f 66 0a 2a 2a  leaf layer of.**
2b81b 20 25 5f 73 65 67 6d 65 6e 74 73 2e 0a 2a 2f 0a   %_segments..*/.
2b81c 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57  static int leafW
2b81d 72 69 74 65 72 53 74 65 70 4d 65 72 67 65 28 66  riterStepMerge(f
2b81e 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
2b81f 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57 72   LeafWriter *pWr
2b820 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20  iter,.          
2b821 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b822 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
2b823 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72  *pTerm, int nTer
2b824 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  m,.             
2b825 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b826 20 20 44 4c 52 65 61 64 65 72 20 2a 70 52 65 61    DLReader *pRea
2b827 64 65 72 73 2c 20 69 6e 74 20 6e 52 65 61 64 65  ders, int nReade
2b828 72 73 29 7b 0a 20 20 63 68 61 72 20 63 5b 56 41  rs){.  char c[VA
2b829 52 49 4e 54 5f 4d 41 58 2b 56 41 52 49 4e 54 5f  RINT_MAX+VARINT_
2b82a 4d 41 58 5d 3b 0a 20 20 69 6e 74 20 69 54 65 72  MAX];.  int iTer
2b82b 6d 44 61 74 61 20 3d 20 70 57 72 69 74 65 72 2d  mData = pWriter-
2b82c 3e 64 61 74 61 2e 6e 44 61 74 61 2c 20 69 44 6f  >data.nData, iDo
2b82d 63 6c 69 73 74 44 61 74 61 3b 0a 20 20 69 6e 74  clistData;.  int
2b82e 20 69 2c 20 6e 44 61 74 61 2c 20 6e 2c 20 6e 41   i, nData, n, nA
2b82f 63 74 75 61 6c 44 61 74 61 2c 20 6e 41 63 74 75  ctualData, nActu
2b830 61 6c 2c 20 72 63 2c 20 6e 54 65 72 6d 44 69 73  al, rc, nTermDis
2b831 74 69 6e 63 74 3b 0a 0a 20 20 41 53 53 45 52 54  tinct;..  ASSERT
2b832 5f 56 41 4c 49 44 5f 4c 45 41 46 5f 4e 4f 44 45  _VALID_LEAF_NODE
2b833 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70  (pWriter->data.p
2b834 44 61 74 61 2c 20 70 57 72 69 74 65 72 2d 3e 64  Data, pWriter->d
2b835 61 74 61 2e 6e 44 61 74 61 29 3b 0a 20 20 6e 54  ata.nData);.  nT
2b836 65 72 6d 44 69 73 74 69 6e 63 74 20 3d 20 6c 65  ermDistinct = le
2b837 61 66 57 72 69 74 65 72 45 6e 63 6f 64 65 54 65  afWriterEncodeTe
2b838 72 6d 28 70 57 72 69 74 65 72 2c 20 70 54 65 72  rm(pWriter, pTer
2b839 6d 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20 2f 2a  m, nTerm);..  /*
2b83a 20 52 65 6d 65 6d 62 65 72 20 6e 54 65 72 6d 44   Remember nTermD
2b83b 69 73 74 69 6e 63 74 20 69 66 20 6f 70 65 6e 69  istinct if openi
2b83c 6e 67 20 61 20 6e 65 77 20 6e 6f 64 65 2e 20 2a  ng a new node. *
2b83d 2f 0a 20 20 69 66 28 20 69 54 65 72 6d 44 61 74  /.  if( iTermDat
2b83e 61 3d 3d 30 20 29 20 70 57 72 69 74 65 72 2d 3e  a==0 ) pWriter->
2b83f 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 20 3d 20  nTermDistinct = 
2b840 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3b 0a 0a  nTermDistinct;..
2b841 20 20 69 44 6f 63 6c 69 73 74 44 61 74 61 20 3d    iDoclistData =
2b842 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e   pWriter->data.n
2b843 44 61 74 61 3b 0a 0a 20 20 2f 2a 20 45 73 74 69  Data;..  /* Esti
2b844 6d 61 74 65 20 74 68 65 20 6c 65 6e 67 74 68 20  mate the length 
2b845 6f 66 20 74 68 65 20 6d 65 72 67 65 64 20 64 6f  of the merged do
2b846 63 6c 69 73 74 20 73 6f 20 77 65 20 63 61 6e 20  clist so we can 
2b847 6c 65 61 76 65 20 73 70 61 63 65 0a 20 20 2a 2a  leave space.  **
2b848 20 74 6f 20 65 6e 63 6f 64 65 20 69 74 2e 0a 20   to encode it.. 
2b849 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6e   */.  for(i=0, n
2b84a 44 61 74 61 3d 30 3b 20 69 3c 6e 52 65 61 64 65  Data=0; i<nReade
2b84b 72 73 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 44  rs; i++){.    nD
2b84c 61 74 61 20 2b 3d 20 64 6c 72 41 6c 6c 44 61 74  ata += dlrAllDat
2b84d 61 42 79 74 65 73 28 26 70 52 65 61 64 65 72 73  aBytes(&pReaders
2b84e 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20  [i]);.  }.  n = 
2b84f 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c  fts3PutVarint(c,
2b850 20 6e 44 61 74 61 29 3b 0a 20 20 64 61 74 61 42   nData);.  dataB
2b851 75 66 66 65 72 41 70 70 65 6e 64 28 26 70 57 72  ufferAppend(&pWr
2b852 69 74 65 72 2d 3e 64 61 74 61 2c 20 63 2c 20 6e  iter->data, c, n
2b853 29 3b 0a 0a 20 20 64 6f 63 4c 69 73 74 4d 65 72  );..  docListMer
2b854 67 65 28 26 70 57 72 69 74 65 72 2d 3e 64 61 74  ge(&pWriter->dat
2b855 61 2c 20 70 52 65 61 64 65 72 73 2c 20 6e 52 65  a, pReaders, nRe
2b856 61 64 65 72 73 29 3b 0a 20 20 41 53 53 45 52 54  aders);.  ASSERT
2b857 5f 56 41 4c 49 44 5f 44 4f 43 4c 49 53 54 28 44  _VALID_DOCLIST(D
2b858 4c 5f 44 45 46 41 55 4c 54 2c 0a 20 20 20 20 20  L_DEFAULT,.     
2b859 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b85a 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e    pWriter->data.
2b85b 70 44 61 74 61 2b 69 44 6f 63 6c 69 73 74 44 61  pData+iDoclistDa
2b85c 74 61 2b 6e 2c 0a 20 20 20 20 20 20 20 20 20 20  ta+n,.          
2b85d 20 20 20 20 20 20 20 20 20 20 20 20 20 70 57 72               pWr
2b85e 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61  iter->data.nData
2b85f 2d 69 44 6f 63 6c 69 73 74 44 61 74 61 2d 6e 2c  -iDoclistData-n,
2b860 20 4e 55 4c 4c 29 3b 0a 0a 20 20 2f 2a 20 54 68   NULL);..  /* Th
2b861 65 20 61 63 74 75 61 6c 20 61 6d 6f 75 6e 74 20  e actual amount 
2b862 6f 66 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20  of doclist data 
2b863 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 63 6f  at this point co
2b864 75 6c 64 20 62 65 20 73 6d 61 6c 6c 65 72 0a 20  uld be smaller. 
2b865 20 2a 2a 20 74 68 61 6e 20 74 68 65 20 6c 65 6e   ** than the len
2b866 67 74 68 20 77 65 20 65 6e 63 6f 64 65 64 2e 20  gth we encoded. 
2b867 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 74   Additionally, t
2b868 68 65 20 73 70 61 63 65 20 72 65 71 75 69 72 65  he space require
2b869 64 20 74 6f 0a 20 20 2a 2a 20 65 6e 63 6f 64 65  d to.  ** encode
2b86a 20 74 68 69 73 20 6c 65 6e 67 74 68 20 63 6f 75   this length cou
2b86b 6c 64 20 62 65 20 73 6d 61 6c 6c 65 72 2e 20 20  ld be smaller.  
2b86c 46 6f 72 20 73 6d 61 6c 6c 20 64 6f 63 6c 69 73  For small doclis
2b86d 74 73 2c 20 74 68 69 73 20 69 73 0a 20 20 2a 2a  ts, this is.  **
2b86e 20 6e 6f 74 20 61 20 62 69 67 20 64 65 61 6c 2c   not a big deal,
2b86f 20 77 65 20 63 61 6e 20 6a 75 73 74 20 75 73 65   we can just use
2b870 20 6d 65 6d 6d 6f 76 65 28 29 20 74 6f 20 61 64   memmove() to ad
2b871 6a 75 73 74 20 74 68 69 6e 67 73 2e 0a 20 20 2a  just things..  *
2b872 2f 0a 20 20 6e 41 63 74 75 61 6c 44 61 74 61 20  /.  nActualData 
2b873 3d 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e  = pWriter->data.
2b874 6e 44 61 74 61 2d 28 69 44 6f 63 6c 69 73 74 44  nData-(iDoclistD
2b875 61 74 61 2b 6e 29 3b 0a 20 20 6e 41 63 74 75 61  ata+n);.  nActua
2b876 6c 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e  l = fts3PutVarin
2b877 74 28 63 2c 20 6e 41 63 74 75 61 6c 44 61 74 61  t(c, nActualData
2b878 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 41 63  );.  assert( nAc
2b879 74 75 61 6c 44 61 74 61 3c 3d 6e 44 61 74 61 20  tualData<=nData 
2b87a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 41 63  );.  assert( nAc
2b87b 74 75 61 6c 3c 3d 6e 20 29 3b 0a 0a 20 20 2f 2a  tual<=n );..  /*
2b87c 20 49 66 20 74 68 65 20 6e 65 77 20 64 6f 63 6c   If the new docl
2b87d 69 73 74 20 69 73 20 62 69 67 20 65 6e 6f 75 67  ist is big enoug
2b87e 68 20 66 6f 72 20 66 6f 72 63 65 20 61 20 73 74  h for force a st
2b87f 61 6e 64 61 6c 6f 6e 65 20 6c 65 61 66 0a 20 20  andalone leaf.  
2b880 2a 2a 20 6e 6f 64 65 2c 20 77 65 20 63 61 6e 20  ** node, we can 
2b881 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6c 75 73  immediately flus
2b882 68 20 69 74 20 69 6e 6c 69 6e 65 20 77 69 74 68  h it inline with
2b883 6f 75 74 20 64 6f 69 6e 67 20 74 68 65 0a 20 20  out doing the.  
2b884 2a 2a 20 6d 65 6d 6d 6f 76 65 28 29 2e 0a 20 20  ** memmove()..  
2b885 2a 2f 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65  */.  /* TODO(she
2b886 73 73 29 20 54 68 69 73 20 74 65 73 74 20 6d 61  ss) This test ma
2b887 74 63 68 65 73 20 6c 65 61 66 57 72 69 74 65 72  tches leafWriter
2b888 53 74 65 70 28 29 2c 20 77 68 69 63 68 20 64 6f  Step(), which do
2b889 65 73 20 74 68 69 73 0a 20 20 2a 2a 20 74 65 73  es this.  ** tes
2b88a 74 20 62 65 66 6f 72 65 20 69 74 20 6b 6e 6f 77  t before it know
2b88b 73 20 74 68 65 20 63 6f 73 74 20 74 6f 20 76 61  s the cost to va
2b88c 72 69 6e 74 2d 65 6e 63 6f 64 65 20 74 68 65 20  rint-encode the 
2b88d 74 65 72 6d 20 61 6e 64 0a 20 20 2a 2a 20 64 6f  term and.  ** do
2b88e 63 6c 69 73 74 20 6c 65 6e 67 74 68 73 2e 20 20  clist lengths.  
2b88f 41 74 20 73 6f 6d 65 20 70 6f 69 6e 74 2c 20 63  At some point, c
2b890 68 61 6e 67 65 20 74 6f 0a 20 20 2a 2a 20 70 57  hange to.  ** pW
2b891 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
2b892 61 2d 69 54 65 72 6d 44 61 74 61 3e 53 54 41 4e  a-iTermData>STAN
2b893 44 41 4c 4f 4e 45 5f 4d 49 4e 2e 0a 20 20 2a 2f  DALONE_MIN..  */
2b894 0a 20 20 69 66 28 20 6e 54 65 72 6d 2b 6e 41 63  .  if( nTerm+nAc
2b895 74 75 61 6c 44 61 74 61 3e 53 54 41 4e 44 41 4c  tualData>STANDAL
2b896 4f 4e 45 5f 4d 49 4e 20 29 7b 0a 20 20 20 20 2f  ONE_MIN ){.    /
2b897 2a 20 50 75 73 68 20 6c 65 61 66 20 6e 6f 64 65  * Push leaf node
2b898 20 66 72 6f 6d 20 62 65 66 6f 72 65 20 74 68 69   from before thi
2b899 73 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 20 20 69  s term. */.    i
2b89a 66 28 20 69 54 65 72 6d 44 61 74 61 3e 30 20 29  f( iTermData>0 )
2b89b 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 65 61  {.      rc = lea
2b89c 66 57 72 69 74 65 72 49 6e 74 65 72 6e 61 6c 46  fWriterInternalF
2b89d 6c 75 73 68 28 76 2c 20 70 57 72 69 74 65 72 2c  lush(v, pWriter,
2b89e 20 30 2c 20 69 54 65 72 6d 44 61 74 61 29 3b 0a   0, iTermData);.
2b89f 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
2b8a0 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
2b8a1 20 72 63 3b 0a 0a 20 20 20 20 20 20 70 57 72 69   rc;..      pWri
2b8a2 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74 69 6e  ter->nTermDistin
2b8a3 63 74 20 3d 20 6e 54 65 72 6d 44 69 73 74 69 6e  ct = nTermDistin
2b8a4 63 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  ct;.    }..    /
2b8a5 2a 20 46 69 78 20 74 68 65 20 65 6e 63 6f 64 65  * Fix the encode
2b8a6 64 20 64 6f 63 6c 69 73 74 20 6c 65 6e 67 74 68  d doclist length
2b8a7 2e 20 2a 2f 0a 20 20 20 20 69 44 6f 63 6c 69 73  . */.    iDoclis
2b8a8 74 44 61 74 61 20 2b 3d 20 6e 20 2d 20 6e 41 63  tData += n - nAc
2b8a9 74 75 61 6c 3b 0a 20 20 20 20 6d 65 6d 63 70 79  tual;.    memcpy
2b8aa 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70  (pWriter->data.p
2b8ab 44 61 74 61 2b 69 44 6f 63 6c 69 73 74 44 61 74  Data+iDoclistDat
2b8ac 61 2c 20 63 2c 20 6e 41 63 74 75 61 6c 29 3b 0a  a, c, nActual);.
2b8ad 0a 20 20 20 20 2f 2a 20 50 75 73 68 20 74 68 65  .    /* Push the
2b8ae 20 73 74 61 6e 64 61 6c 6f 6e 65 20 6c 65 61 66   standalone leaf
2b8af 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20 20 20 72 63   node. */.    rc
2b8b0 20 3d 20 6c 65 61 66 57 72 69 74 65 72 49 6e 6c   = leafWriterInl
2b8b1 69 6e 65 46 6c 75 73 68 28 76 2c 20 70 57 72 69  ineFlush(v, pWri
2b8b2 74 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72  ter, pTerm, nTer
2b8b3 6d 2c 20 69 44 6f 63 6c 69 73 74 44 61 74 61 29  m, iDoclistData)
2b8b4 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
2b8b5 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
2b8b6 20 72 63 3b 0a 0a 20 20 20 20 2f 2a 20 4c 65 61   rc;..    /* Lea
2b8b7 76 65 20 74 68 65 20 6e 6f 64 65 20 65 6d 70 74  ve the node empt
2b8b8 79 2e 20 2a 2f 0a 20 20 20 20 64 61 74 61 42 75  y. */.    dataBu
2b8b9 66 66 65 72 52 65 73 65 74 28 26 70 57 72 69 74  fferReset(&pWrit
2b8ba 65 72 2d 3e 64 61 74 61 29 3b 0a 0a 20 20 20 20  er->data);..    
2b8bb 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
2b8bc 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69    /* At this poi
2b8bd 6e 74 2c 20 77 65 20 6b 6e 6f 77 20 74 68 61 74  nt, we know that
2b8be 20 74 68 65 20 64 6f 63 6c 69 73 74 20 77 61 73   the doclist was
2b8bf 20 73 6d 61 6c 6c 2c 20 73 6f 20 64 6f 20 74 68   small, so do th
2b8c0 65 0a 20 20 2a 2a 20 6d 65 6d 6d 6f 76 65 20 69  e.  ** memmove i
2b8c1 66 20 69 6e 64 69 63 61 74 65 64 2e 0a 20 20 2a  f indicated..  *
2b8c2 2f 0a 20 20 69 66 28 20 6e 41 63 74 75 61 6c 3c  /.  if( nActual<
2b8c3 6e 20 29 7b 0a 20 20 20 20 6d 65 6d 6d 6f 76 65  n ){.    memmove
2b8c4 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70  (pWriter->data.p
2b8c5 44 61 74 61 2b 69 44 6f 63 6c 69 73 74 44 61 74  Data+iDoclistDat
2b8c6 61 2b 6e 41 63 74 75 61 6c 2c 0a 20 20 20 20 20  a+nActual,.     
2b8c7 20 20 20 20 20 20 20 70 57 72 69 74 65 72 2d 3e         pWriter->
2b8c8 64 61 74 61 2e 70 44 61 74 61 2b 69 44 6f 63 6c  data.pData+iDocl
2b8c9 69 73 74 44 61 74 61 2b 6e 2c 0a 20 20 20 20 20  istData+n,.     
2b8ca 20 20 20 20 20 20 20 70 57 72 69 74 65 72 2d 3e         pWriter->
2b8cb 64 61 74 61 2e 6e 44 61 74 61 2d 28 69 44 6f 63  data.nData-(iDoc
2b8cc 6c 69 73 74 44 61 74 61 2b 6e 29 29 3b 0a 20 20  listData+n));.  
2b8cd 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e    pWriter->data.
2b8ce 6e 44 61 74 61 20 2d 3d 20 6e 2d 6e 41 63 74 75  nData -= n-nActu
2b8cf 61 6c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65  al;.  }..  /* Re
2b8d0 70 6c 61 63 65 20 77 72 69 74 74 65 6e 20 6c 65  place written le
2b8d1 6e 67 74 68 20 77 69 74 68 20 61 63 74 75 61 6c  ngth with actual
2b8d2 20 6c 65 6e 67 74 68 2e 20 2a 2f 0a 20 20 6d 65   length. */.  me
2b8d3 6d 63 70 79 28 70 57 72 69 74 65 72 2d 3e 64 61  mcpy(pWriter->da
2b8d4 74 61 2e 70 44 61 74 61 2b 69 44 6f 63 6c 69 73  ta.pData+iDoclis
2b8d5 74 44 61 74 61 2c 20 63 2c 20 6e 41 63 74 75 61  tData, c, nActua
2b8d6 6c 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  l);..  /* If the
2b8d7 20 6e 6f 64 65 20 69 73 20 74 6f 6f 20 6c 61 72   node is too lar
2b8d8 67 65 2c 20 62 72 65 61 6b 20 74 68 69 6e 67 73  ge, break things
2b8d9 20 75 70 2e 20 2a 2f 0a 20 20 2f 2a 20 54 4f 44   up. */.  /* TOD
2b8da 4f 28 73 68 65 73 73 29 20 54 68 69 73 20 74 65  O(shess) This te
2b8db 73 74 20 6d 61 74 63 68 65 73 20 6c 65 61 66 57  st matches leafW
2b8dc 72 69 74 65 72 53 74 65 70 28 29 2c 20 77 68 69  riterStep(), whi
2b8dd 63 68 20 64 6f 65 73 20 74 68 69 73 0a 20 20 2a  ch does this.  *
2b8de 2a 20 74 65 73 74 20 62 65 66 6f 72 65 20 69 74  * test before it
2b8df 20 6b 6e 6f 77 73 20 74 68 65 20 63 6f 73 74 20   knows the cost 
2b8e0 74 6f 20 76 61 72 69 6e 74 2d 65 6e 63 6f 64 65  to varint-encode
2b8e1 20 74 68 65 20 74 65 72 6d 20 61 6e 64 0a 20 20   the term and.  
2b8e2 2a 2a 20 64 6f 63 6c 69 73 74 20 6c 65 6e 67 74  ** doclist lengt
2b8e3 68 73 2e 20 20 41 74 20 73 6f 6d 65 20 70 6f 69  hs.  At some poi
2b8e4 6e 74 2c 20 63 68 61 6e 67 65 20 74 6f 0a 20 20  nt, change to.  
2b8e5 2a 2a 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61  ** pWriter->data
2b8e6 2e 6e 44 61 74 61 3e 4c 45 41 46 5f 4d 41 58 2e  .nData>LEAF_MAX.
2b8e7 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 54 65 72  .  */.  if( iTer
2b8e8 6d 44 61 74 61 2b 6e 54 65 72 6d 2b 6e 41 63 74  mData+nTerm+nAct
2b8e9 75 61 6c 44 61 74 61 3e 4c 45 41 46 5f 4d 41 58  ualData>LEAF_MAX
2b8ea 20 29 7b 0a 20 20 20 20 2f 2a 20 46 6c 75 73 68   ){.    /* Flush
2b8eb 20 6f 75 74 20 74 68 65 20 6c 65 61 64 69 6e 67   out the leading
2b8ec 20 64 61 74 61 20 61 73 20 61 20 6e 6f 64 65 20   data as a node 
2b8ed 2a 2f 0a 20 20 20 20 72 63 20 3d 20 6c 65 61 66  */.    rc = leaf
2b8ee 57 72 69 74 65 72 49 6e 74 65 72 6e 61 6c 46 6c  WriterInternalFl
2b8ef 75 73 68 28 76 2c 20 70 57 72 69 74 65 72 2c 20  ush(v, pWriter, 
2b8f0 30 2c 20 69 54 65 72 6d 44 61 74 61 29 3b 0a 20  0, iTermData);. 
2b8f1 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
2b8f2 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
2b8f3 3b 0a 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  ;..    pWriter->
2b8f4 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 20 3d 20  nTermDistinct = 
2b8f5 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3b 0a 0a  nTermDistinct;..
2b8f6 20 20 20 20 2f 2a 20 52 65 62 75 69 6c 64 20 68      /* Rebuild h
2b8f7 65 61 64 65 72 20 75 73 69 6e 67 20 74 68 65 20  eader using the 
2b8f8 63 75 72 72 65 6e 74 20 74 65 72 6d 20 2a 2f 0a  current term */.
2b8f9 20 20 20 20 6e 20 3d 20 66 74 73 33 50 75 74 56      n = fts3PutV
2b8fa 61 72 69 6e 74 28 70 57 72 69 74 65 72 2d 3e 64  arint(pWriter->d
2b8fb 61 74 61 2e 70 44 61 74 61 2c 20 30 29 3b 0a 20  ata.pData, 0);. 
2b8fc 20 20 20 6e 20 2b 3d 20 66 74 73 33 50 75 74 56     n += fts3PutV
2b8fd 61 72 69 6e 74 28 70 57 72 69 74 65 72 2d 3e 64  arint(pWriter->d
2b8fe 61 74 61 2e 70 44 61 74 61 2b 6e 2c 20 6e 54 65  ata.pData+n, nTe
2b8ff 72 6d 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  rm);.    memcpy(
2b900 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44  pWriter->data.pD
2b901 61 74 61 2b 6e 2c 20 70 54 65 72 6d 2c 20 6e 54  ata+n, pTerm, nT
2b902 65 72 6d 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 6e  erm);.    n += n
2b903 54 65 72 6d 3b 0a 0a 20 20 20 20 2f 2a 20 54 68  Term;..    /* Th
2b904 65 72 65 20 73 68 6f 75 6c 64 20 61 6c 77 61 79  ere should alway
2b905 73 20 62 65 20 72 6f 6f 6d 2c 20 62 65 63 61 75  s be room, becau
2b906 73 65 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  se the previous 
2b907 65 6e 63 6f 64 69 6e 67 0a 20 20 20 20 2a 2a 20  encoding.    ** 
2b908 69 6e 63 6c 75 64 65 64 20 61 6c 6c 20 64 61 74  included all dat
2b909 61 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 63  a necessary to c
2b90a 6f 6e 73 74 72 75 63 74 20 74 68 65 20 74 65 72  onstruct the ter
2b90b 6d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73  m..    */.    as
2b90c 73 65 72 74 28 20 6e 3c 69 44 6f 63 6c 69 73 74  sert( n<iDoclist
2b90d 44 61 74 61 20 29 3b 0a 20 20 20 20 2f 2a 20 53  Data );.    /* S
2b90e 6f 20 6c 6f 6e 67 20 61 73 20 53 54 41 4e 44 41  o long as STANDA
2b90f 4c 4f 4e 45 5f 4d 49 4e 20 69 73 20 68 61 6c 66  LONE_MIN is half
2b910 20 6f 72 20 6c 65 73 73 20 6f 66 20 4c 45 41 46   or less of LEAF
2b911 5f 4d 41 58 2c 20 74 68 65 0a 20 20 20 20 2a 2a  _MAX, the.    **
2b912 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 65 6d 63 70   following memcp
2b913 79 28 29 20 69 73 20 73 61 66 65 20 28 61 73 20  y() is safe (as 
2b914 6f 70 70 6f 73 65 64 20 74 6f 20 6e 65 65 64 69  opposed to needi
2b915 6e 67 20 61 20 6d 65 6d 6d 6f 76 65 29 2e 0a 20  ng a memmove).. 
2b916 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
2b917 28 20 32 2a 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d  ( 2*STANDALONE_M
2b918 49 4e 3c 3d 4c 45 41 46 5f 4d 41 58 20 29 3b 0a  IN<=LEAF_MAX );.
2b919 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 70 57      assert( n+pW
2b91a 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
2b91b 61 2d 69 44 6f 63 6c 69 73 74 44 61 74 61 3c 69  a-iDoclistData<i
2b91c 44 6f 63 6c 69 73 74 44 61 74 61 20 29 3b 0a 20  DoclistData );. 
2b91d 20 20 20 6d 65 6d 63 70 79 28 70 57 72 69 74 65     memcpy(pWrite
2b91e 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 6e 2c  r->data.pData+n,
2b91f 0a 20 20 20 20 20 20 20 20 20 20 20 70 57 72 69  .           pWri
2b920 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b  ter->data.pData+
2b921 69 44 6f 63 6c 69 73 74 44 61 74 61 2c 0a 20 20  iDoclistData,.  
2b922 20 20 20 20 20 20 20 20 20 70 57 72 69 74 65 72           pWriter
2b923 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2d 69 44 6f  ->data.nData-iDo
2b924 63 6c 69 73 74 44 61 74 61 29 3b 0a 20 20 20 20  clistData);.    
2b925 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44  pWriter->data.nD
2b926 61 74 61 20 2d 3d 20 69 44 6f 63 6c 69 73 74 44  ata -= iDoclistD
2b927 61 74 61 2d 6e 3b 0a 20 20 7d 0a 20 20 41 53 53  ata-n;.  }.  ASS
2b928 45 52 54 5f 56 41 4c 49 44 5f 4c 45 41 46 5f 4e  ERT_VALID_LEAF_N
2b929 4f 44 45 28 70 57 72 69 74 65 72 2d 3e 64 61 74  ODE(pWriter->dat
2b92a 61 2e 70 44 61 74 61 2c 20 70 57 72 69 74 65 72  a.pData, pWriter
2b92b 2d 3e 64 61 74 61 2e 6e 44 61 74 61 29 3b 0a 0a  ->data.nData);..
2b92c 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2b92d 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 50 75 73 68 20 70  OK;.}../* Push p
2b92e 54 65 72 6d 5b 6e 54 65 72 6d 5d 20 61 6c 6f 6e  Term[nTerm] alon
2b92f 67 20 77 69 74 68 20 74 68 65 20 64 6f 63 6c 69  g with the docli
2b930 73 74 20 64 61 74 61 20 74 6f 20 74 68 65 20 6c  st data to the l
2b931 65 61 66 20 6c 61 79 65 72 20 6f 66 0a 2a 2a 20  eaf layer of.** 
2b932 25 5f 73 65 67 6d 65 6e 74 73 2e 0a 2a 2f 0a 2f  %_segments..*/./
2b933 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 52 65  * TODO(shess) Re
2b934 76 69 73 65 20 77 72 69 74 65 5a 65 72 6f 53 65  vise writeZeroSe
2b935 67 6d 65 6e 74 28 29 20 73 6f 20 74 68 61 74 20  gment() so that 
2b936 64 6f 63 6c 69 73 74 73 20 61 72 65 0a 2a 2a 20  doclists are.** 
2b937 63 6f 6e 73 74 72 75 63 74 65 64 20 64 69 72 65  constructed dire
2b938 63 74 6c 79 20 69 6e 20 70 57 72 69 74 65 72 2d  ctly in pWriter-
2b939 3e 64 61 74 61 2e 0a 2a 2f 0a 73 74 61 74 69 63  >data..*/.static
2b93a 20 69 6e 74 20 6c 65 61 66 57 72 69 74 65 72 53   int leafWriterS
2b93b 74 65 70 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  tep(fulltext_vta
2b93c 62 20 2a 76 2c 20 4c 65 61 66 57 72 69 74 65 72  b *v, LeafWriter
2b93d 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20   *pWriter,.     
2b93e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b93f 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
2b940 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72  *pTerm, int nTer
2b941 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  m,.             
2b942 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
2b943 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
2b944 69 6e 74 20 6e 44 61 74 61 29 7b 0a 20 20 69 6e  int nData){.  in
2b945 74 20 72 63 3b 0a 20 20 44 4c 52 65 61 64 65 72  t rc;.  DLReader
2b946 20 72 65 61 64 65 72 3b 0a 0a 20 20 64 6c 72 49   reader;..  dlrI
2b947 6e 69 74 28 26 72 65 61 64 65 72 2c 20 44 4c 5f  nit(&reader, DL_
2b948 44 45 46 41 55 4c 54 2c 20 70 44 61 74 61 2c 20  DEFAULT, pData, 
2b949 6e 44 61 74 61 29 3b 0a 20 20 72 63 20 3d 20 6c  nData);.  rc = l
2b94a 65 61 66 57 72 69 74 65 72 53 74 65 70 4d 65 72  eafWriterStepMer
2b94b 67 65 28 76 2c 20 70 57 72 69 74 65 72 2c 20 70  ge(v, pWriter, p
2b94c 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 26 72 65  Term, nTerm, &re
2b94d 61 64 65 72 2c 20 31 29 3b 0a 20 20 64 6c 72 44  ader, 1);.  dlrD
2b94e 65 73 74 72 6f 79 28 26 72 65 61 64 65 72 29 3b  estroy(&reader);
2b94f 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
2b950 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .../************
2b951 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b952 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b953 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b954 2a 2a 2a 2a 2f 0a 2f 2a 20 4c 65 61 66 52 65 61  ****/./* LeafRea
2b955 64 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 69  der is used to i
2b956 74 65 72 61 74 65 20 6f 76 65 72 20 61 6e 20 69  terate over an i
2b957 6e 64 69 76 69 64 75 61 6c 20 6c 65 61 66 20 6e  ndividual leaf n
2b958 6f 64 65 2e 20 2a 2f 0a 74 79 70 65 64 65 66 20  ode. */.typedef 
2b959 73 74 72 75 63 74 20 4c 65 61 66 52 65 61 64 65  struct LeafReade
2b95a 72 20 7b 0a 20 20 44 61 74 61 42 75 66 66 65 72  r {.  DataBuffer
2b95b 20 74 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20   term;          
2b95c 2f 2a 20 63 6f 70 79 20 6f 66 20 63 75 72 72 65  /* copy of curre
2b95d 6e 74 20 74 65 72 6d 2e 20 2a 2f 0a 0a 20 20 63  nt term. */..  c
2b95e 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61  onst char *pData
2b95f 3b 20 20 20 20 20 20 20 20 2f 2a 20 64 61 74 61  ;        /* data
2b960 20 66 6f 72 20 63 75 72 72 65 6e 74 20 74 65 72   for current ter
2b961 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74  m. */.  int nDat
2b962 61 3b 0a 7d 20 4c 65 61 66 52 65 61 64 65 72 3b  a;.} LeafReader;
2b963 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65  ..static void le
2b964 61 66 52 65 61 64 65 72 44 65 73 74 72 6f 79 28  afReaderDestroy(
2b965 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52 65 61  LeafReader *pRea
2b966 64 65 72 29 7b 0a 20 20 64 61 74 61 42 75 66 66  der){.  dataBuff
2b967 65 72 44 65 73 74 72 6f 79 28 26 70 52 65 61 64  erDestroy(&pRead
2b968 65 72 2d 3e 74 65 72 6d 29 3b 0a 20 20 53 43 52  er->term);.  SCR
2b969 41 4d 42 4c 45 28 70 52 65 61 64 65 72 29 3b 0a  AMBLE(pReader);.
2b96a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65  }..static int le
2b96b 61 66 52 65 61 64 65 72 41 74 45 6e 64 28 4c 65  afReaderAtEnd(Le
2b96c 61 66 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  afReader *pReade
2b96d 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 52 65  r){.  return pRe
2b96e 61 64 65 72 2d 3e 6e 44 61 74 61 3c 3d 30 3b 0a  ader->nData<=0;.
2b96f 7d 0a 0a 2f 2a 20 41 63 63 65 73 73 20 74 68 65  }../* Access the
2b970 20 63 75 72 72 65 6e 74 20 74 65 72 6d 2e 20 2a   current term. *
2b971 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61  /.static int lea
2b972 66 52 65 61 64 65 72 54 65 72 6d 42 79 74 65 73  fReaderTermBytes
2b973 28 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52 65  (LeafReader *pRe
2b974 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  ader){.  return 
2b975 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44  pReader->term.nD
2b976 61 74 61 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f  ata;.}.static co
2b977 6e 73 74 20 63 68 61 72 20 2a 6c 65 61 66 52 65  nst char *leafRe
2b978 61 64 65 72 54 65 72 6d 28 4c 65 61 66 52 65 61  aderTerm(LeafRea
2b979 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
2b97a 20 61 73 73 65 72 74 28 20 70 52 65 61 64 65 72   assert( pReader
2b97b 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 3e 30 20 29  ->term.nData>0 )
2b97c 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64  ;.  return pRead
2b97d 65 72 2d 3e 74 65 72 6d 2e 70 44 61 74 61 3b 0a  er->term.pData;.
2b97e 7d 0a 0a 2f 2a 20 41 63 63 65 73 73 20 74 68 65  }../* Access the
2b97f 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 66 6f   doclist data fo
2b980 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 65  r the current te
2b981 72 6d 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  rm. */.static in
2b982 74 20 6c 65 61 66 52 65 61 64 65 72 44 61 74 61  t leafReaderData
2b983 42 79 74 65 73 28 4c 65 61 66 52 65 61 64 65 72  Bytes(LeafReader
2b984 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 69 6e   *pReader){.  in
2b985 74 20 6e 44 61 74 61 3b 0a 20 20 61 73 73 65 72  t nData;.  asser
2b986 74 28 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d  t( pReader->term
2b987 2e 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 66 74  .nData>0 );.  ft
2b988 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52  s3GetVarint32(pR
2b989 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 6e  eader->pData, &n
2b98a 44 61 74 61 29 3b 0a 20 20 72 65 74 75 72 6e 20  Data);.  return 
2b98b 6e 44 61 74 61 3b 0a 7d 0a 73 74 61 74 69 63 20  nData;.}.static 
2b98c 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 65 61 66  const char *leaf
2b98d 52 65 61 64 65 72 44 61 74 61 28 4c 65 61 66 52  ReaderData(LeafR
2b98e 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
2b98f 0a 20 20 69 6e 74 20 6e 2c 20 6e 44 61 74 61 3b  .  int n, nData;
2b990 0a 20 20 61 73 73 65 72 74 28 20 70 52 65 61 64  .  assert( pRead
2b991 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 3e 30  er->term.nData>0
2b992 20 29 3b 0a 20 20 6e 20 3d 20 66 74 73 33 47 65   );.  n = fts3Ge
2b993 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65  tVarint32(pReade
2b994 72 2d 3e 70 44 61 74 61 2c 20 26 6e 44 61 74 61  r->pData, &nData
2b995 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61  );.  return pRea
2b996 64 65 72 2d 3e 70 44 61 74 61 2b 6e 3b 0a 7d 0a  der->pData+n;.}.
2b997 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65 61  .static void lea
2b998 66 52 65 61 64 65 72 49 6e 69 74 28 63 6f 6e 73  fReaderInit(cons
2b999 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69  t char *pData, i
2b99a 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20 20 20  nt nData,.      
2b99b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b99c 20 20 20 20 20 4c 65 61 66 52 65 61 64 65 72 20       LeafReader 
2b99d 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 69 6e 74  *pReader){.  int
2b99e 20 6e 54 65 72 6d 2c 20 6e 3b 0a 0a 20 20 61 73   nTerm, n;..  as
2b99f 73 65 72 74 28 20 6e 44 61 74 61 3e 30 20 29 3b  sert( nData>0 );
2b9a0 0a 20 20 61 73 73 65 72 74 28 20 70 44 61 74 61  .  assert( pData
2b9a1 5b 30 5d 3d 3d 27 5c 30 27 20 29 3b 0a 0a 20 20  [0]=='\0' );..  
2b9a2 43 4c 45 41 52 28 70 52 65 61 64 65 72 29 3b 0a  CLEAR(pReader);.
2b9a3 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 66  .  /* Read the f
2b9a4 69 72 73 74 20 74 65 72 6d 2c 20 73 6b 69 70 70  irst term, skipp
2b9a5 69 6e 67 20 74 68 65 20 68 65 61 64 65 72 20 62  ing the header b
2b9a6 79 74 65 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66 74  yte. */.  n = ft
2b9a7 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 44  s3GetVarint32(pD
2b9a8 61 74 61 2b 31 2c 20 26 6e 54 65 72 6d 29 3b 0a  ata+1, &nTerm);.
2b9a9 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74    dataBufferInit
2b9aa 28 26 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2c  (&pReader->term,
2b9ab 20 6e 54 65 72 6d 29 3b 0a 20 20 64 61 74 61 42   nTerm);.  dataB
2b9ac 75 66 66 65 72 52 65 70 6c 61 63 65 28 26 70 52  ufferReplace(&pR
2b9ad 65 61 64 65 72 2d 3e 74 65 72 6d 2c 20 70 44 61  eader->term, pDa
2b9ae 74 61 2b 31 2b 6e 2c 20 6e 54 65 72 6d 29 3b 0a  ta+1+n, nTerm);.
2b9af 0a 20 20 2f 2a 20 50 6f 73 69 74 69 6f 6e 20 61  .  /* Position a
2b9b0 66 74 65 72 20 74 68 65 20 66 69 72 73 74 20 74  fter the first t
2b9b1 65 72 6d 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  erm. */.  assert
2b9b2 28 20 31 2b 6e 2b 6e 54 65 72 6d 3c 6e 44 61 74  ( 1+n+nTerm<nDat
2b9b3 61 20 29 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e  a );.  pReader->
2b9b4 70 44 61 74 61 20 3d 20 70 44 61 74 61 2b 31 2b  pData = pData+1+
2b9b5 6e 2b 6e 54 65 72 6d 3b 0a 20 20 70 52 65 61 64  n+nTerm;.  pRead
2b9b6 65 72 2d 3e 6e 44 61 74 61 20 3d 20 6e 44 61 74  er->nData = nDat
2b9b7 61 2d 31 2d 6e 2d 6e 54 65 72 6d 3b 0a 7d 0a 0a  a-1-n-nTerm;.}..
2b9b8 2f 2a 20 53 74 65 70 20 74 68 65 20 72 65 61 64  /* Step the read
2b9b9 65 72 20 66 6f 72 77 61 72 64 20 74 6f 20 74 68  er forward to th
2b9ba 65 20 6e 65 78 74 20 74 65 72 6d 2e 20 2a 2f 0a  e next term. */.
2b9bb 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65 61 66  static void leaf
2b9bc 52 65 61 64 65 72 53 74 65 70 28 4c 65 61 66 52  ReaderStep(LeafR
2b9bd 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
2b9be 0a 20 20 69 6e 74 20 6e 2c 20 6e 44 61 74 61 2c  .  int n, nData,
2b9bf 20 6e 50 72 65 66 69 78 2c 20 6e 53 75 66 66 69   nPrefix, nSuffi
2b9c0 78 3b 0a 20 20 61 73 73 65 72 74 28 20 21 6c 65  x;.  assert( !le
2b9c1 61 66 52 65 61 64 65 72 41 74 45 6e 64 28 70 52  afReaderAtEnd(pR
2b9c2 65 61 64 65 72 29 20 29 3b 0a 0a 20 20 2f 2a 20  eader) );..  /* 
2b9c3 53 6b 69 70 20 70 72 65 76 69 6f 75 73 20 65 6e  Skip previous en
2b9c4 74 72 79 27 73 20 64 61 74 61 20 62 6c 6f 63 6b  try's data block
2b9c5 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47  . */.  n = fts3G
2b9c6 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64  etVarint32(pRead
2b9c7 65 72 2d 3e 70 44 61 74 61 2c 20 26 6e 44 61 74  er->pData, &nDat
2b9c8 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 2b  a);.  assert( n+
2b9c9 6e 44 61 74 61 3c 3d 70 52 65 61 64 65 72 2d 3e  nData<=pReader->
2b9ca 6e 44 61 74 61 20 29 3b 0a 20 20 70 52 65 61 64  nData );.  pRead
2b9cb 65 72 2d 3e 70 44 61 74 61 20 2b 3d 20 6e 2b 6e  er->pData += n+n
2b9cc 44 61 74 61 3b 0a 20 20 70 52 65 61 64 65 72 2d  Data;.  pReader-
2b9cd 3e 6e 44 61 74 61 20 2d 3d 20 6e 2b 6e 44 61 74  >nData -= n+nDat
2b9ce 61 3b 0a 0a 20 20 69 66 28 20 21 6c 65 61 66 52  a;..  if( !leafR
2b9cf 65 61 64 65 72 41 74 45 6e 64 28 70 52 65 61 64  eaderAtEnd(pRead
2b9d0 65 72 29 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f  er) ){.    /* Co
2b9d1 6e 73 74 72 75 63 74 20 74 68 65 20 6e 65 77 20  nstruct the new 
2b9d2 74 65 72 6d 20 75 73 69 6e 67 20 61 20 70 72 65  term using a pre
2b9d3 66 69 78 20 66 72 6f 6d 20 74 68 65 20 6f 6c 64  fix from the old
2b9d4 20 74 65 72 6d 20 70 6c 75 73 20 61 0a 20 20 20   term plus a.   
2b9d5 20 2a 2a 20 73 75 66 66 69 78 20 66 72 6f 6d 20   ** suffix from 
2b9d6 74 68 65 20 6c 65 61 66 20 64 61 74 61 2e 0a 20  the leaf data.. 
2b9d7 20 20 20 2a 2f 0a 20 20 20 20 6e 20 3d 20 66 74     */.    n = ft
2b9d8 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52  s3GetVarint32(pR
2b9d9 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 6e  eader->pData, &n
2b9da 50 72 65 66 69 78 29 3b 0a 20 20 20 20 6e 20 2b  Prefix);.    n +
2b9db 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33  = fts3GetVarint3
2b9dc 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61  2(pReader->pData
2b9dd 2b 6e 2c 20 26 6e 53 75 66 66 69 78 29 3b 0a 20  +n, &nSuffix);. 
2b9de 20 20 20 61 73 73 65 72 74 28 20 6e 2b 6e 53 75     assert( n+nSu
2b9df 66 66 69 78 3c 70 52 65 61 64 65 72 2d 3e 6e 44  ffix<pReader->nD
2b9e0 61 74 61 20 29 3b 0a 20 20 20 20 70 52 65 61 64  ata );.    pRead
2b9e1 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 20 3d  er->term.nData =
2b9e2 20 6e 50 72 65 66 69 78 3b 0a 20 20 20 20 64 61   nPrefix;.    da
2b9e3 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 26  taBufferAppend(&
2b9e4 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2c 20 70  pReader->term, p
2b9e5 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c  Reader->pData+n,
2b9e6 20 6e 53 75 66 66 69 78 29 3b 0a 0a 20 20 20 20   nSuffix);..    
2b9e7 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 20 2b  pReader->pData +
2b9e8 3d 20 6e 2b 6e 53 75 66 66 69 78 3b 0a 20 20 20  = n+nSuffix;.   
2b9e9 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20   pReader->nData 
2b9ea 2d 3d 20 6e 2b 6e 53 75 66 66 69 78 3b 0a 20 20  -= n+nSuffix;.  
2b9eb 7d 0a 7d 0a 0a 2f 2a 20 73 74 72 63 6d 70 2d 73  }.}../* strcmp-s
2b9ec 74 79 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20  tyle comparison 
2b9ed 6f 66 20 70 52 65 61 64 65 72 27 73 20 63 75 72  of pReader's cur
2b9ee 72 65 6e 74 20 74 65 72 6d 20 61 67 61 69 6e 73  rent term agains
2b9ef 74 20 70 54 65 72 6d 2e 0a 2a 2a 20 49 66 20 69  t pTerm..** If i
2b9f0 73 50 72 65 66 69 78 2c 20 65 71 75 61 6c 69 74  sPrefix, equalit
2b9f1 79 20 6d 65 61 6e 73 20 65 71 75 61 6c 20 74 68  y means equal th
2b9f2 72 6f 75 67 68 20 6e 54 65 72 6d 20 62 79 74 65  rough nTerm byte
2b9f3 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
2b9f4 20 6c 65 61 66 52 65 61 64 65 72 54 65 72 6d 43   leafReaderTermC
2b9f5 6d 70 28 4c 65 61 66 52 65 61 64 65 72 20 2a 70  mp(LeafReader *p
2b9f6 52 65 61 64 65 72 2c 0a 20 20 20 20 20 20 20 20  Reader,.        
2b9f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b9f8 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
2b9f9 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72  *pTerm, int nTer
2b9fa 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69 78 29  m, int isPrefix)
2b9fb 7b 0a 20 20 69 6e 74 20 63 2c 20 6e 20 3d 20 70  {.  int c, n = p
2b9fc 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61  Reader->term.nDa
2b9fd 74 61 3c 6e 54 65 72 6d 20 3f 20 70 52 65 61 64  ta<nTerm ? pRead
2b9fe 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 20 3a  er->term.nData :
2b9ff 20 6e 54 65 72 6d 3b 0a 20 20 69 66 28 20 6e 3d   nTerm;.  if( n=
2ba00 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 52  =0 ){.    if( pR
2ba01 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74  eader->term.nDat
2ba02 61 3e 30 20 29 20 72 65 74 75 72 6e 20 2d 31 3b  a>0 ) return -1;
2ba03 0a 20 20 20 20 69 66 28 6e 54 65 72 6d 3e 30 20  .    if(nTerm>0 
2ba04 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20  ) return 1;.    
2ba05 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20  return 0;.  }.. 
2ba06 20 63 20 3d 20 6d 65 6d 63 6d 70 28 70 52 65 61   c = memcmp(pRea
2ba07 64 65 72 2d 3e 74 65 72 6d 2e 70 44 61 74 61 2c  der->term.pData,
2ba08 20 70 54 65 72 6d 2c 20 6e 29 3b 0a 20 20 69 66   pTerm, n);.  if
2ba09 28 20 63 21 3d 30 20 29 20 72 65 74 75 72 6e 20  ( c!=0 ) return 
2ba0a 63 3b 0a 20 20 69 66 28 20 69 73 50 72 65 66 69  c;.  if( isPrefi
2ba0b 78 20 26 26 20 6e 3d 3d 6e 54 65 72 6d 20 29 20  x && n==nTerm ) 
2ba0c 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 75  return 0;.  retu
2ba0d 72 6e 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d  rn pReader->term
2ba0e 2e 6e 44 61 74 61 20 2d 20 6e 54 65 72 6d 3b 0a  .nData - nTerm;.
2ba0f 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }.../***********
2ba10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ba11 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ba12 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ba13 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 4c 65 61 76 65 73  *****/./* Leaves
2ba14 52 65 61 64 65 72 20 77 72 61 70 73 20 4c 65 61  Reader wraps Lea
2ba15 66 52 65 61 64 65 72 20 74 6f 20 61 6c 6c 6f 77  fReader to allow
2ba16 20 69 74 65 72 61 74 69 6e 67 20 6f 76 65 72 20   iterating over 
2ba17 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a 20 6c 65  the entire.** le
2ba18 61 66 20 6c 61 79 65 72 20 6f 66 20 74 68 65 20  af layer of the 
2ba19 74 72 65 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  tree..*/.typedef
2ba1a 20 73 74 72 75 63 74 20 4c 65 61 76 65 73 52 65   struct LeavesRe
2ba1b 61 64 65 72 20 7b 0a 20 20 69 6e 74 20 69 64 78  ader {.  int idx
2ba1c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2ba1d 20 20 20 2f 2a 20 49 6e 64 65 78 20 77 69 74 68     /* Index with
2ba1e 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74 2e 20  in the segment. 
2ba1f 2a 2f 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  */..  sqlite3_st
2ba20 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20 20 20 20  mt *pStmt;      
2ba21 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 77 65 27  /* Statement we'
2ba22 72 65 20 73 74 72 65 61 6d 69 6e 67 20 6c 65 61  re streaming lea
2ba23 76 65 73 20 66 72 6f 6d 2e 20 2a 2f 0a 20 20 69  ves from. */.  i
2ba24 6e 74 20 65 6f 66 3b 20 20 20 20 20 20 20 20 20  nt eof;         
2ba25 20 20 20 20 20 20 20 20 20 2f 2a 20 77 65 27 76           /* we'v
2ba26 65 20 73 65 65 6e 20 53 51 4c 49 54 45 5f 44 4f  e seen SQLITE_DO
2ba27 4e 45 20 66 72 6f 6d 20 70 53 74 6d 74 2e 20 2a  NE from pStmt. *
2ba28 2f 0a 0a 20 20 4c 65 61 66 52 65 61 64 65 72 20  /..  LeafReader 
2ba29 6c 65 61 66 52 65 61 64 65 72 3b 20 20 20 20 2f  leafReader;    /
2ba2a 2a 20 72 65 61 64 65 72 20 66 6f 72 20 74 68 65  * reader for the
2ba2b 20 63 75 72 72 65 6e 74 20 6c 65 61 66 2e 20 2a   current leaf. *
2ba2c 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 72  /.  DataBuffer r
2ba2d 6f 6f 74 44 61 74 61 3b 20 20 20 20 20 20 2f 2a  ootData;      /*
2ba2e 20 72 6f 6f 74 20 64 61 74 61 20 66 6f 72 20 69   root data for i
2ba2f 6e 6c 69 6e 65 2e 20 2a 2f 0a 7d 20 4c 65 61 76  nline. */.} Leav
2ba30 65 73 52 65 61 64 65 72 3b 0a 0a 2f 2a 20 41 63  esReader;../* Ac
2ba31 63 65 73 73 20 74 68 65 20 63 75 72 72 65 6e 74  cess the current
2ba32 20 74 65 72 6d 2e 20 2a 2f 0a 73 74 61 74 69 63   term. */.static
2ba33 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65   int leavesReade
2ba34 72 54 65 72 6d 42 79 74 65 73 28 4c 65 61 76 65  rTermBytes(Leave
2ba35 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  sReader *pReader
2ba36 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 70 52  ){.  assert( !pR
2ba37 65 61 64 65 72 2d 3e 65 6f 66 20 29 3b 0a 20 20  eader->eof );.  
2ba38 72 65 74 75 72 6e 20 6c 65 61 66 52 65 61 64 65  return leafReade
2ba39 72 54 65 72 6d 42 79 74 65 73 28 26 70 52 65 61  rTermBytes(&pRea
2ba3a 64 65 72 2d 3e 6c 65 61 66 52 65 61 64 65 72 29  der->leafReader)
2ba3b 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.}.static const
2ba3c 20 63 68 61 72 20 2a 6c 65 61 76 65 73 52 65 61   char *leavesRea
2ba3d 64 65 72 54 65 72 6d 28 4c 65 61 76 65 73 52 65  derTerm(LeavesRe
2ba3e 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
2ba3f 20 20 61 73 73 65 72 74 28 20 21 70 52 65 61 64    assert( !pRead
2ba40 65 72 2d 3e 65 6f 66 20 29 3b 0a 20 20 72 65 74  er->eof );.  ret
2ba41 75 72 6e 20 6c 65 61 66 52 65 61 64 65 72 54 65  urn leafReaderTe
2ba42 72 6d 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61  rm(&pReader->lea
2ba43 66 52 65 61 64 65 72 29 3b 0a 7d 0a 0a 2f 2a 20  fReader);.}../* 
2ba44 41 63 63 65 73 73 20 74 68 65 20 64 6f 63 6c 69  Access the docli
2ba45 73 74 20 64 61 74 61 20 66 6f 72 20 74 68 65 20  st data for the 
2ba46 63 75 72 72 65 6e 74 20 74 65 72 6d 2e 20 2a 2f  current term. */
2ba47 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 76  .static int leav
2ba48 65 73 52 65 61 64 65 72 44 61 74 61 42 79 74 65  esReaderDataByte
2ba49 73 28 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  s(LeavesReader *
2ba4a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65  pReader){.  asse
2ba4b 72 74 28 20 21 70 52 65 61 64 65 72 2d 3e 65 6f  rt( !pReader->eo
2ba4c 66 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 6c 65  f );.  return le
2ba4d 61 66 52 65 61 64 65 72 44 61 74 61 42 79 74 65  afReaderDataByte
2ba4e 73 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66  s(&pReader->leaf
2ba4f 52 65 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69  Reader);.}.stati
2ba50 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 65  c const char *le
2ba51 61 76 65 73 52 65 61 64 65 72 44 61 74 61 28 4c  avesReaderData(L
2ba52 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 52 65  eavesReader *pRe
2ba53 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28  ader){.  assert(
2ba54 20 21 70 52 65 61 64 65 72 2d 3e 65 6f 66 20 29   !pReader->eof )
2ba55 3b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 66 52  ;.  return leafR
2ba56 65 61 64 65 72 44 61 74 61 28 26 70 52 65 61 64  eaderData(&pRead
2ba57 65 72 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b  er->leafReader);
2ba58 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .}..static int l
2ba59 65 61 76 65 73 52 65 61 64 65 72 41 74 45 6e 64  eavesReaderAtEnd
2ba5a 28 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70  (LeavesReader *p
2ba5b 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72  Reader){.  retur
2ba5c 6e 20 70 52 65 61 64 65 72 2d 3e 65 6f 66 3b 0a  n pReader->eof;.
2ba5d 7d 0a 0a 2f 2a 20 6c 6f 61 64 53 65 67 6d 65 6e  }../* loadSegmen
2ba5e 74 4c 65 61 76 65 73 28 29 20 6d 61 79 20 6e 6f  tLeaves() may no
2ba5f 74 20 72 65 61 64 20 61 6c 6c 20 74 68 65 20 77  t read all the w
2ba60 61 79 20 74 6f 20 53 51 4c 49 54 45 5f 44 4f 4e  ay to SQLITE_DON
2ba61 45 2c 20 74 68 75 73 0a 2a 2a 20 6c 65 61 76 69  E, thus.** leavi
2ba62 6e 67 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  ng the statement
2ba63 20 68 61 6e 64 6c 65 20 6f 70 65 6e 2c 20 77 68   handle open, wh
2ba64 69 63 68 20 6c 6f 63 6b 73 20 74 68 65 20 74 61  ich locks the ta
2ba65 62 6c 65 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28  ble..*/./* TODO(
2ba66 73 68 65 73 73 29 20 54 68 69 73 20 22 73 6f 6c  shess) This "sol
2ba67 75 74 69 6f 6e 22 20 69 73 20 6e 6f 74 20 73 61  ution" is not sa
2ba68 74 69 73 66 61 63 74 6f 72 79 2e 20 20 52 65 61  tisfactory.  Rea
2ba69 6c 6c 79 2c 20 74 68 65 72 65 0a 2a 2a 20 73 68  lly, there.** sh
2ba6a 6f 75 6c 64 20 62 65 20 63 68 65 63 6b 2d 69 6e  ould be check-in
2ba6b 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6c   function for al
2ba6c 6c 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64  l statement hand
2ba6d 6c 65 73 20 77 68 69 63 68 0a 2a 2a 20 61 72 72  les which.** arr
2ba6e 61 6e 67 65 73 20 74 6f 20 63 61 6c 6c 20 73 71  anges to call sq
2ba6f 6c 69 74 65 33 5f 72 65 73 65 74 28 29 2e 20 20  lite3_reset().  
2ba70 54 68 69 73 20 6d 6f 73 74 20 6c 69 6b 65 6c 79  This most likely
2ba71 20 77 69 6c 6c 20 72 65 71 75 69 72 65 0a 2a 2a   will require.**
2ba72 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20 74 6f   modification to
2ba73 20 63 6f 6e 74 72 6f 6c 20 66 6c 6f 77 20 61 6c   control flow al
2ba74 6c 20 6f 76 65 72 20 74 68 65 20 70 6c 61 63 65  l over the place
2ba75 2c 20 74 68 6f 75 67 68 2c 20 73 6f 20 66 6f 72  , though, so for
2ba76 20 6e 6f 77 0a 2a 2a 20 6a 75 73 74 20 70 75 6e   now.** just pun
2ba77 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68  t..**.** Note th
2ba78 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 79  e the current sy
2ba79 73 74 65 6d 20 61 73 73 75 6d 65 73 20 74 68 61  stem assumes tha
2ba7a 74 20 73 65 67 6d 65 6e 74 20 6d 65 72 67 65 73  t segment merges
2ba7b 20 77 69 6c 6c 20 72 75 6e 20 74 6f 0a 2a 2a 20   will run to.** 
2ba7c 63 6f 6d 70 6c 65 74 69 6f 6e 2c 20 77 68 69 63  completion, whic
2ba7d 68 20 69 73 20 77 68 79 20 74 68 69 73 20 70 61  h is why this pa
2ba7e 72 74 69 63 75 6c 61 72 20 70 72 6f 62 61 62 6c  rticular probabl
2ba7f 79 20 68 61 73 6e 27 74 20 61 72 69 73 65 6e 20  y hasn't arisen 
2ba80 69 6e 0a 2a 2a 20 74 68 69 73 20 63 61 73 65 2e  in.** this case.
2ba81 20 20 50 72 6f 62 61 62 6c 79 20 61 20 62 72 69    Probably a bri
2ba82 74 74 6c 65 20 61 73 73 75 6d 70 74 69 6f 6e 2e  ttle assumption.
2ba83 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
2ba84 65 61 76 65 73 52 65 61 64 65 72 52 65 73 65 74  eavesReaderReset
2ba85 28 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70  (LeavesReader *p
2ba86 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72  Reader){.  retur
2ba87 6e 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  n sqlite3_reset(
2ba88 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74 29 3b  pReader->pStmt);
2ba89 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .}..static void 
2ba8a 6c 65 61 76 65 73 52 65 61 64 65 72 44 65 73 74  leavesReaderDest
2ba8b 72 6f 79 28 4c 65 61 76 65 73 52 65 61 64 65 72  roy(LeavesReader
2ba8c 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 6c 65   *pReader){.  le
2ba8d 61 66 52 65 61 64 65 72 44 65 73 74 72 6f 79 28  afReaderDestroy(
2ba8e 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66 52 65  &pReader->leafRe
2ba8f 61 64 65 72 29 3b 0a 20 20 64 61 74 61 42 75 66  ader);.  dataBuf
2ba90 66 65 72 44 65 73 74 72 6f 79 28 26 70 52 65 61  ferDestroy(&pRea
2ba91 64 65 72 2d 3e 72 6f 6f 74 44 61 74 61 29 3b 0a  der->rootData);.
2ba92 20 20 53 43 52 41 4d 42 4c 45 28 70 52 65 61 64    SCRAMBLE(pRead
2ba93 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 49 6e 69 74 69  er);.}../* Initi
2ba94 61 6c 69 7a 65 20 70 52 65 61 64 65 72 20 77 69  alize pReader wi
2ba95 74 68 20 74 68 65 20 67 69 76 65 6e 20 72 6f 6f  th the given roo
2ba96 74 20 64 61 74 61 20 28 69 66 20 69 53 74 61 72  t data (if iStar
2ba97 74 42 6c 6f 63 6b 69 64 3d 3d 30 0a 2a 2a 20 74  tBlockid==0.** t
2ba98 68 65 20 6c 65 61 66 20 64 61 74 61 20 77 61 73  he leaf data was
2ba99 20 65 6e 74 69 72 65 6c 79 20 63 6f 6e 74 61 69   entirely contai
2ba9a 6e 65 64 20 69 6e 20 74 68 65 20 72 6f 6f 74 29  ned in the root)
2ba9b 2c 20 6f 72 20 66 72 6f 6d 20 74 68 65 0a 2a 2a  , or from the.**
2ba9c 20 73 74 72 65 61 6d 20 6f 66 20 62 6c 6f 63 6b   stream of block
2ba9d 73 20 62 65 74 77 65 65 6e 20 69 53 74 61 72 74  s between iStart
2ba9e 42 6c 6f 63 6b 69 64 20 61 6e 64 20 69 45 6e 64  Blockid and iEnd
2ba9f 42 6c 6f 63 6b 69 64 2c 20 69 6e 63 6c 75 73 69  Blockid, inclusi
2baa0 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ve..*/.static in
2baa1 74 20 6c 65 61 76 65 73 52 65 61 64 65 72 49 6e  t leavesReaderIn
2baa2 69 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  it(fulltext_vtab
2baa3 20 2a 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20   *v,.           
2baa4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2baa5 20 69 6e 74 20 69 64 78 2c 0a 20 20 20 20 20 20   int idx,.      
2baa6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2baa7 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74        sqlite_int
2baa8 36 34 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64  64 iStartBlockid
2baa9 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2baaa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
2baab 6c 69 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 42  lite_int64 iEndB
2baac 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20 20  lockid,.        
2baad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2baae 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2baaf 70 52 6f 6f 74 44 61 74 61 2c 20 69 6e 74 20 6e  pRootData, int n
2bab0 52 6f 6f 74 44 61 74 61 2c 0a 20 20 20 20 20 20  RootData,.      
2bab1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bab2 20 20 20 20 20 20 4c 65 61 76 65 73 52 65 61 64        LeavesRead
2bab3 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
2bab4 43 4c 45 41 52 28 70 52 65 61 64 65 72 29 3b 0a  CLEAR(pReader);.
2bab5 20 20 70 52 65 61 64 65 72 2d 3e 69 64 78 20 3d    pReader->idx =
2bab6 20 69 64 78 3b 0a 0a 20 20 64 61 74 61 42 75 66   idx;..  dataBuf
2bab7 66 65 72 49 6e 69 74 28 26 70 52 65 61 64 65 72  ferInit(&pReader
2bab8 2d 3e 72 6f 6f 74 44 61 74 61 2c 20 30 29 3b 0a  ->rootData, 0);.
2bab9 20 20 69 66 28 20 69 53 74 61 72 74 42 6c 6f 63    if( iStartBloc
2baba 6b 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  kid==0 ){.    /*
2babb 20 45 6e 74 69 72 65 20 6c 65 61 66 20 6c 65 76   Entire leaf lev
2babc 65 6c 20 66 69 74 20 69 6e 20 72 6f 6f 74 20 64  el fit in root d
2babd 61 74 61 2e 20 2a 2f 0a 20 20 20 20 64 61 74 61  ata. */.    data
2babe 42 75 66 66 65 72 52 65 70 6c 61 63 65 28 26 70  BufferReplace(&p
2babf 52 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61 74 61  Reader->rootData
2bac0 2c 20 70 52 6f 6f 74 44 61 74 61 2c 20 6e 52 6f  , pRootData, nRo
2bac1 6f 74 44 61 74 61 29 3b 0a 20 20 20 20 6c 65 61  otData);.    lea
2bac2 66 52 65 61 64 65 72 49 6e 69 74 28 70 52 65 61  fReaderInit(pRea
2bac3 64 65 72 2d 3e 72 6f 6f 74 44 61 74 61 2e 70 44  der->rootData.pD
2bac4 61 74 61 2c 20 70 52 65 61 64 65 72 2d 3e 72 6f  ata, pReader->ro
2bac5 6f 74 44 61 74 61 2e 6e 44 61 74 61 2c 0a 20 20  otData.nData,.  
2bac6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bac7 20 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66 52   &pReader->leafR
2bac8 65 61 64 65 72 29 3b 0a 20 20 7d 65 6c 73 65 7b  eader);.  }else{
2bac9 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  .    sqlite3_stm
2baca 74 20 2a 73 3b 0a 20 20 20 20 69 6e 74 20 72 63  t *s;.    int rc
2bacb 20 3d 20 73 71 6c 5f 67 65 74 5f 6c 65 61 66 5f   = sql_get_leaf_
2bacc 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 69 64 78  statement(v, idx
2bacd 2c 20 26 73 29 3b 0a 20 20 20 20 69 66 28 20 72  , &s);.    if( r
2bace 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
2bacf 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 72  eturn rc;..    r
2bad0 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  c = sqlite3_bind
2bad1 5f 69 6e 74 36 34 28 73 2c 20 31 2c 20 69 53 74  _int64(s, 1, iSt
2bad2 61 72 74 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 20  artBlockid);.   
2bad3 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2bad4 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
2bad5 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
2bad6 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20  3_bind_int64(s, 
2bad7 32 2c 20 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b  2, iEndBlockid);
2bad8 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
2bad9 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
2bada 72 63 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73 71  rc;..    rc = sq
2badb 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20  lite3_step(s);. 
2badc 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
2badd 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20  E_DONE ){.      
2bade 70 52 65 61 64 65 72 2d 3e 65 6f 66 20 3d 20 31  pReader->eof = 1
2badf 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
2bae0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
2bae1 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
2bae2 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20  TE_ROW ) return 
2bae3 72 63 3b 0a 0a 20 20 20 20 70 52 65 61 64 65 72  rc;..    pReader
2bae4 2d 3e 70 53 74 6d 74 20 3d 20 73 3b 0a 20 20 20  ->pStmt = s;.   
2bae5 20 6c 65 61 66 52 65 61 64 65 72 49 6e 69 74 28   leafReaderInit(
2bae6 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
2bae7 6c 6f 62 28 70 52 65 61 64 65 72 2d 3e 70 53 74  lob(pReader->pSt
2bae8 6d 74 2c 20 30 29 2c 0a 20 20 20 20 20 20 20 20  mt, 0),.        
2bae9 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2baea 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
2baeb 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74 2c 20  pReader->pStmt, 
2baec 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  0),.            
2baed 20 20 20 20 20 20 20 26 70 52 65 61 64 65 72 2d         &pReader-
2baee 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 20 20  >leafReader);.  
2baef 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
2baf0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 53 74 65 70  E_OK;.}../* Step
2baf1 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 65 61   the current lea
2baf2 66 20 66 6f 72 77 61 72 64 20 74 6f 20 74 68 65  f forward to the
2baf3 20 6e 65 78 74 20 74 65 72 6d 2e 20 20 49 66 20   next term.  If 
2baf4 77 65 20 72 65 61 63 68 20 74 68 65 0a 2a 2a 20  we reach the.** 
2baf5 65 6e 64 20 6f 66 20 74 68 65 20 63 75 72 72 65  end of the curre
2baf6 6e 74 20 6c 65 61 66 2c 20 73 74 65 70 20 66 6f  nt leaf, step fo
2baf7 72 77 61 72 64 20 74 6f 20 74 68 65 20 6e 65 78  rward to the nex
2baf8 74 20 6c 65 61 66 20 62 6c 6f 63 6b 2e 0a 2a 2f  t leaf block..*/
2baf9 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 76  .static int leav
2bafa 65 73 52 65 61 64 65 72 53 74 65 70 28 66 75 6c  esReaderStep(ful
2bafb 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c  ltext_vtab *v, L
2bafc 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 52 65  eavesReader *pRe
2bafd 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28  ader){.  assert(
2bafe 20 21 6c 65 61 76 65 73 52 65 61 64 65 72 41 74   !leavesReaderAt
2baff 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a  End(pReader) );.
2bb00 20 20 6c 65 61 66 52 65 61 64 65 72 53 74 65 70    leafReaderStep
2bb01 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66 52  (&pReader->leafR
2bb02 65 61 64 65 72 29 3b 0a 0a 20 20 69 66 28 20 6c  eader);..  if( l
2bb03 65 61 66 52 65 61 64 65 72 41 74 45 6e 64 28 26  eafReaderAtEnd(&
2bb04 70 52 65 61 64 65 72 2d 3e 6c 65 61 66 52 65 61  pReader->leafRea
2bb05 64 65 72 29 20 29 7b 0a 20 20 20 20 69 6e 74 20  der) ){.    int 
2bb06 72 63 3b 0a 20 20 20 20 69 66 28 20 70 52 65 61  rc;.    if( pRea
2bb07 64 65 72 2d 3e 72 6f 6f 74 44 61 74 61 2e 70 44  der->rootData.pD
2bb08 61 74 61 20 29 7b 0a 20 20 20 20 20 20 70 52 65  ata ){.      pRe
2bb09 61 64 65 72 2d 3e 65 6f 66 20 3d 20 31 3b 0a 20  ader->eof = 1;. 
2bb0a 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
2bb0b 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20  TE_OK;.    }.   
2bb0c 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74   rc = sqlite3_st
2bb0d 65 70 28 70 52 65 61 64 65 72 2d 3e 70 53 74 6d  ep(pReader->pStm
2bb0e 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  t);.    if( rc!=
2bb0f 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
2bb10 20 20 20 20 70 52 65 61 64 65 72 2d 3e 65 6f 66      pReader->eof
2bb11 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75   = 1;.      retu
2bb12 72 6e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  rn rc==SQLITE_DO
2bb13 4e 45 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a  NE ? SQLITE_OK :
2bb14 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6c   rc;.    }.    l
2bb15 65 61 66 52 65 61 64 65 72 44 65 73 74 72 6f 79  eafReaderDestroy
2bb16 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66 52  (&pReader->leafR
2bb17 65 61 64 65 72 29 3b 0a 20 20 20 20 6c 65 61 66  eader);.    leaf
2bb18 52 65 61 64 65 72 49 6e 69 74 28 73 71 6c 69 74  ReaderInit(sqlit
2bb19 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70  e3_column_blob(p
2bb1a 52 65 61 64 65 72 2d 3e 70 53 74 6d 74 2c 20 30  Reader->pStmt, 0
2bb1b 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
2bb1c 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f        sqlite3_co
2bb1d 6c 75 6d 6e 5f 62 79 74 65 73 28 70 52 65 61 64  lumn_bytes(pRead
2bb1e 65 72 2d 3e 70 53 74 6d 74 2c 20 30 29 2c 0a 20  er->pStmt, 0),. 
2bb1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bb20 20 20 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66    &pReader->leaf
2bb21 52 65 61 64 65 72 29 3b 0a 20 20 7d 0a 20 20 72  Reader);.  }.  r
2bb22 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2bb23 0a 7d 0a 0a 2f 2a 20 4f 72 64 65 72 20 4c 65 61  .}../* Order Lea
2bb24 76 65 73 52 65 61 64 65 72 73 20 62 79 20 74 68  vesReaders by th
2bb25 65 69 72 20 74 65 72 6d 2c 20 69 67 6e 6f 72 69  eir term, ignori
2bb26 6e 67 20 69 64 78 2e 20 20 52 65 61 64 65 72 73  ng idx.  Readers
2bb27 20 61 74 20 65 6f 66 0a 2a 2a 20 61 6c 77 61 79   at eof.** alway
2bb28 73 20 73 6f 72 74 20 74 6f 20 74 68 65 20 65 6e  s sort to the en
2bb29 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
2bb2a 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72   leavesReaderTer
2bb2b 6d 43 6d 70 28 4c 65 61 76 65 73 52 65 61 64 65  mCmp(LeavesReade
2bb2c 72 20 2a 6c 72 31 2c 20 4c 65 61 76 65 73 52 65  r *lr1, LeavesRe
2bb2d 61 64 65 72 20 2a 6c 72 32 29 7b 0a 20 20 69 66  ader *lr2){.  if
2bb2e 28 20 6c 65 61 76 65 73 52 65 61 64 65 72 41 74  ( leavesReaderAt
2bb2f 45 6e 64 28 6c 72 31 29 20 29 7b 0a 20 20 20 20  End(lr1) ){.    
2bb30 69 66 28 20 6c 65 61 76 65 73 52 65 61 64 65 72  if( leavesReader
2bb31 41 74 45 6e 64 28 6c 72 32 29 20 29 20 72 65 74  AtEnd(lr2) ) ret
2bb32 75 72 6e 20 30 3b 0a 20 20 20 20 72 65 74 75 72  urn 0;.    retur
2bb33 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6c  n 1;.  }.  if( l
2bb34 65 61 76 65 73 52 65 61 64 65 72 41 74 45 6e 64  eavesReaderAtEnd
2bb35 28 6c 72 32 29 20 29 20 72 65 74 75 72 6e 20 2d  (lr2) ) return -
2bb36 31 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6c 65 61  1;..  return lea
2bb37 66 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 26  fReaderTermCmp(&
2bb38 6c 72 31 2d 3e 6c 65 61 66 52 65 61 64 65 72 2c  lr1->leafReader,
2bb39 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2bb3a 20 20 20 20 20 20 20 20 20 20 20 20 6c 65 61 76              leav
2bb3b 65 73 52 65 61 64 65 72 54 65 72 6d 28 6c 72 32  esReaderTerm(lr2
2bb3c 29 2c 20 6c 65 61 76 65 73 52 65 61 64 65 72 54  ), leavesReaderT
2bb3d 65 72 6d 42 79 74 65 73 28 6c 72 32 29 2c 0a 20  ermBytes(lr2),. 
2bb3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bb3f 20 20 20 20 20 20 20 20 20 20 30 29 3b 0a 7d 0a            0);.}.
2bb40 0a 2f 2a 20 53 69 6d 69 6c 61 72 20 74 6f 20 6c  ./* Similar to l
2bb41 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 43  eavesReaderTermC
2bb42 6d 70 28 29 2c 20 77 69 74 68 20 61 64 64 69 74  mp(), with addit
2bb43 69 6f 6e 61 6c 20 6f 72 64 65 72 69 6e 67 20 62  ional ordering b
2bb44 79 20 69 64 78 0a 2a 2a 20 73 6f 20 74 68 61 74  y idx.** so that
2bb45 20 6f 6c 64 65 72 20 73 65 67 6d 65 6e 74 73 20   older segments 
2bb46 73 6f 72 74 20 62 65 66 6f 72 65 20 6e 65 77 65  sort before newe
2bb47 72 20 73 65 67 6d 65 6e 74 73 2e 0a 2a 2f 0a 73  r segments..*/.s
2bb48 74 61 74 69 63 20 69 6e 74 20 6c 65 61 76 65 73  tatic int leaves
2bb49 52 65 61 64 65 72 43 6d 70 28 4c 65 61 76 65 73  ReaderCmp(Leaves
2bb4a 52 65 61 64 65 72 20 2a 6c 72 31 2c 20 4c 65 61  Reader *lr1, Lea
2bb4b 76 65 73 52 65 61 64 65 72 20 2a 6c 72 32 29 7b  vesReader *lr2){
2bb4c 0a 20 20 69 6e 74 20 63 20 3d 20 6c 65 61 76 65  .  int c = leave
2bb4d 73 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 6c  sReaderTermCmp(l
2bb4e 72 31 2c 20 6c 72 32 29 3b 0a 20 20 69 66 28 20  r1, lr2);.  if( 
2bb4f 63 21 3d 30 20 29 20 72 65 74 75 72 6e 20 63 3b  c!=0 ) return c;
2bb50 0a 20 20 72 65 74 75 72 6e 20 6c 72 31 2d 3e 69  .  return lr1->i
2bb51 64 78 2d 6c 72 32 2d 3e 69 64 78 3b 0a 7d 0a 0a  dx-lr2->idx;.}..
2bb52 2f 2a 20 41 73 73 75 6d 65 20 74 68 61 74 20 70  /* Assume that p
2bb53 4c 72 5b 31 5d 2e 2e 70 4c 72 5b 6e 4c 72 5d 20  Lr[1]..pLr[nLr] 
2bb54 61 72 65 20 73 6f 72 74 65 64 2e 20 20 42 75 62  are sorted.  Bub
2bb55 62 6c 65 20 70 4c 72 5b 30 5d 20 69 6e 74 6f 20  ble pLr[0] into 
2bb56 69 74 73 0a 2a 2a 20 73 6f 72 74 65 64 20 70 6f  its.** sorted po
2bb57 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  sition..*/.stati
2bb58 63 20 76 6f 69 64 20 6c 65 61 76 65 73 52 65 61  c void leavesRea
2bb59 64 65 72 52 65 6f 72 64 65 72 28 4c 65 61 76 65  derReorder(Leave
2bb5a 73 52 65 61 64 65 72 20 2a 70 4c 72 2c 20 69 6e  sReader *pLr, in
2bb5b 74 20 6e 4c 72 29 7b 0a 20 20 77 68 69 6c 65 28  t nLr){.  while(
2bb5c 20 6e 4c 72 3e 31 20 26 26 20 6c 65 61 76 65 73   nLr>1 && leaves
2bb5d 52 65 61 64 65 72 43 6d 70 28 70 4c 72 2c 20 70  ReaderCmp(pLr, p
2bb5e 4c 72 2b 31 29 3e 30 20 29 7b 0a 20 20 20 20 4c  Lr+1)>0 ){.    L
2bb5f 65 61 76 65 73 52 65 61 64 65 72 20 74 6d 70 20  eavesReader tmp 
2bb60 3d 20 70 4c 72 5b 30 5d 3b 0a 20 20 20 20 70 4c  = pLr[0];.    pL
2bb61 72 5b 30 5d 20 3d 20 70 4c 72 5b 31 5d 3b 0a 20  r[0] = pLr[1];. 
2bb62 20 20 20 70 4c 72 5b 31 5d 20 3d 20 74 6d 70 3b     pLr[1] = tmp;
2bb63 0a 20 20 20 20 6e 4c 72 2d 2d 3b 0a 20 20 20 20  .    nLr--;.    
2bb64 70 4c 72 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  pLr++;.  }.}../*
2bb65 20 49 6e 69 74 69 61 6c 69 7a 65 73 20 70 52 65   Initializes pRe
2bb66 61 64 65 72 73 20 77 69 74 68 20 74 68 65 20 73  aders with the s
2bb67 65 67 6d 65 6e 74 73 20 66 72 6f 6d 20 6c 65 76  egments from lev
2bb68 65 6c 20 69 4c 65 76 65 6c 2c 20 72 65 74 75 72  el iLevel, retur
2bb69 6e 69 6e 67 0a 2a 2a 20 74 68 65 20 6e 75 6d 62  ning.** the numb
2bb6a 65 72 20 6f 66 20 73 65 67 6d 65 6e 74 73 20 69  er of segments i
2bb6b 6e 20 2a 70 69 52 65 61 64 65 72 73 2e 20 20 4c  n *piReaders.  L
2bb6c 65 61 76 65 73 20 70 52 65 61 64 65 72 73 20 69  eaves pReaders i
2bb6d 6e 20 73 6f 72 74 65 64 0a 2a 2a 20 6f 72 64 65  n sorted.** orde
2bb6e 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
2bb6f 20 6c 65 61 76 65 73 52 65 61 64 65 72 73 49 6e   leavesReadersIn
2bb70 69 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  it(fulltext_vtab
2bb71 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 2c   *v, int iLevel,
2bb72 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2bb73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4c 65                Le
2bb74 61 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61  avesReader *pRea
2bb75 64 65 72 73 2c 20 69 6e 74 20 2a 70 69 52 65 61  ders, int *piRea
2bb76 64 65 72 73 29 7b 0a 20 20 73 71 6c 69 74 65 33  ders){.  sqlite3
2bb77 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20  _stmt *s;.  int 
2bb78 69 2c 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f  i, rc = sql_get_
2bb79 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47  statement(v, SEG
2bb7a 44 49 52 5f 53 45 4c 45 43 54 5f 53 54 4d 54 2c  DIR_SELECT_STMT,
2bb7b 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d   &s);.  if( rc!=
2bb7c 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
2bb7d 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73  rn rc;..  rc = s
2bb7e 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28  qlite3_bind_int(
2bb7f 73 2c 20 31 2c 20 69 4c 65 76 65 6c 29 3b 0a 20  s, 1, iLevel);. 
2bb80 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2bb81 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
2bb82 0a 20 20 69 20 3d 20 30 3b 0a 20 20 77 68 69 6c  .  i = 0;.  whil
2bb83 65 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33  e( (rc = sqlite3
2bb84 5f 73 74 65 70 28 73 29 29 3d 3d 53 51 4c 49 54  _step(s))==SQLIT
2bb85 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 73 71 6c  E_ROW ){.    sql
2bb86 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61 72 74  ite_int64 iStart
2bb87 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
2bb88 6e 5f 69 6e 74 36 34 28 73 2c 20 30 29 3b 0a 20  n_int64(s, 0);. 
2bb89 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
2bb8a 69 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 5f 63  iEnd = sqlite3_c
2bb8b 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31  olumn_int64(s, 1
2bb8c 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  );.    const cha
2bb8d 72 20 2a 70 52 6f 6f 74 44 61 74 61 20 3d 20 73  r *pRootData = s
2bb8e 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
2bb8f 6f 62 28 73 2c 20 32 29 3b 0a 20 20 20 20 69 6e  ob(s, 2);.    in
2bb90 74 20 6e 52 6f 6f 74 44 61 74 61 20 3d 20 73 71  t nRootData = sq
2bb91 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
2bb92 65 73 28 73 2c 20 32 29 3b 0a 0a 20 20 20 20 61  es(s, 2);..    a
2bb93 73 73 65 72 74 28 20 69 3c 4d 45 52 47 45 5f 43  ssert( i<MERGE_C
2bb94 4f 55 4e 54 20 29 3b 0a 20 20 20 20 72 63 20 3d  OUNT );.    rc =
2bb95 20 6c 65 61 76 65 73 52 65 61 64 65 72 49 6e 69   leavesReaderIni
2bb96 74 28 76 2c 20 69 2c 20 69 53 74 61 72 74 2c 20  t(v, i, iStart, 
2bb97 69 45 6e 64 2c 20 70 52 6f 6f 74 44 61 74 61 2c  iEnd, pRootData,
2bb98 20 6e 52 6f 6f 74 44 61 74 61 2c 0a 20 20 20 20   nRootData,.    
2bb99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bb9a 20 20 20 20 20 20 26 70 52 65 61 64 65 72 73 5b        &pReaders[
2bb9b 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  i]);.    if( rc!
2bb9c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65  =SQLITE_OK ) bre
2bb9d 61 6b 3b 0a 0a 20 20 20 20 69 2b 2b 3b 0a 20 20  ak;..    i++;.  
2bb9e 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  }.  if( rc!=SQLI
2bb9f 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 77  TE_DONE ){.    w
2bba0 68 69 6c 65 28 20 69 2d 2d 3e 30 20 29 7b 0a 20  hile( i-->0 ){. 
2bba1 20 20 20 20 20 6c 65 61 76 65 73 52 65 61 64 65       leavesReade
2bba2 72 44 65 73 74 72 6f 79 28 26 70 52 65 61 64 65  rDestroy(&pReade
2bba3 72 73 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20  rs[i]);.    }.  
2bba4 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
2bba5 0a 0a 20 20 2a 70 69 52 65 61 64 65 72 73 20 3d  ..  *piReaders =
2bba6 20 69 3b 0a 0a 20 20 2f 2a 20 4c 65 61 76 65 20   i;..  /* Leave 
2bba7 6f 75 72 20 72 65 73 75 6c 74 73 20 73 6f 72 74  our results sort
2bba8 65 64 20 62 79 20 74 65 72 6d 2c 20 74 68 65 6e  ed by term, then
2bba9 20 61 67 65 2e 20 2a 2f 0a 20 20 77 68 69 6c 65   age. */.  while
2bbaa 28 20 69 2d 2d 20 29 7b 0a 20 20 20 20 6c 65 61  ( i-- ){.    lea
2bbab 76 65 73 52 65 61 64 65 72 52 65 6f 72 64 65 72  vesReaderReorder
2bbac 28 70 52 65 61 64 65 72 73 2b 69 2c 20 2a 70 69  (pReaders+i, *pi
2bbad 52 65 61 64 65 72 73 2d 69 29 3b 0a 20 20 7d 0a  Readers-i);.  }.
2bbae 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2bbaf 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4d 65 72 67 65 20  OK;.}../* Merge 
2bbb0 64 6f 63 6c 69 73 74 73 20 66 72 6f 6d 20 70 52  doclists from pR
2bbb1 65 61 64 65 72 73 5b 6e 52 65 61 64 65 72 73 5d  eaders[nReaders]
2bbb2 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 64   into a single d
2bbb3 6f 63 6c 69 73 74 2c 20 77 68 69 63 68 0a 2a 2a  oclist, which.**
2bbb4 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 70   is written to p
2bbb5 57 72 69 74 65 72 2e 20 20 41 73 73 75 6d 65 73  Writer.  Assumes
2bbb6 20 70 52 65 61 64 65 72 73 20 69 73 20 6f 72 64   pReaders is ord
2bbb7 65 72 65 64 20 6f 6c 64 65 73 74 20 74 6f 0a 2a  ered oldest to.*
2bbb8 2a 20 6e 65 77 65 73 74 2e 0a 2a 2f 0a 2f 2a 20  * newest..*/./* 
2bbb9 54 4f 44 4f 28 73 68 65 73 73 29 20 43 6f 6e 73  TODO(shess) Cons
2bbba 69 64 65 72 20 70 75 74 74 69 6e 67 20 74 68 69  ider putting thi
2bbbb 73 20 69 6e 6c 69 6e 65 20 69 6e 20 73 65 67 6d  s inline in segm
2bbbc 65 6e 74 4d 65 72 67 65 28 29 2e 20 2a 2f 0a 73  entMerge(). */.s
2bbbd 74 61 74 69 63 20 69 6e 74 20 6c 65 61 76 65 73  tatic int leaves
2bbbe 52 65 61 64 65 72 73 4d 65 72 67 65 28 66 75 6c  ReadersMerge(ful
2bbbf 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 0a 20  ltext_vtab *v,. 
2bbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bbc1 20 20 20 20 20 20 20 20 20 20 20 20 20 4c 65 61               Lea
2bbc2 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64  vesReader *pRead
2bbc3 65 72 73 2c 20 69 6e 74 20 6e 52 65 61 64 65 72  ers, int nReader
2bbc4 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  s,.             
2bbc5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bbc6 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57 72   LeafWriter *pWr
2bbc7 69 74 65 72 29 7b 0a 20 20 44 4c 52 65 61 64 65  iter){.  DLReade
2bbc8 72 20 64 6c 52 65 61 64 65 72 73 5b 4d 45 52 47  r dlReaders[MERG
2bbc9 45 5f 43 4f 55 4e 54 5d 3b 0a 20 20 63 6f 6e 73  E_COUNT];.  cons
2bbca 74 20 63 68 61 72 20 2a 70 54 65 72 6d 20 3d 20  t char *pTerm = 
2bbcb 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d  leavesReaderTerm
2bbcc 28 70 52 65 61 64 65 72 73 29 3b 0a 20 20 69 6e  (pReaders);.  in
2bbcd 74 20 69 2c 20 6e 54 65 72 6d 20 3d 20 6c 65 61  t i, nTerm = lea
2bbce 76 65 73 52 65 61 64 65 72 54 65 72 6d 42 79 74  vesReaderTermByt
2bbcf 65 73 28 70 52 65 61 64 65 72 73 29 3b 0a 0a 20  es(pReaders);.. 
2bbd0 20 61 73 73 65 72 74 28 20 6e 52 65 61 64 65 72   assert( nReader
2bbd1 73 3c 3d 4d 45 52 47 45 5f 43 4f 55 4e 54 20 29  s<=MERGE_COUNT )
2bbd2 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  ;..  for(i=0; i<
2bbd3 6e 52 65 61 64 65 72 73 3b 20 69 2b 2b 29 7b 0a  nReaders; i++){.
2bbd4 20 20 20 20 64 6c 72 49 6e 69 74 28 26 64 6c 52      dlrInit(&dlR
2bbd5 65 61 64 65 72 73 5b 69 5d 2c 20 44 4c 5f 44 45  eaders[i], DL_DE
2bbd6 46 41 55 4c 54 2c 0a 20 20 20 20 20 20 20 20 20  FAULT,.         
2bbd7 20 20 20 6c 65 61 76 65 73 52 65 61 64 65 72 44     leavesReaderD
2bbd8 61 74 61 28 70 52 65 61 64 65 72 73 2b 69 29 2c  ata(pReaders+i),
2bbd9 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 65 61  .            lea
2bbda 76 65 73 52 65 61 64 65 72 44 61 74 61 42 79 74  vesReaderDataByt
2bbdb 65 73 28 70 52 65 61 64 65 72 73 2b 69 29 29 3b  es(pReaders+i));
2bbdc 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 6c  .  }..  return l
2bbdd 65 61 66 57 72 69 74 65 72 53 74 65 70 4d 65 72  eafWriterStepMer
2bbde 67 65 28 76 2c 20 70 57 72 69 74 65 72 2c 20 70  ge(v, pWriter, p
2bbdf 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 64 6c 52  Term, nTerm, dlR
2bbe0 65 61 64 65 72 73 2c 20 6e 52 65 61 64 65 72 73  eaders, nReaders
2bbe1 29 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  );.}../* Forward
2bbe2 20 72 65 66 20 64 75 65 20 74 6f 20 6d 75 74 75   ref due to mutu
2bbe3 61 6c 20 72 65 63 75 72 73 69 6f 6e 20 77 69 74  al recursion wit
2bbe4 68 20 73 65 67 64 69 72 4e 65 78 74 49 6e 64 65  h segdirNextInde
2bbe5 78 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69  x(). */.static i
2bbe6 6e 74 20 73 65 67 6d 65 6e 74 4d 65 72 67 65 28  nt segmentMerge(
2bbe7 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
2bbe8 2c 20 69 6e 74 20 69 4c 65 76 65 6c 29 3b 0a 0a  , int iLevel);..
2bbe9 2f 2a 20 50 75 74 20 74 68 65 20 6e 65 78 74 20  /* Put the next 
2bbea 61 76 61 69 6c 61 62 6c 65 20 69 6e 64 65 78 20  available index 
2bbeb 61 74 20 69 4c 65 76 65 6c 20 69 6e 74 6f 20 2a  at iLevel into *
2bbec 70 69 64 78 2e 20 20 49 66 20 69 4c 65 76 65 6c  pidx.  If iLevel
2bbed 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 61 73 20  .** already has 
2bbee 4d 45 52 47 45 5f 43 4f 55 4e 54 20 73 65 67 6d  MERGE_COUNT segm
2bbef 65 6e 74 73 2c 20 74 68 65 79 20 61 72 65 20 6d  ents, they are m
2bbf0 65 72 67 65 64 20 74 6f 20 61 20 68 69 67 68 65  erged to a highe
2bbf1 72 0a 2a 2a 20 6c 65 76 65 6c 20 74 6f 20 6d 61  r.** level to ma
2bbf2 6b 65 20 72 6f 6f 6d 2e 0a 2a 2f 0a 73 74 61 74  ke room..*/.stat
2bbf3 69 63 20 69 6e 74 20 73 65 67 64 69 72 4e 65 78  ic int segdirNex
2bbf4 74 49 6e 64 65 78 28 66 75 6c 6c 74 65 78 74 5f  tIndex(fulltext_
2bbf5 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c 65  vtab *v, int iLe
2bbf6 76 65 6c 2c 20 69 6e 74 20 2a 70 69 64 78 29 7b  vel, int *pidx){
2bbf7 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 65 67 64  .  int rc = segd
2bbf8 69 72 5f 6d 61 78 5f 69 6e 64 65 78 28 76 2c 20  ir_max_index(v, 
2bbf9 69 4c 65 76 65 6c 2c 20 70 69 64 78 29 3b 0a 20  iLevel, pidx);. 
2bbfa 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
2bbfb 44 4f 4e 45 20 29 7b 20 20 20 20 20 20 20 20 20  DONE ){         
2bbfc 20 20 20 20 20 2f 2a 20 4e 6f 20 73 65 67 6d 65       /* No segme
2bbfd 6e 74 73 20 61 74 20 69 4c 65 76 65 6c 2e 20 2a  nts at iLevel. *
2bbfe 2f 0a 20 20 20 20 2a 70 69 64 78 20 3d 20 30 3b  /.    *pidx = 0;
2bbff 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d  .  }else if( rc=
2bc00 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
2bc01 20 20 20 69 66 28 20 2a 70 69 64 78 3d 3d 28 4d     if( *pidx==(M
2bc02 45 52 47 45 5f 43 4f 55 4e 54 2d 31 29 20 29 7b  ERGE_COUNT-1) ){
2bc03 0a 20 20 20 20 20 20 72 63 20 3d 20 73 65 67 6d  .      rc = segm
2bc04 65 6e 74 4d 65 72 67 65 28 76 2c 20 69 4c 65 76  entMerge(v, iLev
2bc05 65 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  el);.      if( r
2bc06 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
2bc07 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
2bc08 2a 70 69 64 78 20 3d 20 30 3b 0a 20 20 20 20 7d  *pidx = 0;.    }
2bc09 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 2a 70 69  else{.      (*pi
2bc0a 64 78 29 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  dx)++;.    }.  }
2bc0b 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
2bc0c 20 72 63 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   rc;.  }.  retur
2bc0d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
2bc0e 2f 2a 20 4d 65 72 67 65 20 4d 45 52 47 45 5f 43  /* Merge MERGE_C
2bc0f 4f 55 4e 54 20 73 65 67 6d 65 6e 74 73 20 61 74  OUNT segments at
2bc10 20 69 4c 65 76 65 6c 20 69 6e 74 6f 20 61 20 6e   iLevel into a n
2bc11 65 77 20 73 65 67 6d 65 6e 74 20 61 74 0a 2a 2a  ew segment at.**
2bc12 20 69 4c 65 76 65 6c 2b 31 2e 20 20 49 66 20 69   iLevel+1.  If i
2bc13 4c 65 76 65 6c 2b 31 20 69 73 20 61 6c 72 65 61  Level+1 is alrea
2bc14 64 79 20 66 75 6c 6c 20 6f 66 20 73 65 67 6d 65  dy full of segme
2bc15 6e 74 73 2c 20 74 68 6f 73 65 20 77 69 6c 6c 20  nts, those will 
2bc16 62 65 0a 2a 2a 20 6d 65 72 67 65 64 20 74 6f 20  be.** merged to 
2bc17 6d 61 6b 65 20 72 6f 6f 6d 2e 0a 2a 2f 0a 73 74  make room..*/.st
2bc18 61 74 69 63 20 69 6e 74 20 73 65 67 6d 65 6e 74  atic int segment
2bc19 4d 65 72 67 65 28 66 75 6c 6c 74 65 78 74 5f 76  Merge(fulltext_v
2bc1a 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76  tab *v, int iLev
2bc1b 65 6c 29 7b 0a 20 20 4c 65 61 66 57 72 69 74 65  el){.  LeafWrite
2bc1c 72 20 77 72 69 74 65 72 3b 0a 20 20 4c 65 61 76  r writer;.  Leav
2bc1d 65 73 52 65 61 64 65 72 20 6c 72 73 5b 4d 45 52  esReader lrs[MER
2bc1e 47 45 5f 43 4f 55 4e 54 5d 3b 0a 20 20 69 6e 74  GE_COUNT];.  int
2bc1f 20 69 2c 20 72 63 2c 20 69 64 78 20 3d 20 30 3b   i, rc, idx = 0;
2bc20 0a 0a 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65  ..  /* Determine
2bc21 20 74 68 65 20 6e 65 78 74 20 61 76 61 69 6c 61   the next availa
2bc22 62 6c 65 20 73 65 67 6d 65 6e 74 20 69 6e 64 65  ble segment inde
2bc23 78 20 61 74 20 74 68 65 20 6e 65 78 74 20 6c 65  x at the next le
2bc24 76 65 6c 2c 0a 20 20 2a 2a 20 6d 65 72 67 69 6e  vel,.  ** mergin
2bc25 67 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a  g as necessary..
2bc26 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 65 67 64    */.  rc = segd
2bc27 69 72 4e 65 78 74 49 6e 64 65 78 28 76 2c 20 69  irNextIndex(v, i
2bc28 4c 65 76 65 6c 2b 31 2c 20 26 69 64 78 29 3b 0a  Level+1, &idx);.
2bc29 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2bc2a 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
2bc2b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73  ..  /* TODO(shes
2bc2c 73 29 20 54 68 69 73 20 61 73 73 75 6d 65 73 20  s) This assumes 
2bc2d 74 68 61 74 20 77 65 27 6c 6c 20 61 6c 77 61 79  that we'll alway
2bc2e 73 20 73 65 65 20 65 78 61 63 74 6c 79 0a 20 20  s see exactly.  
2bc2f 2a 2a 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 73  ** MERGE_COUNT s
2bc30 65 67 6d 65 6e 74 73 20 74 6f 20 6d 65 72 67 65  egments to merge
2bc31 20 61 74 20 61 20 67 69 76 65 6e 20 6c 65 76 65   at a given leve
2bc32 6c 2e 20 20 54 68 61 74 20 77 69 6c 6c 20 62 65  l.  That will be
2bc33 0a 20 20 2a 2a 20 62 72 6f 6b 65 6e 20 69 66 20  .  ** broken if 
2bc34 77 65 20 61 6c 6c 6f 77 20 74 68 65 20 64 65 76  we allow the dev
2bc35 65 6c 6f 70 65 72 20 74 6f 20 72 65 71 75 65 73  eloper to reques
2bc36 74 20 70 72 65 65 6d 70 74 69 76 65 20 6f 72 0a  t preemptive or.
2bc37 20 20 2a 2a 20 64 65 66 65 72 72 65 64 20 6d 65    ** deferred me
2bc38 72 67 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 6d 65  rging..  */.  me
2bc39 6d 73 65 74 28 26 6c 72 73 2c 20 27 5c 30 27 2c  mset(&lrs, '\0',
2bc3a 20 73 69 7a 65 6f 66 28 6c 72 73 29 29 3b 0a 20   sizeof(lrs));. 
2bc3b 20 72 63 20 3d 20 6c 65 61 76 65 73 52 65 61 64   rc = leavesRead
2bc3c 65 72 73 49 6e 69 74 28 76 2c 20 69 4c 65 76 65  ersInit(v, iLeve
2bc3d 6c 2c 20 6c 72 73 2c 20 26 69 29 3b 0a 20 20 69  l, lrs, &i);.  i
2bc3e 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2bc3f 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
2bc40 61 73 73 65 72 74 28 20 69 3d 3d 4d 45 52 47 45  assert( i==MERGE
2bc41 5f 43 4f 55 4e 54 20 29 3b 0a 0a 20 20 6c 65 61  _COUNT );..  lea
2bc42 66 57 72 69 74 65 72 49 6e 69 74 28 69 4c 65 76  fWriterInit(iLev
2bc43 65 6c 2b 31 2c 20 69 64 78 2c 20 26 77 72 69 74  el+1, idx, &writ
2bc44 65 72 29 3b 0a 0a 20 20 2f 2a 20 53 69 6e 63 65  er);..  /* Since
2bc45 20 6c 65 61 76 65 73 52 65 61 64 65 72 52 65 6f   leavesReaderReo
2bc46 72 64 65 72 28 29 20 70 75 73 68 65 73 20 72 65  rder() pushes re
2bc47 61 64 65 72 73 20 61 74 20 65 6f 66 20 74 6f 20  aders at eof to 
2bc48 74 68 65 20 65 6e 64 2c 0a 20 20 2a 2a 20 77 68  the end,.  ** wh
2bc49 65 6e 20 74 68 65 20 66 69 72 73 74 20 72 65 61  en the first rea
2bc4a 64 65 72 20 69 73 20 65 6d 70 74 79 2c 20 61 6c  der is empty, al
2bc4b 6c 20 77 69 6c 6c 20 62 65 20 65 6d 70 74 79 2e  l will be empty.
2bc4c 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 21  .  */.  while( !
2bc4d 6c 65 61 76 65 73 52 65 61 64 65 72 41 74 45 6e  leavesReaderAtEn
2bc4e 64 28 6c 72 73 29 20 29 7b 0a 20 20 20 20 2f 2a  d(lrs) ){.    /*
2bc4f 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20   Figure out how 
2bc50 6d 61 6e 79 20 72 65 61 64 65 72 73 20 73 68 61  many readers sha
2bc51 72 65 20 74 68 65 69 72 20 6e 65 78 74 20 74 65  re their next te
2bc52 72 6d 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69  rm. */.    for(i
2bc53 3d 31 3b 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e  =1; i<MERGE_COUN
2bc54 54 20 26 26 20 21 6c 65 61 76 65 73 52 65 61 64  T && !leavesRead
2bc55 65 72 41 74 45 6e 64 28 6c 72 73 2b 69 29 3b 20  erAtEnd(lrs+i); 
2bc56 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
2bc57 30 21 3d 6c 65 61 76 65 73 52 65 61 64 65 72 54  0!=leavesReaderT
2bc58 65 72 6d 43 6d 70 28 6c 72 73 2c 20 6c 72 73 2b  ermCmp(lrs, lrs+
2bc59 69 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  i) ) break;.    
2bc5a 7d 0a 0a 20 20 20 20 72 63 20 3d 20 6c 65 61 76  }..    rc = leav
2bc5b 65 73 52 65 61 64 65 72 73 4d 65 72 67 65 28 76  esReadersMerge(v
2bc5c 2c 20 6c 72 73 2c 20 69 2c 20 26 77 72 69 74 65  , lrs, i, &write
2bc5d 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  r);.    if( rc!=
2bc5e 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
2bc5f 20 65 72 72 3b 0a 0a 20 20 20 20 2f 2a 20 53 74   err;..    /* St
2bc60 65 70 20 66 6f 72 77 61 72 64 20 74 68 6f 73 65  ep forward those
2bc61 20 74 68 61 74 20 77 65 72 65 20 6d 65 72 67 65   that were merge
2bc62 64 2e 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28  d. */.    while(
2bc63 20 69 2d 2d 3e 30 20 29 7b 0a 20 20 20 20 20 20   i-->0 ){.      
2bc64 72 63 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65  rc = leavesReade
2bc65 72 53 74 65 70 28 76 2c 20 6c 72 73 2b 69 29 3b  rStep(v, lrs+i);
2bc66 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
2bc67 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
2bc68 65 72 72 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 52  err;..      /* R
2bc69 65 6f 72 64 65 72 20 62 79 20 74 65 72 6d 2c 20  eorder by term, 
2bc6a 74 68 65 6e 20 62 79 20 61 67 65 2e 20 2a 2f 0a  then by age. */.
2bc6b 20 20 20 20 20 20 6c 65 61 76 65 73 52 65 61 64        leavesRead
2bc6c 65 72 52 65 6f 72 64 65 72 28 6c 72 73 2b 69 2c  erReorder(lrs+i,
2bc6d 20 4d 45 52 47 45 5f 43 4f 55 4e 54 2d 69 29 3b   MERGE_COUNT-i);
2bc6e 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 66 6f  .    }.  }..  fo
2bc6f 72 28 69 3d 30 3b 20 69 3c 4d 45 52 47 45 5f 43  r(i=0; i<MERGE_C
2bc70 4f 55 4e 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  OUNT; i++){.    
2bc71 6c 65 61 76 65 73 52 65 61 64 65 72 44 65 73 74  leavesReaderDest
2bc72 72 6f 79 28 26 6c 72 73 5b 69 5d 29 3b 0a 20 20  roy(&lrs[i]);.  
2bc73 7d 0a 0a 20 20 72 63 20 3d 20 6c 65 61 66 57 72  }..  rc = leafWr
2bc74 69 74 65 72 46 69 6e 61 6c 69 7a 65 28 76 2c 20  iterFinalize(v, 
2bc75 26 77 72 69 74 65 72 29 3b 0a 20 20 6c 65 61 66  &writer);.  leaf
2bc76 57 72 69 74 65 72 44 65 73 74 72 6f 79 28 26 77  WriterDestroy(&w
2bc77 72 69 74 65 72 29 3b 0a 20 20 69 66 28 20 72 63  riter);.  if( rc
2bc78 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
2bc79 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 44  turn rc;..  /* D
2bc7a 65 6c 65 74 65 20 74 68 65 20 6d 65 72 67 65 64  elete the merged
2bc7b 20 73 65 67 6d 65 6e 74 20 64 61 74 61 2e 20 2a   segment data. *
2bc7c 2f 0a 20 20 72 65 74 75 72 6e 20 73 65 67 64 69  /.  return segdi
2bc7d 72 5f 64 65 6c 65 74 65 28 76 2c 20 69 4c 65 76  r_delete(v, iLev
2bc7e 65 6c 29 3b 0a 0a 20 65 72 72 3a 0a 20 20 66 6f  el);.. err:.  fo
2bc7f 72 28 69 3d 30 3b 20 69 3c 4d 45 52 47 45 5f 43  r(i=0; i<MERGE_C
2bc80 4f 55 4e 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  OUNT; i++){.    
2bc81 6c 65 61 76 65 73 52 65 61 64 65 72 44 65 73 74  leavesReaderDest
2bc82 72 6f 79 28 26 6c 72 73 5b 69 5d 29 3b 0a 20 20  roy(&lrs[i]);.  
2bc83 7d 0a 20 20 6c 65 61 66 57 72 69 74 65 72 44 65  }.  leafWriterDe
2bc84 73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a  stroy(&writer);.
2bc85 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
2bc86 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 74 68  /* Accumulate th
2bc87 65 20 75 6e 69 6f 6e 20 6f 66 20 2a 61 63 63 20  e union of *acc 
2bc88 61 6e 64 20 2a 70 44 61 74 61 20 69 6e 74 6f 20  and *pData into 
2bc89 2a 61 63 63 2e 20 2a 2f 0a 73 74 61 74 69 63 20  *acc. */.static 
2bc8a 76 6f 69 64 20 64 6f 63 4c 69 73 74 41 63 63 75  void docListAccu
2bc8b 6d 75 6c 61 74 65 55 6e 69 6f 6e 28 44 61 74 61  mulateUnion(Data
2bc8c 42 75 66 66 65 72 20 2a 61 63 63 2c 0a 20 20 20  Buffer *acc,.   
2bc8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bc8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bc8f 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
2bc90 61 2c 20 69 6e 74 20 6e 44 61 74 61 29 20 7b 0a  a, int nData) {.
2bc91 20 20 44 61 74 61 42 75 66 66 65 72 20 74 6d 70    DataBuffer tmp
2bc92 20 3d 20 2a 61 63 63 3b 0a 20 20 64 61 74 61 42   = *acc;.  dataB
2bc93 75 66 66 65 72 49 6e 69 74 28 61 63 63 2c 20 74  ufferInit(acc, t
2bc94 6d 70 2e 6e 44 61 74 61 2b 6e 44 61 74 61 29 3b  mp.nData+nData);
2bc95 0a 20 20 64 6f 63 4c 69 73 74 55 6e 69 6f 6e 28  .  docListUnion(
2bc96 74 6d 70 2e 70 44 61 74 61 2c 20 74 6d 70 2e 6e  tmp.pData, tmp.n
2bc97 44 61 74 61 2c 20 70 44 61 74 61 2c 20 6e 44 61  Data, pData, nDa
2bc98 74 61 2c 20 61 63 63 29 3b 0a 20 20 64 61 74 61  ta, acc);.  data
2bc99 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 74  BufferDestroy(&t
2bc9a 6d 70 29 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44 4f 28  mp);.}../* TODO(
2bc9b 73 68 65 73 73 29 20 49 74 20 6d 69 67 68 74 20  shess) It might 
2bc9c 62 65 20 69 6e 74 65 72 65 73 74 69 6e 67 20 74  be interesting t
2bc9d 6f 20 65 78 70 6c 6f 72 65 20 64 69 66 66 65 72  o explore differ
2bc9e 65 6e 74 20 6d 65 72 67 65 0a 2a 2a 20 73 74 72  ent merge.** str
2bc9f 61 74 65 67 69 65 73 2c 20 68 65 72 65 2e 20 20  ategies, here.  
2bca0 46 6f 72 20 69 6e 73 74 61 6e 63 65 2c 20 73 69  For instance, si
2bca1 6e 63 65 20 74 68 69 73 20 69 73 20 61 20 73 6f  nce this is a so
2bca2 72 74 65 64 20 6d 65 72 67 65 2c 20 77 65 0a 2a  rted merge, we.*
2bca3 2a 20 63 6f 75 6c 64 20 65 61 73 69 6c 79 20 6d  * could easily m
2bca4 65 72 67 65 20 6d 61 6e 79 20 64 6f 63 6c 69 73  erge many doclis
2bca5 74 73 20 69 6e 20 70 61 72 61 6c 6c 65 6c 2e 20  ts in parallel. 
2bca6 20 57 69 74 68 20 73 6f 6d 65 0a 2a 2a 20 63 6f   With some.** co
2bca7 6d 70 72 65 68 65 6e 73 69 6f 6e 20 6f 66 20 74  mprehension of t
2bca8 68 65 20 73 74 6f 72 61 67 65 20 66 6f 72 6d 61  he storage forma
2bca9 74 2c 20 77 65 20 63 6f 75 6c 64 20 6d 65 72 67  t, we could merg
2bcaa 65 20 61 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a 20  e all of the.** 
2bcab 64 6f 63 6c 69 73 74 73 20 77 69 74 68 69 6e 20  doclists within 
2bcac 61 20 6c 65 61 66 20 6e 6f 64 65 20 64 69 72 65  a leaf node dire
2bcad 63 74 6c 79 20 66 72 6f 6d 20 74 68 65 20 6c 65  ctly from the le
2bcae 61 66 20 6e 6f 64 65 27 73 20 73 74 6f 72 61 67  af node's storag
2bcaf 65 2e 0a 2a 2a 20 49 74 20 6d 61 79 20 62 65 20  e..** It may be 
2bcb0 77 6f 72 74 68 77 68 69 6c 65 20 74 6f 20 6d 65  worthwhile to me
2bcb1 72 67 65 20 73 6d 61 6c 6c 65 72 20 64 6f 63 6c  rge smaller docl
2bcb2 69 73 74 73 20 62 65 66 6f 72 65 20 6c 61 72 67  ists before larg
2bcb3 65 72 0a 2a 2a 20 64 6f 63 6c 69 73 74 73 2c 20  er.** doclists, 
2bcb4 73 69 6e 63 65 20 74 68 65 79 20 63 61 6e 20 62  since they can b
2bcb5 65 20 74 72 61 76 65 72 73 65 64 20 6d 6f 72 65  e traversed more
2bcb6 20 71 75 69 63 6b 6c 79 20 2d 20 62 75 74 20 74   quickly - but t
2bcb7 68 65 0a 2a 2a 20 72 65 73 75 6c 74 73 20 6d 61  he.** results ma
2bcb8 79 20 68 61 76 65 20 6c 65 73 73 20 6f 76 65 72  y have less over
2bcb9 6c 61 70 2c 20 6d 61 6b 69 6e 67 20 74 68 65 6d  lap, making them
2bcba 20 6d 6f 72 65 20 65 78 70 65 6e 73 69 76 65 20   more expensive 
2bcbb 69 6e 20 61 0a 2a 2a 20 64 69 66 66 65 72 65 6e  in a.** differen
2bcbc 74 20 77 61 79 2e 0a 2a 2f 0a 0a 2f 2a 20 53 63  t way..*/../* Sc
2bcbd 61 6e 20 70 52 65 61 64 65 72 20 66 6f 72 20 70  an pReader for p
2bcbe 54 65 72 6d 2f 6e 54 65 72 6d 2c 20 61 6e 64 20  Term/nTerm, and 
2bcbf 6d 65 72 67 65 20 74 68 65 20 74 65 72 6d 27 73  merge the term's
2bcc0 20 64 6f 63 6c 69 73 74 20 6f 76 65 72 0a 2a 2a   doclist over.**
2bcc1 20 2a 6f 75 74 20 28 61 6e 79 20 64 6f 63 6c 69   *out (any docli
2bcc2 73 74 73 20 77 69 74 68 20 64 75 70 6c 69 63 61  sts with duplica
2bcc3 74 65 20 64 6f 63 69 64 73 20 6f 76 65 72 77 72  te docids overwr
2bcc4 69 74 65 20 74 68 6f 73 65 20 69 6e 20 2a 6f 75  ite those in *ou
2bcc5 74 29 2e 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20  t)..** Internal 
2bcc6 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 6c 6f 61  function for loa
2bcc7 64 53 65 67 6d 65 6e 74 4c 65 61 66 28 29 2e 0a  dSegmentLeaf()..
2bcc8 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f  */.static int lo
2bcc9 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73 49  adSegmentLeavesI
2bcca 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  nt(fulltext_vtab
2bccb 20 2a 76 2c 20 4c 65 61 76 65 73 52 65 61 64 65   *v, LeavesReade
2bccc 72 20 2a 70 52 65 61 64 65 72 2c 0a 20 20 20 20  r *pReader,.    
2bccd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bcce 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
2bccf 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69  t char *pTerm, i
2bcd0 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73  nt nTerm, int is
2bcd1 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20  Prefix,.        
2bcd2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bcd3 20 20 20 20 20 20 20 20 44 61 74 61 42 75 66 66          DataBuff
2bcd4 65 72 20 2a 6f 75 74 29 7b 0a 20 20 2f 2a 20 64  er *out){.  /* d
2bcd5 6f 63 6c 69 73 74 20 64 61 74 61 20 69 73 20 61  oclist data is a
2bcd6 63 63 75 6d 75 6c 61 74 65 64 20 69 6e 74 6f 20  ccumulated into 
2bcd7 70 42 75 66 66 65 72 73 20 73 69 6d 69 6c 61 72  pBuffers similar
2bcd8 20 74 6f 20 68 6f 77 20 6f 6e 65 20 64 6f 65 73   to how one does
2bcd9 0a 20 20 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 20  .  ** increment 
2bcda 69 6e 20 62 69 6e 61 72 79 20 61 72 69 74 68 6d  in binary arithm
2bcdb 65 74 69 63 2e 20 20 49 66 20 69 6e 64 65 78 20  etic.  If index 
2bcdc 30 20 69 73 20 65 6d 70 74 79 2c 20 74 68 65 20  0 is empty, the 
2bcdd 64 61 74 61 20 69 73 0a 20 20 2a 2a 20 73 74 6f  data is.  ** sto
2bcde 72 65 64 20 74 68 65 72 65 2e 20 20 49 66 20 74  red there.  If t
2bcdf 68 65 72 65 20 69 73 20 64 61 74 61 20 74 68 65  here is data the
2bce0 72 65 2c 20 69 74 20 69 73 20 6d 65 72 67 65 64  re, it is merged
2bce1 20 61 6e 64 20 74 68 65 0a 20 20 2a 2a 20 72 65   and the.  ** re
2bce2 73 75 6c 74 73 20 63 61 72 72 69 65 64 20 69 6e  sults carried in
2bce3 74 6f 20 70 6f 73 69 74 69 6f 6e 20 31 2c 20 77  to position 1, w
2bce4 69 74 68 20 66 75 72 74 68 65 72 20 6d 65 72 67  ith further merg
2bce5 65 2d 61 6e 64 2d 63 61 72 72 79 0a 20 20 2a 2a  e-and-carry.  **
2bce6 20 75 6e 74 69 6c 20 61 6e 20 65 6d 70 74 79 20   until an empty 
2bce7 70 6f 73 69 74 69 6f 6e 20 69 73 20 66 6f 75 6e  position is foun
2bce8 64 2e 0a 20 20 2a 2f 0a 20 20 44 61 74 61 42 75  d..  */.  DataBu
2bce9 66 66 65 72 20 2a 70 42 75 66 66 65 72 73 20 3d  ffer *pBuffers =
2bcea 20 4e 55 4c 4c 3b 0a 20 20 69 6e 74 20 6e 42 75   NULL;.  int nBu
2bceb 66 66 65 72 73 20 3d 20 30 2c 20 6e 4d 61 78 42  ffers = 0, nMaxB
2bcec 75 66 66 65 72 73 20 3d 20 30 2c 20 72 63 3b 0a  uffers = 0, rc;.
2bced 0a 20 20 61 73 73 65 72 74 28 20 6e 54 65 72 6d  .  assert( nTerm
2bcee 3e 30 20 29 3b 0a 0a 20 20 66 6f 72 28 72 63 3d  >0 );..  for(rc=
2bcef 53 51 4c 49 54 45 5f 4f 4b 3b 20 72 63 3d 3d 53  SQLITE_OK; rc==S
2bcf0 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 6c 65 61  QLITE_OK && !lea
2bcf1 76 65 73 52 65 61 64 65 72 41 74 45 6e 64 28 70  vesReaderAtEnd(p
2bcf2 52 65 61 64 65 72 29 3b 0a 20 20 20 20 20 20 72  Reader);.      r
2bcf3 63 3d 6c 65 61 76 65 73 52 65 61 64 65 72 53 74  c=leavesReaderSt
2bcf4 65 70 28 76 2c 20 70 52 65 61 64 65 72 29 29 7b  ep(v, pReader)){
2bcf5 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65  .    /* TODO(she
2bcf6 73 73 29 20 52 65 61 6c 6c 79 20 77 61 6e 74 20  ss) Really want 
2bcf7 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d  leavesReaderTerm
2bcf8 43 6d 70 28 29 2c 20 62 75 74 20 74 68 61 74 20  Cmp(), but that 
2bcf9 6e 61 6d 65 20 69 73 0a 20 20 20 20 2a 2a 20 61  name is.    ** a
2bcfa 6c 72 65 61 64 79 20 74 61 6b 65 6e 20 74 6f 20  lready taken to 
2bcfb 63 6f 6d 70 61 72 65 20 74 68 65 20 74 65 72 6d  compare the term
2bcfc 73 20 6f 66 20 74 77 6f 20 4c 65 61 76 65 73 52  s of two LeavesR
2bcfd 65 61 64 65 72 73 2e 20 20 54 68 69 6e 6b 0a 20  eaders.  Think. 
2bcfe 20 20 20 2a 2a 20 6f 6e 20 61 20 62 65 74 74 65     ** on a bette
2bcff 72 20 6e 61 6d 65 2e 20 20 5b 4d 65 61 6e 77 68  r name.  [Meanwh
2bd00 69 6c 65 2c 20 62 72 65 61 6b 20 65 6e 63 61 70  ile, break encap
2bd01 73 75 6c 61 74 69 6f 6e 20 72 61 74 68 65 72 20  sulation rather 
2bd02 74 68 61 6e 0a 20 20 20 20 2a 2a 20 75 73 65 20  than.    ** use 
2bd03 61 20 63 6f 6e 66 75 73 69 6e 67 20 6e 61 6d 65  a confusing name
2bd04 2e 5d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e  .].    */.    in
2bd05 74 20 63 20 3d 20 6c 65 61 66 52 65 61 64 65 72  t c = leafReader
2bd06 54 65 72 6d 43 6d 70 28 26 70 52 65 61 64 65 72  TermCmp(&pReader
2bd07 2d 3e 6c 65 61 66 52 65 61 64 65 72 2c 20 70 54  ->leafReader, pT
2bd08 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72  erm, nTerm, isPr
2bd09 65 66 69 78 29 3b 0a 20 20 20 20 69 66 28 20 63  efix);.    if( c
2bd0a 3e 30 20 29 20 62 72 65 61 6b 3b 20 20 20 20 20  >0 ) break;     
2bd0b 20 2f 2a 20 50 61 73 74 20 61 6e 79 20 70 6f 73   /* Past any pos
2bd0c 73 69 62 6c 65 20 6d 61 74 63 68 65 73 2e 20 2a  sible matches. *
2bd0d 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29  /.    if( c==0 )
2bd0e 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  {.      const ch
2bd0f 61 72 20 2a 70 44 61 74 61 20 3d 20 6c 65 61 76  ar *pData = leav
2bd10 65 73 52 65 61 64 65 72 44 61 74 61 28 70 52 65  esReaderData(pRe
2bd11 61 64 65 72 29 3b 0a 20 20 20 20 20 20 69 6e 74  ader);.      int
2bd12 20 69 42 75 66 66 65 72 2c 20 6e 44 61 74 61 20   iBuffer, nData 
2bd13 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 44 61  = leavesReaderDa
2bd14 74 61 42 79 74 65 73 28 70 52 65 61 64 65 72 29  taBytes(pReader)
2bd15 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69 6e 64  ;..      /* Find
2bd16 20 74 68 65 20 66 69 72 73 74 20 65 6d 70 74 79   the first empty
2bd17 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20 20 20   buffer. */.    
2bd18 20 20 66 6f 72 28 69 42 75 66 66 65 72 3d 30 3b    for(iBuffer=0;
2bd19 20 69 42 75 66 66 65 72 3c 6e 42 75 66 66 65 72   iBuffer<nBuffer
2bd1a 73 3b 20 2b 2b 69 42 75 66 66 65 72 29 7b 0a 20  s; ++iBuffer){. 
2bd1b 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d 70 42         if( 0==pB
2bd1c 75 66 66 65 72 73 5b 69 42 75 66 66 65 72 5d 2e  uffers[iBuffer].
2bd1d 6e 44 61 74 61 20 29 20 62 72 65 61 6b 3b 0a 20  nData ) break;. 
2bd1e 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
2bd1f 20 4f 75 74 20 6f 66 20 62 75 66 66 65 72 73 2c   Out of buffers,
2bd20 20 61 64 64 20 61 6e 20 65 6d 70 74 79 20 6f 6e   add an empty on
2bd21 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  e. */.      if( 
2bd22 69 42 75 66 66 65 72 3d 3d 6e 42 75 66 66 65 72  iBuffer==nBuffer
2bd23 73 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  s ){.        if(
2bd24 20 6e 42 75 66 66 65 72 73 3d 3d 6e 4d 61 78 42   nBuffers==nMaxB
2bd25 75 66 66 65 72 73 20 29 7b 0a 20 20 20 20 20 20  uffers ){.      
2bd26 20 20 20 20 44 61 74 61 42 75 66 66 65 72 20 2a      DataBuffer *
2bd27 70 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 4d 61  p;.          nMa
2bd28 78 42 75 66 66 65 72 73 20 2b 3d 20 32 30 3b 0a  xBuffers += 20;.
2bd29 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61  .          /* Ma
2bd2a 6e 75 61 6c 20 72 65 61 6c 6c 6f 63 20 73 6f 20  nual realloc so 
2bd2b 77 65 20 63 61 6e 20 68 61 6e 64 6c 65 20 4e 55  we can handle NU
2bd2c 4c 4c 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79  LL appropriately
2bd2d 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 70  . */.          p
2bd2e 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
2bd2f 63 28 6e 4d 61 78 42 75 66 66 65 72 73 2a 73 69  c(nMaxBuffers*si
2bd30 7a 65 6f 66 28 2a 70 42 75 66 66 65 72 73 29 29  zeof(*pBuffers))
2bd31 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
2bd32 70 3d 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20  p==NULL ){.     
2bd33 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
2bd34 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
2bd35 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2bd36 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
2bd37 20 20 20 20 69 66 28 20 6e 42 75 66 66 65 72 73      if( nBuffers
2bd38 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  >0 ){.          
2bd39 20 20 61 73 73 65 72 74 28 70 42 75 66 66 65 72    assert(pBuffer
2bd3a 73 21 3d 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20  s!=NULL);.      
2bd3b 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20        memcpy(p, 
2bd3c 70 42 75 66 66 65 72 73 2c 20 6e 42 75 66 66 65  pBuffers, nBuffe
2bd3d 72 73 2a 73 69 7a 65 6f 66 28 2a 70 42 75 66 66  rs*sizeof(*pBuff
2bd3e 65 72 73 29 29 3b 0a 20 20 20 20 20 20 20 20 20  ers));.         
2bd3f 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
2bd40 70 42 75 66 66 65 72 73 29 3b 0a 20 20 20 20 20  pBuffers);.     
2bd41 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
2bd42 20 70 42 75 66 66 65 72 73 20 3d 20 70 3b 0a 20   pBuffers = p;. 
2bd43 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2bd44 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28   dataBufferInit(
2bd45 26 28 70 42 75 66 66 65 72 73 5b 6e 42 75 66 66  &(pBuffers[nBuff
2bd46 65 72 73 5d 29 2c 20 30 29 3b 0a 20 20 20 20 20  ers]), 0);.     
2bd47 20 20 20 6e 42 75 66 66 65 72 73 2b 2b 3b 0a 20     nBuffers++;. 
2bd48 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
2bd49 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20   At this point, 
2bd4a 6d 75 73 74 20 68 61 76 65 20 61 6e 20 65 6d 70  must have an emp
2bd4b 74 79 20 61 74 20 69 42 75 66 66 65 72 2e 20 2a  ty at iBuffer. *
2bd4c 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 69  /.      assert(i
2bd4d 42 75 66 66 65 72 3c 6e 42 75 66 66 65 72 73 20  Buffer<nBuffers 
2bd4e 26 26 20 70 42 75 66 66 65 72 73 5b 69 42 75 66  && pBuffers[iBuf
2bd4f 66 65 72 5d 2e 6e 44 61 74 61 3d 3d 30 29 3b 0a  fer].nData==0);.
2bd50 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 65 6d 70  .      /* If emp
2bd51 74 79 20 77 61 73 20 66 69 72 73 74 20 62 75 66  ty was first buf
2bd52 66 65 72 2c 20 6e 6f 20 6e 65 65 64 20 66 6f 72  fer, no need for
2bd53 20 6d 65 72 67 65 20 6c 6f 67 69 63 2e 20 2a 2f   merge logic. */
2bd54 0a 20 20 20 20 20 20 69 66 28 20 69 42 75 66 66  .      if( iBuff
2bd55 65 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  er==0 ){.       
2bd56 20 64 61 74 61 42 75 66 66 65 72 52 65 70 6c 61   dataBufferRepla
2bd57 63 65 28 26 28 70 42 75 66 66 65 72 73 5b 30 5d  ce(&(pBuffers[0]
2bd58 29 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 29  ), pData, nData)
2bd59 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
2bd5a 20 20 20 20 20 20 20 2f 2a 20 70 41 63 63 20 69         /* pAcc i
2bd5b 73 20 74 68 65 20 65 6d 70 74 79 20 62 75 66 66  s the empty buff
2bd5c 65 72 20 74 68 65 20 6d 65 72 67 65 64 20 64 61  er the merged da
2bd5d 74 61 20 77 69 6c 6c 20 65 6e 64 20 75 70 20 69  ta will end up i
2bd5e 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 44 61  n. */.        Da
2bd5f 74 61 42 75 66 66 65 72 20 2a 70 41 63 63 20 3d  taBuffer *pAcc =
2bd60 20 26 28 70 42 75 66 66 65 72 73 5b 69 42 75 66   &(pBuffers[iBuf
2bd61 66 65 72 5d 29 3b 0a 20 20 20 20 20 20 20 20 44  fer]);.        D
2bd62 61 74 61 42 75 66 66 65 72 20 2a 70 20 3d 20 26  ataBuffer *p = &
2bd63 28 70 42 75 66 66 65 72 73 5b 30 5d 29 3b 0a 0a  (pBuffers[0]);..
2bd64 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c          /* Handl
2bd65 65 20 70 6f 73 69 74 69 6f 6e 20 30 20 73 70 65  e position 0 spe
2bd66 63 69 61 6c 6c 79 20 74 6f 20 61 76 6f 69 64 20  cially to avoid 
2bd67 6e 65 65 64 20 74 6f 20 70 72 69 6d 65 20 70 41  need to prime pA
2bd68 63 63 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 69  cc.        ** wi
2bd69 74 68 20 70 44 61 74 61 2f 6e 44 61 74 61 2e 0a  th pData/nData..
2bd6a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
2bd6b 20 20 20 64 61 74 61 42 75 66 66 65 72 53 77 61     dataBufferSwa
2bd6c 70 28 70 2c 20 70 41 63 63 29 3b 0a 20 20 20 20  p(p, pAcc);.    
2bd6d 20 20 20 20 64 6f 63 4c 69 73 74 41 63 63 75 6d      docListAccum
2bd6e 75 6c 61 74 65 55 6e 69 6f 6e 28 70 41 63 63 2c  ulateUnion(pAcc,
2bd6f 20 70 44 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a   pData, nData);.
2bd70 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 63 63 75  .        /* Accu
2bd71 6d 75 6c 61 74 65 20 72 65 6d 61 69 6e 69 6e 67  mulate remaining
2bd72 20 64 6f 63 6c 69 73 74 73 20 69 6e 74 6f 20 70   doclists into p
2bd73 41 63 63 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  Acc. */.        
2bd74 66 6f 72 28 2b 2b 70 3b 20 70 3c 70 41 63 63 3b  for(++p; p<pAcc;
2bd75 20 2b 2b 70 29 7b 0a 20 20 20 20 20 20 20 20 20   ++p){.         
2bd76 20 64 6f 63 4c 69 73 74 41 63 63 75 6d 75 6c 61   docListAccumula
2bd77 74 65 55 6e 69 6f 6e 28 70 41 63 63 2c 20 70 2d  teUnion(pAcc, p-
2bd78 3e 70 44 61 74 61 2c 20 70 2d 3e 6e 44 61 74 61  >pData, p->nData
2bd79 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  );..          /*
2bd7a 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74   dataBufferReset
2bd7b 28 29 20 63 6f 75 6c 64 20 61 6c 6c 6f 77 20 61  () could allow a
2bd7c 20 6c 61 72 67 65 20 64 6f 63 6c 69 73 74 20 74   large doclist t
2bd7d 6f 20 62 6c 6f 77 20 75 70 0a 20 20 20 20 20 20  o blow up.      
2bd7e 20 20 20 20 2a 2a 20 6f 75 72 20 6d 65 6d 6f 72      ** our memor
2bd7f 79 20 72 65 71 75 69 72 65 6d 65 6e 74 73 2e 0a  y requirements..
2bd80 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
2bd81 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 6e 43         if( p->nC
2bd82 61 70 61 63 69 74 79 3c 31 30 32 34 20 29 7b 0a  apacity<1024 ){.
2bd83 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61              data
2bd84 42 75 66 66 65 72 52 65 73 65 74 28 70 29 3b 0a  BufferReset(p);.
2bd85 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
2bd86 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74  .            dat
2bd87 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 70  aBufferDestroy(p
2bd88 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 64  );.            d
2bd89 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 70 2c  ataBufferInit(p,
2bd8a 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d   0);.          }
2bd8b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2bd8c 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
2bd8d 2f 2a 20 55 6e 69 6f 6e 20 61 6c 6c 20 74 68 65  /* Union all the
2bd8e 20 64 6f 63 6c 69 73 74 73 20 74 6f 67 65 74 68   doclists togeth
2bd8f 65 72 20 69 6e 74 6f 20 2a 6f 75 74 2e 20 2a 2f  er into *out. */
2bd90 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  .  /* TODO(shess
2bd91 29 20 57 68 61 74 20 69 66 20 2a 6f 75 74 20 69  ) What if *out i
2bd92 73 20 62 69 67 3f 20 20 53 69 67 68 2e 20 2a 2f  s big?  Sigh. */
2bd93 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
2bd94 45 5f 4f 4b 20 26 26 20 6e 42 75 66 66 65 72 73  E_OK && nBuffers
2bd95 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 42  >0 ){.    int iB
2bd96 75 66 66 65 72 3b 0a 20 20 20 20 66 6f 72 28 69  uffer;.    for(i
2bd97 42 75 66 66 65 72 3d 30 3b 20 69 42 75 66 66 65  Buffer=0; iBuffe
2bd98 72 3c 6e 42 75 66 66 65 72 73 3b 20 2b 2b 69 42  r<nBuffers; ++iB
2bd99 75 66 66 65 72 29 7b 0a 20 20 20 20 20 20 69 66  uffer){.      if
2bd9a 28 20 70 42 75 66 66 65 72 73 5b 69 42 75 66 66  ( pBuffers[iBuff
2bd9b 65 72 5d 2e 6e 44 61 74 61 3e 30 20 29 7b 0a 20  er].nData>0 ){. 
2bd9c 20 20 20 20 20 20 20 69 66 28 20 6f 75 74 2d 3e         if( out->
2bd9d 6e 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20  nData==0 ){.    
2bd9e 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72        dataBuffer
2bd9f 53 77 61 70 28 6f 75 74 2c 20 26 28 70 42 75 66  Swap(out, &(pBuf
2bda0 66 65 72 73 5b 69 42 75 66 66 65 72 5d 29 29 3b  fers[iBuffer]));
2bda1 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
2bda2 20 20 20 20 20 20 20 20 20 20 64 6f 63 4c 69 73            docLis
2bda3 74 41 63 63 75 6d 75 6c 61 74 65 55 6e 69 6f 6e  tAccumulateUnion
2bda4 28 6f 75 74 2c 20 70 42 75 66 66 65 72 73 5b 69  (out, pBuffers[i
2bda5 42 75 66 66 65 72 5d 2e 70 44 61 74 61 2c 0a 20  Buffer].pData,. 
2bda6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bda7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bda8 70 42 75 66 66 65 72 73 5b 69 42 75 66 66 65 72  pBuffers[iBuffer
2bda9 5d 2e 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 20  ].nData);.      
2bdaa 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
2bdab 7d 0a 20 20 7d 0a 0a 20 20 77 68 69 6c 65 28 20  }.  }..  while( 
2bdac 6e 42 75 66 66 65 72 73 2d 2d 20 29 7b 0a 20 20  nBuffers-- ){.  
2bdad 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74    dataBufferDest
2bdae 72 6f 79 28 26 28 70 42 75 66 66 65 72 73 5b 6e  roy(&(pBuffers[n
2bdaf 42 75 66 66 65 72 73 5d 29 29 3b 0a 20 20 7d 0a  Buffers]));.  }.
2bdb0 20 20 69 66 28 20 70 42 75 66 66 65 72 73 21 3d    if( pBuffers!=
2bdb1 4e 55 4c 4c 20 29 20 73 71 6c 69 74 65 33 5f 66  NULL ) sqlite3_f
2bdb2 72 65 65 28 70 42 75 66 66 65 72 73 29 3b 0a 0a  ree(pBuffers);..
2bdb3 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
2bdb4 2f 2a 20 43 61 6c 6c 20 6c 6f 61 64 53 65 67 6d  /* Call loadSegm
2bdb5 65 6e 74 4c 65 61 76 65 73 49 6e 74 28 29 20 77  entLeavesInt() w
2bdb6 69 74 68 20 70 44 61 74 61 2f 6e 44 61 74 61 20  ith pData/nData 
2bdb7 61 73 20 69 6e 70 75 74 2e 20 2a 2f 0a 73 74 61  as input. */.sta
2bdb8 74 69 63 20 69 6e 74 20 6c 6f 61 64 53 65 67 6d  tic int loadSegm
2bdb9 65 6e 74 4c 65 61 66 28 66 75 6c 6c 74 65 78 74  entLeaf(fulltext
2bdba 5f 76 74 61 62 20 2a 76 2c 20 63 6f 6e 73 74 20  _vtab *v, const 
2bdbb 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74  char *pData, int
2bdbc 20 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20   nData,.        
2bdbd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bdbe 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
2bdbf 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c  Term, int nTerm,
2bdc0 20 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20   int isPrefix,. 
2bdc1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bdc2 20 20 20 20 20 20 20 20 20 20 44 61 74 61 42 75            DataBu
2bdc3 66 66 65 72 20 2a 6f 75 74 29 7b 0a 20 20 4c 65  ffer *out){.  Le
2bdc4 61 76 65 73 52 65 61 64 65 72 20 72 65 61 64 65  avesReader reade
2bdc5 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  r;.  int rc;..  
2bdc6 61 73 73 65 72 74 28 20 6e 44 61 74 61 3e 31 20  assert( nData>1 
2bdc7 29 3b 0a 20 20 61 73 73 65 72 74 28 20 2a 70 44  );.  assert( *pD
2bdc8 61 74 61 3d 3d 27 5c 30 27 20 29 3b 0a 20 20 72  ata=='\0' );.  r
2bdc9 63 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72  c = leavesReader
2bdca 49 6e 69 74 28 76 2c 20 30 2c 20 30 2c 20 30 2c  Init(v, 0, 0, 0,
2bdcb 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 26   pData, nData, &
2bdcc 72 65 61 64 65 72 29 3b 0a 20 20 69 66 28 20 72  reader);.  if( r
2bdcd 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
2bdce 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
2bdcf 3d 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61  = loadSegmentLea
2bdd0 76 65 73 49 6e 74 28 76 2c 20 26 72 65 61 64 65  vesInt(v, &reade
2bdd1 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c  r, pTerm, nTerm,
2bdd2 20 69 73 50 72 65 66 69 78 2c 20 6f 75 74 29 3b   isPrefix, out);
2bdd3 0a 20 20 6c 65 61 76 65 73 52 65 61 64 65 72 52  .  leavesReaderR
2bdd4 65 73 65 74 28 26 72 65 61 64 65 72 29 3b 0a 20  eset(&reader);. 
2bdd5 20 6c 65 61 76 65 73 52 65 61 64 65 72 44 65 73   leavesReaderDes
2bdd6 74 72 6f 79 28 26 72 65 61 64 65 72 29 3b 0a 20  troy(&reader);. 
2bdd7 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
2bdd8 2a 20 43 61 6c 6c 20 6c 6f 61 64 53 65 67 6d 65  * Call loadSegme
2bdd9 6e 74 4c 65 61 76 65 73 49 6e 74 28 29 20 77 69  ntLeavesInt() wi
2bdda 74 68 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65  th the leaf node
2bddb 73 20 66 72 6f 6d 20 69 53 74 61 72 74 4c 65 61  s from iStartLea
2bddc 66 20 74 6f 0a 2a 2a 20 69 45 6e 64 4c 65 61 66  f to.** iEndLeaf
2bddd 20 28 69 6e 63 6c 75 73 69 76 65 29 20 61 73 20   (inclusive) as 
2bdde 69 6e 70 75 74 2c 20 61 6e 64 20 6d 65 72 67 65  input, and merge
2bddf 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 64   the resulting d
2bde0 6f 63 6c 69 73 74 20 69 6e 74 6f 0a 2a 2a 20 6f  oclist into.** o
2bde1 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ut..*/.static in
2bde2 74 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61  t loadSegmentLea
2bde3 76 65 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  ves(fulltext_vta
2bde4 62 20 2a 76 2c 0a 20 20 20 20 20 20 20 20 20 20  b *v,.          
2bde5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bde6 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
2bde7 69 53 74 61 72 74 4c 65 61 66 2c 20 73 71 6c 69  iStartLeaf, sqli
2bde8 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 4c 65 61  te_int64 iEndLea
2bde9 66 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  f,.             
2bdea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bdeb 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72  const char *pTer
2bdec 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e  m, int nTerm, in
2bded 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20 20 20  t isPrefix,.    
2bdee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bdef 20 20 20 20 20 20 20 20 20 44 61 74 61 42 75 66           DataBuf
2bdf0 66 65 72 20 2a 6f 75 74 29 7b 0a 20 20 69 6e 74  fer *out){.  int
2bdf1 20 72 63 3b 0a 20 20 4c 65 61 76 65 73 52 65 61   rc;.  LeavesRea
2bdf2 64 65 72 20 72 65 61 64 65 72 3b 0a 0a 20 20 61  der reader;..  a
2bdf3 73 73 65 72 74 28 20 69 53 74 61 72 74 4c 65 61  ssert( iStartLea
2bdf4 66 3c 3d 69 45 6e 64 4c 65 61 66 20 29 3b 0a 20  f<=iEndLeaf );. 
2bdf5 20 72 63 20 3d 20 6c 65 61 76 65 73 52 65 61 64   rc = leavesRead
2bdf6 65 72 49 6e 69 74 28 76 2c 20 30 2c 20 69 53 74  erInit(v, 0, iSt
2bdf7 61 72 74 4c 65 61 66 2c 20 69 45 6e 64 4c 65 61  artLeaf, iEndLea
2bdf8 66 2c 20 4e 55 4c 4c 2c 20 30 2c 20 26 72 65 61  f, NULL, 0, &rea
2bdf9 64 65 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  der);.  if( rc!=
2bdfa 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
2bdfb 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 6c  rn rc;..  rc = l
2bdfc 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73  oadSegmentLeaves
2bdfd 49 6e 74 28 76 2c 20 26 72 65 61 64 65 72 2c 20  Int(v, &reader, 
2bdfe 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73  pTerm, nTerm, is
2bdff 50 72 65 66 69 78 2c 20 6f 75 74 29 3b 0a 20 20  Prefix, out);.  
2be00 6c 65 61 76 65 73 52 65 61 64 65 72 52 65 73 65  leavesReaderRese
2be01 74 28 26 72 65 61 64 65 72 29 3b 0a 20 20 6c 65  t(&reader);.  le
2be02 61 76 65 73 52 65 61 64 65 72 44 65 73 74 72 6f  avesReaderDestro
2be03 79 28 26 72 65 61 64 65 72 29 3b 0a 20 20 72 65  y(&reader);.  re
2be04 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54  turn rc;.}../* T
2be05 61 6b 69 6e 67 20 70 44 61 74 61 2f 6e 44 61 74  aking pData/nDat
2be06 61 20 61 73 20 61 6e 20 69 6e 74 65 72 69 6f 72  a as an interior
2be07 20 6e 6f 64 65 2c 20 66 69 6e 64 20 74 68 65 20   node, find the 
2be08 73 65 71 75 65 6e 63 65 20 6f 66 20 63 68 69 6c  sequence of chil
2be09 64 0a 2a 2a 20 6e 6f 64 65 73 20 77 68 69 63 68  d.** nodes which
2be0a 20 63 6f 75 6c 64 20 69 6e 63 6c 75 64 65 20 70   could include p
2be0b 54 65 72 6d 2f 6e 54 65 72 6d 2f 69 73 50 72 65  Term/nTerm/isPre
2be0c 66 69 78 2e 20 20 4e 6f 74 65 20 74 68 61 74 20  fix.  Note that 
2be0d 74 68 65 0a 2a 2a 20 69 6e 74 65 72 69 6f 72 20  the.** interior 
2be0e 6e 6f 64 65 20 74 65 72 6d 73 20 6c 6f 67 69 63  node terms logic
2be0f 61 6c 6c 79 20 63 6f 6d 65 20 62 65 74 77 65 65  ally come betwee
2be10 6e 20 74 68 65 20 62 6c 6f 63 6b 73 2c 20 73 6f  n the blocks, so
2be11 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 6f 6e 65   there is.** one
2be12 20 6d 6f 72 65 20 62 6c 6f 63 6b 69 64 20 74 68   more blockid th
2be13 61 6e 20 74 68 65 72 65 20 61 72 65 20 74 65 72  an there are ter
2be14 6d 73 20 28 74 68 61 74 20 62 6c 6f 63 6b 20 63  ms (that block c
2be15 6f 6e 74 61 69 6e 73 20 74 65 72 6d 73 20 3e 3d  ontains terms >=
2be16 0a 2a 2a 20 74 68 65 20 6c 61 73 74 20 69 6e 74  .** the last int
2be17 65 72 69 6f 72 2d 6e 6f 64 65 20 74 65 72 6d 29  erior-node term)
2be18 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  ..*/./* TODO(she
2be19 73 73 29 20 54 68 65 20 63 61 6c 6c 69 6e 67 20  ss) The calling 
2be1a 63 6f 64 65 20 6d 61 79 20 61 6c 72 65 61 64 79  code may already
2be1b 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 65   know that the e
2be1c 6e 64 20 63 68 69 6c 64 20 69 73 0a 2a 2a 20 6e  nd child is.** n
2be1d 6f 74 20 77 6f 72 74 68 20 63 61 6c 63 75 6c 61  ot worth calcula
2be1e 74 69 6e 67 2c 20 62 65 63 61 75 73 65 20 74 68  ting, because th
2be1f 65 20 65 6e 64 20 6d 61 79 20 62 65 20 69 6e 20  e end may be in 
2be20 61 20 6c 61 74 65 72 20 73 69 62 6c 69 6e 67 0a  a later sibling.
2be21 2a 2a 20 6e 6f 64 65 2e 20 20 43 6f 6e 73 69 64  ** node.  Consid
2be22 65 72 20 77 68 65 74 68 65 72 20 62 72 65 61 6b  er whether break
2be23 69 6e 67 20 73 79 6d 6d 65 74 72 79 20 69 73 20  ing symmetry is 
2be24 77 6f 72 74 68 77 68 69 6c 65 2e 20 20 49 20 73  worthwhile.  I s
2be25 75 73 70 65 63 74 0a 2a 2a 20 69 74 20 69 73 20  uspect.** it is 
2be26 6e 6f 74 20 77 6f 72 74 68 77 68 69 6c 65 2e 0a  not worthwhile..
2be27 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67  */.static void g
2be28 65 74 43 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69  etChildrenContai
2be29 6e 69 6e 67 28 63 6f 6e 73 74 20 63 68 61 72 20  ning(const char 
2be2a 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74  *pData, int nDat
2be2b 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  a,.             
2be2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be2d 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
2be2e 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72  *pTerm, int nTer
2be2f 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69 78 2c  m, int isPrefix,
2be30 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2be31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be32 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
2be33 2a 70 69 53 74 61 72 74 43 68 69 6c 64 2c 0a 20  *piStartChild,. 
2be34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be36 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70   sqlite_int64 *p
2be37 69 45 6e 64 43 68 69 6c 64 29 7b 0a 20 20 49 6e  iEndChild){.  In
2be38 74 65 72 69 6f 72 52 65 61 64 65 72 20 72 65 61  teriorReader rea
2be39 64 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  der;..  assert( 
2be3a 6e 44 61 74 61 3e 31 20 29 3b 0a 20 20 61 73 73  nData>1 );.  ass
2be3b 65 72 74 28 20 2a 70 44 61 74 61 21 3d 27 5c 30  ert( *pData!='\0
2be3c 27 20 29 3b 0a 20 20 69 6e 74 65 72 69 6f 72 52  ' );.  interiorR
2be3d 65 61 64 65 72 49 6e 69 74 28 70 44 61 74 61 2c  eaderInit(pData,
2be3e 20 6e 44 61 74 61 2c 20 26 72 65 61 64 65 72 29   nData, &reader)
2be3f 3b 0a 0a 20 20 2f 2a 20 53 63 61 6e 20 66 6f 72  ;..  /* Scan for
2be40 20 74 68 65 20 66 69 72 73 74 20 63 68 69 6c 64   the first child
2be41 20 77 68 69 63 68 20 63 6f 75 6c 64 20 63 6f 6e   which could con
2be42 74 61 69 6e 20 70 54 65 72 6d 2f 6e 54 65 72 6d  tain pTerm/nTerm
2be43 2e 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 21 69  . */.  while( !i
2be44 6e 74 65 72 69 6f 72 52 65 61 64 65 72 41 74 45  nteriorReaderAtE
2be45 6e 64 28 26 72 65 61 64 65 72 29 20 29 7b 0a 20  nd(&reader) ){. 
2be46 20 20 20 69 66 28 20 69 6e 74 65 72 69 6f 72 52     if( interiorR
2be47 65 61 64 65 72 54 65 72 6d 43 6d 70 28 26 72 65  eaderTermCmp(&re
2be48 61 64 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65  ader, pTerm, nTe
2be49 72 6d 2c 20 30 29 3e 30 20 29 20 62 72 65 61 6b  rm, 0)>0 ) break
2be4a 3b 0a 20 20 20 20 69 6e 74 65 72 69 6f 72 52 65  ;.    interiorRe
2be4b 61 64 65 72 53 74 65 70 28 26 72 65 61 64 65 72  aderStep(&reader
2be4c 29 3b 0a 20 20 7d 0a 20 20 2a 70 69 53 74 61 72  );.  }.  *piStar
2be4d 74 43 68 69 6c 64 20 3d 20 69 6e 74 65 72 69 6f  tChild = interio
2be4e 72 52 65 61 64 65 72 43 75 72 72 65 6e 74 42 6c  rReaderCurrentBl
2be4f 6f 63 6b 69 64 28 26 72 65 61 64 65 72 29 3b 0a  ockid(&reader);.
2be50 0a 20 20 2f 2a 20 4b 65 65 70 20 73 63 61 6e 6e  .  /* Keep scann
2be51 69 6e 67 20 74 6f 20 66 69 6e 64 20 61 20 74 65  ing to find a te
2be52 72 6d 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  rm greater than 
2be53 6f 75 72 20 74 65 72 6d 2c 20 75 73 69 6e 67 20  our term, using 
2be54 70 72 65 66 69 78 0a 20 20 2a 2a 20 63 6f 6d 70  prefix.  ** comp
2be55 61 72 69 73 6f 6e 20 69 66 20 69 6e 64 69 63 61  arison if indica
2be56 74 65 64 2e 20 20 49 66 20 69 73 50 72 65 66 69  ted.  If isPrefi
2be57 78 20 69 73 20 66 61 6c 73 65 2c 20 74 68 69 73  x is false, this
2be58 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 20 20 2a   will be the.  *
2be59 2a 20 73 61 6d 65 20 62 6c 6f 63 6b 69 64 20 61  * same blockid a
2be5a 73 20 74 68 65 20 73 74 61 72 74 69 6e 67 20 62  s the starting b
2be5b 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 77 68 69  lock..  */.  whi
2be5c 6c 65 28 20 21 69 6e 74 65 72 69 6f 72 52 65 61  le( !interiorRea
2be5d 64 65 72 41 74 45 6e 64 28 26 72 65 61 64 65 72  derAtEnd(&reader
2be5e 29 20 29 7b 0a 20 20 20 20 69 66 28 20 69 6e 74  ) ){.    if( int
2be5f 65 72 69 6f 72 52 65 61 64 65 72 54 65 72 6d 43  eriorReaderTermC
2be60 6d 70 28 26 72 65 61 64 65 72 2c 20 70 54 65 72  mp(&reader, pTer
2be61 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66  m, nTerm, isPref
2be62 69 78 29 3e 30 20 29 20 62 72 65 61 6b 3b 0a 20  ix)>0 ) break;. 
2be63 20 20 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65     interiorReade
2be64 72 53 74 65 70 28 26 72 65 61 64 65 72 29 3b 0a  rStep(&reader);.
2be65 20 20 7d 0a 20 20 2a 70 69 45 6e 64 43 68 69 6c    }.  *piEndChil
2be66 64 20 3d 20 69 6e 74 65 72 69 6f 72 52 65 61 64  d = interiorRead
2be67 65 72 43 75 72 72 65 6e 74 42 6c 6f 63 6b 69 64  erCurrentBlockid
2be68 28 26 72 65 61 64 65 72 29 3b 0a 0a 20 20 69 6e  (&reader);..  in
2be69 74 65 72 69 6f 72 52 65 61 64 65 72 44 65 73 74  teriorReaderDest
2be6a 72 6f 79 28 26 72 65 61 64 65 72 29 3b 0a 0a 20  roy(&reader);.. 
2be6b 20 2f 2a 20 43 68 69 6c 64 72 65 6e 20 6d 75 73   /* Children mus
2be6c 74 20 61 73 63 65 6e 64 2c 20 61 6e 64 20 69 66  t ascend, and if
2be6d 20 21 70 72 65 66 69 78 2c 20 62 6f 74 68 20 6d   !prefix, both m
2be6e 75 73 74 20 62 65 20 74 68 65 20 73 61 6d 65 2e  ust be the same.
2be6f 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 2a 70   */.  assert( *p
2be70 69 45 6e 64 43 68 69 6c 64 3e 3d 2a 70 69 53 74  iEndChild>=*piSt
2be71 61 72 74 43 68 69 6c 64 20 29 3b 0a 20 20 61 73  artChild );.  as
2be72 73 65 72 74 28 20 69 73 50 72 65 66 69 78 20 7c  sert( isPrefix |
2be73 7c 20 2a 70 69 53 74 61 72 74 43 68 69 6c 64 3d  | *piStartChild=
2be74 3d 2a 70 69 45 6e 64 43 68 69 6c 64 20 29 3b 0a  =*piEndChild );.
2be75 7d 0a 0a 2f 2a 20 52 65 61 64 20 62 6c 6f 63 6b  }../* Read block
2be76 20 61 74 20 69 42 6c 6f 63 6b 69 64 20 61 6e 64   at iBlockid and
2be77 20 70 61 73 73 20 69 74 20 77 69 74 68 20 6f 74   pass it with ot
2be78 68 65 72 20 70 61 72 61 6d 73 20 74 6f 0a 2a 2a  her params to.**
2be79 20 67 65 74 43 68 69 6c 64 72 65 6e 43 6f 6e 74   getChildrenCont
2be7a 61 69 6e 69 6e 67 28 29 2e 0a 2a 2f 0a 73 74 61  aining()..*/.sta
2be7b 74 69 63 20 69 6e 74 20 6c 6f 61 64 41 6e 64 47  tic int loadAndG
2be7c 65 74 43 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69  etChildrenContai
2be7d 6e 69 6e 67 28 0a 20 20 66 75 6c 6c 74 65 78 74  ning(.  fulltext
2be7e 5f 76 74 61 62 20 2a 76 2c 0a 20 20 73 71 6c 69  _vtab *v,.  sqli
2be7f 74 65 5f 69 6e 74 36 34 20 69 42 6c 6f 63 6b 69  te_int64 iBlocki
2be80 64 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  d,.  const char 
2be81 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72  *pTerm, int nTer
2be82 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69 78 2c  m, int isPrefix,
2be83 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
2be84 2a 70 69 53 74 61 72 74 43 68 69 6c 64 2c 20 73  *piStartChild, s
2be85 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69 45  qlite_int64 *piE
2be86 6e 64 43 68 69 6c 64 0a 29 7b 0a 20 20 73 71 6c  ndChild.){.  sql
2be87 69 74 65 33 5f 73 74 6d 74 20 2a 73 20 3d 20 4e  ite3_stmt *s = N
2be88 55 4c 4c 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  ULL;.  int rc;..
2be89 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f 63 6b    assert( iBlock
2be8a 69 64 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  id!=0 );.  asser
2be8b 74 28 20 70 54 65 72 6d 21 3d 4e 55 4c 4c 20 29  t( pTerm!=NULL )
2be8c 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 54 65 72  ;.  assert( nTer
2be8d 6d 21 3d 30 20 29 3b 20 20 20 20 20 20 20 20 2f  m!=0 );        /
2be8e 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 57 68  * TODO(shess) Wh
2be8f 79 20 6e 6f 74 20 61 6c 6c 6f 77 20 74 68 69 73  y not allow this
2be90 3f 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  ? */.  assert( p
2be91 69 53 74 61 72 74 43 68 69 6c 64 21 3d 4e 55 4c  iStartChild!=NUL
2be92 4c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  L );.  assert( p
2be93 69 45 6e 64 43 68 69 6c 64 21 3d 4e 55 4c 4c 20  iEndChild!=NULL 
2be94 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 5f 67  );..  rc = sql_g
2be95 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
2be96 42 4c 4f 43 4b 5f 53 45 4c 45 43 54 5f 53 54 4d  BLOCK_SELECT_STM
2be97 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63  T, &s);.  if( rc
2be98 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
2be99 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
2be9a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
2be9b 74 36 34 28 73 2c 20 31 2c 20 69 42 6c 6f 63 6b  t64(s, 1, iBlock
2be9c 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  id);.  if( rc!=S
2be9d 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
2be9e 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
2be9f 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20  lite3_step(s);. 
2bea0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
2bea1 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 53 51  DONE ) return SQ
2bea2 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69 66  LITE_ERROR;.  if
2bea3 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc!=SQLITE_ROW
2bea4 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
2bea5 20 67 65 74 43 68 69 6c 64 72 65 6e 43 6f 6e 74   getChildrenCont
2bea6 61 69 6e 69 6e 67 28 73 71 6c 69 74 65 33 5f 63  aining(sqlite3_c
2bea7 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 2c 20 30 29  olumn_blob(s, 0)
2bea8 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  , sqlite3_column
2bea9 5f 62 79 74 65 73 28 73 2c 20 30 29 2c 0a 20 20  _bytes(s, 0),.  
2beaa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2beab 20 20 20 20 20 20 70 54 65 72 6d 2c 20 6e 54 65        pTerm, nTe
2beac 72 6d 2c 20 69 73 50 72 65 66 69 78 2c 20 70 69  rm, isPrefix, pi
2bead 53 74 61 72 74 43 68 69 6c 64 2c 20 70 69 45 6e  StartChild, piEn
2beae 64 43 68 69 6c 64 29 3b 0a 0a 20 20 2f 2a 20 57  dChild);..  /* W
2beaf 65 20 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e  e expect only on
2beb0 65 20 72 6f 77 2e 20 20 57 65 20 6d 75 73 74 20  e row.  We must 
2beb1 65 78 65 63 75 74 65 20 61 6e 6f 74 68 65 72 20  execute another 
2beb2 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0a 20  sqlite3_step(). 
2beb3 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20    * to complete 
2beb4 74 68 65 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f  the iteration; o
2beb5 74 68 65 72 77 69 73 65 20 74 68 65 20 74 61 62  therwise the tab
2beb6 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e 0a 20  le will remain. 
2beb7 20 20 2a 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20    * locked. */. 
2beb8 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74   rc = sqlite3_st
2beb9 65 70 28 73 29 3b 0a 20 20 69 66 28 20 72 63 3d  ep(s);.  if( rc=
2beba 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65  =SQLITE_ROW ) re
2bebb 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
2bebc 52 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  R;.  if( rc!=SQL
2bebd 49 54 45 5f 44 4f 4e 45 20 29 20 72 65 74 75 72  ITE_DONE ) retur
2bebe 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20  n rc;..  return 
2bebf 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
2bec0 20 54 72 61 76 65 72 73 65 20 74 68 65 20 74 72   Traverse the tr
2bec1 65 65 20 72 65 70 72 65 73 65 6e 74 65 64 20 62  ee represented b
2bec2 79 20 70 44 61 74 61 5b 6e 44 61 74 61 5d 20 6c  y pData[nData] l
2bec3 6f 6f 6b 69 6e 67 20 66 6f 72 0a 2a 2a 20 70 54  ooking for.** pT
2bec4 65 72 6d 5b 6e 54 65 72 6d 5d 2c 20 70 6c 61 63  erm[nTerm], plac
2bec5 69 6e 67 20 69 74 73 20 64 6f 63 6c 69 73 74 20  ing its doclist 
2bec6 69 6e 74 6f 20 2a 6f 75 74 2e 20 20 54 68 69 73  into *out.  This
2bec7 20 69 73 20 69 6e 74 65 72 6e 61 6c 20 74 6f 0a   is internal to.
2bec8 2a 2a 20 6c 6f 61 64 53 65 67 6d 65 6e 74 28 29  ** loadSegment()
2bec9 20 74 6f 20 6d 61 6b 65 20 65 72 72 6f 72 2d 68   to make error-h
2beca 61 6e 64 6c 69 6e 67 20 63 6c 65 61 6e 65 72 2e  andling cleaner.
2becb 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
2becc 6f 61 64 53 65 67 6d 65 6e 74 49 6e 74 28 66 75  oadSegmentInt(fu
2becd 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
2bece 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
2becf 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20  a, int nData,.  
2bed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bed1 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69          sqlite_i
2bed2 6e 74 36 34 20 69 4c 65 61 76 65 73 45 6e 64 2c  nt64 iLeavesEnd,
2bed3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2bed4 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
2bed5 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e   char *pTerm, in
2bed6 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73 50  t nTerm, int isP
2bed7 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20  refix,.         
2bed8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bed9 20 44 61 74 61 42 75 66 66 65 72 20 2a 6f 75 74   DataBuffer *out
2beda 29 7b 0a 20 20 2f 2a 20 53 70 65 63 69 61 6c 20  ){.  /* Special 
2bedb 63 61 73 65 20 77 68 65 72 65 20 72 6f 6f 74 20  case where root 
2bedc 69 73 20 61 20 6c 65 61 66 2e 20 2a 2f 0a 20 20  is a leaf. */.  
2bedd 69 66 28 20 2a 70 44 61 74 61 3d 3d 27 5c 30 27  if( *pData=='\0'
2bede 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6c   ){.    return l
2bedf 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 66 28 76  oadSegmentLeaf(v
2bee0 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20  , pData, nData, 
2bee1 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73  pTerm, nTerm, is
2bee2 50 72 65 66 69 78 2c 20 6f 75 74 29 3b 0a 20 20  Prefix, out);.  
2bee3 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 72  }else{.    int r
2bee4 63 3b 0a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e  c;.    sqlite_in
2bee5 74 36 34 20 69 53 74 61 72 74 43 68 69 6c 64 2c  t64 iStartChild,
2bee6 20 69 45 6e 64 43 68 69 6c 64 3b 0a 0a 20 20 20   iEndChild;..   
2bee7 20 2f 2a 20 50 72 6f 63 65 73 73 20 70 44 61 74   /* Process pDat
2bee8 61 20 61 73 20 61 6e 20 69 6e 74 65 72 69 6f 72  a as an interior
2bee9 20 6e 6f 64 65 2c 20 74 68 65 6e 20 6c 6f 6f 70   node, then loop
2beea 20 64 6f 77 6e 20 74 68 65 20 74 72 65 65 0a 20   down the tree. 
2beeb 20 20 20 2a 2a 20 75 6e 74 69 6c 20 77 65 20 66     ** until we f
2beec 69 6e 64 20 74 68 65 20 73 65 74 20 6f 66 20 6c  ind the set of l
2beed 65 61 66 20 6e 6f 64 65 73 20 74 6f 20 73 63 61  eaf nodes to sca
2beee 6e 20 66 6f 72 20 74 68 65 20 74 65 72 6d 2e 0a  n for the term..
2beef 20 20 20 20 2a 2f 0a 20 20 20 20 67 65 74 43 68      */.    getCh
2bef0 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e 67  ildrenContaining
2bef1 28 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 70  (pData, nData, p
2bef2 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50  Term, nTerm, isP
2bef3 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20  refix,.         
2bef4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bef5 20 26 69 53 74 61 72 74 43 68 69 6c 64 2c 20 26   &iStartChild, &
2bef6 69 45 6e 64 43 68 69 6c 64 29 3b 0a 20 20 20 20  iEndChild);.    
2bef7 77 68 69 6c 65 28 20 69 53 74 61 72 74 43 68 69  while( iStartChi
2bef8 6c 64 3e 69 4c 65 61 76 65 73 45 6e 64 20 29 7b  ld>iLeavesEnd ){
2bef9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e  .      sqlite_in
2befa 74 36 34 20 69 4e 65 78 74 53 74 61 72 74 2c 20  t64 iNextStart, 
2befb 69 4e 65 78 74 45 6e 64 3b 0a 20 20 20 20 20 20  iNextEnd;.      
2befc 72 63 20 3d 20 6c 6f 61 64 41 6e 64 47 65 74 43  rc = loadAndGetC
2befd 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e  hildrenContainin
2befe 67 28 76 2c 20 69 53 74 61 72 74 43 68 69 6c 64  g(v, iStartChild
2beff 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20  , pTerm, nTerm, 
2bf00 69 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20  isPrefix,.      
2bf01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bf02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bf03 20 20 26 69 4e 65 78 74 53 74 61 72 74 2c 20 26    &iNextStart, &
2bf04 69 4e 65 78 74 45 6e 64 29 3b 0a 20 20 20 20 20  iNextEnd);.     
2bf05 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2bf06 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
2bf07 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65 27  .      /* If we'
2bf08 76 65 20 62 72 61 6e 63 68 65 64 2c 20 66 6f 6c  ve branched, fol
2bf09 6c 6f 77 20 74 68 65 20 65 6e 64 20 62 72 61 6e  low the end bran
2bf0a 63 68 2c 20 74 6f 6f 2e 20 2a 2f 0a 20 20 20 20  ch, too. */.    
2bf0b 20 20 69 66 28 20 69 53 74 61 72 74 43 68 69 6c    if( iStartChil
2bf0c 64 21 3d 69 45 6e 64 43 68 69 6c 64 20 29 7b 0a  d!=iEndChild ){.
2bf0d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69          sqlite_i
2bf0e 6e 74 36 34 20 69 44 75 6d 6d 79 3b 0a 20 20 20  nt64 iDummy;.   
2bf0f 20 20 20 20 20 72 63 20 3d 20 6c 6f 61 64 41 6e       rc = loadAn
2bf10 64 47 65 74 43 68 69 6c 64 72 65 6e 43 6f 6e 74  dGetChildrenCont
2bf11 61 69 6e 69 6e 67 28 76 2c 20 69 45 6e 64 43 68  aining(v, iEndCh
2bf12 69 6c 64 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72  ild, pTerm, nTer
2bf13 6d 2c 20 69 73 50 72 65 66 69 78 2c 0a 20 20 20  m, isPrefix,.   
2bf14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bf15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bf16 20 20 20 20 20 20 20 26 69 44 75 6d 6d 79 2c 20         &iDummy, 
2bf17 26 69 4e 65 78 74 45 6e 64 29 3b 0a 20 20 20 20  &iNextEnd);.    
2bf18 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
2bf19 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
2bf1a 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  c;.      }..    
2bf1b 20 20 61 73 73 65 72 74 28 20 69 4e 65 78 74 53    assert( iNextS
2bf1c 74 61 72 74 3c 3d 69 4e 65 78 74 45 6e 64 20 29  tart<=iNextEnd )
2bf1d 3b 0a 20 20 20 20 20 20 69 53 74 61 72 74 43 68  ;.      iStartCh
2bf1e 69 6c 64 20 3d 20 69 4e 65 78 74 53 74 61 72 74  ild = iNextStart
2bf1f 3b 0a 20 20 20 20 20 20 69 45 6e 64 43 68 69 6c  ;.      iEndChil
2bf20 64 20 3d 20 69 4e 65 78 74 45 6e 64 3b 0a 20 20  d = iNextEnd;.  
2bf21 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
2bf22 69 53 74 61 72 74 43 68 69 6c 64 3c 3d 69 4c 65  iStartChild<=iLe
2bf23 61 76 65 73 45 6e 64 20 29 3b 0a 20 20 20 20 61  avesEnd );.    a
2bf24 73 73 65 72 74 28 20 69 45 6e 64 43 68 69 6c 64  ssert( iEndChild
2bf25 3c 3d 69 4c 65 61 76 65 73 45 6e 64 20 29 3b 0a  <=iLeavesEnd );.
2bf26 0a 20 20 20 20 2f 2a 20 53 63 61 6e 20 74 68 72  .    /* Scan thr
2bf27 6f 75 67 68 20 74 68 65 20 6c 65 61 66 20 73 65  ough the leaf se
2bf28 67 6d 65 6e 74 73 20 66 6f 72 20 64 6f 63 6c 69  gments for docli
2bf29 73 74 73 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75  sts. */.    retu
2bf2a 72 6e 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65  rn loadSegmentLe
2bf2b 61 76 65 73 28 76 2c 20 69 53 74 61 72 74 43 68  aves(v, iStartCh
2bf2c 69 6c 64 2c 20 69 45 6e 64 43 68 69 6c 64 2c 0a  ild, iEndChild,.
2bf2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bf2e 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54 65               pTe
2bf2f 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65  rm, nTerm, isPre
2bf30 66 69 78 2c 20 6f 75 74 29 3b 0a 20 20 7d 0a 7d  fix, out);.  }.}
2bf31 0a 0a 2f 2a 20 43 61 6c 6c 20 6c 6f 61 64 53 65  ../* Call loadSe
2bf32 67 6d 65 6e 74 49 6e 74 28 29 20 74 6f 20 63 6f  gmentInt() to co
2bf33 6c 6c 65 63 74 20 74 68 65 20 64 6f 63 6c 69 73  llect the doclis
2bf34 74 20 66 6f 72 20 70 54 65 72 6d 2f 6e 54 65 72  t for pTerm/nTer
2bf35 6d 2c 20 74 68 65 6e 0a 2a 2a 20 6d 65 72 67 65  m, then.** merge
2bf36 20 69 74 73 20 64 6f 63 6c 69 73 74 20 6f 76 65   its doclist ove
2bf37 72 20 2a 6f 75 74 20 28 61 6e 79 20 64 75 70 6c  r *out (any dupl
2bf38 69 63 61 74 65 20 64 6f 63 6c 69 73 74 73 20 72  icate doclists r
2bf39 65 61 64 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20  ead from the.** 
2bf3a 73 65 67 6d 65 6e 74 20 72 6f 6f 74 65 64 20 61  segment rooted a
2bf3b 74 20 70 44 61 74 61 20 77 69 6c 6c 20 6f 76 65  t pData will ove
2bf3c 72 77 72 69 74 65 20 74 68 6f 73 65 20 69 6e 20  rwrite those in 
2bf3d 2a 6f 75 74 29 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44  *out)..*/./* TOD
2bf3e 4f 28 73 68 65 73 73 29 20 43 6f 6e 73 69 64 65  O(shess) Conside
2bf3f 72 20 63 68 61 6e 67 69 6e 67 20 74 68 69 73 20  r changing this 
2bf40 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  to determine the
2bf41 20 64 65 70 74 68 20 6f 66 20 74 68 65 0a 2a 2a   depth of the.**
2bf42 20 6c 65 61 76 65 73 20 75 73 69 6e 67 20 65 69   leaves using ei
2bf43 74 68 65 72 20 74 68 65 20 66 69 72 73 74 20 63  ther the first c
2bf44 68 61 72 61 63 74 65 72 73 20 6f 66 20 69 6e 74  haracters of int
2bf45 65 72 69 6f 72 20 6e 6f 64 65 73 20 28 77 68 65  erior nodes (whe
2bf46 6e 0a 2a 2a 20 3d 3d 31 2c 20 77 65 27 72 65 20  n.** ==1, we're 
2bf47 6f 6e 65 20 6c 65 76 65 6c 20 61 62 6f 76 65 20  one level above 
2bf48 74 68 65 20 6c 65 61 76 65 73 29 2c 20 6f 72 20  the leaves), or 
2bf49 74 68 65 20 66 69 72 73 74 20 63 68 61 72 61 63  the first charac
2bf4a 74 65 72 20 6f 66 0a 2a 2a 20 74 68 65 20 72 6f  ter of.** the ro
2bf4b 6f 74 20 28 77 68 69 63 68 20 77 69 6c 6c 20 64  ot (which will d
2bf4c 65 73 63 72 69 62 65 20 74 68 65 20 68 65 69 67  escribe the heig
2bf4d 68 74 20 6f 66 20 74 68 65 20 74 72 65 65 20 64  ht of the tree d
2bf4e 69 72 65 63 74 6c 79 29 2e 0a 2a 2a 20 45 69 74  irectly)..** Eit
2bf4f 68 65 72 20 66 65 65 6c 73 20 73 6f 6d 65 77 68  her feels somewh
2bf50 61 74 20 74 72 69 63 6b 79 20 74 6f 20 6d 65 2e  at tricky to me.
2bf51 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  .*/./* TODO(shes
2bf52 73 29 20 54 68 65 20 63 75 72 72 65 6e 74 20 6d  s) The current m
2bf53 65 72 67 65 20 69 73 20 6c 69 6b 65 6c 79 20 74  erge is likely t
2bf54 6f 20 62 65 20 73 6c 6f 77 20 66 6f 72 20 6c 61  o be slow for la
2bf55 72 67 65 0a 2a 2a 20 64 6f 63 6c 69 73 74 73 20  rge.** doclists 
2bf56 28 74 68 6f 75 67 68 20 69 74 20 73 68 6f 75 6c  (though it shoul
2bf57 64 20 70 72 6f 63 65 73 73 20 66 72 6f 6d 20 6e  d process from n
2bf58 65 77 65 73 74 2f 73 6d 61 6c 6c 65 73 74 20 74  ewest/smallest t
2bf59 6f 0a 2a 2a 20 6f 6c 64 65 73 74 2f 6c 61 72 67  o.** oldest/larg
2bf5a 65 73 74 2c 20 73 6f 20 69 74 20 6d 61 79 20 6e  est, so it may n
2bf5b 6f 74 20 62 65 20 74 68 61 74 20 62 61 64 29 2e  ot be that bad).
2bf5c 20 20 49 74 20 6d 69 67 68 74 20 62 65 20 75 73    It might be us
2bf5d 65 66 75 6c 20 74 6f 0a 2a 2a 20 6d 6f 64 69 66  eful to.** modif
2bf5e 79 20 74 68 69 6e 67 73 20 74 6f 20 61 6c 6c 6f  y things to allo
2bf5f 77 20 66 6f 72 20 4e 2d 77 61 79 20 6d 65 72 67  w for N-way merg
2bf60 69 6e 67 2e 20 20 54 68 69 73 20 63 6f 75 6c 64  ing.  This could
2bf61 20 65 69 74 68 65 72 20 62 65 0a 2a 2a 20 77 69   either be.** wi
2bf62 74 68 69 6e 20 61 20 73 65 67 6d 65 6e 74 2c 20  thin a segment, 
2bf63 77 69 74 68 20 70 61 69 72 77 69 73 65 20 6d 65  with pairwise me
2bf64 72 67 65 73 20 61 63 72 6f 73 73 20 73 65 67 6d  rges across segm
2bf65 65 6e 74 73 2c 20 6f 72 20 61 63 72 6f 73 73 0a  ents, or across.
2bf66 2a 2a 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73 20  ** all segments 
2bf67 61 74 20 6f 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74  at once..*/.stat
2bf68 69 63 20 69 6e 74 20 6c 6f 61 64 53 65 67 6d 65  ic int loadSegme
2bf69 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  nt(fulltext_vtab
2bf6a 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20   *v, const char 
2bf6b 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74  *pData, int nDat
2bf6c 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  a,.             
2bf6d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2bf6e 5f 69 6e 74 36 34 20 69 4c 65 61 76 65 73 45 6e  _int64 iLeavesEn
2bf6f 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
2bf70 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
2bf71 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74  char *pTerm, int
2bf72 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73 50 72   nTerm, int isPr
2bf73 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20  efix,.          
2bf74 20 20 20 20 20 20 20 20 20 20 20 20 20 44 61 74               Dat
2bf75 61 42 75 66 66 65 72 20 2a 6f 75 74 29 7b 0a 20  aBuffer *out){. 
2bf76 20 44 61 74 61 42 75 66 66 65 72 20 72 65 73 75   DataBuffer resu
2bf77 6c 74 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  lt;.  int rc;.. 
2bf78 20 61 73 73 65 72 74 28 20 6e 44 61 74 61 3e 31   assert( nData>1
2bf79 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63   );..  /* This c
2bf7a 6f 64 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72  ode should never
2bf7b 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74 68 20   be called with 
2bf7c 62 75 66 66 65 72 65 64 20 75 70 64 61 74 65 73  buffered updates
2bf7d 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 76  . */.  assert( v
2bf7e 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c 30  ->nPendingData<0
2bf7f 20 29 3b 0a 0a 20 20 64 61 74 61 42 75 66 66 65   );..  dataBuffe
2bf80 72 49 6e 69 74 28 26 72 65 73 75 6c 74 2c 20 30  rInit(&result, 0
2bf81 29 3b 0a 20 20 72 63 20 3d 20 6c 6f 61 64 53 65  );.  rc = loadSe
2bf82 67 6d 65 6e 74 49 6e 74 28 76 2c 20 70 44 61 74  gmentInt(v, pDat
2bf83 61 2c 20 6e 44 61 74 61 2c 20 69 4c 65 61 76 65  a, nData, iLeave
2bf84 73 45 6e 64 2c 0a 20 20 20 20 20 20 20 20 20 20  sEnd,.          
2bf85 20 20 20 20 20 20 20 20 20 20 20 20 70 54 65 72              pTer
2bf86 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66  m, nTerm, isPref
2bf87 69 78 2c 20 26 72 65 73 75 6c 74 29 3b 0a 20 20  ix, &result);.  
2bf88 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
2bf89 4b 20 26 26 20 72 65 73 75 6c 74 2e 6e 44 61 74  K && result.nDat
2bf8a 61 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6f  a>0 ){.    if( o
2bf8b 75 74 2d 3e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a  ut->nData==0 ){.
2bf8c 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72        DataBuffer
2bf8d 20 74 6d 70 20 3d 20 2a 6f 75 74 3b 0a 20 20 20   tmp = *out;.   
2bf8e 20 20 20 2a 6f 75 74 20 3d 20 72 65 73 75 6c 74     *out = result
2bf8f 3b 0a 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d  ;.      result =
2bf90 20 74 6d 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   tmp;.    }else{
2bf91 0a 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65  .      DataBuffe
2bf92 72 20 6d 65 72 67 65 64 3b 0a 20 20 20 20 20 20  r merged;.      
2bf93 44 4c 52 65 61 64 65 72 20 72 65 61 64 65 72 73  DLReader readers
2bf94 5b 32 5d 3b 0a 0a 20 20 20 20 20 20 64 6c 72 49  [2];..      dlrI
2bf95 6e 69 74 28 26 72 65 61 64 65 72 73 5b 30 5d 2c  nit(&readers[0],
2bf96 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 6f 75 74   DL_DEFAULT, out
2bf97 2d 3e 70 44 61 74 61 2c 20 6f 75 74 2d 3e 6e 44  ->pData, out->nD
2bf98 61 74 61 29 3b 0a 20 20 20 20 20 20 64 6c 72 49  ata);.      dlrI
2bf99 6e 69 74 28 26 72 65 61 64 65 72 73 5b 31 5d 2c  nit(&readers[1],
2bf9a 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 72 65 73   DL_DEFAULT, res
2bf9b 75 6c 74 2e 70 44 61 74 61 2c 20 72 65 73 75 6c  ult.pData, resul
2bf9c 74 2e 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 20  t.nData);.      
2bf9d 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26  dataBufferInit(&
2bf9e 6d 65 72 67 65 64 2c 20 6f 75 74 2d 3e 6e 44 61  merged, out->nDa
2bf9f 74 61 2b 72 65 73 75 6c 74 2e 6e 44 61 74 61 29  ta+result.nData)
2bfa0 3b 0a 20 20 20 20 20 20 64 6f 63 4c 69 73 74 4d  ;.      docListM
2bfa1 65 72 67 65 28 26 6d 65 72 67 65 64 2c 20 72 65  erge(&merged, re
2bfa2 61 64 65 72 73 2c 20 32 29 3b 0a 20 20 20 20 20  aders, 2);.     
2bfa3 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72   dataBufferDestr
2bfa4 6f 79 28 6f 75 74 29 3b 0a 20 20 20 20 20 20 2a  oy(out);.      *
2bfa5 6f 75 74 20 3d 20 6d 65 72 67 65 64 3b 0a 20 20  out = merged;.  
2bfa6 20 20 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26      dlrDestroy(&
2bfa7 72 65 61 64 65 72 73 5b 30 5d 29 3b 0a 20 20 20  readers[0]);.   
2bfa8 20 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72     dlrDestroy(&r
2bfa9 65 61 64 65 72 73 5b 31 5d 29 3b 0a 20 20 20 20  eaders[1]);.    
2bfaa 7d 0a 20 20 7d 0a 20 20 64 61 74 61 42 75 66 66  }.  }.  dataBuff
2bfab 65 72 44 65 73 74 72 6f 79 28 26 72 65 73 75 6c  erDestroy(&resul
2bfac 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  t);.  return rc;
2bfad 0a 7d 0a 0a 2f 2a 20 53 63 61 6e 20 74 68 65 20  .}../* Scan the 
2bfae 64 61 74 61 62 61 73 65 20 61 6e 64 20 6d 65 72  database and mer
2bfaf 67 65 20 74 6f 67 65 74 68 65 72 20 74 68 65 20  ge together the 
2bfb0 70 6f 73 74 69 6e 67 20 6c 69 73 74 73 20 66 6f  posting lists fo
2bfb1 72 20 74 68 65 20 74 65 72 6d 0a 2a 2a 20 69 6e  r the term.** in
2bfb2 74 6f 20 2a 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74  to *out..*/.stat
2bfb3 69 63 20 69 6e 74 20 74 65 72 6d 53 65 6c 65 63  ic int termSelec
2bfb4 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  t(fulltext_vtab 
2bfb5 2a 76 2c 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c  *v, int iColumn,
2bfb6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2bfb7 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
2bfb8 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54  r *pTerm, int nT
2bfb9 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69  erm, int isPrefi
2bfba 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  x,.             
2bfbb 20 20 20 20 20 20 20 20 20 44 6f 63 4c 69 73 74           DocList
2bfbc 54 79 70 65 20 69 54 79 70 65 2c 20 44 61 74 61  Type iType, Data
2bfbd 42 75 66 66 65 72 20 2a 6f 75 74 29 7b 0a 20 20  Buffer *out){.  
2bfbe 44 61 74 61 42 75 66 66 65 72 20 64 6f 63 6c 69  DataBuffer docli
2bfbf 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  st;.  sqlite3_st
2bfc0 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20  mt *s;.  int rc 
2bfc1 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d  = sql_get_statem
2bfc2 65 6e 74 28 76 2c 20 53 45 47 44 49 52 5f 53 45  ent(v, SEGDIR_SE
2bfc3 4c 45 43 54 5f 41 4c 4c 5f 53 54 4d 54 2c 20 26  LECT_ALL_STMT, &
2bfc4 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  s);.  if( rc!=SQ
2bfc5 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
2bfc6 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20   rc;..  /* This 
2bfc7 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e 65 76 65  code should neve
2bfc8 72 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74 68  r be called with
2bfc9 20 62 75 66 66 65 72 65 64 20 75 70 64 61 74 65   buffered update
2bfca 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  s. */.  assert( 
2bfcb 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c  v->nPendingData<
2bfcc 30 20 29 3b 0a 0a 20 20 64 61 74 61 42 75 66 66  0 );..  dataBuff
2bfcd 65 72 49 6e 69 74 28 26 64 6f 63 6c 69 73 74 2c  erInit(&doclist,
2bfce 20 30 29 3b 0a 0a 20 20 2f 2a 20 54 72 61 76 65   0);..  /* Trave
2bfcf 72 73 65 20 74 68 65 20 73 65 67 6d 65 6e 74 73  rse the segments
2bfd0 20 66 72 6f 6d 20 6f 6c 64 65 73 74 20 74 6f 20   from oldest to 
2bfd1 6e 65 77 65 73 74 20 73 6f 20 74 68 61 74 20 6e  newest so that n
2bfd2 65 77 65 72 20 64 6f 63 6c 69 73 74 0a 20 20 2a  ewer doclist.  *
2bfd3 2a 20 65 6c 65 6d 65 6e 74 73 20 66 6f 72 20 67  * elements for g
2bfd4 69 76 65 6e 20 64 6f 63 69 64 73 20 6f 76 65 72  iven docids over
2bfd5 77 72 69 74 65 20 6f 6c 64 65 72 20 65 6c 65 6d  write older elem
2bfd6 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 77 68 69  ents..  */.  whi
2bfd7 6c 65 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65  le( (rc = sqlite
2bfd8 33 5f 73 74 65 70 28 73 29 29 3d 3d 53 51 4c 49  3_step(s))==SQLI
2bfd9 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 63 6f  TE_ROW ){.    co
2bfda 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 20  nst char *pData 
2bfdb 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
2bfdc 5f 62 6c 6f 62 28 73 2c 20 30 29 3b 0a 20 20 20  _blob(s, 0);.   
2bfdd 20 63 6f 6e 73 74 20 69 6e 74 20 6e 44 61 74 61   const int nData
2bfde 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
2bfdf 6e 5f 62 79 74 65 73 28 73 2c 20 30 29 3b 0a 20  n_bytes(s, 0);. 
2bfe0 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 5f     const sqlite_
2bfe1 69 6e 74 36 34 20 69 4c 65 61 76 65 73 45 6e 64  int64 iLeavesEnd
2bfe2 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
2bfe3 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29 3b 0a 20  n_int64(s, 1);. 
2bfe4 20 20 20 72 63 20 3d 20 6c 6f 61 64 53 65 67 6d     rc = loadSegm
2bfe5 65 6e 74 28 76 2c 20 70 44 61 74 61 2c 20 6e 44  ent(v, pData, nD
2bfe6 61 74 61 2c 20 69 4c 65 61 76 65 73 45 6e 64 2c  ata, iLeavesEnd,
2bfe7 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69   pTerm, nTerm, i
2bfe8 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20  sPrefix,.       
2bfe9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 64                &d
2bfea 6f 63 6c 69 73 74 29 3b 0a 20 20 20 20 69 66 28  oclist);.    if(
2bfeb 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2bfec 20 67 6f 74 6f 20 65 72 72 3b 0a 20 20 7d 0a 20   goto err;.  }. 
2bfed 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
2bfee 44 4f 4e 45 20 29 7b 0a 20 20 20 20 69 66 28 20  DONE ){.    if( 
2bfef 64 6f 63 6c 69 73 74 2e 6e 44 61 74 61 21 3d 30  doclist.nData!=0
2bff0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 4f 44   ){.      /* TOD
2bff1 4f 28 73 68 65 73 73 29 20 54 68 65 20 6f 6c 64  O(shess) The old
2bff2 20 74 65 72 6d 5f 73 65 6c 65 63 74 5f 61 6c 6c   term_select_all
2bff3 28 29 20 63 6f 64 65 20 61 70 70 6c 69 65 64 20  () code applied 
2bff4 74 68 65 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 20  the column.     
2bff5 20 2a 2a 20 72 65 73 74 72 69 63 74 20 61 73 20   ** restrict as 
2bff6 77 65 20 6d 65 72 67 65 64 20 73 65 67 6d 65 6e  we merged segmen
2bff7 74 73 2c 20 6c 65 61 64 69 6e 67 20 74 6f 20 73  ts, leading to s
2bff8 6d 61 6c 6c 65 72 20 62 75 66 66 65 72 73 2e 0a  maller buffers..
2bff9 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73        ** This is
2bffa 20 70 72 6f 62 61 62 6c 79 20 77 6f 72 74 68 77   probably worthw
2bffb 68 69 6c 65 20 74 6f 20 62 72 69 6e 67 20 62 61  hile to bring ba
2bffc 63 6b 2c 20 6f 6e 63 65 20 74 68 65 20 6e 65 77  ck, once the new
2bffd 20 73 74 6f 72 61 67 65 0a 20 20 20 20 20 20 2a   storage.      *
2bffe 2a 20 73 79 73 74 65 6d 20 69 73 20 63 68 65 63  * system is chec
2bfff 6b 65 64 20 69 6e 2e 0a 20 20 20 20 20 20 2a 2f  ked in..      */
2c000 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 75  .      if( iColu
2c001 6d 6e 3d 3d 76 2d 3e 6e 43 6f 6c 75 6d 6e 29 20  mn==v->nColumn) 
2c002 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20  iColumn = -1;.  
2c003 20 20 20 20 64 6f 63 4c 69 73 74 54 72 69 6d 28      docListTrim(
2c004 44 4c 5f 44 45 46 41 55 4c 54 2c 20 64 6f 63 6c  DL_DEFAULT, docl
2c005 69 73 74 2e 70 44 61 74 61 2c 20 64 6f 63 6c 69  ist.pData, docli
2c006 73 74 2e 6e 44 61 74 61 2c 0a 20 20 20 20 20 20  st.nData,.      
2c007 20 20 20 20 20 20 20 20 20 20 20 20 69 43 6f 6c              iCol
2c008 75 6d 6e 2c 20 69 54 79 70 65 2c 20 6f 75 74 29  umn, iType, out)
2c009 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
2c00a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
2c00b 0a 20 65 72 72 3a 0a 20 20 64 61 74 61 42 75 66  . err:.  dataBuf
2c00c 66 65 72 44 65 73 74 72 6f 79 28 26 64 6f 63 6c  ferDestroy(&docl
2c00d 69 73 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ist);.  return r
2c00e 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  c;.}../*********
2c00f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c011 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c012 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 55 73 65 64  *******/./* Used
2c013 20 74 6f 20 68 6f 6c 64 20 68 61 73 68 74 61 62   to hold hashtab
2c014 6c 65 20 64 61 74 61 20 66 6f 72 20 73 6f 72 74  le data for sort
2c015 69 6e 67 2e 20 2a 2f 0a 74 79 70 65 64 65 66 20  ing. */.typedef 
2c016 73 74 72 75 63 74 20 54 65 72 6d 44 61 74 61 20  struct TermData 
2c017 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  {.  const char *
2c018 70 54 65 72 6d 3b 0a 20 20 69 6e 74 20 6e 54 65  pTerm;.  int nTe
2c019 72 6d 3b 0a 20 20 44 4c 43 6f 6c 6c 65 63 74 6f  rm;.  DLCollecto
2c01a 72 20 2a 70 43 6f 6c 6c 65 63 74 6f 72 3b 0a 7d  r *pCollector;.}
2c01b 20 54 65 72 6d 44 61 74 61 3b 0a 0a 2f 2a 20 4f   TermData;../* O
2c01c 72 64 65 72 73 20 54 65 72 6d 44 61 74 61 20 65  rders TermData e
2c01d 6c 65 6d 65 6e 74 73 20 69 6e 20 73 74 72 63 6d  lements in strcm
2c01e 70 20 66 61 73 68 69 6f 6e 20 28 20 3c 30 20 66  p fashion ( <0 f
2c01f 6f 72 20 6c 65 73 73 2d 74 68 61 6e 2c 20 30 0a  or less-than, 0.
2c020 2a 2a 20 66 6f 72 20 65 71 75 61 6c 2c 20 3e 30  ** for equal, >0
2c021 20 66 6f 72 20 67 72 65 61 74 65 72 2d 74 68 61   for greater-tha
2c022 6e 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  n)..*/.static in
2c023 74 20 74 65 72 6d 44 61 74 61 43 6d 70 28 63 6f  t termDataCmp(co
2c024 6e 73 74 20 76 6f 69 64 20 2a 61 76 2c 20 63 6f  nst void *av, co
2c025 6e 73 74 20 76 6f 69 64 20 2a 62 76 29 7b 0a 20  nst void *bv){. 
2c026 20 63 6f 6e 73 74 20 54 65 72 6d 44 61 74 61 20   const TermData 
2c027 2a 61 20 3d 20 28 63 6f 6e 73 74 20 54 65 72 6d  *a = (const Term
2c028 44 61 74 61 20 2a 29 61 76 3b 0a 20 20 63 6f 6e  Data *)av;.  con
2c029 73 74 20 54 65 72 6d 44 61 74 61 20 2a 62 20 3d  st TermData *b =
2c02a 20 28 63 6f 6e 73 74 20 54 65 72 6d 44 61 74 61   (const TermData
2c02b 20 2a 29 62 76 3b 0a 20 20 69 6e 74 20 6e 20 3d   *)bv;.  int n =
2c02c 20 61 2d 3e 6e 54 65 72 6d 3c 62 2d 3e 6e 54 65   a->nTerm<b->nTe
2c02d 72 6d 20 3f 20 61 2d 3e 6e 54 65 72 6d 20 3a 20  rm ? a->nTerm : 
2c02e 62 2d 3e 6e 54 65 72 6d 3b 0a 20 20 69 6e 74 20  b->nTerm;.  int 
2c02f 63 20 3d 20 6d 65 6d 63 6d 70 28 61 2d 3e 70 54  c = memcmp(a->pT
2c030 65 72 6d 2c 20 62 2d 3e 70 54 65 72 6d 2c 20 6e  erm, b->pTerm, n
2c031 29 3b 0a 20 20 69 66 28 20 63 21 3d 30 20 29 20  );.  if( c!=0 ) 
2c032 72 65 74 75 72 6e 20 63 3b 0a 20 20 72 65 74 75  return c;.  retu
2c033 72 6e 20 61 2d 3e 6e 54 65 72 6d 2d 62 2d 3e 6e  rn a->nTerm-b->n
2c034 54 65 72 6d 3b 0a 7d 0a 0a 2f 2a 20 4f 72 64 65  Term;.}../* Orde
2c035 72 20 70 54 65 72 6d 73 20 64 61 74 61 20 62 79  r pTerms data by
2c036 20 74 65 72 6d 2c 20 74 68 65 6e 20 77 72 69 74   term, then writ
2c037 65 20 61 20 6e 65 77 20 6c 65 76 65 6c 20 30 20  e a new level 0 
2c038 73 65 67 6d 65 6e 74 20 75 73 69 6e 67 0a 2a 2a  segment using.**
2c039 20 4c 65 61 66 57 72 69 74 65 72 2e 0a 2a 2f 0a   LeafWriter..*/.
2c03a 73 74 61 74 69 63 20 69 6e 74 20 77 72 69 74 65  static int write
2c03b 5a 65 72 6f 53 65 67 6d 65 6e 74 28 66 75 6c 6c  ZeroSegment(full
2c03c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 66 74  text_vtab *v, ft
2c03d 73 33 48 61 73 68 20 2a 70 54 65 72 6d 73 29 7b  s3Hash *pTerms){
2c03e 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20  .  fts3HashElem 
2c03f 2a 65 3b 0a 20 20 69 6e 74 20 69 64 78 2c 20 72  *e;.  int idx, r
2c040 63 2c 20 69 2c 20 6e 3b 0a 20 20 54 65 72 6d 44  c, i, n;.  TermD
2c041 61 74 61 20 2a 70 44 61 74 61 3b 0a 20 20 4c 65  ata *pData;.  Le
2c042 61 66 57 72 69 74 65 72 20 77 72 69 74 65 72 3b  afWriter writer;
2c043 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 64 6c  .  DataBuffer dl
2c044 3b 0a 0a 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e  ;..  /* Determin
2c045 65 20 74 68 65 20 6e 65 78 74 20 69 6e 64 65 78  e the next index
2c046 20 61 74 20 6c 65 76 65 6c 20 30 2c 20 6d 65 72   at level 0, mer
2c047 67 69 6e 67 20 61 73 20 6e 65 63 65 73 73 61 72  ging as necessar
2c048 79 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73 65 67  y. */.  rc = seg
2c049 64 69 72 4e 65 78 74 49 6e 64 65 78 28 76 2c 20  dirNextIndex(v, 
2c04a 30 2c 20 26 69 64 78 29 3b 0a 20 20 69 66 28 20  0, &idx);.  if( 
2c04b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2c04c 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 6e 20  return rc;..  n 
2c04d 3d 20 66 74 73 33 48 61 73 68 43 6f 75 6e 74 28  = fts3HashCount(
2c04e 70 54 65 72 6d 73 29 3b 0a 20 20 70 44 61 74 61  pTerms);.  pData
2c04f 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
2c050 63 28 6e 2a 73 69 7a 65 6f 66 28 54 65 72 6d 44  c(n*sizeof(TermD
2c051 61 74 61 29 29 3b 0a 0a 20 20 66 6f 72 28 69 20  ata));..  for(i 
2c052 3d 20 30 2c 20 65 20 3d 20 66 74 73 33 48 61 73  = 0, e = fts3Has
2c053 68 46 69 72 73 74 28 70 54 65 72 6d 73 29 3b 20  hFirst(pTerms); 
2c054 65 3b 20 69 2b 2b 2c 20 65 20 3d 20 66 74 73 33  e; i++, e = fts3
2c055 48 61 73 68 4e 65 78 74 28 65 29 29 7b 0a 20 20  HashNext(e)){.  
2c056 20 20 61 73 73 65 72 74 28 20 69 3c 6e 20 29 3b    assert( i<n );
2c057 0a 20 20 20 20 70 44 61 74 61 5b 69 5d 2e 70 54  .    pData[i].pT
2c058 65 72 6d 20 3d 20 66 74 73 33 48 61 73 68 4b 65  erm = fts3HashKe
2c059 79 28 65 29 3b 0a 20 20 20 20 70 44 61 74 61 5b  y(e);.    pData[
2c05a 69 5d 2e 6e 54 65 72 6d 20 3d 20 66 74 73 33 48  i].nTerm = fts3H
2c05b 61 73 68 4b 65 79 73 69 7a 65 28 65 29 3b 0a 20  ashKeysize(e);. 
2c05c 20 20 20 70 44 61 74 61 5b 69 5d 2e 70 43 6f 6c     pData[i].pCol
2c05d 6c 65 63 74 6f 72 20 3d 20 66 74 73 33 48 61 73  lector = fts3Has
2c05e 68 44 61 74 61 28 65 29 3b 0a 20 20 7d 0a 20 20  hData(e);.  }.  
2c05f 61 73 73 65 72 74 28 20 69 3d 3d 6e 20 29 3b 0a  assert( i==n );.
2c060 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  .  /* TODO(shess
2c061 29 20 53 68 6f 75 6c 64 20 77 65 20 61 6c 6c 6f  ) Should we allo
2c062 77 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 63  w user-defined c
2c063 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
2c064 65 73 2c 0a 20 20 2a 2a 20 68 65 72 65 3f 20 20  es,.  ** here?  
2c065 49 20 74 68 69 6e 6b 20 77 65 20 6f 6e 6c 79 20  I think we only 
2c066 6e 65 65 64 20 74 68 61 74 20 6f 6e 63 65 20 77  need that once w
2c067 65 20 73 75 70 70 6f 72 74 20 70 72 65 66 69 78  e support prefix
2c068 20 73 65 61 72 63 68 65 73 2e 0a 20 20 2a 2f 0a   searches..  */.
2c069 20 20 69 66 28 20 6e 3e 31 20 29 20 71 73 6f 72    if( n>1 ) qsor
2c06a 74 28 70 44 61 74 61 2c 20 6e 2c 20 73 69 7a 65  t(pData, n, size
2c06b 6f 66 28 2a 70 44 61 74 61 29 2c 20 74 65 72 6d  of(*pData), term
2c06c 44 61 74 61 43 6d 70 29 3b 0a 0a 20 20 2f 2a 20  DataCmp);..  /* 
2c06d 54 4f 44 4f 28 73 68 65 73 73 29 20 52 65 66 61  TODO(shess) Refa
2c06e 63 74 6f 72 20 73 6f 20 74 68 61 74 20 77 65 20  ctor so that we 
2c06f 63 61 6e 20 77 72 69 74 65 20 64 69 72 65 63 74  can write direct
2c070 6c 79 20 74 6f 20 74 68 65 20 73 65 67 6d 65 6e  ly to the segmen
2c071 74 0a 20 20 2a 2a 20 44 61 74 61 42 75 66 66 65  t.  ** DataBuffe
2c072 72 2c 20 61 73 20 68 61 70 70 65 6e 73 20 66 6f  r, as happens fo
2c073 72 20 73 65 67 6d 65 6e 74 20 6d 65 72 67 65 73  r segment merges
2c074 2e 0a 20 20 2a 2f 0a 20 20 6c 65 61 66 57 72 69  ..  */.  leafWri
2c075 74 65 72 49 6e 69 74 28 30 2c 20 69 64 78 2c 20  terInit(0, idx, 
2c076 26 77 72 69 74 65 72 29 3b 0a 20 20 64 61 74 61  &writer);.  data
2c077 42 75 66 66 65 72 49 6e 69 74 28 26 64 6c 2c 20  BufferInit(&dl, 
2c078 30 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  0);.  for(i=0; i
2c079 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 64 61  <n; i++){.    da
2c07a 74 61 42 75 66 66 65 72 52 65 73 65 74 28 26 64  taBufferReset(&d
2c07b 6c 29 3b 0a 20 20 20 20 64 6c 63 41 64 64 44 6f  l);.    dlcAddDo
2c07c 63 6c 69 73 74 28 70 44 61 74 61 5b 69 5d 2e 70  clist(pData[i].p
2c07d 43 6f 6c 6c 65 63 74 6f 72 2c 20 26 64 6c 29 3b  Collector, &dl);
2c07e 0a 20 20 20 20 72 63 20 3d 20 6c 65 61 66 57 72  .    rc = leafWr
2c07f 69 74 65 72 53 74 65 70 28 76 2c 20 26 77 72 69  iterStep(v, &wri
2c080 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ter,.           
2c081 20 20 20 20 20 20 20 20 20 20 20 20 20 70 44 61               pDa
2c082 74 61 5b 69 5d 2e 70 54 65 72 6d 2c 20 70 44 61  ta[i].pTerm, pDa
2c083 74 61 5b 69 5d 2e 6e 54 65 72 6d 2c 20 64 6c 2e  ta[i].nTerm, dl.
2c084 70 44 61 74 61 2c 20 64 6c 2e 6e 44 61 74 61 29  pData, dl.nData)
2c085 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
2c086 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
2c087 72 72 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 6c  rr;.  }.  rc = l
2c088 65 61 66 57 72 69 74 65 72 46 69 6e 61 6c 69 7a  eafWriterFinaliz
2c089 65 28 76 2c 20 26 77 72 69 74 65 72 29 3b 0a 0a  e(v, &writer);..
2c08a 20 65 72 72 3a 0a 20 20 64 61 74 61 42 75 66 66   err:.  dataBuff
2c08b 65 72 44 65 73 74 72 6f 79 28 26 64 6c 29 3b 0a  erDestroy(&dl);.
2c08c 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
2c08d 44 61 74 61 29 3b 0a 20 20 6c 65 61 66 57 72 69  Data);.  leafWri
2c08e 74 65 72 44 65 73 74 72 6f 79 28 26 77 72 69 74  terDestroy(&writ
2c08f 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  er);.  return rc
2c090 3b 0a 7d 0a 0a 2f 2a 20 49 66 20 70 65 6e 64 69  ;.}../* If pendi
2c091 6e 67 54 65 72 6d 73 20 68 61 73 20 64 61 74 61  ngTerms has data
2c092 2c 20 66 72 65 65 20 69 74 2e 20 2a 2f 0a 73 74  , free it. */.st
2c093 61 74 69 63 20 69 6e 74 20 63 6c 65 61 72 50 65  atic int clearPe
2c094 6e 64 69 6e 67 54 65 72 6d 73 28 66 75 6c 6c 74  ndingTerms(fullt
2c095 65 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20  ext_vtab *v){.  
2c096 69 66 28 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44  if( v->nPendingD
2c097 61 74 61 3e 3d 30 20 29 7b 0a 20 20 20 20 66 74  ata>=0 ){.    ft
2c098 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 3b 0a 20  s3HashElem *e;. 
2c099 20 20 20 66 6f 72 28 65 3d 66 74 73 33 48 61 73     for(e=fts3Has
2c09a 68 46 69 72 73 74 28 26 76 2d 3e 70 65 6e 64 69  hFirst(&v->pendi
2c09b 6e 67 54 65 72 6d 73 29 3b 20 65 3b 20 65 3d 66  ngTerms); e; e=f
2c09c 74 73 33 48 61 73 68 4e 65 78 74 28 65 29 29 7b  ts3HashNext(e)){
2c09d 0a 20 20 20 20 20 20 64 6c 63 44 65 6c 65 74 65  .      dlcDelete
2c09e 28 66 74 73 33 48 61 73 68 44 61 74 61 28 65 29  (fts3HashData(e)
2c09f 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 74 73  );.    }.    fts
2c0a0 33 48 61 73 68 43 6c 65 61 72 28 26 76 2d 3e 70  3HashClear(&v->p
2c0a1 65 6e 64 69 6e 67 54 65 72 6d 73 29 3b 0a 20 20  endingTerms);.  
2c0a2 20 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74    v->nPendingDat
2c0a3 61 20 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 72 65  a = -1;.  }.  re
2c0a4 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
2c0a5 7d 0a 0a 2f 2a 20 49 66 20 70 65 6e 64 69 6e 67  }../* If pending
2c0a6 54 65 72 6d 73 20 68 61 73 20 64 61 74 61 2c 20  Terms has data, 
2c0a7 66 6c 75 73 68 20 69 74 20 74 6f 20 61 20 6c 65  flush it to a le
2c0a8 76 65 6c 2d 7a 65 72 6f 20 73 65 67 6d 65 6e 74  vel-zero segment
2c0a9 2c 20 61 6e 64 0a 2a 2a 20 66 72 65 65 20 69 74  , and.** free it
2c0aa 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2c0ab 66 6c 75 73 68 50 65 6e 64 69 6e 67 54 65 72 6d  flushPendingTerm
2c0ac 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  s(fulltext_vtab 
2c0ad 2a 76 29 7b 0a 20 20 69 66 28 20 76 2d 3e 6e 50  *v){.  if( v->nP
2c0ae 65 6e 64 69 6e 67 44 61 74 61 3e 3d 30 20 29 7b  endingData>=0 ){
2c0af 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 77 72  .    int rc = wr
2c0b0 69 74 65 5a 65 72 6f 53 65 67 6d 65 6e 74 28 76  iteZeroSegment(v
2c0b1 2c 20 26 76 2d 3e 70 65 6e 64 69 6e 67 54 65 72  , &v->pendingTer
2c0b2 6d 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  ms);.    if( rc=
2c0b3 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 63 6c 65  =SQLITE_OK ) cle
2c0b4 61 72 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 76  arPendingTerms(v
2c0b5 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  );.    return rc
2c0b6 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
2c0b7 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20  QLITE_OK;.}../* 
2c0b8 49 66 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20  If pendingTerms 
2c0b9 69 73 20 22 74 6f 6f 20 62 69 67 22 2c 20 6f 72  is "too big", or
2c0ba 20 64 6f 63 69 64 20 69 73 20 6f 75 74 20 6f 66   docid is out of
2c0bb 20 6f 72 64 65 72 2c 20 66 6c 75 73 68 20 69 74   order, flush it
2c0bc 2e 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 2c  ..** Regardless,
2c0bd 20 62 65 20 63 65 72 74 61 69 6e 20 74 68 61 74   be certain that
2c0be 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20 69 73   pendingTerms is
2c0bf 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 66 6f 72   initialized for
2c0c0 20 75 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   use..*/.static 
2c0c1 69 6e 74 20 69 6e 69 74 50 65 6e 64 69 6e 67 54  int initPendingT
2c0c2 65 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76 74  erms(fulltext_vt
2c0c3 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e  ab *v, sqlite_in
2c0c4 74 36 34 20 69 44 6f 63 69 64 29 7b 0a 20 20 2f  t64 iDocid){.  /
2c0c5 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 45 78  * TODO(shess) Ex
2c0c6 70 6c 6f 72 65 20 77 68 65 74 68 65 72 20 70 61  plore whether pa
2c0c7 72 74 69 61 6c 6c 79 20 66 6c 75 73 68 69 6e 67  rtially flushing
2c0c8 20 74 68 65 20 62 75 66 66 65 72 20 6f 6e 0a 20   the buffer on. 
2c0c9 20 2a 2a 20 66 6f 72 63 65 64 2d 66 6c 75 73 68   ** forced-flush
2c0ca 20 77 6f 75 6c 64 20 70 72 6f 76 69 64 65 20 62   would provide b
2c0cb 65 74 74 65 72 20 70 65 72 66 6f 72 6d 61 6e 63  etter performanc
2c0cc 65 2e 20 20 49 20 73 75 73 70 65 63 74 20 74 68  e.  I suspect th
2c0cd 61 74 20 69 66 0a 20 20 2a 2a 20 77 65 20 6f 72  at if.  ** we or
2c0ce 64 65 72 65 64 20 74 68 65 20 64 6f 63 6c 69 73  dered the doclis
2c0cf 74 73 20 62 79 20 73 69 7a 65 20 61 6e 64 20 66  ts by size and f
2c0d0 6c 75 73 68 65 64 20 74 68 65 20 6c 61 72 67 65  lushed the large
2c0d1 73 74 20 75 6e 74 69 6c 20 74 68 65 0a 20 20 2a  st until the.  *
2c0d2 2a 20 62 75 66 66 65 72 20 77 61 73 20 68 61 6c  * buffer was hal
2c0d3 66 20 65 6d 70 74 79 2c 20 74 68 61 74 20 77 6f  f empty, that wo
2c0d4 75 6c 64 20 6c 65 74 20 74 68 65 20 6c 65 73 73  uld let the less
2c0d5 20 66 72 65 71 75 65 6e 74 20 74 65 72 6d 73 0a   frequent terms.
2c0d6 20 20 2a 2a 20 67 65 6e 65 72 61 74 65 20 6c 6f    ** generate lo
2c0d7 6e 67 65 72 20 64 6f 63 6c 69 73 74 73 2e 0a 20  nger doclists.. 
2c0d8 20 2a 2f 0a 20 20 69 66 28 20 69 44 6f 63 69 64   */.  if( iDocid
2c0d9 3c 3d 76 2d 3e 69 50 72 65 76 44 6f 63 69 64 20  <=v->iPrevDocid 
2c0da 7c 7c 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61  || v->nPendingDa
2c0db 74 61 3e 6b 50 65 6e 64 69 6e 67 54 68 72 65 73  ta>kPendingThres
2c0dc 68 6f 6c 64 20 29 7b 0a 20 20 20 20 69 6e 74 20  hold ){.    int 
2c0dd 72 63 20 3d 20 66 6c 75 73 68 50 65 6e 64 69 6e  rc = flushPendin
2c0de 67 54 65 72 6d 73 28 76 29 3b 0a 20 20 20 20 69  gTerms(v);.    i
2c0df 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2c0e0 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
2c0e1 7d 0a 20 20 69 66 28 20 76 2d 3e 6e 50 65 6e 64  }.  if( v->nPend
2c0e2 69 6e 67 44 61 74 61 3c 30 20 29 7b 0a 20 20 20  ingData<0 ){.   
2c0e3 20 66 74 73 33 48 61 73 68 49 6e 69 74 28 26 76   fts3HashInit(&v
2c0e4 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 2c 20  ->pendingTerms, 
2c0e5 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47  FTS3_HASH_STRING
2c0e6 2c 20 31 29 3b 0a 20 20 20 20 76 2d 3e 6e 50 65  , 1);.    v->nPe
2c0e7 6e 64 69 6e 67 44 61 74 61 20 3d 20 30 3b 0a 20  ndingData = 0;. 
2c0e8 20 7d 0a 20 20 76 2d 3e 69 50 72 65 76 44 6f 63   }.  v->iPrevDoc
2c0e9 69 64 20 3d 20 69 44 6f 63 69 64 3b 0a 20 20 72  id = iDocid;.  r
2c0ea 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2c0eb 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63  .}../* This func
2c0ec 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20  tion implements 
2c0ed 74 68 65 20 78 55 70 64 61 74 65 20 63 61 6c 6c  the xUpdate call
2c0ee 62 61 63 6b 3b 20 69 74 20 69 73 20 74 68 65 20  back; it is the 
2c0ef 74 6f 70 2d 6c 65 76 65 6c 20 65 6e 74 72 79 0a  top-level entry.
2c0f0 20 2a 20 70 6f 69 6e 74 20 66 6f 72 20 69 6e 73   * point for ins
2c0f1 65 72 74 69 6e 67 2c 20 64 65 6c 65 74 69 6e 67  erting, deleting
2c0f2 20 6f 72 20 75 70 64 61 74 69 6e 67 20 61 20 72   or updating a r
2c0f3 6f 77 20 69 6e 20 61 20 66 75 6c 6c 2d 74 65 78  ow in a full-tex
2c0f4 74 20 74 61 62 6c 65 2e 20 2a 2f 0a 73 74 61 74  t table. */.stat
2c0f5 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 55  ic int fulltextU
2c0f6 70 64 61 74 65 28 73 71 6c 69 74 65 33 5f 76 74  pdate(sqlite3_vt
2c0f7 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74 20 6e  ab *pVtab, int n
2c0f8 41 72 67 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  Arg, sqlite3_val
2c0f9 75 65 20 2a 2a 70 70 41 72 67 2c 0a 20 20 20 20  ue **ppArg,.    
2c0fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c0fb 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74        sqlite_int
2c0fc 36 34 20 2a 70 52 6f 77 69 64 29 7b 0a 20 20 66  64 *pRowid){.  f
2c0fd 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20  ulltext_vtab *v 
2c0fe 3d 20 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  = (fulltext_vtab
2c0ff 20 2a 29 20 70 56 74 61 62 3b 0a 20 20 69 6e 74   *) pVtab;.  int
2c100 20 72 63 3b 0a 0a 20 20 46 54 53 54 52 41 43 45   rc;..  FTSTRACE
2c101 28 28 22 46 54 53 33 20 55 70 64 61 74 65 20 25  (("FTS3 Update %
2c102 70 5c 6e 22 2c 20 70 56 74 61 62 29 29 3b 0a 0a  p\n", pVtab));..
2c103 20 20 69 66 28 20 6e 41 72 67 3c 32 20 29 7b 0a    if( nArg<2 ){.
2c104 20 20 20 20 72 63 20 3d 20 69 6e 64 65 78 5f 64      rc = index_d
2c105 65 6c 65 74 65 28 76 2c 20 73 71 6c 69 74 65 33  elete(v, sqlite3
2c106 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 70 70 41  _value_int64(ppA
2c107 72 67 5b 30 5d 29 29 3b 0a 20 20 7d 20 65 6c 73  rg[0]));.  } els
2c108 65 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61  e if( sqlite3_va
2c109 6c 75 65 5f 74 79 70 65 28 70 70 41 72 67 5b 30  lue_type(ppArg[0
2c10a 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 4e 55 4c  ]) != SQLITE_NUL
2c10b 4c 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6e 20 75  L ){.    /* An u
2c10c 70 64 61 74 65 3a 0a 20 20 20 20 20 2a 20 70 70  pdate:.     * pp
2c10d 41 72 67 5b 30 5d 20 3d 20 6f 6c 64 20 72 6f 77  Arg[0] = old row
2c10e 69 64 0a 20 20 20 20 20 2a 20 70 70 41 72 67 5b  id.     * ppArg[
2c10f 31 5d 20 3d 20 6e 65 77 20 72 6f 77 69 64 0a 20  1] = new rowid. 
2c110 20 20 20 20 2a 20 70 70 41 72 67 5b 32 2e 2e 32      * ppArg[2..2
2c111 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2d 31 5d 20 3d  +v->nColumn-1] =
2c112 20 76 61 6c 75 65 73 0a 20 20 20 20 20 2a 20 70   values.     * p
2c113 70 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d  pArg[2+v->nColum
2c114 6e 5d 20 3d 20 76 61 6c 75 65 20 66 6f 72 20 6d  n] = value for m
2c115 61 67 69 63 20 63 6f 6c 75 6d 6e 20 28 77 65 20  agic column (we 
2c116 69 67 6e 6f 72 65 20 74 68 69 73 29 0a 20 20 20  ignore this).   
2c117 20 20 2a 20 70 70 41 72 67 5b 32 2b 76 2d 3e 6e    * ppArg[2+v->n
2c118 43 6f 6c 75 6d 6e 2b 31 5d 20 3d 20 76 61 6c 75  Column+1] = valu
2c119 65 20 66 6f 72 20 64 6f 63 69 64 0a 20 20 20 20  e for docid.    
2c11a 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 5f 69   */.    sqlite_i
2c11b 6e 74 36 34 20 72 6f 77 69 64 20 3d 20 73 71 6c  nt64 rowid = sql
2c11c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34  ite3_value_int64
2c11d 28 70 70 41 72 67 5b 30 5d 29 3b 0a 20 20 20 20  (ppArg[0]);.    
2c11e 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  if( sqlite3_valu
2c11f 65 5f 74 79 70 65 28 70 70 41 72 67 5b 31 5d 29  e_type(ppArg[1])
2c120 20 21 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47   != SQLITE_INTEG
2c121 45 52 20 7c 7c 0a 20 20 20 20 20 20 20 20 73 71  ER ||.        sq
2c122 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36  lite3_value_int6
2c123 34 28 70 70 41 72 67 5b 31 5d 29 20 21 3d 20 72  4(ppArg[1]) != r
2c124 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 72 63  owid ){.      rc
2c125 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
2c126 20 20 2f 2a 20 77 65 20 64 6f 6e 27 74 20 61 6c    /* we don't al
2c127 6c 6f 77 20 63 68 61 6e 67 69 6e 67 20 74 68 65  low changing the
2c128 20 72 6f 77 69 64 20 2a 2f 0a 20 20 20 20 7d 65   rowid */.    }e
2c129 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 5f  lse if( sqlite3_
2c12a 76 61 6c 75 65 5f 74 79 70 65 28 70 70 41 72 67  value_type(ppArg
2c12b 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d  [2+v->nColumn+1]
2c12c 29 20 21 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45  ) != SQLITE_INTE
2c12d 47 45 52 20 7c 7c 0a 20 20 20 20 20 20 20 20 20  GER ||.         
2c12e 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c       sqlite3_val
2c12f 75 65 5f 69 6e 74 36 34 28 70 70 41 72 67 5b 32  ue_int64(ppArg[2
2c130 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 29 20  +v->nColumn+1]) 
2c131 21 3d 20 72 6f 77 69 64 20 29 7b 0a 20 20 20 20  != rowid ){.    
2c132 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
2c133 52 4f 52 3b 20 20 2f 2a 20 77 65 20 64 6f 6e 27  ROR;  /* we don'
2c134 74 20 61 6c 6c 6f 77 20 63 68 61 6e 67 69 6e 67  t allow changing
2c135 20 74 68 65 20 64 6f 63 69 64 20 2a 2f 0a 20 20   the docid */.  
2c136 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61    }else{.      a
2c137 73 73 65 72 74 28 20 6e 41 72 67 3d 3d 32 2b 76  ssert( nArg==2+v
2c138 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32 29 3b 0a 20 20  ->nColumn+2);.  
2c139 20 20 20 20 72 63 20 3d 20 69 6e 64 65 78 5f 75      rc = index_u
2c13a 70 64 61 74 65 28 76 2c 20 72 6f 77 69 64 2c 20  pdate(v, rowid, 
2c13b 26 70 70 41 72 67 5b 32 5d 29 3b 0a 20 20 20 20  &ppArg[2]);.    
2c13c 7d 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20  }.  } else {.   
2c13d 20 2f 2a 20 41 6e 20 69 6e 73 65 72 74 3a 0a 20   /* An insert:. 
2c13e 20 20 20 20 2a 20 70 70 41 72 67 5b 31 5d 20 3d      * ppArg[1] =
2c13f 20 72 65 71 75 65 73 74 65 64 20 72 6f 77 69 64   requested rowid
2c140 0a 20 20 20 20 20 2a 20 70 70 41 72 67 5b 32 2e  .     * ppArg[2.
2c141 2e 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2d 31 5d  .2+v->nColumn-1]
2c142 20 3d 20 76 61 6c 75 65 73 0a 20 20 20 20 20 2a   = values.     *
2c143 20 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c   ppArg[2+v->nCol
2c144 75 6d 6e 5d 20 3d 20 76 61 6c 75 65 20 66 6f 72  umn] = value for
2c145 20 6d 61 67 69 63 20 63 6f 6c 75 6d 6e 20 28 77   magic column (w
2c146 65 20 69 67 6e 6f 72 65 20 74 68 69 73 29 0a 20  e ignore this). 
2c147 20 20 20 20 2a 20 70 70 41 72 67 5b 32 2b 76 2d      * ppArg[2+v-
2c148 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 20 3d 20 76 61  >nColumn+1] = va
2c149 6c 75 65 20 66 6f 72 20 64 6f 63 69 64 0a 20 20  lue for docid.  
2c14a 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
2c14b 33 5f 76 61 6c 75 65 20 2a 70 52 65 71 75 65 73  3_value *pReques
2c14c 74 44 6f 63 69 64 20 3d 20 70 70 41 72 67 5b 32  tDocid = ppArg[2
2c14d 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 3b 0a  +v->nColumn+1];.
2c14e 20 20 20 20 61 73 73 65 72 74 28 20 6e 41 72 67      assert( nArg
2c14f 3d 3d 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32  ==2+v->nColumn+2
2c150 29 3b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54  );.    if( SQLIT
2c151 45 5f 4e 55 4c 4c 20 21 3d 20 73 71 6c 69 74 65  E_NULL != sqlite
2c152 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 70 52 65  3_value_type(pRe
2c153 71 75 65 73 74 44 6f 63 69 64 29 20 26 26 0a 20  questDocid) &&. 
2c154 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4e 55         SQLITE_NU
2c155 4c 4c 20 21 3d 20 73 71 6c 69 74 65 33 5f 76 61  LL != sqlite3_va
2c156 6c 75 65 5f 74 79 70 65 28 70 70 41 72 67 5b 31  lue_type(ppArg[1
2c157 5d 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  ]) ){.      /* T
2c158 4f 44 4f 28 73 68 65 73 73 29 20 43 6f 6e 73 69  ODO(shess) Consi
2c159 64 65 72 20 61 6c 6c 6f 77 69 6e 67 20 74 68 69  der allowing thi
2c15a 73 20 74 6f 20 77 6f 72 6b 20 69 66 20 74 68 65  s to work if the
2c15b 20 76 61 6c 75 65 73 20 61 72 65 0a 20 20 20 20   values are.    
2c15c 20 20 2a 2a 20 69 64 65 6e 74 69 63 61 6c 2e 20    ** identical. 
2c15d 20 49 27 6d 20 69 6e 63 6c 69 6e 65 64 20 74 6f   I'm inclined to
2c15e 20 64 69 73 63 6f 75 72 61 67 65 20 74 68 61 74   discourage that
2c15f 20 75 73 61 67 65 2c 20 74 68 6f 75 67 68 2c 0a   usage, though,.
2c160 20 20 20 20 20 20 2a 2a 20 67 69 76 65 6e 20 74        ** given t
2c161 68 61 74 20 62 6f 74 68 20 72 6f 77 69 64 20 61  hat both rowid a
2c162 6e 64 20 64 6f 63 69 64 20 61 72 65 20 73 70 65  nd docid are spe
2c163 63 69 61 6c 20 63 6f 6c 75 6d 6e 73 2e 20 20 42  cial columns.  B
2c164 65 74 74 65 72 0a 20 20 20 20 20 20 2a 2a 20 77  etter.      ** w
2c165 6f 75 6c 64 20 62 65 20 74 6f 20 64 65 66 69 6e  ould be to defin
2c166 65 20 6f 6e 65 20 6f 72 20 74 68 65 20 6f 74 68  e one or the oth
2c167 65 72 20 61 73 20 74 68 65 20 64 65 66 61 75 6c  er as the defaul
2c168 74 20 77 69 6e 6e 65 72 2c 0a 20 20 20 20 20 20  t winner,.      
2c169 2a 2a 20 62 75 74 20 73 68 6f 75 6c 64 20 69 74  ** but should it
2c16a 20 62 65 20 66 74 73 33 2d 63 65 6e 74 72 69 63   be fts3-centric
2c16b 20 28 64 6f 63 69 64 29 20 6f 72 20 53 51 4c 69   (docid) or SQLi
2c16c 74 65 2d 63 65 6e 74 72 69 63 0a 20 20 20 20 20  te-centric.     
2c16d 20 2a 2a 20 28 72 6f 77 69 64 29 3f 0a 20 20 20   ** (rowid)?.   
2c16e 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d     */.      rc =
2c16f 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
2c170 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2c171 69 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20  if( SQLITE_NULL 
2c172 3d 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  == sqlite3_value
2c173 5f 74 79 70 65 28 70 52 65 71 75 65 73 74 44 6f  _type(pRequestDo
2c174 63 69 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20  cid) ){.        
2c175 70 52 65 71 75 65 73 74 44 6f 63 69 64 20 3d 20  pRequestDocid = 
2c176 70 70 41 72 67 5b 31 5d 3b 0a 20 20 20 20 20 20  ppArg[1];.      
2c177 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 69 6e 64  }.      rc = ind
2c178 65 78 5f 69 6e 73 65 72 74 28 76 2c 20 70 52 65  ex_insert(v, pRe
2c179 71 75 65 73 74 44 6f 63 69 64 2c 20 26 70 70 41  questDocid, &ppA
2c17a 72 67 5b 32 5d 2c 20 70 52 6f 77 69 64 29 3b 0a  rg[2], pRowid);.
2c17b 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
2c17c 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69  urn rc;.}..stati
2c17d 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 53 79  c int fulltextSy
2c17e 6e 63 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  nc(sqlite3_vtab 
2c17f 2a 70 56 74 61 62 29 7b 0a 20 20 46 54 53 54 52  *pVtab){.  FTSTR
2c180 41 43 45 28 28 22 46 54 53 33 20 78 53 79 6e 63  ACE(("FTS3 xSync
2c181 28 29 5c 6e 22 29 29 3b 0a 20 20 72 65 74 75 72  ()\n"));.  retur
2c182 6e 20 66 6c 75 73 68 50 65 6e 64 69 6e 67 54 65  n flushPendingTe
2c183 72 6d 73 28 28 66 75 6c 6c 74 65 78 74 5f 76 74  rms((fulltext_vt
2c184 61 62 20 2a 29 70 56 74 61 62 29 3b 0a 7d 0a 0a  ab *)pVtab);.}..
2c185 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74  static int fullt
2c186 65 78 74 42 65 67 69 6e 28 73 71 6c 69 74 65 33  extBegin(sqlite3
2c187 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20  _vtab *pVtab){. 
2c188 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a   fulltext_vtab *
2c189 76 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 76 74  v = (fulltext_vt
2c18a 61 62 20 2a 29 20 70 56 74 61 62 3b 0a 20 20 46  ab *) pVtab;.  F
2c18b 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20 78  TSTRACE(("FTS3 x
2c18c 42 65 67 69 6e 28 29 5c 6e 22 29 29 3b 0a 0a 20  Begin()\n"));.. 
2c18d 20 2f 2a 20 41 6e 79 20 62 75 66 66 65 72 65 64   /* Any buffered
2c18e 20 75 70 64 61 74 65 73 20 73 68 6f 75 6c 64 20   updates should 
2c18f 68 61 76 65 20 62 65 65 6e 20 63 6c 65 61 72 65  have been cleare
2c190 64 20 62 79 20 74 68 65 20 70 72 65 76 69 6f 75  d by the previou
2c191 73 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69  s.  ** transacti
2c192 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  on..  */.  asser
2c193 74 28 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61  t( v->nPendingDa
2c194 74 61 3c 30 20 29 3b 0a 20 20 72 65 74 75 72 6e  ta<0 );.  return
2c195 20 63 6c 65 61 72 50 65 6e 64 69 6e 67 54 65 72   clearPendingTer
2c196 6d 73 28 76 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  ms(v);.}..static
2c197 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 43 6f 6d   int fulltextCom
2c198 6d 69 74 28 73 71 6c 69 74 65 33 5f 76 74 61 62  mit(sqlite3_vtab
2c199 20 2a 70 56 74 61 62 29 7b 0a 20 20 66 75 6c 6c   *pVtab){.  full
2c19a 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d 20 28  text_vtab *v = (
2c19b 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29  fulltext_vtab *)
2c19c 20 70 56 74 61 62 3b 0a 20 20 46 54 53 54 52 41   pVtab;.  FTSTRA
2c19d 43 45 28 28 22 46 54 53 33 20 78 43 6f 6d 6d 69  CE(("FTS3 xCommi
2c19e 74 28 29 5c 6e 22 29 29 3b 0a 0a 20 20 2f 2a 20  t()\n"));..  /* 
2c19f 42 75 66 66 65 72 65 64 20 75 70 64 61 74 65 73  Buffered updates
2c1a0 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65   should have bee
2c1a1 6e 20 63 6c 65 61 72 65 64 20 62 79 20 66 75 6c  n cleared by ful
2c1a2 6c 74 65 78 74 53 79 6e 63 28 29 2e 20 2a 2f 0a  ltextSync(). */.
2c1a3 20 20 61 73 73 65 72 74 28 20 76 2d 3e 6e 50 65    assert( v->nPe
2c1a4 6e 64 69 6e 67 44 61 74 61 3c 30 20 29 3b 0a 20  ndingData<0 );. 
2c1a5 20 72 65 74 75 72 6e 20 63 6c 65 61 72 50 65 6e   return clearPen
2c1a6 64 69 6e 67 54 65 72 6d 73 28 76 29 3b 0a 7d 0a  dingTerms(v);.}.
2c1a7 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c  .static int full
2c1a8 74 65 78 74 52 6f 6c 6c 62 61 63 6b 28 73 71 6c  textRollback(sql
2c1a9 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
2c1aa 29 7b 0a 20 20 46 54 53 54 52 41 43 45 28 28 22  ){.  FTSTRACE(("
2c1ab 46 54 53 33 20 78 52 6f 6c 6c 62 61 63 6b 28 29  FTS3 xRollback()
2c1ac 5c 6e 22 29 29 3b 0a 20 20 72 65 74 75 72 6e 20  \n"));.  return 
2c1ad 63 6c 65 61 72 50 65 6e 64 69 6e 67 54 65 72 6d  clearPendingTerm
2c1ae 73 28 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  s((fulltext_vtab
2c1af 20 2a 29 70 56 74 61 62 29 3b 0a 7d 0a 0a 2f 2a   *)pVtab);.}../*
2c1b0 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
2c1b1 6f 6e 20 6f 66 20 74 68 65 20 73 6e 69 70 70 65  on of the snippe
2c1b2 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  t() function for
2c1b3 20 46 54 53 33 0a 2a 2f 0a 73 74 61 74 69 63 20   FTS3.*/.static 
2c1b4 76 6f 69 64 20 73 6e 69 70 70 65 74 46 75 6e 63  void snippetFunc
2c1b5 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
2c1b6 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20  ext *pContext,. 
2c1b7 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
2c1b8 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
2c1b9 76 0a 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f  v.){.  fulltext_
2c1ba 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b  cursor *pCursor;
2c1bb 0a 20 20 69 66 28 20 61 72 67 63 3c 31 20 29 20  .  if( argc<1 ) 
2c1bc 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73 71  return;.  if( sq
2c1bd 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
2c1be 28 61 72 67 76 5b 30 5d 29 21 3d 53 51 4c 49 54  (argv[0])!=SQLIT
2c1bf 45 5f 42 4c 4f 42 20 7c 7c 0a 20 20 20 20 20 20  E_BLOB ||.      
2c1c0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
2c1c1 74 65 73 28 61 72 67 76 5b 30 5d 29 21 3d 73 69  tes(argv[0])!=si
2c1c2 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 20 29 7b  zeof(pCursor) ){
2c1c3 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
2c1c4 75 6c 74 5f 65 72 72 6f 72 28 70 43 6f 6e 74 65  ult_error(pConte
2c1c5 78 74 2c 20 22 69 6c 6c 65 67 61 6c 20 66 69 72  xt, "illegal fir
2c1c6 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 68  st argument to h
2c1c7 74 6d 6c 5f 73 6e 69 70 70 65 74 22 2c 2d 31 29  tml_snippet",-1)
2c1c8 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63  ;.  }else{.    c
2c1c9 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 61 72  onst char *zStar
2c1ca 74 20 3d 20 22 3c 62 3e 22 3b 0a 20 20 20 20 63  t = "<b>";.    c
2c1cb 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e 64 20  onst char *zEnd 
2c1cc 3d 20 22 3c 2f 62 3e 22 3b 0a 20 20 20 20 63 6f  = "</b>";.    co
2c1cd 6e 73 74 20 63 68 61 72 20 2a 7a 45 6c 6c 69 70  nst char *zEllip
2c1ce 73 69 73 20 3d 20 22 3c 62 3e 2e 2e 2e 3c 2f 62  sis = "<b>...</b
2c1cf 3e 22 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26  >";.    memcpy(&
2c1d0 70 43 75 72 73 6f 72 2c 20 73 71 6c 69 74 65 33  pCursor, sqlite3
2c1d1 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76  _value_blob(argv
2c1d2 5b 30 5d 29 2c 20 73 69 7a 65 6f 66 28 70 43 75  [0]), sizeof(pCu
2c1d3 72 73 6f 72 29 29 3b 0a 20 20 20 20 69 66 28 20  rsor));.    if( 
2c1d4 61 72 67 63 3e 3d 32 20 29 7b 0a 20 20 20 20 20  argc>=2 ){.     
2c1d5 20 7a 53 74 61 72 74 20 3d 20 28 63 6f 6e 73 74   zStart = (const
2c1d6 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76   char*)sqlite3_v
2c1d7 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31  alue_text(argv[1
2c1d8 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 61 72  ]);.      if( ar
2c1d9 67 63 3e 3d 33 20 29 7b 0a 20 20 20 20 20 20 20  gc>=3 ){.       
2c1da 20 7a 45 6e 64 20 3d 20 28 63 6f 6e 73 74 20 63   zEnd = (const c
2c1db 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  har*)sqlite3_val
2c1dc 75 65 5f 74 65 78 74 28 61 72 67 76 5b 32 5d 29  ue_text(argv[2])
2c1dd 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 72  ;.        if( ar
2c1de 67 63 3e 3d 34 20 29 7b 0a 20 20 20 20 20 20 20  gc>=4 ){.       
2c1df 20 20 20 7a 45 6c 6c 69 70 73 69 73 20 3d 20 28     zEllipsis = (
2c1e0 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69  const char*)sqli
2c1e1 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
2c1e2 72 67 76 5b 33 5d 29 3b 0a 20 20 20 20 20 20 20  rgv[3]);.       
2c1e3 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
2c1e4 0a 20 20 20 20 73 6e 69 70 70 65 74 41 6c 6c 4f  .    snippetAllO
2c1e5 66 66 73 65 74 73 28 70 43 75 72 73 6f 72 29 3b  ffsets(pCursor);
2c1e6 0a 20 20 20 20 73 6e 69 70 70 65 74 54 65 78 74  .    snippetText
2c1e7 28 70 43 75 72 73 6f 72 2c 20 7a 53 74 61 72 74  (pCursor, zStart
2c1e8 2c 20 7a 45 6e 64 2c 20 7a 45 6c 6c 69 70 73 69  , zEnd, zEllipsi
2c1e9 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  s);.    sqlite3_
2c1ea 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e  result_text(pCon
2c1eb 74 65 78 74 2c 20 70 43 75 72 73 6f 72 2d 3e 73  text, pCursor->s
2c1ec 6e 69 70 70 65 74 2e 7a 53 6e 69 70 70 65 74 2c  nippet.zSnippet,
2c1ed 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2c1ee 20 20 20 20 20 20 20 20 20 70 43 75 72 73 6f 72           pCursor
2c1ef 2d 3e 73 6e 69 70 70 65 74 2e 6e 53 6e 69 70 70  ->snippet.nSnipp
2c1f0 65 74 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  et, SQLITE_STATI
2c1f1 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  C);.  }.}../*.**
2c1f2 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
2c1f3 6f 66 20 74 68 65 20 6f 66 66 73 65 74 73 28 29  of the offsets()
2c1f4 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 46 54   function for FT
2c1f5 53 33 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  S3.*/.static voi
2c1f6 64 20 73 6e 69 70 70 65 74 4f 66 66 73 65 74 73  d snippetOffsets
2c1f7 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
2c1f8 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78  context *pContex
2c1f9 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
2c1fa 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2c1fb 2a 61 72 67 76 0a 29 7b 0a 20 20 66 75 6c 6c 74  *argv.){.  fullt
2c1fc 65 78 74 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  ext_cursor *pCur
2c1fd 73 6f 72 3b 0a 20 20 69 66 28 20 61 72 67 63 3c  sor;.  if( argc<
2c1fe 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  1 ) return;.  if
2c1ff 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
2c200 74 79 70 65 28 61 72 67 76 5b 30 5d 29 21 3d 53  type(argv[0])!=S
2c201 51 4c 49 54 45 5f 42 4c 4f 42 20 7c 7c 0a 20 20  QLITE_BLOB ||.  
2c202 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75      sqlite3_valu
2c203 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29  e_bytes(argv[0])
2c204 21 3d 73 69 7a 65 6f 66 28 70 43 75 72 73 6f 72  !=sizeof(pCursor
2c205 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
2c206 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 70 43  _result_error(pC
2c207 6f 6e 74 65 78 74 2c 20 22 69 6c 6c 65 67 61 6c  ontext, "illegal
2c208 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
2c209 74 6f 20 6f 66 66 73 65 74 73 22 2c 2d 31 29 3b  to offsets",-1);
2c20a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65  .  }else{.    me
2c20b 6d 63 70 79 28 26 70 43 75 72 73 6f 72 2c 20 73  mcpy(&pCursor, s
2c20c 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
2c20d 62 28 61 72 67 76 5b 30 5d 29 2c 20 73 69 7a 65  b(argv[0]), size
2c20e 6f 66 28 70 43 75 72 73 6f 72 29 29 3b 0a 20 20  of(pCursor));.  
2c20f 20 20 73 6e 69 70 70 65 74 41 6c 6c 4f 66 66 73    snippetAllOffs
2c210 65 74 73 28 70 43 75 72 73 6f 72 29 3b 0a 20 20  ets(pCursor);.  
2c211 20 20 73 6e 69 70 70 65 74 4f 66 66 73 65 74 54    snippetOffsetT
2c212 65 78 74 28 26 70 43 75 72 73 6f 72 2d 3e 73 6e  ext(&pCursor->sn
2c213 69 70 70 65 74 29 3b 0a 20 20 20 20 73 71 6c 69  ippet);.    sqli
2c214 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
2c215 70 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20  pContext,.      
2c216 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c217 20 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70    pCursor->snipp
2c218 65 74 2e 7a 4f 66 66 73 65 74 2c 20 70 43 75 72  et.zOffset, pCur
2c219 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 6e 4f 66  sor->snippet.nOf
2c21a 66 73 65 74 2c 0a 20 20 20 20 20 20 20 20 20 20  fset,.          
2c21b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
2c21c 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
2c21d 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
2c21e 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e  routine implemen
2c21f 74 73 20 74 68 65 20 78 46 69 6e 64 46 75 6e 63  ts the xFindFunc
2c220 74 69 6f 6e 20 6d 65 74 68 6f 64 20 66 6f 72 20  tion method for 
2c221 74 68 65 20 46 54 53 33 0a 2a 2a 20 76 69 72 74  the FTS3.** virt
2c222 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74  ual table..*/.st
2c223 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78  atic int fulltex
2c224 74 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 0a 20  tFindFunction(. 
2c225 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
2c226 56 74 61 62 2c 0a 20 20 69 6e 74 20 6e 41 72 67  Vtab,.  int nArg
2c227 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
2c228 7a 4e 61 6d 65 2c 0a 20 20 76 6f 69 64 20 28 2a  zName,.  void (*
2c229 2a 70 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33  *pxFunc)(sqlite3
2c22a 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71  _context*,int,sq
2c22b 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a  lite3_value**),.
2c22c 20 20 76 6f 69 64 20 2a 2a 70 70 41 72 67 0a 29    void **ppArg.)
2c22d 7b 0a 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a  {.  if( strcmp(z
2c22e 4e 61 6d 65 2c 22 73 6e 69 70 70 65 74 22 29 3d  Name,"snippet")=
2c22f 3d 30 20 29 7b 0a 20 20 20 20 2a 70 78 46 75 6e  =0 ){.    *pxFun
2c230 63 20 3d 20 73 6e 69 70 70 65 74 46 75 6e 63 3b  c = snippetFunc;
2c231 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
2c232 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d   }else if( strcm
2c233 70 28 7a 4e 61 6d 65 2c 22 6f 66 66 73 65 74 73  p(zName,"offsets
2c234 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 78  ")==0 ){.    *px
2c235 46 75 6e 63 20 3d 20 73 6e 69 70 70 65 74 4f 66  Func = snippetOf
2c236 66 73 65 74 73 46 75 6e 63 3b 0a 20 20 20 20 72  fsetsFunc;.    r
2c237 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72  eturn 1;.  }.  r
2c238 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
2c239 2a 20 52 65 6e 61 6d 65 20 61 6e 20 66 74 73 33  * Rename an fts3
2c23a 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69   table..*/.stati
2c23b 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 52 65  c int fulltextRe
2c23c 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  name(.  sqlite3_
2c23d 76 74 61 62 20 2a 70 56 74 61 62 2c 0a 20 20 63  vtab *pVtab,.  c
2c23e 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
2c23f 0a 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76  .){.  fulltext_v
2c240 74 61 62 20 2a 70 20 3d 20 28 66 75 6c 6c 74 65  tab *p = (fullte
2c241 78 74 5f 76 74 61 62 20 2a 29 70 56 74 61 62 3b  xt_vtab *)pVtab;
2c242 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
2c243 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 63 68 61 72  TE_NOMEM;.  char
2c244 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33   *zSql = sqlite3
2c245 5f 6d 70 72 69 6e 74 66 28 0a 20 20 20 20 22 41  _mprintf(.    "A
2c246 4c 54 45 52 20 54 41 42 4c 45 20 25 51 2e 27 25  LTER TABLE %Q.'%
2c247 71 5f 63 6f 6e 74 65 6e 74 27 20 20 52 45 4e 41  q_content'  RENA
2c248 4d 45 20 54 4f 20 27 25 71 5f 63 6f 6e 74 65 6e  ME TO '%q_conten
2c249 74 27 3b 22 0a 20 20 20 20 22 41 4c 54 45 52 20  t';".    "ALTER 
2c24a 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 73 65 67  TABLE %Q.'%q_seg
2c24b 6d 65 6e 74 73 27 20 52 45 4e 41 4d 45 20 54 4f  ments' RENAME TO
2c24c 20 27 25 71 5f 73 65 67 6d 65 6e 74 73 27 3b 22   '%q_segments';"
2c24d 0a 20 20 20 20 22 41 4c 54 45 52 20 54 41 42 4c  .    "ALTER TABL
2c24e 45 20 25 51 2e 27 25 71 5f 73 65 67 64 69 72 27  E %Q.'%q_segdir'
2c24f 20 20 20 52 45 4e 41 4d 45 20 54 4f 20 27 25 71     RENAME TO '%q
2c250 5f 73 65 67 64 69 72 27 3b 22 0a 20 20 20 20 2c  _segdir';".    ,
2c251 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d   p->zDb, p->zNam
2c252 65 2c 20 7a 4e 61 6d 65 20 0a 20 20 20 20 2c 20  e, zName .    , 
2c253 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65  p->zDb, p->zName
2c254 2c 20 7a 4e 61 6d 65 20 0a 20 20 20 20 2c 20 70  , zName .    , p
2c255 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c  ->zDb, p->zName,
2c256 20 7a 4e 61 6d 65 0a 20 20 29 3b 0a 20 20 69 66   zName.  );.  if
2c257 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20 72 63  ( zSql ){.    rc
2c258 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28   = sqlite3_exec(
2c259 70 2d 3e 64 62 2c 20 7a 53 71 6c 2c 20 30 2c 20  p->db, zSql, 0, 
2c25a 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
2c25b 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20  e3_free(zSql);. 
2c25c 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
2c25d 7d 0a 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  }..static const 
2c25e 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 66  sqlite3_module f
2c25f 74 73 33 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20 20  ts3Module = {.  
2c260 2f 2a 20 69 56 65 72 73 69 6f 6e 20 20 20 20 20  /* iVersion     
2c261 20 2a 2f 20 30 2c 0a 20 20 2f 2a 20 78 43 72 65   */ 0,.  /* xCre
2c262 61 74 65 20 20 20 20 20 20 20 2a 2f 20 66 75 6c  ate       */ ful
2c263 6c 74 65 78 74 43 72 65 61 74 65 2c 0a 20 20 2f  ltextCreate,.  /
2c264 2a 20 78 43 6f 6e 6e 65 63 74 20 20 20 20 20 20  * xConnect      
2c265 2a 2f 20 66 75 6c 6c 74 65 78 74 43 6f 6e 6e 65  */ fulltextConne
2c266 63 74 2c 0a 20 20 2f 2a 20 78 42 65 73 74 49 6e  ct,.  /* xBestIn
2c267 64 65 78 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65  dex    */ fullte
2c268 78 74 42 65 73 74 49 6e 64 65 78 2c 0a 20 20 2f  xtBestIndex,.  /
2c269 2a 20 78 44 69 73 63 6f 6e 6e 65 63 74 20 20 20  * xDisconnect   
2c26a 2a 2f 20 66 75 6c 6c 74 65 78 74 44 69 73 63 6f  */ fulltextDisco
2c26b 6e 6e 65 63 74 2c 0a 20 20 2f 2a 20 78 44 65 73  nnect,.  /* xDes
2c26c 74 72 6f 79 20 20 20 20 20 20 2a 2f 20 66 75 6c  troy      */ ful
2c26d 6c 74 65 78 74 44 65 73 74 72 6f 79 2c 0a 20 20  ltextDestroy,.  
2c26e 2f 2a 20 78 4f 70 65 6e 20 20 20 20 20 20 20 20  /* xOpen        
2c26f 20 2a 2f 20 66 75 6c 6c 74 65 78 74 4f 70 65 6e   */ fulltextOpen
2c270 2c 0a 20 20 2f 2a 20 78 43 6c 6f 73 65 20 20 20  ,.  /* xClose   
2c271 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74       */ fulltext
2c272 43 6c 6f 73 65 2c 0a 20 20 2f 2a 20 78 46 69 6c  Close,.  /* xFil
2c273 74 65 72 20 20 20 20 20 20 20 2a 2f 20 66 75 6c  ter       */ ful
2c274 6c 74 65 78 74 46 69 6c 74 65 72 2c 0a 20 20 2f  ltextFilter,.  /
2c275 2a 20 78 4e 65 78 74 20 20 20 20 20 20 20 20 20  * xNext         
2c276 2a 2f 20 66 75 6c 6c 74 65 78 74 4e 65 78 74 2c  */ fulltextNext,
2c277 0a 20 20 2f 2a 20 78 45 6f 66 20 20 20 20 20 20  .  /* xEof      
2c278 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 45      */ fulltextE
2c279 6f 66 2c 0a 20 20 2f 2a 20 78 43 6f 6c 75 6d 6e  of,.  /* xColumn
2c27a 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65         */ fullte
2c27b 78 74 43 6f 6c 75 6d 6e 2c 0a 20 20 2f 2a 20 78  xtColumn,.  /* x
2c27c 52 6f 77 69 64 20 20 20 20 20 20 20 20 2a 2f 20  Rowid        */ 
2c27d 66 75 6c 6c 74 65 78 74 52 6f 77 69 64 2c 0a 20  fulltextRowid,. 
2c27e 20 2f 2a 20 78 55 70 64 61 74 65 20 20 20 20 20   /* xUpdate     
2c27f 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 55 70 64    */ fulltextUpd
2c280 61 74 65 2c 0a 20 20 2f 2a 20 78 42 65 67 69 6e  ate,.  /* xBegin
2c281 20 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74          */ fullt
2c282 65 78 74 42 65 67 69 6e 2c 0a 20 20 2f 2a 20 78  extBegin,.  /* x
2c283 53 79 6e 63 20 20 20 20 20 20 20 20 20 2a 2f 20  Sync         */ 
2c284 66 75 6c 6c 74 65 78 74 53 79 6e 63 2c 0a 20 20  fulltextSync,.  
2c285 2f 2a 20 78 43 6f 6d 6d 69 74 20 20 20 20 20 20  /* xCommit      
2c286 20 2a 2f 20 66 75 6c 6c 74 65 78 74 43 6f 6d 6d   */ fulltextComm
2c287 69 74 2c 0a 20 20 2f 2a 20 78 52 6f 6c 6c 62 61  it,.  /* xRollba
2c288 63 6b 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65  ck     */ fullte
2c289 78 74 52 6f 6c 6c 62 61 63 6b 2c 0a 20 20 2f 2a  xtRollback,.  /*
2c28a 20 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e 20 2a   xFindFunction *
2c28b 2f 20 66 75 6c 6c 74 65 78 74 46 69 6e 64 46 75  / fulltextFindFu
2c28c 6e 63 74 69 6f 6e 2c 0a 20 20 2f 2a 20 78 52 65  nction,.  /* xRe
2c28d 6e 61 6d 65 20 2a 2f 20 20 20 20 20 20 20 66 75  name */       fu
2c28e 6c 6c 74 65 78 74 52 65 6e 61 6d 65 2c 0a 7d 3b  lltextRename,.};
2c28f 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 61  ..static void ha
2c290 73 68 44 65 73 74 72 6f 79 28 76 6f 69 64 20 2a  shDestroy(void *
2c291 70 29 7b 0a 20 20 66 74 73 33 48 61 73 68 20 2a  p){.  fts3Hash *
2c292 70 48 61 73 68 20 3d 20 28 66 74 73 33 48 61 73  pHash = (fts3Has
2c293 68 20 2a 29 70 3b 0a 20 20 73 71 6c 69 74 65 33  h *)p;.  sqlite3
2c294 46 74 73 33 48 61 73 68 43 6c 65 61 72 28 70 48  Fts3HashClear(pH
2c295 61 73 68 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ash);.  sqlite3_
2c296 66 72 65 65 28 70 48 61 73 68 29 3b 0a 7d 0a 0a  free(pHash);.}..
2c297 2f 2a 0a 2a 2a 20 54 68 65 20 66 74 73 33 20 62  /*.** The fts3 b
2c298 75 69 6c 74 2d 69 6e 20 74 6f 6b 65 6e 69 7a 65  uilt-in tokenize
2c299 72 73 20 2d 20 22 73 69 6d 70 6c 65 22 20 61 6e  rs - "simple" an
2c29a 64 20 22 70 6f 72 74 65 72 22 20 2d 20 61 72 65  d "porter" - are
2c29b 20 69 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20   implemented.** 
2c29c 69 6e 20 66 69 6c 65 73 20 66 74 73 33 5f 74 6f  in files fts3_to
2c29d 6b 65 6e 69 7a 65 72 31 2e 63 20 61 6e 64 20 66  kenizer1.c and f
2c29e 74 73 33 5f 70 6f 72 74 65 72 2e 63 20 72 65 73  ts3_porter.c res
2c29f 70 65 63 74 69 76 65 6c 79 2e 20 54 68 65 20 66  pectively. The f
2c2a0 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 74 77 6f 20  ollowing.** two 
2c2a1 66 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74  forward declarat
2c2a2 69 6f 6e 73 20 61 72 65 20 66 6f 72 20 66 75 6e  ions are for fun
2c2a3 63 74 69 6f 6e 73 20 64 65 63 6c 61 72 65 64 20  ctions declared 
2c2a4 69 6e 20 74 68 65 73 65 20 66 69 6c 65 73 0a 2a  in these files.*
2c2a5 2a 20 75 73 65 64 20 74 6f 20 72 65 74 72 69 65  * used to retrie
2c2a6 76 65 20 74 68 65 20 72 65 73 70 65 63 74 69 76  ve the respectiv
2c2a7 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
2c2a8 73 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67  s..**.** Calling
2c2a9 20 73 71 6c 69 74 65 33 46 74 73 33 53 69 6d 70   sqlite3Fts3Simp
2c2aa 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  leTokenizerModul
2c2ab 65 28 29 20 73 65 74 73 20 74 68 65 20 76 61 6c  e() sets the val
2c2ac 75 65 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f  ue pointed.** to
2c2ad 20 62 79 20 74 68 65 20 61 72 67 75 6d 65 6e 74   by the argument
2c2ae 20 74 6f 20 70 6f 69 6e 74 20 61 20 74 68 65 20   to point a the 
2c2af 22 73 69 6d 70 6c 65 22 20 74 6f 6b 65 6e 69 7a  "simple" tokeniz
2c2b0 65 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  er implementatio
2c2b1 6e 2e 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 2e  n..** Function .
2c2b2 2e 2e 50 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65  ..PorterTokenize
2c2b3 72 4d 6f 64 75 6c 65 28 29 20 73 65 74 73 20 2a  rModule() sets *
2c2b4 70 4d 6f 64 75 6c 65 20 74 6f 20 70 6f 69 6e 74  pModule to point
2c2b5 20 74 6f 20 74 68 65 0a 2a 2a 20 70 6f 72 74 65   to the.** porte
2c2b6 72 20 74 6f 6b 65 6e 69 7a 65 72 2f 73 74 65 6d  r tokenizer/stem
2c2b7 6d 65 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  mer implementati
2c2b8 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
2c2b9 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2c2ba 65 33 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65  e3Fts3SimpleToke
2c2bb 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 73 71 6c 69  nizerModule(sqli
2c2bc 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
2c2bd 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f  dule const**ppMo
2c2be 64 75 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  dule);.SQLITE_PR
2c2bf 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2c2c0 65 33 46 74 73 33 50 6f 72 74 65 72 54 6f 6b 65  e3Fts3PorterToke
2c2c1 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 73 71 6c 69  nizerModule(sqli
2c2c2 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
2c2c3 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f  dule const**ppMo
2c2c4 64 75 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  dule);.SQLITE_PR
2c2c5 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2c2c6 65 33 46 74 73 33 49 63 75 54 6f 6b 65 6e 69 7a  e3Fts3IcuTokeniz
2c2c7 65 72 4d 6f 64 75 6c 65 28 73 71 6c 69 74 65 33  erModule(sqlite3
2c2c8 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
2c2c9 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c  e const**ppModul
2c2ca 65 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  e);..SQLITE_PRIV
2c2cb 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
2c2cc 74 73 33 49 6e 69 74 48 61 73 68 54 61 62 6c 65  ts3InitHashTable
2c2cd 28 73 71 6c 69 74 65 33 20 2a 2c 20 66 74 73 33  (sqlite3 *, fts3
2c2ce 48 61 73 68 20 2a 2c 20 63 6f 6e 73 74 20 63 68  Hash *, const ch
2c2cf 61 72 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6e  ar *);../*.** In
2c2d0 69 74 69 61 6c 69 73 65 20 74 68 65 20 66 74 73  itialise the fts
2c2d1 33 20 65 78 74 65 6e 73 69 6f 6e 2e 20 49 66 20  3 extension. If 
2c2d2 74 68 69 73 20 65 78 74 65 6e 73 69 6f 6e 20 69  this extension i
2c2d3 73 20 62 75 69 6c 74 20 61 73 20 70 61 72 74 0a  s built as part.
2c2d4 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  ** of the sqlite
2c2d5 20 6c 69 62 72 61 72 79 2c 20 74 68 65 6e 20 74   library, then t
2c2d6 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
2c2d7 63 61 6c 6c 65 64 20 64 69 72 65 63 74 6c 79 20  called directly 
2c2d8 62 79 0a 2a 2a 20 53 51 4c 69 74 65 2e 20 49 66  by.** SQLite. If
2c2d9 20 66 74 73 33 20 69 73 20 62 75 69 6c 74 20 61   fts3 is built a
2c2da 73 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20  s a dynamically 
2c2db 6c 6f 61 64 61 62 6c 65 20 65 78 74 65 6e 73 69  loadable extensi
2c2dc 6f 6e 2c 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63  on, this.** func
2c2dd 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62  tion is called b
2c2de 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 65 78  y the sqlite3_ex
2c2df 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 28 29 20 65  tension_init() e
2c2e0 6e 74 72 79 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 53  ntry point..*/.S
2c2e1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2c2e2 74 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69  t sqlite3Fts3Ini
2c2e3 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  t(sqlite3 *db){.
2c2e4 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
2c2e5 45 5f 4f 4b 3b 0a 20 20 66 74 73 33 48 61 73 68  E_OK;.  fts3Hash
2c2e6 20 2a 70 48 61 73 68 20 3d 20 30 3b 0a 20 20 63   *pHash = 0;.  c
2c2e7 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b  onst sqlite3_tok
2c2e8 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70  enizer_module *p
2c2e9 53 69 6d 70 6c 65 20 3d 20 30 3b 0a 20 20 63 6f  Simple = 0;.  co
2c2ea 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  nst sqlite3_toke
2c2eb 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 50  nizer_module *pP
2c2ec 6f 72 74 65 72 20 3d 20 30 3b 0a 20 20 63 6f 6e  orter = 0;.  con
2c2ed 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  st sqlite3_token
2c2ee 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 49 63  izer_module *pIc
2c2ef 75 20 3d 20 30 3b 0a 0a 20 20 73 71 6c 69 74 65  u = 0;..  sqlite
2c2f0 33 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e  3Fts3SimpleToken
2c2f1 69 7a 65 72 4d 6f 64 75 6c 65 28 26 70 53 69 6d  izerModule(&pSim
2c2f2 70 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 46  ple);.  sqlite3F
2c2f3 74 73 33 50 6f 72 74 65 72 54 6f 6b 65 6e 69 7a  ts3PorterTokeniz
2c2f4 65 72 4d 6f 64 75 6c 65 28 26 70 50 6f 72 74 65  erModule(&pPorte
2c2f5 72 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  r);.#ifdef SQLIT
2c2f6 45 5f 45 4e 41 42 4c 45 5f 49 43 55 0a 20 20 73  E_ENABLE_ICU.  s
2c2f7 71 6c 69 74 65 33 46 74 73 33 49 63 75 54 6f 6b  qlite3Fts3IcuTok
2c2f8 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 26 70 49  enizerModule(&pI
2c2f9 63 75 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  cu);.#endif..  /
2c2fa 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 69  * Allocate and i
2c2fb 6e 69 74 69 61 6c 69 73 65 20 74 68 65 20 68 61  nitialise the ha
2c2fc 73 68 2d 74 61 62 6c 65 20 75 73 65 64 20 74 6f  sh-table used to
2c2fd 20 73 74 6f 72 65 20 74 6f 6b 65 6e 69 7a 65 72   store tokenizer
2c2fe 73 2e 20 2a 2f 0a 20 20 70 48 61 73 68 20 3d 20  s. */.  pHash = 
2c2ff 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
2c300 69 7a 65 6f 66 28 66 74 73 33 48 61 73 68 29 29  izeof(fts3Hash))
2c301 3b 0a 20 20 69 66 28 20 21 70 48 61 73 68 20 29  ;.  if( !pHash )
2c302 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
2c303 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65  E_NOMEM;.  }else
2c304 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73  {.    sqlite3Fts
2c305 33 48 61 73 68 49 6e 69 74 28 70 48 61 73 68 2c  3HashInit(pHash,
2c306 20 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e   FTS3_HASH_STRIN
2c307 47 2c 20 31 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  G, 1);.  }..  /*
2c308 20 4c 6f 61 64 20 74 68 65 20 62 75 69 6c 74 2d   Load the built-
2c309 69 6e 20 74 6f 6b 65 6e 69 7a 65 72 73 20 69 6e  in tokenizers in
2c30a 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  to the hash tabl
2c30b 65 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53  e */.  if( rc==S
2c30c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
2c30d 69 66 28 20 73 71 6c 69 74 65 33 46 74 73 33 48  if( sqlite3Fts3H
2c30e 61 73 68 49 6e 73 65 72 74 28 70 48 61 73 68 2c  ashInsert(pHash,
2c30f 20 22 73 69 6d 70 6c 65 22 2c 20 37 2c 20 28 76   "simple", 7, (v
2c310 6f 69 64 20 2a 29 70 53 69 6d 70 6c 65 29 0a 20  oid *)pSimple). 
2c311 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 46 74      || sqlite3Ft
2c312 73 33 48 61 73 68 49 6e 73 65 72 74 28 70 48 61  s3HashInsert(pHa
2c313 73 68 2c 20 22 70 6f 72 74 65 72 22 2c 20 37 2c  sh, "porter", 7,
2c314 20 28 76 6f 69 64 20 2a 29 70 50 6f 72 74 65 72   (void *)pPorter
2c315 29 20 0a 20 20 20 20 20 7c 7c 20 28 70 49 63 75  ) .     || (pIcu
2c316 20 26 26 20 73 71 6c 69 74 65 33 46 74 73 33 48   && sqlite3Fts3H
2c317 61 73 68 49 6e 73 65 72 74 28 70 48 61 73 68 2c  ashInsert(pHash,
2c318 20 22 69 63 75 22 2c 20 34 2c 20 28 76 6f 69 64   "icu", 4, (void
2c319 20 2a 29 70 49 63 75 29 29 0a 20 20 20 20 29 7b   *)pIcu)).    ){
2c31a 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
2c31b 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
2c31c 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65    }..  /* Create
2c31d 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
2c31e 6c 65 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e  le wrapper aroun
2c31f 64 20 74 68 65 20 68 61 73 68 2d 74 61 62 6c 65  d the hash-table
2c320 20 61 6e 64 20 6f 76 65 72 6c 6f 61 64 20 0a 20   and overload . 
2c321 20 2a 2a 20 74 68 65 20 74 77 6f 20 73 63 61 6c   ** the two scal
2c322 61 72 20 66 75 6e 63 74 69 6f 6e 73 2e 20 49 66  ar functions. If
2c323 20 74 68 69 73 20 69 73 20 73 75 63 63 65 73 73   this is success
2c324 66 75 6c 2c 20 72 65 67 69 73 74 65 72 20 74 68  ful, register th
2c325 65 0a 20 20 2a 2a 20 6d 6f 64 75 6c 65 20 77 69  e.  ** module wi
2c326 74 68 20 73 71 6c 69 74 65 2e 0a 20 20 2a 2f 0a  th sqlite..  */.
2c327 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d    if( SQLITE_OK=
2c328 3d 72 63 20 0a 20 20 20 26 26 20 53 51 4c 49 54  =rc .   && SQLIT
2c329 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK==(rc = sqli
2c32a 74 65 33 46 74 73 33 49 6e 69 74 48 61 73 68 54  te3Fts3InitHashT
2c32b 61 62 6c 65 28 64 62 2c 20 70 48 61 73 68 2c 20  able(db, pHash, 
2c32c 22 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 22  "fts3_tokenizer"
2c32d 29 29 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f  )).   && SQLITE_
2c32e 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65  OK==(rc = sqlite
2c32f 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74  3_overload_funct
2c330 69 6f 6e 28 64 62 2c 20 22 73 6e 69 70 70 65 74  ion(db, "snippet
2c331 22 2c 20 2d 31 29 29 0a 20 20 20 26 26 20 53 51  ", -1)).   && SQ
2c332 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73  LITE_OK==(rc = s
2c333 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f  qlite3_overload_
2c334 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 6f 66  function(db, "of
2c335 66 73 65 74 73 22 2c 20 2d 31 29 29 0a 20 20 29  fsets", -1)).  )
2c336 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c  {.    return sql
2c337 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
2c338 6c 65 5f 76 32 28 0a 20 20 20 20 20 20 20 20 64  le_v2(.        d
2c339 62 2c 20 22 66 74 73 33 22 2c 20 26 66 74 73 33  b, "fts3", &fts3
2c33a 4d 6f 64 75 6c 65 2c 20 28 76 6f 69 64 20 2a 29  Module, (void *)
2c33b 70 48 61 73 68 2c 20 68 61 73 68 44 65 73 74 72  pHash, hashDestr
2c33c 6f 79 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20  oy.    );.  }.. 
2c33d 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 68 61 73   /* An error has
2c33e 20 6f 63 63 75 72 65 64 2e 20 44 65 6c 65 74 65   occured. Delete
2c33f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
2c340 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 65  and return the e
2c341 72 72 6f 72 20 63 6f 64 65 2e 20 2a 2f 0a 20 20  rror code. */.  
2c342 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
2c343 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 20 70  TE_OK );.  if( p
2c344 48 61 73 68 20 29 7b 0a 20 20 20 20 73 71 6c 69  Hash ){.    sqli
2c345 74 65 33 46 74 73 33 48 61 73 68 43 6c 65 61 72  te3Fts3HashClear
2c346 28 70 48 61 73 68 29 3b 0a 20 20 20 20 73 71 6c  (pHash);.    sql
2c347 69 74 65 33 5f 66 72 65 65 28 70 48 61 73 68 29  ite3_free(pHash)
2c348 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
2c349 63 3b 0a 7d 0a 0a 23 69 66 20 21 53 51 4c 49 54  c;.}..#if !SQLIT
2c34a 45 5f 43 4f 52 45 0a 53 51 4c 49 54 45 5f 41 50  E_CORE.SQLITE_AP
2c34b 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78  I int sqlite3_ex
2c34c 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 28 0a 20 20  tension_init(.  
2c34d 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20  sqlite3 *db, .  
2c34e 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c  char **pzErrMsg,
2c34f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
2c350 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 2a 70  _api_routines *p
2c351 41 70 69 0a 29 7b 0a 20 20 53 51 4c 49 54 45 5f  Api.){.  SQLITE_
2c352 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 32 28  EXTENSION_INIT2(
2c353 70 41 70 69 29 0a 20 20 72 65 74 75 72 6e 20 73  pApi).  return s
2c354 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 28 64  qlite3Fts3Init(d
2c355 62 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 65  b);.}.#endif..#e
2c356 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
2c357 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c  (SQLITE_CORE) ||
2c358 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
2c359 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a  ENABLE_FTS3) */.
2c35a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2c35b 20 45 6e 64 20 6f 66 20 66 74 73 33 2e 63 20 2a   End of fts3.c *
2c35c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c35d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c35e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2c35f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2c360 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33   Begin file fts3
2c361 5f 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  _hash.c ********
2c362 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c363 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2c364 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
2c365 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54  ember 22.**.** T
2c366 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
2c367 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
2c368 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
2c369 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
2c36a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
2c36b 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
2c36c 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
2c36d 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
2c36e 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
2c36f 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
2c370 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
2c371 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
2c372 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
2c373 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
2c374 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
2c375 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
2c376 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
2c377 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c378 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c379 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c37a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c37b 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
2c37c 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  is the implement
2c37d 61 74 69 6f 6e 20 6f 66 20 67 65 6e 65 72 69 63  ation of generic
2c37e 20 68 61 73 68 2d 74 61 62 6c 65 73 20 75 73 65   hash-tables use
2c37f 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 20  d in SQLite..** 
2c380 57 65 27 76 65 20 6d 6f 64 69 66 69 65 64 20 69  We've modified i
2c381 74 20 73 6c 69 67 68 74 6c 79 20 74 6f 20 73 65  t slightly to se
2c382 72 76 65 20 61 73 20 61 20 73 74 61 6e 64 61 6c  rve as a standal
2c383 6f 6e 65 20 68 61 73 68 20 74 61 62 6c 65 0a 2a  one hash table.*
2c384 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
2c385 20 66 6f 72 20 74 68 65 20 66 75 6c 6c 2d 74 65   for the full-te
2c386 78 74 20 69 6e 64 65 78 69 6e 67 20 6d 6f 64 75  xt indexing modu
2c387 6c 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  le..*/../*.** Th
2c388 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66  e code in this f
2c389 69 6c 65 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70  ile is only comp
2c38a 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20  iled if:.**.**  
2c38b 20 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f     * The FTS3 mo
2c38c 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75  dule is being bu
2c38d 69 6c 74 20 61 73 20 61 6e 20 65 78 74 65 6e 73  ilt as an extens
2c38e 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 28 69 6e  ion.**       (in
2c38f 20 77 68 69 63 68 20 63 61 73 65 20 53 51 4c 49   which case SQLI
2c390 54 45 5f 43 4f 52 45 20 69 73 20 6e 6f 74 20 64  TE_CORE is not d
2c391 65 66 69 6e 65 64 29 2c 20 6f 72 0a 2a 2a 0a 2a  efined), or.**.*
2c392 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53 33  *     * The FTS3
2c393 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67   module is being
2c394 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20   built into the 
2c395 63 6f 72 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20  core of.**      
2c396 20 53 51 4c 69 74 65 20 28 69 6e 20 77 68 69 63   SQLite (in whic
2c397 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f 45 4e  h case SQLITE_EN
2c398 41 42 4c 45 5f 46 54 53 33 20 69 73 20 64 65 66  ABLE_FTS3 is def
2c399 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69 66 20 21 64  ined)..*/.#if !d
2c39a 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
2c39b 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
2c39c 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
2c39d 33 29 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c  3)..../*.** Mall
2c39e 6f 63 20 61 6e 64 20 46 72 65 65 20 66 75 6e 63  oc and Free func
2c39f 74 69 6f 6e 73 0a 2a 2f 0a 73 74 61 74 69 63 20  tions.*/.static 
2c3a0 76 6f 69 64 20 2a 66 74 73 33 48 61 73 68 4d 61  void *fts3HashMa
2c3a1 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76  lloc(int n){.  v
2c3a2 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 33  oid *p = sqlite3
2c3a3 5f 6d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69 66  _malloc(n);.  if
2c3a4 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65  ( p ){.    memse
2c3a5 74 28 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a  t(p, 0, n);.  }.
2c3a6 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 73 74    return p;.}.st
2c3a7 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 48 61  atic void fts3Ha
2c3a8 73 68 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b  shFree(void *p){
2c3a9 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
2c3aa 70 29 3b 0a 7d 0a 0a 2f 2a 20 54 75 72 6e 20 62  p);.}../* Turn b
2c3ab 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f 20  ulk memory into 
2c3ac 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f 62 6a  a hash table obj
2c3ad 65 63 74 20 62 79 20 69 6e 69 74 69 61 6c 69 7a  ect by initializ
2c3ae 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 65 6c 64  ing the.** field
2c3af 73 20 6f 66 20 74 68 65 20 48 61 73 68 20 73 74  s of the Hash st
2c3b0 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 22  ructure..**.** "
2c3b1 70 4e 65 77 22 20 69 73 20 61 20 70 6f 69 6e 74  pNew" is a point
2c3b2 65 72 20 74 6f 20 74 68 65 20 68 61 73 68 20 74  er to the hash t
2c3b3 61 62 6c 65 20 74 68 61 74 20 69 73 20 74 6f 20  able that is to 
2c3b4 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a  be initialized..
2c3b5 2a 2a 20 6b 65 79 43 6c 61 73 73 20 69 73 20 6f  ** keyClass is o
2c3b6 6e 65 20 6f 66 20 74 68 65 20 63 6f 6e 73 74 61  ne of the consta
2c3b7 6e 74 73 20 0a 2a 2a 20 46 54 53 33 5f 48 41 53  nts .** FTS3_HAS
2c3b8 48 5f 42 49 4e 41 52 59 20 6f 72 20 46 54 53 33  H_BINARY or FTS3
2c3b9 5f 48 41 53 48 5f 53 54 52 49 4e 47 2e 20 20 54  _HASH_STRING.  T
2c3ba 68 65 20 76 61 6c 75 65 20 6f 66 20 6b 65 79 43  he value of keyC
2c3bb 6c 61 73 73 20 0a 2a 2a 20 64 65 74 65 72 6d 69  lass .** determi
2c3bc 6e 65 73 20 77 68 61 74 20 6b 69 6e 64 20 6f 66  nes what kind of
2c3bd 20 6b 65 79 20 74 68 65 20 68 61 73 68 20 74 61   key the hash ta
2c3be 62 6c 65 20 77 69 6c 6c 20 75 73 65 2e 20 20 22  ble will use.  "
2c3bf 63 6f 70 79 4b 65 79 22 20 69 73 0a 2a 2a 20 74  copyKey" is.** t
2c3c0 72 75 65 20 69 66 20 74 68 65 20 68 61 73 68 20  rue if the hash 
2c3c1 74 61 62 6c 65 20 73 68 6f 75 6c 64 20 6d 61 6b  table should mak
2c3c2 65 20 69 74 73 20 6f 77 6e 20 70 72 69 76 61 74  e its own privat
2c3c3 65 20 63 6f 70 79 20 6f 66 20 6b 65 79 73 20 61  e copy of keys a
2c3c4 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 69  nd.** false if i
2c3c5 74 20 73 68 6f 75 6c 64 20 6a 75 73 74 20 75 73  t should just us
2c3c6 65 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 70  e the supplied p
2c3c7 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  ointer..*/.SQLIT
2c3c8 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2c3c9 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e  qlite3Fts3HashIn
2c3ca 69 74 28 66 74 73 33 48 61 73 68 20 2a 70 4e 65  it(fts3Hash *pNe
2c3cb 77 2c 20 69 6e 74 20 6b 65 79 43 6c 61 73 73 2c  w, int keyClass,
2c3cc 20 69 6e 74 20 63 6f 70 79 4b 65 79 29 7b 0a 20   int copyKey){. 
2c3cd 20 61 73 73 65 72 74 28 20 70 4e 65 77 21 3d 30   assert( pNew!=0
2c3ce 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6b 65   );.  assert( ke
2c3cf 79 43 6c 61 73 73 3e 3d 46 54 53 33 5f 48 41 53  yClass>=FTS3_HAS
2c3d0 48 5f 53 54 52 49 4e 47 20 26 26 20 6b 65 79 43  H_STRING && keyC
2c3d1 6c 61 73 73 3c 3d 46 54 53 33 5f 48 41 53 48 5f  lass<=FTS3_HASH_
2c3d2 42 49 4e 41 52 59 20 29 3b 0a 20 20 70 4e 65 77  BINARY );.  pNew
2c3d3 2d 3e 6b 65 79 43 6c 61 73 73 20 3d 20 6b 65 79  ->keyClass = key
2c3d4 43 6c 61 73 73 3b 0a 20 20 70 4e 65 77 2d 3e 63  Class;.  pNew->c
2c3d5 6f 70 79 4b 65 79 20 3d 20 63 6f 70 79 4b 65 79  opyKey = copyKey
2c3d6 3b 0a 20 20 70 4e 65 77 2d 3e 66 69 72 73 74 20  ;.  pNew->first 
2c3d7 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f 75  = 0;.  pNew->cou
2c3d8 6e 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e  nt = 0;.  pNew->
2c3d9 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 70 4e  htsize = 0;.  pN
2c3da 65 77 2d 3e 68 74 20 3d 20 30 3b 0a 7d 0a 0a 2f  ew->ht = 0;.}../
2c3db 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 65 6e 74  * Remove all ent
2c3dc 72 69 65 73 20 66 72 6f 6d 20 61 20 68 61 73 68  ries from a hash
2c3dd 20 74 61 62 6c 65 2e 20 20 52 65 63 6c 61 69 6d   table.  Reclaim
2c3de 20 61 6c 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20   all memory..** 
2c3df 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e  Call this routin
2c3e0 65 20 74 6f 20 64 65 6c 65 74 65 20 61 20 68 61  e to delete a ha
2c3e1 73 68 20 74 61 62 6c 65 20 6f 72 20 74 6f 20 72  sh table or to r
2c3e2 65 73 65 74 20 61 20 68 61 73 68 20 74 61 62 6c  eset a hash tabl
2c3e3 65 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6d 70 74  e.** to the empt
2c3e4 79 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49  y state..*/.SQLI
2c3e5 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2c3e6 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 43  sqlite3Fts3HashC
2c3e7 6c 65 61 72 28 66 74 73 33 48 61 73 68 20 2a 70  lear(fts3Hash *p
2c3e8 48 29 7b 0a 20 20 66 74 73 33 48 61 73 68 45 6c  H){.  fts3HashEl
2c3e9 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20  em *elem;       
2c3ea 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67    /* For looping
2c3eb 20 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d 65 6e   over all elemen
2c3ec 74 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  ts of the table 
2c3ed 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 48  */..  assert( pH
2c3ee 21 3d 30 20 29 3b 0a 20 20 65 6c 65 6d 20 3d 20  !=0 );.  elem = 
2c3ef 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 70 48 2d  pH->first;.  pH-
2c3f0 3e 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 66 74  >first = 0;.  ft
2c3f1 73 33 48 61 73 68 46 72 65 65 28 70 48 2d 3e 68  s3HashFree(pH->h
2c3f2 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20 30  t);.  pH->ht = 0
2c3f3 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a 65 20 3d  ;.  pH->htsize =
2c3f4 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 65 6c 65   0;.  while( ele
2c3f5 6d 20 29 7b 0a 20 20 20 20 66 74 73 33 48 61 73  m ){.    fts3Has
2c3f6 68 45 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c 65 6d  hElem *next_elem
2c3f7 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20   = elem->next;. 
2c3f8 20 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 79 4b     if( pH->copyK
2c3f9 65 79 20 26 26 20 65 6c 65 6d 2d 3e 70 4b 65 79  ey && elem->pKey
2c3fa 20 29 7b 0a 20 20 20 20 20 20 66 74 73 33 48 61   ){.      fts3Ha
2c3fb 73 68 46 72 65 65 28 65 6c 65 6d 2d 3e 70 4b 65  shFree(elem->pKe
2c3fc 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 74  y);.    }.    ft
2c3fd 73 33 48 61 73 68 46 72 65 65 28 65 6c 65 6d 29  s3HashFree(elem)
2c3fe 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 6e 65 78  ;.    elem = nex
2c3ff 74 5f 65 6c 65 6d 3b 0a 20 20 7d 0a 20 20 70 48  t_elem;.  }.  pH
2c400 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 7d 0a 0a  ->count = 0;.}..
2c401 2f 2a 0a 2a 2a 20 48 61 73 68 20 61 6e 64 20 63  /*.** Hash and c
2c402 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69  omparison functi
2c403 6f 6e 73 20 77 68 65 6e 20 74 68 65 20 6d 6f 64  ons when the mod
2c404 65 20 69 73 20 46 54 53 33 5f 48 41 53 48 5f 53  e is FTS3_HASH_S
2c405 54 52 49 4e 47 0a 2a 2f 0a 73 74 61 74 69 63 20  TRING.*/.static 
2c406 69 6e 74 20 66 74 73 33 53 74 72 48 61 73 68 28  int fts3StrHash(
2c407 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
2c408 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 63  , int nKey){.  c
2c409 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 28  onst char *z = (
2c40a 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 4b 65  const char *)pKe
2c40b 79 3b 0a 20 20 69 6e 74 20 68 20 3d 20 30 3b 0a  y;.  int h = 0;.
2c40c 20 20 69 66 28 20 6e 4b 65 79 3c 3d 30 20 29 20    if( nKey<=0 ) 
2c40d 6e 4b 65 79 20 3d 20 28 69 6e 74 29 20 73 74 72  nKey = (int) str
2c40e 6c 65 6e 28 7a 29 3b 0a 20 20 77 68 69 6c 65 28  len(z);.  while(
2c40f 20 6e 4b 65 79 20 3e 20 30 20 20 29 7b 0a 20 20   nKey > 0  ){.  
2c410 20 20 68 20 3d 20 28 68 3c 3c 33 29 20 5e 20 68    h = (h<<3) ^ h
2c411 20 5e 20 2a 7a 2b 2b 3b 0a 20 20 20 20 6e 4b 65   ^ *z++;.    nKe
2c412 79 2d 2d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  y--;.  }.  retur
2c413 6e 20 68 20 26 20 30 78 37 66 66 66 66 66 66 66  n h & 0x7fffffff
2c414 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 66  ;.}.static int f
2c415 74 73 33 53 74 72 43 6f 6d 70 61 72 65 28 63 6f  ts3StrCompare(co
2c416 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c  nst void *pKey1,
2c417 20 69 6e 74 20 6e 31 2c 20 63 6f 6e 73 74 20 76   int n1, const v
2c418 6f 69 64 20 2a 70 4b 65 79 32 2c 20 69 6e 74 20  oid *pKey2, int 
2c419 6e 32 29 7b 0a 20 20 69 66 28 20 6e 31 21 3d 6e  n2){.  if( n1!=n
2c41a 32 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  2 ) return 1;.  
2c41b 72 65 74 75 72 6e 20 73 74 72 6e 63 6d 70 28 28  return strncmp((
2c41c 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 4b 65 79  const char*)pKey
2c41d 31 2c 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70  1,(const char*)p
2c41e 4b 65 79 32 2c 6e 31 29 3b 0a 7d 0a 0a 2f 2a 0a  Key2,n1);.}../*.
2c41f 2a 2a 20 48 61 73 68 20 61 6e 64 20 63 6f 6d 70  ** Hash and comp
2c420 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 73  arison functions
2c421 20 77 68 65 6e 20 74 68 65 20 6d 6f 64 65 20 69   when the mode i
2c422 73 20 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41  s FTS3_HASH_BINA
2c423 52 59 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  RY.*/.static int
2c424 20 66 74 73 33 42 69 6e 48 61 73 68 28 63 6f 6e   fts3BinHash(con
2c425 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69  st void *pKey, i
2c426 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 20  nt nKey){.  int 
2c427 68 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 63  h = 0;.  const c
2c428 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 20  har *z = (const 
2c429 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a 20 20 77  char *)pKey;.  w
2c42a 68 69 6c 65 28 20 6e 4b 65 79 2d 2d 20 3e 20 30  hile( nKey-- > 0
2c42b 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c   ){.    h = (h<<
2c42c 33 29 20 5e 20 68 20 5e 20 2a 28 7a 2b 2b 29 3b  3) ^ h ^ *(z++);
2c42d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 20  .  }.  return h 
2c42e 26 20 30 78 37 66 66 66 66 66 66 66 3b 0a 7d 0a  & 0x7fffffff;.}.
2c42f 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 42  static int fts3B
2c430 69 6e 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20  inCompare(const 
2c431 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 69 6e 74  void *pKey1, int
2c432 20 6e 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20   n1, const void 
2c433 2a 70 4b 65 79 32 2c 20 69 6e 74 20 6e 32 29 7b  *pKey2, int n2){
2c434 0a 20 20 69 66 28 20 6e 31 21 3d 6e 32 20 29 20  .  if( n1!=n2 ) 
2c435 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65 74 75  return 1;.  retu
2c436 72 6e 20 6d 65 6d 63 6d 70 28 70 4b 65 79 31 2c  rn memcmp(pKey1,
2c437 70 4b 65 79 32 2c 6e 31 29 3b 0a 7d 0a 0a 2f 2a  pKey2,n1);.}../*
2c438 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69  .** Return a poi
2c439 6e 74 65 72 20 74 6f 20 74 68 65 20 61 70 70 72  nter to the appr
2c43a 6f 70 72 69 61 74 65 20 68 61 73 68 20 66 75 6e  opriate hash fun
2c43b 63 74 69 6f 6e 20 67 69 76 65 6e 20 74 68 65 20  ction given the 
2c43c 6b 65 79 20 63 6c 61 73 73 2e 0a 2a 2a 0a 2a 2a  key class..**.**
2c43d 20 54 68 65 20 43 20 73 79 6e 74 61 78 20 69 6e   The C syntax in
2c43e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64   this function d
2c43f 65 66 69 6e 69 74 69 6f 6e 20 6d 61 79 20 62 65  efinition may be
2c440 20 75 6e 66 61 6d 69 6c 61 72 20 74 6f 20 73 6f   unfamilar to so
2c441 6d 65 20 0a 2a 2a 20 70 72 6f 67 72 61 6d 6d 65  me .** programme
2c442 72 73 2c 20 73 6f 20 77 65 20 70 72 6f 76 69 64  rs, so we provid
2c443 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  e the following 
2c444 61 64 64 69 74 69 6f 6e 61 6c 20 65 78 70 6c 61  additional expla
2c445 6e 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68  nation:.**.** Th
2c446 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 75  e name of the fu
2c447 6e 63 74 69 6f 6e 20 69 73 20 22 66 74 73 48 61  nction is "ftsHa
2c448 73 68 46 75 6e 63 74 69 6f 6e 22 2e 20 20 54 68  shFunction".  Th
2c449 65 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 65 73  e function takes
2c44a 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 70 61 72   a.** single par
2c44b 61 6d 65 74 65 72 20 22 6b 65 79 43 6c 61 73 73  ameter "keyClass
2c44c 22 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76  ".  The return v
2c44d 61 6c 75 65 20 6f 66 20 66 74 73 48 61 73 68 46  alue of ftsHashF
2c44e 75 6e 63 74 69 6f 6e 28 29 0a 2a 2a 20 69 73 20  unction().** is 
2c44f 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 6f  a pointer to ano
2c450 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20 20  ther function.  
2c451 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 74 68  Specifically, th
2c452 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a  e return value.*
2c453 2a 20 6f 66 20 66 74 73 48 61 73 68 46 75 6e 63  * of ftsHashFunc
2c454 74 69 6f 6e 28 29 20 69 73 20 61 20 70 6f 69 6e  tion() is a poin
2c455 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f  ter to a functio
2c456 6e 20 74 68 61 74 20 74 61 6b 65 73 20 74 77 6f  n that takes two
2c457 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 77   parameters.** w
2c458 69 74 68 20 74 79 70 65 73 20 22 63 6f 6e 73 74  ith types "const
2c459 20 76 6f 69 64 2a 22 20 61 6e 64 20 22 69 6e 74   void*" and "int
2c45a 22 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e  " and returns an
2c45b 20 22 69 6e 74 22 2e 0a 2a 2f 0a 73 74 61 74 69   "int"..*/.stati
2c45c 63 20 69 6e 74 20 28 2a 66 74 73 48 61 73 68 46  c int (*ftsHashF
2c45d 75 6e 63 74 69 6f 6e 28 69 6e 74 20 6b 65 79 43  unction(int keyC
2c45e 6c 61 73 73 29 29 28 63 6f 6e 73 74 20 76 6f 69  lass))(const voi
2c45f 64 2a 2c 69 6e 74 29 7b 0a 20 20 69 66 28 20 6b  d*,int){.  if( k
2c460 65 79 43 6c 61 73 73 3d 3d 46 54 53 33 5f 48 41  eyClass==FTS3_HA
2c461 53 48 5f 53 54 52 49 4e 47 20 29 7b 0a 20 20 20  SH_STRING ){.   
2c462 20 72 65 74 75 72 6e 20 26 66 74 73 33 53 74 72   return &fts3Str
2c463 48 61 73 68 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Hash;.  }else{. 
2c464 20 20 20 61 73 73 65 72 74 28 20 6b 65 79 43 6c     assert( keyCl
2c465 61 73 73 3d 3d 46 54 53 33 5f 48 41 53 48 5f 42  ass==FTS3_HASH_B
2c466 49 4e 41 52 59 20 29 3b 0a 20 20 20 20 72 65 74  INARY );.    ret
2c467 75 72 6e 20 26 66 74 73 33 42 69 6e 48 61 73 68  urn &fts3BinHash
2c468 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ;.  }.}../*.** R
2c469 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
2c46a 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  to the appropria
2c46b 74 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e  te hash function
2c46c 20 67 69 76 65 6e 20 74 68 65 20 6b 65 79 20 63   given the key c
2c46d 6c 61 73 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  lass..**.** For 
2c46e 68 65 6c 70 20 69 6e 20 69 6e 74 65 72 70 72 65  help in interpre
2c46f 74 65 64 20 74 68 65 20 6f 62 73 63 75 72 65 20  ted the obscure 
2c470 43 20 63 6f 64 65 20 69 6e 20 74 68 65 20 66 75  C code in the fu
2c471 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f  nction definitio
2c472 6e 2c 0a 2a 2a 20 73 65 65 20 74 68 65 20 68 65  n,.** see the he
2c473 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20  ader comment on 
2c474 74 68 65 20 70 72 65 76 69 6f 75 73 20 66 75 6e  the previous fun
2c475 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ction..*/.static
2c476 20 69 6e 74 20 28 2a 66 74 73 43 6f 6d 70 61 72   int (*ftsCompar
2c477 65 46 75 6e 63 74 69 6f 6e 28 69 6e 74 20 6b 65  eFunction(int ke
2c478 79 43 6c 61 73 73 29 29 28 63 6f 6e 73 74 20 76  yClass))(const v
2c479 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
2c47a 6f 69 64 2a 2c 69 6e 74 29 7b 0a 20 20 69 66 28  oid*,int){.  if(
2c47b 20 6b 65 79 43 6c 61 73 73 3d 3d 46 54 53 33 5f   keyClass==FTS3_
2c47c 48 41 53 48 5f 53 54 52 49 4e 47 20 29 7b 0a 20  HASH_STRING ){. 
2c47d 20 20 20 72 65 74 75 72 6e 20 26 66 74 73 33 53     return &fts3S
2c47e 74 72 43 6f 6d 70 61 72 65 3b 0a 20 20 7d 65 6c  trCompare;.  }el
2c47f 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
2c480 6b 65 79 43 6c 61 73 73 3d 3d 46 54 53 33 5f 48  keyClass==FTS3_H
2c481 41 53 48 5f 42 49 4e 41 52 59 20 29 3b 0a 20 20  ASH_BINARY );.  
2c482 20 20 72 65 74 75 72 6e 20 26 66 74 73 33 42 69    return &fts3Bi
2c483 6e 43 6f 6d 70 61 72 65 3b 0a 20 20 7d 0a 7d 0a  nCompare;.  }.}.
2c484 0a 2f 2a 20 4c 69 6e 6b 20 61 6e 20 65 6c 65 6d  ./* Link an elem
2c485 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73  ent into the has
2c486 68 20 74 61 62 6c 65 0a 2a 2f 0a 73 74 61 74 69  h table.*/.stati
2c487 63 20 76 6f 69 64 20 66 74 73 33 48 61 73 68 49  c void fts3HashI
2c488 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 0a 20 20  nsertElement(.  
2c489 66 74 73 33 48 61 73 68 20 2a 70 48 2c 20 20 20  fts3Hash *pH,   
2c48a 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2c48b 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 74 61  complete hash ta
2c48c 62 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ble */.  struct 
2c48d 5f 66 74 73 33 68 74 20 2a 70 45 6e 74 72 79 2c  _fts3ht *pEntry,
2c48e 20 20 2f 2a 20 54 68 65 20 65 6e 74 72 79 20 69    /* The entry i
2c48f 6e 74 6f 20 77 68 69 63 68 20 70 4e 65 77 20 69  nto which pNew i
2c490 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20  s inserted */.  
2c491 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 70 4e  fts3HashElem *pN
2c492 65 77 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ew       /* The 
2c493 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 69 6e  element to be in
2c494 73 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 66  serted */.){.  f
2c495 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 70 48 65  ts3HashElem *pHe
2c496 61 64 3b 20 20 20 20 20 2f 2a 20 46 69 72 73 74  ad;     /* First
2c497 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64 79   element already
2c498 20 69 6e 20 70 45 6e 74 72 79 20 2a 2f 0a 20 20   in pEntry */.  
2c499 70 48 65 61 64 20 3d 20 70 45 6e 74 72 79 2d 3e  pHead = pEntry->
2c49a 63 68 61 69 6e 3b 0a 20 20 69 66 28 20 70 48 65  chain;.  if( pHe
2c49b 61 64 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e  ad ){.    pNew->
2c49c 6e 65 78 74 20 3d 20 70 48 65 61 64 3b 0a 20 20  next = pHead;.  
2c49d 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20 70    pNew->prev = p
2c49e 48 65 61 64 2d 3e 70 72 65 76 3b 0a 20 20 20 20  Head->prev;.    
2c49f 69 66 28 20 70 48 65 61 64 2d 3e 70 72 65 76 20  if( pHead->prev 
2c4a0 29 7b 20 70 48 65 61 64 2d 3e 70 72 65 76 2d 3e  ){ pHead->prev->
2c4a1 6e 65 78 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20  next = pNew; }. 
2c4a2 20 20 20 65 6c 73 65 20 20 20 20 20 20 20 20 20     else         
2c4a3 20 20 20 20 7b 20 70 48 2d 3e 66 69 72 73 74 20      { pH->first 
2c4a4 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70 48  = pNew; }.    pH
2c4a5 65 61 64 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77  ead->prev = pNew
2c4a6 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
2c4a7 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48 2d 3e  New->next = pH->
2c4a8 66 69 72 73 74 3b 0a 20 20 20 20 69 66 28 20 70  first;.    if( p
2c4a9 48 2d 3e 66 69 72 73 74 20 29 7b 20 70 48 2d 3e  H->first ){ pH->
2c4aa 66 69 72 73 74 2d 3e 70 72 65 76 20 3d 20 70 4e  first->prev = pN
2c4ab 65 77 3b 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e  ew; }.    pNew->
2c4ac 70 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 48  prev = 0;.    pH
2c4ad 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a  ->first = pNew;.
2c4ae 20 20 7d 0a 20 20 70 45 6e 74 72 79 2d 3e 63 6f    }.  pEntry->co
2c4af 75 6e 74 2b 2b 3b 0a 20 20 70 45 6e 74 72 79 2d  unt++;.  pEntry-
2c4b0 3e 63 68 61 69 6e 20 3d 20 70 4e 65 77 3b 0a 7d  >chain = pNew;.}
2c4b1 0a 0a 0a 2f 2a 20 52 65 73 69 7a 65 20 74 68 65  .../* Resize the
2c4b2 20 68 61 73 68 20 74 61 62 6c 65 20 73 6f 20 74   hash table so t
2c4b3 68 61 74 20 69 74 20 63 61 6e 74 61 69 6e 73 20  hat it cantains 
2c4b4 22 6e 65 77 5f 73 69 7a 65 22 20 62 75 63 6b 65  "new_size" bucke
2c4b5 74 73 2e 0a 2a 2a 20 22 6e 65 77 5f 73 69 7a 65  ts..** "new_size
2c4b6 22 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65  " must be a powe
2c4b7 72 20 6f 66 20 32 2e 20 20 54 68 65 20 68 61 73  r of 2.  The has
2c4b8 68 20 74 61 62 6c 65 20 6d 69 67 68 74 20 66 61  h table might fa
2c4b9 69 6c 20 0a 2a 2a 20 74 6f 20 72 65 73 69 7a 65  il .** to resize
2c4ba 20 69 66 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63   if sqliteMalloc
2c4bb 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61  () fails..*/.sta
2c4bc 74 69 63 20 76 6f 69 64 20 66 74 73 33 52 65 68  tic void fts3Reh
2c4bd 61 73 68 28 66 74 73 33 48 61 73 68 20 2a 70 48  ash(fts3Hash *pH
2c4be 2c 20 69 6e 74 20 6e 65 77 5f 73 69 7a 65 29 7b  , int new_size){
2c4bf 0a 20 20 73 74 72 75 63 74 20 5f 66 74 73 33 68  .  struct _fts3h
2c4c0 74 20 2a 6e 65 77 5f 68 74 3b 20 20 20 20 20 20  t *new_ht;      
2c4c1 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 68      /* The new h
2c4c2 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 66  ash table */.  f
2c4c3 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65  ts3HashElem *ele
2c4c4 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65 6d 3b 20 20  m, *next_elem;  
2c4c5 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f  /* For looping o
2c4c6 76 65 72 20 65 78 69 73 74 69 6e 67 20 65 6c 65  ver existing ele
2c4c7 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 28  ments */.  int (
2c4c8 2a 78 48 61 73 68 29 28 63 6f 6e 73 74 20 76 6f  *xHash)(const vo
2c4c9 69 64 2a 2c 69 6e 74 29 3b 20 20 20 2f 2a 20 54  id*,int);   /* T
2c4ca 68 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e  he hash function
2c4cb 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 28   */..  assert( (
2c4cc 6e 65 77 5f 73 69 7a 65 20 26 20 28 6e 65 77 5f  new_size & (new_
2c4cd 73 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0a 20  size-1))==0 );. 
2c4ce 20 6e 65 77 5f 68 74 20 3d 20 28 73 74 72 75 63   new_ht = (struc
2c4cf 74 20 5f 66 74 73 33 68 74 20 2a 29 66 74 73 33  t _fts3ht *)fts3
2c4d0 48 61 73 68 4d 61 6c 6c 6f 63 28 20 6e 65 77 5f  HashMalloc( new_
2c4d1 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72 75  size*sizeof(stru
2c4d2 63 74 20 5f 66 74 73 33 68 74 29 20 29 3b 0a 20  ct _fts3ht) );. 
2c4d3 20 69 66 28 20 6e 65 77 5f 68 74 3d 3d 30 20 29   if( new_ht==0 )
2c4d4 20 72 65 74 75 72 6e 3b 0a 20 20 66 74 73 33 48   return;.  fts3H
2c4d5 61 73 68 46 72 65 65 28 70 48 2d 3e 68 74 29 3b  ashFree(pH->ht);
2c4d6 0a 20 20 70 48 2d 3e 68 74 20 3d 20 6e 65 77 5f  .  pH->ht = new_
2c4d7 68 74 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a 65  ht;.  pH->htsize
2c4d8 20 3d 20 6e 65 77 5f 73 69 7a 65 3b 0a 20 20 78   = new_size;.  x
2c4d9 48 61 73 68 20 3d 20 66 74 73 48 61 73 68 46 75  Hash = ftsHashFu
2c4da 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c  nction(pH->keyCl
2c4db 61 73 73 29 3b 0a 20 20 66 6f 72 28 65 6c 65 6d  ass);.  for(elem
2c4dc 3d 70 48 2d 3e 66 69 72 73 74 2c 20 70 48 2d 3e  =pH->first, pH->
2c4dd 66 69 72 73 74 3d 30 3b 20 65 6c 65 6d 3b 20 65  first=0; elem; e
2c4de 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 29  lem = next_elem)
2c4df 7b 0a 20 20 20 20 69 6e 74 20 68 20 3d 20 28 2a  {.    int h = (*
2c4e0 78 48 61 73 68 29 28 65 6c 65 6d 2d 3e 70 4b 65  xHash)(elem->pKe
2c4e1 79 2c 20 65 6c 65 6d 2d 3e 6e 4b 65 79 29 20 26  y, elem->nKey) &
2c4e2 20 28 6e 65 77 5f 73 69 7a 65 2d 31 29 3b 0a 20   (new_size-1);. 
2c4e3 20 20 20 6e 65 78 74 5f 65 6c 65 6d 20 3d 20 65     next_elem = e
2c4e4 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 66  lem->next;.    f
2c4e5 74 73 33 48 61 73 68 49 6e 73 65 72 74 45 6c 65  ts3HashInsertEle
2c4e6 6d 65 6e 74 28 70 48 2c 20 26 6e 65 77 5f 68 74  ment(pH, &new_ht
2c4e7 5b 68 5d 2c 20 65 6c 65 6d 29 3b 0a 20 20 7d 0a  [h], elem);.  }.
2c4e8 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63 74  }../* This funct
2c4e9 69 6f 6e 20 28 66 6f 72 20 69 6e 74 65 72 6e 61  ion (for interna
2c4ea 6c 20 75 73 65 20 6f 6e 6c 79 29 20 6c 6f 63 61  l use only) loca
2c4eb 74 65 73 20 61 6e 20 65 6c 65 6d 65 6e 74 20 69  tes an element i
2c4ec 6e 20 61 6e 0a 2a 2a 20 68 61 73 68 20 74 61 62  n an.** hash tab
2c4ed 6c 65 20 74 68 61 74 20 6d 61 74 63 68 65 73 20  le that matches 
2c4ee 74 68 65 20 67 69 76 65 6e 20 6b 65 79 2e 20 20  the given key.  
2c4ef 54 68 65 20 68 61 73 68 20 66 6f 72 20 74 68 69  The hash for thi
2c4f0 73 20 6b 65 79 20 68 61 73 0a 2a 2a 20 61 6c 72  s key has.** alr
2c4f1 65 61 64 79 20 62 65 65 6e 20 63 6f 6d 70 75 74  eady been comput
2c4f2 65 64 20 61 6e 64 20 69 73 20 70 61 73 73 65 64  ed and is passed
2c4f3 20 61 73 20 74 68 65 20 34 74 68 20 70 61 72 61   as the 4th para
2c4f4 6d 65 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  meter..*/.static
2c4f5 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 66   fts3HashElem *f
2c4f6 74 73 33 46 69 6e 64 45 6c 65 6d 65 6e 74 42 79  ts3FindElementBy
2c4f7 48 61 73 68 28 0a 20 20 63 6f 6e 73 74 20 66 74  Hash(.  const ft
2c4f8 73 33 48 61 73 68 20 2a 70 48 2c 20 2f 2a 20 54  s3Hash *pH, /* T
2c4f9 68 65 20 70 48 20 74 6f 20 62 65 20 73 65 61 72  he pH to be sear
2c4fa 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ched */.  const 
2c4fb 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 2f 2a  void *pKey,   /*
2c4fc 20 54 68 65 20 6b 65 79 20 77 65 20 61 72 65 20   The key we are 
2c4fd 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f  searching for */
2c4fe 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 0a 20 20 69  .  int nKey,.  i
2c4ff 6e 74 20 68 20 20 20 20 20 20 20 20 20 20 20 20  nt h            
2c500 20 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66     /* The hash f
2c501 6f 72 20 74 68 69 73 20 6b 65 79 2e 20 2a 2f 0a  or this key. */.
2c502 29 7b 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65  ){.  fts3HashEle
2c503 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 20  m *elem;        
2c504 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c      /* Used to l
2c505 6f 6f 70 20 74 68 72 75 20 74 68 65 20 65 6c 65  oop thru the ele
2c506 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 69  ment list */.  i
2c507 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20  nt count;       
2c508 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2c509 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   Number of eleme
2c50a 6e 74 73 20 6c 65 66 74 20 74 6f 20 74 65 73 74  nts left to test
2c50b 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d   */.  int (*xCom
2c50c 70 61 72 65 29 28 63 6f 6e 73 74 20 76 6f 69 64  pare)(const void
2c50d 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
2c50e 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 63 6f 6d 70  *,int);  /* comp
2c50f 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  arison function 
2c510 2a 2f 0a 0a 20 20 69 66 28 20 70 48 2d 3e 68 74  */..  if( pH->ht
2c511 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 5f   ){.    struct _
2c512 66 74 73 33 68 74 20 2a 70 45 6e 74 72 79 20 3d  fts3ht *pEntry =
2c513 20 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 20   &pH->ht[h];.   
2c514 20 65 6c 65 6d 20 3d 20 70 45 6e 74 72 79 2d 3e   elem = pEntry->
2c515 63 68 61 69 6e 3b 0a 20 20 20 20 63 6f 75 6e 74  chain;.    count
2c516 20 3d 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74   = pEntry->count
2c517 3b 0a 20 20 20 20 78 43 6f 6d 70 61 72 65 20 3d  ;.    xCompare =
2c518 20 66 74 73 43 6f 6d 70 61 72 65 46 75 6e 63 74   ftsCompareFunct
2c519 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c 61 73 73  ion(pH->keyClass
2c51a 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 63 6f  );.    while( co
2c51b 75 6e 74 2d 2d 20 26 26 20 65 6c 65 6d 20 29 7b  unt-- && elem ){
2c51c 0a 20 20 20 20 20 20 69 66 28 20 28 2a 78 43 6f  .      if( (*xCo
2c51d 6d 70 61 72 65 29 28 65 6c 65 6d 2d 3e 70 4b 65  mpare)(elem->pKe
2c51e 79 2c 65 6c 65 6d 2d 3e 6e 4b 65 79 2c 70 4b 65  y,elem->nKey,pKe
2c51f 79 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b 20 0a 20  y,nKey)==0 ){ . 
2c520 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 65 6c         return el
2c521 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  em;.      }.    
2c522 20 20 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e    elem = elem->n
2c523 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ext;.    }.  }. 
2c524 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
2c525 20 52 65 6d 6f 76 65 20 61 20 73 69 6e 67 6c 65   Remove a single
2c526 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20   entry from the 
2c527 68 61 73 68 20 74 61 62 6c 65 20 67 69 76 65 6e  hash table given
2c528 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
2c529 61 74 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 61 6e  at.** element an
2c52a 64 20 61 20 68 61 73 68 20 6f 6e 20 74 68 65 20  d a hash on the 
2c52b 65 6c 65 6d 65 6e 74 27 73 20 6b 65 79 2e 0a 2a  element's key..*
2c52c 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74  /.static void ft
2c52d 73 33 52 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 42  s3RemoveElementB
2c52e 79 48 61 73 68 28 0a 20 20 66 74 73 33 48 61 73  yHash(.  fts3Has
2c52f 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20 20 2f  h *pH,         /
2c530 2a 20 54 68 65 20 70 48 20 63 6f 6e 74 61 69 6e  * The pH contain
2c531 69 6e 67 20 22 65 6c 65 6d 22 20 2a 2f 0a 20 20  ing "elem" */.  
2c532 66 74 73 33 48 61 73 68 45 6c 65 6d 2a 20 65 6c  fts3HashElem* el
2c533 65 6d 2c 20 20 20 2f 2a 20 54 68 65 20 65 6c 65  em,   /* The ele
2c534 6d 65 6e 74 20 74 6f 20 62 65 20 72 65 6d 6f 76  ment to be remov
2c535 65 64 20 66 72 6f 6d 20 74 68 65 20 70 48 20 2a  ed from the pH *
2c536 2f 0a 20 20 69 6e 74 20 68 20 20 20 20 20 20 20  /.  int h       
2c537 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73            /* Has
2c538 68 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  h value for the 
2c539 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20  element */.){.  
2c53a 73 74 72 75 63 74 20 5f 66 74 73 33 68 74 20 2a  struct _fts3ht *
2c53b 70 45 6e 74 72 79 3b 0a 20 20 69 66 28 20 65 6c  pEntry;.  if( el
2c53c 65 6d 2d 3e 70 72 65 76 20 29 7b 0a 20 20 20 20  em->prev ){.    
2c53d 65 6c 65 6d 2d 3e 70 72 65 76 2d 3e 6e 65 78 74  elem->prev->next
2c53e 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 20 0a   = elem->next; .
2c53f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 48 2d    }else{.    pH-
2c540 3e 66 69 72 73 74 20 3d 20 65 6c 65 6d 2d 3e 6e  >first = elem->n
2c541 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 65  ext;.  }.  if( e
2c542 6c 65 6d 2d 3e 6e 65 78 74 20 29 7b 0a 20 20 20  lem->next ){.   
2c543 20 65 6c 65 6d 2d 3e 6e 65 78 74 2d 3e 70 72 65   elem->next->pre
2c544 76 20 3d 20 65 6c 65 6d 2d 3e 70 72 65 76 3b 0a  v = elem->prev;.
2c545 20 20 7d 0a 20 20 70 45 6e 74 72 79 20 3d 20 26    }.  pEntry = &
2c546 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 69 66 28  pH->ht[h];.  if(
2c547 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3d 3d   pEntry->chain==
2c548 65 6c 65 6d 20 29 7b 0a 20 20 20 20 70 45 6e 74  elem ){.    pEnt
2c549 72 79 2d 3e 63 68 61 69 6e 20 3d 20 65 6c 65 6d  ry->chain = elem
2c54a 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 70 45  ->next;.  }.  pE
2c54b 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20  ntry->count--;. 
2c54c 20 69 66 28 20 70 45 6e 74 72 79 2d 3e 63 6f 75   if( pEntry->cou
2c54d 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20 70 45 6e  nt<=0 ){.    pEn
2c54e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 30 3b 0a  try->chain = 0;.
2c54f 20 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f    }.  if( pH->co
2c550 70 79 4b 65 79 20 26 26 20 65 6c 65 6d 2d 3e 70  pyKey && elem->p
2c551 4b 65 79 20 29 7b 0a 20 20 20 20 66 74 73 33 48  Key ){.    fts3H
2c552 61 73 68 46 72 65 65 28 65 6c 65 6d 2d 3e 70 4b  ashFree(elem->pK
2c553 65 79 29 3b 0a 20 20 7d 0a 20 20 66 74 73 33 48  ey);.  }.  fts3H
2c554 61 73 68 46 72 65 65 28 20 65 6c 65 6d 20 29 3b  ashFree( elem );
2c555 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a  .  pH->count--;.
2c556 20 20 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74 3c    if( pH->count<
2c557 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  =0 ){.    assert
2c558 28 20 70 48 2d 3e 66 69 72 73 74 3d 3d 30 20 29  ( pH->first==0 )
2c559 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 48  ;.    assert( pH
2c55a 2d 3e 63 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20 20  ->count==0 );.  
2c55b 20 20 66 74 73 33 48 61 73 68 43 6c 65 61 72 28    fts3HashClear(
2c55c 70 48 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41  pH);.  }.}../* A
2c55d 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63 61 74 65  ttempt to locate
2c55e 20 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74   an element of t
2c55f 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 48  he hash table pH
2c560 20 77 69 74 68 20 61 20 6b 65 79 0a 2a 2a 20 74   with a key.** t
2c561 68 61 74 20 6d 61 74 63 68 65 73 20 70 4b 65 79  hat matches pKey
2c562 2c 6e 4b 65 79 2e 20 20 52 65 74 75 72 6e 20 74  ,nKey.  Return t
2c563 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 69 73  he data for this
2c564 20 65 6c 65 6d 65 6e 74 20 69 66 20 69 74 20 69   element if it i
2c565 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 6f 72 20 4e  s.** found, or N
2c566 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73 20  ULL if there is 
2c567 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c  no match..*/.SQL
2c568 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2c569 20 2a 73 71 6c 69 74 65 33 46 74 73 33 48 61 73   *sqlite3Fts3Has
2c56a 68 46 69 6e 64 28 63 6f 6e 73 74 20 66 74 73 33  hFind(const fts3
2c56b 48 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73 74 20  Hash *pH, const 
2c56c 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20  void *pKey, int 
2c56d 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 20 68 3b 20  nKey){.  int h; 
2c56e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c56f 2f 2a 20 41 20 68 61 73 68 20 6f 6e 20 6b 65 79  /* A hash on key
2c570 20 2a 2f 0a 20 20 66 74 73 33 48 61 73 68 45 6c   */.  fts3HashEl
2c571 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 2f 2a 20  em *elem;    /* 
2c572 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 68 61 74  The element that
2c573 20 6d 61 74 63 68 65 73 20 6b 65 79 20 2a 2f 0a   matches key */.
2c574 20 20 69 6e 74 20 28 2a 78 48 61 73 68 29 28 63    int (*xHash)(c
2c575 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b  onst void*,int);
2c576 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66 75    /* The hash fu
2c577 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28  nction */..  if(
2c578 20 70 48 3d 3d 30 20 7c 7c 20 70 48 2d 3e 68 74   pH==0 || pH->ht
2c579 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
2c57a 20 20 78 48 61 73 68 20 3d 20 66 74 73 48 61 73    xHash = ftsHas
2c57b 68 46 75 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b 65  hFunction(pH->ke
2c57c 79 43 6c 61 73 73 29 3b 0a 20 20 61 73 73 65 72  yClass);.  asser
2c57d 74 28 20 78 48 61 73 68 21 3d 30 20 29 3b 0a 20  t( xHash!=0 );. 
2c57e 20 68 20 3d 20 28 2a 78 48 61 73 68 29 28 70 4b   h = (*xHash)(pK
2c57f 65 79 2c 6e 4b 65 79 29 3b 0a 20 20 61 73 73 65  ey,nKey);.  asse
2c580 72 74 28 20 28 70 48 2d 3e 68 74 73 69 7a 65 20  rt( (pH->htsize 
2c581 26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29  & (pH->htsize-1)
2c582 29 3d 3d 30 20 29 3b 0a 20 20 65 6c 65 6d 20 3d  )==0 );.  elem =
2c583 20 66 74 73 33 46 69 6e 64 45 6c 65 6d 65 6e 74   fts3FindElement
2c584 42 79 48 61 73 68 28 70 48 2c 70 4b 65 79 2c 6e  ByHash(pH,pKey,n
2c585 4b 65 79 2c 20 68 20 26 20 28 70 48 2d 3e 68 74  Key, h & (pH->ht
2c586 73 69 7a 65 2d 31 29 29 3b 0a 20 20 72 65 74 75  size-1));.  retu
2c587 72 6e 20 65 6c 65 6d 20 3f 20 65 6c 65 6d 2d 3e  rn elem ? elem->
2c588 64 61 74 61 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 20  data : 0;.}../* 
2c589 49 6e 73 65 72 74 20 61 6e 20 65 6c 65 6d 65 6e  Insert an elemen
2c58a 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20  t into the hash 
2c58b 74 61 62 6c 65 20 70 48 2e 20 20 54 68 65 20 6b  table pH.  The k
2c58c 65 79 20 69 73 20 70 4b 65 79 2c 6e 4b 65 79 0a  ey is pKey,nKey.
2c58d 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 20  ** and the data 
2c58e 69 73 20 22 64 61 74 61 22 2e 0a 2a 2a 0a 2a 2a  is "data"..**.**
2c58f 20 49 66 20 6e 6f 20 65 6c 65 6d 65 6e 74 20 65   If no element e
2c590 78 69 73 74 73 20 77 69 74 68 20 61 20 6d 61 74  xists with a mat
2c591 63 68 69 6e 67 20 6b 65 79 2c 20 74 68 65 6e 20  ching key, then 
2c592 61 20 6e 65 77 0a 2a 2a 20 65 6c 65 6d 65 6e 74  a new.** element
2c593 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 41 20   is created.  A 
2c594 63 6f 70 79 20 6f 66 20 74 68 65 20 6b 65 79 20  copy of the key 
2c595 69 73 20 6d 61 64 65 20 69 66 20 74 68 65 20 63  is made if the c
2c596 6f 70 79 4b 65 79 0a 2a 2a 20 66 6c 61 67 20 69  opyKey.** flag i
2c597 73 20 73 65 74 2e 20 20 4e 55 4c 4c 20 69 73 20  s set.  NULL is 
2c598 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
2c599 49 66 20 61 6e 6f 74 68 65 72 20 65 6c 65 6d 65  If another eleme
2c59a 6e 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  nt already exist
2c59b 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20  s with the same 
2c59c 6b 65 79 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a  key, then the.**
2c59d 20 6e 65 77 20 64 61 74 61 20 72 65 70 6c 61 63   new data replac
2c59e 65 73 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20  es the old data 
2c59f 61 6e 64 20 74 68 65 20 6f 6c 64 20 64 61 74 61  and the old data
2c5a0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
2c5a1 20 54 68 65 20 6b 65 79 20 69 73 20 6e 6f 74 20   The key is not 
2c5a2 63 6f 70 69 65 64 20 69 6e 20 74 68 69 73 20 69  copied in this i
2c5a3 6e 73 74 61 6e 63 65 2e 20 20 49 66 20 61 20 6d  nstance.  If a m
2c5a4 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 74 68 65  alloc fails, the
2c5a5 6e 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74  n.** the new dat
2c5a6 61 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  a is returned an
2c5a7 64 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  d the hash table
2c5a8 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a   is unchanged..*
2c5a9 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 64 61 74  *.** If the "dat
2c5aa 61 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  a" parameter to 
2c5ab 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
2c5ac 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a   NULL, then the.
2c5ad 2a 2a 20 65 6c 65 6d 65 6e 74 20 63 6f 72 72 65  ** element corre
2c5ae 73 70 6f 6e 64 69 6e 67 20 74 6f 20 22 6b 65 79  sponding to "key
2c5af 22 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72 6f  " is removed fro
2c5b0 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  m the hash table
2c5b1 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2c5b2 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
2c5b3 33 46 74 73 33 48 61 73 68 49 6e 73 65 72 74 28  3Fts3HashInsert(
2c5b4 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70 48 2c  .  fts3Hash *pH,
2c5b5 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 68          /* The h
2c5b6 61 73 68 20 74 61 62 6c 65 20 74 6f 20 69 6e 73  ash table to ins
2c5b7 65 72 74 20 69 6e 74 6f 20 2a 2f 0a 20 20 63 6f  ert into */.  co
2c5b8 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
2c5b9 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 2a 2f     /* The key */
2c5ba 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20 20  .  int nKey,    
2c5bb 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
2c5bc 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
2c5bd 65 20 6b 65 79 20 2a 2f 0a 20 20 76 6f 69 64 20  e key */.  void 
2c5be 2a 64 61 74 61 20 20 20 20 20 20 20 20 20 20 20  *data           
2c5bf 2f 2a 20 54 68 65 20 64 61 74 61 20 2a 2f 0a 29  /* The data */.)
2c5c0 7b 0a 20 20 69 6e 74 20 68 72 61 77 3b 20 20 20  {.  int hraw;   
2c5c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2c5c2 20 52 61 77 20 68 61 73 68 20 76 61 6c 75 65 20   Raw hash value 
2c5c3 6f 66 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20 20  of the key */.  
2c5c4 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 20  int h;          
2c5c5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65            /* the
2c5c6 20 68 61 73 68 20 6f 66 20 74 68 65 20 6b 65 79   hash of the key
2c5c7 20 6d 6f 64 75 6c 6f 20 68 61 73 68 20 74 61 62   modulo hash tab
2c5c8 6c 65 20 73 69 7a 65 20 2a 2f 0a 20 20 66 74 73  le size */.  fts
2c5c9 33 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b  3HashElem *elem;
2c5ca 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74         /* Used t
2c5cb 6f 20 6c 6f 6f 70 20 74 68 72 75 20 74 68 65 20  o loop thru the 
2c5cc 65 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a  element list */.
2c5cd 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a    fts3HashElem *
2c5ce 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a 20 4e  new_elem;   /* N
2c5cf 65 77 20 65 6c 65 6d 65 6e 74 20 61 64 64 65 64  ew element added
2c5d0 20 74 6f 20 74 68 65 20 70 48 20 2a 2f 0a 20 20   to the pH */.  
2c5d1 69 6e 74 20 28 2a 78 48 61 73 68 29 28 63 6f 6e  int (*xHash)(con
2c5d2 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 20  st void*,int);  
2c5d3 2f 2a 20 54 68 65 20 68 61 73 68 20 66 75 6e 63  /* The hash func
2c5d4 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72  tion */..  asser
2c5d5 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 78 48  t( pH!=0 );.  xH
2c5d6 61 73 68 20 3d 20 66 74 73 48 61 73 68 46 75 6e  ash = ftsHashFun
2c5d7 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c 61  ction(pH->keyCla
2c5d8 73 73 29 3b 0a 20 20 61 73 73 65 72 74 28 20 78  ss);.  assert( x
2c5d9 48 61 73 68 21 3d 30 20 29 3b 0a 20 20 68 72 61  Hash!=0 );.  hra
2c5da 77 20 3d 20 28 2a 78 48 61 73 68 29 28 70 4b 65  w = (*xHash)(pKe
2c5db 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 61 73 73 65  y, nKey);.  asse
2c5dc 72 74 28 20 28 70 48 2d 3e 68 74 73 69 7a 65 20  rt( (pH->htsize 
2c5dd 26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29  & (pH->htsize-1)
2c5de 29 3d 3d 30 20 29 3b 0a 20 20 68 20 3d 20 68 72  )==0 );.  h = hr
2c5df 61 77 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65  aw & (pH->htsize
2c5e0 2d 31 29 3b 0a 20 20 65 6c 65 6d 20 3d 20 66 74  -1);.  elem = ft
2c5e1 73 33 46 69 6e 64 45 6c 65 6d 65 6e 74 42 79 48  s3FindElementByH
2c5e2 61 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79  ash(pH,pKey,nKey
2c5e3 2c 68 29 3b 0a 20 20 69 66 28 20 65 6c 65 6d 20  ,h);.  if( elem 
2c5e4 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64  ){.    void *old
2c5e5 5f 64 61 74 61 20 3d 20 65 6c 65 6d 2d 3e 64 61  _data = elem->da
2c5e6 74 61 3b 0a 20 20 20 20 69 66 28 20 64 61 74 61  ta;.    if( data
2c5e7 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 74 73  ==0 ){.      fts
2c5e8 33 52 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 42 79  3RemoveElementBy
2c5e9 48 61 73 68 28 70 48 2c 65 6c 65 6d 2c 68 29 3b  Hash(pH,elem,h);
2c5ea 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2c5eb 20 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64    elem->data = d
2c5ec 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  ata;.    }.    r
2c5ed 65 74 75 72 6e 20 6f 6c 64 5f 64 61 74 61 3b 0a  eturn old_data;.
2c5ee 20 20 7d 0a 20 20 69 66 28 20 64 61 74 61 3d 3d    }.  if( data==
2c5ef 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
2c5f0 6e 65 77 5f 65 6c 65 6d 20 3d 20 28 66 74 73 33  new_elem = (fts3
2c5f1 48 61 73 68 45 6c 65 6d 2a 29 66 74 73 33 48 61  HashElem*)fts3Ha
2c5f2 73 68 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66  shMalloc( sizeof
2c5f3 28 66 74 73 33 48 61 73 68 45 6c 65 6d 29 20 29  (fts3HashElem) )
2c5f4 3b 0a 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d  ;.  if( new_elem
2c5f5 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 64 61 74  ==0 ) return dat
2c5f6 61 3b 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 70  a;.  if( pH->cop
2c5f7 79 4b 65 79 20 26 26 20 70 4b 65 79 21 3d 30 20  yKey && pKey!=0 
2c5f8 29 7b 0a 20 20 20 20 6e 65 77 5f 65 6c 65 6d 2d  ){.    new_elem-
2c5f9 3e 70 4b 65 79 20 3d 20 66 74 73 33 48 61 73 68  >pKey = fts3Hash
2c5fa 4d 61 6c 6c 6f 63 28 20 6e 4b 65 79 20 29 3b 0a  Malloc( nKey );.
2c5fb 20 20 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d      if( new_elem
2c5fc 2d 3e 70 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20  ->pKey==0 ){.   
2c5fd 20 20 20 66 74 73 33 48 61 73 68 46 72 65 65 28     fts3HashFree(
2c5fe 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 20 20 20  new_elem);.     
2c5ff 20 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20   return data;.  
2c600 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 28    }.    memcpy((
2c601 76 6f 69 64 2a 29 6e 65 77 5f 65 6c 65 6d 2d 3e  void*)new_elem->
2c602 70 4b 65 79 2c 20 70 4b 65 79 2c 20 6e 4b 65 79  pKey, pKey, nKey
2c603 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
2c604 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d  new_elem->pKey =
2c605 20 28 76 6f 69 64 2a 29 70 4b 65 79 3b 0a 20 20   (void*)pKey;.  
2c606 7d 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 6e 4b  }.  new_elem->nK
2c607 65 79 20 3d 20 6e 4b 65 79 3b 0a 20 20 70 48 2d  ey = nKey;.  pH-
2c608 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 69 66 28 20  >count++;.  if( 
2c609 70 48 2d 3e 68 74 73 69 7a 65 3d 3d 30 20 29 7b  pH->htsize==0 ){
2c60a 0a 20 20 20 20 66 74 73 33 52 65 68 61 73 68 28  .    fts3Rehash(
2c60b 70 48 2c 38 29 3b 0a 20 20 20 20 69 66 28 20 70  pH,8);.    if( p
2c60c 48 2d 3e 68 74 73 69 7a 65 3d 3d 30 20 29 7b 0a  H->htsize==0 ){.
2c60d 20 20 20 20 20 20 70 48 2d 3e 63 6f 75 6e 74 20        pH->count 
2c60e 3d 20 30 3b 0a 20 20 20 20 20 20 66 74 73 33 48  = 0;.      fts3H
2c60f 61 73 68 46 72 65 65 28 6e 65 77 5f 65 6c 65 6d  ashFree(new_elem
2c610 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
2c611 64 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  data;.    }.  }.
2c612 20 20 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74 20    if( pH->count 
2c613 3e 20 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a  > pH->htsize ){.
2c614 20 20 20 20 66 74 73 33 52 65 68 61 73 68 28 70      fts3Rehash(p
2c615 48 2c 70 48 2d 3e 68 74 73 69 7a 65 2a 32 29 3b  H,pH->htsize*2);
2c616 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
2c617 48 2d 3e 68 74 73 69 7a 65 3e 30 20 29 3b 0a 20  H->htsize>0 );. 
2c618 20 61 73 73 65 72 74 28 20 28 70 48 2d 3e 68 74   assert( (pH->ht
2c619 73 69 7a 65 20 26 20 28 70 48 2d 3e 68 74 73 69  size & (pH->htsi
2c61a 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20 68  ze-1))==0 );.  h
2c61b 20 3d 20 68 72 61 77 20 26 20 28 70 48 2d 3e 68   = hraw & (pH->h
2c61c 74 73 69 7a 65 2d 31 29 3b 0a 20 20 66 74 73 33  tsize-1);.  fts3
2c61d 48 61 73 68 49 6e 73 65 72 74 45 6c 65 6d 65 6e  HashInsertElemen
2c61e 74 28 70 48 2c 20 26 70 48 2d 3e 68 74 5b 68 5d  t(pH, &pH->ht[h]
2c61f 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 6e  , new_elem);.  n
2c620 65 77 5f 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20  ew_elem->data = 
2c621 64 61 74 61 3b 0a 20 20 72 65 74 75 72 6e 20 30  data;.  return 0
2c622 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21  ;.}..#endif /* !
2c623 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43  defined(SQLITE_C
2c624 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  ORE) || defined(
2c625 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
2c626 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  S3) */../*******
2c627 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66  ******* End of f
2c628 74 73 33 5f 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a  ts3_hash.c *****
2c629 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c62a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c62b 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
2c62c 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
2c62d 6c 65 20 66 74 73 33 5f 70 6f 72 74 65 72 2e 63  le fts3_porter.c
2c62e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2c62f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c630 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
2c631 30 36 20 53 65 70 74 65 6d 62 65 72 20 33 30 0a  06 September 30.
2c632 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
2c633 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
2c634 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
2c635 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
2c636 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
2c637 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
2c638 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
2c639 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
2c63a 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
2c63b 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
2c63c 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
2c63d 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
2c63e 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
2c63f 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
2c640 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
2c641 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
2c642 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
2c643 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
2c644 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c645 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c646 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c647 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
2c648 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
2c649 20 6f 66 20 74 68 65 20 66 75 6c 6c 2d 74 65 78   of the full-tex
2c64a 74 2d 73 65 61 72 63 68 20 74 6f 6b 65 6e 69 7a  t-search tokeniz
2c64b 65 72 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  er that implemen
2c64c 74 73 0a 2a 2a 20 61 20 50 6f 72 74 65 72 20 73  ts.** a Porter s
2c64d 74 65 6d 6d 65 72 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  temmer..*/../*.*
2c64e 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68  * The code in th
2c64f 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20  is file is only 
2c650 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a  compiled if:.**.
2c651 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53  **     * The FTS
2c652 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e  3 module is bein
2c653 67 20 62 75 69 6c 74 20 61 73 20 61 6e 20 65 78  g built as an ex
2c654 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20  tension.**      
2c655 20 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20   (in which case 
2c656 53 51 4c 49 54 45 5f 43 4f 52 45 20 69 73 20 6e  SQLITE_CORE is n
2c657 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 6f 72 0a  ot defined), or.
2c658 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20  **.**     * The 
2c659 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62  FTS3 module is b
2c65a 65 69 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f 20  eing built into 
2c65b 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a 20 20  the core of.**  
2c65c 20 20 20 20 20 53 51 4c 69 74 65 20 28 69 6e 20       SQLite (in 
2c65d 77 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54  which case SQLIT
2c65e 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20 69 73  E_ENABLE_FTS3 is
2c65f 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69   defined)..*/.#i
2c660 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
2c661 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
2c662 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
2c663 5f 46 54 53 33 29 0a 0a 0a 0a 0a 2f 2a 0a 2a 2a  _FTS3)...../*.**
2c664 20 43 6c 61 73 73 20 64 65 72 69 76 65 64 20 66   Class derived f
2c665 72 6f 6d 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  rom sqlite3_toke
2c666 6e 69 7a 65 72 0a 2a 2f 0a 74 79 70 65 64 65 66  nizer.*/.typedef
2c667 20 73 74 72 75 63 74 20 70 6f 72 74 65 72 5f 74   struct porter_t
2c668 6f 6b 65 6e 69 7a 65 72 20 7b 0a 20 20 73 71 6c  okenizer {.  sql
2c669 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 62  ite3_tokenizer b
2c66a 61 73 65 3b 20 20 20 20 20 20 2f 2a 20 42 61 73  ase;      /* Bas
2c66b 65 20 63 6c 61 73 73 20 2a 2f 0a 7d 20 70 6f 72  e class */.} por
2c66c 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 3b 0a 0a  ter_tokenizer;..
2c66d 2f 2a 0a 2a 2a 20 43 6c 61 73 73 20 64 65 72 69  /*.** Class deri
2c66e 76 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 33 5f  ved from sqlit3_
2c66f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
2c670 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
2c671 63 74 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69  ct porter_tokeni
2c672 7a 65 72 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73  zer_cursor {.  s
2c673 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
2c674 5f 63 75 72 73 6f 72 20 62 61 73 65 3b 0a 20 20  _cursor base;.  
2c675 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70  const char *zInp
2c676 75 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ut;          /* 
2c677 69 6e 70 75 74 20 77 65 20 61 72 65 20 74 6f 6b  input we are tok
2c678 65 6e 69 7a 69 6e 67 20 2a 2f 0a 20 20 69 6e 74  enizing */.  int
2c679 20 6e 49 6e 70 75 74 3b 20 20 20 20 20 20 20 20   nInput;        
2c67a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 69 7a            /* siz
2c67b 65 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 2a  e of the input *
2c67c 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b  /.  int iOffset;
2c67d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c67e 20 2f 2a 20 63 75 72 72 65 6e 74 20 70 6f 73 69   /* current posi
2c67f 74 69 6f 6e 20 69 6e 20 7a 49 6e 70 75 74 20 2a  tion in zInput *
2c680 2f 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e 3b 20  /.  int iToken; 
2c681 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c682 20 2f 2a 20 69 6e 64 65 78 20 6f 66 20 6e 65 78   /* index of nex
2c683 74 20 74 6f 6b 65 6e 20 74 6f 20 62 65 20 72 65  t token to be re
2c684 74 75 72 6e 65 64 20 2a 2f 0a 20 20 63 68 61 72  turned */.  char
2c685 20 2a 7a 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20   *zToken;       
2c686 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74 6f 72           /* stor
2c687 61 67 65 20 66 6f 72 20 63 75 72 72 65 6e 74 20  age for current 
2c688 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e  token */.  int n
2c689 41 6c 6c 6f 63 61 74 65 64 3b 20 20 20 20 20 20  Allocated;      
2c68a 20 20 20 20 20 20 20 20 2f 2a 20 73 70 61 63 65          /* space
2c68b 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 7a 54   allocated to zT
2c68c 6f 6b 65 6e 20 62 75 66 66 65 72 20 2a 2f 0a 7d  oken buffer */.}
2c68d 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65   porter_tokenize
2c68e 72 5f 63 75 72 73 6f 72 3b 0a 0a 0a 2f 2a 20 46  r_cursor;.../* F
2c68f 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69  orward declarati
2c690 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  on */.static con
2c691 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  st sqlite3_token
2c692 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 70 6f 72 74  izer_module port
2c693 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  erTokenizerModul
2c694 65 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  e;.../*.** Creat
2c695 65 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a 65  e a new tokenize
2c696 72 20 69 6e 73 74 61 6e 63 65 2e 0a 2a 2f 0a 73  r instance..*/.s
2c697 74 61 74 69 63 20 69 6e 74 20 70 6f 72 74 65 72  tatic int porter
2c698 43 72 65 61 74 65 28 0a 20 20 69 6e 74 20 61 72  Create(.  int ar
2c699 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  gc, const char *
2c69a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c 0a 20 20   const *argv,.  
2c69b 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
2c69c 72 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 0a  r **ppTokenizer.
2c69d 29 7b 0a 20 20 70 6f 72 74 65 72 5f 74 6f 6b 65  ){.  porter_toke
2c69e 6e 69 7a 65 72 20 2a 74 3b 0a 20 20 74 20 3d 20  nizer *t;.  t = 
2c69f 28 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65  (porter_tokenize
2c6a0 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c  r *) sqlite3_mal
2c6a1 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 74 29 29 3b  loc(sizeof(*t));
2c6a2 0a 20 20 69 66 28 20 74 3d 3d 4e 55 4c 4c 20 29  .  if( t==NULL )
2c6a3 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
2c6a4 4f 4d 45 4d 3b 0a 20 20 6d 65 6d 73 65 74 28 74  OMEM;.  memset(t
2c6a5 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 74 29 29  , 0, sizeof(*t))
2c6a6 3b 0a 20 20 2a 70 70 54 6f 6b 65 6e 69 7a 65 72  ;.  *ppTokenizer
2c6a7 20 3d 20 26 74 2d 3e 62 61 73 65 3b 0a 20 20 72   = &t->base;.  r
2c6a8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2c6a9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f  .}../*.** Destro
2c6aa 79 20 61 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f  y a tokenizer.*/
2c6ab 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f 72 74  .static int port
2c6ac 65 72 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65  erDestroy(sqlite
2c6ad 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f  3_tokenizer *pTo
2c6ae 6b 65 6e 69 7a 65 72 29 7b 0a 20 20 73 71 6c 69  kenizer){.  sqli
2c6af 74 65 33 5f 66 72 65 65 28 70 54 6f 6b 65 6e 69  te3_free(pTokeni
2c6b0 7a 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  zer);.  return S
2c6b1 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
2c6b2 2a 2a 20 50 72 65 70 61 72 65 20 74 6f 20 62 65  ** Prepare to be
2c6b3 67 69 6e 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 61  gin tokenizing a
2c6b4 20 70 61 72 74 69 63 75 6c 61 72 20 73 74 72 69   particular stri
2c6b5 6e 67 2e 20 20 54 68 65 20 69 6e 70 75 74 0a 2a  ng.  The input.*
2c6b6 2a 20 73 74 72 69 6e 67 20 74 6f 20 62 65 20 74  * string to be t
2c6b7 6f 6b 65 6e 69 7a 65 64 20 69 73 20 7a 49 6e 70  okenized is zInp
2c6b8 75 74 5b 30 2e 2e 6e 49 6e 70 75 74 2d 31 5d 2e  ut[0..nInput-1].
2c6b9 20 20 41 20 63 75 72 73 6f 72 0a 2a 2a 20 75 73    A cursor.** us
2c6ba 65 64 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 61  ed to incrementa
2c6bb 6c 6c 79 20 74 6f 6b 65 6e 69 7a 65 20 74 68 69  lly tokenize thi
2c6bc 73 20 73 74 72 69 6e 67 20 69 73 20 72 65 74 75  s string is retu
2c6bd 72 6e 65 64 20 69 6e 20 0a 2a 2a 20 2a 70 70 43  rned in .** *ppC
2c6be 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  ursor..*/.static
2c6bf 20 69 6e 74 20 70 6f 72 74 65 72 4f 70 65 6e 28   int porterOpen(
2c6c0 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
2c6c1 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72  izer *pTokenizer
2c6c2 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
2c6c3 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20   tokenizer */.  
2c6c4 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70  const char *zInp
2c6c5 75 74 2c 20 69 6e 74 20 6e 49 6e 70 75 74 2c 20  ut, int nInput, 
2c6c6 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67         /* String
2c6c7 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65 64   to be tokenized
2c6c8 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f   */.  sqlite3_to
2c6c9 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
2c6ca 2a 70 70 43 75 72 73 6f 72 20 20 20 20 2f 2a 20  *ppCursor    /* 
2c6cb 4f 55 54 3a 20 54 6f 6b 65 6e 69 7a 61 74 69 6f  OUT: Tokenizatio
2c6cc 6e 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20  n cursor */.){. 
2c6cd 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65   porter_tokenize
2c6ce 72 5f 63 75 72 73 6f 72 20 2a 63 3b 0a 0a 20 20  r_cursor *c;..  
2c6cf 63 20 3d 20 28 70 6f 72 74 65 72 5f 74 6f 6b 65  c = (porter_toke
2c6d0 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29 20  nizer_cursor *) 
2c6d1 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
2c6d2 69 7a 65 6f 66 28 2a 63 29 29 3b 0a 20 20 69 66  izeof(*c));.  if
2c6d3 28 20 63 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75  ( c==NULL ) retu
2c6d4 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
2c6d5 0a 0a 20 20 63 2d 3e 7a 49 6e 70 75 74 20 3d 20  ..  c->zInput = 
2c6d6 7a 49 6e 70 75 74 3b 0a 20 20 69 66 28 20 7a 49  zInput;.  if( zI
2c6d7 6e 70 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 63  nput==0 ){.    c
2c6d8 2d 3e 6e 49 6e 70 75 74 20 3d 20 30 3b 0a 20 20  ->nInput = 0;.  
2c6d9 7d 65 6c 73 65 20 69 66 28 20 6e 49 6e 70 75 74  }else if( nInput
2c6da 3c 30 20 29 7b 0a 20 20 20 20 63 2d 3e 6e 49 6e  <0 ){.    c->nIn
2c6db 70 75 74 20 3d 20 28 69 6e 74 29 73 74 72 6c 65  put = (int)strle
2c6dc 6e 28 7a 49 6e 70 75 74 29 3b 0a 20 20 7d 65 6c  n(zInput);.  }el
2c6dd 73 65 7b 0a 20 20 20 20 63 2d 3e 6e 49 6e 70 75  se{.    c->nInpu
2c6de 74 20 3d 20 6e 49 6e 70 75 74 3b 0a 20 20 7d 0a  t = nInput;.  }.
2c6df 20 20 63 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30    c->iOffset = 0
2c6e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2c6e1 20 20 2f 2a 20 73 74 61 72 74 20 74 6f 6b 65 6e    /* start token
2c6e2 69 7a 69 6e 67 20 61 74 20 74 68 65 20 62 65 67  izing at the beg
2c6e3 69 6e 6e 69 6e 67 20 2a 2f 0a 20 20 63 2d 3e 69  inning */.  c->i
2c6e4 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 63 2d 3e  Token = 0;.  c->
2c6e5 7a 54 6f 6b 65 6e 20 3d 20 4e 55 4c 4c 3b 20 20  zToken = NULL;  
2c6e6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c6e7 6e 6f 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74  no space allocat
2c6e8 65 64 2c 20 79 65 74 2e 20 2a 2f 0a 20 20 63 2d  ed, yet. */.  c-
2c6e9 3e 6e 41 6c 6c 6f 63 61 74 65 64 20 3d 20 30 3b  >nAllocated = 0;
2c6ea 0a 0a 20 20 2a 70 70 43 75 72 73 6f 72 20 3d 20  ..  *ppCursor = 
2c6eb 26 63 2d 3e 62 61 73 65 3b 0a 20 20 72 65 74 75  &c->base;.  retu
2c6ec 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
2c6ed 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 74  ./*.** Close a t
2c6ee 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73  okenization curs
2c6ef 6f 72 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70  or previously op
2c6f0 65 6e 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74  ened by a call t
2c6f1 6f 0a 2a 2a 20 70 6f 72 74 65 72 4f 70 65 6e 28  o.** porterOpen(
2c6f2 29 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73 74 61 74  ) above..*/.stat
2c6f3 69 63 20 69 6e 74 20 70 6f 72 74 65 72 43 6c 6f  ic int porterClo
2c6f4 73 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  se(sqlite3_token
2c6f5 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75  izer_cursor *pCu
2c6f6 72 73 6f 72 29 7b 0a 20 20 70 6f 72 74 65 72 5f  rsor){.  porter_
2c6f7 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
2c6f8 20 2a 63 20 3d 20 28 70 6f 72 74 65 72 5f 74 6f   *c = (porter_to
2c6f9 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
2c6fa 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 73 71 6c  ) pCursor;.  sql
2c6fb 69 74 65 33 5f 66 72 65 65 28 63 2d 3e 7a 54 6f  ite3_free(c->zTo
2c6fc 6b 65 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ken);.  sqlite3_
2c6fd 66 72 65 65 28 63 29 3b 0a 20 20 72 65 74 75 72  free(c);.  retur
2c6fe 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 2f  n SQLITE_OK;.}./
2c6ff 2a 0a 2a 2a 20 56 6f 77 65 6c 20 6f 72 20 63 6f  *.** Vowel or co
2c700 6e 73 6f 6e 61 6e 74 0a 2a 2f 0a 73 74 61 74 69  nsonant.*/.stati
2c701 63 20 63 6f 6e 73 74 20 63 68 61 72 20 63 54 79  c const char cTy
2c702 70 65 5b 5d 20 3d 20 7b 0a 20 20 20 30 2c 20 31  pe[] = {.   0, 1
2c703 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c  , 1, 1, 0, 1, 1,
2c704 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 0, 1, 1, 1, 
2c705 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31  1, 1, 0, 1, 1, 1
2c706 2c 20 31 2c 20 31 2c 20 30 2c 0a 20 20 20 31 2c  , 1, 1, 0,.   1,
2c707 20 31 2c 20 31 2c 20 32 2c 20 31 0a 7d 3b 0a 0a   1, 1, 2, 1.};..
2c708 2f 2a 0a 2a 2a 20 69 73 43 6f 6e 73 6f 6e 61 6e  /*.** isConsonan
2c709 74 28 29 20 61 6e 64 20 69 73 56 6f 77 65 6c 28  t() and isVowel(
2c70a 29 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74  ) determine if t
2c70b 68 65 69 72 20 66 69 72 73 74 20 63 68 61 72 61  heir first chara
2c70c 63 74 65 72 20 69 6e 0a 2a 2a 20 74 68 65 20 73  cter in.** the s
2c70d 74 72 69 6e 67 20 74 68 65 79 20 70 6f 69 6e 74  tring they point
2c70e 20 74 6f 20 69 73 20 61 20 63 6f 6e 73 6f 6e 61   to is a consona
2c70f 6e 74 20 6f 72 20 61 20 76 6f 77 65 6c 2c 20 61  nt or a vowel, a
2c710 63 63 6f 72 64 69 6e 67 0a 2a 2a 20 74 6f 20 50  ccording.** to P
2c711 6f 72 74 65 72 20 72 75 6c 73 2e 20 20 0a 2a 2a  orter ruls.  .**
2c712 0a 2a 2a 20 41 20 63 6f 6e 73 6f 6e 61 74 65 20  .** A consonate 
2c713 69 73 20 61 6e 79 20 6c 65 74 74 65 72 20 6f 74  is any letter ot
2c714 68 65 72 20 74 68 61 6e 20 27 61 27 2c 20 27 65  her than 'a', 'e
2c715 27 2c 20 27 69 27 2c 20 27 6f 27 2c 20 6f 72 20  ', 'i', 'o', or 
2c716 27 75 27 2e 0a 2a 2a 20 27 59 27 20 69 73 20 61  'u'..** 'Y' is a
2c717 20 63 6f 6e 73 6f 6e 61 6e 74 20 75 6e 6c 65 73   consonant unles
2c718 73 20 69 74 20 66 6f 6c 6c 6f 77 73 20 61 6e 6f  s it follows ano
2c719 74 68 65 72 20 63 6f 6e 73 6f 6e 61 6e 74 2c 0a  ther consonant,.
2c71a 2a 2a 20 69 6e 20 77 68 69 63 68 20 63 61 73 65  ** in which case
2c71b 20 69 74 20 69 73 20 61 20 76 6f 77 65 6c 2e 0a   it is a vowel..
2c71c 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 73 65 20 72  **.** In these r
2c71d 6f 75 74 69 6e 65 2c 20 74 68 65 20 6c 65 74 74  outine, the lett
2c71e 65 72 73 20 61 72 65 20 69 6e 20 72 65 76 65 72  ers are in rever
2c71f 73 65 20 6f 72 64 65 72 2e 20 20 53 6f 20 74 68  se order.  So th
2c720 65 20 27 79 27 20 72 75 6c 65 0a 2a 2a 20 69 73  e 'y' rule.** is
2c721 20 74 68 61 74 20 27 79 27 20 69 73 20 61 20 63   that 'y' is a c
2c722 6f 6e 73 6f 6e 61 6e 74 20 75 6e 6c 65 73 73 20  onsonant unless 
2c723 69 74 20 69 73 20 66 6f 6c 6c 6f 77 65 64 20 62  it is followed b
2c724 79 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 63 6f 6e  y another.** con
2c725 73 6f 6e 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69  sonent..*/.stati
2c726 63 20 69 6e 74 20 69 73 56 6f 77 65 6c 28 63 6f  c int isVowel(co
2c727 6e 73 74 20 63 68 61 72 2a 29 3b 0a 73 74 61 74  nst char*);.stat
2c728 69 63 20 69 6e 74 20 69 73 43 6f 6e 73 6f 6e 61  ic int isConsona
2c729 6e 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  nt(const char *z
2c72a 29 7b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 63 68  ){.  int j;.  ch
2c72b 61 72 20 78 20 3d 20 2a 7a 3b 0a 20 20 69 66 28  ar x = *z;.  if(
2c72c 20 78 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   x==0 ) return 0
2c72d 3b 0a 20 20 61 73 73 65 72 74 28 20 78 3e 3d 27  ;.  assert( x>='
2c72e 61 27 20 26 26 20 78 3c 3d 27 7a 27 20 29 3b 0a  a' && x<='z' );.
2c72f 20 20 6a 20 3d 20 63 54 79 70 65 5b 78 2d 27 61    j = cType[x-'a
2c730 27 5d 3b 0a 20 20 69 66 28 20 6a 3c 32 20 29 20  '];.  if( j<2 ) 
2c731 72 65 74 75 72 6e 20 6a 3b 0a 20 20 72 65 74 75  return j;.  retu
2c732 72 6e 20 7a 5b 31 5d 3d 3d 30 20 7c 7c 20 69 73  rn z[1]==0 || is
2c733 56 6f 77 65 6c 28 7a 20 2b 20 31 29 3b 0a 7d 0a  Vowel(z + 1);.}.
2c734 73 74 61 74 69 63 20 69 6e 74 20 69 73 56 6f 77  static int isVow
2c735 65 6c 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  el(const char *z
2c736 29 7b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 63 68  ){.  int j;.  ch
2c737 61 72 20 78 20 3d 20 2a 7a 3b 0a 20 20 69 66 28  ar x = *z;.  if(
2c738 20 78 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   x==0 ) return 0
2c739 3b 0a 20 20 61 73 73 65 72 74 28 20 78 3e 3d 27  ;.  assert( x>='
2c73a 61 27 20 26 26 20 78 3c 3d 27 7a 27 20 29 3b 0a  a' && x<='z' );.
2c73b 20 20 6a 20 3d 20 63 54 79 70 65 5b 78 2d 27 61    j = cType[x-'a
2c73c 27 5d 3b 0a 20 20 69 66 28 20 6a 3c 32 20 29 20  '];.  if( j<2 ) 
2c73d 72 65 74 75 72 6e 20 31 2d 6a 3b 0a 20 20 72 65  return 1-j;.  re
2c73e 74 75 72 6e 20 69 73 43 6f 6e 73 6f 6e 61 6e 74  turn isConsonant
2c73f 28 7a 20 2b 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  (z + 1);.}../*.*
2c740 2a 20 4c 65 74 20 61 6e 79 20 73 65 71 75 65 6e  * Let any sequen
2c741 63 65 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72  ce of one or mor
2c742 65 20 76 6f 77 65 6c 73 20 62 65 20 72 65 70 72  e vowels be repr
2c743 65 73 65 6e 74 65 64 20 62 79 20 56 20 61 6e 64  esented by V and
2c744 20 6c 65 74 0a 2a 2a 20 43 20 62 65 20 73 65 71   let.** C be seq
2c745 75 65 6e 63 65 20 6f 66 20 6f 6e 65 20 6f 72 20  uence of one or 
2c746 6d 6f 72 65 20 63 6f 6e 73 6f 6e 61 6e 74 73 2e  more consonants.
2c747 20 20 54 68 65 6e 20 65 76 65 72 79 20 77 6f 72    Then every wor
2c748 64 20 63 61 6e 20 62 65 0a 2a 2a 20 72 65 70 72  d can be.** repr
2c749 65 73 65 6e 74 65 64 20 61 73 3a 0a 2a 2a 0a 2a  esented as:.**.*
2c74a 2a 20 20 20 20 20 20 20 20 20 20 20 5b 43 5d 20  *           [C] 
2c74b 28 56 43 29 7b 6d 7d 20 5b 56 5d 0a 2a 2a 0a 2a  (VC){m} [V].**.*
2c74c 2a 20 49 6e 20 70 72 6f 73 65 3a 20 20 41 20 77  * In prose:  A w
2c74d 6f 72 64 20 69 73 20 61 6e 20 6f 70 74 69 6f 6e  ord is an option
2c74e 61 6c 20 63 6f 6e 73 6f 6e 61 6e 74 20 66 6f 6c  al consonant fol
2c74f 6c 6f 77 65 64 20 62 79 20 7a 65 72 6f 20 6f 72  lowed by zero or
2c750 0a 2a 2a 20 76 6f 77 65 6c 2d 63 6f 6e 73 6f 6e  .** vowel-conson
2c751 61 6e 74 20 70 61 69 72 73 20 66 6f 6c 6c 6f 77  ant pairs follow
2c752 65 64 20 62 79 20 61 6e 20 6f 70 74 69 6f 6e 61  ed by an optiona
2c753 6c 20 76 6f 77 65 6c 2e 20 20 22 6d 22 20 69 73  l vowel.  "m" is
2c754 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f   the.** number o
2c755 66 20 76 6f 77 65 6c 20 63 6f 6e 73 6f 6e 61 6e  f vowel consonan
2c756 74 20 70 61 69 72 73 2e 20 20 54 68 69 73 20 72  t pairs.  This r
2c757 6f 75 74 69 6e 65 20 63 6f 6d 70 75 74 65 73 20  outine computes 
2c758 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20  the value.** of 
2c759 6d 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20  m for the first 
2c75a 69 20 62 79 74 65 73 20 6f 66 20 61 20 77 6f 72  i bytes of a wor
2c75b 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  d..**.** Return 
2c75c 74 72 75 65 20 69 66 20 74 68 65 20 6d 2d 76 61  true if the m-va
2c75d 6c 75 65 20 66 6f 72 20 7a 20 69 73 20 31 20 6f  lue for z is 1 o
2c75e 72 20 6d 6f 72 65 2e 20 20 49 6e 20 6f 74 68 65  r more.  In othe
2c75f 72 20 77 6f 72 64 73 2c 0a 2a 2a 20 72 65 74 75  r words,.** retu
2c760 72 6e 20 74 72 75 65 20 69 66 20 7a 20 63 6f 6e  rn true if z con
2c761 74 61 69 6e 73 20 61 74 20 6c 65 61 73 74 20 6f  tains at least o
2c762 6e 65 20 76 6f 77 65 6c 20 74 68 61 74 20 69 73  ne vowel that is
2c763 20 66 6f 6c 6c 6f 77 65 64 0a 2a 2a 20 62 79 20   followed.** by 
2c764 61 20 63 6f 6e 73 6f 6e 61 6e 74 2e 0a 2a 2a 0a  a consonant..**.
2c765 2a 2a 20 49 6e 20 74 68 69 73 20 72 6f 75 74 69  ** In this routi
2c766 6e 65 20 7a 5b 5d 20 69 73 20 69 6e 20 72 65 76  ne z[] is in rev
2c767 65 72 73 65 20 6f 72 64 65 72 2e 20 20 53 6f 20  erse order.  So 
2c768 77 65 20 61 72 65 20 72 65 61 6c 6c 79 20 6c 6f  we are really lo
2c769 6f 6b 69 6e 67 0a 2a 2a 20 66 6f 72 20 61 6e 20  oking.** for an 
2c76a 69 6e 73 74 61 6e 63 65 20 6f 66 20 6f 66 20 61  instance of of a
2c76b 20 63 6f 6e 73 6f 6e 61 6e 74 20 66 6f 6c 6c 6f   consonant follo
2c76c 77 65 64 20 62 79 20 61 20 76 6f 77 65 6c 2e 0a  wed by a vowel..
2c76d 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 5f  */.static int m_
2c76e 67 74 5f 30 28 63 6f 6e 73 74 20 63 68 61 72 20  gt_0(const char 
2c76f 2a 7a 29 7b 0a 20 20 77 68 69 6c 65 28 20 69 73  *z){.  while( is
2c770 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b  Vowel(z) ){ z++;
2c771 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29   }.  if( *z==0 )
2c772 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69   return 0;.  whi
2c773 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28  le( isConsonant(
2c774 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 72  z) ){ z++; }.  r
2c775 65 74 75 72 6e 20 2a 7a 21 3d 30 3b 0a 7d 0a 0a  eturn *z!=0;.}..
2c776 2f 2a 20 4c 69 6b 65 20 6d 67 74 30 20 61 62 6f  /* Like mgt0 abo
2c777 76 65 20 65 78 63 65 70 74 20 77 65 20 61 72 65  ve except we are
2c778 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 76   looking for a v
2c779 61 6c 75 65 20 6f 66 20 6d 20 77 68 69 63 68 20  alue of m which 
2c77a 69 73 0a 2a 2a 20 65 78 61 63 74 6c 79 20 31 0a  is.** exactly 1.
2c77b 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 5f  */.static int m_
2c77c 65 71 5f 31 28 63 6f 6e 73 74 20 63 68 61 72 20  eq_1(const char 
2c77d 2a 7a 29 7b 0a 20 20 77 68 69 6c 65 28 20 69 73  *z){.  while( is
2c77e 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b  Vowel(z) ){ z++;
2c77f 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29   }.  if( *z==0 )
2c780 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69   return 0;.  whi
2c781 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28  le( isConsonant(
2c782 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69  z) ){ z++; }.  i
2c783 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72  f( *z==0 ) retur
2c784 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73  n 0;.  while( is
2c785 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b  Vowel(z) ){ z++;
2c786 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29   }.  if( *z==0 )
2c787 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 77 68 69   return 1;.  whi
2c788 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28  le( isConsonant(
2c789 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 72  z) ){ z++; }.  r
2c78a 65 74 75 72 6e 20 2a 7a 3d 3d 30 3b 0a 7d 0a 0a  eturn *z==0;.}..
2c78b 2f 2a 20 4c 69 6b 65 20 6d 67 74 30 20 61 62 6f  /* Like mgt0 abo
2c78c 76 65 20 65 78 63 65 70 74 20 77 65 20 61 72 65  ve except we are
2c78d 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 76   looking for a v
2c78e 61 6c 75 65 20 6f 66 20 6d 3e 31 20 69 6e 73 74  alue of m>1 inst
2c78f 65 61 64 0a 2a 2a 20 6f 72 20 6d 3e 30 0a 2a 2f  ead.** or m>0.*/
2c790 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 5f 67 74  .static int m_gt
2c791 5f 31 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  _1(const char *z
2c792 29 7b 0a 20 20 77 68 69 6c 65 28 20 69 73 56 6f  ){.  while( isVo
2c793 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d  wel(z) ){ z++; }
2c794 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72  .  if( *z==0 ) r
2c795 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65  eturn 0;.  while
2c796 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29  ( isConsonant(z)
2c797 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28   ){ z++; }.  if(
2c798 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20   *z==0 ) return 
2c799 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 56 6f  0;.  while( isVo
2c79a 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d  wel(z) ){ z++; }
2c79b 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72  .  if( *z==0 ) r
2c79c 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65  eturn 0;.  while
2c79d 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29  ( isConsonant(z)
2c79e 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 72 65 74   ){ z++; }.  ret
2c79f 75 72 6e 20 2a 7a 21 3d 30 3b 0a 7d 0a 0a 2f 2a  urn *z!=0;.}../*
2c7a0 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20  .** Return TRUE 
2c7a1 69 66 20 74 68 65 72 65 20 69 73 20 61 20 76 6f  if there is a vo
2c7a2 77 65 6c 20 61 6e 79 77 68 65 72 65 20 77 69 74  wel anywhere wit
2c7a3 68 69 6e 20 7a 5b 30 2e 2e 6e 2d 31 5d 0a 2a 2f  hin z[0..n-1].*/
2c7a4 0a 73 74 61 74 69 63 20 69 6e 74 20 68 61 73 56  .static int hasV
2c7a5 6f 77 65 6c 28 63 6f 6e 73 74 20 63 68 61 72 20  owel(const char 
2c7a6 2a 7a 29 7b 0a 20 20 77 68 69 6c 65 28 20 69 73  *z){.  while( is
2c7a7 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20  Consonant(z) ){ 
2c7a8 7a 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20  z++; }.  return 
2c7a9 2a 7a 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  *z!=0;.}../*.** 
2c7aa 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74  Return TRUE if t
2c7ab 68 65 20 77 6f 72 64 20 65 6e 64 73 20 69 6e 20  he word ends in 
2c7ac 61 20 64 6f 75 62 6c 65 20 63 6f 6e 73 6f 6e 61  a double consona
2c7ad 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 65  nt..**.** The te
2c7ae 78 74 20 69 73 20 72 65 76 65 72 73 65 64 20 68  xt is reversed h
2c7af 65 72 65 2e 20 53 6f 20 77 65 20 61 72 65 20 72  ere. So we are r
2c7b0 65 61 6c 6c 79 20 6c 6f 6f 6b 69 6e 67 20 61 74  eally looking at
2c7b1 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 77  .** the first tw
2c7b2 6f 20 63 68 61 72 61 63 74 65 72 73 20 6f 66 20  o characters of 
2c7b3 7a 5b 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  z[]..*/.static i
2c7b4 6e 74 20 64 6f 75 62 6c 65 43 6f 6e 73 6f 6e 61  nt doubleConsona
2c7b5 6e 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  nt(const char *z
2c7b6 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 73 43 6f  ){.  return isCo
2c7b7 6e 73 6f 6e 61 6e 74 28 7a 29 20 26 26 20 7a 5b  nsonant(z) && z[
2c7b8 30 5d 3d 3d 7a 5b 31 5d 20 26 26 20 69 73 43 6f  0]==z[1] && isCo
2c7b9 6e 73 6f 6e 61 6e 74 28 7a 2b 31 29 3b 0a 7d 0a  nsonant(z+1);.}.
2c7ba 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
2c7bb 55 45 20 69 66 20 74 68 65 20 77 6f 72 64 20 65  UE if the word e
2c7bc 6e 64 73 20 77 69 74 68 20 74 68 72 65 65 20 6c  nds with three l
2c7bd 65 74 74 65 72 73 20 77 68 69 63 68 0a 2a 2a 20  etters which.** 
2c7be 61 72 65 20 63 6f 6e 73 6f 6e 61 6e 74 2d 76 6f  are consonant-vo
2c7bf 77 65 6c 2d 63 6f 6e 73 6f 6e 65 6e 74 20 61 6e  wel-consonent an
2c7c0 64 20 77 68 65 72 65 20 74 68 65 20 66 69 6e 61  d where the fina
2c7c1 6c 20 63 6f 6e 73 6f 6e 61 6e 74 0a 2a 2a 20 69  l consonant.** i
2c7c2 73 20 6e 6f 74 20 27 77 27 2c 20 27 78 27 2c 20  s not 'w', 'x', 
2c7c3 6f 72 20 27 79 27 2e 0a 2a 2a 0a 2a 2a 20 54 68  or 'y'..**.** Th
2c7c4 65 20 77 6f 72 64 20 69 73 20 72 65 76 65 72 73  e word is revers
2c7c5 65 64 20 68 65 72 65 2e 20 20 53 6f 20 77 65 20  ed here.  So we 
2c7c6 61 72 65 20 72 65 61 6c 6c 79 20 63 68 65 63 6b  are really check
2c7c7 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 72 73 74  ing the.** first
2c7c8 20 74 68 72 65 65 20 6c 65 74 74 65 72 73 20 61   three letters a
2c7c9 6e 64 20 74 68 65 20 66 69 72 73 74 20 6f 6e 65  nd the first one
2c7ca 20 63 61 6e 6e 6f 74 20 62 65 20 69 6e 20 5b 77   cannot be in [w
2c7cb 78 79 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  xy]..*/.static i
2c7cc 6e 74 20 73 74 61 72 5f 6f 68 28 63 6f 6e 73 74  nt star_oh(const
2c7cd 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 72 65 74   char *z){.  ret
2c7ce 75 72 6e 0a 20 20 20 20 7a 5b 30 5d 21 3d 30 20  urn.    z[0]!=0 
2c7cf 26 26 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a  && isConsonant(z
2c7d0 29 20 26 26 0a 20 20 20 20 7a 5b 30 5d 21 3d 27  ) &&.    z[0]!='
2c7d1 77 27 20 26 26 20 7a 5b 30 5d 21 3d 27 78 27 20  w' && z[0]!='x' 
2c7d2 26 26 20 7a 5b 30 5d 21 3d 27 79 27 20 26 26 0a  && z[0]!='y' &&.
2c7d3 20 20 20 20 7a 5b 31 5d 21 3d 30 20 26 26 20 69      z[1]!=0 && i
2c7d4 73 56 6f 77 65 6c 28 7a 2b 31 29 20 26 26 0a 20  sVowel(z+1) &&. 
2c7d5 20 20 20 7a 5b 32 5d 21 3d 30 20 26 26 20 69 73     z[2]!=0 && is
2c7d6 43 6f 6e 73 6f 6e 61 6e 74 28 7a 2b 32 29 3b 0a  Consonant(z+2);.
2c7d7 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  }../*.** If the 
2c7d8 77 6f 72 64 20 65 6e 64 73 20 77 69 74 68 20 7a  word ends with z
2c7d9 46 72 6f 6d 20 61 6e 64 20 78 43 6f 6e 64 28 29  From and xCond()
2c7da 20 69 73 20 74 72 75 65 20 66 6f 72 20 74 68 65   is true for the
2c7db 20 73 74 65 6d 0a 2a 2a 20 6f 66 20 74 68 65 20   stem.** of the 
2c7dc 77 6f 72 64 20 74 68 61 74 20 70 72 65 63 65 65  word that precee
2c7dd 64 73 20 74 68 65 20 7a 46 72 6f 6d 20 65 6e 64  ds the zFrom end
2c7de 69 6e 67 2c 20 74 68 65 6e 20 63 68 61 6e 67 65  ing, then change
2c7df 20 74 68 65 20 0a 2a 2a 20 65 6e 64 69 6e 67 20   the .** ending 
2c7e0 74 6f 20 7a 54 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68  to zTo..**.** Th
2c7e1 65 20 69 6e 70 75 74 20 77 6f 72 64 20 2a 70 7a  e input word *pz
2c7e2 20 61 6e 64 20 7a 46 72 6f 6d 20 61 72 65 20 62   and zFrom are b
2c7e3 6f 74 68 20 69 6e 20 72 65 76 65 72 73 65 20 6f  oth in reverse o
2c7e4 72 64 65 72 2e 20 20 7a 54 6f 0a 2a 2a 20 69 73  rder.  zTo.** is
2c7e5 20 69 6e 20 6e 6f 72 6d 61 6c 20 6f 72 64 65 72   in normal order
2c7e6 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  . .**.** Return 
2c7e7 54 52 55 45 20 69 66 20 7a 46 72 6f 6d 20 6d 61  TRUE if zFrom ma
2c7e8 74 63 68 65 73 2e 20 20 52 65 74 75 72 6e 20 46  tches.  Return F
2c7e9 41 4c 53 45 20 69 66 20 7a 46 72 6f 6d 20 64 6f  ALSE if zFrom do
2c7ea 65 73 20 6e 6f 74 0a 2a 2a 20 6d 61 74 63 68 2e  es not.** match.
2c7eb 20 20 4e 6f 74 20 74 68 61 74 20 54 52 55 45 20    Not that TRUE 
2c7ec 69 73 20 72 65 74 75 72 6e 65 64 20 65 76 65 6e  is returned even
2c7ed 20 69 66 20 78 43 6f 6e 64 28 29 20 66 61 69 6c   if xCond() fail
2c7ee 73 20 61 6e 64 0a 2a 2a 20 6e 6f 20 73 75 62 73  s and.** no subs
2c7ef 74 69 74 75 74 69 6f 6e 20 6f 63 63 75 72 73 2e  titution occurs.
2c7f0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
2c7f1 74 65 6d 28 0a 20 20 63 68 61 72 20 2a 2a 70 7a  tem(.  char **pz
2c7f2 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
2c7f3 20 54 68 65 20 77 6f 72 64 20 62 65 69 6e 67 20   The word being 
2c7f4 73 74 65 6d 6d 65 64 20 28 52 65 76 65 72 73 65  stemmed (Reverse
2c7f5 64 29 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  d) */.  const ch
2c7f6 61 72 20 2a 7a 46 72 6f 6d 2c 20 20 20 20 20 2f  ar *zFrom,     /
2c7f7 2a 20 49 66 20 74 68 65 20 65 6e 64 69 6e 67 20  * If the ending 
2c7f8 6d 61 74 63 68 65 73 20 74 68 69 73 2e 2e 2e 20  matches this... 
2c7f9 28 52 65 76 65 72 73 65 64 29 20 2a 2f 0a 20 20  (Reversed) */.  
2c7fa 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f 2c  const char *zTo,
2c7fb 20 20 20 20 20 20 20 2f 2a 20 2e 2e 2e 20 63 68         /* ... ch
2c7fc 61 6e 67 65 20 74 68 65 20 65 6e 64 69 6e 67 20  ange the ending 
2c7fd 74 6f 20 74 68 69 73 20 28 6e 6f 74 20 72 65 76  to this (not rev
2c7fe 65 72 73 65 64 29 20 2a 2f 0a 20 20 69 6e 74 20  ersed) */.  int 
2c7ff 28 2a 78 43 6f 6e 64 29 28 63 6f 6e 73 74 20 63  (*xCond)(const c
2c800 68 61 72 2a 29 20 20 20 2f 2a 20 43 6f 6e 64 69  har*)   /* Condi
2c801 74 69 6f 6e 20 74 68 61 74 20 6d 75 73 74 20 62  tion that must b
2c802 65 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 63  e true */.){.  c
2c803 68 61 72 20 2a 7a 20 3d 20 2a 70 7a 3b 0a 20 20  har *z = *pz;.  
2c804 77 68 69 6c 65 28 20 2a 7a 46 72 6f 6d 20 26 26  while( *zFrom &&
2c805 20 2a 7a 46 72 6f 6d 3d 3d 2a 7a 20 29 7b 20 7a   *zFrom==*z ){ z
2c806 2b 2b 3b 20 7a 46 72 6f 6d 2b 2b 3b 20 7d 0a 20  ++; zFrom++; }. 
2c807 20 69 66 28 20 2a 7a 46 72 6f 6d 21 3d 30 20 29   if( *zFrom!=0 )
2c808 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28   return 0;.  if(
2c809 20 78 43 6f 6e 64 20 26 26 20 21 78 43 6f 6e 64   xCond && !xCond
2c80a 28 7a 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  (z) ) return 1;.
2c80b 20 20 77 68 69 6c 65 28 20 2a 7a 54 6f 20 29 7b    while( *zTo ){
2c80c 0a 20 20 20 20 2a 28 2d 2d 7a 29 20 3d 20 2a 28  .    *(--z) = *(
2c80d 7a 54 6f 2b 2b 29 3b 0a 20 20 7d 0a 20 20 2a 70  zTo++);.  }.  *p
2c80e 7a 20 3d 20 7a 3b 0a 20 20 72 65 74 75 72 6e 20  z = z;.  return 
2c80f 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  1;.}../*.** This
2c810 20 69 73 20 74 68 65 20 66 61 6c 6c 62 61 63 6b   is the fallback
2c811 20 73 74 65 6d 6d 65 72 20 75 73 65 64 20 77 68   stemmer used wh
2c812 65 6e 20 74 68 65 20 70 6f 72 74 65 72 20 73 74  en the porter st
2c813 65 6d 6d 65 72 20 69 73 0a 2a 2a 20 69 6e 61 70  emmer is.** inap
2c814 70 72 6f 70 72 69 61 74 65 2e 20 20 54 68 65 20  propriate.  The 
2c815 69 6e 70 75 74 20 77 6f 72 64 20 69 73 20 63 6f  input word is co
2c816 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 6f 75  pied into the ou
2c817 74 70 75 74 20 77 69 74 68 0a 2a 2a 20 55 53 2d  tput with.** US-
2c818 41 53 43 49 49 20 63 61 73 65 20 66 6f 6c 64 69  ASCII case foldi
2c819 6e 67 2e 20 20 49 66 20 74 68 65 20 69 6e 70 75  ng.  If the inpu
2c81a 74 20 77 6f 72 64 20 69 73 20 74 6f 6f 20 6c 6f  t word is too lo
2c81b 6e 67 20 28 6d 6f 72 65 0a 2a 2a 20 74 68 61 6e  ng (more.** than
2c81c 20 32 30 20 62 79 74 65 73 20 69 66 20 69 74 20   20 bytes if it 
2c81d 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 64 69 67 69  contains no digi
2c81e 74 73 20 6f 72 20 6d 6f 72 65 20 74 68 61 6e 20  ts or more than 
2c81f 36 20 62 79 74 65 73 20 69 66 0a 2a 2a 20 69 74  6 bytes if.** it
2c820 20 63 6f 6e 74 61 69 6e 73 20 64 69 67 69 74 73   contains digits
2c821 29 20 74 68 65 6e 20 77 6f 72 64 20 69 73 20 74  ) then word is t
2c822 72 75 6e 63 61 74 65 64 20 74 6f 20 32 30 20 6f  runcated to 20 o
2c823 72 20 36 20 62 79 74 65 73 0a 2a 2a 20 62 79 20  r 6 bytes.** by 
2c824 74 61 6b 69 6e 67 20 31 30 20 6f 72 20 33 20 62  taking 10 or 3 b
2c825 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 62 65  ytes from the be
2c826 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 2e  ginning and end.
2c827 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2c828 63 6f 70 79 5f 73 74 65 6d 6d 65 72 28 63 6f 6e  copy_stemmer(con
2c829 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c 20 69 6e  st char *zIn, in
2c82a 74 20 6e 49 6e 2c 20 63 68 61 72 20 2a 7a 4f 75  t nIn, char *zOu
2c82b 74 2c 20 69 6e 74 20 2a 70 6e 4f 75 74 29 7b 0a  t, int *pnOut){.
2c82c 20 20 69 6e 74 20 69 2c 20 6d 78 2c 20 6a 3b 0a    int i, mx, j;.
2c82d 20 20 69 6e 74 20 68 61 73 44 69 67 69 74 20 3d    int hasDigit =
2c82e 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   0;.  for(i=0; i
2c82f 3c 6e 49 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  <nIn; i++){.    
2c830 69 6e 74 20 63 20 3d 20 7a 49 6e 5b 69 5d 3b 0a  int c = zIn[i];.
2c831 20 20 20 20 69 66 28 20 63 3e 3d 27 41 27 20 26      if( c>='A' &
2c832 26 20 63 3c 3d 27 5a 27 20 29 7b 0a 20 20 20 20  & c<='Z' ){.    
2c833 20 20 7a 4f 75 74 5b 69 5d 20 3d 20 63 20 2d 20    zOut[i] = c - 
2c834 27 41 27 20 2b 20 27 61 27 3b 0a 20 20 20 20 7d  'A' + 'a';.    }
2c835 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20  else{.      if( 
2c836 63 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27  c>='0' && c<='9'
2c837 20 29 20 68 61 73 44 69 67 69 74 20 3d 20 31 3b   ) hasDigit = 1;
2c838 0a 20 20 20 20 20 20 7a 4f 75 74 5b 69 5d 20 3d  .      zOut[i] =
2c839 20 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   c;.    }.  }.  
2c83a 6d 78 20 3d 20 68 61 73 44 69 67 69 74 20 3f 20  mx = hasDigit ? 
2c83b 33 20 3a 20 31 30 3b 0a 20 20 69 66 28 20 6e 49  3 : 10;.  if( nI
2c83c 6e 3e 6d 78 2a 32 20 29 7b 0a 20 20 20 20 66 6f  n>mx*2 ){.    fo
2c83d 72 28 6a 3d 6d 78 2c 20 69 3d 6e 49 6e 2d 6d 78  r(j=mx, i=nIn-mx
2c83e 3b 20 69 3c 6e 49 6e 3b 20 69 2b 2b 2c 20 6a 2b  ; i<nIn; i++, j+
2c83f 2b 29 7b 0a 20 20 20 20 20 20 7a 4f 75 74 5b 6a  +){.      zOut[j
2c840 5d 20 3d 20 7a 4f 75 74 5b 69 5d 3b 0a 20 20 20  ] = zOut[i];.   
2c841 20 7d 0a 20 20 20 20 69 20 3d 20 6a 3b 0a 20 20   }.    i = j;.  
2c842 7d 0a 20 20 7a 4f 75 74 5b 69 5d 20 3d 20 30 3b  }.  zOut[i] = 0;
2c843 0a 20 20 2a 70 6e 4f 75 74 20 3d 20 69 3b 0a 7d  .  *pnOut = i;.}
2c844 0a 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 6d 20 74 68  .../*.** Stem th
2c845 65 20 69 6e 70 75 74 20 77 6f 72 64 20 7a 49 6e  e input word zIn
2c846 5b 30 2e 2e 6e 49 6e 2d 31 5d 2e 20 20 53 74 6f  [0..nIn-1].  Sto
2c847 72 65 20 74 68 65 20 6f 75 74 70 75 74 20 69 6e  re the output in
2c848 20 7a 4f 75 74 2e 0a 2a 2a 20 7a 4f 75 74 20 69   zOut..** zOut i
2c849 73 20 61 74 20 6c 65 61 73 74 20 62 69 67 20 65  s at least big e
2c84a 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 6e 49  nough to hold nI
2c84b 6e 20 62 79 74 65 73 2e 20 20 57 72 69 74 65 20  n bytes.  Write 
2c84c 74 68 65 20 61 63 74 75 61 6c 0a 2a 2a 20 73 69  the actual.** si
2c84d 7a 65 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74  ze of the output
2c84e 20 77 6f 72 64 20 28 65 78 63 6c 75 73 69 76 65   word (exclusive
2c84f 20 6f 66 20 74 68 65 20 27 5c 30 27 20 74 65 72   of the '\0' ter
2c850 6d 69 6e 61 74 6f 72 29 20 69 6e 74 6f 20 2a 70  minator) into *p
2c851 6e 4f 75 74 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20  nOut..**.** Any 
2c852 75 70 70 65 72 2d 63 61 73 65 20 63 68 61 72 61  upper-case chara
2c853 63 74 65 72 73 20 69 6e 20 74 68 65 20 55 53 2d  cters in the US-
2c854 41 53 43 49 49 20 63 68 61 72 61 63 74 65 72 20  ASCII character 
2c855 73 65 74 20 28 5b 41 2d 5a 5d 29 0a 2a 2a 20 61  set ([A-Z]).** a
2c856 72 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20  re converted to 
2c857 6c 6f 77 65 72 20 63 61 73 65 2e 20 20 55 70 70  lower case.  Upp
2c858 65 72 2d 63 61 73 65 20 55 54 46 20 63 68 61 72  er-case UTF char
2c859 61 63 74 65 72 73 20 61 72 65 0a 2a 2a 20 75 6e  acters are.** un
2c85a 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 57  changed..**.** W
2c85b 6f 72 64 73 20 74 68 61 74 20 61 72 65 20 6c 6f  ords that are lo
2c85c 6e 67 65 72 20 74 68 61 6e 20 61 62 6f 75 74 20  nger than about 
2c85d 32 30 20 62 79 74 65 73 20 61 72 65 20 73 74 65  20 bytes are ste
2c85e 6d 6d 65 64 20 62 79 20 72 65 74 61 69 6e 69 6e  mmed by retainin
2c85f 67 0a 2a 2a 20 61 20 66 65 77 20 62 79 74 65 73  g.** a few bytes
2c860 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e   from the beginn
2c861 69 6e 67 20 61 6e 64 20 74 68 65 20 65 6e 64 20  ing and the end 
2c862 6f 66 20 74 68 65 20 77 6f 72 64 2e 20 20 49 66  of the word.  If
2c863 20 74 68 65 0a 2a 2a 20 77 6f 72 64 20 63 6f 6e   the.** word con
2c864 74 61 69 6e 73 20 64 69 67 69 74 73 2c 20 33 20  tains digits, 3 
2c865 62 79 74 65 73 20 61 72 65 20 74 61 6b 65 6e 20  bytes are taken 
2c866 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69  from the beginni
2c867 6e 67 20 61 6e 64 0a 2a 2a 20 33 20 62 79 74 65  ng and.** 3 byte
2c868 73 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 2e 20  s from the end. 
2c869 20 46 6f 72 20 6c 6f 6e 67 20 77 6f 72 64 73 20   For long words 
2c86a 77 69 74 68 6f 75 74 20 64 69 67 69 74 73 2c 20  without digits, 
2c86b 31 30 20 62 79 74 65 73 0a 2a 2a 20 61 72 65 20  10 bytes.** are 
2c86c 74 61 6b 65 6e 20 66 72 6f 6d 20 65 61 63 68 20  taken from each 
2c86d 65 6e 64 2e 20 20 55 53 2d 41 53 43 49 49 20 63  end.  US-ASCII c
2c86e 61 73 65 20 66 6f 6c 64 69 6e 67 20 73 74 69 6c  ase folding stil
2c86f 6c 20 61 70 70 6c 69 65 73 2e 0a 2a 2a 20 0a 2a  l applies..** .*
2c870 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 77  * If the input w
2c871 6f 72 64 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 74  ord contains not
2c872 20 64 69 67 69 74 73 20 62 75 74 20 64 6f 65 73   digits but does
2c873 20 63 68 61 72 61 63 74 65 72 73 20 6e 6f 74 20   characters not 
2c874 0a 2a 2a 20 69 6e 20 5b 61 2d 7a 41 2d 5a 5d 20  .** in [a-zA-Z] 
2c875 74 68 65 6e 20 6e 6f 20 73 74 65 6d 6d 69 6e 67  then no stemming
2c876 20 69 73 20 61 74 74 65 6d 70 74 65 64 20 61 6e   is attempted an
2c877 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6a  d this routine j
2c878 75 73 74 20 0a 2a 2a 20 63 6f 70 69 65 73 20 74  ust .** copies t
2c879 68 65 20 69 6e 70 75 74 20 69 6e 74 6f 20 74 68  he input into th
2c87a 65 20 69 6e 70 75 74 20 69 6e 74 6f 20 74 68 65  e input into the
2c87b 20 6f 75 74 70 75 74 20 77 69 74 68 20 55 53 2d   output with US-
2c87c 41 53 43 49 49 0a 2a 2a 20 63 61 73 65 20 66 6f  ASCII.** case fo
2c87d 6c 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 74 65  lding..**.** Ste
2c87e 6d 6d 69 6e 67 20 6e 65 76 65 72 20 69 6e 63 72  mming never incr
2c87f 65 61 73 65 73 20 74 68 65 20 6c 65 6e 67 74 68  eases the length
2c880 20 6f 66 20 74 68 65 20 77 6f 72 64 2e 20 20 53   of the word.  S
2c881 6f 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f  o there is.** no
2c882 20 63 68 61 6e 63 65 20 6f 66 20 6f 76 65 72 66   chance of overf
2c883 6c 6f 77 69 6e 67 20 74 68 65 20 7a 4f 75 74 20  lowing the zOut 
2c884 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  buffer..*/.stati
2c885 63 20 76 6f 69 64 20 70 6f 72 74 65 72 5f 73 74  c void porter_st
2c886 65 6d 6d 65 72 28 63 6f 6e 73 74 20 63 68 61 72  emmer(const char
2c887 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 49 6e 2c 20   *zIn, int nIn, 
2c888 63 68 61 72 20 2a 7a 4f 75 74 2c 20 69 6e 74 20  char *zOut, int 
2c889 2a 70 6e 4f 75 74 29 7b 0a 20 20 69 6e 74 20 69  *pnOut){.  int i
2c88a 2c 20 6a 2c 20 63 3b 0a 20 20 63 68 61 72 20 7a  , j, c;.  char z
2c88b 52 65 76 65 72 73 65 5b 32 38 5d 3b 0a 20 20 63  Reverse[28];.  c
2c88c 68 61 72 20 2a 7a 2c 20 2a 7a 32 3b 0a 20 20 69  har *z, *z2;.  i
2c88d 66 28 20 6e 49 6e 3c 33 20 7c 7c 20 6e 49 6e 3e  f( nIn<3 || nIn>
2c88e 3d 73 69 7a 65 6f 66 28 7a 52 65 76 65 72 73 65  =sizeof(zReverse
2c88f 29 2d 37 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  )-7 ){.    /* Th
2c890 65 20 77 6f 72 64 20 69 73 20 74 6f 6f 20 62 69  e word is too bi
2c891 67 20 6f 72 20 74 6f 6f 20 73 6d 61 6c 6c 20 66  g or too small f
2c892 6f 72 20 74 68 65 20 70 6f 72 74 65 72 20 73 74  or the porter st
2c893 65 6d 6d 65 72 2e 0a 20 20 20 20 2a 2a 20 46 61  emmer..    ** Fa
2c894 6c 6c 62 61 63 6b 20 74 6f 20 74 68 65 20 63 6f  llback to the co
2c895 70 79 20 73 74 65 6d 6d 65 72 20 2a 2f 0a 20 20  py stemmer */.  
2c896 20 20 63 6f 70 79 5f 73 74 65 6d 6d 65 72 28 7a    copy_stemmer(z
2c897 49 6e 2c 20 6e 49 6e 2c 20 7a 4f 75 74 2c 20 70  In, nIn, zOut, p
2c898 6e 4f 75 74 29 3b 0a 20 20 20 20 72 65 74 75 72  nOut);.    retur
2c899 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  n;.  }.  for(i=0
2c89a 2c 20 6a 3d 73 69 7a 65 6f 66 28 7a 52 65 76 65  , j=sizeof(zReve
2c89b 72 73 65 29 2d 36 3b 20 69 3c 6e 49 6e 3b 20 69  rse)-6; i<nIn; i
2c89c 2b 2b 2c 20 6a 2d 2d 29 7b 0a 20 20 20 20 63 20  ++, j--){.    c 
2c89d 3d 20 7a 49 6e 5b 69 5d 3b 0a 20 20 20 20 69 66  = zIn[i];.    if
2c89e 28 20 63 3e 3d 27 41 27 20 26 26 20 63 3c 3d 27  ( c>='A' && c<='
2c89f 5a 27 20 29 7b 0a 20 20 20 20 20 20 7a 52 65 76  Z' ){.      zRev
2c8a0 65 72 73 65 5b 6a 5d 20 3d 20 63 20 2b 20 27 61  erse[j] = c + 'a
2c8a1 27 20 2d 20 27 41 27 3b 0a 20 20 20 20 7d 65 6c  ' - 'A';.    }el
2c8a2 73 65 20 69 66 28 20 63 3e 3d 27 61 27 20 26 26  se if( c>='a' &&
2c8a3 20 63 3c 3d 27 7a 27 20 29 7b 0a 20 20 20 20 20   c<='z' ){.     
2c8a4 20 7a 52 65 76 65 72 73 65 5b 6a 5d 20 3d 20 63   zReverse[j] = c
2c8a5 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2c8a6 20 20 20 2f 2a 20 54 68 65 20 75 73 65 20 6f 66     /* The use of
2c8a7 20 61 20 63 68 61 72 61 63 74 65 72 20 6e 6f 74   a character not
2c8a8 20 69 6e 20 5b 61 2d 7a 41 2d 5a 5d 20 6d 65 61   in [a-zA-Z] mea
2c8a9 6e 73 20 74 68 61 74 20 77 65 20 66 61 6c 6c 62  ns that we fallb
2c8aa 61 63 6b 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20  ack.      ** to 
2c8ab 74 68 65 20 63 6f 70 79 20 73 74 65 6d 6d 65 72  the copy stemmer
2c8ac 20 2a 2f 0a 20 20 20 20 20 20 63 6f 70 79 5f 73   */.      copy_s
2c8ad 74 65 6d 6d 65 72 28 7a 49 6e 2c 20 6e 49 6e 2c  temmer(zIn, nIn,
2c8ae 20 7a 4f 75 74 2c 20 70 6e 4f 75 74 29 3b 0a 20   zOut, pnOut);. 
2c8af 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
2c8b0 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28   }.  }.  memset(
2c8b1 26 7a 52 65 76 65 72 73 65 5b 73 69 7a 65 6f 66  &zReverse[sizeof
2c8b2 28 7a 52 65 76 65 72 73 65 29 2d 35 5d 2c 20 30  (zReverse)-5], 0
2c8b3 2c 20 35 29 3b 0a 20 20 7a 20 3d 20 26 7a 52 65  , 5);.  z = &zRe
2c8b4 76 65 72 73 65 5b 6a 2b 31 5d 3b 0a 0a 0a 20 20  verse[j+1];...  
2c8b5 2f 2a 20 53 74 65 70 20 31 61 20 2a 2f 0a 20 20  /* Step 1a */.  
2c8b6 69 66 28 20 7a 5b 30 5d 3d 3d 27 73 27 20 29 7b  if( z[0]=='s' ){
2c8b7 0a 20 20 20 20 69 66 28 0a 20 20 20 20 20 21 73  .    if(.     !s
2c8b8 74 65 6d 28 26 7a 2c 20 22 73 65 73 73 22 2c 20  tem(&z, "sess", 
2c8b9 22 73 73 22 2c 20 30 29 20 26 26 0a 20 20 20 20  "ss", 0) &&.    
2c8ba 20 21 73 74 65 6d 28 26 7a 2c 20 22 73 65 69 22   !stem(&z, "sei"
2c8bb 2c 20 22 69 22 2c 20 30 29 20 20 26 26 0a 20 20  , "i", 0)  &&.  
2c8bc 20 20 20 21 73 74 65 6d 28 26 7a 2c 20 22 73 73     !stem(&z, "ss
2c8bd 22 2c 20 22 73 73 22 2c 20 30 29 0a 20 20 20 20  ", "ss", 0).    
2c8be 29 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20  ){.      z++;.  
2c8bf 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74    }.  }..  /* St
2c8c0 65 70 20 31 62 20 2a 2f 20 20 0a 20 20 7a 32 20  ep 1b */  .  z2 
2c8c1 3d 20 7a 3b 0a 20 20 69 66 28 20 73 74 65 6d 28  = z;.  if( stem(
2c8c2 26 7a 2c 20 22 64 65 65 22 2c 20 22 65 65 22 2c  &z, "dee", "ee",
2c8c3 20 6d 5f 67 74 5f 30 29 20 29 7b 0a 20 20 20 20   m_gt_0) ){.    
2c8c4 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20  /* Do nothing.  
2c8c5 54 68 65 20 77 6f 72 6b 20 77 61 73 20 61 6c 6c  The work was all
2c8c6 20 69 6e 20 74 68 65 20 74 65 73 74 20 2a 2f 0a   in the test */.
2c8c7 20 20 7d 65 6c 73 65 20 69 66 28 20 0a 20 20 20    }else if( .   
2c8c8 20 20 28 73 74 65 6d 28 26 7a 2c 20 22 67 6e 69    (stem(&z, "gni
2c8c9 22 2c 20 22 22 2c 20 68 61 73 56 6f 77 65 6c 29  ", "", hasVowel)
2c8ca 20 7c 7c 20 73 74 65 6d 28 26 7a 2c 20 22 64 65   || stem(&z, "de
2c8cb 22 2c 20 22 22 2c 20 68 61 73 56 6f 77 65 6c 29  ", "", hasVowel)
2c8cc 29 0a 20 20 20 20 20 20 26 26 20 7a 21 3d 7a 32  ).      && z!=z2
2c8cd 0a 20 20 29 7b 0a 20 20 20 20 20 69 66 28 20 73  .  ){.     if( s
2c8ce 74 65 6d 28 26 7a 2c 20 22 74 61 22 2c 20 22 61  tem(&z, "ta", "a
2c8cf 74 65 22 2c 20 30 29 20 7c 7c 0a 20 20 20 20 20  te", 0) ||.     
2c8d0 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c 62      stem(&z, "lb
2c8d1 22 2c 20 22 62 6c 65 22 2c 20 30 29 20 7c 7c 0a  ", "ble", 0) ||.
2c8d2 20 20 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a           stem(&z
2c8d3 2c 20 22 7a 69 22 2c 20 22 69 7a 65 22 2c 20 30  , "zi", "ize", 0
2c8d4 29 20 29 7b 0a 20 20 20 20 20 20 20 2f 2a 20 44  ) ){.       /* D
2c8d5 6f 20 6e 6f 74 68 69 6e 67 2e 20 20 54 68 65 20  o nothing.  The 
2c8d6 77 6f 72 6b 20 77 61 73 20 61 6c 6c 20 69 6e 20  work was all in 
2c8d7 74 68 65 20 74 65 73 74 20 2a 2f 0a 20 20 20 20  the test */.    
2c8d8 20 7d 65 6c 73 65 20 69 66 28 20 64 6f 75 62 6c   }else if( doubl
2c8d9 65 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 26 26  eConsonant(z) &&
2c8da 20 28 2a 7a 21 3d 27 6c 27 20 26 26 20 2a 7a 21   (*z!='l' && *z!
2c8db 3d 27 73 27 20 26 26 20 2a 7a 21 3d 27 7a 27 29  ='s' && *z!='z')
2c8dc 20 29 7b 0a 20 20 20 20 20 20 20 7a 2b 2b 3b 0a   ){.       z++;.
2c8dd 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6d       }else if( m
2c8de 5f 65 71 5f 31 28 7a 29 20 26 26 20 73 74 61 72  _eq_1(z) && star
2c8df 5f 6f 68 28 7a 29 20 29 7b 0a 20 20 20 20 20 20  _oh(z) ){.      
2c8e0 20 2a 28 2d 2d 7a 29 20 3d 20 27 65 27 3b 0a 20   *(--z) = 'e';. 
2c8e1 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
2c8e2 53 74 65 70 20 31 63 20 2a 2f 0a 20 20 69 66 28  Step 1c */.  if(
2c8e3 20 7a 5b 30 5d 3d 3d 27 79 27 20 26 26 20 68 61   z[0]=='y' && ha
2c8e4 73 56 6f 77 65 6c 28 7a 2b 31 29 20 29 7b 0a 20  sVowel(z+1) ){. 
2c8e5 20 20 20 7a 5b 30 5d 20 3d 20 27 69 27 3b 0a 20     z[0] = 'i';. 
2c8e6 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 32 20   }..  /* Step 2 
2c8e7 2a 2f 0a 20 20 73 77 69 74 63 68 28 20 7a 5b 31  */.  switch( z[1
2c8e8 5d 20 29 7b 0a 20 20 20 63 61 73 65 20 27 61 27  ] ){.   case 'a'
2c8e9 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  :.     stem(&z, 
2c8ea 22 6c 61 6e 6f 69 74 61 22 2c 20 22 61 74 65 22  "lanoita", "ate"
2c8eb 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20  , m_gt_0) ||.   
2c8ec 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c 61 6e 6f    stem(&z, "lano
2c8ed 69 74 22 2c 20 22 74 69 6f 6e 22 2c 20 6d 5f 67  it", "tion", m_g
2c8ee 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b  t_0);.     break
2c8ef 3b 0a 20 20 20 63 61 73 65 20 27 63 27 3a 0a 20  ;.   case 'c':. 
2c8f0 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 63      stem(&z, "ic
2c8f1 6e 65 22 2c 20 22 65 6e 63 65 22 2c 20 6d 5f 67  ne", "ence", m_g
2c8f2 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65  t_0) ||.     ste
2c8f3 6d 28 26 7a 2c 20 22 69 63 6e 61 22 2c 20 22 61  m(&z, "icna", "a
2c8f4 6e 63 65 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20  nce", m_gt_0);. 
2c8f5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61      break;.   ca
2c8f6 73 65 20 27 65 27 3a 0a 20 20 20 20 20 73 74 65  se 'e':.     ste
2c8f7 6d 28 26 7a 2c 20 22 72 65 7a 69 22 2c 20 22 69  m(&z, "rezi", "i
2c8f8 7a 65 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20  ze", m_gt_0);.  
2c8f9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73     break;.   cas
2c8fa 65 20 27 67 27 3a 0a 20 20 20 20 20 73 74 65 6d  e 'g':.     stem
2c8fb 28 26 7a 2c 20 22 69 67 6f 6c 22 2c 20 22 6c 6f  (&z, "igol", "lo
2c8fc 67 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20  g", m_gt_0);.   
2c8fd 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65    break;.   case
2c8fe 20 27 6c 27 3a 0a 20 20 20 20 20 73 74 65 6d 28   'l':.     stem(
2c8ff 26 7a 2c 20 22 69 6c 62 22 2c 20 22 62 6c 65 22  &z, "ilb", "ble"
2c900 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20  , m_gt_0) ||.   
2c901 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 6c 6c 61    stem(&z, "illa
2c902 22 2c 20 22 61 6c 22 2c 20 6d 5f 67 74 5f 30 29  ", "al", m_gt_0)
2c903 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a   ||.     stem(&z
2c904 2c 20 22 69 6c 74 6e 65 22 2c 20 22 65 6e 74 22  , "iltne", "ent"
2c905 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20  , m_gt_0) ||.   
2c906 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 6c 65 22    stem(&z, "ile"
2c907 2c 20 22 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c  , "e", m_gt_0) |
2c908 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  |.     stem(&z, 
2c909 22 69 6c 73 75 6f 22 2c 20 22 6f 75 73 22 2c 20  "ilsuo", "ous", 
2c90a 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72  m_gt_0);.     br
2c90b 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 6f 27  eak;.   case 'o'
2c90c 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  :.     stem(&z, 
2c90d 22 6e 6f 69 74 61 7a 69 22 2c 20 22 69 7a 65 22  "noitazi", "ize"
2c90e 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20  , m_gt_0) ||.   
2c90f 20 20 73 74 65 6d 28 26 7a 2c 20 22 6e 6f 69 74    stem(&z, "noit
2c910 61 22 2c 20 22 61 74 65 22 2c 20 6d 5f 67 74 5f  a", "ate", m_gt_
2c911 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28  0) ||.     stem(
2c912 26 7a 2c 20 22 72 6f 74 61 22 2c 20 22 61 74 65  &z, "rota", "ate
2c913 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20  ", m_gt_0);.    
2c914 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
2c915 27 73 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  's':.     stem(&
2c916 7a 2c 20 22 6d 73 69 6c 61 22 2c 20 22 61 6c 22  z, "msila", "al"
2c917 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20  , m_gt_0) ||.   
2c918 20 20 73 74 65 6d 28 26 7a 2c 20 22 73 73 65 6e    stem(&z, "ssen
2c919 65 76 69 22 2c 20 22 69 76 65 22 2c 20 6d 5f 67  evi", "ive", m_g
2c91a 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65  t_0) ||.     ste
2c91b 6d 28 26 7a 2c 20 22 73 73 65 6e 6c 75 66 22 2c  m(&z, "ssenluf",
2c91c 20 22 66 75 6c 22 2c 20 6d 5f 67 74 5f 30 29 20   "ful", m_gt_0) 
2c91d 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  ||.     stem(&z,
2c91e 20 22 73 73 65 6e 73 75 6f 22 2c 20 22 6f 75 73   "ssensuo", "ous
2c91f 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20  ", m_gt_0);.    
2c920 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
2c921 27 74 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  't':.     stem(&
2c922 7a 2c 20 22 69 74 69 6c 61 22 2c 20 22 61 6c 22  z, "itila", "al"
2c923 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20  , m_gt_0) ||.   
2c924 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 74 69 76    stem(&z, "itiv
2c925 69 22 2c 20 22 69 76 65 22 2c 20 6d 5f 67 74 5f  i", "ive", m_gt_
2c926 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28  0) ||.     stem(
2c927 26 7a 2c 20 22 69 74 69 6c 69 62 22 2c 20 22 62  &z, "itilib", "b
2c928 6c 65 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20  le", m_gt_0);.  
2c929 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20     break;.  }.. 
2c92a 20 2f 2a 20 53 74 65 70 20 33 20 2a 2f 0a 20 20   /* Step 3 */.  
2c92b 73 77 69 74 63 68 28 20 7a 5b 30 5d 20 29 7b 0a  switch( z[0] ){.
2c92c 20 20 20 63 61 73 65 20 27 65 27 3a 0a 20 20 20     case 'e':.   
2c92d 20 20 73 74 65 6d 28 26 7a 2c 20 22 65 74 61 63    stem(&z, "etac
2c92e 69 22 2c 20 22 69 63 22 2c 20 6d 5f 67 74 5f 30  i", "ic", m_gt_0
2c92f 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
2c930 7a 2c 20 22 65 76 69 74 61 22 2c 20 22 22 2c 20  z, "evita", "", 
2c931 6d 5f 67 74 5f 30 29 20 20 20 7c 7c 0a 20 20 20  m_gt_0)   ||.   
2c932 20 20 73 74 65 6d 28 26 7a 2c 20 22 65 7a 69 6c    stem(&z, "ezil
2c933 61 22 2c 20 22 61 6c 22 2c 20 6d 5f 67 74 5f 30  a", "al", m_gt_0
2c934 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  );.     break;. 
2c935 20 20 63 61 73 65 20 27 69 27 3a 0a 20 20 20 20    case 'i':.    
2c936 20 73 74 65 6d 28 26 7a 2c 20 22 69 74 69 63 69   stem(&z, "itici
2c937 22 2c 20 22 69 63 22 2c 20 6d 5f 67 74 5f 30 29  ", "ic", m_gt_0)
2c938 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  ;.     break;.  
2c939 20 63 61 73 65 20 27 6c 27 3a 0a 20 20 20 20 20   case 'l':.     
2c93a 73 74 65 6d 28 26 7a 2c 20 22 6c 61 63 69 22 2c  stem(&z, "laci",
2c93b 20 22 69 63 22 2c 20 6d 5f 67 74 5f 30 29 20 7c   "ic", m_gt_0) |
2c93c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  |.     stem(&z, 
2c93d 22 6c 75 66 22 2c 20 22 22 2c 20 6d 5f 67 74 5f  "luf", "", m_gt_
2c93e 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a  0);.     break;.
2c93f 20 20 20 63 61 73 65 20 27 73 27 3a 0a 20 20 20     case 's':.   
2c940 20 20 73 74 65 6d 28 26 7a 2c 20 22 73 73 65 6e    stem(&z, "ssen
2c941 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a  ", "", m_gt_0);.
2c942 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a       break;.  }.
2c943 0a 20 20 2f 2a 20 53 74 65 70 20 34 20 2a 2f 0a  .  /* Step 4 */.
2c944 20 20 73 77 69 74 63 68 28 20 7a 5b 31 5d 20 29    switch( z[1] )
2c945 7b 0a 20 20 20 63 61 73 65 20 27 61 27 3a 0a 20  {.   case 'a':. 
2c946 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 6c      if( z[0]=='l
2c947 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 32 29  ' && m_gt_1(z+2)
2c948 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20   ){.       z += 
2c949 32 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62  2;.     }.     b
2c94a 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 63  reak;.   case 'c
2c94b 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d  ':.     if( z[0]
2c94c 3d 3d 27 65 27 20 26 26 20 7a 5b 32 5d 3d 3d 27  =='e' && z[2]=='
2c94d 6e 27 20 26 26 20 28 7a 5b 33 5d 3d 3d 27 61 27  n' && (z[3]=='a'
2c94e 20 7c 7c 20 7a 5b 33 5d 3d 3d 27 65 27 29 20 20   || z[3]=='e')  
2c94f 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 34 29 20 20  && m_gt_1(z+4)  
2c950 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 34  ){.       z += 4
2c951 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72  ;.     }.     br
2c952 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 65 27  eak;.   case 'e'
2c953 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d  :.     if( z[0]=
2c954 3d 27 72 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a  ='r' && m_gt_1(z
2c955 2b 32 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20  +2) ){.       z 
2c956 2b 3d 20 32 3b 0a 20 20 20 20 20 7d 0a 20 20 20  += 2;.     }.   
2c957 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65    break;.   case
2c958 20 27 69 27 3a 0a 20 20 20 20 20 69 66 28 20 7a   'i':.     if( z
2c959 5b 30 5d 3d 3d 27 63 27 20 26 26 20 6d 5f 67 74  [0]=='c' && m_gt
2c95a 5f 31 28 7a 2b 32 29 20 29 7b 0a 20 20 20 20 20  _1(z+2) ){.     
2c95b 20 20 7a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 7d    z += 2;.     }
2c95c 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
2c95d 63 61 73 65 20 27 6c 27 3a 0a 20 20 20 20 20 69  case 'l':.     i
2c95e 66 28 20 7a 5b 30 5d 3d 3d 27 65 27 20 26 26 20  f( z[0]=='e' && 
2c95f 7a 5b 32 5d 3d 3d 27 62 27 20 26 26 20 28 7a 5b  z[2]=='b' && (z[
2c960 33 5d 3d 3d 27 61 27 20 7c 7c 20 7a 5b 33 5d 3d  3]=='a' || z[3]=
2c961 3d 27 69 27 29 20 26 26 20 6d 5f 67 74 5f 31 28  ='i') && m_gt_1(
2c962 7a 2b 34 29 20 29 7b 0a 20 20 20 20 20 20 20 7a  z+4) ){.       z
2c963 20 2b 3d 20 34 3b 0a 20 20 20 20 20 7d 0a 20 20   += 4;.     }.  
2c964 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73     break;.   cas
2c965 65 20 27 6e 27 3a 0a 20 20 20 20 20 69 66 28 20  e 'n':.     if( 
2c966 7a 5b 30 5d 3d 3d 27 74 27 20 29 7b 0a 20 20 20  z[0]=='t' ){.   
2c967 20 20 20 20 69 66 28 20 7a 5b 32 5d 3d 3d 27 61      if( z[2]=='a
2c968 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 69 66  ' ){.         if
2c969 28 20 6d 5f 67 74 5f 31 28 7a 2b 33 29 20 29 7b  ( m_gt_1(z+3) ){
2c96a 0a 20 20 20 20 20 20 20 20 20 20 20 7a 20 2b 3d  .           z +=
2c96b 20 33 3b 0a 20 20 20 20 20 20 20 20 20 7d 0a 20   3;.         }. 
2c96c 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
2c96d 7a 5b 32 5d 3d 3d 27 65 27 20 29 7b 0a 20 20 20  z[2]=='e' ){.   
2c96e 20 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22        stem(&z, "
2c96f 74 6e 65 6d 65 22 2c 20 22 22 2c 20 6d 5f 67 74  tneme", "", m_gt
2c970 5f 31 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 20  _1) ||.         
2c971 73 74 65 6d 28 26 7a 2c 20 22 74 6e 65 6d 22 2c  stem(&z, "tnem",
2c972 20 22 22 2c 20 6d 5f 67 74 5f 31 29 20 7c 7c 0a   "", m_gt_1) ||.
2c973 20 20 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a           stem(&z
2c974 2c 20 22 74 6e 65 22 2c 20 22 22 2c 20 6d 5f 67  , "tne", "", m_g
2c975 74 5f 31 29 3b 0a 20 20 20 20 20 20 20 7d 0a 20  t_1);.       }. 
2c976 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b      }.     break
2c977 3b 0a 20 20 20 63 61 73 65 20 27 6f 27 3a 0a 20  ;.   case 'o':. 
2c978 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 75      if( z[0]=='u
2c979 27 20 29 7b 0a 20 20 20 20 20 20 20 69 66 28 20  ' ){.       if( 
2c97a 6d 5f 67 74 5f 31 28 7a 2b 32 29 20 29 7b 0a 20  m_gt_1(z+2) ){. 
2c97b 20 20 20 20 20 20 20 20 7a 20 2b 3d 20 32 3b 0a          z += 2;.
2c97c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 7d 65         }.     }e
2c97d 6c 73 65 20 69 66 28 20 7a 5b 33 5d 3d 3d 27 73  lse if( z[3]=='s
2c97e 27 20 7c 7c 20 7a 5b 33 5d 3d 3d 27 74 27 20 29  ' || z[3]=='t' )
2c97f 7b 0a 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a  {.       stem(&z
2c980 2c 20 22 6e 6f 69 22 2c 20 22 22 2c 20 6d 5f 67  , "noi", "", m_g
2c981 74 5f 31 29 3b 0a 20 20 20 20 20 7d 0a 20 20 20  t_1);.     }.   
2c982 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65    break;.   case
2c983 20 27 73 27 3a 0a 20 20 20 20 20 69 66 28 20 7a   's':.     if( z
2c984 5b 30 5d 3d 3d 27 6d 27 20 26 26 20 7a 5b 32 5d  [0]=='m' && z[2]
2c985 3d 3d 27 69 27 20 26 26 20 6d 5f 67 74 5f 31 28  =='i' && m_gt_1(
2c986 7a 2b 33 29 20 29 7b 0a 20 20 20 20 20 20 20 7a  z+3) ){.       z
2c987 20 2b 3d 20 33 3b 0a 20 20 20 20 20 7d 0a 20 20   += 3;.     }.  
2c988 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73     break;.   cas
2c989 65 20 27 74 27 3a 0a 20 20 20 20 20 73 74 65 6d  e 't':.     stem
2c98a 28 26 7a 2c 20 22 65 74 61 22 2c 20 22 22 2c 20  (&z, "eta", "", 
2c98b 6d 5f 67 74 5f 31 29 20 7c 7c 0a 20 20 20 20 20  m_gt_1) ||.     
2c98c 73 74 65 6d 28 26 7a 2c 20 22 69 74 69 22 2c 20  stem(&z, "iti", 
2c98d 22 22 2c 20 6d 5f 67 74 5f 31 29 3b 0a 20 20 20  "", m_gt_1);.   
2c98e 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65    break;.   case
2c98f 20 27 75 27 3a 0a 20 20 20 20 20 69 66 28 20 7a   'u':.     if( z
2c990 5b 30 5d 3d 3d 27 73 27 20 26 26 20 7a 5b 32 5d  [0]=='s' && z[2]
2c991 3d 3d 27 6f 27 20 26 26 20 6d 5f 67 74 5f 31 28  =='o' && m_gt_1(
2c992 7a 2b 33 29 20 29 7b 0a 20 20 20 20 20 20 20 7a  z+3) ){.       z
2c993 20 2b 3d 20 33 3b 0a 20 20 20 20 20 7d 0a 20 20   += 3;.     }.  
2c994 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73     break;.   cas
2c995 65 20 27 76 27 3a 0a 20 20 20 63 61 73 65 20 27  e 'v':.   case '
2c996 7a 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30  z':.     if( z[0
2c997 5d 3d 3d 27 65 27 20 26 26 20 7a 5b 32 5d 3d 3d  ]=='e' && z[2]==
2c998 27 69 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b  'i' && m_gt_1(z+
2c999 33 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b  3) ){.       z +
2c99a 3d 20 33 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20  = 3;.     }.    
2c99b 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f   break;.  }..  /
2c99c 2a 20 53 74 65 70 20 35 61 20 2a 2f 0a 20 20 69  * Step 5a */.  i
2c99d 66 28 20 7a 5b 30 5d 3d 3d 27 65 27 20 29 7b 0a  f( z[0]=='e' ){.
2c99e 20 20 20 20 69 66 28 20 6d 5f 67 74 5f 31 28 7a      if( m_gt_1(z
2c99f 2b 31 29 20 29 7b 0a 20 20 20 20 20 20 7a 2b 2b  +1) ){.      z++
2c9a0 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
2c9a1 6d 5f 65 71 5f 31 28 7a 2b 31 29 20 26 26 20 21  m_eq_1(z+1) && !
2c9a2 73 74 61 72 5f 6f 68 28 7a 2b 31 29 20 29 7b 0a  star_oh(z+1) ){.
2c9a3 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d        z++;.    }
2c9a4 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20  .  }..  /* Step 
2c9a5 35 62 20 2a 2f 0a 20 20 69 66 28 20 6d 5f 67 74  5b */.  if( m_gt
2c9a6 5f 31 28 7a 29 20 26 26 20 7a 5b 30 5d 3d 3d 27  _1(z) && z[0]=='
2c9a7 6c 27 20 26 26 20 7a 5b 31 5d 3d 3d 27 6c 27 20  l' && z[1]=='l' 
2c9a8 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 0a  ){.    z++;.  }.
2c9a9 0a 20 20 2f 2a 20 7a 5b 5d 20 69 73 20 6e 6f 77  .  /* z[] is now
2c9aa 20 74 68 65 20 73 74 65 6d 6d 65 64 20 77 6f 72   the stemmed wor
2c9ab 64 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64  d in reverse ord
2c9ac 65 72 2e 20 20 46 6c 69 70 20 69 74 20 62 61 63  er.  Flip it bac
2c9ad 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64 20 69 6e  k.  ** around in
2c9ae 74 6f 20 66 6f 72 77 61 72 64 20 6f 72 64 65 72  to forward order
2c9af 20 61 6e 64 20 72 65 74 75 72 6e 2e 0a 20 20 2a   and return..  *
2c9b0 2f 0a 20 20 2a 70 6e 4f 75 74 20 3d 20 69 20 3d  /.  *pnOut = i =
2c9b1 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20 7a 4f   strlen(z);.  zO
2c9b2 75 74 5b 69 5d 20 3d 20 30 3b 0a 20 20 77 68 69  ut[i] = 0;.  whi
2c9b3 6c 65 28 20 2a 7a 20 29 7b 0a 20 20 20 20 7a 4f  le( *z ){.    zO
2c9b4 75 74 5b 2d 2d 69 5d 20 3d 20 2a 28 7a 2b 2b 29  ut[--i] = *(z++)
2c9b5 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ;.  }.}../*.** C
2c9b6 68 61 72 61 63 74 65 72 73 20 74 68 61 74 20 63  haracters that c
2c9b7 61 6e 20 62 65 20 70 61 72 74 20 6f 66 20 61 20  an be part of a 
2c9b8 74 6f 6b 65 6e 2e 20 20 57 65 20 61 73 73 75 6d  token.  We assum
2c9b9 65 20 61 6e 79 20 63 68 61 72 61 63 74 65 72 0a  e any character.
2c9ba 2a 2a 20 77 68 6f 73 65 20 76 61 6c 75 65 20 69  ** whose value i
2c9bb 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 30  s greater than 0
2c9bc 78 38 30 20 28 61 6e 79 20 55 54 46 20 63 68 61  x80 (any UTF cha
2c9bd 72 61 63 74 65 72 29 20 63 61 6e 20 62 65 0a 2a  racter) can be.*
2c9be 2a 20 70 61 72 74 20 6f 66 20 61 20 74 6f 6b 65  * part of a toke
2c9bf 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72  n.  In other wor
2c9c0 64 73 2c 20 64 65 6c 69 6d 69 74 65 72 73 20 61  ds, delimiters a
2c9c1 6c 6c 20 6d 75 73 74 20 68 61 76 65 0a 2a 2a 20  ll must have.** 
2c9c2 76 61 6c 75 65 73 20 6f 66 20 30 78 37 66 20 6f  values of 0x7f o
2c9c3 72 20 6c 6f 77 65 72 2e 0a 2a 2f 0a 73 74 61 74  r lower..*/.stat
2c9c4 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 70 6f  ic const char po
2c9c5 72 74 65 72 49 64 43 68 61 72 5b 5d 20 3d 20 7b  rterIdChar[] = {
2c9c6 0a 2f 2a 20 78 30 20 78 31 20 78 32 20 78 33 20  ./* x0 x1 x2 x3 
2c9c7 78 34 20 78 35 20 78 36 20 78 37 20 78 38 20 78  x4 x5 x6 x7 x8 x
2c9c8 39 20 78 41 20 78 42 20 78 43 20 78 44 20 78 45  9 xA xB xC xD xE
2c9c9 20 78 46 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c   xF */.    1, 1,
2c9ca 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2c9cb 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30  1, 1, 1, 0, 0, 0
2c9cc 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 33  , 0, 0, 0,  /* 3
2c9cd 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31  x */.    0, 1, 1
2c9ce 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2c9cf 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2c9d0 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 34 78 20  1, 1, 1,  /* 4x 
2c9d1 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20  */.    1, 1, 1, 
2c9d2 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2c9d3 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 1, 0, 0, 0,
2c9d4 20 30 2c 20 31 2c 20 20 2f 2a 20 35 78 20 2a 2f   0, 1,  /* 5x */
2c9d5 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c  .    0, 1, 1, 1,
2c9d6 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2c9d7 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2c9d8 2c 20 31 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20  , 1,  /* 6x */. 
2c9d9 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31     1, 1, 1, 1, 1
2c9da 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2c9db 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 0, 0, 0, 0, 
2c9dc 30 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a 7d 3b 0a  0,  /* 7x */.};.
2c9dd 23 64 65 66 69 6e 65 20 69 73 44 65 6c 69 6d 28  #define isDelim(
2c9de 43 29 20 28 28 28 63 68 3d 43 29 26 30 78 38 30  C) (((ch=C)&0x80
2c9df 29 3d 3d 30 20 26 26 20 28 63 68 3c 30 78 33 30  )==0 && (ch<0x30
2c9e0 20 7c 7c 20 21 70 6f 72 74 65 72 49 64 43 68 61   || !porterIdCha
2c9e1 72 5b 63 68 2d 30 78 33 30 5d 29 29 0a 0a 2f 2a  r[ch-0x30]))../*
2c9e2 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20  .** Extract the 
2c9e3 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20  next token from 
2c9e4 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63  a tokenization c
2c9e5 75 72 73 6f 72 2e 20 20 54 68 65 20 63 75 72 73  ursor.  The curs
2c9e6 6f 72 20 6d 75 73 74 0a 2a 2a 20 68 61 76 65 20  or must.** have 
2c9e7 62 65 65 6e 20 6f 70 65 6e 65 64 20 62 79 20 61  been opened by a
2c9e8 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 70   prior call to p
2c9e9 6f 72 74 65 72 4f 70 65 6e 28 29 2e 0a 2a 2f 0a  orterOpen()..*/.
2c9ea 73 74 61 74 69 63 20 69 6e 74 20 70 6f 72 74 65  static int porte
2c9eb 72 4e 65 78 74 28 0a 20 20 73 71 6c 69 74 65 33  rNext(.  sqlite3
2c9ec 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
2c9ed 72 20 2a 70 43 75 72 73 6f 72 2c 20 20 2f 2a 20  r *pCursor,  /* 
2c9ee 43 75 72 73 6f 72 20 72 65 74 75 72 6e 65 64 20  Cursor returned 
2c9ef 62 79 20 70 6f 72 74 65 72 4f 70 65 6e 20 2a 2f  by porterOpen */
2c9f0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  .  const char **
2c9f1 70 7a 54 6f 6b 65 6e 2c 20 20 20 20 20 20 20 20  pzToken,        
2c9f2 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 2a         /* OUT: *
2c9f3 70 7a 54 6f 6b 65 6e 20 69 73 20 74 68 65 20 74  pzToken is the t
2c9f4 6f 6b 65 6e 20 74 65 78 74 20 2a 2f 0a 20 20 69  oken text */.  i
2c9f5 6e 74 20 2a 70 6e 42 79 74 65 73 2c 20 20 20 20  nt *pnBytes,    
2c9f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c9f7 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65     /* OUT: Numbe
2c9f8 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 6f  r of bytes in to
2c9f9 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69  ken */.  int *pi
2c9fa 53 74 61 72 74 4f 66 66 73 65 74 2c 20 20 20 20  StartOffset,    
2c9fb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c9fc 4f 55 54 3a 20 53 74 61 72 74 69 6e 67 20 6f 66  OUT: Starting of
2c9fd 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f  fset of token */
2c9fe 0a 20 20 69 6e 74 20 2a 70 69 45 6e 64 4f 66 66  .  int *piEndOff
2c9ff 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  set,            
2ca00 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45         /* OUT: E
2ca01 6e 64 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20  nding offset of 
2ca02 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a  token */.  int *
2ca03 70 69 50 6f 73 69 74 69 6f 6e 20 20 20 20 20 20  piPosition      
2ca04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2ca05 2a 20 4f 55 54 3a 20 50 6f 73 69 74 69 6f 6e 20  * OUT: Position 
2ca06 69 6e 74 65 67 65 72 20 6f 66 20 74 6f 6b 65 6e  integer of token
2ca07 20 2a 2f 0a 29 7b 0a 20 20 70 6f 72 74 65 72 5f   */.){.  porter_
2ca08 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
2ca09 20 2a 63 20 3d 20 28 70 6f 72 74 65 72 5f 74 6f   *c = (porter_to
2ca0a 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
2ca0b 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 63 6f 6e  ) pCursor;.  con
2ca0c 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 63 2d 3e  st char *z = c->
2ca0d 7a 49 6e 70 75 74 3b 0a 0a 20 20 77 68 69 6c 65  zInput;..  while
2ca0e 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d 3e  ( c->iOffset<c->
2ca0f 6e 49 6e 70 75 74 20 29 7b 0a 20 20 20 20 69 6e  nInput ){.    in
2ca10 74 20 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20  t iStartOffset, 
2ca11 63 68 3b 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e  ch;..    /* Scan
2ca12 20 70 61 73 74 20 64 65 6c 69 6d 69 74 65 72 20   past delimiter 
2ca13 63 68 61 72 61 63 74 65 72 73 20 2a 2f 0a 20 20  characters */.  
2ca14 20 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66 66    while( c->iOff
2ca15 73 65 74 3c 63 2d 3e 6e 49 6e 70 75 74 20 26 26  set<c->nInput &&
2ca16 20 69 73 44 65 6c 69 6d 28 7a 5b 63 2d 3e 69 4f   isDelim(z[c->iO
2ca17 66 66 73 65 74 5d 29 20 29 7b 0a 20 20 20 20 20  ffset]) ){.     
2ca18 20 63 2d 3e 69 4f 66 66 73 65 74 2b 2b 3b 0a 20   c->iOffset++;. 
2ca19 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 75     }..    /* Cou
2ca1a 6e 74 20 6e 6f 6e 2d 64 65 6c 69 6d 69 74 65 72  nt non-delimiter
2ca1b 20 63 68 61 72 61 63 74 65 72 73 2e 20 2a 2f 0a   characters. */.
2ca1c 20 20 20 20 69 53 74 61 72 74 4f 66 66 73 65 74      iStartOffset
2ca1d 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74 3b 0a 20   = c->iOffset;. 
2ca1e 20 20 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66     while( c->iOf
2ca1f 66 73 65 74 3c 63 2d 3e 6e 49 6e 70 75 74 20 26  fset<c->nInput &
2ca20 26 20 21 69 73 44 65 6c 69 6d 28 7a 5b 63 2d 3e  & !isDelim(z[c->
2ca21 69 4f 66 66 73 65 74 5d 29 20 29 7b 0a 20 20 20  iOffset]) ){.   
2ca22 20 20 20 63 2d 3e 69 4f 66 66 73 65 74 2b 2b 3b     c->iOffset++;
2ca23 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
2ca24 63 2d 3e 69 4f 66 66 73 65 74 3e 69 53 74 61 72  c->iOffset>iStar
2ca25 74 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 20  tOffset ){.     
2ca26 20 69 6e 74 20 6e 20 3d 20 63 2d 3e 69 4f 66 66   int n = c->iOff
2ca27 73 65 74 2d 69 53 74 61 72 74 4f 66 66 73 65 74  set-iStartOffset
2ca28 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 63 2d  ;.      if( n>c-
2ca29 3e 6e 41 6c 6c 6f 63 61 74 65 64 20 29 7b 0a 20  >nAllocated ){. 
2ca2a 20 20 20 20 20 20 20 63 2d 3e 6e 41 6c 6c 6f 63         c->nAlloc
2ca2b 61 74 65 64 20 3d 20 6e 2b 32 30 3b 0a 20 20 20  ated = n+20;.   
2ca2c 20 20 20 20 20 63 2d 3e 7a 54 6f 6b 65 6e 20 3d       c->zToken =
2ca2d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
2ca2e 28 63 2d 3e 7a 54 6f 6b 65 6e 2c 20 63 2d 3e 6e  (c->zToken, c->n
2ca2f 41 6c 6c 6f 63 61 74 65 64 29 3b 0a 20 20 20 20  Allocated);.    
2ca30 20 20 20 20 69 66 28 20 63 2d 3e 7a 54 6f 6b 65      if( c->zToke
2ca31 6e 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e  n==NULL ) return
2ca32 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
2ca33 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 6f 72       }.      por
2ca34 74 65 72 5f 73 74 65 6d 6d 65 72 28 26 7a 5b 69  ter_stemmer(&z[i
2ca35 53 74 61 72 74 4f 66 66 73 65 74 5d 2c 20 6e 2c  StartOffset], n,
2ca36 20 63 2d 3e 7a 54 6f 6b 65 6e 2c 20 70 6e 42 79   c->zToken, pnBy
2ca37 74 65 73 29 3b 0a 20 20 20 20 20 20 2a 70 7a 54  tes);.      *pzT
2ca38 6f 6b 65 6e 20 3d 20 63 2d 3e 7a 54 6f 6b 65 6e  oken = c->zToken
2ca39 3b 0a 20 20 20 20 20 20 2a 70 69 53 74 61 72 74  ;.      *piStart
2ca3a 4f 66 66 73 65 74 20 3d 20 69 53 74 61 72 74 4f  Offset = iStartO
2ca3b 66 66 73 65 74 3b 0a 20 20 20 20 20 20 2a 70 69  ffset;.      *pi
2ca3c 45 6e 64 4f 66 66 73 65 74 20 3d 20 63 2d 3e 69  EndOffset = c->i
2ca3d 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 2a 70  Offset;.      *p
2ca3e 69 50 6f 73 69 74 69 6f 6e 20 3d 20 63 2d 3e 69  iPosition = c->i
2ca3f 54 6f 6b 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 72  Token++;.      r
2ca40 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2ca41 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
2ca42 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  urn SQLITE_DONE;
2ca43 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65  .}../*.** The se
2ca44 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 74 68  t of routines th
2ca45 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  at implement the
2ca46 20 70 6f 72 74 65 72 2d 73 74 65 6d 6d 65 72 20   porter-stemmer 
2ca47 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61  tokenizer.*/.sta
2ca48 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  tic const sqlite
2ca49 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
2ca4a 6c 65 20 70 6f 72 74 65 72 54 6f 6b 65 6e 69 7a  le porterTokeniz
2ca4b 65 72 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20 20 30  erModule = {.  0
2ca4c 2c 0a 20 20 70 6f 72 74 65 72 43 72 65 61 74 65  ,.  porterCreate
2ca4d 2c 0a 20 20 70 6f 72 74 65 72 44 65 73 74 72 6f  ,.  porterDestro
2ca4e 79 2c 0a 20 20 70 6f 72 74 65 72 4f 70 65 6e 2c  y,.  porterOpen,
2ca4f 0a 20 20 70 6f 72 74 65 72 43 6c 6f 73 65 2c 0a  .  porterClose,.
2ca50 20 20 70 6f 72 74 65 72 4e 65 78 74 2c 0a 7d 3b    porterNext,.};
2ca51 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ../*.** Allocate
2ca52 20 61 20 6e 65 77 20 70 6f 72 74 65 72 20 74 6f   a new porter to
2ca53 6b 65 6e 69 7a 65 72 2e 20 20 52 65 74 75 72 6e  kenizer.  Return
2ca54 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
2ca55 65 20 6e 65 77 0a 2a 2a 20 74 6f 6b 65 6e 69 7a  e new.** tokeniz
2ca56 65 72 20 69 6e 20 2a 70 70 4d 6f 64 75 6c 65 0a  er in *ppModule.
2ca57 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2ca58 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74  E void sqlite3Ft
2ca59 73 33 50 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65  s3PorterTokenize
2ca5a 72 4d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74  rModule(.  sqlit
2ca5b 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
2ca5c 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64  ule const**ppMod
2ca5d 75 6c 65 0a 29 7b 0a 20 20 2a 70 70 4d 6f 64 75  ule.){.  *ppModu
2ca5e 6c 65 20 3d 20 26 70 6f 72 74 65 72 54 6f 6b 65  le = &porterToke
2ca5f 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b 0a 7d 0a 0a  nizerModule;.}..
2ca60 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
2ca61 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20  ed(SQLITE_CORE) 
2ca62 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
2ca63 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a  E_ENABLE_FTS3) *
2ca64 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
2ca65 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 70  ** End of fts3_p
2ca66 6f 72 74 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  orter.c ********
2ca67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ca68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ca69 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
2ca6a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74  ** Begin file ft
2ca6b 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e 63 20 2a  s3_tokenizer.c *
2ca6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ca6d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ca6e 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4a 75  */./*.** 2007 Ju
2ca6f 6e 65 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ne 22.**.** The 
2ca70 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
2ca71 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
2ca72 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
2ca73 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
2ca74 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
2ca75 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
2ca76 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
2ca77 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
2ca78 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
2ca79 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
2ca7a 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
2ca7b 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
2ca7c 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
2ca7d 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
2ca7e 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
2ca7f 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
2ca80 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
2ca81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ca82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ca83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ca84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ca85 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
2ca86 54 68 69 73 20 69 73 20 70 61 72 74 20 6f 66 20  This is part of 
2ca87 61 6e 20 53 51 4c 69 74 65 20 6d 6f 64 75 6c 65  an SQLite module
2ca88 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 66 75   implementing fu
2ca89 6c 6c 2d 74 65 78 74 20 73 65 61 72 63 68 2e 0a  ll-text search..
2ca8a 2a 2a 20 54 68 69 73 20 70 61 72 74 69 63 75 6c  ** This particul
2ca8b 61 72 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e  ar file implemen
2ca8c 74 73 20 74 68 65 20 67 65 6e 65 72 69 63 20 74  ts the generic t
2ca8d 6f 6b 65 6e 69 7a 65 72 20 69 6e 74 65 72 66 61  okenizer interfa
2ca8e 63 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  ce..*/../*.** Th
2ca8f 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66  e code in this f
2ca90 69 6c 65 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70  ile is only comp
2ca91 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20  iled if:.**.**  
2ca92 20 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f     * The FTS3 mo
2ca93 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75  dule is being bu
2ca94 69 6c 74 20 61 73 20 61 6e 20 65 78 74 65 6e 73  ilt as an extens
2ca95 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 28 69 6e  ion.**       (in
2ca96 20 77 68 69 63 68 20 63 61 73 65 20 53 51 4c 49   which case SQLI
2ca97 54 45 5f 43 4f 52 45 20 69 73 20 6e 6f 74 20 64  TE_CORE is not d
2ca98 65 66 69 6e 65 64 29 2c 20 6f 72 0a 2a 2a 0a 2a  efined), or.**.*
2ca99 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53 33  *     * The FTS3
2ca9a 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67   module is being
2ca9b 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20   built into the 
2ca9c 63 6f 72 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20  core of.**      
2ca9d 20 53 51 4c 69 74 65 20 28 69 6e 20 77 68 69 63   SQLite (in whic
2ca9e 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f 45 4e  h case SQLITE_EN
2ca9f 41 42 4c 45 5f 46 54 53 33 20 69 73 20 64 65 66  ABLE_FTS3 is def
2caa0 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69 66 20 21 64  ined)..*/.#if !d
2caa1 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
2caa2 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
2caa3 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
2caa4 33 29 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  3)..#ifndef SQLI
2caa5 54 45 5f 43 4f 52 45 0a 20 20 53 51 4c 49 54 45  TE_CORE.  SQLITE
2caa6 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31  _EXTENSION_INIT1
2caa7 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
2caa8 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
2caa9 66 20 74 68 65 20 53 51 4c 20 73 63 61 6c 61 72  f the SQL scalar
2caaa 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 63   function for ac
2caab 63 65 73 73 69 6e 67 20 74 68 65 20 75 6e 64 65  cessing the unde
2caac 72 6c 79 69 6e 67 20 0a 2a 2a 20 68 61 73 68 20  rlying .** hash 
2caad 74 61 62 6c 65 2e 20 54 68 69 73 20 66 75 6e 63  table. This func
2caae 74 69 6f 6e 20 6d 61 79 20 62 65 20 63 61 6c 6c  tion may be call
2caaf 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  ed as follows:.*
2cab0 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c 66  *.**   SELECT <f
2cab1 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c 6b  unction-name>(<k
2cab2 65 79 2d 6e 61 6d 65 3e 29 3b 0a 2a 2a 20 20 20  ey-name>);.**   
2cab3 53 45 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f 6e  SELECT <function
2cab4 2d 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d 65  -name>(<key-name
2cab5 3e 2c 20 3c 70 6f 69 6e 74 65 72 3e 29 3b 0a 2a  >, <pointer>);.*
2cab6 2a 0a 2a 2a 20 77 68 65 72 65 20 3c 66 75 6e 63  *.** where <func
2cab7 74 69 6f 6e 2d 6e 61 6d 65 3e 20 69 73 20 74 68  tion-name> is th
2cab8 65 20 6e 61 6d 65 20 70 61 73 73 65 64 20 61 73  e name passed as
2cab9 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
2caba 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 73  ment.** to the s
2cabb 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 48 61  qlite3Fts3InitHa
2cabc 73 68 54 61 62 6c 65 28 29 20 66 75 6e 63 74 69  shTable() functi
2cabd 6f 6e 20 28 65 2e 67 2e 20 27 66 74 73 33 5f 74  on (e.g. 'fts3_t
2cabe 6f 6b 65 6e 69 7a 65 72 27 29 2e 0a 2a 2a 0a 2a  okenizer')..**.*
2cabf 2a 20 49 66 20 74 68 65 20 3c 70 6f 69 6e 74 65  * If the <pointe
2cac0 72 3e 20 61 72 67 75 6d 65 6e 74 20 69 73 20 73  r> argument is s
2cac1 70 65 63 69 66 69 65 64 2c 20 69 74 20 6d 75 73  pecified, it mus
2cac2 74 20 62 65 20 61 20 62 6c 6f 62 20 76 61 6c 75  t be a blob valu
2cac3 65 0a 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20  e.** containing 
2cac4 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 62 65 20  a pointer to be 
2cac5 73 74 6f 72 65 64 20 61 73 20 74 68 65 20 68 61  stored as the ha
2cac6 73 68 20 64 61 74 61 20 63 6f 72 72 65 73 70 6f  sh data correspo
2cac7 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 74 68 65 20  nding.** to the 
2cac8 73 74 72 69 6e 67 20 3c 6b 65 79 2d 6e 61 6d 65  string <key-name
2cac9 3e 2e 20 49 66 20 3c 70 6f 69 6e 74 65 72 3e 20  >. If <pointer> 
2caca 69 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64  is not specified
2cacb 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 73 74  , then.** the st
2cacc 72 69 6e 67 20 3c 6b 65 79 2d 6e 61 6d 65 3e 20  ring <key-name> 
2cacd 6d 75 73 74 20 61 6c 72 65 61 64 79 20 65 78 69  must already exi
2cace 73 74 20 69 6e 20 74 68 65 20 68 61 73 20 74 61  st in the has ta
2cacf 62 6c 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a  ble. Otherwise,.
2cad0 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72  ** an error is r
2cad1 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 57  eturned..**.** W
2cad2 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
2cad3 65 20 3c 70 6f 69 6e 74 65 72 3e 20 61 72 67 75  e <pointer> argu
2cad4 6d 65 6e 74 20 69 73 20 73 70 65 63 69 66 69 65  ment is specifie
2cad5 64 2c 20 74 68 65 20 76 61 6c 75 65 20 72 65 74  d, the value ret
2cad6 75 72 6e 65 64 0a 2a 2a 20 69 73 20 61 20 62 6c  urned.** is a bl
2cad7 6f 62 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ob containing th
2cad8 65 20 70 6f 69 6e 74 65 72 20 73 74 6f 72 65 64  e pointer stored
2cad9 20 61 73 20 74 68 65 20 68 61 73 68 20 64 61 74   as the hash dat
2cada 61 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a  a corresponding.
2cadb 2a 2a 20 74 6f 20 73 74 72 69 6e 67 20 3c 6b 65  ** to string <ke
2cadc 79 2d 6e 61 6d 65 3e 20 28 61 66 74 65 72 20 74  y-name> (after t
2cadd 68 65 20 68 61 73 68 2d 74 61 62 6c 65 20 69 73  he hash-table is
2cade 20 75 70 64 61 74 65 64 2c 20 69 66 20 61 70 70   updated, if app
2cadf 6c 69 63 61 62 6c 65 29 2e 0a 2a 2f 0a 73 74 61  licable)..*/.sta
2cae0 74 69 63 20 76 6f 69 64 20 73 63 61 6c 61 72 46  tic void scalarF
2cae1 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
2cae2 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
2cae3 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
2cae4 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
2cae5 72 67 76 0a 29 7b 0a 20 20 66 74 73 33 48 61 73  rgv.){.  fts3Has
2cae6 68 20 2a 70 48 61 73 68 3b 0a 20 20 76 6f 69 64  h *pHash;.  void
2cae7 20 2a 70 50 74 72 20 3d 20 30 3b 0a 20 20 63 6f   *pPtr = 0;.  co
2cae8 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
2cae9 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69 6e 74 20  r *zName;.  int 
2caea 6e 4e 61 6d 65 3b 0a 0a 20 20 61 73 73 65 72 74  nName;..  assert
2caeb 28 20 61 72 67 63 3d 3d 31 20 7c 7c 20 61 72 67  ( argc==1 || arg
2caec 63 3d 3d 32 20 29 3b 0a 0a 20 20 70 48 61 73 68  c==2 );..  pHash
2caed 20 3d 20 28 66 74 73 33 48 61 73 68 20 2a 29 73   = (fts3Hash *)s
2caee 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
2caef 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 7a 4e  (context);..  zN
2caf0 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  ame = sqlite3_va
2caf1 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
2caf2 29 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c  );.  nName = sql
2caf3 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
2caf4 28 61 72 67 76 5b 30 5d 29 2b 31 3b 0a 0a 20 20  (argv[0])+1;..  
2caf5 69 66 28 20 61 72 67 63 3d 3d 32 20 29 7b 0a 20  if( argc==2 ){. 
2caf6 20 20 20 76 6f 69 64 20 2a 70 4f 6c 64 3b 0a 20     void *pOld;. 
2caf7 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74     int n = sqlit
2caf8 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
2caf9 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 69 66 28  rgv[1]);.    if(
2cafa 20 6e 21 3d 73 69 7a 65 6f 66 28 70 50 74 72 29   n!=sizeof(pPtr)
2cafb 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2cafc 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63  3_result_error(c
2cafd 6f 6e 74 65 78 74 2c 20 22 61 72 67 75 6d 65 6e  ontext, "argumen
2cafe 74 20 74 79 70 65 20 6d 69 73 6d 61 74 63 68 22  t type mismatch"
2caff 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 72 65 74  , -1);.      ret
2cb00 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  urn;.    }.    p
2cb01 50 74 72 20 3d 20 2a 28 76 6f 69 64 20 2a 2a 29  Ptr = *(void **)
2cb02 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
2cb03 6f 62 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20  ob(argv[1]);.   
2cb04 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33 46   pOld = sqlite3F
2cb05 74 73 33 48 61 73 68 49 6e 73 65 72 74 28 70 48  ts3HashInsert(pH
2cb06 61 73 68 2c 20 28 76 6f 69 64 20 2a 29 7a 4e 61  ash, (void *)zNa
2cb07 6d 65 2c 20 6e 4e 61 6d 65 2c 20 70 50 74 72 29  me, nName, pPtr)
2cb08 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c 64 3d 3d  ;.    if( pOld==
2cb09 70 50 74 72 20 29 7b 0a 20 20 20 20 20 20 73 71  pPtr ){.      sq
2cb0a 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
2cb0b 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 6f 75 74  or(context, "out
2cb0c 20 6f 66 20 6d 65 6d 6f 72 79 22 2c 20 2d 31 29   of memory", -1)
2cb0d 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
2cb0e 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
2cb0f 20 20 20 70 50 74 72 20 3d 20 73 71 6c 69 74 65     pPtr = sqlite
2cb10 33 46 74 73 33 48 61 73 68 46 69 6e 64 28 70 48  3Fts3HashFind(pH
2cb11 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  ash, zName, nNam
2cb12 65 29 3b 0a 20 20 20 20 69 66 28 20 21 70 50 74  e);.    if( !pPt
2cb13 72 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20  r ){.      char 
2cb14 2a 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f  *zErr = sqlite3_
2cb15 6d 70 72 69 6e 74 66 28 22 75 6e 6b 6e 6f 77 6e  mprintf("unknown
2cb16 20 74 6f 6b 65 6e 69 7a 65 72 3a 20 25 73 22 2c   tokenizer: %s",
2cb17 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 73   zName);.      s
2cb18 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
2cb19 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72  ror(context, zEr
2cb1a 72 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 73 71  r, -1);.      sq
2cb1b 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72 72 29  lite3_free(zErr)
2cb1c 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
2cb1d 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c      }.  }..  sql
2cb1e 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
2cb1f 28 63 6f 6e 74 65 78 74 2c 20 28 76 6f 69 64 20  (context, (void 
2cb20 2a 29 26 70 50 74 72 2c 20 73 69 7a 65 6f 66 28  *)&pPtr, sizeof(
2cb21 70 50 74 72 29 2c 20 53 51 4c 49 54 45 5f 54 52  pPtr), SQLITE_TR
2cb22 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 0a 23 69 66  ANSIENT);.}..#if
2cb23 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
2cb24 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
2cb25 74 61 74 69 6f 6e 20 6f 66 20 61 20 73 70 65 63  tation of a spec
2cb26 69 61 6c 20 53 51 4c 20 73 63 61 6c 61 72 20 66  ial SQL scalar f
2cb27 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 65 73 74  unction for test
2cb28 69 6e 67 20 74 6f 6b 65 6e 69 7a 65 72 73 20 0a  ing tokenizers .
2cb29 2a 2a 20 64 65 73 69 67 6e 65 64 20 74 6f 20 62  ** designed to b
2cb2a 65 20 75 73 65 64 20 69 6e 20 63 6f 6e 63 65 72  e used in concer
2cb2b 74 20 77 69 74 68 20 74 68 65 20 54 63 6c 20 74  t with the Tcl t
2cb2c 65 73 74 69 6e 67 20 66 72 61 6d 65 77 6f 72 6b  esting framework
2cb2d 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69  . This.** functi
2cb2e 6f 6e 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65  on must be calle
2cb2f 64 20 77 69 74 68 20 74 77 6f 20 61 72 67 75 6d  d with two argum
2cb30 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45  ents:.**.**   SE
2cb31 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e  LECT <function-n
2cb32 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d 65 3e 2c  ame>(<key-name>,
2cb33 20 3c 69 6e 70 75 74 2d 73 74 72 69 6e 67 3e 29   <input-string>)
2cb34 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c 66  ;.**   SELECT <f
2cb35 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c 6b  unction-name>(<k
2cb36 65 79 2d 6e 61 6d 65 3e 2c 20 3c 70 6f 69 6e 74  ey-name>, <point
2cb37 65 72 3e 29 3b 0a 2a 2a 0a 2a 2a 20 77 68 65 72  er>);.**.** wher
2cb38 65 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65  e <function-name
2cb39 3e 20 69 73 20 74 68 65 20 6e 61 6d 65 20 70 61  > is the name pa
2cb3a 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f  ssed as the seco
2cb3b 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74  nd argument.** t
2cb3c 6f 20 74 68 65 20 73 71 6c 69 74 65 33 46 74 73  o the sqlite3Fts
2cb3d 33 49 6e 69 74 48 61 73 68 54 61 62 6c 65 28 29  3InitHashTable()
2cb3e 20 66 75 6e 63 74 69 6f 6e 20 28 65 2e 67 2e 20   function (e.g. 
2cb3f 27 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 27  'fts3_tokenizer'
2cb40 29 0a 2a 2a 20 63 6f 6e 63 61 74 65 6e 61 74 65  ).** concatenate
2cb41 64 20 77 69 74 68 20 74 68 65 20 73 74 72 69 6e  d with the strin
2cb42 67 20 27 5f 74 65 73 74 27 20 28 65 2e 67 2e 20  g '_test' (e.g. 
2cb43 27 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  'fts3_tokenizer_
2cb44 74 65 73 74 27 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  test')..**.** Th
2cb45 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69  e return value i
2cb46 73 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20  s a string that 
2cb47 6d 61 79 20 62 65 20 69 6e 74 65 72 70 72 65 74  may be interpret
2cb48 65 64 20 61 73 20 61 20 54 63 6c 0a 2a 2a 20 6c  ed as a Tcl.** l
2cb49 69 73 74 2e 20 46 6f 72 20 65 61 63 68 20 74 6f  ist. For each to
2cb4a 6b 65 6e 20 69 6e 20 74 68 65 20 3c 69 6e 70 75  ken in the <inpu
2cb4b 74 2d 73 74 72 69 6e 67 3e 2c 20 74 68 72 65 65  t-string>, three
2cb4c 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a   elements are.**
2cb4d 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 72 65   added to the re
2cb4e 74 75 72 6e 65 64 20 6c 69 73 74 2e 20 54 68 65  turned list. The
2cb4f 20 66 69 72 73 74 20 69 73 20 74 68 65 20 74 6f   first is the to
2cb50 6b 65 6e 20 70 6f 73 69 74 69 6f 6e 2c 20 74 68  ken position, th
2cb51 65 20 0a 2a 2a 20 73 65 63 6f 6e 64 20 69 73 20  e .** second is 
2cb52 74 68 65 20 74 6f 6b 65 6e 20 74 65 78 74 20 28  the token text (
2cb53 66 6f 6c 64 65 64 2c 20 73 74 65 6d 6d 65 64 2c  folded, stemmed,
2cb54 20 65 74 63 2e 29 20 61 6e 64 20 74 68 65 20 74   etc.) and the t
2cb55 68 69 72 64 20 69 73 20 74 68 65 0a 2a 2a 20 73  hird is the.** s
2cb56 75 62 73 74 72 69 6e 67 20 6f 66 20 3c 69 6e 70  ubstring of <inp
2cb57 75 74 2d 73 74 72 69 6e 67 3e 20 61 73 73 6f 63  ut-string> assoc
2cb58 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 74  iated with the t
2cb59 6f 6b 65 6e 2e 20 46 6f 72 20 65 78 61 6d 70 6c  oken. For exampl
2cb5a 65 2c 20 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65  e, .** using the
2cb5b 20 62 75 69 6c 74 2d 69 6e 20 22 73 69 6d 70 6c   built-in "simpl
2cb5c 65 22 20 74 6f 6b 65 6e 69 7a 65 72 3a 0a 2a 2a  e" tokenizer:.**
2cb5d 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 66 74 73  .**   SELECT fts
2cb5e 5f 74 6f 6b 65 6e 69 7a 65 72 5f 74 65 73 74 28  _tokenizer_test(
2cb5f 27 73 69 6d 70 6c 65 27 2c 20 27 49 20 64 6f 6e  'simple', 'I don
2cb60 27 74 20 73 65 65 20 68 6f 77 27 29 3b 0a 2a 2a  't see how');.**
2cb61 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75 72 6e 20  .** will return 
2cb62 74 68 65 20 73 74 72 69 6e 67 3a 0a 2a 2a 0a 2a  the string:.**.*
2cb63 2a 20 20 20 22 7b 30 20 69 20 49 20 31 20 64 6f  *   "{0 i I 1 do
2cb64 6e 74 20 64 6f 6e 27 74 20 32 20 73 65 65 20 73  nt don't 2 see s
2cb65 65 65 20 33 20 68 6f 77 20 68 6f 77 7d 22 0a 2a  ee 3 how how}".*
2cb66 2a 20 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76  *   .*/.static v
2cb67 6f 69 64 20 74 65 73 74 46 75 6e 63 28 0a 20 20  oid testFunc(.  
2cb68 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
2cb69 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
2cb6a 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
2cb6b 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
2cb6c 20 20 66 74 73 33 48 61 73 68 20 2a 70 48 61 73    fts3Hash *pHas
2cb6d 68 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  h;.  sqlite3_tok
2cb6e 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70  enizer_module *p
2cb6f 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  ;.  sqlite3_toke
2cb70 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65  nizer *pTokenize
2cb71 72 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  r = 0;.  sqlite3
2cb72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
2cb73 72 20 2a 70 43 73 72 20 3d 20 30 3b 0a 0a 20 20  r *pCsr = 0;..  
2cb74 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 72 72  const char *zErr
2cb75 20 3d 20 30 3b 0a 0a 20 20 63 6f 6e 73 74 20 63   = 0;..  const c
2cb76 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69 6e  har *zName;.  in
2cb77 74 20 6e 4e 61 6d 65 3b 0a 20 20 63 6f 6e 73 74  t nName;.  const
2cb78 20 63 68 61 72 20 2a 7a 49 6e 70 75 74 3b 0a 20   char *zInput;. 
2cb79 20 69 6e 74 20 6e 49 6e 70 75 74 3b 0a 0a 20 20   int nInput;..  
2cb7a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67  const char *zArg
2cb7b 20 3d 20 30 3b 0a 0a 20 20 63 6f 6e 73 74 20 63   = 0;..  const c
2cb7c 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 0a 20 20 69  har *zToken;.  i
2cb7d 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74  nt nToken;.  int
2cb7e 20 69 53 74 61 72 74 3b 0a 20 20 69 6e 74 20 69   iStart;.  int i
2cb7f 45 6e 64 3b 0a 20 20 69 6e 74 20 69 50 6f 73 3b  End;.  int iPos;
2cb80 0a 0a 20 20 54 63 6c 5f 4f 62 6a 20 2a 70 52 65  ..  Tcl_Obj *pRe
2cb81 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72  t;..  assert( ar
2cb82 67 63 3d 3d 32 20 7c 7c 20 61 72 67 63 3d 3d 33  gc==2 || argc==3
2cb83 20 29 3b 0a 0a 20 20 6e 4e 61 6d 65 20 3d 20 73   );..  nName = s
2cb84 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
2cb85 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 7a  es(argv[0]);.  z
2cb86 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63 68  Name = (const ch
2cb87 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
2cb88 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
2cb89 3b 0a 20 20 6e 49 6e 70 75 74 20 3d 20 73 71 6c  ;.  nInput = sql
2cb8a 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
2cb8b 28 61 72 67 76 5b 61 72 67 63 2d 31 5d 29 3b 0a  (argv[argc-1]);.
2cb8c 20 20 7a 49 6e 70 75 74 20 3d 20 28 63 6f 6e 73    zInput = (cons
2cb8d 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
2cb8e 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
2cb8f 5b 61 72 67 63 2d 31 5d 29 3b 0a 0a 20 20 69 66  [argc-1]);..  if
2cb90 28 20 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20  ( argc==3 ){.   
2cb91 20 7a 41 72 67 20 3d 20 28 63 6f 6e 73 74 20 63   zArg = (const c
2cb92 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  har *)sqlite3_va
2cb93 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d  lue_text(argv[1]
2cb94 29 3b 0a 20 20 7d 0a 0a 20 20 70 48 61 73 68 20  );.  }..  pHash 
2cb95 3d 20 28 66 74 73 33 48 61 73 68 20 2a 29 73 71  = (fts3Hash *)sq
2cb96 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28  lite3_user_data(
2cb97 63 6f 6e 74 65 78 74 29 3b 0a 20 20 70 20 3d 20  context);.  p = 
2cb98 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a  (sqlite3_tokeniz
2cb99 65 72 5f 6d 6f 64 75 6c 65 20 2a 29 73 71 6c 69  er_module *)sqli
2cb9a 74 65 33 46 74 73 33 48 61 73 68 46 69 6e 64 28  te3Fts3HashFind(
2cb9b 70 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e  pHash, zName, nN
2cb9c 61 6d 65 2b 31 29 3b 0a 0a 20 20 69 66 28 20 21  ame+1);..  if( !
2cb9d 70 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a  p ){.    char *z
2cb9e 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Err = sqlite3_mp
2cb9f 72 69 6e 74 66 28 22 75 6e 6b 6e 6f 77 6e 20 74  rintf("unknown t
2cba0 6f 6b 65 6e 69 7a 65 72 3a 20 25 73 22 2c 20 7a  okenizer: %s", z
2cba1 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  Name);.    sqlit
2cba2 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
2cba3 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d  context, zErr, -
2cba4 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  1);.    sqlite3_
2cba5 66 72 65 65 28 7a 45 72 72 29 3b 0a 20 20 20 20  free(zErr);.    
2cba6 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 70  return;.  }..  p
2cba7 52 65 74 20 3d 20 54 63 6c 5f 4e 65 77 4f 62 6a  Ret = Tcl_NewObj
2cba8 28 29 3b 0a 20 20 54 63 6c 5f 49 6e 63 72 52 65  ();.  Tcl_IncrRe
2cba9 66 43 6f 75 6e 74 28 70 52 65 74 29 3b 0a 0a 20  fCount(pRet);.. 
2cbaa 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
2cbab 70 2d 3e 78 43 72 65 61 74 65 28 7a 41 72 67 20  p->xCreate(zArg 
2cbac 3f 20 31 20 3a 20 30 2c 20 26 7a 41 72 67 2c 20  ? 1 : 0, &zArg, 
2cbad 26 70 54 6f 6b 65 6e 69 7a 65 72 29 20 29 7b 0a  &pTokenizer) ){.
2cbae 20 20 20 20 7a 45 72 72 20 3d 20 22 65 72 72 6f      zErr = "erro
2cbaf 72 20 69 6e 20 78 43 72 65 61 74 65 28 29 22 3b  r in xCreate()";
2cbb0 0a 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 68  .    goto finish
2cbb1 3b 0a 20 20 7d 0a 20 20 70 54 6f 6b 65 6e 69 7a  ;.  }.  pTokeniz
2cbb2 65 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 3b  er->pModule = p;
2cbb3 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
2cbb4 21 3d 70 2d 3e 78 4f 70 65 6e 28 70 54 6f 6b 65  !=p->xOpen(pToke
2cbb5 6e 69 7a 65 72 2c 20 7a 49 6e 70 75 74 2c 20 6e  nizer, zInput, n
2cbb6 49 6e 70 75 74 2c 20 26 70 43 73 72 29 20 29 7b  Input, &pCsr) ){
2cbb7 0a 20 20 20 20 7a 45 72 72 20 3d 20 22 65 72 72  .    zErr = "err
2cbb8 6f 72 20 69 6e 20 78 4f 70 65 6e 28 29 22 3b 0a  or in xOpen()";.
2cbb9 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 68 3b      goto finish;
2cbba 0a 20 20 7d 0a 20 20 70 43 73 72 2d 3e 70 54 6f  .  }.  pCsr->pTo
2cbbb 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e  kenizer = pToken
2cbbc 69 7a 65 72 3b 0a 0a 20 20 77 68 69 6c 65 28 20  izer;..  while( 
2cbbd 53 51 4c 49 54 45 5f 4f 4b 3d 3d 70 2d 3e 78 4e  SQLITE_OK==p->xN
2cbbe 65 78 74 28 70 43 73 72 2c 20 26 7a 54 6f 6b 65  ext(pCsr, &zToke
2cbbf 6e 2c 20 26 6e 54 6f 6b 65 6e 2c 20 26 69 53 74  n, &nToken, &iSt
2cbc0 61 72 74 2c 20 26 69 45 6e 64 2c 20 26 69 50 6f  art, &iEnd, &iPo
2cbc1 73 29 20 29 7b 0a 20 20 20 20 54 63 6c 5f 4c 69  s) ){.    Tcl_Li
2cbc2 73 74 4f 62 6a 41 70 70 65 6e 64 45 6c 65 6d 65  stObjAppendEleme
2cbc3 6e 74 28 30 2c 20 70 52 65 74 2c 20 54 63 6c 5f  nt(0, pRet, Tcl_
2cbc4 4e 65 77 49 6e 74 4f 62 6a 28 69 50 6f 73 29 29  NewIntObj(iPos))
2cbc5 3b 0a 20 20 20 20 54 63 6c 5f 4c 69 73 74 4f 62  ;.    Tcl_ListOb
2cbc6 6a 41 70 70 65 6e 64 45 6c 65 6d 65 6e 74 28 30  jAppendElement(0
2cbc7 2c 20 70 52 65 74 2c 20 54 63 6c 5f 4e 65 77 53  , pRet, Tcl_NewS
2cbc8 74 72 69 6e 67 4f 62 6a 28 7a 54 6f 6b 65 6e 2c  tringObj(zToken,
2cbc9 20 6e 54 6f 6b 65 6e 29 29 3b 0a 20 20 20 20 7a   nToken));.    z
2cbca 54 6f 6b 65 6e 20 3d 20 26 7a 49 6e 70 75 74 5b  Token = &zInput[
2cbcb 69 53 74 61 72 74 5d 3b 0a 20 20 20 20 6e 54 6f  iStart];.    nTo
2cbcc 6b 65 6e 20 3d 20 69 45 6e 64 2d 69 53 74 61 72  ken = iEnd-iStar
2cbcd 74 3b 0a 20 20 20 20 54 63 6c 5f 4c 69 73 74 4f  t;.    Tcl_ListO
2cbce 62 6a 41 70 70 65 6e 64 45 6c 65 6d 65 6e 74 28  bjAppendElement(
2cbcf 30 2c 20 70 52 65 74 2c 20 54 63 6c 5f 4e 65 77  0, pRet, Tcl_New
2cbd0 53 74 72 69 6e 67 4f 62 6a 28 7a 54 6f 6b 65 6e  StringObj(zToken
2cbd1 2c 20 6e 54 6f 6b 65 6e 29 29 3b 0a 20 20 7d 0a  , nToken));.  }.
2cbd2 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
2cbd3 21 3d 70 2d 3e 78 43 6c 6f 73 65 28 70 43 73 72  !=p->xClose(pCsr
2cbd4 29 20 29 7b 0a 20 20 20 20 7a 45 72 72 20 3d 20  ) ){.    zErr = 
2cbd5 22 65 72 72 6f 72 20 69 6e 20 78 43 6c 6f 73 65  "error in xClose
2cbd6 28 29 22 3b 0a 20 20 20 20 67 6f 74 6f 20 66 69  ()";.    goto fi
2cbd7 6e 69 73 68 3b 0a 20 20 7d 0a 20 20 69 66 28 20  nish;.  }.  if( 
2cbd8 53 51 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78 44  SQLITE_OK!=p->xD
2cbd9 65 73 74 72 6f 79 28 70 54 6f 6b 65 6e 69 7a 65  estroy(pTokenize
2cbda 72 29 20 29 7b 0a 20 20 20 20 7a 45 72 72 20 3d  r) ){.    zErr =
2cbdb 20 22 65 72 72 6f 72 20 69 6e 20 78 44 65 73 74   "error in xDest
2cbdc 72 6f 79 28 29 22 3b 0a 20 20 20 20 67 6f 74 6f  roy()";.    goto
2cbdd 20 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 0a 66 69   finish;.  }..fi
2cbde 6e 69 73 68 3a 0a 20 20 69 66 28 20 7a 45 72 72  nish:.  if( zErr
2cbdf 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
2cbe0 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e  result_error(con
2cbe1 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b  text, zErr, -1);
2cbe2 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
2cbe3 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
2cbe4 74 28 63 6f 6e 74 65 78 74 2c 20 54 63 6c 5f 47  t(context, Tcl_G
2cbe5 65 74 53 74 72 69 6e 67 28 70 52 65 74 29 2c 20  etString(pRet), 
2cbe6 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  -1, SQLITE_TRANS
2cbe7 49 45 4e 54 29 3b 0a 20 20 7d 0a 20 20 54 63 6c  IENT);.  }.  Tcl
2cbe8 5f 44 65 63 72 52 65 66 43 6f 75 6e 74 28 70 52  _DecrRefCount(pR
2cbe9 65 74 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 0a 69  et);.}..static.i
2cbea 6e 74 20 72 65 67 69 73 74 65 72 54 6f 6b 65 6e  nt registerToken
2cbeb 69 7a 65 72 28 0a 20 20 73 71 6c 69 74 65 33 20  izer(.  sqlite3 
2cbec 2a 64 62 2c 20 0a 20 20 63 68 61 72 20 2a 7a 4e  *db, .  char *zN
2cbed 61 6d 65 2c 20 0a 20 20 63 6f 6e 73 74 20 73 71  ame, .  const sq
2cbee 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
2cbef 6d 6f 64 75 6c 65 20 2a 70 0a 29 7b 0a 20 20 69  module *p.){.  i
2cbf0 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33  nt rc;.  sqlite3
2cbf1 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20  _stmt *pStmt;.  
2cbf2 63 6f 6e 73 74 20 63 68 61 72 20 7a 53 71 6c 5b  const char zSql[
2cbf3 5d 20 3d 20 22 53 45 4c 45 43 54 20 66 74 73 33  ] = "SELECT fts3
2cbf4 5f 74 6f 6b 65 6e 69 7a 65 72 28 3f 2c 20 3f 29  _tokenizer(?, ?)
2cbf5 22 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  ";..  rc = sqlit
2cbf6 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62  e3_prepare_v2(db
2cbf7 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74  , zSql, -1, &pSt
2cbf8 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63  mt, 0);.  if( rc
2cbf9 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
2cbfa 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
2cbfb 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  }..  sqlite3_bin
2cbfc 64 5f 74 65 78 74 28 70 53 74 6d 74 2c 20 31 2c  d_text(pStmt, 1,
2cbfd 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53 51 4c 49   zName, -1, SQLI
2cbfe 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 73 71  TE_STATIC);.  sq
2cbff 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28  lite3_bind_blob(
2cc00 70 53 74 6d 74 2c 20 32 2c 20 26 70 2c 20 73 69  pStmt, 2, &p, si
2cc01 7a 65 6f 66 28 70 29 2c 20 53 51 4c 49 54 45 5f  zeof(p), SQLITE_
2cc02 53 54 41 54 49 43 29 3b 0a 20 20 73 71 6c 69 74  STATIC);.  sqlit
2cc03 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 3b 0a  e3_step(pStmt);.
2cc04 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
2cc05 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74  3_finalize(pStmt
2cc06 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 0a 69 6e 74  );.}..static.int
2cc07 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72 28   queryTokenizer(
2cc08 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
2cc09 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  .  char *zName, 
2cc0a 20 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65   .  const sqlite
2cc0b 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
2cc0c 6c 65 20 2a 2a 70 70 0a 29 7b 0a 20 20 69 6e 74  le **pp.){.  int
2cc0d 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73   rc;.  sqlite3_s
2cc0e 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 63 6f  tmt *pStmt;.  co
2cc0f 6e 73 74 20 63 68 61 72 20 7a 53 71 6c 5b 5d 20  nst char zSql[] 
2cc10 3d 20 22 53 45 4c 45 43 54 20 66 74 73 33 5f 74  = "SELECT fts3_t
2cc11 6f 6b 65 6e 69 7a 65 72 28 3f 29 22 3b 0a 0a 20  okenizer(?)";.. 
2cc12 20 2a 70 70 20 3d 20 30 3b 0a 20 20 72 63 20 3d   *pp = 0;.  rc =
2cc13 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
2cc14 5f 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31  _v2(db, zSql, -1
2cc15 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20  , &pStmt, 0);.  
2cc16 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2cc17 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  K ){.    return 
2cc18 72 63 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74  rc;.  }..  sqlit
2cc19 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 70 53 74  e3_bind_text(pSt
2cc1a 6d 74 2c 20 31 2c 20 7a 4e 61 6d 65 2c 20 2d 31  mt, 1, zName, -1
2cc1b 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
2cc1c 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 52  ;.  if( SQLITE_R
2cc1d 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70  OW==sqlite3_step
2cc1e 28 70 53 74 6d 74 29 20 29 7b 0a 20 20 20 20 69  (pStmt) ){.    i
2cc1f 66 28 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  f( sqlite3_colum
2cc20 6e 5f 74 79 70 65 28 70 53 74 6d 74 2c 20 30 29  n_type(pStmt, 0)
2cc21 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b  ==SQLITE_BLOB ){
2cc22 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 70  .      memcpy(pp
2cc23 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  , sqlite3_column
2cc24 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 30 29 2c  _blob(pStmt, 0),
2cc25 20 73 69 7a 65 6f 66 28 2a 70 70 29 29 3b 0a 20   sizeof(*pp));. 
2cc26 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
2cc27 72 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  rn sqlite3_final
2cc28 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 53  ize(pStmt);.}..S
2cc29 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2cc2a 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 53 69  id sqlite3Fts3Si
2cc2b 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64  mpleTokenizerMod
2cc2c 75 6c 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65  ule(sqlite3_toke
2cc2d 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e  nizer_module con
2cc2e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a 0a  st**ppModule);..
2cc2f 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
2cc30 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 63 61 6c  tion of the scal
2cc31 61 72 20 66 75 6e 63 74 69 6f 6e 20 66 74 73 33  ar function fts3
2cc32 5f 74 6f 6b 65 6e 69 7a 65 72 5f 69 6e 74 65 72  _tokenizer_inter
2cc33 6e 61 6c 5f 74 65 73 74 28 29 2e 0a 2a 2a 20 54  nal_test()..** T
2cc34 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
2cc35 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  used for testing
2cc36 20 6f 6e 6c 79 2c 20 69 74 20 69 73 20 6e 6f 74   only, it is not
2cc37 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65   included in the
2cc38 0a 2a 2a 20 62 75 69 6c 64 20 75 6e 6c 65 73 73  .** build unless
2cc39 20 53 51 4c 49 54 45 5f 54 45 53 54 20 69 73 20   SQLITE_TEST is 
2cc3a 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  defined..**.** T
2cc3b 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68  he purpose of th
2cc3c 69 73 20 69 73 20 74 6f 20 74 65 73 74 20 74 68  is is to test th
2cc3d 61 74 20 74 68 65 20 66 74 73 33 5f 74 6f 6b 65  at the fts3_toke
2cc3e 6e 69 7a 65 72 28 29 20 66 75 6e 63 74 69 6f 6e  nizer() function
2cc3f 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20  .** can be used 
2cc40 61 73 20 64 65 73 69 67 6e 65 64 20 62 79 20 74  as designed by t
2cc41 68 65 20 43 2d 63 6f 64 65 20 69 6e 20 74 68 65  he C-code in the
2cc42 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72 20   queryTokenizer 
2cc43 61 6e 64 0a 2a 2a 20 72 65 67 69 73 74 65 72 54  and.** registerT
2cc44 6f 6b 65 6e 69 7a 65 72 28 29 20 66 75 6e 63 74  okenizer() funct
2cc45 69 6f 6e 73 20 61 62 6f 76 65 2e 20 54 68 65 73  ions above. Thes
2cc46 65 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20  e two functions 
2cc47 61 72 65 20 72 65 70 65 61 74 65 64 0a 2a 2a 20  are repeated.** 
2cc48 69 6e 20 74 68 65 20 52 45 41 44 4d 45 2e 74 6f  in the README.to
2cc49 6b 65 6e 69 7a 65 72 20 66 69 6c 65 20 61 73 20  kenizer file as 
2cc4a 61 6e 20 65 78 61 6d 70 6c 65 2c 20 73 6f 20 69  an example, so i
2cc4b 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74  t is important t
2cc4c 6f 0a 2a 2a 20 74 65 73 74 20 74 68 65 6d 2e 0a  o.** test them..
2cc4d 2a 2a 0a 2a 2a 20 54 6f 20 72 75 6e 20 74 68 65  **.** To run the
2cc4e 20 74 65 73 74 73 2c 20 65 76 61 6c 75 61 74 65   tests, evaluate
2cc4f 20 74 68 65 20 66 74 73 33 5f 74 6f 6b 65 6e 69   the fts3_tokeni
2cc50 7a 65 72 5f 69 6e 74 65 72 6e 61 6c 5f 74 65 73  zer_internal_tes
2cc51 74 28 29 20 73 63 61 6c 61 72 0a 2a 2a 20 66 75  t() scalar.** fu
2cc52 6e 63 74 69 6f 6e 20 77 69 74 68 20 6e 6f 20 61  nction with no a
2cc53 72 67 75 6d 65 6e 74 73 2e 20 41 6e 20 61 73 73  rguments. An ass
2cc54 65 72 74 28 29 20 77 69 6c 6c 20 66 61 69 6c 20  ert() will fail 
2cc55 69 66 20 61 20 70 72 6f 62 6c 65 6d 20 69 73 0a  if a problem is.
2cc56 2a 2a 20 64 65 74 65 63 74 65 64 2e 20 69 2e 65  ** detected. i.e
2cc57 2e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c  .:.**.**     SEL
2cc58 45 43 54 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  ECT fts3_tokeniz
2cc59 65 72 5f 69 6e 74 65 72 6e 61 6c 5f 74 65 73 74  er_internal_test
2cc5a 28 29 3b 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63  ();.**.*/.static
2cc5b 20 76 6f 69 64 20 69 6e 74 54 65 73 74 46 75 6e   void intTestFun
2cc5c 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
2cc5d 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
2cc5e 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
2cc5f 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
2cc60 76 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  v.){.  int rc;. 
2cc61 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74   const sqlite3_t
2cc62 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
2cc63 2a 70 31 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c  *p1;.  const sql
2cc64 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
2cc65 6f 64 75 6c 65 20 2a 70 32 3b 0a 20 20 73 71 6c  odule *p2;.  sql
2cc66 69 74 65 33 20 2a 64 62 20 3d 20 28 73 71 6c 69  ite3 *db = (sqli
2cc67 74 65 33 20 2a 29 73 71 6c 69 74 65 33 5f 75 73  te3 *)sqlite3_us
2cc68 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29  er_data(context)
2cc69 3b 0a 0a 20 20 2f 2a 20 54 65 73 74 20 74 68 65  ;..  /* Test the
2cc6a 20 71 75 65 72 79 20 66 75 6e 63 74 69 6f 6e 20   query function 
2cc6b 2a 2f 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33  */.  sqlite3Fts3
2cc6c 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d  SimpleTokenizerM
2cc6d 6f 64 75 6c 65 28 26 70 31 29 3b 0a 20 20 72 63  odule(&p1);.  rc
2cc6e 20 3d 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a 65   = queryTokenize
2cc6f 72 28 64 62 2c 20 22 73 69 6d 70 6c 65 22 2c 20  r(db, "simple", 
2cc70 26 70 32 29 3b 0a 20 20 61 73 73 65 72 74 28 20  &p2);.  assert( 
2cc71 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
2cc72 0a 20 20 61 73 73 65 72 74 28 20 70 31 3d 3d 70  .  assert( p1==p
2cc73 32 20 29 3b 0a 20 20 72 63 20 3d 20 71 75 65 72  2 );.  rc = quer
2cc74 79 54 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 20 22  yTokenizer(db, "
2cc75 6e 6f 73 75 63 68 74 6f 6b 65 6e 69 7a 65 72 22  nosuchtokenizer"
2cc76 2c 20 26 70 32 29 3b 0a 20 20 61 73 73 65 72 74  , &p2);.  assert
2cc77 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 52 52  ( rc==SQLITE_ERR
2cc78 4f 52 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  OR );.  assert( 
2cc79 70 32 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  p2==0 );.  asser
2cc7a 74 28 20 30 3d 3d 73 74 72 63 6d 70 28 73 71 6c  t( 0==strcmp(sql
2cc7b 69 74 65 33 5f 65 72 72 6d 73 67 28 64 62 29 2c  ite3_errmsg(db),
2cc7c 20 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e 69   "unknown tokeni
2cc7d 7a 65 72 3a 20 6e 6f 73 75 63 68 74 6f 6b 65 6e  zer: nosuchtoken
2cc7e 69 7a 65 72 22 29 20 29 3b 0a 0a 20 20 2f 2a 20  izer") );..  /* 
2cc7f 54 65 73 74 20 74 68 65 20 73 74 6f 72 61 67 65  Test the storage
2cc80 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 72   function */.  r
2cc81 63 20 3d 20 72 65 67 69 73 74 65 72 54 6f 6b 65  c = registerToke
2cc82 6e 69 7a 65 72 28 64 62 2c 20 22 6e 6f 73 75 63  nizer(db, "nosuc
2cc83 68 74 6f 6b 65 6e 69 7a 65 72 22 2c 20 70 31 29  htokenizer", p1)
2cc84 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  ;.  assert( rc==
2cc85 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 72  SQLITE_OK );.  r
2cc86 63 20 3d 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a  c = queryTokeniz
2cc87 65 72 28 64 62 2c 20 22 6e 6f 73 75 63 68 74 6f  er(db, "nosuchto
2cc88 6b 65 6e 69 7a 65 72 22 2c 20 26 70 32 29 3b 0a  kenizer", &p2);.
2cc89 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
2cc8a 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 61 73 73  LITE_OK );.  ass
2cc8b 65 72 74 28 20 70 32 3d 3d 70 31 20 29 3b 0a 0a  ert( p2==p1 );..
2cc8c 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
2cc8d 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 22  _text(context, "
2cc8e 6f 6b 22 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  ok", -1, SQLITE_
2cc8f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 23 65 6e 64  STATIC);.}..#end
2cc90 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75 70  if../*.** Set up
2cc91 20 53 51 4c 20 6f 62 6a 65 63 74 73 20 69 6e 20   SQL objects in 
2cc92 64 61 74 61 62 61 73 65 20 64 62 20 75 73 65 64  database db used
2cc93 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 63   to access the c
2cc94 6f 6e 74 65 6e 74 73 20 6f 66 0a 2a 2a 20 74 68  ontents of.** th
2cc95 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 6f 69  e hash table poi
2cc96 6e 74 65 64 20 74 6f 20 62 79 20 61 72 67 75 6d  nted to by argum
2cc97 65 6e 74 20 70 48 61 73 68 2e 20 54 68 65 20 68  ent pHash. The h
2cc98 61 73 68 20 74 61 62 6c 65 20 6d 75 73 74 0a 2a  ash table must.*
2cc99 2a 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 73  * been initialis
2cc9a 65 64 20 74 6f 20 75 73 65 20 73 74 72 69 6e 67  ed to use string
2cc9b 20 6b 65 79 73 2c 20 61 6e 64 20 74 6f 20 74 61   keys, and to ta
2cc9c 6b 65 20 61 20 70 72 69 76 61 74 65 20 63 6f 70  ke a private cop
2cc9d 79 20 0a 2a 2a 20 6f 66 20 74 68 65 20 6b 65 79  y .** of the key
2cc9e 20 77 68 65 6e 20 61 20 76 61 6c 75 65 20 69 73   when a value is
2cc9f 20 69 6e 73 65 72 74 65 64 2e 20 69 2e 65 2e 20   inserted. i.e. 
2cca0 62 79 20 61 20 63 61 6c 6c 20 73 69 6d 69 6c 61  by a call simila
2cca1 72 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 73  r to:.**.**    s
2cca2 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e  qlite3Fts3HashIn
2cca3 69 74 28 70 48 61 73 68 2c 20 46 54 53 33 5f 48  it(pHash, FTS3_H
2cca4 41 53 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0a  ASH_STRING, 1);.
2cca5 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
2cca6 69 6f 6e 20 61 64 64 73 20 61 20 73 63 61 6c 61  ion adds a scala
2cca7 72 20 66 75 6e 63 74 69 6f 6e 20 28 73 65 65 20  r function (see 
2cca8 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 61  header comment a
2cca9 62 6f 76 65 0a 2a 2a 20 73 63 61 6c 61 72 46 75  bove.** scalarFu
2ccaa 6e 63 28 29 20 69 6e 20 74 68 69 73 20 66 69 6c  nc() in this fil
2ccab 65 20 66 6f 72 20 64 65 74 61 69 6c 73 29 20 61  e for details) a
2ccac 6e 64 2c 20 69 66 20 45 4e 41 42 4c 45 5f 54 41  nd, if ENABLE_TA
2ccad 42 4c 45 20 69 73 0a 2a 2a 20 64 65 66 69 6e 65  BLE is.** define
2ccae 64 20 61 74 20 63 6f 6d 70 69 6c 61 74 69 6f 6e  d at compilation
2ccaf 20 74 69 6d 65 2c 20 61 20 74 65 6d 70 6f 72 61   time, a tempora
2ccb0 72 79 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ry virtual table
2ccb1 20 28 73 65 65 20 68 65 61 64 65 72 20 0a 2a 2a   (see header .**
2ccb2 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 20 73   comment above s
2ccb3 74 72 75 63 74 20 48 61 73 68 54 61 62 6c 65 56  truct HashTableV
2ccb4 74 61 62 29 20 74 6f 20 74 68 65 20 64 61 74 61  tab) to the data
2ccb5 62 61 73 65 20 73 63 68 65 6d 61 2e 20 42 6f 74  base schema. Bot
2ccb6 68 20 0a 2a 2a 20 70 72 6f 76 69 64 65 20 72 65  h .** provide re
2ccb7 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 20  ad/write access 
2ccb8 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  to the contents 
2ccb9 6f 66 20 2a 70 48 61 73 68 2e 0a 2a 2a 0a 2a 2a  of *pHash..**.**
2ccba 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d   The third argum
2ccbb 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63  ent to this func
2ccbc 74 69 6f 6e 2c 20 7a 4e 61 6d 65 2c 20 69 73 20  tion, zName, is 
2ccbd 75 73 65 64 20 61 73 20 74 68 65 20 6e 61 6d 65  used as the name
2ccbe 0a 2a 2a 20 6f 66 20 62 6f 74 68 20 74 68 65 20  .** of both the 
2ccbf 73 63 61 6c 61 72 20 61 6e 64 2c 20 69 66 20 63  scalar and, if c
2ccc0 72 65 61 74 65 64 2c 20 74 68 65 20 76 69 72 74  reated, the virt
2ccc1 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51  ual table..*/.SQ
2ccc2 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2ccc3 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74   sqlite3Fts3Init
2ccc4 48 61 73 68 54 61 62 6c 65 28 0a 20 20 73 71 6c  HashTable(.  sql
2ccc5 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 66 74 73  ite3 *db, .  fts
2ccc6 33 48 61 73 68 20 2a 70 48 61 73 68 2c 20 0a 20  3Hash *pHash, . 
2ccc7 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
2ccc8 6d 65 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  me.){.  int rc =
2ccc9 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 76 6f   SQLITE_OK;.  vo
2ccca 69 64 20 2a 70 20 3d 20 28 76 6f 69 64 20 2a 29  id *p = (void *)
2cccb 70 48 61 73 68 3b 0a 20 20 63 6f 6e 73 74 20 69  pHash;.  const i
2cccc 6e 74 20 61 6e 79 20 3d 20 53 51 4c 49 54 45 5f  nt any = SQLITE_
2cccd 41 4e 59 3b 0a 20 20 63 68 61 72 20 2a 7a 54 65  ANY;.  char *zTe
2ccce 73 74 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a  st = 0;.  char *
2cccf 7a 54 65 73 74 32 20 3d 20 30 3b 0a 0a 23 69 66  zTest2 = 0;..#if
2ccd0 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
2ccd1 20 20 76 6f 69 64 20 2a 70 64 62 20 3d 20 28 76    void *pdb = (v
2ccd2 6f 69 64 20 2a 29 64 62 3b 0a 20 20 7a 54 65 73  oid *)db;.  zTes
2ccd3 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  t = sqlite3_mpri
2ccd4 6e 74 66 28 22 25 73 5f 74 65 73 74 22 2c 20 7a  ntf("%s_test", z
2ccd5 4e 61 6d 65 29 3b 0a 20 20 7a 54 65 73 74 32 20  Name);.  zTest2 
2ccd6 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
2ccd7 66 28 22 25 73 5f 69 6e 74 65 72 6e 61 6c 5f 74  f("%s_internal_t
2ccd8 65 73 74 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  est", zName);.  
2ccd9 69 66 28 20 21 7a 54 65 73 74 20 7c 7c 20 21 7a  if( !zTest || !z
2ccda 54 65 73 74 32 20 29 7b 0a 20 20 20 20 72 63 20  Test2 ){.    rc 
2ccdb 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
2ccdc 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66    }.#endif..  if
2ccdd 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 0a  ( rc!=SQLITE_OK.
2ccde 20 20 20 7c 7c 20 28 72 63 20 3d 20 73 71 6c 69     || (rc = sqli
2ccdf 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
2cce0 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 31  ion(db, zName, 1
2cce1 2c 20 61 6e 79 2c 20 70 2c 20 73 63 61 6c 61 72  , any, p, scalar
2cce2 46 75 6e 63 2c 20 30 2c 20 30 29 29 0a 20 20 20  Func, 0, 0)).   
2cce3 7c 7c 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33  || (rc = sqlite3
2cce4 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
2cce5 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 32 2c 20 61  (db, zName, 2, a
2cce6 6e 79 2c 20 70 2c 20 73 63 61 6c 61 72 46 75 6e  ny, p, scalarFun
2cce7 63 2c 20 30 2c 20 30 29 29 0a 23 69 66 64 65 66  c, 0, 0)).#ifdef
2cce8 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20   SQLITE_TEST.   
2cce9 7c 7c 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33  || (rc = sqlite3
2ccea 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
2cceb 28 64 62 2c 20 7a 54 65 73 74 2c 20 32 2c 20 61  (db, zTest, 2, a
2ccec 6e 79 2c 20 70 2c 20 74 65 73 74 46 75 6e 63 2c  ny, p, testFunc,
2cced 20 30 2c 20 30 29 29 0a 20 20 20 7c 7c 20 28 72   0, 0)).   || (r
2ccee 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61  c = sqlite3_crea
2ccef 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20  te_function(db, 
2ccf0 7a 54 65 73 74 2c 20 33 2c 20 61 6e 79 2c 20 70  zTest, 3, any, p
2ccf1 2c 20 74 65 73 74 46 75 6e 63 2c 20 30 2c 20 30  , testFunc, 0, 0
2ccf2 29 29 0a 20 20 20 7c 7c 20 28 72 63 20 3d 20 73  )).   || (rc = s
2ccf3 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
2ccf4 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 54 65 73 74  nction(db, zTest
2ccf5 32 2c 20 30 2c 20 61 6e 79 2c 20 70 64 62 2c 20  2, 0, any, pdb, 
2ccf6 69 6e 74 54 65 73 74 46 75 6e 63 2c 20 30 2c 20  intTestFunc, 0, 
2ccf7 30 29 29 0a 23 65 6e 64 69 66 0a 20 20 29 3b 0a  0)).#endif.  );.
2ccf8 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
2ccf9 7a 54 65 73 74 29 3b 0a 20 20 73 71 6c 69 74 65  zTest);.  sqlite
2ccfa 33 5f 66 72 65 65 28 7a 54 65 73 74 32 29 3b 0a  3_free(zTest2);.
2ccfb 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
2ccfc 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
2ccfd 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20  ed(SQLITE_CORE) 
2ccfe 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
2ccff 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a  E_ENABLE_FTS3) *
2cd00 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
2cd01 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 74  ** End of fts3_t
2cd02 6f 6b 65 6e 69 7a 65 72 2e 63 20 2a 2a 2a 2a 2a  okenizer.c *****
2cd03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cd04 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cd05 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
2cd06 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74  ** Begin file ft
2cd07 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 31 2e 63 20  s3_tokenizer1.c 
2cd08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cd09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cd0a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4f 63  */./*.** 2006 Oc
2cd0b 74 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  t 10.**.** The a
2cd0c 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
2cd0d 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
2cd0e 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
2cd0f 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
2cd10 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
2cd11 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
2cd12 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
2cd13 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
2cd14 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
2cd15 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
2cd16 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
2cd17 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
2cd18 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
2cd19 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
2cd1a 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
2cd1b 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
2cd1c 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
2cd1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cd1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cd1f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cd20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cd21 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49  ********.**.** I
2cd22 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
2cd23 20 74 68 65 20 22 73 69 6d 70 6c 65 22 20 66 75   the "simple" fu
2cd24 6c 6c 2d 74 65 78 74 2d 73 65 61 72 63 68 20 74  ll-text-search t
2cd25 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2f 0a 0a 2f 2a  okenizer..*/../*
2cd26 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20  .** The code in 
2cd27 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c  this file is onl
2cd28 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a  y compiled if:.*
2cd29 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46  *.**     * The F
2cd2a 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65  TS3 module is be
2cd2b 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61 6e 20  ing built as an 
2cd2c 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20  extension.**    
2cd2d 20 20 20 28 69 6e 20 77 68 69 63 68 20 63 61 73     (in which cas
2cd2e 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 69 73  e SQLITE_CORE is
2cd2f 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 6f   not defined), o
2cd30 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68  r.**.**     * Th
2cd31 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73  e FTS3 module is
2cd32 20 62 65 69 6e 67 20 62 75 69 6c 74 20 69 6e 74   being built int
2cd33 6f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a  o the core of.**
2cd34 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 28 69         SQLite (i
2cd35 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 51 4c  n which case SQL
2cd36 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20  ITE_ENABLE_FTS3 
2cd37 69 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a  is defined)..*/.
2cd38 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
2cd39 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66  ITE_CORE) || def
2cd3a 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
2cd3b 4c 45 5f 46 54 53 33 29 0a 0a 0a 0a 0a 74 79 70  LE_FTS3).....typ
2cd3c 65 64 65 66 20 73 74 72 75 63 74 20 73 69 6d 70  edef struct simp
2cd3d 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 7b 0a 20  le_tokenizer {. 
2cd3e 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
2cd3f 65 72 20 62 61 73 65 3b 0a 20 20 63 68 61 72 20  er base;.  char 
2cd40 64 65 6c 69 6d 5b 31 32 38 5d 3b 20 20 20 20 20  delim[128];     
2cd41 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 20          /* flag 
2cd42 41 53 43 49 49 20 64 65 6c 69 6d 69 74 65 72 73  ASCII delimiters
2cd43 20 2a 2f 0a 7d 20 73 69 6d 70 6c 65 5f 74 6f 6b   */.} simple_tok
2cd44 65 6e 69 7a 65 72 3b 0a 0a 74 79 70 65 64 65 66  enizer;..typedef
2cd45 20 73 74 72 75 63 74 20 73 69 6d 70 6c 65 5f 74   struct simple_t
2cd46 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
2cd47 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  {.  sqlite3_toke
2cd48 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 62 61 73  nizer_cursor bas
2cd49 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  e;.  const char 
2cd4a 2a 70 49 6e 70 75 74 3b 20 20 20 20 20 20 20 20  *pInput;        
2cd4b 20 20 2f 2a 20 69 6e 70 75 74 20 77 65 20 61 72    /* input we ar
2cd4c 65 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 2a 2f 0a  e tokenizing */.
2cd4d 20 20 69 6e 74 20 6e 42 79 74 65 73 3b 20 20 20    int nBytes;   
2cd4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2cd4f 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20 69 6e  * size of the in
2cd50 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 66  put */.  int iOf
2cd51 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20  fset;           
2cd52 20 20 20 20 20 20 2f 2a 20 63 75 72 72 65 6e 74        /* current
2cd53 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 70 49 6e   position in pIn
2cd54 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f  put */.  int iTo
2cd55 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  ken;            
2cd56 20 20 20 20 20 20 2f 2a 20 69 6e 64 65 78 20 6f        /* index o
2cd57 66 20 6e 65 78 74 20 74 6f 6b 65 6e 20 74 6f 20  f next token to 
2cd58 62 65 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a 20  be returned */. 
2cd59 20 63 68 61 72 20 2a 70 54 6f 6b 65 6e 3b 20 20   char *pToken;  
2cd5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2cd5b 20 73 74 6f 72 61 67 65 20 66 6f 72 20 63 75 72   storage for cur
2cd5c 72 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20  rent token */.  
2cd5d 69 6e 74 20 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61  int nTokenAlloca
2cd5e 74 65 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ted;         /* 
2cd5f 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20  space allocated 
2cd60 74 6f 20 7a 54 6f 6b 65 6e 20 62 75 66 66 65 72  to zToken buffer
2cd61 20 2a 2f 0a 7d 20 73 69 6d 70 6c 65 5f 74 6f 6b   */.} simple_tok
2cd62 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 3b 0a 0a  enizer_cursor;..
2cd63 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c  ./* Forward decl
2cd64 61 72 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69  aration */.stati
2cd65 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
2cd66 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
2cd67 20 73 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72   simpleTokenizer
2cd68 4d 6f 64 75 6c 65 3b 0a 0a 73 74 61 74 69 63 20  Module;..static 
2cd69 69 6e 74 20 73 69 6d 70 6c 65 44 65 6c 69 6d 28  int simpleDelim(
2cd6a 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72  simple_tokenizer
2cd6b 20 2a 74 2c 20 75 6e 73 69 67 6e 65 64 20 63 68   *t, unsigned ch
2cd6c 61 72 20 63 29 7b 0a 20 20 72 65 74 75 72 6e 20  ar c){.  return 
2cd6d 63 3c 30 78 38 30 20 26 26 20 74 2d 3e 64 65 6c  c<0x80 && t->del
2cd6e 69 6d 5b 63 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  im[c];.}../*.** 
2cd6f 43 72 65 61 74 65 20 61 20 6e 65 77 20 74 6f 6b  Create a new tok
2cd70 65 6e 69 7a 65 72 20 69 6e 73 74 61 6e 63 65 2e  enizer instance.
2cd71 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
2cd72 69 6d 70 6c 65 43 72 65 61 74 65 28 0a 20 20 69  impleCreate(.  i
2cd73 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63  nt argc, const c
2cd74 68 61 72 20 2a 20 63 6f 6e 73 74 20 2a 61 72 67  har * const *arg
2cd75 76 2c 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  v,.  sqlite3_tok
2cd76 65 6e 69 7a 65 72 20 2a 2a 70 70 54 6f 6b 65 6e  enizer **ppToken
2cd77 69 7a 65 72 0a 29 7b 0a 20 20 73 69 6d 70 6c 65  izer.){.  simple
2cd78 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 74 3b 0a 0a  _tokenizer *t;..
2cd79 20 20 74 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f    t = (simple_to
2cd7a 6b 65 6e 69 7a 65 72 20 2a 29 20 73 71 6c 69 74  kenizer *) sqlit
2cd7b 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66  e3_malloc(sizeof
2cd7c 28 2a 74 29 29 3b 0a 20 20 69 66 28 20 74 3d 3d  (*t));.  if( t==
2cd7d 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 20 53 51  NULL ) return SQ
2cd7e 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 6d 65  LITE_NOMEM;.  me
2cd7f 6d 73 65 74 28 74 2c 20 30 2c 20 73 69 7a 65 6f  mset(t, 0, sizeo
2cd80 66 28 2a 74 29 29 3b 0a 0a 20 20 2f 2a 20 54 4f  f(*t));..  /* TO
2cd81 44 4f 28 73 68 65 73 73 29 20 44 65 6c 69 6d 69  DO(shess) Delimi
2cd82 74 65 72 73 20 6e 65 65 64 20 74 6f 20 72 65 6d  ters need to rem
2cd83 61 69 6e 20 74 68 65 20 73 61 6d 65 20 66 72 6f  ain the same fro
2cd84 6d 20 72 75 6e 20 74 6f 20 72 75 6e 2c 0a 20 20  m run to run,.  
2cd85 2a 2a 20 65 6c 73 65 20 77 65 20 6e 65 65 64 20  ** else we need 
2cd86 74 6f 20 72 65 69 6e 64 65 78 2e 20 20 4f 6e 65  to reindex.  One
2cd87 20 73 6f 6c 75 74 69 6f 6e 20 77 6f 75 6c 64 20   solution would 
2cd88 62 65 20 61 20 6d 65 74 61 2d 74 61 62 6c 65 20  be a meta-table 
2cd89 74 6f 0a 20 20 2a 2a 20 74 72 61 63 6b 20 73 75  to.  ** track su
2cd8a 63 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  ch information i
2cd8b 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20  n the database, 
2cd8c 74 68 65 6e 20 77 65 27 64 20 6f 6e 6c 79 20 77  then we'd only w
2cd8d 61 6e 74 20 74 68 69 73 0a 20 20 2a 2a 20 69 6e  ant this.  ** in
2cd8e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 74 68 65  formation on the
2cd8f 20 69 6e 69 74 69 61 6c 20 63 72 65 61 74 65 2e   initial create.
2cd90 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61 72 67 63  .  */.  if( argc
2cd91 3e 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c  >1 ){.    int i,
2cd92 20 6e 20 3d 20 73 74 72 6c 65 6e 28 61 72 67 76   n = strlen(argv
2cd93 5b 31 5d 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  [1]);.    for(i=
2cd94 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20  0; i<n; i++){.  
2cd95 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
2cd96 72 20 63 68 20 3d 20 61 72 67 76 5b 31 5d 5b 69  r ch = argv[1][i
2cd97 5d 3b 0a 20 20 20 20 20 20 2f 2a 20 57 65 20 65  ];.      /* We e
2cd98 78 70 6c 69 63 69 74 6c 79 20 64 6f 6e 27 74 20  xplicitly don't 
2cd99 73 75 70 70 6f 72 74 20 55 54 46 2d 38 20 64 65  support UTF-8 de
2cd9a 6c 69 6d 69 74 65 72 73 20 66 6f 72 20 6e 6f 77  limiters for now
2cd9b 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 63  . */.      if( c
2cd9c 68 3e 3d 30 78 38 30 20 29 7b 0a 20 20 20 20 20  h>=0x80 ){.     
2cd9d 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
2cd9e 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  t);.        retu
2cd9f 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
2cda0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 74  .      }.      t
2cda1 2d 3e 64 65 6c 69 6d 5b 63 68 5d 20 3d 20 31 3b  ->delim[ch] = 1;
2cda2 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c 73 65 20  .    }.  } else 
2cda3 7b 0a 20 20 20 20 2f 2a 20 4d 61 72 6b 20 6e 6f  {.    /* Mark no
2cda4 6e 2d 61 6c 70 68 61 6e 75 6d 65 72 69 63 20 41  n-alphanumeric A
2cda5 53 43 49 49 20 63 68 61 72 61 63 74 65 72 73 20  SCII characters 
2cda6 61 73 20 64 65 6c 69 6d 69 74 65 72 73 20 2a 2f  as delimiters */
2cda7 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
2cda8 66 6f 72 28 69 3d 31 3b 20 69 3c 30 78 38 30 3b  for(i=1; i<0x80;
2cda9 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 74 2d 3e   i++){.      t->
2cdaa 64 65 6c 69 6d 5b 69 5d 20 3d 20 21 69 73 61 6c  delim[i] = !isal
2cdab 6e 75 6d 28 69 29 3b 0a 20 20 20 20 7d 0a 20 20  num(i);.    }.  
2cdac 7d 0a 0a 20 20 2a 70 70 54 6f 6b 65 6e 69 7a 65  }..  *ppTokenize
2cdad 72 20 3d 20 26 74 2d 3e 62 61 73 65 3b 0a 20 20  r = &t->base;.  
2cdae 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
2cdaf 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72  ;.}../*.** Destr
2cdb0 6f 79 20 61 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a  oy a tokenizer.*
2cdb1 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 69 6d  /.static int sim
2cdb2 70 6c 65 44 65 73 74 72 6f 79 28 73 71 6c 69 74  pleDestroy(sqlit
2cdb3 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54  e3_tokenizer *pT
2cdb4 6f 6b 65 6e 69 7a 65 72 29 7b 0a 20 20 73 71 6c  okenizer){.  sql
2cdb5 69 74 65 33 5f 66 72 65 65 28 70 54 6f 6b 65 6e  ite3_free(pToken
2cdb6 69 7a 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20  izer);.  return 
2cdb7 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
2cdb8 0a 2a 2a 20 50 72 65 70 61 72 65 20 74 6f 20 62  .** Prepare to b
2cdb9 65 67 69 6e 20 74 6f 6b 65 6e 69 7a 69 6e 67 20  egin tokenizing 
2cdba 61 20 70 61 72 74 69 63 75 6c 61 72 20 73 74 72  a particular str
2cdbb 69 6e 67 2e 20 20 54 68 65 20 69 6e 70 75 74 0a  ing.  The input.
2cdbc 2a 2a 20 73 74 72 69 6e 67 20 74 6f 20 62 65 20  ** string to be 
2cdbd 74 6f 6b 65 6e 69 7a 65 64 20 69 73 20 70 49 6e  tokenized is pIn
2cdbe 70 75 74 5b 30 2e 2e 6e 42 79 74 65 73 2d 31 5d  put[0..nBytes-1]
2cdbf 2e 20 20 41 20 63 75 72 73 6f 72 0a 2a 2a 20 75  .  A cursor.** u
2cdc0 73 65 64 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74  sed to increment
2cdc1 61 6c 6c 79 20 74 6f 6b 65 6e 69 7a 65 20 74 68  ally tokenize th
2cdc2 69 73 20 73 74 72 69 6e 67 20 69 73 20 72 65 74  is string is ret
2cdc3 75 72 6e 65 64 20 69 6e 20 0a 2a 2a 20 2a 70 70  urned in .** *pp
2cdc4 43 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69  Cursor..*/.stati
2cdc5 63 20 69 6e 74 20 73 69 6d 70 6c 65 4f 70 65 6e  c int simpleOpen
2cdc6 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  (.  sqlite3_toke
2cdc7 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65  nizer *pTokenize
2cdc8 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  r,         /* Th
2cdc9 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20  e tokenizer */. 
2cdca 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 49 6e   const char *pIn
2cdcb 70 75 74 2c 20 69 6e 74 20 6e 42 79 74 65 73 2c  put, int nBytes,
2cdcc 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e          /* Strin
2cdcd 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65  g to be tokenize
2cdce 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74  d */.  sqlite3_t
2cdcf 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
2cdd0 2a 2a 70 70 43 75 72 73 6f 72 20 20 20 20 2f 2a  **ppCursor    /*
2cdd1 20 4f 55 54 3a 20 54 6f 6b 65 6e 69 7a 61 74 69   OUT: Tokenizati
2cdd2 6f 6e 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a  on cursor */.){.
2cdd3 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a    simple_tokeniz
2cdd4 65 72 5f 63 75 72 73 6f 72 20 2a 63 3b 0a 0a 20  er_cursor *c;.. 
2cdd5 20 63 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f 6b   c = (simple_tok
2cdd6 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29  enizer_cursor *)
2cdd7 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
2cdd8 73 69 7a 65 6f 66 28 2a 63 29 29 3b 0a 20 20 69  sizeof(*c));.  i
2cdd9 66 28 20 63 3d 3d 4e 55 4c 4c 20 29 20 72 65 74  f( c==NULL ) ret
2cdda 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
2cddb 3b 0a 0a 20 20 63 2d 3e 70 49 6e 70 75 74 20 3d  ;..  c->pInput =
2cddc 20 70 49 6e 70 75 74 3b 0a 20 20 69 66 28 20 70   pInput;.  if( p
2cddd 49 6e 70 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20  Input==0 ){.    
2cdde 63 2d 3e 6e 42 79 74 65 73 20 3d 20 30 3b 0a 20  c->nBytes = 0;. 
2cddf 20 7d 65 6c 73 65 20 69 66 28 20 6e 42 79 74 65   }else if( nByte
2cde0 73 3c 30 20 29 7b 0a 20 20 20 20 63 2d 3e 6e 42  s<0 ){.    c->nB
2cde1 79 74 65 73 20 3d 20 28 69 6e 74 29 73 74 72 6c  ytes = (int)strl
2cde2 65 6e 28 70 49 6e 70 75 74 29 3b 0a 20 20 7d 65  en(pInput);.  }e
2cde3 6c 73 65 7b 0a 20 20 20 20 63 2d 3e 6e 42 79 74  lse{.    c->nByt
2cde4 65 73 20 3d 20 6e 42 79 74 65 73 3b 0a 20 20 7d  es = nBytes;.  }
2cde5 0a 20 20 63 2d 3e 69 4f 66 66 73 65 74 20 3d 20  .  c->iOffset = 
2cde6 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
2cde7 20 20 20 2f 2a 20 73 74 61 72 74 20 74 6f 6b 65     /* start toke
2cde8 6e 69 7a 69 6e 67 20 61 74 20 74 68 65 20 62 65  nizing at the be
2cde9 67 69 6e 6e 69 6e 67 20 2a 2f 0a 20 20 63 2d 3e  ginning */.  c->
2cdea 69 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 63 2d  iToken = 0;.  c-
2cdeb 3e 70 54 6f 6b 65 6e 20 3d 20 4e 55 4c 4c 3b 20  >pToken = NULL; 
2cdec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2cded 20 6e 6f 20 73 70 61 63 65 20 61 6c 6c 6f 63 61   no space alloca
2cdee 74 65 64 2c 20 79 65 74 2e 20 2a 2f 0a 20 20 63  ted, yet. */.  c
2cdef 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65  ->nTokenAllocate
2cdf0 64 20 3d 20 30 3b 0a 0a 20 20 2a 70 70 43 75 72  d = 0;..  *ppCur
2cdf1 73 6f 72 20 3d 20 26 63 2d 3e 62 61 73 65 3b 0a  sor = &c->base;.
2cdf2 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2cdf3 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f  OK;.}../*.** Clo
2cdf4 73 65 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f  se a tokenizatio
2cdf5 6e 20 63 75 72 73 6f 72 20 70 72 65 76 69 6f 75  n cursor previou
2cdf6 73 6c 79 20 6f 70 65 6e 65 64 20 62 79 20 61 20  sly opened by a 
2cdf7 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 69 6d 70 6c  call to.** simpl
2cdf8 65 4f 70 65 6e 28 29 20 61 62 6f 76 65 2e 0a 2a  eOpen() above..*
2cdf9 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 69 6d  /.static int sim
2cdfa 70 6c 65 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  pleClose(sqlite3
2cdfb 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
2cdfc 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 73  r *pCursor){.  s
2cdfd 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f  imple_tokenizer_
2cdfe 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 73 69 6d  cursor *c = (sim
2cdff 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  ple_tokenizer_cu
2ce00 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72 3b  rsor *) pCursor;
2ce01 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
2ce02 63 2d 3e 70 54 6f 6b 65 6e 29 3b 0a 20 20 73 71  c->pToken);.  sq
2ce03 6c 69 74 65 33 5f 66 72 65 65 28 63 29 3b 0a 20  lite3_free(c);. 
2ce04 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
2ce05 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72  K;.}../*.** Extr
2ce06 61 63 74 20 74 68 65 20 6e 65 78 74 20 74 6f 6b  act the next tok
2ce07 65 6e 20 66 72 6f 6d 20 61 20 74 6f 6b 65 6e 69  en from a tokeni
2ce08 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72 2e 20 20  zation cursor.  
2ce09 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73 74 0a  The cursor must.
2ce0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65  ** have been ope
2ce0b 6e 65 64 20 62 79 20 61 20 70 72 69 6f 72 20 63  ned by a prior c
2ce0c 61 6c 6c 20 74 6f 20 73 69 6d 70 6c 65 4f 70 65  all to simpleOpe
2ce0d 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  n()..*/.static i
2ce0e 6e 74 20 73 69 6d 70 6c 65 4e 65 78 74 28 0a 20  nt simpleNext(. 
2ce0f 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
2ce10 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  er_cursor *pCurs
2ce11 6f 72 2c 20 20 2f 2a 20 43 75 72 73 6f 72 20 72  or,  /* Cursor r
2ce12 65 74 75 72 6e 65 64 20 62 79 20 73 69 6d 70 6c  eturned by simpl
2ce13 65 4f 70 65 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  eOpen */.  const
2ce14 20 63 68 61 72 20 2a 2a 70 70 54 6f 6b 65 6e 2c   char **ppToken,
2ce15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2ce16 2a 20 4f 55 54 3a 20 2a 70 70 54 6f 6b 65 6e 20  * OUT: *ppToken 
2ce17 69 73 20 74 68 65 20 74 6f 6b 65 6e 20 74 65 78  is the token tex
2ce18 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 42 79  t */.  int *pnBy
2ce19 74 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  tes,            
2ce1a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
2ce1b 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  T: Number of byt
2ce1c 65 73 20 69 6e 20 74 6f 6b 65 6e 20 2a 2f 0a 20  es in token */. 
2ce1d 20 69 6e 74 20 2a 70 69 53 74 61 72 74 4f 66 66   int *piStartOff
2ce1e 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  set,            
2ce1f 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61       /* OUT: Sta
2ce20 72 74 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20  rting offset of 
2ce21 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a  token */.  int *
2ce22 70 69 45 6e 64 4f 66 66 73 65 74 2c 20 20 20 20  piEndOffset,    
2ce23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2ce24 2a 20 4f 55 54 3a 20 45 6e 64 69 6e 67 20 6f 66  * OUT: Ending of
2ce25 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f  fset of token */
2ce26 0a 20 20 69 6e 74 20 2a 70 69 50 6f 73 69 74 69  .  int *piPositi
2ce27 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  on              
2ce28 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50         /* OUT: P
2ce29 6f 73 69 74 69 6f 6e 20 69 6e 74 65 67 65 72 20  osition integer 
2ce2a 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20  of token */.){. 
2ce2b 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65   simple_tokenize
2ce2c 72 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 73  r_cursor *c = (s
2ce2d 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f  imple_tokenizer_
2ce2e 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f  cursor *) pCurso
2ce2f 72 3b 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65  r;.  simple_toke
2ce30 6e 69 7a 65 72 20 2a 74 20 3d 20 28 73 69 6d 70  nizer *t = (simp
2ce31 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 20  le_tokenizer *) 
2ce32 70 43 75 72 73 6f 72 2d 3e 70 54 6f 6b 65 6e 69  pCursor->pTokeni
2ce33 7a 65 72 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  zer;.  unsigned 
2ce34 63 68 61 72 20 2a 70 20 3d 20 28 75 6e 73 69 67  char *p = (unsig
2ce35 6e 65 64 20 63 68 61 72 20 2a 29 63 2d 3e 70 49  ned char *)c->pI
2ce36 6e 70 75 74 3b 0a 0a 20 20 77 68 69 6c 65 28 20  nput;..  while( 
2ce37 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d 3e 6e 42  c->iOffset<c->nB
2ce38 79 74 65 73 20 29 7b 0a 20 20 20 20 69 6e 74 20  ytes ){.    int 
2ce39 69 53 74 61 72 74 4f 66 66 73 65 74 3b 0a 0a 20  iStartOffset;.. 
2ce3a 20 20 20 2f 2a 20 53 63 61 6e 20 70 61 73 74 20     /* Scan past 
2ce3b 64 65 6c 69 6d 69 74 65 72 20 63 68 61 72 61 63  delimiter charac
2ce3c 74 65 72 73 20 2a 2f 0a 20 20 20 20 77 68 69 6c  ters */.    whil
2ce3d 65 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d  e( c->iOffset<c-
2ce3e 3e 6e 42 79 74 65 73 20 26 26 20 73 69 6d 70 6c  >nBytes && simpl
2ce3f 65 44 65 6c 69 6d 28 74 2c 20 70 5b 63 2d 3e 69  eDelim(t, p[c->i
2ce40 4f 66 66 73 65 74 5d 29 20 29 7b 0a 20 20 20 20  Offset]) ){.    
2ce41 20 20 63 2d 3e 69 4f 66 66 73 65 74 2b 2b 3b 0a    c->iOffset++;.
2ce42 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f      }..    /* Co
2ce43 75 6e 74 20 6e 6f 6e 2d 64 65 6c 69 6d 69 74 65  unt non-delimite
2ce44 72 20 63 68 61 72 61 63 74 65 72 73 2e 20 2a 2f  r characters. */
2ce45 0a 20 20 20 20 69 53 74 61 72 74 4f 66 66 73 65  .    iStartOffse
2ce46 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74 3b 0a  t = c->iOffset;.
2ce47 20 20 20 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f      while( c->iO
2ce48 66 66 73 65 74 3c 63 2d 3e 6e 42 79 74 65 73 20  ffset<c->nBytes 
2ce49 26 26 20 21 73 69 6d 70 6c 65 44 65 6c 69 6d 28  && !simpleDelim(
2ce4a 74 2c 20 70 5b 63 2d 3e 69 4f 66 66 73 65 74 5d  t, p[c->iOffset]
2ce4b 29 20 29 7b 0a 20 20 20 20 20 20 63 2d 3e 69 4f  ) ){.      c->iO
2ce4c 66 66 73 65 74 2b 2b 3b 0a 20 20 20 20 7d 0a 0a  ffset++;.    }..
2ce4d 20 20 20 20 69 66 28 20 63 2d 3e 69 4f 66 66 73      if( c->iOffs
2ce4e 65 74 3e 69 53 74 61 72 74 4f 66 66 73 65 74 20  et>iStartOffset 
2ce4f 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 2c 20  ){.      int i, 
2ce50 6e 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74 2d 69  n = c->iOffset-i
2ce51 53 74 61 72 74 4f 66 66 73 65 74 3b 0a 20 20 20  StartOffset;.   
2ce52 20 20 20 69 66 28 20 6e 3e 63 2d 3e 6e 54 6f 6b     if( n>c->nTok
2ce53 65 6e 41 6c 6c 6f 63 61 74 65 64 20 29 7b 0a 20  enAllocated ){. 
2ce54 20 20 20 20 20 20 20 63 2d 3e 6e 54 6f 6b 65 6e         c->nToken
2ce55 41 6c 6c 6f 63 61 74 65 64 20 3d 20 6e 2b 32 30  Allocated = n+20
2ce56 3b 0a 20 20 20 20 20 20 20 20 63 2d 3e 70 54 6f  ;.        c->pTo
2ce57 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f 72 65  ken = sqlite3_re
2ce58 61 6c 6c 6f 63 28 63 2d 3e 70 54 6f 6b 65 6e 2c  alloc(c->pToken,
2ce59 20 63 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61   c->nTokenAlloca
2ce5a 74 65 64 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ted);.        if
2ce5b 28 20 63 2d 3e 70 54 6f 6b 65 6e 3d 3d 4e 55 4c  ( c->pToken==NUL
2ce5c 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  L ) return SQLIT
2ce5d 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d  E_NOMEM;.      }
2ce5e 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
2ce5f 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  i<n; i++){.     
2ce60 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73     /* TODO(shess
2ce61 29 20 54 68 69 73 20 6e 65 65 64 73 20 65 78 70  ) This needs exp
2ce62 61 6e 73 69 6f 6e 20 74 6f 20 68 61 6e 64 6c 65  ansion to handle
2ce63 20 55 54 46 2d 38 0a 20 20 20 20 20 20 20 20 2a   UTF-8.        *
2ce64 2a 20 63 61 73 65 2d 69 6e 73 65 6e 73 69 74 69  * case-insensiti
2ce65 76 69 74 79 2e 0a 20 20 20 20 20 20 20 20 2a 2f  vity..        */
2ce66 0a 20 20 20 20 20 20 20 20 75 6e 73 69 67 6e 65  .        unsigne
2ce67 64 20 63 68 61 72 20 63 68 20 3d 20 70 5b 69 53  d char ch = p[iS
2ce68 74 61 72 74 4f 66 66 73 65 74 2b 69 5d 3b 0a 20  tartOffset+i];. 
2ce69 20 20 20 20 20 20 20 63 2d 3e 70 54 6f 6b 65 6e         c->pToken
2ce6a 5b 69 5d 20 3d 20 63 68 3c 30 78 38 30 20 3f 20  [i] = ch<0x80 ? 
2ce6b 74 6f 6c 6f 77 65 72 28 63 68 29 20 3a 20 63 68  tolower(ch) : ch
2ce6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2ce6d 2a 70 70 54 6f 6b 65 6e 20 3d 20 63 2d 3e 70 54  *ppToken = c->pT
2ce6e 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 2a 70 6e 42  oken;.      *pnB
2ce6f 79 74 65 73 20 3d 20 6e 3b 0a 20 20 20 20 20 20  ytes = n;.      
2ce70 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74 20 3d  *piStartOffset =
2ce71 20 69 53 74 61 72 74 4f 66 66 73 65 74 3b 0a 20   iStartOffset;. 
2ce72 20 20 20 20 20 2a 70 69 45 6e 64 4f 66 66 73 65       *piEndOffse
2ce73 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74 3b 0a  t = c->iOffset;.
2ce74 20 20 20 20 20 20 2a 70 69 50 6f 73 69 74 69 6f        *piPositio
2ce75 6e 20 3d 20 63 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b  n = c->iToken++;
2ce76 0a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ..      return S
2ce77 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
2ce78 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
2ce79 49 54 45 5f 44 4f 4e 45 3b 0a 7d 0a 0a 2f 2a 0a  ITE_DONE;.}../*.
2ce7a 2a 2a 20 54 68 65 20 73 65 74 20 6f 66 20 72 6f  ** The set of ro
2ce7b 75 74 69 6e 65 73 20 74 68 61 74 20 69 6d 70 6c  utines that impl
2ce7c 65 6d 65 6e 74 20 74 68 65 20 73 69 6d 70 6c 65  ement the simple
2ce7d 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74   tokenizer.*/.st
2ce7e 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
2ce7f 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
2ce80 75 6c 65 20 73 69 6d 70 6c 65 54 6f 6b 65 6e 69  ule simpleTokeni
2ce81 7a 65 72 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20 20  zerModule = {.  
2ce82 30 2c 0a 20 20 73 69 6d 70 6c 65 43 72 65 61 74  0,.  simpleCreat
2ce83 65 2c 0a 20 20 73 69 6d 70 6c 65 44 65 73 74 72  e,.  simpleDestr
2ce84 6f 79 2c 0a 20 20 73 69 6d 70 6c 65 4f 70 65 6e  oy,.  simpleOpen
2ce85 2c 0a 20 20 73 69 6d 70 6c 65 43 6c 6f 73 65 2c  ,.  simpleClose,
2ce86 0a 20 20 73 69 6d 70 6c 65 4e 65 78 74 2c 0a 7d  .  simpleNext,.}
2ce87 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  ;../*.** Allocat
2ce88 65 20 61 20 6e 65 77 20 73 69 6d 70 6c 65 20 74  e a new simple t
2ce89 6f 6b 65 6e 69 7a 65 72 2e 20 20 52 65 74 75 72  okenizer.  Retur
2ce8a 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
2ce8b 68 65 20 6e 65 77 0a 2a 2a 20 74 6f 6b 65 6e 69  he new.** tokeni
2ce8c 7a 65 72 20 69 6e 20 2a 70 70 4d 6f 64 75 6c 65  zer in *ppModule
2ce8d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2ce8e 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
2ce8f 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a  ts3SimpleTokeniz
2ce90 65 72 4d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69  erModule(.  sqli
2ce91 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
2ce92 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f  dule const**ppMo
2ce93 64 75 6c 65 0a 29 7b 0a 20 20 2a 70 70 4d 6f 64  dule.){.  *ppMod
2ce94 75 6c 65 20 3d 20 26 73 69 6d 70 6c 65 54 6f 6b  ule = &simpleTok
2ce95 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b 0a 7d 0a  enizerModule;.}.
2ce96 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
2ce97 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29  ned(SQLITE_CORE)
2ce98 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
2ce99 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20  TE_ENABLE_FTS3) 
2ce9a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
2ce9b 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f  *** End of fts3_
2ce9c 74 6f 6b 65 6e 69 7a 65 72 31 2e 63 20 2a 2a 2a  tokenizer1.c ***
2ce9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ce9e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ce9f 2a 2a 2f 0a                                      **/.